imgact_linux.c revision 12689
1/*-
2 * Copyright (c) 1994-1995 S�ren Schmidt
3 * All rights reserved.
4 *
5 * Based heavily on /sys/kern/imgact_aout.c which is:
6 * Copyright (c) 1993, David Greenman
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer
13 *    in this position and unchanged.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 *    derived from this software withough specific prior written permission
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 *	$Id: imgact_linux.c,v 1.4 1995/11/22 07:43:42 bde Exp $
32 */
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/resourcevar.h>
37#include <sys/exec.h>
38#include <sys/mman.h>
39#include <sys/imgact.h>
40#include <sys/imgact_aout.h>
41#include <sys/kernel.h>
42#include <sys/sysent.h>
43
44#include <vm/vm.h>
45#include <vm/vm_kern.h>
46#include <vm/vm_param.h>
47#include <vm/pmap.h>
48#include <vm/lock.h>
49#include <vm/vm_map.h>
50#include <vm/vm_prot.h>
51
52#include <i386/linux/sysproto.h>
53
54extern int	exec_linux_imgact __P((struct image_params *iparams));
55
56int
57exec_linux_imgact(imgp)
58    struct image_params *imgp;
59{
60    struct exec *a_out = (struct exec *) imgp->image_header;
61    struct vmspace *vmspace = imgp->proc->p_vmspace;
62    unsigned long vmaddr, virtual_offset, file_offset;
63    unsigned long buffer, bss_size;
64    int error;
65
66    if (((a_out->a_magic >> 16) & 0xff) != 0x64)
67	return -1;
68
69    /*
70     * Set file/virtual offset based on a.out variant.
71     */
72    switch ((int)(a_out->a_magic & 0xffff)) {
73    case 0413:
74	virtual_offset = 0;
75	file_offset = 1024;
76	break;
77    case 0314:
78	virtual_offset = 4096;
79	file_offset = 0;
80	break;
81    default:
82	return (-1);
83    }
84    bss_size = round_page(a_out->a_bss);
85
86    /*
87     * Check various fields in header for validity/bounds.
88     */
89    if (a_out->a_entry < virtual_offset ||
90	a_out->a_entry >= virtual_offset + a_out->a_text ||
91	a_out->a_text % NBPG || a_out->a_data % NBPG)
92	return (-1);
93
94    /* text + data can't exceed file size */
95    if (a_out->a_data + a_out->a_text > imgp->attr->va_size)
96	return (EFAULT);
97    /*
98     * text/data/bss must not exceed limits
99     */
100    if (a_out->a_text > MAXTSIZ || a_out->a_data + bss_size > MAXDSIZ ||
101	a_out->a_data+bss_size > imgp->proc->p_rlimit[RLIMIT_DATA].rlim_cur)
102	return (ENOMEM);
103
104    /* copy in arguments and/or environment from old process */
105    error = exec_extract_strings(imgp);
106    if (error)
107	return (error);
108
109    /*
110     * Destroy old process VM and create a new one (with a new stack)
111     */
112    exec_new_vmspace(imgp);
113
114    /*
115     * Check if file_offset page aligned,.
116     * Currently we cannot handle misalinged file offsets,
117     * and so we read in the entire image (what a waste).
118     */
119    if (file_offset & PGOFSET) {
120#ifdef DEBUG
121	printf("imgact: Non page aligned binary %d\n", file_offset);
122#endif
123	/*
124	 * Map text read/execute
125	 */
126	vmaddr = virtual_offset;
127	error = vm_map_find(&vmspace->vm_map, NULL, 0, &vmaddr,
128		    	    round_page(a_out->a_text), FALSE);
129	if (error)
130	    return error;
131
132	error = vm_mmap(kernel_map, &buffer,
133			round_page(a_out->a_text + file_offset),
134			VM_PROT_READ, VM_PROT_READ, MAP_FILE,
135			(caddr_t) imgp->vp, trunc_page(file_offset));
136	if (error)
137	    return error;
138
139	error = copyout((caddr_t)(buffer + file_offset), (caddr_t)vmaddr,
140			a_out->a_text);
141	if (error)
142	    return error;
143
144	vm_map_remove(kernel_map, trunc_page(vmaddr),
145		      round_page(a_out->a_text + file_offset));
146
147	error = vm_map_protect(&vmspace->vm_map, vmaddr,
148		   	       round_page(a_out->a_text),
149		   	       VM_PROT_EXECUTE|VM_PROT_READ, TRUE);
150	if (error)
151	    return error;
152	/*
153	 * Map data read/write
154	 */
155	vmaddr = virtual_offset + a_out->a_text;
156	error = vm_map_find(&vmspace->vm_map, NULL, 0, &vmaddr,
157		      	    round_page(a_out->a_data + bss_size), FALSE);
158	if (error)
159	    return error;
160
161	error = vm_mmap(kernel_map, &buffer,
162			round_page(a_out->a_data + file_offset),
163			VM_PROT_READ, VM_PROT_READ, MAP_FILE,
164			(caddr_t) imgp->vp,
165			trunc_page(a_out->a_text + file_offset));
166	if (error)
167	    return error;
168
169	error = copyout((caddr_t)(buffer + file_offset),
170			(caddr_t)vmaddr,
171			a_out->a_data);
172	if (error)
173	    return error;
174
175	vm_map_remove(kernel_map, trunc_page(vmaddr),
176		      round_page(a_out->a_data + file_offset));
177
178	error = vm_map_protect(&vmspace->vm_map, vmaddr,
179		   	       round_page(a_out->a_data + bss_size),
180		   	       VM_PROT_WRITE|VM_PROT_READ, TRUE);
181	if (error)
182	    return error;
183    }
184    else {
185#ifdef DEBUG
186	printf("imgact: Page aligned binary %d\n", file_offset);
187#endif
188	/*
189	 * Map text read/execute
190	 */
191	vmaddr = virtual_offset;
192	error = vm_mmap(&vmspace->vm_map, &vmaddr, a_out->a_text,
193	    		VM_PROT_READ | VM_PROT_EXECUTE,
194	    		VM_PROT_READ | VM_PROT_EXECUTE | VM_PROT_WRITE,
195	    		MAP_PRIVATE | MAP_FIXED,
196	    		(caddr_t)imgp->vp, file_offset);
197	if (error)
198	    return (error);
199
200	/*
201	 * Map data read/write
202	 */
203	vmaddr = virtual_offset + a_out->a_text;
204	error = vm_mmap(&vmspace->vm_map, &vmaddr, a_out->a_data,
205			VM_PROT_READ | VM_PROT_WRITE,
206			VM_PROT_ALL, MAP_PRIVATE | MAP_FIXED,
207			(caddr_t)imgp->vp, file_offset + a_out->a_text);
208	if (error)
209	    return (error);
210
211	/*
212	 * Allocate demand-zeroed area for uninitialized data
213	 */
214	if (bss_size != 0) {
215	    vmaddr = virtual_offset + a_out->a_text + a_out->a_data;
216	    error = vm_map_find(&vmspace->vm_map, NULL, 0, &vmaddr,
217				bss_size, FALSE);
218	    if (error)
219		return (error);
220	}
221	/* Indicate that this file should not be modified */
222	imgp->vp->v_flag |= VTEXT;
223    }
224    /* Fill in process VM information */
225    vmspace->vm_tsize = round_page(a_out->a_text) >> PAGE_SHIFT;
226    vmspace->vm_dsize = round_page(a_out->a_data + bss_size) >> PAGE_SHIFT;
227    vmspace->vm_taddr = (caddr_t)virtual_offset;
228    vmspace->vm_daddr = (caddr_t)virtual_offset + a_out->a_text;
229
230    /* Fill in image_params */
231    imgp->interpreted = 0;
232    imgp->entry_addr = a_out->a_entry;
233
234    imgp->proc->p_sysent = &linux_sysvec;
235    return (0);
236}
237
238/*
239 * Tell kern_execve.c about it, with a little help from the linker.
240 * Since `const' objects end up in the text segment, TEXT_SET is the
241 * correct directive to use.
242 */
243const struct execsw linux_execsw = { exec_linux_imgact, "linux" };
244TEXT_SET(execsw_set, linux_execsw);
245
246