xref: /freebsd/sys/i386/linux/imgact_linux.c (revision 3460fab5fced39c7ea597cc7de0ebc3e4c88989a)
1c21dee17SSøren Schmidt /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
383ef78beSPedro F. Giffuni  *
49a14aa01SUlrich Spörlein  * Copyright (c) 1994-1996 Søren Schmidt
5c21dee17SSøren Schmidt  * All rights reserved.
6c21dee17SSøren Schmidt  *
7c21dee17SSøren Schmidt  * Based heavily on /sys/kern/imgact_aout.c which is:
8c21dee17SSøren Schmidt  * Copyright (c) 1993, David Greenman
9c21dee17SSøren Schmidt  *
10c21dee17SSøren Schmidt  * Redistribution and use in source and binary forms, with or without
11c21dee17SSøren Schmidt  * modification, are permitted provided that the following conditions
12c21dee17SSøren Schmidt  * are met:
13c21dee17SSøren Schmidt  * 1. Redistributions of source code must retain the above copyright
140ba1b365SEd Maste  *    notice, this list of conditions and the following disclaimer.
15c21dee17SSøren Schmidt  * 2. Redistributions in binary form must reproduce the above copyright
16c21dee17SSøren Schmidt  *    notice, this list of conditions and the following disclaimer in the
17c21dee17SSøren Schmidt  *    documentation and/or other materials provided with the distribution.
18c21dee17SSøren Schmidt  *
190ba1b365SEd Maste  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
200ba1b365SEd Maste  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
210ba1b365SEd Maste  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
220ba1b365SEd Maste  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
230ba1b365SEd Maste  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
240ba1b365SEd Maste  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
250ba1b365SEd Maste  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
260ba1b365SEd Maste  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
270ba1b365SEd Maste  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
280ba1b365SEd Maste  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
290ba1b365SEd Maste  * SUCH DAMAGE.
30c21dee17SSøren Schmidt  */
31c21dee17SSøren Schmidt 
32c21dee17SSøren Schmidt #include <sys/param.h>
33c21dee17SSøren Schmidt #include <sys/systm.h>
34c21dee17SSøren Schmidt #include <sys/exec.h>
35c21dee17SSøren Schmidt #include <sys/imgact.h>
36c21dee17SSøren Schmidt #include <sys/imgact_aout.h>
37c21dee17SSøren Schmidt #include <sys/kernel.h>
381cd52ec3SBruce Evans #include <sys/lock.h>
39fb919e4dSMark Murray #include <sys/mman.h>
40fb919e4dSMark Murray #include <sys/mutex.h>
41a794e791SBruce Evans #include <sys/proc.h>
421ba5ad42SEdward Tomasz Napierala #include <sys/racct.h>
43fb919e4dSMark Murray #include <sys/resourcevar.h>
44a794e791SBruce Evans #include <sys/vnode.h>
45c21dee17SSøren Schmidt 
46c21dee17SSøren Schmidt #include <vm/vm.h>
47c21dee17SSøren Schmidt #include <vm/vm_kern.h>
48ac9a8f2fSPeter Wemm #include <vm/vm_param.h>
49ac9a8f2fSPeter Wemm #include <vm/pmap.h>
50ac9a8f2fSPeter Wemm #include <vm/vm_map.h>
51e0067d71SBruce Evans #include <vm/vm_extern.h>
52c21dee17SSøren Schmidt 
53d66a5066SPeter Wemm #include <i386/linux/linux.h>
541f3dad5aSBruce Evans 
5589c9a483SAlfred Perlstein static int	exec_linux_imgact(struct image_params *iparams);
561f3dad5aSBruce Evans 
57303b270bSEivind Eklund static int
exec_linux_imgact(struct image_params * imgp)58b07cd97eSMark Murray exec_linux_imgact(struct image_params *imgp)
59c21dee17SSøren Schmidt {
60f58609f0SBruce Evans 	const struct exec *a_out = (const struct exec *) imgp->image_header;
615856e12eSJohn Dyson 	struct vmspace *vmspace;
62ede8dc43SBruce Evans 	vm_offset_t vmaddr;
63ede8dc43SBruce Evans 	unsigned long virtual_offset, file_offset;
64ede8dc43SBruce Evans 	unsigned long bss_size;
653494f31aSKonstantin Belousov 	ssize_t aresid;
66c21dee17SSøren Schmidt 	int error;
67c21dee17SSøren Schmidt 
68c21dee17SSøren Schmidt 	if (((a_out->a_magic >> 16) & 0xff) != 0x64)
69340f4a8dSEd Maste 		return (-1);
70c21dee17SSøren Schmidt 
71c21dee17SSøren Schmidt 	/*
72c21dee17SSøren Schmidt 	 * Set file/virtual offset based on a.out variant.
73c21dee17SSøren Schmidt 	 */
74c21dee17SSøren Schmidt 	switch ((int)(a_out->a_magic & 0xffff)) {
75c21dee17SSøren Schmidt 	case 0413:
76c21dee17SSøren Schmidt 		virtual_offset = 0;
77c21dee17SSøren Schmidt 		file_offset = 1024;
78c21dee17SSøren Schmidt 		break;
79c21dee17SSøren Schmidt 	case 0314:
80c21dee17SSøren Schmidt 		virtual_offset = 4096;
81c21dee17SSøren Schmidt 		file_offset = 0;
82c21dee17SSøren Schmidt 		break;
83c21dee17SSøren Schmidt 	default:
84c21dee17SSøren Schmidt 		return (-1);
85c21dee17SSøren Schmidt 	}
86c21dee17SSøren Schmidt 	bss_size = round_page(a_out->a_bss);
87d66a5066SPeter Wemm #ifdef DEBUG
88e4e6ae13SBruce Evans 	printf("imgact: text: %08lx, data: %08lx, bss: %08lx\n",
89e4e6ae13SBruce Evans 	    (u_long)a_out->a_text, (u_long)a_out->a_data, bss_size);
90d66a5066SPeter Wemm #endif
91c21dee17SSøren Schmidt 
92c21dee17SSøren Schmidt 	/*
93c21dee17SSøren Schmidt 	 * Check various fields in header for validity/bounds.
94c21dee17SSøren Schmidt 	 */
95c21dee17SSøren Schmidt 	if (a_out->a_entry < virtual_offset ||
96c21dee17SSøren Schmidt 	    a_out->a_entry >= virtual_offset + a_out->a_text ||
97f8845af0SPoul-Henning Kamp 	    a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK)
98c21dee17SSøren Schmidt 		return (-1);
99c21dee17SSøren Schmidt 
100c21dee17SSøren Schmidt 	/* text + data can't exceed file size */
101c52007c2SDavid Greenman 	if (a_out->a_data + a_out->a_text > imgp->attr->va_size)
102c21dee17SSøren Schmidt 		return (EFAULT);
103c21dee17SSøren Schmidt 	/*
104c21dee17SSøren Schmidt 	 * text/data/bss must not exceed limits
105c21dee17SSøren Schmidt 	 */
10691d5354aSJohn Baldwin 	PROC_LOCK(imgp->proc);
107cbc89bfbSPaul Saab 	if (a_out->a_text > maxtsiz ||
108f6f6d240SMateusz Guzik 	    a_out->a_data + bss_size > lim_cur_proc(imgp->proc, RLIMIT_DATA) ||
1091ba5ad42SEdward Tomasz Napierala 	    racct_set(imgp->proc, RACCT_DATA, a_out->a_data + bss_size) != 0) {
11091d5354aSJohn Baldwin 		PROC_UNLOCK(imgp->proc);
111c21dee17SSøren Schmidt 		return (ENOMEM);
11291d5354aSJohn Baldwin 	}
11391d5354aSJohn Baldwin 	PROC_UNLOCK(imgp->proc);
114c21dee17SSøren Schmidt 
115b249ce48SMateusz Guzik 	VOP_UNLOCK(imgp->vp);
116619eb6e5SJeff Roberson 
117c21dee17SSøren Schmidt 	/*
118c21dee17SSøren Schmidt 	 * Destroy old process VM and create a new one (with a new stack)
119c21dee17SSøren Schmidt 	 */
12089b57fcfSKonstantin Belousov 	error = exec_new_vmspace(imgp, &linux_sysvec);
12189b57fcfSKonstantin Belousov 	if (error)
12289b57fcfSKonstantin Belousov 		goto fail;
1235856e12eSJohn Dyson 	vmspace = imgp->proc->p_vmspace;
124c21dee17SSøren Schmidt 
125c21dee17SSøren Schmidt 	/*
126c21dee17SSøren Schmidt 	 * Check if file_offset page aligned,.
127802e08a3SAlexander Leidinger 	 * Currently we cannot handle misaligned file offsets,
128c21dee17SSøren Schmidt 	 * and so we read in the entire image (what a waste).
129c21dee17SSøren Schmidt 	 */
130f8845af0SPoul-Henning Kamp 	if (file_offset & PAGE_MASK) {
131c21dee17SSøren Schmidt #ifdef DEBUG
132e4e6ae13SBruce Evans 		printf("imgact: Non page aligned binary %lu\n", file_offset);
133c21dee17SSøren Schmidt #endif
134c21dee17SSøren Schmidt 		/*
1355297fc55SPeter Wemm 		 * Map text+data+bss read/write/execute
136c21dee17SSøren Schmidt 		 */
137c21dee17SSøren Schmidt 		vmaddr = virtual_offset;
138c21dee17SSøren Schmidt 		error = vm_map_find(&vmspace->vm_map, NULL, 0, &vmaddr,
139edb572a3SJohn Baldwin 		    a_out->a_text + a_out->a_data + bss_size, 0, VMFS_NO_SPACE,
140a4fc5c1aSJohn Dyson 		    VM_PROT_ALL, VM_PROT_ALL, 0);
141c21dee17SSøren Schmidt 		if (error)
142619eb6e5SJeff Roberson 			goto fail;
143c21dee17SSøren Schmidt 
1443494f31aSKonstantin Belousov 		error = vn_rdwr(UIO_READ, imgp->vp, (void *)vmaddr, file_offset,
1453494f31aSKonstantin Belousov 		    a_out->a_text + a_out->a_data, UIO_USERSPACE, 0,
1463494f31aSKonstantin Belousov 		    curthread->td_ucred, NOCRED, &aresid, curthread);
1473494f31aSKonstantin Belousov 		if (error != 0)
148619eb6e5SJeff Roberson 			goto fail;
1493494f31aSKonstantin Belousov 		if (aresid != 0) {
1503494f31aSKonstantin Belousov 			error = ENOEXEC;
151619eb6e5SJeff Roberson 			goto fail;
1523494f31aSKonstantin Belousov 		}
153c21dee17SSøren Schmidt 
154c21dee17SSøren Schmidt 		/*
1555297fc55SPeter Wemm 		 * remove write enable on the 'text' part
156c21dee17SSøren Schmidt 		 */
157644055e7SEd Maste 		error = vm_map_protect(&vmspace->vm_map, vmaddr,
1580659df6fSKonstantin Belousov 		    vmaddr + a_out->a_text, 0, VM_PROT_EXECUTE | VM_PROT_READ,
1590659df6fSKonstantin Belousov 		    VM_MAP_PROTECT_SET_MAXPROT);
160c21dee17SSøren Schmidt 		if (error)
161619eb6e5SJeff Roberson 			goto fail;
162644055e7SEd Maste 	} else {
163c21dee17SSøren Schmidt #ifdef DEBUG
164e4e6ae13SBruce Evans 		printf("imgact: Page aligned binary %lu\n", file_offset);
165c21dee17SSøren Schmidt #endif
166c21dee17SSøren Schmidt 		/*
1675297fc55SPeter Wemm 		 * Map text+data read/execute
168c21dee17SSøren Schmidt 		 */
169c21dee17SSøren Schmidt 		vmaddr = virtual_offset;
1705297fc55SPeter Wemm 		error = vm_mmap(&vmspace->vm_map, &vmaddr,
1715297fc55SPeter Wemm 		    a_out->a_text + a_out->a_data,
172644055e7SEd Maste 		    VM_PROT_READ | VM_PROT_EXECUTE, VM_PROT_ALL,
173644055e7SEd Maste 		    MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, imgp->vp, file_offset);
174c21dee17SSøren Schmidt 		if (error)
175619eb6e5SJeff Roberson 			goto fail;
176c21dee17SSøren Schmidt 
177d66a5066SPeter Wemm #ifdef DEBUG
178e4e6ae13SBruce Evans 		printf("imgact: startaddr=%08lx, length=%08lx\n",
179644055e7SEd Maste 		    (u_long)vmaddr,
180644055e7SEd Maste 		    (u_long)a_out->a_text + (u_long)a_out->a_data);
181d66a5066SPeter Wemm #endif
182c21dee17SSøren Schmidt 		/*
1835297fc55SPeter Wemm 		 * allow read/write of data
184c21dee17SSøren Schmidt 		 */
185644055e7SEd Maste 		error = vm_map_protect(&vmspace->vm_map, vmaddr + a_out->a_text,
1860659df6fSKonstantin Belousov 		    vmaddr + a_out->a_text + a_out->a_data, VM_PROT_ALL, 0,
1870659df6fSKonstantin Belousov 		    VM_MAP_PROTECT_SET_PROT);
188c21dee17SSøren Schmidt 		if (error)
189619eb6e5SJeff Roberson 			goto fail;
190c21dee17SSøren Schmidt 
191c21dee17SSøren Schmidt 		/*
1925297fc55SPeter Wemm 		 * Allocate anon demand-zeroed area for uninitialized data
193c21dee17SSøren Schmidt 		 */
194c21dee17SSøren Schmidt 		if (bss_size != 0) {
195c21dee17SSøren Schmidt 			vmaddr = virtual_offset + a_out->a_text + a_out->a_data;
196c21dee17SSøren Schmidt 		error = vm_map_find(&vmspace->vm_map, NULL, 0, &vmaddr,
197edb572a3SJohn Baldwin 		    bss_size, 0, VMFS_NO_SPACE, VM_PROT_ALL, VM_PROT_ALL, 0);
198c21dee17SSøren Schmidt 		if (error)
199619eb6e5SJeff Roberson 			goto fail;
200d66a5066SPeter Wemm #ifdef DEBUG
201644055e7SEd Maste 		printf("imgact: bssaddr=%08lx, length=%08lx\n", (u_long)vmaddr,
202644055e7SEd Maste 		    bss_size);
203d66a5066SPeter Wemm #endif
204c21dee17SSøren Schmidt 		}
205c21dee17SSøren Schmidt 	}
206c21dee17SSøren Schmidt 	/* Fill in process VM information */
207c21dee17SSøren Schmidt 	vmspace->vm_tsize = round_page(a_out->a_text) >> PAGE_SHIFT;
208c21dee17SSøren Schmidt 	vmspace->vm_dsize = round_page(a_out->a_data + bss_size) >> PAGE_SHIFT;
2092cf50c62SBruce Evans 	vmspace->vm_taddr = (caddr_t)(void *)(uintptr_t)virtual_offset;
210644055e7SEd Maste 	vmspace->vm_daddr =
211644055e7SEd Maste 	    (caddr_t)(void *)(uintptr_t)(virtual_offset + a_out->a_text);
212c21dee17SSøren Schmidt 
2131811c1e9SMark Johnston 	error = exec_map_stack(imgp);
2141811c1e9SMark Johnston 	if (error != 0)
2151811c1e9SMark Johnston 		goto fail;
2161811c1e9SMark Johnston 
217c21dee17SSøren Schmidt 	/* Fill in image_params */
218c52007c2SDavid Greenman 	imgp->interpreted = 0;
219c52007c2SDavid Greenman 	imgp->entry_addr = a_out->a_entry;
220c21dee17SSøren Schmidt 
221c52007c2SDavid Greenman 	imgp->proc->p_sysent = &linux_sysvec;
222619eb6e5SJeff Roberson 
223619eb6e5SJeff Roberson fail:
224cb05b60aSAttilio Rao 	vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
225619eb6e5SJeff Roberson 	return (error);
226c21dee17SSøren Schmidt }
227c21dee17SSøren Schmidt 
228c21dee17SSøren Schmidt /*
229c21dee17SSøren Schmidt  * Tell kern_execve.c about it, with a little help from the linker.
230c21dee17SSøren Schmidt  */
231eae594f7SEd Maste static struct execsw linux_execsw = { exec_linux_imgact, "Linux a.out" };
232aa855a59SPeter Wemm EXEC_SET(linuxaout, linux_execsw);
233