Annotation of MiNT/src/mem.c, revision 1.1.1.3

1.1       root        1: /*
                      2: 
1.1.1.3 ! root        3: Copyright 1990,1991,1992 Eric R. Smith.
        !             4: 
        !             5: Copyright 1992,1993 Atari Corporation.
        !             6: 
        !             7: All rights reserved.
1.1       root        8: 
                      9: */
                     10: 
                     11: 
                     12: 
                     13: /*
                     14: 
                     15:  * mem.c:: routines for managing memory regions
                     16: 
                     17:  */
                     18: 
                     19: 
                     20: 
                     21: #include "mint.h"
                     22: 
1.1.1.3 ! root       23: #include "fasttext.h" /* for line A stuff */
        !            24: 
        !            25: 
        !            26: 
        !            27: #ifndef VgetSize
        !            28: 
        !            29: extern long xbios();
        !            30: 
        !            31: #define VgetSize(mode) xbios(91, (short)(mode))
        !            32: 
        !            33: #define Vsetmode(mode) xbios(88, (short)(mode))
        !            34: 
        !            35: #endif
        !            36: 
1.1       root       37: 
                     38: 
                     39: static long core_malloc P_((long, int));
                     40: 
1.1.1.3 ! root       41: static void core_free P_((long));
        !            42: 
1.1       root       43: 
                     44: 
                     45: /* macro for testing whether a memory region is free */
                     46: 
                     47: #define ISFREE(m) ((m)->links == 0)
                     48: 
                     49: 
                     50: 
                     51: /*
                     52: 
1.1.1.2   root       53:  * list of shared text regions currently being executed
                     54: 
                     55:  */
                     56: 
                     57: SHTEXT *text_reg = 0;
                     58: 
                     59: 
                     60: 
                     61: /*
                     62: 
1.1       root       63:  * initialize memory routines
                     64: 
                     65:  */
                     66: 
                     67: 
                     68: 
                     69: /* initial number of memory regions */
                     70: 
                     71: #define NREGIONS 512
                     72: 
                     73: 
                     74: 
                     75: /* number of new regions to allocate when the initial ones are used up */
                     76: 
                     77: #define NEWREGIONS 256
                     78: 
                     79: 
                     80: 
                     81: static MEMREGION use_regions[NREGIONS+1];
                     82: 
                     83: MEMREGION *rfreelist;
                     84: 
                     85: 
                     86: 
1.1.1.3 ! root       87: /* these variables are set in init_core(), and used in
        !            88: 
        !            89:  * init_mem()
        !            90: 
        !            91:  */
        !            92: 
        !            93: static ulong scrnsize, scrnplace;
        !            94: 
        !            95: static SCREEN *vscreen;
        !            96: 
        !            97: 
        !            98: 
1.1       root       99: void
                    100: 
                    101: init_mem()
                    102: 
                    103: {
                    104: 
                    105:        int i;
                    106: 
1.1.1.3 ! root      107:        MEMREGION *r;
        !           108: 
        !           109:        long newbase;
        !           110: 
1.1       root      111: 
                    112: 
                    113:        use_regions[NREGIONS].next = 0;
                    114: 
                    115:        for (i = 0; i < NREGIONS; i++) {
                    116: 
                    117:                use_regions[i].next = &use_regions[i+1];
                    118: 
                    119:        }
                    120: 
                    121:        rfreelist = use_regions;
                    122: 
                    123: 
                    124: 
                    125:        init_core();
                    126: 
                    127:        init_swap();
                    128: 
1.1.1.3 ! root      129: 
        !           130: 
        !           131:        init_tables();              /* initialize MMU constants */
        !           132: 
        !           133: 
        !           134: 
        !           135:        /* mark all the regions in the core & alt lists as "invalid" */
        !           136: 
        !           137:        for (r = *core; r; r = r->next) {
        !           138: 
        !           139:            mark_region(r,PROT_I);
        !           140: 
        !           141:        }
        !           142: 
        !           143:        for (r = *alt; r; r = r->next) {
        !           144: 
        !           145:            mark_region(r,PROT_I);
        !           146: 
        !           147:        }
        !           148: 
        !           149: 
        !           150: 
        !           151:        /* make sure the screen is set up properly */
        !           152: 
        !           153:        newbase = s_realloc(scrnsize);
        !           154: 
        !           155: 
        !           156: 
        !           157:        /* if we did get a new screen, point the new screen
        !           158: 
        !           159:         * at the right place after copying the data
        !           160: 
        !           161:         * if possible, save the screen to another buffer,
        !           162: 
        !           163:         * since if the new screen and old screen overlap
        !           164: 
        !           165:         * the blit will look very ugly.
        !           166: 
        !           167:         * Note that if the screen isn't moveable, then we set
        !           168: 
        !           169:         * scrnsize to a ridiculously large value, and so the
        !           170: 
        !           171:         * s_realloc above failed.
        !           172: 
        !           173:         */
        !           174: 
        !           175:        if (newbase) {
        !           176: 
        !           177:        /* find a free region for temp storage */
        !           178: 
        !           179:                for (r = *core; r; r = r->next) {
        !           180: 
        !           181:                        if (ISFREE(r) && r->len >= scrnsize)
        !           182: 
        !           183:                                break;
        !           184: 
        !           185:                }
        !           186: 
        !           187: 
        !           188: 
        !           189:                if (r) {
        !           190: 
        !           191:                        quickmove((char *)r->loc, (char *)scrnplace, scrnsize);
        !           192: 
        !           193:                        Setscreen((void *)r->loc, (void *)r->loc, -1);
        !           194: 
        !           195:                        Vsync();
        !           196: 
        !           197:                        quickmove((char *)newbase, (char *)r->loc, scrnsize);
        !           198: 
        !           199:                } else {
        !           200: 
        !           201:                        quickmove((char *)newbase, (char *)scrnplace, scrnsize);
        !           202: 
        !           203:                }
        !           204: 
        !           205:                Setscreen((void *)newbase, (void *)newbase, -1);
        !           206: 
        !           207:        /* fix the cursor */
        !           208: 
        !           209:                Cconws("\r\n"); 
        !           210: 
        !           211:        }
        !           212: 
1.1       root      213: }
                    214: 
                    215: 
                    216: 
                    217: /*
                    218: 
                    219:  * init_core(): initialize the core memory map (normal ST ram) and also
                    220: 
                    221:  * the alternate memory map (fast ram on the TT)
                    222: 
                    223:  */
                    224: 
                    225: 
                    226: 
                    227: static MEMREGION *_core_regions = 0, *_alt_regions = 0,
                    228: 
                    229:        *_ker_regions = 0;
                    230: 
                    231: 
                    232: 
                    233: MMAP core = &_core_regions;
                    234: 
                    235: MMAP alt = &_alt_regions;
                    236: 
                    237: MMAP ker = &_ker_regions;
                    238: 
                    239: 
                    240: 
                    241: /* note: add_region must adjust both the size and starting
                    242: 
                    243:  * address of the region being added so that memory is
                    244: 
                    245:  * always properly aligned
                    246: 
                    247:  */
                    248: 
                    249: 
                    250: 
                    251: int
                    252: 
                    253: add_region(map, place, size, mflags)
                    254: 
                    255:        MMAP map;
                    256: 
                    257:        ulong place, size;
                    258: 
                    259:        unsigned mflags;        /* initial flags for region */
                    260: 
                    261: {
                    262: 
1.1.1.3 ! root      263:        MEMREGION *m;
1.1       root      264: 
1.1.1.3 ! root      265:        ulong trimsize;
1.1       root      266: 
                    267: 
                    268: 
1.1.1.3 ! root      269:        TRACELOW(("add_region(map=%lx,place=%lx,size=%lx,flags=%x)",
1.1       root      270: 
1.1.1.3 ! root      271:                map,place,size,mflags));
1.1       root      272: 
                    273: 
                    274: 
                    275:        m = new_region();
                    276: 
                    277:        if (m == 0)
                    278: 
                    279:                return 0;       /* failure */
                    280: 
                    281:        m->links = 0;
                    282: 
                    283: 
                    284: 
1.1.1.3 ! root      285:        if (place & MASKBITS) {
        !           286: 
        !           287:            /* increase place & shorten size by the amount we're trimming */
        !           288: 
        !           289:            trimsize = (MASKBITS+1) - (place & MASKBITS);
        !           290: 
        !           291:            if (size <= trimsize) goto lose;
        !           292: 
        !           293:            size -= trimsize;
        !           294: 
        !           295:            place += trimsize;
        !           296: 
        !           297:        }
        !           298: 
        !           299: 
        !           300: 
        !           301:        /* now trim size DOWN to a multiple of pages */
        !           302: 
        !           303:        if (size & MASKBITS) size &= ~MASKBITS;
        !           304: 
        !           305: 
        !           306: 
        !           307:        /* only add if there's anything left */
        !           308: 
        !           309:        if (size) {
        !           310: 
        !           311:            m->len = size;
1.1       root      312: 
1.1.1.3 ! root      313:            m->loc = place;
        !           314: 
        !           315:            m->next = *map;
        !           316: 
        !           317:            m->mflags = mflags;
        !           318: 
        !           319:            *map = m;
        !           320: 
        !           321:        }
        !           322: 
        !           323:        else {
1.1       root      324: 
1.1.1.3 ! root      325:            /* succeed but don't do anything; dispose of region */
        !           326: 
        !           327: lose:      dispose_region(m);
        !           328: 
        !           329:        }
1.1       root      330: 
                    331:        return 1;       /* success */
                    332: 
                    333: }
                    334: 
                    335: 
                    336: 
                    337: static long
                    338: 
                    339: core_malloc(amt, mode)
                    340: 
                    341:        long amt;
                    342: 
                    343:        int mode;
                    344: 
                    345: {
                    346: 
                    347:        static int mxalloc = -1;        /* does GEMDOS know about Mxalloc? */
                    348: 
                    349:        long ret;
                    350: 
                    351: 
                    352: 
                    353:        if (mxalloc < 0) {
                    354: 
                    355:                ret = (long)Mxalloc(-1L, 0);
                    356: 
                    357:                if (ret == -32) mxalloc = 0;    /* unknown function */
                    358: 
                    359:                else if (ret >= 0) mxalloc = 1;
                    360: 
                    361:                else {
                    362: 
                    363:                        ALERT("GEMDOS returned %ld from Mxalloc", ret);
                    364: 
                    365:                        mxalloc = 0;
                    366: 
                    367:                }
                    368: 
                    369:        }
                    370: 
                    371:        if (mxalloc)
                    372: 
                    373:                return Mxalloc(amt, mode);
                    374: 
                    375:        else if (mode == 1)
                    376: 
                    377:                return 0L;
                    378: 
                    379:        else
                    380: 
                    381:                return Malloc(amt);
                    382: 
                    383: }
                    384: 
                    385: 
                    386: 
1.1.1.3 ! root      387: static void
        !           388: 
        !           389: core_free(where)
        !           390: 
        !           391:        long where;
        !           392: 
        !           393: {
        !           394: 
        !           395:        Mfree((void *)where);
        !           396: 
        !           397: }
        !           398: 
        !           399: 
        !           400: 
