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;
92 for(i = 0; i < conf.nproc; i++){
94 if(PIDCOLOR(p->mmupid) == sweepcolor)
99 ptab = (ulong*)m->ptabbase;
100 ptabend = (ulong*)(m->ptabbase+ptabsize);
101 ptecol = PTECOL(sweepcolor);
102 while(ptab < ptabend) {
103 if((*ptab & PTECOL(3)) == ptecol){
109 m->sweepcolor = (sweepcolor+1) & COLMASK;
110 m->trigcolor = (m->trigcolor+1) & COLMASK;
117 int pid, newcolor, i, x;
121 if(m->mmupid > PIDMAX){
122 /* Used up all mmupids, start again from first. Flush the tlb
123 * to delete any entries with old pids remaining, then reassign
129 for(i = 0; i < conf.nproc; i++){
136 newcolor = PIDCOLOR(m->mmupid);
137 if(newcolor != PIDCOLOR(pid)) {
138 if(newcolor == m->sweepcolor) {
139 /* desperation time. can't block here. punt to fault/putmmu */
140 print("newmmupid: %uld: no free mmu pids\n", up->pid);
141 if(m->mmupid == PIDBASE)
147 else if(newcolor == m->trigcolor)
175 for(i = 0; i < 8; i++)
188 for(i = 0; i < 8; i++){
189 r = VSID(mp, i)|BIT(1)|BIT(2);
201 putmmu(uintptr va, uintptr pa, Page *pg)
204 ulong *p, *ep, *q, pteg;
210 * If mmupid is 0, mmuswitch/newmmupid was unable to assign us
211 * a pid, hence we faulted. Keep calling sched() until the mmusweep
212 * proc catches up, and we are able to get a pid.
214 while((mp = up->mmupid) == 0)
217 vsid = VSID(mp, va>>28);
218 hash = (vsid ^ ((va>>12)&0xffff)) & ptabmask;
219 ptehi = PTE0(1, vsid, 0, va);
220 pteg = m->ptabbase + BY2PTEG*hash;
223 ep = (ulong*)(pteg+BY2PTEG);
232 if(q == nil && (x & BIT(0)) == 0)
237 q = (ulong*)(pteg+m->slotgen);
238 m->slotgen = (m->slotgen + BY2PTE) & (BY2PTEG-1);
241 if (q[0] != ptehi || q[1] != pa){
248 if(pg->txtflush & (1<<m->machno)){
249 dcflush((void*)pg->va, BY2PG);
250 icflush((void*)pg->va, BY2PG);
251 pg->txtflush &= ~(1<<m->machno);
256 checkmmu(uintptr, uintptr)
261 * Return the number of bytes that can be accessed via KADDR(pa).
262 * If pa is not a valid argument to KADDR, return 0.