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