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) */
24 MemACPI = 3, /* ACPI tables */
30 MemMax = (3*1024+768)*MB,
33 typedef struct Map Map;
39 typedef struct RMap RMap;
49 * Memory allocation tracking.
51 static Map mapupa[16];
52 static RMap rmapupa = {
53 "unallocated unbacked physical memory",
55 &mapupa[nelem(mapupa)-1],
58 static Map mapram[16];
59 static RMap rmapram = {
62 &mapram[nelem(mapram)-1],
65 static Map mapumb[64];
66 static RMap rmapumb = {
69 &mapumb[nelem(mapumb)-1],
72 static Map mapumbrw[16];
73 static RMap rmapumbrw = {
76 &mapumbrw[nelem(mapumbrw)-1],
79 static Map mapacpi[16];
80 static RMap rmapacpi = {
83 &mapacpi[nelem(mapacpi)-1],
91 print("%s\n", rmap->name);
92 for(mp = rmap->map; mp->size; mp++)
93 print("\t%8.8luX %8.8luX (%lud)\n", mp->addr, mp->addr+mp->size, mp->size);
100 ulong maxpa, maxpa1, maxpa2;
102 maxpa = (nvramread(0x18)<<8)|nvramread(0x17);
103 maxpa1 = (nvramread(0x31)<<8)|nvramread(0x30);
104 maxpa2 = (nvramread(0x16)<<8)|nvramread(0x15);
105 print("maxpa = %luX -> %luX, maxpa1 = %luX maxpa2 = %luX\n",
106 maxpa, MB+maxpa*KB, maxpa1, maxpa2);
110 mapprint(&rmapumbrw);
116 mapfree(RMap* rmap, ulong addr, ulong size)
125 for(mp = rmap->map; mp->addr <= addr && mp->size; mp++)
128 if(mp > rmap->map && (mp-1)->addr+(mp-1)->size == addr){
129 (mp-1)->size += size;
130 if(addr+size == mp->addr){
131 (mp-1)->size += mp->size;
134 (mp-1)->addr = mp->addr;
135 (mp-1)->size = mp->size;
140 if(addr+size == mp->addr && mp->size){
145 if(mp >= rmap->mapend){
146 print("mapfree: %s: losing 0x%luX, %ld\n",
147 rmap->name, addr, size);
162 mapalloc(RMap* rmap, ulong addr, int size, int align)
168 for(mp = rmap->map; mp->size; mp++){
173 * A specific address range has been given:
174 * if the current map entry is greater then
175 * the address is not in the map;
176 * if the current map entry does not overlap
177 * the beginning of the requested range then
178 * continue on to the next map entry;
179 * if the current map entry does not entirely
180 * contain the requested range then the range
185 if(mp->size < addr - maddr) /* maddr+mp->size < addr, but no overflow */
187 if(addr - maddr > mp->size - size) /* addr+size > maddr+mp->size, but no overflow */
193 maddr = ((maddr+align-1)/align)*align;
194 if(mp->addr+mp->size-maddr < size)
198 mp->addr = maddr+size;
199 mp->size -= maddr-oaddr+size;
203 (mp-1)->addr = mp->addr;
204 }while((mp-1)->size = mp->size);
209 mapfree(rmap, oaddr, maddr-oaddr);
219 * Allocate from the ram map directly to make page tables.
220 * Called by mmuwalk during e820scan.
227 if(conf.mem[0].npage != 0)
228 return xspanalloc(BY2PG, BY2PG, 0);
229 m = mapalloc(&rmapram, 0, BY2PG, BY2PG);
242 if((p = getconf("umbexclude")) == nil)
245 while(p && *p != '\0' && *p != '\n'){
247 addr = strtoul(p, &rptr, 0);
248 if(rptr == nil || rptr == p || *rptr != '-'){
249 print("umbexclude: invalid argument <%s>\n", op);
254 size = strtoul(p, &rptr, 0) - addr + 1;
256 print("umbexclude: bad range <%s>\n", op);
259 if(rptr != nil && *rptr == ',')
263 mapalloc(&rmapumb, addr, size, 0);
273 * Scan the Upper Memory Blocks (0xA0000->0xF0000) for pieces
274 * which aren't used; they can be used later for devices which
275 * want to allocate some virtual address space.
276 * Check for two things:
277 * 1) device BIOS ROM. This should start with a two-byte header
278 * of 0x55 0xAA, followed by a byte giving the size of the ROM
279 * in 512-byte chunks. These ROM's must start on a 2KB boundary.
280 * 2) device memory. This is read-write.
281 * There are some assumptions: there's VGA memory at 0xA0000 and
282 * the VGA BIOS ROM is at 0xC0000. Also, if there's no ROM signature
283 * at 0xE0000 then the whole 64KB up to 0xF0000 is theoretically up
284 * for grabs; check anyway.
287 while(p < (uchar*)KADDR(0xE0000)){
289 * Test for 0x55 0xAA before poking obtrusively,
290 * some machines (e.g. Thinkpad X20) seem to map
291 * something dynamic here (cardbus?) causing weird
292 * problems if it is changed.
294 if(p[0] == 0x55 && p[1] == 0xAA){
301 if(p[0] != 0xCC || p[2*KB-1] != 0xCC){
305 if(p[0] == 0x55 && p[1] == 0xAA){
309 if(p[0] == 0xFF && p[1] == 0xFF)
310 mapfree(&rmapumb, PADDR(p), 2*KB);
313 mapfree(&rmapumbrw, PADDR(p), 2*KB);
318 if(p[0] != 0x55 || p[1] != 0xAA){
321 if(p[0] != 0xCC && p[64*KB-1] != 0xCC)
322 mapfree(&rmapumb, PADDR(p), 64*KB);
329 checksum(void *v, int n)
341 sigscan(uchar *addr, int len, char *sig, int size, int step)
347 e = addr+len-(size > sl ? size : sl);
348 for(p = addr; p <= e; p += step){
349 if(memcmp(p, sig, sl) != 0)
351 if(size && checksum(p, size) != 0)
365 top = ((bda[0x14]<<8) | bda[0x13])*KB;
367 if(top < 64*KB || top > 640*KB)
368 top = 640*KB; /* sanity */
370 /* reserved for bios tables (EBDA) */
377 sigsearch(char* signature, int size)
384 * Search for the data structure:
385 * 1) within the first KiB of the Extended BIOS Data Area (EBDA), or
386 * 2) within the last KiB of system base memory if the EBDA segment
388 * 3) within the BIOS ROM address space between 0xf0000 and 0xfffff
389 * (but will actually check 0xe0000 to 0xfffff).
392 if(memcmp(KADDR(0xfffd9), "EISA", 4) == 0){
393 if((p = (bda[0x0f]<<8)|bda[0x0e]) != 0){
394 if((r = sigscan(KADDR(p<<4), 1024, signature, size, 16)) != nil)
398 if((r = sigscan(KADDR(convmemsize()), 1024, signature, size, 16)) != nil)
401 /* hack for virtualbox: look in KiB below 0xa0000 */
402 if((r = sigscan(KADDR(0xa0000-1024), 1024, signature, size, 16)) != nil)
405 return sigscan(KADDR(0xe0000), 0x20000, signature, size, 16);
411 static char signature[] = "RSD PTR ";
415 if((p = sigsearch(signature, 36)) != nil)
417 if((p = sigsearch(signature, 20)) != nil)
419 for(m = rmapacpi.map; m < rmapacpi.mapend && m->size; m++){
420 if(m->size > 0x7FFFFFFF)
422 if((v = vmap(m->addr, m->size)) != nil){
423 p = sigscan(v, m->size, signature, 36, 4);
425 p = sigscan(v, m->size, signature, 20, 4);
428 return vmap(m->addr + (p - v), 64);
440 * Initialise the memory bank information for conventional memory
441 * (i.e. less than 640KB). The base is the first location after the
442 * bootstrap processor MMU information and the limit is obtained from
443 * the BIOS data area.
448 mapfree(&rmapram, x, pa-x);
449 memset(KADDR(x), 0, pa-x); /* keep us honest */
452 x = PADDR(PGROUND((uintptr)end));
455 panic("kernel too big");
456 mapfree(&rmapram, x, pa-x);
457 memset(KADDR(x), 0, pa-x); /* keep us honest */
461 ramscan(ulong maxmem)
463 ulong *k0, kzero, map, maxkpa, maxpa, pa, *pte, *table, *va, vbase, x;
464 int nvalid[NMemType];
467 * The bootstrap code has has created a prototype page
468 * table which maps the first MemMin of physical memory to KZERO.
469 * The page directory is at m->pdb and the first page of
470 * free memory is after the per-processor MMU information.
475 * Check if the extended memory size can be obtained from the CMOS.
476 * If it's 0 then it's either not known or >= 64MB. Always check
477 * at least 24MB in case there's a memory gap (up to 8MB) below 16MB;
478 * in this case the memory from the gap is remapped to the top of
480 * The value in CMOS is supposed to be the number of KB above 1MB.
483 x = (nvramread(0x18)<<8)|nvramread(0x17);
484 if(x == 0 || x >= (63*KB))
492 maxkpa = (u32int)-KZERO; /* 2^32 - KZERO */
495 * March up memory from MemMin to maxpa 1MB at a time,
496 * mapping the first page and checking the page can
497 * be written and read correctly. The page tables are created here
498 * on the fly, allocating from low memory as necessary.
500 k0 = (ulong*)KADDR(0);
504 memset(nvalid, 0, sizeof(nvalid));
507 * Can't map memory to KADDR(pa) when we're walking because
508 * can only use KADDR for relatively low addresses.
509 * Instead, map each 4MB we scan to the virtual address range
510 * MemMin->MemMin+4MB while we are scanning.
515 * Map the page. Use mapalloc(&rmapram, ...) to make
516 * the page table if necessary, it will be returned to the
517 * pool later if it isn't needed. Map in a fixed range (the second 4M)
518 * because high physical addresses cannot be passed to KADDR.
520 va = (void*)(vbase + pa%(4*MB));
521 table = &m->pdb[PDX(va)];
523 if(map == 0 && (map = mapalloc(&rmapram, 0, BY2PG, BY2PG)) == 0)
525 memset(KADDR(map), 0, BY2PG);
526 *table = map|PTEWRITE|PTEVALID;
527 memset(nvalid, 0, sizeof(nvalid));
529 table = KADDR(PPN(*table));
530 pte = &table[PTX(va)];
532 *pte = pa|PTEWRITE|PTEUNCACHED|PTEVALID;
533 mmuflushtlb(PADDR(m->pdb));
535 * Write a pattern to the page and write a different
536 * pattern to a possible mirror at KZERO. If the data
537 * reads back correctly the chunk is some type of RAM (possibly
538 * a linearly-mapped VGA framebuffer, for instance...) and
539 * can be cleared and added to the memory pool. If not, the
540 * chunk is marked uncached and added to the UMB pool if <16MB
541 * or is marked invalid and added to the UPA pool.
546 nvalid[MemRAM] += MB/BY2PG;
547 mapfree(&rmapram, pa, MB);
550 *pte++ = pa|PTEWRITE|PTEVALID;
553 mmuflushtlb(PADDR(m->pdb));
554 /* memset(va, 0, MB); so damn slow to memset all of memory */
557 nvalid[MemUMB] += MB/BY2PG;
558 mapfree(&rmapumb, pa, MB);
561 *pte++ = pa|PTEWRITE|PTEUNCACHED|PTEVALID;
566 nvalid[MemUPA] += MB/BY2PG;
567 mapfree(&rmapupa, pa, MB);
573 * Done with this 4MB chunk, review the options:
574 * 1) not physical memory and >=16MB - invalidate the PDB entry;
575 * 2) physical memory - use the 4MB page extension if possible;
576 * 3) not physical memory and <16MB - use the 4MB page extension
578 * 4) mixed or no 4MB page extension - commit the already
579 * initialised space for the page table.
581 if(pa%(4*MB) == 0 && pa >= 32*MB && nvalid[MemUPA] == (4*MB)/BY2PG){
583 * If we encounter a 4MB chunk of missing memory
584 * at a sufficiently high offset, call it the end of
585 * memory. Otherwise we run the risk of thinking
586 * that video memory is real RAM.
590 if(pa <= maxkpa && pa%(4*MB) == 0){
591 table = &m->pdb[PDX(KADDR(pa - 4*MB))];
592 if(nvalid[MemUPA] == (4*MB)/BY2PG)
594 else if(nvalid[MemRAM] == (4*MB)/BY2PG && (m->cpuiddx & Pse))
595 *table = (pa - 4*MB)|PTESIZE|PTEWRITE|PTEVALID;
596 else if(nvalid[MemUMB] == (4*MB)/BY2PG && (m->cpuiddx & Pse))
597 *table = (pa - 4*MB)|PTESIZE|PTEWRITE|PTEUNCACHED|PTEVALID;
599 *table = map|PTEWRITE|PTEVALID;
603 mmuflushtlb(PADDR(m->pdb));
607 * If we didn't reach the end of the 4MB chunk, that part won't
608 * be mapped. Commit the already initialised space for the page table.
610 if(pa % (4*MB) && pa <= maxkpa){
611 m->pdb[PDX(KADDR(pa))] = map|PTEWRITE|PTEVALID;
615 mapfree(&rmapram, map, BY2PG);
617 m->pdb[PDX(vbase)] = 0;
618 mmuflushtlb(PADDR(m->pdb));
620 mapfree(&rmapupa, pa, (u32int)-pa);
624 typedef struct Emap Emap;
631 static Emap emap[128];
635 emapcmp(const void *va, const void *vb)
645 if(a->base < b->base)
647 if(a->base > b->base)
653 map(ulong base, ulong len, int type)
656 ulong *table, flags, maxkpa;
659 * Split any call crossing MemMin to make below simpler.
661 if(base < MemMin && len > MemMin-base){
664 map(MemMin, len-n, type);
668 * Let lowraminit and umbscan hash out the low MemMin.
674 * Any non-memory below 16*MB is used as upper mem blocks.
676 if(type == MemUPA && base < 16*MB && len > 16*MB-base){
677 map(base, 16*MB-base, MemUMB);
678 map(16*MB, len-(16*MB-base), MemUPA);
683 * Memory below CPU0END is reserved for the kernel
684 * and already mapped.
686 if(base < PADDR(CPU0END)){
687 n = PADDR(CPU0END) - base;
690 map(PADDR(CPU0END), len-n, type);
695 * Memory between KTZERO and end is the kernel itself
696 * and is already mapped.
698 if(base < PADDR(KTZERO) && len > PADDR(KTZERO)-base){
699 map(base, PADDR(KTZERO)-base, type);
702 if(PADDR(KTZERO) < base && base < PADDR(PGROUND((ulong)end))){
703 n = PADDR(PGROUND((ulong)end));
706 map(PADDR(PGROUND((ulong)end)), len-n, type);
711 * Now we have a simple case.
713 // print("map %.8lux %.8lux %d\n", base, base+len, type);
716 mapfree(&rmapram, base, len);
717 flags = PTEWRITE|PTEVALID;
720 mapfree(&rmapumb, base, len);
721 flags = PTEWRITE|PTEUNCACHED|PTEVALID;
724 mapfree(&rmapupa, base, len);
728 mapfree(&rmapacpi, base, len);
738 * bottom MemMin is already mapped - just twiddle flags.
739 * (not currently used - see above)
742 table = KADDR(PPN(m->pdb[PDX(base)]));
745 for(; base<e; base+=BY2PG)
746 table[PTX(base)] |= flags;
751 * Only map from KZERO to 2^32.
757 if(len > maxkpa-base)
759 pdbmap(m->pdb, base|flags, base+KZERO, len);
766 ulong base, len, last;
771 /* passed by bootloader */
772 if((s = getconf("*e820")) == nil)
773 if((s = getconf("e820")) == nil)
776 while(nemap < nelem(emap)){
783 if(s[1] == ' '){ /* new format */
784 e->type = s[0] - '0';
787 e->base = strtoull(s, &s, 16);
790 e->top = strtoull(s, &s, 16);
791 if(*s != ' ' && *s != 0)
798 qsort(emap, nemap, sizeof emap[0], emapcmp);
800 for(i=0; i<nemap; i++){
803 * pull out the info but only about the low 32 bits...
805 if(e->base >= (1ULL<<32))
813 if(e->top > (1ULL<<32))
818 * If the map skips addresses, mark them available.
821 map(last, base-last, MemUPA);
825 map(base, len, MemRAM);
828 map(base, len, MemACPI);
831 map(base, len, MemReserved);
839 map(last, -last, MemUPA);
853 if(p = getconf("*maxmem"))
854 maxmem = strtoul(p, 0, 0);
859 * Set special attributes for memory between 640KB and 1MB:
860 * VGA memory is writethrough;
861 * BIOS ROM's/UMB's are uncached;
862 * then scan for useful memory.
864 for(pa = 0xA0000; pa < 0xC0000; pa += BY2PG){
865 pte = mmuwalk(m->pdb, (ulong)KADDR(pa), 2, 0);
868 for(pa = 0xC0000; pa < 0x100000; pa += BY2PG){
869 pte = mmuwalk(m->pdb, (ulong)KADDR(pa), 2, 0);
872 mmuflushtlb(PADDR(m->pdb));
880 * Set the conf entries describing banks of allocatable memory.
882 for(i=0; i<nelem(mapram) && i<nelem(conf.mem); i++){
883 mp = &rmapram.map[i];
886 cm->npage = mp->size/BY2PG;
890 for(; i<nelem(mapram); i++)
891 lost += rmapram.map[i].size;
893 print("meminit - lost %lud bytes\n", lost);
900 * Allocate memory from the upper memory blocks.
903 umbmalloc(ulong addr, int size, int align)
907 if(a = mapalloc(&rmapumb, addr, size, align))
908 return (ulong)KADDR(a);
914 umbfree(ulong addr, int size)
916 mapfree(&rmapumb, PADDR(addr), size);
920 umbrwmalloc(ulong addr, int size, int align)
925 if(a = mapalloc(&rmapumbrw, addr, size, align))
926 return(ulong)KADDR(a);
929 * Perhaps the memory wasn't visible before
930 * the interface is initialised, so try again.
932 if((a = umbmalloc(addr, size, align)) == 0)
937 if(p[0] == 0xCC && p[size-1] == 0xCC)
945 umbrwfree(ulong addr, int size)
947 mapfree(&rmapumbrw, PADDR(addr), size);
951 * Give out otherwise-unused physical address space
952 * for use in configuring devices. Note that upaalloc
953 * does not map the physical address into virtual memory.
954 * Call vmap to do that.
957 upaalloc(int size, int align)
961 a = mapalloc(&rmapupa, 0, size, align);
963 print("out of physical address space allocating %d\n", size);
970 upafree(ulong pa, int size)
972 mapfree(&rmapupa, pa, size);
976 upareserve(ulong pa, int size)
980 a = mapalloc(&rmapupa, pa, size, 0);
983 * This can happen when we're using the E820
984 * map, which might have already reserved some
985 * of the regions claimed by the pci devices.
987 // print("upareserve: cannot reserve pa=%#.8lux size=%d\n", pa, size);
989 mapfree(&rmapupa, a, size);