mmu.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #include "u.h"
  2. #include "../port/lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "io.h"
  7. /*
  8. * We have one page table per processor.
  9. *
  10. * Different processes are distinguished via the VSID field in
  11. * the segment registers. As flushing the entire page table is an
  12. * expensive operation, we implement an aging algorithm for
  13. * mmu pids, with a background kproc to purge stale pids en mass.
  14. *
  15. * This needs modifications to run on a multiprocessor.
  16. */
  17. static ulong ptabsize; /* number of bytes in page table */
  18. static ulong ptabmask; /* hash mask */
  19. /*
  20. * VSID is 24 bits. 3 are required to distinguish segments in user
  21. * space (kernel space only uses the BATs). pid 0 is reserved.
  22. * The top 2 bits of the pid are used as a `color' for the background
  23. * pid reclaimation algorithm.
  24. */
  25. enum {
  26. PIDBASE = 1,
  27. PIDBITS = 21,
  28. COLBITS = 2,
  29. PIDMAX = ((1<<PIDBITS)-1),
  30. COLMASK = ((1<<COLBITS)-1),
  31. };
  32. #define VSID(pid, i) (((pid)<<3)|i)
  33. #define PIDCOLOR(pid) ((pid)>>(PIDBITS-COLBITS))
  34. #define PTECOL(color) PTE0(1, VSID(((color)<<(PIDBITS-COLBITS)), 0), 0, 0)
  35. void
  36. mmuinit(void)
  37. {
  38. int lhash, mem;
  39. extern ulong memsize; /* passed in from ROM monitor */
  40. if(ptabsize == 0) {
  41. /* heuristically size the hash table */
  42. lhash = 10;
  43. mem = (1<<23);
  44. while(mem < memsize) {
  45. lhash++;
  46. mem <<= 1;
  47. }
  48. ptabsize = (1<<(lhash+6));
  49. ptabmask = (1<<lhash)-1;
  50. }
  51. m->ptabbase = (ulong)xspanalloc(ptabsize, 0, ptabsize);
  52. putsdr1(PADDR(m->ptabbase) | (ptabmask>>10));
  53. m->mmupid = PIDBASE;
  54. m->sweepcolor = 0;
  55. m->trigcolor = COLMASK;
  56. }
  57. static int
  58. work(void*)
  59. {
  60. return PIDCOLOR(m->mmupid) == m->trigcolor;
  61. }
  62. void
  63. mmusweep(void*)
  64. {
  65. Proc *p;
  66. int i, x, sweepcolor;
  67. ulong *ptab, *ptabend, ptecol;
  68. for(;;) {
  69. if(PIDCOLOR(m->mmupid) != m->trigcolor)
  70. sleep(&m->sweepr, work, nil);
  71. sweepcolor = m->sweepcolor;
  72. x = splhi();
  73. p = proctab(0);
  74. for(i = 0; i < conf.nproc; i++, p++)
  75. if(PIDCOLOR(p->mmupid) == sweepcolor)
  76. p->mmupid = 0;
  77. splx(x);
  78. ptab = (ulong*)m->ptabbase;
  79. ptabend = (ulong*)(m->ptabbase+ptabsize);
  80. ptecol = PTECOL(sweepcolor);
  81. while(ptab < ptabend) {
  82. if((*ptab & PTECOL(3)) == ptecol)
  83. *ptab = 0;
  84. ptab += 2;
  85. }
  86. tlbflushall();
  87. m->sweepcolor = (sweepcolor+1) & COLMASK;
  88. m->trigcolor = (m->trigcolor+1) & COLMASK;
  89. }
  90. }
  91. int
  92. newmmupid(void)
  93. {
  94. int pid, newcolor;
  95. pid = m->mmupid++;
  96. if(m->mmupid > PIDMAX)
  97. m->mmupid = PIDBASE;
  98. newcolor = PIDCOLOR(m->mmupid);
  99. if(newcolor != PIDCOLOR(pid)) {
  100. if(newcolor == m->sweepcolor) {
  101. /* desperation time. can't block here. punt to fault/putmmu */
  102. print("newmmupid: %uld: no free mmu pids\n", up->pid);
  103. if(m->mmupid == PIDBASE)
  104. m->mmupid = PIDMAX;
  105. else
  106. m->mmupid--;
  107. pid = 0;
  108. }
  109. else if(newcolor == m->trigcolor)
  110. wakeup(&m->sweepr);
  111. }
  112. up->mmupid = pid;
  113. return pid;
  114. }
  115. void
  116. flushmmu(void)
  117. {
  118. int x;
  119. x = splhi();
  120. up->newtlb = 1;
  121. mmuswitch(up);
  122. splx(x);
  123. }
  124. /*
  125. * called with splhi
  126. */
  127. void
  128. mmuswitch(Proc *p)
  129. {
  130. int i, mp;
  131. if(p->kp) {
  132. for(i = 0; i < 8; i++)
  133. putsr(i<<28, 0);
  134. return;
  135. }
  136. if(p->newtlb) {
  137. p->mmupid = 0;
  138. p->newtlb = 0;
  139. }
  140. mp = p->mmupid;
  141. if(mp == 0)
  142. mp = newmmupid();
  143. for(i = 0; i < 8; i++)
  144. putsr(i<<28, VSID(mp, i)|BIT(1)|BIT(2));
  145. }
  146. void
  147. mmurelease(Proc* p)
  148. {
  149. p->mmupid = 0;
  150. }
  151. void
  152. putmmu(ulong va, ulong pa, Page *pg)
  153. {
  154. int mp;
  155. char *ctl;
  156. ulong *p, *ep, *q, pteg;
  157. ulong vsid, ptehi, x, hash;
  158. /*
  159. * If mmupid is 0, mmuswitch/newmmupid was unable to assign us
  160. * a pid, hence we faulted. Keep calling sched() until the mmusweep
  161. * proc catches up, and we are able to get a pid.
  162. */
  163. while((mp = up->mmupid) == 0)
  164. sched();
  165. vsid = VSID(mp, va>>28);
  166. hash = (vsid ^ (va>>12)&0xffff) & ptabmask;
  167. ptehi = PTE0(1, vsid, 0, va);
  168. pteg = m->ptabbase + BY2PTEG*hash;
  169. p = (ulong*)pteg;
  170. ep = (ulong*)(pteg+BY2PTEG);
  171. q = nil;
  172. tlbflush(va);
  173. while(p < ep) {
  174. x = p[0];
  175. if(x == ptehi) {
  176. q = p;
  177. break;
  178. }
  179. if(q == nil && (x & BIT(0)) == 0)
  180. q = p;
  181. p += 2;
  182. }
  183. if(q == nil) {
  184. q = (ulong*)(pteg+m->slotgen);
  185. m->slotgen = (m->slotgen + BY2PTE) & (BY2PTEG-1);
  186. }
  187. q[0] = ptehi;
  188. q[1] = pa;
  189. sync();
  190. ctl = &pg->cachectl[m->machno];
  191. switch(*ctl) {
  192. case PG_NEWCOL:
  193. default:
  194. panic("putmmu: %d\n", *ctl);
  195. break;
  196. case PG_NOFLUSH:
  197. break;
  198. case PG_TXTFLUSH:
  199. dcflush((void*)pg->va, BY2PG);
  200. icflush((void*)pg->va, BY2PG);
  201. *ctl = PG_NOFLUSH;
  202. break;
  203. }
  204. }
  205. void
  206. checkmmu(ulong, ulong)
  207. {
  208. }
  209. void
  210. countpagerefs(ulong*, int)
  211. {
  212. }
  213. /*
  214. * Return the number of bytes that can be accessed via KADDR(pa).
  215. * If pa is not a valid argument to KADDR, return 0.
  216. */
  217. ulong
  218. cankaddr(ulong pa)
  219. {
  220. ulong kzero;
  221. kzero = -KZERO;
  222. if(pa >= kzero)
  223. return 0;
  224. return kzero - pa;
  225. }