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

Cache object: 56097918eec1f2120883e37860f136e0


[ 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.