]> git.proxmox.com Git - mirror_qemu.git/blob - linux-user/elfload.c
linux-user/riscv: 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 static inline void init_thread(struct target_pt_regs *regs,
1201 struct image_info *infop)
1202 {
1203 /*Set crmd PG,DA = 1,0 */
1204 regs->csr.crmd = 2 << 3;
1205 regs->csr.era = infop->entry;
1206 regs->regs[3] = infop->start_stack;
1207 }
1208
1209 /* See linux kernel: arch/loongarch/include/asm/elf.h */
1210 #define ELF_NREG 45
1211 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1212
1213 enum {
1214 TARGET_EF_R0 = 0,
1215 TARGET_EF_CSR_ERA = TARGET_EF_R0 + 33,
1216 TARGET_EF_CSR_BADV = TARGET_EF_R0 + 34,
1217 };
1218
1219 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1220 const CPULoongArchState *env)
1221 {
1222 int i;
1223
1224 (*regs)[TARGET_EF_R0] = 0;
1225
1226 for (i = 1; i < ARRAY_SIZE(env->gpr); i++) {
1227 (*regs)[TARGET_EF_R0 + i] = tswapreg(env->gpr[i]);
1228 }
1229
1230 (*regs)[TARGET_EF_CSR_ERA] = tswapreg(env->pc);
1231 (*regs)[TARGET_EF_CSR_BADV] = tswapreg(env->CSR_BADV);
1232 }
1233
1234 #define USE_ELF_CORE_DUMP
1235 #define ELF_EXEC_PAGESIZE 4096
1236
1237 #define ELF_HWCAP get_elf_hwcap()
1238
1239 /* See arch/loongarch/include/uapi/asm/hwcap.h */
1240 enum {
1241 HWCAP_LOONGARCH_CPUCFG = (1 << 0),
1242 HWCAP_LOONGARCH_LAM = (1 << 1),
1243 HWCAP_LOONGARCH_UAL = (1 << 2),
1244 HWCAP_LOONGARCH_FPU = (1 << 3),
1245 HWCAP_LOONGARCH_LSX = (1 << 4),
1246 HWCAP_LOONGARCH_LASX = (1 << 5),
1247 HWCAP_LOONGARCH_CRC32 = (1 << 6),
1248 HWCAP_LOONGARCH_COMPLEX = (1 << 7),
1249 HWCAP_LOONGARCH_CRYPTO = (1 << 8),
1250 HWCAP_LOONGARCH_LVZ = (1 << 9),
1251 HWCAP_LOONGARCH_LBT_X86 = (1 << 10),
1252 HWCAP_LOONGARCH_LBT_ARM = (1 << 11),
1253 HWCAP_LOONGARCH_LBT_MIPS = (1 << 12),
1254 };
1255
1256 static uint32_t get_elf_hwcap(void)
1257 {
1258 LoongArchCPU *cpu = LOONGARCH_CPU(thread_cpu);
1259 uint32_t hwcaps = 0;
1260
1261 hwcaps |= HWCAP_LOONGARCH_CRC32;
1262
1263 if (FIELD_EX32(cpu->env.cpucfg[1], CPUCFG1, UAL)) {
1264 hwcaps |= HWCAP_LOONGARCH_UAL;
1265 }
1266
1267 if (FIELD_EX32(cpu->env.cpucfg[2], CPUCFG2, FP)) {
1268 hwcaps |= HWCAP_LOONGARCH_FPU;
1269 }
1270
1271 if (FIELD_EX32(cpu->env.cpucfg[2], CPUCFG2, LAM)) {
1272 hwcaps |= HWCAP_LOONGARCH_LAM;
1273 }
1274
1275 if (FIELD_EX32(cpu->env.cpucfg[2], CPUCFG2, LSX)) {
1276 hwcaps |= HWCAP_LOONGARCH_LSX;
1277 }
1278
1279 if (FIELD_EX32(cpu->env.cpucfg[2], CPUCFG2, LASX)) {
1280 hwcaps |= HWCAP_LOONGARCH_LASX;
1281 }
1282
1283 return hwcaps;
1284 }
1285
1286 #define ELF_PLATFORM "loongarch"
1287
1288 #endif /* TARGET_LOONGARCH64 */
1289
1290 #ifdef TARGET_MIPS
1291
1292 #ifdef TARGET_MIPS64
1293 #define ELF_CLASS ELFCLASS64
1294 #else
1295 #define ELF_CLASS ELFCLASS32
1296 #endif
1297 #define ELF_ARCH EM_MIPS
1298 #define EXSTACK_DEFAULT true
1299
1300 #ifdef TARGET_ABI_MIPSN32
1301 #define elf_check_abi(x) ((x) & EF_MIPS_ABI2)
1302 #else
1303 #define elf_check_abi(x) (!((x) & EF_MIPS_ABI2))
1304 #endif
1305
1306 #define ELF_BASE_PLATFORM get_elf_base_platform()
1307
1308 #define MATCH_PLATFORM_INSN(_flags, _base_platform) \
1309 do { if ((cpu->env.insn_flags & (_flags)) == _flags) \
1310 { return _base_platform; } } while (0)
1311
1312 static const char *get_elf_base_platform(void)
1313 {
1314 MIPSCPU *cpu = MIPS_CPU(thread_cpu);
1315
1316 /* 64 bit ISAs goes first */
1317 MATCH_PLATFORM_INSN(CPU_MIPS64R6, "mips64r6");
1318 MATCH_PLATFORM_INSN(CPU_MIPS64R5, "mips64r5");
1319 MATCH_PLATFORM_INSN(CPU_MIPS64R2, "mips64r2");
1320 MATCH_PLATFORM_INSN(CPU_MIPS64R1, "mips64");
1321 MATCH_PLATFORM_INSN(CPU_MIPS5, "mips5");
1322 MATCH_PLATFORM_INSN(CPU_MIPS4, "mips4");
1323 MATCH_PLATFORM_INSN(CPU_MIPS3, "mips3");
1324
1325 /* 32 bit ISAs */
1326 MATCH_PLATFORM_INSN(CPU_MIPS32R6, "mips32r6");
1327 MATCH_PLATFORM_INSN(CPU_MIPS32R5, "mips32r5");
1328 MATCH_PLATFORM_INSN(CPU_MIPS32R2, "mips32r2");
1329 MATCH_PLATFORM_INSN(CPU_MIPS32R1, "mips32");
1330 MATCH_PLATFORM_INSN(CPU_MIPS2, "mips2");
1331
1332 /* Fallback */
1333 return "mips";
1334 }
1335 #undef MATCH_PLATFORM_INSN
1336
1337 static inline void init_thread(struct target_pt_regs *regs,
1338 struct image_info *infop)
1339 {
1340 regs->cp0_status = 2 << CP0St_KSU;
1341 regs->cp0_epc = infop->entry;
1342 regs->regs[29] = infop->start_stack;
1343 }
1344
1345 /* See linux kernel: arch/mips/include/asm/elf.h. */
1346 #define ELF_NREG 45
1347 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1348
1349 /* See linux kernel: arch/mips/include/asm/reg.h. */
1350 enum {
1351 #ifdef TARGET_MIPS64
1352 TARGET_EF_R0 = 0,
1353 #else
1354 TARGET_EF_R0 = 6,
1355 #endif
1356 TARGET_EF_R26 = TARGET_EF_R0 + 26,
1357 TARGET_EF_R27 = TARGET_EF_R0 + 27,
1358 TARGET_EF_LO = TARGET_EF_R0 + 32,
1359 TARGET_EF_HI = TARGET_EF_R0 + 33,
1360 TARGET_EF_CP0_EPC = TARGET_EF_R0 + 34,
1361 TARGET_EF_CP0_BADVADDR = TARGET_EF_R0 + 35,
1362 TARGET_EF_CP0_STATUS = TARGET_EF_R0 + 36,
1363 TARGET_EF_CP0_CAUSE = TARGET_EF_R0 + 37
1364 };
1365
1366 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1367 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMIPSState *env)
1368 {
1369 int i;
1370
1371 for (i = 0; i < TARGET_EF_R0; i++) {
1372 (*regs)[i] = 0;
1373 }
1374 (*regs)[TARGET_EF_R0] = 0;
1375
1376 for (i = 1; i < ARRAY_SIZE(env->active_tc.gpr); i++) {
1377 (*regs)[TARGET_EF_R0 + i] = tswapreg(env->active_tc.gpr[i]);
1378 }
1379
1380 (*regs)[TARGET_EF_R26] = 0;
1381 (*regs)[TARGET_EF_R27] = 0;
1382 (*regs)[TARGET_EF_LO] = tswapreg(env->active_tc.LO[0]);
1383 (*regs)[TARGET_EF_HI] = tswapreg(env->active_tc.HI[0]);
1384 (*regs)[TARGET_EF_CP0_EPC] = tswapreg(env->active_tc.PC);
1385 (*regs)[TARGET_EF_CP0_BADVADDR] = tswapreg(env->CP0_BadVAddr);
1386 (*regs)[TARGET_EF_CP0_STATUS] = tswapreg(env->CP0_Status);
1387 (*regs)[TARGET_EF_CP0_CAUSE] = tswapreg(env->CP0_Cause);
1388 }
1389
1390 #define USE_ELF_CORE_DUMP
1391 #define ELF_EXEC_PAGESIZE 4096
1392
1393 /* See arch/mips/include/uapi/asm/hwcap.h. */
1394 enum {
1395 HWCAP_MIPS_R6 = (1 << 0),
1396 HWCAP_MIPS_MSA = (1 << 1),
1397 HWCAP_MIPS_CRC32 = (1 << 2),
1398 HWCAP_MIPS_MIPS16 = (1 << 3),
1399 HWCAP_MIPS_MDMX = (1 << 4),
1400 HWCAP_MIPS_MIPS3D = (1 << 5),
1401 HWCAP_MIPS_SMARTMIPS = (1 << 6),
1402 HWCAP_MIPS_DSP = (1 << 7),
1403 HWCAP_MIPS_DSP2 = (1 << 8),
1404 HWCAP_MIPS_DSP3 = (1 << 9),
1405 HWCAP_MIPS_MIPS16E2 = (1 << 10),
1406 HWCAP_LOONGSON_MMI = (1 << 11),
1407 HWCAP_LOONGSON_EXT = (1 << 12),
1408 HWCAP_LOONGSON_EXT2 = (1 << 13),
1409 HWCAP_LOONGSON_CPUCFG = (1 << 14),
1410 };
1411
1412 #define ELF_HWCAP get_elf_hwcap()
1413
1414 #define GET_FEATURE_INSN(_flag, _hwcap) \
1415 do { if (cpu->env.insn_flags & (_flag)) { hwcaps |= _hwcap; } } while (0)
1416
1417 #define GET_FEATURE_REG_SET(_reg, _mask, _hwcap) \
1418 do { if (cpu->env._reg & (_mask)) { hwcaps |= _hwcap; } } while (0)
1419
1420 #define GET_FEATURE_REG_EQU(_reg, _start, _length, _val, _hwcap) \
1421 do { \
1422 if (extract32(cpu->env._reg, (_start), (_length)) == (_val)) { \
1423 hwcaps |= _hwcap; \
1424 } \
1425 } while (0)
1426
1427 static uint32_t get_elf_hwcap(void)
1428 {
1429 MIPSCPU *cpu = MIPS_CPU(thread_cpu);
1430 uint32_t hwcaps = 0;
1431
1432 GET_FEATURE_REG_EQU(CP0_Config0, CP0C0_AR, CP0C0_AR_LENGTH,
1433 2, HWCAP_MIPS_R6);
1434 GET_FEATURE_REG_SET(CP0_Config3, 1 << CP0C3_MSAP, HWCAP_MIPS_MSA);
1435 GET_FEATURE_INSN(ASE_LMMI, HWCAP_LOONGSON_MMI);
1436 GET_FEATURE_INSN(ASE_LEXT, HWCAP_LOONGSON_EXT);
1437
1438 return hwcaps;
1439 }
1440
1441 #undef GET_FEATURE_REG_EQU
1442 #undef GET_FEATURE_REG_SET
1443 #undef GET_FEATURE_INSN
1444
1445 #endif /* TARGET_MIPS */
1446
1447 #ifdef TARGET_MICROBLAZE
1448
1449 #define elf_check_arch(x) ( (x) == EM_MICROBLAZE || (x) == EM_MICROBLAZE_OLD)
1450
1451 #define ELF_CLASS ELFCLASS32
1452 #define ELF_ARCH EM_MICROBLAZE
1453
1454 static inline void init_thread(struct target_pt_regs *regs,
1455 struct image_info *infop)
1456 {
1457 regs->pc = infop->entry;
1458 regs->r1 = infop->start_stack;
1459
1460 }
1461
1462 #define ELF_EXEC_PAGESIZE 4096
1463
1464 #define USE_ELF_CORE_DUMP
1465 #define ELF_NREG 38
1466 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1467
1468 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1469 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMBState *env)
1470 {
1471 int i, pos = 0;
1472
1473 for (i = 0; i < 32; i++) {
1474 (*regs)[pos++] = tswapreg(env->regs[i]);
1475 }
1476
1477 (*regs)[pos++] = tswapreg(env->pc);
1478 (*regs)[pos++] = tswapreg(mb_cpu_read_msr(env));
1479 (*regs)[pos++] = 0;
1480 (*regs)[pos++] = tswapreg(env->ear);
1481 (*regs)[pos++] = 0;
1482 (*regs)[pos++] = tswapreg(env->esr);
1483 }
1484
1485 #endif /* TARGET_MICROBLAZE */
1486
1487 #ifdef TARGET_NIOS2
1488
1489 #define elf_check_arch(x) ((x) == EM_ALTERA_NIOS2)
1490
1491 #define ELF_CLASS ELFCLASS32
1492 #define ELF_ARCH EM_ALTERA_NIOS2
1493
1494 static void init_thread(struct target_pt_regs *regs, struct image_info *infop)
1495 {
1496 regs->ea = infop->entry;
1497 regs->sp = infop->start_stack;
1498 }
1499
1500 #define LO_COMMPAGE TARGET_PAGE_SIZE
1501
1502 static bool init_guest_commpage(void)
1503 {
1504 static const uint8_t kuser_page[4 + 2 * 64] = {
1505 /* __kuser_helper_version */
1506 [0x00] = 0x02, 0x00, 0x00, 0x00,
1507
1508 /* __kuser_cmpxchg */
1509 [0x04] = 0x3a, 0x6c, 0x3b, 0x00, /* trap 16 */
1510 0x3a, 0x28, 0x00, 0xf8, /* ret */
1511
1512 /* __kuser_sigtramp */
1513 [0x44] = 0xc4, 0x22, 0x80, 0x00, /* movi r2, __NR_rt_sigreturn */
1514 0x3a, 0x68, 0x3b, 0x00, /* trap 0 */
1515 };
1516
1517 void *want = g2h_untagged(LO_COMMPAGE & -qemu_host_page_size);
1518 void *addr = mmap(want, qemu_host_page_size, PROT_READ | PROT_WRITE,
1519 MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
1520
1521 if (addr == MAP_FAILED) {
1522 perror("Allocating guest commpage");
1523 exit(EXIT_FAILURE);
1524 }
1525 if (addr != want) {
1526 return false;
1527 }
1528
1529 memcpy(addr, kuser_page, sizeof(kuser_page));
1530
1531 if (mprotect(addr, qemu_host_page_size, PROT_READ)) {
1532 perror("Protecting guest commpage");
1533 exit(EXIT_FAILURE);
1534 }
1535
1536 page_set_flags(LO_COMMPAGE, LO_COMMPAGE | ~TARGET_PAGE_MASK,
1537 PAGE_READ | PAGE_EXEC | PAGE_VALID);
1538 return true;
1539 }
1540
1541 #define ELF_EXEC_PAGESIZE 4096
1542
1543 #define USE_ELF_CORE_DUMP
1544 #define ELF_NREG 49
1545 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1546
1547 /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
1548 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1549 const CPUNios2State *env)
1550 {
1551 int i;
1552
1553 (*regs)[0] = -1;
1554 for (i = 1; i < 8; i++) /* r0-r7 */
1555 (*regs)[i] = tswapreg(env->regs[i + 7]);
1556
1557 for (i = 8; i < 16; i++) /* r8-r15 */
1558 (*regs)[i] = tswapreg(env->regs[i - 8]);
1559
1560 for (i = 16; i < 24; i++) /* r16-r23 */
1561 (*regs)[i] = tswapreg(env->regs[i + 7]);
1562 (*regs)[24] = -1; /* R_ET */
1563 (*regs)[25] = -1; /* R_BT */
1564 (*regs)[26] = tswapreg(env->regs[R_GP]);
1565 (*regs)[27] = tswapreg(env->regs[R_SP]);
1566 (*regs)[28] = tswapreg(env->regs[R_FP]);
1567 (*regs)[29] = tswapreg(env->regs[R_EA]);
1568 (*regs)[30] = -1; /* R_SSTATUS */
1569 (*regs)[31] = tswapreg(env->regs[R_RA]);
1570
1571 (*regs)[32] = tswapreg(env->pc);
1572
1573 (*regs)[33] = -1; /* R_STATUS */
1574 (*regs)[34] = tswapreg(env->regs[CR_ESTATUS]);
1575
1576 for (i = 35; i < 49; i++) /* ... */
1577 (*regs)[i] = -1;
1578 }
1579
1580 #endif /* TARGET_NIOS2 */
1581
1582 #ifdef TARGET_OPENRISC
1583
1584 #define ELF_ARCH EM_OPENRISC
1585 #define ELF_CLASS ELFCLASS32
1586 #define ELF_DATA ELFDATA2MSB
1587
1588 static inline void init_thread(struct target_pt_regs *regs,
1589 struct image_info *infop)
1590 {
1591 regs->pc = infop->entry;
1592 regs->gpr[1] = infop->start_stack;
1593 }
1594
1595 #define USE_ELF_CORE_DUMP
1596 #define ELF_EXEC_PAGESIZE 8192
1597
1598 /* See linux kernel arch/openrisc/include/asm/elf.h. */
1599 #define ELF_NREG 34 /* gprs and pc, sr */
1600 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1601
1602 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1603 const CPUOpenRISCState *env)
1604 {
1605 int i;
1606
1607 for (i = 0; i < 32; i++) {
1608 (*regs)[i] = tswapreg(cpu_get_gpr(env, i));
1609 }
1610 (*regs)[32] = tswapreg(env->pc);
1611 (*regs)[33] = tswapreg(cpu_get_sr(env));
1612 }
1613 #define ELF_HWCAP 0
1614 #define ELF_PLATFORM NULL
1615
1616 #endif /* TARGET_OPENRISC */
1617
1618 #ifdef TARGET_SH4
1619
1620 #define ELF_CLASS ELFCLASS32
1621 #define ELF_ARCH EM_SH
1622
1623 static inline void init_thread(struct target_pt_regs *regs,
1624 struct image_info *infop)
1625 {
1626 /* Check other registers XXXXX */
1627 regs->pc = infop->entry;
1628 regs->regs[15] = infop->start_stack;
1629 }
1630
1631 /* See linux kernel: arch/sh/include/asm/elf.h. */
1632 #define ELF_NREG 23
1633 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1634
1635 /* See linux kernel: arch/sh/include/asm/ptrace.h. */
1636 enum {
1637 TARGET_REG_PC = 16,
1638 TARGET_REG_PR = 17,
1639 TARGET_REG_SR = 18,
1640 TARGET_REG_GBR = 19,
1641 TARGET_REG_MACH = 20,
1642 TARGET_REG_MACL = 21,
1643 TARGET_REG_SYSCALL = 22
1644 };
1645
1646 static inline void elf_core_copy_regs(target_elf_gregset_t *regs,
1647 const CPUSH4State *env)
1648 {
1649 int i;
1650
1651 for (i = 0; i < 16; i++) {
1652 (*regs)[i] = tswapreg(env->gregs[i]);
1653 }
1654
1655 (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
1656 (*regs)[TARGET_REG_PR] = tswapreg(env->pr);
1657 (*regs)[TARGET_REG_SR] = tswapreg(env->sr);
1658 (*regs)[TARGET_REG_GBR] = tswapreg(env->gbr);
1659 (*regs)[TARGET_REG_MACH] = tswapreg(env->mach);
1660 (*regs)[TARGET_REG_MACL] = tswapreg(env->macl);
1661 (*regs)[TARGET_REG_SYSCALL] = 0; /* FIXME */
1662 }
1663
1664 #define USE_ELF_CORE_DUMP
1665 #define ELF_EXEC_PAGESIZE 4096
1666
1667 enum {
1668 SH_CPU_HAS_FPU = 0x0001, /* Hardware FPU support */
1669 SH_CPU_HAS_P2_FLUSH_BUG = 0x0002, /* Need to flush the cache in P2 area */
1670 SH_CPU_HAS_MMU_PAGE_ASSOC = 0x0004, /* SH3: TLB way selection bit support */
1671 SH_CPU_HAS_DSP = 0x0008, /* SH-DSP: DSP support */
1672 SH_CPU_HAS_PERF_COUNTER = 0x0010, /* Hardware performance counters */
1673 SH_CPU_HAS_PTEA = 0x0020, /* PTEA register */
1674 SH_CPU_HAS_LLSC = 0x0040, /* movli.l/movco.l */
1675 SH_CPU_HAS_L2_CACHE = 0x0080, /* Secondary cache / URAM */
1676 SH_CPU_HAS_OP32 = 0x0100, /* 32-bit instruction support */
1677 SH_CPU_HAS_PTEAEX = 0x0200, /* PTE ASID Extension support */
1678 };
1679
1680 #define ELF_HWCAP get_elf_hwcap()
1681
1682 static uint32_t get_elf_hwcap(void)
1683 {
1684 SuperHCPU *cpu = SUPERH_CPU(thread_cpu);
1685 uint32_t hwcap = 0;
1686
1687 hwcap |= SH_CPU_HAS_FPU;
1688
1689 if (cpu->env.features & SH_FEATURE_SH4A) {
1690 hwcap |= SH_CPU_HAS_LLSC;
1691 }
1692
1693 return hwcap;
1694 }
1695
1696 #endif
1697
1698 #ifdef TARGET_CRIS
1699
1700 #define ELF_CLASS ELFCLASS32
1701 #define ELF_ARCH EM_CRIS
1702
1703 static inline void init_thread(struct target_pt_regs *regs,
1704 struct image_info *infop)
1705 {
1706 regs->erp = infop->entry;
1707 }
1708
1709 #define ELF_EXEC_PAGESIZE 8192
1710
1711 #endif
1712
1713 #ifdef TARGET_M68K
1714
1715 #define ELF_CLASS ELFCLASS32
1716 #define ELF_ARCH EM_68K
1717
1718 /* ??? Does this need to do anything?
1719 #define ELF_PLAT_INIT(_r) */
1720
1721 static inline void init_thread(struct target_pt_regs *regs,
1722 struct image_info *infop)
1723 {
1724 regs->usp = infop->start_stack;
1725 regs->sr = 0;
1726 regs->pc = infop->entry;
1727 }
1728
1729 /* See linux kernel: arch/m68k/include/asm/elf.h. */
1730 #define ELF_NREG 20
1731 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1732
1733 static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUM68KState *env)
1734 {
1735 (*regs)[0] = tswapreg(env->dregs[1]);
1736 (*regs)[1] = tswapreg(env->dregs[2]);
1737 (*regs)[2] = tswapreg(env->dregs[3]);
1738 (*regs)[3] = tswapreg(env->dregs[4]);
1739 (*regs)[4] = tswapreg(env->dregs[5]);
1740 (*regs)[5] = tswapreg(env->dregs[6]);
1741 (*regs)[6] = tswapreg(env->dregs[7]);
1742 (*regs)[7] = tswapreg(env->aregs[0]);
1743 (*regs)[8] = tswapreg(env->aregs[1]);
1744 (*regs)[9] = tswapreg(env->aregs[2]);
1745 (*regs)[10] = tswapreg(env->aregs[3]);
1746 (*regs)[11] = tswapreg(env->aregs[4]);
1747 (*regs)[12] = tswapreg(env->aregs[5]);
1748 (*regs)[13] = tswapreg(env->aregs[6]);
1749 (*regs)[14] = tswapreg(env->dregs[0]);
1750 (*regs)[15] = tswapreg(env->aregs[7]);
1751 (*regs)[16] = tswapreg(env->dregs[0]); /* FIXME: orig_d0 */
1752 (*regs)[17] = tswapreg(env->sr);
1753 (*regs)[18] = tswapreg(env->pc);
1754 (*regs)[19] = 0; /* FIXME: regs->format | regs->vector */
1755 }
1756
1757 #define USE_ELF_CORE_DUMP
1758 #define ELF_EXEC_PAGESIZE 8192
1759
1760 #endif
1761
1762 #ifdef TARGET_ALPHA
1763
1764 #define ELF_CLASS ELFCLASS64
1765 #define ELF_ARCH EM_ALPHA
1766
1767 static inline void init_thread(struct target_pt_regs *regs,
1768 struct image_info *infop)
1769 {
1770 regs->pc = infop->entry;
1771 regs->ps = 8;
1772 regs->usp = infop->start_stack;
1773 }
1774
1775 #define ELF_EXEC_PAGESIZE 8192
1776
1777 #endif /* TARGET_ALPHA */
1778
1779 #ifdef TARGET_S390X
1780
1781 #define ELF_CLASS ELFCLASS64
1782 #define ELF_DATA ELFDATA2MSB
1783 #define ELF_ARCH EM_S390
1784
1785 #include "elf.h"
1786
1787 #define ELF_HWCAP get_elf_hwcap()
1788
1789 #define GET_FEATURE(_feat, _hwcap) \
1790 do { if (s390_has_feat(_feat)) { hwcap |= _hwcap; } } while (0)
1791
1792 uint32_t get_elf_hwcap(void)
1793 {
1794 /*
1795 * Let's assume we always have esan3 and zarch.
1796 * 31-bit processes can use 64-bit registers (high gprs).
1797 */
1798 uint32_t hwcap = HWCAP_S390_ESAN3 | HWCAP_S390_ZARCH | HWCAP_S390_HIGH_GPRS;
1799
1800 GET_FEATURE(S390_FEAT_STFLE, HWCAP_S390_STFLE);
1801 GET_FEATURE(S390_FEAT_MSA, HWCAP_S390_MSA);
1802 GET_FEATURE(S390_FEAT_LONG_DISPLACEMENT, HWCAP_S390_LDISP);
1803 GET_FEATURE(S390_FEAT_EXTENDED_IMMEDIATE, HWCAP_S390_EIMM);
1804 if (s390_has_feat(S390_FEAT_EXTENDED_TRANSLATION_3) &&
1805 s390_has_feat(S390_FEAT_ETF3_ENH)) {
1806 hwcap |= HWCAP_S390_ETF3EH;
1807 }
1808 GET_FEATURE(S390_FEAT_VECTOR, HWCAP_S390_VXRS);
1809 GET_FEATURE(S390_FEAT_VECTOR_ENH, HWCAP_S390_VXRS_EXT);
1810 GET_FEATURE(S390_FEAT_VECTOR_ENH2, HWCAP_S390_VXRS_EXT2);
1811
1812 return hwcap;
1813 }
1814
1815 const char *elf_hwcap_str(uint32_t bit)
1816 {
1817 static const char *hwcap_str[] = {
1818 [HWCAP_S390_NR_ESAN3] = "esan3",
1819 [HWCAP_S390_NR_ZARCH] = "zarch",
1820 [HWCAP_S390_NR_STFLE] = "stfle",
1821 [HWCAP_S390_NR_MSA] = "msa",
1822 [HWCAP_S390_NR_LDISP] = "ldisp",
1823 [HWCAP_S390_NR_EIMM] = "eimm",
1824 [HWCAP_S390_NR_DFP] = "dfp",
1825 [HWCAP_S390_NR_HPAGE] = "edat",
1826 [HWCAP_S390_NR_ETF3EH] = "etf3eh",
1827 [HWCAP_S390_NR_HIGH_GPRS] = "highgprs",
1828 [HWCAP_S390_NR_TE] = "te",
1829 [HWCAP_S390_NR_VXRS] = "vx",
1830 [HWCAP_S390_NR_VXRS_BCD] = "vxd",
1831 [HWCAP_S390_NR_VXRS_EXT] = "vxe",
1832 [HWCAP_S390_NR_GS] = "gs",
1833 [HWCAP_S390_NR_VXRS_EXT2] = "vxe2",
1834 [HWCAP_S390_NR_VXRS_PDE] = "vxp",
1835 [HWCAP_S390_NR_SORT] = "sort",
1836 [HWCAP_S390_NR_DFLT] = "dflt",
1837 [HWCAP_S390_NR_NNPA] = "nnpa",
1838 [HWCAP_S390_NR_PCI_MIO] = "pcimio",
1839 [HWCAP_S390_NR_SIE] = "sie",
1840 };
1841
1842 return bit < ARRAY_SIZE(hwcap_str) ? hwcap_str[bit] : NULL;
1843 }
1844
1845 static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
1846 {
1847 regs->psw.addr = infop->entry;
1848 regs->psw.mask = PSW_MASK_DAT | PSW_MASK_IO | PSW_MASK_EXT | \
1849 PSW_MASK_MCHECK | PSW_MASK_PSTATE | PSW_MASK_64 | \
1850 PSW_MASK_32;
1851 regs->gprs[15] = infop->start_stack;
1852 }
1853
1854 /* See linux kernel: arch/s390/include/uapi/asm/ptrace.h (s390_regs). */
1855 #define ELF_NREG 27
1856 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
1857
1858 enum {
1859 TARGET_REG_PSWM = 0,
1860 TARGET_REG_PSWA = 1,
1861 TARGET_REG_GPRS = 2,
1862 TARGET_REG_ARS = 18,
1863 TARGET_REG_ORIG_R2 = 26,
1864 };
1865
1866 static void elf_core_copy_regs(target_elf_gregset_t *regs,
1867 const CPUS390XState *env)
1868 {
1869 int i;
1870 uint32_t *aregs;
1871
1872 (*regs)[TARGET_REG_PSWM] = tswapreg(env->psw.mask);
1873 (*regs)[TARGET_REG_PSWA] = tswapreg(env->psw.addr);
1874 for (i = 0; i < 16; i++) {
1875 (*regs)[TARGET_REG_GPRS + i] = tswapreg(env->regs[i]);
1876 }
1877 aregs = (uint32_t *)&((*regs)[TARGET_REG_ARS]);
1878 for (i = 0; i < 16; i++) {
1879 aregs[i] = tswap32(env->aregs[i]);
1880 }
1881 (*regs)[TARGET_REG_ORIG_R2] = 0;
1882 }
1883
1884 #define USE_ELF_CORE_DUMP
1885 #define ELF_EXEC_PAGESIZE 4096
1886
1887 #endif /* TARGET_S390X */
1888
1889 #ifdef TARGET_RISCV
1890
1891 #define ELF_ARCH EM_RISCV
1892
1893 #ifdef TARGET_RISCV32
1894 #define ELF_CLASS ELFCLASS32
1895 #define VDSO_HEADER "vdso-32.c.inc"
1896 #else
1897 #define ELF_CLASS ELFCLASS64
1898 #define VDSO_HEADER "vdso-64.c.inc"
1899 #endif
1900
1901 #define ELF_HWCAP get_elf_hwcap()
1902
1903 static uint32_t get_elf_hwcap(void)
1904 {
1905 #define MISA_BIT(EXT) (1 << (EXT - 'A'))
1906 RISCVCPU *cpu = RISCV_CPU(thread_cpu);
1907 uint32_t mask = MISA_BIT('I') | MISA_BIT('M') | MISA_BIT('A')
1908 | MISA_BIT('F') | MISA_BIT('D') | MISA_BIT('C')
1909 | MISA_BIT('V');
1910
1911 return cpu->env.misa_ext & mask;
1912 #undef MISA_BIT
1913 }
1914
1915 static inline void init_thread(struct target_pt_regs *regs,
1916 struct image_info *infop)
1917 {
1918 regs->sepc = infop->entry;
1919 regs->sp = infop->start_stack;
1920 }
1921
1922 #define ELF_EXEC_PAGESIZE 4096
1923
1924 #endif /* TARGET_RISCV */
1925
1926 #ifdef TARGET_HPPA
1927
1928 #define ELF_CLASS ELFCLASS32
1929 #define ELF_ARCH EM_PARISC
1930 #define ELF_PLATFORM "PARISC"
1931 #define STACK_GROWS_DOWN 0
1932 #define STACK_ALIGNMENT 64
1933
1934 #define VDSO_HEADER "vdso.c.inc"
1935
1936 static inline void init_thread(struct target_pt_regs *regs,
1937 struct image_info *infop)
1938 {
1939 regs->iaoq[0] = infop->entry;
1940 regs->iaoq[1] = infop->entry + 4;
1941 regs->gr[23] = 0;
1942 regs->gr[24] = infop->argv;
1943 regs->gr[25] = infop->argc;
1944 /* The top-of-stack contains a linkage buffer. */
1945 regs->gr[30] = infop->start_stack + 64;
1946 regs->gr[31] = infop->entry;
1947 }
1948
1949 #define LO_COMMPAGE 0
1950
1951 static bool init_guest_commpage(void)
1952 {
1953 void *want = g2h_untagged(LO_COMMPAGE);
1954 void *addr = mmap(want, qemu_host_page_size, PROT_NONE,
1955 MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
1956
1957 if (addr == MAP_FAILED) {
1958 perror("Allocating guest commpage");
1959 exit(EXIT_FAILURE);
1960 }
1961 if (addr != want) {
1962 return false;
1963 }
1964
1965 /*
1966 * On Linux, page zero is normally marked execute only + gateway.
1967 * Normal read or write is supposed to fail (thus PROT_NONE above),
1968 * but specific offsets have kernel code mapped to raise permissions
1969 * and implement syscalls. Here, simply mark the page executable.
1970 * Special case the entry points during translation (see do_page_zero).
1971 */
1972 page_set_flags(LO_COMMPAGE, LO_COMMPAGE | ~TARGET_PAGE_MASK,
1973 PAGE_EXEC | PAGE_VALID);
1974 return true;
1975 }
1976
1977 #endif /* TARGET_HPPA */
1978
1979 #ifdef TARGET_XTENSA
1980
1981 #define ELF_CLASS ELFCLASS32
1982 #define ELF_ARCH EM_XTENSA
1983
1984 static inline void init_thread(struct target_pt_regs *regs,
1985 struct image_info *infop)
1986 {
1987 regs->windowbase = 0;
1988 regs->windowstart = 1;
1989 regs->areg[1] = infop->start_stack;
1990 regs->pc = infop->entry;
1991 if (info_is_fdpic(infop)) {
1992 regs->areg[4] = infop->loadmap_addr;
1993 regs->areg[5] = infop->interpreter_loadmap_addr;
1994 if (infop->interpreter_loadmap_addr) {
1995 regs->areg[6] = infop->interpreter_pt_dynamic_addr;
1996 } else {
1997 regs->areg[6] = infop->pt_dynamic_addr;
1998 }
1999 }
2000 }
2001
2002 /* See linux kernel: arch/xtensa/include/asm/elf.h. */
2003 #define ELF_NREG 128
2004 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
2005
2006 enum {
2007 TARGET_REG_PC,
2008 TARGET_REG_PS,
2009 TARGET_REG_LBEG,
2010 TARGET_REG_LEND,
2011 TARGET_REG_LCOUNT,
2012 TARGET_REG_SAR,
2013 TARGET_REG_WINDOWSTART,
2014 TARGET_REG_WINDOWBASE,
2015 TARGET_REG_THREADPTR,
2016 TARGET_REG_AR0 = 64,
2017 };
2018
2019 static void elf_core_copy_regs(target_elf_gregset_t *regs,
2020 const CPUXtensaState *env)
2021 {
2022 unsigned i;
2023
2024 (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
2025 (*regs)[TARGET_REG_PS] = tswapreg(env->sregs[PS] & ~PS_EXCM);
2026 (*regs)[TARGET_REG_LBEG] = tswapreg(env->sregs[LBEG]);
2027 (*regs)[TARGET_REG_LEND] = tswapreg(env->sregs[LEND]);
2028 (*regs)[TARGET_REG_LCOUNT] = tswapreg(env->sregs[LCOUNT]);
2029 (*regs)[TARGET_REG_SAR] = tswapreg(env->sregs[SAR]);
2030 (*regs)[TARGET_REG_WINDOWSTART] = tswapreg(env->sregs[WINDOW_START]);
2031 (*regs)[TARGET_REG_WINDOWBASE] = tswapreg(env->sregs[WINDOW_BASE]);
2032 (*regs)[TARGET_REG_THREADPTR] = tswapreg(env->uregs[THREADPTR]);
2033 xtensa_sync_phys_from_window((CPUXtensaState *)env);
2034 for (i = 0; i < env->config->nareg; ++i) {
2035 (*regs)[TARGET_REG_AR0 + i] = tswapreg(env->phys_regs[i]);
2036 }
2037 }
2038
2039 #define USE_ELF_CORE_DUMP
2040 #define ELF_EXEC_PAGESIZE 4096
2041
2042 #endif /* TARGET_XTENSA */
2043
2044 #ifdef TARGET_HEXAGON
2045
2046 #define ELF_CLASS ELFCLASS32
2047 #define ELF_ARCH EM_HEXAGON
2048
2049 static inline void init_thread(struct target_pt_regs *regs,
2050 struct image_info *infop)
2051 {
2052 regs->sepc = infop->entry;
2053 regs->sp = infop->start_stack;
2054 }
2055
2056 #endif /* TARGET_HEXAGON */
2057
2058 #ifndef ELF_BASE_PLATFORM
2059 #define ELF_BASE_PLATFORM (NULL)
2060 #endif
2061
2062 #ifndef ELF_PLATFORM
2063 #define ELF_PLATFORM (NULL)
2064 #endif
2065
2066 #ifndef ELF_MACHINE
2067 #define ELF_MACHINE ELF_ARCH
2068 #endif
2069
2070 #ifndef elf_check_arch
2071 #define elf_check_arch(x) ((x) == ELF_ARCH)
2072 #endif
2073
2074 #ifndef elf_check_abi
2075 #define elf_check_abi(x) (1)
2076 #endif
2077
2078 #ifndef ELF_HWCAP
2079 #define ELF_HWCAP 0
2080 #endif
2081
2082 #ifndef STACK_GROWS_DOWN
2083 #define STACK_GROWS_DOWN 1
2084 #endif
2085
2086 #ifndef STACK_ALIGNMENT
2087 #define STACK_ALIGNMENT 16
2088 #endif
2089
2090 #ifdef TARGET_ABI32
2091 #undef ELF_CLASS
2092 #define ELF_CLASS ELFCLASS32
2093 #undef bswaptls
2094 #define bswaptls(ptr) bswap32s(ptr)
2095 #endif
2096
2097 #ifndef EXSTACK_DEFAULT
2098 #define EXSTACK_DEFAULT false
2099 #endif
2100
2101 #include "elf.h"
2102
2103 /* We must delay the following stanzas until after "elf.h". */
2104 #if defined(TARGET_AARCH64)
2105
2106 static bool arch_parse_elf_property(uint32_t pr_type, uint32_t pr_datasz,
2107 const uint32_t *data,
2108 struct image_info *info,
2109 Error **errp)
2110 {
2111 if (pr_type == GNU_PROPERTY_AARCH64_FEATURE_1_AND) {
2112 if (pr_datasz != sizeof(uint32_t)) {
2113 error_setg(errp, "Ill-formed GNU_PROPERTY_AARCH64_FEATURE_1_AND");
2114 return false;
2115 }
2116 /* We will extract GNU_PROPERTY_AARCH64_FEATURE_1_BTI later. */
2117 info->note_flags = *data;
2118 }
2119 return true;
2120 }
2121 #define ARCH_USE_GNU_PROPERTY 1
2122
2123 #else
2124
2125 static bool arch_parse_elf_property(uint32_t pr_type, uint32_t pr_datasz,
2126 const uint32_t *data,
2127 struct image_info *info,
2128 Error **errp)
2129 {
2130 g_assert_not_reached();
2131 }
2132 #define ARCH_USE_GNU_PROPERTY 0
2133
2134 #endif
2135
2136 struct exec
2137 {
2138 unsigned int a_info; /* Use macros N_MAGIC, etc for access */
2139 unsigned int a_text; /* length of text, in bytes */
2140 unsigned int a_data; /* length of data, in bytes */
2141 unsigned int a_bss; /* length of uninitialized data area, in bytes */
2142 unsigned int a_syms; /* length of symbol table data in file, in bytes */
2143 unsigned int a_entry; /* start address */
2144 unsigned int a_trsize; /* length of relocation info for text, in bytes */
2145 unsigned int a_drsize; /* length of relocation info for data, in bytes */
2146 };
2147
2148
2149 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
2150 #define OMAGIC 0407
2151 #define NMAGIC 0410
2152 #define ZMAGIC 0413
2153 #define QMAGIC 0314
2154
2155 #define DLINFO_ITEMS 16
2156
2157 static inline void memcpy_fromfs(void * to, const void * from, unsigned long n)
2158 {
2159 memcpy(to, from, n);
2160 }
2161
2162 #ifdef BSWAP_NEEDED
2163 static void bswap_ehdr(struct elfhdr *ehdr)
2164 {
2165 bswap16s(&ehdr->e_type); /* Object file type */
2166 bswap16s(&ehdr->e_machine); /* Architecture */
2167 bswap32s(&ehdr->e_version); /* Object file version */
2168 bswaptls(&ehdr->e_entry); /* Entry point virtual address */
2169 bswaptls(&ehdr->e_phoff); /* Program header table file offset */
2170 bswaptls(&ehdr->e_shoff); /* Section header table file offset */
2171 bswap32s(&ehdr->e_flags); /* Processor-specific flags */
2172 bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
2173 bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
2174 bswap16s(&ehdr->e_phnum); /* Program header table entry count */
2175 bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
2176 bswap16s(&ehdr->e_shnum); /* Section header table entry count */
2177 bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
2178 }
2179
2180 static void bswap_phdr(struct elf_phdr *phdr, int phnum)
2181 {
2182 int i;
2183 for (i = 0; i < phnum; ++i, ++phdr) {
2184 bswap32s(&phdr->p_type); /* Segment type */
2185 bswap32s(&phdr->p_flags); /* Segment flags */
2186 bswaptls(&phdr->p_offset); /* Segment file offset */
2187 bswaptls(&phdr->p_vaddr); /* Segment virtual address */
2188 bswaptls(&phdr->p_paddr); /* Segment physical address */
2189 bswaptls(&phdr->p_filesz); /* Segment size in file */
2190 bswaptls(&phdr->p_memsz); /* Segment size in memory */
2191 bswaptls(&phdr->p_align); /* Segment alignment */
2192 }
2193 }
2194
2195 static void bswap_shdr(struct elf_shdr *shdr, int shnum)
2196 {
2197 int i;
2198 for (i = 0; i < shnum; ++i, ++shdr) {
2199 bswap32s(&shdr->sh_name);
2200 bswap32s(&shdr->sh_type);
2201 bswaptls(&shdr->sh_flags);
2202 bswaptls(&shdr->sh_addr);
2203 bswaptls(&shdr->sh_offset);
2204 bswaptls(&shdr->sh_size);
2205 bswap32s(&shdr->sh_link);
2206 bswap32s(&shdr->sh_info);
2207 bswaptls(&shdr->sh_addralign);
2208 bswaptls(&shdr->sh_entsize);
2209 }
2210 }
2211
2212 static void bswap_sym(struct elf_sym *sym)
2213 {
2214 bswap32s(&sym->st_name);
2215 bswaptls(&sym->st_value);
2216 bswaptls(&sym->st_size);
2217 bswap16s(&sym->st_shndx);
2218 }
2219
2220 #ifdef TARGET_MIPS
2221 static void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags)
2222 {
2223 bswap16s(&abiflags->version);
2224 bswap32s(&abiflags->ases);
2225 bswap32s(&abiflags->isa_ext);
2226 bswap32s(&abiflags->flags1);
2227 bswap32s(&abiflags->flags2);
2228 }
2229 #endif
2230 #else
2231 static inline void bswap_ehdr(struct elfhdr *ehdr) { }
2232 static inline void bswap_phdr(struct elf_phdr *phdr, int phnum) { }
2233 static inline void bswap_shdr(struct elf_shdr *shdr, int shnum) { }
2234 static inline void bswap_sym(struct elf_sym *sym) { }
2235 #ifdef TARGET_MIPS
2236 static inline void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags) { }
2237 #endif
2238 #endif
2239
2240 #ifdef USE_ELF_CORE_DUMP
2241 static int elf_core_dump(int, const CPUArchState *);
2242 #endif /* USE_ELF_CORE_DUMP */
2243 static void load_symbols(struct elfhdr *hdr, const ImageSource *src,
2244 abi_ulong load_bias);
2245
2246 /* Verify the portions of EHDR within E_IDENT for the target.
2247 This can be performed before bswapping the entire header. */
2248 static bool elf_check_ident(struct elfhdr *ehdr)
2249 {
2250 return (ehdr->e_ident[EI_MAG0] == ELFMAG0
2251 && ehdr->e_ident[EI_MAG1] == ELFMAG1
2252 && ehdr->e_ident[EI_MAG2] == ELFMAG2
2253 && ehdr->e_ident[EI_MAG3] == ELFMAG3
2254 && ehdr->e_ident[EI_CLASS] == ELF_CLASS
2255 && ehdr->e_ident[EI_DATA] == ELF_DATA
2256 && ehdr->e_ident[EI_VERSION] == EV_CURRENT);
2257 }
2258
2259 /* Verify the portions of EHDR outside of E_IDENT for the target.
2260 This has to wait until after bswapping the header. */
2261 static bool elf_check_ehdr(struct elfhdr *ehdr)
2262 {
2263 return (elf_check_arch(ehdr->e_machine)
2264 && elf_check_abi(ehdr->e_flags)
2265 && ehdr->e_ehsize == sizeof(struct elfhdr)
2266 && ehdr->e_phentsize == sizeof(struct elf_phdr)
2267 && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN));
2268 }
2269
2270 /*
2271 * 'copy_elf_strings()' copies argument/envelope strings from user
2272 * memory to free pages in kernel mem. These are in a format ready
2273 * to be put directly into the top of new user memory.
2274 *
2275 */
2276 static abi_ulong copy_elf_strings(int argc, char **argv, char *scratch,
2277 abi_ulong p, abi_ulong stack_limit)
2278 {
2279 char *tmp;
2280 int len, i;
2281 abi_ulong top = p;
2282
2283 if (!p) {
2284 return 0; /* bullet-proofing */
2285 }
2286
2287 if (STACK_GROWS_DOWN) {
2288 int offset = ((p - 1) % TARGET_PAGE_SIZE) + 1;
2289 for (i = argc - 1; i >= 0; --i) {
2290 tmp = argv[i];
2291 if (!tmp) {
2292 fprintf(stderr, "VFS: argc is wrong");
2293 exit(-1);
2294 }
2295 len = strlen(tmp) + 1;
2296 tmp += len;
2297
2298 if (len > (p - stack_limit)) {
2299 return 0;
2300 }
2301 while (len) {
2302 int bytes_to_copy = (len > offset) ? offset : len;
2303 tmp -= bytes_to_copy;
2304 p -= bytes_to_copy;
2305 offset -= bytes_to_copy;
2306 len -= bytes_to_copy;
2307
2308 memcpy_fromfs(scratch + offset, tmp, bytes_to_copy);
2309
2310 if (offset == 0) {
2311 memcpy_to_target(p, scratch, top - p);
2312 top = p;
2313 offset = TARGET_PAGE_SIZE;
2314 }
2315 }
2316 }
2317 if (p != top) {
2318 memcpy_to_target(p, scratch + offset, top - p);
2319 }
2320 } else {
2321 int remaining = TARGET_PAGE_SIZE - (p % TARGET_PAGE_SIZE);
2322 for (i = 0; i < argc; ++i) {
2323 tmp = argv[i];
2324 if (!tmp) {
2325 fprintf(stderr, "VFS: argc is wrong");
2326 exit(-1);
2327 }
2328 len = strlen(tmp) + 1;
2329 if (len > (stack_limit - p)) {
2330 return 0;
2331 }
2332 while (len) {
2333 int bytes_to_copy = (len > remaining) ? remaining : len;
2334
2335 memcpy_fromfs(scratch + (p - top), tmp, bytes_to_copy);
2336
2337 tmp += bytes_to_copy;
2338 remaining -= bytes_to_copy;
2339 p += bytes_to_copy;
2340 len -= bytes_to_copy;
2341
2342 if (remaining == 0) {
2343 memcpy_to_target(top, scratch, p - top);
2344 top = p;
2345 remaining = TARGET_PAGE_SIZE;
2346 }
2347 }
2348 }
2349 if (p != top) {
2350 memcpy_to_target(top, scratch, p - top);
2351 }
2352 }
2353
2354 return p;
2355 }
2356
2357 /* Older linux kernels provide up to MAX_ARG_PAGES (default: 32) of
2358 * argument/environment space. Newer kernels (>2.6.33) allow more,
2359 * dependent on stack size, but guarantee at least 32 pages for
2360 * backwards compatibility.
2361 */
2362 #define STACK_LOWER_LIMIT (32 * TARGET_PAGE_SIZE)
2363
2364 static abi_ulong setup_arg_pages(struct linux_binprm *bprm,
2365 struct image_info *info)
2366 {
2367 abi_ulong size, error, guard;
2368 int prot;
2369
2370 size = guest_stack_size;
2371 if (size < STACK_LOWER_LIMIT) {
2372 size = STACK_LOWER_LIMIT;
2373 }
2374
2375 if (STACK_GROWS_DOWN) {
2376 guard = TARGET_PAGE_SIZE;
2377 if (guard < qemu_real_host_page_size()) {
2378 guard = qemu_real_host_page_size();
2379 }
2380 } else {
2381 /* no guard page for hppa target where stack grows upwards. */
2382 guard = 0;
2383 }
2384
2385 prot = PROT_READ | PROT_WRITE;
2386 if (info->exec_stack) {
2387 prot |= PROT_EXEC;
2388 }
2389 error = target_mmap(0, size + guard, prot,
2390 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
2391 if (error == -1) {
2392 perror("mmap stack");
2393 exit(-1);
2394 }
2395
2396 /* We reserve one extra page at the top of the stack as guard. */
2397 if (STACK_GROWS_DOWN) {
2398 target_mprotect(error, guard, PROT_NONE);
2399 info->stack_limit = error + guard;
2400 return info->stack_limit + size - sizeof(void *);
2401 } else {
2402 info->stack_limit = error + size;
2403 return error;
2404 }
2405 }
2406
2407 /**
2408 * zero_bss:
2409 *
2410 * Map and zero the bss. We need to explicitly zero any fractional pages
2411 * after the data section (i.e. bss). Return false on mapping failure.
2412 */
2413 static bool zero_bss(abi_ulong start_bss, abi_ulong end_bss,
2414 int prot, Error **errp)
2415 {
2416 abi_ulong align_bss;
2417
2418 /* We only expect writable bss; the code segment shouldn't need this. */
2419 if (!(prot & PROT_WRITE)) {
2420 error_setg(errp, "PT_LOAD with non-writable bss");
2421 return false;
2422 }
2423
2424 align_bss = TARGET_PAGE_ALIGN(start_bss);
2425 end_bss = TARGET_PAGE_ALIGN(end_bss);
2426
2427 if (start_bss < align_bss) {
2428 int flags = page_get_flags(start_bss);
2429
2430 if (!(flags & PAGE_BITS)) {
2431 /*
2432 * The whole address space of the executable was reserved
2433 * at the start, therefore all pages will be VALID.
2434 * But assuming there are no PROT_NONE PT_LOAD segments,
2435 * a PROT_NONE page means no data all bss, and we can
2436 * simply extend the new anon mapping back to the start
2437 * of the page of bss.
2438 */
2439 align_bss -= TARGET_PAGE_SIZE;
2440 } else {
2441 /*
2442 * The start of the bss shares a page with something.
2443 * The only thing that we expect is the data section,
2444 * which would already be marked writable.
2445 * Overlapping the RX code segment seems malformed.
2446 */
2447 if (!(flags & PAGE_WRITE)) {
2448 error_setg(errp, "PT_LOAD with bss overlapping "
2449 "non-writable page");
2450 return false;
2451 }
2452
2453 /* The page is already mapped and writable. */
2454 memset(g2h_untagged(start_bss), 0, align_bss - start_bss);
2455 }
2456 }
2457
2458 if (align_bss < end_bss &&
2459 target_mmap(align_bss, end_bss - align_bss, prot,
2460 MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0) == -1) {
2461 error_setg_errno(errp, errno, "Error mapping bss");
2462 return false;
2463 }
2464 return true;
2465 }
2466
2467 #if defined(TARGET_ARM)
2468 static int elf_is_fdpic(struct elfhdr *exec)
2469 {
2470 return exec->e_ident[EI_OSABI] == ELFOSABI_ARM_FDPIC;
2471 }
2472 #elif defined(TARGET_XTENSA)
2473 static int elf_is_fdpic(struct elfhdr *exec)
2474 {
2475 return exec->e_ident[EI_OSABI] == ELFOSABI_XTENSA_FDPIC;
2476 }
2477 #else
2478 /* Default implementation, always false. */
2479 static int elf_is_fdpic(struct elfhdr *exec)
2480 {
2481 return 0;
2482 }
2483 #endif
2484
2485 static abi_ulong loader_build_fdpic_loadmap(struct image_info *info, abi_ulong sp)
2486 {
2487 uint16_t n;
2488 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs;
2489
2490 /* elf32_fdpic_loadseg */
2491 n = info->nsegs;
2492 while (n--) {
2493 sp -= 12;
2494 put_user_u32(loadsegs[n].addr, sp+0);
2495 put_user_u32(loadsegs[n].p_vaddr, sp+4);
2496 put_user_u32(loadsegs[n].p_memsz, sp+8);
2497 }
2498
2499 /* elf32_fdpic_loadmap */
2500 sp -= 4;
2501 put_user_u16(0, sp+0); /* version */
2502 put_user_u16(info->nsegs, sp+2); /* nsegs */
2503
2504 info->personality = PER_LINUX_FDPIC;
2505 info->loadmap_addr = sp;
2506
2507 return sp;
2508 }
2509
2510 static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
2511 struct elfhdr *exec,
2512 struct image_info *info,
2513 struct image_info *interp_info,
2514 struct image_info *vdso_info)
2515 {
2516 abi_ulong sp;
2517 abi_ulong u_argc, u_argv, u_envp, u_auxv;
2518 int size;
2519 int i;
2520 abi_ulong u_rand_bytes;
2521 uint8_t k_rand_bytes[16];
2522 abi_ulong u_platform, u_base_platform;
2523 const char *k_platform, *k_base_platform;
2524 const int n = sizeof(elf_addr_t);
2525
2526 sp = p;
2527
2528 /* Needs to be before we load the env/argc/... */
2529 if (elf_is_fdpic(exec)) {
2530 /* Need 4 byte alignment for these structs */
2531 sp &= ~3;
2532 sp = loader_build_fdpic_loadmap(info, sp);
2533 info->other_info = interp_info;
2534 if (interp_info) {
2535 interp_info->other_info = info;
2536 sp = loader_build_fdpic_loadmap(interp_info, sp);
2537 info->interpreter_loadmap_addr = interp_info->loadmap_addr;
2538 info->interpreter_pt_dynamic_addr = interp_info->pt_dynamic_addr;
2539 } else {
2540 info->interpreter_loadmap_addr = 0;
2541 info->interpreter_pt_dynamic_addr = 0;
2542 }
2543 }
2544
2545 u_base_platform = 0;
2546 k_base_platform = ELF_BASE_PLATFORM;
2547 if (k_base_platform) {
2548 size_t len = strlen(k_base_platform) + 1;
2549 if (STACK_GROWS_DOWN) {
2550 sp -= (len + n - 1) & ~(n - 1);
2551 u_base_platform = sp;
2552 /* FIXME - check return value of memcpy_to_target() for failure */
2553 memcpy_to_target(sp, k_base_platform, len);
2554 } else {
2555 memcpy_to_target(sp, k_base_platform, len);
2556 u_base_platform = sp;
2557 sp += len + 1;
2558 }
2559 }
2560
2561 u_platform = 0;
2562 k_platform = ELF_PLATFORM;
2563 if (k_platform) {
2564 size_t len = strlen(k_platform) + 1;
2565 if (STACK_GROWS_DOWN) {
2566 sp -= (len + n - 1) & ~(n - 1);
2567 u_platform = sp;
2568 /* FIXME - check return value of memcpy_to_target() for failure */
2569 memcpy_to_target(sp, k_platform, len);
2570 } else {
2571 memcpy_to_target(sp, k_platform, len);
2572 u_platform = sp;
2573 sp += len + 1;
2574 }
2575 }
2576
2577 /* Provide 16 byte alignment for the PRNG, and basic alignment for
2578 * the argv and envp pointers.
2579 */
2580 if (STACK_GROWS_DOWN) {
2581 sp = QEMU_ALIGN_DOWN(sp, 16);
2582 } else {
2583 sp = QEMU_ALIGN_UP(sp, 16);
2584 }
2585
2586 /*
2587 * Generate 16 random bytes for userspace PRNG seeding.
2588 */
2589 qemu_guest_getrandom_nofail(k_rand_bytes, sizeof(k_rand_bytes));
2590 if (STACK_GROWS_DOWN) {
2591 sp -= 16;
2592 u_rand_bytes = sp;
2593 /* FIXME - check return value of memcpy_to_target() for failure */
2594 memcpy_to_target(sp, k_rand_bytes, 16);
2595 } else {
2596 memcpy_to_target(sp, k_rand_bytes, 16);
2597 u_rand_bytes = sp;
2598 sp += 16;
2599 }
2600
2601 size = (DLINFO_ITEMS + 1) * 2;
2602 if (k_base_platform) {
2603 size += 2;
2604 }
2605 if (k_platform) {
2606 size += 2;
2607 }
2608 if (vdso_info) {
2609 size += 2;
2610 }
2611 #ifdef DLINFO_ARCH_ITEMS
2612 size += DLINFO_ARCH_ITEMS * 2;
2613 #endif
2614 #ifdef ELF_HWCAP2
2615 size += 2;
2616 #endif
2617 info->auxv_len = size * n;
2618
2619 size += envc + argc + 2;
2620 size += 1; /* argc itself */
2621 size *= n;
2622
2623 /* Allocate space and finalize stack alignment for entry now. */
2624 if (STACK_GROWS_DOWN) {
2625 u_argc = QEMU_ALIGN_DOWN(sp - size, STACK_ALIGNMENT);
2626 sp = u_argc;
2627 } else {
2628 u_argc = sp;
2629 sp = QEMU_ALIGN_UP(sp + size, STACK_ALIGNMENT);
2630 }
2631
2632 u_argv = u_argc + n;
2633 u_envp = u_argv + (argc + 1) * n;
2634 u_auxv = u_envp + (envc + 1) * n;
2635 info->saved_auxv = u_auxv;
2636 info->argc = argc;
2637 info->envc = envc;
2638 info->argv = u_argv;
2639 info->envp = u_envp;
2640
2641 /* This is correct because Linux defines
2642 * elf_addr_t as Elf32_Off / Elf64_Off
2643 */
2644 #define NEW_AUX_ENT(id, val) do { \
2645 put_user_ual(id, u_auxv); u_auxv += n; \
2646 put_user_ual(val, u_auxv); u_auxv += n; \
2647 } while(0)
2648
2649 #ifdef ARCH_DLINFO
2650 /*
2651 * ARCH_DLINFO must come first so platform specific code can enforce
2652 * special alignment requirements on the AUXV if necessary (eg. PPC).
2653 */
2654 ARCH_DLINFO;
2655 #endif
2656 /* There must be exactly DLINFO_ITEMS entries here, or the assert
2657 * on info->auxv_len will trigger.
2658 */
2659 NEW_AUX_ENT(AT_PHDR, (abi_ulong)(info->load_addr + exec->e_phoff));
2660 NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr)));
2661 NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
2662 if ((info->alignment & ~qemu_host_page_mask) != 0) {
2663 /* Target doesn't support host page size alignment */
2664 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE));
2665 } else {
2666 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(MAX(TARGET_PAGE_SIZE,
2667 qemu_host_page_size)));
2668 }
2669 NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_info ? interp_info->load_addr : 0));
2670 NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
2671 NEW_AUX_ENT(AT_ENTRY, info->entry);
2672 NEW_AUX_ENT(AT_UID, (abi_ulong) getuid());
2673 NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid());
2674 NEW_AUX_ENT(AT_GID, (abi_ulong) getgid());
2675 NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid());
2676 NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP);
2677 NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK));
2678 NEW_AUX_ENT(AT_RANDOM, (abi_ulong) u_rand_bytes);
2679 NEW_AUX_ENT(AT_SECURE, (abi_ulong) qemu_getauxval(AT_SECURE));
2680 NEW_AUX_ENT(AT_EXECFN, info->file_string);
2681
2682 #ifdef ELF_HWCAP2
2683 NEW_AUX_ENT(AT_HWCAP2, (abi_ulong) ELF_HWCAP2);
2684 #endif
2685
2686 if (u_base_platform) {
2687 NEW_AUX_ENT(AT_BASE_PLATFORM, u_base_platform);
2688 }
2689 if (u_platform) {
2690 NEW_AUX_ENT(AT_PLATFORM, u_platform);
2691 }
2692 if (vdso_info) {
2693 NEW_AUX_ENT(AT_SYSINFO_EHDR, vdso_info->load_addr);
2694 }
2695 NEW_AUX_ENT (AT_NULL, 0);
2696 #undef NEW_AUX_ENT
2697
2698 /* Check that our initial calculation of the auxv length matches how much
2699 * we actually put into it.
2700 */
2701 assert(info->auxv_len == u_auxv - info->saved_auxv);
2702
2703 put_user_ual(argc, u_argc);
2704
2705 p = info->arg_strings;
2706 for (i = 0; i < argc; ++i) {
2707 put_user_ual(p, u_argv);
2708 u_argv += n;
2709 p += target_strlen(p) + 1;
2710 }
2711 put_user_ual(0, u_argv);
2712
2713 p = info->env_strings;
2714 for (i = 0; i < envc; ++i) {
2715 put_user_ual(p, u_envp);
2716 u_envp += n;
2717 p += target_strlen(p) + 1;
2718 }
2719 put_user_ual(0, u_envp);
2720
2721 return sp;
2722 }
2723
2724 #if defined(HI_COMMPAGE)
2725 #define LO_COMMPAGE -1
2726 #elif defined(LO_COMMPAGE)
2727 #define HI_COMMPAGE 0
2728 #else
2729 #define HI_COMMPAGE 0
2730 #define LO_COMMPAGE -1
2731 #ifndef INIT_GUEST_COMMPAGE
2732 #define init_guest_commpage() true
2733 #endif
2734 #endif
2735
2736 /**
2737 * pgb_try_mmap:
2738 * @addr: host start address
2739 * @addr_last: host last address
2740 * @keep: do not unmap the probe region
2741 *
2742 * Return 1 if [@addr, @addr_last] is not mapped in the host,
2743 * return 0 if it is not available to map, and -1 on mmap error.
2744 * If @keep, the region is left mapped on success, otherwise unmapped.
2745 */
2746 static int pgb_try_mmap(uintptr_t addr, uintptr_t addr_last, bool keep)
2747 {
2748 size_t size = addr_last - addr + 1;
2749 void *p = mmap((void *)addr, size, PROT_NONE,
2750 MAP_ANONYMOUS | MAP_PRIVATE |
2751 MAP_NORESERVE | MAP_FIXED_NOREPLACE, -1, 0);
2752 int ret;
2753
2754 if (p == MAP_FAILED) {
2755 return errno == EEXIST ? 0 : -1;
2756 }
2757 ret = p == (void *)addr;
2758 if (!keep || !ret) {
2759 munmap(p, size);
2760 }
2761 return ret;
2762 }
2763
2764 /**
2765 * pgb_try_mmap_skip_brk(uintptr_t addr, uintptr_t size, uintptr_t brk)
2766 * @addr: host address
2767 * @addr_last: host last address
2768 * @brk: host brk
2769 *
2770 * Like pgb_try_mmap, but additionally reserve some memory following brk.
2771 */
2772 static int pgb_try_mmap_skip_brk(uintptr_t addr, uintptr_t addr_last,
2773 uintptr_t brk, bool keep)
2774 {
2775 uintptr_t brk_last = brk + 16 * MiB - 1;
2776
2777 /* Do not map anything close to the host brk. */
2778 if (addr <= brk_last && brk <= addr_last) {
2779 return 0;
2780 }
2781 return pgb_try_mmap(addr, addr_last, keep);
2782 }
2783
2784 /**
2785 * pgb_try_mmap_set:
2786 * @ga: set of guest addrs
2787 * @base: guest_base
2788 * @brk: host brk
2789 *
2790 * Return true if all @ga can be mapped by the host at @base.
2791 * On success, retain the mapping at index 0 for reserved_va.
2792 */
2793
2794 typedef struct PGBAddrs {
2795 uintptr_t bounds[3][2]; /* start/last pairs */
2796 int nbounds;
2797 } PGBAddrs;
2798
2799 static bool pgb_try_mmap_set(const PGBAddrs *ga, uintptr_t base, uintptr_t brk)
2800 {
2801 for (int i = ga->nbounds - 1; i >= 0; --i) {
2802 if (pgb_try_mmap_skip_brk(ga->bounds[i][0] + base,
2803 ga->bounds[i][1] + base,
2804 brk, i == 0 && reserved_va) <= 0) {
2805 return false;
2806 }
2807 }
2808 return true;
2809 }
2810
2811 /**
2812 * pgb_addr_set:
2813 * @ga: output set of guest addrs
2814 * @guest_loaddr: guest image low address
2815 * @guest_loaddr: guest image high address
2816 * @identity: create for identity mapping
2817 *
2818 * Fill in @ga with the image, COMMPAGE and NULL page.
2819 */
2820 static bool pgb_addr_set(PGBAddrs *ga, abi_ulong guest_loaddr,
2821 abi_ulong guest_hiaddr, bool try_identity)
2822 {
2823 int n;
2824
2825 /*
2826 * With a low commpage, or a guest mapped very low,
2827 * we may not be able to use the identity map.
2828 */
2829 if (try_identity) {
2830 if (LO_COMMPAGE != -1 && LO_COMMPAGE < mmap_min_addr) {
2831 return false;
2832 }
2833 if (guest_loaddr != 0 && guest_loaddr < mmap_min_addr) {
2834 return false;
2835 }
2836 }
2837
2838 memset(ga, 0, sizeof(*ga));
2839 n = 0;
2840
2841 if (reserved_va) {
2842 ga->bounds[n][0] = try_identity ? mmap_min_addr : 0;
2843 ga->bounds[n][1] = reserved_va;
2844 n++;
2845 /* LO_COMMPAGE and NULL handled by reserving from 0. */
2846 } else {
2847 /* Add any LO_COMMPAGE or NULL page. */
2848 if (LO_COMMPAGE != -1) {
2849 ga->bounds[n][0] = 0;
2850 ga->bounds[n][1] = LO_COMMPAGE + TARGET_PAGE_SIZE - 1;
2851 n++;
2852 } else if (!try_identity) {
2853 ga->bounds[n][0] = 0;
2854 ga->bounds[n][1] = TARGET_PAGE_SIZE - 1;
2855 n++;
2856 }
2857
2858 /* Add the guest image for ET_EXEC. */
2859 if (guest_loaddr) {
2860 ga->bounds[n][0] = guest_loaddr;
2861 ga->bounds[n][1] = guest_hiaddr;
2862 n++;
2863 }
2864 }
2865
2866 /*
2867 * Temporarily disable
2868 * "comparison is always false due to limited range of data type"
2869 * due to comparison between unsigned and (possible) 0.
2870 */
2871 #pragma GCC diagnostic push
2872 #pragma GCC diagnostic ignored "-Wtype-limits"
2873
2874 /* Add any HI_COMMPAGE not covered by reserved_va. */
2875 if (reserved_va < HI_COMMPAGE) {
2876 ga->bounds[n][0] = HI_COMMPAGE & qemu_host_page_mask;
2877 ga->bounds[n][1] = HI_COMMPAGE + TARGET_PAGE_SIZE - 1;
2878 n++;
2879 }
2880
2881 #pragma GCC diagnostic pop
2882
2883 ga->nbounds = n;
2884 return true;
2885 }
2886
2887 static void pgb_fail_in_use(const char *image_name)
2888 {
2889 error_report("%s: requires virtual address space that is in use "
2890 "(omit the -B option or choose a different value)",
2891 image_name);
2892 exit(EXIT_FAILURE);
2893 }
2894
2895 static void pgb_fixed(const char *image_name, uintptr_t guest_loaddr,
2896 uintptr_t guest_hiaddr, uintptr_t align)
2897 {
2898 PGBAddrs ga;
2899 uintptr_t brk = (uintptr_t)sbrk(0);
2900
2901 if (!QEMU_IS_ALIGNED(guest_base, align)) {
2902 fprintf(stderr, "Requested guest base %p does not satisfy "
2903 "host minimum alignment (0x%" PRIxPTR ")\n",
2904 (void *)guest_base, align);
2905 exit(EXIT_FAILURE);
2906 }
2907
2908 if (!pgb_addr_set(&ga, guest_loaddr, guest_hiaddr, !guest_base)
2909 || !pgb_try_mmap_set(&ga, guest_base, brk)) {
2910 pgb_fail_in_use(image_name);
2911 }
2912 }
2913
2914 /**
2915 * pgb_find_fallback:
2916 *
2917 * This is a fallback method for finding holes in the host address space
2918 * if we don't have the benefit of being able to access /proc/self/map.
2919 * It can potentially take a very long time as we can only dumbly iterate
2920 * up the host address space seeing if the allocation would work.
2921 */
2922 static uintptr_t pgb_find_fallback(const PGBAddrs *ga, uintptr_t align,
2923 uintptr_t brk)
2924 {
2925 /* TODO: come up with a better estimate of how much to skip. */
2926 uintptr_t skip = sizeof(uintptr_t) == 4 ? MiB : GiB;
2927
2928 for (uintptr_t base = skip; ; base += skip) {
2929 base = ROUND_UP(base, align);
2930 if (pgb_try_mmap_set(ga, base, brk)) {
2931 return base;
2932 }
2933 if (base >= -skip) {
2934 return -1;
2935 }
2936 }
2937 }
2938
2939 static uintptr_t pgb_try_itree(const PGBAddrs *ga, uintptr_t base,
2940 IntervalTreeRoot *root)
2941 {
2942 for (int i = ga->nbounds - 1; i >= 0; --i) {
2943 uintptr_t s = base + ga->bounds[i][0];
2944 uintptr_t l = base + ga->bounds[i][1];
2945 IntervalTreeNode *n;
2946
2947 if (l < s) {
2948 /* Wraparound. Skip to advance S to mmap_min_addr. */
2949 return mmap_min_addr - s;
2950 }
2951
2952 n = interval_tree_iter_first(root, s, l);
2953 if (n != NULL) {
2954 /* Conflict. Skip to advance S to LAST + 1. */
2955 return n->last - s + 1;
2956 }
2957 }
2958 return 0; /* success */
2959 }
2960
2961 static uintptr_t pgb_find_itree(const PGBAddrs *ga, IntervalTreeRoot *root,
2962 uintptr_t align, uintptr_t brk)
2963 {
2964 uintptr_t last = mmap_min_addr;
2965 uintptr_t base, skip;
2966
2967 while (true) {
2968 base = ROUND_UP(last, align);
2969 if (base < last) {
2970 return -1;
2971 }
2972
2973 skip = pgb_try_itree(ga, base, root);
2974 if (skip == 0) {
2975 break;
2976 }
2977
2978 last = base + skip;
2979 if (last < base) {
2980 return -1;
2981 }
2982 }
2983
2984 /*
2985 * We've chosen 'base' based on holes in the interval tree,
2986 * but we don't yet know if it is a valid host address.
2987 * Because it is the first matching hole, if the host addresses
2988 * are invalid we know there are no further matches.
2989 */
2990 return pgb_try_mmap_set(ga, base, brk) ? base : -1;
2991 }
2992
2993 static void pgb_dynamic(const char *image_name, uintptr_t guest_loaddr,
2994 uintptr_t guest_hiaddr, uintptr_t align)
2995 {
2996 IntervalTreeRoot *root;
2997 uintptr_t brk, ret;
2998 PGBAddrs ga;
2999
3000 assert(QEMU_IS_ALIGNED(guest_loaddr, align));
3001
3002 /* Try the identity map first. */
3003 if (pgb_addr_set(&ga, guest_loaddr, guest_hiaddr, true)) {
3004 brk = (uintptr_t)sbrk(0);
3005 if (pgb_try_mmap_set(&ga, 0, brk)) {
3006 guest_base = 0;
3007 return;
3008 }
3009 }
3010
3011 /*
3012 * Rebuild the address set for non-identity map.
3013 * This differs in the mapping of the guest NULL page.
3014 */
3015 pgb_addr_set(&ga, guest_loaddr, guest_hiaddr, false);
3016
3017 root = read_self_maps();
3018
3019 /* Read brk after we've read the maps, which will malloc. */
3020 brk = (uintptr_t)sbrk(0);
3021
3022 if (!root) {
3023 ret = pgb_find_fallback(&ga, align, brk);
3024 } else {
3025 /*
3026 * Reserve the area close to the host brk.
3027 * This will be freed with the rest of the tree.
3028 */
3029 IntervalTreeNode *b = g_new0(IntervalTreeNode, 1);
3030 b->start = brk;
3031 b->last = brk + 16 * MiB - 1;
3032 interval_tree_insert(b, root);
3033
3034 ret = pgb_find_itree(&ga, root, align, brk);
3035 free_self_maps(root);
3036 }
3037
3038 if (ret == -1) {
3039 int w = TARGET_LONG_BITS / 4;
3040
3041 error_report("%s: Unable to find a guest_base to satisfy all "
3042 "guest address mapping requirements", image_name);
3043
3044 for (int i = 0; i < ga.nbounds; ++i) {
3045 error_printf(" %0*" PRIx64 "-%0*" PRIx64 "\n",
3046 w, (uint64_t)ga.bounds[i][0],
3047 w, (uint64_t)ga.bounds[i][1]);
3048 }
3049 exit(EXIT_FAILURE);
3050 }
3051 guest_base = ret;
3052 }
3053
3054 void probe_guest_base(const char *image_name, abi_ulong guest_loaddr,
3055 abi_ulong guest_hiaddr)
3056 {
3057 /* In order to use host shmat, we must be able to honor SHMLBA. */
3058 uintptr_t align = MAX(SHMLBA, qemu_host_page_size);
3059
3060 /* Sanity check the guest binary. */
3061 if (reserved_va) {
3062 if (guest_hiaddr > reserved_va) {
3063 error_report("%s: requires more than reserved virtual "
3064 "address space (0x%" PRIx64 " > 0x%lx)",
3065 image_name, (uint64_t)guest_hiaddr, reserved_va);
3066 exit(EXIT_FAILURE);
3067 }
3068 } else {
3069 if (guest_hiaddr != (uintptr_t)guest_hiaddr) {
3070 error_report("%s: requires more virtual address space "
3071 "than the host can provide (0x%" PRIx64 ")",
3072 image_name, (uint64_t)guest_hiaddr + 1);
3073 exit(EXIT_FAILURE);
3074 }
3075 }
3076
3077 if (have_guest_base) {
3078 pgb_fixed(image_name, guest_loaddr, guest_hiaddr, align);
3079 } else {
3080 pgb_dynamic(image_name, guest_loaddr, guest_hiaddr, align);
3081 }
3082
3083 /* Reserve and initialize the commpage. */
3084 if (!init_guest_commpage()) {
3085 /* We have already probed for the commpage being free. */
3086 g_assert_not_reached();
3087 }
3088
3089 assert(QEMU_IS_ALIGNED(guest_base, align));
3090 qemu_log_mask(CPU_LOG_PAGE, "Locating guest address space "
3091 "@ 0x%" PRIx64 "\n", (uint64_t)guest_base);
3092 }
3093
3094 enum {
3095 /* The string "GNU\0" as a magic number. */
3096 GNU0_MAGIC = const_le32('G' | 'N' << 8 | 'U' << 16),
3097 NOTE_DATA_SZ = 1 * KiB,
3098 NOTE_NAME_SZ = 4,
3099 ELF_GNU_PROPERTY_ALIGN = ELF_CLASS == ELFCLASS32 ? 4 : 8,
3100 };
3101
3102 /*
3103 * Process a single gnu_property entry.
3104 * Return false for error.
3105 */
3106 static bool parse_elf_property(const uint32_t *data, int *off, int datasz,
3107 struct image_info *info, bool have_prev_type,
3108 uint32_t *prev_type, Error **errp)
3109 {
3110 uint32_t pr_type, pr_datasz, step;
3111
3112 if (*off > datasz || !QEMU_IS_ALIGNED(*off, ELF_GNU_PROPERTY_ALIGN)) {
3113 goto error_data;
3114 }
3115 datasz -= *off;
3116 data += *off / sizeof(uint32_t);
3117
3118 if (datasz < 2 * sizeof(uint32_t)) {
3119 goto error_data;
3120 }
3121 pr_type = data[0];
3122 pr_datasz = data[1];
3123 data += 2;
3124 datasz -= 2 * sizeof(uint32_t);
3125 step = ROUND_UP(pr_datasz, ELF_GNU_PROPERTY_ALIGN);
3126 if (step > datasz) {
3127 goto error_data;
3128 }
3129
3130 /* Properties are supposed to be unique and sorted on pr_type. */
3131 if (have_prev_type && pr_type <= *prev_type) {
3132 if (pr_type == *prev_type) {
3133 error_setg(errp, "Duplicate property in PT_GNU_PROPERTY");
3134 } else {
3135 error_setg(errp, "Unsorted property in PT_GNU_PROPERTY");
3136 }
3137 return false;
3138 }
3139 *prev_type = pr_type;
3140
3141 if (!arch_parse_elf_property(pr_type, pr_datasz, data, info, errp)) {
3142 return false;
3143 }
3144
3145 *off += 2 * sizeof(uint32_t) + step;
3146 return true;
3147
3148 error_data:
3149 error_setg(errp, "Ill-formed property in PT_GNU_PROPERTY");
3150 return false;
3151 }
3152
3153 /* Process NT_GNU_PROPERTY_TYPE_0. */
3154 static bool parse_elf_properties(const ImageSource *src,
3155 struct image_info *info,
3156 const struct elf_phdr *phdr,
3157 Error **errp)
3158 {
3159 union {
3160 struct elf_note nhdr;
3161 uint32_t data[NOTE_DATA_SZ / sizeof(uint32_t)];
3162 } note;
3163
3164 int n, off, datasz;
3165 bool have_prev_type;
3166 uint32_t prev_type;
3167
3168 /* Unless the arch requires properties, ignore them. */
3169 if (!ARCH_USE_GNU_PROPERTY) {
3170 return true;
3171 }
3172
3173 /* If the properties are crazy large, that's too bad. */
3174 n = phdr->p_filesz;
3175 if (n > sizeof(note)) {
3176 error_setg(errp, "PT_GNU_PROPERTY too large");
3177 return false;
3178 }
3179 if (n < sizeof(note.nhdr)) {
3180 error_setg(errp, "PT_GNU_PROPERTY too small");
3181 return false;
3182 }
3183
3184 if (!imgsrc_read(&note, phdr->p_offset, n, src, errp)) {
3185 return false;
3186 }
3187
3188 /*
3189 * The contents of a valid PT_GNU_PROPERTY is a sequence
3190 * of uint32_t -- swap them all now.
3191 */
3192 #ifdef BSWAP_NEEDED
3193 for (int i = 0; i < n / 4; i++) {
3194 bswap32s(note.data + i);
3195 }
3196 #endif
3197
3198 /*
3199 * Note that nhdr is 3 words, and that the "name" described by namesz
3200 * immediately follows nhdr and is thus at the 4th word. Further, all
3201 * of the inputs to the kernel's round_up are multiples of 4.
3202 */
3203 if (note.nhdr.n_type != NT_GNU_PROPERTY_TYPE_0 ||
3204 note.nhdr.n_namesz != NOTE_NAME_SZ ||
3205 note.data[3] != GNU0_MAGIC) {
3206 error_setg(errp, "Invalid note in PT_GNU_PROPERTY");
3207 return false;
3208 }
3209 off = sizeof(note.nhdr) + NOTE_NAME_SZ;
3210
3211 datasz = note.nhdr.n_descsz + off;
3212 if (datasz > n) {
3213 error_setg(errp, "Invalid note size in PT_GNU_PROPERTY");
3214 return false;
3215 }
3216
3217 have_prev_type = false;
3218 prev_type = 0;
3219 while (1) {
3220 if (off == datasz) {
3221 return true; /* end, exit ok */
3222 }
3223 if (!parse_elf_property(note.data, &off, datasz, info,
3224 have_prev_type, &prev_type, errp)) {
3225 return false;
3226 }
3227 have_prev_type = true;
3228 }
3229 }
3230
3231 /**
3232 * load_elf_image: Load an ELF image into the address space.
3233 * @image_name: the filename of the image, to use in error messages.
3234 * @src: the ImageSource from which to read.
3235 * @info: info collected from the loaded image.
3236 * @ehdr: the ELF header, not yet bswapped.
3237 * @pinterp_name: record any PT_INTERP string found.
3238 *
3239 * On return: @info values will be filled in, as necessary or available.
3240 */
3241
3242 static void load_elf_image(const char *image_name, const ImageSource *src,
3243 struct image_info *info, struct elfhdr *ehdr,
3244 char **pinterp_name)
3245 {
3246 g_autofree struct elf_phdr *phdr = NULL;
3247 abi_ulong load_addr, load_bias, loaddr, hiaddr, error;
3248 int i, prot_exec;
3249 Error *err = NULL;
3250
3251 /*
3252 * First of all, some simple consistency checks.
3253 * Note that we rely on the bswapped ehdr staying in bprm_buf,
3254 * for later use by load_elf_binary and create_elf_tables.
3255 */
3256 if (!imgsrc_read(ehdr, 0, sizeof(*ehdr), src, &err)) {
3257 goto exit_errmsg;
3258 }
3259 if (!elf_check_ident(ehdr)) {
3260 error_setg(&err, "Invalid ELF image for this architecture");
3261 goto exit_errmsg;
3262 }
3263 bswap_ehdr(ehdr);
3264 if (!elf_check_ehdr(ehdr)) {
3265 error_setg(&err, "Invalid ELF image for this architecture");
3266 goto exit_errmsg;
3267 }
3268
3269 phdr = imgsrc_read_alloc(ehdr->e_phoff,
3270 ehdr->e_phnum * sizeof(struct elf_phdr),
3271 src, &err);
3272 if (phdr == NULL) {
3273 goto exit_errmsg;
3274 }
3275 bswap_phdr(phdr, ehdr->e_phnum);
3276
3277 info->nsegs = 0;
3278 info->pt_dynamic_addr = 0;
3279
3280 mmap_lock();
3281
3282 /*
3283 * Find the maximum size of the image and allocate an appropriate
3284 * amount of memory to handle that. Locate the interpreter, if any.
3285 */
3286 loaddr = -1, hiaddr = 0;
3287 info->alignment = 0;
3288 info->exec_stack = EXSTACK_DEFAULT;
3289 for (i = 0; i < ehdr->e_phnum; ++i) {
3290 struct elf_phdr *eppnt = phdr + i;
3291 if (eppnt->p_type == PT_LOAD) {
3292 abi_ulong a = eppnt->p_vaddr - eppnt->p_offset;
3293 if (a < loaddr) {
3294 loaddr = a;
3295 }
3296 a = eppnt->p_vaddr + eppnt->p_memsz - 1;
3297 if (a > hiaddr) {
3298 hiaddr = a;
3299 }
3300 ++info->nsegs;
3301 info->alignment |= eppnt->p_align;
3302 } else if (eppnt->p_type == PT_INTERP && pinterp_name) {
3303 g_autofree char *interp_name = NULL;
3304
3305 if (*pinterp_name) {
3306 error_setg(&err, "Multiple PT_INTERP entries");
3307 goto exit_errmsg;
3308 }
3309
3310 interp_name = imgsrc_read_alloc(eppnt->p_offset, eppnt->p_filesz,
3311 src, &err);
3312 if (interp_name == NULL) {
3313 goto exit_errmsg;
3314 }
3315 if (interp_name[eppnt->p_filesz - 1] != 0) {
3316 error_setg(&err, "Invalid PT_INTERP entry");
3317 goto exit_errmsg;
3318 }
3319 *pinterp_name = g_steal_pointer(&interp_name);
3320 } else if (eppnt->p_type == PT_GNU_PROPERTY) {
3321 if (!parse_elf_properties(src, info, eppnt, &err)) {
3322 goto exit_errmsg;
3323 }
3324 } else if (eppnt->p_type == PT_GNU_STACK) {
3325 info->exec_stack = eppnt->p_flags & PF_X;
3326 }
3327 }
3328
3329 load_addr = loaddr;
3330
3331 if (pinterp_name != NULL) {
3332 if (ehdr->e_type == ET_EXEC) {
3333 /*
3334 * Make sure that the low address does not conflict with
3335 * MMAP_MIN_ADDR or the QEMU application itself.
3336 */
3337 probe_guest_base(image_name, loaddr, hiaddr);
3338 } else {
3339 abi_ulong align;
3340
3341 /*
3342 * The binary is dynamic, but we still need to
3343 * select guest_base. In this case we pass a size.
3344 */
3345 probe_guest_base(image_name, 0, hiaddr - loaddr);
3346
3347 /*
3348 * Avoid collision with the loader by providing a different
3349 * default load address.
3350 */
3351 load_addr += elf_et_dyn_base;
3352
3353 /*
3354 * TODO: Better support for mmap alignment is desirable.
3355 * Since we do not have complete control over the guest
3356 * address space, we prefer the kernel to choose some address
3357 * rather than force the use of LOAD_ADDR via MAP_FIXED.
3358 * But without MAP_FIXED we cannot guarantee alignment,
3359 * only suggest it.
3360 */
3361 align = pow2ceil(info->alignment);
3362 if (align) {
3363 load_addr &= -align;
3364 }
3365 }
3366 }
3367
3368 /*
3369 * Reserve address space for all of this.
3370 *
3371 * In the case of ET_EXEC, we supply MAP_FIXED_NOREPLACE so that we get
3372 * exactly the address range that is required. Without reserved_va,
3373 * the guest address space is not isolated. We have attempted to avoid
3374 * conflict with the host program itself via probe_guest_base, but using
3375 * MAP_FIXED_NOREPLACE instead of MAP_FIXED provides an extra check.
3376 *
3377 * Otherwise this is ET_DYN, and we are searching for a location
3378 * that can hold the memory space required. If the image is
3379 * pre-linked, LOAD_ADDR will be non-zero, and the kernel should
3380 * honor that address if it happens to be free.
3381 *
3382 * In both cases, we will overwrite pages in this range with mappings
3383 * from the executable.
3384 */
3385 load_addr = target_mmap(load_addr, (size_t)hiaddr - loaddr + 1, PROT_NONE,
3386 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE |
3387 (ehdr->e_type == ET_EXEC ? MAP_FIXED_NOREPLACE : 0),
3388 -1, 0);
3389 if (load_addr == -1) {
3390 goto exit_mmap;
3391 }
3392 load_bias = load_addr - loaddr;
3393
3394 if (elf_is_fdpic(ehdr)) {
3395 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs =
3396 g_malloc(sizeof(*loadsegs) * info->nsegs);
3397
3398 for (i = 0; i < ehdr->e_phnum; ++i) {
3399 switch (phdr[i].p_type) {
3400 case PT_DYNAMIC:
3401 info->pt_dynamic_addr = phdr[i].p_vaddr + load_bias;
3402 break;
3403 case PT_LOAD:
3404 loadsegs->addr = phdr[i].p_vaddr + load_bias;
3405 loadsegs->p_vaddr = phdr[i].p_vaddr;
3406 loadsegs->p_memsz = phdr[i].p_memsz;
3407 ++loadsegs;
3408 break;
3409 }
3410 }
3411 }
3412
3413 info->load_bias = load_bias;
3414 info->code_offset = load_bias;
3415 info->data_offset = load_bias;
3416 info->load_addr = load_addr;
3417 info->entry = ehdr->e_entry + load_bias;
3418 info->start_code = -1;
3419 info->end_code = 0;
3420 info->start_data = -1;
3421 info->end_data = 0;
3422 /* Usual start for brk is after all sections of the main executable. */
3423 info->brk = TARGET_PAGE_ALIGN(hiaddr + load_bias);
3424 info->elf_flags = ehdr->e_flags;
3425
3426 prot_exec = PROT_EXEC;
3427 #ifdef TARGET_AARCH64
3428 /*
3429 * If the BTI feature is present, this indicates that the executable
3430 * pages of the startup binary should be mapped with PROT_BTI, so that
3431 * branch targets are enforced.
3432 *
3433 * The startup binary is either the interpreter or the static executable.
3434 * The interpreter is responsible for all pages of a dynamic executable.
3435 *
3436 * Elf notes are backward compatible to older cpus.
3437 * Do not enable BTI unless it is supported.
3438 */
3439 if ((info->note_flags & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)
3440 && (pinterp_name == NULL || *pinterp_name == 0)
3441 && cpu_isar_feature(aa64_bti, ARM_CPU(thread_cpu))) {
3442 prot_exec |= TARGET_PROT_BTI;
3443 }
3444 #endif
3445
3446 for (i = 0; i < ehdr->e_phnum; i++) {
3447 struct elf_phdr *eppnt = phdr + i;
3448 if (eppnt->p_type == PT_LOAD) {
3449 abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em;
3450 int elf_prot = 0;
3451
3452 if (eppnt->p_flags & PF_R) {
3453 elf_prot |= PROT_READ;
3454 }
3455 if (eppnt->p_flags & PF_W) {
3456 elf_prot |= PROT_WRITE;
3457 }
3458 if (eppnt->p_flags & PF_X) {
3459 elf_prot |= prot_exec;
3460 }
3461
3462 vaddr = load_bias + eppnt->p_vaddr;
3463 vaddr_po = vaddr & ~TARGET_PAGE_MASK;
3464 vaddr_ps = vaddr & TARGET_PAGE_MASK;
3465
3466 vaddr_ef = vaddr + eppnt->p_filesz;
3467 vaddr_em = vaddr + eppnt->p_memsz;
3468
3469 /*
3470 * Some segments may be completely empty, with a non-zero p_memsz
3471 * but no backing file segment.
3472 */
3473 if (eppnt->p_filesz != 0) {
3474 error = imgsrc_mmap(vaddr_ps, eppnt->p_filesz + vaddr_po,
3475 elf_prot, MAP_PRIVATE | MAP_FIXED,
3476 src, eppnt->p_offset - vaddr_po);
3477 if (error == -1) {
3478 goto exit_mmap;
3479 }
3480 }
3481
3482 /* If the load segment requests extra zeros (e.g. bss), map it. */
3483 if (vaddr_ef < vaddr_em &&
3484 !zero_bss(vaddr_ef, vaddr_em, elf_prot, &err)) {
3485 goto exit_errmsg;
3486 }
3487
3488 /* Find the full program boundaries. */
3489 if (elf_prot & PROT_EXEC) {
3490 if (vaddr < info->start_code) {
3491 info->start_code = vaddr;
3492 }
3493 if (vaddr_ef > info->end_code) {
3494 info->end_code = vaddr_ef;
3495 }
3496 }
3497 if (elf_prot & PROT_WRITE) {
3498 if (vaddr < info->start_data) {
3499 info->start_data = vaddr;
3500 }
3501 if (vaddr_ef > info->end_data) {
3502 info->end_data = vaddr_ef;
3503 }
3504 }
3505 #ifdef TARGET_MIPS
3506 } else if (eppnt->p_type == PT_MIPS_ABIFLAGS) {
3507 Mips_elf_abiflags_v0 abiflags;
3508
3509 if (!imgsrc_read(&abiflags, eppnt->p_offset, sizeof(abiflags),
3510 src, &err)) {
3511 goto exit_errmsg;
3512 }
3513 bswap_mips_abiflags(&abiflags);
3514 info->fp_abi = abiflags.fp_abi;
3515 #endif
3516 }
3517 }
3518
3519 if (info->end_data == 0) {
3520 info->start_data = info->end_code;
3521 info->end_data = info->end_code;
3522 }
3523
3524 if (qemu_log_enabled()) {
3525 load_symbols(ehdr, src, load_bias);
3526 }
3527
3528 debuginfo_report_elf(image_name, src->fd, load_bias);
3529
3530 mmap_unlock();
3531
3532 close(src->fd);
3533 return;
3534
3535 exit_mmap:
3536 error_setg_errno(&err, errno, "Error mapping file");
3537 goto exit_errmsg;
3538 exit_errmsg:
3539 error_reportf_err(err, "%s: ", image_name);
3540 exit(-1);
3541 }
3542
3543 static void load_elf_interp(const char *filename, struct image_info *info,
3544 char bprm_buf[BPRM_BUF_SIZE])
3545 {
3546 struct elfhdr ehdr;
3547 ImageSource src;
3548 int fd, retval;
3549 Error *err = NULL;
3550
3551 fd = open(path(filename), O_RDONLY);
3552 if (fd < 0) {
3553 error_setg_file_open(&err, errno, filename);
3554 error_report_err(err);
3555 exit(-1);
3556 }
3557
3558 retval = read(fd, bprm_buf, BPRM_BUF_SIZE);
3559 if (retval < 0) {
3560 error_setg_errno(&err, errno, "Error reading file header");
3561 error_reportf_err(err, "%s: ", filename);
3562 exit(-1);
3563 }
3564
3565 src.fd = fd;
3566 src.cache = bprm_buf;
3567 src.cache_size = retval;
3568
3569 load_elf_image(filename, &src, info, &ehdr, NULL);
3570 }
3571
3572 #ifdef VDSO_HEADER
3573 #include VDSO_HEADER
3574 #define vdso_image_info() &vdso_image_info
3575 #else
3576 #define vdso_image_info() NULL
3577 #endif
3578
3579 static void load_elf_vdso(struct image_info *info, const VdsoImageInfo *vdso)
3580 {
3581 ImageSource src;
3582 struct elfhdr ehdr;
3583 abi_ulong load_bias, load_addr;
3584
3585 src.fd = -1;
3586 src.cache = vdso->image;
3587 src.cache_size = vdso->image_size;
3588
3589 load_elf_image("<internal-vdso>", &src, info, &ehdr, NULL);
3590 load_addr = info->load_addr;
3591 load_bias = info->load_bias;
3592
3593 /*
3594 * We need to relocate the VDSO image. The one built into the kernel
3595 * is built for a fixed address. The one built for QEMU is not, since
3596 * that requires close control of the guest address space.
3597 * We pre-processed the image to locate all of the addresses that need
3598 * to be updated.
3599 */
3600 for (unsigned i = 0, n = vdso->reloc_count; i < n; i++) {
3601 abi_ulong *addr = g2h_untagged(load_addr + vdso->relocs[i]);
3602 *addr = tswapal(tswapal(*addr) + load_bias);
3603 }
3604
3605 /* Install signal trampolines, if present. */
3606 if (vdso->sigreturn_ofs) {
3607 default_sigreturn = load_addr + vdso->sigreturn_ofs;
3608 }
3609 if (vdso->rt_sigreturn_ofs) {
3610 default_rt_sigreturn = load_addr + vdso->rt_sigreturn_ofs;
3611 }
3612
3613 /* Remove write from VDSO segment. */
3614 target_mprotect(info->start_data, info->end_data - info->start_data,
3615 PROT_READ | PROT_EXEC);
3616 }
3617
3618 static int symfind(const void *s0, const void *s1)
3619 {
3620 struct elf_sym *sym = (struct elf_sym *)s1;
3621 __typeof(sym->st_value) addr = *(uint64_t *)s0;
3622 int result = 0;
3623
3624 if (addr < sym->st_value) {
3625 result = -1;
3626 } else if (addr >= sym->st_value + sym->st_size) {
3627 result = 1;
3628 }
3629 return result;
3630 }
3631
3632 static const char *lookup_symbolxx(struct syminfo *s, uint64_t orig_addr)
3633 {
3634 #if ELF_CLASS == ELFCLASS32
3635 struct elf_sym *syms = s->disas_symtab.elf32;
3636 #else
3637 struct elf_sym *syms = s->disas_symtab.elf64;
3638 #endif
3639
3640 // binary search
3641 struct elf_sym *sym;
3642
3643 sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), symfind);
3644 if (sym != NULL) {
3645 return s->disas_strtab + sym->st_name;
3646 }
3647
3648 return "";
3649 }
3650
3651 /* FIXME: This should use elf_ops.h */
3652 static int symcmp(const void *s0, const void *s1)
3653 {
3654 struct elf_sym *sym0 = (struct elf_sym *)s0;
3655 struct elf_sym *sym1 = (struct elf_sym *)s1;
3656 return (sym0->st_value < sym1->st_value)
3657 ? -1
3658 : ((sym0->st_value > sym1->st_value) ? 1 : 0);
3659 }
3660
3661 /* Best attempt to load symbols from this ELF object. */
3662 static void load_symbols(struct elfhdr *hdr, const ImageSource *src,
3663 abi_ulong load_bias)
3664 {
3665 int i, shnum, nsyms, sym_idx = 0, str_idx = 0;
3666 g_autofree struct elf_shdr *shdr = NULL;
3667 char *strings = NULL;
3668 struct elf_sym *syms = NULL;
3669 struct elf_sym *new_syms;
3670 uint64_t segsz;
3671
3672 shnum = hdr->e_shnum;
3673 shdr = imgsrc_read_alloc(hdr->e_shoff, shnum * sizeof(struct elf_shdr),
3674 src, NULL);
3675 if (shdr == NULL) {
3676 return;
3677 }
3678
3679 bswap_shdr(shdr, shnum);
3680 for (i = 0; i < shnum; ++i) {
3681 if (shdr[i].sh_type == SHT_SYMTAB) {
3682 sym_idx = i;
3683 str_idx = shdr[i].sh_link;
3684 goto found;
3685 }
3686 }
3687
3688 /* There will be no symbol table if the file was stripped. */
3689 return;
3690
3691 found:
3692 /* Now know where the strtab and symtab are. Snarf them. */
3693
3694 segsz = shdr[str_idx].sh_size;
3695 strings = g_try_malloc(segsz);
3696 if (!strings) {
3697 goto give_up;
3698 }
3699 if (!imgsrc_read(strings, shdr[str_idx].sh_offset, segsz, src, NULL)) {
3700 goto give_up;
3701 }
3702
3703 segsz = shdr[sym_idx].sh_size;
3704 if (segsz / sizeof(struct elf_sym) > INT_MAX) {
3705 /*
3706 * Implausibly large symbol table: give up rather than ploughing
3707 * on with the number of symbols calculation overflowing.
3708 */
3709 goto give_up;
3710 }
3711 nsyms = segsz / sizeof(struct elf_sym);
3712 syms = g_try_malloc(segsz);
3713 if (!syms) {
3714 goto give_up;
3715 }
3716 if (!imgsrc_read(syms, shdr[sym_idx].sh_offset, segsz, src, NULL)) {
3717 goto give_up;
3718 }
3719
3720 for (i = 0; i < nsyms; ) {
3721 bswap_sym(syms + i);
3722 /* Throw away entries which we do not need. */
3723 if (syms[i].st_shndx == SHN_UNDEF
3724 || syms[i].st_shndx >= SHN_LORESERVE
3725 || ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
3726 if (i < --nsyms) {
3727 syms[i] = syms[nsyms];
3728 }
3729 } else {
3730 #if defined(TARGET_ARM) || defined (TARGET_MIPS)
3731 /* The bottom address bit marks a Thumb or MIPS16 symbol. */
3732 syms[i].st_value &= ~(target_ulong)1;
3733 #endif
3734 syms[i].st_value += load_bias;
3735 i++;
3736 }
3737 }
3738
3739 /* No "useful" symbol. */
3740 if (nsyms == 0) {
3741 goto give_up;
3742 }
3743
3744 /*
3745 * Attempt to free the storage associated with the local symbols
3746 * that we threw away. Whether or not this has any effect on the
3747 * memory allocation depends on the malloc implementation and how
3748 * many symbols we managed to discard.
3749 */
3750 new_syms = g_try_renew(struct elf_sym, syms, nsyms);
3751 if (new_syms == NULL) {
3752 goto give_up;
3753 }
3754 syms = new_syms;
3755
3756 qsort(syms, nsyms, sizeof(*syms), symcmp);
3757
3758 {
3759 struct syminfo *s = g_new(struct syminfo, 1);
3760
3761 s->disas_strtab = strings;
3762 s->disas_num_syms = nsyms;
3763 #if ELF_CLASS == ELFCLASS32
3764 s->disas_symtab.elf32 = syms;
3765 #else
3766 s->disas_symtab.elf64 = syms;
3767 #endif
3768 s->lookup_symbol = lookup_symbolxx;
3769 s->next = syminfos;
3770 syminfos = s;
3771 }
3772 return;
3773
3774 give_up:
3775 g_free(strings);
3776 g_free(syms);
3777 }
3778
3779 uint32_t get_elf_eflags(int fd)
3780 {
3781 struct elfhdr ehdr;
3782 off_t offset;
3783 int ret;
3784
3785 /* Read ELF header */
3786 offset = lseek(fd, 0, SEEK_SET);
3787 if (offset == (off_t) -1) {
3788 return 0;
3789 }
3790 ret = read(fd, &ehdr, sizeof(ehdr));
3791 if (ret < sizeof(ehdr)) {
3792 return 0;
3793 }
3794 offset = lseek(fd, offset, SEEK_SET);
3795 if (offset == (off_t) -1) {
3796 return 0;
3797 }
3798
3799 /* Check ELF signature */
3800 if (!elf_check_ident(&ehdr)) {
3801 return 0;
3802 }
3803
3804 /* check header */
3805 bswap_ehdr(&ehdr);
3806 if (!elf_check_ehdr(&ehdr)) {
3807 return 0;
3808 }
3809
3810 /* return architecture id */
3811 return ehdr.e_flags;
3812 }
3813
3814 int load_elf_binary(struct linux_binprm *bprm, struct image_info *info)
3815 {
3816 /*
3817 * We need a copy of the elf header for passing to create_elf_tables.
3818 * We will have overwritten the original when we re-use bprm->buf
3819 * while loading the interpreter. Allocate the storage for this now
3820 * and let elf_load_image do any swapping that may be required.
3821 */
3822 struct elfhdr ehdr;
3823 struct image_info interp_info, vdso_info;
3824 char *elf_interpreter = NULL;
3825 char *scratch;
3826
3827 memset(&interp_info, 0, sizeof(interp_info));
3828 #ifdef TARGET_MIPS
3829 interp_info.fp_abi = MIPS_ABI_FP_UNKNOWN;
3830 #endif
3831
3832 load_elf_image(bprm->filename, &bprm->src, info, &ehdr, &elf_interpreter);
3833
3834 /* Do this so that we can load the interpreter, if need be. We will
3835 change some of these later */
3836 bprm->p = setup_arg_pages(bprm, info);
3837
3838 scratch = g_new0(char, TARGET_PAGE_SIZE);
3839 if (STACK_GROWS_DOWN) {
3840 bprm->p = copy_elf_strings(1, &bprm->filename, scratch,
3841 bprm->p, info->stack_limit);
3842 info->file_string = bprm->p;
3843 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch,
3844 bprm->p, info->stack_limit);
3845 info->env_strings = bprm->p;
3846 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch,
3847 bprm->p, info->stack_limit);
3848 info->arg_strings = bprm->p;
3849 } else {
3850 info->arg_strings = bprm->p;
3851 bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch,
3852 bprm->p, info->stack_limit);
3853 info->env_strings = bprm->p;
3854 bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch,
3855 bprm->p, info->stack_limit);
3856 info->file_string = bprm->p;
3857 bprm->p = copy_elf_strings(1, &bprm->filename, scratch,
3858 bprm->p, info->stack_limit);
3859 }
3860
3861 g_free(scratch);
3862
3863 if (!bprm->p) {
3864 fprintf(stderr, "%s: %s\n", bprm->filename, strerror(E2BIG));
3865 exit(-1);
3866 }
3867
3868 if (elf_interpreter) {
3869 load_elf_interp(elf_interpreter, &interp_info, bprm->buf);
3870
3871 /*
3872 * While unusual because of ELF_ET_DYN_BASE, if we are unlucky
3873 * with the mappings the interpreter can be loaded above but
3874 * near the main executable, which can leave very little room
3875 * for the heap.
3876 * If the current brk has less than 16MB, use the end of the
3877 * interpreter.
3878 */
3879 if (interp_info.brk > info->brk &&
3880 interp_info.load_bias - info->brk < 16 * MiB) {
3881 info->brk = interp_info.brk;
3882 }
3883
3884 /* If the program interpreter is one of these two, then assume
3885 an iBCS2 image. Otherwise assume a native linux image. */
3886
3887 if (strcmp(elf_interpreter, "/usr/lib/libc.so.1") == 0
3888 || strcmp(elf_interpreter, "/usr/lib/ld.so.1") == 0) {
3889 info->personality = PER_SVR4;
3890
3891 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
3892 and some applications "depend" upon this behavior. Since
3893 we do not have the power to recompile these, we emulate
3894 the SVr4 behavior. Sigh. */
3895 target_mmap(0, qemu_host_page_size, PROT_READ | PROT_EXEC,
3896 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
3897 }
3898 #ifdef TARGET_MIPS
3899 info->interp_fp_abi = interp_info.fp_abi;
3900 #endif
3901 }
3902
3903 /*
3904 * Load a vdso if available, which will amongst other things contain the
3905 * signal trampolines. Otherwise, allocate a separate page for them.
3906 */
3907 const VdsoImageInfo *vdso = vdso_image_info();
3908 if (vdso) {
3909 load_elf_vdso(&vdso_info, vdso);
3910 } else if (TARGET_ARCH_HAS_SIGTRAMP_PAGE) {
3911 abi_long tramp_page = target_mmap(0, TARGET_PAGE_SIZE,
3912 PROT_READ | PROT_WRITE,
3913 MAP_PRIVATE | MAP_ANON, -1, 0);
3914 if (tramp_page == -1) {
3915 return -errno;
3916 }
3917
3918 setup_sigtramp(tramp_page);
3919 target_mprotect(tramp_page, TARGET_PAGE_SIZE, PROT_READ | PROT_EXEC);
3920 }
3921
3922 bprm->p = create_elf_tables(bprm->p, bprm->argc, bprm->envc, &ehdr, info,
3923 elf_interpreter ? &interp_info : NULL,
3924 vdso ? &vdso_info : NULL);
3925 info->start_stack = bprm->p;
3926
3927 /* If we have an interpreter, set that as the program's entry point.
3928 Copy the load_bias as well, to help PPC64 interpret the entry
3929 point as a function descriptor. Do this after creating elf tables
3930 so that we copy the original program entry point into the AUXV. */
3931 if (elf_interpreter) {
3932 info->load_bias = interp_info.load_bias;
3933 info->entry = interp_info.entry;
3934 g_free(elf_interpreter);
3935 }
3936
3937 #ifdef USE_ELF_CORE_DUMP
3938 bprm->core_dump = &elf_core_dump;
3939 #endif
3940
3941 return 0;
3942 }
3943
3944 #ifdef USE_ELF_CORE_DUMP
3945 /*
3946 * Definitions to generate Intel SVR4-like core files.
3947 * These mostly have the same names as the SVR4 types with "target_elf_"
3948 * tacked on the front to prevent clashes with linux definitions,
3949 * and the typedef forms have been avoided. This is mostly like
3950 * the SVR4 structure, but more Linuxy, with things that Linux does
3951 * not support and which gdb doesn't really use excluded.
3952 *
3953 * Fields we don't dump (their contents is zero) in linux-user qemu
3954 * are marked with XXX.
3955 *
3956 * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
3957 *
3958 * Porting ELF coredump for target is (quite) simple process. First you
3959 * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for
3960 * the target resides):
3961 *
3962 * #define USE_ELF_CORE_DUMP
3963 *
3964 * Next you define type of register set used for dumping. ELF specification
3965 * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
3966 *
3967 * typedef <target_regtype> target_elf_greg_t;
3968 * #define ELF_NREG <number of registers>
3969 * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG];
3970 *
3971 * Last step is to implement target specific function that copies registers
3972 * from given cpu into just specified register set. Prototype is:
3973 *
3974 * static void elf_core_copy_regs(taret_elf_gregset_t *regs,
3975 * const CPUArchState *env);
3976 *
3977 * Parameters:
3978 * regs - copy register values into here (allocated and zeroed by caller)
3979 * env - copy registers from here
3980 *
3981 * Example for ARM target is provided in this file.
3982 */
3983
3984 /* An ELF note in memory */
3985 struct memelfnote {
3986 const char *name;
3987 size_t namesz;
3988 size_t namesz_rounded;
3989 int type;
3990 size_t datasz;
3991 size_t datasz_rounded;
3992 void *data;
3993 size_t notesz;
3994 };
3995
3996 struct target_elf_siginfo {
3997 abi_int si_signo; /* signal number */
3998 abi_int si_code; /* extra code */
3999 abi_int si_errno; /* errno */
4000 };
4001
4002 struct target_elf_prstatus {
4003 struct target_elf_siginfo pr_info; /* Info associated with signal */
4004 abi_short pr_cursig; /* Current signal */
4005 abi_ulong pr_sigpend; /* XXX */
4006 abi_ulong pr_sighold; /* XXX */
4007 target_pid_t pr_pid;
4008 target_pid_t pr_ppid;
4009 target_pid_t pr_pgrp;
4010 target_pid_t pr_sid;
4011 struct target_timeval pr_utime; /* XXX User time */
4012 struct target_timeval pr_stime; /* XXX System time */
4013 struct target_timeval pr_cutime; /* XXX Cumulative user time */
4014 struct target_timeval pr_cstime; /* XXX Cumulative system time */
4015 target_elf_gregset_t pr_reg; /* GP registers */
4016 abi_int pr_fpvalid; /* XXX */
4017 };
4018
4019 #define ELF_PRARGSZ (80) /* Number of chars for args */
4020
4021 struct target_elf_prpsinfo {
4022 char pr_state; /* numeric process state */
4023 char pr_sname; /* char for pr_state */
4024 char pr_zomb; /* zombie */
4025 char pr_nice; /* nice val */
4026 abi_ulong pr_flag; /* flags */
4027 target_uid_t pr_uid;
4028 target_gid_t pr_gid;
4029 target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
4030 /* Lots missing */
4031 char pr_fname[16] QEMU_NONSTRING; /* filename of executable */
4032 char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
4033 };
4034
4035 /* Here is the structure in which status of each thread is captured. */
4036 struct elf_thread_status {
4037 QTAILQ_ENTRY(elf_thread_status) ets_link;
4038 struct target_elf_prstatus prstatus; /* NT_PRSTATUS */
4039 #if 0
4040 elf_fpregset_t fpu; /* NT_PRFPREG */
4041 struct task_struct *thread;
4042 elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */
4043 #endif
4044 struct memelfnote notes[1];
4045 int num_notes;
4046 };
4047
4048 struct elf_note_info {
4049 struct memelfnote *notes;
4050 struct target_elf_prstatus *prstatus; /* NT_PRSTATUS */
4051 struct target_elf_prpsinfo *psinfo; /* NT_PRPSINFO */
4052
4053 QTAILQ_HEAD(, elf_thread_status) thread_list;
4054 #if 0
4055 /*
4056 * Current version of ELF coredump doesn't support
4057 * dumping fp regs etc.
4058 */
4059 elf_fpregset_t *fpu;
4060 elf_fpxregset_t *xfpu;
4061 int thread_status_size;
4062 #endif
4063 int notes_size;
4064 int numnote;
4065 };
4066
4067 struct vm_area_struct {
4068 target_ulong vma_start; /* start vaddr of memory region */
4069 target_ulong vma_end; /* end vaddr of memory region */
4070 abi_ulong vma_flags; /* protection etc. flags for the region */
4071 QTAILQ_ENTRY(vm_area_struct) vma_link;
4072 };
4073
4074 struct mm_struct {
4075 QTAILQ_HEAD(, vm_area_struct) mm_mmap;
4076 int mm_count; /* number of mappings */
4077 };
4078
4079 static struct mm_struct *vma_init(void);
4080 static void vma_delete(struct mm_struct *);
4081 static int vma_add_mapping(struct mm_struct *, target_ulong,
4082 target_ulong, abi_ulong);
4083 static int vma_get_mapping_count(const struct mm_struct *);
4084 static struct vm_area_struct *vma_first(const struct mm_struct *);
4085 static struct vm_area_struct *vma_next(struct vm_area_struct *);
4086 static abi_ulong vma_dump_size(const struct vm_area_struct *);
4087 static int vma_walker(void *priv, target_ulong start, target_ulong end,
4088 unsigned long flags);
4089
4090 static void fill_elf_header(struct elfhdr *, int, uint16_t, uint32_t);
4091 static void fill_note(struct memelfnote *, const char *, int,
4092 unsigned int, void *);
4093 static void fill_prstatus(struct target_elf_prstatus *, const TaskState *, int);
4094 static int fill_psinfo(struct target_elf_prpsinfo *, const TaskState *);
4095 static void fill_auxv_note(struct memelfnote *, const TaskState *);
4096 static void fill_elf_note_phdr(struct elf_phdr *, int, off_t);
4097 static size_t note_size(const struct memelfnote *);
4098 static void free_note_info(struct elf_note_info *);
4099 static int fill_note_info(struct elf_note_info *, long, const CPUArchState *);
4100 static void fill_thread_info(struct elf_note_info *, const CPUArchState *);
4101
4102 static int dump_write(int, const void *, size_t);
4103 static int write_note(struct memelfnote *, int);
4104 static int write_note_info(struct elf_note_info *, int);
4105
4106 #ifdef BSWAP_NEEDED
4107 static void bswap_prstatus(struct target_elf_prstatus *prstatus)
4108 {
4109 prstatus->pr_info.si_signo = tswap32(prstatus->pr_info.si_signo);
4110 prstatus->pr_info.si_code = tswap32(prstatus->pr_info.si_code);
4111 prstatus->pr_info.si_errno = tswap32(prstatus->pr_info.si_errno);
4112 prstatus->pr_cursig = tswap16(prstatus->pr_cursig);
4113 prstatus->pr_sigpend = tswapal(prstatus->pr_sigpend);
4114 prstatus->pr_sighold = tswapal(prstatus->pr_sighold);
4115 prstatus->pr_pid = tswap32(prstatus->pr_pid);
4116 prstatus->pr_ppid = tswap32(prstatus->pr_ppid);
4117 prstatus->pr_pgrp = tswap32(prstatus->pr_pgrp);
4118 prstatus->pr_sid = tswap32(prstatus->pr_sid);
4119 /* cpu times are not filled, so we skip them */
4120 /* regs should be in correct format already */
4121 prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid);
4122 }
4123
4124 static void bswap_psinfo(struct target_elf_prpsinfo *psinfo)
4125 {
4126 psinfo->pr_flag = tswapal(psinfo->pr_flag);
4127 psinfo->pr_uid = tswap16(psinfo->pr_uid);
4128 psinfo->pr_gid = tswap16(psinfo->pr_gid);
4129 psinfo->pr_pid = tswap32(psinfo->pr_pid);
4130 psinfo->pr_ppid = tswap32(psinfo->pr_ppid);
4131 psinfo->pr_pgrp = tswap32(psinfo->pr_pgrp);
4132 psinfo->pr_sid = tswap32(psinfo->pr_sid);
4133 }
4134
4135 static void bswap_note(struct elf_note *en)
4136 {
4137 bswap32s(&en->n_namesz);
4138 bswap32s(&en->n_descsz);
4139 bswap32s(&en->n_type);
4140 }
4141 #else
4142 static inline void bswap_prstatus(struct target_elf_prstatus *p) { }
4143 static inline void bswap_psinfo(struct target_elf_prpsinfo *p) {}
4144 static inline void bswap_note(struct elf_note *en) { }
4145 #endif /* BSWAP_NEEDED */
4146
4147 /*
4148 * Minimal support for linux memory regions. These are needed
4149 * when we are finding out what memory exactly belongs to
4150 * emulated process. No locks needed here, as long as
4151 * thread that received the signal is stopped.
4152 */
4153
4154 static struct mm_struct *vma_init(void)
4155 {
4156 struct mm_struct *mm;
4157
4158 if ((mm = g_malloc(sizeof (*mm))) == NULL)
4159 return (NULL);
4160
4161 mm->mm_count = 0;
4162 QTAILQ_INIT(&mm->mm_mmap);
4163
4164 return (mm);
4165 }
4166
4167 static void vma_delete(struct mm_struct *mm)
4168 {
4169 struct vm_area_struct *vma;
4170
4171 while ((vma = vma_first(mm)) != NULL) {
4172 QTAILQ_REMOVE(&mm->mm_mmap, vma, vma_link);
4173 g_free(vma);
4174 }
4175 g_free(mm);
4176 }
4177
4178 static int vma_add_mapping(struct mm_struct *mm, target_ulong start,
4179 target_ulong end, abi_ulong flags)
4180 {
4181 struct vm_area_struct *vma;
4182
4183 if ((vma = g_malloc0(sizeof (*vma))) == NULL)
4184 return (-1);
4185
4186 vma->vma_start = start;
4187 vma->vma_end = end;
4188 vma->vma_flags = flags;
4189
4190 QTAILQ_INSERT_TAIL(&mm->mm_mmap, vma, vma_link);
4191 mm->mm_count++;
4192
4193 return (0);
4194 }
4195
4196 static struct vm_area_struct *vma_first(const struct mm_struct *mm)
4197 {
4198 return (QTAILQ_FIRST(&mm->mm_mmap));
4199 }
4200
4201 static struct vm_area_struct *vma_next(struct vm_area_struct *vma)
4202 {
4203 return (QTAILQ_NEXT(vma, vma_link));
4204 }
4205
4206 static int vma_get_mapping_count(const struct mm_struct *mm)
4207 {
4208 return (mm->mm_count);
4209 }
4210
4211 /*
4212 * Calculate file (dump) size of given memory region.
4213 */
4214 static abi_ulong vma_dump_size(const struct vm_area_struct *vma)
4215 {
4216 /* if we cannot even read the first page, skip it */
4217 if (!access_ok_untagged(VERIFY_READ, vma->vma_start, TARGET_PAGE_SIZE))
4218 return (0);
4219
4220 /*
4221 * Usually we don't dump executable pages as they contain
4222 * non-writable code that debugger can read directly from
4223 * target library etc. However, thread stacks are marked
4224 * also executable so we read in first page of given region
4225 * and check whether it contains elf header. If there is
4226 * no elf header, we dump it.
4227 */
4228 if (vma->vma_flags & PROT_EXEC) {
4229 char page[TARGET_PAGE_SIZE];
4230
4231 if (copy_from_user(page, vma->vma_start, sizeof (page))) {
4232 return 0;
4233 }
4234 if ((page[EI_MAG0] == ELFMAG0) &&
4235 (page[EI_MAG1] == ELFMAG1) &&
4236 (page[EI_MAG2] == ELFMAG2) &&
4237 (page[EI_MAG3] == ELFMAG3)) {
4238 /*
4239 * Mappings are possibly from ELF binary. Don't dump
4240 * them.
4241 */
4242 return (0);
4243 }
4244 }
4245
4246 return (vma->vma_end - vma->vma_start);
4247 }
4248
4249 static int vma_walker(void *priv, target_ulong start, target_ulong end,
4250 unsigned long flags)
4251 {
4252 struct mm_struct *mm = (struct mm_struct *)priv;
4253
4254 vma_add_mapping(mm, start, end, flags);
4255 return (0);
4256 }
4257
4258 static void fill_note(struct memelfnote *note, const char *name, int type,
4259 unsigned int sz, void *data)
4260 {
4261 unsigned int namesz;
4262
4263 namesz = strlen(name) + 1;
4264 note->name = name;
4265 note->namesz = namesz;
4266 note->namesz_rounded = roundup(namesz, sizeof (int32_t));
4267 note->type = type;
4268 note->datasz = sz;
4269 note->datasz_rounded = roundup(sz, sizeof (int32_t));
4270
4271 note->data = data;
4272
4273 /*
4274 * We calculate rounded up note size here as specified by
4275 * ELF document.
4276 */
4277 note->notesz = sizeof (struct elf_note) +
4278 note->namesz_rounded + note->datasz_rounded;
4279 }
4280
4281 static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine,
4282 uint32_t flags)
4283 {
4284 (void) memset(elf, 0, sizeof(*elf));
4285
4286 (void) memcpy(elf->e_ident, ELFMAG, SELFMAG);
4287 elf->e_ident[EI_CLASS] = ELF_CLASS;
4288 elf->e_ident[EI_DATA] = ELF_DATA;
4289 elf->e_ident[EI_VERSION] = EV_CURRENT;
4290 elf->e_ident[EI_OSABI] = ELF_OSABI;
4291
4292 elf->e_type = ET_CORE;
4293 elf->e_machine = machine;
4294 elf->e_version = EV_CURRENT;
4295 elf->e_phoff = sizeof(struct elfhdr);
4296 elf->e_flags = flags;
4297 elf->e_ehsize = sizeof(struct elfhdr);
4298 elf->e_phentsize = sizeof(struct elf_phdr);
4299 elf->e_phnum = segs;
4300
4301 bswap_ehdr(elf);
4302 }
4303
4304 static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset)
4305 {
4306 phdr->p_type = PT_NOTE;
4307 phdr->p_offset = offset;
4308 phdr->p_vaddr = 0;
4309 phdr->p_paddr = 0;
4310 phdr->p_filesz = sz;
4311 phdr->p_memsz = 0;
4312 phdr->p_flags = 0;
4313 phdr->p_align = 0;
4314
4315 bswap_phdr(phdr, 1);
4316 }
4317
4318 static size_t note_size(const struct memelfnote *note)
4319 {
4320 return (note->notesz);
4321 }
4322
4323 static void fill_prstatus(struct target_elf_prstatus *prstatus,
4324 const TaskState *ts, int signr)
4325 {
4326 (void) memset(prstatus, 0, sizeof (*prstatus));
4327 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
4328 prstatus->pr_pid = ts->ts_tid;
4329 prstatus->pr_ppid = getppid();
4330 prstatus->pr_pgrp = getpgrp();
4331 prstatus->pr_sid = getsid(0);
4332
4333 bswap_prstatus(prstatus);
4334 }
4335
4336 static int fill_psinfo(struct target_elf_prpsinfo *psinfo, const TaskState *ts)
4337 {
4338 char *base_filename;
4339 unsigned int i, len;
4340
4341 (void) memset(psinfo, 0, sizeof (*psinfo));
4342
4343 len = ts->info->env_strings - ts->info->arg_strings;
4344 if (len >= ELF_PRARGSZ)
4345 len = ELF_PRARGSZ - 1;
4346 if (copy_from_user(&psinfo->pr_psargs, ts->info->arg_strings, len)) {
4347 return -EFAULT;
4348 }
4349 for (i = 0; i < len; i++)
4350 if (psinfo->pr_psargs[i] == 0)
4351 psinfo->pr_psargs[i] = ' ';
4352 psinfo->pr_psargs[len] = 0;
4353
4354 psinfo->pr_pid = getpid();
4355 psinfo->pr_ppid = getppid();
4356 psinfo->pr_pgrp = getpgrp();
4357 psinfo->pr_sid = getsid(0);
4358 psinfo->pr_uid = getuid();
4359 psinfo->pr_gid = getgid();
4360
4361 base_filename = g_path_get_basename(ts->bprm->filename);
4362 /*
4363 * Using strncpy here is fine: at max-length,
4364 * this field is not NUL-terminated.
4365 */
4366 (void) strncpy(psinfo->pr_fname, base_filename,
4367 sizeof(psinfo->pr_fname));
4368
4369 g_free(base_filename);
4370 bswap_psinfo(psinfo);
4371 return (0);
4372 }
4373
4374 static void fill_auxv_note(struct memelfnote *note, const TaskState *ts)
4375 {
4376 elf_addr_t auxv = (elf_addr_t)ts->info->saved_auxv;
4377 elf_addr_t orig_auxv = auxv;
4378 void *ptr;
4379 int len = ts->info->auxv_len;
4380
4381 /*
4382 * Auxiliary vector is stored in target process stack. It contains
4383 * {type, value} pairs that we need to dump into note. This is not
4384 * strictly necessary but we do it here for sake of completeness.
4385 */
4386
4387 /* read in whole auxv vector and copy it to memelfnote */
4388 ptr = lock_user(VERIFY_READ, orig_auxv, len, 0);
4389 if (ptr != NULL) {
4390 fill_note(note, "CORE", NT_AUXV, len, ptr);
4391 unlock_user(ptr, auxv, len);
4392 }
4393 }
4394
4395 /*
4396 * Constructs name of coredump file. We have following convention
4397 * for the name:
4398 * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
4399 *
4400 * Returns the filename
4401 */
4402 static char *core_dump_filename(const TaskState *ts)
4403 {
4404 g_autoptr(GDateTime) now = g_date_time_new_now_local();
4405 g_autofree char *nowstr = g_date_time_format(now, "%Y%m%d-%H%M%S");
4406 g_autofree char *base_filename = g_path_get_basename(ts->bprm->filename);
4407
4408 return g_strdup_printf("qemu_%s_%s_%d.core",
4409 base_filename, nowstr, (int)getpid());
4410 }
4411
4412 static int dump_write(int fd, const void *ptr, size_t size)
4413 {
4414 const char *bufp = (const char *)ptr;
4415 ssize_t bytes_written, bytes_left;
4416 struct rlimit dumpsize;
4417 off_t pos;
4418
4419 bytes_written = 0;
4420 getrlimit(RLIMIT_CORE, &dumpsize);
4421 if ((pos = lseek(fd, 0, SEEK_CUR))==-1) {
4422 if (errno == ESPIPE) { /* not a seekable stream */
4423 bytes_left = size;
4424 } else {
4425 return pos;
4426 }
4427 } else {
4428 if (dumpsize.rlim_cur <= pos) {
4429 return -1;
4430 } else if (dumpsize.rlim_cur == RLIM_INFINITY) {
4431 bytes_left = size;
4432 } else {
4433 size_t limit_left=dumpsize.rlim_cur - pos;
4434 bytes_left = limit_left >= size ? size : limit_left ;
4435 }
4436 }
4437
4438 /*
4439 * In normal conditions, single write(2) should do but
4440 * in case of socket etc. this mechanism is more portable.
4441 */
4442 do {
4443 bytes_written = write(fd, bufp, bytes_left);
4444 if (bytes_written < 0) {
4445 if (errno == EINTR)
4446 continue;
4447 return (-1);
4448 } else if (bytes_written == 0) { /* eof */
4449 return (-1);
4450 }
4451 bufp += bytes_written;
4452 bytes_left -= bytes_written;
4453 } while (bytes_left > 0);
4454
4455 return (0);
4456 }
4457
4458 static int write_note(struct memelfnote *men, int fd)
4459 {
4460 struct elf_note en;
4461
4462 en.n_namesz = men->namesz;
4463 en.n_type = men->type;
4464 en.n_descsz = men->datasz;
4465
4466 bswap_note(&en);
4467
4468 if (dump_write(fd, &en, sizeof(en)) != 0)
4469 return (-1);
4470 if (dump_write(fd, men->name, men->namesz_rounded) != 0)
4471 return (-1);
4472 if (dump_write(fd, men->data, men->datasz_rounded) != 0)
4473 return (-1);
4474
4475 return (0);
4476 }
4477
4478 static void fill_thread_info(struct elf_note_info *info, const CPUArchState *env)
4479 {
4480 CPUState *cpu = env_cpu((CPUArchState *)env);
4481 TaskState *ts = (TaskState *)cpu->opaque;
4482 struct elf_thread_status *ets;
4483
4484 ets = g_malloc0(sizeof (*ets));
4485 ets->num_notes = 1; /* only prstatus is dumped */
4486 fill_prstatus(&ets->prstatus, ts, 0);
4487 elf_core_copy_regs(&ets->prstatus.pr_reg, env);
4488 fill_note(&ets->notes[0], "CORE", NT_PRSTATUS, sizeof (ets->prstatus),
4489 &ets->prstatus);
4490
4491 QTAILQ_INSERT_TAIL(&info->thread_list, ets, ets_link);
4492
4493 info->notes_size += note_size(&ets->notes[0]);
4494 }
4495
4496 static void init_note_info(struct elf_note_info *info)
4497 {
4498 /* Initialize the elf_note_info structure so that it is at
4499 * least safe to call free_note_info() on it. Must be
4500 * called before calling fill_note_info().
4501 */
4502 memset(info, 0, sizeof (*info));
4503 QTAILQ_INIT(&info->thread_list);
4504 }
4505
4506 static int fill_note_info(struct elf_note_info *info,
4507 long signr, const CPUArchState *env)
4508 {
4509 #define NUMNOTES 3
4510 CPUState *cpu = env_cpu((CPUArchState *)env);
4511 TaskState *ts = (TaskState *)cpu->opaque;
4512 int i;
4513
4514 info->notes = g_new0(struct memelfnote, NUMNOTES);
4515 if (info->notes == NULL)
4516 return (-ENOMEM);
4517 info->prstatus = g_malloc0(sizeof (*info->prstatus));
4518 if (info->prstatus == NULL)
4519 return (-ENOMEM);
4520 info->psinfo = g_malloc0(sizeof (*info->psinfo));
4521 if (info->prstatus == NULL)
4522 return (-ENOMEM);
4523
4524 /*
4525 * First fill in status (and registers) of current thread
4526 * including process info & aux vector.
4527 */
4528 fill_prstatus(info->prstatus, ts, signr);
4529 elf_core_copy_regs(&info->prstatus->pr_reg, env);
4530 fill_note(&info->notes[0], "CORE", NT_PRSTATUS,
4531 sizeof (*info->prstatus), info->prstatus);
4532 fill_psinfo(info->psinfo, ts);
4533 fill_note(&info->notes[1], "CORE", NT_PRPSINFO,
4534 sizeof (*info->psinfo), info->psinfo);
4535 fill_auxv_note(&info->notes[2], ts);
4536 info->numnote = 3;
4537
4538 info->notes_size = 0;
4539 for (i = 0; i < info->numnote; i++)
4540 info->notes_size += note_size(&info->notes[i]);
4541
4542 /* read and fill status of all threads */
4543 WITH_QEMU_LOCK_GUARD(&qemu_cpu_list_lock) {
4544 CPU_FOREACH(cpu) {
4545 if (cpu == thread_cpu) {
4546 continue;
4547 }
4548 fill_thread_info(info, cpu_env(cpu));
4549 }
4550 }
4551
4552 return (0);
4553 }
4554
4555 static void free_note_info(struct elf_note_info *info)
4556 {
4557 struct elf_thread_status *ets;
4558
4559 while (!QTAILQ_EMPTY(&info->thread_list)) {
4560 ets = QTAILQ_FIRST(&info->thread_list);
4561 QTAILQ_REMOVE(&info->thread_list, ets, ets_link);
4562 g_free(ets);
4563 }
4564
4565 g_free(info->prstatus);
4566 g_free(info->psinfo);
4567 g_free(info->notes);
4568 }
4569
4570 static int write_note_info(struct elf_note_info *info, int fd)
4571 {
4572 struct elf_thread_status *ets;
4573 int i, error = 0;
4574
4575 /* write prstatus, psinfo and auxv for current thread */
4576 for (i = 0; i < info->numnote; i++)
4577 if ((error = write_note(&info->notes[i], fd)) != 0)
4578 return (error);
4579
4580 /* write prstatus for each thread */
4581 QTAILQ_FOREACH(ets, &info->thread_list, ets_link) {
4582 if ((error = write_note(&ets->notes[0], fd)) != 0)
4583 return (error);
4584 }
4585
4586 return (0);
4587 }
4588
4589 /*
4590 * Write out ELF coredump.
4591 *
4592 * See documentation of ELF object file format in:
4593 * http://www.caldera.com/developers/devspecs/gabi41.pdf
4594 *
4595 * Coredump format in linux is following:
4596 *
4597 * 0 +----------------------+ \
4598 * | ELF header | ET_CORE |
4599 * +----------------------+ |
4600 * | ELF program headers | |--- headers
4601 * | - NOTE section | |
4602 * | - PT_LOAD sections | |
4603 * +----------------------+ /
4604 * | NOTEs: |
4605 * | - NT_PRSTATUS |
4606 * | - NT_PRSINFO |
4607 * | - NT_AUXV |
4608 * +----------------------+ <-- aligned to target page
4609 * | Process memory dump |
4610 * : :
4611 * . .
4612 * : :
4613 * | |
4614 * +----------------------+
4615 *
4616 * NT_PRSTATUS -> struct elf_prstatus (per thread)
4617 * NT_PRSINFO -> struct elf_prpsinfo
4618 * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()).
4619 *
4620 * Format follows System V format as close as possible. Current
4621 * version limitations are as follows:
4622 * - no floating point registers are dumped
4623 *
4624 * Function returns 0 in case of success, negative errno otherwise.
4625 *
4626 * TODO: make this work also during runtime: it should be
4627 * possible to force coredump from running process and then
4628 * continue processing. For example qemu could set up SIGUSR2
4629 * handler (provided that target process haven't registered
4630 * handler for that) that does the dump when signal is received.
4631 */
4632 static int elf_core_dump(int signr, const CPUArchState *env)
4633 {
4634 const CPUState *cpu = env_cpu((CPUArchState *)env);
4635 const TaskState *ts = (const TaskState *)cpu->opaque;
4636 struct vm_area_struct *vma = NULL;
4637 g_autofree char *corefile = NULL;
4638 struct elf_note_info info;
4639 struct elfhdr elf;
4640 struct elf_phdr phdr;
4641 struct rlimit dumpsize;
4642 struct mm_struct *mm = NULL;
4643 off_t offset = 0, data_offset = 0;
4644 int segs = 0;
4645 int fd = -1;
4646
4647 init_note_info(&info);
4648
4649 errno = 0;
4650 getrlimit(RLIMIT_CORE, &dumpsize);
4651 if (dumpsize.rlim_cur == 0)
4652 return 0;
4653
4654 corefile = core_dump_filename(ts);
4655
4656 if ((fd = open(corefile, O_WRONLY | O_CREAT,
4657 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
4658 return (-errno);
4659
4660 /*
4661 * Walk through target process memory mappings and
4662 * set up structure containing this information. After
4663 * this point vma_xxx functions can be used.
4664 */
4665 if ((mm = vma_init()) == NULL)
4666 goto out;
4667
4668 walk_memory_regions(mm, vma_walker);
4669 segs = vma_get_mapping_count(mm);
4670
4671 /*
4672 * Construct valid coredump ELF header. We also
4673 * add one more segment for notes.
4674 */
4675 fill_elf_header(&elf, segs + 1, ELF_MACHINE, 0);
4676 if (dump_write(fd, &elf, sizeof (elf)) != 0)
4677 goto out;
4678
4679 /* fill in the in-memory version of notes */
4680 if (fill_note_info(&info, signr, env) < 0)
4681 goto out;
4682
4683 offset += sizeof (elf); /* elf header */
4684 offset += (segs + 1) * sizeof (struct elf_phdr); /* program headers */
4685
4686 /* write out notes program header */
4687 fill_elf_note_phdr(&phdr, info.notes_size, offset);
4688
4689 offset += info.notes_size;
4690 if (dump_write(fd, &phdr, sizeof (phdr)) != 0)
4691 goto out;
4692
4693 /*
4694 * ELF specification wants data to start at page boundary so
4695 * we align it here.
4696 */
4697 data_offset = offset = roundup(offset, ELF_EXEC_PAGESIZE);
4698
4699 /*
4700 * Write program headers for memory regions mapped in
4701 * the target process.
4702 */
4703 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
4704 (void) memset(&phdr, 0, sizeof (phdr));
4705
4706 phdr.p_type = PT_LOAD;
4707 phdr.p_offset = offset;
4708 phdr.p_vaddr = vma->vma_start;
4709 phdr.p_paddr = 0;
4710 phdr.p_filesz = vma_dump_size(vma);
4711 offset += phdr.p_filesz;
4712 phdr.p_memsz = vma->vma_end - vma->vma_start;
4713 phdr.p_flags = vma->vma_flags & PROT_READ ? PF_R : 0;
4714 if (vma->vma_flags & PROT_WRITE)
4715 phdr.p_flags |= PF_W;
4716 if (vma->vma_flags & PROT_EXEC)
4717 phdr.p_flags |= PF_X;
4718 phdr.p_align = ELF_EXEC_PAGESIZE;
4719
4720 bswap_phdr(&phdr, 1);
4721 if (dump_write(fd, &phdr, sizeof(phdr)) != 0) {
4722 goto out;
4723 }
4724 }
4725
4726 /*
4727 * Next we write notes just after program headers. No
4728 * alignment needed here.
4729 */
4730 if (write_note_info(&info, fd) < 0)
4731 goto out;
4732
4733 /* align data to page boundary */
4734 if (lseek(fd, data_offset, SEEK_SET) != data_offset)
4735 goto out;
4736
4737 /*
4738 * Finally we can dump process memory into corefile as well.
4739 */
4740 for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
4741 abi_ulong addr;
4742 abi_ulong end;
4743
4744 end = vma->vma_start + vma_dump_size(vma);
4745
4746 for (addr = vma->vma_start; addr < end;
4747 addr += TARGET_PAGE_SIZE) {
4748 char page[TARGET_PAGE_SIZE];
4749 int error;
4750
4751 /*
4752 * Read in page from target process memory and
4753 * write it to coredump file.
4754 */
4755 error = copy_from_user(page, addr, sizeof (page));
4756 if (error != 0) {
4757 (void) fprintf(stderr, "unable to dump " TARGET_ABI_FMT_lx "\n",
4758 addr);
4759 errno = -error;
4760 goto out;
4761 }
4762 if (dump_write(fd, page, TARGET_PAGE_SIZE) < 0)
4763 goto out;
4764 }
4765 }
4766
4767 out:
4768 free_note_info(&info);
4769 if (mm != NULL)
4770 vma_delete(mm);
4771 (void) close(fd);
4772
4773 if (errno != 0)
4774 return (-errno);
4775 return (0);
4776 }
4777 #endif /* USE_ELF_CORE_DUMP */
4778
4779 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop)
4780 {
4781 init_thread(regs, infop);
4782 }