xref: /onnv-gate/usr/src/uts/common/vm/seg.h (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*0Sstevel@tonic-gate  * The Regents of the University of California
33*0Sstevel@tonic-gate  * All Rights Reserved
34*0Sstevel@tonic-gate  *
35*0Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*0Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*0Sstevel@tonic-gate  * contributors.
38*0Sstevel@tonic-gate  */
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #ifndef	_VM_SEG_H
41*0Sstevel@tonic-gate #define	_VM_SEG_H
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate #include <sys/vnode.h>
46*0Sstevel@tonic-gate #include <sys/avl.h>
47*0Sstevel@tonic-gate #include <vm/seg_enum.h>
48*0Sstevel@tonic-gate #include <vm/faultcode.h>
49*0Sstevel@tonic-gate #include <vm/hat.h>
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate #ifdef	__cplusplus
52*0Sstevel@tonic-gate extern "C" {
53*0Sstevel@tonic-gate #endif
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate /*
56*0Sstevel@tonic-gate  * VM - Segments.
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate /*
60*0Sstevel@tonic-gate  * kstat statistics for segment advise
61*0Sstevel@tonic-gate  */
62*0Sstevel@tonic-gate typedef struct {
63*0Sstevel@tonic-gate 	kstat_named_t MADV_FREE_hit;
64*0Sstevel@tonic-gate 	kstat_named_t MADV_FREE_miss;
65*0Sstevel@tonic-gate } segadvstat_t;
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate /*
68*0Sstevel@tonic-gate  * memory object ids
69*0Sstevel@tonic-gate  */
70*0Sstevel@tonic-gate typedef struct memid { u_longlong_t val[2]; } memid_t;
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate /*
73*0Sstevel@tonic-gate  * An address space contains a set of segments, managed by drivers.
74*0Sstevel@tonic-gate  * Drivers support mapped devices, sharing, copy-on-write, etc.
75*0Sstevel@tonic-gate  *
76*0Sstevel@tonic-gate  * The seg structure contains a lock to prevent races, the base virtual
77*0Sstevel@tonic-gate  * address and size of the segment, a back pointer to the containing
78*0Sstevel@tonic-gate  * address space, pointers to maintain an AVL tree of segments in the
79*0Sstevel@tonic-gate  * same address space, and procedure and data hooks for the driver.
80*0Sstevel@tonic-gate  * The AVL tree of segments for the address space is sorted by
81*0Sstevel@tonic-gate  * ascending base addresses and overlapping segments are not allowed.
82*0Sstevel@tonic-gate  *
83*0Sstevel@tonic-gate  * After a segment is created, faults may occur on pages of the segment.
84*0Sstevel@tonic-gate  * When a fault occurs, the fault handling code must get the desired
85*0Sstevel@tonic-gate  * object and set up the hardware translation to the object.  For some
86*0Sstevel@tonic-gate  * objects, the fault handling code also implements copy-on-write.
87*0Sstevel@tonic-gate  *
88*0Sstevel@tonic-gate  * When the hat wants to unload a translation, it can call the unload
89*0Sstevel@tonic-gate  * routine which is responsible for processing reference and modify bits.
90*0Sstevel@tonic-gate  *
91*0Sstevel@tonic-gate  * Each segment is protected by it's containing address space lock.  To
92*0Sstevel@tonic-gate  * access any field in the segment structure, the "as" must be locked.
93*0Sstevel@tonic-gate  * If a segment field is to be modified, the address space lock must be
94*0Sstevel@tonic-gate  * write locked.
95*0Sstevel@tonic-gate  */
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate struct seg {
98*0Sstevel@tonic-gate 	caddr_t	s_base;			/* base virtual address */
99*0Sstevel@tonic-gate 	size_t	s_size;			/* size in bytes */
100*0Sstevel@tonic-gate 	uint_t	s_szc;			/* max page size code */
101*0Sstevel@tonic-gate 	uint_t	s_flags;		/* flags for segment, see below */
102*0Sstevel@tonic-gate 	struct	as *s_as;		/* containing address space */
103*0Sstevel@tonic-gate 	avl_node_t s_tree;		/* AVL tree links to segs in this as */
104*0Sstevel@tonic-gate 	struct	seg_ops *s_ops;		/* ops vector: see below */
105*0Sstevel@tonic-gate 	void *s_data;			/* private data for instance */
106*0Sstevel@tonic-gate };
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate #define	S_PURGE		(0x01)		/* seg should be purged in as_gap() */
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate struct	seg_ops {
111*0Sstevel@tonic-gate 	int	(*dup)(struct seg *, struct seg *);
112*0Sstevel@tonic-gate 	int	(*unmap)(struct seg *, caddr_t, size_t);
113*0Sstevel@tonic-gate 	void	(*free)(struct seg *);
114*0Sstevel@tonic-gate 	faultcode_t (*fault)(struct hat *, struct seg *, caddr_t, size_t,
115*0Sstevel@tonic-gate 	    enum fault_type, enum seg_rw);
116*0Sstevel@tonic-gate 	faultcode_t (*faulta)(struct seg *, caddr_t);
117*0Sstevel@tonic-gate 	int	(*setprot)(struct seg *, caddr_t, size_t, uint_t);
118*0Sstevel@tonic-gate 	int	(*checkprot)(struct seg *, caddr_t, size_t, uint_t);
119*0Sstevel@tonic-gate 	int	(*kluster)(struct seg *, caddr_t, ssize_t);
120*0Sstevel@tonic-gate 	size_t	(*swapout)(struct seg *);
121*0Sstevel@tonic-gate 	int	(*sync)(struct seg *, caddr_t, size_t, int, uint_t);
122*0Sstevel@tonic-gate 	size_t	(*incore)(struct seg *, caddr_t, size_t, char *);
123*0Sstevel@tonic-gate 	int	(*lockop)(struct seg *, caddr_t, size_t, int, int, ulong_t *,
124*0Sstevel@tonic-gate 			size_t);
125*0Sstevel@tonic-gate 	int	(*getprot)(struct seg *, caddr_t, size_t, uint_t *);
126*0Sstevel@tonic-gate 	u_offset_t	(*getoffset)(struct seg *, caddr_t);
127*0Sstevel@tonic-gate 	int	(*gettype)(struct seg *, caddr_t);
128*0Sstevel@tonic-gate 	int	(*getvp)(struct seg *, caddr_t, struct vnode **);
129*0Sstevel@tonic-gate 	int	(*advise)(struct seg *, caddr_t, size_t, uint_t);
130*0Sstevel@tonic-gate 	void	(*dump)(struct seg *);
131*0Sstevel@tonic-gate 	int	(*pagelock)(struct seg *, caddr_t, size_t, struct page ***,
132*0Sstevel@tonic-gate 			enum lock_type, enum seg_rw);
133*0Sstevel@tonic-gate 	int	(*setpagesize)(struct seg *, caddr_t, size_t, uint_t);
134*0Sstevel@tonic-gate 	int	(*getmemid)(struct seg *, caddr_t, memid_t *);
135*0Sstevel@tonic-gate 	struct lgrp_mem_policy_info	*(*getpolicy)(struct seg *, caddr_t);
136*0Sstevel@tonic-gate };
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate #ifdef _KERNEL
139*0Sstevel@tonic-gate /*
140*0Sstevel@tonic-gate  * Generic segment operations
141*0Sstevel@tonic-gate  */
142*0Sstevel@tonic-gate extern	void	seg_init(void);
143*0Sstevel@tonic-gate extern	struct	seg *seg_alloc(struct as *as, caddr_t base, size_t size);
144*0Sstevel@tonic-gate extern	int	seg_attach(struct as *as, caddr_t base, size_t size,
145*0Sstevel@tonic-gate 			struct seg *seg);
146*0Sstevel@tonic-gate extern	void	seg_unmap(struct seg *seg);
147*0Sstevel@tonic-gate extern	void	seg_free(struct seg *seg);
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate /*
150*0Sstevel@tonic-gate  * functions for pagelock cache support
151*0Sstevel@tonic-gate  */
152*0Sstevel@tonic-gate extern	void	seg_ppurge(struct seg *seg);
153*0Sstevel@tonic-gate extern	void	seg_ppurge_seg(int (*callback)());
154*0Sstevel@tonic-gate extern	void	seg_pinactive(struct seg *seg, caddr_t addr, size_t len,
155*0Sstevel@tonic-gate 			struct page **pp, enum seg_rw rw, int (*callback)());
156*0Sstevel@tonic-gate extern	int	seg_pinsert_check(struct seg *seg, size_t len, uint_t flags);
157*0Sstevel@tonic-gate extern	int	seg_pinsert(struct seg *seg, caddr_t addr, size_t len,
158*0Sstevel@tonic-gate 			struct page **pp, enum seg_rw rw, uint_t flags,
159*0Sstevel@tonic-gate 			int (*callback)());
160*0Sstevel@tonic-gate extern	struct	page **seg_plookup(struct seg *seg, caddr_t addr,
161*0Sstevel@tonic-gate 			size_t len, enum seg_rw rw);
162*0Sstevel@tonic-gate extern	void	seg_pasync_thread(void);
163*0Sstevel@tonic-gate extern	void	seg_preap(void);
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate extern	int	seg_preapahead;
166*0Sstevel@tonic-gate extern	segadvstat_t  segadvstat;
167*0Sstevel@tonic-gate /*
168*0Sstevel@tonic-gate  * Flags for pagelock cache support
169*0Sstevel@tonic-gate  */
170*0Sstevel@tonic-gate #define	SEGP_ASYNC_FLUSH	0x1	/* flushed by async thread */
171*0Sstevel@tonic-gate #define	SEGP_FORCE_WIRED	0x2	/* skip check against seg_pwindow */
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate /*
174*0Sstevel@tonic-gate  * Return values for seg_pinsert and seg_pinsert_check functions.
175*0Sstevel@tonic-gate  */
176*0Sstevel@tonic-gate #define	SEGP_SUCCESS		0	/* seg_pinsert() succeeded */
177*0Sstevel@tonic-gate #define	SEGP_FAIL		1	/* seg_pinsert() failed */
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate /* Page status bits for segop_incore */
180*0Sstevel@tonic-gate #define	SEG_PAGE_INCORE		0x01	/* VA has a page backing it */
181*0Sstevel@tonic-gate #define	SEG_PAGE_LOCKED		0x02	/* VA has a page that is locked */
182*0Sstevel@tonic-gate #define	SEG_PAGE_HASCOW		0x04	/* VA has a page with a copy-on-write */
183*0Sstevel@tonic-gate #define	SEG_PAGE_SOFTLOCK	0x08	/* VA has a page with softlock held */
184*0Sstevel@tonic-gate #define	SEG_PAGE_VNODEBACKED	0x10	/* Segment is backed by a vnode */
185*0Sstevel@tonic-gate #define	SEG_PAGE_ANON		0x20	/* VA has an anonymous page */
186*0Sstevel@tonic-gate #define	SEG_PAGE_VNODE		0x40	/* VA has a vnode page backing it */
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate #define	SEGOP_DUP(s, n)		    (*(s)->s_ops->dup)((s), (n))
189*0Sstevel@tonic-gate #define	SEGOP_UNMAP(s, a, l)	    (*(s)->s_ops->unmap)((s), (a), (l))
190*0Sstevel@tonic-gate #define	SEGOP_FREE(s)		    (*(s)->s_ops->free)((s))
191*0Sstevel@tonic-gate #define	SEGOP_FAULT(h, s, a, l, t, rw) \
192*0Sstevel@tonic-gate 		(*(s)->s_ops->fault)((h), (s), (a), (l), (t), (rw))
193*0Sstevel@tonic-gate #define	SEGOP_FAULTA(s, a)	    (*(s)->s_ops->faulta)((s), (a))
194*0Sstevel@tonic-gate #define	SEGOP_SETPROT(s, a, l, p)   (*(s)->s_ops->setprot)((s), (a), (l), (p))
195*0Sstevel@tonic-gate #define	SEGOP_CHECKPROT(s, a, l, p) (*(s)->s_ops->checkprot)((s), (a), (l), (p))
196*0Sstevel@tonic-gate #define	SEGOP_KLUSTER(s, a, d)	    (*(s)->s_ops->kluster)((s), (a), (d))
197*0Sstevel@tonic-gate #define	SEGOP_SWAPOUT(s)	    (*(s)->s_ops->swapout)((s))
198*0Sstevel@tonic-gate #define	SEGOP_SYNC(s, a, l, atr, f) \
199*0Sstevel@tonic-gate 		(*(s)->s_ops->sync)((s), (a), (l), (atr), (f))
200*0Sstevel@tonic-gate #define	SEGOP_INCORE(s, a, l, v)    (*(s)->s_ops->incore)((s), (a), (l), (v))
201*0Sstevel@tonic-gate #define	SEGOP_LOCKOP(s, a, l, atr, op, b, p) \
202*0Sstevel@tonic-gate 		(*(s)->s_ops->lockop)((s), (a), (l), (atr), (op), (b), (p))
203*0Sstevel@tonic-gate #define	SEGOP_GETPROT(s, a, l, p)   (*(s)->s_ops->getprot)((s), (a), (l), (p))
204*0Sstevel@tonic-gate #define	SEGOP_GETOFFSET(s, a)	    (*(s)->s_ops->getoffset)((s), (a))
205*0Sstevel@tonic-gate #define	SEGOP_GETTYPE(s, a)	    (*(s)->s_ops->gettype)((s), (a))
206*0Sstevel@tonic-gate #define	SEGOP_GETVP(s, a, vpp)	    (*(s)->s_ops->getvp)((s), (a), (vpp))
207*0Sstevel@tonic-gate #define	SEGOP_ADVISE(s, a, l, b)    (*(s)->s_ops->advise)((s), (a), (l), (b))
208*0Sstevel@tonic-gate #define	SEGOP_DUMP(s)		    (*(s)->s_ops->dump)((s))
209*0Sstevel@tonic-gate #define	SEGOP_PAGELOCK(s, a, l, p, t, rw) \
210*0Sstevel@tonic-gate 		(*(s)->s_ops->pagelock)((s), (a), (l), (p), (t), (rw))
211*0Sstevel@tonic-gate #define	SEGOP_SETPAGESIZE(s, a, l, szc) \
212*0Sstevel@tonic-gate 		(*(s)->s_ops->setpagesize)((s), (a), (l), (szc))
213*0Sstevel@tonic-gate #define	SEGOP_GETMEMID(s, a, mp)    (*(s)->s_ops->getmemid)((s), (a), (mp))
214*0Sstevel@tonic-gate #define	SEGOP_GETPOLICY(s, a)	    (*(s)->s_ops->getpolicy)((s), (a))
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate #define	seg_page(seg, addr) \
217*0Sstevel@tonic-gate 	(((uintptr_t)((addr) - (seg)->s_base)) >> PAGESHIFT)
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate #define	seg_pages(seg) \
220*0Sstevel@tonic-gate 	(((uintptr_t)((seg)->s_size + PAGEOFFSET)) >> PAGESHIFT)
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate #define	IE_NOMEM	-1	/* internal to seg layer */
223*0Sstevel@tonic-gate #define	IE_RETRY	-2	/* internal to seg layer */
224*0Sstevel@tonic-gate #define	IE_REATTACH	-3	/* internal to seg layer */
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate /* Delay/retry factors for seg_p_mem_config_pre_del */
227*0Sstevel@tonic-gate #define	SEGP_PREDEL_DELAY_FACTOR	4
228*0Sstevel@tonic-gate /*
229*0Sstevel@tonic-gate  * As a workaround to being unable to purge the pagelock
230*0Sstevel@tonic-gate  * cache during a DR delete memory operation, we use
231*0Sstevel@tonic-gate  * a stall threshold that is twice the maximum seen
232*0Sstevel@tonic-gate  * during testing.  This workaround will be removed
233*0Sstevel@tonic-gate  * when a suitable fix is found.
234*0Sstevel@tonic-gate  */
235*0Sstevel@tonic-gate #define	SEGP_STALL_SECONDS	25
236*0Sstevel@tonic-gate #define	SEGP_STALL_THRESHOLD \
237*0Sstevel@tonic-gate 	(SEGP_STALL_SECONDS * SEGP_PREDEL_DELAY_FACTOR)
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate #ifdef VMDEBUG
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate uint_t	seg_page(struct seg *, caddr_t);
242*0Sstevel@tonic-gate uint_t	seg_pages(struct seg *);
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate #endif	/* VMDEBUG */
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate #endif	/* _KERNEL */
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate #ifdef	__cplusplus
249*0Sstevel@tonic-gate }
250*0Sstevel@tonic-gate #endif
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate #endif	/* _VM_SEG_H */
253