xref: /netbsd/sys/compat/vax1k/vax1k_exec.c (revision bf9ec67e)
1 /*	$NetBSD: vax1k_exec.c,v 1.6 2001/11/13 02:09:35 lukem 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.6 2001/11/13 02:09:35 lukem 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(p, epp)
79 	struct proc *p;
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 
87 	if (epp->ep_hdrvalid < sizeof(struct exec))
88 		return ENOEXEC;
89 
90 	midmag = ntohl(execp->a_midmag);
91 	mid = (midmag >> 16) & 0x3ff;
92 	magic = midmag & 0xffff;
93 
94 	midmag = mid << 16 | magic;
95 
96 	switch (midmag) {
97 	case (MID_VAX1K << 16) | ZMAGIC:
98 		error = exec_vax1k_prep_anymagic(p, epp, 0, 0);
99 		goto done;
100 
101 	case (MID_VAX1K << 16) | NMAGIC:
102 		error = exec_vax1k_prep_anymagic(p, epp,
103 						 sizeof(struct exec), 1);
104 		goto done;
105 
106 	case (MID_VAX1K << 16) | OMAGIC:
107 		error = exec_vax1k_prep_anymagic(p, epp,
108 						 sizeof(struct exec), 0);
109 		goto done;
110 	}
111 
112 #ifdef COMPAT_43
113 	/*
114 	 * 4.3BSD pre-dates cpu midmag (e.g. MID_VAX1K).   instead, we
115 	 * expect a magic number in native byte order.
116 	 */
117 	switch (execp->a_midmag) {
118 	case ZMAGIC:
119 		error = exec_vax1k_prep_anymagic(p, epp, VAX1K_LDPGSZ, 0);
120 		goto done;
121 
122 	case NMAGIC:
123 		error = exec_vax1k_prep_anymagic(p, epp,
124 					 	sizeof(struct exec), 1);
125 		goto done;
126 
127 	case OMAGIC:
128 		error = exec_vax1k_prep_anymagic(p, epp,
129 					 	sizeof(struct exec), 0);
130 		goto done;
131 	}
132 #endif
133 
134 	/* failed... */
135 	error = ENOEXEC;
136 
137 done:
138 	if (error)
139 		kill_vmcmds(&epp->ep_vmcmds);
140 
141 	return error;
142 }
143 
144 /*
145  * exec_vax1k_prep_anymagic(): Prepare an vax1k ?MAGIC binary's exec package
146  *
147  * First, set of the various offsets/lengths in the exec package.
148  * Note that all code is mapped RW; no protection, but because it is
149  * only used for compatibility it won't hurt.
150  *
151  */
152 int
153 exec_vax1k_prep_anymagic(p, epp, text_foffset, textpad)
154 	struct proc *p;
155 	struct exec_package *epp;
156 	int text_foffset, textpad;
157 {
158         struct exec *execp = epp->ep_hdr;
159 
160 	epp->ep_taddr = execp->a_entry & ~(VAX1K_USRTEXT - 1);
161 	epp->ep_tsize = execp->a_text;
162 	epp->ep_daddr = epp->ep_taddr + epp->ep_tsize;
163 	if (textpad)			/* pad for NMAGIC? */
164 		epp->ep_daddr = (epp->ep_daddr + (VAX1K_LDPGSZ - 1)) &
165 						~(VAX1K_LDPGSZ - 1);
166 	epp->ep_dsize = execp->a_data;
167 	epp->ep_entry = execp->a_entry;
168 
169 	/* first allocate memory for text+data+bss */
170         NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero,
171 		round_page(epp->ep_daddr + epp->ep_dsize + execp->a_bss) -
172 			trunc_page(epp->ep_taddr),	/* size */
173 		trunc_page(epp->ep_taddr), NULLVP,	/* addr, vnode */
174 		0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
175 
176 	/* then read the text in the area we just allocated */
177        	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_readvn,
178 		epp->ep_tsize, epp->ep_taddr, epp->ep_vp, text_foffset,
179     	VM_PROT_WRITE|VM_PROT_READ|VM_PROT_EXECUTE);
180 
181 	/* next read the data */
182 	if (epp->ep_dsize) {
183         	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_readvn,
184 			epp->ep_dsize, epp->ep_daddr, epp->ep_vp,
185 			text_foffset + epp->ep_tsize,
186 			VM_PROT_WRITE|VM_PROT_READ|VM_PROT_EXECUTE);
187 	}
188 
189 	/* now bump up the dsize to include the bss so that sbrk works */
190 	epp->ep_dsize += execp->a_bss;
191 
192 	/* finally, setup the stack ... */
193         return exec_aout_setup_stack(p, epp);
194 }
195