mmu.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. /*
  2. * Memory mappings. Life was easier when 2G of memory was enough.
  3. *
  4. * The kernel memory starts at KZERO, with the text loaded at KZERO+1M
  5. * (9load sits under 1M during the load). The memory from KZERO to the
  6. * top of memory is mapped 1-1 with physical memory, starting at physical
  7. * address 0. All kernel memory and data structures (i.e., the entries stored
  8. * into conf.mem) must sit in this physical range: if KZERO is at 0xF0000000,
  9. * then the kernel can only have 256MB of memory for itself.
  10. *
  11. * The 256M below KZERO comprises three parts. The lowest 4M is the
  12. * virtual page table, a virtual address representation of the current
  13. * page table tree. The second 4M is used for temporary per-process
  14. * mappings managed by kmap and kunmap. The remaining 248M is used
  15. * for global (shared by all procs and all processors) device memory
  16. * mappings and managed by vmap and vunmap. The total amount (256M)
  17. * could probably be reduced somewhat if desired. The largest device
  18. * mapping is that of the video card, and even though modern video cards
  19. * have embarrassing amounts of memory, the video drivers only use one
  20. * frame buffer worth (at most 16M). Each is described in more detail below.
  21. *
  22. * The VPT is a 4M frame constructed by inserting the pdb into itself.
  23. * This short-circuits one level of the page tables, with the result that
  24. * the contents of second-level page tables can be accessed at VPT.
  25. * We use the VPT to edit the page tables (see mmu) after inserting them
  26. * into the page directory. It is a convenient mechanism for mapping what
  27. * might be otherwise-inaccessible pages. The idea was borrowed from
  28. * the Exokernel.
  29. *
  30. * The VPT doesn't solve all our problems, because we still need to
  31. * prepare page directories before we can install them. For that, we
  32. * use tmpmap/tmpunmap, which map a single page at TMPADDR.
  33. */
  34. #include "u.h"
  35. #include "../port/lib.h"
  36. #include "mem.h"
  37. #include "dat.h"
  38. #include "fns.h"
  39. #include "io.h"
  40. /*
  41. * Simple segment descriptors with no translation.
  42. */
  43. #define DATASEGM(p) { 0xFFFF, SEGG|SEGB|(0xF<<16)|SEGP|SEGPL(p)|SEGDATA|SEGW }
  44. #define EXECSEGM(p) { 0xFFFF, SEGG|SEGD|(0xF<<16)|SEGP|SEGPL(p)|SEGEXEC|SEGR }
  45. #define EXEC16SEGM(p) { 0xFFFF, SEGG|(0xF<<16)|SEGP|SEGPL(p)|SEGEXEC|SEGR }
  46. #define TSSSEGM(b,p) { ((b)<<16)|sizeof(Tss),\
  47. ((b)&0xFF000000)|(((b)>>16)&0xFF)|SEGTSS|SEGPL(p)|SEGP }
  48. Segdesc gdt[NGDT] =
  49. {
  50. [NULLSEG] { 0, 0}, /* null descriptor */
  51. [KDSEG] DATASEGM(0), /* kernel data/stack */
  52. [KESEG] EXECSEGM(0), /* kernel code */
  53. [UDSEG] DATASEGM(3), /* user data/stack */
  54. [UESEG] EXECSEGM(3), /* user code */
  55. [TSSSEG] TSSSEGM(0,0), /* tss segment */
  56. [KESEG16] EXEC16SEGM(0), /* kernel code 16-bit */
  57. };
  58. static int didmmuinit;
  59. static void taskswitch(ulong, ulong);
  60. static void memglobal(void);
  61. #define vpt ((ulong*)VPT)
  62. #define VPTX(va) (((ulong)(va))>>12)
  63. #define vpd (vpt+VPTX(VPT))
  64. void
  65. mmuinit0(void)
  66. {
  67. memmove(m->gdt, gdt, sizeof gdt);
  68. }
  69. void
  70. mmuinit(void)
  71. {
  72. ulong x, *p;
  73. ushort ptr[3];
  74. didmmuinit = 1;
  75. if(0) print("vpt=%#.8ux vpd=%#.8lux kmap=%#.8ux\n",
  76. VPT, (ulong)vpd, KMAP);
  77. memglobal();
  78. m->pdb[PDX(VPT)] = PADDR(m->pdb)|PTEWRITE|PTEVALID;
  79. m->tss = malloc(sizeof(Tss));
  80. memset(m->tss, 0, sizeof(Tss));
  81. m->tss->iomap = 0xDFFF<<16;
  82. /*
  83. * We used to keep the GDT in the Mach structure, but it
  84. * turns out that that slows down access to the rest of the
  85. * page. Since the Mach structure is accessed quite often,
  86. * it pays off anywhere from a factor of 1.25 to 2 on real
  87. * hardware to separate them (the AMDs are more sensitive
  88. * than Intels in this regard). Under VMware it pays off
  89. * a factor of about 10 to 100.
  90. */
  91. memmove(m->gdt, gdt, sizeof gdt);
  92. x = (ulong)m->tss;
  93. m->gdt[TSSSEG].d0 = (x<<16)|sizeof(Tss);
  94. m->gdt[TSSSEG].d1 = (x&0xFF000000)|((x>>16)&0xFF)|SEGTSS|SEGPL(0)|SEGP;
  95. ptr[0] = sizeof(gdt)-1;
  96. x = (ulong)m->gdt;
  97. ptr[1] = x & 0xFFFF;
  98. ptr[2] = (x>>16) & 0xFFFF;
  99. lgdt(ptr);
  100. ptr[0] = sizeof(Segdesc)*256-1;
  101. x = IDTADDR;
  102. ptr[1] = x & 0xFFFF;
  103. ptr[2] = (x>>16) & 0xFFFF;
  104. lidt(ptr);
  105. /* make kernel text unwritable */
  106. for(x = KTZERO; x < (ulong)etext; x += BY2PG){
  107. p = mmuwalk(m->pdb, x, 2, 0);
  108. if(p == nil)
  109. panic("mmuinit");
  110. *p &= ~PTEWRITE;
  111. }
  112. taskswitch(PADDR(m->pdb), (ulong)m + BY2PG);
  113. ltr(TSSSEL);
  114. }
  115. /*
  116. * On processors that support it, we set the PTEGLOBAL bit in
  117. * page table and page directory entries that map kernel memory.
  118. * Doing this tells the processor not to bother flushing them
  119. * from the TLB when doing the TLB flush associated with a
  120. * context switch (write to CR3). Since kernel memory mappings
  121. * are never removed, this is safe. (If we ever remove kernel memory
  122. * mappings, we can do a full flush by turning off the PGE bit in CR4,
  123. * writing to CR3, and then turning the PGE bit back on.)
  124. *
  125. * See also mmukmap below.
  126. *
  127. * Processor support for the PTEGLOBAL bit is enabled in devarch.c.
  128. */
  129. static void
  130. memglobal(void)
  131. {
  132. int i, j;
  133. ulong *pde, *pte;
  134. /* only need to do this once, on bootstrap processor */
  135. if(m->machno != 0)
  136. return;
  137. if(!m->havepge)
  138. return;
  139. pde = m->pdb;
  140. for(i=PDX(KZERO); i<1024; i++){
  141. if(pde[i] & PTEVALID){
  142. pde[i] |= PTEGLOBAL;
  143. if(!(pde[i] & PTESIZE)){
  144. pte = KADDR(pde[i]&~(BY2PG-1));
  145. for(j=0; j<1024; j++)
  146. if(pte[j] & PTEVALID)
  147. pte[j] |= PTEGLOBAL;
  148. }
  149. }
  150. }
  151. }
  152. /*
  153. * Flush all the user-space and device-mapping mmu info
  154. * for this process, because something has been deleted.
  155. * It will be paged back in on demand.
  156. */
  157. void
  158. flushmmu(void)
  159. {
  160. int s;
  161. s = splhi();
  162. up->newtlb = 1;
  163. mmuswitch(up);
  164. splx(s);
  165. }
  166. /*
  167. * Flush a single page mapping from the tlb.
  168. */
  169. void
  170. flushpg(ulong va)
  171. {
  172. if(X86FAMILY(m->cpuidax) >= 4)
  173. invlpg(va);
  174. else
  175. putcr3(getcr3());
  176. }
  177. /*
  178. * Allocate a new page for a page directory.
  179. * We keep a small cache of pre-initialized
  180. * page directories in each mach.
  181. */
  182. static Page*
  183. mmupdballoc(void)
  184. {
  185. int s;
  186. Page *page;
  187. ulong *pdb;
  188. s = splhi();
  189. m->pdballoc++;
  190. if(m->pdbpool == 0){
  191. spllo();
  192. page = newpage(0, 0, 0);
  193. page->va = (ulong)vpd;
  194. splhi();
  195. pdb = tmpmap(page);
  196. memmove(pdb, m->pdb, BY2PG);
  197. pdb[PDX(VPT)] = page->pa|PTEWRITE|PTEVALID; /* set up VPT */
  198. tmpunmap(pdb);
  199. }else{
  200. page = m->pdbpool;
  201. m->pdbpool = page->next;
  202. m->pdbcnt--;
  203. }
  204. splx(s);
  205. return page;
  206. }
  207. static void
  208. mmupdbfree(Proc *proc, Page *p)
  209. {
  210. if(islo())
  211. panic("mmupdbfree: islo");
  212. m->pdbfree++;
  213. if(m->pdbcnt >= 10){
  214. p->next = proc->mmufree;
  215. proc->mmufree = p;
  216. }else{
  217. p->next = m->pdbpool;
  218. m->pdbpool = p;
  219. m->pdbcnt++;
  220. }
  221. }
  222. /*
  223. * A user-space memory segment has been deleted, or the
  224. * process is exiting. Clear all the pde entries for user-space
  225. * memory mappings and device mappings. Any entries that
  226. * are needed will be paged back in as necessary.
  227. */
  228. static void
  229. mmuptefree(Proc* proc)
  230. {
  231. int s;
  232. ulong *pdb;
  233. Page **last, *page;
  234. if(proc->mmupdb == nil || proc->mmuused == nil)
  235. return;
  236. s = splhi();
  237. pdb = tmpmap(proc->mmupdb);
  238. last = &proc->mmuused;
  239. for(page = *last; page; page = page->next){
  240. pdb[page->daddr] = 0;
  241. last = &page->next;
  242. }
  243. tmpunmap(pdb);
  244. splx(s);
  245. *last = proc->mmufree;
  246. proc->mmufree = proc->mmuused;
  247. proc->mmuused = 0;
  248. }
  249. static void
  250. taskswitch(ulong pdb, ulong stack)
  251. {
  252. Tss *tss;
  253. tss = m->tss;
  254. tss->ss0 = KDSEL;
  255. tss->esp0 = stack;
  256. tss->ss1 = KDSEL;
  257. tss->esp1 = stack;
  258. tss->ss2 = KDSEL;
  259. tss->esp2 = stack;
  260. putcr3(pdb);
  261. }
  262. void
  263. mmuswitch(Proc* proc)
  264. {
  265. ulong *pdb;
  266. if(proc->newtlb){
  267. mmuptefree(proc);
  268. proc->newtlb = 0;
  269. }
  270. if(proc->mmupdb){
  271. pdb = tmpmap(proc->mmupdb);
  272. pdb[PDX(MACHADDR)] = m->pdb[PDX(MACHADDR)];
  273. tmpunmap(pdb);
  274. taskswitch(proc->mmupdb->pa, (ulong)(proc->kstack+KSTACK));
  275. }else
  276. taskswitch(PADDR(m->pdb), (ulong)(proc->kstack+KSTACK));
  277. }
  278. /*
  279. * Release any pages allocated for a page directory base or page-tables
  280. * for this process:
  281. * switch to the prototype pdb for this processor (m->pdb);
  282. * call mmuptefree() to place all pages used for page-tables (proc->mmuused)
  283. * onto the process' free list (proc->mmufree). This has the side-effect of
  284. * cleaning any user entries in the pdb (proc->mmupdb);
  285. * if there's a pdb put it in the cache of pre-initialised pdb's
  286. * for this processor (m->pdbpool) or on the process' free list;
  287. * finally, place any pages freed back into the free pool (palloc).
  288. * This routine is only called from schedinit() with palloc locked.
  289. */
  290. void
  291. mmurelease(Proc* proc)
  292. {
  293. Page *page, *next;
  294. ulong *pdb;
  295. if(islo())
  296. panic("mmurelease: islo");
  297. taskswitch(PADDR(m->pdb), (ulong)m + BY2PG);
  298. if(proc->kmaptable){
  299. if(proc->mmupdb == nil)
  300. panic("mmurelease: no mmupdb");
  301. if(--proc->kmaptable->ref)
  302. panic("mmurelease: kmap ref %d\n", proc->kmaptable->ref);
  303. if(proc->nkmap)
  304. panic("mmurelease: nkmap %d\n", proc->nkmap);
  305. /*
  306. * remove kmaptable from pdb before putting pdb up for reuse.
  307. */
  308. pdb = tmpmap(proc->mmupdb);
  309. if(PPN(pdb[PDX(KMAP)]) != proc->kmaptable->pa)
  310. panic("mmurelease: bad kmap pde %#.8lux kmap %#.8lux",
  311. pdb[PDX(KMAP)], proc->kmaptable->pa);
  312. pdb[PDX(KMAP)] = 0;
  313. tmpunmap(pdb);
  314. /*
  315. * move kmaptable to free list.
  316. */
  317. pagechainhead(proc->kmaptable);
  318. proc->kmaptable = 0;
  319. }
  320. if(proc->mmupdb){
  321. mmuptefree(proc);
  322. mmupdbfree(proc, proc->mmupdb);
  323. proc->mmupdb = 0;
  324. }
  325. for(page = proc->mmufree; page; page = next){
  326. next = page->next;
  327. if(--page->ref)
  328. panic("mmurelease: page->ref %d\n", page->ref);
  329. pagechainhead(page);
  330. }
  331. if(proc->mmufree && palloc.r.p)
  332. wakeup(&palloc.r);
  333. proc->mmufree = 0;
  334. }
  335. /*
  336. * Allocate and install pdb for the current process.
  337. */
  338. static void
  339. upallocpdb(void)
  340. {
  341. int s;
  342. ulong *pdb;
  343. Page *page;
  344. if(up->mmupdb != nil)
  345. return;
  346. page = mmupdballoc();
  347. s = splhi();
  348. if(up->mmupdb != nil){
  349. /*
  350. * Perhaps we got an interrupt while
  351. * mmupdballoc was sleeping and that
  352. * interrupt allocated an mmupdb?
  353. * Seems unlikely.
  354. */
  355. mmupdbfree(up, page);
  356. splx(s);
  357. return;
  358. }
  359. pdb = tmpmap(page);
  360. pdb[PDX(MACHADDR)] = m->pdb[PDX(MACHADDR)];
  361. tmpunmap(pdb);
  362. up->mmupdb = page;
  363. putcr3(up->mmupdb->pa);
  364. splx(s);
  365. }
  366. /*
  367. * Update the mmu in response to a user fault. pa may have PTEWRITE set.
  368. */
  369. void
  370. putmmu(ulong va, ulong pa, Page*)
  371. {
  372. int old, s;
  373. Page *page;
  374. if(up->mmupdb == nil)
  375. upallocpdb();
  376. /*
  377. * We should be able to get through this with interrupts
  378. * turned on (if we get interrupted we'll just pick up
  379. * where we left off) but we get many faults accessing
  380. * vpt[] near the end of this function, and they always happen
  381. * after the process has been switched out and then
  382. * switched back, usually many times in a row (perhaps
  383. * it cannot switch back successfully for some reason).
  384. *
  385. * In any event, I'm tired of searching for this bug.
  386. * Turn off interrupts during putmmu even though
  387. * we shouldn't need to. - rsc
  388. */
  389. s = splhi();
  390. if(!(vpd[PDX(va)]&PTEVALID)){
  391. if(up->mmufree == 0){
  392. spllo();
  393. page = newpage(0, 0, 0);
  394. splhi();
  395. }
  396. else{
  397. page = up->mmufree;
  398. up->mmufree = page->next;
  399. }
  400. vpd[PDX(va)] = PPN(page->pa)|PTEUSER|PTEWRITE|PTEVALID;
  401. /* page is now mapped into the VPT - clear it */
  402. memset((void*)(VPT+PDX(va)*BY2PG), 0, BY2PG);
  403. page->daddr = PDX(va);
  404. page->next = up->mmuused;
  405. up->mmuused = page;
  406. }
  407. old = vpt[VPTX(va)];
  408. vpt[VPTX(va)] = pa|PTEUSER|PTEVALID;
  409. if(old&PTEVALID)
  410. flushpg(va);
  411. if(getcr3() != up->mmupdb->pa)
  412. print("bad cr3 %.8lux %.8lux\n", getcr3(), up->mmupdb->pa);
  413. splx(s);
  414. }
  415. /*
  416. * Double-check the user MMU.
  417. * Error checking only.
  418. */
  419. void
  420. checkmmu(ulong va, ulong pa)
  421. {
  422. if(up->mmupdb == 0)
  423. return;
  424. if(!(vpd[PDX(va)]&PTEVALID) || !(vpt[VPTX(va)]&PTEVALID))
  425. return;
  426. if(PPN(vpt[VPTX(va)]) != pa)
  427. print("%ld %s: va=0x%08lux pa=0x%08lux pte=0x%08lux\n",
  428. up->pid, up->text,
  429. va, pa, vpt[VPTX(va)]);
  430. }
  431. /*
  432. * Walk the page-table pointed to by pdb and return a pointer
  433. * to the entry for virtual address va at the requested level.
  434. * If the entry is invalid and create isn't requested then bail
  435. * out early. Otherwise, for the 2nd level walk, allocate a new
  436. * page-table page and register it in the 1st level. This is used
  437. * only to edit kernel mappings, which use pages from kernel memory,
  438. * so it's okay to use KADDR to look at the tables.
  439. */
  440. ulong*
  441. mmuwalk(ulong* pdb, ulong va, int level, int create)
  442. {
  443. ulong *table;
  444. void *map;
  445. table = &pdb[PDX(va)];
  446. if(!(*table & PTEVALID) && create == 0)
  447. return 0;
  448. switch(level){
  449. default:
  450. return 0;
  451. case 1:
  452. return table;
  453. case 2:
  454. if(*table & PTESIZE)
  455. panic("mmuwalk2: va %luX entry %luX\n", va, *table);
  456. if(!(*table & PTEVALID)){
  457. /*
  458. * Have to call low-level allocator from
  459. * memory.c if we haven't set up the xalloc
  460. * tables yet.
  461. */
  462. if(didmmuinit)
  463. map = xspanalloc(BY2PG, BY2PG, 0);
  464. else
  465. map = rampage();
  466. if(map == nil)
  467. panic("mmuwalk xspanalloc failed");
  468. *table = PADDR(map)|PTEWRITE|PTEVALID;
  469. }
  470. table = KADDR(PPN(*table));
  471. return &table[PTX(va)];
  472. }
  473. }
  474. /*
  475. * Device mappings are shared by all procs and processors and
  476. * live in the virtual range VMAP to VMAP+VMAPSIZE. The master
  477. * copy of the mappings is stored in mach0->pdb, and they are
  478. * paged in from there as necessary by vmapsync during faults.
  479. */
  480. static Lock vmaplock;
  481. static int findhole(ulong *a, int n, int count);
  482. static ulong vmapalloc(ulong size);
  483. static void pdbunmap(ulong*, ulong, int);
  484. /*
  485. * Add a device mapping to the vmap range.
  486. */
  487. void*
  488. vmap(ulong pa, int size)
  489. {
  490. int osize;
  491. ulong o, va;
  492. /*
  493. * might be asking for less than a page.
  494. */
  495. osize = size;
  496. o = pa & (BY2PG-1);
  497. pa -= o;
  498. size += o;
  499. size = ROUND(size, BY2PG);
  500. if(pa == 0){
  501. print("vmap pa=0 pc=%#.8lux\n", getcallerpc(&pa));
  502. return nil;
  503. }
  504. ilock(&vmaplock);
  505. if((va = vmapalloc(size)) == 0
  506. || pdbmap(MACHP(0)->pdb, pa|PTEUNCACHED|PTEWRITE, va, size) < 0){
  507. iunlock(&vmaplock);
  508. return 0;
  509. }
  510. iunlock(&vmaplock);
  511. /* avoid trap on local processor
  512. for(i=0; i<size; i+=4*MB)
  513. vmapsync(va+i);
  514. */
  515. USED(osize);
  516. // print(" vmap %#.8lux %d => %#.8lux\n", pa+o, osize, va+o);
  517. return (void*)(va + o);
  518. }
  519. static int
  520. findhole(ulong *a, int n, int count)
  521. {
  522. int have, i;
  523. have = 0;
  524. for(i=0; i<n; i++){
  525. if(a[i] == 0)
  526. have++;
  527. else
  528. have = 0;
  529. if(have >= count)
  530. return i+1 - have;
  531. }
  532. return -1;
  533. }
  534. /*
  535. * Look for free space in the vmap.
  536. */
  537. static ulong
  538. vmapalloc(ulong size)
  539. {
  540. int i, n, o;
  541. ulong *vpdb;
  542. int vpdbsize;
  543. vpdb = &MACHP(0)->pdb[PDX(VMAP)];
  544. vpdbsize = VMAPSIZE/(4*MB);
  545. if(size >= 4*MB){
  546. n = (size+4*MB-1) / (4*MB);
  547. if((o = findhole(vpdb, vpdbsize, n)) != -1)
  548. return VMAP + o*4*MB;
  549. return 0;
  550. }
  551. n = (size+BY2PG-1) / BY2PG;
  552. for(i=0; i<vpdbsize; i++)
  553. if((vpdb[i]&PTEVALID) && !(vpdb[i]&PTESIZE))
  554. if((o = findhole(KADDR(PPN(vpdb[i])), WD2PG, n)) != -1)
  555. return VMAP + i*4*MB + o*BY2PG;
  556. if((o = findhole(vpdb, vpdbsize, 1)) != -1)
  557. return VMAP + o*4*MB;
  558. /*
  559. * could span page directory entries, but not worth the trouble.
  560. * not going to be very much contention.
  561. */
  562. return 0;
  563. }
  564. /*
  565. * Remove a device mapping from the vmap range.
  566. * Since pdbunmap does not remove page tables, just entries,
  567. * the call need not be interlocked with vmap.
  568. */
  569. void
  570. vunmap(void *v, int size)
  571. {
  572. int i;
  573. ulong va, o;
  574. Mach *nm;
  575. Proc *p;
  576. /*
  577. * might not be aligned
  578. */
  579. va = (ulong)v;
  580. o = va&(BY2PG-1);
  581. va -= o;
  582. size += o;
  583. size = ROUND(size, BY2PG);
  584. if(size < 0 || va < VMAP || va+size > VMAP+VMAPSIZE)
  585. panic("vunmap va=%#.8lux size=%#x pc=%#.8lux\n",
  586. va, size, getcallerpc(&va));
  587. pdbunmap(MACHP(0)->pdb, va, size);
  588. /*
  589. * Flush mapping from all the tlbs and copied pdbs.
  590. * This can be (and is) slow, since it is called only rarely.
  591. */
  592. for(i=0; i<conf.nproc; i++){
  593. p = proctab(i);
  594. if(p->state == Dead)
  595. continue;
  596. if(p != up)
  597. p->newtlb = 1;
  598. }
  599. for(i=0; i<conf.nmach; i++){
  600. nm = MACHP(i);
  601. if(nm != m)
  602. nm->flushmmu = 1;
  603. }
  604. flushmmu();
  605. for(i=0; i<conf.nmach; i++){
  606. nm = MACHP(i);
  607. if(nm != m)
  608. while((active.machs&(1<<nm->machno)) && nm->flushmmu)
  609. ;
  610. }
  611. }
  612. /*
  613. * Add kernel mappings for pa -> va for a section of size bytes.
  614. */
  615. int
  616. pdbmap(ulong *pdb, ulong pa, ulong va, int size)
  617. {
  618. int pse;
  619. ulong pgsz, *pte, *table;
  620. ulong flag, off;
  621. flag = pa&0xFFF;
  622. pa &= ~0xFFF;
  623. if((MACHP(0)->cpuiddx & 0x08) && (getcr4() & 0x10))
  624. pse = 1;
  625. else
  626. pse = 0;
  627. for(off=0; off<size; off+=pgsz){
  628. table = &pdb[PDX(va+off)];
  629. if((*table&PTEVALID) && (*table&PTESIZE))
  630. panic("vmap: va=%#.8lux pa=%#.8lux pde=%#.8lux",
  631. va+off, pa+off, *table);
  632. /*
  633. * Check if it can be mapped using a 4MB page:
  634. * va, pa aligned and size >= 4MB and processor can do it.
  635. */
  636. if(pse && (pa+off)%(4*MB) == 0 && (va+off)%(4*MB) == 0 && (size-off) >= 4*MB){
  637. *table = (pa+off)|flag|PTESIZE|PTEVALID;
  638. pgsz = 4*MB;
  639. }else{
  640. pte = mmuwalk(pdb, va+off, 2, 1);
  641. if(*pte&PTEVALID)
  642. panic("vmap: va=%#.8lux pa=%#.8lux pte=%#.8lux",
  643. va+off, pa+off, *pte);
  644. *pte = (pa+off)|flag|PTEVALID;
  645. pgsz = BY2PG;
  646. }
  647. }
  648. return 0;
  649. }
  650. /*
  651. * Remove mappings. Must already exist, for sanity.
  652. * Only used for kernel mappings, so okay to use KADDR.
  653. */
  654. static void
  655. pdbunmap(ulong *pdb, ulong va, int size)
  656. {
  657. ulong vae;
  658. ulong *table;
  659. vae = va+size;
  660. while(va < vae){
  661. table = &pdb[PDX(va)];
  662. if(!(*table & PTEVALID)){
  663. panic("vunmap: not mapped");
  664. /*
  665. va = (va+4*MB-1) & ~(4*MB-1);
  666. continue;
  667. */
  668. }
  669. if(*table & PTESIZE){
  670. *table = 0;
  671. va = (va+4*MB-1) & ~(4*MB-1);
  672. continue;
  673. }
  674. table = KADDR(PPN(*table));
  675. if(!(table[PTX(va)] & PTEVALID))
  676. panic("vunmap: not mapped");
  677. table[PTX(va)] = 0;
  678. va += BY2PG;
  679. }
  680. }
  681. /*
  682. * Handle a fault by bringing vmap up to date.
  683. * Only copy pdb entries and they never go away,
  684. * so no locking needed.
  685. */
  686. int
  687. vmapsync(ulong va)
  688. {
  689. ulong entry, *table;
  690. if(va < VMAP || va >= VMAP+VMAPSIZE)
  691. return 0;
  692. entry = MACHP(0)->pdb[PDX(va)];
  693. if(!(entry&PTEVALID))
  694. return 0;
  695. if(!(entry&PTESIZE)){
  696. /* make sure entry will help the fault */
  697. table = KADDR(PPN(entry));
  698. if(!(table[PTX(va)]&PTEVALID))
  699. return 0;
  700. }
  701. vpd[PDX(va)] = entry;
  702. /*
  703. * TLB doesn't cache negative results, so no flush needed.
  704. */
  705. return 1;
  706. }
  707. /*
  708. * KMap is used to map individual pages into virtual memory.
  709. * It is rare to have more than a few KMaps at a time (in the
  710. * absence of interrupts, only two at a time are ever used,
  711. * but interrupts can stack). The mappings are local to a process,
  712. * so we can use the same range of virtual address space for
  713. * all processes without any coordination.
  714. */
  715. #define kpt (vpt+VPTX(KMAP))
  716. #define NKPT (KMAPSIZE/BY2PG)
  717. KMap*
  718. kmap(Page *page)
  719. {
  720. int i, o, s;
  721. if(up == nil)
  722. panic("kmap: up=0 pc=%#.8lux", getcallerpc(&page));
  723. if(up->mmupdb == nil)
  724. upallocpdb();
  725. if(up->nkmap < 0)
  726. panic("kmap %lud %s: nkmap=%d", up->pid, up->text, up->nkmap);
  727. /*
  728. * Splhi shouldn't be necessary here, but paranoia reigns.
  729. * See comment in putmmu above.
  730. */
  731. s = splhi();
  732. up->nkmap++;
  733. if(!(vpd[PDX(KMAP)]&PTEVALID)){
  734. /* allocate page directory */
  735. if(KMAPSIZE > BY2XPG)
  736. panic("bad kmapsize");
  737. if(up->kmaptable != nil)
  738. panic("kmaptable");
  739. spllo();
  740. up->kmaptable = newpage(0, 0, 0);
  741. splhi();
  742. vpd[PDX(KMAP)] = up->kmaptable->pa|PTEWRITE|PTEVALID;
  743. flushpg((ulong)kpt);
  744. memset(kpt, 0, BY2PG);
  745. kpt[0] = page->pa|PTEWRITE|PTEVALID;
  746. up->lastkmap = 0;
  747. splx(s);
  748. return (KMap*)KMAP;
  749. }
  750. if(up->kmaptable == nil)
  751. panic("no kmaptable");
  752. o = up->lastkmap+1;
  753. for(i=0; i<NKPT; i++){
  754. if(kpt[(i+o)%NKPT] == 0){
  755. o = (i+o)%NKPT;
  756. kpt[o] = page->pa|PTEWRITE|PTEVALID;
  757. up->lastkmap = o;
  758. splx(s);
  759. return (KMap*)(KMAP+o*BY2PG);
  760. }
  761. }
  762. panic("out of kmap");
  763. return nil;
  764. }
  765. void
  766. kunmap(KMap *k)
  767. {
  768. ulong va;
  769. va = (ulong)k;
  770. if(up->mmupdb == nil || !(vpd[PDX(KMAP)]&PTEVALID))
  771. panic("kunmap: no kmaps");
  772. if(va < KMAP || va >= KMAP+KMAPSIZE)
  773. panic("kunmap: bad address %#.8lux pc=%#.8lux", va, getcallerpc(&k));
  774. if(!(vpt[VPTX(va)]&PTEVALID))
  775. panic("kunmap: not mapped %#.8lux pc=%#.8lux", va, getcallerpc(&k));
  776. up->nkmap--;
  777. if(up->nkmap < 0)
  778. panic("kunmap %lud %s: nkmap=%d", up->pid, up->text, up->nkmap);
  779. vpt[VPTX(va)] = 0;
  780. flushpg(va);
  781. }
  782. /*
  783. * Temporary one-page mapping used to edit page directories.
  784. *
  785. * The fasttmp #define controls whether the code optimizes
  786. * the case where the page is already mapped in the physical
  787. * memory window.
  788. */
  789. #define fasttmp 1
  790. void*
  791. tmpmap(Page *p)
  792. {
  793. ulong i;
  794. ulong *entry;
  795. if(islo())
  796. panic("tmpaddr: islo");
  797. if(fasttmp && p->pa < -KZERO)
  798. return KADDR(p->pa);
  799. /*
  800. * PDX(TMPADDR) == PDX(MACHADDR), so this
  801. * entry is private to the processor and shared
  802. * between up->mmupdb (if any) and m->pdb.
  803. */
  804. entry = &vpt[VPTX(TMPADDR)];
  805. if(!(*entry&PTEVALID)){
  806. for(i=KZERO; i<=CPU0MACH; i+=BY2PG)
  807. print("%.8lux: *%.8lux=%.8lux (vpt=%.8lux index=%.8lux)\n", i, &vpt[VPTX(i)], vpt[VPTX(i)], vpt, VPTX(i));
  808. panic("tmpmap: no entry");
  809. }
  810. if(PPN(*entry) != PPN(TMPADDR-KZERO))
  811. panic("tmpmap: already mapped entry=%#.8lux", *entry);
  812. *entry = p->pa|PTEWRITE|PTEVALID;
  813. flushpg(TMPADDR);
  814. return (void*)TMPADDR;
  815. }
  816. void
  817. tmpunmap(void *v)
  818. {
  819. ulong *entry;
  820. if(islo())
  821. panic("tmpaddr: islo");
  822. if(fasttmp && (ulong)v >= KZERO && v != (void*)TMPADDR)
  823. return;
  824. if(v != (void*)TMPADDR)
  825. panic("tmpunmap: bad address");
  826. entry = &vpt[VPTX(TMPADDR)];
  827. if(!(*entry&PTEVALID) || PPN(*entry) == PPN(PADDR(TMPADDR)))
  828. panic("tmpmap: not mapped entry=%#.8lux", *entry);
  829. *entry = PPN(TMPADDR-KZERO)|PTEWRITE|PTEVALID;
  830. flushpg(TMPADDR);
  831. }
  832. /*
  833. * These could go back to being macros once the kernel is debugged,
  834. * but the extra checking is nice to have.
  835. */
  836. void*
  837. kaddr(ulong pa)
  838. {
  839. if(pa > (ulong)-KZERO)
  840. panic("kaddr: pa=%#.8lux", pa);
  841. return (void*)(pa+KZERO);
  842. }
  843. ulong
  844. paddr(void *v)
  845. {
  846. ulong va;
  847. va = (ulong)v;
  848. if(va < KZERO)
  849. panic("paddr: va=%#.8lux pc=%#.8lux", va, getcallerpc(&va));
  850. return va-KZERO;
  851. }
  852. /*
  853. * More debugging.
  854. */
  855. void
  856. countpagerefs(ulong *ref, int print)
  857. {
  858. int i, n;
  859. Mach *mm;
  860. Page *pg;
  861. Proc *p;
  862. n = 0;
  863. for(i=0; i<conf.nproc; i++){
  864. p = proctab(i);
  865. if(p->mmupdb){
  866. if(print){
  867. if(ref[pagenumber(p->mmupdb)])
  868. iprint("page %#.8lux is proc %d (pid %lud) pdb\n",
  869. p->mmupdb->pa, i, p->pid);
  870. continue;
  871. }
  872. if(ref[pagenumber(p->mmupdb)]++ == 0)
  873. n++;
  874. else
  875. iprint("page %#.8lux is proc %d (pid %lud) pdb but has other refs!\n",
  876. p->mmupdb->pa, i, p->pid);
  877. }
  878. if(p->kmaptable){
  879. if(print){
  880. if(ref[pagenumber(p->kmaptable)])
  881. iprint("page %#.8lux is proc %d (pid %lud) kmaptable\n",
  882. p->kmaptable->pa, i, p->pid);
  883. continue;
  884. }
  885. if(ref[pagenumber(p->kmaptable)]++ == 0)
  886. n++;
  887. else
  888. iprint("page %#.8lux is proc %d (pid %lud) kmaptable but has other refs!\n",
  889. p->kmaptable->pa, i, p->pid);
  890. }
  891. for(pg=p->mmuused; pg; pg=pg->next){
  892. if(print){
  893. if(ref[pagenumber(pg)])
  894. iprint("page %#.8lux is on proc %d (pid %lud) mmuused\n",
  895. pg->pa, i, p->pid);
  896. continue;
  897. }
  898. if(ref[pagenumber(pg)]++ == 0)
  899. n++;
  900. else
  901. iprint("page %#.8lux is on proc %d (pid %lud) mmuused but has other refs!\n",
  902. pg->pa, i, p->pid);
  903. }
  904. for(pg=p->mmufree; pg; pg=pg->next){
  905. if(print){
  906. if(ref[pagenumber(pg)])
  907. iprint("page %#.8lux is on proc %d (pid %lud) mmufree\n",
  908. pg->pa, i, p->pid);
  909. continue;
  910. }
  911. if(ref[pagenumber(pg)]++ == 0)
  912. n++;
  913. else
  914. iprint("page %#.8lux is on proc %d (pid %lud) mmufree but has other refs!\n",
  915. pg->pa, i, p->pid);
  916. }
  917. }
  918. if(!print)
  919. iprint("%d pages in proc mmu\n", n);
  920. n = 0;
  921. for(i=0; i<conf.nmach; i++){
  922. mm = MACHP(i);
  923. for(pg=mm->pdbpool; pg; pg=pg->next){
  924. if(print){
  925. if(ref[pagenumber(pg)])
  926. iprint("page %#.8lux is in cpu%d pdbpool\n",
  927. pg->pa, i);
  928. continue;
  929. }
  930. if(ref[pagenumber(pg)]++ == 0)
  931. n++;
  932. else
  933. iprint("page %#.8lux is in cpu%d pdbpool but has other refs!\n",
  934. pg->pa, i);
  935. }
  936. }
  937. if(!print){
  938. iprint("%d pages in mach pdbpools\n", n);
  939. for(i=0; i<conf.nmach; i++)
  940. iprint("cpu%d: %d pdballoc, %d pdbfree\n",
  941. i, MACHP(i)->pdballoc, MACHP(i)->pdbfree);
  942. }
  943. }
  944. void
  945. checkfault(ulong, ulong)
  946. {
  947. }