-
Notifications
You must be signed in to change notification settings - Fork 1
/
alloc.c
706 lines (594 loc) · 18.3 KB
/
alloc.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
/* This file is concerned with allocating and freeing arbitrary-size blocks of
* physical memory.
*/
#define _SYSTEM 1
#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 <sys/mman.h>
#include <limits.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <memory.h>
#include <time.h>
#include <stdlib.h>
#include "../pm/memheader.c"
#include "vm.h"
#include "proto.h"
#include "util.h"
#include "glo.h"
#include "sanitycheck.h"
#include "memlist.h"
/* Number of physical pages in a 32-bit address space */
#define NUMBER_PHYSICAL_PAGES (int)(0x100000000ULL/VM_PAGE_SIZE)
#define PAGE_BITMAP_CHUNKS BITMAP_CHUNKS(NUMBER_PHYSICAL_PAGES)
static bitchunk_t free_pages_bitmap[PAGE_BITMAP_CHUNKS];
#define PAGE_CACHE_MAX 10000
static int free_page_cache[PAGE_CACHE_MAX];
static int free_page_cache_size = 0;
/* Used for sanity check. */
static phys_bytes mem_low, mem_high;
static void free_pages(phys_bytes addr, int pages);
static phys_bytes alloc_pages(int pages, int flags);
#if SANITYCHECKS
struct {
int used;
const char *file;
int line;
} pagemap[NUMBER_PHYSICAL_PAGES];
#endif
#define page_isfree(i) GET_BIT(free_pages_bitmap, i)
#define RESERVEDMAGIC 0x6e4c74d5
#define MAXRESERVEDPAGES 300
#define MAXRESERVEDQUEUES 15
static struct reserved_pages {
struct reserved_pages *next; /* next in use */
int max_available; /* queue depth use, 0 if not in use at all */
int npages; /* number of consecutive pages */
int mappedin; /* must reserved pages also be mapped? */
int n_available; /* number of queue entries */
int allocflags; /* allocflags for alloc_mem */
struct reserved_pageslot {
phys_bytes phys;
void *vir;
} slots[MAXRESERVEDPAGES];
u32_t magic;
} reservedqueues[MAXRESERVEDQUEUES], *first_reserved_inuse = NULL;
int missing_spares = 0;
static void sanitycheck_queues(void)
{
struct reserved_pages *mrq;
int m = 0;
for(mrq = first_reserved_inuse; mrq; mrq = mrq->next) {
assert(mrq->max_available > 0);
assert(mrq->max_available >= mrq->n_available);
m += mrq->max_available - mrq->n_available;
}
assert(m == missing_spares);
}
static void sanitycheck_rq(struct reserved_pages *rq)
{
assert(rq->magic == RESERVEDMAGIC);
assert(rq->n_available >= 0);
assert(rq->n_available <= MAXRESERVEDPAGES);
assert(rq->n_available <= rq->max_available);
sanitycheck_queues();
}
void *reservedqueue_new(int max_available, int npages, int mapped, int allocflags)
{
int r;
struct reserved_pages *rq;
assert(max_available > 0);
assert(max_available < MAXRESERVEDPAGES);
assert(npages > 0);
assert(npages < 10);
for(r = 0; r < MAXRESERVEDQUEUES; r++)
if(!reservedqueues[r].max_available)
break;
if(r >= MAXRESERVEDQUEUES) {
printf("VM: %d reserved queues in use\n", MAXRESERVEDQUEUES);
return NULL;
}
rq = &reservedqueues[r];
memset(rq, 0, sizeof(*rq));
rq->next = first_reserved_inuse;
first_reserved_inuse = rq;
rq->max_available = max_available;
rq->npages = npages;
rq->mappedin = mapped;
rq->allocflags = allocflags;
rq->magic = RESERVEDMAGIC;
missing_spares += max_available;
return rq;
}
static void
reservedqueue_fillslot(struct reserved_pages *rq,
struct reserved_pageslot *rps, phys_bytes ph, void *vir)
{
rps->phys = ph;
rps->vir = vir;
assert(missing_spares > 0);
if(rq->mappedin) assert(vir);
missing_spares--;
rq->n_available++;
}
static int
reservedqueue_addslot(struct reserved_pages *rq)
{
phys_bytes cl, cl_addr;
void *vir;
struct reserved_pageslot *rps;
sanitycheck_rq(rq);
if((cl = alloc_mem(rq->npages, rq->allocflags)) == NO_MEM)
return ENOMEM;
cl_addr = CLICK2ABS(cl);
vir = NULL;
if(rq->mappedin) {
if(!(vir = vm_mappages(cl_addr, rq->npages))) {
free_mem(cl, rq->npages);
printf("reservedqueue_addslot: vm_mappages failed\n");
return ENOMEM;
}
}
rps = &rq->slots[rq->n_available];
reservedqueue_fillslot(rq, rps, cl_addr, vir);
return OK;
}
void reservedqueue_add(void *rq_v, void *vir, phys_bytes ph)
{
struct reserved_pages *rq = rq_v;
struct reserved_pageslot *rps;
sanitycheck_rq(rq);
rps = &rq->slots[rq->n_available];
reservedqueue_fillslot(rq, rps, ph, vir);
}
static int reservedqueue_fill(void *rq_v)
{
struct reserved_pages *rq = rq_v;
int r;
sanitycheck_rq(rq);
while(rq->n_available < rq->max_available)
if((r=reservedqueue_addslot(rq)) != OK)
return r;
return OK;
}
int
reservedqueue_alloc(void *rq_v, phys_bytes *ph, void **vir)
{
struct reserved_pages *rq = rq_v;
struct reserved_pageslot *rps;
sanitycheck_rq(rq);
if(rq->n_available < 1) return ENOMEM;
rq->n_available--;
missing_spares++;
rps = &rq->slots[rq->n_available];
*ph = rps->phys;
*vir = rps->vir;
sanitycheck_rq(rq);
return OK;
}
void alloc_cycle(void)
{
struct reserved_pages *rq;
sanitycheck_queues();
for(rq = first_reserved_inuse; rq && missing_spares > 0; rq = rq->next) {
sanitycheck_rq(rq);
reservedqueue_fill(rq);
sanitycheck_rq(rq);
}
sanitycheck_queues();
}
/*===========================================================================*
* alloc_mem *
*===========================================================================*/
phys_clicks alloc_mem(phys_clicks clicks, u32_t memflags)
{
/* Allocate a block of memory from the free list using first fit. The block
* consists of a sequence of contiguous bytes, whose length in clicks is
* given by 'clicks'. A pointer to the block is returned. The block is
* always on a click boundary. This procedure is called when memory is
* needed for FORK or EXEC.
*/
phys_clicks mem = NO_MEM, align_clicks = 0;
if(memflags & PAF_ALIGN64K) {//First fit algorithm here?
align_clicks = (64 * 1024) / CLICK_SIZE;
clicks += align_clicks;
} else if(memflags & PAF_ALIGN16K) {
align_clicks = (16 * 1024) / CLICK_SIZE;
clicks += align_clicks;
}
do {//First fit algorithm here?
mem = alloc_pages(clicks, memflags);
} while(mem == NO_MEM && cache_freepages(clicks) > 0);
if(mem == NO_MEM)//Error state
return mem;
if(align_clicks) {//shifts memory block to be on a click boundry?
phys_clicks o;
o = mem % align_clicks;
if(o > 0) {
phys_clicks e;
e = align_clicks - o;
free_mem(mem, e);
mem += e;
}
}
return mem;
}
void mem_add_total_pages(int pages)
{
total_pages += pages;
}
/*===========================================================================*
* free_mem *
*===========================================================================*/
void free_mem(phys_clicks base, phys_clicks clicks)
{
/* Return a block of free memory to the hole list. The parameters tell where
* the block starts in physical memory and how big it is. The block is added
* to the hole list. If it is contiguous with an existing hole on either end,
* it is merged with the hole or holes.
*/
if (clicks == 0) return;
assert(CLICK_SIZE == VM_PAGE_SIZE);
free_pages(base, clicks);
return;
}
/*===========================================================================*
* mem_init *
*===========================================================================*/
void mem_init(struct memory *chunks)
{
/* Initialize hole lists. There are two lists: 'hole_head' points to a linked
* list of all the holes (unused memory) in the system; 'free_slots' points to
* a linked list of table entries that are not in use. Initially, the former
* list has one entry for each chunk of physical memory, and the second
* list links together the remaining table slots. As memory becomes more
* fragmented in the course of time (i.e., the initial big holes break up into
* smaller holes), new table slots are needed to represent them. These slots
* are taken from the list headed by 'free_slots'.
*/
int i, first = 0;
total_pages = 0;
memset(free_pages_bitmap, 0, sizeof(free_pages_bitmap));
/* Use the chunks of physical memory to allocate holes. */
for (i=NR_MEMS-1; i>=0; i--) {
if (chunks[i].size > 0) {
phys_bytes from = CLICK2ABS(chunks[i].base),
to = CLICK2ABS(chunks[i].base+chunks[i].size)-1;
if(first || from < mem_low) mem_low = from;
if(first || to > mem_high) mem_high = to;
free_mem(chunks[i].base, chunks[i].size);
total_pages += chunks[i].size;
first = 0;
}
}
}
#if SANITYCHECKS
void mem_sanitycheck(const char *file, int line)
{
int i;
for(i = 0; i < NUMBER_PHYSICAL_PAGES; i++) {
if(!page_isfree(i)) continue;
MYASSERT(usedpages_add(i * VM_PAGE_SIZE, VM_PAGE_SIZE) == OK);
}
}
#endif
void memstats(int *nodes, int *pages, int *largest)
{
int i;
*nodes = 0;
*pages = 0;
*largest = 0;
for(i = 0; i < NUMBER_PHYSICAL_PAGES; i++) {
int size = 0;
while(i < NUMBER_PHYSICAL_PAGES && page_isfree(i)) {
size++;
i++;
}
if(size == 0) continue;
(*nodes)++;
(*pages)+= size;
if(size > *largest)
*largest = size;
}
}
static int findbit(int low, int startscan, int pages, int memflags, int *len)//First Fit algorithm is here
{
int run_length = 0, i;
int freerange_start = startscan;
for(i = startscan; i >= low; i--) {//start at startscan and scan backwards until you hit low
if(!page_isfree(i)) {//if the page at that address is free
int pi;
int chunk = i/BITCHUNK_BITS, moved = 0;
run_length = 0;
pi = i;
while(chunk > 0 &&
!MAP_CHUNK(free_pages_bitmap, chunk*BITCHUNK_BITS)) {
chunk--;
moved = 1;
}
if(moved) { i = chunk * BITCHUNK_BITS + BITCHUNK_BITS; }
continue;
}
if(!run_length) { freerange_start = i; run_length = 1; }
else { freerange_start--; run_length++; }
assert(run_length <= pages);
if(run_length == pages) {
/* good block found! */
*len = run_length;
return freerange_start;
}
}
return NO_MEM;
}
static int findbitb(int low, int startscan, int pages, int memflags, int *len)
{
int run_length = 0, i;
int freerange_start = startscan;
int best = INT_MAX;//Maximum value of an int
int bestaddress;
for(i = startscan; i > low; i--) {
if(!page_isfree(i)) {
int pi;
int chunk = i/BITCHUNK_BITS, moved = 0;
run_length = 0;
pi = i;
while(chunk > 0 &&
!MAP_CHUNK(free_pages_bitmap, chunk*BITCHUNK_BITS)) {
chunk--;
moved = 1;
}
if(moved) { i = chunk * BITCHUNK_BITS + BITCHUNK_BITS; }
continue;
}
if((!page_isfree(i)) && (page_isfree(i-1)) && (run_length >= pages) && (run_length < best)) {//if the block is big enough but smaller than the current best, only activates at the end fo the block
best = run_length;//set the block as the new best
bestaddress = freerange_start;//record the start
}
if(!run_length) { freerange_start = i; run_length = 1; }
else { freerange_start--; run_length++; }//this works like in first fit
}
//assert(best <= pages);
if(best >= pages && best < INT_MAX) { //return the address of the start of the block if they found anything.
*len = pages;
return bestaddress;
}
return NO_MEM;
}
static int findbitw(int low, int startscan, int pages, int memflags, int *len)
{
int run_length = 0, i;
int freerange_start = startscan;
int worst = 0;
int worstaddress;
for(i = startscan; i > low; i--) {
if(!page_isfree(i)) {
int pi;
int chunk = i/BITCHUNK_BITS, moved = 0;
run_length = 0;
pi = i;
while(chunk > 0 &&
!MAP_CHUNK(free_pages_bitmap, chunk*BITCHUNK_BITS)) {
chunk--;
moved = 1;
}
if(moved) { i = chunk * BITCHUNK_BITS + BITCHUNK_BITS; }
continue;
}
if((!page_isfree(i)) && (page_isfree(i-1)) && (run_length >= pages) && (run_length > worst)) {
worst = run_length;
worstaddress = freerange_start;
}
if(!run_length) { freerange_start = i; run_length = 1; }
else { freerange_start--; run_length++; }
}
//assert(best <= pages);
if(worst >= pages) {
*len = pages;
return worstaddress;
}
return NO_MEM;
}
static int findbitr(int low, int startscan, int pages, int memflags, int *len)
{
int run_length = 0, i;
int freerange_start = startscan;
int addresslist[500];
int addressnumber = 0;
int rando;
for(i = startscan; i > low; i--) {
if(!page_isfree(i)) {
int pi;
int chunk = i/BITCHUNK_BITS, moved = 0;
run_length = 0;
pi = i;
while(chunk > 0 &&
!MAP_CHUNK(free_pages_bitmap, chunk*BITCHUNK_BITS)) {
chunk--;
moved = 1;
}
if(moved) { i = chunk * BITCHUNK_BITS + BITCHUNK_BITS; }
continue;
}
if((!page_isfree(i)) && (page_isfree(i-1)) && (run_length >= pages)) {
addresslist[addressnumber] = freerange_start;
addressnumber++;
}
if(!run_length) { freerange_start = i; run_length = 1; }
else { freerange_start--; run_length++; }
}
//assert(best <= pages);
if(addressnumber >= 1) {
*len = pages;
int bestaddress = addresslist[rando % addressnumber];
return bestaddress;
}
return NO_MEM;
}
/*===========================================================================*
* alloc_pages *
*===========================================================================*/
static phys_bytes alloc_pages(int pages, int memflags)
{
phys_bytes boundary16 = 16 * 1024 * 1024 / VM_PAGE_SIZE;
phys_bytes boundary1 = 1 * 1024 * 1024 / VM_PAGE_SIZE;
phys_bytes mem = NO_MEM, i; /* page number */
int maxpage = NUMBER_PHYSICAL_PAGES - 1;
static int lastscan = -1;
int startscan, run_length;
if(memflags & PAF_LOWER16MB)
maxpage = boundary16 - 1;
else if(memflags & PAF_LOWER1MB)
maxpage = boundary1 - 1;
else {
/* no position restrictions: check page cache */
if(pages == 1) {
while(free_page_cache_size > 0) {
i = free_page_cache[free_page_cache_size-1];
if(page_isfree(i)) {
free_page_cache_size--;
mem = i;
assert(mem != NO_MEM);
run_length = 1;
break;
}
free_page_cache_size--;
}
}
}
if(lastscan < maxpage && lastscan >= 0)//First fit starts here?
startscan = lastscan;
else startscan = maxpage;
if(CUSTOM_MEM_POLICY == FIRST_FIT){
if(mem == NO_MEM)
mem = findbit(0, startscan, pages, memflags, &run_length);
if(mem == NO_MEM)
mem = findbit(0, maxpage, pages, memflags, &run_length);
if(mem == NO_MEM)
return NO_MEM;
}
else if(CUSTOM_MEM_POLICY == BEST_FIT){
if(mem == NO_MEM)
mem = findbitb(0, startscan, pages, memflags, &run_length);
if(mem == NO_MEM)
mem = findbitb(0, maxpage, pages, memflags, &run_length);
if(mem == NO_MEM)
return NO_MEM;
}
else if(CUSTOM_MEM_POLICY == WORST_FIT){
if(mem == NO_MEM)
mem = findbitw(0, startscan, pages, memflags, &run_length);
if(mem == NO_MEM)
mem = findbitw(0, maxpage, pages, memflags, &run_length);
if(mem == NO_MEM)
return NO_MEM;
}
else if(CUSTOM_MEM_POLICY == RANDOM_FIT){
if(mem == NO_MEM)
mem = findbitr(0, startscan, pages, memflags, &run_length);
if(mem == NO_MEM)
mem = findbitr(0, maxpage, pages, memflags, &run_length);
if(mem == NO_MEM)
return NO_MEM;
}
else{
if(mem == NO_MEM)
mem = findbit(0, lastscan, pages, memflags, &run_length);
if(mem == NO_MEM)
mem = findbit(0, maxpage, pages, memflags, &run_length);
if(mem == NO_MEM)
return NO_MEM;
}
/* remember for next time */
lastscan = mem;
for(i = mem; i < mem + pages; i++) {
UNSET_BIT(free_pages_bitmap, i);
}
if(memflags & PAF_CLEAR) {
int s;
if ((s= sys_memset(NONE, 0, CLICK_SIZE*mem,
VM_PAGE_SIZE*pages)) != OK)
panic("alloc_mem: sys_memset failed: %d", s);
}
return mem;
}
/*===========================================================================*
* free_pages *
*===========================================================================*/
static void free_pages(phys_bytes pageno, int npages)
{
int i, lim = pageno + npages - 1;
#if JUNKFREE
if(sys_memset(NONE, 0xa5a5a5a5, VM_PAGE_SIZE * pageno,
VM_PAGE_SIZE * npages) != OK)
panic("free_pages: sys_memset failed");
#endif
for(i = pageno; i <= lim; i++) {
SET_BIT(free_pages_bitmap, i);
if(free_page_cache_size < PAGE_CACHE_MAX) {
free_page_cache[free_page_cache_size++] = i;
}
}
}
/*===========================================================================*
* printmemstats *
*===========================================================================*/
void printmemstats(void)
{
int nodes, pages, largest;
memstats(&nodes, &pages, &largest);
printf("%d blocks, %d pages (%lukB) free, largest %d pages (%lukB)\n",
nodes, pages, (unsigned long) pages * (VM_PAGE_SIZE/1024),
largest, (unsigned long) largest * (VM_PAGE_SIZE/1024));
}
#if SANITYCHECKS
/*===========================================================================*
* usedpages_reset *
*===========================================================================*/
void usedpages_reset(void)
{
memset(pagemap, 0, sizeof(pagemap));
}
/*===========================================================================*
* usedpages_add *
*===========================================================================*/
int usedpages_add_f(phys_bytes addr, phys_bytes len, const char *file, int line)
{
u32_t pagestart, pages;
if(!incheck)
return OK;
assert(!(addr % VM_PAGE_SIZE));
assert(!(len % VM_PAGE_SIZE));
assert(len > 0);
pagestart = addr / VM_PAGE_SIZE;
pages = len / VM_PAGE_SIZE;
while(pages > 0) {
phys_bytes thisaddr;
assert(pagestart > 0);
assert(pagestart < NUMBER_PHYSICAL_PAGES);
thisaddr = pagestart * VM_PAGE_SIZE;
assert(pagestart < NUMBER_PHYSICAL_PAGES);
if(pagemap[pagestart].used) {
static int warnings = 0;
if(warnings++ < 100)
printf("%s:%d: usedpages_add: addr 0x%lx reused, first %s:%d\n",
file, line, thisaddr, pagemap[pagestart].file, pagemap[pagestart].line);
util_stacktrace();
return EFAULT;
}
pagemap[pagestart].used = 1;
pagemap[pagestart].file = file;
pagemap[pagestart].line = line;
pages--;
pagestart++;
}
return OK;
}
#endif