-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapdb.c
1386 lines (1217 loc) · 40 KB
/
mapdb.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
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 <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <ccan/htable/htable.h>
#include <ccan/likely/likely.h>
#include <ccan/minmax/minmax.h>
#include <l4/types.h>
#include <ukernel/bug.h>
#include <ukernel/misc.h>
#include <ukernel/util.h>
#include <ukernel/trace.h>
#include <ukernel/space.h>
#include <ukernel/mapdb.h>
#include <ukernel/ptab.h>
#define TRACE(fmt, ...) TRACE_MSG(TRID_MAPDB, fmt, ##__VA_ARGS__)
/* mapdb's child/parent reference format is made out of four types of entry in
* a 32-bit unsigned integer. the lowest bits indicate entry type:
* - ....1 = reference
* - ..000 = null (other bits zero)
* - ..010 = tombstone (other bits zero, up=1)
* - ..100 = no-parent or root mapping indicator
* - ..110 = child bucket pointer (index set, up=6)
*
* empties and tombstones only appear in the child array as part of its
* hashing mechanism.
*
* references appear in child and parent tables. its subfields are:
* - bits 31..11: 21 bits of group ID.
* - bits 10..1: 10 bits of index into group.
*
* no-parent indicators appear only in the parent table. its subfields are:
* - bits 10..8: L4.X2 rights mask
* - bits 13..11: "up" access accumulator
* - bit 14 is exempt flag
* - the rest are zero.
*
* child bucket pointers appear in the child table. its value, shifted to the
* right by 3 bits, identifies a <struct child_bucket> within child_bucket_ra.
* a child bucket pointer's corresponding c_aux[] slot contains the parent
* index and a zero rights mask; its "up" accumulator is ignored.
*/
/* constants */
#define REF_NULL 0
#define REF_TOMBSTONE 2
#define REF_NO_PARENT 4
#define REF_BUCKET 6
#define REF_TYPE_MASK 7
#define REF_EXEMPT_BIT 0x4000
/* subfield ctors */
#define REF(ix, grp) (1 | (ix) << 1 | (grp) << 11)
#define REF_ROOT(rights) (REF_NO_PARENT | (rights) << 8)
/* subfield access */
#define REF_INDEX(ref) (((ref) >> 1) & 0x3ff)
#define REF_GROUP_ID(ref) (((ref) >> 11) & 0x1fffff)
#define REF_ADDR(ref) (REF_INDEX((ref)) * PAGE_SIZE)
#define REF_ROOT_RIGHTS(x) (((x) >> 8) & 0x7)
#define REF_ROOT_UP(x) (((x) >> 11) & 0x7)
/* format predicates */
#define IS_REF(x) ((x) & 1)
#define IS_NULL(x) ((x) == 0)
#define IS_ROOT(x) (((x) & REF_TYPE_MASK) == REF_NO_PARENT)
#define IS_EXEMPT(x) (IS_ROOT(x) && ((x) & REF_EXEMPT_BIT))
#define IS_BUCKET(x) (((x) & REF_TYPE_MASK) == REF_BUCKET)
/* the c_aux[] format. */
#define AUX(index, rights, up) (((index) & 0x3ff) \
| ((rights) & 0x7) << 10 | ((up) & 0x7) << 13)
#define AUX_INDEX(x) ((x) & 0x3ff)
#define AUX_RIGHTS(x) (((x) >> 10) & 0x7)
#define AUX_UP(x) (((x) >> 13) & 0x7)
#define AUX_IS_AVAIL(x) (AUX_UP((x)) == 0)
/* ctors */
#define AUX_TOMBSTONE AUX(0, 0, 1)
#define AUX_BUCKETPTR(ix) AUX((ix), 0, 6)
/* maximum probe depth in childref storage. */
#define MAX_PROBE_DEPTH 15
/* maximum # of items sharing the same parent index in childref storage
* before being placed in bucket.
*/
#define MAX_SHARE_CHAIN 3
/* mode bits for unmap_page() on top of the ones for mapdb_unmap(). */
#define UM_RIGHTS 0x70 /* L4_Rights() in mask */
#define UM_CHILD 0x80 /* not a primary Unmap target */
/* usage:
*
* struct ref_iter it;
* for_each_child(&it, grp, parent_ix, c, aux) {
* if(predicate(c, aux)) ref_del(&it, grp);
* }
*/
#define for_each_child(it, grp, p_ix, c, aux) \
for(uint32_t aux, c = ref_first(&(aux), (it), (grp), (p_ix)); \
(c) != 0; \
(c) = ref_next(&(aux), (it), (grp)))
struct ref_iter {
int ix, pos, lim, mask;
uint32_t *b_cs;
uint16_t *b_aux;
int bpos, blim;
};
/* ->cs and ->aux are a childref array just like ->children and ->c_aux in
* map_group, having allocated space for 1 << ->alloc_log2 entries. ->max is
* one past the last valid index, i.e. 0 for empty. there are no holes;
* ref_del() moves the last entry back.
*/
struct child_bucket {
int alloc_log2, max;
uint32_t *cs;
uint16_t *aux; /* alloc'd in same chunk w/ ->cs */
};
struct rangealloc *map_group_ra = NULL;
static struct rangealloc *child_bucket_ra = NULL;
/* tests a known-valid pointer for whether the group is also valid. this is
* ensured by the group ctor and dtor.
*/
static inline bool is_group_valid(struct map_group *grp) {
return grp->space != NULL;
}
/* never fails. */
static struct map_group *group_from_id(long id)
{
assert(id > 0);
#ifndef NDEBUG
if(unlikely(catch_pf() != 0)) {
BUG_ON(true, "lookup of map_group id=%ld faulted!", id);
}
#endif
/* TODO: use constant shifts instead as in find_group() */
struct map_group *g = ra_id2ptr(map_group_ra, id);
BUG_ON(!is_group_valid(g), "group id=%ld isn't alive!", id);
#ifndef NDEBUG
uncatch_pf();
#endif
return g;
}
static inline void ref_set_aux(
struct ref_iter *it, struct map_group *g, int newval)
{
assert(AUX_RIGHTS(newval) != 0); /* use ref_del() instead */
if(it->b_aux != NULL) {
it->b_aux[it->bpos] = newval;
} else {
g->c_aux[it->pos & it->mask] = newval;
}
}
static void ref_enter_bucket(
struct ref_iter *it, struct map_group *g, uint32_t bptr)
{
assert(IS_BUCKET(bptr));
struct child_bucket *b = ra_id2ptr(child_bucket_ra, bptr >> 3);
assert(b->cs != NULL);
assert(b->aux == (uint16_t *)&b->cs[1 << b->alloc_log2]);
it->b_cs = b->cs;
it->b_aux = b->aux;
it->bpos = -1;
it->blim = b->max + 1;
}
static uint32_t ref_next(
uint32_t *aux_p,
struct ref_iter *it, struct map_group *g)
{
if(it->b_cs != NULL) {
bucketmode:
if(++it->bpos < it->blim) {
*aux_p = it->b_aux[it->bpos];
return it->b_cs[it->bpos];
} else {
/* exit the bucket. */
it->b_cs = NULL;
it->b_aux = NULL;
}
}
while(it->pos + 1 < it->lim) {
int slot = ++it->pos & it->mask;
uint32_t c = g->children[slot];
int aux = g->c_aux[slot];
if(IS_REF(c) && AUX_INDEX(aux) == it->ix) {
*aux_p = aux;
return c;
}
if(c == 0) break;
if(IS_BUCKET(c)) {
ref_enter_bucket(it, g, c);
assert(it->b_cs != NULL);
goto bucketmode;
}
assert(c == REF_TOMBSTONE
|| (IS_REF(c) && AUX_INDEX(aux) != it->ix));
}
return 0;
}
/* NOTE: @it only becomes valid if the return value is not 0. */
static uint32_t ref_first(
uint32_t *aux_p,
struct ref_iter *it, struct map_group *g, int ix)
{
if(MG_N_ALLOC_LOG2(g) == 0) return 0;
it->ix = ix;
it->mask = (1 << MG_N_ALLOC_LOG2(g)) - 1;
it->pos = int_hash(ix) & it->mask;
it->lim = it->pos + min(1 << MG_N_ALLOC_LOG2(g), MAX_PROBE_DEPTH);
uint32_t c = g->children[it->pos];
if(IS_BUCKET(c)) ref_enter_bucket(it, g, c);
else {
it->b_cs = NULL;
it->b_aux = NULL;
if(IS_REF(c) && AUX_INDEX(*aux_p = g->c_aux[it->pos]) == ix) return c;
}
return ref_next(aux_p, it, g);
}
/* set tombstone, clear c_aux. */
static void ref_del(struct ref_iter *it, struct map_group *g)
{
if(it->b_cs != NULL) {
int slot = it->pos & it->mask;
uint32_t c = g->children[slot];
assert(IS_BUCKET(c));
struct child_bucket *b = ra_id2ptr(child_bucket_ra, c >> 3);
assert(b->cs != NULL);
assert(b->aux == (uint16_t *)&b->cs[1 << b->alloc_log2]);
assert(b->max > 0);
it->b_cs[it->bpos] = it->b_cs[b->max];
it->b_aux[it->bpos] = it->b_aux[b->max];
if(--b->max == 0) {
/* dispose of the bucket. */
g->children[slot] = b->cs[0];
g->c_aux[slot] = b->aux[0];
free(b->cs);
ra_free(child_bucket_ra, b);
it->b_cs = NULL; it->b_aux = NULL;
} else {
/* visit the item that hopped back, or terminate normally. */
it->bpos--;
it->blim--;
}
} else {
int ix = it->pos & it->mask;
g->children[ix] = REF_TOMBSTONE;
g->c_aux[ix] = AUX_TOMBSTONE;
}
}
/* find the parent group and child array index matching @parent_ref and
* @child_ref. when found, returns the group and leaves *@it at the child
* reference; when not found, returns NULL.
*
* usage: @parent_ref is g->parent[ix]; @child_ref is REF(ix, g_id).
*/
static inline struct map_group *find_child(
struct ref_iter *it, int *aux_p,
uint32_t parent_ref, uint32_t child_ref)
{
assert(IS_REF(parent_ref));
assert(IS_REF(child_ref));
struct map_group *pg = group_from_id(REF_GROUP_ID(parent_ref));
for_each_child(it, pg, REF_INDEX(parent_ref), c, aux) {
if(c == child_ref) {
*aux_p = aux;
return pg;
}
}
return NULL;
}
/* find_child(), but never returns NULL. */
static struct map_group *get_child(
struct ref_iter *it, int *aux_p,
uint32_t p, uint32_t c)
{
struct map_group *g = find_child(it, aux_p, p, c);
#ifndef NDEBUG
BUG_ON(g == NULL, "%s: called from %p; cref={gid=%#x,ix=%#x}", __func__,
__builtin_return_address(0), REF_GROUP_ID(c), REF_INDEX(c));
#endif
return g;
}
#if 0
static void dump_map_group(struct map_group *g)
{
#ifndef NDEBUG
L4_ThreadId_t space_id = space_name(g->space);
TRACE("%s: group %#lx..%#lx in %lu:%lu (#children<=%lu, #maps=%lu):\n",
__func__, MG_START(g), MG_START(g) + GROUP_SIZE - 1,
L4_ThreadNo(space_id), L4_Version(space_id),
1ul << MG_N_ALLOC_LOG2(g), MG_N_MAPS(g));
for(int i=0; i < PAGES_PER_GROUP; i++) {
uint32_t pgid = 0;
int rights = mapdb_query(&pgid, g->space, NULL,
MG_START(g) + i * PAGE_SIZE);
if(rights == 0) continue;
TRACE(" %d: %#lx:%#lx rights=[%c%c%c] parent=%#x pgid=%u\n", i,
(L4_Word_t)MG_START(g) + i * PAGE_SIZE, (L4_Word_t)PAGE_SIZE,
CHECK_FLAG(rights, L4_Readable) ? 'r' : '-',
CHECK_FLAG(rights, L4_Writable) ? 'w' : '-',
CHECK_FLAG(rights, L4_eXecutable) ? 'x' : '-',
g->parent[i], pgid);
}
#endif
}
#endif
#ifndef DEBUG_ME_HARDER
#define check_map_group(g) true
#else
#include <ukernel/invariant.h>
static bool check_map_group(struct map_group *g)
{
INV_CTX;
inv_push("g=%p (id=%u): start=%#lx, n_alloc=%d, n_maps=%lu",
g, ra_ptr2id(map_group_ra, g),
MG_START(g), 1 << MG_N_ALLOC_LOG2(g), MG_N_MAPS(g));
inv_ok1(g->parent != NULL);
int map_count = 0;
for(int i=0; i < PAGES_PER_GROUP; i++) {
uint32_t p = g->parent[i];
inv_push("g->parent[%d]=%#x (ix=%u group_id=%u)",
i, p, REF_INDEX(p), REF_GROUP_ID(p));
if(p != 0) map_count++;
inv_imply1(!IS_NULL(p), IS_ROOT(p) || IS_REF(p));
/* (the reason behind the second clause is that when unmap_page() is
* about to destroy a map_group after removing its last root page,
* REF_ROOT_RIGHTS() for that entry won't satisfy this condition.
* rather than remove the call to check_map_group(), we'll weaken the
* condition.)
*/
inv_imply1(IS_ROOT(p),
REF_ROOT_RIGHTS(p) > 0 || MG_N_MAPS(g) == 1);
/* non-root maps should have a corresponding child pointer in the
* indicated parent.
*/
if(IS_REF(p)) {
if(unlikely(catch_pf() != 0)) {
inv_ok(false, "caught pagefault on parent group");
NOT_REACHED;
}
struct map_group *pg = ra_id2ptr(map_group_ra, REF_GROUP_ID(p));
inv_ok1(is_group_valid(pg));
uint32_t pp = pg->parent[REF_INDEX(p)];
uncatch_pf();
inv_ok1(IS_ROOT(pp) || IS_REF(pp));
struct ref_iter it;
int aux;
pg = find_child(&it, &aux, p, REF(i, ra_ptr2id(map_group_ra, g)));
inv_ok1(pg != NULL);
inv_ok1(AUX_RIGHTS(aux) > 0);
inv_ok1(AUX_INDEX(aux) == REF_INDEX(p));
}
inv_pop();
}
inv_log("map_count=%d", map_count);
inv_ok1(map_count == MG_N_MAPS(g));
inv_push("child array, structural");
inv_imply1(MG_N_ALLOC_LOG2(g) == 0,
g->children == NULL && g->c_aux == NULL);
inv_imply1(MG_N_ALLOC_LOG2(g) > 0,
g->children != NULL && g->c_aux != NULL);
inv_pop();
if(MG_N_ALLOC_LOG2(g) > 0) {
inv_push("child array, logical");
/* each child index may appear either not at all, in at most
* MAX_SHARE_CHAIN simple child pointers, or exactly once as a bucket.
*/
uint8_t seen[PAGES_PER_GROUP];
for(int i=0; i < NUM_ELEMENTS(seen); i++) seen[i] = 0;
for(int i=0, lim = 1 << MG_N_ALLOC_LOG2(g); i < lim; i++) {
uint32_t c = g->children[i];
int ix = AUX_INDEX(g->c_aux[i]);
inv_push("c=%#x, aux=%#x, ix=%d", c, g->c_aux[i], ix);
inv_imply1(IS_BUCKET(c), !(seen[ix] & 1));
inv_ok1(!(seen[ix] & 2));
if(IS_REF(c)) seen[ix] |= 1;
if(IS_BUCKET(c)) seen[ix] |= 2;
inv_pop();
}
inv_pop();
inv_push("child array, reference");
/* deference all child pointers. they should be valid. */
for(int i=0, lim = 1 << MG_N_ALLOC_LOG2(g); i < lim; i++) {
uint32_t cv = g->children[i], *c;
uint16_t auxv = g->c_aux[i], *aux;
inv_log("cv=%#x, auxv=%#x", cv, auxv);
int sublim;
if(IS_REF(cv)) {
c = &cv;
aux = &auxv;
sublim = 1;
} else if(IS_BUCKET(cv)) {
struct child_bucket *b = ra_id2ptr(child_bucket_ra, cv >> 3);
inv_ok1(b->cs != NULL);
inv_ok1(b->aux == (uint16_t *)&b->cs[1 << b->alloc_log2]);
c = b->cs;
aux = b->aux;
sublim = b->max + 1;
} else {
inv_ok1(IS_NULL(cv) || IS_ROOT(cv) || cv == REF_TOMBSTONE);
continue;
}
for(int j=0; j < sublim; j++) {
cv = c[j];
auxv = aux[j];
if(cv == 0) continue;
inv_ok1(IS_REF(cv));
inv_push("child group id is %u", REF_GROUP_ID(cv));
struct map_group *cg = ra_id2ptr(map_group_ra,
REF_GROUP_ID(cv));
inv_ok1(is_group_valid(cg));
int cv_ix = REF_INDEX(cv);
inv_ok1(IS_REF(cg->parent[cv_ix]));
inv_ok1(REF_INDEX(cg->parent[cv_ix]) == AUX_INDEX(auxv));
inv_pop();
}
}
inv_pop();
}
inv_pop(); /* for g=... */
return true;
inv_fail:
return false;
}
#endif
static void destroy_group(struct map_group *g)
{
htable_del(&g->space->ptab_groups, int_hash(MG_START(g)), g);
free(g->parent);
free(g->children); /* also releases g->c_aux */
g->children = NULL; g->c_aux = NULL; g->parent = NULL;
x86_free_ptab(g);
g->space = NULL;
assert(!is_group_valid(g));
ra_free(map_group_ra, g);
}
static struct map_group *add_group(struct space *sp, uintptr_t addr)
{
struct map_group *g = ra_alloc(map_group_ra, -1);
if(unlikely(g == NULL)) return NULL;
assert(!is_group_valid(g));
*g = (struct map_group){
.space = sp, .addr = addr & ~GROUP_MASK,
.parent = calloc(GROUP_SIZE / PAGE_SIZE, sizeof(uint32_t)),
};
TRACE("%s: sp=%p, g=%p (id=%u), ->addr=%#lx\n", __func__, sp, g,
ra_ptr2id(map_group_ra, g), g->addr);
int n = x86_alloc_ptab(g);
bool ok = htable_add(&sp->ptab_groups, int_hash(MG_START(g)), g);
if(g->parent == NULL || n < 0 || !ok) {
destroy_group(g);
return NULL;
}
return g;
}
static inline bool _cmp_group_addr(const void *cand, void *keyptr) {
const struct map_group *g = cand;
return MG_START(g) == *(uintptr_t *)keyptr;
}
struct map_group *find_group(struct space *sp, uintptr_t addr) {
uintptr_t key = addr & ~GROUP_MASK;
return htable_get(&sp->ptab_groups, int_hash(key), &_cmp_group_addr, &key);
}
/* turn @addr within @g into a group-id + index pair. */
static inline L4_Word_t addr_to_ref(struct map_group *g, uintptr_t addr)
{
assert(addr >= MG_START(g) && addr < MG_START(g) + GROUP_SIZE);
assert(map_group_ra->id_shift == 5);
uintptr_t grp_bits = ((uintptr_t)g & map_group_ra->and_mask) << 6,
ix = (addr - MG_START(g)) >> (PAGE_BITS - 1);
L4_Word_t ref = grp_bits | ix;
assert(REF_ADDR(ref) + MG_START(g) == (addr & ~PAGE_MASK));
assert(group_from_id(REF_GROUP_ID(ref)) == g);
return ref;
}
static size_t rehash_map_group(const void *ptr, void *priv)
{
const struct map_group *g = ptr;
return int_hash(MG_START(g));
}
/* find an empty or bucket slot within c_aux[], determined by that entry
* granting no rights. return -1 if chain length was exceeded.
*
* if @shares != NULL, fill in *@n_shares_p and @shares[0..*@n_shares_p-1]
* with slot offsets referencing the same parent index through the entire hash
* chain for @parent_ix.
*/
static int probe_add(
const uint16_t *c_aux, size_t sz, int parent_ix,
int *shares, int *n_shares_p)
{
assert(shares == NULL || *n_shares_p == 0);
size_t mask = sz - 1, hash = int_hash(parent_ix);
int pos = hash & mask, first = -1;
for(int end = pos + min_t(int, sz, MAX_PROBE_DEPTH); pos < end; pos++) {
int slot = pos & mask, aux = c_aux[slot];
if(AUX_RIGHTS(aux) == 0) {
/* tombstone, bucket, or empty. */
if(AUX_UP(aux) == 6) {
/* bucket. */
if(AUX_INDEX(aux) == parent_ix) {
/* ours! */
assert(shares == NULL || *n_shares_p == 0);
return slot;
}
} else {
assert(aux == AUX_TOMBSTONE || AUX_IS_AVAIL(aux));
if(first < 0) first = slot; /* free slot of some kind. */
if(AUX_IS_AVAIL(aux)) return first; /* end of chain. */
}
} else if(shares != NULL && AUX_INDEX(aux) == parent_ix) {
assert(*n_shares_p < MAX_SHARE_CHAIN);
shares[(*n_shares_p)++] = slot;
}
}
TRACE("%s: max depth hit w/ sz=%u, hash=%#x, first=%d\n",
__func__, (unsigned)sz, (unsigned)hash, first);
assert(first < 0 || AUX_RIGHTS(c_aux[first]) == 0);
return first;
}
/* rehash existing children into a larger table. returns false on ENOMEM, true
* otherwise.
*/
static bool grow_children_array(struct map_group *g)
{
unsigned new_scale, old_size;
if(MG_N_ALLOC_LOG2(g) == 0) {
/* allocate a cache line and a half iff @g has any maps. */
new_scale = MG_N_MAPS(g) > PAGES_PER_GROUP / 33 ? 4 : 1;
old_size = 0;
} else {
new_scale = MG_N_ALLOC_LOG2(g) + 1;
old_size = 1u << MG_N_ALLOC_LOG2(g);
}
unsigned new_size = 1u << new_scale;
assert(new_size > old_size);
uint32_t *new_children = calloc(new_size,
sizeof(uint32_t) + sizeof(uint16_t));
if(new_children == NULL) return false;
uint16_t *new_c_aux = (uint16_t *)&new_children[new_size];
for(int i=0; i < old_size; i++) {
if(!IS_REF(g->children[i]) && !IS_BUCKET(g->children[i])) continue;
int slot = probe_add(new_c_aux, new_size,
AUX_INDEX(g->c_aux[i]), NULL, NULL);
/* NOTE: if the hashing gets randomized at some point, and the random
* seed is recomputed for every table, it might be that insert will
* fail with an overlong hash chain. for now this'll never happen
* because the hash values used for both tables are the same, so
* chains will at most become shorter.
*/
BUG_ON(slot < 0, "hash chain overflow in grow_children_array()");
assert(!IS_BUCKET(new_children[slot]));
new_c_aux[slot] = g->c_aux[i];
new_children[slot] = g->children[i];
}
free(g->children);
g->children = new_children;
g->c_aux = new_c_aux;
g->addr = (g->addr & ~(0x1f << 10)) | (new_scale << 10);
assert(MG_N_ALLOC_LOG2(g) == new_scale);
return true;
}
static struct child_bucket *make_bucket(
struct map_group *g, int *shares, int n_shares,
uint32_t childref, int aux)
{
struct child_bucket *b = ra_alloc(child_bucket_ra, -1);
BUG_ON(b == NULL, "out of child buckets!"); /* TODO: share RA space */
b->alloc_log2 = size_to_shift(n_shares + 1);
int sz = 1 << b->alloc_log2;
assert(sz >= n_shares + 1);
b->cs = calloc(sz, sizeof(uint32_t) + sizeof(uint16_t));
BUG_ON(b->cs == NULL, "out of memory!");
b->aux = (uint16_t *)&b->cs[sz];
for(int i=0; i < n_shares; i++) {
int slot = shares[i];
b->cs[i] = g->children[slot];
b->aux[i] = g->c_aux[slot];
g->children[slot] = REF_TOMBSTONE;
g->c_aux[slot] = AUX_TOMBSTONE;
}
b->cs[n_shares] = childref;
b->aux[n_shares] = aux;
b->max = n_shares;
assert(b->max > 0);
return b;
}
static bool add_to_bucket(struct child_bucket *b, uint32_t cref, int aux)
{
int sz = 1 << b->alloc_log2;
if(unlikely(b->max + 1 == sz)) {
uint32_t *cs = realloc(b->cs,
sz * 2 * (sizeof(uint32_t) + sizeof(uint16_t)));
if(cs == NULL) return false;
uint16_t *old_aux = (uint16_t *)&cs[sz],
*new_aux = (uint16_t *)&cs[sz * 2];
memcpy(new_aux, old_aux, sizeof(uint16_t) * sz);
memset(&new_aux[sz], 0, sizeof(uint16_t) * sz);
memset(&cs[sz], 0, sizeof(uint32_t) * sz);
b->cs = cs;
b->aux = new_aux;
b->alloc_log2++;
}
int ix = ++b->max;
b->cs[ix] = cref;
b->aux[ix] = aux;
return true;
}
static bool add_child(
struct map_group *g, uint32_t childref,
int parent_ix, int rights)
{
assert((rights & L4_FullyAccessible) == rights);
assert(rights > 0);
assert(parent_ix >= 0 && parent_ix < PAGES_PER_GROUP);
assert(IS_REF(childref));
if(unlikely(g->children == NULL) && !grow_children_array(g)) {
return false;
}
int slot, aux = AUX(parent_ix, rights, 0);
do {
size_t sz = 1u << MG_N_ALLOC_LOG2(g);
int shares[MAX_SHARE_CHAIN], n_shares = 0;
slot = probe_add(g->c_aux, sz, parent_ix, shares, &n_shares);
if(n_shares == MAX_SHARE_CHAIN) {
struct child_bucket *b = make_bucket(g, shares, n_shares,
childref, aux);
slot = shares[0];
g->children[slot] = ra_ptr2id(child_bucket_ra, b) << 3 | REF_BUCKET;
g->c_aux[slot] = AUX_BUCKETPTR(parent_ix);
} else if(slot >= 0 && IS_BUCKET(g->children[slot])) {
struct child_bucket *b = ra_id2ptr(child_bucket_ra,
g->children[slot] >> 3);
if(!add_to_bucket(b, childref, aux)) return false;
} else if(slot >= 0) {
g->children[slot] = childref;
g->c_aux[slot] = AUX(parent_ix, rights, 0);
assert(probe_add(g->c_aux, sz, parent_ix, NULL, NULL) != slot);
} else {
if(!grow_children_array(g)) return false;
}
} while(slot < 0);
return true;
}
/* moves children of @g:@ix to be children of @pg, updating their parent
* pointers. if @pg is not NULL, children will be assigned to the map
* indicated by @g->parent[@ix]; if @pg is NULL, children will be moved to
* root.
*
* returns 0 on success, or -ENOMEM on failure, leaving a partially-completed
* state behind. calling the function again with the exact same parameters
* will resume the operation and complete into the same final state.
*/
static int reparent_children(
struct map_group *g, int ix, struct map_group *pg)
{
assert((pg == NULL) == IS_ROOT(g->parent[ix]));
if(MG_N_ALLOC_LOG2(g) == 0) return 0;
TRACE("%s: g=%p, ix=%#x, pg=%p\n", __func__, g, ix, pg);
/* loop over the children of @g:@ix. */
int parent_ix = REF_INDEX(g->parent[ix]);
struct ref_iter it;
for_each_child(&it, g, ix, c, aux) {
int rights_up = (aux >> 10) & 0x3f;
ref_del(&it, g);
struct map_group *cg = group_from_id(REF_GROUP_ID(c));
if(pg == NULL) {
cg->parent[REF_INDEX(c)] = REF_ROOT(rights_up);
TRACE("%s: made cg=%p,ix=%#x a root map\n", __func__,
cg, REF_INDEX(c));
} else {
TRACE("%s: adding child c=%#x to pg=%p,ix=%#x\n", __func__,
c, pg, parent_ix);
if(!add_child(pg, c, parent_ix, rights_up & 0x7)) return -ENOMEM;
cg->parent[REF_INDEX(c)] = REF(parent_ix,
ra_ptr2id(map_group_ra, pg));
}
}
TRACE("%s: done!\n", __func__);
return 0;
}
/* returns negative errno on failure. */
int mapdb_put(
struct space *sp, L4_Fpage_t fpage, uint32_t first_page_id,
bool is_exempt)
{
assert(L4_Size(fpage) <= GROUP_SIZE);
TRACE("%s: sp=%p, fpage=%#lx:%#lx, access=%c%c%c, fpi=%u, is_exempt=%s\n",
__func__, sp, L4_Address(fpage), L4_Size(fpage),
CHECK_FLAG(L4_Rights(fpage), L4_Readable) ? 'r' : '-',
CHECK_FLAG(L4_Rights(fpage), L4_Writable) ? 'w' : '-',
CHECK_FLAG(L4_Rights(fpage), L4_eXecutable) ? 'x' : '-',
first_page_id, btos(is_exempt));
/* mapdb_put() will only create unparented maps in sigma0_space, in any
* space while sigma0_space hasn't yet been defined, in @sp's UTCB area,
* and in @sp's KIP area. other causes of such entries are nonrecursive
* flushes: grants and mappings on top of.
*/
assert(fpage_overlap(fpage, sp->utcb_area)
|| fpage_overlap(fpage, sp->kip_area)
|| sigma0_space == NULL || sp == sigma0_space);
bool g_is_new = false;
struct map_group *g = find_group(sp, L4_Address(fpage));
if(g == NULL) {
g = add_group(sp, L4_Address(fpage));
if(g == NULL) return -ENOMEM;
g_is_new = true;
}
assert(g_is_new || check_map_group(g));
pt_iter_t it; pt_iter_init_group(&it, g);
int first_ix = (L4_Address(fpage) - MG_START(g)) / PAGE_SIZE;
uint32_t parent = REF_ROOT(L4_Rights(fpage))
| (is_exempt ? REF_EXEMPT_BIT : 0);
for(int i=0; i < L4_Size(fpage) / PAGE_SIZE; i++) {
if(g->parent[first_ix + i] == 0) {
assert(MG_N_MAPS(g) < PAGES_PER_GROUP);
if(!g_is_new) g->addr++; else g_is_new = false;
}
L4_Word_t addr = L4_Address(fpage) + i * PAGE_SIZE;
pt_set_page(&it, addr, first_page_id + i, L4_Rights(fpage));
g->parent[first_ix + i] = parent;
}
assert(check_map_group(g));
return 0;
}
/* TODO: should catch and return -ENOMEM from add_child() etc. */
int mapdb_map(
struct space *from_space, L4_Fpage_t map_page,
struct space *to_space, L4_Word_t dest_addr)
{
assert((dest_addr & (L4_Size(map_page) - 1)) == 0);
int given = 0;
/* well this is a bit vile: the map_page > group_size case. recursion
* recurs, baby.
*/
int n_groups = L4_Size(map_page) / GROUP_SIZE;
if(unlikely(n_groups > 1)) {
for(int i=0; i < n_groups; i++) {
L4_Fpage_t fp = L4_Fpage(
L4_Address(map_page) + i * GROUP_SIZE,
GROUP_SIZE);
L4_Set_Rights(&fp, L4_Rights(map_page));
int n = mapdb_map(from_space, fp, to_space,
dest_addr + i * GROUP_SIZE);
if(unlikely(n < 0)) return n;
given |= n;
}
return given;
}
TRACE("%s: from_space=%p, map_page=%#lx:%#lx, to_space=%p, dest_addr=%#lx\n",
__func__, from_space, L4_Address(map_page), L4_Size(map_page),
to_space, dest_addr);
/* smaller cases affecting a single group in @from_space. */
L4_Word_t addr = L4_Address(map_page);
int n_pages = L4_Size(map_page) / PAGE_SIZE;
struct map_group *g_src = find_group(from_space, addr), *g_dst = NULL;
pt_iter_t it_dst;
uintptr_t g_dst_end = 0;
int g_dst_id = 0, g_src_id = ra_ptr2id(map_group_ra, g_src);
for(int i=0; i < n_pages; i++, addr += PAGE_SIZE, dest_addr += PAGE_SIZE) {
int from_ix = (addr - MG_START(g_src)) / PAGE_SIZE;
if(unlikely(IS_EXEMPT(g_src->parent[from_ix]))) continue;
bool dst_is_new = false;
if(g_dst_end <= dest_addr) {
g_dst = find_group(to_space, dest_addr);
if(g_dst == NULL) {
g_dst = add_group(to_space, dest_addr);
if(g_dst == NULL) {
/* TODO: undo maps up to this point & return -ENOMEM */
panic("mapdb_map() doesn't handle failing add_group()");
}
dst_is_new = true;
}
assert(dst_is_new || check_map_group(g_dst));
g_dst_end = MG_START(g_dst) + GROUP_SIZE;
g_dst_id = ra_ptr2id(map_group_ra, g_dst);
pt_iter_init_group(&it_dst, g_dst);
}
uint32_t s_pgid = 0, d_pgid = 0;
TRACE("%s: mapping g_src=%p,addr=%#lx to g_dst=%p,dest_addr=%#lx\n",
__func__, g_src, addr, g_dst, dest_addr);
int to_ix = (dest_addr - MG_START(g_dst)) / PAGE_SIZE;
if(unlikely(IS_EXEMPT(g_dst->parent[to_ix]))) continue;
int s_rights = mapdb_query(&s_pgid, from_space, g_src, addr),
d_rights = mapdb_query(&d_pgid, to_space, g_dst, dest_addr),
map_rights = s_rights & L4_Rights(map_page);
if(map_rights == 0 && s_rights != 0) {
/* no-op: map_page identified no access that should be granted,
* but we're not trying to map from a hole in @from_space.
*/
continue;
}
uint32_t d_parent = g_dst->parent[to_ix];
if(d_rights > 0 && (s_rights == 0 || s_pgid != d_pgid
|| IS_ROOT(d_parent)
|| REF_INDEX(d_parent) != from_ix
|| REF_GROUP_ID(d_parent) != g_src_id))
{
/* remove dest map ahead of overwrite or clear. */
TRACE("%s: tossing dest at to_ix=%d\n", __func__, to_ix);
int n = reparent_children(g_dst, to_ix,
IS_REF(d_parent) ? group_from_id(REF_GROUP_ID(d_parent)) : NULL);
if(n < 0) {
/* FIXME: handle it */
panic("can't handle reparent_children() failure");
}
if(unlikely(IS_ROOT(d_parent))) {
/* check that children exist for this mapping. */
bool found = false;
struct ref_iter dp_it;
for_each_child(&dp_it, g_dst, to_ix, c, aux) {
found = true;
break;
}
if(!found) {
/* warn a brotha. */
printf("WARNING: orphaning root pgid=%u (phys=%#x) in overmap!\n",
d_pgid, (uintptr_t)d_pgid << PAGE_BITS);
panic("strict mode -- let's not proceed");
} else {
/* this is so rare that it's usually an error. */
printf("NOTE: root pgid=%u was mapped over\n", d_pgid);
}
} else {
assert(IS_REF(d_parent));
struct ref_iter opg_it;
int dummy;
struct map_group *opg = get_child(&opg_it, &dummy,
d_parent, REF(to_ix, g_dst_id));
ref_del(&opg_it, opg);
}
d_parent = 0;
d_rights = 0;
}
bool kill_dst = false;
if(s_rights == 0) {
/* clear. */
assert(d_parent == 0);
pt_clear_page(&it_dst, dest_addr);
if(dst_is_new) {
/* mapping a blank into a fresh empty group. */
kill_dst = true;
} else if(MG_N_MAPS(g_dst) == 1) {
/* mapping a blank over the last page in g_dst. */
kill_dst = true;
} else {
g_dst->addr--;
assert(MG_N_MAPS(g_dst) < PAGES_PER_GROUP);
}
} else if(s_pgid == d_pgid && (s_rights & ~d_rights) > 0
&& REF_INDEX(d_parent) == from_ix)
{
/* expand. */
TRACE("%s: expanding rights from %#x to %#x\n", __func__,
d_rights, d_rights | map_rights);
struct ref_iter p_it;
/* (get w/ the program, compiler: accessing a potentially
* undefined variable implies that the part where it was assigned
* must have been executed, as it is here, complete w/ an assert
* for the return value.)
*/
int aux = 0;
struct map_group *foo = get_child(&p_it, &aux, d_parent,
REF(to_ix, g_dst_id));
assert(foo == g_src);
ref_set_aux(&p_it, g_src, aux | (map_rights & 0x7) << 10);
pt_set_rights(&it_dst, dest_addr, d_rights | map_rights);
} else if(d_rights == 0) {
/* set. */
TRACE("%s: insert/replace case at sp=%p dest_addr=%#lx\n",
__func__, to_space, dest_addr);
assert(s_pgid > 0); /* good as true */
bool ok = add_child(g_src, REF(to_ix, g_dst_id),
from_ix, map_rights);
if(!ok) {
/* FIXME */
panic("can't handle OOM from add_child()");
}
pt_set_page(&it_dst, dest_addr, s_pgid, map_rights);
d_parent = REF(from_ix, g_src_id);
if(g_dst->parent[to_ix] == 0) {
assert(MG_N_MAPS(g_dst) < PAGES_PER_GROUP);
if(!dst_is_new) g_dst->addr++;
}
} else {
/* the goggles, they do nothing */
TRACE("%s: no-op!\n", __func__);