1.1       root      401: void
                    402: 
                    403: init_core()
                    404: 
                    405: {
                    406: 
1.1.1.3 ! root      407:        extern int FalconVideo; /* set in main.c */
        !           408: 
        !           409:        int scrndone = 0;
        !           410: 
1.1       root      411:        ulong size;
                    412: 
                    413:        ulong place;
                    414: 
1.1.1.3 ! root      415:        ulong temp;
        !           416: 
1.1       root      417:        void *tossave;
                    418: 
                    419: 
                    420: 
                    421:        tossave = (void *)core_malloc((long)TOS_MEM, 0);
                    422: 
                    423:        if (!tossave) {
                    424: 
                    425:                FATAL("Not enough memory to run MiNT");
                    426: 
                    427:        }
                    428: 
                    429: 
                    430: 
                    431: /* initialize kernel memory */
                    432: 
                    433:        place = (ulong)core_malloc(KERNEL_MEM, 3);
                    434: 
                    435:        if (place != 0) {
                    436: 
1.1.1.3 ! root      437:                nalloc_arena_add((void *)place,KERNEL_MEM);
        !           438: 
        !           439:        }
        !           440: 
        !           441: 
        !           442: 
        !           443: /*
        !           444: 
        !           445:  * find out where the screen is. We want to manage the screen
        !           446: 
        !           447:  * memory along with all the other memory, so that Srealloc()
        !           448: 
        !           449:  * can be used by the XBIOS to allocate screens from the
        !           450: 
        !           451:  * end of memory -- this avoids fragmentation problems when
        !           452: 
        !           453:  * changing resolutions.
        !           454: 
        !           455:  */
        !           456: 
        !           457: /* Note, however, that some graphics boards (e.g. Matrix)
        !           458: 
        !           459:  * are unable to change the screen address. We fake out the
        !           460: 
        !           461:  * rest of our code by pretending to have a really huge
        !           462: 
        !           463:  * screen that can't be changed.
        !           464: 
        !           465:  */
        !           466: 
        !           467:        scrnplace = (long)Physbase();
        !           468: 
        !           469: 
        !           470: 
        !           471:        vscreen = (SCREEN *)((char *)lineA0() - 346);
        !           472: 
        !           473:        if (FalconVideo) {
        !           474: 
        !           475:        /* the Falcon can tell us the screen size */
        !           476: 
        !           477:            scrnsize = VgetSize(Vsetmode(-1));
        !           478: 
        !           479:        } else {
        !           480: 
        !           481:        /* otherwise, use the line A variables */
        !           482: 
        !           483:            scrnsize = (vscreen->maxy+1)*(long)vscreen->linelen;
        !           484: 
        !           485:        }
        !           486: 
        !           487: 
        !           488: 
        !           489: /* check for a graphics card with fixed screen location */
        !           490: 
        !           491: #define phys_top_st (*(ulong *)0x42eL)
        !           492: 
        !           493: 
        !           494: 
        !           495:        if (scrnplace >= phys_top_st) {
        !           496: 
        !           497: /* screen isn't in ST RAM */
        !           498: 
        !           499:                scrnsize = 0x7fffffffUL;
        !           500: 
        !           501:                scrndone = 1;
        !           502: 
        !           503:        } else {
        !           504: 
        !           505:                temp = (ulong)core_malloc(scrnsize+256L, 0);
        !           506: 
        !           507:                if (temp) {
        !           508: 
        !           509:                        (void)Setscreen((void *)-1L,
        !           510: 
        !           511:                                (void *)((temp+511)&(0xffffff00L)), -1);
        !           512: 
        !           513:                        if ((long)Physbase() != (temp+511)&(0xffffff00L)) {
        !           514: 
        !           515:                                scrnsize = 0x7fffffffUL;
        !           516: 
        !           517:                                scrndone = 1;
        !           518: 
        !           519:                        }
        !           520: 
        !           521:                        (void)Setscreen((void *)-1L, (void *)scrnplace, -1);
        !           522: 
        !           523:                        core_free(temp);
        !           524: 
        !           525:                }
1.1       root      526: 
                    527:        }
                    528: 
                    529: 
                    530: 
                    531: /* initialize ST RAM */
                    532: 
                    533:        size = (ulong)core_malloc(-1L, 0);
                    534: 
                    535:        while (size > 0) {
                    536: 
                    537:                place = (ulong)core_malloc(size, 0);
                    538: 
1.1.1.3 ! root      539:                if (place + size == scrnplace) {
        !           540: 
        !           541:                        size += scrnsize;
        !           542: 
        !           543:                        scrndone = 1;
        !           544: 
        !           545:                }
        !           546: 
1.1       root      547:                if (!add_region(core, place, size, M_CORE))
                    548: 
                    549:                        FATAL("init_mem: unable to add a region");
                    550: 
                    551:                size = (ulong)core_malloc(-1L, 0);
                    552: 
                    553:        }
                    554: 
                    555: 
                    556: 
1.1.1.3 ! root      557:        if (!scrndone) {
        !           558: 
        !           559:                (void)add_region(core, scrnplace, scrnsize, M_CORE);
        !           560: 
        !           561:        }
        !           562: 
        !           563: 
        !           564: 
1.1       root      565: /* initialize alternate RAM */
                    566: 
                    567:        size = (ulong)core_malloc(-1L, 1);
                    568: 
                    569:        while (size > 0) {
                    570: 
                    571:                place = (ulong)core_malloc(size, 1);
                    572: 
                    573:                if (!add_region(alt, place, size, M_ALT))
                    574: 
                    575:                        FATAL("init_mem: unable to add a region");
                    576: 
                    577:                size = (ulong)core_malloc(-1L, 1);
                    578: 
                    579:        }
                    580: 
                    581: 
                    582: 
                    583:        (void)Mfree(tossave);           /* leave some memory for TOS to use */
                    584: 
                    585: }
                    586: 
                    587: 
                    588: 
                    589: /*
                    590: 
                    591:  * init_swap(): initialize the swap area; for now, this does nothing
                    592: 
                    593:  */
                    594: 
                    595: 
                    596: 
                    597: MEMREGION *_swap_regions = 0;
                    598: 
                    599: MMAP swap = &_swap_regions;
                    600: 
                    601: 
                    602: 
                    603: void
                    604: 
                    605: init_swap()
                    606: 
                    607: {
                    608: 
                    609: }
                    610: 
                    611: 
                    612: 
                    613: /*
                    614: 
                    615:  * routines for allocating/deallocating memory regions
                    616: 
                    617:  */
                    618: 
                    619: 
                    620: 
                    621: /*
                    622: 
                    623:  * new_region returns a new memory region descriptor, or NULL
                    624: 
                    625:  */
                    626: 
                    627: 
                    628: 
                    629: MEMREGION *
                    630: 
                    631: new_region()
                    632: 
                    633: {
                    634: 
                    635:        MEMREGION *m, *newfrees;
                    636: 
                    637:        int i;
                    638: 
                    639: 
                    640: 
                    641:        m = rfreelist;
                    642: 
                    643:        if (!m) {
                    644: 
                    645:                ALERT("new_region: ran out of free regions");
                    646: 
                    647:                return 0;
                    648: 
                    649:        }
                    650: 
                    651:        assert(ISFREE(m));
                    652: 
                    653:        rfreelist = m->next;
                    654: 
                    655:        m->next = 0;
                    656: 
                    657: 
                    658: 
                    659: /* if we're running low on free regions, allocate some more
                    660: 
                    661:  * we have to do this with at least 1 free region left so that get_region
                    662: 
                    663:  * has a chance of working
                    664: 
                    665:  */
                    666: 
                    667:        if (rfreelist && !rfreelist->next) {
                    668: 
                    669:                MEMREGION *newstuff;
                    670: 
                    671: 
                    672: 
1.1.1.3 ! root      673:                TRACELOW(("get_region: getting new region descriptors"));
1.1       root      674: 
1.1.1.3 ! root      675:                newstuff = get_region(ker, NEWREGIONS*SIZEOF(MEMREGION), PROT_S);
1.1       root      676: 
                    677:                if (!newstuff)
                    678: 
1.1.1.3 ! root      679:                    newstuff = get_region(alt,NEWREGIONS*SIZEOF(MEMREGION), PROT_S);
1.1       root      680: 
                    681:                if (!newstuff)
                    682: 
1.1.1.3 ! root      683:                    newstuff = get_region(core, NEWREGIONS*SIZEOF(MEMREGION), PROT_S);
1.1       root      684: 
                    685:                newfrees = newstuff ? (MEMREGION *)newstuff->loc : 0;
                    686: 
                    687:                if (newfrees) {
                    688: 
                    689:                        newfrees[NEWREGIONS-1].next = 0;
                    690: 
                    691:                        newfrees[NEWREGIONS-1].links = 0;
                    692: 
                    693:                        for (i = 0; i < NEWREGIONS-1; i++) {
                    694: 
                    695:                                newfrees[i].next = &newfrees[i+1];
                    696: 
                    697:                                newfrees[i].links = 0;
                    698: 
                    699:                        }
                    700: 
                    701:                        rfreelist = newfrees;
                    702: 
                    703:                } else {
                    704: 
1.1.1.2   root      705:                        DEBUG(("couldn't get new region descriptors!"));
1.1       root      706: 
                    707:                }
                    708: 
                    709:        }
                    710: 
                    711: 
                    712: 
                    713:        return m;
                    714: 
                    715: }
                    716: 
                    717: 
                    718: 
                    719: /*
                    720: 
                    721:  * dispose_region destroys a memory region descriptor
                    722: 
                    723:  */
                    724: 
                    725: 
                    726: 
                    727: void
                    728: 
                    729: dispose_region(m)
                    730: 
                    731:        MEMREGION *m;
                    732: 
                    733: {
                    734: 
                    735:        m->next = rfreelist;
                    736: 
                    737:        rfreelist = m;
                    738: 
                    739: }
                    740: 
                    741: 
                    742: 
                    743: /*
                    744: 
1.1.1.3 ! root      745:  * change_prot_status: change the status of a region to 'newmode'.  We're
        !           746: 
        !           747:  * given its starting address, not its region structure pointer, so we have
        !           748: 
        !           749:  * to find the region pointer; since this is illegal if proc doesn't own
        !           750: 
        !           751:  * the region, we know we'll find the region struct pointer in proc->mem.
        !           752: 
        !           753:  *
        !           754: 
        !           755:  * If the proc doesn't own it, you get EACCDN.  There are no other errors.
        !           756: 
        !           757:  * God help you if newmode isn't legal!
        !           758: 
        !           759:  */
        !           760: 
        !           761: 
        !           762: 
        !           763: long
        !           764: 
        !           765: change_prot_status(proc,start,newmode)
        !           766: 
        !           767: PROC *proc;
        !           768: 
        !           769: long start;
        !           770: 
        !           771: int newmode;
        !           772: 
        !           773: {
        !           774: 
        !           775:     MEMREGION **mr;
        !           776: 
        !           777:     int i;
        !           778: 
        !           779: 
        !           780: 
        !           781:     /* return EACCDN if you don't own the region in question */
        !           782: 
        !           783:     if (!proc->mem) return EACCDN;
        !           784: 
        !           785: 
        !           786: 
        !           787:     for (mr = proc->mem, i = 0; i < proc->num_reg; i++, mr++) {
        !           788: 
        !           789:        if ((*mr)->loc == start) goto found;
        !           790: 
        !           791:     }
        !           792: 
        !           793:     return EACCDN;
        !           794: 
        !           795: 
        !           796: 
        !           797: found:
        !           798: 
        !           799:     mark_region(*mr,newmode);
        !           800: 
        !           801:     return E_OK;
        !           802: 
        !           803: }
        !           804: 
        !           805: 
        !           806: 
        !           807: /*
        !           808: 
1.1       root      809:  * virtaddr
                    810: 
                    811:  * attach_region(proc, reg): attach the region to the given process:
                    812: 
                    813:  * returns the address at which it was attached, or NULL if the process
                    814: 
                    815:  * cannot attach more regions. The region link count is incremented if
                    816: 
                    817:  * the attachment is successful.
                    818: 
1.1.1.3 ! root      819:  *
        !           820: 
        !           821:  * Calls mark_region on the region to mark this process as an owner.
        !           822: 
        !           823:  * This is redundant since it executes a complicated no-op for every
        !           824: 
        !           825:  * other process in the system.  Sigh.
        !           826: 
1.1       root      827:  */
                    828: 
                    829: 
                    830: 
                    831: virtaddr
                    832: 
                    833: attach_region(proc, reg)
                    834: 
                    835:        PROC *proc;
                    836: 
                    837:        MEMREGION *reg;
                    838: 
                    839: {
                    840: 
                    841:        int i;
                    842: 
                    843:        MEMREGION **newmem;
                    844: 
                    845:        virtaddr *newaddr;
                    846: 
                    847: 
                    848: 
1.1.1.3 ! root      849:        TRACELOW(("attach_region %lx len %lx to pid %d",
        !           850: 
        !           851:            reg->loc, reg->len, proc->pid));
        !           852: 
        !           853: 
        !           854: 
1.1       root      855:        if (!reg || !reg->loc) {
                    856: 
                    857:                ALERT("attach_region: attaching a null region??");
                    858: 
                    859:                return 0;
                    860: 
                    861:        }
                    862: 
1.1.1.3 ! root      863: 
        !           864: 
        !           865: again:
        !           866: 
1.1       root      867:        for (i = 0; i < proc->num_reg; i++) {
                    868: 
                    869:                if (!proc->mem[i]) {
                    870: 
                    871:                        assert(proc->addr[i] == 0);
                    872: 
                    873:                        reg->links++;
                    874: 
                    875:                        proc->mem[i] = reg;
                    876: 
                    877:                        proc->addr[i] = (virtaddr) reg->loc;
                    878: 
1.1.1.3 ! root      879:                        mark_region(reg,PROT_NOCHANGE);
        !           880: 
1.1       root      881:                        return proc->addr[i];
                    882: 
                    883:                }
                    884: 
                    885:        }
                    886: 
                    887: 
                    888: 
                    889: /* Hmmm, OK, we have to expand the process' memory table */
                    890: 
1.1.1.3 ! root      891:        TRACELOW(("Expanding process memory table"));
1.1       root      892: 
                    893:        i = proc->num_reg + NUM_REGIONS;
                    894: 
                    895: 
                    896: 
                    897:        newmem = kmalloc(i * SIZEOF(MEMREGION *));
                    898: 
                    899:        newaddr = kmalloc(i * SIZEOF(virtaddr));
                    900: 
                    901: 
                    902: 
                    903:        if (newmem && newaddr) {
                    904: 
1.1.1.3 ! root      905:        /*
        !           906: 
        !           907:         * We have to use temps while allocating and freeing mem
        !           908: 
        !           909:         * and addr so the memory protection code won't walk this
        !           910: 
        !           911:         * process' memory list in the middle.
        !           912: 
        !           913:         */
        !           914: 
        !           915:                void *pmem, *paddr;
        !           916: 
        !           917: 
        !           918: 
1.1       root      919:        /* copy over the old address mapping */
                    920: 
                    921:                for (i = 0; i < proc->num_reg; i++) {
                    922: 
                    923:                        newmem[i] = proc->mem[i];
                    924: 
                    925:                        newaddr[i] = proc->addr[i];
                    926: 
                    927:                        if (newmem[i] == 0)
                    928: 
                    929:                                assert(newaddr[i] == 0);
                    930: 
                    931:                }
                    932: 
                    933:        /* initialize the rest of the tables */
                    934: 
                    935:                for(; i < proc->num_reg + NUM_REGIONS; i++) {
                    936: 
                    937:                        newmem[i] = 0;
                    938: 
                    939:                        newaddr[i] = 0;
                    940: 
                    941:                }
                    942: 
1.1.1.3 ! root      943:        /* free the old tables (carefully! for memory protection) */
        !           944: 
        !           945:                pmem = proc->mem;
1.1       root      946: 
1.1.1.3 ! root      947:                paddr = proc->addr;
        !           948: 
        !           949:                proc->mem = NULL;
        !           950: 
        !           951:                proc->addr = NULL;
        !           952: 
        !           953:                kfree(pmem); kfree(paddr);
1.1       root      954: 
                    955:                proc->mem = newmem;
                    956: 
                    957:                proc->addr = newaddr;
                    958: 
                    959:                proc->num_reg += NUM_REGIONS;
                    960: 
1.1.1.3 ! root      961:        /* this time we will succeed */
1.1       root      962: 
1.1.1.3 ! root      963:                goto again;
1.1       root      964: 
1.1.1.3 ! root      965:        } else {
1.1       root      966: 
1.1.1.3 ! root      967:                if (newmem) kfree(newmem);
1.1       root      968: 
1.1.1.3 ! root      969:                if (newaddr) kfree(newaddr);
1.1       root      970: 
1.1.1.3 ! root      971:                DEBUG(("attach_region: failed"));
1.1       root      972: 
1.1.1.3 ! root      973:                return 0;
1.1       root      974: 
1.1.1.3 ! root      975:        }
1.1       root      976: 
                    977: }
                    978: 
                    979: 
                    980: 
                    981: /*
                    982: 
                    983:  * detach_region(proc, reg): remove region from the procedure's address
                    984: 
                    985:  * space. If no more processes reference the region, return it to the
                    986: 
                    987:  * system. Note that we search backwards, so that the most recent
                    988: 
                    989:  * attachment of memory gets detached!
                    990: 
1.1.1.3 ! root      991:  *
        !           992: 
        !           993:  * Calls mark_region on the region to mark this process as a non-owner.
        !           994: 
        !           995:  * This is redundant since it executes a complicated no-op for every
        !           996: 
        !           997:  * other process in the system.  Sigh.
        !           998: 
1.1       root      999:  */
                   1000: 
                   1001: 
                   1002: 
                   1003: void
                   1004: 
                   1005: detach_region(proc, reg)
                   1006: 
                   1007:        PROC *proc;
                   1008: 
                   1009:        MEMREGION *reg;
                   1010: 
                   1011: {
                   1012: 
                   1013:        int i;
                   1014: 
                   1015: 
                   1016: 
                   1017:        if (!reg) return;
                   1018: 
1.1.1.3 ! root     1019: 
        !          1020: 
        !          1021:        TRACELOW(("detach_region %lx len %lx from pid %d",
        !          1022: 
        !          1023:            reg->loc, reg->len, proc->pid));
        !          1024: 
        !          1025: 
        !          1026: 
1.1       root     1027:        for (i = proc->num_reg - 1; i >= 0; i--) {
                   1028: 
                   1029:                if (proc->mem[i] == reg) {
                   1030: 
                   1031:                        reg->links--;
                   1032: 
                   1033:                        proc->mem[i] = 0; proc->addr[i] = 0;
                   1034: 
                   1035:                        if (reg->links == 0) {
                   1036: 
                   1037:                                free_region(reg);
                   1038: 
                   1039:                        }
                   1040: 
1.1.1.3 ! root     1041:                        else {
        !          1042: 
        !          1043:                                /* cause curproc's table to be updated */
        !          1044: 
        !          1045:                                mark_region(reg,PROT_NOCHANGE);
        !          1046: 
        !          1047:                        }
        !          1048: 
1.1       root     1049:                        return;
                   1050: 
                   1051:                }
                   1052: 
                   1053:        }
                   1054: 
1.1.1.2   root     1055:        DEBUG(("detach_region: region not attached"));
1.1       root     1056: 
                   1057: }
                   1058: 
                   1059: 
                   1060: 
                   1061: /*
                   1062: 
1.1.1.3 ! root     1063:  * get_region(MMAP map, ulong size, int mode) -- allocate a new region of the
1.1       root     1064: 
                   1065:  * given size in the given memory map. if no region big enough is available,
                   1066: 
                   1067:  * return NULL, otherwise return a pointer to the region.
                   1068: 
1.1.1.3 ! root     1069:  * "mode" tells us about memory protection modes
        !          1070: 
        !          1071:  *
        !          1072: 
1.1       root     1073:  * the "links" field in the region is set to 1
                   1074: 
                   1075:  *
                   1076: 
                   1077:  * BEWARE: new_region may call get_region (indirectly), so we have to be
                   1078: 
                   1079:  * _very_ careful with re-entrancy in this function
                   1080: 
                   1081:  */
                   1082: 
                   1083: 
                   1084: 
                   1085: MEMREGION *
                   1086: 
