memory.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /*
  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.
  4. */
  5. #include "u.h"
  6. #include "lib.h"
  7. #include "mem.h"
  8. #include "dat.h"
  9. #include "fns.h"
  10. #include "io.h"
  11. #define MEMDEBUG 0
  12. #define PDX(va) ((((ulong)(va))>>22) & 0x03FF)
  13. #define PTX(va) ((((ulong)(va))>>12) & 0x03FF)
  14. enum {
  15. MemUPA = 0, /* unbacked physical address */
  16. MemRAM = 1, /* physical memory */
  17. MemUMB = 2, /* upper memory block (<16MB) */
  18. NMemType = 3,
  19. KB = 1024,
  20. MemMinMB = 4, /* minimum physical memory (<=4MB) */
  21. MemMaxMB = 768, /* maximum physical memory to check */
  22. NMemBase = 10,
  23. };
  24. typedef struct {
  25. int size;
  26. ulong addr;
  27. } Map;
  28. typedef struct {
  29. char* name;
  30. Map* map;
  31. Map* mapend;
  32. Lock;
  33. } RMap;
  34. static Map mapupa[8];
  35. static RMap rmapupa = {
  36. "unallocated unbacked physical memory",
  37. mapupa,
  38. &mapupa[7],
  39. };
  40. static Map xmapupa[8];
  41. static RMap xrmapupa = {
  42. "unbacked physical memory",
  43. xmapupa,
  44. &xmapupa[7],
  45. };
  46. static Map mapram[8];
  47. static RMap rmapram = {
  48. "physical memory",
  49. mapram,
  50. &mapram[7],
  51. };
  52. static Map mapumb[64];
  53. static RMap rmapumb = {
  54. "upper memory block",
  55. mapumb,
  56. &mapumb[63],
  57. };
  58. static Map mapumbrw[8];
  59. static RMap rmapumbrw = {
  60. "UMB device memory",
  61. mapumbrw,
  62. &mapumbrw[7],
  63. };
  64. void
  65. memdebug(void)
  66. {
  67. Map *mp;
  68. ulong maxpa, maxpa1, maxpa2;
  69. if(MEMDEBUG == 0)
  70. return;
  71. maxpa = (nvramread(0x18)<<8)|nvramread(0x17);
  72. maxpa1 = (nvramread(0x31)<<8)|nvramread(0x30);
  73. maxpa2 = (nvramread(0x16)<<8)|nvramread(0x15);
  74. print("maxpa = %luX -> %luX, maxpa1 = %luX maxpa2 = %luX\n",
  75. maxpa, MB+maxpa*KB, maxpa1, maxpa2);
  76. for(mp = rmapram.map; mp->size; mp++)
  77. print("%8.8luX %8.8luX %8.8luX\n", mp->addr, (ulong)mp->size, mp->addr+mp->size);
  78. for(mp = rmapumb.map; mp->size; mp++)
  79. print("%8.8luX %8.8luX %8.8luX\n", mp->addr, (ulong)mp->size, mp->addr+mp->size);
  80. for(mp = rmapumbrw.map; mp->size; mp++)
  81. print("%8.8luX %8.8luX %8.8luX\n", mp->addr, (ulong)mp->size, mp->addr+mp->size);
  82. for(mp = rmapupa.map; mp->size; mp++)
  83. print("%8.8luX %8.8luX %8.8luX\n", mp->addr, (ulong)mp->size, mp->addr+mp->size);
  84. }
  85. void
  86. mapfree(RMap* rmap, ulong addr, ulong size)
  87. {
  88. Map *mp;
  89. ulong t;
  90. if(size == 0)
  91. return;
  92. lock(rmap);
  93. for(mp = rmap->map; mp->addr <= addr && mp->size; mp++)
  94. ;
  95. if(mp > rmap->map && (mp-1)->addr+(mp-1)->size == addr){
  96. (mp-1)->size += size;
  97. if(addr+size == mp->addr){
  98. (mp-1)->size += mp->size;
  99. while(mp->size){
  100. mp++;
  101. (mp-1)->addr = mp->addr;
  102. (mp-1)->size = mp->size;
  103. }
  104. }
  105. }
  106. else{
  107. if(addr+size == mp->addr && mp->size){
  108. mp->addr -= size;
  109. mp->size += size;
  110. }
  111. else do{
  112. if(mp >= rmap->mapend){
  113. print("mapfree: %s: losing 0x%luX, %lud\n",
  114. rmap->name, addr, size);
  115. break;
  116. }
  117. t = mp->addr;
  118. mp->addr = addr;
  119. addr = t;
  120. t = mp->size;
  121. mp->size = size;
  122. mp++;
  123. }while(size = t);
  124. }
  125. unlock(rmap);
  126. }
  127. ulong
  128. mapalloc(RMap* rmap, ulong addr, int size, int align)
  129. {
  130. Map *mp;
  131. ulong maddr, oaddr;
  132. lock(rmap);
  133. for(mp = rmap->map; mp->size; mp++){
  134. maddr = mp->addr;
  135. if(addr){
  136. /*
  137. * A specific address range has been given:
  138. * if the current map entry is greater then
  139. * the address is not in the map;
  140. * if the current map entry does not overlap
  141. * the beginning of the requested range then
  142. * continue on to the next map entry;
  143. * if the current map entry does not entirely
  144. * contain the requested range then the range
  145. * is not in the map.
  146. */
  147. if(maddr > addr)
  148. break;
  149. if(mp->size < addr - maddr) /* maddr+mp->size < addr, but no overflow */
  150. continue;
  151. if(addr - maddr > mp->size - size) /* addr+size > maddr+mp->size, but no overflow */
  152. break;
  153. maddr = addr;
  154. }
  155. if(align > 0)
  156. maddr = ((maddr+align-1)/align)*align;
  157. if(mp->addr+mp->size-maddr < size)
  158. continue;
  159. oaddr = mp->addr;
  160. mp->addr = maddr+size;
  161. mp->size -= maddr-oaddr+size;
  162. if(mp->size == 0){
  163. do{
  164. mp++;
  165. (mp-1)->addr = mp->addr;
  166. }while((mp-1)->size = mp->size);
  167. }
  168. unlock(rmap);
  169. if(oaddr != maddr)
  170. mapfree(rmap, oaddr, maddr-oaddr);
  171. return maddr;
  172. }
  173. unlock(rmap);
  174. return 0;
  175. }
  176. static void
  177. umbscan(void)
  178. {
  179. uchar *p;
  180. /*
  181. * Scan the Upper Memory Blocks (0xA0000->0xF0000) for pieces
  182. * which aren't used; they can be used later for devices which
  183. * want to allocate some virtual address space.
  184. * Check for two things:
  185. * 1) device BIOS ROM. This should start with a two-byte header
  186. * of 0x55 0xAA, followed by a byte giving the size of the ROM
  187. * in 512-byte chunks. These ROM's must start on a 2KB boundary.
  188. * 2) device memory. This is read-write.
  189. * There are some assumptions: there's VGA memory at 0xA0000 and
  190. * the VGA BIOS ROM is at 0xC0000. Also, if there's no ROM signature
  191. * at 0xE0000 then the whole 64KB up to 0xF0000 is theoretically up
  192. * for grabs; check anyway.
  193. */
  194. p = KADDR(0xD0000); /*RSC: changed from 0xC0000 */
  195. while(p < (uchar*)KADDR(0xE0000)){
  196. if (p[0] == 0x55 && p[1] == 0xAA) {
  197. /* Skip p[2] chunks of 512 bytes. Test for 0x55 AA before
  198. poking obtrusively, or else the Thinkpad X20 dies when
  199. setting up the cardbus (PB) */
  200. p += p[2] * 512;
  201. continue;
  202. }
  203. p[0] = 0xCC;
  204. p[2*KB-1] = 0xCC;
  205. if(p[0] != 0xCC || p[2*KB-1] != 0xCC){
  206. p[0] = 0x55;
  207. p[1] = 0xAA;
  208. p[2] = 4;
  209. if(p[0] == 0x55 && p[1] == 0xAA){
  210. p += p[2]*512;
  211. continue;
  212. }
  213. if(p[0] == 0xFF && p[1] == 0xFF)
  214. mapfree(&rmapumb, PADDR(p), 2*KB);
  215. }
  216. else
  217. mapfree(&rmapumbrw, PADDR(p), 2*KB);
  218. p += 2*KB;
  219. }
  220. p = KADDR(0xE0000);
  221. if(p[0] != 0x55 || p[1] != 0xAA){
  222. p[0] = 0xCC;
  223. p[64*KB-1] = 0xCC;
  224. if(p[0] != 0xCC && p[64*KB-1] != 0xCC)
  225. mapfree(&rmapumb, PADDR(p), 64*KB);
  226. }
  227. }
  228. void
  229. meminit(ulong)
  230. {
  231. /* A hack to initialize unbacked physical memory. It's assumed PCI space is assigned by
  232. the BIOS in the 0xF0000000 range and 9load never needs more than 0x2000... to run. These
  233. values leave ample space for memory allocations for uninitialized PCI cards (e.g. cardbus
  234. cards). (pb) */
  235. ulong maxmem = 0x40000000;
  236. umbscan();
  237. mapfree(&rmapupa, maxmem, 0x00000000-maxmem);
  238. if(MEMDEBUG)
  239. memdebug();
  240. }
  241. ulong
  242. umbmalloc(ulong addr, int size, int align)
  243. {
  244. ulong a;
  245. if(a = mapalloc(&rmapumb, addr, size, align))
  246. return (ulong)KADDR(a);
  247. return 0;
  248. }
  249. void
  250. umbfree(ulong addr, int size)
  251. {
  252. mapfree(&rmapumb, PADDR(addr), size);
  253. }
  254. ulong
  255. umbrwmalloc(ulong addr, int size, int align)
  256. {
  257. ulong a;
  258. uchar *p;
  259. if(a = mapalloc(&rmapumbrw, addr, size, align))
  260. return(ulong)KADDR(a);
  261. /*
  262. * Perhaps the memory wasn't visible before
  263. * the interface is initialised, so try again.
  264. */
  265. if((a = umbmalloc(addr, size, align)) == 0)
  266. return 0;
  267. p = (uchar*)a;
  268. p[0] = 0xCC;
  269. p[size-1] = 0xCC;
  270. if(p[0] == 0xCC && p[size-1] == 0xCC)
  271. return a;
  272. umbfree(a, size);
  273. return 0;
  274. }
  275. void
  276. umbrwfree(ulong addr, int size)
  277. {
  278. mapfree(&rmapumbrw, PADDR(addr), size);
  279. }
  280. ulong*
  281. mmuwalk(ulong* pdb, ulong va, int level, int create)
  282. {
  283. ulong pa, *table;
  284. /*
  285. * Walk the page-table pointed to by pdb and return a pointer
  286. * to the entry for virtual address va at the requested level.
  287. * If the entry is invalid and create isn't requested then bail
  288. * out early. Otherwise, for the 2nd level walk, allocate a new
  289. * page-table page and register it in the 1st level.
  290. */
  291. table = &pdb[PDX(va)];
  292. if(!(*table & PTEVALID) && create == 0)
  293. return 0;
  294. switch(level){
  295. default:
  296. return 0;
  297. case 1:
  298. return table;
  299. case 2:
  300. if(*table & PTESIZE)
  301. panic("mmuwalk2: va 0x%ux entry 0x%ux\n", va, *table);
  302. if(!(*table & PTEVALID)){
  303. pa = PADDR(ialloc(BY2PG, BY2PG));
  304. *table = pa|PTEWRITE|PTEVALID;
  305. }
  306. table = KADDR(PPN(*table));
  307. return &table[PTX(va)];
  308. }
  309. }
  310. static Lock mmukmaplock;
  311. ulong
  312. mmukmap(ulong pa, ulong va, int size)
  313. {
  314. ulong pae, *table, *pdb, pgsz, *pte, x;
  315. int pse, sync;
  316. extern int cpuidax, cpuiddx;
  317. pdb = KADDR(getcr3());
  318. if((cpuiddx & 0x08) && (getcr4() & 0x10))
  319. pse = 1;
  320. else
  321. pse = 0;
  322. sync = 0;
  323. pa = PPN(pa);
  324. if(va == 0)
  325. va = (ulong)KADDR(pa);
  326. else
  327. va = PPN(va);
  328. pae = pa + size;
  329. lock(&mmukmaplock);
  330. while(pa < pae){
  331. table = &pdb[PDX(va)];
  332. /*
  333. * Possibly already mapped.
  334. */
  335. if(*table & PTEVALID){
  336. if(*table & PTESIZE){
  337. /*
  338. * Big page. Does it fit within?
  339. * If it does, adjust pgsz so the correct end can be
  340. * returned and get out.
  341. * If not, adjust pgsz up to the next 4MB boundary
  342. * and continue.
  343. */
  344. x = PPN(*table);
  345. if(x != pa)
  346. panic("mmukmap1: pa 0x%ux entry 0x%ux\n",
  347. pa, *table);
  348. x += 4*MB;
  349. if(pae <= x){
  350. pa = pae;
  351. break;
  352. }
  353. pgsz = x - pa;
  354. pa += pgsz;
  355. va += pgsz;
  356. continue;
  357. }
  358. else{
  359. /*
  360. * Little page. Walk to the entry.
  361. * If the entry is valid, set pgsz and continue.
  362. * If not, make it so, set pgsz, sync and continue.
  363. */
  364. pte = mmuwalk(pdb, va, 2, 0);
  365. if(pte && *pte & PTEVALID){
  366. x = PPN(*pte);
  367. if(x != pa)
  368. panic("mmukmap2: pa 0x%ux entry 0x%ux\n",
  369. pa, *pte);
  370. pgsz = BY2PG;
  371. pa += pgsz;
  372. va += pgsz;
  373. sync++;
  374. continue;
  375. }
  376. }
  377. }
  378. /*
  379. * Not mapped. Check if it can be mapped using a big page -
  380. * starts on a 4MB boundary, size >= 4MB and processor can do it.
  381. * If not a big page, walk the walk, talk the talk.
  382. * Sync is set.
  383. */
  384. if(pse && (pa % (4*MB)) == 0 && (pae >= pa+4*MB)){
  385. *table = pa|PTESIZE|PTEWRITE|PTEUNCACHED|PTEVALID;
  386. pgsz = 4*MB;
  387. }
  388. else{
  389. pte = mmuwalk(pdb, va, 2, 1);
  390. *pte = pa|PTEWRITE|PTEUNCACHED|PTEVALID;
  391. pgsz = BY2PG;
  392. }
  393. pa += pgsz;
  394. va += pgsz;
  395. sync++;
  396. }
  397. unlock(&mmukmaplock);
  398. /*
  399. * If something was added
  400. * then need to sync up.
  401. */
  402. if(sync)
  403. putcr3(PADDR(pdb));
  404. return pa;
  405. }
  406. ulong
  407. upamalloc(ulong addr, int size, int align)
  408. {
  409. ulong ae, a;
  410. USED(align);
  411. if((a = mapalloc(&rmapupa, addr, size, align)) == 0){
  412. memdebug();
  413. return 0;
  414. }
  415. /*
  416. * This is a travesty, but they all are.
  417. */
  418. ae = mmukmap(a, 0, size);
  419. /*
  420. * Should check here that it was all delivered
  421. * and put it back and barf if not.
  422. */
  423. USED(ae);
  424. /*
  425. * Be very careful this returns a PHYSICAL address.
  426. */
  427. return a;
  428. }
  429. void
  430. upafree(ulong pa, int size)
  431. {
  432. USED(pa, size);
  433. }