2 /*--------------------------------------------------------------------*/
3 /*--- Startup: create initial process image on Solaris ---*/
4 /*--- initimg-solaris.c ---*/
5 /*--------------------------------------------------------------------*/
8 This file is part of Valgrind, a dynamic binary instrumentation
11 Copyright (C) 2011-2017 Petr Pavlu
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
29 The GNU General Public License is contained in the file COPYING.
32 /* Copyright 2013-2017, Ivo Raisr <ivosh@ivosh.net>. */
34 #if defined(VGO_solaris)
36 /* Note: This file is based on initimg-linux.c. */
38 #include "pub_core_basics.h"
39 #include "pub_core_vki.h"
40 #include "pub_core_debuglog.h"
41 #include "pub_core_libcbase.h"
42 #include "pub_core_libcassert.h"
43 #include "pub_core_libcfile.h"
44 #include "pub_core_libcproc.h"
45 #include "pub_core_libcprint.h"
46 #include "pub_core_xarray.h"
47 #include "pub_core_clientstate.h"
48 #include "pub_core_aspacemgr.h"
49 #include "pub_core_mallocfree.h"
50 #include "pub_core_machine.h"
51 #include "pub_core_ume.h"
52 #include "pub_core_options.h"
53 #include "pub_core_syswrap.h"
54 #include "pub_core_tooliface.h" /* VG_TRACK */
55 #include "pub_core_threadstate.h" /* ThreadArchState */
56 #include "priv_initimg_pathscan.h"
57 #include "pub_core_initimg.h" /* self */
60 /*====================================================================*/
61 /*=== Loading the client ===*/
62 /*====================================================================*/
64 /* Load the client whose name is VG_(argv_the_exename). */
65 static void load_client(/*OUT*/ExeInfo
*info
,
66 /*OUT*/HChar
*out_exe_name
, SizeT out_exe_name_size
)
68 const HChar
*exe_name
;
72 vg_assert(VG_(args_the_exename
));
73 exe_name
= ML_(find_executable
)(VG_(args_the_exename
));
76 VG_(printf
)("valgrind: %s: command not found\n", VG_(args_the_exename
));
77 /* Return POSIX's NOTFOUND. */
82 VG_(memset
)(info
, 0, sizeof(*info
));
83 ret
= VG_(do_exec
)(exe_name
, info
);
85 VG_(printf
)("valgrind: could not execute '%s'\n", exe_name
);
90 /* The client was successfully loaded! Continue. */
92 /* Save resolved exename. */
93 if (VG_(strlen
)(exe_name
) + 1 > out_exe_name_size
) {
94 /* This should not really happen. */
95 VG_(printf
)("valgrind: execname %s is too long\n", exe_name
);
99 VG_(strcpy
)(out_exe_name
, exe_name
);
101 /* Get hold of a file descriptor which refers to the client executable.
102 This is needed for attaching to GDB. */
103 res
= VG_(open
)(exe_name
, VKI_O_RDONLY
, VKI_S_IRUSR
);
104 if (!sr_isError(res
))
105 VG_(cl_exec_fd
) = sr_Res(res
);
107 /* Set initial brk values. */
108 if (info
->ldsoexec
) {
109 VG_(brk_base
) = VG_(brk_limit
) = -1;
111 VG_(brk_base
) = VG_(brk_limit
) = info
->brkbase
;
116 /*====================================================================*/
117 /*=== Setting up the client's environment ===*/
118 /*====================================================================*/
120 /* Prepare the client's environment. This is basically a copy of our
123 LD_PRELOAD=$VALGRIND_LIB/vgpreload_core-PLATFORM.so:
124 ($VALGRIND_LIB/vgpreload_TOOL-PLATFORM.so:)?
127 If this is missing, then it is added.
129 Also, remove any binding for VALGRIND_LAUNCHER=. The client should not be
132 If this needs to handle any more variables it should be hacked into
133 something table driven. The copy is VG_(malloc)'d space.
135 static HChar
**setup_client_env(HChar
**origenv
, const HChar
*toolname
)
137 const HChar
*ld_preload
= "LD_PRELOAD=";
138 SizeT ld_preload_len
= VG_(strlen
)(ld_preload
);
139 Bool ld_preload_done
= False
;
140 SizeT vglib_len
= VG_(strlen
)(VG_(libdir
));
144 HChar
*preload_tool_path
;
147 /* Alloc space for the
148 <path>/vgpreload_core-<platform>.so and
149 <path>/vgpreload_<tool>-<platform>.so
150 paths. We might not need the space for the tool path, but it doesn't
151 hurt to over-allocate briefly. */
152 SizeT preload_core_path_size
= vglib_len
+ sizeof("/vgpreload_core-") - 1
153 + sizeof(VG_PLATFORM
) - 1
155 SizeT preload_tool_path_size
= vglib_len
+ sizeof("/vgpreload_") - 1
156 + VG_(strlen
)(toolname
) + 1 /*-*/
157 + sizeof(VG_PLATFORM
) - 1
159 SizeT preload_string_size
= preload_core_path_size
160 + preload_tool_path_size
;
161 HChar
*preload_string
= VG_(malloc
)("initimg-solaris.sce.1",
162 preload_string_size
);
164 /* Check that the parameters are sane. */
168 /* Determine if there's a vgpreload_<tool>-<platform>.so file, and setup
170 preload_tool_path
= VG_(malloc
)("initimg-solaris.sce.2",
171 preload_tool_path_size
);
172 VG_(sprintf
)(preload_tool_path
, "%s/vgpreload_%s-%s.so", VG_(libdir
),
173 toolname
, VG_PLATFORM
);
174 if (!VG_(access
)(preload_tool_path
, True
/*r*/, False
/*w*/, False
/*x*/)) {
175 /* The tool's .so exists, put it into LD_PRELOAD with the core's so. */
176 VG_(sprintf
)(preload_string
, "%s/vgpreload_core-%s.so:%s", VG_(libdir
),
177 VG_PLATFORM
, preload_tool_path
);
180 /* The tool's .so doesn't exist, put only the core's .so into
182 VG_(sprintf
)(preload_string
, "%s/vgpreload_core-%s.so", VG_(libdir
),
185 VG_(free
)(preload_tool_path
);
187 VG_(debugLog
)(2, "initimg", "preload_string:\n");
188 VG_(debugLog
)(2, "initimg", " \"%s\"\n", preload_string
);
190 /* Count the original size of the env. */
192 for (cpp
= origenv
; *cpp
; cpp
++)
195 /* Allocate a new space, envc + 1 new entry + NULL. */
196 ret
= VG_(malloc
)("initimg-solaris.sce.3", sizeof(HChar
*) * (envc
+ 1 + 1));
199 for (cpp
= ret
; *origenv
; )
203 vg_assert(envc
== cpp
- ret
);
205 /* Walk over the new environment, mashing as we go. */
206 for (cpp
= ret
; *cpp
; cpp
++) {
207 if (VG_(memcmp
)(*cpp
, ld_preload
, ld_preload_len
))
210 /* LD_PRELOAD entry found, smash it. */
211 SizeT size
= VG_(strlen
)(*cpp
) + 1 /*:*/
212 + preload_string_size
;
213 HChar
*cp
= VG_(malloc
)("initimg-solaris.sce.4", size
);
215 VG_(sprintf
)(cp
, "%s%s:%s", ld_preload
, preload_string
,
216 (*cpp
) + ld_preload_len
);
219 ld_preload_done
= True
;
222 /* Add the missing bits. */
223 if (!ld_preload_done
) {
224 SizeT size
= ld_preload_len
+ preload_string_size
;
225 HChar
*cp
= VG_(malloc
)("initimg-solaris.sce.5", size
);
227 VG_(sprintf
)(cp
, "%s%s", ld_preload
, preload_string
);
231 /* We've got ret[0 .. envc-1] live now. */
233 /* Find and remove a binding for VALGRIND_LAUNCHER. */
235 const HChar
*v_launcher
= VALGRIND_LAUNCHER
"=";
236 SizeT v_launcher_len
= VG_(strlen
)(v_launcher
);
238 for (i
= 0; i
< envc
; i
++)
239 if (!VG_(memcmp
)(ret
[i
], v_launcher
, v_launcher_len
)) {
240 /* VALGRIND_LAUNCHER was found. */
245 /* VALGRIND_LAUNCHER was found, remove it. */
246 for (; i
< envc
- 1; i
++)
252 VG_(free
)(preload_string
);
259 /*====================================================================*/
260 /*=== Setting up the client's stack ===*/
261 /*====================================================================*/
263 /* Add a string onto the string table, and return its address. */
264 static HChar
*copy_str(HChar
**tab
, const HChar
*str
)
278 #if defined(SOLARIS_RESERVE_SYSSTAT_ADDR) || \
279 defined(SOLARIS_RESERVE_SYSSTAT_ZONE_ADDR)
280 #define ORIG_AUXV_PRESENT 1
283 #if defined(ORIG_AUXV_PRESENT)
284 /* The auxiliary vector might not be present. So we cross-check pointers from
285 argv and envp pointing to the string table. */
286 static vki_auxv_t
*find_original_auxv(Addr init_sp
)
288 HChar
**sp
= (HChar
**) init_sp
;
289 HChar
*lowest_str_ptr
= (HChar
*) UINTPTR_MAX
; // lowest ptr to string table
293 while (*sp
!= NULL
) { // skip argv
294 if (*sp
< lowest_str_ptr
)
295 lowest_str_ptr
= *sp
;
300 while (*sp
!= 0) { // skip env
301 if (*sp
< lowest_str_ptr
)
302 lowest_str_ptr
= *sp
;
307 if ((Addr
) sp
< (Addr
) lowest_str_ptr
) {
308 return (vki_auxv_t
*) sp
;
314 static void copy_auxv_entry(const vki_auxv_t
*orig_auxv
, Int type
,
315 const HChar
*type_name
, vki_auxv_t
*auxv
)
317 vg_assert(auxv
!= NULL
);
319 if (orig_auxv
== NULL
) {
320 VG_(printf
)("valgrind: Cannot locate auxiliary vector.\n");
321 VG_(printf
)("valgrind: Cannot continue. Sorry.\n\n");
325 for ( ; orig_auxv
->a_type
!= VKI_AT_NULL
; orig_auxv
++) {
326 if (orig_auxv
->a_type
== type
) {
328 auxv
->a_un
.a_val
= orig_auxv
->a_un
.a_val
;
333 VG_(printf
)("valgrind: Cannot locate %s in the aux\n", type_name
);
334 VG_(printf
)("valgrind: vector. Cannot continue. Sorry.\n\n");
337 #endif /* ORIG_AUXV_PRESENT */
339 /* This sets up the client's initial stack, containing the args,
340 environment and aux vector.
342 The format of the stack is:
344 higher address +-----------------+ <- clstack_end
362 lower address +-----------------+ <- sp
366 Allocate and create the initial client stack. It is allocated down from
367 clstack_end, which was previously determined by the address space manager.
368 The returned value is the SP value for the client.
370 Note that auxiliary vector is *not* created by kernel on illumos and
371 Solaris 11 if the program is statically linked (which is our case).
372 Although we now taught Solaris 11.4 to create the auxiliary vector, we still
373 have to build auxv from scratch, to make the code consistent. */
375 static Addr
setup_client_stack(Addr init_sp
,
379 SizeT clstack_max_size
,
380 const HChar
*resolved_exe_name
)
384 HChar
*strtab
; /* string table */
388 SizeT stringsize
; /* total size of strings in bytes */
389 SizeT auxsize
; /* total size of auxv in bytes */
390 Int argc
; /* total argc */
391 Int envc
; /* total number of env vars */
392 SizeT stacksize
; /* total client stack size */
393 Addr client_SP
; /* client stack base (initial SP) */
397 vg_assert(VG_IS_PAGE_ALIGNED(clstack_end
+ 1));
398 vg_assert(VG_(args_the_exename
));
399 vg_assert(VG_(args_for_client
));
401 # if defined(ORIG_AUXV_PRESENT)
402 /* Get the original auxv (if any). */
403 vki_auxv_t
*orig_auxv
= find_original_auxv(init_sp
);
404 # endif /* ORIG_AUXV_PRESENT */
406 /* ==================== compute sizes ==================== */
408 /* First of all, work out how big the client stack will be. */
411 /* Paste on the extra args if the loader needs them (i.e. the #!
412 interpreter and its argument). */
414 if (info
->interp_name
) {
416 stringsize
+= VG_(strlen
)(info
->interp_name
) + 1;
418 if (info
->interp_args
) {
420 stringsize
+= VG_(strlen
)(info
->interp_args
) + 1;
423 /* Now scan the args we're given... */
425 stringsize
+= VG_(strlen
)(VG_(args_the_exename
)) + 1;
426 for (i
= 0; i
< VG_(sizeXA
)(VG_(args_for_client
)); i
++) {
428 stringsize
+= VG_(strlen
)(*(HChar
**)
429 VG_(indexXA
)(VG_(args_for_client
), i
)) + 1;
432 /* ...and the environment. */
434 for (cpp
= orig_envp
; *cpp
; cpp
++) {
436 stringsize
+= VG_(strlen
)(*cpp
) + 1;
439 /* Now, how big is the auxv?
443 AT_PHDR (not for elfs with no PT_PHDR, such as ld.so.1)
450 AT_SUN_SYSSTAT_ADDR (if supported)
451 AT_SUN_SYSSTAT_ZONE_ADDR (if supported)
454 It would be possible to also add AT_PHENT, AT_PHNUM, AT_SUN_LDDATA,
455 but they don't seem to be so important. */
456 auxsize
= 10 * sizeof(*auxv
);
457 # if defined(SOLARIS_RESERVE_SYSSTAT_ADDR)
458 auxsize
+= sizeof(*auxv
);
460 # if defined(SOLARIS_RESERVE_SYSSTAT_ZONE_ADDR)
461 auxsize
+= sizeof(*auxv
);
464 # if defined(VGA_x86) || defined(VGA_amd64)
465 /* AT_SUN_PLATFORM string. */
466 stringsize
+= VG_(strlen
)("i86pc") + 1;
468 # error "Unknown architecture"
470 /* AT_SUN_EXECNAME string. */
471 stringsize
+= VG_(strlen
)(resolved_exe_name
) + 1;
473 /* Calculate how big the client stack is. */
475 sizeof(Word
) + /* argc */
476 sizeof(HChar
**) + /* argc[0] == exename */
477 sizeof(HChar
**) * argc
+ /* argv */
478 sizeof(HChar
**) + /* terminal NULL */
479 sizeof(HChar
**) * envc
+ /* envp */
480 sizeof(HChar
**) + /* terminal NULL */
482 VG_ROUNDUP(stringsize
, sizeof(Word
)); /* strings (aligned) */
484 /* The variable client_SP is the client's stack pointer. */
485 client_SP
= clstack_end
- stacksize
;
486 client_SP
= VG_ROUNDDN(client_SP
, 16); /* Make stack 16 byte aligned. */
488 /* Calculate base of the string table (aligned). */
489 stringbase
= (HChar
*)clstack_end
- VG_ROUNDUP(stringsize
, sizeof(Int
));
492 clstack_start
= VG_PGROUNDDN(client_SP
);
494 /* Calculate the max stack size. */
495 clstack_max_size
= VG_PGROUNDUP(clstack_max_size
);
497 /* Record stack extent -- needed for stack-change code. */
498 VG_(clstk_start_base
) = clstack_start
;
499 VG_(clstk_end
) = clstack_end
;
500 VG_(clstk_max_size
) = clstack_max_size
;
503 VG_(printf
)("stringsize=%lu, auxsize=%lu, stacksize=%lu, maxsize=%#lx\n"
504 "clstack_start %#lx\n"
505 "clstack_end %#lx\n",
506 stringsize
, auxsize
, stacksize
, clstack_max_size
,
507 clstack_start
, clstack_end
);
509 /* ==================== allocate space ==================== */
512 SizeT anon_size
= clstack_end
- clstack_start
+ 1;
513 SizeT resvn_size
= clstack_max_size
- anon_size
;
514 Addr anon_start
= clstack_start
;
515 Addr resvn_start
= anon_start
- resvn_size
;
516 SizeT inner_HACK
= 0;
519 /* So far we've only accounted for space requirements down to the stack
520 pointer. If this target's ABI requires a redzone below the stack
521 pointer, we need to allocate an extra page, to handle the worst case
522 in which the stack pointer is almost at the bottom of a page, and so
523 there is insufficient room left over to put the redzone in. In this
524 case the simple thing to do is allocate an extra page, by shrinking
525 the reservation by one page and growing the anonymous area by a
526 corresponding page. */
527 vg_assert(VG_STACK_REDZONE_SZB
>= 0);
528 vg_assert(VG_STACK_REDZONE_SZB
< VKI_PAGE_SIZE
);
529 if (VG_STACK_REDZONE_SZB
> 0) {
530 vg_assert(resvn_size
> VKI_PAGE_SIZE
);
531 resvn_size
-= VKI_PAGE_SIZE
;
532 anon_start
-= VKI_PAGE_SIZE
;
533 anon_size
+= VKI_PAGE_SIZE
;
536 vg_assert(VG_IS_PAGE_ALIGNED(anon_size
));
537 vg_assert(VG_IS_PAGE_ALIGNED(resvn_size
));
538 vg_assert(VG_IS_PAGE_ALIGNED(anon_start
));
539 vg_assert(VG_IS_PAGE_ALIGNED(resvn_start
));
540 vg_assert(resvn_start
== clstack_end
+ 1 - clstack_max_size
);
543 /* Create 1M non-fault-extending stack. */
544 inner_HACK
= 1024 * 1024;
548 VG_(printf
)("resvn_start=%#lx, resvn_size=%#lx\n"
549 "anon_start=%#lx, anon_size=%#lx\n",
550 resvn_start
, resvn_size
, anon_start
, anon_size
);
552 /* Create a shrinkable reservation followed by an anonymous segment.
553 Together these constitute a growdown stack. */
554 ok
= VG_(am_create_reservation
)(resvn_start
,
555 resvn_size
- inner_HACK
,
557 anon_size
+ inner_HACK
);
559 /* Allocate a stack - mmap enough space for the stack. */
560 res
= VG_(am_mmap_anon_fixed_client
)(anon_start
- inner_HACK
,
561 anon_size
+ inner_HACK
,
564 if (!ok
|| sr_isError(res
)) {
565 /* Allocation of the stack failed. We have to stop. */
566 VG_(printf
)("valgrind: "
567 "I failed to allocate space for the application's stack.\n");
568 VG_(printf
)("valgrind: "
569 "This may be the result of a very large --main-stacksize=\n");
570 VG_(printf
)("valgrind: setting. Cannot continue. Sorry.\n\n");
576 /* ==================== create client stack ==================== */
578 ptr
= (Addr
*)client_SP
;
580 /* Copy-out client argc. */
583 /* Copy-out client argv. */
584 if (info
->interp_name
)
585 *ptr
++ = (Addr
)copy_str(&strtab
, info
->interp_name
);
586 if (info
->interp_args
)
587 *ptr
++ = (Addr
)copy_str(&strtab
, info
->interp_args
);
589 *ptr
++ = (Addr
)copy_str(&strtab
, VG_(args_the_exename
));
590 for (i
= 0; i
< VG_(sizeXA
)(VG_(args_for_client
)); i
++)
591 *ptr
++ = (Addr
)copy_str(
592 &strtab
, *(HChar
**) VG_(indexXA
)(VG_(args_for_client
), i
));
596 VG_(client_envp
) = (HChar
**)ptr
;
597 for (cpp
= orig_envp
; *cpp
; ptr
++, cpp
++)
598 *ptr
= (Addr
)copy_str(&strtab
, *cpp
);
601 /* Create aux vector. */
603 VG_(client_auxv
) = (UWord
*)ptr
;
605 /* AT_SUN_PLATFORM */
606 auxv
->a_type
= VKI_AT_SUN_PLATFORM
;
607 # if defined(VGA_x86) || defined(VGA_amd64)
608 auxv
->a_un
.a_ptr
= copy_str(&strtab
, "i86pc");
610 # error "Unknown architecture"
614 /* AT_SUN_EXECNAME */
615 auxv
->a_type
= VKI_AT_SUN_EXECNAME
;
616 auxv
->a_un
.a_ptr
= copy_str(&strtab
, resolved_exe_name
);
620 if ((info
->real_phdr_present
) && (info
->phdr
!= 0)) {
621 auxv
->a_type
= VKI_AT_PHDR
;
622 auxv
->a_un
.a_val
= info
->phdr
;
627 auxv
->a_type
= VKI_AT_BASE
;
628 auxv
->a_un
.a_val
= info
->interp_offset
;
632 auxv
->a_type
= VKI_AT_ENTRY
;
633 auxv
->a_un
.a_val
= info
->entry
;
637 auxv
->a_type
= VKI_AT_FLAGS
;
638 # if defined(VGA_x86) || defined(VGA_amd64)
639 auxv
->a_un
.a_val
= 0; /* 0 on i86pc */
641 # error "Unknown architecture"
646 auxv
->a_type
= VKI_AT_PAGESZ
;
647 auxv
->a_un
.a_val
= VKI_PAGE_SIZE
;
650 /* AT_SUN_AUXFLAFGS */
651 auxv
->a_type
= VKI_AT_SUN_AUXFLAGS
;
652 /* XXX Handle AF_SUN_SETUGID? */
653 auxv
->a_un
.a_val
= VKI_AF_SUN_HWCAPVERIFY
;
659 VexArchInfo vex_archinfo
;
662 VG_(machine_get_VexArchInfo
)(&vex_arch
, &vex_archinfo
);
664 # if defined(VGA_x86)
665 vg_assert(vex_arch
== VexArchX86
);
667 /* Set default hwcaps. */
669 VKI_AV_386_FPU
/* x87-style floating point */
670 | VKI_AV_386_TSC
/* rdtsc insn */
671 | VKI_AV_386_CX8
/* cmpxchg8b insn */
672 | VKI_AV_386_SEP
/* sysenter and sysexit */
673 | VKI_AV_386_AMD_SYSC
/* AMD's syscall and sysret */
674 | VKI_AV_386_CMOV
/* conditional move insns */
675 | VKI_AV_386_MMX
/* MMX insn */
676 | VKI_AV_386_AHF
; /* lahf/sahf insns */
678 /* Handle additional hwcaps. */
679 if (vex_archinfo
.hwcaps
& VEX_HWCAPS_X86_SSE1
)
681 VKI_AV_386_FXSR
/* fxsave and fxrstor */
682 | VKI_AV_386_SSE
; /* SSE insns and regs */
683 if (vex_archinfo
.hwcaps
& VEX_HWCAPS_X86_SSE2
) {
684 vg_assert(vex_archinfo
.hwcaps
& VEX_HWCAPS_X86_SSE1
);
686 VKI_AV_386_SSE2
; /* SSE2 insns and regs */
688 if (vex_archinfo
.hwcaps
& VEX_HWCAPS_X86_SSE3
) {
689 vg_assert(vex_archinfo
.hwcaps
& VEX_HWCAPS_X86_SSE2
);
691 VKI_AV_386_SSE3
/* SSE3 insns and regs */
692 | VKI_AV_386_SSSE3
; /* Intel SSSE3 insns */
694 if (vex_archinfo
.hwcaps
& VEX_HWCAPS_X86_LZCNT
)
696 VKI_AV_386_AMD_LZCNT
; /* AMD's LZCNT insn */
699 AV_386_AMD_MMX AMD's MMX insns
700 AV_386_AMD_3DNow AMD's 3Dnow! insns
701 AV_386_AMD_3DNowx AMD's 3Dnow! extended insns
702 AV_386_CX16 cmpxchg16b insn
703 AV_386_TSCP rdtscp instruction
704 AV_386_AMD_SSE4A AMD's SSE4A insns
705 AV_386_POPCNT POPCNT insn
706 AV_386_SSE4_1 Intel SSE4.1 insns
707 AV_386_SSE4_2 Intel SSE4.2 insns
708 AV_386_MOVBE Intel MOVBE insns
709 AV_386_AES Intel AES insns
710 AV_386_PCLMULQDQ Intel PCLMULQDQ insn
711 AV_386_XSAVE Intel XSAVE/XRSTOR insns
712 AV_386_AVX Intel AVX insns
714 AV_386_VMX Intel VMX support
715 AV_386_AMD_SVM AMD SVM support
717 AV_386_AMD_XOP AMD XOP insns
718 AV_386_AMD_FMA4 AMD FMA4 insns */
720 # elif defined(VGA_amd64)
721 vg_assert(vex_arch
== VexArchAMD64
);
723 /* Set default hwcaps. */
725 VKI_AV_386_FPU
/* x87-style floating point */
726 | VKI_AV_386_TSC
/* rdtsc insn */
727 | VKI_AV_386_CX8
/* cmpxchg8b insn */
728 | VKI_AV_386_AMD_SYSC
/* AMD's syscall and sysret */
729 | VKI_AV_386_CMOV
/* conditional move insns */
730 | VKI_AV_386_MMX
/* MMX insn */
731 | VKI_AV_386_AHF
/* lahf/sahf insns */
732 | VKI_AV_386_FXSR
/* fxsave and fxrstor */
733 | VKI_AV_386_SSE
/* SSE insns and regs */
734 | VKI_AV_386_SSE2
; /* SSE2 insns and regs */
736 /* Handle additional hwcaps. */
737 if (vex_archinfo
.hwcaps
& VEX_HWCAPS_AMD64_SSE3
)
739 VKI_AV_386_SSE3
/* SSE3 insns and regs */
740 | VKI_AV_386_SSSE3
; /* Intel SSSE3 insns */
741 if (vex_archinfo
.hwcaps
& VEX_HWCAPS_AMD64_CX16
)
743 VKI_AV_386_CX16
; /* cmpxchg16b insn */
744 if (vex_archinfo
.hwcaps
& VEX_HWCAPS_AMD64_LZCNT
)
746 VKI_AV_386_AMD_LZCNT
; /* AMD's LZCNT insn */
747 if (vex_archinfo
.hwcaps
& VEX_HWCAPS_AMD64_RDTSCP
)
749 VKI_AV_386_TSCP
; /* rdtscp instruction */
750 if ((vex_archinfo
.hwcaps
& VEX_HWCAPS_AMD64_SSE3
) &&
751 (vex_archinfo
.hwcaps
& VEX_HWCAPS_AMD64_CX16
)) {
752 /* The CPUID simulation provided by VEX claims to have POPCNT, AES
753 and SSE4 (SSE4.1/SSE4.2) in the SSE3+CX16 configuration. */
755 VKI_AV_386_POPCNT
/* POPCNT insn */
756 | VKI_AV_386_AES
/* Intel AES insns */
757 | VKI_AV_386_SSE4_1
/* Intel SSE4.1 insns */
758 | VKI_AV_386_SSE4_2
; /* Intel SSE4.2 insns */
760 if ((vex_archinfo
.hwcaps
& VEX_HWCAPS_AMD64_SSE3
) &&
761 (vex_archinfo
.hwcaps
& VEX_HWCAPS_AMD64_CX16
) &&
762 (vex_archinfo
.hwcaps
& VEX_HWCAPS_AMD64_AVX
)) {
763 /* The CPUID simulation provided by VEX claims to have PCLMULQDQ and
764 XSAVE in the SSE3+CX16+AVX configuration. */
766 VKI_AV_386_PCLMULQDQ
/* Intel PCLMULQDQ insn */
767 | VKI_AV_386_XSAVE
; /* Intel XSAVE/XRSTOR insns */
770 AV_386_SEP sysenter and sysexit
771 AV_386_AMD_MMX AMD's MMX insns
772 AV_386_AMD_3DNow AMD's 3Dnow! insns
773 AV_386_AMD_3DNowx AMD's 3Dnow! extended insns
774 AV_386_AMD_SSE4A AMD's SSE4A insns
775 AV_386_MOVBE Intel MOVBE insns
776 AV_386_AVX Intel AVX insns
778 AV_386_VMX Intel VMX support
779 AV_386_AMD_SVM AMD SVM support
781 AV_386_AMD_XOP AMD XOP insns
782 AV_386_AMD_FMA4 AMD FMA4 insns
784 TODO VEX supports AVX, BMI and AVX2. Investigate if they can be
785 enabled on Solaris/illumos.
789 # error "Unknown architecture"
792 auxv
->a_type
= VKI_AT_SUN_HWCAP
;
793 auxv
->a_un
.a_val
= hwcaps
;
801 AV_386_2_F16C F16C half percision extensions
802 AV_386_2_RDRAND RDRAND insn
804 AV2_386_RDRAND Intel RDRAND insns
805 AV2_386_FMA Intel FMA insn
806 AV2_386_F16C IEEE half precn(float) insn
807 AV2_386_AMD_TBM AMD TBM insn
808 AV2_386_BMI1 Intel BMI1 insn
809 AV2_386_FSGSBASE Intel RD/WR FS/GSBASE insn
810 AV2_386_AVX2 Intel AVX2 insns
811 AV2_386_BMI2 Intel BMI2 insns
812 AV2_386_HLE Intel HLE insns
813 AV2_386_RTM Intel RTM insns
814 AV2_386_EFS Intel Enhanced Fast String
815 AV2_386_RDSEED Intel RDSEED insn
816 AV2_386_ADX Intel ADX insns
817 AV2_386_PRFCHW Intel PREFETCHW hint
821 # if defined(SOLARIS_RESERVE_SYSSTAT_ADDR)
822 /* AT_SUN_SYSSTAT_ADDR */
823 copy_auxv_entry(orig_auxv
, VKI_AT_SUN_SYSSTAT_ADDR
,
824 "AT_SUN_SYSSTAT_ADDR", auxv
);
825 VG_(change_mapping_ownership
)(auxv
->a_un
.a_val
, True
);
829 # if defined(SOLARIS_RESERVE_SYSSTAT_ZONE_ADDR)
830 /* AT_SUN_SYSSTAT_ZONE_ADDR */
831 copy_auxv_entry(orig_auxv
, VKI_AT_SUN_SYSSTAT_ZONE_ADDR
,
832 "AT_SUN_SYSSTAT_ZONE_ADDR", auxv
);
833 VG_(change_mapping_ownership
)(auxv
->a_un
.a_val
, True
);
838 auxv
->a_type
= VKI_AT_NULL
;
839 auxv
->a_un
.a_val
= 0;
841 vg_assert(strtab
- stringbase
== stringsize
);
843 /* The variable client_SP is now pointing at client's argc/argv. */
846 VG_(printf
)("startup SP = %#lx\n", client_SP
);
850 /*====================================================================*/
851 /*=== TOP-LEVEL: VG_(setup_client_initial_image) ===*/
852 /*====================================================================*/
854 /* Create the client's initial memory image. */
855 IIFinaliseImageInfo
VG_(ii_create_image
)(IICreateImageInfo iicii
,
856 const VexArchInfo
*vex_archinfo
)
860 HChar resolved_exe_name
[VKI_PATH_MAX
];
862 IIFinaliseImageInfo iifii
;
863 VG_(memset
)(&iifii
, 0, sizeof(iifii
));
865 //--------------------------------------------------------------
866 // Load client executable, finding in $PATH if necessary
867 // p: early_process_cmd_line_options() [for 'exec', 'need_help']
868 // p: layout_remaining_space [so there's space]
869 //--------------------------------------------------------------
870 VG_(debugLog
)(1, "initimg", "Loading client\n");
872 if (!VG_(args_the_exename
)) {
873 VG_(err_missing_prog
)();
877 load_client(&info
, resolved_exe_name
, sizeof(resolved_exe_name
));
878 iifii
.initial_client_IP
= info
.init_ip
;
879 /* Note: TOC isn't available on Solaris. */
880 iifii
.initial_client_TOC
= info
.init_toc
;
881 iifii
.initial_client_TP
= info
.init_thrptr
;
882 /* Note that iifii.client_auxv is never set on Solaris, because it isn't
883 necessary to have this value in VG_(ii_finalise_image). */
885 //--------------------------------------------------------------
886 // Set up client's environment
887 // p: set-libdir [for VG_(libdir)]
888 // p: early_process_cmd_line_options() [for toolname]
889 //--------------------------------------------------------------
890 VG_(debugLog
)(1, "initimg", "Setup client env\n");
891 env
= setup_client_env(iicii
.envp
, iicii
.toolname
);
893 //--------------------------------------------------------------
894 // Setup client stack and EIP
895 // p: load_client() [for 'info']
896 // p: fix_environment() [for 'env']
897 //--------------------------------------------------------------
899 /* When allocating space for the client stack, take notice of the
900 --main-stacksize value. This makes it possible to run programs with
901 very large (primary) stack requirements simply by specifying
903 /* Logic is as follows:
904 - By default, use the client's current stack rlimit.
905 - If that exceeds 16M, clamp to 16M.
906 - If a larger --main-stacksize value is specified, use that instead.
907 - In all situations, the minimum allowed stack size is 1M.
909 Addr init_sp
= (Addr
) (iicii
.argv
- 1);
910 SizeT m1
= 1024 * 1024;
912 SizeT szB
= (SizeT
)VG_(client_rlimit_stack
).rlim_cur
;
918 if (VG_(clo_main_stacksize
) > 0)
919 szB
= VG_(clo_main_stacksize
);
923 szB
= VG_PGROUNDUP(szB
);
924 VG_(debugLog
)(1, "initimg",
925 "Setup client stack: size will be %ld\n", szB
);
927 iifii
.clstack_max_size
= szB
;
928 iifii
.initial_client_SP
= setup_client_stack(init_sp
, env
, &info
,
930 iifii
.clstack_max_size
,
934 VG_(debugLog
)(2, "initimg", "Client info: "
935 "initial_IP=%#lx, initial_TOC=%#lx, brk_base=%#lx\n",
936 iifii
.initial_client_IP
, iifii
.initial_client_TOC
,
938 VG_(debugLog
)(2, "initimg", "Client info: "
939 "initial_SP=%#lx, max_stack_size=%lu\n",
940 iifii
.initial_client_SP
,
941 iifii
.clstack_max_size
);
945 /* We are executing the runtime linker itself.
946 Initial data (brk) segment is setup on demand, after the target dynamic
947 executable has been loaded or when a first brk() syscall is made.
948 It cannot be established now because it would conflict with a temporary
949 stack which ld.so.1 (when executed directly) uses for loading the
950 target dynamic executable. See PRE(sys_brk) in syswrap-solaris.c. */
952 if (!VG_(setup_client_dataseg
)()) {
953 VG_(printf
)("valgrind: cannot initialize data segment (brk).\n");
958 VG_(free
)(info
.interp_name
);
959 VG_(free
)(info
.interp_args
);
964 /*====================================================================*/
965 /*=== TOP-LEVEL: VG_(finalise_image) ===*/
966 /*====================================================================*/
968 /* Just before starting the client, we may need to make final adjustments to
969 its initial image. Also we need to set up the VEX guest state for thread 1
970 (the root thread) and copy in essential starting values. This is handed
971 the IIFinaliseImageInfo created by VG_(ii_create_image).
973 void VG_(ii_finalise_image
)(IIFinaliseImageInfo iifii
)
975 ThreadArchState
*arch
= &VG_(threads
)[1].arch
;
977 # if defined(VGA_x86)
978 vg_assert(0 == sizeof(VexGuestX86State
) % LibVEX_GUEST_STATE_ALIGN
);
980 /* Zero out the initial state, and set up the simulated FPU in a sane
982 LibVEX_GuestX86_initialise(&arch
->vex
);
984 /* Zero out the shadow areas. */
985 VG_(memset
)(&arch
->vex_shadow1
, 0, sizeof(VexGuestX86State
));
986 VG_(memset
)(&arch
->vex_shadow2
, 0, sizeof(VexGuestX86State
));
988 /* Put essential stuff into the new state. */
989 arch
->vex
.guest_ESP
= iifii
.initial_client_SP
;
990 arch
->vex
.guest_EIP
= iifii
.initial_client_IP
;
991 LibVEX_GuestX86_put_eflags(VKI_PSL_USER
, &arch
->vex
);
993 /* Set %cs, %ds, %ss and %es to default values. */
994 __asm__
__volatile__ ("movw %%cs, %[cs]" : [cs
] "=m" (arch
->vex
.guest_CS
));
995 __asm__
__volatile__ ("movw %%ds, %[ds]" : [ds
] "=m" (arch
->vex
.guest_DS
));
996 __asm__
__volatile__ ("movw %%ss, %[ss]" : [ss
] "=m" (arch
->vex
.guest_SS
));
997 __asm__
__volatile__ ("movw %%es, %[es]" : [es
] "=m" (arch
->vex
.guest_ES
));
1000 /* Initial thread pointer value will be saved in GDT when the thread is
1001 started in the syswrap module and a thread's GDT is allocated. */
1002 ThreadOSstate
*os
= &VG_(threads
)[1].os_state
;
1003 os
->thrptr
= iifii
.initial_client_TP
;
1006 # elif defined(VGA_amd64)
1007 vg_assert(0 == sizeof(VexGuestAMD64State
) % LibVEX_GUEST_STATE_ALIGN
);
1009 /* Zero out the initial state, and set up the simulated FPU in a sane
1011 LibVEX_GuestAMD64_initialise(&arch
->vex
);
1013 /* Zero out the shadow areas. */
1014 VG_(memset
)(&arch
->vex_shadow1
, 0, sizeof(VexGuestAMD64State
));
1015 VG_(memset
)(&arch
->vex_shadow2
, 0, sizeof(VexGuestAMD64State
));
1017 /* Put essential stuff into the new state. */
1018 arch
->vex
.guest_RSP
= iifii
.initial_client_SP
;
1019 arch
->vex
.guest_RIP
= iifii
.initial_client_IP
;
1020 arch
->vex
.guest_FS_CONST
= iifii
.initial_client_TP
;
1021 LibVEX_GuestAMD64_put_rflags(VKI_PSL_USER
, &arch
->vex
);
1024 # error "Unknown platform"
1027 /* Tell the tool that we just wrote to the registers. */
1028 VG_TRACK(post_reg_write
, Vg_CoreStartup
, 1/*tid*/, 0/*offset*/,
1029 sizeof(VexGuestArchState
));
1031 if (VG_(brk_base
) != -1 ) {
1032 /* Make inaccessible/unaddressable the end of the client data segment.
1033 See PRE(sys_brk) in syswrap-solaris.c for details. */
1034 VG_(track_client_dataseg
)(1 /* tid */);
1038 #endif // defined(VGO_solaris)
1040 /*--------------------------------------------------------------------*/
1042 /*--------------------------------------------------------------------*/