-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.cpp
296 lines (253 loc) · 8.55 KB
/
process.cpp
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include <cpu_defs.h>
#include <kernel.h>
#include <lib/string.h>
#include <lib/utils.h>
#include <mm/mm.h>
#include <mm/page_alloc.h>
#include <process.h>
#include <syscall.h>
#include <irq.hpp>
#include <elf.h>
#include <kernel-abi/syscall_nr.h>
u64 current_pid = 0;
u64 next_pid = 1;
Process *processes[MAX_PROCESS];
u64 create_process() {
auto pid = next_pid++;
// TODO: optimize with low memory consumption
auto log2size = log2(sizeof(Process)) + 1;
if (log2size < Log2MinSize) {
log2size = Log2MinSize;
}
auto process_mem = kernel_page_alloc(log2size);
auto phy_start = kernel2phy((u64)process_mem);
auto process = new(process_mem) Process(pid, phy_start);
assert(processes[pid] == nullptr, "Process already created");
processes[pid] = process;
return pid;
}
void test_apic();
void rtl8139_test();
void buddy_allocator_usage();
void thread2_start(void *) {
Kernel::sp() << "thread2 started\n";
u64 i = 0;
while (true) {
kyield();
// Kernel::sp() << SerialPort::IntRadix::Hex << "thread2 run " << i << " " << reg_new << "\n";
i++;
if (i % 3000 == 0) {
rtl8139_test();
}
if (i % 5000 == 0) {
// TODO: we need lock
// buddy_allocator_usage();
}
}
}
extern "C" char _binary_user_init_start[];
extern "C" char _binary_user_init_end[];
extern "C" char _binary_busybox_start[];
extern "C" char _binary_busybox_end[];
Process *current() {
return processes[current_pid];
}
void exec_main() {
u64 size = _binary_user_init_end - _binary_user_init_start;
char *start = _binary_user_init_start;
Kernel::sp() << "init elf size = " << SerialPort::IntRadix::Hex << size << ", start bytes 0x" << (u64)start[0] << " 0x" << (u64)start[1] << "\n";
u64 busybox_size = _binary_busybox_end - _binary_busybox_start;
char *busybox_start = _binary_busybox_start;
Kernel::sp() << "busybox elf size = " << SerialPort::IntRadix::Hex << busybox_size << ", start bytes 0x" << (u64)busybox_start[0] << " 0x" << (u64)busybox_start[1] << "\n";
auto proc = current();
// auto start_addr = proc->load_elf_from_buffer(busybox_start, busybox_size);
auto start_addr = proc->load_elf_from_buffer(start, size);
Kernel::sp() << "ELF file loaded\n";
// goto user space
{
auto &c = proc->context;
c.cs = USER_CODE_SELECTOR;
c.ds = USER_DATA_SELECTOR;
c.ss = USER_DATA_SELECTOR;
c.es = USER_DATA_SELECTOR;
// TODO: user real selector
c.fs = 0;
c.gs = 0;
c.rsp = (u64)proc->user_stack + proc->user_stack_size;
c.rip = (u64)start_addr;
// enable interrupt
c.rflags = 0x200;
return_from_syscall(&c);
}
}
u64 create_kthread(const kstring &name, void (*start)(void*), void *cookie) {
auto pid = create_process();
auto p = processes[pid];
p->tmp_start = start;
p->name = name;
p->cookie = cookie;
return pid;
}
void main_start(void *) {
cli();
Kernel::sp() << "main process started\n";
Kernel::sp() << "main process creating process 2\n";
// maybe user better mechanism to synchronize
create_process();
auto new_proc = processes[next_pid-1];
new_proc->tmp_start = thread2_start;
new_proc->name[0] = '2';
Kernel::sp() << "main process loading\n";
exec_main();
Kernel::k->panic("Should not reach here\n");
// u64 i = 0;
// while (true) {
// u64 reg_new;
// register u64 reg asm("rax");
// reg = (u64)0x2223;
// do_syscall();
// __asm__ __volatile__("movq %%rax, %0" :"=r"(reg_new));
//// Kernel::sp() << SerialPort::IntRadix::Hex << "main process run " << i << " " << reg_new << "\n";
// i++;
// }
}
void process_init() {
memset(processes, 0, sizeof(processes));
next_pid = 1;
current_pid = 1;
create_process();
auto main_process = processes[1];
main_process->tmp_start = main_start;
}
void schedule() {
processes[current_pid]->state = ProcessState::Wait;
while (true) {
if (current_pid == next_pid) {
current_pid = 1;
} else {
current_pid++;
}
auto process = processes[current_pid];
if (process && process->id && process->state == ProcessState::Wait) {
process->state = ProcessState::Running;
break;
}
}
}
void store_current_thread_context(Context *context) {
memcpy(
&processes[current_pid]->context,
context,
sizeof(Context)
);
}
Context *current_context() {
return &processes[current_pid]->context;
}
void kyield() {
asm volatile(
"movq $0x2, %%rax\t\n"
"int $42"
:
:
:"%rax"
);
}
void Process::map_user_addr(u64 vaddr, u64 paddr, u64 n_pages) {
for (u64 i = 0; i < n_pages; i++) {
auto vpage = (vaddr >> 12) + i;
auto ppage = (paddr >> 12) + i;
pts->pt[vpage].p = 1;
pts->pt[vpage].rw = 1;
pts->pt[vpage].us = 1;
pts->pt[vpage].base_addr = ppage;
}
}
void *Process::load_elf_from_buffer(char *buffer, unsigned long size) {
Elf64_Ehdr *ehdr = (Elf64_Ehdr*)buffer;
const char *ElfMagic = "\x7f" "ELF";
for (int i = 0; i < 4; i++) {
if (ElfMagic[i] != buffer[i]) {
Kernel::k->panic("Corrupted ELF file, invalid header magic");
}
}
assert(ehdr->e_phoff, "program header table not found\n");
Kernel::sp() << "elf has " << SerialPort::IntRadix::Dec << ehdr->e_phnum << " sections at 0x" << SerialPort::IntRadix::Hex << ehdr->e_phoff << "\n";
Kernel::sp() << " p_offset p_filesz p_vaddr p_memsz\n";
for (int i = 0; i < ehdr->e_phnum; i++) {
auto phdr = (Elf64_Phdr*)(buffer + ehdr->e_phoff + ehdr->e_phentsize * i);
if (phdr->p_type == PT_LOAD && phdr->p_memsz > 0) {
Kernel::sp() << SerialPort::IntRadix::Hex << " 0x" << phdr->p_offset << " 0x" << phdr->p_filesz << " 0x" << phdr->p_vaddr << " 0x" << phdr->p_memsz << "\n";
assert(phdr->p_vaddr >= 0x100000, "Segment is at wrong location");
assert(phdr->p_vaddr+phdr->p_memsz <= USER_IMAGE_START + USER_IMAGE_SIZE, "Segment too big");
if (phdr->p_filesz <= phdr->p_memsz) {
memcpy((void*)phdr->p_vaddr, buffer + phdr->p_offset, phdr->p_filesz);
} else {
Kernel::k->panic("\nDon't know how to load sections that has filesize > memsize");
}
}
}
Kernel::sp() << "entrypoint at " << SerialPort::IntRadix::Hex << ehdr->e_entry << "\n";
return (void*)ehdr->e_entry;
}
void Process::kernel_entrypoint() {
Kernel::sp() << "starting process pid = "<< current_pid << " '" << name.c_str() << "'\n";
if (tmp_start) {
// Kernel::sp() << "has tmp_start, going for it" << "\n";
tmp_start(cookie);
}
Kernel::sp() << "process quit '" << name.c_str() << "'\n";
Kernel::k->panic("thread quit");
}
Process::Process(unsigned long id, unsigned long start_phy)
:id(id), start_phy(start_phy), syscall_(make_kup<Syscall>(Kernel::k, this)) {
auto pts_log2size = log2(sizeof(PageTabletSructures)) + 1;
pts = (PageTabletSructures*)kernel_page_alloc(pts_log2size);
pts_paddr = kernel2phy((u64)pts);
memset(pts, 0, sizeof(PageTabletSructures));
// reuse kernel space map
pts->pml4t[256].p = 1;
pts->pml4t[256].rw = 1;
pts->pml4t[256].base_addr = get_kernel_pdpt_phy_addr() >> 12;
// create a new user space map
pts->pml4t[0].p = 1;
pts->pml4t[0].rw = 1;
pts->pml4t[0].us = 1;
pts->pml4t[0].base_addr = (pts_paddr + ((u64)&pts->pdpt[0] - (u64)pts)) >> 12;
// only the first pdpt is used
pts->pdpt[0].p = 1;
pts->pdpt[0].rw = 1;
pts->pdpt[0].us = 1; // allow userspace
pts->pdpt[0].base_addr = (pts_paddr + ((u64)&pts->pdt[0] - (u64)pts)) >> 12;
for (u64 i = 0; i < sizeof(pts->pdt) / sizeof(pts->pdt[0]); i++) {
pts->pdt[i].p = 1;
pts->pdt[i].rw = 1;
pts->pdt[i].us = 1;
pts->pdt[i].base_addr = (pts_paddr + ((u64)&pts->pt[i * PAGES_PER_TABLE] - (u64)pts)) >> 12;
}
// 1MiB ~ 9MiB are for exec image
user_image = (u8*)USER_IMAGE_START;
user_image_phy_addr = physical_page_alloc(log2(USER_IMAGE_SIZE));
map_user_addr((u64)user_image, user_image_phy_addr, user_image_size / PAGE_SIZE);
// 16MiB ~ 32MiB for user brk
brk_start = USER_BRK_START;
brk_end = USER_BRK_START + USER_BRK_SIZE;
// 32MiB ~ 33MiB are for user stack
user_stack = (u8*)(33UL*1024UL*1024UL);
user_stack_phy_addr = physical_page_alloc(log2(USER_STACK_SIZE));
map_user_addr((u64)user_stack, user_stack_phy_addr, user_stack_size / PAGE_SIZE);
context.cr3 = pts_paddr;
context.cs = KERNEL_CODE_SELECTOR;
context.ds = KERNEL_DATA_SELECTOR;
context.ss = KERNEL_DATA_SELECTOR;
context.es = KERNEL_DATA_SELECTOR;
context.fs = KERNEL_DATA_SELECTOR;
context.gs = KERNEL_DATA_SELECTOR;
context.rsp = (u64)kernel_stack_bottom;
context.rip = (u64)&process_entrypoint;
// NOTE: enable interrupt in the process
context.rflags = 0x200;
// first parameter for process_entry_point(p)
context.rdi = (u64)this;
print();
}