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