Skip to content

Commit

Permalink
Merge pull request #2106 from ghaerr/fmemfree
Browse files Browse the repository at this point in the history
[kernel,libc] Add _fmemfree system call and wrapper functions
  • Loading branch information
ghaerr authored Nov 18, 2024
2 parents 9c42710 + ce44308 commit 6df0e7d
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 2 deletions.
3 changes: 2 additions & 1 deletion elks/arch/i86/kernel/strace.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,5 +171,6 @@ struct sc_info elks_table2[] = {
ENTRY("connect", packinfo(3, P_SSHORT, P_PDATA, P_SSHORT )),
ENTRY("setsockopt", packinfo(5, P_SSHORT, P_SSHORT, P_SSHORT )), /* +2 args*/
ENTRY("getsocknam", packinfo(4, P_SSHORT, P_DATA, P_PUSHORT)), /* +1 arg*/
ENTRY("fmemalloc", packinfo(2, P_USHORT, P_PUSHORT, P_NONE) ), // 206
ENTRY("fmemalloc", packinfo(2, P_USHORT, P_PUSHORT, P_NONE) ),
ENTRY("fmemfree", packinfo(1, P_USHORT, P_NONE, P_NONE) ), // 207
};
1 change: 1 addition & 0 deletions elks/arch/i86/kernel/syscall.dat
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ connect +203 3 = CONFIG_SOCKET
setsockopt +204 5 = CONFIG_SOCKET
getsocknam +205 4 = CONFIG_SOCKET
fmemalloc +206 2 *
fmemfree +207 1 *
#
# Name No Args Flag&comment
#
Expand Down
22 changes: 22 additions & 0 deletions elks/arch/i86/mm/malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,28 @@ int sys_fmemalloc(int paras, unsigned short *pseg)
return 0;
}

// free process allocated memory
int sys_fmemfree(unsigned short segment)
{
list_s *n;

for (n = _seg_all.next; n != &_seg_all; ) {
segment_s * seg = structof (n, segment_s, all);

if (seg->base == segment) {
if (seg->pid == current->pid) {
seg_free(seg);
return 0;
}
debug("sys_fmemfree: not owner %04x\n", segment);
return -EACCES;
}
n = seg->all.next;
}
debug("sys_fmemfree: segment not found %04x\n", segment);
return -EINVAL;
}

// free all program allocated segments for PID pid
void seg_free_pid(pid_t pid)
{
Expand Down
5 changes: 5 additions & 0 deletions elkscmd/man/man2/fmemalloc.2
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ _fmemalloc \- allocate far memory outside process
.nf
.ft B
_fmemalloc(int \fIparas\fP, unsigned short *\fIpseg\fP)
_fmemfree(unsigned short \fIseg\fP)
.ft R
.fi
.SH DESCRIPTION
Expand All @@ -16,6 +17,10 @@ paragraphs of main memory
from outside the process for use using a __far pointer within the application.
The segment address of the memory is stored in the pointer
.IR pseg .
.PP
.B _fmemfree
frees the memory segment allocated by a previous
.BR _fmemalloc .
.SH "RETURN VALUE
The value 0 is returned if no error occurs. Otherwise,
the call returns a negative error number.
Expand Down
7 changes: 6 additions & 1 deletion elkscmd/man/man3/fmemalloc.3
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ fmemalloc \- allocate far memory outside process
#include <stdlib.h>

void __far *fmemalloc(unsigned long \fIsize\fP)
int fmemfree(void __far *\fIptr\fP)
.ft R
.fi
.SH DESCRIPTION
Expand All @@ -19,7 +20,11 @@ within the application.
The
.IR size
parameter will be rounded up to the next paragraph boundary.

.PP
.B fmemfree
frees memory allocated by a prevous
.BR fmemalloc .
.PP
Any allocated memory will be automatically freed on exit from the application.
.SH "RETURN VALUE
The value 0 is returned if no error occurs. Otherwise,
Expand Down
2 changes: 2 additions & 0 deletions libc/include/malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ extern void __wcnear *(*__alloca_alloc)(size_t);

/* alloc from main memory */
void __far *fmemalloc(unsigned long size);
int fmemfree(void __far *ptr);
int _fmemalloc(int paras, unsigned short *pseg);
int _fmemfree(unsigned short seg);

#endif
1 change: 1 addition & 0 deletions libc/include/watcom/syselks.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ typedef int syscall_res;
#define SYS_setsockopt 204
#define SYS_getsocknam 205
#define SYS_fmemalloc 206
#define SYS_fmemfree 207


#define _sys_exit(rc) sys_call1n(SYS_exit, rc)
Expand Down
1 change: 1 addition & 0 deletions libc/malloc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ OBJS = \
realloc.o \
sbrk.o \
fmemalloc.o \
fmemfree.o \

.PHONY: all

Expand Down
7 changes: 7 additions & 0 deletions libc/malloc/fmemfree.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <malloc.h>

/* free from main memory */
int fmemfree(void __far *ptr)
{
return _fmemfree((unsigned short)((unsigned long)ptr >> 16));
}
1 change: 1 addition & 0 deletions libc/watcom/syscall/crt0.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ char **__argv;
char *__program_filename;
char **environ;
unsigned int __stacklow;
unsigned char _HShift = 12; /* huge pointer support required by pia.asm */

static unsigned int _SP(void);
#pragma aux _SP = __value [__sp]
Expand Down
14 changes: 14 additions & 0 deletions libc/watcom/syscall/fmemfree.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/****************************************************************************
*
* Description: ELKS _fmemfree() system call.
*
****************************************************************************/

#include <malloc.h>
#include "watcom/syselks.h"

int _fmemfree( unsigned short __seg )
{
syscall_res res = sys_call1( SYS_fmemfree, __seg);
__syscall_return( int, res );
}

0 comments on commit 6df0e7d

Please sign in to comment.