The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/powerpc/powerpc/elf64_machdep.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
    3  *
    4  * Copyright 1996-1998 John D. Polstra.
    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  * 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  *
   16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   26  *
   27  * $FreeBSD$
   28  */
   29 
   30 #include <sys/param.h>
   31 #include <sys/kernel.h>
   32 #include <sys/systm.h>
   33 #include <sys/exec.h>
   34 #include <sys/imgact.h>
   35 #include <sys/malloc.h>
   36 #include <sys/proc.h>
   37 #include <sys/namei.h>
   38 #include <sys/fcntl.h>
   39 #include <sys/sysent.h>
   40 #include <sys/imgact_elf.h>
   41 #include <sys/jail.h>
   42 #include <sys/smp.h>
   43 #include <sys/syscall.h>
   44 #include <sys/signalvar.h>
   45 #include <sys/vnode.h>
   46 #include <sys/linker.h>
   47 
   48 #include <vm/vm.h>
   49 #include <vm/vm_param.h>
   50 
   51 #include <machine/altivec.h>
   52 #include <machine/cpu.h>
   53 #include <machine/fpu.h>
   54 #include <machine/elf.h>
   55 #include <machine/md_var.h>
   56 
   57 #include <powerpc/powerpc/elf_common.c>
   58 
   59 static void exec_setregs_funcdesc(struct thread *td, struct image_params *imgp,
   60     uintptr_t stack);
   61 
   62 struct sysentvec elf64_freebsd_sysvec_v1 = {
   63         .sv_size        = SYS_MAXSYSCALL,
   64         .sv_table       = sysent,
   65         .sv_transtrap   = NULL,
   66         .sv_fixup       = __elfN(freebsd_fixup),
   67         .sv_sendsig     = sendsig,
   68         .sv_sigcode     = sigcode64,
   69         .sv_szsigcode   = &szsigcode64,
   70         .sv_name        = "FreeBSD ELF64",
   71         .sv_coredump    = __elfN(coredump),
   72         .sv_imgact_try  = NULL,
   73         .sv_minsigstksz = MINSIGSTKSZ,
   74         .sv_minuser     = VM_MIN_ADDRESS,
   75         .sv_maxuser     = VM_MAXUSER_ADDRESS,
   76         .sv_usrstack    = USRSTACK,
   77         .sv_psstrings   = PS_STRINGS,
   78         .sv_stackprot   = VM_PROT_ALL,
   79         .sv_copyout_auxargs = __elfN(powerpc_copyout_auxargs),
   80         .sv_copyout_strings = exec_copyout_strings,
   81         .sv_setregs     = exec_setregs_funcdesc,
   82         .sv_fixlimit    = NULL,
   83         .sv_maxssiz     = NULL,
   84         .sv_flags       = SV_ABI_FREEBSD | SV_LP64 | SV_SHP | SV_ASLR |
   85                             SV_TIMEKEEP | SV_RNG_SEED_VER,
   86         .sv_set_syscall_retval = cpu_set_syscall_retval,
   87         .sv_fetch_syscall_args = cpu_fetch_syscall_args,
   88         .sv_syscallnames = syscallnames,
   89         .sv_shared_page_base = SHAREDPAGE,
   90         .sv_shared_page_len = PAGE_SIZE,
   91         .sv_schedtail   = NULL,
   92         .sv_thread_detach = NULL,
   93         .sv_trap        = NULL,
   94         .sv_hwcap       = &cpu_features,
   95         .sv_hwcap2      = &cpu_features2,
   96         .sv_onexec_old  = exec_onexec_old,
   97         .sv_onexit      = exit_onexit,
   98 };
   99 
  100 struct sysentvec elf64_freebsd_sysvec_v2 = {
  101         .sv_size        = SYS_MAXSYSCALL,
  102         .sv_table       = sysent,
  103         .sv_transtrap   = NULL,
  104         .sv_fixup       = __elfN(freebsd_fixup),
  105         .sv_sendsig     = sendsig,
  106         .sv_sigcode     = sigcode64, /* Fixed up in ppc64_init_sysvecs(). */
  107         .sv_szsigcode   = &szsigcode64,
  108         .sv_name        = "FreeBSD ELF64 V2",
  109         .sv_coredump    = __elfN(coredump),
  110         .sv_imgact_try  = NULL,
  111         .sv_minsigstksz = MINSIGSTKSZ,
  112         .sv_minuser     = VM_MIN_ADDRESS,
  113         .sv_maxuser     = VM_MAXUSER_ADDRESS,
  114         .sv_usrstack    = USRSTACK,
  115         .sv_psstrings   = PS_STRINGS,
  116         .sv_stackprot   = VM_PROT_ALL,
  117         .sv_copyout_auxargs = __elfN(powerpc_copyout_auxargs),
  118         .sv_copyout_strings = exec_copyout_strings,
  119         .sv_setregs     = exec_setregs,
  120         .sv_fixlimit    = NULL,
  121         .sv_maxssiz     = NULL,
  122         .sv_flags       = SV_ABI_FREEBSD | SV_LP64 | SV_SHP |
  123                             SV_TIMEKEEP | SV_RNG_SEED_VER,
  124         .sv_set_syscall_retval = cpu_set_syscall_retval,
  125         .sv_fetch_syscall_args = cpu_fetch_syscall_args,
  126         .sv_syscallnames = syscallnames,
  127         .sv_shared_page_base = SHAREDPAGE,
  128         .sv_shared_page_len = PAGE_SIZE,
  129         .sv_schedtail   = NULL,
  130         .sv_thread_detach = NULL,
  131         .sv_trap        = NULL,
  132         .sv_hwcap       = &cpu_features,
  133         .sv_hwcap2      = &cpu_features2,
  134         .sv_onexec_old  = exec_onexec_old,
  135         .sv_onexit      = exit_onexit,
  136 };
  137 
  138 static boolean_t ppc64_elfv1_header_match(struct image_params *params,
  139     int32_t *, uint32_t *);
  140 static boolean_t ppc64_elfv2_header_match(struct image_params *params,
  141     int32_t *, uint32_t *);
  142 
  143 static Elf64_Brandinfo freebsd_brand_info_elfv1 = {
  144         .brand          = ELFOSABI_FREEBSD,
  145         .machine        = EM_PPC64,
  146         .compat_3_brand = "FreeBSD",
  147         .emul_path      = NULL,
  148         .interp_path    = "/libexec/ld-elf.so.1",
  149         .sysvec         = &elf64_freebsd_sysvec_v1,
  150         .interp_newpath = NULL,
  151         .brand_note     = &elf64_freebsd_brandnote,
  152         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE,
  153         .header_supported = &ppc64_elfv1_header_match
  154 };
  155 
  156 SYSINIT(elf64v1, SI_SUB_EXEC, SI_ORDER_ANY,
  157     (sysinit_cfunc_t) elf64_insert_brand_entry,
  158     &freebsd_brand_info_elfv1);
  159 
  160 static Elf64_Brandinfo freebsd_brand_info_elfv2 = {
  161         .brand          = ELFOSABI_FREEBSD,
  162         .machine        = EM_PPC64,
  163         .compat_3_brand = "FreeBSD",
  164         .emul_path      = NULL,
  165         .interp_path    = "/libexec/ld-elf.so.1",
  166         .sysvec         = &elf64_freebsd_sysvec_v2,
  167         .interp_newpath = NULL,
  168         .brand_note     = &elf64_freebsd_brandnote,
  169         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE,
  170         .header_supported = &ppc64_elfv2_header_match
  171 };
  172 
  173 SYSINIT(elf64v2, SI_SUB_EXEC, SI_ORDER_ANY,
  174     (sysinit_cfunc_t) elf64_insert_brand_entry,
  175     &freebsd_brand_info_elfv2);
  176 
  177 static Elf64_Brandinfo freebsd_brand_oinfo = {
  178         .brand          = ELFOSABI_FREEBSD,
  179         .machine        = EM_PPC64,
  180         .compat_3_brand = "FreeBSD",
  181         .emul_path      = NULL,
  182         .interp_path    = "/usr/libexec/ld-elf.so.1",
  183         .sysvec         = &elf64_freebsd_sysvec_v1,
  184         .interp_newpath = NULL,
  185         .brand_note     = &elf64_freebsd_brandnote,
  186         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE,
  187         .header_supported = &ppc64_elfv1_header_match
  188 };
  189 
  190 SYSINIT(oelf64, SI_SUB_EXEC, SI_ORDER_ANY,
  191         (sysinit_cfunc_t) elf64_insert_brand_entry,
  192         &freebsd_brand_oinfo);
  193 
  194 void elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase);
  195 
  196 static void
  197 ppc64_init_sysvecs(void *arg)
  198 {
  199         exec_sysvec_init(&elf64_freebsd_sysvec_v2);
  200         exec_sysvec_init_secondary(&elf64_freebsd_sysvec_v2,
  201             &elf64_freebsd_sysvec_v1);
  202         /*
  203          * Adjust elfv2 sigcode after elfv1 sysvec is initialized.
  204          * exec_sysvec_init_secondary() assumes secondary sysvecs use
  205          * identical signal code, and skips allocating a second copy.
  206          * Since the ELFv2 trampoline is a strict subset of the ELFv1 code,
  207          * we can work around this by adjusting the base address. This also
  208          * avoids two copies of the trampoline code being allocated!
  209          */
  210         elf64_freebsd_sysvec_v2.sv_sigcode_base +=
  211             (uintptr_t)sigcode64_elfv2 - (uintptr_t)&sigcode64;
  212         elf64_freebsd_sysvec_v2.sv_szsigcode = &szsigcode64_elfv2;
  213 }
  214 SYSINIT(elf64_sysvec, SI_SUB_EXEC, SI_ORDER_ANY, ppc64_init_sysvecs, NULL);
  215 
  216 static boolean_t
  217 ppc64_elfv1_header_match(struct image_params *params, int32_t *osrel __unused,
  218     uint32_t *fctl0 __unused)
  219 {
  220         const Elf64_Ehdr *hdr = (const Elf64_Ehdr *)params->image_header;
  221         int abi = (hdr->e_flags & 3);
  222 
  223         return (abi == 0 || abi == 1);
  224 }
  225 
  226 static boolean_t
  227 ppc64_elfv2_header_match(struct image_params *params, int32_t *osrel __unused,
  228     uint32_t *fctl0 __unused)
  229 {
  230         const Elf64_Ehdr *hdr = (const Elf64_Ehdr *)params->image_header;
  231         int abi = (hdr->e_flags & 3);
  232 
  233         return (abi == 2);
  234 }
  235 
  236 static void  
  237 exec_setregs_funcdesc(struct thread *td, struct image_params *imgp,
  238     uintptr_t stack)
  239 {
  240         struct trapframe *tf;
  241         register_t entry_desc[3];
  242 
  243         tf = trapframe(td);
  244         exec_setregs(td, imgp, stack);
  245 
  246         /*
  247          * For 64-bit ELFv1, we need to disentangle the function
  248          * descriptor
  249          *
  250          * 0. entry point
  251          * 1. TOC value (r2)
  252          * 2. Environment pointer (r11)
  253          */
  254 
  255         (void)copyin((void *)imgp->entry_addr, entry_desc,
  256             sizeof(entry_desc));
  257         tf->srr0 = entry_desc[0] + imgp->reloc_base;
  258         tf->fixreg[2] = entry_desc[1] + imgp->reloc_base;
  259         tf->fixreg[11] = entry_desc[2] + imgp->reloc_base;
  260 }
  261 
  262 void
  263 elf64_dump_thread(struct thread *td, void *dst, size_t *off)
  264 {
  265         size_t len;
  266         struct pcb *pcb;
  267         uint64_t vshr[32];
  268         uint64_t *vsr_dw1;
  269         int vsr_idx;
  270 
  271         len = 0;
  272         pcb = td->td_pcb;
  273 
  274         if (pcb->pcb_flags & PCB_VEC) {
  275                 save_vec_nodrop(td);
  276                 if (dst != NULL) {
  277                         len += elf64_populate_note(NT_PPC_VMX,
  278                             &pcb->pcb_vec, (char *)dst + len,
  279                             sizeof(pcb->pcb_vec), NULL);
  280                 } else
  281                         len += elf64_populate_note(NT_PPC_VMX, NULL, NULL,
  282                             sizeof(pcb->pcb_vec), NULL);
  283         }
  284 
  285         if (pcb->pcb_flags & PCB_VSX) {
  286                 save_fpu_nodrop(td);
  287                 if (dst != NULL) {
  288                         /*
  289                          * Doubleword 0 of VSR0-VSR31 overlap with FPR0-FPR31 and
  290                          * VSR32-VSR63 overlap with VR0-VR31, so we only copy
  291                          * the non-overlapping data, which is doubleword 1 of VSR0-VSR31.
  292                          */
  293                         for (vsr_idx = 0; vsr_idx < nitems(vshr); vsr_idx++) {
  294                                 vsr_dw1 = (uint64_t *)&pcb->pcb_fpu.fpr[vsr_idx].vsr[2];
  295                                 vshr[vsr_idx] = *vsr_dw1;
  296                         }
  297                         len += elf64_populate_note(NT_PPC_VSX,
  298                             vshr, (char *)dst + len,
  299                             sizeof(vshr), NULL);
  300                 } else
  301                         len += elf64_populate_note(NT_PPC_VSX, NULL, NULL,
  302                             sizeof(vshr), NULL);
  303         }
  304 
  305         *off = len;
  306 }
  307 
  308 bool
  309 elf_is_ifunc_reloc(Elf_Size r_info)
  310 {
  311 
  312         return (ELF_R_TYPE(r_info) == R_PPC_IRELATIVE);
  313 }
  314 
  315 /* Process one elf relocation with addend. */
  316 static int
  317 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
  318     int type, int local, elf_lookup_fn lookup)
  319 {
  320         Elf_Addr *where;
  321         Elf_Addr addr;
  322         Elf_Addr addend, val;
  323         Elf_Word rtype, symidx;
  324         const Elf_Rela *rela;
  325         int error;
  326 
  327         switch (type) {
  328         case ELF_RELOC_REL:
  329                 panic("PPC only supports RELA relocations");
  330                 break;
  331         case ELF_RELOC_RELA:
  332                 rela = (const Elf_Rela *)data;
  333                 where = (Elf_Addr *) (relocbase + rela->r_offset);
  334                 addend = rela->r_addend;
  335                 rtype = ELF_R_TYPE(rela->r_info);
  336                 symidx = ELF_R_SYM(rela->r_info);
  337                 break;
  338         default:
  339                 panic("elf_reloc: unknown relocation mode %d\n", type);
  340         }
  341 
  342         switch (rtype) {
  343         case R_PPC_NONE:
  344                 break;
  345 
  346         case R_PPC64_ADDR64:    /* doubleword64 S + A */
  347                 error = lookup(lf, symidx, 1, &addr);
  348                 if (error != 0)
  349                         return (-1);
  350                 addr += addend;
  351                 *where = addr;
  352                 break;
  353 
  354         case R_PPC_RELATIVE:    /* doubleword64 B + A */
  355                 *where = elf_relocaddr(lf, relocbase + addend);
  356                 break;
  357 
  358         case R_PPC_JMP_SLOT:    /* function descriptor copy */
  359                 lookup(lf, symidx, 1, &addr);
  360 #if !defined(_CALL_ELF) || _CALL_ELF == 1
  361                 memcpy(where, (Elf_Addr *)addr, 3*sizeof(Elf_Addr));
  362 #else
  363                 *where = addr;
  364 #endif
  365                 __asm __volatile("dcbst 0,%0; sync" :: "r"(where) : "memory");
  366                 break;
  367 
  368         case R_PPC_IRELATIVE:
  369                 addr = relocbase + addend;
  370                 val = ((Elf64_Addr (*)(void))addr)();
  371                 if (*where != val)
  372                         *where = val;
  373                 break;
  374 
  375         default:
  376                 printf("kldload: unexpected relocation type %d, "
  377                     "symbol index %d\n", (int)rtype, symidx);
  378                 return (-1);
  379         }
  380         return (0);
  381 }
  382 
  383 void
  384 elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase)
  385 {
  386         Elf_Rela *rela = NULL, *relalim;
  387         Elf_Addr relasz = 0;
  388         Elf_Addr *where;
  389 
  390         /*
  391          * Extract the rela/relasz values from the dynamic section
  392          */
  393         for (; dynp->d_tag != DT_NULL; dynp++) {
  394                 switch (dynp->d_tag) {
  395                 case DT_RELA:
  396                         rela = (Elf_Rela *)(relocbase+dynp->d_un.d_ptr);
  397                         break;
  398                 case DT_RELASZ:
  399                         relasz = dynp->d_un.d_val;
  400                         break;
  401                 }
  402         }
  403 
  404         /*
  405          * Relocate these values
  406          */
  407         relalim = (Elf_Rela *)((caddr_t)rela + relasz);
  408         for (; rela < relalim; rela++) {
  409                 if (ELF_R_TYPE(rela->r_info) != R_PPC_RELATIVE)
  410                         continue;
  411                 where = (Elf_Addr *)(relocbase + rela->r_offset);
  412                 *where = (Elf_Addr)(relocbase + rela->r_addend);
  413         }
  414 }
  415 
  416 int
  417 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
  418     elf_lookup_fn lookup)
  419 {
  420 
  421         return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
  422 }
  423 
  424 int
  425 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
  426     int type, elf_lookup_fn lookup)
  427 {
  428 
  429         return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
  430 }
  431 
  432 int
  433 elf_cpu_load_file(linker_file_t lf)
  434 {
  435         /* Only sync the cache for non-kernel modules */
  436         if (lf->id != 1)
  437                 __syncicache(lf->address, lf->size);
  438         return (0);
  439 }
  440 
  441 int
  442 elf_cpu_unload_file(linker_file_t lf __unused)
  443 {
  444 
  445         return (0);
  446 }
  447 
  448 int
  449 elf_cpu_parse_dynamic(caddr_t loadbase __unused, Elf_Dyn *dynamic __unused)
  450 {
  451 
  452         return (0);
  453 }

Cache object: b8ab2fd890cb53413278f610ec515a82


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.