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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
|
#include "globals.h"
#include "kernel.h"
#include <errno.h>
#include "vm/anon.h"
#include "vm/shadow.h"
#include "util/debug.h"
#include "util/printf.h"
#include "util/string.h"
#include "fs/file.h"
#include "fs/vfs_syscall.h"
#include "fs/vnode.h"
#include "mm/mm.h"
#include "mm/mman.h"
#include "mm/slab.h"
#include "mm/tlb.h"
static slab_allocator_t *vmmap_allocator;
static slab_allocator_t *vmarea_allocator;
void vmmap_init(void)
{
vmmap_allocator = slab_allocator_create("vmmap", sizeof(vmmap_t));
vmarea_allocator = slab_allocator_create("vmarea", sizeof(vmarea_t));
KASSERT(vmmap_allocator && vmarea_allocator);
}
/*
* Allocate and initialize a new vmarea using vmarea_allocator.
*/
vmarea_t *vmarea_alloc(void)
{
// NOT_YET_IMPLEMENTED("VM: vmarea_alloc");
// Allocate a new vmarea
vmarea_t *new_vmarea = (vmarea_t *)slab_obj_alloc(vmarea_allocator);
if (new_vmarea == NULL)
{
return NULL;
}
// Initialize the fields of the vmarea
new_vmarea->vma_start = 0;
new_vmarea->vma_end = 0;
new_vmarea->vma_off = 0;
new_vmarea->vma_prot = 0;
new_vmarea->vma_flags = 0;
new_vmarea->vma_obj = NULL;
new_vmarea->vma_vmmap = NULL;
list_link_init(&new_vmarea->vma_plink);
// Return the new vmarea
return new_vmarea;
}
/*
* Free the vmarea by removing it from any lists it may be on, putting its
* vma_obj if it exists, and freeing the vmarea_t.
*/
void vmarea_free(vmarea_t *vma)
{
// NOT_YET_IMPLEMENTED("VM: vmarea_free");
// Remove the vmarea from any lists it may be on
if (list_link_is_linked(&vma->vma_plink))
{
list_remove(&vma->vma_plink);
}
// Put the vma_obj if it exists
if (vma->vma_obj != NULL)
{
mobj_put(&vma->vma_obj);
}
// Free the vmarea
slab_obj_free(vmarea_allocator, vma);
}
/*
* Create and initialize a new vmmap. Initialize all the fields of vmmap_t.
*/
vmmap_t *vmmap_create(void)
{
// NOT_YET_IMPLEMENTED("VM: vmmap_create");
// Allocate a new vmmap
vmmap_t *new_vmmap = (vmmap_t *)slab_obj_alloc(vmmap_allocator);
if (new_vmmap == NULL)
{
return NULL;
}
// Initialize the fields of the vmmap
list_init(&new_vmmap->vmm_list);
new_vmmap->vmm_proc = curproc;
return new_vmmap;
}
/*
* Destroy the map pointed to by mapp and set *mapp = NULL.
* Remember to free each vma in the maps list.
*/
void vmmap_destroy(vmmap_t **mapp)
{
// NOT_YET_IMPLEMENTED("VM: vmmap_destroy");
vmmap_t *map = *mapp;
// Iterate through the list of vmareas and free each one
list_iterate(&(map)->vmm_list, vma, vmarea_t, vma_plink)
{
list_remove(&vma->vma_plink);
vmarea_free(vma);
}
// Free the map
slab_obj_free(vmmap_allocator, map);
// Set the map to NULL
*mapp = NULL;
}
/*
* Add a vmarea to an address space. Assumes (i.e. asserts to some extent) the
* vmarea is valid. Iterate through the list of vmareas, and add it
* accordingly.
*/
void vmmap_insert(vmmap_t *map, vmarea_t *new_vma)
{
// NOT_YET_IMPLEMENTED("VM: vmmap_insert*");
new_vma->vma_vmmap = map;
// iterate over the list of vmareas
list_iterate(&map->vmm_list, vma, vmarea_t, vma_plink)
{
// if the new vmarea is after the current vmarea
if (vma->vma_start > new_vma->vma_end)
{
// insert the new vmarea before the current vmarea
list_insert_before(&vma->vma_plink, &new_vma->vma_plink);
return;
}
}
// insert this map to the tail
list_insert_tail(&map->vmm_list, &new_vma->vma_plink);
}
/*
* Find a contiguous range of free virtual pages of length npages in the given
* address space. Returns starting page number for the range, without altering the map.
* Return -1 if no such range exists.
*
* Your algorithm should be first fit.
* You should assert that dir is VMMAP_DIR_LOHI OR VMMAP_DIR_HILO.
* If dir is:
* - VMMAP_DIR_HILO: find a gap as high in the address space as possible,
* starting from USER_MEM_HIGH.
* - VMMAP_DIR_LOHI: find a gap as low in the address space as possible,
* starting from USER_MEM_LOW.
*
* Make sure you are converting between page numbers and addresses correctly!
*/
ssize_t vmmap_find_range(vmmap_t *map, size_t npages, int dir)
{
// : vmmap_find_range");
// case 1: dir is VMMAP_DIR_LOHI
if (dir == VMMAP_DIR_LOHI)
{
// iterate over the page numbers, going from low to high
// determine the continguous range of free virtual pages
size_t start_page, contig_page = 0;
size_t vfn = ADDR_TO_PN(USER_MEM_LOW);
while (vfn++ < ADDR_TO_PN(USER_MEM_HIGH))
{
// Lookup the vmarea for this page number
vmarea_t *vma = vmmap_lookup(map, vfn);
if (vma == NULL)
{
// if unmapped, document this
if (contig_page == 0)
{
start_page = 0;
}
contig_page++;
}
else
{
// if mapped, start over
start_page = contig_page = 0;
}
// if the range exists, return the start
if (contig_page == npages)
{
return start_page;
}
}
}
// case 2: dir is VMMAP_DIR_HILO
if (dir == VMMAP_DIR_HILO)
{
// iterate over the page numbers
size_t contig = 0;
size_t vfn = ADDR_TO_PN(USER_MEM_HIGH);
while (--vfn >= ADDR_TO_PN(USER_MEM_LOW))
{
// Lookup the vmarea for this page number
vmarea_t *vma = vmmap_lookup(map, vfn);
if (vma == NULL)
{
// if unmapped, increment the contig
contig++;
}
else
{
// if mapped, reset the contig
contig = 0;
}
// if there are n contiguous pages, return the current vfn
if (contig == npages)
{
return vfn;
}
}
}
// if no range exists, return -1
return -1;
}
/*
* Return the vm_area that vfn (a page number) lies in. Scan the address space looking
* for a vma whose range covers vfn. If the page is unmapped, return NULL.
*/
vmarea_t *vmmap_lookup(vmmap_t *map, size_t vfn)
{
// NOT_YET_IMPLEMENTED("VM: vmmap_lookup");
// iterate over the list of vmareas
list_iterate(&map->vmm_list, vma, vmarea_t, vma_plink)
{
// if the vfn lies within the range of the current vmarea
if (vfn >= vma->vma_start && vfn < vma->vma_end)
{
return vma;
}
}
// if the page is unmapped, return NULL
return NULL;
}
/*
* For each vmarea in the map, if it is a shadow object, call shadow_collapse.
*/
void vmmap_collapse(vmmap_t *map)
{
list_iterate(&map->vmm_list, vma, vmarea_t, vma_plink)
{
if (vma->vma_obj->mo_type == MOBJ_SHADOW)
{
// mobj_lock(vma->vma_obj);
shadow_collapse(vma->vma_obj);
// mobj_unlock(vma->vma_obj);
}
}
}
/*
* This is where the magic of fork's copy-on-write gets set up.
*
* Upon successful return, the new vmmap should be a clone of map with all
* shadow objects properly set up.
*
* For each vmarea, clone it's members.
* 1) vmarea is share-mapped, you don't need to do anything special.
* 2) vmarea is not share-mapped, time for shadow objects:
* a) Create two shadow objects, one for map and one for the new vmmap you
* are constructing, both of which shadow the current vma_obj the vmarea
* being cloned.
* b) After creating the shadow objects, put the original vma_obj
* c) and insert the shadow objects into their respective vma's.
*
* Be sure to clean up in any error case, manage the reference counts correctly,
* and to lock/unlock properly.
*/
vmmap_t *vmmap_clone(vmmap_t *map)
{
// NOT_YET_IMPLEMENTED("VM: vmmap_clone");
vmmap_collapse(map);
// Create a new vmmap
vmmap_t *new_vmmap = vmmap_create();
if (new_vmmap == NULL)
{
return NULL;
}
// Iterate over the list of vmareas
list_iterate(&map->vmm_list, vma, vmarea_t, vma_plink)
{
// Create a new vmarea
vmarea_t *new_vmarea = vmarea_alloc();
if (new_vmarea == NULL)
{
vmmap_destroy(&new_vmmap);
return NULL;
}
// Clone the fields of the vmarea
new_vmarea->vma_start = vma->vma_start;
new_vmarea->vma_end = vma->vma_end;
new_vmarea->vma_off = vma->vma_off;
new_vmarea->vma_prot = vma->vma_prot;
new_vmarea->vma_flags = vma->vma_flags;
// If the vmarea is share-mapped
if (vma->vma_flags & MAP_SHARED)
{
mobj_lock(vma->vma_obj);
new_vmarea->vma_obj = vma->vma_obj;
mobj_ref(new_vmarea->vma_obj);
mobj_unlock(vma->vma_obj);
}
// If the vmarea is not share-mapped
else
{
// Create two shadow objects
mobj_lock(vma->vma_obj);
mobj_t *shadow_obj_map = shadow_create(vma->vma_obj);
mobj_unlock(vma->vma_obj);
mobj_unlock(shadow_obj_map); // unlock the map before use
if (shadow_obj_map == NULL)
{
vmarea_free(new_vmarea);
vmmap_destroy(&new_vmmap);
return NULL;
}
mobj_lock(vma->vma_obj);
mobj_t *shadow_obj_new = shadow_create(vma->vma_obj);
mobj_unlock(vma->vma_obj);
mobj_unlock(shadow_obj_new); // unlock the new before use
if (shadow_obj_new == NULL)
{
mobj_put(&shadow_obj_map);
vmarea_free(new_vmarea);
vmmap_destroy(&new_vmmap);
return NULL;
}
// Put the original vma_obj
mobj_put(&vma->vma_obj);
// Insert the shadow objects into their respective vmareas
new_vmarea->vma_obj = shadow_obj_new;
vma->vma_obj = shadow_obj_map;
}
// Insert the new vmarea into the new vmmap
vmmap_insert(new_vmmap, new_vmarea);
}
// Return the new vmmap
return new_vmmap;
}
/*
*
* Insert a mapping into the map starting at lopage for npages pages.
*
* file - If provided, the vnode of the file to be mapped in
* lopage - If provided, the desired start range of the mapping
* prot - See mman.h for possible values
* flags - See do_mmap()'s comments for possible values
* off - Offset in the file to start mapping at, in bytes
* dir - VMMAP_DIR_LOHI or VMMAP_DIR_HILO
* new_vma - If provided, on success, must point to the new vmarea_t
*
* Return 0 on success, or:
* - ENOMEM: On vmarea_alloc, anon_create, shadow_create or
* vmmap_find_range failure
* - Propagate errors from file->vn_ops->mmap and vmmap_remove
*
* Hints:
* - You can assume/assert that all input is valid. It may help to write
* this function and do_mmap() somewhat in tandem.
* - If file is NULL, create an anon object.
* - If file is non-NULL, use the vnode's mmap operation to get the mobj.
* Do not assume it is file->vn_obj (mostly relevant for special devices).
* - If lopage is 0, use vmmap_find_range() to get a valid range
* - If lopage is not 0, the direction flag (dir) is ignored.
* - If lopage is nonzero and MAP_FIXED is specified and
* the given range overlaps with any preexisting mappings,
* remove the preexisting mappings.
* - If MAP_PRIVATE is specified, set up a shadow object. Be careful with
* refcounts!
* - Be careful: off is in bytes (albeit should be page-aligned), but
* vma->vma_off is in pages.
* - Be careful with the order of operations. Hold off on any irreversible
* work until there is no more chance of failure.
*/
long vmmap_map(vmmap_t *map, vnode_t *file, size_t lopage, size_t npages,
int prot, int flags, off_t off, int dir, vmarea_t **new_vma)
{
// NOT_YET_IMPLEMENTED("VM: vmmap_map");
// return -1;
// ASK: why are these needed!!
KASSERT(map != NULL);
KASSERT(prot == PROT_NONE
|| prot == PROT_READ
|| prot == PROT_WRITE
|| prot == PROT_EXEC
|| prot == (PROT_READ | PROT_WRITE)
|| prot == (PROT_READ | PROT_EXEC)
|| prot == (PROT_WRITE | PROT_EXEC)
|| prot == (PROT_READ | PROT_WRITE | PROT_EXEC));
KASSERT((flags & MAP_TYPE) == MAP_SHARED || (flags & MAP_TYPE) == MAP_PRIVATE);
if (lopage == 0)
{
KASSERT(dir == VMMAP_DIR_LOHI || dir == VMMAP_DIR_HILO);
ssize_t res = vmmap_find_range(map, npages, dir);
if (res == -1) {
return -ENOMEM;
}
lopage = res;
}
if (lopage != 0 && (flags & MAP_FIXED))
{
long ret = vmmap_remove(map, lopage, npages);
if (ret < 0)
{
return ret;
}
}
// alloc the new vma
vmarea_t *vma = vmarea_alloc();
if (!vma)
{
return -ENOMEM;
}
// fill in fields, except for mobj and vma
vma->vma_start = lopage;
vma->vma_end = lopage + npages;
vma->vma_off = ADDR_TO_PN(off);
vma->vma_prot = prot;
vma->vma_flags = flags;
// make the mobj, depending on the case (anon or mmap)
mobj_t *obj = NULL;
if (file == NULL)
{
obj = anon_create();
if (obj == NULL)
{
vmarea_free(vma);
return -ENOMEM;
}
mobj_unlock(obj);
}
else
{
long ret = file->vn_ops->mmap(file, &obj);
if (ret < 0)
{
vmarea_free(vma);
return ret;
}
}
vma->vma_obj = obj;
// if the flag is private, upgrade the obj to a shadow obj
if (flags & MAP_PRIVATE)
{
mobj_t *shadow_obj = shadow_create(obj);
if (shadow_obj == NULL)
{
vmarea_free(vma);
mobj_put(&obj);
return -ENOMEM;
}
// unlock from creation
mobj_unlock(shadow_obj);
vma->vma_obj = shadow_obj;
// put the og obj
mobj_put(&obj);
}
// now that vma is ready, set it
vmmap_insert(map, vma);
if (new_vma != NULL)
{
*new_vma = vma;
}
return 0;
}
/*
* Iterate over the mapping's vmm_list and make sure that the specified range
* is completely empty. You will have to handle the following cases:
*
* Key: [ ] = existing vmarea_t
* ******* = region to be unmapped
*
* Case 1: [ ******* ]
* The region to be unmapped lies completely inside the vmarea. We need to
* split the old vmarea into two vmareas. Be sure to increment the refcount of
* the object associated with the vmarea.
*
* Case 2: [ *******]**
* The region overlaps the end of the vmarea. Just shorten the length of
* the mapping.
*
* Case 3: *[***** ]
* The region overlaps the beginning of the vmarea. Move the beginning of
* the mapping (remember to update vma_off), and shorten its length.
*
* Case 4: *[*************]**
* The region completely contains the vmarea. Remove the vmarea from the
* list.
*
* Return 0 on success, or:
* - ENOMEM: Failed to allocate a new vmarea when splitting a vmarea (case 1).
*
* Hints:
* - Whenever you shorten/remove any mappings, be sure to call pt_unmap_range()
* tlb_flush_range() to clean your pagetables and TLB.
*/
long vmmap_remove(vmmap_t *map, size_t lopage, size_t npages)
{
// NOT_YET_IMPLEMENTED("VM: vmmap_remove");
// Iterate over the list of vmareas
list_iterate(&map->vmm_list, vma, vmarea_t, vma_plink)
{
// if the vmarea is completely inside the region to be unmapped
if (vma->vma_start < lopage && vma->vma_end > lopage + npages)
{
// split the old vmarea into two vmareas
vmarea_t *new_vmarea = vmarea_alloc();
if (new_vmarea == NULL)
{
return -ENOMEM;
}
// Set the fields of the new vmarea
new_vmarea->vma_start = lopage + npages;
new_vmarea->vma_end = vma->vma_end;
new_vmarea->vma_off += lopage + npages - vma->vma_start;
new_vmarea->vma_prot = vma->vma_prot;
new_vmarea->vma_flags = vma->vma_flags;
// new_vmarea->vma_vmmap = map;
mobj_lock(vma->vma_obj);
new_vmarea->vma_obj = vma->vma_obj;
// increment the refcount of the object associated with the vmarea
mobj_ref(new_vmarea->vma_obj);
mobj_unlock(vma->vma_obj);
// Shorten the length of the old vmarea
vma->vma_end = lopage;
// Insert the new vmarea into the map
vmmap_insert(map, new_vmarea);
// call pt_unmap_range() and tlb_flush_range() to clean the pagetables and TLB
pt_unmap_range(
curproc->p_pml4,
PN_TO_ADDR(lopage),
PN_TO_ADDR(lopage + npages));
tlb_flush_range(
PN_TO_ADDR(lopage),
npages
);
}
// if the region overlaps the end of the vmarea
else if (vma->vma_start < lopage && vma->vma_end > lopage && vma->vma_end <= lopage + npages)
{
// shorten the length of the mapping
vma->vma_end = lopage;
// call pt_unmap_range() and tlb_flush_range() to clean the pagetables and TLB
pt_unmap_range(
curproc->p_pml4,
PN_TO_ADDR(lopage),
PN_TO_ADDR(lopage + npages));
tlb_flush_range(
PN_TO_ADDR(lopage),
npages
);
}
// if the region overlaps the beginning of the vmarea
else if (vma->vma_start >= lopage && vma->vma_end > lopage + npages && vma->vma_start < lopage + npages)
{
// move the beginning of the mapping and shorten its length
vma->vma_off += lopage + npages - vma->vma_start;
vma->vma_start = lopage + npages;
// call pt_unmap_range() and tlb_flush_range() to clean the pagetables and TLB
pt_unmap_range(
curproc->p_pml4,
PN_TO_ADDR(lopage),
PN_TO_ADDR(lopage + npages));
tlb_flush_range(
PN_TO_ADDR(lopage),
npages
);
}
// if the region completely contains the vmarea
else if (vma->vma_start >= lopage && vma->vma_end <= lopage + npages)
{
// remove the vmarea from the list
list_remove(&vma->vma_plink);
vmarea_free(vma);
// call pt_unmap_range() and tlb_flush_range() to clean the pagetables and TLB
pt_unmap_range(
curproc->p_pml4,
PN_TO_ADDR(lopage),
PN_TO_ADDR(lopage + npages));
tlb_flush_range(
PN_TO_ADDR(lopage),
npages
);
}
}
return 0;
}
/*
* Returns 1 if the given address space has no mappings for the given range,
* 0 otherwise.
*/
long vmmap_is_range_empty(vmmap_t *map, size_t startvfn, size_t npages)
{
// NOT_YET_IMPLEMENTED("VM: vmmap_is_range_empty");
// Iterate over the list of vmareas
list_iterate(&map->vmm_list, vma, vmarea_t, vma_plink)
{
// if the range completely contains the vmarea
if (vma->vma_start <= startvfn && vma->vma_end >= startvfn + npages)
{
return 0;
}
// if the start of the vmarea is greater than or equal to the start of the range
if (vma->vma_start < startvfn + npages && vma->vma_start >= startvfn)
{
return 0;
}
// check if the end of the vmarea is greater than the start of the range
if (vma->vma_end > startvfn && vma->vma_end <= startvfn + npages)
{
return 0;
}
}
return 1;
}
/*
* Read into 'buf' from the virtual address space of 'map'. Start at 'vaddr'
* for size 'count'. 'vaddr' is not necessarily page-aligned. count is in bytes.
*
* Hints:
* 1) Find the vmareas that correspond to the region to read from.
* 2) Find the pframes within those vmareas corresponding to the virtual
* addresses you want to read.
* 3) Read from those page frames and copy it into `buf`.
* 4) You will not need to check the permissisons of the area.
* 5) You may assume/assert that all areas exist.
*
* Return 0 on success, -errno on error (propagate from the routines called).
* This routine will be used within copy_from_user().
*/
long vmmap_read(vmmap_t *map, const void *vaddr, void *buf, size_t count)
{
// NOT_YET_IMPLEMENTED("VM: vmmap_read");
// Iterate over the page numbers
size_t vfn = ADDR_TO_PN(vaddr);
size_t end_vfn = ADDR_TO_PN(vaddr + count);
size_t bytes_read = 0;
while (vfn <= end_vfn)
{
// Lookup the vmarea for this page number
vmarea_t *vma = vmmap_lookup(map, vfn);
if (vma == NULL)
{
return -EFAULT;
}
// Find the pframe for this page number
pframe_t *pf;
mobj_lock(vma->vma_obj);
long ret = mobj_get_pframe(vma->vma_obj, vfn - vma->vma_start + vma->vma_off, 0, &pf);
mobj_unlock(vma->vma_obj);
if (ret < 0)
{
return ret;
}
// Read from the pframe and copy it into buf
void *cursor = bytes_read + vaddr;
size_t bytes_this_iteration = MIN(PAGE_SIZE - PAGE_OFFSET(cursor), count - bytes_read);
memcpy(
(void *) buf + bytes_read,
(void *) pf->pf_addr + PAGE_OFFSET(cursor),
bytes_this_iteration
);
// Unlock the pframe
pframe_release(&pf);
// Increment the bytes read
bytes_read += bytes_this_iteration;
// check if we have read enough
if (bytes_read >= count)
{
return 0;
}
// Increment the page number
vfn++;
}
return 0;
}
/*
* Write from 'buf' into the virtual address space of 'map' starting at
* 'vaddr' for size 'count'.
*
* Hints:
* 1) Find the vmareas to write to.
* 2) Find the correct pframes within those areas that contain the virtual addresses
* that you want to write data to.
* 3) Write to the pframes, copying data from buf.
* 4) You do not need check permissions of the areas you use.
* 5) Assume/assert that all areas exist.
* 6) Remember to dirty the pages that you write to.
*
* Returns 0 on success, -errno on error (propagate from the routines called).
* This routine will be used within copy_to_user().
*/
long vmmap_write(vmmap_t *map, void *vaddr, const void *buf, size_t count)
{
// NOT_YET_IMPLEMENTED("VM: vmmap_write");
// Iterate over the page numbers
size_t vfn = ADDR_TO_PN(vaddr);
size_t end_vfn = ADDR_TO_PN(vaddr + count);
size_t bytes_written = 0;
while(vfn <= end_vfn)
{
// Lookup the vmarea for this page number
vmarea_t *vma = vmmap_lookup(map, vfn);
if (vma == NULL)
{
return -EFAULT;
}
// Find the pframe for this page number
pframe_t *pf;
mobj_lock(vma->vma_obj);
long ret = mobj_get_pframe(vma->vma_obj, vfn - vma->vma_start + vma->vma_off, 1, &pf);
mobj_unlock(vma->vma_obj);
if (ret < 0)
{
return ret;
}
// Write to the pframe, copying data from buf
void *cursor = bytes_written + vaddr;
size_t bytes_this_iteration = MIN(PAGE_SIZE - PAGE_OFFSET(cursor), count - bytes_written);
memcpy(
(void *)pf->pf_addr + PAGE_OFFSET(cursor),
(void *)buf + bytes_written,
bytes_this_iteration
);
// Dirty the pframe
pf->pf_dirty = 1;
// Unlock the pframe
pframe_release(&pf);
// Increment the bytes written
bytes_written += bytes_this_iteration;
// check if we have written enough
if (bytes_written >= count)
{
return 0;
}
// Increment the page number
vfn++;
}
return 0;
}
size_t vmmap_mapping_info(const void *vmmap, char *buf, size_t osize)
{
return vmmap_mapping_info_helper(vmmap, buf, osize, "");
}
size_t vmmap_mapping_info_helper(const void *vmmap, char *buf, size_t osize,
char *prompt)
{
KASSERT(0 < osize);
KASSERT(NULL != buf);
KASSERT(NULL != vmmap);
vmmap_t *map = (vmmap_t *)vmmap;
ssize_t size = (ssize_t)osize;
int len =
snprintf(buf, (size_t)size, "%s%37s %5s %7s %18s %11s %23s\n", prompt,
"VADDR RANGE", "PROT", "FLAGS", "MOBJ", "OFFSET", "VFN RANGE");
list_iterate(&map->vmm_list, vma, vmarea_t, vma_plink)
{
size -= len;
buf += len;
if (0 >= size)
{
goto end;
}
len =
snprintf(buf, (size_t)size,
"%s0x%p-0x%p %c%c%c %7s 0x%p %#.9lx %#.9lx-%#.9lx\n",
prompt, (void *)(vma->vma_start << PAGE_SHIFT),
(void *)(vma->vma_end << PAGE_SHIFT),
(vma->vma_prot & PROT_READ ? 'r' : '-'),
(vma->vma_prot & PROT_WRITE ? 'w' : '-'),
(vma->vma_prot & PROT_EXEC ? 'x' : '-'),
(vma->vma_flags & MAP_SHARED ? " SHARED" : "PRIVATE"),
vma->vma_obj, vma->vma_off, vma->vma_start, vma->vma_end);
}
end:
if (size <= 0)
{
size = osize;
buf[osize - 1] = '\0';
}
return osize - size;
}
|