1.1.1.3 ! root     1087: get_region(map, size, mode)
1.1       root     1088: 
                   1089:        MMAP map;
                   1090: 
                   1091:        ulong size;
                   1092: 
1.1.1.3 ! root     1093:        int mode;
        !          1094: 
1.1       root     1095: {
                   1096: 
                   1097:        MEMREGION *m, *n;
                   1098: 
                   1099: 
                   1100: 
1.1.1.3 ! root     1101:        TRACELOW(("get_region(%s,%lx,%x)",
        !          1102: 
        !          1103:                (map == ker ? "ker" : (map == core ? "core" : "alt")),
        !          1104: 
        !          1105:                size, mode));
        !          1106: 
        !          1107: 
        !          1108: 
1.1       root     1109: /* precautionary measures */
                   1110: 
                   1111:        if (size == 0) {
                   1112: 
1.1.1.2   root     1113:                DEBUG(("request for 0 bytes??"));
1.1       root     1114: 
                   1115:                size = 1;
                   1116: 
                   1117:        }
                   1118: 
                   1119: 
                   1120: 
                   1121:        size = ROUND(size);
                   1122: 
                   1123: 
                   1124: 
                   1125:        n = *map;
                   1126: 
                   1127: 
                   1128: 
                   1129:        sanity_check(map);
                   1130: 
                   1131: /* exact matches are likely to be rare, so we pre-allocate a new
                   1132: 
                   1133:  * region here; this helps us to avoid re-entrancy problems
                   1134: 
                   1135:  * when new_region calls get_region
                   1136: 
                   1137:  */
                   1138: 
                   1139:        m = new_region();
                   1140: 
                   1141: 
                   1142: 
                   1143:        while (n) {
                   1144: 
                   1145:                if (ISFREE(n)) {
                   1146: 
                   1147:                        if (n->len == size) {
                   1148: 
                   1149:                                if (m) dispose_region(m);
                   1150: 
                   1151:                                n->links++;
                   1152: 
1.1.1.3 ! root     1153:                                goto win;
1.1       root     1154: 
                   1155:                        }
                   1156: 
                   1157:                        else if (n->len > size) {
                   1158: 
                   1159: /* split a new region, 'm', which will contain the free bytes after n */
                   1160: 
                   1161:                                if (m) {
                   1162: 
                   1163:                                        m->next = n->next;
                   1164: 
                   1165:                                        n->next = m;
                   1166: 
                   1167:                                        m->mflags = n->mflags & M_MAP;
                   1168: 
                   1169:                                        m->loc = n->loc + size;
                   1170: 
                   1171:                                        m->len = n->len - size;
                   1172: 
                   1173:                                        n->len = size;
                   1174: 
                   1175:                                        n->links++;
                   1176: 
1.1.1.3 ! root     1177:                                        goto win;
1.1       root     1178: 
                   1179:                                } else {
                   1180: 
1.1.1.2   root     1181:                                    DEBUG(("get_region: no regions left"));
1.1       root     1182: 
                   1183:                                    return 0;
                   1184: 
                   1185:                                }
                   1186: 
                   1187:                        }
                   1188: 
                   1189:                }
                   1190: 
                   1191:                n = n->next;
                   1192: 
                   1193:        }
                   1194: 
                   1195: 
                   1196: 
                   1197:        if (m)
                   1198: 
                   1199:                dispose_region(m);
                   1200: 
1.1.1.3 ! root     1201: 
        !          1202: 
        !          1203:        TRACELOW(("get_region: no memory left in this map"));
        !          1204: 
1.1       root     1205:        return NULL;
                   1206: 
1.1.1.3 ! root     1207: 
        !          1208: 
        !          1209: win:
        !          1210: 
        !          1211:        mark_region(n, mode & PROT_PROTMODE);
        !          1212: 
        !          1213:        if (mode & M_KEEP) n->mflags |= M_KEEP;
        !          1214: 
        !          1215: 
        !          1216: 
        !          1217:        return n;
        !          1218: 
1.1       root     1219: }
                   1220: 
                   1221: 
                   1222: 
                   1223: /*
                   1224: 
                   1225:  * free_region(MEMREGION *reg): free the indicated region. The map
                   1226: 
                   1227:  * in which the region is contained is given by reg->mflags.
                   1228: 
                   1229:  * the caller is responsible for making sure that the region
                   1230: 
                   1231:  * really should be freed, i.e. that reg->links == 0.
                   1232: 
1.1.1.2   root     1233:  *
                   1234: 
                   1235:  * special things to do:
                   1236: 
                   1237:  * if the region is a shared text region, we must close the
                   1238: 
                   1239:  * associated file descriptor
                   1240: 
1.1       root     1241:  */
                   1242: 
                   1243: 
                   1244: 
                   1245: void
                   1246: 
                   1247: free_region(reg)
                   1248: 
                   1249:        MEMREGION *reg;
                   1250: 
                   1251: {
                   1252: 
                   1253:        MMAP map;
                   1254: 
                   1255:        MEMREGION *m;
                   1256: 
1.1.1.2   root     1257:        SHTEXT *s, **old;
                   1258: 
1.1       root     1259: 
                   1260: 
                   1261:        if (!reg) return;
                   1262: 
                   1263: 
                   1264: 
                   1265:        assert(ISFREE(reg));
                   1266: 
                   1267: 
                   1268: 
1.1.1.2   root     1269:        if (reg->mflags & M_SHTEXT) {
                   1270: 
                   1271:                TRACE(("freeing shared text region"));
                   1272: 
                   1273:                old = &text_reg;
                   1274: 
                   1275:                for(;;) {
                   1276: 
                   1277:                        s = *old;
                   1278: 
                   1279:                        if (!s) break;
                   1280: 
                   1281:                        if (s->text == reg) {
                   1282: 
1.1.1.3 ! root     1283:                                if (s->f)
        !          1284: 
        !          1285:                                        do_close(s->f);
1.1.1.2   root     1286: 
                   1287:                                *old = s->next;
                   1288: 
                   1289:                                kfree(s);
                   1290: 
                   1291:                                break;
                   1292: 
                   1293:                        }
                   1294: 
                   1295:                        old = &s->next;
                   1296: 
                   1297:                }
                   1298: 
                   1299:                if (!s) {
                   1300: 
                   1301:                    DEBUG(("No shared text entry for M_SHTEXT region??"));
                   1302: 
                   1303:                }
                   1304: 
                   1305:        }
                   1306: 
                   1307: 
                   1308: 
1.1       root     1309:        if (reg->mflags & M_CORE)
                   1310: 
                   1311:                map = core;
                   1312: 
                   1313:        else if (reg->mflags & M_ALT)
                   1314: 
                   1315:                map = alt;
                   1316: 
                   1317:        else if (reg->mflags & M_KER)
                   1318: 
                   1319:                map = ker;
                   1320: 
                   1321:        else {
                   1322: 
                   1323:                FATAL("free_region: region flags not valid (%x)", reg->mflags);
                   1324: 
                   1325:        }
                   1326: 
                   1327:        reg->mflags &= M_MAP;
                   1328: 
                   1329: 
                   1330: 
1.1.1.3 ! root     1331: /* unhook any vectors pointing into this region */
        !          1332: 
        !          1333:        unlink_vectors(reg->loc, reg->loc + reg->len);
1.1       root     1334: 
                   1335: 
1.1.1.3 ! root     1336: 
        !          1337: /* BUG(?): should invalidate caches entries - a copyback cache could stuff
1.1.1.2   root     1338: 
                   1339:  * things into freed memory.
                   1340: 
1.1.1.3 ! root     1341:  *     cinv(reg->loc, reg->len);
1.1.1.2   root     1342: 
                   1343:  */
                   1344: 
1.1.1.3 ! root     1345:        m = *map;
        !          1346: 
        !          1347:        assert(m);
        !          1348: 
        !          1349: 
        !          1350: 
        !          1351:        /* MEMPROT: invalidate */
        !          1352: 
        !          1353:        if (map == core || map == alt)
        !          1354: 
        !          1355:            mark_region(reg,PROT_I);
        !          1356: 
        !          1357: 
        !          1358: 
1.1       root     1359:        if (m == reg) goto merge_after;
                   1360: 
                   1361: 
                   1362: 
                   1363: /* merge previous region if it's free and contiguous with 'reg' */
                   1364: 
                   1365: 
                   1366: 
                   1367: /* first, we find the region */
                   1368: 
                   1369:        while (m && m->next != reg)
                   1370: 
                   1371:                m = m->next;
                   1372: 
                   1373: 
                   1374: 
1.1.1.2   root     1375:        if (m == NULL) {
                   1376: 
                   1377:                FATAL("couldn't find region %lx: loc: %lx len: %ld",
                   1378: 
                   1379:                        reg, reg->loc, reg->len);
                   1380: 
                   1381:        }
1.1       root     1382: 
                   1383: 
                   1384: 
                   1385:        if (ISFREE(m) && (m->loc + m->len == reg->loc)) {
                   1386: 
                   1387:                m->len += reg->len;
                   1388: 
                   1389:                assert(m->next == reg);
                   1390: 
                   1391:                m->next = reg->next;
                   1392: 
                   1393:                reg->next = 0;
                   1394: 
                   1395:                dispose_region(reg);
                   1396: 
                   1397:                reg = m;
                   1398: 
                   1399:        }
                   1400: 
                   1401: 
                   1402: 
                   1403: /* merge next region if it's free and contiguous with 'reg' */
                   1404: 
                   1405: merge_after:
                   1406: 
                   1407:        m = reg->next;
                   1408: 
                   1409:        if (m && ISFREE(m) && reg->loc + reg->len == m->loc) {
                   1410: 
                   1411:                reg->len += m->len;
                   1412: 
                   1413:                reg->next = m->next;
                   1414: 
                   1415:                m->next = 0;
                   1416: 
                   1417:                dispose_region(m);
                   1418: 
                   1419:        }
                   1420: 
                   1421: 
                   1422: 
                   1423:        sanity_check(map);
                   1424: 
                   1425: }
                   1426: 
                   1427: 
                   1428: 
                   1429: /*
                   1430: 
                   1431:  * shrink_region(MEMREGION *reg, ulong newsize):
                   1432: 
                   1433:  *   shrink region 'reg', so that it is now 'newsize' bytes long.
                   1434: 
                   1435:  *   if 'newsize' is bigger than the region's current size, return EGSBF;
                   1436: 
                   1437:  *   otherwise return 0.
                   1438: 
                   1439:  */
                   1440: 
                   1441: 
                   1442: 
                   1443: long
                   1444: 
                   1445: shrink_region(reg, newsize)
                   1446: 
                   1447:        MEMREGION *reg;
                   1448: 
                   1449:        ulong newsize;
                   1450: 
                   1451: {
                   1452: 
                   1453:        MEMREGION *n;
                   1454: 
                   1455:        ulong diff;
                   1456: 
                   1457: 
                   1458: 
                   1459: 
                   1460: 
                   1461:        newsize = ROUND(newsize);
                   1462: 
                   1463: 
                   1464: 
                   1465:        assert(reg->links > 0);
                   1466: 
                   1467: 
                   1468: 
1.1.1.2   root     1469:        if (!(reg->mflags & (M_CORE | M_ALT | M_KER))) {
1.1       root     1470: 
                   1471:                FATAL("shrink_region: bad region flags (%x)", reg->mflags);
                   1472: 
                   1473:        }
                   1474: 
                   1475: 
                   1476: 
                   1477: /* shrinking to 0 is the same as freeing */
                   1478: 
                   1479:        if (newsize == 0) {
                   1480: 
                   1481:                detach_region(curproc, reg);
                   1482: 
                   1483:                return 0;
                   1484: 
                   1485:        }
                   1486: 
                   1487: 
                   1488: 
                   1489: /* if new size is the same as old size, don't do anything */
                   1490: 
                   1491:        if (newsize == reg->len) {
                   1492: 
                   1493:                return 0;       /* nothing to do */
                   1494: 
                   1495:        }
                   1496: 
                   1497: 
                   1498: 
                   1499:        if (newsize > reg->len) {
                   1500: 
1.1.1.2   root     1501:                DEBUG(("shrink_region: request to make region bigger"));
1.1       root     1502: 
                   1503:                return EGSBF;   /* growth failure */
                   1504: 
                   1505:        }
                   1506: 
                   1507: 
                   1508: 
                   1509: /* OK, we're going to free (reg->len - newsize) bytes at the end of
                   1510: 
                   1511:    this block. If the block after us is already free, simply add the
                   1512: 
                   1513:    space to that block.
                   1514: 
                   1515:  */
                   1516: 
                   1517:        n = reg->next;
                   1518: 
                   1519:        diff = reg->len - newsize;
                   1520: 
                   1521: 
                   1522: 
                   1523:        if (n && ISFREE(n) && reg->loc + reg->len == n->loc) {
                   1524: 
                   1525:                reg->len = newsize;
                   1526: 
                   1527:                n->loc -= diff;
                   1528: 
                   1529:                n->len += diff;
                   1530: 
1.1.1.3 ! root     1531:                /* MEMPROT: invalidate the second half */
        !          1532: 
        !          1533:                /* (part of it is already invalid; that's OK) */
        !          1534: 
        !          1535:                mark_region(n,PROT_I);
        !          1536: 
        !          1537: 
        !          1538: 
1.1       root     1539:                return 0;
                   1540: 
                   1541:        }
                   1542: 
                   1543:        else {
                   1544: 
                   1545:                n = new_region();
                   1546: 
                   1547:                if (!n) {
                   1548: 
1.1.1.2   root     1549:                        DEBUG(("shrink_region: new_region failed"));
1.1       root     1550: 
                   1551:                        return EINTRN;
                   1552: 
                   1553:                }
                   1554: 
                   1555:                reg->len = newsize;
                   1556: 
                   1557:                n->loc = reg->loc + newsize;
                   1558: 
                   1559:                n->len = diff;
                   1560: 
                   1561:                n->mflags = reg->mflags & M_MAP;
                   1562: 
                   1563:                n->next = reg->next;
                   1564: 
                   1565:                reg->next = n;
                   1566: 
1.1.1.3 ! root     1567:                /* MEMPROT: invalidate the new, free region */
        !          1568: 
        !          1569:                mark_region(n,PROT_I);
        !          1570: 
1.1       root     1571:        }
                   1572: 
                   1573:        return 0;
                   1574: 
                   1575: }
                   1576: 
                   1577: 
                   1578: 
                   1579: /*
                   1580: 
                   1581:  * max_rsize(map): return the length of the biggest free region
                   1582: 
                   1583:  * in the given memory map, or 0 if no regions remain.
                   1584: 
                   1585:  */
                   1586: 
                   1587: 
                   1588: 
                   1589: long
                   1590: 
                   1591: max_rsize(map)
                   1592: 
                   1593:        MMAP map;
                   1594: 
                   1595: {
                   1596: 
1.1.1.2   root     1597:        MEMREGION *m;
1.1       root     1598: 
                   1599:        long size = 0;
                   1600: 
                   1601: 
                   1602: 
                   1603:        for (m = *map; m; m = m->next) {
                   1604: 
                   1605:                if (ISFREE(m)) {
                   1606: 
                   1607:                        if (m->len > size) {
                   1608: 
                   1609:                                size = m->len;
                   1610: 
                   1611:                        }
                   1612: 
                   1613:                }
                   1614: 
                   1615:        }
                   1616: 
                   1617:        return size;
                   1618: 
                   1619: }
                   1620: 
                   1621: 
                   1622: 
                   1623: /*
                   1624: 
                   1625:  * tot_rsize(map, flag): if flag == 1, return the total number of bytes in
                   1626: 
                   1627:  * the given memory map; if flag == 0, return only the number of free
                   1628: 
                   1629:  * bytes
                   1630: 
                   1631:  */
                   1632: 
                   1633: 
                   1634: 
                   1635: long
                   1636: 
                   1637: tot_rsize(map, flag)
                   1638: 
                   1639:        MMAP map;
                   1640: 
                   1641:        int flag;
                   1642: 
                   1643: {
                   1644: 
                   1645:        MEMREGION *m;
                   1646: 
                   1647:        long size = 0;
                   1648: 
                   1649: 
                   1650: 
                   1651:        for (m = *map; m; m = m->next) {
                   1652: 
                   1653:                if (flag || ISFREE(m)) {
                   1654: 
                   1655:                        size += m->len;
                   1656: 
                   1657:                }
                   1658: 
                   1659:        }
                   1660: 
                   1661:        return size;
                   1662: 
                   1663: }
                   1664: 
                   1665: 
                   1666: 
                   1667: /*
                   1668: 
1.1.1.3 ! root     1669:  * alloc_region(MMAP map, ulong size, int mode): allocate a new region and
1.1       root     1670: 
1.1.1.3 ! root     1671:  * attach it to the current process; returns the address at which the region
1.1       root     1672: 
1.1.1.3 ! root     1673:  * was attached, or NULL. The mode argument is the memory protection mode to
1.1       root     1674: 
1.1.1.3 ! root     1675:  * give to get_region, and in turn to mark_region.
1.1       root     1676: 
                   1677:  */
                   1678: 
                   1679: 
                   1680: 
                   1681: virtaddr
                   1682: 
