📄 space.c
字号:
/* *---------------------------------------------------------------------- * T-Kernel / Standard Extension * * Copyright (C) 2006 by Ken Sakamura. All rights reserved. * T-Kernel / Standard Extension is distributed * under the T-License for T-Kernel / Standard Extension. *---------------------------------------------------------------------- * * Version: 1.00.00 * Released by T-Engine Forum(http://www.t-engine.org) at 2006/8/11. * *---------------------------------------------------------------------- *//* * space.c (memory) * * Segment management (ARM920 Virtual storage version) * Logical space control * * (*) It is necessary to use some functions prior to T-Kernel startup. * As for shared space, it must be able to do so without T-Kernel * system call. * (Exercise care in processing related to Imalloc) */#include "segmgr.h"#include "prcinfo.h"#define TSD_ILS_MSK_0XFFFFC000 0xffffc000U#define TSD_ULS_VAL_M1 (-1)#define TSD_LSC_PPL_3 3#define TSD_GAI_DIV_4 4#define TSD_ILS_DIV_4 4#define TSD_ILS_CR_0X55555555 0x55555555UInline BOOL isCurrentSpace( UW lsid );Inline void SetCurPDE( VP laddr, PDE *pde );LOCAL PDE* GetPDE( VP laddr, VP uatb, ER *p_err );/* * Page directory position (logical address) * A unique space page directory is a table of unique spaces for the maximum number of * processes + 1. At the start is a unique space for "lsid = 0" used in tasks that do * not have unique spaces. */EXPORT PDE *SATB; /* Shared space */LOCAL PDE *UATB; /* Unique space */EXPORT UW MaxLSID; /* Maximum number of unique spaces *//* * Obtain LSID */EXPORT UW GetLSID_pinfo( PINFO *pinfo ){ return (UW)((((UB*)pinfo - (UB*)TopPI) / PINFO_sz) + 1);}EXPORT UW GetLSID_tid( ID taskid ){ return (UW)(GetTSKSPC_tid(taskid).lsid);}/* * Obtain UATB */EXPORT VP GetUATB_lsid( UW lsid ){ if ( lsid >= MaxLSID ) { DEBUG_PRINT(("GetUATB_lsid illegal lsid = %d\n", lsid)); lsid = 0; /* Invalid LSID */ } return UATB + (lsid * (UW)(NUM_PDIR_ENTRIES / TSD_GAI_DIV_4));}EXPORT VP GetUATB_tid( ID taskid ){ return GetTSKSPC_tid(taskid).uatb;}/* * Obtain PINFO */EXPORT PINFO* GetPINFO_lsid( UW lsid ){ if ( lsid <= 0U ) { return NULL; } if ( lsid >= MaxLSID ) { DEBUG_PRINT(("GetPINFO_lsid illegal lsid = %d\n", lsid)); return NULL; /* Invalid LSID */ } return (PINFO*)((UB*)TopPI + ((lsid - 1U) * (UW)PINFO_sz));}/* * Obtain T_TSKSPC */EXPORT T_TSKSPC GetTSKSPC_pinfo( PINFO *pinfo ){ T_TSKSPC tskspc; tskspc.lsid = (W)GetLSID_pinfo(pinfo); tskspc.uatb = GetUATB_lsid((UW)tskspc.lsid); return tskspc;}EXPORT T_TSKSPC GetTSKSPC_lsid( UW lsid ){ T_TSKSPC tskspc; tskspc.lsid = (W)(( lsid >= MaxLSID )? 0U: lsid); tskspc.uatb = GetUATB_lsid(lsid); return tskspc;}EXPORT T_TSKSPC GetTSKSPC_tid( ID taskid ){ T_TSKSPC tskspc; ER err; err = tk_get_tsp(taskid, &tskspc); if ( err < E_OK ) { DEBUG_PRINT(("GetTSKSPC_tid err = %d\n", err)); tskspc.lsid = 0; tskspc.uatb = UATB; } return tskspc;}/* * Switch logical spaces */EXPORT ER ChangeLogicalSpace( T_TSKSPC *curspc, T_TSKSPC chgspc ){ ER err; if ( curspc != NULL ) { err = tk_get_tsp(TSK_SELF, curspc); if ( err < E_OK ) { goto err_ret; } } err = tk_set_tsp(TSK_SELF, &chgspc); if ( err < E_OK ) { goto err_ret; } return E_OK;err_ret: DEBUG_PRINT(("ChangeLogicalSpace err = %d\n", err)); return err;}/* * If lsid unique space is the current space, return TRUE. */Inline BOOL isCurrentSpace( UW lsid ){ return ( GetLSID_tid(TSK_SELF) == lsid );}/* ------------------------------------------------------------------------ *//* * Obtain page directory entry */LOCAL PDE* GetPDE( VP laddr, VP uatb, ER *p_err ){ PDE *pde; if ( isLocalSpace(laddr) != 0 ) { /* Unique space */ if ( uatb == NULL ) { /* No unique space */ *p_err = E_PAR; goto err_ret; } pde = (PDE*)uatb + PDIR_NUM((UW)laddr - (UW)LOCALSPACE_TOP); } else { /* Shared space */ pde = SATB + PDIR_NUM(laddr); } return pde;err_ret: DEBUG_PRINT(("GetPDE err = %d\n", *p_err)); return NULL;}/* * Register entries in the current page directory. */Inline void SetCurPDE( VP laddr, PDE *pde ){ SATB[PDIR_NUM(laddr)] = *pde; WriteBackPageTable(&SATB[PDIR_NUM(laddr)]);}/* ------------------------------------------------------------------------ *//* * Page table access function *//* * Initialize page table access handle * "lsid = 0" indicates the current space. */EXPORT void InitPTH( PTH *pth, VP laddr, UW lsid ){ if ( lsid == 0U ) { /* Specify "uatb = SATB" based on the assumption that page directories are not changed. * When "lsid is 0", the function is also called from the page fault handler. * Therefore, TSK_SELF cannot be used to obtain the current space. */ pth->curspc = TRUE; pth->uatb = SATB + PDIR_NUM(LOCALSPACE_TOP); } else { pth->curspc = ( (!isLocalSpace(laddr)) || isCurrentSpace(lsid) ); pth->uatb = GetUATB_lsid(lsid); } pth->laddr = PageAlignL(laddr); pth->pde = NULL;}/* * Termination processing of page table access handle * When set at "purge = TRUE", purge all TLB. * When the intended address is located in a unique space, perform purging only if * this unique space is the current space. * When set at "purge = FALSE", do not purge TLB. */EXPORT void EndPTH( PTH *pth, BOOL purge ){ if ( purge && pth->curspc ) { PurgeAllTLB(); ExtFlushCacheWT(); }}/* * Move to the next page table entry. * Immediately after InitPTH(), move to the first page specified by InitPTH(). * Cannot move across a boundary between shared and unique spaces (operation is not guaranteed). */EXPORT ER NextPTE( PTH *pth ){ PDE *pde = pth->pde; ER err; if ( pde != NULL ) { /* Second and subsequent times: move to the next page */ pth->laddr = NextPage(pth->laddr); if ( ++(pth->i) >= (W)N_PTE ) { pde = NULL; } } if ( pde == NULL ) { /* Obtain page directory entry */ pde = GetPDE(pth->laddr, pth->uatb, &err); if ( pde == NULL ) { goto err_ret; } /* If there is no page table, return error. */ if ( pde->c[0].p == 0 ) { err = E_MACV; goto err_ret; } /* Position of page table entry */ pth->pde = pde; pth->pte = PFAtoLADR(pde->c[0].pfa); pth->i = (W)PTBL_NUM(pth->laddr); } return E_OK;err_ret: DEBUG_PRINT(("NextPTE err = %d\n", err)); return err;}/* * Fetch from the current page table */EXPORT UW GetPTE( PTH *pth ){ return (UW)CnvGetPTE(pth->pte[pth->i]).w;}/* * Set to the current page table * When set at "purge = TRUE", purge TLB of intended pages. * When the intended address is located in a unique space, perform purging only if * this unique space is the current space. * When set at "purge = FALSE", do not purge TLB. */EXPORT void SetPTE( PTH *pth, UW pte_v, BOOL purge ){ PTE *pte, new_pte; pte = &pth->pte[pth->i]; new_pte.w = pte_v; *pte = CnvSetPTE(new_pte); if ( pth->curspc != 0 ) { if ( purge != 0 ) { PurgePageTLB(pth->laddr, pte); } else { WriteBackPageTable(pte); } }}/* * Change the current page table. * pte = pte & pte_m | pte_v * * When set at "purge = TRUE", purge TLB of intended pages. * When the intended address is located in a unique space, perform purging only if * this unique space is the current space. * When set at "purge = FALSE", do not purge TLB. * * Return a page table setting value before change. */EXPORT UW ChgPTE( PTH *pth, UW pte_v, UW pte_m, BOOL purge ){ PTE *pte, old_pte, new_pte; UW imask; pte = &pth->pte[pth->i]; DI(imask); old_pte = CnvGetPTE(*pte); new_pte.w = (old_pte.w & pte_m) | pte_v; if ( pth->curspc && (old_pte.c.p != 0) ) { if ( new_pte.c.p == 0 ) { /* To cancel pages, flush and cancel the cache. */ (void)ExtFlushCacheWB(pth->laddr, 0); } else { /* Write back cache */ FlushDCache(pth->laddr, 0); } } *pte = CnvSetPTE(new_pte); if ( pth->curspc != 0 ) { if ( purge != 0 ) { PurgePageTLB(pth->laddr, pte); } else { WriteBackPageTable(pte); } } EI(imask); return old_pte.w;}/* ------------------------------------------------------------------------ *//* * Refer to page table */EXPORT UW GET_PTE( VP laddr, UW lsid ){ PDE *pde; PTE *pte; ER err; /* Obtain page directory */ pde = GetPDE(laddr, GetUATB_lsid(lsid), &err); if ( pde == NULL ) { goto err_ret; } /* If there is no page table, return error. */ if ( pde->c[0].p == 0 ) { err = E_MACV; goto err_ret; } pte = PFAtoLADR(pde->c[0].pfa); return (UW)CnvGetPTE(pte[PTBL_NUM(laddr)]).w;err_ret: DEBUG_PRINT(("GET_PTE err = %d\n", err)); return PTE_NONE;}/* * Change page table * pte = pte & pte_m | pte_v * * When set at "purge = TRUE", purge TLB of intended pages. * When the intended address is located in a unique space, perform purging only if * this unique space is the current space. * When set at "purge = FALSE", do not purge TLB. * * Return a page table setting value before change. */EXPORT UW CHG_PTE( VP laddr, UW lsid, UW pte_v, UW pte_m, BOOL purge ){ PDE *pde; PTE *pte; PTE old_pte, new_pte; BOOL curspc; UW imask; ER err; /* Performed in the current space: TRUE */ curspc = ( (!isLocalSpace(laddr)) || isCurrentSpace(lsid) ); /* Obtain page directory */ pde = GetPDE(laddr, GetUATB_lsid(lsid), &err); if ( pde == NULL ) { goto err_ret; } /* If there is no page table, return error. */ if ( pde->c[0].p == 0 ) { err = E_MACV; goto err_ret; } pte = PFAtoLADR(pde->c[0].pfa); pte += PTBL_NUM(laddr); DI(imask); old_pte = CnvGetPTE(*pte); new_pte.w = (old_pte.w & pte_m) | pte_v; if ( curspc && (old_pte.c.p != 0) ) { if ( new_pte.c.p == 0 ) { /* To cancel pages, flush and cancel the cache. */ (void)ExtFlushCacheWB(laddr, 0); } else { /* Write back cache */ FlushDCache(laddr, 0); } } *pte = CnvSetPTE(new_pte); if ( curspc != 0 ) { if ( purge != 0 ) { PurgePageTLB(laddr, pte); } else { WriteBackPageTable(pte); } } EI(imask); return old_pte.w;err_ret: DEBUG_PRINT(("CHG_PTE laddr = %#x lsid = %d err = %d\n", laddr, lsid, err)); return PTE_NONE;}/* ------------------------------------------------------------------------ *//* * Generate logical space * In lsid logical space, allocate virtual memory with set_pte attribute to npage of * area (starting from a page that includes a logical address laddr). If the logical * set_pte is a value (PTE) specified for a page table. */EXPORT ER __MakeSpace( VP laddr, W npage, UW lsid, UW set_pte ){ VP la = laddr; W np = npage; PTE set; BOOL curspc, purge; VP uatb; PDE *pde; PTE *pte; PFE *pfe; W i; ER err; /* Performed in the current space: TRUE */ curspc = ( (lsid > 0U) && isCurrentSpace(lsid) ); /* Necessary to purge TLB: TRUE */ purge = (!isLocalSpace(laddr)) || curspc; set.w = set_pte; uatb = GetUATB_lsid(lsid); while ( np > 0 ) { /* Obtain page directory entry */ pde = GetPDE(la, uatb, &err); if ( pde == NULL ) { goto err_ret; } /* If there is no page table, obtain a new one. */ if ( pde->c[0].p == 0 ) { /* Obtain page table */ pfe = GetPFM(PFS_lock, FALSE); if ( pfe == NULL ) { err = E_NOMEM; goto err_ret; } pte = PFEtoLADR(pfe); /* Initialize page table */ memset_w(pte, PTE_NONE, (size_t)N_PTE); /* Set page directory */ SetPDE(pde, PDE_NORM | (UW)toPhysicalAddress(pte)); WriteBackPageTable(pde); /* When changing the current unique space, update PDE of system page table as well. */ if ( isLocalSpace(la) && curspc ) { SetCurPDE(la, pde); } } else { /* Obtain page table address and page number */ pte = PFAtoLADR(pde->c[0].pfa); pfe = LADRtoPFE(pte); } /* Set page table */ for ( i = (W)PTBL_NUM(la); i < (W)N_PTE; ++i ) { if ( pte[i].w != PTE_NONE ) { /* This page is already used. */ err = E_MACV; goto err_ret; } pte[i] = set; if ( purge != 0 ) { PurgePageTLB(la, &pte[i]); }#if RESIDENT_ONLY /* If disk map is not used, make page tables resident here. * If disk map is used, make them resident by _MapDisk(). */ if ( pte[i].c.pfa >= PB_BASE ) { if ( pte[i].c.p == 0 ) { /* Page in. */ err = PageIn(la, lsid); if ( err < E_OK ) { goto err_ret; } } /* Set frames locks at locked state. */ LockPageFrame(PFAtoPFE(pte[i].c.pfa));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -