Repository navigation
Expand file tree
/
Copy pathregion.c
More file actions
1493 lines (1234 loc) · 37.8 KB
File metadata and controls
- Code
- Blame
1493 lines (1234 loc) · 37.8 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
1
8000
97
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
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <minix/com.h>
#include <minix/callnr.h>
#include <minix/type.h>
#include <minix/config.h>
#include <minix/const.h>
#include <minix/sysutil.h>
#include <minix/syslib.h>
#include <minix/debug.h>
#include <minix/bitmap.h>
#include <minix/hash.h>
#include <machine/multiboot.h>
#include <sys/mman.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdint.h>
#include <sys/param.h>
#include "vm.h"
#include "proto.h"
#include "util.h"
#include "glo.h"
#include "region.h"
#include "sanitycheck.h"
#include "memlist.h"
#include "memtype.h"
#include "regionavl.h"
static struct vir_region *map_copy_region(struct vmproc *vmp, struct
vir_region *vr);
void map_region_init(void)
{
}
static void map_printregion(struct vir_region *vr)
{
unsigned int i;
struct phys_region *ph;
printf("map_printmap: map_name: %s\n", vr->def_memtype->name);
printf("\t%lx (len 0x%lx, %lukB), %p, %s\n",
vr->vaddr, vr->length, vr->length/1024,
vr->def_memtype->name,
(vr->flags & VR_WRITABLE) ? "writable" : "readonly");
printf("\t\tphysblocks:\n");
for(i = 0; i < vr->length/VM_PAGE_SIZE; i++) {
if(!(ph=vr->physblocks[i])) continue;
printf("\t\t@ %lx (refs %d): phys 0x%lx, %s\n",
(vr->vaddr + ph->offset),
ph->ph->refcount, ph->ph->phys,
pt_writable(vr->parent, vr->vaddr + ph->offset) ? "W" : "R");
}
}
struct phys_region *physblock_get(struct vir_region *region, vir_bytes offset)
{
int i;
struct phys_region *foundregion;
assert(!(offset % VM_PAGE_SIZE));
assert( /* offset >= 0 && */ offset < region->length);
i = offset/VM_PAGE_SIZE;
if((foundregion = region->physblocks[i]))
assert(foundregion->offset == offset);
return foundregion;
}
void physblock_set(struct vir_region *region, vir_bytes offset,
struct phys_region *newphysr)
{
int i;
struct vmproc *proc;
assert(!(offset % VM_PAGE_SIZE));
assert( /* offset >= 0 && */ offset < region->length);
i = offset/VM_PAGE_SIZE;
proc = region->parent;
assert(proc);
if(newphysr) {
assert(!region->physblocks[i]);
assert(newphysr->offset == offset);
proc->vm_total += VM_PAGE_SIZE;
if (proc->vm_total > proc->vm_total_max)
proc->vm_total_max = proc->vm_total;
} else {
assert(region->physblocks[i]);
proc->vm_total -= VM_PAGE_SIZE;
}
region->physblocks[i] = newphysr;
}
/*===========================================================================*
* map_printmap *
*===========================================================================*/
void map_printmap(struct vmproc *vmp)
{
struct vir_region *vr;
region_iter iter;
printf("memory regions in process %d:\n", vmp->vm_endpoint);
region_start_iter_least(&vmp->vm_regions_avl, &iter);
while((vr = region_get_iter(&iter))) {
map_printregion(vr);
region_incr_iter(&iter);
}
}
static struct vir_region *getnextvr(struct vir_region *vr)
{
struct vir_region *nextvr;
region_iter v_iter;
SLABSANE(vr);
region_start_iter(&vr->parent->vm_regions_avl, &v_iter, vr->vaddr, AVL_EQUAL);
assert(region_get_iter(&v_iter));
assert(region_get_iter(&v_iter) == vr);
region_incr_iter(&v_iter);
nextvr = region_get_iter(&v_iter);
if(!nextvr) return NULL;
SLABSANE(nextvr);
assert(vr->parent == nextvr->parent);
assert(vr->vaddr < nextvr->vaddr);
assert(vr->vaddr + vr->length <= nextvr->vaddr);
return nextvr;
}
static int pr_writable(struct vir_region *vr, struct phys_region *pr)
{
assert(pr->memtype->writable);
return ((vr->flags & VR_WRITABLE) && pr->memtype->writable(pr));
}
#if SANITYCHECKS
/*===========================================================================*
* map_sanitycheck_pt *
*===========================================================================*/
static int map_sanitycheck_pt(struct vmproc *vmp,
struct vir_region *vr, struct phys_region *pr)
{
struct phys_block *pb = pr->ph;
int rw;
int r;
if(pr_writable(vr, pr))
rw = PTF_WRITE;
else
rw = PTF_READ;
r = pt_writemap(vmp, &vmp->vm_pt, vr->vaddr + pr->offset,
pb->phys, VM_PAGE_SIZE, PTF_PRESENT | PTF_USER | rw, WMF_VERIFY);
if(r != OK) {
printf("proc %d phys_region 0x%lx sanity check failed\n",
vmp->vm_endpoint, pr->offset);
map_printregion(vr);
}
return r;
}
/*===========================================================================*
* map_sanitycheck *
*===========================================================================*/
void map_sanitycheck(const char *file, int line)
{
struct vmproc *vmp;
/* Macro for looping over all physical blocks of all regions of
* all processes.
*/
#define ALLREGIONS(regioncode, physcode) \
for(vmp = vmproc; vmp < &vmproc[VMP_NR]; vmp++) { \
vir_bytes voffset; \
region_iter v_iter; \
struct vir_region *vr; \
if(!(vmp->vm_flags & VMF_INUSE)) \
continue; \
region_start_iter_least(&vmp->vm_regions_avl, &v_iter); \
while((vr = region_get_ite
8000
r(&v_iter))) { \
struct phys_region *pr; \
regioncode; \
for(voffset = 0; voffset < vr->length; \
voffset += VM_PAGE_SIZE) { \
if(!(pr = physblock_get(vr, voffset))) \
continue; \
physcode; \
} \
region_incr_iter(&v_iter); \
} \
}
#define MYSLABSANE(s) MYASSERT(slabsane_f(__FILE__, __LINE__, s, sizeof(*(s))))
/* Basic pointers check. */
ALLREGIONS(MYSLABSANE(vr),MYSLABSANE(pr); MYSLABSANE(pr->ph);MYSLABSANE(pr->parent));
ALLREGIONS(/* MYASSERT(vr->parent == vmp) */,MYASSERT(pr->parent == vr););
/* Do counting for consistency check. */
ALLREGIONS(;,USE(pr->ph, pr->ph->seencount = 0;););
ALLREGIONS(;,MYASSERT(pr->offset == voffset););
ALLREGIONS(;,USE(pr->ph, pr->ph->seencount++;);
if(pr->ph->seencount == 1) {
if(pr->memtype->ev_sanitycheck)
pr->memtype->ev_sanitycheck(pr, file, line);
}
);
/* Do consistency check. */
ALLREGIONS({ struct vir_region *nextvr = getnextvr(vr);
if(nextvr) {
MYASSERT(vr->vaddr < nextvr->vaddr);
MYASSERT(vr->vaddr + vr->length <= nextvr->vaddr);
}
}
MYASSERT(!(vr->vaddr % VM_PAGE_SIZE));,
if(pr->ph->flags & PBF_INCACHE) pr->ph->seencount++;
if(pr->ph->refcount != pr->ph->seencount) {
map_printmap(vmp);
printf("ph in vr %p: 0x%lx refcount %u "
"but seencount %u\n",
vr, pr->offset,
pr->ph->refcount, pr->ph->seencount);
}
{
int n_others = 0;
struct phys_region *others;
if(pr->ph->refcount > 0) {
MYASSERT(pr->ph->firstregion);
if(pr->ph->refcount == 1) {
MYASSERT(pr->ph->firstregion == pr);
}
} else {
MYASSERT(!pr->ph->firstregion);
}
for(others = pr->ph->firstregion; others;
others = others->next_ph_list) {
MYSLABSANE(others);
MYASSERT(others->ph == pr->ph);
n_others++;
}
if(pr->ph->flags & PBF_INCACHE) n_others++;
MYASSERT(pr->ph->refcount == n_others);
}
MYASSERT(pr->ph->refcount == pr->ph->seencount);
MYASSERT(!(pr->offset % VM_PAGE_SIZE)););
ALLREGIONS(,MYASSERT(map_sanitycheck_pt(vmp, vr, pr) == OK));
}
#endif
/*=========================================================================*
* map_ph_writept *
*=========================================================================*/
int map_ph_writept(struct vmproc *vmp, struct vir_region *vr,
struct phys_region *pr)
{
int flags = PTF_PRESENT | PTF_USER;
struct phys_block *pb = pr->ph;
assert(vr);
assert(pr);
assert(pb);
assert(!(vr->vaddr % VM_PAGE_SIZE));
assert(!(pr->offset % VM_PAGE_SIZE));
assert(pb->refcount > 0);
if(pr_writable(vr, pr))
flags |= PTF_WRITE;
else
flags |= PTF_READ;
if(vr->def_memtype->pt_flags)
flags |= vr->def_memtype->pt_flags(vr);
if(pt_writemap(vmp, &vmp->vm_pt, vr->vaddr + pr->offset,
pb->phys, VM_PAGE_SIZE, flags,
#if SANITYCHECKS
!pr->written ? 0 :
#endif
WMF_OVERWRITE) != OK) {
printf("VM: map_writept: pt_writemap failed\n");
return ENOMEM;
}
#if SANITYCHECKS
USE(pr, pr->written = 1;);
#endif
return OK;
}
#define SLOT_FAIL ((vir_bytes) -1)
/*===========================================================================*
* region_find_slot_range *
*===========================================================================*/
static vir_bytes region_find_slot_range(struct vmproc *vmp,
vir_bytes minv, vir_bytes maxv, vir_bytes length)
{
struct vir_region *lastregion;
vir_bytes startv = 0;
int foundflag = 0;
region_iter iter;
SANITYCHECK(SCL_FUNCTIONS);
/* Length must be reasonable. */
assert(length > 0);
/* Special case: allow caller to set maxv to 0 meaning 'I want
* it to be mapped in right here.'
*/
if(maxv == 0) {
maxv = minv + length;
/* Sanity check. */
if(maxv <= minv) {
printf("region_find_slot: minv 0x%lx and bytes 0x%lx\n",
minv, length);
return SLOT_FAIL;
}
}
/* Basic input sanity checks. */
assert(!(length % VM_PAGE_SIZE));
if(minv >= maxv) {
printf("VM: 1 minv: 0x%lx maxv: 0x%lx length: 0x%lx\n",
minv, maxv, length);
}
assert(minv < maxv);
if(minv + length > maxv)
return SLOT_FAIL;
#define FREEVRANGE_TRY(rangestart, rangeend) { \
vir_bytes frstart = (rangestart), frend = (rangeend); \
frstart = MAX(frstart, minv); \
frend = MIN(frend, maxv); \
if(frend > frstart && (frend - frstart) >= length) { \
startv = frend-length; \
foundflag = 1; \
} }
#define FREEVRANGE(start, end) { \
assert(!foundflag); \
FREEVRANGE_TRY(((start)+VM_PAGE_SIZE), ((end)-VM_PAGE_SIZE)); \
if(!foundflag) { \
FREEVRANGE_TRY((start), (end)); \
} \
}
/* find region after maxv. */
region_start_iter(&vmp->vm_regions_avl, &iter, maxv, AVL_GREATER_EQUAL);
lastregion = region_get_iter(&iter);
if(!lastregion) {
/* This is the free virtual address space after the last region. */
region_start_iter(&vmp->vm_regions_avl, &iter, maxv, AVL_LESS);
lastregion = region_get_iter(&iter);
FREEVRANGE(lastregion ?
lastregion->vaddr+lastregion->length : 0, VM_DATATOP);
}
if(!foundflag) {
struct vir_region *vr;
while((vr = region_get_iter(&iter)) && !foundflag) {
struct vir_region *nextvr;
region_decr_iter(&iter);
nextvr = region_get_iter(&iter);
FREEVRANGE(nextvr ? nextvr->vaddr+nextvr->length : 0,
vr->vaddr);
}
}
if(!foundflag) {
return SLOT_FAIL;
}
/* However we got it, startv must be in the requested range. */
assert(startv >= minv);
assert(startv < maxv);
assert(startv + length <= maxv);
/* remember this position as a hint for next time. */
vmp->vm_region_top = startv + length;
return startv;
}
/*===========================================================================*
* region_find_slot *
*===========================================================================*/
static vir_bytes region_find_slot(struct vmproc *vmp,
vir_bytes minv, vir_bytes maxv, vir_bytes length)
{
vir_bytes v, hint = vmp->vm_region_top;
/* use the top of the last inserted region as a minv hint if
* possible. remember that a zero maxv is a special case.
*/
if(maxv && hint < maxv && hint >= minv) {
v = region_find_slot_range(vmp, minv, hint, length);
if(v != SLOT_FAIL)
return v;
}
return region_find_slot_range(vmp, minv, maxv, length);
}
static unsigned int phys_slot(vir_bytes len)
{
assert(!(len % VM_PAGE_SIZE));
return len / VM_PAGE_SIZE;
}
static struct vir_region *region_new(struct vmproc *vmp, vir_bytes startv, vir_bytes length,
int flags, mem_type_t *memtype)
{
struct vir_region *newregion;
struct phys_region **newphysregions;
static u32_t id;
int slots = phys_slot(length);
if(!(SLABALLOC(newregion))) {
printf("vm: region_new: could not allocate\n");
return NULL;
}
/* Fill in node details. */
USE(newregion,
memset(newregion, 0, sizeof(*newregion));
newregion->vaddr = startv;
newregion->length = length;
newregion->flags = flags;
newregion->def_memtype = memtype;
newregion->remaps = 0;
newregion->id = id++;
newregion->lower = newregion->higher = NULL;
newregion->parent = vmp;);
if(!(newphysregions = calloc(slots, sizeof(struct phys_region *)))) {
printf("VM: region_new: allocating phys blocks failed\n");
SLABFREE(newregion);
return NULL;
}
USE(newregion, newregion->physblocks = newphysregions;);
return newregion;
}
/*===========================================================================*
* map_page_region *
*===========================================================================*/
struct vir_region *map_page_region(struct vmproc *vmp, vir_bytes minv,
vir_bytes maxv, vir_bytes length, u32_t flags, int mapflags,
mem_type_t *memtype)
{
struct vir_region *newregion;
vir_bytes startv;
assert(!(length % VM_PAGE_SIZE));
SANITYCHECK(SCL_FUNCTIONS);
startv = region_find_slot(vmp, minv, maxv, length);
if (startv == SLOT_FAIL)
return NULL;
/* Now we want a new region. */
if(!(newregion = region_new(vmp, startv, length, flags, memtype))) {
printf("VM: map_page_region: allocating region failed\n");
return NULL;
}
/* If a new event is specified, invoke it. */
if(newregion->def_memtype->ev_new) {
if(newregion->def_memtype->ev_new(newregion) != OK) {
/* ev_new will have freed and removed the region */
return NULL;
}
}
if(mapflags & MF_PREALLOC) {
if(map_handle_memory(vmp, newregion, 0, length, 1,
NULL, 0, 0) != OK) {
printf("VM: map_page_region: prealloc failed\n");
free(newregion->physblocks);
USE(newregion,
newregion->physblocks = NULL;);
SLABFREE(newregion);
return NULL;
}
}
/* Pre-allocations should be uninitialized, but after that it's a
* different story.
*/
USE(newregion, newregion->flags &= ~VR_UNINITIALIZED;);
/* Link it. */
region_insert(&vmp->vm_regions_avl, newregion);
#if SANITYCHECKS
assert(startv == newregion->vaddr);
{
struct vir_region *nextvr;
if((nextvr = getnextvr(newregion))) {
assert(newregion->vaddr < nextvr->vaddr);
}
}
#endif
SANITYCHECK(SCL_FUNCTIONS);
return newregion;
}
/*===========================================================================*
* map_subfree *
*===========================================================================*/
static int map_subfree(struct vir_region *region,
vir_bytes start, vir_bytes len)
{
struct phys_region *pr;
vir_bytes end = start+len;
vir_bytes voffset;
#if SANITYCHECKS
SLABSANE(region);
for(voffset = 0; voffset < phys_slot(region->length);
voffset += VM_PAGE_SIZE) {
struct phys_region *others;
struct phys_block *pb;
if(!(pr = physblock_get(region, voffset)))
continue;
pb = pr->ph;
for(others = pb->firstregion; others;
others = others->next_ph_list) {
assert(others->ph == pb);
}
}
#endif
for(voffset = start; voffset < end; voffset+=VM_PAGE_SIZE) {
if(!(pr = physblock_get(region, voffset)))
continue;
assert(pr->offset >= start);
assert(pr->offset < end);
pb_unreferenced(region, pr, 1);
SLABFREE(pr);
}
return OK;
}
/*===========================================================================*
* map_free *
*===========================================================================*/
int map_free(struct vir_region *region)
{
int r;
if((r=map_subfree(region, 0, region->length)) != OK) {
printf("%d\n", __LINE__);
return r;
}
if(region->def_memtype->ev_delete)
region->def_memtype->ev_delete(region);
free(region->physblocks);
region->physblocks = NULL;
SLABFREE(region);
return OK;
}
/*========================================================================*
* map_free_proc *
*========================================================================*/
int map_free_proc(struct vmproc *vmp)
{
struct vir_region *r;
while((r = region_search_root(&vmp->vm_regions_avl))) {
SANITYCHECK(SCL_DETAIL);
#if SANITYCHECKS
nocheck++;
#endif
region_remove(&vmp->vm_regions_avl, r->vaddr); /* For sanity checks. */
map_free(r);
#if SANITYCHECKS
nocheck--;
#endif
SANITYCHECK(SCL_DETAIL);
}
region_init(&vmp->vm_regions_avl);
SANITYCHECK(SCL_FUNCTIONS);
return OK;
}
/*===========================================================================*
* map_lookup *
*===========================================================================*/
struct vir_region *map_lookup(struct vmproc *vmp,
vir_bytes offset, struct phys_region **physr)
{
struct vir_region *r;
SANITYCHECK(SCL_FUNCTIONS);
#if SANITYCHECKS
if(!region_search_root(&vmp->vm_regions_avl))
panic("process has no regions: %d", vmp->vm_endpoint);
#endif
if((r = region_search(&vmp->vm_regions_avl, offset, AVL_LESS_EQUAL))) {
vir_bytes ph;
if(offset >= r->vaddr && offset < r->vaddr + r->length) {
ph = offset - r->vaddr;
if(physr) {
*physr = physblock_get(r, ph);
if(*physr) assert((*physr)->offset == ph);
}
return r;
}
}
SANITYCHECK(SCL_FUNCTIONS);
return NULL;
}
u32_t vrallocflags(u32_t flags)
{
u32_t allocflags = 0;
if(flags & VR_PHYS64K)
allocflags |= PAF_ALIGN64K;
if(flags & VR_LOWER16MB)
allocflags |= PAF_LOWER16MB;
if(flags & VR_LOWER1MB)
allocflags |= PAF_LOWER1MB;
if(!(flags & VR_UNINITIALIZED))
allocflags |= PAF_CLEAR;
return allocflags;
}
/*===========================================================================*
* map_pf *
*===========================================================================*/
int map_pf(struct vmproc *vmp,
struct vir_region *region,
vir_bytes offset,
int write,
vfs_callback_t pf_callback,
void *state,
int len,
int *io)
{
struct phys_region *ph;
int r = OK;
offset -= offset % VM_PAGE_SIZE;
/* assert(offset >= 0); */ /* always true */
assert(offset < region->length);
assert(!(region->vaddr % VM_PAGE_SIZE));
assert(!(write && !(region->flags & VR_WRITABLE)));
SANITYCHECK(SCL_FUNCTIONS);
if(!(ph = physblock_get(region, offset))) {
struct phys_block *pb;
/* New block. */
if(!(pb = pb_new(MAP_NONE))) {
printf("map_pf: pb_new failed\n");
return ENOMEM;
}
if(!(ph = pb_reference(pb, offset, region,
region->def_memtype))) {
printf("map_pf: pb_reference failed\n");
pb_free(pb);
return ENOMEM;
}
}
assert(ph);
assert(ph->ph);
/* If we're writing and the block is already
* writable, nothing to do.
*/
assert(ph->memtype->writable);
if(!write || !ph->memtype->writable(ph)) {
assert(ph->memtype->ev_pagefault);
assert(ph->ph);
if((r = ph->memtype->ev_pagefault(vmp,
region, ph, write, pf_callback, state, len, io)) == SUSPEND) {
return SUSPEND;
}
if(r != OK) {
#if 0
printf("map_pf: pagefault in %s failed\n", ph->memtype->name);
#endif
if(ph)
pb_unreferenced(region, ph, 1);
return r;
}
assert(ph);
assert(ph->ph);
assert(ph->ph->phys != MAP_NONE);
}
assert(ph->ph);
assert(ph->ph->phys != MAP_NONE);
if((r = map_ph_writept(vmp, region, ph)) != OK) {
printf("map_pf: writept failed\n");
return r;
}
SANITYCHECK(SCL_FUNCTIONS);
#if SANITYCHECKS
if(OK != pt_checkrange(&vmp->vm_pt, region->vaddr+offset,
VM_PAGE_SIZE, write)) {
panic("map_pf: pt_checkrange failed: %d", r);
}
#endif
return r;
}
int map_handle_memory(struct vmproc *vmp,
struct vir_region *region, vir_bytes start_offset, vir_bytes length,
int write, vfs_callback_t cb, void *state, int statelen)
{
vir_bytes offset, lim;
int r;
int io = 0;
assert(length > 0);
lim = start_offset + length;
assert(lim > start_offset);
for(offset = start_offset; offset < lim; offset += VM_PAGE_SIZE)
if((r = map_pf(vmp, region, offset, write,
cb, state, statelen, &io)) != OK)
return r;
return OK;
}
/*===========================================================================*
* map_pin_memory *
*===========================================================================*/
int map_pin_memory(struct vmproc *vmp)
{
struct vir_region *vr;
int r;
region_iter iter;
region_start_iter_least(&vmp->vm_regions_avl, &iter);
/* Scan all memory regions. */
while((vr = region_get_iter(&iter))) {
/* Make sure region is mapped to physical memory and writable.*/
r = map_handle_memory(vmp, vr, 0, vr->length, 1, NULL, 0, 0);
if(r != OK) {
panic("map_pin_memory: map_handle_memory failed: %d", r);
}
region_incr_iter(&iter);
}
return OK;
}
/*===========================================================================*
* map_copy_region *
*===========================================================================*/
struct vir_region *map_copy_region(struct vmproc *vmp, struct vir_region *vr)
{
/* map_copy_region creates a complete copy of the vir_region
* data structure, linking in the same phys_blocks directly,
* but all in limbo, i.e., the caller has to link the vir_region
* to a process. Therefore it doesn't increase the refcount in
* the phys_block; the caller has to do this once it's linked.
* The reason for this is to keep the sanity checks working
* within this function.
*/
struct vir_region *newvr;
struct phys_region *ph;
int r;
#if SANITYCHECKS
unsigned int cr;
cr = physregions(vr);
#endif
vir_bytes p;
if(!(newvr = region_new(vr->parent, vr->vaddr, vr->length, vr->flags, vr->def_memtype)))
return NULL;
USE(newvr, newvr->parent = vmp;);
if(vr->def_memtype->ev_copy && (r=vr->def_memtype->ev_copy(vr, newvr)) != OK) {
map_free(newvr);
printf("VM: memtype-specific copy failed (%d)\n", r);
return NULL;
}
for(p = 0; p < phys_slot(vr->length); p++) {
struct phys_region *newph;
if(!(ph = physblock_get(vr, p*VM_PAGE_SIZE))) continue;
newph = pb_reference(ph->ph, ph->offset, newvr,
vr->def_memtype);
if(!newph) { map_free(newvr); return NULL; }
if(ph->memtype->ev_reference)
ph->memtype->ev_reference(ph, newph);
#if SANITYCHECKS
USE(newph, newph->written = 0;);
assert(physregions(vr) == cr);
#endif
}
#if SANITYCHECKS
assert(physregions(vr) == physregions(newvr));
#endif
return newvr;
}
/*===========================================================================*
* copy_abs2region *
*===========================================================================*/
int copy_abs2region(phys_bytes absaddr, struct vir_region *destregion,
phys_bytes offset, phys_bytes len)
{
assert(destregion);
assert(destregion->physblocks);
while(len > 0) {
phys_bytes sublen, suboffset;
struct phys_region *ph;
assert(destregion);
assert(destregion->physblocks);
if(!(ph = physblock_get(destregion, offset))) {
printf("VM: copy_abs2region: no phys region found (1).\n");
return EFAULT;
}
assert(ph->offset <= offset);
if(ph->offset+VM_PAGE_SIZE <= offset) {
printf("VM: copy_abs2region: no phys region found (2).\n");
return EFAULT;
}
suboffset = offset - ph->offset;
assert(suboffset < VM_PAGE_SIZE);
sublen = len;
if(sublen > VM_PAGE_SIZE - suboffset)
sublen = VM_PAGE_SIZE - suboffset;
assert(suboffset + sublen <= VM_PAGE_SIZE);
if(ph->ph->refcount != 1) {
printf("VM: copy_abs2region: refcount not 1.\n");
return EFAULT;
}
if(sys_abscopy(absaddr, ph->ph->phys + suboffset, sublen) != OK) {
printf("VM: copy_abs2region: abscopy failed.\n");
return EFAULT;
}
absaddr += sublen;
offset += sublen;
len -= sublen;
}
return OK;
}
/*=========================================================================*
* map_writept *
*=========================================================================*/
int map_writept(struct vmproc *vmp)
{
struct vir_region *vr;
struct phys_region *ph;
int r;
region_iter v_iter;
region_start_iter_least(&vmp->vm_regions_avl, &v_iter);
while((vr = region_get_iter(&v_iter))) {
vir_bytes p;
for(p = 0; p < vr->length; p += VM_PAGE_SIZE) {
if(!(ph = physblock_get(vr, p))) continue;
if((r=map_ph_writept(vmp, vr, ph)) != OK) {
printf("VM: map_writept: failed\n");
return r;
}
}
region_incr_iter(&v_iter);
}
return OK;
}
/*========================================================================*
* map_proc_copy *
*========================================================================*/
int map_proc_copy(struct vmproc *dst, struct vmproc *src)
{
/* Copy all the memory regions from the src process to the dst process. */
region_init(&dst->vm_regions_avl);
return map_proc_copy_from(dst, src, NULL);
}
/*========================================================================*
* map_proc_copy_from *
*========================================================================*/
int map_proc_copy_from(struct vmproc *dst, struct vmproc *src,
struct vir_region *start_src_vr)
{
struct vir_region *vr;
region_iter v_iter;
if(!start_src_vr)
start_src_vr = region_search_least(&src->vm_regions_avl);
assert(start_src_vr);
assert(start_src_vr->parent == src);
region_start_iter(&src->vm_regions_avl, &v_iter,
start_src_vr->vaddr, AVL_EQUAL);
assert(region_get_iter(&v_iter) == start_src_vr);
/* Copy source regions after the destination's last region (if any). */
SANITYCHECK(SCL_FUNCTIONS);
while((vr = region_get_iter(&v_iter))) {
struct vir_region *newvr;
if(!(newvr = map_copy_region(dst, vr))) {
map_free_proc(dst);
return ENOMEM;
}
region_insert(&dst->vm_regions_avl, newvr);
assert(vr->length == newvr->length);
#if SANITYCHECKS
{
vir_bytes vaddr;
struct phys_region *orig_ph, *new_ph;
assert(vr->physblocks != newvr->physblocks);
for(vaddr = 0; vaddr < vr->length; vaddr += VM_PAGE_SIZE) {
orig_ph = physblock_get(vr, vaddr);
new_ph = physblock_get(newvr, vaddr);
if(!orig_ph) { assert(!new_ph); continue;}
assert(new_ph);
assert(orig_ph != new_ph);
assert(orig_ph->ph == new_ph->ph);
}
}
#endif
region_incr_iter(&v_iter);
}
map_writept(src);
map_writept(dst);
SANITYCHECK(SCL_FUNCTIONS);
return OK;
}
int map_region_extend_upto_v(struct vmproc *vmp, vir_bytes v)
{
vir_bytes offset = v, limit, extralen;