1 /**	$MirOS: src/sys/sys/exec.h,v 1.3 2014/04/10 00:11:36 tg Exp $	*/
2 /*	$OpenBSD: exec.h,v 1.23 2007/01/29 11:33:24 otto Exp $	*/
3 /*	$NetBSD: exec.h,v 1.59 1996/02/09 18:25:09 christos Exp $	*/
4 
5 /*-
6  * Copyright (c) 1994 Christopher G. Demetriou
7  * Copyright (c) 1993 Theo de Raadt
8  * Copyright (c) 1992, 1993
9  *	The Regents of the University of California.  All rights reserved.
10  * (c) UNIX System Laboratories, Inc.
11  * All or some portions of this file are derived from material licensed
12  * to the University of California by American Telephone and Telegraph
13  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
14  * the permission of UNIX System Laboratories, Inc.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  * 3. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *	@(#)exec.h	8.3 (Berkeley) 1/21/94
41  */
42 
43 #ifndef _SYS_EXEC_H_
44 #define _SYS_EXEC_H_
45 
46 /*
47  * The following structure is found at the top of the user stack of each
48  * user process. The ps program uses it to locate argv and environment
49  * strings. Programs that wish ps to display other information may modify
50  * it; normally ps_argvstr points to argv[0], and ps_nargvstr is the same
51  * as the program's argc. The fields ps_envstr and ps_nenvstr are the
52  * equivalent for the environment.
53  */
54 struct ps_strings {
55 	char	**ps_argvstr;	/* first of 0 or more argument strings */
56 	int	ps_nargvstr;	/* the number of argument strings */
57 	char	**ps_envstr;	/* first of 0 or more environment strings */
58 	int	ps_nenvstr;	/* the number of environment strings */
59 };
60 
61 /*
62  * Address of ps_strings structure (in user space).
63  */
64 #ifdef MACHINE_STACK_GROWS_UP
65 #define	PS_STRINGS	((struct ps_strings *)(USRSTACK))
66 #else
67 #define	PS_STRINGS \
68 	((struct ps_strings *)(USRSTACK - sizeof(struct ps_strings)))
69 #endif
70 
71 /*
72  * Below the PS_STRINGS and sigtramp, we may require a gap on the stack
73  * (used to copyin/copyout various emulation data structures).
74  */
75 #define	STACKGAPLEN	(2*1024)	/* plenty enough for now */
76 
77 #ifdef MACHINE_STACK_GROWS_UP
78 #define	STACKGAPBASE_UNALIGNED	\
79 	((caddr_t)PS_STRINGS + sizeof(struct ps_strings))
80 #else
81 #define	STACKGAPBASE_UNALIGNED	\
82 	((caddr_t)PS_STRINGS - STACKGAPLEN)
83 #endif
84 #define	STACKGAPBASE		\
85 	((caddr_t)ALIGN(STACKGAPBASE_UNALIGNED))
86 
87 /*
88  * the following structures allow execve() to put together processes
89  * in a more extensible and cleaner way.
90  *
91  * the exec_package struct defines an executable being execve()'d.
92  * it contains the header, the vmspace-building commands, the vnode
93  * information, and the arguments associated with the newly-execve'd
94  * process.
95  *
96  * the exec_vmcmd struct defines a command description to be used
97  * in creating the new process's vmspace.
98  */
99 
100 struct proc;
101 struct exec_package;
102 
103 typedef int (*exec_makecmds_fcn)(struct proc *, struct exec_package *);
104 
105 struct execsw {
106 	u_int	es_hdrsz;		/* size of header for this format */
107 	exec_makecmds_fcn es_check;	/* function to check exec format */
108 	struct emul *es_emul;		/* emulation */
109 };
110 
111 struct exec_vmcmd {
112 	int	(*ev_proc)(struct proc *p, struct exec_vmcmd *cmd);
113 				/* procedure to run for region of vmspace */
114 	u_long	ev_len;		/* length of the segment to map */
115 	u_long	ev_addr;	/* address in the vmspace to place it at */
116 	struct	vnode *ev_vp;	/* vnode pointer for the file w/the data */
117 	u_long	ev_offset;	/* offset in the file for the data */
118 	u_int	ev_prot;	/* protections for segment */
119 	int	ev_flags;
120 #define VMCMD_RELATIVE  0x0001  /* ev_addr is relative to base entry */
121 #define VMCMD_BASE      0x0002  /* marks a base entry */
122 };
123 
124 #define	EXEC_DEFAULT_VMCMD_SETSIZE	8	/* # of cmds in set to start */
125 
126 /* exec vmspace-creation command set; see below */
127 struct exec_vmcmd_set {
128 	u_int	evs_cnt;
129 	u_int	evs_used;
130 	struct	exec_vmcmd *evs_cmds;
131 	struct	exec_vmcmd evs_start[EXEC_DEFAULT_VMCMD_SETSIZE];
132 };
133 
134 struct exec_package {
135 	char	*ep_name;		/* file's name */
136 	void	*ep_hdr;		/* file's exec header */
137 	u_int	ep_hdrlen;		/* length of ep_hdr */
138 	u_int	ep_hdrvalid;		/* bytes of ep_hdr that are valid */
139 	struct nameidata *ep_ndp;	/* namei data pointer for lookups */
140 	struct	exec_vmcmd_set ep_vmcmds;  /* vmcmds used to build vmspace */
141 	struct	vnode *ep_vp;		/* executable's vnode */
142 	struct	vattr *ep_vap;		/* executable's attributes */
143 	u_long	ep_taddr;		/* process's text address */
144 	u_long	ep_tsize;		/* size of process's text */
145 	u_long	ep_daddr;		/* process's data(+bss) address */
146 	u_long	ep_dsize;		/* size of process's data(+bss) */
147 	u_long	ep_maxsaddr;		/* proc's max stack addr ("top") */
148 	u_long	ep_minsaddr;		/* proc's min stack addr ("bottom") */
149 	u_long	ep_ssize;		/* size of process's stack */
150 	u_long	ep_entry;		/* process's entry point */
151 	u_int	ep_flags;		/* flags; see below. */
152 	char	**ep_fa;		/* a fake args vector for scripts */
153 	int	ep_fd;			/* a file descriptor we're holding */
154 	struct  emul *ep_emul;		/* os emulation */
155 	void	*ep_emul_arg;		/* emulation argument */
156 	void	*ep_emul_argp;		/* emulation argument pointer */
157 	char	*ep_interp;		/* name of interpreter if any */
158 	u_long	ep_interp_pos;		/* interpreter load position */
159 };
160 #define	EXEC_INDIR	0x0001		/* script handling already done */
161 #define	EXEC_HASFD	0x0002		/* holding a shell script */
162 #define	EXEC_HASARGL	0x0004		/* has fake args vector */
163 #define	EXEC_SKIPARG	0x0008		/* don't copy user-supplied argv[0] */
164 #define	EXEC_DESTR	0x0010		/* destructive ops performed */
165 
166 #ifdef _KERNEL
167 /*
168  * funtions used either by execve() or the various cpu-dependent execve()
169  * hooks.
170  */
171 int	exec_makecmds(struct proc *, struct exec_package *);
172 int	exec_runcmds(struct proc *, struct exec_package *);
173 void	vmcmdset_extend(struct exec_vmcmd_set *);
174 void	kill_vmcmds(struct exec_vmcmd_set *evsp);
175 int	vmcmd_map_pagedvn(struct proc *, struct exec_vmcmd *);
176 int	vmcmd_map_readvn(struct proc *, struct exec_vmcmd *);
177 int	vmcmd_map_zero(struct proc *, struct exec_vmcmd *);
178 void	*copyargs(struct exec_package *,
179 				    struct ps_strings *,
180 				    void *, void *);
181 void	setregs(struct proc *, struct exec_package *,
182 				    u_long, register_t *);
183 int	check_exec(struct proc *, struct exec_package *);
184 int	exec_setup_stack(struct proc *, struct exec_package *);
185 int	exec_setup_heap(struct proc *, struct exec_package *);
186 int	exec_process_vmcmds(struct proc *, struct exec_package *);
187 
188 #ifdef DEBUG
189 void	new_vmcmd(struct exec_vmcmd_set *evsp,
190 		    int (*proc)(struct proc *p, struct exec_vmcmd *),
191 		    u_long len, u_long addr, struct vnode *vp, u_long offset,
192 		    u_int prot, int flags);
193 #define	NEW_VMCMD(evsp,proc,len,addr,vp,offset,prot) \
194 	new_vmcmd(evsp,proc,len,addr,vp,offset,prot, 0);
195 #define NEW_VMCMD2(evsp,proc,len,addr,vp,offset,prot,flags) \
196 	new_vmcmd(evsp,proc,len,addr,vp,offset,prot,flags)
197 #else	/* DEBUG */
198 #define NEW_VMCMD(evsp,proc,len,addr,vp,offset,prot) \
199 	NEW_VMCMD2(evsp,proc,len,addr,vp,offset,prot,0)
200 #define	NEW_VMCMD2(evsp,proc,len,addr,vp,offset,prot,flags) do { \
201 	struct exec_vmcmd *vcp; \
202 	if ((evsp)->evs_used >= (evsp)->evs_cnt) \
203 		vmcmdset_extend(evsp); \
204 	vcp = &(evsp)->evs_cmds[(evsp)->evs_used++]; \
205 	vcp->ev_proc = (proc); \
206 	vcp->ev_len = (len); \
207 	vcp->ev_addr = (addr); \
208 	if ((vcp->ev_vp = (vp)) != NULLVP) \
209 		VREF(vp); \
210 	vcp->ev_offset = (offset); \
211 	vcp->ev_prot = (prot); \
212 	vcp->ev_flags = (flags); \
213 } while (0)
214 
215 #endif /* DEBUG */
216 
217 /* Initialize an empty vmcmd set */
218 #define VMCMDSET_INIT(vmc) do { \
219 	(vmc)->evs_cnt = EXEC_DEFAULT_VMCMD_SETSIZE; \
220 	(vmc)->evs_cmds = (vmc)->evs_start; \
221 	(vmc)->evs_used = 0; \
222 } while (0)
223 
224 /*
225  * Exec function switch:
226  *
227  * Note that each makecmds function is responsible for loading the
228  * exec package with the necessary functions for any exec-type-specific
229  * handling.
230  *
231  * Functions for specific exec types should be defined in their own
232  * header file.
233  */
234 extern struct	execsw execsw[];
235 extern int	nexecs;
236 extern int	exec_maxhdrsz;
237 
238 #endif /* _KERNEL */
239 
240 #include <sys/exec_aout.h>
241 #include <machine/exec.h>
242 
243 #endif /* !_SYS_EXEC_H_ */
244