1.1.1.3 ! root     1683: alloc_region(map, size, mode)
1.1       root     1684: 
                   1685:        MMAP map;
                   1686: 
                   1687:        ulong size;
                   1688: 
1.1.1.3 ! root     1689:        int mode;
        !          1690: 
1.1       root     1691: {
                   1692: 
                   1693:        MEMREGION *m;
                   1694: 
                   1695:        PROC *proc = curproc;
                   1696: 
                   1697:        virtaddr v;
                   1698: 
                   1699: 
                   1700: 
1.1.1.3 ! root     1701:        TRACELOW(("alloc_region(map,size: %lx,mode: %x)",size,mode));
        !          1702: 
        !          1703:        if (!size) {
        !          1704: 
        !          1705:            DEBUG(("alloc_region of zero bytes?!"));
        !          1706: 
        !          1707:            return 0;
        !          1708: 
        !          1709:        }
        !          1710: 
        !          1711: 
        !          1712: 
        !          1713:        m = get_region(map, size, mode);
1.1       root     1714: 
                   1715:        if (!m) {
                   1716: 
1.1.1.3 ! root     1717:                TRACELOW(("alloc_region: get_region failed"));
        !          1718: 
1.1       root     1719:                return 0;
                   1720: 
                   1721:        }
                   1722: 
                   1723: 
                   1724: 
1.1.1.2   root     1725: /* sanity check: even addresses only, please */
                   1726: 
                   1727:        assert((m->loc & MASKBITS) == 0);
                   1728: 
                   1729: 
                   1730: 
1.1       root     1731:        v = attach_region(proc, m);
                   1732: 
                   1733: /* NOTE: get_region returns a region with link count 1; since attach_region
                   1734: 
                   1735:  * increments the link count, we restore it after calling attach_region
                   1736: 
                   1737:  */
                   1738: 
                   1739:        m->links = 1;
                   1740: 
                   1741:        if (!v) {
                   1742: 
                   1743:                m->links = 0;
                   1744: 
                   1745:                free_region(m);
                   1746: 
1.1.1.3 ! root     1747:                TRACE(("alloc_region: attach_region failed"));
        !          1748: 
1.1       root     1749:                return 0;
                   1750: 
                   1751:        }
                   1752: 
                   1753:        return v;
                   1754: 
                   1755: }
                   1756: 
                   1757: 
                   1758: 
                   1759: /*
                   1760: 
                   1761:  * routines for creating a copy of an environment, and a new basepage.
                   1762: 
                   1763:  * note that the memory regions created should immediately be attached to
                   1764: 
                   1765:  * a process! Also note that create_env always operates in ST RAM, but
                   1766: 
                   1767:  * create_base might not.
                   1768: 
                   1769:  */
                   1770: 
                   1771: 
                   1772: 
                   1773: MEMREGION *
                   1774: 
                   1775: create_env(env)
                   1776: 
                   1777:        const char *env;
                   1778: 
                   1779: {
                   1780: 
                   1781:        long size;
                   1782: 
                   1783:        MEMREGION *m;
                   1784: 
                   1785:        virtaddr v;
                   1786: 
                   1787:        const char *old;
                   1788: 
                   1789:        char *new;
                   1790: 
1.1.1.3 ! root     1791: 
1.1       root     1792: 
                   1793:        if (!env) {
                   1794: 
                   1795:                env = ((BASEPAGE *)curproc->base)->p_env;
                   1796: 
                   1797:                        /* duplicate parent's environment */
                   1798: 
                   1799:        }
                   1800: 
                   1801:        size = 2;
                   1802: 
                   1803:        old = env;
                   1804: 
                   1805:        while (*env || *(env+1))
                   1806: 
                   1807:                env++,size++;
                   1808: 
1.1.1.3 ! root     1809: 
        !          1810: 
        !          1811:        v = alloc_region(core, size, PROT_P);
        !          1812: 
        !          1813:        /* if core fails, try alt */
        !          1814: 
        !          1815:        if (!v)
        !          1816: 
        !          1817:            v = alloc_region(alt, size, PROT_P);
        !          1818: 
        !          1819: 
1.1       root     1820: 
                   1821:        if (!v) {
                   1822: 
1.1.1.2   root     1823:                DEBUG(("create_env: alloc_region failed"));
1.1       root     1824: 
                   1825:                return (MEMREGION *)0;
                   1826: 
                   1827:        }
                   1828: 
                   1829:        m = addr2mem(v);
                   1830: 
                   1831: 
                   1832: 
                   1833: /* copy the old environment into the new */
                   1834: 
                   1835:        new = (char *) m->loc;
                   1836: 
1.1.1.3 ! root     1837:        TRACE(("copying environment: from %lx to %lx", old, new));
        !          1838: 
1.1       root     1839:        while (size > 0) {
                   1840: 
                   1841:                *new++ = *old++;
                   1842: 
                   1843:                --size;
                   1844: 
                   1845:        }
                   1846: 
1.1.1.3 ! root     1847:        TRACE(("finished copying environment"));
        !          1848: 
        !          1849: 
        !          1850: 
1.1       root     1851:        return m;
                   1852: 
                   1853: }
                   1854: 
                   1855: 
                   1856: 
                   1857: MEMREGION *
                   1858: 
                   1859: create_base(cmd, env, flags, prgsize)
                   1860: 
                   1861:        const char *cmd;
                   1862: 
                   1863:        MEMREGION *env;
                   1864: 
                   1865:        ulong flags, prgsize;
                   1866: 
                   1867: {
                   1868: 
                   1869:        long len, coresize, altsize;
                   1870: 
                   1871:        MMAP map;
                   1872: 
                   1873:        MEMREGION *m;
                   1874: 
                   1875:        BASEPAGE *b;
                   1876: 
1.1.1.3 ! root     1877:        short protmode;
        !          1878: 
1.1       root     1879: 
                   1880: 
                   1881: /* if flags & F_ALTLOAD == 1, then we might decide to load in alternate
                   1882: 
                   1883:    RAM if enough is available. "enough" is: if more alt ram than ST ram,
                   1884: 
                   1885:    load there; otherwise, if more than (minalt+1)*128K alt ram available
                   1886: 
                   1887:    for heap space, load in alt ram ("minalt" is the high byte of flags)
                   1888: 
                   1889:  */
                   1890: 
                   1891:        if (flags & F_ALTLOAD) {
                   1892: 
                   1893:                coresize = max_rsize(core);
                   1894: 
                   1895:                altsize = max_rsize(alt);
                   1896: 
                   1897:                if (altsize >= coresize)
                   1898: 
                   1899:                        map = alt;
                   1900: 
                   1901:                else {
                   1902: 
1.1.1.2   root     1903:                        len = (flags & F_MINALT) >> 28L;
1.1       root     1904: 
                   1905:                        len = (len+1)*128*1024L + prgsize + 256;
                   1906: 
                   1907:                        if (altsize >= len)
                   1908: 
                   1909:                                map = alt;
                   1910: 
                   1911:                        else
                   1912: 
                   1913:                                map = core;
                   1914: 
                   1915:                }
                   1916: 
                   1917:        }
                   1918: 
                   1919:        else
                   1920: 
                   1921:                map = core;
                   1922: 
                   1923: 
                   1924: 
                   1925:        len = max_rsize(map);
                   1926: 
1.1.1.3 ! root     1927:        if (len < prgsize) {
        !          1928: 
        !          1929:            /* can't possibly load this file in its eligible regions */
        !          1930: 
        !          1931:            DEBUG(("create_base: max_rsize smaller than prgsize"));
        !          1932: 
        !          1933:            return 0;
        !          1934: 
        !          1935:        }
        !          1936: 
1.1       root     1937: 
                   1938: 
                   1939: /* make sure that a little bit of memory is left over */
                   1940: 
                   1941:        if (len > 2*KEEP_MEM) {
                   1942: 
                   1943:                len -= KEEP_MEM;
                   1944: 
                   1945:        }
                   1946: 
1.1.1.3 ! root     1947: 
        !          1948: 
        !          1949:        protmode = (flags & F_PROTMODE) >> F_PROTSHIFT;
        !          1950: 
        !          1951: 
        !          1952: 
        !          1953:        m = addr2mem(alloc_region(map, len, protmode));
1.1       root     1954: 
                   1955:        if (!m) {
                   1956: 
1.1.1.2   root     1957:                DEBUG(("create_base: alloc_region failed"));
1.1       root     1958: 
                   1959:                return 0;
                   1960: 
                   1961:        }
                   1962: 
                   1963:        b = (BASEPAGE *)(m->loc);
                   1964: 
                   1965: 
                   1966: 
                   1967:        zero((char *)b, (long)sizeof(BASEPAGE));
                   1968: 
                   1969:        b->p_lowtpa = (long)b;
                   1970: 
                   1971:        b->p_hitpa = m->loc + m->len;
                   1972: 
                   1973:        b->p_env = (char *)env->loc;
                   1974: 
                   1975:        b->p_flags = flags;
                   1976: 
                   1977: 
                   1978: 
                   1979:        if (cmd)
                   1980: 
                   1981:                strncpy(b->p_cmdlin, cmd, 126);
                   1982: 
                   1983:        return m;
                   1984: 
                   1985: }
                   1986: 
                   1987: 
                   1988: 
                   1989: /*
                   1990: 
1.1.1.2   root     1991:  * load_region(): loads the program with the given file name
1.1       root     1992: 
1.1.1.2   root     1993:  * into a new region, and returns a pointer to that region. On
1.1       root     1994: 
1.1.1.2   root     1995:  * an error, returns 0 and leaves the error number in mint_errno.
1.1       root     1996: 
1.1.1.2   root     1997:  * "env" points to an already set up environment region, as returned
1.1       root     1998: 
1.1.1.3 ! root     1999:  * by create_env. On success, "xp" points to the file attributes, which
        !          2000: 
        !          2001:  * Pexec has already determined, and "fp" points to the programs
1.1       root     2002: 
1.1.1.3 ! root     2003:  * prgflags. "text" is a pointer to a MEMREGION
1.1       root     2004: 
1.1.1.2   root     2005:  * pointer, which will be set to the region occupied by the shared
1.1       root     2006: 
1.1.1.2   root     2007:  * text segment of this program (if applicable).
1.1       root     2008: 
1.1.1.2   root     2009:  */
1.1       root     2010: 
                   2011: 
                   2012: 
                   2013: MEMREGION *
                   2014: 
1.1.1.3 ! root     2015: load_region(filename, env, cmdlin, xp, text, fp)
1.1       root     2016: 
                   2017:        const char *filename;
                   2018: 
                   2019:        MEMREGION *env;
                   2020: 
                   2021:        const char *cmdlin;
                   2022: 
                   2023:        XATTR *xp;              /* attributes for the file just loaded */
                   2024: 
1.1.1.2   root     2025:        MEMREGION **text;       /* set to point to shared text region,
                   2026: 
                   2027:                                   if any */
                   2028: 
1.1.1.3 ! root     2029:        long *fp;               /* prgflags for this file */
        !          2030: 
1.1       root     2031: {
                   2032: 
                   2033:        FILEPTR *f;
                   2034: 
                   2035:        DEVDRV *dev;
                   2036: 
1.1.1.2   root     2037:        MEMREGION *reg, *shtext;
1.1       root     2038: 
                   2039:        BASEPAGE *b;
                   2040: 
1.1.1.2   root     2041:        long size, start;
1.1       root     2042: 
1.1.1.2   root     2043:        FILEHEAD fh;
1.1       root     2044: 
                   2045: 
                   2046: 
1.1.1.2   root     2047: /* bug: this should be O_DENYW mode, not O_DENYNONE */
1.1       root     2048: 
1.1.1.2   root     2049: /* we must use O_DENYNONE because of the desktop and because of the
1.1       root     2050: 
1.1.1.2   root     2051:  * TOS file system brain-damage
1.1       root     2052: 
1.1.1.2   root     2053:  */
1.1       root     2054: 
                   2055:        f = do_open(filename, O_DENYNONE | O_EXEC, 0, xp);
                   2056: 
                   2057:        if (!f) {
                   2058: 
                   2059:                return 0;               /* mint_errno set by do_open */
                   2060: 
                   2061:        }
                   2062: 
                   2063: 
                   2064: 
1.1.1.2   root     2065:        dev = f->dev;
1.1       root     2066: 
1.1.1.2   root     2067:        size = (*dev->read)(f, (void *)&fh, (long)sizeof(fh));
1.1       root     2068: 
1.1.1.2   root     2069:        if (fh.fmagic != GEMDOS_MAGIC || size != (long)sizeof(fh)) {
1.1       root     2070: 
1.1.1.2   root     2071:                DEBUG(("load_region: file not executable"));
1.1       root     2072: 
                   2073:                mint_errno = ENOEXEC;
                   2074: 
                   2075: failed:
                   2076: 
                   2077:                do_close(f);
                   2078: 
                   2079:                return 0;
                   2080: 
                   2081:        }
                   2082: 
                   2083: 
                   2084: 
1.1.1.3 ! root     2085:        *fp = fh.flag;
        !          2086: 
        !          2087: 
        !          2088: 
1.1.1.2   root     2089:        if (fh.flag & F_SHTEXT) {
                   2090: 
                   2091:                TRACE(("loading shared text segment"));
                   2092: 
                   2093:                shtext = get_text_seg(f, &fh, xp);
                   2094: 
                   2095:                if (!shtext) {
                   2096: 
                   2097:                        DEBUG(("load_region: unable to get shared text segment"));
                   2098: 
                   2099: /* mint_errno set in get_text_seg */
                   2100: 
                   2101:                        goto failed;
                   2102: 
                   2103:                }
                   2104: 
                   2105:                size = fh.fdata + fh.fbss;
                   2106: 
                   2107:        } else {
                   2108: 
                   2109:                size = fh.ftext + fh.fdata + fh.fbss;
                   2110: 
                   2111:                shtext = 0;
                   2112: 
                   2113:        }
                   2114: 
                   2115: 
1.1       root     2116: 
1.1.1.3 ! root     2117:        reg = create_base(cmdlin, env, fh.flag, size);
1.1       root     2118: 
1.1.1.3 ! root     2119:        if (reg && size+1024L > reg->len) {
1.1       root     2120: 
1.1.1.3 ! root     2121:                DEBUG(("load_region: insufficient memory to load"));
1.1       root     2122: 
1.1.1.3 ! root     2123:                detach_region(curproc, reg);
1.1       root     2124: 
1.1.1.3 ! root     2125:                reg = 0;
1.1       root     2126: 
                   2127:        }
                   2128: 
                   2129: 
                   2130: 
                   2131:        if (reg == 0) {
                   2132: 
1.1.1.2   root     2133:                if (shtext) {
                   2134: 
                   2135:                        detach_region(curproc, shtext);
                   2136: 
                   2137:                }
                   2138: 
1.1       root     2139:                mint_errno = ENSMEM;
                   2140: 
                   2141:                goto failed;
                   2142: 
                   2143:        }
                   2144: 
                   2145: 
                   2146: 
                   2147:        b = (BASEPAGE *)reg->loc;
                   2148: 
                   2149:        b->p_flags = fh.flag;
                   2150: 
1.1.1.2   root     2151:        if (shtext) {
                   2152: 
                   2153:                b->p_tbase = shtext->loc;
1.1       root     2154: 
1.1.1.2   root     2155:                b->p_tlen = 0;
1.1       root     2156: 
1.1.1.2   root     2157:                b->p_dbase = b->p_lowtpa + 256;
                   2158: 
                   2159:        } else {
                   2160: 
                   2161:                b->p_tbase = b->p_lowtpa + 256;
                   2162: 
                   2163:                b->p_tlen = fh.ftext;
                   2164: 
                   2165:                b->p_dbase = b->p_tbase + b->p_tlen;
                   2166: 
                   2167:        }
1.1       root     2168: 
                   2169:        b->p_dlen = fh.fdata;
                   2170: 
                   2171:        b->p_bbase = b->p_dbase + b->p_dlen;
                   2172: 
1.1.1.2   root     2173:        b->p_blen = fh.fbss;
1.1       root     2174: 
                   2175: 
                   2176: 
1.1.1.2   root     2177: /* if shared text, then we start loading at the end of the
1.1       root     2178: 
1.1.1.2   root     2179:  * text region, since that is already set up
                   2180: 
                   2181:  */
                   2182: 
                   2183:        if (shtext) {
                   2184: 
                   2185:        /* skip over text info */
                   2186: 
                   2187:                size = fh.fdata;
                   2188: 
                   2189:                start = fh.ftext;
                   2190: 
                   2191:        } else {
                   2192: 
                   2193:                size = fh.ftext + fh.fdata;
                   2194: 
                   2195:                start = 0;
                   2196: 
                   2197:        }
                   2198: 
                   2199: 
                   2200: 
                   2201:        mint_errno = (int)load_and_reloc(f, &fh, (char *)b+256, start,
                   2202: 
                   2203:                        size, b);
                   2204: 
                   2205: 
                   2206: 
                   2207:        if (mint_errno) {
1.1       root     2208: 
                   2209:                detach_region(curproc, reg);
                   2210: 
1.1.1.2   root     2211:                if (shtext) detach_region(curproc, shtext);
                   2212: 
1.1       root     2213:                goto failed;
                   2214: 
                   2215:        }
                   2216: 
                   2217: 
                   2218: 
                   2219:        if (fh.flag & F_FASTLOAD)                       /* fastload bit */
                   2220: 
                   2221:                size = b->p_blen;
                   2222: 
                   2223:        else
                   2224: 
                   2225:                size = b->p_hitpa - b->p_bbase;
                   2226: 
                   2227:        zero((char *)b->p_bbase, size);
                   2228: 
                   2229: 
                   2230: 
1.1.1.2   root     2231:        do_close(f);
1.1       root     2232: 
1.1.1.2   root     2233:        *text = shtext;
1.1       root     2234: 
1.1.1.2   root     2235:        return reg;
1.1       root     2236: 
1.1.1.2   root     2237: }
                   2238: 
                   2239: 
                   2240: 
                   2241: /*
                   2242: 
                   2243:  * load_and_reloc(f, fh, where, start, nbytes): load and relocate from
                   2244: 
                   2245:  * the open GEMDOS executable file f "nbytes" bytes starting at offset
                   2246: 
                   2247:  * "start" (relative to the end of the file header, i.e. from the first
                   2248: 
                   2249:  * byte of the actual program image in the file). "where" is the address
                   2250: 
                   2251:  * in (physical) memory into which the loaded image must be placed; it is
                   2252: 
                   2253:  * assumed that "where" is big enough to hold "nbytes" bytes!
1.1       root     2254: 
                   2255:  */
                   2256: 
                   2257: 
                   2258: 
