diff --git a/sched/entry.S b/sched/entry.S index 06fbc8f..6261a89 100644 --- a/sched/entry.S +++ b/sched/entry.S @@ -213,6 +213,78 @@ gate_syscall: .global gate_syscall_int80 gate_syscall_int80: cli + /* stack frame */ + pushq %rbp + movq %rsp, %rbp + + subq $88, %rsp + + /* save registers */ + movq %rdi, -8(%rbp) + movq %rsi, -16(%rbp) + movq %rdx, -24(%rbp) + movq %rcx, -32(%rbp) + movq %r8, -40(%rbp) + movq %r9, -48(%rbp) + movq %r10, -56(%rbp) + movq %r11, -64(%rbp) + movq %rax, -72(%rbp) + + /* save context */ + call proc_current_regs + movq -72(%rbp), %rdi /* rax */ + movq %rdi, 0(%rax) + movq %rbx, 8(%rax) /* rbx */ + movq -32(%rbp), %rdi /* rcx */ + movq %rdi, 16(%rax) + movq -24(%rbp), %rdi /* rdx */ + movq %rdi, 24(%rax) + movq -16(%rbp), %rdi /* rsi */ + movq %rdi, 32(%rax) + movq -8(%rbp), %rdi /* rdi */ + movq %rdi, 40(%rax) + movq (%rbp), %rdi + movq %rdi, 48(%rax) /* rbp */ + movq -40(%rbp), %rdi /* r8 */ + movq %rdi, 56(%rax) + movq -48(%rbp), %rdi /* r9 */ + movq %rdi, 64(%rax) + movq -56(%rbp), %rdi /* r10 */ + movq %rdi, 72(%rax) + movq -64(%rbp), %rdi /* r11 */ + movq %rdi, 80(%rax) + movq %r12, 88(%rax) + movq %r13, 96(%rax) + movq %r14, 104(%rax) + movq %r15, 112(%rax) + + + /* save IRETQ frame */ + movq 8(%rbp), %rdi + movq %rdi, 120(%rax) /* rip */ + movq 16(%rbp), %rdi + movq %rdi, 128(%rax) /* cs */ + movq 24(%rbp), %rdi + movq %rdi, 136(%rax) /* rflags */ + movq 32(%rbp), %rdi + movq %rdi, 144(%rax) /* rsp */ + movq 40(%rbp), %rdi + movq %rdi, 152(%rax) /* ss */ + + /* restore argument registers */ + movq 40(%rax), %rdi + movq 32(%rax), %rsi + movq 24(%rax), %rdx + movq 72(%rax), %r10 + movq 56(%rax), %r8 + movq 0(%rax), %rax + + /* leave stack frame */ + addq $88, %rsp + popq %rbp + + + movq %r10, %rcx movq %rax, %r9 diff --git a/sched/proc.c b/sched/proc.c index b497933..0cc089c 100644 --- a/sched/proc.c +++ b/sched/proc.c @@ -277,48 +277,6 @@ struct proc *proc_create() return proc; } -struct proc *proc_fork(struct proc *parent) -{ - struct proc *proc = proc_create(); - - proc_clone_mmap(parent, proc); - - /* copy file descriptors */ - struct rbnode *node = rbt_minimum(parent->fd_map.root); - while (node) { - struct file_descriptor *fdesc = (void *)node->value; - struct file *file = fdesc->file; - - struct file_descriptor *new_fdesc = slab_alloc(fd_slab); - new_fdesc->fd = fdesc->fd; - new_fdesc->file = file; - new_fdesc->pos = fdesc->pos; - new_fdesc->flags = fdesc->flags; - - struct rbnode *new_node = rbt_insert(&proc->fd_map, fdesc->fd); - new_node->value = (uintptr_t)new_fdesc; - - node = rbt_successor(node); - } - - /* copy parent */ - proc->parent = parent; - - /* copy children */ - node = rbt_minimum(parent->children.root); - while (node) { - struct proc *child = (void *)node->value; - struct proc *new_child = proc_fork(child); - - struct rbnode *new_node = rbt_insert(&proc->children, child->pid); - new_node->value = (uintptr_t)new_child; - - node = rbt_successor(node); - } - - return proc; -} - struct proc *proc_get(pid_t pid) { struct rbnode *proc_node = rbt_search(proc_tree, pid); diff --git a/sched/syscall.c b/sched/syscall.c index 2d8ad38..c885bf4 100644 --- a/sched/syscall.c +++ b/sched/syscall.c @@ -59,7 +59,6 @@ ssize_t sys_read(int fd, void *buf, size_t count) if (tmp_buf == NULL) return -ENOMEM; - ssize_t ret = vfs_read(file, tmp_buf, fdesc->pos, count); proc->state = PROC_RUNNING; @@ -153,17 +152,35 @@ void sys_exit(int status) pid_t sys_fork() { - struct proc *proc = proc_find(getpid()); - if (proc == NULL) - return -1; + struct proc *parent = proc_find(getpid()); + struct proc *proc = proc_create(); - struct proc *new_proc = proc_fork(proc); - if (new_proc == NULL) - return -1; + proc_clone_mmap(parent, proc); - sys_set_return(new_proc, 0); + /* copy file descriptors */ + struct rbnode *node = rbt_minimum(parent->fd_map.root); + while (node) { + struct file_descriptor *fdesc = (void *)node->value; + struct file *file = fdesc->file; - return new_proc->pid; + struct file_descriptor *new_fdesc = slab_alloc(fd_slab); + new_fdesc->fd = fdesc->fd; + new_fdesc->file = file; + new_fdesc->pos = fdesc->pos; + new_fdesc->flags = fdesc->flags; + + struct rbnode *new_node = rbt_insert(&proc->fd_map, fdesc->fd); + new_node->value = (uintptr_t)new_fdesc; + + node = rbt_successor(node); + } + + /* copy parent */ + proc->parent = parent; + proc->regs = parent->regs; + sys_set_return(proc, 0); + + return proc->pid; } static void syscall_insert(uint64_t syscall_no, syscall_t syscall)