📄 space.c
字号:
}#endif pfe->dbn_no++; /* Number of valid pages */ la = NextPage(la); /* Next page */ if ( --np <= 0 ) { break; } } } return E_OK;err_ret: (void)__UnmakeSpace(laddr, npage - np, lsid); DEBUG_PRINT(("__MakeSpace err = %d\n", err)); return err;}EXPORT ER _MakeSpace( VP laddr, W npage, UW lsid, UW set_pte ){ ER err; if ( (set_pte & (UW)(PT_Present|PT_Valid)) != 0U ) { err = E_PAR; goto err_ret; } LockSEG(); err = __MakeSpace(laddr, npage, lsid, set_pte); UnlockSEG(); return err;err_ret: DEBUG_PRINT(("_MakeSpace err = %d\n", err)); return err;}/* * Delete logical space * In lsid logical space, delete virtual memory with set_pte attribute allocated to npage * of area (starting from a page that includes a logical address laddr). If the logical * space does not have a process unique space, specify "lsid = 0". * Locked pages are unlocked. */EXPORT ER __UnmakeSpace( VP laddr, W npage, UW lsid ){ VP la = laddr; W np = npage; BOOL curspc, purge; VP uatb; PDE *pde; PTE *pte; PFE *pfe; PINFO *pinfo; W i; ER err, error = E_OK; /* Performed in the current space: TRUE */ curspc = ( (lsid > 0U) && isCurrentSpace(lsid) ); /* Necessary to purge TLB: TRUE */ purge = (!isLocalSpace(laddr)) || curspc; /* When performed on a unique space, obtain PINFO on the process. */ pinfo = ( isLocalSpace(laddr) != 0 )? GetPINFO_lsid(lsid): NULL; uatb = GetUATB_lsid(lsid); while ( np > 0 ) { /* Obtain page directory entry */ pde = GetPDE(la, uatb, &err); if ( pde == NULL ) { error = err; } if ( (pde == NULL) || (pde->c[0].p == 0) ) { /* No page table */ i = (W)(N_PTE - PTBL_NUM(la)); la = (VP)((VB*)la + (PAGESIZE * i)); np -= i; continue; } /* Obtain page table address and page number */ pte = PFAtoLADR(pde->c[0].pfa); pfe = LADRtoPFE(pte); for ( i = (W)PTBL_NUM(la); i < (W)N_PTE; ++i ) { PTE old; old = pte[i]; if ( old.w != (UW)PTE_NONE ) { /* If pages are valid, flush the cache. */ if ( purge && (old.c.p != 0) ) { (void)ExtFlushCacheWB(la, 0); } /* Set invalid PTE */ pte[i].w = PTE_NONE; if ( purge != 0 ) { PurgePageTLB(la, &pte[i]); } /* Discontinue relations with PTE. */ err = UnlinkPage(old, pinfo); if ( err < E_OK ) { error = err; } pfe->dbn_no--; /* Number of valid pages */ } la = NextPage(la); /* Next page */ if ( --np <= 0 ) { break; } } if ( pfe->dbn_no == 0 ) { /* When a page table becomes empty, delete the page table itself. */ SetPDE(pde, PDE_NONE); WriteBackPageTable(pde); /* When changing the current unique space, update PDE of system page table as well. */ if ( isLocalSpace((VB*)la - PAGESIZE) && curspc ) { SetCurPDE((VB*)la - PAGESIZE, pde); } /* Release page frames for page tables. */ DiscardPageFrame(pfe); } } if ( purge != 0 ) { ExtFlushCacheWT(); } if ( error < E_OK ) { goto err_ret; } return E_OK;err_ret: DEBUG_PRINT(("__UnmakeSpace err = %d\n", error)); return error;}EXPORT ER _UnmakeSpace( VP laddr, W npage, UW lsid ){ ER err; LockSEG(); err = __UnmakeSpace(laddr, npage, lsid); UnlockSEG(); return err;}/* * Change logical space attribute * In lsid logical space, change attributes of virtual memory allocated to npage of area * (starting from a page that includes a logical address laddr) into chg_pte. * Change attributes related to specification of access mode only. * The other attributes of page table are not changed. * If the logical space does not have a process unique space, specify "lsid = 0". */EXPORT ER __ChangeSpace( VP laddr, W npage, UW lsid, UW chg_pte ){const UW CHG_MSK = (PT_Writable|PT_User); VP la = laddr; W np = npage; BOOL curspc, purge; VP uatb; PDE *pde; PTE *pte; W i; ER err, error = E_OK; /* Performed in the current unique space: TRUE */ curspc = ( (lsid > 0U) && isCurrentSpace(lsid) ); /* Necessary to purge TLB: TRUE */ purge = (!isLocalSpace(laddr)) || curspc; chg_pte &= CHG_MSK; uatb = GetUATB_lsid(lsid); while ( np > 0 ) { /* Obtain page directory entry */ pde = GetPDE(la, uatb, &err); if ( pde == NULL ) { error = err; } if ( (pde == NULL) || (pde->c[0].p == 0) ) { /* No page table */ i = (W)(N_PTE - PTBL_NUM(la)); la = (VP)((VB*)la + (PAGESIZE * i)); np -= i; continue; } /* Obtain page table address. */ pte = PFAtoLADR(pde->c[0].pfa); for ( i = (W)PTBL_NUM(la); i < (W)N_PTE; ++i ) { /* Change attribute of page table. */ pte[i].w = (pte[i].w & ~CHG_MSK) | chg_pte; if ( pte[i].c.p != 0 ) { SetPTE_AP(&pte[i], pte[i].a.ap0); } if ( purge != 0 ) { PurgePageTLB(la, &pte[i]); } la = NextPage(la); /* Next page */ if ( --np <= 0 ) { break; } } } if ( error < E_OK ) { goto err_ret; } return E_OK;err_ret: DEBUG_PRINT(("__ChangeSpace err = %d\n", error)); return error;}EXPORT ER _ChangeSpace( VP laddr, W npage, UW lsid, UW chg_pte ){ ER err; LockSEG(); err = __ChangeSpace(laddr, npage, lsid, chg_pte); UnlockSEG(); return err;}/* * Lock logical space * Allocate real memory to len bytes of area (starting from a logical address laddr) * to make the area resident (lock the area). */EXPORT ER __LockSpace( VP laddr, W len ){ UW lsid; VB *la, *end; PDE *pde; PTE *pte; PFE *pfe; W i, limit_new, limit_exs; BOOL newpg; ER err; if ( len <= 0 ) { err = E_PAR; goto err_ret1; } /* Non-logical spaces are always made resident. */ if ( !isLogicalSpace(laddr) ) { return E_OK; } lsid = ( isLocalSpace(laddr) != 0U )? GetLSID_tid(TSK_SELF): 0U; end = (VB*)laddr + len; la = PageAlignL(laddr); /* getting the number of pages to enable to resident * limit_new : the number of new pages to enable to resident * limit_exs : the number of existing pages to enable to resident */ limit_new = LockablePages(&limit_exs); while ( la < end ) { /* Obtain page directory entry */ pde = SATB + PDIR_NUM(la); if ( pde->c[0].p == 0 ) { goto err_adr; /*No page table*/ } /* Obtain page table address. */ pte = PFAtoLADR(pde->c[0].pfa); for ( i = (W)PTBL_NUM(la); i < N_PTE; ++i ) { if ( pte[i].w == (UW)PTE_NONE ) { goto err_adr; /* Invalid PTE */ } newpg = ( (pte[i].c.p == 0) && (pte[i].c.va == 0) ); if ( pte[i].c.p == 0 ) { /* Page in. */ err = PageIn(la, lsid); if ( err < E_OK ) { goto err_ret2; } } if ( err == 1 ) { /* the case of first residentation */ if ( (!newpg) && (limit_exs > 0) ) { --limit_exs; } else if ( --limit_new < 0 ) { LockCount(pfe, la, lsid, TSD_ULS_VAL_M1); err = E_NOMEM; goto err_ret2; } } pfe = PFAtoPFE(pte[i].c.pfa); /* Count the number of locking. */ err = LockCount(pfe, la, lsid, (+1)); if ( err < E_OK ) { goto err_ret2; } /* Set frames locks at locked state. */ LockPageFrame(pfe); la += PAGESIZE; if ( la >= end ) { break; } } } return E_OK;err_adr: err = E_MACV;err_ret2: (void)__UnlockSpace(laddr, la - (VB*)laddr);err_ret1: DEBUG_PRINT(("__LockSpace err = %d\n", err)); return err;}EXPORT ER _LockSpace( VP laddr, W len ){ ER err; LockSEG(); err = __LockSpace(laddr, len); UnlockSEG(); return err;}/* * Unlock logcal space * Make len bytes of area (starting from a logical address laddr) non-resident (unlock the area). */EXPORT ER __UnlockSpace( VP laddr, W len ){ UW lsid; VB *la, *end; PDE *pde; PTE *pte; PFE *pfe; W i, n; ER err, error = E_OK; if ( len <= 0 ) { error = E_PAR; goto err_ret; } /* Non-logical spaces are always made resident. */ if ( !isLogicalSpace(laddr) ) { return E_OK; } lsid = ( isLocalSpace(laddr) != 0 )? GetLSID_tid(TSK_SELF): 0U; end = (VB*)laddr + len; la = PageAlignL(laddr); while ( la < end ) { /* Obtain page directory entry */ pde = SATB + PDIR_NUM(la); if ( pde->c[0].p == 0 ) { /* No page table */ error = E_MACV; i = (W)(N_PTE - PTBL_NUM(la)); la += PAGESIZE * i; continue; } /* Obtain page table address. */ pte = PFAtoLADR(pde->c[0].pfa); for ( i = (W)PTBL_NUM(la); i < (W)N_PTE; ++i ) { if ( pte[i].c.p != 0 ) { pfe = PFAtoPFE(pte[i].c.pfa); /* Count the number of locking. */ err = LockCount(pfe, la, lsid, TSD_ULS_VAL_M1); if ( err < E_OK ) { error = err; } /* When the number of locking becomes 0, unlock page frames. */ if ( err == 0 ) {#if !RESIDENT_ONLY /* Reset the locked state of page frames. */ UnlockPageFrame(pfe);#endif /* If disk maps are writable, disable the cache. */ if ( (pfe->dbn_id == PAGEFILE_ID) || (pte[i].c.w == 0) ) { n = (W)(PT_Cachable | PT_Bufferable); } else { n = PT_Bufferable; } pte[i].w |= (UW)n; PurgePageTLB(la, &pte[i]); } } else { error = E_MACV; } la += PAGESIZE; if ( la >= end ) { break; } } }err_ret:#ifdef DEBUG if ( error < E_OK ) { DEBUG_PRINT(("__UnlockSpace err = %d\n", error)); }#endif return error;}EXPORT ER _UnlockSpace( VP laddr, W len ){ ER err; LockSEG(); err = __UnlockSpace(laddr, len); UnlockSEG(); return err;}/* * Obtain physical address * In a logical space of the task, determine the physical address of logical address * laddr and return it as *paddr. As a return value, return the number of bytes of * consecutive physical memory in len bytes of area starting from laddr. * In the returned bytes of area starting from laddr, the cache is disabled. * The disabled cache is reset by UnlockSpace(). * It is necessary to lock it in advance by LockSpace(). * * (*) Do not execute LockSEG(). * It causes deadlock when called from the disk driver. * (*) Note that page fault may occur when access is made to *paddr. */EXPORT W _CnvPhysicalAddr( VP laddr, W len, VP *paddr ){ VP la; PDE *pde; PTE *pte; W i; UW next_pfa = 0; W plen; UW imask; /* Since the cache cannot be controlled in a non-logical space, return error. (except for ROM area) */ if ( !isLogicalSpace(laddr) ) { if ( !isROM(laddr) ) { return E_PAR; } *paddr = toPhysicalAddress(laddr); return len; } la = laddr; plen = 0; do { /* Obtain page directory entry */ pde = SATB + PDIR_NUM(la); if ( pde->c[0].p == 0 ) { return E_MACV; /* No page table */ } /* Obtain page table address. */ pte = PFAtoLADR(pde->c[0].pfa); for ( i = (W)PTBL_NUM(la); i < (W)N_PTE; ++i ) { if ( pte[i].c.p == 0 ) { break; /* Page is absent. */ } if ( plen == 0 ) { /* First page */ *paddr = (VB*)PFAtoPADR(pte[i].c.pfa) + POFS_NUM(la); plen = PAGESIZE - (W)POFS_NUM(la); next_pfa = pte[i].c.pfa + 1U; } else { if ( pte[i].c.pfa != next_pfa ) { break; /* Non-consecutive */ } plen += PAGESIZE; next_pfa++; } DI(imask); /* In DMA transfer, an update flag is not set. * Set an update flag based on the assumption that physical * address are used for DMA transfer. * However, this is not the case if writing is disabled. */ if ( pte[i].c.w != 0 ) { PFAtoPFE(pte[i].c.pfa)->upd = TRUE; } /* Disable the cache. */ (void)ExtFlushCacheWB(la, 0); ExtFlushCacheWT(); pte[i].w &= ~(UW)(PT_Cachable|PT_Bufferable); PurgePageTLB(la, &pte[i]); EI(imask); la = (VB*)laddr + plen; if ( plen >= len ) { break; } } } while ( i == (W)N_PTE ); if ( plen > len ) { plen = len; } return plen;}/* * Check access attribute of logical space * In a logical space of the task, check if it is possible to make access to len bytes * of area (starting from a logical address laddr) from env environment in mode. * If it is possible, return E_OK; if not possible, return E_MACV. * * (*) Do not execute LockSEG(). * It causes deadlock when called from the disk driver. */EXPORT ER _ChkSpace( VP laddr, W len, UW mode, UW env )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -