uvm_fault.c

来自「基于组件方式开发操作系统的OSKIT源代码」· C语言 代码 · 共 1,896 行 · 第 1/4 页

C
1,896 行
字号
				 * PG_BUSY|PG_RELEASED then pgo_get				 * made it PG_BUSY for us and gave				 * us a handle to it.   remember this				 * page as "uobjpage." (for later use).				 */								if (lcv == centeridx) {					uobjpage = pages[lcv];					UVMHIST_LOG(maphist, "  got uobjpage "					    "(0x%x) with locked get", 					    uobjpage, 0,0,0);					continue;				}					/* 				 * note: calling pgo_get with locked data				 * structures returns us pages which are				 * neither busy nor released, so we don't				 * need to check for this.   we can just				 * directly enter the page (after moving it				 * to the head of the active queue [useful?]).				 */				uvm_lock_pageq();				uvm_pageactivate(pages[lcv]);	/* reactivate */				uvm_unlock_pageq();				UVMHIST_LOG(maphist,				  "  MAPPING: n obj: pm=0x%x, va=0x%x, pg=0x%x",				  ufi.orig_map->pmap, currva, pages[lcv], 0);				uvmexp.fltnomap++;				/*				 * Since this page isn't the page that's				 * actually fauling, ignore pmap_enter()				 * failures; it's not critical that we				 * enter these right now.				 */				(void) pmap_enter(ufi.orig_map->pmap, currva,				    VM_PAGE_TO_PHYS(pages[lcv]),				    pages[lcv]->flags & PG_RDONLY ?				    VM_PROT_READ : enter_prot & MASK(ufi.entry),				    PMAP_CANFAIL |				     (wired ? PMAP_WIRED : 0));				/* 				 * NOTE: page can't be PG_WANTED or PG_RELEASED				 * because we've held the lock the whole time				 * we've had the handle.				 */				pages[lcv]->flags &= ~(PG_BUSY); /* un-busy! */				UVM_PAGE_OWN(pages[lcv], NULL);			}	/* for "lcv" loop */		}   /* "gotpages" != 0 */		/* note: object still _locked_ */	} else {		uobjpage = NULL;	}	/* locked (shadowed): maps(read), amap */	/* locked (!shadowed): maps(read), amap(if there), 		 uobj(if !null), uobjpage(if !null) */	/*	 * note that at this point we are done with any front or back pages.	 * we are now going to focus on the center page (i.e. the one we've	 * faulted on).  if we have faulted on the top (anon) layer	 * [i.e. case 1], then the anon we want is anons[centeridx] (we have	 * not touched it yet).  if we have faulted on the bottom (uobj)	 * layer [i.e. case 2] and the page was both present and available,	 * then we've got a pointer to it as "uobjpage" and we've already	 * made it BUSY.	 */	/*	 * there are four possible cases we must address: 1A, 1B, 2A, and 2B	 */	/*	 * redirect case 2: if we are not shadowed, go to case 2.	 */	if (shadowed == FALSE) 		goto Case2;	/* locked: maps(read), amap */	/*	 * handle case 1: fault on an anon in our amap	 */	anon = anons[centeridx];	UVMHIST_LOG(maphist, "  case 1 fault: anon=0x%x", anon, 0,0,0);	simple_lock(&anon->an_lock);	/* locked: maps(read), amap, anon */	/*	 * no matter if we have case 1A or case 1B we are going to need to	 * have the anon's memory resident.   ensure that now.	 */	/*	 * let uvmfault_anonget do the dirty work.	 * if it fails (!OK) it will unlock everything for us.	 * if it succeeds, locks are still valid and locked.	 * also, if it is OK, then the anon's page is on the queues.	 * if the page is on loan from a uvm_object, then anonget will	 * lock that object for us if it does not fail.	 */	result = uvmfault_anonget(&ufi, amap, anon);	switch (result) {	case VM_PAGER_OK:		break; 	case VM_PAGER_REFAULT:		goto ReFault;	case VM_PAGER_AGAIN:		tsleep(&lbolt, PVM, "fltagain1", 0);		goto ReFault;	default:#ifdef DIAGNOSTIC		panic("uvm_fault: uvmfault_anonget -> %d", result);#else		return (KERN_PROTECTION_FAILURE);#endif	}	/*	 * uobj is non null if the page is on loan from an object (i.e. uobj)	 */	uobj = anon->u.an_page->uobject;	/* locked by anonget if !NULL */	/* locked: maps(read), amap, anon, uobj(if one) */	/*	 * special handling for loaned pages 	 */	if (anon->u.an_page->loan_count) {		if ((access_type & VM_PROT_WRITE) == 0) {						/*			 * for read faults on loaned pages we just cap the			 * protection at read-only.			 */			enter_prot = enter_prot & ~VM_PROT_WRITE;		} else {			/*			 * note that we can't allow writes into a loaned page!			 *			 * if we have a write fault on a loaned page in an			 * anon then we need to look at the anon's ref count.			 * if it is greater than one then we are going to do			 * a normal copy-on-write fault into a new anon (this			 * is not a problem).  however, if the reference count			 * is one (a case where we would normally allow a			 * write directly to the page) then we need to kill			 * the loan before we continue.			 */			/* >1 case is already ok */			if (anon->an_ref == 1) {				/* get new un-owned replacement page */				pg = uvm_pagealloc(NULL, 0, NULL, 0);				if (pg == NULL) {					uvmfault_unlockall(&ufi, amap, uobj,					    anon);					uvm_wait("flt_noram2");					goto ReFault;				}				/*				 * copy data, kill loan, and drop uobj lock				 * (if any)				 */				/* copy old -> new */				uvm_pagecopy(anon->u.an_page, pg);				/* force reload */				pmap_page_protect(anon->u.an_page,						  VM_PROT_NONE);				uvm_lock_pageq();	  /* KILL loan */				if (uobj)					/* if we were loaning */					anon->u.an_page->loan_count--;				anon->u.an_page->uanon = NULL;				/* in case we owned */				anon->u.an_page->pqflags &= ~PQ_ANON;				uvm_unlock_pageq();				if (uobj) {					simple_unlock(&uobj->vmobjlock);					uobj = NULL;				}				/* install new page in anon */				anon->u.an_page = pg;				pg->uanon = anon;				pg->pqflags |= PQ_ANON;				pg->flags &= ~(PG_BUSY|PG_FAKE);				UVM_PAGE_OWN(pg, NULL);				/* done! */			}     /* ref == 1 */		}       /* write fault */	}         /* loan count */	/*	 * if we are case 1B then we will need to allocate a new blank	 * anon to transfer the data into.   note that we have a lock	 * on anon, so no one can busy or release the page until we are done.	 * also note that the ref count can't drop to zero here because	 * it is > 1 and we are only dropping one ref.	 *	 * in the (hopefully very rare) case that we are out of RAM we 	 * will unlock, wait for more RAM, and refault.    	 *	 * if we are out of anon VM we kill the process (XXX: could wait?).	 */	if ((access_type & VM_PROT_WRITE) != 0 && anon->an_ref > 1) {		UVMHIST_LOG(maphist, "  case 1B: COW fault",0,0,0,0);		uvmexp.flt_acow++;		oanon = anon;		/* oanon = old, locked anon */		anon = uvm_analloc();		if (anon)			pg = uvm_pagealloc(NULL, 0, anon, 0);		/* check for out of RAM */		if (anon == NULL || pg == NULL) {			if (anon)				uvm_anfree(anon);			uvmfault_unlockall(&ufi, amap, uobj, oanon);			KASSERT(uvmexp.swpgonly <= uvmexp.swpages);			if (anon == NULL || uvmexp.swpgonly == uvmexp.swpages) {				UVMHIST_LOG(maphist,				    "<- failed.  out of VM",0,0,0,0);				uvmexp.fltnoanon++;				return (KERN_RESOURCE_SHORTAGE);			}			uvmexp.fltnoram++;			uvm_wait("flt_noram3");	/* out of RAM, wait for more */			goto ReFault;		}		/* got all resources, replace anon with nanon */		uvm_pagecopy(oanon->u.an_page, pg);	/* pg now !PG_CLEAN */		pg->flags &= ~(PG_BUSY|PG_FAKE);	/* un-busy! new page */		UVM_PAGE_OWN(pg, NULL);		amap_add(&ufi.entry->aref, ufi.orig_rvaddr - ufi.entry->start,		    anon, 1);		/* deref: can not drop to zero here by defn! */		oanon->an_ref--;			 		/*		 * note: oanon still locked.   anon is _not_ locked, but we		 * have the sole references to in from amap which _is_ locked.		 * thus, no one can get at it until we are done with it.		 */	} else {		uvmexp.flt_anon++;		oanon = anon;		/* old, locked anon is same as anon */		pg = anon->u.an_page;		if (anon->an_ref > 1)     /* disallow writes to ref > 1 anons */			enter_prot = enter_prot & ~VM_PROT_WRITE;	}	/* locked: maps(read), amap, oanon */	/*	 * now map the page in ...	 * XXX: old fault unlocks object before pmap_enter.  this seems	 * suspect since some other thread could blast the page out from	 * under us between the unlock and the pmap_enter.	 */	UVMHIST_LOG(maphist, "  MAPPING: anon: pm=0x%x, va=0x%x, pg=0x%x",	    ufi.orig_map->pmap, ufi.orig_rvaddr, pg, 0);	if (pmap_enter(ufi.orig_map->pmap, ufi.orig_rvaddr, VM_PAGE_TO_PHYS(pg),	    enter_prot, access_type | PMAP_CANFAIL | (wired ? PMAP_WIRED : 0))	    != KERN_SUCCESS) {		/*		 * No need to undo what we did; we can simply think of		 * this as the pmap throwing away the mapping information.		 *		 * We do, however, have to go through the ReFault path,		 * as the map may change while we're asleep.		 */		uvmfault_unlockall(&ufi, amap, uobj, oanon);		KASSERT(uvmexp.swpgonly <= uvmexp.swpages);		if (uvmexp.swpgonly == uvmexp.swpages) {			UVMHIST_LOG(maphist,			    "<- failed.  out of VM",0,0,0,0);			/* XXX instrumentation */			return (KERN_RESOURCE_SHORTAGE);		}		/* XXX instrumentation */		uvm_wait("flt_pmfail1");		goto ReFault;	}	/*	 * ... update the page queues.	 */	uvm_lock_pageq();	if (fault_type == VM_FAULT_WIRE) {		uvm_pagewire(pg);		/*		 * since the now-wired page cannot be paged out,		 * release its swap resources for others to use.		 * since an anon with no swap cannot be PG_CLEAN,		 * clear its clean flag now.		 */		pg->flags &= ~(PG_CLEAN);		uvm_anon_dropswap(anon);	} else {		/* activate it */		uvm_pageactivate(pg);	}	uvm_unlock_pageq();	/*	 * done case 1!  finish up by unlocking everything and returning success	 */	uvmfault_unlockall(&ufi, amap, uobj, oanon);	return (KERN_SUCCESS);Case2:	/*	 * handle case 2: faulting on backing object or zero fill	 */	/*	 * locked:	 * maps(read), amap(if there), uobj(if !null), uobjpage(if !null)	 */	/*	 * note that uobjpage can not be PGO_DONTCARE at this point.  we now	 * set uobjpage to PGO_DONTCARE if we are doing a zero fill.  if we	 * have a backing object, check and see if we are going to promote	 * the data up to an anon during the fault.	 */	if (uobj == NULL) {		uobjpage = PGO_DONTCARE;			promote = TRUE;		/* always need anon here */	} else {		KASSERT(uobjpage != PGO_DONTCARE);		promote = (access_type & VM_PROT_WRITE) &&		     UVM_ET_ISCOPYONWRITE(ufi.entry);	}	UVMHIST_LOG(maphist, "  case 2 fault: promote=%d, zfill=%d",	    promote, (uobj == NULL), 0,0);	/*	 * if uobjpage is not null then we do not need to do I/O to get the	 * uobjpage.	 *	 * if uobjpage is null, then we need to unlock and ask the pager to 	 * get the data for us.   once we have the data, we need to reverify	 * the state the world.   we are currently not holding any resources.	 */	if (uobjpage) {		/* update rusage counters */		curproc->p_addr->u_stats.p_ru.ru_minflt++;	} else {		/* update rusage counters */		curproc->p_addr->u_stats.p_ru.ru_majflt++;				/* locked: maps(read), amap(if there), uobj */		uvmfault_unlockall(&ufi, amap, NULL, NULL);		/* locked: uobj */		uvmexp.fltget++;		gotpages = 1;		uoff = (ufi.orig_rvaddr - ufi.entry->start) + ufi.entry->offset;		result = uobj->pgops->pgo_get(uobj, uoff, &uobjpage, &gotpages,		    0, access_type & MASK(ufi.entry), ufi.entry->advice,		    PGO_SYNCIO);		/* locked: uobjpage(if result OK) */		/*		 * recover from I/O		 */		if (result != VM_PAGER_OK) {			KASSERT(result != VM_PAGER_PEND);			if (result == VM_PAGER_AGAIN) {				UVMHIST_LOG(maphist,				    "  pgo_get says TRY AGAIN!",0,0,0,0);				tsleep((caddr_t)&lbolt, PVM, "fltagain2", 0);				goto ReFault;			}			UVMHIST_LOG(maphist, "<- pgo_get failed (code %d)",			    result, 0,0,0);			return (KERN_PROTECTION_FAILURE); /* XXX i/o error */		}		/* locked: uobjpage */		/*		 * re-verify the state of the world by first trying to relock		 * the maps.  always relock the object.		 */		locked = uvmfault_relock(&ufi);		if (locked && amap)			amap_lock(amap);		simple_lock(&uobj->vmobjlock);				/* locked(locked): maps(read), amap(if !null), uobj, uobjpage */		/* locked(!locked): uobj, uobjpage */		/*		 * verify that the page has not be released and re-verify		 * that amap slot is still free.   if there is a problem,		 * we unlock and clean up.		 */		if ((uobjpage->flags & PG_RELEASED) != 0 ||		    (locked && amap && 		    amap_lookup(&ufi.entry->aref,		      ufi.orig_rvaddr - ufi.entry->start))) {			if (locked) 				uvmfault_unlockall(&ufi, amap, NULL, NULL);			locked = FALSE;		}		/*		 * didn't get the lock?   release the page and retry.		 */		if (locked == FALSE) {			UVMHIST_LOG(maphist,			    "  wasn't able to relock after fault: retry", 			    0,0,0,0);			if (uobjpage->flags & PG_WANTED)				/* still holding object lock */				wakeup(uobjpage);			if (uobjpage->flags & PG_RELEASED) {				uvmexp.fltpgrele++;				KASSERT(uobj->pgops->pgo_releasepg != NULL);				/* frees page */

⌨️ 快捷键说明

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