sunos_exec_aout.c revision 1.1.2.2 1 1.1.2.2 bouyer /* $NetBSD: sunos_exec_aout.c,v 1.1.2.2 2000/12/08 09:08:43 bouyer Exp $ */
2 1.1.2.2 bouyer
3 1.1.2.2 bouyer /*
4 1.1.2.2 bouyer * Copyright (c) 1993 Theo de Raadt
5 1.1.2.2 bouyer * All rights reserved.
6 1.1.2.2 bouyer *
7 1.1.2.2 bouyer * Redistribution and use in source and binary forms, with or without
8 1.1.2.2 bouyer * modification, are permitted provided that the following conditions
9 1.1.2.2 bouyer * are met:
10 1.1.2.2 bouyer * 1. Redistributions of source code must retain the above copyright
11 1.1.2.2 bouyer * notice, this list of conditions and the following disclaimer.
12 1.1.2.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.2.2 bouyer * notice, this list of conditions and the following disclaimer in the
14 1.1.2.2 bouyer * documentation and/or other materials provided with the distribution.
15 1.1.2.2 bouyer * 3. The name of the author may not be used to endorse or promote products
16 1.1.2.2 bouyer * derived from this software without specific prior written permission
17 1.1.2.2 bouyer *
18 1.1.2.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1.2.2 bouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.1.2.2 bouyer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.1.2.2 bouyer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1.2.2 bouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 1.1.2.2 bouyer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 1.1.2.2 bouyer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 1.1.2.2 bouyer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 1.1.2.2 bouyer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 1.1.2.2 bouyer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1.2.2 bouyer */
29 1.1.2.2 bouyer
30 1.1.2.2 bouyer #include <sys/param.h>
31 1.1.2.2 bouyer #include <sys/systm.h>
32 1.1.2.2 bouyer #include <sys/filedesc.h>
33 1.1.2.2 bouyer #include <sys/kernel.h>
34 1.1.2.2 bouyer #include <sys/proc.h>
35 1.1.2.2 bouyer #include <sys/mount.h>
36 1.1.2.2 bouyer #include <sys/malloc.h>
37 1.1.2.2 bouyer #include <sys/namei.h>
38 1.1.2.2 bouyer #include <sys/signalvar.h>
39 1.1.2.2 bouyer #include <sys/vnode.h>
40 1.1.2.2 bouyer #include <sys/file.h>
41 1.1.2.2 bouyer #include <sys/exec.h>
42 1.1.2.2 bouyer #include <sys/resourcevar.h>
43 1.1.2.2 bouyer #include <sys/wait.h>
44 1.1.2.2 bouyer
45 1.1.2.2 bouyer #include <sys/mman.h>
46 1.1.2.2 bouyer
47 1.1.2.2 bouyer #include <machine/cpu.h>
48 1.1.2.2 bouyer #include <machine/reg.h>
49 1.1.2.2 bouyer
50 1.1.2.2 bouyer #include <compat/sunos/sunos.h>
51 1.1.2.2 bouyer #include <compat/sunos/sunos_exec.h>
52 1.1.2.2 bouyer #include <compat/sunos/sunos_syscall.h>
53 1.1.2.2 bouyer
54 1.1.2.2 bouyer #ifdef __sparc__
55 1.1.2.2 bouyer #define sunos_exec_aout_prep_zmagic exec_aout_prep_zmagic
56 1.1.2.2 bouyer #define sunos_exec_aout_prep_nmagic exec_aout_prep_nmagic
57 1.1.2.2 bouyer #define sunos_exec_aout_prep_omagic exec_aout_prep_omagic
58 1.1.2.2 bouyer #endif
59 1.1.2.2 bouyer
60 1.1.2.2 bouyer int sunos_exec_aout_prep_zmagic __P((struct proc *, struct exec_package *));
61 1.1.2.2 bouyer int sunos_exec_aout_prep_nmagic __P((struct proc *, struct exec_package *));
62 1.1.2.2 bouyer int sunos_exec_aout_prep_omagic __P((struct proc *, struct exec_package *));
63 1.1.2.2 bouyer
64 1.1.2.2 bouyer int
65 1.1.2.2 bouyer exec_sunos_aout_makecmds(p, epp)
66 1.1.2.2 bouyer struct proc *p;
67 1.1.2.2 bouyer struct exec_package *epp;
68 1.1.2.2 bouyer {
69 1.1.2.2 bouyer struct sunos_exec *sunmag = epp->ep_hdr;
70 1.1.2.2 bouyer int error = ENOEXEC;
71 1.1.2.2 bouyer
72 1.1.2.2 bouyer if(sunmag->a_machtype != SUNOS_M_NATIVE)
73 1.1.2.2 bouyer return (ENOEXEC);
74 1.1.2.2 bouyer
75 1.1.2.2 bouyer switch (sunmag->a_magic) {
76 1.1.2.2 bouyer case ZMAGIC:
77 1.1.2.2 bouyer error = sunos_exec_aout_prep_zmagic(p, epp);
78 1.1.2.2 bouyer break;
79 1.1.2.2 bouyer case NMAGIC:
80 1.1.2.2 bouyer error = sunos_exec_aout_prep_nmagic(p, epp);
81 1.1.2.2 bouyer break;
82 1.1.2.2 bouyer case OMAGIC:
83 1.1.2.2 bouyer error = sunos_exec_aout_prep_omagic(p, epp);
84 1.1.2.2 bouyer break;
85 1.1.2.2 bouyer }
86 1.1.2.2 bouyer return error;
87 1.1.2.2 bouyer }
88 1.1.2.2 bouyer
89 1.1.2.2 bouyer /*
90 1.1.2.2 bouyer * the code below is only needed for sun3 emulation.
91 1.1.2.2 bouyer */
92 1.1.2.2 bouyer #ifndef __sparc__
93 1.1.2.2 bouyer
94 1.1.2.2 bouyer /* suns keep data seg aligned to SEGSIZ because of sun custom mmu */
95 1.1.2.2 bouyer #define SEGSIZ 0x20000
96 1.1.2.2 bouyer #define SUNOS_N_TXTADDR(x,m) __LDPGSZ
97 1.1.2.2 bouyer #define SUNOS_N_DATADDR(x,m) (((m)==OMAGIC) ? \
98 1.1.2.2 bouyer (SUNOS_N_TXTADDR(x,m) + (x).a_text) : \
99 1.1.2.2 bouyer (SEGSIZ + ((SUNOS_N_TXTADDR(x,m) + (x).a_text - 1) & ~(SEGSIZ-1))))
100 1.1.2.2 bouyer #define SUNOS_N_BSSADDR(x,m) (SUNOS_N_DATADDR(x,m)+(x).a_data)
101 1.1.2.2 bouyer
102 1.1.2.2 bouyer #define SUNOS_N_TXTOFF(x,m) ((m)==ZMAGIC ? 0 : sizeof (struct exec))
103 1.1.2.2 bouyer #define SUNOS_N_DATOFF(x,m) (SUNOS_N_TXTOFF(x,m) + (x).a_text)
104 1.1.2.2 bouyer
105 1.1.2.2 bouyer /*
106 1.1.2.2 bouyer * sunos_exec_aout_prep_zmagic(): Prepare a SunOS ZMAGIC binary's exec package
107 1.1.2.2 bouyer *
108 1.1.2.2 bouyer * First, set of the various offsets/lengths in the exec package.
109 1.1.2.2 bouyer *
110 1.1.2.2 bouyer * Then, mark the text image busy (so it can be demand paged) or error
111 1.1.2.2 bouyer * out if this is not possible. Finally, set up vmcmds for the
112 1.1.2.2 bouyer * text, data, bss, and stack segments.
113 1.1.2.2 bouyer */
114 1.1.2.2 bouyer int
115 1.1.2.2 bouyer sunos_exec_aout_prep_zmagic(p, epp)
116 1.1.2.2 bouyer struct proc *p;
117 1.1.2.2 bouyer struct exec_package *epp;
118 1.1.2.2 bouyer {
119 1.1.2.2 bouyer struct exec *execp = epp->ep_hdr;
120 1.1.2.2 bouyer
121 1.1.2.2 bouyer epp->ep_taddr = SUNOS_N_TXTADDR(*execp, ZMAGIC);
122 1.1.2.2 bouyer epp->ep_tsize = execp->a_text;
123 1.1.2.2 bouyer epp->ep_daddr = SUNOS_N_DATADDR(*execp, ZMAGIC);
124 1.1.2.2 bouyer epp->ep_dsize = execp->a_data + execp->a_bss;
125 1.1.2.2 bouyer epp->ep_entry = execp->a_entry;
126 1.1.2.2 bouyer
127 1.1.2.2 bouyer /*
128 1.1.2.2 bouyer * check if vnode is in open for writing, because we want to
129 1.1.2.2 bouyer * demand-page out of it. if it is, don't do it, for various
130 1.1.2.2 bouyer * reasons
131 1.1.2.2 bouyer */
132 1.1.2.2 bouyer if ((execp->a_text != 0 || execp->a_data != 0) &&
133 1.1.2.2 bouyer epp->ep_vp->v_writecount != 0) {
134 1.1.2.2 bouyer #ifdef DIAGNOSTIC
135 1.1.2.2 bouyer if (epp->ep_vp->v_flag & VTEXT)
136 1.1.2.2 bouyer panic("exec: a VTEXT vnode has writecount != 0\n");
137 1.1.2.2 bouyer #endif
138 1.1.2.2 bouyer return ETXTBSY;
139 1.1.2.2 bouyer }
140 1.1.2.2 bouyer vn_marktext(epp->ep_vp);
141 1.1.2.2 bouyer
142 1.1.2.2 bouyer /* set up command for text segment */
143 1.1.2.2 bouyer NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
144 1.1.2.2 bouyer epp->ep_taddr, epp->ep_vp, SUNOS_N_TXTOFF(*execp, ZMAGIC),
145 1.1.2.2 bouyer VM_PROT_READ|VM_PROT_EXECUTE);
146 1.1.2.2 bouyer
147 1.1.2.2 bouyer /* set up command for data segment */
148 1.1.2.2 bouyer NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
149 1.1.2.2 bouyer epp->ep_daddr, epp->ep_vp, SUNOS_N_DATOFF(*execp, ZMAGIC),
150 1.1.2.2 bouyer VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
151 1.1.2.2 bouyer
152 1.1.2.2 bouyer /* set up command for bss segment */
153 1.1.2.2 bouyer NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
154 1.1.2.2 bouyer epp->ep_daddr + execp->a_data, NULLVP, 0,
155 1.1.2.2 bouyer VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
156 1.1.2.2 bouyer
157 1.1.2.2 bouyer return exec_aout_setup_stack(p, epp);
158 1.1.2.2 bouyer }
159 1.1.2.2 bouyer
160 1.1.2.2 bouyer /*
161 1.1.2.2 bouyer * sunos_exec_aout_prep_nmagic(): Prepare a SunOS NMAGIC binary's exec package
162 1.1.2.2 bouyer */
163 1.1.2.2 bouyer int
164 1.1.2.2 bouyer sunos_exec_aout_prep_nmagic(p, epp)
165 1.1.2.2 bouyer struct proc *p;
166 1.1.2.2 bouyer struct exec_package *epp;
167 1.1.2.2 bouyer {
168 1.1.2.2 bouyer struct exec *execp = epp->ep_hdr;
169 1.1.2.2 bouyer long bsize, baddr;
170 1.1.2.2 bouyer
171 1.1.2.2 bouyer epp->ep_taddr = SUNOS_N_TXTADDR(*execp, NMAGIC);
172 1.1.2.2 bouyer epp->ep_tsize = execp->a_text;
173 1.1.2.2 bouyer epp->ep_daddr = SUNOS_N_DATADDR(*execp, NMAGIC);
174 1.1.2.2 bouyer epp->ep_dsize = execp->a_data + execp->a_bss;
175 1.1.2.2 bouyer epp->ep_entry = execp->a_entry;
176 1.1.2.2 bouyer
177 1.1.2.2 bouyer /* set up command for text segment */
178 1.1.2.2 bouyer NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
179 1.1.2.2 bouyer epp->ep_taddr, epp->ep_vp, SUNOS_N_TXTOFF(*execp, NMAGIC),
180 1.1.2.2 bouyer VM_PROT_READ|VM_PROT_EXECUTE);
181 1.1.2.2 bouyer
182 1.1.2.2 bouyer /* set up command for data segment */
183 1.1.2.2 bouyer NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
184 1.1.2.2 bouyer epp->ep_daddr, epp->ep_vp, SUNOS_N_DATOFF(*execp, NMAGIC),
185 1.1.2.2 bouyer VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
186 1.1.2.2 bouyer
187 1.1.2.2 bouyer /* set up command for bss segment */
188 1.1.2.2 bouyer baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
189 1.1.2.2 bouyer bsize = epp->ep_daddr + epp->ep_dsize - baddr;
190 1.1.2.2 bouyer if (bsize > 0)
191 1.1.2.2 bouyer NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
192 1.1.2.2 bouyer NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
193 1.1.2.2 bouyer
194 1.1.2.2 bouyer return exec_aout_setup_stack(p, epp);
195 1.1.2.2 bouyer }
196 1.1.2.2 bouyer
197 1.1.2.2 bouyer /*
198 1.1.2.2 bouyer * sunos_exec_aout_prep_omagic(): Prepare a SunOS OMAGIC binary's exec package
199 1.1.2.2 bouyer */
200 1.1.2.2 bouyer int
201 1.1.2.2 bouyer sunos_exec_aout_prep_omagic(p, epp)
202 1.1.2.2 bouyer struct proc *p;
203 1.1.2.2 bouyer struct exec_package *epp;
204 1.1.2.2 bouyer {
205 1.1.2.2 bouyer struct exec *execp = epp->ep_hdr;
206 1.1.2.2 bouyer long bsize, baddr;
207 1.1.2.2 bouyer
208 1.1.2.2 bouyer epp->ep_taddr = SUNOS_N_TXTADDR(*execp, OMAGIC);
209 1.1.2.2 bouyer epp->ep_tsize = execp->a_text;
210 1.1.2.2 bouyer epp->ep_daddr = SUNOS_N_DATADDR(*execp, OMAGIC);
211 1.1.2.2 bouyer epp->ep_dsize = execp->a_data + execp->a_bss;
212 1.1.2.2 bouyer epp->ep_entry = execp->a_entry;
213 1.1.2.2 bouyer
214 1.1.2.2 bouyer /* set up command for text and data segments */
215 1.1.2.2 bouyer NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
216 1.1.2.2 bouyer execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
217 1.1.2.2 bouyer SUNOS_N_TXTOFF(*execp, OMAGIC), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
218 1.1.2.2 bouyer
219 1.1.2.2 bouyer /* set up command for bss segment */
220 1.1.2.2 bouyer baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
221 1.1.2.2 bouyer bsize = epp->ep_daddr + epp->ep_dsize - baddr;
222 1.1.2.2 bouyer if (bsize > 0)
223 1.1.2.2 bouyer NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
224 1.1.2.2 bouyer NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
225 1.1.2.2 bouyer
226 1.1.2.2 bouyer return exec_aout_setup_stack(p, epp);
227 1.1.2.2 bouyer }
228 1.1.2.2 bouyer #endif /* !sparc */
229