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