segment.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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. static void imagereclaim(void);
  8. static void imagechanreclaim(void);
  9. #include "io.h"
  10. /*
  11. * Attachable segment types
  12. */
  13. static Physseg physseg[10] = {
  14. { SG_SHARED, "shared", 0, SEGMAXSIZE, 0, 0 },
  15. { SG_BSS, "memory", 0, SEGMAXSIZE, 0, 0 },
  16. { 0, 0, 0, 0, 0, 0 },
  17. };
  18. static Lock physseglock;
  19. #define NFREECHAN 64
  20. #define IHASHSIZE 64
  21. #define ihash(s) imagealloc.hash[s%IHASHSIZE]
  22. static struct Imagealloc
  23. {
  24. Lock;
  25. Image *free;
  26. Image *hash[IHASHSIZE];
  27. QLock ireclaim; /* mutex on reclaiming free images */
  28. Chan **freechan; /* free image channels */
  29. int nfreechan; /* number of free channels */
  30. int szfreechan; /* size of freechan array */
  31. QLock fcreclaim; /* mutex on reclaiming free channels */
  32. }imagealloc;
  33. Segment* (*_globalsegattach)(Proc*, char*);
  34. void
  35. initseg(void)
  36. {
  37. Image *i, *ie;
  38. imagealloc.free = xalloc(conf.nimage*sizeof(Image));
  39. ie = &imagealloc.free[conf.nimage-1];
  40. for(i = imagealloc.free; i < ie; i++)
  41. i->next = i+1;
  42. i->next = 0;
  43. imagealloc.freechan = malloc(NFREECHAN * sizeof(Chan*));
  44. imagealloc.szfreechan = NFREECHAN;
  45. }
  46. Segment *
  47. newseg(int type, ulong base, ulong size)
  48. {
  49. Segment *s;
  50. int mapsize;
  51. if(size > (SEGMAPSIZE*PTEPERTAB))
  52. error(Enovmem);
  53. if(swapfull())
  54. error(Enoswap);
  55. s = smalloc(sizeof(Segment));
  56. s->ref = 1;
  57. s->type = type;
  58. s->base = base;
  59. s->top = base+(size*BY2PG);
  60. s->size = size;
  61. mapsize = ROUND(size, PTEPERTAB)/PTEPERTAB;
  62. if(mapsize > nelem(s->ssegmap)){
  63. mapsize *= 2;
  64. if(mapsize > (SEGMAPSIZE*PTEPERTAB))
  65. mapsize = (SEGMAPSIZE*PTEPERTAB);
  66. s->map = smalloc(mapsize*sizeof(Pte*));
  67. s->mapsize = mapsize;
  68. }
  69. else{
  70. s->map = s->ssegmap;
  71. s->mapsize = nelem(s->ssegmap);
  72. }
  73. return s;
  74. }
  75. void
  76. putseg(Segment *s)
  77. {
  78. Pte **pp, **emap;
  79. Image *i;
  80. if(s == 0)
  81. return;
  82. i = s->image;
  83. if(i != 0) {
  84. lock(i);
  85. lock(s);
  86. if(i->s == s && s->ref == 1)
  87. i->s = 0;
  88. unlock(i);
  89. }
  90. else
  91. lock(s);
  92. s->ref--;
  93. if(s->ref != 0) {
  94. unlock(s);
  95. return;
  96. }
  97. unlock(s);
  98. qlock(&s->lk);
  99. if(i)
  100. putimage(i);
  101. emap = &s->map[s->mapsize];
  102. for(pp = s->map; pp < emap; pp++)
  103. if(*pp)
  104. freepte(s, *pp);
  105. qunlock(&s->lk);
  106. if(s->map != s->ssegmap)
  107. free(s->map);
  108. if(s->profile != 0)
  109. free(s->profile);
  110. free(s);
  111. }
  112. void
  113. relocateseg(Segment *s, ulong offset)
  114. {
  115. Page **pg, *x;
  116. Pte *pte, **p, **endpte;
  117. endpte = &s->map[s->mapsize];
  118. for(p = s->map; p < endpte; p++) {
  119. if(*p == 0)
  120. continue;
  121. pte = *p;
  122. for(pg = pte->first; pg <= pte->last; pg++) {
  123. if(x = *pg)
  124. x->va += offset;
  125. }
  126. }
  127. }
  128. Segment*
  129. dupseg(Segment **seg, int segno, int share)
  130. {
  131. int i, size;
  132. Pte *pte;
  133. Segment *n, *s;
  134. SET(n);
  135. s = seg[segno];
  136. qlock(&s->lk);
  137. if(waserror()){
  138. qunlock(&s->lk);
  139. nexterror();
  140. }
  141. switch(s->type&SG_TYPE) {
  142. case SG_TEXT: /* New segment shares pte set */
  143. case SG_SHARED:
  144. case SG_PHYSICAL:
  145. goto sameseg;
  146. case SG_STACK:
  147. n = newseg(s->type, s->base, s->size);
  148. break;
  149. case SG_BSS: /* Just copy on write */
  150. if(share)
  151. goto sameseg;
  152. n = newseg(s->type, s->base, s->size);
  153. break;
  154. case SG_DATA: /* Copy on write plus demand load info */
  155. if(segno == TSEG){
  156. poperror();
  157. qunlock(&s->lk);
  158. return data2txt(s);
  159. }
  160. if(share)
  161. goto sameseg;
  162. n = newseg(s->type, s->base, s->size);
  163. incref(s->image);
  164. n->image = s->image;
  165. n->fstart = s->fstart;
  166. n->flen = s->flen;
  167. break;
  168. }
  169. size = s->mapsize;
  170. for(i = 0; i < size; i++)
  171. if(pte = s->map[i])
  172. n->map[i] = ptecpy(pte);
  173. n->flushme = s->flushme;
  174. if(s->ref > 1)
  175. procflushseg(s);
  176. poperror();
  177. qunlock(&s->lk);
  178. return n;
  179. sameseg:
  180. incref(s);
  181. poperror();
  182. qunlock(&s->lk);
  183. return s;
  184. }
  185. void
  186. segpage(Segment *s, Page *p)
  187. {
  188. Pte **pte;
  189. ulong off;
  190. Page **pg;
  191. if(p->va < s->base || p->va >= s->top)
  192. panic("segpage");
  193. off = p->va - s->base;
  194. pte = &s->map[off/PTEMAPMEM];
  195. if(*pte == 0)
  196. *pte = ptealloc();
  197. pg = &(*pte)->pages[(off&(PTEMAPMEM-1))/BY2PG];
  198. *pg = p;
  199. if(pg < (*pte)->first)
  200. (*pte)->first = pg;
  201. if(pg > (*pte)->last)
  202. (*pte)->last = pg;
  203. }
  204. Image*
  205. attachimage(int type, Chan *c, ulong base, ulong len)
  206. {
  207. Image *i, **l;
  208. /* reclaim any free channels from reclaimed segments */
  209. if(imagealloc.nfreechan)
  210. imagechanreclaim();
  211. lock(&imagealloc);
  212. /*
  213. * Search the image cache for remains of the text from a previous
  214. * or currently running incarnation
  215. */
  216. for(i = ihash(c->qid.path); i; i = i->hash) {
  217. if(c->qid.path == i->qid.path) {
  218. lock(i);
  219. if(eqqid(c->qid, i->qid) &&
  220. eqqid(c->mqid, i->mqid) &&
  221. c->mchan == i->mchan &&
  222. c->type == i->type) {
  223. i->ref++;
  224. goto found;
  225. }
  226. unlock(i);
  227. }
  228. }
  229. /*
  230. * imagereclaim dumps pages from the free list which are cached by image
  231. * structures. This should free some image structures.
  232. */
  233. while(!(i = imagealloc.free)) {
  234. unlock(&imagealloc);
  235. imagereclaim();
  236. sched();
  237. lock(&imagealloc);
  238. }
  239. imagealloc.free = i->next;
  240. lock(i);
  241. incref(c);
  242. i->c = c;
  243. i->type = c->type;
  244. i->qid = c->qid;
  245. i->mqid = c->mqid;
  246. i->mchan = c->mchan;
  247. i->ref = 1;
  248. l = &ihash(c->qid.path);
  249. i->hash = *l;
  250. *l = i;
  251. found:
  252. unlock(&imagealloc);
  253. if(i->s == 0) {
  254. /* Disaster after commit in exec */
  255. if(waserror()) {
  256. unlock(i);
  257. pexit(Enovmem, 1);
  258. }
  259. i->s = newseg(type, base, len);
  260. i->s->image = i;
  261. poperror();
  262. }
  263. else
  264. incref(i->s);
  265. return i;
  266. }
  267. static struct {
  268. int calls; /* times imagereclaim was called */
  269. int loops; /* times the main loop was run */
  270. uvlong ticks; /* total time in the main loop */
  271. uvlong maxt; /* longest time in main loop */
  272. } irstats;
  273. static void
  274. imagereclaim(void)
  275. {
  276. Page *p;
  277. uvlong ticks;
  278. irstats.calls++;
  279. /* Somebody is already cleaning the page cache */
  280. if(!canqlock(&imagealloc.ireclaim))
  281. return;
  282. lock(&palloc);
  283. ticks = fastticks(nil);
  284. for(p = palloc.head; p; p = p->next) {
  285. if(p->ref == 0 && p->image && canlock(p)) {
  286. if(p->ref == 0)
  287. uncachepage(p);
  288. unlock(p);
  289. }
  290. }
  291. ticks = fastticks(nil) - ticks;
  292. unlock(&palloc);
  293. irstats.loops++;
  294. irstats.ticks += ticks;
  295. if(ticks > irstats.maxt)
  296. irstats.maxt = ticks;
  297. //print("T%llud+", ticks);
  298. qunlock(&imagealloc.ireclaim);
  299. }
  300. /*
  301. * since close can block, this has to be called outside of
  302. * spin locks.
  303. */
  304. static void
  305. imagechanreclaim(void)
  306. {
  307. Chan *c;
  308. /* Somebody is already cleaning the image chans */
  309. if(!canqlock(&imagealloc.fcreclaim))
  310. return;
  311. /*
  312. * We don't have to recheck that nfreechan > 0 after we
  313. * acquire the lock, because we're the only ones who decrement
  314. * it (the other lock contender increments it), and there's only
  315. * one of us thanks to the qlock above.
  316. */
  317. while(imagealloc.nfreechan > 0){
  318. lock(&imagealloc);
  319. imagealloc.nfreechan--;
  320. c = imagealloc.freechan[imagealloc.nfreechan];
  321. unlock(&imagealloc);
  322. cclose(c);
  323. }
  324. qunlock(&imagealloc.fcreclaim);
  325. }
  326. void
  327. putimage(Image *i)
  328. {
  329. Chan *c, **cp;
  330. Image *f, **l;
  331. if(i->notext)
  332. return;
  333. lock(i);
  334. if(--i->ref == 0) {
  335. l = &ihash(i->qid.path);
  336. mkqid(&i->qid, ~0, ~0, QTFILE);
  337. unlock(i);
  338. c = i->c;
  339. lock(&imagealloc);
  340. for(f = *l; f; f = f->hash) {
  341. if(f == i) {
  342. *l = i->hash;
  343. break;
  344. }
  345. l = &f->hash;
  346. }
  347. i->next = imagealloc.free;
  348. imagealloc.free = i;
  349. /* defer freeing channel till we're out of spin lock's */
  350. if(imagealloc.nfreechan == imagealloc.szfreechan){
  351. imagealloc.szfreechan += NFREECHAN;
  352. cp = malloc(imagealloc.szfreechan*sizeof(Chan*));
  353. if(cp == nil)
  354. panic("putimage");
  355. memmove(cp, imagealloc.freechan, imagealloc.nfreechan*sizeof(Chan*));
  356. free(imagealloc.freechan);
  357. imagealloc.freechan = cp;
  358. }
  359. imagealloc.freechan[imagealloc.nfreechan++] = c;
  360. unlock(&imagealloc);
  361. return;
  362. }
  363. unlock(i);
  364. }
  365. long
  366. ibrk(ulong addr, int seg)
  367. {
  368. Segment *s, *ns;
  369. ulong newtop, newsize;
  370. int i, mapsize;
  371. Pte **map;
  372. s = up->seg[seg];
  373. if(s == 0)
  374. error(Ebadarg);
  375. if(addr == 0)
  376. return s->base;
  377. qlock(&s->lk);
  378. /* We may start with the bss overlapping the data */
  379. if(addr < s->base) {
  380. if(seg != BSEG || up->seg[DSEG] == 0 || addr < up->seg[DSEG]->base) {
  381. qunlock(&s->lk);
  382. error(Enovmem);
  383. }
  384. addr = s->base;
  385. }
  386. newtop = PGROUND(addr);
  387. newsize = (newtop-s->base)/BY2PG;
  388. if(newtop < s->top) {
  389. mfreeseg(s, newtop, (s->top-newtop)/BY2PG);
  390. qunlock(&s->lk);
  391. flushmmu();
  392. return 0;
  393. }
  394. if(swapfull()){
  395. qunlock(&s->lk);
  396. error(Enoswap);
  397. }
  398. for(i = 0; i < NSEG; i++) {
  399. ns = up->seg[i];
  400. if(ns == 0 || ns == s)
  401. continue;
  402. if(newtop >= ns->base && newtop < ns->top) {
  403. qunlock(&s->lk);
  404. error(Esoverlap);
  405. }
  406. }
  407. if(newsize > (SEGMAPSIZE*PTEPERTAB)) {
  408. qunlock(&s->lk);
  409. error(Enovmem);
  410. }
  411. mapsize = ROUND(newsize, PTEPERTAB)/PTEPERTAB;
  412. if(mapsize > s->mapsize){
  413. map = smalloc(mapsize*sizeof(Pte*));
  414. memmove(map, s->map, s->mapsize*sizeof(Pte*));
  415. if(s->map != s->ssegmap)
  416. free(s->map);
  417. s->map = map;
  418. s->mapsize = mapsize;
  419. }
  420. s->top = newtop;
  421. s->size = newsize;
  422. qunlock(&s->lk);
  423. return 0;
  424. }
  425. /*
  426. * called with s->lk locked
  427. */
  428. void
  429. mfreeseg(Segment *s, ulong start, int pages)
  430. {
  431. int i, j, size;
  432. ulong soff;
  433. Page *pg;
  434. Page *list;
  435. soff = start-s->base;
  436. j = (soff&(PTEMAPMEM-1))/BY2PG;
  437. size = s->mapsize;
  438. list = nil;
  439. for(i = soff/PTEMAPMEM; i < size; i++) {
  440. if(pages <= 0)
  441. break;
  442. if(s->map[i] == 0) {
  443. pages -= PTEPERTAB-j;
  444. j = 0;
  445. continue;
  446. }
  447. while(j < PTEPERTAB) {
  448. pg = s->map[i]->pages[j];
  449. /*
  450. * We want to zero s->map[i]->page[j] and putpage(pg),
  451. * but we have to make sure other processors flush the entry
  452. * entry from their TLBs before the page is freed.
  453. * We construct a list of the pages to be freed, zero
  454. * the entries, then (below) call procflushseg, and call
  455. * putpage on the whole list.
  456. *
  457. * Swapped-out pages don't appear in TLBs, so it's okay
  458. * to putswap those pages before procflushseg.
  459. */
  460. if(pg){
  461. if(onswap(pg))
  462. putswap(pg);
  463. else{
  464. pg->next = list;
  465. list = pg;
  466. }
  467. s->map[i]->pages[j] = 0;
  468. }
  469. if(--pages == 0)
  470. goto out;
  471. j++;
  472. }
  473. j = 0;
  474. }
  475. out:
  476. /* flush this seg in all other processes */
  477. if(s->ref > 1)
  478. procflushseg(s);
  479. /* free the pages */
  480. for(pg = list; pg != nil; pg = list){
  481. list = list->next;
  482. putpage(pg);
  483. }
  484. }
  485. Segment*
  486. isoverlap(Proc *p, ulong va, int len)
  487. {
  488. int i;
  489. Segment *ns;
  490. ulong newtop;
  491. newtop = va+len;
  492. for(i = 0; i < NSEG; i++) {
  493. ns = p->seg[i];
  494. if(ns == 0)
  495. continue;
  496. if((newtop > ns->base && newtop <= ns->top) ||
  497. (va >= ns->base && va < ns->top))
  498. return ns;
  499. }
  500. return nil;
  501. }
  502. int
  503. addphysseg(Physseg* new)
  504. {
  505. Physseg *ps;
  506. /*
  507. * Check not already entered and there is room
  508. * for a new entry and the terminating null entry.
  509. */
  510. lock(&physseglock);
  511. for(ps = physseg; ps->name; ps++){
  512. if(strcmp(ps->name, new->name) == 0){
  513. unlock(&physseglock);
  514. return -1;
  515. }
  516. }
  517. if(ps-physseg >= nelem(physseg)-2){
  518. unlock(&physseglock);
  519. return -1;
  520. }
  521. *ps = *new;
  522. unlock(&physseglock);
  523. return 0;
  524. }
  525. int
  526. isphysseg(char *name)
  527. {
  528. Physseg *ps;
  529. int rv = 0;
  530. lock(&physseglock);
  531. for(ps = physseg; ps->name; ps++){
  532. if(strcmp(ps->name, name) == 0){
  533. rv = 1;
  534. break;
  535. }
  536. }
  537. unlock(&physseglock);
  538. return rv;
  539. }
  540. ulong
  541. segattach(Proc *p, ulong attr, char *name, ulong va, ulong len)
  542. {
  543. int sno;
  544. Segment *s, *os;
  545. Physseg *ps;
  546. if(va != 0 && (va&KZERO) == KZERO) /* BUG: Only ok for now */
  547. error(Ebadarg);
  548. validaddr((ulong)name, 1, 0);
  549. vmemchr(name, 0, ~0);
  550. for(sno = 0; sno < NSEG; sno++)
  551. if(p->seg[sno] == nil && sno != ESEG)
  552. break;
  553. if(sno == NSEG)
  554. error(Enovmem);
  555. /*
  556. * first look for a global segment with the
  557. * same name
  558. */
  559. if(_globalsegattach != nil){
  560. s = (*_globalsegattach)(p, name);
  561. if(s != nil){
  562. p->seg[sno] = s;
  563. return s->base;
  564. }
  565. }
  566. len = PGROUND(len);
  567. if(len == 0)
  568. error(Ebadarg);
  569. /*
  570. * Find a hole in the address space.
  571. * Starting at the lowest possible stack address - len,
  572. * check for an overlapping segment, and repeat at the
  573. * base of that segment - len until either a hole is found
  574. * or the address space is exhausted.
  575. */
  576. if(va == 0) {
  577. va = p->seg[SSEG]->base - len;
  578. for(;;) {
  579. os = isoverlap(p, va, len);
  580. if(os == nil)
  581. break;
  582. va = os->base;
  583. if(len > va)
  584. error(Enovmem);
  585. va -= len;
  586. }
  587. }
  588. va = va&~(BY2PG-1);
  589. if(isoverlap(p, va, len) != nil)
  590. error(Esoverlap);
  591. for(ps = physseg; ps->name; ps++)
  592. if(strcmp(name, ps->name) == 0)
  593. goto found;
  594. error(Ebadarg);
  595. found:
  596. if(len > ps->size)
  597. error(Enovmem);
  598. attr &= ~SG_TYPE; /* Turn off what is not allowed */
  599. attr |= ps->attr; /* Copy in defaults */
  600. s = newseg(attr, va, len/BY2PG);
  601. s->pseg = ps;
  602. p->seg[sno] = s;
  603. return va;
  604. }
  605. void
  606. pteflush(Pte *pte, int s, int e)
  607. {
  608. int i;
  609. Page *p;
  610. for(i = s; i < e; i++) {
  611. p = pte->pages[i];
  612. if(pagedout(p) == 0)
  613. memset(p->cachectl, PG_TXTFLUSH, sizeof(p->cachectl));
  614. }
  615. }
  616. long
  617. syssegflush(ulong *arg)
  618. {
  619. Segment *s;
  620. ulong addr, l;
  621. Pte *pte;
  622. int chunk, ps, pe, len;
  623. addr = arg[0];
  624. len = arg[1];
  625. while(len > 0) {
  626. s = seg(up, addr, 1);
  627. if(s == 0)
  628. error(Ebadarg);
  629. s->flushme = 1;
  630. more:
  631. l = len;
  632. if(addr+l > s->top)
  633. l = s->top - addr;
  634. ps = addr-s->base;
  635. pte = s->map[ps/PTEMAPMEM];
  636. ps &= PTEMAPMEM-1;
  637. pe = PTEMAPMEM;
  638. if(pe-ps > l){
  639. pe = ps + l;
  640. pe = (pe+BY2PG-1)&~(BY2PG-1);
  641. }
  642. if(pe == ps) {
  643. qunlock(&s->lk);
  644. error(Ebadarg);
  645. }
  646. if(pte)
  647. pteflush(pte, ps/BY2PG, pe/BY2PG);
  648. chunk = pe-ps;
  649. len -= chunk;
  650. addr += chunk;
  651. if(len > 0 && addr < s->top)
  652. goto more;
  653. qunlock(&s->lk);
  654. }
  655. flushmmu();
  656. return 0;
  657. }
  658. void
  659. segclock(ulong pc)
  660. {
  661. Segment *s;
  662. s = up->seg[TSEG];
  663. if(s == 0 || s->profile == 0)
  664. return;
  665. s->profile[0] += TK2MS(1);
  666. if(pc >= s->base && pc < s->top) {
  667. pc -= s->base;
  668. s->profile[pc>>LRESPROF] += TK2MS(1);
  669. }
  670. }