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 52768Ssl108498 * Common Development and Distribution License (the "License"). 62768Ssl108498 * 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 /* 229121SVamsi.Krishna@Sun.COM * Copyright 2009 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 #ifndef _VM_AS_H 400Sstevel@tonic-gate #define _VM_AS_H 410Sstevel@tonic-gate 420Sstevel@tonic-gate #include <sys/watchpoint.h> 430Sstevel@tonic-gate #include <vm/seg.h> 440Sstevel@tonic-gate #include <vm/faultcode.h> 450Sstevel@tonic-gate #include <vm/hat.h> 460Sstevel@tonic-gate #include <sys/avl.h> 472768Ssl108498 #include <sys/proc.h> 480Sstevel@tonic-gate 490Sstevel@tonic-gate #ifdef __cplusplus 500Sstevel@tonic-gate extern "C" { 510Sstevel@tonic-gate #endif 520Sstevel@tonic-gate 530Sstevel@tonic-gate /* 540Sstevel@tonic-gate * VM - Address spaces. 550Sstevel@tonic-gate */ 560Sstevel@tonic-gate 570Sstevel@tonic-gate /* 580Sstevel@tonic-gate * Each address space consists of a sorted list of segments 590Sstevel@tonic-gate * and machine dependent address translation information. 600Sstevel@tonic-gate * 610Sstevel@tonic-gate * All the hard work is in the segment drivers and the 620Sstevel@tonic-gate * hardware address translation code. 630Sstevel@tonic-gate * 640Sstevel@tonic-gate * The segment list is represented as an AVL tree. 650Sstevel@tonic-gate * 660Sstevel@tonic-gate * The address space lock (a_lock) is a long term lock which serializes 670Sstevel@tonic-gate * access to certain operations (as_map, as_unmap) and protects the 680Sstevel@tonic-gate * underlying generic segment data (seg.h) along with some fields in the 690Sstevel@tonic-gate * address space structure as shown below: 700Sstevel@tonic-gate * 710Sstevel@tonic-gate * address space structure segment structure 720Sstevel@tonic-gate * 730Sstevel@tonic-gate * a_segtree s_base 740Sstevel@tonic-gate * a_size s_size 750Sstevel@tonic-gate * a_lastgap s_link 760Sstevel@tonic-gate * a_seglast s_ops 770Sstevel@tonic-gate * s_as 780Sstevel@tonic-gate * s_data 790Sstevel@tonic-gate * 800Sstevel@tonic-gate * The address space contents lock (a_contents) is a short term 810Sstevel@tonic-gate * lock that protects most of the data in the address space structure. 820Sstevel@tonic-gate * This lock is always acquired after the "a_lock" in all situations 830Sstevel@tonic-gate * except while dealing with AS_CLAIMGAP to avoid deadlocks. 840Sstevel@tonic-gate * 850Sstevel@tonic-gate * The following fields are protected by this lock: 860Sstevel@tonic-gate * 870Sstevel@tonic-gate * a_flags (AS_PAGLCK, AS_CLAIMGAP, etc.) 880Sstevel@tonic-gate * a_unmapwait 890Sstevel@tonic-gate * a_seglast 900Sstevel@tonic-gate * 910Sstevel@tonic-gate * The address space lock (a_lock) is always held prior to any segment 920Sstevel@tonic-gate * operation. Some segment drivers use the address space lock to protect 930Sstevel@tonic-gate * some or all of their segment private data, provided the version of 940Sstevel@tonic-gate * "a_lock" (read vs. write) is consistent with the use of the data. 950Sstevel@tonic-gate * 960Sstevel@tonic-gate * The following fields are protected by the hat layer lock: 970Sstevel@tonic-gate * 980Sstevel@tonic-gate * a_vbits 990Sstevel@tonic-gate * a_hat 1000Sstevel@tonic-gate * a_hrm 1010Sstevel@tonic-gate */ 1020Sstevel@tonic-gate 1030Sstevel@tonic-gate struct as { 1040Sstevel@tonic-gate kmutex_t a_contents; /* protect certain fields in the structure */ 1050Sstevel@tonic-gate uchar_t a_flags; /* as attributes */ 1060Sstevel@tonic-gate uchar_t a_vbits; /* used for collecting statistics */ 1070Sstevel@tonic-gate kcondvar_t a_cv; /* used by as_rangelock */ 1080Sstevel@tonic-gate struct hat *a_hat; /* hat structure */ 1090Sstevel@tonic-gate struct hrmstat *a_hrm; /* ref and mod bits */ 1100Sstevel@tonic-gate caddr_t a_userlimit; /* highest allowable address in this as */ 1110Sstevel@tonic-gate struct seg *a_seglast; /* last segment hit on the addr space */ 1120Sstevel@tonic-gate krwlock_t a_lock; /* protects segment related fields */ 113*10169SSudheer.Abdul-Salam@Sun.COM size_t a_size; /* total size of address space */ 1140Sstevel@tonic-gate struct seg *a_lastgap; /* last seg found by as_gap() w/ AS_HI (mmap) */ 1150Sstevel@tonic-gate struct seg *a_lastgaphl; /* last seg saved in as_gap() either for */ 1160Sstevel@tonic-gate /* AS_HI or AS_LO used in as_addseg() */ 1170Sstevel@tonic-gate avl_tree_t a_segtree; /* segments in this address space. (AVL tree) */ 1180Sstevel@tonic-gate avl_tree_t a_wpage; /* watched pages (procfs) */ 1190Sstevel@tonic-gate uchar_t a_updatedir; /* mappings changed, rebuild a_objectdir */ 1200Sstevel@tonic-gate timespec_t a_updatetime; /* time when mappings last changed */ 1210Sstevel@tonic-gate vnode_t **a_objectdir; /* object directory (procfs) */ 1220Sstevel@tonic-gate size_t a_sizedir; /* size of object directory */ 1230Sstevel@tonic-gate struct as_callback *a_callbacks; /* callback list */ 1240Sstevel@tonic-gate void *a_xhat; /* list of xhat providers */ 1252768Ssl108498 proc_t *a_proc; /* back pointer to proc */ 126*10169SSudheer.Abdul-Salam@Sun.COM size_t a_resvsize; /* size of reserved part of address space */ 1270Sstevel@tonic-gate }; 1280Sstevel@tonic-gate 1290Sstevel@tonic-gate #define AS_PAGLCK 0x80 1300Sstevel@tonic-gate #define AS_CLAIMGAP 0x40 1310Sstevel@tonic-gate #define AS_UNMAPWAIT 0x20 1320Sstevel@tonic-gate #define AS_NEEDSPURGE 0x10 /* mostly for seg_nf, see as_purge() */ 1336695Saguzovsk #define AS_NOUNMAPWAIT 0x02 1340Sstevel@tonic-gate #define AS_BUSY 0x01 /* needed by XHAT framework */ 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate #define AS_ISPGLCK(as) ((as)->a_flags & AS_PAGLCK) 1370Sstevel@tonic-gate #define AS_ISCLAIMGAP(as) ((as)->a_flags & AS_CLAIMGAP) 1380Sstevel@tonic-gate #define AS_ISUNMAPWAIT(as) ((as)->a_flags & AS_UNMAPWAIT) 1390Sstevel@tonic-gate #define AS_ISBUSY(as) ((as)->a_flags & AS_BUSY) 1406695Saguzovsk #define AS_ISNOUNMAPWAIT(as) ((as)->a_flags & AS_NOUNMAPWAIT) 1410Sstevel@tonic-gate 1420Sstevel@tonic-gate #define AS_SETPGLCK(as) ((as)->a_flags |= AS_PAGLCK) 1430Sstevel@tonic-gate #define AS_SETCLAIMGAP(as) ((as)->a_flags |= AS_CLAIMGAP) 1440Sstevel@tonic-gate #define AS_SETUNMAPWAIT(as) ((as)->a_flags |= AS_UNMAPWAIT) 1450Sstevel@tonic-gate #define AS_SETBUSY(as) ((as)->a_flags |= AS_BUSY) 1466695Saguzovsk #define AS_SETNOUNMAPWAIT(as) ((as)->a_flags |= AS_NOUNMAPWAIT) 1470Sstevel@tonic-gate 1480Sstevel@tonic-gate #define AS_CLRPGLCK(as) ((as)->a_flags &= ~AS_PAGLCK) 1490Sstevel@tonic-gate #define AS_CLRCLAIMGAP(as) ((as)->a_flags &= ~AS_CLAIMGAP) 1500Sstevel@tonic-gate #define AS_CLRUNMAPWAIT(as) ((as)->a_flags &= ~AS_UNMAPWAIT) 1510Sstevel@tonic-gate #define AS_CLRBUSY(as) ((as)->a_flags &= ~AS_BUSY) 1526695Saguzovsk #define AS_CLRNOUNMAPWAIT(as) ((as)->a_flags &= ~AS_NOUNMAPWAIT) 1530Sstevel@tonic-gate 1540Sstevel@tonic-gate #define AS_TYPE_64BIT(as) \ 1550Sstevel@tonic-gate (((as)->a_userlimit > (caddr_t)UINT32_MAX) ? 1 : 0) 1560Sstevel@tonic-gate 1570Sstevel@tonic-gate /* 1582991Ssusans * Flags for as_map/as_map_ansegs 1592991Ssusans */ 1602991Ssusans #define AS_MAP_NO_LPOOB ((uint_t)-1) 1612991Ssusans #define AS_MAP_HEAP ((uint_t)-2) 1622991Ssusans #define AS_MAP_STACK ((uint_t)-3) 1632991Ssusans 1642991Ssusans /* 1650Sstevel@tonic-gate * The as_callback is the basic structure which supports the ability to 1660Sstevel@tonic-gate * inform clients of specific events pertaining to address space management. 1670Sstevel@tonic-gate * A user calls as_add_callback to register an address space callback 1680Sstevel@tonic-gate * for a range of pages, specifying the events that need to occur. 1690Sstevel@tonic-gate * When as_do_callbacks is called and finds a 'matching' entry, the 1700Sstevel@tonic-gate * callback is called once, and the callback function MUST call 1710Sstevel@tonic-gate * as_delete_callback when all callback activities are complete. 1720Sstevel@tonic-gate * The thread calling as_do_callbacks blocks until the as_delete_callback 1730Sstevel@tonic-gate * is called. This allows for asynchorous events to subside before the 1740Sstevel@tonic-gate * as_do_callbacks thread continues. 1750Sstevel@tonic-gate * 1760Sstevel@tonic-gate * An example of the need for this is a driver which has done long-term 1770Sstevel@tonic-gate * locking of memory. Address space management operations (events) such 1780Sstevel@tonic-gate * as as_free, as_umap, and as_setprot will block indefinitely until the 1790Sstevel@tonic-gate * pertinent memory is unlocked. The callback mechanism provides the 1800Sstevel@tonic-gate * way to inform the driver of the event so that the driver may do the 1810Sstevel@tonic-gate * necessary unlocking. 1820Sstevel@tonic-gate * 1830Sstevel@tonic-gate * The contents of this structure is protected by a_contents lock 1840Sstevel@tonic-gate */ 1850Sstevel@tonic-gate typedef void (*callback_func_t)(struct as *, void *, uint_t); 1860Sstevel@tonic-gate struct as_callback { 1870Sstevel@tonic-gate struct as_callback *ascb_next; /* list link */ 1880Sstevel@tonic-gate uint_t ascb_events; /* event types */ 1890Sstevel@tonic-gate callback_func_t ascb_func; /* callback function */ 1900Sstevel@tonic-gate void *ascb_arg; /* callback argument */ 1910Sstevel@tonic-gate caddr_t ascb_saddr; /* start address */ 1920Sstevel@tonic-gate size_t ascb_len; /* address range */ 1930Sstevel@tonic-gate }; 1940Sstevel@tonic-gate /* 1950Sstevel@tonic-gate * Callback events 1960Sstevel@tonic-gate */ 1970Sstevel@tonic-gate #define AS_FREE_EVENT 0x1 1980Sstevel@tonic-gate #define AS_SETPROT_EVENT 0x2 1990Sstevel@tonic-gate #define AS_UNMAP_EVENT 0x4 2000Sstevel@tonic-gate #define AS_CALLBACK_CALLED ((uint_t)(1U << (8 * sizeof (uint_t) - 1U))) 2010Sstevel@tonic-gate #define AS_UNMAPWAIT_EVENT \ 2020Sstevel@tonic-gate (AS_FREE_EVENT | AS_SETPROT_EVENT | AS_UNMAP_EVENT) 2030Sstevel@tonic-gate #define AS_ALL_EVENT \ 2040Sstevel@tonic-gate (AS_FREE_EVENT | AS_SETPROT_EVENT | AS_UNMAP_EVENT) 2050Sstevel@tonic-gate 2060Sstevel@tonic-gate 2070Sstevel@tonic-gate /* Return code values for as_callback_delete */ 2080Sstevel@tonic-gate enum as_cbdelete_rc { 2090Sstevel@tonic-gate AS_CALLBACK_DELETED, 2100Sstevel@tonic-gate AS_CALLBACK_NOTFOUND, 2110Sstevel@tonic-gate AS_CALLBACK_DELETE_DEFERRED 2120Sstevel@tonic-gate }; 2130Sstevel@tonic-gate 2140Sstevel@tonic-gate #ifdef _KERNEL 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate /* 2170Sstevel@tonic-gate * Flags for as_gap. 2180Sstevel@tonic-gate */ 2190Sstevel@tonic-gate #define AH_DIR 0x1 /* direction flag mask */ 2200Sstevel@tonic-gate #define AH_LO 0x0 /* find lowest hole */ 2210Sstevel@tonic-gate #define AH_HI 0x1 /* find highest hole */ 2220Sstevel@tonic-gate #define AH_CONTAIN 0x2 /* hole must contain `addr' */ 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate extern struct as kas; /* kernel's address space */ 2250Sstevel@tonic-gate 2260Sstevel@tonic-gate /* 2270Sstevel@tonic-gate * Macros for address space locking. 2280Sstevel@tonic-gate */ 2290Sstevel@tonic-gate #define AS_LOCK_ENTER(as, lock, type) rw_enter((lock), (type)) 2300Sstevel@tonic-gate #define AS_LOCK_EXIT(as, lock) rw_exit((lock)) 2310Sstevel@tonic-gate #define AS_LOCK_DESTROY(as, lock) rw_destroy((lock)) 2320Sstevel@tonic-gate #define AS_LOCK_TRYENTER(as, lock, type) rw_tryenter((lock), (type)) 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate /* 2350Sstevel@tonic-gate * Macros to test lock states. 2360Sstevel@tonic-gate */ 2370Sstevel@tonic-gate #define AS_LOCK_HELD(as, lock) RW_LOCK_HELD((lock)) 2380Sstevel@tonic-gate #define AS_READ_HELD(as, lock) RW_READ_HELD((lock)) 2390Sstevel@tonic-gate #define AS_WRITE_HELD(as, lock) RW_WRITE_HELD((lock)) 2400Sstevel@tonic-gate 2410Sstevel@tonic-gate /* 2420Sstevel@tonic-gate * macros to walk thru segment lists 2430Sstevel@tonic-gate */ 2440Sstevel@tonic-gate #define AS_SEGFIRST(as) avl_first(&(as)->a_segtree) 2450Sstevel@tonic-gate #define AS_SEGNEXT(as, seg) AVL_NEXT(&(as)->a_segtree, (seg)) 2460Sstevel@tonic-gate #define AS_SEGPREV(as, seg) AVL_PREV(&(as)->a_segtree, (seg)) 2470Sstevel@tonic-gate 2480Sstevel@tonic-gate void as_init(void); 2490Sstevel@tonic-gate void as_avlinit(struct as *); 2500Sstevel@tonic-gate struct seg *as_segat(struct as *as, caddr_t addr); 2510Sstevel@tonic-gate void as_rangelock(struct as *as); 2520Sstevel@tonic-gate void as_rangeunlock(struct as *as); 2532768Ssl108498 struct as *as_alloc(); 2540Sstevel@tonic-gate void as_free(struct as *as); 2559121SVamsi.Krishna@Sun.COM int as_dup(struct as *as, struct proc *forkedproc); 2560Sstevel@tonic-gate struct seg *as_findseg(struct as *as, caddr_t addr, int tail); 2570Sstevel@tonic-gate int as_addseg(struct as *as, struct seg *newseg); 2580Sstevel@tonic-gate struct seg *as_removeseg(struct as *as, struct seg *seg); 2590Sstevel@tonic-gate faultcode_t as_fault(struct hat *hat, struct as *as, caddr_t addr, size_t size, 2600Sstevel@tonic-gate enum fault_type type, enum seg_rw rw); 2610Sstevel@tonic-gate faultcode_t as_faulta(struct as *as, caddr_t addr, size_t size); 2620Sstevel@tonic-gate int as_setprot(struct as *as, caddr_t addr, size_t size, uint_t prot); 2630Sstevel@tonic-gate int as_checkprot(struct as *as, caddr_t addr, size_t size, uint_t prot); 2640Sstevel@tonic-gate int as_unmap(struct as *as, caddr_t addr, size_t size); 2650Sstevel@tonic-gate int as_map(struct as *as, caddr_t addr, size_t size, int ((*crfp)()), 2660Sstevel@tonic-gate void *argsp); 2670Sstevel@tonic-gate void as_purge(struct as *as); 2680Sstevel@tonic-gate int as_gap(struct as *as, size_t minlen, caddr_t *basep, size_t *lenp, 2690Sstevel@tonic-gate uint_t flags, caddr_t addr); 2705668Smec int as_gap_aligned(struct as *as, size_t minlen, caddr_t *basep, 2715668Smec size_t *lenp, uint_t flags, caddr_t addr, size_t align, 2725668Smec size_t redzone, size_t off); 2735668Smec 2740Sstevel@tonic-gate int as_memory(struct as *as, caddr_t *basep, size_t *lenp); 2750Sstevel@tonic-gate size_t as_swapout(struct as *as); 2760Sstevel@tonic-gate int as_incore(struct as *as, caddr_t addr, size_t size, char *vec, 2770Sstevel@tonic-gate size_t *sizep); 2780Sstevel@tonic-gate int as_ctl(struct as *as, caddr_t addr, size_t size, int func, int attr, 2790Sstevel@tonic-gate uintptr_t arg, ulong_t *lock_map, size_t pos); 2800Sstevel@tonic-gate int as_pagelock(struct as *as, struct page ***ppp, caddr_t addr, 2810Sstevel@tonic-gate size_t size, enum seg_rw rw); 2820Sstevel@tonic-gate void as_pageunlock(struct as *as, struct page **pp, caddr_t addr, 2830Sstevel@tonic-gate size_t size, enum seg_rw rw); 2840Sstevel@tonic-gate int as_setpagesize(struct as *as, caddr_t addr, size_t size, uint_t szc, 2850Sstevel@tonic-gate boolean_t wait); 2862991Ssusans int as_set_default_lpsize(struct as *as, caddr_t addr, size_t size); 2870Sstevel@tonic-gate void as_setwatch(struct as *as); 2880Sstevel@tonic-gate void as_clearwatch(struct as *as); 2890Sstevel@tonic-gate int as_getmemid(struct as *, caddr_t, memid_t *); 2900Sstevel@tonic-gate 2910Sstevel@tonic-gate int as_add_callback(struct as *, void (*)(), void *, uint_t, 2920Sstevel@tonic-gate caddr_t, size_t, int); 2930Sstevel@tonic-gate uint_t as_delete_callback(struct as *, void *); 2940Sstevel@tonic-gate 2950Sstevel@tonic-gate #endif /* _KERNEL */ 2960Sstevel@tonic-gate 2970Sstevel@tonic-gate #ifdef __cplusplus 2980Sstevel@tonic-gate } 2990Sstevel@tonic-gate #endif 3000Sstevel@tonic-gate 3010Sstevel@tonic-gate #endif /* _VM_AS_H */ 302