xref: /netbsd-src/sys/compat/m68k4k/m68k4k_exec.c (revision 1d577fe3790e662ec592913bd86905a501cfc612)
1 /*	$NetBSD: m68k4k_exec.c,v 1.25 2019/11/20 19:37:53 pgoyette 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 m68k4k exectuables.
35  *
36  * Taken directly from kern/exec_aout.c and frobbed to map text and
37  * data as m68k4k executables expect.
38  *
39  * This module only works on machines with PAGE_SIZE == 4096.  It's not clear
40  * that making it work on other machines is worth the trouble.
41  */
42 
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: m68k4k_exec.c,v 1.25 2019/11/20 19:37:53 pgoyette Exp $");
45 
46 #if !defined(__m68k__)
47 #error YOU GOTTA BE KIDDING!
48 #endif
49 
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/proc.h>
53 #include <sys/malloc.h>
54 #include <sys/module.h>
55 #include <sys/vnode.h>
56 #include <sys/exec.h>
57 #include <sys/exec_aout.h>
58 #include <sys/resourcevar.h>
59 
60 #include <compat/m68k4k/m68k4k_exec.h>
61 
62 MODULE(MODULE_CLASS_EXEC, exec_m68k4k, NULL);
63 
64 static struct execsw exec_m68k4k_execsw = {
65 	.es_hdrsz = sizeof(struct exec),
66 	.es_makecmds = exec_m68k4k_makecmds,
67 	.u = {
68 		.elf_probe_func = NULL,
69 	},
70 	.es_emul = &emul_netbsd,
71 	.es_prio = EXECSW_PRIO_ANY,
72 	.es_arglen = 0,
73 	.es_copyargs = copyargs,
74 	.es_setregs = NULL,
75 	.es_coredump = coredump_netbsd,
76 	.es_setup_stack = exec_setup_stack,
77 };
78 
79 static int
exec_m68k4k_modcmd(modcmd_t cmd,void * arg)80 exec_m68k4k_modcmd(modcmd_t cmd, void *arg)
81 {
82 
83 	switch (cmd) {
84 	case MODULE_CMD_INIT:
85 		return exec_add(&exec_m68k4k_execsw, 1);
86 
87 	case MODULE_CMD_FINI:
88 		return exec_remove(&exec_m68k4k_execsw, 1);
89 
90 	default:
91 		return ENOTTY;
92         }
93 }
94 
95 int	exec_m68k4k_prep_zmagic(struct lwp *, struct exec_package *);
96 int	exec_m68k4k_prep_nmagic(struct lwp *, struct exec_package *);
97 int	exec_m68k4k_prep_omagic(struct lwp *, struct exec_package *);
98 
99 /*
100  * exec_m68k4k_makecmds(): Check if it's an a.out-format executable
101  * with an m68k4k magic number.
102  *
103  * Given a proc pointer and an exec package pointer, see if the referent
104  * of the epp is in a.out format.  Just check 'standard' magic numbers for
105  * this architecture.
106  *
107  * This function, in the former case, or the hook, in the latter, is
108  * responsible for creating a set of vmcmds which can be used to build
109  * the process's vm space and inserting them into the exec package.
110  */
111 
112 int
exec_m68k4k_makecmds(struct lwp * l,struct exec_package * epp)113 exec_m68k4k_makecmds(struct lwp *l, struct exec_package *epp)
114 {
115 	u_long midmag, magic;
116 	u_short mid;
117 	int error;
118 	struct exec *execp = epp->ep_hdr;
119 
120 	/* See note above... */
121 	if (M68K4K_LDPGSZ != PAGE_SIZE)
122 		return ENOEXEC;
123 
124 	if (epp->ep_hdrvalid < sizeof(struct exec))
125 		return ENOEXEC;
126 
127 	midmag = ntohl(execp->a_midmag);
128 	mid = (midmag >> 16) & 0x3ff;
129 	magic = midmag & 0xffff;
130 
131 	midmag = mid << 16 | magic;
132 
133 	switch (midmag) {
134 	case (MID_M68K4K << 16) | ZMAGIC:
135 		error = exec_m68k4k_prep_zmagic(l, epp);
136 		break;
137 	case (MID_M68K4K << 16) | NMAGIC:
138 		error = exec_m68k4k_prep_nmagic(l, epp);
139 		break;
140 	case (MID_M68K4K << 16) | OMAGIC:
141 		error = exec_m68k4k_prep_omagic(l, epp);
142 		break;
143 	default:
144 		error = ENOEXEC;
145 	}
146 
147 	if (error)
148 		kill_vmcmds(&epp->ep_vmcmds);
149 
150 	return error;
151 }
152 
153 /*
154  * exec_m68k4k_prep_zmagic(): Prepare an m68k4k ZMAGIC binary's exec package
155  *
156  * First, set of the various offsets/lengths in the exec package.
157  *
158  * Then, mark the text image busy (so it can be demand paged) or error
159  * out if this is not possible.  Finally, set up vmcmds for the
160  * text, data, bss, and stack segments.
161  */
162 
163 int
exec_m68k4k_prep_zmagic(struct lwp * l,struct exec_package * epp)164 exec_m68k4k_prep_zmagic(struct lwp *l, struct exec_package *epp)
165 {
166 	struct exec *execp = epp->ep_hdr;
167 	int error;
168 
169 	epp->ep_taddr = M68K4K_USRTEXT;
170 	epp->ep_tsize = execp->a_text;
171 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
172 	epp->ep_dsize = execp->a_data + execp->a_bss;
173 	epp->ep_entry = execp->a_entry;
174 
175 	error = vn_marktext(epp->ep_vp);
176 	if (error)
177 		return (error);
178 
179 	/* set up command for text segment */
180 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
181 	    epp->ep_taddr, epp->ep_vp, 0, VM_PROT_READ|VM_PROT_EXECUTE);
182 
183 	/* set up command for data segment */
184 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
185 	    epp->ep_daddr, epp->ep_vp, execp->a_text,
186 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
187 
188 	/* set up command for bss segment */
189 	if (execp->a_bss)
190 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
191 		    epp->ep_daddr + execp->a_data, NULLVP, 0,
192 		    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
193 
194 	return (*epp->ep_esch->es_setup_stack)(l, epp);
195 }
196 
197 /*
198  * exec_m68k4k_prep_nmagic(): Prepare a m68k4k NMAGIC binary's exec package
199  */
200 
201 int
exec_m68k4k_prep_nmagic(struct lwp * l,struct exec_package * epp)202 exec_m68k4k_prep_nmagic(struct lwp *l, struct exec_package *epp)
203 {
204 	struct exec *execp = epp->ep_hdr;
205 	long bsize, baddr;
206 
207 	epp->ep_taddr = M68K4K_USRTEXT;
208 	epp->ep_tsize = execp->a_text;
209 	epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text,
210 	    M68K4K_LDPGSZ);
211 	epp->ep_dsize = execp->a_data + execp->a_bss;
212 	epp->ep_entry = execp->a_entry;
213 
214 	/* set up command for text segment */
215 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
216 	    epp->ep_taddr, epp->ep_vp, sizeof(struct exec),
217 	    VM_PROT_READ|VM_PROT_EXECUTE);
218 
219 	/* set up command for data segment */
220 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
221 	    epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct exec),
222 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
223 
224 	/* set up command for bss segment */
225 	baddr = roundup(epp->ep_daddr + execp->a_data, PAGE_SIZE);
226 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
227 	if (bsize > 0)
228 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
229 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
230 
231 	return (*epp->ep_esch->es_setup_stack)(l, epp);
232 }
233 
234 /*
235  * exec_m68k4k_prep_omagic(): Prepare a m68k4k OMAGIC binary's exec package
236  */
237 
238 int
exec_m68k4k_prep_omagic(struct lwp * l,struct exec_package * epp)239 exec_m68k4k_prep_omagic(struct lwp *l, struct exec_package *epp)
240 {
241 	struct exec *execp = epp->ep_hdr;
242 	long dsize, bsize, baddr;
243 
244 	epp->ep_taddr = M68K4K_USRTEXT;
245 	epp->ep_tsize = execp->a_text;
246 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
247 	epp->ep_dsize = execp->a_data + execp->a_bss;
248 	epp->ep_entry = execp->a_entry;
249 
250 	/* set up command for text and data segments */
251 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
252 	    execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
253 	    sizeof(struct exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
254 
255 	/* set up command for bss segment */
256 	baddr = roundup(epp->ep_daddr + execp->a_data, PAGE_SIZE);
257 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
258 	if (bsize > 0)
259 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
260 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
261 
262 	/*
263 	 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
264 	 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
265 	 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
266 	 * respectively to page boundaries.
267 	 * Compensate `ep_dsize' for the amount of data covered by the last
268 	 * text page.
269 	 */
270 	dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text,
271 							PAGE_SIZE);
272 	epp->ep_dsize = (dsize > 0) ? dsize : 0;
273 	return (*epp->ep_esch->es_setup_stack)(l, epp);
274 }
275