proc->thread stage 4: rework the VFS and DEVICE subsystems to take thread
[dragonfly.git] / sys / emulation / ibcs2 / coff / imgact_coff.c
1 /*-
2  * Copyright (c) 1994 Sean Eric Fagan
3  * Copyright (c) 1994 Søren Schmidt
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer
11  *    in this position and unchanged.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software withough specific prior written permission
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * $FreeBSD: src/sys/i386/ibcs2/imgact_coff.c,v 1.40 1999/12/15 23:01:47 eivind Exp $
30  * $DragonFly: src/sys/emulation/ibcs2/coff/Attic/imgact_coff.c,v 1.3 2003/06/25 03:55:53 dillon Exp $
31  */
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/mman.h>
36 #include <sys/imgact.h>
37 #include <sys/kernel.h>
38 #include <sys/fcntl.h>
39 #include <sys/malloc.h>
40 #include <sys/mount.h>
41 #include <sys/proc.h>
42 #include <sys/namei.h>
43 #include <sys/vnode.h>
44
45 #include <vm/vm.h>
46 #include <vm/pmap.h>
47 #include <vm/vm_map.h>
48 #include <vm/vm_kern.h>
49 #include <vm/vm_extern.h>
50 #include <vm/vm_zone.h>
51
52 #include <i386/ibcs2/coff.h>
53 #include <i386/ibcs2/ibcs2_util.h>
54
55 extern struct sysentvec ibcs2_svr3_sysvec;
56
57 static int coff_load_file __P((struct thread *td, char *name));
58 static int exec_coff_imgact __P((struct image_params *imgp));
59
60 static int load_coff_section __P((struct vmspace *vmspace, struct vnode *vp, vm_offset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot));
61
62 static int
63 load_coff_section(struct vmspace *vmspace, struct vnode *vp, vm_offset_t offset,
64                   caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot)
65 {
66         size_t map_len;
67         vm_offset_t map_offset;
68         vm_offset_t map_addr;
69         int error;
70         unsigned char *data_buf = 0;
71         size_t copy_len;
72
73         map_offset = trunc_page(offset);
74         map_addr = trunc_page((vm_offset_t)vmaddr);
75
76         if (memsz > filsz) {
77                 /*
78                  * We have the stupid situation that
79                  * the section is longer than it is on file,
80                  * which means it has zero-filled areas, and
81                  * we have to work for it.  Stupid iBCS!
82                  */
83                 map_len = trunc_page(offset + filsz) - trunc_page(map_offset);
84         } else {
85                 /*
86                  * The only stuff we care about is on disk, and we
87                  * don't care if we map in more than is really there.
88                  */
89                 map_len = round_page(offset + filsz) - trunc_page(map_offset);
90         }
91
92         DPRINTF(("%s(%d):  vm_mmap(&vmspace->vm_map, &0x%08lx, 0x%x, 0x%x, "
93                 "VM_PROT_ALL, MAP_PRIVATE | MAP_FIXED, vp, 0x%x)\n",
94                 __FILE__, __LINE__, map_addr, map_len, prot, map_offset));
95
96         if ((error = vm_mmap(&vmspace->vm_map,
97                              &map_addr,
98                              map_len,
99                              prot,
100                              VM_PROT_ALL,
101                              MAP_PRIVATE | MAP_FIXED,
102                              (caddr_t) vp,
103                              map_offset)) != 0)
104                 return error;
105
106         if (memsz == filsz) {
107                 /* We're done! */
108                 return 0;
109         }
110
111         /*
112          * Now we have screwball stuff, to accomodate stupid COFF.
113          * We have to map the remaining bit of the file into the kernel's
114          * memory map, allocate some anonymous memory, copy that last
115          * bit into it, and then we're done. *sigh*
116          * For clean-up reasons, we actally map in the file last.
117          */
118
119         copy_len = (offset + filsz) - trunc_page(offset + filsz);
120         map_addr = trunc_page((vm_offset_t)vmaddr + filsz);
121         map_len = round_page((vm_offset_t)vmaddr + memsz) - map_addr;
122
123         DPRINTF(("%s(%d): vm_map_find(&vmspace->vm_map, NULL, 0, &0x%08lx,0x%x, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0)\n", __FILE__, __LINE__, map_addr, map_len));
124
125         if (map_len != 0) {
126                 error = vm_map_find(&vmspace->vm_map, NULL, 0, &map_addr,
127                                     map_len, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
128                 if (error)
129                         return error;
130         }
131
132         if ((error = vm_mmap(kernel_map,
133                             (vm_offset_t *) &data_buf,
134                             PAGE_SIZE,
135                             VM_PROT_READ,
136                             VM_PROT_READ,
137                             0,
138                             (caddr_t) vp,
139                             trunc_page(offset + filsz))) != 0)
140                 return error;
141
142         error = copyout(data_buf, (caddr_t) map_addr, copy_len);
143
144         if (vm_map_remove(kernel_map,
145                           (vm_offset_t) data_buf,
146                           (vm_offset_t) data_buf + PAGE_SIZE))
147                 panic("load_coff_section vm_map_remove failed");
148
149         return error;
150 }
151
152 static int
153 coff_load_file(struct thread *td, char *name)
154 {
155         struct vmspace *vmspace;
156         int error;
157         struct nameidata nd;
158         struct vnode *vp;
159         struct vattr attr;
160         struct filehdr *fhdr;
161         struct aouthdr *ahdr;
162         struct scnhdr *scns;
163         char *ptr = 0;
164         int nscns;
165         unsigned long text_offset = 0, text_address = 0, text_size = 0;
166         unsigned long data_offset = 0, data_address = 0, data_size = 0;
167         unsigned long bss_size = 0;
168         struct ucred *cred;
169         int i;
170
171         KKASSERT(td->td_proc);
172         cred = td->td_proc->p_ucred;
173         vmspace = td->td_proc->p_vmspace;
174
175         /* XXX use of 'curthread' should be 'td'?*/
176         NDINIT(&nd, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, UIO_SYSSPACE, name, curthread);
177
178         error = namei(&nd);
179         if (error)
180                 return error;
181
182         vp = nd.ni_vp;
183         if (vp == NULL)
184                 return ENOEXEC;
185
186         if (vp->v_writecount) {
187                 error = ETXTBSY;
188                 goto fail;
189         }
190
191         if ((error = VOP_GETATTR(vp, &attr, cred, td)) != 0)
192                 goto fail;
193
194         if ((vp->v_mount->mnt_flag & MNT_NOEXEC)
195             || ((attr.va_mode & 0111) == 0)
196             || (attr.va_type != VREG))
197                 goto fail;
198
199         if (attr.va_size == 0) {
200                 error = ENOEXEC;
201                 goto fail;
202         }
203
204         if ((error = VOP_ACCESS(vp, VEXEC, cred, td)) != 0)
205                 goto fail;
206
207         if ((error = VOP_OPEN(vp, FREAD, cred, td)) != 0)
208                 goto fail;
209
210         /*
211          * Lose the lock on the vnode. It's no longer needed, and must not
212          * exist for the pagefault paging to work below.
213          */
214         VOP_UNLOCK(vp, 0, td);
215
216         if ((error = vm_mmap(kernel_map,
217                             (vm_offset_t *) &ptr,
218                             PAGE_SIZE,
219                             VM_PROT_READ,
220                             VM_PROT_READ,
221                             0,
222                             (caddr_t) vp,
223                             0)) != 0)
224                 goto unlocked_fail;
225
226         fhdr = (struct filehdr *)ptr;
227
228         if (fhdr->f_magic != I386_COFF) {
229                 error = ENOEXEC;
230                 goto dealloc_and_fail;
231         }
232
233         nscns = fhdr->f_nscns;
234
235         if ((nscns * sizeof(struct scnhdr)) > PAGE_SIZE) {
236                 /*
237                  * XXX -- just fail.  I'm so lazy.
238                  */
239                 error = ENOEXEC;
240                 goto dealloc_and_fail;
241         }
242
243         ahdr = (struct aouthdr*)(ptr + sizeof(struct filehdr));
244
245         scns = (struct scnhdr*)(ptr + sizeof(struct filehdr)
246                           + sizeof(struct aouthdr));
247
248         for (i = 0; i < nscns; i++) {
249                 if (scns[i].s_flags & STYP_NOLOAD)
250                         continue;
251                 else if (scns[i].s_flags & STYP_TEXT) {
252                         text_address = scns[i].s_vaddr;
253                         text_size = scns[i].s_size;
254                         text_offset = scns[i].s_scnptr;
255                 }
256                 else if (scns[i].s_flags & STYP_DATA) {
257                         data_address = scns[i].s_vaddr;
258                         data_size = scns[i].s_size;
259                         data_offset = scns[i].s_scnptr;
260                 } else if (scns[i].s_flags & STYP_BSS) {
261                         bss_size = scns[i].s_size;
262                 }
263         }
264
265         if ((error = load_coff_section(vmspace, vp, text_offset,
266                                       (caddr_t)(void *)(uintptr_t)text_address,
267                                       text_size, text_size,
268                                       VM_PROT_READ | VM_PROT_EXECUTE)) != 0) {
269                 goto dealloc_and_fail;
270         }
271         if ((error = load_coff_section(vmspace, vp, data_offset,
272                                       (caddr_t)(void *)(uintptr_t)data_address,
273                                       data_size + bss_size, data_size,
274                                       VM_PROT_ALL)) != 0) {
275                 goto dealloc_and_fail;
276         }
277
278         error = 0;
279
280  dealloc_and_fail:
281         if (vm_map_remove(kernel_map,
282                           (vm_offset_t) ptr,
283                           (vm_offset_t) ptr + PAGE_SIZE))
284                 panic(__FUNCTION__ " vm_map_remove failed");
285
286  fail:
287         VOP_UNLOCK(vp, 0, td);
288  unlocked_fail:
289         NDFREE(&nd, NDF_ONLY_PNBUF);
290         vrele(nd.ni_vp);
291         return error;
292 }
293
294 static int
295 exec_coff_imgact(imgp)
296         struct image_params *imgp;
297 {
298         const struct filehdr *fhdr = (const struct filehdr*)imgp->image_header;
299         const struct aouthdr *ahdr;
300         const struct scnhdr *scns;
301         int i;
302         struct vmspace *vmspace;
303         int nscns;
304         int error;
305         unsigned long text_offset = 0, text_address = 0, text_size = 0;
306         unsigned long data_offset = 0, data_address = 0, data_size = 0;
307         unsigned long bss_size = 0;
308         caddr_t hole;
309
310         if (fhdr->f_magic != I386_COFF ||
311             !(fhdr->f_flags & F_EXEC)) {
312
313                  DPRINTF(("%s(%d): return -1\n", __FILE__, __LINE__));
314                  return -1;
315         }
316
317         nscns = fhdr->f_nscns;
318         if ((nscns * sizeof(struct scnhdr)) > PAGE_SIZE) {
319                 /*
320                  * For now, return an error -- need to be able to
321                  * read in all of the section structures.
322                  */
323
324                 DPRINTF(("%s(%d): return -1\n", __FILE__, __LINE__));
325                 return -1;
326         }
327
328         ahdr = (const struct aouthdr*)
329                ((const char*)(imgp->image_header) + sizeof(struct filehdr));
330         imgp->entry_addr = ahdr->entry;
331
332         scns = (const struct scnhdr*)
333                ((const char*)(imgp->image_header) + sizeof(struct filehdr) +
334                 sizeof(struct aouthdr));
335
336         if ((error = exec_extract_strings(imgp)) != 0) {
337                 DPRINTF(("%s(%d):  return %d\n", __FILE__, __LINE__, error));
338                 return error;
339         }
340
341         exec_new_vmspace(imgp);
342         vmspace = imgp->proc->p_vmspace;
343
344         for (i = 0; i < nscns; i++) {
345
346           DPRINTF(("i = %d, scns[i].s_name = %s, scns[i].s_vaddr = %08lx, "
347                    "scns[i].s_scnptr = %d\n", i, scns[i].s_name,
348                    scns[i].s_vaddr, scns[i].s_scnptr));
349           if (scns[i].s_flags & STYP_NOLOAD) {
350                 /*
351                  * A section that is not loaded, for whatever
352                  * reason.  It takes precedance over other flag
353                  * bits...
354                  */
355                 continue;
356           } else if (scns[i].s_flags & STYP_TEXT) {
357                 text_address = scns[i].s_vaddr;
358                 text_size = scns[i].s_size;
359                 text_offset = scns[i].s_scnptr;
360           } else if (scns[i].s_flags & STYP_DATA) {
361                 /* .data section */
362                 data_address = scns[i].s_vaddr;
363                 data_size = scns[i].s_size;
364                 data_offset = scns[i].s_scnptr;
365           } else if (scns[i].s_flags & STYP_BSS) {
366                 /* .bss section */
367                 bss_size = scns[i].s_size;
368           } else if (scns[i].s_flags & STYP_LIB) {
369                 char *buf = 0;
370                 int foff = trunc_page(scns[i].s_scnptr);
371                 int off = scns[i].s_scnptr - foff;
372                 int len = round_page(scns[i].s_size + PAGE_SIZE);
373                 int j;
374
375                 if ((error = vm_mmap(kernel_map,
376                                     (vm_offset_t *) &buf,
377                                     len,
378                                     VM_PROT_READ,
379                                     VM_PROT_READ,
380                                     0,
381                                     (caddr_t) imgp->vp,
382                                     foff)) != 0) {
383                         return ENOEXEC;
384                 }
385                 if(scns[i].s_size) {
386                         char *libbuf;
387                         int emul_path_len = strlen(ibcs2_emul_path);
388
389                         libbuf = malloc(MAXPATHLEN + emul_path_len,
390                                         M_TEMP, M_WAITOK);
391                         strcpy(libbuf, ibcs2_emul_path);
392
393                         for (j = off; j < scns[i].s_size + off; j++) {
394                                 char *libname;
395
396                                 libname = buf + j + 4 * *(long*)(buf + j + 4);
397                                 j += 4* *(long*)(buf + j);
398
399                                 DPRINTF(("%s(%d):  shared library %s\n",
400                                          __FILE__, __LINE__, libname));
401                                 strcpy(&libbuf[emul_path_len], libname);
402                                 error = coff_load_file(imgp->proc->p_thread, libbuf);
403                                 if (error)
404                                         error = coff_load_file(
405                                                     imgp->proc->p_thread,
406                                                     libname);
407                                 if (error)
408                                         break;
409                         }
410                         free(libbuf, M_TEMP);
411                 }
412                 if (vm_map_remove(kernel_map,
413                                   (vm_offset_t) buf,
414                                   (vm_offset_t) buf + len))
415                         panic("exec_coff_imgact vm_map_remove failed");
416                 if (error)
417                         return error;
418                 }
419         }
420         /*
421          * Map in .text now
422          */
423
424         DPRINTF(("%s(%d):  load_coff_section(vmspace, "
425                 "imgp->vp, %08lx, %08lx, 0x%x, 0x%x, 0x%x)\n",
426                 __FILE__, __LINE__, text_offset, text_address,
427                 text_size, text_size, VM_PROT_READ | VM_PROT_EXECUTE));
428         if ((error = load_coff_section(vmspace, imgp->vp,
429                                       text_offset,
430                                       (caddr_t)(void *)(uintptr_t)text_address,
431                                       text_size, text_size,
432                                       VM_PROT_READ | VM_PROT_EXECUTE)) != 0) {
433                 DPRINTF(("%s(%d): error = %d\n", __FILE__, __LINE__, error));
434                 return error;
435         }
436         /*
437          * Map in .data and .bss now
438          */
439
440
441         DPRINTF(("%s(%d): load_coff_section(vmspace, "
442                 "imgp->vp, 0x%08lx, 0x%08lx, 0x%x, 0x%x, 0x%x)\n",
443                 __FILE__, __LINE__, data_offset, data_address,
444                 data_size + bss_size, data_size, VM_PROT_ALL));
445         if ((error = load_coff_section(vmspace, imgp->vp,
446                                       data_offset,
447                                       (caddr_t)(void *)(uintptr_t)data_address,
448                                       data_size + bss_size, data_size,
449                                       VM_PROT_ALL)) != 0) {
450
451                 DPRINTF(("%s(%d): error = %d\n", __FILE__, __LINE__, error));
452                 return error;
453         }
454
455         imgp->interpreted = 0;
456         imgp->proc->p_sysent = &ibcs2_svr3_sysvec;
457
458         vmspace->vm_tsize = round_page(text_size) >> PAGE_SHIFT;
459         vmspace->vm_dsize = round_page(data_size + bss_size) >> PAGE_SHIFT;
460         vmspace->vm_taddr = (caddr_t)(void *)(uintptr_t)text_address;
461         vmspace->vm_daddr = (caddr_t)(void *)(uintptr_t)data_address;
462
463         hole = (caddr_t)trunc_page((vm_offset_t)vmspace->vm_daddr) + ctob(vmspace->vm_dsize);
464
465
466         DPRINTF(("%s(%d): vm_map_find(&vmspace->vm_map, NULL, 0, &0x%08lx, PAGE_SIZE, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0)\n",
467                 __FILE__, __LINE__, hole));
468         DPRINTF(("imgact: error = %d\n", error));
469
470         error = vm_map_find(&vmspace->vm_map, NULL, 0,
471                             (vm_offset_t *) &hole, PAGE_SIZE, FALSE,
472                                 VM_PROT_ALL, VM_PROT_ALL, 0);
473
474         DPRINTF(("IBCS2: start vm_dsize = 0x%x, vm_daddr = 0x%x end = 0x%x\n",
475                 ctob(vmspace->vm_dsize), vmspace->vm_daddr,
476                 ctob(vmspace->vm_dsize) + vmspace->vm_daddr ));
477         DPRINTF(("%s(%d):  returning successfully!\n", __FILE__, __LINE__));
478
479         /* Indicate that this file should not be modified */
480         imgp->vp->v_flag |= VTEXT;
481         return 0;
482 }
483
484 /*
485  * Tell kern_execve.c about it, with a little help from the linker.
486  */
487 static struct execsw coff_execsw = { exec_coff_imgact, "coff" };
488 EXEC_SET(coff, coff_execsw);