kernel - Fix panic when usched is used to force a cpu w/the dfly scheduler
[dragonfly.git] / sys / kern / imgact_elf.c
1 /*-
2  * Copyright (c) 2000 David O'Brien
3  * Copyright (c) 1995-1996 Søren Schmidt
4  * Copyright (c) 1996 Peter Wemm
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer
12  *    in this position and unchanged.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * $FreeBSD: src/sys/kern/imgact_elf.c,v 1.73.2.13 2002/12/28 19:49:41 dillon Exp $
31  */
32
33 #include <sys/param.h>
34 #include <sys/exec.h>
35 #include <sys/fcntl.h>
36 #include <sys/file.h>
37 #include <sys/imgact.h>
38 #include <sys/imgact_elf.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/mman.h>
42 #include <sys/systm.h>
43 #include <sys/proc.h>
44 #include <sys/nlookup.h>
45 #include <sys/pioctl.h>
46 #include <sys/procfs.h>
47 #include <sys/resourcevar.h>
48 #include <sys/signalvar.h>
49 #include <sys/stat.h>
50 #include <sys/syscall.h>
51 #include <sys/sysctl.h>
52 #include <sys/sysent.h>
53 #include <sys/vnode.h>
54 #include <sys/eventhandler.h>
55
56 #include <cpu/lwbuf.h>
57
58 #include <vm/vm.h>
59 #include <vm/vm_kern.h>
60 #include <vm/vm_param.h>
61 #include <vm/pmap.h>
62 #include <sys/lock.h>
63 #include <vm/vm_map.h>
64 #include <vm/vm_object.h>
65 #include <vm/vm_extern.h>
66
67 #include <machine/elf.h>
68 #include <machine/md_var.h>
69 #include <sys/mount.h>
70 #include <sys/ckpt.h>
71
72 #define OLD_EI_BRAND    8
73 #define truncps(va,ps)  ((va) & ~(ps - 1))
74 #define aligned(a,t)    (truncps((u_long)(a), sizeof(t)) == (u_long)(a))
75
76 static int __elfN(check_header)(const Elf_Ehdr *hdr);
77 static Elf_Brandinfo *__elfN(get_brandinfo)(struct image_params *imgp,
78     const char *interp, int32_t *osrel);
79 static int __elfN(load_file)(struct proc *p, const char *file, u_long *addr,
80     u_long *entry);
81 static int __elfN(load_section)(struct proc *p,
82     struct vmspace *vmspace, struct vnode *vp,
83     vm_offset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz,
84     vm_prot_t prot);
85 static int __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp);
86 static boolean_t __elfN(bsd_trans_osrel)(const Elf_Note *note,
87     int32_t *osrel);
88 static boolean_t __elfN(check_note)(struct image_params *imgp,
89     Elf_Brandnote *checknote, int32_t *osrel);
90 static vm_prot_t __elfN(trans_prot)(Elf_Word);
91 static Elf_Word __elfN(untrans_prot)(vm_prot_t);
92 static boolean_t check_PT_NOTE(struct image_params *imgp,
93     Elf_Brandnote *checknote, int32_t *osrel, const Elf_Phdr * pnote);
94 static boolean_t extract_interpreter(struct image_params *imgp,
95     const Elf_Phdr *pinterpreter, char *data);
96
97 static int elf_legacy_coredump = 0;
98 static int __elfN(fallback_brand) = -1;
99 #if defined(__x86_64__)
100 SYSCTL_NODE(_kern, OID_AUTO, elf64, CTLFLAG_RW, 0, "");
101 SYSCTL_INT(_debug, OID_AUTO, elf64_legacy_coredump, CTLFLAG_RW,
102     &elf_legacy_coredump, 0, "legacy coredump mode");
103 SYSCTL_INT(_kern_elf64, OID_AUTO, fallback_brand, CTLFLAG_RW,
104     &elf64_fallback_brand, 0, "ELF64 brand of last resort");
105 TUNABLE_INT("kern.elf64.fallback_brand", &elf64_fallback_brand);
106 #else /* i386 assumed */
107 SYSCTL_NODE(_kern, OID_AUTO, elf32, CTLFLAG_RW, 0, "");
108 SYSCTL_INT(_debug, OID_AUTO, elf32_legacy_coredump, CTLFLAG_RW,
109     &elf_legacy_coredump, 0, "legacy coredump mode");
110 SYSCTL_INT(_kern_elf32, OID_AUTO, fallback_brand, CTLFLAG_RW,
111     &elf32_fallback_brand, 0, "ELF32 brand of last resort");
112 TUNABLE_INT("kern.elf32.fallback_brand", &elf32_fallback_brand);
113 #endif
114
115 static Elf_Brandinfo *elf_brand_list[MAX_BRANDS];
116
117 static const char DRAGONFLY_ABI_VENDOR[] = "DragonFly";
118 static const char FREEBSD_ABI_VENDOR[]   = "FreeBSD";
119
120 Elf_Brandnote __elfN(dragonfly_brandnote) = {
121         .hdr.n_namesz   = sizeof(DRAGONFLY_ABI_VENDOR),
122         .hdr.n_descsz   = sizeof(int32_t),
123         .hdr.n_type     = 1,
124         .vendor         = DRAGONFLY_ABI_VENDOR,
125         .flags          = BN_TRANSLATE_OSREL,
126         .trans_osrel    = __elfN(bsd_trans_osrel),
127 };
128
129 Elf_Brandnote __elfN(freebsd_brandnote) = {
130         .hdr.n_namesz   = sizeof(FREEBSD_ABI_VENDOR),
131         .hdr.n_descsz   = sizeof(int32_t),
132         .hdr.n_type     = 1,
133         .vendor         = FREEBSD_ABI_VENDOR,
134         .flags          = BN_TRANSLATE_OSREL,
135         .trans_osrel    = __elfN(bsd_trans_osrel),
136 };
137
138 int
139 __elfN(insert_brand_entry)(Elf_Brandinfo *entry)
140 {
141         int i;
142
143         for (i = 0; i < MAX_BRANDS; i++) {
144                 if (elf_brand_list[i] == NULL) {
145                         elf_brand_list[i] = entry;
146                         break;
147                 }
148         }
149         if (i == MAX_BRANDS) {
150                 uprintf("WARNING: %s: could not insert brandinfo entry: %p\n",
151                         __func__, entry);
152                 return (-1);
153         }
154         return (0);
155 }
156
157 int
158 __elfN(remove_brand_entry)(Elf_Brandinfo *entry)
159 {
160         int i;
161
162         for (i = 0; i < MAX_BRANDS; i++) {
163                 if (elf_brand_list[i] == entry) {
164                         elf_brand_list[i] = NULL;
165                         break;
166                 }
167         }
168         if (i == MAX_BRANDS)
169                 return (-1);
170         return (0);
171 }
172
173 /*
174  * Check if an elf brand is being used anywhere in the system.
175  *
176  * Used by the linux emulation module unloader.  This isn't safe from
177  * races.
178  */
179 struct elf_brand_inuse_info {
180         int rval;
181         Elf_Brandinfo *entry;
182 };
183
184 static int elf_brand_inuse_callback(struct proc *p, void *data);
185
186 int
187 __elfN(brand_inuse)(Elf_Brandinfo *entry)
188 {
189         struct elf_brand_inuse_info info;
190
191         info.rval = FALSE;
192         info.entry = entry;
193         allproc_scan(elf_brand_inuse_callback, &info);
194         return (info.rval);
195 }
196
197 static
198 int
199 elf_brand_inuse_callback(struct proc *p, void *data)
200 {
201         struct elf_brand_inuse_info *info = data;
202
203         if (p->p_sysent == info->entry->sysvec) {
204                 info->rval = TRUE;
205                 return (-1);
206         }
207         return (0);
208 }
209
210 static int
211 __elfN(check_header)(const Elf_Ehdr *hdr)
212 {
213         Elf_Brandinfo *bi;
214         int i;
215
216         if (!IS_ELF(*hdr) ||
217             hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
218             hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
219             hdr->e_ident[EI_VERSION] != EV_CURRENT ||
220             hdr->e_phentsize != sizeof(Elf_Phdr) ||
221             hdr->e_ehsize != sizeof(Elf_Ehdr) ||
222             hdr->e_version != ELF_TARG_VER)
223                 return (ENOEXEC);
224
225         /*
226          * Make sure we have at least one brand for this machine.
227          */
228
229         for (i = 0; i < MAX_BRANDS; i++) {
230                 bi = elf_brand_list[i];
231                 if (bi != NULL && bi->machine == hdr->e_machine)
232                         break;
233         }
234         if (i == MAX_BRANDS)
235                 return (ENOEXEC);
236
237         return (0);
238 }
239
240 static int
241 __elfN(load_section)(struct proc *p, struct vmspace *vmspace, struct vnode *vp,
242                  vm_offset_t offset, caddr_t vmaddr, size_t memsz,
243                  size_t filsz, vm_prot_t prot)
244 {
245         size_t map_len;
246         vm_offset_t map_addr;
247         int error, rv, cow;
248         int count;
249         int shared;
250         size_t copy_len;
251         vm_object_t object;
252         vm_offset_t file_addr;
253
254         object = vp->v_object;
255         error = 0;
256
257         /*
258          * In most cases we will be able to use a shared lock on the
259          * object we are inserting into the map.  The lock will be
260          * upgraded in situations where new VM pages must be allocated.
261          */
262         shared = vm_object_hold_maybe_shared(object);
263
264         /*
265          * It's necessary to fail if the filsz + offset taken from the
266          * header is greater than the actual file pager object's size.
267          * If we were to allow this, then the vm_map_find() below would
268          * walk right off the end of the file object and into the ether.
269          *
270          * While I'm here, might as well check for something else that
271          * is invalid: filsz cannot be greater than memsz.
272          */
273         if ((off_t)filsz + offset > vp->v_filesize || filsz > memsz) {
274                 uprintf("elf_load_section: truncated ELF file\n");
275                 vm_object_drop(object);
276                 return (ENOEXEC);
277         }
278
279         map_addr = trunc_page((vm_offset_t)vmaddr);
280         file_addr = trunc_page(offset);
281
282         /*
283          * We have two choices.  We can either clear the data in the last page
284          * of an oversized mapping, or we can start the anon mapping a page
285          * early and copy the initialized data into that first page.  We
286          * choose the second..
287          */
288         if (memsz > filsz)
289                 map_len = trunc_page(offset+filsz) - file_addr;
290         else
291                 map_len = round_page(offset+filsz) - file_addr;
292
293         if (map_len != 0) {
294                 vm_object_reference_locked(object);
295
296                 /* cow flags: don't dump readonly sections in core */
297                 cow = MAP_COPY_ON_WRITE | MAP_PREFAULT;
298                 if ((prot & VM_PROT_WRITE) == 0)
299                         cow |= MAP_DISABLE_COREDUMP;
300                 if (shared == 0)
301                         cow |= MAP_PREFAULT_RELOCK;
302
303                 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
304                 vm_map_lock(&vmspace->vm_map);
305                 rv = vm_map_insert(&vmspace->vm_map, &count,
306                                       object,
307                                       file_addr,        /* file offset */
308                                       map_addr,         /* virtual start */
309                                       map_addr + map_len,/* virtual end */
310                                       VM_MAPTYPE_NORMAL,
311                                       prot, VM_PROT_ALL,
312                                       cow);
313                 vm_map_unlock(&vmspace->vm_map);
314                 vm_map_entry_release(count);
315
316                 /*
317                  * NOTE: Object must have a hold ref when calling
318                  * vm_object_deallocate().
319                  */
320                 if (rv != KERN_SUCCESS) {
321                         vm_object_drop(object);
322                         vm_object_deallocate(object);
323                         return (EINVAL);
324                 }
325
326                 /* we can stop now if we've covered it all */
327                 if (memsz == filsz) {
328                         vm_object_drop(object);
329                         return (0);
330                 }
331         }
332
333         /*
334          * We have to get the remaining bit of the file into the first part
335          * of the oversized map segment.  This is normally because the .data
336          * segment in the file is extended to provide bss.  It's a neat idea
337          * to try and save a page, but it's a pain in the behind to implement.
338          */
339         copy_len = (offset + filsz) - trunc_page(offset + filsz);
340         map_addr = trunc_page((vm_offset_t)vmaddr + filsz);
341         map_len = round_page((vm_offset_t)vmaddr + memsz) - map_addr;
342
343         /* This had damn well better be true! */
344         if (map_len != 0) {
345                 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
346                 vm_map_lock(&vmspace->vm_map);
347                 rv = vm_map_insert(&vmspace->vm_map, &count,
348                                         NULL, 0,
349                                         map_addr, map_addr + map_len,
350                                         VM_MAPTYPE_NORMAL,
351                                         VM_PROT_ALL, VM_PROT_ALL,
352                                         0);
353                 vm_map_unlock(&vmspace->vm_map);
354                 vm_map_entry_release(count);
355                 if (rv != KERN_SUCCESS) {
356                         vm_object_drop(object);
357                         return (EINVAL);
358                 }
359         }
360
361         if (copy_len != 0) {
362                 struct lwbuf *lwb;
363                 struct lwbuf lwb_cache;
364                 vm_page_t m;
365
366                 m = vm_fault_object_page(object, trunc_page(offset + filsz),
367                                          VM_PROT_READ, 0, shared, &error);
368                 if (m) {
369                         lwb = lwbuf_alloc(m, &lwb_cache);
370                         error = copyout((caddr_t)lwbuf_kva(lwb),
371                                         (caddr_t)map_addr, copy_len);
372                         lwbuf_free(lwb);
373                         vm_page_unhold(m);
374                 }
375                 if (error) {
376                         vm_object_drop(object);
377                         return (error);
378                 }
379         }
380
381         vm_object_drop(object);
382
383         /*
384          * set it to the specified protection
385          */
386         vm_map_protect(&vmspace->vm_map, map_addr, map_addr + map_len,
387                        prot, FALSE);
388
389         return (error);
390 }
391
392 /*
393  * Load the file "file" into memory.  It may be either a shared object
394  * or an executable.
395  *
396  * The "addr" reference parameter is in/out.  On entry, it specifies
397  * the address where a shared object should be loaded.  If the file is
398  * an executable, this value is ignored.  On exit, "addr" specifies
399  * where the file was actually loaded.
400  *
401  * The "entry" reference parameter is out only.  On exit, it specifies
402  * the entry point for the loaded file.
403  */
404 static int
405 __elfN(load_file)(struct proc *p, const char *file, u_long *addr, u_long *entry)
406 {
407         struct {
408                 struct nlookupdata nd;
409                 struct vattr attr;
410                 struct image_params image_params;
411         } *tempdata;
412         const Elf_Ehdr *hdr = NULL;
413         const Elf_Phdr *phdr = NULL;
414         struct nlookupdata *nd;
415         struct vmspace *vmspace = p->p_vmspace;
416         struct vattr *attr;
417         struct image_params *imgp;
418         struct mount *topmnt;
419         vm_prot_t prot;
420         u_long rbase;
421         u_long base_addr = 0;
422         int error, i, numsegs;
423
424         tempdata = kmalloc(sizeof(*tempdata), M_TEMP, M_WAITOK);
425         nd = &tempdata->nd;
426         attr = &tempdata->attr;
427         imgp = &tempdata->image_params;
428
429         /*
430          * Initialize part of the common data
431          */
432         imgp->proc = p;
433         imgp->attr = attr;
434         imgp->firstpage = NULL;
435         imgp->image_header = NULL;
436         imgp->vp = NULL;
437
438         error = nlookup_init(nd, file, UIO_SYSSPACE, NLC_FOLLOW);
439         if (error == 0)
440                 error = nlookup(nd);
441         if (error == 0)
442                 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &imgp->vp);
443         topmnt = nd->nl_nch.mount;
444         nlookup_done(nd);
445         if (error)
446                 goto fail;
447
448         /*
449          * Check permissions, modes, uid, etc on the file, and "open" it.
450          */
451         error = exec_check_permissions(imgp, topmnt);
452         if (error) {
453                 vn_unlock(imgp->vp);
454                 goto fail;
455         }
456
457         error = exec_map_first_page(imgp);
458         /*
459          * Also make certain that the interpreter stays the same, so set
460          * its VTEXT flag, too.
461          */
462         if (error == 0)
463                 vsetflags(imgp->vp, VTEXT);
464         vn_unlock(imgp->vp);
465         if (error)
466                 goto fail;
467
468         hdr = (const Elf_Ehdr *)imgp->image_header;
469         if ((error = __elfN(check_header)(hdr)) != 0)
470                 goto fail;
471         if (hdr->e_type == ET_DYN)
472                 rbase = *addr;
473         else if (hdr->e_type == ET_EXEC)
474                 rbase = 0;
475         else {
476                 error = ENOEXEC;
477                 goto fail;
478         }
479
480         /* Only support headers that fit within first page for now      */
481         /*    (multiplication of two Elf_Half fields will not overflow) */
482         if ((hdr->e_phoff > PAGE_SIZE) ||
483             (hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE - hdr->e_phoff) {
484                 error = ENOEXEC;
485                 goto fail;
486         }
487
488         phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
489         if (!aligned(phdr, Elf_Addr)) {
490                 error = ENOEXEC;
491                 goto fail;
492         }
493
494         for (i = 0, numsegs = 0; i < hdr->e_phnum; i++) {
495                 if (phdr[i].p_type == PT_LOAD && phdr[i].p_memsz != 0) {
496                         /* Loadable segment */
497                         prot = __elfN(trans_prot)(phdr[i].p_flags);
498                         error = __elfN(load_section)(
499                                     p, vmspace, imgp->vp,
500                                     phdr[i].p_offset,
501                                     (caddr_t)phdr[i].p_vaddr +
502                                     rbase,
503                                     phdr[i].p_memsz,
504                                     phdr[i].p_filesz, prot);
505                         if (error != 0)
506                                 goto fail;
507                         /*
508                          * Establish the base address if this is the
509                          * first segment.
510                          */
511                         if (numsegs == 0)
512                                 base_addr = trunc_page(phdr[i].p_vaddr + rbase);
513                         numsegs++;
514                 }
515         }
516         *addr = base_addr;
517         *entry = (unsigned long)hdr->e_entry + rbase;
518
519 fail:
520         if (imgp->firstpage)
521                 exec_unmap_first_page(imgp);
522         if (imgp->vp) {
523                 vrele(imgp->vp);
524                 imgp->vp = NULL;
525         }
526         kfree(tempdata, M_TEMP);
527
528         return (error);
529 }
530
531 static Elf_Brandinfo *
532 __elfN(get_brandinfo)(struct image_params *imgp, const char *interp,
533     int32_t *osrel)
534 {
535         const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header;
536         Elf_Brandinfo *bi;
537         boolean_t ret;
538         int i;
539
540         /* We support four types of branding -- (1) the ELF EI_OSABI field
541          * that SCO added to the ELF spec, (2) FreeBSD 3.x's traditional string
542          * branding within the ELF header, (3) path of the `interp_path' field,
543          * and (4) the ".note.ABI-tag" ELF section.
544          */
545
546         /* Look for an ".note.ABI-tag" ELF section */
547         for (i = 0; i < MAX_BRANDS; i++) {
548                 bi = elf_brand_list[i];
549
550                 if (bi == NULL)
551                         continue;
552                 if (hdr->e_machine == bi->machine && (bi->flags &
553                     (BI_BRAND_NOTE|BI_BRAND_NOTE_MANDATORY)) != 0) {
554                         ret = __elfN(check_note)(imgp, bi->brand_note, osrel);
555                         if (ret)
556                                 return (bi);
557                 }
558         }
559
560         /* If the executable has a brand, search for it in the brand list. */
561         for (i = 0;  i < MAX_BRANDS;  i++) {
562                 bi = elf_brand_list[i];
563
564                 if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
565                         continue;
566                 if (hdr->e_machine == bi->machine &&
567                     (hdr->e_ident[EI_OSABI] == bi->brand ||
568                     strncmp((const char *)&hdr->e_ident[OLD_EI_BRAND],
569                     bi->compat_3_brand, strlen(bi->compat_3_brand)) == 0))
570                         return (bi);
571         }
572
573         /* Lacking a known brand, search for a recognized interpreter. */
574         if (interp != NULL) {
575                 for (i = 0;  i < MAX_BRANDS;  i++) {
576                         bi = elf_brand_list[i];
577
578                         if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
579                                 continue;
580                         if (hdr->e_machine == bi->machine &&
581                             strcmp(interp, bi->interp_path) == 0)
582                                 return (bi);
583                 }
584         }
585
586         /* Lacking a recognized interpreter, try the default brand */
587         for (i = 0; i < MAX_BRANDS; i++) {
588                 bi = elf_brand_list[i];
589
590                 if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
591                         continue;
592                 if (hdr->e_machine == bi->machine &&
593                     __elfN(fallback_brand) == bi->brand)
594                         return (bi);
595         }
596         return (NULL);
597 }
598
599 static int
600 __CONCAT(exec_,__elfN(imgact))(struct image_params *imgp)
601 {
602         const Elf_Ehdr *hdr = (const Elf_Ehdr *) imgp->image_header;
603         const Elf_Phdr *phdr;
604         Elf_Auxargs *elf_auxargs;
605         struct vmspace *vmspace;
606         vm_prot_t prot;
607         u_long text_size = 0, data_size = 0, total_size = 0;
608         u_long text_addr = 0, data_addr = 0;
609         u_long seg_size, seg_addr;
610         u_long addr, baddr, et_dyn_addr, entry = 0, proghdr = 0;
611         int32_t osrel = 0;
612         int error = 0, i, n;
613         boolean_t failure;
614         char *interp = NULL;
615         const char *newinterp = NULL;
616         Elf_Brandinfo *brand_info;
617         char *path;
618
619         /*
620          * Do we have a valid ELF header ?
621          *
622          * Only allow ET_EXEC & ET_DYN here, reject ET_DYN later if a particular
623          * brand doesn't support it.  Both DragonFly platforms do by default.
624          */
625         if (__elfN(check_header)(hdr) != 0 ||
626             (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN))
627                 return (-1);
628
629         /*
630          * From here on down, we return an errno, not -1, as we've
631          * detected an ELF file.
632          */
633
634         if ((hdr->e_phoff > PAGE_SIZE) ||
635             (hdr->e_phoff + hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE) {
636                 /* Only support headers in first page for now */
637                 return (ENOEXEC);
638         }
639         phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
640         if (!aligned(phdr, Elf_Addr))
641                 return (ENOEXEC);
642         n = 0;
643         baddr = 0;
644         for (i = 0; i < hdr->e_phnum; i++) {
645                 if (phdr[i].p_type == PT_LOAD) {
646                         if (n == 0)
647                                 baddr = phdr[i].p_vaddr;
648                         n++;
649                         continue;
650                 }
651                 if (phdr[i].p_type == PT_INTERP) {
652                         /*
653                          * If interp is already defined there are more than
654                          * one PT_INTERP program headers present.  Take only
655                          * the first one and ignore the rest.
656                          */
657                         if (interp != NULL)
658                                 continue;
659
660                         if (phdr[i].p_filesz == 0 ||
661                             phdr[i].p_filesz > PAGE_SIZE ||
662                             phdr[i].p_filesz > MAXPATHLEN)
663                                 return (ENOEXEC);
664
665                         interp = kmalloc(phdr[i].p_filesz, M_TEMP, M_WAITOK);
666                         failure = extract_interpreter(imgp, &phdr[i], interp);
667                         if (failure) {
668                                 kfree(interp, M_TEMP);
669                                 return (ENOEXEC);
670                         }
671                         continue;
672                 }
673         }
674         
675         brand_info = __elfN(get_brandinfo)(imgp, interp, &osrel);
676         if (brand_info == NULL) {
677                 uprintf("ELF binary type \"%u\" not known.\n",
678                     hdr->e_ident[EI_OSABI]);
679                 if (interp != NULL)
680                         kfree(interp, M_TEMP);
681                 return (ENOEXEC);
682         }
683         if (hdr->e_type == ET_DYN) {
684                 if ((brand_info->flags & BI_CAN_EXEC_DYN) == 0) {
685                         if (interp != NULL)
686                                 kfree(interp, M_TEMP);
687                         return (ENOEXEC);
688                 }
689                 /*
690                  * Honour the base load address from the dso if it is
691                  * non-zero for some reason.
692                  */
693                 if (baddr == 0)
694                         et_dyn_addr = ET_DYN_LOAD_ADDR;
695                 else
696                         et_dyn_addr = 0;
697         } else
698                 et_dyn_addr = 0;
699
700         if (interp != NULL && brand_info->interp_newpath != NULL)
701                 newinterp = brand_info->interp_newpath;
702
703         exec_new_vmspace(imgp, NULL);
704
705         /*
706          * Yeah, I'm paranoid.  There is every reason in the world to get
707          * VTEXT now since from here on out, there are places we can have
708          * a context switch.  Better safe than sorry; I really don't want
709          * the file to change while it's being loaded.
710          */
711         vsetflags(imgp->vp, VTEXT);
712
713         vmspace = imgp->proc->p_vmspace;
714
715         for (i = 0; i < hdr->e_phnum; i++) {
716                 switch (phdr[i].p_type) {
717                 case PT_LOAD:   /* Loadable segment */
718                         if (phdr[i].p_memsz == 0)
719                                 break;
720                         prot = __elfN(trans_prot)(phdr[i].p_flags);
721
722                         if ((error = __elfN(load_section)(
723                                         imgp->proc,
724                                         vmspace,
725                                         imgp->vp,
726                                         phdr[i].p_offset,
727                                         (caddr_t)phdr[i].p_vaddr + et_dyn_addr,
728                                         phdr[i].p_memsz,
729                                         phdr[i].p_filesz,
730                                         prot)) != 0) {
731                                 if (interp != NULL)
732                                         kfree (interp, M_TEMP);
733                                 return (error);
734                         }
735
736                         /*
737                          * If this segment contains the program headers,
738                          * remember their virtual address for the AT_PHDR
739                          * aux entry. Static binaries don't usually include
740                          * a PT_PHDR entry.
741                          */
742                         if (phdr[i].p_offset == 0 &&
743                             hdr->e_phoff + hdr->e_phnum * hdr->e_phentsize
744                                 <= phdr[i].p_filesz)
745                                 proghdr = phdr[i].p_vaddr + hdr->e_phoff +
746                                     et_dyn_addr;
747
748                         seg_addr = trunc_page(phdr[i].p_vaddr + et_dyn_addr);
749                         seg_size = round_page(phdr[i].p_memsz +
750                             phdr[i].p_vaddr + et_dyn_addr - seg_addr);
751
752                         /*
753                          * Is this .text or .data?  We can't use
754                          * VM_PROT_WRITE or VM_PROT_EXEC, it breaks the
755                          * alpha terribly and possibly does other bad
756                          * things so we stick to the old way of figuring
757                          * it out:  If the segment contains the program
758                          * entry point, it's a text segment, otherwise it
759                          * is a data segment.
760                          *
761                          * Note that obreak() assumes that data_addr + 
762                          * data_size == end of data load area, and the ELF
763                          * file format expects segments to be sorted by
764                          * address.  If multiple data segments exist, the
765                          * last one will be used.
766                          */
767                         if (hdr->e_entry >= phdr[i].p_vaddr &&
768                             hdr->e_entry < (phdr[i].p_vaddr +
769                             phdr[i].p_memsz)) {
770                                 text_size = seg_size;
771                                 text_addr = seg_addr;
772                                 entry = (u_long)hdr->e_entry + et_dyn_addr;
773                         } else {
774                                 data_size = seg_size;
775                                 data_addr = seg_addr;
776                         }
777                         total_size += seg_size;
778
779                         /*
780                          * Check limits.  It should be safe to check the
781                          * limits after loading the segment since we do
782                          * not actually fault in all the segment's pages.
783                          */
784                         if (data_size >
785                             imgp->proc->p_rlimit[RLIMIT_DATA].rlim_cur ||
786                             text_size > maxtsiz ||
787                             total_size >
788                             imgp->proc->p_rlimit[RLIMIT_VMEM].rlim_cur) {
789                                 if (interp != NULL)
790                                         kfree(interp, M_TEMP);
791                                 error = ENOMEM;
792                                 return (error);
793                         }
794                         break;
795                 case PT_PHDR:   /* Program header table info */
796                         proghdr = phdr[i].p_vaddr + et_dyn_addr;
797                         break;
798                 default:
799                         break;
800                 }
801         }
802
803         vmspace->vm_tsize = text_size >> PAGE_SHIFT;
804         vmspace->vm_taddr = (caddr_t)(uintptr_t)text_addr;
805         vmspace->vm_dsize = data_size >> PAGE_SHIFT;
806         vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr;
807
808         addr = ELF_RTLD_ADDR(vmspace);
809
810         imgp->entry_addr = entry;
811
812         imgp->proc->p_sysent = brand_info->sysvec;
813         EVENTHANDLER_INVOKE(process_exec, imgp);
814
815         if (interp != NULL) {
816                 int have_interp = FALSE;
817                 if (brand_info->emul_path != NULL &&
818                     brand_info->emul_path[0] != '\0') {
819                         path = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK);
820                         ksnprintf(path, MAXPATHLEN, "%s%s",
821                             brand_info->emul_path, interp);
822                         error = __elfN(load_file)(imgp->proc, path, &addr,
823                             &imgp->entry_addr);
824                         kfree(path, M_TEMP);
825                         if (error == 0)
826                                 have_interp = TRUE;
827                 }
828                 if (!have_interp && newinterp != NULL) {
829                         error = __elfN(load_file)(imgp->proc, newinterp,
830                             &addr, &imgp->entry_addr);
831                         if (error == 0)
832                                 have_interp = TRUE;
833                 }
834                 if (!have_interp) {
835                         error = __elfN(load_file)(imgp->proc, interp, &addr,
836                             &imgp->entry_addr);
837                 }
838                 if (error != 0) {
839                         uprintf("ELF interpreter %s not found\n", interp);
840                         kfree(interp, M_TEMP);
841                         return (error);
842                 }
843                 kfree(interp, M_TEMP);
844         } else
845                 addr = et_dyn_addr;
846
847         /*
848          * Construct auxargs table (used by the fixup routine)
849          */
850         elf_auxargs = kmalloc(sizeof(Elf_Auxargs), M_TEMP, M_WAITOK);
851         elf_auxargs->execfd = -1;
852         elf_auxargs->phdr = proghdr;
853         elf_auxargs->phent = hdr->e_phentsize;
854         elf_auxargs->phnum = hdr->e_phnum;
855         elf_auxargs->pagesz = PAGE_SIZE;
856         elf_auxargs->base = addr;
857         elf_auxargs->flags = 0;
858         elf_auxargs->entry = entry;
859
860         imgp->auxargs = elf_auxargs;
861         imgp->interpreted = 0;
862         imgp->proc->p_osrel = osrel;
863
864         return (error);
865 }
866
867 int
868 __elfN(dragonfly_fixup)(register_t **stack_base, struct image_params *imgp)
869 {
870         Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs;
871         Elf_Addr *base;
872         Elf_Addr *pos;
873
874         base = (Elf_Addr *)*stack_base;
875         pos = base + (imgp->args->argc + imgp->args->envc + 2);
876
877         if (args->execfd != -1)
878                 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
879         AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
880         AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
881         AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
882         AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
883         AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
884         AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
885         AUXARGS_ENTRY(pos, AT_BASE, args->base);
886         if (imgp->execpathp != 0)
887                 AUXARGS_ENTRY(pos, AT_EXECPATH, imgp->execpathp);
888         AUXARGS_ENTRY(pos, AT_OSRELDATE, osreldate);
889         AUXARGS_ENTRY(pos, AT_NULL, 0);
890
891         kfree(imgp->auxargs, M_TEMP);
892         imgp->auxargs = NULL;
893
894         base--;
895         suword(base, (long)imgp->args->argc);
896         *stack_base = (register_t *)base;
897         return (0);
898 }
899
900 /*
901  * Code for generating ELF core dumps.
902  */
903
904 typedef int (*segment_callback)(vm_map_entry_t, void *);
905
906 /* Closure for cb_put_phdr(). */
907 struct phdr_closure {
908         Elf_Phdr *phdr;         /* Program header to fill in (incremented) */
909         Elf_Phdr *phdr_max;     /* Pointer bound for error check */
910         Elf_Off offset;         /* Offset of segment in core file */
911 };
912
913 /* Closure for cb_size_segment(). */
914 struct sseg_closure {
915         int count;              /* Count of writable segments. */
916         size_t vsize;           /* Total size of all writable segments. */
917 };
918
919 /* Closure for cb_put_fp(). */
920 struct fp_closure {
921         struct vn_hdr *vnh;
922         struct vn_hdr *vnh_max;
923         int count;
924         struct stat *sb;
925 };
926
927 typedef struct elf_buf {
928         char    *buf;
929         size_t  off;
930         size_t  off_max;
931 } *elf_buf_t;
932
933 static void *target_reserve(elf_buf_t target, size_t bytes, int *error);
934
935 static int cb_put_phdr (vm_map_entry_t, void *);
936 static int cb_size_segment (vm_map_entry_t, void *);
937 static int cb_fpcount_segment(vm_map_entry_t, void *);
938 static int cb_put_fp(vm_map_entry_t, void *);
939
940
941 static int each_segment (struct proc *, segment_callback, void *, int);
942 static int __elfN(corehdr)(struct lwp *, int, struct file *, struct ucred *,
943                         int, elf_buf_t);
944 enum putmode { WRITE, DRYRUN };
945 static int __elfN(puthdr)(struct lwp *, elf_buf_t, int sig, enum putmode,
946                         int, struct file *);
947 static int elf_putallnotes(struct lwp *, elf_buf_t, int, enum putmode);
948 static int __elfN(putnote)(elf_buf_t, const char *, int, const void *, size_t);
949
950 static int elf_putsigs(struct lwp *, elf_buf_t);
951 static int elf_puttextvp(struct proc *, elf_buf_t);
952 static int elf_putfiles(struct proc *, elf_buf_t, struct file *);
953
954 int
955 __elfN(coredump)(struct lwp *lp, int sig, struct vnode *vp, off_t limit)
956 {
957         struct file *fp; 
958         int error;
959
960         if ((error = falloc(NULL, &fp, NULL)) != 0)
961                 return (error);
962         fsetcred(fp, lp->lwp_proc->p_ucred);
963
964         /*
965          * XXX fixme.
966          */
967         fp->f_type = DTYPE_VNODE;
968         fp->f_flag = O_CREAT|O_WRONLY|O_NOFOLLOW;
969         fp->f_ops = &vnode_fileops;
970         fp->f_data = vp;
971         
972         error = generic_elf_coredump(lp, sig, fp, limit);
973
974         fp->f_type = 0;
975         fp->f_flag = 0;
976         fp->f_ops = &badfileops;
977         fp->f_data = NULL;
978         fdrop(fp);
979         return (error);
980 }
981
982 int
983 generic_elf_coredump(struct lwp *lp, int sig, struct file *fp, off_t limit)
984 {
985         struct proc *p = lp->lwp_proc;
986         struct ucred *cred = p->p_ucred;
987         int error = 0;
988         struct sseg_closure seginfo;
989         struct elf_buf target;
990
991         if (!fp)
992                 kprintf("can't dump core - null fp\n");
993
994         /*
995          * Size the program segments
996          */
997         seginfo.count = 0;
998         seginfo.vsize = 0;
999         each_segment(p, cb_size_segment, &seginfo, 1);
1000
1001         /*
1002          * Calculate the size of the core file header area by making
1003          * a dry run of generating it.  Nothing is written, but the
1004          * size is calculated.
1005          */
1006         bzero(&target, sizeof(target));
1007         __elfN(puthdr)(lp, &target, sig, DRYRUN, seginfo.count, fp);
1008
1009         if (target.off + seginfo.vsize >= limit)
1010                 return (EFAULT);
1011
1012         /*
1013          * Allocate memory for building the header, fill it up,
1014          * and write it out.
1015          */
1016         target.off_max = target.off;
1017         target.off = 0;
1018         target.buf = kmalloc(target.off_max, M_TEMP, M_WAITOK|M_ZERO);
1019
1020         error = __elfN(corehdr)(lp, sig, fp, cred, seginfo.count, &target);
1021
1022         /* Write the contents of all of the writable segments. */
1023         if (error == 0) {
1024                 Elf_Phdr *php;
1025                 int i;
1026                 ssize_t nbytes;
1027
1028                 php = (Elf_Phdr *)(target.buf + sizeof(Elf_Ehdr)) + 1;
1029                 for (i = 0; i < seginfo.count; i++) {
1030                         error = fp_write(fp, (caddr_t)php->p_vaddr,
1031                                         php->p_filesz, &nbytes, UIO_USERSPACE);
1032                         if (error != 0)
1033                                 break;
1034                         php++;
1035                 }
1036         }
1037         kfree(target.buf, M_TEMP);
1038         
1039         return (error);
1040 }
1041
1042 /*
1043  * A callback for each_segment() to write out the segment's
1044  * program header entry.
1045  */
1046 static int
1047 cb_put_phdr(vm_map_entry_t entry, void *closure)
1048 {
1049         struct phdr_closure *phc = closure;
1050         Elf_Phdr *phdr = phc->phdr;
1051
1052         if (phc->phdr == phc->phdr_max)
1053                 return (EINVAL);
1054
1055         phc->offset = round_page(phc->offset);
1056
1057         phdr->p_type = PT_LOAD;
1058         phdr->p_offset = phc->offset;
1059         phdr->p_vaddr = entry->start;
1060         phdr->p_paddr = 0;
1061         phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
1062         phdr->p_align = PAGE_SIZE;
1063         phdr->p_flags = __elfN(untrans_prot)(entry->protection);
1064
1065         phc->offset += phdr->p_filesz;
1066         ++phc->phdr;
1067         return (0);
1068 }
1069
1070 /*
1071  * A callback for each_writable_segment() to gather information about
1072  * the number of segments and their total size.
1073  */
1074 static int
1075 cb_size_segment(vm_map_entry_t entry, void *closure)
1076 {
1077         struct sseg_closure *ssc = closure;
1078
1079         ++ssc->count;
1080         ssc->vsize += entry->end - entry->start;
1081         return (0);
1082 }
1083
1084 /*
1085  * A callback for each_segment() to gather information about
1086  * the number of text segments.
1087  */
1088 static int
1089 cb_fpcount_segment(vm_map_entry_t entry, void *closure)
1090 {
1091         int *count = closure;
1092         struct vnode *vp;
1093
1094         if (entry->object.vm_object->type == OBJT_VNODE) {
1095                 vp = (struct vnode *)entry->object.vm_object->handle;
1096                 if ((vp->v_flag & VCKPT) && curproc->p_textvp == vp)
1097                         return (0);
1098                 ++*count;
1099         }
1100         return (0);
1101 }
1102
1103 static int
1104 cb_put_fp(vm_map_entry_t entry, void *closure) 
1105 {
1106         struct fp_closure *fpc = closure;
1107         struct vn_hdr *vnh = fpc->vnh;
1108         Elf_Phdr *phdr = &vnh->vnh_phdr;
1109         struct vnode *vp;
1110         int error;
1111
1112         /*
1113          * If an entry represents a vnode then write out a file handle.
1114          *
1115          * If we are checkpointing a checkpoint-restored program we do
1116          * NOT record the filehandle for the old checkpoint vnode (which
1117          * is mapped all over the place).  Instead we rely on the fact
1118          * that a checkpoint-restored program does not mmap() the checkpt
1119          * vnode NOCORE, so its contents will be written out to the
1120          * new checkpoint file.  This is necessary because the 'old'
1121          * checkpoint file is typically destroyed when a new one is created
1122          * and thus cannot be used to restore the new checkpoint.
1123          *
1124          * Theoretically we could create a chain of checkpoint files and
1125          * operate the checkpointing operation kinda like an incremental
1126          * checkpoint, but a checkpoint restore would then likely wind up
1127          * referencing many prior checkpoint files and that is a bit over
1128          * the top for the purpose of the checkpoint API.
1129          */
1130         if (entry->object.vm_object->type == OBJT_VNODE) {
1131                 vp = (struct vnode *)entry->object.vm_object->handle;
1132                 if ((vp->v_flag & VCKPT) && curproc->p_textvp == vp)
1133                         return (0);
1134                 if (vnh == fpc->vnh_max)
1135                         return (EINVAL);
1136
1137                 if (vp->v_mount)
1138                         vnh->vnh_fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
1139                 error = VFS_VPTOFH(vp, &vnh->vnh_fh.fh_fid);
1140                 if (error) {
1141                         char *freepath, *fullpath;
1142
1143                         if (vn_fullpath(curproc, vp, &fullpath, &freepath, 0)) {
1144                                 kprintf("Warning: coredump, error %d: cannot store file handle for vnode %p\n", error, vp);
1145                         } else {
1146                                 kprintf("Warning: coredump, error %d: cannot store file handle for %s\n", error, fullpath);
1147                                 kfree(freepath, M_TEMP);
1148                         }
1149                         error = 0;
1150                 }
1151
1152                 phdr->p_type = PT_LOAD;
1153                 phdr->p_offset = 0;        /* not written to core */
1154                 phdr->p_vaddr = entry->start;
1155                 phdr->p_paddr = 0;
1156                 phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
1157                 phdr->p_align = PAGE_SIZE;
1158                 phdr->p_flags = 0;
1159                 if (entry->protection & VM_PROT_READ)
1160                         phdr->p_flags |= PF_R;
1161                 if (entry->protection & VM_PROT_WRITE)
1162                         phdr->p_flags |= PF_W;
1163                 if (entry->protection & VM_PROT_EXECUTE)
1164                         phdr->p_flags |= PF_X;
1165                 ++fpc->vnh;
1166                 ++fpc->count;
1167         }
1168         return (0);
1169 }
1170
1171 /*
1172  * For each writable segment in the process's memory map, call the given
1173  * function with a pointer to the map entry and some arbitrary
1174  * caller-supplied data.
1175  */
1176 static int
1177 each_segment(struct proc *p, segment_callback func, void *closure, int writable)
1178 {
1179         int error = 0;
1180         vm_map_t map = &p->p_vmspace->vm_map;
1181         vm_map_entry_t entry;
1182
1183         for (entry = map->header.next; error == 0 && entry != &map->header;
1184             entry = entry->next) {
1185                 vm_object_t obj;
1186                 vm_object_t lobj;
1187                 vm_object_t tobj;
1188
1189                 /*
1190                  * Don't dump inaccessible mappings, deal with legacy
1191                  * coredump mode.
1192                  *
1193                  * Note that read-only segments related to the elf binary
1194                  * are marked MAP_ENTRY_NOCOREDUMP now so we no longer
1195                  * need to arbitrarily ignore such segments.
1196                  */
1197                 if (elf_legacy_coredump) {
1198                         if (writable && (entry->protection & VM_PROT_RW) != VM_PROT_RW)
1199                                 continue;
1200                 } else {
1201                         if (writable && (entry->protection & VM_PROT_ALL) == 0)
1202                                 continue;
1203                 }
1204
1205                 /*
1206                  * Dont include memory segment in the coredump if
1207                  * MAP_NOCORE is set in mmap(2) or MADV_NOCORE in
1208                  * madvise(2).
1209                  *
1210                  * Currently we only dump normal VM object maps.  We do
1211                  * not dump submaps or virtual page tables.
1212                  */
1213                 if (writable && (entry->eflags & MAP_ENTRY_NOCOREDUMP))
1214                         continue;
1215                 if (entry->maptype != VM_MAPTYPE_NORMAL)
1216                         continue;
1217                 if ((obj = entry->object.vm_object) == NULL)
1218                         continue;
1219
1220                 /*
1221                  * Find the bottom-most object, leaving the base object
1222                  * and the bottom-most object held (but only one hold
1223                  * if they happen to be the same).
1224                  */
1225                 vm_object_hold(obj);
1226
1227                 lobj = obj;
1228                 while (lobj && (tobj = lobj->backing_object) != NULL) {
1229                         KKASSERT(tobj != obj);
1230                         vm_object_hold(tobj);
1231                         if (tobj == lobj->backing_object) {
1232                                 if (lobj != obj) {
1233                                         vm_object_lock_swap();
1234                                         vm_object_drop(lobj);
1235                                 }
1236                                 lobj = tobj;
1237                         } else {
1238                                 vm_object_drop(tobj);
1239                         }
1240                 }
1241
1242                 /*
1243                  * The callback only applies to default, swap, or vnode
1244                  * objects.  Other types of objects such as memory-mapped
1245                  * devices are ignored.
1246                  */
1247                 if (lobj->type == OBJT_DEFAULT || lobj->type == OBJT_SWAP ||
1248                     lobj->type == OBJT_VNODE) {
1249                         error = (*func)(entry, closure);
1250                 }
1251                 if (lobj != obj)
1252                         vm_object_drop(lobj);
1253                 vm_object_drop(obj);
1254         }
1255         return (error);
1256 }
1257
1258 static
1259 void *
1260 target_reserve(elf_buf_t target, size_t bytes, int *error)
1261 {
1262     void *res = NULL;
1263
1264     if (target->buf) {
1265             if (target->off + bytes > target->off_max)
1266                     *error = EINVAL;
1267             else
1268                     res = target->buf + target->off;
1269     }
1270     target->off += bytes;
1271     return (res);
1272 }
1273
1274 /*
1275  * Write the core file header to the file, including padding up to
1276  * the page boundary.
1277  */
1278 static int
1279 __elfN(corehdr)(struct lwp *lp, int sig, struct file *fp, struct ucred *cred,
1280             int numsegs, elf_buf_t target)
1281 {
1282         int error;
1283         ssize_t nbytes;
1284
1285         /*
1286          * Fill in the header.  The fp is passed so we can detect and flag
1287          * a checkpoint file pointer within the core file itself, because
1288          * it may not be restored from the same file handle.
1289          */
1290         error = __elfN(puthdr)(lp, target, sig, WRITE, numsegs, fp);
1291
1292         /* Write it to the core file. */
1293         if (error == 0) {
1294                 error = fp_write(fp, target->buf, target->off, &nbytes,
1295                                  UIO_SYSSPACE);
1296         }
1297         return (error);
1298 }
1299
1300 static int
1301 __elfN(puthdr)(struct lwp *lp, elf_buf_t target, int sig, enum putmode mode,
1302     int numsegs, struct file *fp)
1303 {
1304         struct proc *p = lp->lwp_proc;
1305         int error = 0;
1306         size_t phoff;
1307         size_t noteoff;
1308         size_t notesz;
1309         Elf_Ehdr *ehdr;
1310         Elf_Phdr *phdr;
1311
1312         ehdr = target_reserve(target, sizeof(Elf_Ehdr), &error);
1313
1314         phoff = target->off;
1315         phdr = target_reserve(target, (numsegs + 1) * sizeof(Elf_Phdr), &error);
1316
1317         noteoff = target->off;
1318         if (error == 0)
1319                 elf_putallnotes(lp, target, sig, mode);
1320         notesz = target->off - noteoff;
1321
1322         /*
1323          * put extra cruft for dumping process state here 
1324          *  - we really want it be before all the program 
1325          *    mappings
1326          *  - we just need to update the offset accordingly
1327          *    and GDB will be none the wiser.
1328          */
1329         if (error == 0)
1330                 error = elf_puttextvp(p, target);
1331         if (error == 0)
1332                 error = elf_putsigs(lp, target);
1333         if (error == 0)
1334                 error = elf_putfiles(p, target, fp);
1335
1336         /*
1337          * Align up to a page boundary for the program segments.  The
1338          * actual data will be written to the outptu file, not to elf_buf_t,
1339          * so we do not have to do any further bounds checking.
1340          */
1341         target->off = round_page(target->off);
1342         if (error == 0 && ehdr != NULL) {
1343                 /*
1344                  * Fill in the ELF header.
1345                  */
1346                 ehdr->e_ident[EI_MAG0] = ELFMAG0;
1347                 ehdr->e_ident[EI_MAG1] = ELFMAG1;
1348                 ehdr->e_ident[EI_MAG2] = ELFMAG2;
1349                 ehdr->e_ident[EI_MAG3] = ELFMAG3;
1350                 ehdr->e_ident[EI_CLASS] = ELF_CLASS;
1351                 ehdr->e_ident[EI_DATA] = ELF_DATA;
1352                 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
1353                 ehdr->e_ident[EI_OSABI] = ELFOSABI_NONE;
1354                 ehdr->e_ident[EI_ABIVERSION] = 0;
1355                 ehdr->e_ident[EI_PAD] = 0;
1356                 ehdr->e_type = ET_CORE;
1357                 ehdr->e_machine = ELF_ARCH;
1358                 ehdr->e_version = EV_CURRENT;
1359                 ehdr->e_entry = 0;
1360                 ehdr->e_phoff = phoff;
1361                 ehdr->e_flags = 0;
1362                 ehdr->e_ehsize = sizeof(Elf_Ehdr);
1363                 ehdr->e_phentsize = sizeof(Elf_Phdr);
1364                 ehdr->e_phnum = numsegs + 1;
1365                 ehdr->e_shentsize = sizeof(Elf_Shdr);
1366                 ehdr->e_shnum = 0;
1367                 ehdr->e_shstrndx = SHN_UNDEF;
1368         }
1369         if (error == 0 && phdr != NULL) {
1370                 /*
1371                  * Fill in the program header entries.
1372                  */
1373                 struct phdr_closure phc;
1374
1375                 /* The note segement. */
1376                 phdr->p_type = PT_NOTE;
1377                 phdr->p_offset = noteoff;
1378                 phdr->p_vaddr = 0;
1379                 phdr->p_paddr = 0;
1380                 phdr->p_filesz = notesz;
1381                 phdr->p_memsz = 0;
1382                 phdr->p_flags = 0;
1383                 phdr->p_align = 0;
1384                 ++phdr;
1385
1386                 /* All the writable segments from the program. */
1387                 phc.phdr = phdr;
1388                 phc.phdr_max = phdr + numsegs;
1389                 phc.offset = target->off;
1390                 each_segment(p, cb_put_phdr, &phc, 1);
1391         }
1392         return (error);
1393 }
1394
1395 /*
1396  * Append core dump notes to target ELF buffer or simply update target size
1397  * if dryrun selected.
1398  */
1399 static int
1400 elf_putallnotes(struct lwp *corelp, elf_buf_t target, int sig,
1401     enum putmode mode)
1402 {
1403         struct proc *p = corelp->lwp_proc;
1404         int error;
1405         struct {
1406                 prstatus_t status;
1407                 prfpregset_t fpregs;
1408                 prpsinfo_t psinfo;
1409         } *tmpdata;
1410         prstatus_t *status;
1411         prfpregset_t *fpregs;
1412         prpsinfo_t *psinfo;
1413         struct lwp *lp;
1414
1415         /*
1416          * Allocate temporary storage for notes on heap to avoid stack overflow.
1417          */
1418         if (mode != DRYRUN) {
1419                 tmpdata = kmalloc(sizeof(*tmpdata), M_TEMP, M_ZERO | M_WAITOK);
1420                 status = &tmpdata->status;
1421                 fpregs = &tmpdata->fpregs;
1422                 psinfo = &tmpdata->psinfo;
1423         } else {
1424                 tmpdata = NULL;
1425                 status = NULL;
1426                 fpregs = NULL;
1427                 psinfo = NULL;
1428         }
1429
1430         /*
1431          * Append LWP-agnostic note.
1432          */
1433         if (mode != DRYRUN) {
1434                 psinfo->pr_version = PRPSINFO_VERSION;
1435                 psinfo->pr_psinfosz = sizeof(prpsinfo_t);
1436                 strlcpy(psinfo->pr_fname, p->p_comm,
1437                         sizeof(psinfo->pr_fname));
1438                 /*
1439                  * XXX - We don't fill in the command line arguments
1440                  * properly yet.
1441                  */
1442                 strlcpy(psinfo->pr_psargs, p->p_comm,
1443                         sizeof(psinfo->pr_psargs));
1444         }
1445         error =
1446             __elfN(putnote)(target, "CORE", NT_PRPSINFO, psinfo, sizeof *psinfo);
1447         if (error)
1448                 goto exit;
1449
1450         /*
1451          * Append first note for LWP that triggered core so that it is
1452          * the selected one when the debugger starts.
1453          */
1454         if (mode != DRYRUN) {
1455                 status->pr_version = PRSTATUS_VERSION;
1456                 status->pr_statussz = sizeof(prstatus_t);
1457                 status->pr_gregsetsz = sizeof(gregset_t);
1458                 status->pr_fpregsetsz = sizeof(fpregset_t);
1459                 status->pr_osreldate = osreldate;
1460                 status->pr_cursig = sig;
1461                 /*
1462                  * XXX GDB needs unique pr_pid for each LWP and does not
1463                  * not support pr_pid==0 but lwp_tid can be 0, so hack unique
1464                  * value.
1465                  */
1466                 status->pr_pid = corelp->lwp_tid;
1467                 fill_regs(corelp, &status->pr_reg);
1468                 fill_fpregs(corelp, fpregs);
1469         }
1470         error =
1471             __elfN(putnote)(target, "CORE", NT_PRSTATUS, status, sizeof *status);
1472         if (error)
1473                 goto exit;
1474         error =
1475             __elfN(putnote)(target, "CORE", NT_FPREGSET, fpregs, sizeof *fpregs);
1476         if (error)
1477                 goto exit;
1478
1479         /*
1480          * Then append notes for other LWPs.
1481          */
1482         FOREACH_LWP_IN_PROC(lp, p) {
1483                 if (lp == corelp)
1484                         continue;
1485                 /* skip lwps being created */
1486                 if (lp->lwp_thread == NULL)
1487                         continue;
1488                 if (mode != DRYRUN) {
1489                         status->pr_pid = lp->lwp_tid;
1490                         fill_regs(lp, &status->pr_reg);
1491                         fill_fpregs(lp, fpregs);
1492                 }
1493                 error = __elfN(putnote)(target, "CORE", NT_PRSTATUS,
1494                                         status, sizeof *status);
1495                 if (error)
1496                         goto exit;
1497                 error = __elfN(putnote)(target, "CORE", NT_FPREGSET,
1498                                         fpregs, sizeof *fpregs);
1499                 if (error)
1500                         goto exit;
1501         }
1502
1503 exit:
1504         if (tmpdata != NULL)
1505                 kfree(tmpdata, M_TEMP);
1506         return (error);
1507 }
1508
1509 /*
1510  * Generate a note sub-structure.
1511  *
1512  * NOTE: 4-byte alignment.
1513  */
1514 static int
1515 __elfN(putnote)(elf_buf_t target, const char *name, int type,
1516             const void *desc, size_t descsz)
1517 {
1518         int error = 0;
1519         char *dst;
1520         Elf_Note note;
1521
1522         note.n_namesz = strlen(name) + 1;
1523         note.n_descsz = descsz;
1524         note.n_type = type;
1525         dst = target_reserve(target, sizeof(note), &error);
1526         if (dst != NULL)
1527                 bcopy(&note, dst, sizeof note);
1528         dst = target_reserve(target, note.n_namesz, &error);
1529         if (dst != NULL)
1530                 bcopy(name, dst, note.n_namesz);
1531         target->off = roundup2(target->off, sizeof(Elf_Word));
1532         dst = target_reserve(target, note.n_descsz, &error);
1533         if (dst != NULL)
1534                 bcopy(desc, dst, note.n_descsz);
1535         target->off = roundup2(target->off, sizeof(Elf_Word));
1536         return (error);
1537 }
1538
1539
1540 static int
1541 elf_putsigs(struct lwp *lp, elf_buf_t target)
1542 {
1543         /* XXX lwp handle more than one lwp */
1544         struct proc *p = lp->lwp_proc;
1545         int error = 0;
1546         struct ckpt_siginfo *csi;
1547
1548         csi = target_reserve(target, sizeof(struct ckpt_siginfo), &error);
1549         if (csi) {
1550                 csi->csi_ckptpisz = sizeof(struct ckpt_siginfo);
1551                 bcopy(p->p_sigacts, &csi->csi_sigacts, sizeof(*p->p_sigacts));
1552                 bcopy(&p->p_realtimer, &csi->csi_itimerval, sizeof(struct itimerval));
1553                 bcopy(&lp->lwp_sigmask, &csi->csi_sigmask,
1554                         sizeof(sigset_t));
1555                 csi->csi_sigparent = p->p_sigparent;
1556         }
1557         return (error);
1558 }
1559
1560 static int
1561 elf_putfiles(struct proc *p, elf_buf_t target, struct file *ckfp)
1562 {
1563         int error = 0;
1564         int i;
1565         struct ckpt_filehdr *cfh = NULL;
1566         struct ckpt_fileinfo *cfi;
1567         struct file *fp;        
1568         struct vnode *vp;
1569         /*
1570          * the duplicated loop is gross, but it was the only way
1571          * to eliminate uninitialized variable warnings 
1572          */
1573         cfh = target_reserve(target, sizeof(struct ckpt_filehdr), &error);
1574         if (cfh) {
1575                 cfh->cfh_nfiles = 0;            
1576         }
1577
1578         /*
1579          * ignore STDIN/STDERR/STDOUT.
1580          */
1581         for (i = 3; error == 0 && i < p->p_fd->fd_nfiles; i++) {
1582                 fp = holdfp(p->p_fd, i, -1);
1583                 if (fp == NULL)
1584                         continue;
1585                 /* 
1586                  * XXX Only checkpoint vnodes for now.
1587                  */
1588                 if (fp->f_type != DTYPE_VNODE) {
1589                         fdrop(fp);
1590                         continue;
1591                 }
1592                 cfi = target_reserve(target, sizeof(struct ckpt_fileinfo),
1593                                         &error);
1594                 if (cfi == NULL) {
1595                         fdrop(fp);
1596                         continue;
1597                 }
1598                 cfi->cfi_index = -1;
1599                 cfi->cfi_type = fp->f_type;
1600                 cfi->cfi_flags = fp->f_flag;
1601                 cfi->cfi_offset = fp->f_offset;
1602                 cfi->cfi_ckflags = 0;
1603
1604                 if (fp == ckfp)
1605                         cfi->cfi_ckflags |= CKFIF_ISCKPTFD;
1606                 /* f_count and f_msgcount should not be saved/restored */
1607                 /* XXX save cred info */
1608
1609                 switch(fp->f_type) {
1610                 case DTYPE_VNODE:
1611                         vp = (struct vnode *)fp->f_data;
1612                         /*
1613                          * it looks like a bug in ptrace is marking 
1614                          * a non-vnode as a vnode - until we find the 
1615                          * root cause this will at least prevent
1616                          * further panics from truss
1617                          */
1618                         if (vp == NULL || vp->v_mount == NULL)
1619                                 break;
1620                         cfh->cfh_nfiles++;
1621                         cfi->cfi_index = i;
1622                         cfi->cfi_fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
1623                         error = VFS_VPTOFH(vp, &cfi->cfi_fh.fh_fid);
1624                         break;
1625                 default:
1626                         break;
1627                 }
1628                 fdrop(fp);
1629         }
1630         return (error);
1631 }
1632
1633 static int
1634 elf_puttextvp(struct proc *p, elf_buf_t target)
1635 {
1636         int error = 0;
1637         int *vn_count;
1638         struct fp_closure fpc;
1639         struct ckpt_vminfo *vminfo;
1640
1641         vminfo = target_reserve(target, sizeof(struct ckpt_vminfo), &error);
1642         if (vminfo != NULL) {
1643                 vminfo->cvm_dsize = p->p_vmspace->vm_dsize;
1644                 vminfo->cvm_tsize = p->p_vmspace->vm_tsize;
1645                 vminfo->cvm_daddr = p->p_vmspace->vm_daddr;
1646                 vminfo->cvm_taddr = p->p_vmspace->vm_taddr;
1647         }
1648
1649         fpc.count = 0;
1650         vn_count = target_reserve(target, sizeof(int), &error);
1651         if (target->buf != NULL) {
1652                 fpc.vnh = (struct vn_hdr *)(target->buf + target->off);
1653                 fpc.vnh_max = fpc.vnh + 
1654                         (target->off_max - target->off) / sizeof(struct vn_hdr);
1655                 error = each_segment(p, cb_put_fp, &fpc, 0);
1656                 if (vn_count)
1657                         *vn_count = fpc.count;
1658         } else {
1659                 error = each_segment(p, cb_fpcount_segment, &fpc.count, 0);
1660         }
1661         target->off += fpc.count * sizeof(struct vn_hdr);
1662         return (error);
1663 }
1664
1665 /*
1666  * Try to find the appropriate ABI-note section for checknote,
1667  * The entire image is searched if necessary, not only the first page.
1668  */
1669 static boolean_t
1670 __elfN(check_note)(struct image_params *imgp, Elf_Brandnote *checknote,
1671     int32_t *osrel)
1672 {
1673         boolean_t valid_note_found;
1674         const Elf_Phdr *phdr, *pnote;
1675         const Elf_Ehdr *hdr;
1676         int i;
1677
1678         valid_note_found = FALSE;
1679         hdr = (const Elf_Ehdr *)imgp->image_header;
1680         phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
1681
1682         for (i = 0; i < hdr->e_phnum; i++) {
1683                 if (phdr[i].p_type == PT_NOTE) {
1684                         pnote = &phdr[i];
1685                         valid_note_found = check_PT_NOTE (imgp, checknote,
1686                                 osrel, pnote);
1687                         if (valid_note_found)
1688                                 break;
1689                 }
1690         }
1691         return valid_note_found;
1692 }
1693
1694 /*
1695  * Be careful not to create new overflow conditions when checking
1696  * for overflow.
1697  */
1698 static boolean_t
1699 note_overflow(const Elf_Note *note, size_t maxsize)
1700 {
1701         if (sizeof(*note) > maxsize)
1702                 return TRUE;
1703         if (note->n_namesz > maxsize - sizeof(*note))
1704                 return TRUE;
1705         return FALSE;
1706 }
1707
1708 static boolean_t
1709 hdr_overflow(__ElfN(Off) off_beg, __ElfN(Size) size)
1710 {
1711         __ElfN(Off) off_end;
1712
1713         off_end = off_beg + size;
1714         if (off_end < off_beg)
1715                 return TRUE;
1716         return FALSE;
1717 }
1718
1719 static boolean_t
1720 check_PT_NOTE(struct image_params *imgp, Elf_Brandnote *checknote,
1721               int32_t *osrel, const Elf_Phdr * pnote)
1722 {
1723         boolean_t limited_to_first_page;
1724         boolean_t found = FALSE;
1725         const Elf_Note *note, *note0, *note_end;
1726         const char *note_name;
1727         __ElfN(Off) noteloc, firstloc;
1728         __ElfN(Size) notesz, firstlen, endbyte;
1729         struct lwbuf *lwb;
1730         struct lwbuf lwb_cache;
1731         const char *page;
1732         char *data = NULL;
1733         int n;
1734
1735         if (hdr_overflow(pnote->p_offset, pnote->p_filesz))
1736                 return (FALSE);
1737         notesz = pnote->p_filesz;
1738         noteloc = pnote->p_offset;
1739         endbyte = noteloc + notesz;
1740         limited_to_first_page = noteloc < PAGE_SIZE && endbyte < PAGE_SIZE;
1741
1742         if (limited_to_first_page) {
1743                 note = (const Elf_Note *)(imgp->image_header + noteloc);
1744                 note_end = (const Elf_Note *)(imgp->image_header + endbyte);
1745                 note0 = note;
1746         } else {
1747                 firstloc = noteloc & PAGE_MASK;
1748                 firstlen = PAGE_SIZE - firstloc;
1749                 if (notesz < sizeof(Elf_Note) || notesz > PAGE_SIZE)
1750                         return (FALSE);
1751
1752                 lwb = &lwb_cache;
1753                 if (exec_map_page(imgp, noteloc >> PAGE_SHIFT, &lwb, &page))
1754                         return (FALSE);
1755                 if (firstlen < notesz) {         /* crosses page boundary */
1756                         data = kmalloc(notesz, M_TEMP, M_WAITOK);
1757                         bcopy(page + firstloc, data, firstlen);
1758
1759                         exec_unmap_page(lwb);
1760                         lwb = &lwb_cache;
1761                         if (exec_map_page(imgp, (noteloc >> PAGE_SHIFT) + 1,
1762                                 &lwb, &page)) {
1763                                 kfree(data, M_TEMP);
1764                                 return (FALSE);
1765                         }
1766                         bcopy(page, data + firstlen, notesz - firstlen);
1767                         note = note0 = (const Elf_Note *)(data);
1768                         note_end = (const Elf_Note *)(data + notesz);
1769                 } else {
1770                         note = note0 = (const Elf_Note *)(page + firstloc);
1771                         note_end = (const Elf_Note *)(page + firstloc +
1772                                 firstlen);
1773                 }
1774         }
1775
1776         for (n = 0; n < 100 && note >= note0 && note < note_end; n++) {
1777                 if (!aligned(note, Elf32_Addr))
1778                         break;
1779                 if (note_overflow(note, (const char *)note_end -
1780                                         (const char *)note)) {
1781                         break;
1782                 }
1783                 note_name = (const char *)(note + 1);
1784
1785                 if (note->n_namesz == checknote->hdr.n_namesz
1786                     && note->n_descsz == checknote->hdr.n_descsz
1787                     && note->n_type == checknote->hdr.n_type
1788                     && (strncmp(checknote->vendor, note_name,
1789                         checknote->hdr.n_namesz) == 0)) {
1790                         /* Fetch osreldata from ABI.note-tag */
1791                         if ((checknote->flags & BN_TRANSLATE_OSREL) != 0 &&
1792                             checknote->trans_osrel != NULL)
1793                                 checknote->trans_osrel(note, osrel);
1794                         found = TRUE;
1795                         break;
1796                 }
1797                 note = (const Elf_Note *)((const char *)(note + 1) +
1798                     roundup2(note->n_namesz, sizeof(Elf32_Addr)) +
1799                     roundup2(note->n_descsz, sizeof(Elf32_Addr)));
1800         }
1801
1802         if (!limited_to_first_page) {
1803                 if (data != NULL)
1804                         kfree(data, M_TEMP);
1805                 exec_unmap_page(lwb);
1806         }
1807         return (found);
1808 }
1809
1810 /*
1811  * The interpreter program header may be located beyond the first page, so
1812  * regardless of its location, a copy of the interpreter path is created so
1813  * that it may be safely referenced by the calling function in all case.  The
1814  * memory is allocated by calling function, and the copying is done here.
1815  */
1816 static boolean_t
1817 extract_interpreter(struct image_params *imgp, const Elf_Phdr *pinterpreter,
1818                     char *data)
1819 {
1820         boolean_t limited_to_first_page;
1821         const boolean_t result_success = FALSE;
1822         const boolean_t result_failure = TRUE;
1823         __ElfN(Off) pathloc, firstloc;
1824         __ElfN(Size) pathsz, firstlen, endbyte;
1825         struct lwbuf *lwb;
1826         struct lwbuf lwb_cache;
1827         const char *page;
1828
1829         if (hdr_overflow(pinterpreter->p_offset, pinterpreter->p_filesz))
1830                 return (result_failure);
1831         pathsz  = pinterpreter->p_filesz;
1832         pathloc = pinterpreter->p_offset;
1833         endbyte = pathloc + pathsz;
1834
1835         limited_to_first_page = pathloc < PAGE_SIZE && endbyte < PAGE_SIZE;
1836         if (limited_to_first_page) {
1837                 bcopy(imgp->image_header + pathloc, data, pathsz);
1838                 return (result_success);
1839         }
1840
1841         firstloc = pathloc & PAGE_MASK;
1842         firstlen = PAGE_SIZE - firstloc;
1843
1844         lwb = &lwb_cache;
1845         if (exec_map_page(imgp, pathloc >> PAGE_SHIFT, &lwb, &page))
1846                 return (result_failure);
1847
1848         if (firstlen < pathsz) {         /* crosses page boundary */
1849                 bcopy(page + firstloc, data, firstlen);
1850
1851                 exec_unmap_page(lwb);
1852                 lwb = &lwb_cache;
1853                 if (exec_map_page(imgp, (pathloc >> PAGE_SHIFT) + 1, &lwb,
1854                         &page))
1855                         return (result_failure);
1856                 bcopy(page, data + firstlen, pathsz - firstlen);
1857         } else
1858                 bcopy(page + firstloc, data, pathsz);
1859
1860         exec_unmap_page(lwb);
1861         return (result_success);
1862 }
1863
1864 static boolean_t
1865 __elfN(bsd_trans_osrel)(const Elf_Note *note, int32_t *osrel)
1866 {
1867         uintptr_t p;
1868
1869         p = (uintptr_t)(note + 1);
1870         p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
1871         *osrel = *(const int32_t *)(p);
1872
1873         return (TRUE);
1874 }
1875
1876 /*
1877  * Tell kern_execve.c about it, with a little help from the linker.
1878  */
1879 #if defined(__x86_64__)
1880 static struct execsw elf_execsw = {exec_elf64_imgact, "ELF64"};
1881 EXEC_SET_ORDERED(elf64, elf_execsw, SI_ORDER_FIRST);
1882 #else /* i386 assumed */
1883 static struct execsw elf_execsw = {exec_elf32_imgact, "ELF32"};
1884 EXEC_SET_ORDERED(elf32, elf_execsw, SI_ORDER_FIRST);
1885 #endif
1886
1887 static vm_prot_t
1888 __elfN(trans_prot)(Elf_Word flags)
1889 {
1890         vm_prot_t prot;
1891
1892         prot = 0;
1893         if (flags & PF_X)
1894                 prot |= VM_PROT_EXECUTE;
1895         if (flags & PF_W)
1896                 prot |= VM_PROT_WRITE;
1897         if (flags & PF_R)
1898                 prot |= VM_PROT_READ;
1899         return (prot);
1900 }
1901
1902 static Elf_Word
1903 __elfN(untrans_prot)(vm_prot_t prot)
1904 {
1905         Elf_Word flags;
1906
1907         flags = 0;
1908         if (prot & VM_PROT_EXECUTE)
1909                 flags |= PF_X;
1910         if (prot & VM_PROT_READ)
1911                 flags |= PF_R;
1912         if (prot & VM_PROT_WRITE)
1913                 flags |= PF_W;
1914         return (flags);
1915 }