xref: /illumos-gate/usr/src/uts/common/vm/seg_map.h (revision 581cede61ac9c14d8d4ea452562a567189eead78)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27 /*	  All Rights Reserved  	*/
28 
29 /*
30  * University Copyright- Copyright (c) 1982, 1986, 1988
31  * The Regents of the University of California
32  * All Rights Reserved
33  *
34  * University Acknowledgment- Portions of this document are derived from
35  * software developed by the University of California, Berkeley, and its
36  * contributors.
37  */
38 
39 #ifndef	_VM_SEG_MAP_H
40 #define	_VM_SEG_MAP_H
41 
42 #pragma ident	"%Z%%M%	%I%	%E% SMI"
43 
44 #ifdef	__cplusplus
45 extern "C" {
46 #endif
47 
48 /*
49  * When segmap is created it is possible to program its behavior,
50  *	using the create args [needed for performance reasons].
51  * Segmap creates n lists of pages.
52  *	For VAC machines, there will be at least one free list
53  *	per color. If more than one free list per color is needed,
54  *	set nfreelist as needed.
55  *
56  *	For PAC machines, it will be treated as VAC with only one
57  *	color- every page is of the same color. Again, set nfreelist
58  *	to get more than one free list.
59  */
60 struct segmap_crargs {
61 	uint_t	prot;
62 	uint_t	shmsize;	/* shm_alignment for VAC, 0 for PAC. */
63 	uint_t	nfreelist;	/* number of freelist per color, >= 1 */
64 };
65 
66 #include <vm/kpm.h>
67 #include <vm/vpm.h>
68 
69 /*
70  * Each smap struct represents a MAXBSIZE sized mapping to the
71  * <sm_vp, sm_off> given in the structure.  The location of the
72  * the structure in the array gives the virtual address of the
73  * mapping. Structure rearranged for 64bit sm_off.
74  */
75 struct	smap {
76 	kmutex_t	sm_mtx;		/* protect non-list fields */
77 	struct	vnode	*sm_vp;		/* vnode pointer (if mapped) */
78 	struct	smap	*sm_hash;	/* hash pointer */
79 	struct	smap	*sm_next;	/* next pointer */
80 	struct	smap	*sm_prev;	/* previous pointer */
81 	u_offset_t	sm_off;		/* file offset for mapping */
82 	ushort_t	sm_bitmap;	/* bit map for locked translations */
83 	ushort_t	sm_refcnt;	/* reference count for uses */
84 	ushort_t	sm_flags;	/* smap flags */
85 	ushort_t	sm_free_ndx;	/* freelist */
86 #ifdef	SEGKPM_SUPPORT
87 	struct kpme	sm_kpme;	/* segkpm */
88 #endif
89 };
90 
91 #ifdef	SEGKPM_SUPPORT
92 #define	GET_KPME(smp)	(&(smp)->sm_kpme)
93 #define	sm_kpme_next	sm_kpme.kpe_next
94 #define	sm_kpme_prev	sm_kpme.kpe_prev
95 #define	sm_kpme_page	sm_kpme.kpe_page
96 #else
97 #define	GET_KPME(smp)	((struct kpme *)NULL)
98 #endif
99 
100 /* sm_flags */
101 #define	SM_KPM_NEWPAGE	   0x00000001	/* page created in segmap_getmapft */
102 #define	SM_NOTKPM_RELEASED 0x00000002	/* released smap not in segkpm mode */
103 #define	SM_QNDX_ZERO	   0x00000004	/* on the index 0 freelist */
104 #define	SM_READ_DATA	   0x00000010	/* page created for read */
105 #define	SM_WRITE_DATA	   0x00000020	/* page created for write */
106 
107 /*
108  * Multiple smap free lists are maintained so that allocations
109  * will scale with cpu count. Each free list is made up of 2 queues
110  * so that allocations and deallocations can proceed concurrently.
111  * Each queue structure is padded to 64 bytes to avoid false sharing.
112  */
113 #define	SM_FREEQ_PAD (64 - sizeof (struct smap *) - sizeof (kmutex_t))
114 struct 	sm_freeq {
115 	struct smap	*smq_free;	/* points into freelist */
116 	kmutex_t	smq_mtx;	/* protects smq_free */
117 	char		smq_pad[SM_FREEQ_PAD];
118 };
119 
120 struct	smfree {
121 	struct sm_freeq	sm_freeq[2];	/* alloc and release queues */
122 	struct sm_freeq	*sm_allocq;	/* current allocq */
123 	struct sm_freeq	*sm_releq;	/* current releq */
124 	kcondvar_t	sm_free_cv;
125 	ushort_t	sm_want;	/* someone wants a slot of this color */
126 };
127 
128 /*
129  * Cached smaps are kept on hash chains to enable fast reclaim lookups.
130  */
131 struct  smaphash {
132 	kmutex_t	sh_mtx;		/* protects this hash chain */
133 	struct  smap	*sh_hash_list;  /* start of hash chain */
134 };
135 
136 /*
137  * (Semi) private data maintained by the segmap driver per SEGMENT mapping
138  * All fields in segmap_data are read-only after the segment is created.
139  *
140  */
141 
142 struct	segmap_data {
143 	struct	smap	*smd_sm;	/* array of smap structures */
144 	long		smd_npages;	/* size of smap array */
145 	struct smfree	*smd_free;	/* ptr to freelist header array */
146 	struct smaphash *smd_hash;	/* ptr to hash header array */
147 	int		smd_nfree;	/* number of free lists */
148 	uchar_t		smd_prot;	/* protections for all smap's */
149 };
150 
151 /*
152  * Statistics for segmap operations.
153  *
154  * No explicit locking to protect these stats.
155  */
156 struct segmapcnt {
157 	kstat_named_t	smp_fault;	/* number of segmap_faults */
158 	kstat_named_t	smp_faulta;	/* number of segmap_faultas */
159 	kstat_named_t	smp_getmap;	/* number of segmap_getmaps */
160 	kstat_named_t	smp_get_use;	/* getmaps that reuse existing map */
161 	kstat_named_t	smp_get_reclaim; /* getmaps that do a reclaim */
162 	kstat_named_t	smp_get_reuse;	/* getmaps that reuse a slot */
163 	kstat_named_t	smp_get_unused;	/* getmaps that reuse existing map */
164 	kstat_named_t	smp_get_nofree;	/* getmaps with no free slots */
165 	kstat_named_t	smp_rel_async;	/* releases that are async */
166 	kstat_named_t	smp_rel_write;	/* releases that write */
167 	kstat_named_t	smp_rel_free;	/* releases that free */
168 	kstat_named_t	smp_rel_abort;	/* releases that abort */
169 	kstat_named_t	smp_rel_dontneed; /* releases with dontneed set */
170 	kstat_named_t	smp_release;	/* releases with no other action */
171 	kstat_named_t	smp_pagecreate;	/* pagecreates */
172 	kstat_named_t   smp_free_notfree; /* pages not freed in */
173 					/* segmap_pagefree */
174 	kstat_named_t   smp_free_dirty; /* dirty pages freeed */
175 					/* in segmap_pagefree */
176 	kstat_named_t   smp_free;	/* clean pages freeed in */
177 					/* segmap_pagefree */
178 	kstat_named_t	smp_stolen;	/* segmap_getmapflt() stole */
179 					/* from get_free_smp() */
180 	kstat_named_t	smp_get_nomtx;	/* free smaps but no mutex */
181 };
182 
183 /*
184  * These are flags used on release.  Some of these might get handled
185  * by segment operations needed for msync (when we figure them out).
186  * SM_ASYNC modifies SM_WRITE.  SM_DONTNEED modifies SM_FREE.  SM_FREE
187  * and SM_INVAL as well as SM_FREE and SM_DESTROY are mutually exclusive.
188  * SM_DESTROY behaves like SM_INVAL but also forces the pages to be
189  * destroyed -- this prevents them from being written to the backing
190  * store.
191  */
192 #define	SM_WRITE	0x01		/* write back the pages upon release */
193 #define	SM_ASYNC	0x02		/* do the write asynchronously */
194 #define	SM_FREE		0x04		/* put pages back on free list */
195 #define	SM_INVAL	0x08		/* invalidate page (no caching) */
196 #define	SM_DONTNEED	0x10		/* less likely to be needed soon */
197 #define	SM_DESTROY	0x20		/* invalidate page, don't write back */
198 
199 /*
200  * These are the forcefault flags used on getmapflt.
201  *
202  * The orginal semantic was extended to allow using the segkpm mapping
203  * scheme w/o a major segmap interface change for MAXBSIZE == PAGESIZE
204  * (which is required to enable segkpm for MAXBSIZE > PAGESIZE).
205  * Most segmap consumers needn't to be changed at all or only need to
206  * be changed slightly to take advantage of segkpm. Because the segkpm
207  * virtual address is based on the physical address of a page, a page is
208  * required to determine the virtual address (return value). Pages mapped
209  * with segkpm are always at least read locked and are hence protected
210  * from pageout or fsflush from segmap_getmap until segmap_release. This
211  * implies, that the segkpm mappings are locked within this period too.
212  * No trap driven segmap_fault's are possible in segkpm mode.
213  *
214  * The following combinations of "forcefault" and "rw" allow segkpm mode.
215  * (1) SM_FAULT, S_READ
216  * (2) SM_FAULT, S_WRITE
217  * (3) SM_PAGECREATE, S_WRITE
218  * (4) SM_LOCKPROTO, {S_READ, S_WRITE, S_OTHER}
219  *
220  * The regular additional operations (come in pairs in most of the cases):
221  * . segmap_pagecreate/segmap_pageunlock
222  * . segmap_fault(F_SOFTLOCK)/segmap_fault(F_SOFTUNLOCK)
223  *
224  * are mostly a no-op in segkpm mode with the following exceptions:
225  * . The "newpage" return value of segmap_pagecreate is still supported
226  *   for zeroout operations needed on newly created pages.
227  *
228  * . segmap_fault() must follow when a error could be expected in
229  *   the VOP_GETPAGE. In segkpm mode this error is recognized in
230  *   segmap_getmapflt and returned from the following segmap_fault()
231  *   call. The "hole" optimization (read only after first VOP_GETPAGE
232  *   mapping in segmap_getmapflt followed by a trap driven protection
233  *   fault and a second VOP_GETPAGE via segmap_fault) cannot be used.
234  *
235  * . segmap_fault(F_SOFTUNLOCK) must follow when segmap_getmapflt was
236  *   called w/ (SM_LOCKPROTO, S_OTHER). S_WRITE has to be applied, when
237  *   the page should be marked "dirty". Otherwise the page is not
238  *   written to the backing store later (as mentioned above, no page
239  *   or protection faults are possible in segkpm mode). Caller cannot
240  *   use only S_OTHER and rely on a protection fault to force the page
241  *   to become dirty.
242  *
243  * . The segmap_pagecreate parameter softlock is ignored, pages and
244  *   mappings are locked anyway.
245  *
246  * SM_LOCKPROTO is used in the fbio layer and some special segmap consumers.
247  */
248 #define	SM_PAGECREATE	0x00		/* create page in segkpm mode, no I/O */
249 #define	SM_FAULT	0x01		/* fault in page if necessary */
250 #define	SM_LOCKPROTO	0x02		/* lock/unlock protocol used */
251 
252 #define	MAXBSHIFT	13		/* log2(MAXBSIZE) */
253 
254 #define	MAXBOFFSET	(MAXBSIZE - 1)
255 #define	MAXBMASK	(~MAXBOFFSET)
256 
257 /*
258  * SMAP_HASHAVELEN is the average length desired for this chain, from
259  * which the size of the smd_hash table is derived at segment create time.
260  * SMAP_HASHVPSHIFT is defined so that 1 << SMAP_HASHVPSHIFT is the
261  * approximate size of a vnode struct.
262  */
263 #define	SMAP_HASHAVELEN		4
264 #define	SMAP_HASHVPSHIFT	6
265 
266 
267 #ifdef _KERNEL
268 /*
269  * The kernel generic mapping segment.
270  */
271 extern struct seg *segkmap;
272 
273 /*
274  * Public seg_map segment operations.
275  */
276 extern int	segmap_create(struct seg *, void *);
277 extern int	segmap_pagecreate(struct seg *, caddr_t, size_t, int);
278 extern void	segmap_pageunlock(struct seg *, caddr_t, size_t, enum seg_rw);
279 extern faultcode_t segmap_fault(struct hat *, struct seg *, caddr_t, size_t,
280 		enum fault_type, enum seg_rw);
281 extern caddr_t	segmap_getmap(struct seg *, struct vnode *, u_offset_t);
282 extern caddr_t	segmap_getmapflt(struct seg *, struct vnode *, u_offset_t,
283 		size_t, int, enum seg_rw);
284 extern int	segmap_release(struct seg *, caddr_t, uint_t);
285 extern void	segmap_flush(struct seg *, struct vnode *);
286 extern void	segmap_inval(struct seg *, struct vnode *, u_offset_t);
287 
288 #endif	/* _KERNEL */
289 
290 #ifdef	__cplusplus
291 }
292 #endif
293 
294 #endif	/* _VM_SEG_MAP_H */
295