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 #define ELF_OSABI ELFOSABI_SYSV
38 /* from personality.h */
41 * Flags for bug emulation.
43 * These occupy the top three bytes.
46 ADDR_NO_RANDOMIZE
= 0x0040000, /* disable randomization of VA space */
47 FDPIC_FUNCPTRS
= 0x0080000, /* userspace function ptrs point to
48 descriptors (signal handling) */
49 MMAP_PAGE_ZERO
= 0x0100000,
50 ADDR_COMPAT_LAYOUT
= 0x0200000,
51 READ_IMPLIES_EXEC
= 0x0400000,
52 ADDR_LIMIT_32BIT
= 0x0800000,
53 SHORT_INODE
= 0x1000000,
54 WHOLE_SECONDS
= 0x2000000,
55 STICKY_TIMEOUTS
= 0x4000000,
56 ADDR_LIMIT_3GB
= 0x8000000,
62 * These go in the low byte. Avoid using the top bit, it will
63 * conflict with error returns.
67 PER_LINUX_32BIT
= 0x0000 | ADDR_LIMIT_32BIT
,
68 PER_LINUX_FDPIC
= 0x0000 | FDPIC_FUNCPTRS
,
69 PER_SVR4
= 0x0001 | STICKY_TIMEOUTS
| MMAP_PAGE_ZERO
,
70 PER_SVR3
= 0x0002 | STICKY_TIMEOUTS
| SHORT_INODE
,
71 PER_SCOSVR3
= 0x0003 | STICKY_TIMEOUTS
| WHOLE_SECONDS
| SHORT_INODE
,
72 PER_OSR5
= 0x0003 | STICKY_TIMEOUTS
| WHOLE_SECONDS
,
73 PER_WYSEV386
= 0x0004 | STICKY_TIMEOUTS
| SHORT_INODE
,
74 PER_ISCR4
= 0x0005 | STICKY_TIMEOUTS
,
76 PER_SUNOS
= 0x0006 | STICKY_TIMEOUTS
,
77 PER_XENIX
= 0x0007 | STICKY_TIMEOUTS
| SHORT_INODE
,
79 PER_LINUX32_3GB
= 0x0008 | ADDR_LIMIT_3GB
,
80 PER_IRIX32
= 0x0009 | STICKY_TIMEOUTS
,/* IRIX5 32-bit */
81 PER_IRIXN32
= 0x000a | STICKY_TIMEOUTS
,/* IRIX6 new 32-bit */
82 PER_IRIX64
= 0x000b | STICKY_TIMEOUTS
,/* IRIX6 64-bit */
84 PER_SOLARIS
= 0x000d | STICKY_TIMEOUTS
,
85 PER_UW7
= 0x000e | STICKY_TIMEOUTS
| MMAP_PAGE_ZERO
,
86 PER_OSF4
= 0x000f, /* OSF/1 v4 */
92 * Return the base personality without flags.
94 #define personality(pers) (pers & PER_MASK)
96 int info_is_fdpic(struct image_info
*info
)
98 return info
->personality
== PER_LINUX_FDPIC
;
101 /* this flag is uneffective under linux too, should be deleted */
102 #ifndef MAP_DENYWRITE
103 #define MAP_DENYWRITE 0
106 /* should probably go in elf.h */
111 #if TARGET_BIG_ENDIAN
112 #define ELF_DATA ELFDATA2MSB
114 #define ELF_DATA ELFDATA2LSB
117 #ifdef TARGET_ABI_MIPSN32
118 typedef abi_ullong target_elf_greg_t
;
119 #define tswapreg(ptr) tswap64(ptr)
121 typedef abi_ulong target_elf_greg_t
;
122 #define tswapreg(ptr) tswapal(ptr)
126 typedef abi_ushort target_uid_t
;
127 typedef abi_ushort target_gid_t
;
129 typedef abi_uint target_uid_t
;
130 typedef abi_uint target_gid_t
;
132 typedef abi_int target_pid_t
;
136 #define ELF_HWCAP get_elf_hwcap()
138 static uint32_t get_elf_hwcap(void)
140 X86CPU
*cpu
= X86_CPU(thread_cpu
);
142 return cpu
->env
.features
[FEAT_1_EDX
];
146 #define ELF_START_MMAP 0x2aaaaab000ULL
148 #define ELF_CLASS ELFCLASS64
149 #define ELF_ARCH EM_X86_64
151 #define ELF_PLATFORM "x86_64"
153 static inline void init_thread(struct target_pt_regs
*regs
, struct image_info
*infop
)
156 regs
->rsp
= infop
->start_stack
;
157 regs
->rip
= infop
->entry
;
161 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
164 * Note that ELF_NREG should be 29 as there should be place for
165 * TRAPNO and ERR "registers" as well but linux doesn't dump
168 * See linux kernel: arch/x86/include/asm/elf.h
170 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUX86State
*env
)
172 (*regs
)[0] = tswapreg(env
->regs
[15]);
173 (*regs
)[1] = tswapreg(env
->regs
[14]);
174 (*regs
)[2] = tswapreg(env
->regs
[13]);
175 (*regs
)[3] = tswapreg(env
->regs
[12]);
176 (*regs
)[4] = tswapreg(env
->regs
[R_EBP
]);
177 (*regs
)[5] = tswapreg(env
->regs
[R_EBX
]);
178 (*regs
)[6] = tswapreg(env
->regs
[11]);
179 (*regs
)[7] = tswapreg(env
->regs
[10]);
180 (*regs
)[8] = tswapreg(env
->regs
[9]);
181 (*regs
)[9] = tswapreg(env
->regs
[8]);
182 (*regs
)[10] = tswapreg(env
->regs
[R_EAX
]);
183 (*regs
)[11] = tswapreg(env
->regs
[R_ECX
]);
184 (*regs
)[12] = tswapreg(env
->regs
[R_EDX
]);
185 (*regs
)[13] = tswapreg(env
->regs
[R_ESI
]);
186 (*regs
)[14] = tswapreg(env
->regs
[R_EDI
]);
187 (*regs
)[15] = tswapreg(env
->regs
[R_EAX
]); /* XXX */
188 (*regs
)[16] = tswapreg(env
->eip
);
189 (*regs
)[17] = tswapreg(env
->segs
[R_CS
].selector
& 0xffff);
190 (*regs
)[18] = tswapreg(env
->eflags
);
191 (*regs
)[19] = tswapreg(env
->regs
[R_ESP
]);
192 (*regs
)[20] = tswapreg(env
->segs
[R_SS
].selector
& 0xffff);
193 (*regs
)[21] = tswapreg(env
->segs
[R_FS
].selector
& 0xffff);
194 (*regs
)[22] = tswapreg(env
->segs
[R_GS
].selector
& 0xffff);
195 (*regs
)[23] = tswapreg(env
->segs
[R_DS
].selector
& 0xffff);
196 (*regs
)[24] = tswapreg(env
->segs
[R_ES
].selector
& 0xffff);
197 (*regs
)[25] = tswapreg(env
->segs
[R_FS
].selector
& 0xffff);
198 (*regs
)[26] = tswapreg(env
->segs
[R_GS
].selector
& 0xffff);
201 #if ULONG_MAX > UINT32_MAX
202 #define INIT_GUEST_COMMPAGE
203 static bool init_guest_commpage(void)
206 * The vsyscall page is at a high negative address aka kernel space,
207 * which means that we cannot actually allocate it with target_mmap.
208 * We still should be able to use page_set_flags, unless the user
209 * has specified -R reserved_va, which would trigger an assert().
211 if (reserved_va
!= 0 &&
212 TARGET_VSYSCALL_PAGE
+ TARGET_PAGE_SIZE
- 1 > reserved_va
) {
213 error_report("Cannot allocate vsyscall page");
216 page_set_flags(TARGET_VSYSCALL_PAGE
,
217 TARGET_VSYSCALL_PAGE
| ~TARGET_PAGE_MASK
,
218 PAGE_EXEC
| PAGE_VALID
);
224 #define ELF_START_MMAP 0x80000000
227 * This is used to ensure we don't load something for the wrong architecture.
229 #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
232 * These are used to set parameters in the core dumps.
234 #define ELF_CLASS ELFCLASS32
235 #define ELF_ARCH EM_386
237 #define ELF_PLATFORM get_elf_platform()
238 #define EXSTACK_DEFAULT true
240 static const char *get_elf_platform(void)
242 static char elf_platform
[] = "i386";
243 int family
= object_property_get_int(OBJECT(thread_cpu
), "family", NULL
);
248 elf_platform
[1] = '0' + family
;
253 static inline void init_thread(struct target_pt_regs
*regs
,
254 struct image_info
*infop
)
256 regs
->esp
= infop
->start_stack
;
257 regs
->eip
= infop
->entry
;
259 /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program
260 starts %edx contains a pointer to a function which might be
261 registered using `atexit'. This provides a mean for the
262 dynamic linker to call DT_FINI functions for shared libraries
263 that have been loaded before the code runs.
265 A value of 0 tells we have no such handler. */
270 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
273 * Note that ELF_NREG should be 19 as there should be place for
274 * TRAPNO and ERR "registers" as well but linux doesn't dump
277 * See linux kernel: arch/x86/include/asm/elf.h
279 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUX86State
*env
)
281 (*regs
)[0] = tswapreg(env
->regs
[R_EBX
]);
282 (*regs
)[1] = tswapreg(env
->regs
[R_ECX
]);
283 (*regs
)[2] = tswapreg(env
->regs
[R_EDX
]);
284 (*regs
)[3] = tswapreg(env
->regs
[R_ESI
]);
285 (*regs
)[4] = tswapreg(env
->regs
[R_EDI
]);
286 (*regs
)[5] = tswapreg(env
->regs
[R_EBP
]);
287 (*regs
)[6] = tswapreg(env
->regs
[R_EAX
]);
288 (*regs
)[7] = tswapreg(env
->segs
[R_DS
].selector
& 0xffff);
289 (*regs
)[8] = tswapreg(env
->segs
[R_ES
].selector
& 0xffff);
290 (*regs
)[9] = tswapreg(env
->segs
[R_FS
].selector
& 0xffff);
291 (*regs
)[10] = tswapreg(env
->segs
[R_GS
].selector
& 0xffff);
292 (*regs
)[11] = tswapreg(env
->regs
[R_EAX
]); /* XXX */
293 (*regs
)[12] = tswapreg(env
->eip
);
294 (*regs
)[13] = tswapreg(env
->segs
[R_CS
].selector
& 0xffff);
295 (*regs
)[14] = tswapreg(env
->eflags
);
296 (*regs
)[15] = tswapreg(env
->regs
[R_ESP
]);
297 (*regs
)[16] = tswapreg(env
->segs
[R_SS
].selector
& 0xffff);
301 #define USE_ELF_CORE_DUMP
302 #define ELF_EXEC_PAGESIZE 4096
308 #ifndef TARGET_AARCH64
309 /* 32 bit ARM definitions */
311 #define ELF_START_MMAP 0x80000000
313 #define ELF_ARCH EM_ARM
314 #define ELF_CLASS ELFCLASS32
315 #define EXSTACK_DEFAULT true
317 static inline void init_thread(struct target_pt_regs
*regs
,
318 struct image_info
*infop
)
320 abi_long stack
= infop
->start_stack
;
321 memset(regs
, 0, sizeof(*regs
));
323 regs
->uregs
[16] = ARM_CPU_MODE_USR
;
324 if (infop
->entry
& 1) {
325 regs
->uregs
[16] |= CPSR_T
;
327 regs
->uregs
[15] = infop
->entry
& 0xfffffffe;
328 regs
->uregs
[13] = infop
->start_stack
;
329 /* FIXME - what to for failure of get_user()? */
330 get_user_ual(regs
->uregs
[2], stack
+ 8); /* envp */
331 get_user_ual(regs
->uregs
[1], stack
+ 4); /* envp */
332 /* XXX: it seems that r0 is zeroed after ! */
334 /* For uClinux PIC binaries. */
335 /* XXX: Linux does this only on ARM with no MMU (do we care ?) */
336 regs
->uregs
[10] = infop
->start_data
;
338 /* Support ARM FDPIC. */
339 if (info_is_fdpic(infop
)) {
340 /* As described in the ABI document, r7 points to the loadmap info
341 * prepared by the kernel. If an interpreter is needed, r8 points
342 * to the interpreter loadmap and r9 points to the interpreter
343 * PT_DYNAMIC info. If no interpreter is needed, r8 is zero, and
344 * r9 points to the main program PT_DYNAMIC info.
346 regs
->uregs
[7] = infop
->loadmap_addr
;
347 if (infop
->interpreter_loadmap_addr
) {
348 /* Executable is dynamically loaded. */
349 regs
->uregs
[8] = infop
->interpreter_loadmap_addr
;
350 regs
->uregs
[9] = infop
->interpreter_pt_dynamic_addr
;
353 regs
->uregs
[9] = infop
->pt_dynamic_addr
;
359 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
361 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUARMState
*env
)
363 (*regs
)[0] = tswapreg(env
->regs
[0]);
364 (*regs
)[1] = tswapreg(env
->regs
[1]);
365 (*regs
)[2] = tswapreg(env
->regs
[2]);
366 (*regs
)[3] = tswapreg(env
->regs
[3]);
367 (*regs
)[4] = tswapreg(env
->regs
[4]);
368 (*regs
)[5] = tswapreg(env
->regs
[5]);
369 (*regs
)[6] = tswapreg(env
->regs
[6]);
370 (*regs
)[7] = tswapreg(env
->regs
[7]);
371 (*regs
)[8] = tswapreg(env
->regs
[8]);
372 (*regs
)[9] = tswapreg(env
->regs
[9]);
373 (*regs
)[10] = tswapreg(env
->regs
[10]);
374 (*regs
)[11] = tswapreg(env
->regs
[11]);
375 (*regs
)[12] = tswapreg(env
->regs
[12]);
376 (*regs
)[13] = tswapreg(env
->regs
[13]);
377 (*regs
)[14] = tswapreg(env
->regs
[14]);
378 (*regs
)[15] = tswapreg(env
->regs
[15]);
380 (*regs
)[16] = tswapreg(cpsr_read((CPUARMState
*)env
));
381 (*regs
)[17] = tswapreg(env
->regs
[0]); /* XXX */
384 #define USE_ELF_CORE_DUMP
385 #define ELF_EXEC_PAGESIZE 4096
389 ARM_HWCAP_ARM_SWP
= 1 << 0,
390 ARM_HWCAP_ARM_HALF
= 1 << 1,
391 ARM_HWCAP_ARM_THUMB
= 1 << 2,
392 ARM_HWCAP_ARM_26BIT
= 1 << 3,
393 ARM_HWCAP_ARM_FAST_MULT
= 1 << 4,
394 ARM_HWCAP_ARM_FPA
= 1 << 5,
395 ARM_HWCAP_ARM_VFP
= 1 << 6,
396 ARM_HWCAP_ARM_EDSP
= 1 << 7,
397 ARM_HWCAP_ARM_JAVA
= 1 << 8,
398 ARM_HWCAP_ARM_IWMMXT
= 1 << 9,
399 ARM_HWCAP_ARM_CRUNCH
= 1 << 10,
400 ARM_HWCAP_ARM_THUMBEE
= 1 << 11,
401 ARM_HWCAP_ARM_NEON
= 1 << 12,
402 ARM_HWCAP_ARM_VFPv3
= 1 << 13,
403 ARM_HWCAP_ARM_VFPv3D16
= 1 << 14,
404 ARM_HWCAP_ARM_TLS
= 1 << 15,
405 ARM_HWCAP_ARM_VFPv4
= 1 << 16,
406 ARM_HWCAP_ARM_IDIVA
= 1 << 17,
407 ARM_HWCAP_ARM_IDIVT
= 1 << 18,
408 ARM_HWCAP_ARM_VFPD32
= 1 << 19,
409 ARM_HWCAP_ARM_LPAE
= 1 << 20,
410 ARM_HWCAP_ARM_EVTSTRM
= 1 << 21,
414 ARM_HWCAP2_ARM_AES
= 1 << 0,
415 ARM_HWCAP2_ARM_PMULL
= 1 << 1,
416 ARM_HWCAP2_ARM_SHA1
= 1 << 2,
417 ARM_HWCAP2_ARM_SHA2
= 1 << 3,
418 ARM_HWCAP2_ARM_CRC32
= 1 << 4,
421 /* The commpage only exists for 32 bit kernels */
423 #define HI_COMMPAGE (intptr_t)0xffff0f00u
425 static bool init_guest_commpage(void)
427 ARMCPU
*cpu
= ARM_CPU(thread_cpu
);
433 * M-profile allocates maximum of 2GB address space, so can never
434 * allocate the commpage. Skip it.
436 if (arm_feature(&cpu
->env
, ARM_FEATURE_M
)) {
440 commpage
= HI_COMMPAGE
& -qemu_host_page_size
;
441 want
= g2h_untagged(commpage
);
442 addr
= mmap(want
, qemu_host_page_size
, PROT_READ
| PROT_WRITE
,
443 MAP_ANONYMOUS
| MAP_PRIVATE
| MAP_FIXED
, -1, 0);
445 if (addr
== MAP_FAILED
) {
446 perror("Allocating guest commpage");
453 /* Set kernel helper versions; rest of page is 0. */
454 __put_user(5, (uint32_t *)g2h_untagged(0xffff0ffcu
));
456 if (mprotect(addr
, qemu_host_page_size
, PROT_READ
)) {
457 perror("Protecting guest commpage");
461 page_set_flags(commpage
, commpage
| ~qemu_host_page_mask
,
462 PAGE_READ
| PAGE_EXEC
| PAGE_VALID
);
466 #define ELF_HWCAP get_elf_hwcap()
467 #define ELF_HWCAP2 get_elf_hwcap2()
469 static uint32_t get_elf_hwcap(void)
471 ARMCPU
*cpu
= ARM_CPU(thread_cpu
);
474 hwcaps
|= ARM_HWCAP_ARM_SWP
;
475 hwcaps
|= ARM_HWCAP_ARM_HALF
;
476 hwcaps
|= ARM_HWCAP_ARM_THUMB
;
477 hwcaps
|= ARM_HWCAP_ARM_FAST_MULT
;
479 /* probe for the extra features */
480 #define GET_FEATURE(feat, hwcap) \
481 do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0)
483 #define GET_FEATURE_ID(feat, hwcap) \
484 do { if (cpu_isar_feature(feat, cpu)) { hwcaps |= hwcap; } } while (0)
486 /* EDSP is in v5TE and above, but all our v5 CPUs are v5TE */
487 GET_FEATURE(ARM_FEATURE_V5
, ARM_HWCAP_ARM_EDSP
);
488 GET_FEATURE(ARM_FEATURE_IWMMXT
, ARM_HWCAP_ARM_IWMMXT
);
489 GET_FEATURE(ARM_FEATURE_THUMB2EE
, ARM_HWCAP_ARM_THUMBEE
);
490 GET_FEATURE(ARM_FEATURE_NEON
, ARM_HWCAP_ARM_NEON
);
491 GET_FEATURE(ARM_FEATURE_V6K
, ARM_HWCAP_ARM_TLS
);
492 GET_FEATURE(ARM_FEATURE_LPAE
, ARM_HWCAP_ARM_LPAE
);
493 GET_FEATURE_ID(aa32_arm_div
, ARM_HWCAP_ARM_IDIVA
);
494 GET_FEATURE_ID(aa32_thumb_div
, ARM_HWCAP_ARM_IDIVT
);
495 GET_FEATURE_ID(aa32_vfp
, ARM_HWCAP_ARM_VFP
);
497 if (cpu_isar_feature(aa32_fpsp_v3
, cpu
) ||
498 cpu_isar_feature(aa32_fpdp_v3
, cpu
)) {
499 hwcaps
|= ARM_HWCAP_ARM_VFPv3
;
500 if (cpu_isar_feature(aa32_simd_r32
, cpu
)) {
501 hwcaps
|= ARM_HWCAP_ARM_VFPD32
;
503 hwcaps
|= ARM_HWCAP_ARM_VFPv3D16
;
506 GET_FEATURE_ID(aa32_simdfmac
, ARM_HWCAP_ARM_VFPv4
);
511 static uint32_t get_elf_hwcap2(void)
513 ARMCPU
*cpu
= ARM_CPU(thread_cpu
);
516 GET_FEATURE_ID(aa32_aes
, ARM_HWCAP2_ARM_AES
);
517 GET_FEATURE_ID(aa32_pmull
, ARM_HWCAP2_ARM_PMULL
);
518 GET_FEATURE_ID(aa32_sha1
, ARM_HWCAP2_ARM_SHA1
);
519 GET_FEATURE_ID(aa32_sha2
, ARM_HWCAP2_ARM_SHA2
);
520 GET_FEATURE_ID(aa32_crc32
, ARM_HWCAP2_ARM_CRC32
);
525 #undef GET_FEATURE_ID
527 #define ELF_PLATFORM get_elf_platform()
529 static const char *get_elf_platform(void)
531 CPUARMState
*env
= thread_cpu
->env_ptr
;
533 #if TARGET_BIG_ENDIAN
539 if (arm_feature(env
, ARM_FEATURE_V8
)) {
541 } else if (arm_feature(env
, ARM_FEATURE_V7
)) {
542 if (arm_feature(env
, ARM_FEATURE_M
)) {
547 } else if (arm_feature(env
, ARM_FEATURE_V6
)) {
549 } else if (arm_feature(env
, ARM_FEATURE_V5
)) {
559 /* 64 bit ARM definitions */
560 #define ELF_START_MMAP 0x80000000
562 #define ELF_ARCH EM_AARCH64
563 #define ELF_CLASS ELFCLASS64
564 #if TARGET_BIG_ENDIAN
565 # define ELF_PLATFORM "aarch64_be"
567 # define ELF_PLATFORM "aarch64"
570 static inline void init_thread(struct target_pt_regs
*regs
,
571 struct image_info
*infop
)
573 abi_long stack
= infop
->start_stack
;
574 memset(regs
, 0, sizeof(*regs
));
576 regs
->pc
= infop
->entry
& ~0x3ULL
;
581 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
583 static void elf_core_copy_regs(target_elf_gregset_t
*regs
,
584 const CPUARMState
*env
)
588 for (i
= 0; i
< 32; i
++) {
589 (*regs
)[i
] = tswapreg(env
->xregs
[i
]);
591 (*regs
)[32] = tswapreg(env
->pc
);
592 (*regs
)[33] = tswapreg(pstate_read((CPUARMState
*)env
));
595 #define USE_ELF_CORE_DUMP
596 #define ELF_EXEC_PAGESIZE 4096
599 ARM_HWCAP_A64_FP
= 1 << 0,
600 ARM_HWCAP_A64_ASIMD
= 1 << 1,
601 ARM_HWCAP_A64_EVTSTRM
= 1 << 2,
602 ARM_HWCAP_A64_AES
= 1 << 3,
603 ARM_HWCAP_A64_PMULL
= 1 << 4,
604 ARM_HWCAP_A64_SHA1
= 1 << 5,
605 ARM_HWCAP_A64_SHA2
= 1 << 6,
606 ARM_HWCAP_A64_CRC32
= 1 << 7,
607 ARM_HWCAP_A64_ATOMICS
= 1 << 8,
608 ARM_HWCAP_A64_FPHP
= 1 << 9,
609 ARM_HWCAP_A64_ASIMDHP
= 1 << 10,
610 ARM_HWCAP_A64_CPUID
= 1 << 11,
611 ARM_HWCAP_A64_ASIMDRDM
= 1 << 12,
612 ARM_HWCAP_A64_JSCVT
= 1 << 13,
613 ARM_HWCAP_A64_FCMA
= 1 << 14,
614 ARM_HWCAP_A64_LRCPC
= 1 << 15,
615 ARM_HWCAP_A64_DCPOP
= 1 << 16,
616 ARM_HWCAP_A64_SHA3
= 1 << 17,
617 ARM_HWCAP_A64_SM3
= 1 << 18,
618 ARM_HWCAP_A64_SM4
= 1 << 19,
619 ARM_HWCAP_A64_ASIMDDP
= 1 << 20,
620 ARM_HWCAP_A64_SHA512
= 1 << 21,
621 ARM_HWCAP_A64_SVE
= 1 << 22,
622 ARM_HWCAP_A64_ASIMDFHM
= 1 << 23,
623 ARM_HWCAP_A64_DIT
= 1 << 24,
624 ARM_HWCAP_A64_USCAT
= 1 << 25,
625 ARM_HWCAP_A64_ILRCPC
= 1 << 26,
626 ARM_HWCAP_A64_FLAGM
= 1 << 27,
627 ARM_HWCAP_A64_SSBS
= 1 << 28,
628 ARM_HWCAP_A64_SB
= 1 << 29,
629 ARM_HWCAP_A64_PACA
= 1 << 30,
630 ARM_HWCAP_A64_PACG
= 1UL << 31,
632 ARM_HWCAP2_A64_DCPODP
= 1 << 0,
633 ARM_HWCAP2_A64_SVE2
= 1 << 1,
634 ARM_HWCAP2_A64_SVEAES
= 1 << 2,
635 ARM_HWCAP2_A64_SVEPMULL
= 1 << 3,
636 ARM_HWCAP2_A64_SVEBITPERM
= 1 << 4,
637 ARM_HWCAP2_A64_SVESHA3
= 1 << 5,
638 ARM_HWCAP2_A64_SVESM4
= 1 << 6,
639 ARM_HWCAP2_A64_FLAGM2
= 1 << 7,
640 ARM_HWCAP2_A64_FRINT
= 1 << 8,
641 ARM_HWCAP2_A64_SVEI8MM
= 1 << 9,
642 ARM_HWCAP2_A64_SVEF32MM
= 1 << 10,
643 ARM_HWCAP2_A64_SVEF64MM
= 1 << 11,
644 ARM_HWCAP2_A64_SVEBF16
= 1 << 12,
645 ARM_HWCAP2_A64_I8MM
= 1 << 13,
646 ARM_HWCAP2_A64_BF16
= 1 << 14,
647 ARM_HWCAP2_A64_DGH
= 1 << 15,
648 ARM_HWCAP2_A64_RNG
= 1 << 16,
649 ARM_HWCAP2_A64_BTI
= 1 << 17,
650 ARM_HWCAP2_A64_MTE
= 1 << 18,
651 ARM_HWCAP2_A64_ECV
= 1 << 19,
652 ARM_HWCAP2_A64_AFP
= 1 << 20,
653 ARM_HWCAP2_A64_RPRES
= 1 << 21,
654 ARM_HWCAP2_A64_MTE3
= 1 << 22,
655 ARM_HWCAP2_A64_SME
= 1 << 23,
656 ARM_HWCAP2_A64_SME_I16I64
= 1 << 24,
657 ARM_HWCAP2_A64_SME_F64F64
= 1 << 25,
658 ARM_HWCAP2_A64_SME_I8I32
= 1 << 26,
659 ARM_HWCAP2_A64_SME_F16F32
= 1 << 27,
660 ARM_HWCAP2_A64_SME_B16F32
= 1 << 28,
661 ARM_HWCAP2_A64_SME_F32F32
= 1 << 29,
662 ARM_HWCAP2_A64_SME_FA64
= 1 << 30,
665 #define ELF_HWCAP get_elf_hwcap()
666 #define ELF_HWCAP2 get_elf_hwcap2()
668 #define GET_FEATURE_ID(feat, hwcap) \
669 do { if (cpu_isar_feature(feat, cpu)) { hwcaps |= hwcap; } } while (0)
671 static uint32_t get_elf_hwcap(void)
673 ARMCPU
*cpu
= ARM_CPU(thread_cpu
);
676 hwcaps
|= ARM_HWCAP_A64_FP
;
677 hwcaps
|= ARM_HWCAP_A64_ASIMD
;
678 hwcaps
|= ARM_HWCAP_A64_CPUID
;
680 /* probe for the extra features */
682 GET_FEATURE_ID(aa64_aes
, ARM_HWCAP_A64_AES
);
683 GET_FEATURE_ID(aa64_pmull
, ARM_HWCAP_A64_PMULL
);
684 GET_FEATURE_ID(aa64_sha1
, ARM_HWCAP_A64_SHA1
);
685 GET_FEATURE_ID(aa64_sha256
, ARM_HWCAP_A64_SHA2
);
686 GET_FEATURE_ID(aa64_sha512
, ARM_HWCAP_A64_SHA512
);
687 GET_FEATURE_ID(aa64_crc32
, ARM_HWCAP_A64_CRC32
);
688 GET_FEATURE_ID(aa64_sha3
, ARM_HWCAP_A64_SHA3
);
689 GET_FEATURE_ID(aa64_sm3
, ARM_HWCAP_A64_SM3
);
690 GET_FEATURE_ID(aa64_sm4
, ARM_HWCAP_A64_SM4
);
691 GET_FEATURE_ID(aa64_fp16
, ARM_HWCAP_A64_FPHP
| ARM_HWCAP_A64_ASIMDHP
);
692 GET_FEATURE_ID(aa64_atomics
, ARM_HWCAP_A64_ATOMICS
);
693 GET_FEATURE_ID(aa64_rdm
, ARM_HWCAP_A64_ASIMDRDM
);
694 GET_FEATURE_ID(aa64_dp
, ARM_HWCAP_A64_ASIMDDP
);
695 GET_FEATURE_ID(aa64_fcma
, ARM_HWCAP_A64_FCMA
);
696 GET_FEATURE_ID(aa64_sve
, ARM_HWCAP_A64_SVE
);
697 GET_FEATURE_ID(aa64_pauth
, ARM_HWCAP_A64_PACA
| ARM_HWCAP_A64_PACG
);
698 GET_FEATURE_ID(aa64_fhm
, ARM_HWCAP_A64_ASIMDFHM
);
699 GET_FEATURE_ID(aa64_jscvt
, ARM_HWCAP_A64_JSCVT
);
700 GET_FEATURE_ID(aa64_sb
, ARM_HWCAP_A64_SB
);
701 GET_FEATURE_ID(aa64_condm_4
, ARM_HWCAP_A64_FLAGM
);
702 GET_FEATURE_ID(aa64_dcpop
, ARM_HWCAP_A64_DCPOP
);
703 GET_FEATURE_ID(aa64_rcpc_8_3
, ARM_HWCAP_A64_LRCPC
);
704 GET_FEATURE_ID(aa64_rcpc_8_4
, ARM_HWCAP_A64_ILRCPC
);
709 static uint32_t get_elf_hwcap2(void)
711 ARMCPU
*cpu
= ARM_CPU(thread_cpu
);
714 GET_FEATURE_ID(aa64_dcpodp
, ARM_HWCAP2_A64_DCPODP
);
715 GET_FEATURE_ID(aa64_sve2
, ARM_HWCAP2_A64_SVE2
);
716 GET_FEATURE_ID(aa64_sve2_aes
, ARM_HWCAP2_A64_SVEAES
);
717 GET_FEATURE_ID(aa64_sve2_pmull128
, ARM_HWCAP2_A64_SVEPMULL
);
718 GET_FEATURE_ID(aa64_sve2_bitperm
, ARM_HWCAP2_A64_SVEBITPERM
);
719 GET_FEATURE_ID(aa64_sve2_sha3
, ARM_HWCAP2_A64_SVESHA3
);
720 GET_FEATURE_ID(aa64_sve2_sm4
, ARM_HWCAP2_A64_SVESM4
);
721 GET_FEATURE_ID(aa64_condm_5
, ARM_HWCAP2_A64_FLAGM2
);
722 GET_FEATURE_ID(aa64_frint
, ARM_HWCAP2_A64_FRINT
);
723 GET_FEATURE_ID(aa64_sve_i8mm
, ARM_HWCAP2_A64_SVEI8MM
);
724 GET_FEATURE_ID(aa64_sve_f32mm
, ARM_HWCAP2_A64_SVEF32MM
);
725 GET_FEATURE_ID(aa64_sve_f64mm
, ARM_HWCAP2_A64_SVEF64MM
);
726 GET_FEATURE_ID(aa64_sve_bf16
, ARM_HWCAP2_A64_SVEBF16
);
727 GET_FEATURE_ID(aa64_i8mm
, ARM_HWCAP2_A64_I8MM
);
728 GET_FEATURE_ID(aa64_bf16
, ARM_HWCAP2_A64_BF16
);
729 GET_FEATURE_ID(aa64_rndr
, ARM_HWCAP2_A64_RNG
);
730 GET_FEATURE_ID(aa64_bti
, ARM_HWCAP2_A64_BTI
);
731 GET_FEATURE_ID(aa64_mte
, ARM_HWCAP2_A64_MTE
);
732 GET_FEATURE_ID(aa64_sme
, (ARM_HWCAP2_A64_SME
|
733 ARM_HWCAP2_A64_SME_F32F32
|
734 ARM_HWCAP2_A64_SME_B16F32
|
735 ARM_HWCAP2_A64_SME_F16F32
|
736 ARM_HWCAP2_A64_SME_I8I32
));
737 GET_FEATURE_ID(aa64_sme_f64f64
, ARM_HWCAP2_A64_SME_F64F64
);
738 GET_FEATURE_ID(aa64_sme_i16i64
, ARM_HWCAP2_A64_SME_I16I64
);
739 GET_FEATURE_ID(aa64_sme_fa64
, ARM_HWCAP2_A64_SME_FA64
);
744 #undef GET_FEATURE_ID
746 #endif /* not TARGET_AARCH64 */
747 #endif /* TARGET_ARM */
750 #ifdef TARGET_SPARC64
752 #define ELF_START_MMAP 0x80000000
753 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
754 | HWCAP_SPARC_MULDIV | HWCAP_SPARC_V9)
756 #define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS )
758 #define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC )
761 #define ELF_CLASS ELFCLASS64
762 #define ELF_ARCH EM_SPARCV9
764 #define ELF_START_MMAP 0x80000000
765 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
766 | HWCAP_SPARC_MULDIV)
767 #define ELF_CLASS ELFCLASS32
768 #define ELF_ARCH EM_SPARC
769 #endif /* TARGET_SPARC64 */
771 static inline void init_thread(struct target_pt_regs
*regs
,
772 struct image_info
*infop
)
774 /* Note that target_cpu_copy_regs does not read psr/tstate. */
775 regs
->pc
= infop
->entry
;
776 regs
->npc
= regs
->pc
+ 4;
778 regs
->u_regs
[14] = (infop
->start_stack
- 16 * sizeof(abi_ulong
)
779 - TARGET_STACK_BIAS
);
781 #endif /* TARGET_SPARC */
785 #define ELF_MACHINE PPC_ELF_MACHINE
786 #define ELF_START_MMAP 0x80000000
788 #if defined(TARGET_PPC64)
790 #define elf_check_arch(x) ( (x) == EM_PPC64 )
792 #define ELF_CLASS ELFCLASS64
796 #define ELF_CLASS ELFCLASS32
797 #define EXSTACK_DEFAULT true
801 #define ELF_ARCH EM_PPC
803 /* Feature masks for the Aux Vector Hardware Capabilities (AT_HWCAP).
804 See arch/powerpc/include/asm/cputable.h. */
806 QEMU_PPC_FEATURE_32
= 0x80000000,
807 QEMU_PPC_FEATURE_64
= 0x40000000,
808 QEMU_PPC_FEATURE_601_INSTR
= 0x20000000,
809 QEMU_PPC_FEATURE_HAS_ALTIVEC
= 0x10000000,
810 QEMU_PPC_FEATURE_HAS_FPU
= 0x08000000,
811 QEMU_PPC_FEATURE_HAS_MMU
= 0x04000000,
812 QEMU_PPC_FEATURE_HAS_4xxMAC
= 0x02000000,
813 QEMU_PPC_FEATURE_UNIFIED_CACHE
= 0x01000000,
814 QEMU_PPC_FEATURE_HAS_SPE
= 0x00800000,
815 QEMU_PPC_FEATURE_HAS_EFP_SINGLE
= 0x00400000,
816 QEMU_PPC_FEATURE_HAS_EFP_DOUBLE
= 0x00200000,
817 QEMU_PPC_FEATURE_NO_TB
= 0x00100000,
818 QEMU_PPC_FEATURE_POWER4
= 0x00080000,
819 QEMU_PPC_FEATURE_POWER5
= 0x00040000,
820 QEMU_PPC_FEATURE_POWER5_PLUS
= 0x00020000,
821 QEMU_PPC_FEATURE_CELL
= 0x00010000,
822 QEMU_PPC_FEATURE_BOOKE
= 0x00008000,
823 QEMU_PPC_FEATURE_SMT
= 0x00004000,
824 QEMU_PPC_FEATURE_ICACHE_SNOOP
= 0x00002000,
825 QEMU_PPC_FEATURE_ARCH_2_05
= 0x00001000,
826 QEMU_PPC_FEATURE_PA6T
= 0x00000800,
827 QEMU_PPC_FEATURE_HAS_DFP
= 0x00000400,
828 QEMU_PPC_FEATURE_POWER6_EXT
= 0x00000200,
829 QEMU_PPC_FEATURE_ARCH_2_06
= 0x00000100,
830 QEMU_PPC_FEATURE_HAS_VSX
= 0x00000080,
831 QEMU_PPC_FEATURE_PSERIES_PERFMON_COMPAT
= 0x00000040,
833 QEMU_PPC_FEATURE_TRUE_LE
= 0x00000002,
834 QEMU_PPC_FEATURE_PPC_LE
= 0x00000001,
836 /* Feature definitions in AT_HWCAP2. */
837 QEMU_PPC_FEATURE2_ARCH_2_07
= 0x80000000, /* ISA 2.07 */
838 QEMU_PPC_FEATURE2_HAS_HTM
= 0x40000000, /* Hardware Transactional Memory */
839 QEMU_PPC_FEATURE2_HAS_DSCR
= 0x20000000, /* Data Stream Control Register */
840 QEMU_PPC_FEATURE2_HAS_EBB
= 0x10000000, /* Event Base Branching */
841 QEMU_PPC_FEATURE2_HAS_ISEL
= 0x08000000, /* Integer Select */
842 QEMU_PPC_FEATURE2_HAS_TAR
= 0x04000000, /* Target Address Register */
843 QEMU_PPC_FEATURE2_VEC_CRYPTO
= 0x02000000,
844 QEMU_PPC_FEATURE2_HTM_NOSC
= 0x01000000,
845 QEMU_PPC_FEATURE2_ARCH_3_00
= 0x00800000, /* ISA 3.00 */
846 QEMU_PPC_FEATURE2_HAS_IEEE128
= 0x00400000, /* VSX IEEE Bin Float 128-bit */
847 QEMU_PPC_FEATURE2_DARN
= 0x00200000, /* darn random number insn */
848 QEMU_PPC_FEATURE2_SCV
= 0x00100000, /* scv syscall */
849 QEMU_PPC_FEATURE2_HTM_NO_SUSPEND
= 0x00080000, /* TM w/o suspended state */
850 QEMU_PPC_FEATURE2_ARCH_3_1
= 0x00040000, /* ISA 3.1 */
851 QEMU_PPC_FEATURE2_MMA
= 0x00020000, /* Matrix-Multiply Assist */
854 #define ELF_HWCAP get_elf_hwcap()
856 static uint32_t get_elf_hwcap(void)
858 PowerPCCPU
*cpu
= POWERPC_CPU(thread_cpu
);
859 uint32_t features
= 0;
861 /* We don't have to be terribly complete here; the high points are
862 Altivec/FP/SPE support. Anything else is just a bonus. */
863 #define GET_FEATURE(flag, feature) \
864 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
865 #define GET_FEATURE2(flags, feature) \
867 if ((cpu->env.insns_flags2 & flags) == flags) { \
868 features |= feature; \
871 GET_FEATURE(PPC_64B
, QEMU_PPC_FEATURE_64
);
872 GET_FEATURE(PPC_FLOAT
, QEMU_PPC_FEATURE_HAS_FPU
);
873 GET_FEATURE(PPC_ALTIVEC
, QEMU_PPC_FEATURE_HAS_ALTIVEC
);
874 GET_FEATURE(PPC_SPE
, QEMU_PPC_FEATURE_HAS_SPE
);
875 GET_FEATURE(PPC_SPE_SINGLE
, QEMU_PPC_FEATURE_HAS_EFP_SINGLE
);
876 GET_FEATURE(PPC_SPE_DOUBLE
, QEMU_PPC_FEATURE_HAS_EFP_DOUBLE
);
877 GET_FEATURE(PPC_BOOKE
, QEMU_PPC_FEATURE_BOOKE
);
878 GET_FEATURE(PPC_405_MAC
, QEMU_PPC_FEATURE_HAS_4xxMAC
);
879 GET_FEATURE2(PPC2_DFP
, QEMU_PPC_FEATURE_HAS_DFP
);
880 GET_FEATURE2(PPC2_VSX
, QEMU_PPC_FEATURE_HAS_VSX
);
881 GET_FEATURE2((PPC2_PERM_ISA206
| PPC2_DIVE_ISA206
| PPC2_ATOMIC_ISA206
|
882 PPC2_FP_CVT_ISA206
| PPC2_FP_TST_ISA206
),
883 QEMU_PPC_FEATURE_ARCH_2_06
);
890 #define ELF_HWCAP2 get_elf_hwcap2()
892 static uint32_t get_elf_hwcap2(void)
894 PowerPCCPU
*cpu
= POWERPC_CPU(thread_cpu
);
895 uint32_t features
= 0;
897 #define GET_FEATURE(flag, feature) \
898 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
899 #define GET_FEATURE2(flag, feature) \
900 do { if (cpu->env.insns_flags2 & flag) { features |= feature; } } while (0)
902 GET_FEATURE(PPC_ISEL
, QEMU_PPC_FEATURE2_HAS_ISEL
);
903 GET_FEATURE2(PPC2_BCTAR_ISA207
, QEMU_PPC_FEATURE2_HAS_TAR
);
904 GET_FEATURE2((PPC2_BCTAR_ISA207
| PPC2_LSQ_ISA207
| PPC2_ALTIVEC_207
|
905 PPC2_ISA207S
), QEMU_PPC_FEATURE2_ARCH_2_07
|
906 QEMU_PPC_FEATURE2_VEC_CRYPTO
);
907 GET_FEATURE2(PPC2_ISA300
, QEMU_PPC_FEATURE2_ARCH_3_00
|
908 QEMU_PPC_FEATURE2_DARN
| QEMU_PPC_FEATURE2_HAS_IEEE128
);
909 GET_FEATURE2(PPC2_ISA310
, QEMU_PPC_FEATURE2_ARCH_3_1
|
910 QEMU_PPC_FEATURE2_MMA
);
919 * The requirements here are:
920 * - keep the final alignment of sp (sp & 0xf)
921 * - make sure the 32-bit value at the first 16 byte aligned position of
922 * AUXV is greater than 16 for glibc compatibility.
923 * AT_IGNOREPPC is used for that.
924 * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC,
925 * even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
927 #define DLINFO_ARCH_ITEMS 5
928 #define ARCH_DLINFO \
930 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu); \
932 * Handle glibc compatibility: these magic entries must \
933 * be at the lowest addresses in the final auxv. \
935 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
936 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
937 NEW_AUX_ENT(AT_DCACHEBSIZE, cpu->env.dcache_line_size); \
938 NEW_AUX_ENT(AT_ICACHEBSIZE, cpu->env.icache_line_size); \
939 NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \
942 static inline void init_thread(struct target_pt_regs
*_regs
, struct image_info
*infop
)
944 _regs
->gpr
[1] = infop
->start_stack
;
945 #if defined(TARGET_PPC64)
946 if (get_ppc64_abi(infop
) < 2) {
948 get_user_u64(val
, infop
->entry
+ 8);
949 _regs
->gpr
[2] = val
+ infop
->load_bias
;
950 get_user_u64(val
, infop
->entry
);
951 infop
->entry
= val
+ infop
->load_bias
;
953 _regs
->gpr
[12] = infop
->entry
; /* r12 set to global entry address */
956 _regs
->nip
= infop
->entry
;
959 /* See linux kernel: arch/powerpc/include/asm/elf.h. */
961 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
963 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUPPCState
*env
)
966 target_ulong ccr
= 0;
968 for (i
= 0; i
< ARRAY_SIZE(env
->gpr
); i
++) {
969 (*regs
)[i
] = tswapreg(env
->gpr
[i
]);
972 (*regs
)[32] = tswapreg(env
->nip
);
973 (*regs
)[33] = tswapreg(env
->msr
);
974 (*regs
)[35] = tswapreg(env
->ctr
);
975 (*regs
)[36] = tswapreg(env
->lr
);
976 (*regs
)[37] = tswapreg(cpu_read_xer(env
));
978 ccr
= ppc_get_cr(env
);
979 (*regs
)[38] = tswapreg(ccr
);
982 #define USE_ELF_CORE_DUMP
983 #define ELF_EXEC_PAGESIZE 4096
987 #ifdef TARGET_LOONGARCH64
989 #define ELF_START_MMAP 0x80000000
991 #define ELF_CLASS ELFCLASS64
992 #define ELF_ARCH EM_LOONGARCH
993 #define EXSTACK_DEFAULT true
995 #define elf_check_arch(x) ((x) == EM_LOONGARCH)
997 static inline void init_thread(struct target_pt_regs
*regs
,
998 struct image_info
*infop
)
1000 /*Set crmd PG,DA = 1,0 */
1001 regs
->csr
.crmd
= 2 << 3;
1002 regs
->csr
.era
= infop
->entry
;
1003 regs
->regs
[3] = infop
->start_stack
;
1006 /* See linux kernel: arch/loongarch/include/asm/elf.h */
1008 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1012 TARGET_EF_CSR_ERA
= TARGET_EF_R0
+ 33,
1013 TARGET_EF_CSR_BADV
= TARGET_EF_R0
+ 34,
1016 static void elf_core_copy_regs(target_elf_gregset_t
*regs
,
1017 const CPULoongArchState
*env
)
1021 (*regs
)[TARGET_EF_R0
] = 0;
1023 for (i
= 1; i
< ARRAY_SIZE(env
->gpr
); i
++) {
1024 (*regs
)[TARGET_EF_R0
+ i
] = tswapreg(env
->gpr
[i
]);
1027 (*regs
)[TARGET_EF_CSR_ERA
] = tswapreg(env
->pc
);
1028 (*regs
)[TARGET_EF_CSR_BADV
] = tswapreg(env
->CSR_BADV
);
1031 #define USE_ELF_CORE_DUMP
1032 #define ELF_EXEC_PAGESIZE 4096
1034 #define ELF_HWCAP get_elf_hwcap()
1036 /* See arch/loongarch/include/uapi/asm/hwcap.h */
1038 HWCAP_LOONGARCH_CPUCFG
= (1 << 0),
1039 HWCAP_LOONGARCH_LAM
= (1 << 1),
1040 HWCAP_LOONGARCH_UAL
= (1 << 2),
1041 HWCAP_LOONGARCH_FPU
= (1 << 3),
1042 HWCAP_LOONGARCH_LSX
= (1 << 4),
1043 HWCAP_LOONGARCH_LASX
= (1 << 5),
1044 HWCAP_LOONGARCH_CRC32
= (1 << 6),
1045 HWCAP_LOONGARCH_COMPLEX
= (1 << 7),
1046 HWCAP_LOONGARCH_CRYPTO
= (1 << 8),
1047 HWCAP_LOONGARCH_LVZ
= (1 << 9),
1048 HWCAP_LOONGARCH_LBT_X86
= (1 << 10),
1049 HWCAP_LOONGARCH_LBT_ARM
= (1 << 11),
1050 HWCAP_LOONGARCH_LBT_MIPS
= (1 << 12),
1053 static uint32_t get_elf_hwcap(void)
1055 LoongArchCPU
*cpu
= LOONGARCH_CPU(thread_cpu
);
1056 uint32_t hwcaps
= 0;
1058 hwcaps
|= HWCAP_LOONGARCH_CRC32
;
1060 if (FIELD_EX32(cpu
->env
.cpucfg
[1], CPUCFG1
, UAL
)) {
1061 hwcaps
|= HWCAP_LOONGARCH_UAL
;
1064 if (FIELD_EX32(cpu
->env
.cpucfg
[2], CPUCFG2
, FP
)) {
1065 hwcaps
|= HWCAP_LOONGARCH_FPU
;
1068 if (FIELD_EX32(cpu
->env
.cpucfg
[2], CPUCFG2
, LAM
)) {
1069 hwcaps
|= HWCAP_LOONGARCH_LAM
;
1075 #define ELF_PLATFORM "loongarch"
1077 #endif /* TARGET_LOONGARCH64 */
1081 #define ELF_START_MMAP 0x80000000
1083 #ifdef TARGET_MIPS64
1084 #define ELF_CLASS ELFCLASS64
1086 #define ELF_CLASS ELFCLASS32
1088 #define ELF_ARCH EM_MIPS
1089 #define EXSTACK_DEFAULT true
1091 #ifdef TARGET_ABI_MIPSN32
1092 #define elf_check_abi(x) ((x) & EF_MIPS_ABI2)
1094 #define elf_check_abi(x) (!((x) & EF_MIPS_ABI2))
1097 #define ELF_BASE_PLATFORM get_elf_base_platform()
1099 #define MATCH_PLATFORM_INSN(_flags, _base_platform) \
1100 do { if ((cpu->env.insn_flags & (_flags)) == _flags) \
1101 { return _base_platform; } } while (0)
1103 static const char *get_elf_base_platform(void)
1105 MIPSCPU
*cpu
= MIPS_CPU(thread_cpu
);
1107 /* 64 bit ISAs goes first */
1108 MATCH_PLATFORM_INSN(CPU_MIPS64R6
, "mips64r6");
1109 MATCH_PLATFORM_INSN(CPU_MIPS64R5
, "mips64r5");
1110 MATCH_PLATFORM_INSN(CPU_MIPS64R2
, "mips64r2");
1111 MATCH_PLATFORM_INSN(CPU_MIPS64R1
, "mips64");
1112 MATCH_PLATFORM_INSN(CPU_MIPS5
, "mips5");
1113 MATCH_PLATFORM_INSN(CPU_MIPS4
, "mips4");
1114 MATCH_PLATFORM_INSN(CPU_MIPS3
, "mips3");
1117 MATCH_PLATFORM_INSN(CPU_MIPS32R6
, "mips32r6");
1118 MATCH_PLATFORM_INSN(CPU_MIPS32R5
, "mips32r5");
1119 MATCH_PLATFORM_INSN(CPU_MIPS32R2
, "mips32r2");
1120 MATCH_PLATFORM_INSN(CPU_MIPS32R1
, "mips32");
1121 MATCH_PLATFORM_INSN(CPU_MIPS2
, "mips2");
1126 #undef MATCH_PLATFORM_INSN
1128 static inline void init_thread(struct target_pt_regs
*regs
,
1129 struct image_info
*infop
)
1131 regs
->cp0_status
= 2 << CP0St_KSU
;
1132 regs
->cp0_epc
= infop
->entry
;
1133 regs
->regs
[29] = infop
->start_stack
;
1136 /* See linux kernel: arch/mips/include/asm/elf.h. */
1138 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1140 /* See linux kernel: arch/mips/include/asm/reg.h. */
1142 #ifdef TARGET_MIPS64
1147 TARGET_EF_R26
= TARGET_EF_R0
+ 26,
1148 TARGET_EF_R27
= TARGET_EF_R0
+ 27,
1149 TARGET_EF_LO
= TARGET_EF_R0
+ 32,
1150 TARGET_EF_HI
= TARGET_EF_R0
+ 33,
1151 TARGET_EF_CP0_EPC
= TARGET_EF_R0
+ 34,
1152 TARGET_EF_CP0_BADVADDR
= TARGET_EF_R0
+ 35,
1153 TARGET_EF_CP0_STATUS
= TARGET_EF_R0
+ 36,
1154 TARGET_EF_CP0_CAUSE
= TARGET_EF_R0
+ 37
1157 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1158 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUMIPSState
*env
)
1162 for (i
= 0; i
< TARGET_EF_R0
; i
++) {
1165 (*regs
)[TARGET_EF_R0
] = 0;
1167 for (i
= 1; i
< ARRAY_SIZE(env
->active_tc
.gpr
); i
++) {
1168 (*regs
)[TARGET_EF_R0
+ i
] = tswapreg(env
->active_tc
.gpr
[i
]);
1171 (*regs
)[TARGET_EF_R26
] = 0;
1172 (*regs
)[TARGET_EF_R27
] = 0;
1173 (*regs
)[TARGET_EF_LO
] = tswapreg(env
->active_tc
.LO
[0]);
1174 (*regs
)[TARGET_EF_HI
] = tswapreg(env
->active_tc
.HI
[0]);
1175 (*regs
)[TARGET_EF_CP0_EPC
] = tswapreg(env
->active_tc
.PC
);
1176 (*regs
)[TARGET_EF_CP0_BADVADDR
] = tswapreg(env
->CP0_BadVAddr
);
1177 (*regs
)[TARGET_EF_CP0_STATUS
] = tswapreg(env
->CP0_Status
);
1178 (*regs
)[TARGET_EF_CP0_CAUSE
] = tswapreg(env
->CP0_Cause
);
1181 #define USE_ELF_CORE_DUMP
1182 #define ELF_EXEC_PAGESIZE 4096
1184 /* See arch/mips/include/uapi/asm/hwcap.h. */
1186 HWCAP_MIPS_R6
= (1 << 0),
1187 HWCAP_MIPS_MSA
= (1 << 1),
1188 HWCAP_MIPS_CRC32
= (1 << 2),
1189 HWCAP_MIPS_MIPS16
= (1 << 3),
1190 HWCAP_MIPS_MDMX
= (1 << 4),
1191 HWCAP_MIPS_MIPS3D
= (1 << 5),
1192 HWCAP_MIPS_SMARTMIPS
= (1 << 6),
1193 HWCAP_MIPS_DSP
= (1 << 7),
1194 HWCAP_MIPS_DSP2
= (1 << 8),
1195 HWCAP_MIPS_DSP3
= (1 << 9),
1196 HWCAP_MIPS_MIPS16E2
= (1 << 10),
1197 HWCAP_LOONGSON_MMI
= (1 << 11),
1198 HWCAP_LOONGSON_EXT
= (1 << 12),
1199 HWCAP_LOONGSON_EXT2
= (1 << 13),
1200 HWCAP_LOONGSON_CPUCFG
= (1 << 14),
1203 #define ELF_HWCAP get_elf_hwcap()
1205 #define GET_FEATURE_INSN(_flag, _hwcap) \
1206 do { if (cpu->env.insn_flags & (_flag)) { hwcaps |= _hwcap; } } while (0)
1208 #define GET_FEATURE_REG_SET(_reg, _mask, _hwcap) \
1209 do { if (cpu->env._reg & (_mask)) { hwcaps |= _hwcap; } } while (0)
1211 #define GET_FEATURE_REG_EQU(_reg, _start, _length, _val, _hwcap) \
1213 if (extract32(cpu->env._reg, (_start), (_length)) == (_val)) { \
1218 static uint32_t get_elf_hwcap(void)
1220 MIPSCPU
*cpu
= MIPS_CPU(thread_cpu
);
1221 uint32_t hwcaps
= 0;
1223 GET_FEATURE_REG_EQU(CP0_Config0
, CP0C0_AR
, CP0C0_AR_LENGTH
,
1225 GET_FEATURE_REG_SET(CP0_Config3
, 1 << CP0C3_MSAP
, HWCAP_MIPS_MSA
);
1226 GET_FEATURE_INSN(ASE_LMMI
, HWCAP_LOONGSON_MMI
);
1227 GET_FEATURE_INSN(ASE_LEXT
, HWCAP_LOONGSON_EXT
);
1232 #undef GET_FEATURE_REG_EQU
1233 #undef GET_FEATURE_REG_SET
1234 #undef GET_FEATURE_INSN
1236 #endif /* TARGET_MIPS */
1238 #ifdef TARGET_MICROBLAZE
1240 #define ELF_START_MMAP 0x80000000
1242 #define elf_check_arch(x) ( (x) == EM_MICROBLAZE || (x) == EM_MICROBLAZE_OLD)
1244 #define ELF_CLASS ELFCLASS32
1245 #define ELF_ARCH EM_MICROBLAZE
1247 static inline void init_thread(struct target_pt_regs
*regs
,
1248 struct image_info
*infop
)
1250 regs
->pc
= infop
->entry
;
1251 regs
->r1
= infop
->start_stack
;
1255 #define ELF_EXEC_PAGESIZE 4096
1257 #define USE_ELF_CORE_DUMP
1259 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1261 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1262 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUMBState
*env
)
1266 for (i
= 0; i
< 32; i
++) {
1267 (*regs
)[pos
++] = tswapreg(env
->regs
[i
]);
1270 (*regs
)[pos
++] = tswapreg(env
->pc
);
1271 (*regs
)[pos
++] = tswapreg(mb_cpu_read_msr(env
));
1273 (*regs
)[pos
++] = tswapreg(env
->ear
);
1275 (*regs
)[pos
++] = tswapreg(env
->esr
);
1278 #endif /* TARGET_MICROBLAZE */
1282 #define ELF_START_MMAP 0x80000000
1284 #define elf_check_arch(x) ((x) == EM_ALTERA_NIOS2)
1286 #define ELF_CLASS ELFCLASS32
1287 #define ELF_ARCH EM_ALTERA_NIOS2
1289 static void init_thread(struct target_pt_regs
*regs
, struct image_info
*infop
)
1291 regs
->ea
= infop
->entry
;
1292 regs
->sp
= infop
->start_stack
;
1295 #define LO_COMMPAGE TARGET_PAGE_SIZE
1297 static bool init_guest_commpage(void)
1299 static const uint8_t kuser_page
[4 + 2 * 64] = {
1300 /* __kuser_helper_version */
1301 [0x00] = 0x02, 0x00, 0x00, 0x00,
1303 /* __kuser_cmpxchg */
1304 [0x04] = 0x3a, 0x6c, 0x3b, 0x00, /* trap 16 */
1305 0x3a, 0x28, 0x00, 0xf8, /* ret */
1307 /* __kuser_sigtramp */
1308 [0x44] = 0xc4, 0x22, 0x80, 0x00, /* movi r2, __NR_rt_sigreturn */
1309 0x3a, 0x68, 0x3b, 0x00, /* trap 0 */
1312 void *want
= g2h_untagged(LO_COMMPAGE
& -qemu_host_page_size
);
1313 void *addr
= mmap(want
, qemu_host_page_size
, PROT_READ
| PROT_WRITE
,
1314 MAP_ANONYMOUS
| MAP_PRIVATE
| MAP_FIXED
, -1, 0);
1316 if (addr
== MAP_FAILED
) {
1317 perror("Allocating guest commpage");
1324 memcpy(addr
, kuser_page
, sizeof(kuser_page
));
1326 if (mprotect(addr
, qemu_host_page_size
, PROT_READ
)) {
1327 perror("Protecting guest commpage");
1331 page_set_flags(LO_COMMPAGE
, LO_COMMPAGE
| ~TARGET_PAGE_MASK
,
1332 PAGE_READ
| PAGE_EXEC
| PAGE_VALID
);
1336 #define ELF_EXEC_PAGESIZE 4096
1338 #define USE_ELF_CORE_DUMP
1340 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1342 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1343 static void elf_core_copy_regs(target_elf_gregset_t
*regs
,
1344 const CPUNios2State
*env
)
1349 for (i
= 1; i
< 8; i
++) /* r0-r7 */
1350 (*regs
)[i
] = tswapreg(env
->regs
[i
+ 7]);
1352 for (i
= 8; i
< 16; i
++) /* r8-r15 */
1353 (*regs
)[i
] = tswapreg(env
->regs
[i
- 8]);
1355 for (i
= 16; i
< 24; i
++) /* r16-r23 */
1356 (*regs
)[i
] = tswapreg(env
->regs
[i
+ 7]);
1357 (*regs
)[24] = -1; /* R_ET */
1358 (*regs
)[25] = -1; /* R_BT */
1359 (*regs
)[26] = tswapreg(env
->regs
[R_GP
]);
1360 (*regs
)[27] = tswapreg(env
->regs
[R_SP
]);
1361 (*regs
)[28] = tswapreg(env
->regs
[R_FP
]);
1362 (*regs
)[29] = tswapreg(env
->regs
[R_EA
]);
1363 (*regs
)[30] = -1; /* R_SSTATUS */
1364 (*regs
)[31] = tswapreg(env
->regs
[R_RA
]);
1366 (*regs
)[32] = tswapreg(env
->pc
);
1368 (*regs
)[33] = -1; /* R_STATUS */
1369 (*regs
)[34] = tswapreg(env
->regs
[CR_ESTATUS
]);
1371 for (i
= 35; i
< 49; i
++) /* ... */
1375 #endif /* TARGET_NIOS2 */
1377 #ifdef TARGET_OPENRISC
1379 #define ELF_START_MMAP 0x08000000
1381 #define ELF_ARCH EM_OPENRISC
1382 #define ELF_CLASS ELFCLASS32
1383 #define ELF_DATA ELFDATA2MSB
1385 static inline void init_thread(struct target_pt_regs
*regs
,
1386 struct image_info
*infop
)
1388 regs
->pc
= infop
->entry
;
1389 regs
->gpr
[1] = infop
->start_stack
;
1392 #define USE_ELF_CORE_DUMP
1393 #define ELF_EXEC_PAGESIZE 8192
1395 /* See linux kernel arch/openrisc/include/asm/elf.h. */
1396 #define ELF_NREG 34 /* gprs and pc, sr */
1397 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1399 static void elf_core_copy_regs(target_elf_gregset_t
*regs
,
1400 const CPUOpenRISCState
*env
)
1404 for (i
= 0; i
< 32; i
++) {
1405 (*regs
)[i
] = tswapreg(cpu_get_gpr(env
, i
));
1407 (*regs
)[32] = tswapreg(env
->pc
);
1408 (*regs
)[33] = tswapreg(cpu_get_sr(env
));
1411 #define ELF_PLATFORM NULL
1413 #endif /* TARGET_OPENRISC */
1417 #define ELF_START_MMAP 0x80000000
1419 #define ELF_CLASS ELFCLASS32
1420 #define ELF_ARCH EM_SH
1422 static inline void init_thread(struct target_pt_regs
*regs
,
1423 struct image_info
*infop
)
1425 /* Check other registers XXXXX */
1426 regs
->pc
= infop
->entry
;
1427 regs
->regs
[15] = infop
->start_stack
;
1430 /* See linux kernel: arch/sh/include/asm/elf.h. */
1432 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1434 /* See linux kernel: arch/sh/include/asm/ptrace.h. */
1439 TARGET_REG_GBR
= 19,
1440 TARGET_REG_MACH
= 20,
1441 TARGET_REG_MACL
= 21,
1442 TARGET_REG_SYSCALL
= 22
1445 static inline void elf_core_copy_regs(target_elf_gregset_t
*regs
,
1446 const CPUSH4State
*env
)
1450 for (i
= 0; i
< 16; i
++) {
1451 (*regs
)[i
] = tswapreg(env
->gregs
[i
]);
1454 (*regs
)[TARGET_REG_PC
] = tswapreg(env
->pc
);
1455 (*regs
)[TARGET_REG_PR
] = tswapreg(env
->pr
);
1456 (*regs
)[TARGET_REG_SR
] = tswapreg(env
->sr
);
1457 (*regs
)[TARGET_REG_GBR
] = tswapreg(env
->gbr
);
1458 (*regs
)[TARGET_REG_MACH
] = tswapreg(env
->mach
);
1459 (*regs
)[TARGET_REG_MACL
] = tswapreg(env
->macl
);
1460 (*regs
)[TARGET_REG_SYSCALL
] = 0; /* FIXME */
1463 #define USE_ELF_CORE_DUMP
1464 #define ELF_EXEC_PAGESIZE 4096
1467 SH_CPU_HAS_FPU
= 0x0001, /* Hardware FPU support */
1468 SH_CPU_HAS_P2_FLUSH_BUG
= 0x0002, /* Need to flush the cache in P2 area */
1469 SH_CPU_HAS_MMU_PAGE_ASSOC
= 0x0004, /* SH3: TLB way selection bit support */
1470 SH_CPU_HAS_DSP
= 0x0008, /* SH-DSP: DSP support */
1471 SH_CPU_HAS_PERF_COUNTER
= 0x0010, /* Hardware performance counters */
1472 SH_CPU_HAS_PTEA
= 0x0020, /* PTEA register */
1473 SH_CPU_HAS_LLSC
= 0x0040, /* movli.l/movco.l */
1474 SH_CPU_HAS_L2_CACHE
= 0x0080, /* Secondary cache / URAM */
1475 SH_CPU_HAS_OP32
= 0x0100, /* 32-bit instruction support */
1476 SH_CPU_HAS_PTEAEX
= 0x0200, /* PTE ASID Extension support */
1479 #define ELF_HWCAP get_elf_hwcap()
1481 static uint32_t get_elf_hwcap(void)
1483 SuperHCPU
*cpu
= SUPERH_CPU(thread_cpu
);
1486 hwcap
|= SH_CPU_HAS_FPU
;
1488 if (cpu
->env
.features
& SH_FEATURE_SH4A
) {
1489 hwcap
|= SH_CPU_HAS_LLSC
;
1499 #define ELF_START_MMAP 0x80000000
1501 #define ELF_CLASS ELFCLASS32
1502 #define ELF_ARCH EM_CRIS
1504 static inline void init_thread(struct target_pt_regs
*regs
,
1505 struct image_info
*infop
)
1507 regs
->erp
= infop
->entry
;
1510 #define ELF_EXEC_PAGESIZE 8192
1516 #define ELF_START_MMAP 0x80000000
1518 #define ELF_CLASS ELFCLASS32
1519 #define ELF_ARCH EM_68K
1521 /* ??? Does this need to do anything?
1522 #define ELF_PLAT_INIT(_r) */
1524 static inline void init_thread(struct target_pt_regs
*regs
,
1525 struct image_info
*infop
)
1527 regs
->usp
= infop
->start_stack
;
1529 regs
->pc
= infop
->entry
;
1532 /* See linux kernel: arch/m68k/include/asm/elf.h. */
1534 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1536 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUM68KState
*env
)
1538 (*regs
)[0] = tswapreg(env
->dregs
[1]);
1539 (*regs
)[1] = tswapreg(env
->dregs
[2]);
1540 (*regs
)[2] = tswapreg(env
->dregs
[3]);
1541 (*regs
)[3] = tswapreg(env
->dregs
[4]);
1542 (*regs
)[4] = tswapreg(env
->dregs
[5]);
1543 (*regs
)[5] = tswapreg(env
->dregs
[6]);
1544 (*regs
)[6] = tswapreg(env
->dregs
[7]);
1545 (*regs
)[7] = tswapreg(env
->aregs
[0]);
1546 (*regs
)[8] = tswapreg(env
->aregs
[1]);
1547 (*regs
)[9] = tswapreg(env
->aregs
[2]);
1548 (*regs
)[10] = tswapreg(env
->aregs
[3]);
1549 (*regs
)[11] = tswapreg(env
->aregs
[4]);
1550 (*regs
)[12] = tswapreg(env
->aregs
[5]);
1551 (*regs
)[13] = tswapreg(env
->aregs
[6]);
1552 (*regs
)[14] = tswapreg(env
->dregs
[0]);
1553 (*regs
)[15] = tswapreg(env
->aregs
[7]);
1554 (*regs
)[16] = tswapreg(env
->dregs
[0]); /* FIXME: orig_d0 */
1555 (*regs
)[17] = tswapreg(env
->sr
);
1556 (*regs
)[18] = tswapreg(env
->pc
);
1557 (*regs
)[19] = 0; /* FIXME: regs->format | regs->vector */
1560 #define USE_ELF_CORE_DUMP
1561 #define ELF_EXEC_PAGESIZE 8192
1567 #define ELF_START_MMAP (0x30000000000ULL)
1569 #define ELF_CLASS ELFCLASS64
1570 #define ELF_ARCH EM_ALPHA
1572 static inline void init_thread(struct target_pt_regs
*regs
,
1573 struct image_info
*infop
)
1575 regs
->pc
= infop
->entry
;
1577 regs
->usp
= infop
->start_stack
;
1580 #define ELF_EXEC_PAGESIZE 8192
1582 #endif /* TARGET_ALPHA */
1586 #define ELF_START_MMAP (0x20000000000ULL)
1588 #define ELF_CLASS ELFCLASS64
1589 #define ELF_DATA ELFDATA2MSB
1590 #define ELF_ARCH EM_S390
1594 #define ELF_HWCAP get_elf_hwcap()
1596 #define GET_FEATURE(_feat, _hwcap) \
1597 do { if (s390_has_feat(_feat)) { hwcap |= _hwcap; } } while (0)
1599 uint32_t get_elf_hwcap(void)
1602 * Let's assume we always have esan3 and zarch.
1603 * 31-bit processes can use 64-bit registers (high gprs).
1605 uint32_t hwcap
= HWCAP_S390_ESAN3
| HWCAP_S390_ZARCH
| HWCAP_S390_HIGH_GPRS
;
1607 GET_FEATURE(S390_FEAT_STFLE
, HWCAP_S390_STFLE
);
1608 GET_FEATURE(S390_FEAT_MSA
, HWCAP_S390_MSA
);
1609 GET_FEATURE(S390_FEAT_LONG_DISPLACEMENT
, HWCAP_S390_LDISP
);
1610 GET_FEATURE(S390_FEAT_EXTENDED_IMMEDIATE
, HWCAP_S390_EIMM
);
1611 if (s390_has_feat(S390_FEAT_EXTENDED_TRANSLATION_3
) &&
1612 s390_has_feat(S390_FEAT_ETF3_ENH
)) {
1613 hwcap
|= HWCAP_S390_ETF3EH
;
1615 GET_FEATURE(S390_FEAT_VECTOR
, HWCAP_S390_VXRS
);
1616 GET_FEATURE(S390_FEAT_VECTOR_ENH
, HWCAP_S390_VXRS_EXT
);
1621 const char *elf_hwcap_str(uint32_t bit
)
1623 static const char *hwcap_str
[] = {
1624 [HWCAP_S390_NR_ESAN3
] = "esan3",
1625 [HWCAP_S390_NR_ZARCH
] = "zarch",
1626 [HWCAP_S390_NR_STFLE
] = "stfle",
1627 [HWCAP_S390_NR_MSA
] = "msa",
1628 [HWCAP_S390_NR_LDISP
] = "ldisp",
1629 [HWCAP_S390_NR_EIMM
] = "eimm",
1630 [HWCAP_S390_NR_DFP
] = "dfp",
1631 [HWCAP_S390_NR_HPAGE
] = "edat",
1632 [HWCAP_S390_NR_ETF3EH
] = "etf3eh",
1633 [HWCAP_S390_NR_HIGH_GPRS
] = "highgprs",
1634 [HWCAP_S390_NR_TE
] = "te",
1635 [HWCAP_S390_NR_VXRS
] = "vx",
1636 [HWCAP_S390_NR_VXRS_BCD
] = "vxd",
1637 [HWCAP_S390_NR_VXRS_EXT
] = "vxe",
1638 [HWCAP_S390_NR_GS
] = "gs",
1639 [HWCAP_S390_NR_VXRS_EXT2
] = "vxe2",
1640 [HWCAP_S390_NR_VXRS_PDE
] = "vxp",
1641 [HWCAP_S390_NR_SORT
] = "sort",
1642 [HWCAP_S390_NR_DFLT
] = "dflt",
1643 [HWCAP_S390_NR_NNPA
] = "nnpa",
1644 [HWCAP_S390_NR_PCI_MIO
] = "pcimio",
1645 [HWCAP_S390_NR_SIE
] = "sie",
1648 return bit
< ARRAY_SIZE(hwcap_str
) ? hwcap_str
[bit
] : NULL
;
1651 static inline void init_thread(struct target_pt_regs
*regs
, struct image_info
*infop
)
1653 regs
->psw
.addr
= infop
->entry
;
1654 regs
->psw
.mask
= PSW_MASK_DAT
| PSW_MASK_IO
| PSW_MASK_EXT
| \
1655 PSW_MASK_MCHECK
| PSW_MASK_PSTATE
| PSW_MASK_64
| \
1657 regs
->gprs
[15] = infop
->start_stack
;
1660 /* See linux kernel: arch/s390/include/uapi/asm/ptrace.h (s390_regs). */
1662 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1665 TARGET_REG_PSWM
= 0,
1666 TARGET_REG_PSWA
= 1,
1667 TARGET_REG_GPRS
= 2,
1668 TARGET_REG_ARS
= 18,
1669 TARGET_REG_ORIG_R2
= 26,
1672 static void elf_core_copy_regs(target_elf_gregset_t
*regs
,
1673 const CPUS390XState
*env
)
1678 (*regs
)[TARGET_REG_PSWM
] = tswapreg(env
->psw
.mask
);
1679 (*regs
)[TARGET_REG_PSWA
] = tswapreg(env
->psw
.addr
);
1680 for (i
= 0; i
< 16; i
++) {
1681 (*regs
)[TARGET_REG_GPRS
+ i
] = tswapreg(env
->regs
[i
]);
1683 aregs
= (uint32_t *)&((*regs
)[TARGET_REG_ARS
]);
1684 for (i
= 0; i
< 16; i
++) {
1685 aregs
[i
] = tswap32(env
->aregs
[i
]);
1687 (*regs
)[TARGET_REG_ORIG_R2
] = 0;
1690 #define USE_ELF_CORE_DUMP
1691 #define ELF_EXEC_PAGESIZE 4096
1693 #endif /* TARGET_S390X */
1697 #define ELF_START_MMAP 0x80000000
1698 #define ELF_ARCH EM_RISCV
1700 #ifdef TARGET_RISCV32
1701 #define ELF_CLASS ELFCLASS32
1703 #define ELF_CLASS ELFCLASS64
1706 #define ELF_HWCAP get_elf_hwcap()
1708 static uint32_t get_elf_hwcap(void)
1710 #define MISA_BIT(EXT) (1 << (EXT - 'A'))
1711 RISCVCPU
*cpu
= RISCV_CPU(thread_cpu
);
1712 uint32_t mask
= MISA_BIT('I') | MISA_BIT('M') | MISA_BIT('A')
1713 | MISA_BIT('F') | MISA_BIT('D') | MISA_BIT('C')
1716 return cpu
->env
.misa_ext
& mask
;
1720 static inline void init_thread(struct target_pt_regs
*regs
,
1721 struct image_info
*infop
)
1723 regs
->sepc
= infop
->entry
;
1724 regs
->sp
= infop
->start_stack
;
1727 #define ELF_EXEC_PAGESIZE 4096
1729 #endif /* TARGET_RISCV */
1733 #define ELF_START_MMAP 0x80000000
1734 #define ELF_CLASS ELFCLASS32
1735 #define ELF_ARCH EM_PARISC
1736 #define ELF_PLATFORM "PARISC"
1737 #define STACK_GROWS_DOWN 0
1738 #define STACK_ALIGNMENT 64
1740 static inline void init_thread(struct target_pt_regs
*regs
,
1741 struct image_info
*infop
)
1743 regs
->iaoq
[0] = infop
->entry
;
1744 regs
->iaoq
[1] = infop
->entry
+ 4;
1746 regs
->gr
[24] = infop
->argv
;
1747 regs
->gr
[25] = infop
->argc
;
1748 /* The top-of-stack contains a linkage buffer. */
1749 regs
->gr
[30] = infop
->start_stack
+ 64;
1750 regs
->gr
[31] = infop
->entry
;
1753 #define LO_COMMPAGE 0
1755 static bool init_guest_commpage(void)
1757 void *want
= g2h_untagged(LO_COMMPAGE
);
1758 void *addr
= mmap(want
, qemu_host_page_size
, PROT_NONE
,
1759 MAP_ANONYMOUS
| MAP_PRIVATE
| MAP_FIXED
, -1, 0);
1761 if (addr
== MAP_FAILED
) {
1762 perror("Allocating guest commpage");
1770 * On Linux, page zero is normally marked execute only + gateway.
1771 * Normal read or write is supposed to fail (thus PROT_NONE above),
1772 * but specific offsets have kernel code mapped to raise permissions
1773 * and implement syscalls. Here, simply mark the page executable.
1774 * Special case the entry points during translation (see do_page_zero).
1776 page_set_flags(LO_COMMPAGE
, LO_COMMPAGE
| ~TARGET_PAGE_MASK
,
1777 PAGE_EXEC
| PAGE_VALID
);
1781 #endif /* TARGET_HPPA */
1783 #ifdef TARGET_XTENSA
1785 #define ELF_START_MMAP 0x20000000
1787 #define ELF_CLASS ELFCLASS32
1788 #define ELF_ARCH EM_XTENSA
1790 static inline void init_thread(struct target_pt_regs
*regs
,
1791 struct image_info
*infop
)
1793 regs
->windowbase
= 0;
1794 regs
->windowstart
= 1;
1795 regs
->areg
[1] = infop
->start_stack
;
1796 regs
->pc
= infop
->entry
;
1797 if (info_is_fdpic(infop
)) {
1798 regs
->areg
[4] = infop
->loadmap_addr
;
1799 regs
->areg
[5] = infop
->interpreter_loadmap_addr
;
1800 if (infop
->interpreter_loadmap_addr
) {
1801 regs
->areg
[6] = infop
->interpreter_pt_dynamic_addr
;
1803 regs
->areg
[6] = infop
->pt_dynamic_addr
;
1808 /* See linux kernel: arch/xtensa/include/asm/elf.h. */
1809 #define ELF_NREG 128
1810 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1819 TARGET_REG_WINDOWSTART
,
1820 TARGET_REG_WINDOWBASE
,
1821 TARGET_REG_THREADPTR
,
1822 TARGET_REG_AR0
= 64,
1825 static void elf_core_copy_regs(target_elf_gregset_t
*regs
,
1826 const CPUXtensaState
*env
)
1830 (*regs
)[TARGET_REG_PC
] = tswapreg(env
->pc
);
1831 (*regs
)[TARGET_REG_PS
] = tswapreg(env
->sregs
[PS
] & ~PS_EXCM
);
1832 (*regs
)[TARGET_REG_LBEG
] = tswapreg(env
->sregs
[LBEG
]);
1833 (*regs
)[TARGET_REG_LEND
] = tswapreg(env
->sregs
[LEND
]);
1834 (*regs
)[TARGET_REG_LCOUNT
] = tswapreg(env
->sregs
[LCOUNT
]);
1835 (*regs
)[TARGET_REG_SAR
] = tswapreg(env
->sregs
[SAR
]);
1836 (*regs
)[TARGET_REG_WINDOWSTART
] = tswapreg(env
->sregs
[WINDOW_START
]);
1837 (*regs
)[TARGET_REG_WINDOWBASE
] = tswapreg(env
->sregs
[WINDOW_BASE
]);
1838 (*regs
)[TARGET_REG_THREADPTR
] = tswapreg(env
->uregs
[THREADPTR
]);
1839 xtensa_sync_phys_from_window((CPUXtensaState
*)env
);
1840 for (i
= 0; i
< env
->config
->nareg
; ++i
) {
1841 (*regs
)[TARGET_REG_AR0
+ i
] = tswapreg(env
->phys_regs
[i
]);
1845 #define USE_ELF_CORE_DUMP
1846 #define ELF_EXEC_PAGESIZE 4096
1848 #endif /* TARGET_XTENSA */
1850 #ifdef TARGET_HEXAGON
1852 #define ELF_START_MMAP 0x20000000
1854 #define ELF_CLASS ELFCLASS32
1855 #define ELF_ARCH EM_HEXAGON
1857 static inline void init_thread(struct target_pt_regs
*regs
,
1858 struct image_info
*infop
)
1860 regs
->sepc
= infop
->entry
;
1861 regs
->sp
= infop
->start_stack
;
1864 #endif /* TARGET_HEXAGON */
1866 #ifndef ELF_BASE_PLATFORM
1867 #define ELF_BASE_PLATFORM (NULL)
1870 #ifndef ELF_PLATFORM
1871 #define ELF_PLATFORM (NULL)
1875 #define ELF_MACHINE ELF_ARCH
1878 #ifndef elf_check_arch
1879 #define elf_check_arch(x) ((x) == ELF_ARCH)
1882 #ifndef elf_check_abi
1883 #define elf_check_abi(x) (1)
1890 #ifndef STACK_GROWS_DOWN
1891 #define STACK_GROWS_DOWN 1
1894 #ifndef STACK_ALIGNMENT
1895 #define STACK_ALIGNMENT 16
1900 #define ELF_CLASS ELFCLASS32
1902 #define bswaptls(ptr) bswap32s(ptr)
1905 #ifndef EXSTACK_DEFAULT
1906 #define EXSTACK_DEFAULT false
1911 /* We must delay the following stanzas until after "elf.h". */
1912 #if defined(TARGET_AARCH64)
1914 static bool arch_parse_elf_property(uint32_t pr_type
, uint32_t pr_datasz
,
1915 const uint32_t *data
,
1916 struct image_info
*info
,
1919 if (pr_type
== GNU_PROPERTY_AARCH64_FEATURE_1_AND
) {
1920 if (pr_datasz
!= sizeof(uint32_t)) {
1921 error_setg(errp
, "Ill-formed GNU_PROPERTY_AARCH64_FEATURE_1_AND");
1924 /* We will extract GNU_PROPERTY_AARCH64_FEATURE_1_BTI later. */
1925 info
->note_flags
= *data
;
1929 #define ARCH_USE_GNU_PROPERTY 1
1933 static bool arch_parse_elf_property(uint32_t pr_type
, uint32_t pr_datasz
,
1934 const uint32_t *data
,
1935 struct image_info
*info
,
1938 g_assert_not_reached();
1940 #define ARCH_USE_GNU_PROPERTY 0
1946 unsigned int a_info
; /* Use macros N_MAGIC, etc for access */
1947 unsigned int a_text
; /* length of text, in bytes */
1948 unsigned int a_data
; /* length of data, in bytes */
1949 unsigned int a_bss
; /* length of uninitialized data area, in bytes */
1950 unsigned int a_syms
; /* length of symbol table data in file, in bytes */
1951 unsigned int a_entry
; /* start address */
1952 unsigned int a_trsize
; /* length of relocation info for text, in bytes */
1953 unsigned int a_drsize
; /* length of relocation info for data, in bytes */
1957 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
1963 /* Necessary parameters */
1964 #define TARGET_ELF_EXEC_PAGESIZE \
1965 (((eppnt->p_align & ~qemu_host_page_mask) != 0) ? \
1966 TARGET_PAGE_SIZE : MAX(qemu_host_page_size, TARGET_PAGE_SIZE))
1967 #define TARGET_ELF_PAGELENGTH(_v) ROUND_UP((_v), TARGET_ELF_EXEC_PAGESIZE)
1968 #define TARGET_ELF_PAGESTART(_v) ((_v) & \
1969 ~(abi_ulong)(TARGET_ELF_EXEC_PAGESIZE-1))
1970 #define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
1972 #define DLINFO_ITEMS 16
1974 static inline void memcpy_fromfs(void * to
, const void * from
, unsigned long n
)
1976 memcpy(to
, from
, n
);
1980 static void bswap_ehdr(struct elfhdr
*ehdr
)
1982 bswap16s(&ehdr
->e_type
); /* Object file type */
1983 bswap16s(&ehdr
->e_machine
); /* Architecture */
1984 bswap32s(&ehdr
->e_version
); /* Object file version */
1985 bswaptls(&ehdr
->e_entry
); /* Entry point virtual address */
1986 bswaptls(&ehdr
->e_phoff
); /* Program header table file offset */
1987 bswaptls(&ehdr
->e_shoff
); /* Section header table file offset */
1988 bswap32s(&ehdr
->e_flags
); /* Processor-specific flags */
1989 bswap16s(&ehdr
->e_ehsize
); /* ELF header size in bytes */
1990 bswap16s(&ehdr
->e_phentsize
); /* Program header table entry size */
1991 bswap16s(&ehdr
->e_phnum
); /* Program header table entry count */
1992 bswap16s(&ehdr
->e_shentsize
); /* Section header table entry size */
1993 bswap16s(&ehdr
->e_shnum
); /* Section header table entry count */
1994 bswap16s(&ehdr
->e_shstrndx
); /* Section header string table index */
1997 static void bswap_phdr(struct elf_phdr
*phdr
, int phnum
)
2000 for (i
= 0; i
< phnum
; ++i
, ++phdr
) {
2001 bswap32s(&phdr
->p_type
); /* Segment type */
2002 bswap32s(&phdr
->p_flags
); /* Segment flags */
2003 bswaptls(&phdr
->p_offset
); /* Segment file offset */
2004 bswaptls(&phdr
->p_vaddr
); /* Segment virtual address */
2005 bswaptls(&phdr
->p_paddr
); /* Segment physical address */
2006 bswaptls(&phdr
->p_filesz
); /* Segment size in file */
2007 bswaptls(&phdr
->p_memsz
); /* Segment size in memory */
2008 bswaptls(&phdr
->p_align
); /* Segment alignment */
2012 static void bswap_shdr(struct elf_shdr
*shdr
, int shnum
)
2015 for (i
= 0; i
< shnum
; ++i
, ++shdr
) {
2016 bswap32s(&shdr
->sh_name
);
2017 bswap32s(&shdr
->sh_type
);
2018 bswaptls(&shdr
->sh_flags
);
2019 bswaptls(&shdr
->sh_addr
);
2020 bswaptls(&shdr
->sh_offset
);
2021 bswaptls(&shdr
->sh_size
);
2022 bswap32s(&shdr
->sh_link
);
2023 bswap32s(&shdr
->sh_info
);
2024 bswaptls(&shdr
->sh_addralign
);
2025 bswaptls(&shdr
->sh_entsize
);
2029 static void bswap_sym(struct elf_sym
*sym
)
2031 bswap32s(&sym
->st_name
);
2032 bswaptls(&sym
->st_value
);
2033 bswaptls(&sym
->st_size
);
2034 bswap16s(&sym
->st_shndx
);
2038 static void bswap_mips_abiflags(Mips_elf_abiflags_v0
*abiflags
)
2040 bswap16s(&abiflags
->version
);
2041 bswap32s(&abiflags
->ases
);
2042 bswap32s(&abiflags
->isa_ext
);
2043 bswap32s(&abiflags
->flags1
);
2044 bswap32s(&abiflags
->flags2
);
2048 static inline void bswap_ehdr(struct elfhdr
*ehdr
) { }
2049 static inline void bswap_phdr(struct elf_phdr
*phdr
, int phnum
) { }
2050 static inline void bswap_shdr(struct elf_shdr
*shdr
, int shnum
) { }
2051 static inline void bswap_sym(struct elf_sym
*sym
) { }
2053 static inline void bswap_mips_abiflags(Mips_elf_abiflags_v0
*abiflags
) { }
2057 #ifdef USE_ELF_CORE_DUMP
2058 static int elf_core_dump(int, const CPUArchState
*);
2059 #endif /* USE_ELF_CORE_DUMP */
2060 static void load_symbols(struct elfhdr
*hdr
, int fd
, abi_ulong load_bias
);
2062 /* Verify the portions of EHDR within E_IDENT for the target.
2063 This can be performed before bswapping the entire header. */
2064 static bool elf_check_ident(struct elfhdr
*ehdr
)
2066 return (ehdr
->e_ident
[EI_MAG0
] == ELFMAG0
2067 && ehdr
->e_ident
[EI_MAG1
] == ELFMAG1
2068 && ehdr
->e_ident
[EI_MAG2
] == ELFMAG2
2069 && ehdr
->e_ident
[EI_MAG3
] == ELFMAG3
2070 && ehdr
->e_ident
[EI_CLASS
] == ELF_CLASS
2071 && ehdr
->e_ident
[EI_DATA
] == ELF_DATA
2072 && ehdr
->e_ident
[EI_VERSION
] == EV_CURRENT
);
2075 /* Verify the portions of EHDR outside of E_IDENT for the target.
2076 This has to wait until after bswapping the header. */
2077 static bool elf_check_ehdr(struct elfhdr
*ehdr
)
2079 return (elf_check_arch(ehdr
->e_machine
)
2080 && elf_check_abi(ehdr
->e_flags
)
2081 && ehdr
->e_ehsize
== sizeof(struct elfhdr
)
2082 && ehdr
->e_phentsize
== sizeof(struct elf_phdr
)
2083 && (ehdr
->e_type
== ET_EXEC
|| ehdr
->e_type
== ET_DYN
));
2087 * 'copy_elf_strings()' copies argument/envelope strings from user
2088 * memory to free pages in kernel mem. These are in a format ready
2089 * to be put directly into the top of new user memory.
2092 static abi_ulong
copy_elf_strings(int argc
, char **argv
, char *scratch
,
2093 abi_ulong p
, abi_ulong stack_limit
)
2100 return 0; /* bullet-proofing */
2103 if (STACK_GROWS_DOWN
) {
2104 int offset
= ((p
- 1) % TARGET_PAGE_SIZE
) + 1;
2105 for (i
= argc
- 1; i
>= 0; --i
) {
2108 fprintf(stderr
, "VFS: argc is wrong");
2111 len
= strlen(tmp
) + 1;
2114 if (len
> (p
- stack_limit
)) {
2118 int bytes_to_copy
= (len
> offset
) ? offset
: len
;
2119 tmp
-= bytes_to_copy
;
2121 offset
-= bytes_to_copy
;
2122 len
-= bytes_to_copy
;
2124 memcpy_fromfs(scratch
+ offset
, tmp
, bytes_to_copy
);
2127 memcpy_to_target(p
, scratch
, top
- p
);
2129 offset
= TARGET_PAGE_SIZE
;
2134 memcpy_to_target(p
, scratch
+ offset
, top
- p
);
2137 int remaining
= TARGET_PAGE_SIZE
- (p
% TARGET_PAGE_SIZE
);
2138 for (i
= 0; i
< argc
; ++i
) {
2141 fprintf(stderr
, "VFS: argc is wrong");
2144 len
= strlen(tmp
) + 1;
2145 if (len
> (stack_limit
- p
)) {
2149 int bytes_to_copy
= (len
> remaining
) ? remaining
: len
;
2151 memcpy_fromfs(scratch
+ (p
- top
), tmp
, bytes_to_copy
);
2153 tmp
+= bytes_to_copy
;
2154 remaining
-= bytes_to_copy
;
2156 len
-= bytes_to_copy
;
2158 if (remaining
== 0) {
2159 memcpy_to_target(top
, scratch
, p
- top
);
2161 remaining
= TARGET_PAGE_SIZE
;
2166 memcpy_to_target(top
, scratch
, p
- top
);
2173 /* Older linux kernels provide up to MAX_ARG_PAGES (default: 32) of
2174 * argument/environment space. Newer kernels (>2.6.33) allow more,
2175 * dependent on stack size, but guarantee at least 32 pages for
2176 * backwards compatibility.
2178 #define STACK_LOWER_LIMIT (32 * TARGET_PAGE_SIZE)
2180 static abi_ulong
setup_arg_pages(struct linux_binprm
*bprm
,
2181 struct image_info
*info
)
2183 abi_ulong size
, error
, guard
;
2186 size
= guest_stack_size
;
2187 if (size
< STACK_LOWER_LIMIT
) {
2188 size
= STACK_LOWER_LIMIT
;
2191 if (STACK_GROWS_DOWN
) {
2192 guard
= TARGET_PAGE_SIZE
;
2193 if (guard
< qemu_real_host_page_size()) {
2194 guard
= qemu_real_host_page_size();
2197 /* no guard page for hppa target where stack grows upwards. */
2201 prot
= PROT_READ
| PROT_WRITE
;
2202 if (info
->exec_stack
) {
2205 error
= target_mmap(0, size
+ guard
, prot
,
2206 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
2208 perror("mmap stack");
2212 /* We reserve one extra page at the top of the stack as guard. */
2213 if (STACK_GROWS_DOWN
) {
2214 target_mprotect(error
, guard
, PROT_NONE
);
2215 info
->stack_limit
= error
+ guard
;
2216 return info
->stack_limit
+ size
- sizeof(void *);
2218 info
->stack_limit
= error
+ size
;
2223 /* Map and zero the bss. We need to explicitly zero any fractional pages
2224 after the data section (i.e. bss). */
2225 static void zero_bss(abi_ulong elf_bss
, abi_ulong last_bss
, int prot
)
2227 uintptr_t host_start
, host_map_start
, host_end
;
2229 last_bss
= TARGET_PAGE_ALIGN(last_bss
);
2231 /* ??? There is confusion between qemu_real_host_page_size and
2232 qemu_host_page_size here and elsewhere in target_mmap, which
2233 may lead to the end of the data section mapping from the file
2234 not being mapped. At least there was an explicit test and
2235 comment for that here, suggesting that "the file size must
2236 be known". The comment probably pre-dates the introduction
2237 of the fstat system call in target_mmap which does in fact
2238 find out the size. What isn't clear is if the workaround
2239 here is still actually needed. For now, continue with it,
2240 but merge it with the "normal" mmap that would allocate the bss. */
2242 host_start
= (uintptr_t) g2h_untagged(elf_bss
);
2243 host_end
= (uintptr_t) g2h_untagged(last_bss
);
2244 host_map_start
= REAL_HOST_PAGE_ALIGN(host_start
);
2246 if (host_map_start
< host_end
) {
2247 void *p
= mmap((void *)host_map_start
, host_end
- host_map_start
,
2248 prot
, MAP_FIXED
| MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
2249 if (p
== MAP_FAILED
) {
2250 perror("cannot mmap brk");
2255 /* Ensure that the bss page(s) are valid */
2256 if ((page_get_flags(last_bss
-1) & prot
) != prot
) {
2257 page_set_flags(elf_bss
& TARGET_PAGE_MASK
, last_bss
- 1,
2261 if (host_start
< host_map_start
) {
2262 memset((void *)host_start
, 0, host_map_start
- host_start
);
2266 #if defined(TARGET_ARM)
2267 static int elf_is_fdpic(struct elfhdr
*exec
)
2269 return exec
->e_ident
[EI_OSABI
] == ELFOSABI_ARM_FDPIC
;
2271 #elif defined(TARGET_XTENSA)
2272 static int elf_is_fdpic(struct elfhdr
*exec
)
2274 return exec
->e_ident
[EI_OSABI
] == ELFOSABI_XTENSA_FDPIC
;
2277 /* Default implementation, always false. */
2278 static int elf_is_fdpic(struct elfhdr
*exec
)
2284 static abi_ulong
loader_build_fdpic_loadmap(struct image_info
*info
, abi_ulong sp
)
2287 struct elf32_fdpic_loadseg
*loadsegs
= info
->loadsegs
;
2289 /* elf32_fdpic_loadseg */
2293 put_user_u32(loadsegs
[n
].addr
, sp
+0);
2294 put_user_u32(loadsegs
[n
].p_vaddr
, sp
+4);
2295 put_user_u32(loadsegs
[n
].p_memsz
, sp
+8);
2298 /* elf32_fdpic_loadmap */
2300 put_user_u16(0, sp
+0); /* version */
2301 put_user_u16(info
->nsegs
, sp
+2); /* nsegs */
2303 info
->personality
= PER_LINUX_FDPIC
;
2304 info
->loadmap_addr
= sp
;
2309 static abi_ulong
create_elf_tables(abi_ulong p
, int argc
, int envc
,
2310 struct elfhdr
*exec
,
2311 struct image_info
*info
,
2312 struct image_info
*interp_info
)
2315 abi_ulong u_argc
, u_argv
, u_envp
, u_auxv
;
2318 abi_ulong u_rand_bytes
;
2319 uint8_t k_rand_bytes
[16];
2320 abi_ulong u_platform
, u_base_platform
;
2321 const char *k_platform
, *k_base_platform
;
2322 const int n
= sizeof(elf_addr_t
);
2326 /* Needs to be before we load the env/argc/... */
2327 if (elf_is_fdpic(exec
)) {
2328 /* Need 4 byte alignment for these structs */
2330 sp
= loader_build_fdpic_loadmap(info
, sp
);
2331 info
->other_info
= interp_info
;
2333 interp_info
->other_info
= info
;
2334 sp
= loader_build_fdpic_loadmap(interp_info
, sp
);
2335 info
->interpreter_loadmap_addr
= interp_info
->loadmap_addr
;
2336 info
->interpreter_pt_dynamic_addr
= interp_info
->pt_dynamic_addr
;
2338 info
->interpreter_loadmap_addr
= 0;
2339 info
->interpreter_pt_dynamic_addr
= 0;
2343 u_base_platform
= 0;
2344 k_base_platform
= ELF_BASE_PLATFORM
;
2345 if (k_base_platform
) {
2346 size_t len
= strlen(k_base_platform
) + 1;
2347 if (STACK_GROWS_DOWN
) {
2348 sp
-= (len
+ n
- 1) & ~(n
- 1);
2349 u_base_platform
= sp
;
2350 /* FIXME - check return value of memcpy_to_target() for failure */
2351 memcpy_to_target(sp
, k_base_platform
, len
);
2353 memcpy_to_target(sp
, k_base_platform
, len
);
2354 u_base_platform
= sp
;
2360 k_platform
= ELF_PLATFORM
;
2362 size_t len
= strlen(k_platform
) + 1;
2363 if (STACK_GROWS_DOWN
) {
2364 sp
-= (len
+ n
- 1) & ~(n
- 1);
2366 /* FIXME - check return value of memcpy_to_target() for failure */
2367 memcpy_to_target(sp
, k_platform
, len
);
2369 memcpy_to_target(sp
, k_platform
, len
);
2375 /* Provide 16 byte alignment for the PRNG, and basic alignment for
2376 * the argv and envp pointers.
2378 if (STACK_GROWS_DOWN
) {
2379 sp
= QEMU_ALIGN_DOWN(sp
, 16);
2381 sp
= QEMU_ALIGN_UP(sp
, 16);
2385 * Generate 16 random bytes for userspace PRNG seeding.
2387 qemu_guest_getrandom_nofail(k_rand_bytes
, sizeof(k_rand_bytes
));
2388 if (STACK_GROWS_DOWN
) {
2391 /* FIXME - check return value of memcpy_to_target() for failure */
2392 memcpy_to_target(sp
, k_rand_bytes
, 16);
2394 memcpy_to_target(sp
, k_rand_bytes
, 16);
2399 size
= (DLINFO_ITEMS
+ 1) * 2;
2400 if (k_base_platform
)
2404 #ifdef DLINFO_ARCH_ITEMS
2405 size
+= DLINFO_ARCH_ITEMS
* 2;
2410 info
->auxv_len
= size
* n
;
2412 size
+= envc
+ argc
+ 2;
2413 size
+= 1; /* argc itself */
2416 /* Allocate space and finalize stack alignment for entry now. */
2417 if (STACK_GROWS_DOWN
) {
2418 u_argc
= QEMU_ALIGN_DOWN(sp
- size
, STACK_ALIGNMENT
);
2422 sp
= QEMU_ALIGN_UP(sp
+ size
, STACK_ALIGNMENT
);
2425 u_argv
= u_argc
+ n
;
2426 u_envp
= u_argv
+ (argc
+ 1) * n
;
2427 u_auxv
= u_envp
+ (envc
+ 1) * n
;
2428 info
->saved_auxv
= u_auxv
;
2431 info
->argv
= u_argv
;
2432 info
->envp
= u_envp
;
2434 /* This is correct because Linux defines
2435 * elf_addr_t as Elf32_Off / Elf64_Off
2437 #define NEW_AUX_ENT(id, val) do { \
2438 put_user_ual(id, u_auxv); u_auxv += n; \
2439 put_user_ual(val, u_auxv); u_auxv += n; \
2444 * ARCH_DLINFO must come first so platform specific code can enforce
2445 * special alignment requirements on the AUXV if necessary (eg. PPC).
2449 /* There must be exactly DLINFO_ITEMS entries here, or the assert
2450 * on info->auxv_len will trigger.
2452 NEW_AUX_ENT(AT_PHDR
, (abi_ulong
)(info
->load_addr
+ exec
->e_phoff
));
2453 NEW_AUX_ENT(AT_PHENT
, (abi_ulong
)(sizeof (struct elf_phdr
)));
2454 NEW_AUX_ENT(AT_PHNUM
, (abi_ulong
)(exec
->e_phnum
));
2455 if ((info
->alignment
& ~qemu_host_page_mask
) != 0) {
2456 /* Target doesn't support host page size alignment */
2457 NEW_AUX_ENT(AT_PAGESZ
, (abi_ulong
)(TARGET_PAGE_SIZE
));
2459 NEW_AUX_ENT(AT_PAGESZ
, (abi_ulong
)(MAX(TARGET_PAGE_SIZE
,
2460 qemu_host_page_size
)));
2462 NEW_AUX_ENT(AT_BASE
, (abi_ulong
)(interp_info
? interp_info
->load_addr
: 0));
2463 NEW_AUX_ENT(AT_FLAGS
, (abi_ulong
)0);
2464 NEW_AUX_ENT(AT_ENTRY
, info
->entry
);
2465 NEW_AUX_ENT(AT_UID
, (abi_ulong
) getuid());
2466 NEW_AUX_ENT(AT_EUID
, (abi_ulong
) geteuid());
2467 NEW_AUX_ENT(AT_GID
, (abi_ulong
) getgid());
2468 NEW_AUX_ENT(AT_EGID
, (abi_ulong
) getegid());
2469 NEW_AUX_ENT(AT_HWCAP
, (abi_ulong
) ELF_HWCAP
);
2470 NEW_AUX_ENT(AT_CLKTCK
, (abi_ulong
) sysconf(_SC_CLK_TCK
));
2471 NEW_AUX_ENT(AT_RANDOM
, (abi_ulong
) u_rand_bytes
);
2472 NEW_AUX_ENT(AT_SECURE
, (abi_ulong
) qemu_getauxval(AT_SECURE
));
2473 NEW_AUX_ENT(AT_EXECFN
, info
->file_string
);
2476 NEW_AUX_ENT(AT_HWCAP2
, (abi_ulong
) ELF_HWCAP2
);
2479 if (u_base_platform
) {
2480 NEW_AUX_ENT(AT_BASE_PLATFORM
, u_base_platform
);
2483 NEW_AUX_ENT(AT_PLATFORM
, u_platform
);
2485 NEW_AUX_ENT (AT_NULL
, 0);
2488 /* Check that our initial calculation of the auxv length matches how much
2489 * we actually put into it.
2491 assert(info
->auxv_len
== u_auxv
- info
->saved_auxv
);
2493 put_user_ual(argc
, u_argc
);
2495 p
= info
->arg_strings
;
2496 for (i
= 0; i
< argc
; ++i
) {
2497 put_user_ual(p
, u_argv
);
2499 p
+= target_strlen(p
) + 1;
2501 put_user_ual(0, u_argv
);
2503 p
= info
->env_strings
;
2504 for (i
= 0; i
< envc
; ++i
) {
2505 put_user_ual(p
, u_envp
);
2507 p
+= target_strlen(p
) + 1;
2509 put_user_ual(0, u_envp
);
2514 #if defined(HI_COMMPAGE)
2515 #define LO_COMMPAGE -1
2516 #elif defined(LO_COMMPAGE)
2517 #define HI_COMMPAGE 0
2519 #define HI_COMMPAGE 0
2520 #define LO_COMMPAGE -1
2521 #ifndef INIT_GUEST_COMMPAGE
2522 #define init_guest_commpage() true
2526 static void pgb_fail_in_use(const char *image_name
)
2528 error_report("%s: requires virtual address space that is in use "
2529 "(omit the -B option or choose a different value)",
2534 static void pgb_have_guest_base(const char *image_name
, abi_ulong guest_loaddr
,
2535 abi_ulong guest_hiaddr
, long align
)
2537 const int flags
= MAP_ANONYMOUS
| MAP_PRIVATE
| MAP_NORESERVE
;
2540 if (!QEMU_IS_ALIGNED(guest_base
, align
)) {
2541 fprintf(stderr
, "Requested guest base %p does not satisfy "
2542 "host minimum alignment (0x%lx)\n",
2543 (void *)guest_base
, align
);
2547 /* Sanity check the guest binary. */
2549 if (guest_hiaddr
> reserved_va
) {
2550 error_report("%s: requires more than reserved virtual "
2551 "address space (0x%" PRIx64
" > 0x%lx)",
2552 image_name
, (uint64_t)guest_hiaddr
, reserved_va
);
2556 #if HOST_LONG_BITS < TARGET_ABI_BITS
2557 if ((guest_hiaddr
- guest_base
) > ~(uintptr_t)0) {
2558 error_report("%s: requires more virtual address space "
2559 "than the host can provide (0x%" PRIx64
")",
2560 image_name
, (uint64_t)guest_hiaddr
+ 1 - guest_base
);
2567 * Expand the allocation to the entire reserved_va.
2568 * Exclude the mmap_min_addr hole.
2571 guest_loaddr
= (guest_base
>= mmap_min_addr
? 0
2572 : mmap_min_addr
- guest_base
);
2573 guest_hiaddr
= reserved_va
;
2576 /* Reserve the address space for the binary, or reserved_va. */
2577 test
= g2h_untagged(guest_loaddr
);
2578 addr
= mmap(test
, guest_hiaddr
- guest_loaddr
+ 1, PROT_NONE
, flags
, -1, 0);
2580 pgb_fail_in_use(image_name
);
2582 qemu_log_mask(CPU_LOG_PAGE
,
2583 "%s: base @ %p for %" PRIu64
" bytes\n",
2584 __func__
, addr
, (uint64_t)guest_hiaddr
- guest_loaddr
+ 1);
2588 * pgd_find_hole_fallback: potential mmap address
2589 * @guest_size: size of available space
2590 * @brk: location of break
2591 * @align: memory alignment
2593 * This is a fallback method for finding a hole in the host address
2594 * space if we don't have the benefit of being able to access
2595 * /proc/self/map. It can potentially take a very long time as we can
2596 * only dumbly iterate up the host address space seeing if the
2597 * allocation would work.
2599 static uintptr_t pgd_find_hole_fallback(uintptr_t guest_size
, uintptr_t brk
,
2600 long align
, uintptr_t offset
)
2604 /* Start (aligned) at the bottom and work our way up */
2605 base
= ROUND_UP(mmap_min_addr
, align
);
2608 uintptr_t align_start
, end
;
2609 align_start
= ROUND_UP(base
, align
);
2610 end
= align_start
+ guest_size
+ offset
;
2612 /* if brk is anywhere in the range give ourselves some room to grow. */
2613 if (align_start
<= brk
&& brk
< end
) {
2614 base
= brk
+ (16 * MiB
);
2616 } else if (align_start
+ guest_size
< align_start
) {
2617 /* we have run out of space */
2620 int flags
= MAP_ANONYMOUS
| MAP_PRIVATE
| MAP_NORESERVE
|
2621 MAP_FIXED_NOREPLACE
;
2622 void * mmap_start
= mmap((void *) align_start
, guest_size
,
2623 PROT_NONE
, flags
, -1, 0);
2624 if (mmap_start
!= MAP_FAILED
) {
2625 munmap(mmap_start
, guest_size
);
2626 if (mmap_start
== (void *) align_start
) {
2627 qemu_log_mask(CPU_LOG_PAGE
,
2628 "%s: base @ %p for %" PRIdPTR
" bytes\n",
2629 __func__
, mmap_start
+ offset
, guest_size
);
2630 return (uintptr_t) mmap_start
+ offset
;
2633 base
+= qemu_host_page_size
;
2638 /* Return value for guest_base, or -1 if no hole found. */
2639 static uintptr_t pgb_find_hole(uintptr_t guest_loaddr
, uintptr_t guest_size
,
2640 long align
, uintptr_t offset
)
2642 GSList
*maps
, *iter
;
2643 uintptr_t this_start
, this_end
, next_start
, brk
;
2646 assert(QEMU_IS_ALIGNED(guest_loaddr
, align
));
2648 maps
= read_self_maps();
2650 /* Read brk after we've read the maps, which will malloc. */
2651 brk
= (uintptr_t)sbrk(0);
2654 return pgd_find_hole_fallback(guest_size
, brk
, align
, offset
);
2657 /* The first hole is before the first map entry. */
2658 this_start
= mmap_min_addr
;
2660 for (iter
= maps
; iter
;
2661 this_start
= next_start
, iter
= g_slist_next(iter
)) {
2662 uintptr_t align_start
, hole_size
;
2664 this_end
= ((MapInfo
*)iter
->data
)->start
;
2665 next_start
= ((MapInfo
*)iter
->data
)->end
;
2666 align_start
= ROUND_UP(this_start
+ offset
, align
);
2668 /* Skip holes that are too small. */
2669 if (align_start
>= this_end
) {
2672 hole_size
= this_end
- align_start
;
2673 if (hole_size
< guest_size
) {
2677 /* If this hole contains brk, give ourselves some room to grow. */
2678 if (this_start
<= brk
&& brk
< this_end
) {
2679 hole_size
-= guest_size
;
2680 if (sizeof(uintptr_t) == 8 && hole_size
>= 1 * GiB
) {
2681 align_start
+= 1 * GiB
;
2682 } else if (hole_size
>= 16 * MiB
) {
2683 align_start
+= 16 * MiB
;
2685 align_start
= (this_end
- guest_size
) & -align
;
2686 if (align_start
< this_start
) {
2692 /* Record the lowest successful match. */
2696 /* If this hole contains the identity map, select it. */
2697 if (align_start
<= guest_loaddr
&&
2698 guest_loaddr
+ guest_size
<= this_end
) {
2701 /* If this hole ends above the identity map, stop looking. */
2702 if (this_end
>= guest_loaddr
) {
2706 free_self_maps(maps
);
2709 qemu_log_mask(CPU_LOG_PAGE
, "%s: base @ %" PRIxPTR
2710 " for %" PRIuPTR
" bytes\n",
2711 __func__
, ret
, guest_size
);
2717 static void pgb_static(const char *image_name
, abi_ulong orig_loaddr
,
2718 abi_ulong orig_hiaddr
, long align
)
2720 uintptr_t loaddr
= orig_loaddr
;
2721 uintptr_t hiaddr
= orig_hiaddr
;
2722 uintptr_t offset
= 0;
2725 if (hiaddr
!= orig_hiaddr
) {
2726 error_report("%s: requires virtual address space that the "
2727 "host cannot provide (0x%" PRIx64
")",
2728 image_name
, (uint64_t)orig_hiaddr
+ 1);
2735 * Extend the allocation to include the commpage.
2736 * For a 64-bit host, this is just 4GiB; for a 32-bit host we
2737 * need to ensure there is space bellow the guest_base so we
2738 * can map the commpage in the place needed when the address
2739 * arithmetic wraps around.
2741 if (sizeof(uintptr_t) == 8 || loaddr
>= 0x80000000u
) {
2742 hiaddr
= UINT32_MAX
;
2744 offset
= -(HI_COMMPAGE
& -align
);
2746 } else if (LO_COMMPAGE
!= -1) {
2747 loaddr
= MIN(loaddr
, LO_COMMPAGE
& -align
);
2750 addr
= pgb_find_hole(loaddr
, hiaddr
- loaddr
+ 1, align
, offset
);
2753 * If HI_COMMPAGE, there *might* be a non-consecutive allocation
2754 * that can satisfy both. But as the normal arm32 link base address
2755 * is ~32k, and we extend down to include the commpage, making the
2756 * overhead only ~96k, this is unlikely.
2758 error_report("%s: Unable to allocate %#zx bytes of "
2759 "virtual address space", image_name
,
2760 (size_t)(hiaddr
- loaddr
));
2766 qemu_log_mask(CPU_LOG_PAGE
, "%s: base @ %"PRIxPTR
" for %" PRIuPTR
" bytes\n",
2767 __func__
, addr
, hiaddr
- loaddr
);
2770 static void pgb_dynamic(const char *image_name
, long align
)
2773 * The executable is dynamic and does not require a fixed address.
2774 * All we need is a commpage that satisfies align.
2775 * If we do not need a commpage, leave guest_base == 0.
2778 uintptr_t addr
, commpage
;
2780 /* 64-bit hosts should have used reserved_va. */
2781 assert(sizeof(uintptr_t) == 4);
2784 * By putting the commpage at the first hole, that puts guest_base
2785 * just above that, and maximises the positive guest addresses.
2787 commpage
= HI_COMMPAGE
& -align
;
2788 addr
= pgb_find_hole(commpage
, -commpage
, align
, 0);
2794 static void pgb_reserved_va(const char *image_name
, abi_ulong guest_loaddr
,
2795 abi_ulong guest_hiaddr
, long align
)
2797 int flags
= MAP_ANONYMOUS
| MAP_PRIVATE
| MAP_NORESERVE
;
2800 if (guest_hiaddr
> reserved_va
) {
2801 error_report("%s: requires more than reserved virtual "
2802 "address space (0x%" PRIx64
" > 0x%lx)",
2803 image_name
, (uint64_t)guest_hiaddr
, reserved_va
);
2807 /* Widen the "image" to the entire reserved address space. */
2808 pgb_static(image_name
, 0, reserved_va
, align
);
2810 /* osdep.h defines this as 0 if it's missing */
2811 flags
|= MAP_FIXED_NOREPLACE
;
2813 /* Reserve the memory on the host. */
2814 assert(guest_base
!= 0);
2815 test
= g2h_untagged(0);
2816 addr
= mmap(test
, reserved_va
+ 1, PROT_NONE
, flags
, -1, 0);
2817 if (addr
== MAP_FAILED
|| addr
!= test
) {
2818 error_report("Unable to reserve 0x%lx bytes of virtual address "
2819 "space at %p (%s) for use as guest address space (check your "
2820 "virtual memory ulimit setting, mmap_min_addr or reserve less "
2821 "using qemu-user's -R option)",
2822 reserved_va
+ 1, test
, strerror(errno
));
2826 qemu_log_mask(CPU_LOG_PAGE
, "%s: base @ %p for %lu bytes\n",
2827 __func__
, addr
, reserved_va
+ 1);
2830 void probe_guest_base(const char *image_name
, abi_ulong guest_loaddr
,
2831 abi_ulong guest_hiaddr
)
2833 /* In order to use host shmat, we must be able to honor SHMLBA. */
2834 uintptr_t align
= MAX(SHMLBA
, qemu_host_page_size
);
2836 if (have_guest_base
) {
2837 pgb_have_guest_base(image_name
, guest_loaddr
, guest_hiaddr
, align
);
2838 } else if (reserved_va
) {
2839 pgb_reserved_va(image_name
, guest_loaddr
, guest_hiaddr
, align
);
2840 } else if (guest_loaddr
) {
2841 pgb_static(image_name
, guest_loaddr
, guest_hiaddr
, align
);
2843 pgb_dynamic(image_name
, align
);
2846 /* Reserve and initialize the commpage. */
2847 if (!init_guest_commpage()) {
2849 * With have_guest_base, the user has selected the address and
2850 * we are trying to work with that. Otherwise, we have selected
2851 * free space and init_guest_commpage must succeeded.
2853 assert(have_guest_base
);
2854 pgb_fail_in_use(image_name
);
2857 assert(QEMU_IS_ALIGNED(guest_base
, align
));
2858 qemu_log_mask(CPU_LOG_PAGE
, "Locating guest address space "
2859 "@ 0x%" PRIx64
"\n", (uint64_t)guest_base
);
2863 /* The string "GNU\0" as a magic number. */
2864 GNU0_MAGIC
= const_le32('G' | 'N' << 8 | 'U' << 16),
2865 NOTE_DATA_SZ
= 1 * KiB
,
2867 ELF_GNU_PROPERTY_ALIGN
= ELF_CLASS
== ELFCLASS32
? 4 : 8,
2871 * Process a single gnu_property entry.
2872 * Return false for error.
2874 static bool parse_elf_property(const uint32_t *data
, int *off
, int datasz
,
2875 struct image_info
*info
, bool have_prev_type
,
2876 uint32_t *prev_type
, Error
**errp
)
2878 uint32_t pr_type
, pr_datasz
, step
;
2880 if (*off
> datasz
|| !QEMU_IS_ALIGNED(*off
, ELF_GNU_PROPERTY_ALIGN
)) {
2884 data
+= *off
/ sizeof(uint32_t);
2886 if (datasz
< 2 * sizeof(uint32_t)) {
2890 pr_datasz
= data
[1];
2892 datasz
-= 2 * sizeof(uint32_t);
2893 step
= ROUND_UP(pr_datasz
, ELF_GNU_PROPERTY_ALIGN
);
2894 if (step
> datasz
) {
2898 /* Properties are supposed to be unique and sorted on pr_type. */
2899 if (have_prev_type
&& pr_type
<= *prev_type
) {
2900 if (pr_type
== *prev_type
) {
2901 error_setg(errp
, "Duplicate property in PT_GNU_PROPERTY");
2903 error_setg(errp
, "Unsorted property in PT_GNU_PROPERTY");
2907 *prev_type
= pr_type
;
2909 if (!arch_parse_elf_property(pr_type
, pr_datasz
, data
, info
, errp
)) {
2913 *off
+= 2 * sizeof(uint32_t) + step
;
2917 error_setg(errp
, "Ill-formed property in PT_GNU_PROPERTY");
2921 /* Process NT_GNU_PROPERTY_TYPE_0. */
2922 static bool parse_elf_properties(int image_fd
,
2923 struct image_info
*info
,
2924 const struct elf_phdr
*phdr
,
2925 char bprm_buf
[BPRM_BUF_SIZE
],
2929 struct elf_note nhdr
;
2930 uint32_t data
[NOTE_DATA_SZ
/ sizeof(uint32_t)];
2934 bool have_prev_type
;
2937 /* Unless the arch requires properties, ignore them. */
2938 if (!ARCH_USE_GNU_PROPERTY
) {
2942 /* If the properties are crazy large, that's too bad. */
2944 if (n
> sizeof(note
)) {
2945 error_setg(errp
, "PT_GNU_PROPERTY too large");
2948 if (n
< sizeof(note
.nhdr
)) {
2949 error_setg(errp
, "PT_GNU_PROPERTY too small");
2953 if (phdr
->p_offset
+ n
<= BPRM_BUF_SIZE
) {
2954 memcpy(¬e
, bprm_buf
+ phdr
->p_offset
, n
);
2956 ssize_t len
= pread(image_fd
, ¬e
, n
, phdr
->p_offset
);
2958 error_setg_errno(errp
, errno
, "Error reading file header");
2964 * The contents of a valid PT_GNU_PROPERTY is a sequence
2965 * of uint32_t -- swap them all now.
2968 for (int i
= 0; i
< n
/ 4; i
++) {
2969 bswap32s(note
.data
+ i
);
2974 * Note that nhdr is 3 words, and that the "name" described by namesz
2975 * immediately follows nhdr and is thus at the 4th word. Further, all
2976 * of the inputs to the kernel's round_up are multiples of 4.
2978 if (note
.nhdr
.n_type
!= NT_GNU_PROPERTY_TYPE_0
||
2979 note
.nhdr
.n_namesz
!= NOTE_NAME_SZ
||
2980 note
.data
[3] != GNU0_MAGIC
) {
2981 error_setg(errp
, "Invalid note in PT_GNU_PROPERTY");
2984 off
= sizeof(note
.nhdr
) + NOTE_NAME_SZ
;
2986 datasz
= note
.nhdr
.n_descsz
+ off
;
2988 error_setg(errp
, "Invalid note size in PT_GNU_PROPERTY");
2992 have_prev_type
= false;
2995 if (off
== datasz
) {
2996 return true; /* end, exit ok */
2998 if (!parse_elf_property(note
.data
, &off
, datasz
, info
,
2999 have_prev_type
, &prev_type
, errp
)) {
3002 have_prev_type
= true;
3006 /* Load an ELF image into the address space.
3008 IMAGE_NAME is the filename of the image, to use in error messages.
3009 IMAGE_FD is the open file descriptor for the image.
3011 BPRM_BUF is a copy of the beginning of the file; this of course
3012 contains the elf file header at offset 0. It is assumed that this
3013 buffer is sufficiently aligned to present no problems to the host
3014 in accessing data at aligned offsets within the buffer.
3016 On return: INFO values will be filled in, as necessary or available. */
3018 static void load_elf_image(const char *image_name
, int image_fd
,
3019 struct image_info
*info
, char **pinterp_name
,
3020 char bprm_buf
[BPRM_BUF_SIZE
])
3022 struct elfhdr
*ehdr
= (struct elfhdr
*)bprm_buf
;
3023 struct elf_phdr
*phdr
;
3024 abi_ulong load_addr
, load_bias
, loaddr
, hiaddr
, error
;
3025 int i
, retval
, prot_exec
;
3028 /* First of all, some simple consistency checks */
3029 if (!elf_check_ident(ehdr
)) {
3030 error_setg(&err
, "Invalid ELF image for this architecture");
3034 if (!elf_check_ehdr(ehdr
)) {
3035 error_setg(&err
, "Invalid ELF image for this architecture");
3039 i
= ehdr
->e_phnum
* sizeof(struct elf_phdr
);
3040 if (ehdr
->e_phoff
+ i
<= BPRM_BUF_SIZE
) {
3041 phdr
= (struct elf_phdr
*)(bprm_buf
+ ehdr
->e_phoff
);
3043 phdr
= (struct elf_phdr
*) alloca(i
);
3044 retval
= pread(image_fd
, phdr
, i
, ehdr
->e_phoff
);
3049 bswap_phdr(phdr
, ehdr
->e_phnum
);
3052 info
->pt_dynamic_addr
= 0;
3057 * Find the maximum size of the image and allocate an appropriate
3058 * amount of memory to handle that. Locate the interpreter, if any.
3060 loaddr
= -1, hiaddr
= 0;
3061 info
->alignment
= 0;
3062 info
->exec_stack
= EXSTACK_DEFAULT
;
3063 for (i
= 0; i
< ehdr
->e_phnum
; ++i
) {
3064 struct elf_phdr
*eppnt
= phdr
+ i
;
3065 if (eppnt
->p_type
== PT_LOAD
) {
3066 abi_ulong a
= eppnt
->p_vaddr
- eppnt
->p_offset
;
3070 a
= eppnt
->p_vaddr
+ eppnt
->p_memsz
- 1;
3075 info
->alignment
|= eppnt
->p_align
;
3076 } else if (eppnt
->p_type
== PT_INTERP
&& pinterp_name
) {
3077 g_autofree
char *interp_name
= NULL
;
3079 if (*pinterp_name
) {
3080 error_setg(&err
, "Multiple PT_INTERP entries");
3084 interp_name
= g_malloc(eppnt
->p_filesz
);
3086 if (eppnt
->p_offset
+ eppnt
->p_filesz
<= BPRM_BUF_SIZE
) {
3087 memcpy(interp_name
, bprm_buf
+ eppnt
->p_offset
,
3090 retval
= pread(image_fd
, interp_name
, eppnt
->p_filesz
,
3092 if (retval
!= eppnt
->p_filesz
) {
3096 if (interp_name
[eppnt
->p_filesz
- 1] != 0) {
3097 error_setg(&err
, "Invalid PT_INTERP entry");
3100 *pinterp_name
= g_steal_pointer(&interp_name
);
3101 } else if (eppnt
->p_type
== PT_GNU_PROPERTY
) {
3102 if (!parse_elf_properties(image_fd
, info
, eppnt
, bprm_buf
, &err
)) {
3105 } else if (eppnt
->p_type
== PT_GNU_STACK
) {
3106 info
->exec_stack
= eppnt
->p_flags
& PF_X
;
3110 if (pinterp_name
!= NULL
) {
3112 * This is the main executable.
3114 * Reserve extra space for brk.
3115 * We hold on to this space while placing the interpreter
3116 * and the stack, lest they be placed immediately after
3117 * the data segment and block allocation from the brk.
3119 * 16MB is chosen as "large enough" without being so large as
3120 * to allow the result to not fit with a 32-bit guest on a
3121 * 32-bit host. However some 64 bit guests (e.g. s390x)
3122 * attempt to place their heap further ahead and currently
3123 * nothing stops them smashing into QEMUs address space.
3125 #if TARGET_LONG_BITS == 64
3126 info
->reserve_brk
= 32 * MiB
;
3128 info
->reserve_brk
= 16 * MiB
;
3130 hiaddr
+= info
->reserve_brk
;
3132 if (ehdr
->e_type
== ET_EXEC
) {
3134 * Make sure that the low address does not conflict with
3135 * MMAP_MIN_ADDR or the QEMU application itself.
3137 probe_guest_base(image_name
, loaddr
, hiaddr
);
3140 * The binary is dynamic, but we still need to
3141 * select guest_base. In this case we pass a size.
3143 probe_guest_base(image_name
, 0, hiaddr
- loaddr
);
3148 * Reserve address space for all of this.
3150 * In the case of ET_EXEC, we supply MAP_FIXED so that we get
3151 * exactly the address range that is required.
3153 * Otherwise this is ET_DYN, and we are searching for a location
3154 * that can hold the memory space required. If the image is
3155 * pre-linked, LOADDR will be non-zero, and the kernel should
3156 * honor that address if it happens to be free.
3158 * In both cases, we will overwrite pages in this range with mappings
3159 * from the executable.
3161 load_addr
= target_mmap(loaddr
, (size_t)hiaddr
- loaddr
+ 1, PROT_NONE
,
3162 MAP_PRIVATE
| MAP_ANON
| MAP_NORESERVE
|
3163 (ehdr
->e_type
== ET_EXEC
? MAP_FIXED
: 0),
3165 if (load_addr
== -1) {
3168 load_bias
= load_addr
- loaddr
;
3170 if (elf_is_fdpic(ehdr
)) {
3171 struct elf32_fdpic_loadseg
*loadsegs
= info
->loadsegs
=
3172 g_malloc(sizeof(*loadsegs
) * info
->nsegs
);
3174 for (i
= 0; i
< ehdr
->e_phnum
; ++i
) {
3175 switch (phdr
[i
].p_type
) {
3177 info
->pt_dynamic_addr
= phdr
[i
].p_vaddr
+ load_bias
;
3180 loadsegs
->addr
= phdr
[i
].p_vaddr
+ load_bias
;
3181 loadsegs
->p_vaddr
= phdr
[i
].p_vaddr
;
3182 loadsegs
->p_memsz
= phdr
[i
].p_memsz
;
3189 info
->load_bias
= load_bias
;
3190 info
->code_offset
= load_bias
;
3191 info
->data_offset
= load_bias
;
3192 info
->load_addr
= load_addr
;
3193 info
->entry
= ehdr
->e_entry
+ load_bias
;
3194 info
->start_code
= -1;
3196 info
->start_data
= -1;
3199 info
->elf_flags
= ehdr
->e_flags
;
3201 prot_exec
= PROT_EXEC
;
3202 #ifdef TARGET_AARCH64
3204 * If the BTI feature is present, this indicates that the executable
3205 * pages of the startup binary should be mapped with PROT_BTI, so that
3206 * branch targets are enforced.
3208 * The startup binary is either the interpreter or the static executable.
3209 * The interpreter is responsible for all pages of a dynamic executable.
3211 * Elf notes are backward compatible to older cpus.
3212 * Do not enable BTI unless it is supported.
3214 if ((info
->note_flags
& GNU_PROPERTY_AARCH64_FEATURE_1_BTI
)
3215 && (pinterp_name
== NULL
|| *pinterp_name
== 0)
3216 && cpu_isar_feature(aa64_bti
, ARM_CPU(thread_cpu
))) {
3217 prot_exec
|= TARGET_PROT_BTI
;
3221 for (i
= 0; i
< ehdr
->e_phnum
; i
++) {
3222 struct elf_phdr
*eppnt
= phdr
+ i
;
3223 if (eppnt
->p_type
== PT_LOAD
) {
3224 abi_ulong vaddr
, vaddr_po
, vaddr_ps
, vaddr_ef
, vaddr_em
, vaddr_len
;
3227 if (eppnt
->p_flags
& PF_R
) {
3228 elf_prot
|= PROT_READ
;
3230 if (eppnt
->p_flags
& PF_W
) {
3231 elf_prot
|= PROT_WRITE
;
3233 if (eppnt
->p_flags
& PF_X
) {
3234 elf_prot
|= prot_exec
;
3237 vaddr
= load_bias
+ eppnt
->p_vaddr
;
3238 vaddr_po
= TARGET_ELF_PAGEOFFSET(vaddr
);
3239 vaddr_ps
= TARGET_ELF_PAGESTART(vaddr
);
3241 vaddr_ef
= vaddr
+ eppnt
->p_filesz
;
3242 vaddr_em
= vaddr
+ eppnt
->p_memsz
;
3245 * Some segments may be completely empty, with a non-zero p_memsz
3246 * but no backing file segment.
3248 if (eppnt
->p_filesz
!= 0) {
3249 vaddr_len
= TARGET_ELF_PAGELENGTH(eppnt
->p_filesz
+ vaddr_po
);
3250 error
= target_mmap(vaddr_ps
, vaddr_len
, elf_prot
,
3251 MAP_PRIVATE
| MAP_FIXED
,
3252 image_fd
, eppnt
->p_offset
- vaddr_po
);
3259 * If the load segment requests extra zeros (e.g. bss), map it.
3261 if (eppnt
->p_filesz
< eppnt
->p_memsz
) {
3262 zero_bss(vaddr_ef
, vaddr_em
, elf_prot
);
3264 } else if (eppnt
->p_memsz
!= 0) {
3265 vaddr_len
= TARGET_ELF_PAGELENGTH(eppnt
->p_memsz
+ vaddr_po
);
3266 error
= target_mmap(vaddr_ps
, vaddr_len
, elf_prot
,
3267 MAP_PRIVATE
| MAP_FIXED
| MAP_ANONYMOUS
,
3275 /* Find the full program boundaries. */
3276 if (elf_prot
& PROT_EXEC
) {
3277 if (vaddr
< info
->start_code
) {
3278 info
->start_code
= vaddr
;
3280 if (vaddr_ef
> info
->end_code
) {
3281 info
->end_code
= vaddr_ef
;
3284 if (elf_prot
& PROT_WRITE
) {
3285 if (vaddr
< info
->start_data
) {
3286 info
->start_data
= vaddr
;
3288 if (vaddr_ef
> info
->end_data
) {
3289 info
->end_data
= vaddr_ef
;
3292 if (vaddr_em
> info
->brk
) {
3293 info
->brk
= vaddr_em
;
3296 } else if (eppnt
->p_type
== PT_MIPS_ABIFLAGS
) {
3297 Mips_elf_abiflags_v0 abiflags
;
3298 if (eppnt
->p_filesz
< sizeof(Mips_elf_abiflags_v0
)) {
3299 error_setg(&err
, "Invalid PT_MIPS_ABIFLAGS entry");
3302 if (eppnt
->p_offset
+ eppnt
->p_filesz
<= BPRM_BUF_SIZE
) {
3303 memcpy(&abiflags
, bprm_buf
+ eppnt
->p_offset
,
3304 sizeof(Mips_elf_abiflags_v0
));
3306 retval
= pread(image_fd
, &abiflags
, sizeof(Mips_elf_abiflags_v0
),
3308 if (retval
!= sizeof(Mips_elf_abiflags_v0
)) {
3312 bswap_mips_abiflags(&abiflags
);
3313 info
->fp_abi
= abiflags
.fp_abi
;
3318 if (info
->end_data
== 0) {
3319 info
->start_data
= info
->end_code
;
3320 info
->end_data
= info
->end_code
;
3323 if (qemu_log_enabled()) {
3324 load_symbols(ehdr
, image_fd
, load_bias
);
3327 debuginfo_report_elf(image_name
, image_fd
, load_bias
);
3336 error_setg(&err
, "Incomplete read of file header");
3338 error_setg_errno(&err
, errno
, "Error reading file header");
3342 error_setg_errno(&err
, errno
, "Error mapping file");
3345 error_reportf_err(err
, "%s: ", image_name
);
3349 static void load_elf_interp(const char *filename
, struct image_info
*info
,
3350 char bprm_buf
[BPRM_BUF_SIZE
])
3355 fd
= open(path(filename
), O_RDONLY
);
3357 error_setg_file_open(&err
, errno
, filename
);
3358 error_report_err(err
);
3362 retval
= read(fd
, bprm_buf
, BPRM_BUF_SIZE
);
3364 error_setg_errno(&err
, errno
, "Error reading file header");
3365 error_reportf_err(err
, "%s: ", filename
);
3369 if (retval
< BPRM_BUF_SIZE
) {
3370 memset(bprm_buf
+ retval
, 0, BPRM_BUF_SIZE
- retval
);
3373 load_elf_image(filename
, fd
, info
, NULL
, bprm_buf
);
3376 static int symfind(const void *s0
, const void *s1
)
3378 struct elf_sym
*sym
= (struct elf_sym
*)s1
;
3379 __typeof(sym
->st_value
) addr
= *(uint64_t *)s0
;
3382 if (addr
< sym
->st_value
) {
3384 } else if (addr
>= sym
->st_value
+ sym
->st_size
) {
3390 static const char *lookup_symbolxx(struct syminfo
*s
, uint64_t orig_addr
)
3392 #if ELF_CLASS == ELFCLASS32
3393 struct elf_sym
*syms
= s
->disas_symtab
.elf32
;
3395 struct elf_sym
*syms
= s
->disas_symtab
.elf64
;
3399 struct elf_sym
*sym
;
3401 sym
= bsearch(&orig_addr
, syms
, s
->disas_num_syms
, sizeof(*syms
), symfind
);
3403 return s
->disas_strtab
+ sym
->st_name
;
3409 /* FIXME: This should use elf_ops.h */
3410 static int symcmp(const void *s0
, const void *s1
)
3412 struct elf_sym
*sym0
= (struct elf_sym
*)s0
;
3413 struct elf_sym
*sym1
= (struct elf_sym
*)s1
;
3414 return (sym0
->st_value
< sym1
->st_value
)
3416 : ((sym0
->st_value
> sym1
->st_value
) ? 1 : 0);
3419 /* Best attempt to load symbols from this ELF object. */
3420 static void load_symbols(struct elfhdr
*hdr
, int fd
, abi_ulong load_bias
)
3422 int i
, shnum
, nsyms
, sym_idx
= 0, str_idx
= 0;
3424 struct elf_shdr
*shdr
;
3425 char *strings
= NULL
;
3426 struct syminfo
*s
= NULL
;
3427 struct elf_sym
*new_syms
, *syms
= NULL
;
3429 shnum
= hdr
->e_shnum
;
3430 i
= shnum
* sizeof(struct elf_shdr
);
3431 shdr
= (struct elf_shdr
*)alloca(i
);
3432 if (pread(fd
, shdr
, i
, hdr
->e_shoff
) != i
) {
3436 bswap_shdr(shdr
, shnum
);
3437 for (i
= 0; i
< shnum
; ++i
) {
3438 if (shdr
[i
].sh_type
== SHT_SYMTAB
) {
3440 str_idx
= shdr
[i
].sh_link
;
3445 /* There will be no symbol table if the file was stripped. */
3449 /* Now know where the strtab and symtab are. Snarf them. */
3450 s
= g_try_new(struct syminfo
, 1);
3455 segsz
= shdr
[str_idx
].sh_size
;
3456 s
->disas_strtab
= strings
= g_try_malloc(segsz
);
3458 pread(fd
, strings
, segsz
, shdr
[str_idx
].sh_offset
) != segsz
) {
3462 segsz
= shdr
[sym_idx
].sh_size
;
3463 syms
= g_try_malloc(segsz
);
3464 if (!syms
|| pread(fd
, syms
, segsz
, shdr
[sym_idx
].sh_offset
) != segsz
) {
3468 if (segsz
/ sizeof(struct elf_sym
) > INT_MAX
) {
3469 /* Implausibly large symbol table: give up rather than ploughing
3470 * on with the number of symbols calculation overflowing
3474 nsyms
= segsz
/ sizeof(struct elf_sym
);
3475 for (i
= 0; i
< nsyms
; ) {
3476 bswap_sym(syms
+ i
);
3477 /* Throw away entries which we do not need. */
3478 if (syms
[i
].st_shndx
== SHN_UNDEF
3479 || syms
[i
].st_shndx
>= SHN_LORESERVE
3480 || ELF_ST_TYPE(syms
[i
].st_info
) != STT_FUNC
) {
3482 syms
[i
] = syms
[nsyms
];
3485 #if defined(TARGET_ARM) || defined (TARGET_MIPS)
3486 /* The bottom address bit marks a Thumb or MIPS16 symbol. */
3487 syms
[i
].st_value
&= ~(target_ulong
)1;
3489 syms
[i
].st_value
+= load_bias
;
3494 /* No "useful" symbol. */
3499 /* Attempt to free the storage associated with the local symbols
3500 that we threw away. Whether or not this has any effect on the
3501 memory allocation depends on the malloc implementation and how
3502 many symbols we managed to discard. */
3503 new_syms
= g_try_renew(struct elf_sym
, syms
, nsyms
);
3504 if (new_syms
== NULL
) {
3509 qsort(syms
, nsyms
, sizeof(*syms
), symcmp
);
3511 s
->disas_num_syms
= nsyms
;
3512 #if ELF_CLASS == ELFCLASS32
3513 s
->disas_symtab
.elf32
= syms
;
3515 s
->disas_symtab
.elf64
= syms
;
3517 s
->lookup_symbol
= lookup_symbolxx
;
3529 uint32_t get_elf_eflags(int fd
)
3535 /* Read ELF header */
3536 offset
= lseek(fd
, 0, SEEK_SET
);
3537 if (offset
== (off_t
) -1) {
3540 ret
= read(fd
, &ehdr
, sizeof(ehdr
));
3541 if (ret
< sizeof(ehdr
)) {
3544 offset
= lseek(fd
, offset
, SEEK_SET
);
3545 if (offset
== (off_t
) -1) {
3549 /* Check ELF signature */
3550 if (!elf_check_ident(&ehdr
)) {
3556 if (!elf_check_ehdr(&ehdr
)) {
3560 /* return architecture id */
3561 return ehdr
.e_flags
;
3564 int load_elf_binary(struct linux_binprm
*bprm
, struct image_info
*info
)
3566 struct image_info interp_info
;
3567 struct elfhdr elf_ex
;
3568 char *elf_interpreter
= NULL
;
3571 memset(&interp_info
, 0, sizeof(interp_info
));
3573 interp_info
.fp_abi
= MIPS_ABI_FP_UNKNOWN
;
3576 info
->start_mmap
= (abi_ulong
)ELF_START_MMAP
;
3578 load_elf_image(bprm
->filename
, bprm
->fd
, info
,
3579 &elf_interpreter
, bprm
->buf
);
3581 /* ??? We need a copy of the elf header for passing to create_elf_tables.
3582 If we do nothing, we'll have overwritten this when we re-use bprm->buf
3583 when we load the interpreter. */
3584 elf_ex
= *(struct elfhdr
*)bprm
->buf
;
3586 /* Do this so that we can load the interpreter, if need be. We will
3587 change some of these later */
3588 bprm
->p
= setup_arg_pages(bprm
, info
);
3590 scratch
= g_new0(char, TARGET_PAGE_SIZE
);
3591 if (STACK_GROWS_DOWN
) {
3592 bprm
->p
= copy_elf_strings(1, &bprm
->filename
, scratch
,
3593 bprm
->p
, info
->stack_limit
);
3594 info
->file_string
= bprm
->p
;
3595 bprm
->p
= copy_elf_strings(bprm
->envc
, bprm
->envp
, scratch
,
3596 bprm
->p
, info
->stack_limit
);
3597 info
->env_strings
= bprm
->p
;
3598 bprm
->p
= copy_elf_strings(bprm
->argc
, bprm
->argv
, scratch
,
3599 bprm
->p
, info
->stack_limit
);
3600 info
->arg_strings
= bprm
->p
;
3602 info
->arg_strings
= bprm
->p
;
3603 bprm
->p
= copy_elf_strings(bprm
->argc
, bprm
->argv
, scratch
,
3604 bprm
->p
, info
->stack_limit
);
3605 info
->env_strings
= bprm
->p
;
3606 bprm
->p
= copy_elf_strings(bprm
->envc
, bprm
->envp
, scratch
,
3607 bprm
->p
, info
->stack_limit
);
3608 info
->file_string
= bprm
->p
;
3609 bprm
->p
= copy_elf_strings(1, &bprm
->filename
, scratch
,
3610 bprm
->p
, info
->stack_limit
);
3616 fprintf(stderr
, "%s: %s\n", bprm
->filename
, strerror(E2BIG
));
3620 if (elf_interpreter
) {
3621 load_elf_interp(elf_interpreter
, &interp_info
, bprm
->buf
);
3623 /* If the program interpreter is one of these two, then assume
3624 an iBCS2 image. Otherwise assume a native linux image. */
3626 if (strcmp(elf_interpreter
, "/usr/lib/libc.so.1") == 0
3627 || strcmp(elf_interpreter
, "/usr/lib/ld.so.1") == 0) {
3628 info
->personality
= PER_SVR4
;
3630 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
3631 and some applications "depend" upon this behavior. Since
3632 we do not have the power to recompile these, we emulate
3633 the SVr4 behavior. Sigh. */
3634 target_mmap(0, qemu_host_page_size
, PROT_READ
| PROT_EXEC
,
3635 MAP_FIXED
| MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
3638 info
->interp_fp_abi
= interp_info
.fp_abi
;
3643 * TODO: load a vdso, which would also contain the signal trampolines.
3644 * Otherwise, allocate a private page to hold them.
3646 if (TARGET_ARCH_HAS_SIGTRAMP_PAGE
) {
3647 abi_long tramp_page
= target_mmap(0, TARGET_PAGE_SIZE
,
3648 PROT_READ
| PROT_WRITE
,
3649 MAP_PRIVATE
| MAP_ANON
, -1, 0);
3650 if (tramp_page
== -1) {
3654 setup_sigtramp(tramp_page
);
3655 target_mprotect(tramp_page
, TARGET_PAGE_SIZE
, PROT_READ
| PROT_EXEC
);
3658 bprm
->p
= create_elf_tables(bprm
->p
, bprm
->argc
, bprm
->envc
, &elf_ex
,
3659 info
, (elf_interpreter
? &interp_info
: NULL
));
3660 info
->start_stack
= bprm
->p
;
3662 /* If we have an interpreter, set that as the program's entry point.
3663 Copy the load_bias as well, to help PPC64 interpret the entry
3664 point as a function descriptor. Do this after creating elf tables
3665 so that we copy the original program entry point into the AUXV. */
3666 if (elf_interpreter
) {
3667 info
->load_bias
= interp_info
.load_bias
;
3668 info
->entry
= interp_info
.entry
;
3669 g_free(elf_interpreter
);
3672 #ifdef USE_ELF_CORE_DUMP
3673 bprm
->core_dump
= &elf_core_dump
;
3677 * If we reserved extra space for brk, release it now.
3678 * The implementation of do_brk in syscalls.c expects to be able
3679 * to mmap pages in this space.
3681 if (info
->reserve_brk
) {
3682 abi_ulong start_brk
= HOST_PAGE_ALIGN(info
->brk
);
3683 abi_ulong end_brk
= HOST_PAGE_ALIGN(info
->brk
+ info
->reserve_brk
);
3684 target_munmap(start_brk
, end_brk
- start_brk
);
3690 #ifdef USE_ELF_CORE_DUMP
3692 * Definitions to generate Intel SVR4-like core files.
3693 * These mostly have the same names as the SVR4 types with "target_elf_"
3694 * tacked on the front to prevent clashes with linux definitions,
3695 * and the typedef forms have been avoided. This is mostly like
3696 * the SVR4 structure, but more Linuxy, with things that Linux does
3697 * not support and which gdb doesn't really use excluded.
3699 * Fields we don't dump (their contents is zero) in linux-user qemu
3700 * are marked with XXX.
3702 * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
3704 * Porting ELF coredump for target is (quite) simple process. First you
3705 * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for
3706 * the target resides):
3708 * #define USE_ELF_CORE_DUMP
3710 * Next you define type of register set used for dumping. ELF specification
3711 * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
3713 * typedef <target_regtype> target_elf_greg_t;
3714 * #define ELF_NREG <number of registers>
3715 * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG];
3717 * Last step is to implement target specific function that copies registers
3718 * from given cpu into just specified register set. Prototype is:
3720 * static void elf_core_copy_regs(taret_elf_gregset_t *regs,
3721 * const CPUArchState *env);
3724 * regs - copy register values into here (allocated and zeroed by caller)
3725 * env - copy registers from here
3727 * Example for ARM target is provided in this file.
3730 /* An ELF note in memory */
3734 size_t namesz_rounded
;
3737 size_t datasz_rounded
;
3742 struct target_elf_siginfo
{
3743 abi_int si_signo
; /* signal number */
3744 abi_int si_code
; /* extra code */
3745 abi_int si_errno
; /* errno */
3748 struct target_elf_prstatus
{
3749 struct target_elf_siginfo pr_info
; /* Info associated with signal */
3750 abi_short pr_cursig
; /* Current signal */
3751 abi_ulong pr_sigpend
; /* XXX */
3752 abi_ulong pr_sighold
; /* XXX */
3753 target_pid_t pr_pid
;
3754 target_pid_t pr_ppid
;
3755 target_pid_t pr_pgrp
;
3756 target_pid_t pr_sid
;
3757 struct target_timeval pr_utime
; /* XXX User time */
3758 struct target_timeval pr_stime
; /* XXX System time */
3759 struct target_timeval pr_cutime
; /* XXX Cumulative user time */
3760 struct target_timeval pr_cstime
; /* XXX Cumulative system time */
3761 target_elf_gregset_t pr_reg
; /* GP registers */
3762 abi_int pr_fpvalid
; /* XXX */
3765 #define ELF_PRARGSZ (80) /* Number of chars for args */
3767 struct target_elf_prpsinfo
{
3768 char pr_state
; /* numeric process state */
3769 char pr_sname
; /* char for pr_state */
3770 char pr_zomb
; /* zombie */
3771 char pr_nice
; /* nice val */
3772 abi_ulong pr_flag
; /* flags */
3773 target_uid_t pr_uid
;
3774 target_gid_t pr_gid
;
3775 target_pid_t pr_pid
, pr_ppid
, pr_pgrp
, pr_sid
;
3777 char pr_fname
[16] QEMU_NONSTRING
; /* filename of executable */
3778 char pr_psargs
[ELF_PRARGSZ
]; /* initial part of arg list */
3781 /* Here is the structure in which status of each thread is captured. */
3782 struct elf_thread_status
{
3783 QTAILQ_ENTRY(elf_thread_status
) ets_link
;
3784 struct target_elf_prstatus prstatus
; /* NT_PRSTATUS */
3786 elf_fpregset_t fpu
; /* NT_PRFPREG */
3787 struct task_struct
*thread
;
3788 elf_fpxregset_t xfpu
; /* ELF_CORE_XFPREG_TYPE */
3790 struct memelfnote notes
[1];
3794 struct elf_note_info
{
3795 struct memelfnote
*notes
;
3796 struct target_elf_prstatus
*prstatus
; /* NT_PRSTATUS */
3797 struct target_elf_prpsinfo
*psinfo
; /* NT_PRPSINFO */
3799 QTAILQ_HEAD(, elf_thread_status
) thread_list
;
3802 * Current version of ELF coredump doesn't support
3803 * dumping fp regs etc.
3805 elf_fpregset_t
*fpu
;
3806 elf_fpxregset_t
*xfpu
;
3807 int thread_status_size
;
3813 struct vm_area_struct
{
3814 target_ulong vma_start
; /* start vaddr of memory region */
3815 target_ulong vma_end
; /* end vaddr of memory region */
3816 abi_ulong vma_flags
; /* protection etc. flags for the region */
3817 QTAILQ_ENTRY(vm_area_struct
) vma_link
;
3821 QTAILQ_HEAD(, vm_area_struct
) mm_mmap
;
3822 int mm_count
; /* number of mappings */
3825 static struct mm_struct
*vma_init(void);
3826 static void vma_delete(struct mm_struct
*);
3827 static int vma_add_mapping(struct mm_struct
*, target_ulong
,
3828 target_ulong
, abi_ulong
);
3829 static int vma_get_mapping_count(const struct mm_struct
*);
3830 static struct vm_area_struct
*vma_first(const struct mm_struct
*);
3831 static struct vm_area_struct
*vma_next(struct vm_area_struct
*);
3832 static abi_ulong
vma_dump_size(const struct vm_area_struct
*);
3833 static int vma_walker(void *priv
, target_ulong start
, target_ulong end
,
3834 unsigned long flags
);
3836 static void fill_elf_header(struct elfhdr
*, int, uint16_t, uint32_t);
3837 static void fill_note(struct memelfnote
*, const char *, int,
3838 unsigned int, void *);
3839 static void fill_prstatus(struct target_elf_prstatus
*, const TaskState
*, int);
3840 static int fill_psinfo(struct target_elf_prpsinfo
*, const TaskState
*);
3841 static void fill_auxv_note(struct memelfnote
*, const TaskState
*);
3842 static void fill_elf_note_phdr(struct elf_phdr
*, int, off_t
);
3843 static size_t note_size(const struct memelfnote
*);
3844 static void free_note_info(struct elf_note_info
*);
3845 static int fill_note_info(struct elf_note_info
*, long, const CPUArchState
*);
3846 static void fill_thread_info(struct elf_note_info
*, const CPUArchState
*);
3848 static int dump_write(int, const void *, size_t);
3849 static int write_note(struct memelfnote
*, int);
3850 static int write_note_info(struct elf_note_info
*, int);
3853 static void bswap_prstatus(struct target_elf_prstatus
*prstatus
)
3855 prstatus
->pr_info
.si_signo
= tswap32(prstatus
->pr_info
.si_signo
);
3856 prstatus
->pr_info
.si_code
= tswap32(prstatus
->pr_info
.si_code
);
3857 prstatus
->pr_info
.si_errno
= tswap32(prstatus
->pr_info
.si_errno
);
3858 prstatus
->pr_cursig
= tswap16(prstatus
->pr_cursig
);
3859 prstatus
->pr_sigpend
= tswapal(prstatus
->pr_sigpend
);
3860 prstatus
->pr_sighold
= tswapal(prstatus
->pr_sighold
);
3861 prstatus
->pr_pid
= tswap32(prstatus
->pr_pid
);
3862 prstatus
->pr_ppid
= tswap32(prstatus
->pr_ppid
);
3863 prstatus
->pr_pgrp
= tswap32(prstatus
->pr_pgrp
);
3864 prstatus
->pr_sid
= tswap32(prstatus
->pr_sid
);
3865 /* cpu times are not filled, so we skip them */
3866 /* regs should be in correct format already */
3867 prstatus
->pr_fpvalid
= tswap32(prstatus
->pr_fpvalid
);
3870 static void bswap_psinfo(struct target_elf_prpsinfo
*psinfo
)
3872 psinfo
->pr_flag
= tswapal(psinfo
->pr_flag
);
3873 psinfo
->pr_uid
= tswap16(psinfo
->pr_uid
);
3874 psinfo
->pr_gid
= tswap16(psinfo
->pr_gid
);
3875 psinfo
->pr_pid
= tswap32(psinfo
->pr_pid
);
3876 psinfo
->pr_ppid
= tswap32(psinfo
->pr_ppid
);
3877 psinfo
->pr_pgrp
= tswap32(psinfo
->pr_pgrp
);
3878 psinfo
->pr_sid
= tswap32(psinfo
->pr_sid
);
3881 static void bswap_note(struct elf_note
*en
)
3883 bswap32s(&en
->n_namesz
);
3884 bswap32s(&en
->n_descsz
);
3885 bswap32s(&en
->n_type
);
3888 static inline void bswap_prstatus(struct target_elf_prstatus
*p
) { }
3889 static inline void bswap_psinfo(struct target_elf_prpsinfo
*p
) {}
3890 static inline void bswap_note(struct elf_note
*en
) { }
3891 #endif /* BSWAP_NEEDED */
3894 * Minimal support for linux memory regions. These are needed
3895 * when we are finding out what memory exactly belongs to
3896 * emulated process. No locks needed here, as long as
3897 * thread that received the signal is stopped.
3900 static struct mm_struct
*vma_init(void)
3902 struct mm_struct
*mm
;
3904 if ((mm
= g_malloc(sizeof (*mm
))) == NULL
)
3908 QTAILQ_INIT(&mm
->mm_mmap
);
3913 static void vma_delete(struct mm_struct
*mm
)
3915 struct vm_area_struct
*vma
;
3917 while ((vma
= vma_first(mm
)) != NULL
) {
3918 QTAILQ_REMOVE(&mm
->mm_mmap
, vma
, vma_link
);
3924 static int vma_add_mapping(struct mm_struct
*mm
, target_ulong start
,
3925 target_ulong end
, abi_ulong flags
)
3927 struct vm_area_struct
*vma
;
3929 if ((vma
= g_malloc0(sizeof (*vma
))) == NULL
)
3932 vma
->vma_start
= start
;
3934 vma
->vma_flags
= flags
;
3936 QTAILQ_INSERT_TAIL(&mm
->mm_mmap
, vma
, vma_link
);
3942 static struct vm_area_struct
*vma_first(const struct mm_struct
*mm
)
3944 return (QTAILQ_FIRST(&mm
->mm_mmap
));
3947 static struct vm_area_struct
*vma_next(struct vm_area_struct
*vma
)
3949 return (QTAILQ_NEXT(vma
, vma_link
));
3952 static int vma_get_mapping_count(const struct mm_struct
*mm
)
3954 return (mm
->mm_count
);
3958 * Calculate file (dump) size of given memory region.
3960 static abi_ulong
vma_dump_size(const struct vm_area_struct
*vma
)
3962 /* if we cannot even read the first page, skip it */
3963 if (!access_ok_untagged(VERIFY_READ
, vma
->vma_start
, TARGET_PAGE_SIZE
))
3967 * Usually we don't dump executable pages as they contain
3968 * non-writable code that debugger can read directly from
3969 * target library etc. However, thread stacks are marked
3970 * also executable so we read in first page of given region
3971 * and check whether it contains elf header. If there is
3972 * no elf header, we dump it.
3974 if (vma
->vma_flags
& PROT_EXEC
) {
3975 char page
[TARGET_PAGE_SIZE
];
3977 if (copy_from_user(page
, vma
->vma_start
, sizeof (page
))) {
3980 if ((page
[EI_MAG0
] == ELFMAG0
) &&
3981 (page
[EI_MAG1
] == ELFMAG1
) &&
3982 (page
[EI_MAG2
] == ELFMAG2
) &&
3983 (page
[EI_MAG3
] == ELFMAG3
)) {
3985 * Mappings are possibly from ELF binary. Don't dump
3992 return (vma
->vma_end
- vma
->vma_start
);
3995 static int vma_walker(void *priv
, target_ulong start
, target_ulong end
,
3996 unsigned long flags
)
3998 struct mm_struct
*mm
= (struct mm_struct
*)priv
;
4000 vma_add_mapping(mm
, start
, end
, flags
);
4004 static void fill_note(struct memelfnote
*note
, const char *name
, int type
,
4005 unsigned int sz
, void *data
)
4007 unsigned int namesz
;
4009 namesz
= strlen(name
) + 1;
4011 note
->namesz
= namesz
;
4012 note
->namesz_rounded
= roundup(namesz
, sizeof (int32_t));
4015 note
->datasz_rounded
= roundup(sz
, sizeof (int32_t));
4020 * We calculate rounded up note size here as specified by
4023 note
->notesz
= sizeof (struct elf_note
) +
4024 note
->namesz_rounded
+ note
->datasz_rounded
;
4027 static void fill_elf_header(struct elfhdr
*elf
, int segs
, uint16_t machine
,
4030 (void) memset(elf
, 0, sizeof(*elf
));
4032 (void) memcpy(elf
->e_ident
, ELFMAG
, SELFMAG
);
4033 elf
->e_ident
[EI_CLASS
] = ELF_CLASS
;
4034 elf
->e_ident
[EI_DATA
] = ELF_DATA
;
4035 elf
->e_ident
[EI_VERSION
] = EV_CURRENT
;
4036 elf
->e_ident
[EI_OSABI
] = ELF_OSABI
;
4038 elf
->e_type
= ET_CORE
;
4039 elf
->e_machine
= machine
;
4040 elf
->e_version
= EV_CURRENT
;
4041 elf
->e_phoff
= sizeof(struct elfhdr
);
4042 elf
->e_flags
= flags
;
4043 elf
->e_ehsize
= sizeof(struct elfhdr
);
4044 elf
->e_phentsize
= sizeof(struct elf_phdr
);
4045 elf
->e_phnum
= segs
;
4050 static void fill_elf_note_phdr(struct elf_phdr
*phdr
, int sz
, off_t offset
)
4052 phdr
->p_type
= PT_NOTE
;
4053 phdr
->p_offset
= offset
;
4056 phdr
->p_filesz
= sz
;
4061 bswap_phdr(phdr
, 1);
4064 static size_t note_size(const struct memelfnote
*note
)
4066 return (note
->notesz
);
4069 static void fill_prstatus(struct target_elf_prstatus
*prstatus
,
4070 const TaskState
*ts
, int signr
)
4072 (void) memset(prstatus
, 0, sizeof (*prstatus
));
4073 prstatus
->pr_info
.si_signo
= prstatus
->pr_cursig
= signr
;
4074 prstatus
->pr_pid
= ts
->ts_tid
;
4075 prstatus
->pr_ppid
= getppid();
4076 prstatus
->pr_pgrp
= getpgrp();
4077 prstatus
->pr_sid
= getsid(0);
4079 bswap_prstatus(prstatus
);
4082 static int fill_psinfo(struct target_elf_prpsinfo
*psinfo
, const TaskState
*ts
)
4084 char *base_filename
;
4085 unsigned int i
, len
;
4087 (void) memset(psinfo
, 0, sizeof (*psinfo
));
4089 len
= ts
->info
->env_strings
- ts
->info
->arg_strings
;
4090 if (len
>= ELF_PRARGSZ
)
4091 len
= ELF_PRARGSZ
- 1;
4092 if (copy_from_user(&psinfo
->pr_psargs
, ts
->info
->arg_strings
, len
)) {
4095 for (i
= 0; i
< len
; i
++)
4096 if (psinfo
->pr_psargs
[i
] == 0)
4097 psinfo
->pr_psargs
[i
] = ' ';
4098 psinfo
->pr_psargs
[len
] = 0;
4100 psinfo
->pr_pid
= getpid();
4101 psinfo
->pr_ppid
= getppid();
4102 psinfo
->pr_pgrp
= getpgrp();
4103 psinfo
->pr_sid
= getsid(0);
4104 psinfo
->pr_uid
= getuid();
4105 psinfo
->pr_gid
= getgid();
4107 base_filename
= g_path_get_basename(ts
->bprm
->filename
);
4109 * Using strncpy here is fine: at max-length,
4110 * this field is not NUL-terminated.
4112 (void) strncpy(psinfo
->pr_fname
, base_filename
,
4113 sizeof(psinfo
->pr_fname
));
4115 g_free(base_filename
);
4116 bswap_psinfo(psinfo
);
4120 static void fill_auxv_note(struct memelfnote
*note
, const TaskState
*ts
)
4122 elf_addr_t auxv
= (elf_addr_t
)ts
->info
->saved_auxv
;
4123 elf_addr_t orig_auxv
= auxv
;
4125 int len
= ts
->info
->auxv_len
;
4128 * Auxiliary vector is stored in target process stack. It contains
4129 * {type, value} pairs that we need to dump into note. This is not
4130 * strictly necessary but we do it here for sake of completeness.
4133 /* read in whole auxv vector and copy it to memelfnote */
4134 ptr
= lock_user(VERIFY_READ
, orig_auxv
, len
, 0);
4136 fill_note(note
, "CORE", NT_AUXV
, len
, ptr
);
4137 unlock_user(ptr
, auxv
, len
);
4142 * Constructs name of coredump file. We have following convention
4144 * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
4146 * Returns the filename
4148 static char *core_dump_filename(const TaskState
*ts
)
4150 g_autoptr(GDateTime
) now
= g_date_time_new_now_local();
4151 g_autofree
char *nowstr
= g_date_time_format(now
, "%Y%m%d-%H%M%S");
4152 g_autofree
char *base_filename
= g_path_get_basename(ts
->bprm
->filename
);
4154 return g_strdup_printf("qemu_%s_%s_%d.core",
4155 base_filename
, nowstr
, (int)getpid());
4158 static int dump_write(int fd
, const void *ptr
, size_t size
)
4160 const char *bufp
= (const char *)ptr
;
4161 ssize_t bytes_written
, bytes_left
;
4162 struct rlimit dumpsize
;
4166 getrlimit(RLIMIT_CORE
, &dumpsize
);
4167 if ((pos
= lseek(fd
, 0, SEEK_CUR
))==-1) {
4168 if (errno
== ESPIPE
) { /* not a seekable stream */
4174 if (dumpsize
.rlim_cur
<= pos
) {
4176 } else if (dumpsize
.rlim_cur
== RLIM_INFINITY
) {
4179 size_t limit_left
=dumpsize
.rlim_cur
- pos
;
4180 bytes_left
= limit_left
>= size
? size
: limit_left
;
4185 * In normal conditions, single write(2) should do but
4186 * in case of socket etc. this mechanism is more portable.
4189 bytes_written
= write(fd
, bufp
, bytes_left
);
4190 if (bytes_written
< 0) {
4194 } else if (bytes_written
== 0) { /* eof */
4197 bufp
+= bytes_written
;
4198 bytes_left
-= bytes_written
;
4199 } while (bytes_left
> 0);
4204 static int write_note(struct memelfnote
*men
, int fd
)
4208 en
.n_namesz
= men
->namesz
;
4209 en
.n_type
= men
->type
;
4210 en
.n_descsz
= men
->datasz
;
4214 if (dump_write(fd
, &en
, sizeof(en
)) != 0)
4216 if (dump_write(fd
, men
->name
, men
->namesz_rounded
) != 0)
4218 if (dump_write(fd
, men
->data
, men
->datasz_rounded
) != 0)
4224 static void fill_thread_info(struct elf_note_info
*info
, const CPUArchState
*env
)
4226 CPUState
*cpu
= env_cpu((CPUArchState
*)env
);
4227 TaskState
*ts
= (TaskState
*)cpu
->opaque
;
4228 struct elf_thread_status
*ets
;
4230 ets
= g_malloc0(sizeof (*ets
));
4231 ets
->num_notes
= 1; /* only prstatus is dumped */
4232 fill_prstatus(&ets
->prstatus
, ts
, 0);
4233 elf_core_copy_regs(&ets
->prstatus
.pr_reg
, env
);
4234 fill_note(&ets
->notes
[0], "CORE", NT_PRSTATUS
, sizeof (ets
->prstatus
),
4237 QTAILQ_INSERT_TAIL(&info
->thread_list
, ets
, ets_link
);
4239 info
->notes_size
+= note_size(&ets
->notes
[0]);
4242 static void init_note_info(struct elf_note_info
*info
)
4244 /* Initialize the elf_note_info structure so that it is at
4245 * least safe to call free_note_info() on it. Must be
4246 * called before calling fill_note_info().
4248 memset(info
, 0, sizeof (*info
));
4249 QTAILQ_INIT(&info
->thread_list
);
4252 static int fill_note_info(struct elf_note_info
*info
,
4253 long signr
, const CPUArchState
*env
)
4256 CPUState
*cpu
= env_cpu((CPUArchState
*)env
);
4257 TaskState
*ts
= (TaskState
*)cpu
->opaque
;
4260 info
->notes
= g_new0(struct memelfnote
, NUMNOTES
);
4261 if (info
->notes
== NULL
)
4263 info
->prstatus
= g_malloc0(sizeof (*info
->prstatus
));
4264 if (info
->prstatus
== NULL
)
4266 info
->psinfo
= g_malloc0(sizeof (*info
->psinfo
));
4267 if (info
->prstatus
== NULL
)
4271 * First fill in status (and registers) of current thread
4272 * including process info & aux vector.
4274 fill_prstatus(info
->prstatus
, ts
, signr
);
4275 elf_core_copy_regs(&info
->prstatus
->pr_reg
, env
);
4276 fill_note(&info
->notes
[0], "CORE", NT_PRSTATUS
,
4277 sizeof (*info
->prstatus
), info
->prstatus
);
4278 fill_psinfo(info
->psinfo
, ts
);
4279 fill_note(&info
->notes
[1], "CORE", NT_PRPSINFO
,
4280 sizeof (*info
->psinfo
), info
->psinfo
);
4281 fill_auxv_note(&info
->notes
[2], ts
);
4284 info
->notes_size
= 0;
4285 for (i
= 0; i
< info
->numnote
; i
++)
4286 info
->notes_size
+= note_size(&info
->notes
[i
]);
4288 /* read and fill status of all threads */
4289 WITH_QEMU_LOCK_GUARD(&qemu_cpu_list_lock
) {
4291 if (cpu
== thread_cpu
) {
4294 fill_thread_info(info
, cpu
->env_ptr
);
4301 static void free_note_info(struct elf_note_info
*info
)
4303 struct elf_thread_status
*ets
;
4305 while (!QTAILQ_EMPTY(&info
->thread_list
)) {
4306 ets
= QTAILQ_FIRST(&info
->thread_list
);
4307 QTAILQ_REMOVE(&info
->thread_list
, ets
, ets_link
);
4311 g_free(info
->prstatus
);
4312 g_free(info
->psinfo
);
4313 g_free(info
->notes
);
4316 static int write_note_info(struct elf_note_info
*info
, int fd
)
4318 struct elf_thread_status
*ets
;
4321 /* write prstatus, psinfo and auxv for current thread */
4322 for (i
= 0; i
< info
->numnote
; i
++)
4323 if ((error
= write_note(&info
->notes
[i
], fd
)) != 0)
4326 /* write prstatus for each thread */
4327 QTAILQ_FOREACH(ets
, &info
->thread_list
, ets_link
) {
4328 if ((error
= write_note(&ets
->notes
[0], fd
)) != 0)
4336 * Write out ELF coredump.
4338 * See documentation of ELF object file format in:
4339 * http://www.caldera.com/developers/devspecs/gabi41.pdf
4341 * Coredump format in linux is following:
4343 * 0 +----------------------+ \
4344 * | ELF header | ET_CORE |
4345 * +----------------------+ |
4346 * | ELF program headers | |--- headers
4347 * | - NOTE section | |
4348 * | - PT_LOAD sections | |
4349 * +----------------------+ /
4354 * +----------------------+ <-- aligned to target page
4355 * | Process memory dump |
4360 * +----------------------+
4362 * NT_PRSTATUS -> struct elf_prstatus (per thread)
4363 * NT_PRSINFO -> struct elf_prpsinfo
4364 * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()).
4366 * Format follows System V format as close as possible. Current
4367 * version limitations are as follows:
4368 * - no floating point registers are dumped
4370 * Function returns 0 in case of success, negative errno otherwise.
4372 * TODO: make this work also during runtime: it should be
4373 * possible to force coredump from running process and then
4374 * continue processing. For example qemu could set up SIGUSR2
4375 * handler (provided that target process haven't registered
4376 * handler for that) that does the dump when signal is received.
4378 static int elf_core_dump(int signr
, const CPUArchState
*env
)
4380 const CPUState
*cpu
= env_cpu((CPUArchState
*)env
);
4381 const TaskState
*ts
= (const TaskState
*)cpu
->opaque
;
4382 struct vm_area_struct
*vma
= NULL
;
4383 g_autofree
char *corefile
= NULL
;
4384 struct elf_note_info info
;
4386 struct elf_phdr phdr
;
4387 struct rlimit dumpsize
;
4388 struct mm_struct
*mm
= NULL
;
4389 off_t offset
= 0, data_offset
= 0;
4393 init_note_info(&info
);
4396 getrlimit(RLIMIT_CORE
, &dumpsize
);
4397 if (dumpsize
.rlim_cur
== 0)
4400 corefile
= core_dump_filename(ts
);
4402 if ((fd
= open(corefile
, O_WRONLY
| O_CREAT
,
4403 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
)) < 0)
4407 * Walk through target process memory mappings and
4408 * set up structure containing this information. After
4409 * this point vma_xxx functions can be used.
4411 if ((mm
= vma_init()) == NULL
)
4414 walk_memory_regions(mm
, vma_walker
);
4415 segs
= vma_get_mapping_count(mm
);
4418 * Construct valid coredump ELF header. We also
4419 * add one more segment for notes.
4421 fill_elf_header(&elf
, segs
+ 1, ELF_MACHINE
, 0);
4422 if (dump_write(fd
, &elf
, sizeof (elf
)) != 0)
4425 /* fill in the in-memory version of notes */
4426 if (fill_note_info(&info
, signr
, env
) < 0)
4429 offset
+= sizeof (elf
); /* elf header */
4430 offset
+= (segs
+ 1) * sizeof (struct elf_phdr
); /* program headers */
4432 /* write out notes program header */
4433 fill_elf_note_phdr(&phdr
, info
.notes_size
, offset
);
4435 offset
+= info
.notes_size
;
4436 if (dump_write(fd
, &phdr
, sizeof (phdr
)) != 0)
4440 * ELF specification wants data to start at page boundary so
4443 data_offset
= offset
= roundup(offset
, ELF_EXEC_PAGESIZE
);
4446 * Write program headers for memory regions mapped in
4447 * the target process.
4449 for (vma
= vma_first(mm
); vma
!= NULL
; vma
= vma_next(vma
)) {
4450 (void) memset(&phdr
, 0, sizeof (phdr
));
4452 phdr
.p_type
= PT_LOAD
;
4453 phdr
.p_offset
= offset
;
4454 phdr
.p_vaddr
= vma
->vma_start
;
4456 phdr
.p_filesz
= vma_dump_size(vma
);
4457 offset
+= phdr
.p_filesz
;
4458 phdr
.p_memsz
= vma
->vma_end
- vma
->vma_start
;
4459 phdr
.p_flags
= vma
->vma_flags
& PROT_READ
? PF_R
: 0;
4460 if (vma
->vma_flags
& PROT_WRITE
)
4461 phdr
.p_flags
|= PF_W
;
4462 if (vma
->vma_flags
& PROT_EXEC
)
4463 phdr
.p_flags
|= PF_X
;
4464 phdr
.p_align
= ELF_EXEC_PAGESIZE
;
4466 bswap_phdr(&phdr
, 1);
4467 if (dump_write(fd
, &phdr
, sizeof(phdr
)) != 0) {
4473 * Next we write notes just after program headers. No
4474 * alignment needed here.
4476 if (write_note_info(&info
, fd
) < 0)
4479 /* align data to page boundary */
4480 if (lseek(fd
, data_offset
, SEEK_SET
) != data_offset
)
4484 * Finally we can dump process memory into corefile as well.
4486 for (vma
= vma_first(mm
); vma
!= NULL
; vma
= vma_next(vma
)) {
4490 end
= vma
->vma_start
+ vma_dump_size(vma
);
4492 for (addr
= vma
->vma_start
; addr
< end
;
4493 addr
+= TARGET_PAGE_SIZE
) {
4494 char page
[TARGET_PAGE_SIZE
];
4498 * Read in page from target process memory and
4499 * write it to coredump file.
4501 error
= copy_from_user(page
, addr
, sizeof (page
));
4503 (void) fprintf(stderr
, "unable to dump " TARGET_ABI_FMT_lx
"\n",
4508 if (dump_write(fd
, page
, TARGET_PAGE_SIZE
) < 0)
4514 free_note_info(&info
);
4523 #endif /* USE_ELF_CORE_DUMP */
4525 void do_init_thread(struct target_pt_regs
*regs
, struct image_info
*infop
)
4527 init_thread(regs
, infop
);