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_OPENRISC 1509 1510 #define ELF_ARCH EM_OPENRISC 1511 #define ELF_CLASS ELFCLASS32 1512 #define ELF_DATA ELFDATA2MSB 1513 1514 static inline void init_thread(struct target_pt_regs *regs, 1515 struct image_info *infop) 1516 { 1517 regs->pc = infop->entry; 1518 regs->gpr[1] = infop->start_stack; 1519 } 1520 1521 #define USE_ELF_CORE_DUMP 1522 #define ELF_EXEC_PAGESIZE 8192 1523 1524 /* See linux kernel arch/openrisc/include/asm/elf.h. */ 1525 #define ELF_NREG 34 /* gprs and pc, sr */ 1526 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; 1527 1528 static void elf_core_copy_regs(target_elf_gregset_t *regs, 1529 const CPUOpenRISCState *env) 1530 { 1531 int i; 1532 1533 for (i = 0; i < 32; i++) { 1534 (*regs)[i] = tswapreg(cpu_get_gpr(env, i)); 1535 } 1536 (*regs)[32] = tswapreg(env->pc); 1537 (*regs)[33] = tswapreg(cpu_get_sr(env)); 1538 } 1539 #define ELF_HWCAP 0 1540 #define ELF_PLATFORM NULL 1541 1542 #endif /* TARGET_OPENRISC */ 1543 1544 #ifdef TARGET_SH4 1545 1546 #define ELF_CLASS ELFCLASS32 1547 #define ELF_ARCH EM_SH 1548 1549 static inline void init_thread(struct target_pt_regs *regs, 1550 struct image_info *infop) 1551 { 1552 /* Check other registers XXXXX */ 1553 regs->pc = infop->entry; 1554 regs->regs[15] = infop->start_stack; 1555 } 1556 1557 /* See linux kernel: arch/sh/include/asm/elf.h. */ 1558 #define ELF_NREG 23 1559 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; 1560 1561 /* See linux kernel: arch/sh/include/asm/ptrace.h. */ 1562 enum { 1563 TARGET_REG_PC = 16, 1564 TARGET_REG_PR = 17, 1565 TARGET_REG_SR = 18, 1566 TARGET_REG_GBR = 19, 1567 TARGET_REG_MACH = 20, 1568 TARGET_REG_MACL = 21, 1569 TARGET_REG_SYSCALL = 22 1570 }; 1571 1572 static inline void elf_core_copy_regs(target_elf_gregset_t *regs, 1573 const CPUSH4State *env) 1574 { 1575 int i; 1576 1577 for (i = 0; i < 16; i++) { 1578 (*regs)[i] = tswapreg(env->gregs[i]); 1579 } 1580 1581 (*regs)[TARGET_REG_PC] = tswapreg(env->pc); 1582 (*regs)[TARGET_REG_PR] = tswapreg(env->pr); 1583 (*regs)[TARGET_REG_SR] = tswapreg(env->sr); 1584 (*regs)[TARGET_REG_GBR] = tswapreg(env->gbr); 1585 (*regs)[TARGET_REG_MACH] = tswapreg(env->mach); 1586 (*regs)[TARGET_REG_MACL] = tswapreg(env->macl); 1587 (*regs)[TARGET_REG_SYSCALL] = 0; /* FIXME */ 1588 } 1589 1590 #define USE_ELF_CORE_DUMP 1591 #define ELF_EXEC_PAGESIZE 4096 1592 1593 enum { 1594 SH_CPU_HAS_FPU = 0x0001, /* Hardware FPU support */ 1595 SH_CPU_HAS_P2_FLUSH_BUG = 0x0002, /* Need to flush the cache in P2 area */ 1596 SH_CPU_HAS_MMU_PAGE_ASSOC = 0x0004, /* SH3: TLB way selection bit support */ 1597 SH_CPU_HAS_DSP = 0x0008, /* SH-DSP: DSP support */ 1598 SH_CPU_HAS_PERF_COUNTER = 0x0010, /* Hardware performance counters */ 1599 SH_CPU_HAS_PTEA = 0x0020, /* PTEA register */ 1600 SH_CPU_HAS_LLSC = 0x0040, /* movli.l/movco.l */ 1601 SH_CPU_HAS_L2_CACHE = 0x0080, /* Secondary cache / URAM */ 1602 SH_CPU_HAS_OP32 = 0x0100, /* 32-bit instruction support */ 1603 SH_CPU_HAS_PTEAEX = 0x0200, /* PTE ASID Extension support */ 1604 }; 1605 1606 #define ELF_HWCAP get_elf_hwcap() 1607 1608 static uint32_t get_elf_hwcap(void) 1609 { 1610 SuperHCPU *cpu = SUPERH_CPU(thread_cpu); 1611 uint32_t hwcap = 0; 1612 1613 hwcap |= SH_CPU_HAS_FPU; 1614 1615 if (cpu->env.features & SH_FEATURE_SH4A) { 1616 hwcap |= SH_CPU_HAS_LLSC; 1617 } 1618 1619 return hwcap; 1620 } 1621 1622 #endif 1623 1624 #ifdef TARGET_CRIS 1625 1626 #define ELF_CLASS ELFCLASS32 1627 #define ELF_ARCH EM_CRIS 1628 1629 static inline void init_thread(struct target_pt_regs *regs, 1630 struct image_info *infop) 1631 { 1632 regs->erp = infop->entry; 1633 } 1634 1635 #define ELF_EXEC_PAGESIZE 8192 1636 1637 #endif 1638 1639 #ifdef TARGET_M68K 1640 1641 #define ELF_CLASS ELFCLASS32 1642 #define ELF_ARCH EM_68K 1643 1644 /* ??? Does this need to do anything? 1645 #define ELF_PLAT_INIT(_r) */ 1646 1647 static inline void init_thread(struct target_pt_regs *regs, 1648 struct image_info *infop) 1649 { 1650 regs->usp = infop->start_stack; 1651 regs->sr = 0; 1652 regs->pc = infop->entry; 1653 } 1654 1655 /* See linux kernel: arch/m68k/include/asm/elf.h. */ 1656 #define ELF_NREG 20 1657 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; 1658 1659 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUM68KState *env) 1660 { 1661 (*regs)[0] = tswapreg(env->dregs[1]); 1662 (*regs)[1] = tswapreg(env->dregs[2]); 1663 (*regs)[2] = tswapreg(env->dregs[3]); 1664 (*regs)[3] = tswapreg(env->dregs[4]); 1665 (*regs)[4] = tswapreg(env->dregs[5]); 1666 (*regs)[5] = tswapreg(env->dregs[6]); 1667 (*regs)[6] = tswapreg(env->dregs[7]); 1668 (*regs)[7] = tswapreg(env->aregs[0]); 1669 (*regs)[8] = tswapreg(env->aregs[1]); 1670 (*regs)[9] = tswapreg(env->aregs[2]); 1671 (*regs)[10] = tswapreg(env->aregs[3]); 1672 (*regs)[11] = tswapreg(env->aregs[4]); 1673 (*regs)[12] = tswapreg(env->aregs[5]); 1674 (*regs)[13] = tswapreg(env->aregs[6]); 1675 (*regs)[14] = tswapreg(env->dregs[0]); 1676 (*regs)[15] = tswapreg(env->aregs[7]); 1677 (*regs)[16] = tswapreg(env->dregs[0]); /* FIXME: orig_d0 */ 1678 (*regs)[17] = tswapreg(env->sr); 1679 (*regs)[18] = tswapreg(env->pc); 1680 (*regs)[19] = 0; /* FIXME: regs->format | regs->vector */ 1681 } 1682 1683 #define USE_ELF_CORE_DUMP 1684 #define ELF_EXEC_PAGESIZE 8192 1685 1686 #endif 1687 1688 #ifdef TARGET_ALPHA 1689 1690 #define ELF_CLASS ELFCLASS64 1691 #define ELF_ARCH EM_ALPHA 1692 1693 static inline void init_thread(struct target_pt_regs *regs, 1694 struct image_info *infop) 1695 { 1696 regs->pc = infop->entry; 1697 regs->ps = 8; 1698 regs->usp = infop->start_stack; 1699 } 1700 1701 #define ELF_EXEC_PAGESIZE 8192 1702 1703 #endif /* TARGET_ALPHA */ 1704 1705 #ifdef TARGET_S390X 1706 1707 #define ELF_CLASS ELFCLASS64 1708 #define ELF_DATA ELFDATA2MSB 1709 #define ELF_ARCH EM_S390 1710 1711 #include "elf.h" 1712 1713 #define ELF_HWCAP get_elf_hwcap() 1714 1715 #define GET_FEATURE(_feat, _hwcap) \ 1716 do { if (s390_has_feat(_feat)) { hwcap |= _hwcap; } } while (0) 1717 1718 uint32_t get_elf_hwcap(void) 1719 { 1720 /* 1721 * Let's assume we always have esan3 and zarch. 1722 * 31-bit processes can use 64-bit registers (high gprs). 1723 */ 1724 uint32_t hwcap = HWCAP_S390_ESAN3 | HWCAP_S390_ZARCH | HWCAP_S390_HIGH_GPRS; 1725 1726 GET_FEATURE(S390_FEAT_STFLE, HWCAP_S390_STFLE); 1727 GET_FEATURE(S390_FEAT_MSA, HWCAP_S390_MSA); 1728 GET_FEATURE(S390_FEAT_LONG_DISPLACEMENT, HWCAP_S390_LDISP); 1729 GET_FEATURE(S390_FEAT_EXTENDED_IMMEDIATE, HWCAP_S390_EIMM); 1730 if (s390_has_feat(S390_FEAT_EXTENDED_TRANSLATION_3) && 1731 s390_has_feat(S390_FEAT_ETF3_ENH)) { 1732 hwcap |= HWCAP_S390_ETF3EH; 1733 } 1734 GET_FEATURE(S390_FEAT_VECTOR, HWCAP_S390_VXRS); 1735 GET_FEATURE(S390_FEAT_VECTOR_ENH, HWCAP_S390_VXRS_EXT); 1736 GET_FEATURE(S390_FEAT_VECTOR_ENH2, HWCAP_S390_VXRS_EXT2); 1737 1738 return hwcap; 1739 } 1740 1741 const char *elf_hwcap_str(uint32_t bit) 1742 { 1743 static const char *hwcap_str[] = { 1744 [HWCAP_S390_NR_ESAN3] = "esan3", 1745 [HWCAP_S390_NR_ZARCH] = "zarch", 1746 [HWCAP_S390_NR_STFLE] = "stfle", 1747 [HWCAP_S390_NR_MSA] = "msa", 1748 [HWCAP_S390_NR_LDISP] = "ldisp", 1749 [HWCAP_S390_NR_EIMM] = "eimm", 1750 [HWCAP_S390_NR_DFP] = "dfp", 1751 [HWCAP_S390_NR_HPAGE] = "edat", 1752 [HWCAP_S390_NR_ETF3EH] = "etf3eh", 1753 [HWCAP_S390_NR_HIGH_GPRS] = "highgprs", 1754 [HWCAP_S390_NR_TE] = "te", 1755 [HWCAP_S390_NR_VXRS] = "vx", 1756 [HWCAP_S390_NR_VXRS_BCD] = "vxd", 1757 [HWCAP_S390_NR_VXRS_EXT] = "vxe", 1758 [HWCAP_S390_NR_GS] = "gs", 1759 [HWCAP_S390_NR_VXRS_EXT2] = "vxe2", 1760 [HWCAP_S390_NR_VXRS_PDE] = "vxp", 1761 [HWCAP_S390_NR_SORT] = "sort", 1762 [HWCAP_S390_NR_DFLT] = "dflt", 1763 [HWCAP_S390_NR_NNPA] = "nnpa", 1764 [HWCAP_S390_NR_PCI_MIO] = "pcimio", 1765 [HWCAP_S390_NR_SIE] = "sie", 1766 }; 1767 1768 return bit < ARRAY_SIZE(hwcap_str) ? hwcap_str[bit] : NULL; 1769 } 1770 1771 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop) 1772 { 1773 regs->psw.addr = infop->entry; 1774 regs->psw.mask = PSW_MASK_DAT | PSW_MASK_IO | PSW_MASK_EXT | \ 1775 PSW_MASK_MCHECK | PSW_MASK_PSTATE | PSW_MASK_64 | \ 1776 PSW_MASK_32; 1777 regs->gprs[15] = infop->start_stack; 1778 } 1779 1780 /* See linux kernel: arch/s390/include/uapi/asm/ptrace.h (s390_regs). */ 1781 #define ELF_NREG 27 1782 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; 1783 1784 enum { 1785 TARGET_REG_PSWM = 0, 1786 TARGET_REG_PSWA = 1, 1787 TARGET_REG_GPRS = 2, 1788 TARGET_REG_ARS = 18, 1789 TARGET_REG_ORIG_R2 = 26, 1790 }; 1791 1792 static void elf_core_copy_regs(target_elf_gregset_t *regs, 1793 const CPUS390XState *env) 1794 { 1795 int i; 1796 uint32_t *aregs; 1797 1798 (*regs)[TARGET_REG_PSWM] = tswapreg(env->psw.mask); 1799 (*regs)[TARGET_REG_PSWA] = tswapreg(env->psw.addr); 1800 for (i = 0; i < 16; i++) { 1801 (*regs)[TARGET_REG_GPRS + i] = tswapreg(env->regs[i]); 1802 } 1803 aregs = (uint32_t *)&((*regs)[TARGET_REG_ARS]); 1804 for (i = 0; i < 16; i++) { 1805 aregs[i] = tswap32(env->aregs[i]); 1806 } 1807 (*regs)[TARGET_REG_ORIG_R2] = 0; 1808 } 1809 1810 #define USE_ELF_CORE_DUMP 1811 #define ELF_EXEC_PAGESIZE 4096 1812 1813 #define VDSO_HEADER "vdso.c.inc" 1814 1815 #endif /* TARGET_S390X */ 1816 1817 #ifdef TARGET_RISCV 1818 1819 #define ELF_ARCH EM_RISCV 1820 1821 #ifdef TARGET_RISCV32 1822 #define ELF_CLASS ELFCLASS32 1823 #define VDSO_HEADER "vdso-32.c.inc" 1824 #else 1825 #define ELF_CLASS ELFCLASS64 1826 #define VDSO_HEADER "vdso-64.c.inc" 1827 #endif 1828 1829 #define ELF_HWCAP get_elf_hwcap() 1830 1831 static uint32_t get_elf_hwcap(void) 1832 { 1833 #define MISA_BIT(EXT) (1 << (EXT - 'A')) 1834 RISCVCPU *cpu = RISCV_CPU(thread_cpu); 1835 uint32_t mask = MISA_BIT('I') | MISA_BIT('M') | MISA_BIT('A') 1836 | MISA_BIT('F') | MISA_BIT('D') | MISA_BIT('C') 1837 | MISA_BIT('V'); 1838 1839 return cpu->env.misa_ext & mask; 1840 #undef MISA_BIT 1841 } 1842 1843 static inline void init_thread(struct target_pt_regs *regs, 1844 struct image_info *infop) 1845 { 1846 regs->sepc = infop->entry; 1847 regs->sp = infop->start_stack; 1848 } 1849 1850 #define ELF_EXEC_PAGESIZE 4096 1851 1852 #endif /* TARGET_RISCV */ 1853 1854 #ifdef TARGET_HPPA 1855 1856 #define ELF_CLASS ELFCLASS32 1857 #define ELF_ARCH EM_PARISC 1858 #define ELF_PLATFORM "PARISC" 1859 #define STACK_GROWS_DOWN 0 1860 #define STACK_ALIGNMENT 64 1861 1862 #define VDSO_HEADER "vdso.c.inc" 1863 1864 static inline void init_thread(struct target_pt_regs *regs, 1865 struct image_info *infop) 1866 { 1867 regs->iaoq[0] = infop->entry; 1868 regs->iaoq[1] = infop->entry + 4; 1869 regs->gr[23] = 0; 1870 regs->gr[24] = infop->argv; 1871 regs->gr[25] = infop->argc; 1872 /* The top-of-stack contains a linkage buffer. */ 1873 regs->gr[30] = infop->start_stack + 64; 1874 regs->gr[31] = infop->entry; 1875 } 1876 1877 #define LO_COMMPAGE 0 1878 1879 static bool init_guest_commpage(void) 1880 { 1881 /* If reserved_va, then we have already mapped 0 page on the host. */ 1882 if (!reserved_va) { 1883 void *want, *addr; 1884 1885 want = g2h_untagged(LO_COMMPAGE); 1886 addr = mmap(want, TARGET_PAGE_SIZE, PROT_NONE, 1887 MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED_NOREPLACE, -1, 0); 1888 if (addr == MAP_FAILED) { 1889 perror("Allocating guest commpage"); 1890 exit(EXIT_FAILURE); 1891 } 1892 if (addr != want) { 1893 return false; 1894 } 1895 } 1896 1897 /* 1898 * On Linux, page zero is normally marked execute only + gateway. 1899 * Normal read or write is supposed to fail (thus PROT_NONE above), 1900 * but specific offsets have kernel code mapped to raise permissions 1901 * and implement syscalls. Here, simply mark the page executable. 1902 * Special case the entry points during translation (see do_page_zero). 1903 */ 1904 page_set_flags(LO_COMMPAGE, LO_COMMPAGE | ~TARGET_PAGE_MASK, 1905 PAGE_EXEC | PAGE_VALID); 1906 return true; 1907 } 1908 1909 #endif /* TARGET_HPPA */ 1910 1911 #ifdef TARGET_XTENSA 1912 1913 #define ELF_CLASS ELFCLASS32 1914 #define ELF_ARCH EM_XTENSA 1915 1916 static inline void init_thread(struct target_pt_regs *regs, 1917 struct image_info *infop) 1918 { 1919 regs->windowbase = 0; 1920 regs->windowstart = 1; 1921 regs->areg[1] = infop->start_stack; 1922 regs->pc = infop->entry; 1923 if (info_is_fdpic(infop)) { 1924 regs->areg[4] = infop->loadmap_addr; 1925 regs->areg[5] = infop->interpreter_loadmap_addr; 1926 if (infop->interpreter_loadmap_addr) { 1927 regs->areg[6] = infop->interpreter_pt_dynamic_addr; 1928 } else { 1929 regs->areg[6] = infop->pt_dynamic_addr; 1930 } 1931 } 1932 } 1933 1934 /* See linux kernel: arch/xtensa/include/asm/elf.h. */ 1935 #define ELF_NREG 128 1936 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; 1937 1938 enum { 1939 TARGET_REG_PC, 1940 TARGET_REG_PS, 1941 TARGET_REG_LBEG, 1942 TARGET_REG_LEND, 1943 TARGET_REG_LCOUNT, 1944 TARGET_REG_SAR, 1945 TARGET_REG_WINDOWSTART, 1946 TARGET_REG_WINDOWBASE, 1947 TARGET_REG_THREADPTR, 1948 TARGET_REG_AR0 = 64, 1949 }; 1950 1951 static void elf_core_copy_regs(target_elf_gregset_t *regs, 1952 const CPUXtensaState *env) 1953 { 1954 unsigned i; 1955 1956 (*regs)[TARGET_REG_PC] = tswapreg(env->pc); 1957 (*regs)[TARGET_REG_PS] = tswapreg(env->sregs[PS] & ~PS_EXCM); 1958 (*regs)[TARGET_REG_LBEG] = tswapreg(env->sregs[LBEG]); 1959 (*regs)[TARGET_REG_LEND] = tswapreg(env->sregs[LEND]); 1960 (*regs)[TARGET_REG_LCOUNT] = tswapreg(env->sregs[LCOUNT]); 1961 (*regs)[TARGET_REG_SAR] = tswapreg(env->sregs[SAR]); 1962 (*regs)[TARGET_REG_WINDOWSTART] = tswapreg(env->sregs[WINDOW_START]); 1963 (*regs)[TARGET_REG_WINDOWBASE] = tswapreg(env->sregs[WINDOW_BASE]); 1964 (*regs)[TARGET_REG_THREADPTR] = tswapreg(env->uregs[THREADPTR]); 1965 xtensa_sync_phys_from_window((CPUXtensaState *)env); 1966 for (i = 0; i < env->config->nareg; ++i) { 1967 (*regs)[TARGET_REG_AR0 + i] = tswapreg(env->phys_regs[i]); 1968 } 1969 } 1970 1971 #define USE_ELF_CORE_DUMP 1972 #define ELF_EXEC_PAGESIZE 4096 1973 1974 #endif /* TARGET_XTENSA */ 1975 1976 #ifdef TARGET_HEXAGON 1977 1978 #define ELF_CLASS ELFCLASS32 1979 #define ELF_ARCH EM_HEXAGON 1980 1981 static inline void init_thread(struct target_pt_regs *regs, 1982 struct image_info *infop) 1983 { 1984 regs->sepc = infop->entry; 1985 regs->sp = infop->start_stack; 1986 } 1987 1988 #endif /* TARGET_HEXAGON */ 1989 1990 #ifndef ELF_BASE_PLATFORM 1991 #define ELF_BASE_PLATFORM (NULL) 1992 #endif 1993 1994 #ifndef ELF_PLATFORM 1995 #define ELF_PLATFORM (NULL) 1996 #endif 1997 1998 #ifndef ELF_MACHINE 1999 #define ELF_MACHINE ELF_ARCH 2000 #endif 2001 2002 #ifndef elf_check_arch 2003 #define elf_check_arch(x) ((x) == ELF_ARCH) 2004 #endif 2005 2006 #ifndef elf_check_abi 2007 #define elf_check_abi(x) (1) 2008 #endif 2009 2010 #ifndef ELF_HWCAP 2011 #define ELF_HWCAP 0 2012 #endif 2013 2014 #ifndef STACK_GROWS_DOWN 2015 #define STACK_GROWS_DOWN 1 2016 #endif 2017 2018 #ifndef STACK_ALIGNMENT 2019 #define STACK_ALIGNMENT 16 2020 #endif 2021 2022 #ifdef TARGET_ABI32 2023 #undef ELF_CLASS 2024 #define ELF_CLASS ELFCLASS32 2025 #undef bswaptls 2026 #define bswaptls(ptr) bswap32s(ptr) 2027 #endif 2028 2029 #ifndef EXSTACK_DEFAULT 2030 #define EXSTACK_DEFAULT false 2031 #endif 2032 2033 #include "elf.h" 2034 2035 /* We must delay the following stanzas until after "elf.h". */ 2036 #if defined(TARGET_AARCH64) 2037 2038 static bool arch_parse_elf_property(uint32_t pr_type, uint32_t pr_datasz, 2039 const uint32_t *data, 2040 struct image_info *info, 2041 Error **errp) 2042 { 2043 if (pr_type == GNU_PROPERTY_AARCH64_FEATURE_1_AND) { 2044 if (pr_datasz != sizeof(uint32_t)) { 2045 error_setg(errp, "Ill-formed GNU_PROPERTY_AARCH64_FEATURE_1_AND"); 2046 return false; 2047 } 2048 /* We will extract GNU_PROPERTY_AARCH64_FEATURE_1_BTI later. */ 2049 info->note_flags = *data; 2050 } 2051 return true; 2052 } 2053 #define ARCH_USE_GNU_PROPERTY 1 2054 2055 #else 2056 2057 static bool arch_parse_elf_property(uint32_t pr_type, uint32_t pr_datasz, 2058 const uint32_t *data, 2059 struct image_info *info, 2060 Error **errp) 2061 { 2062 g_assert_not_reached(); 2063 } 2064 #define ARCH_USE_GNU_PROPERTY 0 2065 2066 #endif 2067 2068 struct exec 2069 { 2070 unsigned int a_info; /* Use macros N_MAGIC, etc for access */ 2071 unsigned int a_text; /* length of text, in bytes */ 2072 unsigned int a_data; /* length of data, in bytes */ 2073 unsigned int a_bss; /* length of uninitialized data area, in bytes */ 2074 unsigned int a_syms; /* length of symbol table data in file, in bytes */ 2075 unsigned int a_entry; /* start address */ 2076 unsigned int a_trsize; /* length of relocation info for text, in bytes */ 2077 unsigned int a_drsize; /* length of relocation info for data, in bytes */ 2078 }; 2079 2080 2081 #define N_MAGIC(exec) ((exec).a_info & 0xffff) 2082 #define OMAGIC 0407 2083 #define NMAGIC 0410 2084 #define ZMAGIC 0413 2085 #define QMAGIC 0314 2086 2087 #define DLINFO_ITEMS 16 2088 2089 static inline void memcpy_fromfs(void * to, const void * from, unsigned long n) 2090 { 2091 memcpy(to, from, n); 2092 } 2093 2094 #ifdef BSWAP_NEEDED 2095 static void bswap_ehdr(struct elfhdr *ehdr) 2096 { 2097 bswap16s(&ehdr->e_type); /* Object file type */ 2098 bswap16s(&ehdr->e_machine); /* Architecture */ 2099 bswap32s(&ehdr->e_version); /* Object file version */ 2100 bswaptls(&ehdr->e_entry); /* Entry point virtual address */ 2101 bswaptls(&ehdr->e_phoff); /* Program header table file offset */ 2102 bswaptls(&ehdr->e_shoff); /* Section header table file offset */ 2103 bswap32s(&ehdr->e_flags); /* Processor-specific flags */ 2104 bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */ 2105 bswap16s(&ehdr->e_phentsize); /* Program header table entry size */ 2106 bswap16s(&ehdr->e_phnum); /* Program header table entry count */ 2107 bswap16s(&ehdr->e_shentsize); /* Section header table entry size */ 2108 bswap16s(&ehdr->e_shnum); /* Section header table entry count */ 2109 bswap16s(&ehdr->e_shstrndx); /* Section header string table index */ 2110 } 2111 2112 static void bswap_phdr(struct elf_phdr *phdr, int phnum) 2113 { 2114 int i; 2115 for (i = 0; i < phnum; ++i, ++phdr) { 2116 bswap32s(&phdr->p_type); /* Segment type */ 2117 bswap32s(&phdr->p_flags); /* Segment flags */ 2118 bswaptls(&phdr->p_offset); /* Segment file offset */ 2119 bswaptls(&phdr->p_vaddr); /* Segment virtual address */ 2120 bswaptls(&phdr->p_paddr); /* Segment physical address */ 2121 bswaptls(&phdr->p_filesz); /* Segment size in file */ 2122 bswaptls(&phdr->p_memsz); /* Segment size in memory */ 2123 bswaptls(&phdr->p_align); /* Segment alignment */ 2124 } 2125 } 2126 2127 static void bswap_shdr(struct elf_shdr *shdr, int shnum) 2128 { 2129 int i; 2130 for (i = 0; i < shnum; ++i, ++shdr) { 2131 bswap32s(&shdr->sh_name); 2132 bswap32s(&shdr->sh_type); 2133 bswaptls(&shdr->sh_flags); 2134 bswaptls(&shdr->sh_addr); 2135 bswaptls(&shdr->sh_offset); 2136 bswaptls(&shdr->sh_size); 2137 bswap32s(&shdr->sh_link); 2138 bswap32s(&shdr->sh_info); 2139 bswaptls(&shdr->sh_addralign); 2140 bswaptls(&shdr->sh_entsize); 2141 } 2142 } 2143 2144 static void bswap_sym(struct elf_sym *sym) 2145 { 2146 bswap32s(&sym->st_name); 2147 bswaptls(&sym->st_value); 2148 bswaptls(&sym->st_size); 2149 bswap16s(&sym->st_shndx); 2150 } 2151 2152 #ifdef TARGET_MIPS 2153 static void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags) 2154 { 2155 bswap16s(&abiflags->version); 2156 bswap32s(&abiflags->ases); 2157 bswap32s(&abiflags->isa_ext); 2158 bswap32s(&abiflags->flags1); 2159 bswap32s(&abiflags->flags2); 2160 } 2161 #endif 2162 #else 2163 static inline void bswap_ehdr(struct elfhdr *ehdr) { } 2164 static inline void bswap_phdr(struct elf_phdr *phdr, int phnum) { } 2165 static inline void bswap_shdr(struct elf_shdr *shdr, int shnum) { } 2166 static inline void bswap_sym(struct elf_sym *sym) { } 2167 #ifdef TARGET_MIPS 2168 static inline void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags) { } 2169 #endif 2170 #endif 2171 2172 #ifdef USE_ELF_CORE_DUMP 2173 static int elf_core_dump(int, const CPUArchState *); 2174 #endif /* USE_ELF_CORE_DUMP */ 2175 static void load_symbols(struct elfhdr *hdr, const ImageSource *src, 2176 abi_ulong load_bias); 2177 2178 /* Verify the portions of EHDR within E_IDENT for the target. 2179 This can be performed before bswapping the entire header. */ 2180 static bool elf_check_ident(struct elfhdr *ehdr) 2181 { 2182 return (ehdr->e_ident[EI_MAG0] == ELFMAG0 2183 && ehdr->e_ident[EI_MAG1] == ELFMAG1 2184 && ehdr->e_ident[EI_MAG2] == ELFMAG2 2185 && ehdr->e_ident[EI_MAG3] == ELFMAG3 2186 && ehdr->e_ident[EI_CLASS] == ELF_CLASS 2187 && ehdr->e_ident[EI_DATA] == ELF_DATA 2188 && ehdr->e_ident[EI_VERSION] == EV_CURRENT); 2189 } 2190 2191 /* Verify the portions of EHDR outside of E_IDENT for the target. 2192 This has to wait until after bswapping the header. */ 2193 static bool elf_check_ehdr(struct elfhdr *ehdr) 2194 { 2195 return (elf_check_arch(ehdr->e_machine) 2196 && elf_check_abi(ehdr->e_flags) 2197 && ehdr->e_ehsize == sizeof(struct elfhdr) 2198 && ehdr->e_phentsize == sizeof(struct elf_phdr) 2199 && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN)); 2200 } 2201 2202 /* 2203 * 'copy_elf_strings()' copies argument/envelope strings from user 2204 * memory to free pages in kernel mem. These are in a format ready 2205 * to be put directly into the top of new user memory. 2206 * 2207 */ 2208 static abi_ulong copy_elf_strings(int argc, char **argv, char *scratch, 2209 abi_ulong p, abi_ulong stack_limit) 2210 { 2211 char *tmp; 2212 int len, i; 2213 abi_ulong top = p; 2214 2215 if (!p) { 2216 return 0; /* bullet-proofing */ 2217 } 2218 2219 if (STACK_GROWS_DOWN) { 2220 int offset = ((p - 1) % TARGET_PAGE_SIZE) + 1; 2221 for (i = argc - 1; i >= 0; --i) { 2222 tmp = argv[i]; 2223 if (!tmp) { 2224 fprintf(stderr, "VFS: argc is wrong"); 2225 exit(-1); 2226 } 2227 len = strlen(tmp) + 1; 2228 tmp += len; 2229 2230 if (len > (p - stack_limit)) { 2231 return 0; 2232 } 2233 while (len) { 2234 int bytes_to_copy = (len > offset) ? offset : len; 2235 tmp -= bytes_to_copy; 2236 p -= bytes_to_copy; 2237 offset -= bytes_to_copy; 2238 len -= bytes_to_copy; 2239 2240 memcpy_fromfs(scratch + offset, tmp, bytes_to_copy); 2241 2242 if (offset == 0) { 2243 memcpy_to_target(p, scratch, top - p); 2244 top = p; 2245 offset = TARGET_PAGE_SIZE; 2246 } 2247 } 2248 } 2249 if (p != top) { 2250 memcpy_to_target(p, scratch + offset, top - p); 2251 } 2252 } else { 2253 int remaining = TARGET_PAGE_SIZE - (p % TARGET_PAGE_SIZE); 2254 for (i = 0; i < argc; ++i) { 2255 tmp = argv[i]; 2256 if (!tmp) { 2257 fprintf(stderr, "VFS: argc is wrong"); 2258 exit(-1); 2259 } 2260 len = strlen(tmp) + 1; 2261 if (len > (stack_limit - p)) { 2262 return 0; 2263 } 2264 while (len) { 2265 int bytes_to_copy = (len > remaining) ? remaining : len; 2266 2267 memcpy_fromfs(scratch + (p - top), tmp, bytes_to_copy); 2268 2269 tmp += bytes_to_copy; 2270 remaining -= bytes_to_copy; 2271 p += bytes_to_copy; 2272 len -= bytes_to_copy; 2273 2274 if (remaining == 0) { 2275 memcpy_to_target(top, scratch, p - top); 2276 top = p; 2277 remaining = TARGET_PAGE_SIZE; 2278 } 2279 } 2280 } 2281 if (p != top) { 2282 memcpy_to_target(top, scratch, p - top); 2283 } 2284 } 2285 2286 return p; 2287 } 2288 2289 /* Older linux kernels provide up to MAX_ARG_PAGES (default: 32) of 2290 * argument/environment space. Newer kernels (>2.6.33) allow more, 2291 * dependent on stack size, but guarantee at least 32 pages for 2292 * backwards compatibility. 2293 */ 2294 #define STACK_LOWER_LIMIT (32 * TARGET_PAGE_SIZE) 2295 2296 static abi_ulong setup_arg_pages(struct linux_binprm *bprm, 2297 struct image_info *info) 2298 { 2299 abi_ulong size, error, guard; 2300 int prot; 2301 2302 size = guest_stack_size; 2303 if (size < STACK_LOWER_LIMIT) { 2304 size = STACK_LOWER_LIMIT; 2305 } 2306 2307 if (STACK_GROWS_DOWN) { 2308 guard = TARGET_PAGE_SIZE; 2309 if (guard < qemu_real_host_page_size()) { 2310 guard = qemu_real_host_page_size(); 2311 } 2312 } else { 2313 /* no guard page for hppa target where stack grows upwards. */ 2314 guard = 0; 2315 } 2316 2317 prot = PROT_READ | PROT_WRITE; 2318 if (info->exec_stack) { 2319 prot |= PROT_EXEC; 2320 } 2321 error = target_mmap(0, size + guard, prot, 2322 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 2323 if (error == -1) { 2324 perror("mmap stack"); 2325 exit(-1); 2326 } 2327 2328 /* We reserve one extra page at the top of the stack as guard. */ 2329 if (STACK_GROWS_DOWN) { 2330 target_mprotect(error, guard, PROT_NONE); 2331 info->stack_limit = error + guard; 2332 return info->stack_limit + size - sizeof(void *); 2333 } else { 2334 info->stack_limit = error + size; 2335 return error; 2336 } 2337 } 2338 2339 /** 2340 * zero_bss: 2341 * 2342 * Map and zero the bss. We need to explicitly zero any fractional pages 2343 * after the data section (i.e. bss). Return false on mapping failure. 2344 */ 2345 static bool zero_bss(abi_ulong start_bss, abi_ulong end_bss, 2346 int prot, Error **errp) 2347 { 2348 abi_ulong align_bss; 2349 2350 /* We only expect writable bss; the code segment shouldn't need this. */ 2351 if (!(prot & PROT_WRITE)) { 2352 error_setg(errp, "PT_LOAD with non-writable bss"); 2353 return false; 2354 } 2355 2356 align_bss = TARGET_PAGE_ALIGN(start_bss); 2357 end_bss = TARGET_PAGE_ALIGN(end_bss); 2358 2359 if (start_bss < align_bss) { 2360 int flags = page_get_flags(start_bss); 2361 2362 if (!(flags & PAGE_BITS)) { 2363 /* 2364 * The whole address space of the executable was reserved 2365 * at the start, therefore all pages will be VALID. 2366 * But assuming there are no PROT_NONE PT_LOAD segments, 2367 * a PROT_NONE page means no data all bss, and we can 2368 * simply extend the new anon mapping back to the start 2369 * of the page of bss. 2370 */ 2371 align_bss -= TARGET_PAGE_SIZE; 2372 } else { 2373 /* 2374 * The start of the bss shares a page with something. 2375 * The only thing that we expect is the data section, 2376 * which would already be marked writable. 2377 * Overlapping the RX code segment seems malformed. 2378 */ 2379 if (!(flags & PAGE_WRITE)) { 2380 error_setg(errp, "PT_LOAD with bss overlapping " 2381 "non-writable page"); 2382 return false; 2383 } 2384 2385 /* The page is already mapped and writable. */ 2386 memset(g2h_untagged(start_bss), 0, align_bss - start_bss); 2387 } 2388 } 2389 2390 if (align_bss < end_bss && 2391 target_mmap(align_bss, end_bss - align_bss, prot, 2392 MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0) == -1) { 2393 error_setg_errno(errp, errno, "Error mapping bss"); 2394 return false; 2395 } 2396 return true; 2397 } 2398 2399 #if defined(TARGET_ARM) 2400 static int elf_is_fdpic(struct elfhdr *exec) 2401 { 2402 return exec->e_ident[EI_OSABI] == ELFOSABI_ARM_FDPIC; 2403 } 2404 #elif defined(TARGET_XTENSA) 2405 static int elf_is_fdpic(struct elfhdr *exec) 2406 { 2407 return exec->e_ident[EI_OSABI] == ELFOSABI_XTENSA_FDPIC; 2408 } 2409 #else 2410 /* Default implementation, always false. */ 2411 static int elf_is_fdpic(struct elfhdr *exec) 2412 { 2413 return 0; 2414 } 2415 #endif 2416 2417 static abi_ulong loader_build_fdpic_loadmap(struct image_info *info, abi_ulong sp) 2418 { 2419 uint16_t n; 2420 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs; 2421 2422 /* elf32_fdpic_loadseg */ 2423 n = info->nsegs; 2424 while (n--) { 2425 sp -= 12; 2426 put_user_u32(loadsegs[n].addr, sp+0); 2427 put_user_u32(loadsegs[n].p_vaddr, sp+4); 2428 put_user_u32(loadsegs[n].p_memsz, sp+8); 2429 } 2430 2431 /* elf32_fdpic_loadmap */ 2432 sp -= 4; 2433 put_user_u16(0, sp+0); /* version */ 2434 put_user_u16(info->nsegs, sp+2); /* nsegs */ 2435 2436 info->personality = PER_LINUX_FDPIC; 2437 info->loadmap_addr = sp; 2438 2439 return sp; 2440 } 2441 2442 static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc, 2443 struct elfhdr *exec, 2444 struct image_info *info, 2445 struct image_info *interp_info, 2446 struct image_info *vdso_info) 2447 { 2448 abi_ulong sp; 2449 abi_ulong u_argc, u_argv, u_envp, u_auxv; 2450 int size; 2451 int i; 2452 abi_ulong u_rand_bytes; 2453 uint8_t k_rand_bytes[16]; 2454 abi_ulong u_platform, u_base_platform; 2455 const char *k_platform, *k_base_platform; 2456 const int n = sizeof(elf_addr_t); 2457 2458 sp = p; 2459 2460 /* Needs to be before we load the env/argc/... */ 2461 if (elf_is_fdpic(exec)) { 2462 /* Need 4 byte alignment for these structs */ 2463 sp &= ~3; 2464 sp = loader_build_fdpic_loadmap(info, sp); 2465 info->other_info = interp_info; 2466 if (interp_info) { 2467 interp_info->other_info = info; 2468 sp = loader_build_fdpic_loadmap(interp_info, sp); 2469 info->interpreter_loadmap_addr = interp_info->loadmap_addr; 2470 info->interpreter_pt_dynamic_addr = interp_info->pt_dynamic_addr; 2471 } else { 2472 info->interpreter_loadmap_addr = 0; 2473 info->interpreter_pt_dynamic_addr = 0; 2474 } 2475 } 2476 2477 u_base_platform = 0; 2478 k_base_platform = ELF_BASE_PLATFORM; 2479 if (k_base_platform) { 2480 size_t len = strlen(k_base_platform) + 1; 2481 if (STACK_GROWS_DOWN) { 2482 sp -= (len + n - 1) & ~(n - 1); 2483 u_base_platform = sp; 2484 /* FIXME - check return value of memcpy_to_target() for failure */ 2485 memcpy_to_target(sp, k_base_platform, len); 2486 } else { 2487 memcpy_to_target(sp, k_base_platform, len); 2488 u_base_platform = sp; 2489 sp += len + 1; 2490 } 2491 } 2492 2493 u_platform = 0; 2494 k_platform = ELF_PLATFORM; 2495 if (k_platform) { 2496 size_t len = strlen(k_platform) + 1; 2497 if (STACK_GROWS_DOWN) { 2498 sp -= (len + n - 1) & ~(n - 1); 2499 u_platform = sp; 2500 /* FIXME - check return value of memcpy_to_target() for failure */ 2501 memcpy_to_target(sp, k_platform, len); 2502 } else { 2503 memcpy_to_target(sp, k_platform, len); 2504 u_platform = sp; 2505 sp += len + 1; 2506 } 2507 } 2508 2509 /* Provide 16 byte alignment for the PRNG, and basic alignment for 2510 * the argv and envp pointers. 2511 */ 2512 if (STACK_GROWS_DOWN) { 2513 sp = QEMU_ALIGN_DOWN(sp, 16); 2514 } else { 2515 sp = QEMU_ALIGN_UP(sp, 16); 2516 } 2517 2518 /* 2519 * Generate 16 random bytes for userspace PRNG seeding. 2520 */ 2521 qemu_guest_getrandom_nofail(k_rand_bytes, sizeof(k_rand_bytes)); 2522 if (STACK_GROWS_DOWN) { 2523 sp -= 16; 2524 u_rand_bytes = sp; 2525 /* FIXME - check return value of memcpy_to_target() for failure */ 2526 memcpy_to_target(sp, k_rand_bytes, 16); 2527 } else { 2528 memcpy_to_target(sp, k_rand_bytes, 16); 2529 u_rand_bytes = sp; 2530 sp += 16; 2531 } 2532 2533 size = (DLINFO_ITEMS + 1) * 2; 2534 if (k_base_platform) { 2535 size += 2; 2536 } 2537 if (k_platform) { 2538 size += 2; 2539 } 2540 if (vdso_info) { 2541 size += 2; 2542 } 2543 #ifdef DLINFO_ARCH_ITEMS 2544 size += DLINFO_ARCH_ITEMS * 2; 2545 #endif 2546 #ifdef ELF_HWCAP2 2547 size += 2; 2548 #endif 2549 info->auxv_len = size * n; 2550 2551 size += envc + argc + 2; 2552 size += 1; /* argc itself */ 2553 size *= n; 2554 2555 /* Allocate space and finalize stack alignment for entry now. */ 2556 if (STACK_GROWS_DOWN) { 2557 u_argc = QEMU_ALIGN_DOWN(sp - size, STACK_ALIGNMENT); 2558 sp = u_argc; 2559 } else { 2560 u_argc = sp; 2561 sp = QEMU_ALIGN_UP(sp + size, STACK_ALIGNMENT); 2562 } 2563 2564 u_argv = u_argc + n; 2565 u_envp = u_argv + (argc + 1) * n; 2566 u_auxv = u_envp + (envc + 1) * n; 2567 info->saved_auxv = u_auxv; 2568 info->argc = argc; 2569 info->envc = envc; 2570 info->argv = u_argv; 2571 info->envp = u_envp; 2572 2573 /* This is correct because Linux defines 2574 * elf_addr_t as Elf32_Off / Elf64_Off 2575 */ 2576 #define NEW_AUX_ENT(id, val) do { \ 2577 put_user_ual(id, u_auxv); u_auxv += n; \ 2578 put_user_ual(val, u_auxv); u_auxv += n; \ 2579 } while(0) 2580 2581 #ifdef ARCH_DLINFO 2582 /* 2583 * ARCH_DLINFO must come first so platform specific code can enforce 2584 * special alignment requirements on the AUXV if necessary (eg. PPC). 2585 */ 2586 ARCH_DLINFO; 2587 #endif 2588 /* There must be exactly DLINFO_ITEMS entries here, or the assert 2589 * on info->auxv_len will trigger. 2590 */ 2591 NEW_AUX_ENT(AT_PHDR, (abi_ulong)(info->load_addr + exec->e_phoff)); 2592 NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr))); 2593 NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum)); 2594 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE)); 2595 NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_info ? interp_info->load_addr : 0)); 2596 NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0); 2597 NEW_AUX_ENT(AT_ENTRY, info->entry); 2598 NEW_AUX_ENT(AT_UID, (abi_ulong) getuid()); 2599 NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid()); 2600 NEW_AUX_ENT(AT_GID, (abi_ulong) getgid()); 2601 NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid()); 2602 NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP); 2603 NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK)); 2604 NEW_AUX_ENT(AT_RANDOM, (abi_ulong) u_rand_bytes); 2605 NEW_AUX_ENT(AT_SECURE, (abi_ulong) qemu_getauxval(AT_SECURE)); 2606 NEW_AUX_ENT(AT_EXECFN, info->file_string); 2607 2608 #ifdef ELF_HWCAP2 2609 NEW_AUX_ENT(AT_HWCAP2, (abi_ulong) ELF_HWCAP2); 2610 #endif 2611 2612 if (u_base_platform) { 2613 NEW_AUX_ENT(AT_BASE_PLATFORM, u_base_platform); 2614 } 2615 if (u_platform) { 2616 NEW_AUX_ENT(AT_PLATFORM, u_platform); 2617 } 2618 if (vdso_info) { 2619 NEW_AUX_ENT(AT_SYSINFO_EHDR, vdso_info->load_addr); 2620 } 2621 NEW_AUX_ENT (AT_NULL, 0); 2622 #undef NEW_AUX_ENT 2623 2624 /* Check that our initial calculation of the auxv length matches how much 2625 * we actually put into it. 2626 */ 2627 assert(info->auxv_len == u_auxv - info->saved_auxv); 2628 2629 put_user_ual(argc, u_argc); 2630 2631 p = info->arg_strings; 2632 for (i = 0; i < argc; ++i) { 2633 put_user_ual(p, u_argv); 2634 u_argv += n; 2635 p += target_strlen(p) + 1; 2636 } 2637 put_user_ual(0, u_argv); 2638 2639 p = info->env_strings; 2640 for (i = 0; i < envc; ++i) { 2641 put_user_ual(p, u_envp); 2642 u_envp += n; 2643 p += target_strlen(p) + 1; 2644 } 2645 put_user_ual(0, u_envp); 2646 2647 return sp; 2648 } 2649 2650 #if defined(HI_COMMPAGE) 2651 #define LO_COMMPAGE -1 2652 #elif defined(LO_COMMPAGE) 2653 #define HI_COMMPAGE 0 2654 #else 2655 #define HI_COMMPAGE 0 2656 #define LO_COMMPAGE -1 2657 #ifndef INIT_GUEST_COMMPAGE 2658 #define init_guest_commpage() true 2659 #endif 2660 #endif 2661 2662 /** 2663 * pgb_try_mmap: 2664 * @addr: host start address 2665 * @addr_last: host last address 2666 * @keep: do not unmap the probe region 2667 * 2668 * Return 1 if [@addr, @addr_last] is not mapped in the host, 2669 * return 0 if it is not available to map, and -1 on mmap error. 2670 * If @keep, the region is left mapped on success, otherwise unmapped. 2671 */ 2672 static int pgb_try_mmap(uintptr_t addr, uintptr_t addr_last, bool keep) 2673 { 2674 size_t size = addr_last - addr + 1; 2675 void *p = mmap((void *)addr, size, PROT_NONE, 2676 MAP_ANONYMOUS | MAP_PRIVATE | 2677 MAP_NORESERVE | MAP_FIXED_NOREPLACE, -1, 0); 2678 int ret; 2679 2680 if (p == MAP_FAILED) { 2681 return errno == EEXIST ? 0 : -1; 2682 } 2683 ret = p == (void *)addr; 2684 if (!keep || !ret) { 2685 munmap(p, size); 2686 } 2687 return ret; 2688 } 2689 2690 /** 2691 * pgb_try_mmap_skip_brk(uintptr_t addr, uintptr_t size, uintptr_t brk) 2692 * @addr: host address 2693 * @addr_last: host last address 2694 * @brk: host brk 2695 * 2696 * Like pgb_try_mmap, but additionally reserve some memory following brk. 2697 */ 2698 static int pgb_try_mmap_skip_brk(uintptr_t addr, uintptr_t addr_last, 2699 uintptr_t brk, bool keep) 2700 { 2701 uintptr_t brk_last = brk + 16 * MiB - 1; 2702 2703 /* Do not map anything close to the host brk. */ 2704 if (addr <= brk_last && brk <= addr_last) { 2705 return 0; 2706 } 2707 return pgb_try_mmap(addr, addr_last, keep); 2708 } 2709 2710 /** 2711 * pgb_try_mmap_set: 2712 * @ga: set of guest addrs 2713 * @base: guest_base 2714 * @brk: host brk 2715 * 2716 * Return true if all @ga can be mapped by the host at @base. 2717 * On success, retain the mapping at index 0 for reserved_va. 2718 */ 2719 2720 typedef struct PGBAddrs { 2721 uintptr_t bounds[3][2]; /* start/last pairs */ 2722 int nbounds; 2723 } PGBAddrs; 2724 2725 static bool pgb_try_mmap_set(const PGBAddrs *ga, uintptr_t base, uintptr_t brk) 2726 { 2727 for (int i = ga->nbounds - 1; i >= 0; --i) { 2728 if (pgb_try_mmap_skip_brk(ga->bounds[i][0] + base, 2729 ga->bounds[i][1] + base, 2730 brk, i == 0 && reserved_va) <= 0) { 2731 return false; 2732 } 2733 } 2734 return true; 2735 } 2736 2737 /** 2738 * pgb_addr_set: 2739 * @ga: output set of guest addrs 2740 * @guest_loaddr: guest image low address 2741 * @guest_loaddr: guest image high address 2742 * @identity: create for identity mapping 2743 * 2744 * Fill in @ga with the image, COMMPAGE and NULL page. 2745 */ 2746 static bool pgb_addr_set(PGBAddrs *ga, abi_ulong guest_loaddr, 2747 abi_ulong guest_hiaddr, bool try_identity) 2748 { 2749 int n; 2750 2751 /* 2752 * With a low commpage, or a guest mapped very low, 2753 * we may not be able to use the identity map. 2754 */ 2755 if (try_identity) { 2756 if (LO_COMMPAGE != -1 && LO_COMMPAGE < mmap_min_addr) { 2757 return false; 2758 } 2759 if (guest_loaddr != 0 && guest_loaddr < mmap_min_addr) { 2760 return false; 2761 } 2762 } 2763 2764 memset(ga, 0, sizeof(*ga)); 2765 n = 0; 2766 2767 if (reserved_va) { 2768 ga->bounds[n][0] = try_identity ? mmap_min_addr : 0; 2769 ga->bounds[n][1] = reserved_va; 2770 n++; 2771 /* LO_COMMPAGE and NULL handled by reserving from 0. */ 2772 } else { 2773 /* Add any LO_COMMPAGE or NULL page. */ 2774 if (LO_COMMPAGE != -1) { 2775 ga->bounds[n][0] = 0; 2776 ga->bounds[n][1] = LO_COMMPAGE + TARGET_PAGE_SIZE - 1; 2777 n++; 2778 } else if (!try_identity) { 2779 ga->bounds[n][0] = 0; 2780 ga->bounds[n][1] = TARGET_PAGE_SIZE - 1; 2781 n++; 2782 } 2783 2784 /* Add the guest image for ET_EXEC. */ 2785 if (guest_loaddr) { 2786 ga->bounds[n][0] = guest_loaddr; 2787 ga->bounds[n][1] = guest_hiaddr; 2788 n++; 2789 } 2790 } 2791 2792 /* 2793 * Temporarily disable 2794 * "comparison is always false due to limited range of data type" 2795 * due to comparison between unsigned and (possible) 0. 2796 */ 2797 #pragma GCC diagnostic push 2798 #pragma GCC diagnostic ignored "-Wtype-limits" 2799 2800 /* Add any HI_COMMPAGE not covered by reserved_va. */ 2801 if (reserved_va < HI_COMMPAGE) { 2802 ga->bounds[n][0] = HI_COMMPAGE & qemu_real_host_page_mask(); 2803 ga->bounds[n][1] = HI_COMMPAGE + TARGET_PAGE_SIZE - 1; 2804 n++; 2805 } 2806 2807 #pragma GCC diagnostic pop 2808 2809 ga->nbounds = n; 2810 return true; 2811 } 2812 2813 static void pgb_fail_in_use(const char *image_name) 2814 { 2815 error_report("%s: requires virtual address space that is in use " 2816 "(omit the -B option or choose a different value)", 2817 image_name); 2818 exit(EXIT_FAILURE); 2819 } 2820 2821 static void pgb_fixed(const char *image_name, uintptr_t guest_loaddr, 2822 uintptr_t guest_hiaddr, uintptr_t align) 2823 { 2824 PGBAddrs ga; 2825 uintptr_t brk = (uintptr_t)sbrk(0); 2826 2827 if (!QEMU_IS_ALIGNED(guest_base, align)) { 2828 fprintf(stderr, "Requested guest base %p does not satisfy " 2829 "host minimum alignment (0x%" PRIxPTR ")\n", 2830 (void *)guest_base, align); 2831 exit(EXIT_FAILURE); 2832 } 2833 2834 if (!pgb_addr_set(&ga, guest_loaddr, guest_hiaddr, !guest_base) 2835 || !pgb_try_mmap_set(&ga, guest_base, brk)) { 2836 pgb_fail_in_use(image_name); 2837 } 2838 } 2839 2840 /** 2841 * pgb_find_fallback: 2842 * 2843 * This is a fallback method for finding holes in the host address space 2844 * if we don't have the benefit of being able to access /proc/self/map. 2845 * It can potentially take a very long time as we can only dumbly iterate 2846 * up the host address space seeing if the allocation would work. 2847 */ 2848 static uintptr_t pgb_find_fallback(const PGBAddrs *ga, uintptr_t align, 2849 uintptr_t brk) 2850 { 2851 /* TODO: come up with a better estimate of how much to skip. */ 2852 uintptr_t skip = sizeof(uintptr_t) == 4 ? MiB : GiB; 2853 2854 for (uintptr_t base = skip; ; base += skip) { 2855 base = ROUND_UP(base, align); 2856 if (pgb_try_mmap_set(ga, base, brk)) { 2857 return base; 2858 } 2859 if (base >= -skip) { 2860 return -1; 2861 } 2862 } 2863 } 2864 2865 static uintptr_t pgb_try_itree(const PGBAddrs *ga, uintptr_t base, 2866 IntervalTreeRoot *root) 2867 { 2868 for (int i = ga->nbounds - 1; i >= 0; --i) { 2869 uintptr_t s = base + ga->bounds[i][0]; 2870 uintptr_t l = base + ga->bounds[i][1]; 2871 IntervalTreeNode *n; 2872 2873 if (l < s) { 2874 /* Wraparound. Skip to advance S to mmap_min_addr. */ 2875 return mmap_min_addr - s; 2876 } 2877 2878 n = interval_tree_iter_first(root, s, l); 2879 if (n != NULL) { 2880 /* Conflict. Skip to advance S to LAST + 1. */ 2881 return n->last - s + 1; 2882 } 2883 } 2884 return 0; /* success */ 2885 } 2886 2887 static uintptr_t pgb_find_itree(const PGBAddrs *ga, IntervalTreeRoot *root, 2888 uintptr_t align, uintptr_t brk) 2889 { 2890 uintptr_t last = mmap_min_addr; 2891 uintptr_t base, skip; 2892 2893 while (true) { 2894 base = ROUND_UP(last, align); 2895 if (base < last) { 2896 return -1; 2897 } 2898 2899 skip = pgb_try_itree(ga, base, root); 2900 if (skip == 0) { 2901 break; 2902 } 2903 2904 last = base + skip; 2905 if (last < base) { 2906 return -1; 2907 } 2908 } 2909 2910 /* 2911 * We've chosen 'base' based on holes in the interval tree, 2912 * but we don't yet know if it is a valid host address. 2913 * Because it is the first matching hole, if the host addresses 2914 * are invalid we know there are no further matches. 2915 */ 2916 return pgb_try_mmap_set(ga, base, brk) ? base : -1; 2917 } 2918 2919 static void pgb_dynamic(const char *image_name, uintptr_t guest_loaddr, 2920 uintptr_t guest_hiaddr, uintptr_t align) 2921 { 2922 IntervalTreeRoot *root; 2923 uintptr_t brk, ret; 2924 PGBAddrs ga; 2925 2926 /* Try the identity map first. */ 2927 if (pgb_addr_set(&ga, guest_loaddr, guest_hiaddr, true)) { 2928 brk = (uintptr_t)sbrk(0); 2929 if (pgb_try_mmap_set(&ga, 0, brk)) { 2930 guest_base = 0; 2931 return; 2932 } 2933 } 2934 2935 /* 2936 * Rebuild the address set for non-identity map. 2937 * This differs in the mapping of the guest NULL page. 2938 */ 2939 pgb_addr_set(&ga, guest_loaddr, guest_hiaddr, false); 2940 2941 root = read_self_maps(); 2942 2943 /* Read brk after we've read the maps, which will malloc. */ 2944 brk = (uintptr_t)sbrk(0); 2945 2946 if (!root) { 2947 ret = pgb_find_fallback(&ga, align, brk); 2948 } else { 2949 /* 2950 * Reserve the area close to the host brk. 2951 * This will be freed with the rest of the tree. 2952 */ 2953 IntervalTreeNode *b = g_new0(IntervalTreeNode, 1); 2954 b->start = brk; 2955 b->last = brk + 16 * MiB - 1; 2956 interval_tree_insert(b, root); 2957 2958 ret = pgb_find_itree(&ga, root, align, brk); 2959 free_self_maps(root); 2960 } 2961 2962 if (ret == -1) { 2963 int w = TARGET_LONG_BITS / 4; 2964 2965 error_report("%s: Unable to find a guest_base to satisfy all " 2966 "guest address mapping requirements", image_name); 2967 2968 for (int i = 0; i < ga.nbounds; ++i) { 2969 error_printf(" %0*" PRIx64 "-%0*" PRIx64 "\n", 2970 w, (uint64_t)ga.bounds[i][0], 2971 w, (uint64_t)ga.bounds[i][1]); 2972 } 2973 exit(EXIT_FAILURE); 2974 } 2975 guest_base = ret; 2976 } 2977 2978 void probe_guest_base(const char *image_name, abi_ulong guest_loaddr, 2979 abi_ulong guest_hiaddr) 2980 { 2981 /* In order to use host shmat, we must be able to honor SHMLBA. */ 2982 uintptr_t align = MAX(SHMLBA, TARGET_PAGE_SIZE); 2983 2984 /* Sanity check the guest binary. */ 2985 if (reserved_va) { 2986 if (guest_hiaddr > reserved_va) { 2987 error_report("%s: requires more than reserved virtual " 2988 "address space (0x%" PRIx64 " > 0x%lx)", 2989 image_name, (uint64_t)guest_hiaddr, reserved_va); 2990 exit(EXIT_FAILURE); 2991 } 2992 } else { 2993 if (guest_hiaddr != (uintptr_t)guest_hiaddr) { 2994 error_report("%s: requires more virtual address space " 2995 "than the host can provide (0x%" PRIx64 ")", 2996 image_name, (uint64_t)guest_hiaddr + 1); 2997 exit(EXIT_FAILURE); 2998 } 2999 } 3000 3001 if (have_guest_base) { 3002 pgb_fixed(image_name, guest_loaddr, guest_hiaddr, align); 3003 } else { 3004 pgb_dynamic(image_name, guest_loaddr, guest_hiaddr, align); 3005 } 3006 3007 /* Reserve and initialize the commpage. */ 3008 if (!init_guest_commpage()) { 3009 /* We have already probed for the commpage being free. */ 3010 g_assert_not_reached(); 3011 } 3012 3013 assert(QEMU_IS_ALIGNED(guest_base, align)); 3014 qemu_log_mask(CPU_LOG_PAGE, "Locating guest address space " 3015 "@ 0x%" PRIx64 "\n", (uint64_t)guest_base); 3016 } 3017 3018 enum { 3019 /* The string "GNU\0" as a magic number. */ 3020 GNU0_MAGIC = const_le32('G' | 'N' << 8 | 'U' << 16), 3021 NOTE_DATA_SZ = 1 * KiB, 3022 NOTE_NAME_SZ = 4, 3023 ELF_GNU_PROPERTY_ALIGN = ELF_CLASS == ELFCLASS32 ? 4 : 8, 3024 }; 3025 3026 /* 3027 * Process a single gnu_property entry. 3028 * Return false for error. 3029 */ 3030 static bool parse_elf_property(const uint32_t *data, int *off, int datasz, 3031 struct image_info *info, bool have_prev_type, 3032 uint32_t *prev_type, Error **errp) 3033 { 3034 uint32_t pr_type, pr_datasz, step; 3035 3036 if (*off > datasz || !QEMU_IS_ALIGNED(*off, ELF_GNU_PROPERTY_ALIGN)) { 3037 goto error_data; 3038 } 3039 datasz -= *off; 3040 data += *off / sizeof(uint32_t); 3041 3042 if (datasz < 2 * sizeof(uint32_t)) { 3043 goto error_data; 3044 } 3045 pr_type = data[0]; 3046 pr_datasz = data[1]; 3047 data += 2; 3048 datasz -= 2 * sizeof(uint32_t); 3049 step = ROUND_UP(pr_datasz, ELF_GNU_PROPERTY_ALIGN); 3050 if (step > datasz) { 3051 goto error_data; 3052 } 3053 3054 /* Properties are supposed to be unique and sorted on pr_type. */ 3055 if (have_prev_type && pr_type <= *prev_type) { 3056 if (pr_type == *prev_type) { 3057 error_setg(errp, "Duplicate property in PT_GNU_PROPERTY"); 3058 } else { 3059 error_setg(errp, "Unsorted property in PT_GNU_PROPERTY"); 3060 } 3061 return false; 3062 } 3063 *prev_type = pr_type; 3064 3065 if (!arch_parse_elf_property(pr_type, pr_datasz, data, info, errp)) { 3066 return false; 3067 } 3068 3069 *off += 2 * sizeof(uint32_t) + step; 3070 return true; 3071 3072 error_data: 3073 error_setg(errp, "Ill-formed property in PT_GNU_PROPERTY"); 3074 return false; 3075 } 3076 3077 /* Process NT_GNU_PROPERTY_TYPE_0. */ 3078 static bool parse_elf_properties(const ImageSource *src, 3079 struct image_info *info, 3080 const struct elf_phdr *phdr, 3081 Error **errp) 3082 { 3083 union { 3084 struct elf_note nhdr; 3085 uint32_t data[NOTE_DATA_SZ / sizeof(uint32_t)]; 3086 } note; 3087 3088 int n, off, datasz; 3089 bool have_prev_type; 3090 uint32_t prev_type; 3091 3092 /* Unless the arch requires properties, ignore them. */ 3093 if (!ARCH_USE_GNU_PROPERTY) { 3094 return true; 3095 } 3096 3097 /* If the properties are crazy large, that's too bad. */ 3098 n = phdr->p_filesz; 3099 if (n > sizeof(note)) { 3100 error_setg(errp, "PT_GNU_PROPERTY too large"); 3101 return false; 3102 } 3103 if (n < sizeof(note.nhdr)) { 3104 error_setg(errp, "PT_GNU_PROPERTY too small"); 3105 return false; 3106 } 3107 3108 if (!imgsrc_read(¬e, phdr->p_offset, n, src, errp)) { 3109 return false; 3110 } 3111 3112 /* 3113 * The contents of a valid PT_GNU_PROPERTY is a sequence 3114 * of uint32_t -- swap them all now. 3115 */ 3116 #ifdef BSWAP_NEEDED 3117 for (int i = 0; i < n / 4; i++) { 3118 bswap32s(note.data + i); 3119 } 3120 #endif 3121 3122 /* 3123 * Note that nhdr is 3 words, and that the "name" described by namesz 3124 * immediately follows nhdr and is thus at the 4th word. Further, all 3125 * of the inputs to the kernel's round_up are multiples of 4. 3126 */ 3127 if (note.nhdr.n_type != NT_GNU_PROPERTY_TYPE_0 || 3128 note.nhdr.n_namesz != NOTE_NAME_SZ || 3129 note.data[3] != GNU0_MAGIC) { 3130 error_setg(errp, "Invalid note in PT_GNU_PROPERTY"); 3131 return false; 3132 } 3133 off = sizeof(note.nhdr) + NOTE_NAME_SZ; 3134 3135 datasz = note.nhdr.n_descsz + off; 3136 if (datasz > n) { 3137 error_setg(errp, "Invalid note size in PT_GNU_PROPERTY"); 3138 return false; 3139 } 3140 3141 have_prev_type = false; 3142 prev_type = 0; 3143 while (1) { 3144 if (off == datasz) { 3145 return true; /* end, exit ok */ 3146 } 3147 if (!parse_elf_property(note.data, &off, datasz, info, 3148 have_prev_type, &prev_type, errp)) { 3149 return false; 3150 } 3151 have_prev_type = true; 3152 } 3153 } 3154 3155 /** 3156 * load_elf_image: Load an ELF image into the address space. 3157 * @image_name: the filename of the image, to use in error messages. 3158 * @src: the ImageSource from which to read. 3159 * @info: info collected from the loaded image. 3160 * @ehdr: the ELF header, not yet bswapped. 3161 * @pinterp_name: record any PT_INTERP string found. 3162 * 3163 * On return: @info values will be filled in, as necessary or available. 3164 */ 3165 3166 static void load_elf_image(const char *image_name, const ImageSource *src, 3167 struct image_info *info, struct elfhdr *ehdr, 3168 char **pinterp_name) 3169 { 3170 g_autofree struct elf_phdr *phdr = NULL; 3171 abi_ulong load_addr, load_bias, loaddr, hiaddr, error; 3172 int i, prot_exec; 3173 Error *err = NULL; 3174 3175 /* 3176 * First of all, some simple consistency checks. 3177 * Note that we rely on the bswapped ehdr staying in bprm_buf, 3178 * for later use by load_elf_binary and create_elf_tables. 3179 */ 3180 if (!imgsrc_read(ehdr, 0, sizeof(*ehdr), src, &err)) { 3181 goto exit_errmsg; 3182 } 3183 if (!elf_check_ident(ehdr)) { 3184 error_setg(&err, "Invalid ELF image for this architecture"); 3185 goto exit_errmsg; 3186 } 3187 bswap_ehdr(ehdr); 3188 if (!elf_check_ehdr(ehdr)) { 3189 error_setg(&err, "Invalid ELF image for this architecture"); 3190 goto exit_errmsg; 3191 } 3192 3193 phdr = imgsrc_read_alloc(ehdr->e_phoff, 3194 ehdr->e_phnum * sizeof(struct elf_phdr), 3195 src, &err); 3196 if (phdr == NULL) { 3197 goto exit_errmsg; 3198 } 3199 bswap_phdr(phdr, ehdr->e_phnum); 3200 3201 info->nsegs = 0; 3202 info->pt_dynamic_addr = 0; 3203 3204 mmap_lock(); 3205 3206 /* 3207 * Find the maximum size of the image and allocate an appropriate 3208 * amount of memory to handle that. Locate the interpreter, if any. 3209 */ 3210 loaddr = -1, hiaddr = 0; 3211 info->alignment = 0; 3212 info->exec_stack = EXSTACK_DEFAULT; 3213 for (i = 0; i < ehdr->e_phnum; ++i) { 3214 struct elf_phdr *eppnt = phdr + i; 3215 if (eppnt->p_type == PT_LOAD) { 3216 abi_ulong a = eppnt->p_vaddr & TARGET_PAGE_MASK; 3217 if (a < loaddr) { 3218 loaddr = a; 3219 } 3220 a = eppnt->p_vaddr + eppnt->p_memsz - 1; 3221 if (a > hiaddr) { 3222 hiaddr = a; 3223 } 3224 ++info->nsegs; 3225 info->alignment |= eppnt->p_align; 3226 } else if (eppnt->p_type == PT_INTERP && pinterp_name) { 3227 g_autofree char *interp_name = NULL; 3228 3229 if (*pinterp_name) { 3230 error_setg(&err, "Multiple PT_INTERP entries"); 3231 goto exit_errmsg; 3232 } 3233 3234 interp_name = imgsrc_read_alloc(eppnt->p_offset, eppnt->p_filesz, 3235 src, &err); 3236 if (interp_name == NULL) { 3237 goto exit_errmsg; 3238 } 3239 if (interp_name[eppnt->p_filesz - 1] != 0) { 3240 error_setg(&err, "Invalid PT_INTERP entry"); 3241 goto exit_errmsg; 3242 } 3243 *pinterp_name = g_steal_pointer(&interp_name); 3244 } else if (eppnt->p_type == PT_GNU_PROPERTY) { 3245 if (!parse_elf_properties(src, info, eppnt, &err)) { 3246 goto exit_errmsg; 3247 } 3248 } else if (eppnt->p_type == PT_GNU_STACK) { 3249 info->exec_stack = eppnt->p_flags & PF_X; 3250 } 3251 } 3252 3253 load_addr = loaddr; 3254 3255 if (pinterp_name != NULL) { 3256 if (ehdr->e_type == ET_EXEC) { 3257 /* 3258 * Make sure that the low address does not conflict with 3259 * MMAP_MIN_ADDR or the QEMU application itself. 3260 */ 3261 probe_guest_base(image_name, loaddr, hiaddr); 3262 } else { 3263 abi_ulong align; 3264 3265 /* 3266 * The binary is dynamic, but we still need to 3267 * select guest_base. In this case we pass a size. 3268 */ 3269 probe_guest_base(image_name, 0, hiaddr - loaddr); 3270 3271 /* 3272 * Avoid collision with the loader by providing a different 3273 * default load address. 3274 */ 3275 load_addr += elf_et_dyn_base; 3276 3277 /* 3278 * TODO: Better support for mmap alignment is desirable. 3279 * Since we do not have complete control over the guest 3280 * address space, we prefer the kernel to choose some address 3281 * rather than force the use of LOAD_ADDR via MAP_FIXED. 3282 * But without MAP_FIXED we cannot guarantee alignment, 3283 * only suggest it. 3284 */ 3285 align = pow2ceil(info->alignment); 3286 if (align) { 3287 load_addr &= -align; 3288 } 3289 } 3290 } 3291 3292 /* 3293 * Reserve address space for all of this. 3294 * 3295 * In the case of ET_EXEC, we supply MAP_FIXED_NOREPLACE so that we get 3296 * exactly the address range that is required. Without reserved_va, 3297 * the guest address space is not isolated. We have attempted to avoid 3298 * conflict with the host program itself via probe_guest_base, but using 3299 * MAP_FIXED_NOREPLACE instead of MAP_FIXED provides an extra check. 3300 * 3301 * Otherwise this is ET_DYN, and we are searching for a location 3302 * that can hold the memory space required. If the image is 3303 * pre-linked, LOAD_ADDR will be non-zero, and the kernel should 3304 * honor that address if it happens to be free. 3305 * 3306 * In both cases, we will overwrite pages in this range with mappings 3307 * from the executable. 3308 */ 3309 load_addr = target_mmap(load_addr, (size_t)hiaddr - loaddr + 1, PROT_NONE, 3310 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | 3311 (ehdr->e_type == ET_EXEC ? MAP_FIXED_NOREPLACE : 0), 3312 -1, 0); 3313 if (load_addr == -1) { 3314 goto exit_mmap; 3315 } 3316 load_bias = load_addr - loaddr; 3317 3318 if (elf_is_fdpic(ehdr)) { 3319 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs = 3320 g_malloc(sizeof(*loadsegs) * info->nsegs); 3321 3322 for (i = 0; i < ehdr->e_phnum; ++i) { 3323 switch (phdr[i].p_type) { 3324 case PT_DYNAMIC: 3325 info->pt_dynamic_addr = phdr[i].p_vaddr + load_bias; 3326 break; 3327 case PT_LOAD: 3328 loadsegs->addr = phdr[i].p_vaddr + load_bias; 3329 loadsegs->p_vaddr = phdr[i].p_vaddr; 3330 loadsegs->p_memsz = phdr[i].p_memsz; 3331 ++loadsegs; 3332 break; 3333 } 3334 } 3335 } 3336 3337 info->load_bias = load_bias; 3338 info->code_offset = load_bias; 3339 info->data_offset = load_bias; 3340 info->load_addr = load_addr; 3341 info->entry = ehdr->e_entry + load_bias; 3342 info->start_code = -1; 3343 info->end_code = 0; 3344 info->start_data = -1; 3345 info->end_data = 0; 3346 /* Usual start for brk is after all sections of the main executable. */ 3347 info->brk = TARGET_PAGE_ALIGN(hiaddr + load_bias); 3348 info->elf_flags = ehdr->e_flags; 3349 3350 prot_exec = PROT_EXEC; 3351 #ifdef TARGET_AARCH64 3352 /* 3353 * If the BTI feature is present, this indicates that the executable 3354 * pages of the startup binary should be mapped with PROT_BTI, so that 3355 * branch targets are enforced. 3356 * 3357 * The startup binary is either the interpreter or the static executable. 3358 * The interpreter is responsible for all pages of a dynamic executable. 3359 * 3360 * Elf notes are backward compatible to older cpus. 3361 * Do not enable BTI unless it is supported. 3362 */ 3363 if ((info->note_flags & GNU_PROPERTY_AARCH64_FEATURE_1_BTI) 3364 && (pinterp_name == NULL || *pinterp_name == 0) 3365 && cpu_isar_feature(aa64_bti, ARM_CPU(thread_cpu))) { 3366 prot_exec |= TARGET_PROT_BTI; 3367 } 3368 #endif 3369 3370 for (i = 0; i < ehdr->e_phnum; i++) { 3371 struct elf_phdr *eppnt = phdr + i; 3372 if (eppnt->p_type == PT_LOAD) { 3373 abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em; 3374 int elf_prot = 0; 3375 3376 if (eppnt->p_flags & PF_R) { 3377 elf_prot |= PROT_READ; 3378 } 3379 if (eppnt->p_flags & PF_W) { 3380 elf_prot |= PROT_WRITE; 3381 } 3382 if (eppnt->p_flags & PF_X) { 3383 elf_prot |= prot_exec; 3384 } 3385 3386 vaddr = load_bias + eppnt->p_vaddr; 3387 vaddr_po = vaddr & ~TARGET_PAGE_MASK; 3388 vaddr_ps = vaddr & TARGET_PAGE_MASK; 3389 3390 vaddr_ef = vaddr + eppnt->p_filesz; 3391 vaddr_em = vaddr + eppnt->p_memsz; 3392 3393 /* 3394 * Some segments may be completely empty, with a non-zero p_memsz 3395 * but no backing file segment. 3396 */ 3397 if (eppnt->p_filesz != 0) { 3398 error = imgsrc_mmap(vaddr_ps, eppnt->p_filesz + vaddr_po, 3399 elf_prot, MAP_PRIVATE | MAP_FIXED, 3400 src, eppnt->p_offset - vaddr_po); 3401 if (error == -1) { 3402 goto exit_mmap; 3403 } 3404 } 3405 3406 /* If the load segment requests extra zeros (e.g. bss), map it. */ 3407 if (vaddr_ef < vaddr_em && 3408 !zero_bss(vaddr_ef, vaddr_em, elf_prot, &err)) { 3409 goto exit_errmsg; 3410 } 3411 3412 /* Find the full program boundaries. */ 3413 if (elf_prot & PROT_EXEC) { 3414 if (vaddr < info->start_code) { 3415 info->start_code = vaddr; 3416 } 3417 if (vaddr_ef > info->end_code) { 3418 info->end_code = vaddr_ef; 3419 } 3420 } 3421 if (elf_prot & PROT_WRITE) { 3422 if (vaddr < info->start_data) { 3423 info->start_data = vaddr; 3424 } 3425 if (vaddr_ef > info->end_data) { 3426 info->end_data = vaddr_ef; 3427 } 3428 } 3429 #ifdef TARGET_MIPS 3430 } else if (eppnt->p_type == PT_MIPS_ABIFLAGS) { 3431 Mips_elf_abiflags_v0 abiflags; 3432 3433 if (!imgsrc_read(&abiflags, eppnt->p_offset, sizeof(abiflags), 3434 src, &err)) { 3435 goto exit_errmsg; 3436 } 3437 bswap_mips_abiflags(&abiflags); 3438 info->fp_abi = abiflags.fp_abi; 3439 #endif 3440 } 3441 } 3442 3443 if (info->end_data == 0) { 3444 info->start_data = info->end_code; 3445 info->end_data = info->end_code; 3446 } 3447 3448 if (qemu_log_enabled()) { 3449 load_symbols(ehdr, src, load_bias); 3450 } 3451 3452 debuginfo_report_elf(image_name, src->fd, load_bias); 3453 3454 mmap_unlock(); 3455 3456 close(src->fd); 3457 return; 3458 3459 exit_mmap: 3460 error_setg_errno(&err, errno, "Error mapping file"); 3461 goto exit_errmsg; 3462 exit_errmsg: 3463 error_reportf_err(err, "%s: ", image_name); 3464 exit(-1); 3465 } 3466 3467 static void load_elf_interp(const char *filename, struct image_info *info, 3468 char bprm_buf[BPRM_BUF_SIZE]) 3469 { 3470 struct elfhdr ehdr; 3471 ImageSource src; 3472 int fd, retval; 3473 Error *err = NULL; 3474 3475 fd = open(path(filename), O_RDONLY); 3476 if (fd < 0) { 3477 error_setg_file_open(&err, errno, filename); 3478 error_report_err(err); 3479 exit(-1); 3480 } 3481 3482 retval = read(fd, bprm_buf, BPRM_BUF_SIZE); 3483 if (retval < 0) { 3484 error_setg_errno(&err, errno, "Error reading file header"); 3485 error_reportf_err(err, "%s: ", filename); 3486 exit(-1); 3487 } 3488 3489 src.fd = fd; 3490 src.cache = bprm_buf; 3491 src.cache_size = retval; 3492 3493 load_elf_image(filename, &src, info, &ehdr, NULL); 3494 } 3495 3496 #ifdef VDSO_HEADER 3497 #include VDSO_HEADER 3498 #define vdso_image_info() &vdso_image_info 3499 #else 3500 #define vdso_image_info() NULL 3501 #endif 3502 3503 static void load_elf_vdso(struct image_info *info, const VdsoImageInfo *vdso) 3504 { 3505 ImageSource src; 3506 struct elfhdr ehdr; 3507 abi_ulong load_bias, load_addr; 3508 3509 src.fd = -1; 3510 src.cache = vdso->image; 3511 src.cache_size = vdso->image_size; 3512 3513 load_elf_image("<internal-vdso>", &src, info, &ehdr, NULL); 3514 load_addr = info->load_addr; 3515 load_bias = info->load_bias; 3516 3517 /* 3518 * We need to relocate the VDSO image. The one built into the kernel 3519 * is built for a fixed address. The one built for QEMU is not, since 3520 * that requires close control of the guest address space. 3521 * We pre-processed the image to locate all of the addresses that need 3522 * to be updated. 3523 */ 3524 for (unsigned i = 0, n = vdso->reloc_count; i < n; i++) { 3525 abi_ulong *addr = g2h_untagged(load_addr + vdso->relocs[i]); 3526 *addr = tswapal(tswapal(*addr) + load_bias); 3527 } 3528 3529 /* Install signal trampolines, if present. */ 3530 if (vdso->sigreturn_ofs) { 3531 default_sigreturn = load_addr + vdso->sigreturn_ofs; 3532 } 3533 if (vdso->rt_sigreturn_ofs) { 3534 default_rt_sigreturn = load_addr + vdso->rt_sigreturn_ofs; 3535 } 3536 3537 /* Remove write from VDSO segment. */ 3538 target_mprotect(info->start_data, info->end_data - info->start_data, 3539 PROT_READ | PROT_EXEC); 3540 } 3541 3542 static int symfind(const void *s0, const void *s1) 3543 { 3544 struct elf_sym *sym = (struct elf_sym *)s1; 3545 __typeof(sym->st_value) addr = *(uint64_t *)s0; 3546 int result = 0; 3547 3548 if (addr < sym->st_value) { 3549 result = -1; 3550 } else if (addr >= sym->st_value + sym->st_size) { 3551 result = 1; 3552 } 3553 return result; 3554 } 3555 3556 static const char *lookup_symbolxx(struct syminfo *s, uint64_t orig_addr) 3557 { 3558 #if ELF_CLASS == ELFCLASS32 3559 struct elf_sym *syms = s->disas_symtab.elf32; 3560 #else 3561 struct elf_sym *syms = s->disas_symtab.elf64; 3562 #endif 3563 3564 // binary search 3565 struct elf_sym *sym; 3566 3567 sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), symfind); 3568 if (sym != NULL) { 3569 return s->disas_strtab + sym->st_name; 3570 } 3571 3572 return ""; 3573 } 3574 3575 /* FIXME: This should use elf_ops.h.inc */ 3576 static int symcmp(const void *s0, const void *s1) 3577 { 3578 struct elf_sym *sym0 = (struct elf_sym *)s0; 3579 struct elf_sym *sym1 = (struct elf_sym *)s1; 3580 return (sym0->st_value < sym1->st_value) 3581 ? -1 3582 : ((sym0->st_value > sym1->st_value) ? 1 : 0); 3583 } 3584 3585 /* Best attempt to load symbols from this ELF object. */ 3586 static void load_symbols(struct elfhdr *hdr, const ImageSource *src, 3587 abi_ulong load_bias) 3588 { 3589 int i, shnum, nsyms, sym_idx = 0, str_idx = 0; 3590 g_autofree struct elf_shdr *shdr = NULL; 3591 char *strings = NULL; 3592 struct elf_sym *syms = NULL; 3593 struct elf_sym *new_syms; 3594 uint64_t segsz; 3595 3596 shnum = hdr->e_shnum; 3597 shdr = imgsrc_read_alloc(hdr->e_shoff, shnum * sizeof(struct elf_shdr), 3598 src, NULL); 3599 if (shdr == NULL) { 3600 return; 3601 } 3602 3603 bswap_shdr(shdr, shnum); 3604 for (i = 0; i < shnum; ++i) { 3605 if (shdr[i].sh_type == SHT_SYMTAB) { 3606 sym_idx = i; 3607 str_idx = shdr[i].sh_link; 3608 goto found; 3609 } 3610 } 3611 3612 /* There will be no symbol table if the file was stripped. */ 3613 return; 3614 3615 found: 3616 /* Now know where the strtab and symtab are. Snarf them. */ 3617 3618 segsz = shdr[str_idx].sh_size; 3619 strings = g_try_malloc(segsz); 3620 if (!strings) { 3621 goto give_up; 3622 } 3623 if (!imgsrc_read(strings, shdr[str_idx].sh_offset, segsz, src, NULL)) { 3624 goto give_up; 3625 } 3626 3627 segsz = shdr[sym_idx].sh_size; 3628 if (segsz / sizeof(struct elf_sym) > INT_MAX) { 3629 /* 3630 * Implausibly large symbol table: give up rather than ploughing 3631 * on with the number of symbols calculation overflowing. 3632 */ 3633 goto give_up; 3634 } 3635 nsyms = segsz / sizeof(struct elf_sym); 3636 syms = g_try_malloc(segsz); 3637 if (!syms) { 3638 goto give_up; 3639 } 3640 if (!imgsrc_read(syms, shdr[sym_idx].sh_offset, segsz, src, NULL)) { 3641 goto give_up; 3642 } 3643 3644 for (i = 0; i < nsyms; ) { 3645 bswap_sym(syms + i); 3646 /* Throw away entries which we do not need. */ 3647 if (syms[i].st_shndx == SHN_UNDEF 3648 || syms[i].st_shndx >= SHN_LORESERVE 3649 || ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) { 3650 if (i < --nsyms) { 3651 syms[i] = syms[nsyms]; 3652 } 3653 } else { 3654 #if defined(TARGET_ARM) || defined (TARGET_MIPS) 3655 /* The bottom address bit marks a Thumb or MIPS16 symbol. */ 3656 syms[i].st_value &= ~(target_ulong)1; 3657 #endif 3658 syms[i].st_value += load_bias; 3659 i++; 3660 } 3661 } 3662 3663 /* No "useful" symbol. */ 3664 if (nsyms == 0) { 3665 goto give_up; 3666 } 3667 3668 /* 3669 * Attempt to free the storage associated with the local symbols 3670 * that we threw away. Whether or not this has any effect on the 3671 * memory allocation depends on the malloc implementation and how 3672 * many symbols we managed to discard. 3673 */ 3674 new_syms = g_try_renew(struct elf_sym, syms, nsyms); 3675 if (new_syms == NULL) { 3676 goto give_up; 3677 } 3678 syms = new_syms; 3679 3680 qsort(syms, nsyms, sizeof(*syms), symcmp); 3681 3682 { 3683 struct syminfo *s = g_new(struct syminfo, 1); 3684 3685 s->disas_strtab = strings; 3686 s->disas_num_syms = nsyms; 3687 #if ELF_CLASS == ELFCLASS32 3688 s->disas_symtab.elf32 = syms; 3689 #else 3690 s->disas_symtab.elf64 = syms; 3691 #endif 3692 s->lookup_symbol = lookup_symbolxx; 3693 s->next = syminfos; 3694 syminfos = s; 3695 } 3696 return; 3697 3698 give_up: 3699 g_free(strings); 3700 g_free(syms); 3701 } 3702 3703 uint32_t get_elf_eflags(int fd) 3704 { 3705 struct elfhdr ehdr; 3706 off_t offset; 3707 int ret; 3708 3709 /* Read ELF header */ 3710 offset = lseek(fd, 0, SEEK_SET); 3711 if (offset == (off_t) -1) { 3712 return 0; 3713 } 3714 ret = read(fd, &ehdr, sizeof(ehdr)); 3715 if (ret < sizeof(ehdr)) { 3716 return 0; 3717 } 3718 offset = lseek(fd, offset, SEEK_SET); 3719 if (offset == (off_t) -1) { 3720 return 0; 3721 } 3722 3723 /* Check ELF signature */ 3724 if (!elf_check_ident(&ehdr)) { 3725 return 0; 3726 } 3727 3728 /* check header */ 3729 bswap_ehdr(&ehdr); 3730 if (!elf_check_ehdr(&ehdr)) { 3731 return 0; 3732 } 3733 3734 /* return architecture id */ 3735 return ehdr.e_flags; 3736 } 3737 3738 int load_elf_binary(struct linux_binprm *bprm, struct image_info *info) 3739 { 3740 /* 3741 * We need a copy of the elf header for passing to create_elf_tables. 3742 * We will have overwritten the original when we re-use bprm->buf 3743 * while loading the interpreter. Allocate the storage for this now 3744 * and let elf_load_image do any swapping that may be required. 3745 */ 3746 struct elfhdr ehdr; 3747 struct image_info interp_info, vdso_info; 3748 char *elf_interpreter = NULL; 3749 char *scratch; 3750 3751 memset(&interp_info, 0, sizeof(interp_info)); 3752 #ifdef TARGET_MIPS 3753 interp_info.fp_abi = MIPS_ABI_FP_UNKNOWN; 3754 #endif 3755 3756 load_elf_image(bprm->filename, &bprm->src, info, &ehdr, &elf_interpreter); 3757 3758 /* Do this so that we can load the interpreter, if need be. We will 3759 change some of these later */ 3760 bprm->p = setup_arg_pages(bprm, info); 3761 3762 scratch = g_new0(char, TARGET_PAGE_SIZE); 3763 if (STACK_GROWS_DOWN) { 3764 bprm->p = copy_elf_strings(1, &bprm->filename, scratch, 3765 bprm->p, info->stack_limit); 3766 info->file_string = bprm->p; 3767 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch, 3768 bprm->p, info->stack_limit); 3769 info->env_strings = bprm->p; 3770 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch, 3771 bprm->p, info->stack_limit); 3772 info->arg_strings = bprm->p; 3773 } else { 3774 info->arg_strings = bprm->p; 3775 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch, 3776 bprm->p, info->stack_limit); 3777 info->env_strings = bprm->p; 3778 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch, 3779 bprm->p, info->stack_limit); 3780 info->file_string = bprm->p; 3781 bprm->p = copy_elf_strings(1, &bprm->filename, scratch, 3782 bprm->p, info->stack_limit); 3783 } 3784 3785 g_free(scratch); 3786 3787 if (!bprm->p) { 3788 fprintf(stderr, "%s: %s\n", bprm->filename, strerror(E2BIG)); 3789 exit(-1); 3790 } 3791 3792 if (elf_interpreter) { 3793 load_elf_interp(elf_interpreter, &interp_info, bprm->buf); 3794 3795 /* 3796 * While unusual because of ELF_ET_DYN_BASE, if we are unlucky 3797 * with the mappings the interpreter can be loaded above but 3798 * near the main executable, which can leave very little room 3799 * for the heap. 3800 * If the current brk has less than 16MB, use the end of the 3801 * interpreter. 3802 */ 3803 if (interp_info.brk > info->brk && 3804 interp_info.load_bias - info->brk < 16 * MiB) { 3805 info->brk = interp_info.brk; 3806 } 3807 3808 /* If the program interpreter is one of these two, then assume 3809 an iBCS2 image. Otherwise assume a native linux image. */ 3810 3811 if (strcmp(elf_interpreter, "/usr/lib/libc.so.1") == 0 3812 || strcmp(elf_interpreter, "/usr/lib/ld.so.1") == 0) { 3813 info->personality = PER_SVR4; 3814 3815 /* Why this, you ask??? Well SVr4 maps page 0 as read-only, 3816 and some applications "depend" upon this behavior. Since 3817 we do not have the power to recompile these, we emulate 3818 the SVr4 behavior. Sigh. */ 3819 target_mmap(0, TARGET_PAGE_SIZE, PROT_READ | PROT_EXEC, 3820 MAP_FIXED_NOREPLACE | MAP_PRIVATE | MAP_ANONYMOUS, 3821 -1, 0); 3822 } 3823 #ifdef TARGET_MIPS 3824 info->interp_fp_abi = interp_info.fp_abi; 3825 #endif 3826 } 3827 3828 /* 3829 * Load a vdso if available, which will amongst other things contain the 3830 * signal trampolines. Otherwise, allocate a separate page for them. 3831 */ 3832 const VdsoImageInfo *vdso = vdso_image_info(); 3833 if (vdso) { 3834 load_elf_vdso(&vdso_info, vdso); 3835 info->vdso = vdso_info.load_bias; 3836 } else if (TARGET_ARCH_HAS_SIGTRAMP_PAGE) { 3837 abi_long tramp_page = target_mmap(0, TARGET_PAGE_SIZE, 3838 PROT_READ | PROT_WRITE, 3839 MAP_PRIVATE | MAP_ANON, -1, 0); 3840 if (tramp_page == -1) { 3841 return -errno; 3842 } 3843 3844 setup_sigtramp(tramp_page); 3845 target_mprotect(tramp_page, TARGET_PAGE_SIZE, PROT_READ | PROT_EXEC); 3846 } 3847 3848 bprm->p = create_elf_tables(bprm->p, bprm->argc, bprm->envc, &ehdr, info, 3849 elf_interpreter ? &interp_info : NULL, 3850 vdso ? &vdso_info : NULL); 3851 info->start_stack = bprm->p; 3852 3853 /* If we have an interpreter, set that as the program's entry point. 3854 Copy the load_bias as well, to help PPC64 interpret the entry 3855 point as a function descriptor. Do this after creating elf tables 3856 so that we copy the original program entry point into the AUXV. */ 3857 if (elf_interpreter) { 3858 info->load_bias = interp_info.load_bias; 3859 info->entry = interp_info.entry; 3860 g_free(elf_interpreter); 3861 } 3862 3863 #ifdef USE_ELF_CORE_DUMP 3864 bprm->core_dump = &elf_core_dump; 3865 #endif 3866 3867 return 0; 3868 } 3869 3870 #ifdef USE_ELF_CORE_DUMP 3871 #include "exec/translate-all.h" 3872 3873 /* 3874 * Definitions to generate Intel SVR4-like core files. 3875 * These mostly have the same names as the SVR4 types with "target_elf_" 3876 * tacked on the front to prevent clashes with linux definitions, 3877 * and the typedef forms have been avoided. This is mostly like 3878 * the SVR4 structure, but more Linuxy, with things that Linux does 3879 * not support and which gdb doesn't really use excluded. 3880 * 3881 * Fields we don't dump (their contents is zero) in linux-user qemu 3882 * are marked with XXX. 3883 * 3884 * Core dump code is copied from linux kernel (fs/binfmt_elf.c). 3885 * 3886 * Porting ELF coredump for target is (quite) simple process. First you 3887 * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for 3888 * the target resides): 3889 * 3890 * #define USE_ELF_CORE_DUMP 3891 * 3892 * Next you define type of register set used for dumping. ELF specification 3893 * says that it needs to be array of elf_greg_t that has size of ELF_NREG. 3894 * 3895 * typedef <target_regtype> target_elf_greg_t; 3896 * #define ELF_NREG <number of registers> 3897 * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG]; 3898 * 3899 * Last step is to implement target specific function that copies registers 3900 * from given cpu into just specified register set. Prototype is: 3901 * 3902 * static void elf_core_copy_regs(taret_elf_gregset_t *regs, 3903 * const CPUArchState *env); 3904 * 3905 * Parameters: 3906 * regs - copy register values into here (allocated and zeroed by caller) 3907 * env - copy registers from here 3908 * 3909 * Example for ARM target is provided in this file. 3910 */ 3911 3912 struct target_elf_siginfo { 3913 abi_int si_signo; /* signal number */ 3914 abi_int si_code; /* extra code */ 3915 abi_int si_errno; /* errno */ 3916 }; 3917 3918 struct target_elf_prstatus { 3919 struct target_elf_siginfo pr_info; /* Info associated with signal */ 3920 abi_short pr_cursig; /* Current signal */ 3921 abi_ulong pr_sigpend; /* XXX */ 3922 abi_ulong pr_sighold; /* XXX */ 3923 target_pid_t pr_pid; 3924 target_pid_t pr_ppid; 3925 target_pid_t pr_pgrp; 3926 target_pid_t pr_sid; 3927 struct target_timeval pr_utime; /* XXX User time */ 3928 struct target_timeval pr_stime; /* XXX System time */ 3929 struct target_timeval pr_cutime; /* XXX Cumulative user time */ 3930 struct target_timeval pr_cstime; /* XXX Cumulative system time */ 3931 target_elf_gregset_t pr_reg; /* GP registers */ 3932 abi_int pr_fpvalid; /* XXX */ 3933 }; 3934 3935 #define ELF_PRARGSZ (80) /* Number of chars for args */ 3936 3937 struct target_elf_prpsinfo { 3938 char pr_state; /* numeric process state */ 3939 char pr_sname; /* char for pr_state */ 3940 char pr_zomb; /* zombie */ 3941 char pr_nice; /* nice val */ 3942 abi_ulong pr_flag; /* flags */ 3943 target_uid_t pr_uid; 3944 target_gid_t pr_gid; 3945 target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid; 3946 /* Lots missing */ 3947 char pr_fname[16] QEMU_NONSTRING; /* filename of executable */ 3948 char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */ 3949 }; 3950 3951 #ifdef BSWAP_NEEDED 3952 static void bswap_prstatus(struct target_elf_prstatus *prstatus) 3953 { 3954 prstatus->pr_info.si_signo = tswap32(prstatus->pr_info.si_signo); 3955 prstatus->pr_info.si_code = tswap32(prstatus->pr_info.si_code); 3956 prstatus->pr_info.si_errno = tswap32(prstatus->pr_info.si_errno); 3957 prstatus->pr_cursig = tswap16(prstatus->pr_cursig); 3958 prstatus->pr_sigpend = tswapal(prstatus->pr_sigpend); 3959 prstatus->pr_sighold = tswapal(prstatus->pr_sighold); 3960 prstatus->pr_pid = tswap32(prstatus->pr_pid); 3961 prstatus->pr_ppid = tswap32(prstatus->pr_ppid); 3962 prstatus->pr_pgrp = tswap32(prstatus->pr_pgrp); 3963 prstatus->pr_sid = tswap32(prstatus->pr_sid); 3964 /* cpu times are not filled, so we skip them */ 3965 /* regs should be in correct format already */ 3966 prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid); 3967 } 3968 3969 static void bswap_psinfo(struct target_elf_prpsinfo *psinfo) 3970 { 3971 psinfo->pr_flag = tswapal(psinfo->pr_flag); 3972 psinfo->pr_uid = tswap16(psinfo->pr_uid); 3973 psinfo->pr_gid = tswap16(psinfo->pr_gid); 3974 psinfo->pr_pid = tswap32(psinfo->pr_pid); 3975 psinfo->pr_ppid = tswap32(psinfo->pr_ppid); 3976 psinfo->pr_pgrp = tswap32(psinfo->pr_pgrp); 3977 psinfo->pr_sid = tswap32(psinfo->pr_sid); 3978 } 3979 3980 static void bswap_note(struct elf_note *en) 3981 { 3982 bswap32s(&en->n_namesz); 3983 bswap32s(&en->n_descsz); 3984 bswap32s(&en->n_type); 3985 } 3986 #else 3987 static inline void bswap_prstatus(struct target_elf_prstatus *p) { } 3988 static inline void bswap_psinfo(struct target_elf_prpsinfo *p) {} 3989 static inline void bswap_note(struct elf_note *en) { } 3990 #endif /* BSWAP_NEEDED */ 3991 3992 /* 3993 * Calculate file (dump) size of given memory region. 3994 */ 3995 static size_t vma_dump_size(target_ulong start, target_ulong end, 3996 unsigned long flags) 3997 { 3998 /* The area must be readable. */ 3999 if (!(flags & PAGE_READ)) { 4000 return 0; 4001 } 4002 4003 /* 4004 * Usually we don't dump executable pages as they contain 4005 * non-writable code that debugger can read directly from 4006 * target library etc. If there is no elf header, we dump it. 4007 */ 4008 if (!(flags & PAGE_WRITE_ORG) && 4009 (flags & PAGE_EXEC) && 4010 memcmp(g2h_untagged(start), ELFMAG, SELFMAG) == 0) { 4011 return 0; 4012 } 4013 4014 return end - start; 4015 } 4016 4017 static size_t size_note(const char *name, size_t datasz) 4018 { 4019 size_t namesz = strlen(name) + 1; 4020 4021 namesz = ROUND_UP(namesz, 4); 4022 datasz = ROUND_UP(datasz, 4); 4023 4024 return sizeof(struct elf_note) + namesz + datasz; 4025 } 4026 4027 static void *fill_note(void **pptr, int type, const char *name, size_t datasz) 4028 { 4029 void *ptr = *pptr; 4030 struct elf_note *n = ptr; 4031 size_t namesz = strlen(name) + 1; 4032 4033 n->n_namesz = namesz; 4034 n->n_descsz = datasz; 4035 n->n_type = type; 4036 bswap_note(n); 4037 4038 ptr += sizeof(*n); 4039 memcpy(ptr, name, namesz); 4040 4041 namesz = ROUND_UP(namesz, 4); 4042 datasz = ROUND_UP(datasz, 4); 4043 4044 *pptr = ptr + namesz + datasz; 4045 return ptr + namesz; 4046 } 4047 4048 static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine, 4049 uint32_t flags) 4050 { 4051 memcpy(elf->e_ident, ELFMAG, SELFMAG); 4052 4053 elf->e_ident[EI_CLASS] = ELF_CLASS; 4054 elf->e_ident[EI_DATA] = ELF_DATA; 4055 elf->e_ident[EI_VERSION] = EV_CURRENT; 4056 elf->e_ident[EI_OSABI] = ELF_OSABI; 4057 4058 elf->e_type = ET_CORE; 4059 elf->e_machine = machine; 4060 elf->e_version = EV_CURRENT; 4061 elf->e_phoff = sizeof(struct elfhdr); 4062 elf->e_flags = flags; 4063 elf->e_ehsize = sizeof(struct elfhdr); 4064 elf->e_phentsize = sizeof(struct elf_phdr); 4065 elf->e_phnum = segs; 4066 4067 bswap_ehdr(elf); 4068 } 4069 4070 static void fill_elf_note_phdr(struct elf_phdr *phdr, size_t sz, off_t offset) 4071 { 4072 phdr->p_type = PT_NOTE; 4073 phdr->p_offset = offset; 4074 phdr->p_filesz = sz; 4075 4076 bswap_phdr(phdr, 1); 4077 } 4078 4079 static void fill_prstatus_note(void *data, const TaskState *ts, 4080 CPUState *cpu, int signr) 4081 { 4082 /* 4083 * Because note memory is only aligned to 4, and target_elf_prstatus 4084 * may well have higher alignment requirements, fill locally and 4085 * memcpy to the destination afterward. 4086 */ 4087 struct target_elf_prstatus prstatus = { 4088 .pr_info.si_signo = signr, 4089 .pr_cursig = signr, 4090 .pr_pid = ts->ts_tid, 4091 .pr_ppid = getppid(), 4092 .pr_pgrp = getpgrp(), 4093 .pr_sid = getsid(0), 4094 }; 4095 4096 elf_core_copy_regs(&prstatus.pr_reg, cpu_env(cpu)); 4097 bswap_prstatus(&prstatus); 4098 memcpy(data, &prstatus, sizeof(prstatus)); 4099 } 4100 4101 static void fill_prpsinfo_note(void *data, const TaskState *ts) 4102 { 4103 /* 4104 * Because note memory is only aligned to 4, and target_elf_prpsinfo 4105 * may well have higher alignment requirements, fill locally and 4106 * memcpy to the destination afterward. 4107 */ 4108 struct target_elf_prpsinfo psinfo = { 4109 .pr_pid = getpid(), 4110 .pr_ppid = getppid(), 4111 .pr_pgrp = getpgrp(), 4112 .pr_sid = getsid(0), 4113 .pr_uid = getuid(), 4114 .pr_gid = getgid(), 4115 }; 4116 char *base_filename; 4117 size_t len; 4118 4119 len = ts->info->env_strings - ts->info->arg_strings; 4120 len = MIN(len, ELF_PRARGSZ); 4121 memcpy(&psinfo.pr_psargs, g2h_untagged(ts->info->arg_strings), len); 4122 for (size_t i = 0; i < len; i++) { 4123 if (psinfo.pr_psargs[i] == 0) { 4124 psinfo.pr_psargs[i] = ' '; 4125 } 4126 } 4127 4128 base_filename = g_path_get_basename(ts->bprm->filename); 4129 /* 4130 * Using strncpy here is fine: at max-length, 4131 * this field is not NUL-terminated. 4132 */ 4133 strncpy(psinfo.pr_fname, base_filename, sizeof(psinfo.pr_fname)); 4134 g_free(base_filename); 4135 4136 bswap_psinfo(&psinfo); 4137 memcpy(data, &psinfo, sizeof(psinfo)); 4138 } 4139 4140 static void fill_auxv_note(void *data, const TaskState *ts) 4141 { 4142 memcpy(data, g2h_untagged(ts->info->saved_auxv), ts->info->auxv_len); 4143 } 4144 4145 /* 4146 * Constructs name of coredump file. We have following convention 4147 * for the name: 4148 * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core 4149 * 4150 * Returns the filename 4151 */ 4152 static char *core_dump_filename(const TaskState *ts) 4153 { 4154 g_autoptr(GDateTime) now = g_date_time_new_now_local(); 4155 g_autofree char *nowstr = g_date_time_format(now, "%Y%m%d-%H%M%S"); 4156 g_autofree char *base_filename = g_path_get_basename(ts->bprm->filename); 4157 4158 return g_strdup_printf("qemu_%s_%s_%d.core", 4159 base_filename, nowstr, (int)getpid()); 4160 } 4161 4162 static int dump_write(int fd, const void *ptr, size_t size) 4163 { 4164 const char *bufp = (const char *)ptr; 4165 ssize_t bytes_written, bytes_left; 4166 4167 bytes_written = 0; 4168 bytes_left = size; 4169 4170 /* 4171 * In normal conditions, single write(2) should do but 4172 * in case of socket etc. this mechanism is more portable. 4173 */ 4174 do { 4175 bytes_written = write(fd, bufp, bytes_left); 4176 if (bytes_written < 0) { 4177 if (errno == EINTR) 4178 continue; 4179 return (-1); 4180 } else if (bytes_written == 0) { /* eof */ 4181 return (-1); 4182 } 4183 bufp += bytes_written; 4184 bytes_left -= bytes_written; 4185 } while (bytes_left > 0); 4186 4187 return (0); 4188 } 4189 4190 static int wmr_page_unprotect_regions(void *opaque, target_ulong start, 4191 target_ulong end, unsigned long flags) 4192 { 4193 if ((flags & (PAGE_WRITE | PAGE_WRITE_ORG)) == PAGE_WRITE_ORG) { 4194 size_t step = MAX(TARGET_PAGE_SIZE, qemu_real_host_page_size()); 4195 4196 while (1) { 4197 page_unprotect(start, 0); 4198 if (end - start <= step) { 4199 break; 4200 } 4201 start += step; 4202 } 4203 } 4204 return 0; 4205 } 4206 4207 typedef struct { 4208 unsigned count; 4209 size_t size; 4210 } CountAndSizeRegions; 4211 4212 static int wmr_count_and_size_regions(void *opaque, target_ulong start, 4213 target_ulong end, unsigned long flags) 4214 { 4215 CountAndSizeRegions *css = opaque; 4216 4217 css->count++; 4218 css->size += vma_dump_size(start, end, flags); 4219 return 0; 4220 } 4221 4222 typedef struct { 4223 struct elf_phdr *phdr; 4224 off_t offset; 4225 } FillRegionPhdr; 4226 4227 static int wmr_fill_region_phdr(void *opaque, target_ulong start, 4228 target_ulong end, unsigned long flags) 4229 { 4230 FillRegionPhdr *d = opaque; 4231 struct elf_phdr *phdr = d->phdr; 4232 4233 phdr->p_type = PT_LOAD; 4234 phdr->p_vaddr = start; 4235 phdr->p_paddr = 0; 4236 phdr->p_filesz = vma_dump_size(start, end, flags); 4237 phdr->p_offset = d->offset; 4238 d->offset += phdr->p_filesz; 4239 phdr->p_memsz = end - start; 4240 phdr->p_flags = (flags & PAGE_READ ? PF_R : 0) 4241 | (flags & PAGE_WRITE_ORG ? PF_W : 0) 4242 | (flags & PAGE_EXEC ? PF_X : 0); 4243 phdr->p_align = ELF_EXEC_PAGESIZE; 4244 4245 bswap_phdr(phdr, 1); 4246 d->phdr = phdr + 1; 4247 return 0; 4248 } 4249 4250 static int wmr_write_region(void *opaque, target_ulong start, 4251 target_ulong end, unsigned long flags) 4252 { 4253 int fd = *(int *)opaque; 4254 size_t size = vma_dump_size(start, end, flags); 4255 4256 if (!size) { 4257 return 0; 4258 } 4259 return dump_write(fd, g2h_untagged(start), size); 4260 } 4261 4262 /* 4263 * Write out ELF coredump. 4264 * 4265 * See documentation of ELF object file format in: 4266 * http://www.caldera.com/developers/devspecs/gabi41.pdf 4267 * 4268 * Coredump format in linux is following: 4269 * 4270 * 0 +----------------------+ \ 4271 * | ELF header | ET_CORE | 4272 * +----------------------+ | 4273 * | ELF program headers | |--- headers 4274 * | - NOTE section | | 4275 * | - PT_LOAD sections | | 4276 * +----------------------+ / 4277 * | NOTEs: | 4278 * | - NT_PRSTATUS | 4279 * | - NT_PRSINFO | 4280 * | - NT_AUXV | 4281 * +----------------------+ <-- aligned to target page 4282 * | Process memory dump | 4283 * : : 4284 * . . 4285 * : : 4286 * | | 4287 * +----------------------+ 4288 * 4289 * NT_PRSTATUS -> struct elf_prstatus (per thread) 4290 * NT_PRSINFO -> struct elf_prpsinfo 4291 * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()). 4292 * 4293 * Format follows System V format as close as possible. Current 4294 * version limitations are as follows: 4295 * - no floating point registers are dumped 4296 * 4297 * Function returns 0 in case of success, negative errno otherwise. 4298 * 4299 * TODO: make this work also during runtime: it should be 4300 * possible to force coredump from running process and then 4301 * continue processing. For example qemu could set up SIGUSR2 4302 * handler (provided that target process haven't registered 4303 * handler for that) that does the dump when signal is received. 4304 */ 4305 static int elf_core_dump(int signr, const CPUArchState *env) 4306 { 4307 const CPUState *cpu = env_cpu((CPUArchState *)env); 4308 const TaskState *ts = (const TaskState *)get_task_state((CPUState *)cpu); 4309 struct rlimit dumpsize; 4310 CountAndSizeRegions css; 4311 off_t offset, note_offset, data_offset; 4312 size_t note_size; 4313 int cpus, ret; 4314 int fd = -1; 4315 CPUState *cpu_iter; 4316 4317 if (prctl(PR_GET_DUMPABLE) == 0) { 4318 return 0; 4319 } 4320 4321 if (getrlimit(RLIMIT_CORE, &dumpsize) < 0 || dumpsize.rlim_cur == 0) { 4322 return 0; 4323 } 4324 4325 cpu_list_lock(); 4326 mmap_lock(); 4327 4328 /* By unprotecting, we merge vmas that might be split. */ 4329 walk_memory_regions(NULL, wmr_page_unprotect_regions); 4330 4331 /* 4332 * Walk through target process memory mappings and 4333 * set up structure containing this information. 4334 */ 4335 memset(&css, 0, sizeof(css)); 4336 walk_memory_regions(&css, wmr_count_and_size_regions); 4337 4338 cpus = 0; 4339 CPU_FOREACH(cpu_iter) { 4340 cpus++; 4341 } 4342 4343 offset = sizeof(struct elfhdr); 4344 offset += (css.count + 1) * sizeof(struct elf_phdr); 4345 note_offset = offset; 4346 4347 offset += size_note("CORE", ts->info->auxv_len); 4348 offset += size_note("CORE", sizeof(struct target_elf_prpsinfo)); 4349 offset += size_note("CORE", sizeof(struct target_elf_prstatus)) * cpus; 4350 note_size = offset - note_offset; 4351 data_offset = ROUND_UP(offset, ELF_EXEC_PAGESIZE); 4352 4353 /* Do not dump if the corefile size exceeds the limit. */ 4354 if (dumpsize.rlim_cur != RLIM_INFINITY 4355 && dumpsize.rlim_cur < data_offset + css.size) { 4356 errno = 0; 4357 goto out; 4358 } 4359 4360 { 4361 g_autofree char *corefile = core_dump_filename(ts); 4362 fd = open(corefile, O_WRONLY | O_CREAT | O_TRUNC, 4363 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); 4364 } 4365 if (fd < 0) { 4366 goto out; 4367 } 4368 4369 /* 4370 * There is a fair amount of alignment padding within the notes 4371 * as well as preceeding the process memory. Allocate a zeroed 4372 * block to hold it all. Write all of the headers directly into 4373 * this buffer and then write it out as a block. 4374 */ 4375 { 4376 g_autofree void *header = g_malloc0(data_offset); 4377 FillRegionPhdr frp; 4378 void *hptr, *dptr; 4379 4380 /* Create elf file header. */ 4381 hptr = header; 4382 fill_elf_header(hptr, css.count + 1, ELF_MACHINE, 0); 4383 hptr += sizeof(struct elfhdr); 4384 4385 /* Create elf program headers. */ 4386 fill_elf_note_phdr(hptr, note_size, note_offset); 4387 hptr += sizeof(struct elf_phdr); 4388 4389 frp.phdr = hptr; 4390 frp.offset = data_offset; 4391 walk_memory_regions(&frp, wmr_fill_region_phdr); 4392 hptr = frp.phdr; 4393 4394 /* Create the notes. */ 4395 dptr = fill_note(&hptr, NT_AUXV, "CORE", ts->info->auxv_len); 4396 fill_auxv_note(dptr, ts); 4397 4398 dptr = fill_note(&hptr, NT_PRPSINFO, "CORE", 4399 sizeof(struct target_elf_prpsinfo)); 4400 fill_prpsinfo_note(dptr, ts); 4401 4402 CPU_FOREACH(cpu_iter) { 4403 dptr = fill_note(&hptr, NT_PRSTATUS, "CORE", 4404 sizeof(struct target_elf_prstatus)); 4405 fill_prstatus_note(dptr, ts, cpu_iter, 4406 cpu_iter == cpu ? signr : 0); 4407 } 4408 4409 if (dump_write(fd, header, data_offset) < 0) { 4410 goto out; 4411 } 4412 } 4413 4414 /* 4415 * Finally write process memory into the corefile as well. 4416 */ 4417 if (walk_memory_regions(&fd, wmr_write_region) < 0) { 4418 goto out; 4419 } 4420 errno = 0; 4421 4422 out: 4423 ret = -errno; 4424 mmap_unlock(); 4425 cpu_list_unlock(); 4426 if (fd >= 0) { 4427 close(fd); 4428 } 4429 return ret; 4430 } 4431 #endif /* USE_ELF_CORE_DUMP */ 4432 4433 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop) 4434 { 4435 init_thread(regs, infop); 4436 } 4437