3 #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 reclamation 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)
47 memsize = conf.npage * BY2PG;
49 /* heuristically size the hash table */
52 while(mem < memsize) {
56 ptabsize = (1<<(lhash+6));
57 ptabmask = (1<<lhash)-1;
59 m->ptabbase = (ulong)xspanalloc(ptabsize, 0, ptabsize);
60 /* set page table base address */
61 putsdr1(PADDR(m->ptabbase) | (ptabmask>>10));
64 m->trigcolor = COLMASK;
66 for(i = 0; i < 16; i++)
73 return PIDCOLOR(m->mmupid) == m->trigcolor;
81 ulong *ptab, *ptabend, ptecol;
87 if(PIDCOLOR(m->mmupid) != m->trigcolor)
88 sleep(&m->sweepr, work, nil);
90 sweepcolor = m->sweepcolor;
93 for(i = 0; i < conf.nproc; i++, p++)
94 if(PIDCOLOR(p->mmupid) == sweepcolor)
98 ptab = (ulong*)m->ptabbase;
99 ptabend = (ulong*)(m->ptabbase+ptabsize);
100 ptecol = PTECOL(sweepcolor);
101 while(ptab < ptabend) {
102 if((*ptab & PTECOL(3)) == ptecol){
108 m->sweepcolor = (sweepcolor+1) & COLMASK;
109 m->trigcolor = (m->trigcolor+1) & COLMASK;
116 int pid, newcolor, i, x;
120 if(m->mmupid > PIDMAX){
121 /* Used up all mmupids, start again from first. Flush the tlb
122 * to delete any entries with old pids remaining, then reassign
129 for(i = 0; i < conf.nproc; i++, p++)
134 newcolor = PIDCOLOR(m->mmupid);
135 if(newcolor != PIDCOLOR(pid)) {
136 if(newcolor == m->sweepcolor) {
137 /* desperation time. can't block here. punt to fault/putmmu */
138 print("newmmupid: %uld: no free mmu pids\n", up->pid);
139 if(m->mmupid == PIDBASE)
145 else if(newcolor == m->trigcolor)
173 for(i = 0; i < 8; i++)
186 for(i = 0; i < 8; i++){
187 r = VSID(mp, i)|BIT(1)|BIT(2);
199 putmmu(uintptr va, uintptr pa, Page *pg)
202 ulong *p, *ep, *q, pteg;
208 * If mmupid is 0, mmuswitch/newmmupid was unable to assign us
209 * a pid, hence we faulted. Keep calling sched() until the mmusweep
210 * proc catches up, and we are able to get a pid.
212 while((mp = up->mmupid) == 0)
215 vsid = VSID(mp, va>>28);
216 hash = (vsid ^ ((va>>12)&0xffff)) & ptabmask;
217 ptehi = PTE0(1, vsid, 0, va);
218 pteg = m->ptabbase + BY2PTEG*hash;
221 ep = (ulong*)(pteg+BY2PTEG);
230 if(q == nil && (x & BIT(0)) == 0)
235 q = (ulong*)(pteg+m->slotgen);
236 m->slotgen = (m->slotgen + BY2PTE) & (BY2PTEG-1);
239 if (q[0] != ptehi || q[1] != pa){
246 if(pg->txtflush & (1<<m->machno)){
247 dcflush((void*)pg->va, BY2PG);
248 icflush((void*)pg->va, BY2PG);
249 pg->txtflush &= ~(1<<m->machno);
254 checkmmu(uintptr, uintptr)
259 countpagerefs(ulong*, int)
264 * Return the number of bytes that can be accessed via KADDR(pa).
265 * If pa is not a valid argument to KADDR, return 0.