uvm_loan.c

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

C
757 行
字号
	pg->loan_count++;	uvm_pagewire(pg);	/* always wire it */	uvm_unlock_pageq();	**output = pg;	*output = (*output) + 1;	/* unlock anon and return success */	if (pg->uobject)		simple_unlock(&pg->uobject->vmobjlock);	simple_unlock(&anon->an_lock);	return(1);}/* * uvm_loanuobj: loan a page from a uobj out * * => return value: *	-1 = fatal error, everything is unlocked, abort. *	 0 = lookup in ufi went stale, everything unlocked, relookup and *		try again *	 1 = got it, everything still locked */intuvm_loanuobj(ufi, output, flags, va)	struct uvm_faultinfo *ufi;	void ***output;	int flags;	vaddr_t va;{	struct vm_amap *amap = ufi->entry->aref.ar_amap;	struct uvm_object *uobj = ufi->entry->object.uvm_obj;	struct vm_page *pg;	struct vm_anon *anon;	int result, npages;	boolean_t locked;	/*	 * first we must make sure the page is resident.	 *	 * XXXCDC: duplicate code with uvm_fault().	 */	if (uobj->pgops->pgo_get) {		npages = 1;		pg = NULL;		result = uobj->pgops->pgo_get(uobj, va - ufi->entry->start,		    &pg, &npages, 0, VM_PROT_READ, MADV_NORMAL, PGO_LOCKED);	} else {		result = VM_PAGER_ERROR;	}	/*	 * check the result of the locked pgo_get.  if there is a problem,	 * then we fail the loan.	 */	if (result != VM_PAGER_OK && result != VM_PAGER_UNLOCK) {		uvmfault_unlockall(ufi, amap, uobj, NULL);		return(-1);	}	/*	 * if we need to unlock for I/O, do so now.	 */	if (result == VM_PAGER_UNLOCK) {		uvmfault_unlockall(ufi, amap, NULL, NULL);				npages = 1;		/* locked: uobj */		result = uobj->pgops->pgo_get(uobj, va - ufi->entry->start,		    &pg, &npages, 0, VM_PROT_READ, MADV_NORMAL, 0);		/* locked: <nothing> */				/*		 * check for errors		 */		if (result != VM_PAGER_OK) {			 if (result == VM_PAGER_AGAIN) {				tsleep((caddr_t)&lbolt, PVM, "fltagain2", 0);				return(0); /* redo the lookup and try again */			} 			return(-1);	/* total failure */		}		/*		 * pgo_get was a success.   attempt to relock everything.		 */		locked = uvmfault_relock(ufi);		if (locked && amap)			amap_lock(amap);		simple_lock(&uobj->vmobjlock);		/*		 * verify that the page has not be released and re-verify		 * that amap slot is still free.   if there is a problem we		 * drop our lock (thus force a lookup refresh/retry).		 */					if ((pg->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) {			if (pg->flags & PG_WANTED)				/* still holding object lock */				wakeup(pg);			if (pg->flags & PG_RELEASED) {#ifdef DIAGNOSTIC				if (uobj->pgops->pgo_releasepg == NULL)			panic("uvm_loanuobj: object has no releasepg function");#endif				/* frees page */				if (uobj->pgops->pgo_releasepg(pg, NULL))					simple_unlock(&uobj->vmobjlock);				return (0);			}			uvm_lock_pageq();			uvm_pageactivate(pg); /* make sure it is in queues */			uvm_unlock_pageq();			pg->flags &= ~(PG_BUSY|PG_WANTED);			UVM_PAGE_OWN(pg, NULL);			simple_unlock(&uobj->vmobjlock);			return (0);		}	}	/*	 * at this point we have the page we want ("pg") marked PG_BUSY for us	 * and we have all data structures locked.   do the loanout.   page can	 * not be PG_RELEASED (we caught this above).	 */	if ((flags & UVM_LOAN_TOANON) == 0) {	/* loan to wired-kernel page? */		uvm_lock_pageq();		if (pg->loan_count == 0)			pmap_page_protect(pg, VM_PROT_READ);		pg->loan_count++;		uvm_pagewire(pg);		uvm_unlock_pageq();		**output = pg;		*output = (*output) + 1;		if (pg->flags & PG_WANTED)			wakeup(pg);		pg->flags &= ~(PG_WANTED|PG_BUSY);		UVM_PAGE_OWN(pg, NULL);		return(1);		/* got it! */	}	/*	 * must be a loan to an anon.   check to see if there is already	 * an anon associated with this page.  if so, then just return	 * a reference to this object.   the page should already be 	 * mapped read-only because it is already on loan.	 */	if (pg->uanon) {		anon = pg->uanon;		simple_lock(&anon->an_lock);		anon->an_ref++;		simple_unlock(&anon->an_lock);		**output = anon;		*output = (*output) + 1;		uvm_lock_pageq();		uvm_pageactivate(pg);	/* reactivate */		uvm_unlock_pageq();		if (pg->flags & PG_WANTED)			wakeup(pg);		pg->flags &= ~(PG_WANTED|PG_BUSY);		UVM_PAGE_OWN(pg, NULL);		return(1);	}		/*	 * need to allocate a new anon	 */	anon = uvm_analloc();	if (anon == NULL) {		/* out of VM! */		if (pg->flags & PG_WANTED)			wakeup(pg);		pg->flags &= ~(PG_WANTED|PG_BUSY);		UVM_PAGE_OWN(pg, NULL);		uvmfault_unlockall(ufi, amap, uobj, NULL);		return(-1);	}	anon->u.an_page = pg;	pg->uanon = anon;	uvm_lock_pageq();	if (pg->loan_count == 0)		pmap_page_protect(pg, VM_PROT_READ);	pg->loan_count++;	uvm_pageactivate(pg);	uvm_unlock_pageq();	**output = anon;	*output = (*output) + 1;	if (pg->flags & PG_WANTED)		wakeup(pg);	pg->flags &= ~(PG_WANTED|PG_BUSY);	UVM_PAGE_OWN(pg, NULL);	return(1);}/* * uvm_loanzero: "loan" a zero-fill page out * * => return value: *	-1 = fatal error, everything is unlocked, abort. *	 0 = lookup in ufi went stale, everything unlocked, relookup and *		try again *	 1 = got it, everything still locked */intuvm_loanzero(ufi, output, flags)	struct uvm_faultinfo *ufi;	void ***output;	int flags;{	struct vm_anon *anon;	struct vm_page *pg;	if ((flags & UVM_LOAN_TOANON) == 0) {	/* loaning to kernel-page */		while ((pg = uvm_pagealloc(NULL, 0, NULL,		    UVM_PGA_ZERO)) == NULL) {			uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, 			    ufi->entry->object.uvm_obj, NULL);			uvm_wait("loanzero1");			if (!uvmfault_relock(ufi))				return(0);			if (ufi->entry->aref.ar_amap)				amap_lock(ufi->entry->aref.ar_amap);			if (ufi->entry->object.uvm_obj)				simple_lock(				    &ufi->entry->object.uvm_obj->vmobjlock);			/* ... and try again */		}				/* got a zero'd page; return */		pg->flags &= ~(PG_BUSY|PG_FAKE);		UVM_PAGE_OWN(pg, NULL);		**output = pg;		*output = (*output) + 1;		uvm_lock_pageq();		/* wire it as we are loaning to kernel-page */		uvm_pagewire(pg);		pg->loan_count = 1;		uvm_unlock_pageq();		return(1);	}	/* loaning to an anon */	while ((anon = uvm_analloc()) == NULL || 	    (pg = uvm_pagealloc(NULL, 0, anon, UVM_PGA_ZERO)) == NULL) {				/* unlock everything */		uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap,		       ufi->entry->object.uvm_obj, NULL);				/* out of swap causes us to fail */		if (anon == NULL)			return(-1);		uvm_anfree(anon);		uvm_wait("loanzero2");		/* wait for pagedaemon */		if (!uvmfault_relock(ufi))			/* map changed while unlocked, need relookup */			return (0);		/* relock everything else */		if (ufi->entry->aref.ar_amap)			amap_lock(ufi->entry->aref.ar_amap);		if (ufi->entry->object.uvm_obj)			simple_lock(&ufi->entry->object.uvm_obj->vmobjlock);		/* ... and try again */	}	/* got a zero'd page; return */	pg->flags &= ~(PG_BUSY|PG_FAKE);	UVM_PAGE_OWN(pg, NULL);	uvm_lock_pageq();	uvm_pageactivate(pg);	uvm_unlock_pageq();	**output = anon;	*output = (*output) + 1;	return(1);}/* * uvm_unloananon: kill loans on anons (basically a normal ref drop) * * => we expect all our resources to be unlocked */voiduvm_unloananon(aloans, nanons)	struct vm_anon **aloans;	int nanons;{	struct vm_anon *anon;	while (nanons-- > 0) {		int refs;		anon = *aloans++;		simple_lock(&anon->an_lock);		refs = --anon->an_ref;		simple_unlock(&anon->an_lock);		if (refs == 0) {			uvm_anfree(anon);	/* last reference: kill anon */		}	}}/* * uvm_unloanpage: kill loans on pages loaned out to the kernel * * => we expect all our resources to be unlocked */voiduvm_unloanpage(ploans, npages)	struct vm_page **ploans;	int npages;{	struct vm_page *pg;	uvm_lock_pageq();	while (npages-- > 0) {		pg = *ploans++;		if (pg->loan_count < 1)			panic("uvm_unloanpage: page %p isn't loaned", pg);		pg->loan_count--;		/* drop loan */		uvm_pageunwire(pg);		/* and wire */		/*		 * if page is unowned and we killed last loan, then we can		 * free it		 */		if (pg->loan_count == 0 && pg->uobject == NULL &&		    pg->uanon == NULL) {			if (pg->flags & PG_BUSY)	panic("uvm_unloanpage: page %p unowned but PG_BUSY!", pg);			/* be safe */			pmap_page_protect(pg, VM_PROT_NONE);			uvm_pagefree(pg);	/* pageq locked above */		}	}	uvm_unlock_pageq();}

⌨️ 快捷键说明

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