1 /* This is the Linux kernel elf-loading code, ported into user space */ 2 #include "qemu/osdep.h" 3 #include <sys/param.h> 4 5 #include <sys/prctl.h> 6 #include <sys/resource.h> 7 #include <sys/shm.h> 8 9 #include "qemu.h" 10 #include "user/tswap-target.h" 11 #include "user/page-protection.h" 12 #include "exec/page-protection.h" 13 #include "exec/translation-block.h" 14 #include "user/guest-base.h" 15 #include "user-internals.h" 16 #include "signal-common.h" 17 #include "loader.h" 18 #include "user-mmap.h" 19 #include "disas/disas.h" 20 #include "qemu/bitops.h" 21 #include "qemu/path.h" 22 #include "qemu/queue.h" 23 #include "qemu/guest-random.h" 24 #include "qemu/units.h" 25 #include "qemu/selfmap.h" 26 #include "qemu/lockable.h" 27 #include "qapi/error.h" 28 #include "qemu/error-report.h" 29 #include "target_signal.h" 30 #include "tcg/debuginfo.h" 31 32 #ifdef TARGET_ARM 33 #include "target/arm/cpu-features.h" 34 #endif 35 36 #ifdef _ARCH_PPC64 37 #undef ARCH_DLINFO 38 #undef ELF_PLATFORM 39 #undef ELF_HWCAP 40 #undef ELF_HWCAP2 41 #undef ELF_CLASS 42 #undef ELF_DATA 43 #undef ELF_ARCH 44 #endif 45 46 #ifndef TARGET_ARCH_HAS_SIGTRAMP_PAGE 47 #define TARGET_ARCH_HAS_SIGTRAMP_PAGE 0 48 #endif 49 50 typedef struct { 51 const uint8_t *image; 52 const uint32_t *relocs; 53 unsigned image_size; 54 unsigned reloc_count; 55 unsigned sigreturn_ofs; 56 unsigned rt_sigreturn_ofs; 57 } VdsoImageInfo; 58 59 #define ELF_OSABI ELFOSABI_SYSV 60 61 /* from personality.h */ 62 63 /* 64 * Flags for bug emulation. 65 * 66 * These occupy the top three bytes. 67 */ 68 enum { 69 ADDR_NO_RANDOMIZE = 0x0040000, /* disable randomization of VA space */ 70 FDPIC_FUNCPTRS = 0x0080000, /* userspace function ptrs point to 71 descriptors (signal handling) */ 72 MMAP_PAGE_ZERO = 0x0100000, 73 ADDR_COMPAT_LAYOUT = 0x0200000, 74 READ_IMPLIES_EXEC = 0x0400000, 75 ADDR_LIMIT_32BIT = 0x0800000, 76 SHORT_INODE = 0x1000000, 77 WHOLE_SECONDS = 0x2000000, 78 STICKY_TIMEOUTS = 0x4000000, 79 ADDR_LIMIT_3GB = 0x8000000, 80 }; 81 82 /* 83 * Personality types. 84 * 85 * These go in the low byte. Avoid using the top bit, it will 86 * conflict with error returns. 87 */ 88 enum { 89 PER_LINUX = 0x0000, 90 PER_LINUX_32BIT = 0x0000 | ADDR_LIMIT_32BIT, 91 PER_LINUX_FDPIC = 0x0000 | FDPIC_FUNCPTRS, 92 PER_SVR4 = 0x0001 | STICKY_TIMEOUTS | MMAP_PAGE_ZERO, 93 PER_SVR3 = 0x0002 | STICKY_TIMEOUTS | SHORT_INODE, 94 PER_SCOSVR3 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS | SHORT_INODE, 95 PER_OSR5 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS, 96 PER_WYSEV386 = 0x0004 | STICKY_TIMEOUTS | SHORT_INODE, 97 PER_ISCR4 = 0x0005 | STICKY_TIMEOUTS, 98 PER_BSD = 0x0006, 99 PER_SUNOS = 0x0006 | STICKY_TIMEOUTS, 100 PER_XENIX = 0x0007 | STICKY_TIMEOUTS | SHORT_INODE, 101 PER_LINUX32 = 0x0008, 102 PER_LINUX32_3GB = 0x0008 | ADDR_LIMIT_3GB, 103 PER_IRIX32 = 0x0009 | STICKY_TIMEOUTS,/* IRIX5 32-bit */ 104 PER_IRIXN32 = 0x000a | STICKY_TIMEOUTS,/* IRIX6 new 32-bit */ 105 PER_IRIX64 = 0x000b | STICKY_TIMEOUTS,/* IRIX6 64-bit */ 106 PER_RISCOS = 0x000c, 107 PER_SOLARIS = 0x000d | STICKY_TIMEOUTS, 108 PER_UW7 = 0x000e | STICKY_TIMEOUTS | MMAP_PAGE_ZERO, 109 PER_OSF4 = 0x000f, /* OSF/1 v4 */ 110 PER_HPUX = 0x0010, 111 PER_MASK = 0x00ff, 112 }; 113 114 /* 115 * Return the base personality without flags. 116 */ 117 #define personality(pers) (pers & PER_MASK) 118 119 int info_is_fdpic(struct image_info *info) 120 { 121 return info->personality == PER_LINUX_FDPIC; 122 } 123 124 /* this flag is uneffective under linux too, should be deleted */ 125 #ifndef MAP_DENYWRITE 126 #define MAP_DENYWRITE 0 127 #endif 128 129 /* should probably go in elf.h */ 130 #ifndef ELIBBAD 131 #define ELIBBAD 80 132 #endif 133 134 #if TARGET_BIG_ENDIAN 135 #define ELF_DATA ELFDATA2MSB 136 #else 137 #define ELF_DATA ELFDATA2LSB 138 #endif 139 140 #ifdef TARGET_ABI_MIPSN32 141 typedef abi_ullong target_elf_greg_t; 142 #define tswapreg(ptr) tswap64(ptr) 143 #else 144 typedef abi_ulong target_elf_greg_t; 145 #define tswapreg(ptr) tswapal(ptr) 146 #endif 147 148 #ifdef USE_UID16 149 typedef abi_ushort target_uid_t; 150 typedef abi_ushort target_gid_t; 151 #else 152 typedef abi_uint target_uid_t; 153 typedef abi_uint target_gid_t; 154 #endif 155 typedef abi_int target_pid_t; 156 157 #ifdef TARGET_I386 158 159 #define ELF_HWCAP get_elf_hwcap() 160 161 static uint32_t get_elf_hwcap(void) 162 { 163 X86CPU *cpu = X86_CPU(thread_cpu); 164 165 return cpu->env.features[FEAT_1_EDX]; 166 } 167 168 #ifdef TARGET_X86_64 169 #define ELF_CLASS ELFCLASS64 170 #define ELF_ARCH EM_X86_64 171 172 #define ELF_PLATFORM "x86_64" 173 174 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop) 175 { 176 regs->rax = 0; 177 regs->rsp = infop->start_stack; 178 regs->rip = infop->entry; 179 } 180 181 #define ELF_NREG 27 182 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; 183 184 /* 185 * Note that ELF_NREG should be 29 as there should be place for 186 * TRAPNO and ERR "registers" as well but linux doesn't dump 187 * those. 188 * 189 * See linux kernel: arch/x86/include/asm/elf.h 190 */ 191 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env) 192 { 193 (*regs)[0] = tswapreg(env->regs[15]); 194 (*regs)[1] = tswapreg(env->regs[14]); 195 (*regs)[2] = tswapreg(env->regs[13]); 196 (*regs)[3] = tswapreg(env->regs[12]); 197 (*regs)[4] = tswapreg(env->regs[R_EBP]); 198 (*regs)[5] = tswapreg(env->regs[R_EBX]); 199 (*regs)[6] = tswapreg(env->regs[11]); 200 (*regs)[7] = tswapreg(env->regs[10]); 201 (*regs)[8] = tswapreg(env->regs[9]); 202 (*regs)[9] = tswapreg(env->regs[8]); 203 (*regs)[10] = tswapreg(env->regs[R_EAX]); 204 (*regs)[11] = tswapreg(env->regs[R_ECX]); 205 (*regs)[12] = tswapreg(env->regs[R_EDX]); 206 (*regs)[13] = tswapreg(env->regs[R_ESI]); 207 (*regs)[14] = tswapreg(env->regs[R_EDI]); 208 (*regs)[15] = tswapreg(get_task_state(env_cpu_const(env))->orig_ax); 209 (*regs)[16] = tswapreg(env->eip); 210 (*regs)[17] = tswapreg(env->segs[R_CS].selector & 0xffff); 211 (*regs)[18] = tswapreg(env->eflags); 212 (*regs)[19] = tswapreg(env->regs[R_ESP]); 213 (*regs)[20] = tswapreg(env->segs[R_SS].selector & 0xffff); 214 (*regs)[21] = tswapreg(env->segs[R_FS].selector & 0xffff); 215 (*regs)[22] = tswapreg(env->segs[R_GS].selector & 0xffff); 216 (*regs)[23] = tswapreg(env->segs[R_DS].selector & 0xffff); 217 (*regs)[24] = tswapreg(env->segs[R_ES].selector & 0xffff); 218 (*regs)[25] = tswapreg(env->segs[R_FS].selector & 0xffff); 219 (*regs)[26] = tswapreg(env->segs[R_GS].selector & 0xffff); 220 } 221 222 #if ULONG_MAX > UINT32_MAX 223 #define INIT_GUEST_COMMPAGE 224 static bool init_guest_commpage(void) 225 { 226 /* 227 * The vsyscall page is at a high negative address aka kernel space, 228 * which means that we cannot actually allocate it with target_mmap. 229 * We still should be able to use page_set_flags, unless the user 230 * has specified -R reserved_va, which would trigger an assert(). 231 */ 232 if (reserved_va != 0 && 233 TARGET_VSYSCALL_PAGE + TARGET_PAGE_SIZE - 1 > reserved_va) { 234 error_report("Cannot allocate vsyscall page"); 235 exit(EXIT_FAILURE); 236 } 237 page_set_flags(TARGET_VSYSCALL_PAGE, 238 TARGET_VSYSCALL_PAGE | ~TARGET_PAGE_MASK, 239 PAGE_EXEC | PAGE_VALID); 240 return true; 241 } 242 #endif 243 #else 244 245 /* 246 * This is used to ensure we don't load something for the wrong architecture. 247 */ 248 #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) ) 249 250 /* 251 * These are used to set parameters in the core dumps. 252 */ 253 #define ELF_CLASS ELFCLASS32 254 #define ELF_ARCH EM_386 255 256 #define ELF_PLATFORM get_elf_platform() 257 #define EXSTACK_DEFAULT true 258 259 static const char *get_elf_platform(void) 260 { 261 static char elf_platform[] = "i386"; 262 int family = object_property_get_int(OBJECT(thread_cpu), "family", NULL); 263 if (family > 6) { 264 family = 6; 265 } 266 if (family >= 3) { 267 elf_platform[1] = '0' + family; 268 } 269 return elf_platform; 270 } 271 272 static inline void init_thread(struct target_pt_regs *regs, 273 struct image_info *infop) 274 { 275 regs->esp = infop->start_stack; 276 regs->eip = infop->entry; 277 278 /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program 279 starts %edx contains a pointer to a function which might be 280 registered using `atexit'. This provides a mean for the 281 dynamic linker to call DT_FINI functions for shared libraries 282 that have been loaded before the code runs. 283 284 A value of 0 tells we have no such handler. */ 285 regs->edx = 0; 286 } 287 288 #define ELF_NREG 17 289 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; 290 291 /* 292 * Note that ELF_NREG should be 19 as there should be place for 293 * TRAPNO and ERR "registers" as well but linux doesn't dump 294 * those. 295 * 296 * See linux kernel: arch/x86/include/asm/elf.h 297 */ 298 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env) 299 { 300 (*regs)[0] = tswapreg(env->regs[R_EBX]); 301 (*regs)[1] = tswapreg(env->regs[R_ECX]); 302 (*regs)[2] = tswapreg(env->regs[R_EDX]); 303 (*regs)[3] = tswapreg(env->regs[R_ESI]); 304 (*regs)[4] = tswapreg(env->regs[R_EDI]); 305 (*regs)[5] = tswapreg(env->regs[R_EBP]); 306 (*regs)[6] = tswapreg(env->regs[R_EAX]); 307 (*regs)[7] = tswapreg(env->segs[R_DS].selector & 0xffff); 308 (*regs)[8] = tswapreg(env->segs[R_ES].selector & 0xffff); 309 (*regs)[9] = tswapreg(env->segs[R_FS].selector & 0xffff); 310 (*regs)[10] = tswapreg(env->segs[R_GS].selector & 0xffff); 311 (*regs)[11] = tswapreg(get_task_state(env_cpu_const(env))->orig_ax); 312 (*regs)[12] = tswapreg(env->eip); 313 (*regs)[13] = tswapreg(env->segs[R_CS].selector & 0xffff); 314 (*regs)[14] = tswapreg(env->eflags); 315 (*regs)[15] = tswapreg(env->regs[R_ESP]); 316 (*regs)[16] = tswapreg(env->segs[R_SS].selector & 0xffff); 317 } 318 319 /* 320 * i386 is the only target which supplies AT_SYSINFO for the vdso. 321 * All others only supply AT_SYSINFO_EHDR. 322 */ 323 #define DLINFO_ARCH_ITEMS (vdso_info != NULL) 324 #define ARCH_DLINFO \ 325 do { \ 326 if (vdso_info) { \ 327 NEW_AUX_ENT(AT_SYSINFO, vdso_info->entry); \ 328 } \ 329 } while (0) 330 331 #endif /* TARGET_X86_64 */ 332 333 #define VDSO_HEADER "vdso.c.inc" 334 335 #define USE_ELF_CORE_DUMP 336 #define ELF_EXEC_PAGESIZE 4096 337 338 #endif /* TARGET_I386 */ 339 340 #ifdef TARGET_ARM 341 342 #ifndef TARGET_AARCH64 343 /* 32 bit ARM definitions */ 344 345 #define ELF_ARCH EM_ARM 346 #define ELF_CLASS ELFCLASS32 347 #define EXSTACK_DEFAULT true 348 349 static inline void init_thread(struct target_pt_regs *regs, 350 struct image_info *infop) 351 { 352 abi_long stack = infop->start_stack; 353 memset(regs, 0, sizeof(*regs)); 354 355 regs->uregs[16] = ARM_CPU_MODE_USR; 356 if (infop->entry & 1) { 357 regs->uregs[16] |= CPSR_T; 358 } 359 regs->uregs[15] = infop->entry & 0xfffffffe; 360 regs->uregs[13] = infop->start_stack; 361 /* FIXME - what to for failure of get_user()? */ 362 get_user_ual(regs->uregs[2], stack + 8); /* envp */ 363 get_user_ual(regs->uregs[1], stack + 4); /* envp */ 364 /* XXX: it seems that r0 is zeroed after ! */ 365 regs->uregs[0] = 0; 366 /* For uClinux PIC binaries. */ 367 /* XXX: Linux does this only on ARM with no MMU (do we care ?) */ 368 regs->uregs[10] = infop->start_data; 369 370 /* Support ARM FDPIC. */ 371 if (info_is_fdpic(infop)) { 372 /* As described in the ABI document, r7 points to the loadmap info 373 * prepared by the kernel. If an interpreter is needed, r8 points 374 * to the interpreter loadmap and r9 points to the interpreter 375 * PT_DYNAMIC info. If no interpreter is needed, r8 is zero, and 376 * r9 points to the main program PT_DYNAMIC info. 377 */ 378 regs->uregs[7] = infop->loadmap_addr; 379 if (infop->interpreter_loadmap_addr) { 380 /* Executable is dynamically loaded. */ 381 regs->uregs[8] = infop->interpreter_loadmap_addr; 382 regs->uregs[9] = infop->interpreter_pt_dynamic_addr; 383 } else { 384 regs->uregs[8] = 0; 385 regs->uregs[9] = infop->pt_dynamic_addr; 386 } 387 } 388 } 389 390 #define ELF_NREG 18 391 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; 392 393 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUARMState *env) 394 { 395 (*regs)[0] = tswapreg(env->regs[0]); 396 (*regs)[1] = tswapreg(env->regs[1]); 397 (*regs)[2] = tswapreg(env->regs[2]); 398 (*regs)[3] = tswapreg(env->regs[3]); 399 (*regs)[4] = tswapreg(env->regs[4]); 400 (*regs)[5] = tswapreg(env->regs[5]); 401 (*regs)[6] = tswapreg(env->regs[6]); 402 (*regs)[7] = tswapreg(env->regs[7]); 403 (*regs)[8] = tswapreg(env->regs[8]); 404 (*regs)[9] = tswapreg(env->regs[9]); 405 (*regs)[10] = tswapreg(env->regs[10]); 406 (*regs)[11] = tswapreg(env->regs[11]); 407 (*regs)[12] = tswapreg(env->regs[12]); 408 (*regs)[13] = tswapreg(env->regs[13]); 409 (*regs)[14] = tswapreg(env->regs[14]); 410 (*regs)[15] = tswapreg(env->regs[15]); 411 412 (*regs)[16] = tswapreg(cpsr_read((CPUARMState *)env)); 413 (*regs)[17] = tswapreg(env->regs[0]); /* XXX */ 414 } 415 416 #define USE_ELF_CORE_DUMP 417 #define ELF_EXEC_PAGESIZE 4096 418 419 enum 420 { 421 ARM_HWCAP_ARM_SWP = 1 << 0, 422 ARM_HWCAP_ARM_HALF = 1 << 1, 423 ARM_HWCAP_ARM_THUMB = 1 << 2, 424 ARM_HWCAP_ARM_26BIT = 1 << 3, 425 ARM_HWCAP_ARM_FAST_MULT = 1 << 4, 426 ARM_HWCAP_ARM_FPA = 1 << 5, 427 ARM_HWCAP_ARM_VFP = 1 << 6, 428 ARM_HWCAP_ARM_EDSP = 1 << 7, 429 ARM_HWCAP_ARM_JAVA = 1 << 8, 430 ARM_HWCAP_ARM_IWMMXT = 1 << 9, 431 ARM_HWCAP_ARM_CRUNCH = 1 << 10, 432 ARM_HWCAP_ARM_THUMBEE = 1 << 11, 433 ARM_HWCAP_ARM_NEON = 1 << 12, 434 ARM_HWCAP_ARM_VFPv3 = 1 << 13, 435 ARM_HWCAP_ARM_VFPv3D16 = 1 << 14, 436 ARM_HWCAP_ARM_TLS = 1 << 15, 437 ARM_HWCAP_ARM_VFPv4 = 1 << 16, 438 ARM_HWCAP_ARM_IDIVA = 1 << 17, 439 ARM_HWCAP_ARM_IDIVT = 1 << 18, 440 ARM_HWCAP_ARM_VFPD32 = 1 << 19, 441 ARM_HWCAP_ARM_LPAE = 1 << 20, 442 ARM_HWCAP_ARM_EVTSTRM = 1 << 21, 443 ARM_HWCAP_ARM_FPHP = 1 << 22, 444 ARM_HWCAP_ARM_ASIMDHP = 1 << 23, 445 ARM_HWCAP_ARM_ASIMDDP = 1 << 24, 446 ARM_HWCAP_ARM_ASIMDFHM = 1 << 25, 447 ARM_HWCAP_ARM_ASIMDBF16 = 1 << 26, 448 ARM_HWCAP_ARM_I8MM = 1 << 27, 449 }; 450 451 enum { 452 ARM_HWCAP2_ARM_AES = 1 << 0, 453 ARM_HWCAP2_ARM_PMULL = 1 << 1, 454 ARM_HWCAP2_ARM_SHA1 = 1 << 2, 455 ARM_HWCAP2_ARM_SHA2 = 1 << 3, 456 ARM_HWCAP2_ARM_CRC32 = 1 << 4, 457 ARM_HWCAP2_ARM_SB = 1 << 5, 458 ARM_HWCAP2_ARM_SSBS = 1 << 6, 459 }; 460 461 /* The commpage only exists for 32 bit kernels */ 462 463 #define HI_COMMPAGE (intptr_t)0xffff0f00u 464 465 static bool init_guest_commpage(void) 466 { 467 ARMCPU *cpu = ARM_CPU(thread_cpu); 468 int host_page_size = qemu_real_host_page_size(); 469 abi_ptr commpage; 470 void *want; 471 void *addr; 472 473 /* 474 * M-profile allocates maximum of 2GB address space, so can never 475 * allocate the commpage. Skip it. 476 */ 477 if (arm_feature(&cpu->env, ARM_FEATURE_M)) { 478 return true; 479 } 480 481 commpage = HI_COMMPAGE & -host_page_size; 482 want = g2h_untagged(commpage); 483 addr = mmap(want, host_page_size, PROT_READ | PROT_WRITE, 484 MAP_ANONYMOUS | MAP_PRIVATE | 485 (commpage < reserved_va ? MAP_FIXED : MAP_FIXED_NOREPLACE), 486 -1, 0); 487 488 if (addr == MAP_FAILED) { 489 perror("Allocating guest commpage"); 490 exit(EXIT_FAILURE); 491 } 492 if (addr != want) { 493 return false; 494 } 495 496 /* Set kernel helper versions; rest of page is 0. */ 497 __put_user(5, (uint32_t *)g2h_untagged(0xffff0ffcu)); 498 499 if (mprotect(addr, host_page_size, PROT_READ)) { 500 perror("Protecting guest commpage"); 501 exit(EXIT_FAILURE); 502 } 503 504 page_set_flags(commpage, commpage | (host_page_size - 1), 505 PAGE_READ | PAGE_EXEC | PAGE_VALID); 506 return true; 507 } 508 509 #define ELF_HWCAP get_elf_hwcap() 510 #define ELF_HWCAP2 get_elf_hwcap2() 511 512 uint32_t get_elf_hwcap(void) 513 { 514 ARMCPU *cpu = ARM_CPU(thread_cpu); 515 uint32_t hwcaps = 0; 516 517 hwcaps |= ARM_HWCAP_ARM_SWP; 518 hwcaps |= ARM_HWCAP_ARM_HALF; 519 hwcaps |= ARM_HWCAP_ARM_THUMB; 520 hwcaps |= ARM_HWCAP_ARM_FAST_MULT; 521 522 /* probe for the extra features */ 523 #define GET_FEATURE(feat, hwcap) \ 524 do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0) 525 526 #define GET_FEATURE_ID(feat, hwcap) \ 527 do { if (cpu_isar_feature(feat, cpu)) { hwcaps |= hwcap; } } while (0) 528 529 /* EDSP is in v5TE and above, but all our v5 CPUs are v5TE */ 530 GET_FEATURE(ARM_FEATURE_V5, ARM_HWCAP_ARM_EDSP); 531 GET_FEATURE(ARM_FEATURE_IWMMXT, ARM_HWCAP_ARM_IWMMXT); 532 GET_FEATURE(ARM_FEATURE_THUMB2EE, ARM_HWCAP_ARM_THUMBEE); 533 GET_FEATURE(ARM_FEATURE_NEON, ARM_HWCAP_ARM_NEON); 534 GET_FEATURE(ARM_FEATURE_V6K, ARM_HWCAP_ARM_TLS); 535 GET_FEATURE(ARM_FEATURE_LPAE, ARM_HWCAP_ARM_LPAE); 536 GET_FEATURE_ID(aa32_arm_div, ARM_HWCAP_ARM_IDIVA); 537 GET_FEATURE_ID(aa32_thumb_div, ARM_HWCAP_ARM_IDIVT); 538 GET_FEATURE_ID(aa32_vfp, ARM_HWCAP_ARM_VFP); 539 540 if (cpu_isar_feature(aa32_fpsp_v3, cpu) || 541 cpu_isar_feature(aa32_fpdp_v3, cpu)) { 542 hwcaps |= ARM_HWCAP_ARM_VFPv3; 543 if (cpu_isar_feature(aa32_simd_r32, cpu)) { 544 hwcaps |= ARM_HWCAP_ARM_VFPD32; 545 } else { 546 hwcaps |= ARM_HWCAP_ARM_VFPv3D16; 547 } 548 } 549 GET_FEATURE_ID(aa32_simdfmac, ARM_HWCAP_ARM_VFPv4); 550 /* 551 * MVFR1.FPHP and .SIMDHP must be in sync, and QEMU uses the same 552 * isar_feature function for both. The kernel reports them as two hwcaps. 553 */ 554 GET_FEATURE_ID(aa32_fp16_arith, ARM_HWCAP_ARM_FPHP); 555 GET_FEATURE_ID(aa32_fp16_arith, ARM_HWCAP_ARM_ASIMDHP); 556 GET_FEATURE_ID(aa32_dp, ARM_HWCAP_ARM_ASIMDDP); 557 GET_FEATURE_ID(aa32_fhm, ARM_HWCAP_ARM_ASIMDFHM); 558 GET_FEATURE_ID(aa32_bf16, ARM_HWCAP_ARM_ASIMDBF16); 559 GET_FEATURE_ID(aa32_i8mm, ARM_HWCAP_ARM_I8MM); 560 561 return hwcaps; 562 } 563 564 uint64_t get_elf_hwcap2(void) 565 { 566 ARMCPU *cpu = ARM_CPU(thread_cpu); 567 uint64_t hwcaps = 0; 568 569 GET_FEATURE_ID(aa32_aes, ARM_HWCAP2_ARM_AES); 570 GET_FEATURE_ID(aa32_pmull, ARM_HWCAP2_ARM_PMULL); 571 GET_FEATURE_ID(aa32_sha1, ARM_HWCAP2_ARM_SHA1); 572 GET_FEATURE_ID(aa32_sha2, ARM_HWCAP2_ARM_SHA2); 573 GET_FEATURE_ID(aa32_crc32, ARM_HWCAP2_ARM_CRC32); 574 GET_FEATURE_ID(aa32_sb, ARM_HWCAP2_ARM_SB); 575 GET_FEATURE_ID(aa32_ssbs, ARM_HWCAP2_ARM_SSBS); 576 return hwcaps; 577 } 578 579 const char *elf_hwcap_str(uint32_t bit) 580 { 581 static const char *hwcap_str[] = { 582 [__builtin_ctz(ARM_HWCAP_ARM_SWP )] = "swp", 583 [__builtin_ctz(ARM_HWCAP_ARM_HALF )] = "half", 584 [__builtin_ctz(ARM_HWCAP_ARM_THUMB )] = "thumb", 585 [__builtin_ctz(ARM_HWCAP_ARM_26BIT )] = "26bit", 586 [__builtin_ctz(ARM_HWCAP_ARM_FAST_MULT)] = "fast_mult", 587 [__builtin_ctz(ARM_HWCAP_ARM_FPA )] = "fpa", 588 [__builtin_ctz(ARM_HWCAP_ARM_VFP )] = "vfp", 589 [__builtin_ctz(ARM_HWCAP_ARM_EDSP )] = "edsp", 590 [__builtin_ctz(ARM_HWCAP_ARM_JAVA )] = "java", 591 [__builtin_ctz(ARM_HWCAP_ARM_IWMMXT )] = "iwmmxt", 592 [__builtin_ctz(ARM_HWCAP_ARM_CRUNCH )] = "crunch", 593 [__builtin_ctz(ARM_HWCAP_ARM_THUMBEE )] = "thumbee", 594 [__builtin_ctz(ARM_HWCAP_ARM_NEON )] = "neon", 595 [__builtin_ctz(ARM_HWCAP_ARM_VFPv3 )] = "vfpv3", 596 [__builtin_ctz(ARM_HWCAP_ARM_VFPv3D16 )] = "vfpv3d16", 597 [__builtin_ctz(ARM_HWCAP_ARM_TLS )] = "tls", 598 [__builtin_ctz(ARM_HWCAP_ARM_VFPv4 )] = "vfpv4", 599 [__builtin_ctz(ARM_HWCAP_ARM_IDIVA )] = "idiva", 600 [__builtin_ctz(ARM_HWCAP_ARM_IDIVT )] = "idivt", 601 [__builtin_ctz(ARM_HWCAP_ARM_VFPD32 )] = "vfpd32", 602 [__builtin_ctz(ARM_HWCAP_ARM_LPAE )] = "lpae", 603 [__builtin_ctz(ARM_HWCAP_ARM_EVTSTRM )] = "evtstrm", 604 [__builtin_ctz(ARM_HWCAP_ARM_FPHP )] = "fphp", 605 [__builtin_ctz(ARM_HWCAP_ARM_ASIMDHP )] = "asimdhp", 606 [__builtin_ctz(ARM_HWCAP_ARM_ASIMDDP )] = "asimddp", 607 [__builtin_ctz(ARM_HWCAP_ARM_ASIMDFHM )] = "asimdfhm", 608 [__builtin_ctz(ARM_HWCAP_ARM_ASIMDBF16)] = "asimdbf16", 609 [__builtin_ctz(ARM_HWCAP_ARM_I8MM )] = "i8mm", 610 }; 611 612 return bit < ARRAY_SIZE(hwcap_str) ? hwcap_str[bit] : NULL; 613 } 614 615 const char *elf_hwcap2_str(uint32_t bit) 616 { 617 static const char *hwcap_str[] = { 618 [__builtin_ctz(ARM_HWCAP2_ARM_AES )] = "aes", 619 [__builtin_ctz(ARM_HWCAP2_ARM_PMULL)] = "pmull", 620 [__builtin_ctz(ARM_HWCAP2_ARM_SHA1 )] = "sha1", 621 [__builtin_ctz(ARM_HWCAP2_ARM_SHA2 )] = "sha2", 622 [__builtin_ctz(ARM_HWCAP2_ARM_CRC32)] = "crc32", 623 [__builtin_ctz(ARM_HWCAP2_ARM_SB )] = "sb", 624 [__builtin_ctz(ARM_HWCAP2_ARM_SSBS )] = "ssbs", 625 }; 626 627 return bit < ARRAY_SIZE(hwcap_str) ? hwcap_str[bit] : NULL; 628 } 629 630 #undef GET_FEATURE 631 #undef GET_FEATURE_ID 632 633 #define ELF_PLATFORM get_elf_platform() 634 635 static const char *get_elf_platform(void) 636 { 637 CPUARMState *env = cpu_env(thread_cpu); 638 639 #if TARGET_BIG_ENDIAN 640 # define END "b" 641 #else 642 # define END "l" 643 #endif 644 645 if (arm_feature(env, ARM_FEATURE_V8)) { 646 return "v8" END; 647 } else if (arm_feature(env, ARM_FEATURE_V7)) { 648 if (arm_feature(env, ARM_FEATURE_M)) { 649 return "v7m" END; 650 } else { 651 return "v7" END; 652 } 653 } else if (arm_feature(env, ARM_FEATURE_V6)) { 654 return "v6" END; 655 } else if (arm_feature(env, ARM_FEATURE_V5)) { 656 return "v5" END; 657 } else { 658 return "v4" END; 659 } 660 661 #undef END 662 } 663 664 #if TARGET_BIG_ENDIAN 665 #include "elf.h" 666 #include "vdso-be8.c.inc" 667 #include "vdso-be32.c.inc" 668 669 static const VdsoImageInfo *vdso_image_info(uint32_t elf_flags) 670 { 671 return (EF_ARM_EABI_VERSION(elf_flags) >= EF_ARM_EABI_VER4 672 && (elf_flags & EF_ARM_BE8) 673 ? &vdso_be8_image_info 674 : &vdso_be32_image_info); 675 } 676 #define vdso_image_info vdso_image_info 677 #else 678 # define VDSO_HEADER "vdso-le.c.inc" 679 #endif 680 681 #else 682 /* 64 bit ARM definitions */ 683 684 #define ELF_ARCH EM_AARCH64 685 #define ELF_CLASS ELFCLASS64 686 #if TARGET_BIG_ENDIAN 687 # define ELF_PLATFORM "aarch64_be" 688 #else 689 # define ELF_PLATFORM "aarch64" 690 #endif 691 692 static inline void init_thread(struct target_pt_regs *regs, 693 struct image_info *infop) 694 { 695 abi_long stack = infop->start_stack; 696 memset(regs, 0, sizeof(*regs)); 697 698 regs->pc = infop->entry & ~0x3ULL; 699 regs->sp = stack; 700 } 701 702 #define ELF_NREG 34 703 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; 704 705 static void elf_core_copy_regs(target_elf_gregset_t *regs, 706 const CPUARMState *env) 707 { 708 int i; 709 710 for (i = 0; i < 32; i++) { 711 (*regs)[i] = tswapreg(env->xregs[i]); 712 } 713 (*regs)[32] = tswapreg(env->pc); 714 (*regs)[33] = tswapreg(pstate_read((CPUARMState *)env)); 715 } 716 717 #define USE_ELF_CORE_DUMP 718 #define ELF_EXEC_PAGESIZE 4096 719 720 enum { 721 ARM_HWCAP_A64_FP = 1 << 0, 722 ARM_HWCAP_A64_ASIMD = 1 << 1, 723 ARM_HWCAP_A64_EVTSTRM = 1 << 2, 724 ARM_HWCAP_A64_AES = 1 << 3, 725 ARM_HWCAP_A64_PMULL = 1 << 4, 726 ARM_HWCAP_A64_SHA1 = 1 << 5, 727 ARM_HWCAP_A64_SHA2 = 1 << 6, 728 ARM_HWCAP_A64_CRC32 = 1 << 7, 729 ARM_HWCAP_A64_ATOMICS = 1 << 8, 730 ARM_HWCAP_A64_FPHP = 1 << 9, 731 ARM_HWCAP_A64_ASIMDHP = 1 << 10, 732 ARM_HWCAP_A64_CPUID = 1 << 11, 733 ARM_HWCAP_A64_ASIMDRDM = 1 << 12, 734 ARM_HWCAP_A64_JSCVT = 1 << 13, 735 ARM_HWCAP_A64_FCMA = 1 << 14, 736 ARM_HWCAP_A64_LRCPC = 1 << 15, 737 ARM_HWCAP_A64_DCPOP = 1 << 16, 738 ARM_HWCAP_A64_SHA3 = 1 << 17, 739 ARM_HWCAP_A64_SM3 = 1 << 18, 740 ARM_HWCAP_A64_SM4 = 1 << 19, 741 ARM_HWCAP_A64_ASIMDDP = 1 << 20, 742 ARM_HWCAP_A64_SHA512 = 1 << 21, 743 ARM_HWCAP_A64_SVE = 1 << 22, 744 ARM_HWCAP_A64_ASIMDFHM = 1 << 23, 745 ARM_HWCAP_A64_DIT = 1 << 24, 746 ARM_HWCAP_A64_USCAT = 1 << 25, 747 ARM_HWCAP_A64_ILRCPC = 1 << 26, 748 ARM_HWCAP_A64_FLAGM = 1 << 27, 749 ARM_HWCAP_A64_SSBS = 1 << 28, 750 ARM_HWCAP_A64_SB = 1 << 29, 751 ARM_HWCAP_A64_PACA = 1 << 30, 752 ARM_HWCAP_A64_PACG = 1UL << 31, 753 754 ARM_HWCAP2_A64_DCPODP = 1 << 0, 755 ARM_HWCAP2_A64_SVE2 = 1 << 1, 756 ARM_HWCAP2_A64_SVEAES = 1 << 2, 757 ARM_HWCAP2_A64_SVEPMULL = 1 << 3, 758 ARM_HWCAP2_A64_SVEBITPERM = 1 << 4, 759 ARM_HWCAP2_A64_SVESHA3 = 1 << 5, 760 ARM_HWCAP2_A64_SVESM4 = 1 << 6, 761 ARM_HWCAP2_A64_FLAGM2 = 1 << 7, 762 ARM_HWCAP2_A64_FRINT = 1 << 8, 763 ARM_HWCAP2_A64_SVEI8MM = 1 << 9, 764 ARM_HWCAP2_A64_SVEF32MM = 1 << 10, 765 ARM_HWCAP2_A64_SVEF64MM = 1 << 11, 766 ARM_HWCAP2_A64_SVEBF16 = 1 << 12, 767 ARM_HWCAP2_A64_I8MM = 1 << 13, 768 ARM_HWCAP2_A64_BF16 = 1 << 14, 769 ARM_HWCAP2_A64_DGH = 1 << 15, 770 ARM_HWCAP2_A64_RNG = 1 << 16, 771 ARM_HWCAP2_A64_BTI = 1 << 17, 772 ARM_HWCAP2_A64_MTE = 1 << 18, 773 ARM_HWCAP2_A64_ECV = 1 << 19, 774 ARM_HWCAP2_A64_AFP = 1 << 20, 775 ARM_HWCAP2_A64_RPRES = 1 << 21, 776 ARM_HWCAP2_A64_MTE3 = 1 << 22, 777 ARM_HWCAP2_A64_SME = 1 << 23, 778 ARM_HWCAP2_A64_SME_I16I64 = 1 << 24, 779 ARM_HWCAP2_A64_SME_F64F64 = 1 << 25, 780 ARM_HWCAP2_A64_SME_I8I32 = 1 << 26, 781 ARM_HWCAP2_A64_SME_F16F32 = 1 << 27, 782 ARM_HWCAP2_A64_SME_B16F32 = 1 << 28, 783 ARM_HWCAP2_A64_SME_F32F32 = 1 << 29, 784 ARM_HWCAP2_A64_SME_FA64 = 1 << 30, 785 ARM_HWCAP2_A64_WFXT = 1ULL << 31, 786 ARM_HWCAP2_A64_EBF16 = 1ULL << 32, 787 ARM_HWCAP2_A64_SVE_EBF16 = 1ULL << 33, 788 ARM_HWCAP2_A64_CSSC = 1ULL << 34, 789 ARM_HWCAP2_A64_RPRFM = 1ULL << 35, 790 ARM_HWCAP2_A64_SVE2P1 = 1ULL << 36, 791 ARM_HWCAP2_A64_SME2 = 1ULL << 37, 792 ARM_HWCAP2_A64_SME2P1 = 1ULL << 38, 793 ARM_HWCAP2_A64_SME_I16I32 = 1ULL << 39, 794 ARM_HWCAP2_A64_SME_BI32I32 = 1ULL << 40, 795 ARM_HWCAP2_A64_SME_B16B16 = 1ULL << 41, 796 ARM_HWCAP2_A64_SME_F16F16 = 1ULL << 42, 797 ARM_HWCAP2_A64_MOPS = 1ULL << 43, 798 ARM_HWCAP2_A64_HBC = 1ULL << 44, 799 }; 800 801 #define ELF_HWCAP get_elf_hwcap() 802 #define ELF_HWCAP2 get_elf_hwcap2() 803 804 #define GET_FEATURE_ID(feat, hwcap) \ 805 do { if (cpu_isar_feature(feat, cpu)) { hwcaps |= hwcap; } } while (0) 806 807 uint32_t get_elf_hwcap(void) 808 { 809 ARMCPU *cpu = ARM_CPU(thread_cpu); 810 uint32_t hwcaps = 0; 811 812 hwcaps |= ARM_HWCAP_A64_FP; 813 hwcaps |= ARM_HWCAP_A64_ASIMD; 814 hwcaps |= ARM_HWCAP_A64_CPUID; 815 816 /* probe for the extra features */ 817 818 GET_FEATURE_ID(aa64_aes, ARM_HWCAP_A64_AES); 819 GET_FEATURE_ID(aa64_pmull, ARM_HWCAP_A64_PMULL); 820 GET_FEATURE_ID(aa64_sha1, ARM_HWCAP_A64_SHA1); 821 GET_FEATURE_ID(aa64_sha256, ARM_HWCAP_A64_SHA2); 822 GET_FEATURE_ID(aa64_sha512, ARM_HWCAP_A64_SHA512); 823 GET_FEATURE_ID(aa64_crc32, ARM_HWCAP_A64_CRC32); 824 GET_FEATURE_ID(aa64_sha3, ARM_HWCAP_A64_SHA3); 825 GET_FEATURE_ID(aa64_sm3, ARM_HWCAP_A64_SM3); 826 GET_FEATURE_ID(aa64_sm4, ARM_HWCAP_A64_SM4); 827 GET_FEATURE_ID(aa64_fp16, ARM_HWCAP_A64_FPHP | ARM_HWCAP_A64_ASIMDHP); 828 GET_FEATURE_ID(aa64_atomics, ARM_HWCAP_A64_ATOMICS); 829 GET_FEATURE_ID(aa64_lse2, ARM_HWCAP_A64_USCAT); 830 GET_FEATURE_ID(aa64_rdm, ARM_HWCAP_A64_ASIMDRDM); 831 GET_FEATURE_ID(aa64_dp, ARM_HWCAP_A64_ASIMDDP); 832 GET_FEATURE_ID(aa64_fcma, ARM_HWCAP_A64_FCMA); 833 GET_FEATURE_ID(aa64_sve, ARM_HWCAP_A64_SVE); 834 GET_FEATURE_ID(aa64_pauth, ARM_HWCAP_A64_PACA | ARM_HWCAP_A64_PACG); 835 GET_FEATURE_ID(aa64_fhm, ARM_HWCAP_A64_ASIMDFHM); 836 GET_FEATURE_ID(aa64_dit, ARM_HWCAP_A64_DIT); 837 GET_FEATURE_ID(aa64_jscvt, ARM_HWCAP_A64_JSCVT); 838 GET_FEATURE_ID(aa64_sb, ARM_HWCAP_A64_SB); 839 GET_FEATURE_ID(aa64_condm_4, ARM_HWCAP_A64_FLAGM); 840 GET_FEATURE_ID(aa64_dcpop, ARM_HWCAP_A64_DCPOP); 841 GET_FEATURE_ID(aa64_rcpc_8_3, ARM_HWCAP_A64_LRCPC); 842 GET_FEATURE_ID(aa64_rcpc_8_4, ARM_HWCAP_A64_ILRCPC); 843 844 return hwcaps; 845 } 846 847 uint64_t get_elf_hwcap2(void) 848 { 849 ARMCPU *cpu = ARM_CPU(thread_cpu); 850 uint64_t hwcaps = 0; 851 852 GET_FEATURE_ID(aa64_dcpodp, ARM_HWCAP2_A64_DCPODP); 853 GET_FEATURE_ID(aa64_sve2, ARM_HWCAP2_A64_SVE2); 854 GET_FEATURE_ID(aa64_sve2_aes, ARM_HWCAP2_A64_SVEAES); 855 GET_FEATURE_ID(aa64_sve2_pmull128, ARM_HWCAP2_A64_SVEPMULL); 856 GET_FEATURE_ID(aa64_sve2_bitperm, ARM_HWCAP2_A64_SVEBITPERM); 857 GET_FEATURE_ID(aa64_sve2_sha3, ARM_HWCAP2_A64_SVESHA3); 858 GET_FEATURE_ID(aa64_sve2_sm4, ARM_HWCAP2_A64_SVESM4); 859 GET_FEATURE_ID(aa64_condm_5, ARM_HWCAP2_A64_FLAGM2); 860 GET_FEATURE_ID(aa64_frint, ARM_HWCAP2_A64_FRINT); 861 GET_FEATURE_ID(aa64_sve_i8mm, ARM_HWCAP2_A64_SVEI8MM); 862 GET_FEATURE_ID(aa64_sve_f32mm, ARM_HWCAP2_A64_SVEF32MM); 863 GET_FEATURE_ID(aa64_sve_f64mm, ARM_HWCAP2_A64_SVEF64MM); 864 GET_FEATURE_ID(aa64_sve_bf16, ARM_HWCAP2_A64_SVEBF16); 865 GET_FEATURE_ID(aa64_i8mm, ARM_HWCAP2_A64_I8MM); 866 GET_FEATURE_ID(aa64_bf16, ARM_HWCAP2_A64_BF16); 867 GET_FEATURE_ID(aa64_rndr, ARM_HWCAP2_A64_RNG); 868 GET_FEATURE_ID(aa64_bti, ARM_HWCAP2_A64_BTI); 869 GET_FEATURE_ID(aa64_mte, ARM_HWCAP2_A64_MTE); 870 GET_FEATURE_ID(aa64_mte3, ARM_HWCAP2_A64_MTE3); 871 GET_FEATURE_ID(aa64_sme, (ARM_HWCAP2_A64_SME | 872 ARM_HWCAP2_A64_SME_F32F32 | 873 ARM_HWCAP2_A64_SME_B16F32 | 874 ARM_HWCAP2_A64_SME_F16F32 | 875 ARM_HWCAP2_A64_SME_I8I32)); 876 GET_FEATURE_ID(aa64_sme_f64f64, ARM_HWCAP2_A64_SME_F64F64); 877 GET_FEATURE_ID(aa64_sme_i16i64, ARM_HWCAP2_A64_SME_I16I64); 878 GET_FEATURE_ID(aa64_sme_fa64, ARM_HWCAP2_A64_SME_FA64); 879 GET_FEATURE_ID(aa64_hbc, ARM_HWCAP2_A64_HBC); 880 GET_FEATURE_ID(aa64_mops, ARM_HWCAP2_A64_MOPS); 881 882 return hwcaps; 883 } 884 885 const char *elf_hwcap_str(uint32_t bit) 886 { 887 static const char *hwcap_str[] = { 888 [__builtin_ctz(ARM_HWCAP_A64_FP )] = "fp", 889 [__builtin_ctz(ARM_HWCAP_A64_ASIMD )] = "asimd", 890 [__builtin_ctz(ARM_HWCAP_A64_EVTSTRM )] = "evtstrm", 891 [__builtin_ctz(ARM_HWCAP_A64_AES )] = "aes", 892 [__builtin_ctz(ARM_HWCAP_A64_PMULL )] = "pmull", 893 [__builtin_ctz(ARM_HWCAP_A64_SHA1 )] = "sha1", 894 [__builtin_ctz(ARM_HWCAP_A64_SHA2 )] = "sha2", 895 [__builtin_ctz(ARM_HWCAP_A64_CRC32 )] = "crc32", 896 [__builtin_ctz(ARM_HWCAP_A64_ATOMICS )] = "atomics", 897 [__builtin_ctz(ARM_HWCAP_A64_FPHP )] = "fphp", 898 [__builtin_ctz(ARM_HWCAP_A64_ASIMDHP )] = "asimdhp", 899 [__builtin_ctz(ARM_HWCAP_A64_CPUID )] = "cpuid", 900 [__builtin_ctz(ARM_HWCAP_A64_ASIMDRDM)] = "asimdrdm", 901 [__builtin_ctz(ARM_HWCAP_A64_JSCVT )] = "jscvt", 902 [__builtin_ctz(ARM_HWCAP_A64_FCMA )] = "fcma", 903 [__builtin_ctz(ARM_HWCAP_A64_LRCPC )] = "lrcpc", 904 [__builtin_ctz(ARM_HWCAP_A64_DCPOP )] = "dcpop", 905 [__builtin_ctz(ARM_HWCAP_A64_SHA3 )] = "sha3", 906 [__builtin_ctz(ARM_HWCAP_A64_SM3 )] = "sm3", 907 [__builtin_ctz(ARM_HWCAP_A64_SM4 )] = "sm4", 908 [__builtin_ctz(ARM_HWCAP_A64_ASIMDDP )] = "asimddp", 909 [__builtin_ctz(ARM_HWCAP_A64_SHA512 )] = "sha512", 910 [__builtin_ctz(ARM_HWCAP_A64_SVE )] = "sve", 911 [__builtin_ctz(ARM_HWCAP_A64_ASIMDFHM)] = "asimdfhm", 912 [__builtin_ctz(ARM_HWCAP_A64_DIT )] = "dit", 913 [__builtin_ctz(ARM_HWCAP_A64_USCAT )] = "uscat", 914 [__builtin_ctz(ARM_HWCAP_A64_ILRCPC )] = "ilrcpc", 915 [__builtin_ctz(ARM_HWCAP_A64_FLAGM )] = "flagm", 916 [__builtin_ctz(ARM_HWCAP_A64_SSBS )] = "ssbs", 917 [__builtin_ctz(ARM_HWCAP_A64_SB )] = "sb", 918 [__builtin_ctz(ARM_HWCAP_A64_PACA )] = "paca", 919 [__builtin_ctz(ARM_HWCAP_A64_PACG )] = "pacg", 920 }; 921 922 return bit < ARRAY_SIZE(hwcap_str) ? hwcap_str[bit] : NULL; 923 } 924 925 const char *elf_hwcap2_str(uint32_t bit) 926 { 927 static const char *hwcap_str[] = { 928 [__builtin_ctz(ARM_HWCAP2_A64_DCPODP )] = "dcpodp", 929 [__builtin_ctz(ARM_HWCAP2_A64_SVE2 )] = "sve2", 930 [__builtin_ctz(ARM_HWCAP2_A64_SVEAES )] = "sveaes", 931 [__builtin_ctz(ARM_HWCAP2_A64_SVEPMULL )] = "svepmull", 932 [__builtin_ctz(ARM_HWCAP2_A64_SVEBITPERM )] = "svebitperm", 933 [__builtin_ctz(ARM_HWCAP2_A64_SVESHA3 )] = "svesha3", 934 [__builtin_ctz(ARM_HWCAP2_A64_SVESM4 )] = "svesm4", 935 [__builtin_ctz(ARM_HWCAP2_A64_FLAGM2 )] = "flagm2", 936 [__builtin_ctz(ARM_HWCAP2_A64_FRINT )] = "frint", 937 [__builtin_ctz(ARM_HWCAP2_A64_SVEI8MM )] = "svei8mm", 938 [__builtin_ctz(ARM_HWCAP2_A64_SVEF32MM )] = "svef32mm", 939 [__builtin_ctz(ARM_HWCAP2_A64_SVEF64MM )] = "svef64mm", 940 [__builtin_ctz(ARM_HWCAP2_A64_SVEBF16 )] = "svebf16", 941 [__builtin_ctz(ARM_HWCAP2_A64_I8MM )] = "i8mm", 942 [__builtin_ctz(ARM_HWCAP2_A64_BF16 )] = "bf16", 943 [__builtin_ctz(ARM_HWCAP2_A64_DGH )] = "dgh", 944 [__builtin_ctz(ARM_HWCAP2_A64_RNG )] = "rng", 945 [__builtin_ctz(ARM_HWCAP2_A64_BTI )] = "bti", 946 [__builtin_ctz(ARM_HWCAP2_A64_MTE )] = "mte", 947 [__builtin_ctz(ARM_HWCAP2_A64_ECV )] = "ecv", 948 [__builtin_ctz(ARM_HWCAP2_A64_AFP )] = "afp", 949 [__builtin_ctz(ARM_HWCAP2_A64_RPRES )] = "rpres", 950 [__builtin_ctz(ARM_HWCAP2_A64_MTE3 )] = "mte3", 951 [__builtin_ctz(ARM_HWCAP2_A64_SME )] = "sme", 952 [__builtin_ctz(ARM_HWCAP2_A64_SME_I16I64 )] = "smei16i64", 953 [__builtin_ctz(ARM_HWCAP2_A64_SME_F64F64 )] = "smef64f64", 954 [__builtin_ctz(ARM_HWCAP2_A64_SME_I8I32 )] = "smei8i32", 955 [__builtin_ctz(ARM_HWCAP2_A64_SME_F16F32 )] = "smef16f32", 956 [__builtin_ctz(ARM_HWCAP2_A64_SME_B16F32 )] = "smeb16f32", 957 [__builtin_ctz(ARM_HWCAP2_A64_SME_F32F32 )] = "smef32f32", 958 [__builtin_ctz(ARM_HWCAP2_A64_SME_FA64 )] = "smefa64", 959 [__builtin_ctz(ARM_HWCAP2_A64_WFXT )] = "wfxt", 960 [__builtin_ctzll(ARM_HWCAP2_A64_EBF16 )] = "ebf16", 961 [__builtin_ctzll(ARM_HWCAP2_A64_SVE_EBF16 )] = "sveebf16", 962 [__builtin_ctzll(ARM_HWCAP2_A64_CSSC )] = "cssc", 963 [__builtin_ctzll(ARM_HWCAP2_A64_RPRFM )] = "rprfm", 964 [__builtin_ctzll(ARM_HWCAP2_A64_SVE2P1 )] = "sve2p1", 965 [__builtin_ctzll(ARM_HWCAP2_A64_SME2 )] = "sme2", 966 [__builtin_ctzll(ARM_HWCAP2_A64_SME2P1 )] = "sme2p1", 967 [__builtin_ctzll(ARM_HWCAP2_A64_SME_I16I32 )] = "smei16i32", 968 [__builtin_ctzll(ARM_HWCAP2_A64_SME_BI32I32)] = "smebi32i32", 969 [__builtin_ctzll(ARM_HWCAP2_A64_SME_B16B16 )] = "smeb16b16", 970 [__builtin_ctzll(ARM_HWCAP2_A64_SME_F16F16 )] = "smef16f16", 971 [__builtin_ctzll(ARM_HWCAP2_A64_MOPS )] = "mops", 972 [__builtin_ctzll(ARM_HWCAP2_A64_HBC )] = "hbc", 973 }; 974 975 return bit < ARRAY_SIZE(hwcap_str) ? hwcap_str[bit] : NULL; 976 } 977 978 #undef GET_FEATURE_ID 979 980 #if TARGET_BIG_ENDIAN 981 # define VDSO_HEADER "vdso-be.c.inc" 982 #else 983 # define VDSO_HEADER "vdso-le.c.inc" 984 #endif 985 986 #endif /* not TARGET_AARCH64 */ 987 988 #endif /* TARGET_ARM */ 989 990 #ifdef TARGET_SPARC 991 992 #ifndef TARGET_SPARC64 993 # define ELF_CLASS ELFCLASS32 994 # define ELF_ARCH EM_SPARC 995 #elif defined(TARGET_ABI32) 996 # define ELF_CLASS ELFCLASS32 997 # define elf_check_arch(x) ((x) == EM_SPARC32PLUS || (x) == EM_SPARC) 998 #else 999 # define ELF_CLASS ELFCLASS64 1000 # define ELF_ARCH EM_SPARCV9 1001 #endif 1002 1003 #include "elf.h" 1004 1005 #define ELF_HWCAP get_elf_hwcap() 1006 1007 static uint32_t get_elf_hwcap(void) 1008 { 1009 /* There are not many sparc32 hwcap bits -- we have all of them. */ 1010 uint32_t r = HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | 1011 HWCAP_SPARC_SWAP | HWCAP_SPARC_MULDIV; 1012 1013 #ifdef TARGET_SPARC64 1014 CPUSPARCState *env = cpu_env(thread_cpu); 1015 uint32_t features = env->def.features; 1016 1017 r |= HWCAP_SPARC_V9 | HWCAP_SPARC_V8PLUS; 1018 /* 32x32 multiply and divide are efficient. */ 1019 r |= HWCAP_SPARC_MUL32 | HWCAP_SPARC_DIV32; 1020 /* We don't have an internal feature bit for this. */ 1021 r |= HWCAP_SPARC_POPC; 1022 r |= features & CPU_FEATURE_FSMULD ? HWCAP_SPARC_FSMULD : 0; 1023 r |= features & CPU_FEATURE_VIS1 ? HWCAP_SPARC_VIS : 0; 1024 r |= features & CPU_FEATURE_VIS2 ? HWCAP_SPARC_VIS2 : 0; 1025 r |= features & CPU_FEATURE_FMAF ? HWCAP_SPARC_FMAF : 0; 1026 r |= features & CPU_FEATURE_VIS3 ? HWCAP_SPARC_VIS3 : 0; 1027 r |= features & CPU_FEATURE_IMA ? HWCAP_SPARC_IMA : 0; 1028 #endif 1029 1030 return r; 1031 } 1032 1033 static inline void init_thread(struct target_pt_regs *regs, 1034 struct image_info *infop) 1035 { 1036 /* Note that target_cpu_copy_regs does not read psr/tstate. */ 1037 regs->pc = infop->entry; 1038 regs->npc = regs->pc + 4; 1039 regs->y = 0; 1040 regs->u_regs[14] = (infop->start_stack - 16 * sizeof(abi_ulong) 1041 - TARGET_STACK_BIAS); 1042 } 1043 #endif /* TARGET_SPARC */ 1044 1045 #ifdef TARGET_PPC 1046 1047 #define ELF_MACHINE PPC_ELF_MACHINE 1048 1049 #if defined(TARGET_PPC64) 1050 1051 #define elf_check_arch(x) ( (x) == EM_PPC64 ) 1052 1053 #define ELF_CLASS ELFCLASS64 1054 1055 #else 1056 1057 #define ELF_CLASS ELFCLASS32 1058 #define EXSTACK_DEFAULT true 1059 1060 #endif 1061 1062 #define ELF_ARCH EM_PPC 1063 1064 /* Feature masks for the Aux Vector Hardware Capabilities (AT_HWCAP). 1065 See arch/powerpc/include/asm/cputable.h. */ 1066 enum { 1067 QEMU_PPC_FEATURE_32 = 0x80000000, 1068 QEMU_PPC_FEATURE_64 = 0x40000000, 1069 QEMU_PPC_FEATURE_601_INSTR = 0x20000000, 1070 QEMU_PPC_FEATURE_HAS_ALTIVEC = 0x10000000, 1071 QEMU_PPC_FEATURE_HAS_FPU = 0x08000000, 1072 QEMU_PPC_FEATURE_HAS_MMU = 0x04000000, 1073 QEMU_PPC_FEATURE_HAS_4xxMAC = 0x02000000, 1074 QEMU_PPC_FEATURE_UNIFIED_CACHE = 0x01000000, 1075 QEMU_PPC_FEATURE_HAS_SPE = 0x00800000, 1076 QEMU_PPC_FEATURE_HAS_EFP_SINGLE = 0x00400000, 1077 QEMU_PPC_FEATURE_HAS_EFP_DOUBLE = 0x00200000, 1078 QEMU_PPC_FEATURE_NO_TB = 0x00100000, 1079 QEMU_PPC_FEATURE_POWER4 = 0x00080000, 1080 QEMU_PPC_FEATURE_POWER5 = 0x00040000, 1081 QEMU_PPC_FEATURE_POWER5_PLUS = 0x00020000, 1082 QEMU_PPC_FEATURE_CELL = 0x00010000, 1083 QEMU_PPC_FEATURE_BOOKE = 0x00008000, 1084 QEMU_PPC_FEATURE_SMT = 0x00004000, 1085 QEMU_PPC_FEATURE_ICACHE_SNOOP = 0x00002000, 1086 QEMU_PPC_FEATURE_ARCH_2_05 = 0x00001000, 1087 QEMU_PPC_FEATURE_PA6T = 0x00000800, 1088 QEMU_PPC_FEATURE_HAS_DFP = 0x00000400, 1089 QEMU_PPC_FEATURE_POWER6_EXT = 0x00000200, 1090 QEMU_PPC_FEATURE_ARCH_2_06 = 0x00000100, 1091 QEMU_PPC_FEATURE_HAS_VSX = 0x00000080, 1092 QEMU_PPC_FEATURE_PSERIES_PERFMON_COMPAT = 0x00000040, 1093 1094 QEMU_PPC_FEATURE_TRUE_LE = 0x00000002, 1095 QEMU_PPC_FEATURE_PPC_LE = 0x00000001, 1096 1097 /* Feature definitions in AT_HWCAP2. */ 1098 QEMU_PPC_FEATURE2_ARCH_2_07 = 0x80000000, /* ISA 2.07 */ 1099 QEMU_PPC_FEATURE2_HAS_HTM = 0x40000000, /* Hardware Transactional Memory */ 1100 QEMU_PPC_FEATURE2_HAS_DSCR = 0x20000000, /* Data Stream Control Register */ 1101 QEMU_PPC_FEATURE2_HAS_EBB = 0x10000000, /* Event Base Branching */ 1102 QEMU_PPC_FEATURE2_HAS_ISEL = 0x08000000, /* Integer Select */ 1103 QEMU_PPC_FEATURE2_HAS_TAR = 0x04000000, /* Target Address Register */ 1104 QEMU_PPC_FEATURE2_VEC_CRYPTO = 0x02000000, 1105 QEMU_PPC_FEATURE2_HTM_NOSC = 0x01000000, 1106 QEMU_PPC_FEATURE2_ARCH_3_00 = 0x00800000, /* ISA 3.00 */ 1107 QEMU_PPC_FEATURE2_HAS_IEEE128 = 0x00400000, /* VSX IEEE Bin Float 128-bit */ 1108 QEMU_PPC_FEATURE2_DARN = 0x00200000, /* darn random number insn */ 1109 QEMU_PPC_FEATURE2_SCV = 0x00100000, /* scv syscall */ 1110 QEMU_PPC_FEATURE2_HTM_NO_SUSPEND = 0x00080000, /* TM w/o suspended state */ 1111 QEMU_PPC_FEATURE2_ARCH_3_1 = 0x00040000, /* ISA 3.1 */ 1112 QEMU_PPC_FEATURE2_MMA = 0x00020000, /* Matrix-Multiply Assist */ 1113 }; 1114 1115 #define ELF_HWCAP get_elf_hwcap() 1116 1117 static uint32_t get_elf_hwcap(void) 1118 { 1119 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu); 1120 uint32_t features = 0; 1121 1122 /* We don't have to be terribly complete here; the high points are 1123 Altivec/FP/SPE support. Anything else is just a bonus. */ 1124 #define GET_FEATURE(flag, feature) \ 1125 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0) 1126 #define GET_FEATURE2(flags, feature) \ 1127 do { \ 1128 if ((cpu->env.insns_flags2 & flags) == flags) { \ 1129 features |= feature; \ 1130 } \ 1131 } while (0) 1132 GET_FEATURE(PPC_64B, QEMU_PPC_FEATURE_64); 1133 GET_FEATURE(PPC_FLOAT, QEMU_PPC_FEATURE_HAS_FPU); 1134 GET_FEATURE(PPC_ALTIVEC, QEMU_PPC_FEATURE_HAS_ALTIVEC); 1135 GET_FEATURE(PPC_SPE, QEMU_PPC_FEATURE_HAS_SPE); 1136 GET_FEATURE(PPC_SPE_SINGLE, QEMU_PPC_FEATURE_HAS_EFP_SINGLE); 1137 GET_FEATURE(PPC_SPE_DOUBLE, QEMU_PPC_FEATURE_HAS_EFP_DOUBLE); 1138 GET_FEATURE(PPC_BOOKE, QEMU_PPC_FEATURE_BOOKE); 1139 GET_FEATURE(PPC_405_MAC, QEMU_PPC_FEATURE_HAS_4xxMAC); 1140 GET_FEATURE2(PPC2_DFP, QEMU_PPC_FEATURE_HAS_DFP); 1141 GET_FEATURE2(PPC2_VSX, QEMU_PPC_FEATURE_HAS_VSX); 1142 GET_FEATURE2((PPC2_PERM_ISA206 | PPC2_DIVE_ISA206 | PPC2_ATOMIC_ISA206 | 1143 PPC2_FP_CVT_ISA206 | PPC2_FP_TST_ISA206), 1144 QEMU_PPC_FEATURE_ARCH_2_06); 1145 #undef GET_FEATURE 1146 #undef GET_FEATURE2 1147 1148 return features; 1149 } 1150 1151 #define ELF_HWCAP2 get_elf_hwcap2() 1152 1153 static uint32_t get_elf_hwcap2(void) 1154 { 1155 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu); 1156 uint32_t features = 0; 1157 1158 #define GET_FEATURE(flag, feature) \ 1159 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0) 1160 #define GET_FEATURE2(flag, feature) \ 1161 do { if (cpu->env.insns_flags2 & flag) { features |= feature; } } while (0) 1162 1163 GET_FEATURE(PPC_ISEL, QEMU_PPC_FEATURE2_HAS_ISEL); 1164 GET_FEATURE2(PPC2_BCTAR_ISA207, QEMU_PPC_FEATURE2_HAS_TAR); 1165 GET_FEATURE2((PPC2_BCTAR_ISA207 | PPC2_LSQ_ISA207 | PPC2_ALTIVEC_207 | 1166 PPC2_ISA207S), QEMU_PPC_FEATURE2_ARCH_2_07 | 1167 QEMU_PPC_FEATURE2_VEC_CRYPTO); 1168 GET_FEATURE2(PPC2_ISA300, QEMU_PPC_FEATURE2_ARCH_3_00 | 1169 QEMU_PPC_FEATURE2_DARN | QEMU_PPC_FEATURE2_HAS_IEEE128); 1170 GET_FEATURE2(PPC2_ISA310, QEMU_PPC_FEATURE2_ARCH_3_1 | 1171 QEMU_PPC_FEATURE2_MMA); 1172 1173 #undef GET_FEATURE 1174 #undef GET_FEATURE2 1175 1176 return features; 1177 } 1178 1179 /* 1180 * The requirements here are: 1181 * - keep the final alignment of sp (sp & 0xf) 1182 * - make sure the 32-bit value at the first 16 byte aligned position of 1183 * AUXV is greater than 16 for glibc compatibility. 1184 * AT_IGNOREPPC is used for that. 1185 * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC, 1186 * even if DLINFO_ARCH_ITEMS goes to zero or is undefined. 1187 */ 1188 #define DLINFO_ARCH_ITEMS 5 1189 #define ARCH_DLINFO \ 1190 do { \ 1191 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu); \ 1192 /* \ 1193 * Handle glibc compatibility: these magic entries must \ 1194 * be at the lowest addresses in the final auxv. \ 1195 */ \ 1196 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \ 1197 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \ 1198 NEW_AUX_ENT(AT_DCACHEBSIZE, cpu->env.dcache_line_size); \ 1199 NEW_AUX_ENT(AT_ICACHEBSIZE, cpu->env.icache_line_size); \ 1200 NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \ 1201 } while (0) 1202 1203 static inline void init_thread(struct target_pt_regs *_regs, struct image_info *infop) 1204 { 1205 _regs->gpr[1] = infop->start_stack; 1206 #if defined(TARGET_PPC64) 1207 if (get_ppc64_abi(infop) < 2) { 1208 uint64_t val; 1209 get_user_u64(val, infop->entry + 8); 1210 _regs->gpr[2] = val + infop->load_bias; 1211 get_user_u64(val, infop->entry); 1212 infop->entry = val + infop->load_bias; 1213 } else { 1214 _regs->gpr[12] = infop->entry; /* r12 set to global entry address */ 1215 } 1216 #endif 1217 _regs->nip = infop->entry; 1218 } 1219 1220 /* See linux kernel: arch/powerpc/include/asm/elf.h. */ 1221 #define ELF_NREG 48 1222 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; 1223 1224 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUPPCState *env) 1225 { 1226 int i; 1227 target_ulong ccr = 0; 1228 1229 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) { 1230 (*regs)[i] = tswapreg(env->gpr[i]); 1231 } 1232 1233 (*regs)[32] = tswapreg(env->nip); 1234 (*regs)[33] = tswapreg(env->msr); 1235 (*regs)[35] = tswapreg(env->ctr); 1236 (*regs)[36] = tswapreg(env->lr); 1237 (*regs)[37] = tswapreg(cpu_read_xer(env)); 1238 1239 ccr = ppc_get_cr(env); 1240 (*regs)[38] = tswapreg(ccr); 1241 } 1242 1243 #define USE_ELF_CORE_DUMP 1244 #define ELF_EXEC_PAGESIZE 4096 1245 1246 #ifndef TARGET_PPC64 1247 # define VDSO_HEADER "vdso-32.c.inc" 1248 #elif TARGET_BIG_ENDIAN 1249 # define VDSO_HEADER "vdso-64.c.inc" 1250 #else 1251 # define VDSO_HEADER "vdso-64le.c.inc" 1252 #endif 1253 1254 #endif 1255 1256 #ifdef TARGET_LOONGARCH64 1257 1258 #define ELF_CLASS ELFCLASS64 1259 #define ELF_ARCH EM_LOONGARCH 1260 #define EXSTACK_DEFAULT true 1261 1262 #define elf_check_arch(x) ((x) == EM_LOONGARCH) 1263 1264 #define VDSO_HEADER "vdso.c.inc" 1265 1266 static inline void init_thread(struct target_pt_regs *regs, 1267 struct image_info *infop) 1268 { 1269 /*Set crmd PG,DA = 1,0 */ 1270 regs->csr.crmd = 2 << 3; 1271 regs->csr.era = infop->entry; 1272 regs->regs[3] = infop->start_stack; 1273 } 1274 1275 /* See linux kernel: arch/loongarch/include/asm/elf.h */ 1276 #define ELF_NREG 45 1277 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; 1278 1279 enum { 1280 TARGET_EF_R0 = 0, 1281 TARGET_EF_CSR_ERA = TARGET_EF_R0 + 33, 1282 TARGET_EF_CSR_BADV = TARGET_EF_R0 + 34, 1283 }; 1284 1285 static void elf_core_copy_regs(target_elf_gregset_t *regs, 1286 const CPULoongArchState *env) 1287 { 1288 int i; 1289 1290 (*regs)[TARGET_EF_R0] = 0; 1291 1292 for (i = 1; i < ARRAY_SIZE(env->gpr); i++) { 1293 (*regs)[TARGET_EF_R0 + i] = tswapreg(env->gpr[i]); 1294 } 1295 1296 (*regs)[TARGET_EF_CSR_ERA] = tswapreg(env->pc); 1297 (*regs)[TARGET_EF_CSR_BADV] = tswapreg(env->CSR_BADV); 1298 } 1299 1300 #define USE_ELF_CORE_DUMP 1301 #define ELF_EXEC_PAGESIZE 4096 1302 1303 #define ELF_HWCAP get_elf_hwcap() 1304 1305 /* See arch/loongarch/include/uapi/asm/hwcap.h */ 1306 enum { 1307 HWCAP_LOONGARCH_CPUCFG = (1 << 0), 1308 HWCAP_LOONGARCH_LAM = (1 << 1), 1309 HWCAP_LOONGARCH_UAL = (1 << 2), 1310 HWCAP_LOONGARCH_FPU = (1 << 3), 1311 HWCAP_LOONGARCH_LSX = (1 << 4), 1312 HWCAP_LOONGARCH_LASX = (1 << 5), 1313 HWCAP_LOONGARCH_CRC32 = (1 << 6), 1314 HWCAP_LOONGARCH_COMPLEX = (1 << 7), 1315 HWCAP_LOONGARCH_CRYPTO = (1 << 8), 1316 HWCAP_LOONGARCH_LVZ = (1 << 9), 1317 HWCAP_LOONGARCH_LBT_X86 = (1 << 10), 1318 HWCAP_LOONGARCH_LBT_ARM = (1 << 11), 1319 HWCAP_LOONGARCH_LBT_MIPS = (1 << 12), 1320 }; 1321 1322 static uint32_t get_elf_hwcap(void) 1323 { 1324 LoongArchCPU *cpu = LOONGARCH_CPU(thread_cpu); 1325 uint32_t hwcaps = 0; 1326 1327 hwcaps |= HWCAP_LOONGARCH_CRC32; 1328 1329 if (FIELD_EX32(cpu->env.cpucfg[1], CPUCFG1, UAL)) { 1330 hwcaps |= HWCAP_LOONGARCH_UAL; 1331 } 1332 1333 if (FIELD_EX32(cpu->env.cpucfg[2], CPUCFG2, FP)) { 1334 hwcaps |= HWCAP_LOONGARCH_FPU; 1335 } 1336 1337 if (FIELD_EX32(cpu->env.cpucfg[2], CPUCFG2, LAM)) { 1338 hwcaps |= HWCAP_LOONGARCH_LAM; 1339 } 1340 1341 if (FIELD_EX32(cpu->env.cpucfg[2], CPUCFG2, LSX)) { 1342 hwcaps |= HWCAP_LOONGARCH_LSX; 1343 } 1344 1345 if (FIELD_EX32(cpu->env.cpucfg[2], CPUCFG2, LASX)) { 1346 hwcaps |= HWCAP_LOONGARCH_LASX; 1347 } 1348 1349 return hwcaps; 1350 } 1351 1352 #define ELF_PLATFORM "loongarch" 1353 1354 #endif /* TARGET_LOONGARCH64 */ 1355 1356 #ifdef TARGET_MIPS 1357 1358 #ifdef TARGET_MIPS64 1359 #define ELF_CLASS ELFCLASS64 1360 #else 1361 #define ELF_CLASS ELFCLASS32 1362 #endif 1363 #define ELF_ARCH EM_MIPS 1364 #define EXSTACK_DEFAULT true 1365 1366 #ifdef TARGET_ABI_MIPSN32 1367 #define elf_check_abi(x) ((x) & EF_MIPS_ABI2) 1368 #else 1369 #define elf_check_abi(x) (!((x) & EF_MIPS_ABI2)) 1370 #endif 1371 1372 #define ELF_BASE_PLATFORM get_elf_base_platform() 1373 1374 #define MATCH_PLATFORM_INSN(_flags, _base_platform) \ 1375 do { if ((cpu->env.insn_flags & (_flags)) == _flags) \ 1376 { return _base_platform; } } while (0) 1377 1378 static const char *get_elf_base_platform(void) 1379 { 1380 MIPSCPU *cpu = MIPS_CPU(thread_cpu); 1381 1382 /* 64 bit ISAs goes first */ 1383 MATCH_PLATFORM_INSN(CPU_MIPS64R6, "mips64r6"); 1384 MATCH_PLATFORM_INSN(CPU_MIPS64R5, "mips64r5"); 1385 MATCH_PLATFORM_INSN(CPU_MIPS64R2, "mips64r2"); 1386 MATCH_PLATFORM_INSN(CPU_MIPS64R1, "mips64"); 1387 MATCH_PLATFORM_INSN(CPU_MIPS5, "mips5"); 1388 MATCH_PLATFORM_INSN(CPU_MIPS4, "mips4"); 1389 MATCH_PLATFORM_INSN(CPU_MIPS3, "mips3"); 1390 1391 /* 32 bit ISAs */ 1392 MATCH_PLATFORM_INSN(CPU_MIPS32R6, "mips32r6"); 1393 MATCH_PLATFORM_INSN(CPU_MIPS32R5, "mips32r5"); 1394 MATCH_PLATFORM_INSN(CPU_MIPS32R2, "mips32r2"); 1395 MATCH_PLATFORM_INSN(CPU_MIPS32R1, "mips32"); 1396 MATCH_PLATFORM_INSN(CPU_MIPS2, "mips2"); 1397 1398 /* Fallback */ 1399 return "mips"; 1400 } 1401 #undef MATCH_PLATFORM_INSN 1402 1403 static inline void init_thread(struct target_pt_regs *regs, 1404 struct image_info *infop) 1405 { 1406 regs->cp0_status = 2 << CP0St_KSU; 1407 regs->cp0_epc = infop->entry; 1408 regs->regs[29] = infop->start_stack; 1409 } 1410 1411 /* See linux kernel: arch/mips/include/asm/elf.h. */ 1412 #define ELF_NREG 45 1413 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; 1414 1415 /* See linux kernel: arch/mips/include/asm/reg.h. */ 1416 enum { 1417 #ifdef TARGET_MIPS64 1418 TARGET_EF_R0 = 0, 1419 #else 1420 TARGET_EF_R0 = 6, 1421 #endif 1422 TARGET_EF_R26 = TARGET_EF_R0 + 26, 1423 TARGET_EF_R27 = TARGET_EF_R0 + 27, 1424 TARGET_EF_LO = TARGET_EF_R0 + 32, 1425 TARGET_EF_HI = TARGET_EF_R0 + 33, 1426 TARGET_EF_CP0_EPC = TARGET_EF_R0 + 34, 1427 TARGET_EF_CP0_BADVADDR = TARGET_EF_R0 + 35, 1428 TARGET_EF_CP0_STATUS = TARGET_EF_R0 + 36, 1429 TARGET_EF_CP0_CAUSE = TARGET_EF_R0 + 37 1430 }; 1431 1432 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */ 1433 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMIPSState *env) 1434 { 1435 int i; 1436 1437 for (i = 0; i < TARGET_EF_R0; i++) { 1438 (*regs)[i] = 0; 1439 } 1440 (*regs)[TARGET_EF_R0] = 0; 1441 1442 for (i = 1; i < ARRAY_SIZE(env->active_tc.gpr); i++) { 1443 (*regs)[TARGET_EF_R0 + i] = tswapreg(env->active_tc.gpr[i]); 1444 } 1445 1446 (*regs)[TARGET_EF_R26] = 0; 1447 (*regs)[TARGET_EF_R27] = 0; 1448 (*regs)[TARGET_EF_LO] = tswapreg(env->active_tc.LO[0]); 1449 (*regs)[TARGET_EF_HI] = tswapreg(env->active_tc.HI[0]); 1450 (*regs)[TARGET_EF_CP0_EPC] = tswapreg(env->active_tc.PC); 1451 (*regs)[TARGET_EF_CP0_BADVADDR] = tswapreg(env->CP0_BadVAddr); 1452 (*regs)[TARGET_EF_CP0_STATUS] = tswapreg(env->CP0_Status); 1453 (*regs)[TARGET_EF_CP0_CAUSE] = tswapreg(env->CP0_Cause); 1454 } 1455 1456 #define USE_ELF_CORE_DUMP 1457 #define ELF_EXEC_PAGESIZE 4096 1458 1459 /* See arch/mips/include/uapi/asm/hwcap.h. */ 1460 enum { 1461 HWCAP_MIPS_R6 = (1 << 0), 1462 HWCAP_MIPS_MSA = (1 << 1), 1463 HWCAP_MIPS_CRC32 = (1 << 2), 1464 HWCAP_MIPS_MIPS16 = (1 << 3), 1465 HWCAP_MIPS_MDMX = (1 << 4), 1466 HWCAP_MIPS_MIPS3D = (1 << 5), 1467 HWCAP_MIPS_SMARTMIPS = (1 << 6), 1468 HWCAP_MIPS_DSP = (1 << 7), 1469 HWCAP_MIPS_DSP2 = (1 << 8), 1470 HWCAP_MIPS_DSP3 = (1 << 9), 1471 HWCAP_MIPS_MIPS16E2 = (1 << 10), 1472 HWCAP_LOONGSON_MMI = (1 << 11), 1473 HWCAP_LOONGSON_EXT = (1 << 12), 1474 HWCAP_LOONGSON_EXT2 = (1 << 13), 1475 HWCAP_LOONGSON_CPUCFG = (1 << 14), 1476 }; 1477 1478 #define ELF_HWCAP get_elf_hwcap() 1479 1480 #define GET_FEATURE_INSN(_flag, _hwcap) \ 1481 do { if (cpu->env.insn_flags & (_flag)) { hwcaps |= _hwcap; } } while (0) 1482 1483 #define GET_FEATURE_REG_SET(_reg, _mask, _hwcap) \ 1484 do { if (cpu->env._reg & (_mask)) { hwcaps |= _hwcap; } } while (0) 1485 1486 #define GET_FEATURE_REG_EQU(_reg, _start, _length, _val, _hwcap) \ 1487 do { \ 1488 if (extract32(cpu->env._reg, (_start), (_length)) == (_val)) { \ 1489 hwcaps |= _hwcap; \ 1490 } \ 1491 } while (0) 1492 1493 static uint32_t get_elf_hwcap(void) 1494 { 1495 MIPSCPU *cpu = MIPS_CPU(thread_cpu); 1496 uint32_t hwcaps = 0; 1497 1498 GET_FEATURE_REG_EQU(CP0_Config0, CP0C0_AR, CP0C0_AR_LENGTH, 1499 2, HWCAP_MIPS_R6); 1500 GET_FEATURE_REG_SET(CP0_Config3, 1 << CP0C3_MSAP, HWCAP_MIPS_MSA); 1501 GET_FEATURE_INSN(ASE_LMMI, HWCAP_LOONGSON_MMI); 1502 GET_FEATURE_INSN(ASE_LEXT, HWCAP_LOONGSON_EXT); 1503 1504 return hwcaps; 1505 } 1506 1507 #undef GET_FEATURE_REG_EQU 1508 #undef GET_FEATURE_REG_SET 1509 #undef GET_FEATURE_INSN 1510 1511 #endif /* TARGET_MIPS */ 1512 1513 #ifdef TARGET_MICROBLAZE 1514 1515 #define elf_check_arch(x) ( (x) == EM_MICROBLAZE || (x) == EM_MICROBLAZE_OLD) 1516 1517 #define ELF_CLASS ELFCLASS32 1518 #define ELF_ARCH EM_MICROBLAZE 1519 1520 static inline void init_thread(struct target_pt_regs *regs, 1521 struct image_info *infop) 1522 { 1523 regs->pc = infop->entry; 1524 regs->r1 = infop->start_stack; 1525 1526 } 1527 1528 #define ELF_EXEC_PAGESIZE 4096 1529 1530 #define USE_ELF_CORE_DUMP 1531 #define ELF_NREG 38 1532 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; 1533 1534 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */ 1535 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMBState *env) 1536 { 1537 int i, pos = 0; 1538 1539 for (i = 0; i < 32; i++) { 1540 (*regs)[pos++] = tswapreg(env->regs[i]); 1541 } 1542 1543 (*regs)[pos++] = tswapreg(env->pc); 1544 (*regs)[pos++] = tswapreg(mb_cpu_read_msr(env)); 1545 (*regs)[pos++] = 0; 1546 (*regs)[pos++] = tswapreg(env->ear); 1547 (*regs)[pos++] = 0; 1548 (*regs)[pos++] = tswapreg(env->esr); 1549 } 1550 1551 #endif /* TARGET_MICROBLAZE */ 1552 1553 #ifdef TARGET_OPENRISC 1554 1555 #define ELF_ARCH EM_OPENRISC 1556 #define ELF_CLASS ELFCLASS32 1557 #define ELF_DATA ELFDATA2MSB 1558 1559 static inline void init_thread(struct target_pt_regs *regs, 1560 struct image_info *infop) 1561 { 1562 regs->pc = infop->entry; 1563 regs->gpr[1] = infop->start_stack; 1564 } 1565 1566 #define USE_ELF_CORE_DUMP 1567 #define ELF_EXEC_PAGESIZE 8192 1568 1569 /* See linux kernel arch/openrisc/include/asm/elf.h. */ 1570 #define ELF_NREG 34 /* gprs and pc, sr */ 1571 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; 1572 1573 static void elf_core_copy_regs(target_elf_gregset_t *regs, 1574 const CPUOpenRISCState *env) 1575 { 1576 int i; 1577 1578 for (i = 0; i < 32; i++) { 1579 (*regs)[i] = tswapreg(cpu_get_gpr(env, i)); 1580 } 1581 (*regs)[32] = tswapreg(env->pc); 1582 (*regs)[33] = tswapreg(cpu_get_sr(env)); 1583 } 1584 #define ELF_HWCAP 0 1585 #define ELF_PLATFORM NULL 1586 1587 #endif /* TARGET_OPENRISC */ 1588 1589 #ifdef TARGET_SH4 1590 1591 #define ELF_CLASS ELFCLASS32 1592 #define ELF_ARCH EM_SH 1593 1594 static inline void init_thread(struct target_pt_regs *regs, 1595 struct image_info *infop) 1596 { 1597 /* Check other registers XXXXX */ 1598 regs->pc = infop->entry; 1599 regs->regs[15] = infop->start_stack; 1600 } 1601 1602 /* See linux kernel: arch/sh/include/asm/elf.h. */ 1603 #define ELF_NREG 23 1604 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; 1605 1606 /* See linux kernel: arch/sh/include/asm/ptrace.h. */ 1607 enum { 1608 TARGET_REG_PC = 16, 1609 TARGET_REG_PR = 17, 1610 TARGET_REG_SR = 18, 1611 TARGET_REG_GBR = 19, 1612 TARGET_REG_MACH = 20, 1613 TARGET_REG_MACL = 21, 1614 TARGET_REG_SYSCALL = 22 1615 }; 1616 1617 static inline void elf_core_copy_regs(target_elf_gregset_t *regs, 1618 const CPUSH4State *env) 1619 { 1620 int i; 1621 1622 for (i = 0; i < 16; i++) { 1623 (*regs)[i] = tswapreg(env->gregs[i]); 1624 } 1625 1626 (*regs)[TARGET_REG_PC] = tswapreg(env->pc); 1627 (*regs)[TARGET_REG_PR] = tswapreg(env->pr); 1628 (*regs)[TARGET_REG_SR] = tswapreg(env->sr); 1629 (*regs)[TARGET_REG_GBR] = tswapreg(env->gbr); 1630 (*regs)[TARGET_REG_MACH] = tswapreg(env->mach); 1631 (*regs)[TARGET_REG_MACL] = tswapreg(env->macl); 1632 (*regs)[TARGET_REG_SYSCALL] = 0; /* FIXME */ 1633 } 1634 1635 #define USE_ELF_CORE_DUMP 1636 #define ELF_EXEC_PAGESIZE 4096 1637 1638 enum { 1639 SH_CPU_HAS_FPU = 0x0001, /* Hardware FPU support */ 1640 SH_CPU_HAS_P2_FLUSH_BUG = 0x0002, /* Need to flush the cache in P2 area */ 1641 SH_CPU_HAS_MMU_PAGE_ASSOC = 0x0004, /* SH3: TLB way selection bit support */ 1642 SH_CPU_HAS_DSP = 0x0008, /* SH-DSP: DSP support */ 1643 SH_CPU_HAS_PERF_COUNTER = 0x0010, /* Hardware performance counters */ 1644 SH_CPU_HAS_PTEA = 0x0020, /* PTEA register */ 1645 SH_CPU_HAS_LLSC = 0x0040, /* movli.l/movco.l */ 1646 SH_CPU_HAS_L2_CACHE = 0x0080, /* Secondary cache / URAM */ 1647 SH_CPU_HAS_OP32 = 0x0100, /* 32-bit instruction support */ 1648 SH_CPU_HAS_PTEAEX = 0x0200, /* PTE ASID Extension support */ 1649 }; 1650 1651 #define ELF_HWCAP get_elf_hwcap() 1652 1653 static uint32_t get_elf_hwcap(void) 1654 { 1655 SuperHCPU *cpu = SUPERH_CPU(thread_cpu); 1656 uint32_t hwcap = 0; 1657 1658 hwcap |= SH_CPU_HAS_FPU; 1659 1660 if (cpu->env.features & SH_FEATURE_SH4A) { 1661 hwcap |= SH_CPU_HAS_LLSC; 1662 } 1663 1664 return hwcap; 1665 } 1666 1667 #endif 1668 1669 #ifdef TARGET_M68K 1670 1671 #define ELF_CLASS ELFCLASS32 1672 #define ELF_ARCH EM_68K 1673 1674 /* ??? Does this need to do anything? 1675 #define ELF_PLAT_INIT(_r) */ 1676 1677 static inline void init_thread(struct target_pt_regs *regs, 1678 struct image_info *infop) 1679 { 1680 regs->usp = infop->start_stack; 1681 regs->sr = 0; 1682 regs->pc = infop->entry; 1683 } 1684 1685 /* See linux kernel: arch/m68k/include/asm/elf.h. */ 1686 #define ELF_NREG 20 1687 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; 1688 1689 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUM68KState *env) 1690 { 1691 (*regs)[0] = tswapreg(env->dregs[1]); 1692 (*regs)[1] = tswapreg(env->dregs[2]); 1693 (*regs)[2] = tswapreg(env->dregs[3]); 1694 (*regs)[3] = tswapreg(env->dregs[4]); 1695 (*regs)[4] = tswapreg(env->dregs[5]); 1696 (*regs)[5] = tswapreg(env->dregs[6]); 1697 (*regs)[6] = tswapreg(env->dregs[7]); 1698 (*regs)[7] = tswapreg(env->aregs[0]); 1699 (*regs)[8] = tswapreg(env->aregs[1]); 1700 (*regs)[9] = tswapreg(env->aregs[2]); 1701 (*regs)[10] = tswapreg(env->aregs[3]); 1702 (*regs)[11] = tswapreg(env->aregs[4]); 1703 (*regs)[12] = tswapreg(env->aregs[5]); 1704 (*regs)[13] = tswapreg(env->aregs[6]); 1705 (*regs)[14] = tswapreg(env->dregs[0]); 1706 (*regs)[15] = tswapreg(env->aregs[7]); 1707 (*regs)[16] = tswapreg(env->dregs[0]); /* FIXME: orig_d0 */ 1708 (*regs)[17] = tswapreg(env->sr); 1709 (*regs)[18] = tswapreg(env->pc); 1710 (*regs)[19] = 0; /* FIXME: regs->format | regs->vector */ 1711 } 1712 1713 #define USE_ELF_CORE_DUMP 1714 #define ELF_EXEC_PAGESIZE 8192 1715 1716 #endif 1717 1718 #ifdef TARGET_ALPHA 1719 1720 #define ELF_CLASS ELFCLASS64 1721 #define ELF_ARCH EM_ALPHA 1722 1723 static inline void init_thread(struct target_pt_regs *regs, 1724 struct image_info *infop) 1725 { 1726 regs->pc = infop->entry; 1727 regs->ps = 8; 1728 regs->usp = infop->start_stack; 1729 } 1730 1731 #define ELF_EXEC_PAGESIZE 8192 1732 1733 #endif /* TARGET_ALPHA */ 1734 1735 #ifdef TARGET_S390X 1736 1737 #define ELF_CLASS ELFCLASS64 1738 #define ELF_DATA ELFDATA2MSB 1739 #define ELF_ARCH EM_S390 1740 1741 #include "elf.h" 1742 1743 #define ELF_HWCAP get_elf_hwcap() 1744 1745 #define GET_FEATURE(_feat, _hwcap) \ 1746 do { if (s390_has_feat(_feat)) { hwcap |= _hwcap; } } while (0) 1747 1748 uint32_t get_elf_hwcap(void) 1749 { 1750 /* 1751 * Let's assume we always have esan3 and zarch. 1752 * 31-bit processes can use 64-bit registers (high gprs). 1753 */ 1754 uint32_t hwcap = HWCAP_S390_ESAN3 | HWCAP_S390_ZARCH | HWCAP_S390_HIGH_GPRS; 1755 1756 GET_FEATURE(S390_FEAT_STFLE, HWCAP_S390_STFLE); 1757 GET_FEATURE(S390_FEAT_MSA, HWCAP_S390_MSA); 1758 GET_FEATURE(S390_FEAT_LONG_DISPLACEMENT, HWCAP_S390_LDISP); 1759 GET_FEATURE(S390_FEAT_EXTENDED_IMMEDIATE, HWCAP_S390_EIMM); 1760 if (s390_has_feat(S390_FEAT_EXTENDED_TRANSLATION_3) && 1761 s390_has_feat(S390_FEAT_ETF3_ENH)) { 1762 hwcap |= HWCAP_S390_ETF3EH; 1763 } 1764 GET_FEATURE(S390_FEAT_VECTOR, HWCAP_S390_VXRS); 1765 GET_FEATURE(S390_FEAT_VECTOR_ENH, HWCAP_S390_VXRS_EXT); 1766 GET_FEATURE(S390_FEAT_VECTOR_ENH2, HWCAP_S390_VXRS_EXT2); 1767 1768 return hwcap; 1769 } 1770 1771 const char *elf_hwcap_str(uint32_t bit) 1772 { 1773 static const char *hwcap_str[] = { 1774 [HWCAP_S390_NR_ESAN3] = "esan3", 1775 [HWCAP_S390_NR_ZARCH] = "zarch", 1776 [HWCAP_S390_NR_STFLE] = "stfle", 1777 [HWCAP_S390_NR_MSA] = "msa", 1778 [HWCAP_S390_NR_LDISP] = "ldisp", 1779 [HWCAP_S390_NR_EIMM] = "eimm", 1780 [HWCAP_S390_NR_DFP] = "dfp", 1781 [HWCAP_S390_NR_HPAGE] = "edat", 1782 [HWCAP_S390_NR_ETF3EH] = "etf3eh", 1783 [HWCAP_S390_NR_HIGH_GPRS] = "highgprs", 1784 [HWCAP_S390_NR_TE] = "te", 1785 [HWCAP_S390_NR_VXRS] = "vx", 1786 [HWCAP_S390_NR_VXRS_BCD] = "vxd", 1787 [HWCAP_S390_NR_VXRS_EXT] = "vxe", 1788 [HWCAP_S390_NR_GS] = "gs", 1789 [HWCAP_S390_NR_VXRS_EXT2] = "vxe2", 1790 [HWCAP_S390_NR_VXRS_PDE] = "vxp", 1791 [HWCAP_S390_NR_SORT] = "sort", 1792 [HWCAP_S390_NR_DFLT] = "dflt", 1793 [HWCAP_S390_NR_NNPA] = "nnpa", 1794 [HWCAP_S390_NR_PCI_MIO] = "pcimio", 1795 [HWCAP_S390_NR_SIE] = "sie", 1796 }; 1797 1798 return bit < ARRAY_SIZE(hwcap_str) ? hwcap_str[bit] : NULL; 1799 } 1800 1801 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop) 1802 { 1803 regs->psw.addr = infop->entry; 1804 regs->psw.mask = PSW_MASK_DAT | PSW_MASK_IO | PSW_MASK_EXT | \ 1805 PSW_MASK_MCHECK | PSW_MASK_PSTATE | PSW_MASK_64 | \ 1806 PSW_MASK_32; 1807 regs->gprs[15] = infop->start_stack; 1808 } 1809 1810 /* See linux kernel: arch/s390/include/uapi/asm/ptrace.h (s390_regs). */ 1811 #define ELF_NREG 27 1812 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; 1813 1814 enum { 1815 TARGET_REG_PSWM = 0, 1816 TARGET_REG_PSWA = 1, 1817 TARGET_REG_GPRS = 2, 1818 TARGET_REG_ARS = 18, 1819 TARGET_REG_ORIG_R2 = 26, 1820 }; 1821 1822 static void elf_core_copy_regs(target_elf_gregset_t *regs, 1823 const CPUS390XState *env) 1824 { 1825 int i; 1826 uint32_t *aregs; 1827 1828 (*regs)[TARGET_REG_PSWM] = tswapreg(env->psw.mask); 1829 (*regs)[TARGET_REG_PSWA] = tswapreg(env->psw.addr); 1830 for (i = 0; i < 16; i++) { 1831 (*regs)[TARGET_REG_GPRS + i] = tswapreg(env->regs[i]); 1832 } 1833 aregs = (uint32_t *)&((*regs)[TARGET_REG_ARS]); 1834 for (i = 0; i < 16; i++) { 1835 aregs[i] = tswap32(env->aregs[i]); 1836 } 1837 (*regs)[TARGET_REG_ORIG_R2] = 0; 1838 } 1839 1840 #define USE_ELF_CORE_DUMP 1841 #define ELF_EXEC_PAGESIZE 4096 1842 1843 #define VDSO_HEADER "vdso.c.inc" 1844 1845 #endif /* TARGET_S390X */ 1846 1847 #ifdef TARGET_RISCV 1848 1849 #define ELF_ARCH EM_RISCV 1850 1851 #ifdef TARGET_RISCV32 1852 #define ELF_CLASS ELFCLASS32 1853 #define VDSO_HEADER "vdso-32.c.inc" 1854 #else 1855 #define ELF_CLASS ELFCLASS64 1856 #define VDSO_HEADER "vdso-64.c.inc" 1857 #endif 1858 1859 #define ELF_HWCAP get_elf_hwcap() 1860 1861 static uint32_t get_elf_hwcap(void) 1862 { 1863 #define MISA_BIT(EXT) (1 << (EXT - 'A')) 1864 RISCVCPU *cpu = RISCV_CPU(thread_cpu); 1865 uint32_t mask = MISA_BIT('I') | MISA_BIT('M') | MISA_BIT('A') 1866 | MISA_BIT('F') | MISA_BIT('D') | MISA_BIT('C') 1867 | MISA_BIT('V'); 1868 1869 return cpu->env.misa_ext & mask; 1870 #undef MISA_BIT 1871 } 1872 1873 static inline void init_thread(struct target_pt_regs *regs, 1874 struct image_info *infop) 1875 { 1876 regs->sepc = infop->entry; 1877 regs->sp = infop->start_stack; 1878 } 1879 1880 #define ELF_EXEC_PAGESIZE 4096 1881 1882 #endif /* TARGET_RISCV */ 1883 1884 #ifdef TARGET_HPPA 1885 1886 #define ELF_CLASS ELFCLASS32 1887 #define ELF_ARCH EM_PARISC 1888 #define ELF_PLATFORM "PARISC" 1889 #define STACK_GROWS_DOWN 0 1890 #define STACK_ALIGNMENT 64 1891 1892 #define VDSO_HEADER "vdso.c.inc" 1893 1894 static inline void init_thread(struct target_pt_regs *regs, 1895 struct image_info *infop) 1896 { 1897 regs->iaoq[0] = infop->entry | PRIV_USER; 1898 regs->iaoq[1] = regs->iaoq[0] + 4; 1899 regs->gr[23] = 0; 1900 regs->gr[24] = infop->argv; 1901 regs->gr[25] = infop->argc; 1902 /* The top-of-stack contains a linkage buffer. */ 1903 regs->gr[30] = infop->start_stack + 64; 1904 regs->gr[31] = infop->entry; 1905 } 1906 1907 #define LO_COMMPAGE 0 1908 1909 static bool init_guest_commpage(void) 1910 { 1911 /* If reserved_va, then we have already mapped 0 page on the host. */ 1912 if (!reserved_va) { 1913 void *want, *addr; 1914 1915 want = g2h_untagged(LO_COMMPAGE); 1916 addr = mmap(want, TARGET_PAGE_SIZE, PROT_NONE, 1917 MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED_NOREPLACE, -1, 0); 1918 if (addr == MAP_FAILED) { 1919 perror("Allocating guest commpage"); 1920 exit(EXIT_FAILURE); 1921 } 1922 if (addr != want) { 1923 return false; 1924 } 1925 } 1926 1927 /* 1928 * On Linux, page zero is normally marked execute only + gateway. 1929 * Normal read or write is supposed to fail (thus PROT_NONE above), 1930 * but specific offsets have kernel code mapped to raise permissions 1931 * and implement syscalls. Here, simply mark the page executable. 1932 * Special case the entry points during translation (see do_page_zero). 1933 */ 1934 page_set_flags(LO_COMMPAGE, LO_COMMPAGE | ~TARGET_PAGE_MASK, 1935 PAGE_EXEC | PAGE_VALID); 1936 return true; 1937 } 1938 1939 #endif /* TARGET_HPPA */ 1940 1941 #ifdef TARGET_XTENSA 1942 1943 #define ELF_CLASS ELFCLASS32 1944 #define ELF_ARCH EM_XTENSA 1945 1946 static inline void init_thread(struct target_pt_regs *regs, 1947 struct image_info *infop) 1948 { 1949 regs->windowbase = 0; 1950 regs->windowstart = 1; 1951 regs->areg[1] = infop->start_stack; 1952 regs->pc = infop->entry; 1953 if (info_is_fdpic(infop)) { 1954 regs->areg[4] = infop->loadmap_addr; 1955 regs->areg[5] = infop->interpreter_loadmap_addr; 1956 if (infop->interpreter_loadmap_addr) { 1957 regs->areg[6] = infop->interpreter_pt_dynamic_addr; 1958 } else { 1959 regs->areg[6] = infop->pt_dynamic_addr; 1960 } 1961 } 1962 } 1963 1964 /* See linux kernel: arch/xtensa/include/asm/elf.h. */ 1965 #define ELF_NREG 128 1966 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; 1967 1968 enum { 1969 TARGET_REG_PC, 1970 TARGET_REG_PS, 1971 TARGET_REG_LBEG, 1972 TARGET_REG_LEND, 1973 TARGET_REG_LCOUNT, 1974 TARGET_REG_SAR, 1975 TARGET_REG_WINDOWSTART, 1976 TARGET_REG_WINDOWBASE, 1977 TARGET_REG_THREADPTR, 1978 TARGET_REG_AR0 = 64, 1979 }; 1980 1981 static void elf_core_copy_regs(target_elf_gregset_t *regs, 1982 const CPUXtensaState *env) 1983 { 1984 unsigned i; 1985 1986 (*regs)[TARGET_REG_PC] = tswapreg(env->pc); 1987 (*regs)[TARGET_REG_PS] = tswapreg(env->sregs[PS] & ~PS_EXCM); 1988 (*regs)[TARGET_REG_LBEG] = tswapreg(env->sregs[LBEG]); 1989 (*regs)[TARGET_REG_LEND] = tswapreg(env->sregs[LEND]); 1990 (*regs)[TARGET_REG_LCOUNT] = tswapreg(env->sregs[LCOUNT]); 1991 (*regs)[TARGET_REG_SAR] = tswapreg(env->sregs[SAR]); 1992 (*regs)[TARGET_REG_WINDOWSTART] = tswapreg(env->sregs[WINDOW_START]); 1993 (*regs)[TARGET_REG_WINDOWBASE] = tswapreg(env->sregs[WINDOW_BASE]); 1994 (*regs)[TARGET_REG_THREADPTR] = tswapreg(env->uregs[THREADPTR]); 1995 xtensa_sync_phys_from_window((CPUXtensaState *)env); 1996 for (i = 0; i < env->config->nareg; ++i) { 1997 (*regs)[TARGET_REG_AR0 + i] = tswapreg(env->phys_regs[i]); 1998 } 1999 } 2000 2001 #define USE_ELF_CORE_DUMP 2002 #define ELF_EXEC_PAGESIZE 4096 2003 2004 #endif /* TARGET_XTENSA */ 2005 2006 #ifdef TARGET_HEXAGON 2007 2008 #define ELF_CLASS ELFCLASS32 2009 #define ELF_ARCH EM_HEXAGON 2010 2011 static inline void init_thread(struct target_pt_regs *regs, 2012 struct image_info *infop) 2013 { 2014 regs->sepc = infop->entry; 2015 regs->sp = infop->start_stack; 2016 } 2017 2018 #endif /* TARGET_HEXAGON */ 2019 2020 #ifndef ELF_BASE_PLATFORM 2021 #define ELF_BASE_PLATFORM (NULL) 2022 #endif 2023 2024 #ifndef ELF_PLATFORM 2025 #define ELF_PLATFORM (NULL) 2026 #endif 2027 2028 #ifndef ELF_MACHINE 2029 #define ELF_MACHINE ELF_ARCH 2030 #endif 2031 2032 #ifndef elf_check_arch 2033 #define elf_check_arch(x) ((x) == ELF_ARCH) 2034 #endif 2035 2036 #ifndef elf_check_abi 2037 #define elf_check_abi(x) (1) 2038 #endif 2039 2040 #ifndef ELF_HWCAP 2041 #define ELF_HWCAP 0 2042 #endif 2043 2044 #ifndef STACK_GROWS_DOWN 2045 #define STACK_GROWS_DOWN 1 2046 #endif 2047 2048 #ifndef STACK_ALIGNMENT 2049 #define STACK_ALIGNMENT 16 2050 #endif 2051 2052 #ifdef TARGET_ABI32 2053 #undef ELF_CLASS 2054 #define ELF_CLASS ELFCLASS32 2055 #undef bswaptls 2056 #define bswaptls(ptr) bswap32s(ptr) 2057 #endif 2058 2059 #ifndef EXSTACK_DEFAULT 2060 #define EXSTACK_DEFAULT false 2061 #endif 2062 2063 #include "elf.h" 2064 2065 /* We must delay the following stanzas until after "elf.h". */ 2066 #if defined(TARGET_AARCH64) 2067 2068 static bool arch_parse_elf_property(uint32_t pr_type, uint32_t pr_datasz, 2069 const uint32_t *data, 2070 struct image_info *info, 2071 Error **errp) 2072 { 2073 if (pr_type == GNU_PROPERTY_AARCH64_FEATURE_1_AND) { 2074 if (pr_datasz != sizeof(uint32_t)) { 2075 error_setg(errp, "Ill-formed GNU_PROPERTY_AARCH64_FEATURE_1_AND"); 2076 return false; 2077 } 2078 /* We will extract GNU_PROPERTY_AARCH64_FEATURE_1_BTI later. */ 2079 info->note_flags = *data; 2080 } 2081 return true; 2082 } 2083 #define ARCH_USE_GNU_PROPERTY 1 2084 2085 #else 2086 2087 static bool arch_parse_elf_property(uint32_t pr_type, uint32_t pr_datasz, 2088 const uint32_t *data, 2089 struct image_info *info, 2090 Error **errp) 2091 { 2092 g_assert_not_reached(); 2093 } 2094 #define ARCH_USE_GNU_PROPERTY 0 2095 2096 #endif 2097 2098 struct exec 2099 { 2100 unsigned int a_info; /* Use macros N_MAGIC, etc for access */ 2101 unsigned int a_text; /* length of text, in bytes */ 2102 unsigned int a_data; /* length of data, in bytes */ 2103 unsigned int a_bss; /* length of uninitialized data area, in bytes */ 2104 unsigned int a_syms; /* length of symbol table data in file, in bytes */ 2105 unsigned int a_entry; /* start address */ 2106 unsigned int a_trsize; /* length of relocation info for text, in bytes */ 2107 unsigned int a_drsize; /* length of relocation info for data, in bytes */ 2108 }; 2109 2110 2111 #define N_MAGIC(exec) ((exec).a_info & 0xffff) 2112 #define OMAGIC 0407 2113 #define NMAGIC 0410 2114 #define ZMAGIC 0413 2115 #define QMAGIC 0314 2116 2117 #define DLINFO_ITEMS 16 2118 2119 static inline void memcpy_fromfs(void * to, const void * from, unsigned long n) 2120 { 2121 memcpy(to, from, n); 2122 } 2123 2124 #ifdef BSWAP_NEEDED 2125 static void bswap_ehdr(struct elfhdr *ehdr) 2126 { 2127 bswap16s(&ehdr->e_type); /* Object file type */ 2128 bswap16s(&ehdr->e_machine); /* Architecture */ 2129 bswap32s(&ehdr->e_version); /* Object file version */ 2130 bswaptls(&ehdr->e_entry); /* Entry point virtual address */ 2131 bswaptls(&ehdr->e_phoff); /* Program header table file offset */ 2132 bswaptls(&ehdr->e_shoff); /* Section header table file offset */ 2133 bswap32s(&ehdr->e_flags); /* Processor-specific flags */ 2134 bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */ 2135 bswap16s(&ehdr->e_phentsize); /* Program header table entry size */ 2136 bswap16s(&ehdr->e_phnum); /* Program header table entry count */ 2137 bswap16s(&ehdr->e_shentsize); /* Section header table entry size */ 2138 bswap16s(&ehdr->e_shnum); /* Section header table entry count */ 2139 bswap16s(&ehdr->e_shstrndx); /* Section header string table index */ 2140 } 2141 2142 static void bswap_phdr(struct elf_phdr *phdr, int phnum) 2143 { 2144 int i; 2145 for (i = 0; i < phnum; ++i, ++phdr) { 2146 bswap32s(&phdr->p_type); /* Segment type */ 2147 bswap32s(&phdr->p_flags); /* Segment flags */ 2148 bswaptls(&phdr->p_offset); /* Segment file offset */ 2149 bswaptls(&phdr->p_vaddr); /* Segment virtual address */ 2150 bswaptls(&phdr->p_paddr); /* Segment physical address */ 2151 bswaptls(&phdr->p_filesz); /* Segment size in file */ 2152 bswaptls(&phdr->p_memsz); /* Segment size in memory */ 2153 bswaptls(&phdr->p_align); /* Segment alignment */ 2154 } 2155 } 2156 2157 static void bswap_shdr(struct elf_shdr *shdr, int shnum) 2158 { 2159 int i; 2160 for (i = 0; i < shnum; ++i, ++shdr) { 2161 bswap32s(&shdr->sh_name); 2162 bswap32s(&shdr->sh_type); 2163 bswaptls(&shdr->sh_flags); 2164 bswaptls(&shdr->sh_addr); 2165 bswaptls(&shdr->sh_offset); 2166 bswaptls(&shdr->sh_size); 2167 bswap32s(&shdr->sh_link); 2168 bswap32s(&shdr->sh_info); 2169 bswaptls(&shdr->sh_addralign); 2170 bswaptls(&shdr->sh_entsize); 2171 } 2172 } 2173 2174 static void bswap_sym(struct elf_sym *sym) 2175 { 2176 bswap32s(&sym->st_name); 2177 bswaptls(&sym->st_value); 2178 bswaptls(&sym->st_size); 2179 bswap16s(&sym->st_shndx); 2180 } 2181 2182 #ifdef TARGET_MIPS 2183 static void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags) 2184 { 2185 bswap16s(&abiflags->version); 2186 bswap32s(&abiflags->ases); 2187 bswap32s(&abiflags->isa_ext); 2188 bswap32s(&abiflags->flags1); 2189 bswap32s(&abiflags->flags2); 2190 } 2191 #endif 2192 #else 2193 static inline void bswap_ehdr(struct elfhdr *ehdr) { } 2194 static inline void bswap_phdr(struct elf_phdr *phdr, int phnum) { } 2195 static inline void bswap_shdr(struct elf_shdr *shdr, int shnum) { } 2196 static inline void bswap_sym(struct elf_sym *sym) { } 2197 #ifdef TARGET_MIPS 2198 static inline void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags) { } 2199 #endif 2200 #endif 2201 2202 #ifdef USE_ELF_CORE_DUMP 2203 static int elf_core_dump(int, const CPUArchState *); 2204 #endif /* USE_ELF_CORE_DUMP */ 2205 static void load_symbols(struct elfhdr *hdr, const ImageSource *src, 2206 abi_ulong load_bias); 2207 2208 /* Verify the portions of EHDR within E_IDENT for the target. 2209 This can be performed before bswapping the entire header. */ 2210 static bool elf_check_ident(struct elfhdr *ehdr) 2211 { 2212 return (ehdr->e_ident[EI_MAG0] == ELFMAG0 2213 && ehdr->e_ident[EI_MAG1] == ELFMAG1 2214 && ehdr->e_ident[EI_MAG2] == ELFMAG2 2215 && ehdr->e_ident[EI_MAG3] == ELFMAG3 2216 && ehdr->e_ident[EI_CLASS] == ELF_CLASS 2217 && ehdr->e_ident[EI_DATA] == ELF_DATA 2218 && ehdr->e_ident[EI_VERSION] == EV_CURRENT); 2219 } 2220 2221 /* Verify the portions of EHDR outside of E_IDENT for the target. 2222 This has to wait until after bswapping the header. */ 2223 static bool elf_check_ehdr(struct elfhdr *ehdr) 2224 { 2225 return (elf_check_arch(ehdr->e_machine) 2226 && elf_check_abi(ehdr->e_flags) 2227 && ehdr->e_ehsize == sizeof(struct elfhdr) 2228 && ehdr->e_phentsize == sizeof(struct elf_phdr) 2229 && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN)); 2230 } 2231 2232 /* 2233 * 'copy_elf_strings()' copies argument/envelope strings from user 2234 * memory to free pages in kernel mem. These are in a format ready 2235 * to be put directly into the top of new user memory. 2236 * 2237 */ 2238 static abi_ulong copy_elf_strings(int argc, char **argv, char *scratch, 2239 abi_ulong p, abi_ulong stack_limit) 2240 { 2241 char *tmp; 2242 int len, i; 2243 abi_ulong top = p; 2244 2245 if (!p) { 2246 return 0; /* bullet-proofing */ 2247 } 2248 2249 if (STACK_GROWS_DOWN) { 2250 int offset = ((p - 1) % TARGET_PAGE_SIZE) + 1; 2251 for (i = argc - 1; i >= 0; --i) { 2252 tmp = argv[i]; 2253 if (!tmp) { 2254 fprintf(stderr, "VFS: argc is wrong"); 2255 exit(-1); 2256 } 2257 len = strlen(tmp) + 1; 2258 tmp += len; 2259 2260 if (len > (p - stack_limit)) { 2261 return 0; 2262 } 2263 while (len) { 2264 int bytes_to_copy = (len > offset) ? offset : len; 2265 tmp -= bytes_to_copy; 2266 p -= bytes_to_copy; 2267 offset -= bytes_to_copy; 2268 len -= bytes_to_copy; 2269 2270 memcpy_fromfs(scratch + offset, tmp, bytes_to_copy); 2271 2272 if (offset == 0) { 2273 memcpy_to_target(p, scratch, top - p); 2274 top = p; 2275 offset = TARGET_PAGE_SIZE; 2276 } 2277 } 2278 } 2279 if (p != top) { 2280 memcpy_to_target(p, scratch + offset, top - p); 2281 } 2282 } else { 2283 int remaining = TARGET_PAGE_SIZE - (p % TARGET_PAGE_SIZE); 2284 for (i = 0; i < argc; ++i) { 2285 tmp = argv[i]; 2286 if (!tmp) { 2287 fprintf(stderr, "VFS: argc is wrong"); 2288 exit(-1); 2289 } 2290 len = strlen(tmp) + 1; 2291 if (len > (stack_limit - p)) { 2292 return 0; 2293 } 2294 while (len) { 2295 int bytes_to_copy = (len > remaining) ? remaining : len; 2296 2297 memcpy_fromfs(scratch + (p - top), tmp, bytes_to_copy); 2298 2299 tmp += bytes_to_copy; 2300 remaining -= bytes_to_copy; 2301 p += bytes_to_copy; 2302 len -= bytes_to_copy; 2303 2304 if (remaining == 0) { 2305 memcpy_to_target(top, scratch, p - top); 2306 top = p; 2307 remaining = TARGET_PAGE_SIZE; 2308 } 2309 } 2310 } 2311 if (p != top) { 2312 memcpy_to_target(top, scratch, p - top); 2313 } 2314 } 2315 2316 return p; 2317 } 2318 2319 /* Older linux kernels provide up to MAX_ARG_PAGES (default: 32) of 2320 * argument/environment space. Newer kernels (>2.6.33) allow more, 2321 * dependent on stack size, but guarantee at least 32 pages for 2322 * backwards compatibility. 2323 */ 2324 #define STACK_LOWER_LIMIT (32 * TARGET_PAGE_SIZE) 2325 2326 static abi_ulong setup_arg_pages(struct linux_binprm *bprm, 2327 struct image_info *info) 2328 { 2329 abi_ulong size, error, guard; 2330 int prot; 2331 2332 size = guest_stack_size; 2333 if (size < STACK_LOWER_LIMIT) { 2334 size = STACK_LOWER_LIMIT; 2335 } 2336 2337 if (STACK_GROWS_DOWN) { 2338 guard = TARGET_PAGE_SIZE; 2339 if (guard < qemu_real_host_page_size()) { 2340 guard = qemu_real_host_page_size(); 2341 } 2342 } else { 2343 /* no guard page for hppa target where stack grows upwards. */ 2344 guard = 0; 2345 } 2346 2347 prot = PROT_READ | PROT_WRITE; 2348 if (info->exec_stack) { 2349 prot |= PROT_EXEC; 2350 } 2351 error = target_mmap(0, size + guard, prot, 2352 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 2353 if (error == -1) { 2354 perror("mmap stack"); 2355 exit(-1); 2356 } 2357 2358 /* We reserve one extra page at the top of the stack as guard. */ 2359 if (STACK_GROWS_DOWN) { 2360 target_mprotect(error, guard, PROT_NONE); 2361 info->stack_limit = error + guard; 2362 return info->stack_limit + size - sizeof(void *); 2363 } else { 2364 info->stack_limit = error + size; 2365 return error; 2366 } 2367 } 2368 2369 /** 2370 * zero_bss: 2371 * 2372 * Map and zero the bss. We need to explicitly zero any fractional pages 2373 * after the data section (i.e. bss). Return false on mapping failure. 2374 */ 2375 static bool zero_bss(abi_ulong start_bss, abi_ulong end_bss, 2376 int prot, Error **errp) 2377 { 2378 abi_ulong align_bss; 2379 2380 /* We only expect writable bss; the code segment shouldn't need this. */ 2381 if (!(prot & PROT_WRITE)) { 2382 error_setg(errp, "PT_LOAD with non-writable bss"); 2383 return false; 2384 } 2385 2386 align_bss = TARGET_PAGE_ALIGN(start_bss); 2387 end_bss = TARGET_PAGE_ALIGN(end_bss); 2388 2389 if (start_bss < align_bss) { 2390 int flags = page_get_flags(start_bss); 2391 2392 if (!(flags & PAGE_RWX)) { 2393 /* 2394 * The whole address space of the executable was reserved 2395 * at the start, therefore all pages will be VALID. 2396 * But assuming there are no PROT_NONE PT_LOAD segments, 2397 * a PROT_NONE page means no data all bss, and we can 2398 * simply extend the new anon mapping back to the start 2399 * of the page of bss. 2400 */ 2401 align_bss -= TARGET_PAGE_SIZE; 2402 } else { 2403 /* 2404 * The start of the bss shares a page with something. 2405 * The only thing that we expect is the data section, 2406 * which would already be marked writable. 2407 * Overlapping the RX code segment seems malformed. 2408 */ 2409 if (!(flags & PAGE_WRITE)) { 2410 error_setg(errp, "PT_LOAD with bss overlapping " 2411 "non-writable page"); 2412 return false; 2413 } 2414 2415 /* The page is already mapped and writable. */ 2416 memset(g2h_untagged(start_bss), 0, align_bss - start_bss); 2417 } 2418 } 2419 2420 if (align_bss < end_bss && 2421 target_mmap(align_bss, end_bss - align_bss, prot, 2422 MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0) == -1) { 2423 error_setg_errno(errp, errno, "Error mapping bss"); 2424 return false; 2425 } 2426 return true; 2427 } 2428 2429 #if defined(TARGET_ARM) 2430 static int elf_is_fdpic(struct elfhdr *exec) 2431 { 2432 return exec->e_ident[EI_OSABI] == ELFOSABI_ARM_FDPIC; 2433 } 2434 #elif defined(TARGET_XTENSA) 2435 static int elf_is_fdpic(struct elfhdr *exec) 2436 { 2437 return exec->e_ident[EI_OSABI] == ELFOSABI_XTENSA_FDPIC; 2438 } 2439 #else 2440 /* Default implementation, always false. */ 2441 static int elf_is_fdpic(struct elfhdr *exec) 2442 { 2443 return 0; 2444 } 2445 #endif 2446 2447 static abi_ulong loader_build_fdpic_loadmap(struct image_info *info, abi_ulong sp) 2448 { 2449 uint16_t n; 2450 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs; 2451 2452 /* elf32_fdpic_loadseg */ 2453 n = info->nsegs; 2454 while (n--) { 2455 sp -= 12; 2456 put_user_u32(loadsegs[n].addr, sp+0); 2457 put_user_u32(loadsegs[n].p_vaddr, sp+4); 2458 put_user_u32(loadsegs[n].p_memsz, sp+8); 2459 } 2460 2461 /* elf32_fdpic_loadmap */ 2462 sp -= 4; 2463 put_user_u16(0, sp+0); /* version */ 2464 put_user_u16(info->nsegs, sp+2); /* nsegs */ 2465 2466 info->personality = PER_LINUX_FDPIC; 2467 info->loadmap_addr = sp; 2468 2469 return sp; 2470 } 2471 2472 static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc, 2473 struct elfhdr *exec, 2474 struct image_info *info, 2475 struct image_info *interp_info, 2476 struct image_info *vdso_info) 2477 { 2478 abi_ulong sp; 2479 abi_ulong u_argc, u_argv, u_envp, u_auxv; 2480 int size; 2481 int i; 2482 abi_ulong u_rand_bytes; 2483 uint8_t k_rand_bytes[16]; 2484 abi_ulong u_platform, u_base_platform; 2485 const char *k_platform, *k_base_platform; 2486 const int n = sizeof(elf_addr_t); 2487 2488 sp = p; 2489 2490 /* Needs to be before we load the env/argc/... */ 2491 if (elf_is_fdpic(exec)) { 2492 /* Need 4 byte alignment for these structs */ 2493 sp &= ~3; 2494 sp = loader_build_fdpic_loadmap(info, sp); 2495 info->other_info = interp_info; 2496 if (interp_info) { 2497 interp_info->other_info = info; 2498 sp = loader_build_fdpic_loadmap(interp_info, sp); 2499 info->interpreter_loadmap_addr = interp_info->loadmap_addr; 2500 info->interpreter_pt_dynamic_addr = interp_info->pt_dynamic_addr; 2501 } else { 2502 info->interpreter_loadmap_addr = 0; 2503 info->interpreter_pt_dynamic_addr = 0; 2504 } 2505 } 2506 2507 u_base_platform = 0; 2508 k_base_platform = ELF_BASE_PLATFORM; 2509 if (k_base_platform) { 2510 size_t len = strlen(k_base_platform) + 1; 2511 if (STACK_GROWS_DOWN) { 2512 sp -= (len + n - 1) & ~(n - 1); 2513 u_base_platform = sp; 2514 /* FIXME - check return value of memcpy_to_target() for failure */ 2515 memcpy_to_target(sp, k_base_platform, len); 2516 } else { 2517 memcpy_to_target(sp, k_base_platform, len); 2518 u_base_platform = sp; 2519 sp += len + 1; 2520 } 2521 } 2522 2523 u_platform = 0; 2524 k_platform = ELF_PLATFORM; 2525 if (k_platform) { 2526 size_t len = strlen(k_platform) + 1; 2527 if (STACK_GROWS_DOWN) { 2528 sp -= (len + n - 1) & ~(n - 1); 2529 u_platform = sp; 2530 /* FIXME - check return value of memcpy_to_target() for failure */ 2531 memcpy_to_target(sp, k_platform, len); 2532 } else { 2533 memcpy_to_target(sp, k_platform, len); 2534 u_platform = sp; 2535 sp += len + 1; 2536 } 2537 } 2538 2539 /* Provide 16 byte alignment for the PRNG, and basic alignment for 2540 * the argv and envp pointers. 2541 */ 2542 if (STACK_GROWS_DOWN) { 2543 sp = QEMU_ALIGN_DOWN(sp, 16); 2544 } else { 2545 sp = QEMU_ALIGN_UP(sp, 16); 2546 } 2547 2548 /* 2549 * Generate 16 random bytes for userspace PRNG seeding. 2550 */ 2551 qemu_guest_getrandom_nofail(k_rand_bytes, sizeof(k_rand_bytes)); 2552 if (STACK_GROWS_DOWN) { 2553 sp -= 16; 2554 u_rand_bytes = sp; 2555 /* FIXME - check return value of memcpy_to_target() for failure */ 2556 memcpy_to_target(sp, k_rand_bytes, 16); 2557 } else { 2558 memcpy_to_target(sp, k_rand_bytes, 16); 2559 u_rand_bytes = sp; 2560 sp += 16; 2561 } 2562 2563 size = (DLINFO_ITEMS + 1) * 2; 2564 if (k_base_platform) { 2565 size += 2; 2566 } 2567 if (k_platform) { 2568 size += 2; 2569 } 2570 if (vdso_info) { 2571 size += 2; 2572 } 2573 #ifdef DLINFO_ARCH_ITEMS 2574 size += DLINFO_ARCH_ITEMS * 2; 2575 #endif 2576 #ifdef ELF_HWCAP2 2577 size += 2; 2578 #endif 2579 info->auxv_len = size * n; 2580 2581 size += envc + argc + 2; 2582 size += 1; /* argc itself */ 2583 size *= n; 2584 2585 /* Allocate space and finalize stack alignment for entry now. */ 2586 if (STACK_GROWS_DOWN) { 2587 u_argc = QEMU_ALIGN_DOWN(sp - size, STACK_ALIGNMENT); 2588 sp = u_argc; 2589 } else { 2590 u_argc = sp; 2591 sp = QEMU_ALIGN_UP(sp + size, STACK_ALIGNMENT); 2592 } 2593 2594 u_argv = u_argc + n; 2595 u_envp = u_argv + (argc + 1) * n; 2596 u_auxv = u_envp + (envc + 1) * n; 2597 info->saved_auxv = u_auxv; 2598 info->argc = argc; 2599 info->envc = envc; 2600 info->argv = u_argv; 2601 info->envp = u_envp; 2602 2603 /* This is correct because Linux defines 2604 * elf_addr_t as Elf32_Off / Elf64_Off 2605 */ 2606 #define NEW_AUX_ENT(id, val) do { \ 2607 put_user_ual(id, u_auxv); u_auxv += n; \ 2608 put_user_ual(val, u_auxv); u_auxv += n; \ 2609 } while(0) 2610 2611 #ifdef ARCH_DLINFO 2612 /* 2613 * ARCH_DLINFO must come first so platform specific code can enforce 2614 * special alignment requirements on the AUXV if necessary (eg. PPC). 2615 */ 2616 ARCH_DLINFO; 2617 #endif 2618 /* There must be exactly DLINFO_ITEMS entries here, or the assert 2619 * on info->auxv_len will trigger. 2620 */ 2621 NEW_AUX_ENT(AT_PHDR, (abi_ulong)(info->load_addr + exec->e_phoff)); 2622 NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr))); 2623 NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum)); 2624 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE)); 2625 NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_info ? interp_info->load_addr : 0)); 2626 NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0); 2627 NEW_AUX_ENT(AT_ENTRY, info->entry); 2628 NEW_AUX_ENT(AT_UID, (abi_ulong) getuid()); 2629 NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid()); 2630 NEW_AUX_ENT(AT_GID, (abi_ulong) getgid()); 2631 NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid()); 2632 NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP); 2633 NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK)); 2634 NEW_AUX_ENT(AT_RANDOM, (abi_ulong) u_rand_bytes); 2635 NEW_AUX_ENT(AT_SECURE, (abi_ulong) qemu_getauxval(AT_SECURE)); 2636 NEW_AUX_ENT(AT_EXECFN, info->file_string); 2637 2638 #ifdef ELF_HWCAP2 2639 NEW_AUX_ENT(AT_HWCAP2, (abi_ulong) ELF_HWCAP2); 2640 #endif 2641 2642 if (u_base_platform) { 2643 NEW_AUX_ENT(AT_BASE_PLATFORM, u_base_platform); 2644 } 2645 if (u_platform) { 2646 NEW_AUX_ENT(AT_PLATFORM, u_platform); 2647 } 2648 if (vdso_info) { 2649 NEW_AUX_ENT(AT_SYSINFO_EHDR, vdso_info->load_addr); 2650 } 2651 NEW_AUX_ENT (AT_NULL, 0); 2652 #undef NEW_AUX_ENT 2653 2654 /* Check that our initial calculation of the auxv length matches how much 2655 * we actually put into it. 2656 */ 2657 assert(info->auxv_len == u_auxv - info->saved_auxv); 2658 2659 put_user_ual(argc, u_argc); 2660 2661 p = info->arg_strings; 2662 for (i = 0; i < argc; ++i) { 2663 put_user_ual(p, u_argv); 2664 u_argv += n; 2665 p += target_strlen(p) + 1; 2666 } 2667 put_user_ual(0, u_argv); 2668 2669 p = info->env_strings; 2670 for (i = 0; i < envc; ++i) { 2671 put_user_ual(p, u_envp); 2672 u_envp += n; 2673 p += target_strlen(p) + 1; 2674 } 2675 put_user_ual(0, u_envp); 2676 2677 return sp; 2678 } 2679 2680 #if defined(HI_COMMPAGE) 2681 #define LO_COMMPAGE -1 2682 #elif defined(LO_COMMPAGE) 2683 #define HI_COMMPAGE 0 2684 #else 2685 #define HI_COMMPAGE 0 2686 #define LO_COMMPAGE -1 2687 #ifndef INIT_GUEST_COMMPAGE 2688 #define init_guest_commpage() true 2689 #endif 2690 #endif 2691 2692 /** 2693 * pgb_try_mmap: 2694 * @addr: host start address 2695 * @addr_last: host last address 2696 * @keep: do not unmap the probe region 2697 * 2698 * Return 1 if [@addr, @addr_last] is not mapped in the host, 2699 * return 0 if it is not available to map, and -1 on mmap error. 2700 * If @keep, the region is left mapped on success, otherwise unmapped. 2701 */ 2702 static int pgb_try_mmap(uintptr_t addr, uintptr_t addr_last, bool keep) 2703 { 2704 size_t size = addr_last - addr + 1; 2705 void *p = mmap((void *)addr, size, PROT_NONE, 2706 MAP_ANONYMOUS | MAP_PRIVATE | 2707 MAP_NORESERVE | MAP_FIXED_NOREPLACE, -1, 0); 2708 int ret; 2709 2710 if (p == MAP_FAILED) { 2711 return errno == EEXIST ? 0 : -1; 2712 } 2713 ret = p == (void *)addr; 2714 if (!keep || !ret) { 2715 munmap(p, size); 2716 } 2717 return ret; 2718 } 2719 2720 /** 2721 * pgb_try_mmap_skip_brk(uintptr_t addr, uintptr_t size, uintptr_t brk) 2722 * @addr: host address 2723 * @addr_last: host last address 2724 * @brk: host brk 2725 * 2726 * Like pgb_try_mmap, but additionally reserve some memory following brk. 2727 */ 2728 static int pgb_try_mmap_skip_brk(uintptr_t addr, uintptr_t addr_last, 2729 uintptr_t brk, bool keep) 2730 { 2731 uintptr_t brk_last = brk + 16 * MiB - 1; 2732 2733 /* Do not map anything close to the host brk. */ 2734 if (addr <= brk_last && brk <= addr_last) { 2735 return 0; 2736 } 2737 return pgb_try_mmap(addr, addr_last, keep); 2738 } 2739 2740 /** 2741 * pgb_try_mmap_set: 2742 * @ga: set of guest addrs 2743 * @base: guest_base 2744 * @brk: host brk 2745 * 2746 * Return true if all @ga can be mapped by the host at @base. 2747 * On success, retain the mapping at index 0 for reserved_va. 2748 */ 2749 2750 typedef struct PGBAddrs { 2751 uintptr_t bounds[3][2]; /* start/last pairs */ 2752 int nbounds; 2753 } PGBAddrs; 2754 2755 static bool pgb_try_mmap_set(const PGBAddrs *ga, uintptr_t base, uintptr_t brk) 2756 { 2757 for (int i = ga->nbounds - 1; i >= 0; --i) { 2758 if (pgb_try_mmap_skip_brk(ga->bounds[i][0] + base, 2759 ga->bounds[i][1] + base, 2760 brk, i == 0 && reserved_va) <= 0) { 2761 return false; 2762 } 2763 } 2764 return true; 2765 } 2766 2767 /** 2768 * pgb_addr_set: 2769 * @ga: output set of guest addrs 2770 * @guest_loaddr: guest image low address 2771 * @guest_loaddr: guest image high address 2772 * @identity: create for identity mapping 2773 * 2774 * Fill in @ga with the image, COMMPAGE and NULL page. 2775 */ 2776 static bool pgb_addr_set(PGBAddrs *ga, abi_ulong guest_loaddr, 2777 abi_ulong guest_hiaddr, bool try_identity) 2778 { 2779 int n; 2780 2781 /* 2782 * With a low commpage, or a guest mapped very low, 2783 * we may not be able to use the identity map. 2784 */ 2785 if (try_identity) { 2786 if (LO_COMMPAGE != -1 && LO_COMMPAGE < mmap_min_addr) { 2787 return false; 2788 } 2789 if (guest_loaddr != 0 && guest_loaddr < mmap_min_addr) { 2790 return false; 2791 } 2792 } 2793 2794 memset(ga, 0, sizeof(*ga)); 2795 n = 0; 2796 2797 if (reserved_va) { 2798 ga->bounds[n][0] = try_identity ? mmap_min_addr : 0; 2799 ga->bounds[n][1] = reserved_va; 2800 n++; 2801 /* LO_COMMPAGE and NULL handled by reserving from 0. */ 2802 } else { 2803 /* Add any LO_COMMPAGE or NULL page. */ 2804 if (LO_COMMPAGE != -1) { 2805 ga->bounds[n][0] = 0; 2806 ga->bounds[n][1] = LO_COMMPAGE + TARGET_PAGE_SIZE - 1; 2807 n++; 2808 } else if (!try_identity) { 2809 ga->bounds[n][0] = 0; 2810 ga->bounds[n][1] = TARGET_PAGE_SIZE - 1; 2811 n++; 2812 } 2813 2814 /* Add the guest image for ET_EXEC. */ 2815 if (guest_loaddr) { 2816 ga->bounds[n][0] = guest_loaddr; 2817 ga->bounds[n][1] = guest_hiaddr; 2818 n++; 2819 } 2820 } 2821 2822 /* 2823 * Temporarily disable 2824 * "comparison is always false due to limited range of data type" 2825 * due to comparison between unsigned and (possible) 0. 2826 */ 2827 #pragma GCC diagnostic push 2828 #pragma GCC diagnostic ignored "-Wtype-limits" 2829 2830 /* Add any HI_COMMPAGE not covered by reserved_va. */ 2831 if (reserved_va < HI_COMMPAGE) { 2832 ga->bounds[n][0] = HI_COMMPAGE & qemu_real_host_page_mask(); 2833 ga->bounds[n][1] = HI_COMMPAGE + TARGET_PAGE_SIZE - 1; 2834 n++; 2835 } 2836 2837 #pragma GCC diagnostic pop 2838 2839 ga->nbounds = n; 2840 return true; 2841 } 2842 2843 static void pgb_fail_in_use(const char *image_name) 2844 { 2845 error_report("%s: requires virtual address space that is in use " 2846 "(omit the -B option or choose a different value)", 2847 image_name); 2848 exit(EXIT_FAILURE); 2849 } 2850 2851 static void pgb_fixed(const char *image_name, uintptr_t guest_loaddr, 2852 uintptr_t guest_hiaddr, uintptr_t align) 2853 { 2854 PGBAddrs ga; 2855 uintptr_t brk = (uintptr_t)sbrk(0); 2856 2857 if (!QEMU_IS_ALIGNED(guest_base, align)) { 2858 fprintf(stderr, "Requested guest base %p does not satisfy " 2859 "host minimum alignment (0x%" PRIxPTR ")\n", 2860 (void *)guest_base, align); 2861 exit(EXIT_FAILURE); 2862 } 2863 2864 if (!pgb_addr_set(&ga, guest_loaddr, guest_hiaddr, !guest_base) 2865 || !pgb_try_mmap_set(&ga, guest_base, brk)) { 2866 pgb_fail_in_use(image_name); 2867 } 2868 } 2869 2870 /** 2871 * pgb_find_fallback: 2872 * 2873 * This is a fallback method for finding holes in the host address space 2874 * if we don't have the benefit of being able to access /proc/self/map. 2875 * It can potentially take a very long time as we can only dumbly iterate 2876 * up the host address space seeing if the allocation would work. 2877 */ 2878 static uintptr_t pgb_find_fallback(const PGBAddrs *ga, uintptr_t align, 2879 uintptr_t brk) 2880 { 2881 /* TODO: come up with a better estimate of how much to skip. */ 2882 uintptr_t skip = sizeof(uintptr_t) == 4 ? MiB : GiB; 2883 2884 for (uintptr_t base = skip; ; base += skip) { 2885 base = ROUND_UP(base, align); 2886 if (pgb_try_mmap_set(ga, base, brk)) { 2887 return base; 2888 } 2889 if (base >= -skip) { 2890 return -1; 2891 } 2892 } 2893 } 2894 2895 static uintptr_t pgb_try_itree(const PGBAddrs *ga, uintptr_t base, 2896 IntervalTreeRoot *root) 2897 { 2898 for (int i = ga->nbounds - 1; i >= 0; --i) { 2899 uintptr_t s = base + ga->bounds[i][0]; 2900 uintptr_t l = base + ga->bounds[i][1]; 2901 IntervalTreeNode *n; 2902 2903 if (l < s) { 2904 /* Wraparound. Skip to advance S to mmap_min_addr. */ 2905 return mmap_min_addr - s; 2906 } 2907 2908 n = interval_tree_iter_first(root, s, l); 2909 if (n != NULL) { 2910 /* Conflict. Skip to advance S to LAST + 1. */ 2911 return n->last - s + 1; 2912 } 2913 } 2914 return 0; /* success */ 2915 } 2916 2917 static uintptr_t pgb_find_itree(const PGBAddrs *ga, IntervalTreeRoot *root, 2918 uintptr_t align, uintptr_t brk) 2919 { 2920 uintptr_t last = sizeof(uintptr_t) == 4 ? MiB : GiB; 2921 uintptr_t base, skip; 2922 2923 while (true) { 2924 base = ROUND_UP(last, align); 2925 if (base < last) { 2926 return -1; 2927 } 2928 2929 skip = pgb_try_itree(ga, base, root); 2930 if (skip == 0) { 2931 break; 2932 } 2933 2934 last = base + skip; 2935 if (last < base) { 2936 return -1; 2937 } 2938 } 2939 2940 /* 2941 * We've chosen 'base' based on holes in the interval tree, 2942 * but we don't yet know if it is a valid host address. 2943 * Because it is the first matching hole, if the host addresses 2944 * are invalid we know there are no further matches. 2945 */ 2946 return pgb_try_mmap_set(ga, base, brk) ? base : -1; 2947 } 2948 2949 static void pgb_dynamic(const char *image_name, uintptr_t guest_loaddr, 2950 uintptr_t guest_hiaddr, uintptr_t align) 2951 { 2952 IntervalTreeRoot *root; 2953 uintptr_t brk, ret; 2954 PGBAddrs ga; 2955 2956 /* Try the identity map first. */ 2957 if (pgb_addr_set(&ga, guest_loaddr, guest_hiaddr, true)) { 2958 brk = (uintptr_t)sbrk(0); 2959 if (pgb_try_mmap_set(&ga, 0, brk)) { 2960 guest_base = 0; 2961 return; 2962 } 2963 } 2964 2965 /* 2966 * Rebuild the address set for non-identity map. 2967 * This differs in the mapping of the guest NULL page. 2968 */ 2969 pgb_addr_set(&ga, guest_loaddr, guest_hiaddr, false); 2970 2971 root = read_self_maps(); 2972 2973 /* Read brk after we've read the maps, which will malloc. */ 2974 brk = (uintptr_t)sbrk(0); 2975 2976 if (!root) { 2977 ret = pgb_find_fallback(&ga, align, brk); 2978 } else { 2979 /* 2980 * Reserve the area close to the host brk. 2981 * This will be freed with the rest of the tree. 2982 */ 2983 IntervalTreeNode *b = g_new0(IntervalTreeNode, 1); 2984 b->start = brk; 2985 b->last = brk + 16 * MiB - 1; 2986 interval_tree_insert(b, root); 2987 2988 ret = pgb_find_itree(&ga, root, align, brk); 2989 free_self_maps(root); 2990 } 2991 2992 if (ret == -1) { 2993 int w = TARGET_LONG_BITS / 4; 2994 2995 error_report("%s: Unable to find a guest_base to satisfy all " 2996 "guest address mapping requirements", image_name); 2997 2998 for (int i = 0; i < ga.nbounds; ++i) { 2999 error_printf(" %0*" PRIx64 "-%0*" PRIx64 "\n", 3000 w, (uint64_t)ga.bounds[i][0], 3001 w, (uint64_t)ga.bounds[i][1]); 3002 } 3003 exit(EXIT_FAILURE); 3004 } 3005 guest_base = ret; 3006 } 3007 3008 void probe_guest_base(const char *image_name, abi_ulong guest_loaddr, 3009 abi_ulong guest_hiaddr) 3010 { 3011 /* In order to use host shmat, we must be able to honor SHMLBA. */ 3012 uintptr_t align = MAX(SHMLBA, TARGET_PAGE_SIZE); 3013 3014 /* Sanity check the guest binary. */ 3015 if (reserved_va) { 3016 if (guest_hiaddr > reserved_va) { 3017 error_report("%s: requires more than reserved virtual " 3018 "address space (0x%" PRIx64 " > 0x%lx)", 3019 image_name, (uint64_t)guest_hiaddr, reserved_va); 3020 exit(EXIT_FAILURE); 3021 } 3022 } else { 3023 if (guest_hiaddr != (uintptr_t)guest_hiaddr) { 3024 error_report("%s: requires more virtual address space " 3025 "than the host can provide (0x%" PRIx64 ")", 3026 image_name, (uint64_t)guest_hiaddr + 1); 3027 exit(EXIT_FAILURE); 3028 } 3029 } 3030 3031 if (have_guest_base) { 3032 pgb_fixed(image_name, guest_loaddr, guest_hiaddr, align); 3033 } else { 3034 pgb_dynamic(image_name, guest_loaddr, guest_hiaddr, align); 3035 } 3036 3037 /* Reserve and initialize the commpage. */ 3038 if (!init_guest_commpage()) { 3039 /* We have already probed for the commpage being free. */ 3040 g_assert_not_reached(); 3041 } 3042 3043 assert(QEMU_IS_ALIGNED(guest_base, align)); 3044 qemu_log_mask(CPU_LOG_PAGE, "Locating guest address space " 3045 "@ 0x%" PRIx64 "\n", (uint64_t)guest_base); 3046 } 3047 3048 enum { 3049 /* The string "GNU\0" as a magic number. */ 3050 GNU0_MAGIC = const_le32('G' | 'N' << 8 | 'U' << 16), 3051 NOTE_DATA_SZ = 1 * KiB, 3052 NOTE_NAME_SZ = 4, 3053 ELF_GNU_PROPERTY_ALIGN = ELF_CLASS == ELFCLASS32 ? 4 : 8, 3054 }; 3055 3056 /* 3057 * Process a single gnu_property entry. 3058 * Return false for error. 3059 */ 3060 static bool parse_elf_property(const uint32_t *data, int *off, int datasz, 3061 struct image_info *info, bool have_prev_type, 3062 uint32_t *prev_type, Error **errp) 3063 { 3064 uint32_t pr_type, pr_datasz, step; 3065 3066 if (*off > datasz || !QEMU_IS_ALIGNED(*off, ELF_GNU_PROPERTY_ALIGN)) { 3067 goto error_data; 3068 } 3069 datasz -= *off; 3070 data += *off / sizeof(uint32_t); 3071 3072 if (datasz < 2 * sizeof(uint32_t)) { 3073 goto error_data; 3074 } 3075 pr_type = data[0]; 3076 pr_datasz = data[1]; 3077 data += 2; 3078 datasz -= 2 * sizeof(uint32_t); 3079 step = ROUND_UP(pr_datasz, ELF_GNU_PROPERTY_ALIGN); 3080 if (step > datasz) { 3081 goto error_data; 3082 } 3083 3084 /* Properties are supposed to be unique and sorted on pr_type. */ 3085 if (have_prev_type && pr_type <= *prev_type) { 3086 if (pr_type == *prev_type) { 3087 error_setg(errp, "Duplicate property in PT_GNU_PROPERTY"); 3088 } else { 3089 error_setg(errp, "Unsorted property in PT_GNU_PROPERTY"); 3090 } 3091 return false; 3092 } 3093 *prev_type = pr_type; 3094 3095 if (!arch_parse_elf_property(pr_type, pr_datasz, data, info, errp)) { 3096 return false; 3097 } 3098 3099 *off += 2 * sizeof(uint32_t) + step; 3100 return true; 3101 3102 error_data: 3103 error_setg(errp, "Ill-formed property in PT_GNU_PROPERTY"); 3104 return false; 3105 } 3106 3107 /* Process NT_GNU_PROPERTY_TYPE_0. */ 3108 static bool parse_elf_properties(const ImageSource *src, 3109 struct image_info *info, 3110 const struct elf_phdr *phdr, 3111 Error **errp) 3112 { 3113 union { 3114 struct elf_note nhdr; 3115 uint32_t data[NOTE_DATA_SZ / sizeof(uint32_t)]; 3116 } note; 3117 3118 int n, off, datasz; 3119 bool have_prev_type; 3120 uint32_t prev_type; 3121 3122 /* Unless the arch requires properties, ignore them. */ 3123 if (!ARCH_USE_GNU_PROPERTY) { 3124 return true; 3125 } 3126 3127 /* If the properties are crazy large, that's too bad. */ 3128 n = phdr->p_filesz; 3129 if (n > sizeof(note)) { 3130 error_setg(errp, "PT_GNU_PROPERTY too large"); 3131 return false; 3132 } 3133 if (n < sizeof(note.nhdr)) { 3134 error_setg(errp, "PT_GNU_PROPERTY too small"); 3135 return false; 3136 } 3137 3138 if (!imgsrc_read(¬e, phdr->p_offset, n, src, errp)) { 3139 return false; 3140 } 3141 3142 /* 3143 * The contents of a valid PT_GNU_PROPERTY is a sequence of uint32_t. 3144 * Swap most of them now, beyond the header and namesz. 3145 */ 3146 #ifdef BSWAP_NEEDED 3147 for (int i = 4; i < n / 4; i++) { 3148 bswap32s(note.data + i); 3149 } 3150 #endif 3151 3152 /* 3153 * Note that nhdr is 3 words, and that the "name" described by namesz 3154 * immediately follows nhdr and is thus at the 4th word. Further, all 3155 * of the inputs to the kernel's round_up are multiples of 4. 3156 */ 3157 if (tswap32(note.nhdr.n_type) != NT_GNU_PROPERTY_TYPE_0 || 3158 tswap32(note.nhdr.n_namesz) != NOTE_NAME_SZ || 3159 note.data[3] != GNU0_MAGIC) { 3160 error_setg(errp, "Invalid note in PT_GNU_PROPERTY"); 3161 return false; 3162 } 3163 off = sizeof(note.nhdr) + NOTE_NAME_SZ; 3164 3165 datasz = tswap32(note.nhdr.n_descsz) + off; 3166 if (datasz > n) { 3167 error_setg(errp, "Invalid note size in PT_GNU_PROPERTY"); 3168 return false; 3169 } 3170 3171 have_prev_type = false; 3172 prev_type = 0; 3173 while (1) { 3174 if (off == datasz) { 3175 return true; /* end, exit ok */ 3176 } 3177 if (!parse_elf_property(note.data, &off, datasz, info, 3178 have_prev_type, &prev_type, errp)) { 3179 return false; 3180 } 3181 have_prev_type = true; 3182 } 3183 } 3184 3185 /** 3186 * load_elf_image: Load an ELF image into the address space. 3187 * @image_name: the filename of the image, to use in error messages. 3188 * @src: the ImageSource from which to read. 3189 * @info: info collected from the loaded image. 3190 * @ehdr: the ELF header, not yet bswapped. 3191 * @pinterp_name: record any PT_INTERP string found. 3192 * 3193 * On return: @info values will be filled in, as necessary or available. 3194 */ 3195 3196 static void load_elf_image(const char *image_name, const ImageSource *src, 3197 struct image_info *info, struct elfhdr *ehdr, 3198 char **pinterp_name) 3199 { 3200 g_autofree struct elf_phdr *phdr = NULL; 3201 abi_ulong load_addr, load_bias, loaddr, hiaddr, error, align; 3202 size_t reserve_size, align_size; 3203 int i, prot_exec; 3204 Error *err = NULL; 3205 3206 /* 3207 * First of all, some simple consistency checks. 3208 * Note that we rely on the bswapped ehdr staying in bprm_buf, 3209 * for later use by load_elf_binary and create_elf_tables. 3210 */ 3211 if (!imgsrc_read(ehdr, 0, sizeof(*ehdr), src, &err)) { 3212 goto exit_errmsg; 3213 } 3214 if (!elf_check_ident(ehdr)) { 3215 error_setg(&err, "Invalid ELF image for this architecture"); 3216 goto exit_errmsg; 3217 } 3218 bswap_ehdr(ehdr); 3219 if (!elf_check_ehdr(ehdr)) { 3220 error_setg(&err, "Invalid ELF image for this architecture"); 3221 goto exit_errmsg; 3222 } 3223 3224 phdr = imgsrc_read_alloc(ehdr->e_phoff, 3225 ehdr->e_phnum * sizeof(struct elf_phdr), 3226 src, &err); 3227 if (phdr == NULL) { 3228 goto exit_errmsg; 3229 } 3230 bswap_phdr(phdr, ehdr->e_phnum); 3231 3232 info->nsegs = 0; 3233 info->pt_dynamic_addr = 0; 3234 3235 mmap_lock(); 3236 3237 /* 3238 * Find the maximum size of the image and allocate an appropriate 3239 * amount of memory to handle that. Locate the interpreter, if any. 3240 */ 3241 loaddr = -1, hiaddr = 0; 3242 align = 0; 3243 info->exec_stack = EXSTACK_DEFAULT; 3244 for (i = 0; i < ehdr->e_phnum; ++i) { 3245 struct elf_phdr *eppnt = phdr + i; 3246 if (eppnt->p_type == PT_LOAD) { 3247 abi_ulong a = eppnt->p_vaddr & TARGET_PAGE_MASK; 3248 if (a < loaddr) { 3249 loaddr = a; 3250 } 3251 a = eppnt->p_vaddr + eppnt->p_memsz - 1; 3252 if (a > hiaddr) { 3253 hiaddr = a; 3254 } 3255 ++info->nsegs; 3256 align |= eppnt->p_align; 3257 } else if (eppnt->p_type == PT_INTERP && pinterp_name) { 3258 g_autofree char *interp_name = NULL; 3259 3260 if (*pinterp_name) { 3261 error_setg(&err, "Multiple PT_INTERP entries"); 3262 goto exit_errmsg; 3263 } 3264 3265 interp_name = imgsrc_read_alloc(eppnt->p_offset, eppnt->p_filesz, 3266 src, &err); 3267 if (interp_name == NULL) { 3268 goto exit_errmsg; 3269 } 3270 if (interp_name[eppnt->p_filesz - 1] != 0) { 3271 error_setg(&err, "Invalid PT_INTERP entry"); 3272 goto exit_errmsg; 3273 } 3274 *pinterp_name = g_steal_pointer(&interp_name); 3275 } else if (eppnt->p_type == PT_GNU_PROPERTY) { 3276 if (!parse_elf_properties(src, info, eppnt, &err)) { 3277 goto exit_errmsg; 3278 } 3279 } else if (eppnt->p_type == PT_GNU_STACK) { 3280 info->exec_stack = eppnt->p_flags & PF_X; 3281 } 3282 } 3283 3284 load_addr = loaddr; 3285 3286 align = pow2ceil(align); 3287 3288 if (pinterp_name != NULL) { 3289 if (ehdr->e_type == ET_EXEC) { 3290 /* 3291 * Make sure that the low address does not conflict with 3292 * MMAP_MIN_ADDR or the QEMU application itself. 3293 */ 3294 probe_guest_base(image_name, loaddr, hiaddr); 3295 } else { 3296 /* 3297 * The binary is dynamic, but we still need to 3298 * select guest_base. In this case we pass a size. 3299 */ 3300 probe_guest_base(image_name, 0, hiaddr - loaddr); 3301 3302 /* 3303 * Avoid collision with the loader by providing a different 3304 * default load address. 3305 */ 3306 load_addr += elf_et_dyn_base; 3307 3308 /* 3309 * TODO: Better support for mmap alignment is desirable. 3310 * Since we do not have complete control over the guest 3311 * address space, we prefer the kernel to choose some address 3312 * rather than force the use of LOAD_ADDR via MAP_FIXED. 3313 */ 3314 if (align) { 3315 load_addr &= -align; 3316 } 3317 } 3318 } 3319 3320 /* 3321 * Reserve address space for all of this. 3322 * 3323 * In the case of ET_EXEC, we supply MAP_FIXED_NOREPLACE so that we get 3324 * exactly the address range that is required. Without reserved_va, 3325 * the guest address space is not isolated. We have attempted to avoid 3326 * conflict with the host program itself via probe_guest_base, but using 3327 * MAP_FIXED_NOREPLACE instead of MAP_FIXED provides an extra check. 3328 * 3329 * Otherwise this is ET_DYN, and we are searching for a location 3330 * that can hold the memory space required. If the image is 3331 * pre-linked, LOAD_ADDR will be non-zero, and the kernel should 3332 * honor that address if it happens to be free. 3333 * 3334 * In both cases, we will overwrite pages in this range with mappings 3335 * from the executable. 3336 */ 3337 reserve_size = (size_t)hiaddr - loaddr + 1; 3338 align_size = reserve_size; 3339 3340 if (ehdr->e_type != ET_EXEC && align > qemu_real_host_page_size()) { 3341 align_size += align - 1; 3342 } 3343 3344 load_addr = target_mmap(load_addr, align_size, PROT_NONE, 3345 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | 3346 (ehdr->e_type == ET_EXEC ? MAP_FIXED_NOREPLACE : 0), 3347 -1, 0); 3348 if (load_addr == -1) { 3349 goto exit_mmap; 3350 } 3351 3352 if (align_size != reserve_size) { 3353 abi_ulong align_addr = ROUND_UP(load_addr, align); 3354 abi_ulong align_end = TARGET_PAGE_ALIGN(align_addr + reserve_size); 3355 abi_ulong load_end = TARGET_PAGE_ALIGN(load_addr + align_size); 3356 3357 if (align_addr != load_addr) { 3358 target_munmap(load_addr, align_addr - load_addr); 3359 } 3360 if (align_end != load_end) { 3361 target_munmap(align_end, load_end - align_end); 3362 } 3363 load_addr = align_addr; 3364 } 3365 3366 load_bias = load_addr - loaddr; 3367 3368 if (elf_is_fdpic(ehdr)) { 3369 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs = 3370 g_malloc(sizeof(*loadsegs) * info->nsegs); 3371 3372 for (i = 0; i < ehdr->e_phnum; ++i) { 3373 switch (phdr[i].p_type) { 3374 case PT_DYNAMIC: 3375 info->pt_dynamic_addr = phdr[i].p_vaddr + load_bias; 3376 break; 3377 case PT_LOAD: 3378 loadsegs->addr = phdr[i].p_vaddr + load_bias; 3379 loadsegs->p_vaddr = phdr[i].p_vaddr; 3380 loadsegs->p_memsz = phdr[i].p_memsz; 3381 ++loadsegs; 3382 break; 3383 } 3384 } 3385 } 3386 3387 info->load_bias = load_bias; 3388 info->code_offset = load_bias; 3389 info->data_offset = load_bias; 3390 info->load_addr = load_addr; 3391 info->entry = ehdr->e_entry + load_bias; 3392 info->start_code = -1; 3393 info->end_code = 0; 3394 info->start_data = -1; 3395 info->end_data = 0; 3396 /* Usual start for brk is after all sections of the main executable. */ 3397 info->brk = TARGET_PAGE_ALIGN(hiaddr + load_bias); 3398 info->elf_flags = ehdr->e_flags; 3399 3400 prot_exec = PROT_EXEC; 3401 #ifdef TARGET_AARCH64 3402 /* 3403 * If the BTI feature is present, this indicates that the executable 3404 * pages of the startup binary should be mapped with PROT_BTI, so that 3405 * branch targets are enforced. 3406 * 3407 * The startup binary is either the interpreter or the static executable. 3408 * The interpreter is responsible for all pages of a dynamic executable. 3409 * 3410 * Elf notes are backward compatible to older cpus. 3411 * Do not enable BTI unless it is supported. 3412 */ 3413 if ((info->note_flags & GNU_PROPERTY_AARCH64_FEATURE_1_BTI) 3414 && (pinterp_name == NULL || *pinterp_name == 0) 3415 && cpu_isar_feature(aa64_bti, ARM_CPU(thread_cpu))) { 3416 prot_exec |= TARGET_PROT_BTI; 3417 } 3418 #endif 3419 3420 for (i = 0; i < ehdr->e_phnum; i++) { 3421 struct elf_phdr *eppnt = phdr + i; 3422 if (eppnt->p_type == PT_LOAD) { 3423 abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em; 3424 int elf_prot = 0; 3425 3426 if (eppnt->p_flags & PF_R) { 3427 elf_prot |= PROT_READ; 3428 } 3429 if (eppnt->p_flags & PF_W) { 3430 elf_prot |= PROT_WRITE; 3431 } 3432 if (eppnt->p_flags & PF_X) { 3433 elf_prot |= prot_exec; 3434 } 3435 3436 vaddr = load_bias + eppnt->p_vaddr; 3437 vaddr_po = vaddr & ~TARGET_PAGE_MASK; 3438 vaddr_ps = vaddr & TARGET_PAGE_MASK; 3439 3440 vaddr_ef = vaddr + eppnt->p_filesz; 3441 vaddr_em = vaddr + eppnt->p_memsz; 3442 3443 /* 3444 * Some segments may be completely empty, with a non-zero p_memsz 3445 * but no backing file segment. 3446 */ 3447 if (eppnt->p_filesz != 0) { 3448 error = imgsrc_mmap(vaddr_ps, eppnt->p_filesz + vaddr_po, 3449 elf_prot, MAP_PRIVATE | MAP_FIXED, 3450 src, eppnt->p_offset - vaddr_po); 3451 if (error == -1) { 3452 goto exit_mmap; 3453 } 3454 } 3455 3456 /* If the load segment requests extra zeros (e.g. bss), map it. */ 3457 if (vaddr_ef < vaddr_em && 3458 !zero_bss(vaddr_ef, vaddr_em, elf_prot, &err)) { 3459 goto exit_errmsg; 3460 } 3461 3462 /* Find the full program boundaries. */ 3463 if (elf_prot & PROT_EXEC) { 3464 if (vaddr < info->start_code) { 3465 info->start_code = vaddr; 3466 } 3467 if (vaddr_ef > info->end_code) { 3468 info->end_code = vaddr_ef; 3469 } 3470 } 3471 if (elf_prot & PROT_WRITE) { 3472 if (vaddr < info->start_data) { 3473 info->start_data = vaddr; 3474 } 3475 if (vaddr_ef > info->end_data) { 3476 info->end_data = vaddr_ef; 3477 } 3478 } 3479 #ifdef TARGET_MIPS 3480 } else if (eppnt->p_type == PT_MIPS_ABIFLAGS) { 3481 Mips_elf_abiflags_v0 abiflags; 3482 3483 if (!imgsrc_read(&abiflags, eppnt->p_offset, sizeof(abiflags), 3484 src, &err)) { 3485 goto exit_errmsg; 3486 } 3487 bswap_mips_abiflags(&abiflags); 3488 info->fp_abi = abiflags.fp_abi; 3489 #endif 3490 } 3491 } 3492 3493 if (info->end_data == 0) { 3494 info->start_data = info->end_code; 3495 info->end_data = info->end_code; 3496 } 3497 3498 if (qemu_log_enabled()) { 3499 load_symbols(ehdr, src, load_bias); 3500 } 3501 3502 debuginfo_report_elf(image_name, src->fd, load_bias); 3503 3504 mmap_unlock(); 3505 3506 close(src->fd); 3507 return; 3508 3509 exit_mmap: 3510 error_setg_errno(&err, errno, "Error mapping file"); 3511 goto exit_errmsg; 3512 exit_errmsg: 3513 error_reportf_err(err, "%s: ", image_name); 3514 exit(-1); 3515 } 3516 3517 static void load_elf_interp(const char *filename, struct image_info *info, 3518 char bprm_buf[BPRM_BUF_SIZE]) 3519 { 3520 struct elfhdr ehdr; 3521 ImageSource src; 3522 int fd, retval; 3523 Error *err = NULL; 3524 3525 fd = open(path(filename), O_RDONLY); 3526 if (fd < 0) { 3527 error_setg_file_open(&err, errno, filename); 3528 error_report_err(err); 3529 exit(-1); 3530 } 3531 3532 retval = read(fd, bprm_buf, BPRM_BUF_SIZE); 3533 if (retval < 0) { 3534 error_setg_errno(&err, errno, "Error reading file header"); 3535 error_reportf_err(err, "%s: ", filename); 3536 exit(-1); 3537 } 3538 3539 src.fd = fd; 3540 src.cache = bprm_buf; 3541 src.cache_size = retval; 3542 3543 load_elf_image(filename, &src, info, &ehdr, NULL); 3544 } 3545 3546 #ifndef vdso_image_info 3547 #ifdef VDSO_HEADER 3548 #include VDSO_HEADER 3549 #define vdso_image_info(flags) &vdso_image_info 3550 #else 3551 #define vdso_image_info(flags) NULL 3552 #endif /* VDSO_HEADER */ 3553 #endif /* vdso_image_info */ 3554 3555 static void load_elf_vdso(struct image_info *info, const VdsoImageInfo *vdso) 3556 { 3557 ImageSource src; 3558 struct elfhdr ehdr; 3559 abi_ulong load_bias, load_addr; 3560 3561 src.fd = -1; 3562 src.cache = vdso->image; 3563 src.cache_size = vdso->image_size; 3564 3565 load_elf_image("<internal-vdso>", &src, info, &ehdr, NULL); 3566 load_addr = info->load_addr; 3567 load_bias = info->load_bias; 3568 3569 /* 3570 * We need to relocate the VDSO image. The one built into the kernel 3571 * is built for a fixed address. The one built for QEMU is not, since 3572 * that requires close control of the guest address space. 3573 * We pre-processed the image to locate all of the addresses that need 3574 * to be updated. 3575 */ 3576 for (unsigned i = 0, n = vdso->reloc_count; i < n; i++) { 3577 abi_ulong *addr = g2h_untagged(load_addr + vdso->relocs[i]); 3578 *addr = tswapal(tswapal(*addr) + load_bias); 3579 } 3580 3581 /* Install signal trampolines, if present. */ 3582 if (vdso->sigreturn_ofs) { 3583 default_sigreturn = load_addr + vdso->sigreturn_ofs; 3584 } 3585 if (vdso->rt_sigreturn_ofs) { 3586 default_rt_sigreturn = load_addr + vdso->rt_sigreturn_ofs; 3587 } 3588 3589 /* Remove write from VDSO segment. */ 3590 target_mprotect(info->start_data, info->end_data - info->start_data, 3591 PROT_READ | PROT_EXEC); 3592 } 3593 3594 static int symfind(const void *s0, const void *s1) 3595 { 3596 struct elf_sym *sym = (struct elf_sym *)s1; 3597 __typeof(sym->st_value) addr = *(uint64_t *)s0; 3598 int result = 0; 3599 3600 if (addr < sym->st_value) { 3601 result = -1; 3602 } else if (addr >= sym->st_value + sym->st_size) { 3603 result = 1; 3604 } 3605 return result; 3606 } 3607 3608 static const char *lookup_symbolxx(struct syminfo *s, uint64_t orig_addr) 3609 { 3610 #if ELF_CLASS == ELFCLASS32 3611 struct elf_sym *syms = s->disas_symtab.elf32; 3612 #else 3613 struct elf_sym *syms = s->disas_symtab.elf64; 3614 #endif 3615 3616 // binary search 3617 struct elf_sym *sym; 3618 3619 sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), symfind); 3620 if (sym != NULL) { 3621 return s->disas_strtab + sym->st_name; 3622 } 3623 3624 return ""; 3625 } 3626 3627 /* FIXME: This should use elf_ops.h.inc */ 3628 static int symcmp(const void *s0, const void *s1) 3629 { 3630 struct elf_sym *sym0 = (struct elf_sym *)s0; 3631 struct elf_sym *sym1 = (struct elf_sym *)s1; 3632 return (sym0->st_value < sym1->st_value) 3633 ? -1 3634 : ((sym0->st_value > sym1->st_value) ? 1 : 0); 3635 } 3636 3637 /* Best attempt to load symbols from this ELF object. */ 3638 static void load_symbols(struct elfhdr *hdr, const ImageSource *src, 3639 abi_ulong load_bias) 3640 { 3641 int i, shnum, nsyms, sym_idx = 0, str_idx = 0; 3642 g_autofree struct elf_shdr *shdr = NULL; 3643 char *strings = NULL; 3644 struct elf_sym *syms = NULL; 3645 struct elf_sym *new_syms; 3646 uint64_t segsz; 3647 3648 shnum = hdr->e_shnum; 3649 shdr = imgsrc_read_alloc(hdr->e_shoff, shnum * sizeof(struct elf_shdr), 3650 src, NULL); 3651 if (shdr == NULL) { 3652 return; 3653 } 3654 3655 bswap_shdr(shdr, shnum); 3656 for (i = 0; i < shnum; ++i) { 3657 if (shdr[i].sh_type == SHT_SYMTAB) { 3658 sym_idx = i; 3659 str_idx = shdr[i].sh_link; 3660 goto found; 3661 } 3662 } 3663 3664 /* There will be no symbol table if the file was stripped. */ 3665 return; 3666 3667 found: 3668 /* Now know where the strtab and symtab are. Snarf them. */ 3669 3670 segsz = shdr[str_idx].sh_size; 3671 strings = g_try_malloc(segsz); 3672 if (!strings) { 3673 goto give_up; 3674 } 3675 if (!imgsrc_read(strings, shdr[str_idx].sh_offset, segsz, src, NULL)) { 3676 goto give_up; 3677 } 3678 3679 segsz = shdr[sym_idx].sh_size; 3680 if (segsz / sizeof(struct elf_sym) > INT_MAX) { 3681 /* 3682 * Implausibly large symbol table: give up rather than ploughing 3683 * on with the number of symbols calculation overflowing. 3684 */ 3685 goto give_up; 3686 } 3687 nsyms = segsz / sizeof(struct elf_sym); 3688 syms = g_try_malloc(segsz); 3689 if (!syms) { 3690 goto give_up; 3691 } 3692 if (!imgsrc_read(syms, shdr[sym_idx].sh_offset, segsz, src, NULL)) { 3693 goto give_up; 3694 } 3695 3696 for (i = 0; i < nsyms; ) { 3697 bswap_sym(syms + i); 3698 /* Throw away entries which we do not need. */ 3699 if (syms[i].st_shndx == SHN_UNDEF 3700 || syms[i].st_shndx >= SHN_LORESERVE 3701 || ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) { 3702 if (i < --nsyms) { 3703 syms[i] = syms[nsyms]; 3704 } 3705 } else { 3706 #if defined(TARGET_ARM) || defined (TARGET_MIPS) 3707 /* The bottom address bit marks a Thumb or MIPS16 symbol. */ 3708 syms[i].st_value &= ~(target_ulong)1; 3709 #endif 3710 syms[i].st_value += load_bias; 3711 i++; 3712 } 3713 } 3714 3715 /* No "useful" symbol. */ 3716 if (nsyms == 0) { 3717 goto give_up; 3718 } 3719 3720 /* 3721 * Attempt to free the storage associated with the local symbols 3722 * that we threw away. Whether or not this has any effect on the 3723 * memory allocation depends on the malloc implementation and how 3724 * many symbols we managed to discard. 3725 */ 3726 new_syms = g_try_renew(struct elf_sym, syms, nsyms); 3727 if (new_syms == NULL) { 3728 goto give_up; 3729 } 3730 syms = new_syms; 3731 3732 qsort(syms, nsyms, sizeof(*syms), symcmp); 3733 3734 { 3735 struct syminfo *s = g_new(struct syminfo, 1); 3736 3737 s->disas_strtab = strings; 3738 s->disas_num_syms = nsyms; 3739 #if ELF_CLASS == ELFCLASS32 3740 s->disas_symtab.elf32 = syms; 3741 #else 3742 s->disas_symtab.elf64 = syms; 3743 #endif 3744 s->lookup_symbol = lookup_symbolxx; 3745 s->next = syminfos; 3746 syminfos = s; 3747 } 3748 return; 3749 3750 give_up: 3751 g_free(strings); 3752 g_free(syms); 3753 } 3754 3755 uint32_t get_elf_eflags(int fd) 3756 { 3757 struct elfhdr ehdr; 3758 off_t offset; 3759 int ret; 3760 3761 /* Read ELF header */ 3762 offset = lseek(fd, 0, SEEK_SET); 3763 if (offset == (off_t) -1) { 3764 return 0; 3765 } 3766 ret = read(fd, &ehdr, sizeof(ehdr)); 3767 if (ret < sizeof(ehdr)) { 3768 return 0; 3769 } 3770 offset = lseek(fd, offset, SEEK_SET); 3771 if (offset == (off_t) -1) { 3772 return 0; 3773 } 3774 3775 /* Check ELF signature */ 3776 if (!elf_check_ident(&ehdr)) { 3777 return 0; 3778 } 3779 3780 /* check header */ 3781 bswap_ehdr(&ehdr); 3782 if (!elf_check_ehdr(&ehdr)) { 3783 return 0; 3784 } 3785 3786 /* return architecture id */ 3787 return ehdr.e_flags; 3788 } 3789 3790 int load_elf_binary(struct linux_binprm *bprm, struct image_info *info) 3791 { 3792 /* 3793 * We need a copy of the elf header for passing to create_elf_tables. 3794 * We will have overwritten the original when we re-use bprm->buf 3795 * while loading the interpreter. Allocate the storage for this now 3796 * and let elf_load_image do any swapping that may be required. 3797 */ 3798 struct elfhdr ehdr; 3799 struct image_info interp_info, vdso_info; 3800 char *elf_interpreter = NULL; 3801 char *scratch; 3802 3803 memset(&interp_info, 0, sizeof(interp_info)); 3804 #ifdef TARGET_MIPS 3805 interp_info.fp_abi = MIPS_ABI_FP_UNKNOWN; 3806 #endif 3807 3808 load_elf_image(bprm->filename, &bprm->src, info, &ehdr, &elf_interpreter); 3809 3810 /* Do this so that we can load the interpreter, if need be. We will 3811 change some of these later */ 3812 bprm->p = setup_arg_pages(bprm, info); 3813 3814 scratch = g_new0(char, TARGET_PAGE_SIZE); 3815 if (STACK_GROWS_DOWN) { 3816 bprm->p = copy_elf_strings(1, &bprm->filename, scratch, 3817 bprm->p, info->stack_limit); 3818 info->file_string = bprm->p; 3819 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch, 3820 bprm->p, info->stack_limit); 3821 info->env_strings = bprm->p; 3822 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch, 3823 bprm->p, info->stack_limit); 3824 info->arg_strings = bprm->p; 3825 } else { 3826 info->arg_strings = bprm->p; 3827 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch, 3828 bprm->p, info->stack_limit); 3829 info->env_strings = bprm->p; 3830 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch, 3831 bprm->p, info->stack_limit); 3832 info->file_string = bprm->p; 3833 bprm->p = copy_elf_strings(1, &bprm->filename, scratch, 3834 bprm->p, info->stack_limit); 3835 } 3836 3837 g_free(scratch); 3838 3839 if (!bprm->p) { 3840 fprintf(stderr, "%s: %s\n", bprm->filename, strerror(E2BIG)); 3841 exit(-1); 3842 } 3843 3844 if (elf_interpreter) { 3845 load_elf_interp(elf_interpreter, &interp_info, bprm->buf); 3846 3847 /* 3848 * While unusual because of ELF_ET_DYN_BASE, if we are unlucky 3849 * with the mappings the interpreter can be loaded above but 3850 * near the main executable, which can leave very little room 3851 * for the heap. 3852 * If the current brk has less than 16MB, use the end of the 3853 * interpreter. 3854 */ 3855 if (interp_info.brk > info->brk && 3856 interp_info.load_bias - info->brk < 16 * MiB) { 3857 info->brk = interp_info.brk; 3858 } 3859 3860 /* If the program interpreter is one of these two, then assume 3861 an iBCS2 image. Otherwise assume a native linux image. */ 3862 3863 if (strcmp(elf_interpreter, "/usr/lib/libc.so.1") == 0 3864 || strcmp(elf_interpreter, "/usr/lib/ld.so.1") == 0) { 3865 info->personality = PER_SVR4; 3866 3867 /* Why this, you ask??? Well SVr4 maps page 0 as read-only, 3868 and some applications "depend" upon this behavior. Since 3869 we do not have the power to recompile these, we emulate 3870 the SVr4 behavior. Sigh. */ 3871 target_mmap(0, TARGET_PAGE_SIZE, PROT_READ | PROT_EXEC, 3872 MAP_FIXED_NOREPLACE | MAP_PRIVATE | MAP_ANONYMOUS, 3873 -1, 0); 3874 } 3875 #ifdef TARGET_MIPS 3876 info->interp_fp_abi = interp_info.fp_abi; 3877 #endif 3878 } 3879 3880 /* 3881 * Load a vdso if available, which will amongst other things contain the 3882 * signal trampolines. Otherwise, allocate a separate page for them. 3883 */ 3884 const VdsoImageInfo *vdso = vdso_image_info(info->elf_flags); 3885 if (vdso) { 3886 load_elf_vdso(&vdso_info, vdso); 3887 info->vdso = vdso_info.load_bias; 3888 } else if (TARGET_ARCH_HAS_SIGTRAMP_PAGE) { 3889 abi_long tramp_page = target_mmap(0, TARGET_PAGE_SIZE, 3890 PROT_READ | PROT_WRITE, 3891 MAP_PRIVATE | MAP_ANON, -1, 0); 3892 if (tramp_page == -1) { 3893 return -errno; 3894 } 3895 3896 setup_sigtramp(tramp_page); 3897 target_mprotect(tramp_page, TARGET_PAGE_SIZE, PROT_READ | PROT_EXEC); 3898 } 3899 3900 bprm->p = create_elf_tables(bprm->p, bprm->argc, bprm->envc, &ehdr, info, 3901 elf_interpreter ? &interp_info : NULL, 3902 vdso ? &vdso_info : NULL); 3903 info->start_stack = bprm->p; 3904 3905 /* If we have an interpreter, set that as the program's entry point. 3906 Copy the load_bias as well, to help PPC64 interpret the entry 3907 point as a function descriptor. Do this after creating elf tables 3908 so that we copy the original program entry point into the AUXV. */ 3909 if (elf_interpreter) { 3910 info->load_bias = interp_info.load_bias; 3911 info->entry = interp_info.entry; 3912 g_free(elf_interpreter); 3913 } 3914 3915 #ifdef USE_ELF_CORE_DUMP 3916 bprm->core_dump = &elf_core_dump; 3917 #endif 3918 3919 return 0; 3920 } 3921 3922 #ifdef USE_ELF_CORE_DUMP 3923 3924 /* 3925 * Definitions to generate Intel SVR4-like core files. 3926 * These mostly have the same names as the SVR4 types with "target_elf_" 3927 * tacked on the front to prevent clashes with linux definitions, 3928 * and the typedef forms have been avoided. This is mostly like 3929 * the SVR4 structure, but more Linuxy, with things that Linux does 3930 * not support and which gdb doesn't really use excluded. 3931 * 3932 * Fields we don't dump (their contents is zero) in linux-user qemu 3933 * are marked with XXX. 3934 * 3935 * Core dump code is copied from linux kernel (fs/binfmt_elf.c). 3936 * 3937 * Porting ELF coredump for target is (quite) simple process. First you 3938 * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for 3939 * the target resides): 3940 * 3941 * #define USE_ELF_CORE_DUMP 3942 * 3943 * Next you define type of register set used for dumping. ELF specification 3944 * says that it needs to be array of elf_greg_t that has size of ELF_NREG. 3945 * 3946 * typedef <target_regtype> target_elf_greg_t; 3947 * #define ELF_NREG <number of registers> 3948 * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG]; 3949 * 3950 * Last step is to implement target specific function that copies registers 3951 * from given cpu into just specified register set. Prototype is: 3952 * 3953 * static void elf_core_copy_regs(taret_elf_gregset_t *regs, 3954 * const CPUArchState *env); 3955 * 3956 * Parameters: 3957 * regs - copy register values into here (allocated and zeroed by caller) 3958 * env - copy registers from here 3959 * 3960 * Example for ARM target is provided in this file. 3961 */ 3962 3963 struct target_elf_siginfo { 3964 abi_int si_signo; /* signal number */ 3965 abi_int si_code; /* extra code */ 3966 abi_int si_errno; /* errno */ 3967 }; 3968 3969 struct target_elf_prstatus { 3970 struct target_elf_siginfo pr_info; /* Info associated with signal */ 3971 abi_short pr_cursig; /* Current signal */ 3972 abi_ulong pr_sigpend; /* XXX */ 3973 abi_ulong pr_sighold; /* XXX */ 3974 target_pid_t pr_pid; 3975 target_pid_t pr_ppid; 3976 target_pid_t pr_pgrp; 3977 target_pid_t pr_sid; 3978 struct target_timeval pr_utime; /* XXX User time */ 3979 struct target_timeval pr_stime; /* XXX System time */ 3980 struct target_timeval pr_cutime; /* XXX Cumulative user time */ 3981 struct target_timeval pr_cstime; /* XXX Cumulative system time */ 3982 target_elf_gregset_t pr_reg; /* GP registers */ 3983 abi_int pr_fpvalid; /* XXX */ 3984 }; 3985 3986 #define ELF_PRARGSZ (80) /* Number of chars for args */ 3987 3988 struct target_elf_prpsinfo { 3989 char pr_state; /* numeric process state */ 3990 char pr_sname; /* char for pr_state */ 3991 char pr_zomb; /* zombie */ 3992 char pr_nice; /* nice val */ 3993 abi_ulong pr_flag; /* flags */ 3994 target_uid_t pr_uid; 3995 target_gid_t pr_gid; 3996 target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid; 3997 /* Lots missing */ 3998 char pr_fname[16] QEMU_NONSTRING; /* filename of executable */ 3999 char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */ 4000 }; 4001 4002 #ifdef BSWAP_NEEDED 4003 static void bswap_prstatus(struct target_elf_prstatus *prstatus) 4004 { 4005 prstatus->pr_info.si_signo = tswap32(prstatus->pr_info.si_signo); 4006 prstatus->pr_info.si_code = tswap32(prstatus->pr_info.si_code); 4007 prstatus->pr_info.si_errno = tswap32(prstatus->pr_info.si_errno); 4008 prstatus->pr_cursig = tswap16(prstatus->pr_cursig); 4009 prstatus->pr_sigpend = tswapal(prstatus->pr_sigpend); 4010 prstatus->pr_sighold = tswapal(prstatus->pr_sighold); 4011 prstatus->pr_pid = tswap32(prstatus->pr_pid); 4012 prstatus->pr_ppid = tswap32(prstatus->pr_ppid); 4013 prstatus->pr_pgrp = tswap32(prstatus->pr_pgrp); 4014 prstatus->pr_sid = tswap32(prstatus->pr_sid); 4015 /* cpu times are not filled, so we skip them */ 4016 /* regs should be in correct format already */ 4017 prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid); 4018 } 4019 4020 static void bswap_psinfo(struct target_elf_prpsinfo *psinfo) 4021 { 4022 psinfo->pr_flag = tswapal(psinfo->pr_flag); 4023 psinfo->pr_uid = tswap16(psinfo->pr_uid); 4024 psinfo->pr_gid = tswap16(psinfo->pr_gid); 4025 psinfo->pr_pid = tswap32(psinfo->pr_pid); 4026 psinfo->pr_ppid = tswap32(psinfo->pr_ppid); 4027 psinfo->pr_pgrp = tswap32(psinfo->pr_pgrp); 4028 psinfo->pr_sid = tswap32(psinfo->pr_sid); 4029 } 4030 4031 static void bswap_note(struct elf_note *en) 4032 { 4033 bswap32s(&en->n_namesz); 4034 bswap32s(&en->n_descsz); 4035 bswap32s(&en->n_type); 4036 } 4037 #else 4038 static inline void bswap_prstatus(struct target_elf_prstatus *p) { } 4039 static inline void bswap_psinfo(struct target_elf_prpsinfo *p) {} 4040 static inline void bswap_note(struct elf_note *en) { } 4041 #endif /* BSWAP_NEEDED */ 4042 4043 /* 4044 * Calculate file (dump) size of given memory region. 4045 */ 4046 static size_t vma_dump_size(target_ulong start, target_ulong end, 4047 unsigned long flags) 4048 { 4049 /* The area must be readable. */ 4050 if (!(flags & PAGE_READ)) { 4051 return 0; 4052 } 4053 4054 /* 4055 * Usually we don't dump executable pages as they contain 4056 * non-writable code that debugger can read directly from 4057 * target library etc. If there is no elf header, we dump it. 4058 */ 4059 if (!(flags & PAGE_WRITE_ORG) && 4060 (flags & PAGE_EXEC) && 4061 memcmp(g2h_untagged(start), ELFMAG, SELFMAG) == 0) { 4062 return 0; 4063 } 4064 4065 return end - start; 4066 } 4067 4068 static size_t size_note(const char *name, size_t datasz) 4069 { 4070 size_t namesz = strlen(name) + 1; 4071 4072 namesz = ROUND_UP(namesz, 4); 4073 datasz = ROUND_UP(datasz, 4); 4074 4075 return sizeof(struct elf_note) + namesz + datasz; 4076 } 4077 4078 static void *fill_note(void **pptr, int type, const char *name, size_t datasz) 4079 { 4080 void *ptr = *pptr; 4081 struct elf_note *n = ptr; 4082 size_t namesz = strlen(name) + 1; 4083 4084 n->n_namesz = namesz; 4085 n->n_descsz = datasz; 4086 n->n_type = type; 4087 bswap_note(n); 4088 4089 ptr += sizeof(*n); 4090 memcpy(ptr, name, namesz); 4091 4092 namesz = ROUND_UP(namesz, 4); 4093 datasz = ROUND_UP(datasz, 4); 4094 4095 *pptr = ptr + namesz + datasz; 4096 return ptr + namesz; 4097 } 4098 4099 static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine, 4100 uint32_t flags) 4101 { 4102 memcpy(elf->e_ident, ELFMAG, SELFMAG); 4103 4104 elf->e_ident[EI_CLASS] = ELF_CLASS; 4105 elf->e_ident[EI_DATA] = ELF_DATA; 4106 elf->e_ident[EI_VERSION] = EV_CURRENT; 4107 elf->e_ident[EI_OSABI] = ELF_OSABI; 4108 4109 elf->e_type = ET_CORE; 4110 elf->e_machine = machine; 4111 elf->e_version = EV_CURRENT; 4112 elf->e_phoff = sizeof(struct elfhdr); 4113 elf->e_flags = flags; 4114 elf->e_ehsize = sizeof(struct elfhdr); 4115 elf->e_phentsize = sizeof(struct elf_phdr); 4116 elf->e_phnum = segs; 4117 4118 bswap_ehdr(elf); 4119 } 4120 4121 static void fill_elf_note_phdr(struct elf_phdr *phdr, size_t sz, off_t offset) 4122 { 4123 phdr->p_type = PT_NOTE; 4124 phdr->p_offset = offset; 4125 phdr->p_filesz = sz; 4126 4127 bswap_phdr(phdr, 1); 4128 } 4129 4130 static void fill_prstatus_note(void *data, CPUState *cpu, int signr) 4131 { 4132 /* 4133 * Because note memory is only aligned to 4, and target_elf_prstatus 4134 * may well have higher alignment requirements, fill locally and 4135 * memcpy to the destination afterward. 4136 */ 4137 struct target_elf_prstatus prstatus = { 4138 .pr_info.si_signo = signr, 4139 .pr_cursig = signr, 4140 .pr_pid = get_task_state(cpu)->ts_tid, 4141 .pr_ppid = getppid(), 4142 .pr_pgrp = getpgrp(), 4143 .pr_sid = getsid(0), 4144 }; 4145 4146 elf_core_copy_regs(&prstatus.pr_reg, cpu_env(cpu)); 4147 bswap_prstatus(&prstatus); 4148 memcpy(data, &prstatus, sizeof(prstatus)); 4149 } 4150 4151 static void fill_prpsinfo_note(void *data, const TaskState *ts) 4152 { 4153 /* 4154 * Because note memory is only aligned to 4, and target_elf_prpsinfo 4155 * may well have higher alignment requirements, fill locally and 4156 * memcpy to the destination afterward. 4157 */ 4158 struct target_elf_prpsinfo psinfo = { 4159 .pr_pid = getpid(), 4160 .pr_ppid = getppid(), 4161 .pr_pgrp = getpgrp(), 4162 .pr_sid = getsid(0), 4163 .pr_uid = getuid(), 4164 .pr_gid = getgid(), 4165 }; 4166 char *base_filename; 4167 size_t len; 4168 4169 len = ts->info->env_strings - ts->info->arg_strings; 4170 len = MIN(len, ELF_PRARGSZ); 4171 memcpy(&psinfo.pr_psargs, g2h_untagged(ts->info->arg_strings), len); 4172 for (size_t i = 0; i < len; i++) { 4173 if (psinfo.pr_psargs[i] == 0) { 4174 psinfo.pr_psargs[i] = ' '; 4175 } 4176 } 4177 4178 base_filename = g_path_get_basename(ts->bprm->filename); 4179 /* 4180 * Using strncpy here is fine: at max-length, 4181 * this field is not NUL-terminated. 4182 */ 4183 strncpy(psinfo.pr_fname, base_filename, sizeof(psinfo.pr_fname)); 4184 g_free(base_filename); 4185 4186 bswap_psinfo(&psinfo); 4187 memcpy(data, &psinfo, sizeof(psinfo)); 4188 } 4189 4190 static void fill_auxv_note(void *data, const TaskState *ts) 4191 { 4192 memcpy(data, g2h_untagged(ts->info->saved_auxv), ts->info->auxv_len); 4193 } 4194 4195 /* 4196 * Constructs name of coredump file. We have following convention 4197 * for the name: 4198 * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core 4199 * 4200 * Returns the filename 4201 */ 4202 static char *core_dump_filename(const TaskState *ts) 4203 { 4204 g_autoptr(GDateTime) now = g_date_time_new_now_local(); 4205 g_autofree char *nowstr = g_date_time_format(now, "%Y%m%d-%H%M%S"); 4206 g_autofree char *base_filename = g_path_get_basename(ts->bprm->filename); 4207 4208 return g_strdup_printf("qemu_%s_%s_%d.core", 4209 base_filename, nowstr, (int)getpid()); 4210 } 4211 4212 static int dump_write(int fd, const void *ptr, size_t size) 4213 { 4214 const char *bufp = (const char *)ptr; 4215 ssize_t bytes_written, bytes_left; 4216 4217 bytes_written = 0; 4218 bytes_left = size; 4219 4220 /* 4221 * In normal conditions, single write(2) should do but 4222 * in case of socket etc. this mechanism is more portable. 4223 */ 4224 do { 4225 bytes_written = write(fd, bufp, bytes_left); 4226 if (bytes_written < 0) { 4227 if (errno == EINTR) 4228 continue; 4229 return (-1); 4230 } else if (bytes_written == 0) { /* eof */ 4231 return (-1); 4232 } 4233 bufp += bytes_written; 4234 bytes_left -= bytes_written; 4235 } while (bytes_left > 0); 4236 4237 return (0); 4238 } 4239 4240 static int wmr_page_unprotect_regions(void *opaque, target_ulong start, 4241 target_ulong end, unsigned long flags) 4242 { 4243 if ((flags & (PAGE_WRITE | PAGE_WRITE_ORG)) == PAGE_WRITE_ORG) { 4244 size_t step = MAX(TARGET_PAGE_SIZE, qemu_real_host_page_size()); 4245 4246 while (1) { 4247 page_unprotect(start, 0); 4248 if (end - start <= step) { 4249 break; 4250 } 4251 start += step; 4252 } 4253 } 4254 return 0; 4255 } 4256 4257 typedef struct { 4258 unsigned count; 4259 size_t size; 4260 } CountAndSizeRegions; 4261 4262 static int wmr_count_and_size_regions(void *opaque, target_ulong start, 4263 target_ulong end, unsigned long flags) 4264 { 4265 CountAndSizeRegions *css = opaque; 4266 4267 css->count++; 4268 css->size += vma_dump_size(start, end, flags); 4269 return 0; 4270 } 4271 4272 typedef struct { 4273 struct elf_phdr *phdr; 4274 off_t offset; 4275 } FillRegionPhdr; 4276 4277 static int wmr_fill_region_phdr(void *opaque, target_ulong start, 4278 target_ulong end, unsigned long flags) 4279 { 4280 FillRegionPhdr *d = opaque; 4281 struct elf_phdr *phdr = d->phdr; 4282 4283 phdr->p_type = PT_LOAD; 4284 phdr->p_vaddr = start; 4285 phdr->p_paddr = 0; 4286 phdr->p_filesz = vma_dump_size(start, end, flags); 4287 phdr->p_offset = d->offset; 4288 d->offset += phdr->p_filesz; 4289 phdr->p_memsz = end - start; 4290 phdr->p_flags = (flags & PAGE_READ ? PF_R : 0) 4291 | (flags & PAGE_WRITE_ORG ? PF_W : 0) 4292 | (flags & PAGE_EXEC ? PF_X : 0); 4293 phdr->p_align = ELF_EXEC_PAGESIZE; 4294 4295 bswap_phdr(phdr, 1); 4296 d->phdr = phdr + 1; 4297 return 0; 4298 } 4299 4300 static int wmr_write_region(void *opaque, target_ulong start, 4301 target_ulong end, unsigned long flags) 4302 { 4303 int fd = *(int *)opaque; 4304 size_t size = vma_dump_size(start, end, flags); 4305 4306 if (!size) { 4307 return 0; 4308 } 4309 return dump_write(fd, g2h_untagged(start), size); 4310 } 4311 4312 /* 4313 * Write out ELF coredump. 4314 * 4315 * See documentation of ELF object file format in: 4316 * http://www.caldera.com/developers/devspecs/gabi41.pdf 4317 * 4318 * Coredump format in linux is following: 4319 * 4320 * 0 +----------------------+ \ 4321 * | ELF header | ET_CORE | 4322 * +----------------------+ | 4323 * | ELF program headers | |--- headers 4324 * | - NOTE section | | 4325 * | - PT_LOAD sections | | 4326 * +----------------------+ / 4327 * | NOTEs: | 4328 * | - NT_PRSTATUS | 4329 * | - NT_PRSINFO | 4330 * | - NT_AUXV | 4331 * +----------------------+ <-- aligned to target page 4332 * | Process memory dump | 4333 * : : 4334 * . . 4335 * : : 4336 * | | 4337 * +----------------------+ 4338 * 4339 * NT_PRSTATUS -> struct elf_prstatus (per thread) 4340 * NT_PRSINFO -> struct elf_prpsinfo 4341 * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()). 4342 * 4343 * Format follows System V format as close as possible. Current 4344 * version limitations are as follows: 4345 * - no floating point registers are dumped 4346 * 4347 * Function returns 0 in case of success, negative errno otherwise. 4348 * 4349 * TODO: make this work also during runtime: it should be 4350 * possible to force coredump from running process and then 4351 * continue processing. For example qemu could set up SIGUSR2 4352 * handler (provided that target process haven't registered 4353 * handler for that) that does the dump when signal is received. 4354 */ 4355 static int elf_core_dump(int signr, const CPUArchState *env) 4356 { 4357 const CPUState *cpu = env_cpu_const(env); 4358 const TaskState *ts = (const TaskState *)get_task_state((CPUState *)cpu); 4359 struct rlimit dumpsize; 4360 CountAndSizeRegions css; 4361 off_t offset, note_offset, data_offset; 4362 size_t note_size; 4363 int cpus, ret; 4364 int fd = -1; 4365 CPUState *cpu_iter; 4366 4367 if (prctl(PR_GET_DUMPABLE) == 0) { 4368 return 0; 4369 } 4370 4371 if (getrlimit(RLIMIT_CORE, &dumpsize) < 0 || dumpsize.rlim_cur == 0) { 4372 return 0; 4373 } 4374 4375 cpu_list_lock(); 4376 mmap_lock(); 4377 4378 /* By unprotecting, we merge vmas that might be split. */ 4379 walk_memory_regions(NULL, wmr_page_unprotect_regions); 4380 4381 /* 4382 * Walk through target process memory mappings and 4383 * set up structure containing this information. 4384 */ 4385 memset(&css, 0, sizeof(css)); 4386 walk_memory_regions(&css, wmr_count_and_size_regions); 4387 4388 cpus = 0; 4389 CPU_FOREACH(cpu_iter) { 4390 cpus++; 4391 } 4392 4393 offset = sizeof(struct elfhdr); 4394 offset += (css.count + 1) * sizeof(struct elf_phdr); 4395 note_offset = offset; 4396 4397 offset += size_note("CORE", ts->info->auxv_len); 4398 offset += size_note("CORE", sizeof(struct target_elf_prpsinfo)); 4399 offset += size_note("CORE", sizeof(struct target_elf_prstatus)) * cpus; 4400 note_size = offset - note_offset; 4401 data_offset = ROUND_UP(offset, ELF_EXEC_PAGESIZE); 4402 4403 /* Do not dump if the corefile size exceeds the limit. */ 4404 if (dumpsize.rlim_cur != RLIM_INFINITY 4405 && dumpsize.rlim_cur < data_offset + css.size) { 4406 errno = 0; 4407 goto out; 4408 } 4409 4410 { 4411 g_autofree char *corefile = core_dump_filename(ts); 4412 fd = open(corefile, O_WRONLY | O_CREAT | O_TRUNC, 4413 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); 4414 } 4415 if (fd < 0) { 4416 goto out; 4417 } 4418 4419 /* 4420 * There is a fair amount of alignment padding within the notes 4421 * as well as preceeding the process memory. Allocate a zeroed 4422 * block to hold it all. Write all of the headers directly into 4423 * this buffer and then write it out as a block. 4424 */ 4425 { 4426 g_autofree void *header = g_malloc0(data_offset); 4427 FillRegionPhdr frp; 4428 void *hptr, *dptr; 4429 4430 /* Create elf file header. */ 4431 hptr = header; 4432 fill_elf_header(hptr, css.count + 1, ELF_MACHINE, 0); 4433 hptr += sizeof(struct elfhdr); 4434 4435 /* Create elf program headers. */ 4436 fill_elf_note_phdr(hptr, note_size, note_offset); 4437 hptr += sizeof(struct elf_phdr); 4438 4439 frp.phdr = hptr; 4440 frp.offset = data_offset; 4441 walk_memory_regions(&frp, wmr_fill_region_phdr); 4442 hptr = frp.phdr; 4443 4444 /* Create the notes. */ 4445 dptr = fill_note(&hptr, NT_AUXV, "CORE", ts->info->auxv_len); 4446 fill_auxv_note(dptr, ts); 4447 4448 dptr = fill_note(&hptr, NT_PRPSINFO, "CORE", 4449 sizeof(struct target_elf_prpsinfo)); 4450 fill_prpsinfo_note(dptr, ts); 4451 4452 CPU_FOREACH(cpu_iter) { 4453 dptr = fill_note(&hptr, NT_PRSTATUS, "CORE", 4454 sizeof(struct target_elf_prstatus)); 4455 fill_prstatus_note(dptr, cpu_iter, cpu_iter == cpu ? signr : 0); 4456 } 4457 4458 if (dump_write(fd, header, data_offset) < 0) { 4459 goto out; 4460 } 4461 } 4462 4463 /* 4464 * Finally write process memory into the corefile as well. 4465 */ 4466 if (walk_memory_regions(&fd, wmr_write_region) < 0) { 4467 goto out; 4468 } 4469 errno = 0; 4470 4471 out: 4472 ret = -errno; 4473 mmap_unlock(); 4474 cpu_list_unlock(); 4475 if (fd >= 0) { 4476 close(fd); 4477 } 4478 return ret; 4479 } 4480 #endif /* USE_ELF_CORE_DUMP */ 4481 4482 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop) 4483 { 4484 init_thread(regs, infop); 4485 } 4486