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"
19 MemUPA = 0, /* unbacked physical address */
20 MemRAM = 1, /* physical memory */
21 MemUMB = 2, /* upper memory block (<16MB) */
28 MemMax = (3*1024+768)*MB,
31 typedef struct Map Map;
37 typedef struct RMap RMap;
47 * Memory allocation tracking.
49 static Map mapupa[16];
50 static RMap rmapupa = {
51 "unallocated unbacked physical memory",
53 &mapupa[nelem(mapupa)-1],
56 static Map mapram[16];
57 static RMap rmapram = {
60 &mapram[nelem(mapram)-1],
63 static Map mapumb[64];
64 static RMap rmapumb = {
67 &mapumb[nelem(mapumb)-1],
70 static Map mapumbrw[16];
71 static RMap rmapumbrw = {
74 &mapumbrw[nelem(mapumbrw)-1],
82 print("%s\n", rmap->name);
83 for(mp = rmap->map; mp->size; mp++)
84 print("\t%8.8luX %8.8luX (%lud)\n", mp->addr, mp->addr+mp->size, mp->size);
91 ulong maxpa, maxpa1, maxpa2;
93 maxpa = (nvramread(0x18)<<8)|nvramread(0x17);
94 maxpa1 = (nvramread(0x31)<<8)|nvramread(0x30);
95 maxpa2 = (nvramread(0x16)<<8)|nvramread(0x15);
96 print("maxpa = %luX -> %luX, maxpa1 = %luX maxpa2 = %luX\n",
97 maxpa, MB+maxpa*KB, maxpa1, maxpa2);
101 mapprint(&rmapumbrw);
106 mapfree(RMap* rmap, ulong addr, ulong size)
115 for(mp = rmap->map; mp->addr <= addr && mp->size; mp++)
118 if(mp > rmap->map && (mp-1)->addr+(mp-1)->size == addr){
119 (mp-1)->size += size;
120 if(addr+size == mp->addr){
121 (mp-1)->size += mp->size;
124 (mp-1)->addr = mp->addr;
125 (mp-1)->size = mp->size;
130 if(addr+size == mp->addr && mp->size){
135 if(mp >= rmap->mapend){
136 print("mapfree: %s: losing 0x%luX, %ld\n",
137 rmap->name, addr, size);
152 mapalloc(RMap* rmap, ulong addr, int size, int align)
158 for(mp = rmap->map; mp->size; mp++){
163 * A specific address range has been given:
164 * if the current map entry is greater then
165 * the address is not in the map;
166 * if the current map entry does not overlap
167 * the beginning of the requested range then
168 * continue on to the next map entry;
169 * if the current map entry does not entirely
170 * contain the requested range then the range
175 if(mp->size < addr - maddr) /* maddr+mp->size < addr, but no overflow */
177 if(addr - maddr > mp->size - size) /* addr+size > maddr+mp->size, but no overflow */
183 maddr = ((maddr+align-1)/align)*align;
184 if(mp->addr+mp->size-maddr < size)
188 mp->addr = maddr+size;
189 mp->size -= maddr-oaddr+size;
193 (mp-1)->addr = mp->addr;
194 }while((mp-1)->size = mp->size);
199 mapfree(rmap, oaddr, maddr-oaddr);
209 * Allocate from the ram map directly to make page tables.
210 * Called by mmuwalk during e820scan.
217 m = mapalloc(&rmapram, 0, BY2PG, BY2PG);
230 if((p = getconf("umbexclude")) == nil)
233 while(p && *p != '\0' && *p != '\n'){
235 addr = strtoul(p, &rptr, 0);
236 if(rptr == nil || rptr == p || *rptr != '-'){
237 print("umbexclude: invalid argument <%s>\n", op);
242 size = strtoul(p, &rptr, 0) - addr + 1;
244 print("umbexclude: bad range <%s>\n", op);
247 if(rptr != nil && *rptr == ',')
251 mapalloc(&rmapumb, addr, size, 0);
261 * Scan the Upper Memory Blocks (0xA0000->0xF0000) for pieces
262 * which aren't used; they can be used later for devices which
263 * want to allocate some virtual address space.
264 * Check for two things:
265 * 1) device BIOS ROM. This should start with a two-byte header
266 * of 0x55 0xAA, followed by a byte giving the size of the ROM
267 * in 512-byte chunks. These ROM's must start on a 2KB boundary.
268 * 2) device memory. This is read-write.
269 * There are some assumptions: there's VGA memory at 0xA0000 and
270 * the VGA BIOS ROM is at 0xC0000. Also, if there's no ROM signature
271 * at 0xE0000 then the whole 64KB up to 0xF0000 is theoretically up
272 * for grabs; check anyway.
275 while(p < (uchar*)KADDR(0xE0000)){
277 * Test for 0x55 0xAA before poking obtrusively,
278 * some machines (e.g. Thinkpad X20) seem to map
279 * something dynamic here (cardbus?) causing weird
280 * problems if it is changed.
282 if(p[0] == 0x55 && p[1] == 0xAA){
289 if(p[0] != 0xCC || p[2*KB-1] != 0xCC){
293 if(p[0] == 0x55 && p[1] == 0xAA){
297 if(p[0] == 0xFF && p[1] == 0xFF)
298 mapfree(&rmapumb, PADDR(p), 2*KB);
301 mapfree(&rmapumbrw, PADDR(p), 2*KB);
306 if(p[0] != 0x55 || p[1] != 0xAA){
309 if(p[0] != 0xCC && p[64*KB-1] != 0xCC)
310 mapfree(&rmapumb, PADDR(p), 64*KB);
317 checksum(void *v, int n)
329 sigscan(uchar* addr, int len, char* signature)
335 sl = strlen(signature);
336 for(p = addr; p+sl < e; p += 16)
337 if(memcmp(p, signature, sl) == 0)
343 sigsearch(char* signature)
350 * Search for the data structure:
351 * 1) within the first KiB of the Extended BIOS Data Area (EBDA), or
352 * 2) within the last KiB of system base memory if the EBDA segment
354 * 3) within the BIOS ROM address space between 0xf0000 and 0xfffff
355 * (but will actually check 0xe0000 to 0xfffff).
358 if(memcmp(KADDR(0xfffd9), "EISA", 4) == 0){
359 if((p = (bda[0x0f]<<8)|bda[0x0e]) != 0){
360 if((r = sigscan(KADDR(p<<4), 1024, signature)) != nil)
365 if((p = ((bda[0x14]<<8)|bda[0x13])*1024) != 0){
366 if((r = sigscan(KADDR(p-1024), 1024, signature)) != nil)
369 /* hack for virtualbox: look in KiB below 0xa0000 */
370 if((r = sigscan(KADDR(0xa0000-1024), 1024, signature)) != nil)
373 return sigscan(KADDR(0xe0000), 0x20000, signature);
383 * Initialise the memory bank information for conventional memory
384 * (i.e. less than 640KB). The base is the first location after the
385 * bootstrap processor MMU information and the limit is obtained from
386 * the BIOS data area.
389 bda = (uchar*)KADDR(0x400);
390 n = ((bda[0x14]<<8)|bda[0x13])*KB-x;
391 mapfree(&rmapram, x, n);
392 memset(KADDR(x), 0, n); /* keep us honest */
394 x = PADDR(PGROUND((ulong)end));
397 panic("kernel too big");
398 mapfree(&rmapram, x, pa-x);
399 memset(KADDR(x), 0, pa-x); /* keep us honest */
403 ramscan(ulong maxmem)
405 ulong *k0, kzero, map, maxkpa, maxpa, pa, *pte, *table, *va, vbase, x;
406 int nvalid[NMemType];
409 * The bootstrap code has has created a prototype page
410 * table which maps the first MemMin of physical memory to KZERO.
411 * The page directory is at m->pdb and the first page of
412 * free memory is after the per-processor MMU information.
417 * Check if the extended memory size can be obtained from the CMOS.
418 * If it's 0 then it's either not known or >= 64MB. Always check
419 * at least 24MB in case there's a memory gap (up to 8MB) below 16MB;
420 * in this case the memory from the gap is remapped to the top of
422 * The value in CMOS is supposed to be the number of KB above 1MB.
425 x = (nvramread(0x18)<<8)|nvramread(0x17);
426 if(x == 0 || x >= (63*KB))
434 maxkpa = (u32int)-KZERO; /* 2^32 - KZERO */
437 * March up memory from MemMin to maxpa 1MB at a time,
438 * mapping the first page and checking the page can
439 * be written and read correctly. The page tables are created here
440 * on the fly, allocating from low memory as necessary.
442 k0 = (ulong*)KADDR(0);
446 memset(nvalid, 0, sizeof(nvalid));
449 * Can't map memory to KADDR(pa) when we're walking because
450 * can only use KADDR for relatively low addresses.
451 * Instead, map each 4MB we scan to the virtual address range
452 * MemMin->MemMin+4MB while we are scanning.
457 * Map the page. Use mapalloc(&rmapram, ...) to make
458 * the page table if necessary, it will be returned to the
459 * pool later if it isn't needed. Map in a fixed range (the second 4M)
460 * because high physical addresses cannot be passed to KADDR.
462 va = (void*)(vbase + pa%(4*MB));
463 table = &m->pdb[PDX(va)];
465 if(map == 0 && (map = mapalloc(&rmapram, 0, BY2PG, BY2PG)) == 0)
467 memset(KADDR(map), 0, BY2PG);
468 *table = map|PTEWRITE|PTEVALID;
469 memset(nvalid, 0, sizeof(nvalid));
471 table = KADDR(PPN(*table));
472 pte = &table[PTX(va)];
474 *pte = pa|PTEWRITE|PTEUNCACHED|PTEVALID;
475 mmuflushtlb(PADDR(m->pdb));
477 * Write a pattern to the page and write a different
478 * pattern to a possible mirror at KZERO. If the data
479 * reads back correctly the chunk is some type of RAM (possibly
480 * a linearly-mapped VGA framebuffer, for instance...) and
481 * can be cleared and added to the memory pool. If not, the
482 * chunk is marked uncached and added to the UMB pool if <16MB
483 * or is marked invalid and added to the UPA pool.
488 nvalid[MemRAM] += MB/BY2PG;
489 mapfree(&rmapram, pa, MB);
492 *pte++ = pa|PTEWRITE|PTEVALID;
495 mmuflushtlb(PADDR(m->pdb));
496 /* memset(va, 0, MB); so damn slow to memset all of memory */
499 nvalid[MemUMB] += MB/BY2PG;
500 mapfree(&rmapumb, pa, MB);
503 *pte++ = pa|PTEWRITE|PTEUNCACHED|PTEVALID;
508 nvalid[MemUPA] += MB/BY2PG;
509 mapfree(&rmapupa, pa, MB);
515 * Done with this 4MB chunk, review the options:
516 * 1) not physical memory and >=16MB - invalidate the PDB entry;
517 * 2) physical memory - use the 4MB page extension if possible;
518 * 3) not physical memory and <16MB - use the 4MB page extension
520 * 4) mixed or no 4MB page extension - commit the already
521 * initialised space for the page table.
523 if(pa%(4*MB) == 0 && pa >= 32*MB && nvalid[MemUPA] == (4*MB)/BY2PG){
525 * If we encounter a 4MB chunk of missing memory
526 * at a sufficiently high offset, call it the end of
527 * memory. Otherwise we run the risk of thinking
528 * that video memory is real RAM.
532 if(pa <= maxkpa && pa%(4*MB) == 0){
533 table = &m->pdb[PDX(KADDR(pa - 4*MB))];
534 if(nvalid[MemUPA] == (4*MB)/BY2PG)
536 else if(nvalid[MemRAM] == (4*MB)/BY2PG && (m->cpuiddx & Pse))
537 *table = (pa - 4*MB)|PTESIZE|PTEWRITE|PTEVALID;
538 else if(nvalid[MemUMB] == (4*MB)/BY2PG && (m->cpuiddx & Pse))
539 *table = (pa - 4*MB)|PTESIZE|PTEWRITE|PTEUNCACHED|PTEVALID;
541 *table = map|PTEWRITE|PTEVALID;
545 mmuflushtlb(PADDR(m->pdb));
549 * If we didn't reach the end of the 4MB chunk, that part won't
550 * be mapped. Commit the already initialised space for the page table.
552 if(pa % (4*MB) && pa <= maxkpa){
553 m->pdb[PDX(KADDR(pa))] = map|PTEWRITE|PTEVALID;
557 mapfree(&rmapram, map, BY2PG);
559 m->pdb[PDX(vbase)] = 0;
560 mmuflushtlb(PADDR(m->pdb));
562 mapfree(&rmapupa, pa, (u32int)-pa);
566 typedef struct Emap Emap;
573 static Emap emap[128];
577 emapcmp(const void *va, const void *vb)
583 if(a->base < b->base)
585 if(a->base > b->base)
587 return a->len - b->len;
591 map(ulong base, ulong len, int type)
594 ulong *table, flags, maxkpa;
597 * Split any call crossing MemMin to make below simpler.
599 if(base < MemMin && len > MemMin-base){
602 map(MemMin, len-n, type);
606 * Let lowraminit and umbscan hash out the low MemMin.
612 * Any non-memory below 16*MB is used as upper mem blocks.
614 if(type == MemUPA && base < 16*MB && base+len > 16*MB){
615 map(base, 16*MB-base, MemUMB);
616 map(16*MB, len-(16*MB-base), MemUPA);
621 * Memory below CPU0END is reserved for the kernel
622 * and already mapped.
624 if(base < PADDR(CPU0END)){
625 n = PADDR(CPU0END) - base;
628 map(PADDR(CPU0END), len-n, type);
633 * Memory between KTZERO and end is the kernel itself
634 * and is already mapped.
636 if(base < PADDR(KTZERO) && base+len > PADDR(KTZERO)){
637 map(base, PADDR(KTZERO)-base, type);
640 if(PADDR(KTZERO) < base && base < PADDR(PGROUND((ulong)end))){
641 n = PADDR(PGROUND((ulong)end));
644 map(PADDR(PGROUND((ulong)end)), len-n, type);
649 * Now we have a simple case.
651 // print("map %.8lux %.8lux %d\n", base, base+len, type);
654 mapfree(&rmapram, base, len);
655 flags = PTEWRITE|PTEVALID;
658 mapfree(&rmapumb, base, len);
659 flags = PTEWRITE|PTEUNCACHED|PTEVALID;
662 mapfree(&rmapupa, base, len);
672 * bottom MemMin is already mapped - just twiddle flags.
673 * (not currently used - see above)
676 table = KADDR(PPN(m->pdb[PDX(base)]));
679 for(; base<e; base+=BY2PG)
680 table[PTX(base)] |= flags;
685 * Only map from KZERO to 2^32.
691 if(len > maxkpa-base)
693 pdbmap(m->pdb, base|flags, base+KZERO, len);
706 /* passed by bootloader */
707 if((s = getconf("*e820")) == nil)
708 if((s = getconf("e820")) == nil)
710 for(nemap = 0; nemap < nelem(emap); nemap++){
717 if(s[1] == ' '){ /* new format */
718 e->type = s[0] - '0';
721 e->base = strtoull(s, &s, 16);
724 e->len = strtoull(s, &s, 16);
725 if(*s != ' ' && *s != 0 || e->len <= e->base)
731 qsort(emap, nemap, sizeof emap[0], emapcmp);
733 for(i=0; i<nemap; i++){
736 * pull out the info but only about the low 32 bits...
738 if(e->base >= (1LL<<32))
741 if(base+e->len > (1LL<<32))
746 * If the map skips addresses, mark them available.
749 map(last, e->base-last, MemUPA);
751 map(base, len, (e->type == 1) ? MemRAM : MemReserved);
754 map(last, (u32int)-last, MemUPA);
768 if(p = getconf("*maxmem"))
769 maxmem = strtoul(p, 0, 0);
774 * Set special attributes for memory between 640KB and 1MB:
775 * VGA memory is writethrough;
776 * BIOS ROM's/UMB's are uncached;
777 * then scan for useful memory.
779 for(pa = 0xA0000; pa < 0xC0000; pa += BY2PG){
780 pte = mmuwalk(m->pdb, (ulong)KADDR(pa), 2, 0);
783 for(pa = 0xC0000; pa < 0x100000; pa += BY2PG){
784 pte = mmuwalk(m->pdb, (ulong)KADDR(pa), 2, 0);
787 mmuflushtlb(PADDR(m->pdb));
795 * Set the conf entries describing banks of allocatable memory.
797 for(i=0; i<nelem(mapram) && i<nelem(conf.mem); i++){
798 mp = &rmapram.map[i];
801 cm->npage = mp->size/BY2PG;
805 for(; i<nelem(mapram); i++)
806 lost += rmapram.map[i].size;
808 print("meminit - lost %lud bytes\n", lost);
815 * Allocate memory from the upper memory blocks.
818 umbmalloc(ulong addr, int size, int align)
822 if(a = mapalloc(&rmapumb, addr, size, align))
823 return (ulong)KADDR(a);
829 umbfree(ulong addr, int size)
831 mapfree(&rmapumb, PADDR(addr), size);
835 umbrwmalloc(ulong addr, int size, int align)
840 if(a = mapalloc(&rmapumbrw, addr, size, align))
841 return(ulong)KADDR(a);
844 * Perhaps the memory wasn't visible before
845 * the interface is initialised, so try again.
847 if((a = umbmalloc(addr, size, align)) == 0)
852 if(p[0] == 0xCC && p[size-1] == 0xCC)
860 umbrwfree(ulong addr, int size)
862 mapfree(&rmapumbrw, PADDR(addr), size);
866 * Give out otherwise-unused physical address space
867 * for use in configuring devices. Note that upaalloc
868 * does not map the physical address into virtual memory.
869 * Call vmap to do that.
872 upaalloc(int size, int align)
876 a = mapalloc(&rmapupa, 0, size, align);
878 print("out of physical address space allocating %d\n", size);
885 upafree(ulong pa, int size)
887 mapfree(&rmapupa, pa, size);
891 upareserve(ulong pa, int size)
895 a = mapalloc(&rmapupa, pa, size, 0);
898 * This can happen when we're using the E820
899 * map, which might have already reserved some
900 * of the regions claimed by the pci devices.
902 // print("upareserve: cannot reserve pa=%#.8lux size=%d\n", pa, size);
904 mapfree(&rmapupa, a, size);