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 "qapi/error.h"
21 #include "target_signal.h"
33 #define ELF_OSABI ELFOSABI_SYSV
35 /* from personality.h */
38 * Flags for bug emulation.
40 * These occupy the top three bytes.
43 ADDR_NO_RANDOMIZE
= 0x0040000, /* disable randomization of VA space */
44 FDPIC_FUNCPTRS
= 0x0080000, /* userspace function ptrs point to
45 descriptors (signal handling) */
46 MMAP_PAGE_ZERO
= 0x0100000,
47 ADDR_COMPAT_LAYOUT
= 0x0200000,
48 READ_IMPLIES_EXEC
= 0x0400000,
49 ADDR_LIMIT_32BIT
= 0x0800000,
50 SHORT_INODE
= 0x1000000,
51 WHOLE_SECONDS
= 0x2000000,
52 STICKY_TIMEOUTS
= 0x4000000,
53 ADDR_LIMIT_3GB
= 0x8000000,
59 * These go in the low byte. Avoid using the top bit, it will
60 * conflict with error returns.
64 PER_LINUX_32BIT
= 0x0000 | ADDR_LIMIT_32BIT
,
65 PER_LINUX_FDPIC
= 0x0000 | FDPIC_FUNCPTRS
,
66 PER_SVR4
= 0x0001 | STICKY_TIMEOUTS
| MMAP_PAGE_ZERO
,
67 PER_SVR3
= 0x0002 | STICKY_TIMEOUTS
| SHORT_INODE
,
68 PER_SCOSVR3
= 0x0003 | STICKY_TIMEOUTS
| WHOLE_SECONDS
| SHORT_INODE
,
69 PER_OSR5
= 0x0003 | STICKY_TIMEOUTS
| WHOLE_SECONDS
,
70 PER_WYSEV386
= 0x0004 | STICKY_TIMEOUTS
| SHORT_INODE
,
71 PER_ISCR4
= 0x0005 | STICKY_TIMEOUTS
,
73 PER_SUNOS
= 0x0006 | STICKY_TIMEOUTS
,
74 PER_XENIX
= 0x0007 | STICKY_TIMEOUTS
| SHORT_INODE
,
76 PER_LINUX32_3GB
= 0x0008 | ADDR_LIMIT_3GB
,
77 PER_IRIX32
= 0x0009 | STICKY_TIMEOUTS
,/* IRIX5 32-bit */
78 PER_IRIXN32
= 0x000a | STICKY_TIMEOUTS
,/* IRIX6 new 32-bit */
79 PER_IRIX64
= 0x000b | STICKY_TIMEOUTS
,/* IRIX6 64-bit */
81 PER_SOLARIS
= 0x000d | STICKY_TIMEOUTS
,
82 PER_UW7
= 0x000e | STICKY_TIMEOUTS
| MMAP_PAGE_ZERO
,
83 PER_OSF4
= 0x000f, /* OSF/1 v4 */
89 * Return the base personality without flags.
91 #define personality(pers) (pers & PER_MASK)
93 int info_is_fdpic(struct image_info
*info
)
95 return info
->personality
== PER_LINUX_FDPIC
;
98 /* this flag is uneffective under linux too, should be deleted */
100 #define MAP_DENYWRITE 0
103 /* should probably go in elf.h */
108 #if TARGET_BIG_ENDIAN
109 #define ELF_DATA ELFDATA2MSB
111 #define ELF_DATA ELFDATA2LSB
114 #ifdef TARGET_ABI_MIPSN32
115 typedef abi_ullong target_elf_greg_t
;
116 #define tswapreg(ptr) tswap64(ptr)
118 typedef abi_ulong target_elf_greg_t
;
119 #define tswapreg(ptr) tswapal(ptr)
123 typedef abi_ushort target_uid_t
;
124 typedef abi_ushort target_gid_t
;
126 typedef abi_uint target_uid_t
;
127 typedef abi_uint target_gid_t
;
129 typedef abi_int target_pid_t
;
133 #define ELF_HWCAP get_elf_hwcap()
135 static uint32_t get_elf_hwcap(void)
137 X86CPU
*cpu
= X86_CPU(thread_cpu
);
139 return cpu
->env
.features
[FEAT_1_EDX
];
143 #define ELF_START_MMAP 0x2aaaaab000ULL
145 #define ELF_CLASS ELFCLASS64
146 #define ELF_ARCH EM_X86_64
148 #define ELF_PLATFORM "x86_64"
150 static inline void init_thread(struct target_pt_regs
*regs
, struct image_info
*infop
)
153 regs
->rsp
= infop
->start_stack
;
154 regs
->rip
= infop
->entry
;
158 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
161 * Note that ELF_NREG should be 29 as there should be place for
162 * TRAPNO and ERR "registers" as well but linux doesn't dump
165 * See linux kernel: arch/x86/include/asm/elf.h
167 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUX86State
*env
)
169 (*regs
)[0] = tswapreg(env
->regs
[15]);
170 (*regs
)[1] = tswapreg(env
->regs
[14]);
171 (*regs
)[2] = tswapreg(env
->regs
[13]);
172 (*regs
)[3] = tswapreg(env
->regs
[12]);
173 (*regs
)[4] = tswapreg(env
->regs
[R_EBP
]);
174 (*regs
)[5] = tswapreg(env
->regs
[R_EBX
]);
175 (*regs
)[6] = tswapreg(env
->regs
[11]);
176 (*regs
)[7] = tswapreg(env
->regs
[10]);
177 (*regs
)[8] = tswapreg(env
->regs
[9]);
178 (*regs
)[9] = tswapreg(env
->regs
[8]);
179 (*regs
)[10] = tswapreg(env
->regs
[R_EAX
]);
180 (*regs
)[11] = tswapreg(env
->regs
[R_ECX
]);
181 (*regs
)[12] = tswapreg(env
->regs
[R_EDX
]);
182 (*regs
)[13] = tswapreg(env
->regs
[R_ESI
]);
183 (*regs
)[14] = tswapreg(env
->regs
[R_EDI
]);
184 (*regs
)[15] = tswapreg(env
->regs
[R_EAX
]); /* XXX */
185 (*regs
)[16] = tswapreg(env
->eip
);
186 (*regs
)[17] = tswapreg(env
->segs
[R_CS
].selector
& 0xffff);
187 (*regs
)[18] = tswapreg(env
->eflags
);
188 (*regs
)[19] = tswapreg(env
->regs
[R_ESP
]);
189 (*regs
)[20] = tswapreg(env
->segs
[R_SS
].selector
& 0xffff);
190 (*regs
)[21] = tswapreg(env
->segs
[R_FS
].selector
& 0xffff);
191 (*regs
)[22] = tswapreg(env
->segs
[R_GS
].selector
& 0xffff);
192 (*regs
)[23] = tswapreg(env
->segs
[R_DS
].selector
& 0xffff);
193 (*regs
)[24] = tswapreg(env
->segs
[R_ES
].selector
& 0xffff);
194 (*regs
)[25] = tswapreg(env
->segs
[R_FS
].selector
& 0xffff);
195 (*regs
)[26] = tswapreg(env
->segs
[R_GS
].selector
& 0xffff);
200 #define ELF_START_MMAP 0x80000000
203 * This is used to ensure we don't load something for the wrong architecture.
205 #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
208 * These are used to set parameters in the core dumps.
210 #define ELF_CLASS ELFCLASS32
211 #define ELF_ARCH EM_386
213 #define ELF_PLATFORM get_elf_platform()
215 static const char *get_elf_platform(void)
217 static char elf_platform
[] = "i386";
218 int family
= object_property_get_int(OBJECT(thread_cpu
), "family", NULL
);
223 elf_platform
[1] = '0' + family
;
228 static inline void init_thread(struct target_pt_regs
*regs
,
229 struct image_info
*infop
)
231 regs
->esp
= infop
->start_stack
;
232 regs
->eip
= infop
->entry
;
234 /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program
235 starts %edx contains a pointer to a function which might be
236 registered using `atexit'. This provides a mean for the
237 dynamic linker to call DT_FINI functions for shared libraries
238 that have been loaded before the code runs.
240 A value of 0 tells we have no such handler. */
245 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
248 * Note that ELF_NREG should be 19 as there should be place for
249 * TRAPNO and ERR "registers" as well but linux doesn't dump
252 * See linux kernel: arch/x86/include/asm/elf.h
254 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUX86State
*env
)
256 (*regs
)[0] = tswapreg(env
->regs
[R_EBX
]);
257 (*regs
)[1] = tswapreg(env
->regs
[R_ECX
]);
258 (*regs
)[2] = tswapreg(env
->regs
[R_EDX
]);
259 (*regs
)[3] = tswapreg(env
->regs
[R_ESI
]);
260 (*regs
)[4] = tswapreg(env
->regs
[R_EDI
]);
261 (*regs
)[5] = tswapreg(env
->regs
[R_EBP
]);
262 (*regs
)[6] = tswapreg(env
->regs
[R_EAX
]);
263 (*regs
)[7] = tswapreg(env
->segs
[R_DS
].selector
& 0xffff);
264 (*regs
)[8] = tswapreg(env
->segs
[R_ES
].selector
& 0xffff);
265 (*regs
)[9] = tswapreg(env
->segs
[R_FS
].selector
& 0xffff);
266 (*regs
)[10] = tswapreg(env
->segs
[R_GS
].selector
& 0xffff);
267 (*regs
)[11] = tswapreg(env
->regs
[R_EAX
]); /* XXX */
268 (*regs
)[12] = tswapreg(env
->eip
);
269 (*regs
)[13] = tswapreg(env
->segs
[R_CS
].selector
& 0xffff);
270 (*regs
)[14] = tswapreg(env
->eflags
);
271 (*regs
)[15] = tswapreg(env
->regs
[R_ESP
]);
272 (*regs
)[16] = tswapreg(env
->segs
[R_SS
].selector
& 0xffff);
276 #define USE_ELF_CORE_DUMP
277 #define ELF_EXEC_PAGESIZE 4096
283 #ifndef TARGET_AARCH64
284 /* 32 bit ARM definitions */
286 #define ELF_START_MMAP 0x80000000
288 #define ELF_ARCH EM_ARM
289 #define ELF_CLASS ELFCLASS32
291 static inline void init_thread(struct target_pt_regs
*regs
,
292 struct image_info
*infop
)
294 abi_long stack
= infop
->start_stack
;
295 memset(regs
, 0, sizeof(*regs
));
297 regs
->uregs
[16] = ARM_CPU_MODE_USR
;
298 if (infop
->entry
& 1) {
299 regs
->uregs
[16] |= CPSR_T
;
301 regs
->uregs
[15] = infop
->entry
& 0xfffffffe;
302 regs
->uregs
[13] = infop
->start_stack
;
303 /* FIXME - what to for failure of get_user()? */
304 get_user_ual(regs
->uregs
[2], stack
+ 8); /* envp */
305 get_user_ual(regs
->uregs
[1], stack
+ 4); /* envp */
306 /* XXX: it seems that r0 is zeroed after ! */
308 /* For uClinux PIC binaries. */
309 /* XXX: Linux does this only on ARM with no MMU (do we care ?) */
310 regs
->uregs
[10] = infop
->start_data
;
312 /* Support ARM FDPIC. */
313 if (info_is_fdpic(infop
)) {
314 /* As described in the ABI document, r7 points to the loadmap info
315 * prepared by the kernel. If an interpreter is needed, r8 points
316 * to the interpreter loadmap and r9 points to the interpreter
317 * PT_DYNAMIC info. If no interpreter is needed, r8 is zero, and
318 * r9 points to the main program PT_DYNAMIC info.
320 regs
->uregs
[7] = infop
->loadmap_addr
;
321 if (infop
->interpreter_loadmap_addr
) {
322 /* Executable is dynamically loaded. */
323 regs
->uregs
[8] = infop
->interpreter_loadmap_addr
;
324 regs
->uregs
[9] = infop
->interpreter_pt_dynamic_addr
;
327 regs
->uregs
[9] = infop
->pt_dynamic_addr
;
333 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
335 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUARMState
*env
)
337 (*regs
)[0] = tswapreg(env
->regs
[0]);
338 (*regs
)[1] = tswapreg(env
->regs
[1]);
339 (*regs
)[2] = tswapreg(env
->regs
[2]);
340 (*regs
)[3] = tswapreg(env
->regs
[3]);
341 (*regs
)[4] = tswapreg(env
->regs
[4]);
342 (*regs
)[5] = tswapreg(env
->regs
[5]);
343 (*regs
)[6] = tswapreg(env
->regs
[6]);
344 (*regs
)[7] = tswapreg(env
->regs
[7]);
345 (*regs
)[8] = tswapreg(env
->regs
[8]);
346 (*regs
)[9] = tswapreg(env
->regs
[9]);
347 (*regs
)[10] = tswapreg(env
->regs
[10]);
348 (*regs
)[11] = tswapreg(env
->regs
[11]);
349 (*regs
)[12] = tswapreg(env
->regs
[12]);
350 (*regs
)[13] = tswapreg(env
->regs
[13]);
351 (*regs
)[14] = tswapreg(env
->regs
[14]);
352 (*regs
)[15] = tswapreg(env
->regs
[15]);
354 (*regs
)[16] = tswapreg(cpsr_read((CPUARMState
*)env
));
355 (*regs
)[17] = tswapreg(env
->regs
[0]); /* XXX */
358 #define USE_ELF_CORE_DUMP
359 #define ELF_EXEC_PAGESIZE 4096
363 ARM_HWCAP_ARM_SWP
= 1 << 0,
364 ARM_HWCAP_ARM_HALF
= 1 << 1,
365 ARM_HWCAP_ARM_THUMB
= 1 << 2,
366 ARM_HWCAP_ARM_26BIT
= 1 << 3,
367 ARM_HWCAP_ARM_FAST_MULT
= 1 << 4,
368 ARM_HWCAP_ARM_FPA
= 1 << 5,
369 ARM_HWCAP_ARM_VFP
= 1 << 6,
370 ARM_HWCAP_ARM_EDSP
= 1 << 7,
371 ARM_HWCAP_ARM_JAVA
= 1 << 8,
372 ARM_HWCAP_ARM_IWMMXT
= 1 << 9,
373 ARM_HWCAP_ARM_CRUNCH
= 1 << 10,
374 ARM_HWCAP_ARM_THUMBEE
= 1 << 11,
375 ARM_HWCAP_ARM_NEON
= 1 << 12,
376 ARM_HWCAP_ARM_VFPv3
= 1 << 13,
377 ARM_HWCAP_ARM_VFPv3D16
= 1 << 14,
378 ARM_HWCAP_ARM_TLS
= 1 << 15,
379 ARM_HWCAP_ARM_VFPv4
= 1 << 16,
380 ARM_HWCAP_ARM_IDIVA
= 1 << 17,
381 ARM_HWCAP_ARM_IDIVT
= 1 << 18,
382 ARM_HWCAP_ARM_VFPD32
= 1 << 19,
383 ARM_HWCAP_ARM_LPAE
= 1 << 20,
384 ARM_HWCAP_ARM_EVTSTRM
= 1 << 21,
388 ARM_HWCAP2_ARM_AES
= 1 << 0,
389 ARM_HWCAP2_ARM_PMULL
= 1 << 1,
390 ARM_HWCAP2_ARM_SHA1
= 1 << 2,
391 ARM_HWCAP2_ARM_SHA2
= 1 << 3,
392 ARM_HWCAP2_ARM_CRC32
= 1 << 4,
395 /* The commpage only exists for 32 bit kernels */
397 #define HI_COMMPAGE (intptr_t)0xffff0f00u
399 static bool init_guest_commpage(void)
401 abi_ptr commpage
= HI_COMMPAGE
& -qemu_host_page_size
;
402 void *want
= g2h_untagged(commpage
);
403 void *addr
= mmap(want
, qemu_host_page_size
, PROT_READ
| PROT_WRITE
,
404 MAP_ANONYMOUS
| MAP_PRIVATE
| MAP_FIXED
, -1, 0);
406 if (addr
== MAP_FAILED
) {
407 perror("Allocating guest commpage");
414 /* Set kernel helper versions; rest of page is 0. */
415 __put_user(5, (uint32_t *)g2h_untagged(0xffff0ffcu
));
417 if (mprotect(addr
, qemu_host_page_size
, PROT_READ
)) {
418 perror("Protecting guest commpage");
422 page_set_flags(commpage
, commpage
+ qemu_host_page_size
,
423 PAGE_READ
| PAGE_EXEC
| PAGE_VALID
);
427 #define ELF_HWCAP get_elf_hwcap()
428 #define ELF_HWCAP2 get_elf_hwcap2()
430 static uint32_t get_elf_hwcap(void)
432 ARMCPU
*cpu
= ARM_CPU(thread_cpu
);
435 hwcaps
|= ARM_HWCAP_ARM_SWP
;
436 hwcaps
|= ARM_HWCAP_ARM_HALF
;
437 hwcaps
|= ARM_HWCAP_ARM_THUMB
;
438 hwcaps
|= ARM_HWCAP_ARM_FAST_MULT
;
440 /* probe for the extra features */
441 #define GET_FEATURE(feat, hwcap) \
442 do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0)
444 #define GET_FEATURE_ID(feat, hwcap) \
445 do { if (cpu_isar_feature(feat, cpu)) { hwcaps |= hwcap; } } while (0)
447 /* EDSP is in v5TE and above, but all our v5 CPUs are v5TE */
448 GET_FEATURE(ARM_FEATURE_V5
, ARM_HWCAP_ARM_EDSP
);
449 GET_FEATURE(ARM_FEATURE_IWMMXT
, ARM_HWCAP_ARM_IWMMXT
);
450 GET_FEATURE(ARM_FEATURE_THUMB2EE
, ARM_HWCAP_ARM_THUMBEE
);
451 GET_FEATURE(ARM_FEATURE_NEON
, ARM_HWCAP_ARM_NEON
);
452 GET_FEATURE(ARM_FEATURE_V6K
, ARM_HWCAP_ARM_TLS
);
453 GET_FEATURE(ARM_FEATURE_LPAE
, ARM_HWCAP_ARM_LPAE
);
454 GET_FEATURE_ID(aa32_arm_div
, ARM_HWCAP_ARM_IDIVA
);
455 GET_FEATURE_ID(aa32_thumb_div
, ARM_HWCAP_ARM_IDIVT
);
456 GET_FEATURE_ID(aa32_vfp
, ARM_HWCAP_ARM_VFP
);
458 if (cpu_isar_feature(aa32_fpsp_v3
, cpu
) ||
459 cpu_isar_feature(aa32_fpdp_v3
, cpu
)) {
460 hwcaps
|= ARM_HWCAP_ARM_VFPv3
;
461 if (cpu_isar_feature(aa32_simd_r32
, cpu
)) {
462 hwcaps
|= ARM_HWCAP_ARM_VFPD32
;
464 hwcaps
|= ARM_HWCAP_ARM_VFPv3D16
;
467 GET_FEATURE_ID(aa32_simdfmac
, ARM_HWCAP_ARM_VFPv4
);
472 static uint32_t get_elf_hwcap2(void)
474 ARMCPU
*cpu
= ARM_CPU(thread_cpu
);
477 GET_FEATURE_ID(aa32_aes
, ARM_HWCAP2_ARM_AES
);
478 GET_FEATURE_ID(aa32_pmull
, ARM_HWCAP2_ARM_PMULL
);
479 GET_FEATURE_ID(aa32_sha1
, ARM_HWCAP2_ARM_SHA1
);
480 GET_FEATURE_ID(aa32_sha2
, ARM_HWCAP2_ARM_SHA2
);
481 GET_FEATURE_ID(aa32_crc32
, ARM_HWCAP2_ARM_CRC32
);
486 #undef GET_FEATURE_ID
488 #define ELF_PLATFORM get_elf_platform()
490 static const char *get_elf_platform(void)
492 CPUARMState
*env
= thread_cpu
->env_ptr
;
494 #if TARGET_BIG_ENDIAN
500 if (arm_feature(env
, ARM_FEATURE_V8
)) {
502 } else if (arm_feature(env
, ARM_FEATURE_V7
)) {
503 if (arm_feature(env
, ARM_FEATURE_M
)) {
508 } else if (arm_feature(env
, ARM_FEATURE_V6
)) {
510 } else if (arm_feature(env
, ARM_FEATURE_V5
)) {
520 /* 64 bit ARM definitions */
521 #define ELF_START_MMAP 0x80000000
523 #define ELF_ARCH EM_AARCH64
524 #define ELF_CLASS ELFCLASS64
525 #if TARGET_BIG_ENDIAN
526 # define ELF_PLATFORM "aarch64_be"
528 # define ELF_PLATFORM "aarch64"
531 static inline void init_thread(struct target_pt_regs
*regs
,
532 struct image_info
*infop
)
534 abi_long stack
= infop
->start_stack
;
535 memset(regs
, 0, sizeof(*regs
));
537 regs
->pc
= infop
->entry
& ~0x3ULL
;
542 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
544 static void elf_core_copy_regs(target_elf_gregset_t
*regs
,
545 const CPUARMState
*env
)
549 for (i
= 0; i
< 32; i
++) {
550 (*regs
)[i
] = tswapreg(env
->xregs
[i
]);
552 (*regs
)[32] = tswapreg(env
->pc
);
553 (*regs
)[33] = tswapreg(pstate_read((CPUARMState
*)env
));
556 #define USE_ELF_CORE_DUMP
557 #define ELF_EXEC_PAGESIZE 4096
560 ARM_HWCAP_A64_FP
= 1 << 0,
561 ARM_HWCAP_A64_ASIMD
= 1 << 1,
562 ARM_HWCAP_A64_EVTSTRM
= 1 << 2,
563 ARM_HWCAP_A64_AES
= 1 << 3,
564 ARM_HWCAP_A64_PMULL
= 1 << 4,
565 ARM_HWCAP_A64_SHA1
= 1 << 5,
566 ARM_HWCAP_A64_SHA2
= 1 << 6,
567 ARM_HWCAP_A64_CRC32
= 1 << 7,
568 ARM_HWCAP_A64_ATOMICS
= 1 << 8,
569 ARM_HWCAP_A64_FPHP
= 1 << 9,
570 ARM_HWCAP_A64_ASIMDHP
= 1 << 10,
571 ARM_HWCAP_A64_CPUID
= 1 << 11,
572 ARM_HWCAP_A64_ASIMDRDM
= 1 << 12,
573 ARM_HWCAP_A64_JSCVT
= 1 << 13,
574 ARM_HWCAP_A64_FCMA
= 1 << 14,
575 ARM_HWCAP_A64_LRCPC
= 1 << 15,
576 ARM_HWCAP_A64_DCPOP
= 1 << 16,
577 ARM_HWCAP_A64_SHA3
= 1 << 17,
578 ARM_HWCAP_A64_SM3
= 1 << 18,
579 ARM_HWCAP_A64_SM4
= 1 << 19,
580 ARM_HWCAP_A64_ASIMDDP
= 1 << 20,
581 ARM_HWCAP_A64_SHA512
= 1 << 21,
582 ARM_HWCAP_A64_SVE
= 1 << 22,
583 ARM_HWCAP_A64_ASIMDFHM
= 1 << 23,
584 ARM_HWCAP_A64_DIT
= 1 << 24,
585 ARM_HWCAP_A64_USCAT
= 1 << 25,
586 ARM_HWCAP_A64_ILRCPC
= 1 << 26,
587 ARM_HWCAP_A64_FLAGM
= 1 << 27,
588 ARM_HWCAP_A64_SSBS
= 1 << 28,
589 ARM_HWCAP_A64_SB
= 1 << 29,
590 ARM_HWCAP_A64_PACA
= 1 << 30,
591 ARM_HWCAP_A64_PACG
= 1UL << 31,
593 ARM_HWCAP2_A64_DCPODP
= 1 << 0,
594 ARM_HWCAP2_A64_SVE2
= 1 << 1,
595 ARM_HWCAP2_A64_SVEAES
= 1 << 2,
596 ARM_HWCAP2_A64_SVEPMULL
= 1 << 3,
597 ARM_HWCAP2_A64_SVEBITPERM
= 1 << 4,
598 ARM_HWCAP2_A64_SVESHA3
= 1 << 5,
599 ARM_HWCAP2_A64_SVESM4
= 1 << 6,
600 ARM_HWCAP2_A64_FLAGM2
= 1 << 7,
601 ARM_HWCAP2_A64_FRINT
= 1 << 8,
602 ARM_HWCAP2_A64_SVEI8MM
= 1 << 9,
603 ARM_HWCAP2_A64_SVEF32MM
= 1 << 10,
604 ARM_HWCAP2_A64_SVEF64MM
= 1 << 11,
605 ARM_HWCAP2_A64_SVEBF16
= 1 << 12,
606 ARM_HWCAP2_A64_I8MM
= 1 << 13,
607 ARM_HWCAP2_A64_BF16
= 1 << 14,
608 ARM_HWCAP2_A64_DGH
= 1 << 15,
609 ARM_HWCAP2_A64_RNG
= 1 << 16,
610 ARM_HWCAP2_A64_BTI
= 1 << 17,
611 ARM_HWCAP2_A64_MTE
= 1 << 18,
612 ARM_HWCAP2_A64_ECV
= 1 << 19,
613 ARM_HWCAP2_A64_AFP
= 1 << 20,
614 ARM_HWCAP2_A64_RPRES
= 1 << 21,
615 ARM_HWCAP2_A64_MTE3
= 1 << 22,
616 ARM_HWCAP2_A64_SME
= 1 << 23,
617 ARM_HWCAP2_A64_SME_I16I64
= 1 << 24,
618 ARM_HWCAP2_A64_SME_F64F64
= 1 << 25,
619 ARM_HWCAP2_A64_SME_I8I32
= 1 << 26,
620 ARM_HWCAP2_A64_SME_F16F32
= 1 << 27,
621 ARM_HWCAP2_A64_SME_B16F32
= 1 << 28,
622 ARM_HWCAP2_A64_SME_F32F32
= 1 << 29,
623 ARM_HWCAP2_A64_SME_FA64
= 1 << 30,
626 #define ELF_HWCAP get_elf_hwcap()
627 #define ELF_HWCAP2 get_elf_hwcap2()
629 #define GET_FEATURE_ID(feat, hwcap) \
630 do { if (cpu_isar_feature(feat, cpu)) { hwcaps |= hwcap; } } while (0)
632 static uint32_t get_elf_hwcap(void)
634 ARMCPU
*cpu
= ARM_CPU(thread_cpu
);
637 hwcaps
|= ARM_HWCAP_A64_FP
;
638 hwcaps
|= ARM_HWCAP_A64_ASIMD
;
639 hwcaps
|= ARM_HWCAP_A64_CPUID
;
641 /* probe for the extra features */
643 GET_FEATURE_ID(aa64_aes
, ARM_HWCAP_A64_AES
);
644 GET_FEATURE_ID(aa64_pmull
, ARM_HWCAP_A64_PMULL
);
645 GET_FEATURE_ID(aa64_sha1
, ARM_HWCAP_A64_SHA1
);
646 GET_FEATURE_ID(aa64_sha256
, ARM_HWCAP_A64_SHA2
);
647 GET_FEATURE_ID(aa64_sha512
, ARM_HWCAP_A64_SHA512
);
648 GET_FEATURE_ID(aa64_crc32
, ARM_HWCAP_A64_CRC32
);
649 GET_FEATURE_ID(aa64_sha3
, ARM_HWCAP_A64_SHA3
);
650 GET_FEATURE_ID(aa64_sm3
, ARM_HWCAP_A64_SM3
);
651 GET_FEATURE_ID(aa64_sm4
, ARM_HWCAP_A64_SM4
);
652 GET_FEATURE_ID(aa64_fp16
, ARM_HWCAP_A64_FPHP
| ARM_HWCAP_A64_ASIMDHP
);
653 GET_FEATURE_ID(aa64_atomics
, ARM_HWCAP_A64_ATOMICS
);
654 GET_FEATURE_ID(aa64_rdm
, ARM_HWCAP_A64_ASIMDRDM
);
655 GET_FEATURE_ID(aa64_dp
, ARM_HWCAP_A64_ASIMDDP
);
656 GET_FEATURE_ID(aa64_fcma
, ARM_HWCAP_A64_FCMA
);
657 GET_FEATURE_ID(aa64_sve
, ARM_HWCAP_A64_SVE
);
658 GET_FEATURE_ID(aa64_pauth
, ARM_HWCAP_A64_PACA
| ARM_HWCAP_A64_PACG
);
659 GET_FEATURE_ID(aa64_fhm
, ARM_HWCAP_A64_ASIMDFHM
);
660 GET_FEATURE_ID(aa64_jscvt
, ARM_HWCAP_A64_JSCVT
);
661 GET_FEATURE_ID(aa64_sb
, ARM_HWCAP_A64_SB
);
662 GET_FEATURE_ID(aa64_condm_4
, ARM_HWCAP_A64_FLAGM
);
663 GET_FEATURE_ID(aa64_dcpop
, ARM_HWCAP_A64_DCPOP
);
664 GET_FEATURE_ID(aa64_rcpc_8_3
, ARM_HWCAP_A64_LRCPC
);
665 GET_FEATURE_ID(aa64_rcpc_8_4
, ARM_HWCAP_A64_ILRCPC
);
670 static uint32_t get_elf_hwcap2(void)
672 ARMCPU
*cpu
= ARM_CPU(thread_cpu
);
675 GET_FEATURE_ID(aa64_dcpodp
, ARM_HWCAP2_A64_DCPODP
);
676 GET_FEATURE_ID(aa64_sve2
, ARM_HWCAP2_A64_SVE2
);
677 GET_FEATURE_ID(aa64_sve2_aes
, ARM_HWCAP2_A64_SVEAES
);
678 GET_FEATURE_ID(aa64_sve2_pmull128
, ARM_HWCAP2_A64_SVEPMULL
);
679 GET_FEATURE_ID(aa64_sve2_bitperm
, ARM_HWCAP2_A64_SVEBITPERM
);
680 GET_FEATURE_ID(aa64_sve2_sha3
, ARM_HWCAP2_A64_SVESHA3
);
681 GET_FEATURE_ID(aa64_sve2_sm4
, ARM_HWCAP2_A64_SVESM4
);
682 GET_FEATURE_ID(aa64_condm_5
, ARM_HWCAP2_A64_FLAGM2
);
683 GET_FEATURE_ID(aa64_frint
, ARM_HWCAP2_A64_FRINT
);
684 GET_FEATURE_ID(aa64_sve_i8mm
, ARM_HWCAP2_A64_SVEI8MM
);
685 GET_FEATURE_ID(aa64_sve_f32mm
, ARM_HWCAP2_A64_SVEF32MM
);
686 GET_FEATURE_ID(aa64_sve_f64mm
, ARM_HWCAP2_A64_SVEF64MM
);
687 GET_FEATURE_ID(aa64_sve_bf16
, ARM_HWCAP2_A64_SVEBF16
);
688 GET_FEATURE_ID(aa64_i8mm
, ARM_HWCAP2_A64_I8MM
);
689 GET_FEATURE_ID(aa64_bf16
, ARM_HWCAP2_A64_BF16
);
690 GET_FEATURE_ID(aa64_rndr
, ARM_HWCAP2_A64_RNG
);
691 GET_FEATURE_ID(aa64_bti
, ARM_HWCAP2_A64_BTI
);
692 GET_FEATURE_ID(aa64_mte
, ARM_HWCAP2_A64_MTE
);
693 GET_FEATURE_ID(aa64_sme
, (ARM_HWCAP2_A64_SME
|
694 ARM_HWCAP2_A64_SME_F32F32
|
695 ARM_HWCAP2_A64_SME_B16F32
|
696 ARM_HWCAP2_A64_SME_F16F32
|
697 ARM_HWCAP2_A64_SME_I8I32
));
698 GET_FEATURE_ID(aa64_sme_f64f64
, ARM_HWCAP2_A64_SME_F64F64
);
699 GET_FEATURE_ID(aa64_sme_i16i64
, ARM_HWCAP2_A64_SME_I16I64
);
700 GET_FEATURE_ID(aa64_sme_fa64
, ARM_HWCAP2_A64_SME_FA64
);
705 #undef GET_FEATURE_ID
707 #endif /* not TARGET_AARCH64 */
708 #endif /* TARGET_ARM */
711 #ifdef TARGET_SPARC64
713 #define ELF_START_MMAP 0x80000000
714 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
715 | HWCAP_SPARC_MULDIV | HWCAP_SPARC_V9)
717 #define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS )
719 #define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC )
722 #define ELF_CLASS ELFCLASS64
723 #define ELF_ARCH EM_SPARCV9
725 #define ELF_START_MMAP 0x80000000
726 #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
727 | HWCAP_SPARC_MULDIV)
728 #define ELF_CLASS ELFCLASS32
729 #define ELF_ARCH EM_SPARC
730 #endif /* TARGET_SPARC64 */
732 static inline void init_thread(struct target_pt_regs
*regs
,
733 struct image_info
*infop
)
735 /* Note that target_cpu_copy_regs does not read psr/tstate. */
736 regs
->pc
= infop
->entry
;
737 regs
->npc
= regs
->pc
+ 4;
739 regs
->u_regs
[14] = (infop
->start_stack
- 16 * sizeof(abi_ulong
)
740 - TARGET_STACK_BIAS
);
742 #endif /* TARGET_SPARC */
746 #define ELF_MACHINE PPC_ELF_MACHINE
747 #define ELF_START_MMAP 0x80000000
749 #if defined(TARGET_PPC64)
751 #define elf_check_arch(x) ( (x) == EM_PPC64 )
753 #define ELF_CLASS ELFCLASS64
757 #define ELF_CLASS ELFCLASS32
761 #define ELF_ARCH EM_PPC
763 /* Feature masks for the Aux Vector Hardware Capabilities (AT_HWCAP).
764 See arch/powerpc/include/asm/cputable.h. */
766 QEMU_PPC_FEATURE_32
= 0x80000000,
767 QEMU_PPC_FEATURE_64
= 0x40000000,
768 QEMU_PPC_FEATURE_601_INSTR
= 0x20000000,
769 QEMU_PPC_FEATURE_HAS_ALTIVEC
= 0x10000000,
770 QEMU_PPC_FEATURE_HAS_FPU
= 0x08000000,
771 QEMU_PPC_FEATURE_HAS_MMU
= 0x04000000,
772 QEMU_PPC_FEATURE_HAS_4xxMAC
= 0x02000000,
773 QEMU_PPC_FEATURE_UNIFIED_CACHE
= 0x01000000,
774 QEMU_PPC_FEATURE_HAS_SPE
= 0x00800000,
775 QEMU_PPC_FEATURE_HAS_EFP_SINGLE
= 0x00400000,
776 QEMU_PPC_FEATURE_HAS_EFP_DOUBLE
= 0x00200000,
777 QEMU_PPC_FEATURE_NO_TB
= 0x00100000,
778 QEMU_PPC_FEATURE_POWER4
= 0x00080000,
779 QEMU_PPC_FEATURE_POWER5
= 0x00040000,
780 QEMU_PPC_FEATURE_POWER5_PLUS
= 0x00020000,
781 QEMU_PPC_FEATURE_CELL
= 0x00010000,
782 QEMU_PPC_FEATURE_BOOKE
= 0x00008000,
783 QEMU_PPC_FEATURE_SMT
= 0x00004000,
784 QEMU_PPC_FEATURE_ICACHE_SNOOP
= 0x00002000,
785 QEMU_PPC_FEATURE_ARCH_2_05
= 0x00001000,
786 QEMU_PPC_FEATURE_PA6T
= 0x00000800,
787 QEMU_PPC_FEATURE_HAS_DFP
= 0x00000400,
788 QEMU_PPC_FEATURE_POWER6_EXT
= 0x00000200,
789 QEMU_PPC_FEATURE_ARCH_2_06
= 0x00000100,
790 QEMU_PPC_FEATURE_HAS_VSX
= 0x00000080,
791 QEMU_PPC_FEATURE_PSERIES_PERFMON_COMPAT
= 0x00000040,
793 QEMU_PPC_FEATURE_TRUE_LE
= 0x00000002,
794 QEMU_PPC_FEATURE_PPC_LE
= 0x00000001,
796 /* Feature definitions in AT_HWCAP2. */
797 QEMU_PPC_FEATURE2_ARCH_2_07
= 0x80000000, /* ISA 2.07 */
798 QEMU_PPC_FEATURE2_HAS_HTM
= 0x40000000, /* Hardware Transactional Memory */
799 QEMU_PPC_FEATURE2_HAS_DSCR
= 0x20000000, /* Data Stream Control Register */
800 QEMU_PPC_FEATURE2_HAS_EBB
= 0x10000000, /* Event Base Branching */
801 QEMU_PPC_FEATURE2_HAS_ISEL
= 0x08000000, /* Integer Select */
802 QEMU_PPC_FEATURE2_HAS_TAR
= 0x04000000, /* Target Address Register */
803 QEMU_PPC_FEATURE2_VEC_CRYPTO
= 0x02000000,
804 QEMU_PPC_FEATURE2_HTM_NOSC
= 0x01000000,
805 QEMU_PPC_FEATURE2_ARCH_3_00
= 0x00800000, /* ISA 3.00 */
806 QEMU_PPC_FEATURE2_HAS_IEEE128
= 0x00400000, /* VSX IEEE Bin Float 128-bit */
807 QEMU_PPC_FEATURE2_DARN
= 0x00200000, /* darn random number insn */
808 QEMU_PPC_FEATURE2_SCV
= 0x00100000, /* scv syscall */
809 QEMU_PPC_FEATURE2_HTM_NO_SUSPEND
= 0x00080000, /* TM w/o suspended state */
810 QEMU_PPC_FEATURE2_ARCH_3_1
= 0x00040000, /* ISA 3.1 */
811 QEMU_PPC_FEATURE2_MMA
= 0x00020000, /* Matrix-Multiply Assist */
814 #define ELF_HWCAP get_elf_hwcap()
816 static uint32_t get_elf_hwcap(void)
818 PowerPCCPU
*cpu
= POWERPC_CPU(thread_cpu
);
819 uint32_t features
= 0;
821 /* We don't have to be terribly complete here; the high points are
822 Altivec/FP/SPE support. Anything else is just a bonus. */
823 #define GET_FEATURE(flag, feature) \
824 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
825 #define GET_FEATURE2(flags, feature) \
827 if ((cpu->env.insns_flags2 & flags) == flags) { \
828 features |= feature; \
831 GET_FEATURE(PPC_64B
, QEMU_PPC_FEATURE_64
);
832 GET_FEATURE(PPC_FLOAT
, QEMU_PPC_FEATURE_HAS_FPU
);
833 GET_FEATURE(PPC_ALTIVEC
, QEMU_PPC_FEATURE_HAS_ALTIVEC
);
834 GET_FEATURE(PPC_SPE
, QEMU_PPC_FEATURE_HAS_SPE
);
835 GET_FEATURE(PPC_SPE_SINGLE
, QEMU_PPC_FEATURE_HAS_EFP_SINGLE
);
836 GET_FEATURE(PPC_SPE_DOUBLE
, QEMU_PPC_FEATURE_HAS_EFP_DOUBLE
);
837 GET_FEATURE(PPC_BOOKE
, QEMU_PPC_FEATURE_BOOKE
);
838 GET_FEATURE(PPC_405_MAC
, QEMU_PPC_FEATURE_HAS_4xxMAC
);
839 GET_FEATURE2(PPC2_DFP
, QEMU_PPC_FEATURE_HAS_DFP
);
840 GET_FEATURE2(PPC2_VSX
, QEMU_PPC_FEATURE_HAS_VSX
);
841 GET_FEATURE2((PPC2_PERM_ISA206
| PPC2_DIVE_ISA206
| PPC2_ATOMIC_ISA206
|
842 PPC2_FP_CVT_ISA206
| PPC2_FP_TST_ISA206
),
843 QEMU_PPC_FEATURE_ARCH_2_06
);
850 #define ELF_HWCAP2 get_elf_hwcap2()
852 static uint32_t get_elf_hwcap2(void)
854 PowerPCCPU
*cpu
= POWERPC_CPU(thread_cpu
);
855 uint32_t features
= 0;
857 #define GET_FEATURE(flag, feature) \
858 do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
859 #define GET_FEATURE2(flag, feature) \
860 do { if (cpu->env.insns_flags2 & flag) { features |= feature; } } while (0)
862 GET_FEATURE(PPC_ISEL
, QEMU_PPC_FEATURE2_HAS_ISEL
);
863 GET_FEATURE2(PPC2_BCTAR_ISA207
, QEMU_PPC_FEATURE2_HAS_TAR
);
864 GET_FEATURE2((PPC2_BCTAR_ISA207
| PPC2_LSQ_ISA207
| PPC2_ALTIVEC_207
|
865 PPC2_ISA207S
), QEMU_PPC_FEATURE2_ARCH_2_07
|
866 QEMU_PPC_FEATURE2_VEC_CRYPTO
);
867 GET_FEATURE2(PPC2_ISA300
, QEMU_PPC_FEATURE2_ARCH_3_00
|
868 QEMU_PPC_FEATURE2_DARN
| QEMU_PPC_FEATURE2_HAS_IEEE128
);
869 GET_FEATURE2(PPC2_ISA310
, QEMU_PPC_FEATURE2_ARCH_3_1
|
870 QEMU_PPC_FEATURE2_MMA
);
879 * The requirements here are:
880 * - keep the final alignment of sp (sp & 0xf)
881 * - make sure the 32-bit value at the first 16 byte aligned position of
882 * AUXV is greater than 16 for glibc compatibility.
883 * AT_IGNOREPPC is used for that.
884 * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC,
885 * even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
887 #define DLINFO_ARCH_ITEMS 5
888 #define ARCH_DLINFO \
890 PowerPCCPU *cpu = POWERPC_CPU(thread_cpu); \
892 * Handle glibc compatibility: these magic entries must \
893 * be at the lowest addresses in the final auxv. \
895 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
896 NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
897 NEW_AUX_ENT(AT_DCACHEBSIZE, cpu->env.dcache_line_size); \
898 NEW_AUX_ENT(AT_ICACHEBSIZE, cpu->env.icache_line_size); \
899 NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \
902 static inline void init_thread(struct target_pt_regs
*_regs
, struct image_info
*infop
)
904 _regs
->gpr
[1] = infop
->start_stack
;
905 #if defined(TARGET_PPC64)
906 if (get_ppc64_abi(infop
) < 2) {
908 get_user_u64(val
, infop
->entry
+ 8);
909 _regs
->gpr
[2] = val
+ infop
->load_bias
;
910 get_user_u64(val
, infop
->entry
);
911 infop
->entry
= val
+ infop
->load_bias
;
913 _regs
->gpr
[12] = infop
->entry
; /* r12 set to global entry address */
916 _regs
->nip
= infop
->entry
;
919 /* See linux kernel: arch/powerpc/include/asm/elf.h. */
921 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
923 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUPPCState
*env
)
926 target_ulong ccr
= 0;
928 for (i
= 0; i
< ARRAY_SIZE(env
->gpr
); i
++) {
929 (*regs
)[i
] = tswapreg(env
->gpr
[i
]);
932 (*regs
)[32] = tswapreg(env
->nip
);
933 (*regs
)[33] = tswapreg(env
->msr
);
934 (*regs
)[35] = tswapreg(env
->ctr
);
935 (*regs
)[36] = tswapreg(env
->lr
);
936 (*regs
)[37] = tswapreg(cpu_read_xer(env
));
938 for (i
= 0; i
< ARRAY_SIZE(env
->crf
); i
++) {
939 ccr
|= env
->crf
[i
] << (32 - ((i
+ 1) * 4));
941 (*regs
)[38] = tswapreg(ccr
);
944 #define USE_ELF_CORE_DUMP
945 #define ELF_EXEC_PAGESIZE 4096
949 #ifdef TARGET_LOONGARCH64
951 #define ELF_START_MMAP 0x80000000
953 #define ELF_CLASS ELFCLASS64
954 #define ELF_ARCH EM_LOONGARCH
956 #define elf_check_arch(x) ((x) == EM_LOONGARCH)
958 static inline void init_thread(struct target_pt_regs
*regs
,
959 struct image_info
*infop
)
961 /*Set crmd PG,DA = 1,0 */
962 regs
->csr
.crmd
= 2 << 3;
963 regs
->csr
.era
= infop
->entry
;
964 regs
->regs
[3] = infop
->start_stack
;
967 /* See linux kernel: arch/loongarch/include/asm/elf.h */
969 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
973 TARGET_EF_CSR_ERA
= TARGET_EF_R0
+ 33,
974 TARGET_EF_CSR_BADV
= TARGET_EF_R0
+ 34,
977 static void elf_core_copy_regs(target_elf_gregset_t
*regs
,
978 const CPULoongArchState
*env
)
982 (*regs
)[TARGET_EF_R0
] = 0;
984 for (i
= 1; i
< ARRAY_SIZE(env
->gpr
); i
++) {
985 (*regs
)[TARGET_EF_R0
+ i
] = tswapreg(env
->gpr
[i
]);
988 (*regs
)[TARGET_EF_CSR_ERA
] = tswapreg(env
->pc
);
989 (*regs
)[TARGET_EF_CSR_BADV
] = tswapreg(env
->CSR_BADV
);
992 #define USE_ELF_CORE_DUMP
993 #define ELF_EXEC_PAGESIZE 4096
995 #define ELF_HWCAP get_elf_hwcap()
997 /* See arch/loongarch/include/uapi/asm/hwcap.h */
999 HWCAP_LOONGARCH_CPUCFG
= (1 << 0),
1000 HWCAP_LOONGARCH_LAM
= (1 << 1),
1001 HWCAP_LOONGARCH_UAL
= (1 << 2),
1002 HWCAP_LOONGARCH_FPU
= (1 << 3),
1003 HWCAP_LOONGARCH_LSX
= (1 << 4),
1004 HWCAP_LOONGARCH_LASX
= (1 << 5),
1005 HWCAP_LOONGARCH_CRC32
= (1 << 6),
1006 HWCAP_LOONGARCH_COMPLEX
= (1 << 7),
1007 HWCAP_LOONGARCH_CRYPTO
= (1 << 8),
1008 HWCAP_LOONGARCH_LVZ
= (1 << 9),
1009 HWCAP_LOONGARCH_LBT_X86
= (1 << 10),
1010 HWCAP_LOONGARCH_LBT_ARM
= (1 << 11),
1011 HWCAP_LOONGARCH_LBT_MIPS
= (1 << 12),
1014 static uint32_t get_elf_hwcap(void)
1016 LoongArchCPU
*cpu
= LOONGARCH_CPU(thread_cpu
);
1017 uint32_t hwcaps
= 0;
1019 hwcaps
|= HWCAP_LOONGARCH_CRC32
;
1021 if (FIELD_EX32(cpu
->env
.cpucfg
[1], CPUCFG1
, UAL
)) {
1022 hwcaps
|= HWCAP_LOONGARCH_UAL
;
1025 if (FIELD_EX32(cpu
->env
.cpucfg
[2], CPUCFG2
, FP
)) {
1026 hwcaps
|= HWCAP_LOONGARCH_FPU
;
1029 if (FIELD_EX32(cpu
->env
.cpucfg
[2], CPUCFG2
, LAM
)) {
1030 hwcaps
|= HWCAP_LOONGARCH_LAM
;
1036 #define ELF_PLATFORM "loongarch"
1038 #endif /* TARGET_LOONGARCH64 */
1042 #define ELF_START_MMAP 0x80000000
1044 #ifdef TARGET_MIPS64
1045 #define ELF_CLASS ELFCLASS64
1047 #define ELF_CLASS ELFCLASS32
1049 #define ELF_ARCH EM_MIPS
1051 #ifdef TARGET_ABI_MIPSN32
1052 #define elf_check_abi(x) ((x) & EF_MIPS_ABI2)
1054 #define elf_check_abi(x) (!((x) & EF_MIPS_ABI2))
1057 static inline void init_thread(struct target_pt_regs
*regs
,
1058 struct image_info
*infop
)
1060 regs
->cp0_status
= 2 << CP0St_KSU
;
1061 regs
->cp0_epc
= infop
->entry
;
1062 regs
->regs
[29] = infop
->start_stack
;
1065 /* See linux kernel: arch/mips/include/asm/elf.h. */
1067 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1069 /* See linux kernel: arch/mips/include/asm/reg.h. */
1071 #ifdef TARGET_MIPS64
1076 TARGET_EF_R26
= TARGET_EF_R0
+ 26,
1077 TARGET_EF_R27
= TARGET_EF_R0
+ 27,
1078 TARGET_EF_LO
= TARGET_EF_R0
+ 32,
1079 TARGET_EF_HI
= TARGET_EF_R0
+ 33,
1080 TARGET_EF_CP0_EPC
= TARGET_EF_R0
+ 34,
1081 TARGET_EF_CP0_BADVADDR
= TARGET_EF_R0
+ 35,
1082 TARGET_EF_CP0_STATUS
= TARGET_EF_R0
+ 36,
1083 TARGET_EF_CP0_CAUSE
= TARGET_EF_R0
+ 37
1086 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1087 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUMIPSState
*env
)
1091 for (i
= 0; i
< TARGET_EF_R0
; i
++) {
1094 (*regs
)[TARGET_EF_R0
] = 0;
1096 for (i
= 1; i
< ARRAY_SIZE(env
->active_tc
.gpr
); i
++) {
1097 (*regs
)[TARGET_EF_R0
+ i
] = tswapreg(env
->active_tc
.gpr
[i
]);
1100 (*regs
)[TARGET_EF_R26
] = 0;
1101 (*regs
)[TARGET_EF_R27
] = 0;
1102 (*regs
)[TARGET_EF_LO
] = tswapreg(env
->active_tc
.LO
[0]);
1103 (*regs
)[TARGET_EF_HI
] = tswapreg(env
->active_tc
.HI
[0]);
1104 (*regs
)[TARGET_EF_CP0_EPC
] = tswapreg(env
->active_tc
.PC
);
1105 (*regs
)[TARGET_EF_CP0_BADVADDR
] = tswapreg(env
->CP0_BadVAddr
);
1106 (*regs
)[TARGET_EF_CP0_STATUS
] = tswapreg(env
->CP0_Status
);
1107 (*regs
)[TARGET_EF_CP0_CAUSE
] = tswapreg(env
->CP0_Cause
);
1110 #define USE_ELF_CORE_DUMP
1111 #define ELF_EXEC_PAGESIZE 4096
1113 /* See arch/mips/include/uapi/asm/hwcap.h. */
1115 HWCAP_MIPS_R6
= (1 << 0),
1116 HWCAP_MIPS_MSA
= (1 << 1),
1117 HWCAP_MIPS_CRC32
= (1 << 2),
1118 HWCAP_MIPS_MIPS16
= (1 << 3),
1119 HWCAP_MIPS_MDMX
= (1 << 4),
1120 HWCAP_MIPS_MIPS3D
= (1 << 5),
1121 HWCAP_MIPS_SMARTMIPS
= (1 << 6),
1122 HWCAP_MIPS_DSP
= (1 << 7),
1123 HWCAP_MIPS_DSP2
= (1 << 8),
1124 HWCAP_MIPS_DSP3
= (1 << 9),
1125 HWCAP_MIPS_MIPS16E2
= (1 << 10),
1126 HWCAP_LOONGSON_MMI
= (1 << 11),
1127 HWCAP_LOONGSON_EXT
= (1 << 12),
1128 HWCAP_LOONGSON_EXT2
= (1 << 13),
1129 HWCAP_LOONGSON_CPUCFG
= (1 << 14),
1132 #define ELF_HWCAP get_elf_hwcap()
1134 #define GET_FEATURE_INSN(_flag, _hwcap) \
1135 do { if (cpu->env.insn_flags & (_flag)) { hwcaps |= _hwcap; } } while (0)
1137 #define GET_FEATURE_REG_SET(_reg, _mask, _hwcap) \
1138 do { if (cpu->env._reg & (_mask)) { hwcaps |= _hwcap; } } while (0)
1140 #define GET_FEATURE_REG_EQU(_reg, _start, _length, _val, _hwcap) \
1142 if (extract32(cpu->env._reg, (_start), (_length)) == (_val)) { \
1147 static uint32_t get_elf_hwcap(void)
1149 MIPSCPU
*cpu
= MIPS_CPU(thread_cpu
);
1150 uint32_t hwcaps
= 0;
1152 GET_FEATURE_REG_EQU(CP0_Config0
, CP0C0_AR
, CP0C0_AR_LENGTH
,
1154 GET_FEATURE_REG_SET(CP0_Config3
, 1 << CP0C3_MSAP
, HWCAP_MIPS_MSA
);
1155 GET_FEATURE_INSN(ASE_LMMI
, HWCAP_LOONGSON_MMI
);
1156 GET_FEATURE_INSN(ASE_LEXT
, HWCAP_LOONGSON_EXT
);
1161 #undef GET_FEATURE_REG_EQU
1162 #undef GET_FEATURE_REG_SET
1163 #undef GET_FEATURE_INSN
1165 #endif /* TARGET_MIPS */
1167 #ifdef TARGET_MICROBLAZE
1169 #define ELF_START_MMAP 0x80000000
1171 #define elf_check_arch(x) ( (x) == EM_MICROBLAZE || (x) == EM_MICROBLAZE_OLD)
1173 #define ELF_CLASS ELFCLASS32
1174 #define ELF_ARCH EM_MICROBLAZE
1176 static inline void init_thread(struct target_pt_regs
*regs
,
1177 struct image_info
*infop
)
1179 regs
->pc
= infop
->entry
;
1180 regs
->r1
= infop
->start_stack
;
1184 #define ELF_EXEC_PAGESIZE 4096
1186 #define USE_ELF_CORE_DUMP
1188 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1190 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1191 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUMBState
*env
)
1195 for (i
= 0; i
< 32; i
++) {
1196 (*regs
)[pos
++] = tswapreg(env
->regs
[i
]);
1199 (*regs
)[pos
++] = tswapreg(env
->pc
);
1200 (*regs
)[pos
++] = tswapreg(mb_cpu_read_msr(env
));
1202 (*regs
)[pos
++] = tswapreg(env
->ear
);
1204 (*regs
)[pos
++] = tswapreg(env
->esr
);
1207 #endif /* TARGET_MICROBLAZE */
1211 #define ELF_START_MMAP 0x80000000
1213 #define elf_check_arch(x) ((x) == EM_ALTERA_NIOS2)
1215 #define ELF_CLASS ELFCLASS32
1216 #define ELF_ARCH EM_ALTERA_NIOS2
1218 static void init_thread(struct target_pt_regs
*regs
, struct image_info
*infop
)
1220 regs
->ea
= infop
->entry
;
1221 regs
->sp
= infop
->start_stack
;
1224 #define LO_COMMPAGE TARGET_PAGE_SIZE
1226 static bool init_guest_commpage(void)
1228 static const uint8_t kuser_page
[4 + 2 * 64] = {
1229 /* __kuser_helper_version */
1230 [0x00] = 0x02, 0x00, 0x00, 0x00,
1232 /* __kuser_cmpxchg */
1233 [0x04] = 0x3a, 0x6c, 0x3b, 0x00, /* trap 16 */
1234 0x3a, 0x28, 0x00, 0xf8, /* ret */
1236 /* __kuser_sigtramp */
1237 [0x44] = 0xc4, 0x22, 0x80, 0x00, /* movi r2, __NR_rt_sigreturn */
1238 0x3a, 0x68, 0x3b, 0x00, /* trap 0 */
1241 void *want
= g2h_untagged(LO_COMMPAGE
& -qemu_host_page_size
);
1242 void *addr
= mmap(want
, qemu_host_page_size
, PROT_READ
| PROT_WRITE
,
1243 MAP_ANONYMOUS
| MAP_PRIVATE
| MAP_FIXED
, -1, 0);
1245 if (addr
== MAP_FAILED
) {
1246 perror("Allocating guest commpage");
1253 memcpy(addr
, kuser_page
, sizeof(kuser_page
));
1255 if (mprotect(addr
, qemu_host_page_size
, PROT_READ
)) {
1256 perror("Protecting guest commpage");
1260 page_set_flags(LO_COMMPAGE
, LO_COMMPAGE
+ TARGET_PAGE_SIZE
,
1261 PAGE_READ
| PAGE_EXEC
| PAGE_VALID
);
1265 #define ELF_EXEC_PAGESIZE 4096
1267 #define USE_ELF_CORE_DUMP
1269 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1271 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1272 static void elf_core_copy_regs(target_elf_gregset_t
*regs
,
1273 const CPUNios2State
*env
)
1278 for (i
= 1; i
< 8; i
++) /* r0-r7 */
1279 (*regs
)[i
] = tswapreg(env
->regs
[i
+ 7]);
1281 for (i
= 8; i
< 16; i
++) /* r8-r15 */
1282 (*regs
)[i
] = tswapreg(env
->regs
[i
- 8]);
1284 for (i
= 16; i
< 24; i
++) /* r16-r23 */
1285 (*regs
)[i
] = tswapreg(env
->regs
[i
+ 7]);
1286 (*regs
)[24] = -1; /* R_ET */
1287 (*regs
)[25] = -1; /* R_BT */
1288 (*regs
)[26] = tswapreg(env
->regs
[R_GP
]);
1289 (*regs
)[27] = tswapreg(env
->regs
[R_SP
]);
1290 (*regs
)[28] = tswapreg(env
->regs
[R_FP
]);
1291 (*regs
)[29] = tswapreg(env
->regs
[R_EA
]);
1292 (*regs
)[30] = -1; /* R_SSTATUS */
1293 (*regs
)[31] = tswapreg(env
->regs
[R_RA
]);
1295 (*regs
)[32] = tswapreg(env
->pc
);
1297 (*regs
)[33] = -1; /* R_STATUS */
1298 (*regs
)[34] = tswapreg(env
->regs
[CR_ESTATUS
]);
1300 for (i
= 35; i
< 49; i
++) /* ... */
1304 #endif /* TARGET_NIOS2 */
1306 #ifdef TARGET_OPENRISC
1308 #define ELF_START_MMAP 0x08000000
1310 #define ELF_ARCH EM_OPENRISC
1311 #define ELF_CLASS ELFCLASS32
1312 #define ELF_DATA ELFDATA2MSB
1314 static inline void init_thread(struct target_pt_regs
*regs
,
1315 struct image_info
*infop
)
1317 regs
->pc
= infop
->entry
;
1318 regs
->gpr
[1] = infop
->start_stack
;
1321 #define USE_ELF_CORE_DUMP
1322 #define ELF_EXEC_PAGESIZE 8192
1324 /* See linux kernel arch/openrisc/include/asm/elf.h. */
1325 #define ELF_NREG 34 /* gprs and pc, sr */
1326 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1328 static void elf_core_copy_regs(target_elf_gregset_t
*regs
,
1329 const CPUOpenRISCState
*env
)
1333 for (i
= 0; i
< 32; i
++) {
1334 (*regs
)[i
] = tswapreg(cpu_get_gpr(env
, i
));
1336 (*regs
)[32] = tswapreg(env
->pc
);
1337 (*regs
)[33] = tswapreg(cpu_get_sr(env
));
1340 #define ELF_PLATFORM NULL
1342 #endif /* TARGET_OPENRISC */
1346 #define ELF_START_MMAP 0x80000000
1348 #define ELF_CLASS ELFCLASS32
1349 #define ELF_ARCH EM_SH
1351 static inline void init_thread(struct target_pt_regs
*regs
,
1352 struct image_info
*infop
)
1354 /* Check other registers XXXXX */
1355 regs
->pc
= infop
->entry
;
1356 regs
->regs
[15] = infop
->start_stack
;
1359 /* See linux kernel: arch/sh/include/asm/elf.h. */
1361 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1363 /* See linux kernel: arch/sh/include/asm/ptrace.h. */
1368 TARGET_REG_GBR
= 19,
1369 TARGET_REG_MACH
= 20,
1370 TARGET_REG_MACL
= 21,
1371 TARGET_REG_SYSCALL
= 22
1374 static inline void elf_core_copy_regs(target_elf_gregset_t
*regs
,
1375 const CPUSH4State
*env
)
1379 for (i
= 0; i
< 16; i
++) {
1380 (*regs
)[i
] = tswapreg(env
->gregs
[i
]);
1383 (*regs
)[TARGET_REG_PC
] = tswapreg(env
->pc
);
1384 (*regs
)[TARGET_REG_PR
] = tswapreg(env
->pr
);
1385 (*regs
)[TARGET_REG_SR
] = tswapreg(env
->sr
);
1386 (*regs
)[TARGET_REG_GBR
] = tswapreg(env
->gbr
);
1387 (*regs
)[TARGET_REG_MACH
] = tswapreg(env
->mach
);
1388 (*regs
)[TARGET_REG_MACL
] = tswapreg(env
->macl
);
1389 (*regs
)[TARGET_REG_SYSCALL
] = 0; /* FIXME */
1392 #define USE_ELF_CORE_DUMP
1393 #define ELF_EXEC_PAGESIZE 4096
1396 SH_CPU_HAS_FPU
= 0x0001, /* Hardware FPU support */
1397 SH_CPU_HAS_P2_FLUSH_BUG
= 0x0002, /* Need to flush the cache in P2 area */
1398 SH_CPU_HAS_MMU_PAGE_ASSOC
= 0x0004, /* SH3: TLB way selection bit support */
1399 SH_CPU_HAS_DSP
= 0x0008, /* SH-DSP: DSP support */
1400 SH_CPU_HAS_PERF_COUNTER
= 0x0010, /* Hardware performance counters */
1401 SH_CPU_HAS_PTEA
= 0x0020, /* PTEA register */
1402 SH_CPU_HAS_LLSC
= 0x0040, /* movli.l/movco.l */
1403 SH_CPU_HAS_L2_CACHE
= 0x0080, /* Secondary cache / URAM */
1404 SH_CPU_HAS_OP32
= 0x0100, /* 32-bit instruction support */
1405 SH_CPU_HAS_PTEAEX
= 0x0200, /* PTE ASID Extension support */
1408 #define ELF_HWCAP get_elf_hwcap()
1410 static uint32_t get_elf_hwcap(void)
1412 SuperHCPU
*cpu
= SUPERH_CPU(thread_cpu
);
1415 hwcap
|= SH_CPU_HAS_FPU
;
1417 if (cpu
->env
.features
& SH_FEATURE_SH4A
) {
1418 hwcap
|= SH_CPU_HAS_LLSC
;
1428 #define ELF_START_MMAP 0x80000000
1430 #define ELF_CLASS ELFCLASS32
1431 #define ELF_ARCH EM_CRIS
1433 static inline void init_thread(struct target_pt_regs
*regs
,
1434 struct image_info
*infop
)
1436 regs
->erp
= infop
->entry
;
1439 #define ELF_EXEC_PAGESIZE 8192
1445 #define ELF_START_MMAP 0x80000000
1447 #define ELF_CLASS ELFCLASS32
1448 #define ELF_ARCH EM_68K
1450 /* ??? Does this need to do anything?
1451 #define ELF_PLAT_INIT(_r) */
1453 static inline void init_thread(struct target_pt_regs
*regs
,
1454 struct image_info
*infop
)
1456 regs
->usp
= infop
->start_stack
;
1458 regs
->pc
= infop
->entry
;
1461 /* See linux kernel: arch/m68k/include/asm/elf.h. */
1463 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1465 static void elf_core_copy_regs(target_elf_gregset_t
*regs
, const CPUM68KState
*env
)
1467 (*regs
)[0] = tswapreg(env
->dregs
[1]);
1468 (*regs
)[1] = tswapreg(env
->dregs
[2]);
1469 (*regs
)[2] = tswapreg(env
->dregs
[3]);
1470 (*regs
)[3] = tswapreg(env
->dregs
[4]);
1471 (*regs
)[4] = tswapreg(env
->dregs
[5]);
1472 (*regs
)[5] = tswapreg(env
->dregs
[6]);
1473 (*regs
)[6] = tswapreg(env
->dregs
[7]);
1474 (*regs
)[7] = tswapreg(env
->aregs
[0]);
1475 (*regs
)[8] = tswapreg(env
->aregs
[1]);
1476 (*regs
)[9] = tswapreg(env
->aregs
[2]);
1477 (*regs
)[10] = tswapreg(env
->aregs
[3]);
1478 (*regs
)[11] = tswapreg(env
->aregs
[4]);
1479 (*regs
)[12] = tswapreg(env
->aregs
[5]);
1480 (*regs
)[13] = tswapreg(env
->aregs
[6]);
1481 (*regs
)[14] = tswapreg(env
->dregs
[0]);
1482 (*regs
)[15] = tswapreg(env
->aregs
[7]);
1483 (*regs
)[16] = tswapreg(env
->dregs
[0]); /* FIXME: orig_d0 */
1484 (*regs
)[17] = tswapreg(env
->sr
);
1485 (*regs
)[18] = tswapreg(env
->pc
);
1486 (*regs
)[19] = 0; /* FIXME: regs->format | regs->vector */
1489 #define USE_ELF_CORE_DUMP
1490 #define ELF_EXEC_PAGESIZE 8192
1496 #define ELF_START_MMAP (0x30000000000ULL)
1498 #define ELF_CLASS ELFCLASS64
1499 #define ELF_ARCH EM_ALPHA
1501 static inline void init_thread(struct target_pt_regs
*regs
,
1502 struct image_info
*infop
)
1504 regs
->pc
= infop
->entry
;
1506 regs
->usp
= infop
->start_stack
;
1509 #define ELF_EXEC_PAGESIZE 8192
1511 #endif /* TARGET_ALPHA */
1515 #define ELF_START_MMAP (0x20000000000ULL)
1517 #define ELF_CLASS ELFCLASS64
1518 #define ELF_DATA ELFDATA2MSB
1519 #define ELF_ARCH EM_S390
1523 #define ELF_HWCAP get_elf_hwcap()
1525 #define GET_FEATURE(_feat, _hwcap) \
1526 do { if (s390_has_feat(_feat)) { hwcap |= _hwcap; } } while (0)
1528 static uint32_t get_elf_hwcap(void)
1531 * Let's assume we always have esan3 and zarch.
1532 * 31-bit processes can use 64-bit registers (high gprs).
1534 uint32_t hwcap
= HWCAP_S390_ESAN3
| HWCAP_S390_ZARCH
| HWCAP_S390_HIGH_GPRS
;
1536 GET_FEATURE(S390_FEAT_STFLE
, HWCAP_S390_STFLE
);
1537 GET_FEATURE(S390_FEAT_MSA
, HWCAP_S390_MSA
);
1538 GET_FEATURE(S390_FEAT_LONG_DISPLACEMENT
, HWCAP_S390_LDISP
);
1539 GET_FEATURE(S390_FEAT_EXTENDED_IMMEDIATE
, HWCAP_S390_EIMM
);
1540 if (s390_has_feat(S390_FEAT_EXTENDED_TRANSLATION_3
) &&
1541 s390_has_feat(S390_FEAT_ETF3_ENH
)) {
1542 hwcap
|= HWCAP_S390_ETF3EH
;
1544 GET_FEATURE(S390_FEAT_VECTOR
, HWCAP_S390_VXRS
);
1545 GET_FEATURE(S390_FEAT_VECTOR_ENH
, HWCAP_S390_VXRS_EXT
);
1550 static inline void init_thread(struct target_pt_regs
*regs
, struct image_info
*infop
)
1552 regs
->psw
.addr
= infop
->entry
;
1553 regs
->psw
.mask
= PSW_MASK_64
| PSW_MASK_32
;
1554 regs
->gprs
[15] = infop
->start_stack
;
1557 /* See linux kernel: arch/s390/include/uapi/asm/ptrace.h (s390_regs). */
1559 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1562 TARGET_REG_PSWM
= 0,
1563 TARGET_REG_PSWA
= 1,
1564 TARGET_REG_GPRS
= 2,
1565 TARGET_REG_ARS
= 18,
1566 TARGET_REG_ORIG_R2
= 26,
1569 static void elf_core_copy_regs(target_elf_gregset_t
*regs
,
1570 const CPUS390XState
*env
)
1575 (*regs
)[TARGET_REG_PSWM
] = tswapreg(env
->psw
.mask
);
1576 (*regs
)[TARGET_REG_PSWA
] = tswapreg(env
->psw
.addr
);
1577 for (i
= 0; i
< 16; i
++) {
1578 (*regs
)[TARGET_REG_GPRS
+ i
] = tswapreg(env
->regs
[i
]);
1580 aregs
= (uint32_t *)&((*regs
)[TARGET_REG_ARS
]);
1581 for (i
= 0; i
< 16; i
++) {
1582 aregs
[i
] = tswap32(env
->aregs
[i
]);
1584 (*regs
)[TARGET_REG_ORIG_R2
] = 0;
1587 #define USE_ELF_CORE_DUMP
1588 #define ELF_EXEC_PAGESIZE 4096
1590 #endif /* TARGET_S390X */
1594 #define ELF_START_MMAP 0x80000000
1595 #define ELF_ARCH EM_RISCV
1597 #ifdef TARGET_RISCV32
1598 #define ELF_CLASS ELFCLASS32
1600 #define ELF_CLASS ELFCLASS64
1603 #define ELF_HWCAP get_elf_hwcap()
1605 static uint32_t get_elf_hwcap(void)
1607 #define MISA_BIT(EXT) (1 << (EXT - 'A'))
1608 RISCVCPU
*cpu
= RISCV_CPU(thread_cpu
);
1609 uint32_t mask
= MISA_BIT('I') | MISA_BIT('M') | MISA_BIT('A')
1610 | MISA_BIT('F') | MISA_BIT('D') | MISA_BIT('C');
1612 return cpu
->env
.misa_ext
& mask
;
1616 static inline void init_thread(struct target_pt_regs
*regs
,
1617 struct image_info
*infop
)
1619 regs
->sepc
= infop
->entry
;
1620 regs
->sp
= infop
->start_stack
;
1623 #define ELF_EXEC_PAGESIZE 4096
1625 #endif /* TARGET_RISCV */
1629 #define ELF_START_MMAP 0x80000000
1630 #define ELF_CLASS ELFCLASS32
1631 #define ELF_ARCH EM_PARISC
1632 #define ELF_PLATFORM "PARISC"
1633 #define STACK_GROWS_DOWN 0
1634 #define STACK_ALIGNMENT 64
1636 static inline void init_thread(struct target_pt_regs
*regs
,
1637 struct image_info
*infop
)
1639 regs
->iaoq
[0] = infop
->entry
;
1640 regs
->iaoq
[1] = infop
->entry
+ 4;
1642 regs
->gr
[24] = infop
->argv
;
1643 regs
->gr
[25] = infop
->argc
;
1644 /* The top-of-stack contains a linkage buffer. */
1645 regs
->gr
[30] = infop
->start_stack
+ 64;
1646 regs
->gr
[31] = infop
->entry
;
1649 #define LO_COMMPAGE 0
1651 static bool init_guest_commpage(void)
1653 void *want
= g2h_untagged(LO_COMMPAGE
);
1654 void *addr
= mmap(want
, qemu_host_page_size
, PROT_NONE
,
1655 MAP_ANONYMOUS
| MAP_PRIVATE
| MAP_FIXED
, -1, 0);
1657 if (addr
== MAP_FAILED
) {
1658 perror("Allocating guest commpage");
1666 * On Linux, page zero is normally marked execute only + gateway.
1667 * Normal read or write is supposed to fail (thus PROT_NONE above),
1668 * but specific offsets have kernel code mapped to raise permissions
1669 * and implement syscalls. Here, simply mark the page executable.
1670 * Special case the entry points during translation (see do_page_zero).
1672 page_set_flags(LO_COMMPAGE
, LO_COMMPAGE
+ TARGET_PAGE_SIZE
,
1673 PAGE_EXEC
| PAGE_VALID
);
1677 #endif /* TARGET_HPPA */
1679 #ifdef TARGET_XTENSA
1681 #define ELF_START_MMAP 0x20000000
1683 #define ELF_CLASS ELFCLASS32
1684 #define ELF_ARCH EM_XTENSA
1686 static inline void init_thread(struct target_pt_regs
*regs
,
1687 struct image_info
*infop
)
1689 regs
->windowbase
= 0;
1690 regs
->windowstart
= 1;
1691 regs
->areg
[1] = infop
->start_stack
;
1692 regs
->pc
= infop
->entry
;
1695 /* See linux kernel: arch/xtensa/include/asm/elf.h. */
1696 #define ELF_NREG 128
1697 typedef target_elf_greg_t target_elf_gregset_t
[ELF_NREG
];
1706 TARGET_REG_WINDOWSTART
,
1707 TARGET_REG_WINDOWBASE
,
1708 TARGET_REG_THREADPTR
,
1709 TARGET_REG_AR0
= 64,
1712 static void elf_core_copy_regs(target_elf_gregset_t
*regs
,
1713 const CPUXtensaState
*env
)
1717 (*regs
)[TARGET_REG_PC
] = tswapreg(env
->pc
);
1718 (*regs
)[TARGET_REG_PS
] = tswapreg(env
->sregs
[PS
] & ~PS_EXCM
);
1719 (*regs
)[TARGET_REG_LBEG
] = tswapreg(env
->sregs
[LBEG
]);
1720 (*regs
)[TARGET_REG_LEND
] = tswapreg(env
->sregs
[LEND
]);
1721 (*regs
)[TARGET_REG_LCOUNT
] = tswapreg(env
->sregs
[LCOUNT
]);
1722 (*regs
)[TARGET_REG_SAR
] = tswapreg(env
->sregs
[SAR
]);
1723 (*regs
)[TARGET_REG_WINDOWSTART
] = tswapreg(env
->sregs
[WINDOW_START
]);
1724 (*regs
)[TARGET_REG_WINDOWBASE
] = tswapreg(env
->sregs
[WINDOW_BASE
]);
1725 (*regs
)[TARGET_REG_THREADPTR
] = tswapreg(env
->uregs
[THREADPTR
]);
1726 xtensa_sync_phys_from_window((CPUXtensaState
*)env
);
1727 for (i
= 0; i
< env
->config
->nareg
; ++i
) {
1728 (*regs
)[TARGET_REG_AR0
+ i
] = tswapreg(env
->phys_regs
[i
]);
1732 #define USE_ELF_CORE_DUMP
1733 #define ELF_EXEC_PAGESIZE 4096
1735 #endif /* TARGET_XTENSA */
1737 #ifdef TARGET_HEXAGON
1739 #define ELF_START_MMAP 0x20000000
1741 #define ELF_CLASS ELFCLASS32
1742 #define ELF_ARCH EM_HEXAGON
1744 static inline void init_thread(struct target_pt_regs
*regs
,
1745 struct image_info
*infop
)
1747 regs
->sepc
= infop
->entry
;
1748 regs
->sp
= infop
->start_stack
;
1751 #endif /* TARGET_HEXAGON */
1753 #ifndef ELF_PLATFORM
1754 #define ELF_PLATFORM (NULL)
1758 #define ELF_MACHINE ELF_ARCH
1761 #ifndef elf_check_arch
1762 #define elf_check_arch(x) ((x) == ELF_ARCH)
1765 #ifndef elf_check_abi
1766 #define elf_check_abi(x) (1)
1773 #ifndef STACK_GROWS_DOWN
1774 #define STACK_GROWS_DOWN 1
1777 #ifndef STACK_ALIGNMENT
1778 #define STACK_ALIGNMENT 16
1783 #define ELF_CLASS ELFCLASS32
1785 #define bswaptls(ptr) bswap32s(ptr)
1790 /* We must delay the following stanzas until after "elf.h". */
1791 #if defined(TARGET_AARCH64)
1793 static bool arch_parse_elf_property(uint32_t pr_type
, uint32_t pr_datasz
,
1794 const uint32_t *data
,
1795 struct image_info
*info
,
1798 if (pr_type
== GNU_PROPERTY_AARCH64_FEATURE_1_AND
) {
1799 if (pr_datasz
!= sizeof(uint32_t)) {
1800 error_setg(errp
, "Ill-formed GNU_PROPERTY_AARCH64_FEATURE_1_AND");
1803 /* We will extract GNU_PROPERTY_AARCH64_FEATURE_1_BTI later. */
1804 info
->note_flags
= *data
;
1808 #define ARCH_USE_GNU_PROPERTY 1
1812 static bool arch_parse_elf_property(uint32_t pr_type
, uint32_t pr_datasz
,
1813 const uint32_t *data
,
1814 struct image_info
*info
,
1817 g_assert_not_reached();
1819 #define ARCH_USE_GNU_PROPERTY 0
1825 unsigned int a_info
; /* Use macros N_MAGIC, etc for access */
1826 unsigned int a_text
; /* length of text, in bytes */
1827 unsigned int a_data
; /* length of data, in bytes */
1828 unsigned int a_bss
; /* length of uninitialized data area, in bytes */
1829 unsigned int a_syms
; /* length of symbol table data in file, in bytes */
1830 unsigned int a_entry
; /* start address */
1831 unsigned int a_trsize
; /* length of relocation info for text, in bytes */
1832 unsigned int a_drsize
; /* length of relocation info for data, in bytes */
1836 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
1842 /* Necessary parameters */
1843 #define TARGET_ELF_EXEC_PAGESIZE \
1844 (((eppnt->p_align & ~qemu_host_page_mask) != 0) ? \
1845 TARGET_PAGE_SIZE : MAX(qemu_host_page_size, TARGET_PAGE_SIZE))
1846 #define TARGET_ELF_PAGELENGTH(_v) ROUND_UP((_v), TARGET_ELF_EXEC_PAGESIZE)
1847 #define TARGET_ELF_PAGESTART(_v) ((_v) & \
1848 ~(abi_ulong)(TARGET_ELF_EXEC_PAGESIZE-1))
1849 #define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
1851 #define DLINFO_ITEMS 16
1853 static inline void memcpy_fromfs(void * to
, const void * from
, unsigned long n
)
1855 memcpy(to
, from
, n
);
1859 static void bswap_ehdr(struct elfhdr
*ehdr
)
1861 bswap16s(&ehdr
->e_type
); /* Object file type */
1862 bswap16s(&ehdr
->e_machine
); /* Architecture */
1863 bswap32s(&ehdr
->e_version
); /* Object file version */
1864 bswaptls(&ehdr
->e_entry
); /* Entry point virtual address */
1865 bswaptls(&ehdr
->e_phoff
); /* Program header table file offset */
1866 bswaptls(&ehdr
->e_shoff
); /* Section header table file offset */
1867 bswap32s(&ehdr
->e_flags
); /* Processor-specific flags */
1868 bswap16s(&ehdr
->e_ehsize
); /* ELF header size in bytes */
1869 bswap16s(&ehdr
->e_phentsize
); /* Program header table entry size */
1870 bswap16s(&ehdr
->e_phnum
); /* Program header table entry count */
1871 bswap16s(&ehdr
->e_shentsize
); /* Section header table entry size */
1872 bswap16s(&ehdr
->e_shnum
); /* Section header table entry count */
1873 bswap16s(&ehdr
->e_shstrndx
); /* Section header string table index */
1876 static void bswap_phdr(struct elf_phdr
*phdr
, int phnum
)
1879 for (i
= 0; i
< phnum
; ++i
, ++phdr
) {
1880 bswap32s(&phdr
->p_type
); /* Segment type */
1881 bswap32s(&phdr
->p_flags
); /* Segment flags */
1882 bswaptls(&phdr
->p_offset
); /* Segment file offset */
1883 bswaptls(&phdr
->p_vaddr
); /* Segment virtual address */
1884 bswaptls(&phdr
->p_paddr
); /* Segment physical address */
1885 bswaptls(&phdr
->p_filesz
); /* Segment size in file */
1886 bswaptls(&phdr
->p_memsz
); /* Segment size in memory */
1887 bswaptls(&phdr
->p_align
); /* Segment alignment */
1891 static void bswap_shdr(struct elf_shdr
*shdr
, int shnum
)
1894 for (i
= 0; i
< shnum
; ++i
, ++shdr
) {
1895 bswap32s(&shdr
->sh_name
);
1896 bswap32s(&shdr
->sh_type
);
1897 bswaptls(&shdr
->sh_flags
);
1898 bswaptls(&shdr
->sh_addr
);
1899 bswaptls(&shdr
->sh_offset
);
1900 bswaptls(&shdr
->sh_size
);
1901 bswap32s(&shdr
->sh_link
);
1902 bswap32s(&shdr
->sh_info
);
1903 bswaptls(&shdr
->sh_addralign
);
1904 bswaptls(&shdr
->sh_entsize
);
1908 static void bswap_sym(struct elf_sym
*sym
)
1910 bswap32s(&sym
->st_name
);
1911 bswaptls(&sym
->st_value
);
1912 bswaptls(&sym
->st_size
);
1913 bswap16s(&sym
->st_shndx
);
1917 static void bswap_mips_abiflags(Mips_elf_abiflags_v0
*abiflags
)
1919 bswap16s(&abiflags
->version
);
1920 bswap32s(&abiflags
->ases
);
1921 bswap32s(&abiflags
->isa_ext
);
1922 bswap32s(&abiflags
->flags1
);
1923 bswap32s(&abiflags
->flags2
);
1927 static inline void bswap_ehdr(struct elfhdr
*ehdr
) { }
1928 static inline void bswap_phdr(struct elf_phdr
*phdr
, int phnum
) { }
1929 static inline void bswap_shdr(struct elf_shdr
*shdr
, int shnum
) { }
1930 static inline void bswap_sym(struct elf_sym
*sym
) { }
1932 static inline void bswap_mips_abiflags(Mips_elf_abiflags_v0
*abiflags
) { }
1936 #ifdef USE_ELF_CORE_DUMP
1937 static int elf_core_dump(int, const CPUArchState
*);
1938 #endif /* USE_ELF_CORE_DUMP */
1939 static void load_symbols(struct elfhdr
*hdr
, int fd
, abi_ulong load_bias
);
1941 /* Verify the portions of EHDR within E_IDENT for the target.
1942 This can be performed before bswapping the entire header. */
1943 static bool elf_check_ident(struct elfhdr
*ehdr
)
1945 return (ehdr
->e_ident
[EI_MAG0
] == ELFMAG0
1946 && ehdr
->e_ident
[EI_MAG1
] == ELFMAG1
1947 && ehdr
->e_ident
[EI_MAG2
] == ELFMAG2
1948 && ehdr
->e_ident
[EI_MAG3
] == ELFMAG3
1949 && ehdr
->e_ident
[EI_CLASS
] == ELF_CLASS
1950 && ehdr
->e_ident
[EI_DATA
] == ELF_DATA
1951 && ehdr
->e_ident
[EI_VERSION
] == EV_CURRENT
);
1954 /* Verify the portions of EHDR outside of E_IDENT for the target.
1955 This has to wait until after bswapping the header. */
1956 static bool elf_check_ehdr(struct elfhdr
*ehdr
)
1958 return (elf_check_arch(ehdr
->e_machine
)
1959 && elf_check_abi(ehdr
->e_flags
)
1960 && ehdr
->e_ehsize
== sizeof(struct elfhdr
)
1961 && ehdr
->e_phentsize
== sizeof(struct elf_phdr
)
1962 && (ehdr
->e_type
== ET_EXEC
|| ehdr
->e_type
== ET_DYN
));
1966 * 'copy_elf_strings()' copies argument/envelope strings from user
1967 * memory to free pages in kernel mem. These are in a format ready
1968 * to be put directly into the top of new user memory.
1971 static abi_ulong
copy_elf_strings(int argc
, char **argv
, char *scratch
,
1972 abi_ulong p
, abi_ulong stack_limit
)
1979 return 0; /* bullet-proofing */
1982 if (STACK_GROWS_DOWN
) {
1983 int offset
= ((p
- 1) % TARGET_PAGE_SIZE
) + 1;
1984 for (i
= argc
- 1; i
>= 0; --i
) {
1987 fprintf(stderr
, "VFS: argc is wrong");
1990 len
= strlen(tmp
) + 1;
1993 if (len
> (p
- stack_limit
)) {
1997 int bytes_to_copy
= (len
> offset
) ? offset
: len
;
1998 tmp
-= bytes_to_copy
;
2000 offset
-= bytes_to_copy
;
2001 len
-= bytes_to_copy
;
2003 memcpy_fromfs(scratch
+ offset
, tmp
, bytes_to_copy
);
2006 memcpy_to_target(p
, scratch
, top
- p
);
2008 offset
= TARGET_PAGE_SIZE
;
2013 memcpy_to_target(p
, scratch
+ offset
, top
- p
);
2016 int remaining
= TARGET_PAGE_SIZE
- (p
% TARGET_PAGE_SIZE
);
2017 for (i
= 0; i
< argc
; ++i
) {
2020 fprintf(stderr
, "VFS: argc is wrong");
2023 len
= strlen(tmp
) + 1;
2024 if (len
> (stack_limit
- p
)) {
2028 int bytes_to_copy
= (len
> remaining
) ? remaining
: len
;
2030 memcpy_fromfs(scratch
+ (p
- top
), tmp
, bytes_to_copy
);
2032 tmp
+= bytes_to_copy
;
2033 remaining
-= bytes_to_copy
;
2035 len
-= bytes_to_copy
;
2037 if (remaining
== 0) {
2038 memcpy_to_target(top
, scratch
, p
- top
);
2040 remaining
= TARGET_PAGE_SIZE
;
2045 memcpy_to_target(top
, scratch
, p
- top
);
2052 /* Older linux kernels provide up to MAX_ARG_PAGES (default: 32) of
2053 * argument/environment space. Newer kernels (>2.6.33) allow more,
2054 * dependent on stack size, but guarantee at least 32 pages for
2055 * backwards compatibility.
2057 #define STACK_LOWER_LIMIT (32 * TARGET_PAGE_SIZE)
2059 static abi_ulong
setup_arg_pages(struct linux_binprm
*bprm
,
2060 struct image_info
*info
)
2062 abi_ulong size
, error
, guard
;
2064 size
= guest_stack_size
;
2065 if (size
< STACK_LOWER_LIMIT
) {
2066 size
= STACK_LOWER_LIMIT
;
2068 guard
= TARGET_PAGE_SIZE
;
2069 if (guard
< qemu_real_host_page_size()) {
2070 guard
= qemu_real_host_page_size();
2073 error
= target_mmap(0, size
+ guard
, PROT_READ
| PROT_WRITE
,
2074 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
2076 perror("mmap stack");
2080 /* We reserve one extra page at the top of the stack as guard. */
2081 if (STACK_GROWS_DOWN
) {
2082 target_mprotect(error
, guard
, PROT_NONE
);
2083 info
->stack_limit
= error
+ guard
;
2084 return info
->stack_limit
+ size
- sizeof(void *);
2086 target_mprotect(error
+ size
, guard
, PROT_NONE
);
2087 info
->stack_limit
= error
+ size
;
2092 /* Map and zero the bss. We need to explicitly zero any fractional pages
2093 after the data section (i.e. bss). */
2094 static void zero_bss(abi_ulong elf_bss
, abi_ulong last_bss
, int prot
)
2096 uintptr_t host_start
, host_map_start
, host_end
;
2098 last_bss
= TARGET_PAGE_ALIGN(last_bss
);
2100 /* ??? There is confusion between qemu_real_host_page_size and
2101 qemu_host_page_size here and elsewhere in target_mmap, which
2102 may lead to the end of the data section mapping from the file
2103 not being mapped. At least there was an explicit test and
2104 comment for that here, suggesting that "the file size must
2105 be known". The comment probably pre-dates the introduction
2106 of the fstat system call in target_mmap which does in fact
2107 find out the size. What isn't clear is if the workaround
2108 here is still actually needed. For now, continue with it,
2109 but merge it with the "normal" mmap that would allocate the bss. */
2111 host_start
= (uintptr_t) g2h_untagged(elf_bss
);
2112 host_end
= (uintptr_t) g2h_untagged(last_bss
);
2113 host_map_start
= REAL_HOST_PAGE_ALIGN(host_start
);
2115 if (host_map_start
< host_end
) {
2116 void *p
= mmap((void *)host_map_start
, host_end
- host_map_start
,
2117 prot
, MAP_FIXED
| MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
2118 if (p
== MAP_FAILED
) {
2119 perror("cannot mmap brk");
2124 /* Ensure that the bss page(s) are valid */
2125 if ((page_get_flags(last_bss
-1) & prot
) != prot
) {
2126 page_set_flags(elf_bss
& TARGET_PAGE_MASK
, last_bss
, prot
| PAGE_VALID
);
2129 if (host_start
< host_map_start
) {
2130 memset((void *)host_start
, 0, host_map_start
- host_start
);
2135 static int elf_is_fdpic(struct elfhdr
*exec
)
2137 return exec
->e_ident
[EI_OSABI
] == ELFOSABI_ARM_FDPIC
;
2140 /* Default implementation, always false. */
2141 static int elf_is_fdpic(struct elfhdr
*exec
)
2147 static abi_ulong
loader_build_fdpic_loadmap(struct image_info
*info
, abi_ulong sp
)
2150 struct elf32_fdpic_loadseg
*loadsegs
= info
->loadsegs
;
2152 /* elf32_fdpic_loadseg */
2156 put_user_u32(loadsegs
[n
].addr
, sp
+0);
2157 put_user_u32(loadsegs
[n
].p_vaddr
, sp
+4);
2158 put_user_u32(loadsegs
[n
].p_memsz
, sp
+8);
2161 /* elf32_fdpic_loadmap */
2163 put_user_u16(0, sp
+0); /* version */
2164 put_user_u16(info
->nsegs
, sp
+2); /* nsegs */
2166 info
->personality
= PER_LINUX_FDPIC
;
2167 info
->loadmap_addr
= sp
;
2172 static abi_ulong
create_elf_tables(abi_ulong p
, int argc
, int envc
,
2173 struct elfhdr
*exec
,
2174 struct image_info
*info
,
2175 struct image_info
*interp_info
)
2178 abi_ulong u_argc
, u_argv
, u_envp
, u_auxv
;
2181 abi_ulong u_rand_bytes
;
2182 uint8_t k_rand_bytes
[16];
2183 abi_ulong u_platform
;
2184 const char *k_platform
;
2185 const int n
= sizeof(elf_addr_t
);
2189 /* Needs to be before we load the env/argc/... */
2190 if (elf_is_fdpic(exec
)) {
2191 /* Need 4 byte alignment for these structs */
2193 sp
= loader_build_fdpic_loadmap(info
, sp
);
2194 info
->other_info
= interp_info
;
2196 interp_info
->other_info
= info
;
2197 sp
= loader_build_fdpic_loadmap(interp_info
, sp
);
2198 info
->interpreter_loadmap_addr
= interp_info
->loadmap_addr
;
2199 info
->interpreter_pt_dynamic_addr
= interp_info
->pt_dynamic_addr
;
2201 info
->interpreter_loadmap_addr
= 0;
2202 info
->interpreter_pt_dynamic_addr
= 0;
2207 k_platform
= ELF_PLATFORM
;
2209 size_t len
= strlen(k_platform
) + 1;
2210 if (STACK_GROWS_DOWN
) {
2211 sp
-= (len
+ n
- 1) & ~(n
- 1);
2213 /* FIXME - check return value of memcpy_to_target() for failure */
2214 memcpy_to_target(sp
, k_platform
, len
);
2216 memcpy_to_target(sp
, k_platform
, len
);
2222 /* Provide 16 byte alignment for the PRNG, and basic alignment for
2223 * the argv and envp pointers.
2225 if (STACK_GROWS_DOWN
) {
2226 sp
= QEMU_ALIGN_DOWN(sp
, 16);
2228 sp
= QEMU_ALIGN_UP(sp
, 16);
2232 * Generate 16 random bytes for userspace PRNG seeding.
2234 qemu_guest_getrandom_nofail(k_rand_bytes
, sizeof(k_rand_bytes
));
2235 if (STACK_GROWS_DOWN
) {
2238 /* FIXME - check return value of memcpy_to_target() for failure */
2239 memcpy_to_target(sp
, k_rand_bytes
, 16);
2241 memcpy_to_target(sp
, k_rand_bytes
, 16);
2246 size
= (DLINFO_ITEMS
+ 1) * 2;
2249 #ifdef DLINFO_ARCH_ITEMS
2250 size
+= DLINFO_ARCH_ITEMS
* 2;
2255 info
->auxv_len
= size
* n
;
2257 size
+= envc
+ argc
+ 2;
2258 size
+= 1; /* argc itself */
2261 /* Allocate space and finalize stack alignment for entry now. */
2262 if (STACK_GROWS_DOWN
) {
2263 u_argc
= QEMU_ALIGN_DOWN(sp
- size
, STACK_ALIGNMENT
);
2267 sp
= QEMU_ALIGN_UP(sp
+ size
, STACK_ALIGNMENT
);
2270 u_argv
= u_argc
+ n
;
2271 u_envp
= u_argv
+ (argc
+ 1) * n
;
2272 u_auxv
= u_envp
+ (envc
+ 1) * n
;
2273 info
->saved_auxv
= u_auxv
;
2276 info
->argv
= u_argv
;
2277 info
->envp
= u_envp
;
2279 /* This is correct because Linux defines
2280 * elf_addr_t as Elf32_Off / Elf64_Off
2282 #define NEW_AUX_ENT(id, val) do { \
2283 put_user_ual(id, u_auxv); u_auxv += n; \
2284 put_user_ual(val, u_auxv); u_auxv += n; \
2289 * ARCH_DLINFO must come first so platform specific code can enforce
2290 * special alignment requirements on the AUXV if necessary (eg. PPC).
2294 /* There must be exactly DLINFO_ITEMS entries here, or the assert
2295 * on info->auxv_len will trigger.
2297 NEW_AUX_ENT(AT_PHDR
, (abi_ulong
)(info
->load_addr
+ exec
->e_phoff
));
2298 NEW_AUX_ENT(AT_PHENT
, (abi_ulong
)(sizeof (struct elf_phdr
)));
2299 NEW_AUX_ENT(AT_PHNUM
, (abi_ulong
)(exec
->e_phnum
));
2300 if ((info
->alignment
& ~qemu_host_page_mask
) != 0) {
2301 /* Target doesn't support host page size alignment */
2302 NEW_AUX_ENT(AT_PAGESZ
, (abi_ulong
)(TARGET_PAGE_SIZE
));
2304 NEW_AUX_ENT(AT_PAGESZ
, (abi_ulong
)(MAX(TARGET_PAGE_SIZE
,
2305 qemu_host_page_size
)));
2307 NEW_AUX_ENT(AT_BASE
, (abi_ulong
)(interp_info
? interp_info
->load_addr
: 0));
2308 NEW_AUX_ENT(AT_FLAGS
, (abi_ulong
)0);
2309 NEW_AUX_ENT(AT_ENTRY
, info
->entry
);
2310 NEW_AUX_ENT(AT_UID
, (abi_ulong
) getuid());
2311 NEW_AUX_ENT(AT_EUID
, (abi_ulong
) geteuid());
2312 NEW_AUX_ENT(AT_GID
, (abi_ulong
) getgid());
2313 NEW_AUX_ENT(AT_EGID
, (abi_ulong
) getegid());
2314 NEW_AUX_ENT(AT_HWCAP
, (abi_ulong
) ELF_HWCAP
);
2315 NEW_AUX_ENT(AT_CLKTCK
, (abi_ulong
) sysconf(_SC_CLK_TCK
));
2316 NEW_AUX_ENT(AT_RANDOM
, (abi_ulong
) u_rand_bytes
);
2317 NEW_AUX_ENT(AT_SECURE
, (abi_ulong
) qemu_getauxval(AT_SECURE
));
2318 NEW_AUX_ENT(AT_EXECFN
, info
->file_string
);
2321 NEW_AUX_ENT(AT_HWCAP2
, (abi_ulong
) ELF_HWCAP2
);
2325 NEW_AUX_ENT(AT_PLATFORM
, u_platform
);
2327 NEW_AUX_ENT (AT_NULL
, 0);
2330 /* Check that our initial calculation of the auxv length matches how much
2331 * we actually put into it.
2333 assert(info
->auxv_len
== u_auxv
- info
->saved_auxv
);
2335 put_user_ual(argc
, u_argc
);
2337 p
= info
->arg_strings
;
2338 for (i
= 0; i
< argc
; ++i
) {
2339 put_user_ual(p
, u_argv
);
2341 p
+= target_strlen(p
) + 1;
2343 put_user_ual(0, u_argv
);
2345 p
= info
->env_strings
;
2346 for (i
= 0; i
< envc
; ++i
) {
2347 put_user_ual(p
, u_envp
);
2349 p
+= target_strlen(p
) + 1;
2351 put_user_ual(0, u_envp
);
2356 #if defined(HI_COMMPAGE)
2357 #define LO_COMMPAGE -1
2358 #elif defined(LO_COMMPAGE)
2359 #define HI_COMMPAGE 0
2361 #define HI_COMMPAGE 0
2362 #define LO_COMMPAGE -1
2363 #define init_guest_commpage() true
2366 static void pgb_fail_in_use(const char *image_name
)
2368 error_report("%s: requires virtual address space that is in use "
2369 "(omit the -B option or choose a different value)",
2374 static void pgb_have_guest_base(const char *image_name
, abi_ulong guest_loaddr
,
2375 abi_ulong guest_hiaddr
, long align
)
2377 const int flags
= MAP_ANONYMOUS
| MAP_PRIVATE
| MAP_NORESERVE
;
2380 if (!QEMU_IS_ALIGNED(guest_base
, align
)) {
2381 fprintf(stderr
, "Requested guest base %p does not satisfy "
2382 "host minimum alignment (0x%lx)\n",
2383 (void *)guest_base
, align
);
2387 /* Sanity check the guest binary. */
2389 if (guest_hiaddr
> reserved_va
) {
2390 error_report("%s: requires more than reserved virtual "
2391 "address space (0x%" PRIx64
" > 0x%lx)",
2392 image_name
, (uint64_t)guest_hiaddr
, reserved_va
);
2396 #if HOST_LONG_BITS < TARGET_ABI_BITS
2397 if ((guest_hiaddr
- guest_base
) > ~(uintptr_t)0) {
2398 error_report("%s: requires more virtual address space "
2399 "than the host can provide (0x%" PRIx64
")",
2400 image_name
, (uint64_t)guest_hiaddr
- guest_base
);
2407 * Expand the allocation to the entire reserved_va.
2408 * Exclude the mmap_min_addr hole.
2411 guest_loaddr
= (guest_base
>= mmap_min_addr
? 0
2412 : mmap_min_addr
- guest_base
);
2413 guest_hiaddr
= reserved_va
;
2416 /* Reserve the address space for the binary, or reserved_va. */
2417 test
= g2h_untagged(guest_loaddr
);
2418 addr
= mmap(test
, guest_hiaddr
- guest_loaddr
, PROT_NONE
, flags
, -1, 0);
2420 pgb_fail_in_use(image_name
);
2422 qemu_log_mask(CPU_LOG_PAGE
,
2423 "%s: base @ %p for " TARGET_ABI_FMT_ld
" bytes\n",
2424 __func__
, addr
, guest_hiaddr
- guest_loaddr
);
2428 * pgd_find_hole_fallback: potential mmap address
2429 * @guest_size: size of available space
2430 * @brk: location of break
2431 * @align: memory alignment
2433 * This is a fallback method for finding a hole in the host address
2434 * space if we don't have the benefit of being able to access
2435 * /proc/self/map. It can potentially take a very long time as we can
2436 * only dumbly iterate up the host address space seeing if the
2437 * allocation would work.
2439 static uintptr_t pgd_find_hole_fallback(uintptr_t guest_size
, uintptr_t brk
,
2440 long align
, uintptr_t offset
)
2444 /* Start (aligned) at the bottom and work our way up */
2445 base
= ROUND_UP(mmap_min_addr
, align
);
2448 uintptr_t align_start
, end
;
2449 align_start
= ROUND_UP(base
, align
);
2450 end
= align_start
+ guest_size
+ offset
;
2452 /* if brk is anywhere in the range give ourselves some room to grow. */
2453 if (align_start
<= brk
&& brk
< end
) {
2454 base
= brk
+ (16 * MiB
);
2456 } else if (align_start
+ guest_size
< align_start
) {
2457 /* we have run out of space */
2460 int flags
= MAP_ANONYMOUS
| MAP_PRIVATE
| MAP_NORESERVE
|
2461 MAP_FIXED_NOREPLACE
;
2462 void * mmap_start
= mmap((void *) align_start
, guest_size
,
2463 PROT_NONE
, flags
, -1, 0);
2464 if (mmap_start
!= MAP_FAILED
) {
2465 munmap(mmap_start
, guest_size
);
2466 if (mmap_start
== (void *) align_start
) {
2467 qemu_log_mask(CPU_LOG_PAGE
,
2468 "%s: base @ %p for %" PRIdPTR
" bytes\n",
2469 __func__
, mmap_start
+ offset
, guest_size
);
2470 return (uintptr_t) mmap_start
+ offset
;
2473 base
+= qemu_host_page_size
;
2478 /* Return value for guest_base, or -1 if no hole found. */
2479 static uintptr_t pgb_find_hole(uintptr_t guest_loaddr
, uintptr_t guest_size
,
2480 long align
, uintptr_t offset
)
2482 GSList
*maps
, *iter
;
2483 uintptr_t this_start
, this_end
, next_start
, brk
;
2486 assert(QEMU_IS_ALIGNED(guest_loaddr
, align
));
2488 maps
= read_self_maps();
2490 /* Read brk after we've read the maps, which will malloc. */
2491 brk
= (uintptr_t)sbrk(0);
2494 return pgd_find_hole_fallback(guest_size
, brk
, align
, offset
);
2497 /* The first hole is before the first map entry. */
2498 this_start
= mmap_min_addr
;
2500 for (iter
= maps
; iter
;
2501 this_start
= next_start
, iter
= g_slist_next(iter
)) {
2502 uintptr_t align_start
, hole_size
;
2504 this_end
= ((MapInfo
*)iter
->data
)->start
;
2505 next_start
= ((MapInfo
*)iter
->data
)->end
;
2506 align_start
= ROUND_UP(this_start
+ offset
, align
);
2508 /* Skip holes that are too small. */
2509 if (align_start
>= this_end
) {
2512 hole_size
= this_end
- align_start
;
2513 if (hole_size
< guest_size
) {
2517 /* If this hole contains brk, give ourselves some room to grow. */
2518 if (this_start
<= brk
&& brk
< this_end
) {
2519 hole_size
-= guest_size
;
2520 if (sizeof(uintptr_t) == 8 && hole_size
>= 1 * GiB
) {
2521 align_start
+= 1 * GiB
;
2522 } else if (hole_size
>= 16 * MiB
) {
2523 align_start
+= 16 * MiB
;
2525 align_start
= (this_end
- guest_size
) & -align
;
2526 if (align_start
< this_start
) {
2532 /* Record the lowest successful match. */
2536 /* If this hole contains the identity map, select it. */
2537 if (align_start
<= guest_loaddr
&&
2538 guest_loaddr
+ guest_size
<= this_end
) {
2541 /* If this hole ends above the identity map, stop looking. */
2542 if (this_end
>= guest_loaddr
) {
2546 free_self_maps(maps
);
2549 qemu_log_mask(CPU_LOG_PAGE
, "%s: base @ %" PRIxPTR
2550 " for %" PRIuPTR
" bytes\n",
2551 __func__
, ret
, guest_size
);
2557 static void pgb_static(const char *image_name
, abi_ulong orig_loaddr
,
2558 abi_ulong orig_hiaddr
, long align
)
2560 uintptr_t loaddr
= orig_loaddr
;
2561 uintptr_t hiaddr
= orig_hiaddr
;
2562 uintptr_t offset
= 0;
2565 if (hiaddr
!= orig_hiaddr
) {
2566 error_report("%s: requires virtual address space that the "
2567 "host cannot provide (0x%" PRIx64
")",
2568 image_name
, (uint64_t)orig_hiaddr
);
2575 * Extend the allocation to include the commpage.
2576 * For a 64-bit host, this is just 4GiB; for a 32-bit host we
2577 * need to ensure there is space bellow the guest_base so we
2578 * can map the commpage in the place needed when the address
2579 * arithmetic wraps around.
2581 if (sizeof(uintptr_t) == 8 || loaddr
>= 0x80000000u
) {
2582 hiaddr
= (uintptr_t) 4 << 30;
2584 offset
= -(HI_COMMPAGE
& -align
);
2586 } else if (LO_COMMPAGE
!= -1) {
2587 loaddr
= MIN(loaddr
, LO_COMMPAGE
& -align
);
2590 addr
= pgb_find_hole(loaddr
, hiaddr
- loaddr
, align
, offset
);
2593 * If HI_COMMPAGE, there *might* be a non-consecutive allocation
2594 * that can satisfy both. But as the normal arm32 link base address
2595 * is ~32k, and we extend down to include the commpage, making the
2596 * overhead only ~96k, this is unlikely.
2598 error_report("%s: Unable to allocate %#zx bytes of "
2599 "virtual address space", image_name
,
2600 (size_t)(hiaddr
- loaddr
));
2606 qemu_log_mask(CPU_LOG_PAGE
, "%s: base @ %"PRIxPTR
" for %" PRIuPTR
" bytes\n",
2607 __func__
, addr
, hiaddr
- loaddr
);
2610 static void pgb_dynamic(const char *image_name
, long align
)
2613 * The executable is dynamic and does not require a fixed address.
2614 * All we need is a commpage that satisfies align.
2615 * If we do not need a commpage, leave guest_base == 0.
2618 uintptr_t addr
, commpage
;
2620 /* 64-bit hosts should have used reserved_va. */
2621 assert(sizeof(uintptr_t) == 4);
2624 * By putting the commpage at the first hole, that puts guest_base
2625 * just above that, and maximises the positive guest addresses.
2627 commpage
= HI_COMMPAGE
& -align
;
2628 addr
= pgb_find_hole(commpage
, -commpage
, align
, 0);
2634 static void pgb_reserved_va(const char *image_name
, abi_ulong guest_loaddr
,
2635 abi_ulong guest_hiaddr
, long align
)
2637 int flags
= MAP_ANONYMOUS
| MAP_PRIVATE
| MAP_NORESERVE
;
2640 if (guest_hiaddr
> reserved_va
) {
2641 error_report("%s: requires more than reserved virtual "
2642 "address space (0x%" PRIx64
" > 0x%lx)",
2643 image_name
, (uint64_t)guest_hiaddr
, reserved_va
);
2647 /* Widen the "image" to the entire reserved address space. */
2648 pgb_static(image_name
, 0, reserved_va
, align
);
2650 /* osdep.h defines this as 0 if it's missing */
2651 flags
|= MAP_FIXED_NOREPLACE
;
2653 /* Reserve the memory on the host. */
2654 assert(guest_base
!= 0);
2655 test
= g2h_untagged(0);
2656 addr
= mmap(test
, reserved_va
, PROT_NONE
, flags
, -1, 0);
2657 if (addr
== MAP_FAILED
|| addr
!= test
) {
2658 error_report("Unable to reserve 0x%lx bytes of virtual address "
2659 "space at %p (%s) for use as guest address space (check your "
2660 "virtual memory ulimit setting, min_mmap_addr or reserve less "
2661 "using -R option)", reserved_va
, test
, strerror(errno
));
2665 qemu_log_mask(CPU_LOG_PAGE
, "%s: base @ %p for %lu bytes\n",
2666 __func__
, addr
, reserved_va
);
2669 void probe_guest_base(const char *image_name
, abi_ulong guest_loaddr
,
2670 abi_ulong guest_hiaddr
)
2672 /* In order to use host shmat, we must be able to honor SHMLBA. */
2673 uintptr_t align
= MAX(SHMLBA
, qemu_host_page_size
);
2675 if (have_guest_base
) {
2676 pgb_have_guest_base(image_name
, guest_loaddr
, guest_hiaddr
, align
);
2677 } else if (reserved_va
) {
2678 pgb_reserved_va(image_name
, guest_loaddr
, guest_hiaddr
, align
);
2679 } else if (guest_loaddr
) {
2680 pgb_static(image_name
, guest_loaddr
, guest_hiaddr
, align
);
2682 pgb_dynamic(image_name
, align
);
2685 /* Reserve and initialize the commpage. */
2686 if (!init_guest_commpage()) {
2688 * With have_guest_base, the user has selected the address and
2689 * we are trying to work with that. Otherwise, we have selected
2690 * free space and init_guest_commpage must succeeded.
2692 assert(have_guest_base
);
2693 pgb_fail_in_use(image_name
);
2696 assert(QEMU_IS_ALIGNED(guest_base
, align
));
2697 qemu_log_mask(CPU_LOG_PAGE
, "Locating guest address space "
2698 "@ 0x%" PRIx64
"\n", (uint64_t)guest_base
);
2702 /* The string "GNU\0" as a magic number. */
2703 GNU0_MAGIC
= const_le32('G' | 'N' << 8 | 'U' << 16),
2704 NOTE_DATA_SZ
= 1 * KiB
,
2706 ELF_GNU_PROPERTY_ALIGN
= ELF_CLASS
== ELFCLASS32
? 4 : 8,
2710 * Process a single gnu_property entry.
2711 * Return false for error.
2713 static bool parse_elf_property(const uint32_t *data
, int *off
, int datasz
,
2714 struct image_info
*info
, bool have_prev_type
,
2715 uint32_t *prev_type
, Error
**errp
)
2717 uint32_t pr_type
, pr_datasz
, step
;
2719 if (*off
> datasz
|| !QEMU_IS_ALIGNED(*off
, ELF_GNU_PROPERTY_ALIGN
)) {
2723 data
+= *off
/ sizeof(uint32_t);
2725 if (datasz
< 2 * sizeof(uint32_t)) {
2729 pr_datasz
= data
[1];
2731 datasz
-= 2 * sizeof(uint32_t);
2732 step
= ROUND_UP(pr_datasz
, ELF_GNU_PROPERTY_ALIGN
);
2733 if (step
> datasz
) {
2737 /* Properties are supposed to be unique and sorted on pr_type. */
2738 if (have_prev_type
&& pr_type
<= *prev_type
) {
2739 if (pr_type
== *prev_type
) {
2740 error_setg(errp
, "Duplicate property in PT_GNU_PROPERTY");
2742 error_setg(errp
, "Unsorted property in PT_GNU_PROPERTY");
2746 *prev_type
= pr_type
;
2748 if (!arch_parse_elf_property(pr_type
, pr_datasz
, data
, info
, errp
)) {
2752 *off
+= 2 * sizeof(uint32_t) + step
;
2756 error_setg(errp
, "Ill-formed property in PT_GNU_PROPERTY");
2760 /* Process NT_GNU_PROPERTY_TYPE_0. */
2761 static bool parse_elf_properties(int image_fd
,
2762 struct image_info
*info
,
2763 const struct elf_phdr
*phdr
,
2764 char bprm_buf
[BPRM_BUF_SIZE
],
2768 struct elf_note nhdr
;
2769 uint32_t data
[NOTE_DATA_SZ
/ sizeof(uint32_t)];
2773 bool have_prev_type
;
2776 /* Unless the arch requires properties, ignore them. */
2777 if (!ARCH_USE_GNU_PROPERTY
) {
2781 /* If the properties are crazy large, that's too bad. */
2783 if (n
> sizeof(note
)) {
2784 error_setg(errp
, "PT_GNU_PROPERTY too large");
2787 if (n
< sizeof(note
.nhdr
)) {
2788 error_setg(errp
, "PT_GNU_PROPERTY too small");
2792 if (phdr
->p_offset
+ n
<= BPRM_BUF_SIZE
) {
2793 memcpy(¬e
, bprm_buf
+ phdr
->p_offset
, n
);
2795 ssize_t len
= pread(image_fd
, ¬e
, n
, phdr
->p_offset
);
2797 error_setg_errno(errp
, errno
, "Error reading file header");
2803 * The contents of a valid PT_GNU_PROPERTY is a sequence
2804 * of uint32_t -- swap them all now.
2807 for (int i
= 0; i
< n
/ 4; i
++) {
2808 bswap32s(note
.data
+ i
);
2813 * Note that nhdr is 3 words, and that the "name" described by namesz
2814 * immediately follows nhdr and is thus at the 4th word. Further, all
2815 * of the inputs to the kernel's round_up are multiples of 4.
2817 if (note
.nhdr
.n_type
!= NT_GNU_PROPERTY_TYPE_0
||
2818 note
.nhdr
.n_namesz
!= NOTE_NAME_SZ
||
2819 note
.data
[3] != GNU0_MAGIC
) {
2820 error_setg(errp
, "Invalid note in PT_GNU_PROPERTY");
2823 off
= sizeof(note
.nhdr
) + NOTE_NAME_SZ
;
2825 datasz
= note
.nhdr
.n_descsz
+ off
;
2827 error_setg(errp
, "Invalid note size in PT_GNU_PROPERTY");
2831 have_prev_type
= false;
2834 if (off
== datasz
) {
2835 return true; /* end, exit ok */
2837 if (!parse_elf_property(note
.data
, &off
, datasz
, info
,
2838 have_prev_type
, &prev_type
, errp
)) {
2841 have_prev_type
= true;
2845 /* Load an ELF image into the address space.
2847 IMAGE_NAME is the filename of the image, to use in error messages.
2848 IMAGE_FD is the open file descriptor for the image.
2850 BPRM_BUF is a copy of the beginning of the file; this of course
2851 contains the elf file header at offset 0. It is assumed that this
2852 buffer is sufficiently aligned to present no problems to the host
2853 in accessing data at aligned offsets within the buffer.
2855 On return: INFO values will be filled in, as necessary or available. */
2857 static void load_elf_image(const char *image_name
, int image_fd
,
2858 struct image_info
*info
, char **pinterp_name
,
2859 char bprm_buf
[BPRM_BUF_SIZE
])
2861 struct elfhdr
*ehdr
= (struct elfhdr
*)bprm_buf
;
2862 struct elf_phdr
*phdr
;
2863 abi_ulong load_addr
, load_bias
, loaddr
, hiaddr
, error
;
2864 int i
, retval
, prot_exec
;
2867 /* First of all, some simple consistency checks */
2868 if (!elf_check_ident(ehdr
)) {
2869 error_setg(&err
, "Invalid ELF image for this architecture");
2873 if (!elf_check_ehdr(ehdr
)) {
2874 error_setg(&err
, "Invalid ELF image for this architecture");
2878 i
= ehdr
->e_phnum
* sizeof(struct elf_phdr
);
2879 if (ehdr
->e_phoff
+ i
<= BPRM_BUF_SIZE
) {
2880 phdr
= (struct elf_phdr
*)(bprm_buf
+ ehdr
->e_phoff
);
2882 phdr
= (struct elf_phdr
*) alloca(i
);
2883 retval
= pread(image_fd
, phdr
, i
, ehdr
->e_phoff
);
2888 bswap_phdr(phdr
, ehdr
->e_phnum
);
2891 info
->pt_dynamic_addr
= 0;
2896 * Find the maximum size of the image and allocate an appropriate
2897 * amount of memory to handle that. Locate the interpreter, if any.
2899 loaddr
= -1, hiaddr
= 0;
2900 info
->alignment
= 0;
2901 for (i
= 0; i
< ehdr
->e_phnum
; ++i
) {
2902 struct elf_phdr
*eppnt
= phdr
+ i
;
2903 if (eppnt
->p_type
== PT_LOAD
) {
2904 abi_ulong a
= eppnt
->p_vaddr
- eppnt
->p_offset
;
2908 a
= eppnt
->p_vaddr
+ eppnt
->p_memsz
;
2913 info
->alignment
|= eppnt
->p_align
;
2914 } else if (eppnt
->p_type
== PT_INTERP
&& pinterp_name
) {
2915 g_autofree
char *interp_name
= NULL
;
2917 if (*pinterp_name
) {
2918 error_setg(&err
, "Multiple PT_INTERP entries");
2922 interp_name
= g_malloc(eppnt
->p_filesz
);
2924 if (eppnt
->p_offset
+ eppnt
->p_filesz
<= BPRM_BUF_SIZE
) {
2925 memcpy(interp_name
, bprm_buf
+ eppnt
->p_offset
,
2928 retval
= pread(image_fd
, interp_name
, eppnt
->p_filesz
,
2930 if (retval
!= eppnt
->p_filesz
) {
2934 if (interp_name
[eppnt
->p_filesz
- 1] != 0) {
2935 error_setg(&err
, "Invalid PT_INTERP entry");
2938 *pinterp_name
= g_steal_pointer(&interp_name
);
2939 } else if (eppnt
->p_type
== PT_GNU_PROPERTY
) {
2940 if (!parse_elf_properties(image_fd
, info
, eppnt
, bprm_buf
, &err
)) {
2946 if (pinterp_name
!= NULL
) {
2948 * This is the main executable.
2950 * Reserve extra space for brk.
2951 * We hold on to this space while placing the interpreter
2952 * and the stack, lest they be placed immediately after
2953 * the data segment and block allocation from the brk.
2955 * 16MB is chosen as "large enough" without being so large as
2956 * to allow the result to not fit with a 32-bit guest on a
2957 * 32-bit host. However some 64 bit guests (e.g. s390x)
2958 * attempt to place their heap further ahead and currently
2959 * nothing stops them smashing into QEMUs address space.
2961 #if TARGET_LONG_BITS == 64
2962 info
->reserve_brk
= 32 * MiB
;
2964 info
->reserve_brk
= 16 * MiB
;
2966 hiaddr
+= info
->reserve_brk
;
2968 if (ehdr
->e_type
== ET_EXEC
) {
2970 * Make sure that the low address does not conflict with
2971 * MMAP_MIN_ADDR or the QEMU application itself.
2973 probe_guest_base(image_name
, loaddr
, hiaddr
);
2976 * The binary is dynamic, but we still need to
2977 * select guest_base. In this case we pass a size.
2979 probe_guest_base(image_name
, 0, hiaddr
- loaddr
);
2984 * Reserve address space for all of this.
2986 * In the case of ET_EXEC, we supply MAP_FIXED so that we get
2987 * exactly the address range that is required.
2989 * Otherwise this is ET_DYN, and we are searching for a location
2990 * that can hold the memory space required. If the image is
2991 * pre-linked, LOADDR will be non-zero, and the kernel should
2992 * honor that address if it happens to be free.
2994 * In both cases, we will overwrite pages in this range with mappings
2995 * from the executable.
2997 load_addr
= target_mmap(loaddr
, hiaddr
- loaddr
, PROT_NONE
,
2998 MAP_PRIVATE
| MAP_ANON
| MAP_NORESERVE
|
2999 (ehdr
->e_type
== ET_EXEC
? MAP_FIXED
: 0),
3001 if (load_addr
== -1) {
3004 load_bias
= load_addr
- loaddr
;
3006 if (elf_is_fdpic(ehdr
)) {
3007 struct elf32_fdpic_loadseg
*loadsegs
= info
->loadsegs
=
3008 g_malloc(sizeof(*loadsegs
) * info
->nsegs
);
3010 for (i
= 0; i
< ehdr
->e_phnum
; ++i
) {
3011 switch (phdr
[i
].p_type
) {
3013 info
->pt_dynamic_addr
= phdr
[i
].p_vaddr
+ load_bias
;
3016 loadsegs
->addr
= phdr
[i
].p_vaddr
+ load_bias
;
3017 loadsegs
->p_vaddr
= phdr
[i
].p_vaddr
;
3018 loadsegs
->p_memsz
= phdr
[i
].p_memsz
;
3025 info
->load_bias
= load_bias
;
3026 info
->code_offset
= load_bias
;
3027 info
->data_offset
= load_bias
;
3028 info
->load_addr
= load_addr
;
3029 info
->entry
= ehdr
->e_entry
+ load_bias
;
3030 info
->start_code
= -1;
3032 info
->start_data
= -1;
3035 info
->elf_flags
= ehdr
->e_flags
;
3037 prot_exec
= PROT_EXEC
;
3038 #ifdef TARGET_AARCH64
3040 * If the BTI feature is present, this indicates that the executable
3041 * pages of the startup binary should be mapped with PROT_BTI, so that
3042 * branch targets are enforced.
3044 * The startup binary is either the interpreter or the static executable.
3045 * The interpreter is responsible for all pages of a dynamic executable.
3047 * Elf notes are backward compatible to older cpus.
3048 * Do not enable BTI unless it is supported.
3050 if ((info
->note_flags
& GNU_PROPERTY_AARCH64_FEATURE_1_BTI
)
3051 && (pinterp_name
== NULL
|| *pinterp_name
== 0)
3052 && cpu_isar_feature(aa64_bti
, ARM_CPU(thread_cpu
))) {
3053 prot_exec
|= TARGET_PROT_BTI
;
3057 for (i
= 0; i
< ehdr
->e_phnum
; i
++) {
3058 struct elf_phdr
*eppnt
= phdr
+ i
;
3059 if (eppnt
->p_type
== PT_LOAD
) {
3060 abi_ulong vaddr
, vaddr_po
, vaddr_ps
, vaddr_ef
, vaddr_em
, vaddr_len
;
3063 if (eppnt
->p_flags
& PF_R
) {
3064 elf_prot
|= PROT_READ
;
3066 if (eppnt
->p_flags
& PF_W
) {
3067 elf_prot
|= PROT_WRITE
;
3069 if (eppnt
->p_flags
& PF_X
) {
3070 elf_prot
|= prot_exec
;
3073 vaddr
= load_bias
+ eppnt
->p_vaddr
;
3074 vaddr_po
= TARGET_ELF_PAGEOFFSET(vaddr
);
3075 vaddr_ps
= TARGET_ELF_PAGESTART(vaddr
);
3077 vaddr_ef
= vaddr
+ eppnt
->p_filesz
;
3078 vaddr_em
= vaddr
+ eppnt
->p_memsz
;
3081 * Some segments may be completely empty, with a non-zero p_memsz
3082 * but no backing file segment.
3084 if (eppnt
->p_filesz
!= 0) {
3085 vaddr_len
= TARGET_ELF_PAGELENGTH(eppnt
->p_filesz
+ vaddr_po
);
3086 error
= target_mmap(vaddr_ps
, vaddr_len
, elf_prot
,
3087 MAP_PRIVATE
| MAP_FIXED
,
3088 image_fd
, eppnt
->p_offset
- vaddr_po
);
3095 * If the load segment requests extra zeros (e.g. bss), map it.
3097 if (eppnt
->p_filesz
< eppnt
->p_memsz
) {
3098 zero_bss(vaddr_ef
, vaddr_em
, elf_prot
);
3100 } else if (eppnt
->p_memsz
!= 0) {
3101 vaddr_len
= TARGET_ELF_PAGELENGTH(eppnt
->p_memsz
+ vaddr_po
);
3102 error
= target_mmap(vaddr_ps
, vaddr_len
, elf_prot
,
3103 MAP_PRIVATE
| MAP_FIXED
| MAP_ANONYMOUS
,
3111 /* Find the full program boundaries. */
3112 if (elf_prot
& PROT_EXEC
) {
3113 if (vaddr
< info
->start_code
) {
3114 info
->start_code
= vaddr
;
3116 if (vaddr_ef
> info
->end_code
) {
3117 info
->end_code
= vaddr_ef
;
3120 if (elf_prot
& PROT_WRITE
) {
3121 if (vaddr
< info
->start_data
) {
3122 info
->start_data
= vaddr
;
3124 if (vaddr_ef
> info
->end_data
) {
3125 info
->end_data
= vaddr_ef
;
3128 if (vaddr_em
> info
->brk
) {
3129 info
->brk
= vaddr_em
;
3132 } else if (eppnt
->p_type
== PT_MIPS_ABIFLAGS
) {
3133 Mips_elf_abiflags_v0 abiflags
;
3134 if (eppnt
->p_filesz
< sizeof(Mips_elf_abiflags_v0
)) {
3135 error_setg(&err
, "Invalid PT_MIPS_ABIFLAGS entry");
3138 if (eppnt
->p_offset
+ eppnt
->p_filesz
<= BPRM_BUF_SIZE
) {
3139 memcpy(&abiflags
, bprm_buf
+ eppnt
->p_offset
,
3140 sizeof(Mips_elf_abiflags_v0
));
3142 retval
= pread(image_fd
, &abiflags
, sizeof(Mips_elf_abiflags_v0
),
3144 if (retval
!= sizeof(Mips_elf_abiflags_v0
)) {
3148 bswap_mips_abiflags(&abiflags
);
3149 info
->fp_abi
= abiflags
.fp_abi
;
3154 if (info
->end_data
== 0) {
3155 info
->start_data
= info
->end_code
;
3156 info
->end_data
= info
->end_code
;
3159 if (qemu_log_enabled()) {
3160 load_symbols(ehdr
, image_fd
, load_bias
);
3170 error_setg(&err
, "Incomplete read of file header");
3172 error_setg_errno(&err
, errno
, "Error reading file header");
3176 error_setg_errno(&err
, errno
, "Error mapping file");
3179 error_reportf_err(err
, "%s: ", image_name
);
3183 static void load_elf_interp(const char *filename
, struct image_info
*info
,
3184 char bprm_buf
[BPRM_BUF_SIZE
])
3189 fd
= open(path(filename
), O_RDONLY
);
3191 error_setg_file_open(&err
, errno
, filename
);
3192 error_report_err(err
);
3196 retval
= read(fd
, bprm_buf
, BPRM_BUF_SIZE
);
3198 error_setg_errno(&err
, errno
, "Error reading file header");
3199 error_reportf_err(err
, "%s: ", filename
);
3203 if (retval
< BPRM_BUF_SIZE
) {
3204 memset(bprm_buf
+ retval
, 0, BPRM_BUF_SIZE
- retval
);
3207 load_elf_image(filename
, fd
, info
, NULL
, bprm_buf
);
3210 static int symfind(const void *s0
, const void *s1
)
3212 target_ulong addr
= *(target_ulong
*)s0
;
3213 struct elf_sym
*sym
= (struct elf_sym
*)s1
;
3215 if (addr
< sym
->st_value
) {
3217 } else if (addr
>= sym
->st_value
+ sym
->st_size
) {
3223 static const char *lookup_symbolxx(struct syminfo
*s
, target_ulong orig_addr
)
3225 #if ELF_CLASS == ELFCLASS32
3226 struct elf_sym
*syms
= s
->disas_symtab
.elf32
;
3228 struct elf_sym
*syms
= s
->disas_symtab
.elf64
;
3232 struct elf_sym
*sym
;
3234 sym
= bsearch(&orig_addr
, syms
, s
->disas_num_syms
, sizeof(*syms
), symfind
);
3236 return s
->disas_strtab
+ sym
->st_name
;
3242 /* FIXME: This should use elf_ops.h */
3243 static int symcmp(const void *s0
, const void *s1
)
3245 struct elf_sym
*sym0
= (struct elf_sym
*)s0
;
3246 struct elf_sym
*sym1
= (struct elf_sym
*)s1
;
3247 return (sym0
->st_value
< sym1
->st_value
)
3249 : ((sym0
->st_value
> sym1
->st_value
) ? 1 : 0);
3252 /* Best attempt to load symbols from this ELF object. */
3253 static void load_symbols(struct elfhdr
*hdr
, int fd
, abi_ulong load_bias
)
3255 int i
, shnum
, nsyms
, sym_idx
= 0, str_idx
= 0;
3257 struct elf_shdr
*shdr
;
3258 char *strings
= NULL
;
3259 struct syminfo
*s
= NULL
;
3260 struct elf_sym
*new_syms
, *syms
= NULL
;
3262 shnum
= hdr
->e_shnum
;
3263 i
= shnum
* sizeof(struct elf_shdr
);
3264 shdr
= (struct elf_shdr
*)alloca(i
);
3265 if (pread(fd
, shdr
, i
, hdr
->e_shoff
) != i
) {
3269 bswap_shdr(shdr
, shnum
);
3270 for (i
= 0; i
< shnum
; ++i
) {
3271 if (shdr
[i
].sh_type
== SHT_SYMTAB
) {
3273 str_idx
= shdr
[i
].sh_link
;
3278 /* There will be no symbol table if the file was stripped. */
3282 /* Now know where the strtab and symtab are. Snarf them. */
3283 s
= g_try_new(struct syminfo
, 1);
3288 segsz
= shdr
[str_idx
].sh_size
;
3289 s
->disas_strtab
= strings
= g_try_malloc(segsz
);
3291 pread(fd
, strings
, segsz
, shdr
[str_idx
].sh_offset
) != segsz
) {
3295 segsz
= shdr
[sym_idx
].sh_size
;
3296 syms
= g_try_malloc(segsz
);
3297 if (!syms
|| pread(fd
, syms
, segsz
, shdr
[sym_idx
].sh_offset
) != segsz
) {
3301 if (segsz
/ sizeof(struct elf_sym
) > INT_MAX
) {
3302 /* Implausibly large symbol table: give up rather than ploughing
3303 * on with the number of symbols calculation overflowing
3307 nsyms
= segsz
/ sizeof(struct elf_sym
);
3308 for (i
= 0; i
< nsyms
; ) {
3309 bswap_sym(syms
+ i
);
3310 /* Throw away entries which we do not need. */
3311 if (syms
[i
].st_shndx
== SHN_UNDEF
3312 || syms
[i
].st_shndx
>= SHN_LORESERVE
3313 || ELF_ST_TYPE(syms
[i
].st_info
) != STT_FUNC
) {
3315 syms
[i
] = syms
[nsyms
];
3318 #if defined(TARGET_ARM) || defined (TARGET_MIPS)
3319 /* The bottom address bit marks a Thumb or MIPS16 symbol. */
3320 syms
[i
].st_value
&= ~(target_ulong
)1;
3322 syms
[i
].st_value
+= load_bias
;
3327 /* No "useful" symbol. */
3332 /* Attempt to free the storage associated with the local symbols
3333 that we threw away. Whether or not this has any effect on the
3334 memory allocation depends on the malloc implementation and how
3335 many symbols we managed to discard. */
3336 new_syms
= g_try_renew(struct elf_sym
, syms
, nsyms
);
3337 if (new_syms
== NULL
) {
3342 qsort(syms
, nsyms
, sizeof(*syms
), symcmp
);
3344 s
->disas_num_syms
= nsyms
;
3345 #if ELF_CLASS == ELFCLASS32
3346 s
->disas_symtab
.elf32
= syms
;
3348 s
->disas_symtab
.elf64
= syms
;
3350 s
->lookup_symbol
= lookup_symbolxx
;
3362 uint32_t get_elf_eflags(int fd
)
3368 /* Read ELF header */
3369 offset
= lseek(fd
, 0, SEEK_SET
);
3370 if (offset
== (off_t
) -1) {
3373 ret
= read(fd
, &ehdr
, sizeof(ehdr
));
3374 if (ret
< sizeof(ehdr
)) {
3377 offset
= lseek(fd
, offset
, SEEK_SET
);
3378 if (offset
== (off_t
) -1) {
3382 /* Check ELF signature */
3383 if (!elf_check_ident(&ehdr
)) {
3389 if (!elf_check_ehdr(&ehdr
)) {
3393 /* return architecture id */
3394 return ehdr
.e_flags
;
3397 int load_elf_binary(struct linux_binprm
*bprm
, struct image_info
*info
)
3399 struct image_info interp_info
;
3400 struct elfhdr elf_ex
;
3401 char *elf_interpreter
= NULL
;
3404 memset(&interp_info
, 0, sizeof(interp_info
));
3406 interp_info
.fp_abi
= MIPS_ABI_FP_UNKNOWN
;
3409 info
->start_mmap
= (abi_ulong
)ELF_START_MMAP
;
3411 load_elf_image(bprm
->filename
, bprm
->fd
, info
,
3412 &elf_interpreter
, bprm
->buf
);
3414 /* ??? We need a copy of the elf header for passing to create_elf_tables.
3415 If we do nothing, we'll have overwritten this when we re-use bprm->buf
3416 when we load the interpreter. */
3417 elf_ex
= *(struct elfhdr
*)bprm
->buf
;
3419 /* Do this so that we can load the interpreter, if need be. We will
3420 change some of these later */
3421 bprm
->p
= setup_arg_pages(bprm
, info
);
3423 scratch
= g_new0(char, TARGET_PAGE_SIZE
);
3424 if (STACK_GROWS_DOWN
) {
3425 bprm
->p
= copy_elf_strings(1, &bprm
->filename
, scratch
,
3426 bprm
->p
, info
->stack_limit
);
3427 info
->file_string
= bprm
->p
;
3428 bprm
->p
= copy_elf_strings(bprm
->envc
, bprm
->envp
, scratch
,
3429 bprm
->p
, info
->stack_limit
);
3430 info
->env_strings
= bprm
->p
;
3431 bprm
->p
= copy_elf_strings(bprm
->argc
, bprm
->argv
, scratch
,
3432 bprm
->p
, info
->stack_limit
);
3433 info
->arg_strings
= bprm
->p
;
3435 info
->arg_strings
= bprm
->p
;
3436 bprm
->p
= copy_elf_strings(bprm
->argc
, bprm
->argv
, scratch
,
3437 bprm
->p
, info
->stack_limit
);
3438 info
->env_strings
= bprm
->p
;
3439 bprm
->p
= copy_elf_strings(bprm
->envc
, bprm
->envp
, scratch
,
3440 bprm
->p
, info
->stack_limit
);
3441 info
->file_string
= bprm
->p
;
3442 bprm
->p
= copy_elf_strings(1, &bprm
->filename
, scratch
,
3443 bprm
->p
, info
->stack_limit
);
3449 fprintf(stderr
, "%s: %s\n", bprm
->filename
, strerror(E2BIG
));
3453 if (elf_interpreter
) {
3454 load_elf_interp(elf_interpreter
, &interp_info
, bprm
->buf
);
3456 /* If the program interpreter is one of these two, then assume
3457 an iBCS2 image. Otherwise assume a native linux image. */
3459 if (strcmp(elf_interpreter
, "/usr/lib/libc.so.1") == 0
3460 || strcmp(elf_interpreter
, "/usr/lib/ld.so.1") == 0) {
3461 info
->personality
= PER_SVR4
;
3463 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
3464 and some applications "depend" upon this behavior. Since
3465 we do not have the power to recompile these, we emulate
3466 the SVr4 behavior. Sigh. */
3467 target_mmap(0, qemu_host_page_size
, PROT_READ
| PROT_EXEC
,
3468 MAP_FIXED
| MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
3471 info
->interp_fp_abi
= interp_info
.fp_abi
;
3476 * TODO: load a vdso, which would also contain the signal trampolines.
3477 * Otherwise, allocate a private page to hold them.
3479 if (TARGET_ARCH_HAS_SIGTRAMP_PAGE
) {
3480 abi_long tramp_page
= target_mmap(0, TARGET_PAGE_SIZE
,
3481 PROT_READ
| PROT_WRITE
,
3482 MAP_PRIVATE
| MAP_ANON
, -1, 0);
3483 if (tramp_page
== -1) {
3487 setup_sigtramp(tramp_page
);
3488 target_mprotect(tramp_page
, TARGET_PAGE_SIZE
, PROT_READ
| PROT_EXEC
);
3491 bprm
->p
= create_elf_tables(bprm
->p
, bprm
->argc
, bprm
->envc
, &elf_ex
,
3492 info
, (elf_interpreter
? &interp_info
: NULL
));
3493 info
->start_stack
= bprm
->p
;
3495 /* If we have an interpreter, set that as the program's entry point.
3496 Copy the load_bias as well, to help PPC64 interpret the entry
3497 point as a function descriptor. Do this after creating elf tables
3498 so that we copy the original program entry point into the AUXV. */
3499 if (elf_interpreter
) {
3500 info
->load_bias
= interp_info
.load_bias
;
3501 info
->entry
= interp_info
.entry
;
3502 g_free(elf_interpreter
);
3505 #ifdef USE_ELF_CORE_DUMP
3506 bprm
->core_dump
= &elf_core_dump
;
3510 * If we reserved extra space for brk, release it now.
3511 * The implementation of do_brk in syscalls.c expects to be able
3512 * to mmap pages in this space.
3514 if (info
->reserve_brk
) {
3515 abi_ulong start_brk
= HOST_PAGE_ALIGN(info
->brk
);
3516 abi_ulong end_brk
= HOST_PAGE_ALIGN(info
->brk
+ info
->reserve_brk
);
3517 target_munmap(start_brk
, end_brk
- start_brk
);
3523 #ifdef USE_ELF_CORE_DUMP
3525 * Definitions to generate Intel SVR4-like core files.
3526 * These mostly have the same names as the SVR4 types with "target_elf_"
3527 * tacked on the front to prevent clashes with linux definitions,
3528 * and the typedef forms have been avoided. This is mostly like
3529 * the SVR4 structure, but more Linuxy, with things that Linux does
3530 * not support and which gdb doesn't really use excluded.
3532 * Fields we don't dump (their contents is zero) in linux-user qemu
3533 * are marked with XXX.
3535 * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
3537 * Porting ELF coredump for target is (quite) simple process. First you
3538 * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for
3539 * the target resides):
3541 * #define USE_ELF_CORE_DUMP
3543 * Next you define type of register set used for dumping. ELF specification
3544 * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
3546 * typedef <target_regtype> target_elf_greg_t;
3547 * #define ELF_NREG <number of registers>
3548 * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG];
3550 * Last step is to implement target specific function that copies registers
3551 * from given cpu into just specified register set. Prototype is:
3553 * static void elf_core_copy_regs(taret_elf_gregset_t *regs,
3554 * const CPUArchState *env);
3557 * regs - copy register values into here (allocated and zeroed by caller)
3558 * env - copy registers from here
3560 * Example for ARM target is provided in this file.
3563 /* An ELF note in memory */
3567 size_t namesz_rounded
;
3570 size_t datasz_rounded
;
3575 struct target_elf_siginfo
{
3576 abi_int si_signo
; /* signal number */
3577 abi_int si_code
; /* extra code */
3578 abi_int si_errno
; /* errno */
3581 struct target_elf_prstatus
{
3582 struct target_elf_siginfo pr_info
; /* Info associated with signal */
3583 abi_short pr_cursig
; /* Current signal */
3584 abi_ulong pr_sigpend
; /* XXX */
3585 abi_ulong pr_sighold
; /* XXX */
3586 target_pid_t pr_pid
;
3587 target_pid_t pr_ppid
;
3588 target_pid_t pr_pgrp
;
3589 target_pid_t pr_sid
;
3590 struct target_timeval pr_utime
; /* XXX User time */
3591 struct target_timeval pr_stime
; /* XXX System time */
3592 struct target_timeval pr_cutime
; /* XXX Cumulative user time */
3593 struct target_timeval pr_cstime
; /* XXX Cumulative system time */
3594 target_elf_gregset_t pr_reg
; /* GP registers */
3595 abi_int pr_fpvalid
; /* XXX */
3598 #define ELF_PRARGSZ (80) /* Number of chars for args */
3600 struct target_elf_prpsinfo
{
3601 char pr_state
; /* numeric process state */
3602 char pr_sname
; /* char for pr_state */
3603 char pr_zomb
; /* zombie */
3604 char pr_nice
; /* nice val */
3605 abi_ulong pr_flag
; /* flags */
3606 target_uid_t pr_uid
;
3607 target_gid_t pr_gid
;
3608 target_pid_t pr_pid
, pr_ppid
, pr_pgrp
, pr_sid
;
3610 char pr_fname
[16] QEMU_NONSTRING
; /* filename of executable */
3611 char pr_psargs
[ELF_PRARGSZ
]; /* initial part of arg list */
3614 /* Here is the structure in which status of each thread is captured. */
3615 struct elf_thread_status
{
3616 QTAILQ_ENTRY(elf_thread_status
) ets_link
;
3617 struct target_elf_prstatus prstatus
; /* NT_PRSTATUS */
3619 elf_fpregset_t fpu
; /* NT_PRFPREG */
3620 struct task_struct
*thread
;
3621 elf_fpxregset_t xfpu
; /* ELF_CORE_XFPREG_TYPE */
3623 struct memelfnote notes
[1];
3627 struct elf_note_info
{
3628 struct memelfnote
*notes
;
3629 struct target_elf_prstatus
*prstatus
; /* NT_PRSTATUS */
3630 struct target_elf_prpsinfo
*psinfo
; /* NT_PRPSINFO */
3632 QTAILQ_HEAD(, elf_thread_status
) thread_list
;
3635 * Current version of ELF coredump doesn't support
3636 * dumping fp regs etc.
3638 elf_fpregset_t
*fpu
;
3639 elf_fpxregset_t
*xfpu
;
3640 int thread_status_size
;
3646 struct vm_area_struct
{
3647 target_ulong vma_start
; /* start vaddr of memory region */
3648 target_ulong vma_end
; /* end vaddr of memory region */
3649 abi_ulong vma_flags
; /* protection etc. flags for the region */
3650 QTAILQ_ENTRY(vm_area_struct
) vma_link
;
3654 QTAILQ_HEAD(, vm_area_struct
) mm_mmap
;
3655 int mm_count
; /* number of mappings */
3658 static struct mm_struct
*vma_init(void);
3659 static void vma_delete(struct mm_struct
*);
3660 static int vma_add_mapping(struct mm_struct
*, target_ulong
,
3661 target_ulong
, abi_ulong
);
3662 static int vma_get_mapping_count(const struct mm_struct
*);
3663 static struct vm_area_struct
*vma_first(const struct mm_struct
*);
3664 static struct vm_area_struct
*vma_next(struct vm_area_struct
*);
3665 static abi_ulong
vma_dump_size(const struct vm_area_struct
*);
3666 static int vma_walker(void *priv
, target_ulong start
, target_ulong end
,
3667 unsigned long flags
);
3669 static void fill_elf_header(struct elfhdr
*, int, uint16_t, uint32_t);
3670 static void fill_note(struct memelfnote
*, const char *, int,
3671 unsigned int, void *);
3672 static void fill_prstatus(struct target_elf_prstatus
*, const TaskState
*, int);
3673 static int fill_psinfo(struct target_elf_prpsinfo
*, const TaskState
*);
3674 static void fill_auxv_note(struct memelfnote
*, const TaskState
*);
3675 static void fill_elf_note_phdr(struct elf_phdr
*, int, off_t
);
3676 static size_t note_size(const struct memelfnote
*);
3677 static void free_note_info(struct elf_note_info
*);
3678 static int fill_note_info(struct elf_note_info
*, long, const CPUArchState
*);
3679 static void fill_thread_info(struct elf_note_info
*, const CPUArchState
*);
3681 static int dump_write(int, const void *, size_t);
3682 static int write_note(struct memelfnote
*, int);
3683 static int write_note_info(struct elf_note_info
*, int);
3686 static void bswap_prstatus(struct target_elf_prstatus
*prstatus
)
3688 prstatus
->pr_info
.si_signo
= tswap32(prstatus
->pr_info
.si_signo
);
3689 prstatus
->pr_info
.si_code
= tswap32(prstatus
->pr_info
.si_code
);
3690 prstatus
->pr_info
.si_errno
= tswap32(prstatus
->pr_info
.si_errno
);
3691 prstatus
->pr_cursig
= tswap16(prstatus
->pr_cursig
);
3692 prstatus
->pr_sigpend
= tswapal(prstatus
->pr_sigpend
);
3693 prstatus
->pr_sighold
= tswapal(prstatus
->pr_sighold
);
3694 prstatus
->pr_pid
= tswap32(prstatus
->pr_pid
);
3695 prstatus
->pr_ppid
= tswap32(prstatus
->pr_ppid
);
3696 prstatus
->pr_pgrp
= tswap32(prstatus
->pr_pgrp
);
3697 prstatus
->pr_sid
= tswap32(prstatus
->pr_sid
);
3698 /* cpu times are not filled, so we skip them */
3699 /* regs should be in correct format already */
3700 prstatus
->pr_fpvalid
= tswap32(prstatus
->pr_fpvalid
);
3703 static void bswap_psinfo(struct target_elf_prpsinfo
*psinfo
)
3705 psinfo
->pr_flag
= tswapal(psinfo
->pr_flag
);
3706 psinfo
->pr_uid
= tswap16(psinfo
->pr_uid
);
3707 psinfo
->pr_gid
= tswap16(psinfo
->pr_gid
);
3708 psinfo
->pr_pid
= tswap32(psinfo
->pr_pid
);
3709 psinfo
->pr_ppid
= tswap32(psinfo
->pr_ppid
);
3710 psinfo
->pr_pgrp
= tswap32(psinfo
->pr_pgrp
);
3711 psinfo
->pr_sid
= tswap32(psinfo
->pr_sid
);
3714 static void bswap_note(struct elf_note
*en
)
3716 bswap32s(&en
->n_namesz
);
3717 bswap32s(&en
->n_descsz
);
3718 bswap32s(&en
->n_type
);
3721 static inline void bswap_prstatus(struct target_elf_prstatus
*p
) { }
3722 static inline void bswap_psinfo(struct target_elf_prpsinfo
*p
) {}
3723 static inline void bswap_note(struct elf_note
*en
) { }
3724 #endif /* BSWAP_NEEDED */
3727 * Minimal support for linux memory regions. These are needed
3728 * when we are finding out what memory exactly belongs to
3729 * emulated process. No locks needed here, as long as
3730 * thread that received the signal is stopped.
3733 static struct mm_struct
*vma_init(void)
3735 struct mm_struct
*mm
;
3737 if ((mm
= g_malloc(sizeof (*mm
))) == NULL
)
3741 QTAILQ_INIT(&mm
->mm_mmap
);
3746 static void vma_delete(struct mm_struct
*mm
)
3748 struct vm_area_struct
*vma
;
3750 while ((vma
= vma_first(mm
)) != NULL
) {
3751 QTAILQ_REMOVE(&mm
->mm_mmap
, vma
, vma_link
);
3757 static int vma_add_mapping(struct mm_struct
*mm
, target_ulong start
,
3758 target_ulong end
, abi_ulong flags
)
3760 struct vm_area_struct
*vma
;
3762 if ((vma
= g_malloc0(sizeof (*vma
))) == NULL
)
3765 vma
->vma_start
= start
;
3767 vma
->vma_flags
= flags
;
3769 QTAILQ_INSERT_TAIL(&mm
->mm_mmap
, vma
, vma_link
);
3775 static struct vm_area_struct
*vma_first(const struct mm_struct
*mm
)
3777 return (QTAILQ_FIRST(&mm
->mm_mmap
));
3780 static struct vm_area_struct
*vma_next(struct vm_area_struct
*vma
)
3782 return (QTAILQ_NEXT(vma
, vma_link
));
3785 static int vma_get_mapping_count(const struct mm_struct
*mm
)
3787 return (mm
->mm_count
);
3791 * Calculate file (dump) size of given memory region.
3793 static abi_ulong
vma_dump_size(const struct vm_area_struct
*vma
)
3795 /* if we cannot even read the first page, skip it */
3796 if (!access_ok_untagged(VERIFY_READ
, vma
->vma_start
, TARGET_PAGE_SIZE
))
3800 * Usually we don't dump executable pages as they contain
3801 * non-writable code that debugger can read directly from
3802 * target library etc. However, thread stacks are marked
3803 * also executable so we read in first page of given region
3804 * and check whether it contains elf header. If there is
3805 * no elf header, we dump it.
3807 if (vma
->vma_flags
& PROT_EXEC
) {
3808 char page
[TARGET_PAGE_SIZE
];
3810 if (copy_from_user(page
, vma
->vma_start
, sizeof (page
))) {
3813 if ((page
[EI_MAG0
] == ELFMAG0
) &&
3814 (page
[EI_MAG1
] == ELFMAG1
) &&
3815 (page
[EI_MAG2
] == ELFMAG2
) &&
3816 (page
[EI_MAG3
] == ELFMAG3
)) {
3818 * Mappings are possibly from ELF binary. Don't dump
3825 return (vma
->vma_end
- vma
->vma_start
);
3828 static int vma_walker(void *priv
, target_ulong start
, target_ulong end
,
3829 unsigned long flags
)
3831 struct mm_struct
*mm
= (struct mm_struct
*)priv
;
3833 vma_add_mapping(mm
, start
, end
, flags
);
3837 static void fill_note(struct memelfnote
*note
, const char *name
, int type
,
3838 unsigned int sz
, void *data
)
3840 unsigned int namesz
;
3842 namesz
= strlen(name
) + 1;
3844 note
->namesz
= namesz
;
3845 note
->namesz_rounded
= roundup(namesz
, sizeof (int32_t));
3848 note
->datasz_rounded
= roundup(sz
, sizeof (int32_t));
3853 * We calculate rounded up note size here as specified by
3856 note
->notesz
= sizeof (struct elf_note
) +
3857 note
->namesz_rounded
+ note
->datasz_rounded
;
3860 static void fill_elf_header(struct elfhdr
*elf
, int segs
, uint16_t machine
,
3863 (void) memset(elf
, 0, sizeof(*elf
));
3865 (void) memcpy(elf
->e_ident
, ELFMAG
, SELFMAG
);
3866 elf
->e_ident
[EI_CLASS
] = ELF_CLASS
;
3867 elf
->e_ident
[EI_DATA
] = ELF_DATA
;
3868 elf
->e_ident
[EI_VERSION
] = EV_CURRENT
;
3869 elf
->e_ident
[EI_OSABI
] = ELF_OSABI
;
3871 elf
->e_type
= ET_CORE
;
3872 elf
->e_machine
= machine
;
3873 elf
->e_version
= EV_CURRENT
;
3874 elf
->e_phoff
= sizeof(struct elfhdr
);
3875 elf
->e_flags
= flags
;
3876 elf
->e_ehsize
= sizeof(struct elfhdr
);
3877 elf
->e_phentsize
= sizeof(struct elf_phdr
);
3878 elf
->e_phnum
= segs
;
3883 static void fill_elf_note_phdr(struct elf_phdr
*phdr
, int sz
, off_t offset
)
3885 phdr
->p_type
= PT_NOTE
;
3886 phdr
->p_offset
= offset
;
3889 phdr
->p_filesz
= sz
;
3894 bswap_phdr(phdr
, 1);
3897 static size_t note_size(const struct memelfnote
*note
)
3899 return (note
->notesz
);
3902 static void fill_prstatus(struct target_elf_prstatus
*prstatus
,
3903 const TaskState
*ts
, int signr
)
3905 (void) memset(prstatus
, 0, sizeof (*prstatus
));
3906 prstatus
->pr_info
.si_signo
= prstatus
->pr_cursig
= signr
;
3907 prstatus
->pr_pid
= ts
->ts_tid
;
3908 prstatus
->pr_ppid
= getppid();
3909 prstatus
->pr_pgrp
= getpgrp();
3910 prstatus
->pr_sid
= getsid(0);
3912 bswap_prstatus(prstatus
);
3915 static int fill_psinfo(struct target_elf_prpsinfo
*psinfo
, const TaskState
*ts
)
3917 char *base_filename
;
3918 unsigned int i
, len
;
3920 (void) memset(psinfo
, 0, sizeof (*psinfo
));
3922 len
= ts
->info
->env_strings
- ts
->info
->arg_strings
;
3923 if (len
>= ELF_PRARGSZ
)
3924 len
= ELF_PRARGSZ
- 1;
3925 if (copy_from_user(&psinfo
->pr_psargs
, ts
->info
->arg_strings
, len
)) {
3928 for (i
= 0; i
< len
; i
++)
3929 if (psinfo
->pr_psargs
[i
] == 0)
3930 psinfo
->pr_psargs
[i
] = ' ';
3931 psinfo
->pr_psargs
[len
] = 0;
3933 psinfo
->pr_pid
= getpid();
3934 psinfo
->pr_ppid
= getppid();
3935 psinfo
->pr_pgrp
= getpgrp();
3936 psinfo
->pr_sid
= getsid(0);
3937 psinfo
->pr_uid
= getuid();
3938 psinfo
->pr_gid
= getgid();
3940 base_filename
= g_path_get_basename(ts
->bprm
->filename
);
3942 * Using strncpy here is fine: at max-length,
3943 * this field is not NUL-terminated.
3945 (void) strncpy(psinfo
->pr_fname
, base_filename
,
3946 sizeof(psinfo
->pr_fname
));
3948 g_free(base_filename
);
3949 bswap_psinfo(psinfo
);
3953 static void fill_auxv_note(struct memelfnote
*note
, const TaskState
*ts
)
3955 elf_addr_t auxv
= (elf_addr_t
)ts
->info
->saved_auxv
;
3956 elf_addr_t orig_auxv
= auxv
;
3958 int len
= ts
->info
->auxv_len
;
3961 * Auxiliary vector is stored in target process stack. It contains
3962 * {type, value} pairs that we need to dump into note. This is not
3963 * strictly necessary but we do it here for sake of completeness.
3966 /* read in whole auxv vector and copy it to memelfnote */
3967 ptr
= lock_user(VERIFY_READ
, orig_auxv
, len
, 0);
3969 fill_note(note
, "CORE", NT_AUXV
, len
, ptr
);
3970 unlock_user(ptr
, auxv
, len
);
3975 * Constructs name of coredump file. We have following convention
3977 * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
3979 * Returns the filename
3981 static char *core_dump_filename(const TaskState
*ts
)
3983 g_autoptr(GDateTime
) now
= g_date_time_new_now_local();
3984 g_autofree
char *nowstr
= g_date_time_format(now
, "%Y%m%d-%H%M%S");
3985 g_autofree
char *base_filename
= g_path_get_basename(ts
->bprm
->filename
);
3987 return g_strdup_printf("qemu_%s_%s_%d.core",
3988 base_filename
, nowstr
, (int)getpid());
3991 static int dump_write(int fd
, const void *ptr
, size_t size
)
3993 const char *bufp
= (const char *)ptr
;
3994 ssize_t bytes_written
, bytes_left
;
3995 struct rlimit dumpsize
;
3999 getrlimit(RLIMIT_CORE
, &dumpsize
);
4000 if ((pos
= lseek(fd
, 0, SEEK_CUR
))==-1) {
4001 if (errno
== ESPIPE
) { /* not a seekable stream */
4007 if (dumpsize
.rlim_cur
<= pos
) {
4009 } else if (dumpsize
.rlim_cur
== RLIM_INFINITY
) {
4012 size_t limit_left
=dumpsize
.rlim_cur
- pos
;
4013 bytes_left
= limit_left
>= size
? size
: limit_left
;
4018 * In normal conditions, single write(2) should do but
4019 * in case of socket etc. this mechanism is more portable.
4022 bytes_written
= write(fd
, bufp
, bytes_left
);
4023 if (bytes_written
< 0) {
4027 } else if (bytes_written
== 0) { /* eof */
4030 bufp
+= bytes_written
;
4031 bytes_left
-= bytes_written
;
4032 } while (bytes_left
> 0);
4037 static int write_note(struct memelfnote
*men
, int fd
)
4041 en
.n_namesz
= men
->namesz
;
4042 en
.n_type
= men
->type
;
4043 en
.n_descsz
= men
->datasz
;
4047 if (dump_write(fd
, &en
, sizeof(en
)) != 0)
4049 if (dump_write(fd
, men
->name
, men
->namesz_rounded
) != 0)
4051 if (dump_write(fd
, men
->data
, men
->datasz_rounded
) != 0)
4057 static void fill_thread_info(struct elf_note_info
*info
, const CPUArchState
*env
)
4059 CPUState
*cpu
= env_cpu((CPUArchState
*)env
);
4060 TaskState
*ts
= (TaskState
*)cpu
->opaque
;
4061 struct elf_thread_status
*ets
;
4063 ets
= g_malloc0(sizeof (*ets
));
4064 ets
->num_notes
= 1; /* only prstatus is dumped */
4065 fill_prstatus(&ets
->prstatus
, ts
, 0);
4066 elf_core_copy_regs(&ets
->prstatus
.pr_reg
, env
);
4067 fill_note(&ets
->notes
[0], "CORE", NT_PRSTATUS
, sizeof (ets
->prstatus
),
4070 QTAILQ_INSERT_TAIL(&info
->thread_list
, ets
, ets_link
);
4072 info
->notes_size
+= note_size(&ets
->notes
[0]);
4075 static void init_note_info(struct elf_note_info
*info
)
4077 /* Initialize the elf_note_info structure so that it is at
4078 * least safe to call free_note_info() on it. Must be
4079 * called before calling fill_note_info().
4081 memset(info
, 0, sizeof (*info
));
4082 QTAILQ_INIT(&info
->thread_list
);
4085 static int fill_note_info(struct elf_note_info
*info
,
4086 long signr
, const CPUArchState
*env
)
4089 CPUState
*cpu
= env_cpu((CPUArchState
*)env
);
4090 TaskState
*ts
= (TaskState
*)cpu
->opaque
;
4093 info
->notes
= g_new0(struct memelfnote
, NUMNOTES
);
4094 if (info
->notes
== NULL
)
4096 info
->prstatus
= g_malloc0(sizeof (*info
->prstatus
));
4097 if (info
->prstatus
== NULL
)
4099 info
->psinfo
= g_malloc0(sizeof (*info
->psinfo
));
4100 if (info
->prstatus
== NULL
)
4104 * First fill in status (and registers) of current thread
4105 * including process info & aux vector.
4107 fill_prstatus(info
->prstatus
, ts
, signr
);
4108 elf_core_copy_regs(&info
->prstatus
->pr_reg
, env
);
4109 fill_note(&info
->notes
[0], "CORE", NT_PRSTATUS
,
4110 sizeof (*info
->prstatus
), info
->prstatus
);
4111 fill_psinfo(info
->psinfo
, ts
);
4112 fill_note(&info
->notes
[1], "CORE", NT_PRPSINFO
,
4113 sizeof (*info
->psinfo
), info
->psinfo
);
4114 fill_auxv_note(&info
->notes
[2], ts
);
4117 info
->notes_size
= 0;
4118 for (i
= 0; i
< info
->numnote
; i
++)
4119 info
->notes_size
+= note_size(&info
->notes
[i
]);
4121 /* read and fill status of all threads */
4124 if (cpu
== thread_cpu
) {
4127 fill_thread_info(info
, cpu
->env_ptr
);
4134 static void free_note_info(struct elf_note_info
*info
)
4136 struct elf_thread_status
*ets
;
4138 while (!QTAILQ_EMPTY(&info
->thread_list
)) {
4139 ets
= QTAILQ_FIRST(&info
->thread_list
);
4140 QTAILQ_REMOVE(&info
->thread_list
, ets
, ets_link
);
4144 g_free(info
->prstatus
);
4145 g_free(info
->psinfo
);
4146 g_free(info
->notes
);
4149 static int write_note_info(struct elf_note_info
*info
, int fd
)
4151 struct elf_thread_status
*ets
;
4154 /* write prstatus, psinfo and auxv for current thread */
4155 for (i
= 0; i
< info
->numnote
; i
++)
4156 if ((error
= write_note(&info
->notes
[i
], fd
)) != 0)
4159 /* write prstatus for each thread */
4160 QTAILQ_FOREACH(ets
, &info
->thread_list
, ets_link
) {
4161 if ((error
= write_note(&ets
->notes
[0], fd
)) != 0)
4169 * Write out ELF coredump.
4171 * See documentation of ELF object file format in:
4172 * http://www.caldera.com/developers/devspecs/gabi41.pdf
4174 * Coredump format in linux is following:
4176 * 0 +----------------------+ \
4177 * | ELF header | ET_CORE |
4178 * +----------------------+ |
4179 * | ELF program headers | |--- headers
4180 * | - NOTE section | |
4181 * | - PT_LOAD sections | |
4182 * +----------------------+ /
4187 * +----------------------+ <-- aligned to target page
4188 * | Process memory dump |
4193 * +----------------------+
4195 * NT_PRSTATUS -> struct elf_prstatus (per thread)
4196 * NT_PRSINFO -> struct elf_prpsinfo
4197 * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()).
4199 * Format follows System V format as close as possible. Current
4200 * version limitations are as follows:
4201 * - no floating point registers are dumped
4203 * Function returns 0 in case of success, negative errno otherwise.
4205 * TODO: make this work also during runtime: it should be
4206 * possible to force coredump from running process and then
4207 * continue processing. For example qemu could set up SIGUSR2
4208 * handler (provided that target process haven't registered
4209 * handler for that) that does the dump when signal is received.
4211 static int elf_core_dump(int signr
, const CPUArchState
*env
)
4213 const CPUState
*cpu
= env_cpu((CPUArchState
*)env
);
4214 const TaskState
*ts
= (const TaskState
*)cpu
->opaque
;
4215 struct vm_area_struct
*vma
= NULL
;
4216 g_autofree
char *corefile
= NULL
;
4217 struct elf_note_info info
;
4219 struct elf_phdr phdr
;
4220 struct rlimit dumpsize
;
4221 struct mm_struct
*mm
= NULL
;
4222 off_t offset
= 0, data_offset
= 0;
4226 init_note_info(&info
);
4229 getrlimit(RLIMIT_CORE
, &dumpsize
);
4230 if (dumpsize
.rlim_cur
== 0)
4233 corefile
= core_dump_filename(ts
);
4235 if ((fd
= open(corefile
, O_WRONLY
| O_CREAT
,
4236 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
)) < 0)
4240 * Walk through target process memory mappings and
4241 * set up structure containing this information. After
4242 * this point vma_xxx functions can be used.
4244 if ((mm
= vma_init()) == NULL
)
4247 walk_memory_regions(mm
, vma_walker
);
4248 segs
= vma_get_mapping_count(mm
);
4251 * Construct valid coredump ELF header. We also
4252 * add one more segment for notes.
4254 fill_elf_header(&elf
, segs
+ 1, ELF_MACHINE
, 0);
4255 if (dump_write(fd
, &elf
, sizeof (elf
)) != 0)
4258 /* fill in the in-memory version of notes */
4259 if (fill_note_info(&info
, signr
, env
) < 0)
4262 offset
+= sizeof (elf
); /* elf header */
4263 offset
+= (segs
+ 1) * sizeof (struct elf_phdr
); /* program headers */
4265 /* write out notes program header */
4266 fill_elf_note_phdr(&phdr
, info
.notes_size
, offset
);
4268 offset
+= info
.notes_size
;
4269 if (dump_write(fd
, &phdr
, sizeof (phdr
)) != 0)
4273 * ELF specification wants data to start at page boundary so
4276 data_offset
= offset
= roundup(offset
, ELF_EXEC_PAGESIZE
);
4279 * Write program headers for memory regions mapped in
4280 * the target process.
4282 for (vma
= vma_first(mm
); vma
!= NULL
; vma
= vma_next(vma
)) {
4283 (void) memset(&phdr
, 0, sizeof (phdr
));
4285 phdr
.p_type
= PT_LOAD
;
4286 phdr
.p_offset
= offset
;
4287 phdr
.p_vaddr
= vma
->vma_start
;
4289 phdr
.p_filesz
= vma_dump_size(vma
);
4290 offset
+= phdr
.p_filesz
;
4291 phdr
.p_memsz
= vma
->vma_end
- vma
->vma_start
;
4292 phdr
.p_flags
= vma
->vma_flags
& PROT_READ
? PF_R
: 0;
4293 if (vma
->vma_flags
& PROT_WRITE
)
4294 phdr
.p_flags
|= PF_W
;
4295 if (vma
->vma_flags
& PROT_EXEC
)
4296 phdr
.p_flags
|= PF_X
;
4297 phdr
.p_align
= ELF_EXEC_PAGESIZE
;
4299 bswap_phdr(&phdr
, 1);
4300 if (dump_write(fd
, &phdr
, sizeof(phdr
)) != 0) {
4306 * Next we write notes just after program headers. No
4307 * alignment needed here.
4309 if (write_note_info(&info
, fd
) < 0)
4312 /* align data to page boundary */
4313 if (lseek(fd
, data_offset
, SEEK_SET
) != data_offset
)
4317 * Finally we can dump process memory into corefile as well.
4319 for (vma
= vma_first(mm
); vma
!= NULL
; vma
= vma_next(vma
)) {
4323 end
= vma
->vma_start
+ vma_dump_size(vma
);
4325 for (addr
= vma
->vma_start
; addr
< end
;
4326 addr
+= TARGET_PAGE_SIZE
) {
4327 char page
[TARGET_PAGE_SIZE
];
4331 * Read in page from target process memory and
4332 * write it to coredump file.
4334 error
= copy_from_user(page
, addr
, sizeof (page
));
4336 (void) fprintf(stderr
, "unable to dump " TARGET_ABI_FMT_lx
"\n",
4341 if (dump_write(fd
, page
, TARGET_PAGE_SIZE
) < 0)
4347 free_note_info(&info
);
4356 #endif /* USE_ELF_CORE_DUMP */
4358 void do_init_thread(struct target_pt_regs
*regs
, struct image_info
*infop
)
4360 init_thread(regs
, infop
);