Darwin: Make stack growth tracking consistent with other architectures
[valgrind.git] / coregrind / m_libcproc.c
blob045a37870cb533faacc5504357e4198aa42ae658
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-2012 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_(arena_malloc)(VG_AR_CORE, "libcproc.es.1",
118 len + VG_(strlen)(val) + 2);
119 HChar **oldenv = NULL;
121 VG_(sprintf)(valstr, "%s=%s", varname, val);
123 for (cpp = env; cpp && *cpp; cpp++) {
124 if (VG_(strncmp)(varname, *cpp, len) == 0 && (*cpp)[len] == '=') {
125 *cpp = valstr;
126 return oldenv;
130 if (env == NULL) {
131 env = VG_(arena_malloc)(VG_AR_CORE, "libcproc.es.2", sizeof(HChar *) * 2);
132 env[0] = valstr;
133 env[1] = NULL;
135 *envp = env;
137 } else {
138 Int envlen = (cpp-env) + 2;
139 HChar **newenv = VG_(arena_malloc)(VG_AR_CORE, "libcproc.es.3",
140 envlen * sizeof(HChar *));
142 for (cpp = newenv; *env; )
143 *cpp++ = *env++;
144 *cpp++ = valstr;
145 *cpp++ = NULL;
147 oldenv = *envp;
149 *envp = newenv;
152 return oldenv;
156 /* Walk through a colon-separated environment variable, and remove the
157 entries which match remove_pattern. It slides everything down over
158 the removed entries, and pads the remaining space with '\0'. It
159 modifies the entries in place (in the client address space), but it
160 shouldn't matter too much, since we only do this just before an
161 execve().
163 This is also careful to mop up any excess ':'s, since empty strings
164 delimited by ':' are considered to be '.' in a path.
166 static void mash_colon_env(HChar *varp, const HChar *remove_pattern)
168 HChar *const start = varp;
169 HChar *entry_start = varp;
170 HChar *output = varp;
172 if (varp == NULL)
173 return;
175 while(*varp) {
176 if (*varp == ':') {
177 HChar prev;
178 Bool match;
180 /* This is a bit subtle: we want to match against the entry
181 we just copied, because it may have overlapped with
182 itself, junking the original. */
184 prev = *output;
185 *output = '\0';
187 match = VG_(string_match)(remove_pattern, entry_start);
189 *output = prev;
191 if (match) {
192 output = entry_start;
193 varp++; /* skip ':' after removed entry */
194 } else
195 entry_start = output+1; /* entry starts after ':' */
198 if (*varp)
199 *output++ = *varp++;
202 /* make sure last entry is nul terminated */
203 *output = '\0';
205 /* match against the last entry */
206 if (VG_(string_match)(remove_pattern, entry_start)) {
207 output = entry_start;
208 if (output > start) {
209 /* remove trailing ':' */
210 output--;
211 vg_assert(*output == ':');
215 /* pad out the left-overs with '\0' */
216 while(output < varp)
217 *output++ = '\0';
221 // Removes all the Valgrind-added stuff from the passed environment. Used
222 // when starting child processes, so they don't see that added stuff.
223 void VG_(env_remove_valgrind_env_stuff)(HChar** envp)
226 #if defined(VGO_darwin)
228 // Environment cleanup is also handled during parent launch
229 // in vg_preloaded.c:vg_cleanup_env().
231 #endif
233 Int i;
234 HChar* ld_preload_str = NULL;
235 HChar* ld_library_path_str = NULL;
236 HChar* dyld_insert_libraries_str = NULL;
237 HChar* buf;
239 // Find LD_* variables
240 // DDD: should probably conditionally compiled some of this:
241 // - LD_LIBRARY_PATH is universal?
242 // - LD_PRELOAD is on Linux, not on Darwin, not sure about AIX
243 // - DYLD_INSERT_LIBRARIES and DYLD_SHARED_REGION are Darwin-only
244 for (i = 0; envp[i] != NULL; i++) {
245 if (VG_(strncmp)(envp[i], "LD_PRELOAD=", 11) == 0) {
246 envp[i] = VG_(arena_strdup)(VG_AR_CORE, "libcproc.erves.1", envp[i]);
247 ld_preload_str = &envp[i][11];
249 if (VG_(strncmp)(envp[i], "LD_LIBRARY_PATH=", 16) == 0) {
250 envp[i] = VG_(arena_strdup)(VG_AR_CORE, "libcproc.erves.2", envp[i]);
251 ld_library_path_str = &envp[i][16];
253 if (VG_(strncmp)(envp[i], "DYLD_INSERT_LIBRARIES=", 22) == 0) {
254 envp[i] = VG_(arena_strdup)(VG_AR_CORE, "libcproc.erves.3", envp[i]);
255 dyld_insert_libraries_str = &envp[i][22];
259 buf = VG_(arena_malloc)(VG_AR_CORE, "libcproc.erves.4",
260 VG_(strlen)(VG_(libdir)) + 20);
262 // Remove Valgrind-specific entries from LD_*.
263 VG_(sprintf)(buf, "%s*/vgpreload_*.so", VG_(libdir));
264 mash_colon_env(ld_preload_str, buf);
265 mash_colon_env(dyld_insert_libraries_str, buf);
266 VG_(sprintf)(buf, "%s*", VG_(libdir));
267 mash_colon_env(ld_library_path_str, buf);
269 // Remove VALGRIND_LAUNCHER variable.
270 VG_(env_unsetenv)(envp, VALGRIND_LAUNCHER);
272 // Remove DYLD_SHARED_REGION variable.
273 VG_(env_unsetenv)(envp, "DYLD_SHARED_REGION");
275 // XXX if variable becomes empty, remove it completely?
277 VG_(arena_free)(VG_AR_CORE, buf);
280 /* ---------------------------------------------------------------------
281 Various important syscall wrappers
282 ------------------------------------------------------------------ */
284 Int VG_(waitpid)(Int pid, Int *status, Int options)
286 # if defined(VGO_linux)
287 SysRes res = VG_(do_syscall4)(__NR_wait4,
288 pid, (UWord)status, options, 0);
289 return sr_isError(res) ? -1 : sr_Res(res);
290 # elif defined(VGO_darwin)
291 SysRes res = VG_(do_syscall4)(__NR_wait4_nocancel,
292 pid, (UWord)status, options, 0);
293 return sr_isError(res) ? -1 : sr_Res(res);
294 # else
295 # error Unknown OS
296 # endif
299 /* clone the environment */
300 HChar **VG_(env_clone) ( HChar **oldenv )
302 HChar **oldenvp;
303 HChar **newenvp;
304 HChar **newenv;
305 Int envlen;
307 vg_assert(oldenv);
308 for (oldenvp = oldenv; oldenvp && *oldenvp; oldenvp++);
310 envlen = oldenvp - oldenv + 1;
312 newenv = VG_(arena_malloc)(VG_AR_CORE, "libcproc.ec.1",
313 envlen * sizeof(HChar *));
315 oldenvp = oldenv;
316 newenvp = newenv;
318 while (oldenvp && *oldenvp) {
319 *newenvp++ = *oldenvp++;
322 *newenvp = *oldenvp;
324 return newenv;
327 void VG_(execv) ( const HChar* filename, HChar** argv )
329 HChar** envp;
330 SysRes res;
332 /* restore the DATA rlimit for the child */
333 VG_(setrlimit)(VKI_RLIMIT_DATA, &VG_(client_rlimit_data));
335 envp = VG_(env_clone)(VG_(client_envp));
336 VG_(env_remove_valgrind_env_stuff)( envp );
338 res = VG_(do_syscall3)(__NR_execve,
339 (UWord)filename, (UWord)argv, (UWord)envp);
341 VG_(printf)("EXEC failed, errno = %lld\n", (Long)sr_Err(res));
344 /* Return -1 if error, else 0. NOTE does not indicate return code of
345 child! */
346 Int VG_(system) ( const HChar* cmd )
348 Int pid;
349 if (cmd == NULL)
350 return 1;
351 pid = VG_(fork)();
352 if (pid < 0)
353 return -1;
354 if (pid == 0) {
355 /* child */
356 const HChar* argv[4] = { "/bin/sh", "-c", cmd, 0 };
357 VG_(execv)(argv[0], (HChar **)argv);
359 /* If we're still alive here, execv failed. */
360 VG_(exit)(1);
361 } else {
362 /* parent */
363 /* We have to set SIGCHLD to its default behaviour in order that
364 VG_(waitpid) works (at least on AIX). According to the Linux
365 man page for waitpid:
367 POSIX.1-2001 specifies that if the disposition of SIGCHLD is
368 set to SIG_IGN or the SA_NOCLDWAIT flag is set for SIGCHLD
369 (see sigaction(2)), then children that terminate do not
370 become zombies and a call to wait() or waitpid() will block
371 until all children have terminated, and then fail with errno
372 set to ECHILD. (The original POSIX standard left the
373 behaviour of setting SIGCHLD to SIG_IGN unspecified.)
375 Int ir, zzz;
376 vki_sigaction_toK_t sa, sa2;
377 vki_sigaction_fromK_t saved_sa;
378 VG_(memset)( &sa, 0, sizeof(sa) );
379 VG_(sigemptyset)(&sa.sa_mask);
380 sa.ksa_handler = VKI_SIG_DFL;
381 sa.sa_flags = 0;
382 ir = VG_(sigaction)(VKI_SIGCHLD, &sa, &saved_sa);
383 vg_assert(ir == 0);
385 zzz = VG_(waitpid)(pid, NULL, 0);
387 VG_(convert_sigaction_fromK_to_toK)( &saved_sa, &sa2 );
388 ir = VG_(sigaction)(VKI_SIGCHLD, &sa2, NULL);
389 vg_assert(ir == 0);
390 return zzz == -1 ? -1 : 0;
394 /* ---------------------------------------------------------------------
395 Resource limits
396 ------------------------------------------------------------------ */
398 /* Support for getrlimit. */
399 Int VG_(getrlimit) (Int resource, struct vki_rlimit *rlim)
401 SysRes res = VG_(mk_SysRes_Error)(VKI_ENOSYS);
402 /* res = getrlimit( resource, rlim ); */
403 # ifdef __NR_ugetrlimit
404 res = VG_(do_syscall2)(__NR_ugetrlimit, resource, (UWord)rlim);
405 # endif
406 if (sr_isError(res) && sr_Err(res) == VKI_ENOSYS)
407 res = VG_(do_syscall2)(__NR_getrlimit, resource, (UWord)rlim);
408 return sr_isError(res) ? -1 : sr_Res(res);
412 /* Support for setrlimit. */
413 Int VG_(setrlimit) (Int resource, const struct vki_rlimit *rlim)
415 SysRes res;
416 /* res = setrlimit( resource, rlim ); */
417 res = VG_(do_syscall2)(__NR_setrlimit, resource, (UWord)rlim);
418 return sr_isError(res) ? -1 : sr_Res(res);
421 /* Support for prctl. */
422 Int VG_(prctl) (Int option,
423 ULong arg2, ULong arg3, ULong arg4, ULong arg5)
425 SysRes res = VG_(mk_SysRes_Error)(VKI_ENOSYS);
426 # if defined(VGO_linux)
427 /* res = prctl( option, arg2, arg3, arg4, arg5 ); */
428 res = VG_(do_syscall5)(__NR_prctl, (UWord) option,
429 (UWord) arg2, (UWord) arg3, (UWord) arg4,
430 (UWord) arg5);
431 # endif
433 return sr_isError(res) ? -1 : sr_Res(res);
436 /* ---------------------------------------------------------------------
437 pids, etc
438 ------------------------------------------------------------------ */
440 Int VG_(gettid)(void)
442 # if defined(VGO_linux)
443 SysRes res = VG_(do_syscall0)(__NR_gettid);
445 if (sr_isError(res) && sr_Res(res) == VKI_ENOSYS) {
446 HChar pid[16];
448 * The gettid system call does not exist. The obvious assumption
449 * to make at this point would be that we are running on an older
450 * system where the getpid system call actually returns the ID of
451 * the current thread.
453 * Unfortunately it seems that there are some systems with a kernel
454 * where getpid has been changed to return the ID of the thread group
455 * leader but where the gettid system call has not yet been added.
457 * So instead of calling getpid here we use readlink to see where
458 * the /proc/self link is pointing...
461 res = VG_(do_syscall3)(__NR_readlink, (UWord)"/proc/self",
462 (UWord)pid, sizeof(pid));
463 if (!sr_isError(res) && sr_Res(res) > 0) {
464 HChar* s;
465 pid[sr_Res(res)] = '\0';
466 res = VG_(mk_SysRes_Success)( VG_(strtoll10)(pid, &s) );
467 if (*s != '\0') {
468 VG_(message)(Vg_DebugMsg,
469 "Warning: invalid file name linked to by /proc/self: %s\n",
470 pid);
475 return sr_Res(res);
477 # elif defined(VGO_darwin)
478 // Darwin's gettid syscall is something else.
479 // Use Mach thread ports for lwpid instead.
480 return mach_thread_self();
482 # else
483 # error "Unknown OS"
484 # endif
487 /* You'd be amazed how many places need to know the current pid. */
488 Int VG_(getpid) ( void )
490 /* ASSUMES SYSCALL ALWAYS SUCCEEDS */
491 return sr_Res( VG_(do_syscall0)(__NR_getpid) );
494 Int VG_(getpgrp) ( void )
496 /* ASSUMES SYSCALL ALWAYS SUCCEEDS */
497 return sr_Res( VG_(do_syscall0)(__NR_getpgrp) );
500 Int VG_(getppid) ( void )
502 /* ASSUMES SYSCALL ALWAYS SUCCEEDS */
503 return sr_Res( VG_(do_syscall0)(__NR_getppid) );
506 Int VG_(geteuid) ( void )
508 /* ASSUMES SYSCALL ALWAYS SUCCEEDS */
509 # if defined(__NR_geteuid32)
510 // We use the 32-bit version if it's supported. Otherwise, IDs greater
511 // than 65536 cause problems, as bug #151209 showed.
512 return sr_Res( VG_(do_syscall0)(__NR_geteuid32) );
513 # else
514 return sr_Res( VG_(do_syscall0)(__NR_geteuid) );
515 # endif
518 Int VG_(getegid) ( void )
520 /* ASSUMES SYSCALL ALWAYS SUCCEEDS */
521 # if defined(__NR_getegid32)
522 // We use the 32-bit version if it's supported. Otherwise, IDs greater
523 // than 65536 cause problems, as bug #151209 showed.
524 return sr_Res( VG_(do_syscall0)(__NR_getegid32) );
525 # else
526 return sr_Res( VG_(do_syscall0)(__NR_getegid) );
527 # endif
530 /* Get supplementary groups into list[0 .. size-1]. Returns the
531 number of groups written, or -1 if error. Note that in order to be
532 portable, the groups are 32-bit unsigned ints regardless of the
533 platform. */
534 Int VG_(getgroups)( Int size, UInt* list )
536 # if defined(VGP_x86_linux) || defined(VGP_ppc32_linux) \
537 || defined(VGP_mips64_linux)
538 Int i;
539 SysRes sres;
540 UShort list16[64];
541 if (size < 0) return -1;
542 if (size > 64) size = 64;
543 sres = VG_(do_syscall2)(__NR_getgroups, size, (Addr)list16);
544 if (sr_isError(sres))
545 return -1;
546 if (sr_Res(sres) > size)
547 return -1;
548 for (i = 0; i < sr_Res(sres); i++)
549 list[i] = (UInt)list16[i];
550 return sr_Res(sres);
552 # elif defined(VGP_amd64_linux) || defined(VGP_ppc64_linux) \
553 || defined(VGP_arm_linux) \
554 || defined(VGO_darwin) || defined(VGP_s390x_linux) \
555 || defined(VGP_mips32_linux)
556 SysRes sres;
557 sres = VG_(do_syscall2)(__NR_getgroups, size, (Addr)list);
558 if (sr_isError(sres))
559 return -1;
560 return sr_Res(sres);
562 # else
563 # error "VG_(getgroups): needs implementation on this platform"
564 # endif
567 /* ---------------------------------------------------------------------
568 Process tracing
569 ------------------------------------------------------------------ */
571 Int VG_(ptrace) ( Int request, Int pid, void *addr, void *data )
573 SysRes res;
574 res = VG_(do_syscall4)(__NR_ptrace, request, pid, (UWord)addr, (UWord)data);
575 if (sr_isError(res))
576 return -1;
577 return sr_Res(res);
580 /* ---------------------------------------------------------------------
581 Fork
582 ------------------------------------------------------------------ */
584 Int VG_(fork) ( void )
586 # if defined(VGO_linux)
587 SysRes res;
588 res = VG_(do_syscall0)(__NR_fork);
589 if (sr_isError(res))
590 return -1;
591 return sr_Res(res);
593 # elif defined(VGO_darwin)
594 SysRes res;
595 res = VG_(do_syscall0)(__NR_fork); /* __NR_fork is UX64 */
596 if (sr_isError(res))
597 return -1;
598 /* on success: wLO = child pid; wHI = 1 for child, 0 for parent */
599 if (sr_ResHI(res) != 0) {
600 return 0; /* this is child: return 0 instead of child pid */
602 return sr_Res(res);
604 # else
605 # error "Unknown OS"
606 # endif
609 /* ---------------------------------------------------------------------
610 Timing stuff
611 ------------------------------------------------------------------ */
613 UInt VG_(read_millisecond_timer) ( void )
615 /* 'now' and 'base' are in microseconds */
616 static ULong base = 0;
617 ULong now;
619 # if defined(VGO_linux)
620 { SysRes res;
621 struct vki_timespec ts_now;
622 res = VG_(do_syscall2)(__NR_clock_gettime, VKI_CLOCK_MONOTONIC,
623 (UWord)&ts_now);
624 if (sr_isError(res) == 0) {
625 now = ts_now.tv_sec * 1000000ULL + ts_now.tv_nsec / 1000;
626 } else {
627 struct vki_timeval tv_now;
628 res = VG_(do_syscall2)(__NR_gettimeofday, (UWord)&tv_now, (UWord)NULL);
629 vg_assert(! sr_isError(res));
630 now = tv_now.tv_sec * 1000000ULL + tv_now.tv_usec;
634 # elif defined(VGO_darwin)
635 // Weird: it seems that gettimeofday() doesn't fill in the timeval, but
636 // rather returns the tv_sec as the low 32 bits of the result and the
637 // tv_usec as the high 32 bits of the result. (But the timeval cannot be
638 // NULL!) See bug 200990.
639 { SysRes res;
640 struct vki_timeval tv_now = { 0, 0 };
641 res = VG_(do_syscall2)(__NR_gettimeofday, (UWord)&tv_now, (UWord)NULL);
642 vg_assert(! sr_isError(res));
643 now = sr_Res(res) * 1000000ULL + sr_ResHI(res);
646 # else
647 # error "Unknown OS"
648 # endif
650 /* COMMON CODE */
651 if (base == 0)
652 base = now;
654 return (now - base) / 1000;
658 /* ---------------------------------------------------------------------
659 atfork()
660 ------------------------------------------------------------------ */
662 struct atfork {
663 vg_atfork_t pre;
664 vg_atfork_t parent;
665 vg_atfork_t child;
668 #define VG_MAX_ATFORK 10
670 static struct atfork atforks[VG_MAX_ATFORK];
671 static Int n_atfork = 0;
673 void VG_(atfork)(vg_atfork_t pre, vg_atfork_t parent, vg_atfork_t child)
675 Int i;
677 for (i = 0; i < n_atfork; i++) {
678 if (atforks[i].pre == pre &&
679 atforks[i].parent == parent &&
680 atforks[i].child == child)
681 return;
684 if (n_atfork >= VG_MAX_ATFORK)
685 VG_(core_panic)(
686 "Too many VG_(atfork) handlers requested: raise VG_MAX_ATFORK");
688 atforks[n_atfork].pre = pre;
689 atforks[n_atfork].parent = parent;
690 atforks[n_atfork].child = child;
692 n_atfork++;
695 void VG_(do_atfork_pre)(ThreadId tid)
697 Int i;
699 for (i = 0; i < n_atfork; i++)
700 if (atforks[i].pre != NULL)
701 (*atforks[i].pre)(tid);
704 void VG_(do_atfork_parent)(ThreadId tid)
706 Int i;
708 for (i = 0; i < n_atfork; i++)
709 if (atforks[i].parent != NULL)
710 (*atforks[i].parent)(tid);
713 void VG_(do_atfork_child)(ThreadId tid)
715 Int i;
717 for (i = 0; i < n_atfork; i++)
718 if (atforks[i].child != NULL)
719 (*atforks[i].child)(tid);
723 /* ---------------------------------------------------------------------
724 icache invalidation
725 ------------------------------------------------------------------ */
727 void VG_(invalidate_icache) ( void *ptr, SizeT nbytes )
729 if (nbytes == 0) return; // nothing to do
731 // Get cache info
732 VexArchInfo vai;
733 VG_(machine_get_VexArchInfo)(NULL, &vai);
735 // If I-caches are coherent, nothing needs to be done here
736 if (vai.hwcache_info.icaches_maintain_coherence) return;
738 # if defined(VGA_ppc32) || defined(VGA_ppc64)
739 Addr startaddr = (Addr) ptr;
740 Addr endaddr = startaddr + nbytes;
741 Addr cls;
742 Addr addr;
744 VG_(machine_get_VexArchInfo)( NULL, &vai );
745 cls = vai.ppc_cache_line_szB;
747 /* Stay sane .. */
748 vg_assert(cls == 32 || cls == 64 || cls == 128);
750 startaddr &= ~(cls - 1);
751 for (addr = startaddr; addr < endaddr; addr += cls) {
752 __asm__ __volatile__("dcbst 0,%0" : : "r" (addr));
754 __asm__ __volatile__("sync");
755 for (addr = startaddr; addr < endaddr; addr += cls) {
756 __asm__ __volatile__("icbi 0,%0" : : "r" (addr));
758 __asm__ __volatile__("sync; isync");
760 # elif defined(VGP_arm_linux)
761 /* ARM cache flushes are privileged, so we must defer to the kernel. */
762 Addr startaddr = (Addr) ptr;
763 Addr endaddr = startaddr + nbytes;
764 VG_(do_syscall2)(__NR_ARM_cacheflush, startaddr, endaddr);
766 # elif defined(VGA_mips32) || defined(VGA_mips64)
767 SysRes sres = VG_(do_syscall3)(__NR_cacheflush, (UWord) ptr,
768 (UWord) nbytes, (UWord) 3);
769 vg_assert( sres._isError == 0 );
771 # endif
775 /*--------------------------------------------------------------------*/
776 /*--- end ---*/
777 /*--------------------------------------------------------------------*/