drd: Add a consistency check
[valgrind.git] / coregrind / m_libcproc.c
bloba371ce310ee64a20ac84f38f23cf17f76d683138
2 /*--------------------------------------------------------------------*/
3 /*--- Process-related libc stuff. m_libcproc.c ---*/
4 /*--------------------------------------------------------------------*/
6 /*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
10 Copyright (C) 2000-2013 Julian Seward
11 jseward@acm.org
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of the
16 License, or (at your option) any later version.
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 02111-1307, USA.
28 The GNU General Public License is contained in the file COPYING.
31 #include "pub_core_basics.h"
32 #include "pub_core_machine.h" // For VG_(machine_get_VexArchInfo)
33 #include "pub_core_vki.h"
34 #include "pub_core_vkiscnums.h"
35 #include "pub_core_libcbase.h"
36 #include "pub_core_libcassert.h"
37 #include "pub_core_libcprint.h"
38 #include "pub_core_libcproc.h"
39 #include "pub_core_libcsignal.h"
40 #include "pub_core_seqmatch.h"
41 #include "pub_core_mallocfree.h"
42 #include "pub_core_syscall.h"
43 #include "pub_core_xarray.h"
44 #include "pub_core_clientstate.h"
46 #if defined(VGO_darwin)
47 /* --- !!! --- EXTERNAL HEADERS start --- !!! --- */
48 #include <mach/mach.h> /* mach_thread_self */
49 /* --- !!! --- EXTERNAL HEADERS end --- !!! --- */
50 #endif
52 /* IMPORTANT: on Darwin it is essential to use the _nocancel versions
53 of syscalls rather than the vanilla version, if a _nocancel version
54 is available. See docs/internals/Darwin-notes.txt for the reason
55 why. */
57 /* ---------------------------------------------------------------------
58 Command line and environment stuff
59 ------------------------------------------------------------------ */
61 /* As deduced from sp_at_startup, the client's argc, argv[] and
62 envp[] as extracted from the client's stack at startup-time. */
63 HChar** VG_(client_envp) = NULL;
65 /* Path to library directory */
66 const HChar *VG_(libdir) = VG_LIBDIR;
68 const HChar *VG_(LD_PRELOAD_var_name) =
69 #if defined(VGO_linux)
70 "LD_PRELOAD";
71 #elif defined(VGO_darwin)
72 "DYLD_INSERT_LIBRARIES";
73 #else
74 # error Unknown OS
75 #endif
77 /* We do getenv without libc's help by snooping around in
78 VG_(client_envp) as determined at startup time. */
79 HChar *VG_(getenv)(const HChar *varname)
81 Int i, n;
82 vg_assert( VG_(client_envp) );
83 n = VG_(strlen)(varname);
84 for (i = 0; VG_(client_envp)[i] != NULL; i++) {
85 HChar* s = VG_(client_envp)[i];
86 if (VG_(strncmp)(varname, s, n) == 0 && s[n] == '=') {
87 return & s[n+1];
90 return NULL;
93 void VG_(env_unsetenv) ( HChar **env, const HChar *varname )
95 HChar **from, **to;
96 vg_assert(env);
97 vg_assert(varname);
98 to = NULL;
99 Int len = VG_(strlen)(varname);
101 for (from = to = env; from && *from; from++) {
102 if (!(VG_(strncmp)(varname, *from, len) == 0 && (*from)[len] == '=')) {
103 *to = *from;
104 to++;
107 *to = *from;
110 /* set the environment; returns the old env if a new one was allocated */
111 HChar **VG_(env_setenv) ( HChar ***envp, const HChar* varname,
112 const HChar *val )
114 HChar **env = (*envp);
115 HChar **cpp;
116 Int len = VG_(strlen)(varname);
117 HChar *valstr = VG_(malloc)("libcproc.es.1", len + VG_(strlen)(val) + 2);
118 HChar **oldenv = NULL;
120 VG_(sprintf)(valstr, "%s=%s", varname, val);
122 for (cpp = env; cpp && *cpp; cpp++) {
123 if (VG_(strncmp)(varname, *cpp, len) == 0 && (*cpp)[len] == '=') {
124 *cpp = valstr;
125 return oldenv;
129 if (env == NULL) {
130 env = VG_(malloc)("libcproc.es.2", sizeof(HChar *) * 2);
131 env[0] = valstr;
132 env[1] = NULL;
134 *envp = env;
136 } else {
137 Int envlen = (cpp-env) + 2;
138 HChar **newenv = VG_(malloc)("libcproc.es.3", envlen * sizeof(HChar *));
140 for (cpp = newenv; *env; )
141 *cpp++ = *env++;
142 *cpp++ = valstr;
143 *cpp++ = NULL;
145 oldenv = *envp;
147 *envp = newenv;
150 return oldenv;
154 /* Walk through a colon-separated environment variable, and remove the
155 entries which match remove_pattern. It slides everything down over
156 the removed entries, and pads the remaining space with '\0'. It
157 modifies the entries in place (in the client address space), but it
158 shouldn't matter too much, since we only do this just before an
159 execve().
161 This is also careful to mop up any excess ':'s, since empty strings
162 delimited by ':' are considered to be '.' in a path.
164 static void mash_colon_env(HChar *varp, const HChar *remove_pattern)
166 HChar *const start = varp;
167 HChar *entry_start = varp;
168 HChar *output = varp;
170 if (varp == NULL)
171 return;
173 while(*varp) {
174 if (*varp == ':') {
175 HChar prev;
176 Bool match;
178 /* This is a bit subtle: we want to match against the entry
179 we just copied, because it may have overlapped with
180 itself, junking the original. */
182 prev = *output;
183 *output = '\0';
185 match = VG_(string_match)(remove_pattern, entry_start);
187 *output = prev;
189 if (match) {
190 output = entry_start;
191 varp++; /* skip ':' after removed entry */
192 } else
193 entry_start = output+1; /* entry starts after ':' */
196 if (*varp)
197 *output++ = *varp++;
200 /* make sure last entry is nul terminated */
201 *output = '\0';
203 /* match against the last entry */
204 if (VG_(string_match)(remove_pattern, entry_start)) {
205 output = entry_start;
206 if (output > start) {
207 /* remove trailing ':' */
208 output--;
209 vg_assert(*output == ':');
213 /* pad out the left-overs with '\0' */
214 while(output < varp)
215 *output++ = '\0';
219 // Removes all the Valgrind-added stuff from the passed environment. Used
220 // when starting child processes, so they don't see that added stuff.
221 void VG_(env_remove_valgrind_env_stuff)(HChar** envp)
224 #if defined(VGO_darwin)
226 // Environment cleanup is also handled during parent launch
227 // in vg_preloaded.c:vg_cleanup_env().
229 #endif
231 Int i;
232 HChar* ld_preload_str = NULL;
233 HChar* ld_library_path_str = NULL;
234 HChar* dyld_insert_libraries_str = NULL;
235 HChar* buf;
237 // Find LD_* variables
238 // DDD: should probably conditionally compiled some of this:
239 // - LD_LIBRARY_PATH is universal?
240 // - LD_PRELOAD is on Linux, not on Darwin, not sure about AIX
241 // - DYLD_INSERT_LIBRARIES and DYLD_SHARED_REGION are Darwin-only
242 for (i = 0; envp[i] != NULL; i++) {
243 if (VG_(strncmp)(envp[i], "LD_PRELOAD=", 11) == 0) {
244 envp[i] = VG_(strdup)("libcproc.erves.1", envp[i]);
245 ld_preload_str = &envp[i][11];
247 if (VG_(strncmp)(envp[i], "LD_LIBRARY_PATH=", 16) == 0) {
248 envp[i] = VG_(strdup)("libcproc.erves.2", envp[i]);
249 ld_library_path_str = &envp[i][16];
251 if (VG_(strncmp)(envp[i], "DYLD_INSERT_LIBRARIES=", 22) == 0) {
252 envp[i] = VG_(strdup)("libcproc.erves.3", envp[i]);
253 dyld_insert_libraries_str = &envp[i][22];
257 buf = VG_(malloc)("libcproc.erves.4", VG_(strlen)(VG_(libdir)) + 20);
259 // Remove Valgrind-specific entries from LD_*.
260 VG_(sprintf)(buf, "%s*/vgpreload_*.so", VG_(libdir));
261 mash_colon_env(ld_preload_str, buf);
262 mash_colon_env(dyld_insert_libraries_str, buf);
263 VG_(sprintf)(buf, "%s*", VG_(libdir));
264 mash_colon_env(ld_library_path_str, buf);
266 // Remove VALGRIND_LAUNCHER variable.
267 VG_(env_unsetenv)(envp, VALGRIND_LAUNCHER);
269 // Remove DYLD_SHARED_REGION variable.
270 VG_(env_unsetenv)(envp, "DYLD_SHARED_REGION");
272 // XXX if variable becomes empty, remove it completely?
274 VG_(free)(buf);
277 /* ---------------------------------------------------------------------
278 Various important syscall wrappers
279 ------------------------------------------------------------------ */
281 Int VG_(waitpid)(Int pid, Int *status, Int options)
283 # if defined(VGO_linux)
284 SysRes res = VG_(do_syscall4)(__NR_wait4,
285 pid, (UWord)status, options, 0);
286 return sr_isError(res) ? -1 : sr_Res(res);
287 # elif defined(VGO_darwin)
288 SysRes res = VG_(do_syscall4)(__NR_wait4_nocancel,
289 pid, (UWord)status, options, 0);
290 return sr_isError(res) ? -1 : sr_Res(res);
291 # else
292 # error Unknown OS
293 # endif
296 /* clone the environment */
297 HChar **VG_(env_clone) ( HChar **oldenv )
299 HChar **oldenvp;
300 HChar **newenvp;
301 HChar **newenv;
302 Int envlen;
304 vg_assert(oldenv);
305 for (oldenvp = oldenv; oldenvp && *oldenvp; oldenvp++);
307 envlen = oldenvp - oldenv + 1;
309 newenv = VG_(malloc)("libcproc.ec.1", envlen * sizeof(HChar *));
311 oldenvp = oldenv;
312 newenvp = newenv;
314 while (oldenvp && *oldenvp) {
315 *newenvp++ = *oldenvp++;
318 *newenvp = *oldenvp;
320 return newenv;
323 void VG_(execv) ( const HChar* filename, HChar** argv )
325 HChar** envp;
326 SysRes res;
328 /* restore the DATA rlimit for the child */
329 VG_(setrlimit)(VKI_RLIMIT_DATA, &VG_(client_rlimit_data));
331 envp = VG_(env_clone)(VG_(client_envp));
332 VG_(env_remove_valgrind_env_stuff)( envp );
334 res = VG_(do_syscall3)(__NR_execve,
335 (UWord)filename, (UWord)argv, (UWord)envp);
337 VG_(printf)("EXEC failed, errno = %lld\n", (Long)sr_Err(res));
340 /* Return -1 if error, else 0. NOTE does not indicate return code of
341 child! */
342 Int VG_(system) ( const HChar* cmd )
344 Int pid;
345 if (cmd == NULL)
346 return 1;
347 pid = VG_(fork)();
348 if (pid < 0)
349 return -1;
350 if (pid == 0) {
351 /* child */
352 const HChar* argv[4] = { "/bin/sh", "-c", cmd, 0 };
353 VG_(execv)(argv[0], CONST_CAST(HChar **,argv));
355 /* If we're still alive here, execv failed. */
356 VG_(exit)(1);
357 } else {
358 /* parent */
359 /* We have to set SIGCHLD to its default behaviour in order that
360 VG_(waitpid) works (at least on AIX). According to the Linux
361 man page for waitpid:
363 POSIX.1-2001 specifies that if the disposition of SIGCHLD is
364 set to SIG_IGN or the SA_NOCLDWAIT flag is set for SIGCHLD
365 (see sigaction(2)), then children that terminate do not
366 become zombies and a call to wait() or waitpid() will block
367 until all children have terminated, and then fail with errno
368 set to ECHILD. (The original POSIX standard left the
369 behaviour of setting SIGCHLD to SIG_IGN unspecified.)
371 Int ir, zzz;
372 vki_sigaction_toK_t sa, sa2;
373 vki_sigaction_fromK_t saved_sa;
374 VG_(memset)( &sa, 0, sizeof(sa) );
375 VG_(sigemptyset)(&sa.sa_mask);
376 sa.ksa_handler = VKI_SIG_DFL;
377 sa.sa_flags = 0;
378 ir = VG_(sigaction)(VKI_SIGCHLD, &sa, &saved_sa);
379 vg_assert(ir == 0);
381 zzz = VG_(waitpid)(pid, NULL, 0);
383 VG_(convert_sigaction_fromK_to_toK)( &saved_sa, &sa2 );
384 ir = VG_(sigaction)(VKI_SIGCHLD, &sa2, NULL);
385 vg_assert(ir == 0);
386 return zzz == -1 ? -1 : 0;
390 /* ---------------------------------------------------------------------
391 Resource limits
392 ------------------------------------------------------------------ */
394 /* Support for getrlimit. */
395 Int VG_(getrlimit) (Int resource, struct vki_rlimit *rlim)
397 SysRes res = VG_(mk_SysRes_Error)(VKI_ENOSYS);
398 /* res = getrlimit( resource, rlim ); */
399 # ifdef __NR_ugetrlimit
400 res = VG_(do_syscall2)(__NR_ugetrlimit, resource, (UWord)rlim);
401 # endif
402 if (sr_isError(res) && sr_Err(res) == VKI_ENOSYS)
403 res = VG_(do_syscall2)(__NR_getrlimit, resource, (UWord)rlim);
404 return sr_isError(res) ? -1 : sr_Res(res);
408 /* Support for setrlimit. */
409 Int VG_(setrlimit) (Int resource, const struct vki_rlimit *rlim)
411 SysRes res;
412 /* res = setrlimit( resource, rlim ); */
413 res = VG_(do_syscall2)(__NR_setrlimit, resource, (UWord)rlim);
414 return sr_isError(res) ? -1 : sr_Res(res);
417 /* Support for prctl. */
418 Int VG_(prctl) (Int option,
419 ULong arg2, ULong arg3, ULong arg4, ULong arg5)
421 SysRes res = VG_(mk_SysRes_Error)(VKI_ENOSYS);
422 # if defined(VGO_linux)
423 /* res = prctl( option, arg2, arg3, arg4, arg5 ); */
424 res = VG_(do_syscall5)(__NR_prctl, (UWord) option,
425 (UWord) arg2, (UWord) arg3, (UWord) arg4,
426 (UWord) arg5);
427 # endif
429 return sr_isError(res) ? -1 : sr_Res(res);
432 /* ---------------------------------------------------------------------
433 pids, etc
434 ------------------------------------------------------------------ */
436 Int VG_(gettid)(void)
438 # if defined(VGO_linux)
439 SysRes res = VG_(do_syscall0)(__NR_gettid);
441 if (sr_isError(res) && sr_Res(res) == VKI_ENOSYS) {
442 HChar pid[16];
444 * The gettid system call does not exist. The obvious assumption
445 * to make at this point would be that we are running on an older
446 * system where the getpid system call actually returns the ID of
447 * the current thread.
449 * Unfortunately it seems that there are some systems with a kernel
450 * where getpid has been changed to return the ID of the thread group
451 * leader but where the gettid system call has not yet been added.
453 * So instead of calling getpid here we use readlink to see where
454 * the /proc/self link is pointing...
457 # if defined(VGP_arm64_linux)
458 res = VG_(do_syscall4)(__NR_readlinkat, VKI_AT_FDCWD,
459 (UWord)"/proc/self",
460 (UWord)pid, sizeof(pid));
461 # else
462 res = VG_(do_syscall3)(__NR_readlink, (UWord)"/proc/self",
463 (UWord)pid, sizeof(pid));
464 # endif
465 if (!sr_isError(res) && sr_Res(res) > 0) {
466 HChar* s;
467 pid[sr_Res(res)] = '\0';
468 res = VG_(mk_SysRes_Success)( VG_(strtoll10)(pid, &s) );
469 if (*s != '\0') {
470 VG_(message)(Vg_DebugMsg,
471 "Warning: invalid file name linked to by /proc/self: %s\n",
472 pid);
477 return sr_Res(res);
479 # elif defined(VGO_darwin)
480 // Darwin's gettid syscall is something else.
481 // Use Mach thread ports for lwpid instead.
482 return mach_thread_self();
484 # else
485 # error "Unknown OS"
486 # endif
489 /* You'd be amazed how many places need to know the current pid. */
490 Int VG_(getpid) ( void )
492 /* ASSUMES SYSCALL ALWAYS SUCCEEDS */
493 return sr_Res( VG_(do_syscall0)(__NR_getpid) );
496 Int VG_(getpgrp) ( void )
498 /* ASSUMES SYSCALL ALWAYS SUCCEEDS */
499 return sr_Res( VG_(do_syscall0)(__NR_getpgrp) );
502 Int VG_(getppid) ( void )
504 /* ASSUMES SYSCALL ALWAYS SUCCEEDS */
505 return sr_Res( VG_(do_syscall0)(__NR_getppid) );
508 Int VG_(geteuid) ( void )
510 /* ASSUMES SYSCALL ALWAYS SUCCEEDS */
511 # if defined(__NR_geteuid32)
512 // We use the 32-bit version if it's supported. Otherwise, IDs greater
513 // than 65536 cause problems, as bug #151209 showed.
514 return sr_Res( VG_(do_syscall0)(__NR_geteuid32) );
515 # else
516 return sr_Res( VG_(do_syscall0)(__NR_geteuid) );
517 # endif
520 Int VG_(getegid) ( void )
522 /* ASSUMES SYSCALL ALWAYS SUCCEEDS */
523 # if defined(__NR_getegid32)
524 // We use the 32-bit version if it's supported. Otherwise, IDs greater
525 // than 65536 cause problems, as bug #151209 showed.
526 return sr_Res( VG_(do_syscall0)(__NR_getegid32) );
527 # else
528 return sr_Res( VG_(do_syscall0)(__NR_getegid) );
529 # endif
532 /* Get supplementary groups into list[0 .. size-1]. Returns the
533 number of groups written, or -1 if error. Note that in order to be
534 portable, the groups are 32-bit unsigned ints regardless of the
535 platform.
536 As a special case, if size == 0 the function returns the number of
537 groups leaving list untouched. */
538 Int VG_(getgroups)( Int size, UInt* list )
540 if (size < 0) return -1;
542 # if defined(VGP_x86_linux) || defined(VGP_ppc32_linux) \
543 || defined(VGP_mips64_linux)
544 Int i;
545 SysRes sres;
546 UShort list16[size];
547 sres = VG_(do_syscall2)(__NR_getgroups, size, (Addr)list16);
548 if (sr_isError(sres))
549 return -1;
550 if (size != 0) {
551 for (i = 0; i < sr_Res(sres); i++)
552 list[i] = (UInt)list16[i];
554 return sr_Res(sres);
556 # elif defined(VGP_amd64_linux) || defined(VGP_arm_linux) \
557 || defined(VGP_ppc64be_linux) || defined(VGP_ppc64le_linux) \
558 || defined(VGO_darwin) || defined(VGP_s390x_linux) \
559 || defined(VGP_mips32_linux) || defined(VGP_arm64_linux)
560 SysRes sres;
561 sres = VG_(do_syscall2)(__NR_getgroups, size, (Addr)list);
562 if (sr_isError(sres))
563 return -1;
564 return sr_Res(sres);
566 # else
567 # error "VG_(getgroups): needs implementation on this platform"
568 # endif
571 /* ---------------------------------------------------------------------
572 Process tracing
573 ------------------------------------------------------------------ */
575 Int VG_(ptrace) ( Int request, Int pid, void *addr, void *data )
577 SysRes res;
578 res = VG_(do_syscall4)(__NR_ptrace, request, pid, (UWord)addr, (UWord)data);
579 if (sr_isError(res))
580 return -1;
581 return sr_Res(res);
584 /* ---------------------------------------------------------------------
585 Fork
586 ------------------------------------------------------------------ */
588 Int VG_(fork) ( void )
590 # if defined(VGP_arm64_linux)
591 SysRes res;
592 res = VG_(do_syscall5)(__NR_clone, VKI_SIGCHLD,
593 (UWord)NULL, (UWord)NULL, (UWord)NULL, (UWord)NULL);
594 if (sr_isError(res))
595 return -1;
596 return sr_Res(res);
598 # elif defined(VGO_linux)
599 SysRes res;
600 res = VG_(do_syscall0)(__NR_fork);
601 if (sr_isError(res))
602 return -1;
603 return sr_Res(res);
605 # elif defined(VGO_darwin)
606 SysRes res;
607 res = VG_(do_syscall0)(__NR_fork); /* __NR_fork is UX64 */
608 if (sr_isError(res))
609 return -1;
610 /* on success: wLO = child pid; wHI = 1 for child, 0 for parent */
611 if (sr_ResHI(res) != 0) {
612 return 0; /* this is child: return 0 instead of child pid */
614 return sr_Res(res);
616 # else
617 # error "Unknown OS"
618 # endif
621 /* ---------------------------------------------------------------------
622 Timing stuff
623 ------------------------------------------------------------------ */
625 UInt VG_(read_millisecond_timer) ( void )
627 /* 'now' and 'base' are in microseconds */
628 static ULong base = 0;
629 ULong now;
631 # if defined(VGO_linux)
632 { SysRes res;
633 struct vki_timespec ts_now;
634 res = VG_(do_syscall2)(__NR_clock_gettime, VKI_CLOCK_MONOTONIC,
635 (UWord)&ts_now);
636 if (sr_isError(res) == 0) {
637 now = ts_now.tv_sec * 1000000ULL + ts_now.tv_nsec / 1000;
638 } else {
639 struct vki_timeval tv_now;
640 res = VG_(do_syscall2)(__NR_gettimeofday, (UWord)&tv_now, (UWord)NULL);
641 vg_assert(! sr_isError(res));
642 now = tv_now.tv_sec * 1000000ULL + tv_now.tv_usec;
646 # elif defined(VGO_darwin)
647 // Weird: it seems that gettimeofday() doesn't fill in the timeval, but
648 // rather returns the tv_sec as the low 32 bits of the result and the
649 // tv_usec as the high 32 bits of the result. (But the timeval cannot be
650 // NULL!) See bug 200990.
651 { SysRes res;
652 struct vki_timeval tv_now = { 0, 0 };
653 res = VG_(do_syscall2)(__NR_gettimeofday, (UWord)&tv_now, (UWord)NULL);
654 vg_assert(! sr_isError(res));
655 now = sr_Res(res) * 1000000ULL + sr_ResHI(res);
658 # else
659 # error "Unknown OS"
660 # endif
662 /* COMMON CODE */
663 if (base == 0)
664 base = now;
666 return (now - base) / 1000;
670 /* ---------------------------------------------------------------------
671 atfork()
672 ------------------------------------------------------------------ */
674 struct atfork {
675 vg_atfork_t pre;
676 vg_atfork_t parent;
677 vg_atfork_t child;
680 #define VG_MAX_ATFORK 10
682 static struct atfork atforks[VG_MAX_ATFORK];
683 static Int n_atfork = 0;
685 void VG_(atfork)(vg_atfork_t pre, vg_atfork_t parent, vg_atfork_t child)
687 Int i;
689 for (i = 0; i < n_atfork; i++) {
690 if (atforks[i].pre == pre &&
691 atforks[i].parent == parent &&
692 atforks[i].child == child)
693 return;
696 if (n_atfork >= VG_MAX_ATFORK)
697 VG_(core_panic)(
698 "Too many VG_(atfork) handlers requested: raise VG_MAX_ATFORK");
700 atforks[n_atfork].pre = pre;
701 atforks[n_atfork].parent = parent;
702 atforks[n_atfork].child = child;
704 n_atfork++;
707 void VG_(do_atfork_pre)(ThreadId tid)
709 Int i;
711 for (i = 0; i < n_atfork; i++)
712 if (atforks[i].pre != NULL)
713 (*atforks[i].pre)(tid);
716 void VG_(do_atfork_parent)(ThreadId tid)
718 Int i;
720 for (i = 0; i < n_atfork; i++)
721 if (atforks[i].parent != NULL)
722 (*atforks[i].parent)(tid);
725 void VG_(do_atfork_child)(ThreadId tid)
727 Int i;
729 for (i = 0; i < n_atfork; i++)
730 if (atforks[i].child != NULL)
731 (*atforks[i].child)(tid);
735 /* ---------------------------------------------------------------------
736 icache invalidation
737 ------------------------------------------------------------------ */
739 void VG_(invalidate_icache) ( void *ptr, SizeT nbytes )
741 if (nbytes == 0) return; // nothing to do
743 // Get cache info
744 VexArchInfo vai;
745 VG_(machine_get_VexArchInfo)(NULL, &vai);
747 // If I-caches are coherent, nothing needs to be done here
748 if (vai.hwcache_info.icaches_maintain_coherence) return;
750 # if defined(VGA_ppc32) || defined(VGA_ppc64be) || defined(VGA_ppc64le)
751 Addr startaddr = (Addr) ptr;
752 Addr endaddr = startaddr + nbytes;
753 Addr cls;
754 Addr addr;
756 VG_(machine_get_VexArchInfo)( NULL, &vai );
757 cls = vai.ppc_icache_line_szB;
759 /* Stay sane .. */
760 vg_assert(cls == 16 || cls == 32 || cls == 64 || cls == 128);
762 startaddr &= ~(cls - 1);
763 for (addr = startaddr; addr < endaddr; addr += cls) {
764 __asm__ __volatile__("dcbst 0,%0" : : "r" (addr));
766 __asm__ __volatile__("sync");
767 for (addr = startaddr; addr < endaddr; addr += cls) {
768 __asm__ __volatile__("icbi 0,%0" : : "r" (addr));
770 __asm__ __volatile__("sync; isync");
772 # elif defined(VGP_arm_linux)
773 /* ARM cache flushes are privileged, so we must defer to the kernel. */
774 Addr startaddr = (Addr) ptr;
775 Addr endaddr = startaddr + nbytes;
776 VG_(do_syscall2)(__NR_ARM_cacheflush, startaddr, endaddr);
778 # elif defined(VGP_arm64_linux)
779 // This arm64_linux section of this function VG_(invalidate_icache)
780 // is copied from
781 // https://github.com/armvixl/vixl/blob/master/src/a64/cpu-a64.cc
782 // which has the following copyright notice:
784 Copyright 2013, ARM Limited
785 All rights reserved.
787 Redistribution and use in source and binary forms, with or without
788 modification, are permitted provided that the following conditions are met:
790 * Redistributions of source code must retain the above copyright notice,
791 this list of conditions and the following disclaimer.
792 * Redistributions in binary form must reproduce the above copyright notice,
793 this list of conditions and the following disclaimer in the documentation
794 and/or other materials provided with the distribution.
795 * Neither the name of ARM Limited nor the names of its contributors may be
796 used to endorse or promote products derived from this software without
797 specific prior written permission.
799 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
800 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
801 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
802 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
803 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
804 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
805 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
806 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
807 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
808 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
811 // Ask what the I and D line sizes are
812 UInt cache_type_register;
813 // Copy the content of the cache type register to a core register.
814 __asm__ __volatile__ ("mrs %[ctr], ctr_el0" // NOLINT
815 : [ctr] "=r" (cache_type_register));
817 const Int kDCacheLineSizeShift = 16;
818 const Int kICacheLineSizeShift = 0;
819 const UInt kDCacheLineSizeMask = 0xf << kDCacheLineSizeShift;
820 const UInt kICacheLineSizeMask = 0xf << kICacheLineSizeShift;
822 // The cache type register holds the size of the I and D caches as a power of
823 // two.
824 const UInt dcache_line_size_power_of_two =
825 (cache_type_register & kDCacheLineSizeMask) >> kDCacheLineSizeShift;
826 const UInt icache_line_size_power_of_two =
827 (cache_type_register & kICacheLineSizeMask) >> kICacheLineSizeShift;
829 const UInt dcache_line_size_ = 4 * (1 << dcache_line_size_power_of_two);
830 const UInt icache_line_size_ = 4 * (1 << icache_line_size_power_of_two);
832 Addr start = (Addr)ptr;
833 // Sizes will be used to generate a mask big enough to cover a pointer.
834 Addr dsize = (Addr)dcache_line_size_;
835 Addr isize = (Addr)icache_line_size_;
837 // Cache line sizes are always a power of 2.
838 Addr dstart = start & ~(dsize - 1);
839 Addr istart = start & ~(isize - 1);
840 Addr end = start + nbytes;
842 __asm__ __volatile__ (
843 // Clean every line of the D cache containing the target data.
844 "0: \n\t"
845 // dc : Data Cache maintenance
846 // c : Clean
847 // va : by (Virtual) Address
848 // u : to the point of Unification
849 // The point of unification for a processor is the point by which the
850 // instruction and data caches are guaranteed to see the same copy of a
851 // memory location. See ARM DDI 0406B page B2-12 for more information.
852 "dc cvau, %[dline] \n\t"
853 "add %[dline], %[dline], %[dsize] \n\t"
854 "cmp %[dline], %[end] \n\t"
855 "b.lt 0b \n\t"
856 // Barrier to make sure the effect of the code above is visible to the rest
857 // of the world.
858 // dsb : Data Synchronisation Barrier
859 // ish : Inner SHareable domain
860 // The point of unification for an Inner Shareable shareability domain is
861 // the point by which the instruction and data caches of all the processors
862 // in that Inner Shareable shareability domain are guaranteed to see the
863 // same copy of a memory location. See ARM DDI 0406B page B2-12 for more
864 // information.
865 "dsb ish \n\t"
866 // Invalidate every line of the I cache containing the target data.
867 "1: \n\t"
868 // ic : instruction cache maintenance
869 // i : invalidate
870 // va : by address
871 // u : to the point of unification
872 "ic ivau, %[iline] \n\t"
873 "add %[iline], %[iline], %[isize] \n\t"
874 "cmp %[iline], %[end] \n\t"
875 "b.lt 1b \n\t"
876 // Barrier to make sure the effect of the code above is visible to the rest
877 // of the world.
878 "dsb ish \n\t"
879 // Barrier to ensure any prefetching which happened before this code is
880 // discarded.
881 // isb : Instruction Synchronisation Barrier
882 "isb \n\t"
883 : [dline] "+r" (dstart),
884 [iline] "+r" (istart)
885 : [dsize] "r" (dsize),
886 [isize] "r" (isize),
887 [end] "r" (end)
888 // This code does not write to memory but without the dependency gcc might
889 // move this code before the code is generated.
890 : "cc", "memory"
893 # elif defined(VGA_mips32) || defined(VGA_mips64)
894 SysRes sres = VG_(do_syscall3)(__NR_cacheflush, (UWord) ptr,
895 (UWord) nbytes, (UWord) 3);
896 vg_assert( sres._isError == 0 );
898 # endif
902 /* ---------------------------------------------------------------------
903 dcache flushing
904 ------------------------------------------------------------------ */
906 void VG_(flush_dcache) ( void *ptr, SizeT nbytes )
908 /* Currently this is only required on ARM64. */
909 # if defined(VGA_arm64)
910 Addr startaddr = (Addr) ptr;
911 Addr endaddr = startaddr + nbytes;
912 Addr cls;
913 Addr addr;
915 ULong ctr_el0;
916 __asm__ __volatile__ ("mrs %0, ctr_el0" : "=r"(ctr_el0));
917 cls = 4 * (1ULL << (0xF & (ctr_el0 >> 16)));
919 /* Stay sane .. */
920 vg_assert(cls == 64);
922 startaddr &= ~(cls - 1);
923 for (addr = startaddr; addr < endaddr; addr += cls) {
924 __asm__ __volatile__("dc cvau, %0" : : "r" (addr));
926 __asm__ __volatile__("dsb ish");
927 # endif
930 /*--------------------------------------------------------------------*/
931 /*--- end ---*/
932 /*--------------------------------------------------------------------*/