⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 space.c

📁 T-kernel 的extension源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
	(void)__UnmakeSpace(laddr, npage - np, lsid);err_ret1:	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;	VP	uatb;	PDE	*pde;	PTE	*pte;	PFE	*pfe;	PINFO	*pinfo;	W	i;	ER	err, error = E_OK;	/* For a shared space, specify "lsid = 0". */	if ( !isLocalSpace(laddr) ) {		lsid = 0;	}	/* When performed on a unique space, obtain PINFO on the process. */	pinfo = GetPINFO_lsid(lsid);	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.p == 0) ) {			/* No page table */			i = (W)N_PTE - (W)PTBL_NUM(la);			la = (VP)((VB*)la + (PAGESIZE * i));			np -= i;			continue;		}		/* Obtain page table address and page number */		pte = PFAtoLADR(pde->c.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 ( old.c.p != 0 ) {					ExtFlushCache(la, lsid);				}				/* Set invalid PTE */				pte[i].w = (UW)PTE_NONE;				PurgePageTLB(la, lsid);				/* 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. */			pde->w = PDE_NONE;			/* Release page frames for page tables. */			DiscardPageFrame(pfe);		}	}	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 and copy-on-write 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 = (UW)(PT_Writable|PT_CopyOnWrite);	VP	la  = laddr;	W	np  = npage;	VP	uatb;	PDE	*pde;	PTE	*pte, set;	PFE	*pfe;	W	i;	ER	err, error = E_OK;	/* For a shared space, specify "lsid = 0". */	if ( !isLocalSpace(laddr) ) {		lsid = 0;	}	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.p == 0) ) {			/* No page table */			i = (W)N_PTE - (W)PTBL_NUM(la);			la = (VP)((VB*)la + (PAGESIZE * i));			np -= i;			continue;		}		/* Obtain page table address. */		pte = PFAtoLADR(pde->c.pfa);		for ( i = (W)PTBL_NUM(la); i < (W)N_PTE; ++i ) {			/* Change attribute of page table. */			set.w = (pte[i].w & ~CHG_MSK) | chg_pte;			if ( ((set.w & (PT_Present | PT_Valid)) != 0)			  && ((set.w & (PT_Writable | PT_Update)) == PT_Update) ) {				pfe = PFAtoPFE(set.c.pfa);				if ( pfe != NULL ) {					pfe->upd = TRUE;				}				set.c.d = 0;			}			pte[i].w = set.w;			PurgePageTLB(la, lsid);			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 ){	T_TSKSPC	tskspc;	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;	}	if ( isLocalSpace(laddr) != 0 ) {		tskspc = GetTSKSPC_tid(TSK_SELF);	} else {		tskspc = GetTSKSPC_lsid(0);	}	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 = GetPDE(la, tskspc.uatb, &err);		if ( pde == NULL ) {			goto err_ret2;		}		if ( pde->c.p == 0 ) {			goto err_adr; /* No page table */		}		/* Obtain page table address. */		pte = PFAtoLADR(pde->c.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, tskspc.lsid);				if ( err < E_OK ) {					goto err_ret2;				}			}			if ( pte[i].c.cow != 0 ) {				/* Perform copy-on-write. */				err = CopyOnWrite(la, tskspc.lsid);				if ( err < E_OK ) {					goto err_ret2;				}			}			pfe = PFAtoPFE(pte[i].c.pfa);			/* Count the number of locking. */			err = LockCount(pfe, la, tskspc.lsid, (+1));			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, tskspc.lsid, TSD_ULS_VAL_M1);					err = E_NOMEM;					goto err_ret2;				}			}			pte[i].c.va = 0;  /* Indicate that page frames are locked. */			/* 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 ){	T_TSKSPC	tskspc;	VB		*la, *end;	PDE		*pde;	PTE		*pte;	PFE		*pfe;	W		i;	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;	}	if ( isLocalSpace(laddr) != 0 ) {		tskspc = GetTSKSPC_tid(TSK_SELF);	} else {		tskspc = GetTSKSPC_lsid(0);	}	end = (VB*)laddr + len;	la  = PageAlignL(laddr);	while ( la < end ) {		/* Obtain page directory entry */		pde = GetPDE(la, tskspc.uatb, &err);		if ( (pde == NULL) || (pde->c.p == 0) ) {			/* No page table */			error = ( pde == NULL )? err: E_MACV;			i = (W)N_PTE - (W)PTBL_NUM(la);			la += PAGESIZE * i;			continue;		}		/* Obtain page table address. */		pte = PFAtoLADR(pde->c.pfa);		for ( i = (W)PTBL_NUM(la); i < (W)N_PTE; ++i ) {			if ( (pte[i].c.p != 0) && (pte[i].c.va == 0) ) {				pfe = PFAtoPFE(pte[i].c.pfa);				/* Count the number of locking. */				err = LockCount(pfe, la, (UW)tskspc.lsid, TSD_ULS_VAL_M1);				if ( err < E_OK ) {					error = err;				}				/* When the number of locking becomes 0, unlock page frames. */				if ( err == 0 ) {					pte[i].c.va = 1; /* Indicate that page frames are unlocked. */					/* Reset the locked state of page frames. */					UnlockPageFrame(pfe);					/* If disk maps are writable,					   leave the cache disabled. */					if ( (pfe->dbn_id == PAGEFILE_ID)					  || (pte[i].c.w == 0) ) {						/* Enable the cache. */						pte[i].w |= (UW)PT_Cachable;					}					PurgePageTLB(la, (UW)tskspc.lsid);				}			} 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;	VP	uatb;	UW	lsid;	PDE	*pde;	PTE	*pte;	W	i;	UW	next_pfa;	W	plen;	UW	imask;	ER	err;	/* Non-logical spaces are handled in the same manner as physical addresses. */	if ( !isLogicalSpace(laddr) ) {		/* For the time being, do not include kseg0 area (except for ROM area)		   where the cache cannot be controlled on a page basis. */		if ( isKSEG0(laddr) && !isROM(laddr) ) {			return E_PAR;		}		*paddr = (VP)toPhysicalAddress(laddr);		return len;	}	/* Obtain a current space. */	lsid = CurrentLSID();	uatb = GetUATB_lsid(lsid);	la = laddr;	plen = 0;	next_pfa = 0;	do {		/* Obtain page directory entry */		pde = GetPDE(la, uatb, &err);		if ( (pde == NULL) || (pde->c.p == 0) ) {			return E_MACV;	/* No page table */		}		/* Obtain page table address. */		pte = PFAtoLADR(pde->c.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 + 1;			} 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 for the page table.			 * 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 ) {				pte[i].c.d = 1;			}			/* When the write buffer is on, the cache is off. */			if ( pte[i].c.wb == 0 ) {				/* Disable the cache. */				ExtFlushCache(la, lsid);				pte[i].w &= ~(UW)PT_Cachable;				PurgePageTLB(la, lsid);			}			EI(imask);			la = (VB*)laddr + plen;			if ( plen >= len ) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -