fault.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. int
  49. fixfault(Segment *s, ulong addr, int read, int doputmmu)
  50. {
  51. int type;
  52. int ref;
  53. Pte **p, *etp;
  54. ulong mmuphys=0, soff;
  55. Page **pg, *lkp, *new;
  56. Page *(*fn)(Segment*, ulong);
  57. addr &= ~(BY2PG-1);
  58. soff = addr-s->base;
  59. p = &s->map[soff/PTEMAPMEM];
  60. if(*p == 0)
  61. *p = ptealloc();
  62. etp = *p;
  63. pg = &etp->pages[(soff&(PTEMAPMEM-1))/BY2PG];
  64. type = s->type&SG_TYPE;
  65. if(pg < etp->first)
  66. etp->first = pg;
  67. if(pg > etp->last)
  68. etp->last = pg;
  69. switch(type) {
  70. default:
  71. panic("fault");
  72. break;
  73. case SG_TEXT: /* Demand load */
  74. if(pagedout(*pg))
  75. pio(s, addr, soff, pg);
  76. mmuphys = PPN((*pg)->pa) | PTERONLY|PTEVALID;
  77. (*pg)->modref = PG_REF;
  78. break;
  79. case SG_BSS:
  80. case SG_SHARED: /* Zero fill on demand */
  81. case SG_STACK:
  82. if(*pg == 0) {
  83. new = newpage(1, &s, addr);
  84. if(s == 0)
  85. return -1;
  86. *pg = new;
  87. }
  88. goto common;
  89. case SG_DATA:
  90. common: /* Demand load/pagein/copy on write */
  91. if(pagedout(*pg))
  92. pio(s, addr, soff, pg);
  93. /*
  94. * It's only possible to copy on write if
  95. * we're the only user of the segment.
  96. */
  97. if(read && conf.copymode == 0 && s->ref == 1) {
  98. mmuphys = PPN((*pg)->pa)|PTERONLY|PTEVALID;
  99. (*pg)->modref |= PG_REF;
  100. break;
  101. }
  102. lkp = *pg;
  103. lock(lkp);
  104. if(lkp->image == &swapimage)
  105. ref = lkp->ref + swapcount(lkp->daddr);
  106. else
  107. ref = lkp->ref;
  108. if(ref > 1) {
  109. unlock(lkp);
  110. if(swapfull()){
  111. qunlock(&s->lk);
  112. pprint("swap space full\n");
  113. faulterror(Enoswap, nil, 1);
  114. }
  115. new = newpage(0, &s, addr);
  116. if(s == 0)
  117. return -1;
  118. *pg = new;
  119. copypage(lkp, *pg);
  120. putpage(lkp);
  121. }
  122. else {
  123. /* save a copy of the original for the image cache */
  124. if(lkp->image && !swapfull())
  125. duppage(lkp);
  126. unlock(lkp);
  127. }
  128. mmuphys = PPN((*pg)->pa) | PTEWRITE | PTEVALID;
  129. (*pg)->modref = PG_MOD|PG_REF;
  130. break;
  131. case SG_PHYSICAL:
  132. if(*pg == 0) {
  133. fn = s->pseg->pgalloc;
  134. if(fn)
  135. *pg = (*fn)(s, addr);
  136. else {
  137. new = smalloc(sizeof(Page));
  138. new->va = addr;
  139. new->pa = s->pseg->pa+(addr-s->base);
  140. new->ref = 1;
  141. *pg = new;
  142. }
  143. }
  144. mmuphys = PPN((*pg)->pa) |PTEWRITE|PTEUNCACHED|PTEVALID;
  145. (*pg)->modref = PG_MOD|PG_REF;
  146. break;
  147. }
  148. qunlock(&s->lk);
  149. if(doputmmu)
  150. putmmu(addr, mmuphys, *pg);
  151. return 0;
  152. }
  153. void
  154. pio(Segment *s, ulong addr, ulong soff, Page **p)
  155. {
  156. Page *new;
  157. KMap *k;
  158. Chan *c;
  159. int n, ask;
  160. char *kaddr;
  161. ulong daddr;
  162. Page *loadrec;
  163. retry:
  164. loadrec = *p;
  165. if(loadrec == 0) { /* from a text/data image */
  166. daddr = s->fstart+soff;
  167. new = lookpage(s->image, daddr);
  168. if(new != nil) {
  169. *p = new;
  170. return;
  171. }
  172. }
  173. else { /* from a swap image */
  174. daddr = swapaddr(loadrec);
  175. new = lookpage(&swapimage, daddr);
  176. if(new != nil) {
  177. putswap(loadrec);
  178. *p = new;
  179. return;
  180. }
  181. }
  182. qunlock(&s->lk);
  183. new = newpage(0, 0, addr);
  184. k = kmap(new);
  185. kaddr = (char*)VA(k);
  186. if(loadrec == 0) { /* This is demand load */
  187. c = s->image->c;
  188. while(waserror()) {
  189. if(strcmp(up->errstr, Eintr) == 0)
  190. continue;
  191. kunmap(k);
  192. putpage(new);
  193. faulterror("sys: demand load I/O error", c, 0);
  194. }
  195. ask = s->flen-soff;
  196. if(ask > BY2PG)
  197. ask = BY2PG;
  198. n = devtab[c->type]->read(c, kaddr, ask, daddr);
  199. if(n != ask)
  200. faulterror(Eioload, c, 0);
  201. if(ask < BY2PG)
  202. memset(kaddr+ask, 0, BY2PG-ask);
  203. poperror();
  204. kunmap(k);
  205. qlock(&s->lk);
  206. /*
  207. * race, another proc may have gotten here first while
  208. * s->lk was unlocked
  209. */
  210. if(*p == 0) {
  211. new->daddr = daddr;
  212. cachepage(new, s->image);
  213. *p = new;
  214. }
  215. else
  216. putpage(new);
  217. }
  218. else { /* This is paged out */
  219. c = swapimage.c;
  220. if(waserror()) {
  221. kunmap(k);
  222. putpage(new);
  223. qlock(&s->lk);
  224. qunlock(&s->lk);
  225. faulterror("sys: page in I/O error", c, 0);
  226. }
  227. n = devtab[c->type]->read(c, kaddr, BY2PG, daddr);
  228. if(n != BY2PG)
  229. faulterror(Eioload, c, 0);
  230. poperror();
  231. kunmap(k);
  232. qlock(&s->lk);
  233. /*
  234. * race, another proc may have gotten here first
  235. * (and the pager may have run on that page) while
  236. * s->lk was unlocked
  237. */
  238. if(*p != loadrec){
  239. if(!pagedout(*p)){
  240. /* another process did it for me */
  241. putpage(new);
  242. goto done;
  243. } else {
  244. /* another process and the pager got in */
  245. putpage(new);
  246. goto retry;
  247. }
  248. }
  249. new->daddr = daddr;
  250. cachepage(new, &swapimage);
  251. *p = new;
  252. putswap(loadrec);
  253. }
  254. done:
  255. if(s->flushme)
  256. memset((*p)->cachectl, PG_TXTFLUSH, sizeof((*p)->cachectl));
  257. }
  258. /*
  259. * Called only in a system call
  260. */
  261. int
  262. okaddr(ulong addr, ulong len, int write)
  263. {
  264. Segment *s;
  265. if((long)len >= 0) {
  266. for(;;) {
  267. s = seg(up, addr, 0);
  268. if(s == 0 || (write && (s->type&SG_RONLY)))
  269. break;
  270. if(addr+len > s->top) {
  271. len -= s->top - addr;
  272. addr = s->top;
  273. continue;
  274. }
  275. return 1;
  276. }
  277. }
  278. pprint("suicide: invalid address 0x%lux in sys call pc=0x%lux\n", addr, userpc());
  279. return 0;
  280. }
  281. void
  282. validaddr(ulong addr, ulong len, int write)
  283. {
  284. if(!okaddr(addr, len, write))
  285. pexit("Suicide", 0);
  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. }