1.1.1.2   root     2259: long
                   2260: 
                   2261: load_and_reloc(f, fh, where, start, nbytes, base)
                   2262: 
                   2263:        FILEPTR *f;
                   2264: 
                   2265:        FILEHEAD *fh;
                   2266: 
                   2267:        char *where;
                   2268: 
                   2269:        long start;
                   2270: 
                   2271:        long nbytes;
                   2272: 
                   2273:        BASEPAGE *base;
                   2274: 
                   2275: {
                   2276: 
                   2277:        unsigned char c, *next;
                   2278: 
                   2279:        long r;
                   2280: 
                   2281:        DEVDRV *dev;
                   2282: 
                   2283: #define LRBUFSIZ 8196
                   2284: 
                   2285:        static unsigned char buffer[LRBUFSIZ];
                   2286: 
                   2287:        long fixup, size, bytes_read;
                   2288: 
                   2289:        long reloc;
                   2290: 
                   2291: 
                   2292: 
                   2293: 
                   2294: 
                   2295: TRACE(("load_and_reloc: %ld to %ld at %lx", start, nbytes+start, where));
                   2296: 
                   2297:        dev = f->dev;
                   2298: 
                   2299: 
                   2300: 
                   2301:        r = (*dev->lseek)(f, start+sizeof(FILEHEAD), SEEK_SET);
                   2302: 
                   2303:        if (r < 0) return r;
                   2304: 
                   2305:        r = (*dev->read)(f, where, nbytes);
                   2306: 
                   2307:        if (r != nbytes) {
                   2308: 
                   2309:                DEBUG(("load_region: unexpected EOF"));
                   2310: 
                   2311:                return ENOEXEC;
                   2312: 
                   2313:        }
                   2314: 
                   2315: 
                   2316: 
                   2317: /* now do the relocation */
                   2318: 
                   2319: /* skip over symbol table, etc. */
                   2320: 
                   2321:        r = (*dev->lseek)(f, sizeof(FILEHEAD) + fh->ftext + fh->fdata +
                   2322: 
                   2323:                        fh->fsym, SEEK_SET);
                   2324: 
                   2325:        if (r < 0) return ENOEXEC;
                   2326: 
                   2327: 
                   2328: 
                   2329:        if (fh->reloc != 0 || (*dev->read)(f, (char *)&fixup, 4L) != 4L
                   2330: 
                   2331:            || fixup == 0) {
                   2332: 
                   2333:                return 0;       /* no relocation to be performed */
                   2334: 
                   2335:        }
                   2336: 
                   2337: 
                   2338: 
                   2339:        size = LRBUFSIZ;
                   2340: 
                   2341:        bytes_read = 0;
                   2342: 
                   2343:        next = buffer;
                   2344: 
1.1       root     2345: 
                   2346: 
1.1.1.2   root     2347:        do {
                   2348: 
                   2349:                if (fixup >= nbytes + start) {
                   2350: 
                   2351:                        TRACE(("load_region: end of relocation at %ld", fixup));
                   2352: 
                   2353:                        break;
                   2354: 
                   2355:                }
                   2356: 
                   2357:                else if (fixup >= start) {
1.1       root     2358: 
1.1.1.2   root     2359:                        reloc = *((long *)(where + fixup - start));
                   2360: 
                   2361:                        if (reloc < fh->ftext) {
                   2362: 
                   2363:                                reloc += base->p_tbase;
                   2364: 
                   2365:                        } else if (reloc < fh->ftext + fh->fdata && base->p_dbase) {
                   2366: 
                   2367:                                reloc += base->p_dbase - fh->ftext;
                   2368: 
                   2369:                        } else if (reloc < fh->ftext + fh->fdata + fh->fbss && base->p_bbase) {
                   2370: 
                   2371:                                reloc += base->p_bbase - (fh->ftext + fh->fdata);
                   2372: 
                   2373:                        } else {
                   2374: 
                   2375:                                DEBUG(("load_region: bad relocation: %ld", reloc));
                   2376: 
                   2377:                                if (base->p_dbase)
                   2378: 
                   2379:                                    reloc += base->p_dbase - fh->ftext; /* assume data reloc */
                   2380: 
                   2381:                                else if (base->p_bbase)
                   2382: 
                   2383:                                    reloc += base->p_bbase - (fh->ftext + fh->fdata);
                   2384: 
                   2385:                                else
                   2386: 
                   2387:                                    return ENOEXEC;
                   2388: 
                   2389:                        }
                   2390: 
                   2391:                        *((long *)(where + fixup - start)) = reloc;
                   2392: 
                   2393:                }
1.1       root     2394: 
                   2395:                do {
                   2396: 
1.1.1.2   root     2397:                        if (!bytes_read) {
1.1       root     2398: 
1.1.1.2   root     2399:                                bytes_read =
1.1       root     2400: 
1.1.1.2   root     2401:                                    (*dev->read)(f,(char *)buffer,size);
                   2402: 
                   2403:                                next = buffer;
1.1       root     2404: 
                   2405:                        }
                   2406: 
1.1.1.2   root     2407:                        if (bytes_read < 0) {
1.1       root     2408: 
1.1.1.2   root     2409:                                DEBUG(("load_region: EOF in relocation"));
1.1       root     2410: 
1.1.1.2   root     2411:                                return ENOEXEC;
1.1       root     2412: 
1.1.1.2   root     2413:                        }
1.1       root     2414: 
1.1.1.2   root     2415:                        else if (bytes_read == 0)
1.1       root     2416: 
1.1.1.2   root     2417:                                c = 0;
1.1       root     2418: 
1.1.1.2   root     2419:                        else {
1.1       root     2420: 
1.1.1.2   root     2421:                                c = *next++; bytes_read--;
1.1       root     2422: 
1.1.1.2   root     2423:                        }
1.1       root     2424: 
1.1.1.2   root     2425:                        if (c == 1) fixup += 254;
1.1       root     2426: 
1.1.1.2   root     2427:                } while (c == 1);
1.1       root     2428: 
1.1.1.2   root     2429:                fixup += ( (unsigned) c) & 0xff;
1.1       root     2430: 
1.1.1.2   root     2431:        } while (c);
1.1       root     2432: 
                   2433: 
                   2434: 
1.1.1.2   root     2435:        return 0;
1.1       root     2436: 
1.1.1.2   root     2437: }
                   2438: 
                   2439: 
                   2440: 
                   2441: /*
                   2442: 
                   2443:  * function to check for existence of a shared text region
                   2444: 
                   2445:  * corresponding to file "f", and if none is found, to create one
                   2446: 
                   2447:  * the memory region being returned is attached to the current
                   2448: 
                   2449:  * process
                   2450: 
                   2451:  */
                   2452: 
                   2453: 
                   2454: 
                   2455: MEMREGION *
                   2456: 
                   2457: get_text_seg(f, fh, xp)
                   2458: 
                   2459:        FILEPTR *f;
                   2460: 
                   2461:        FILEHEAD *fh;
                   2462: 
                   2463:        XATTR *xp;
                   2464: 
                   2465: {
                   2466: 
                   2467:        SHTEXT *s;
1.1       root     2468: 
1.1.1.2   root     2469:        MEMREGION *m;
                   2470: 
                   2471:        long r;
                   2472: 
                   2473:        BASEPAGE b;
                   2474: 
                   2475: 
                   2476: 
                   2477:        s = text_reg;
                   2478: 
                   2479: 
                   2480: 
                   2481:        while(s) {
                   2482: 
1.1.1.3 ! root     2483:                if (s->f && samefile(&f->fc, &s->f->fc) &&
1.1.1.2   root     2484: 
                   2485:                    xp->mtime == s->mtime &&
                   2486: 
                   2487:                    xp->mdate == s->mdate)
                   2488: 
                   2489:                {
1.1       root     2490: 
1.1.1.2   root     2491:                        m = s->text;
1.1       root     2492: 
1.1.1.2   root     2493:                        if (attach_region(curproc, m)) {
1.1       root     2494: 
1.1.1.2   root     2495: TRACE(("re-using shared text region %lx", m));
                   2496: 
                   2497:                                return m;
                   2498: 
                   2499:                        }
                   2500: 
                   2501:                        else {
                   2502: 
                   2503:                                mint_errno = ENSMEM;
                   2504: 
                   2505:                                return 0;
                   2506: 
                   2507:                        }
                   2508: 
                   2509:                }
                   2510: 
                   2511:                s = s->next;
1.1       root     2512: 
                   2513:        }
                   2514: 
                   2515: 
                   2516: 
1.1.1.2   root     2517: /* hmmm, not found; OK, we'll have to create a new text region */
                   2518: 
                   2519: 
                   2520: 
                   2521:        s = kmalloc(SIZEOF(SHTEXT));
                   2522: 
                   2523:        if (!s) {
                   2524: 
                   2525:                mint_errno = ENSMEM;
                   2526: 
                   2527:                return 0;
                   2528: 
                   2529:        }
                   2530: 
                   2531:        m = 0;
                   2532: 
                   2533: /* actually, I can't see why loading in TT RAM is ever undesireable,
                   2534: 
                   2535:  * since shared text programs should be very clean (and since only
                   2536: 
                   2537:  * the text segment is going in there). But better safe than sorry.
                   2538: 
                   2539:  */
                   2540: 
                   2541:        if (fh->flag & F_ALTLOAD) {
                   2542: 
1.1.1.3 ! root     2543:                m = addr2mem(alloc_region(alt, fh->ftext, PROT_P));
1.1.1.2   root     2544: 
                   2545:        }
                   2546: 
                   2547:        if (!m)
                   2548: 
1.1.1.3 ! root     2549:                m = addr2mem(alloc_region(core, fh->ftext, PROT_P));
1.1.1.2   root     2550: 
                   2551: 
                   2552: 
                   2553:        if (!m) {
                   2554: 
                   2555:                kfree(s);
                   2556: 
                   2557:                mint_errno = ENSMEM;
                   2558: 
                   2559:                return 0;
                   2560: 
                   2561:        }
                   2562: 
                   2563: 
                   2564: 
                   2565: /* set up a fake "basepage" for load_and_reloc
                   2566: 
                   2567:  * note: the 0 values should make load_and_reloc
                   2568: 
                   2569:  * barf on any attempts at data relocation, since we have
                   2570: 
                   2571:  * no data segment
                   2572: 
                   2573:  */
                   2574: 
                   2575: TRACE(("attempting to create shared text region"));
                   2576: 
                   2577: 
                   2578: 
                   2579:        b.p_tbase = m->loc;
                   2580: 
                   2581:        b.p_tlen = fh->ftext;
                   2582: 
                   2583:        b.p_dbase = 0;
                   2584: 
                   2585:        b.p_dlen = 0;
                   2586: 
                   2587:        b.p_bbase = b.p_blen = 0;
                   2588: 
1.1       root     2589: 
1.1.1.2   root     2590: 
                   2591:        r = load_and_reloc(f, fh, (char *)m->loc, 0, fh->ftext, &b);
                   2592: 
                   2593:        if (r) {
                   2594: 
                   2595:                m->links = 0;
                   2596: 
                   2597:                detach_region(curproc, m);
                   2598: 
                   2599:                kfree(s);
                   2600: 
                   2601:                return 0;
                   2602: 
                   2603:        }
                   2604: 
                   2605: 
                   2606: 
                   2607: /* region has valid shared text data */
                   2608: 
                   2609:        m->mflags |= M_SHTEXT;
                   2610: 
                   2611: 
                   2612: 
                   2613: /*
                   2614: 
                   2615:  * KLUDGE: to make sure we always have up to date shared text
                   2616: 
                   2617:  * info, even across a network, we leave the file passed
                   2618: 
                   2619:  * to us open with DENYWRITE mode, so that nobody will
                   2620: 
                   2621:  * modify it.
                   2622: 
                   2623:  */
                   2624: 
                   2625:        f->links++;     /* keep the file open longer */
                   2626: 
                   2627: 
                   2628: 
                   2629: /* BUG: what if someone already has the file open for
                   2630: 
                   2631:  * writing? Then we could get screwed...
                   2632: 
                   2633:  */
                   2634: 
                   2635:        f->flags = (f->flags & ~O_SHMODE) | O_DENYW;
                   2636: 
                   2637:        s->f = f;
                   2638: 
                   2639:        s->text = m;
                   2640: 
                   2641:        s->next = text_reg;
                   2642: 
                   2643:        s->mtime = xp->mtime;
                   2644: 
                   2645:        s->mdate = xp->mdate;
                   2646: 
                   2647:        text_reg = s;
                   2648: 
                   2649: TRACE(("shared text region %lx created", m));
                   2650: 
                   2651:        return m;
1.1       root     2652: 
                   2653: }
                   2654: 
                   2655: 
                   2656: 
                   2657: /*
                   2658: 
                   2659:  * exec_region(p, mem, thread): create a child process out of a mem region
                   2660: 
                   2661:  * "p" is the process structure set up by the parent; it may be "curproc",
                   2662: 
                   2663:  * if we're overlaying. "mem" is the loaded memory region returned by
                   2664: 
                   2665:  * "load region". Any open files (other than the standard handles) owned
                   2666: 
                   2667:  * by "p" are closed, and if thread !=0 all memory is released; the caller
                   2668: 
                   2669:  * must explicitly attach the environment and base region. The caller must
                   2670: 
                   2671:  * also put "p" on the appropriate queue (most likely READY_Q).
                   2672: 
                   2673:  */
                   2674: 
                   2675: 
                   2676: 
                   2677: extern long mint_dos(), mint_bios();
                   2678: 
                   2679: 
                   2680: 
                   2681: void rts() {}          /* dummy termination routine */
                   2682: 
                   2683: 
                   2684: 
                   2685: PROC *
                   2686: 
                   2687: exec_region(p, mem, thread)
                   2688: 
                   2689:        PROC      *p;
                   2690: 
                   2691:        MEMREGION *mem;
                   2692: 
                   2693:        int thread;
                   2694: 
                   2695: {
                   2696: 
                   2697:        BASEPAGE *b;
                   2698: 
                   2699:        FILEPTR *f;
                   2700: 
                   2701:        int i;
                   2702: 
                   2703:        MEMREGION *m;
                   2704: 
                   2705: 
                   2706: 
1.1.1.2   root     2707:        TRACE(("exec_region"));
1.1       root     2708: 
                   2709: 
                   2710: 
                   2711:        b = (BASEPAGE *) mem->loc;
                   2712: 
                   2713: 
                   2714: 
1.1.1.2   root     2715:        cpush((void *)b->p_tbase, b->p_tlen);   /* flush cached versions of the text */
                   2716: 
                   2717:        
                   2718: 
1.1       root     2719: /* set some (undocumented) variables in the basepage */
                   2720: 
                   2721:        b->p_defdrv = p->curdrv;
                   2722: 
                   2723:        for (i = 0; i < 6; i++)
                   2724: 
                   2725:                b->p_devx[i] = i;
                   2726: 
                   2727: 
                   2728: 
                   2729:        p->dta = (DTABUF *)(b->p_dta = &b->p_cmdlin[0]);
                   2730: 
                   2731:        p->base = b;
                   2732: 
                   2733: 
                   2734: 
                   2735: /* close extra open files */
                   2736: 
                   2737:        for (i = MIN_OPEN; i < MAX_OPEN; i++) {
                   2738: 
1.1.1.2   root     2739:                if ( (f = p->handle[i]) != 0 && (p->fdflags[i] & FD_CLOEXEC) ) {
1.1       root     2740: 
                   2741:                        do_pclose(p, f);
                   2742: 
                   2743:                        p->handle[i] = 0;
                   2744: 
                   2745:                }
                   2746: 
                   2747:        }
                   2748: 
                   2749: 
                   2750: 
                   2751: /* initialize memory */
                   2752: 
                   2753:        recalc_maxmem(p);
                   2754: 
                   2755:        if (p->maxmem) {
                   2756: 
                   2757:                shrink_region(mem, p->maxmem);
                   2758: 
                   2759:                b->p_hitpa = b->p_lowtpa + mem->len;
                   2760: 
                   2761:        }
                   2762: 
                   2763: 
                   2764: 
                   2765:        p->memflags = b->p_flags;
                   2766: 
                   2767: 
                   2768: 
                   2769:        if (!thread) {
                   2770: 
                   2771:                for (i = 0; i < p->num_reg; i++) {
                   2772: 
                   2773:                        m = p->mem[i];
                   2774: 
                   2775:                        if (m) {
                   2776: 
                   2777:                                m->links--;
                   2778: 
                   2779:                                if (m->links <= 0)
                   2780: 
                   2781:                                        free_region(m);
                   2782: 
                   2783:                        }
                   2784: 
                   2785:                }
                   2786: 
                   2787:                if (p->num_reg > NUM_REGIONS) {
                   2788: 
1.1.1.3 ! root     2789:                        /*
        !          2790: 
        !          2791:                         * If the proc struct has a larger mem array than
        !          2792: 
        !          2793:                         * the default, then free it and allocate a
        !          2794: 
        !          2795:                         * default-sized one.
        !          2796: 
        !          2797:                         */
1.1       root     2798: 
                   2799: 
                   2800: 
1.1.1.3 ! root     2801:                        /*
1.1       root     2802: 
1.1.1.3 ! root     2803:                         * hoo ha! Memory protection problem here. Use
        !          2804: 
        !          2805:                         * temps and pre-clear p->mem so memprot doesn't try
        !          2806: 
        !          2807:                         * to walk these structures as we're freeing and
        !          2808: 
        !          2809:                         * reallocating them!  (Calling kmalloc can cause
        !          2810: 
        !          2811:                         * a table walk if the alloc results in calling
        !          2812: 
        !          2813:                         * get_region.)
        !          2814: 
        !          2815:                         */
        !          2816: 
        !          2817:                        void *pmem, *paddr;
        !          2818: 
        !          2819: 
        !          2820: 
        !          2821:                        pmem = p->mem;
        !          2822: 
        !          2823:                        paddr = p->addr;
        !          2824: 
        !          2825:                        p->mem = NULL; p->addr = NULL;
        !          2826: 
        !          2827:                        kfree(pmem); kfree(paddr);
        !          2828: 
        !          2829: 
        !          2830: 
        !          2831:                        pmem = kmalloc(NUM_REGIONS * SIZEOF(MEMREGION *));
        !          2832: 
        !          2833:                        paddr = kmalloc(NUM_REGIONS * SIZEOF(virtaddr));
        !          2834: 
        !          2835:                        assert(pmem && paddr);
        !          2836: 
        !          2837:                        p->mem = pmem;
        !          2838: 
        !          2839:                        p->addr = paddr;
1.1       root     2840: 
                   2841:                        p->num_reg = NUM_REGIONS;
                   2842: 
                   2843:                }
                   2844: 
                   2845:                zero((char *)p->mem, (p->num_reg)*SIZEOF(MEMREGION *));
                   2846: 
                   2847:                zero((char *)p->addr, (p->num_reg)*SIZEOF(virtaddr));
                   2848: 
                   2849:        }
                   2850: 
                   2851: 
                   2852: 
                   2853: /* initialize signals */
                   2854: 
                   2855:        p->sigmask = 0;
                   2856: 
                   2857:        for (i = 0; i < NSIG; i++) {
                   2858: 
                   2859:                if (p->sighandle[i] != SIG_IGN) {
                   2860: 
                   2861:                        p->sighandle[i] = SIG_DFL;
                   2862: 
                   2863:                        p->sigflags[i] = 0;
                   2864: 
                   2865:                        p->sigextra[i] = 0;
                   2866: 
                   2867:                }
                   2868: 
                   2869:        }
                   2870: 
                   2871: 
                   2872: 
                   2873: /* zero the user registers, and set the FPU in a "clear" state */
                   2874: 
                   2875:        for (i = 0; i < 15; i++)
                   2876: 
                   2877:                p->ctxt[CURRENT].regs[i] = 0;
                   2878: 
                   2879:        p->ctxt[CURRENT].sr = 0;
                   2880: 
                   2881:        p->ctxt[CURRENT].fstate[0] = 0;
                   2882: 
                   2883: 
                   2884: 
                   2885: /* set PC, stack registers, etc. appropriately */
                   2886: 
                   2887:        p->ctxt[CURRENT].pc = b->p_tbase;
                   2888: 
                   2889: 
                   2890: 
                   2891: /* The "-0x20" is to make sure that syscall.s won't run past the end of
                   2892: 
                   2893:  * memory when the user makes a system call and doesn't push very many
                   2894: 
                   2895:  * parameters -- syscall always tries to copy the maximum possible number
                   2896: 
                   2897:  * of parms.
                   2898: 
                   2899:  *
                   2900: 
                   2901:  * NOTE: there's a sanity check here in case programs Mshrink a basepage
                   2902: 
                   2903:  * without fixing the p_hitpa field in the basepage; this is to ensure
                   2904: 
                   2905:  * compatibility with older versions of MiNT, which ignore p_hitpa.
                   2906: 
                   2907:  */
                   2908: 
                   2909:        if (valid_address(b->p_hitpa - 0x20))
                   2910: 
                   2911:                p->ctxt[CURRENT].usp = b->p_hitpa - 0x20;
                   2912: 
                   2913:        else
                   2914: 
                   2915:                p->ctxt[CURRENT].usp = mem->loc + mem->len - 0x20;
                   2916: 
                   2917: 
                   2918: 
                   2919:        p->ctxt[CURRENT].ssp = (long)(p->stack + ISTKSIZE);
                   2920: 
                   2921:        p->ctxt[CURRENT].term_vec = (long)rts;
                   2922: 
                   2923: 
                   2924: 
                   2925: /* set up stack for process */
                   2926: 
                   2927:        *((long *)(p->ctxt[CURRENT].usp + 4)) = (long) b;
                   2928: 
                   2929: 
                   2930: 
                   2931: /* check for a valid text region. some compilers (e.g. Lattice 3) just throw
                   2932: 
                   2933:    everything into the text region, including data; fork() must be careful
                   2934: 
                   2935:    to save the whole region, then. We assume that if the compiler (or
                   2936: 
                   2937:    assembler, or whatever) goes to the trouble of making separate text, data,
                   2938: 
                   2939:    and bss regions, then the text region is code and isn't modified and
                   2940: 
                   2941:    fork doesn't have to save it.
                   2942: 
                   2943:  */
                   2944: 
1.1.1.2   root     2945:        if (b->p_blen != 0 || b->p_dlen != 0)
1.1       root     2946: 
                   2947:                p->txtsize = b->p_tlen;
                   2948: 
                   2949:        else
                   2950: 
                   2951:                p->txtsize = 0;
                   2952: 
                   2953: 
                   2954: 
                   2955: /*
                   2956: 
                   2957:  * An ugly hack: dLibs tries to poke around in the parent's address space
                   2958: 
                   2959:  * to find stuff. For now, we'll allow this by faking a pointer into
                   2960: 
                   2961:  * the parent's address space in the place in the basepage where dLibs is
                   2962: 
                   2963:  * expecting it. This ugly hack only works correctly if the Pexec'ing
                   2964: 
                   2965:  * program (i.e. curproc) is in user mode.
                   2966: 
                   2967:  */
                   2968: 
1.1.1.2   root     2969:        if (curproc != rootproc)
                   2970: 
                   2971:                curproc->base->p_usp = curproc->ctxt[SYSCALL].usp - 0x32;
1.1       root     2972: 
                   2973: 
                   2974: 
                   2975:        return p;
                   2976: 
                   2977: }
                   2978: 
                   2979: 
                   2980: 
                   2981: /*
                   2982: 
                   2983:  * misc. utility routines
                   2984: 
                   2985:  */
                   2986: 
                   2987: 
                   2988: 
                   2989: /*
                   2990: 
                   2991:  * long memused(p): return total memory allocated to process p
                   2992: 
                   2993:  */
                   2994: 
                   2995: 
                   2996: 
                   2997: long
                   2998: 
                   2999: memused(p)
                   3000: 
                   3001:        PROC *p;
                   3002: 
                   3003: {
                   3004: 
                   3005:        int i;
                   3006: 
                   3007:        long size;
                   3008: 
                   3009: 
                   3010: 
1.1.1.3 ! root     3011:        /* a ZOMBIE owns no memory and its mem array ptr is zero */
        !          3012: 
        !          3013:        if (p->mem == NULL) return 0;
        !          3014: 
        !          3015: 
        !          3016: 
1.1       root     3017:        size = 0;
                   3018: 
                   3019:        for (i = 0; i < p->num_reg; i++) {
                   3020: 
                   3021:                if (p->mem[i])
                   3022: 
                   3023:                        size += p->mem[i]->len;
                   3024: 
                   3025:        }
                   3026: 
                   3027:        return size;
                   3028: 
                   3029: }
                   3030: 
                   3031: 
                   3032: 
                   3033: /* 
                   3034: 
                   3035:  * recalculate the maximum memory limit on a process; this limit depends
                   3036: 
                   3037:  * on the max. allocated memory and max. total memory limits set by
                   3038: 
                   3039:  * p_setlimit (see dos.c), and (perhaps) on the size of the program
                   3040: 
                   3041:  * that the process is executing. whenever any of these things
                   3042: 
                   3043:  * change (through p_exec or p_setlimit) this routine must be called
                   3044: 
                   3045:  */
                   3046: 
                   3047: 
                   3048: 
                   3049: void
                   3050: 
                   3051: recalc_maxmem(p)
                   3052: 
                   3053:        PROC *p;
                   3054: 
                   3055: {
                   3056: 
                   3057:        BASEPAGE *b;
                   3058: 
                   3059:        long siz;
                   3060: 
                   3061: 
                   3062: 
                   3063:        b = (BASEPAGE *)p->base;
                   3064: 
                   3065:        if (b)
                   3066: 
                   3067:                siz = b->p_tlen + b->p_dlen + b->p_blen;
                   3068: 
                   3069:        else
                   3070: 
                   3071:                siz = 0;
                   3072: 
                   3073:        p->maxmem = 0;
                   3074: 
                   3075:        if (p->maxdata) {
                   3076: 
                   3077:                p->maxmem = p->maxdata + siz;
                   3078: 
                   3079:        }
                   3080: 
                   3081: 
                   3082: 
                   3083:        if (p->maxcore) {
                   3084: 
                   3085:                if (p->maxmem == 0 || p->maxmem > p->maxcore)
                   3086: 
                   3087:                        p->maxmem = p->maxcore;
                   3088: 
                   3089:        }
                   3090: 
                   3091:        if (p->maxmem && p->maxmem < siz)
                   3092: 
                   3093:                p->maxmem = siz;
                   3094: 
                   3095: }
                   3096: 
                   3097: 
                   3098: 
                   3099: /*
                   3100: 
                   3101:  * valid_address: checks to see if the indicated address falls within
                   3102: 
                   3103:  * memory attached to the current process
                   3104: 
                   3105:  */
                   3106: 
                   3107: 
                   3108: 
                   3109: int
                   3110: 
                   3111: valid_address(addr)
                   3112: 
                   3113:        long addr;
                   3114: 
                   3115: {
                   3116: 
                   3117:        int i;
                   3118: 
                   3119:        MEMREGION *m;
                   3120: 
                   3121: 
                   3122: 
                   3123:        for (i = 0; i < curproc->num_reg; i++) {
                   3124: 
                   3125:                if ((m = curproc->mem[i]) != 0) {
                   3126: 
                   3127:                        if (addr >= m->loc && addr <= m->loc + m->len)
                   3128: 
                   3129:                                return 1;
                   3130: 
                   3131:                }
                   3132: 
                   3133:        }
                   3134: 
                   3135:        return 0;
                   3136: 
                   3137: }
                   3138: 
                   3139: 
                   3140: 
                   3141: /*
                   3142: 
1.1.1.3 ! root     3143:  * convert an address to a memory region; this works only in
        !          3144: 
        !          3145:  * the ST RAM and TT RAM maps, and will fail for memory that
        !          3146: 
        !          3147:  * MiNT doesn't own or which is virtualized
        !          3148: 
        !          3149:  */
        !          3150: 
        !          3151: 
        !          3152: 
        !          3153: MEMREGION *
        !          3154: 
        !          3155: addr2region(addr)
        !          3156: 
        !          3157:        long addr;
        !          3158: 
        !          3159: {
        !          3160: 
        !          3161:        unsigned long ua = (unsigned long) addr;
        !          3162: 
        !          3163: 
        !          3164: 
        !          3165:        extern ulong mint_top_st, mint_top_tt;
        !          3166: 
        !          3167:        MEMREGION *r;
        !          3168: 
        !          3169:        MMAP map;
        !          3170: 
        !          3171: 
        !          3172: 
        !          3173:        if (ua < mint_top_st) {
        !          3174: 
        !          3175:                map = core;
        !          3176: 
        !          3177:        } else if (ua < mint_top_tt) {
        !          3178: 
        !          3179:                map = alt;
        !          3180: 
        !          3181:        } else {
        !          3182: 
        !          3183:                return 0;
        !          3184: 
        !          3185:        }
        !          3186: 
        !          3187: 
        !          3188: 
        !          3189:        for (r = *map; r; r = r->next) {
        !          3190: 
        !          3191:                if (addr >= r->loc && addr < r->loc + r->len)
        !          3192: 
        !          3193:                        return r;
        !          3194: 
        !          3195:        }
        !          3196: 
        !          3197:        return 0;
        !          3198: 
        !          3199: }
        !          3200: 
        !          3201: 
        !          3202: 
        !          3203: /*
        !          3204: 
1.1       root     3205:  * some debugging stuff
                   3206: 
                   3207:  */
                   3208: 
                   3209: 
                   3210: 
                   3211: void
                   3212: 
