forked from anshugupta673/x86_64-decOS-MSC-KIIT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.c
215 lines (173 loc) · 5.36 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
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
#include "process.h"
#include "trap.h"
#include "memory.h"
#include "print.h"
#include "lib.h"
#include "debug.h"
extern struct TSS Tss;
static struct Process process_table[NUM_PROC];
static int pid_num = 1;
static struct ProcessControl pc;
static void set_tss(struct Process *proc)
{
Tss.rsp0 = proc->stack + STACK_SIZE;
}
static struct Process* find_unused_process(void)
{
struct Process *process = NULL;
for (int i = 0; i < NUM_PROC; i++) {
if (process_table[i].state == PROC_UNUSED) {
process = &process_table[i];
break;
}
}
return process;
}
static void set_process_entry(struct Process *proc, uint64_t addr)
{
uint64_t stack_top;
proc->state = PROC_INIT;
proc->pid = pid_num++;
proc->stack = (uint64_t)kalloc();
ASSERT(proc->stack != 0);
memset((void*)proc->stack, 0, PAGE_SIZE);
stack_top = proc->stack + STACK_SIZE;
proc->context = stack_top - sizeof(struct TrapFrame) - 7*8;
*(uint64_t*)(proc->context + 6*8) = (uint64_t)TrapReturn;
proc->tf = (struct TrapFrame*)(stack_top - sizeof(struct TrapFrame));
proc->tf->cs = 0x10|3;
proc->tf->rip = 0x400000;
proc->tf->ss = 0x18|3;
proc->tf->rsp = 0x400000 + PAGE_SIZE;
proc->tf->rflags = 0x202;
proc->page_map = setup_kvm();
ASSERT(proc->page_map != 0);
ASSERT(setup_uvm(proc->page_map, P2V(addr), 5120));
proc->state = PROC_READY;
}
static struct ProcessControl* get_pc(void)
{
return &pc;
}
void init_process(void)
{
struct ProcessControl *process_control;
struct Process *process;
struct HeadList *list;
uint64_t addr[3] = {0x20000, 0x30000, 0x40000};
process_control = get_pc();
list = &process_control->ready_list;
for (int i = 0; i < 3; i++) {
process = find_unused_process();
set_process_entry(process, addr[i]);
append_list_tail(list, (struct List*)process);
}
}
void launch(void)
{
struct ProcessControl *process_control;
struct Process *process;
process_control = get_pc();
process = (struct Process*)remove_list_head(&process_control->ready_list);
process->state = PROC_RUNNING;
process_control->current_process = process;
set_tss(process);
switch_vm(process->page_map);
pstart(process->tf);
}
static void switch_process(struct Process *prev, struct Process *current)
{
set_tss(current);
switch_vm(current->page_map);
swap(&prev->context, current->context);
}
static void schedule(void)
{
struct Process *prev_proc;
struct Process *current_proc;
struct ProcessControl *process_control;
struct HeadList *list;
process_control = get_pc();
prev_proc = process_control->current_process;
list = &process_control->ready_list;
ASSERT(!is_list_empty(list));
current_proc = (struct Process*)remove_list_head(list);
current_proc->state = PROC_RUNNING;
process_control->current_process = current_proc;
switch_process(prev_proc, current_proc);
}
void yield(void)
{
struct ProcessControl *process_control;
struct Process *process;
struct HeadList *list;
process_control = get_pc();
list = &process_control->ready_list;
if (is_list_empty(list)) {
return;
}
process = process_control->current_process;
process->state = PROC_READY;
append_list_tail(list, (struct List*)process);
schedule();
}
void sleep(int wait)
{
struct ProcessControl *process_control;
struct Process *process;
process_control = get_pc();
process = process_control->current_process;
process->state = PROC_SLEEP;
process->wait = wait;
append_list_tail(&process_control->wait_list, (struct List*)process);
schedule();
}
void wake_up(int wait)
{
struct ProcessControl *process_control;
struct Process *process;
struct HeadList *ready_list;
struct HeadList *wait_list;
process_control = get_pc();
ready_list = &process_control->ready_list;
wait_list = &process_control->wait_list;
process = (struct Process*)remove_list(wait_list, wait);
while (process != NULL) {
process->state = PROC_READY;
append_list_tail(ready_list, (struct List*)process);
process = (struct Process*)remove_list(wait_list, wait);
}
}
void exit(void)
{
struct ProcessControl *process_control;
struct Process* process;
struct HeadList *list;
process_control = get_pc();
process = process_control->current_process;
process->state = PROC_KILLED;
list = &process_control->kill_list;
append_list_tail(list, (struct List*)process);
wake_up(1);
schedule();
}
void wait(void)
{
struct ProcessControl *process_control;
struct Process *process;
struct HeadList *list;
process_control = get_pc();
list = &process_control->kill_list;
while (1) {
if (!is_list_empty(list)) {
process = (struct Process*)remove_list_head(list);
ASSERT(process->state == PROC_KILLED);
kfree(process->stack);
free_vm(process->page_map);
memset(process, 0, sizeof(struct Process));
}
else {
sleep(1);
}
}
}