[sys] Fix fork syscall return values

This commit is contained in:
Kevin Lange 2011-12-15 15:31:18 -06:00
parent dae4083454
commit 37c3ab67ce
5 changed files with 27 additions and 9 deletions

View File

@ -67,7 +67,7 @@ DECL_SYSCALL0(getpid);
DECL_SYSCALL3(execve, char *, char **, char **);
DECL_SYSCALL0(fork);
DECL_SYSCALL2(kill, int, int);
DECL_SYSCALL1(wait, unsigned int);
DECL_SYSCALL1(wait, int);
/* Memory management */
DECL_SYSCALL1(sbrk, int);

View File

@ -207,8 +207,8 @@ irq_common_stub:
global read_eip
read_eip: ; Clever girl
pop eax
jmp eax
pop eax
jmp eax
global copy_page_physical
copy_page_physical:

View File

@ -85,7 +85,11 @@ static int write(int fd, char * ptr, int len) {
return out;
}
static int wait(unsigned int child) {
static int wait(int child) {
if (child < 1) {
kprintf("lol nope\n");
return 0;
}
process_t * volatile child_task = process_from_pid(child);
/* If the child task doesn't exist, bail */
if (!child_task) return -1;
@ -248,6 +252,9 @@ syscall_handler(
}
uintptr_t location = syscalls[r->eax];
/* In case of a fork, we need to return the PID to the correct place */
volatile uintptr_t stack = current_process->image.stack - KERNEL_STACK_SIZE;
uint32_t ret;
asm volatile (
"push %1\n"
@ -262,5 +269,10 @@ syscall_handler(
"pop %%ebx\n"
"pop %%ebx\n"
: "=a" (ret) : "r" (r->edi), "r" (r->esi), "r" (r->edx), "r" (r->ecx), "r" (r->ebx), "r" (location));
volatile uintptr_t n_stack = current_process->image.stack - KERNEL_STACK_SIZE;
if (n_stack != stack) {
uintptr_t temp = ((uintptr_t)r - stack);
r = (struct regs *)(n_stack + temp);
}
r->eax = ret;
}

View File

@ -116,6 +116,9 @@ fork() {
/* Disable interrupts */
IRQ_OFF;
unsigned int magic = 0xDEADBEEF;
uintptr_t esp, ebp, eip;
/* Make a pointer to the parent process (us) on the stack */
process_t * parent = (process_t *)current_process;
/* Clone the current process' page directory */
@ -124,14 +127,13 @@ fork() {
process_t * new_proc = spawn_process(current_process);
/* Set the new process' page directory to clone */
set_process_environment(new_proc, directory);
/* Read the instruction pointer */
uintptr_t eip = read_eip();
eip = read_eip();
if (current_process == parent) {
/* Returned as the parent */
uintptr_t esp;
uintptr_t ebp;
/* Verify magic */
assert(magic == 0xDEADBEEF && "Bad process fork magic (parent)!");
/* Collect the stack and base pointers */
asm volatile ("mov %%esp, %0" : "=r" (esp));
asm volatile ("mov %%ebp, %0" : "=r" (ebp));
@ -145,15 +147,19 @@ fork() {
}
/* Copy the kernel stack from this process to new process */
memcpy((void *)(new_proc->image.stack - KERNEL_STACK_SIZE), (void *)(current_process->image.stack - KERNEL_STACK_SIZE), KERNEL_STACK_SIZE);
/* Set the new process instruction pointer (to the return from read_eip) */
new_proc->thread.eip = eip;
/* Add the new process to the ready queue */
make_process_ready(new_proc);
/* Reenable interrupts */
IRQ_ON;
/* Return the child PID */
return new_proc->id;
} else {
assert(magic == 0xDEADBEEF && "Bad process fork magic (child)!");
/* Child fork is complete, return */
return 0;
}

View File

@ -12,4 +12,4 @@ DEFN_SYSCALL0(fork, 8)
DEFN_SYSCALL0(getpid, 9)
DEFN_SYSCALL1(sbrk, 10, int)
DEFN_SYSCALL1(wait, 16, unsigned int)
DEFN_SYSCALL1(wait, 17, int)