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