1 /* This is the Linux kernel elf-loading code, ported into user space */
2 #include "qemu/osdep.h"
5 #include <sys/resource.h>
9 #include "user-internals.h"
10 #include "signal-common.h"
12 #include "user-mmap.h"
13 #include "disas/disas.h"
14 #include "qemu/bitops.h"
15 #include "qemu/path.h"
16 #include "qemu/queue.h"
17 #include "qemu/guest-random.h"
18 #include "qemu/units.h"
19 #include "qemu/selfmap.h"
20 #include "qemu/lockable.h"
21 #include "qapi/error.h"
22 #include "qemu/error-report.h"
23 #include "target_signal.h"
24 #include "accel/tcg/debuginfo.h"
36 #ifndef TARGET_ARCH_HAS_SIGTRAMP_PAGE
37 #define TARGET_ARCH_HAS_SIGTRAMP_PAGE 0
42 const uint32_t *relocs
;
45 unsigned sigreturn_ofs
;
46 unsigned rt_sigreturn_ofs
;
49 #define ELF_OSABI ELFOSABI_SYSV
51 /* from personality.h */
54 * Flags for bug emulation.
56 * These occupy the top three bytes.
59 ADDR_NO_RANDOMIZE
= 0x0040000, /* disable randomization of VA space */
60 FDPIC_FUNCPTRS
= 0x0080000, /* userspace function ptrs point to
61 descriptors (signal handling) */
62 MMAP_PAGE_ZERO
= 0x0100000,
63 ADDR_COMPAT_LAYOUT
= 0x0200000,
64 READ_IMPLIES_EXEC
= 0x0400000,
65 ADDR_LIMIT_32BIT
= 0x0800000,
66 SHORT_INODE
= 0x1000000,
67 WHOLE_SECONDS
= 0x2000000,
68 STICKY_TIMEOUTS
= 0x4000000,
69 ADDR_LIMIT_3GB
= 0x8000000,
75 * These go in the low byte. Avoid using the top bit, it will
76 * conflict with error returns.
80 PER_LINUX_32BIT
= 0x0000 | ADDR_LIMIT_32BIT
,
81 PER_LINUX_FDPIC
= 0x0000 | FDPIC_FUNCPTRS
,
82 PER_SVR4
= 0x0001 | STICKY_TIMEOUTS
| MMAP_PAGE_ZERO
,
83 PER_SVR3
= 0x0002 | STICKY_TIMEOUTS
| SHORT_INODE
,
84 PER_SCOSVR3
= 0x0003 | STICKY_TIMEOUTS
| WHOLE_SECONDS
| SHORT_INODE
,
85 PER_OSR5
= 0x0003 | STICKY_TIMEOUTS
| WHOLE_SECONDS
,
86 PER_WYSEV386
= 0x0004 | STICKY_TIMEOUTS
| SHORT_INODE
,
87 PER_ISCR4
= 0x0005 | STICKY_TIMEOUTS
,
89 PER_SUNOS
= 0x0006 | STICKY_TIMEOUTS
,
90 PER_XENIX
= 0x0007 | STICKY_TIMEOUTS
| SHORT_INODE
,
92 PER_LINUX32_3GB
= 0x0008 | ADDR_LIMIT_3GB
,
93 PER_IRIX32
= 0x0009 | STICKY_TIMEOUTS
,/* IRIX5 32-bit */
94 PER_IRIXN32
= 0x000a | STICKY_TIMEOUTS
,/* IRIX6 new 32-bit */
95 PER_IRIX64
= 0x000b | STICKY_TIMEOUTS
,/* IRIX6 64-bit */
97 PER_SOLARIS
= 0x000d | STICKY_TIMEOUTS
,
98 PER_UW7
= 0x000e | STICKY_TIMEOUTS
| MMAP_PAGE_ZERO
,
99 PER_OSF4
= 0x000f, /* OSF/1 v4 */
105 * Return the base personality without flags.
107 #define personality(pers) (pers & PER_MASK)
109 int info_is_fdpic(struct image_info
*info
)
111 return info
->personality
== PER_LINUX_FDPIC
;
114 /* this flag is uneffective under linux too, should be deleted */
115 #ifndef MAP_DENYWRITE
116 #define MAP_DENYWRITE 0
119 /* should probably go in elf.h */
124 #if TARGET_BIG_ENDIAN
125 #define ELF_DATA ELFDATA2MSB
127 #define ELF_DATA ELFDATA2LSB
130 #ifdef TARGET_ABI_MIPSN32
131 typedef abi_ullong target_elf_greg_t
;
132 #define tswapreg(ptr) tswap64(ptr)
134 typedef abi_ulong target_elf_greg_t
;
135 #define tswapreg(ptr) tswapal(ptr)
139 typedef abi_ushort target_uid_t
;
140 typedef abi_ushort target_gid_t
;
142 typedef abi_uint target_uid_t
;
143 typedef abi_uint target_gid_t
;
145 typedef abi_int target_pid_t
;
149 #define ELF_HWCAP get_elf_hwcap()
151 static uint32_t get_elf_hwcap(void)
153 X86CPU
*cpu
= X86_CPU(thread_cpu
);
155 return cpu
->env
.features
[FEAT_1_EDX
];
159 #define ELF_CLASS ELFCLASS64
160 #define ELF_ARCH EM_X86_64
162 #define ELF_PLATFORM "x86_64"
164 static inline void init_thread(struct target_pt_regs
*regs
, struct image_info
*infop
)
167 regs
->rsp
= infop
->start_stack
;
168 regs
->rip
= infop
->entry
;
172 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
175 * Note that ELF_NREG should be 29 as there should be place for
176 * TRAPNO and ERR "registers" as well but linux doesn't dump
179 * See linux kernel: arch/x86/include/asm/elf.h
181 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUX86State
*env
)
183 (*regs
)[0] = tswapreg(env
->regs
[15]);
184 (*regs
)[1] = tswapreg(env
->regs
[14]);
185 (*regs
)[2] = tswapreg(env
->regs
[13]);
186 (*regs
)[3] = tswapreg(env
->regs
[12]);
187 (*regs
)[4] = tswapreg(env
->regs
[R_EBP
]);
188 (*regs
)[5] = tswapreg(env
->regs
[R_EBX
]);
189 (*regs
)[6] = tswapreg(env
->regs
[11]);
190 (*regs
)[7] = tswapreg(env
->regs
[10]);
191 (*regs
)[8] = tswapreg(env
->regs
[9]);
192 (*regs
)[9] = tswapreg(env
->regs
[8]);
193 (*regs
)[10] = tswapreg(env
->regs
[R_EAX
]);
194 (*regs
)[11] = tswapreg(env
->regs
[R_ECX
]);
195 (*regs
)[12] = tswapreg(env
->regs
[R_EDX
]);
196 (*regs
)[13] = tswapreg(env
->regs
[R_ESI
]);
197 (*regs
)[14] = tswapreg(env
->regs
[R_EDI
]);
198 (*regs
)[15] = tswapreg(env
->regs
[R_EAX
]); /* XXX */
199 (*regs
)[16] = tswapreg(env
->eip
);
200 (*regs
)[17] = tswapreg(env
->segs
[R_CS
].selector
& 0xffff);
201 (*regs
)[18] = tswapreg(env
->eflags
);
202 (*regs
)[19] = tswapreg(env
->regs
[R_ESP
]);
203 (*regs
)[20] = tswapreg(env
->segs
[R_SS
].selector
& 0xffff);
204 (*regs
)[21] = tswapreg(env
->segs
[R_FS
].selector
& 0xffff);
205 (*regs
)[22] = tswapreg(env
->segs
[R_GS
].selector
& 0xffff);
206 (*regs
)[23] = tswapreg(env
->segs
[R_DS
].selector
& 0xffff);
207 (*regs
)[24] = tswapreg(env
->segs
[R_ES
].selector
& 0xffff);
208 (*regs
)[25] = tswapreg(env
->segs
[R_FS
].selector
& 0xffff);
209 (*regs
)[26] = tswapreg(env
->segs
[R_GS
].selector
& 0xffff);
212 #if ULONG_MAX > UINT32_MAX
213 #define INIT_GUEST_COMMPAGE
214 static bool init_guest_commpage(void)
217 * The vsyscall page is at a high negative address aka kernel space,
218 * which means that we cannot actually allocate it with target_mmap.
219 * We still should be able to use page_set_flags, unless the user
220 * has specified -R reserved_va, which would trigger an assert().
222 if (reserved_va
!= 0 &&
223 TARGET_VSYSCALL_PAGE
+ TARGET_PAGE_SIZE
- 1 > reserved_va
) {
224 error_report("Cannot allocate vsyscall page");
227 page_set_flags(TARGET_VSYSCALL_PAGE
,
228 TARGET_VSYSCALL_PAGE
| ~TARGET_PAGE_MASK
,
229 PAGE_EXEC
| PAGE_VALID
);
236 * This is used to ensure we don't load something for the wrong architecture.
238 #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
241 * These are used to set parameters in the core dumps.
243 #define ELF_CLASS ELFCLASS32
244 #define ELF_ARCH EM_386
246 #define ELF_PLATFORM get_elf_platform()
247 #define EXSTACK_DEFAULT true
249 static const char *get_elf_platform(void)
251 static char elf_platform
[] = "i386";
252 int family
= object_property_get_int(OBJECT(thread_cpu
), "family", NULL
);
257 elf_platform
[1] = '0' + family
;
262 static inline void init_thread(struct target_pt_regs
*regs
,
263 struct image_info
*infop
)
265 regs
->esp
= infop
->start_stack
;
266 regs
->eip
= infop
->entry
;
268 /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program
269 starts %edx contains a pointer to a function which might be
270 registered using `atexit'. This provides a mean for the
271 dynamic linker to call DT_FINI functions for shared libraries
272 that have been loaded before the code runs.
274 A value of 0 tells we have no such handler. */
279 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
282 * Note that ELF_NREG should be 19 as there should be place for
283 * TRAPNO and ERR "registers" as well but linux doesn't dump
286 * See linux kernel: arch/x86/include/asm/elf.h
288 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUX86State
*env
)
290 (*regs
)[0] = tswapreg(env
->regs
[R_EBX
]);
291 (*regs
)[1] = tswapreg(env
->regs
[R_ECX
]);
292 (*regs
)[2] = tswapreg(env
->regs
[R_EDX
]);
293 (*regs
)[3] = tswapreg(env
->regs
[R_ESI
]);
294 (*regs
)[4] = tswapreg(env
->regs
[R_EDI
]);
295 (*regs
)[5] = tswapreg(env
->regs
[R_EBP
]);
296 (*regs
)[6] = tswapreg(env
->regs
[R_EAX
]);
297 (*regs
)[7] = tswapreg(env
->segs
[R_DS
].selector
& 0xffff);
298 (*regs
)[8] = tswapreg(env
->segs
[R_ES
].selector
& 0xffff);
299 (*regs
)[9] = tswapreg(env
->segs
[R_FS
].selector
& 0xffff);
300 (*regs
)[10] = tswapreg(env
->segs
[R_GS
].selector
& 0xffff);
301 (*regs
)[11] = tswapreg(env
->regs
[R_EAX
]); /* XXX */
302 (*regs
)[12] = tswapreg(env
->eip
);
303 (*regs
)[13] = tswapreg(env
->segs
[R_CS
].selector
& 0xffff);
304 (*regs
)[14] = tswapreg(env
->eflags
);
305 (*regs
)[15] = tswapreg(env
->regs
[R_ESP
]);
306 (*regs
)[16] = tswapreg(env
->segs
[R_SS
].selector
& 0xffff);
310 * i386 is the only target which supplies AT_SYSINFO for the vdso.
311 * All others only supply AT_SYSINFO_EHDR.
313 #define DLINFO_ARCH_ITEMS (vdso_info != NULL)
314 #define ARCH_DLINFO \
317 NEW_AUX_ENT(AT_SYSINFO, vdso_info->entry); \
321 #endif /* TARGET_X86_64 */
323 #define VDSO_HEADER "vdso.c.inc"
325 #define USE_ELF_CORE_DUMP
326 #define ELF_EXEC_PAGESIZE 4096
328 #endif /* TARGET_I386 */
332 #ifndef TARGET_AARCH64
333 /* 32 bit ARM definitions */
335 #define ELF_ARCH EM_ARM
336 #define ELF_CLASS ELFCLASS32
337 #define EXSTACK_DEFAULT true
339 static inline void init_thread(struct target_pt_regs
*regs
,
340 struct image_info
*infop
)
342 abi_long stack
= infop
->start_stack
;
343 memset(regs
, 0, sizeof(*regs
));
345 regs
->uregs
[16] = ARM_CPU_MODE_USR
;
346 if (infop
->entry
& 1) {
347 regs
->uregs
[16] |= CPSR_T
;
349 regs
->uregs
[15] = infop
->entry
& 0xfffffffe;
350 regs
->uregs
[13] = infop
->start_stack
;
351 /* FIXME - what to for failure of get_user()? */
352 get_user_ual(regs
->uregs
[2], stack
+ 8); /* envp */
353 get_user_ual(regs
->uregs
[1], stack
+ 4); /* envp */
354 /* XXX: it seems that r0 is zeroed after ! */
356 /* For uClinux PIC binaries. */
357 /* XXX: Linux does this only on ARM with no MMU (do we care ?) */
358 regs
->uregs
[10] = infop
->start_data
;
360 /* Support ARM FDPIC. */
361 if (info_is_fdpic(infop
)) {
362 /* As described in the ABI document, r7 points to the loadmap info
363 * prepared by the kernel. If an interpreter is needed, r8 points
364 * to the interpreter loadmap and r9 points to the interpreter
365 * PT_DYNAMIC info. If no interpreter is needed, r8 is zero, and
366 * r9 points to the main program PT_DYNAMIC info.
368 regs
->uregs
[7] = infop
->loadmap_addr
;
369 if (infop
->interpreter_loadmap_addr
) {
370 /* Executable is dynamically loaded. */
371 regs
->uregs
[8] = infop
->interpreter_loadmap_addr
;
372 regs
->uregs
[9] = infop
->interpreter_pt_dynamic_addr
;
375 regs
->uregs
[9] = infop
->pt_dynamic_addr
;
381 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
383 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUARMState
*env
)
385 (*regs
)[0] = tswapreg(env
->regs
[0]);
386 (*regs
)[1] = tswapreg(env
->regs
[1]);
387 (*regs
)[2] = tswapreg(env
->regs
[2]);
388 (*regs
)[3] = tswapreg(env
->regs
[3]);
389 (*regs
)[4] = tswapreg(env
->regs
[4]);
390 (*regs
)[5] = tswapreg(env
->regs
[5]);
391 (*regs
)[6] = tswapreg(env
->regs
[6]);
392 (*regs
)[7] = tswapreg(env
->regs
[7]);
393 (*regs
)[8] = tswapreg(env
->regs
[8]);
394 (*regs
)[9] = tswapreg(env
->regs
[9]);
395 (*regs
)[10] = tswapreg(env
->regs
[10]);
396 (*regs
)[11] = tswapreg(env
->regs
[11]);
397 (*regs
)[12] = tswapreg(env
->regs
[12]);
398 (*regs
)[13] = tswapreg(env
->regs
[13]);
399 (*regs
)[14] = tswapreg(env
->regs
[14]);
400 (*regs
)[15] = tswapreg(env
->regs
[15]);
402 (*regs
)[16] = tswapreg(cpsr_read((CPUARMState
*)env
));
403 (*regs
)[17] = tswapreg(env
->regs
[0]); /* XXX */
406 #define USE_ELF_CORE_DUMP
407 #define ELF_EXEC_PAGESIZE 4096
411 ARM_HWCAP_ARM_SWP
= 1 << 0,
412 ARM_HWCAP_ARM_HALF
= 1 << 1,
413 ARM_HWCAP_ARM_THUMB
= 1 << 2,
414 ARM_HWCAP_ARM_26BIT
= 1 << 3,
415 ARM_HWCAP_ARM_FAST_MULT
= 1 << 4,
416 ARM_HWCAP_ARM_FPA
= 1 << 5,
417 ARM_HWCAP_ARM_VFP
= 1 << 6,
418 ARM_HWCAP_ARM_EDSP
= 1 << 7,
419 ARM_HWCAP_ARM_JAVA
= 1 << 8,
420 ARM_HWCAP_ARM_IWMMXT
= 1 << 9,
421 ARM_HWCAP_ARM_CRUNCH
= 1 << 10,
422 ARM_HWCAP_ARM_THUMBEE
= 1 << 11,
423 ARM_HWCAP_ARM_NEON
= 1 << 12,
424 ARM_HWCAP_ARM_VFPv3
= 1 << 13,
425 ARM_HWCAP_ARM_VFPv3D16
= 1 << 14,
426 ARM_HWCAP_ARM_TLS
= 1 << 15,
427 ARM_HWCAP_ARM_VFPv4
= 1 << 16,
428 ARM_HWCAP_ARM_IDIVA
= 1 << 17,
429 ARM_HWCAP_ARM_IDIVT
= 1 << 18,
430 ARM_HWCAP_ARM_VFPD32
= 1 << 19,
431 ARM_HWCAP_ARM_LPAE
= 1 << 20,
432 ARM_HWCAP_ARM_EVTSTRM
= 1 << 21,
433 ARM_HWCAP_ARM_FPHP
= 1 << 22,
434 ARM_HWCAP_ARM_ASIMDHP
= 1 << 23,
435 ARM_HWCAP_ARM_ASIMDDP
= 1 << 24,
436 ARM_HWCAP_ARM_ASIMDFHM
= 1 << 25,
437 ARM_HWCAP_ARM_ASIMDBF16
= 1 << 26,
438 ARM_HWCAP_ARM_I8MM
= 1 << 27,
442 ARM_HWCAP2_ARM_AES
= 1 << 0,
443 ARM_HWCAP2_ARM_PMULL
= 1 << 1,
444 ARM_HWCAP2_ARM_SHA1
= 1 << 2,
445 ARM_HWCAP2_ARM_SHA2
= 1 << 3,
446 ARM_HWCAP2_ARM_CRC32
= 1 << 4,
447 ARM_HWCAP2_ARM_SB
= 1 << 5,
448 ARM_HWCAP2_ARM_SSBS
= 1 << 6,
451 /* The commpage only exists for 32 bit kernels */
453 #define HI_COMMPAGE (intptr_t)0xffff0f00u
455 static bool init_guest_commpage(void)
457 ARMCPU
*cpu
= ARM_CPU(thread_cpu
);
463 * M-profile allocates maximum of 2GB address space, so can never
464 * allocate the commpage. Skip it.
466 if (arm_feature(&cpu
->env
, ARM_FEATURE_M
)) {
470 commpage
= HI_COMMPAGE
& -qemu_host_page_size
;
471 want
= g2h_untagged(commpage
);
472 addr
= mmap(want
, qemu_host_page_size
, PROT_READ
| PROT_WRITE
,
473 MAP_ANONYMOUS
| MAP_PRIVATE
| MAP_FIXED
, -1, 0);
475 if (addr
== MAP_FAILED
) {
476 perror("Allocating guest commpage");
483 /* Set kernel helper versions; rest of page is 0. */
484 __put_user(5, (uint32_t *)g2h_untagged(0xffff0ffcu
));
486 if (mprotect(addr
, qemu_host_page_size
, PROT_READ
)) {
487 perror("Protecting guest commpage");
491 page_set_flags(commpage
, commpage
| ~qemu_host_page_mask
,
492 PAGE_READ
| PAGE_EXEC
| PAGE_VALID
);
496 #define ELF_HWCAP get_elf_hwcap()
497 #define ELF_HWCAP2 get_elf_hwcap2()
499 uint32_t get_elf_hwcap(void)
501 ARMCPU
*cpu
= ARM_CPU(thread_cpu
);
504 hwcaps
|= ARM_HWCAP_ARM_SWP
;
505 hwcaps
|= ARM_HWCAP_ARM_HALF
;
506 hwcaps
|= ARM_HWCAP_ARM_THUMB
;
507 hwcaps
|= ARM_HWCAP_ARM_FAST_MULT
;
509 /* probe for the extra features */
510 #define GET_FEATURE(feat, hwcap) \
511 do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0)
513 #define GET_FEATURE_ID(feat, hwcap) \
514 do { if (cpu_isar_feature(feat, cpu)) { hwcaps |= hwcap; } } while (0)
516 /* EDSP is in v5TE and above, but all our v5 CPUs are v5TE */
517 GET_FEATURE(ARM_FEATURE_V5
, ARM_HWCAP_ARM_EDSP
);
518 GET_FEATURE(ARM_FEATURE_IWMMXT
, ARM_HWCAP_ARM_IWMMXT
);
519 GET_FEATURE(ARM_FEATURE_THUMB2EE
, ARM_HWCAP_ARM_THUMBEE
);
520 GET_FEATURE(ARM_FEATURE_NEON
, ARM_HWCAP_ARM_NEON
);
521 GET_FEATURE(ARM_FEATURE_V6K
, ARM_HWCAP_ARM_TLS
);
522 GET_FEATURE(ARM_FEATURE_LPAE
, ARM_HWCAP_ARM_LPAE
);
523 GET_FEATURE_ID(aa32_arm_div
, ARM_HWCAP_ARM_IDIVA
);
524 GET_FEATURE_ID(aa32_thumb_div
, ARM_HWCAP_ARM_IDIVT
);
525 GET_FEATURE_ID(aa32_vfp
, ARM_HWCAP_ARM_VFP
);
527 if (cpu_isar_feature(aa32_fpsp_v3
, cpu
) ||
528 cpu_isar_feature(aa32_fpdp_v3
, cpu
)) {
529 hwcaps
|= ARM_HWCAP_ARM_VFPv3
;
530 if (cpu_isar_feature(aa32_simd_r32
, cpu
)) {
531 hwcaps
|= ARM_HWCAP_ARM_VFPD32
;
533 hwcaps
|= ARM_HWCAP_ARM_VFPv3D16
;
536 GET_FEATURE_ID(aa32_simdfmac
, ARM_HWCAP_ARM_VFPv4
);
538 * MVFR1.FPHP and .SIMDHP must be in sync, and QEMU uses the same
539 * isar_feature function for both. The kernel reports them as two hwcaps.
541 GET_FEATURE_ID(aa32_fp16_arith
, ARM_HWCAP_ARM_FPHP
);
542 GET_FEATURE_ID(aa32_fp16_arith
, ARM_HWCAP_ARM_ASIMDHP
);
543 GET_FEATURE_ID(aa32_dp
, ARM_HWCAP_ARM_ASIMDDP
);
544 GET_FEATURE_ID(aa32_fhm
, ARM_HWCAP_ARM_ASIMDFHM
);
545 GET_FEATURE_ID(aa32_bf16
, ARM_HWCAP_ARM_ASIMDBF16
);
546 GET_FEATURE_ID(aa32_i8mm
, ARM_HWCAP_ARM_I8MM
);
551 uint32_t get_elf_hwcap2(void)
553 ARMCPU
*cpu
= ARM_CPU(thread_cpu
);
556 GET_FEATURE_ID(aa32_aes
, ARM_HWCAP2_ARM_AES
);
557 GET_FEATURE_ID(aa32_pmull
, ARM_HWCAP2_ARM_PMULL
);
558 GET_FEATURE_ID(aa32_sha1
, ARM_HWCAP2_ARM_SHA1
);
559 GET_FEATURE_ID(aa32_sha2
, ARM_HWCAP2_ARM_SHA2
);
560 GET_FEATURE_ID(aa32_crc32
, ARM_HWCAP2_ARM_CRC32
);
561 GET_FEATURE_ID(aa32_sb
, ARM_HWCAP2_ARM_SB
);
562 GET_FEATURE_ID(aa32_ssbs
, ARM_HWCAP2_ARM_SSBS
);
566 const char *elf_hwcap_str(uint32_t bit
)
568 static const char *hwcap_str
[] = {
569 [__builtin_ctz(ARM_HWCAP_ARM_SWP
)] = "swp",
570 [__builtin_ctz(ARM_HWCAP_ARM_HALF
)] = "half",
571 [__builtin_ctz(ARM_HWCAP_ARM_THUMB
)] = "thumb",
572 [__builtin_ctz(ARM_HWCAP_ARM_26BIT
)] = "26bit",
573 [__builtin_ctz(ARM_HWCAP_ARM_FAST_MULT
)] = "fast_mult",
574 [__builtin_ctz(ARM_HWCAP_ARM_FPA
)] = "fpa",
575 [__builtin_ctz(ARM_HWCAP_ARM_VFP
)] = "vfp",
576 [__builtin_ctz(ARM_HWCAP_ARM_EDSP
)] = "edsp",
577 [__builtin_ctz(ARM_HWCAP_ARM_JAVA
)] = "java",
578 [__builtin_ctz(ARM_HWCAP_ARM_IWMMXT
)] = "iwmmxt",
579 [__builtin_ctz(ARM_HWCAP_ARM_CRUNCH
)] = "crunch",
580 [__builtin_ctz(ARM_HWCAP_ARM_THUMBEE
)] = "thumbee",
581 [__builtin_ctz(ARM_HWCAP_ARM_NEON
)] = "neon",
582 [__builtin_ctz(ARM_HWCAP_ARM_VFPv3
)] = "vfpv3",
583 [__builtin_ctz(ARM_HWCAP_ARM_VFPv3D16
)] = "vfpv3d16",
584 [__builtin_ctz(ARM_HWCAP_ARM_TLS
)] = "tls",
585 [__builtin_ctz(ARM_HWCAP_ARM_VFPv4
)] = "vfpv4",
586 [__builtin_ctz(ARM_HWCAP_ARM_IDIVA
)] = "idiva",
587 [__builtin_ctz(ARM_HWCAP_ARM_IDIVT
)] = "idivt",
588 [__builtin_ctz(ARM_HWCAP_ARM_VFPD32
)] = "vfpd32",
589 [__builtin_ctz(ARM_HWCAP_ARM_LPAE
)] = "lpae",
590 [__builtin_ctz(ARM_HWCAP_ARM_EVTSTRM
)] = "evtstrm",
591 [__builtin_ctz(ARM_HWCAP_ARM_FPHP
)] = "fphp",
592 [__builtin_ctz(ARM_HWCAP_ARM_ASIMDHP
)] = "asimdhp",
593 [__builtin_ctz(ARM_HWCAP_ARM_ASIMDDP
)] = "asimddp",
594 [__builtin_ctz(ARM_HWCAP_ARM_ASIMDFHM
)] = "asimdfhm",
595 [__builtin_ctz(ARM_HWCAP_ARM_ASIMDBF16
)] = "asimdbf16",
596 [__builtin_ctz(ARM_HWCAP_ARM_I8MM
)] = "i8mm",
599 return bit
< ARRAY_SIZE(hwcap_str
) ? hwcap_str
[bit
] : NULL
;
602 const char *elf_hwcap2_str(uint32_t bit
)
604 static const char *hwcap_str
[] = {
605 [__builtin_ctz(ARM_HWCAP2_ARM_AES
)] = "aes",
606 [__builtin_ctz(ARM_HWCAP2_ARM_PMULL
)] = "pmull",
607 [__builtin_ctz(ARM_HWCAP2_ARM_SHA1
)] = "sha1",
608 [__builtin_ctz(ARM_HWCAP2_ARM_SHA2
)] = "sha2",
609 [__builtin_ctz(ARM_HWCAP2_ARM_CRC32
)] = "crc32",
610 [__builtin_ctz(ARM_HWCAP2_ARM_SB
)] = "sb",
611 [__builtin_ctz(ARM_HWCAP2_ARM_SSBS
)] = "ssbs",
614 return bit
< ARRAY_SIZE(hwcap_str
) ? hwcap_str
[bit
] : NULL
;
618 #undef GET_FEATURE_ID
620 #define ELF_PLATFORM get_elf_platform()
622 static const char *get_elf_platform(void)
624 CPUARMState
*env
= cpu_env(thread_cpu
);
626 #if TARGET_BIG_ENDIAN
632 if (arm_feature(env
, ARM_FEATURE_V8
)) {
634 } else if (arm_feature(env
, ARM_FEATURE_V7
)) {
635 if (arm_feature(env
, ARM_FEATURE_M
)) {
640 } else if (arm_feature(env
, ARM_FEATURE_V6
)) {
642 } else if (arm_feature(env
, ARM_FEATURE_V5
)) {
652 /* 64 bit ARM definitions */
654 #define ELF_ARCH EM_AARCH64
655 #define ELF_CLASS ELFCLASS64
656 #if TARGET_BIG_ENDIAN
657 # define ELF_PLATFORM "aarch64_be"
659 # define ELF_PLATFORM "aarch64"
662 static inline void init_thread(struct target_pt_regs
*regs
,
663 struct image_info
*infop
)
665 abi_long stack
= infop
->start_stack
;
666 memset(regs
, 0, sizeof(*regs
));
668 regs
->pc
= infop
->entry
& ~0x3ULL
;
673 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
675 static void elf_core_copy_regs(target_elf_gregset_t
*regs
,
676 const CPUARMState
*env
)
680 for (i
= 0; i
< 32; i
++) {
681 (*regs
)[i
] = tswapreg(env
->xregs
[i
]);
683 (*regs
)[32] = tswapreg(env
->pc
);
684 (*regs
)[33] = tswapreg(pstate_read((CPUARMState
*)env
));
687 #define USE_ELF_CORE_DUMP
688 #define ELF_EXEC_PAGESIZE 4096
691 ARM_HWCAP_A64_FP
= 1 << 0,
692 ARM_HWCAP_A64_ASIMD
= 1 << 1,
693 ARM_HWCAP_A64_EVTSTRM
= 1 << 2,
694 ARM_HWCAP_A64_AES
= 1 << 3,
695 ARM_HWCAP_A64_PMULL
= 1 << 4,
696 ARM_HWCAP_A64_SHA1
= 1 << 5,
697 ARM_HWCAP_A64_SHA2
= 1 << 6,
698 ARM_HWCAP_A64_CRC32
= 1 << 7,
699 ARM_HWCAP_A64_ATOMICS
= 1 << 8,
700 ARM_HWCAP_A64_FPHP
= 1 << 9,
701 ARM_HWCAP_A64_ASIMDHP
= 1 << 10,
702 ARM_HWCAP_A64_CPUID
= 1 << 11,
703 ARM_HWCAP_A64_ASIMDRDM
= 1 << 12,
704 ARM_HWCAP_A64_JSCVT
= 1 << 13,
705 ARM_HWCAP_A64_FCMA
= 1 << 14,
706 ARM_HWCAP_A64_LRCPC
= 1 << 15,
707 ARM_HWCAP_A64_DCPOP
= 1 << 16,
708 ARM_HWCAP_A64_SHA3
= 1 << 17,
709 ARM_HWCAP_A64_SM3
= 1 << 18,
710 ARM_HWCAP_A64_SM4
= 1 << 19,
711 ARM_HWCAP_A64_ASIMDDP
= 1 << 20,
712 ARM_HWCAP_A64_SHA512
= 1 << 21,
713 ARM_HWCAP_A64_SVE
= 1 << 22,
714 ARM_HWCAP_A64_ASIMDFHM
= 1 << 23,
715 ARM_HWCAP_A64_DIT
= 1 << 24,
716 ARM_HWCAP_A64_USCAT
= 1 << 25,
717 ARM_HWCAP_A64_ILRCPC
= 1 << 26,
718 ARM_HWCAP_A64_FLAGM
= 1 << 27,
719 ARM_HWCAP_A64_SSBS
= 1 << 28,
720 ARM_HWCAP_A64_SB
= 1 << 29,
721 ARM_HWCAP_A64_PACA
= 1 << 30,
722 ARM_HWCAP_A64_PACG
= 1UL << 31,
724 ARM_HWCAP2_A64_DCPODP
= 1 << 0,
725 ARM_HWCAP2_A64_SVE2
= 1 << 1,
726 ARM_HWCAP2_A64_SVEAES
= 1 << 2,
727 ARM_HWCAP2_A64_SVEPMULL
= 1 << 3,
728 ARM_HWCAP2_A64_SVEBITPERM
= 1 << 4,
729 ARM_HWCAP2_A64_SVESHA3
= 1 << 5,
730 ARM_HWCAP2_A64_SVESM4
= 1 << 6,
731 ARM_HWCAP2_A64_FLAGM2
= 1 << 7,
732 ARM_HWCAP2_A64_FRINT
= 1 << 8,
733 ARM_HWCAP2_A64_SVEI8MM
= 1 << 9,
734 ARM_HWCAP2_A64_SVEF32MM
= 1 << 10,
735 ARM_HWCAP2_A64_SVEF64MM
= 1 << 11,
736 ARM_HWCAP2_A64_SVEBF16
= 1 << 12,
737 ARM_HWCAP2_A64_I8MM
= 1 << 13,
738 ARM_HWCAP2_A64_BF16
= 1 << 14,
739 ARM_HWCAP2_A64_DGH
= 1 << 15,
740 ARM_HWCAP2_A64_RNG
= 1 << 16,
741 ARM_HWCAP2_A64_BTI
= 1 << 17,
742 ARM_HWCAP2_A64_MTE
= 1 << 18,
743 ARM_HWCAP2_A64_ECV
= 1 << 19,
744 ARM_HWCAP2_A64_AFP
= 1 << 20,
745 ARM_HWCAP2_A64_RPRES
= 1 << 21,
746 ARM_HWCAP2_A64_MTE3
= 1 << 22,
747 ARM_HWCAP2_A64_SME
= 1 << 23,
748 ARM_HWCAP2_A64_SME_I16I64
= 1 << 24,
749 ARM_HWCAP2_A64_SME_F64F64
= 1 << 25,
750 ARM_HWCAP2_A64_SME_I8I32
= 1 << 26,
751 ARM_HWCAP2_A64_SME_F16F32
= 1 << 27,
752 ARM_HWCAP2_A64_SME_B16F32
= 1 << 28,
753 ARM_HWCAP2_A64_SME_F32F32
= 1 << 29,
754 ARM_HWCAP2_A64_SME_FA64
= 1 << 30,
755 ARM_HWCAP2_A64_WFXT
= 1ULL << 31,
756 ARM_HWCAP2_A64_EBF16
= 1ULL << 32,
757 ARM_HWCAP2_A64_SVE_EBF16
= 1ULL << 33,
758 ARM_HWCAP2_A64_CSSC
= 1ULL << 34,
759 ARM_HWCAP2_A64_RPRFM
= 1ULL << 35,
760 ARM_HWCAP2_A64_SVE2P1
= 1ULL << 36,
761 ARM_HWCAP2_A64_SME2
= 1ULL << 37,
762 ARM_HWCAP2_A64_SME2P1
= 1ULL << 38,
763 ARM_HWCAP2_A64_SME_I16I32
= 1ULL << 39,
764 ARM_HWCAP2_A64_SME_BI32I32
= 1ULL << 40,
765 ARM_HWCAP2_A64_SME_B16B16
= 1ULL << 41,
766 ARM_HWCAP2_A64_SME_F16F16
= 1ULL << 42,
767 ARM_HWCAP2_A64_MOPS
= 1ULL << 43,
768 ARM_HWCAP2_A64_HBC
= 1ULL << 44,
771 #define ELF_HWCAP get_elf_hwcap()
772 #define ELF_HWCAP2 get_elf_hwcap2()
774 #define GET_FEATURE_ID(feat, hwcap) \
775 do { if (cpu_isar_feature(feat, cpu)) { hwcaps |= hwcap; } } while (0)
777 uint32_t get_elf_hwcap(void)
779 ARMCPU
*cpu
= ARM_CPU(thread_cpu
);
782 hwcaps
|= ARM_HWCAP_A64_FP
;
783 hwcaps
|= ARM_HWCAP_A64_ASIMD
;
784 hwcaps
|= ARM_HWCAP_A64_CPUID
;
786 /* probe for the extra features */
788 GET_FEATURE_ID(aa64_aes
, ARM_HWCAP_A64_AES
);
789 GET_FEATURE_ID(aa64_pmull
, ARM_HWCAP_A64_PMULL
);
790 GET_FEATURE_ID(aa64_sha1
, ARM_HWCAP_A64_SHA1
);
791 GET_FEATURE_ID(aa64_sha256
, ARM_HWCAP_A64_SHA2
);
792 GET_FEATURE_ID(aa64_sha512
, ARM_HWCAP_A64_SHA512
);
793 GET_FEATURE_ID(aa64_crc32
, ARM_HWCAP_A64_CRC32
);
794 GET_FEATURE_ID(aa64_sha3
, ARM_HWCAP_A64_SHA3
);
795 GET_FEATURE_ID(aa64_sm3
, ARM_HWCAP_A64_SM3
);
796 GET_FEATURE_ID(aa64_sm4
, ARM_HWCAP_A64_SM4
);
797 GET_FEATURE_ID(aa64_fp16
, ARM_HWCAP_A64_FPHP
| ARM_HWCAP_A64_ASIMDHP
);
798 GET_FEATURE_ID(aa64_atomics
, ARM_HWCAP_A64_ATOMICS
);
799 GET_FEATURE_ID(aa64_rdm
, ARM_HWCAP_A64_ASIMDRDM
);
800 GET_FEATURE_ID(aa64_dp
, ARM_HWCAP_A64_ASIMDDP
);
801 GET_FEATURE_ID(aa64_fcma
, ARM_HWCAP_A64_FCMA
);
802 GET_FEATURE_ID(aa64_sve
, ARM_HWCAP_A64_SVE
);
803 GET_FEATURE_ID(aa64_pauth
, ARM_HWCAP_A64_PACA
| ARM_HWCAP_A64_PACG
);
804 GET_FEATURE_ID(aa64_fhm
, ARM_HWCAP_A64_ASIMDFHM
);
805 GET_FEATURE_ID(aa64_jscvt
, ARM_HWCAP_A64_JSCVT
);
806 GET_FEATURE_ID(aa64_sb
, ARM_HWCAP_A64_SB
);
807 GET_FEATURE_ID(aa64_condm_4
, ARM_HWCAP_A64_FLAGM
);
808 GET_FEATURE_ID(aa64_dcpop
, ARM_HWCAP_A64_DCPOP
);
809 GET_FEATURE_ID(aa64_rcpc_8_3
, ARM_HWCAP_A64_LRCPC
);
810 GET_FEATURE_ID(aa64_rcpc_8_4
, ARM_HWCAP_A64_ILRCPC
);
815 uint32_t get_elf_hwcap2(void)
817 ARMCPU
*cpu
= ARM_CPU(thread_cpu
);
820 GET_FEATURE_ID(aa64_dcpodp
, ARM_HWCAP2_A64_DCPODP
);
821 GET_FEATURE_ID(aa64_sve2
, ARM_HWCAP2_A64_SVE2
);
822 GET_FEATURE_ID(aa64_sve2_aes
, ARM_HWCAP2_A64_SVEAES
);
823 GET_FEATURE_ID(aa64_sve2_pmull128
, ARM_HWCAP2_A64_SVEPMULL
);
824 GET_FEATURE_ID(aa64_sve2_bitperm
, ARM_HWCAP2_A64_SVEBITPERM
);
825 GET_FEATURE_ID(aa64_sve2_sha3
, ARM_HWCAP2_A64_SVESHA3
);
826 GET_FEATURE_ID(aa64_sve2_sm4
, ARM_HWCAP2_A64_SVESM4
);
827 GET_FEATURE_ID(aa64_condm_5
, ARM_HWCAP2_A64_FLAGM2
);
828 GET_FEATURE_ID(aa64_frint
, ARM_HWCAP2_A64_FRINT
);
829 GET_FEATURE_ID(aa64_sve_i8mm
, ARM_HWCAP2_A64_SVEI8MM
);
830 GET_FEATURE_ID(aa64_sve_f32mm
, ARM_HWCAP2_A64_SVEF32MM
);
831 GET_FEATURE_ID(aa64_sve_f64mm
, ARM_HWCAP2_A64_SVEF64MM
);
832 GET_FEATURE_ID(aa64_sve_bf16
, ARM_HWCAP2_A64_SVEBF16
);
833 GET_FEATURE_ID(aa64_i8mm
, ARM_HWCAP2_A64_I8MM
);
834 GET_FEATURE_ID(aa64_bf16
, ARM_HWCAP2_A64_BF16
);
835 GET_FEATURE_ID(aa64_rndr
, ARM_HWCAP2_A64_RNG
);
836 GET_FEATURE_ID(aa64_bti
, ARM_HWCAP2_A64_BTI
);
837 GET_FEATURE_ID(aa64_mte
, ARM_HWCAP2_A64_MTE
);
838 GET_FEATURE_ID(aa64_sme
, (ARM_HWCAP2_A64_SME
|
839 ARM_HWCAP2_A64_SME_F32F32
|
840 ARM_HWCAP2_A64_SME_B16F32
|
841 ARM_HWCAP2_A64_SME_F16F32
|
842 ARM_HWCAP2_A64_SME_I8I32
));
843 GET_FEATURE_ID(aa64_sme_f64f64
, ARM_HWCAP2_A64_SME_F64F64
);
844 GET_FEATURE_ID(aa64_sme_i16i64
, ARM_HWCAP2_A64_SME_I16I64
);
845 GET_FEATURE_ID(aa64_sme_fa64
, ARM_HWCAP2_A64_SME_FA64
);
846 GET_FEATURE_ID(aa64_hbc
, ARM_HWCAP2_A64_HBC
);
847 GET_FEATURE_ID(aa64_mops
, ARM_HWCAP2_A64_MOPS
);
852 const char *elf_hwcap_str(uint32_t bit
)
854 static const char *hwcap_str
[] = {
855 [__builtin_ctz(ARM_HWCAP_A64_FP
)] = "fp",
856 [__builtin_ctz(ARM_HWCAP_A64_ASIMD
)] = "asimd",
857 [__builtin_ctz(ARM_HWCAP_A64_EVTSTRM
)] = "evtstrm",
858 [__builtin_ctz(ARM_HWCAP_A64_AES
)] = "aes",
859 [__builtin_ctz(ARM_HWCAP_A64_PMULL
)] = "pmull",
860 [__builtin_ctz(ARM_HWCAP_A64_SHA1
)] = "sha1",
861 [__builtin_ctz(ARM_HWCAP_A64_SHA2
)] = "sha2",
862 [__builtin_ctz(ARM_HWCAP_A64_CRC32
)] = "crc32",
863 [__builtin_ctz(ARM_HWCAP_A64_ATOMICS
)] = "atomics",
864 [__builtin_ctz(ARM_HWCAP_A64_FPHP
)] = "fphp",
865 [__builtin_ctz(ARM_HWCAP_A64_ASIMDHP
)] = "asimdhp",
866 [__builtin_ctz(ARM_HWCAP_A64_CPUID
)] = "cpuid",
867 [__builtin_ctz(ARM_HWCAP_A64_ASIMDRDM
)] = "asimdrdm",
868 [__builtin_ctz(ARM_HWCAP_A64_JSCVT
)] = "jscvt",
869 [__builtin_ctz(ARM_HWCAP_A64_FCMA
)] = "fcma",
870 [__builtin_ctz(ARM_HWCAP_A64_LRCPC
)] = "lrcpc",
871 [__builtin_ctz(ARM_HWCAP_A64_DCPOP
)] = "dcpop",
872 [__builtin_ctz(ARM_HWCAP_A64_SHA3
)] = "sha3",
873 [__builtin_ctz(ARM_HWCAP_A64_SM3
)] = "sm3",
874 [__builtin_ctz(ARM_HWCAP_A64_SM4
)] = "sm4",
875 [__builtin_ctz(ARM_HWCAP_A64_ASIMDDP
)] = "asimddp",
876 [__builtin_ctz(ARM_HWCAP_A64_SHA512
)] = "sha512",
877 [__builtin_ctz(ARM_HWCAP_A64_SVE
)] = "sve",
878 [__builtin_ctz(ARM_HWCAP_A64_ASIMDFHM
)] = "asimdfhm",
879 [__builtin_ctz(ARM_HWCAP_A64_DIT
)] = "dit",
880 [__builtin_ctz(ARM_HWCAP_A64_USCAT
)] = "uscat",
881 [__builtin_ctz(ARM_HWCAP_A64_ILRCPC
)] = "ilrcpc",
882 [__builtin_ctz(ARM_HWCAP_A64_FLAGM
)] = "flagm",
883 [__builtin_ctz(ARM_HWCAP_A64_SSBS
)] = "ssbs",
884 [__builtin_ctz(ARM_HWCAP_A64_SB
)] = "sb",
885 [__builtin_ctz(ARM_HWCAP_A64_PACA
)] = "paca",
886 [__builtin_ctz(ARM_HWCAP_A64_PACG
)] = "pacg",
889 return bit
< ARRAY_SIZE(hwcap_str
) ? hwcap_str
[bit
] : NULL
;
892 const char *elf_hwcap2_str(uint32_t bit
)
894 static const char *hwcap_str
[] = {
895 [__builtin_ctz(ARM_HWCAP2_A64_DCPODP
)] = "dcpodp",
896 [__builtin_ctz(ARM_HWCAP2_A64_SVE2
)] = "sve2",
897 [__builtin_ctz(ARM_HWCAP2_A64_SVEAES
)] = "sveaes",
898 [__builtin_ctz(ARM_HWCAP2_A64_SVEPMULL
)] = "svepmull",
899 [__builtin_ctz(ARM_HWCAP2_A64_SVEBITPERM
)] = "svebitperm",
900 [__builtin_ctz(ARM_HWCAP2_A64_SVESHA3
)] = "svesha3",
901 [__builtin_ctz(ARM_HWCAP2_A64_SVESM4
)] = "svesm4",
902 [__builtin_ctz(ARM_HWCAP2_A64_FLAGM2
)] = "flagm2",
903 [__builtin_ctz(ARM_HWCAP2_A64_FRINT
)] = "frint",
904 [__builtin_ctz(ARM_HWCAP2_A64_SVEI8MM
)] = "svei8mm",
905 [__builtin_ctz(ARM_HWCAP2_A64_SVEF32MM
)] = "svef32mm",
906 [__builtin_ctz(ARM_HWCAP2_A64_SVEF64MM
)] = "svef64mm",
907 [__builtin_ctz(ARM_HWCAP2_A64_SVEBF16
)] = "svebf16",
908 [__builtin_ctz(ARM_HWCAP2_A64_I8MM
)] = "i8mm",
909 [__builtin_ctz(ARM_HWCAP2_A64_BF16
)] = "bf16",
910 [__builtin_ctz(ARM_HWCAP2_A64_DGH
)] = "dgh",
911 [__builtin_ctz(ARM_HWCAP2_A64_RNG
)] = "rng",
912 [__builtin_ctz(ARM_HWCAP2_A64_BTI
)] = "bti",
913 [__builtin_ctz(ARM_HWCAP2_A64_MTE
)] = "mte",
914 [__builtin_ctz(ARM_HWCAP2_A64_ECV
)] = "ecv",
915 [__builtin_ctz(ARM_HWCAP2_A64_AFP
)] = "afp",
916 [__builtin_ctz(ARM_HWCAP2_A64_RPRES
)] = "rpres",
917 [__builtin_ctz(ARM_HWCAP2_A64_MTE3
)] = "mte3",
918 [__builtin_ctz(ARM_HWCAP2_A64_SME
)] = "sme",
919 [__builtin_ctz(ARM_HWCAP2_A64_SME_I16I64
)] = "smei16i64",
920 [__builtin_ctz(ARM_HWCAP2_A64_SME_F64F64
)] = "smef64f64",
921 [__builtin_ctz(ARM_HWCAP2_A64_SME_I8I32
)] = "smei8i32",
922 [__builtin_ctz(ARM_HWCAP2_A64_SME_F16F32
)] = "smef16f32",
923 [__builtin_ctz(ARM_HWCAP2_A64_SME_B16F32
)] = "smeb16f32",
924 [__builtin_ctz(ARM_HWCAP2_A64_SME_F32F32
)] = "smef32f32",
925 [__builtin_ctz(ARM_HWCAP2_A64_SME_FA64
)] = "smefa64",
926 [__builtin_ctz(ARM_HWCAP2_A64_WFXT
)] = "wfxt",
927 [__builtin_ctzll(ARM_HWCAP2_A64_EBF16
)] = "ebf16",
928 [__builtin_ctzll(ARM_HWCAP2_A64_SVE_EBF16
)] = "sveebf16",
929 [__builtin_ctzll(ARM_HWCAP2_A64_CSSC
)] = "cssc",
930 [__builtin_ctzll(ARM_HWCAP2_A64_RPRFM
)] = "rprfm",
931 [__builtin_ctzll(ARM_HWCAP2_A64_SVE2P1
)] = "sve2p1",
932 [__builtin_ctzll(ARM_HWCAP2_A64_SME2
)] = "sme2",
933 [__builtin_ctzll(ARM_HWCAP2_A64_SME2P1
)] = "sme2p1",
934 [__builtin_ctzll(ARM_HWCAP2_A64_SME_I16I32
)] = "smei16i32",
935 [__builtin_ctzll(ARM_HWCAP2_A64_SME_BI32I32
)] = "smebi32i32",
936 [__builtin_ctzll(ARM_HWCAP2_A64_SME_B16B16
)] = "smeb16b16",
937 [__builtin_ctzll(ARM_HWCAP2_A64_SME_F16F16
)] = "smef16f16",
938 [__builtin_ctzll(ARM_HWCAP2_A64_MOPS
)] = "mops",
939 [__builtin_ctzll(ARM_HWCAP2_A64_HBC
)] = "hbc",
942 return bit
< ARRAY_SIZE(hwcap_str
) ? hwcap_str
[bit
] : NULL
;
945 #undef GET_FEATURE_ID
947 #endif /* not TARGET_AARCH64 */
949 #if TARGET_BIG_ENDIAN
950 # define VDSO_HEADER "vdso-be.c.inc"
952 # define VDSO_HEADER "vdso-le.c.inc"
955 #endif /* TARGET_ARM */
958 #ifdef TARGET_SPARC64
960 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
961 | HWCAP_SPARC_MULDIV | HWCAP_SPARC_V9)
963 #define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS )
965 #define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC )
968 #define ELF_CLASS ELFCLASS64
969 #define ELF_ARCH EM_SPARCV9
971 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
972 | HWCAP_SPARC_MULDIV)
973 #define ELF_CLASS ELFCLASS32
974 #define ELF_ARCH EM_SPARC
975 #endif /* TARGET_SPARC64 */
977 static inline void init_thread(struct target_pt_regs
*regs
,
978 struct image_info
*infop
)
980 /* Note that target_cpu_copy_regs does not read psr/tstate. */
981 regs
->pc
= infop
->entry
;
982 regs
->npc
= regs
->pc
+ 4;
984 regs
->u_regs
[14] = (infop
->start_stack
- 16 * sizeof(abi_ulong
)
985 - TARGET_STACK_BIAS
);
987 #endif /* TARGET_SPARC */
991 #define ELF_MACHINE PPC_ELF_MACHINE
993 #if defined(TARGET_PPC64)
995 #define elf_check_arch(x) ( (x) == EM_PPC64 )
997 #define ELF_CLASS ELFCLASS64
1001 #define ELF_CLASS ELFCLASS32
1002 #define EXSTACK_DEFAULT true
1006 #define ELF_ARCH EM_PPC
1008 /* Feature masks for the Aux Vector Hardware Capabilities (AT_HWCAP).
1009 See arch/powerpc/include/asm/cputable.h. */
1011 QEMU_PPC_FEATURE_32
= 0x80000000,
1012 QEMU_PPC_FEATURE_64
= 0x40000000,
1013 QEMU_PPC_FEATURE_601_INSTR
= 0x20000000,
1014 QEMU_PPC_FEATURE_HAS_ALTIVEC
= 0x10000000,
1015 QEMU_PPC_FEATURE_HAS_FPU
= 0x08000000,
1016 QEMU_PPC_FEATURE_HAS_MMU
= 0x04000000,
1017 QEMU_PPC_FEATURE_HAS_4xxMAC
= 0x02000000,
1018 QEMU_PPC_FEATURE_UNIFIED_CACHE
= 0x01000000,
1019 QEMU_PPC_FEATURE_HAS_SPE
= 0x00800000,
1020 QEMU_PPC_FEATURE_HAS_EFP_SINGLE
= 0x00400000,
1021 QEMU_PPC_FEATURE_HAS_EFP_DOUBLE
= 0x00200000,
1022 QEMU_PPC_FEATURE_NO_TB
= 0x00100000,
1023 QEMU_PPC_FEATURE_POWER4
= 0x00080000,
1024 QEMU_PPC_FEATURE_POWER5
= 0x00040000,
1025 QEMU_PPC_FEATURE_POWER5_PLUS
= 0x00020000,
1026 QEMU_PPC_FEATURE_CELL
= 0x00010000,
1027 QEMU_PPC_FEATURE_BOOKE
= 0x00008000,
1028 QEMU_PPC_FEATURE_SMT
= 0x00004000,
1029 QEMU_PPC_FEATURE_ICACHE_SNOOP
= 0x00002000,
1030 QEMU_PPC_FEATURE_ARCH_2_05
= 0x00001000,
1031 QEMU_PPC_FEATURE_PA6T
= 0x00000800,
1032 QEMU_PPC_FEATURE_HAS_DFP
= 0x00000400,
1033 QEMU_PPC_FEATURE_POWER6_EXT
= 0x00000200,
1034 QEMU_PPC_FEATURE_ARCH_2_06
= 0x00000100,
1035 QEMU_PPC_FEATURE_HAS_VSX
= 0x00000080,
1036 QEMU_PPC_FEATURE_PSERIES_PERFMON_COMPAT
= 0x00000040,
1038 QEMU_PPC_FEATURE_TRUE_LE
= 0x00000002,
1039 QEMU_PPC_FEATURE_PPC_LE
= 0x00000001,
1041 /* Feature definitions in AT_HWCAP2. */
1042 QEMU_PPC_FEATURE2_ARCH_2_07
= 0x80000000, /* ISA 2.07 */
1043 QEMU_PPC_FEATURE2_HAS_HTM
= 0x40000000, /* Hardware Transactional Memory */
1044 QEMU_PPC_FEATURE2_HAS_DSCR
= 0x20000000, /* Data Stream Control Register */
1045 QEMU_PPC_FEATURE2_HAS_EBB
= 0x10000000, /* Event Base Branching */
1046 QEMU_PPC_FEATURE2_HAS_ISEL
= 0x08000000, /* Integer Select */
1047 QEMU_PPC_FEATURE2_HAS_TAR
= 0x04000000, /* Target Address Register */
1048 QEMU_PPC_FEATURE2_VEC_CRYPTO
= 0x02000000,
1049 QEMU_PPC_FEATURE2_HTM_NOSC
= 0x01000000,
1050 QEMU_PPC_FEATURE2_ARCH_3_00
= 0x00800000, /* ISA 3.00 */
1051 QEMU_PPC_FEATURE2_HAS_IEEE128
= 0x00400000, /* VSX IEEE Bin Float 128-bit */
1052 QEMU_PPC_FEATURE2_DARN
= 0x00200000, /* darn random number insn */
1053 QEMU_PPC_FEATURE2_SCV
= 0x00100000, /* scv syscall */
1054 QEMU_PPC_FEATURE2_HTM_NO_SUSPEND
= 0x00080000, /* TM w/o suspended state */
1055 QEMU_PPC_FEATURE2_ARCH_3_1
= 0x00040000, /* ISA 3.1 */
1056 QEMU_PPC_FEATURE2_MMA
= 0x00020000, /* Matrix-Multiply Assist */
1059 #define ELF_HWCAP get_elf_hwcap()
1061 static uint32_t get_elf_hwcap(void)
1063 PowerPCCPU
*cpu
= POWERPC_CPU(thread_cpu
);
1064 uint32_t features
= 0;
1066 /* We don't have to be terribly complete here; the high points are
1067 Altivec/FP/SPE support. Anything else is just a bonus. */
1068 #define GET_FEATURE(flag, feature) \
1069 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
1070 #define GET_FEATURE2(flags, feature) \
1072 if ((cpu->env.insns_flags2 & flags) == flags) { \
1073 features |= feature; \
1076 GET_FEATURE(PPC_64B
, QEMU_PPC_FEATURE_64
);
1077 GET_FEATURE(PPC_FLOAT
, QEMU_PPC_FEATURE_HAS_FPU
);
1078 GET_FEATURE(PPC_ALTIVEC
, QEMU_PPC_FEATURE_HAS_ALTIVEC
);
1079 GET_FEATURE(PPC_SPE
, QEMU_PPC_FEATURE_HAS_SPE
);
1080 GET_FEATURE(PPC_SPE_SINGLE
, QEMU_PPC_FEATURE_HAS_EFP_SINGLE
);
1081 GET_FEATURE(PPC_SPE_DOUBLE
, QEMU_PPC_FEATURE_HAS_EFP_DOUBLE
);
1082 GET_FEATURE(PPC_BOOKE
, QEMU_PPC_FEATURE_BOOKE
);
1083 GET_FEATURE(PPC_405_MAC
, QEMU_PPC_FEATURE_HAS_4xxMAC
);
1084 GET_FEATURE2(PPC2_DFP
, QEMU_PPC_FEATURE_HAS_DFP
);
1085 GET_FEATURE2(PPC2_VSX
, QEMU_PPC_FEATURE_HAS_VSX
);
1086 GET_FEATURE2((PPC2_PERM_ISA206
| PPC2_DIVE_ISA206
| PPC2_ATOMIC_ISA206
|
1087 PPC2_FP_CVT_ISA206
| PPC2_FP_TST_ISA206
),
1088 QEMU_PPC_FEATURE_ARCH_2_06
);
1095 #define ELF_HWCAP2 get_elf_hwcap2()
1097 static uint32_t get_elf_hwcap2(void)
1099 PowerPCCPU
*cpu
= POWERPC_CPU(thread_cpu
);
1100 uint32_t features
= 0;
1102 #define GET_FEATURE(flag, feature) \
1103 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
1104 #define GET_FEATURE2(flag, feature) \
1105 do { if (cpu->env.insns_flags2 & flag) { features |= feature; } } while (0)
1107 GET_FEATURE(PPC_ISEL
, QEMU_PPC_FEATURE2_HAS_ISEL
);
1108 GET_FEATURE2(PPC2_BCTAR_ISA207
, QEMU_PPC_FEATURE2_HAS_TAR
);
1109 GET_FEATURE2((PPC2_BCTAR_ISA207
| PPC2_LSQ_ISA207
| PPC2_ALTIVEC_207
|
1110 PPC2_ISA207S
), QEMU_PPC_FEATURE2_ARCH_2_07
|
1111 QEMU_PPC_FEATURE2_VEC_CRYPTO
);
1112 GET_FEATURE2(PPC2_ISA300
, QEMU_PPC_FEATURE2_ARCH_3_00
|
1113 QEMU_PPC_FEATURE2_DARN
| QEMU_PPC_FEATURE2_HAS_IEEE128
);
1114 GET_FEATURE2(PPC2_ISA310
, QEMU_PPC_FEATURE2_ARCH_3_1
|
1115 QEMU_PPC_FEATURE2_MMA
);
1124 * The requirements here are:
1125 * - keep the final alignment of sp (sp & 0xf)
1126 * - make sure the 32-bit value at the first 16 byte aligned position of
1127 * AUXV is greater than 16 for glibc compatibility.
1128 * AT_IGNOREPPC is used for that.
1129 * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC,
1130 * even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
1132 #define DLINFO_ARCH_ITEMS 5
1133 #define ARCH_DLINFO \
1135 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu); \
1137 * Handle glibc compatibility: these magic entries must \
1138 * be at the lowest addresses in the final auxv. \
1140 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
1141 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
1142 NEW_AUX_ENT(AT_DCACHEBSIZE, cpu->env.dcache_line_size); \
1143 NEW_AUX_ENT(AT_ICACHEBSIZE, cpu->env.icache_line_size); \
1144 NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \
1147 static inline void init_thread(struct target_pt_regs
*_regs
, struct image_info
*infop
)
1149 _regs
->gpr
[1] = infop
->start_stack
;
1150 #if defined(TARGET_PPC64)
1151 if (get_ppc64_abi(infop
) < 2) {
1153 get_user_u64(val
, infop
->entry
+ 8);
1154 _regs
->gpr
[2] = val
+ infop
->load_bias
;
1155 get_user_u64(val
, infop
->entry
);
1156 infop
->entry
= val
+ infop
->load_bias
;
1158 _regs
->gpr
[12] = infop
->entry
; /* r12 set to global entry address */
1161 _regs
->nip
= infop
->entry
;
1164 /* See linux kernel: arch/powerpc/include/asm/elf.h. */
1166 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1168 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUPPCState
*env
)
1171 target_ulong ccr
= 0;
1173 for (i
= 0; i
< ARRAY_SIZE(env
->gpr
); i
++) {
1174 (*regs
)[i
] = tswapreg(env
->gpr
[i
]);
1177 (*regs
)[32] = tswapreg(env
->nip
);
1178 (*regs
)[33] = tswapreg(env
->msr
);
1179 (*regs
)[35] = tswapreg(env
->ctr
);
1180 (*regs
)[36] = tswapreg(env
->lr
);
1181 (*regs
)[37] = tswapreg(cpu_read_xer(env
));
1183 ccr
= ppc_get_cr(env
);
1184 (*regs
)[38] = tswapreg(ccr
);
1187 #define USE_ELF_CORE_DUMP
1188 #define ELF_EXEC_PAGESIZE 4096
1192 #ifdef TARGET_LOONGARCH64
1194 #define ELF_CLASS ELFCLASS64
1195 #define ELF_ARCH EM_LOONGARCH
1196 #define EXSTACK_DEFAULT true
1198 #define elf_check_arch(x) ((x) == EM_LOONGARCH)
1200 #define VDSO_HEADER "vdso.c.inc"
1202 static inline void init_thread(struct target_pt_regs
*regs
,
1203 struct image_info
*infop
)
1205 /*Set crmd PG,DA = 1,0 */
1206 regs
->csr
.crmd
= 2 << 3;
1207 regs
->csr
.era
= infop
->entry
;
1208 regs
->regs
[3] = infop
->start_stack
;
1211 /* See linux kernel: arch/loongarch/include/asm/elf.h */
1213 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1217 TARGET_EF_CSR_ERA
= TARGET_EF_R0
+ 33,
1218 TARGET_EF_CSR_BADV
= TARGET_EF_R0
+ 34,
1221 static void elf_core_copy_regs(target_elf_gregset_t
*regs
,
1222 const CPULoongArchState
*env
)
1226 (*regs
)[TARGET_EF_R0
] = 0;
1228 for (i
= 1; i
< ARRAY_SIZE(env
->gpr
); i
++) {
1229 (*regs
)[TARGET_EF_R0
+ i
] = tswapreg(env
->gpr
[i
]);
1232 (*regs
)[TARGET_EF_CSR_ERA
] = tswapreg(env
->pc
);
1233 (*regs
)[TARGET_EF_CSR_BADV
] = tswapreg(env
->CSR_BADV
);
1236 #define USE_ELF_CORE_DUMP
1237 #define ELF_EXEC_PAGESIZE 4096
1239 #define ELF_HWCAP get_elf_hwcap()
1241 /* See arch/loongarch/include/uapi/asm/hwcap.h */
1243 HWCAP_LOONGARCH_CPUCFG
= (1 << 0),
1244 HWCAP_LOONGARCH_LAM
= (1 << 1),
1245 HWCAP_LOONGARCH_UAL
= (1 << 2),
1246 HWCAP_LOONGARCH_FPU
= (1 << 3),
1247 HWCAP_LOONGARCH_LSX
= (1 << 4),
1248 HWCAP_LOONGARCH_LASX
= (1 << 5),
1249 HWCAP_LOONGARCH_CRC32
= (1 << 6),
1250 HWCAP_LOONGARCH_COMPLEX
= (1 << 7),
1251 HWCAP_LOONGARCH_CRYPTO
= (1 << 8),
1252 HWCAP_LOONGARCH_LVZ
= (1 << 9),
1253 HWCAP_LOONGARCH_LBT_X86
= (1 << 10),
1254 HWCAP_LOONGARCH_LBT_ARM
= (1 << 11),
1255 HWCAP_LOONGARCH_LBT_MIPS
= (1 << 12),
1258 static uint32_t get_elf_hwcap(void)
1260 LoongArchCPU
*cpu
= LOONGARCH_CPU(thread_cpu
);
1261 uint32_t hwcaps
= 0;
1263 hwcaps
|= HWCAP_LOONGARCH_CRC32
;
1265 if (FIELD_EX32(cpu
->env
.cpucfg
[1], CPUCFG1
, UAL
)) {
1266 hwcaps
|= HWCAP_LOONGARCH_UAL
;
1269 if (FIELD_EX32(cpu
->env
.cpucfg
[2], CPUCFG2
, FP
)) {
1270 hwcaps
|= HWCAP_LOONGARCH_FPU
;
1273 if (FIELD_EX32(cpu
->env
.cpucfg
[2], CPUCFG2
, LAM
)) {
1274 hwcaps
|= HWCAP_LOONGARCH_LAM
;
1277 if (FIELD_EX32(cpu
->env
.cpucfg
[2], CPUCFG2
, LSX
)) {
1278 hwcaps
|= HWCAP_LOONGARCH_LSX
;
1281 if (FIELD_EX32(cpu
->env
.cpucfg
[2], CPUCFG2
, LASX
)) {
1282 hwcaps
|= HWCAP_LOONGARCH_LASX
;
1288 #define ELF_PLATFORM "loongarch"
1290 #endif /* TARGET_LOONGARCH64 */
1294 #ifdef TARGET_MIPS64
1295 #define ELF_CLASS ELFCLASS64
1297 #define ELF_CLASS ELFCLASS32
1299 #define ELF_ARCH EM_MIPS
1300 #define EXSTACK_DEFAULT true
1302 #ifdef TARGET_ABI_MIPSN32
1303 #define elf_check_abi(x) ((x) & EF_MIPS_ABI2)
1305 #define elf_check_abi(x) (!((x) & EF_MIPS_ABI2))
1308 #define ELF_BASE_PLATFORM get_elf_base_platform()
1310 #define MATCH_PLATFORM_INSN(_flags, _base_platform) \
1311 do { if ((cpu->env.insn_flags & (_flags)) == _flags) \
1312 { return _base_platform; } } while (0)
1314 static const char *get_elf_base_platform(void)
1316 MIPSCPU
*cpu
= MIPS_CPU(thread_cpu
);
1318 /* 64 bit ISAs goes first */
1319 MATCH_PLATFORM_INSN(CPU_MIPS64R6
, "mips64r6");
1320 MATCH_PLATFORM_INSN(CPU_MIPS64R5
, "mips64r5");
1321 MATCH_PLATFORM_INSN(CPU_MIPS64R2
, "mips64r2");
1322 MATCH_PLATFORM_INSN(CPU_MIPS64R1
, "mips64");
1323 MATCH_PLATFORM_INSN(CPU_MIPS5
, "mips5");
1324 MATCH_PLATFORM_INSN(CPU_MIPS4
, "mips4");
1325 MATCH_PLATFORM_INSN(CPU_MIPS3
, "mips3");
1328 MATCH_PLATFORM_INSN(CPU_MIPS32R6
, "mips32r6");
1329 MATCH_PLATFORM_INSN(CPU_MIPS32R5
, "mips32r5");
1330 MATCH_PLATFORM_INSN(CPU_MIPS32R2
, "mips32r2");
1331 MATCH_PLATFORM_INSN(CPU_MIPS32R1
, "mips32");
1332 MATCH_PLATFORM_INSN(CPU_MIPS2
, "mips2");
1337 #undef MATCH_PLATFORM_INSN
1339 static inline void init_thread(struct target_pt_regs
*regs
,
1340 struct image_info
*infop
)
1342 regs
->cp0_status
= 2 << CP0St_KSU
;
1343 regs
->cp0_epc
= infop
->entry
;
1344 regs
->regs
[29] = infop
->start_stack
;
1347 /* See linux kernel: arch/mips/include/asm/elf.h. */
1349 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1351 /* See linux kernel: arch/mips/include/asm/reg.h. */
1353 #ifdef TARGET_MIPS64
1358 TARGET_EF_R26
= TARGET_EF_R0
+ 26,
1359 TARGET_EF_R27
= TARGET_EF_R0
+ 27,
1360 TARGET_EF_LO
= TARGET_EF_R0
+ 32,
1361 TARGET_EF_HI
= TARGET_EF_R0
+ 33,
1362 TARGET_EF_CP0_EPC
= TARGET_EF_R0
+ 34,
1363 TARGET_EF_CP0_BADVADDR
= TARGET_EF_R0
+ 35,
1364 TARGET_EF_CP0_STATUS
= TARGET_EF_R0
+ 36,
1365 TARGET_EF_CP0_CAUSE
= TARGET_EF_R0
+ 37
1368 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1369 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUMIPSState
*env
)
1373 for (i
= 0; i
< TARGET_EF_R0
; i
++) {
1376 (*regs
)[TARGET_EF_R0
] = 0;
1378 for (i
= 1; i
< ARRAY_SIZE(env
->active_tc
.gpr
); i
++) {
1379 (*regs
)[TARGET_EF_R0
+ i
] = tswapreg(env
->active_tc
.gpr
[i
]);
1382 (*regs
)[TARGET_EF_R26
] = 0;
1383 (*regs
)[TARGET_EF_R27
] = 0;
1384 (*regs
)[TARGET_EF_LO
] = tswapreg(env
->active_tc
.LO
[0]);
1385 (*regs
)[TARGET_EF_HI
] = tswapreg(env
->active_tc
.HI
[0]);
1386 (*regs
)[TARGET_EF_CP0_EPC
] = tswapreg(env
->active_tc
.PC
);
1387 (*regs
)[TARGET_EF_CP0_BADVADDR
] = tswapreg(env
->CP0_BadVAddr
);
1388 (*regs
)[TARGET_EF_CP0_STATUS
] = tswapreg(env
->CP0_Status
);
1389 (*regs
)[TARGET_EF_CP0_CAUSE
] = tswapreg(env
->CP0_Cause
);
1392 #define USE_ELF_CORE_DUMP
1393 #define ELF_EXEC_PAGESIZE 4096
1395 /* See arch/mips/include/uapi/asm/hwcap.h. */
1397 HWCAP_MIPS_R6
= (1 << 0),
1398 HWCAP_MIPS_MSA
= (1 << 1),
1399 HWCAP_MIPS_CRC32
= (1 << 2),
1400 HWCAP_MIPS_MIPS16
= (1 << 3),
1401 HWCAP_MIPS_MDMX
= (1 << 4),
1402 HWCAP_MIPS_MIPS3D
= (1 << 5),
1403 HWCAP_MIPS_SMARTMIPS
= (1 << 6),
1404 HWCAP_MIPS_DSP
= (1 << 7),
1405 HWCAP_MIPS_DSP2
= (1 << 8),
1406 HWCAP_MIPS_DSP3
= (1 << 9),
1407 HWCAP_MIPS_MIPS16E2
= (1 << 10),
1408 HWCAP_LOONGSON_MMI
= (1 << 11),
1409 HWCAP_LOONGSON_EXT
= (1 << 12),
1410 HWCAP_LOONGSON_EXT2
= (1 << 13),
1411 HWCAP_LOONGSON_CPUCFG
= (1 << 14),
1414 #define ELF_HWCAP get_elf_hwcap()
1416 #define GET_FEATURE_INSN(_flag, _hwcap) \
1417 do { if (cpu->env.insn_flags & (_flag)) { hwcaps |= _hwcap; } } while (0)
1419 #define GET_FEATURE_REG_SET(_reg, _mask, _hwcap) \
1420 do { if (cpu->env._reg & (_mask)) { hwcaps |= _hwcap; } } while (0)
1422 #define GET_FEATURE_REG_EQU(_reg, _start, _length, _val, _hwcap) \
1424 if (extract32(cpu->env._reg, (_start), (_length)) == (_val)) { \
1429 static uint32_t get_elf_hwcap(void)
1431 MIPSCPU
*cpu
= MIPS_CPU(thread_cpu
);
1432 uint32_t hwcaps
= 0;
1434 GET_FEATURE_REG_EQU(CP0_Config0
, CP0C0_AR
, CP0C0_AR_LENGTH
,
1436 GET_FEATURE_REG_SET(CP0_Config3
, 1 << CP0C3_MSAP
, HWCAP_MIPS_MSA
);
1437 GET_FEATURE_INSN(ASE_LMMI
, HWCAP_LOONGSON_MMI
);
1438 GET_FEATURE_INSN(ASE_LEXT
, HWCAP_LOONGSON_EXT
);
1443 #undef GET_FEATURE_REG_EQU
1444 #undef GET_FEATURE_REG_SET
1445 #undef GET_FEATURE_INSN
1447 #endif /* TARGET_MIPS */
1449 #ifdef TARGET_MICROBLAZE
1451 #define elf_check_arch(x) ( (x) == EM_MICROBLAZE || (x) == EM_MICROBLAZE_OLD)
1453 #define ELF_CLASS ELFCLASS32
1454 #define ELF_ARCH EM_MICROBLAZE
1456 static inline void init_thread(struct target_pt_regs
*regs
,
1457 struct image_info
*infop
)
1459 regs
->pc
= infop
->entry
;
1460 regs
->r1
= infop
->start_stack
;
1464 #define ELF_EXEC_PAGESIZE 4096
1466 #define USE_ELF_CORE_DUMP
1468 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1470 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1471 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUMBState
*env
)
1475 for (i
= 0; i
< 32; i
++) {
1476 (*regs
)[pos
++] = tswapreg(env
->regs
[i
]);
1479 (*regs
)[pos
++] = tswapreg(env
->pc
);
1480 (*regs
)[pos
++] = tswapreg(mb_cpu_read_msr(env
));
1482 (*regs
)[pos
++] = tswapreg(env
->ear
);
1484 (*regs
)[pos
++] = tswapreg(env
->esr
);
1487 #endif /* TARGET_MICROBLAZE */
1491 #define elf_check_arch(x) ((x) == EM_ALTERA_NIOS2)
1493 #define ELF_CLASS ELFCLASS32
1494 #define ELF_ARCH EM_ALTERA_NIOS2
1496 static void init_thread(struct target_pt_regs
*regs
, struct image_info
*infop
)
1498 regs
->ea
= infop
->entry
;
1499 regs
->sp
= infop
->start_stack
;
1502 #define LO_COMMPAGE TARGET_PAGE_SIZE
1504 static bool init_guest_commpage(void)
1506 static const uint8_t kuser_page
[4 + 2 * 64] = {
1507 /* __kuser_helper_version */
1508 [0x00] = 0x02, 0x00, 0x00, 0x00,
1510 /* __kuser_cmpxchg */
1511 [0x04] = 0x3a, 0x6c, 0x3b, 0x00, /* trap 16 */
1512 0x3a, 0x28, 0x00, 0xf8, /* ret */
1514 /* __kuser_sigtramp */
1515 [0x44] = 0xc4, 0x22, 0x80, 0x00, /* movi r2, __NR_rt_sigreturn */
1516 0x3a, 0x68, 0x3b, 0x00, /* trap 0 */
1519 void *want
= g2h_untagged(LO_COMMPAGE
& -qemu_host_page_size
);
1520 void *addr
= mmap(want
, qemu_host_page_size
, PROT_READ
| PROT_WRITE
,
1521 MAP_ANONYMOUS
| MAP_PRIVATE
| MAP_FIXED
, -1, 0);
1523 if (addr
== MAP_FAILED
) {
1524 perror("Allocating guest commpage");
1531 memcpy(addr
, kuser_page
, sizeof(kuser_page
));
1533 if (mprotect(addr
, qemu_host_page_size
, PROT_READ
)) {
1534 perror("Protecting guest commpage");
1538 page_set_flags(LO_COMMPAGE
, LO_COMMPAGE
| ~TARGET_PAGE_MASK
,
1539 PAGE_READ
| PAGE_EXEC
| PAGE_VALID
);
1543 #define ELF_EXEC_PAGESIZE 4096
1545 #define USE_ELF_CORE_DUMP
1547 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1549 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1550 static void elf_core_copy_regs(target_elf_gregset_t
*regs
,
1551 const CPUNios2State
*env
)
1556 for (i
= 1; i
< 8; i
++) /* r0-r7 */
1557 (*regs
)[i
] = tswapreg(env
->regs
[i
+ 7]);
1559 for (i
= 8; i
< 16; i
++) /* r8-r15 */
1560 (*regs
)[i
] = tswapreg(env
->regs
[i
- 8]);
1562 for (i
= 16; i
< 24; i
++) /* r16-r23 */
1563 (*regs
)[i
] = tswapreg(env
->regs
[i
+ 7]);
1564 (*regs
)[24] = -1; /* R_ET */
1565 (*regs
)[25] = -1; /* R_BT */
1566 (*regs
)[26] = tswapreg(env
->regs
[R_GP
]);
1567 (*regs
)[27] = tswapreg(env
->regs
[R_SP
]);
1568 (*regs
)[28] = tswapreg(env
->regs
[R_FP
]);
1569 (*regs
)[29] = tswapreg(env
->regs
[R_EA
]);
1570 (*regs
)[30] = -1; /* R_SSTATUS */
1571 (*regs
)[31] = tswapreg(env
->regs
[R_RA
]);
1573 (*regs
)[32] = tswapreg(env
->pc
);
1575 (*regs
)[33] = -1; /* R_STATUS */
1576 (*regs
)[34] = tswapreg(env
->regs
[CR_ESTATUS
]);
1578 for (i
= 35; i
< 49; i
++) /* ... */
1582 #endif /* TARGET_NIOS2 */
1584 #ifdef TARGET_OPENRISC
1586 #define ELF_ARCH EM_OPENRISC
1587 #define ELF_CLASS ELFCLASS32
1588 #define ELF_DATA ELFDATA2MSB
1590 static inline void init_thread(struct target_pt_regs
*regs
,
1591 struct image_info
*infop
)
1593 regs
->pc
= infop
->entry
;
1594 regs
->gpr
[1] = infop
->start_stack
;
1597 #define USE_ELF_CORE_DUMP
1598 #define ELF_EXEC_PAGESIZE 8192
1600 /* See linux kernel arch/openrisc/include/asm/elf.h. */
1601 #define ELF_NREG 34 /* gprs and pc, sr */
1602 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1604 static void elf_core_copy_regs(target_elf_gregset_t
*regs
,
1605 const CPUOpenRISCState
*env
)
1609 for (i
= 0; i
< 32; i
++) {
1610 (*regs
)[i
] = tswapreg(cpu_get_gpr(env
, i
));
1612 (*regs
)[32] = tswapreg(env
->pc
);
1613 (*regs
)[33] = tswapreg(cpu_get_sr(env
));
1616 #define ELF_PLATFORM NULL
1618 #endif /* TARGET_OPENRISC */
1622 #define ELF_CLASS ELFCLASS32
1623 #define ELF_ARCH EM_SH
1625 static inline void init_thread(struct target_pt_regs
*regs
,
1626 struct image_info
*infop
)
1628 /* Check other registers XXXXX */
1629 regs
->pc
= infop
->entry
;
1630 regs
->regs
[15] = infop
->start_stack
;
1633 /* See linux kernel: arch/sh/include/asm/elf.h. */
1635 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1637 /* See linux kernel: arch/sh/include/asm/ptrace.h. */
1642 TARGET_REG_GBR
= 19,
1643 TARGET_REG_MACH
= 20,
1644 TARGET_REG_MACL
= 21,
1645 TARGET_REG_SYSCALL
= 22
1648 static inline void elf_core_copy_regs(target_elf_gregset_t
*regs
,
1649 const CPUSH4State
*env
)
1653 for (i
= 0; i
< 16; i
++) {
1654 (*regs
)[i
] = tswapreg(env
->gregs
[i
]);
1657 (*regs
)[TARGET_REG_PC
] = tswapreg(env
->pc
);
1658 (*regs
)[TARGET_REG_PR
] = tswapreg(env
->pr
);
1659 (*regs
)[TARGET_REG_SR
] = tswapreg(env
->sr
);
1660 (*regs
)[TARGET_REG_GBR
] = tswapreg(env
->gbr
);
1661 (*regs
)[TARGET_REG_MACH
] = tswapreg(env
->mach
);
1662 (*regs
)[TARGET_REG_MACL
] = tswapreg(env
->macl
);
1663 (*regs
)[TARGET_REG_SYSCALL
] = 0; /* FIXME */
1666 #define USE_ELF_CORE_DUMP
1667 #define ELF_EXEC_PAGESIZE 4096
1670 SH_CPU_HAS_FPU
= 0x0001, /* Hardware FPU support */
1671 SH_CPU_HAS_P2_FLUSH_BUG
= 0x0002, /* Need to flush the cache in P2 area */
1672 SH_CPU_HAS_MMU_PAGE_ASSOC
= 0x0004, /* SH3: TLB way selection bit support */
1673 SH_CPU_HAS_DSP
= 0x0008, /* SH-DSP: DSP support */
1674 SH_CPU_HAS_PERF_COUNTER
= 0x0010, /* Hardware performance counters */
1675 SH_CPU_HAS_PTEA
= 0x0020, /* PTEA register */
1676 SH_CPU_HAS_LLSC
= 0x0040, /* movli.l/movco.l */
1677 SH_CPU_HAS_L2_CACHE
= 0x0080, /* Secondary cache / URAM */
1678 SH_CPU_HAS_OP32
= 0x0100, /* 32-bit instruction support */
1679 SH_CPU_HAS_PTEAEX
= 0x0200, /* PTE ASID Extension support */
1682 #define ELF_HWCAP get_elf_hwcap()
1684 static uint32_t get_elf_hwcap(void)
1686 SuperHCPU
*cpu
= SUPERH_CPU(thread_cpu
);
1689 hwcap
|= SH_CPU_HAS_FPU
;
1691 if (cpu
->env
.features
& SH_FEATURE_SH4A
) {
1692 hwcap
|= SH_CPU_HAS_LLSC
;
1702 #define ELF_CLASS ELFCLASS32
1703 #define ELF_ARCH EM_CRIS
1705 static inline void init_thread(struct target_pt_regs
*regs
,
1706 struct image_info
*infop
)
1708 regs
->erp
= infop
->entry
;
1711 #define ELF_EXEC_PAGESIZE 8192
1717 #define ELF_CLASS ELFCLASS32
1718 #define ELF_ARCH EM_68K
1720 /* ??? Does this need to do anything?
1721 #define ELF_PLAT_INIT(_r) */
1723 static inline void init_thread(struct target_pt_regs
*regs
,
1724 struct image_info
*infop
)
1726 regs
->usp
= infop
->start_stack
;
1728 regs
->pc
= infop
->entry
;
1731 /* See linux kernel: arch/m68k/include/asm/elf.h. */
1733 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1735 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUM68KState
*env
)
1737 (*regs
)[0] = tswapreg(env
->dregs
[1]);
1738 (*regs
)[1] = tswapreg(env
->dregs
[2]);
1739 (*regs
)[2] = tswapreg(env
->dregs
[3]);
1740 (*regs
)[3] = tswapreg(env
->dregs
[4]);
1741 (*regs
)[4] = tswapreg(env
->dregs
[5]);
1742 (*regs
)[5] = tswapreg(env
->dregs
[6]);
1743 (*regs
)[6] = tswapreg(env
->dregs
[7]);
1744 (*regs
)[7] = tswapreg(env
->aregs
[0]);
1745 (*regs
)[8] = tswapreg(env
->aregs
[1]);
1746 (*regs
)[9] = tswapreg(env
->aregs
[2]);
1747 (*regs
)[10] = tswapreg(env
->aregs
[3]);
1748 (*regs
)[11] = tswapreg(env
->aregs
[4]);
1749 (*regs
)[12] = tswapreg(env
->aregs
[5]);
1750 (*regs
)[13] = tswapreg(env
->aregs
[6]);
1751 (*regs
)[14] = tswapreg(env
->dregs
[0]);
1752 (*regs
)[15] = tswapreg(env
->aregs
[7]);
1753 (*regs
)[16] = tswapreg(env
->dregs
[0]); /* FIXME: orig_d0 */
1754 (*regs
)[17] = tswapreg(env
->sr
);
1755 (*regs
)[18] = tswapreg(env
->pc
);
1756 (*regs
)[19] = 0; /* FIXME: regs->format | regs->vector */
1759 #define USE_ELF_CORE_DUMP
1760 #define ELF_EXEC_PAGESIZE 8192
1766 #define ELF_CLASS ELFCLASS64
1767 #define ELF_ARCH EM_ALPHA
1769 static inline void init_thread(struct target_pt_regs
*regs
,
1770 struct image_info
*infop
)
1772 regs
->pc
= infop
->entry
;
1774 regs
->usp
= infop
->start_stack
;
1777 #define ELF_EXEC_PAGESIZE 8192
1779 #endif /* TARGET_ALPHA */
1783 #define ELF_CLASS ELFCLASS64
1784 #define ELF_DATA ELFDATA2MSB
1785 #define ELF_ARCH EM_S390
1789 #define ELF_HWCAP get_elf_hwcap()
1791 #define GET_FEATURE(_feat, _hwcap) \
1792 do { if (s390_has_feat(_feat)) { hwcap |= _hwcap; } } while (0)
1794 uint32_t get_elf_hwcap(void)
1797 * Let's assume we always have esan3 and zarch.
1798 * 31-bit processes can use 64-bit registers (high gprs).
1800 uint32_t hwcap
= HWCAP_S390_ESAN3
| HWCAP_S390_ZARCH
| HWCAP_S390_HIGH_GPRS
;
1802 GET_FEATURE(S390_FEAT_STFLE
, HWCAP_S390_STFLE
);
1803 GET_FEATURE(S390_FEAT_MSA
, HWCAP_S390_MSA
);
1804 GET_FEATURE(S390_FEAT_LONG_DISPLACEMENT
, HWCAP_S390_LDISP
);
1805 GET_FEATURE(S390_FEAT_EXTENDED_IMMEDIATE
, HWCAP_S390_EIMM
);
1806 if (s390_has_feat(S390_FEAT_EXTENDED_TRANSLATION_3
) &&
1807 s390_has_feat(S390_FEAT_ETF3_ENH
)) {
1808 hwcap
|= HWCAP_S390_ETF3EH
;
1810 GET_FEATURE(S390_FEAT_VECTOR
, HWCAP_S390_VXRS
);
1811 GET_FEATURE(S390_FEAT_VECTOR_ENH
, HWCAP_S390_VXRS_EXT
);
1812 GET_FEATURE(S390_FEAT_VECTOR_ENH2
, HWCAP_S390_VXRS_EXT2
);
1817 const char *elf_hwcap_str(uint32_t bit
)
1819 static const char *hwcap_str
[] = {
1820 [HWCAP_S390_NR_ESAN3
] = "esan3",
1821 [HWCAP_S390_NR_ZARCH
] = "zarch",
1822 [HWCAP_S390_NR_STFLE
] = "stfle",
1823 [HWCAP_S390_NR_MSA
] = "msa",
1824 [HWCAP_S390_NR_LDISP
] = "ldisp",
1825 [HWCAP_S390_NR_EIMM
] = "eimm",
1826 [HWCAP_S390_NR_DFP
] = "dfp",
1827 [HWCAP_S390_NR_HPAGE
] = "edat",
1828 [HWCAP_S390_NR_ETF3EH
] = "etf3eh",
1829 [HWCAP_S390_NR_HIGH_GPRS
] = "highgprs",
1830 [HWCAP_S390_NR_TE
] = "te",
1831 [HWCAP_S390_NR_VXRS
] = "vx",
1832 [HWCAP_S390_NR_VXRS_BCD
] = "vxd",
1833 [HWCAP_S390_NR_VXRS_EXT
] = "vxe",
1834 [HWCAP_S390_NR_GS
] = "gs",
1835 [HWCAP_S390_NR_VXRS_EXT2
] = "vxe2",
1836 [HWCAP_S390_NR_VXRS_PDE
] = "vxp",
1837 [HWCAP_S390_NR_SORT
] = "sort",
1838 [HWCAP_S390_NR_DFLT
] = "dflt",
1839 [HWCAP_S390_NR_NNPA
] = "nnpa",
1840 [HWCAP_S390_NR_PCI_MIO
] = "pcimio",
1841 [HWCAP_S390_NR_SIE
] = "sie",
1844 return bit
< ARRAY_SIZE(hwcap_str
) ? hwcap_str
[bit
] : NULL
;
1847 static inline void init_thread(struct target_pt_regs
*regs
, struct image_info
*infop
)
1849 regs
->psw
.addr
= infop
->entry
;
1850 regs
->psw
.mask
= PSW_MASK_DAT
| PSW_MASK_IO
| PSW_MASK_EXT
| \
1851 PSW_MASK_MCHECK
| PSW_MASK_PSTATE
| PSW_MASK_64
| \
1853 regs
->gprs
[15] = infop
->start_stack
;
1856 /* See linux kernel: arch/s390/include/uapi/asm/ptrace.h (s390_regs). */
1858 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1861 TARGET_REG_PSWM
= 0,
1862 TARGET_REG_PSWA
= 1,
1863 TARGET_REG_GPRS
= 2,
1864 TARGET_REG_ARS
= 18,
1865 TARGET_REG_ORIG_R2
= 26,
1868 static void elf_core_copy_regs(target_elf_gregset_t
*regs
,
1869 const CPUS390XState
*env
)
1874 (*regs
)[TARGET_REG_PSWM
] = tswapreg(env
->psw
.mask
);
1875 (*regs
)[TARGET_REG_PSWA
] = tswapreg(env
->psw
.addr
);
1876 for (i
= 0; i
< 16; i
++) {
1877 (*regs
)[TARGET_REG_GPRS
+ i
] = tswapreg(env
->regs
[i
]);
1879 aregs
= (uint32_t *)&((*regs
)[TARGET_REG_ARS
]);
1880 for (i
= 0; i
< 16; i
++) {
1881 aregs
[i
] = tswap32(env
->aregs
[i
]);
1883 (*regs
)[TARGET_REG_ORIG_R2
] = 0;
1886 #define USE_ELF_CORE_DUMP
1887 #define ELF_EXEC_PAGESIZE 4096
1889 #endif /* TARGET_S390X */
1893 #define ELF_ARCH EM_RISCV
1895 #ifdef TARGET_RISCV32
1896 #define ELF_CLASS ELFCLASS32
1897 #define VDSO_HEADER "vdso-32.c.inc"
1899 #define ELF_CLASS ELFCLASS64
1900 #define VDSO_HEADER "vdso-64.c.inc"
1903 #define ELF_HWCAP get_elf_hwcap()
1905 static uint32_t get_elf_hwcap(void)
1907 #define MISA_BIT(EXT) (1 << (EXT - 'A'))
1908 RISCVCPU
*cpu
= RISCV_CPU(thread_cpu
);
1909 uint32_t mask
= MISA_BIT('I') | MISA_BIT('M') | MISA_BIT('A')
1910 | MISA_BIT('F') | MISA_BIT('D') | MISA_BIT('C')
1913 return cpu
->env
.misa_ext
& mask
;
1917 static inline void init_thread(struct target_pt_regs
*regs
,
1918 struct image_info
*infop
)
1920 regs
->sepc
= infop
->entry
;
1921 regs
->sp
= infop
->start_stack
;
1924 #define ELF_EXEC_PAGESIZE 4096
1926 #endif /* TARGET_RISCV */
1930 #define ELF_CLASS ELFCLASS32
1931 #define ELF_ARCH EM_PARISC
1932 #define ELF_PLATFORM "PARISC"
1933 #define STACK_GROWS_DOWN 0
1934 #define STACK_ALIGNMENT 64
1936 #define VDSO_HEADER "vdso.c.inc"
1938 static inline void init_thread(struct target_pt_regs
*regs
,
1939 struct image_info
*infop
)
1941 regs
->iaoq
[0] = infop
->entry
;
1942 regs
->iaoq
[1] = infop
->entry
+ 4;
1944 regs
->gr
[24] = infop
->argv
;
1945 regs
->gr
[25] = infop
->argc
;
1946 /* The top-of-stack contains a linkage buffer. */
1947 regs
->gr
[30] = infop
->start_stack
+ 64;
1948 regs
->gr
[31] = infop
->entry
;
1951 #define LO_COMMPAGE 0
1953 static bool init_guest_commpage(void)
1955 void *want
= g2h_untagged(LO_COMMPAGE
);
1956 void *addr
= mmap(want
, qemu_host_page_size
, PROT_NONE
,
1957 MAP_ANONYMOUS
| MAP_PRIVATE
| MAP_FIXED
, -1, 0);
1959 if (addr
== MAP_FAILED
) {
1960 perror("Allocating guest commpage");
1968 * On Linux, page zero is normally marked execute only + gateway.
1969 * Normal read or write is supposed to fail (thus PROT_NONE above),
1970 * but specific offsets have kernel code mapped to raise permissions
1971 * and implement syscalls. Here, simply mark the page executable.
1972 * Special case the entry points during translation (see do_page_zero).
1974 page_set_flags(LO_COMMPAGE
, LO_COMMPAGE
| ~TARGET_PAGE_MASK
,
1975 PAGE_EXEC
| PAGE_VALID
);
1979 #endif /* TARGET_HPPA */
1981 #ifdef TARGET_XTENSA
1983 #define ELF_CLASS ELFCLASS32
1984 #define ELF_ARCH EM_XTENSA
1986 static inline void init_thread(struct target_pt_regs
*regs
,
1987 struct image_info
*infop
)
1989 regs
->windowbase
= 0;
1990 regs
->windowstart
= 1;
1991 regs
->areg
[1] = infop
->start_stack
;
1992 regs
->pc
= infop
->entry
;
1993 if (info_is_fdpic(infop
)) {
1994 regs
->areg
[4] = infop
->loadmap_addr
;
1995 regs
->areg
[5] = infop
->interpreter_loadmap_addr
;
1996 if (infop
->interpreter_loadmap_addr
) {
1997 regs
->areg
[6] = infop
->interpreter_pt_dynamic_addr
;
1999 regs
->areg
[6] = infop
->pt_dynamic_addr
;
2004 /* See linux kernel: arch/xtensa/include/asm/elf.h. */
2005 #define ELF_NREG 128
2006 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
2015 TARGET_REG_WINDOWSTART
,
2016 TARGET_REG_WINDOWBASE
,
2017 TARGET_REG_THREADPTR
,
2018 TARGET_REG_AR0
= 64,
2021 static void elf_core_copy_regs(target_elf_gregset_t
*regs
,
2022 const CPUXtensaState
*env
)
2026 (*regs
)[TARGET_REG_PC
] = tswapreg(env
->pc
);
2027 (*regs
)[TARGET_REG_PS
] = tswapreg(env
->sregs
[PS
] & ~PS_EXCM
);
2028 (*regs
)[TARGET_REG_LBEG
] = tswapreg(env
->sregs
[LBEG
]);
2029 (*regs
)[TARGET_REG_LEND
] = tswapreg(env
->sregs
[LEND
]);
2030 (*regs
)[TARGET_REG_LCOUNT
] = tswapreg(env
->sregs
[LCOUNT
]);
2031 (*regs
)[TARGET_REG_SAR
] = tswapreg(env
->sregs
[SAR
]);
2032 (*regs
)[TARGET_REG_WINDOWSTART
] = tswapreg(env
->sregs
[WINDOW_START
]);
2033 (*regs
)[TARGET_REG_WINDOWBASE
] = tswapreg(env
->sregs
[WINDOW_BASE
]);
2034 (*regs
)[TARGET_REG_THREADPTR
] = tswapreg(env
->uregs
[THREADPTR
]);
2035 xtensa_sync_phys_from_window((CPUXtensaState
*)env
);
2036 for (i
= 0; i
< env
->config
->nareg
; ++i
) {
2037 (*regs
)[TARGET_REG_AR0
+ i
] = tswapreg(env
->phys_regs
[i
]);
2041 #define USE_ELF_CORE_DUMP
2042 #define ELF_EXEC_PAGESIZE 4096
2044 #endif /* TARGET_XTENSA */
2046 #ifdef TARGET_HEXAGON
2048 #define ELF_CLASS ELFCLASS32
2049 #define ELF_ARCH EM_HEXAGON
2051 static inline void init_thread(struct target_pt_regs
*regs
,
2052 struct image_info
*infop
)
2054 regs
->sepc
= infop
->entry
;
2055 regs
->sp
= infop
->start_stack
;
2058 #endif /* TARGET_HEXAGON */
2060 #ifndef ELF_BASE_PLATFORM
2061 #define ELF_BASE_PLATFORM (NULL)
2064 #ifndef ELF_PLATFORM
2065 #define ELF_PLATFORM (NULL)
2069 #define ELF_MACHINE ELF_ARCH
2072 #ifndef elf_check_arch
2073 #define elf_check_arch(x) ((x) == ELF_ARCH)
2076 #ifndef elf_check_abi
2077 #define elf_check_abi(x) (1)
2084 #ifndef STACK_GROWS_DOWN
2085 #define STACK_GROWS_DOWN 1
2088 #ifndef STACK_ALIGNMENT
2089 #define STACK_ALIGNMENT 16
2094 #define ELF_CLASS ELFCLASS32
2096 #define bswaptls(ptr) bswap32s(ptr)
2099 #ifndef EXSTACK_DEFAULT
2100 #define EXSTACK_DEFAULT false
2105 /* We must delay the following stanzas until after "elf.h". */
2106 #if defined(TARGET_AARCH64)
2108 static bool arch_parse_elf_property(uint32_t pr_type
, uint32_t pr_datasz
,
2109 const uint32_t *data
,
2110 struct image_info
*info
,
2113 if (pr_type
== GNU_PROPERTY_AARCH64_FEATURE_1_AND
) {
2114 if (pr_datasz
!= sizeof(uint32_t)) {
2115 error_setg(errp
, "Ill-formed GNU_PROPERTY_AARCH64_FEATURE_1_AND");
2118 /* We will extract GNU_PROPERTY_AARCH64_FEATURE_1_BTI later. */
2119 info
->note_flags
= *data
;
2123 #define ARCH_USE_GNU_PROPERTY 1
2127 static bool arch_parse_elf_property(uint32_t pr_type
, uint32_t pr_datasz
,
2128 const uint32_t *data
,
2129 struct image_info
*info
,
2132 g_assert_not_reached();
2134 #define ARCH_USE_GNU_PROPERTY 0
2140 unsigned int a_info
; /* Use macros N_MAGIC, etc for access */
2141 unsigned int a_text
; /* length of text, in bytes */
2142 unsigned int a_data
; /* length of data, in bytes */
2143 unsigned int a_bss
; /* length of uninitialized data area, in bytes */
2144 unsigned int a_syms
; /* length of symbol table data in file, in bytes */
2145 unsigned int a_entry
; /* start address */
2146 unsigned int a_trsize
; /* length of relocation info for text, in bytes */
2147 unsigned int a_drsize
; /* length of relocation info for data, in bytes */
2151 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
2157 #define DLINFO_ITEMS 16
2159 static inline void memcpy_fromfs(void * to
, const void * from
, unsigned long n
)
2161 memcpy(to
, from
, n
);
2165 static void bswap_ehdr(struct elfhdr
*ehdr
)
2167 bswap16s(&ehdr
->e_type
); /* Object file type */
2168 bswap16s(&ehdr
->e_machine
); /* Architecture */
2169 bswap32s(&ehdr
->e_version
); /* Object file version */
2170 bswaptls(&ehdr
->e_entry
); /* Entry point virtual address */
2171 bswaptls(&ehdr
->e_phoff
); /* Program header table file offset */
2172 bswaptls(&ehdr
->e_shoff
); /* Section header table file offset */
2173 bswap32s(&ehdr
->e_flags
); /* Processor-specific flags */
2174 bswap16s(&ehdr
->e_ehsize
); /* ELF header size in bytes */
2175 bswap16s(&ehdr
->e_phentsize
); /* Program header table entry size */
2176 bswap16s(&ehdr
->e_phnum
); /* Program header table entry count */
2177 bswap16s(&ehdr
->e_shentsize
); /* Section header table entry size */
2178 bswap16s(&ehdr
->e_shnum
); /* Section header table entry count */
2179 bswap16s(&ehdr
->e_shstrndx
); /* Section header string table index */
2182 static void bswap_phdr(struct elf_phdr
*phdr
, int phnum
)
2185 for (i
= 0; i
< phnum
; ++i
, ++phdr
) {
2186 bswap32s(&phdr
->p_type
); /* Segment type */
2187 bswap32s(&phdr
->p_flags
); /* Segment flags */
2188 bswaptls(&phdr
->p_offset
); /* Segment file offset */
2189 bswaptls(&phdr
->p_vaddr
); /* Segment virtual address */
2190 bswaptls(&phdr
->p_paddr
); /* Segment physical address */
2191 bswaptls(&phdr
->p_filesz
); /* Segment size in file */
2192 bswaptls(&phdr
->p_memsz
); /* Segment size in memory */
2193 bswaptls(&phdr
->p_align
); /* Segment alignment */
2197 static void bswap_shdr(struct elf_shdr
*shdr
, int shnum
)
2200 for (i
= 0; i
< shnum
; ++i
, ++shdr
) {
2201 bswap32s(&shdr
->sh_name
);
2202 bswap32s(&shdr
->sh_type
);
2203 bswaptls(&shdr
->sh_flags
);
2204 bswaptls(&shdr
->sh_addr
);
2205 bswaptls(&shdr
->sh_offset
);
2206 bswaptls(&shdr
->sh_size
);
2207 bswap32s(&shdr
->sh_link
);
2208 bswap32s(&shdr
->sh_info
);
2209 bswaptls(&shdr
->sh_addralign
);
2210 bswaptls(&shdr
->sh_entsize
);
2214 static void bswap_sym(struct elf_sym
*sym
)
2216 bswap32s(&sym
->st_name
);
2217 bswaptls(&sym
->st_value
);
2218 bswaptls(&sym
->st_size
);
2219 bswap16s(&sym
->st_shndx
);
2223 static void bswap_mips_abiflags(Mips_elf_abiflags_v0
*abiflags
)
2225 bswap16s(&abiflags
->version
);
2226 bswap32s(&abiflags
->ases
);
2227 bswap32s(&abiflags
->isa_ext
);
2228 bswap32s(&abiflags
->flags1
);
2229 bswap32s(&abiflags
->flags2
);
2233 static inline void bswap_ehdr(struct elfhdr
*ehdr
) { }
2234 static inline void bswap_phdr(struct elf_phdr
*phdr
, int phnum
) { }
2235 static inline void bswap_shdr(struct elf_shdr
*shdr
, int shnum
) { }
2236 static inline void bswap_sym(struct elf_sym
*sym
) { }
2238 static inline void bswap_mips_abiflags(Mips_elf_abiflags_v0
*abiflags
) { }
2242 #ifdef USE_ELF_CORE_DUMP
2243 static int elf_core_dump(int, const CPUArchState
*);
2244 #endif /* USE_ELF_CORE_DUMP */
2245 static void load_symbols(struct elfhdr
*hdr
, const ImageSource
*src
,
2246 abi_ulong load_bias
);
2248 /* Verify the portions of EHDR within E_IDENT for the target.
2249 This can be performed before bswapping the entire header. */
2250 static bool elf_check_ident(struct elfhdr
*ehdr
)
2252 return (ehdr
->e_ident
[EI_MAG0
] == ELFMAG0
2253 && ehdr
->e_ident
[EI_MAG1
] == ELFMAG1
2254 && ehdr
->e_ident
[EI_MAG2
] == ELFMAG2
2255 && ehdr
->e_ident
[EI_MAG3
] == ELFMAG3
2256 && ehdr
->e_ident
[EI_CLASS
] == ELF_CLASS
2257 && ehdr
->e_ident
[EI_DATA
] == ELF_DATA
2258 && ehdr
->e_ident
[EI_VERSION
] == EV_CURRENT
);
2261 /* Verify the portions of EHDR outside of E_IDENT for the target.
2262 This has to wait until after bswapping the header. */
2263 static bool elf_check_ehdr(struct elfhdr
*ehdr
)
2265 return (elf_check_arch(ehdr
->e_machine
)
2266 && elf_check_abi(ehdr
->e_flags
)
2267 && ehdr
->e_ehsize
== sizeof(struct elfhdr
)
2268 && ehdr
->e_phentsize
== sizeof(struct elf_phdr
)
2269 && (ehdr
->e_type
== ET_EXEC
|| ehdr
->e_type
== ET_DYN
));
2273 * 'copy_elf_strings()' copies argument/envelope strings from user
2274 * memory to free pages in kernel mem. These are in a format ready
2275 * to be put directly into the top of new user memory.
2278 static abi_ulong
copy_elf_strings(int argc
, char **argv
, char *scratch
,
2279 abi_ulong p
, abi_ulong stack_limit
)
2286 return 0; /* bullet-proofing */
2289 if (STACK_GROWS_DOWN
) {
2290 int offset
= ((p
- 1) % TARGET_PAGE_SIZE
) + 1;
2291 for (i
= argc
- 1; i
>= 0; --i
) {
2294 fprintf(stderr
, "VFS: argc is wrong");
2297 len
= strlen(tmp
) + 1;
2300 if (len
> (p
- stack_limit
)) {
2304 int bytes_to_copy
= (len
> offset
) ? offset
: len
;
2305 tmp
-= bytes_to_copy
;
2307 offset
-= bytes_to_copy
;
2308 len
-= bytes_to_copy
;
2310 memcpy_fromfs(scratch
+ offset
, tmp
, bytes_to_copy
);
2313 memcpy_to_target(p
, scratch
, top
- p
);
2315 offset
= TARGET_PAGE_SIZE
;
2320 memcpy_to_target(p
, scratch
+ offset
, top
- p
);
2323 int remaining
= TARGET_PAGE_SIZE
- (p
% TARGET_PAGE_SIZE
);
2324 for (i
= 0; i
< argc
; ++i
) {
2327 fprintf(stderr
, "VFS: argc is wrong");
2330 len
= strlen(tmp
) + 1;
2331 if (len
> (stack_limit
- p
)) {
2335 int bytes_to_copy
= (len
> remaining
) ? remaining
: len
;
2337 memcpy_fromfs(scratch
+ (p
- top
), tmp
, bytes_to_copy
);
2339 tmp
+= bytes_to_copy
;
2340 remaining
-= bytes_to_copy
;
2342 len
-= bytes_to_copy
;
2344 if (remaining
== 0) {
2345 memcpy_to_target(top
, scratch
, p
- top
);
2347 remaining
= TARGET_PAGE_SIZE
;
2352 memcpy_to_target(top
, scratch
, p
- top
);
2359 /* Older linux kernels provide up to MAX_ARG_PAGES (default: 32) of
2360 * argument/environment space. Newer kernels (>2.6.33) allow more,
2361 * dependent on stack size, but guarantee at least 32 pages for
2362 * backwards compatibility.
2364 #define STACK_LOWER_LIMIT (32 * TARGET_PAGE_SIZE)
2366 static abi_ulong
setup_arg_pages(struct linux_binprm
*bprm
,
2367 struct image_info
*info
)
2369 abi_ulong size
, error
, guard
;
2372 size
= guest_stack_size
;
2373 if (size
< STACK_LOWER_LIMIT
) {
2374 size
= STACK_LOWER_LIMIT
;
2377 if (STACK_GROWS_DOWN
) {
2378 guard
= TARGET_PAGE_SIZE
;
2379 if (guard
< qemu_real_host_page_size()) {
2380 guard
= qemu_real_host_page_size();
2383 /* no guard page for hppa target where stack grows upwards. */
2387 prot
= PROT_READ
| PROT_WRITE
;
2388 if (info
->exec_stack
) {
2391 error
= target_mmap(0, size
+ guard
, prot
,
2392 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
2394 perror("mmap stack");
2398 /* We reserve one extra page at the top of the stack as guard. */
2399 if (STACK_GROWS_DOWN
) {
2400 target_mprotect(error
, guard
, PROT_NONE
);
2401 info
->stack_limit
= error
+ guard
;
2402 return info
->stack_limit
+ size
- sizeof(void *);
2404 info
->stack_limit
= error
+ size
;
2412 * Map and zero the bss. We need to explicitly zero any fractional pages
2413 * after the data section (i.e. bss). Return false on mapping failure.
2415 static bool zero_bss(abi_ulong start_bss
, abi_ulong end_bss
,
2416 int prot
, Error
**errp
)
2418 abi_ulong align_bss
;
2420 /* We only expect writable bss; the code segment shouldn't need this. */
2421 if (!(prot
& PROT_WRITE
)) {
2422 error_setg(errp
, "PT_LOAD with non-writable bss");
2426 align_bss
= TARGET_PAGE_ALIGN(start_bss
);
2427 end_bss
= TARGET_PAGE_ALIGN(end_bss
);
2429 if (start_bss
< align_bss
) {
2430 int flags
= page_get_flags(start_bss
);
2432 if (!(flags
& PAGE_BITS
)) {
2434 * The whole address space of the executable was reserved
2435 * at the start, therefore all pages will be VALID.
2436 * But assuming there are no PROT_NONE PT_LOAD segments,
2437 * a PROT_NONE page means no data all bss, and we can
2438 * simply extend the new anon mapping back to the start
2439 * of the page of bss.
2441 align_bss
-= TARGET_PAGE_SIZE
;
2444 * The start of the bss shares a page with something.
2445 * The only thing that we expect is the data section,
2446 * which would already be marked writable.
2447 * Overlapping the RX code segment seems malformed.
2449 if (!(flags
& PAGE_WRITE
)) {
2450 error_setg(errp
, "PT_LOAD with bss overlapping "
2451 "non-writable page");
2455 /* The page is already mapped and writable. */
2456 memset(g2h_untagged(start_bss
), 0, align_bss
- start_bss
);
2460 if (align_bss
< end_bss
&&
2461 target_mmap(align_bss
, end_bss
- align_bss
, prot
,
2462 MAP_FIXED
| MAP_PRIVATE
| MAP_ANON
, -1, 0) == -1) {
2463 error_setg_errno(errp
, errno
, "Error mapping bss");
2469 #if defined(TARGET_ARM)
2470 static int elf_is_fdpic(struct elfhdr
*exec
)
2472 return exec
->e_ident
[EI_OSABI
] == ELFOSABI_ARM_FDPIC
;
2474 #elif defined(TARGET_XTENSA)
2475 static int elf_is_fdpic(struct elfhdr
*exec
)
2477 return exec
->e_ident
[EI_OSABI
] == ELFOSABI_XTENSA_FDPIC
;
2480 /* Default implementation, always false. */
2481 static int elf_is_fdpic(struct elfhdr
*exec
)
2487 static abi_ulong
loader_build_fdpic_loadmap(struct image_info
*info
, abi_ulong sp
)
2490 struct elf32_fdpic_loadseg
*loadsegs
= info
->loadsegs
;
2492 /* elf32_fdpic_loadseg */
2496 put_user_u32(loadsegs
[n
].addr
, sp
+0);
2497 put_user_u32(loadsegs
[n
].p_vaddr
, sp
+4);
2498 put_user_u32(loadsegs
[n
].p_memsz
, sp
+8);
2501 /* elf32_fdpic_loadmap */
2503 put_user_u16(0, sp
+0); /* version */
2504 put_user_u16(info
->nsegs
, sp
+2); /* nsegs */
2506 info
->personality
= PER_LINUX_FDPIC
;
2507 info
->loadmap_addr
= sp
;
2512 static abi_ulong
create_elf_tables(abi_ulong p
, int argc
, int envc
,
2513 struct elfhdr
*exec
,
2514 struct image_info
*info
,
2515 struct image_info
*interp_info
,
2516 struct image_info
*vdso_info
)
2519 abi_ulong u_argc
, u_argv
, u_envp
, u_auxv
;
2522 abi_ulong u_rand_bytes
;
2523 uint8_t k_rand_bytes
[16];
2524 abi_ulong u_platform
, u_base_platform
;
2525 const char *k_platform
, *k_base_platform
;
2526 const int n
= sizeof(elf_addr_t
);
2530 /* Needs to be before we load the env/argc/... */
2531 if (elf_is_fdpic(exec
)) {
2532 /* Need 4 byte alignment for these structs */
2534 sp
= loader_build_fdpic_loadmap(info
, sp
);
2535 info
->other_info
= interp_info
;
2537 interp_info
->other_info
= info
;
2538 sp
= loader_build_fdpic_loadmap(interp_info
, sp
);
2539 info
->interpreter_loadmap_addr
= interp_info
->loadmap_addr
;
2540 info
->interpreter_pt_dynamic_addr
= interp_info
->pt_dynamic_addr
;
2542 info
->interpreter_loadmap_addr
= 0;
2543 info
->interpreter_pt_dynamic_addr
= 0;
2547 u_base_platform
= 0;
2548 k_base_platform
= ELF_BASE_PLATFORM
;
2549 if (k_base_platform
) {
2550 size_t len
= strlen(k_base_platform
) + 1;
2551 if (STACK_GROWS_DOWN
) {
2552 sp
-= (len
+ n
- 1) & ~(n
- 1);
2553 u_base_platform
= sp
;
2554 /* FIXME - check return value of memcpy_to_target() for failure */
2555 memcpy_to_target(sp
, k_base_platform
, len
);
2557 memcpy_to_target(sp
, k_base_platform
, len
);
2558 u_base_platform
= sp
;
2564 k_platform
= ELF_PLATFORM
;
2566 size_t len
= strlen(k_platform
) + 1;
2567 if (STACK_GROWS_DOWN
) {
2568 sp
-= (len
+ n
- 1) & ~(n
- 1);
2570 /* FIXME - check return value of memcpy_to_target() for failure */
2571 memcpy_to_target(sp
, k_platform
, len
);
2573 memcpy_to_target(sp
, k_platform
, len
);
2579 /* Provide 16 byte alignment for the PRNG, and basic alignment for
2580 * the argv and envp pointers.
2582 if (STACK_GROWS_DOWN
) {
2583 sp
= QEMU_ALIGN_DOWN(sp
, 16);
2585 sp
= QEMU_ALIGN_UP(sp
, 16);
2589 * Generate 16 random bytes for userspace PRNG seeding.
2591 qemu_guest_getrandom_nofail(k_rand_bytes
, sizeof(k_rand_bytes
));
2592 if (STACK_GROWS_DOWN
) {
2595 /* FIXME - check return value of memcpy_to_target() for failure */
2596 memcpy_to_target(sp
, k_rand_bytes
, 16);
2598 memcpy_to_target(sp
, k_rand_bytes
, 16);
2603 size
= (DLINFO_ITEMS
+ 1) * 2;
2604 if (k_base_platform
) {
2613 #ifdef DLINFO_ARCH_ITEMS
2614 size
+= DLINFO_ARCH_ITEMS
* 2;
2619 info
->auxv_len
= size
* n
;
2621 size
+= envc
+ argc
+ 2;
2622 size
+= 1; /* argc itself */
2625 /* Allocate space and finalize stack alignment for entry now. */
2626 if (STACK_GROWS_DOWN
) {
2627 u_argc
= QEMU_ALIGN_DOWN(sp
- size
, STACK_ALIGNMENT
);
2631 sp
= QEMU_ALIGN_UP(sp
+ size
, STACK_ALIGNMENT
);
2634 u_argv
= u_argc
+ n
;
2635 u_envp
= u_argv
+ (argc
+ 1) * n
;
2636 u_auxv
= u_envp
+ (envc
+ 1) * n
;
2637 info
->saved_auxv
= u_auxv
;
2640 info
->argv
= u_argv
;
2641 info
->envp
= u_envp
;
2643 /* This is correct because Linux defines
2644 * elf_addr_t as Elf32_Off / Elf64_Off
2646 #define NEW_AUX_ENT(id, val) do { \
2647 put_user_ual(id, u_auxv); u_auxv += n; \
2648 put_user_ual(val, u_auxv); u_auxv += n; \
2653 * ARCH_DLINFO must come first so platform specific code can enforce
2654 * special alignment requirements on the AUXV if necessary (eg. PPC).
2658 /* There must be exactly DLINFO_ITEMS entries here, or the assert
2659 * on info->auxv_len will trigger.
2661 NEW_AUX_ENT(AT_PHDR
, (abi_ulong
)(info
->load_addr
+ exec
->e_phoff
));
2662 NEW_AUX_ENT(AT_PHENT
, (abi_ulong
)(sizeof (struct elf_phdr
)));
2663 NEW_AUX_ENT(AT_PHNUM
, (abi_ulong
)(exec
->e_phnum
));
2664 if ((info
->alignment
& ~qemu_host_page_mask
) != 0) {
2665 /* Target doesn't support host page size alignment */
2666 NEW_AUX_ENT(AT_PAGESZ
, (abi_ulong
)(TARGET_PAGE_SIZE
));
2668 NEW_AUX_ENT(AT_PAGESZ
, (abi_ulong
)(MAX(TARGET_PAGE_SIZE
,
2669 qemu_host_page_size
)));
2671 NEW_AUX_ENT(AT_BASE
, (abi_ulong
)(interp_info
? interp_info
->load_addr
: 0));
2672 NEW_AUX_ENT(AT_FLAGS
, (abi_ulong
)0);
2673 NEW_AUX_ENT(AT_ENTRY
, info
->entry
);
2674 NEW_AUX_ENT(AT_UID
, (abi_ulong
) getuid());
2675 NEW_AUX_ENT(AT_EUID
, (abi_ulong
) geteuid());
2676 NEW_AUX_ENT(AT_GID
, (abi_ulong
) getgid());
2677 NEW_AUX_ENT(AT_EGID
, (abi_ulong
) getegid());
2678 NEW_AUX_ENT(AT_HWCAP
, (abi_ulong
) ELF_HWCAP
);
2679 NEW_AUX_ENT(AT_CLKTCK
, (abi_ulong
) sysconf(_SC_CLK_TCK
));
2680 NEW_AUX_ENT(AT_RANDOM
, (abi_ulong
) u_rand_bytes
);
2681 NEW_AUX_ENT(AT_SECURE
, (abi_ulong
) qemu_getauxval(AT_SECURE
));
2682 NEW_AUX_ENT(AT_EXECFN
, info
->file_string
);
2685 NEW_AUX_ENT(AT_HWCAP2
, (abi_ulong
) ELF_HWCAP2
);
2688 if (u_base_platform
) {
2689 NEW_AUX_ENT(AT_BASE_PLATFORM
, u_base_platform
);
2692 NEW_AUX_ENT(AT_PLATFORM
, u_platform
);
2695 NEW_AUX_ENT(AT_SYSINFO_EHDR
, vdso_info
->load_addr
);
2697 NEW_AUX_ENT (AT_NULL
, 0);
2700 /* Check that our initial calculation of the auxv length matches how much
2701 * we actually put into it.
2703 assert(info
->auxv_len
== u_auxv
- info
->saved_auxv
);
2705 put_user_ual(argc
, u_argc
);
2707 p
= info
->arg_strings
;
2708 for (i
= 0; i
< argc
; ++i
) {
2709 put_user_ual(p
, u_argv
);
2711 p
+= target_strlen(p
) + 1;
2713 put_user_ual(0, u_argv
);
2715 p
= info
->env_strings
;
2716 for (i
= 0; i
< envc
; ++i
) {
2717 put_user_ual(p
, u_envp
);
2719 p
+= target_strlen(p
) + 1;
2721 put_user_ual(0, u_envp
);
2726 #if defined(HI_COMMPAGE)
2727 #define LO_COMMPAGE -1
2728 #elif defined(LO_COMMPAGE)
2729 #define HI_COMMPAGE 0
2731 #define HI_COMMPAGE 0
2732 #define LO_COMMPAGE -1
2733 #ifndef INIT_GUEST_COMMPAGE
2734 #define init_guest_commpage() true
2740 * @addr: host start address
2741 * @addr_last: host last address
2742 * @keep: do not unmap the probe region
2744 * Return 1 if [@addr, @addr_last] is not mapped in the host,
2745 * return 0 if it is not available to map, and -1 on mmap error.
2746 * If @keep, the region is left mapped on success, otherwise unmapped.
2748 static int pgb_try_mmap(uintptr_t addr
, uintptr_t addr_last
, bool keep
)
2750 size_t size
= addr_last
- addr
+ 1;
2751 void *p
= mmap((void *)addr
, size
, PROT_NONE
,
2752 MAP_ANONYMOUS
| MAP_PRIVATE
|
2753 MAP_NORESERVE
| MAP_FIXED_NOREPLACE
, -1, 0);
2756 if (p
== MAP_FAILED
) {
2757 return errno
== EEXIST
? 0 : -1;
2759 ret
= p
== (void *)addr
;
2760 if (!keep
|| !ret
) {
2767 * pgb_try_mmap_skip_brk(uintptr_t addr, uintptr_t size, uintptr_t brk)
2768 * @addr: host address
2769 * @addr_last: host last address
2772 * Like pgb_try_mmap, but additionally reserve some memory following brk.
2774 static int pgb_try_mmap_skip_brk(uintptr_t addr
, uintptr_t addr_last
,
2775 uintptr_t brk
, bool keep
)
2777 uintptr_t brk_last
= brk
+ 16 * MiB
- 1;
2779 /* Do not map anything close to the host brk. */
2780 if (addr
<= brk_last
&& brk
<= addr_last
) {
2783 return pgb_try_mmap(addr
, addr_last
, keep
);
2788 * @ga: set of guest addrs
2792 * Return true if all @ga can be mapped by the host at @base.
2793 * On success, retain the mapping at index 0 for reserved_va.
2796 typedef struct PGBAddrs
{
2797 uintptr_t bounds
[3][2]; /* start/last pairs */
2801 static bool pgb_try_mmap_set(const PGBAddrs
*ga
, uintptr_t base
, uintptr_t brk
)
2803 for (int i
= ga
->nbounds
- 1; i
>= 0; --i
) {
2804 if (pgb_try_mmap_skip_brk(ga
->bounds
[i
][0] + base
,
2805 ga
->bounds
[i
][1] + base
,
2806 brk
, i
== 0 && reserved_va
) <= 0) {
2815 * @ga: output set of guest addrs
2816 * @guest_loaddr: guest image low address
2817 * @guest_loaddr: guest image high address
2818 * @identity: create for identity mapping
2820 * Fill in @ga with the image, COMMPAGE and NULL page.
2822 static bool pgb_addr_set(PGBAddrs
*ga
, abi_ulong guest_loaddr
,
2823 abi_ulong guest_hiaddr
, bool try_identity
)
2828 * With a low commpage, or a guest mapped very low,
2829 * we may not be able to use the identity map.
2832 if (LO_COMMPAGE
!= -1 && LO_COMMPAGE
< mmap_min_addr
) {
2835 if (guest_loaddr
!= 0 && guest_loaddr
< mmap_min_addr
) {
2840 memset(ga
, 0, sizeof(*ga
));
2844 ga
->bounds
[n
][0] = try_identity
? mmap_min_addr
: 0;
2845 ga
->bounds
[n
][1] = reserved_va
;
2847 /* LO_COMMPAGE and NULL handled by reserving from 0. */
2849 /* Add any LO_COMMPAGE or NULL page. */
2850 if (LO_COMMPAGE
!= -1) {
2851 ga
->bounds
[n
][0] = 0;
2852 ga
->bounds
[n
][1] = LO_COMMPAGE
+ TARGET_PAGE_SIZE
- 1;
2854 } else if (!try_identity
) {
2855 ga
->bounds
[n
][0] = 0;
2856 ga
->bounds
[n
][1] = TARGET_PAGE_SIZE
- 1;
2860 /* Add the guest image for ET_EXEC. */
2862 ga
->bounds
[n
][0] = guest_loaddr
;
2863 ga
->bounds
[n
][1] = guest_hiaddr
;
2869 * Temporarily disable
2870 * "comparison is always false due to limited range of data type"
2871 * due to comparison between unsigned and (possible) 0.
2873 #pragma GCC diagnostic push
2874 #pragma GCC diagnostic ignored "-Wtype-limits"
2876 /* Add any HI_COMMPAGE not covered by reserved_va. */
2877 if (reserved_va
< HI_COMMPAGE
) {
2878 ga
->bounds
[n
][0] = HI_COMMPAGE
& qemu_host_page_mask
;
2879 ga
->bounds
[n
][1] = HI_COMMPAGE
+ TARGET_PAGE_SIZE
- 1;
2883 #pragma GCC diagnostic pop
2889 static void pgb_fail_in_use(const char *image_name
)
2891 error_report("%s: requires virtual address space that is in use "
2892 "(omit the -B option or choose a different value)",
2897 static void pgb_fixed(const char *image_name
, uintptr_t guest_loaddr
,
2898 uintptr_t guest_hiaddr
, uintptr_t align
)
2901 uintptr_t brk
= (uintptr_t)sbrk(0);
2903 if (!QEMU_IS_ALIGNED(guest_base
, align
)) {
2904 fprintf(stderr
, "Requested guest base %p does not satisfy "
2905 "host minimum alignment (0x%" PRIxPTR
")\n",
2906 (void *)guest_base
, align
);
2910 if (!pgb_addr_set(&ga
, guest_loaddr
, guest_hiaddr
, !guest_base
)
2911 || !pgb_try_mmap_set(&ga
, guest_base
, brk
)) {
2912 pgb_fail_in_use(image_name
);
2917 * pgb_find_fallback:
2919 * This is a fallback method for finding holes in the host address space
2920 * if we don't have the benefit of being able to access /proc/self/map.
2921 * It can potentially take a very long time as we can only dumbly iterate
2922 * up the host address space seeing if the allocation would work.
2924 static uintptr_t pgb_find_fallback(const PGBAddrs
*ga
, uintptr_t align
,
2927 /* TODO: come up with a better estimate of how much to skip. */
2928 uintptr_t skip
= sizeof(uintptr_t) == 4 ? MiB
: GiB
;
2930 for (uintptr_t base
= skip
; ; base
+= skip
) {
2931 base
= ROUND_UP(base
, align
);
2932 if (pgb_try_mmap_set(ga
, base
, brk
)) {
2935 if (base
>= -skip
) {
2941 static uintptr_t pgb_try_itree(const PGBAddrs
*ga
, uintptr_t base
,
2942 IntervalTreeRoot
*root
)
2944 for (int i
= ga
->nbounds
- 1; i
>= 0; --i
) {
2945 uintptr_t s
= base
+ ga
->bounds
[i
][0];
2946 uintptr_t l
= base
+ ga
->bounds
[i
][1];
2947 IntervalTreeNode
*n
;
2950 /* Wraparound. Skip to advance S to mmap_min_addr. */
2951 return mmap_min_addr
- s
;
2954 n
= interval_tree_iter_first(root
, s
, l
);
2956 /* Conflict. Skip to advance S to LAST + 1. */
2957 return n
->last
- s
+ 1;
2960 return 0; /* success */
2963 static uintptr_t pgb_find_itree(const PGBAddrs
*ga
, IntervalTreeRoot
*root
,
2964 uintptr_t align
, uintptr_t brk
)
2966 uintptr_t last
= mmap_min_addr
;
2967 uintptr_t base
, skip
;
2970 base
= ROUND_UP(last
, align
);
2975 skip
= pgb_try_itree(ga
, base
, root
);
2987 * We've chosen 'base' based on holes in the interval tree,
2988 * but we don't yet know if it is a valid host address.
2989 * Because it is the first matching hole, if the host addresses
2990 * are invalid we know there are no further matches.
2992 return pgb_try_mmap_set(ga
, base
, brk
) ? base
: -1;
2995 static void pgb_dynamic(const char *image_name
, uintptr_t guest_loaddr
,
2996 uintptr_t guest_hiaddr
, uintptr_t align
)
2998 IntervalTreeRoot
*root
;
3002 assert(QEMU_IS_ALIGNED(guest_loaddr
, align
));
3004 /* Try the identity map first. */
3005 if (pgb_addr_set(&ga
, guest_loaddr
, guest_hiaddr
, true)) {
3006 brk
= (uintptr_t)sbrk(0);
3007 if (pgb_try_mmap_set(&ga
, 0, brk
)) {
3014 * Rebuild the address set for non-identity map.
3015 * This differs in the mapping of the guest NULL page.
3017 pgb_addr_set(&ga
, guest_loaddr
, guest_hiaddr
, false);
3019 root
= read_self_maps();
3021 /* Read brk after we've read the maps, which will malloc. */
3022 brk
= (uintptr_t)sbrk(0);
3025 ret
= pgb_find_fallback(&ga
, align
, brk
);
3028 * Reserve the area close to the host brk.
3029 * This will be freed with the rest of the tree.
3031 IntervalTreeNode
*b
= g_new0(IntervalTreeNode
, 1);
3033 b
->last
= brk
+ 16 * MiB
- 1;
3034 interval_tree_insert(b
, root
);
3036 ret
= pgb_find_itree(&ga
, root
, align
, brk
);
3037 free_self_maps(root
);
3041 int w
= TARGET_LONG_BITS
/ 4;
3043 error_report("%s: Unable to find a guest_base to satisfy all "
3044 "guest address mapping requirements", image_name
);
3046 for (int i
= 0; i
< ga
.nbounds
; ++i
) {
3047 error_printf(" %0*" PRIx64
"-%0*" PRIx64
"\n",
3048 w
, (uint64_t)ga
.bounds
[i
][0],
3049 w
, (uint64_t)ga
.bounds
[i
][1]);
3056 void probe_guest_base(const char *image_name
, abi_ulong guest_loaddr
,
3057 abi_ulong guest_hiaddr
)
3059 /* In order to use host shmat, we must be able to honor SHMLBA. */
3060 uintptr_t align
= MAX(SHMLBA
, qemu_host_page_size
);
3062 /* Sanity check the guest binary. */
3064 if (guest_hiaddr
> reserved_va
) {
3065 error_report("%s: requires more than reserved virtual "
3066 "address space (0x%" PRIx64
" > 0x%lx)",
3067 image_name
, (uint64_t)guest_hiaddr
, reserved_va
);
3071 if (guest_hiaddr
!= (uintptr_t)guest_hiaddr
) {
3072 error_report("%s: requires more virtual address space "
3073 "than the host can provide (0x%" PRIx64
")",
3074 image_name
, (uint64_t)guest_hiaddr
+ 1);
3079 if (have_guest_base
) {
3080 pgb_fixed(image_name
, guest_loaddr
, guest_hiaddr
, align
);
3082 pgb_dynamic(image_name
, guest_loaddr
, guest_hiaddr
, align
);
3085 /* Reserve and initialize the commpage. */
3086 if (!init_guest_commpage()) {
3087 /* We have already probed for the commpage being free. */
3088 g_assert_not_reached();
3091 assert(QEMU_IS_ALIGNED(guest_base
, align
));
3092 qemu_log_mask(CPU_LOG_PAGE
, "Locating guest address space "
3093 "@ 0x%" PRIx64
"\n", (uint64_t)guest_base
);
3097 /* The string "GNU\0" as a magic number. */
3098 GNU0_MAGIC
= const_le32('G' | 'N' << 8 | 'U' << 16),
3099 NOTE_DATA_SZ
= 1 * KiB
,
3101 ELF_GNU_PROPERTY_ALIGN
= ELF_CLASS
== ELFCLASS32
? 4 : 8,
3105 * Process a single gnu_property entry.
3106 * Return false for error.
3108 static bool parse_elf_property(const uint32_t *data
, int *off
, int datasz
,
3109 struct image_info
*info
, bool have_prev_type
,
3110 uint32_t *prev_type
, Error
**errp
)
3112 uint32_t pr_type
, pr_datasz
, step
;
3114 if (*off
> datasz
|| !QEMU_IS_ALIGNED(*off
, ELF_GNU_PROPERTY_ALIGN
)) {
3118 data
+= *off
/ sizeof(uint32_t);
3120 if (datasz
< 2 * sizeof(uint32_t)) {
3124 pr_datasz
= data
[1];
3126 datasz
-= 2 * sizeof(uint32_t);
3127 step
= ROUND_UP(pr_datasz
, ELF_GNU_PROPERTY_ALIGN
);
3128 if (step
> datasz
) {
3132 /* Properties are supposed to be unique and sorted on pr_type. */
3133 if (have_prev_type
&& pr_type
<= *prev_type
) {
3134 if (pr_type
== *prev_type
) {
3135 error_setg(errp
, "Duplicate property in PT_GNU_PROPERTY");
3137 error_setg(errp
, "Unsorted property in PT_GNU_PROPERTY");
3141 *prev_type
= pr_type
;
3143 if (!arch_parse_elf_property(pr_type
, pr_datasz
, data
, info
, errp
)) {
3147 *off
+= 2 * sizeof(uint32_t) + step
;
3151 error_setg(errp
, "Ill-formed property in PT_GNU_PROPERTY");
3155 /* Process NT_GNU_PROPERTY_TYPE_0. */
3156 static bool parse_elf_properties(const ImageSource
*src
,
3157 struct image_info
*info
,
3158 const struct elf_phdr
*phdr
,
3162 struct elf_note nhdr
;
3163 uint32_t data
[NOTE_DATA_SZ
/ sizeof(uint32_t)];
3167 bool have_prev_type
;
3170 /* Unless the arch requires properties, ignore them. */
3171 if (!ARCH_USE_GNU_PROPERTY
) {
3175 /* If the properties are crazy large, that's too bad. */
3177 if (n
> sizeof(note
)) {
3178 error_setg(errp
, "PT_GNU_PROPERTY too large");
3181 if (n
< sizeof(note
.nhdr
)) {
3182 error_setg(errp
, "PT_GNU_PROPERTY too small");
3186 if (!imgsrc_read(¬e
, phdr
->p_offset
, n
, src
, errp
)) {
3191 * The contents of a valid PT_GNU_PROPERTY is a sequence
3192 * of uint32_t -- swap them all now.
3195 for (int i
= 0; i
< n
/ 4; i
++) {
3196 bswap32s(note
.data
+ i
);
3201 * Note that nhdr is 3 words, and that the "name" described by namesz
3202 * immediately follows nhdr and is thus at the 4th word. Further, all
3203 * of the inputs to the kernel's round_up are multiples of 4.
3205 if (note
.nhdr
.n_type
!= NT_GNU_PROPERTY_TYPE_0
||
3206 note
.nhdr
.n_namesz
!= NOTE_NAME_SZ
||
3207 note
.data
[3] != GNU0_MAGIC
) {
3208 error_setg(errp
, "Invalid note in PT_GNU_PROPERTY");
3211 off
= sizeof(note
.nhdr
) + NOTE_NAME_SZ
;
3213 datasz
= note
.nhdr
.n_descsz
+ off
;
3215 error_setg(errp
, "Invalid note size in PT_GNU_PROPERTY");
3219 have_prev_type
= false;
3222 if (off
== datasz
) {
3223 return true; /* end, exit ok */
3225 if (!parse_elf_property(note
.data
, &off
, datasz
, info
,
3226 have_prev_type
, &prev_type
, errp
)) {
3229 have_prev_type
= true;
3234 * load_elf_image: Load an ELF image into the address space.
3235 * @image_name: the filename of the image, to use in error messages.
3236 * @src: the ImageSource from which to read.
3237 * @info: info collected from the loaded image.
3238 * @ehdr: the ELF header, not yet bswapped.
3239 * @pinterp_name: record any PT_INTERP string found.
3241 * On return: @info values will be filled in, as necessary or available.
3244 static void load_elf_image(const char *image_name
, const ImageSource
*src
,
3245 struct image_info
*info
, struct elfhdr
*ehdr
,
3246 char **pinterp_name
)
3248 g_autofree
struct elf_phdr
*phdr
= NULL
;
3249 abi_ulong load_addr
, load_bias
, loaddr
, hiaddr
, error
;
3254 * First of all, some simple consistency checks.
3255 * Note that we rely on the bswapped ehdr staying in bprm_buf,
3256 * for later use by load_elf_binary and create_elf_tables.
3258 if (!imgsrc_read(ehdr
, 0, sizeof(*ehdr
), src
, &err
)) {
3261 if (!elf_check_ident(ehdr
)) {
3262 error_setg(&err
, "Invalid ELF image for this architecture");
3266 if (!elf_check_ehdr(ehdr
)) {
3267 error_setg(&err
, "Invalid ELF image for this architecture");
3271 phdr
= imgsrc_read_alloc(ehdr
->e_phoff
,
3272 ehdr
->e_phnum
* sizeof(struct elf_phdr
),
3277 bswap_phdr(phdr
, ehdr
->e_phnum
);
3280 info
->pt_dynamic_addr
= 0;
3285 * Find the maximum size of the image and allocate an appropriate
3286 * amount of memory to handle that. Locate the interpreter, if any.
3288 loaddr
= -1, hiaddr
= 0;
3289 info
->alignment
= 0;
3290 info
->exec_stack
= EXSTACK_DEFAULT
;
3291 for (i
= 0; i
< ehdr
->e_phnum
; ++i
) {
3292 struct elf_phdr
*eppnt
= phdr
+ i
;
3293 if (eppnt
->p_type
== PT_LOAD
) {
3294 abi_ulong a
= eppnt
->p_vaddr
- eppnt
->p_offset
;
3298 a
= eppnt
->p_vaddr
+ eppnt
->p_memsz
- 1;
3303 info
->alignment
|= eppnt
->p_align
;
3304 } else if (eppnt
->p_type
== PT_INTERP
&& pinterp_name
) {
3305 g_autofree
char *interp_name
= NULL
;
3307 if (*pinterp_name
) {
3308 error_setg(&err
, "Multiple PT_INTERP entries");
3312 interp_name
= imgsrc_read_alloc(eppnt
->p_offset
, eppnt
->p_filesz
,
3314 if (interp_name
== NULL
) {
3317 if (interp_name
[eppnt
->p_filesz
- 1] != 0) {
3318 error_setg(&err
, "Invalid PT_INTERP entry");
3321 *pinterp_name
= g_steal_pointer(&interp_name
);
3322 } else if (eppnt
->p_type
== PT_GNU_PROPERTY
) {
3323 if (!parse_elf_properties(src
, info
, eppnt
, &err
)) {
3326 } else if (eppnt
->p_type
== PT_GNU_STACK
) {
3327 info
->exec_stack
= eppnt
->p_flags
& PF_X
;
3333 if (pinterp_name
!= NULL
) {
3334 if (ehdr
->e_type
== ET_EXEC
) {
3336 * Make sure that the low address does not conflict with
3337 * MMAP_MIN_ADDR or the QEMU application itself.
3339 probe_guest_base(image_name
, loaddr
, hiaddr
);
3344 * The binary is dynamic, but we still need to
3345 * select guest_base. In this case we pass a size.
3347 probe_guest_base(image_name
, 0, hiaddr
- loaddr
);
3350 * Avoid collision with the loader by providing a different
3351 * default load address.
3353 load_addr
+= elf_et_dyn_base
;
3356 * TODO: Better support for mmap alignment is desirable.
3357 * Since we do not have complete control over the guest
3358 * address space, we prefer the kernel to choose some address
3359 * rather than force the use of LOAD_ADDR via MAP_FIXED.
3360 * But without MAP_FIXED we cannot guarantee alignment,
3363 align
= pow2ceil(info
->alignment
);
3365 load_addr
&= -align
;
3371 * Reserve address space for all of this.
3373 * In the case of ET_EXEC, we supply MAP_FIXED_NOREPLACE so that we get
3374 * exactly the address range that is required. Without reserved_va,
3375 * the guest address space is not isolated. We have attempted to avoid
3376 * conflict with the host program itself via probe_guest_base, but using
3377 * MAP_FIXED_NOREPLACE instead of MAP_FIXED provides an extra check.
3379 * Otherwise this is ET_DYN, and we are searching for a location
3380 * that can hold the memory space required. If the image is
3381 * pre-linked, LOAD_ADDR will be non-zero, and the kernel should
3382 * honor that address if it happens to be free.
3384 * In both cases, we will overwrite pages in this range with mappings
3385 * from the executable.
3387 load_addr
= target_mmap(load_addr
, (size_t)hiaddr
- loaddr
+ 1, PROT_NONE
,
3388 MAP_PRIVATE
| MAP_ANON
| MAP_NORESERVE
|
3389 (ehdr
->e_type
== ET_EXEC
? MAP_FIXED_NOREPLACE
: 0),
3391 if (load_addr
== -1) {
3394 load_bias
= load_addr
- loaddr
;
3396 if (elf_is_fdpic(ehdr
)) {
3397 struct elf32_fdpic_loadseg
*loadsegs
= info
->loadsegs
=
3398 g_malloc(sizeof(*loadsegs
) * info
->nsegs
);
3400 for (i
= 0; i
< ehdr
->e_phnum
; ++i
) {
3401 switch (phdr
[i
].p_type
) {
3403 info
->pt_dynamic_addr
= phdr
[i
].p_vaddr
+ load_bias
;
3406 loadsegs
->addr
= phdr
[i
].p_vaddr
+ load_bias
;
3407 loadsegs
->p_vaddr
= phdr
[i
].p_vaddr
;
3408 loadsegs
->p_memsz
= phdr
[i
].p_memsz
;
3415 info
->load_bias
= load_bias
;
3416 info
->code_offset
= load_bias
;
3417 info
->data_offset
= load_bias
;
3418 info
->load_addr
= load_addr
;
3419 info
->entry
= ehdr
->e_entry
+ load_bias
;
3420 info
->start_code
= -1;
3422 info
->start_data
= -1;
3424 /* Usual start for brk is after all sections of the main executable. */
3425 info
->brk
= TARGET_PAGE_ALIGN(hiaddr
+ load_bias
);
3426 info
->elf_flags
= ehdr
->e_flags
;
3428 prot_exec
= PROT_EXEC
;
3429 #ifdef TARGET_AARCH64
3431 * If the BTI feature is present, this indicates that the executable
3432 * pages of the startup binary should be mapped with PROT_BTI, so that
3433 * branch targets are enforced.
3435 * The startup binary is either the interpreter or the static executable.
3436 * The interpreter is responsible for all pages of a dynamic executable.
3438 * Elf notes are backward compatible to older cpus.
3439 * Do not enable BTI unless it is supported.
3441 if ((info
->note_flags
& GNU_PROPERTY_AARCH64_FEATURE_1_BTI
)
3442 && (pinterp_name
== NULL
|| *pinterp_name
== 0)
3443 && cpu_isar_feature(aa64_bti
, ARM_CPU(thread_cpu
))) {
3444 prot_exec
|= TARGET_PROT_BTI
;
3448 for (i
= 0; i
< ehdr
->e_phnum
; i
++) {
3449 struct elf_phdr
*eppnt
= phdr
+ i
;
3450 if (eppnt
->p_type
== PT_LOAD
) {
3451 abi_ulong vaddr
, vaddr_po
, vaddr_ps
, vaddr_ef
, vaddr_em
;
3454 if (eppnt
->p_flags
& PF_R
) {
3455 elf_prot
|= PROT_READ
;
3457 if (eppnt
->p_flags
& PF_W
) {
3458 elf_prot
|= PROT_WRITE
;
3460 if (eppnt
->p_flags
& PF_X
) {
3461 elf_prot
|= prot_exec
;
3464 vaddr
= load_bias
+ eppnt
->p_vaddr
;
3465 vaddr_po
= vaddr
& ~TARGET_PAGE_MASK
;
3466 vaddr_ps
= vaddr
& TARGET_PAGE_MASK
;
3468 vaddr_ef
= vaddr
+ eppnt
->p_filesz
;
3469 vaddr_em
= vaddr
+ eppnt
->p_memsz
;
3472 * Some segments may be completely empty, with a non-zero p_memsz
3473 * but no backing file segment.
3475 if (eppnt
->p_filesz
!= 0) {
3476 error
= imgsrc_mmap(vaddr_ps
, eppnt
->p_filesz
+ vaddr_po
,
3477 elf_prot
, MAP_PRIVATE
| MAP_FIXED
,
3478 src
, eppnt
->p_offset
- vaddr_po
);
3484 /* If the load segment requests extra zeros (e.g. bss), map it. */
3485 if (vaddr_ef
< vaddr_em
&&
3486 !zero_bss(vaddr_ef
, vaddr_em
, elf_prot
, &err
)) {
3490 /* Find the full program boundaries. */
3491 if (elf_prot
& PROT_EXEC
) {
3492 if (vaddr
< info
->start_code
) {
3493 info
->start_code
= vaddr
;
3495 if (vaddr_ef
> info
->end_code
) {
3496 info
->end_code
= vaddr_ef
;
3499 if (elf_prot
& PROT_WRITE
) {
3500 if (vaddr
< info
->start_data
) {
3501 info
->start_data
= vaddr
;
3503 if (vaddr_ef
> info
->end_data
) {
3504 info
->end_data
= vaddr_ef
;
3508 } else if (eppnt
->p_type
== PT_MIPS_ABIFLAGS
) {
3509 Mips_elf_abiflags_v0 abiflags
;
3511 if (!imgsrc_read(&abiflags
, eppnt
->p_offset
, sizeof(abiflags
),
3515 bswap_mips_abiflags(&abiflags
);
3516 info
->fp_abi
= abiflags
.fp_abi
;
3521 if (info
->end_data
== 0) {
3522 info
->start_data
= info
->end_code
;
3523 info
->end_data
= info
->end_code
;
3526 if (qemu_log_enabled()) {
3527 load_symbols(ehdr
, src
, load_bias
);
3530 debuginfo_report_elf(image_name
, src
->fd
, load_bias
);
3538 error_setg_errno(&err
, errno
, "Error mapping file");
3541 error_reportf_err(err
, "%s: ", image_name
);
3545 static void load_elf_interp(const char *filename
, struct image_info
*info
,
3546 char bprm_buf
[BPRM_BUF_SIZE
])
3553 fd
= open(path(filename
), O_RDONLY
);
3555 error_setg_file_open(&err
, errno
, filename
);
3556 error_report_err(err
);
3560 retval
= read(fd
, bprm_buf
, BPRM_BUF_SIZE
);
3562 error_setg_errno(&err
, errno
, "Error reading file header");
3563 error_reportf_err(err
, "%s: ", filename
);
3568 src
.cache
= bprm_buf
;
3569 src
.cache_size
= retval
;
3571 load_elf_image(filename
, &src
, info
, &ehdr
, NULL
);
3575 #include VDSO_HEADER
3576 #define vdso_image_info() &vdso_image_info
3578 #define vdso_image_info() NULL
3581 static void load_elf_vdso(struct image_info
*info
, const VdsoImageInfo
*vdso
)
3585 abi_ulong load_bias
, load_addr
;
3588 src
.cache
= vdso
->image
;
3589 src
.cache_size
= vdso
->image_size
;
3591 load_elf_image("<internal-vdso>", &src
, info
, &ehdr
, NULL
);
3592 load_addr
= info
->load_addr
;
3593 load_bias
= info
->load_bias
;
3596 * We need to relocate the VDSO image. The one built into the kernel
3597 * is built for a fixed address. The one built for QEMU is not, since
3598 * that requires close control of the guest address space.
3599 * We pre-processed the image to locate all of the addresses that need
3602 for (unsigned i
= 0, n
= vdso
->reloc_count
; i
< n
; i
++) {
3603 abi_ulong
*addr
= g2h_untagged(load_addr
+ vdso
->relocs
[i
]);
3604 *addr
= tswapal(tswapal(*addr
) + load_bias
);
3607 /* Install signal trampolines, if present. */
3608 if (vdso
->sigreturn_ofs
) {
3609 default_sigreturn
= load_addr
+ vdso
->sigreturn_ofs
;
3611 if (vdso
->rt_sigreturn_ofs
) {
3612 default_rt_sigreturn
= load_addr
+ vdso
->rt_sigreturn_ofs
;
3615 /* Remove write from VDSO segment. */
3616 target_mprotect(info
->start_data
, info
->end_data
- info
->start_data
,
3617 PROT_READ
| PROT_EXEC
);
3620 static int symfind(const void *s0
, const void *s1
)
3622 struct elf_sym
*sym
= (struct elf_sym
*)s1
;
3623 __typeof(sym
->st_value
) addr
= *(uint64_t *)s0
;
3626 if (addr
< sym
->st_value
) {
3628 } else if (addr
>= sym
->st_value
+ sym
->st_size
) {
3634 static const char *lookup_symbolxx(struct syminfo
*s
, uint64_t orig_addr
)
3636 #if ELF_CLASS == ELFCLASS32
3637 struct elf_sym
*syms
= s
->disas_symtab
.elf32
;
3639 struct elf_sym
*syms
= s
->disas_symtab
.elf64
;
3643 struct elf_sym
*sym
;
3645 sym
= bsearch(&orig_addr
, syms
, s
->disas_num_syms
, sizeof(*syms
), symfind
);
3647 return s
->disas_strtab
+ sym
->st_name
;
3653 /* FIXME: This should use elf_ops.h */
3654 static int symcmp(const void *s0
, const void *s1
)
3656 struct elf_sym
*sym0
= (struct elf_sym
*)s0
;
3657 struct elf_sym
*sym1
= (struct elf_sym
*)s1
;
3658 return (sym0
->st_value
< sym1
->st_value
)
3660 : ((sym0
->st_value
> sym1
->st_value
) ? 1 : 0);
3663 /* Best attempt to load symbols from this ELF object. */
3664 static void load_symbols(struct elfhdr
*hdr
, const ImageSource
*src
,
3665 abi_ulong load_bias
)
3667 int i
, shnum
, nsyms
, sym_idx
= 0, str_idx
= 0;
3668 g_autofree
struct elf_shdr
*shdr
= NULL
;
3669 char *strings
= NULL
;
3670 struct elf_sym
*syms
= NULL
;
3671 struct elf_sym
*new_syms
;
3674 shnum
= hdr
->e_shnum
;
3675 shdr
= imgsrc_read_alloc(hdr
->e_shoff
, shnum
* sizeof(struct elf_shdr
),
3681 bswap_shdr(shdr
, shnum
);
3682 for (i
= 0; i
< shnum
; ++i
) {
3683 if (shdr
[i
].sh_type
== SHT_SYMTAB
) {
3685 str_idx
= shdr
[i
].sh_link
;
3690 /* There will be no symbol table if the file was stripped. */
3694 /* Now know where the strtab and symtab are. Snarf them. */
3696 segsz
= shdr
[str_idx
].sh_size
;
3697 strings
= g_try_malloc(segsz
);
3701 if (!imgsrc_read(strings
, shdr
[str_idx
].sh_offset
, segsz
, src
, NULL
)) {
3705 segsz
= shdr
[sym_idx
].sh_size
;
3706 if (segsz
/ sizeof(struct elf_sym
) > INT_MAX
) {
3708 * Implausibly large symbol table: give up rather than ploughing
3709 * on with the number of symbols calculation overflowing.
3713 nsyms
= segsz
/ sizeof(struct elf_sym
);
3714 syms
= g_try_malloc(segsz
);
3718 if (!imgsrc_read(syms
, shdr
[sym_idx
].sh_offset
, segsz
, src
, NULL
)) {
3722 for (i
= 0; i
< nsyms
; ) {
3723 bswap_sym(syms
+ i
);
3724 /* Throw away entries which we do not need. */
3725 if (syms
[i
].st_shndx
== SHN_UNDEF
3726 || syms
[i
].st_shndx
>= SHN_LORESERVE
3727 || ELF_ST_TYPE(syms
[i
].st_info
) != STT_FUNC
) {
3729 syms
[i
] = syms
[nsyms
];
3732 #if defined(TARGET_ARM) || defined (TARGET_MIPS)
3733 /* The bottom address bit marks a Thumb or MIPS16 symbol. */
3734 syms
[i
].st_value
&= ~(target_ulong
)1;
3736 syms
[i
].st_value
+= load_bias
;
3741 /* No "useful" symbol. */
3747 * Attempt to free the storage associated with the local symbols
3748 * that we threw away. Whether or not this has any effect on the
3749 * memory allocation depends on the malloc implementation and how
3750 * many symbols we managed to discard.
3752 new_syms
= g_try_renew(struct elf_sym
, syms
, nsyms
);
3753 if (new_syms
== NULL
) {
3758 qsort(syms
, nsyms
, sizeof(*syms
), symcmp
);
3761 struct syminfo
*s
= g_new(struct syminfo
, 1);
3763 s
->disas_strtab
= strings
;
3764 s
->disas_num_syms
= nsyms
;
3765 #if ELF_CLASS == ELFCLASS32
3766 s
->disas_symtab
.elf32
= syms
;
3768 s
->disas_symtab
.elf64
= syms
;
3770 s
->lookup_symbol
= lookup_symbolxx
;
3781 uint32_t get_elf_eflags(int fd
)
3787 /* Read ELF header */
3788 offset
= lseek(fd
, 0, SEEK_SET
);
3789 if (offset
== (off_t
) -1) {
3792 ret
= read(fd
, &ehdr
, sizeof(ehdr
));
3793 if (ret
< sizeof(ehdr
)) {
3796 offset
= lseek(fd
, offset
, SEEK_SET
);
3797 if (offset
== (off_t
) -1) {
3801 /* Check ELF signature */
3802 if (!elf_check_ident(&ehdr
)) {
3808 if (!elf_check_ehdr(&ehdr
)) {
3812 /* return architecture id */
3813 return ehdr
.e_flags
;
3816 int load_elf_binary(struct linux_binprm
*bprm
, struct image_info
*info
)
3819 * We need a copy of the elf header for passing to create_elf_tables.
3820 * We will have overwritten the original when we re-use bprm->buf
3821 * while loading the interpreter. Allocate the storage for this now
3822 * and let elf_load_image do any swapping that may be required.
3825 struct image_info interp_info
, vdso_info
;
3826 char *elf_interpreter
= NULL
;
3829 memset(&interp_info
, 0, sizeof(interp_info
));
3831 interp_info
.fp_abi
= MIPS_ABI_FP_UNKNOWN
;
3834 load_elf_image(bprm
->filename
, &bprm
->src
, info
, &ehdr
, &elf_interpreter
);
3836 /* Do this so that we can load the interpreter, if need be. We will
3837 change some of these later */
3838 bprm
->p
= setup_arg_pages(bprm
, info
);
3840 scratch
= g_new0(char, TARGET_PAGE_SIZE
);
3841 if (STACK_GROWS_DOWN
) {
3842 bprm
->p
= copy_elf_strings(1, &bprm
->filename
, scratch
,
3843 bprm
->p
, info
->stack_limit
);
3844 info
->file_string
= bprm
->p
;
3845 bprm
->p
= copy_elf_strings(bprm
->envc
, bprm
->envp
, scratch
,
3846 bprm
->p
, info
->stack_limit
);
3847 info
->env_strings
= bprm
->p
;
3848 bprm
->p
= copy_elf_strings(bprm
->argc
, bprm
->argv
, scratch
,
3849 bprm
->p
, info
->stack_limit
);
3850 info
->arg_strings
= bprm
->p
;
3852 info
->arg_strings
= bprm
->p
;
3853 bprm
->p
= copy_elf_strings(bprm
->argc
, bprm
->argv
, scratch
,
3854 bprm
->p
, info
->stack_limit
);
3855 info
->env_strings
= bprm
->p
;
3856 bprm
->p
= copy_elf_strings(bprm
->envc
, bprm
->envp
, scratch
,
3857 bprm
->p
, info
->stack_limit
);
3858 info
->file_string
= bprm
->p
;
3859 bprm
->p
= copy_elf_strings(1, &bprm
->filename
, scratch
,
3860 bprm
->p
, info
->stack_limit
);
3866 fprintf(stderr
, "%s: %s\n", bprm
->filename
, strerror(E2BIG
));
3870 if (elf_interpreter
) {
3871 load_elf_interp(elf_interpreter
, &interp_info
, bprm
->buf
);
3874 * While unusual because of ELF_ET_DYN_BASE, if we are unlucky
3875 * with the mappings the interpreter can be loaded above but
3876 * near the main executable, which can leave very little room
3878 * If the current brk has less than 16MB, use the end of the
3881 if (interp_info
.brk
> info
->brk
&&
3882 interp_info
.load_bias
- info
->brk
< 16 * MiB
) {
3883 info
->brk
= interp_info
.brk
;
3886 /* If the program interpreter is one of these two, then assume
3887 an iBCS2 image. Otherwise assume a native linux image. */
3889 if (strcmp(elf_interpreter
, "/usr/lib/libc.so.1") == 0
3890 || strcmp(elf_interpreter
, "/usr/lib/ld.so.1") == 0) {
3891 info
->personality
= PER_SVR4
;
3893 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
3894 and some applications "depend" upon this behavior. Since
3895 we do not have the power to recompile these, we emulate
3896 the SVr4 behavior. Sigh. */
3897 target_mmap(0, qemu_host_page_size
, PROT_READ
| PROT_EXEC
,
3898 MAP_FIXED
| MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
3901 info
->interp_fp_abi
= interp_info
.fp_abi
;
3906 * Load a vdso if available, which will amongst other things contain the
3907 * signal trampolines. Otherwise, allocate a separate page for them.
3909 const VdsoImageInfo
*vdso
= vdso_image_info();
3911 load_elf_vdso(&vdso_info
, vdso
);
3912 } else if (TARGET_ARCH_HAS_SIGTRAMP_PAGE
) {
3913 abi_long tramp_page
= target_mmap(0, TARGET_PAGE_SIZE
,
3914 PROT_READ
| PROT_WRITE
,
3915 MAP_PRIVATE
| MAP_ANON
, -1, 0);
3916 if (tramp_page
== -1) {
3920 setup_sigtramp(tramp_page
);
3921 target_mprotect(tramp_page
, TARGET_PAGE_SIZE
, PROT_READ
| PROT_EXEC
);
3924 bprm
->p
= create_elf_tables(bprm
->p
, bprm
->argc
, bprm
->envc
, &ehdr
, info
,
3925 elf_interpreter
? &interp_info
: NULL
,
3926 vdso
? &vdso_info
: NULL
);
3927 info
->start_stack
= bprm
->p
;
3929 /* If we have an interpreter, set that as the program's entry point.
3930 Copy the load_bias as well, to help PPC64 interpret the entry
3931 point as a function descriptor. Do this after creating elf tables
3932 so that we copy the original program entry point into the AUXV. */
3933 if (elf_interpreter
) {
3934 info
->load_bias
= interp_info
.load_bias
;
3935 info
->entry
= interp_info
.entry
;
3936 g_free(elf_interpreter
);
3939 #ifdef USE_ELF_CORE_DUMP
3940 bprm
->core_dump
= &elf_core_dump
;
3946 #ifdef USE_ELF_CORE_DUMP
3948 * Definitions to generate Intel SVR4-like core files.
3949 * These mostly have the same names as the SVR4 types with "target_elf_"
3950 * tacked on the front to prevent clashes with linux definitions,
3951 * and the typedef forms have been avoided. This is mostly like
3952 * the SVR4 structure, but more Linuxy, with things that Linux does
3953 * not support and which gdb doesn't really use excluded.
3955 * Fields we don't dump (their contents is zero) in linux-user qemu
3956 * are marked with XXX.
3958 * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
3960 * Porting ELF coredump for target is (quite) simple process. First you
3961 * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for
3962 * the target resides):
3964 * #define USE_ELF_CORE_DUMP
3966 * Next you define type of register set used for dumping. ELF specification
3967 * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
3969 * typedef <target_regtype> target_elf_greg_t;
3970 * #define ELF_NREG <number of registers>
3971 * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG];
3973 * Last step is to implement target specific function that copies registers
3974 * from given cpu into just specified register set. Prototype is:
3976 * static void elf_core_copy_regs(taret_elf_gregset_t *regs,
3977 * const CPUArchState *env);
3980 * regs - copy register values into here (allocated and zeroed by caller)
3981 * env - copy registers from here
3983 * Example for ARM target is provided in this file.
3986 /* An ELF note in memory */
3990 size_t namesz_rounded
;
3993 size_t datasz_rounded
;
3998 struct target_elf_siginfo
{
3999 abi_int si_signo
; /* signal number */
4000 abi_int si_code
; /* extra code */
4001 abi_int si_errno
; /* errno */
4004 struct target_elf_prstatus
{
4005 struct target_elf_siginfo pr_info
; /* Info associated with signal */
4006 abi_short pr_cursig
; /* Current signal */
4007 abi_ulong pr_sigpend
; /* XXX */
4008 abi_ulong pr_sighold
; /* XXX */
4009 target_pid_t pr_pid
;
4010 target_pid_t pr_ppid
;
4011 target_pid_t pr_pgrp
;
4012 target_pid_t pr_sid
;
4013 struct target_timeval pr_utime
; /* XXX User time */
4014 struct target_timeval pr_stime
; /* XXX System time */
4015 struct target_timeval pr_cutime
; /* XXX Cumulative user time */
4016 struct target_timeval pr_cstime
; /* XXX Cumulative system time */
4017 target_elf_gregset_t pr_reg
; /* GP registers */
4018 abi_int pr_fpvalid
; /* XXX */
4021 #define ELF_PRARGSZ (80) /* Number of chars for args */
4023 struct target_elf_prpsinfo
{
4024 char pr_state
; /* numeric process state */
4025 char pr_sname
; /* char for pr_state */
4026 char pr_zomb
; /* zombie */
4027 char pr_nice
; /* nice val */
4028 abi_ulong pr_flag
; /* flags */
4029 target_uid_t pr_uid
;
4030 target_gid_t pr_gid
;
4031 target_pid_t pr_pid
, pr_ppid
, pr_pgrp
, pr_sid
;
4033 char pr_fname
[16] QEMU_NONSTRING
; /* filename of executable */
4034 char pr_psargs
[ELF_PRARGSZ
]; /* initial part of arg list */
4037 /* Here is the structure in which status of each thread is captured. */
4038 struct elf_thread_status
{
4039 QTAILQ_ENTRY(elf_thread_status
) ets_link
;
4040 struct target_elf_prstatus prstatus
; /* NT_PRSTATUS */
4042 elf_fpregset_t fpu
; /* NT_PRFPREG */
4043 struct task_struct
*thread
;
4044 elf_fpxregset_t xfpu
; /* ELF_CORE_XFPREG_TYPE */
4046 struct memelfnote notes
[1];
4050 struct elf_note_info
{
4051 struct memelfnote
*notes
;
4052 struct target_elf_prstatus
*prstatus
; /* NT_PRSTATUS */
4053 struct target_elf_prpsinfo
*psinfo
; /* NT_PRPSINFO */
4055 QTAILQ_HEAD(, elf_thread_status
) thread_list
;
4058 * Current version of ELF coredump doesn't support
4059 * dumping fp regs etc.
4061 elf_fpregset_t
*fpu
;
4062 elf_fpxregset_t
*xfpu
;
4063 int thread_status_size
;
4069 struct vm_area_struct
{
4070 target_ulong vma_start
; /* start vaddr of memory region */
4071 target_ulong vma_end
; /* end vaddr of memory region */
4072 abi_ulong vma_flags
; /* protection etc. flags for the region */
4073 QTAILQ_ENTRY(vm_area_struct
) vma_link
;
4077 QTAILQ_HEAD(, vm_area_struct
) mm_mmap
;
4078 int mm_count
; /* number of mappings */
4081 static struct mm_struct
*vma_init(void);
4082 static void vma_delete(struct mm_struct
*);
4083 static int vma_add_mapping(struct mm_struct
*, target_ulong
,
4084 target_ulong
, abi_ulong
);
4085 static int vma_get_mapping_count(const struct mm_struct
*);
4086 static struct vm_area_struct
*vma_first(const struct mm_struct
*);
4087 static struct vm_area_struct
*vma_next(struct vm_area_struct
*);
4088 static abi_ulong
vma_dump_size(const struct vm_area_struct
*);
4089 static int vma_walker(void *priv
, target_ulong start
, target_ulong end
,
4090 unsigned long flags
);
4092 static void fill_elf_header(struct elfhdr
*, int, uint16_t, uint32_t);
4093 static void fill_note(struct memelfnote
*, const char *, int,
4094 unsigned int, void *);
4095 static void fill_prstatus(struct target_elf_prstatus
*, const TaskState
*, int);
4096 static int fill_psinfo(struct target_elf_prpsinfo
*, const TaskState
*);
4097 static void fill_auxv_note(struct memelfnote
*, const TaskState
*);
4098 static void fill_elf_note_phdr(struct elf_phdr
*, int, off_t
);
4099 static size_t note_size(const struct memelfnote
*);
4100 static void free_note_info(struct elf_note_info
*);
4101 static int fill_note_info(struct elf_note_info
*, long, const CPUArchState
*);
4102 static void fill_thread_info(struct elf_note_info
*, const CPUArchState
*);
4104 static int dump_write(int, const void *, size_t);
4105 static int write_note(struct memelfnote
*, int);
4106 static int write_note_info(struct elf_note_info
*, int);
4109 static void bswap_prstatus(struct target_elf_prstatus
*prstatus
)
4111 prstatus
->pr_info
.si_signo
= tswap32(prstatus
->pr_info
.si_signo
);
4112 prstatus
->pr_info
.si_code
= tswap32(prstatus
->pr_info
.si_code
);
4113 prstatus
->pr_info
.si_errno
= tswap32(prstatus
->pr_info
.si_errno
);
4114 prstatus
->pr_cursig
= tswap16(prstatus
->pr_cursig
);
4115 prstatus
->pr_sigpend
= tswapal(prstatus
->pr_sigpend
);
4116 prstatus
->pr_sighold
= tswapal(prstatus
->pr_sighold
);
4117 prstatus
->pr_pid
= tswap32(prstatus
->pr_pid
);
4118 prstatus
->pr_ppid
= tswap32(prstatus
->pr_ppid
);
4119 prstatus
->pr_pgrp
= tswap32(prstatus
->pr_pgrp
);
4120 prstatus
->pr_sid
= tswap32(prstatus
->pr_sid
);
4121 /* cpu times are not filled, so we skip them */
4122 /* regs should be in correct format already */
4123 prstatus
->pr_fpvalid
= tswap32(prstatus
->pr_fpvalid
);
4126 static void bswap_psinfo(struct target_elf_prpsinfo
*psinfo
)
4128 psinfo
->pr_flag
= tswapal(psinfo
->pr_flag
);
4129 psinfo
->pr_uid
= tswap16(psinfo
->pr_uid
);
4130 psinfo
->pr_gid
= tswap16(psinfo
->pr_gid
);
4131 psinfo
->pr_pid
= tswap32(psinfo
->pr_pid
);
4132 psinfo
->pr_ppid
= tswap32(psinfo
->pr_ppid
);
4133 psinfo
->pr_pgrp
= tswap32(psinfo
->pr_pgrp
);
4134 psinfo
->pr_sid
= tswap32(psinfo
->pr_sid
);
4137 static void bswap_note(struct elf_note
*en
)
4139 bswap32s(&en
->n_namesz
);
4140 bswap32s(&en
->n_descsz
);
4141 bswap32s(&en
->n_type
);
4144 static inline void bswap_prstatus(struct target_elf_prstatus
*p
) { }
4145 static inline void bswap_psinfo(struct target_elf_prpsinfo
*p
) {}
4146 static inline void bswap_note(struct elf_note
*en
) { }
4147 #endif /* BSWAP_NEEDED */
4150 * Minimal support for linux memory regions. These are needed
4151 * when we are finding out what memory exactly belongs to
4152 * emulated process. No locks needed here, as long as
4153 * thread that received the signal is stopped.
4156 static struct mm_struct
*vma_init(void)
4158 struct mm_struct
*mm
;
4160 if ((mm
= g_malloc(sizeof (*mm
))) == NULL
)
4164 QTAILQ_INIT(&mm
->mm_mmap
);
4169 static void vma_delete(struct mm_struct
*mm
)
4171 struct vm_area_struct
*vma
;
4173 while ((vma
= vma_first(mm
)) != NULL
) {
4174 QTAILQ_REMOVE(&mm
->mm_mmap
, vma
, vma_link
);
4180 static int vma_add_mapping(struct mm_struct
*mm
, target_ulong start
,
4181 target_ulong end
, abi_ulong flags
)
4183 struct vm_area_struct
*vma
;
4185 if ((vma
= g_malloc0(sizeof (*vma
))) == NULL
)
4188 vma
->vma_start
= start
;
4190 vma
->vma_flags
= flags
;
4192 QTAILQ_INSERT_TAIL(&mm
->mm_mmap
, vma
, vma_link
);
4198 static struct vm_area_struct
*vma_first(const struct mm_struct
*mm
)
4200 return (QTAILQ_FIRST(&mm
->mm_mmap
));
4203 static struct vm_area_struct
*vma_next(struct vm_area_struct
*vma
)
4205 return (QTAILQ_NEXT(vma
, vma_link
));
4208 static int vma_get_mapping_count(const struct mm_struct
*mm
)
4210 return (mm
->mm_count
);
4214 * Calculate file (dump) size of given memory region.
4216 static abi_ulong
vma_dump_size(const struct vm_area_struct
*vma
)
4218 /* if we cannot even read the first page, skip it */
4219 if (!access_ok_untagged(VERIFY_READ
, vma
->vma_start
, TARGET_PAGE_SIZE
))
4223 * Usually we don't dump executable pages as they contain
4224 * non-writable code that debugger can read directly from
4225 * target library etc. However, thread stacks are marked
4226 * also executable so we read in first page of given region
4227 * and check whether it contains elf header. If there is
4228 * no elf header, we dump it.
4230 if (vma
->vma_flags
& PROT_EXEC
) {
4231 char page
[TARGET_PAGE_SIZE
];
4233 if (copy_from_user(page
, vma
->vma_start
, sizeof (page
))) {
4236 if ((page
[EI_MAG0
] == ELFMAG0
) &&
4237 (page
[EI_MAG1
] == ELFMAG1
) &&
4238 (page
[EI_MAG2
] == ELFMAG2
) &&
4239 (page
[EI_MAG3
] == ELFMAG3
)) {
4241 * Mappings are possibly from ELF binary. Don't dump
4248 return (vma
->vma_end
- vma
->vma_start
);
4251 static int vma_walker(void *priv
, target_ulong start
, target_ulong end
,
4252 unsigned long flags
)
4254 struct mm_struct
*mm
= (struct mm_struct
*)priv
;
4256 vma_add_mapping(mm
, start
, end
, flags
);
4260 static void fill_note(struct memelfnote
*note
, const char *name
, int type
,
4261 unsigned int sz
, void *data
)
4263 unsigned int namesz
;
4265 namesz
= strlen(name
) + 1;
4267 note
->namesz
= namesz
;
4268 note
->namesz_rounded
= roundup(namesz
, sizeof (int32_t));
4271 note
->datasz_rounded
= roundup(sz
, sizeof (int32_t));
4276 * We calculate rounded up note size here as specified by
4279 note
->notesz
= sizeof (struct elf_note
) +
4280 note
->namesz_rounded
+ note
->datasz_rounded
;
4283 static void fill_elf_header(struct elfhdr
*elf
, int segs
, uint16_t machine
,
4286 (void) memset(elf
, 0, sizeof(*elf
));
4288 (void) memcpy(elf
->e_ident
, ELFMAG
, SELFMAG
);
4289 elf
->e_ident
[EI_CLASS
] = ELF_CLASS
;
4290 elf
->e_ident
[EI_DATA
] = ELF_DATA
;
4291 elf
->e_ident
[EI_VERSION
] = EV_CURRENT
;
4292 elf
->e_ident
[EI_OSABI
] = ELF_OSABI
;
4294 elf
->e_type
= ET_CORE
;
4295 elf
->e_machine
= machine
;
4296 elf
->e_version
= EV_CURRENT
;
4297 elf
->e_phoff
= sizeof(struct elfhdr
);
4298 elf
->e_flags
= flags
;
4299 elf
->e_ehsize
= sizeof(struct elfhdr
);
4300 elf
->e_phentsize
= sizeof(struct elf_phdr
);
4301 elf
->e_phnum
= segs
;
4306 static void fill_elf_note_phdr(struct elf_phdr
*phdr
, int sz
, off_t offset
)
4308 phdr
->p_type
= PT_NOTE
;
4309 phdr
->p_offset
= offset
;
4312 phdr
->p_filesz
= sz
;
4317 bswap_phdr(phdr
, 1);
4320 static size_t note_size(const struct memelfnote
*note
)
4322 return (note
->notesz
);
4325 static void fill_prstatus(struct target_elf_prstatus
*prstatus
,
4326 const TaskState
*ts
, int signr
)
4328 (void) memset(prstatus
, 0, sizeof (*prstatus
));
4329 prstatus
->pr_info
.si_signo
= prstatus
->pr_cursig
= signr
;
4330 prstatus
->pr_pid
= ts
->ts_tid
;
4331 prstatus
->pr_ppid
= getppid();
4332 prstatus
->pr_pgrp
= getpgrp();
4333 prstatus
->pr_sid
= getsid(0);
4335 bswap_prstatus(prstatus
);
4338 static int fill_psinfo(struct target_elf_prpsinfo
*psinfo
, const TaskState
*ts
)
4340 char *base_filename
;
4341 unsigned int i
, len
;
4343 (void) memset(psinfo
, 0, sizeof (*psinfo
));
4345 len
= ts
->info
->env_strings
- ts
->info
->arg_strings
;
4346 if (len
>= ELF_PRARGSZ
)
4347 len
= ELF_PRARGSZ
- 1;
4348 if (copy_from_user(&psinfo
->pr_psargs
, ts
->info
->arg_strings
, len
)) {
4351 for (i
= 0; i
< len
; i
++)
4352 if (psinfo
->pr_psargs
[i
] == 0)
4353 psinfo
->pr_psargs
[i
] = ' ';
4354 psinfo
->pr_psargs
[len
] = 0;
4356 psinfo
->pr_pid
= getpid();
4357 psinfo
->pr_ppid
= getppid();
4358 psinfo
->pr_pgrp
= getpgrp();
4359 psinfo
->pr_sid
= getsid(0);
4360 psinfo
->pr_uid
= getuid();
4361 psinfo
->pr_gid
= getgid();
4363 base_filename
= g_path_get_basename(ts
->bprm
->filename
);
4365 * Using strncpy here is fine: at max-length,
4366 * this field is not NUL-terminated.
4368 (void) strncpy(psinfo
->pr_fname
, base_filename
,
4369 sizeof(psinfo
->pr_fname
));
4371 g_free(base_filename
);
4372 bswap_psinfo(psinfo
);
4376 static void fill_auxv_note(struct memelfnote
*note
, const TaskState
*ts
)
4378 elf_addr_t auxv
= (elf_addr_t
)ts
->info
->saved_auxv
;
4379 elf_addr_t orig_auxv
= auxv
;
4381 int len
= ts
->info
->auxv_len
;
4384 * Auxiliary vector is stored in target process stack. It contains
4385 * {type, value} pairs that we need to dump into note. This is not
4386 * strictly necessary but we do it here for sake of completeness.
4389 /* read in whole auxv vector and copy it to memelfnote */
4390 ptr
= lock_user(VERIFY_READ
, orig_auxv
, len
, 0);
4392 fill_note(note
, "CORE", NT_AUXV
, len
, ptr
);
4393 unlock_user(ptr
, auxv
, len
);
4398 * Constructs name of coredump file. We have following convention
4400 * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
4402 * Returns the filename
4404 static char *core_dump_filename(const TaskState
*ts
)
4406 g_autoptr(GDateTime
) now
= g_date_time_new_now_local();
4407 g_autofree
char *nowstr
= g_date_time_format(now
, "%Y%m%d-%H%M%S");
4408 g_autofree
char *base_filename
= g_path_get_basename(ts
->bprm
->filename
);
4410 return g_strdup_printf("qemu_%s_%s_%d.core",
4411 base_filename
, nowstr
, (int)getpid());
4414 static int dump_write(int fd
, const void *ptr
, size_t size
)
4416 const char *bufp
= (const char *)ptr
;
4417 ssize_t bytes_written
, bytes_left
;
4418 struct rlimit dumpsize
;
4422 getrlimit(RLIMIT_CORE
, &dumpsize
);
4423 if ((pos
= lseek(fd
, 0, SEEK_CUR
))==-1) {
4424 if (errno
== ESPIPE
) { /* not a seekable stream */
4430 if (dumpsize
.rlim_cur
<= pos
) {
4432 } else if (dumpsize
.rlim_cur
== RLIM_INFINITY
) {
4435 size_t limit_left
=dumpsize
.rlim_cur
- pos
;
4436 bytes_left
= limit_left
>= size
? size
: limit_left
;
4441 * In normal conditions, single write(2) should do but
4442 * in case of socket etc. this mechanism is more portable.
4445 bytes_written
= write(fd
, bufp
, bytes_left
);
4446 if (bytes_written
< 0) {
4450 } else if (bytes_written
== 0) { /* eof */
4453 bufp
+= bytes_written
;
4454 bytes_left
-= bytes_written
;
4455 } while (bytes_left
> 0);
4460 static int write_note(struct memelfnote
*men
, int fd
)
4464 en
.n_namesz
= men
->namesz
;
4465 en
.n_type
= men
->type
;
4466 en
.n_descsz
= men
->datasz
;
4470 if (dump_write(fd
, &en
, sizeof(en
)) != 0)
4472 if (dump_write(fd
, men
->name
, men
->namesz_rounded
) != 0)
4474 if (dump_write(fd
, men
->data
, men
->datasz_rounded
) != 0)
4480 static void fill_thread_info(struct elf_note_info
*info
, const CPUArchState
*env
)
4482 CPUState
*cpu
= env_cpu((CPUArchState
*)env
);
4483 TaskState
*ts
= (TaskState
*)cpu
->opaque
;
4484 struct elf_thread_status
*ets
;
4486 ets
= g_malloc0(sizeof (*ets
));
4487 ets
->num_notes
= 1; /* only prstatus is dumped */
4488 fill_prstatus(&ets
->prstatus
, ts
, 0);
4489 elf_core_copy_regs(&ets
->prstatus
.pr_reg
, env
);
4490 fill_note(&ets
->notes
[0], "CORE", NT_PRSTATUS
, sizeof (ets
->prstatus
),
4493 QTAILQ_INSERT_TAIL(&info
->thread_list
, ets
, ets_link
);
4495 info
->notes_size
+= note_size(&ets
->notes
[0]);
4498 static void init_note_info(struct elf_note_info
*info
)
4500 /* Initialize the elf_note_info structure so that it is at
4501 * least safe to call free_note_info() on it. Must be
4502 * called before calling fill_note_info().
4504 memset(info
, 0, sizeof (*info
));
4505 QTAILQ_INIT(&info
->thread_list
);
4508 static int fill_note_info(struct elf_note_info
*info
,
4509 long signr
, const CPUArchState
*env
)
4512 CPUState
*cpu
= env_cpu((CPUArchState
*)env
);
4513 TaskState
*ts
= (TaskState
*)cpu
->opaque
;
4516 info
->notes
= g_new0(struct memelfnote
, NUMNOTES
);
4517 if (info
->notes
== NULL
)
4519 info
->prstatus
= g_malloc0(sizeof (*info
->prstatus
));
4520 if (info
->prstatus
== NULL
)
4522 info
->psinfo
= g_malloc0(sizeof (*info
->psinfo
));
4523 if (info
->prstatus
== NULL
)
4527 * First fill in status (and registers) of current thread
4528 * including process info & aux vector.
4530 fill_prstatus(info
->prstatus
, ts
, signr
);
4531 elf_core_copy_regs(&info
->prstatus
->pr_reg
, env
);
4532 fill_note(&info
->notes
[0], "CORE", NT_PRSTATUS
,
4533 sizeof (*info
->prstatus
), info
->prstatus
);
4534 fill_psinfo(info
->psinfo
, ts
);
4535 fill_note(&info
->notes
[1], "CORE", NT_PRPSINFO
,
4536 sizeof (*info
->psinfo
), info
->psinfo
);
4537 fill_auxv_note(&info
->notes
[2], ts
);
4540 info
->notes_size
= 0;
4541 for (i
= 0; i
< info
->numnote
; i
++)
4542 info
->notes_size
+= note_size(&info
->notes
[i
]);
4544 /* read and fill status of all threads */
4545 WITH_QEMU_LOCK_GUARD(&qemu_cpu_list_lock
) {
4547 if (cpu
== thread_cpu
) {
4550 fill_thread_info(info
, cpu_env(cpu
));
4557 static void free_note_info(struct elf_note_info
*info
)
4559 struct elf_thread_status
*ets
;
4561 while (!QTAILQ_EMPTY(&info
->thread_list
)) {
4562 ets
= QTAILQ_FIRST(&info
->thread_list
);
4563 QTAILQ_REMOVE(&info
->thread_list
, ets
, ets_link
);
4567 g_free(info
->prstatus
);
4568 g_free(info
->psinfo
);
4569 g_free(info
->notes
);
4572 static int write_note_info(struct elf_note_info
*info
, int fd
)
4574 struct elf_thread_status
*ets
;
4577 /* write prstatus, psinfo and auxv for current thread */
4578 for (i
= 0; i
< info
->numnote
; i
++)
4579 if ((error
= write_note(&info
->notes
[i
], fd
)) != 0)
4582 /* write prstatus for each thread */
4583 QTAILQ_FOREACH(ets
, &info
->thread_list
, ets_link
) {
4584 if ((error
= write_note(&ets
->notes
[0], fd
)) != 0)
4592 * Write out ELF coredump.
4594 * See documentation of ELF object file format in:
4595 * http://www.caldera.com/developers/devspecs/gabi41.pdf
4597 * Coredump format in linux is following:
4599 * 0 +----------------------+ \
4600 * | ELF header | ET_CORE |
4601 * +----------------------+ |
4602 * | ELF program headers | |--- headers
4603 * | - NOTE section | |
4604 * | - PT_LOAD sections | |
4605 * +----------------------+ /
4610 * +----------------------+ <-- aligned to target page
4611 * | Process memory dump |
4616 * +----------------------+
4618 * NT_PRSTATUS -> struct elf_prstatus (per thread)
4619 * NT_PRSINFO -> struct elf_prpsinfo
4620 * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()).
4622 * Format follows System V format as close as possible. Current
4623 * version limitations are as follows:
4624 * - no floating point registers are dumped
4626 * Function returns 0 in case of success, negative errno otherwise.
4628 * TODO: make this work also during runtime: it should be
4629 * possible to force coredump from running process and then
4630 * continue processing. For example qemu could set up SIGUSR2
4631 * handler (provided that target process haven't registered
4632 * handler for that) that does the dump when signal is received.
4634 static int elf_core_dump(int signr
, const CPUArchState
*env
)
4636 const CPUState
*cpu
= env_cpu((CPUArchState
*)env
);
4637 const TaskState
*ts
= (const TaskState
*)cpu
->opaque
;
4638 struct vm_area_struct
*vma
= NULL
;
4639 g_autofree
char *corefile
= NULL
;
4640 struct elf_note_info info
;
4642 struct elf_phdr phdr
;
4643 struct rlimit dumpsize
;
4644 struct mm_struct
*mm
= NULL
;
4645 off_t offset
= 0, data_offset
= 0;
4649 init_note_info(&info
);
4652 getrlimit(RLIMIT_CORE
, &dumpsize
);
4653 if (dumpsize
.rlim_cur
== 0)
4656 corefile
= core_dump_filename(ts
);
4658 if ((fd
= open(corefile
, O_WRONLY
| O_CREAT
,
4659 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
)) < 0)
4663 * Walk through target process memory mappings and
4664 * set up structure containing this information. After
4665 * this point vma_xxx functions can be used.
4667 if ((mm
= vma_init()) == NULL
)
4670 walk_memory_regions(mm
, vma_walker
);
4671 segs
= vma_get_mapping_count(mm
);
4674 * Construct valid coredump ELF header. We also
4675 * add one more segment for notes.
4677 fill_elf_header(&elf
, segs
+ 1, ELF_MACHINE
, 0);
4678 if (dump_write(fd
, &elf
, sizeof (elf
)) != 0)
4681 /* fill in the in-memory version of notes */
4682 if (fill_note_info(&info
, signr
, env
) < 0)
4685 offset
+= sizeof (elf
); /* elf header */
4686 offset
+= (segs
+ 1) * sizeof (struct elf_phdr
); /* program headers */
4688 /* write out notes program header */
4689 fill_elf_note_phdr(&phdr
, info
.notes_size
, offset
);
4691 offset
+= info
.notes_size
;
4692 if (dump_write(fd
, &phdr
, sizeof (phdr
)) != 0)
4696 * ELF specification wants data to start at page boundary so
4699 data_offset
= offset
= roundup(offset
, ELF_EXEC_PAGESIZE
);
4702 * Write program headers for memory regions mapped in
4703 * the target process.
4705 for (vma
= vma_first(mm
); vma
!= NULL
; vma
= vma_next(vma
)) {
4706 (void) memset(&phdr
, 0, sizeof (phdr
));
4708 phdr
.p_type
= PT_LOAD
;
4709 phdr
.p_offset
= offset
;
4710 phdr
.p_vaddr
= vma
->vma_start
;
4712 phdr
.p_filesz
= vma_dump_size(vma
);
4713 offset
+= phdr
.p_filesz
;
4714 phdr
.p_memsz
= vma
->vma_end
- vma
->vma_start
;
4715 phdr
.p_flags
= vma
->vma_flags
& PROT_READ
? PF_R
: 0;
4716 if (vma
->vma_flags
& PROT_WRITE
)
4717 phdr
.p_flags
|= PF_W
;
4718 if (vma
->vma_flags
& PROT_EXEC
)
4719 phdr
.p_flags
|= PF_X
;
4720 phdr
.p_align
= ELF_EXEC_PAGESIZE
;
4722 bswap_phdr(&phdr
, 1);
4723 if (dump_write(fd
, &phdr
, sizeof(phdr
)) != 0) {
4729 * Next we write notes just after program headers. No
4730 * alignment needed here.
4732 if (write_note_info(&info
, fd
) < 0)
4735 /* align data to page boundary */
4736 if (lseek(fd
, data_offset
, SEEK_SET
) != data_offset
)
4740 * Finally we can dump process memory into corefile as well.
4742 for (vma
= vma_first(mm
); vma
!= NULL
; vma
= vma_next(vma
)) {
4746 end
= vma
->vma_start
+ vma_dump_size(vma
);
4748 for (addr
= vma
->vma_start
; addr
< end
;
4749 addr
+= TARGET_PAGE_SIZE
) {
4750 char page
[TARGET_PAGE_SIZE
];
4754 * Read in page from target process memory and
4755 * write it to coredump file.
4757 error
= copy_from_user(page
, addr
, sizeof (page
));
4759 (void) fprintf(stderr
, "unable to dump " TARGET_ABI_FMT_lx
"\n",
4764 if (dump_write(fd
, page
, TARGET_PAGE_SIZE
) < 0)
4770 free_note_info(&info
);
4779 #endif /* USE_ELF_CORE_DUMP */
4781 void do_init_thread(struct target_pt_regs
*regs
, struct image_info
*infop
)
4783 init_thread(regs
, infop
);