]> git.lizzy.rs Git - plan9front.git/blob - sys/src/9/mtx/mmu.c
devip: only add interface route for "on-link" prefixes
[plan9front.git] / sys / src / 9 / mtx / mmu.c
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 /*
9  *      We have one page table per processor.
10  *
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.
15  *
16  *      This needs modifications to run on a multiprocessor.
17  */
18
19 static ulong    ptabsize;                       /* number of bytes in page table */
20 static ulong    ptabmask;               /* hash mask */
21
22 /*
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.
27  */
28
29 enum {
30         PIDBASE = 1,
31         PIDBITS = 21,
32         COLBITS = 2,
33         PIDMAX = ((1<<PIDBITS)-1),
34         COLMASK = ((1<<COLBITS)-1),
35 };
36
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)
40
41 void
42 mmuinit(void)
43 {
44         int lhash, mem;
45         extern ulong memsize;   /* passed in from ROM monitor */
46
47         if(ptabsize == 0) {
48                 /* heuristically size the hash table */
49                 lhash = 10;
50                 mem = (1<<23);
51                 while(mem < memsize) {
52                         lhash++;
53                         mem <<= 1;
54                 }
55                 ptabsize = (1<<(lhash+6));
56                 ptabmask = (1<<lhash)-1;
57         }
58
59         m->ptabbase = (ulong)xspanalloc(ptabsize, 0, ptabsize);
60         putsdr1(PADDR(m->ptabbase) | (ptabmask>>10));
61         m->mmupid = PIDBASE;
62         m->sweepcolor = 0;
63         m->trigcolor = COLMASK;
64 }
65
66 static int
67 work(void*)
68 {
69         return PIDCOLOR(m->mmupid) == m->trigcolor;
70 }
71
72 void
73 mmusweep(void*)
74 {
75         Proc *p;
76         int i, x, sweepcolor;
77         ulong *ptab, *ptabend, ptecol;
78
79         while(waserror())
80                 ;
81
82         for(;;) {
83                 if(PIDCOLOR(m->mmupid) != m->trigcolor)
84                         sleep(&m->sweepr, work, nil);
85
86                 sweepcolor = m->sweepcolor;
87                 x = splhi();
88                 p = proctab(0);
89                 for(i = 0; i < conf.nproc; i++, p++)
90                         if(PIDCOLOR(p->mmupid) == sweepcolor)
91                                 p->mmupid = 0;
92                 splx(x);
93
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)
99                                 *ptab = 0;
100                         ptab += 2;
101                 }
102                 tlbflushall();
103
104                 m->sweepcolor = (sweepcolor+1) & COLMASK;
105                 m->trigcolor = (m->trigcolor+1) & COLMASK;
106         }
107 }
108
109 int
110 newmmupid(void)
111 {
112         int pid, newcolor;
113
114         pid = m->mmupid++;
115         if(m->mmupid > PIDMAX)
116                 m->mmupid = PIDBASE;
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)
123                                 m->mmupid = PIDMAX;
124                         else
125                                 m->mmupid--;
126                         pid = 0;
127                 }
128                 else if(newcolor == m->trigcolor)
129                         wakeup(&m->sweepr);
130         }
131         up->mmupid = pid;
132         return pid;
133 }
134
135 void
136 flushmmu(void)
137 {
138         int x;
139
140         x = splhi();
141         up->newtlb = 1;
142         mmuswitch(up);
143         splx(x);
144 }
145
146 /*
147  * called with splhi
148  */
149 void
150 mmuswitch(Proc *p)
151 {
152         int i, mp;
153
154         if(p->kp) {
155                 for(i = 0; i < 8; i++)
156                         putsr(i<<28, 0);
157                 return;
158         }
159
160         if(p->newtlb) {
161                 p->mmupid = 0;
162                 p->newtlb = 0;
163         }
164         mp = p->mmupid;
165         if(mp == 0)
166                 mp = newmmupid();
167
168         for(i = 0; i < 8; i++)
169                 putsr(i<<28, VSID(mp, i)|BIT(1)|BIT(2));
170 }
171
172 void
173 mmurelease(Proc* p)
174 {
175         p->mmupid = 0;
176 }
177
178 void
179 putmmu(uintptr va, uintptr pa, Page *pg)
180 {
181         int mp;
182         ulong *p, *ep, *q, pteg;
183         ulong vsid, ptehi, x, hash;
184
185         /*
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.
189          */
190         while((mp = up->mmupid) == 0)
191                 sched();
192
193         vsid = VSID(mp, va>>28);
194         hash = (vsid ^ (va>>12)&0xffff) & ptabmask;
195         ptehi = PTE0(1, vsid, 0, va);
196
197         pteg = m->ptabbase + BY2PTEG*hash;
198         p = (ulong*)pteg;
199         ep = (ulong*)(pteg+BY2PTEG);
200         q = nil;
201         tlbflush(va);
202         while(p < ep) {
203                 x = p[0];
204                 if(x == ptehi) {
205                         q = p;
206                         break;
207                 }
208                 if(q == nil && (x & BIT(0)) == 0)
209                         q = p;
210                 p += 2;
211         }
212         if(q == nil) {
213                 q = (ulong*)(pteg+m->slotgen);
214                 m->slotgen = (m->slotgen + BY2PTE) & (BY2PTEG-1);
215         }
216         q[0] = ptehi;
217         q[1] = pa;
218         sync();
219
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);
224         }
225 }
226
227 void
228 checkmmu(uintptr, uintptr)
229 {
230 }
231
232 void
233 countpagerefs(ulong*, int)
234 {
235 }
236
237 /*
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.
240  */
241 ulong
242 cankaddr(ulong pa)
243 {
244         ulong kzero;
245
246         kzero = -KZERO;
247         if(pa >= kzero)
248                 return 0;
249         return kzero - pa;
250 }