xref: /onnv-gate/usr/src/uts/common/vm/seg.h (revision 6695:12d7dd4459fd)
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
53247Sgjelinek  * Common Development and Distribution License (the "License").
63247Sgjelinek  * 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*6695Saguzovsk  * Copyright 2008 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_H
400Sstevel@tonic-gate #define	_VM_SEG_H
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #include <sys/vnode.h>
450Sstevel@tonic-gate #include <sys/avl.h>
460Sstevel@tonic-gate #include <vm/seg_enum.h>
470Sstevel@tonic-gate #include <vm/faultcode.h>
480Sstevel@tonic-gate #include <vm/hat.h>
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #ifdef	__cplusplus
510Sstevel@tonic-gate extern "C" {
520Sstevel@tonic-gate #endif
530Sstevel@tonic-gate 
540Sstevel@tonic-gate /*
550Sstevel@tonic-gate  * VM - Segments.
560Sstevel@tonic-gate  */
570Sstevel@tonic-gate 
58*6695Saguzovsk struct anon_map;
59*6695Saguzovsk 
600Sstevel@tonic-gate /*
610Sstevel@tonic-gate  * kstat statistics for segment advise
620Sstevel@tonic-gate  */
630Sstevel@tonic-gate typedef struct {
640Sstevel@tonic-gate 	kstat_named_t MADV_FREE_hit;
650Sstevel@tonic-gate 	kstat_named_t MADV_FREE_miss;
660Sstevel@tonic-gate } segadvstat_t;
670Sstevel@tonic-gate 
680Sstevel@tonic-gate /*
690Sstevel@tonic-gate  * memory object ids
700Sstevel@tonic-gate  */
710Sstevel@tonic-gate typedef struct memid { u_longlong_t val[2]; } memid_t;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate  * An address space contains a set of segments, managed by drivers.
750Sstevel@tonic-gate  * Drivers support mapped devices, sharing, copy-on-write, etc.
760Sstevel@tonic-gate  *
770Sstevel@tonic-gate  * The seg structure contains a lock to prevent races, the base virtual
780Sstevel@tonic-gate  * address and size of the segment, a back pointer to the containing
790Sstevel@tonic-gate  * address space, pointers to maintain an AVL tree of segments in the
800Sstevel@tonic-gate  * same address space, and procedure and data hooks for the driver.
810Sstevel@tonic-gate  * The AVL tree of segments for the address space is sorted by
820Sstevel@tonic-gate  * ascending base addresses and overlapping segments are not allowed.
830Sstevel@tonic-gate  *
840Sstevel@tonic-gate  * After a segment is created, faults may occur on pages of the segment.
850Sstevel@tonic-gate  * When a fault occurs, the fault handling code must get the desired
860Sstevel@tonic-gate  * object and set up the hardware translation to the object.  For some
870Sstevel@tonic-gate  * objects, the fault handling code also implements copy-on-write.
880Sstevel@tonic-gate  *
890Sstevel@tonic-gate  * When the hat wants to unload a translation, it can call the unload
900Sstevel@tonic-gate  * routine which is responsible for processing reference and modify bits.
910Sstevel@tonic-gate  *
920Sstevel@tonic-gate  * Each segment is protected by it's containing address space lock.  To
930Sstevel@tonic-gate  * access any field in the segment structure, the "as" must be locked.
940Sstevel@tonic-gate  * If a segment field is to be modified, the address space lock must be
950Sstevel@tonic-gate  * write locked.
960Sstevel@tonic-gate  */
970Sstevel@tonic-gate 
98*6695Saguzovsk typedef struct pcache_link {
99*6695Saguzovsk 	struct pcache_link	*p_lnext;
100*6695Saguzovsk 	struct pcache_link	*p_lprev;
101*6695Saguzovsk } pcache_link_t;
102*6695Saguzovsk 
103*6695Saguzovsk typedef struct seg {
1040Sstevel@tonic-gate 	caddr_t	s_base;			/* base virtual address */
1050Sstevel@tonic-gate 	size_t	s_size;			/* size in bytes */
1060Sstevel@tonic-gate 	uint_t	s_szc;			/* max page size code */
1070Sstevel@tonic-gate 	uint_t	s_flags;		/* flags for segment, see below */
1080Sstevel@tonic-gate 	struct	as *s_as;		/* containing address space */
1090Sstevel@tonic-gate 	avl_node_t s_tree;		/* AVL tree links to segs in this as */
1100Sstevel@tonic-gate 	struct	seg_ops *s_ops;		/* ops vector: see below */
1110Sstevel@tonic-gate 	void *s_data;			/* private data for instance */
112*6695Saguzovsk 	kmutex_t s_pmtx;		/* protects seg's pcache list */
113*6695Saguzovsk 	pcache_link_t s_phead;		/* head of seg's pcache list */
114*6695Saguzovsk } seg_t;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate #define	S_PURGE		(0x01)		/* seg should be purged in as_gap() */
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate struct	seg_ops {
1190Sstevel@tonic-gate 	int	(*dup)(struct seg *, struct seg *);
1200Sstevel@tonic-gate 	int	(*unmap)(struct seg *, caddr_t, size_t);
1210Sstevel@tonic-gate 	void	(*free)(struct seg *);
1220Sstevel@tonic-gate 	faultcode_t (*fault)(struct hat *, struct seg *, caddr_t, size_t,
1230Sstevel@tonic-gate 	    enum fault_type, enum seg_rw);
1240Sstevel@tonic-gate 	faultcode_t (*faulta)(struct seg *, caddr_t);
1250Sstevel@tonic-gate 	int	(*setprot)(struct seg *, caddr_t, size_t, uint_t);
1260Sstevel@tonic-gate 	int	(*checkprot)(struct seg *, caddr_t, size_t, uint_t);
1270Sstevel@tonic-gate 	int	(*kluster)(struct seg *, caddr_t, ssize_t);
1280Sstevel@tonic-gate 	size_t	(*swapout)(struct seg *);
1290Sstevel@tonic-gate 	int	(*sync)(struct seg *, caddr_t, size_t, int, uint_t);
1300Sstevel@tonic-gate 	size_t	(*incore)(struct seg *, caddr_t, size_t, char *);
1310Sstevel@tonic-gate 	int	(*lockop)(struct seg *, caddr_t, size_t, int, int, ulong_t *,
1320Sstevel@tonic-gate 			size_t);
1330Sstevel@tonic-gate 	int	(*getprot)(struct seg *, caddr_t, size_t, uint_t *);
1340Sstevel@tonic-gate 	u_offset_t	(*getoffset)(struct seg *, caddr_t);
1350Sstevel@tonic-gate 	int	(*gettype)(struct seg *, caddr_t);
1360Sstevel@tonic-gate 	int	(*getvp)(struct seg *, caddr_t, struct vnode **);
1370Sstevel@tonic-gate 	int	(*advise)(struct seg *, caddr_t, size_t, uint_t);
1380Sstevel@tonic-gate 	void	(*dump)(struct seg *);
1390Sstevel@tonic-gate 	int	(*pagelock)(struct seg *, caddr_t, size_t, struct page ***,
1400Sstevel@tonic-gate 			enum lock_type, enum seg_rw);
1410Sstevel@tonic-gate 	int	(*setpagesize)(struct seg *, caddr_t, size_t, uint_t);
1420Sstevel@tonic-gate 	int	(*getmemid)(struct seg *, caddr_t, memid_t *);
1430Sstevel@tonic-gate 	struct lgrp_mem_policy_info	*(*getpolicy)(struct seg *, caddr_t);
144670Selowe 	int	(*capable)(struct seg *, segcapability_t);
1450Sstevel@tonic-gate };
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate #ifdef _KERNEL
148*6695Saguzovsk 
1490Sstevel@tonic-gate /*
1500Sstevel@tonic-gate  * Generic segment operations
1510Sstevel@tonic-gate  */
1520Sstevel@tonic-gate extern	void	seg_init(void);
1530Sstevel@tonic-gate extern	struct	seg *seg_alloc(struct as *as, caddr_t base, size_t size);
1540Sstevel@tonic-gate extern	int	seg_attach(struct as *as, caddr_t base, size_t size,
1550Sstevel@tonic-gate 			struct seg *seg);
1560Sstevel@tonic-gate extern	void	seg_unmap(struct seg *seg);
1570Sstevel@tonic-gate extern	void	seg_free(struct seg *seg);
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate /*
1600Sstevel@tonic-gate  * functions for pagelock cache support
1610Sstevel@tonic-gate  */
162*6695Saguzovsk typedef	int (*seg_preclaim_cbfunc_t)(void *, caddr_t, size_t,
163*6695Saguzovsk     struct page **, enum seg_rw, int);
164*6695Saguzovsk 
165*6695Saguzovsk extern	struct	page **seg_plookup(struct seg *seg, struct anon_map *amp,
166*6695Saguzovsk     caddr_t addr, size_t len, enum seg_rw rw, uint_t flags);
167*6695Saguzovsk extern	void	seg_pinactive(struct seg *seg, struct anon_map *amp,
168*6695Saguzovsk     caddr_t addr, size_t len, struct page **pp, enum seg_rw rw,
169*6695Saguzovsk     uint_t flags, seg_preclaim_cbfunc_t callback);
170*6695Saguzovsk 
171*6695Saguzovsk extern	void	seg_ppurge(struct seg *seg, struct anon_map *amp,
172*6695Saguzovsk     uint_t flags);
173*6695Saguzovsk extern	void	seg_ppurge_wiredpp(struct page **pp);
174*6695Saguzovsk 
175*6695Saguzovsk extern	int	seg_pinsert_check(struct seg *seg, struct anon_map *amp,
176*6695Saguzovsk     caddr_t addr, size_t len, uint_t flags);
177*6695Saguzovsk extern	int	seg_pinsert(struct seg *seg, struct anon_map *amp,
178*6695Saguzovsk     caddr_t addr, size_t len, size_t wlen, struct page **pp, enum seg_rw rw,
179*6695Saguzovsk     uint_t flags, seg_preclaim_cbfunc_t callback);
180*6695Saguzovsk 
1810Sstevel@tonic-gate extern	void	seg_pasync_thread(void);
1820Sstevel@tonic-gate extern	void	seg_preap(void);
1833480Sjfrank extern	int	seg_p_disable(void);
1843480Sjfrank extern	void	seg_p_enable(void);
1850Sstevel@tonic-gate 
186*6695Saguzovsk extern	segadvstat_t	segadvstat;
187*6695Saguzovsk 
1880Sstevel@tonic-gate /*
189*6695Saguzovsk  * Flags for pagelock cache support.
190*6695Saguzovsk  * Flags argument is passed as uint_t to pcache routines.  upper 16 bits of
191*6695Saguzovsk  * the flags argument are reserved for alignment page shift when SEGP_PSHIFT
192*6695Saguzovsk  * is set.
1930Sstevel@tonic-gate  */
194*6695Saguzovsk #define	SEGP_FORCE_WIRED	0x1	/* skip check against seg_pwindow */
195*6695Saguzovsk #define	SEGP_AMP		0x2	/* anon map's pcache entry */
196*6695Saguzovsk #define	SEGP_PSHIFT		0x4	/* addr pgsz shift for hash function */
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate /*
1990Sstevel@tonic-gate  * Return values for seg_pinsert and seg_pinsert_check functions.
2000Sstevel@tonic-gate  */
2010Sstevel@tonic-gate #define	SEGP_SUCCESS		0	/* seg_pinsert() succeeded */
2020Sstevel@tonic-gate #define	SEGP_FAIL		1	/* seg_pinsert() failed */
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate /* Page status bits for segop_incore */
2050Sstevel@tonic-gate #define	SEG_PAGE_INCORE		0x01	/* VA has a page backing it */
2060Sstevel@tonic-gate #define	SEG_PAGE_LOCKED		0x02	/* VA has a page that is locked */
2070Sstevel@tonic-gate #define	SEG_PAGE_HASCOW		0x04	/* VA has a page with a copy-on-write */
2080Sstevel@tonic-gate #define	SEG_PAGE_SOFTLOCK	0x08	/* VA has a page with softlock held */
2090Sstevel@tonic-gate #define	SEG_PAGE_VNODEBACKED	0x10	/* Segment is backed by a vnode */
2100Sstevel@tonic-gate #define	SEG_PAGE_ANON		0x20	/* VA has an anonymous page */
2110Sstevel@tonic-gate #define	SEG_PAGE_VNODE		0x40	/* VA has a vnode page backing it */
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate #define	SEGOP_DUP(s, n)		    (*(s)->s_ops->dup)((s), (n))
2140Sstevel@tonic-gate #define	SEGOP_UNMAP(s, a, l)	    (*(s)->s_ops->unmap)((s), (a), (l))
2150Sstevel@tonic-gate #define	SEGOP_FREE(s)		    (*(s)->s_ops->free)((s))
2160Sstevel@tonic-gate #define	SEGOP_FAULT(h, s, a, l, t, rw) \
2170Sstevel@tonic-gate 		(*(s)->s_ops->fault)((h), (s), (a), (l), (t), (rw))
2180Sstevel@tonic-gate #define	SEGOP_FAULTA(s, a)	    (*(s)->s_ops->faulta)((s), (a))
2190Sstevel@tonic-gate #define	SEGOP_SETPROT(s, a, l, p)   (*(s)->s_ops->setprot)((s), (a), (l), (p))
2200Sstevel@tonic-gate #define	SEGOP_CHECKPROT(s, a, l, p) (*(s)->s_ops->checkprot)((s), (a), (l), (p))
2210Sstevel@tonic-gate #define	SEGOP_KLUSTER(s, a, d)	    (*(s)->s_ops->kluster)((s), (a), (d))
2220Sstevel@tonic-gate #define	SEGOP_SWAPOUT(s)	    (*(s)->s_ops->swapout)((s))
2230Sstevel@tonic-gate #define	SEGOP_SYNC(s, a, l, atr, f) \
2240Sstevel@tonic-gate 		(*(s)->s_ops->sync)((s), (a), (l), (atr), (f))
2250Sstevel@tonic-gate #define	SEGOP_INCORE(s, a, l, v)    (*(s)->s_ops->incore)((s), (a), (l), (v))
2260Sstevel@tonic-gate #define	SEGOP_LOCKOP(s, a, l, atr, op, b, p) \
2270Sstevel@tonic-gate 		(*(s)->s_ops->lockop)((s), (a), (l), (atr), (op), (b), (p))
2280Sstevel@tonic-gate #define	SEGOP_GETPROT(s, a, l, p)   (*(s)->s_ops->getprot)((s), (a), (l), (p))
2290Sstevel@tonic-gate #define	SEGOP_GETOFFSET(s, a)	    (*(s)->s_ops->getoffset)((s), (a))
2300Sstevel@tonic-gate #define	SEGOP_GETTYPE(s, a)	    (*(s)->s_ops->gettype)((s), (a))
2310Sstevel@tonic-gate #define	SEGOP_GETVP(s, a, vpp)	    (*(s)->s_ops->getvp)((s), (a), (vpp))
2320Sstevel@tonic-gate #define	SEGOP_ADVISE(s, a, l, b)    (*(s)->s_ops->advise)((s), (a), (l), (b))
2330Sstevel@tonic-gate #define	SEGOP_DUMP(s)		    (*(s)->s_ops->dump)((s))
2340Sstevel@tonic-gate #define	SEGOP_PAGELOCK(s, a, l, p, t, rw) \
2350Sstevel@tonic-gate 		(*(s)->s_ops->pagelock)((s), (a), (l), (p), (t), (rw))
2360Sstevel@tonic-gate #define	SEGOP_SETPAGESIZE(s, a, l, szc) \
2370Sstevel@tonic-gate 		(*(s)->s_ops->setpagesize)((s), (a), (l), (szc))
2380Sstevel@tonic-gate #define	SEGOP_GETMEMID(s, a, mp)    (*(s)->s_ops->getmemid)((s), (a), (mp))
2390Sstevel@tonic-gate #define	SEGOP_GETPOLICY(s, a)	    (*(s)->s_ops->getpolicy)((s), (a))
240670Selowe #define	SEGOP_CAPABLE(s, c)	    (*(s)->s_ops->capable)((s), (c))
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate #define	seg_page(seg, addr) \
2430Sstevel@tonic-gate 	(((uintptr_t)((addr) - (seg)->s_base)) >> PAGESHIFT)
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate #define	seg_pages(seg) \
2460Sstevel@tonic-gate 	(((uintptr_t)((seg)->s_size + PAGEOFFSET)) >> PAGESHIFT)
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate #define	IE_NOMEM	-1	/* internal to seg layer */
2490Sstevel@tonic-gate #define	IE_RETRY	-2	/* internal to seg layer */
2500Sstevel@tonic-gate #define	IE_REATTACH	-3	/* internal to seg layer */
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate /* Delay/retry factors for seg_p_mem_config_pre_del */
2530Sstevel@tonic-gate #define	SEGP_PREDEL_DELAY_FACTOR	4
2540Sstevel@tonic-gate /*
2550Sstevel@tonic-gate  * As a workaround to being unable to purge the pagelock
2560Sstevel@tonic-gate  * cache during a DR delete memory operation, we use
2570Sstevel@tonic-gate  * a stall threshold that is twice the maximum seen
2580Sstevel@tonic-gate  * during testing.  This workaround will be removed
2590Sstevel@tonic-gate  * when a suitable fix is found.
2600Sstevel@tonic-gate  */
2610Sstevel@tonic-gate #define	SEGP_STALL_SECONDS	25
2620Sstevel@tonic-gate #define	SEGP_STALL_THRESHOLD \
2630Sstevel@tonic-gate 	(SEGP_STALL_SECONDS * SEGP_PREDEL_DELAY_FACTOR)
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate #ifdef VMDEBUG
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate uint_t	seg_page(struct seg *, caddr_t);
2680Sstevel@tonic-gate uint_t	seg_pages(struct seg *);
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate #endif	/* VMDEBUG */
2710Sstevel@tonic-gate 
2723247Sgjelinek boolean_t	seg_can_change_zones(struct seg *);
2733247Sgjelinek size_t		seg_swresv(struct seg *);
2743247Sgjelinek 
2750Sstevel@tonic-gate #endif	/* _KERNEL */
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate #ifdef	__cplusplus
2780Sstevel@tonic-gate }
2790Sstevel@tonic-gate #endif
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate #endif	/* _VM_SEG_H */
282