fault.ed (1625B)
1 1i 2 #define WANT_M 3 4 . 5 ,s/lock(lkp)/lock(\&lkp->lk)/g 6 ,s/s->ref/s->ref.ref/g 7 /^fault/ s/^// 8 /if(s == 0)/a 9 iprint("%ld %s fault %#x no segment\n", up->pid, up->text, addr); 10 { Segment **s, **et, *n; 11 12 et = &up->seg[NSEG]; 13 for(s = up->seg; s < et; s++) { 14 n = *s; 15 if(n == 0) 16 continue; 17 print("segment %#lux %#lux\n", n->base, n->top); 18 } 19 } 20 . 21 /qunlock(&s->lk)/a 22 iprint("%ld %s fault %#x write in read-only\n", up->pid, up->text, addr); 23 . 24 /done:/ s/$/;/ 25 +1;/^}/-1d 26 /^okaddr/-1c 27 void* 28 . 29 /^{/+1a 30 ulong addr0; 31 32 addr0 = addr; 33 . 34 /s = seg/ s/0/1/ 35 /if(s == 0/;+1 c 36 if(s == 0) 37 break; 38 if(write && (s->type&SG_RONLY)){ 39 qunlock(&s->lk); 40 break; 41 } 42 . 43 /continue/i 44 qunlock(&s->lk); 45 . 46 /return/c 47 qunlock(&s->lk); 48 return up->pmmu.uzero+addr0; 49 . 50 /^validaddr/-1;/^}/ c 51 void* 52 uvalidaddr(ulong addr, ulong len, int write) 53 { 54 void *v; 55 56 v = okaddr(addr, len, write); 57 if(v == nil) 58 pexit("Suicide", 0); 59 60 // This is a valid address, but the host kernel 61 // might not know that. In case we're going 62 // to pass the address to the host kernel in a 63 // system call, fault in the pages. 64 volatile char *a = v; 65 ulong i; 66 for(i=0; i<len; i+=BY2PG){ 67 if(write) 68 a[i] = a[i]; 69 else 70 (void)a[i]; 71 } 72 if(len > 0){ 73 if(write) 74 a[len-1] = a[len-1]; 75 else 76 (void)a[len-1]; 77 } 78 return v; 79 } 80 . 81 /^vmemchr/ s/^// 82 /int m/ s/m/m_/ 83 /ulong a/ s/ulong /uchar */ 84 /a = (ulong)s/;/while/c 85 a = s; 86 while(PGROUND((ulong)a) != PGROUND((ulong)a+n-1)){ 87 . 88 /m =/ s/m/m_/ 89 s/a/(ulong)a/ 90 /memchr/ s/m)/m_)/ 91 s/(void\*)a/a/ 92 /m;/ s/m/m_/ 93 /m;/ s/m/m_/ 94 /KZERO/ s/(.*)/(isuaddr(a))/ 95 +1 s/validaddr(.*)/uvalidaddr(a-up->pmmu.uzero, 1, 0)/