fault.c 7.3 KB

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