xref: /onnv-gate/usr/src/uts/common/vm/seg_map.h (revision 1841:9d7ebafcda38)
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
5*1841Spraks  * Common Development and Distribution License (the "License").
6*1841Spraks  * 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 /*
22*1841Spraks  * 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 #ifndef	_VM_SEG_MAP_H
400Sstevel@tonic-gate #define	_VM_SEG_MAP_H
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #ifdef	__cplusplus
450Sstevel@tonic-gate extern "C" {
460Sstevel@tonic-gate #endif
470Sstevel@tonic-gate 
480Sstevel@tonic-gate /*
490Sstevel@tonic-gate  * When segmap is created it is possible to program its behavior,
500Sstevel@tonic-gate  *	using the create args [needed for performance reasons].
510Sstevel@tonic-gate  * Segmap creates n lists of pages.
520Sstevel@tonic-gate  *	For VAC machines, there will be at least one free list
530Sstevel@tonic-gate  *	per color. If more than one free list per color is needed,
540Sstevel@tonic-gate  *	set nfreelist as needed.
550Sstevel@tonic-gate  *
560Sstevel@tonic-gate  *	For PAC machines, it will be treated as VAC with only one
570Sstevel@tonic-gate  *	color- every page is of the same color. Again, set nfreelist
580Sstevel@tonic-gate  *	to get more than one free list.
590Sstevel@tonic-gate  */
600Sstevel@tonic-gate struct segmap_crargs {
610Sstevel@tonic-gate 	uint_t	prot;
620Sstevel@tonic-gate 	uint_t	shmsize;	/* shm_alignment for VAC, 0 for PAC. */
630Sstevel@tonic-gate 	uint_t	nfreelist;	/* number of freelist per color, >= 1 */
640Sstevel@tonic-gate };
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #include <vm/kpm.h>
67*1841Spraks #include <vm/vpm.h>
680Sstevel@tonic-gate 
690Sstevel@tonic-gate /*
700Sstevel@tonic-gate  * Each smap struct represents a MAXBSIZE sized mapping to the
710Sstevel@tonic-gate  * <sm_vp, sm_off> given in the structure.  The location of the
720Sstevel@tonic-gate  * the structure in the array gives the virtual address of the
730Sstevel@tonic-gate  * mapping. Structure rearranged for 64bit sm_off.
740Sstevel@tonic-gate  */
750Sstevel@tonic-gate struct	smap {
760Sstevel@tonic-gate 	kmutex_t	sm_mtx;		/* protect non-list fields */
770Sstevel@tonic-gate 	struct	vnode	*sm_vp;		/* vnode pointer (if mapped) */
780Sstevel@tonic-gate 	struct	smap	*sm_hash;	/* hash pointer */
790Sstevel@tonic-gate 	struct	smap	*sm_next;	/* next pointer */
800Sstevel@tonic-gate 	struct	smap	*sm_prev;	/* previous pointer */
810Sstevel@tonic-gate 	u_offset_t	sm_off;		/* file offset for mapping */
820Sstevel@tonic-gate 	ushort_t	sm_bitmap;	/* bit map for locked translations */
830Sstevel@tonic-gate 	ushort_t	sm_refcnt;	/* reference count for uses */
840Sstevel@tonic-gate 	ushort_t	sm_flags;	/* smap flags */
850Sstevel@tonic-gate 	ushort_t	sm_free_ndx;	/* freelist */
860Sstevel@tonic-gate #ifdef	SEGKPM_SUPPORT
870Sstevel@tonic-gate 	struct kpme	sm_kpme;	/* segkpm */
880Sstevel@tonic-gate #endif
890Sstevel@tonic-gate };
900Sstevel@tonic-gate 
910Sstevel@tonic-gate #ifdef	SEGKPM_SUPPORT
920Sstevel@tonic-gate #define	GET_KPME(smp)	(&(smp)->sm_kpme)
930Sstevel@tonic-gate #define	sm_kpme_next	sm_kpme.kpe_next
940Sstevel@tonic-gate #define	sm_kpme_prev	sm_kpme.kpe_prev
950Sstevel@tonic-gate #define	sm_kpme_page	sm_kpme.kpe_page
960Sstevel@tonic-gate #else
970Sstevel@tonic-gate #define	GET_KPME(smp)	((struct kpme *)NULL)
980Sstevel@tonic-gate #endif
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate /* sm_flags */
1010Sstevel@tonic-gate #define	SM_KPM_NEWPAGE	   0x00000001	/* page created in segmap_getmapft */
1020Sstevel@tonic-gate #define	SM_NOTKPM_RELEASED 0x00000002	/* released smap not in segkpm mode */
1030Sstevel@tonic-gate #define	SM_QNDX_ZERO	   0x00000004	/* on the index 0 freelist */
1040Sstevel@tonic-gate #define	SM_READ_DATA	   0x00000010	/* page created for read */
1050Sstevel@tonic-gate #define	SM_WRITE_DATA	   0x00000020	/* page created for write */
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate /*
1080Sstevel@tonic-gate  * Multiple smap free lists are maintained so that allocations
1090Sstevel@tonic-gate  * will scale with cpu count. Each free list is made up of 2 queues
1100Sstevel@tonic-gate  * so that allocations and deallocations can proceed concurrently.
1110Sstevel@tonic-gate  * Each queue structure is padded to 64 bytes to avoid false sharing.
1120Sstevel@tonic-gate  */
1130Sstevel@tonic-gate #define	SM_FREEQ_PAD (64 - sizeof (struct smap *) - sizeof (kmutex_t))
1140Sstevel@tonic-gate struct 	sm_freeq {
1150Sstevel@tonic-gate 	struct smap	*smq_free;	/* points into freelist */
1160Sstevel@tonic-gate 	kmutex_t	smq_mtx;	/* protects smq_free */
1170Sstevel@tonic-gate 	char		smq_pad[SM_FREEQ_PAD];
1180Sstevel@tonic-gate };
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate struct	smfree {
1210Sstevel@tonic-gate 	struct sm_freeq	sm_freeq[2];	/* alloc and release queues */
1220Sstevel@tonic-gate 	struct sm_freeq	*sm_allocq;	/* current allocq */
1230Sstevel@tonic-gate 	struct sm_freeq	*sm_releq;	/* current releq */
1240Sstevel@tonic-gate 	kcondvar_t	sm_free_cv;
1250Sstevel@tonic-gate 	ushort_t	sm_want;	/* someone wants a slot of this color */
1260Sstevel@tonic-gate };
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate /*
1290Sstevel@tonic-gate  * Cached smaps are kept on hash chains to enable fast reclaim lookups.
1300Sstevel@tonic-gate  */
1310Sstevel@tonic-gate struct  smaphash {
1320Sstevel@tonic-gate 	kmutex_t	sh_mtx;		/* protects this hash chain */
1330Sstevel@tonic-gate 	struct  smap	*sh_hash_list;  /* start of hash chain */
1340Sstevel@tonic-gate };
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate /*
1370Sstevel@tonic-gate  * (Semi) private data maintained by the segmap driver per SEGMENT mapping
1380Sstevel@tonic-gate  * All fields in segmap_data are read-only after the segment is created.
1390Sstevel@tonic-gate  *
1400Sstevel@tonic-gate  */
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate struct	segmap_data {
1430Sstevel@tonic-gate 	struct	smap	*smd_sm;	/* array of smap structures */
1440Sstevel@tonic-gate 	long		smd_npages;	/* size of smap array */
1450Sstevel@tonic-gate 	struct smfree	*smd_free;	/* ptr to freelist header array */
1460Sstevel@tonic-gate 	struct smaphash *smd_hash;	/* ptr to hash header array */
1470Sstevel@tonic-gate 	int		smd_nfree;	/* number of free lists */
1480Sstevel@tonic-gate 	uchar_t		smd_prot;	/* protections for all smap's */
1490Sstevel@tonic-gate };
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate /*
1520Sstevel@tonic-gate  * Statistics for segmap operations.
1530Sstevel@tonic-gate  *
1540Sstevel@tonic-gate  * No explicit locking to protect these stats.
1550Sstevel@tonic-gate  */
1560Sstevel@tonic-gate struct segmapcnt {
1570Sstevel@tonic-gate 	kstat_named_t	smp_fault;	/* number of segmap_faults */
1580Sstevel@tonic-gate 	kstat_named_t	smp_faulta;	/* number of segmap_faultas */
1590Sstevel@tonic-gate 	kstat_named_t	smp_getmap;	/* number of segmap_getmaps */
1600Sstevel@tonic-gate 	kstat_named_t	smp_get_use;	/* getmaps that reuse existing map */
1610Sstevel@tonic-gate 	kstat_named_t	smp_get_reclaim; /* getmaps that do a reclaim */
1620Sstevel@tonic-gate 	kstat_named_t	smp_get_reuse;	/* getmaps that reuse a slot */
1630Sstevel@tonic-gate 	kstat_named_t	smp_get_unused;	/* getmaps that reuse existing map */
1640Sstevel@tonic-gate 	kstat_named_t	smp_get_nofree;	/* getmaps with no free slots */
1650Sstevel@tonic-gate 	kstat_named_t	smp_rel_async;	/* releases that are async */
1660Sstevel@tonic-gate 	kstat_named_t	smp_rel_write;	/* releases that write */
1670Sstevel@tonic-gate 	kstat_named_t	smp_rel_free;	/* releases that free */
1680Sstevel@tonic-gate 	kstat_named_t	smp_rel_abort;	/* releases that abort */
1690Sstevel@tonic-gate 	kstat_named_t	smp_rel_dontneed; /* releases with dontneed set */
1700Sstevel@tonic-gate 	kstat_named_t	smp_release;	/* releases with no other action */
1710Sstevel@tonic-gate 	kstat_named_t	smp_pagecreate;	/* pagecreates */
1720Sstevel@tonic-gate 	kstat_named_t   smp_free_notfree; /* pages not freed in */
1730Sstevel@tonic-gate 					/* segmap_pagefree */
1740Sstevel@tonic-gate 	kstat_named_t   smp_free_dirty; /* dirty pages freeed */
1750Sstevel@tonic-gate 					/* in segmap_pagefree */
1760Sstevel@tonic-gate 	kstat_named_t   smp_free;	/* clean pages freeed in */
1770Sstevel@tonic-gate 					/* segmap_pagefree */
1780Sstevel@tonic-gate 	kstat_named_t	smp_stolen;	/* segmap_getmapflt() stole */
1790Sstevel@tonic-gate 					/* from get_free_smp() */
1800Sstevel@tonic-gate 	kstat_named_t	smp_get_nomtx;	/* free smaps but no mutex */
1810Sstevel@tonic-gate };
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate /*
1840Sstevel@tonic-gate  * These are flags used on release.  Some of these might get handled
1850Sstevel@tonic-gate  * by segment operations needed for msync (when we figure them out).
1860Sstevel@tonic-gate  * SM_ASYNC modifies SM_WRITE.  SM_DONTNEED modifies SM_FREE.  SM_FREE
1870Sstevel@tonic-gate  * and SM_INVAL as well as SM_FREE and SM_DESTROY are mutually exclusive.
1880Sstevel@tonic-gate  * SM_DESTROY behaves like SM_INVAL but also forces the pages to be
1890Sstevel@tonic-gate  * destroyed -- this prevents them from being written to the backing
1900Sstevel@tonic-gate  * store.
1910Sstevel@tonic-gate  */
1920Sstevel@tonic-gate #define	SM_WRITE	0x01		/* write back the pages upon release */
1930Sstevel@tonic-gate #define	SM_ASYNC	0x02		/* do the write asynchronously */
1940Sstevel@tonic-gate #define	SM_FREE		0x04		/* put pages back on free list */
1950Sstevel@tonic-gate #define	SM_INVAL	0x08		/* invalidate page (no caching) */
1960Sstevel@tonic-gate #define	SM_DONTNEED	0x10		/* less likely to be needed soon */
1970Sstevel@tonic-gate #define	SM_DESTROY	0x20		/* invalidate page, don't write back */
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate /*
2000Sstevel@tonic-gate  * These are the forcefault flags used on getmapflt.
2010Sstevel@tonic-gate  *
2020Sstevel@tonic-gate  * The orginal semantic was extended to allow using the segkpm mapping
2030Sstevel@tonic-gate  * scheme w/o a major segmap interface change for MAXBSIZE == PAGESIZE
2040Sstevel@tonic-gate  * (which is required to enable segkpm for MAXBSIZE > PAGESIZE).
2050Sstevel@tonic-gate  * Most segmap consumers needn't to be changed at all or only need to
2060Sstevel@tonic-gate  * be changed slightly to take advantage of segkpm. Because the segkpm
2070Sstevel@tonic-gate  * virtual address is based on the physical address of a page, a page is
2080Sstevel@tonic-gate  * required to determine the virtual address (return value). Pages mapped
2090Sstevel@tonic-gate  * with segkpm are always at least read locked and are hence protected
2100Sstevel@tonic-gate  * from pageout or fsflush from segmap_getmap until segmap_release. This
2110Sstevel@tonic-gate  * implies, that the segkpm mappings are locked within this period too.
2120Sstevel@tonic-gate  * No trap driven segmap_fault's are possible in segkpm mode.
2130Sstevel@tonic-gate  *
2140Sstevel@tonic-gate  * The following combinations of "forcefault" and "rw" allow segkpm mode.
2150Sstevel@tonic-gate  * (1) SM_FAULT, S_READ
2160Sstevel@tonic-gate  * (2) SM_FAULT, S_WRITE
2170Sstevel@tonic-gate  * (3) SM_PAGECREATE, S_WRITE
2180Sstevel@tonic-gate  * (4) SM_LOCKPROTO, {S_READ, S_WRITE, S_OTHER}
2190Sstevel@tonic-gate  *
2200Sstevel@tonic-gate  * The regular additional operations (come in pairs in most of the cases):
2210Sstevel@tonic-gate  * . segmap_pagecreate/segmap_pageunlock
2220Sstevel@tonic-gate  * . segmap_fault(F_SOFTLOCK)/segmap_fault(F_SOFTUNLOCK)
2230Sstevel@tonic-gate  *
2240Sstevel@tonic-gate  * are mostly a no-op in segkpm mode with the following exceptions:
2250Sstevel@tonic-gate  * . The "newpage" return value of segmap_pagecreate is still supported
2260Sstevel@tonic-gate  *   for zeroout operations needed on newly created pages.
2270Sstevel@tonic-gate  *
2280Sstevel@tonic-gate  * . segmap_fault() must follow when a error could be expected in
2290Sstevel@tonic-gate  *   the VOP_GETPAGE. In segkpm mode this error is recognized in
2300Sstevel@tonic-gate  *   segmap_getmapflt and returned from the following segmap_fault()
2310Sstevel@tonic-gate  *   call. The "hole" optimization (read only after first VOP_GETPAGE
2320Sstevel@tonic-gate  *   mapping in segmap_getmapflt followed by a trap driven protection
2330Sstevel@tonic-gate  *   fault and a second VOP_GETPAGE via segmap_fault) cannot be used.
2340Sstevel@tonic-gate  *
2350Sstevel@tonic-gate  * . segmap_fault(F_SOFTUNLOCK) must follow when segmap_getmapflt was
2360Sstevel@tonic-gate  *   called w/ (SM_LOCKPROTO, S_OTHER). S_WRITE has to be applied, when
2370Sstevel@tonic-gate  *   the page should be marked "dirty". Otherwise the page is not
2380Sstevel@tonic-gate  *   written to the backing store later (as mentioned above, no page
2390Sstevel@tonic-gate  *   or protection faults are possible in segkpm mode). Caller cannot
2400Sstevel@tonic-gate  *   use only S_OTHER and rely on a protection fault to force the page
2410Sstevel@tonic-gate  *   to become dirty.
2420Sstevel@tonic-gate  *
2430Sstevel@tonic-gate  * . The segmap_pagecreate parameter softlock is ignored, pages and
2440Sstevel@tonic-gate  *   mappings are locked anyway.
2450Sstevel@tonic-gate  *
2460Sstevel@tonic-gate  * SM_LOCKPROTO is used in the fbio layer and some special segmap consumers.
2470Sstevel@tonic-gate  */
2480Sstevel@tonic-gate #define	SM_PAGECREATE	0x00		/* create page in segkpm mode, no I/O */
2490Sstevel@tonic-gate #define	SM_FAULT	0x01		/* fault in page if necessary */
2500Sstevel@tonic-gate #define	SM_LOCKPROTO	0x02		/* lock/unlock protocol used */
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate #define	MAXBSHIFT	13		/* log2(MAXBSIZE) */
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate #define	MAXBOFFSET	(MAXBSIZE - 1)
2550Sstevel@tonic-gate #define	MAXBMASK	(~MAXBOFFSET)
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate /*
2580Sstevel@tonic-gate  * SMAP_HASHAVELEN is the average length desired for this chain, from
2590Sstevel@tonic-gate  * which the size of the smd_hash table is derived at segment create time.
2600Sstevel@tonic-gate  * SMAP_HASHVPSHIFT is defined so that 1 << SMAP_HASHVPSHIFT is the
2610Sstevel@tonic-gate  * approximate size of a vnode struct.
2620Sstevel@tonic-gate  */
2630Sstevel@tonic-gate #define	SMAP_HASHAVELEN		4
2640Sstevel@tonic-gate #define	SMAP_HASHVPSHIFT	6
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate #ifdef _KERNEL
2680Sstevel@tonic-gate /*
2690Sstevel@tonic-gate  * The kernel generic mapping segment.
2700Sstevel@tonic-gate  */
2710Sstevel@tonic-gate extern struct seg *segkmap;
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate /*
2740Sstevel@tonic-gate  * Public seg_map segment operations.
2750Sstevel@tonic-gate  */
2760Sstevel@tonic-gate extern int	segmap_create(struct seg *, void *);
2770Sstevel@tonic-gate extern int	segmap_pagecreate(struct seg *, caddr_t, size_t, int);
2780Sstevel@tonic-gate extern void	segmap_pageunlock(struct seg *, caddr_t, size_t, enum seg_rw);
2790Sstevel@tonic-gate extern faultcode_t segmap_fault(struct hat *, struct seg *, caddr_t, size_t,
2800Sstevel@tonic-gate 		enum fault_type, enum seg_rw);
2810Sstevel@tonic-gate extern caddr_t	segmap_getmap(struct seg *, struct vnode *, u_offset_t);
2820Sstevel@tonic-gate extern caddr_t	segmap_getmapflt(struct seg *, struct vnode *, u_offset_t,
2830Sstevel@tonic-gate 		size_t, int, enum seg_rw);
2840Sstevel@tonic-gate extern int	segmap_release(struct seg *, caddr_t, uint_t);
2850Sstevel@tonic-gate extern void	segmap_flush(struct seg *, struct vnode *);
2860Sstevel@tonic-gate extern void	segmap_inval(struct seg *, struct vnode *, u_offset_t);
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate #endif	/* _KERNEL */
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate #ifdef	__cplusplus
2910Sstevel@tonic-gate }
2920Sstevel@tonic-gate #endif
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate #endif	/* _VM_SEG_MAP_H */
295