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 2009 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_VN_H
40#define	_VM_SEG_VN_H
41
42#include <sys/lgrp.h>
43#include <vm/anon.h>
44
45#ifdef	__cplusplus
46extern "C" {
47#endif
48
49/*
50 * A pointer to this structure is passed to segvn_create().
51 */
52typedef struct segvn_crargs {
53	struct	vnode *vp;	/* vnode mapped from */
54	struct	cred *cred;	/* credentials */
55	u_offset_t	offset; /* starting offset of vnode for mapping */
56	uchar_t	type;		/* type of sharing done */
57	uchar_t	prot;		/* protections */
58	uchar_t	maxprot;	/* maximum protections */
59	uint_t	flags;		/* flags */
60	struct	anon_map *amp;	/* anon mapping to map to */
61	uint_t	szc;		/* max preferred page size code */
62	uint_t	lgrp_mem_policy_flags;
63} segvn_crargs_t;
64
65/*
66 * (Semi) private data maintained by the seg_vn driver per segment mapping.
67 *
68 * The read/write segment lock protects all of segvn_data including the
69 * vpage array.  All fields in segvn_data are treated as read-only when
70 * the "read" version of the address space and the segment locks are held.
71 * The "write" version of the segment lock, however, is required in order to
72 * update the following fields:
73 *
74 *	pageprot
75 *	prot
76 *	amp
77 *	vpage
78 *
79 * 	softlockcnt
80 * is written by acquiring either the readers lock on the segment and
81 * freemem lock, or any lock combination which guarantees exclusive use
82 * of this segment (e.g., adress space writers lock,
83 * address space readers lock + segment writers lock).
84 */
85typedef struct	segvn_data {
86	krwlock_t lock;		/* protect segvn_data and vpage array */
87	kmutex_t segfree_syncmtx; /* barrier lock for segvn_free() */
88	uchar_t	pageprot;	/* true if per page protections present */
89	uchar_t	prot;		/* current segment prot if pageprot == 0 */
90	uchar_t	maxprot;	/* maximum segment protections */
91	uchar_t	type;		/* type of sharing done */
92	u_offset_t offset;	/* starting offset of vnode for mapping */
93	struct	vnode *vp;	/* vnode that segment mapping is to */
94	ulong_t	anon_index;	/* starting index into anon_map anon array */
95	struct	anon_map *amp;	/* pointer to anon share structure, if needed */
96	struct	vpage *vpage;	/* per-page information, if needed */
97	struct	cred *cred;	/* mapping credentials */
98	size_t	swresv;		/* swap space reserved for this segment */
99	uchar_t	advice;		/* madvise flags for segment */
100	uchar_t	pageadvice;	/* true if per page advice set */
101	ushort_t flags;		/* flags - from sys/mman.h */
102	spgcnt_t softlockcnt;	/* # of pages SOFTLOCKED in seg */
103	lgrp_mem_policy_info_t policy_info; /* memory allocation policy */
104	hat_region_cookie_t rcookie;	/* region for hat calls */
105	lgrp_mem_policy_info_t tr_policy_info; /* memory allocation for TR */
106	struct	seg *seg;	/* pointer back to seg */
107	struct	segvn_data *svn_trnext; /* textrepl list next link */
108	struct	segvn_data *svn_trprev; /* textrepl list prev link */
109	int	tr_state;	/* TR (text replication) state */
110	uchar_t	pageswap;	/* true if per page swap accounting is set */
111	spgcnt_t softlockcnt_sbase; /* # of softlocks for seg start addr */
112	spgcnt_t softlockcnt_send; /* # of softlocks for seg end addr */
113} segvn_data_t;
114
115#ifdef _KERNEL
116
117/*
118 * segment text replication states.
119 */
120#define	SEGVN_TR_INIT	(0)	/* Check if text replication can be enabled */
121#define	SEGVN_TR_ON	(1)	/* Text replication is enabled */
122#define	SEGVN_TR_OFF	(2)	/* Text replication is disabled */
123
124/*
125 * Macros for segvn segment driver locking.
126 */
127#define	SEGVN_LOCK_ENTER(as, lock, type)	rw_enter((lock), (type))
128#define	SEGVN_LOCK_EXIT(as, lock)		rw_exit((lock))
129#define	SEGVN_LOCK_DOWNGRADE(as, lock)		rw_downgrade((lock))
130#define	SEGVN_LOCK_TRYENTER(as, lock, type)	rw_tryenter((lock), (type))
131
132/*
133 * Macros to test lock states.
134 */
135#define	SEGVN_LOCK_HELD(as, lock)		RW_LOCK_HELD((lock))
136#define	SEGVN_READ_HELD(as, lock)		RW_READ_HELD((lock))
137#define	SEGVN_WRITE_HELD(as, lock)		RW_WRITE_HELD((lock))
138
139/*
140 * Macro used to detect the need to Break the sharing of COW pages
141 *
142 * The rw == S_WRITE is for the COW case
143 * rw == S_READ and type == SOFTLOCK is for the physio case
144 * We don't want to share a softlocked page because it can cause problems
145 * with multithreaded apps but if rw == S_READ_NOCOW it's ok to not break
146 * sharing of COW pages even in SOFTLOCK case.
147 */
148#define	BREAK_COW_SHARE(rw, type, seg_type) ((rw == S_WRITE || \
149	(type == F_SOFTLOCK && rw != S_READ_NOCOW)) && \
150	seg_type == MAP_PRIVATE)
151
152#define	SEGVN_ZFOD_ARGS(prot, max)	\
153	{ NULL, NULL, 0, MAP_PRIVATE, prot, max, 0, NULL, 0, 0 }
154
155#define	AS_MAP_CHECK_VNODE_LPOOB(crfp, argsp)				\
156	((crfp) == (int (*)())segvn_create &&				\
157	(((struct segvn_crargs *)(argsp))->flags &			\
158	    (MAP_TEXT | MAP_INITDATA)) &&				\
159	((struct segvn_crargs *)(argsp))->szc == 0 &&			\
160	((struct segvn_crargs *)(argsp))->vp != NULL)
161
162#define	AS_MAP_CHECK_ANON_LPOOB(crfp, argsp)				\
163	((crfp) == (int (*)())segvn_create &&				\
164	(((struct segvn_crargs *)(argsp))->szc == 0 ||			\
165	((struct segvn_crargs *)(argsp))->szc == AS_MAP_HEAP ||		\
166	((struct segvn_crargs *)(argsp))->szc == AS_MAP_STACK) &&	\
167	((struct segvn_crargs *)(argsp))->vp == NULL)
168
169#define	SVNTR_HASH_FUNC(vp)	(((((uintptr_t)(vp)) >> 4) ^		\
170				    (((uintptr_t)(vp)) >> 11)) & 	\
171					(svntr_hashtab_sz - 1))
172
173#define	SEGVN_TR_ADDSTAT(stat)						\
174	segvn_textrepl_stats[CPU->cpu_id].tr_stat_##stat++
175
176#define	SEGVN_DATA(seg)	((struct segvn_data *)(seg)->s_data)
177#define	SEG_IS_PARTIAL_RESV(seg)	\
178	((seg)->s_ops == &segvn_ops && SEGVN_DATA(seg) != NULL && \
179	(SEGVN_DATA(seg)->vp == NULL || \
180	SEGVN_DATA(seg)->vp->v_type != VREG) &&	\
181	(SEGVN_DATA(seg)->flags & MAP_NORESERVE))
182
183/*
184 * A hash table entry looked up by vnode, off/eoff and szc to find anon map to
185 * use for text replication based on main thread's (t_tid = 1) lgrp.
186 */
187typedef struct svntr {
188	struct vnode	*tr_vp;		/* text file vnode */
189	u_offset_t	tr_off;		/* tr_vp mapping start offset */
190	size_t		tr_eoff;	/* tr_vp mapping end offset */
191	uint_t		tr_szc;		/* tr_vp mapping pagesize */
192	int		tr_valid;	/* entry validity state */
193	struct svntr	*tr_next;	/* next svntr in this hash bucket */
194	timestruc_t	tr_mtime;	/* tr_vp modification time */
195	timestruc_t	tr_ctime;	/* time of last change to attributes */
196	ulong_t		tr_refcnt;	/* number of segs sharing this entry */
197	segvn_data_t	*tr_svnhead;	/* list of segs sharing this entry */
198	struct anon_map	*tr_amp[NLGRPS_MAX]; /* per lgrp anon maps */
199} svntr_t;
200
201typedef struct svntr_bucket {
202	svntr_t		*tr_head;	/* first svntr in this hash bucket */
203	kmutex_t	tr_lock;	/* per bucket lock */
204} svntr_bucket_t;
205
206typedef struct svntr_stats {
207	ulong_t		tr_stat_gaerr;   /* VOP_GETATTR() failures */
208	ulong_t		tr_stat_overmap; /* no TR due to beyond EOF mappings */
209	ulong_t		tr_stat_wrcnt;	 /* no TR due to writtable mappings */
210	ulong_t		tr_stat_stale;	 /* TR entry is stale */
211	ulong_t		tr_stat_overlap; /* overlap with other mappings */
212	ulong_t		tr_stat_nokmem;	 /* no TR due to kmem alloc failures */
213	ulong_t		tr_stat_noanon;	 /* no TR due to no swap space */
214	ulong_t		tr_stat_normem;  /* no TR due to no repl memory */
215	ulong_t		tr_stat_nolock;	 /* async TR failure due to locks */
216	ulong_t		tr_stat_asyncrepl; /* number of async TRs */
217	ulong_t		tr_stat_repl;	  /* number of sync TRs */
218	ulong_t		tr_stat_newamp;	  /* number of new amp allocs for TR */
219} svntr_stats_t;
220
221extern void	segvn_init(void);
222extern int	segvn_create(struct seg *, void *);
223
224extern	struct seg_ops segvn_ops;
225
226/*
227 * Provided as shorthand for creating user zfod segments.
228 */
229extern	caddr_t zfod_argsp;
230extern	caddr_t kzfod_argsp;
231extern	caddr_t stack_exec_argsp;
232extern	caddr_t stack_noexec_argsp;
233
234#endif	/* _KERNEL */
235
236#ifdef	__cplusplus
237}
238#endif
239
240#endif	/* _VM_SEG_VN_H */
241