]> git.lizzy.rs Git - plan9front.git/blob - sys/src/9/port/fault.c
syscallfmt: use up->syserrstr instead of up->errstr (import from sources)
[plan9front.git] / sys / src / 9 / port / fault.c
1 #include        "u.h"
2 #include        "../port/lib.h"
3 #include        "mem.h"
4 #include        "dat.h"
5 #include        "fns.h"
6 #include        "../port/error.h"
7
8 int
9 fault(ulong addr, int read)
10 {
11         Segment *s;
12         char *sps;
13         int pnd;
14
15         if(up == nil)
16                 panic("fault: nil up");
17         if(up->nlocks.ref)
18                 print("fault: nlocks %ld\n", up->nlocks.ref);
19
20         pnd = up->notepending;
21         sps = up->psstate;
22         up->psstate = "Fault";
23
24         m->pfault++;
25         for(;;) {
26                 spllo();
27
28                 s = seg(up, addr, 1);           /* leaves s->lk qlocked if seg != nil */
29                 if(s == 0) {
30                         up->psstate = sps;
31                         return -1;
32                 }
33
34                 if(!read && (s->type&SG_RONLY)) {
35                         qunlock(&s->lk);
36                         up->psstate = sps;
37                         return -1;
38                 }
39
40                 if(fixfault(s, addr, read, 1) == 0)
41                         break;
42
43                 splhi();
44                 switch(up->procctl){
45                 case Proc_exitme:
46                 case Proc_exitbig:
47                         procctl(up);
48                 }
49         }
50
51         up->psstate = sps;
52         up->notepending |= pnd;
53
54         return 0;
55 }
56
57 static void
58 faulterror(char *s, Chan *c, int freemem)
59 {
60         char buf[ERRMAX];
61
62         if(c && c->path){
63                 snprint(buf, sizeof buf, "%s accessing %s: %s", s, c->path->s, up->errstr);
64                 s = buf;
65         }
66         if(up->nerrlab) {
67                 postnote(up, 1, s, NDebug);
68                 error(s);
69         }
70         pexit(s, freemem);
71 }
72
73 void    (*checkaddr)(ulong, Segment *, Page *);
74 ulong   addr2check;
75
76 int
77 fixfault(Segment *s, ulong addr, int read, int doputmmu)
78 {
79         int type;
80         int ref;
81         Pte **p, *etp;
82         ulong mmuphys=0, soff;
83         Page **pg, *lkp, *new;
84         Page *(*fn)(Segment*, ulong);
85
86         addr &= ~(BY2PG-1);
87         soff = addr-s->base;
88         p = &s->map[soff/PTEMAPMEM];
89         if(*p == 0)
90                 *p = ptealloc();
91
92         etp = *p;
93         pg = &etp->pages[(soff&(PTEMAPMEM-1))/BY2PG];
94         type = s->type&SG_TYPE;
95
96         if(pg < etp->first)
97                 etp->first = pg;
98         if(pg > etp->last)
99                 etp->last = pg;
100
101         switch(type) {
102         default:
103                 panic("fault");
104                 break;
105
106         case SG_TEXT:                   /* Demand load */
107                 if(pagedout(*pg))
108                         pio(s, addr, soff, pg);
109
110                 mmuphys = PPN((*pg)->pa) | PTERONLY|PTEVALID;
111                 (*pg)->modref = PG_REF;
112                 break;
113
114         case SG_BSS:
115         case SG_SHARED:                 /* Zero fill on demand */
116         case SG_STACK:
117                 if(*pg == 0) {
118                         new = newpage(1, &s, addr);
119                         if(s == 0)
120                                 return -1;
121
122                         *pg = new;
123                 }
124                 goto common;
125
126         case SG_DATA:
127         common:                 /* Demand load/pagein/copy on write */
128                 if(pagedout(*pg))
129                         pio(s, addr, soff, pg);
130
131                 /*
132                  *  It's only possible to copy on write if
133                  *  we're the only user of the segment.
134                  */
135                 if(read && conf.copymode == 0 && s->ref == 1) {
136                         mmuphys = PPN((*pg)->pa)|PTERONLY|PTEVALID;
137                         (*pg)->modref |= PG_REF;
138                         break;
139                 }
140
141                 lkp = *pg;
142                 lock(lkp);
143                 if(lkp->ref < 1)
144                         panic("fault: lkp->ref %d < 1", lkp->ref);
145                 if(lkp->image == &swapimage)
146                         ref = lkp->ref + swapcount(lkp->daddr);
147                 else
148                         ref = lkp->ref;
149                 if(ref == 1 && lkp->image){
150                         /* save a copy of the original for the image cache */
151                         duppage(lkp);
152                         ref = lkp->ref;
153                 }
154                 unlock(lkp);
155                 if(ref > 1){
156                         new = newpage(0, &s, addr);
157                         if(s == 0)
158                                 return -1;
159                         *pg = new;
160                         copypage(lkp, *pg);
161                         putpage(lkp);
162                 }
163                 mmuphys = PPN((*pg)->pa) | PTEWRITE | PTEVALID;
164                 (*pg)->modref = PG_MOD|PG_REF;
165                 break;
166
167         case SG_PHYSICAL:
168                 if(*pg == 0) {
169                         fn = s->pseg->pgalloc;
170                         if(fn)
171                                 *pg = (*fn)(s, addr);
172                         else {
173                                 new = smalloc(sizeof(Page));
174                                 new->va = addr;
175                                 new->pa = s->pseg->pa+(addr-s->base);
176                                 new->ref = 1;
177                                 *pg = new;
178                         }
179                 }
180
181                 if (checkaddr && addr == addr2check)
182                         (*checkaddr)(addr, s, *pg);
183                 mmuphys = PPN((*pg)->pa) |PTEWRITE|PTEUNCACHED|PTEVALID;
184                 (*pg)->modref = PG_MOD|PG_REF;
185                 break;
186         }
187         qunlock(&s->lk);
188
189         if(doputmmu)
190                 putmmu(addr, mmuphys, *pg);
191
192         return 0;
193 }
194
195 void
196 pio(Segment *s, ulong addr, ulong soff, Page **p)
197 {
198         Page *new;
199         KMap *k;
200         Chan *c;
201         int n, ask;
202         char *kaddr;
203         ulong daddr;
204         Page *loadrec;
205
206 retry:
207         loadrec = *p;
208         if(loadrec == 0) {      /* from a text/data image */
209                 daddr = s->fstart+soff;
210                 new = lookpage(s->image, daddr);
211                 if(new != nil) {
212                         *p = new;
213                         return;
214                 }
215
216                 c = s->image->c;
217                 ask = s->flen-soff;
218                 if(ask > BY2PG)
219                         ask = BY2PG;
220         }
221         else {                  /* from a swap image */
222                 daddr = swapaddr(loadrec);
223                 new = lookpage(&swapimage, daddr);
224                 if(new != nil) {
225                         putswap(loadrec);
226                         *p = new;
227                         return;
228                 }
229
230                 c = swapimage.c;
231                 ask = BY2PG;
232         }
233         qunlock(&s->lk);
234
235         new = newpage(0, 0, addr);
236         k = kmap(new);
237         kaddr = (char*)VA(k);
238
239         while(waserror()) {
240                 if(strcmp(up->errstr, Eintr) == 0)
241                         continue;
242                 kunmap(k);
243                 putpage(new);
244                 faulterror(Eioload, c, 0);
245         }
246         n = devtab[c->type]->read(c, kaddr, ask, daddr);
247         if(n != ask)
248                 error(Eioload);
249         if(ask < BY2PG)
250                 memset(kaddr+ask, 0, BY2PG-ask);
251
252         poperror();
253         kunmap(k);
254         qlock(&s->lk);
255         if(loadrec == 0) {      /* This is demand load */
256                 /*
257                  *  race, another proc may have gotten here first while
258                  *  s->lk was unlocked
259                  */
260                 if(*p == 0) { 
261                         new->daddr = daddr;
262                         cachepage(new, s->image);
263                         *p = new;
264                 }
265                 else
266                         putpage(new);
267         }
268         else {                  /* This is paged out */
269                 /*
270                  *  race, another proc may have gotten here first
271                  *  (and the pager may have run on that page) while
272                  *  s->lk was unlocked
273                  */
274                 if(*p != loadrec){
275                         if(!pagedout(*p)){
276                                 /* another process did it for me */
277                                 putpage(new);
278                                 goto done;
279                         } else if(*p) {
280                                 /* another process and the pager got in */
281                                 putpage(new);
282                                 goto retry;
283                         } else {
284                                 /* another process segfreed the page */
285                                 k = kmap(new);
286                                 memset((void*)VA(k), 0, ask);
287                                 kunmap(k);
288                                 *p = new;
289                                 goto done;
290                         }
291                 }
292
293                 new->daddr = daddr;
294                 cachepage(new, &swapimage);
295                 *p = new;
296                 putswap(loadrec);
297         }
298
299 done:
300         if(s->flushme)
301                 memset((*p)->cachectl, PG_TXTFLUSH, sizeof((*p)->cachectl));
302 }
303
304 /*
305  * Called only in a system call
306  */
307 int
308 okaddr(ulong addr, ulong len, int write)
309 {
310         Segment *s;
311
312         if((long)len >= 0) {
313                 for(;;) {
314                         s = seg(up, addr, 0);
315                         if(s == 0 || (write && (s->type&SG_RONLY)))
316                                 break;
317
318                         if(addr+len > s->top) {
319                                 len -= s->top - addr;
320                                 addr = s->top;
321                                 continue;
322                         }
323                         return 1;
324                 }
325         }
326         pprint("suicide: invalid address %#lux/%lud in sys call pc=%#lux\n", addr, len, userpc());
327         return 0;
328 }
329
330 void
331 validaddr(ulong addr, ulong len, int write)
332 {
333         if(!okaddr(addr, len, write)){
334                 postnote(up, 1, "sys: bad address in syscall", NDebug);
335                 error(Ebadarg);
336         }
337 }
338
339 /*
340  * &s[0] is known to be a valid address.
341  */
342 void*
343 vmemchr(void *s, int c, int n)
344 {
345         int m;
346         ulong a;
347         void *t;
348
349         a = (ulong)s;
350         while(PGROUND(a) != PGROUND(a+n-1)){
351                 /* spans pages; handle this page */
352                 m = BY2PG - (a & (BY2PG-1));
353                 t = memchr((void*)a, c, m);
354                 if(t)
355                         return t;
356                 a += m;
357                 n -= m;
358                 if(a < KZERO)
359                         validaddr(a, 1, 0);
360         }
361
362         /* fits in one page */
363         return memchr((void*)a, c, n);
364 }
365
366 Segment*
367 seg(Proc *p, ulong addr, int dolock)
368 {
369         Segment **s, **et, *n;
370
371         et = &p->seg[NSEG];
372         for(s = p->seg; s < et; s++) {
373                 n = *s;
374                 if(n == 0)
375                         continue;
376                 if(addr >= n->base && addr < n->top) {
377                         if(dolock == 0)
378                                 return n;
379
380                         qlock(&n->lk);
381                         if(addr >= n->base && addr < n->top)
382                                 return n;
383                         qunlock(&n->lk);
384                 }
385         }
386
387         return 0;
388 }
389
390 extern void checkmmu(ulong, ulong);
391 void
392 checkpages(void)
393 {
394         int checked;
395         ulong addr, off;
396         Pte *p;
397         Page *pg;
398         Segment **sp, **ep, *s;
399         
400         if(up == nil)
401                 return;
402
403         checked = 0;
404         for(sp=up->seg, ep=&up->seg[NSEG]; sp<ep; sp++){
405                 s = *sp;
406                 if(s == nil)
407                         continue;
408                 qlock(&s->lk);
409                 for(addr=s->base; addr<s->top; addr+=BY2PG){
410                         off = addr - s->base;
411                         p = s->map[off/PTEMAPMEM];
412                         if(p == 0)
413                                 continue;
414                         pg = p->pages[(off&(PTEMAPMEM-1))/BY2PG];
415                         if(pg == 0 || pagedout(pg))
416                                 continue;
417                         checkmmu(addr, pg->pa);
418                         checked++;
419                 }
420                 qunlock(&s->lk);
421         }
422         print("%ld %s: checked %d page table entries\n", up->pid, up->text, checked);
423 }