struct vm_map *map;
vm_map_t map;
uvm_map_pageable_all(map, flags, limit)	struct vm_map *map;	int flags;	vsize_t limit;{	struct vm_map_entry *entry, *failed_entry;	vsize_t size;	int rv;#ifdef DIAGNOSTIC	u_int timestamp_save;#endif	UVMHIST_FUNC("uvm_map_pageable_all"); UVMHIST_CALLED(maphist);	UVMHIST_LOG(maphist,"(map=0x%x,flags=0x%x)", map, flags, 0, 0);	KASSERT(map->flags & VM_MAP_PAGEABLE);	vm_map_lock(map);	/*	 * handle wiring and unwiring separately.	 */	if (flags == 0) {			/* unwire */		/*		 * POSIX 1003.1b -- munlockall unlocks all regions,		 * regardless of how many times mlockall has been called.		 */		for (entry = map->header.next; entry != &map->header;		     entry = entry->next) {			if (VM_MAPENT_ISWIRED(entry))				uvm_map_entry_unwire(map, entry);		}		vm_map_modflags(map, 0, VM_MAP_WIREFUTURE);		vm_map_unlock(map);		UVMHIST_LOG(maphist,"<- done (OK UNWIRE)",0,0,0,0);		return 0;	}	if (flags & MCL_FUTURE) {		/*		 * must wire all future mappings; remember this.		 */		vm_map_modflags(map, VM_MAP_WIREFUTURE, 0);	}	if ((flags & MCL_CURRENT) == 0) {		/*		 * no more work to do!		 */		UVMHIST_LOG(maphist,"<- done (OK no wire)",0,0,0,0);		vm_map_unlock(map);		return 0;	}	/*	 * wire case: in three passes [XXXCDC: ugly block of code here]	 *	 * 1: holding the write lock, count all pages mapped by non-wired	 *    entries.  if this would cause us to go over our limit, we fail.	 *	 * 2: still holding the write lock, we create any anonymous maps that	 *    need to be created.  then we increment its wiring count.	 *	 * 3: we downgrade to a read lock, and call uvm_fault_wire to fault	 *    in the pages for any newly wired area (wired_count == 1).	 *	 *    downgrading to a read lock for uvm_fault_wire avoids a possible	 *    deadlock with another thread that may have faulted on one of	 *    the pages to be wired (it would mark the page busy, blocking	 *    us, then in turn block on the map lock that we hold).  because	 *    of problems in the recursive lock package, we cannot upgrade	 *    to a write lock in vm_map_lookup.  thus, any actions that	 *    require the write lock must be done beforehand.  because we	 *    keep the read lock on the map, the copy-on-write status of the	 *    entries we modify here cannot change.	 */	for (size = 0, entry = map->header.next; entry != &map->header;	     entry = entry->next) {		if (entry->protection != VM_PROT_NONE &&		    VM_MAPENT_ISWIRED(entry) == 0) { /* not already wired? */			size += entry->end - entry->start;		}	}	if (atop(size) + uvmexp.wired > uvmexp.wiredmax) {		vm_map_unlock(map);		return ENOMEM;	}	/* XXX non-pmap_wired_count case must be handled by caller */#ifdef pmap_wired_count	if (limit != 0 &&	    (size + ptoa(pmap_wired_count(vm_map_pmap(map))) > limit)) {		vm_map_unlock(map);		return ENOMEM;	}#endif	/*	 * Pass 2.	 */	for (entry = map->header.next; entry != &map->header;	     entry = entry->next) {		if (entry->protection == VM_PROT_NONE)			continue;		if (VM_MAPENT_ISWIRED(entry) == 0) { /* not already wired? */			/*			 * perform actions of vm_map_lookup that need the			 * write lock on the map: create an anonymous map			 * for a copy-on-write region, or an anonymous map			 * for a zero-fill region.  (XXXCDC: submap case			 * ok?)			 */			if (!UVM_ET_ISSUBMAP(entry)) {	/* not submap */				if (UVM_ET_ISNEEDSCOPY(entry) &&				    ((entry->protection & VM_PROT_WRITE) ||				     (entry->object.uvm_obj == NULL))) {					amap_copy(map, entry, M_WAITOK, TRUE,					    entry->start, entry->end);					/* XXXCDC: wait OK? */				}			}		}		entry->wired_count++;	}	/*	 * Pass 3.	 */#ifdef DIAGNOSTIC	timestamp_save = map->timestamp;#endif	vm_map_busy(map);	vm_map_downgrade(map);	rv = 0;	for (entry = map->header.next; entry != &map->header;	     entry = entry->next) {		if (entry->wired_count == 1) {			rv = uvm_fault_wire(map, entry->start, entry->end,			     entry->protection);			if (rv) {				/*				 * wiring failed.  break out of the loop.				 * we'll clean up the map below, once we				 * have a write lock again.				 */				break;			}		}	}	if (rv) {		/*		 * Get back an exclusive (write) lock.		 */		vm_map_upgrade(map);		vm_map_unbusy(map);#ifdef DIAGNOSTIC		if (timestamp_save != map->timestamp)			panic("uvm_map_pageable_all: stale map");#endif		/*		 * first drop the wiring count on all the entries		 * which haven't actually been wired yet.		 *		 * Skip VM_PROT_NONE entries like we did above.		 */		failed_entry = entry;		for (/* nothing */; entry != &map->header;		     entry = entry->next) {			if (entry->protection == VM_PROT_NONE)				continue;			entry->wired_count--;		}		/*		 * now, unwire all the entries that were successfully		 * wired above.		 *		 * Skip VM_PROT_NONE entries like we did above.		 */		for (entry = map->header.next; entry != failed_entry;		     entry = entry->next) {			if (entry->protection == VM_PROT_NONE)				continue;			entry->wired_count--;			if (VM_MAPENT_ISWIRED(entry))				uvm_map_entry_unwire(map, entry);		}		vm_map_unlock(map);		UVMHIST_LOG(maphist,"<- done (RV=%d)", rv,0,0,0);		return (rv);	}	/* We are holding a read lock here. */	vm_map_unbusy(map);	vm_map_unlock_read(map);	UVMHIST_LOG(maphist,"<- done (OK WIRE)",0,0,0,0);	return 0;}