elf64_machdep.c 12 KB

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