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