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 xmapupa[16];
57 static RMap xrmapupa = {
58 "unbacked physical memory",
60 &xmapupa[nelem(xmapupa)-1],
63 static Map mapram[16];
64 static RMap rmapram = {
67 &mapram[nelem(mapram)-1],
70 static Map mapumb[64];
71 static RMap rmapumb = {
74 &mapumb[nelem(mapumb)-1],
77 static Map mapumbrw[16];
78 static RMap rmapumbrw = {
81 &mapumbrw[nelem(mapumbrw)-1],
89 print("%s\n", rmap->name);
90 for(mp = rmap->map; mp->size; mp++)
91 print("\t%8.8luX %8.8luX (%lud)\n", mp->addr, mp->addr+mp->size, mp->size);
98 ulong maxpa, maxpa1, maxpa2;
100 maxpa = (nvramread(0x18)<<8)|nvramread(0x17);
101 maxpa1 = (nvramread(0x31)<<8)|nvramread(0x30);
102 maxpa2 = (nvramread(0x16)<<8)|nvramread(0x15);
103 print("maxpa = %luX -> %luX, maxpa1 = %luX maxpa2 = %luX\n",
104 maxpa, MB+maxpa*KB, maxpa1, maxpa2);
108 mapprint(&rmapumbrw);
113 mapfree(RMap* rmap, ulong addr, ulong size)
122 for(mp = rmap->map; mp->addr <= addr && mp->size; mp++)
125 if(mp > rmap->map && (mp-1)->addr+(mp-1)->size == addr){
126 (mp-1)->size += size;
127 if(addr+size == mp->addr){
128 (mp-1)->size += mp->size;
131 (mp-1)->addr = mp->addr;
132 (mp-1)->size = mp->size;
137 if(addr+size == mp->addr && mp->size){
142 if(mp >= rmap->mapend){
143 print("mapfree: %s: losing 0x%luX, %ld\n",
144 rmap->name, addr, size);
159 mapalloc(RMap* rmap, ulong addr, int size, int align)
165 for(mp = rmap->map; mp->size; mp++){
170 * A specific address range has been given:
171 * if the current map entry is greater then
172 * the address is not in the map;
173 * if the current map entry does not overlap
174 * the beginning of the requested range then
175 * continue on to the next map entry;
176 * if the current map entry does not entirely
177 * contain the requested range then the range
182 if(mp->size < addr - maddr) /* maddr+mp->size < addr, but no overflow */
184 if(addr - maddr > mp->size - size) /* addr+size > maddr+mp->size, but no overflow */
190 maddr = ((maddr+align-1)/align)*align;
191 if(mp->addr+mp->size-maddr < size)
195 mp->addr = maddr+size;
196 mp->size -= maddr-oaddr+size;
200 (mp-1)->addr = mp->addr;
201 }while((mp-1)->size = mp->size);
206 mapfree(rmap, oaddr, maddr-oaddr);
216 * Allocate from the ram map directly to make page tables.
217 * Called by mmuwalk during e820scan.
224 m = mapalloc(&rmapram, 0, BY2PG, BY2PG);
237 if((p = getconf("umbexclude")) == nil)
240 while(p && *p != '\0' && *p != '\n'){
242 addr = strtoul(p, &rptr, 0);
243 if(rptr == nil || rptr == p || *rptr != '-'){
244 print("umbexclude: invalid argument <%s>\n", op);
249 size = strtoul(p, &rptr, 0) - addr + 1;
251 print("umbexclude: bad range <%s>\n", op);
254 if(rptr != nil && *rptr == ',')
258 mapalloc(&rmapumb, addr, size, 0);
268 * Scan the Upper Memory Blocks (0xA0000->0xF0000) for pieces
269 * which aren't used; they can be used later for devices which
270 * want to allocate some virtual address space.
271 * Check for two things:
272 * 1) device BIOS ROM. This should start with a two-byte header
273 * of 0x55 0xAA, followed by a byte giving the size of the ROM
274 * in 512-byte chunks. These ROM's must start on a 2KB boundary.
275 * 2) device memory. This is read-write.
276 * There are some assumptions: there's VGA memory at 0xA0000 and
277 * the VGA BIOS ROM is at 0xC0000. Also, if there's no ROM signature
278 * at 0xE0000 then the whole 64KB up to 0xF0000 is theoretically up
279 * for grabs; check anyway.
282 while(p < (uchar*)KADDR(0xE0000)){
284 * Test for 0x55 0xAA before poking obtrusively,
285 * some machines (e.g. Thinkpad X20) seem to map
286 * something dynamic here (cardbus?) causing weird
287 * problems if it is changed.
289 if(p[0] == 0x55 && p[1] == 0xAA){
296 if(p[0] != 0xCC || p[2*KB-1] != 0xCC){
300 if(p[0] == 0x55 && p[1] == 0xAA){
304 if(p[0] == 0xFF && p[1] == 0xFF)
305 mapfree(&rmapumb, PADDR(p), 2*KB);
308 mapfree(&rmapumbrw, PADDR(p), 2*KB);
313 if(p[0] != 0x55 || p[1] != 0xAA){
316 if(p[0] != 0xCC && p[64*KB-1] != 0xCC)
317 mapfree(&rmapumb, PADDR(p), 64*KB);
330 * Initialise the memory bank information for conventional memory
331 * (i.e. less than 640KB). The base is the first location after the
332 * bootstrap processor MMU information and the limit is obtained from
333 * the BIOS data area.
336 bda = (uchar*)KADDR(0x400);
337 n = ((bda[0x14]<<8)|bda[0x13])*KB-x;
338 mapfree(&rmapram, x, n);
339 memset(KADDR(x), 0, n); /* keep us honest */
341 x = PADDR(PGROUND((ulong)end));
344 panic("kernel too big");
345 mapfree(&rmapram, x, pa-x);
346 memset(KADDR(x), 0, pa-x); /* keep us honest */
350 ramscan(ulong maxmem)
352 ulong *k0, kzero, map, maxkpa, maxpa, pa, *pte, *table, *va, vbase, x;
353 int nvalid[NMemType];
356 * The bootstrap code has has created a prototype page
357 * table which maps the first MemMin of physical memory to KZERO.
358 * The page directory is at m->pdb and the first page of
359 * free memory is after the per-processor MMU information.
364 * Check if the extended memory size can be obtained from the CMOS.
365 * If it's 0 then it's either not known or >= 64MB. Always check
366 * at least 24MB in case there's a memory gap (up to 8MB) below 16MB;
367 * in this case the memory from the gap is remapped to the top of
369 * The value in CMOS is supposed to be the number of KB above 1MB.
372 x = (nvramread(0x18)<<8)|nvramread(0x17);
373 if(x == 0 || x >= (63*KB))
381 maxkpa = (u32int)-KZERO; /* 2^32 - KZERO */
384 * March up memory from MemMin to maxpa 1MB at a time,
385 * mapping the first page and checking the page can
386 * be written and read correctly. The page tables are created here
387 * on the fly, allocating from low memory as necessary.
389 k0 = (ulong*)KADDR(0);
393 memset(nvalid, 0, sizeof(nvalid));
396 * Can't map memory to KADDR(pa) when we're walking because
397 * can only use KADDR for relatively low addresses.
398 * Instead, map each 4MB we scan to the virtual address range
399 * MemMin->MemMin+4MB while we are scanning.
404 * Map the page. Use mapalloc(&rmapram, ...) to make
405 * the page table if necessary, it will be returned to the
406 * pool later if it isn't needed. Map in a fixed range (the second 4M)
407 * because high physical addresses cannot be passed to KADDR.
409 va = (void*)(vbase + pa%(4*MB));
410 table = &m->pdb[PDX(va)];
412 if(map == 0 && (map = mapalloc(&rmapram, 0, BY2PG, BY2PG)) == 0)
414 memset(KADDR(map), 0, BY2PG);
415 *table = map|PTEWRITE|PTEVALID;
416 memset(nvalid, 0, sizeof(nvalid));
418 table = KADDR(PPN(*table));
419 pte = &table[PTX(va)];
421 *pte = pa|PTEWRITE|PTEUNCACHED|PTEVALID;
422 mmuflushtlb(PADDR(m->pdb));
424 * Write a pattern to the page and write a different
425 * pattern to a possible mirror at KZERO. If the data
426 * reads back correctly the chunk is some type of RAM (possibly
427 * a linearly-mapped VGA framebuffer, for instance...) and
428 * can be cleared and added to the memory pool. If not, the
429 * chunk is marked uncached and added to the UMB pool if <16MB
430 * or is marked invalid and added to the UPA pool.
435 nvalid[MemRAM] += MB/BY2PG;
436 mapfree(&rmapram, pa, MB);
439 *pte++ = pa|PTEWRITE|PTEVALID;
442 mmuflushtlb(PADDR(m->pdb));
443 /* memset(va, 0, MB); so damn slow to memset all of memory */
446 nvalid[MemUMB] += MB/BY2PG;
447 mapfree(&rmapumb, pa, MB);
450 *pte++ = pa|PTEWRITE|PTEUNCACHED|PTEVALID;
455 nvalid[MemUPA] += MB/BY2PG;
456 mapfree(&rmapupa, pa, MB);
462 * Done with this 4MB chunk, review the options:
463 * 1) not physical memory and >=16MB - invalidate the PDB entry;
464 * 2) physical memory - use the 4MB page extension if possible;
465 * 3) not physical memory and <16MB - use the 4MB page extension
467 * 4) mixed or no 4MB page extension - commit the already
468 * initialised space for the page table.
470 if(pa%(4*MB) == 0 && pa >= 32*MB && nvalid[MemUPA] == (4*MB)/BY2PG){
472 * If we encounter a 4MB chunk of missing memory
473 * at a sufficiently high offset, call it the end of
474 * memory. Otherwise we run the risk of thinking
475 * that video memory is real RAM.
479 if(pa <= maxkpa && pa%(4*MB) == 0){
480 table = &m->pdb[PDX(KADDR(pa - 4*MB))];
481 if(nvalid[MemUPA] == (4*MB)/BY2PG)
483 else if(nvalid[MemRAM] == (4*MB)/BY2PG && (m->cpuiddx & 0x08))
484 *table = (pa - 4*MB)|PTESIZE|PTEWRITE|PTEVALID;
485 else if(nvalid[MemUMB] == (4*MB)/BY2PG && (m->cpuiddx & 0x08))
486 *table = (pa - 4*MB)|PTESIZE|PTEWRITE|PTEUNCACHED|PTEVALID;
488 *table = map|PTEWRITE|PTEVALID;
492 mmuflushtlb(PADDR(m->pdb));
496 * If we didn't reach the end of the 4MB chunk, that part won't
497 * be mapped. Commit the already initialised space for the page table.
499 if(pa % (4*MB) && pa <= maxkpa){
500 m->pdb[PDX(KADDR(pa))] = map|PTEWRITE|PTEVALID;
504 mapfree(&rmapram, map, BY2PG);
506 m->pdb[PDX(vbase)] = 0;
507 mmuflushtlb(PADDR(m->pdb));
509 mapfree(&rmapupa, pa, (u32int)-pa);
514 * BIOS Int 0x15 E820 memory map.
518 SMAP = ('S'<<24)|('M'<<16)|('A'<<8)|'P',
524 typedef struct Emap Emap;
531 static Emap emap[16];
534 static char *etypes[] =
544 emapcmp(const void *va, const void *vb)
550 if(a->base < b->base)
552 if(a->base > b->base)
558 return a->type - b->type;
562 map(ulong base, ulong len, int type)
565 ulong *table, flags, maxkpa;
568 * Split any call crossing MemMin to make below simpler.
570 if(base < MemMin && len > MemMin-base){
573 map(MemMin, len-n, type);
577 * Let lowraminit and umbscan hash out the low MemMin.
583 * Any non-memory below 16*MB is used as upper mem blocks.
585 if(type == MemUPA && base < 16*MB && base+len > 16*MB){
586 map(base, 16*MB-base, MemUMB);
587 map(16*MB, len-(16*MB-base), MemUPA);
592 * Memory below CPU0END is reserved for the kernel
593 * and already mapped.
595 if(base < PADDR(CPU0END)){
596 n = PADDR(CPU0END) - base;
599 map(PADDR(CPU0END), len-n, type);
604 * Memory between KTZERO and end is the kernel itself
605 * and is already mapped.
607 if(base < PADDR(KTZERO) && base+len > PADDR(KTZERO)){
608 map(base, PADDR(KTZERO)-base, type);
611 if(PADDR(KTZERO) < base && base < PADDR(PGROUND((ulong)end))){
612 n = PADDR(PGROUND((ulong)end));
615 map(PADDR(PGROUND((ulong)end)), len-n, type);
620 * Now we have a simple case.
622 // print("map %.8lux %.8lux %d\n", base, base+len, type);
625 mapfree(&rmapram, base, len);
626 flags = PTEWRITE|PTEVALID;
629 mapfree(&rmapumb, base, len);
630 flags = PTEWRITE|PTEUNCACHED|PTEVALID;
633 mapfree(&rmapupa, base, len);
643 * bottom MemMin is already mapped - just twiddle flags.
644 * (not currently used - see above)
647 table = KADDR(PPN(m->pdb[PDX(base)]));
650 for(; base<e; base+=BY2PG)
651 table[PTX(base)] |= flags;
656 * Only map from KZERO to 2^32.
662 if(len > maxkpa-base)
664 pdbmap(m->pdb, base|flags, base+KZERO, len);
673 ulong cont, base, len;
678 if((s = getconf("e820")) != nil){
679 for(nemap = 0; nemap < nelem(emap); nemap++){
683 e->base = strtoull(s, &s, 16);
686 e->len = strtoull(s, &s, 16) - e->base;
687 if(*s != ' ' && *s != 0 || e->len >= 1ull<<32 || e->len == 0)
692 if(getconf("*norealmode") || getconf("*noe820scan"))
695 for(i=0; i<nelem(emap); i++){
696 memset(&u, 0, sizeof u);
701 u.es = (PADDR(RMBUF)>>4)&0xF000;
702 u.di = PADDR(RMBUF)&0xFFFF;
706 if((u.flags&Carry) || u.ax != SMAP || u.cx != 20)
718 qsort(emap, nemap, sizeof emap[0], emapcmp);
720 if(s == nil && getconf("*noe820print") == nil){
721 for(i=0; i<nemap; i++){
723 print("E820: %.8llux %.8llux ", e->base, e->base+e->len);
724 if(e->type < nelem(etypes))
725 print("%s\n", etypes[e->type]);
727 print("type=%lud\n", e->type);
732 for(i=0; i<nemap; i++){
735 * pull out the info but only about the low 32 bits...
737 if(e->base >= (1LL<<32))
740 if(base+e->len > (1LL<<32))
745 * If the map skips addresses, mark them available.
748 map(last, e->base-last, MemUPA);
750 if(e->type == Ememory)
751 map(base, len, MemRAM);
753 map(base, len, MemReserved);
756 map(last, (u32int)-last, MemUPA);
770 if(p = getconf("*maxmem"))
771 maxmem = strtoul(p, 0, 0);
776 * Set special attributes for memory between 640KB and 1MB:
777 * VGA memory is writethrough;
778 * BIOS ROM's/UMB's are uncached;
779 * then scan for useful memory.
781 for(pa = 0xA0000; pa < 0xC0000; pa += BY2PG){
782 pte = mmuwalk(m->pdb, (ulong)KADDR(pa), 2, 0);
785 for(pa = 0xC0000; pa < 0x100000; pa += BY2PG){
786 pte = mmuwalk(m->pdb, (ulong)KADDR(pa), 2, 0);
789 mmuflushtlb(PADDR(m->pdb));
797 * Set the conf entries describing banks of allocatable memory.
799 for(i=0; i<nelem(mapram) && i<nelem(conf.mem); i++){
800 mp = &rmapram.map[i];
803 cm->npage = mp->size/BY2PG;
807 for(; i<nelem(mapram); i++)
808 lost += rmapram.map[i].size;
810 print("meminit - lost %lud bytes\n", lost);
817 * Allocate memory from the upper memory blocks.
820 umbmalloc(ulong addr, int size, int align)
824 if(a = mapalloc(&rmapumb, addr, size, align))
825 return (ulong)KADDR(a);
831 umbfree(ulong addr, int size)
833 mapfree(&rmapumb, PADDR(addr), size);
837 umbrwmalloc(ulong addr, int size, int align)
842 if(a = mapalloc(&rmapumbrw, addr, size, align))
843 return(ulong)KADDR(a);
846 * Perhaps the memory wasn't visible before
847 * the interface is initialised, so try again.
849 if((a = umbmalloc(addr, size, align)) == 0)
854 if(p[0] == 0xCC && p[size-1] == 0xCC)
862 umbrwfree(ulong addr, int size)
864 mapfree(&rmapumbrw, PADDR(addr), size);
868 * Give out otherwise-unused physical address space
869 * for use in configuring devices. Note that unlike upamalloc
870 * before it, upaalloc does not map the physical address
871 * into virtual memory. Call vmap to do that.
874 upaalloc(int size, int align)
878 a = mapalloc(&rmapupa, 0, size, align);
880 print("out of physical address space allocating %d\n", size);
887 upafree(ulong pa, int size)
889 mapfree(&rmapupa, pa, size);
893 upareserve(ulong pa, int size)
897 a = mapalloc(&rmapupa, pa, size, 0);
900 * This can happen when we're using the E820
901 * map, which might have already reserved some
902 * of the regions claimed by the pci devices.
904 // print("upareserve: cannot reserve pa=%#.8lux size=%d\n", pa, size);
906 mapfree(&rmapupa, a, size);