]>
Commit | Line | Data |
---|---|---|
31e31b8a | 1 | /* This is the Linux kernel elf-loading code, ported into user space */ |
d39594e9 | 2 | #include "qemu/osdep.h" |
edf8e2af | 3 | #include <sys/param.h> |
31e31b8a | 4 | |
edf8e2af | 5 | #include <sys/resource.h> |
30ab9ef2 | 6 | #include <sys/shm.h> |
31e31b8a | 7 | |
3ef693a0 | 8 | #include "qemu.h" |
3b249d26 | 9 | #include "user-internals.h" |
db2af69d | 10 | #include "signal-common.h" |
3ad0a769 | 11 | #include "loader.h" |
5423e6d3 | 12 | #include "user-mmap.h" |
76cad711 | 13 | #include "disas/disas.h" |
ce543844 | 14 | #include "qemu/bitops.h" |
f348b6d1 | 15 | #include "qemu/path.h" |
dc5e9ac7 | 16 | #include "qemu/queue.h" |
c6a2377f | 17 | #include "qemu/guest-random.h" |
6fd59449 | 18 | #include "qemu/units.h" |
ee947430 | 19 | #include "qemu/selfmap.h" |
c7f17e7b | 20 | #include "qapi/error.h" |
db2af69d | 21 | #include "target_signal.h" |
31e31b8a | 22 | |
e58ffeb3 | 23 | #ifdef _ARCH_PPC64 |
a6cc84f4 | 24 | #undef ARCH_DLINFO |
25 | #undef ELF_PLATFORM | |
26 | #undef ELF_HWCAP | |
ad6919dc | 27 | #undef ELF_HWCAP2 |
a6cc84f4 | 28 | #undef ELF_CLASS |
29 | #undef ELF_DATA | |
30 | #undef ELF_ARCH | |
31 | #endif | |
32 | ||
edf8e2af MW |
33 | #define ELF_OSABI ELFOSABI_SYSV |
34 | ||
cb33da57 BS |
35 | /* from personality.h */ |
36 | ||
37 | /* | |
38 | * Flags for bug emulation. | |
39 | * | |
40 | * These occupy the top three bytes. | |
41 | */ | |
42 | enum { | |
d97ef72e RH |
43 | ADDR_NO_RANDOMIZE = 0x0040000, /* disable randomization of VA space */ |
44 | FDPIC_FUNCPTRS = 0x0080000, /* userspace function ptrs point to | |
45 | descriptors (signal handling) */ | |
46 | MMAP_PAGE_ZERO = 0x0100000, | |
47 | ADDR_COMPAT_LAYOUT = 0x0200000, | |
48 | READ_IMPLIES_EXEC = 0x0400000, | |
49 | ADDR_LIMIT_32BIT = 0x0800000, | |
50 | SHORT_INODE = 0x1000000, | |
51 | WHOLE_SECONDS = 0x2000000, | |
52 | STICKY_TIMEOUTS = 0x4000000, | |
53 | ADDR_LIMIT_3GB = 0x8000000, | |
cb33da57 BS |
54 | }; |
55 | ||
56 | /* | |
57 | * Personality types. | |
58 | * | |
59 | * These go in the low byte. Avoid using the top bit, it will | |
60 | * conflict with error returns. | |
61 | */ | |
62 | enum { | |
d97ef72e RH |
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,/* IRIX5 32-bit */ | |
78 | PER_IRIXN32 = 0x000a | STICKY_TIMEOUTS,/* IRIX6 new 32-bit */ | |
79 | PER_IRIX64 = 0x000b | STICKY_TIMEOUTS,/* IRIX6 64-bit */ | |
80 | PER_RISCOS = 0x000c, | |
81 | PER_SOLARIS = 0x000d | STICKY_TIMEOUTS, | |
82 | PER_UW7 = 0x000e | STICKY_TIMEOUTS | MMAP_PAGE_ZERO, | |
83 | PER_OSF4 = 0x000f, /* OSF/1 v4 */ | |
84 | PER_HPUX = 0x0010, | |
85 | PER_MASK = 0x00ff, | |
cb33da57 BS |
86 | }; |
87 | ||
88 | /* | |
89 | * Return the base personality without flags. | |
90 | */ | |
d97ef72e | 91 | #define personality(pers) (pers & PER_MASK) |
cb33da57 | 92 | |
3cb10cfa CL |
93 | int info_is_fdpic(struct image_info *info) |
94 | { | |
95 | return info->personality == PER_LINUX_FDPIC; | |
96 | } | |
97 | ||
83fb7adf FB |
98 | /* this flag is uneffective under linux too, should be deleted */ |
99 | #ifndef MAP_DENYWRITE | |
100 | #define MAP_DENYWRITE 0 | |
101 | #endif | |
102 | ||
103 | /* should probably go in elf.h */ | |
104 | #ifndef ELIBBAD | |
105 | #define ELIBBAD 80 | |
106 | #endif | |
107 | ||
ee3eb3a7 | 108 | #if TARGET_BIG_ENDIAN |
28490231 RH |
109 | #define ELF_DATA ELFDATA2MSB |
110 | #else | |
111 | #define ELF_DATA ELFDATA2LSB | |
112 | #endif | |
113 | ||
a29f998d | 114 | #ifdef TARGET_ABI_MIPSN32 |
918fc54c PB |
115 | typedef abi_ullong target_elf_greg_t; |
116 | #define tswapreg(ptr) tswap64(ptr) | |
a29f998d PB |
117 | #else |
118 | typedef abi_ulong target_elf_greg_t; | |
119 | #define tswapreg(ptr) tswapal(ptr) | |
120 | #endif | |
121 | ||
21e807fa | 122 | #ifdef USE_UID16 |
1ddd592f PB |
123 | typedef abi_ushort target_uid_t; |
124 | typedef abi_ushort target_gid_t; | |
21e807fa | 125 | #else |
f8fd4fc4 PB |
126 | typedef abi_uint target_uid_t; |
127 | typedef abi_uint target_gid_t; | |
21e807fa | 128 | #endif |
f8fd4fc4 | 129 | typedef abi_int target_pid_t; |
21e807fa | 130 | |
30ac07d4 FB |
131 | #ifdef TARGET_I386 |
132 | ||
15338fd7 FB |
133 | #define ELF_HWCAP get_elf_hwcap() |
134 | ||
135 | static uint32_t get_elf_hwcap(void) | |
136 | { | |
a2247f8e AF |
137 | X86CPU *cpu = X86_CPU(thread_cpu); |
138 | ||
139 | return cpu->env.features[FEAT_1_EDX]; | |
15338fd7 FB |
140 | } |
141 | ||
84409ddb JM |
142 | #ifdef TARGET_X86_64 |
143 | #define ELF_START_MMAP 0x2aaaaab000ULL | |
84409ddb JM |
144 | |
145 | #define ELF_CLASS ELFCLASS64 | |
84409ddb JM |
146 | #define ELF_ARCH EM_X86_64 |
147 | ||
9263ba84 RH |
148 | #define ELF_PLATFORM "x86_64" |
149 | ||
84409ddb JM |
150 | static 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 | ||
9edc5d79 | 157 | #define ELF_NREG 27 |
c227f099 | 158 | typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; |
9edc5d79 MW |
159 | |
160 | /* | |
161 | * Note that ELF_NREG should be 29 as there should be place for | |
162 | * TRAPNO and ERR "registers" as well but linux doesn't dump | |
163 | * those. | |
164 | * | |
165 | * See linux kernel: arch/x86/include/asm/elf.h | |
166 | */ | |
05390248 | 167 | static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env) |
9edc5d79 | 168 | { |
030912e0 IL |
169 | (*regs)[0] = tswapreg(env->regs[15]); |
170 | (*regs)[1] = tswapreg(env->regs[14]); | |
171 | (*regs)[2] = tswapreg(env->regs[13]); | |
172 | (*regs)[3] = tswapreg(env->regs[12]); | |
173 | (*regs)[4] = tswapreg(env->regs[R_EBP]); | |
174 | (*regs)[5] = tswapreg(env->regs[R_EBX]); | |
175 | (*regs)[6] = tswapreg(env->regs[11]); | |
176 | (*regs)[7] = tswapreg(env->regs[10]); | |
177 | (*regs)[8] = tswapreg(env->regs[9]); | |
178 | (*regs)[9] = tswapreg(env->regs[8]); | |
179 | (*regs)[10] = tswapreg(env->regs[R_EAX]); | |
180 | (*regs)[11] = tswapreg(env->regs[R_ECX]); | |
181 | (*regs)[12] = tswapreg(env->regs[R_EDX]); | |
182 | (*regs)[13] = tswapreg(env->regs[R_ESI]); | |
183 | (*regs)[14] = tswapreg(env->regs[R_EDI]); | |
184 | (*regs)[15] = tswapreg(env->regs[R_EAX]); /* XXX */ | |
185 | (*regs)[16] = tswapreg(env->eip); | |
186 | (*regs)[17] = tswapreg(env->segs[R_CS].selector & 0xffff); | |
187 | (*regs)[18] = tswapreg(env->eflags); | |
188 | (*regs)[19] = tswapreg(env->regs[R_ESP]); | |
189 | (*regs)[20] = tswapreg(env->segs[R_SS].selector & 0xffff); | |
190 | (*regs)[21] = tswapreg(env->segs[R_FS].selector & 0xffff); | |
191 | (*regs)[22] = tswapreg(env->segs[R_GS].selector & 0xffff); | |
192 | (*regs)[23] = tswapreg(env->segs[R_DS].selector & 0xffff); | |
193 | (*regs)[24] = tswapreg(env->segs[R_ES].selector & 0xffff); | |
194 | (*regs)[25] = tswapreg(env->segs[R_FS].selector & 0xffff); | |
195 | (*regs)[26] = tswapreg(env->segs[R_GS].selector & 0xffff); | |
9edc5d79 MW |
196 | } |
197 | ||
d461b73e RH |
198 | #if ULONG_MAX > UINT32_MAX |
199 | #define INIT_GUEST_COMMPAGE | |
200 | static bool init_guest_commpage(void) | |
201 | { | |
202 | /* | |
203 | * The vsyscall page is at a high negative address aka kernel space, | |
204 | * which means that we cannot actually allocate it with target_mmap. | |
205 | * We still should be able to use page_set_flags, unless the user | |
206 | * has specified -R reserved_va, which would trigger an assert(). | |
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 | |
84409ddb JM |
219 | #else |
220 | ||
30ac07d4 FB |
221 | #define ELF_START_MMAP 0x80000000 |
222 | ||
30ac07d4 FB |
223 | /* |
224 | * This is used to ensure we don't load something for the wrong architecture. | |
225 | */ | |
226 | #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) ) | |
227 | ||
228 | /* | |
229 | * These are used to set parameters in the core dumps. | |
230 | */ | |
d97ef72e | 231 | #define ELF_CLASS ELFCLASS32 |
d97ef72e | 232 | #define ELF_ARCH EM_386 |
30ac07d4 | 233 | |
9263ba84 | 234 | #define ELF_PLATFORM get_elf_platform() |
872f3d04 | 235 | #define EXSTACK_DEFAULT true |
9263ba84 RH |
236 | |
237 | static 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 | ||
d97ef72e RH |
250 | static inline void init_thread(struct target_pt_regs *regs, |
251 | struct image_info *infop) | |
b346ff46 FB |
252 | { |
253 | regs->esp = infop->start_stack; | |
254 | regs->eip = infop->entry; | |
e5fe0c52 PB |
255 | |
256 | /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program | |
257 | starts %edx contains a pointer to a function which might be | |
258 | registered using `atexit'. This provides a mean for the | |
259 | dynamic linker to call DT_FINI functions for shared libraries | |
260 | that have been loaded before the code runs. | |
261 | ||
262 | A value of 0 tells we have no such handler. */ | |
263 | regs->edx = 0; | |
b346ff46 | 264 | } |
9edc5d79 | 265 | |
9edc5d79 | 266 | #define ELF_NREG 17 |
c227f099 | 267 | typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; |
9edc5d79 MW |
268 | |
269 | /* | |
270 | * Note that ELF_NREG should be 19 as there should be place for | |
271 | * TRAPNO and ERR "registers" as well but linux doesn't dump | |
272 | * those. | |
273 | * | |
274 | * See linux kernel: arch/x86/include/asm/elf.h | |
275 | */ | |
05390248 | 276 | static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env) |
9edc5d79 | 277 | { |
030912e0 IL |
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]); /* XXX */ | |
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); | |
9edc5d79 | 295 | } |
84409ddb | 296 | #endif |
b346ff46 | 297 | |
9edc5d79 | 298 | #define USE_ELF_CORE_DUMP |
d97ef72e | 299 | #define ELF_EXEC_PAGESIZE 4096 |
b346ff46 FB |
300 | |
301 | #endif | |
302 | ||
303 | #ifdef TARGET_ARM | |
304 | ||
24e76ff0 PM |
305 | #ifndef TARGET_AARCH64 |
306 | /* 32 bit ARM definitions */ | |
307 | ||
b346ff46 FB |
308 | #define ELF_START_MMAP 0x80000000 |
309 | ||
b597c3f7 | 310 | #define ELF_ARCH EM_ARM |
d97ef72e | 311 | #define ELF_CLASS ELFCLASS32 |
872f3d04 | 312 | #define EXSTACK_DEFAULT true |
b346ff46 | 313 | |
d97ef72e RH |
314 | static inline void init_thread(struct target_pt_regs *regs, |
315 | struct image_info *infop) | |
b346ff46 | 316 | { |
992f48a0 | 317 | abi_long stack = infop->start_stack; |
b346ff46 | 318 | memset(regs, 0, sizeof(*regs)); |
99033cae | 319 | |
167e4cdc PM |
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; | |
2f619698 | 326 | /* FIXME - what to for failure of get_user()? */ |
167e4cdc PM |
327 | get_user_ual(regs->uregs[2], stack + 8); /* envp */ |
328 | get_user_ual(regs->uregs[1], stack + 4); /* envp */ | |
a1516e92 | 329 | /* XXX: it seems that r0 is zeroed after ! */ |
167e4cdc | 330 | regs->uregs[0] = 0; |
e5fe0c52 | 331 | /* For uClinux PIC binaries. */ |
863cf0b7 | 332 | /* XXX: Linux does this only on ARM with no MMU (do we care ?) */ |
167e4cdc | 333 | regs->uregs[10] = infop->start_data; |
3cb10cfa CL |
334 | |
335 | /* Support ARM FDPIC. */ | |
336 | if (info_is_fdpic(infop)) { | |
337 | /* As described in the ABI document, r7 points to the loadmap info | |
338 | * prepared by the kernel. If an interpreter is needed, r8 points | |
339 | * to the interpreter loadmap and r9 points to the interpreter | |
340 | * PT_DYNAMIC info. If no interpreter is needed, r8 is zero, and | |
341 | * r9 points to the main program PT_DYNAMIC info. | |
342 | */ | |
343 | regs->uregs[7] = infop->loadmap_addr; | |
344 | if (infop->interpreter_loadmap_addr) { | |
345 | /* Executable is dynamically loaded. */ | |
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 | } | |
b346ff46 FB |
353 | } |
354 | ||
edf8e2af | 355 | #define ELF_NREG 18 |
c227f099 | 356 | typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; |
edf8e2af | 357 | |
05390248 | 358 | static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUARMState *env) |
edf8e2af | 359 | { |
86cd7b2d PB |
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]); /* XXX */ | |
edf8e2af MW |
379 | } |
380 | ||
30ac07d4 | 381 | #define USE_ELF_CORE_DUMP |
d97ef72e | 382 | #define ELF_EXEC_PAGESIZE 4096 |
30ac07d4 | 383 | |
afce2927 FB |
384 | enum |
385 | { | |
d97ef72e RH |
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, | |
43ce393e PM |
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, | |
24682654 PM |
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, | |
afce2927 FB |
408 | }; |
409 | ||
ad6919dc PM |
410 | enum { |
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 | ||
6b1275ff PM |
418 | /* The commpage only exists for 32 bit kernels */ |
419 | ||
66346faf | 420 | #define HI_COMMPAGE (intptr_t)0xffff0f00u |
806d1021 | 421 | |
ee947430 AB |
422 | static bool init_guest_commpage(void) |
423 | { | |
fbd3c4cf RH |
424 | abi_ptr commpage = HI_COMMPAGE & -qemu_host_page_size; |
425 | void *want = g2h_untagged(commpage); | |
ee947430 | 426 | void *addr = mmap(want, qemu_host_page_size, PROT_READ | PROT_WRITE, |
5c3e87f3 | 427 | MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0); |
97cc7560 | 428 | |
ee947430 AB |
429 | if (addr == MAP_FAILED) { |
430 | perror("Allocating guest commpage"); | |
431 | exit(EXIT_FAILURE); | |
97cc7560 | 432 | } |
ee947430 AB |
433 | if (addr != want) { |
434 | return false; | |
97cc7560 DDAG |
435 | } |
436 | ||
ee947430 | 437 | /* Set kernel helper versions; rest of page is 0. */ |
3e8f1628 | 438 | __put_user(5, (uint32_t *)g2h_untagged(0xffff0ffcu)); |
97cc7560 | 439 | |
ee947430 | 440 | if (mprotect(addr, qemu_host_page_size, PROT_READ)) { |
97cc7560 | 441 | perror("Protecting guest commpage"); |
ee947430 | 442 | exit(EXIT_FAILURE); |
97cc7560 | 443 | } |
fbd3c4cf RH |
444 | |
445 | page_set_flags(commpage, commpage + qemu_host_page_size, | |
446 | PAGE_READ | PAGE_EXEC | PAGE_VALID); | |
ee947430 | 447 | return true; |
97cc7560 | 448 | } |
adf050b1 BC |
449 | |
450 | #define ELF_HWCAP get_elf_hwcap() | |
ad6919dc | 451 | #define ELF_HWCAP2 get_elf_hwcap2() |
adf050b1 BC |
452 | |
453 | static uint32_t get_elf_hwcap(void) | |
454 | { | |
a2247f8e | 455 | ARMCPU *cpu = ARM_CPU(thread_cpu); |
adf050b1 BC |
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; | |
adf050b1 BC |
462 | |
463 | /* probe for the extra features */ | |
464 | #define GET_FEATURE(feat, hwcap) \ | |
a2247f8e | 465 | do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0) |
962fcbf2 RH |
466 | |
467 | #define GET_FEATURE_ID(feat, hwcap) \ | |
468 | do { if (cpu_isar_feature(feat, cpu)) { hwcaps |= hwcap; } } while (0) | |
469 | ||
24682654 PM |
470 | /* EDSP is in v5TE and above, but all our v5 CPUs are v5TE */ |
471 | GET_FEATURE(ARM_FEATURE_V5, ARM_HWCAP_ARM_EDSP); | |
adf050b1 BC |
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); | |
24682654 | 475 | GET_FEATURE(ARM_FEATURE_V6K, ARM_HWCAP_ARM_TLS); |
bfa8a370 | 476 | GET_FEATURE(ARM_FEATURE_LPAE, ARM_HWCAP_ARM_LPAE); |
873b73c0 PM |
477 | GET_FEATURE_ID(aa32_arm_div, ARM_HWCAP_ARM_IDIVA); |
478 | GET_FEATURE_ID(aa32_thumb_div, ARM_HWCAP_ARM_IDIVT); | |
bfa8a370 RH |
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); | |
adf050b1 BC |
491 | |
492 | return hwcaps; | |
493 | } | |
afce2927 | 494 | |
ad6919dc PM |
495 | static uint32_t get_elf_hwcap2(void) |
496 | { | |
497 | ARMCPU *cpu = ARM_CPU(thread_cpu); | |
498 | uint32_t hwcaps = 0; | |
499 | ||
962fcbf2 RH |
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); | |
ad6919dc PM |
505 | return hwcaps; |
506 | } | |
507 | ||
508 | #undef GET_FEATURE | |
962fcbf2 | 509 | #undef GET_FEATURE_ID |
ad6919dc | 510 | |
13ec4ec3 RH |
511 | #define ELF_PLATFORM get_elf_platform() |
512 | ||
513 | static const char *get_elf_platform(void) | |
514 | { | |
515 | CPUARMState *env = thread_cpu->env_ptr; | |
516 | ||
ee3eb3a7 | 517 | #if TARGET_BIG_ENDIAN |
13ec4ec3 RH |
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 | ||
24e76ff0 PM |
542 | #else |
543 | /* 64 bit ARM definitions */ | |
544 | #define ELF_START_MMAP 0x80000000 | |
545 | ||
b597c3f7 | 546 | #define ELF_ARCH EM_AARCH64 |
24e76ff0 | 547 | #define ELF_CLASS ELFCLASS64 |
ee3eb3a7 | 548 | #if TARGET_BIG_ENDIAN |
e20e3ec9 RH |
549 | # define ELF_PLATFORM "aarch64_be" |
550 | #else | |
551 | # define ELF_PLATFORM "aarch64" | |
552 | #endif | |
24e76ff0 PM |
553 | |
554 | static 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 | |
565 | typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; | |
566 | ||
567 | static 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 | ||
582 | enum { | |
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, | |
955f56d4 AB |
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, | |
0083a1fa RH |
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, | |
2041df4a RH |
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, | |
68948d18 RH |
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, | |
f9982cea RH |
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, | |
24e76ff0 PM |
647 | }; |
648 | ||
2041df4a RH |
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) | |
24e76ff0 PM |
654 | |
655 | static 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; | |
37020ff1 | 662 | hwcaps |= ARM_HWCAP_A64_CPUID; |
24e76ff0 PM |
663 | |
664 | /* probe for the extra features */ | |
962fcbf2 RH |
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); | |
5763190f | 675 | GET_FEATURE_ID(aa64_fp16, ARM_HWCAP_A64_FPHP | ARM_HWCAP_A64_ASIMDHP); |
962fcbf2 RH |
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); | |
cd208a1c | 680 | GET_FEATURE_ID(aa64_sve, ARM_HWCAP_A64_SVE); |
29d26ab2 | 681 | GET_FEATURE_ID(aa64_pauth, ARM_HWCAP_A64_PACA | ARM_HWCAP_A64_PACG); |
1c9af3a9 RH |
682 | GET_FEATURE_ID(aa64_fhm, ARM_HWCAP_A64_ASIMDFHM); |
683 | GET_FEATURE_ID(aa64_jscvt, ARM_HWCAP_A64_JSCVT); | |
9888bd1e | 684 | GET_FEATURE_ID(aa64_sb, ARM_HWCAP_A64_SB); |
b89d9c98 | 685 | GET_FEATURE_ID(aa64_condm_4, ARM_HWCAP_A64_FLAGM); |
0d57b499 | 686 | GET_FEATURE_ID(aa64_dcpop, ARM_HWCAP_A64_DCPOP); |
2677cf9f | 687 | GET_FEATURE_ID(aa64_rcpc_8_3, ARM_HWCAP_A64_LRCPC); |
a1229109 | 688 | GET_FEATURE_ID(aa64_rcpc_8_4, ARM_HWCAP_A64_ILRCPC); |
962fcbf2 | 689 | |
2041df4a RH |
690 | return hwcaps; |
691 | } | |
692 | ||
693 | static uint32_t get_elf_hwcap2(void) | |
694 | { | |
695 | ARMCPU *cpu = ARM_CPU(thread_cpu); | |
696 | uint32_t hwcaps = 0; | |
697 | ||
0d57b499 | 698 | GET_FEATURE_ID(aa64_dcpodp, ARM_HWCAP2_A64_DCPODP); |
cdc8d8b2 RH |
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); | |
2041df4a RH |
705 | GET_FEATURE_ID(aa64_condm_5, ARM_HWCAP2_A64_FLAGM2); |
706 | GET_FEATURE_ID(aa64_frint, ARM_HWCAP2_A64_FRINT); | |
cdc8d8b2 RH |
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); | |
6c47a905 | 710 | GET_FEATURE_ID(aa64_sve_bf16, ARM_HWCAP2_A64_SVEBF16); |
cdc8d8b2 | 711 | GET_FEATURE_ID(aa64_i8mm, ARM_HWCAP2_A64_I8MM); |
6c47a905 | 712 | GET_FEATURE_ID(aa64_bf16, ARM_HWCAP2_A64_BF16); |
68948d18 RH |
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); | |
f9982cea RH |
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); | |
24e76ff0 PM |
724 | |
725 | return hwcaps; | |
726 | } | |
727 | ||
2041df4a RH |
728 | #undef GET_FEATURE_ID |
729 | ||
24e76ff0 PM |
730 | #endif /* not TARGET_AARCH64 */ |
731 | #endif /* TARGET_ARM */ | |
30ac07d4 | 732 | |
853d6f7a | 733 | #ifdef TARGET_SPARC |
a315a145 | 734 | #ifdef TARGET_SPARC64 |
853d6f7a FB |
735 | |
736 | #define ELF_START_MMAP 0x80000000 | |
cf973e46 AT |
737 | #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \ |
738 | | HWCAP_SPARC_MULDIV | HWCAP_SPARC_V9) | |
992f48a0 | 739 | #ifndef TARGET_ABI32 |
cb33da57 | 740 | #define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS ) |
992f48a0 BS |
741 | #else |
742 | #define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC ) | |
743 | #endif | |
853d6f7a | 744 | |
a315a145 | 745 | #define ELF_CLASS ELFCLASS64 |
5ef54116 | 746 | #define ELF_ARCH EM_SPARCV9 |
a315a145 FB |
747 | #else |
748 | #define ELF_START_MMAP 0x80000000 | |
cf973e46 AT |
749 | #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \ |
750 | | HWCAP_SPARC_MULDIV) | |
853d6f7a | 751 | #define ELF_CLASS ELFCLASS32 |
853d6f7a | 752 | #define ELF_ARCH EM_SPARC |
089a2256 | 753 | #endif /* TARGET_SPARC64 */ |
853d6f7a | 754 | |
d97ef72e RH |
755 | static inline void init_thread(struct target_pt_regs *regs, |
756 | struct image_info *infop) | |
853d6f7a | 757 | { |
089a2256 | 758 | /* Note that target_cpu_copy_regs does not read psr/tstate. */ |
f5155289 FB |
759 | regs->pc = infop->entry; |
760 | regs->npc = regs->pc + 4; | |
761 | regs->y = 0; | |
089a2256 RH |
762 | regs->u_regs[14] = (infop->start_stack - 16 * sizeof(abi_ulong) |
763 | - TARGET_STACK_BIAS); | |
853d6f7a | 764 | } |
089a2256 | 765 | #endif /* TARGET_SPARC */ |
853d6f7a | 766 | |
67867308 FB |
767 | #ifdef TARGET_PPC |
768 | ||
4ecd4d16 | 769 | #define ELF_MACHINE PPC_ELF_MACHINE |
67867308 FB |
770 | #define ELF_START_MMAP 0x80000000 |
771 | ||
74154d7e | 772 | #if defined(TARGET_PPC64) |
84409ddb JM |
773 | |
774 | #define elf_check_arch(x) ( (x) == EM_PPC64 ) | |
775 | ||
d97ef72e | 776 | #define ELF_CLASS ELFCLASS64 |
84409ddb JM |
777 | |
778 | #else | |
779 | ||
d97ef72e | 780 | #define ELF_CLASS ELFCLASS32 |
872f3d04 | 781 | #define EXSTACK_DEFAULT true |
84409ddb JM |
782 | |
783 | #endif | |
784 | ||
d97ef72e | 785 | #define ELF_ARCH EM_PPC |
67867308 | 786 | |
df84e4f3 NF |
787 | /* Feature masks for the Aux Vector Hardware Capabilities (AT_HWCAP). |
788 | See arch/powerpc/include/asm/cputable.h. */ | |
789 | enum { | |
3efa9a67 | 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, | |
a60438dd TM |
819 | |
820 | /* Feature definitions in AT_HWCAP2. */ | |
821 | QEMU_PPC_FEATURE2_ARCH_2_07 = 0x80000000, /* ISA 2.07 */ | |
822 | QEMU_PPC_FEATURE2_HAS_HTM = 0x40000000, /* Hardware Transactional Memory */ | |
823 | QEMU_PPC_FEATURE2_HAS_DSCR = 0x20000000, /* Data Stream Control Register */ | |
824 | QEMU_PPC_FEATURE2_HAS_EBB = 0x10000000, /* Event Base Branching */ | |
825 | QEMU_PPC_FEATURE2_HAS_ISEL = 0x08000000, /* Integer Select */ | |
826 | QEMU_PPC_FEATURE2_HAS_TAR = 0x04000000, /* Target Address Register */ | |
24c373ec LV |
827 | QEMU_PPC_FEATURE2_VEC_CRYPTO = 0x02000000, |
828 | QEMU_PPC_FEATURE2_HTM_NOSC = 0x01000000, | |
be0c46d4 | 829 | QEMU_PPC_FEATURE2_ARCH_3_00 = 0x00800000, /* ISA 3.00 */ |
24c373ec LV |
830 | QEMU_PPC_FEATURE2_HAS_IEEE128 = 0x00400000, /* VSX IEEE Bin Float 128-bit */ |
831 | QEMU_PPC_FEATURE2_DARN = 0x00200000, /* darn random number insn */ | |
832 | QEMU_PPC_FEATURE2_SCV = 0x00100000, /* scv syscall */ | |
833 | QEMU_PPC_FEATURE2_HTM_NO_SUSPEND = 0x00080000, /* TM w/o suspended state */ | |
96c343cc JS |
834 | QEMU_PPC_FEATURE2_ARCH_3_1 = 0x00040000, /* ISA 3.1 */ |
835 | QEMU_PPC_FEATURE2_MMA = 0x00020000, /* Matrix-Multiply Assist */ | |
df84e4f3 NF |
836 | }; |
837 | ||
838 | #define ELF_HWCAP get_elf_hwcap() | |
839 | ||
840 | static uint32_t get_elf_hwcap(void) | |
841 | { | |
a2247f8e | 842 | PowerPCCPU *cpu = POWERPC_CPU(thread_cpu); |
df84e4f3 NF |
843 | uint32_t features = 0; |
844 | ||
845 | /* We don't have to be terribly complete here; the high points are | |
846 | Altivec/FP/SPE support. Anything else is just a bonus. */ | |
d97ef72e | 847 | #define GET_FEATURE(flag, feature) \ |
a2247f8e | 848 | do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0) |
58eb5308 MW |
849 | #define GET_FEATURE2(flags, feature) \ |
850 | do { \ | |
851 | if ((cpu->env.insns_flags2 & flags) == flags) { \ | |
852 | features |= feature; \ | |
853 | } \ | |
854 | } while (0) | |
3efa9a67 | 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); | |
0e019746 TM |
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); | |
df84e4f3 | 868 | #undef GET_FEATURE |
0e019746 | 869 | #undef GET_FEATURE2 |
df84e4f3 NF |
870 | |
871 | return features; | |
872 | } | |
873 | ||
a60438dd TM |
874 | #define ELF_HWCAP2 get_elf_hwcap2() |
875 | ||
876 | static 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 | | |
24c373ec LV |
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 | | |
8a589aeb | 892 | QEMU_PPC_FEATURE2_DARN | QEMU_PPC_FEATURE2_HAS_IEEE128); |
96c343cc JS |
893 | GET_FEATURE2(PPC2_ISA310, QEMU_PPC_FEATURE2_ARCH_3_1 | |
894 | QEMU_PPC_FEATURE2_MMA); | |
a60438dd TM |
895 | |
896 | #undef GET_FEATURE | |
897 | #undef GET_FEATURE2 | |
898 | ||
899 | return features; | |
900 | } | |
901 | ||
f5155289 FB |
902 | /* |
903 | * The requirements here are: | |
904 | * - keep the final alignment of sp (sp & 0xf) | |
905 | * - make sure the 32-bit value at the first 16 byte aligned position of | |
906 | * AUXV is greater than 16 for glibc compatibility. | |
907 | * AT_IGNOREPPC is used for that. | |
908 | * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC, | |
909 | * even if DLINFO_ARCH_ITEMS goes to zero or is undefined. | |
910 | */ | |
0bccf03d | 911 | #define DLINFO_ARCH_ITEMS 5 |
d97ef72e RH |
912 | #define ARCH_DLINFO \ |
913 | do { \ | |
623e250a | 914 | PowerPCCPU *cpu = POWERPC_CPU(thread_cpu); \ |
d97ef72e | 915 | /* \ |
82991bed PM |
916 | * Handle glibc compatibility: these magic entries must \ |
917 | * be at the lowest addresses in the final auxv. \ | |
d97ef72e RH |
918 | */ \ |
919 | NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \ | |
920 | NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \ | |
82991bed PM |
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); \ | |
d97ef72e | 924 | } while (0) |
f5155289 | 925 | |
67867308 FB |
926 | static inline void init_thread(struct target_pt_regs *_regs, struct image_info *infop) |
927 | { | |
67867308 | 928 | _regs->gpr[1] = infop->start_stack; |
74154d7e | 929 | #if defined(TARGET_PPC64) |
d90b94cd | 930 | if (get_ppc64_abi(infop) < 2) { |
2ccf97ec PM |
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; | |
d90b94cd DK |
936 | } else { |
937 | _regs->gpr[12] = infop->entry; /* r12 set to global entry address */ | |
938 | } | |
84409ddb | 939 | #endif |
67867308 FB |
940 | _regs->nip = infop->entry; |
941 | } | |
942 | ||
e2f3e741 NF |
943 | /* See linux kernel: arch/powerpc/include/asm/elf.h. */ |
944 | #define ELF_NREG 48 | |
945 | typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; | |
946 | ||
05390248 | 947 | static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUPPCState *env) |
e2f3e741 NF |
948 | { |
949 | int i; | |
950 | target_ulong ccr = 0; | |
951 | ||
952 | for (i = 0; i < ARRAY_SIZE(env->gpr); i++) { | |
86cd7b2d | 953 | (*regs)[i] = tswapreg(env->gpr[i]); |
e2f3e741 NF |
954 | } |
955 | ||
86cd7b2d PB |
956 | (*regs)[32] = tswapreg(env->nip); |
957 | (*regs)[33] = tswapreg(env->msr); | |
958 | (*regs)[35] = tswapreg(env->ctr); | |
959 | (*regs)[36] = tswapreg(env->lr); | |
10de0521 | 960 | (*regs)[37] = tswapreg(cpu_read_xer(env)); |
e2f3e741 NF |
961 | |
962 | for (i = 0; i < ARRAY_SIZE(env->crf); i++) { | |
963 | ccr |= env->crf[i] << (32 - ((i + 1) * 4)); | |
964 | } | |
86cd7b2d | 965 | (*regs)[38] = tswapreg(ccr); |
e2f3e741 NF |
966 | } |
967 | ||
968 | #define USE_ELF_CORE_DUMP | |
d97ef72e | 969 | #define ELF_EXEC_PAGESIZE 4096 |
67867308 FB |
970 | |
971 | #endif | |
972 | ||
3418fe25 SG |
973 | #ifdef TARGET_LOONGARCH64 |
974 | ||
975 | #define ELF_START_MMAP 0x80000000 | |
976 | ||
977 | #define ELF_CLASS ELFCLASS64 | |
978 | #define ELF_ARCH EM_LOONGARCH | |
872f3d04 | 979 | #define EXSTACK_DEFAULT true |
3418fe25 SG |
980 | |
981 | #define elf_check_arch(x) ((x) == EM_LOONGARCH) | |
982 | ||
983 | static inline void init_thread(struct target_pt_regs *regs, | |
984 | struct image_info *infop) | |
985 | { | |
986 | /*Set crmd PG,DA = 1,0 */ | |
987 | regs->csr.crmd = 2 << 3; | |
988 | regs->csr.era = infop->entry; | |
989 | regs->regs[3] = infop->start_stack; | |
990 | } | |
991 | ||
992 | /* See linux kernel: arch/loongarch/include/asm/elf.h */ | |
993 | #define ELF_NREG 45 | |
994 | typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; | |
995 | ||
996 | enum { | |
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 | ||
1002 | static 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 | /* See arch/loongarch/include/uapi/asm/hwcap.h */ | |
1023 | enum { | |
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 | ||
1039 | static 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 /* TARGET_LOONGARCH64 */ | |
1064 | ||
048f6b4d FB |
1065 | #ifdef TARGET_MIPS |
1066 | ||
1067 | #define ELF_START_MMAP 0x80000000 | |
1068 | ||
388bb21a TS |
1069 | #ifdef TARGET_MIPS64 |
1070 | #define ELF_CLASS ELFCLASS64 | |
1071 | #else | |
048f6b4d | 1072 | #define ELF_CLASS ELFCLASS32 |
388bb21a | 1073 | #endif |
048f6b4d | 1074 | #define ELF_ARCH EM_MIPS |
872f3d04 | 1075 | #define EXSTACK_DEFAULT true |
048f6b4d | 1076 | |
ace3d654 CMAB |
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 | ||
fbf47c18 JY |
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 | ||
1089 | static const char *get_elf_base_platform(void) | |
1090 | { | |
1091 | MIPSCPU *cpu = MIPS_CPU(thread_cpu); | |
1092 | ||
1093 | /* 64 bit ISAs goes first */ | |
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 | /* 32 bit ISAs */ | |
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 | /* Fallback */ | |
1110 | return "mips"; | |
1111 | } | |
1112 | #undef MATCH_PLATFORM_INSN | |
1113 | ||
d97ef72e RH |
1114 | static inline void init_thread(struct target_pt_regs *regs, |
1115 | struct image_info *infop) | |
048f6b4d | 1116 | { |
623a930e | 1117 | regs->cp0_status = 2 << CP0St_KSU; |
048f6b4d FB |
1118 | regs->cp0_epc = infop->entry; |
1119 | regs->regs[29] = infop->start_stack; | |
1120 | } | |
1121 | ||
51e52606 NF |
1122 | /* See linux kernel: arch/mips/include/asm/elf.h. */ |
1123 | #define ELF_NREG 45 | |
1124 | typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; | |
1125 | ||
1126 | /* See linux kernel: arch/mips/include/asm/reg.h. */ | |
1127 | enum { | |
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 | /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */ | |
05390248 | 1144 | static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMIPSState *env) |
51e52606 NF |
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++) { | |
a29f998d | 1154 | (*regs)[TARGET_EF_R0 + i] = tswapreg(env->active_tc.gpr[i]); |
51e52606 NF |
1155 | } |
1156 | ||
1157 | (*regs)[TARGET_EF_R26] = 0; | |
1158 | (*regs)[TARGET_EF_R27] = 0; | |
a29f998d PB |
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); | |
51e52606 NF |
1165 | } |
1166 | ||
1167 | #define USE_ELF_CORE_DUMP | |
388bb21a TS |
1168 | #define ELF_EXEC_PAGESIZE 4096 |
1169 | ||
46a1ee4f JC |
1170 | /* See arch/mips/include/uapi/asm/hwcap.h. */ |
1171 | enum { | |
1172 | HWCAP_MIPS_R6 = (1 << 0), | |
1173 | HWCAP_MIPS_MSA = (1 << 1), | |
9ea313ba PMD |
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), | |
46a1ee4f JC |
1187 | }; |
1188 | ||
1189 | #define ELF_HWCAP get_elf_hwcap() | |
1190 | ||
7d9a3d96 | 1191 | #define GET_FEATURE_INSN(_flag, _hwcap) \ |
6dd97bfc PMD |
1192 | do { if (cpu->env.insn_flags & (_flag)) { hwcaps |= _hwcap; } } while (0) |
1193 | ||
388765a0 PMD |
1194 | #define GET_FEATURE_REG_SET(_reg, _mask, _hwcap) \ |
1195 | do { if (cpu->env._reg & (_mask)) { hwcaps |= _hwcap; } } while (0) | |
1196 | ||
ce543844 PMD |
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 | ||
46a1ee4f JC |
1204 | static uint32_t get_elf_hwcap(void) |
1205 | { | |
1206 | MIPSCPU *cpu = MIPS_CPU(thread_cpu); | |
1207 | uint32_t hwcaps = 0; | |
1208 | ||
ce543844 PMD |
1209 | GET_FEATURE_REG_EQU(CP0_Config0, CP0C0_AR, CP0C0_AR_LENGTH, |
1210 | 2, HWCAP_MIPS_R6); | |
388765a0 | 1211 | GET_FEATURE_REG_SET(CP0_Config3, 1 << CP0C3_MSAP, HWCAP_MIPS_MSA); |
53673d0f PMD |
1212 | GET_FEATURE_INSN(ASE_LMMI, HWCAP_LOONGSON_MMI); |
1213 | GET_FEATURE_INSN(ASE_LEXT, HWCAP_LOONGSON_EXT); | |
46a1ee4f | 1214 | |
46a1ee4f JC |
1215 | return hwcaps; |
1216 | } | |
1217 | ||
ce543844 | 1218 | #undef GET_FEATURE_REG_EQU |
388765a0 | 1219 | #undef GET_FEATURE_REG_SET |
7d9a3d96 | 1220 | #undef GET_FEATURE_INSN |
6dd97bfc | 1221 | |
048f6b4d FB |
1222 | #endif /* TARGET_MIPS */ |
1223 | ||
b779e29e EI |
1224 | #ifdef TARGET_MICROBLAZE |
1225 | ||
1226 | #define ELF_START_MMAP 0x80000000 | |
1227 | ||
0d5d4699 | 1228 | #define elf_check_arch(x) ( (x) == EM_MICROBLAZE || (x) == EM_MICROBLAZE_OLD) |
b779e29e EI |
1229 | |
1230 | #define ELF_CLASS ELFCLASS32 | |
0d5d4699 | 1231 | #define ELF_ARCH EM_MICROBLAZE |
b779e29e | 1232 | |
d97ef72e RH |
1233 | static inline void init_thread(struct target_pt_regs *regs, |
1234 | struct image_info *infop) | |
b779e29e EI |
1235 | { |
1236 | regs->pc = infop->entry; | |
1237 | regs->r1 = infop->start_stack; | |
1238 | ||
1239 | } | |
1240 | ||
b779e29e EI |
1241 | #define ELF_EXEC_PAGESIZE 4096 |
1242 | ||
e4cbd44d EI |
1243 | #define USE_ELF_CORE_DUMP |
1244 | #define ELF_NREG 38 | |
1245 | typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; | |
1246 | ||
1247 | /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */ | |
05390248 | 1248 | static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMBState *env) |
e4cbd44d EI |
1249 | { |
1250 | int i, pos = 0; | |
1251 | ||
1252 | for (i = 0; i < 32; i++) { | |
86cd7b2d | 1253 | (*regs)[pos++] = tswapreg(env->regs[i]); |
e4cbd44d EI |
1254 | } |
1255 | ||
af20a93a | 1256 | (*regs)[pos++] = tswapreg(env->pc); |
1074c0fb | 1257 | (*regs)[pos++] = tswapreg(mb_cpu_read_msr(env)); |
af20a93a RH |
1258 | (*regs)[pos++] = 0; |
1259 | (*regs)[pos++] = tswapreg(env->ear); | |
1260 | (*regs)[pos++] = 0; | |
1261 | (*regs)[pos++] = tswapreg(env->esr); | |
e4cbd44d EI |
1262 | } |
1263 | ||
b779e29e EI |
1264 | #endif /* TARGET_MICROBLAZE */ |
1265 | ||
a0a839b6 MV |
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 | ||
1275 | static void init_thread(struct target_pt_regs *regs, struct image_info *infop) | |
1276 | { | |
1277 | regs->ea = infop->entry; | |
1278 | regs->sp = infop->start_stack; | |
a0a839b6 MV |
1279 | } |
1280 | ||
f5ef0e51 RH |
1281 | #define LO_COMMPAGE TARGET_PAGE_SIZE |
1282 | ||
1283 | static bool init_guest_commpage(void) | |
1284 | { | |
1285 | static const uint8_t kuser_page[4 + 2 * 64] = { | |
1286 | /* __kuser_helper_version */ | |
1287 | [0x00] = 0x02, 0x00, 0x00, 0x00, | |
1288 | ||
1289 | /* __kuser_cmpxchg */ | |
1290 | [0x04] = 0x3a, 0x6c, 0x3b, 0x00, /* trap 16 */ | |
1291 | 0x3a, 0x28, 0x00, 0xf8, /* ret */ | |
1292 | ||
1293 | /* __kuser_sigtramp */ | |
1294 | [0x44] = 0xc4, 0x22, 0x80, 0x00, /* movi r2, __NR_rt_sigreturn */ | |
1295 | 0x3a, 0x68, 0x3b, 0x00, /* trap 0 */ | |
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 | ||
a0a839b6 MV |
1322 | #define ELF_EXEC_PAGESIZE 4096 |
1323 | ||
1324 | #define USE_ELF_CORE_DUMP | |
1325 | #define ELF_NREG 49 | |
1326 | typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; | |
1327 | ||
1328 | /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */ | |
1329 | static 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++) /* r0-r7 */ | |
1336 | (*regs)[i] = tswapreg(env->regs[i + 7]); | |
1337 | ||
1338 | for (i = 8; i < 16; i++) /* r8-r15 */ | |
1339 | (*regs)[i] = tswapreg(env->regs[i - 8]); | |
1340 | ||
1341 | for (i = 16; i < 24; i++) /* r16-r23 */ | |
1342 | (*regs)[i] = tswapreg(env->regs[i + 7]); | |
1343 | (*regs)[24] = -1; /* R_ET */ | |
1344 | (*regs)[25] = -1; /* R_BT */ | |
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; /* R_SSTATUS */ | |
1350 | (*regs)[31] = tswapreg(env->regs[R_RA]); | |
1351 | ||
17a406ee | 1352 | (*regs)[32] = tswapreg(env->pc); |
a0a839b6 MV |
1353 | |
1354 | (*regs)[33] = -1; /* R_STATUS */ | |
1355 | (*regs)[34] = tswapreg(env->regs[CR_ESTATUS]); | |
1356 | ||
1357 | for (i = 35; i < 49; i++) /* ... */ | |
1358 | (*regs)[i] = -1; | |
1359 | } | |
1360 | ||
1361 | #endif /* TARGET_NIOS2 */ | |
1362 | ||
d962783e JL |
1363 | #ifdef TARGET_OPENRISC |
1364 | ||
1365 | #define ELF_START_MMAP 0x08000000 | |
1366 | ||
d962783e JL |
1367 | #define ELF_ARCH EM_OPENRISC |
1368 | #define ELF_CLASS ELFCLASS32 | |
1369 | #define ELF_DATA ELFDATA2MSB | |
1370 | ||
1371 | static 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 | /* See linux kernel arch/openrisc/include/asm/elf.h. */ | |
1382 | #define ELF_NREG 34 /* gprs and pc, sr */ | |
1383 | typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; | |
1384 | ||
1385 | static 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++) { | |
d89e71e8 | 1391 | (*regs)[i] = tswapreg(cpu_get_gpr(env, i)); |
d962783e | 1392 | } |
86cd7b2d | 1393 | (*regs)[32] = tswapreg(env->pc); |
84775c43 | 1394 | (*regs)[33] = tswapreg(cpu_get_sr(env)); |
d962783e JL |
1395 | } |
1396 | #define ELF_HWCAP 0 | |
1397 | #define ELF_PLATFORM NULL | |
1398 | ||
1399 | #endif /* TARGET_OPENRISC */ | |
1400 | ||
fdf9b3e8 FB |
1401 | #ifdef TARGET_SH4 |
1402 | ||
1403 | #define ELF_START_MMAP 0x80000000 | |
1404 | ||
fdf9b3e8 | 1405 | #define ELF_CLASS ELFCLASS32 |
fdf9b3e8 FB |
1406 | #define ELF_ARCH EM_SH |
1407 | ||
d97ef72e RH |
1408 | static inline void init_thread(struct target_pt_regs *regs, |
1409 | struct image_info *infop) | |
fdf9b3e8 | 1410 | { |
d97ef72e RH |
1411 | /* Check other registers XXXXX */ |
1412 | regs->pc = infop->entry; | |
1413 | regs->regs[15] = infop->start_stack; | |
fdf9b3e8 FB |
1414 | } |
1415 | ||
7631c97e NF |
1416 | /* See linux kernel: arch/sh/include/asm/elf.h. */ |
1417 | #define ELF_NREG 23 | |
1418 | typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; | |
1419 | ||
1420 | /* See linux kernel: arch/sh/include/asm/ptrace.h. */ | |
1421 | enum { | |
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 | ||
d97ef72e | 1431 | static inline void elf_core_copy_regs(target_elf_gregset_t *regs, |
05390248 | 1432 | const CPUSH4State *env) |
7631c97e NF |
1433 | { |
1434 | int i; | |
1435 | ||
1436 | for (i = 0; i < 16; i++) { | |
72cd500b | 1437 | (*regs)[i] = tswapreg(env->gregs[i]); |
7631c97e NF |
1438 | } |
1439 | ||
86cd7b2d PB |
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); | |
7631c97e NF |
1446 | (*regs)[TARGET_REG_SYSCALL] = 0; /* FIXME */ |
1447 | } | |
1448 | ||
1449 | #define USE_ELF_CORE_DUMP | |
fdf9b3e8 FB |
1450 | #define ELF_EXEC_PAGESIZE 4096 |
1451 | ||
e42fd944 RH |
1452 | enum { |
1453 | SH_CPU_HAS_FPU = 0x0001, /* Hardware FPU support */ | |
1454 | SH_CPU_HAS_P2_FLUSH_BUG = 0x0002, /* Need to flush the cache in P2 area */ | |
1455 | SH_CPU_HAS_MMU_PAGE_ASSOC = 0x0004, /* SH3: TLB way selection bit support */ | |
1456 | SH_CPU_HAS_DSP = 0x0008, /* SH-DSP: DSP support */ | |
1457 | SH_CPU_HAS_PERF_COUNTER = 0x0010, /* Hardware performance counters */ | |
1458 | SH_CPU_HAS_PTEA = 0x0020, /* PTEA register */ | |
1459 | SH_CPU_HAS_LLSC = 0x0040, /* movli.l/movco.l */ | |
1460 | SH_CPU_HAS_L2_CACHE = 0x0080, /* Secondary cache / URAM */ | |
1461 | SH_CPU_HAS_OP32 = 0x0100, /* 32-bit instruction support */ | |
1462 | SH_CPU_HAS_PTEAEX = 0x0200, /* PTE ASID Extension support */ | |
1463 | }; | |
1464 | ||
1465 | #define ELF_HWCAP get_elf_hwcap() | |
1466 | ||
1467 | static 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 | ||
fdf9b3e8 FB |
1481 | #endif |
1482 | ||
48733d19 TS |
1483 | #ifdef TARGET_CRIS |
1484 | ||
1485 | #define ELF_START_MMAP 0x80000000 | |
1486 | ||
48733d19 | 1487 | #define ELF_CLASS ELFCLASS32 |
48733d19 TS |
1488 | #define ELF_ARCH EM_CRIS |
1489 | ||
d97ef72e RH |
1490 | static inline void init_thread(struct target_pt_regs *regs, |
1491 | struct image_info *infop) | |
48733d19 | 1492 | { |
d97ef72e | 1493 | regs->erp = infop->entry; |
48733d19 TS |
1494 | } |
1495 | ||
48733d19 TS |
1496 | #define ELF_EXEC_PAGESIZE 8192 |
1497 | ||
1498 | #endif | |
1499 | ||
e6e5906b PB |
1500 | #ifdef TARGET_M68K |
1501 | ||
1502 | #define ELF_START_MMAP 0x80000000 | |
1503 | ||
d97ef72e | 1504 | #define ELF_CLASS ELFCLASS32 |
d97ef72e | 1505 | #define ELF_ARCH EM_68K |
e6e5906b PB |
1506 | |
1507 | /* ??? Does this need to do anything? | |
d97ef72e | 1508 | #define ELF_PLAT_INIT(_r) */ |
e6e5906b | 1509 | |
d97ef72e RH |
1510 | static inline void init_thread(struct target_pt_regs *regs, |
1511 | struct image_info *infop) | |
e6e5906b PB |
1512 | { |
1513 | regs->usp = infop->start_stack; | |
1514 | regs->sr = 0; | |
1515 | regs->pc = infop->entry; | |
1516 | } | |
1517 | ||
7a93cc55 NF |
1518 | /* See linux kernel: arch/m68k/include/asm/elf.h. */ |
1519 | #define ELF_NREG 20 | |
1520 | typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; | |
1521 | ||
05390248 | 1522 | static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUM68KState *env) |
7a93cc55 | 1523 | { |
86cd7b2d PB |
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]); /* FIXME: orig_d0 */ | |
1541 | (*regs)[17] = tswapreg(env->sr); | |
1542 | (*regs)[18] = tswapreg(env->pc); | |
7a93cc55 NF |
1543 | (*regs)[19] = 0; /* FIXME: regs->format | regs->vector */ |
1544 | } | |
1545 | ||
1546 | #define USE_ELF_CORE_DUMP | |
d97ef72e | 1547 | #define ELF_EXEC_PAGESIZE 8192 |
e6e5906b PB |
1548 | |
1549 | #endif | |
1550 | ||
7a3148a9 JM |
1551 | #ifdef TARGET_ALPHA |
1552 | ||
1553 | #define ELF_START_MMAP (0x30000000000ULL) | |
1554 | ||
7a3148a9 | 1555 | #define ELF_CLASS ELFCLASS64 |
7a3148a9 JM |
1556 | #define ELF_ARCH EM_ALPHA |
1557 | ||
d97ef72e RH |
1558 | static inline void init_thread(struct target_pt_regs *regs, |
1559 | struct image_info *infop) | |
7a3148a9 JM |
1560 | { |
1561 | regs->pc = infop->entry; | |
1562 | regs->ps = 8; | |
1563 | regs->usp = infop->start_stack; | |
7a3148a9 JM |
1564 | } |
1565 | ||
7a3148a9 JM |
1566 | #define ELF_EXEC_PAGESIZE 8192 |
1567 | ||
1568 | #endif /* TARGET_ALPHA */ | |
1569 | ||
a4c075f1 UH |
1570 | #ifdef TARGET_S390X |
1571 | ||
1572 | #define ELF_START_MMAP (0x20000000000ULL) | |
1573 | ||
a4c075f1 UH |
1574 | #define ELF_CLASS ELFCLASS64 |
1575 | #define ELF_DATA ELFDATA2MSB | |
1576 | #define ELF_ARCH EM_S390 | |
1577 | ||
6d88baf1 DH |
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 | ||
1585 | static uint32_t get_elf_hwcap(void) | |
1586 | { | |
1587 | /* | |
1588 | * Let's assume we always have esan3 and zarch. | |
1589 | * 31-bit processes can use 64-bit registers (high gprs). | |
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); | |
da215c23 | 1602 | GET_FEATURE(S390_FEAT_VECTOR_ENH, HWCAP_S390_VXRS_EXT); |
6d88baf1 DH |
1603 | |
1604 | return hwcap; | |
1605 | } | |
1606 | ||
a4c075f1 UH |
1607 | static 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 | ||
4a1e8931 IL |
1614 | /* See linux kernel: arch/s390/include/uapi/asm/ptrace.h (s390_regs). */ |
1615 | #define ELF_NREG 27 | |
1616 | typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; | |
1617 | ||
1618 | enum { | |
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 | ||
1626 | static 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 | ||
a4c075f1 UH |
1647 | #endif /* TARGET_S390X */ |
1648 | ||
47ae93cd MC |
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 | ||
cb46938c KC |
1660 | #define ELF_HWCAP get_elf_hwcap() |
1661 | ||
1662 | static 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 | ||
e91a7227 | 1669 | return cpu->env.misa_ext & mask; |
cb46938c KC |
1670 | #undef MISA_BIT |
1671 | } | |
1672 | ||
47ae93cd MC |
1673 | static 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 /* TARGET_RISCV */ | |
1683 | ||
7c248bcd RH |
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 | ||
1693 | static 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; | |
60f1c801 RH |
1699 | regs->gr[24] = infop->argv; |
1700 | regs->gr[25] = infop->argc; | |
7c248bcd RH |
1701 | /* The top-of-stack contains a linkage buffer. */ |
1702 | regs->gr[30] = infop->start_stack + 64; | |
1703 | regs->gr[31] = infop->entry; | |
1704 | } | |
1705 | ||
eee816c0 RH |
1706 | #define LO_COMMPAGE 0 |
1707 | ||
1708 | static 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 | * On Linux, page zero is normally marked execute only + gateway. | |
1724 | * Normal read or write is supposed to fail (thus PROT_NONE above), | |
1725 | * but specific offsets have kernel code mapped to raise permissions | |
1726 | * and implement syscalls. Here, simply mark the page executable. | |
1727 | * Special case the entry points during translation (see do_page_zero). | |
1728 | */ | |
1729 | page_set_flags(LO_COMMPAGE, LO_COMMPAGE + TARGET_PAGE_SIZE, | |
1730 | PAGE_EXEC | PAGE_VALID); | |
1731 | return true; | |
1732 | } | |
1733 | ||
7c248bcd RH |
1734 | #endif /* TARGET_HPPA */ |
1735 | ||
ba7651fb MF |
1736 | #ifdef TARGET_XTENSA |
1737 | ||
1738 | #define ELF_START_MMAP 0x20000000 | |
1739 | ||
1740 | #define ELF_CLASS ELFCLASS32 | |
1741 | #define ELF_ARCH EM_XTENSA | |
1742 | ||
1743 | static 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 | /* See linux kernel: arch/xtensa/include/asm/elf.h. */ | |
1753 | #define ELF_NREG 128 | |
1754 | typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; | |
1755 | ||
1756 | enum { | |
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 | ||
1769 | static 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 /* TARGET_XTENSA */ | |
1793 | ||
d2a56bd2 TS |
1794 | #ifdef TARGET_HEXAGON |
1795 | ||
1796 | #define ELF_START_MMAP 0x20000000 | |
1797 | ||
1798 | #define ELF_CLASS ELFCLASS32 | |
1799 | #define ELF_ARCH EM_HEXAGON | |
1800 | ||
1801 | static 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 /* TARGET_HEXAGON */ | |
1809 | ||
fcdc0ab4 JY |
1810 | #ifndef ELF_BASE_PLATFORM |
1811 | #define ELF_BASE_PLATFORM (NULL) | |
1812 | #endif | |
1813 | ||
15338fd7 FB |
1814 | #ifndef ELF_PLATFORM |
1815 | #define ELF_PLATFORM (NULL) | |
1816 | #endif | |
1817 | ||
75be901c PC |
1818 | #ifndef ELF_MACHINE |
1819 | #define ELF_MACHINE ELF_ARCH | |
1820 | #endif | |
1821 | ||
d276a604 PC |
1822 | #ifndef elf_check_arch |
1823 | #define elf_check_arch(x) ((x) == ELF_ARCH) | |
1824 | #endif | |
1825 | ||
ace3d654 CMAB |
1826 | #ifndef elf_check_abi |
1827 | #define elf_check_abi(x) (1) | |
1828 | #endif | |
1829 | ||
15338fd7 FB |
1830 | #ifndef ELF_HWCAP |
1831 | #define ELF_HWCAP 0 | |
1832 | #endif | |
1833 | ||
7c4ee5bc RH |
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 | ||
992f48a0 | 1842 | #ifdef TARGET_ABI32 |
cb33da57 | 1843 | #undef ELF_CLASS |
992f48a0 | 1844 | #define ELF_CLASS ELFCLASS32 |
cb33da57 BS |
1845 | #undef bswaptls |
1846 | #define bswaptls(ptr) bswap32s(ptr) | |
1847 | #endif | |
1848 | ||
872f3d04 RH |
1849 | #ifndef EXSTACK_DEFAULT |
1850 | #define EXSTACK_DEFAULT false | |
1851 | #endif | |
1852 | ||
31e31b8a | 1853 | #include "elf.h" |
09bfb054 | 1854 | |
e8384b37 RH |
1855 | /* We must delay the following stanzas until after "elf.h". */ |
1856 | #if defined(TARGET_AARCH64) | |
1857 | ||
1858 | static 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 | /* We will extract GNU_PROPERTY_AARCH64_FEATURE_1_BTI later. */ | |
1869 | info->note_flags = *data; | |
1870 | } | |
1871 | return true; | |
1872 | } | |
1873 | #define ARCH_USE_GNU_PROPERTY 1 | |
1874 | ||
1875 | #else | |
1876 | ||
83f990eb RH |
1877 | static 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 | ||
e8384b37 RH |
1886 | #endif |
1887 | ||
09bfb054 FB |
1888 | struct exec |
1889 | { | |
d97ef72e RH |
1890 | unsigned int a_info; /* Use macros N_MAGIC, etc for access */ |
1891 | unsigned int a_text; /* length of text, in bytes */ | |
1892 | unsigned int a_data; /* length of data, in bytes */ | |
1893 | unsigned int a_bss; /* length of uninitialized data area, in bytes */ | |
1894 | unsigned int a_syms; /* length of symbol table data in file, in bytes */ | |
1895 | unsigned int a_entry; /* start address */ | |
1896 | unsigned int a_trsize; /* length of relocation info for text, in bytes */ | |
1897 | unsigned int a_drsize; /* length of relocation info for data, in bytes */ | |
09bfb054 FB |
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 | ||
31e31b8a | 1907 | /* Necessary parameters */ |
94894ff2 SB |
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) | |
79cb1f1d YK |
1912 | #define TARGET_ELF_PAGESTART(_v) ((_v) & \ |
1913 | ~(abi_ulong)(TARGET_ELF_EXEC_PAGESIZE-1)) | |
54936004 | 1914 | #define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1)) |
31e31b8a | 1915 | |
e0d1673d | 1916 | #define DLINFO_ITEMS 16 |
31e31b8a | 1917 | |
09bfb054 FB |
1918 | static inline void memcpy_fromfs(void * to, const void * from, unsigned long n) |
1919 | { | |
d97ef72e | 1920 | memcpy(to, from, n); |
09bfb054 | 1921 | } |
d691f669 | 1922 | |
31e31b8a | 1923 | #ifdef BSWAP_NEEDED |
92a31b1f | 1924 | static void bswap_ehdr(struct elfhdr *ehdr) |
31e31b8a | 1925 | { |
d97ef72e RH |
1926 | bswap16s(&ehdr->e_type); /* Object file type */ |
1927 | bswap16s(&ehdr->e_machine); /* Architecture */ | |
1928 | bswap32s(&ehdr->e_version); /* Object file version */ | |
1929 | bswaptls(&ehdr->e_entry); /* Entry point virtual address */ | |
1930 | bswaptls(&ehdr->e_phoff); /* Program header table file offset */ | |
1931 | bswaptls(&ehdr->e_shoff); /* Section header table file offset */ | |
1932 | bswap32s(&ehdr->e_flags); /* Processor-specific flags */ | |
1933 | bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */ | |
1934 | bswap16s(&ehdr->e_phentsize); /* Program header table entry size */ | |
1935 | bswap16s(&ehdr->e_phnum); /* Program header table entry count */ | |
1936 | bswap16s(&ehdr->e_shentsize); /* Section header table entry size */ | |
1937 | bswap16s(&ehdr->e_shnum); /* Section header table entry count */ | |
1938 | bswap16s(&ehdr->e_shstrndx); /* Section header string table index */ | |
31e31b8a FB |
1939 | } |
1940 | ||
991f8f0c | 1941 | static void bswap_phdr(struct elf_phdr *phdr, int phnum) |
31e31b8a | 1942 | { |
991f8f0c RH |
1943 | int i; |
1944 | for (i = 0; i < phnum; ++i, ++phdr) { | |
1945 | bswap32s(&phdr->p_type); /* Segment type */ | |
1946 | bswap32s(&phdr->p_flags); /* Segment flags */ | |
1947 | bswaptls(&phdr->p_offset); /* Segment file offset */ | |
1948 | bswaptls(&phdr->p_vaddr); /* Segment virtual address */ | |
1949 | bswaptls(&phdr->p_paddr); /* Segment physical address */ | |
1950 | bswaptls(&phdr->p_filesz); /* Segment size in file */ | |
1951 | bswaptls(&phdr->p_memsz); /* Segment size in memory */ | |
1952 | bswaptls(&phdr->p_align); /* Segment alignment */ | |
1953 | } | |
31e31b8a | 1954 | } |
689f936f | 1955 | |
991f8f0c | 1956 | static void bswap_shdr(struct elf_shdr *shdr, int shnum) |
689f936f | 1957 | { |
991f8f0c RH |
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 | } | |
689f936f FB |
1971 | } |
1972 | ||
7a3148a9 | 1973 | static void bswap_sym(struct elf_sym *sym) |
689f936f FB |
1974 | { |
1975 | bswap32s(&sym->st_name); | |
7a3148a9 JM |
1976 | bswaptls(&sym->st_value); |
1977 | bswaptls(&sym->st_size); | |
689f936f FB |
1978 | bswap16s(&sym->st_shndx); |
1979 | } | |
5dd0db52 SM |
1980 | |
1981 | #ifdef TARGET_MIPS | |
1982 | static 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 | |
991f8f0c RH |
1991 | #else |
1992 | static inline void bswap_ehdr(struct elfhdr *ehdr) { } | |
1993 | static inline void bswap_phdr(struct elf_phdr *phdr, int phnum) { } | |
1994 | static inline void bswap_shdr(struct elf_shdr *shdr, int shnum) { } | |
1995 | static inline void bswap_sym(struct elf_sym *sym) { } | |
5dd0db52 SM |
1996 | #ifdef TARGET_MIPS |
1997 | static inline void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags) { } | |
1998 | #endif | |
31e31b8a FB |
1999 | #endif |
2000 | ||
edf8e2af | 2001 | #ifdef USE_ELF_CORE_DUMP |
9349b4f9 | 2002 | static int elf_core_dump(int, const CPUArchState *); |
edf8e2af | 2003 | #endif /* USE_ELF_CORE_DUMP */ |
682674b8 | 2004 | static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias); |
edf8e2af | 2005 | |
9058abdd RH |
2006 | /* Verify the portions of EHDR within E_IDENT for the target. |
2007 | This can be performed before bswapping the entire header. */ | |
2008 | static 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 | /* Verify the portions of EHDR outside of E_IDENT for the target. | |
2020 | This has to wait until after bswapping the header. */ | |
2021 | static bool elf_check_ehdr(struct elfhdr *ehdr) | |
2022 | { | |
2023 | return (elf_check_arch(ehdr->e_machine) | |
ace3d654 | 2024 | && elf_check_abi(ehdr->e_flags) |
9058abdd RH |
2025 | && ehdr->e_ehsize == sizeof(struct elfhdr) |
2026 | && ehdr->e_phentsize == sizeof(struct elf_phdr) | |
9058abdd RH |
2027 | && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN)); |
2028 | } | |
2029 | ||
31e31b8a | 2030 | /* |
e5fe0c52 | 2031 | * 'copy_elf_strings()' copies argument/envelope strings from user |
31e31b8a FB |
2032 | * memory to free pages in kernel mem. These are in a format ready |
2033 | * to be put directly into the top of new user memory. | |
2034 | * | |
2035 | */ | |
59baae9a SB |
2036 | static abi_ulong copy_elf_strings(int argc, char **argv, char *scratch, |
2037 | abi_ulong p, abi_ulong stack_limit) | |
31e31b8a | 2038 | { |
59baae9a | 2039 | char *tmp; |
7c4ee5bc | 2040 | int len, i; |
59baae9a | 2041 | abi_ulong top = p; |
31e31b8a FB |
2042 | |
2043 | if (!p) { | |
d97ef72e | 2044 | return 0; /* bullet-proofing */ |
31e31b8a | 2045 | } |
59baae9a | 2046 | |
7c4ee5bc RH |
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; | |
59baae9a | 2057 | |
7c4ee5bc RH |
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 | } | |
d97ef72e | 2076 | } |
7c4ee5bc RH |
2077 | if (p != top) { |
2078 | memcpy_to_target(p, scratch + offset, top - p); | |
d97ef72e | 2079 | } |
7c4ee5bc RH |
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 | } | |
d97ef72e RH |
2107 | } |
2108 | } | |
7c4ee5bc RH |
2109 | if (p != top) { |
2110 | memcpy_to_target(top, scratch, p - top); | |
2111 | } | |
59baae9a SB |
2112 | } |
2113 | ||
31e31b8a FB |
2114 | return p; |
2115 | } | |
2116 | ||
59baae9a SB |
2117 | /* Older linux kernels provide up to MAX_ARG_PAGES (default: 32) of |
2118 | * argument/environment space. Newer kernels (>2.6.33) allow more, | |
2119 | * dependent on stack size, but guarantee at least 32 pages for | |
2120 | * backwards compatibility. | |
2121 | */ | |
2122 | #define STACK_LOWER_LIMIT (32 * TARGET_PAGE_SIZE) | |
2123 | ||
2124 | static abi_ulong setup_arg_pages(struct linux_binprm *bprm, | |
992f48a0 | 2125 | struct image_info *info) |
53a5960a | 2126 | { |
59baae9a | 2127 | abi_ulong size, error, guard; |
872f3d04 | 2128 | int prot; |
31e31b8a | 2129 | |
703e0e89 | 2130 | size = guest_stack_size; |
59baae9a SB |
2131 | if (size < STACK_LOWER_LIMIT) { |
2132 | size = STACK_LOWER_LIMIT; | |
60dcbcb5 | 2133 | } |
f4388205 HD |
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 | /* no guard page for hppa target where stack grows upwards. */ | |
2142 | guard = 0; | |
60dcbcb5 RH |
2143 | } |
2144 | ||
872f3d04 RH |
2145 | prot = PROT_READ | PROT_WRITE; |
2146 | if (info->exec_stack) { | |
2147 | prot |= PROT_EXEC; | |
2148 | } | |
2149 | error = target_mmap(0, size + guard, prot, | |
60dcbcb5 | 2150 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
09bfb054 | 2151 | if (error == -1) { |
60dcbcb5 | 2152 | perror("mmap stack"); |
09bfb054 FB |
2153 | exit(-1); |
2154 | } | |
31e31b8a | 2155 | |
60dcbcb5 | 2156 | /* We reserve one extra page at the top of the stack as guard. */ |
7c4ee5bc RH |
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 { | |
7c4ee5bc RH |
2162 | info->stack_limit = error + size; |
2163 | return error; | |
2164 | } | |
31e31b8a FB |
2165 | } |
2166 | ||
cf129f3a RH |
2167 | /* Map and zero the bss. We need to explicitly zero any fractional pages |
2168 | after the data section (i.e. bss). */ | |
2169 | static void zero_bss(abi_ulong elf_bss, abi_ulong last_bss, int prot) | |
31e31b8a | 2170 | { |
cf129f3a RH |
2171 | uintptr_t host_start, host_map_start, host_end; |
2172 | ||
2173 | last_bss = TARGET_PAGE_ALIGN(last_bss); | |
2174 | ||
2175 | /* ??? There is confusion between qemu_real_host_page_size and | |
2176 | qemu_host_page_size here and elsewhere in target_mmap, which | |
2177 | may lead to the end of the data section mapping from the file | |
2178 | not being mapped. At least there was an explicit test and | |
2179 | comment for that here, suggesting that "the file size must | |
2180 | be known". The comment probably pre-dates the introduction | |
2181 | of the fstat system call in target_mmap which does in fact | |
2182 | find out the size. What isn't clear is if the workaround | |
2183 | here is still actually needed. For now, continue with it, | |
2184 | but merge it with the "normal" mmap that would allocate the bss. */ | |
2185 | ||
3e8f1628 RH |
2186 | host_start = (uintptr_t) g2h_untagged(elf_bss); |
2187 | host_end = (uintptr_t) g2h_untagged(last_bss); | |
0c2d70c4 | 2188 | host_map_start = REAL_HOST_PAGE_ALIGN(host_start); |
cf129f3a RH |
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); | |
853d6f7a | 2196 | } |
f46e9a0b | 2197 | } |
853d6f7a | 2198 | |
f46e9a0b TM |
2199 | /* Ensure that the bss page(s) are valid */ |
2200 | if ((page_get_flags(last_bss-1) & prot) != prot) { | |
2201 | page_set_flags(elf_bss & TARGET_PAGE_MASK, last_bss, prot | PAGE_VALID); | |
cf129f3a | 2202 | } |
31e31b8a | 2203 | |
cf129f3a RH |
2204 | if (host_start < host_map_start) { |
2205 | memset((void *)host_start, 0, host_map_start - host_start); | |
2206 | } | |
2207 | } | |
53a5960a | 2208 | |
cf58affe CL |
2209 | #ifdef TARGET_ARM |
2210 | static int elf_is_fdpic(struct elfhdr *exec) | |
2211 | { | |
2212 | return exec->e_ident[EI_OSABI] == ELFOSABI_ARM_FDPIC; | |
2213 | } | |
2214 | #else | |
a99856cd CL |
2215 | /* Default implementation, always false. */ |
2216 | static int elf_is_fdpic(struct elfhdr *exec) | |
2217 | { | |
2218 | return 0; | |
2219 | } | |
cf58affe | 2220 | #endif |
a99856cd | 2221 | |
1af02e83 MF |
2222 | static 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 | /* elf32_fdpic_loadseg */ | |
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 | /* elf32_fdpic_loadmap */ | |
2237 | sp -= 4; | |
2238 | put_user_u16(0, sp+0); /* version */ | |
2239 | put_user_u16(info->nsegs, sp+2); /* nsegs */ | |
2240 | ||
2241 | info->personality = PER_LINUX_FDPIC; | |
2242 | info->loadmap_addr = sp; | |
2243 | ||
2244 | return sp; | |
2245 | } | |
1af02e83 | 2246 | |
992f48a0 | 2247 | static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc, |
8e62a717 RH |
2248 | struct elfhdr *exec, |
2249 | struct image_info *info, | |
2250 | struct image_info *interp_info) | |
31e31b8a | 2251 | { |
d97ef72e | 2252 | abi_ulong sp; |
7c4ee5bc | 2253 | abi_ulong u_argc, u_argv, u_envp, u_auxv; |
d97ef72e | 2254 | int size; |
14322bad LA |
2255 | int i; |
2256 | abi_ulong u_rand_bytes; | |
2257 | uint8_t k_rand_bytes[16]; | |
fcdc0ab4 JY |
2258 | abi_ulong u_platform, u_base_platform; |
2259 | const char *k_platform, *k_base_platform; | |
d97ef72e RH |
2260 | const int n = sizeof(elf_addr_t); |
2261 | ||
2262 | sp = p; | |
1af02e83 | 2263 | |
1af02e83 MF |
2264 | /* Needs to be before we load the env/argc/... */ |
2265 | if (elf_is_fdpic(exec)) { | |
2266 | /* Need 4 byte alignment for these structs */ | |
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); | |
3cb10cfa CL |
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; | |
1af02e83 MF |
2278 | } |
2279 | } | |
1af02e83 | 2280 | |
fcdc0ab4 JY |
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 | /* FIXME - check return value of memcpy_to_target() for failure */ | |
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 | ||
d97ef72e RH |
2297 | u_platform = 0; |
2298 | k_platform = ELF_PLATFORM; | |
2299 | if (k_platform) { | |
2300 | size_t len = strlen(k_platform) + 1; | |
7c4ee5bc RH |
2301 | if (STACK_GROWS_DOWN) { |
2302 | sp -= (len + n - 1) & ~(n - 1); | |
2303 | u_platform = sp; | |
2304 | /* FIXME - check return value of memcpy_to_target() for failure */ | |
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 | /* Provide 16 byte alignment for the PRNG, and basic alignment for | |
2314 | * the argv and envp pointers. | |
2315 | */ | |
2316 | if (STACK_GROWS_DOWN) { | |
2317 | sp = QEMU_ALIGN_DOWN(sp, 16); | |
2318 | } else { | |
2319 | sp = QEMU_ALIGN_UP(sp, 16); | |
d97ef72e | 2320 | } |
14322bad LA |
2321 | |
2322 | /* | |
c6a2377f | 2323 | * Generate 16 random bytes for userspace PRNG seeding. |
14322bad | 2324 | */ |
c6a2377f | 2325 | qemu_guest_getrandom_nofail(k_rand_bytes, sizeof(k_rand_bytes)); |
7c4ee5bc RH |
2326 | if (STACK_GROWS_DOWN) { |
2327 | sp -= 16; | |
2328 | u_rand_bytes = sp; | |
2329 | /* FIXME - check return value of memcpy_to_target() for failure */ | |
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 | } | |
14322bad | 2336 | |
d97ef72e | 2337 | size = (DLINFO_ITEMS + 1) * 2; |
fcdc0ab4 JY |
2338 | if (k_base_platform) |
2339 | size += 2; | |
d97ef72e RH |
2340 | if (k_platform) |
2341 | size += 2; | |
f5155289 | 2342 | #ifdef DLINFO_ARCH_ITEMS |
d97ef72e | 2343 | size += DLINFO_ARCH_ITEMS * 2; |
ad6919dc PM |
2344 | #endif |
2345 | #ifdef ELF_HWCAP2 | |
2346 | size += 2; | |
f5155289 | 2347 | #endif |
f516511e PM |
2348 | info->auxv_len = size * n; |
2349 | ||
d97ef72e | 2350 | size += envc + argc + 2; |
b9329d4b | 2351 | size += 1; /* argc itself */ |
d97ef72e | 2352 | size *= n; |
7c4ee5bc RH |
2353 | |
2354 | /* Allocate space and finalize stack alignment for entry now. */ | |
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; | |
60f1c801 RH |
2367 | info->argc = argc; |
2368 | info->envc = envc; | |
2369 | info->argv = u_argv; | |
2370 | info->envp = u_envp; | |
d97ef72e RH |
2371 | |
2372 | /* This is correct because Linux defines | |
2373 | * elf_addr_t as Elf32_Off / Elf64_Off | |
2374 | */ | |
2375 | #define NEW_AUX_ENT(id, val) do { \ | |
7c4ee5bc RH |
2376 | put_user_ual(id, u_auxv); u_auxv += n; \ |
2377 | put_user_ual(val, u_auxv); u_auxv += n; \ | |
d97ef72e RH |
2378 | } while(0) |
2379 | ||
82991bed PM |
2380 | #ifdef ARCH_DLINFO |
2381 | /* | |
2382 | * ARCH_DLINFO must come first so platform specific code can enforce | |
2383 | * special alignment requirements on the AUXV if necessary (eg. PPC). | |
2384 | */ | |
2385 | ARCH_DLINFO; | |
2386 | #endif | |
f516511e PM |
2387 | /* There must be exactly DLINFO_ITEMS entries here, or the assert |
2388 | * on info->auxv_len will trigger. | |
2389 | */ | |
8e62a717 | 2390 | NEW_AUX_ENT(AT_PHDR, (abi_ulong)(info->load_addr + exec->e_phoff)); |
d97ef72e RH |
2391 | NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr))); |
2392 | NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum)); | |
33143c44 LV |
2393 | if ((info->alignment & ~qemu_host_page_mask) != 0) { |
2394 | /* Target doesn't support host page size alignment */ | |
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 | } | |
8e62a717 | 2400 | NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_info ? interp_info->load_addr : 0)); |
d97ef72e | 2401 | NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0); |
8e62a717 | 2402 | NEW_AUX_ENT(AT_ENTRY, info->entry); |
d97ef72e RH |
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)); | |
14322bad | 2409 | NEW_AUX_ENT(AT_RANDOM, (abi_ulong) u_rand_bytes); |
444cd5c3 | 2410 | NEW_AUX_ENT(AT_SECURE, (abi_ulong) qemu_getauxval(AT_SECURE)); |
e0d1673d | 2411 | NEW_AUX_ENT(AT_EXECFN, info->file_string); |
14322bad | 2412 | |
ad6919dc PM |
2413 | #ifdef ELF_HWCAP2 |
2414 | NEW_AUX_ENT(AT_HWCAP2, (abi_ulong) ELF_HWCAP2); | |
2415 | #endif | |
2416 | ||
fcdc0ab4 JY |
2417 | if (u_base_platform) { |
2418 | NEW_AUX_ENT(AT_BASE_PLATFORM, u_base_platform); | |
2419 | } | |
7c4ee5bc | 2420 | if (u_platform) { |
d97ef72e | 2421 | NEW_AUX_ENT(AT_PLATFORM, u_platform); |
7c4ee5bc | 2422 | } |
7c4ee5bc | 2423 | NEW_AUX_ENT (AT_NULL, 0); |
f5155289 FB |
2424 | #undef NEW_AUX_ENT |
2425 | ||
f516511e PM |
2426 | /* Check that our initial calculation of the auxv length matches how much |
2427 | * we actually put into it. | |
2428 | */ | |
2429 | assert(info->auxv_len == u_auxv - info->saved_auxv); | |
7c4ee5bc RH |
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); | |
edf8e2af | 2448 | |
d97ef72e | 2449 | return sp; |
31e31b8a FB |
2450 | } |
2451 | ||
f5ef0e51 | 2452 | #if defined(HI_COMMPAGE) |
eee816c0 | 2453 | #define LO_COMMPAGE -1 |
f5ef0e51 RH |
2454 | #elif defined(LO_COMMPAGE) |
2455 | #define HI_COMMPAGE 0 | |
2456 | #else | |
66346faf | 2457 | #define HI_COMMPAGE 0 |
eee816c0 | 2458 | #define LO_COMMPAGE -1 |
d461b73e | 2459 | #ifndef INIT_GUEST_COMMPAGE |
ee947430 | 2460 | #define init_guest_commpage() true |
8756e136 | 2461 | #endif |
d461b73e | 2462 | #endif |
dce10401 | 2463 | |
ee947430 AB |
2464 | static 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 | } | |
dce10401 | 2471 | |
ee947430 AB |
2472 | static 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; | |
2a53535a | 2477 | |
ee947430 | 2478 | if (!QEMU_IS_ALIGNED(guest_base, align)) { |
5ca870b9 | 2479 | fprintf(stderr, "Requested guest base %p does not satisfy " |
ee947430 | 2480 | "host minimum alignment (0x%lx)\n", |
5ca870b9 | 2481 | (void *)guest_base, align); |
ee947430 AB |
2482 | exit(EXIT_FAILURE); |
2483 | } | |
2484 | ||
2485 | /* Sanity check the guest binary. */ | |
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); | |
2a53535a | 2492 | } |
ee947430 | 2493 | } else { |
a932eec4 | 2494 | #if HOST_LONG_BITS < TARGET_ABI_BITS |
ee947430 AB |
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); | |
2a53535a | 2500 | } |
a932eec4 | 2501 | #endif |
2a53535a | 2502 | } |
2a53535a | 2503 | |
ee947430 AB |
2504 | /* |
2505 | * Expand the allocation to the entire reserved_va. | |
2506 | * Exclude the mmap_min_addr hole. | |
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 | } | |
806d1021 | 2513 | |
ee947430 | 2514 | /* Reserve the address space for the binary, or reserved_va. */ |
3e8f1628 | 2515 | test = g2h_untagged(guest_loaddr); |
ee947430 AB |
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 | } | |
e7588237 AB |
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); | |
ee947430 AB |
2523 | } |
2524 | ||
ad592e37 AB |
2525 | /** |
2526 | * pgd_find_hole_fallback: potential mmap address | |
2527 | * @guest_size: size of available space | |
2528 | * @brk: location of break | |
2529 | * @align: memory alignment | |
2530 | * | |
2531 | * This is a fallback method for finding a hole in the host address | |
2532 | * space if we don't have the benefit of being able to access | |
2533 | * /proc/self/map. It can potentially take a very long time as we can | |
2534 | * only dumbly iterate up the host address space seeing if the | |
2535 | * allocation would work. | |
2536 | */ | |
5c3e87f3 AB |
2537 | static uintptr_t pgd_find_hole_fallback(uintptr_t guest_size, uintptr_t brk, |
2538 | long align, uintptr_t offset) | |
ad592e37 AB |
2539 | { |
2540 | uintptr_t base; | |
2541 | ||
2542 | /* Start (aligned) at the bottom and work our way up */ | |
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); | |
5c3e87f3 | 2548 | end = align_start + guest_size + offset; |
ad592e37 AB |
2549 | |
2550 | /* if brk is anywhere in the range give ourselves some room to grow. */ | |
2551 | if (align_start <= brk && brk < end) { | |
2552 | base = brk + (16 * MiB); | |
2553 | continue; | |
2554 | } else if (align_start + guest_size < align_start) { | |
2555 | /* we have run out of space */ | |
2556 | return -1; | |
2557 | } else { | |
2667e069 AB |
2558 | int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE | |
2559 | MAP_FIXED_NOREPLACE; | |
ad592e37 AB |
2560 | void * mmap_start = mmap((void *) align_start, guest_size, |
2561 | PROT_NONE, flags, -1, 0); | |
2562 | if (mmap_start != MAP_FAILED) { | |
7e588fbc | 2563 | munmap(mmap_start, guest_size); |
934eed51 | 2564 | if (mmap_start == (void *) align_start) { |
e7588237 AB |
2565 | qemu_log_mask(CPU_LOG_PAGE, |
2566 | "%s: base @ %p for %" PRIdPTR" bytes\n", | |
2567 | __func__, mmap_start + offset, guest_size); | |
2667e069 AB |
2568 | return (uintptr_t) mmap_start + offset; |
2569 | } | |
ad592e37 AB |
2570 | } |
2571 | base += qemu_host_page_size; | |
2572 | } | |
2573 | } | |
2574 | } | |
2575 | ||
ee947430 AB |
2576 | /* Return value for guest_base, or -1 if no hole found. */ |
2577 | static uintptr_t pgb_find_hole(uintptr_t guest_loaddr, uintptr_t guest_size, | |
5c3e87f3 | 2578 | long align, uintptr_t offset) |
ee947430 AB |
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(); | |
dce10401 | 2587 | |
ee947430 AB |
2588 | /* Read brk after we've read the maps, which will malloc. */ |
2589 | brk = (uintptr_t)sbrk(0); | |
2590 | ||
ad592e37 | 2591 | if (!maps) { |
190674f3 | 2592 | return pgd_find_hole_fallback(guest_size, brk, align, offset); |
ad592e37 AB |
2593 | } |
2594 | ||
ee947430 AB |
2595 | /* The first hole is before the first map entry. */ |
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; | |
5c3e87f3 | 2604 | align_start = ROUND_UP(this_start + offset, align); |
ee947430 AB |
2605 | |
2606 | /* Skip holes that are too small. */ | |
2607 | if (align_start >= this_end) { | |
2608 | continue; | |
2609 | } | |
2610 | hole_size = this_end - align_start; | |
2611 | if (hole_size < guest_size) { | |
2612 | continue; | |
aac362e4 LS |
2613 | } |
2614 | ||
ee947430 AB |
2615 | /* If this hole contains brk, give ourselves some room to grow. */ |
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 | } | |
806d1021 | 2627 | } |
806d1021 MI |
2628 | } |
2629 | ||
ee947430 AB |
2630 | /* Record the lowest successful match. */ |
2631 | if (ret < 0) { | |
190674f3 | 2632 | ret = align_start; |
dce10401 | 2633 | } |
ee947430 AB |
2634 | /* If this hole contains the identity map, select it. */ |
2635 | if (align_start <= guest_loaddr && | |
2636 | guest_loaddr + guest_size <= this_end) { | |
2637 | ret = 0; | |
b859040d | 2638 | } |
ee947430 AB |
2639 | /* If this hole ends above the identity map, stop looking. */ |
2640 | if (this_end >= guest_loaddr) { | |
2641 | break; | |
dce10401 MI |
2642 | } |
2643 | } | |
ee947430 | 2644 | free_self_maps(maps); |
dce10401 | 2645 | |
e7588237 AB |
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 | ||
ee947430 | 2652 | return ret; |
dce10401 MI |
2653 | } |
2654 | ||
ee947430 AB |
2655 | static void pgb_static(const char *image_name, abi_ulong orig_loaddr, |
2656 | abi_ulong orig_hiaddr, long align) | |
f3ed1f5d | 2657 | { |
ee947430 AB |
2658 | uintptr_t loaddr = orig_loaddr; |
2659 | uintptr_t hiaddr = orig_hiaddr; | |
5c3e87f3 | 2660 | uintptr_t offset = 0; |
ee947430 | 2661 | uintptr_t addr; |
f3ed1f5d | 2662 | |
ee947430 AB |
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 | } | |
f3ed1f5d | 2669 | |
ee947430 | 2670 | loaddr &= -align; |
66346faf | 2671 | if (HI_COMMPAGE) { |
ee947430 AB |
2672 | /* |
2673 | * Extend the allocation to include the commpage. | |
5c3e87f3 AB |
2674 | * For a 64-bit host, this is just 4GiB; for a 32-bit host we |
2675 | * need to ensure there is space bellow the guest_base so we | |
2676 | * can map the commpage in the place needed when the address | |
2677 | * arithmetic wraps around. | |
ee947430 AB |
2678 | */ |
2679 | if (sizeof(uintptr_t) == 8 || loaddr >= 0x80000000u) { | |
5c3e87f3 | 2680 | hiaddr = (uintptr_t) 4 << 30; |
f3ed1f5d | 2681 | } else { |
66346faf | 2682 | offset = -(HI_COMMPAGE & -align); |
f3ed1f5d | 2683 | } |
eee816c0 | 2684 | } else if (LO_COMMPAGE != -1) { |
f5ef0e51 | 2685 | loaddr = MIN(loaddr, LO_COMMPAGE & -align); |
ee947430 | 2686 | } |
dce10401 | 2687 | |
5c3e87f3 | 2688 | addr = pgb_find_hole(loaddr, hiaddr - loaddr, align, offset); |
ee947430 AB |
2689 | if (addr == -1) { |
2690 | /* | |
66346faf | 2691 | * If HI_COMMPAGE, there *might* be a non-consecutive allocation |
ee947430 AB |
2692 | * that can satisfy both. But as the normal arm32 link base address |
2693 | * is ~32k, and we extend down to include the commpage, making the | |
2694 | * overhead only ~96k, this is unlikely. | |
dce10401 | 2695 | */ |
ee947430 AB |
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; | |
e7588237 AB |
2703 | |
2704 | qemu_log_mask(CPU_LOG_PAGE, "%s: base @ %"PRIxPTR" for %" PRIuPTR" bytes\n", | |
2705 | __func__, addr, hiaddr - loaddr); | |
ee947430 | 2706 | } |
dce10401 | 2707 | |
ee947430 AB |
2708 | static void pgb_dynamic(const char *image_name, long align) |
2709 | { | |
2710 | /* | |
2711 | * The executable is dynamic and does not require a fixed address. | |
2712 | * All we need is a commpage that satisfies align. | |
2713 | * If we do not need a commpage, leave guest_base == 0. | |
2714 | */ | |
66346faf | 2715 | if (HI_COMMPAGE) { |
ee947430 AB |
2716 | uintptr_t addr, commpage; |
2717 | ||
2718 | /* 64-bit hosts should have used reserved_va. */ | |
2719 | assert(sizeof(uintptr_t) == 4); | |
2720 | ||
2721 | /* | |
2722 | * By putting the commpage at the first hole, that puts guest_base | |
2723 | * just above that, and maximises the positive guest addresses. | |
2724 | */ | |
66346faf | 2725 | commpage = HI_COMMPAGE & -align; |
5c3e87f3 | 2726 | addr = pgb_find_hole(commpage, -commpage, align, 0); |
ee947430 AB |
2727 | assert(addr != -1); |
2728 | guest_base = addr; | |
2729 | } | |
2730 | } | |
2731 | ||
2732 | static void pgb_reserved_va(const char *image_name, abi_ulong guest_loaddr, | |
2733 | abi_ulong guest_hiaddr, long align) | |
2734 | { | |
c1f6ad79 | 2735 | int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE; |
ee947430 AB |
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); | |
f3ed1f5d | 2743 | } |
f3ed1f5d | 2744 | |
ee947430 AB |
2745 | /* Widen the "image" to the entire reserved address space. */ |
2746 | pgb_static(image_name, 0, reserved_va, align); | |
2747 | ||
2667e069 | 2748 | /* osdep.h defines this as 0 if it's missing */ |
c1f6ad79 | 2749 | flags |= MAP_FIXED_NOREPLACE; |
c1f6ad79 | 2750 | |
ee947430 AB |
2751 | /* Reserve the memory on the host. */ |
2752 | assert(guest_base != 0); | |
3e8f1628 | 2753 | test = g2h_untagged(0); |
ee947430 | 2754 | addr = mmap(test, reserved_va, PROT_NONE, flags, -1, 0); |
fb730c86 | 2755 | if (addr == MAP_FAILED || addr != test) { |
ee947430 | 2756 | error_report("Unable to reserve 0x%lx bytes of virtual address " |
87966743 | 2757 | "space at %p (%s) for use as guest address space (check your " |
fb730c86 AB |
2758 | "virtual memory ulimit setting, min_mmap_addr or reserve less " |
2759 | "using -R option)", reserved_va, test, strerror(errno)); | |
ee947430 AB |
2760 | exit(EXIT_FAILURE); |
2761 | } | |
e7588237 AB |
2762 | |
2763 | qemu_log_mask(CPU_LOG_PAGE, "%s: base @ %p for %lu bytes\n", | |
2764 | __func__, addr, reserved_va); | |
f3ed1f5d PM |
2765 | } |
2766 | ||
ee947430 AB |
2767 | void probe_guest_base(const char *image_name, abi_ulong guest_loaddr, |
2768 | abi_ulong guest_hiaddr) | |
2769 | { | |
2770 | /* In order to use host shmat, we must be able to honor SHMLBA. */ | |
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 | /* Reserve and initialize the commpage. */ | |
2784 | if (!init_guest_commpage()) { | |
2785 | /* | |
2786 | * With have_guest_base, the user has selected the address and | |
2787 | * we are trying to work with that. Otherwise, we have selected | |
2788 | * free space and init_guest_commpage must succeeded. | |
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 | } | |
f3ed1f5d | 2798 | |
83f990eb RH |
2799 | enum { |
2800 | /* The string "GNU\0" as a magic number. */ | |
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 | * Process a single gnu_property entry. | |
2809 | * Return false for error. | |
2810 | */ | |
2811 | static 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 | /* Properties are supposed to be unique and sorted on pr_type. */ | |
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 | /* Process NT_GNU_PROPERTY_TYPE_0. */ | |
2859 | static 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 | /* Unless the arch requires properties, ignore them. */ | |
2875 | if (!ARCH_USE_GNU_PROPERTY) { | |
2876 | return true; | |
2877 | } | |
2878 | ||
2879 | /* If the properties are crazy large, that's too bad. */ | |
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 | * The contents of a valid PT_GNU_PROPERTY is a sequence | |
2902 | * of uint32_t -- swap them all now. | |
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 | * Note that nhdr is 3 words, and that the "name" described by namesz | |
2912 | * immediately follows nhdr and is thus at the 4th word. Further, all | |
2913 | * of the inputs to the kernel's round_up are multiples of 4. | |
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; /* end, exit ok */ | |
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 | ||
8e62a717 | 2943 | /* Load an ELF image into the address space. |
31e31b8a | 2944 | |
8e62a717 RH |
2945 | IMAGE_NAME is the filename of the image, to use in error messages. |
2946 | IMAGE_FD is the open file descriptor for the image. | |
2947 | ||
2948 | BPRM_BUF is a copy of the beginning of the file; this of course | |
2949 | contains the elf file header at offset 0. It is assumed that this | |
2950 | buffer is sufficiently aligned to present no problems to the host | |
2951 | in accessing data at aligned offsets within the buffer. | |
2952 | ||
2953 | On return: INFO values will be filled in, as necessary or available. */ | |
2954 | ||
2955 | static void load_elf_image(const char *image_name, int image_fd, | |
bf858897 | 2956 | struct image_info *info, char **pinterp_name, |
8e62a717 | 2957 | char bprm_buf[BPRM_BUF_SIZE]) |
31e31b8a | 2958 | { |
8e62a717 RH |
2959 | struct elfhdr *ehdr = (struct elfhdr *)bprm_buf; |
2960 | struct elf_phdr *phdr; | |
2961 | abi_ulong load_addr, load_bias, loaddr, hiaddr, error; | |
e8384b37 | 2962 | int i, retval, prot_exec; |
c7f17e7b | 2963 | Error *err = NULL; |
5fafdf24 | 2964 | |
8e62a717 | 2965 | /* First of all, some simple consistency checks */ |
8e62a717 | 2966 | if (!elf_check_ident(ehdr)) { |
c7f17e7b | 2967 | error_setg(&err, "Invalid ELF image for this architecture"); |
8e62a717 RH |
2968 | goto exit_errmsg; |
2969 | } | |
2970 | bswap_ehdr(ehdr); | |
2971 | if (!elf_check_ehdr(ehdr)) { | |
c7f17e7b | 2972 | error_setg(&err, "Invalid ELF image for this architecture"); |
8e62a717 | 2973 | goto exit_errmsg; |
d97ef72e | 2974 | } |
5fafdf24 | 2975 | |
8e62a717 RH |
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); | |
9955ffac | 2979 | } else { |
8e62a717 RH |
2980 | phdr = (struct elf_phdr *) alloca(i); |
2981 | retval = pread(image_fd, phdr, i, ehdr->e_phoff); | |
9955ffac | 2982 | if (retval != i) { |
8e62a717 | 2983 | goto exit_read; |
9955ffac | 2984 | } |
d97ef72e | 2985 | } |
8e62a717 | 2986 | bswap_phdr(phdr, ehdr->e_phnum); |
09bfb054 | 2987 | |
1af02e83 MF |
2988 | info->nsegs = 0; |
2989 | info->pt_dynamic_addr = 0; | |
1af02e83 | 2990 | |
98c1076c AB |
2991 | mmap_lock(); |
2992 | ||
8a1a5274 RH |
2993 | /* |
2994 | * Find the maximum size of the image and allocate an appropriate | |
2995 | * amount of memory to handle that. Locate the interpreter, if any. | |
2996 | */ | |
682674b8 | 2997 | loaddr = -1, hiaddr = 0; |
33143c44 | 2998 | info->alignment = 0; |
872f3d04 | 2999 | info->exec_stack = EXSTACK_DEFAULT; |
8e62a717 | 3000 | for (i = 0; i < ehdr->e_phnum; ++i) { |
4d9d535a RH |
3001 | struct elf_phdr *eppnt = phdr + i; |
3002 | if (eppnt->p_type == PT_LOAD) { | |
3003 | abi_ulong a = eppnt->p_vaddr - eppnt->p_offset; | |
682674b8 RH |
3004 | if (a < loaddr) { |
3005 | loaddr = a; | |
3006 | } | |
4d9d535a | 3007 | a = eppnt->p_vaddr + eppnt->p_memsz; |
682674b8 RH |
3008 | if (a > hiaddr) { |
3009 | hiaddr = a; | |
3010 | } | |
1af02e83 | 3011 | ++info->nsegs; |
4d9d535a | 3012 | info->alignment |= eppnt->p_align; |
8a1a5274 RH |
3013 | } else if (eppnt->p_type == PT_INTERP && pinterp_name) { |
3014 | g_autofree char *interp_name = NULL; | |
3015 | ||
3016 | if (*pinterp_name) { | |
c7f17e7b | 3017 | error_setg(&err, "Multiple PT_INTERP entries"); |
8a1a5274 RH |
3018 | goto exit_errmsg; |
3019 | } | |
c7f17e7b | 3020 | |
8a1a5274 | 3021 | interp_name = g_malloc(eppnt->p_filesz); |
8a1a5274 RH |
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) { | |
c7f17e7b | 3030 | goto exit_read; |
8a1a5274 RH |
3031 | } |
3032 | } | |
3033 | if (interp_name[eppnt->p_filesz - 1] != 0) { | |
c7f17e7b | 3034 | error_setg(&err, "Invalid PT_INTERP entry"); |
8a1a5274 RH |
3035 | goto exit_errmsg; |
3036 | } | |
3037 | *pinterp_name = g_steal_pointer(&interp_name); | |
83f990eb RH |
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 | } | |
872f3d04 RH |
3042 | } else if (eppnt->p_type == PT_GNU_STACK) { |
3043 | info->exec_stack = eppnt->p_flags & PF_X; | |
682674b8 RH |
3044 | } |
3045 | } | |
3046 | ||
6fd59449 RH |
3047 | if (pinterp_name != NULL) { |
3048 | /* | |
3049 | * This is the main executable. | |
3050 | * | |
3051 | * Reserve extra space for brk. | |
3052 | * We hold on to this space while placing the interpreter | |
3053 | * and the stack, lest they be placed immediately after | |
3054 | * the data segment and block allocation from the brk. | |
3055 | * | |
11d36727 AB |
3056 | * 16MB is chosen as "large enough" without being so large as |
3057 | * to allow the result to not fit with a 32-bit guest on a | |
3058 | * 32-bit host. However some 64 bit guests (e.g. s390x) | |
3059 | * attempt to place their heap further ahead and currently | |
3060 | * nothing stops them smashing into QEMUs address space. | |
6fd59449 | 3061 | */ |
11d36727 AB |
3062 | #if TARGET_LONG_BITS == 64 |
3063 | info->reserve_brk = 32 * MiB; | |
3064 | #else | |
6fd59449 | 3065 | info->reserve_brk = 16 * MiB; |
11d36727 | 3066 | #endif |
6fd59449 RH |
3067 | hiaddr += info->reserve_brk; |
3068 | ||
3069 | if (ehdr->e_type == ET_EXEC) { | |
3070 | /* | |
3071 | * Make sure that the low address does not conflict with | |
3072 | * MMAP_MIN_ADDR or the QEMU application itself. | |
3073 | */ | |
3074 | probe_guest_base(image_name, loaddr, hiaddr); | |
ee947430 AB |
3075 | } else { |
3076 | /* | |
3077 | * The binary is dynamic, but we still need to | |
3078 | * select guest_base. In this case we pass a size. | |
3079 | */ | |
3080 | probe_guest_base(image_name, 0, hiaddr - loaddr); | |
d97ef72e | 3081 | } |
6fd59449 RH |
3082 | } |
3083 | ||
3084 | /* | |
3085 | * Reserve address space for all of this. | |
3086 | * | |
3087 | * In the case of ET_EXEC, we supply MAP_FIXED so that we get | |
3088 | * exactly the address range that is required. | |
3089 | * | |
3090 | * Otherwise this is ET_DYN, and we are searching for a location | |
3091 | * that can hold the memory space required. If the image is | |
3092 | * pre-linked, LOADDR will be non-zero, and the kernel should | |
3093 | * honor that address if it happens to be free. | |
3094 | * | |
3095 | * In both cases, we will overwrite pages in this range with mappings | |
3096 | * from the executable. | |
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) { | |
c7f17e7b | 3103 | goto exit_mmap; |
d97ef72e | 3104 | } |
682674b8 | 3105 | load_bias = load_addr - loaddr; |
d97ef72e | 3106 | |
a99856cd | 3107 | if (elf_is_fdpic(ehdr)) { |
1af02e83 | 3108 | struct elf32_fdpic_loadseg *loadsegs = info->loadsegs = |
7267c094 | 3109 | g_malloc(sizeof(*loadsegs) * info->nsegs); |
1af02e83 MF |
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 | } | |
1af02e83 | 3125 | |
8e62a717 | 3126 | info->load_bias = load_bias; |
dc12567a JK |
3127 | info->code_offset = load_bias; |
3128 | info->data_offset = load_bias; | |
8e62a717 RH |
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; | |
d8fd2954 | 3136 | info->elf_flags = ehdr->e_flags; |
8e62a717 | 3137 | |
e8384b37 RH |
3138 | prot_exec = PROT_EXEC; |
3139 | #ifdef TARGET_AARCH64 | |
3140 | /* | |
3141 | * If the BTI feature is present, this indicates that the executable | |
3142 | * pages of the startup binary should be mapped with PROT_BTI, so that | |
3143 | * branch targets are enforced. | |
3144 | * | |
3145 | * The startup binary is either the interpreter or the static executable. | |
3146 | * The interpreter is responsible for all pages of a dynamic executable. | |
3147 | * | |
3148 | * Elf notes are backward compatible to older cpus. | |
3149 | * Do not enable BTI unless it is supported. | |
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 | ||
8e62a717 RH |
3158 | for (i = 0; i < ehdr->e_phnum; i++) { |
3159 | struct elf_phdr *eppnt = phdr + i; | |
d97ef72e | 3160 | if (eppnt->p_type == PT_LOAD) { |
94894ff2 | 3161 | abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em, vaddr_len; |
d97ef72e | 3162 | int elf_prot = 0; |
d97ef72e | 3163 | |
e5eaf570 RH |
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) { | |
e8384b37 | 3171 | elf_prot |= prot_exec; |
e5eaf570 | 3172 | } |
d97ef72e | 3173 | |
682674b8 RH |
3174 | vaddr = load_bias + eppnt->p_vaddr; |
3175 | vaddr_po = TARGET_ELF_PAGEOFFSET(vaddr); | |
3176 | vaddr_ps = TARGET_ELF_PAGESTART(vaddr); | |
22d113b5 GM |
3177 | |
3178 | vaddr_ef = vaddr + eppnt->p_filesz; | |
3179 | vaddr_em = vaddr + eppnt->p_memsz; | |
682674b8 | 3180 | |
d87146bc | 3181 | /* |
22d113b5 GM |
3182 | * Some segments may be completely empty, with a non-zero p_memsz |
3183 | * but no backing file segment. | |
d87146bc GM |
3184 | */ |
3185 | if (eppnt->p_filesz != 0) { | |
22d113b5 | 3186 | vaddr_len = TARGET_ELF_PAGELENGTH(eppnt->p_filesz + vaddr_po); |
d87146bc GM |
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) { | |
c7f17e7b | 3192 | goto exit_mmap; |
d87146bc | 3193 | } |
09bfb054 | 3194 | |
22d113b5 GM |
3195 | /* |
3196 | * If the load segment requests extra zeros (e.g. bss), map it. | |
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); | |
31e31b8a | 3206 | |
22d113b5 GM |
3207 | if (error == -1) { |
3208 | goto exit_mmap; | |
3209 | } | |
cf129f3a | 3210 | } |
8e62a717 RH |
3211 | |
3212 | /* Find the full program boundaries. */ | |
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 | } | |
8a045188 TB |
3228 | } |
3229 | if (vaddr_em > info->brk) { | |
3230 | info->brk = vaddr_em; | |
8e62a717 | 3231 | } |
5dd0db52 SM |
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)) { | |
c7f17e7b | 3236 | error_setg(&err, "Invalid PT_MIPS_ABIFLAGS entry"); |
5dd0db52 SM |
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)) { | |
c7f17e7b | 3246 | goto exit_read; |
5dd0db52 SM |
3247 | } |
3248 | } | |
3249 | bswap_mips_abiflags(&abiflags); | |
c94cb6c9 | 3250 | info->fp_abi = abiflags.fp_abi; |
5dd0db52 | 3251 | #endif |
d97ef72e | 3252 | } |
682674b8 | 3253 | } |
5fafdf24 | 3254 | |
8e62a717 RH |
3255 | if (info->end_data == 0) { |
3256 | info->start_data = info->end_code; | |
3257 | info->end_data = info->end_code; | |
8e62a717 RH |
3258 | } |
3259 | ||
682674b8 | 3260 | if (qemu_log_enabled()) { |
8e62a717 | 3261 | load_symbols(ehdr, image_fd, load_bias); |
682674b8 | 3262 | } |
31e31b8a | 3263 | |
98c1076c AB |
3264 | mmap_unlock(); |
3265 | ||
8e62a717 RH |
3266 | close(image_fd); |
3267 | return; | |
3268 | ||
3269 | exit_read: | |
3270 | if (retval >= 0) { | |
c7f17e7b RH |
3271 | error_setg(&err, "Incomplete read of file header"); |
3272 | } else { | |
3273 | error_setg_errno(&err, errno, "Error reading file header"); | |
8e62a717 | 3274 | } |
c7f17e7b RH |
3275 | goto exit_errmsg; |
3276 | exit_mmap: | |
3277 | error_setg_errno(&err, errno, "Error mapping file"); | |
3278 | goto exit_errmsg; | |
8e62a717 | 3279 | exit_errmsg: |
c7f17e7b | 3280 | error_reportf_err(err, "%s: ", image_name); |
8e62a717 RH |
3281 | exit(-1); |
3282 | } | |
3283 | ||
3284 | static void load_elf_interp(const char *filename, struct image_info *info, | |
3285 | char bprm_buf[BPRM_BUF_SIZE]) | |
3286 | { | |
3287 | int fd, retval; | |
808f6563 | 3288 | Error *err = NULL; |
8e62a717 RH |
3289 | |
3290 | fd = open(path(filename), O_RDONLY); | |
3291 | if (fd < 0) { | |
808f6563 RH |
3292 | error_setg_file_open(&err, errno, filename); |
3293 | error_report_err(err); | |
3294 | exit(-1); | |
8e62a717 | 3295 | } |
31e31b8a | 3296 | |
8e62a717 RH |
3297 | retval = read(fd, bprm_buf, BPRM_BUF_SIZE); |
3298 | if (retval < 0) { | |
808f6563 RH |
3299 | error_setg_errno(&err, errno, "Error reading file header"); |
3300 | error_reportf_err(err, "%s: ", filename); | |
3301 | exit(-1); | |
8e62a717 | 3302 | } |
808f6563 | 3303 | |
8e62a717 RH |
3304 | if (retval < BPRM_BUF_SIZE) { |
3305 | memset(bprm_buf + retval, 0, BPRM_BUF_SIZE - retval); | |
3306 | } | |
3307 | ||
bf858897 | 3308 | load_elf_image(filename, fd, info, NULL, bprm_buf); |
31e31b8a FB |
3309 | } |
3310 | ||
49918a75 PB |
3311 | static int symfind(const void *s0, const void *s1) |
3312 | { | |
c7c530cd | 3313 | target_ulong addr = *(target_ulong *)s0; |
49918a75 PB |
3314 | struct elf_sym *sym = (struct elf_sym *)s1; |
3315 | int result = 0; | |
c7c530cd | 3316 | if (addr < sym->st_value) { |
49918a75 | 3317 | result = -1; |
c7c530cd | 3318 | } else if (addr >= sym->st_value + sym->st_size) { |
49918a75 PB |
3319 | result = 1; |
3320 | } | |
3321 | return result; | |
3322 | } | |
3323 | ||
3324 | static 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 | // binary search | |
49918a75 PB |
3333 | struct elf_sym *sym; |
3334 | ||
c7c530cd | 3335 | sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), symfind); |
7cba04f6 | 3336 | if (sym != NULL) { |
49918a75 PB |
3337 | return s->disas_strtab + sym->st_name; |
3338 | } | |
3339 | ||
3340 | return ""; | |
3341 | } | |
3342 | ||
3343 | /* FIXME: This should use elf_ops.h */ | |
3344 | static 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 | ||
689f936f | 3353 | /* Best attempt to load symbols from this ELF object. */ |
682674b8 | 3354 | static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias) |
689f936f | 3355 | { |
682674b8 | 3356 | int i, shnum, nsyms, sym_idx = 0, str_idx = 0; |
1e06262d | 3357 | uint64_t segsz; |
682674b8 | 3358 | struct elf_shdr *shdr; |
b9475279 CV |
3359 | char *strings = NULL; |
3360 | struct syminfo *s = NULL; | |
3361 | struct elf_sym *new_syms, *syms = NULL; | |
689f936f | 3362 | |
682674b8 RH |
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; | |
49918a75 PB |
3375 | goto found; |
3376 | } | |
689f936f | 3377 | } |
682674b8 RH |
3378 | |
3379 | /* There will be no symbol table if the file was stripped. */ | |
3380 | return; | |
689f936f FB |
3381 | |
3382 | found: | |
682674b8 | 3383 | /* Now know where the strtab and symtab are. Snarf them. */ |
0ef9ea29 | 3384 | s = g_try_new(struct syminfo, 1); |
682674b8 | 3385 | if (!s) { |
b9475279 | 3386 | goto give_up; |
682674b8 | 3387 | } |
5fafdf24 | 3388 | |
1e06262d PM |
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) { | |
b9475279 | 3393 | goto give_up; |
682674b8 | 3394 | } |
49918a75 | 3395 | |
1e06262d PM |
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) { | |
b9475279 | 3399 | goto give_up; |
682674b8 | 3400 | } |
31e31b8a | 3401 | |
1e06262d PM |
3402 | if (segsz / sizeof(struct elf_sym) > INT_MAX) { |
3403 | /* Implausibly large symbol table: give up rather than ploughing | |
3404 | * on with the number of symbols calculation overflowing | |
3405 | */ | |
3406 | goto give_up; | |
3407 | } | |
3408 | nsyms = segsz / sizeof(struct elf_sym); | |
682674b8 | 3409 | for (i = 0; i < nsyms; ) { |
49918a75 | 3410 | bswap_sym(syms + i); |
682674b8 RH |
3411 | /* Throw away entries which we do not need. */ |
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) { | |
49918a75 PB |
3416 | syms[i] = syms[nsyms]; |
3417 | } | |
682674b8 | 3418 | } else { |
49918a75 | 3419 | #if defined(TARGET_ARM) || defined (TARGET_MIPS) |
682674b8 RH |
3420 | /* The bottom address bit marks a Thumb or MIPS16 symbol. */ |
3421 | syms[i].st_value &= ~(target_ulong)1; | |
0774bed1 | 3422 | #endif |
682674b8 RH |
3423 | syms[i].st_value += load_bias; |
3424 | i++; | |
3425 | } | |
0774bed1 | 3426 | } |
49918a75 | 3427 | |
b9475279 CV |
3428 | /* No "useful" symbol. */ |
3429 | if (nsyms == 0) { | |
3430 | goto give_up; | |
3431 | } | |
3432 | ||
5d5c9930 RH |
3433 | /* Attempt to free the storage associated with the local symbols |
3434 | that we threw away. Whether or not this has any effect on the | |
3435 | memory allocation depends on the malloc implementation and how | |
3436 | many symbols we managed to discard. */ | |
0ef9ea29 | 3437 | new_syms = g_try_renew(struct elf_sym, syms, nsyms); |
8d79de6e | 3438 | if (new_syms == NULL) { |
b9475279 | 3439 | goto give_up; |
5d5c9930 | 3440 | } |
8d79de6e | 3441 | syms = new_syms; |
5d5c9930 | 3442 | |
49918a75 | 3443 | qsort(syms, nsyms, sizeof(*syms), symcmp); |
689f936f | 3444 | |
49918a75 PB |
3445 | s->disas_num_syms = nsyms; |
3446 | #if ELF_CLASS == ELFCLASS32 | |
3447 | s->disas_symtab.elf32 = syms; | |
49918a75 PB |
3448 | #else |
3449 | s->disas_symtab.elf64 = syms; | |
49918a75 | 3450 | #endif |
682674b8 | 3451 | s->lookup_symbol = lookup_symbolxx; |
e80cfcfc FB |
3452 | s->next = syminfos; |
3453 | syminfos = s; | |
b9475279 CV |
3454 | |
3455 | return; | |
3456 | ||
3457 | give_up: | |
0ef9ea29 PM |
3458 | g_free(s); |
3459 | g_free(strings); | |
3460 | g_free(syms); | |
689f936f | 3461 | } |
31e31b8a | 3462 | |
768fe76e YS |
3463 | uint32_t get_elf_eflags(int fd) |
3464 | { | |
3465 | struct elfhdr ehdr; | |
3466 | off_t offset; | |
3467 | int ret; | |
3468 | ||
3469 | /* Read ELF header */ | |
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 | /* Check ELF signature */ | |
3484 | if (!elf_check_ident(&ehdr)) { | |
3485 | return 0; | |
3486 | } | |
3487 | ||
3488 | /* check header */ | |
3489 | bswap_ehdr(&ehdr); | |
3490 | if (!elf_check_ehdr(&ehdr)) { | |
3491 | return 0; | |
3492 | } | |
3493 | ||
3494 | /* return architecture id */ | |
3495 | return ehdr.e_flags; | |
3496 | } | |
3497 | ||
f0116c54 | 3498 | int load_elf_binary(struct linux_binprm *bprm, struct image_info *info) |
31e31b8a | 3499 | { |
8e62a717 | 3500 | struct image_info interp_info; |
31e31b8a | 3501 | struct elfhdr elf_ex; |
8e62a717 | 3502 | char *elf_interpreter = NULL; |
59baae9a | 3503 | char *scratch; |
31e31b8a | 3504 | |
abcac736 DS |
3505 | memset(&interp_info, 0, sizeof(interp_info)); |
3506 | #ifdef TARGET_MIPS | |
3507 | interp_info.fp_abi = MIPS_ABI_FP_UNKNOWN; | |
3508 | #endif | |
3509 | ||
bf858897 | 3510 | info->start_mmap = (abi_ulong)ELF_START_MMAP; |
bf858897 RH |
3511 | |
3512 | load_elf_image(bprm->filename, bprm->fd, info, | |
3513 | &elf_interpreter, bprm->buf); | |
31e31b8a | 3514 | |
bf858897 RH |
3515 | /* ??? We need a copy of the elf header for passing to create_elf_tables. |
3516 | If we do nothing, we'll have overwritten this when we re-use bprm->buf | |
3517 | when we load the interpreter. */ | |
3518 | elf_ex = *(struct elfhdr *)bprm->buf; | |
31e31b8a | 3519 | |
59baae9a SB |
3520 | /* Do this so that we can load the interpreter, if need be. We will |
3521 | change some of these later */ | |
3522 | bprm->p = setup_arg_pages(bprm, info); | |
3523 | ||
3524 | scratch = g_new0(char, TARGET_PAGE_SIZE); | |
7c4ee5bc RH |
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 | ||
59baae9a SB |
3547 | g_free(scratch); |
3548 | ||
e5fe0c52 | 3549 | if (!bprm->p) { |
bf858897 RH |
3550 | fprintf(stderr, "%s: %s\n", bprm->filename, strerror(E2BIG)); |
3551 | exit(-1); | |
379f6698 | 3552 | } |
379f6698 | 3553 | |
8e62a717 RH |
3554 | if (elf_interpreter) { |
3555 | load_elf_interp(elf_interpreter, &interp_info, bprm->buf); | |
31e31b8a | 3556 | |
8e62a717 RH |
3557 | /* If the program interpreter is one of these two, then assume |
3558 | an iBCS2 image. Otherwise assume a native linux image. */ | |
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; | |
31e31b8a | 3563 | |
8e62a717 RH |
3564 | /* Why this, you ask??? Well SVr4 maps page 0 as read-only, |
3565 | and some applications "depend" upon this behavior. Since | |
3566 | we do not have the power to recompile these, we emulate | |
3567 | the SVr4 behavior. Sigh. */ | |
3568 | target_mmap(0, qemu_host_page_size, PROT_READ | PROT_EXEC, | |
68754b44 | 3569 | MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
8e62a717 | 3570 | } |
c94cb6c9 SM |
3571 | #ifdef TARGET_MIPS |
3572 | info->interp_fp_abi = interp_info.fp_abi; | |
3573 | #endif | |
31e31b8a FB |
3574 | } |
3575 | ||
db2af69d RH |
3576 | /* |
3577 | * TODO: load a vdso, which would also contain the signal trampolines. | |
3578 | * Otherwise, allocate a private page to hold them. | |
3579 | */ | |
3580 | if (TARGET_ARCH_HAS_SIGTRAMP_PAGE) { | |
802ae45e LV |
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 | ||
db2af69d RH |
3588 | setup_sigtramp(tramp_page); |
3589 | target_mprotect(tramp_page, TARGET_PAGE_SIZE, PROT_READ | PROT_EXEC); | |
3590 | } | |
3591 | ||
8e62a717 RH |
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 | /* If we have an interpreter, set that as the program's entry point. | |
8e78064e | 3597 | Copy the load_bias as well, to help PPC64 interpret the entry |
8e62a717 RH |
3598 | point as a function descriptor. Do this after creating elf tables |
3599 | so that we copy the original program entry point into the AUXV. */ | |
3600 | if (elf_interpreter) { | |
8e78064e | 3601 | info->load_bias = interp_info.load_bias; |
8e62a717 | 3602 | info->entry = interp_info.entry; |
2b323087 | 3603 | g_free(elf_interpreter); |
8e62a717 | 3604 | } |
31e31b8a | 3605 | |
edf8e2af MW |
3606 | #ifdef USE_ELF_CORE_DUMP |
3607 | bprm->core_dump = &elf_core_dump; | |
3608 | #endif | |
3609 | ||
6fd59449 RH |
3610 | /* |
3611 | * If we reserved extra space for brk, release it now. | |
3612 | * The implementation of do_brk in syscalls.c expects to be able | |
3613 | * to mmap pages in this space. | |
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 | ||
31e31b8a FB |
3621 | return 0; |
3622 | } | |
3623 | ||
edf8e2af | 3624 | #ifdef USE_ELF_CORE_DUMP |
edf8e2af MW |
3625 | /* |
3626 | * Definitions to generate Intel SVR4-like core files. | |
a2547a13 | 3627 | * These mostly have the same names as the SVR4 types with "target_elf_" |
edf8e2af MW |
3628 | * tacked on the front to prevent clashes with linux definitions, |
3629 | * and the typedef forms have been avoided. This is mostly like | |
3630 | * the SVR4 structure, but more Linuxy, with things that Linux does | |
3631 | * not support and which gdb doesn't really use excluded. | |
3632 | * | |
3633 | * Fields we don't dump (their contents is zero) in linux-user qemu | |
3634 | * are marked with XXX. | |
3635 | * | |
3636 | * Core dump code is copied from linux kernel (fs/binfmt_elf.c). | |
3637 | * | |
3638 | * Porting ELF coredump for target is (quite) simple process. First you | |
dd0a3651 | 3639 | * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for |
edf8e2af MW |
3640 | * the target resides): |
3641 | * | |
3642 | * #define USE_ELF_CORE_DUMP | |
3643 | * | |
3644 | * Next you define type of register set used for dumping. ELF specification | |
3645 | * says that it needs to be array of elf_greg_t that has size of ELF_NREG. | |
3646 | * | |
c227f099 | 3647 | * typedef <target_regtype> target_elf_greg_t; |
edf8e2af | 3648 | * #define ELF_NREG <number of registers> |
c227f099 | 3649 | * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG]; |
edf8e2af | 3650 | * |
edf8e2af MW |
3651 | * Last step is to implement target specific function that copies registers |
3652 | * from given cpu into just specified register set. Prototype is: | |
3653 | * | |
c227f099 | 3654 | * static void elf_core_copy_regs(taret_elf_gregset_t *regs, |
9349b4f9 | 3655 | * const CPUArchState *env); |
edf8e2af MW |
3656 | * |
3657 | * Parameters: | |
3658 | * regs - copy register values into here (allocated and zeroed by caller) | |
3659 | * env - copy registers from here | |
3660 | * | |
3661 | * Example for ARM target is provided in this file. | |
3662 | */ | |
3663 | ||
3664 | /* An ELF note in memory */ | |
3665 | struct memelfnote { | |
3666 | const char *name; | |
3667 | size_t namesz; | |
3668 | size_t namesz_rounded; | |
3669 | int type; | |
3670 | size_t datasz; | |
80f5ce75 | 3671 | size_t datasz_rounded; |
edf8e2af MW |
3672 | void *data; |
3673 | size_t notesz; | |
3674 | }; | |
3675 | ||
a2547a13 | 3676 | struct target_elf_siginfo { |
f8fd4fc4 PB |
3677 | abi_int si_signo; /* signal number */ |
3678 | abi_int si_code; /* extra code */ | |
3679 | abi_int si_errno; /* errno */ | |
edf8e2af MW |
3680 | }; |
3681 | ||
a2547a13 LD |
3682 | struct target_elf_prstatus { |
3683 | struct target_elf_siginfo pr_info; /* Info associated with signal */ | |
1ddd592f | 3684 | abi_short pr_cursig; /* Current signal */ |
ca98ac83 PB |
3685 | abi_ulong pr_sigpend; /* XXX */ |
3686 | abi_ulong pr_sighold; /* XXX */ | |
c227f099 AL |
3687 | target_pid_t pr_pid; |
3688 | target_pid_t pr_ppid; | |
3689 | target_pid_t pr_pgrp; | |
3690 | target_pid_t pr_sid; | |
edf8e2af MW |
3691 | struct target_timeval pr_utime; /* XXX User time */ |
3692 | struct target_timeval pr_stime; /* XXX System time */ | |
3693 | struct target_timeval pr_cutime; /* XXX Cumulative user time */ | |
3694 | struct target_timeval pr_cstime; /* XXX Cumulative system time */ | |
c227f099 | 3695 | target_elf_gregset_t pr_reg; /* GP registers */ |
f8fd4fc4 | 3696 | abi_int pr_fpvalid; /* XXX */ |
edf8e2af MW |
3697 | }; |
3698 | ||
3699 | #define ELF_PRARGSZ (80) /* Number of chars for args */ | |
3700 | ||
a2547a13 | 3701 | struct target_elf_prpsinfo { |
edf8e2af MW |
3702 | char pr_state; /* numeric process state */ |
3703 | char pr_sname; /* char for pr_state */ | |
3704 | char pr_zomb; /* zombie */ | |
3705 | char pr_nice; /* nice val */ | |
ca98ac83 | 3706 | abi_ulong pr_flag; /* flags */ |
c227f099 AL |
3707 | target_uid_t pr_uid; |
3708 | target_gid_t pr_gid; | |
3709 | target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid; | |
edf8e2af | 3710 | /* Lots missing */ |
d7eb2b92 | 3711 | char pr_fname[16] QEMU_NONSTRING; /* filename of executable */ |
edf8e2af MW |
3712 | char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */ |
3713 | }; | |
3714 | ||
3715 | /* Here is the structure in which status of each thread is captured. */ | |
3716 | struct elf_thread_status { | |
72cf2d4f | 3717 | QTAILQ_ENTRY(elf_thread_status) ets_link; |
a2547a13 | 3718 | struct target_elf_prstatus prstatus; /* NT_PRSTATUS */ |
edf8e2af MW |
3719 | #if 0 |
3720 | elf_fpregset_t fpu; /* NT_PRFPREG */ | |
3721 | struct task_struct *thread; | |
3722 | elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */ | |
3723 | #endif | |
3724 | struct memelfnote notes[1]; | |
3725 | int num_notes; | |
3726 | }; | |
3727 | ||
3728 | struct elf_note_info { | |
3729 | struct memelfnote *notes; | |
a2547a13 LD |
3730 | struct target_elf_prstatus *prstatus; /* NT_PRSTATUS */ |
3731 | struct target_elf_prpsinfo *psinfo; /* NT_PRPSINFO */ | |
edf8e2af | 3732 | |
b58deb34 | 3733 | QTAILQ_HEAD(, elf_thread_status) thread_list; |
edf8e2af MW |
3734 | #if 0 |
3735 | /* | |
3736 | * Current version of ELF coredump doesn't support | |
3737 | * dumping fp regs etc. | |
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 | ||
3747 | struct vm_area_struct { | |
1a1c4db9 MI |
3748 | target_ulong vma_start; /* start vaddr of memory region */ |
3749 | target_ulong vma_end; /* end vaddr of memory region */ | |
3750 | abi_ulong vma_flags; /* protection etc. flags for the region */ | |
72cf2d4f | 3751 | QTAILQ_ENTRY(vm_area_struct) vma_link; |
edf8e2af MW |
3752 | }; |
3753 | ||
3754 | struct mm_struct { | |
72cf2d4f | 3755 | QTAILQ_HEAD(, vm_area_struct) mm_mmap; |
edf8e2af MW |
3756 | int mm_count; /* number of mappings */ |
3757 | }; | |
3758 | ||
3759 | static struct mm_struct *vma_init(void); | |
3760 | static void vma_delete(struct mm_struct *); | |
1a1c4db9 MI |
3761 | static int vma_add_mapping(struct mm_struct *, target_ulong, |
3762 | target_ulong, abi_ulong); | |
edf8e2af MW |
3763 | static int vma_get_mapping_count(const struct mm_struct *); |
3764 | static struct vm_area_struct *vma_first(const struct mm_struct *); | |
3765 | static struct vm_area_struct *vma_next(struct vm_area_struct *); | |
3766 | static abi_ulong vma_dump_size(const struct vm_area_struct *); | |
1a1c4db9 | 3767 | static int vma_walker(void *priv, target_ulong start, target_ulong end, |
d97ef72e | 3768 | unsigned long flags); |
edf8e2af MW |
3769 | |
3770 | static void fill_elf_header(struct elfhdr *, int, uint16_t, uint32_t); | |
3771 | static void fill_note(struct memelfnote *, const char *, int, | |
d97ef72e | 3772 | unsigned int, void *); |
a2547a13 LD |
3773 | static void fill_prstatus(struct target_elf_prstatus *, const TaskState *, int); |
3774 | static int fill_psinfo(struct target_elf_prpsinfo *, const TaskState *); | |
edf8e2af MW |
3775 | static void fill_auxv_note(struct memelfnote *, const TaskState *); |
3776 | static void fill_elf_note_phdr(struct elf_phdr *, int, off_t); | |
3777 | static size_t note_size(const struct memelfnote *); | |
3778 | static void free_note_info(struct elf_note_info *); | |
9349b4f9 AF |
3779 | static int fill_note_info(struct elf_note_info *, long, const CPUArchState *); |
3780 | static void fill_thread_info(struct elf_note_info *, const CPUArchState *); | |
edf8e2af MW |
3781 | |
3782 | static int dump_write(int, const void *, size_t); | |
3783 | static int write_note(struct memelfnote *, int); | |
3784 | static int write_note_info(struct elf_note_info *, int); | |
3785 | ||
3786 | #ifdef BSWAP_NEEDED | |
a2547a13 | 3787 | static void bswap_prstatus(struct target_elf_prstatus *prstatus) |
edf8e2af | 3788 | { |
ca98ac83 PB |
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); | |
edf8e2af | 3792 | prstatus->pr_cursig = tswap16(prstatus->pr_cursig); |
ca98ac83 PB |
3793 | prstatus->pr_sigpend = tswapal(prstatus->pr_sigpend); |
3794 | prstatus->pr_sighold = tswapal(prstatus->pr_sighold); | |
edf8e2af MW |
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 | /* cpu times are not filled, so we skip them */ | |
3800 | /* regs should be in correct format already */ | |
3801 | prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid); | |
3802 | } | |
3803 | ||
a2547a13 | 3804 | static void bswap_psinfo(struct target_elf_prpsinfo *psinfo) |
edf8e2af | 3805 | { |
ca98ac83 | 3806 | psinfo->pr_flag = tswapal(psinfo->pr_flag); |
edf8e2af MW |
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 | } | |
991f8f0c RH |
3814 | |
3815 | static 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 | |
3822 | static inline void bswap_prstatus(struct target_elf_prstatus *p) { } | |
3823 | static inline void bswap_psinfo(struct target_elf_prpsinfo *p) {} | |
3824 | static inline void bswap_note(struct elf_note *en) { } | |
edf8e2af MW |
3825 | #endif /* BSWAP_NEEDED */ |
3826 | ||
3827 | /* | |
3828 | * Minimal support for linux memory regions. These are needed | |
3829 | * when we are finding out what memory exactly belongs to | |
3830 | * emulated process. No locks needed here, as long as | |
3831 | * thread that received the signal is stopped. | |
3832 | */ | |
3833 | ||
3834 | static struct mm_struct *vma_init(void) | |
3835 | { | |
3836 | struct mm_struct *mm; | |
3837 | ||
7267c094 | 3838 | if ((mm = g_malloc(sizeof (*mm))) == NULL) |
edf8e2af MW |
3839 | return (NULL); |
3840 | ||
3841 | mm->mm_count = 0; | |
72cf2d4f | 3842 | QTAILQ_INIT(&mm->mm_mmap); |
edf8e2af MW |
3843 | |
3844 | return (mm); | |
3845 | } | |
3846 | ||
3847 | static void vma_delete(struct mm_struct *mm) | |
3848 | { | |
3849 | struct vm_area_struct *vma; | |
3850 | ||
3851 | while ((vma = vma_first(mm)) != NULL) { | |
72cf2d4f | 3852 | QTAILQ_REMOVE(&mm->mm_mmap, vma, vma_link); |
7267c094 | 3853 | g_free(vma); |
edf8e2af | 3854 | } |
7267c094 | 3855 | g_free(mm); |
edf8e2af MW |
3856 | } |
3857 | ||
1a1c4db9 MI |
3858 | static int vma_add_mapping(struct mm_struct *mm, target_ulong start, |
3859 | target_ulong end, abi_ulong flags) | |
edf8e2af MW |
3860 | { |
3861 | struct vm_area_struct *vma; | |
3862 | ||
7267c094 | 3863 | if ((vma = g_malloc0(sizeof (*vma))) == NULL) |
edf8e2af MW |
3864 | return (-1); |
3865 | ||
3866 | vma->vma_start = start; | |
3867 | vma->vma_end = end; | |
3868 | vma->vma_flags = flags; | |
3869 | ||
72cf2d4f | 3870 | QTAILQ_INSERT_TAIL(&mm->mm_mmap, vma, vma_link); |
edf8e2af MW |
3871 | mm->mm_count++; |
3872 | ||
3873 | return (0); | |
3874 | } | |
3875 | ||
3876 | static struct vm_area_struct *vma_first(const struct mm_struct *mm) | |
3877 | { | |
72cf2d4f | 3878 | return (QTAILQ_FIRST(&mm->mm_mmap)); |
edf8e2af MW |
3879 | } |
3880 | ||
3881 | static struct vm_area_struct *vma_next(struct vm_area_struct *vma) | |
3882 | { | |
72cf2d4f | 3883 | return (QTAILQ_NEXT(vma, vma_link)); |
edf8e2af MW |
3884 | } |
3885 | ||
3886 | static int vma_get_mapping_count(const struct mm_struct *mm) | |
3887 | { | |
3888 | return (mm->mm_count); | |
3889 | } | |
3890 | ||
3891 | /* | |
3892 | * Calculate file (dump) size of given memory region. | |
3893 | */ | |
3894 | static abi_ulong vma_dump_size(const struct vm_area_struct *vma) | |
3895 | { | |
3896 | /* if we cannot even read the first page, skip it */ | |
c7169b02 | 3897 | if (!access_ok_untagged(VERIFY_READ, vma->vma_start, TARGET_PAGE_SIZE)) |
edf8e2af MW |
3898 | return (0); |
3899 | ||
3900 | /* | |
3901 | * Usually we don't dump executable pages as they contain | |
3902 | * non-writable code that debugger can read directly from | |
3903 | * target library etc. However, thread stacks are marked | |
3904 | * also executable so we read in first page of given region | |
3905 | * and check whether it contains elf header. If there is | |
3906 | * no elf header, we dump it. | |
3907 | */ | |
3908 | if (vma->vma_flags & PROT_EXEC) { | |
3909 | char page[TARGET_PAGE_SIZE]; | |
3910 | ||
022625a8 PM |
3911 | if (copy_from_user(page, vma->vma_start, sizeof (page))) { |
3912 | return 0; | |
3913 | } | |
edf8e2af MW |
3914 | if ((page[EI_MAG0] == ELFMAG0) && |
3915 | (page[EI_MAG1] == ELFMAG1) && | |
3916 | (page[EI_MAG2] == ELFMAG2) && | |
3917 | (page[EI_MAG3] == ELFMAG3)) { | |
3918 | /* | |
3919 | * Mappings are possibly from ELF binary. Don't dump | |
3920 | * them. | |
3921 | */ | |
3922 | return (0); | |
3923 | } | |
3924 | } | |
3925 | ||
3926 | return (vma->vma_end - vma->vma_start); | |
3927 | } | |
3928 | ||
1a1c4db9 | 3929 | static int vma_walker(void *priv, target_ulong start, target_ulong end, |
d97ef72e | 3930 | unsigned long flags) |
edf8e2af MW |
3931 | { |
3932 | struct mm_struct *mm = (struct mm_struct *)priv; | |
3933 | ||
edf8e2af MW |
3934 | vma_add_mapping(mm, start, end, flags); |
3935 | return (0); | |
3936 | } | |
3937 | ||
3938 | static void fill_note(struct memelfnote *note, const char *name, int type, | |
d97ef72e | 3939 | unsigned int sz, void *data) |
edf8e2af MW |
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; | |
80f5ce75 LV |
3948 | note->datasz = sz; |
3949 | note->datasz_rounded = roundup(sz, sizeof (int32_t)); | |
3950 | ||
edf8e2af MW |
3951 | note->data = data; |
3952 | ||
3953 | /* | |
3954 | * We calculate rounded up note size here as specified by | |
3955 | * ELF document. | |
3956 | */ | |
3957 | note->notesz = sizeof (struct elf_note) + | |
80f5ce75 | 3958 | note->namesz_rounded + note->datasz_rounded; |
edf8e2af MW |
3959 | } |
3960 | ||
3961 | static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine, | |
d97ef72e | 3962 | uint32_t flags) |
edf8e2af MW |
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 | ||
edf8e2af | 3981 | bswap_ehdr(elf); |
edf8e2af MW |
3982 | } |
3983 | ||
3984 | static 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 | ||
991f8f0c | 3995 | bswap_phdr(phdr, 1); |
edf8e2af MW |
3996 | } |
3997 | ||
3998 | static size_t note_size(const struct memelfnote *note) | |
3999 | { | |
4000 | return (note->notesz); | |
4001 | } | |
4002 | ||
a2547a13 | 4003 | static void fill_prstatus(struct target_elf_prstatus *prstatus, |
d97ef72e | 4004 | const TaskState *ts, int signr) |
edf8e2af MW |
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 | ||
edf8e2af | 4013 | bswap_prstatus(prstatus); |
edf8e2af MW |
4014 | } |
4015 | ||
a2547a13 | 4016 | static int fill_psinfo(struct target_elf_prpsinfo *psinfo, const TaskState *ts) |
edf8e2af | 4017 | { |
900cfbca | 4018 | char *base_filename; |
edf8e2af MW |
4019 | unsigned int i, len; |
4020 | ||
4021 | (void) memset(psinfo, 0, sizeof (*psinfo)); | |
4022 | ||
5f779a3a | 4023 | len = ts->info->env_strings - ts->info->arg_strings; |
edf8e2af MW |
4024 | if (len >= ELF_PRARGSZ) |
4025 | len = ELF_PRARGSZ - 1; | |
5f779a3a | 4026 | if (copy_from_user(&psinfo->pr_psargs, ts->info->arg_strings, len)) { |
edf8e2af | 4027 | return -EFAULT; |
5f779a3a | 4028 | } |
edf8e2af MW |
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 | ||
900cfbca JM |
4041 | base_filename = g_path_get_basename(ts->bprm->filename); |
4042 | /* | |
4043 | * Using strncpy here is fine: at max-length, | |
4044 | * this field is not NUL-terminated. | |
4045 | */ | |
edf8e2af | 4046 | (void) strncpy(psinfo->pr_fname, base_filename, |
d97ef72e | 4047 | sizeof(psinfo->pr_fname)); |
edf8e2af | 4048 | |
900cfbca | 4049 | g_free(base_filename); |
edf8e2af | 4050 | bswap_psinfo(psinfo); |
edf8e2af MW |
4051 | return (0); |
4052 | } | |
4053 | ||
4054 | static 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; | |
edf8e2af | 4058 | void *ptr; |
125b0f55 | 4059 | int len = ts->info->auxv_len; |
edf8e2af MW |
4060 | |
4061 | /* | |
4062 | * Auxiliary vector is stored in target process stack. It contains | |
4063 | * {type, value} pairs that we need to dump into note. This is not | |
4064 | * strictly necessary but we do it here for sake of completeness. | |
4065 | */ | |
4066 | ||
edf8e2af MW |
4067 | /* read in whole auxv vector and copy it to memelfnote */ |
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 | * Constructs name of coredump file. We have following convention | |
4077 | * for the name: | |
4078 | * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core | |
4079 | * | |
68af19ad | 4080 | * Returns the filename |
edf8e2af | 4081 | */ |
68af19ad | 4082 | static char *core_dump_filename(const TaskState *ts) |
edf8e2af | 4083 | { |
68af19ad DB |
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); | |
edf8e2af | 4087 | |
68af19ad DB |
4088 | return g_strdup_printf("qemu_%s_%s_%d.core", |
4089 | base_filename, nowstr, (int)getpid()); | |
edf8e2af MW |
4090 | } |
4091 | ||
4092 | static 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) { /* not a seekable stream */ | |
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 | * In normal conditions, single write(2) should do but | |
4120 | * in case of socket etc. this mechanism is more portable. | |
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) { /* eof */ | |
4129 | return (-1); | |
4130 | } | |
4131 | bufp += bytes_written; | |
4132 | bytes_left -= bytes_written; | |
4133 | } while (bytes_left > 0); | |
4134 | ||
4135 | return (0); | |
4136 | } | |
4137 | ||
4138 | static 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 | ||
edf8e2af | 4146 | bswap_note(&en); |
edf8e2af MW |
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); | |
80f5ce75 | 4152 | if (dump_write(fd, men->data, men->datasz_rounded) != 0) |
edf8e2af MW |
4153 | return (-1); |
4154 | ||
4155 | return (0); | |
4156 | } | |
4157 | ||
9349b4f9 | 4158 | static void fill_thread_info(struct elf_note_info *info, const CPUArchState *env) |
edf8e2af | 4159 | { |
29a0af61 | 4160 | CPUState *cpu = env_cpu((CPUArchState *)env); |
0429a971 | 4161 | TaskState *ts = (TaskState *)cpu->opaque; |
edf8e2af MW |
4162 | struct elf_thread_status *ets; |
4163 | ||
7267c094 | 4164 | ets = g_malloc0(sizeof (*ets)); |
edf8e2af MW |
4165 | ets->num_notes = 1; /* only prstatus is dumped */ |
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), | |
d97ef72e | 4169 | &ets->prstatus); |
edf8e2af | 4170 | |
72cf2d4f | 4171 | QTAILQ_INSERT_TAIL(&info->thread_list, ets, ets_link); |
edf8e2af MW |
4172 | |
4173 | info->notes_size += note_size(&ets->notes[0]); | |
4174 | } | |
4175 | ||
6afafa86 PM |
4176 | static void init_note_info(struct elf_note_info *info) |
4177 | { | |
4178 | /* Initialize the elf_note_info structure so that it is at | |
4179 | * least safe to call free_note_info() on it. Must be | |
4180 | * called before calling fill_note_info(). | |
4181 | */ | |
4182 | memset(info, 0, sizeof (*info)); | |
4183 | QTAILQ_INIT(&info->thread_list); | |
4184 | } | |
4185 | ||
edf8e2af | 4186 | static int fill_note_info(struct elf_note_info *info, |
9349b4f9 | 4187 | long signr, const CPUArchState *env) |
edf8e2af MW |
4188 | { |
4189 | #define NUMNOTES 3 | |
29a0af61 | 4190 | CPUState *cpu = env_cpu((CPUArchState *)env); |
0429a971 | 4191 | TaskState *ts = (TaskState *)cpu->opaque; |
edf8e2af MW |
4192 | int i; |
4193 | ||
c78d65e8 | 4194 | info->notes = g_new0(struct memelfnote, NUMNOTES); |
edf8e2af MW |
4195 | if (info->notes == NULL) |
4196 | return (-ENOMEM); | |
7267c094 | 4197 | info->prstatus = g_malloc0(sizeof (*info->prstatus)); |
edf8e2af MW |
4198 | if (info->prstatus == NULL) |
4199 | return (-ENOMEM); | |
7267c094 | 4200 | info->psinfo = g_malloc0(sizeof (*info->psinfo)); |
edf8e2af MW |
4201 | if (info->prstatus == NULL) |
4202 | return (-ENOMEM); | |
4203 | ||
4204 | /* | |
4205 | * First fill in status (and registers) of current thread | |
4206 | * including process info & aux vector. | |
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, | |
d97ef72e | 4211 | sizeof (*info->prstatus), info->prstatus); |
edf8e2af MW |
4212 | fill_psinfo(info->psinfo, ts); |
4213 | fill_note(&info->notes[1], "CORE", NT_PRPSINFO, | |
d97ef72e | 4214 | sizeof (*info->psinfo), info->psinfo); |
edf8e2af MW |
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 | /* read and fill status of all threads */ | |
4223 | cpu_list_lock(); | |
bdc44640 | 4224 | CPU_FOREACH(cpu) { |
a2247f8e | 4225 | if (cpu == thread_cpu) { |
edf8e2af | 4226 | continue; |
182735ef | 4227 | } |
2f6f4290 | 4228 | fill_thread_info(info, cpu->env_ptr); |
edf8e2af MW |
4229 | } |
4230 | cpu_list_unlock(); | |
4231 | ||
4232 | return (0); | |
4233 | } | |
4234 | ||
4235 | static void free_note_info(struct elf_note_info *info) | |
4236 | { | |
4237 | struct elf_thread_status *ets; | |
4238 | ||
72cf2d4f BS |
4239 | while (!QTAILQ_EMPTY(&info->thread_list)) { |
4240 | ets = QTAILQ_FIRST(&info->thread_list); | |
4241 | QTAILQ_REMOVE(&info->thread_list, ets, ets_link); | |
7267c094 | 4242 | g_free(ets); |
edf8e2af MW |
4243 | } |
4244 | ||
7267c094 AL |
4245 | g_free(info->prstatus); |
4246 | g_free(info->psinfo); | |
4247 | g_free(info->notes); | |
edf8e2af MW |
4248 | } |
4249 | ||
4250 | static 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 | /* write prstatus, psinfo and auxv for current thread */ | |
4256 | for (i = 0; i < info->numnote; i++) | |
4257 | if ((error = write_note(&info->notes[i], fd)) != 0) | |
4258 | return (error); | |
4259 | ||
4260 | /* write prstatus for each thread */ | |
52a53afe | 4261 | QTAILQ_FOREACH(ets, &info->thread_list, ets_link) { |
edf8e2af MW |
4262 | if ((error = write_note(&ets->notes[0], fd)) != 0) |
4263 | return (error); | |
4264 | } | |
4265 | ||
4266 | return (0); | |
4267 | } | |
4268 | ||
4269 | /* | |
4270 | * Write out ELF coredump. | |
4271 | * | |
4272 | * See documentation of ELF object file format in: | |
4273 | * http://www.caldera.com/developers/devspecs/gabi41.pdf | |
4274 | * | |
4275 | * Coredump format in linux is following: | |
4276 | * | |
4277 | * 0 +----------------------+ \ | |
4278 | * | ELF header | ET_CORE | | |
4279 | * +----------------------+ | | |
4280 | * | ELF program headers | |--- headers | |
4281 | * | - NOTE section | | | |
4282 | * | - PT_LOAD sections | | | |
4283 | * +----------------------+ / | |
4284 | * | NOTEs: | | |
4285 | * | - NT_PRSTATUS | | |
4286 | * | - NT_PRSINFO | | |
4287 | * | - NT_AUXV | | |
4288 | * +----------------------+ <-- aligned to target page | |
4289 | * | Process memory dump | | |
4290 | * : : | |
4291 | * . . | |
4292 | * : : | |
4293 | * | | | |
4294 | * +----------------------+ | |
4295 | * | |
4296 | * NT_PRSTATUS -> struct elf_prstatus (per thread) | |
4297 | * NT_PRSINFO -> struct elf_prpsinfo | |
4298 | * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()). | |
4299 | * | |
4300 | * Format follows System V format as close as possible. Current | |
4301 | * version limitations are as follows: | |
4302 | * - no floating point registers are dumped | |
4303 | * | |
4304 | * Function returns 0 in case of success, negative errno otherwise. | |
4305 | * | |
4306 | * TODO: make this work also during runtime: it should be | |
4307 | * possible to force coredump from running process and then | |
4308 | * continue processing. For example qemu could set up SIGUSR2 | |
4309 | * handler (provided that target process haven't registered | |
4310 | * handler for that) that does the dump when signal is received. | |
4311 | */ | |
9349b4f9 | 4312 | static int elf_core_dump(int signr, const CPUArchState *env) |
edf8e2af | 4313 | { |
29a0af61 | 4314 | const CPUState *cpu = env_cpu((CPUArchState *)env); |
0429a971 | 4315 | const TaskState *ts = (const TaskState *)cpu->opaque; |
edf8e2af | 4316 | struct vm_area_struct *vma = NULL; |
68af19ad | 4317 | g_autofree char *corefile = NULL; |
edf8e2af MW |
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 | ||
6afafa86 PM |
4327 | init_note_info(&info); |
4328 | ||
edf8e2af MW |
4329 | errno = 0; |
4330 | getrlimit(RLIMIT_CORE, &dumpsize); | |
4331 | if (dumpsize.rlim_cur == 0) | |
d97ef72e | 4332 | return 0; |
edf8e2af | 4333 | |
68af19ad | 4334 | corefile = core_dump_filename(ts); |
edf8e2af MW |
4335 | |
4336 | if ((fd = open(corefile, O_WRONLY | O_CREAT, | |
d97ef72e | 4337 | S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0) |
edf8e2af MW |
4338 | return (-errno); |
4339 | ||
4340 | /* | |
4341 | * Walk through target process memory mappings and | |
4342 | * set up structure containing this information. After | |
4343 | * this point vma_xxx functions can be used. | |
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 | * Construct valid coredump ELF header. We also | |
4353 | * add one more segment for notes. | |
4354 | */ | |
4355 | fill_elf_header(&elf, segs + 1, ELF_MACHINE, 0); | |
4356 | if (dump_write(fd, &elf, sizeof (elf)) != 0) | |
4357 | goto out; | |
4358 | ||
b6af0975 | 4359 | /* fill in the in-memory version of notes */ |
edf8e2af MW |
4360 | if (fill_note_info(&info, signr, env) < 0) |
4361 | goto out; | |
4362 | ||
4363 | offset += sizeof (elf); /* elf header */ | |
4364 | offset += (segs + 1) * sizeof (struct elf_phdr); /* program headers */ | |
4365 | ||
4366 | /* write out notes program header */ | |
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 | * ELF specification wants data to start at page boundary so | |
4375 | * we align it here. | |
4376 | */ | |
80f5ce75 | 4377 | data_offset = offset = roundup(offset, ELF_EXEC_PAGESIZE); |
edf8e2af MW |
4378 | |
4379 | /* | |
4380 | * Write program headers for memory regions mapped in | |
4381 | * the target process. | |
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 | ||
80f5ce75 | 4400 | bswap_phdr(&phdr, 1); |
772034b6 PM |
4401 | if (dump_write(fd, &phdr, sizeof(phdr)) != 0) { |
4402 | goto out; | |
4403 | } | |
edf8e2af MW |
4404 | } |
4405 | ||
4406 | /* | |
4407 | * Next we write notes just after program headers. No | |
4408 | * alignment needed here. | |
4409 | */ | |
4410 | if (write_note_info(&info, fd) < 0) | |
4411 | goto out; | |
4412 | ||
4413 | /* align data to page boundary */ | |
edf8e2af MW |
4414 | if (lseek(fd, data_offset, SEEK_SET) != data_offset) |
4415 | goto out; | |
4416 | ||
4417 | /* | |
4418 | * Finally we can dump process memory into corefile as well. | |
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; | |
d97ef72e | 4427 | addr += TARGET_PAGE_SIZE) { |
edf8e2af MW |
4428 | char page[TARGET_PAGE_SIZE]; |
4429 | int error; | |
4430 | ||
4431 | /* | |
4432 | * Read in page from target process memory and | |
4433 | * write it to coredump file. | |
4434 | */ | |
4435 | error = copy_from_user(page, addr, sizeof (page)); | |
4436 | if (error != 0) { | |
49995e17 | 4437 | (void) fprintf(stderr, "unable to dump " TARGET_ABI_FMT_lx "\n", |
d97ef72e | 4438 | addr); |
edf8e2af MW |
4439 | errno = -error; |
4440 | goto out; | |
4441 | } | |
4442 | if (dump_write(fd, page, TARGET_PAGE_SIZE) < 0) | |
4443 | goto out; | |
4444 | } | |
4445 | } | |
4446 | ||
d97ef72e | 4447 | out: |
edf8e2af MW |
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 | } | |
edf8e2af MW |
4457 | #endif /* USE_ELF_CORE_DUMP */ |
4458 | ||
e5fe0c52 PB |
4459 | void do_init_thread(struct target_pt_regs *regs, struct image_info *infop) |
4460 | { | |
4461 | init_thread(regs, infop); | |
4462 | } |