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