fork: properly set up child process
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
parent
ec4e9fc27d
commit
b926d50365
6 changed files with 73 additions and 25 deletions
|
|
@ -121,6 +121,7 @@ _syscall_enter:
|
|||
swapgs
|
||||
sysretq
|
||||
|
||||
/* called from the scheduler to switch processes */
|
||||
.global switch_to
|
||||
switch_to:
|
||||
push_all
|
||||
|
|
@ -129,6 +130,7 @@ switch_to:
|
|||
pop_all
|
||||
ret
|
||||
|
||||
/* return path after a process was execed */
|
||||
.global _enter_userspace_init
|
||||
_enter_userspace_init:
|
||||
mov rcx, rdi
|
||||
|
|
@ -153,6 +155,14 @@ _enter_userspace_init:
|
|||
|
||||
sysretq
|
||||
|
||||
/* set as entrypoint for a new process that was just forked */
|
||||
.global fork_trampoline
|
||||
fork_trampoline:
|
||||
mov rsp, rdx
|
||||
swapgs
|
||||
sysretq
|
||||
|
||||
|
||||
/* exceptions */
|
||||
exception 0x00
|
||||
exception 0x01
|
||||
|
|
|
|||
|
|
@ -52,7 +52,7 @@ void enter_userspace_init(struct thread *thread, uaddr_t entry)
|
|||
_enter_userspace_init(entry, 0x202, thread->ustack_top);
|
||||
}
|
||||
|
||||
void _enter_userspace_init(uaddr_t entry, u64 flags, uaddr_t stack_top);
|
||||
void fork_trampoline();
|
||||
|
||||
pid_t sys_fork()
|
||||
{
|
||||
|
|
@ -69,18 +69,17 @@ pid_t sys_fork()
|
|||
vaddr_t ktop = (vaddr_t)kstack + KSTACK_SIZE;
|
||||
child->kstack_top = ktop;
|
||||
|
||||
/* switch_to's pop_all restores rax..r15, then ret returns.
|
||||
* Set up so child resumes via _enter_userspace_init(entry, flags, stack_top)
|
||||
* with rax=0 (fork return value).
|
||||
/* Copy parent's register state, then:
|
||||
* rax = 0 (child fork return value)
|
||||
* rdx = parent_r->rsp (user stack for fork_trampoline's mov rsp, rdx)
|
||||
* ret_addr = fork_trampoline (does mov rsp, rdx; swapgs; sysretq)
|
||||
*/
|
||||
struct pt_regs *parent_r = current_pt_regs();
|
||||
struct pt_regs *child_r = (struct pt_regs *)(ktop - sizeof(struct pt_regs));
|
||||
memset(child_r, 0, 15 * sizeof(u64));
|
||||
child_r->rdx = parent_r->rsp; /* rdx = user stack */
|
||||
child_r->rdi = parent_r->rcx; /* rdi = return addr */
|
||||
child_r->rsi = 0x202; /* rsi = flags */
|
||||
/* last address on the stack, *(ktop) is not actually mapped */
|
||||
*(u64 *)(ktop - 8) = (u64)_enter_userspace_init;
|
||||
memcpy(child_r, parent_r, sizeof(struct pt_regs) - sizeof(u64));
|
||||
child_r->rax = 0;
|
||||
child_r->rdx = parent_r->rsp;
|
||||
*(u64 *)(ktop - 8) = (u64)fork_trampoline;
|
||||
child->kstack_ptr = (uaddr_t)child_r;
|
||||
|
||||
child->state = PROC_STATE_RUNNING;
|
||||
|
|
|
|||
|
|
@ -21,6 +21,12 @@ struct timespec get_realtime()
|
|||
return ret;
|
||||
}
|
||||
|
||||
time_t unix_time()
|
||||
{
|
||||
u64 sec = (rdtsc() - timekeeper.boot_tsc) / timekeeper.tsc_hz;
|
||||
return timekeeper.boot_unix_seconds + sec;
|
||||
}
|
||||
|
||||
void time_init()
|
||||
{
|
||||
timekeeper.tsc_hz = tsc_calibrate();
|
||||
|
|
|
|||
|
|
@ -5,11 +5,12 @@
|
|||
#include <kernel/types.h>
|
||||
|
||||
struct timespec {
|
||||
i64 tv_sec;
|
||||
time_t tv_sec;
|
||||
i64 tv_nsec;
|
||||
};
|
||||
|
||||
time_t arch_get_real_time();
|
||||
time_t unix_time();
|
||||
u64 tsc_calibrate();
|
||||
u64 rdtsc();
|
||||
struct timespec get_realtime();
|
||||
|
|
|
|||
|
|
@ -71,6 +71,13 @@ static int copy_parent(struct proc *new, struct proc *parent)
|
|||
|
||||
spinlock_release(&parent->mem_lock);
|
||||
|
||||
if (parent->fd_array_size) {
|
||||
spinlock_acquire(&parent->fd_lock);
|
||||
new->fd_array_size = parent->fd_array_size;
|
||||
new->fdt = kalloc(new->fd_array_size * sizeof(struct file), ALLOC_KERN);
|
||||
spinlock_release(&parent->fd_lock);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -9,31 +9,56 @@ static struct {
|
|||
struct thread *head;
|
||||
struct thread *tail;
|
||||
spinlock_t lock;
|
||||
} _run_queue;
|
||||
} sched_queue;
|
||||
|
||||
void sched_add_thread(struct thread *t)
|
||||
{
|
||||
spinlock_acquire(&_run_queue.lock);
|
||||
spinlock_acquire(&sched_queue.lock);
|
||||
t->next = NULL;
|
||||
if (_run_queue.tail)
|
||||
_run_queue.tail->next = t;
|
||||
_run_queue.tail = t;
|
||||
if (!_run_queue.head)
|
||||
_run_queue.head = t;
|
||||
spinlock_release(&_run_queue.lock);
|
||||
if (sched_queue.tail)
|
||||
sched_queue.tail->next = t;
|
||||
sched_queue.tail = t;
|
||||
if (!sched_queue.head)
|
||||
sched_queue.head = t;
|
||||
spinlock_release(&sched_queue.lock);
|
||||
}
|
||||
|
||||
void sched_purge_proc(struct proc *proc)
|
||||
{
|
||||
spinlock_acquire(&sched_queue.lock);
|
||||
struct thread *t = sched_queue.head;
|
||||
struct thread *p = NULL;
|
||||
while (t) {
|
||||
if (t->proc == proc) {
|
||||
if (p)
|
||||
p->next = t->next;
|
||||
else
|
||||
sched_queue.head = t->next;
|
||||
|
||||
if (!t->next)
|
||||
sched_queue.tail = p;
|
||||
|
||||
t->next = NULL;
|
||||
|
||||
} else {
|
||||
p = t;
|
||||
}
|
||||
t = t->next;
|
||||
}
|
||||
spinlock_release(&sched_queue.lock);
|
||||
}
|
||||
|
||||
static struct thread *dequeue_thread()
|
||||
{
|
||||
spinlock_acquire(&_run_queue.lock);
|
||||
struct thread *t = _run_queue.head;
|
||||
spinlock_acquire(&sched_queue.lock);
|
||||
struct thread *t = sched_queue.head;
|
||||
if (t) {
|
||||
_run_queue.head = t->next;
|
||||
if (!_run_queue.head)
|
||||
_run_queue.tail = NULL;
|
||||
sched_queue.head = t->next;
|
||||
if (!sched_queue.head)
|
||||
sched_queue.tail = NULL;
|
||||
t->next = NULL;
|
||||
}
|
||||
spinlock_release(&_run_queue.lock);
|
||||
spinlock_release(&sched_queue.lock);
|
||||
return t;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue