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