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