vax1k_exec.c revision 1.4 1 /* $NetBSD: vax1k_exec.c,v 1.4 2001/08/31 18:44:12 chuck 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/param.h>
43 #include <sys/systm.h>
44 #include <sys/proc.h>
45 #include <sys/malloc.h>
46 #include <sys/vnode.h>
47 #include <sys/exec.h>
48 #include <sys/resourcevar.h>
49
50 #include <compat/vax1k/vax1k_exec.h>
51
52 #include "opt_compat_43.h"
53
54 int exec_vax1k_prep_anymagic __P((struct proc *p, struct exec_package *epp,
55 int, int));
56
57 /*
58 * exec_vax1k_makecmds(): Check if it's an a.out-format executable
59 * with an vax1k magic number.
60 *
61 * Given a proc pointer and an exec package pointer, see if the referent
62 * of the epp is in a.out format. Just check 'standard' magic numbers for
63 * this architecture.
64 *
65 * This function, in the former case, or the hook, in the latter, is
66 * responsible for creating a set of vmcmds which can be used to build
67 * the process's vm space and inserting them into the exec package.
68 */
69
70 int
71 exec_vax1k_makecmds(p, epp)
72 struct proc *p;
73 struct exec_package *epp;
74 {
75 u_long midmag, magic;
76 u_short mid;
77 int error;
78 struct exec *execp = epp->ep_hdr;
79
80 if (epp->ep_hdrvalid < sizeof(struct exec))
81 return ENOEXEC;
82
83 midmag = ntohl(execp->a_midmag);
84 mid = (midmag >> 16) & 0x3ff;
85 magic = midmag & 0xffff;
86
87 midmag = mid << 16 | magic;
88
89 switch (midmag) {
90 case (MID_VAX1K << 16) | ZMAGIC:
91 error = exec_vax1k_prep_anymagic(p, epp, 0, 0);
92 goto done;
93
94 case (MID_VAX1K << 16) | NMAGIC:
95 error = exec_vax1k_prep_anymagic(p, epp,
96 sizeof(struct exec), 1);
97 goto done;
98
99 case (MID_VAX1K << 16) | OMAGIC:
100 error = exec_vax1k_prep_anymagic(p, epp,
101 sizeof(struct exec), 0);
102 goto done;
103 }
104
105 #ifdef COMPAT_43
106 /*
107 * 4.3BSD pre-dates cpu midmag (e.g. MID_VAX1K). instead, we
108 * expect a magic number in native byte order.
109 */
110 switch (execp->a_midmag) {
111 case ZMAGIC:
112 error = exec_vax1k_prep_anymagic(p, epp, VAX1K_LDPGSZ, 0);
113 goto done;
114
115 case NMAGIC:
116 error = exec_vax1k_prep_anymagic(p, epp,
117 sizeof(struct exec), 1);
118 goto done;
119
120 case OMAGIC:
121 error = exec_vax1k_prep_anymagic(p, epp,
122 sizeof(struct exec), 0);
123 goto done;
124 }
125 #endif
126
127 /* failed... */
128 error = ENOEXEC;
129
130 done:
131 if (error)
132 kill_vmcmds(&epp->ep_vmcmds);
133
134 return error;
135 }
136
137 /*
138 * exec_vax1k_prep_anymagic(): Prepare an vax1k ?MAGIC binary's exec package
139 *
140 * First, set of the various offsets/lengths in the exec package.
141 * Note that all code is mapped RW; no protection, but because it is
142 * only used for compatibility it won't hurt.
143 *
144 */
145 int
146 exec_vax1k_prep_anymagic(p, epp, text_foffset, textpad)
147 struct proc *p;
148 struct exec_package *epp;
149 int text_foffset, textpad;
150 {
151 struct exec *execp = epp->ep_hdr;
152
153 epp->ep_taddr = execp->a_entry & ~(VAX1K_USRTEXT - 1);
154 epp->ep_tsize = execp->a_text;
155 epp->ep_daddr = epp->ep_taddr + epp->ep_tsize;
156 if (textpad) /* pad for NMAGIC? */
157 epp->ep_daddr = (epp->ep_daddr + (VAX1K_LDPGSZ - 1)) &
158 ~(VAX1K_LDPGSZ - 1);
159 epp->ep_dsize = execp->a_data;
160 epp->ep_entry = execp->a_entry;
161
162 /* first allocate memory for text+data+bss */
163 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero,
164 round_page(epp->ep_daddr + epp->ep_dsize + execp->a_bss) -
165 trunc_page(epp->ep_taddr), /* size */
166 trunc_page(epp->ep_taddr), NULLVP, /* addr, vnode */
167 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
168
169 /* then read the text in the area we just allocated */
170 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_readvn,
171 epp->ep_tsize, epp->ep_taddr, epp->ep_vp, text_foffset,
172 VM_PROT_WRITE|VM_PROT_READ|VM_PROT_EXECUTE);
173
174 /* next read the data */
175 if (epp->ep_dsize) {
176 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_readvn,
177 epp->ep_dsize, epp->ep_daddr, epp->ep_vp,
178 text_foffset + epp->ep_tsize,
179 VM_PROT_WRITE|VM_PROT_READ|VM_PROT_EXECUTE);
180 }
181
182 /* now bump up the dsize to include the bss so that sbrk works */
183 epp->ep_dsize += execp->a_bss;
184
185 /* finally, setup the stack ... */
186 return exec_aout_setup_stack(p, epp);
187 }
188