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