arm64: Handle sp, lr, fp as DwReg in CfiExpr
[valgrind.git] / coregrind / m_initimg / initimg-linux.c
blob73dab34363a0bb59914d8f9861b3a1a35c79a6a2
2 /*--------------------------------------------------------------------*/
3 /*--- Startup: create initial process image on Linux ---*/
4 /*--- initimg-linux.c ---*/
5 /*--------------------------------------------------------------------*/
7 /*
8 This file is part of Valgrind, a dynamic binary instrumentation
9 framework.
11 Copyright (C) 2000-2017 Julian Seward
12 jseward@acm.org
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation; either version 2 of the
17 License, or (at your option) any later version.
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, see <http://www.gnu.org/licenses/>.
27 The GNU General Public License is contained in the file COPYING.
30 #if defined(VGO_linux)
32 #include "pub_core_basics.h"
33 #include "pub_core_vki.h"
34 #include "pub_core_debuglog.h"
35 #include "pub_core_libcbase.h"
36 #include "pub_core_libcassert.h"
37 #include "pub_core_libcfile.h"
38 #include "pub_core_libcproc.h"
39 #include "pub_core_libcprint.h"
40 #include "pub_core_xarray.h"
41 #include "pub_core_clientstate.h"
42 #include "pub_core_aspacemgr.h"
43 #include "pub_core_mallocfree.h"
44 #include "pub_core_machine.h"
45 #include "pub_core_ume.h"
46 #include "pub_core_options.h"
47 #include "pub_core_syscall.h"
48 #include "pub_core_tooliface.h" /* VG_TRACK */
49 #include "pub_core_threadstate.h" /* ThreadArchState */
50 #include "pub_core_pathscan.h" /* find_executable */
51 #include "pub_core_initimg.h" /* self */
53 /* --- !!! --- EXTERNAL HEADERS start --- !!! --- */
54 #define _GNU_SOURCE
55 #define _FILE_OFFSET_BITS 64
56 /* This is for ELF types etc, and also the AT_ constants. */
57 #include <elf.h>
58 /* --- !!! --- EXTERNAL HEADERS end --- !!! --- */
61 /*====================================================================*/
62 /*=== Loading the client ===*/
63 /*====================================================================*/
65 /* Load the client whose name is VG_(argv_the_exename). */
67 static void load_client ( /*MOD*/ExeInfo* info,
68 /*OUT*/Addr* client_ip,
69 /*OUT*/Addr* client_toc)
71 const HChar* exe_name;
72 Int ret;
73 SysRes res;
75 vg_assert( VG_(args_the_exename) != NULL);
76 exe_name = VG_(find_executable)( VG_(args_the_exename) );
78 if (!exe_name) {
79 VG_(printf)("valgrind: %s: command not found\n", VG_(args_the_exename));
80 VG_(exit)(127); // 127 is Posix NOTFOUND
83 ret = VG_(do_exec)(exe_name, info);
84 if (ret < 0) {
85 VG_(printf)("valgrind: could not execute '%s'\n", exe_name);
86 VG_(exit)(1);
89 // The client was successfully loaded! Continue.
91 /* Get hold of a file descriptor which refers to the client
92 executable. This is needed for attaching to GDB. */
93 res = VG_(open)(exe_name, VKI_O_RDONLY, VKI_S_IRUSR);
94 if (!sr_isError(res))
95 VG_(cl_exec_fd) = sr_Res(res);
97 /* Copy necessary bits of 'info' that were filled in */
98 *client_ip = info->init_ip;
99 *client_toc = info->init_toc;
100 VG_(brk_base) = VG_(brk_limit) = VG_PGROUNDUP(info->brkbase);
104 /*====================================================================*/
105 /*=== Setting up the client's environment ===*/
106 /*====================================================================*/
108 /* Prepare the client's environment. This is basically a copy of our
109 environment, except:
111 LD_PRELOAD=$VALGRIND_LIB/vgpreload_core-PLATFORM.so:
112 ($VALGRIND_LIB/vgpreload_TOOL-PLATFORM.so:)?
113 $LD_PRELOAD
115 If this is missing, then it is added.
117 Also, remove any binding for VALGRIND_LAUNCHER=. The client should
118 not be able to see this.
120 If this needs to handle any more variables it should be hacked
121 into something table driven. The copy is VG_(malloc)'d space.
123 static HChar** setup_client_env ( HChar** origenv, const HChar* toolname)
125 vg_assert(origenv);
126 vg_assert(toolname);
128 const HChar* preload_core = "vgpreload_core";
129 const HChar* ld_preload = "LD_PRELOAD=";
130 const HChar* v_launcher = VALGRIND_LAUNCHER "=";
131 Int ld_preload_len = VG_(strlen)( ld_preload );
132 Int v_launcher_len = VG_(strlen)( v_launcher );
133 Bool ld_preload_done = False;
134 Int vglib_len = VG_(strlen)(VG_(libdir));
135 Bool debug = False;
137 HChar** cpp;
138 HChar** ret;
139 HChar* preload_tool_path;
140 Int envc, i;
142 /* Alloc space for the vgpreload_core.so path and vgpreload_<tool>.so
143 paths. We might not need the space for vgpreload_<tool>.so, but it
144 doesn't hurt to over-allocate briefly. The 16s are just cautious
145 slop. */
146 Int preload_core_path_len = vglib_len + sizeof(preload_core)
147 + sizeof(VG_PLATFORM) + 16;
148 Int preload_tool_path_len = vglib_len + VG_(strlen)(toolname)
149 + sizeof(VG_PLATFORM) + 16;
150 Int preload_string_len = preload_core_path_len + preload_tool_path_len;
151 HChar* preload_string = VG_(malloc)("initimg-linux.sce.1",
152 preload_string_len);
153 /* Determine if there's a vgpreload_<tool>_<platform>.so file, and setup
154 preload_string. */
155 preload_tool_path = VG_(malloc)("initimg-linux.sce.2", preload_tool_path_len);
156 VG_(snprintf)(preload_tool_path, preload_tool_path_len,
157 "%s/vgpreload_%s-%s.so", VG_(libdir), toolname, VG_PLATFORM);
158 if (VG_(access)(preload_tool_path, True/*r*/, False/*w*/, False/*x*/) == 0) {
159 VG_(snprintf)(preload_string, preload_string_len, "%s/%s-%s.so:%s",
160 VG_(libdir), preload_core, VG_PLATFORM, preload_tool_path);
161 } else {
162 VG_(snprintf)(preload_string, preload_string_len, "%s/%s-%s.so",
163 VG_(libdir), preload_core, VG_PLATFORM);
165 VG_(free)(preload_tool_path);
167 VG_(debugLog)(2, "initimg", "preload_string:\n");
168 VG_(debugLog)(2, "initimg", " \"%s\"\n", preload_string);
170 /* Count the original size of the env */
171 if (debug) VG_(printf)("\n\n");
172 envc = 0;
173 for (cpp = origenv; cpp && *cpp; cpp++) {
174 envc++;
175 if (debug) VG_(printf)("XXXXXXXXX: BEFORE %s\n", *cpp);
178 /* Allocate a new space */
179 ret = VG_(malloc) ("initimg-linux.sce.3",
180 sizeof(HChar *) * (envc+1+1)); /* 1 new entry + NULL */
182 /* copy it over */
183 for (cpp = ret; *origenv; ) {
184 if (debug) VG_(printf)("XXXXXXXXX: COPY %s\n", *origenv);
185 *cpp++ = *origenv++;
187 *cpp = NULL;
189 vg_assert(envc == (cpp - ret));
191 /* Walk over the new environment, mashing as we go */
192 for (cpp = ret; cpp && *cpp; cpp++) {
193 if (VG_(memcmp)(*cpp, ld_preload, ld_preload_len) == 0) {
194 Int len = VG_(strlen)(*cpp) + preload_string_len;
195 HChar *cp = VG_(malloc)("initimg-linux.sce.4", len);
197 VG_(snprintf)(cp, len, "%s%s:%s",
198 ld_preload, preload_string, (*cpp)+ld_preload_len);
200 *cpp = cp;
202 ld_preload_done = True;
204 if (debug) VG_(printf)("XXXXXXXXX: MASH %s\n", *cpp);
207 /* Add the missing bits */
208 if (!ld_preload_done) {
209 Int len = ld_preload_len + preload_string_len;
210 HChar *cp = VG_(malloc) ("initimg-linux.sce.5", len);
212 VG_(snprintf)(cp, len, "%s%s", ld_preload, preload_string);
214 ret[envc++] = cp;
215 if (debug) VG_(printf)("XXXXXXXXX: ADD %s\n", cp);
218 /* ret[0 .. envc-1] is live now. */
219 /* Find and remove a binding for VALGRIND_LAUNCHER. */
220 for (i = 0; i < envc; i++)
221 if (0 == VG_(memcmp)(ret[i], v_launcher, v_launcher_len))
222 break;
224 if (i < envc) {
225 for (; i < envc-1; i++)
226 ret[i] = ret[i+1];
227 envc--;
230 VG_(free)(preload_string);
231 ret[envc] = NULL;
233 for (i = 0; i < envc; i++) {
234 if (debug) VG_(printf)("XXXXXXXXX: FINAL %s\n", ret[i]);
237 return ret;
241 /*====================================================================*/
242 /*=== Setting up the client's stack ===*/
243 /*====================================================================*/
245 #ifndef AT_DCACHEBSIZE
246 #define AT_DCACHEBSIZE 19
247 #endif /* AT_DCACHEBSIZE */
249 #ifndef AT_ICACHEBSIZE
250 #define AT_ICACHEBSIZE 20
251 #endif /* AT_ICACHEBSIZE */
253 #ifndef AT_UCACHEBSIZE
254 #define AT_UCACHEBSIZE 21
255 #endif /* AT_UCACHEBSIZE */
257 #ifndef AT_BASE_PLATFORM
258 #define AT_BASE_PLATFORM 24
259 #endif /* AT_BASE_PLATFORM */
261 #ifndef AT_RANDOM
262 #define AT_RANDOM 25
263 #endif /* AT_RANDOM */
265 #ifndef AT_HWCAP2
266 #define AT_HWCAP2 26
267 #endif /* AT_HWCAP2 */
269 #ifndef AT_EXECFN
270 #define AT_EXECFN 31
271 #endif /* AT_EXECFN */
273 #ifndef AT_SYSINFO
274 #define AT_SYSINFO 32
275 #endif /* AT_SYSINFO */
277 #ifndef AT_SYSINFO_EHDR
278 #define AT_SYSINFO_EHDR 33
279 #endif /* AT_SYSINFO_EHDR */
281 #ifndef AT_SECURE
282 #define AT_SECURE 23 /* secure mode boolean */
283 #endif /* AT_SECURE */
285 /* Add a string onto the string table, and return its address */
286 static HChar *copy_str(HChar **tab, const HChar *str)
288 HChar *cp = *tab;
289 HChar *orig = cp;
291 while(*str)
292 *cp++ = *str++;
293 *cp++ = '\0';
295 if (0)
296 VG_(printf)("copied %p \"%s\" len %lld\n", orig, orig, (Long)(cp-orig));
298 *tab = cp;
300 return orig;
304 /* ----------------------------------------------------------------
306 This sets up the client's initial stack, containing the args,
307 environment and aux vector.
309 The format of the stack is:
311 higher address +-----------------+ <- clstack_end
313 : string table :
315 +-----------------+
316 | AT_NULL |
318 | auxv |
319 +-----------------+
320 | NULL |
322 | envp |
323 +-----------------+
324 | NULL |
326 | argv |
327 +-----------------+
328 | argc |
329 lower address +-----------------+ <- sp
330 | undefined |
333 Allocate and create the initial client stack. It is allocated down
334 from clstack_end, which was previously determined by the address
335 space manager. The returned value is the SP value for the client.
337 The client's auxv is created by copying and modifying our own one.
338 As a side effect of scanning our own auxv, some important bits of
339 info are collected:
341 VG_(cache_line_size_ppc32) // ppc32 only -- cache line size
342 VG_(have_altivec_ppc32) // ppc32 only -- is Altivec supported?
344 ---------------------------------------------------------------- */
346 struct auxv
348 Word a_type;
349 union {
350 void *a_ptr;
351 Word a_val;
352 } u;
355 static
356 struct auxv *find_auxv(UWord* sp)
358 sp++; // skip argc (Nb: is word-sized, not int-sized!)
360 while (*sp != 0) // skip argv
361 sp++;
362 sp++;
364 while (*sp != 0) // skip env
365 sp++;
366 sp++;
368 #if defined(VGA_ppc32) || defined(VGA_ppc64be) || defined(VGA_ppc64le)
369 # if defined AT_IGNOREPPC
370 while (*sp == AT_IGNOREPPC) // skip AT_IGNOREPPC entries
371 sp += 2;
372 # endif
373 #endif
375 return (struct auxv *)sp;
378 static
379 Addr setup_client_stack( void* init_sp,
380 HChar** orig_envp,
381 const ExeInfo* info,
382 UInt** client_auxv,
383 Addr clstack_end,
384 SizeT clstack_max_size,
385 const VexArchInfo* vex_archinfo )
387 /* The HW configuration setting (hwcaps) of the target can be
388 * checked against the Vex settings of the host platform as given
389 * by the values in vex_archinfo.
392 SysRes res;
393 HChar **cpp;
394 HChar *strtab; /* string table */
395 HChar *stringbase;
396 Addr *ptr;
397 struct auxv *auxv;
398 const struct auxv *orig_auxv;
399 const struct auxv *cauxv;
400 unsigned stringsize; /* total size of strings in bytes */
401 unsigned auxsize; /* total size of auxv in bytes */
402 Int argc; /* total argc */
403 Int envc; /* total number of env vars */
404 unsigned stacksize; /* total client stack size */
405 Addr client_SP; /* client stack base (initial SP) */
406 Addr clstack_start;
407 Int i;
409 vg_assert(VG_IS_PAGE_ALIGNED(clstack_end+1));
410 vg_assert( VG_(args_for_client) );
412 /* use our own auxv as a prototype */
413 orig_auxv = find_auxv(init_sp);
415 /* ==================== compute sizes ==================== */
417 /* first of all, work out how big the client stack will be */
418 stringsize = 0;
420 /* paste on the extra args if the loader needs them (ie, the #!
421 interpreter and its argument) */
422 argc = 0;
423 if (info->interp_name != NULL) {
424 argc++;
425 stringsize += VG_(strlen)(info->interp_name) + 1;
427 if (info->interp_args != NULL) {
428 argc++;
429 stringsize += VG_(strlen)(info->interp_args) + 1;
432 /* now scan the args we're given... */
433 stringsize += VG_(strlen)( VG_(args_the_exename) ) + 1;
435 for (i = 0; i < VG_(sizeXA)( VG_(args_for_client) ); i++) {
436 argc++;
437 stringsize += VG_(strlen)( * (HChar**)
438 VG_(indexXA)( VG_(args_for_client), i ))
439 + 1;
442 /* ...and the environment */
443 envc = 0;
444 for (cpp = orig_envp; cpp && *cpp; cpp++) {
445 envc++;
446 stringsize += VG_(strlen)(*cpp) + 1;
449 /* now, how big is the auxv? */
450 auxsize = sizeof(*auxv); /* there's always at least one entry: AT_NULL */
451 for (cauxv = orig_auxv; cauxv->a_type != AT_NULL; cauxv++) {
452 if (cauxv->a_type == AT_PLATFORM ||
453 cauxv->a_type == AT_BASE_PLATFORM)
454 stringsize += VG_(strlen)(cauxv->u.a_ptr) + 1;
455 else if (cauxv->a_type == AT_RANDOM)
456 stringsize += 16;
457 else if (cauxv->a_type == AT_EXECFN)
458 stringsize += VG_(strlen)(VG_(args_the_exename)) + 1;
459 auxsize += sizeof(*cauxv);
462 # if defined(VGP_ppc32_linux) || defined(VGP_ppc64be_linux) \
463 || defined(VGP_ppc64le_linux)
464 auxsize += 2 * sizeof(*cauxv);
465 # endif
467 /* OK, now we know how big the client stack is */
468 stacksize =
469 sizeof(Word) + /* argc */
470 sizeof(HChar **) + /* argc[0] == exename */
471 sizeof(HChar **)*argc + /* argv */
472 sizeof(HChar **) + /* terminal NULL */
473 sizeof(HChar **)*envc + /* envp */
474 sizeof(HChar **) + /* terminal NULL */
475 auxsize + /* auxv */
476 VG_ROUNDUP(stringsize, sizeof(Word)); /* strings (aligned) */
478 if (0) VG_(printf)("stacksize = %u\n", stacksize);
480 /* client_SP is the client's stack pointer */
481 client_SP = clstack_end - stacksize;
482 client_SP = VG_ROUNDDN(client_SP, 16); /* make stack 16 byte aligned */
484 /* base of the string table (aligned) */
485 stringbase = strtab = (HChar *)clstack_end
486 - VG_ROUNDUP(stringsize, sizeof(int));
488 clstack_start = VG_PGROUNDDN(client_SP);
490 /* The max stack size */
491 clstack_max_size = VG_PGROUNDUP(clstack_max_size);
493 if (0)
494 VG_(printf)("stringsize=%u auxsize=%u stacksize=%u maxsize=0x%lx\n"
495 "clstack_start %p\n"
496 "clstack_end %p\n",
497 stringsize, auxsize, stacksize, clstack_max_size,
498 (void*)clstack_start, (void*)clstack_end);
500 /* ==================== allocate space ==================== */
502 { SizeT anon_size = clstack_end - clstack_start + 1;
503 SizeT resvn_size = clstack_max_size - anon_size;
504 Addr anon_start = clstack_start;
505 Addr resvn_start = anon_start - resvn_size;
506 SizeT inner_HACK = 0;
507 Bool ok;
509 /* So far we've only accounted for space requirements down to the
510 stack pointer. If this target's ABI requires a redzone below
511 the stack pointer, we need to allocate an extra page, to
512 handle the worst case in which the stack pointer is almost at
513 the bottom of a page, and so there is insufficient room left
514 over to put the redzone in. In this case the simple thing to
515 do is allocate an extra page, by shrinking the reservation by
516 one page and growing the anonymous area by a corresponding
517 page. */
518 vg_assert(VG_STACK_REDZONE_SZB >= 0);
519 vg_assert(VG_STACK_REDZONE_SZB < VKI_PAGE_SIZE);
520 if (VG_STACK_REDZONE_SZB > 0) {
521 vg_assert(resvn_size > VKI_PAGE_SIZE);
522 resvn_size -= VKI_PAGE_SIZE;
523 anon_start -= VKI_PAGE_SIZE;
524 anon_size += VKI_PAGE_SIZE;
527 vg_assert(VG_IS_PAGE_ALIGNED(anon_size));
528 vg_assert(VG_IS_PAGE_ALIGNED(resvn_size));
529 vg_assert(VG_IS_PAGE_ALIGNED(anon_start));
530 vg_assert(VG_IS_PAGE_ALIGNED(resvn_start));
531 vg_assert(resvn_start == clstack_end + 1 - clstack_max_size);
533 # ifdef ENABLE_INNER
534 inner_HACK = 1024*1024; // create 1M non-fault-extending stack
535 # endif
537 if (0)
538 VG_(printf)("%#lx 0x%lx %#lx 0x%lx\n",
539 resvn_start, resvn_size, anon_start, anon_size);
541 /* Create a shrinkable reservation followed by an anonymous
542 segment. Together these constitute a growdown stack. */
543 res = VG_(mk_SysRes_Error)(0);
544 ok = VG_(am_create_reservation)(
545 resvn_start,
546 resvn_size -inner_HACK,
547 SmUpper,
548 anon_size +inner_HACK
550 if (ok) {
551 /* allocate a stack - mmap enough space for the stack */
552 res = VG_(am_mmap_anon_fixed_client)(
553 anon_start -inner_HACK,
554 anon_size +inner_HACK,
555 info->stack_prot
558 if ((!ok) || sr_isError(res)) {
559 /* Allocation of the stack failed. We have to stop. */
560 VG_(printf)("valgrind: "
561 "I failed to allocate space for the application's stack.\n");
562 VG_(printf)("valgrind: "
563 "This may be the result of a very large --main-stacksize=\n");
564 VG_(printf)("valgrind: setting. Cannot continue. Sorry.\n\n");
565 VG_(exit)(1);
568 vg_assert(ok);
569 vg_assert(!sr_isError(res));
571 /* Record stack extent -- needed for stack-change code. */
572 VG_(clstk_start_base) = anon_start -inner_HACK;
573 VG_(clstk_end) = VG_(clstk_start_base) + anon_size +inner_HACK -1;
577 /* ==================== create client stack ==================== */
579 ptr = (Addr*)client_SP;
581 /* --- client argc --- */
582 *ptr++ = argc + 1;
584 /* --- client argv --- */
585 if (info->interp_name)
586 *ptr++ = (Addr)copy_str(&strtab, info->interp_name);
587 if (info->interp_args)
588 *ptr++ = (Addr)copy_str(&strtab, info->interp_args);
590 *ptr++ = (Addr)copy_str(&strtab, VG_(args_the_exename));
592 for (i = 0; i < VG_(sizeXA)( VG_(args_for_client) ); i++) {
593 *ptr++ = (Addr)copy_str(
594 &strtab,
595 * (HChar**) VG_(indexXA)( VG_(args_for_client), i )
598 *ptr++ = 0;
600 /* --- envp --- */
601 VG_(client_envp) = (HChar **)ptr;
602 for (cpp = orig_envp; cpp && *cpp; ptr++, cpp++)
603 *ptr = (Addr)copy_str(&strtab, *cpp);
604 *ptr++ = 0;
606 /* --- auxv --- */
607 auxv = (struct auxv *)ptr;
608 *client_auxv = (UInt *)auxv;
609 VG_(client_auxv) = (UWord *)*client_auxv;
610 // ??? According to 'man proc', auxv is a array of unsigned long
611 // terminated by two zeros. Why is valgrind working with UInt ?
612 // We do not take ULong* (as ULong 8 bytes on a 32 bits),
613 // => we take UWord*
615 # if defined(VGP_ppc32_linux) || defined(VGP_ppc64be_linux) \
616 || defined(VGP_ppc64le_linux)
617 auxv[0].a_type = AT_IGNOREPPC;
618 auxv[0].u.a_val = AT_IGNOREPPC;
619 auxv[1].a_type = AT_IGNOREPPC;
620 auxv[1].u.a_val = AT_IGNOREPPC;
621 auxv += 2;
622 # endif
624 for (; orig_auxv->a_type != AT_NULL; auxv++, orig_auxv++) {
626 /* copy the entry... */
627 *auxv = *orig_auxv;
629 /* ...and fix up / examine the copy */
630 switch(auxv->a_type) {
632 case AT_IGNORE:
633 case AT_PHENT:
634 case AT_PAGESZ:
635 case AT_FLAGS:
636 case AT_NOTELF:
637 case AT_UID:
638 case AT_EUID:
639 case AT_GID:
640 case AT_EGID:
641 case AT_CLKTCK:
642 # if !defined(VGPV_arm_linux_android) \
643 && !defined(VGPV_x86_linux_android) \
644 && !defined(VGPV_mips32_linux_android) \
645 && !defined(VGPV_arm64_linux_android)
646 case AT_FPUCW: /* missing on android */
647 # endif
648 /* All these are pointerless, so we don't need to do
649 anything about them. */
650 break;
652 case AT_PHDR:
653 if (info->phdr == 0)
654 auxv->a_type = AT_IGNORE;
655 else
656 auxv->u.a_val = info->phdr;
657 break;
659 case AT_PHNUM:
660 if (info->phdr == 0)
661 auxv->a_type = AT_IGNORE;
662 else
663 auxv->u.a_val = info->phnum;
664 break;
666 case AT_BASE:
667 auxv->u.a_val = info->interp_offset;
668 break;
670 case AT_PLATFORM:
671 case AT_BASE_PLATFORM:
672 /* points to a platform description string */
673 auxv->u.a_ptr = copy_str(&strtab, orig_auxv->u.a_ptr);
674 break;
676 case AT_ENTRY:
677 auxv->u.a_val = info->entry;
678 break;
680 case AT_HWCAP:
681 # if defined(VGP_arm_linux)
682 { Bool has_neon = (auxv->u.a_val & VKI_HWCAP_NEON) > 0;
683 VG_(debugLog)(2, "initimg",
684 "ARM has-neon from-auxv: %s\n",
685 has_neon ? "YES" : "NO");
686 VG_(machine_arm_set_has_NEON)( has_neon );
687 # define VKI_HWCAP_TLS 32768
688 Bool has_tls = (auxv->u.a_val & VKI_HWCAP_TLS) > 0;
689 # undef VKI_HWCAP_TLS
690 VG_(debugLog)(2, "initimg",
691 "ARM has-tls from-auxv: %s\n",
692 has_tls ? "YES" : "NO");
693 /* If real hw sets properly HWCAP_TLS, we might
694 use this info to decide to really execute set_tls syscall
695 in syswrap-arm-linux.c rather than to base this on
696 conditional compilation. */
698 # elif defined(VGP_s390x_linux)
700 /* Out of the hardware features available on the platform,
701 advertise those "below" TE, as well as the ones explicitly
702 ORed in the expression below. Anything else, such as TE
703 itself, is not supported by Valgrind. */
704 auxv->u.a_val &= ((VKI_HWCAP_S390_TE - 1)
705 | VKI_HWCAP_S390_VXRS
706 | VKI_HWCAP_S390_VXRS_EXT);
708 # elif defined(VGP_arm64_linux)
710 /* Limit the AT_HWCAP to just those features we explicitly
711 support in VEX. */
712 #define ARM64_SUPPORTED_HWCAP (VKI_HWCAP_ATOMICS \
713 | VKI_HWCAP_AES \
714 | VKI_HWCAP_PMULL \
715 | VKI_HWCAP_SHA1 \
716 | VKI_HWCAP_SHA2 \
717 | VKI_HWCAP_CRC32 \
718 | VKI_HWCAP_FP \
719 | VKI_HWCAP_ASIMD)
720 auxv->u.a_val &= ARM64_SUPPORTED_HWCAP;
722 # endif
723 break;
724 # if defined(VGP_ppc64be_linux) || defined(VGP_ppc64le_linux)
725 case AT_HWCAP2: {
726 Bool auxv_2_07, hw_caps_2_07;
727 Bool auxv_3_0, hw_caps_3_0;
728 Bool auxv_3_1, hw_caps_3_1;
730 /* The HWCAP2 field may contain an arch_2_07 entry that indicates
731 * if the processor is compliant with the 2.07 ISA. (i.e. Power 8
732 * or beyond). The Valgrind vai.hwcaps value
733 * (coregrind/m_machine.c) has the VEX_HWCAPS_PPC64_ISA2_07
734 * flag set so Valgrind knows about Power8. Need to pass the
735 * HWCAP2 value along so the user level programs can detect that
736 * the processor supports ISA 2.07 and beyond.
738 /* Power Architecture 64-Bit ELF V2 ABI Specification
739 July 21, 2014, version 1.0, Page 124
740 www-03.ibm.com/technologyconnect/tgcm/TGCMServlet.wss?alias=OpenPOWER&linkid=1n0000
742 AT_HWCAP2
743 The a_val member of this entry is a bit map of hardware
744 capabilities. Some bit mask values include:
746 PPC_FEATURE2_ARCH_2_07 0x80000000
747 PPC_FEATURE2_HAS_HTM 0x40000000
748 PPC_FEATURE2_HAS_DSCR 0x20000000
749 PPC_FEATURE2_HAS_EBB 0x10000000
750 PPC_FEATURE2_HAS_ISEL 0x08000000
751 PPC_FEATURE2_HAS_TAR 0x04000000
752 PPC_FEATURE2_HAS_VCRYPTO 0x02000000
753 PPC_FEATURE2_HTM_NOSC 0x01000000
754 PPC_FEATURE2_ARCH_3_00 0x00800000
755 PPC_FEATURE2_HAS_IEEE128 0x00400000
756 PPC_FEATURE2_DARN 0x00200000
757 PPC_FEATURE2_SCV 0x00100000
758 PPC_FEATURE2_HTM_NO_SUSPEND 0x00080000
759 PPC_FEATURE2_ARCH_3_1 0x00040000
760 PPC_FEATURE2_MMA 0x00020000
762 auxv_2_07 = (auxv->u.a_val & 0x80000000ULL) == 0x80000000ULL;
763 hw_caps_2_07 = (vex_archinfo->hwcaps & VEX_HWCAPS_PPC64_ISA2_07)
764 == VEX_HWCAPS_PPC64_ISA2_07;
766 /* Verify the PPC_FEATURE2_ARCH_2_07 setting in HWCAP2
767 * matches the setting in VEX HWCAPS.
769 vg_assert(auxv_2_07 == hw_caps_2_07);
771 /* Power ISA version 3.0B
772 March 29, 2017
773 https://ibm.ent.box.com/s/1hzcwkwf8rbju5h9iyf44wm94amnlcrv
775 https://openpowerfoundation.org/technical/resource-catalog/
776 http://openpowerfoundation.org/wp-content/uploads/resources/leabi/leabi-20170510.pdf
777 64-bit ELF V2 ABI specification for Power. HWCAP2 bit pattern
778 for ISA 3.0, page 112.
781 /* ISA 3.0 */
782 auxv_3_0 = (auxv->u.a_val & 0x00800000ULL) == 0x00800000ULL;
783 hw_caps_3_0 = (vex_archinfo->hwcaps & VEX_HWCAPS_PPC64_ISA3_0)
784 == VEX_HWCAPS_PPC64_ISA3_0;
786 /* Verify the PPC_FEATURE2_ARCH_3_00 setting in HWCAP2
787 * matches the setting in VEX HWCAPS.
789 vg_assert(auxv_3_0 == hw_caps_3_0);
791 /* Power ISA version 3.1
792 https://ibm.ent.box.com/s/hhjfw0x0lrbtyzmiaffnbxh2fuo0fog0
794 64-bit ELF V? ABI specification for Power. HWCAP2 bit pattern
795 for ISA 3.0, page ?.
797 ADD PUBLIC LINK WHEN AVAILABLE
799 /* ISA 3.1 */
800 auxv_3_1 = (auxv->u.a_val & 0x00040000ULL) == 0x00040000ULL;
801 hw_caps_3_1 = (vex_archinfo->hwcaps & VEX_HWCAPS_PPC64_ISA3_1)
802 == VEX_HWCAPS_PPC64_ISA3_1;
804 /* Verify the PPC_FEATURE2_ARCH_3_1 setting in HWCAP2
805 * matches the setting in VEX HWCAPS.
807 vg_assert(auxv_3_1 == hw_caps_3_1);
809 /* Mask unrecognized HWCAP bits. Only keep the bits that have
810 * explicit support in VEX. Filter out HTM bits since the
811 * transaction begin instruction (tbegin) is always failed in
812 * Valgrind causing the code to execute the failure path.
813 * Also filter out the DARN random number (bug #411189).
814 * And the SCV syscall (bug #431157).
816 auxv->u.a_val &= (0x80000000ULL /* ARCH_2_07 */
817 | 0x20000000ULL /* DSCR */
818 | 0x10000000ULL /* EBB */
819 | 0x08000000ULL /* ISEL */
820 | 0x04000000ULL /* TAR */
821 | 0x04000000ULL /* VEC_CRYPTO */
822 | 0x00800000ULL /* ARCH_3_00 */
823 | 0x00400000ULL /* HAS_IEEE128 */
824 | 0x00040000ULL); /* ARCH_3_1 */
827 break;
828 # endif
830 case AT_ICACHEBSIZE:
831 case AT_DCACHEBSIZE:
832 case AT_UCACHEBSIZE:
833 # if defined(VGP_ppc32_linux)
834 /* acquire cache info */
835 if (auxv->u.a_val > 0) {
836 VG_(machine_ppc32_set_clszB)( auxv->u.a_val );
837 VG_(debugLog)(2, "initimg",
838 "PPC32 icache line size %u (type %u)\n",
839 (UInt)auxv->u.a_val, (UInt)auxv->a_type );
841 # elif defined(VGP_ppc64be_linux) || defined(VGP_ppc64le_linux)
842 /* acquire cache info */
843 if (auxv->u.a_val > 0) {
844 VG_(machine_ppc64_set_clszB)( auxv->u.a_val );
845 VG_(debugLog)(2, "initimg",
846 "PPC64 icache line size %u (type %u)\n",
847 (UInt)auxv->u.a_val, (UInt)auxv->a_type );
849 # endif
850 break;
852 # if defined(VGP_ppc32_linux) || defined(VGP_ppc64be_linux) \
853 || defined(VGP_ppc64le_linux)
854 case AT_IGNOREPPC:
855 break;
856 # endif
858 case AT_SECURE:
859 /* If this is 1, then it means that this program is
860 running suid, and therefore the dynamic linker should
861 be careful about LD_PRELOAD, etc. However, since
862 stage1 (the thing the kernel actually execve's) should
863 never be SUID, and we need LD_PRELOAD to work for the
864 client, we set AT_SECURE to 0. */
865 auxv->u.a_val = 0;
866 break;
868 case AT_SYSINFO:
869 /* Trash this, because we don't reproduce it */
870 auxv->a_type = AT_IGNORE;
871 break;
873 # if !defined(VGP_ppc32_linux) && !defined(VGP_ppc64be_linux) \
874 && !defined(VGP_ppc64le_linux) \
875 && !defined(VGP_mips32_linux) && !defined(VGP_mips64_linux) \
876 && !defined(VGP_nanomips_linux)
877 case AT_SYSINFO_EHDR: {
878 /* Trash this, because we don't reproduce it */
879 const NSegment* ehdrseg = VG_(am_find_nsegment)((Addr)auxv->u.a_ptr);
880 vg_assert(ehdrseg);
881 VG_(am_munmap_valgrind)(ehdrseg->start, ehdrseg->end - ehdrseg->start);
882 auxv->a_type = AT_IGNORE;
883 break;
885 # endif
887 case AT_RANDOM:
888 /* points to 16 random bytes - we need to ensure this is
889 propagated to the client as glibc will assume it is
890 present if it is built for kernel 2.6.29 or later */
891 auxv->u.a_ptr = strtab;
892 VG_(memcpy)(strtab, orig_auxv->u.a_ptr, 16);
893 strtab += 16;
894 break;
896 case AT_EXECFN:
897 /* points to the executable filename */
898 auxv->u.a_ptr = copy_str(&strtab, VG_(args_the_exename));
899 break;
901 default:
902 /* stomp out anything we don't know about */
903 VG_(debugLog)(2, "initimg",
904 "stomping auxv entry %llu\n",
905 (ULong)auxv->a_type);
906 auxv->a_type = AT_IGNORE;
907 break;
910 *auxv = *orig_auxv;
911 vg_assert(auxv->a_type == AT_NULL);
913 vg_assert((strtab-stringbase) == stringsize);
915 /* client_SP is pointing at client's argc/argv */
917 if (0) VG_(printf)("startup SP = %#lx\n", client_SP);
918 return client_SP;
922 /* Allocate the client data segment. It is an expandable anonymous
923 mapping abutting a shrinkable reservation of size max_dseg_size.
924 The data segment starts at VG_(brk_base), which is page-aligned,
925 and runs up to VG_(brk_limit), which isn't. */
927 static void setup_client_dataseg ( SizeT max_size )
929 Bool ok;
930 SysRes sres;
931 Addr anon_start = VG_(brk_base);
932 SizeT anon_size = VKI_PAGE_SIZE;
933 Addr resvn_start = anon_start + anon_size;
934 SizeT resvn_size = max_size - anon_size;
936 vg_assert(VG_IS_PAGE_ALIGNED(anon_size));
937 vg_assert(VG_IS_PAGE_ALIGNED(resvn_size));
938 vg_assert(VG_IS_PAGE_ALIGNED(anon_start));
939 vg_assert(VG_IS_PAGE_ALIGNED(resvn_start));
941 /* Because there's been no brk activity yet: */
942 vg_assert(VG_(brk_base) == VG_(brk_limit));
944 /* Try to create the data seg and associated reservation where
945 VG_(brk_base) says. */
946 ok = VG_(am_create_reservation)(
947 resvn_start,
948 resvn_size,
949 SmLower,
950 anon_size
953 if (!ok) {
954 /* Hmm, that didn't work. Well, let aspacem suggest an address
955 it likes better, and try again with that. */
956 anon_start = VG_(am_get_advisory_client_simple)
957 ( 0/*floating*/, anon_size+resvn_size, &ok );
958 if (ok) {
959 resvn_start = anon_start + anon_size;
960 ok = VG_(am_create_reservation)(
961 resvn_start,
962 resvn_size,
963 SmLower,
964 anon_size
966 if (ok)
967 VG_(brk_base) = VG_(brk_limit) = anon_start;
969 /* that too might have failed, but if it has, we're hosed: there
970 is no Plan C. */
972 vg_assert(ok);
974 /* We make the data segment (heap) executable because LinuxThreads on
975 ppc32 creates trampolines in this area. Also, on x86/Linux the data
976 segment is RWX natively, at least according to /proc/self/maps.
977 Also, having a non-executable data seg would kill any program which
978 tried to create code in the data seg and then run it. */
979 sres = VG_(am_mmap_anon_fixed_client)(
980 anon_start,
981 anon_size,
982 VKI_PROT_READ|VKI_PROT_WRITE|VKI_PROT_EXEC
984 vg_assert(!sr_isError(sres));
985 vg_assert(sr_Res(sres) == anon_start);
989 /*====================================================================*/
990 /*=== TOP-LEVEL: VG_(setup_client_initial_image) ===*/
991 /*====================================================================*/
993 /* Create the client's initial memory image. */
994 IIFinaliseImageInfo VG_(ii_create_image)( IICreateImageInfo iicii,
995 const VexArchInfo* vex_archinfo )
997 ExeInfo info;
998 HChar** env = NULL;
1000 IIFinaliseImageInfo iifii = {
1001 .clstack_max_size = 0,
1002 .initial_client_SP = 0,
1003 .initial_client_IP = 0,
1004 .initial_client_TOC = 0,
1005 .client_auxv = NULL,
1006 .arch_elf_state = VKI_INIT_ARCH_ELF_STATE,
1009 //--------------------------------------------------------------
1010 // Load client executable, finding in $PATH if necessary
1011 // p: get_helprequest_and_toolname() [for 'exec', 'need_help']
1012 // p: layout_remaining_space [so there's space]
1013 //--------------------------------------------------------------
1014 VG_(debugLog)(1, "initimg", "Loading client\n");
1016 if (VG_(args_the_exename) == NULL)
1017 VG_(err_missing_prog)();
1019 VG_(memset)(&info, 0, sizeof(info));
1020 info.arch_elf_state = &iifii.arch_elf_state;
1022 load_client(&info, &iifii.initial_client_IP, &iifii.initial_client_TOC);
1024 //--------------------------------------------------------------
1025 // Set up client's environment
1026 // p: set-libdir [for VG_(libdir)]
1027 // p: get_helprequest_and_toolname [for toolname]
1028 //--------------------------------------------------------------
1029 VG_(debugLog)(1, "initimg", "Setup client env\n");
1030 env = setup_client_env(iicii.envp, iicii.toolname);
1032 //--------------------------------------------------------------
1033 // Setup client stack, eip, and VG_(client_arg[cv])
1034 // p: load_client() [for 'info']
1035 // p: fix_environment() [for 'env']
1036 //--------------------------------------------------------------
1038 /* When allocating space for the client stack on Linux, take
1039 notice of the --main-stacksize value. This makes it possible
1040 to run programs with very large (primary) stack requirements
1041 simply by specifying --main-stacksize. */
1042 /* Logic is as follows:
1043 - by default, use the client's current stack rlimit
1044 - if that exceeds 16M, clamp to 16M
1045 - if a larger --main-stacksize value is specified, use that instead
1046 - in all situations, the minimum allowed stack size is 1M
1048 void* init_sp = iicii.argv - 1;
1049 SizeT m1 = 1024 * 1024;
1050 SizeT m16 = 16 * m1;
1051 SizeT szB = (SizeT)VG_(client_rlimit_stack).rlim_cur;
1052 if (szB < m1) szB = m1;
1053 if (szB > m16) szB = m16;
1054 if (VG_(clo_main_stacksize) > 0) szB = VG_(clo_main_stacksize);
1055 if (szB < m1) szB = m1;
1056 szB = VG_PGROUNDUP(szB);
1057 VG_(debugLog)(1, "initimg",
1058 "Setup client stack: size will be %lu\n", szB);
1060 iifii.clstack_max_size = szB;
1062 iifii.initial_client_SP
1063 = setup_client_stack( init_sp, env,
1064 &info, &iifii.client_auxv,
1065 iicii.clstack_end, iifii.clstack_max_size,
1066 vex_archinfo );
1068 VG_(free)(env);
1070 VG_(debugLog)(2, "initimg",
1071 "Client info: "
1072 "initial_IP=%p initial_TOC=%p brk_base=%p\n",
1073 (void*)(iifii.initial_client_IP),
1074 (void*)(iifii.initial_client_TOC),
1075 (void*)VG_(brk_base) );
1076 VG_(debugLog)(2, "initimg",
1077 "Client info: "
1078 "initial_SP=%p max_stack_size=%lu\n",
1079 (void*)(iifii.initial_client_SP),
1080 iifii.clstack_max_size );
1083 //--------------------------------------------------------------
1084 // Setup client data (brk) segment. Initially a 1-page segment
1085 // which abuts a shrinkable reservation.
1086 // p: load_client() [for 'info' and hence VG_(brk_base)]
1087 //--------------------------------------------------------------
1089 SizeT m1 = 1024 * 1024;
1090 SizeT m8 = 8 * m1;
1091 SizeT dseg_max_size = (SizeT)VG_(client_rlimit_data).rlim_cur;
1092 VG_(debugLog)(1, "initimg", "Setup client data (brk) segment\n");
1093 if (dseg_max_size < m1) dseg_max_size = m1;
1094 if (dseg_max_size > m8) dseg_max_size = m8;
1095 dseg_max_size = VG_PGROUNDUP(dseg_max_size);
1097 setup_client_dataseg( dseg_max_size );
1100 VG_(free)(info.interp_name); info.interp_name = NULL;
1101 VG_(free)(info.interp_args); info.interp_args = NULL;
1102 return iifii;
1106 /*====================================================================*/
1107 /*=== TOP-LEVEL: VG_(finalise_thread1state) ===*/
1108 /*====================================================================*/
1110 /* Just before starting the client, we may need to make final
1111 adjustments to its initial image. Also we need to set up the VEX
1112 guest state for thread 1 (the root thread) and copy in essential
1113 starting values. This is handed the IIFinaliseImageInfo created by
1114 VG_(ii_create_image).
1116 void VG_(ii_finalise_image)( IIFinaliseImageInfo iifii )
1118 ThreadArchState* arch = &VG_(threads)[1].arch;
1120 /* On Linux we get client_{ip/sp/toc}, and start the client with
1121 all other registers zeroed. */
1123 # if defined(VGP_x86_linux)
1124 vg_assert(0 == sizeof(VexGuestX86State) % LibVEX_GUEST_STATE_ALIGN);
1126 /* Zero out the initial state, and set up the simulated FPU in a
1127 sane way. */
1128 LibVEX_GuestX86_initialise(&arch->vex);
1130 /* Zero out the shadow areas. */
1131 VG_(memset)(&arch->vex_shadow1, 0, sizeof(VexGuestX86State));
1132 VG_(memset)(&arch->vex_shadow2, 0, sizeof(VexGuestX86State));
1134 /* Put essential stuff into the new state. */
1135 arch->vex.guest_ESP = iifii.initial_client_SP;
1136 arch->vex.guest_EIP = iifii.initial_client_IP;
1138 /* initialise %cs, %ds and %ss to point at the operating systems
1139 default code, data and stack segments. Also %es (see #291253). */
1140 asm volatile("movw %%cs, %0" : : "m" (arch->vex.guest_CS));
1141 asm volatile("movw %%ds, %0" : : "m" (arch->vex.guest_DS));
1142 asm volatile("movw %%ss, %0" : : "m" (arch->vex.guest_SS));
1143 asm volatile("movw %%es, %0" : : "m" (arch->vex.guest_ES));
1145 # elif defined(VGP_amd64_linux)
1146 vg_assert(0 == sizeof(VexGuestAMD64State) % LibVEX_GUEST_STATE_ALIGN);
1148 /* Zero out the initial state, and set up the simulated FPU in a
1149 sane way. */
1150 LibVEX_GuestAMD64_initialise(&arch->vex);
1152 /* Zero out the shadow areas. */
1153 VG_(memset)(&arch->vex_shadow1, 0, sizeof(VexGuestAMD64State));
1154 VG_(memset)(&arch->vex_shadow2, 0, sizeof(VexGuestAMD64State));
1156 /* Put essential stuff into the new state. */
1157 arch->vex.guest_RSP = iifii.initial_client_SP;
1158 arch->vex.guest_RIP = iifii.initial_client_IP;
1160 # elif defined(VGP_ppc32_linux)
1161 vg_assert(0 == sizeof(VexGuestPPC32State) % LibVEX_GUEST_STATE_ALIGN);
1163 /* Zero out the initial state, and set up the simulated FPU in a
1164 sane way. */
1165 LibVEX_GuestPPC32_initialise(&arch->vex);
1167 /* Zero out the shadow areas. */
1168 VG_(memset)(&arch->vex_shadow1, 0, sizeof(VexGuestPPC32State));
1169 VG_(memset)(&arch->vex_shadow2, 0, sizeof(VexGuestPPC32State));
1171 /* Put essential stuff into the new state. */
1172 arch->vex.guest_GPR1 = iifii.initial_client_SP;
1173 arch->vex.guest_CIA = iifii.initial_client_IP;
1175 # elif defined(VGP_ppc64be_linux) || defined(VGP_ppc64le_linux)
1176 vg_assert(0 == sizeof(VexGuestPPC64State) % LibVEX_GUEST_STATE_ALIGN);
1178 /* Zero out the initial state, and set up the simulated FPU in a
1179 sane way. */
1180 LibVEX_GuestPPC64_initialise(&arch->vex);
1182 /* Zero out the shadow areas. */
1183 VG_(memset)(&arch->vex_shadow1, 0, sizeof(VexGuestPPC64State));
1184 VG_(memset)(&arch->vex_shadow2, 0, sizeof(VexGuestPPC64State));
1186 /* Put essential stuff into the new state. */
1187 arch->vex.guest_GPR1 = iifii.initial_client_SP;
1188 arch->vex.guest_GPR2 = iifii.initial_client_TOC;
1189 arch->vex.guest_CIA = iifii.initial_client_IP;
1190 #if defined(VGP_ppc64le_linux)
1191 arch->vex.guest_GPR12 = iifii.initial_client_IP;
1192 #endif
1194 # elif defined(VGP_arm_linux)
1195 /* Zero out the initial state, and set up the simulated FPU in a
1196 sane way. */
1197 LibVEX_GuestARM_initialise(&arch->vex);
1199 /* Zero out the shadow areas. */
1200 VG_(memset)(&arch->vex_shadow1, 0, sizeof(VexGuestARMState));
1201 VG_(memset)(&arch->vex_shadow2, 0, sizeof(VexGuestARMState));
1203 arch->vex.guest_R13 = iifii.initial_client_SP;
1204 arch->vex.guest_R15T = iifii.initial_client_IP;
1206 /* This is just EABI stuff. */
1207 // FIXME jrs: what's this for?
1208 arch->vex.guest_R1 = iifii.initial_client_SP;
1210 # elif defined(VGP_arm64_linux)
1211 /* Zero out the initial state. */
1212 LibVEX_GuestARM64_initialise(&arch->vex);
1214 /* Zero out the shadow areas. */
1215 VG_(memset)(&arch->vex_shadow1, 0, sizeof(VexGuestARM64State));
1216 VG_(memset)(&arch->vex_shadow2, 0, sizeof(VexGuestARM64State));
1218 arch->vex.guest_XSP = iifii.initial_client_SP;
1219 arch->vex.guest_PC = iifii.initial_client_IP;
1221 # elif defined(VGP_s390x_linux)
1222 vg_assert(0 == sizeof(VexGuestS390XState) % LibVEX_GUEST_STATE_ALIGN);
1224 /* Zero out the initial state. This also sets the guest_fpc to 0, which
1225 is also done by the kernel for the fpc during execve. */
1226 LibVEX_GuestS390X_initialise(&arch->vex);
1228 /* Mark all registers as undefined ... */
1229 VG_(memset)(&arch->vex_shadow1, 0xFF, sizeof(VexGuestS390XState));
1230 VG_(memset)(&arch->vex_shadow2, 0x00, sizeof(VexGuestS390XState));
1231 /* ... except SP, FPC, and IA */
1232 arch->vex_shadow1.guest_SP = 0;
1233 arch->vex_shadow1.guest_fpc = 0;
1234 arch->vex_shadow1.guest_IA = 0;
1236 /* Put essential stuff into the new state. */
1237 arch->vex.guest_SP = iifii.initial_client_SP;
1238 arch->vex.guest_IA = iifii.initial_client_IP;
1239 /* See sys_execve in <linux>/arch/s390/kernel/process.c */
1240 arch->vex.guest_fpc = 0;
1242 /* Tell the tool about the registers we just wrote */
1243 VG_TRACK(post_reg_write, Vg_CoreStartup, /*tid*/1, VG_O_STACK_PTR, 8);
1244 VG_TRACK(post_reg_write, Vg_CoreStartup, /*tid*/1, VG_O_FPC_REG, 4);
1245 VG_TRACK(post_reg_write, Vg_CoreStartup, /*tid*/1, VG_O_INSTR_PTR, 8);
1247 /* At the end of this function there is code to mark all guest state
1248 registers as defined. For s390 that would be wrong, because the ABI
1249 says that all registers except SP, IA, and FPC are undefined upon
1250 process startup. */
1251 #define PRECISE_GUEST_REG_DEFINEDNESS_AT_STARTUP 1
1253 # elif defined(VGP_mips32_linux) || defined(VGP_nanomips_linux)
1254 vg_assert(0 == sizeof(VexGuestMIPS32State) % LibVEX_GUEST_STATE_ALIGN);
1255 /* Zero out the initial state, and set up the simulated FPU in a
1256 sane way. */
1257 LibVEX_GuestMIPS32_initialise(&arch->vex);
1259 /* Zero out the shadow areas. */
1260 VG_(memset)(&arch->vex_shadow1, 0, sizeof(VexGuestMIPS32State));
1261 VG_(memset)(&arch->vex_shadow2, 0, sizeof(VexGuestMIPS32State));
1263 arch->vex.guest_r29 = iifii.initial_client_SP;
1264 arch->vex.guest_PC = iifii.initial_client_IP;
1265 arch->vex.guest_r31 = iifii.initial_client_SP;
1267 # if !defined(VGP_nanomips_linux)
1268 if (iifii.arch_elf_state.overall_fp_mode == VKI_FP_FR1) {
1269 arch->vex.guest_CP0_status |= MIPS_CP0_STATUS_FR;
1272 # endif
1273 # elif defined(VGP_mips64_linux)
1274 vg_assert(0 == sizeof(VexGuestMIPS64State) % LibVEX_GUEST_STATE_ALIGN);
1275 /* Zero out the initial state, and set up the simulated FPU in a
1276 sane way. */
1277 LibVEX_GuestMIPS64_initialise(&arch->vex);
1279 /* Zero out the shadow areas. */
1280 VG_(memset)(&arch->vex_shadow1, 0, sizeof(VexGuestMIPS64State));
1281 VG_(memset)(&arch->vex_shadow2, 0, sizeof(VexGuestMIPS64State));
1283 arch->vex.guest_r29 = iifii.initial_client_SP;
1284 arch->vex.guest_PC = iifii.initial_client_IP;
1285 arch->vex.guest_r31 = iifii.initial_client_SP;
1287 # else
1288 # error Unknown platform
1289 # endif
1291 # if !defined(PRECISE_GUEST_REG_DEFINEDNESS_AT_STARTUP)
1292 /* Tell the tool that we just wrote to the registers. */
1293 VG_TRACK( post_reg_write, Vg_CoreStartup, /*tid*/1, /*offset*/0,
1294 sizeof(VexGuestArchState));
1295 # endif
1297 /* Tell the tool about the client data segment and then kill it which will
1298 make it inaccessible/unaddressable. */
1299 const NSegment *seg = VG_(am_find_nsegment)(VG_(brk_base));
1300 vg_assert(seg);
1301 vg_assert(seg->kind == SkAnonC);
1302 VG_TRACK(new_mem_brk, VG_(brk_base), seg->end + 1 - VG_(brk_base),
1303 1/*tid*/);
1304 VG_TRACK(die_mem_brk, VG_(brk_base), seg->end + 1 - VG_(brk_base));
1307 #endif // defined(VGO_linux)
1309 /*--------------------------------------------------------------------*/
1310 /*--- ---*/
1311 /*--------------------------------------------------------------------*/