xref: /titanic_41/usr/src/uts/common/sys/user.h (revision 40db2e2b777b79f3dd0d6d9629593a07f86b9c0a)
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 2007 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 #ifndef _SYS_USER_H
31 #define	_SYS_USER_H
32 
33 #pragma ident	"%Z%%M%	%I%	%E% SMI"
34 
35 #include <sys/types.h>
36 #include <sys/signal.h>
37 
38 #ifdef	__cplusplus
39 extern "C" {
40 #endif
41 
42 /*
43  * struct exdata is visible in and out of the kernel. This is because it
44  * is referenced in <sys/core.h> which doesn't have this kind of magic.
45  */
46 struct exdata {
47 	struct vnode	*vp;
48 	size_t	ux_tsize;	/* text size */
49 	size_t	ux_dsize;	/* data size */
50 	size_t	ux_bsize;	/* bss size */
51 	size_t	ux_lsize;	/* lib size */
52 	long	ux_nshlibs;	/* number of shared libs needed */
53 	short	ux_mach;	/* machine type */
54 	short	ux_mag;		/* magic number MUST be here */
55 	off_t	ux_toffset;	/* file offset to raw text */
56 	off_t	ux_doffset;	/* file offset to raw data */
57 	off_t	ux_loffset;	/* file offset to lib sctn */
58 	caddr_t	ux_txtorg;	/* start addr of text in mem */
59 	caddr_t	ux_datorg;	/* start addr of data in mem */
60 	caddr_t	ux_entloc;	/* entry location */
61 };
62 
63 #ifdef	__cplusplus
64 }
65 #endif
66 
67 #if defined(_KERNEL) || defined(_KMEMUSER)
68 
69 #include <sys/param.h>
70 #include <sys/pcb.h>
71 #include <sys/siginfo.h>
72 #include <sys/resource.h>
73 #include <sys/time.h>
74 #include <sys/auxv.h>
75 #include <sys/errno.h>
76 #include <sys/t_lock.h>
77 #include <sys/refstr.h>
78 
79 #ifdef	__cplusplus
80 extern "C" {
81 #endif
82 
83 /*
84  * Entry in the per-process list of open files.
85  * Note: only certain fields are copied in flist_grow() and flist_fork().
86  * This is indicated in brackets in the structure member comments.
87  */
88 typedef struct uf_entry {
89 	kmutex_t	uf_lock;	/* per-fd lock [never copied] */
90 	struct file	*uf_file;	/* file pointer [grow, fork] */
91 	struct fpollinfo *uf_fpollinfo;	/* poll state [grow] */
92 	int		uf_refcnt;	/* LWPs accessing this file [grow] */
93 	int		uf_alloc;	/* right subtree allocs [grow, fork] */
94 	short		uf_flag;	/* fcntl F_GETFD flags [grow, fork] */
95 	short		uf_busy;	/* file is allocated [grow, fork] */
96 	kcondvar_t	uf_wanted_cv;	/* waiting for setf() [never copied] */
97 	kcondvar_t	uf_closing_cv;	/* waiting for close() [never copied] */
98 	struct portfd 	*uf_portfd;	/* associated with port [grow] */
99 	/* Avoid false sharing - pad to coherency granularity (64 bytes) */
100 	char		uf_pad[64 - sizeof (kmutex_t) - 2 * sizeof (void*) -
101 		2 * sizeof (int) - 2 * sizeof (short) -
102 		2 * sizeof (kcondvar_t) - sizeof (struct portfd *)];
103 } uf_entry_t;
104 
105 /*
106  * Retired file lists -- see flist_grow() for details.
107  */
108 typedef struct uf_rlist {
109 	struct uf_rlist	*ur_next;
110 	uf_entry_t	*ur_list;
111 	int		ur_nfiles;
112 } uf_rlist_t;
113 
114 /*
115  * Per-process file information.
116  */
117 typedef struct uf_info {
118 	kmutex_t	fi_lock;	/* see below */
119 	int		fi_badfd;	/* bad file descriptor # */
120 	int		fi_action;	/* action to take on bad fd use */
121 	int		fi_nfiles;	/* number of entries in fi_list[] */
122 	uf_entry_t *volatile fi_list;	/* current file list */
123 	uf_rlist_t	*fi_rlist;	/* retired file lists */
124 } uf_info_t;
125 
126 /*
127  * File list locking.
128  *
129  * Each process has a list of open files, fi_list, indexed by fd.
130  * fi_list is an array of uf_entry_t structures, each with its own lock.
131  * One might think that the correct way to lock a file descriptor would be:
132  *
133  *	ufp = fip->fi_list[fd];
134  *	mutex_enter(&ufp->uf_lock);
135  *
136  * However, that construct is only safe if fi_lock is already held.  If not,
137  * fi_list can change in the window between loading ufp and entering uf_lock.
138  * The UF_ENTER() macro deals with this possibility.  UF_ENTER(ufp, fip, fd)
139  * locks fd and sets ufp to fd's uf_entry.  The locking rules are as follows:
140  *
141  * (1) fi_lock protects fi_list and fi_nfiles.  It also protects the
142  *     uf_alloc and uf_busy fields of every fd's ufp; see fd_find() for
143  *     details on file descriptor allocation.
144  *
145  * (2) UF_ENTER(ufp, fip, fd) locks descriptor fd and sets ufp to point
146  *     to the uf_entry_t for fd.  UF_ENTER() protects all fields in ufp
147  *     except uf_alloc and uf_busy.  UF_ENTER(ufp, fip, fd) also prevents
148  *     ufp->uf_alloc, ufp->uf_busy, fip->fi_list and fip->fi_nfiles from
149  *     changing.
150  *
151  * (3) The lock ordering is (1), (2).
152  *
153  * (4) Note that fip->fi_list and fip->fi_nfiles cannot change while *any*
154  *     file list lock is held.  Thus flist_grow() must acquire all such
155  *     locks -- fi_lock and every fd's uf_lock -- to install a new file list.
156  */
157 #define	UF_ENTER(ufp, fip, fd)					\
158 	for (;;) {						\
159 		uf_entry_t *_flist = (fip)->fi_list;		\
160 		ufp = &_flist[fd];				\
161 		ASSERT((fd) < (fip)->fi_nfiles);		\
162 		mutex_enter(&ufp->uf_lock);			\
163 		if (_flist == (fip)->fi_list)			\
164 			break;					\
165 		mutex_exit(&ufp->uf_lock);			\
166 	}
167 
168 #define	UF_EXIT(ufp)	mutex_exit(&ufp->uf_lock)
169 
170 #define	PSARGSZ		80	/* Space for exec arguments (used by ps(1)) */
171 #define	MAXCOMLEN	16	/* <= MAXNAMLEN, >= sizeof (ac_comm) */
172 
173 typedef struct {		/* kernel syscall set type */
174 	uint_t	word[9];	/* space for syscall numbers [1..288] */
175 } k_sysset_t;
176 
177 /*
178  * __KERN_NAUXV_IMPL is defined as a convenience sizing mechanism
179  * for the portions of the kernel that care about aux vectors.
180  *
181  * Applications that need to know how many aux vectors the kernel
182  * supplies should use the proc(4) interface to read /proc/PID/auxv.
183  *
184  * This value should not be changed in a patch.
185  */
186 #if defined(__sparc)
187 #define	__KERN_NAUXV_IMPL 19
188 #elif defined(__i386) || defined(__amd64)
189 #define	__KERN_NAUXV_IMPL 21
190 #endif
191 
192 struct execsw;
193 
194 /*
195  * The user structure; one allocated per process.  Contains all the
196  * per-process data that doesn't need to be referenced while the
197  * process is swapped.
198  */
199 typedef	struct	user {
200 	/*
201 	 * These fields are initialized at process creation time and never
202 	 * modified.  They can be accessed without acquiring locks.
203 	 */
204 	struct execsw *u_execsw;	/* pointer to exec switch entry */
205 	auxv_t  u_auxv[__KERN_NAUXV_IMPL]; /* aux vector from exec */
206 	timestruc_t u_start;		/* hrestime at process start */
207 	clock_t	u_ticks;		/* lbolt at process start */
208 	char	u_comm[MAXCOMLEN + 1];	/* executable file name from exec */
209 	char	u_psargs[PSARGSZ];	/* arguments from exec */
210 	int	u_argc;			/* value of argc passed to main() */
211 	uintptr_t u_argv;		/* value of argv passed to main() */
212 	uintptr_t u_envp;		/* value of envp passed to main() */
213 
214 	/*
215 	 * These fields are protected by p_lock:
216 	 */
217 	struct vnode *u_cdir;		/* current directory */
218 	struct vnode *u_rdir;		/* root directory */
219 	uint64_t u_mem;			/* accumulated memory usage */
220 	size_t	u_mem_max;		/* peak RSS (K) */
221 	mode_t	u_cmask;		/* mask for file creation */
222 	char	u_acflag;		/* accounting flag */
223 	char	u_systrap;		/* /proc: any syscall mask bits set? */
224 	refstr_t *u_cwd;		/* cached string for cwd */
225 
226 	k_sysset_t u_entrymask;		/* /proc syscall stop-on-entry mask */
227 	k_sysset_t u_exitmask;		/* /proc syscall stop-on-exit mask */
228 	k_sigset_t u_signodefer;	/* signals defered when caught */
229 	k_sigset_t u_sigonstack;	/* signals taken on alternate stack */
230 	k_sigset_t u_sigresethand;	/* signals reset when caught */
231 	k_sigset_t u_sigrestart;	/* signals that restart system calls */
232 	k_sigset_t u_sigmask[MAXSIG];	/* signals held while in catcher */
233 	void	(*u_signal[MAXSIG])();	/* Disposition of signals */
234 
235 	/*
236 	 * Resource controls provide the backend for process resource limits,
237 	 * the interfaces for which are maintained for compatibility.  To
238 	 * preserve the behaviour associated with the RLIM_SAVED_CUR and
239 	 * RLIM_SAVED_MAX tokens, we retain the "saved" rlimits.
240 	 */
241 	struct rlimit64	u_saved_rlimit[RLIM_NSAVED];
242 
243 	uf_info_t	u_finfo;	/* open file information */
244 } user_t;
245 
246 #include <sys/proc.h>			/* cannot include before user defined */
247 
248 #ifdef	_KERNEL
249 #define	P_FINFO(p)	(&(p)->p_user.u_finfo)
250 #endif	/* _KERNEL */
251 
252 #ifdef	__cplusplus
253 }
254 #endif
255 
256 #else	/* defined(_KERNEL) || defined(_KMEMUSER) */
257 
258 /*
259  * Here, we define a fake version of struct user for programs
260  * (debuggers) that use ptrace() to read and modify the saved
261  * registers directly in the u-area.  ptrace() has been removed
262  * from the operating system and now exists as a library function
263  * in libc, built on the /proc process filesystem.  The ptrace()
264  * library function provides access only to the members of the
265  * fake struct user defined here.
266  *
267  * User-level programs that must know the real contents of struct
268  * user will have to define _KMEMUSER before including <sys/user.h>.
269  * Such programs also become machine specific. Carefully consider
270  * the consequences of your actions.
271  */
272 
273 #include <sys/regset.h>
274 
275 #ifdef	__cplusplus
276 extern "C" {
277 #endif
278 
279 #define	PSARGSZ		80	/* Space for exec arguments (used by ps(1)) */
280 
281 typedef	struct	user {
282 	gregset_t	u_reg;		/* user's saved registers */
283 	greg_t		*u_ar0;		/* address of user's saved R0 */
284 	char	u_psargs[PSARGSZ];	/* arguments from exec */
285 	void	(*u_signal[MAXSIG])();	/* Disposition of signals */
286 	int		u_code;		/* fault code on trap */
287 	caddr_t		u_addr;		/* fault PC on trap */
288 } user_t;
289 
290 #ifdef	__cplusplus
291 }
292 #endif
293 
294 #endif	/* defined(_KERNEL) || defined(_KMEMUSER) */
295 
296 #endif	/* _SYS_USER_H */
297