2 #include "../port/lib.h"
6 #include "../port/error.h"
8 static void imagereclaim(void);
9 static void imagechanreclaim(void);
14 * Attachable segment types
16 static Physseg physseg[10] = {
17 { SG_SHARED, "shared", 0, SEGMAXSIZE, 0, 0 },
18 { SG_BSS, "memory", 0, SEGMAXSIZE, 0, 0 },
22 static Lock physseglock;
26 #define ihash(s) imagealloc.hash[s%IHASHSIZE]
27 static struct Imagealloc
31 Image *hash[IHASHSIZE];
32 QLock ireclaim; /* mutex on reclaiming free images */
34 Chan **freechan; /* free image channels */
35 int nfreechan; /* number of free channels */
36 int szfreechan; /* size of freechan array */
37 QLock fcreclaim; /* mutex on reclaiming free channels */
40 Segment* (*_globalsegattach)(Proc*, char*);
47 imagealloc.free = xalloc(conf.nimage*sizeof(Image));
48 if(imagealloc.free == nil)
49 panic("initseg: no memory for Image");
50 ie = &imagealloc.free[conf.nimage-1];
51 for(i = imagealloc.free; i < ie; i++)
54 imagealloc.freechan = malloc(NFREECHAN * sizeof(Chan*));
55 if(imagealloc.freechan == nil)
56 panic("initseg: no memory for Chan");
57 imagealloc.szfreechan = NFREECHAN;
61 newseg(int type, ulong base, ulong size)
66 if(size > (SEGMAPSIZE*PTEPERTAB))
69 s = smalloc(sizeof(Segment));
73 s->top = base+(size*BY2PG);
75 s->sema.prev = &s->sema;
76 s->sema.next = &s->sema;
78 mapsize = ROUND(size, PTEPERTAB)/PTEPERTAB;
79 if(mapsize > nelem(s->ssegmap)){
81 if(mapsize > (SEGMAPSIZE*PTEPERTAB))
82 mapsize = (SEGMAPSIZE*PTEPERTAB);
83 s->map = smalloc(mapsize*sizeof(Pte*));
88 s->mapsize = nelem(s->ssegmap);
107 if(i->s == s && s->ref == 1)
125 emap = &s->map[s->mapsize];
126 for(pp = s->map; pp < emap; pp++)
131 if(s->map != s->ssegmap)
139 relocateseg(Segment *s, ulong offset)
142 Pte *pte, **p, **endpte;
144 endpte = &s->map[s->mapsize];
145 for(p = s->map; p < endpte; p++) {
149 for(pg = pte->first; pg <= pte->last; pg++) {
157 dupseg(Segment **seg, int segno, int share)
171 switch(s->type&SG_TYPE) {
172 case SG_TEXT: /* New segment shares pte set */
178 n = newseg(s->type, s->base, s->size);
181 case SG_BSS: /* Just copy on write */
184 n = newseg(s->type, s->base, s->size);
187 case SG_DATA: /* Copy on write plus demand load info */
197 n = newseg(s->type, s->base, s->size);
201 n->fstart = s->fstart;
206 for(i = 0; i < size; i++)
208 n->map[i] = ptecpy(pte);
210 n->flushme = s->flushme;
225 segpage(Segment *s, Page *p)
231 if(p->va < s->base || p->va >= s->top)
234 off = p->va - s->base;
235 pte = &s->map[off/PTEMAPMEM];
239 pg = &(*pte)->pages[(off&(PTEMAPMEM-1))/BY2PG];
241 if(pg < (*pte)->first)
243 if(pg > (*pte)->last)
248 attachimage(int type, Chan *c, ulong base, ulong len)
252 /* reclaim any free channels from reclaimed segments */
253 if(imagealloc.nfreechan)
259 * Search the image cache for remains of the text from a previous
260 * or currently running incarnation
262 for(i = ihash(c->qid.path); i; i = i->hash) {
263 if(c->qid.path == i->qid.path) {
265 if(eqqid(c->qid, i->qid) &&
266 eqqid(c->mqid, i->mqid) &&
267 c->mchan == i->mchan &&
268 c->type == i->type) {
276 * imagereclaim dumps pages from the free list which are cached by image
277 * structures. This should free some image structures.
279 while(!(i = imagealloc.free)) {
286 imagealloc.free = i->next;
295 l = &ihash(c->qid.path);
302 /* Disaster after commit in exec */
307 i->s = newseg(type, base, len);
319 int calls; /* times imagereclaim was called */
320 int loops; /* times the main loop was run */
321 uvlong ticks; /* total time in the main loop */
322 uvlong maxt; /* longest time in main loop */
333 /* Somebody is already cleaning the page cache */
334 if(!canqlock(&imagealloc.ireclaim))
338 ticks = fastticks(nil);
341 * All the pages with images backing them are at the
342 * end of the list (see putpage) so start there and work
345 for(p = palloc.tail; p && p->image && n<1000; p = p->prev) {
346 if(p->ref == 0 && canlock(p)) {
354 ticks = fastticks(nil) - ticks;
357 irstats.ticks += ticks;
358 if(ticks > irstats.maxt)
359 irstats.maxt = ticks;
360 //print("T%llud+", ticks);
361 qunlock(&imagealloc.ireclaim);
365 * since close can block, this has to be called outside of
369 imagechanreclaim(void)
373 /* Somebody is already cleaning the image chans */
374 if(!canqlock(&imagealloc.fcreclaim))
378 * We don't have to recheck that nfreechan > 0 after we
379 * acquire the lock, because we're the only ones who decrement
380 * it (the other lock contender increments it), and there's only
381 * one of us thanks to the qlock above.
383 while(imagealloc.nfreechan > 0){
385 imagealloc.nfreechan--;
386 c = imagealloc.freechan[imagealloc.nfreechan];
391 qunlock(&imagealloc.fcreclaim);
405 l = &ihash(i->qid.path);
406 mkqid(&i->qid, ~0, ~0, QTFILE);
411 for(f = *l; f; f = f->hash) {
419 i->next = imagealloc.free;
422 /* defer freeing channel till we're out of spin lock's */
423 if(imagealloc.nfreechan == imagealloc.szfreechan){
424 imagealloc.szfreechan += NFREECHAN;
425 cp = malloc(imagealloc.szfreechan*sizeof(Chan*));
428 memmove(cp, imagealloc.freechan, imagealloc.nfreechan*sizeof(Chan*));
429 free(imagealloc.freechan);
430 imagealloc.freechan = cp;
432 imagealloc.freechan[imagealloc.nfreechan++] = c;
441 ibrk(ulong addr, int seg)
444 ulong newtop, newsize;
457 /* We may start with the bss overlapping the data */
459 if(seg != BSEG || up->seg[DSEG] == 0 || addr < up->seg[DSEG]->base) {
466 newtop = PGROUND(addr);
467 newsize = (newtop-s->base)/BY2PG;
468 if(newtop < s->top) {
470 * do not shrink a segment shared with other procs, as the
471 * to-be-freed address space may have been passed to the kernel
472 * already by another proc and is past the validaddr stage.
478 mfreeseg(s, newtop, (s->top-newtop)/BY2PG);
486 for(i = 0; i < NSEG; i++) {
488 if(ns == 0 || ns == s)
490 if(newtop >= ns->base && newtop < ns->top) {
496 if(newsize > (SEGMAPSIZE*PTEPERTAB)) {
500 mapsize = ROUND(newsize, PTEPERTAB)/PTEPERTAB;
501 if(mapsize > s->mapsize){
502 map = smalloc(mapsize*sizeof(Pte*));
503 memmove(map, s->map, s->mapsize*sizeof(Pte*));
504 if(s->map != s->ssegmap)
507 s->mapsize = mapsize;
517 * called with s->lk locked
520 mcountseg(Segment *s)
526 for(i = 0; i < s->mapsize; i++){
529 map = s->map[i]->pages;
530 for(j = 0; j < PTEPERTAB; j++)
538 * called with s->lk locked
541 mfreeseg(Segment *s, ulong start, int pages)
548 soff = start-s->base;
549 j = (soff&(PTEMAPMEM-1))/BY2PG;
553 for(i = soff/PTEMAPMEM; i < size; i++) {
557 pages -= PTEPERTAB-j;
561 while(j < PTEPERTAB) {
562 pg = s->map[i]->pages[j];
564 * We want to zero s->map[i]->page[j] and putpage(pg),
565 * but we have to make sure other processors flush the
566 * entry from their TLBs before the page is freed.
567 * We construct a list of the pages to be freed, zero
568 * the entries, then (below) call procflushseg, and call
569 * putpage on the whole list.
571 * Swapped-out pages don't appear in TLBs, so it's okay
572 * to putswap those pages before procflushseg.
581 s->map[i]->pages[j] = 0;
590 /* flush this seg in all other processes */
595 for(pg = list; pg != nil; pg = list){
602 isoverlap(Proc *p, ulong va, int len)
609 for(i = 0; i < NSEG; i++) {
613 if((newtop > ns->base && newtop <= ns->top) ||
614 (va >= ns->base && va < ns->top))
621 addphysseg(Physseg* new)
626 * Check not already entered and there is room
627 * for a new entry and the terminating null entry.
630 for(ps = physseg; ps->name; ps++){
631 if(strcmp(ps->name, new->name) == 0){
632 unlock(&physseglock);
636 if(ps-physseg >= nelem(physseg)-2){
637 unlock(&physseglock);
642 unlock(&physseglock);
648 isphysseg(char *name)
654 for(ps = physseg; ps->name; ps++){
655 if(strcmp(ps->name, name) == 0){
660 unlock(&physseglock);
665 segattach(Proc *p, ulong attr, char *name, ulong va, ulong len)
671 if(va != 0 && va >= USTKTOP)
674 validaddr((ulong)name, 1, 0);
675 vmemchr(name, 0, ~0);
677 for(sno = 0; sno < NSEG; sno++)
678 if(p->seg[sno] == nil && sno != ESEG)
685 * first look for a global segment with the
688 if(_globalsegattach != nil){
689 s = (*_globalsegattach)(p, name);
701 * Find a hole in the address space.
702 * Starting at the lowest possible stack address - len,
703 * check for an overlapping segment, and repeat at the
704 * base of that segment - len until either a hole is found
705 * or the address space is exhausted. Ensure that we don't
709 for (os = p->seg[SSEG]; os != nil; os = isoverlap(p, va, len)) {
718 if(va == 0 || va >= USTKTOP)
722 if(isoverlap(p, va, len) != nil)
725 for(ps = physseg; ps->name; ps++)
726 if(strcmp(name, ps->name) == 0)
734 attr &= ~SG_TYPE; /* Turn off what is not allowed */
735 attr |= ps->attr; /* Copy in defaults */
737 s = newseg(attr, va, len/BY2PG);
745 pteflush(Pte *pte, int s, int e)
750 for(i = s; i < e; i++) {
753 memset(p->cachectl, PG_TXTFLUSH, sizeof(p->cachectl));
758 syssegflush(ulong *arg)
763 int chunk, ps, pe, len;
769 s = seg(up, addr, 1);
780 pte = s->map[ps/PTEMAPMEM];
785 pe = (pe+BY2PG-1)&~(BY2PG-1);
793 pteflush(pte, ps/BY2PG, pe/BY2PG);
799 if(len > 0 && addr < s->top)
814 if(s == 0 || s->profile == 0)
817 s->profile[0] += TK2MS(1);
818 if(pc >= s->base && pc < s->top) {
820 s->profile[pc>>LRESPROF] += TK2MS(1);