2 * Size memory and create the kernel page-tables on the fly while doing so.
3 * Called from main(), this code should only be run by the bootstrap processor.
5 * MemMin is what the bootstrap code in l.s has already mapped;
6 * MemMax is the limit of physical memory to scan.
9 #include "../port/lib.h"
18 u32int MemMin = 8*MB; /* set in l.s */
21 MemUPA = 0, /* unbacked physical address */
22 MemRAM = 1, /* physical memory */
23 MemUMB = 2, /* upper memory block (<16MB) */
29 MemMax = (3*1024+768)*MB,
32 typedef struct Map Map;
38 typedef struct RMap RMap;
48 * Memory allocation tracking.
50 static Map mapupa[16];
51 static RMap rmapupa = {
52 "unallocated unbacked physical memory",
54 &mapupa[nelem(mapupa)-1],
57 static Map mapram[16];
58 static RMap rmapram = {
61 &mapram[nelem(mapram)-1],
64 static Map mapumb[64];
65 static RMap rmapumb = {
68 &mapumb[nelem(mapumb)-1],
71 static Map mapumbrw[16];
72 static RMap rmapumbrw = {
75 &mapumbrw[nelem(mapumbrw)-1],
83 print("%s\n", rmap->name);
84 for(mp = rmap->map; mp->size; mp++)
85 print("\t%8.8luX %8.8luX (%lud)\n", mp->addr, mp->addr+mp->size, mp->size);
92 ulong maxpa, maxpa1, maxpa2;
94 maxpa = (nvramread(0x18)<<8)|nvramread(0x17);
95 maxpa1 = (nvramread(0x31)<<8)|nvramread(0x30);
96 maxpa2 = (nvramread(0x16)<<8)|nvramread(0x15);
97 print("maxpa = %luX -> %luX, maxpa1 = %luX maxpa2 = %luX\n",
98 maxpa, MB+maxpa*KB, maxpa1, maxpa2);
102 mapprint(&rmapumbrw);
107 mapfree(RMap* rmap, ulong addr, ulong size)
116 for(mp = rmap->map; mp->addr <= addr && mp->size; mp++)
119 if(mp > rmap->map && (mp-1)->addr+(mp-1)->size == addr){
120 (mp-1)->size += size;
121 if(addr+size == mp->addr){
122 (mp-1)->size += mp->size;
125 (mp-1)->addr = mp->addr;
126 (mp-1)->size = mp->size;
131 if(addr+size == mp->addr && mp->size){
136 if(mp >= rmap->mapend){
137 print("mapfree: %s: losing 0x%luX, %ld\n",
138 rmap->name, addr, size);
153 mapalloc(RMap* rmap, ulong addr, int size, int align)
159 for(mp = rmap->map; mp->size; mp++){
164 * A specific address range has been given:
165 * if the current map entry is greater then
166 * the address is not in the map;
167 * if the current map entry does not overlap
168 * the beginning of the requested range then
169 * continue on to the next map entry;
170 * if the current map entry does not entirely
171 * contain the requested range then the range
176 if(mp->size < addr - maddr) /* maddr+mp->size < addr, but no overflow */
178 if(addr - maddr > mp->size - size) /* addr+size > maddr+mp->size, but no overflow */
184 maddr = ((maddr+align-1)/align)*align;
185 if(mp->addr+mp->size-maddr < size)
189 mp->addr = maddr+size;
190 mp->size -= maddr-oaddr+size;
194 (mp-1)->addr = mp->addr;
195 }while((mp-1)->size = mp->size);
200 mapfree(rmap, oaddr, maddr-oaddr);
210 * Allocate from the ram map directly to make page tables.
211 * Called by mmuwalk during e820scan.
218 m = mapalloc(&rmapram, 0, BY2PG, BY2PG);
231 if((p = getconf("umbexclude")) == nil)
234 while(p && *p != '\0' && *p != '\n'){
236 addr = strtoul(p, &rptr, 0);
237 if(rptr == nil || rptr == p || *rptr != '-'){
238 print("umbexclude: invalid argument <%s>\n", op);
243 size = strtoul(p, &rptr, 0) - addr + 1;
245 print("umbexclude: bad range <%s>\n", op);
248 if(rptr != nil && *rptr == ',')
252 mapalloc(&rmapumb, addr, size, 0);
262 * Scan the Upper Memory Blocks (0xA0000->0xF0000) for pieces
263 * which aren't used; they can be used later for devices which
264 * want to allocate some virtual address space.
265 * Check for two things:
266 * 1) device BIOS ROM. This should start with a two-byte header
267 * of 0x55 0xAA, followed by a byte giving the size of the ROM
268 * in 512-byte chunks. These ROM's must start on a 2KB boundary.
269 * 2) device memory. This is read-write.
270 * There are some assumptions: there's VGA memory at 0xA0000 and
271 * the VGA BIOS ROM is at 0xC0000. Also, if there's no ROM signature
272 * at 0xE0000 then the whole 64KB up to 0xF0000 is theoretically up
273 * for grabs; check anyway.
276 while(p < (uchar*)KADDR(0xE0000)){
278 * Test for 0x55 0xAA before poking obtrusively,
279 * some machines (e.g. Thinkpad X20) seem to map
280 * something dynamic here (cardbus?) causing weird
281 * problems if it is changed.
283 if(p[0] == 0x55 && p[1] == 0xAA){
290 if(p[0] != 0xCC || p[2*KB-1] != 0xCC){
294 if(p[0] == 0x55 && p[1] == 0xAA){
298 if(p[0] == 0xFF && p[1] == 0xFF)
299 mapfree(&rmapumb, PADDR(p), 2*KB);
302 mapfree(&rmapumbrw, PADDR(p), 2*KB);
307 if(p[0] != 0x55 || p[1] != 0xAA){
310 if(p[0] != 0xCC && p[64*KB-1] != 0xCC)
311 mapfree(&rmapumb, PADDR(p), 64*KB);
318 checksum(void *v, int n)
330 sigscan(uchar* addr, int len, char* signature)
336 sl = strlen(signature);
337 for(p = addr; p+sl < e; p += 16)
338 if(memcmp(p, signature, sl) == 0)
350 top = ((bda[0x14]<<8) | bda[0x13])*KB;
352 if(top < 64*KB || top > 640*KB)
353 top = 640*KB; /* sanity */
355 /* reserved for bios tables (EBDA) */
362 sigsearch(char* signature)
369 * Search for the data structure:
370 * 1) within the first KiB of the Extended BIOS Data Area (EBDA), or
371 * 2) within the last KiB of system base memory if the EBDA segment
373 * 3) within the BIOS ROM address space between 0xf0000 and 0xfffff
374 * (but will actually check 0xe0000 to 0xfffff).
377 if(memcmp(KADDR(0xfffd9), "EISA", 4) == 0){
378 if((p = (bda[0x0f]<<8)|bda[0x0e]) != 0){
379 if((r = sigscan(KADDR(p<<4), 1024, signature)) != nil)
383 if((r = sigscan(KADDR(convmemsize()), 1024, signature)) != nil)
386 /* hack for virtualbox: look in KiB below 0xa0000 */
387 if((r = sigscan(KADDR(0xa0000-1024), 1024, signature)) != nil)
390 return sigscan(KADDR(0xe0000), 0x20000, signature);
399 * Initialise the memory bank information for conventional memory
400 * (i.e. less than 640KB). The base is the first location after the
401 * bootstrap processor MMU information and the limit is obtained from
402 * the BIOS data area.
407 mapfree(&rmapram, x, pa-x);
408 memset(KADDR(x), 0, pa-x); /* keep us honest */
411 x = PADDR(PGROUND((uintptr)end));
414 panic("kernel too big");
415 mapfree(&rmapram, x, pa-x);
416 memset(KADDR(x), 0, pa-x); /* keep us honest */
420 ramscan(ulong maxmem)
422 ulong *k0, kzero, map, maxkpa, maxpa, pa, *pte, *table, *va, vbase, x;
423 int nvalid[NMemType];
426 * The bootstrap code has has created a prototype page
427 * table which maps the first MemMin of physical memory to KZERO.
428 * The page directory is at m->pdb and the first page of
429 * free memory is after the per-processor MMU information.
434 * Check if the extended memory size can be obtained from the CMOS.
435 * If it's 0 then it's either not known or >= 64MB. Always check
436 * at least 24MB in case there's a memory gap (up to 8MB) below 16MB;
437 * in this case the memory from the gap is remapped to the top of
439 * The value in CMOS is supposed to be the number of KB above 1MB.
442 x = (nvramread(0x18)<<8)|nvramread(0x17);
443 if(x == 0 || x >= (63*KB))
451 maxkpa = (u32int)-KZERO; /* 2^32 - KZERO */
454 * March up memory from MemMin to maxpa 1MB at a time,
455 * mapping the first page and checking the page can
456 * be written and read correctly. The page tables are created here
457 * on the fly, allocating from low memory as necessary.
459 k0 = (ulong*)KADDR(0);
463 memset(nvalid, 0, sizeof(nvalid));
466 * Can't map memory to KADDR(pa) when we're walking because
467 * can only use KADDR for relatively low addresses.
468 * Instead, map each 4MB we scan to the virtual address range
469 * MemMin->MemMin+4MB while we are scanning.
474 * Map the page. Use mapalloc(&rmapram, ...) to make
475 * the page table if necessary, it will be returned to the
476 * pool later if it isn't needed. Map in a fixed range (the second 4M)
477 * because high physical addresses cannot be passed to KADDR.
479 va = (void*)(vbase + pa%(4*MB));
480 table = &m->pdb[PDX(va)];
482 if(map == 0 && (map = mapalloc(&rmapram, 0, BY2PG, BY2PG)) == 0)
484 memset(KADDR(map), 0, BY2PG);
485 *table = map|PTEWRITE|PTEVALID;
486 memset(nvalid, 0, sizeof(nvalid));
488 table = KADDR(PPN(*table));
489 pte = &table[PTX(va)];
491 *pte = pa|PTEWRITE|PTEUNCACHED|PTEVALID;
492 mmuflushtlb(PADDR(m->pdb));
494 * Write a pattern to the page and write a different
495 * pattern to a possible mirror at KZERO. If the data
496 * reads back correctly the chunk is some type of RAM (possibly
497 * a linearly-mapped VGA framebuffer, for instance...) and
498 * can be cleared and added to the memory pool. If not, the
499 * chunk is marked uncached and added to the UMB pool if <16MB
500 * or is marked invalid and added to the UPA pool.
505 nvalid[MemRAM] += MB/BY2PG;
506 mapfree(&rmapram, pa, MB);
509 *pte++ = pa|PTEWRITE|PTEVALID;
512 mmuflushtlb(PADDR(m->pdb));
513 /* memset(va, 0, MB); so damn slow to memset all of memory */
516 nvalid[MemUMB] += MB/BY2PG;
517 mapfree(&rmapumb, pa, MB);
520 *pte++ = pa|PTEWRITE|PTEUNCACHED|PTEVALID;
525 nvalid[MemUPA] += MB/BY2PG;
526 mapfree(&rmapupa, pa, MB);
532 * Done with this 4MB chunk, review the options:
533 * 1) not physical memory and >=16MB - invalidate the PDB entry;
534 * 2) physical memory - use the 4MB page extension if possible;
535 * 3) not physical memory and <16MB - use the 4MB page extension
537 * 4) mixed or no 4MB page extension - commit the already
538 * initialised space for the page table.
540 if(pa%(4*MB) == 0 && pa >= 32*MB && nvalid[MemUPA] == (4*MB)/BY2PG){
542 * If we encounter a 4MB chunk of missing memory
543 * at a sufficiently high offset, call it the end of
544 * memory. Otherwise we run the risk of thinking
545 * that video memory is real RAM.
549 if(pa <= maxkpa && pa%(4*MB) == 0){
550 table = &m->pdb[PDX(KADDR(pa - 4*MB))];
551 if(nvalid[MemUPA] == (4*MB)/BY2PG)
553 else if(nvalid[MemRAM] == (4*MB)/BY2PG && (m->cpuiddx & Pse))
554 *table = (pa - 4*MB)|PTESIZE|PTEWRITE|PTEVALID;
555 else if(nvalid[MemUMB] == (4*MB)/BY2PG && (m->cpuiddx & Pse))
556 *table = (pa - 4*MB)|PTESIZE|PTEWRITE|PTEUNCACHED|PTEVALID;
558 *table = map|PTEWRITE|PTEVALID;
562 mmuflushtlb(PADDR(m->pdb));
566 * If we didn't reach the end of the 4MB chunk, that part won't
567 * be mapped. Commit the already initialised space for the page table.
569 if(pa % (4*MB) && pa <= maxkpa){
570 m->pdb[PDX(KADDR(pa))] = map|PTEWRITE|PTEVALID;
574 mapfree(&rmapram, map, BY2PG);
576 m->pdb[PDX(vbase)] = 0;
577 mmuflushtlb(PADDR(m->pdb));
579 mapfree(&rmapupa, pa, (u32int)-pa);
583 typedef struct Emap Emap;
590 static Emap emap[128];
594 emapcmp(const void *va, const void *vb)
604 if(a->base < b->base)
606 if(a->base > b->base)
612 map(ulong base, ulong len, int type)
615 ulong *table, flags, maxkpa;
618 * Split any call crossing MemMin to make below simpler.
620 if(base < MemMin && len > MemMin-base){
623 map(MemMin, len-n, type);
627 * Let lowraminit and umbscan hash out the low MemMin.
633 * Any non-memory below 16*MB is used as upper mem blocks.
635 if(type == MemUPA && base < 16*MB && len > 16*MB-base){
636 map(base, 16*MB-base, MemUMB);
637 map(16*MB, len-(16*MB-base), MemUPA);
642 * Memory below CPU0END is reserved for the kernel
643 * and already mapped.
645 if(base < PADDR(CPU0END)){
646 n = PADDR(CPU0END) - base;
649 map(PADDR(CPU0END), len-n, type);
654 * Memory between KTZERO and end is the kernel itself
655 * and is already mapped.
657 if(base < PADDR(KTZERO) && len > PADDR(KTZERO)-base){
658 map(base, PADDR(KTZERO)-base, type);
661 if(PADDR(KTZERO) < base && base < PADDR(PGROUND((ulong)end))){
662 n = PADDR(PGROUND((ulong)end));
665 map(PADDR(PGROUND((ulong)end)), len-n, type);
670 * Now we have a simple case.
672 // print("map %.8lux %.8lux %d\n", base, base+len, type);
675 mapfree(&rmapram, base, len);
676 flags = PTEWRITE|PTEVALID;
679 mapfree(&rmapumb, base, len);
680 flags = PTEWRITE|PTEUNCACHED|PTEVALID;
683 mapfree(&rmapupa, base, len);
693 * bottom MemMin is already mapped - just twiddle flags.
694 * (not currently used - see above)
697 table = KADDR(PPN(m->pdb[PDX(base)]));
700 for(; base<e; base+=BY2PG)
701 table[PTX(base)] |= flags;
706 * Only map from KZERO to 2^32.
712 if(len > maxkpa-base)
714 pdbmap(m->pdb, base|flags, base+KZERO, len);
721 ulong base, len, last;
726 /* passed by bootloader */
727 if((s = getconf("*e820")) == nil)
728 if((s = getconf("e820")) == nil)
731 while(nemap < nelem(emap)){
738 if(s[1] == ' '){ /* new format */
739 e->type = s[0] - '0';
742 e->base = strtoull(s, &s, 16);
745 e->top = strtoull(s, &s, 16);
746 if(*s != ' ' && *s != 0)
753 qsort(emap, nemap, sizeof emap[0], emapcmp);
755 for(i=0; i<nemap; i++){
758 * pull out the info but only about the low 32 bits...
760 if(e->base >= (1ULL<<32))
768 if(e->top > (1ULL<<32))
773 * If the map skips addresses, mark them available.
776 map(last, base-last, MemUPA);
777 map(base, len, (e->type == 1) ? MemRAM : MemReserved);
783 map(last, -last, MemUPA);
797 if(p = getconf("*maxmem"))
798 maxmem = strtoul(p, 0, 0);
803 * Set special attributes for memory between 640KB and 1MB:
804 * VGA memory is writethrough;
805 * BIOS ROM's/UMB's are uncached;
806 * then scan for useful memory.
808 for(pa = 0xA0000; pa < 0xC0000; pa += BY2PG){
809 pte = mmuwalk(m->pdb, (ulong)KADDR(pa), 2, 0);
812 for(pa = 0xC0000; pa < 0x100000; pa += BY2PG){
813 pte = mmuwalk(m->pdb, (ulong)KADDR(pa), 2, 0);
816 mmuflushtlb(PADDR(m->pdb));
824 * Set the conf entries describing banks of allocatable memory.
826 for(i=0; i<nelem(mapram) && i<nelem(conf.mem); i++){
827 mp = &rmapram.map[i];
830 cm->npage = mp->size/BY2PG;
834 for(; i<nelem(mapram); i++)
835 lost += rmapram.map[i].size;
837 print("meminit - lost %lud bytes\n", lost);
844 * Allocate memory from the upper memory blocks.
847 umbmalloc(ulong addr, int size, int align)
851 if(a = mapalloc(&rmapumb, addr, size, align))
852 return (ulong)KADDR(a);
858 umbfree(ulong addr, int size)
860 mapfree(&rmapumb, PADDR(addr), size);
864 umbrwmalloc(ulong addr, int size, int align)
869 if(a = mapalloc(&rmapumbrw, addr, size, align))
870 return(ulong)KADDR(a);
873 * Perhaps the memory wasn't visible before
874 * the interface is initialised, so try again.
876 if((a = umbmalloc(addr, size, align)) == 0)
881 if(p[0] == 0xCC && p[size-1] == 0xCC)
889 umbrwfree(ulong addr, int size)
891 mapfree(&rmapumbrw, PADDR(addr), size);
895 * Give out otherwise-unused physical address space
896 * for use in configuring devices. Note that upaalloc
897 * does not map the physical address into virtual memory.
898 * Call vmap to do that.
901 upaalloc(int size, int align)
905 a = mapalloc(&rmapupa, 0, size, align);
907 print("out of physical address space allocating %d\n", size);
914 upafree(ulong pa, int size)
916 mapfree(&rmapupa, pa, size);
920 upareserve(ulong pa, int size)
924 a = mapalloc(&rmapupa, pa, size, 0);
927 * This can happen when we're using the E820
928 * map, which might have already reserved some
929 * of the regions claimed by the pci devices.
931 // print("upareserve: cannot reserve pa=%#.8lux size=%d\n", pa, size);
933 mapfree(&rmapupa, a, size);