Sync usage with man page.
[netbsd-mini2440.git] / sys / compat / sunos / sunos_exec_aout.c
blob0231142bca4f63e6a92d4e63238d553e0d635d6c
1 /* $NetBSD: sunos_exec_aout.c,v 1.17 2007/12/08 18:36:23 dsl Exp $ */
3 /*
4 * Copyright (c) 1993 Theo de Raadt
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: sunos_exec_aout.c,v 1.17 2007/12/08 18:36:23 dsl Exp $");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/filedesc.h>
36 #include <sys/kernel.h>
37 #include <sys/proc.h>
38 #include <sys/mount.h>
39 #include <sys/malloc.h>
40 #include <sys/namei.h>
41 #include <sys/signalvar.h>
42 #include <sys/vnode.h>
43 #include <sys/file.h>
44 #include <sys/exec.h>
45 #include <sys/exec_aout.h>
46 #include <sys/resourcevar.h>
47 #include <sys/wait.h>
49 #include <sys/mman.h>
51 #include <sys/cpu.h>
52 #include <machine/reg.h>
54 #include <compat/sunos/sunos.h>
55 #include <compat/sunos/sunos_exec.h>
56 #include <compat/sunos/sunos_syscall.h>
58 #ifdef __sparc__
59 #define sunos_exec_aout_prep_zmagic exec_aout_prep_zmagic
60 #define sunos_exec_aout_prep_nmagic exec_aout_prep_nmagic
61 #define sunos_exec_aout_prep_omagic exec_aout_prep_omagic
62 #endif
64 int sunos_exec_aout_prep_zmagic(struct lwp *, struct exec_package *);
65 int sunos_exec_aout_prep_nmagic(struct lwp *, struct exec_package *);
66 int sunos_exec_aout_prep_omagic(struct lwp *, struct exec_package *);
68 int
69 exec_sunos_aout_makecmds(struct lwp *l, struct exec_package *epp)
71 struct sunos_exec *sunmag = epp->ep_hdr;
72 int error = ENOEXEC;
74 if (!SUNOS_M_NATIVE(sunmag->a_machtype))
75 return (ENOEXEC);
77 switch (sunmag->a_magic) {
78 case ZMAGIC:
79 error = sunos_exec_aout_prep_zmagic(l, epp);
80 break;
81 case NMAGIC:
82 error = sunos_exec_aout_prep_nmagic(l, epp);
83 break;
84 case OMAGIC:
85 error = sunos_exec_aout_prep_omagic(l, epp);
86 break;
88 return error;
92 * the code below is only needed for sun2/sun3 emulation.
94 #ifndef __sparc__
96 /* suns keep data seg aligned to SEGSIZ because of sun custom mmu */
97 #define SEGSIZ 0x20000
98 #define SUNOS_N_TXTADDR(x,m) 0x2000
99 #define SUNOS_N_DATADDR(x,m) (((m)==OMAGIC) ? \
100 (SUNOS_N_TXTADDR(x,m) + (x).a_text) : \
101 (SEGSIZ + ((SUNOS_N_TXTADDR(x,m) + (x).a_text - 1) & ~(SEGSIZ-1))))
102 #define SUNOS_N_BSSADDR(x,m) (SUNOS_N_DATADDR(x,m)+(x).a_data)
104 #define SUNOS_N_TXTOFF(x,m) ((m)==ZMAGIC ? 0 : sizeof (struct exec))
105 #define SUNOS_N_DATOFF(x,m) (SUNOS_N_TXTOFF(x,m) + (x).a_text)
108 * sunos_exec_aout_prep_zmagic(): Prepare a SunOS ZMAGIC binary's exec package
110 * First, set of the various offsets/lengths in the exec package.
112 * Then, mark the text image busy (so it can be demand paged) or error
113 * out if this is not possible. Finally, set up vmcmds for the
114 * text, data, bss, and stack segments.
117 sunos_exec_aout_prep_zmagic(struct lwp *l, struct exec_package *epp)
119 struct exec *execp = epp->ep_hdr;
120 int error;
122 epp->ep_taddr = SUNOS_N_TXTADDR(*execp, ZMAGIC);
123 epp->ep_tsize = execp->a_text;
124 epp->ep_daddr = SUNOS_N_DATADDR(*execp, ZMAGIC);
125 epp->ep_dsize = execp->a_data + execp->a_bss;
126 epp->ep_entry = execp->a_entry;
128 error = vn_marktext(epp->ep_vp);
129 if (error)
130 return (error);
132 /* set up command for text segment */
133 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
134 epp->ep_taddr, epp->ep_vp, SUNOS_N_TXTOFF(*execp, ZMAGIC),
135 VM_PROT_READ|VM_PROT_EXECUTE);
137 /* set up command for data segment */
138 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
139 epp->ep_daddr, epp->ep_vp, SUNOS_N_DATOFF(*execp, ZMAGIC),
140 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
142 /* set up command for bss segment */
143 if (execp->a_bss)
144 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
145 epp->ep_daddr + execp->a_data, NULLVP, 0,
146 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
148 return (*epp->ep_esch->es_setup_stack)(l, epp);
152 * sunos_exec_aout_prep_nmagic(): Prepare a SunOS NMAGIC binary's exec package
155 sunos_exec_aout_prep_nmagic(struct lwp *l, struct exec_package *epp)
157 struct exec *execp = epp->ep_hdr;
158 long bsize, baddr;
160 epp->ep_taddr = SUNOS_N_TXTADDR(*execp, NMAGIC);
161 epp->ep_tsize = execp->a_text;
162 epp->ep_daddr = SUNOS_N_DATADDR(*execp, NMAGIC);
163 epp->ep_dsize = execp->a_data + execp->a_bss;
164 epp->ep_entry = execp->a_entry;
166 /* set up command for text segment */
167 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
168 epp->ep_taddr, epp->ep_vp, SUNOS_N_TXTOFF(*execp, NMAGIC),
169 VM_PROT_READ|VM_PROT_EXECUTE);
171 /* set up command for data segment */
172 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
173 epp->ep_daddr, epp->ep_vp, SUNOS_N_DATOFF(*execp, NMAGIC),
174 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
176 /* set up command for bss segment */
177 baddr = roundup(epp->ep_daddr + execp->a_data, PAGE_SIZE);
178 bsize = epp->ep_daddr + epp->ep_dsize - baddr;
179 if (bsize > 0)
180 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
181 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
183 return (*epp->ep_esch->es_setup_stack)(l, epp);
187 * sunos_exec_aout_prep_omagic(): Prepare a SunOS OMAGIC binary's exec package
190 sunos_exec_aout_prep_omagic(struct lwp *l, struct exec_package *epp)
192 struct exec *execp = epp->ep_hdr;
193 long bsize, baddr;
195 epp->ep_taddr = SUNOS_N_TXTADDR(*execp, OMAGIC);
196 epp->ep_tsize = execp->a_text;
197 epp->ep_daddr = SUNOS_N_DATADDR(*execp, OMAGIC);
198 epp->ep_dsize = execp->a_data + execp->a_bss;
199 epp->ep_entry = execp->a_entry;
201 /* set up command for text and data segments */
202 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
203 execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
204 SUNOS_N_TXTOFF(*execp, OMAGIC), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
206 /* set up command for bss segment */
207 baddr = roundup(epp->ep_daddr + execp->a_data, PAGE_SIZE);
208 bsize = epp->ep_daddr + epp->ep_dsize - baddr;
209 if (bsize > 0)
210 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
211 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
213 return (*epp->ep_esch->es_setup_stack)(l, epp);
215 #endif /* !sparc */