vax1k_exec.c revision 1.8.2.5 1 /* $NetBSD: vax1k_exec.c,v 1.8.2.5 2004/09/21 13:26:14 skrll Exp $ */
2
3 /*
4 * Copyright (c) 1993, 1994 Christopher G. Demetriou
5 * All rights reserved.
6 *
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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Christopher G. Demetriou.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Exec glue to provide compatibility with older NetBSD vax1k exectuables.
35 *
36 * Because NetBSD/vax now uses 4k page size, older binaries (that started
37 * on an 1k boundary) cannot be mmap'ed. Therefore they are read in
38 * (via vn_rdwr) as OMAGIC binaries and executed. This will use a little
39 * bit more memory, but otherwise won't affect the execution speed.
40 */
41
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: vax1k_exec.c,v 1.8.2.5 2004/09/21 13:26:14 skrll Exp $");
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/proc.h>
48 #include <sys/malloc.h>
49 #include <sys/vnode.h>
50 #include <sys/exec.h>
51 #include <sys/resourcevar.h>
52
53 #include <compat/vax1k/vax1k_exec.h>
54
55 #if defined(_KERNEL_OPT)
56 #include "opt_compat_43.h"
57 #else
58 #define COMPAT_43 /* enable 4.3BSD binaries for lkm */
59 #endif
60
61 int exec_vax1k_prep_anymagic __P((struct proc *p, struct exec_package *epp,
62 int, int));
63
64 /*
65 * exec_vax1k_makecmds(): Check if it's an a.out-format executable
66 * with an vax1k magic number.
67 *
68 * Given a proc pointer and an exec package pointer, see if the referent
69 * of the epp is in a.out format. Just check 'standard' magic numbers for
70 * this architecture.
71 *
72 * This function, in the former case, or the hook, in the latter, is
73 * responsible for creating a set of vmcmds which can be used to build
74 * the process's vm space and inserting them into the exec package.
75 */
76
77 int
78 exec_vax1k_makecmds(l, epp)
79 struct lwp *l;
80 struct exec_package *epp;
81 {
82 u_long midmag, magic;
83 u_short mid;
84 int error;
85 struct exec *execp = epp->ep_hdr;
86 struct proc *p = l->l_proc;
87
88 if (epp->ep_hdrvalid < sizeof(struct exec))
89 return ENOEXEC;
90
91 midmag = ntohl(execp->a_midmag);
92 mid = (midmag >> 16) & 0x3ff;
93 magic = midmag & 0xffff;
94
95 midmag = mid << 16 | magic;
96
97 switch (midmag) {
98 case (MID_VAX1K << 16) | ZMAGIC:
99 error = exec_vax1k_prep_anymagic(p, epp, 0, 0);
100 goto done;
101
102 case (MID_VAX1K << 16) | NMAGIC:
103 error = exec_vax1k_prep_anymagic(p, epp,
104 sizeof(struct exec), 1);
105 goto done;
106
107 case (MID_VAX1K << 16) | OMAGIC:
108 error = exec_vax1k_prep_anymagic(p, epp,
109 sizeof(struct exec), 0);
110 goto done;
111 }
112
113 #ifdef COMPAT_43
114 /*
115 * 4.3BSD pre-dates CPU midmag (e.g. MID_VAX1K). instead, we
116 * expect a magic number in native byte order.
117 */
118 switch (execp->a_midmag) {
119 case ZMAGIC:
120 error = exec_vax1k_prep_anymagic(p, epp, VAX1K_LDPGSZ, 0);
121 goto done;
122
123 case NMAGIC:
124 error = exec_vax1k_prep_anymagic(p, epp,
125 sizeof(struct exec), 1);
126 goto done;
127
128 case OMAGIC:
129 error = exec_vax1k_prep_anymagic(p, epp,
130 sizeof(struct exec), 0);
131 goto done;
132 }
133 #endif
134
135 /* failed... */
136 error = ENOEXEC;
137
138 done:
139 if (error)
140 kill_vmcmds(&epp->ep_vmcmds);
141
142 return error;
143 }
144
145 /*
146 * exec_vax1k_prep_anymagic(): Prepare an vax1k ?MAGIC binary's exec package
147 *
148 * First, set of the various offsets/lengths in the exec package.
149 * Note that all code is mapped RW; no protection, but because it is
150 * only used for compatibility it won't hurt.
151 *
152 */
153 int
154 exec_vax1k_prep_anymagic(p, epp, text_foffset, textpad)
155 struct proc *p;
156 struct exec_package *epp;
157 int text_foffset, textpad;
158 {
159 struct exec *execp = epp->ep_hdr;
160
161 epp->ep_taddr = execp->a_entry & ~(VAX1K_USRTEXT - 1);
162 epp->ep_tsize = execp->a_text;
163 epp->ep_daddr = epp->ep_taddr + epp->ep_tsize;
164 if (textpad) /* pad for NMAGIC? */
165 epp->ep_daddr = (epp->ep_daddr + (VAX1K_LDPGSZ - 1)) &
166 ~(VAX1K_LDPGSZ - 1);
167 epp->ep_dsize = execp->a_data;
168 epp->ep_entry = execp->a_entry;
169
170 /* first allocate memory for text+data+bss */
171 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero,
172 round_page(epp->ep_daddr + epp->ep_dsize + execp->a_bss) -
173 trunc_page(epp->ep_taddr), /* size */
174 trunc_page(epp->ep_taddr), NULLVP, /* addr, vnode */
175 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
176
177 /* then read the text in the area we just allocated */
178 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_readvn,
179 epp->ep_tsize, epp->ep_taddr, epp->ep_vp, text_foffset,
180 VM_PROT_WRITE|VM_PROT_READ|VM_PROT_EXECUTE);
181
182 /* next read the data */
183 if (epp->ep_dsize) {
184 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_readvn,
185 epp->ep_dsize, epp->ep_daddr, epp->ep_vp,
186 text_foffset + epp->ep_tsize,
187 VM_PROT_WRITE|VM_PROT_READ|VM_PROT_EXECUTE);
188 }
189
190 /* now bump up the dsize to include the bss so that sbrk works */
191 epp->ep_dsize += execp->a_bss;
192
193 /* finally, setup the stack ... */
194 return (*epp->ep_esch->es_setup_stack)(p, epp);
195 }
196