mmu.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #include <u.h>
  2. #include <ureg.h>
  3. #include "../port/lib.h"
  4. #include "mem.h"
  5. #include "dat.h"
  6. #include "fns.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, i;
  39. ulong memsize;
  40. memsize = conf.npage * BY2PG;
  41. if(ptabsize == 0) {
  42. /* heuristically size the hash table */
  43. lhash = 10;
  44. mem = (1<<23);
  45. while(mem < memsize) {
  46. lhash++;
  47. mem <<= 1;
  48. }
  49. ptabsize = (1<<(lhash+6));
  50. ptabmask = (1<<lhash)-1;
  51. }
  52. m->ptabbase = (ulong)xspanalloc(ptabsize, 0, ptabsize);
  53. /* set page table base address */
  54. putsdr1(PADDR(m->ptabbase) | (ptabmask>>10));
  55. m->mmupid = PIDBASE;
  56. m->sweepcolor = 0;
  57. m->trigcolor = COLMASK;
  58. for(i = 0; i < 16; i++)
  59. putsr(i<<28, 0);
  60. }
  61. static int
  62. work(void*)
  63. {
  64. return PIDCOLOR(m->mmupid) == m->trigcolor;
  65. }
  66. void
  67. mmusweep(void*)
  68. {
  69. Proc *p;
  70. int i, x, sweepcolor;
  71. ulong *ptab, *ptabend, ptecol;
  72. for(;;) {
  73. if(PIDCOLOR(m->mmupid) != m->trigcolor)
  74. sleep(&m->sweepr, work, nil);
  75. sweepcolor = m->sweepcolor;
  76. x = splhi();
  77. p = proctab(0);
  78. for(i = 0; i < conf.nproc; i++, p++)
  79. if(PIDCOLOR(p->mmupid) == sweepcolor)
  80. p->mmupid = 0;
  81. splx(x);
  82. ptab = (ulong*)m->ptabbase;
  83. ptabend = (ulong*)(m->ptabbase+ptabsize);
  84. ptecol = PTECOL(sweepcolor);
  85. while(ptab < ptabend) {
  86. if((*ptab & PTECOL(3)) == ptecol){
  87. *ptab = 0;
  88. }
  89. ptab += 2;
  90. }
  91. m->sweepcolor = (sweepcolor+1) & COLMASK;
  92. m->trigcolor = (m->trigcolor+1) & COLMASK;
  93. }
  94. }
  95. int
  96. newmmupid(void)
  97. {
  98. int pid, newcolor, i, x;
  99. Proc *p;
  100. pid = m->mmupid++;
  101. if(m->mmupid > PIDMAX){
  102. /* Used up all mmupids, start again from first. Flush the tlb
  103. * to delete any entries with old pids remaining, then reassign
  104. * all pids.
  105. */
  106. m->mmupid = PIDBASE;
  107. x = splhi();
  108. tlbflushall();
  109. p = proctab(0);
  110. for(i = 0; i < conf.nproc; i++, p++)
  111. p->mmupid = 0;
  112. splx(x);
  113. wakeup(&m->sweepr);
  114. }
  115. newcolor = PIDCOLOR(m->mmupid);
  116. if(newcolor != PIDCOLOR(pid)) {
  117. if(newcolor == m->sweepcolor) {
  118. /* desperation time. can't block here. punt to fault/putmmu */
  119. print("newmmupid: %uld: no free mmu pids\n", up->pid);
  120. if(m->mmupid == PIDBASE)
  121. m->mmupid = PIDMAX;
  122. else
  123. m->mmupid--;
  124. pid = 0;
  125. }
  126. else if(newcolor == m->trigcolor)
  127. wakeup(&m->sweepr);
  128. }
  129. up->mmupid = pid;
  130. return pid;
  131. }
  132. void
  133. flushmmu(void)
  134. {
  135. int x;
  136. x = splhi();
  137. up->newtlb = 1;
  138. mmuswitch(up);
  139. splx(x);
  140. }
  141. /*
  142. * called with splhi
  143. */
  144. void
  145. mmuswitch(Proc *p)
  146. {
  147. int i, mp;
  148. ulong r;
  149. if(p->kp) {
  150. for(i = 0; i < 8; i++)
  151. putsr(i<<28, 0);
  152. return;
  153. }
  154. if(p->newtlb) {
  155. p->mmupid = 0;
  156. p->newtlb = 0;
  157. }
  158. mp = p->mmupid;
  159. if(mp == 0)
  160. mp = newmmupid();
  161. for(i = 0; i < 8; i++){
  162. r = VSID(mp, i)|BIT(1)|BIT(2);
  163. putsr(i<<28, r);
  164. }
  165. }
  166. void
  167. mmurelease(Proc* p)
  168. {
  169. p->mmupid = 0;
  170. }
  171. void
  172. putmmu(ulong va, ulong pa, Page *pg)
  173. {
  174. int mp;
  175. char *ctl;
  176. ulong *p, *ep, *q, pteg;
  177. ulong vsid, hash;
  178. ulong ptehi, x;
  179. static ulong pva;
  180. /*
  181. * If mmupid is 0, mmuswitch/newmmupid was unable to assign us
  182. * a pid, hence we faulted. Keep calling sched() until the mmusweep
  183. * proc catches up, and we are able to get a pid.
  184. */
  185. while((mp = up->mmupid) == 0)
  186. sched();
  187. vsid = VSID(mp, va>>28);
  188. hash = (vsid ^ ((va>>12)&0xffff)) & ptabmask;
  189. ptehi = PTE0(1, vsid, 0, va);
  190. pteg = m->ptabbase + BY2PTEG*hash;
  191. p = (ulong*)pteg;
  192. ep = (ulong*)(pteg+BY2PTEG);
  193. q = nil;
  194. while(p < ep) {
  195. x = p[0];
  196. if(x == ptehi) {
  197. q = p;
  198. break;
  199. }
  200. if(q == nil && (x & BIT(0)) == 0)
  201. q = p;
  202. p += 2;
  203. }
  204. if(q == nil) {
  205. q = (ulong*)(pteg+m->slotgen);
  206. m->slotgen = (m->slotgen + BY2PTE) & (BY2PTEG-1);
  207. }
  208. if (q[0] != ptehi || q[1] != pa){
  209. tlbflush(va);
  210. m->tlbpurge++;
  211. }
  212. q[0] = ptehi;
  213. q[1] = pa;
  214. ctl = &pg->cachectl[m->machno];
  215. switch(*ctl) {
  216. case PG_NEWCOL:
  217. default:
  218. panic("putmmu: %d\n", *ctl);
  219. break;
  220. case PG_TXTFLUSH:
  221. dcflush((void*)pg->va, BY2PG);
  222. icflush((void*)pg->va, BY2PG);
  223. *ctl = PG_NOFLUSH;
  224. break;
  225. case PG_NOFLUSH:
  226. break;
  227. }
  228. }