1.1.1.3 ! root     3213: DUMP_ALL_MEM()
        !          3214: 
        !          3215: {
        !          3216: 
        !          3217:        DUMPMEM(ker);
        !          3218: 
        !          3219:        DUMPMEM(core);
        !          3220: 
        !          3221:        DUMPMEM(alt);
        !          3222: 
        !          3223: }
        !          3224: 
        !          3225: 
        !          3226: 
        !          3227: void
        !          3228: 
1.1       root     3229: DUMPMEM(map)
                   3230: 
                   3231:        MMAP map;
                   3232: 
                   3233: {
                   3234: 
1.1.1.2   root     3235: #ifndef NO_DEBUG_INFO
                   3236: 
1.1       root     3237:        MEMREGION *m;
                   3238: 
                   3239: 
                   3240: 
                   3241:        m = *map;
                   3242: 
1.1.1.3 ! root     3243:        FORCE("%s memory dump: starting at region %lx",
        !          3244: 
        !          3245:                (map == ker ? "ker" : (map == core ? "core" : "alt")), m);
1.1       root     3246: 
                   3247:        while (m) {
                   3248: 
1.1.1.3 ! root     3249:            FORCE("%ld bytes at %lx (%d links); next region %lx", m->len, m->loc,
1.1       root     3250: 
1.1.1.3 ! root     3251:                    m->links, m->next);
1.1       root     3252: 
1.1.1.3 ! root     3253:            m = m->next;
1.1       root     3254: 
                   3255:        }
                   3256: 
1.1.1.2   root     3257: #endif
                   3258: 
1.1       root     3259: }
                   3260: 
                   3261: 
                   3262: 
                   3263: void
                   3264: 
                   3265: sanity_check(map)
                   3266: 
                   3267:        MMAP map;
                   3268: 
                   3269: {
                   3270: 
                   3271: #ifdef SANITY_CHECK
                   3272: 
                   3273:        MEMREGION *m, *nxt;
                   3274: 
                   3275:        long end;
                   3276: 
                   3277: 
                   3278: 
                   3279:        m = *map;
                   3280: 
                   3281:        while (m) {
                   3282: 
                   3283:                nxt = m->next;
                   3284: 
                   3285:                if (nxt) {
                   3286: 
                   3287:                        end = m->loc + m->len;
                   3288: 
                   3289:                        if (m->loc < nxt->loc && end > nxt->loc) {
                   3290: 
                   3291:                                FATAL("MEMORY CHAIN CORRUPTED");
                   3292: 
                   3293:                        }
                   3294: 
                   3295:                        else if (end == nxt->loc && ISFREE(m) && ISFREE(nxt)) {
                   3296: 
                   3297:                                ALERT("Continguous memory regions not merged!");
                   3298: 
                   3299:                        }
                   3300: 
                   3301:                }
                   3302: 
                   3303:                m = nxt;
                   3304: 
                   3305:        }
                   3306: 
1.1.1.2   root     3307: #else
                   3308: 
                   3309:        UNUSED(map);
                   3310: 
1.1       root     3311: #endif
                   3312: 
                   3313: }
                   3314: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.