Implement linux_mmap2().
[dragonfly.git] / sys / emulation / linux / i386 / imgact_linux.c
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  * $FreeBSD: src/sys/i386/linux/imgact_linux.c,v 1.35.2.2 2001/11/03 01:41:08 ps Exp $
32  * $DragonFly: src/sys/emulation/linux/i386/imgact_linux.c,v 1.5 2003/11/12 01:00:33 daver Exp $
33  */
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/resourcevar.h>
38 #include <sys/exec.h>
39 #include <sys/mman.h>
40 #include <sys/imgact.h>
41 #include <sys/imgact_aout.h>
42 #include <sys/kernel.h>
43 #include <sys/lock.h>
44 #include <sys/proc.h>
45 #include <sys/vnode.h>
46
47 #include <vm/vm.h>
48 #include <vm/vm_kern.h>
49 #include <vm/vm_param.h>
50 #include <vm/pmap.h>
51 #include <vm/vm_map.h>
52 #include <vm/vm_extern.h>
53
54 #include "linux.h"
55
56 static int      exec_linux_imgact (struct image_params *iparams);
57
58 static int
59 exec_linux_imgact(imgp)
60     struct image_params *imgp;
61 {
62     const struct exec *a_out = (const struct exec *) imgp->image_header;
63     struct vmspace *vmspace;
64     vm_offset_t vmaddr;
65     unsigned long virtual_offset, file_offset;
66     vm_offset_t buffer;
67     unsigned long bss_size;
68     int error;
69
70     if (((a_out->a_magic >> 16) & 0xff) != 0x64)
71         return -1;
72
73     /*
74      * Set file/virtual offset based on a.out variant.
75      */
76     switch ((int)(a_out->a_magic & 0xffff)) {
77     case 0413:
78         virtual_offset = 0;
79         file_offset = 1024;
80         break;
81     case 0314:
82         virtual_offset = 4096;
83         file_offset = 0;
84         break;
85     default:
86         return (-1);
87     }
88     bss_size = round_page(a_out->a_bss);
89 #ifdef DEBUG
90     printf("imgact: text: %08lx, data: %08lx, bss: %08lx\n",
91         (u_long)a_out->a_text, (u_long)a_out->a_data, bss_size);
92 #endif
93
94     /*
95      * Check various fields in header for validity/bounds.
96      */
97     if (a_out->a_entry < virtual_offset ||
98         a_out->a_entry >= virtual_offset + a_out->a_text ||
99         a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK)
100         return (-1);
101
102     /* text + data can't exceed file size */
103     if (a_out->a_data + a_out->a_text > imgp->attr->va_size)
104         return (EFAULT);
105     /*
106      * text/data/bss must not exceed limits
107      */
108     if (a_out->a_text > maxtsiz ||
109         a_out->a_data + bss_size > imgp->proc->p_rlimit[RLIMIT_DATA].rlim_cur)
110         return (ENOMEM);
111
112     /*
113      * Destroy old process VM and create a new one (with a new stack)
114      */
115     exec_new_vmspace(imgp);
116     vmspace = imgp->proc->p_vmspace;
117
118     /*
119      * Check if file_offset page aligned,.
120      * Currently we cannot handle misalinged file offsets,
121      * and so we read in the entire image (what a waste).
122      */
123     if (file_offset & PAGE_MASK) {
124 #ifdef DEBUG
125         printf("imgact: Non page aligned binary %lu\n", file_offset);
126 #endif
127         /*
128          * Map text+data+bss read/write/execute
129          */
130         vmaddr = virtual_offset;
131         error = vm_map_find(&vmspace->vm_map, NULL, 0, &vmaddr,
132                             a_out->a_text + a_out->a_data + bss_size, FALSE,
133                             VM_PROT_ALL, VM_PROT_ALL, 0);
134         if (error)
135             return error;
136
137         error = vm_mmap(kernel_map, &buffer,
138                         round_page(a_out->a_text + a_out->a_data + file_offset),
139                         VM_PROT_READ, VM_PROT_READ, 0,
140                         (caddr_t) imgp->vp, trunc_page(file_offset));
141         if (error)
142             return error;
143
144         error = copyout((caddr_t)(void *)(uintptr_t)(buffer + file_offset),
145                         (caddr_t)vmaddr, a_out->a_text + a_out->a_data);
146
147         vm_map_remove(kernel_map, buffer,
148                       buffer + round_page(a_out->a_text + a_out->a_data + file_offset));
149
150         if (error)
151             return error;
152
153         /*
154          * remove write enable on the 'text' part
155          */
156         error = vm_map_protect(&vmspace->vm_map,
157                                vmaddr,
158                                vmaddr + a_out->a_text,
159                                VM_PROT_EXECUTE|VM_PROT_READ,
160                                TRUE);
161         if (error)
162             return error;
163     }
164     else {
165 #ifdef DEBUG
166         printf("imgact: Page aligned binary %lu\n", file_offset);
167 #endif
168         /*
169          * Map text+data read/execute
170          */
171         vmaddr = virtual_offset;
172         error = vm_mmap(&vmspace->vm_map, &vmaddr,
173                         a_out->a_text + a_out->a_data,
174                         VM_PROT_READ | VM_PROT_EXECUTE,
175                         VM_PROT_ALL,
176                         MAP_PRIVATE | MAP_FIXED,
177                         (caddr_t)imgp->vp, file_offset);
178         if (error)
179             return (error);
180     
181 #ifdef DEBUG
182         printf("imgact: startaddr=%08lx, length=%08lx\n",
183             (u_long)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=%08lx, length=%08lx\n",
207                 (u_long)vmaddr, bss_size);
208 #endif
209
210         }
211         /* Indicate that this file should not be modified */
212         imgp->vp->v_flag |= VTEXT;
213     }
214     /* Fill in process VM information */
215     vmspace->vm_tsize = round_page(a_out->a_text) >> PAGE_SHIFT;
216     vmspace->vm_dsize = round_page(a_out->a_data + bss_size) >> PAGE_SHIFT;
217     vmspace->vm_taddr = (caddr_t)(void *)(uintptr_t)virtual_offset;
218     vmspace->vm_daddr = (caddr_t)(void *)(uintptr_t)
219         (virtual_offset + a_out->a_text);
220
221     /* Fill in image_params */
222     imgp->interpreted = 0;
223     imgp->entry_addr = a_out->a_entry;
224     
225     imgp->proc->p_sysent = &linux_sysvec;
226     return (0);
227 }
228
229 /*
230  * Tell kern_execve.c about it, with a little help from the linker.
231  */
232 static struct execsw linux_execsw = { exec_linux_imgact, "linux a.out" };
233 EXEC_SET(linuxaout, linux_execsw);