-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsvmm.c
More file actions
385 lines (272 loc) · 6.41 KB
/
svmm.c
File metadata and controls
385 lines (272 loc) · 6.41 KB
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#include "svmm.h"
#include "defs.h"
#include "memlayout.h"
#include "mmu.h"
#include "param.h"
#define NULL_PROC_ID -1
#define NULL ((void *)0)
svm_t *head = NULL;
svm_t *tail = NULL;
uint count = 0;
void *start_addr = NULL;
void *end_addr = NULL;
static pte_t *pgdirwalk(pde_t *pgdir, const void *va, int alloc) {
pde_t *pde;
pte_t *pgtab;
pde = &pgdir[PDX(va)];
if (*pde & PTE_P) {
pgtab = (pte_t *)P2V(PTE_ADDR(*pde));
} else {
if (!alloc || (pgtab = (pte_t *)kalloc()) == 0)
return 0;
// Make sure all those PTE_P bits are zero.
memset(pgtab, 0, PGSIZE);
// The permissions here are overly generous, but they can
// be further restricted by the permissions in the page table
// entries, if necessary.
*pde = V2P(pgtab) | PTE_P | PTE_W | PTE_U;
}
return &pgtab[PTX(va)];
}
static int pagesmap(pde_t *pgdir, void *va, uint size, uint pa, int perm) {
char *a, *last;
pte_t *pte;
a = (char *)PGROUNDDOWN((uint)va);
last = (char *)PGROUNDDOWN(((uint)va) + size - 1);
// Mapping VA -> PA
for (;;) {
if ((pte = pgdirwalk(pgdir, a, 1)) == 0)
return -1;
if (*pte & PTE_P)
panic("remap");
*pte = pa | perm | PTE_P;
if (a == last)
break;
a += PGSIZE;
pa += PGSIZE;
}
return 0;
}
int keycmp(const char *p, const char *q) {
while (*p && *p == *q)
p++, q++;
return (uchar)*p - (uchar)*q;
}
svm_t *add_shared_vm(char *key, uint npages, uint *pas) {
svm_t *svm;
int idx = 0;
svm = (svm_t *)kalloc();
if (!svm) {
// memory allocation failure
// Throw exception or return NULL
return NULL;
}
svm->key = key;
svm->npages = npages;
svm->pas = pas;
svm->references = 0;
svm->prev = NULL;
svm->next = NULL;
for (; idx < NPROC; idx++) {
svm->procs[idx].pid = NULL_PROC_ID;
svm->procs[idx].va = NULL;
}
if (tail != NULL) {
svm->prev = tail;
tail->next = svm;
tail = svm;
}
if (head == NULL) {
head = svm;
if (tail == NULL) {
tail = svm;
}
}
count++;
return svm;
}
svm_t *get_shared_vm(char *key) {
if (head == NULL) {
return NULL;
}
svm_t *cur = head;
for (; cur != NULL; cur = cur->next) {
if (keycmp(cur->key, key) == 0) {
return cur;
}
}
return NULL;
}
int remove_shared_vm(char *key) {
svm_t *cur = get_shared_vm(key);
if (cur == NULL) {
return 0;
}
svm_t *prev = cur->prev;
svm_t *next = cur->next;
if (head == cur) {
head = cur->next;
if (head != NULL) {
head->prev = NULL;
}
} else if (tail == cur) {
tail = cur->prev;
if (tail != NULL) {
tail->next = NULL;
}
} else {
if (prev != NULL) {
prev->next = next;
}
if (next != NULL) {
next->prev = prev;
}
}
kfree((char *)cur->pas);
kfree((char *)cur);
count--;
return 1;
}
int add_proc(void *va, svm_t *svm, int pid) {
if (svm == NULL) {
return 0;
}
int idx = get_proc_idx(svm, NULL_PROC_ID);
if (idx == -1) {
return 0;
}
svm->references += 1;
svm->procs[idx].pid = pid;
svm->procs[idx].va = va;
return 1;
}
int get_proc_idx(svm_t *svm, int pid) {
if (svm == NULL) {
return 0;
}
int idx;
for (idx = 0; idx < NPROC; idx++) {
if (svm->procs[idx].pid == pid) {
return idx;
}
}
return -1;
}
int remove_proc(svm_t *svm, int pid) {
if (svm == NULL) {
return 0;
}
int idx = get_proc_idx(svm, pid);
if (idx == -1) {
return 0;
}
svm->references -= 1;
svm->procs[idx].pid = NULL_PROC_ID;
svm->procs[idx].va = NULL;
return 1;
}
uint *alloc_physical_pages(uint npages) {
int pg_idx;
uint *pa_store = (uint *)kalloc();
if (!pa_store) {
cprintf("Unable to allocate memory to store physical addresses\n");
return NULL;
}
uint *pa_str_ptr = pa_store;
for (pg_idx = 0; pg_idx < npages; pg_idx++) {
char *mem = kalloc();
if (mem == 0) {
cprintf("alloc_physical_pages out of memory\n");
dealloc_physical_pages(pa_store, pg_idx);
return NULL;
}
memset(mem, 0, PGSIZE);
*pa_str_ptr = V2P(mem);
pa_str_ptr++;
}
return pa_store;
}
void dealloc_physical_pages(uint *pas, uint npages) {
int pg_idx;
for (pg_idx = 0; pg_idx < npages; pg_idx++) {
kfree(P2V(pas[pg_idx]));
}
}
void *map_shared_pages_to_proc(uint *pas, pde_t *pgdir, uint sz, uint npages) {
uint va = PGROUNDUP(sz);
uint va_idx = va;
for (int pg_idx = 0; pg_idx < npages; pg_idx++) {
// Setting up PTE with VA
if (pagesmap(pgdir, (char *)va_idx, PGSIZE, pas[pg_idx], PTE_W | PTE_U) <
0) {
cprintf("mappages out of memory (2)\n");
unmap_shared_pages_to_proc((void*)va, pgdir, pg_idx);
dealloc_physical_pages(pas, npages);
return 0;
}
va_idx += PGSIZE;
}
return (char *)va;
}
void unmap_shared_pages_to_proc(void *va, pde_t *pgdir, uint npages) {
pte_t *pte;
char *va_idx = (char *)va;
for (int pg_idx = 0; pg_idx < npages; pg_idx++) {
// Unmapping PTE with VA
pte = pgdirwalk(pgdir, va_idx, 0);
if (!pte) {
panic("unmap");
}
*pte = 0;
va_idx += PGSIZE;
}
}
void *get_shared_pages(char *key, uint npages, int pid, uint proc_sz,
pde_t *pgdir) {
svm_t *svm;
uint *pas;
if(proc_sz + (npages * PGSIZE) >= KERNBASE){
return NULL;
}
svm = get_shared_vm(key);
if (svm != NULL) {
int proc_idx = get_proc_idx(svm, pid);
if (proc_idx != -1) {
return svm->procs[proc_idx].va;
}
pas = svm->pas;
} else {
pas = alloc_physical_pages(npages);
}
char *va = (char *)map_shared_pages_to_proc(pas, pgdir, proc_sz, npages);
if (va == NULL) {
// mapping failed, return null
return NULL;
}
if (svm == NULL) {
svm = add_shared_vm(key, npages, pas);
}
add_proc(va, svm, pid);
return (void *)va;
}
int free_shared_pages(char *key, int pid, pde_t *pgdir) {
svm_t *svm = get_shared_vm(key);
if (svm == NULL) {
// throw error saying its not allocated
return -1;
}
int svm_proc_idx = get_proc_idx(svm, pid);
if (svm_proc_idx == -1) {
// throw error saying the shared memory was never allocated for this process
return -2;
}
uint npages = svm->npages;
char *va = svm->procs[svm_proc_idx].va;
remove_proc(svm, pid);
if (svm->references == 0) {
dealloc_physical_pages(svm->pas, npages);
remove_shared_vm(key);
}
unmap_shared_pages_to_proc(va, pgdir, npages);
return npages;
}