10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51899Svsakar * Common Development and Distribution License (the "License"). 61899Svsakar * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 221899Svsakar * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 270Sstevel@tonic-gate /* All Rights Reserved */ 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* 300Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 310Sstevel@tonic-gate * The Regents of the University of California 320Sstevel@tonic-gate * All Rights Reserved 330Sstevel@tonic-gate * 340Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 350Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 360Sstevel@tonic-gate * contributors. 370Sstevel@tonic-gate */ 380Sstevel@tonic-gate 390Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 400Sstevel@tonic-gate 410Sstevel@tonic-gate /* 420Sstevel@tonic-gate * VM - address spaces. 430Sstevel@tonic-gate */ 440Sstevel@tonic-gate 450Sstevel@tonic-gate #include <sys/types.h> 460Sstevel@tonic-gate #include <sys/t_lock.h> 470Sstevel@tonic-gate #include <sys/param.h> 480Sstevel@tonic-gate #include <sys/errno.h> 490Sstevel@tonic-gate #include <sys/systm.h> 500Sstevel@tonic-gate #include <sys/mman.h> 510Sstevel@tonic-gate #include <sys/sysmacros.h> 520Sstevel@tonic-gate #include <sys/cpuvar.h> 530Sstevel@tonic-gate #include <sys/sysinfo.h> 540Sstevel@tonic-gate #include <sys/kmem.h> 550Sstevel@tonic-gate #include <sys/vnode.h> 560Sstevel@tonic-gate #include <sys/vmsystm.h> 570Sstevel@tonic-gate #include <sys/cmn_err.h> 580Sstevel@tonic-gate #include <sys/debug.h> 590Sstevel@tonic-gate #include <sys/tnf_probe.h> 600Sstevel@tonic-gate #include <sys/vtrace.h> 610Sstevel@tonic-gate 620Sstevel@tonic-gate #include <vm/hat.h> 630Sstevel@tonic-gate #include <vm/xhat.h> 640Sstevel@tonic-gate #include <vm/as.h> 650Sstevel@tonic-gate #include <vm/seg.h> 660Sstevel@tonic-gate #include <vm/seg_vn.h> 670Sstevel@tonic-gate #include <vm/seg_dev.h> 680Sstevel@tonic-gate #include <vm/seg_kmem.h> 690Sstevel@tonic-gate #include <vm/seg_map.h> 700Sstevel@tonic-gate #include <vm/seg_spt.h> 710Sstevel@tonic-gate #include <vm/page.h> 720Sstevel@tonic-gate 730Sstevel@tonic-gate clock_t deadlk_wait = 1; /* number of ticks to wait before retrying */ 740Sstevel@tonic-gate 750Sstevel@tonic-gate static struct kmem_cache *as_cache; 760Sstevel@tonic-gate 770Sstevel@tonic-gate static void as_setwatchprot(struct as *, caddr_t, size_t, uint_t); 780Sstevel@tonic-gate static void as_clearwatchprot(struct as *, caddr_t, size_t); 791899Svsakar int as_map_locked(struct as *, caddr_t, size_t, int ((*)()), void *); 800Sstevel@tonic-gate 810Sstevel@tonic-gate 820Sstevel@tonic-gate /* 830Sstevel@tonic-gate * Verifying the segment lists is very time-consuming; it may not be 840Sstevel@tonic-gate * desirable always to define VERIFY_SEGLIST when DEBUG is set. 850Sstevel@tonic-gate */ 860Sstevel@tonic-gate #ifdef DEBUG 870Sstevel@tonic-gate #define VERIFY_SEGLIST 880Sstevel@tonic-gate int do_as_verify = 0; 890Sstevel@tonic-gate #endif 900Sstevel@tonic-gate 910Sstevel@tonic-gate /* 920Sstevel@tonic-gate * Allocate a new callback data structure entry and fill in the events of 930Sstevel@tonic-gate * interest, the address range of interest, and the callback argument. 940Sstevel@tonic-gate * Link the entry on the as->a_callbacks list. A callback entry for the 950Sstevel@tonic-gate * entire address space may be specified with vaddr = 0 and size = -1. 960Sstevel@tonic-gate * 970Sstevel@tonic-gate * CALLERS RESPONSIBILITY: If not calling from within the process context for 980Sstevel@tonic-gate * the specified as, the caller must guarantee persistence of the specified as 990Sstevel@tonic-gate * for the duration of this function (eg. pages being locked within the as 1000Sstevel@tonic-gate * will guarantee persistence). 1010Sstevel@tonic-gate */ 1020Sstevel@tonic-gate int 1030Sstevel@tonic-gate as_add_callback(struct as *as, void (*cb_func)(), void *arg, uint_t events, 1040Sstevel@tonic-gate caddr_t vaddr, size_t size, int sleepflag) 1050Sstevel@tonic-gate { 1060Sstevel@tonic-gate struct as_callback *current_head, *cb; 1070Sstevel@tonic-gate caddr_t saddr; 1080Sstevel@tonic-gate size_t rsize; 1090Sstevel@tonic-gate 1100Sstevel@tonic-gate /* callback function and an event are mandatory */ 1110Sstevel@tonic-gate if ((cb_func == NULL) || ((events & AS_ALL_EVENT) == 0)) 1120Sstevel@tonic-gate return (EINVAL); 1130Sstevel@tonic-gate 1140Sstevel@tonic-gate /* Adding a callback after as_free has been called is not allowed */ 1150Sstevel@tonic-gate if (as == &kas) 1160Sstevel@tonic-gate return (ENOMEM); 1170Sstevel@tonic-gate 1180Sstevel@tonic-gate /* 1190Sstevel@tonic-gate * vaddr = 0 and size = -1 is used to indicate that the callback range 1200Sstevel@tonic-gate * is the entire address space so no rounding is done in that case. 1210Sstevel@tonic-gate */ 1220Sstevel@tonic-gate if (size != -1) { 1230Sstevel@tonic-gate saddr = (caddr_t)((uintptr_t)vaddr & (uintptr_t)PAGEMASK); 1240Sstevel@tonic-gate rsize = (((size_t)(vaddr + size) + PAGEOFFSET) & PAGEMASK) - 1250Sstevel@tonic-gate (size_t)saddr; 1260Sstevel@tonic-gate /* check for wraparound */ 1270Sstevel@tonic-gate if (saddr + rsize < saddr) 1280Sstevel@tonic-gate return (ENOMEM); 1290Sstevel@tonic-gate } else { 1300Sstevel@tonic-gate if (vaddr != 0) 1310Sstevel@tonic-gate return (EINVAL); 1320Sstevel@tonic-gate saddr = vaddr; 1330Sstevel@tonic-gate rsize = size; 1340Sstevel@tonic-gate } 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate /* Allocate and initialize a callback entry */ 1370Sstevel@tonic-gate cb = kmem_zalloc(sizeof (struct as_callback), sleepflag); 1380Sstevel@tonic-gate if (cb == NULL) 1390Sstevel@tonic-gate return (EAGAIN); 1400Sstevel@tonic-gate 1410Sstevel@tonic-gate cb->ascb_func = cb_func; 1420Sstevel@tonic-gate cb->ascb_arg = arg; 1430Sstevel@tonic-gate cb->ascb_events = events; 1440Sstevel@tonic-gate cb->ascb_saddr = saddr; 1450Sstevel@tonic-gate cb->ascb_len = rsize; 1460Sstevel@tonic-gate 1470Sstevel@tonic-gate /* Add the entry to the list */ 1480Sstevel@tonic-gate mutex_enter(&as->a_contents); 1490Sstevel@tonic-gate current_head = as->a_callbacks; 1500Sstevel@tonic-gate as->a_callbacks = cb; 1510Sstevel@tonic-gate cb->ascb_next = current_head; 1520Sstevel@tonic-gate 1530Sstevel@tonic-gate /* 1540Sstevel@tonic-gate * The call to this function may lose in a race with 1550Sstevel@tonic-gate * a pertinent event - eg. a thread does long term memory locking 1560Sstevel@tonic-gate * but before the callback is added another thread executes as_unmap. 1570Sstevel@tonic-gate * A broadcast here resolves that. 1580Sstevel@tonic-gate */ 1590Sstevel@tonic-gate if ((cb->ascb_events & AS_UNMAPWAIT_EVENT) && AS_ISUNMAPWAIT(as)) { 1600Sstevel@tonic-gate AS_CLRUNMAPWAIT(as); 1610Sstevel@tonic-gate cv_broadcast(&as->a_cv); 1620Sstevel@tonic-gate } 1630Sstevel@tonic-gate 1640Sstevel@tonic-gate mutex_exit(&as->a_contents); 1650Sstevel@tonic-gate return (0); 1660Sstevel@tonic-gate } 1670Sstevel@tonic-gate 1680Sstevel@tonic-gate /* 1690Sstevel@tonic-gate * Search the callback list for an entry which pertains to arg. 1700Sstevel@tonic-gate * 1710Sstevel@tonic-gate * This is called from within the client upon completion of the callback. 1720Sstevel@tonic-gate * RETURN VALUES: 1730Sstevel@tonic-gate * AS_CALLBACK_DELETED (callback entry found and deleted) 1740Sstevel@tonic-gate * AS_CALLBACK_NOTFOUND (no callback entry found - this is ok) 1750Sstevel@tonic-gate * AS_CALLBACK_DELETE_DEFERRED (callback is in process, delete of this 1760Sstevel@tonic-gate * entry will be made in as_do_callbacks) 1770Sstevel@tonic-gate * 1780Sstevel@tonic-gate * If as_delete_callback encounters a matching entry with AS_CALLBACK_CALLED 1790Sstevel@tonic-gate * set, it indicates that as_do_callbacks is processing this entry. The 1800Sstevel@tonic-gate * AS_ALL_EVENT events are cleared in the entry, and a broadcast is made 1810Sstevel@tonic-gate * to unblock as_do_callbacks, in case it is blocked. 1820Sstevel@tonic-gate * 1830Sstevel@tonic-gate * CALLERS RESPONSIBILITY: If not calling from within the process context for 1840Sstevel@tonic-gate * the specified as, the caller must guarantee persistence of the specified as 1850Sstevel@tonic-gate * for the duration of this function (eg. pages being locked within the as 1860Sstevel@tonic-gate * will guarantee persistence). 1870Sstevel@tonic-gate */ 1880Sstevel@tonic-gate uint_t 1890Sstevel@tonic-gate as_delete_callback(struct as *as, void *arg) 1900Sstevel@tonic-gate { 1910Sstevel@tonic-gate struct as_callback **prevcb = &as->a_callbacks; 1920Sstevel@tonic-gate struct as_callback *cb; 1930Sstevel@tonic-gate uint_t rc = AS_CALLBACK_NOTFOUND; 1940Sstevel@tonic-gate 1950Sstevel@tonic-gate mutex_enter(&as->a_contents); 1960Sstevel@tonic-gate for (cb = as->a_callbacks; cb; prevcb = &cb->ascb_next, cb = *prevcb) { 1970Sstevel@tonic-gate if (cb->ascb_arg != arg) 1980Sstevel@tonic-gate continue; 1990Sstevel@tonic-gate 2000Sstevel@tonic-gate /* 2010Sstevel@tonic-gate * If the events indicate AS_CALLBACK_CALLED, just clear 2020Sstevel@tonic-gate * AS_ALL_EVENT in the events field and wakeup the thread 2030Sstevel@tonic-gate * that may be waiting in as_do_callbacks. as_do_callbacks 2040Sstevel@tonic-gate * will take care of removing this entry from the list. In 2050Sstevel@tonic-gate * that case, return AS_CALLBACK_DELETE_DEFERRED. Otherwise 2060Sstevel@tonic-gate * (AS_CALLBACK_CALLED not set), just remove it from the 2070Sstevel@tonic-gate * list, return the memory and return AS_CALLBACK_DELETED. 2080Sstevel@tonic-gate */ 2090Sstevel@tonic-gate if ((cb->ascb_events & AS_CALLBACK_CALLED) != 0) { 2100Sstevel@tonic-gate /* leave AS_CALLBACK_CALLED */ 2110Sstevel@tonic-gate cb->ascb_events &= ~AS_ALL_EVENT; 2120Sstevel@tonic-gate rc = AS_CALLBACK_DELETE_DEFERRED; 2130Sstevel@tonic-gate cv_broadcast(&as->a_cv); 2140Sstevel@tonic-gate } else { 2150Sstevel@tonic-gate *prevcb = cb->ascb_next; 2160Sstevel@tonic-gate kmem_free(cb, sizeof (struct as_callback)); 2170Sstevel@tonic-gate rc = AS_CALLBACK_DELETED; 2180Sstevel@tonic-gate } 2190Sstevel@tonic-gate break; 2200Sstevel@tonic-gate } 2210Sstevel@tonic-gate mutex_exit(&as->a_contents); 2220Sstevel@tonic-gate return (rc); 2230Sstevel@tonic-gate } 2240Sstevel@tonic-gate 2250Sstevel@tonic-gate /* 2260Sstevel@tonic-gate * Searches the as callback list for a matching entry. 2270Sstevel@tonic-gate * Returns a pointer to the first matching callback, or NULL if 2280Sstevel@tonic-gate * nothing is found. 2290Sstevel@tonic-gate * This function never sleeps so it is ok to call it with more 2300Sstevel@tonic-gate * locks held but the (required) a_contents mutex. 2310Sstevel@tonic-gate * 2320Sstevel@tonic-gate * See also comment on as_do_callbacks below. 2330Sstevel@tonic-gate */ 2340Sstevel@tonic-gate static struct as_callback * 2350Sstevel@tonic-gate as_find_callback(struct as *as, uint_t events, caddr_t event_addr, 2360Sstevel@tonic-gate size_t event_len) 2370Sstevel@tonic-gate { 2380Sstevel@tonic-gate struct as_callback *cb; 2390Sstevel@tonic-gate 2400Sstevel@tonic-gate ASSERT(MUTEX_HELD(&as->a_contents)); 2410Sstevel@tonic-gate for (cb = as->a_callbacks; cb != NULL; cb = cb->ascb_next) { 2420Sstevel@tonic-gate /* 2430Sstevel@tonic-gate * If the callback has not already been called, then 2440Sstevel@tonic-gate * check if events or address range pertains. An event_len 2450Sstevel@tonic-gate * of zero means do an unconditional callback. 2460Sstevel@tonic-gate */ 2470Sstevel@tonic-gate if (((cb->ascb_events & AS_CALLBACK_CALLED) != 0) || 2480Sstevel@tonic-gate ((event_len != 0) && (((cb->ascb_events & events) == 0) || 2490Sstevel@tonic-gate (event_addr + event_len < cb->ascb_saddr) || 2500Sstevel@tonic-gate (event_addr > (cb->ascb_saddr + cb->ascb_len))))) { 2510Sstevel@tonic-gate continue; 2520Sstevel@tonic-gate } 2530Sstevel@tonic-gate break; 2540Sstevel@tonic-gate } 2550Sstevel@tonic-gate return (cb); 2560Sstevel@tonic-gate } 2570Sstevel@tonic-gate 2580Sstevel@tonic-gate /* 2590Sstevel@tonic-gate * Executes a given callback and removes it from the callback list for 2600Sstevel@tonic-gate * this address space. 2610Sstevel@tonic-gate * This function may sleep so the caller must drop all locks except 2620Sstevel@tonic-gate * a_contents before calling this func. 2630Sstevel@tonic-gate * 2640Sstevel@tonic-gate * See also comments on as_do_callbacks below. 2650Sstevel@tonic-gate */ 2660Sstevel@tonic-gate static void 2670Sstevel@tonic-gate as_execute_callback(struct as *as, struct as_callback *cb, 2680Sstevel@tonic-gate uint_t events) 2690Sstevel@tonic-gate { 2700Sstevel@tonic-gate struct as_callback **prevcb; 2710Sstevel@tonic-gate void *cb_arg; 2720Sstevel@tonic-gate 2730Sstevel@tonic-gate ASSERT(MUTEX_HELD(&as->a_contents) && (cb->ascb_events & events)); 2740Sstevel@tonic-gate cb->ascb_events |= AS_CALLBACK_CALLED; 2750Sstevel@tonic-gate mutex_exit(&as->a_contents); 2760Sstevel@tonic-gate (*cb->ascb_func)(as, cb->ascb_arg, events); 2770Sstevel@tonic-gate mutex_enter(&as->a_contents); 2780Sstevel@tonic-gate /* 2790Sstevel@tonic-gate * the callback function is required to delete the callback 2800Sstevel@tonic-gate * when the callback function determines it is OK for 2810Sstevel@tonic-gate * this thread to continue. as_delete_callback will clear 2820Sstevel@tonic-gate * the AS_ALL_EVENT in the events field when it is deleted. 2830Sstevel@tonic-gate * If the callback function called as_delete_callback, 2840Sstevel@tonic-gate * events will already be cleared and there will be no blocking. 2850Sstevel@tonic-gate */ 2860Sstevel@tonic-gate while ((cb->ascb_events & events) != 0) { 2870Sstevel@tonic-gate cv_wait(&as->a_cv, &as->a_contents); 2880Sstevel@tonic-gate } 2890Sstevel@tonic-gate /* 2900Sstevel@tonic-gate * This entry needs to be taken off the list. Normally, the 2910Sstevel@tonic-gate * callback func itself does that, but unfortunately the list 2920Sstevel@tonic-gate * may have changed while the callback was running because the 2930Sstevel@tonic-gate * a_contents mutex was dropped and someone else other than the 2940Sstevel@tonic-gate * callback func itself could have called as_delete_callback, 2950Sstevel@tonic-gate * so we have to search to find this entry again. The entry 2960Sstevel@tonic-gate * must have AS_CALLBACK_CALLED, and have the same 'arg'. 2970Sstevel@tonic-gate */ 2980Sstevel@tonic-gate cb_arg = cb->ascb_arg; 2990Sstevel@tonic-gate prevcb = &as->a_callbacks; 3000Sstevel@tonic-gate for (cb = as->a_callbacks; cb != NULL; 3010Sstevel@tonic-gate prevcb = &cb->ascb_next, cb = *prevcb) { 3020Sstevel@tonic-gate if (((cb->ascb_events & AS_CALLBACK_CALLED) == 0) || 3030Sstevel@tonic-gate (cb_arg != cb->ascb_arg)) { 3040Sstevel@tonic-gate continue; 3050Sstevel@tonic-gate } 3060Sstevel@tonic-gate *prevcb = cb->ascb_next; 3070Sstevel@tonic-gate kmem_free(cb, sizeof (struct as_callback)); 3080Sstevel@tonic-gate break; 3090Sstevel@tonic-gate } 3100Sstevel@tonic-gate } 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate /* 3130Sstevel@tonic-gate * Check the callback list for a matching event and intersection of 3140Sstevel@tonic-gate * address range. If there is a match invoke the callback. Skip an entry if: 3150Sstevel@tonic-gate * - a callback is already in progress for this entry (AS_CALLBACK_CALLED) 3160Sstevel@tonic-gate * - not event of interest 3170Sstevel@tonic-gate * - not address range of interest 3180Sstevel@tonic-gate * 3190Sstevel@tonic-gate * An event_len of zero indicates a request for an unconditional callback 3200Sstevel@tonic-gate * (regardless of event), only the AS_CALLBACK_CALLED is checked. The 3210Sstevel@tonic-gate * a_contents lock must be dropped before a callback, so only one callback 3220Sstevel@tonic-gate * can be done before returning. Return -1 (true) if a callback was 3230Sstevel@tonic-gate * executed and removed from the list, else return 0 (false). 3240Sstevel@tonic-gate * 3250Sstevel@tonic-gate * The logically separate parts, i.e. finding a matching callback and 3260Sstevel@tonic-gate * executing a given callback have been separated into two functions 3270Sstevel@tonic-gate * so that they can be called with different sets of locks held beyond 3280Sstevel@tonic-gate * the always-required a_contents. as_find_callback does not sleep so 3290Sstevel@tonic-gate * it is ok to call it if more locks than a_contents (i.e. the a_lock 3300Sstevel@tonic-gate * rwlock) are held. as_execute_callback on the other hand may sleep 3310Sstevel@tonic-gate * so all locks beyond a_contents must be dropped by the caller if one 3320Sstevel@tonic-gate * does not want to end comatose. 3330Sstevel@tonic-gate */ 3340Sstevel@tonic-gate static int 3350Sstevel@tonic-gate as_do_callbacks(struct as *as, uint_t events, caddr_t event_addr, 3360Sstevel@tonic-gate size_t event_len) 3370Sstevel@tonic-gate { 3380Sstevel@tonic-gate struct as_callback *cb; 3390Sstevel@tonic-gate 3400Sstevel@tonic-gate if ((cb = as_find_callback(as, events, event_addr, event_len))) { 3410Sstevel@tonic-gate as_execute_callback(as, cb, events); 3420Sstevel@tonic-gate return (-1); 3430Sstevel@tonic-gate } 3440Sstevel@tonic-gate return (0); 3450Sstevel@tonic-gate } 3460Sstevel@tonic-gate 3470Sstevel@tonic-gate /* 3480Sstevel@tonic-gate * Search for the segment containing addr. If a segment containing addr 3490Sstevel@tonic-gate * exists, that segment is returned. If no such segment exists, and 3500Sstevel@tonic-gate * the list spans addresses greater than addr, then the first segment 3510Sstevel@tonic-gate * whose base is greater than addr is returned; otherwise, NULL is 3520Sstevel@tonic-gate * returned unless tail is true, in which case the last element of the 3530Sstevel@tonic-gate * list is returned. 3540Sstevel@tonic-gate * 3550Sstevel@tonic-gate * a_seglast is used to cache the last found segment for repeated 3560Sstevel@tonic-gate * searches to the same addr (which happens frequently). 3570Sstevel@tonic-gate */ 3580Sstevel@tonic-gate struct seg * 3590Sstevel@tonic-gate as_findseg(struct as *as, caddr_t addr, int tail) 3600Sstevel@tonic-gate { 3610Sstevel@tonic-gate struct seg *seg = as->a_seglast; 3620Sstevel@tonic-gate avl_index_t where; 3630Sstevel@tonic-gate 3640Sstevel@tonic-gate ASSERT(AS_LOCK_HELD(as, &as->a_lock)); 3650Sstevel@tonic-gate 3660Sstevel@tonic-gate if (seg != NULL && 3670Sstevel@tonic-gate seg->s_base <= addr && 3680Sstevel@tonic-gate addr < seg->s_base + seg->s_size) 3690Sstevel@tonic-gate return (seg); 3700Sstevel@tonic-gate 3710Sstevel@tonic-gate seg = avl_find(&as->a_segtree, &addr, &where); 3720Sstevel@tonic-gate if (seg != NULL) 3730Sstevel@tonic-gate return (as->a_seglast = seg); 3740Sstevel@tonic-gate 3750Sstevel@tonic-gate seg = avl_nearest(&as->a_segtree, where, AVL_AFTER); 3760Sstevel@tonic-gate if (seg == NULL && tail) 3770Sstevel@tonic-gate seg = avl_last(&as->a_segtree); 3780Sstevel@tonic-gate return (as->a_seglast = seg); 3790Sstevel@tonic-gate } 3800Sstevel@tonic-gate 3810Sstevel@tonic-gate #ifdef VERIFY_SEGLIST 3820Sstevel@tonic-gate /* 3830Sstevel@tonic-gate * verify that the linked list is coherent 3840Sstevel@tonic-gate */ 3850Sstevel@tonic-gate static void 3860Sstevel@tonic-gate as_verify(struct as *as) 3870Sstevel@tonic-gate { 3880Sstevel@tonic-gate struct seg *seg, *seglast, *p, *n; 3890Sstevel@tonic-gate uint_t nsegs = 0; 3900Sstevel@tonic-gate 3910Sstevel@tonic-gate if (do_as_verify == 0) 3920Sstevel@tonic-gate return; 3930Sstevel@tonic-gate 3940Sstevel@tonic-gate seglast = as->a_seglast; 3950Sstevel@tonic-gate 3960Sstevel@tonic-gate for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) { 3970Sstevel@tonic-gate ASSERT(seg->s_as == as); 3980Sstevel@tonic-gate p = AS_SEGPREV(as, seg); 3990Sstevel@tonic-gate n = AS_SEGNEXT(as, seg); 4000Sstevel@tonic-gate ASSERT(p == NULL || p->s_as == as); 4010Sstevel@tonic-gate ASSERT(p == NULL || p->s_base < seg->s_base); 4020Sstevel@tonic-gate ASSERT(n == NULL || n->s_base > seg->s_base); 4030Sstevel@tonic-gate ASSERT(n != NULL || seg == avl_last(&as->a_segtree)); 4040Sstevel@tonic-gate if (seg == seglast) 4050Sstevel@tonic-gate seglast = NULL; 4060Sstevel@tonic-gate nsegs++; 4070Sstevel@tonic-gate } 4080Sstevel@tonic-gate ASSERT(seglast == NULL); 4090Sstevel@tonic-gate ASSERT(avl_numnodes(&as->a_segtree) == nsegs); 4100Sstevel@tonic-gate } 4110Sstevel@tonic-gate #endif /* VERIFY_SEGLIST */ 4120Sstevel@tonic-gate 4130Sstevel@tonic-gate /* 4140Sstevel@tonic-gate * Add a new segment to the address space. The avl_find() 4150Sstevel@tonic-gate * may be expensive so we attempt to use last segment accessed 4160Sstevel@tonic-gate * in as_gap() as an insertion point. 4170Sstevel@tonic-gate */ 4180Sstevel@tonic-gate int 4190Sstevel@tonic-gate as_addseg(struct as *as, struct seg *newseg) 4200Sstevel@tonic-gate { 4210Sstevel@tonic-gate struct seg *seg; 4220Sstevel@tonic-gate caddr_t addr; 4230Sstevel@tonic-gate caddr_t eaddr; 4240Sstevel@tonic-gate avl_index_t where; 4250Sstevel@tonic-gate 4260Sstevel@tonic-gate ASSERT(AS_WRITE_HELD(as, &as->a_lock)); 4270Sstevel@tonic-gate 4280Sstevel@tonic-gate as->a_updatedir = 1; /* inform /proc */ 4290Sstevel@tonic-gate gethrestime(&as->a_updatetime); 4300Sstevel@tonic-gate 4310Sstevel@tonic-gate if (as->a_lastgaphl != NULL) { 4320Sstevel@tonic-gate struct seg *hseg = NULL; 4330Sstevel@tonic-gate struct seg *lseg = NULL; 4340Sstevel@tonic-gate 4350Sstevel@tonic-gate if (as->a_lastgaphl->s_base > newseg->s_base) { 4360Sstevel@tonic-gate hseg = as->a_lastgaphl; 4370Sstevel@tonic-gate lseg = AVL_PREV(&as->a_segtree, hseg); 4380Sstevel@tonic-gate } else { 4390Sstevel@tonic-gate lseg = as->a_lastgaphl; 4400Sstevel@tonic-gate hseg = AVL_NEXT(&as->a_segtree, lseg); 4410Sstevel@tonic-gate } 4420Sstevel@tonic-gate 4430Sstevel@tonic-gate if (hseg && lseg && lseg->s_base < newseg->s_base && 4440Sstevel@tonic-gate hseg->s_base > newseg->s_base) { 4450Sstevel@tonic-gate avl_insert_here(&as->a_segtree, newseg, lseg, 4460Sstevel@tonic-gate AVL_AFTER); 4470Sstevel@tonic-gate as->a_lastgaphl = NULL; 4480Sstevel@tonic-gate as->a_seglast = newseg; 4490Sstevel@tonic-gate return (0); 4500Sstevel@tonic-gate } 4510Sstevel@tonic-gate as->a_lastgaphl = NULL; 4520Sstevel@tonic-gate } 4530Sstevel@tonic-gate 4540Sstevel@tonic-gate addr = newseg->s_base; 4550Sstevel@tonic-gate eaddr = addr + newseg->s_size; 4560Sstevel@tonic-gate again: 4570Sstevel@tonic-gate 4580Sstevel@tonic-gate seg = avl_find(&as->a_segtree, &addr, &where); 4590Sstevel@tonic-gate 4600Sstevel@tonic-gate if (seg == NULL) 4610Sstevel@tonic-gate seg = avl_nearest(&as->a_segtree, where, AVL_AFTER); 4620Sstevel@tonic-gate 4630Sstevel@tonic-gate if (seg == NULL) 4640Sstevel@tonic-gate seg = avl_last(&as->a_segtree); 4650Sstevel@tonic-gate 4660Sstevel@tonic-gate if (seg != NULL) { 4670Sstevel@tonic-gate caddr_t base = seg->s_base; 4680Sstevel@tonic-gate 4690Sstevel@tonic-gate /* 4700Sstevel@tonic-gate * If top of seg is below the requested address, then 4710Sstevel@tonic-gate * the insertion point is at the end of the linked list, 4720Sstevel@tonic-gate * and seg points to the tail of the list. Otherwise, 4730Sstevel@tonic-gate * the insertion point is immediately before seg. 4740Sstevel@tonic-gate */ 4750Sstevel@tonic-gate if (base + seg->s_size > addr) { 4760Sstevel@tonic-gate if (addr >= base || eaddr > base) { 4770Sstevel@tonic-gate #ifdef __sparc 4780Sstevel@tonic-gate extern struct seg_ops segnf_ops; 4790Sstevel@tonic-gate 4800Sstevel@tonic-gate /* 4810Sstevel@tonic-gate * no-fault segs must disappear if overlaid. 4820Sstevel@tonic-gate * XXX need new segment type so 4830Sstevel@tonic-gate * we don't have to check s_ops 4840Sstevel@tonic-gate */ 4850Sstevel@tonic-gate if (seg->s_ops == &segnf_ops) { 4860Sstevel@tonic-gate seg_unmap(seg); 4870Sstevel@tonic-gate goto again; 4880Sstevel@tonic-gate } 4890Sstevel@tonic-gate #endif 4900Sstevel@tonic-gate return (-1); /* overlapping segment */ 4910Sstevel@tonic-gate } 4920Sstevel@tonic-gate } 4930Sstevel@tonic-gate } 4940Sstevel@tonic-gate as->a_seglast = newseg; 4950Sstevel@tonic-gate avl_insert(&as->a_segtree, newseg, where); 4960Sstevel@tonic-gate 4970Sstevel@tonic-gate #ifdef VERIFY_SEGLIST 4980Sstevel@tonic-gate as_verify(as); 4990Sstevel@tonic-gate #endif 5000Sstevel@tonic-gate return (0); 5010Sstevel@tonic-gate } 5020Sstevel@tonic-gate 5030Sstevel@tonic-gate struct seg * 5040Sstevel@tonic-gate as_removeseg(struct as *as, struct seg *seg) 5050Sstevel@tonic-gate { 5060Sstevel@tonic-gate avl_tree_t *t; 5070Sstevel@tonic-gate 5080Sstevel@tonic-gate ASSERT(AS_WRITE_HELD(as, &as->a_lock)); 5090Sstevel@tonic-gate 5100Sstevel@tonic-gate as->a_updatedir = 1; /* inform /proc */ 5110Sstevel@tonic-gate gethrestime(&as->a_updatetime); 5120Sstevel@tonic-gate 5130Sstevel@tonic-gate if (seg == NULL) 5140Sstevel@tonic-gate return (NULL); 5150Sstevel@tonic-gate 5160Sstevel@tonic-gate t = &as->a_segtree; 5170Sstevel@tonic-gate if (as->a_seglast == seg) 5180Sstevel@tonic-gate as->a_seglast = NULL; 5190Sstevel@tonic-gate as->a_lastgaphl = NULL; 5200Sstevel@tonic-gate 5210Sstevel@tonic-gate /* 5220Sstevel@tonic-gate * if this segment is at an address higher than 5230Sstevel@tonic-gate * a_lastgap, set a_lastgap to the next segment (NULL if last segment) 5240Sstevel@tonic-gate */ 5250Sstevel@tonic-gate if (as->a_lastgap && 5260Sstevel@tonic-gate (seg == as->a_lastgap || seg->s_base > as->a_lastgap->s_base)) 5270Sstevel@tonic-gate as->a_lastgap = AVL_NEXT(t, seg); 5280Sstevel@tonic-gate 5290Sstevel@tonic-gate /* 5300Sstevel@tonic-gate * remove the segment from the seg tree 5310Sstevel@tonic-gate */ 5320Sstevel@tonic-gate avl_remove(t, seg); 5330Sstevel@tonic-gate 5340Sstevel@tonic-gate #ifdef VERIFY_SEGLIST 5350Sstevel@tonic-gate as_verify(as); 5360Sstevel@tonic-gate #endif 5370Sstevel@tonic-gate return (seg); 5380Sstevel@tonic-gate } 5390Sstevel@tonic-gate 5400Sstevel@tonic-gate /* 5410Sstevel@tonic-gate * Find a segment containing addr. 5420Sstevel@tonic-gate */ 5430Sstevel@tonic-gate struct seg * 5440Sstevel@tonic-gate as_segat(struct as *as, caddr_t addr) 5450Sstevel@tonic-gate { 5460Sstevel@tonic-gate struct seg *seg = as->a_seglast; 5470Sstevel@tonic-gate 5480Sstevel@tonic-gate ASSERT(AS_LOCK_HELD(as, &as->a_lock)); 5490Sstevel@tonic-gate 5500Sstevel@tonic-gate if (seg != NULL && seg->s_base <= addr && 5510Sstevel@tonic-gate addr < seg->s_base + seg->s_size) 5520Sstevel@tonic-gate return (seg); 5530Sstevel@tonic-gate 5540Sstevel@tonic-gate seg = avl_find(&as->a_segtree, &addr, NULL); 5550Sstevel@tonic-gate return (seg); 5560Sstevel@tonic-gate } 5570Sstevel@tonic-gate 5580Sstevel@tonic-gate /* 5590Sstevel@tonic-gate * Serialize all searches for holes in an address space to 5600Sstevel@tonic-gate * prevent two or more threads from allocating the same virtual 5610Sstevel@tonic-gate * address range. The address space must not be "read/write" 5620Sstevel@tonic-gate * locked by the caller since we may block. 5630Sstevel@tonic-gate */ 5640Sstevel@tonic-gate void 5650Sstevel@tonic-gate as_rangelock(struct as *as) 5660Sstevel@tonic-gate { 5670Sstevel@tonic-gate mutex_enter(&as->a_contents); 5680Sstevel@tonic-gate while (AS_ISCLAIMGAP(as)) 5690Sstevel@tonic-gate cv_wait(&as->a_cv, &as->a_contents); 5700Sstevel@tonic-gate AS_SETCLAIMGAP(as); 5710Sstevel@tonic-gate mutex_exit(&as->a_contents); 5720Sstevel@tonic-gate } 5730Sstevel@tonic-gate 5740Sstevel@tonic-gate /* 5750Sstevel@tonic-gate * Release hold on a_state & AS_CLAIMGAP and signal any other blocked threads. 5760Sstevel@tonic-gate */ 5770Sstevel@tonic-gate void 5780Sstevel@tonic-gate as_rangeunlock(struct as *as) 5790Sstevel@tonic-gate { 5800Sstevel@tonic-gate mutex_enter(&as->a_contents); 5810Sstevel@tonic-gate AS_CLRCLAIMGAP(as); 5820Sstevel@tonic-gate cv_signal(&as->a_cv); 5830Sstevel@tonic-gate mutex_exit(&as->a_contents); 5840Sstevel@tonic-gate } 5850Sstevel@tonic-gate 5860Sstevel@tonic-gate /* 5870Sstevel@tonic-gate * compar segments (or just an address) by segment address range 5880Sstevel@tonic-gate */ 5890Sstevel@tonic-gate static int 5900Sstevel@tonic-gate as_segcompar(const void *x, const void *y) 5910Sstevel@tonic-gate { 5920Sstevel@tonic-gate struct seg *a = (struct seg *)x; 5930Sstevel@tonic-gate struct seg *b = (struct seg *)y; 5940Sstevel@tonic-gate 5950Sstevel@tonic-gate if (a->s_base < b->s_base) 5960Sstevel@tonic-gate return (-1); 5970Sstevel@tonic-gate if (a->s_base >= b->s_base + b->s_size) 5980Sstevel@tonic-gate return (1); 5990Sstevel@tonic-gate return (0); 6000Sstevel@tonic-gate } 6010Sstevel@tonic-gate 6020Sstevel@tonic-gate 6030Sstevel@tonic-gate void 6040Sstevel@tonic-gate as_avlinit(struct as *as) 6050Sstevel@tonic-gate { 6060Sstevel@tonic-gate avl_create(&as->a_segtree, as_segcompar, sizeof (struct seg), 6070Sstevel@tonic-gate offsetof(struct seg, s_tree)); 6080Sstevel@tonic-gate avl_create(&as->a_wpage, wp_compare, sizeof (struct watched_page), 6090Sstevel@tonic-gate offsetof(struct watched_page, wp_link)); 6100Sstevel@tonic-gate } 6110Sstevel@tonic-gate 6120Sstevel@tonic-gate /*ARGSUSED*/ 6130Sstevel@tonic-gate static int 6140Sstevel@tonic-gate as_constructor(void *buf, void *cdrarg, int kmflags) 6150Sstevel@tonic-gate { 6160Sstevel@tonic-gate struct as *as = buf; 6170Sstevel@tonic-gate 6180Sstevel@tonic-gate mutex_init(&as->a_contents, NULL, MUTEX_DEFAULT, NULL); 6190Sstevel@tonic-gate cv_init(&as->a_cv, NULL, CV_DEFAULT, NULL); 6200Sstevel@tonic-gate rw_init(&as->a_lock, NULL, RW_DEFAULT, NULL); 6210Sstevel@tonic-gate as_avlinit(as); 6220Sstevel@tonic-gate return (0); 6230Sstevel@tonic-gate } 6240Sstevel@tonic-gate 6250Sstevel@tonic-gate /*ARGSUSED1*/ 6260Sstevel@tonic-gate static void 6270Sstevel@tonic-gate as_destructor(void *buf, void *cdrarg) 6280Sstevel@tonic-gate { 6290Sstevel@tonic-gate struct as *as = buf; 6300Sstevel@tonic-gate 6310Sstevel@tonic-gate avl_destroy(&as->a_segtree); 6320Sstevel@tonic-gate mutex_destroy(&as->a_contents); 6330Sstevel@tonic-gate cv_destroy(&as->a_cv); 6340Sstevel@tonic-gate rw_destroy(&as->a_lock); 6350Sstevel@tonic-gate } 6360Sstevel@tonic-gate 6370Sstevel@tonic-gate void 6380Sstevel@tonic-gate as_init(void) 6390Sstevel@tonic-gate { 6400Sstevel@tonic-gate as_cache = kmem_cache_create("as_cache", sizeof (struct as), 0, 6410Sstevel@tonic-gate as_constructor, as_destructor, NULL, NULL, NULL, 0); 6420Sstevel@tonic-gate } 6430Sstevel@tonic-gate 6440Sstevel@tonic-gate /* 6450Sstevel@tonic-gate * Allocate and initialize an address space data structure. 6460Sstevel@tonic-gate * We call hat_alloc to allow any machine dependent 6470Sstevel@tonic-gate * information in the hat structure to be initialized. 6480Sstevel@tonic-gate */ 6490Sstevel@tonic-gate struct as * 6500Sstevel@tonic-gate as_alloc(void) 6510Sstevel@tonic-gate { 6520Sstevel@tonic-gate struct as *as; 6530Sstevel@tonic-gate 6540Sstevel@tonic-gate as = kmem_cache_alloc(as_cache, KM_SLEEP); 6550Sstevel@tonic-gate 6560Sstevel@tonic-gate as->a_flags = 0; 6570Sstevel@tonic-gate as->a_vbits = 0; 6580Sstevel@tonic-gate as->a_hrm = NULL; 6590Sstevel@tonic-gate as->a_seglast = NULL; 6600Sstevel@tonic-gate as->a_size = 0; 6610Sstevel@tonic-gate as->a_updatedir = 0; 6620Sstevel@tonic-gate gethrestime(&as->a_updatetime); 6630Sstevel@tonic-gate as->a_objectdir = NULL; 6640Sstevel@tonic-gate as->a_sizedir = 0; 6650Sstevel@tonic-gate as->a_userlimit = (caddr_t)USERLIMIT; 6660Sstevel@tonic-gate as->a_lastgap = NULL; 6670Sstevel@tonic-gate as->a_lastgaphl = NULL; 6680Sstevel@tonic-gate as->a_callbacks = NULL; 6690Sstevel@tonic-gate 6700Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER); 6710Sstevel@tonic-gate as->a_hat = hat_alloc(as); /* create hat for default system mmu */ 6720Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 6730Sstevel@tonic-gate 6740Sstevel@tonic-gate as->a_xhat = NULL; 6750Sstevel@tonic-gate 6760Sstevel@tonic-gate return (as); 6770Sstevel@tonic-gate } 6780Sstevel@tonic-gate 6790Sstevel@tonic-gate /* 6800Sstevel@tonic-gate * Free an address space data structure. 6810Sstevel@tonic-gate * Need to free the hat first and then 6820Sstevel@tonic-gate * all the segments on this as and finally 6830Sstevel@tonic-gate * the space for the as struct itself. 6840Sstevel@tonic-gate */ 6850Sstevel@tonic-gate void 6860Sstevel@tonic-gate as_free(struct as *as) 6870Sstevel@tonic-gate { 6880Sstevel@tonic-gate struct hat *hat = as->a_hat; 6890Sstevel@tonic-gate struct seg *seg, *next; 6900Sstevel@tonic-gate int called = 0; 6910Sstevel@tonic-gate 6920Sstevel@tonic-gate top: 6930Sstevel@tonic-gate /* 6940Sstevel@tonic-gate * Invoke ALL callbacks. as_do_callbacks will do one callback 6950Sstevel@tonic-gate * per call, and not return (-1) until the callback has completed. 6960Sstevel@tonic-gate * When as_do_callbacks returns zero, all callbacks have completed. 6970Sstevel@tonic-gate */ 6980Sstevel@tonic-gate mutex_enter(&as->a_contents); 6990Sstevel@tonic-gate while (as->a_callbacks && as_do_callbacks(as, AS_ALL_EVENT, 0, 0)); 7000Sstevel@tonic-gate 7010Sstevel@tonic-gate /* This will prevent new XHATs from attaching to as */ 7020Sstevel@tonic-gate if (!called) 7030Sstevel@tonic-gate AS_SETBUSY(as); 7040Sstevel@tonic-gate mutex_exit(&as->a_contents); 7050Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER); 7060Sstevel@tonic-gate 7070Sstevel@tonic-gate if (!called) { 7080Sstevel@tonic-gate called = 1; 7090Sstevel@tonic-gate hat_free_start(hat); 7100Sstevel@tonic-gate if (as->a_xhat != NULL) 7110Sstevel@tonic-gate xhat_free_start_all(as); 7120Sstevel@tonic-gate } 7130Sstevel@tonic-gate for (seg = AS_SEGFIRST(as); seg != NULL; seg = next) { 7140Sstevel@tonic-gate int err; 7150Sstevel@tonic-gate 7160Sstevel@tonic-gate next = AS_SEGNEXT(as, seg); 7170Sstevel@tonic-gate err = SEGOP_UNMAP(seg, seg->s_base, seg->s_size); 7180Sstevel@tonic-gate if (err == EAGAIN) { 7190Sstevel@tonic-gate mutex_enter(&as->a_contents); 7200Sstevel@tonic-gate if (as->a_callbacks) { 7210Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 7220Sstevel@tonic-gate } else { 7230Sstevel@tonic-gate /* 7240Sstevel@tonic-gate * Memory is currently locked. Wait for a 7250Sstevel@tonic-gate * cv_signal that it has been unlocked, then 7260Sstevel@tonic-gate * try the operation again. 7270Sstevel@tonic-gate */ 7280Sstevel@tonic-gate if (AS_ISUNMAPWAIT(as) == 0) 7290Sstevel@tonic-gate cv_broadcast(&as->a_cv); 7300Sstevel@tonic-gate AS_SETUNMAPWAIT(as); 7310Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 7320Sstevel@tonic-gate while (AS_ISUNMAPWAIT(as)) 7330Sstevel@tonic-gate cv_wait(&as->a_cv, &as->a_contents); 7340Sstevel@tonic-gate } 7350Sstevel@tonic-gate mutex_exit(&as->a_contents); 7360Sstevel@tonic-gate goto top; 7370Sstevel@tonic-gate } else { 7380Sstevel@tonic-gate /* 7390Sstevel@tonic-gate * We do not expect any other error return at this 7400Sstevel@tonic-gate * time. This is similar to an ASSERT in seg_unmap() 7410Sstevel@tonic-gate */ 7420Sstevel@tonic-gate ASSERT(err == 0); 7430Sstevel@tonic-gate } 7440Sstevel@tonic-gate } 7450Sstevel@tonic-gate hat_free_end(hat); 7460Sstevel@tonic-gate if (as->a_xhat != NULL) 7470Sstevel@tonic-gate xhat_free_end_all(as); 7480Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 7490Sstevel@tonic-gate 7500Sstevel@tonic-gate /* /proc stuff */ 7510Sstevel@tonic-gate ASSERT(avl_numnodes(&as->a_wpage) == 0); 7520Sstevel@tonic-gate if (as->a_objectdir) { 7530Sstevel@tonic-gate kmem_free(as->a_objectdir, as->a_sizedir * sizeof (vnode_t *)); 7540Sstevel@tonic-gate as->a_objectdir = NULL; 7550Sstevel@tonic-gate as->a_sizedir = 0; 7560Sstevel@tonic-gate } 7570Sstevel@tonic-gate 7580Sstevel@tonic-gate /* 7590Sstevel@tonic-gate * Free the struct as back to kmem. Assert it has no segments. 7600Sstevel@tonic-gate */ 7610Sstevel@tonic-gate ASSERT(avl_numnodes(&as->a_segtree) == 0); 7620Sstevel@tonic-gate kmem_cache_free(as_cache, as); 7630Sstevel@tonic-gate } 7640Sstevel@tonic-gate 7650Sstevel@tonic-gate int 7660Sstevel@tonic-gate as_dup(struct as *as, struct as **outas) 7670Sstevel@tonic-gate { 7680Sstevel@tonic-gate struct as *newas; 7690Sstevel@tonic-gate struct seg *seg, *newseg; 7700Sstevel@tonic-gate int error; 7710Sstevel@tonic-gate 7720Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER); 7730Sstevel@tonic-gate as_clearwatch(as); 7740Sstevel@tonic-gate newas = as_alloc(); 7750Sstevel@tonic-gate newas->a_userlimit = as->a_userlimit; 7760Sstevel@tonic-gate AS_LOCK_ENTER(newas, &newas->a_lock, RW_WRITER); 7770Sstevel@tonic-gate 7780Sstevel@tonic-gate /* This will prevent new XHATs from attaching */ 7790Sstevel@tonic-gate mutex_enter(&as->a_contents); 7800Sstevel@tonic-gate AS_SETBUSY(as); 7810Sstevel@tonic-gate mutex_exit(&as->a_contents); 7820Sstevel@tonic-gate mutex_enter(&newas->a_contents); 7830Sstevel@tonic-gate AS_SETBUSY(newas); 7840Sstevel@tonic-gate mutex_exit(&newas->a_contents); 7850Sstevel@tonic-gate 7860Sstevel@tonic-gate 7870Sstevel@tonic-gate for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) { 7880Sstevel@tonic-gate 7890Sstevel@tonic-gate if (seg->s_flags & S_PURGE) 7900Sstevel@tonic-gate continue; 7910Sstevel@tonic-gate 7920Sstevel@tonic-gate newseg = seg_alloc(newas, seg->s_base, seg->s_size); 7930Sstevel@tonic-gate if (newseg == NULL) { 7940Sstevel@tonic-gate AS_LOCK_EXIT(newas, &newas->a_lock); 7950Sstevel@tonic-gate as_setwatch(as); 7960Sstevel@tonic-gate mutex_enter(&as->a_contents); 7970Sstevel@tonic-gate AS_CLRBUSY(as); 7980Sstevel@tonic-gate mutex_exit(&as->a_contents); 7990Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 8000Sstevel@tonic-gate as_free(newas); 8010Sstevel@tonic-gate return (-1); 8020Sstevel@tonic-gate } 8030Sstevel@tonic-gate if ((error = SEGOP_DUP(seg, newseg)) != 0) { 8040Sstevel@tonic-gate /* 8050Sstevel@tonic-gate * We call seg_free() on the new seg 8060Sstevel@tonic-gate * because the segment is not set up 8070Sstevel@tonic-gate * completely; i.e. it has no ops. 8080Sstevel@tonic-gate */ 8090Sstevel@tonic-gate as_setwatch(as); 8100Sstevel@tonic-gate mutex_enter(&as->a_contents); 8110Sstevel@tonic-gate AS_CLRBUSY(as); 8120Sstevel@tonic-gate mutex_exit(&as->a_contents); 8130Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 8140Sstevel@tonic-gate seg_free(newseg); 8150Sstevel@tonic-gate AS_LOCK_EXIT(newas, &newas->a_lock); 8160Sstevel@tonic-gate as_free(newas); 8170Sstevel@tonic-gate return (error); 8180Sstevel@tonic-gate } 8190Sstevel@tonic-gate newas->a_size += seg->s_size; 8200Sstevel@tonic-gate } 8210Sstevel@tonic-gate 8220Sstevel@tonic-gate error = hat_dup(as->a_hat, newas->a_hat, NULL, 0, HAT_DUP_ALL); 8230Sstevel@tonic-gate if (as->a_xhat != NULL) 8240Sstevel@tonic-gate error |= xhat_dup_all(as, newas, NULL, 0, HAT_DUP_ALL); 8250Sstevel@tonic-gate 8260Sstevel@tonic-gate mutex_enter(&newas->a_contents); 8270Sstevel@tonic-gate AS_CLRBUSY(newas); 8280Sstevel@tonic-gate mutex_exit(&newas->a_contents); 8290Sstevel@tonic-gate AS_LOCK_EXIT(newas, &newas->a_lock); 8300Sstevel@tonic-gate 8310Sstevel@tonic-gate as_setwatch(as); 8320Sstevel@tonic-gate mutex_enter(&as->a_contents); 8330Sstevel@tonic-gate AS_CLRBUSY(as); 8340Sstevel@tonic-gate mutex_exit(&as->a_contents); 8350Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 8360Sstevel@tonic-gate if (error != 0) { 8370Sstevel@tonic-gate as_free(newas); 8380Sstevel@tonic-gate return (error); 8390Sstevel@tonic-gate } 8400Sstevel@tonic-gate *outas = newas; 8410Sstevel@tonic-gate return (0); 8420Sstevel@tonic-gate } 8430Sstevel@tonic-gate 8440Sstevel@tonic-gate /* 8450Sstevel@tonic-gate * Handle a ``fault'' at addr for size bytes. 8460Sstevel@tonic-gate */ 8470Sstevel@tonic-gate faultcode_t 8480Sstevel@tonic-gate as_fault(struct hat *hat, struct as *as, caddr_t addr, size_t size, 8490Sstevel@tonic-gate enum fault_type type, enum seg_rw rw) 8500Sstevel@tonic-gate { 8510Sstevel@tonic-gate struct seg *seg; 8520Sstevel@tonic-gate caddr_t raddr; /* rounded down addr */ 8530Sstevel@tonic-gate size_t rsize; /* rounded up size */ 8540Sstevel@tonic-gate size_t ssize; 8550Sstevel@tonic-gate faultcode_t res = 0; 8560Sstevel@tonic-gate caddr_t addrsav; 8570Sstevel@tonic-gate struct seg *segsav; 8580Sstevel@tonic-gate int as_lock_held; 8590Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread); 8600Sstevel@tonic-gate int is_xhat = 0; 8610Sstevel@tonic-gate int holding_wpage = 0; 8620Sstevel@tonic-gate extern struct seg_ops segdev_ops; 8630Sstevel@tonic-gate 8640Sstevel@tonic-gate 8650Sstevel@tonic-gate 8660Sstevel@tonic-gate if (as->a_hat != hat) { 8670Sstevel@tonic-gate /* This must be an XHAT then */ 8680Sstevel@tonic-gate is_xhat = 1; 8690Sstevel@tonic-gate 8700Sstevel@tonic-gate if ((type != F_INVAL) || (as == &kas)) 8710Sstevel@tonic-gate return (FC_NOSUPPORT); 8720Sstevel@tonic-gate } 8730Sstevel@tonic-gate 8740Sstevel@tonic-gate retry: 8750Sstevel@tonic-gate if (!is_xhat) { 8760Sstevel@tonic-gate /* 8770Sstevel@tonic-gate * Indicate that the lwp is not to be stopped while waiting 8780Sstevel@tonic-gate * for a pagefault. This is to avoid deadlock while debugging 8790Sstevel@tonic-gate * a process via /proc over NFS (in particular). 8800Sstevel@tonic-gate */ 881209Scwb if (lwp != NULL) { 8820Sstevel@tonic-gate lwp->lwp_nostop++; 883209Scwb lwp->lwp_nostop_r++; 884209Scwb } 8850Sstevel@tonic-gate 8860Sstevel@tonic-gate /* 8870Sstevel@tonic-gate * same length must be used when we softlock and softunlock. 8880Sstevel@tonic-gate * We don't support softunlocking lengths less than 8890Sstevel@tonic-gate * the original length when there is largepage support. 8900Sstevel@tonic-gate * See seg_dev.c for more comments. 8910Sstevel@tonic-gate */ 8920Sstevel@tonic-gate switch (type) { 8930Sstevel@tonic-gate 8940Sstevel@tonic-gate case F_SOFTLOCK: 8950Sstevel@tonic-gate CPU_STATS_ADD_K(vm, softlock, 1); 8960Sstevel@tonic-gate break; 8970Sstevel@tonic-gate 8980Sstevel@tonic-gate case F_SOFTUNLOCK: 8990Sstevel@tonic-gate break; 9000Sstevel@tonic-gate 9010Sstevel@tonic-gate case F_PROT: 9020Sstevel@tonic-gate CPU_STATS_ADD_K(vm, prot_fault, 1); 9030Sstevel@tonic-gate break; 9040Sstevel@tonic-gate 9050Sstevel@tonic-gate case F_INVAL: 9060Sstevel@tonic-gate CPU_STATS_ENTER_K(); 9070Sstevel@tonic-gate CPU_STATS_ADDQ(CPU, vm, as_fault, 1); 9080Sstevel@tonic-gate if (as == &kas) 9090Sstevel@tonic-gate CPU_STATS_ADDQ(CPU, vm, kernel_asflt, 1); 9100Sstevel@tonic-gate CPU_STATS_EXIT_K(); 9110Sstevel@tonic-gate break; 9120Sstevel@tonic-gate } 9130Sstevel@tonic-gate } 9140Sstevel@tonic-gate 9150Sstevel@tonic-gate /* Kernel probe */ 9160Sstevel@tonic-gate TNF_PROBE_3(address_fault, "vm pagefault", /* CSTYLED */, 9170Sstevel@tonic-gate tnf_opaque, address, addr, 9180Sstevel@tonic-gate tnf_fault_type, fault_type, type, 9190Sstevel@tonic-gate tnf_seg_access, access, rw); 9200Sstevel@tonic-gate 9210Sstevel@tonic-gate raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK); 9220Sstevel@tonic-gate rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) - 9230Sstevel@tonic-gate (size_t)raddr; 9240Sstevel@tonic-gate 9250Sstevel@tonic-gate /* 9260Sstevel@tonic-gate * XXX -- Don't grab the as lock for segkmap. We should grab it for 9270Sstevel@tonic-gate * correctness, but then we could be stuck holding this lock for 9280Sstevel@tonic-gate * a LONG time if the fault needs to be resolved on a slow 9290Sstevel@tonic-gate * filesystem, and then no-one will be able to exec new commands, 9300Sstevel@tonic-gate * as exec'ing requires the write lock on the as. 9310Sstevel@tonic-gate */ 9320Sstevel@tonic-gate if (as == &kas && segkmap && segkmap->s_base <= raddr && 9330Sstevel@tonic-gate raddr + size < segkmap->s_base + segkmap->s_size) { 9340Sstevel@tonic-gate /* 9350Sstevel@tonic-gate * if (as==&kas), this can't be XHAT: we've already returned 9360Sstevel@tonic-gate * FC_NOSUPPORT. 9370Sstevel@tonic-gate */ 9380Sstevel@tonic-gate seg = segkmap; 9390Sstevel@tonic-gate as_lock_held = 0; 9400Sstevel@tonic-gate } else { 9410Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 9420Sstevel@tonic-gate if (is_xhat && avl_numnodes(&as->a_wpage) != 0) { 9430Sstevel@tonic-gate /* 9440Sstevel@tonic-gate * Grab and hold the writers' lock on the as 9450Sstevel@tonic-gate * if the fault is to a watched page. 9460Sstevel@tonic-gate * This will keep CPUs from "peeking" at the 9470Sstevel@tonic-gate * address range while we're temporarily boosting 9480Sstevel@tonic-gate * the permissions for the XHAT device to 9490Sstevel@tonic-gate * resolve the fault in the segment layer. 9500Sstevel@tonic-gate * 9510Sstevel@tonic-gate * We could check whether faulted address 9520Sstevel@tonic-gate * is within a watched page and only then grab 9530Sstevel@tonic-gate * the writer lock, but this is simpler. 9540Sstevel@tonic-gate */ 9550Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 9560Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER); 9570Sstevel@tonic-gate } 9580Sstevel@tonic-gate 9590Sstevel@tonic-gate seg = as_segat(as, raddr); 9600Sstevel@tonic-gate if (seg == NULL) { 9610Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 962209Scwb if ((lwp != NULL) && (!is_xhat)) { 9630Sstevel@tonic-gate lwp->lwp_nostop--; 964209Scwb lwp->lwp_nostop_r--; 965209Scwb } 9660Sstevel@tonic-gate return (FC_NOMAP); 9670Sstevel@tonic-gate } 9680Sstevel@tonic-gate 9690Sstevel@tonic-gate as_lock_held = 1; 9700Sstevel@tonic-gate } 9710Sstevel@tonic-gate 9720Sstevel@tonic-gate addrsav = raddr; 9730Sstevel@tonic-gate segsav = seg; 9740Sstevel@tonic-gate 9750Sstevel@tonic-gate for (; rsize != 0; rsize -= ssize, raddr += ssize) { 9760Sstevel@tonic-gate if (raddr >= seg->s_base + seg->s_size) { 9770Sstevel@tonic-gate seg = AS_SEGNEXT(as, seg); 9780Sstevel@tonic-gate if (seg == NULL || raddr != seg->s_base) { 9790Sstevel@tonic-gate res = FC_NOMAP; 9800Sstevel@tonic-gate break; 9810Sstevel@tonic-gate } 9820Sstevel@tonic-gate } 9830Sstevel@tonic-gate if (raddr + rsize > seg->s_base + seg->s_size) 9840Sstevel@tonic-gate ssize = seg->s_base + seg->s_size - raddr; 9850Sstevel@tonic-gate else 9860Sstevel@tonic-gate ssize = rsize; 9870Sstevel@tonic-gate 9880Sstevel@tonic-gate if (!is_xhat || (seg->s_ops != &segdev_ops)) { 9890Sstevel@tonic-gate 9900Sstevel@tonic-gate if (is_xhat && avl_numnodes(&as->a_wpage) != 0 && 9910Sstevel@tonic-gate pr_is_watchpage_as(raddr, rw, as)) { 9920Sstevel@tonic-gate /* 9930Sstevel@tonic-gate * Handle watch pages. If we're faulting on a 9940Sstevel@tonic-gate * watched page from an X-hat, we have to 9950Sstevel@tonic-gate * restore the original permissions while we 9960Sstevel@tonic-gate * handle the fault. 9970Sstevel@tonic-gate */ 9980Sstevel@tonic-gate as_clearwatch(as); 9990Sstevel@tonic-gate holding_wpage = 1; 10000Sstevel@tonic-gate } 10010Sstevel@tonic-gate 10020Sstevel@tonic-gate res = SEGOP_FAULT(hat, seg, raddr, ssize, type, rw); 10030Sstevel@tonic-gate 10040Sstevel@tonic-gate /* Restore watchpoints */ 10050Sstevel@tonic-gate if (holding_wpage) { 10060Sstevel@tonic-gate as_setwatch(as); 10070Sstevel@tonic-gate holding_wpage = 0; 10080Sstevel@tonic-gate } 10090Sstevel@tonic-gate 10100Sstevel@tonic-gate if (res != 0) 10110Sstevel@tonic-gate break; 10120Sstevel@tonic-gate } else { 10130Sstevel@tonic-gate /* XHAT does not support seg_dev */ 10140Sstevel@tonic-gate res = FC_NOSUPPORT; 10150Sstevel@tonic-gate break; 10160Sstevel@tonic-gate } 10170Sstevel@tonic-gate } 10180Sstevel@tonic-gate 10190Sstevel@tonic-gate /* 10200Sstevel@tonic-gate * If we were SOFTLOCKing and encountered a failure, 10210Sstevel@tonic-gate * we must SOFTUNLOCK the range we already did. (Maybe we 10220Sstevel@tonic-gate * should just panic if we are SOFTLOCKing or even SOFTUNLOCKing 10230Sstevel@tonic-gate * right here...) 10240Sstevel@tonic-gate */ 10250Sstevel@tonic-gate if (res != 0 && type == F_SOFTLOCK) { 10260Sstevel@tonic-gate for (seg = segsav; addrsav < raddr; addrsav += ssize) { 10270Sstevel@tonic-gate if (addrsav >= seg->s_base + seg->s_size) 10280Sstevel@tonic-gate seg = AS_SEGNEXT(as, seg); 10290Sstevel@tonic-gate ASSERT(seg != NULL); 10300Sstevel@tonic-gate /* 10310Sstevel@tonic-gate * Now call the fault routine again to perform the 10320Sstevel@tonic-gate * unlock using S_OTHER instead of the rw variable 10330Sstevel@tonic-gate * since we never got a chance to touch the pages. 10340Sstevel@tonic-gate */ 10350Sstevel@tonic-gate if (raddr > seg->s_base + seg->s_size) 10360Sstevel@tonic-gate ssize = seg->s_base + seg->s_size - addrsav; 10370Sstevel@tonic-gate else 10380Sstevel@tonic-gate ssize = raddr - addrsav; 10390Sstevel@tonic-gate (void) SEGOP_FAULT(hat, seg, addrsav, ssize, 10400Sstevel@tonic-gate F_SOFTUNLOCK, S_OTHER); 10410Sstevel@tonic-gate } 10420Sstevel@tonic-gate } 10430Sstevel@tonic-gate if (as_lock_held) 10440Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 1045209Scwb if ((lwp != NULL) && (!is_xhat)) { 10460Sstevel@tonic-gate lwp->lwp_nostop--; 1047209Scwb lwp->lwp_nostop_r--; 1048209Scwb } 10490Sstevel@tonic-gate /* 10500Sstevel@tonic-gate * If the lower levels returned EDEADLK for a fault, 10510Sstevel@tonic-gate * It means that we should retry the fault. Let's wait 10520Sstevel@tonic-gate * a bit also to let the deadlock causing condition clear. 10530Sstevel@tonic-gate * This is part of a gross hack to work around a design flaw 10540Sstevel@tonic-gate * in the ufs/sds logging code and should go away when the 10550Sstevel@tonic-gate * logging code is re-designed to fix the problem. See bug 10560Sstevel@tonic-gate * 4125102 for details of the problem. 10570Sstevel@tonic-gate */ 10580Sstevel@tonic-gate if (FC_ERRNO(res) == EDEADLK) { 10590Sstevel@tonic-gate delay(deadlk_wait); 10600Sstevel@tonic-gate res = 0; 10610Sstevel@tonic-gate goto retry; 10620Sstevel@tonic-gate } 10630Sstevel@tonic-gate return (res); 10640Sstevel@tonic-gate } 10650Sstevel@tonic-gate 10660Sstevel@tonic-gate 10670Sstevel@tonic-gate 10680Sstevel@tonic-gate /* 10690Sstevel@tonic-gate * Asynchronous ``fault'' at addr for size bytes. 10700Sstevel@tonic-gate */ 10710Sstevel@tonic-gate faultcode_t 10720Sstevel@tonic-gate as_faulta(struct as *as, caddr_t addr, size_t size) 10730Sstevel@tonic-gate { 10740Sstevel@tonic-gate struct seg *seg; 10750Sstevel@tonic-gate caddr_t raddr; /* rounded down addr */ 10760Sstevel@tonic-gate size_t rsize; /* rounded up size */ 10770Sstevel@tonic-gate faultcode_t res = 0; 10780Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread); 10790Sstevel@tonic-gate 10800Sstevel@tonic-gate retry: 10810Sstevel@tonic-gate /* 10820Sstevel@tonic-gate * Indicate that the lwp is not to be stopped while waiting 10830Sstevel@tonic-gate * for a pagefault. This is to avoid deadlock while debugging 10840Sstevel@tonic-gate * a process via /proc over NFS (in particular). 10850Sstevel@tonic-gate */ 1086209Scwb if (lwp != NULL) { 10870Sstevel@tonic-gate lwp->lwp_nostop++; 1088209Scwb lwp->lwp_nostop_r++; 1089209Scwb } 10900Sstevel@tonic-gate 10910Sstevel@tonic-gate raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK); 10920Sstevel@tonic-gate rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) - 10930Sstevel@tonic-gate (size_t)raddr; 10940Sstevel@tonic-gate 10950Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 10960Sstevel@tonic-gate seg = as_segat(as, raddr); 10970Sstevel@tonic-gate if (seg == NULL) { 10980Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 1099209Scwb if (lwp != NULL) { 11000Sstevel@tonic-gate lwp->lwp_nostop--; 1101209Scwb lwp->lwp_nostop_r--; 1102209Scwb } 11030Sstevel@tonic-gate return (FC_NOMAP); 11040Sstevel@tonic-gate } 11050Sstevel@tonic-gate 11060Sstevel@tonic-gate for (; rsize != 0; rsize -= PAGESIZE, raddr += PAGESIZE) { 11070Sstevel@tonic-gate if (raddr >= seg->s_base + seg->s_size) { 11080Sstevel@tonic-gate seg = AS_SEGNEXT(as, seg); 11090Sstevel@tonic-gate if (seg == NULL || raddr != seg->s_base) { 11100Sstevel@tonic-gate res = FC_NOMAP; 11110Sstevel@tonic-gate break; 11120Sstevel@tonic-gate } 11130Sstevel@tonic-gate } 11140Sstevel@tonic-gate res = SEGOP_FAULTA(seg, raddr); 11150Sstevel@tonic-gate if (res != 0) 11160Sstevel@tonic-gate break; 11170Sstevel@tonic-gate } 11180Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 1119209Scwb if (lwp != NULL) { 11200Sstevel@tonic-gate lwp->lwp_nostop--; 1121209Scwb lwp->lwp_nostop_r--; 1122209Scwb } 11230Sstevel@tonic-gate /* 11240Sstevel@tonic-gate * If the lower levels returned EDEADLK for a fault, 11250Sstevel@tonic-gate * It means that we should retry the fault. Let's wait 11260Sstevel@tonic-gate * a bit also to let the deadlock causing condition clear. 11270Sstevel@tonic-gate * This is part of a gross hack to work around a design flaw 11280Sstevel@tonic-gate * in the ufs/sds logging code and should go away when the 11290Sstevel@tonic-gate * logging code is re-designed to fix the problem. See bug 11300Sstevel@tonic-gate * 4125102 for details of the problem. 11310Sstevel@tonic-gate */ 11320Sstevel@tonic-gate if (FC_ERRNO(res) == EDEADLK) { 11330Sstevel@tonic-gate delay(deadlk_wait); 11340Sstevel@tonic-gate res = 0; 11350Sstevel@tonic-gate goto retry; 11360Sstevel@tonic-gate } 11370Sstevel@tonic-gate return (res); 11380Sstevel@tonic-gate } 11390Sstevel@tonic-gate 11400Sstevel@tonic-gate /* 11410Sstevel@tonic-gate * Set the virtual mapping for the interval from [addr : addr + size) 11420Sstevel@tonic-gate * in address space `as' to have the specified protection. 11430Sstevel@tonic-gate * It is ok for the range to cross over several segments, 11440Sstevel@tonic-gate * as long as they are contiguous. 11450Sstevel@tonic-gate */ 11460Sstevel@tonic-gate int 11470Sstevel@tonic-gate as_setprot(struct as *as, caddr_t addr, size_t size, uint_t prot) 11480Sstevel@tonic-gate { 11490Sstevel@tonic-gate struct seg *seg; 11500Sstevel@tonic-gate struct as_callback *cb; 11510Sstevel@tonic-gate size_t ssize; 11520Sstevel@tonic-gate caddr_t raddr; /* rounded down addr */ 11530Sstevel@tonic-gate size_t rsize; /* rounded up size */ 11540Sstevel@tonic-gate int error = 0, writer = 0; 11550Sstevel@tonic-gate caddr_t saveraddr; 11560Sstevel@tonic-gate size_t saversize; 11570Sstevel@tonic-gate 11580Sstevel@tonic-gate setprot_top: 11590Sstevel@tonic-gate raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK); 11600Sstevel@tonic-gate rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) - 11610Sstevel@tonic-gate (size_t)raddr; 11620Sstevel@tonic-gate 11630Sstevel@tonic-gate if (raddr + rsize < raddr) /* check for wraparound */ 11640Sstevel@tonic-gate return (ENOMEM); 11650Sstevel@tonic-gate 11660Sstevel@tonic-gate saveraddr = raddr; 11670Sstevel@tonic-gate saversize = rsize; 11680Sstevel@tonic-gate 11690Sstevel@tonic-gate /* 11700Sstevel@tonic-gate * Normally we only lock the as as a reader. But 11710Sstevel@tonic-gate * if due to setprot the segment driver needs to split 11720Sstevel@tonic-gate * a segment it will return IE_RETRY. Therefore we re-aquire 11730Sstevel@tonic-gate * the as lock as a writer so the segment driver can change 11740Sstevel@tonic-gate * the seg list. Also the segment driver will return IE_RETRY 11750Sstevel@tonic-gate * after it has changed the segment list so we therefore keep 11760Sstevel@tonic-gate * locking as a writer. Since these opeartions should be rare 11770Sstevel@tonic-gate * want to only lock as a writer when necessary. 11780Sstevel@tonic-gate */ 11790Sstevel@tonic-gate if (writer || avl_numnodes(&as->a_wpage) != 0) { 11800Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER); 11810Sstevel@tonic-gate } else { 11820Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 11830Sstevel@tonic-gate } 11840Sstevel@tonic-gate 11850Sstevel@tonic-gate as_clearwatchprot(as, raddr, rsize); 11860Sstevel@tonic-gate seg = as_segat(as, raddr); 11870Sstevel@tonic-gate if (seg == NULL) { 11880Sstevel@tonic-gate as_setwatch(as); 11890Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 11900Sstevel@tonic-gate return (ENOMEM); 11910Sstevel@tonic-gate } 11920Sstevel@tonic-gate 11930Sstevel@tonic-gate for (; rsize != 0; rsize -= ssize, raddr += ssize) { 11940Sstevel@tonic-gate if (raddr >= seg->s_base + seg->s_size) { 11950Sstevel@tonic-gate seg = AS_SEGNEXT(as, seg); 11960Sstevel@tonic-gate if (seg == NULL || raddr != seg->s_base) { 11970Sstevel@tonic-gate error = ENOMEM; 11980Sstevel@tonic-gate break; 11990Sstevel@tonic-gate } 12000Sstevel@tonic-gate } 12010Sstevel@tonic-gate if ((raddr + rsize) > (seg->s_base + seg->s_size)) 12020Sstevel@tonic-gate ssize = seg->s_base + seg->s_size - raddr; 12030Sstevel@tonic-gate else 12040Sstevel@tonic-gate ssize = rsize; 12050Sstevel@tonic-gate error = SEGOP_SETPROT(seg, raddr, ssize, prot); 12060Sstevel@tonic-gate 12070Sstevel@tonic-gate if (error == IE_NOMEM) { 12080Sstevel@tonic-gate error = EAGAIN; 12090Sstevel@tonic-gate break; 12100Sstevel@tonic-gate } 12110Sstevel@tonic-gate 12120Sstevel@tonic-gate if (error == IE_RETRY) { 12130Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 12140Sstevel@tonic-gate writer = 1; 12150Sstevel@tonic-gate goto setprot_top; 12160Sstevel@tonic-gate } 12170Sstevel@tonic-gate 12180Sstevel@tonic-gate if (error == EAGAIN) { 12190Sstevel@tonic-gate /* 12200Sstevel@tonic-gate * Make sure we have a_lock as writer. 12210Sstevel@tonic-gate */ 12220Sstevel@tonic-gate if (writer == 0) { 12230Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 12240Sstevel@tonic-gate writer = 1; 12250Sstevel@tonic-gate goto setprot_top; 12260Sstevel@tonic-gate } 12270Sstevel@tonic-gate 12280Sstevel@tonic-gate /* 12290Sstevel@tonic-gate * Memory is currently locked. It must be unlocked 12300Sstevel@tonic-gate * before this operation can succeed through a retry. 12310Sstevel@tonic-gate * The possible reasons for locked memory and 12320Sstevel@tonic-gate * corresponding strategies for unlocking are: 12330Sstevel@tonic-gate * (1) Normal I/O 12340Sstevel@tonic-gate * wait for a signal that the I/O operation 12350Sstevel@tonic-gate * has completed and the memory is unlocked. 12360Sstevel@tonic-gate * (2) Asynchronous I/O 12370Sstevel@tonic-gate * The aio subsystem does not unlock pages when 12380Sstevel@tonic-gate * the I/O is completed. Those pages are unlocked 12390Sstevel@tonic-gate * when the application calls aiowait/aioerror. 12400Sstevel@tonic-gate * So, to prevent blocking forever, cv_broadcast() 12410Sstevel@tonic-gate * is done to wake up aio_cleanup_thread. 12420Sstevel@tonic-gate * Subsequently, segvn_reclaim will be called, and 12430Sstevel@tonic-gate * that will do AS_CLRUNMAPWAIT() and wake us up. 12440Sstevel@tonic-gate * (3) Long term page locking: 12450Sstevel@tonic-gate * Drivers intending to have pages locked for a 12460Sstevel@tonic-gate * period considerably longer than for normal I/O 12470Sstevel@tonic-gate * (essentially forever) may have registered for a 12480Sstevel@tonic-gate * callback so they may unlock these pages on 12490Sstevel@tonic-gate * request. This is needed to allow this operation 12500Sstevel@tonic-gate * to succeed. Each entry on the callback list is 12510Sstevel@tonic-gate * examined. If the event or address range pertains 12520Sstevel@tonic-gate * the callback is invoked (unless it already is in 12530Sstevel@tonic-gate * progress). The a_contents lock must be dropped 12540Sstevel@tonic-gate * before the callback, so only one callback can 12550Sstevel@tonic-gate * be done at a time. Go to the top and do more 12560Sstevel@tonic-gate * until zero is returned. If zero is returned, 12570Sstevel@tonic-gate * either there were no callbacks for this event 12580Sstevel@tonic-gate * or they were already in progress. 12590Sstevel@tonic-gate */ 12600Sstevel@tonic-gate mutex_enter(&as->a_contents); 12610Sstevel@tonic-gate if (as->a_callbacks && 12620Sstevel@tonic-gate (cb = as_find_callback(as, AS_SETPROT_EVENT, 12630Sstevel@tonic-gate seg->s_base, seg->s_size))) { 12640Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 12650Sstevel@tonic-gate as_execute_callback(as, cb, AS_SETPROT_EVENT); 12660Sstevel@tonic-gate } else { 12670Sstevel@tonic-gate if (AS_ISUNMAPWAIT(as) == 0) 12680Sstevel@tonic-gate cv_broadcast(&as->a_cv); 12690Sstevel@tonic-gate AS_SETUNMAPWAIT(as); 12700Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 12710Sstevel@tonic-gate while (AS_ISUNMAPWAIT(as)) 12720Sstevel@tonic-gate cv_wait(&as->a_cv, &as->a_contents); 12730Sstevel@tonic-gate } 12740Sstevel@tonic-gate mutex_exit(&as->a_contents); 12750Sstevel@tonic-gate goto setprot_top; 12760Sstevel@tonic-gate } else if (error != 0) 12770Sstevel@tonic-gate break; 12780Sstevel@tonic-gate } 12790Sstevel@tonic-gate if (error != 0) { 12800Sstevel@tonic-gate as_setwatch(as); 12810Sstevel@tonic-gate } else { 12820Sstevel@tonic-gate as_setwatchprot(as, saveraddr, saversize, prot); 12830Sstevel@tonic-gate } 12840Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 12850Sstevel@tonic-gate return (error); 12860Sstevel@tonic-gate } 12870Sstevel@tonic-gate 12880Sstevel@tonic-gate /* 12890Sstevel@tonic-gate * Check to make sure that the interval [addr, addr + size) 12900Sstevel@tonic-gate * in address space `as' has at least the specified protection. 12910Sstevel@tonic-gate * It is ok for the range to cross over several segments, as long 12920Sstevel@tonic-gate * as they are contiguous. 12930Sstevel@tonic-gate */ 12940Sstevel@tonic-gate int 12950Sstevel@tonic-gate as_checkprot(struct as *as, caddr_t addr, size_t size, uint_t prot) 12960Sstevel@tonic-gate { 12970Sstevel@tonic-gate struct seg *seg; 12980Sstevel@tonic-gate size_t ssize; 12990Sstevel@tonic-gate caddr_t raddr; /* rounded down addr */ 13000Sstevel@tonic-gate size_t rsize; /* rounded up size */ 13010Sstevel@tonic-gate int error = 0; 13020Sstevel@tonic-gate 13030Sstevel@tonic-gate raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK); 13040Sstevel@tonic-gate rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) - 13050Sstevel@tonic-gate (size_t)raddr; 13060Sstevel@tonic-gate 13070Sstevel@tonic-gate if (raddr + rsize < raddr) /* check for wraparound */ 13080Sstevel@tonic-gate return (ENOMEM); 13090Sstevel@tonic-gate 13100Sstevel@tonic-gate /* 13110Sstevel@tonic-gate * This is ugly as sin... 13120Sstevel@tonic-gate * Normally, we only acquire the address space readers lock. 13130Sstevel@tonic-gate * However, if the address space has watchpoints present, 13140Sstevel@tonic-gate * we must acquire the writer lock on the address space for 13150Sstevel@tonic-gate * the benefit of as_clearwatchprot() and as_setwatchprot(). 13160Sstevel@tonic-gate */ 13170Sstevel@tonic-gate if (avl_numnodes(&as->a_wpage) != 0) 13180Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER); 13190Sstevel@tonic-gate else 13200Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 13210Sstevel@tonic-gate as_clearwatchprot(as, raddr, rsize); 13220Sstevel@tonic-gate seg = as_segat(as, raddr); 13230Sstevel@tonic-gate if (seg == NULL) { 13240Sstevel@tonic-gate as_setwatch(as); 13250Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 13260Sstevel@tonic-gate return (ENOMEM); 13270Sstevel@tonic-gate } 13280Sstevel@tonic-gate 13290Sstevel@tonic-gate for (; rsize != 0; rsize -= ssize, raddr += ssize) { 13300Sstevel@tonic-gate if (raddr >= seg->s_base + seg->s_size) { 13310Sstevel@tonic-gate seg = AS_SEGNEXT(as, seg); 13320Sstevel@tonic-gate if (seg == NULL || raddr != seg->s_base) { 13330Sstevel@tonic-gate error = ENOMEM; 13340Sstevel@tonic-gate break; 13350Sstevel@tonic-gate } 13360Sstevel@tonic-gate } 13370Sstevel@tonic-gate if ((raddr + rsize) > (seg->s_base + seg->s_size)) 13380Sstevel@tonic-gate ssize = seg->s_base + seg->s_size - raddr; 13390Sstevel@tonic-gate else 13400Sstevel@tonic-gate ssize = rsize; 13410Sstevel@tonic-gate 13420Sstevel@tonic-gate error = SEGOP_CHECKPROT(seg, raddr, ssize, prot); 13430Sstevel@tonic-gate if (error != 0) 13440Sstevel@tonic-gate break; 13450Sstevel@tonic-gate } 13460Sstevel@tonic-gate as_setwatch(as); 13470Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 13480Sstevel@tonic-gate return (error); 13490Sstevel@tonic-gate } 13500Sstevel@tonic-gate 13510Sstevel@tonic-gate int 13520Sstevel@tonic-gate as_unmap(struct as *as, caddr_t addr, size_t size) 13530Sstevel@tonic-gate { 13540Sstevel@tonic-gate struct seg *seg, *seg_next; 13550Sstevel@tonic-gate struct as_callback *cb; 13560Sstevel@tonic-gate caddr_t raddr, eaddr; 13570Sstevel@tonic-gate size_t ssize; 13580Sstevel@tonic-gate int err; 13590Sstevel@tonic-gate 13600Sstevel@tonic-gate top: 13610Sstevel@tonic-gate raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK); 13620Sstevel@tonic-gate eaddr = (caddr_t)(((uintptr_t)(addr + size) + PAGEOFFSET) & 13630Sstevel@tonic-gate (uintptr_t)PAGEMASK); 13640Sstevel@tonic-gate 13650Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER); 13660Sstevel@tonic-gate 13670Sstevel@tonic-gate as->a_updatedir = 1; /* inform /proc */ 13680Sstevel@tonic-gate gethrestime(&as->a_updatetime); 13690Sstevel@tonic-gate 13700Sstevel@tonic-gate /* 13710Sstevel@tonic-gate * Use as_findseg to find the first segment in the range, then 13720Sstevel@tonic-gate * step through the segments in order, following s_next. 13730Sstevel@tonic-gate */ 13740Sstevel@tonic-gate as_clearwatchprot(as, raddr, eaddr - raddr); 13750Sstevel@tonic-gate 13760Sstevel@tonic-gate for (seg = as_findseg(as, raddr, 0); seg != NULL; seg = seg_next) { 13770Sstevel@tonic-gate if (eaddr <= seg->s_base) 13780Sstevel@tonic-gate break; /* eaddr was in a gap; all done */ 13790Sstevel@tonic-gate 13800Sstevel@tonic-gate /* this is implied by the test above */ 13810Sstevel@tonic-gate ASSERT(raddr < eaddr); 13820Sstevel@tonic-gate 13830Sstevel@tonic-gate if (raddr < seg->s_base) 13840Sstevel@tonic-gate raddr = seg->s_base; /* raddr was in a gap */ 13850Sstevel@tonic-gate 13860Sstevel@tonic-gate if (eaddr > (seg->s_base + seg->s_size)) 13870Sstevel@tonic-gate ssize = seg->s_base + seg->s_size - raddr; 13880Sstevel@tonic-gate else 13890Sstevel@tonic-gate ssize = eaddr - raddr; 13900Sstevel@tonic-gate 13910Sstevel@tonic-gate /* 13920Sstevel@tonic-gate * Save next segment pointer since seg can be 13930Sstevel@tonic-gate * destroyed during the segment unmap operation. 13940Sstevel@tonic-gate */ 13950Sstevel@tonic-gate seg_next = AS_SEGNEXT(as, seg); 13960Sstevel@tonic-gate 13970Sstevel@tonic-gate err = SEGOP_UNMAP(seg, raddr, ssize); 13980Sstevel@tonic-gate if (err == EAGAIN) { 13990Sstevel@tonic-gate /* 14000Sstevel@tonic-gate * Memory is currently locked. It must be unlocked 14010Sstevel@tonic-gate * before this operation can succeed through a retry. 14020Sstevel@tonic-gate * The possible reasons for locked memory and 14030Sstevel@tonic-gate * corresponding strategies for unlocking are: 14040Sstevel@tonic-gate * (1) Normal I/O 14050Sstevel@tonic-gate * wait for a signal that the I/O operation 14060Sstevel@tonic-gate * has completed and the memory is unlocked. 14070Sstevel@tonic-gate * (2) Asynchronous I/O 14080Sstevel@tonic-gate * The aio subsystem does not unlock pages when 14090Sstevel@tonic-gate * the I/O is completed. Those pages are unlocked 14100Sstevel@tonic-gate * when the application calls aiowait/aioerror. 14110Sstevel@tonic-gate * So, to prevent blocking forever, cv_broadcast() 14120Sstevel@tonic-gate * is done to wake up aio_cleanup_thread. 14130Sstevel@tonic-gate * Subsequently, segvn_reclaim will be called, and 14140Sstevel@tonic-gate * that will do AS_CLRUNMAPWAIT() and wake us up. 14150Sstevel@tonic-gate * (3) Long term page locking: 14160Sstevel@tonic-gate * Drivers intending to have pages locked for a 14170Sstevel@tonic-gate * period considerably longer than for normal I/O 14180Sstevel@tonic-gate * (essentially forever) may have registered for a 14190Sstevel@tonic-gate * callback so they may unlock these pages on 14200Sstevel@tonic-gate * request. This is needed to allow this operation 14210Sstevel@tonic-gate * to succeed. Each entry on the callback list is 14220Sstevel@tonic-gate * examined. If the event or address range pertains 14230Sstevel@tonic-gate * the callback is invoked (unless it already is in 14240Sstevel@tonic-gate * progress). The a_contents lock must be dropped 14250Sstevel@tonic-gate * before the callback, so only one callback can 14260Sstevel@tonic-gate * be done at a time. Go to the top and do more 14270Sstevel@tonic-gate * until zero is returned. If zero is returned, 14280Sstevel@tonic-gate * either there were no callbacks for this event 14290Sstevel@tonic-gate * or they were already in progress. 14300Sstevel@tonic-gate */ 14310Sstevel@tonic-gate as_setwatch(as); 14320Sstevel@tonic-gate mutex_enter(&as->a_contents); 14330Sstevel@tonic-gate if (as->a_callbacks && 14340Sstevel@tonic-gate (cb = as_find_callback(as, AS_UNMAP_EVENT, 14350Sstevel@tonic-gate seg->s_base, seg->s_size))) { 14360Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 14370Sstevel@tonic-gate as_execute_callback(as, cb, AS_UNMAP_EVENT); 14380Sstevel@tonic-gate } else { 14390Sstevel@tonic-gate if (AS_ISUNMAPWAIT(as) == 0) 14400Sstevel@tonic-gate cv_broadcast(&as->a_cv); 14410Sstevel@tonic-gate AS_SETUNMAPWAIT(as); 14420Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 14430Sstevel@tonic-gate while (AS_ISUNMAPWAIT(as)) 14440Sstevel@tonic-gate cv_wait(&as->a_cv, &as->a_contents); 14450Sstevel@tonic-gate } 14460Sstevel@tonic-gate mutex_exit(&as->a_contents); 14470Sstevel@tonic-gate goto top; 14480Sstevel@tonic-gate } else if (err == IE_RETRY) { 14490Sstevel@tonic-gate as_setwatch(as); 14500Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 14510Sstevel@tonic-gate goto top; 14520Sstevel@tonic-gate } else if (err) { 14530Sstevel@tonic-gate as_setwatch(as); 14540Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 14550Sstevel@tonic-gate return (-1); 14560Sstevel@tonic-gate } 14570Sstevel@tonic-gate 14580Sstevel@tonic-gate as->a_size -= ssize; 14590Sstevel@tonic-gate raddr += ssize; 14600Sstevel@tonic-gate } 14610Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 14620Sstevel@tonic-gate return (0); 14630Sstevel@tonic-gate } 14640Sstevel@tonic-gate 14650Sstevel@tonic-gate static int 14662414Saguzovsk as_map_segvn_segs(struct as *as, caddr_t addr, size_t size, uint_t szcvec, 14670Sstevel@tonic-gate int (*crfp)(), struct segvn_crargs *vn_a, int *segcreated) 14680Sstevel@tonic-gate { 14690Sstevel@tonic-gate uint_t szc; 14700Sstevel@tonic-gate uint_t nszc; 14710Sstevel@tonic-gate int error; 14720Sstevel@tonic-gate caddr_t a; 14730Sstevel@tonic-gate caddr_t eaddr; 14740Sstevel@tonic-gate size_t segsize; 14750Sstevel@tonic-gate struct seg *seg; 14762414Saguzovsk size_t pgsz; 14772414Saguzovsk int do_off = (vn_a->vp != NULL || vn_a->amp != NULL); 14780Sstevel@tonic-gate uint_t save_szcvec; 14792414Saguzovsk 14802414Saguzovsk ASSERT(AS_WRITE_HELD(as, &as->a_lock)); 14812414Saguzovsk ASSERT(IS_P2ALIGNED(addr, PAGESIZE)); 14822414Saguzovsk ASSERT(IS_P2ALIGNED(size, PAGESIZE)); 14832414Saguzovsk ASSERT(vn_a->vp == NULL || vn_a->amp == NULL); 14842414Saguzovsk if (!do_off) { 14852414Saguzovsk vn_a->offset = 0; 14862414Saguzovsk } 14872414Saguzovsk 14882414Saguzovsk if (szcvec <= 1) { 14892414Saguzovsk seg = seg_alloc(as, addr, size); 14902414Saguzovsk if (seg == NULL) { 14912414Saguzovsk return (ENOMEM); 14922414Saguzovsk } 14932414Saguzovsk vn_a->szc = 0; 14942414Saguzovsk error = (*crfp)(seg, vn_a); 14952414Saguzovsk if (error != 0) { 14962414Saguzovsk seg_free(seg); 14972414Saguzovsk } 14982414Saguzovsk return (error); 14992414Saguzovsk } 15002414Saguzovsk 15012414Saguzovsk eaddr = addr + size; 15022414Saguzovsk save_szcvec = szcvec; 15032414Saguzovsk szcvec >>= 1; 15042414Saguzovsk szc = 0; 15052414Saguzovsk nszc = 0; 15062414Saguzovsk while (szcvec) { 15072414Saguzovsk if ((szcvec & 0x1) == 0) { 15082414Saguzovsk nszc++; 15092414Saguzovsk szcvec >>= 1; 15102414Saguzovsk continue; 15112414Saguzovsk } 15122414Saguzovsk nszc++; 15132414Saguzovsk pgsz = page_get_pagesize(nszc); 15142414Saguzovsk a = (caddr_t)P2ROUNDUP((uintptr_t)addr, pgsz); 15152414Saguzovsk if (a != addr) { 15162414Saguzovsk ASSERT(a < eaddr); 15172414Saguzovsk segsize = a - addr; 15182414Saguzovsk seg = seg_alloc(as, addr, segsize); 15192414Saguzovsk if (seg == NULL) { 15202414Saguzovsk return (ENOMEM); 15212414Saguzovsk } 15222414Saguzovsk vn_a->szc = szc; 15232414Saguzovsk error = (*crfp)(seg, vn_a); 15242414Saguzovsk if (error != 0) { 15252414Saguzovsk seg_free(seg); 15262414Saguzovsk return (error); 15272414Saguzovsk } 15282414Saguzovsk *segcreated = 1; 15292414Saguzovsk if (do_off) { 15302414Saguzovsk vn_a->offset += segsize; 15312414Saguzovsk } 15322414Saguzovsk addr = a; 15332414Saguzovsk } 15342414Saguzovsk szc = nszc; 15352414Saguzovsk szcvec >>= 1; 15362414Saguzovsk } 15372414Saguzovsk 15382414Saguzovsk ASSERT(addr < eaddr); 15392414Saguzovsk szcvec = save_szcvec | 1; /* add 8K pages */ 15402414Saguzovsk while (szcvec) { 15412414Saguzovsk a = (caddr_t)P2ALIGN((uintptr_t)eaddr, pgsz); 15422414Saguzovsk ASSERT(a >= addr); 15432414Saguzovsk if (a != addr) { 15442414Saguzovsk segsize = a - addr; 15452414Saguzovsk seg = seg_alloc(as, addr, segsize); 15462414Saguzovsk if (seg == NULL) { 15472414Saguzovsk return (ENOMEM); 15482414Saguzovsk } 15492414Saguzovsk vn_a->szc = szc; 15502414Saguzovsk error = (*crfp)(seg, vn_a); 15512414Saguzovsk if (error != 0) { 15522414Saguzovsk seg_free(seg); 15532414Saguzovsk return (error); 15542414Saguzovsk } 15552414Saguzovsk *segcreated = 1; 15562414Saguzovsk if (do_off) { 15572414Saguzovsk vn_a->offset += segsize; 15582414Saguzovsk } 15592414Saguzovsk addr = a; 15602414Saguzovsk } 15612414Saguzovsk szcvec &= ~(1 << szc); 15622414Saguzovsk if (szcvec) { 15632414Saguzovsk szc = highbit(szcvec) - 1; 15642414Saguzovsk pgsz = page_get_pagesize(szc); 15652414Saguzovsk } 15662414Saguzovsk } 15672414Saguzovsk ASSERT(addr == eaddr); 15682414Saguzovsk 15692414Saguzovsk return (0); 15702414Saguzovsk } 15712414Saguzovsk 15722414Saguzovsk static int 15732414Saguzovsk as_map_vnsegs(struct as *as, caddr_t addr, size_t size, 15742414Saguzovsk int (*crfp)(), struct segvn_crargs *vn_a, int *segcreated) 15752414Saguzovsk { 15762991Ssusans uint_t mapflags = vn_a->flags & (MAP_TEXT | MAP_INITDATA); 15772991Ssusans int type = (vn_a->type == MAP_SHARED) ? MAPPGSZC_SHM : MAPPGSZC_PRIVM; 15782991Ssusans uint_t szcvec = map_pgszcvec(addr, size, (uintptr_t)addr, mapflags, 15792991Ssusans type, 0); 15802414Saguzovsk int error; 15812414Saguzovsk struct seg *seg; 15820Sstevel@tonic-gate struct vattr va; 15830Sstevel@tonic-gate u_offset_t eoff; 15840Sstevel@tonic-gate size_t save_size = 0; 15850Sstevel@tonic-gate 15860Sstevel@tonic-gate ASSERT(AS_WRITE_HELD(as, &as->a_lock)); 15870Sstevel@tonic-gate ASSERT(IS_P2ALIGNED(addr, PAGESIZE)); 15880Sstevel@tonic-gate ASSERT(IS_P2ALIGNED(size, PAGESIZE)); 15890Sstevel@tonic-gate ASSERT(vn_a->vp != NULL); 15900Sstevel@tonic-gate ASSERT(vn_a->amp == NULL); 15910Sstevel@tonic-gate 15920Sstevel@tonic-gate again: 15930Sstevel@tonic-gate if (szcvec <= 1) { 15940Sstevel@tonic-gate seg = seg_alloc(as, addr, size); 15950Sstevel@tonic-gate if (seg == NULL) { 15960Sstevel@tonic-gate return (ENOMEM); 15970Sstevel@tonic-gate } 15980Sstevel@tonic-gate vn_a->szc = 0; 15990Sstevel@tonic-gate error = (*crfp)(seg, vn_a); 16000Sstevel@tonic-gate if (error != 0) { 16010Sstevel@tonic-gate seg_free(seg); 16020Sstevel@tonic-gate } 16030Sstevel@tonic-gate return (error); 16040Sstevel@tonic-gate } 16050Sstevel@tonic-gate 16060Sstevel@tonic-gate va.va_mask = AT_SIZE; 16070Sstevel@tonic-gate if (VOP_GETATTR(vn_a->vp, &va, ATTR_HINT, vn_a->cred) != 0) { 16080Sstevel@tonic-gate szcvec = 0; 16090Sstevel@tonic-gate goto again; 16100Sstevel@tonic-gate } 16110Sstevel@tonic-gate eoff = vn_a->offset & PAGEMASK; 16120Sstevel@tonic-gate if (eoff >= va.va_size) { 16130Sstevel@tonic-gate szcvec = 0; 16140Sstevel@tonic-gate goto again; 16150Sstevel@tonic-gate } 16160Sstevel@tonic-gate eoff += size; 16170Sstevel@tonic-gate if (btopr(va.va_size) < btopr(eoff)) { 16180Sstevel@tonic-gate save_size = size; 16190Sstevel@tonic-gate size = va.va_size - (vn_a->offset & PAGEMASK); 16200Sstevel@tonic-gate size = P2ROUNDUP_TYPED(size, PAGESIZE, size_t); 16212991Ssusans szcvec = map_pgszcvec(addr, size, (uintptr_t)addr, mapflags, 16222991Ssusans type, 0); 16230Sstevel@tonic-gate if (szcvec <= 1) { 16240Sstevel@tonic-gate size = save_size; 16250Sstevel@tonic-gate goto again; 16260Sstevel@tonic-gate } 16270Sstevel@tonic-gate } 16280Sstevel@tonic-gate 16292414Saguzovsk error = as_map_segvn_segs(as, addr, size, szcvec, crfp, vn_a, 16302414Saguzovsk segcreated); 16312414Saguzovsk if (error != 0) { 16322414Saguzovsk return (error); 16330Sstevel@tonic-gate } 16340Sstevel@tonic-gate if (save_size) { 16352414Saguzovsk addr += size; 16360Sstevel@tonic-gate size = save_size - size; 16372414Saguzovsk szcvec = 0; 16380Sstevel@tonic-gate goto again; 16390Sstevel@tonic-gate } 16402414Saguzovsk return (0); 16412414Saguzovsk } 16420Sstevel@tonic-gate 16432991Ssusans /* 16442991Ssusans * as_map_ansegs: shared or private anonymous memory. Note that the flags 16452991Ssusans * passed to map_pgszvec cannot be MAP_INITDATA, for anon. 16462991Ssusans */ 16472414Saguzovsk static int 16482991Ssusans as_map_ansegs(struct as *as, caddr_t addr, size_t size, 16492414Saguzovsk int (*crfp)(), struct segvn_crargs *vn_a, int *segcreated) 16502414Saguzovsk { 16512991Ssusans uint_t szcvec; 16522991Ssusans uchar_t type; 16532991Ssusans 16542991Ssusans ASSERT(vn_a->type == MAP_SHARED || vn_a->type == MAP_PRIVATE); 16552991Ssusans if (vn_a->type == MAP_SHARED) { 16562991Ssusans type = MAPPGSZC_SHM; 16572991Ssusans } else if (vn_a->type == MAP_PRIVATE) { 16582991Ssusans if (vn_a->szc == AS_MAP_HEAP) { 16592991Ssusans type = MAPPGSZC_HEAP; 16602991Ssusans } else if (vn_a->szc == AS_MAP_STACK) { 16612991Ssusans type = MAPPGSZC_STACK; 16622991Ssusans } else { 16632991Ssusans type = MAPPGSZC_PRIVM; 16642991Ssusans } 16652991Ssusans } 16662991Ssusans szcvec = map_pgszcvec(addr, size, vn_a->amp == NULL ? 16672991Ssusans (uintptr_t)addr : (uintptr_t)P2ROUNDUP(vn_a->offset, PAGESIZE), 16682991Ssusans (vn_a->flags & MAP_TEXT), type, 0); 16692414Saguzovsk ASSERT(AS_WRITE_HELD(as, &as->a_lock)); 16702414Saguzovsk ASSERT(IS_P2ALIGNED(addr, PAGESIZE)); 16712414Saguzovsk ASSERT(IS_P2ALIGNED(size, PAGESIZE)); 16722414Saguzovsk ASSERT(vn_a->vp == NULL); 16732414Saguzovsk 16742414Saguzovsk return (as_map_segvn_segs(as, addr, size, szcvec, 16752414Saguzovsk crfp, vn_a, segcreated)); 16760Sstevel@tonic-gate } 16770Sstevel@tonic-gate 16780Sstevel@tonic-gate int 16790Sstevel@tonic-gate as_map(struct as *as, caddr_t addr, size_t size, int (*crfp)(), void *argsp) 16800Sstevel@tonic-gate { 16811899Svsakar AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER); 16821899Svsakar return (as_map_locked(as, addr, size, crfp, argsp)); 16831899Svsakar } 16841899Svsakar 16851899Svsakar int 16861899Svsakar as_map_locked(struct as *as, caddr_t addr, size_t size, int (*crfp)(), 16871899Svsakar void *argsp) 16881899Svsakar { 16890Sstevel@tonic-gate struct seg *seg = NULL; 16900Sstevel@tonic-gate caddr_t raddr; /* rounded down addr */ 16910Sstevel@tonic-gate size_t rsize; /* rounded up size */ 16920Sstevel@tonic-gate int error; 16932991Ssusans int unmap = 0; 16940Sstevel@tonic-gate struct proc *p = curproc; 1695*3183Ssusans struct segvn_crargs crargs; 16960Sstevel@tonic-gate 16970Sstevel@tonic-gate raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK); 16980Sstevel@tonic-gate rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) - 16990Sstevel@tonic-gate (size_t)raddr; 17000Sstevel@tonic-gate 17010Sstevel@tonic-gate /* 17020Sstevel@tonic-gate * check for wrap around 17030Sstevel@tonic-gate */ 17040Sstevel@tonic-gate if ((raddr + rsize < raddr) || (as->a_size > (ULONG_MAX - size))) { 17050Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 17060Sstevel@tonic-gate return (ENOMEM); 17070Sstevel@tonic-gate } 17080Sstevel@tonic-gate 17090Sstevel@tonic-gate as->a_updatedir = 1; /* inform /proc */ 17100Sstevel@tonic-gate gethrestime(&as->a_updatetime); 17110Sstevel@tonic-gate 17120Sstevel@tonic-gate if (as != &kas && as->a_size + rsize > (size_t)p->p_vmem_ctl) { 17130Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 17140Sstevel@tonic-gate 17150Sstevel@tonic-gate (void) rctl_action(rctlproc_legacy[RLIMIT_VMEM], p->p_rctls, p, 17160Sstevel@tonic-gate RCA_UNSAFE_ALL); 17170Sstevel@tonic-gate 17180Sstevel@tonic-gate return (ENOMEM); 17190Sstevel@tonic-gate } 17200Sstevel@tonic-gate 17212991Ssusans if (AS_MAP_CHECK_VNODE_LPOOB(crfp, argsp)) { 1722*3183Ssusans crargs = *(struct segvn_crargs *)argsp; 1723*3183Ssusans error = as_map_vnsegs(as, raddr, rsize, crfp, &crargs, &unmap); 17242991Ssusans if (error != 0) { 17252991Ssusans AS_LOCK_EXIT(as, &as->a_lock); 17262991Ssusans if (unmap) { 17272991Ssusans (void) as_unmap(as, addr, size); 17282991Ssusans } 17292991Ssusans return (error); 17302414Saguzovsk } 17312991Ssusans } else if (AS_MAP_CHECK_ANON_LPOOB(crfp, argsp)) { 1732*3183Ssusans crargs = *(struct segvn_crargs *)argsp; 1733*3183Ssusans error = as_map_ansegs(as, raddr, rsize, crfp, &crargs, &unmap); 17340Sstevel@tonic-gate if (error != 0) { 17350Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 17360Sstevel@tonic-gate if (unmap) { 17370Sstevel@tonic-gate (void) as_unmap(as, addr, size); 17380Sstevel@tonic-gate } 17390Sstevel@tonic-gate return (error); 17400Sstevel@tonic-gate } 17410Sstevel@tonic-gate } else { 17420Sstevel@tonic-gate seg = seg_alloc(as, addr, size); 17430Sstevel@tonic-gate if (seg == NULL) { 17440Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 17450Sstevel@tonic-gate return (ENOMEM); 17460Sstevel@tonic-gate } 17470Sstevel@tonic-gate 17480Sstevel@tonic-gate error = (*crfp)(seg, argsp); 17490Sstevel@tonic-gate if (error != 0) { 17500Sstevel@tonic-gate seg_free(seg); 17510Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 17520Sstevel@tonic-gate return (error); 17530Sstevel@tonic-gate } 17540Sstevel@tonic-gate } 17550Sstevel@tonic-gate 17560Sstevel@tonic-gate /* 17570Sstevel@tonic-gate * Add size now so as_unmap will work if as_ctl fails. 17580Sstevel@tonic-gate */ 17590Sstevel@tonic-gate as->a_size += rsize; 17600Sstevel@tonic-gate 17610Sstevel@tonic-gate as_setwatch(as); 17620Sstevel@tonic-gate 17630Sstevel@tonic-gate /* 17640Sstevel@tonic-gate * If the address space is locked, 17650Sstevel@tonic-gate * establish memory locks for the new segment. 17660Sstevel@tonic-gate */ 17670Sstevel@tonic-gate mutex_enter(&as->a_contents); 17680Sstevel@tonic-gate if (AS_ISPGLCK(as)) { 17690Sstevel@tonic-gate mutex_exit(&as->a_contents); 17700Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 17710Sstevel@tonic-gate error = as_ctl(as, addr, size, MC_LOCK, 0, 0, NULL, 0); 17720Sstevel@tonic-gate if (error != 0) 17730Sstevel@tonic-gate (void) as_unmap(as, addr, size); 17740Sstevel@tonic-gate } else { 17750Sstevel@tonic-gate mutex_exit(&as->a_contents); 17760Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 17770Sstevel@tonic-gate } 17780Sstevel@tonic-gate return (error); 17790Sstevel@tonic-gate } 17800Sstevel@tonic-gate 17810Sstevel@tonic-gate 17820Sstevel@tonic-gate /* 17830Sstevel@tonic-gate * Delete all segments in the address space marked with S_PURGE. 17840Sstevel@tonic-gate * This is currently used for Sparc V9 nofault ASI segments (seg_nf.c). 17850Sstevel@tonic-gate * These segments are deleted as a first step before calls to as_gap(), so 17860Sstevel@tonic-gate * that they don't affect mmap() or shmat(). 17870Sstevel@tonic-gate */ 17880Sstevel@tonic-gate void 17890Sstevel@tonic-gate as_purge(struct as *as) 17900Sstevel@tonic-gate { 17910Sstevel@tonic-gate struct seg *seg; 17920Sstevel@tonic-gate struct seg *next_seg; 17930Sstevel@tonic-gate 17940Sstevel@tonic-gate /* 17950Sstevel@tonic-gate * the setting of NEEDSPURGE is protect by as_rangelock(), so 17960Sstevel@tonic-gate * no need to grab a_contents mutex for this check 17970Sstevel@tonic-gate */ 17980Sstevel@tonic-gate if ((as->a_flags & AS_NEEDSPURGE) == 0) 17990Sstevel@tonic-gate return; 18000Sstevel@tonic-gate 18010Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER); 18020Sstevel@tonic-gate next_seg = NULL; 18030Sstevel@tonic-gate seg = AS_SEGFIRST(as); 18040Sstevel@tonic-gate while (seg != NULL) { 18050Sstevel@tonic-gate next_seg = AS_SEGNEXT(as, seg); 18060Sstevel@tonic-gate if (seg->s_flags & S_PURGE) 18070Sstevel@tonic-gate SEGOP_UNMAP(seg, seg->s_base, seg->s_size); 18080Sstevel@tonic-gate seg = next_seg; 18090Sstevel@tonic-gate } 18100Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 18110Sstevel@tonic-gate 18120Sstevel@tonic-gate mutex_enter(&as->a_contents); 18130Sstevel@tonic-gate as->a_flags &= ~AS_NEEDSPURGE; 18140Sstevel@tonic-gate mutex_exit(&as->a_contents); 18150Sstevel@tonic-gate } 18160Sstevel@tonic-gate 18170Sstevel@tonic-gate /* 18180Sstevel@tonic-gate * Find a hole of at least size minlen within [base, base + len). 18190Sstevel@tonic-gate * 18200Sstevel@tonic-gate * If flags specifies AH_HI, the hole will have the highest possible address 18210Sstevel@tonic-gate * in the range. We use the as->a_lastgap field to figure out where to 18220Sstevel@tonic-gate * start looking for a gap. 18230Sstevel@tonic-gate * 18240Sstevel@tonic-gate * Otherwise, the gap will have the lowest possible address. 18250Sstevel@tonic-gate * 18260Sstevel@tonic-gate * If flags specifies AH_CONTAIN, the hole will contain the address addr. 18270Sstevel@tonic-gate * 18280Sstevel@tonic-gate * If an adequate hole is found, base and len are set to reflect the part of 18290Sstevel@tonic-gate * the hole that is within range, and 0 is returned, otherwise, 18300Sstevel@tonic-gate * -1 is returned. 18310Sstevel@tonic-gate * 18320Sstevel@tonic-gate * NOTE: This routine is not correct when base+len overflows caddr_t. 18330Sstevel@tonic-gate */ 18340Sstevel@tonic-gate int 18350Sstevel@tonic-gate as_gap(struct as *as, size_t minlen, caddr_t *basep, size_t *lenp, uint_t flags, 18360Sstevel@tonic-gate caddr_t addr) 18370Sstevel@tonic-gate { 18380Sstevel@tonic-gate caddr_t lobound = *basep; 18390Sstevel@tonic-gate caddr_t hibound = lobound + *lenp; 18400Sstevel@tonic-gate struct seg *lseg, *hseg; 18410Sstevel@tonic-gate caddr_t lo, hi; 18420Sstevel@tonic-gate int forward; 18430Sstevel@tonic-gate caddr_t save_base; 18440Sstevel@tonic-gate size_t save_len; 18450Sstevel@tonic-gate 18460Sstevel@tonic-gate save_base = *basep; 18470Sstevel@tonic-gate save_len = *lenp; 18480Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 18490Sstevel@tonic-gate if (AS_SEGFIRST(as) == NULL) { 18500Sstevel@tonic-gate if (valid_va_range(basep, lenp, minlen, flags & AH_DIR)) { 18510Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 18520Sstevel@tonic-gate return (0); 18530Sstevel@tonic-gate } else { 18540Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 18550Sstevel@tonic-gate *basep = save_base; 18560Sstevel@tonic-gate *lenp = save_len; 18570Sstevel@tonic-gate return (-1); 18580Sstevel@tonic-gate } 18590Sstevel@tonic-gate } 18600Sstevel@tonic-gate 18610Sstevel@tonic-gate /* 18620Sstevel@tonic-gate * Set up to iterate over all the inter-segment holes in the given 18630Sstevel@tonic-gate * direction. lseg is NULL for the lowest-addressed hole and hseg is 18640Sstevel@tonic-gate * NULL for the highest-addressed hole. If moving backwards, we reset 18650Sstevel@tonic-gate * sseg to denote the highest-addressed segment. 18660Sstevel@tonic-gate */ 18670Sstevel@tonic-gate forward = (flags & AH_DIR) == AH_LO; 18680Sstevel@tonic-gate if (forward) { 18690Sstevel@tonic-gate hseg = as_findseg(as, lobound, 1); 18700Sstevel@tonic-gate lseg = AS_SEGPREV(as, hseg); 18710Sstevel@tonic-gate } else { 18720Sstevel@tonic-gate 18730Sstevel@tonic-gate /* 18740Sstevel@tonic-gate * If allocating at least as much as the last allocation, 18750Sstevel@tonic-gate * use a_lastgap's base as a better estimate of hibound. 18760Sstevel@tonic-gate */ 18770Sstevel@tonic-gate if (as->a_lastgap && 18780Sstevel@tonic-gate minlen >= as->a_lastgap->s_size && 18790Sstevel@tonic-gate hibound >= as->a_lastgap->s_base) 18800Sstevel@tonic-gate hibound = as->a_lastgap->s_base; 18810Sstevel@tonic-gate 18820Sstevel@tonic-gate hseg = as_findseg(as, hibound, 1); 18830Sstevel@tonic-gate if (hseg->s_base + hseg->s_size < hibound) { 18840Sstevel@tonic-gate lseg = hseg; 18850Sstevel@tonic-gate hseg = NULL; 18860Sstevel@tonic-gate } else { 18870Sstevel@tonic-gate lseg = AS_SEGPREV(as, hseg); 18880Sstevel@tonic-gate } 18890Sstevel@tonic-gate } 18900Sstevel@tonic-gate 18910Sstevel@tonic-gate for (;;) { 18920Sstevel@tonic-gate /* 18930Sstevel@tonic-gate * Set lo and hi to the hole's boundaries. (We should really 18940Sstevel@tonic-gate * use MAXADDR in place of hibound in the expression below, 18950Sstevel@tonic-gate * but can't express it easily; using hibound in its place is 18960Sstevel@tonic-gate * harmless.) 18970Sstevel@tonic-gate */ 18980Sstevel@tonic-gate lo = (lseg == NULL) ? 0 : lseg->s_base + lseg->s_size; 18990Sstevel@tonic-gate hi = (hseg == NULL) ? hibound : hseg->s_base; 19000Sstevel@tonic-gate /* 19010Sstevel@tonic-gate * If the iteration has moved past the interval from lobound 19020Sstevel@tonic-gate * to hibound it's pointless to continue. 19030Sstevel@tonic-gate */ 19040Sstevel@tonic-gate if ((forward && lo > hibound) || (!forward && hi < lobound)) 19050Sstevel@tonic-gate break; 19060Sstevel@tonic-gate else if (lo > hibound || hi < lobound) 19070Sstevel@tonic-gate goto cont; 19080Sstevel@tonic-gate /* 19090Sstevel@tonic-gate * Candidate hole lies at least partially within the allowable 19100Sstevel@tonic-gate * range. Restrict it to fall completely within that range, 19110Sstevel@tonic-gate * i.e., to [max(lo, lobound), min(hi, hibound)]. 19120Sstevel@tonic-gate */ 19130Sstevel@tonic-gate if (lo < lobound) 19140Sstevel@tonic-gate lo = lobound; 19150Sstevel@tonic-gate if (hi > hibound) 19160Sstevel@tonic-gate hi = hibound; 19170Sstevel@tonic-gate /* 19180Sstevel@tonic-gate * Verify that the candidate hole is big enough and meets 19190Sstevel@tonic-gate * hardware constraints. 19200Sstevel@tonic-gate */ 19210Sstevel@tonic-gate *basep = lo; 19220Sstevel@tonic-gate *lenp = hi - lo; 19230Sstevel@tonic-gate if (valid_va_range(basep, lenp, minlen, 19240Sstevel@tonic-gate forward ? AH_LO : AH_HI) && 19250Sstevel@tonic-gate ((flags & AH_CONTAIN) == 0 || 19260Sstevel@tonic-gate (*basep <= addr && *basep + *lenp > addr))) { 19270Sstevel@tonic-gate if (!forward) 19280Sstevel@tonic-gate as->a_lastgap = hseg; 19290Sstevel@tonic-gate if (hseg != NULL) 19300Sstevel@tonic-gate as->a_lastgaphl = hseg; 19310Sstevel@tonic-gate else 19320Sstevel@tonic-gate as->a_lastgaphl = lseg; 19330Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 19340Sstevel@tonic-gate return (0); 19350Sstevel@tonic-gate } 19360Sstevel@tonic-gate cont: 19370Sstevel@tonic-gate /* 19380Sstevel@tonic-gate * Move to the next hole. 19390Sstevel@tonic-gate */ 19400Sstevel@tonic-gate if (forward) { 19410Sstevel@tonic-gate lseg = hseg; 19420Sstevel@tonic-gate if (lseg == NULL) 19430Sstevel@tonic-gate break; 19440Sstevel@tonic-gate hseg = AS_SEGNEXT(as, hseg); 19450Sstevel@tonic-gate } else { 19460Sstevel@tonic-gate hseg = lseg; 19470Sstevel@tonic-gate if (hseg == NULL) 19480Sstevel@tonic-gate break; 19490Sstevel@tonic-gate lseg = AS_SEGPREV(as, lseg); 19500Sstevel@tonic-gate } 19510Sstevel@tonic-gate } 19520Sstevel@tonic-gate *basep = save_base; 19530Sstevel@tonic-gate *lenp = save_len; 19540Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 19550Sstevel@tonic-gate return (-1); 19560Sstevel@tonic-gate } 19570Sstevel@tonic-gate 19580Sstevel@tonic-gate /* 19590Sstevel@tonic-gate * Return the next range within [base, base + len) that is backed 19600Sstevel@tonic-gate * with "real memory". Skip holes and non-seg_vn segments. 19610Sstevel@tonic-gate * We're lazy and only return one segment at a time. 19620Sstevel@tonic-gate */ 19630Sstevel@tonic-gate int 19640Sstevel@tonic-gate as_memory(struct as *as, caddr_t *basep, size_t *lenp) 19650Sstevel@tonic-gate { 19660Sstevel@tonic-gate extern struct seg_ops segspt_shmops; /* needs a header file */ 19670Sstevel@tonic-gate struct seg *seg; 19680Sstevel@tonic-gate caddr_t addr, eaddr; 19690Sstevel@tonic-gate caddr_t segend; 19700Sstevel@tonic-gate 19710Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 19720Sstevel@tonic-gate 19730Sstevel@tonic-gate addr = *basep; 19740Sstevel@tonic-gate eaddr = addr + *lenp; 19750Sstevel@tonic-gate 19760Sstevel@tonic-gate seg = as_findseg(as, addr, 0); 19770Sstevel@tonic-gate if (seg != NULL) 19780Sstevel@tonic-gate addr = MAX(seg->s_base, addr); 19790Sstevel@tonic-gate 19800Sstevel@tonic-gate for (;;) { 19810Sstevel@tonic-gate if (seg == NULL || addr >= eaddr || eaddr <= seg->s_base) { 19820Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 19830Sstevel@tonic-gate return (EINVAL); 19840Sstevel@tonic-gate } 19850Sstevel@tonic-gate 19860Sstevel@tonic-gate if (seg->s_ops == &segvn_ops) { 19870Sstevel@tonic-gate segend = seg->s_base + seg->s_size; 19880Sstevel@tonic-gate break; 19890Sstevel@tonic-gate } 19900Sstevel@tonic-gate 19910Sstevel@tonic-gate /* 19920Sstevel@tonic-gate * We do ISM by looking into the private data 19930Sstevel@tonic-gate * to determine the real size of the segment. 19940Sstevel@tonic-gate */ 19950Sstevel@tonic-gate if (seg->s_ops == &segspt_shmops) { 19960Sstevel@tonic-gate segend = seg->s_base + spt_realsize(seg); 19970Sstevel@tonic-gate if (addr < segend) 19980Sstevel@tonic-gate break; 19990Sstevel@tonic-gate } 20000Sstevel@tonic-gate 20010Sstevel@tonic-gate seg = AS_SEGNEXT(as, seg); 20020Sstevel@tonic-gate 20030Sstevel@tonic-gate if (seg != NULL) 20040Sstevel@tonic-gate addr = seg->s_base; 20050Sstevel@tonic-gate } 20060Sstevel@tonic-gate 20070Sstevel@tonic-gate *basep = addr; 20080Sstevel@tonic-gate 20090Sstevel@tonic-gate if (segend > eaddr) 20100Sstevel@tonic-gate *lenp = eaddr - addr; 20110Sstevel@tonic-gate else 20120Sstevel@tonic-gate *lenp = segend - addr; 20130Sstevel@tonic-gate 20140Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 20150Sstevel@tonic-gate return (0); 20160Sstevel@tonic-gate } 20170Sstevel@tonic-gate 20180Sstevel@tonic-gate /* 20190Sstevel@tonic-gate * Swap the pages associated with the address space as out to 20200Sstevel@tonic-gate * secondary storage, returning the number of bytes actually 20210Sstevel@tonic-gate * swapped. 20220Sstevel@tonic-gate * 20230Sstevel@tonic-gate * The value returned is intended to correlate well with the process's 20240Sstevel@tonic-gate * memory requirements. Its usefulness for this purpose depends on 20250Sstevel@tonic-gate * how well the segment-level routines do at returning accurate 20260Sstevel@tonic-gate * information. 20270Sstevel@tonic-gate */ 20280Sstevel@tonic-gate size_t 20290Sstevel@tonic-gate as_swapout(struct as *as) 20300Sstevel@tonic-gate { 20310Sstevel@tonic-gate struct seg *seg; 20320Sstevel@tonic-gate size_t swpcnt = 0; 20330Sstevel@tonic-gate 20340Sstevel@tonic-gate /* 20350Sstevel@tonic-gate * Kernel-only processes have given up their address 20360Sstevel@tonic-gate * spaces. Of course, we shouldn't be attempting to 20370Sstevel@tonic-gate * swap out such processes in the first place... 20380Sstevel@tonic-gate */ 20390Sstevel@tonic-gate if (as == NULL) 20400Sstevel@tonic-gate return (0); 20410Sstevel@tonic-gate 20420Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 20430Sstevel@tonic-gate 20440Sstevel@tonic-gate /* Prevent XHATs from attaching */ 20450Sstevel@tonic-gate mutex_enter(&as->a_contents); 20460Sstevel@tonic-gate AS_SETBUSY(as); 20470Sstevel@tonic-gate mutex_exit(&as->a_contents); 20480Sstevel@tonic-gate 20490Sstevel@tonic-gate 20500Sstevel@tonic-gate /* 20510Sstevel@tonic-gate * Free all mapping resources associated with the address 20520Sstevel@tonic-gate * space. The segment-level swapout routines capitalize 20530Sstevel@tonic-gate * on this unmapping by scavanging pages that have become 20540Sstevel@tonic-gate * unmapped here. 20550Sstevel@tonic-gate */ 20560Sstevel@tonic-gate hat_swapout(as->a_hat); 20570Sstevel@tonic-gate if (as->a_xhat != NULL) 20580Sstevel@tonic-gate xhat_swapout_all(as); 20590Sstevel@tonic-gate 20600Sstevel@tonic-gate mutex_enter(&as->a_contents); 20610Sstevel@tonic-gate AS_CLRBUSY(as); 20620Sstevel@tonic-gate mutex_exit(&as->a_contents); 20630Sstevel@tonic-gate 20640Sstevel@tonic-gate /* 20650Sstevel@tonic-gate * Call the swapout routines of all segments in the address 20660Sstevel@tonic-gate * space to do the actual work, accumulating the amount of 20670Sstevel@tonic-gate * space reclaimed. 20680Sstevel@tonic-gate */ 20690Sstevel@tonic-gate for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) { 20700Sstevel@tonic-gate struct seg_ops *ov = seg->s_ops; 20710Sstevel@tonic-gate 20720Sstevel@tonic-gate /* 20730Sstevel@tonic-gate * We have to check to see if the seg has 20740Sstevel@tonic-gate * an ops vector because the seg may have 20750Sstevel@tonic-gate * been in the middle of being set up when 20760Sstevel@tonic-gate * the process was picked for swapout. 20770Sstevel@tonic-gate */ 20780Sstevel@tonic-gate if ((ov != NULL) && (ov->swapout != NULL)) 20790Sstevel@tonic-gate swpcnt += SEGOP_SWAPOUT(seg); 20800Sstevel@tonic-gate } 20810Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 20820Sstevel@tonic-gate return (swpcnt); 20830Sstevel@tonic-gate } 20840Sstevel@tonic-gate 20850Sstevel@tonic-gate /* 20860Sstevel@tonic-gate * Determine whether data from the mappings in interval [addr, addr + size) 20870Sstevel@tonic-gate * are in the primary memory (core) cache. 20880Sstevel@tonic-gate */ 20890Sstevel@tonic-gate int 20900Sstevel@tonic-gate as_incore(struct as *as, caddr_t addr, 20910Sstevel@tonic-gate size_t size, char *vec, size_t *sizep) 20920Sstevel@tonic-gate { 20930Sstevel@tonic-gate struct seg *seg; 20940Sstevel@tonic-gate size_t ssize; 20950Sstevel@tonic-gate caddr_t raddr; /* rounded down addr */ 20960Sstevel@tonic-gate size_t rsize; /* rounded up size */ 20970Sstevel@tonic-gate size_t isize; /* iteration size */ 20980Sstevel@tonic-gate int error = 0; /* result, assume success */ 20990Sstevel@tonic-gate 21000Sstevel@tonic-gate *sizep = 0; 21010Sstevel@tonic-gate raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK); 21020Sstevel@tonic-gate rsize = ((((size_t)addr + size) + PAGEOFFSET) & PAGEMASK) - 21030Sstevel@tonic-gate (size_t)raddr; 21040Sstevel@tonic-gate 21050Sstevel@tonic-gate if (raddr + rsize < raddr) /* check for wraparound */ 21060Sstevel@tonic-gate return (ENOMEM); 21070Sstevel@tonic-gate 21080Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 21090Sstevel@tonic-gate seg = as_segat(as, raddr); 21100Sstevel@tonic-gate if (seg == NULL) { 21110Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 21120Sstevel@tonic-gate return (-1); 21130Sstevel@tonic-gate } 21140Sstevel@tonic-gate 21150Sstevel@tonic-gate for (; rsize != 0; rsize -= ssize, raddr += ssize) { 21160Sstevel@tonic-gate if (raddr >= seg->s_base + seg->s_size) { 21170Sstevel@tonic-gate seg = AS_SEGNEXT(as, seg); 21180Sstevel@tonic-gate if (seg == NULL || raddr != seg->s_base) { 21190Sstevel@tonic-gate error = -1; 21200Sstevel@tonic-gate break; 21210Sstevel@tonic-gate } 21220Sstevel@tonic-gate } 21230Sstevel@tonic-gate if ((raddr + rsize) > (seg->s_base + seg->s_size)) 21240Sstevel@tonic-gate ssize = seg->s_base + seg->s_size - raddr; 21250Sstevel@tonic-gate else 21260Sstevel@tonic-gate ssize = rsize; 21270Sstevel@tonic-gate *sizep += isize = SEGOP_INCORE(seg, raddr, ssize, vec); 21280Sstevel@tonic-gate if (isize != ssize) { 21290Sstevel@tonic-gate error = -1; 21300Sstevel@tonic-gate break; 21310Sstevel@tonic-gate } 21320Sstevel@tonic-gate vec += btopr(ssize); 21330Sstevel@tonic-gate } 21340Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 21350Sstevel@tonic-gate return (error); 21360Sstevel@tonic-gate } 21370Sstevel@tonic-gate 21380Sstevel@tonic-gate static void 21390Sstevel@tonic-gate as_segunlock(struct seg *seg, caddr_t addr, int attr, 21400Sstevel@tonic-gate ulong_t *bitmap, size_t position, size_t npages) 21410Sstevel@tonic-gate { 21420Sstevel@tonic-gate caddr_t range_start; 21430Sstevel@tonic-gate size_t pos1 = position; 21440Sstevel@tonic-gate size_t pos2; 21450Sstevel@tonic-gate size_t size; 21460Sstevel@tonic-gate size_t end_pos = npages + position; 21470Sstevel@tonic-gate 21480Sstevel@tonic-gate while (bt_range(bitmap, &pos1, &pos2, end_pos)) { 21490Sstevel@tonic-gate size = ptob((pos2 - pos1)); 21500Sstevel@tonic-gate range_start = (caddr_t)((uintptr_t)addr + 21510Sstevel@tonic-gate ptob(pos1 - position)); 21520Sstevel@tonic-gate 21530Sstevel@tonic-gate (void) SEGOP_LOCKOP(seg, range_start, size, attr, MC_UNLOCK, 21540Sstevel@tonic-gate (ulong_t *)NULL, (size_t)NULL); 21550Sstevel@tonic-gate pos1 = pos2; 21560Sstevel@tonic-gate } 21570Sstevel@tonic-gate } 21580Sstevel@tonic-gate 21590Sstevel@tonic-gate static void 21600Sstevel@tonic-gate as_unlockerr(struct as *as, int attr, ulong_t *mlock_map, 21610Sstevel@tonic-gate caddr_t raddr, size_t rsize) 21620Sstevel@tonic-gate { 21630Sstevel@tonic-gate struct seg *seg = as_segat(as, raddr); 21640Sstevel@tonic-gate size_t ssize; 21650Sstevel@tonic-gate 21660Sstevel@tonic-gate while (rsize != 0) { 21670Sstevel@tonic-gate if (raddr >= seg->s_base + seg->s_size) 21680Sstevel@tonic-gate seg = AS_SEGNEXT(as, seg); 21690Sstevel@tonic-gate 21700Sstevel@tonic-gate if ((raddr + rsize) > (seg->s_base + seg->s_size)) 21710Sstevel@tonic-gate ssize = seg->s_base + seg->s_size - raddr; 21720Sstevel@tonic-gate else 21730Sstevel@tonic-gate ssize = rsize; 21740Sstevel@tonic-gate 21750Sstevel@tonic-gate as_segunlock(seg, raddr, attr, mlock_map, 0, btopr(ssize)); 21760Sstevel@tonic-gate 21770Sstevel@tonic-gate rsize -= ssize; 21780Sstevel@tonic-gate raddr += ssize; 21790Sstevel@tonic-gate } 21800Sstevel@tonic-gate } 21810Sstevel@tonic-gate 21820Sstevel@tonic-gate /* 21830Sstevel@tonic-gate * Cache control operations over the interval [addr, addr + size) in 21840Sstevel@tonic-gate * address space "as". 21850Sstevel@tonic-gate */ 21860Sstevel@tonic-gate /*ARGSUSED*/ 21870Sstevel@tonic-gate int 21880Sstevel@tonic-gate as_ctl(struct as *as, caddr_t addr, size_t size, int func, int attr, 21890Sstevel@tonic-gate uintptr_t arg, ulong_t *lock_map, size_t pos) 21900Sstevel@tonic-gate { 21910Sstevel@tonic-gate struct seg *seg; /* working segment */ 21920Sstevel@tonic-gate caddr_t raddr; /* rounded down addr */ 21930Sstevel@tonic-gate caddr_t initraddr; /* saved initial rounded down addr */ 21940Sstevel@tonic-gate size_t rsize; /* rounded up size */ 21950Sstevel@tonic-gate size_t initrsize; /* saved initial rounded up size */ 21960Sstevel@tonic-gate size_t ssize; /* size of seg */ 21970Sstevel@tonic-gate int error = 0; /* result */ 21980Sstevel@tonic-gate size_t mlock_size; /* size of bitmap */ 21990Sstevel@tonic-gate ulong_t *mlock_map; /* pointer to bitmap used */ 22000Sstevel@tonic-gate /* to represent the locked */ 22010Sstevel@tonic-gate /* pages. */ 22020Sstevel@tonic-gate retry: 22030Sstevel@tonic-gate if (error == IE_RETRY) 22040Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER); 22050Sstevel@tonic-gate else 22060Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 22070Sstevel@tonic-gate 22080Sstevel@tonic-gate /* 22090Sstevel@tonic-gate * If these are address space lock/unlock operations, loop over 22100Sstevel@tonic-gate * all segments in the address space, as appropriate. 22110Sstevel@tonic-gate */ 22120Sstevel@tonic-gate if (func == MC_LOCKAS) { 22130Sstevel@tonic-gate size_t npages, idx; 22140Sstevel@tonic-gate size_t rlen = 0; /* rounded as length */ 22150Sstevel@tonic-gate 22160Sstevel@tonic-gate idx = pos; 22170Sstevel@tonic-gate 22180Sstevel@tonic-gate if (arg & MCL_FUTURE) { 22190Sstevel@tonic-gate mutex_enter(&as->a_contents); 22200Sstevel@tonic-gate AS_SETPGLCK(as); 22210Sstevel@tonic-gate mutex_exit(&as->a_contents); 22220Sstevel@tonic-gate } 22230Sstevel@tonic-gate if ((arg & MCL_CURRENT) == 0) { 22240Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 22250Sstevel@tonic-gate return (0); 22260Sstevel@tonic-gate } 22270Sstevel@tonic-gate 22280Sstevel@tonic-gate seg = AS_SEGFIRST(as); 22290Sstevel@tonic-gate if (seg == NULL) { 22300Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 22310Sstevel@tonic-gate return (0); 22320Sstevel@tonic-gate } 22330Sstevel@tonic-gate 22340Sstevel@tonic-gate do { 22350Sstevel@tonic-gate raddr = (caddr_t)((uintptr_t)seg->s_base & 22360Sstevel@tonic-gate (uintptr_t)PAGEMASK); 22370Sstevel@tonic-gate rlen += (((uintptr_t)(seg->s_base + seg->s_size) + 22380Sstevel@tonic-gate PAGEOFFSET) & PAGEMASK) - (uintptr_t)raddr; 22390Sstevel@tonic-gate } while ((seg = AS_SEGNEXT(as, seg)) != NULL); 22400Sstevel@tonic-gate 22410Sstevel@tonic-gate mlock_size = BT_BITOUL(btopr(rlen)); 22420Sstevel@tonic-gate if ((mlock_map = (ulong_t *)kmem_zalloc(mlock_size * 22430Sstevel@tonic-gate sizeof (ulong_t), KM_NOSLEEP)) == NULL) { 22440Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 22450Sstevel@tonic-gate return (EAGAIN); 22460Sstevel@tonic-gate } 22470Sstevel@tonic-gate 22480Sstevel@tonic-gate for (seg = AS_SEGFIRST(as); seg; seg = AS_SEGNEXT(as, seg)) { 22490Sstevel@tonic-gate error = SEGOP_LOCKOP(seg, seg->s_base, 22500Sstevel@tonic-gate seg->s_size, attr, MC_LOCK, mlock_map, pos); 22510Sstevel@tonic-gate if (error != 0) 22520Sstevel@tonic-gate break; 22530Sstevel@tonic-gate pos += seg_pages(seg); 22540Sstevel@tonic-gate } 22550Sstevel@tonic-gate 22560Sstevel@tonic-gate if (error) { 22570Sstevel@tonic-gate for (seg = AS_SEGFIRST(as); seg != NULL; 22580Sstevel@tonic-gate seg = AS_SEGNEXT(as, seg)) { 22590Sstevel@tonic-gate 22600Sstevel@tonic-gate raddr = (caddr_t)((uintptr_t)seg->s_base & 22610Sstevel@tonic-gate (uintptr_t)PAGEMASK); 22620Sstevel@tonic-gate npages = seg_pages(seg); 22630Sstevel@tonic-gate as_segunlock(seg, raddr, attr, mlock_map, 22640Sstevel@tonic-gate idx, npages); 22650Sstevel@tonic-gate idx += npages; 22660Sstevel@tonic-gate } 22670Sstevel@tonic-gate } 22680Sstevel@tonic-gate 22690Sstevel@tonic-gate kmem_free(mlock_map, mlock_size * sizeof (ulong_t)); 22700Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 22710Sstevel@tonic-gate goto lockerr; 22720Sstevel@tonic-gate } else if (func == MC_UNLOCKAS) { 22730Sstevel@tonic-gate mutex_enter(&as->a_contents); 22740Sstevel@tonic-gate AS_CLRPGLCK(as); 22750Sstevel@tonic-gate mutex_exit(&as->a_contents); 22760Sstevel@tonic-gate 22770Sstevel@tonic-gate for (seg = AS_SEGFIRST(as); seg; seg = AS_SEGNEXT(as, seg)) { 22780Sstevel@tonic-gate error = SEGOP_LOCKOP(seg, seg->s_base, 22790Sstevel@tonic-gate seg->s_size, attr, MC_UNLOCK, NULL, 0); 22800Sstevel@tonic-gate if (error != 0) 22810Sstevel@tonic-gate break; 22820Sstevel@tonic-gate } 22830Sstevel@tonic-gate 22840Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 22850Sstevel@tonic-gate goto lockerr; 22860Sstevel@tonic-gate } 22870Sstevel@tonic-gate 22880Sstevel@tonic-gate /* 22890Sstevel@tonic-gate * Normalize addresses and sizes. 22900Sstevel@tonic-gate */ 22910Sstevel@tonic-gate initraddr = raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK); 22920Sstevel@tonic-gate initrsize = rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) - 22930Sstevel@tonic-gate (size_t)raddr; 22940Sstevel@tonic-gate 22950Sstevel@tonic-gate if (raddr + rsize < raddr) { /* check for wraparound */ 22960Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 22970Sstevel@tonic-gate return (ENOMEM); 22980Sstevel@tonic-gate } 22990Sstevel@tonic-gate 23000Sstevel@tonic-gate /* 23010Sstevel@tonic-gate * Get initial segment. 23020Sstevel@tonic-gate */ 23030Sstevel@tonic-gate if ((seg = as_segat(as, raddr)) == NULL) { 23040Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 23050Sstevel@tonic-gate return (ENOMEM); 23060Sstevel@tonic-gate } 23070Sstevel@tonic-gate 23080Sstevel@tonic-gate if (func == MC_LOCK) { 23090Sstevel@tonic-gate mlock_size = BT_BITOUL(btopr(rsize)); 23100Sstevel@tonic-gate if ((mlock_map = (ulong_t *)kmem_zalloc(mlock_size * 23110Sstevel@tonic-gate sizeof (ulong_t), KM_NOSLEEP)) == NULL) { 23120Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 23130Sstevel@tonic-gate return (EAGAIN); 23140Sstevel@tonic-gate } 23150Sstevel@tonic-gate } 23160Sstevel@tonic-gate 23170Sstevel@tonic-gate /* 23180Sstevel@tonic-gate * Loop over all segments. If a hole in the address range is 23190Sstevel@tonic-gate * discovered, then fail. For each segment, perform the appropriate 23200Sstevel@tonic-gate * control operation. 23210Sstevel@tonic-gate */ 23220Sstevel@tonic-gate while (rsize != 0) { 23230Sstevel@tonic-gate 23240Sstevel@tonic-gate /* 23250Sstevel@tonic-gate * Make sure there's no hole, calculate the portion 23260Sstevel@tonic-gate * of the next segment to be operated over. 23270Sstevel@tonic-gate */ 23280Sstevel@tonic-gate if (raddr >= seg->s_base + seg->s_size) { 23290Sstevel@tonic-gate seg = AS_SEGNEXT(as, seg); 23300Sstevel@tonic-gate if (seg == NULL || raddr != seg->s_base) { 23310Sstevel@tonic-gate if (func == MC_LOCK) { 23320Sstevel@tonic-gate as_unlockerr(as, attr, mlock_map, 23330Sstevel@tonic-gate initraddr, initrsize - rsize); 23340Sstevel@tonic-gate kmem_free(mlock_map, 23350Sstevel@tonic-gate mlock_size * sizeof (ulong_t)); 23360Sstevel@tonic-gate } 23370Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 23380Sstevel@tonic-gate return (ENOMEM); 23390Sstevel@tonic-gate } 23400Sstevel@tonic-gate } 23410Sstevel@tonic-gate if ((raddr + rsize) > (seg->s_base + seg->s_size)) 23420Sstevel@tonic-gate ssize = seg->s_base + seg->s_size - raddr; 23430Sstevel@tonic-gate else 23440Sstevel@tonic-gate ssize = rsize; 23450Sstevel@tonic-gate 23460Sstevel@tonic-gate /* 23470Sstevel@tonic-gate * Dispatch on specific function. 23480Sstevel@tonic-gate */ 23490Sstevel@tonic-gate switch (func) { 23500Sstevel@tonic-gate 23510Sstevel@tonic-gate /* 23520Sstevel@tonic-gate * Synchronize cached data from mappings with backing 23530Sstevel@tonic-gate * objects. 23540Sstevel@tonic-gate */ 23550Sstevel@tonic-gate case MC_SYNC: 23560Sstevel@tonic-gate if (error = SEGOP_SYNC(seg, raddr, ssize, 23570Sstevel@tonic-gate attr, (uint_t)arg)) { 23580Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 23590Sstevel@tonic-gate return (error); 23600Sstevel@tonic-gate } 23610Sstevel@tonic-gate break; 23620Sstevel@tonic-gate 23630Sstevel@tonic-gate /* 23640Sstevel@tonic-gate * Lock pages in memory. 23650Sstevel@tonic-gate */ 23660Sstevel@tonic-gate case MC_LOCK: 23670Sstevel@tonic-gate if (error = SEGOP_LOCKOP(seg, raddr, ssize, 23680Sstevel@tonic-gate attr, func, mlock_map, pos)) { 23690Sstevel@tonic-gate as_unlockerr(as, attr, mlock_map, initraddr, 23700Sstevel@tonic-gate initrsize - rsize + ssize); 23710Sstevel@tonic-gate kmem_free(mlock_map, mlock_size * 23720Sstevel@tonic-gate sizeof (ulong_t)); 23730Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 23740Sstevel@tonic-gate goto lockerr; 23750Sstevel@tonic-gate } 23760Sstevel@tonic-gate break; 23770Sstevel@tonic-gate 23780Sstevel@tonic-gate /* 23790Sstevel@tonic-gate * Unlock mapped pages. 23800Sstevel@tonic-gate */ 23810Sstevel@tonic-gate case MC_UNLOCK: 23820Sstevel@tonic-gate (void) SEGOP_LOCKOP(seg, raddr, ssize, attr, func, 23830Sstevel@tonic-gate (ulong_t *)NULL, (size_t)NULL); 23840Sstevel@tonic-gate break; 23850Sstevel@tonic-gate 23860Sstevel@tonic-gate /* 23870Sstevel@tonic-gate * Store VM advise for mapped pages in segment layer. 23880Sstevel@tonic-gate */ 23890Sstevel@tonic-gate case MC_ADVISE: 23900Sstevel@tonic-gate error = SEGOP_ADVISE(seg, raddr, ssize, (uint_t)arg); 23910Sstevel@tonic-gate 23920Sstevel@tonic-gate /* 23930Sstevel@tonic-gate * Check for regular errors and special retry error 23940Sstevel@tonic-gate */ 23950Sstevel@tonic-gate if (error) { 23960Sstevel@tonic-gate if (error == IE_RETRY) { 23970Sstevel@tonic-gate /* 23980Sstevel@tonic-gate * Need to acquire writers lock, so 23990Sstevel@tonic-gate * have to drop readers lock and start 24000Sstevel@tonic-gate * all over again 24010Sstevel@tonic-gate */ 24020Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 24030Sstevel@tonic-gate goto retry; 24040Sstevel@tonic-gate } else if (error == IE_REATTACH) { 24050Sstevel@tonic-gate /* 24060Sstevel@tonic-gate * Find segment for current address 24070Sstevel@tonic-gate * because current segment just got 24080Sstevel@tonic-gate * split or concatenated 24090Sstevel@tonic-gate */ 24100Sstevel@tonic-gate seg = as_segat(as, raddr); 24110Sstevel@tonic-gate if (seg == NULL) { 24120Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 24130Sstevel@tonic-gate return (ENOMEM); 24140Sstevel@tonic-gate } 24150Sstevel@tonic-gate } else { 24160Sstevel@tonic-gate /* 24170Sstevel@tonic-gate * Regular error 24180Sstevel@tonic-gate */ 24190Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 24200Sstevel@tonic-gate return (error); 24210Sstevel@tonic-gate } 24220Sstevel@tonic-gate } 24230Sstevel@tonic-gate break; 24240Sstevel@tonic-gate 24250Sstevel@tonic-gate /* 24260Sstevel@tonic-gate * Can't happen. 24270Sstevel@tonic-gate */ 24280Sstevel@tonic-gate default: 24290Sstevel@tonic-gate panic("as_ctl: bad operation %d", func); 24300Sstevel@tonic-gate /*NOTREACHED*/ 24310Sstevel@tonic-gate } 24320Sstevel@tonic-gate 24330Sstevel@tonic-gate rsize -= ssize; 24340Sstevel@tonic-gate raddr += ssize; 24350Sstevel@tonic-gate } 24360Sstevel@tonic-gate 24370Sstevel@tonic-gate if (func == MC_LOCK) 24380Sstevel@tonic-gate kmem_free(mlock_map, mlock_size * sizeof (ulong_t)); 24390Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 24400Sstevel@tonic-gate return (0); 24410Sstevel@tonic-gate lockerr: 24420Sstevel@tonic-gate 24430Sstevel@tonic-gate /* 24440Sstevel@tonic-gate * If the lower levels returned EDEADLK for a segment lockop, 24450Sstevel@tonic-gate * it means that we should retry the operation. Let's wait 24460Sstevel@tonic-gate * a bit also to let the deadlock causing condition clear. 24470Sstevel@tonic-gate * This is part of a gross hack to work around a design flaw 24480Sstevel@tonic-gate * in the ufs/sds logging code and should go away when the 24490Sstevel@tonic-gate * logging code is re-designed to fix the problem. See bug 24500Sstevel@tonic-gate * 4125102 for details of the problem. 24510Sstevel@tonic-gate */ 24520Sstevel@tonic-gate if (error == EDEADLK) { 24530Sstevel@tonic-gate delay(deadlk_wait); 24540Sstevel@tonic-gate error = 0; 24550Sstevel@tonic-gate goto retry; 24560Sstevel@tonic-gate } 24570Sstevel@tonic-gate return (error); 24580Sstevel@tonic-gate } 24590Sstevel@tonic-gate 24600Sstevel@tonic-gate /* 24610Sstevel@tonic-gate * Special code for exec to move the stack segment from its interim 24620Sstevel@tonic-gate * place in the old address to the right place in the new address space. 24630Sstevel@tonic-gate */ 24640Sstevel@tonic-gate /*ARGSUSED*/ 24650Sstevel@tonic-gate int 24660Sstevel@tonic-gate as_exec(struct as *oas, caddr_t ostka, size_t stksz, 24670Sstevel@tonic-gate struct as *nas, caddr_t nstka, uint_t hatflag) 24680Sstevel@tonic-gate { 24690Sstevel@tonic-gate struct seg *stkseg; 24700Sstevel@tonic-gate 24710Sstevel@tonic-gate AS_LOCK_ENTER(oas, &oas->a_lock, RW_WRITER); 24720Sstevel@tonic-gate stkseg = as_segat(oas, ostka); 24730Sstevel@tonic-gate stkseg = as_removeseg(oas, stkseg); 24740Sstevel@tonic-gate ASSERT(stkseg != NULL); 24750Sstevel@tonic-gate ASSERT(stkseg->s_base == ostka && stkseg->s_size == stksz); 24760Sstevel@tonic-gate stkseg->s_as = nas; 24770Sstevel@tonic-gate stkseg->s_base = nstka; 24780Sstevel@tonic-gate 24790Sstevel@tonic-gate /* 24800Sstevel@tonic-gate * It's ok to lock the address space we are about to exec to. 24810Sstevel@tonic-gate */ 24820Sstevel@tonic-gate AS_LOCK_ENTER(nas, &nas->a_lock, RW_WRITER); 24830Sstevel@tonic-gate ASSERT(avl_numnodes(&nas->a_wpage) == 0); 24840Sstevel@tonic-gate nas->a_size += stkseg->s_size; 24850Sstevel@tonic-gate oas->a_size -= stkseg->s_size; 24860Sstevel@tonic-gate (void) as_addseg(nas, stkseg); 24870Sstevel@tonic-gate AS_LOCK_EXIT(nas, &nas->a_lock); 24880Sstevel@tonic-gate AS_LOCK_EXIT(oas, &oas->a_lock); 24890Sstevel@tonic-gate return (0); 24900Sstevel@tonic-gate } 24910Sstevel@tonic-gate 24920Sstevel@tonic-gate static int 24930Sstevel@tonic-gate f_decode(faultcode_t fault_err) 24940Sstevel@tonic-gate { 24950Sstevel@tonic-gate int error = 0; 24960Sstevel@tonic-gate 24970Sstevel@tonic-gate switch (FC_CODE(fault_err)) { 24980Sstevel@tonic-gate case FC_OBJERR: 24990Sstevel@tonic-gate error = FC_ERRNO(fault_err); 25000Sstevel@tonic-gate break; 25010Sstevel@tonic-gate case FC_PROT: 25020Sstevel@tonic-gate error = EACCES; 25030Sstevel@tonic-gate break; 25040Sstevel@tonic-gate default: 25050Sstevel@tonic-gate error = EFAULT; 25060Sstevel@tonic-gate break; 25070Sstevel@tonic-gate } 25080Sstevel@tonic-gate return (error); 25090Sstevel@tonic-gate } 25100Sstevel@tonic-gate 25110Sstevel@tonic-gate /* 25120Sstevel@tonic-gate * lock pages in a given address space. Return shadow list. If 25130Sstevel@tonic-gate * the list is NULL, the MMU mapping is also locked. 25140Sstevel@tonic-gate */ 25150Sstevel@tonic-gate int 25160Sstevel@tonic-gate as_pagelock(struct as *as, struct page ***ppp, caddr_t addr, 25170Sstevel@tonic-gate size_t size, enum seg_rw rw) 25180Sstevel@tonic-gate { 25190Sstevel@tonic-gate size_t rsize; 25200Sstevel@tonic-gate caddr_t base; 25210Sstevel@tonic-gate caddr_t raddr; 25220Sstevel@tonic-gate faultcode_t fault_err; 25230Sstevel@tonic-gate struct seg *seg; 25240Sstevel@tonic-gate int res; 25250Sstevel@tonic-gate int prefaulted = 0; 25260Sstevel@tonic-gate 25270Sstevel@tonic-gate TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_AS_LOCK_START, 25280Sstevel@tonic-gate "as_pagelock_start: addr %p size %ld", addr, size); 25290Sstevel@tonic-gate 25300Sstevel@tonic-gate raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK); 25310Sstevel@tonic-gate rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) - 25320Sstevel@tonic-gate (size_t)raddr; 25330Sstevel@tonic-gate top: 25340Sstevel@tonic-gate /* 25350Sstevel@tonic-gate * if the request crosses two segments let 25360Sstevel@tonic-gate * as_fault handle it. 25370Sstevel@tonic-gate */ 25380Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 25390Sstevel@tonic-gate seg = as_findseg(as, addr, 0); 25400Sstevel@tonic-gate if ((seg == NULL) || ((base = seg->s_base) > addr) || 25410Sstevel@tonic-gate (addr + size) > base + seg->s_size) { 25420Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 25430Sstevel@tonic-gate goto slow; 25440Sstevel@tonic-gate } 25450Sstevel@tonic-gate 25460Sstevel@tonic-gate TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_SEG_LOCK_START, 25470Sstevel@tonic-gate "seg_lock_1_start: raddr %p rsize %ld", raddr, rsize); 25480Sstevel@tonic-gate 25490Sstevel@tonic-gate /* 25500Sstevel@tonic-gate * try to lock pages and pass back shadow list 25510Sstevel@tonic-gate */ 25520Sstevel@tonic-gate res = SEGOP_PAGELOCK(seg, raddr, rsize, ppp, L_PAGELOCK, rw); 25530Sstevel@tonic-gate 25540Sstevel@tonic-gate TRACE_0(TR_FAC_PHYSIO, TR_PHYSIO_SEG_LOCK_END, "seg_lock_1_end"); 25550Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 25560Sstevel@tonic-gate if (res == 0) { 25570Sstevel@tonic-gate return (0); 25580Sstevel@tonic-gate } else if (res == ENOTSUP || prefaulted) { 25590Sstevel@tonic-gate /* 25600Sstevel@tonic-gate * (1) segment driver doesn't support PAGELOCK fastpath, or 25610Sstevel@tonic-gate * (2) we've already tried fast path unsuccessfully after 25620Sstevel@tonic-gate * faulting in the addr range below; system might be 25630Sstevel@tonic-gate * thrashing or there may not be enough availrmem. 25640Sstevel@tonic-gate */ 25650Sstevel@tonic-gate goto slow; 25660Sstevel@tonic-gate } 25670Sstevel@tonic-gate 25680Sstevel@tonic-gate TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_AS_FAULT_START, 25690Sstevel@tonic-gate "as_fault_start: addr %p size %ld", addr, size); 25700Sstevel@tonic-gate 25710Sstevel@tonic-gate /* 25720Sstevel@tonic-gate * we might get here because of some COW fault or non 25730Sstevel@tonic-gate * existing page. Let as_fault deal with it. Just load 25740Sstevel@tonic-gate * the page, don't lock the MMU mapping. 25750Sstevel@tonic-gate */ 25760Sstevel@tonic-gate fault_err = as_fault(as->a_hat, as, addr, size, F_INVAL, rw); 25770Sstevel@tonic-gate if (fault_err != 0) { 25780Sstevel@tonic-gate return (f_decode(fault_err)); 25790Sstevel@tonic-gate } 25800Sstevel@tonic-gate 25810Sstevel@tonic-gate prefaulted = 1; 25820Sstevel@tonic-gate 25830Sstevel@tonic-gate /* 25840Sstevel@tonic-gate * try fast path again; since we've dropped a_lock, 25850Sstevel@tonic-gate * we need to try the dance from the start to see if 25860Sstevel@tonic-gate * the addr range is still valid. 25870Sstevel@tonic-gate */ 25880Sstevel@tonic-gate goto top; 25890Sstevel@tonic-gate slow: 25900Sstevel@tonic-gate /* 25910Sstevel@tonic-gate * load the page and lock the MMU mapping. 25920Sstevel@tonic-gate */ 25930Sstevel@tonic-gate fault_err = as_fault(as->a_hat, as, addr, size, F_SOFTLOCK, rw); 25940Sstevel@tonic-gate if (fault_err != 0) { 25950Sstevel@tonic-gate return (f_decode(fault_err)); 25960Sstevel@tonic-gate } 25970Sstevel@tonic-gate *ppp = NULL; 25980Sstevel@tonic-gate 25990Sstevel@tonic-gate TRACE_0(TR_FAC_PHYSIO, TR_PHYSIO_AS_LOCK_END, "as_pagelock_end"); 26000Sstevel@tonic-gate return (0); 26010Sstevel@tonic-gate } 26020Sstevel@tonic-gate 26030Sstevel@tonic-gate /* 26040Sstevel@tonic-gate * unlock pages in a given address range 26050Sstevel@tonic-gate */ 26060Sstevel@tonic-gate void 26070Sstevel@tonic-gate as_pageunlock(struct as *as, struct page **pp, caddr_t addr, size_t size, 26080Sstevel@tonic-gate enum seg_rw rw) 26090Sstevel@tonic-gate { 26100Sstevel@tonic-gate struct seg *seg; 26110Sstevel@tonic-gate size_t rsize; 26120Sstevel@tonic-gate caddr_t raddr; 26130Sstevel@tonic-gate 26140Sstevel@tonic-gate TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_AS_UNLOCK_START, 26150Sstevel@tonic-gate "as_pageunlock_start: addr %p size %ld", addr, size); 26160Sstevel@tonic-gate 26170Sstevel@tonic-gate /* 26180Sstevel@tonic-gate * if the shadow list is NULL, as_pagelock was 26190Sstevel@tonic-gate * falling back to as_fault 26200Sstevel@tonic-gate */ 26210Sstevel@tonic-gate if (pp == NULL) { 26220Sstevel@tonic-gate (void) as_fault(as->a_hat, as, addr, size, F_SOFTUNLOCK, rw); 26230Sstevel@tonic-gate return; 26240Sstevel@tonic-gate } 26250Sstevel@tonic-gate raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK); 26260Sstevel@tonic-gate rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) - 26270Sstevel@tonic-gate (size_t)raddr; 26280Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 26290Sstevel@tonic-gate seg = as_findseg(as, addr, 0); 26300Sstevel@tonic-gate ASSERT(seg); 26310Sstevel@tonic-gate TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_SEG_UNLOCK_START, 26320Sstevel@tonic-gate "seg_unlock_start: raddr %p rsize %ld", raddr, rsize); 26330Sstevel@tonic-gate SEGOP_PAGELOCK(seg, raddr, rsize, &pp, L_PAGEUNLOCK, rw); 26340Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 26350Sstevel@tonic-gate TRACE_0(TR_FAC_PHYSIO, TR_PHYSIO_AS_UNLOCK_END, "as_pageunlock_end"); 26360Sstevel@tonic-gate } 26370Sstevel@tonic-gate 26380Sstevel@tonic-gate /* 26390Sstevel@tonic-gate * reclaim cached pages in a given address range 26400Sstevel@tonic-gate */ 26410Sstevel@tonic-gate void 26420Sstevel@tonic-gate as_pagereclaim(struct as *as, struct page **pp, caddr_t addr, 26430Sstevel@tonic-gate size_t size, enum seg_rw rw) 26440Sstevel@tonic-gate { 26450Sstevel@tonic-gate struct seg *seg; 26460Sstevel@tonic-gate size_t rsize; 26470Sstevel@tonic-gate caddr_t raddr; 26480Sstevel@tonic-gate 26490Sstevel@tonic-gate ASSERT(AS_READ_HELD(as, &as->a_lock)); 26500Sstevel@tonic-gate ASSERT(pp != NULL); 26510Sstevel@tonic-gate 26520Sstevel@tonic-gate raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK); 26530Sstevel@tonic-gate rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) - 26540Sstevel@tonic-gate (size_t)raddr; 26550Sstevel@tonic-gate seg = as_findseg(as, addr, 0); 26560Sstevel@tonic-gate ASSERT(seg); 26570Sstevel@tonic-gate SEGOP_PAGELOCK(seg, raddr, rsize, &pp, L_PAGERECLAIM, rw); 26580Sstevel@tonic-gate } 26590Sstevel@tonic-gate 26600Sstevel@tonic-gate #define MAXPAGEFLIP 4 26610Sstevel@tonic-gate #define MAXPAGEFLIPSIZ MAXPAGEFLIP*PAGESIZE 26620Sstevel@tonic-gate 26630Sstevel@tonic-gate int 26640Sstevel@tonic-gate as_setpagesize(struct as *as, caddr_t addr, size_t size, uint_t szc, 26650Sstevel@tonic-gate boolean_t wait) 26660Sstevel@tonic-gate { 26670Sstevel@tonic-gate struct seg *seg; 26680Sstevel@tonic-gate size_t ssize; 26690Sstevel@tonic-gate caddr_t raddr; /* rounded down addr */ 26700Sstevel@tonic-gate size_t rsize; /* rounded up size */ 26710Sstevel@tonic-gate int error = 0; 26720Sstevel@tonic-gate size_t pgsz = page_get_pagesize(szc); 26730Sstevel@tonic-gate 26740Sstevel@tonic-gate setpgsz_top: 26750Sstevel@tonic-gate if (!IS_P2ALIGNED(addr, pgsz) || !IS_P2ALIGNED(size, pgsz)) { 26760Sstevel@tonic-gate return (EINVAL); 26770Sstevel@tonic-gate } 26780Sstevel@tonic-gate 26790Sstevel@tonic-gate raddr = addr; 26800Sstevel@tonic-gate rsize = size; 26810Sstevel@tonic-gate 26820Sstevel@tonic-gate if (raddr + rsize < raddr) /* check for wraparound */ 26830Sstevel@tonic-gate return (ENOMEM); 26840Sstevel@tonic-gate 26850Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER); 26860Sstevel@tonic-gate as_clearwatchprot(as, raddr, rsize); 26870Sstevel@tonic-gate seg = as_segat(as, raddr); 26880Sstevel@tonic-gate if (seg == NULL) { 26890Sstevel@tonic-gate as_setwatch(as); 26900Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 26910Sstevel@tonic-gate return (ENOMEM); 26920Sstevel@tonic-gate } 26930Sstevel@tonic-gate 26940Sstevel@tonic-gate for (; rsize != 0; rsize -= ssize, raddr += ssize) { 26950Sstevel@tonic-gate if (raddr >= seg->s_base + seg->s_size) { 26960Sstevel@tonic-gate seg = AS_SEGNEXT(as, seg); 26970Sstevel@tonic-gate if (seg == NULL || raddr != seg->s_base) { 26980Sstevel@tonic-gate error = ENOMEM; 26990Sstevel@tonic-gate break; 27000Sstevel@tonic-gate } 27010Sstevel@tonic-gate } 27020Sstevel@tonic-gate if ((raddr + rsize) > (seg->s_base + seg->s_size)) { 27030Sstevel@tonic-gate ssize = seg->s_base + seg->s_size - raddr; 27040Sstevel@tonic-gate } else { 27050Sstevel@tonic-gate ssize = rsize; 27060Sstevel@tonic-gate } 27070Sstevel@tonic-gate 27080Sstevel@tonic-gate error = SEGOP_SETPAGESIZE(seg, raddr, ssize, szc); 27090Sstevel@tonic-gate 27100Sstevel@tonic-gate if (error == IE_NOMEM) { 27110Sstevel@tonic-gate error = EAGAIN; 27120Sstevel@tonic-gate break; 27130Sstevel@tonic-gate } 27140Sstevel@tonic-gate 27150Sstevel@tonic-gate if (error == IE_RETRY) { 27160Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 27170Sstevel@tonic-gate goto setpgsz_top; 27180Sstevel@tonic-gate } 27190Sstevel@tonic-gate 27200Sstevel@tonic-gate if (error == ENOTSUP) { 27210Sstevel@tonic-gate error = EINVAL; 27220Sstevel@tonic-gate break; 27230Sstevel@tonic-gate } 27240Sstevel@tonic-gate 27250Sstevel@tonic-gate if (wait && (error == EAGAIN)) { 27260Sstevel@tonic-gate /* 27270Sstevel@tonic-gate * Memory is currently locked. It must be unlocked 27280Sstevel@tonic-gate * before this operation can succeed through a retry. 27290Sstevel@tonic-gate * The possible reasons for locked memory and 27300Sstevel@tonic-gate * corresponding strategies for unlocking are: 27310Sstevel@tonic-gate * (1) Normal I/O 27320Sstevel@tonic-gate * wait for a signal that the I/O operation 27330Sstevel@tonic-gate * has completed and the memory is unlocked. 27340Sstevel@tonic-gate * (2) Asynchronous I/O 27350Sstevel@tonic-gate * The aio subsystem does not unlock pages when 27360Sstevel@tonic-gate * the I/O is completed. Those pages are unlocked 27370Sstevel@tonic-gate * when the application calls aiowait/aioerror. 27380Sstevel@tonic-gate * So, to prevent blocking forever, cv_broadcast() 27390Sstevel@tonic-gate * is done to wake up aio_cleanup_thread. 27400Sstevel@tonic-gate * Subsequently, segvn_reclaim will be called, and 27410Sstevel@tonic-gate * that will do AS_CLRUNMAPWAIT() and wake us up. 27420Sstevel@tonic-gate * (3) Long term page locking: 27430Sstevel@tonic-gate * This is not relevant for as_setpagesize() 27440Sstevel@tonic-gate * because we cannot change the page size for 27450Sstevel@tonic-gate * driver memory. The attempt to do so will 27460Sstevel@tonic-gate * fail with a different error than EAGAIN so 27470Sstevel@tonic-gate * there's no need to trigger as callbacks like 27480Sstevel@tonic-gate * as_unmap, as_setprot or as_free would do. 27490Sstevel@tonic-gate */ 27500Sstevel@tonic-gate mutex_enter(&as->a_contents); 27510Sstevel@tonic-gate if (AS_ISUNMAPWAIT(as) == 0) { 27520Sstevel@tonic-gate cv_broadcast(&as->a_cv); 27530Sstevel@tonic-gate } 27540Sstevel@tonic-gate AS_SETUNMAPWAIT(as); 27550Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 27560Sstevel@tonic-gate while (AS_ISUNMAPWAIT(as)) { 27570Sstevel@tonic-gate cv_wait(&as->a_cv, &as->a_contents); 27580Sstevel@tonic-gate } 27590Sstevel@tonic-gate mutex_exit(&as->a_contents); 27600Sstevel@tonic-gate goto setpgsz_top; 27610Sstevel@tonic-gate } else if (error != 0) { 27620Sstevel@tonic-gate break; 27630Sstevel@tonic-gate } 27640Sstevel@tonic-gate } 27650Sstevel@tonic-gate as_setwatch(as); 27660Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 27670Sstevel@tonic-gate return (error); 27680Sstevel@tonic-gate } 27690Sstevel@tonic-gate 27700Sstevel@tonic-gate /* 27712991Ssusans * as_iset3_default_lpsize() just calls SEGOP_SETPAGESIZE() on all segments 27722991Ssusans * in its chunk where s_szc is less than the szc we want to set. 27732991Ssusans */ 27742991Ssusans static int 27752991Ssusans as_iset3_default_lpsize(struct as *as, caddr_t raddr, size_t rsize, uint_t szc, 27762991Ssusans int *retry) 27772991Ssusans { 27782991Ssusans struct seg *seg; 27792991Ssusans size_t ssize; 27802991Ssusans int error; 27812991Ssusans 27822991Ssusans seg = as_segat(as, raddr); 27832991Ssusans if (seg == NULL) { 27842991Ssusans panic("as_iset3_default_lpsize: no seg"); 27852991Ssusans } 27862991Ssusans 27872991Ssusans for (; rsize != 0; rsize -= ssize, raddr += ssize) { 27882991Ssusans if (raddr >= seg->s_base + seg->s_size) { 27892991Ssusans seg = AS_SEGNEXT(as, seg); 27902991Ssusans if (seg == NULL || raddr != seg->s_base) { 27912991Ssusans panic("as_iset3_default_lpsize: as changed"); 27922991Ssusans } 27932991Ssusans } 27942991Ssusans if ((raddr + rsize) > (seg->s_base + seg->s_size)) { 27952991Ssusans ssize = seg->s_base + seg->s_size - raddr; 27962991Ssusans } else { 27972991Ssusans ssize = rsize; 27982991Ssusans } 27992991Ssusans 28002991Ssusans if (szc > seg->s_szc) { 28012991Ssusans error = SEGOP_SETPAGESIZE(seg, raddr, ssize, szc); 28022991Ssusans /* Only retry on EINVAL segments that have no vnode. */ 28032991Ssusans if (error == EINVAL) { 28042991Ssusans vnode_t *vp = NULL; 28052991Ssusans if ((SEGOP_GETTYPE(seg, raddr) & MAP_SHARED) && 28062991Ssusans (SEGOP_GETVP(seg, raddr, &vp) != 0 || 28072991Ssusans vp == NULL)) { 28082991Ssusans *retry = 1; 28092991Ssusans } else { 28102991Ssusans *retry = 0; 28112991Ssusans } 28122991Ssusans } 28132991Ssusans if (error) { 28142991Ssusans return (error); 28152991Ssusans } 28162991Ssusans } 28172991Ssusans } 28182991Ssusans return (0); 28192991Ssusans } 28202991Ssusans 28212991Ssusans /* 28222991Ssusans * as_iset2_default_lpsize() calls as_iset3_default_lpsize() to set the 28232991Ssusans * pagesize on each segment in its range, but if any fails with EINVAL, 28242991Ssusans * then it reduces the pagesizes to the next size in the bitmap and 28252991Ssusans * retries as_iset3_default_lpsize(). The reason why the code retries 28262991Ssusans * smaller allowed sizes on EINVAL is because (a) the anon offset may not 28272991Ssusans * match the bigger sizes, and (b) it's hard to get this offset (to begin 28282991Ssusans * with) to pass to map_pgszcvec(). 28292991Ssusans */ 28302991Ssusans static int 28312991Ssusans as_iset2_default_lpsize(struct as *as, caddr_t addr, size_t size, uint_t szc, 28322991Ssusans uint_t szcvec) 28332991Ssusans { 28342991Ssusans int error; 28352991Ssusans int retry; 28362991Ssusans 28372991Ssusans for (;;) { 28382991Ssusans error = as_iset3_default_lpsize(as, addr, size, szc, &retry); 28392991Ssusans if (error == EINVAL && retry) { 28402991Ssusans szcvec &= ~(1 << szc); 28412991Ssusans if (szcvec <= 1) { 28422991Ssusans return (EINVAL); 28432991Ssusans } 28442991Ssusans szc = highbit(szcvec) - 1; 28452991Ssusans } else { 28462991Ssusans return (error); 28472991Ssusans } 28482991Ssusans } 28492991Ssusans } 28502991Ssusans 28512991Ssusans /* 28522991Ssusans * as_iset1_default_lpsize() breaks its chunk into areas where existing 28532991Ssusans * segments have a smaller szc than we want to set. For each such area, 28542991Ssusans * it calls as_iset2_default_lpsize() 28552991Ssusans */ 28562991Ssusans static int 28572991Ssusans as_iset1_default_lpsize(struct as *as, caddr_t raddr, size_t rsize, uint_t szc, 28582991Ssusans uint_t szcvec) 28592991Ssusans { 28602991Ssusans struct seg *seg; 28612991Ssusans size_t ssize; 28622991Ssusans caddr_t setaddr = raddr; 28632991Ssusans size_t setsize = 0; 28642991Ssusans int set; 28652991Ssusans int error; 28662991Ssusans 28672991Ssusans ASSERT(AS_WRITE_HELD(as, &as->a_lock)); 28682991Ssusans 28692991Ssusans seg = as_segat(as, raddr); 28702991Ssusans if (seg == NULL) { 28712991Ssusans panic("as_iset1_default_lpsize: no seg"); 28722991Ssusans } 28732991Ssusans if (seg->s_szc < szc) { 28742991Ssusans set = 1; 28752991Ssusans } else { 28762991Ssusans set = 0; 28772991Ssusans } 28782991Ssusans 28792991Ssusans for (; rsize != 0; rsize -= ssize, raddr += ssize, setsize += ssize) { 28802991Ssusans if (raddr >= seg->s_base + seg->s_size) { 28812991Ssusans seg = AS_SEGNEXT(as, seg); 28822991Ssusans if (seg == NULL || raddr != seg->s_base) { 28832991Ssusans panic("as_iset1_default_lpsize: as changed"); 28842991Ssusans } 28852991Ssusans if (seg->s_szc >= szc && set) { 28862991Ssusans ASSERT(setsize != 0); 28872991Ssusans error = as_iset2_default_lpsize(as, 28882991Ssusans setaddr, setsize, szc, szcvec); 28892991Ssusans if (error) { 28902991Ssusans return (error); 28912991Ssusans } 28922991Ssusans set = 0; 28932991Ssusans } else if (seg->s_szc < szc && !set) { 28942991Ssusans setaddr = raddr; 28952991Ssusans setsize = 0; 28962991Ssusans set = 1; 28972991Ssusans } 28982991Ssusans } 28992991Ssusans if ((raddr + rsize) > (seg->s_base + seg->s_size)) { 29002991Ssusans ssize = seg->s_base + seg->s_size - raddr; 29012991Ssusans } else { 29022991Ssusans ssize = rsize; 29032991Ssusans } 29042991Ssusans } 29052991Ssusans error = 0; 29062991Ssusans if (set) { 29072991Ssusans ASSERT(setsize != 0); 29082991Ssusans error = as_iset2_default_lpsize(as, setaddr, setsize, 29092991Ssusans szc, szcvec); 29102991Ssusans } 29112991Ssusans return (error); 29122991Ssusans } 29132991Ssusans 29142991Ssusans /* 29152991Ssusans * as_iset_default_lpsize() breaks its chunk according to the size code bitmap 29162991Ssusans * returned by map_pgszcvec() (similar to as_map_segvn_segs()), and passes each 29172991Ssusans * chunk to as_iset1_default_lpsize(). 29182991Ssusans */ 29192991Ssusans static int 29202991Ssusans as_iset_default_lpsize(struct as *as, caddr_t addr, size_t size, int flags, 29212991Ssusans int type) 29222991Ssusans { 29232991Ssusans int rtype = (type & MAP_SHARED) ? MAPPGSZC_SHM : MAPPGSZC_PRIVM; 29242991Ssusans uint_t szcvec = map_pgszcvec(addr, size, (uintptr_t)addr, 29252991Ssusans flags, rtype, 1); 29262991Ssusans uint_t szc; 29272991Ssusans uint_t nszc; 29282991Ssusans int error; 29292991Ssusans caddr_t a; 29302991Ssusans caddr_t eaddr; 29312991Ssusans size_t segsize; 29322991Ssusans size_t pgsz; 29332991Ssusans uint_t save_szcvec; 29342991Ssusans 29352991Ssusans ASSERT(AS_WRITE_HELD(as, &as->a_lock)); 29362991Ssusans ASSERT(IS_P2ALIGNED(addr, PAGESIZE)); 29372991Ssusans ASSERT(IS_P2ALIGNED(size, PAGESIZE)); 29382991Ssusans 29392991Ssusans szcvec &= ~1; 29402991Ssusans if (szcvec <= 1) { /* skip if base page size */ 29412991Ssusans return (0); 29422991Ssusans } 29432991Ssusans 29442991Ssusans /* Get the pagesize of the first larger page size. */ 29452991Ssusans szc = lowbit(szcvec) - 1; 29462991Ssusans pgsz = page_get_pagesize(szc); 29472991Ssusans eaddr = addr + size; 29482991Ssusans addr = (caddr_t)P2ROUNDUP((uintptr_t)addr, pgsz); 29492991Ssusans eaddr = (caddr_t)P2ALIGN((uintptr_t)eaddr, pgsz); 29502991Ssusans 29512991Ssusans save_szcvec = szcvec; 29522991Ssusans szcvec >>= (szc + 1); 29532991Ssusans nszc = szc; 29542991Ssusans while (szcvec) { 29552991Ssusans if ((szcvec & 0x1) == 0) { 29562991Ssusans nszc++; 29572991Ssusans szcvec >>= 1; 29582991Ssusans continue; 29592991Ssusans } 29602991Ssusans nszc++; 29612991Ssusans pgsz = page_get_pagesize(nszc); 29622991Ssusans a = (caddr_t)P2ROUNDUP((uintptr_t)addr, pgsz); 29632991Ssusans if (a != addr) { 29642991Ssusans ASSERT(szc > 0); 29652991Ssusans ASSERT(a < eaddr); 29662991Ssusans segsize = a - addr; 29672991Ssusans error = as_iset1_default_lpsize(as, addr, segsize, szc, 29682991Ssusans save_szcvec); 29692991Ssusans if (error) { 29702991Ssusans return (error); 29712991Ssusans } 29722991Ssusans addr = a; 29732991Ssusans } 29742991Ssusans szc = nszc; 29752991Ssusans szcvec >>= 1; 29762991Ssusans } 29772991Ssusans 29782991Ssusans ASSERT(addr < eaddr); 29792991Ssusans szcvec = save_szcvec; 29802991Ssusans while (szcvec) { 29812991Ssusans a = (caddr_t)P2ALIGN((uintptr_t)eaddr, pgsz); 29822991Ssusans ASSERT(a >= addr); 29832991Ssusans if (a != addr) { 29842991Ssusans ASSERT(szc > 0); 29852991Ssusans segsize = a - addr; 29862991Ssusans error = as_iset1_default_lpsize(as, addr, segsize, szc, 29872991Ssusans save_szcvec); 29882991Ssusans if (error) { 29892991Ssusans return (error); 29902991Ssusans } 29912991Ssusans addr = a; 29922991Ssusans } 29932991Ssusans szcvec &= ~(1 << szc); 29942991Ssusans if (szcvec) { 29952991Ssusans szc = highbit(szcvec) - 1; 29962991Ssusans pgsz = page_get_pagesize(szc); 29972991Ssusans } 29982991Ssusans } 29992991Ssusans ASSERT(addr == eaddr); 30002991Ssusans 30012991Ssusans return (0); 30022991Ssusans } 30032991Ssusans 30042991Ssusans /* 30052991Ssusans * Set the default large page size for the range. Called via memcntl with 30062991Ssusans * page size set to 0. as_set_default_lpsize breaks the range down into 30072991Ssusans * chunks with the same type/flags, ignores-non segvn segments, and passes 30082991Ssusans * each chunk to as_iset_default_lpsize(). 30092991Ssusans */ 30102991Ssusans int 30112991Ssusans as_set_default_lpsize(struct as *as, caddr_t addr, size_t size) 30122991Ssusans { 30132991Ssusans struct seg *seg; 30142991Ssusans caddr_t raddr; 30152991Ssusans size_t rsize; 30162991Ssusans size_t ssize; 30172991Ssusans int rtype, rflags; 30182991Ssusans int stype, sflags; 30192991Ssusans int error; 30202991Ssusans caddr_t setaddr; 30212991Ssusans size_t setsize; 30222991Ssusans int segvn; 30232991Ssusans 30242991Ssusans if (size == 0) 30252991Ssusans return (0); 30262991Ssusans 30272991Ssusans AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER); 30282991Ssusans again: 30292991Ssusans error = 0; 30302991Ssusans 30312991Ssusans raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK); 30322991Ssusans rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) - 30332991Ssusans (size_t)raddr; 30342991Ssusans 30352991Ssusans if (raddr + rsize < raddr) { /* check for wraparound */ 30362991Ssusans AS_LOCK_EXIT(as, &as->a_lock); 30372991Ssusans return (ENOMEM); 30382991Ssusans } 30392991Ssusans as_clearwatchprot(as, raddr, rsize); 30402991Ssusans seg = as_segat(as, raddr); 30412991Ssusans if (seg == NULL) { 30422991Ssusans as_setwatch(as); 30432991Ssusans AS_LOCK_EXIT(as, &as->a_lock); 30442991Ssusans return (ENOMEM); 30452991Ssusans } 30462991Ssusans if (seg->s_ops == &segvn_ops) { 30472991Ssusans rtype = SEGOP_GETTYPE(seg, addr); 30482991Ssusans rflags = rtype & (MAP_TEXT | MAP_INITDATA); 30492991Ssusans rtype = rtype & (MAP_SHARED | MAP_PRIVATE); 30502991Ssusans segvn = 1; 30512991Ssusans } else { 30522991Ssusans segvn = 0; 30532991Ssusans } 30542991Ssusans setaddr = raddr; 30552991Ssusans setsize = 0; 30562991Ssusans 30572991Ssusans for (; rsize != 0; rsize -= ssize, raddr += ssize, setsize += ssize) { 30582991Ssusans if (raddr >= (seg->s_base + seg->s_size)) { 30592991Ssusans seg = AS_SEGNEXT(as, seg); 30602991Ssusans if (seg == NULL || raddr != seg->s_base) { 30612991Ssusans error = ENOMEM; 30622991Ssusans break; 30632991Ssusans } 30642991Ssusans if (seg->s_ops == &segvn_ops) { 30652991Ssusans stype = SEGOP_GETTYPE(seg, raddr); 30662991Ssusans sflags = stype & (MAP_TEXT | MAP_INITDATA); 30672991Ssusans stype &= (MAP_SHARED | MAP_PRIVATE); 30682991Ssusans if (segvn && (rflags != sflags || 30692991Ssusans rtype != stype)) { 30702991Ssusans /* 30712991Ssusans * The next segment is also segvn but 30722991Ssusans * has different flags and/or type. 30732991Ssusans */ 30742991Ssusans ASSERT(setsize != 0); 30752991Ssusans error = as_iset_default_lpsize(as, 30762991Ssusans setaddr, setsize, rflags, rtype); 30772991Ssusans if (error) { 30782991Ssusans break; 30792991Ssusans } 30802991Ssusans rflags = sflags; 30812991Ssusans rtype = stype; 30822991Ssusans setaddr = raddr; 30832991Ssusans setsize = 0; 30842991Ssusans } else if (!segvn) { 30852991Ssusans rflags = sflags; 30862991Ssusans rtype = stype; 30872991Ssusans setaddr = raddr; 30882991Ssusans setsize = 0; 30892991Ssusans segvn = 1; 30902991Ssusans } 30912991Ssusans } else if (segvn) { 30922991Ssusans /* The next segment is not segvn. */ 30932991Ssusans ASSERT(setsize != 0); 30942991Ssusans error = as_iset_default_lpsize(as, 30952991Ssusans setaddr, setsize, rflags, rtype); 30962991Ssusans if (error) { 30972991Ssusans break; 30982991Ssusans } 30992991Ssusans segvn = 0; 31002991Ssusans } 31012991Ssusans } 31022991Ssusans if ((raddr + rsize) > (seg->s_base + seg->s_size)) { 31032991Ssusans ssize = seg->s_base + seg->s_size - raddr; 31042991Ssusans } else { 31052991Ssusans ssize = rsize; 31062991Ssusans } 31072991Ssusans } 31082991Ssusans if (error == 0 && segvn) { 31092991Ssusans /* The last chunk when rsize == 0. */ 31102991Ssusans ASSERT(setsize != 0); 31112991Ssusans error = as_iset_default_lpsize(as, setaddr, setsize, 31122991Ssusans rflags, rtype); 31132991Ssusans } 31142991Ssusans 31152991Ssusans if (error == IE_RETRY) { 31162991Ssusans goto again; 31172991Ssusans } else if (error == IE_NOMEM) { 31182991Ssusans error = EAGAIN; 31192991Ssusans } else if (error == ENOTSUP) { 31202991Ssusans error = EINVAL; 31212991Ssusans } else if (error == EAGAIN) { 31222991Ssusans mutex_enter(&as->a_contents); 31232991Ssusans if (AS_ISUNMAPWAIT(as) == 0) { 31242991Ssusans cv_broadcast(&as->a_cv); 31252991Ssusans } 31262991Ssusans AS_SETUNMAPWAIT(as); 31272991Ssusans AS_LOCK_EXIT(as, &as->a_lock); 31282991Ssusans while (AS_ISUNMAPWAIT(as)) { 31292991Ssusans cv_wait(&as->a_cv, &as->a_contents); 31302991Ssusans } 31312991Ssusans mutex_exit(&as->a_contents); 31322991Ssusans AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER); 31332991Ssusans goto again; 31342991Ssusans } 31352991Ssusans 31362991Ssusans as_setwatch(as); 31372991Ssusans AS_LOCK_EXIT(as, &as->a_lock); 31382991Ssusans return (error); 31392991Ssusans } 31402991Ssusans 31412991Ssusans /* 31420Sstevel@tonic-gate * Setup all of the uninitialized watched pages that we can. 31430Sstevel@tonic-gate */ 31440Sstevel@tonic-gate void 31450Sstevel@tonic-gate as_setwatch(struct as *as) 31460Sstevel@tonic-gate { 31470Sstevel@tonic-gate struct watched_page *pwp; 31480Sstevel@tonic-gate struct seg *seg; 31490Sstevel@tonic-gate caddr_t vaddr; 31500Sstevel@tonic-gate uint_t prot; 31510Sstevel@tonic-gate int err, retrycnt; 31520Sstevel@tonic-gate 31530Sstevel@tonic-gate if (avl_numnodes(&as->a_wpage) == 0) 31540Sstevel@tonic-gate return; 31550Sstevel@tonic-gate 31560Sstevel@tonic-gate ASSERT(AS_WRITE_HELD(as, &as->a_lock)); 31570Sstevel@tonic-gate 31580Sstevel@tonic-gate for (pwp = avl_first(&as->a_wpage); pwp != NULL; 31590Sstevel@tonic-gate pwp = AVL_NEXT(&as->a_wpage, pwp)) { 31600Sstevel@tonic-gate retrycnt = 0; 31610Sstevel@tonic-gate retry: 31620Sstevel@tonic-gate vaddr = pwp->wp_vaddr; 31630Sstevel@tonic-gate if (pwp->wp_oprot != 0 || /* already set up */ 31640Sstevel@tonic-gate (seg = as_segat(as, vaddr)) == NULL || 31650Sstevel@tonic-gate SEGOP_GETPROT(seg, vaddr, 0, &prot) != 0) 31660Sstevel@tonic-gate continue; 31670Sstevel@tonic-gate 31680Sstevel@tonic-gate pwp->wp_oprot = prot; 31690Sstevel@tonic-gate if (pwp->wp_read) 31700Sstevel@tonic-gate prot &= ~(PROT_READ|PROT_WRITE|PROT_EXEC); 31710Sstevel@tonic-gate if (pwp->wp_write) 31720Sstevel@tonic-gate prot &= ~PROT_WRITE; 31730Sstevel@tonic-gate if (pwp->wp_exec) 31740Sstevel@tonic-gate prot &= ~(PROT_READ|PROT_WRITE|PROT_EXEC); 31750Sstevel@tonic-gate if (!(pwp->wp_flags & WP_NOWATCH) && prot != pwp->wp_oprot) { 31760Sstevel@tonic-gate err = SEGOP_SETPROT(seg, vaddr, PAGESIZE, prot); 31770Sstevel@tonic-gate if (err == IE_RETRY) { 31780Sstevel@tonic-gate pwp->wp_oprot = 0; 31790Sstevel@tonic-gate ASSERT(retrycnt == 0); 31800Sstevel@tonic-gate retrycnt++; 31810Sstevel@tonic-gate goto retry; 31820Sstevel@tonic-gate } 31830Sstevel@tonic-gate } 31840Sstevel@tonic-gate pwp->wp_prot = prot; 31850Sstevel@tonic-gate } 31860Sstevel@tonic-gate } 31870Sstevel@tonic-gate 31880Sstevel@tonic-gate /* 31890Sstevel@tonic-gate * Clear all of the watched pages in the address space. 31900Sstevel@tonic-gate */ 31910Sstevel@tonic-gate void 31920Sstevel@tonic-gate as_clearwatch(struct as *as) 31930Sstevel@tonic-gate { 31940Sstevel@tonic-gate struct watched_page *pwp; 31950Sstevel@tonic-gate struct seg *seg; 31960Sstevel@tonic-gate caddr_t vaddr; 31970Sstevel@tonic-gate uint_t prot; 31980Sstevel@tonic-gate int err, retrycnt; 31990Sstevel@tonic-gate 32000Sstevel@tonic-gate if (avl_numnodes(&as->a_wpage) == 0) 32010Sstevel@tonic-gate return; 32020Sstevel@tonic-gate 32030Sstevel@tonic-gate ASSERT(AS_WRITE_HELD(as, &as->a_lock)); 32040Sstevel@tonic-gate 32050Sstevel@tonic-gate for (pwp = avl_first(&as->a_wpage); pwp != NULL; 32060Sstevel@tonic-gate pwp = AVL_NEXT(&as->a_wpage, pwp)) { 32070Sstevel@tonic-gate retrycnt = 0; 32080Sstevel@tonic-gate retry: 32090Sstevel@tonic-gate vaddr = pwp->wp_vaddr; 32100Sstevel@tonic-gate if (pwp->wp_oprot == 0 || /* not set up */ 32110Sstevel@tonic-gate (seg = as_segat(as, vaddr)) == NULL) 32120Sstevel@tonic-gate continue; 32130Sstevel@tonic-gate 32140Sstevel@tonic-gate if ((prot = pwp->wp_oprot) != pwp->wp_prot) { 32150Sstevel@tonic-gate err = SEGOP_SETPROT(seg, vaddr, PAGESIZE, prot); 32160Sstevel@tonic-gate if (err == IE_RETRY) { 32170Sstevel@tonic-gate ASSERT(retrycnt == 0); 32180Sstevel@tonic-gate retrycnt++; 32190Sstevel@tonic-gate goto retry; 32200Sstevel@tonic-gate } 32210Sstevel@tonic-gate } 32220Sstevel@tonic-gate pwp->wp_oprot = 0; 32230Sstevel@tonic-gate pwp->wp_prot = 0; 32240Sstevel@tonic-gate } 32250Sstevel@tonic-gate } 32260Sstevel@tonic-gate 32270Sstevel@tonic-gate /* 32280Sstevel@tonic-gate * Force a new setup for all the watched pages in the range. 32290Sstevel@tonic-gate */ 32300Sstevel@tonic-gate static void 32310Sstevel@tonic-gate as_setwatchprot(struct as *as, caddr_t addr, size_t size, uint_t prot) 32320Sstevel@tonic-gate { 32330Sstevel@tonic-gate struct watched_page *pwp; 32340Sstevel@tonic-gate struct watched_page tpw; 32350Sstevel@tonic-gate caddr_t eaddr = addr + size; 32360Sstevel@tonic-gate caddr_t vaddr; 32370Sstevel@tonic-gate struct seg *seg; 32380Sstevel@tonic-gate int err, retrycnt; 32390Sstevel@tonic-gate uint_t wprot; 32400Sstevel@tonic-gate avl_index_t where; 32410Sstevel@tonic-gate 32420Sstevel@tonic-gate if (avl_numnodes(&as->a_wpage) == 0) 32430Sstevel@tonic-gate return; 32440Sstevel@tonic-gate 32450Sstevel@tonic-gate ASSERT(AS_WRITE_HELD(as, &as->a_lock)); 32460Sstevel@tonic-gate 32470Sstevel@tonic-gate tpw.wp_vaddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK); 32480Sstevel@tonic-gate if ((pwp = avl_find(&as->a_wpage, &tpw, &where)) == NULL) 32490Sstevel@tonic-gate pwp = avl_nearest(&as->a_wpage, where, AVL_AFTER); 32500Sstevel@tonic-gate 32510Sstevel@tonic-gate while (pwp != NULL && pwp->wp_vaddr < eaddr) { 32520Sstevel@tonic-gate retrycnt = 0; 32530Sstevel@tonic-gate vaddr = pwp->wp_vaddr; 32540Sstevel@tonic-gate 32550Sstevel@tonic-gate wprot = prot; 32560Sstevel@tonic-gate if (pwp->wp_read) 32570Sstevel@tonic-gate wprot &= ~(PROT_READ|PROT_WRITE|PROT_EXEC); 32580Sstevel@tonic-gate if (pwp->wp_write) 32590Sstevel@tonic-gate wprot &= ~PROT_WRITE; 32600Sstevel@tonic-gate if (pwp->wp_exec) 32610Sstevel@tonic-gate wprot &= ~(PROT_READ|PROT_WRITE|PROT_EXEC); 32620Sstevel@tonic-gate if (!(pwp->wp_flags & WP_NOWATCH) && wprot != pwp->wp_oprot) { 32630Sstevel@tonic-gate retry: 32640Sstevel@tonic-gate seg = as_segat(as, vaddr); 32650Sstevel@tonic-gate if (seg == NULL) { 32660Sstevel@tonic-gate panic("as_setwatchprot: no seg"); 32670Sstevel@tonic-gate /*NOTREACHED*/ 32680Sstevel@tonic-gate } 32690Sstevel@tonic-gate err = SEGOP_SETPROT(seg, vaddr, PAGESIZE, wprot); 32700Sstevel@tonic-gate if (err == IE_RETRY) { 32710Sstevel@tonic-gate ASSERT(retrycnt == 0); 32720Sstevel@tonic-gate retrycnt++; 32730Sstevel@tonic-gate goto retry; 32740Sstevel@tonic-gate } 32750Sstevel@tonic-gate } 32760Sstevel@tonic-gate pwp->wp_oprot = prot; 32770Sstevel@tonic-gate pwp->wp_prot = wprot; 32780Sstevel@tonic-gate 32790Sstevel@tonic-gate pwp = AVL_NEXT(&as->a_wpage, pwp); 32800Sstevel@tonic-gate } 32810Sstevel@tonic-gate } 32820Sstevel@tonic-gate 32830Sstevel@tonic-gate /* 32840Sstevel@tonic-gate * Clear all of the watched pages in the range. 32850Sstevel@tonic-gate */ 32860Sstevel@tonic-gate static void 32870Sstevel@tonic-gate as_clearwatchprot(struct as *as, caddr_t addr, size_t size) 32880Sstevel@tonic-gate { 32890Sstevel@tonic-gate caddr_t eaddr = addr + size; 32900Sstevel@tonic-gate struct watched_page *pwp; 32910Sstevel@tonic-gate struct watched_page tpw; 32920Sstevel@tonic-gate uint_t prot; 32930Sstevel@tonic-gate struct seg *seg; 32940Sstevel@tonic-gate int err, retrycnt; 32950Sstevel@tonic-gate avl_index_t where; 32960Sstevel@tonic-gate 32970Sstevel@tonic-gate if (avl_numnodes(&as->a_wpage) == 0) 32980Sstevel@tonic-gate return; 32990Sstevel@tonic-gate 33000Sstevel@tonic-gate tpw.wp_vaddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK); 33010Sstevel@tonic-gate if ((pwp = avl_find(&as->a_wpage, &tpw, &where)) == NULL) 33020Sstevel@tonic-gate pwp = avl_nearest(&as->a_wpage, where, AVL_AFTER); 33030Sstevel@tonic-gate 33040Sstevel@tonic-gate ASSERT(AS_WRITE_HELD(as, &as->a_lock)); 33050Sstevel@tonic-gate 33060Sstevel@tonic-gate while (pwp != NULL && pwp->wp_vaddr < eaddr) { 33070Sstevel@tonic-gate ASSERT(addr >= pwp->wp_vaddr); 33080Sstevel@tonic-gate 33090Sstevel@tonic-gate if ((prot = pwp->wp_oprot) != 0) { 33100Sstevel@tonic-gate retrycnt = 0; 33110Sstevel@tonic-gate 33120Sstevel@tonic-gate if (prot != pwp->wp_prot) { 33130Sstevel@tonic-gate retry: 33140Sstevel@tonic-gate seg = as_segat(as, pwp->wp_vaddr); 33150Sstevel@tonic-gate if (seg == NULL) 33160Sstevel@tonic-gate continue; 33170Sstevel@tonic-gate err = SEGOP_SETPROT(seg, pwp->wp_vaddr, 33180Sstevel@tonic-gate PAGESIZE, prot); 33190Sstevel@tonic-gate if (err == IE_RETRY) { 33200Sstevel@tonic-gate ASSERT(retrycnt == 0); 33210Sstevel@tonic-gate retrycnt++; 33220Sstevel@tonic-gate goto retry; 33230Sstevel@tonic-gate 33240Sstevel@tonic-gate } 33250Sstevel@tonic-gate } 33260Sstevel@tonic-gate pwp->wp_oprot = 0; 33270Sstevel@tonic-gate pwp->wp_prot = 0; 33280Sstevel@tonic-gate } 33290Sstevel@tonic-gate 33300Sstevel@tonic-gate pwp = AVL_NEXT(&as->a_wpage, pwp); 33310Sstevel@tonic-gate } 33320Sstevel@tonic-gate } 33330Sstevel@tonic-gate 33340Sstevel@tonic-gate void 33350Sstevel@tonic-gate as_signal_proc(struct as *as, k_siginfo_t *siginfo) 33360Sstevel@tonic-gate { 33370Sstevel@tonic-gate struct proc *p; 33380Sstevel@tonic-gate 33390Sstevel@tonic-gate mutex_enter(&pidlock); 33400Sstevel@tonic-gate for (p = practive; p; p = p->p_next) { 33410Sstevel@tonic-gate if (p->p_as == as) { 33420Sstevel@tonic-gate mutex_enter(&p->p_lock); 33430Sstevel@tonic-gate if (p->p_as == as) 33440Sstevel@tonic-gate sigaddq(p, NULL, siginfo, KM_NOSLEEP); 33450Sstevel@tonic-gate mutex_exit(&p->p_lock); 33460Sstevel@tonic-gate } 33470Sstevel@tonic-gate } 33480Sstevel@tonic-gate mutex_exit(&pidlock); 33490Sstevel@tonic-gate } 33500Sstevel@tonic-gate 33510Sstevel@tonic-gate /* 33520Sstevel@tonic-gate * return memory object ID 33530Sstevel@tonic-gate */ 33540Sstevel@tonic-gate int 33550Sstevel@tonic-gate as_getmemid(struct as *as, caddr_t addr, memid_t *memidp) 33560Sstevel@tonic-gate { 33570Sstevel@tonic-gate struct seg *seg; 33580Sstevel@tonic-gate int sts; 33590Sstevel@tonic-gate 33600Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 33610Sstevel@tonic-gate seg = as_segat(as, addr); 33620Sstevel@tonic-gate if (seg == NULL) { 33630Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 33640Sstevel@tonic-gate return (EFAULT); 33650Sstevel@tonic-gate } 33660Sstevel@tonic-gate /* 33670Sstevel@tonic-gate * catch old drivers which may not support getmemid 33680Sstevel@tonic-gate */ 33690Sstevel@tonic-gate if (seg->s_ops->getmemid == NULL) { 33700Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 33710Sstevel@tonic-gate return (ENODEV); 33720Sstevel@tonic-gate } 33730Sstevel@tonic-gate 33740Sstevel@tonic-gate sts = SEGOP_GETMEMID(seg, addr, memidp); 33750Sstevel@tonic-gate 33760Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 33770Sstevel@tonic-gate return (sts); 33780Sstevel@tonic-gate } 3379