2 #include "../port/lib.h"
9 * We have one page table per processor.
11 * Different processes are distinguished via the VSID field in
12 * the segment registers. As flushing the entire page table is an
13 * expensive operation, we implement an aging algorithm for
14 * mmu pids, with a background kproc to purge stale pids en mass.
16 * This needs modifications to run on a multiprocessor.
19 static ulong ptabsize; /* number of bytes in page table */
20 static ulong ptabmask; /* hash mask */
23 * VSID is 24 bits. 3 are required to distinguish segments in user
24 * space (kernel space only uses the BATs). pid 0 is reserved.
25 * The top 2 bits of the pid are used as a `color' for the background
26 * pid reclaimation algorithm.
33 PIDMAX = ((1<<PIDBITS)-1),
34 COLMASK = ((1<<COLBITS)-1),
37 #define VSID(pid, i) (((pid)<<3)|i)
38 #define PIDCOLOR(pid) ((pid)>>(PIDBITS-COLBITS))
39 #define PTECOL(color) PTE0(1, VSID(((color)<<(PIDBITS-COLBITS)), 0), 0, 0)
45 extern ulong memsize; /* passed in from ROM monitor */
48 /* heuristically size the hash table */
51 while(mem < memsize) {
55 ptabsize = (1<<(lhash+6));
56 ptabmask = (1<<lhash)-1;
59 m->ptabbase = (ulong)xspanalloc(ptabsize, 0, ptabsize);
60 putsdr1(PADDR(m->ptabbase) | (ptabmask>>10));
63 m->trigcolor = COLMASK;
69 return PIDCOLOR(m->mmupid) == m->trigcolor;
77 ulong *ptab, *ptabend, ptecol;
83 if(PIDCOLOR(m->mmupid) != m->trigcolor)
84 sleep(&m->sweepr, work, nil);
86 sweepcolor = m->sweepcolor;
89 for(i = 0; i < conf.nproc; i++, p++)
90 if(PIDCOLOR(p->mmupid) == sweepcolor)
94 ptab = (ulong*)m->ptabbase;
95 ptabend = (ulong*)(m->ptabbase+ptabsize);
96 ptecol = PTECOL(sweepcolor);
97 while(ptab < ptabend) {
98 if((*ptab & PTECOL(3)) == ptecol)
104 m->sweepcolor = (sweepcolor+1) & COLMASK;
105 m->trigcolor = (m->trigcolor+1) & COLMASK;
115 if(m->mmupid > PIDMAX)
117 newcolor = PIDCOLOR(m->mmupid);
118 if(newcolor != PIDCOLOR(pid)) {
119 if(newcolor == m->sweepcolor) {
120 /* desperation time. can't block here. punt to fault/putmmu */
121 print("newmmupid: %uld: no free mmu pids\n", up->pid);
122 if(m->mmupid == PIDBASE)
128 else if(newcolor == m->trigcolor)
155 for(i = 0; i < 8; i++)
168 for(i = 0; i < 8; i++)
169 putsr(i<<28, VSID(mp, i)|BIT(1)|BIT(2));
179 putmmu(uintptr va, uintptr pa, Page *pg)
182 ulong *p, *ep, *q, pteg;
183 ulong vsid, ptehi, x, hash;
186 * If mmupid is 0, mmuswitch/newmmupid was unable to assign us
187 * a pid, hence we faulted. Keep calling sched() until the mmusweep
188 * proc catches up, and we are able to get a pid.
190 while((mp = up->mmupid) == 0)
193 vsid = VSID(mp, va>>28);
194 hash = (vsid ^ (va>>12)&0xffff) & ptabmask;
195 ptehi = PTE0(1, vsid, 0, va);
197 pteg = m->ptabbase + BY2PTEG*hash;
199 ep = (ulong*)(pteg+BY2PTEG);
208 if(q == nil && (x & BIT(0)) == 0)
213 q = (ulong*)(pteg+m->slotgen);
214 m->slotgen = (m->slotgen + BY2PTE) & (BY2PTEG-1);
220 if(pg->txtflush & (1<<m->machno)){
221 dcflush((void*)pg->va, BY2PG);
222 icflush((void*)pg->va, BY2PG);
223 pg->txtflush &= ~(1<<m->machno);
228 checkmmu(uintptr, uintptr)
233 countpagerefs(ulong*, int)
238 * Return the number of bytes that can be accessed via KADDR(pa).
239 * If pa is not a valid argument to KADDR, return 0.