Bug 439685 compiler warning in callgrind/main.c
[valgrind.git] / coregrind / m_initimg / initimg-solaris.c
blob3e473c8803c081da648122b97d1f5673d86ece36
2 /*--------------------------------------------------------------------*/
3 /*--- Startup: create initial process image on Solaris ---*/
4 /*--- initimg-solaris.c ---*/
5 /*--------------------------------------------------------------------*/
7 /*
8 This file is part of Valgrind, a dynamic binary instrumentation
9 framework.
11 Copyright (C) 2011-2017 Petr Pavlu
12 setup@dagobah.cz
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 /* Copyright 2013-2017, Ivo Raisr <ivosh@ivosh.net>. */
32 #if defined(VGO_solaris)
34 /* Note: This file is based on initimg-linux.c. */
36 #include "pub_core_basics.h"
37 #include "pub_core_vki.h"
38 #include "pub_core_debuglog.h"
39 #include "pub_core_libcbase.h"
40 #include "pub_core_libcassert.h"
41 #include "pub_core_libcfile.h"
42 #include "pub_core_libcproc.h"
43 #include "pub_core_libcprint.h"
44 #include "pub_core_xarray.h"
45 #include "pub_core_clientstate.h"
46 #include "pub_core_aspacemgr.h"
47 #include "pub_core_mallocfree.h"
48 #include "pub_core_machine.h"
49 #include "pub_core_ume.h"
50 #include "pub_core_options.h"
51 #include "pub_core_syswrap.h"
52 #include "pub_core_tooliface.h" /* VG_TRACK */
53 #include "pub_core_threadstate.h" /* ThreadArchState */
54 #include "pub_core_pathscan.h" /* find_executable */
55 #include "pub_core_initimg.h" /* self */
58 /*====================================================================*/
59 /*=== Loading the client ===*/
60 /*====================================================================*/
62 /* Load the client whose name is VG_(argv_the_exename). */
63 static void load_client(/*OUT*/ExeInfo *info,
64 /*OUT*/HChar *out_exe_name, SizeT out_exe_name_size)
66 const HChar *exe_name;
67 Int ret;
68 SysRes res;
70 vg_assert(VG_(args_the_exename));
71 exe_name = VG_(find_executable)(VG_(args_the_exename));
73 if (!exe_name) {
74 VG_(printf)("valgrind: %s: command not found\n", VG_(args_the_exename));
75 /* Return POSIX's NOTFOUND. */
76 VG_(exit)(127);
77 /*NOTREACHED*/
80 VG_(memset)(info, 0, sizeof(*info));
81 ret = VG_(do_exec)(exe_name, info);
82 if (ret < 0) {
83 VG_(printf)("valgrind: could not execute '%s'\n", exe_name);
84 VG_(exit)(1);
85 /*NOTREACHED*/
88 /* The client was successfully loaded! Continue. */
90 /* Save resolved exename. */
91 if (VG_(strlen)(exe_name) + 1 > out_exe_name_size) {
92 /* This should not really happen. */
93 VG_(printf)("valgrind: execname %s is too long\n", exe_name);
94 VG_(exit)(1);
95 /*NOTREACHED*/
97 VG_(strcpy)(out_exe_name, exe_name);
99 /* Get hold of a file descriptor which refers to the client executable.
100 This is needed for attaching to GDB. */
101 res = VG_(open)(exe_name, VKI_O_RDONLY, VKI_S_IRUSR);
102 if (!sr_isError(res))
103 VG_(cl_exec_fd) = sr_Res(res);
105 /* Set initial brk values. */
106 if (info->ldsoexec) {
107 VG_(brk_base) = VG_(brk_limit) = -1;
108 } else {
109 VG_(brk_base) = VG_(brk_limit) = info->brkbase;
114 /*====================================================================*/
115 /*=== Setting up the client's environment ===*/
116 /*====================================================================*/
118 /* Prepare the client's environment. This is basically a copy of our
119 environment, except:
121 LD_PRELOAD=$VALGRIND_LIB/vgpreload_core-PLATFORM.so:
122 ($VALGRIND_LIB/vgpreload_TOOL-PLATFORM.so:)?
123 $LD_PRELOAD
125 If this is missing, then it is added.
127 Also, remove any binding for VALGRIND_LAUNCHER=. The client should not be
128 able to see this.
130 If this needs to handle any more variables it should be hacked into
131 something table driven. The copy is VG_(malloc)'d space.
133 static HChar **setup_client_env(HChar **origenv, const HChar *toolname)
135 const HChar *ld_preload = "LD_PRELOAD=";
136 SizeT ld_preload_len = VG_(strlen)(ld_preload);
137 Bool ld_preload_done = False;
138 SizeT vglib_len = VG_(strlen)(VG_(libdir));
140 HChar **cpp;
141 HChar **ret;
142 HChar *preload_tool_path;
143 SizeT envc, i;
145 /* Alloc space for the
146 <path>/vgpreload_core-<platform>.so and
147 <path>/vgpreload_<tool>-<platform>.so
148 paths. We might not need the space for the tool path, but it doesn't
149 hurt to over-allocate briefly. */
150 SizeT preload_core_path_size = vglib_len + sizeof("/vgpreload_core-") - 1
151 + sizeof(VG_PLATFORM) - 1
152 + sizeof(".so");
153 SizeT preload_tool_path_size = vglib_len + sizeof("/vgpreload_") - 1
154 + VG_(strlen)(toolname) + 1 /*-*/
155 + sizeof(VG_PLATFORM) - 1
156 + sizeof(".so");
157 SizeT preload_string_size = preload_core_path_size
158 + preload_tool_path_size;
159 HChar *preload_string = VG_(malloc)("initimg-solaris.sce.1",
160 preload_string_size);
162 /* Check that the parameters are sane. */
163 vg_assert(origenv);
164 vg_assert(toolname);
166 /* Determine if there's a vgpreload_<tool>-<platform>.so file, and setup
167 preload_string. */
168 preload_tool_path = VG_(malloc)("initimg-solaris.sce.2",
169 preload_tool_path_size);
170 VG_(sprintf)(preload_tool_path, "%s/vgpreload_%s-%s.so", VG_(libdir),
171 toolname, VG_PLATFORM);
172 if (!VG_(access)(preload_tool_path, True/*r*/, False/*w*/, False/*x*/)) {
173 /* The tool's .so exists, put it into LD_PRELOAD with the core's so. */
174 VG_(sprintf)(preload_string, "%s/vgpreload_core-%s.so:%s", VG_(libdir),
175 VG_PLATFORM, preload_tool_path);
177 else {
178 /* The tool's .so doesn't exist, put only the core's .so into
179 LD_PRELOAD. */
180 VG_(sprintf)(preload_string, "%s/vgpreload_core-%s.so", VG_(libdir),
181 VG_PLATFORM);
183 VG_(free)(preload_tool_path);
185 VG_(debugLog)(2, "initimg", "preload_string:\n");
186 VG_(debugLog)(2, "initimg", " \"%s\"\n", preload_string);
188 /* Count the original size of the env. */
189 envc = 0;
190 for (cpp = origenv; *cpp; cpp++)
191 envc++;
193 /* Allocate a new space, envc + 1 new entry + NULL. */
194 ret = VG_(malloc)("initimg-solaris.sce.3", sizeof(HChar*) * (envc + 1 + 1));
196 /* Copy it over. */
197 for (cpp = ret; *origenv; )
198 *cpp++ = *origenv++;
199 *cpp = NULL;
201 vg_assert(envc == cpp - ret);
203 /* Walk over the new environment, mashing as we go. */
204 for (cpp = ret; *cpp; cpp++) {
205 if (VG_(memcmp)(*cpp, ld_preload, ld_preload_len))
206 continue;
208 /* LD_PRELOAD entry found, smash it. */
209 SizeT size = VG_(strlen)(*cpp) + 1 /*:*/
210 + preload_string_size;
211 HChar *cp = VG_(malloc)("initimg-solaris.sce.4", size);
213 VG_(sprintf)(cp, "%s%s:%s", ld_preload, preload_string,
214 (*cpp) + ld_preload_len);
215 *cpp = cp;
217 ld_preload_done = True;
220 /* Add the missing bits. */
221 if (!ld_preload_done) {
222 SizeT size = ld_preload_len + preload_string_size;
223 HChar *cp = VG_(malloc)("initimg-solaris.sce.5", size);
225 VG_(sprintf)(cp, "%s%s", ld_preload, preload_string);
226 ret[envc++] = cp;
229 /* We've got ret[0 .. envc-1] live now. */
231 /* Find and remove a binding for VALGRIND_LAUNCHER. */
233 const HChar *v_launcher = VALGRIND_LAUNCHER "=";
234 SizeT v_launcher_len = VG_(strlen)(v_launcher);
236 for (i = 0; i < envc; i++)
237 if (!VG_(memcmp)(ret[i], v_launcher, v_launcher_len)) {
238 /* VALGRIND_LAUNCHER was found. */
239 break;
242 if (i < envc) {
243 /* VALGRIND_LAUNCHER was found, remove it. */
244 for (; i < envc - 1; i++)
245 ret[i] = ret[i + 1];
246 envc--;
250 VG_(free)(preload_string);
251 ret[envc] = NULL;
253 return ret;
257 /*====================================================================*/
258 /*=== Setting up the client's stack ===*/
259 /*====================================================================*/
261 /* Add a string onto the string table, and return its address. */
262 static HChar *copy_str(HChar **tab, const HChar *str)
264 HChar *cp = *tab;
265 HChar *orig = cp;
267 while (*str)
268 *cp++ = *str++;
269 *cp++ = '\0';
271 *tab = cp;
273 return orig;
276 #if defined(SOLARIS_RESERVE_SYSSTAT_ADDR) || \
277 defined(SOLARIS_RESERVE_SYSSTAT_ZONE_ADDR)
278 #define ORIG_AUXV_PRESENT 1
279 #endif
281 #if defined(ORIG_AUXV_PRESENT)
282 /* The auxiliary vector might not be present. So we cross-check pointers from
283 argv and envp pointing to the string table. */
284 static vki_auxv_t *find_original_auxv(Addr init_sp)
286 HChar **sp = (HChar **) init_sp;
287 HChar *lowest_str_ptr = (HChar *) UINTPTR_MAX; // lowest ptr to string table
289 sp++; // skip argc
291 while (*sp != NULL) { // skip argv
292 if (*sp < lowest_str_ptr)
293 lowest_str_ptr = *sp;
294 sp++;
296 sp++;
298 while (*sp != 0) { // skip env
299 if (*sp < lowest_str_ptr)
300 lowest_str_ptr = *sp;
301 sp++;
303 sp++;
305 if ((Addr) sp < (Addr) lowest_str_ptr) {
306 return (vki_auxv_t *) sp;
307 } else {
308 return NULL;
312 static void copy_auxv_entry(const vki_auxv_t *orig_auxv, Int type,
313 const HChar *type_name, vki_auxv_t *auxv)
315 vg_assert(auxv != NULL);
317 if (orig_auxv == NULL) {
318 VG_(printf)("valgrind: Cannot locate auxiliary vector.\n");
319 VG_(printf)("valgrind: Cannot continue. Sorry.\n\n");
320 VG_(exit)(1);
323 for ( ; orig_auxv->a_type != VKI_AT_NULL; orig_auxv++) {
324 if (orig_auxv->a_type == type) {
325 auxv->a_type = type;
326 auxv->a_un.a_val = orig_auxv->a_un.a_val;
327 return;
331 VG_(printf)("valgrind: Cannot locate %s in the aux\n", type_name);
332 VG_(printf)("valgrind: vector. Cannot continue. Sorry.\n\n");
333 VG_(exit)(1);
335 #endif /* ORIG_AUXV_PRESENT */
337 /* This sets up the client's initial stack, containing the args,
338 environment and aux vector.
340 The format of the stack is:
342 higher address +-----------------+ <- clstack_end
344 : string table :
346 +-----------------+
347 | AT_NULL |
349 | auxv |
350 +-----------------+
351 | NULL |
353 | envp |
354 +-----------------+
355 | NULL |
357 | argv |
358 +-----------------+
359 | argc |
360 lower address +-----------------+ <- sp
361 | undefined |
364 Allocate and create the initial client stack. It is allocated down from
365 clstack_end, which was previously determined by the address space manager.
366 The returned value is the SP value for the client.
368 Note that auxiliary vector is *not* created by kernel on illumos and
369 Solaris 11 if the program is statically linked (which is our case).
370 Although we now taught Solaris 11.4 to create the auxiliary vector, we still
371 have to build auxv from scratch, to make the code consistent. */
373 static Addr setup_client_stack(Addr init_sp,
374 HChar **orig_envp,
375 const ExeInfo *info,
376 Addr clstack_end,
377 SizeT clstack_max_size,
378 const HChar *resolved_exe_name)
380 SysRes res;
381 HChar **cpp;
382 HChar *strtab; /* string table */
383 HChar *stringbase;
384 Addr *ptr;
385 vki_auxv_t *auxv;
386 SizeT stringsize; /* total size of strings in bytes */
387 SizeT auxsize; /* total size of auxv in bytes */
388 Int argc; /* total argc */
389 Int envc; /* total number of env vars */
390 SizeT stacksize; /* total client stack size */
391 Addr client_SP; /* client stack base (initial SP) */
392 Addr clstack_start;
393 Int i;
395 vg_assert(VG_IS_PAGE_ALIGNED(clstack_end + 1));
396 vg_assert(VG_(args_the_exename));
397 vg_assert(VG_(args_for_client));
399 # if defined(ORIG_AUXV_PRESENT)
400 /* Get the original auxv (if any). */
401 vki_auxv_t *orig_auxv = find_original_auxv(init_sp);
402 # endif /* ORIG_AUXV_PRESENT */
404 /* ==================== compute sizes ==================== */
406 /* First of all, work out how big the client stack will be. */
407 stringsize = 0;
409 /* Paste on the extra args if the loader needs them (i.e. the #!
410 interpreter and its argument). */
411 argc = 0;
412 if (info->interp_name) {
413 argc++;
414 stringsize += VG_(strlen)(info->interp_name) + 1;
416 if (info->interp_args) {
417 argc++;
418 stringsize += VG_(strlen)(info->interp_args) + 1;
421 /* Now scan the args we're given... */
422 argc++;
423 stringsize += VG_(strlen)(VG_(args_the_exename)) + 1;
424 for (i = 0; i < VG_(sizeXA)(VG_(args_for_client)); i++) {
425 argc++;
426 stringsize += VG_(strlen)(*(HChar**)
427 VG_(indexXA)(VG_(args_for_client), i)) + 1;
430 /* ...and the environment. */
431 envc = 0;
432 for (cpp = orig_envp; *cpp; cpp++) {
433 envc++;
434 stringsize += VG_(strlen)(*cpp) + 1;
437 /* Now, how big is the auxv?
439 AT_SUN_PLATFORM
440 AT_SUN_EXECNAME
441 AT_PHDR (not for elfs with no PT_PHDR, such as ld.so.1)
442 AT_BASE
443 AT_ENTRY
444 AT_FLAGS
445 AT_PAGESZ
446 AT_SUN_AUXFLAFGS
447 AT_SUN_HWCAP
448 AT_SUN_SYSSTAT_ADDR (if supported)
449 AT_SUN_SYSSTAT_ZONE_ADDR (if supported)
450 AT_NULL
452 It would be possible to also add AT_PHENT, AT_PHNUM, AT_SUN_LDDATA,
453 but they don't seem to be so important. */
454 auxsize = 10 * sizeof(*auxv);
455 # if defined(SOLARIS_RESERVE_SYSSTAT_ADDR)
456 auxsize += sizeof(*auxv);
457 # endif
458 # if defined(SOLARIS_RESERVE_SYSSTAT_ZONE_ADDR)
459 auxsize += sizeof(*auxv);
460 # endif
462 # if defined(VGA_x86) || defined(VGA_amd64)
463 /* AT_SUN_PLATFORM string. */
464 stringsize += VG_(strlen)("i86pc") + 1;
465 # else
466 # error "Unknown architecture"
467 # endif
468 /* AT_SUN_EXECNAME string. */
469 stringsize += VG_(strlen)(resolved_exe_name) + 1;
471 /* Calculate how big the client stack is. */
472 stacksize =
473 sizeof(Word) + /* argc */
474 sizeof(HChar**) + /* argc[0] == exename */
475 sizeof(HChar**) * argc + /* argv */
476 sizeof(HChar**) + /* terminal NULL */
477 sizeof(HChar**) * envc + /* envp */
478 sizeof(HChar**) + /* terminal NULL */
479 auxsize + /* auxv */
480 VG_ROUNDUP(stringsize, sizeof(Word)); /* strings (aligned) */
482 /* The variable client_SP is the client's stack pointer. */
483 client_SP = clstack_end - stacksize;
484 client_SP = VG_ROUNDDN(client_SP, 16); /* Make stack 16 byte aligned. */
486 /* Calculate base of the string table (aligned). */
487 stringbase = (HChar*)clstack_end - VG_ROUNDUP(stringsize, sizeof(Int));
488 strtab = stringbase;
490 clstack_start = VG_PGROUNDDN(client_SP);
492 /* Calculate the max stack size. */
493 clstack_max_size = VG_PGROUNDUP(clstack_max_size);
495 /* Record stack extent -- needed for stack-change code. */
496 VG_(clstk_start_base) = clstack_start;
497 VG_(clstk_end) = clstack_end;
498 VG_(clstk_max_size) = clstack_max_size;
500 if (0)
501 VG_(printf)("stringsize=%lu, auxsize=%lu, stacksize=%lu, maxsize=%#lx\n"
502 "clstack_start %#lx\n"
503 "clstack_end %#lx\n",
504 stringsize, auxsize, stacksize, clstack_max_size,
505 clstack_start, clstack_end);
507 /* ==================== allocate space ==================== */
510 SizeT anon_size = clstack_end - clstack_start + 1;
511 SizeT resvn_size = clstack_max_size - anon_size;
512 Addr anon_start = clstack_start;
513 Addr resvn_start = anon_start - resvn_size;
514 SizeT inner_HACK = 0;
515 Bool ok;
517 /* So far we've only accounted for space requirements down to the stack
518 pointer. If this target's ABI requires a redzone below the stack
519 pointer, we need to allocate an extra page, to handle the worst case
520 in which the stack pointer is almost at the bottom of a page, and so
521 there is insufficient room left over to put the redzone in. In this
522 case the simple thing to do is allocate an extra page, by shrinking
523 the reservation by one page and growing the anonymous area by a
524 corresponding page. */
525 vg_assert(VG_STACK_REDZONE_SZB >= 0);
526 vg_assert(VG_STACK_REDZONE_SZB < VKI_PAGE_SIZE);
527 if (VG_STACK_REDZONE_SZB > 0) {
528 vg_assert(resvn_size > VKI_PAGE_SIZE);
529 resvn_size -= VKI_PAGE_SIZE;
530 anon_start -= VKI_PAGE_SIZE;
531 anon_size += VKI_PAGE_SIZE;
534 vg_assert(VG_IS_PAGE_ALIGNED(anon_size));
535 vg_assert(VG_IS_PAGE_ALIGNED(resvn_size));
536 vg_assert(VG_IS_PAGE_ALIGNED(anon_start));
537 vg_assert(VG_IS_PAGE_ALIGNED(resvn_start));
538 vg_assert(resvn_start == clstack_end + 1 - clstack_max_size);
540 # ifdef ENABLE_INNER
541 /* Create 1M non-fault-extending stack. */
542 inner_HACK = 1024 * 1024;
543 # endif
545 if (0)
546 VG_(printf)("resvn_start=%#lx, resvn_size=%#lx\n"
547 "anon_start=%#lx, anon_size=%#lx\n",
548 resvn_start, resvn_size, anon_start, anon_size);
550 /* Create a shrinkable reservation followed by an anonymous segment.
551 Together these constitute a growdown stack. */
552 ok = VG_(am_create_reservation)(resvn_start,
553 resvn_size - inner_HACK,
554 SmUpper,
555 anon_size + inner_HACK);
556 if (ok) {
557 /* Allocate a stack - mmap enough space for the stack. */
558 res = VG_(am_mmap_anon_fixed_client)(anon_start - inner_HACK,
559 anon_size + inner_HACK,
560 info->stack_prot);
562 if (!ok || sr_isError(res)) {
563 /* Allocation of the stack failed. We have to stop. */
564 VG_(printf)("valgrind: "
565 "I failed to allocate space for the application's stack.\n");
566 VG_(printf)("valgrind: "
567 "This may be the result of a very large --main-stacksize=\n");
568 VG_(printf)("valgrind: setting. Cannot continue. Sorry.\n\n");
569 VG_(exit)(1);
570 /*NOTREACHED*/
574 /* ==================== create client stack ==================== */
576 ptr = (Addr*)client_SP;
578 /* Copy-out client argc. */
579 *ptr++ = argc;
581 /* Copy-out client argv. */
582 if (info->interp_name)
583 *ptr++ = (Addr)copy_str(&strtab, info->interp_name);
584 if (info->interp_args)
585 *ptr++ = (Addr)copy_str(&strtab, info->interp_args);
587 *ptr++ = (Addr)copy_str(&strtab, VG_(args_the_exename));
588 for (i = 0; i < VG_(sizeXA)(VG_(args_for_client)); i++)
589 *ptr++ = (Addr)copy_str(
590 &strtab, *(HChar**) VG_(indexXA)(VG_(args_for_client), i));
591 *ptr++ = 0;
593 /* Copy-out envp. */
594 VG_(client_envp) = (HChar**)ptr;
595 for (cpp = orig_envp; *cpp; ptr++, cpp++)
596 *ptr = (Addr)copy_str(&strtab, *cpp);
597 *ptr++ = 0;
599 /* Create aux vector. */
600 auxv = (auxv_t*)ptr;
601 VG_(client_auxv) = (UWord*)ptr;
603 /* AT_SUN_PLATFORM */
604 auxv->a_type = VKI_AT_SUN_PLATFORM;
605 # if defined(VGA_x86) || defined(VGA_amd64)
606 auxv->a_un.a_ptr = copy_str(&strtab, "i86pc");
607 # else
608 # error "Unknown architecture"
609 # endif
610 auxv++;
612 /* AT_SUN_EXECNAME */
613 auxv->a_type = VKI_AT_SUN_EXECNAME;
614 auxv->a_un.a_ptr = copy_str(&strtab, resolved_exe_name);
615 auxv++;
617 /* AT_PHDR */
618 if ((info->real_phdr_present) && (info->phdr != 0)) {
619 auxv->a_type = VKI_AT_PHDR;
620 auxv->a_un.a_val = info->phdr;
621 auxv++;
624 /* AT_BASE */
625 auxv->a_type = VKI_AT_BASE;
626 auxv->a_un.a_val = info->interp_offset;
627 auxv++;
629 /* AT_ENTRY */
630 auxv->a_type = VKI_AT_ENTRY;
631 auxv->a_un.a_val = info->entry;
632 auxv++;
634 /* AT_FLAGS */
635 auxv->a_type = VKI_AT_FLAGS;
636 # if defined(VGA_x86) || defined(VGA_amd64)
637 auxv->a_un.a_val = 0; /* 0 on i86pc */
638 # else
639 # error "Unknown architecture"
640 # endif
641 auxv++;
643 /* AT_PAGESZ */
644 auxv->a_type = VKI_AT_PAGESZ;
645 auxv->a_un.a_val = VKI_PAGE_SIZE;
646 auxv++;
648 /* AT_SUN_AUXFLAFGS */
649 auxv->a_type = VKI_AT_SUN_AUXFLAGS;
650 /* XXX Handle AF_SUN_SETUGID? */
651 auxv->a_un.a_val = VKI_AF_SUN_HWCAPVERIFY;
652 auxv++;
654 /* AT_SUN_HWCAP */
656 VexArch vex_arch;
657 VexArchInfo vex_archinfo;
658 UInt hwcaps;
660 VG_(machine_get_VexArchInfo)(&vex_arch, &vex_archinfo);
662 # if defined(VGA_x86)
663 vg_assert(vex_arch == VexArchX86);
665 /* Set default hwcaps. */
666 hwcaps =
667 VKI_AV_386_FPU /* x87-style floating point */
668 | VKI_AV_386_TSC /* rdtsc insn */
669 | VKI_AV_386_CX8 /* cmpxchg8b insn */
670 | VKI_AV_386_SEP /* sysenter and sysexit */
671 | VKI_AV_386_AMD_SYSC /* AMD's syscall and sysret */
672 | VKI_AV_386_CMOV /* conditional move insns */
673 | VKI_AV_386_MMX /* MMX insn */
674 | VKI_AV_386_AHF; /* lahf/sahf insns */
676 /* Handle additional hwcaps. */
677 if (vex_archinfo.hwcaps & VEX_HWCAPS_X86_SSE1)
678 hwcaps |=
679 VKI_AV_386_FXSR /* fxsave and fxrstor */
680 | VKI_AV_386_SSE; /* SSE insns and regs */
681 if (vex_archinfo.hwcaps & VEX_HWCAPS_X86_SSE2) {
682 vg_assert(vex_archinfo.hwcaps & VEX_HWCAPS_X86_SSE1);
683 hwcaps |=
684 VKI_AV_386_SSE2; /* SSE2 insns and regs */
686 if (vex_archinfo.hwcaps & VEX_HWCAPS_X86_SSE3) {
687 vg_assert(vex_archinfo.hwcaps & VEX_HWCAPS_X86_SSE2);
688 hwcaps |=
689 VKI_AV_386_SSE3 /* SSE3 insns and regs */
690 | VKI_AV_386_SSSE3; /* Intel SSSE3 insns */
692 if (vex_archinfo.hwcaps & VEX_HWCAPS_X86_LZCNT)
693 hwcaps |=
694 VKI_AV_386_AMD_LZCNT; /* AMD's LZCNT insn */
696 /* No support for:
697 AV_386_AMD_MMX AMD's MMX insns
698 AV_386_AMD_3DNow AMD's 3Dnow! insns
699 AV_386_AMD_3DNowx AMD's 3Dnow! extended insns
700 AV_386_CX16 cmpxchg16b insn
701 AV_386_TSCP rdtscp instruction
702 AV_386_AMD_SSE4A AMD's SSE4A insns
703 AV_386_POPCNT POPCNT insn
704 AV_386_SSE4_1 Intel SSE4.1 insns
705 AV_386_SSE4_2 Intel SSE4.2 insns
706 AV_386_MOVBE Intel MOVBE insns
707 AV_386_AES Intel AES insns
708 AV_386_PCLMULQDQ Intel PCLMULQDQ insn
709 AV_386_XSAVE Intel XSAVE/XRSTOR insns
710 AV_386_AVX Intel AVX insns
711 illumos only:
712 AV_386_VMX Intel VMX support
713 AV_386_AMD_SVM AMD SVM support
714 solaris only:
715 AV_386_AMD_XOP AMD XOP insns
716 AV_386_AMD_FMA4 AMD FMA4 insns */
718 # elif defined(VGA_amd64)
719 vg_assert(vex_arch == VexArchAMD64);
721 /* Set default hwcaps. */
722 hwcaps =
723 VKI_AV_386_FPU /* x87-style floating point */
724 | VKI_AV_386_TSC /* rdtsc insn */
725 | VKI_AV_386_CX8 /* cmpxchg8b insn */
726 | VKI_AV_386_AMD_SYSC /* AMD's syscall and sysret */
727 | VKI_AV_386_CMOV /* conditional move insns */
728 | VKI_AV_386_MMX /* MMX insn */
729 | VKI_AV_386_AHF /* lahf/sahf insns */
730 | VKI_AV_386_FXSR /* fxsave and fxrstor */
731 | VKI_AV_386_SSE /* SSE insns and regs */
732 | VKI_AV_386_SSE2; /* SSE2 insns and regs */
734 /* Handle additional hwcaps. */
735 if (vex_archinfo.hwcaps & VEX_HWCAPS_AMD64_SSE3)
736 hwcaps |=
737 VKI_AV_386_SSE3 /* SSE3 insns and regs */
738 | VKI_AV_386_SSSE3; /* Intel SSSE3 insns */
739 if (vex_archinfo.hwcaps & VEX_HWCAPS_AMD64_CX16)
740 hwcaps |=
741 VKI_AV_386_CX16; /* cmpxchg16b insn */
742 if (vex_archinfo.hwcaps & VEX_HWCAPS_AMD64_LZCNT)
743 hwcaps |=
744 VKI_AV_386_AMD_LZCNT; /* AMD's LZCNT insn */
745 if (vex_archinfo.hwcaps & VEX_HWCAPS_AMD64_RDTSCP)
746 hwcaps |=
747 VKI_AV_386_TSCP; /* rdtscp instruction */
748 if ((vex_archinfo.hwcaps & VEX_HWCAPS_AMD64_SSE3) &&
749 (vex_archinfo.hwcaps & VEX_HWCAPS_AMD64_CX16)) {
750 /* The CPUID simulation provided by VEX claims to have POPCNT, AES
751 and SSE4 (SSE4.1/SSE4.2) in the SSE3+CX16 configuration. */
752 hwcaps |=
753 VKI_AV_386_POPCNT /* POPCNT insn */
754 | VKI_AV_386_AES /* Intel AES insns */
755 | VKI_AV_386_SSE4_1 /* Intel SSE4.1 insns */
756 | VKI_AV_386_SSE4_2; /* Intel SSE4.2 insns */
758 if ((vex_archinfo.hwcaps & VEX_HWCAPS_AMD64_SSE3) &&
759 (vex_archinfo.hwcaps & VEX_HWCAPS_AMD64_CX16) &&
760 (vex_archinfo.hwcaps & VEX_HWCAPS_AMD64_AVX)) {
761 /* The CPUID simulation provided by VEX claims to have PCLMULQDQ and
762 XSAVE in the SSE3+CX16+AVX configuration. */
763 hwcaps |=
764 VKI_AV_386_PCLMULQDQ /* Intel PCLMULQDQ insn */
765 | VKI_AV_386_XSAVE; /* Intel XSAVE/XRSTOR insns */
767 /* No support for:
768 AV_386_SEP sysenter and sysexit
769 AV_386_AMD_MMX AMD's MMX insns
770 AV_386_AMD_3DNow AMD's 3Dnow! insns
771 AV_386_AMD_3DNowx AMD's 3Dnow! extended insns
772 AV_386_AMD_SSE4A AMD's SSE4A insns
773 AV_386_MOVBE Intel MOVBE insns
774 AV_386_AVX Intel AVX insns
775 illumos only:
776 AV_386_VMX Intel VMX support
777 AV_386_AMD_SVM AMD SVM support
778 solaris only:
779 AV_386_AMD_XOP AMD XOP insns
780 AV_386_AMD_FMA4 AMD FMA4 insns
782 TODO VEX supports AVX, BMI and AVX2. Investigate if they can be
783 enabled on Solaris/illumos.
786 # else
787 # error "Unknown architecture"
788 # endif
790 auxv->a_type = VKI_AT_SUN_HWCAP;
791 auxv->a_un.a_val = hwcaps;
792 auxv++;
795 /* AT_SUN_HWCAP2 */
797 /* No support for:
798 illumos only:
799 AV_386_2_F16C F16C half percision extensions
800 AV_386_2_RDRAND RDRAND insn
801 solaris only:
802 AV2_386_RDRAND Intel RDRAND insns
803 AV2_386_FMA Intel FMA insn
804 AV2_386_F16C IEEE half precn(float) insn
805 AV2_386_AMD_TBM AMD TBM insn
806 AV2_386_BMI1 Intel BMI1 insn
807 AV2_386_FSGSBASE Intel RD/WR FS/GSBASE insn
808 AV2_386_AVX2 Intel AVX2 insns
809 AV2_386_BMI2 Intel BMI2 insns
810 AV2_386_HLE Intel HLE insns
811 AV2_386_RTM Intel RTM insns
812 AV2_386_EFS Intel Enhanced Fast String
813 AV2_386_RDSEED Intel RDSEED insn
814 AV2_386_ADX Intel ADX insns
815 AV2_386_PRFCHW Intel PREFETCHW hint
819 # if defined(SOLARIS_RESERVE_SYSSTAT_ADDR)
820 /* AT_SUN_SYSSTAT_ADDR */
821 copy_auxv_entry(orig_auxv, VKI_AT_SUN_SYSSTAT_ADDR,
822 "AT_SUN_SYSSTAT_ADDR", auxv);
823 VG_(change_mapping_ownership)(auxv->a_un.a_val, True);
824 auxv++;
825 # endif
827 # if defined(SOLARIS_RESERVE_SYSSTAT_ZONE_ADDR)
828 /* AT_SUN_SYSSTAT_ZONE_ADDR */
829 copy_auxv_entry(orig_auxv, VKI_AT_SUN_SYSSTAT_ZONE_ADDR,
830 "AT_SUN_SYSSTAT_ZONE_ADDR", auxv);
831 VG_(change_mapping_ownership)(auxv->a_un.a_val, True);
832 auxv++;
833 # endif
835 /* AT_NULL */
836 auxv->a_type = VKI_AT_NULL;
837 auxv->a_un.a_val = 0;
839 vg_assert(strtab - stringbase == stringsize);
841 /* The variable client_SP is now pointing at client's argc/argv. */
843 if (0)
844 VG_(printf)("startup SP = %#lx\n", client_SP);
845 return client_SP;
848 /*====================================================================*/
849 /*=== TOP-LEVEL: VG_(setup_client_initial_image) ===*/
850 /*====================================================================*/
852 /* Create the client's initial memory image. */
853 IIFinaliseImageInfo VG_(ii_create_image)(IICreateImageInfo iicii,
854 const VexArchInfo *vex_archinfo)
856 ExeInfo info;
857 HChar **env = NULL;
858 HChar resolved_exe_name[VKI_PATH_MAX];
860 IIFinaliseImageInfo iifii;
861 VG_(memset)(&iifii, 0, sizeof(iifii));
863 //--------------------------------------------------------------
864 // Load client executable, finding in $PATH if necessary
865 // p: early_process_cmd_line_options() [for 'exec', 'need_help']
866 // p: layout_remaining_space [so there's space]
867 //--------------------------------------------------------------
868 VG_(debugLog)(1, "initimg", "Loading client\n");
870 if (!VG_(args_the_exename)) {
871 VG_(err_missing_prog)();
872 /*NOTREACHED*/
875 load_client(&info, resolved_exe_name, sizeof(resolved_exe_name));
876 iifii.initial_client_IP = info.init_ip;
877 /* Note: TOC isn't available on Solaris. */
878 iifii.initial_client_TOC = info.init_toc;
879 iifii.initial_client_TP = info.init_thrptr;
880 /* Note that iifii.client_auxv is never set on Solaris, because it isn't
881 necessary to have this value in VG_(ii_finalise_image). */
883 //--------------------------------------------------------------
884 // Set up client's environment
885 // p: set-libdir [for VG_(libdir)]
886 // p: early_process_cmd_line_options() [for toolname]
887 //--------------------------------------------------------------
888 VG_(debugLog)(1, "initimg", "Setup client env\n");
889 env = setup_client_env(iicii.envp, iicii.toolname);
891 //--------------------------------------------------------------
892 // Setup client stack and EIP
893 // p: load_client() [for 'info']
894 // p: fix_environment() [for 'env']
895 //--------------------------------------------------------------
897 /* When allocating space for the client stack, take notice of the
898 --main-stacksize value. This makes it possible to run programs with
899 very large (primary) stack requirements simply by specifying
900 --main-stacksize. */
901 /* Logic is as follows:
902 - By default, use the client's current stack rlimit.
903 - If that exceeds 16M, clamp to 16M.
904 - If a larger --main-stacksize value is specified, use that instead.
905 - In all situations, the minimum allowed stack size is 1M.
907 Addr init_sp = (Addr) (iicii.argv - 1);
908 SizeT m1 = 1024 * 1024;
909 SizeT m16 = 16 * m1;
910 SizeT szB = (SizeT)VG_(client_rlimit_stack).rlim_cur;
911 if (szB < m1)
912 szB = m1;
913 if (szB > m16)
914 szB = m16;
916 if (VG_(clo_main_stacksize) > 0)
917 szB = VG_(clo_main_stacksize);
918 if (szB < m1)
919 szB = m1;
921 szB = VG_PGROUNDUP(szB);
922 VG_(debugLog)(1, "initimg",
923 "Setup client stack: size will be %ld\n", szB);
925 iifii.clstack_max_size = szB;
926 iifii.initial_client_SP = setup_client_stack(init_sp, env, &info,
927 iicii.clstack_end,
928 iifii.clstack_max_size,
929 resolved_exe_name);
930 VG_(free)(env);
932 VG_(debugLog)(2, "initimg", "Client info: "
933 "initial_IP=%#lx, initial_TOC=%#lx, brk_base=%#lx\n",
934 iifii.initial_client_IP, iifii.initial_client_TOC,
935 VG_(brk_base));
936 VG_(debugLog)(2, "initimg", "Client info: "
937 "initial_SP=%#lx, max_stack_size=%lu\n",
938 iifii.initial_client_SP,
939 iifii.clstack_max_size);
942 if (info.ldsoexec) {
943 /* We are executing the runtime linker itself.
944 Initial data (brk) segment is setup on demand, after the target dynamic
945 executable has been loaded or when a first brk() syscall is made.
946 It cannot be established now because it would conflict with a temporary
947 stack which ld.so.1 (when executed directly) uses for loading the
948 target dynamic executable. See PRE(sys_brk) in syswrap-solaris.c. */
949 } else {
950 if (!VG_(setup_client_dataseg)()) {
951 VG_(printf)("valgrind: cannot initialize data segment (brk).\n");
952 VG_(exit)(1);
956 VG_(free)(info.interp_name);
957 VG_(free)(info.interp_args);
958 return iifii;
962 /*====================================================================*/
963 /*=== TOP-LEVEL: VG_(finalise_image) ===*/
964 /*====================================================================*/
966 /* Just before starting the client, we may need to make final adjustments to
967 its initial image. Also we need to set up the VEX guest state for thread 1
968 (the root thread) and copy in essential starting values. This is handed
969 the IIFinaliseImageInfo created by VG_(ii_create_image).
971 void VG_(ii_finalise_image)(IIFinaliseImageInfo iifii)
973 ThreadArchState *arch = &VG_(threads)[1].arch;
975 # if defined(VGA_x86)
976 vg_assert(0 == sizeof(VexGuestX86State) % LibVEX_GUEST_STATE_ALIGN);
978 /* Zero out the initial state, and set up the simulated FPU in a sane
979 way. */
980 LibVEX_GuestX86_initialise(&arch->vex);
982 /* Zero out the shadow areas. */
983 VG_(memset)(&arch->vex_shadow1, 0, sizeof(VexGuestX86State));
984 VG_(memset)(&arch->vex_shadow2, 0, sizeof(VexGuestX86State));
986 /* Put essential stuff into the new state. */
987 arch->vex.guest_ESP = iifii.initial_client_SP;
988 arch->vex.guest_EIP = iifii.initial_client_IP;
989 LibVEX_GuestX86_put_eflags(VKI_PSL_USER, &arch->vex);
991 /* Set %cs, %ds, %ss and %es to default values. */
992 __asm__ __volatile__ ("movw %%cs, %[cs]" : [cs] "=m" (arch->vex.guest_CS));
993 __asm__ __volatile__ ("movw %%ds, %[ds]" : [ds] "=m" (arch->vex.guest_DS));
994 __asm__ __volatile__ ("movw %%ss, %[ss]" : [ss] "=m" (arch->vex.guest_SS));
995 __asm__ __volatile__ ("movw %%es, %[es]" : [es] "=m" (arch->vex.guest_ES));
998 /* Initial thread pointer value will be saved in GDT when the thread is
999 started in the syswrap module and a thread's GDT is allocated. */
1000 ThreadOSstate *os = &VG_(threads)[1].os_state;
1001 os->thrptr = iifii.initial_client_TP;
1004 # elif defined(VGA_amd64)
1005 vg_assert(0 == sizeof(VexGuestAMD64State) % LibVEX_GUEST_STATE_ALIGN);
1007 /* Zero out the initial state, and set up the simulated FPU in a sane
1008 way. */
1009 LibVEX_GuestAMD64_initialise(&arch->vex);
1011 /* Zero out the shadow areas. */
1012 VG_(memset)(&arch->vex_shadow1, 0, sizeof(VexGuestAMD64State));
1013 VG_(memset)(&arch->vex_shadow2, 0, sizeof(VexGuestAMD64State));
1015 /* Put essential stuff into the new state. */
1016 arch->vex.guest_RSP = iifii.initial_client_SP;
1017 arch->vex.guest_RIP = iifii.initial_client_IP;
1018 arch->vex.guest_FS_CONST = iifii.initial_client_TP;
1019 LibVEX_GuestAMD64_put_rflags(VKI_PSL_USER, &arch->vex);
1021 # else
1022 # error "Unknown platform"
1023 # endif
1025 /* Tell the tool that we just wrote to the registers. */
1026 VG_TRACK(post_reg_write, Vg_CoreStartup, 1/*tid*/, 0/*offset*/,
1027 sizeof(VexGuestArchState));
1029 if (VG_(brk_base) != -1 ) {
1030 /* Make inaccessible/unaddressable the end of the client data segment.
1031 See PRE(sys_brk) in syswrap-solaris.c for details. */
1032 VG_(track_client_dataseg)(1 /* tid */);
1036 #endif // defined(VGO_solaris)
1038 /*--------------------------------------------------------------------*/
1039 /*--- ---*/
1040 /*--------------------------------------------------------------------*/