mmu.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  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(m->tss->cr3);
  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. if(m->pdbpool == 0){
  190. spllo();
  191. page = newpage(0, 0, 0);
  192. page->va = (ulong)vpd;
  193. splhi();
  194. pdb = tmpmap(page);
  195. memmove(pdb, m->pdb, BY2PG);
  196. pdb[PDX(VPT)] = page->pa|PTEWRITE|PTEVALID; /* set up VPT */
  197. tmpunmap(pdb);
  198. }else{
  199. page = m->pdbpool;
  200. m->pdbpool = page->next;
  201. m->pdbcnt--;
  202. }
  203. splx(s);
  204. return page;
  205. }
  206. static void
  207. mmupdbfree(Proc *proc, Page *p)
  208. {
  209. if(islo())
  210. panic("mmupdbfree: islo");
  211. if(m->pdbcnt >= 10){
  212. p->next = proc->mmufree;
  213. proc->mmufree = p;
  214. }else{
  215. p->next = m->pdbpool;
  216. m->pdbpool = p;
  217. }
  218. }
  219. /*
  220. * A user-space memory segment has been deleted, or the
  221. * process is exiting. Clear all the pde entries for user-space
  222. * memory mappings and device mappings. Any entries that
  223. * are needed will be paged back in as necessary.
  224. */
  225. static void
  226. mmuptefree(Proc* proc)
  227. {
  228. int s;
  229. ulong *pdb;
  230. Page **last, *page;
  231. if(proc->mmupdb == nil || proc->mmuused == nil)
  232. return;
  233. s = splhi();
  234. pdb = tmpmap(proc->mmupdb);
  235. last = &proc->mmuused;
  236. for(page = *last; page; page = page->next){
  237. pdb[page->daddr] = 0;
  238. last = &page->next;
  239. }
  240. tmpunmap(pdb);
  241. splx(s);
  242. *last = proc->mmufree;
  243. proc->mmufree = proc->mmuused;
  244. proc->mmuused = 0;
  245. }
  246. static void
  247. taskswitch(ulong pdb, ulong stack)
  248. {
  249. Tss *tss;
  250. tss = m->tss;
  251. tss->ss0 = KDSEL;
  252. tss->esp0 = stack;
  253. tss->ss1 = KDSEL;
  254. tss->esp1 = stack;
  255. tss->ss2 = KDSEL;
  256. tss->esp2 = stack;
  257. tss->cr3 = pdb;
  258. putcr3(pdb);
  259. }
  260. void
  261. mmuswitch(Proc* proc)
  262. {
  263. ulong *pdb;
  264. if(proc->newtlb){
  265. mmuptefree(proc);
  266. proc->newtlb = 0;
  267. }
  268. if(proc->mmupdb){
  269. pdb = tmpmap(proc->mmupdb);
  270. pdb[PDX(MACHADDR)] = m->pdb[PDX(MACHADDR)];
  271. tmpunmap(pdb);
  272. taskswitch(proc->mmupdb->pa, (ulong)(proc->kstack+KSTACK));
  273. }else
  274. taskswitch(PADDR(m->pdb), (ulong)(proc->kstack+KSTACK));
  275. }
  276. /*
  277. * Release any pages allocated for a page directory base or page-tables
  278. * for this process:
  279. * switch to the prototype pdb for this processor (m->pdb);
  280. * call mmuptefree() to place all pages used for page-tables (proc->mmuused)
  281. * onto the process' free list (proc->mmufree). This has the side-effect of
  282. * cleaning any user entries in the pdb (proc->mmupdb);
  283. * if there's a pdb put it in the cache of pre-initialised pdb's
  284. * for this processor (m->pdbpool) or on the process' free list;
  285. * finally, place any pages freed back into the free pool (palloc).
  286. * This routine is only called from sched() with palloc locked.
  287. */
  288. void
  289. mmurelease(Proc* proc)
  290. {
  291. Page *page, *next;
  292. taskswitch(PADDR(m->pdb), (ulong)m + BY2PG);
  293. if(proc->mmupdb){
  294. mmuptefree(proc);
  295. mmupdbfree(proc, proc->mmupdb);
  296. proc->mmupdb = 0;
  297. }
  298. for(page = proc->mmufree; page; page = next){
  299. next = page->next;
  300. if(--page->ref)
  301. panic("mmurelease: page->ref %d\n", page->ref);
  302. pagechainhead(page);
  303. }
  304. if(proc->mmufree && palloc.r.p)
  305. wakeup(&palloc.r);
  306. proc->mmufree = 0;
  307. }
  308. /*
  309. * Allocate and install pdb for the current process.
  310. */
  311. static void
  312. upallocpdb(void)
  313. {
  314. int s;
  315. ulong *pdb;
  316. Page *page;
  317. page = mmupdballoc();
  318. s = splhi();
  319. pdb = tmpmap(page);
  320. pdb[PDX(MACHADDR)] = m->pdb[PDX(MACHADDR)];
  321. tmpunmap(pdb);
  322. up->mmupdb = page;
  323. mmuflushtlb(up->mmupdb->pa);
  324. splx(s);
  325. }
  326. /*
  327. * Update the mmu in response to a user fault. pa may have PTEWRITE set.
  328. */
  329. void
  330. putmmu(ulong va, ulong pa, Page*)
  331. {
  332. int old;
  333. Page *page;
  334. if(up->mmupdb == nil)
  335. upallocpdb();
  336. if(!(vpd[PDX(va)]&PTEVALID)){
  337. if(up->mmufree == 0)
  338. page = newpage(0, 0, 0);
  339. else{
  340. page = up->mmufree;
  341. up->mmufree = page->next;
  342. }
  343. vpd[PDX(va)] = PPN(page->pa)|PTEUSER|PTEWRITE|PTEVALID;
  344. /* page is now mapped into the VPT - clear it */
  345. memset((void*)(VPT+PDX(va)*BY2PG), 0, BY2PG);
  346. page->daddr = PDX(va);
  347. page->next = up->mmuused;
  348. up->mmuused = page;
  349. }
  350. old = vpt[VPTX(va)];
  351. vpt[VPTX(va)] = pa|PTEUSER|PTEVALID;
  352. if(old&PTEVALID)
  353. flushpg(va);
  354. }
  355. /*
  356. * Double-check the user MMU.
  357. * Error checking only.
  358. */
  359. void
  360. checkmmu(ulong va, ulong pa)
  361. {
  362. if(up->mmupdb == 0)
  363. return;
  364. if(!(vpd[PDX(va)]&PTEVALID) || !(vpt[VPTX(va)]&PTEVALID))
  365. return;
  366. if(PPN(vpt[VPTX(va)]) != pa)
  367. print("%ld %s: va=0x%08lux pa=0x%08lux pte=0x%08lux\n",
  368. up->pid, up->text,
  369. va, pa, vpt[VPTX(va)]);
  370. }
  371. /*
  372. * Walk the page-table pointed to by pdb and return a pointer
  373. * to the entry for virtual address va at the requested level.
  374. * If the entry is invalid and create isn't requested then bail
  375. * out early. Otherwise, for the 2nd level walk, allocate a new
  376. * page-table page and register it in the 1st level. This is used
  377. * only to edit kernel mappings, which use pages from kernel memory,
  378. * so it's okay to use KADDR to look at the tables.
  379. */
  380. ulong*
  381. mmuwalk(ulong* pdb, ulong va, int level, int create)
  382. {
  383. ulong *table;
  384. void *map;
  385. table = &pdb[PDX(va)];
  386. if(!(*table & PTEVALID) && create == 0)
  387. return 0;
  388. switch(level){
  389. default:
  390. return 0;
  391. case 1:
  392. return table;
  393. case 2:
  394. if(*table & PTESIZE)
  395. panic("mmuwalk2: va %luX entry %luX\n", va, *table);
  396. if(!(*table & PTEVALID)){
  397. /*
  398. * Have to call low-level allocator from
  399. * memory.c if we haven't set up the xalloc
  400. * tables yet.
  401. */
  402. if(didmmuinit)
  403. map = xspanalloc(BY2PG, BY2PG, 0);
  404. else
  405. map = rampage();
  406. if(map == nil)
  407. panic("mmuwalk xspanalloc failed");
  408. *table = PADDR(map)|PTEWRITE|PTEVALID;
  409. }
  410. table = KADDR(PPN(*table));
  411. return &table[PTX(va)];
  412. }
  413. }
  414. /*
  415. * Device mappings are shared by all procs and processors and
  416. * live in the virtual range VMAP to VMAP+VMAPSIZE. The master
  417. * copy of the mappings is stored in mach0->pdb, and they are
  418. * paged in from there as necessary by vmapsync during faults.
  419. */
  420. static Lock vmaplock;
  421. static int findhole(ulong *a, int n, int count);
  422. static ulong vmapalloc(ulong size);
  423. static void pdbunmap(ulong*, ulong, int);
  424. /*
  425. * Add a device mapping to the vmap range.
  426. */
  427. void*
  428. vmap(ulong pa, int size)
  429. {
  430. int osize;
  431. ulong o, va;
  432. /*
  433. * might be asking for less than a page.
  434. */
  435. osize = size;
  436. o = pa & (BY2PG-1);
  437. pa -= o;
  438. size += o;
  439. size = ROUND(size, BY2PG);
  440. if(pa == 0){
  441. print("vmap pa=0 pc=%#.8lux\n", getcallerpc(&pa));
  442. return nil;
  443. }
  444. ilock(&vmaplock);
  445. if((va = vmapalloc(size)) == 0
  446. || pdbmap(MACHP(0)->pdb, pa|PTEUNCACHED|PTEWRITE, va, size) < 0){
  447. iunlock(&vmaplock);
  448. return 0;
  449. }
  450. iunlock(&vmaplock);
  451. /* avoid trap on local processor
  452. for(i=0; i<size; i+=4*MB)
  453. vmapsync(va+i);
  454. */
  455. USED(osize);
  456. // print(" vmap %#.8lux %d => %#.8lux\n", pa+o, osize, va+o);
  457. return (void*)(va + o);
  458. }
  459. static int
  460. findhole(ulong *a, int n, int count)
  461. {
  462. int have, i;
  463. have = 0;
  464. for(i=0; i<n; i++){
  465. if(a[i] == 0)
  466. have++;
  467. else
  468. have = 0;
  469. if(have >= count)
  470. return i+1 - have;
  471. }
  472. return -1;
  473. }
  474. /*
  475. * Look for free space in the vmap.
  476. */
  477. static ulong
  478. vmapalloc(ulong size)
  479. {
  480. int i, n, o;
  481. ulong *vpdb;
  482. int vpdbsize;
  483. vpdb = &MACHP(0)->pdb[PDX(VMAP)];
  484. vpdbsize = VMAPSIZE/(4*MB);
  485. if(size >= 4*MB){
  486. n = (size+4*MB-1) / (4*MB);
  487. if((o = findhole(vpdb, vpdbsize, n)) != -1)
  488. return VMAP + o*4*MB;
  489. return VMAP + o;
  490. }
  491. n = (size+BY2PG-1) / BY2PG;
  492. for(i=0; i<vpdbsize; i++)
  493. if((vpdb[i]&PTEVALID) && !(vpdb[i]&PTESIZE))
  494. if((o = findhole(KADDR(PPN(vpdb[i])), WD2PG, n)) != -1)
  495. return VMAP + i*4*MB + o*BY2PG;
  496. if((o = findhole(vpdb, vpdbsize, 1)) != -1)
  497. return VMAP + o*4*MB;
  498. /*
  499. * could span page directory entries, but not worth the trouble.
  500. * not going to be very much contention.
  501. */
  502. return 0;
  503. }
  504. /*
  505. * Remove a device mapping from the vmap range.
  506. * Since pdbunmap does not remove page tables, just entries,
  507. * the call need not be interlocked with vmap.
  508. */
  509. void
  510. vunmap(void *v, int size)
  511. {
  512. int i;
  513. ulong va, o;
  514. Mach *nm;
  515. Proc *p;
  516. /*
  517. * might not be aligned
  518. */
  519. va = (ulong)v;
  520. o = va&(BY2PG-1);
  521. va -= o;
  522. size += o;
  523. size = ROUND(size, BY2PG);
  524. if(size < 0 || va < VMAP || va+size > VMAP+VMAPSIZE)
  525. panic("vunmap va=%#.8lux size=%#x pc=%#.8lux\n",
  526. va, size, getcallerpc(&va));
  527. pdbunmap(MACHP(0)->pdb, va, size);
  528. /*
  529. * Flush mapping from all the tlbs and copied pdbs.
  530. * This can be (and is) slow, since it is called only rarely.
  531. */
  532. for(i=0; i<conf.nproc; i++){
  533. p = proctab(i);
  534. if(p->state == Dead)
  535. continue;
  536. if(p != up)
  537. p->newtlb = 1;
  538. }
  539. for(i=0; i<conf.nmach; i++){
  540. nm = MACHP(i);
  541. if(nm != m)
  542. nm->flushmmu = 1;
  543. }
  544. flushmmu();
  545. for(i=0; i<conf.nmach; i++){
  546. nm = MACHP(i);
  547. if(nm != m)
  548. while((active.machs&(1<<nm->machno)) && nm->flushmmu)
  549. ;
  550. }
  551. }
  552. /*
  553. * Add kernel mappings for pa -> va for a section of size bytes.
  554. */
  555. int
  556. pdbmap(ulong *pdb, ulong pa, ulong va, int size)
  557. {
  558. int pse;
  559. ulong pae, pgsz, *pte, *table;
  560. ulong flag;
  561. flag = pa&0xFFF;
  562. pa &= ~0xFFF;
  563. if((MACHP(0)->cpuiddx & 0x08) && (getcr4() & 0x10))
  564. pse = 1;
  565. else
  566. pse = 0;
  567. pae = pa + size;
  568. while(pa < pae){
  569. table = &pdb[PDX(va)];
  570. if((*table&PTEVALID) && (*table&PTESIZE))
  571. panic("vmap: va=%#.8lux pa=%#.8lux pde=%#.8lux",
  572. va, pa, *table);
  573. /*
  574. * Check if it can be mapped using a 4MB page:
  575. * va, pa aligned and size >= 4MB and processor can do it.
  576. */
  577. if(pse && pa%(4*MB) == 0 && va%(4*MB) == 0 && (pae >= pa+4*MB)){
  578. *table = pa|PTESIZE|flag|PTEVALID;
  579. pgsz = 4*MB;
  580. }else{
  581. pte = mmuwalk(pdb, va, 2, 1);
  582. if(*pte&PTEVALID)
  583. panic("vmap: va=%#.8lux pa=%#.8lux pte=%#.8lux",
  584. va, pa, *pte);
  585. *pte = pa|flag|PTEVALID;
  586. pgsz = BY2PG;
  587. }
  588. pa += pgsz;
  589. va += pgsz;
  590. }
  591. return 0;
  592. }
  593. /*
  594. * Remove mappings. Must already exist, for sanity.
  595. * Only used for kernel mappings, so okay to use KADDR.
  596. */
  597. static void
  598. pdbunmap(ulong *pdb, ulong va, int size)
  599. {
  600. ulong vae;
  601. ulong *table;
  602. vae = va+size;
  603. while(va < vae){
  604. table = &pdb[PDX(va)];
  605. if(!(*table & PTEVALID)){
  606. panic("vunmap: not mapped");
  607. /*
  608. va = (va+4*MB-1) & ~(4*MB-1);
  609. continue;
  610. */
  611. }
  612. if(*table & PTESIZE){
  613. *table = 0;
  614. va = (va+4*MB-1) & ~(4*MB-1);
  615. continue;
  616. }
  617. table = KADDR(PPN(*table));
  618. if(!(table[PTX(va)] & PTEVALID))
  619. panic("vunmap: not mapped");
  620. table[PTX(va)] = 0;
  621. va += BY2PG;
  622. }
  623. }
  624. /*
  625. * Handle a fault by bringing vmap up to date.
  626. * Only copy pdb entries and they never go away,
  627. * so no locking needed.
  628. */
  629. int
  630. vmapsync(ulong va)
  631. {
  632. ulong entry, *table;
  633. if(va < VMAP || va >= VMAP+VMAPSIZE)
  634. return 0;
  635. entry = MACHP(0)->pdb[PDX(va)];
  636. if(!(entry&PTEVALID))
  637. return 0;
  638. if(!(entry&PTESIZE)){
  639. /* make sure entry will help the fault */
  640. table = KADDR(PPN(entry));
  641. if(!(table[PTX(va)]&PTEVALID))
  642. return 0;
  643. }
  644. vpd[PDX(va)] = entry;
  645. /*
  646. * TLB doesn't cache negative results, so no flush needed.
  647. */
  648. return 1;
  649. }
  650. /*
  651. * KMap is used to map individual pages into virtual memory.
  652. * It is rare to have more than a few KMaps at a time (in the
  653. * absence of interrupts, only two at a time are ever used,
  654. * but interrupts can stack). The mappings are local to a process,
  655. * so we can use the same range of virtual address space for
  656. * all processes without any coordination.
  657. */
  658. #define kpt (vpt+VPTX(KMAP))
  659. #define NKPT (KMAPSIZE/BY2PG)
  660. KMap*
  661. kmap(Page *page)
  662. {
  663. int i, o, s;
  664. Page *pdb;
  665. if(up == nil)
  666. panic("kmap: up=0 pc=%#.8lux", getcallerpc(&page));
  667. if(up->mmupdb == nil)
  668. upallocpdb();
  669. if(!(vpd[PDX(KMAP)]&PTEVALID)){
  670. /* allocate page directory */
  671. if(KMAPSIZE > BY2XPG)
  672. panic("bad kmapsize");
  673. s = spllo();
  674. pdb = newpage(0, 0, 0);
  675. splx(s);
  676. vpd[PDX(KMAP)] = pdb->pa|PTEWRITE|PTEVALID;
  677. memset(kpt, 0, BY2PG);
  678. /* might as well finish the job */
  679. kpt[0] = page->pa|PTEWRITE|PTEVALID;
  680. up->lastkmap = 0;
  681. return (KMap*)KMAP;
  682. }
  683. o = up->lastkmap+1;
  684. for(i=0; i<NKPT; i++){
  685. if(kpt[(i+o)%NKPT] == 0){
  686. o = (i+o)%NKPT;
  687. kpt[o] = page->pa|PTEWRITE|PTEVALID;
  688. up->lastkmap = o;
  689. return (KMap*)(KMAP+o*BY2PG);
  690. }
  691. }
  692. panic("out of kmap");
  693. return nil;
  694. }
  695. void
  696. kunmap(KMap *k)
  697. {
  698. ulong va;
  699. va = (ulong)k;
  700. if(up->mmupdb == nil || !(vpd[PDX(KMAP)]&PTEVALID))
  701. panic("kunmap: no kmaps");
  702. if(va < KMAP || va >= KMAP+KMAPSIZE)
  703. panic("kunmap: bad address %#.8lux pc=%#.8lux", va, getcallerpc(&k));
  704. if(!(vpt[VPTX(va)]&PTEVALID))
  705. panic("kunmap: not mapped %#.8lux pc=%#.8lux", va, getcallerpc(&k));
  706. vpt[VPTX(va)] = 0;
  707. flushpg(va);
  708. }
  709. /*
  710. * Temporary one-page mapping used to edit page directories.
  711. *
  712. * The fasttmp #define controls whether the code optimizes
  713. * the case where the page is already mapped in the physical
  714. * memory window.
  715. */
  716. #define fasttmp 1
  717. void*
  718. tmpmap(Page *p)
  719. {
  720. ulong i;
  721. ulong *entry;
  722. if(islo())
  723. panic("tmpaddr: islo");
  724. if(fasttmp && p->pa < -KZERO)
  725. return KADDR(p->pa);
  726. /*
  727. * PDX(TMPADDR) == PDX(MACHADDR), so this
  728. * entry is private to the processor and shared
  729. * between up->mmupdb (if any) and m->pdb.
  730. */
  731. entry = &vpt[VPTX(TMPADDR)];
  732. if(!(*entry&PTEVALID)){
  733. for(i=KZERO; i<=CPU0MACH; i+=BY2PG)
  734. print("%.8lux: *%.8lux=%.8lux (vpt=%.8lux index=%.8lux)\n", i, &vpt[VPTX(i)], vpt[VPTX(i)], vpt, VPTX(i));
  735. panic("tmpmap: no entry");
  736. }
  737. if(PPN(*entry) != PPN(TMPADDR-KZERO))
  738. panic("tmpmap: already mapped entry=%#.8lux", *entry);
  739. *entry = p->pa|PTEWRITE|PTEVALID;
  740. flushpg(TMPADDR);
  741. return (void*)TMPADDR;
  742. }
  743. void
  744. tmpunmap(void *v)
  745. {
  746. ulong *entry;
  747. if(islo())
  748. panic("tmpaddr: islo");
  749. if(fasttmp && (ulong)v >= KZERO && v != (void*)TMPADDR)
  750. return;
  751. if(v != (void*)TMPADDR)
  752. panic("tmpunmap: bad address");
  753. entry = &vpt[VPTX(TMPADDR)];
  754. if(!(*entry&PTEVALID) || PPN(*entry) == PPN(PADDR(TMPADDR)))
  755. panic("tmpmap: not mapped entry=%#.8lux", *entry);
  756. *entry = PPN(TMPADDR-KZERO)|PTEWRITE|PTEVALID;
  757. flushpg(TMPADDR);
  758. }
  759. /*
  760. * These could go back to being macros once the kernel is debugged,
  761. * but the extra checking is nice to have.
  762. */
  763. void*
  764. kaddr(ulong pa)
  765. {
  766. if(pa > (ulong)-KZERO)
  767. panic("kaddr: pa=%#.8lux", pa);
  768. return (void*)(pa+KZERO);
  769. }
  770. ulong
  771. paddr(void *v)
  772. {
  773. ulong va;
  774. va = (ulong)v;
  775. if(va < KZERO)
  776. panic("paddr: va=%#.8lux", va);
  777. return va-KZERO;
  778. }