-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathprocess.c
97 lines (83 loc) · 1.77 KB
/
process.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "process.h"
#include "assembly_interface.h"
#include "data_structures/page_table.h"
#include "memory.h"
#include "kernel_stdio.h"
#include "stdlib.h"
uint32_t next_pid = 0;
struct process_t {
uint32_t pid;
page_directory_t pd;
};
void create_process(struct file_t* file) {
struct process_t* p = malloc(sizeof(struct process_t));
p->pid = next_pid;
next_pid += 1;
page_in(TMP_PAGE_0);
page_directory_t pd = TMP_PAGE_0;
p->pd = pd;
void* pd_physical_address = virtual_to_physical(pd);
uint32_t pde = make_page_directory_entry(
pd_physical_address,
FOUR_KB,
false,
false,
SUPERVISOR,
READ_WRITE,
true
);
pd[1023] = pde;
page_in(TMP_PAGE_1);
page_table_t code_pt = TMP_PAGE_1;
pde = make_page_directory_entry(
virtual_to_physical(code_pt),
FOUR_KB,
false,
false,
USER,
READ_ONLY,
true
);
pd[0] = pde;
page_in(TMP_PAGE_2);
void* user_code = TMP_PAGE_2;
uint32_t pte = make_page_table_entry(
virtual_to_physical(user_code),
false,
false,
false,
USER,
READ_ONLY,
true
);
code_pt[0] = pte;
page_in(TMP_PAGE_3);
page_table_t stack_pt = TMP_PAGE_3;
pde = make_page_directory_entry(
virtual_to_physical(stack_pt),
FOUR_KB,
false,
false,
USER,
READ_WRITE,
true
);
pd[page_directory_offset(UPPER_GB_START) - 1] = pde;
page_in(TMP_PAGE_4);
pte = make_page_table_entry(
virtual_to_physical(TMP_PAGE_4),
false,
false,
false,
USER,
READ_WRITE,
true
);
stack_pt[1023] = pte;
map_kernel_into_page_directory(pd);
for (uint32_t i = 0; i < file->size; i++) {
((char*)user_code)[i] = (file->bytes)[i];
}
set_page_directory(virtual_to_physical(pd));
enter_user_mode();
}