sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1 | /* -*- c -*- |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2 | ---------------------------------------------------------------- |
| 3 | |
| 4 | Notice that the following BSD-style license applies to this one |
| 5 | file (valgrind.h) only. The entire rest of Valgrind is licensed |
| 6 | under the terms of the GNU General Public License, version 2. See |
| 7 | the COPYING file in the source distribution for details. |
| 8 | |
| 9 | ---------------------------------------------------------------- |
| 10 | |
njn | b9c427c | 2004-12-01 14:14:42 +0000 | [diff] [blame] | 11 | This file is part of Valgrind, a dynamic binary instrumentation |
| 12 | framework. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 13 | |
njn | 5361242 | 2005-03-12 16:22:54 +0000 | [diff] [blame] | 14 | Copyright (C) 2000-2005 Julian Seward. All rights reserved. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 15 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 16 | Redistribution and use in source and binary forms, with or without |
| 17 | modification, are permitted provided that the following conditions |
| 18 | are met: |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 19 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 20 | 1. Redistributions of source code must retain the above copyright |
| 21 | notice, this list of conditions and the following disclaimer. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 22 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 23 | 2. The origin of this software must not be misrepresented; you must |
| 24 | not claim that you wrote the original software. If you use this |
| 25 | software in a product, an acknowledgment in the product |
| 26 | documentation would be appreciated but is not required. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 27 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 28 | 3. Altered source versions must be plainly marked as such, and must |
| 29 | not be misrepresented as being the original software. |
| 30 | |
| 31 | 4. The name of the author may not be used to endorse or promote |
| 32 | products derived from this software without specific prior written |
| 33 | permission. |
| 34 | |
| 35 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
| 36 | OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 37 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 38 | ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 39 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 40 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 41 | GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 42 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 43 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 44 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 45 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 46 | |
| 47 | ---------------------------------------------------------------- |
| 48 | |
| 49 | Notice that the above BSD-style license applies to this one file |
| 50 | (valgrind.h) only. The entire rest of Valgrind is licensed under |
| 51 | the terms of the GNU General Public License, version 2. See the |
| 52 | COPYING file in the source distribution for details. |
| 53 | |
| 54 | ---------------------------------------------------------------- |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 55 | */ |
| 56 | |
| 57 | |
njn | 30d76c6 | 2005-06-18 15:07:39 +0000 | [diff] [blame] | 58 | /* This file is for inclusion into client (your!) code. |
| 59 | |
| 60 | You can use these macros to manipulate and query Valgrind's |
| 61 | execution inside your own programs. |
| 62 | |
| 63 | The resulting executables will still run without Valgrind, just a |
| 64 | little bit more slowly than they otherwise would, but otherwise |
| 65 | unchanged. When not running on valgrind, each client request |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 66 | consumes very few (eg. 7) instructions, so the resulting performance |
njn | 30d76c6 | 2005-06-18 15:07:39 +0000 | [diff] [blame] | 67 | loss is negligible unless you plan to execute client requests |
| 68 | millions of times per second. Nevertheless, if that is still a |
| 69 | problem, you can compile with the NVALGRIND symbol defined (gcc |
| 70 | -DNVALGRIND) so that client requests are not even compiled in. */ |
| 71 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 72 | #ifndef __VALGRIND_H |
| 73 | #define __VALGRIND_H |
| 74 | |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 75 | #include <stdarg.h> |
| 76 | |
njn | 3dd0a91 | 2005-06-28 19:44:10 +0000 | [diff] [blame] | 77 | /* Nb: this file might be included in a file compiled with -ansi. So |
| 78 | we can't use C++ style "//" comments nor the "asm" keyword (instead |
| 79 | use "__asm__"). */ |
| 80 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 81 | /* Derive some tags indicating what the target architecture is. Note |
| 82 | that in this file we're using the compiler's CPP symbols for |
| 83 | identifying architectures, which are different to the ones we use |
| 84 | within the rest of Valgrind. Note, __powerpc__ is active for both |
| 85 | 32 and 64-bit PPC, whereas __powerpc64__ is only active for the |
| 86 | latter. */ |
| 87 | #undef ARCH_x86 |
| 88 | #undef ARCH_amd64 |
| 89 | #undef ARCH_ppc32 |
| 90 | #undef ARCH_ppc64 |
| 91 | |
| 92 | #if defined(__i386__) |
| 93 | # define ARCH_x86 1 |
| 94 | #elif defined(__x86_64__) |
| 95 | # define ARCH_amd64 1 |
| 96 | #elif defined(__powerpc__) && !defined(__powerpc64__) |
| 97 | # define ARCH_ppc32 1 |
| 98 | #elif defined(__powerpc__) && defined(__powerpc64__) |
| 99 | # define ARCH_ppc64 1 |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 100 | #endif |
| 101 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 102 | /* If we're not compiling for our target architecture, don't generate |
| 103 | any inline asms. */ |
| 104 | #if !defined(ARCH_x86) && !defined(ARCH_amd64) \ |
| 105 | && !defined(ARCH_ppc32) && !defined(ARCH_ppc64) |
| 106 | # if !defined(NVALGRIND) |
| 107 | # define NVALGRIND 1 |
| 108 | # endif |
| 109 | #endif |
| 110 | |
| 111 | |
njn | 30d76c6 | 2005-06-18 15:07:39 +0000 | [diff] [blame] | 112 | /* ------------------------------------------------------------------ */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 113 | /* ARCHITECTURE SPECIFICS for SPECIAL INSTRUCTIONS. There is nothing */ |
| 114 | /* in here of use to end-users -- skip to the next section. */ |
njn | 30d76c6 | 2005-06-18 15:07:39 +0000 | [diff] [blame] | 115 | /* ------------------------------------------------------------------ */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 116 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 117 | #if defined(NVALGRIND) |
njn | 26aba4d | 2005-05-16 13:31:23 +0000 | [diff] [blame] | 118 | |
| 119 | /* Define NVALGRIND to completely remove the Valgrind magic sequence |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 120 | from the compiled code (analogous to NDEBUG's effects on |
| 121 | assert()) */ |
| 122 | #define VALGRIND_DO_CLIENT_REQUEST( \ |
| 123 | _zzq_rlval, _zzq_default, _zzq_request, \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 124 | _zzq_arg1, _zzq_arg2, _zzq_arg3, _zzq_arg4, _zzq_arg5) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 125 | { \ |
| 126 | (_zzq_rlval) = (_zzq_default); \ |
njn | 26aba4d | 2005-05-16 13:31:23 +0000 | [diff] [blame] | 127 | } |
| 128 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 129 | #else /* ! NVALGRIND */ |
nethercote | e90c683 | 2004-10-18 18:07:49 +0000 | [diff] [blame] | 130 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 131 | /* The following defines the magic code sequences which the JITter |
| 132 | spots and handles magically. Don't look too closely at them as |
| 133 | they will rot your brain. |
| 134 | |
| 135 | The assembly code sequences for all architectures is in this one |
| 136 | file. This is because this file must be stand-alone, and we don't |
| 137 | want to have multiple files. |
| 138 | |
| 139 | For VALGRIND_DO_CLIENT_REQUEST, we must ensure that the default |
| 140 | value gets put in the return slot, so that everything works when |
| 141 | this is executed not under Valgrind. Args are passed in a memory |
| 142 | block, and so there's no intrinsic limit to the number that could |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 143 | be passed, but it's currently five. |
nethercote | e90c683 | 2004-10-18 18:07:49 +0000 | [diff] [blame] | 144 | |
nethercote | 5426544 | 2004-10-26 12:56:58 +0000 | [diff] [blame] | 145 | The macro args are: |
| 146 | _zzq_rlval result lvalue |
| 147 | _zzq_default default value (result returned when running on real CPU) |
| 148 | _zzq_request request code |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 149 | _zzq_arg1..5 request params |
nethercote | 5426544 | 2004-10-26 12:56:58 +0000 | [diff] [blame] | 150 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 151 | The other two macros are used to support function wrapping, and are |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 152 | a lot simpler. VALGRIND_GET_NR_CONTEXT returns the value of the |
| 153 | guest's NRADDR pseudo-register and whatever other information is |
| 154 | needed to safely run the call original from the wrapper: on |
| 155 | ppc64-linux, the R2 value at the divert point is also needed. This |
| 156 | information is abstracted into a user-visible type, OrigFn. |
| 157 | |
| 158 | VALGRIND_CALL_NOREDIR_* behaves the same as the following on the |
| 159 | guest, but guarantees that the branch instruction will not be |
| 160 | redirected: x86: call *%eax, amd64: call *%rax, ppc32/ppc64: |
| 161 | branch-and-link-to-r11. VALGRIND_CALL_NOREDIR is just text, not a |
| 162 | complete inline asm, since it needs to be combined with more magic |
| 163 | inline asm stuff to be useful. |
nethercote | e90c683 | 2004-10-18 18:07:49 +0000 | [diff] [blame] | 164 | */ |
| 165 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 166 | /* ---------------------------- x86 ---------------------------- */ |
sewardj | de4a4ab | 2005-03-23 13:10:32 +0000 | [diff] [blame] | 167 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 168 | #if defined(ARCH_x86) |
sewardj | c885844 | 2006-01-20 15:17:20 +0000 | [diff] [blame] | 169 | |
| 170 | typedef |
| 171 | struct { |
| 172 | unsigned int nraddr; /* where's the code? */ |
| 173 | } |
| 174 | OrigFn; |
| 175 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 176 | #define __SPECIAL_INSTRUCTION_PREAMBLE \ |
| 177 | "roll $3, %%edi ; roll $13, %%edi\n\t" \ |
sewardj | 1a85f4f | 2006-01-12 21:15:35 +0000 | [diff] [blame] | 178 | "roll $29, %%edi ; roll $19, %%edi\n\t" |
sewardj | de4a4ab | 2005-03-23 13:10:32 +0000 | [diff] [blame] | 179 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 180 | #define VALGRIND_DO_CLIENT_REQUEST( \ |
| 181 | _zzq_rlval, _zzq_default, _zzq_request, \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 182 | _zzq_arg1, _zzq_arg2, _zzq_arg3, _zzq_arg4, _zzq_arg5) \ |
| 183 | { volatile unsigned int _zzq_args[6]; \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 184 | volatile unsigned int _zzq_result; \ |
| 185 | _zzq_args[0] = (unsigned int)(_zzq_request); \ |
| 186 | _zzq_args[1] = (unsigned int)(_zzq_arg1); \ |
| 187 | _zzq_args[2] = (unsigned int)(_zzq_arg2); \ |
| 188 | _zzq_args[3] = (unsigned int)(_zzq_arg3); \ |
| 189 | _zzq_args[4] = (unsigned int)(_zzq_arg4); \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 190 | _zzq_args[5] = (unsigned int)(_zzq_arg5); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 191 | __asm__ volatile(__SPECIAL_INSTRUCTION_PREAMBLE \ |
| 192 | /* %EDX = client_request ( %EAX ) */ \ |
| 193 | "xchgl %%ebx,%%ebx" \ |
| 194 | : "=d" (_zzq_result) \ |
| 195 | : "a" (&_zzq_args[0]), "0" (_zzq_default) \ |
| 196 | : "cc", "memory" \ |
| 197 | ); \ |
| 198 | _zzq_rlval = _zzq_result; \ |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 199 | } |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 200 | |
sewardj | c885844 | 2006-01-20 15:17:20 +0000 | [diff] [blame] | 201 | #define VALGRIND_GET_NR_CONTEXT(_zzq_rlval) \ |
| 202 | { volatile OrigFn* _zzq_orig = &(_zzq_rlval); \ |
| 203 | volatile unsigned int __addr; \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 204 | __asm__ volatile(__SPECIAL_INSTRUCTION_PREAMBLE \ |
| 205 | /* %EAX = guest_NRADDR */ \ |
| 206 | "xchgl %%ecx,%%ecx" \ |
| 207 | : "=a" (__addr) \ |
| 208 | : \ |
| 209 | : "cc", "memory" \ |
| 210 | ); \ |
sewardj | c885844 | 2006-01-20 15:17:20 +0000 | [diff] [blame] | 211 | _zzq_orig->nraddr = __addr; \ |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 212 | } |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 213 | |
| 214 | #define VALGRIND_CALL_NOREDIR_EAX \ |
| 215 | __SPECIAL_INSTRUCTION_PREAMBLE \ |
| 216 | /* call-noredir *%EAX */ \ |
| 217 | "xchgl %%edx,%%edx\n\t" |
| 218 | #endif /* ARCH_x86 */ |
| 219 | |
| 220 | /* --------------------------- amd64 --------------------------- */ |
| 221 | |
| 222 | #if defined(ARCH_amd64) |
sewardj | c885844 | 2006-01-20 15:17:20 +0000 | [diff] [blame] | 223 | |
| 224 | typedef |
| 225 | struct { |
| 226 | unsigned long long int nraddr; /* where's the code? */ |
| 227 | } |
| 228 | OrigFn; |
| 229 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 230 | #define __SPECIAL_INSTRUCTION_PREAMBLE \ |
| 231 | "rolq $3, %%rdi ; rolq $13, %%rdi\n\t" \ |
sewardj | 1a85f4f | 2006-01-12 21:15:35 +0000 | [diff] [blame] | 232 | "rolq $61, %%rdi ; rolq $51, %%rdi\n\t" |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 233 | |
| 234 | #define VALGRIND_DO_CLIENT_REQUEST( \ |
| 235 | _zzq_rlval, _zzq_default, _zzq_request, \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 236 | _zzq_arg1, _zzq_arg2, _zzq_arg3, _zzq_arg4, _zzq_arg5) \ |
| 237 | { volatile unsigned long long int _zzq_args[6]; \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 238 | volatile unsigned long long int _zzq_result; \ |
| 239 | _zzq_args[0] = (unsigned long long int)(_zzq_request); \ |
| 240 | _zzq_args[1] = (unsigned long long int)(_zzq_arg1); \ |
| 241 | _zzq_args[2] = (unsigned long long int)(_zzq_arg2); \ |
| 242 | _zzq_args[3] = (unsigned long long int)(_zzq_arg3); \ |
| 243 | _zzq_args[4] = (unsigned long long int)(_zzq_arg4); \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 244 | _zzq_args[5] = (unsigned long long int)(_zzq_arg5); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 245 | __asm__ volatile(__SPECIAL_INSTRUCTION_PREAMBLE \ |
| 246 | /* %RDX = client_request ( %RAX ) */ \ |
| 247 | "xchgq %%rbx,%%rbx" \ |
| 248 | : "=d" (_zzq_result) \ |
| 249 | : "a" (&_zzq_args[0]), "0" (_zzq_default) \ |
| 250 | : "cc", "memory" \ |
| 251 | ); \ |
| 252 | _zzq_rlval = _zzq_result; \ |
| 253 | } |
| 254 | |
sewardj | c885844 | 2006-01-20 15:17:20 +0000 | [diff] [blame] | 255 | #define VALGRIND_GET_NR_CONTEXT(_zzq_rlval) \ |
| 256 | { volatile OrigFn* _zzq_orig = &(_zzq_rlval); \ |
| 257 | volatile unsigned long long int __addr; \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 258 | __asm__ volatile(__SPECIAL_INSTRUCTION_PREAMBLE \ |
| 259 | /* %RAX = guest_NRADDR */ \ |
| 260 | "xchgq %%rcx,%%rcx" \ |
| 261 | : "=a" (__addr) \ |
| 262 | : \ |
| 263 | : "cc", "memory" \ |
| 264 | ); \ |
sewardj | c885844 | 2006-01-20 15:17:20 +0000 | [diff] [blame] | 265 | _zzq_orig->nraddr = __addr; \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | #define VALGRIND_CALL_NOREDIR_RAX \ |
| 269 | __SPECIAL_INSTRUCTION_PREAMBLE \ |
| 270 | /* call-noredir *%RAX */ \ |
| 271 | "xchgq %%rdx,%%rdx\n\t" |
| 272 | #endif /* ARCH_amd64 */ |
| 273 | |
| 274 | /* --------------------------- ppc32 --------------------------- */ |
| 275 | |
| 276 | #if defined(ARCH_ppc32) |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 277 | |
| 278 | typedef |
| 279 | struct { |
sewardj | c885844 | 2006-01-20 15:17:20 +0000 | [diff] [blame] | 280 | unsigned int nraddr; /* where's the code? */ |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 281 | } |
| 282 | OrigFn; |
| 283 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 284 | #define __SPECIAL_INSTRUCTION_PREAMBLE \ |
| 285 | "rlwinm 0,0,3,0,0 ; rlwinm 0,0,13,0,0\n\t" \ |
sewardj | 1a85f4f | 2006-01-12 21:15:35 +0000 | [diff] [blame] | 286 | "rlwinm 0,0,29,0,0 ; rlwinm 0,0,19,0,0\n\t" |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 287 | |
| 288 | #define VALGRIND_DO_CLIENT_REQUEST( \ |
| 289 | _zzq_rlval, _zzq_default, _zzq_request, \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 290 | _zzq_arg1, _zzq_arg2, _zzq_arg3, _zzq_arg4, _zzq_arg5) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 291 | \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 292 | { unsigned int _zzq_args[6]; \ |
sewardj | 1a85f4f | 2006-01-12 21:15:35 +0000 | [diff] [blame] | 293 | register unsigned int _zzq_result __asm__("r3"); \ |
| 294 | register unsigned int* _zzq_ptr __asm__("r4"); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 295 | _zzq_args[0] = (unsigned int)(_zzq_request); \ |
| 296 | _zzq_args[1] = (unsigned int)(_zzq_arg1); \ |
| 297 | _zzq_args[2] = (unsigned int)(_zzq_arg2); \ |
| 298 | _zzq_args[3] = (unsigned int)(_zzq_arg3); \ |
| 299 | _zzq_args[4] = (unsigned int)(_zzq_arg4); \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 300 | _zzq_args[5] = (unsigned int)(_zzq_arg5); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 301 | _zzq_ptr = _zzq_args; \ |
| 302 | __asm__ volatile(__SPECIAL_INSTRUCTION_PREAMBLE \ |
| 303 | /* %R3 = client_request ( %R4 ) */ \ |
| 304 | "or 1,1,1" \ |
| 305 | : "=r" (_zzq_result) \ |
| 306 | : "0" (_zzq_default), "r" (_zzq_ptr) \ |
| 307 | : "cc", "memory"); \ |
| 308 | _zzq_rlval = _zzq_result; \ |
| 309 | } |
| 310 | |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 311 | #define VALGRIND_GET_NR_CONTEXT(_zzq_rlval) \ |
| 312 | { volatile OrigFn* _zzq_orig = &(_zzq_rlval); \ |
| 313 | register unsigned int __addr __asm__("r3"); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 314 | __asm__ volatile(__SPECIAL_INSTRUCTION_PREAMBLE \ |
| 315 | /* %R3 = guest_NRADDR */ \ |
| 316 | "or 2,2,2" \ |
| 317 | : "=r" (__addr) \ |
| 318 | : \ |
| 319 | : "cc", "memory" \ |
| 320 | ); \ |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 321 | _zzq_orig->nraddr = __addr; \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | #define VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11 \ |
| 325 | __SPECIAL_INSTRUCTION_PREAMBLE \ |
| 326 | /* branch-and-link-to-noredir *%R11 */ \ |
| 327 | "or 3,3,3\n\t" |
| 328 | #endif /* ARCH_ppc32 */ |
| 329 | |
| 330 | /* --------------------------- ppc64 --------------------------- */ |
| 331 | |
| 332 | #if defined(ARCH_ppc64) |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 333 | |
| 334 | typedef |
| 335 | struct { |
| 336 | unsigned long long int nraddr; /* where's the code? */ |
| 337 | unsigned long long int r2; /* what tocptr do we need? */ |
| 338 | } |
| 339 | OrigFn; |
| 340 | |
sewardj | 1a85f4f | 2006-01-12 21:15:35 +0000 | [diff] [blame] | 341 | #define __SPECIAL_INSTRUCTION_PREAMBLE \ |
| 342 | "rotldi 0,0,3 ; rotldi 0,0,13\n\t" \ |
| 343 | "rotldi 0,0,61 ; rotldi 0,0,51\n\t" |
| 344 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 345 | #define VALGRIND_DO_CLIENT_REQUEST( \ |
| 346 | _zzq_rlval, _zzq_default, _zzq_request, \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 347 | _zzq_arg1, _zzq_arg2, _zzq_arg3, _zzq_arg4, _zzq_arg5) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 348 | \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 349 | { unsigned long long int _zzq_args[6]; \ |
sewardj | 1a85f4f | 2006-01-12 21:15:35 +0000 | [diff] [blame] | 350 | register unsigned long long int _zzq_result __asm__("r3"); \ |
| 351 | register unsigned long long int* _zzq_ptr __asm__("r4"); \ |
| 352 | _zzq_args[0] = (unsigned long long int)(_zzq_request); \ |
| 353 | _zzq_args[1] = (unsigned long long int)(_zzq_arg1); \ |
| 354 | _zzq_args[2] = (unsigned long long int)(_zzq_arg2); \ |
| 355 | _zzq_args[3] = (unsigned long long int)(_zzq_arg3); \ |
| 356 | _zzq_args[4] = (unsigned long long int)(_zzq_arg4); \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 357 | _zzq_args[5] = (unsigned long long int)(_zzq_arg5); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 358 | _zzq_ptr = _zzq_args; \ |
sewardj | 1a85f4f | 2006-01-12 21:15:35 +0000 | [diff] [blame] | 359 | __asm__ volatile(__SPECIAL_INSTRUCTION_PREAMBLE \ |
| 360 | /* %R3 = client_request ( %R4 ) */ \ |
| 361 | "or 1,1,1" \ |
| 362 | : "=r" (_zzq_result) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 363 | : "0" (_zzq_default), "r" (_zzq_ptr) \ |
sewardj | 1a85f4f | 2006-01-12 21:15:35 +0000 | [diff] [blame] | 364 | : "cc", "memory"); \ |
| 365 | _zzq_rlval = _zzq_result; \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 366 | } |
sewardj | 1a85f4f | 2006-01-12 21:15:35 +0000 | [diff] [blame] | 367 | |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 368 | #define VALGRIND_GET_NR_CONTEXT(_zzq_rlval) \ |
| 369 | { volatile OrigFn* _zzq_orig = &(_zzq_rlval); \ |
| 370 | register unsigned long long int __addr __asm__("r3"); \ |
sewardj | 1a85f4f | 2006-01-12 21:15:35 +0000 | [diff] [blame] | 371 | __asm__ volatile(__SPECIAL_INSTRUCTION_PREAMBLE \ |
| 372 | /* %R3 = guest_NRADDR */ \ |
| 373 | "or 2,2,2" \ |
| 374 | : "=r" (__addr) \ |
| 375 | : \ |
| 376 | : "cc", "memory" \ |
| 377 | ); \ |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 378 | _zzq_orig->nraddr = __addr; \ |
| 379 | __asm__ volatile(__SPECIAL_INSTRUCTION_PREAMBLE \ |
| 380 | /* %R3 = guest_NRADDR_GPR2 */ \ |
| 381 | "or 4,4,4" \ |
| 382 | : "=r" (__addr) \ |
| 383 | : \ |
| 384 | : "cc", "memory" \ |
| 385 | ); \ |
| 386 | _zzq_orig->r2 = __addr; \ |
sewardj | 1a85f4f | 2006-01-12 21:15:35 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | #define VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11 \ |
| 390 | __SPECIAL_INSTRUCTION_PREAMBLE \ |
| 391 | /* branch-and-link-to-noredir *%R11 */ \ |
| 392 | "or 3,3,3\n\t" |
| 393 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 394 | #endif /* ARCH_ppc64 */ |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 395 | |
njn | 3dd0a91 | 2005-06-28 19:44:10 +0000 | [diff] [blame] | 396 | /* Insert assembly code for other architectures here... */ |
njn | 26aba4d | 2005-05-16 13:31:23 +0000 | [diff] [blame] | 397 | |
sewardj | 37091fb | 2002-11-16 11:06:50 +0000 | [diff] [blame] | 398 | #endif /* NVALGRIND */ |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 399 | |
nethercote | 69d9c46 | 2004-10-26 13:00:12 +0000 | [diff] [blame] | 400 | |
njn | 30d76c6 | 2005-06-18 15:07:39 +0000 | [diff] [blame] | 401 | /* ------------------------------------------------------------------ */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 402 | /* ARCHITECTURE SPECIFICS for FUNCTION WRAPPING. This is all very */ |
| 403 | /* ugly. It's the least-worst tradeoff I can think of. */ |
| 404 | /* ------------------------------------------------------------------ */ |
| 405 | |
| 406 | /* This section defines magic (a.k.a appalling-hack) macros for doing |
| 407 | guaranteed-no-redirection macros, so as to get from function |
| 408 | wrappers to the functions they are wrapping. The whole point is to |
| 409 | construct standard call sequences, but to do the call itself with a |
| 410 | special no-redirect call pseudo-instruction that the JIT |
| 411 | understands and handles specially. This section is long and |
| 412 | repetitious, and I can't see a way to make it shorter. |
| 413 | |
| 414 | The naming scheme is as follows: |
| 415 | |
| 416 | CALL_FN_{W,v}_{v,W,WW,WWW,WWWW,5W,6W,7W,etc} |
| 417 | |
| 418 | 'W' stands for "word" and 'v' for "void". Hence there are |
| 419 | different macros for calling arity 0, 1, 2, 3, 4, etc, functions, |
| 420 | and for each, the possibility of returning a word-typed result, or |
| 421 | no result. |
| 422 | */ |
| 423 | |
| 424 | /* Use these to write the name of your wrapper. NOTE: duplicates |
| 425 | VG_WRAP_FUNCTION_Z{U,Z} in pub_tool_redir.h. */ |
| 426 | |
| 427 | #define I_WRAP_SONAME_FNNAME_ZU(soname,fnname) \ |
| 428 | _vgwZU_##soname##_##fnname |
| 429 | |
| 430 | #define I_WRAP_SONAME_FNNAME_ZZ(soname,fnname) \ |
| 431 | _vgwZZ_##soname##_##fnname |
| 432 | |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 433 | /* Use this macro from within a wrapper function to collect the |
| 434 | context (address and possibly other info) of the original function. |
| 435 | Once you have that you can then use it in one of the CALL_FN_ |
| 436 | macros. The type of the argument _lval is OrigFn. */ |
| 437 | #define VALGRIND_GET_ORIG_FN(_lval) VALGRIND_GET_NR_CONTEXT(_lval) |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 438 | |
| 439 | /* Derivatives of the main macros below, for calling functions |
| 440 | returning void. */ |
| 441 | |
| 442 | #define CALL_FN_v_v(fnptr) \ |
| 443 | do { volatile unsigned long _junk; \ |
| 444 | CALL_FN_W_v(_junk,fnptr); } while (0) |
| 445 | |
| 446 | #define CALL_FN_v_W(fnptr, arg1) \ |
| 447 | do { volatile unsigned long _junk; \ |
| 448 | CALL_FN_W_W(_junk,fnptr,arg1); } while (0) |
| 449 | |
| 450 | #define CALL_FN_v_WW(fnptr, arg1,arg2) \ |
| 451 | do { volatile unsigned long _junk; \ |
| 452 | CALL_FN_W_WW(_junk,fnptr,arg1,arg2); } while (0) |
| 453 | |
| 454 | /* ---------------------------- x86 ---------------------------- */ |
| 455 | |
| 456 | #if defined(ARCH_x86) |
| 457 | |
| 458 | /* These regs are trashed by the hidden call. No need to mention eax |
| 459 | as gcc can already see that, plus causes gcc to bomb. */ |
| 460 | #define __CALLER_SAVED_REGS /*"eax"*/ "ecx", "edx" |
| 461 | |
| 462 | /* These CALL_FN_ macros assume that on x86-linux, sizeof(unsigned |
| 463 | long) == 4. */ |
| 464 | |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 465 | #define CALL_FN_W_v(lval, orig) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 466 | do { \ |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 467 | volatile OrigFn _orig = (orig); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 468 | volatile unsigned long _argvec[1]; \ |
| 469 | volatile unsigned long _res; \ |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 470 | _argvec[0] = (unsigned long)_orig.nraddr; \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 471 | __asm__ volatile( \ |
| 472 | "movl (%%eax), %%eax\n\t" /* target->%eax */ \ |
| 473 | VALGRIND_CALL_NOREDIR_EAX \ |
| 474 | : /*out*/ "=a" (_res) \ |
| 475 | : /*in*/ "a" (&_argvec[0]) \ |
| 476 | : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \ |
| 477 | ); \ |
| 478 | lval = (__typeof__(lval)) _res; \ |
| 479 | } while (0) |
| 480 | |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 481 | #define CALL_FN_W_W(lval, orig, arg1) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 482 | do { \ |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 483 | volatile OrigFn _orig = (orig); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 484 | volatile unsigned long _argvec[2]; \ |
| 485 | volatile unsigned long _res; \ |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 486 | _argvec[0] = (unsigned long)_orig.nraddr; \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 487 | _argvec[1] = (unsigned long)(arg1); \ |
| 488 | __asm__ volatile( \ |
| 489 | "pushl 4(%%eax)\n\t" \ |
| 490 | "movl (%%eax), %%eax\n\t" /* target->%eax */ \ |
| 491 | VALGRIND_CALL_NOREDIR_EAX \ |
| 492 | "addl $4, %%esp\n" \ |
| 493 | : /*out*/ "=a" (_res) \ |
| 494 | : /*in*/ "a" (&_argvec[0]) \ |
| 495 | : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \ |
| 496 | ); \ |
| 497 | lval = (__typeof__(lval)) _res; \ |
| 498 | } while (0) |
| 499 | |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 500 | #define CALL_FN_W_WW(lval, orig, arg1,arg2) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 501 | do { \ |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 502 | volatile OrigFn _orig = (orig); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 503 | volatile unsigned long _argvec[3]; \ |
| 504 | volatile unsigned long _res; \ |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 505 | _argvec[0] = (unsigned long)_orig.nraddr; \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 506 | _argvec[1] = (unsigned long)(arg1); \ |
| 507 | _argvec[2] = (unsigned long)(arg2); \ |
| 508 | __asm__ volatile( \ |
| 509 | "pushl 8(%%eax)\n\t" \ |
| 510 | "pushl 4(%%eax)\n\t" \ |
| 511 | "movl (%%eax), %%eax\n\t" /* target->%eax */ \ |
| 512 | VALGRIND_CALL_NOREDIR_EAX \ |
| 513 | "addl $8, %%esp\n" \ |
| 514 | : /*out*/ "=a" (_res) \ |
| 515 | : /*in*/ "a" (&_argvec[0]) \ |
| 516 | : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \ |
| 517 | ); \ |
| 518 | lval = (__typeof__(lval)) _res; \ |
| 519 | } while (0) |
| 520 | |
sewardj | 9e8b07a | 2006-02-18 21:13:29 +0000 | [diff] [blame^] | 521 | #define CALL_FN_W_WWW(lval, orig, arg1,arg2,arg3) \ |
| 522 | do { \ |
| 523 | volatile OrigFn _orig = (orig); \ |
| 524 | volatile unsigned long _argvec[4]; \ |
| 525 | volatile unsigned long _res; \ |
| 526 | _argvec[0] = (unsigned long)_orig.nraddr; \ |
| 527 | _argvec[1] = (unsigned long)(arg1); \ |
| 528 | _argvec[2] = (unsigned long)(arg2); \ |
| 529 | _argvec[3] = (unsigned long)(arg3); \ |
| 530 | __asm__ volatile( \ |
| 531 | "pushl 12(%%eax)\n\t" \ |
| 532 | "pushl 8(%%eax)\n\t" \ |
| 533 | "pushl 4(%%eax)\n\t" \ |
| 534 | "movl (%%eax), %%eax\n\t" /* target->%eax */ \ |
| 535 | VALGRIND_CALL_NOREDIR_EAX \ |
| 536 | "addl $12, %%esp\n" \ |
| 537 | : /*out*/ "=a" (_res) \ |
| 538 | : /*in*/ "a" (&_argvec[0]) \ |
| 539 | : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \ |
| 540 | ); \ |
| 541 | lval = (__typeof__(lval)) _res; \ |
| 542 | } while (0) |
| 543 | |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 544 | #define CALL_FN_W_WWWW(lval, orig, arg1,arg2,arg3,arg4) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 545 | do { \ |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 546 | volatile OrigFn _orig = (orig); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 547 | volatile unsigned long _argvec[5]; \ |
| 548 | volatile unsigned long _res; \ |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 549 | _argvec[0] = (unsigned long)_orig.nraddr; \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 550 | _argvec[1] = (unsigned long)(arg1); \ |
| 551 | _argvec[2] = (unsigned long)(arg2); \ |
| 552 | _argvec[3] = (unsigned long)(arg3); \ |
| 553 | _argvec[4] = (unsigned long)(arg4); \ |
| 554 | __asm__ volatile( \ |
| 555 | "pushl 16(%%eax)\n\t" \ |
| 556 | "pushl 12(%%eax)\n\t" \ |
| 557 | "pushl 8(%%eax)\n\t" \ |
| 558 | "pushl 4(%%eax)\n\t" \ |
| 559 | "movl (%%eax), %%eax\n\t" /* target->%eax */ \ |
| 560 | VALGRIND_CALL_NOREDIR_EAX \ |
| 561 | "addl $16, %%esp\n" \ |
| 562 | : /*out*/ "=a" (_res) \ |
| 563 | : /*in*/ "a" (&_argvec[0]) \ |
| 564 | : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \ |
| 565 | ); \ |
| 566 | lval = (__typeof__(lval)) _res; \ |
| 567 | } while (0) |
| 568 | |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 569 | #define CALL_FN_W_5W(lval, orig, arg1,arg2,arg3,arg4,arg5) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 570 | do { \ |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 571 | volatile OrigFn _orig = (orig); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 572 | volatile unsigned long _argvec[6]; \ |
| 573 | volatile unsigned long _res; \ |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 574 | _argvec[0] = (unsigned long)_orig.nraddr; \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 575 | _argvec[1] = (unsigned long)(arg1); \ |
| 576 | _argvec[2] = (unsigned long)(arg2); \ |
| 577 | _argvec[3] = (unsigned long)(arg3); \ |
| 578 | _argvec[4] = (unsigned long)(arg4); \ |
| 579 | _argvec[5] = (unsigned long)(arg5); \ |
| 580 | __asm__ volatile( \ |
| 581 | "pushl 20(%%eax)\n\t" \ |
| 582 | "pushl 16(%%eax)\n\t" \ |
| 583 | "pushl 12(%%eax)\n\t" \ |
| 584 | "pushl 8(%%eax)\n\t" \ |
| 585 | "pushl 4(%%eax)\n\t" \ |
| 586 | "movl (%%eax), %%eax\n\t" /* target->%eax */ \ |
| 587 | VALGRIND_CALL_NOREDIR_EAX \ |
| 588 | "addl $20, %%esp\n" \ |
| 589 | : /*out*/ "=a" (_res) \ |
| 590 | : /*in*/ "a" (&_argvec[0]) \ |
| 591 | : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \ |
| 592 | ); \ |
| 593 | lval = (__typeof__(lval)) _res; \ |
| 594 | } while (0) |
| 595 | |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 596 | #define CALL_FN_W_6W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 597 | do { \ |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 598 | volatile OrigFn _orig = (orig); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 599 | volatile unsigned long _argvec[7]; \ |
| 600 | volatile unsigned long _res; \ |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 601 | _argvec[0] = (unsigned long)_orig.nraddr; \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 602 | _argvec[1] = (unsigned long)(arg1); \ |
| 603 | _argvec[2] = (unsigned long)(arg2); \ |
| 604 | _argvec[3] = (unsigned long)(arg3); \ |
| 605 | _argvec[4] = (unsigned long)(arg4); \ |
| 606 | _argvec[5] = (unsigned long)(arg5); \ |
| 607 | _argvec[6] = (unsigned long)(arg6); \ |
| 608 | __asm__ volatile( \ |
| 609 | "pushl 24(%%eax)\n\t" \ |
| 610 | "pushl 20(%%eax)\n\t" \ |
| 611 | "pushl 16(%%eax)\n\t" \ |
| 612 | "pushl 12(%%eax)\n\t" \ |
| 613 | "pushl 8(%%eax)\n\t" \ |
| 614 | "pushl 4(%%eax)\n\t" \ |
| 615 | "movl (%%eax), %%eax\n\t" /* target->%eax */ \ |
| 616 | VALGRIND_CALL_NOREDIR_EAX \ |
| 617 | "addl $24, %%esp\n" \ |
| 618 | : /*out*/ "=a" (_res) \ |
| 619 | : /*in*/ "a" (&_argvec[0]) \ |
| 620 | : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \ |
| 621 | ); \ |
| 622 | lval = (__typeof__(lval)) _res; \ |
| 623 | } while (0) |
| 624 | |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 625 | #define CALL_FN_W_7W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6, \ |
| 626 | arg7) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 627 | do { \ |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 628 | volatile OrigFn _orig = (orig); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 629 | volatile unsigned long _argvec[8]; \ |
| 630 | volatile unsigned long _res; \ |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 631 | _argvec[0] = (unsigned long)_orig.nraddr; \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 632 | _argvec[1] = (unsigned long)(arg1); \ |
| 633 | _argvec[2] = (unsigned long)(arg2); \ |
| 634 | _argvec[3] = (unsigned long)(arg3); \ |
| 635 | _argvec[4] = (unsigned long)(arg4); \ |
| 636 | _argvec[5] = (unsigned long)(arg5); \ |
| 637 | _argvec[6] = (unsigned long)(arg6); \ |
| 638 | _argvec[7] = (unsigned long)(arg7); \ |
| 639 | __asm__ volatile( \ |
| 640 | "pushl 28(%%eax)\n\t" \ |
| 641 | "pushl 24(%%eax)\n\t" \ |
| 642 | "pushl 20(%%eax)\n\t" \ |
| 643 | "pushl 16(%%eax)\n\t" \ |
| 644 | "pushl 12(%%eax)\n\t" \ |
| 645 | "pushl 8(%%eax)\n\t" \ |
| 646 | "pushl 4(%%eax)\n\t" \ |
| 647 | "movl (%%eax), %%eax\n\t" /* target->%eax */ \ |
| 648 | VALGRIND_CALL_NOREDIR_EAX \ |
| 649 | "addl $28, %%esp\n" \ |
| 650 | : /*out*/ "=a" (_res) \ |
| 651 | : /*in*/ "a" (&_argvec[0]) \ |
| 652 | : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \ |
| 653 | ); \ |
| 654 | lval = (__typeof__(lval)) _res; \ |
| 655 | } while (0) |
| 656 | |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 657 | #define CALL_FN_W_8W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6, \ |
| 658 | arg7,arg8) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 659 | do { \ |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 660 | volatile OrigFn _orig = (orig); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 661 | volatile unsigned long _argvec[9]; \ |
| 662 | volatile unsigned long _res; \ |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 663 | _argvec[0] = (unsigned long)_orig.nraddr; \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 664 | _argvec[1] = (unsigned long)(arg1); \ |
| 665 | _argvec[2] = (unsigned long)(arg2); \ |
| 666 | _argvec[3] = (unsigned long)(arg3); \ |
| 667 | _argvec[4] = (unsigned long)(arg4); \ |
| 668 | _argvec[5] = (unsigned long)(arg5); \ |
| 669 | _argvec[6] = (unsigned long)(arg6); \ |
| 670 | _argvec[7] = (unsigned long)(arg7); \ |
| 671 | _argvec[8] = (unsigned long)(arg8); \ |
| 672 | __asm__ volatile( \ |
| 673 | "pushl 32(%%eax)\n\t" \ |
| 674 | "pushl 28(%%eax)\n\t" \ |
| 675 | "pushl 24(%%eax)\n\t" \ |
| 676 | "pushl 20(%%eax)\n\t" \ |
| 677 | "pushl 16(%%eax)\n\t" \ |
| 678 | "pushl 12(%%eax)\n\t" \ |
| 679 | "pushl 8(%%eax)\n\t" \ |
| 680 | "pushl 4(%%eax)\n\t" \ |
| 681 | "movl (%%eax), %%eax\n\t" /* target->%eax */ \ |
| 682 | VALGRIND_CALL_NOREDIR_EAX \ |
| 683 | "addl $32, %%esp\n" \ |
| 684 | : /*out*/ "=a" (_res) \ |
| 685 | : /*in*/ "a" (&_argvec[0]) \ |
| 686 | : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \ |
| 687 | ); \ |
| 688 | lval = (__typeof__(lval)) _res; \ |
| 689 | } while (0) |
| 690 | |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 691 | #define CALL_FN_W_12W(lval, orig, arg1,arg2,arg3,arg4,arg5, \ |
| 692 | arg6,arg7,arg8,arg9,arg10, \ |
| 693 | arg11,arg12) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 694 | do { \ |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 695 | volatile OrigFn _orig = (orig); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 696 | volatile unsigned long _argvec[13]; \ |
| 697 | volatile unsigned long _res; \ |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 698 | _argvec[0] = (unsigned long)_orig.nraddr; \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 699 | _argvec[1] = (unsigned long)(arg1); \ |
| 700 | _argvec[2] = (unsigned long)(arg2); \ |
| 701 | _argvec[3] = (unsigned long)(arg3); \ |
| 702 | _argvec[4] = (unsigned long)(arg4); \ |
| 703 | _argvec[5] = (unsigned long)(arg5); \ |
| 704 | _argvec[6] = (unsigned long)(arg6); \ |
| 705 | _argvec[7] = (unsigned long)(arg7); \ |
| 706 | _argvec[8] = (unsigned long)(arg8); \ |
| 707 | _argvec[9] = (unsigned long)(arg9); \ |
| 708 | _argvec[10] = (unsigned long)(arg10); \ |
| 709 | _argvec[11] = (unsigned long)(arg11); \ |
| 710 | _argvec[12] = (unsigned long)(arg12); \ |
| 711 | __asm__ volatile( \ |
| 712 | "pushl 48(%%eax)\n\t" \ |
| 713 | "pushl 44(%%eax)\n\t" \ |
| 714 | "pushl 40(%%eax)\n\t" \ |
| 715 | "pushl 36(%%eax)\n\t" \ |
| 716 | "pushl 32(%%eax)\n\t" \ |
| 717 | "pushl 28(%%eax)\n\t" \ |
| 718 | "pushl 24(%%eax)\n\t" \ |
| 719 | "pushl 20(%%eax)\n\t" \ |
| 720 | "pushl 16(%%eax)\n\t" \ |
| 721 | "pushl 12(%%eax)\n\t" \ |
| 722 | "pushl 8(%%eax)\n\t" \ |
| 723 | "pushl 4(%%eax)\n\t" \ |
| 724 | "movl (%%eax), %%eax\n\t" /* target->%eax */ \ |
| 725 | VALGRIND_CALL_NOREDIR_EAX \ |
| 726 | "addl $48, %%esp\n" \ |
| 727 | : /*out*/ "=a" (_res) \ |
| 728 | : /*in*/ "a" (&_argvec[0]) \ |
| 729 | : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \ |
| 730 | ); \ |
| 731 | lval = (__typeof__(lval)) _res; \ |
| 732 | } while (0) |
| 733 | |
| 734 | #endif /* ARCH_x86 */ |
| 735 | |
| 736 | /* --------------------------- amd64 --------------------------- */ |
| 737 | |
| 738 | #if defined(ARCH_amd64) |
| 739 | |
| 740 | /* ARGREGS: rdi rsi rdx rcx r8 r9 (the rest on stack in R-to-L order) */ |
| 741 | |
| 742 | /* These regs are trashed by the hidden call. */ |
| 743 | #define __CALLER_SAVED_REGS /*"rax",*/ "rcx", "rdx", "rsi", \ |
| 744 | "rdi", "r8", "r9", "r10", "r11" |
| 745 | |
| 746 | /* These CALL_FN_ macros assume that on amd64-linux, sizeof(unsigned |
| 747 | long) == 8. */ |
| 748 | |
sewardj | c885844 | 2006-01-20 15:17:20 +0000 | [diff] [blame] | 749 | #define CALL_FN_W_v(lval, orig) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 750 | do { \ |
sewardj | c885844 | 2006-01-20 15:17:20 +0000 | [diff] [blame] | 751 | volatile OrigFn _orig = (orig); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 752 | volatile unsigned long _argvec[1]; \ |
| 753 | volatile unsigned long _res; \ |
sewardj | c885844 | 2006-01-20 15:17:20 +0000 | [diff] [blame] | 754 | _argvec[0] = (unsigned long)_orig.nraddr; \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 755 | __asm__ volatile( \ |
| 756 | "movq (%%rax), %%rax\n\t" /* target->%rax */ \ |
| 757 | VALGRIND_CALL_NOREDIR_RAX \ |
| 758 | : /*out*/ "=a" (_res) \ |
| 759 | : /*in*/ "a" (&_argvec[0]) \ |
| 760 | : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \ |
| 761 | ); \ |
| 762 | lval = (__typeof__(lval)) _res; \ |
| 763 | } while (0) |
| 764 | |
sewardj | c885844 | 2006-01-20 15:17:20 +0000 | [diff] [blame] | 765 | #define CALL_FN_W_W(lval, orig, arg1) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 766 | do { \ |
sewardj | c885844 | 2006-01-20 15:17:20 +0000 | [diff] [blame] | 767 | volatile OrigFn _orig = (orig); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 768 | volatile unsigned long _argvec[2]; \ |
| 769 | volatile unsigned long _res; \ |
sewardj | c885844 | 2006-01-20 15:17:20 +0000 | [diff] [blame] | 770 | _argvec[0] = (unsigned long)_orig.nraddr; \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 771 | _argvec[1] = (unsigned long)(arg1); \ |
| 772 | __asm__ volatile( \ |
| 773 | "movq 8(%%rax), %%rdi\n\t" \ |
| 774 | "movq (%%rax), %%rax\n\t" /* target->%rax */ \ |
| 775 | VALGRIND_CALL_NOREDIR_RAX \ |
| 776 | : /*out*/ "=a" (_res) \ |
| 777 | : /*in*/ "a" (&_argvec[0]) \ |
| 778 | : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \ |
| 779 | ); \ |
| 780 | lval = (__typeof__(lval)) _res; \ |
| 781 | } while (0) |
| 782 | |
sewardj | c885844 | 2006-01-20 15:17:20 +0000 | [diff] [blame] | 783 | #define CALL_FN_W_WW(lval, orig, arg1,arg2) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 784 | do { \ |
sewardj | c885844 | 2006-01-20 15:17:20 +0000 | [diff] [blame] | 785 | volatile OrigFn _orig = (orig); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 786 | volatile unsigned long _argvec[3]; \ |
| 787 | volatile unsigned long _res; \ |
sewardj | c885844 | 2006-01-20 15:17:20 +0000 | [diff] [blame] | 788 | _argvec[0] = (unsigned long)_orig.nraddr; \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 789 | _argvec[1] = (unsigned long)(arg1); \ |
| 790 | _argvec[2] = (unsigned long)(arg2); \ |
| 791 | __asm__ volatile( \ |
| 792 | "movq 16(%%rax), %%rsi\n\t" \ |
| 793 | "movq 8(%%rax), %%rdi\n\t" \ |
| 794 | "movq (%%rax), %%rax\n\t" /* target->%rax */ \ |
| 795 | VALGRIND_CALL_NOREDIR_RAX \ |
| 796 | : /*out*/ "=a" (_res) \ |
| 797 | : /*in*/ "a" (&_argvec[0]) \ |
| 798 | : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \ |
| 799 | ); \ |
| 800 | lval = (__typeof__(lval)) _res; \ |
| 801 | } while (0) |
| 802 | |
| 803 | #endif /* ARCH_amd64 */ |
| 804 | |
| 805 | /* --------------------------- ppc32 --------------------------- */ |
| 806 | |
| 807 | #if defined(ARCH_ppc32) |
| 808 | |
| 809 | /* ARGREGS: r3 r4 r5 r6 r7 r8 r9 r10 (the rest on stack somewhere) */ |
| 810 | |
| 811 | /* These regs are trashed by the hidden call. */ |
| 812 | #define __CALLER_SAVED_REGS "lr", \ |
| 813 | "r0", "r2", "r3", "r4", "r5", "r6", \ |
| 814 | "r7", "r8", "r9", "r10", "r11", "r12" |
| 815 | |
| 816 | /* These CALL_FN_ macros assume that on ppc32-linux, sizeof(unsigned |
| 817 | long) == 4. */ |
| 818 | |
sewardj | 38de099 | 2006-01-20 16:46:34 +0000 | [diff] [blame] | 819 | #define CALL_FN_W_v(lval, orig) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 820 | do { \ |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 821 | volatile OrigFn _orig = (orig); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 822 | volatile unsigned long _argvec[1]; \ |
| 823 | volatile unsigned long _res; \ |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 824 | _argvec[0] = (unsigned long)_orig.nraddr; \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 825 | __asm__ volatile( \ |
| 826 | "mr 11,%1\n\t" \ |
| 827 | "lwz 11,0(11)\n\t" /* target->r11 */ \ |
| 828 | VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11 \ |
| 829 | "mr %0,3" \ |
| 830 | : /*out*/ "=r" (_res) \ |
| 831 | : /*in*/ "r" (&_argvec[0]) \ |
| 832 | : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \ |
| 833 | ); \ |
| 834 | lval = (__typeof__(lval)) _res; \ |
| 835 | } while (0) |
| 836 | |
sewardj | 38de099 | 2006-01-20 16:46:34 +0000 | [diff] [blame] | 837 | #define CALL_FN_W_W(lval, orig, arg1) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 838 | do { \ |
sewardj | 38de099 | 2006-01-20 16:46:34 +0000 | [diff] [blame] | 839 | volatile OrigFn _orig = (orig); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 840 | volatile unsigned long _argvec[2]; \ |
| 841 | volatile unsigned long _res; \ |
sewardj | 38de099 | 2006-01-20 16:46:34 +0000 | [diff] [blame] | 842 | _argvec[0] = (unsigned long)_orig.nraddr; \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 843 | _argvec[1] = (unsigned long)arg1; \ |
| 844 | __asm__ volatile( \ |
| 845 | "mr 11,%1\n\t" \ |
| 846 | "lwz 3,4(11)\n\t" /* arg1->r3 */ \ |
| 847 | "lwz 11,0(11)\n\t" /* target->r11 */ \ |
| 848 | VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11 \ |
| 849 | "mr %0,3" \ |
| 850 | : /*out*/ "=r" (_res) \ |
| 851 | : /*in*/ "r" (&_argvec[0]) \ |
| 852 | : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \ |
| 853 | ); \ |
| 854 | lval = (__typeof__(lval)) _res; \ |
| 855 | } while (0) |
| 856 | |
sewardj | 38de099 | 2006-01-20 16:46:34 +0000 | [diff] [blame] | 857 | #define CALL_FN_W_WW(lval, orig, arg1,arg2) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 858 | do { \ |
sewardj | 38de099 | 2006-01-20 16:46:34 +0000 | [diff] [blame] | 859 | volatile OrigFn _orig = (orig); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 860 | volatile unsigned long _argvec[3]; \ |
| 861 | volatile unsigned long _res; \ |
sewardj | 38de099 | 2006-01-20 16:46:34 +0000 | [diff] [blame] | 862 | _argvec[0] = (unsigned long)_orig.nraddr; \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 863 | _argvec[1] = (unsigned long)arg1; \ |
| 864 | _argvec[2] = (unsigned long)arg2; \ |
| 865 | __asm__ volatile( \ |
| 866 | "mr 11,%1\n\t" \ |
| 867 | "lwz 3,4(11)\n\t" /* arg1->r3 */ \ |
| 868 | "lwz 4,8(11)\n\t" \ |
| 869 | "lwz 11,0(11)\n\t" /* target->r11 */ \ |
| 870 | VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11 \ |
| 871 | "mr %0,3" \ |
| 872 | : /*out*/ "=r" (_res) \ |
| 873 | : /*in*/ "r" (&_argvec[0]) \ |
| 874 | : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \ |
| 875 | ); \ |
| 876 | lval = (__typeof__(lval)) _res; \ |
| 877 | } while (0) |
| 878 | |
| 879 | #endif /* ARCH_ppc32 */ |
| 880 | |
| 881 | /* --------------------------- ppc64 --------------------------- */ |
| 882 | |
sewardj | 9734b20 | 2006-01-17 01:49:37 +0000 | [diff] [blame] | 883 | #if defined(ARCH_ppc64) |
| 884 | |
| 885 | /* ARGREGS: r3 r4 r5 r6 r7 r8 r9 r10 (the rest on stack somewhere) */ |
| 886 | |
| 887 | /* These regs are trashed by the hidden call. */ |
| 888 | #define __CALLER_SAVED_REGS "lr", \ |
| 889 | "r0", "r2", "r3", "r4", "r5", "r6", \ |
| 890 | "r7", "r8", "r9", "r10", "r11", "r12" |
| 891 | |
| 892 | /* These CALL_FN_ macros assume that on ppc64-linux, sizeof(unsigned |
| 893 | long) == 8. */ |
| 894 | |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 895 | #define CALL_FN_W_v(lval, orig) \ |
sewardj | 9734b20 | 2006-01-17 01:49:37 +0000 | [diff] [blame] | 896 | do { \ |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 897 | volatile OrigFn _orig = (orig); \ |
| 898 | volatile unsigned long _argvec[3+0]; \ |
sewardj | 9734b20 | 2006-01-17 01:49:37 +0000 | [diff] [blame] | 899 | volatile unsigned long _res; \ |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 900 | /* _argvec[0] holds current r2 across the call */ \ |
| 901 | _argvec[1] = (unsigned long)_orig.r2; \ |
| 902 | _argvec[2] = (unsigned long)_orig.nraddr; \ |
sewardj | 9734b20 | 2006-01-17 01:49:37 +0000 | [diff] [blame] | 903 | __asm__ volatile( \ |
| 904 | "mr 11,%1\n\t" \ |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 905 | "std 2,-16(11)\n\t" /* save tocptr */ \ |
| 906 | "ld 2,-8(11)\n\t" /* use nraddr's tocptr */ \ |
| 907 | "ld 11, 0(11)\n\t" /* target->r11 */ \ |
sewardj | 9734b20 | 2006-01-17 01:49:37 +0000 | [diff] [blame] | 908 | VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11 \ |
| 909 | "mr 11,%1\n\t" \ |
| 910 | "mr %0,3\n\t" \ |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 911 | "ld 2,-16(11)" /* restore tocptr */ \ |
sewardj | 9734b20 | 2006-01-17 01:49:37 +0000 | [diff] [blame] | 912 | : /*out*/ "=r" (_res) \ |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 913 | : /*in*/ "r" (&_argvec[2]) \ |
sewardj | 9734b20 | 2006-01-17 01:49:37 +0000 | [diff] [blame] | 914 | : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \ |
| 915 | ); \ |
| 916 | lval = (__typeof__(lval)) _res; \ |
| 917 | } while (0) |
| 918 | |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 919 | #define CALL_FN_W_W(lval, orig, arg1) \ |
sewardj | 9734b20 | 2006-01-17 01:49:37 +0000 | [diff] [blame] | 920 | do { \ |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 921 | volatile OrigFn _orig = (orig); \ |
| 922 | volatile unsigned long _argvec[3+1]; \ |
sewardj | 9734b20 | 2006-01-17 01:49:37 +0000 | [diff] [blame] | 923 | volatile unsigned long _res; \ |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 924 | /* _argvec[0] holds current r2 across the call */ \ |
| 925 | _argvec[1] = (unsigned long)_orig.r2; \ |
| 926 | _argvec[2] = (unsigned long)_orig.nraddr; \ |
| 927 | _argvec[2+1] = (unsigned long)arg1; \ |
sewardj | 9734b20 | 2006-01-17 01:49:37 +0000 | [diff] [blame] | 928 | __asm__ volatile( \ |
| 929 | "mr 11,%1\n\t" \ |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 930 | "std 2,-16(11)\n\t" /* save tocptr */ \ |
| 931 | "ld 2,-8(11)\n\t" /* use nraddr's tocptr */ \ |
| 932 | "ld 3, 8(11)\n\t" /* arg1->r3 */ \ |
| 933 | "ld 11, 0(11)\n\t" /* target->r11 */ \ |
sewardj | 9734b20 | 2006-01-17 01:49:37 +0000 | [diff] [blame] | 934 | VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11 \ |
| 935 | "mr 11,%1\n\t" \ |
| 936 | "mr %0,3\n\t" \ |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 937 | "ld 2,-16(11)" /* restore tocptr */ \ |
sewardj | 9734b20 | 2006-01-17 01:49:37 +0000 | [diff] [blame] | 938 | : /*out*/ "=r" (_res) \ |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 939 | : /*in*/ "r" (&_argvec[2]) \ |
sewardj | 9734b20 | 2006-01-17 01:49:37 +0000 | [diff] [blame] | 940 | : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \ |
| 941 | ); \ |
| 942 | lval = (__typeof__(lval)) _res; \ |
| 943 | } while (0) |
| 944 | |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 945 | #define CALL_FN_W_WW(lval, orig, arg1,arg2) \ |
sewardj | 9734b20 | 2006-01-17 01:49:37 +0000 | [diff] [blame] | 946 | do { \ |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 947 | volatile OrigFn _orig = (orig); \ |
| 948 | volatile unsigned long _argvec[3+2]; \ |
sewardj | 9734b20 | 2006-01-17 01:49:37 +0000 | [diff] [blame] | 949 | volatile unsigned long _res; \ |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 950 | /* _argvec[0] holds current r2 across the call */ \ |
| 951 | _argvec[1] = (unsigned long)_orig.r2; \ |
| 952 | _argvec[2] = (unsigned long)_orig.nraddr; \ |
| 953 | _argvec[2+1] = (unsigned long)arg1; \ |
| 954 | _argvec[2+2] = (unsigned long)arg2; \ |
sewardj | 9734b20 | 2006-01-17 01:49:37 +0000 | [diff] [blame] | 955 | __asm__ volatile( \ |
| 956 | "mr 11,%1\n\t" \ |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 957 | "std 2,-16(11)\n\t" /* save tocptr */ \ |
| 958 | "ld 2,-8(11)\n\t" /* use nraddr's tocptr */ \ |
| 959 | "ld 3, 8(11)\n\t" /* arg1->r3 */ \ |
| 960 | "ld 4, 16(11)\n\t" /* arg1->r4 */ \ |
| 961 | "ld 11, 0(11)\n\t" /* target->r11 */ \ |
sewardj | 9734b20 | 2006-01-17 01:49:37 +0000 | [diff] [blame] | 962 | VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11 \ |
| 963 | "mr 11,%1\n\t" \ |
| 964 | "mr %0,3\n\t" \ |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 965 | "ld 2,-16(11)" /* restore tocptr */ \ |
sewardj | 9734b20 | 2006-01-17 01:49:37 +0000 | [diff] [blame] | 966 | : /*out*/ "=r" (_res) \ |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 967 | : /*in*/ "r" (&_argvec[2]) \ |
sewardj | 9734b20 | 2006-01-17 01:49:37 +0000 | [diff] [blame] | 968 | : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \ |
| 969 | ); \ |
| 970 | lval = (__typeof__(lval)) _res; \ |
| 971 | } while (0) |
| 972 | |
| 973 | #endif /* ARCH_ppc64 */ |
| 974 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 975 | |
| 976 | /* ------------------------------------------------------------------ */ |
| 977 | /* ARCHITECTURE INDEPENDENT MACROS for CLIENT REQUESTS. */ |
| 978 | /* */ |
njn | 30d76c6 | 2005-06-18 15:07:39 +0000 | [diff] [blame] | 979 | /* ------------------------------------------------------------------ */ |
| 980 | |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 981 | /* Some request codes. There are many more of these, but most are not |
| 982 | exposed to end-user view. These are the public ones, all of the |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 983 | form 0x1000 + small_number. |
njn | d799418 | 2003-10-02 13:44:04 +0000 | [diff] [blame] | 984 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 985 | Core ones are in the range 0x00000000--0x0000ffff. The non-public |
| 986 | ones start at 0x2000. |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 987 | */ |
| 988 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 989 | /* These macros are used by tools -- they must be public, but don't |
| 990 | embed them into other programs. */ |
njn | fc26ff9 | 2004-11-22 19:12:49 +0000 | [diff] [blame] | 991 | #define VG_USERREQ_TOOL_BASE(a,b) \ |
njn | 4c79121 | 2003-05-02 17:53:54 +0000 | [diff] [blame] | 992 | ((unsigned int)(((a)&0xff) << 24 | ((b)&0xff) << 16)) |
njn | fc26ff9 | 2004-11-22 19:12:49 +0000 | [diff] [blame] | 993 | #define VG_IS_TOOL_USERREQ(a, b, v) \ |
| 994 | (VG_USERREQ_TOOL_BASE(a,b) == ((v) & 0xffff0000)) |
sewardj | 3404251 | 2002-10-22 04:14:35 +0000 | [diff] [blame] | 995 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 996 | typedef |
njn | 4c79121 | 2003-05-02 17:53:54 +0000 | [diff] [blame] | 997 | enum { VG_USERREQ__RUNNING_ON_VALGRIND = 0x1001, |
| 998 | VG_USERREQ__DISCARD_TRANSLATIONS = 0x1002, |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 999 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1000 | /* These allow any function to be called from the simulated |
| 1001 | CPU but run on the real CPU. Nb: the first arg passed to |
| 1002 | the function is always the ThreadId of the running |
| 1003 | thread! So CLIENT_CALL0 actually requires a 1 arg |
njn | d4795be | 2004-11-24 11:57:51 +0000 | [diff] [blame] | 1004 | function, etc. */ |
njn | 4c79121 | 2003-05-02 17:53:54 +0000 | [diff] [blame] | 1005 | VG_USERREQ__CLIENT_CALL0 = 0x1101, |
| 1006 | VG_USERREQ__CLIENT_CALL1 = 0x1102, |
| 1007 | VG_USERREQ__CLIENT_CALL2 = 0x1103, |
| 1008 | VG_USERREQ__CLIENT_CALL3 = 0x1104, |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1009 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1010 | /* Can be useful in regression testing suites -- eg. can |
| 1011 | send Valgrind's output to /dev/null and still count |
| 1012 | errors. */ |
njn | 4c79121 | 2003-05-02 17:53:54 +0000 | [diff] [blame] | 1013 | VG_USERREQ__COUNT_ERRORS = 0x1201, |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 1014 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1015 | /* These are useful and can be interpreted by any tool that |
| 1016 | tracks malloc() et al, by using vg_replace_malloc.c. */ |
njn | d799418 | 2003-10-02 13:44:04 +0000 | [diff] [blame] | 1017 | VG_USERREQ__MALLOCLIKE_BLOCK = 0x1301, |
| 1018 | VG_USERREQ__FREELIKE_BLOCK = 0x1302, |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 1019 | /* Memory pool support. */ |
| 1020 | VG_USERREQ__CREATE_MEMPOOL = 0x1303, |
| 1021 | VG_USERREQ__DESTROY_MEMPOOL = 0x1304, |
| 1022 | VG_USERREQ__MEMPOOL_ALLOC = 0x1305, |
| 1023 | VG_USERREQ__MEMPOOL_FREE = 0x1306, |
njn | d799418 | 2003-10-02 13:44:04 +0000 | [diff] [blame] | 1024 | |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 1025 | /* Allow printfs to valgrind log. */ |
njn | 30d76c6 | 2005-06-18 15:07:39 +0000 | [diff] [blame] | 1026 | VG_USERREQ__PRINTF = 0x1401, |
rjwalsh | 0140af5 | 2005-06-04 20:42:33 +0000 | [diff] [blame] | 1027 | VG_USERREQ__PRINTF_BACKTRACE = 0x1402, |
| 1028 | |
| 1029 | /* Stack support. */ |
| 1030 | VG_USERREQ__STACK_REGISTER = 0x1501, |
| 1031 | VG_USERREQ__STACK_DEREGISTER = 0x1502, |
| 1032 | VG_USERREQ__STACK_CHANGE = 0x1503, |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1033 | } Vg_ClientRequest; |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 1034 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1035 | #if !defined(__GNUC__) |
| 1036 | # define __extension__ /* */ |
mueller | c9b3655 | 2003-12-31 14:32:23 +0000 | [diff] [blame] | 1037 | #endif |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 1038 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1039 | /* Returns the number of Valgrinds this code is running under. That |
| 1040 | is, 0 if running natively, 1 if running under Valgrind, 2 if |
| 1041 | running under Valgrind which is running under another Valgrind, |
| 1042 | etc. */ |
| 1043 | #define RUNNING_ON_VALGRIND __extension__ \ |
| 1044 | ({unsigned int _qzz_res; \ |
| 1045 | VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0 /* if not */, \ |
| 1046 | VG_USERREQ__RUNNING_ON_VALGRIND, \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 1047 | 0, 0, 0, 0, 0); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1048 | _qzz_res; \ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1049 | }) |
| 1050 | |
| 1051 | |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 1052 | /* Discard translation of code in the range [_qzz_addr .. _qzz_addr + |
| 1053 | _qzz_len - 1]. Useful if you are debugging a JITter or some such, |
| 1054 | since it provides a way to make sure valgrind will retranslate the |
| 1055 | invalidated area. Returns no value. */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1056 | #define VALGRIND_DISCARD_TRANSLATIONS(_qzz_addr,_qzz_len) \ |
| 1057 | {unsigned int _qzz_res; \ |
| 1058 | VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \ |
| 1059 | VG_USERREQ__DISCARD_TRANSLATIONS, \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 1060 | _qzz_addr, _qzz_len, 0, 0, 0); \ |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
njn | 26aba4d | 2005-05-16 13:31:23 +0000 | [diff] [blame] | 1063 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1064 | /* These requests are for getting Valgrind itself to print something. |
| 1065 | Possibly with a backtrace. This is a really ugly hack. */ |
| 1066 | |
| 1067 | #if defined(NVALGRIND) |
| 1068 | |
| 1069 | # define VALGRIND_PRINTF(...) |
| 1070 | # define VALGRIND_PRINTF_BACKTRACE(...) |
njn | 26aba4d | 2005-05-16 13:31:23 +0000 | [diff] [blame] | 1071 | |
| 1072 | #else /* NVALGRIND */ |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 1073 | |
fitzhardinge | a09a1b5 | 2003-11-07 23:09:48 +0000 | [diff] [blame] | 1074 | int VALGRIND_PRINTF(const char *format, ...) |
| 1075 | __attribute__((format(__printf__, 1, 2))); |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 1076 | __attribute__((weak)) |
| 1077 | int |
fitzhardinge | a09a1b5 | 2003-11-07 23:09:48 +0000 | [diff] [blame] | 1078 | VALGRIND_PRINTF(const char *format, ...) |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 1079 | { |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 1080 | unsigned long _qzz_res; |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 1081 | va_list vargs; |
| 1082 | va_start(vargs, format); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1083 | VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, VG_USERREQ__PRINTF, |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 1084 | (unsigned long)format, (unsigned long)vargs, |
| 1085 | 0, 0, 0); |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 1086 | va_end(vargs); |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 1087 | return (int)_qzz_res; |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
fitzhardinge | a09a1b5 | 2003-11-07 23:09:48 +0000 | [diff] [blame] | 1090 | int VALGRIND_PRINTF_BACKTRACE(const char *format, ...) |
| 1091 | __attribute__((format(__printf__, 1, 2))); |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 1092 | __attribute__((weak)) |
| 1093 | int |
fitzhardinge | a09a1b5 | 2003-11-07 23:09:48 +0000 | [diff] [blame] | 1094 | VALGRIND_PRINTF_BACKTRACE(const char *format, ...) |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 1095 | { |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 1096 | unsigned long _qzz_res; |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 1097 | va_list vargs; |
| 1098 | va_start(vargs, format); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1099 | VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, VG_USERREQ__PRINTF_BACKTRACE, |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 1100 | (unsigned long)format, (unsigned long)vargs, |
| 1101 | 0, 0, 0); |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 1102 | va_end(vargs); |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 1103 | return (int)_qzz_res; |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 1106 | #endif /* NVALGRIND */ |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 1107 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1108 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1109 | /* These requests allow control to move from the simulated CPU to the |
| 1110 | real CPU, calling an arbitary function */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1111 | #define VALGRIND_NON_SIMD_CALL0(_qyy_fn) \ |
| 1112 | ({unsigned long _qyy_res; \ |
| 1113 | VALGRIND_DO_CLIENT_REQUEST(_qyy_res, 0 /* default return */, \ |
| 1114 | VG_USERREQ__CLIENT_CALL0, \ |
| 1115 | _qyy_fn, \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 1116 | 0, 0, 0, 0); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1117 | _qyy_res; \ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1118 | }) |
| 1119 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1120 | #define VALGRIND_NON_SIMD_CALL1(_qyy_fn, _qyy_arg1) \ |
| 1121 | ({unsigned long _qyy_res; \ |
| 1122 | VALGRIND_DO_CLIENT_REQUEST(_qyy_res, 0 /* default return */, \ |
| 1123 | VG_USERREQ__CLIENT_CALL1, \ |
| 1124 | _qyy_fn, \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 1125 | _qyy_arg1, 0, 0, 0); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1126 | _qyy_res; \ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1127 | }) |
| 1128 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1129 | #define VALGRIND_NON_SIMD_CALL2(_qyy_fn, _qyy_arg1, _qyy_arg2) \ |
| 1130 | ({unsigned long _qyy_res; \ |
| 1131 | VALGRIND_DO_CLIENT_REQUEST(_qyy_res, 0 /* default return */, \ |
| 1132 | VG_USERREQ__CLIENT_CALL2, \ |
| 1133 | _qyy_fn, \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 1134 | _qyy_arg1, _qyy_arg2, 0, 0); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1135 | _qyy_res; \ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1136 | }) |
| 1137 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1138 | #define VALGRIND_NON_SIMD_CALL3(_qyy_fn, _qyy_arg1, _qyy_arg2, _qyy_arg3) \ |
| 1139 | ({unsigned long _qyy_res; \ |
| 1140 | VALGRIND_DO_CLIENT_REQUEST(_qyy_res, 0 /* default return */, \ |
| 1141 | VG_USERREQ__CLIENT_CALL3, \ |
| 1142 | _qyy_fn, \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 1143 | _qyy_arg1, _qyy_arg2, \ |
| 1144 | _qyy_arg3, 0); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1145 | _qyy_res; \ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1146 | }) |
| 1147 | |
| 1148 | |
nethercote | 7cc9c23 | 2004-01-21 15:08:04 +0000 | [diff] [blame] | 1149 | /* Counts the number of errors that have been recorded by a tool. Nb: |
| 1150 | the tool must record the errors with VG_(maybe_record_error)() or |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 1151 | VG_(unique_error)() for them to be counted. */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1152 | #define VALGRIND_COUNT_ERRORS \ |
| 1153 | ({unsigned int _qyy_res; \ |
| 1154 | VALGRIND_DO_CLIENT_REQUEST(_qyy_res, 0 /* default return */, \ |
| 1155 | VG_USERREQ__COUNT_ERRORS, \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 1156 | 0, 0, 0, 0, 0); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1157 | _qyy_res; \ |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 1158 | }) |
| 1159 | |
njn | d799418 | 2003-10-02 13:44:04 +0000 | [diff] [blame] | 1160 | /* Mark a block of memory as having been allocated by a malloc()-like |
| 1161 | function. `addr' is the start of the usable block (ie. after any |
| 1162 | redzone) `rzB' is redzone size if the allocator can apply redzones; |
| 1163 | use '0' if not. Adding redzones makes it more likely Valgrind will spot |
| 1164 | block overruns. `is_zeroed' indicates if the memory is zeroed, as it is |
| 1165 | for calloc(). Put it immediately after the point where a block is |
| 1166 | allocated. |
| 1167 | |
| 1168 | If you're allocating memory via superblocks, and then handing out small |
| 1169 | chunks of each superblock, if you don't have redzones on your small |
| 1170 | blocks, it's worth marking the superblock with VALGRIND_MAKE_NOACCESS |
| 1171 | when it's created, so that block overruns are detected. But if you can |
| 1172 | put redzones on, it's probably better to not do this, so that messages |
| 1173 | for small overruns are described in terms of the small block rather than |
| 1174 | the superblock (but if you have a big overrun that skips over a redzone, |
| 1175 | you could miss an error this way). See memcheck/tests/custom_alloc.c |
| 1176 | for an example. |
| 1177 | |
| 1178 | Nb: block must be freed via a free()-like function specified |
| 1179 | with VALGRIND_FREELIKE_BLOCK or mismatch errors will occur. */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1180 | #define VALGRIND_MALLOCLIKE_BLOCK(addr, sizeB, rzB, is_zeroed) \ |
| 1181 | {unsigned int _qzz_res; \ |
| 1182 | VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \ |
| 1183 | VG_USERREQ__MALLOCLIKE_BLOCK, \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 1184 | addr, sizeB, rzB, is_zeroed, 0); \ |
njn | d799418 | 2003-10-02 13:44:04 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | /* Mark a block of memory as having been freed by a free()-like function. |
| 1188 | `rzB' is redzone size; it must match that given to |
| 1189 | VALGRIND_MALLOCLIKE_BLOCK. Memory not freed will be detected by the leak |
| 1190 | checker. Put it immediately after the point where the block is freed. */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1191 | #define VALGRIND_FREELIKE_BLOCK(addr, rzB) \ |
| 1192 | {unsigned int _qzz_res; \ |
| 1193 | VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \ |
| 1194 | VG_USERREQ__FREELIKE_BLOCK, \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 1195 | addr, rzB, 0, 0, 0); \ |
njn | d799418 | 2003-10-02 13:44:04 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 1198 | /* Create a memory pool. */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1199 | #define VALGRIND_CREATE_MEMPOOL(pool, rzB, is_zeroed) \ |
| 1200 | {unsigned int _qzz_res; \ |
| 1201 | VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \ |
| 1202 | VG_USERREQ__CREATE_MEMPOOL, \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 1203 | pool, rzB, is_zeroed, 0, 0); \ |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | /* Destroy a memory pool. */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1207 | #define VALGRIND_DESTROY_MEMPOOL(pool) \ |
| 1208 | {unsigned int _qzz_res; \ |
| 1209 | VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \ |
| 1210 | VG_USERREQ__DESTROY_MEMPOOL, \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 1211 | pool, 0, 0, 0, 0); \ |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | /* Associate a piece of memory with a memory pool. */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1215 | #define VALGRIND_MEMPOOL_ALLOC(pool, addr, size) \ |
| 1216 | {unsigned int _qzz_res; \ |
| 1217 | VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \ |
| 1218 | VG_USERREQ__MEMPOOL_ALLOC, \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 1219 | pool, addr, size, 0, 0); \ |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | /* Disassociate a piece of memory from a memory pool. */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1223 | #define VALGRIND_MEMPOOL_FREE(pool, addr) \ |
| 1224 | {unsigned int _qzz_res; \ |
| 1225 | VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \ |
| 1226 | VG_USERREQ__MEMPOOL_FREE, \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 1227 | pool, addr, 0, 0, 0); \ |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
rjwalsh | 0140af5 | 2005-06-04 20:42:33 +0000 | [diff] [blame] | 1230 | /* Mark a piece of memory as being a stack. Returns a stack id. */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1231 | #define VALGRIND_STACK_REGISTER(start, end) \ |
| 1232 | ({unsigned int _qzz_res; \ |
| 1233 | VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \ |
| 1234 | VG_USERREQ__STACK_REGISTER, \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 1235 | start, end, 0, 0, 0); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1236 | _qzz_res; \ |
rjwalsh | 0140af5 | 2005-06-04 20:42:33 +0000 | [diff] [blame] | 1237 | }) |
| 1238 | |
| 1239 | /* Unmark the piece of memory associated with a stack id as being a |
| 1240 | stack. */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1241 | #define VALGRIND_STACK_DEREGISTER(id) \ |
| 1242 | {unsigned int _qzz_res; \ |
| 1243 | VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \ |
| 1244 | VG_USERREQ__STACK_DEREGISTER, \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 1245 | id, 0, 0, 0, 0); \ |
rjwalsh | 0140af5 | 2005-06-04 20:42:33 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
| 1248 | /* Change the start and end address of the stack id. */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1249 | #define VALGRIND_STACK_CHANGE(id, start, end) \ |
| 1250 | {unsigned int _qzz_res; \ |
| 1251 | VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \ |
| 1252 | VG_USERREQ__STACK_CHANGE, \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 1253 | id, start, end, 0, 0); \ |
rjwalsh | 0140af5 | 2005-06-04 20:42:33 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1256 | |
| 1257 | #undef ARCH_x86 |
| 1258 | #undef ARCH_amd64 |
| 1259 | #undef ARCH_ppc32 |
| 1260 | #undef ARCH_ppc64 |
| 1261 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1262 | #endif /* __VALGRIND_H */ |