imgact_linux.c revision 10221
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.1 1995/06/25 17:32:32 sos 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
47int
48exec_linux_imgact(iparams)
49    struct image_params *iparams;
50{
51    struct exec *a_out = (struct exec *) iparams->image_header;
52    struct vmspace *vmspace = iparams->proc->p_vmspace;
53    unsigned long vmaddr, virtual_offset, file_offset;
54    unsigned long buffer, bss_size;
55    int error;
56    extern struct sysentvec linux_sysvec;
57
58    if (((a_out->a_magic >> 16) & 0xff) != 0x64)
59	return -1;
60
61    /*
62     * Set file/virtual offset based on a.out variant.
63     */
64    switch ((int)(a_out->a_magic & 0xffff)) {
65    case 0413:
66	virtual_offset = 0;
67	file_offset = 1024;
68	break;
69    case 0314:
70	virtual_offset = 4096;
71	file_offset = 0;
72	break;
73    default:
74	return (-1);
75    }
76    bss_size = round_page(a_out->a_bss);
77
78    /*
79     * Check various fields in header for validity/bounds.
80     */
81    if (a_out->a_entry < virtual_offset ||
82	a_out->a_entry >= virtual_offset + a_out->a_text ||
83	a_out->a_text % NBPG || a_out->a_data % NBPG)
84	return (-1);
85
86    /* text + data can't exceed file size */
87    if (a_out->a_data + a_out->a_text > iparams->attr->va_size)
88	return (EFAULT);
89    /*
90     * text/data/bss must not exceed limits
91     */
92    if (a_out->a_text > MAXTSIZ || a_out->a_data + bss_size > MAXDSIZ ||
93	a_out->a_data+bss_size > iparams->proc->p_rlimit[RLIMIT_DATA].rlim_cur)
94	return (ENOMEM);
95
96    /* copy in arguments and/or environment from old process */
97    error = exec_extract_strings(iparams);
98    if (error)
99	return (error);
100
101    /*
102     * Destroy old process VM and create a new one (with a new stack)
103     */
104    exec_new_vmspace(iparams);
105
106    /*
107     * Check if file_offset page aligned,.
108     * Currently we cannot handle misalinged file offsets,
109     * and so we read in the entire image (what a waste).
110     */
111    if (file_offset & PGOFSET) {
112#ifdef DEBUG
113	printf("imgact: Non page aligned binary %d\n", file_offset);
114#endif
115	/*
116	 * Map text read/execute
117	 */
118	vmaddr = virtual_offset;
119	error = vm_map_find(&vmspace->vm_map, NULL, 0, &vmaddr,
120		    	    round_page(a_out->a_text), FALSE);
121	if (error)
122	    return error;
123
124	error = vm_mmap(kernel_map, &buffer,
125			round_page(a_out->a_text + file_offset),
126			VM_PROT_READ, VM_PROT_READ, MAP_FILE,
127			(caddr_t) iparams->vnodep, trunc_page(file_offset));
128	if (error)
129	    return error;
130
131	error = copyout((caddr_t)(buffer + file_offset), (caddr_t)vmaddr,
132			a_out->a_text);
133	if (error)
134	    return error;
135
136	vm_map_remove(kernel_map, trunc_page(vmaddr),
137		      round_page(a_out->a_text + file_offset));
138
139	error = vm_map_protect(&vmspace->vm_map, vmaddr,
140		   	       round_page(a_out->a_text),
141		   	       VM_PROT_EXECUTE|VM_PROT_READ, TRUE);
142	if (error)
143	    return error;
144	/*
145	 * Map data read/write
146	 */
147	vmaddr = virtual_offset + a_out->a_text;
148	error = vm_map_find(&vmspace->vm_map, NULL, 0, &vmaddr,
149		      	    round_page(a_out->a_data + bss_size), FALSE);
150	if (error)
151	    return error;
152
153	error = vm_mmap(kernel_map, &buffer,
154			round_page(a_out->a_data + file_offset),
155			VM_PROT_READ, VM_PROT_READ, MAP_FILE,
156			(caddr_t) iparams->vnodep,
157			trunc_page(a_out->a_text + file_offset));
158	if (error)
159	    return error;
160
161	error = copyout((caddr_t)(buffer + file_offset),
162			(caddr_t)vmaddr,
163			a_out->a_data);
164	if (error)
165	    return error;
166
167	vm_map_remove(kernel_map, trunc_page(vmaddr),
168		      round_page(a_out->a_data + file_offset));
169
170	error = vm_map_protect(&vmspace->vm_map, vmaddr,
171		   	       round_page(a_out->a_data + bss_size),
172		   	       VM_PROT_WRITE|VM_PROT_READ, TRUE);
173	if (error)
174	    return error;
175    }
176    else {
177#ifdef DEBUG
178	printf("imgact: Page aligned binary %d\n", file_offset);
179#endif
180	/*
181	 * Map text read/execute
182	 */
183	vmaddr = virtual_offset;
184	error = vm_mmap(&vmspace->vm_map, &vmaddr, a_out->a_text,
185	    		VM_PROT_READ | VM_PROT_EXECUTE,
186	    		VM_PROT_READ | VM_PROT_EXECUTE | VM_PROT_WRITE,
187	    		MAP_PRIVATE | MAP_FIXED,
188	    		(caddr_t)iparams->vnodep, file_offset);
189	if (error)
190	    return (error);
191
192	/*
193	 * Map data read/write
194	 */
195	vmaddr = virtual_offset + a_out->a_text;
196	error = vm_mmap(&vmspace->vm_map, &vmaddr, a_out->a_data,
197			VM_PROT_READ | VM_PROT_WRITE,
198			VM_PROT_ALL, MAP_PRIVATE | MAP_FIXED,
199			(caddr_t)iparams->vnodep, file_offset + a_out->a_text);
200	if (error)
201	    return (error);
202
203	/*
204	 * Allocate demand-zeroed area for uninitialized data
205	 */
206	if (bss_size != 0) {
207	    vmaddr = virtual_offset + a_out->a_text + a_out->a_data;
208	    error = vm_map_find(&vmspace->vm_map, NULL, 0, &vmaddr,
209				bss_size, FALSE);
210	    if (error)
211		return (error);
212	}
213	/* Indicate that this file should not be modified */
214	iparams->vnodep->v_flag |= VTEXT;
215    }
216    /* Fill in process VM information */
217    vmspace->vm_tsize = round_page(a_out->a_text) >> PAGE_SHIFT;
218    vmspace->vm_dsize = round_page(a_out->a_data + bss_size) >> PAGE_SHIFT;
219    vmspace->vm_taddr = (caddr_t)virtual_offset;
220    vmspace->vm_daddr = (caddr_t)virtual_offset + a_out->a_text;
221
222    /* Fill in image_params */
223    iparams->interpreted = 0;
224    iparams->entry_addr = a_out->a_entry;
225
226    iparams->proc->p_sysent = &linux_sysvec;
227    return (0);
228}
229
230/*
231 * Tell kern_execve.c about it, with a little help from the linker.
232 * Since `const' objects end up in the text segment, TEXT_SET is the
233 * correct directive to use.
234 */
235const struct execsw linux_execsw = { exec_linux_imgact, "linux" };
236TEXT_SET(execsw_set, linux_execsw);
237
238