-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsysproc.c
296 lines (240 loc) · 6.86 KB
/
sysproc.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
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 "types.h"
#include "x86.h"
#include "defs.h"
#include "date.h"
#include "param.h"
#include "memlayout.h"
#include "mmu.h"
#include "proc.h"
#include "svmm.h"
#define NULL ((void*)0)
#define SHARED_PAGE_LIMIT 1000
int
sys_fork(void)
{
return fork();
}
int
sys_exit(void)
{
exit();
return 0; // not reached
}
int
sys_wait(void)
{
return wait();
}
int
sys_kill(void)
{
int pid;
if(argint(0, &pid) < 0)
return -1;
return kill(pid);
}
int
sys_getpid(void)
{
return myproc()->pid;
}
int
sys_sbrk(void)
{
int addr;
int n;
if(argint(0, &n) < 0)
return -1;
addr = myproc()->sz;
if(growproc(n) < 0)
return -1;
return addr;
}
int
sys_sleep(void)
{
int n;
uint ticks0;
if(argint(0, &n) < 0)
return -1;
acquire(&tickslock);
ticks0 = ticks;
while(ticks - ticks0 < n){
if(myproc()->killed){
release(&tickslock);
return -1;
}
sleep(&ticks, &tickslock);
}
release(&tickslock);
return 0;
}
// return how many clock tick interrupts have occurred
// since start.
int
sys_uptime(void)
{
uint xticks;
acquire(&tickslock);
xticks = ticks;
release(&tickslock);
return xticks;
}
// void test_svm_ds(){
// int i;
// uint * test1_pas = (uint *)kalloc();
// *test1_pas = 234602496;
// uint * test2_pas = (uint *)kalloc();
// *test2_pas = 234602496;
// *(test2_pas+1) = 234315776;
// uint * test3_pas = (uint *)kalloc();
// *test3_pas = 234602496;
// *(test3_pas+1) = 234315776;
// *(test3_pas+2) = 234614784;
// uint * test4_pas = (uint *)kalloc();
// *test4_pas = 234602496;
// *(test4_pas+1) = 234315776;
// *(test4_pas+2) = 234614784;
// *(test4_pas+3) = 234618880;
// uint * test5_pas = (uint *)kalloc();
// *test5_pas = 234602496;
// *(test5_pas+1) = 234315776;
// *(test5_pas+2) = 234614784;
// *(test5_pas+3) = 234618880;
// *(test5_pas+4) = 234622976;
// // svm_t *test1 =
// add_shared_vm("test1", 1, test1_pas);
// // svm_t *test2 =
// add_shared_vm("test2", 2, test2_pas);
// // svm_t *test3 =
// add_shared_vm("test3", 3, test3_pas);
// // svm_t *test4 =
// add_shared_vm("test4", 4, test4_pas);
// // svm_t *test5 =
// add_shared_vm("test5", 5, test5_pas);
// // cprintf("Key: %s\n", test1->key);
// // cprintf("npages: %d\n", test1->npages);
// // cprintf("references: %d\n", test1->references);
// // for(i=0; i<test1->npages; i++){
// // cprintf("\tindex: %d, value: %d\n\n", i, test1->pas[i]);
// // }
// svm_t *test1_get = get_shared_vm("test1");
// cprintf("Key: %s\n", test1_get->key);
// cprintf("npages: %d\n", test1_get->npages);
// cprintf("references: %d\n", test1_get->references);
// for(i=0; i<test1_get->npages; i++){
// cprintf("\tindex: %d, value: %x\n\n", i, test1_get->pas[i]);
// }
// svm_t *test2_get = get_shared_vm("test2");
// cprintf("Key: %s\n", test2_get->key);
// cprintf("npages: %d\n", test2_get->npages);
// cprintf("references: %d\n", test2_get->references);
// for(i=0; i<test2_get->npages; i++){
// cprintf("\tindex: %d, value: %x\n\n", i, test2_get->pas[i]);
// }
// svm_t *test3_get = get_shared_vm("test3");
// cprintf("Key: %s\n", test3_get->key);
// cprintf("npages: %d\n", test3_get->npages);
// cprintf("references: %d\n", test3_get->references);
// for(i=0; i<test3_get->npages; i++){
// cprintf("\tindex: %d, value: %x\n\n", i, test3_get->pas[i]);
// }
// svm_t *test4_get = get_shared_vm("test4");
// cprintf("Key: %s\n", test4_get->key);
// cprintf("npages: %d\n", test4_get->npages);
// cprintf("references: %d\n", test4_get->references);
// for(i=0; i<test4_get->npages; i++){
// cprintf("\tindex: %d, value: %x\n\n", i, test4_get->pas[i]);
// }
// svm_t *test5_get = get_shared_vm("test5");
// cprintf("Key: %s\n", test5_get->key);
// cprintf("npages: %d\n", test5_get->npages);
// cprintf("references: %d\n", test5_get->references);
// for(i=0; i<test5_get->npages; i++){
// cprintf("\tindex: %d, value: %x\n\n", i, test5_get->pas[i]);
// }
// uint va1 = PGROUNDUP(myproc()->sz);
// cprintf("Adding to test1 : va -> %x\n", va1);
// int proc_add = add_proc((char *)va1, test1_get, myproc()->pid);
// if (!proc_add) {
// cprintf("Adding proc failed!\n");
// }
// int proc_idx = get_proc_idx(test1_get, myproc()->pid);
// cprintf("The proc pid : %d, va: %x\n", test1_get->procs[proc_idx].pid,
// test1_get->procs[proc_idx].va);
// uint * pas = alloc_physical_pages(2);
// cprintf("2 Physical pages allocated\n");
// for(int i=0; i<2; i++){
// cprintf("\tpa: %x\n", pas[i]);
// }
// void * va = map_shared_pages_to_proc(pas, myproc()->pgdir, myproc()->sz, 2);
// cprintf("\n\nSuccessfully mapped the PAs to VAs\n\n");
// unmap_shared_pages_to_proc(va, myproc()->pgdir, 2);
// cprintf("\n\nSuccessfully unmapped the PAs to VAs\n\n");
// dealloc_physical_pages(pas, 2);
// cprintf("2 Physical pages dedallocated\n");
// int proc_rem = remove_proc(test1_get, myproc()->pid);
// if (!proc_rem) {
// cprintf("Removing proc failed!\n");
// }
// int proc_idx_fail = get_proc_idx(test1_get, myproc()->pid);
// if (proc_idx_fail == -1) {
// cprintf("Get failed as expected!\n");
// }
// int success1 = remove_shared_vm("test1");
// int success2 = remove_shared_vm("test2");
// int success3 = remove_shared_vm("test3");
// int success4 = remove_shared_vm("test4");
// int success5 = remove_shared_vm("test5");
// if(!success1 || !success2 || !success3 || !success4 || !success5){
// cprintf("%s\n", "Failed to remove shared vm\n");
// }else{
// cprintf("%s\n", "Removed all the shared vms\n");
// kfree((char*)test1_pas);
// kfree((char*)test2_pas);
// kfree((char*)test3_pas);
// kfree((char*)test4_pas);
// kfree((char*)test5_pas);
// }
// }
int sys_getsharedpage(void){
char *key;
int npages;
if(argstr(0, &key) < 0 || argint(1, &npages) < 0)
return -1;
if(key == NULL || strncmp(key, "", 1) == 0){
return -2;
}
if(npages < 0 || npages == 0 || npages > SHARED_PAGE_LIMIT){
return -3;
}
struct proc * cur_proc = myproc();
void * va = get_shared_pages(key, npages, cur_proc->pid, cur_proc->sz, cur_proc->pgdir);
if(va == NULL){
return -4;
}
cur_proc->sz += (npages * PGSIZE);
switchuvm(cur_proc);
return (int)va;
}
int sys_freesharedpage(void){
char *key;
if(argstr(0, &key) < 0)
return -1;
if(key == NULL || strncmp(key, "", 1) == 0){
return -2;
}
struct proc * cur_proc = myproc();
int npages = free_shared_pages(key, cur_proc->pid, cur_proc->pgdir);
if(npages == -1){
cprintf("No shared memory associated with this key : %s\n", key);
return -3;
}
if(npages == -2){
cprintf("Shared memory not associated with this process pid : %d\n", cur_proc->pid);
return -4;
}
cur_proc->sz -= (npages * PGSIZE);
switchuvm(cur_proc);
return 0;
}