fault.c 7.2 KB

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