AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
fork.c
Go to the documentation of this file.
1
6
7#include <core/system.h>
8#include <core/string.h>
9#include <core/arch.h>
10#include <sys/proc.h>
11#include <sys/sched.h>
12
13#include <aquila/errno.h>
14
16
17/*
18 * Sets up the child's thread of a `fork()`: its kernel stack, its saved
19 * registers (its parent's), and its FP/SIMD state.
20 */
21int arch_proc_fork(struct thread *thread, struct proc *fork)
22{
23 struct aarch64_thread *parch = thread->arch;
24 struct aarch64_thread *farch = aarch64_thread_new();
25
26 if (!farch)
27 return -ENOMEM;
28
29 /* a copy of the fork system call's registers, at the top of its stack
30 * (where user mode's next exception lands) */
31 struct aarch64_regs *regs = (struct aarch64_regs *) (farch->kstack - sizeof(struct aarch64_regs));
32 *regs = *parch->regs;
33 farch->regs = regs;
34
35 /* when it's first chosen, it starts at aarch64_fork_return
36 * (cpu/vectors.S), which returns to user mode from that frame */
37 extern void aarch64_fork_return(void);
38 farch->ctx.lr = (uintptr_t) aarch64_fork_return;
39 farch->ctx.sp = (uintptr_t) regs;
40 farch->ctx.fp = 0;
41
42 /* its FP/SIMD registers are its parent's */
43 if (aarch64_fpsimd_fork(thread, farch)) {
44 kfree((void *) farch->kstack_base);
45 kfree(farch);
46 return -ENOMEM;
47 }
48
49 struct thread *fthread = (struct thread *) fork->threads.head->value;
50 fthread->arch = farch;
51
52 return 0;
53}
struct aarch64_thread * aarch64_thread_new(void)
Makes a thread's aarch64 state, with its kernel stack.
Definition thread.c:30
int arch_proc_fork(struct thread *thread, struct proc *fork)
Sets up the CPU state of a fork() child's thread: a copy of the parent's kernel stack,...
Definition fork.c:21
int aarch64_fpsimd_fork(struct thread *parent, struct aarch64_thread *child)
Gives a child of fork() a copy of its parent's FP/SIMD state, if the parent has one.
Definition fpsimd.c:129
void kfree(void *)
Frees an object that kmalloc() gave.
Definition kvmem.c:146
__SIZE_TYPE__ uintptr_t
Definition stddef.h:12
uint64_t lr
Definition arch.h:16
uint64_t sp
Definition arch.h:17
uint64_t fp
Definition arch.h:15
What an exception saves on the kernel stack (cpu/vectors.S): the context it interrupted.
Definition cpu.h:12
A thread's aarch64 state (thread->arch).
Definition arch.h:25
struct aarch64_ctx ctx
Definition arch.h:29
uintptr_t kstack_base
Definition arch.h:27
uintptr_t kstack
Definition arch.h:26
struct aarch64_regs * regs
Definition arch.h:33
A process.
Definition proc.h:75
struct queue threads
its threads
Definition proc.h:136
void * value
Definition queue.h:20
struct qnode * head
Definition queue.h:31
A thread.
Definition thread.h:23
void * arch
the architecture's own data: its registers, its kernel stack
Definition thread.h:84