AquilaOS documentation
How it works, and its code
Toggle main menu visibility
Loading...
Searching...
No Matches
fork.c
Go to the documentation of this file.
1
4
5
#include <
core/system.h
>
6
#include <
core/string.h
>
7
#include <
core/arch.h
>
8
9
#include <
sys/proc.h
>
10
#include <
sys/sched.h
>
11
12
#include <aquila/errno.h>
13
14
MALLOC_DECLARE
(M_X86_FPU);
15
16
/*
17
* Sets up the x86 state of a fork's child thread: a copy of the parent's
18
* kernel stack and FPU state, and a start at `x86_fork_return`, which
19
* returns to user mode through the fork system call's frame, in the copy.
20
*/
21
int
arch_proc_fork
(
struct
thread
*
thread
,
struct
proc
*fork)
22
{
23
int
err = 0;
24
25
struct
x86_thread
*ptarch =
thread
->
arch
;
26
struct
x86_thread
*ftarch =
NULL
;
27
28
ftarch =
kmalloc
(
sizeof
(
struct
x86_thread
), &M_X86_THREAD,
M_ZERO
);
29
30
if
(ftarch ==
NULL
) {
31
/* no memory for the child thread's x86 state */
32
err = -ENOMEM;
33
goto
free_resources;
34
}
35
36
/* the child's kernel stack */
37
uintptr_t
fkstack_base = (
uintptr_t
)
kmalloc
(
KERN_STACK_SIZE
, &M_KERN_STACK, 0);
38
39
if
(!fkstack_base) {
40
err = -ENOMEM;
41
goto
free_resources;
42
}
43
44
/* a copy of the parent's kernel stack (in a signal handler, the
45
* context it interrupted is at the top, and kstack below it) */
46
ftarch->
kstack_base
= fkstack_base;
47
ftarch->
kstack
= fkstack_base + (ptarch->
kstack
- ptarch->
kstack_base
);
48
memcpy
((
void
*) fkstack_base, (
void
*) ptarch->
kstack_base
,
KERN_STACK_SIZE
);
49
50
/* the fork system call's registers, at the same place in the copy */
51
struct
x86_regs
*fork_regs = (
void
*) (fkstack_base + ((
uintptr_t
) ptarch->
regs
- ptarch->
kstack_base
));
52
ftarch->
regs
= fork_regs;
53
54
extern
void
x86_fork_return();
55
#if ARCH_BITS==32
56
ftarch->eip = (
uintptr_t
) x86_fork_return;
57
ftarch->esp = (
uintptr_t
) fork_regs;
58
ftarch->eflags = ptarch->eflags;
59
#else
60
ftarch->
rip
= (
uintptr_t
) x86_fork_return;
61
ftarch->
rsp
= (
uintptr_t
) fork_regs;
62
ftarch->
rflags
= ptarch->
rflags
;
63
#endif
64
65
/* the FPU's state as the parent has it (its registers, MXCSR, the
66
* control word), if it used the FPU: the child loads it at its first
67
* use */
68
ftarch->
fpu_enabled
= 0;
69
ftarch->
fpu_context
=
NULL
;
70
71
if
(ptarch->
fpu_enabled
) {
72
if
(!(ftarch->
fpu_context
=
kmalloc
(512, &M_X86_FPU, 0))) {
73
err
= -ENOMEM;
74
goto
free_resources;
75
}
76
77
ftarch->
fpu_enabled
=
x86_fpu_save_to
(ftarch->
fpu_context
);
78
}
79
80
struct
thread
*fthread = (
struct
thread
*) fork->
threads
.
head
->
value
;
81
fthread->
arch
= ftarch;
82
83
return
0;
84
85
free_resources:
86
if
(ftarch) {
87
if
(ftarch->
kstack_base
)
88
kfree
((
void
*) ftarch->
kstack_base
);
89
kfree
(ftarch);
90
}
91
return
err;
92
}
KERN_STACK_SIZE
#define KERN_STACK_SIZE
Definition
arch.h:68
arch_proc_fork
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
x86_fpu_save_to
int x86_fpu_save_to(void *buf)
Copies the current thread's FPU state, for a signal handler's frame (sys/signal.c).
Definition
fpu.c:198
arch.h
system.h
kmalloc
void * kmalloc(size_t size, struct malloc_type *type, int flags)
Allocates memory for one of the kernel's structures.
Definition
kvmem.c:123
MALLOC_DECLARE
#define MALLOC_DECLARE(type)
Definition
kvmem.h:29
M_ZERO
#define M_ZERO
Definition
kvmem.h:26
kfree
void kfree(void *)
Frees an object that kmalloc() gave.
Definition
kvmem.c:146
memcpy
void * memcpy(void *dst, const void *src, size_t n)
Copies n bytes; dst and src must not overlap.
Definition
libc.c:11
proc.h
sched.h
NULL
#define NULL
Definition
stddef.h:27
uintptr_t
__SIZE_TYPE__ uintptr_t
Definition
stddef.h:12
string.h
proc
A process.
Definition
proc.h:75
proc::threads
struct queue threads
its threads
Definition
proc.h:136
qnode::value
void * value
Definition
queue.h:20
queue::head
struct qnode * head
Definition
queue.h:31
thread
A thread.
Definition
thread.h:23
thread::arch
void * arch
the architecture's own data: its registers, its kernel stack
Definition
thread.h:84
x86_regs
The registers an interrupt or exception saved, on the kernel stack, as cpu.S's stubs push them: the g...
Definition
cpu.h:12
x86_regs::err
uint64_t err
Definition
cpu.h:23
x86_thread
A thread's x86 state: its kernel stack, and where it resumes.
Definition
arch.h:16
x86_thread::kstack_base
uintptr_t kstack_base
Definition
arch.h:18
x86_thread::rip
uintptr_t rip
Definition
arch.h:27
x86_thread::fpu_enabled
int fpu_enabled
Definition
arch.h:37
x86_thread::fpu_context
void * fpu_context
Definition
arch.h:35
x86_thread::rflags
uintptr_t rflags
Definition
arch.h:30
x86_thread::rsp
uintptr_t rsp
Definition
arch.h:28
x86_thread::regs
struct x86_regs * regs
Definition
arch.h:34
x86_thread::kstack
uintptr_t kstack
Definition
arch.h:17
kernel
arch
i386
sys
fork.c
Generated by
1.18.0