tests/vg_regtest: Always evaluate prerequisite expressions with sh
[valgrind.git] / coregrind / m_initimg / initimg-solaris.c
blob4db11a87fa225e73caedd05f985a07163ee06df0
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-2014 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, write to the Free Software
26 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 02111-1307, USA.
29 The GNU General Public License is contained in the file COPYING.
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_tooliface.h" /* VG_TRACK */
52 #include "pub_core_threadstate.h" /* ThreadArchState */
53 #include "priv_initimg_pathscan.h"
54 #include "pub_core_initimg.h" /* self */
57 /*====================================================================*/
58 /*=== Loading the client ===*/
59 /*====================================================================*/
61 /* Load the client whose name is VG_(argv_the_exename). */
62 static void load_client(/*OUT*/ExeInfo *info,
63 /*OUT*/HChar *out_exe_name, SizeT out_exe_name_size)
65 const HChar *exe_name;
66 Int ret;
67 SysRes res;
69 vg_assert(VG_(args_the_exename));
70 exe_name = ML_(find_executable)(VG_(args_the_exename));
72 if (!exe_name) {
73 VG_(printf)("valgrind: %s: command not found\n", VG_(args_the_exename));
74 /* Return POSIX's NOTFOUND. */
75 VG_(exit)(127);
76 /*NOTREACHED*/
79 VG_(memset)(info, 0, sizeof(*info));
80 ret = VG_(do_exec)(exe_name, info);
81 if (ret < 0) {
82 VG_(printf)("valgrind: could not execute '%s'\n", exe_name);
83 VG_(exit)(1);
84 /*NOTREACHED*/
87 /* The client was successfully loaded! Continue. */
89 /* Save resolved exename. */
90 if (VG_(strlen)(exe_name) + 1 > out_exe_name_size) {
91 /* This should not really happen. */
92 VG_(printf)("valgrind: execname %s is too long\n", exe_name);
93 VG_(exit)(1);
94 /*NOTREACHED*/
96 VG_(strcpy)(out_exe_name, exe_name);
98 /* Get hold of a file descriptor which refers to the client executable.
99 This is needed for attaching to GDB. */
100 res = VG_(open)(exe_name, VKI_O_RDONLY, VKI_S_IRUSR);
101 if (!sr_isError(res))
102 VG_(cl_exec_fd) = sr_Res(res);
104 /* Set initial brk values. */
105 VG_(brk_base) = VG_(brk_limit) = info->brkbase;
109 /*====================================================================*/
110 /*=== Setting up the client's environment ===*/
111 /*====================================================================*/
113 /* Prepare the client's environment. This is basically a copy of our
114 environment, except:
116 LD_PRELOAD=$VALGRIND_LIB/vgpreload_core-PLATFORM.so:
117 ($VALGRIND_LIB/vgpreload_TOOL-PLATFORM.so:)?
118 $LD_PRELOAD
120 If this is missing, then it is added.
122 Also, remove any binding for VALGRIND_LAUNCHER=. The client should not be
123 able to see this.
125 If this needs to handle any more variables it should be hacked into
126 something table driven. The copy is VG_(malloc)'d space.
128 static HChar **setup_client_env(HChar **origenv, const HChar *toolname)
130 const HChar *ld_preload = "LD_PRELOAD=";
131 SizeT ld_preload_len = VG_(strlen)(ld_preload);
132 Bool ld_preload_done = False;
133 SizeT vglib_len = VG_(strlen)(VG_(libdir));
135 HChar **cpp;
136 HChar **ret;
137 HChar *preload_tool_path;
138 SizeT envc, i;
140 /* Alloc space for the
141 <path>/vgpreload_core-<platform>.so and
142 <path>/vgpreload_<tool>-<platform>.so
143 paths. We might not need the space for the tool path, but it doesn't
144 hurt to over-allocate briefly. */
145 SizeT preload_core_path_size = vglib_len + sizeof("/vgpreload_core-") - 1
146 + sizeof(VG_PLATFORM) - 1
147 + sizeof(".so");
148 SizeT preload_tool_path_size = vglib_len + sizeof("/vgpreload_") - 1
149 + VG_(strlen)(toolname) + 1 /*-*/
150 + sizeof(VG_PLATFORM) - 1
151 + sizeof(".so");
152 SizeT preload_string_size = preload_core_path_size
153 + preload_tool_path_size;
154 HChar *preload_string = VG_(malloc)("initimg-solaris.sce.1",
155 preload_string_size);
157 /* Check that the parameters are sane. */
158 vg_assert(origenv);
159 vg_assert(toolname);
161 /* Determine if there's a vgpreload_<tool>-<platform>.so file, and setup
162 preload_string. */
163 preload_tool_path = VG_(malloc)("initimg-solaris.sce.2",
164 preload_tool_path_size);
165 VG_(sprintf)(preload_tool_path, "%s/vgpreload_%s-%s.so", VG_(libdir),
166 toolname, VG_PLATFORM);
167 if (!VG_(access)(preload_tool_path, True/*r*/, False/*w*/, False/*x*/)) {
168 /* The tool's .so exists, put it into LD_PRELOAD with the core's so. */
169 VG_(sprintf)(preload_string, "%s/vgpreload_core-%s.so:%s", VG_(libdir),
170 VG_PLATFORM, preload_tool_path);
172 else {
173 /* The tool's .so doesn't exist, put only the core's .so into
174 LD_PRELOAD. */
175 VG_(sprintf)(preload_string, "%s/vgpreload_core-%s.so", VG_(libdir),
176 VG_PLATFORM);
178 VG_(free)(preload_tool_path);
180 VG_(debugLog)(2, "initimg", "preload_string:\n");
181 VG_(debugLog)(2, "initimg", " \"%s\"\n", preload_string);
183 /* Count the original size of the env. */
184 envc = 0;
185 for (cpp = origenv; *cpp; cpp++)
186 envc++;
188 /* Allocate a new space, envc + 1 new entry + NULL. */
189 ret = VG_(malloc)("initimg-solaris.sce.3", sizeof(HChar*) * (envc + 1 + 1));
191 /* Copy it over. */
192 for (cpp = ret; *origenv; )
193 *cpp++ = *origenv++;
194 *cpp = NULL;
196 vg_assert(envc == cpp - ret);
198 /* Walk over the new environment, mashing as we go. */
199 for (cpp = ret; *cpp; cpp++) {
200 if (VG_(memcmp)(*cpp, ld_preload, ld_preload_len))
201 continue;
203 /* LD_PRELOAD entry found, smash it. */
204 SizeT size = VG_(strlen)(*cpp) + 1 /*:*/
205 + preload_string_size;
206 HChar *cp = VG_(malloc)("initimg-solaris.sce.4", size);
208 VG_(sprintf)(cp, "%s%s:%s", ld_preload, preload_string,
209 (*cpp) + ld_preload_len);
210 *cpp = cp;
212 ld_preload_done = True;
215 /* Add the missing bits. */
216 if (!ld_preload_done) {
217 SizeT size = ld_preload_len + preload_string_size;
218 HChar *cp = VG_(malloc)("initimg-solaris.sce.5", size);
220 VG_(sprintf)(cp, "%s%s", ld_preload, preload_string);
221 ret[envc++] = cp;
224 /* We've got ret[0 .. envc-1] live now. */
226 /* Find and remove a binding for VALGRIND_LAUNCHER. */
228 const HChar *v_launcher = VALGRIND_LAUNCHER "=";
229 SizeT v_launcher_len = VG_(strlen)(v_launcher);
231 for (i = 0; i < envc; i++)
232 if (!VG_(memcmp(ret[i], v_launcher, v_launcher_len))) {
233 /* VALGRIND_LAUNCHER was found. */
234 break;
237 if (i < envc) {
238 /* VALGRIND_LAUNCHER was found, remove it. */
239 for (; i < envc - 1; i++)
240 ret[i] = ret[i + 1];
241 envc--;
245 VG_(free)(preload_string);
246 ret[envc] = NULL;
248 return ret;
252 /*====================================================================*/
253 /*=== Setting up the client's stack ===*/
254 /*====================================================================*/
256 /* Add a string onto the string table, and return its address. */
257 static HChar *copy_str(HChar **tab, const HChar *str)
259 HChar *cp = *tab;
260 HChar *orig = cp;
262 while (*str)
263 *cp++ = *str++;
264 *cp++ = '\0';
266 *tab = cp;
268 return orig;
272 /* This sets up the client's initial stack, containing the args,
273 environment and aux vector.
275 The format of the stack is:
277 higher address +-----------------+ <- clstack_end
279 : string table :
281 +-----------------+
282 | AT_NULL |
284 | auxv |
285 +-----------------+
286 | NULL |
288 | envp |
289 +-----------------+
290 | NULL |
292 | argv |
293 +-----------------+
294 | argc |
295 lower address +-----------------+ <- sp
296 | undefined |
299 Allocate and create the initial client stack. It is allocated down from
300 clstack_end, which was previously determined by the address space manager.
301 The returned value is the SP value for the client.
303 Note that no aux vector is created by kernel on Solaris if the program is
304 statically linked (which is our case). That means we have to build auxv
305 from scratch. */
307 static Addr setup_client_stack(void *init_sp,
308 HChar **orig_envp,
309 const ExeInfo *info,
310 Addr clstack_end,
311 SizeT clstack_max_size,
312 const HChar *resolved_exe_name)
314 SysRes res;
315 HChar **cpp;
316 HChar *strtab; /* string table */
317 HChar *stringbase;
318 Addr *ptr;
319 vki_auxv_t *auxv;
320 SizeT stringsize; /* total size of strings in bytes */
321 SizeT auxsize; /* total size of auxv in bytes */
322 Int argc; /* total argc */
323 Int envc; /* total number of env vars */
324 SizeT stacksize; /* total client stack size */
325 Addr client_SP; /* client stack base (initial SP) */
326 Addr clstack_start;
327 Int i;
329 vg_assert(VG_IS_PAGE_ALIGNED(clstack_end + 1));
330 vg_assert(VG_(args_the_exename));
331 vg_assert(VG_(args_for_client));
333 /* ==================== compute sizes ==================== */
335 /* First of all, work out how big the client stack will be. */
336 stringsize = 0;
338 /* Paste on the extra args if the loader needs them (i.e. the #!
339 interpreter and its argument). */
340 argc = 0;
341 if (info->interp_name) {
342 argc++;
343 stringsize += VG_(strlen)(info->interp_name) + 1;
345 if (info->interp_args) {
346 argc++;
347 stringsize += VG_(strlen)(info->interp_args) + 1;
350 /* Now scan the args we're given... */
351 argc++;
352 stringsize += VG_(strlen)(VG_(args_the_exename)) + 1;
353 for (i = 0; i < VG_(sizeXA)(VG_(args_for_client)); i++) {
354 argc++;
355 stringsize += VG_(strlen)(*(HChar**)
356 VG_(indexXA)(VG_(args_for_client), i)) + 1;
359 /* ...and the environment. */
360 envc = 0;
361 for (cpp = orig_envp; *cpp; cpp++) {
362 envc++;
363 stringsize += VG_(strlen)(*cpp) + 1;
366 /* Now, how big is the auxv?
368 AT_SUN_PLATFORM
369 AT_SUN_EXECNAME
370 AT_PHDR
371 AT_BASE
372 AT_FLAGS
373 AT_PAGESZ
374 AT_SUN_AUXFLAFGS
375 AT_SUN_HWCAP
376 AT_NULL
378 It would be possible to also add AT_PHENT, AT_PHNUM, AT_ENTRY,
379 AT_SUN_LDDATA, but they don't seem to be so important. */
380 auxsize = 9 * sizeof(*auxv);
381 # if defined(VGA_x86) || defined(VGA_amd64)
382 /* AT_SUN_PLATFORM string. */
383 stringsize += VG_(strlen)("i86pc") + 1;
384 # else
385 # error "Unknown architecture"
386 # endif
387 /* AT_SUN_EXECNAME string. */
388 stringsize += VG_(strlen)(resolved_exe_name) + 1;
390 /* Calculate how big the client stack is. */
391 stacksize =
392 sizeof(Word) + /* argc */
393 sizeof(HChar**) + /* argc[0] == exename */
394 sizeof(HChar**) * argc + /* argv */
395 sizeof(HChar**) + /* terminal NULL */
396 sizeof(HChar**) * envc + /* envp */
397 sizeof(HChar**) + /* terminal NULL */
398 auxsize + /* auxv */
399 VG_ROUNDUP(stringsize, sizeof(Word)); /* strings (aligned) */
401 /* The variable client_SP is the client's stack pointer. */
402 client_SP = clstack_end - stacksize;
403 client_SP = VG_ROUNDDN(client_SP, 16); /* Make stack 16 byte aligned. */
405 /* Calculate base of the string table (aligned). */
406 stringbase = (HChar*)clstack_end - VG_ROUNDUP(stringsize, sizeof(Int));
407 strtab = stringbase;
409 clstack_start = VG_PGROUNDDN(client_SP);
411 /* Calculate the max stack size. */
412 clstack_max_size = VG_PGROUNDUP(clstack_max_size);
414 /* Record stack extent -- needed for stack-change code. */
415 VG_(clstk_start_base) = clstack_start;
416 VG_(clstk_end) = clstack_end;
417 VG_(clstk_max_size) = clstack_max_size;
419 if (0)
420 VG_(printf)("stringsize=%lu, auxsize=%lu, stacksize=%lu, maxsize=%#lx\n"
421 "clstack_start %#lx\n"
422 "clstack_end %#lx\n",
423 stringsize, auxsize, stacksize, clstack_max_size,
424 clstack_start, clstack_end);
426 /* ==================== allocate space ==================== */
429 SizeT anon_size = clstack_end - clstack_start + 1;
430 SizeT resvn_size = clstack_max_size - anon_size;
431 Addr anon_start = clstack_start;
432 Addr resvn_start = anon_start - resvn_size;
433 SizeT inner_HACK = 0;
434 Bool ok;
436 /* So far we've only accounted for space requirements down to the stack
437 pointer. If this target's ABI requires a redzone below the stack
438 pointer, we need to allocate an extra page, to handle the worst case
439 in which the stack pointer is almost at the bottom of a page, and so
440 there is insufficient room left over to put the redzone in. In this
441 case the simple thing to do is allocate an extra page, by shrinking
442 the reservation by one page and growing the anonymous area by a
443 corresponding page. */
444 vg_assert(VG_STACK_REDZONE_SZB >= 0);
445 vg_assert(VG_STACK_REDZONE_SZB < VKI_PAGE_SIZE);
446 if (VG_STACK_REDZONE_SZB > 0) {
447 vg_assert(resvn_size > VKI_PAGE_SIZE);
448 resvn_size -= VKI_PAGE_SIZE;
449 anon_start -= VKI_PAGE_SIZE;
450 anon_size += VKI_PAGE_SIZE;
453 vg_assert(VG_IS_PAGE_ALIGNED(anon_size));
454 vg_assert(VG_IS_PAGE_ALIGNED(resvn_size));
455 vg_assert(VG_IS_PAGE_ALIGNED(anon_start));
456 vg_assert(VG_IS_PAGE_ALIGNED(resvn_start));
457 vg_assert(resvn_start == clstack_end + 1 - clstack_max_size);
459 # ifdef ENABLE_INNER
460 /* Create 1M non-fault-extending stack. */
461 inner_HACK = 1024 * 1024;
462 # endif
464 if (0)
465 VG_(printf)("resvn_start=%#lx, resvn_size=%#lx\n"
466 "anon_start=%#lx, anon_size=%#lx\n",
467 resvn_start, resvn_size, anon_start, anon_size);
469 /* Create a shrinkable reservation followed by an anonymous segment.
470 Together these constitute a growdown stack. */
471 ok = VG_(am_create_reservation)(resvn_start,
472 resvn_size - inner_HACK,
473 SmUpper,
474 anon_size + inner_HACK);
475 if (ok) {
476 /* Allocate a stack - mmap enough space for the stack. */
477 res = VG_(am_mmap_anon_fixed_client)(anon_start - inner_HACK,
478 anon_size + inner_HACK,
479 info->stack_prot);
481 if (!ok || sr_isError(res)) {
482 /* Allocation of the stack failed. We have to stop. */
483 VG_(printf)("valgrind: "
484 "I failed to allocate space for the application's stack.\n");
485 VG_(printf)("valgrind: "
486 "This may be the result of a very large --main-stacksize=\n");
487 VG_(printf)("valgrind: setting. Cannot continue. Sorry.\n\n");
488 VG_(exit)(1);
489 /*NOTREACHED*/
493 /* ==================== create client stack ==================== */
495 ptr = (Addr*)client_SP;
497 /* Copy-out client argc. */
498 *ptr++ = argc;
500 /* Copy-out client argv. */
501 if (info->interp_name) {
502 *ptr++ = (Addr)copy_str(&strtab, info->interp_name);
503 VG_(free)(info->interp_name);
505 if (info->interp_args) {
506 *ptr++ = (Addr)copy_str(&strtab, info->interp_args);
507 VG_(free)(info->interp_args);
510 *ptr++ = (Addr)copy_str(&strtab, VG_(args_the_exename));
511 for (i = 0; i < VG_(sizeXA)(VG_(args_for_client)); i++)
512 *ptr++ = (Addr)copy_str(
513 &strtab, *(HChar**) VG_(indexXA)(VG_(args_for_client), i));
514 *ptr++ = 0;
516 /* Copy-out envp. */
517 VG_(client_envp) = (HChar**)ptr;
518 for (cpp = orig_envp; *cpp; ptr++, cpp++)
519 *ptr = (Addr)copy_str(&strtab, *cpp);
520 *ptr++ = 0;
522 /* Create aux vector. */
523 auxv = (auxv_t*)ptr;
524 VG_(client_auxv) = (UWord*)ptr;
526 /* AT_SUN_PLATFORM */
527 auxv->a_type = VKI_AT_SUN_PLATFORM;
528 # if defined(VGA_x86) || defined(VGA_amd64)
529 auxv->a_un.a_ptr = copy_str(&strtab, "i86pc");
530 # else
531 # error "Unknown architecture"
532 # endif
533 auxv++;
535 /* AT_SUN_EXECNAME */
536 auxv->a_type = VKI_AT_SUN_EXECNAME;
537 auxv->a_un.a_ptr = copy_str(&strtab, resolved_exe_name);
538 auxv++;
540 /* AT_PHDR */
541 if (info->phdr) {
542 auxv->a_type = VKI_AT_PHDR;
543 auxv->a_un.a_val = info->phdr;
544 auxv++;
547 /* AT_BASE */
548 auxv->a_type = VKI_AT_BASE;
549 auxv->a_un.a_val = info->interp_offset;
550 auxv++;
552 /* AT_FLAGS */
553 auxv->a_type = VKI_AT_FLAGS;
554 # if defined(VGA_x86) || defined(VGA_amd64)
555 auxv->a_un.a_val = 0; /* 0 on i86pc */
556 # else
557 # error "Unknown architecture"
558 # endif
559 auxv++;
561 /* AT_PAGESZ */
562 auxv->a_type = VKI_AT_PAGESZ;
563 auxv->a_un.a_val = VKI_PAGE_SIZE;
564 auxv++;
566 /* AT_SUN_AUXFLAFGS */
567 auxv->a_type = VKI_AT_SUN_AUXFLAGS;
568 /* XXX Handle AF_SUN_SETUGID? */
569 auxv->a_un.a_val = VKI_AF_SUN_HWCAPVERIFY;
570 auxv++;
572 /* AT_SUN_HWCAP */
574 VexArch vex_arch;
575 VexArchInfo vex_archinfo;
576 UInt hwcaps;
578 VG_(machine_get_VexArchInfo)(&vex_arch, &vex_archinfo);
580 # if defined(VGA_x86)
581 vg_assert(vex_arch == VexArchX86);
583 /* Set default hwcaps. */
584 hwcaps =
585 VKI_AV_386_FPU /* x87-style floating point */
586 | VKI_AV_386_TSC /* rdtsc insn */
587 | VKI_AV_386_CX8 /* cmpxchg8b insn */
588 | VKI_AV_386_SEP /* sysenter and sysexit */
589 | VKI_AV_386_AMD_SYSC /* AMD's syscall and sysret */
590 | VKI_AV_386_CMOV /* conditional move insns */
591 | VKI_AV_386_MMX /* MMX insn */
592 | VKI_AV_386_AHF; /* lahf/sahf insns */
594 /* Handle additional hwcaps. */
595 if (vex_archinfo.hwcaps & VEX_HWCAPS_X86_SSE1)
596 hwcaps |=
597 VKI_AV_386_FXSR /* fxsave and fxrstor */
598 | VKI_AV_386_SSE; /* SSE insns and regs */
599 if (vex_archinfo.hwcaps & VEX_HWCAPS_X86_SSE2) {
600 vg_assert(vex_archinfo.hwcaps & VEX_HWCAPS_X86_SSE1);
601 hwcaps |=
602 VKI_AV_386_SSE2; /* SSE2 insns and regs */
604 if (vex_archinfo.hwcaps & VEX_HWCAPS_X86_SSE3) {
605 vg_assert(vex_archinfo.hwcaps & VEX_HWCAPS_X86_SSE2);
606 hwcaps |=
607 VKI_AV_386_SSE3 /* SSE3 insns and regs */
608 | VKI_AV_386_SSSE3; /* Intel SSSE3 insns */
610 if (vex_archinfo.hwcaps & VEX_HWCAPS_X86_LZCNT)
611 hwcaps |=
612 VKI_AV_386_AMD_LZCNT; /* AMD's LZCNT insn */
614 /* No support for:
615 AV_386_AMD_MMX AMD's MMX insns
616 AV_386_AMD_3DNow AMD's 3Dnow! insns
617 AV_386_AMD_3DNowx AMD's 3Dnow! extended insns
618 AV_386_CX16 cmpxchg16b insn
619 AV_386_TSCP rdtscp instruction
620 AV_386_AMD_SSE4A AMD's SSE4A insns
621 AV_386_POPCNT POPCNT insn
622 AV_386_SSE4_1 Intel SSE4.1 insns
623 AV_386_SSE4_2 Intel SSE4.2 insns
624 AV_386_MOVBE Intel MOVBE insns
625 AV_386_AES Intel AES insns
626 AV_386_PCLMULQDQ Intel PCLMULQDQ insn
627 AV_386_XSAVE Intel XSAVE/XRSTOR insns
628 AV_386_AVX Intel AVX insns
629 illumos only:
630 AV_386_VMX Intel VMX support
631 AV_386_AMD_SVM AMD SVM support
632 solaris only:
633 AV_386_AMD_XOP AMD XOP insns
634 AV_386_AMD_FMA4 AMD FMA4 insns */
636 # elif defined(VGA_amd64)
637 vg_assert(vex_arch == VexArchAMD64);
639 /* Set default hwcaps. */
640 hwcaps =
641 VKI_AV_386_FPU /* x87-style floating point */
642 | VKI_AV_386_TSC /* rdtsc insn */
643 | VKI_AV_386_CX8 /* cmpxchg8b insn */
644 | VKI_AV_386_AMD_SYSC /* AMD's syscall and sysret */
645 | VKI_AV_386_CMOV /* conditional move insns */
646 | VKI_AV_386_MMX /* MMX insn */
647 | VKI_AV_386_AHF /* lahf/sahf insns */
648 | VKI_AV_386_FXSR /* fxsave and fxrstor */
649 | VKI_AV_386_SSE /* SSE insns and regs */
650 | VKI_AV_386_SSE2; /* SSE2 insns and regs */
652 /* Handle additional hwcaps. */
653 if (vex_archinfo.hwcaps & VEX_HWCAPS_AMD64_SSE3)
654 hwcaps |=
655 VKI_AV_386_SSE3 /* SSE3 insns and regs */
656 | VKI_AV_386_SSSE3; /* Intel SSSE3 insns */
657 if (vex_archinfo.hwcaps & VEX_HWCAPS_AMD64_CX16)
658 hwcaps |=
659 VKI_AV_386_CX16; /* cmpxchg16b insn */
660 if (vex_archinfo.hwcaps & VEX_HWCAPS_AMD64_LZCNT)
661 hwcaps |=
662 VKI_AV_386_AMD_LZCNT; /* AMD's LZCNT insn */
663 if (vex_archinfo.hwcaps & VEX_HWCAPS_AMD64_RDTSCP)
664 hwcaps |=
665 VKI_AV_386_TSCP; /* rdtscp instruction */
666 if ((vex_archinfo.hwcaps & VEX_HWCAPS_AMD64_SSE3) &&
667 (vex_archinfo.hwcaps & VEX_HWCAPS_AMD64_CX16)) {
668 /* The CPUID simulation provided by VEX claims to have POPCNT, AES
669 and SSE4 (SSE4.1/SSE4.2) in the SSE3+CX16 configuration. */
670 hwcaps |=
671 VKI_AV_386_POPCNT /* POPCNT insn */
672 | VKI_AV_386_AES /* Intel AES insns */
673 | VKI_AV_386_SSE4_1 /* Intel SSE4.1 insns */
674 | VKI_AV_386_SSE4_2; /* Intel SSE4.2 insns */
676 if ((vex_archinfo.hwcaps & VEX_HWCAPS_AMD64_SSE3) &&
677 (vex_archinfo.hwcaps & VEX_HWCAPS_AMD64_CX16) &&
678 (vex_archinfo.hwcaps & VEX_HWCAPS_AMD64_AVX)) {
679 /* The CPUID simulation provided by VEX claims to have PCLMULQDQ and
680 XSAVE in the SSE3+CX16+AVX configuration. */
681 hwcaps |=
682 VKI_AV_386_PCLMULQDQ /* Intel PCLMULQDQ insn */
683 | VKI_AV_386_XSAVE; /* Intel XSAVE/XRSTOR insns */
685 /* No support for:
686 AV_386_SEP sysenter and sysexit
687 AV_386_AMD_MMX AMD's MMX insns
688 AV_386_AMD_3DNow AMD's 3Dnow! insns
689 AV_386_AMD_3DNowx AMD's 3Dnow! extended insns
690 AV_386_AMD_SSE4A AMD's SSE4A insns
691 AV_386_MOVBE Intel MOVBE insns
692 AV_386_AVX Intel AVX insns
693 illumos only:
694 AV_386_VMX Intel VMX support
695 AV_386_AMD_SVM AMD SVM support
696 solaris only:
697 AV_386_AMD_XOP AMD XOP insns
698 AV_386_AMD_FMA4 AMD FMA4 insns
700 TODO VEX supports AVX, BMI and AVX2. Investigate if they can be
701 enabled on Solaris/illumos.
704 # else
705 # error "Unknown architecture"
706 # endif
708 auxv->a_type = VKI_AT_SUN_HWCAP;
709 auxv->a_un.a_val = hwcaps;
710 auxv++;
713 /* AT_SUN_HWCAP2 */
715 /* No support for:
716 illumos only:
717 AV_386_2_F16C F16C half percision extensions
718 AV_386_2_RDRAND RDRAND insn
719 solaris only:
720 AV2_386_RDRAND Intel RDRAND insns
721 AV2_386_FMA Intel FMA insn
722 AV2_386_F16C IEEE half precn(float) insn
723 AV2_386_AMD_TBM AMD TBM insn
724 AV2_386_BMI1 Intel BMI1 insn
725 AV2_386_FSGSBASE Intel RD/WR FS/GSBASE insn
726 AV2_386_AVX2 Intel AVX2 insns
727 AV2_386_BMI2 Intel BMI2 insns
728 AV2_386_HLE Intel HLE insns
729 AV2_386_RTM Intel RTM insns
730 AV2_386_EFS Intel Enhanced Fast String
731 AV2_386_RDSEED Intel RDSEED insn
732 AV2_386_ADX Intel ADX insns
733 AV2_386_PRFCHW Intel PREFETCHW hint
737 /* AT_NULL */
738 auxv->a_type = VKI_AT_NULL;
739 auxv->a_un.a_val = 0;
741 vg_assert(strtab - stringbase == stringsize);
743 /* The variable client_SP is now pointing at client's argc/argv. */
745 if (0)
746 VG_(printf)("startup SP = %#lx\n", client_SP);
747 return client_SP;
751 /* Allocate the client data segment. It is an expandable anonymous mapping
752 abutting a 1-page reservation. The data segment starts at VG_(brk_base)
753 and runs up to VG_(brk_limit). None of these two values have to be
754 page-aligned.
755 Reservation segment is used to protect the data segment merging with
756 a pre-existing segment. This should be no problem because address space
757 manager ensures that requests for client address space are satisfied from
758 the highest available addresses. However when memory is low, data segment
759 can meet with mmap'ed objects and the reservation segment separates these.
760 The page that contains VG_(brk_base) is already allocated by the program's
761 loaded data segment. The brk syscall wrapper handles this special case.
762 See the brk syscall wrapper for more information. */
763 static void setup_client_dataseg(SizeT initial_size)
765 Bool ok;
766 SysRes sres;
767 Addr anon_start = VG_PGROUNDUP(VG_(brk_base));
768 SizeT anon_size = VG_PGROUNDUP(initial_size);
769 Addr resvn_start = anon_start + anon_size;
770 SizeT resvn_size = VKI_PAGE_SIZE;
771 const NSegment *seg;
772 UInt prot;
774 vg_assert(VG_IS_PAGE_ALIGNED(anon_size));
775 vg_assert(VG_IS_PAGE_ALIGNED(resvn_size));
776 vg_assert(VG_IS_PAGE_ALIGNED(anon_start));
777 vg_assert(VG_IS_PAGE_ALIGNED(resvn_start));
779 /* Stay sane (because there's been no brk activity yet). */
780 vg_assert(VG_(brk_base) == VG_(brk_limit));
782 /* Find the loaded data segment and remember its protection. */
783 seg = VG_(am_find_nsegment)(VG_(brk_base) - 1);
784 vg_assert(seg);
785 prot = (seg->hasR ? VKI_PROT_READ : 0)
786 | (seg->hasW ? VKI_PROT_WRITE : 0)
787 | (seg->hasX ? VKI_PROT_EXEC : 0);
789 /* Try to create the data segment and associated reservation where
790 VG_(brk_base) says. */
791 ok = VG_(am_create_reservation)(resvn_start, resvn_size, SmLower, anon_size);
792 if (!ok) {
793 /* That didn't work, we're hosed. */
794 VG_(printf)("valgrind: cannot initialize a brk segment\n");
795 VG_(exit)(1);
796 /*NOTREACHED*/
798 vg_assert(ok);
800 /* Map the data segment. */
801 sres = VG_(am_mmap_anon_fixed_client)(anon_start, anon_size, prot);
802 vg_assert(!sr_isError(sres));
803 vg_assert(sr_Res(sres) == anon_start);
807 /*====================================================================*/
808 /*=== TOP-LEVEL: VG_(setup_client_initial_image) ===*/
809 /*====================================================================*/
811 /* Create the client's initial memory image. */
812 IIFinaliseImageInfo VG_(ii_create_image)(IICreateImageInfo iicii,
813 const VexArchInfo *vex_archinfo)
815 ExeInfo info;
816 HChar **env = NULL;
817 HChar resolved_exe_name[VKI_PATH_MAX];
819 IIFinaliseImageInfo iifii;
820 VG_(memset)(&iifii, 0, sizeof(iifii));
822 //--------------------------------------------------------------
823 // Load client executable, finding in $PATH if necessary
824 // p: early_process_cmd_line_options() [for 'exec', 'need_help']
825 // p: layout_remaining_space [so there's space]
826 //--------------------------------------------------------------
827 VG_(debugLog)(1, "initimg", "Loading client\n");
829 if (!VG_(args_the_exename)) {
830 VG_(err_missing_prog)();
831 /*NOTREACHED*/
834 load_client(&info, resolved_exe_name, sizeof(resolved_exe_name));
835 iifii.initial_client_IP = info.init_ip;
836 /* Note: TOC isn't available on Solaris. */
837 iifii.initial_client_TOC = info.init_toc;
838 iifii.initial_client_TP = info.init_thrptr;
839 /* Note that iifii.client_auxv is never set on Solaris, because it isn't
840 necessary to have this value in VG_(ii_finalise_image). */
842 //--------------------------------------------------------------
843 // Set up client's environment
844 // p: set-libdir [for VG_(libdir)]
845 // p: early_process_cmd_line_options() [for toolname]
846 //--------------------------------------------------------------
847 VG_(debugLog)(1, "initimg", "Setup client env\n");
848 env = setup_client_env(iicii.envp, iicii.toolname);
850 //--------------------------------------------------------------
851 // Setup client stack and EIP
852 // p: load_client() [for 'info']
853 // p: fix_environment() [for 'env']
854 //--------------------------------------------------------------
856 /* When allocating space for the client stack, take notice of the
857 --main-stacksize value. This makes it possible to run programs with
858 very large (primary) stack requirements simply by specifying
859 --main-stacksize. */
860 /* Logic is as follows:
861 - By default, use the client's current stack rlimit.
862 - If that exceeds 16M, clamp to 16M.
863 - If a larger --main-stacksize value is specified, use that instead.
864 - In all situations, the minimum allowed stack size is 1M.
866 void *init_sp = iicii.argv - 1;
867 SizeT m1 = 1024 * 1024;
868 SizeT m16 = 16 * m1;
869 SizeT szB = (SizeT)VG_(client_rlimit_stack).rlim_cur;
870 if (szB < m1)
871 szB = m1;
872 if (szB > m16)
873 szB = m16;
875 if (VG_(clo_main_stacksize) > 0)
876 szB = VG_(clo_main_stacksize);
877 if (szB < m1)
878 szB = m1;
880 szB = VG_PGROUNDUP(szB);
881 VG_(debugLog)(1, "initimg",
882 "Setup client stack: size will be %ld\n", szB);
884 iifii.clstack_max_size = szB;
885 iifii.initial_client_SP = setup_client_stack(init_sp, env, &info,
886 iicii.clstack_end,
887 iifii.clstack_max_size,
888 resolved_exe_name);
889 VG_(free)(env);
891 VG_(debugLog)(2, "initimg", "Client info: "
892 "initial_IP=%#lx, initial_TOC=%#lx, brk_base=%#lx\n",
893 iifii.initial_client_IP, iifii.initial_client_TOC,
894 VG_(brk_base));
895 VG_(debugLog)(2, "initimg", "Client info: "
896 "initial_SP=%#lx, max_stack_size=%lu\n",
897 iifii.initial_client_SP,
898 iifii.clstack_max_size);
901 //--------------------------------------------------------------
902 // Setup client data (brk) segment. Initially segment at least
903 // 1 MB and at most 8 MB large which abuts a 1-page reservation.
904 // p: load_client() [for 'info' and hence VG_(brk_base)]
905 //--------------------------------------------------------------
907 SizeT m1 = 1024 * 1024;
908 SizeT m8 = 8 * m1;
909 SizeT dseg_max_size = VG_(client_rlimit_data).rlim_cur;
910 VG_(debugLog)(1, "initimg", "Setup client data (brk) segment at %#lx\n",
911 VG_(brk_base));
912 if (dseg_max_size < m1)
913 dseg_max_size = m1;
914 if (dseg_max_size > m8)
915 dseg_max_size = m8;
916 dseg_max_size = VG_PGROUNDUP(dseg_max_size);
918 setup_client_dataseg(dseg_max_size);
921 return iifii;
925 /*====================================================================*/
926 /*=== TOP-LEVEL: VG_(finalise_image) ===*/
927 /*====================================================================*/
929 /* Just before starting the client, we may need to make final adjustments to
930 its initial image. Also we need to set up the VEX guest state for thread 1
931 (the root thread) and copy in essential starting values. This is handed
932 the IIFinaliseImageInfo created by VG_(ii_create_image).
934 void VG_(ii_finalise_image)(IIFinaliseImageInfo iifii)
936 ThreadArchState *arch = &VG_(threads)[1].arch;
937 const NSegment *seg;
939 # if defined(VGA_x86)
940 vg_assert(0 == sizeof(VexGuestX86State) % LibVEX_GUEST_STATE_ALIGN);
942 /* Zero out the initial state, and set up the simulated FPU in a sane
943 way. */
944 LibVEX_GuestX86_initialise(&arch->vex);
946 /* Zero out the shadow areas. */
947 VG_(memset)(&arch->vex_shadow1, 0, sizeof(VexGuestX86State));
948 VG_(memset)(&arch->vex_shadow2, 0, sizeof(VexGuestX86State));
950 /* Put essential stuff into the new state. */
951 arch->vex.guest_ESP = iifii.initial_client_SP;
952 arch->vex.guest_EIP = iifii.initial_client_IP;
953 LibVEX_GuestX86_put_eflags(VKI_PSL_USER, &arch->vex);
955 /* Set %cs, %ds, %ss and %es to default values. */
956 __asm__ __volatile__ ("movw %%cs, %[cs]" : [cs] "=m" (arch->vex.guest_CS));
957 __asm__ __volatile__ ("movw %%ds, %[ds]" : [ds] "=m" (arch->vex.guest_DS));
958 __asm__ __volatile__ ("movw %%ss, %[ss]" : [ss] "=m" (arch->vex.guest_SS));
959 __asm__ __volatile__ ("movw %%es, %[es]" : [es] "=m" (arch->vex.guest_ES));
962 /* Initial thread pointer value will be saved in GDT when the thread is
963 started in the syswrap module and a thread's GDT is allocated. */
964 ThreadOSstate *os = &VG_(threads)[1].os_state;
965 os->thrptr = iifii.initial_client_TP;
968 # elif defined(VGA_amd64)
969 vg_assert(0 == sizeof(VexGuestAMD64State) % LibVEX_GUEST_STATE_ALIGN);
971 /* Zero out the initial state, and set up the simulated FPU in a sane
972 way. */
973 LibVEX_GuestAMD64_initialise(&arch->vex);
975 /* Zero out the shadow areas. */
976 VG_(memset)(&arch->vex_shadow1, 0, sizeof(VexGuestAMD64State));
977 VG_(memset)(&arch->vex_shadow2, 0, sizeof(VexGuestAMD64State));
979 /* Put essential stuff into the new state. */
980 arch->vex.guest_RSP = iifii.initial_client_SP;
981 arch->vex.guest_RIP = iifii.initial_client_IP;
982 arch->vex.guest_FS_CONST = iifii.initial_client_TP;
983 LibVEX_GuestAMD64_put_rflags(VKI_PSL_USER, &arch->vex);
985 # else
986 # error "Unknown platform"
987 # endif
989 /* Tell the tool that we just wrote to the registers. */
990 VG_TRACK(post_reg_write, Vg_CoreStartup, 1/*tid*/, 0/*offset*/,
991 sizeof(VexGuestArchState));
993 /* Tell the tool about the client data segment and then kill it which will
994 make it inaccessible/unaddressable. */
995 seg = VG_(am_find_nsegment)(VG_PGROUNDUP(VG_(brk_base)));
996 vg_assert(seg);
997 vg_assert(seg->kind == SkAnonC);
998 VG_TRACK(new_mem_brk, VG_(brk_base), seg->end + 1 - VG_(brk_base),
999 1/*tid*/);
1000 VG_TRACK(die_mem_brk, VG_(brk_base), seg->end + 1 - VG_(brk_base));
1003 #endif // defined(VGO_solaris)
1005 /*--------------------------------------------------------------------*/
1006 /*--- ---*/
1007 /*--------------------------------------------------------------------*/