From: Matthias Kruk Date: Mon, 2 Dec 2019 08:12:40 +0000 (+0900) Subject: Implement sbrk() POSIX syscall X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=67e8287d7033a2e9dac0cf8ff2ef1f729f269720;p=corax Implement sbrk() POSIX syscall - Implement sys_sbrk() syscall handler - Add brk() and sbrk() syscall stubs to klibc - Add prototypes for brk() and sbrk() to unistd.h --- diff --git a/include/corax/syscall.h b/include/corax/syscall.h index d8e20df..ba39618 100644 --- a/include/corax/syscall.h +++ b/include/corax/syscall.h @@ -23,6 +23,7 @@ #define SYS_WAITID 5 #define SYS_SETSID 6 #define SYS_SLEEP 7 +#define SYS_SBRK 8 /* Corax-specific variations of POSIX syscalls */ #define SYS_EXECFVE 128 diff --git a/include/unistd.h b/include/unistd.h index 5c9d929..4b87539 100644 --- a/include/unistd.h +++ b/include/unistd.h @@ -8,5 +8,7 @@ extern pid_t fork(void); extern pid_t vfork(void); extern int execfve(void*, const char**, const char**); extern unsigned int sleep(unsigned int); +extern int brk(void*); +extern void* sbrk(int); #endif /* _UNISTD_H */ diff --git a/kernel/core/posixcall.c b/kernel/core/posixcall.c index 76a8fcc..8d3985d 100644 --- a/kernel/core/posixcall.c +++ b/kernel/core/posixcall.c @@ -49,6 +49,27 @@ int sys_fork(stack_frame_t *stk) return(ret_val); } +int sys_sbrk(ssize_t increment) +{ + process_t *cproc; + pg_dir_t *pgdir; + int ret_val; + + cproc = process_get_current(); + + process_lock(cproc); + + ret_val = process_get_pagedir(cproc, &pgdir); + + if(!ret_val) { + ret_val = (int)pg_dir_sbrk(pgdir, increment); + } + + process_unlock(cproc); + + return(ret_val); +} + #if FEATURE(ELF) int sys_execelf(stack_frame_t *stk) { @@ -366,6 +387,10 @@ int sys_posixcall(stack_frame_t *stk) ret_val = sys_sleep((unsigned int)stk->ebx); break; + case SYS_SBRK: + ret_val = sys_sbrk((ssize_t)stk->ebx); + break; + case SYS_EXECFVE: ret_val = sys_execfve(stk); break; diff --git a/kernel/klibc/posixcall.S b/kernel/klibc/posixcall.S index 0b455c2..f54608b 100644 --- a/kernel/klibc/posixcall.S +++ b/kernel/klibc/posixcall.S @@ -9,6 +9,8 @@ .global fork .global execfve + .global brk + .global sbrk fork: pushl %ebx @@ -31,4 +33,32 @@ execfve: popl %ebx ret +brk: + pushl $0 + call sbrk + + /* + * The eax register contains the current sbrk, 8(%esp) the desired sbrk. + * We have to pass the difference to sbrk() to increase or decrease it. + */ + movl 8(%esp), %edx + subl %eax, %edx + + /* sbrk(8(%esp) - sbrk(0)) */ + movl %edx, (%esp) + call sbrk + + addl $4, %esp + ret + +sbrk: + pushl %ebx + + movl $SYS_SBRK, %eax + movl 8(%esp), %ebx + int $SYSCALL_POSIX + + popl %ebx + ret + #endif /* FEATURE(POSIX) */