1
2 /*--------------------------------------------------------------------*/
3 /*--- Process-related libc stuff. m_libcproc.c ---*/
4 /*--------------------------------------------------------------------*/
5
6 /*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
9
10 Copyright (C) 2000-2017 Julian Seward
11 jseward@acm.org
12
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.
17
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.
22
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.
27
28 The GNU General Public License is contained in the file COPYING.
29 */
30
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_libcfile.h"
38 #include "pub_core_libcprint.h"
39 #include "pub_core_libcproc.h"
40 #include "pub_core_libcsignal.h"
41 #include "pub_core_seqmatch.h"
42 #include "pub_core_mallocfree.h"
43 #include "pub_core_syscall.h"
44 #include "pub_core_xarray.h"
45 #include "pub_core_clientstate.h"
46
47 #if defined(VGO_darwin)
48 /* --- !!! --- EXTERNAL HEADERS start --- !!! --- */
49 #include <mach/mach.h> /* mach_thread_self */
50 /* --- !!! --- EXTERNAL HEADERS end --- !!! --- */
51 #endif
52
53 /* IMPORTANT: on Darwin it is essential to use the _nocancel versions
54 of syscalls rather than the vanilla version, if a _nocancel version
55 is available. See docs/internals/Darwin-notes.txt for the reason
56 why. */
57
58 /* ---------------------------------------------------------------------
59 Command line and environment stuff
60 ------------------------------------------------------------------ */
61
62 /* As deduced from sp_at_startup, the client's argc, argv[] and
63 envp[] as extracted from the client's stack at startup-time. */
64 HChar** VG_(client_envp) = NULL;
65
66 /* Path to library directory */
67 const HChar *VG_(libdir) = VG_LIBDIR;
68
69 const HChar *VG_(LD_PRELOAD_var_name) =
70 #if defined(VGO_linux) || defined(VGO_solaris) || defined(VGO_dragonfly)
71 "LD_PRELOAD";
72 #elif defined(VGO_darwin)
73 "DYLD_INSERT_LIBRARIES";
74 #else
75 # error Unknown OS
76 #endif
77
78 /* We do getenv without libc's help by snooping around in
79 VG_(client_envp) as determined at startup time. */
VG_(getenv)80 HChar *VG_(getenv)(const HChar *varname)
81 {
82 Int i, n;
83 vg_assert( VG_(client_envp) );
84 n = VG_(strlen)(varname);
85 for (i = 0; VG_(client_envp)[i] != NULL; i++) {
86 HChar* s = VG_(client_envp)[i];
87 if (VG_(strncmp)(varname, s, n) == 0 && s[n] == '=') {
88 return & s[n+1];
89 }
90 }
91 return NULL;
92 }
93
94 /* If free_fn is not NULL, it is called on "unset" environment variable. */
VG_(env_unsetenv)95 void VG_(env_unsetenv) ( HChar **env, const HChar *varname,
96 void (*free_fn) (void *) )
97 {
98 HChar **from, **to;
99 vg_assert(env);
100 vg_assert(varname);
101 to = NULL;
102 Int len = VG_(strlen)(varname);
103
104 for (from = to = env; from && *from; from++) {
105 if (!(VG_(strncmp)(varname, *from, len) == 0 && (*from)[len] == '=')) {
106 *to = *from;
107 to++;
108 } else if (free_fn != NULL) {
109 free_fn(*from);
110 }
111 }
112 *to = *from;
113 }
114
115 /* set the environment; returns the old env if a new one was allocated */
VG_(env_setenv)116 HChar **VG_(env_setenv) ( HChar ***envp, const HChar* varname,
117 const HChar *val )
118 {
119 HChar **env = (*envp);
120 HChar **cpp;
121 Int len = VG_(strlen)(varname);
122 HChar *valstr = VG_(malloc)("libcproc.es.1", len + VG_(strlen)(val) + 2);
123 HChar **oldenv = NULL;
124
125 VG_(sprintf)(valstr, "%s=%s", varname, val);
126
127 for (cpp = env; cpp && *cpp; cpp++) {
128 if (VG_(strncmp)(varname, *cpp, len) == 0 && (*cpp)[len] == '=') {
129 *cpp = valstr;
130 return oldenv;
131 }
132 }
133
134 if (env == NULL) {
135 env = VG_(malloc)("libcproc.es.2", sizeof(HChar *) * 2);
136 env[0] = valstr;
137 env[1] = NULL;
138
139 *envp = env;
140
141 } else {
142 Int envlen = (cpp-env) + 2;
143 HChar **newenv = VG_(malloc)("libcproc.es.3", envlen * sizeof(HChar *));
144
145 for (cpp = newenv; *env; )
146 *cpp++ = *env++;
147 *cpp++ = valstr;
148 *cpp++ = NULL;
149
150 oldenv = *envp;
151
152 *envp = newenv;
153 }
154
155 return oldenv;
156 }
157
158
159 /* Walk through a colon-separated environment variable, and remove the
160 entries which match remove_pattern. It slides everything down over
161 the removed entries, and pads the remaining space with '\0'. It
162 modifies the entries in place (in the client address space), but it
163 shouldn't matter too much, since we only do this just before an
164 execve().
165
166 This is also careful to mop up any excess ':'s, since empty strings
167 delimited by ':' are considered to be '.' in a path.
168 */
mash_colon_env(HChar * varp,const HChar * remove_pattern)169 static void mash_colon_env(HChar *varp, const HChar *remove_pattern)
170 {
171 HChar *const start = varp;
172 HChar *entry_start = varp;
173 HChar *output = varp;
174
175 if (varp == NULL)
176 return;
177
178 while(*varp) {
179 if (*varp == ':') {
180 HChar prev;
181 Bool match;
182
183 /* This is a bit subtle: we want to match against the entry
184 we just copied, because it may have overlapped with
185 itself, junking the original. */
186
187 prev = *output;
188 *output = '\0';
189
190 match = VG_(string_match)(remove_pattern, entry_start);
191
192 *output = prev;
193
194 if (match) {
195 output = entry_start;
196 varp++; /* skip ':' after removed entry */
197 } else
198 entry_start = output+1; /* entry starts after ':' */
199 }
200
201 if (*varp)
202 *output++ = *varp++;
203 }
204
205 /* make sure last entry is nul terminated */
206 *output = '\0';
207
208 /* match against the last entry */
209 if (VG_(string_match)(remove_pattern, entry_start)) {
210 output = entry_start;
211 if (output > start) {
212 /* remove trailing ':' */
213 output--;
214 vg_assert(*output == ':');
215 }
216 }
217
218 /* pad out the left-overs with '\0' */
219 while(output < varp)
220 *output++ = '\0';
221 }
222
223
224 /* Removes all the Valgrind-added stuff from the passed environment. Used
225 when starting child processes, so they don't see that added stuff.
226 If the ro_strings option is set to True then all strings referenced by envp
227 are considered read-only, which means they will be duplicated before they
228 are modified.
229 If free_fn is not NULL, it is called on "unset" environment variables. */
VG_(env_remove_valgrind_env_stuff)230 void VG_(env_remove_valgrind_env_stuff)(HChar** envp, Bool ro_strings,
231 void (*free_fn) (void *) )
232 {
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;
238
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 if (ro_strings)
247 envp[i] = VG_(strdup)("libcproc.erves.1", envp[i]);
248 ld_preload_str = &envp[i][11];
249 }
250 if (VG_(strncmp)(envp[i], "LD_LIBRARY_PATH=", 16) == 0) {
251 if (ro_strings)
252 envp[i] = VG_(strdup)("libcproc.erves.2", envp[i]);
253 ld_library_path_str = &envp[i][16];
254 }
255 if (VG_(strncmp)(envp[i], "DYLD_INSERT_LIBRARIES=", 22) == 0) {
256 if (ro_strings)
257 envp[i] = VG_(strdup)("libcproc.erves.3", envp[i]);
258 dyld_insert_libraries_str = &envp[i][22];
259 }
260 }
261
262 buf = VG_(malloc)("libcproc.erves.4", VG_(strlen)(VG_(libdir)) + 20);
263
264 // Remove Valgrind-specific entries from LD_*.
265 VG_(sprintf)(buf, "%s*/vgpreload_*.so", VG_(libdir));
266 mash_colon_env(ld_preload_str, buf);
267 mash_colon_env(dyld_insert_libraries_str, buf);
268 VG_(sprintf)(buf, "%s*", VG_(libdir));
269 mash_colon_env(ld_library_path_str, buf);
270
271 // Remove VALGRIND_LAUNCHER variable.
272 VG_(env_unsetenv)(envp, VALGRIND_LAUNCHER, free_fn);
273
274 // Remove DYLD_SHARED_REGION variable.
275 VG_(env_unsetenv)(envp, "DYLD_SHARED_REGION", free_fn);
276
277 // XXX if variable becomes empty, remove it completely?
278
279 VG_(free)(buf);
280 }
281
282 /* Resolves filename of VG_(cl_exec_fd) and copies it to the buffer.
283 Buffer must not be NULL and buf_size must be at least 1.
284 If buffer is not large enough it is terminated with '\0' only
285 when 'terminate_with_NUL == True'. */
VG_(client_fname)286 void VG_(client_fname)(HChar *buffer, SizeT buf_size, Bool terminate_with_NUL)
287 {
288 vg_assert(buffer != NULL);
289 vg_assert(buf_size >= 1);
290
291 const HChar *name;
292 if (VG_(resolve_filename)(VG_(cl_exec_fd), &name)) {
293 const HChar *n = name + VG_(strlen)(name) - 1;
294
295 while (n > name && *n != '/')
296 n--;
297 if (n != name)
298 n++;
299
300 VG_(strncpy)(buffer, n, buf_size);
301 if (terminate_with_NUL)
302 buffer[buf_size - 1] = '\0';
303 } else {
304 buffer[0] = '\0';
305 }
306 }
307
add_string(HChar * buffer,SizeT * buf_size,const HChar * string)308 static Bool add_string(HChar *buffer, SizeT *buf_size, const HChar *string)
309 {
310 SizeT len = VG_(strlen)(string);
311 VG_(strncat)(buffer, string, *buf_size);
312 if (len >= *buf_size - 1) {
313 *buf_size = 0;
314 return False;
315 } else {
316 *buf_size -= len;
317 return True;
318 }
319 }
320
321 /* Concatenates client exename and command line arguments into
322 the buffer. Buffer must not be NULL and buf_size must be
323 at least 1. Buffer is always terminated with '\0'. */
VG_(client_cmd_and_args)324 void VG_(client_cmd_and_args)(HChar *buffer, SizeT buf_size)
325 {
326 vg_assert(buffer != NULL);
327 vg_assert(buf_size >= 1);
328
329 buffer[0] = '\0';
330
331 if (add_string(buffer, &buf_size, VG_(args_the_exename)) == False)
332 return;
333
334 Int i;
335 for (i = 0; i < VG_(sizeXA)(VG_(args_for_client)); i++) {
336 if (add_string(buffer, &buf_size, " ") == False)
337 return;
338
339 HChar *arg = *(HChar **) VG_(indexXA)(VG_(args_for_client), i);
340 if (add_string(buffer, &buf_size, arg) == False)
341 return;
342 }
343 }
344
345 /* ---------------------------------------------------------------------
346 Various important syscall wrappers
347 ------------------------------------------------------------------ */
348
VG_(waitpid)349 Int VG_(waitpid)(Int pid, Int *status, Int options)
350 {
351 # if defined(VGO_linux) || defined(VGO_dragonfly)
352 SysRes res = VG_(do_syscall4)(__NR_wait4,
353 pid, (UWord)status, options, 0);
354 return sr_isError(res) ? -1 : sr_Res(res);
355 # elif defined(VGO_darwin)
356 SysRes res = VG_(do_syscall4)(__NR_wait4_nocancel,
357 pid, (UWord)status, options, 0);
358 return sr_isError(res) ? -1 : sr_Res(res);
359 # elif defined(VGO_solaris)
360 SysRes res;
361 vki_idtype_t idtype;
362 vki_id_t id;
363 vki_siginfo_t info;
364
365 /* We need to do a lot of work here. */
366
367 if (pid > 0) {
368 idtype = VKI_P_PID;
369 id = pid;
370 }
371 else if (pid < -1) {
372 idtype = VKI_P_PGID;
373 id = -pid;
374 }
375 else if (pid == -1) {
376 idtype = VKI_P_ALL;
377 id = 0;
378 }
379 else {
380 idtype = VKI_P_PGID;
381 res = VG_(do_syscall0)(__NR_getpid);
382 id = sr_ResHI(res);
383 }
384
385 options |= VKI_WEXITED | VKI_WTRAPPED;
386
387 res = VG_(do_syscall4)(__NR_waitsys, idtype, id, (UWord)&info, options);
388 if (sr_isError(res))
389 return -1;
390
391 if (status) {
392 Int s = info.si_status & 0xff;
393
394 switch (info.si_code) {
395 case VKI_CLD_EXITED:
396 s <<= 8;
397 break;
398 case VKI_CLD_DUMPED:
399 s |= VKI_WCOREFLG;
400 break;
401 case VKI_CLD_KILLED:
402 break;
403 case VKI_CLD_TRAPPED:
404 case VKI_CLD_STOPPED:
405 s <<= 8;
406 s |= VKI_WSTOPFLG;
407 break;
408 case VKI_CLD_CONTINUED:
409 s = VKI_WCONTFLG;
410 break;
411 }
412 *status = s;
413 }
414
415 return info.si_pid;
416 # else
417 # error Unknown OS
418 # endif
419 }
420
421 /* clone the environment */
VG_(env_clone)422 HChar **VG_(env_clone) ( HChar **oldenv )
423 {
424 HChar **oldenvp;
425 HChar **newenvp;
426 HChar **newenv;
427 Int envlen;
428
429 vg_assert(oldenv);
430 for (oldenvp = oldenv; oldenvp && *oldenvp; oldenvp++);
431
432 envlen = oldenvp - oldenv + 1;
433
434 newenv = VG_(malloc)("libcproc.ec.1", envlen * sizeof(HChar *));
435
436 oldenvp = oldenv;
437 newenvp = newenv;
438
439 while (oldenvp && *oldenvp) {
440 *newenvp++ = *oldenvp++;
441 }
442
443 *newenvp = *oldenvp;
444
445 return newenv;
446 }
447
VG_(execv)448 void VG_(execv) ( const HChar* filename, const HChar** argv )
449 {
450 HChar** envp;
451 SysRes res;
452
453 envp = VG_(env_clone)(VG_(client_envp));
454 VG_(env_remove_valgrind_env_stuff)( envp, True /*ro_strings*/, NULL );
455
456 res = VG_(do_syscall3)(__NR_execve,
457 (UWord)filename, (UWord)argv, (UWord)envp);
458
459 VG_(printf)("EXEC failed, errno = %lld\n", (Long)sr_Err(res));
460 }
461
462 /* Spawns a new child. Uses either spawn syscall or fork+execv combo. */
VG_(spawn)463 Int VG_(spawn) ( const HChar *filename, const HChar **argv )
464 {
465 vg_assert(filename != NULL);
466 vg_assert(argv != NULL);
467
468 # if defined(VGO_solaris) && defined(SOLARIS_SPAWN_SYSCALL)
469 HChar **envp = VG_(env_clone)(VG_(client_envp));
470 for (HChar **p = envp; *p != NULL; p++) {
471 *p = VG_(strdup)("libcproc.s.1", *p);
472 }
473 VG_(env_remove_valgrind_env_stuff)(envp, /* ro_strings */ False, VG_(free));
474
475 /* Now combine argv and argp into argenv. */
476 SizeT argenv_size = 1 + 1;
477 for (const HChar **p = argv; *p != NULL; p++) {
478 argenv_size += VG_(strlen)(*p) + 2;
479 }
480 for (HChar **p = envp; *p != NULL; p++) {
481 argenv_size += VG_(strlen)(*p) + 2;
482 }
483
484 HChar *argenv = VG_(malloc)("libcproc.s.2", argenv_size);
485 HChar *current = argenv;
486 # define COPY_CHAR_TO_ARGENV(dst, character) \
487 do { \
488 *(dst) = character; \
489 (dst) += 1; \
490 } while (0)
491 # define COPY_STRING_TO_ARGENV(dst, src) \
492 do { \
493 COPY_CHAR_TO_ARGENV(dst, '\1'); \
494 SizeT src_len = VG_(strlen)((src)) + 1; \
495 VG_(memcpy)((dst), (src), src_len); \
496 (dst) += src_len; \
497 } while (0)
498
499 for (const HChar **p = argv; *p != NULL; p++) {
500 COPY_STRING_TO_ARGENV(current, *p);
501 }
502 COPY_CHAR_TO_ARGENV(current, '\0');
503 for (HChar **p = envp; *p != NULL; p++) {
504 COPY_STRING_TO_ARGENV(current, *p);
505 }
506 COPY_CHAR_TO_ARGENV(current, '\0');
507 vg_assert(current == argenv + argenv_size);
508 # undef COPY_CHAR_TO_ARGENV
509 # undef COPY_STRING_TOARGENV
510
511 SysRes res = VG_(do_syscall5)(__NR_spawn, (UWord) filename, (UWord) NULL, 0,
512 (UWord) argenv, argenv_size);
513
514 VG_(free)(argenv);
515 for (HChar **p = envp; *p != NULL; p++) {
516 VG_(free)(*p);
517 }
518 VG_(free)(envp);
519
520 if (sr_isError(res))
521 return -1;
522 return sr_Res(res);
523
524 # else
525
526 Int pid = VG_(fork)();
527 if (pid < 0)
528 return -1;
529 if (pid == 0) {
530 /* child */
531 VG_(execv)(argv[0], argv);
532
533 /* If we're still alive here, execv failed. */
534 VG_(exit)(1);
535 } else {
536 return pid;
537 }
538 # endif /* VGO_solaris && SOLARIS_SPAWN_SYSCALL */
539 }
540
541 /* Return -1 if error, else 0. NOTE does not indicate return code of
542 child! */
VG_(system)543 Int VG_(system) ( const HChar* cmd )
544 {
545 Int pid;
546 if (cmd == NULL)
547 return 1;
548
549 const HChar *argv[4] = { "/bin/sh", "-c", cmd, 0 };
550 pid = VG_(spawn)(argv[0], argv);
551 if (pid < 0)
552 return -1;
553
554 vg_assert(pid > 0);
555 /* parent */
556 /* We have to set SIGCHLD to its default behaviour in order that
557 VG_(waitpid) works (at least on AIX). According to the Linux
558 man page for waitpid:
559
560 POSIX.1-2001 specifies that if the disposition of SIGCHLD is
561 set to SIG_IGN or the SA_NOCLDWAIT flag is set for SIGCHLD
562 (see sigaction(2)), then children that terminate do not
563 become zombies and a call to wait() or waitpid() will block
564 until all children have terminated, and then fail with errno
565 set to ECHILD. (The original POSIX standard left the
566 behaviour of setting SIGCHLD to SIG_IGN unspecified.)
567 */
568 Int ir, zzz;
569 vki_sigaction_toK_t sa, sa2;
570 vki_sigaction_fromK_t saved_sa;
571 VG_(memset)( &sa, 0, sizeof(sa) );
572 VG_(sigemptyset)(&sa.sa_mask);
573 sa.ksa_handler = VKI_SIG_DFL;
574 sa.sa_flags = 0;
575 ir = VG_(sigaction)(VKI_SIGCHLD, &sa, &saved_sa);
576 vg_assert(ir == 0);
577
578 zzz = VG_(waitpid)(pid, NULL, 0);
579
580 VG_(convert_sigaction_fromK_to_toK)( &saved_sa, &sa2 );
581 ir = VG_(sigaction)(VKI_SIGCHLD, &sa2, NULL);
582 vg_assert(ir == 0);
583 return zzz == -1 ? -1 : 0;
584 }
585
VG_(sysctl)586 Int VG_(sysctl)(Int *name, UInt namelen, void *oldp, SizeT *oldlenp, void *newp, SizeT newlen)
587 {
588 SysRes res;
589 # if defined(VGO_darwin) || defined(VGO_dragonfly)
590 res = VG_(do_syscall6)(__NR___sysctl,
591 (UWord)name, namelen, (UWord)oldp, (UWord)oldlenp, (UWord)newp, newlen);
592 # else
593 res = VG_(mk_SysRes_Error)(VKI_ENOSYS);
594 # endif
595 return sr_isError(res) ? -1 : sr_Res(res);
596 }
597
598 /* ---------------------------------------------------------------------
599 Resource limits
600 ------------------------------------------------------------------ */
601
602 /* Support for getrlimit. */
VG_(getrlimit)603 Int VG_(getrlimit) (Int resource, struct vki_rlimit *rlim)
604 {
605 SysRes res = VG_(mk_SysRes_Error)(VKI_ENOSYS);
606 /* res = getrlimit( resource, rlim ); */
607 # ifdef __NR_ugetrlimit
608 res = VG_(do_syscall2)(__NR_ugetrlimit, resource, (UWord)rlim);
609 # endif
610 if (sr_isError(res) && sr_Err(res) == VKI_ENOSYS)
611 res = VG_(do_syscall2)(__NR_getrlimit, resource, (UWord)rlim);
612 return sr_isError(res) ? -1 : sr_Res(res);
613 }
614
615
616 /* Support for setrlimit. */
VG_(setrlimit)617 Int VG_(setrlimit) (Int resource, const struct vki_rlimit *rlim)
618 {
619 SysRes res;
620 /* res = setrlimit( resource, rlim ); */
621 res = VG_(do_syscall2)(__NR_setrlimit, resource, (UWord)rlim);
622 return sr_isError(res) ? -1 : sr_Res(res);
623 }
624
625 /* Support for prctl. */
VG_(prctl)626 Int VG_(prctl) (Int option,
627 ULong arg2, ULong arg3, ULong arg4, ULong arg5)
628 {
629 SysRes res = VG_(mk_SysRes_Error)(VKI_ENOSYS);
630 # if defined(VGO_linux)
631 /* res = prctl( option, arg2, arg3, arg4, arg5 ); */
632 res = VG_(do_syscall5)(__NR_prctl, (UWord) option,
633 (UWord) arg2, (UWord) arg3, (UWord) arg4,
634 (UWord) arg5);
635 # endif
636
637 return sr_isError(res) ? -1 : sr_Res(res);
638 }
639
640 /* ---------------------------------------------------------------------
641 pids, etc
642 ------------------------------------------------------------------ */
643
VG_(gettid)644 Int VG_(gettid)(void)
645 {
646 # if defined(VGO_linux)
647 SysRes res = VG_(do_syscall0)(__NR_gettid);
648
649 if (sr_isError(res) && sr_Res(res) == VKI_ENOSYS) {
650 HChar pid[16];
651 /*
652 * The gettid system call does not exist. The obvious assumption
653 * to make at this point would be that we are running on an older
654 * system where the getpid system call actually returns the ID of
655 * the current thread.
656 *
657 * Unfortunately it seems that there are some systems with a kernel
658 * where getpid has been changed to return the ID of the thread group
659 * leader but where the gettid system call has not yet been added.
660 *
661 * So instead of calling getpid here we use readlink to see where
662 * the /proc/self link is pointing...
663 */
664
665 # if defined(VGP_arm64_linux)
666 res = VG_(do_syscall4)(__NR_readlinkat, VKI_AT_FDCWD,
667 (UWord)"/proc/self",
668 (UWord)pid, sizeof(pid));
669 # else
670 res = VG_(do_syscall3)(__NR_readlink, (UWord)"/proc/self",
671 (UWord)pid, sizeof(pid));
672 # endif
673 if (!sr_isError(res) && sr_Res(res) > 0) {
674 HChar* s;
675 pid[sr_Res(res)] = '\0';
676 res = VG_(mk_SysRes_Success)( VG_(strtoll10)(pid, &s) );
677 if (*s != '\0') {
678 VG_(message)(Vg_DebugMsg,
679 "Warning: invalid file name linked to by /proc/self: %s\n",
680 pid);
681 }
682 }
683 }
684
685 return sr_Res(res);
686
687 # elif defined(VGO_dragonfly)
688 SysRes res;
689 long tid;
690
691 res = VG_(do_syscall0)(__NR_lwp_gettid);
692 if (sr_isError(res))
693 tid = sr_Res(VG_(do_syscall0)(__NR_getpid));
694 else
695 tid = sr_Res(res);
696
697 return tid;
698
699 # elif defined(VGO_darwin)
700 // Darwin's gettid syscall is something else.
701 // Use Mach thread ports for lwpid instead.
702 return mach_thread_self();
703
704 # elif defined(VGO_solaris)
705 SysRes res = VG_(do_syscall0)(__NR_lwp_self);
706 return sr_Res(res);
707
708 # else
709 # error "Unknown OS"
710 # endif
711 }
712
713 /* You'd be amazed how many places need to know the current pid. */
VG_(getpid)714 Int VG_(getpid) ( void )
715 {
716 /* ASSUMES SYSCALL ALWAYS SUCCEEDS */
717 return sr_Res( VG_(do_syscall0)(__NR_getpid) );
718 }
719
VG_(getpgrp)720 Int VG_(getpgrp) ( void )
721 {
722 /* ASSUMES SYSCALL ALWAYS SUCCEEDS */
723 # if defined(VGP_arm64_linux)
724 return sr_Res( VG_(do_syscall1)(__NR_getpgid, 0) );
725 # elif defined(VGO_linux) || defined(VGO_darwin) || defined(VGO_dragonfly)
726 return sr_Res( VG_(do_syscall0)(__NR_getpgrp) );
727 # elif defined(VGO_solaris)
728 /* Uses the shared pgrpsys syscall, 0 for the getpgrp variant. */
729 return sr_Res( VG_(do_syscall1)(__NR_pgrpsys, 0) );
730 # else
731 # error Unknown OS
732 # endif
733 }
734
VG_(getppid)735 Int VG_(getppid) ( void )
736 {
737 /* ASSUMES SYSCALL ALWAYS SUCCEEDS */
738 # if defined(VGO_linux) || defined(VGO_darwin) || defined(VGO_dragonfly)
739 return sr_Res( VG_(do_syscall0)(__NR_getppid) );
740 # elif defined(VGO_solaris)
741 /* Uses the shared getpid/getppid syscall, val2 contains a parent pid. */
742 return sr_ResHI( VG_(do_syscall0)(__NR_getpid) );
743 # else
744 # error Unknown OS
745 # endif
746 }
747
VG_(geteuid)748 Int VG_(geteuid) ( void )
749 {
750 /* ASSUMES SYSCALL ALWAYS SUCCEEDS */
751 # if defined(VGO_linux) || defined(VGO_darwin) || defined(VGO_dragonfly)
752 {
753 # if defined(__NR_geteuid32)
754 // We use the 32-bit version if it's supported. Otherwise, IDs greater
755 // than 65536 cause problems, as bug #151209 showed.
756 return sr_Res( VG_(do_syscall0)(__NR_geteuid32) );
757 # else
758 return sr_Res( VG_(do_syscall0)(__NR_geteuid) );
759 # endif
760 }
761 # elif defined(VGO_solaris)
762 /* Uses the shared getuid/geteuid syscall, val2 contains the effective
763 uid. */
764 return sr_ResHI( VG_(do_syscall0)(__NR_getuid) );
765 # else
766 # error Unknown OS
767 # endif
768 }
769
VG_(getegid)770 Int VG_(getegid) ( void )
771 {
772 # if defined(VGO_linux) || defined(VGO_darwin) || defined(VGO_dragonfly)
773 /* ASSUMES SYSCALL ALWAYS SUCCEEDS */
774 # if defined(__NR_getegid32)
775 // We use the 32-bit version if it's supported. Otherwise, IDs greater
776 // than 65536 cause problems, as bug #151209 showed.
777 return sr_Res( VG_(do_syscall0)(__NR_getegid32) );
778 # else
779 return sr_Res( VG_(do_syscall0)(__NR_getegid) );
780 # endif
781
782 # elif defined(VGO_solaris)
783 /* Uses the shared getgid/getegid syscall, val2 contains the effective
784 gid. */
785 return sr_ResHI( VG_(do_syscall0)(__NR_getgid) );
786 # else
787 # error Unknown OS
788 # endif
789 }
790
791 /* Get supplementary groups into list[0 .. size-1]. Returns the
792 number of groups written, or -1 if error. Note that in order to be
793 portable, the groups are 32-bit unsigned ints regardless of the
794 platform.
795 As a special case, if size == 0 the function returns the number of
796 groups leaving list untouched. */
VG_(getgroups)797 Int VG_(getgroups)( Int size, UInt* list )
798 {
799 if (size < 0) return -1;
800
801 # if defined(VGP_x86_linux) || defined(VGP_ppc32_linux) \
802 || defined(VGP_mips64_linux)
803 Int i;
804 SysRes sres;
805 UShort list16[size];
806 sres = VG_(do_syscall2)(__NR_getgroups, size, (Addr)list16);
807 if (sr_isError(sres))
808 return -1;
809 if (size != 0) {
810 for (i = 0; i < sr_Res(sres); i++)
811 list[i] = (UInt)list16[i];
812 }
813 return sr_Res(sres);
814
815 # elif defined(VGP_amd64_linux) || defined(VGP_arm_linux) \
816 || defined(VGP_ppc64be_linux) || defined(VGP_ppc64le_linux) \
817 || defined(VGO_darwin) || defined(VGP_s390x_linux) \
818 || defined(VGP_mips32_linux) || defined(VGP_arm64_linux) \
819 || defined(VGO_solaris) || defined(VGO_dragonfly)
820 SysRes sres;
821 sres = VG_(do_syscall2)(__NR_getgroups, size, (Addr)list);
822 if (sr_isError(sres))
823 return -1;
824 return sr_Res(sres);
825
826 # else
827 # error "VG_(getgroups): needs implementation on this platform"
828 # endif
829 }
830
831 /* ---------------------------------------------------------------------
832 Process tracing
833 ------------------------------------------------------------------ */
834
VG_(ptrace)835 Int VG_(ptrace) ( Int request, Int pid, void *addr, void *data )
836 {
837 SysRes res;
838 # if defined(VGO_linux) || defined(VGO_darwin) || defined(VGO_dragonfly)
839 res = VG_(do_syscall4)(__NR_ptrace, request, pid, (UWord)addr, (UWord)data);
840 # elif defined(VGO_solaris)
841 /* There is no ptrace syscall on Solaris. Such requests has to be
842 implemented using the /proc interface. Callers of VG_(ptrace) should
843 ensure that this function is not reached on Solaris, i.e. they must
844 provide a special code for Solaris for whatever feature they provide. */
845 I_die_here;
846 # else
847 # error Unknown OS
848 # endif
849 if (sr_isError(res))
850 return -1;
851 return sr_Res(res);
852 }
853
854 /* ---------------------------------------------------------------------
855 Fork
856 ------------------------------------------------------------------ */
857
VG_(fork)858 Int VG_(fork) ( void )
859 {
860 # if defined(VGP_arm64_linux)
861 SysRes res;
862 res = VG_(do_syscall5)(__NR_clone, VKI_SIGCHLD,
863 (UWord)NULL, (UWord)NULL, (UWord)NULL, (UWord)NULL);
864 if (sr_isError(res))
865 return -1;
866 return sr_Res(res);
867
868 # elif defined(VGO_linux) || defined(VGO_dragonfly)
869 SysRes res;
870 res = VG_(do_syscall0)(__NR_fork);
871 if (sr_isError(res))
872 return -1;
873 return sr_Res(res);
874
875 # elif defined(VGO_darwin)
876 SysRes res;
877 res = VG_(do_syscall0)(__NR_fork); /* __NR_fork is UX64 */
878 if (sr_isError(res))
879 return -1;
880 /* on success: wLO = child pid; wHI = 1 for child, 0 for parent */
881 if (sr_ResHI(res) != 0) {
882 return 0; /* this is child: return 0 instead of child pid */
883 }
884 return sr_Res(res);
885
886 # elif defined(VGO_solaris)
887 /* Using fork() on Solaris is not really the best thing to do. Solaris
888 does not do memory overcommitment so fork() can fail if there is not
889 enough memory to copy the current process into a new one.
890 Prefer to use VG_(spawn)() over VG_(fork)() + VG_(execv)(). */
891 SysRes res;
892 res = VG_(do_syscall2)(__NR_forksys, 0 /*subcode (fork)*/, 0 /*flags*/);
893 if (sr_isError(res))
894 return -1;
895 /* On success:
896 val = a pid of the child in the parent, a pid of the parent in the
897 child,
898 val2 = 0 in the parent process, 1 in the child process. */
899 if (sr_ResHI(res) != 0) {
900 return 0;
901 }
902 return sr_Res(res);
903
904 # else
905 # error "Unknown OS"
906 # endif
907 }
908
909 /* ---------------------------------------------------------------------
910 Timing stuff
911 ------------------------------------------------------------------ */
912
VG_(read_millisecond_timer)913 UInt VG_(read_millisecond_timer) ( void )
914 {
915 /* 'now' and 'base' are in microseconds */
916 static ULong base = 0;
917 ULong now;
918
919 # if defined(VGO_linux) || defined(VGO_solaris)
920 { SysRes res;
921 struct vki_timespec ts_now;
922 res = VG_(do_syscall2)(__NR_clock_gettime, VKI_CLOCK_MONOTONIC,
923 (UWord)&ts_now);
924 if (sr_isError(res) == 0) {
925 now = ts_now.tv_sec * 1000000ULL + ts_now.tv_nsec / 1000;
926 } else {
927 struct vki_timeval tv_now;
928 /* Note: On Solaris, this syscall takes only one parameter but the
929 extra dummy one does not cause any harm. */
930 res = VG_(do_syscall2)(__NR_gettimeofday, (UWord)&tv_now, (UWord)NULL);
931 vg_assert(! sr_isError(res));
932 now = tv_now.tv_sec * 1000000ULL + tv_now.tv_usec;
933 }
934 }
935
936 # elif defined(VGO_dragonfly)
937 { SysRes res;
938 struct vki_timeval tv_now;
939 res = VG_(do_syscall2)(__NR_gettimeofday, (UWord)&tv_now, (UWord)NULL);
940 vg_assert(! sr_isError(res));
941 now = tv_now.tv_sec * 1000000ULL + tv_now.tv_usec;
942 }
943 # elif defined(VGO_darwin)
944 // Weird: it seems that gettimeofday() doesn't fill in the timeval, but
945 // rather returns the tv_sec as the low 32 bits of the result and the
946 // tv_usec as the high 32 bits of the result. (But the timeval cannot be
947 // NULL!) See bug 200990.
948 { SysRes res;
949 struct vki_timeval tv_now = { 0, 0 };
950 res = VG_(do_syscall2)(__NR_gettimeofday, (UWord)&tv_now, (UWord)NULL);
951 vg_assert(! sr_isError(res));
952 now = sr_Res(res) * 1000000ULL + sr_ResHI(res);
953 }
954
955 # else
956 # error "Unknown OS"
957 # endif
958
959 /* COMMON CODE */
960 if (base == 0)
961 base = now;
962
963 return (now - base) / 1000;
964 }
965
VG_(gettimeofday)966 Int VG_(gettimeofday)(struct vki_timeval *tv, struct vki_timezone *tz)
967 {
968 SysRes res;
969 res = VG_(do_syscall2)(__NR_gettimeofday, (UWord)tv, (UWord)tz);
970
971 if (! sr_isError(res)) return 0;
972
973 /* Make sure, argument values are deterministic upon failure */
974 if (tv) *tv = (struct vki_timeval){ .tv_sec = 0, .tv_usec = 0 };
975 if (tz) *tz = (struct vki_timezone){ .tz_minuteswest = 0, .tz_dsttime = 0 };
976
977 return -1;
978 }
979
VG_(get_user_milliseconds)980 UInt VG_(get_user_milliseconds)(void)
981 {
982 UInt res = 0;
983 # if defined(VGO_linux)
984 {
985 struct vki_rusage ru;
986 VG_(memset)(&ru, 0, sizeof(ru));
987 SysRes sr = VG_(do_syscall2)(__NR_getrusage, VKI_RUSAGE_SELF, (UWord)&ru);
988 if (!sr_isError(sr)) {
989 res = ru.ru_utime.tv_sec * 1000 + ru.ru_utime.tv_usec / 1000;
990 }
991 }
992
993 # elif defined(VGO_solaris)
994 {
995 struct vki_rusage ru;
996 VG_(memset)(&ru, 0, sizeof(ru));
997 SysRes sr = VG_(do_syscall2)(__NR_rusagesys, VKI__RUSAGESYS_GETRUSAGE,
998 (UWord) &ru);
999 if (!sr_isError(sr)) {
1000 res = ru.ru_utime.tv_sec * 1000 + ru.ru_utime.tv_usec / 1000;
1001 }
1002 }
1003
1004 # elif defined(VGO_darwin)
1005 res = 0;
1006
1007 # elif defined(VGO_dragonfly)
1008 res = 0;
1009
1010 # else
1011 # error "Unknown OS"
1012 # endif
1013
1014 return res;
1015 }
1016
1017
1018 /* ---------------------------------------------------------------------
1019 atfork()
1020 ------------------------------------------------------------------ */
1021
1022 struct atfork {
1023 vg_atfork_t pre;
1024 vg_atfork_t parent;
1025 vg_atfork_t child;
1026 };
1027
1028 #define VG_MAX_ATFORK 10
1029
1030 static struct atfork atforks[VG_MAX_ATFORK];
1031 static Int n_atfork = 0;
1032
VG_(atfork)1033 void VG_(atfork)(vg_atfork_t pre, vg_atfork_t parent, vg_atfork_t child)
1034 {
1035 Int i;
1036
1037 for (i = 0; i < n_atfork; i++) {
1038 if (atforks[i].pre == pre &&
1039 atforks[i].parent == parent &&
1040 atforks[i].child == child)
1041 return;
1042 }
1043
1044 if (n_atfork >= VG_MAX_ATFORK)
1045 VG_(core_panic)(
1046 "Too many VG_(atfork) handlers requested: raise VG_MAX_ATFORK");
1047
1048 atforks[n_atfork].pre = pre;
1049 atforks[n_atfork].parent = parent;
1050 atforks[n_atfork].child = child;
1051
1052 n_atfork++;
1053 }
1054
VG_(do_atfork_pre)1055 void VG_(do_atfork_pre)(ThreadId tid)
1056 {
1057 Int i;
1058
1059 for (i = 0; i < n_atfork; i++)
1060 if (atforks[i].pre != NULL)
1061 (*atforks[i].pre)(tid);
1062 }
1063
VG_(do_atfork_parent)1064 void VG_(do_atfork_parent)(ThreadId tid)
1065 {
1066 Int i;
1067
1068 for (i = 0; i < n_atfork; i++)
1069 if (atforks[i].parent != NULL)
1070 (*atforks[i].parent)(tid);
1071 }
1072
VG_(do_atfork_child)1073 void VG_(do_atfork_child)(ThreadId tid)
1074 {
1075 Int i;
1076
1077 for (i = 0; i < n_atfork; i++)
1078 if (atforks[i].child != NULL)
1079 (*atforks[i].child)(tid);
1080 }
1081
1082 /* ---------------------------------------------------------------------
1083 Dragonfly sysctlbyname(), modfind(), etc
1084 ------------------------------------------------------------------ */
1085
1086 #if defined(VGO_dragonfly)
VG_(sysctlbyname)1087 Int VG_(sysctlbyname)(const Char *name, void *oldp, vki_size_t *oldlenp, void *newp, vki_size_t newlen)
1088 {
1089 Int oid[2];
1090 Int real_oid[10];
1091 vki_size_t oidlen;
1092 int error;
1093
1094 oid[0] = 0; /* magic */
1095 oid[1] = 3; /* undocumented */
1096 oidlen = sizeof(real_oid);
1097 error = VG_(sysctl)(oid, 2, real_oid, &oidlen, (void *)name, VG_(strlen)(name));
1098 if (error < 0)
1099 return error;
1100 oidlen /= sizeof(int);
1101 error = VG_(sysctl)(real_oid, oidlen, oldp, oldlenp, newp, newlen);
1102 return error;
1103 }
1104
VG_(getosreldate)1105 Int VG_(getosreldate)(void)
1106 {
1107 static Int osreldate = 0;
1108 vki_size_t osreldatel;
1109
1110 if (osreldate == 0) {
1111 osreldatel = sizeof(osreldate);
1112 VG_(sysctlbyname)("kern.osreldate", &osreldate, &osreldatel, 0, 0);
1113 }
1114 return (osreldate);
1115 }
1116
VG_(is32on64)1117 Bool VG_(is32on64)(void)
1118 {
1119 #if defined(VGP_amd64_dragonfly)
1120 return False;
1121 #elif defined(VGP_x86_dragonfly)
1122 Int oid[2], error;
1123 vki_size_t len;
1124 char machbuf[32];
1125 static Int is32on64 = -1;
1126
1127 if (is32on64 == -1) {
1128 oid[0] = VKI_CTL_HW;
1129 oid[1] = VKI_HW_MACHINE;
1130 len = sizeof(machbuf);
1131 error = VG_(sysctl)(oid, 2, machbuf, &len, NULL, 0);
1132 if (error == 0) {
1133 machbuf[31] = '\0';
1134 if (VG_(strcmp)(machbuf, "amd64") == 0)
1135 is32on64 = 1;
1136 else
1137 is32on64 = 0;
1138 } else {
1139 is32on64 = -2;
1140 }
1141 }
1142 if (is32on64 == 1) {
1143 return True;
1144 } else {
1145 return False;
1146 }
1147 #else
1148 # error Unknown platform
1149 #endif
1150 }
1151 #endif
1152
1153 /* ---------------------------------------------------------------------
1154 icache invalidation
1155 ------------------------------------------------------------------ */
1156
VG_(invalidate_icache)1157 void VG_(invalidate_icache) ( void *ptr, SizeT nbytes )
1158 {
1159 if (nbytes == 0) return; // nothing to do
1160
1161 // Get cache info
1162 VexArchInfo vai;
1163 VG_(machine_get_VexArchInfo)(NULL, &vai);
1164
1165 // If I-caches are coherent, nothing needs to be done here
1166 if (vai.hwcache_info.icaches_maintain_coherence) return;
1167
1168 # if defined(VGA_ppc32) || defined(VGA_ppc64be) || defined(VGA_ppc64le)
1169 Addr startaddr = (Addr) ptr;
1170 Addr endaddr = startaddr + nbytes;
1171 Addr cls;
1172 Addr addr;
1173
1174 cls = vai.ppc_icache_line_szB;
1175
1176 /* Stay sane .. */
1177 vg_assert(cls == 16 || cls == 32 || cls == 64 || cls == 128);
1178
1179 startaddr &= ~(cls - 1);
1180 for (addr = startaddr; addr < endaddr; addr += cls) {
1181 __asm__ __volatile__("dcbst 0,%0" : : "r" (addr));
1182 }
1183 __asm__ __volatile__("sync");
1184 for (addr = startaddr; addr < endaddr; addr += cls) {
1185 __asm__ __volatile__("icbi 0,%0" : : "r" (addr));
1186 }
1187 __asm__ __volatile__("sync; isync");
1188
1189 # elif defined(VGP_arm_linux)
1190 /* ARM cache flushes are privileged, so we must defer to the kernel. */
1191 Addr startaddr = (Addr) ptr;
1192 Addr endaddr = startaddr + nbytes;
1193 VG_(do_syscall2)(__NR_ARM_cacheflush, startaddr, endaddr);
1194
1195 # elif defined(VGP_arm64_linux)
1196 // This arm64_linux section of this function VG_(invalidate_icache)
1197 // is copied from
1198 // https://github.com/armvixl/vixl/blob/master/src/a64/cpu-a64.cc
1199 // which has the following copyright notice:
1200 /*
1201 Copyright 2013, ARM Limited
1202 All rights reserved.
1203
1204 Redistribution and use in source and binary forms, with or without
1205 modification, are permitted provided that the following conditions are met:
1206
1207 * Redistributions of source code must retain the above copyright notice,
1208 this list of conditions and the following disclaimer.
1209 * Redistributions in binary form must reproduce the above copyright notice,
1210 this list of conditions and the following disclaimer in the documentation
1211 and/or other materials provided with the distribution.
1212 * Neither the name of ARM Limited nor the names of its contributors may be
1213 used to endorse or promote products derived from this software without
1214 specific prior written permission.
1215
1216 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
1217 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1218 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1219 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
1220 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1221 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
1222 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
1223 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
1224 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1225 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1226 */
1227
1228 // Ask what the I and D line sizes are
1229 UInt cache_type_register;
1230 // Copy the content of the cache type register to a core register.
1231 __asm__ __volatile__ ("mrs %[ctr], ctr_el0" // NOLINT
1232 : [ctr] "=r" (cache_type_register));
1233
1234 const Int kDCacheLineSizeShift = 16;
1235 const Int kICacheLineSizeShift = 0;
1236 const UInt kDCacheLineSizeMask = 0xf << kDCacheLineSizeShift;
1237 const UInt kICacheLineSizeMask = 0xf << kICacheLineSizeShift;
1238
1239 // The cache type register holds the size of the I and D caches as a power of
1240 // two.
1241 const UInt dcache_line_size_power_of_two =
1242 (cache_type_register & kDCacheLineSizeMask) >> kDCacheLineSizeShift;
1243 const UInt icache_line_size_power_of_two =
1244 (cache_type_register & kICacheLineSizeMask) >> kICacheLineSizeShift;
1245
1246 const UInt dcache_line_size_ = 4 * (1 << dcache_line_size_power_of_two);
1247 const UInt icache_line_size_ = 4 * (1 << icache_line_size_power_of_two);
1248
1249 Addr start = (Addr)ptr;
1250 // Sizes will be used to generate a mask big enough to cover a pointer.
1251 Addr dsize = (Addr)dcache_line_size_;
1252 Addr isize = (Addr)icache_line_size_;
1253
1254 // Cache line sizes are always a power of 2.
1255 Addr dstart = start & ~(dsize - 1);
1256 Addr istart = start & ~(isize - 1);
1257 Addr end = start + nbytes;
1258
1259 __asm__ __volatile__ (
1260 // Clean every line of the D cache containing the target data.
1261 "0: \n\t"
1262 // dc : Data Cache maintenance
1263 // c : Clean
1264 // va : by (Virtual) Address
1265 // u : to the point of Unification
1266 // The point of unification for a processor is the point by which the
1267 // instruction and data caches are guaranteed to see the same copy of a
1268 // memory location. See ARM DDI 0406B page B2-12 for more information.
1269 "dc cvau, %[dline] \n\t"
1270 "add %[dline], %[dline], %[dsize] \n\t"
1271 "cmp %[dline], %[end] \n\t"
1272 "b.lt 0b \n\t"
1273 // Barrier to make sure the effect of the code above is visible to the rest
1274 // of the world.
1275 // dsb : Data Synchronisation Barrier
1276 // ish : Inner SHareable domain
1277 // The point of unification for an Inner Shareable shareability domain is
1278 // the point by which the instruction and data caches of all the processors
1279 // in that Inner Shareable shareability domain are guaranteed to see the
1280 // same copy of a memory location. See ARM DDI 0406B page B2-12 for more
1281 // information.
1282 "dsb ish \n\t"
1283 // Invalidate every line of the I cache containing the target data.
1284 "1: \n\t"
1285 // ic : instruction cache maintenance
1286 // i : invalidate
1287 // va : by address
1288 // u : to the point of unification
1289 "ic ivau, %[iline] \n\t"
1290 "add %[iline], %[iline], %[isize] \n\t"
1291 "cmp %[iline], %[end] \n\t"
1292 "b.lt 1b \n\t"
1293 // Barrier to make sure the effect of the code above is visible to the rest
1294 // of the world.
1295 "dsb ish \n\t"
1296 // Barrier to ensure any prefetching which happened before this code is
1297 // discarded.
1298 // isb : Instruction Synchronisation Barrier
1299 "isb \n\t"
1300 : [dline] "+r" (dstart),
1301 [iline] "+r" (istart)
1302 : [dsize] "r" (dsize),
1303 [isize] "r" (isize),
1304 [end] "r" (end)
1305 // This code does not write to memory but without the dependency gcc might
1306 // move this code before the code is generated.
1307 : "cc", "memory"
1308 );
1309
1310 # elif defined(VGA_mips32) || defined(VGA_mips64)
1311 SysRes sres = VG_(do_syscall3)(__NR_cacheflush, (UWord) ptr,
1312 (UWord) nbytes, (UWord) 3);
1313 vg_assert( !sr_isError(sres) );
1314
1315 # endif
1316 }
1317
1318
1319 /* ---------------------------------------------------------------------
1320 dcache flushing
1321 ------------------------------------------------------------------ */
1322
VG_(flush_dcache)1323 void VG_(flush_dcache) ( void *ptr, SizeT nbytes )
1324 {
1325 /* Currently this is only required on ARM64. */
1326 # if defined(VGA_arm64)
1327 Addr startaddr = (Addr) ptr;
1328 Addr endaddr = startaddr + nbytes;
1329 Addr cls;
1330 Addr addr;
1331
1332 ULong ctr_el0;
1333 __asm__ __volatile__ ("mrs %0, ctr_el0" : "=r"(ctr_el0));
1334 cls = 4 * (1ULL << (0xF & (ctr_el0 >> 16)));
1335
1336 /* Stay sane .. */
1337 vg_assert(cls == 64 || cls == 128);
1338
1339 startaddr &= ~(cls - 1);
1340 for (addr = startaddr; addr < endaddr; addr += cls) {
1341 __asm__ __volatile__("dc cvau, %0" : : "r" (addr));
1342 }
1343 __asm__ __volatile__("dsb ish");
1344 # endif
1345 }
1346
1347 /*--------------------------------------------------------------------*/
1348 /*--- end ---*/
1349 /*--------------------------------------------------------------------*/
1350