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 | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 521 | #define CALL_FN_W_WWWW(lval, orig, arg1,arg2,arg3,arg4) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 522 | do { \ |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 523 | volatile OrigFn _orig = (orig); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 524 | volatile unsigned long _argvec[5]; \ |
| 525 | volatile unsigned long _res; \ |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 526 | _argvec[0] = (unsigned long)_orig.nraddr; \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 527 | _argvec[1] = (unsigned long)(arg1); \ |
| 528 | _argvec[2] = (unsigned long)(arg2); \ |
| 529 | _argvec[3] = (unsigned long)(arg3); \ |
| 530 | _argvec[4] = (unsigned long)(arg4); \ |
| 531 | __asm__ volatile( \ |
| 532 | "pushl 16(%%eax)\n\t" \ |
| 533 | "pushl 12(%%eax)\n\t" \ |
| 534 | "pushl 8(%%eax)\n\t" \ |
| 535 | "pushl 4(%%eax)\n\t" \ |
| 536 | "movl (%%eax), %%eax\n\t" /* target->%eax */ \ |
| 537 | VALGRIND_CALL_NOREDIR_EAX \ |
| 538 | "addl $16, %%esp\n" \ |
| 539 | : /*out*/ "=a" (_res) \ |
| 540 | : /*in*/ "a" (&_argvec[0]) \ |
| 541 | : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \ |
| 542 | ); \ |
| 543 | lval = (__typeof__(lval)) _res; \ |
| 544 | } while (0) |
| 545 | |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 546 | #define CALL_FN_W_5W(lval, orig, arg1,arg2,arg3,arg4,arg5) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 547 | do { \ |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 548 | volatile OrigFn _orig = (orig); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 549 | volatile unsigned long _argvec[6]; \ |
| 550 | volatile unsigned long _res; \ |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 551 | _argvec[0] = (unsigned long)_orig.nraddr; \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 552 | _argvec[1] = (unsigned long)(arg1); \ |
| 553 | _argvec[2] = (unsigned long)(arg2); \ |
| 554 | _argvec[3] = (unsigned long)(arg3); \ |
| 555 | _argvec[4] = (unsigned long)(arg4); \ |
| 556 | _argvec[5] = (unsigned long)(arg5); \ |
| 557 | __asm__ volatile( \ |
| 558 | "pushl 20(%%eax)\n\t" \ |
| 559 | "pushl 16(%%eax)\n\t" \ |
| 560 | "pushl 12(%%eax)\n\t" \ |
| 561 | "pushl 8(%%eax)\n\t" \ |
| 562 | "pushl 4(%%eax)\n\t" \ |
| 563 | "movl (%%eax), %%eax\n\t" /* target->%eax */ \ |
| 564 | VALGRIND_CALL_NOREDIR_EAX \ |
| 565 | "addl $20, %%esp\n" \ |
| 566 | : /*out*/ "=a" (_res) \ |
| 567 | : /*in*/ "a" (&_argvec[0]) \ |
| 568 | : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \ |
| 569 | ); \ |
| 570 | lval = (__typeof__(lval)) _res; \ |
| 571 | } while (0) |
| 572 | |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 573 | #define CALL_FN_W_6W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 574 | do { \ |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 575 | volatile OrigFn _orig = (orig); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 576 | volatile unsigned long _argvec[7]; \ |
| 577 | volatile unsigned long _res; \ |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 578 | _argvec[0] = (unsigned long)_orig.nraddr; \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 579 | _argvec[1] = (unsigned long)(arg1); \ |
| 580 | _argvec[2] = (unsigned long)(arg2); \ |
| 581 | _argvec[3] = (unsigned long)(arg3); \ |
| 582 | _argvec[4] = (unsigned long)(arg4); \ |
| 583 | _argvec[5] = (unsigned long)(arg5); \ |
| 584 | _argvec[6] = (unsigned long)(arg6); \ |
| 585 | __asm__ volatile( \ |
| 586 | "pushl 24(%%eax)\n\t" \ |
| 587 | "pushl 20(%%eax)\n\t" \ |
| 588 | "pushl 16(%%eax)\n\t" \ |
| 589 | "pushl 12(%%eax)\n\t" \ |
| 590 | "pushl 8(%%eax)\n\t" \ |
| 591 | "pushl 4(%%eax)\n\t" \ |
| 592 | "movl (%%eax), %%eax\n\t" /* target->%eax */ \ |
| 593 | VALGRIND_CALL_NOREDIR_EAX \ |
| 594 | "addl $24, %%esp\n" \ |
| 595 | : /*out*/ "=a" (_res) \ |
| 596 | : /*in*/ "a" (&_argvec[0]) \ |
| 597 | : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \ |
| 598 | ); \ |
| 599 | lval = (__typeof__(lval)) _res; \ |
| 600 | } while (0) |
| 601 | |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 602 | #define CALL_FN_W_7W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6, \ |
| 603 | arg7) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 604 | do { \ |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 605 | volatile OrigFn _orig = (orig); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 606 | volatile unsigned long _argvec[8]; \ |
| 607 | volatile unsigned long _res; \ |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 608 | _argvec[0] = (unsigned long)_orig.nraddr; \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 609 | _argvec[1] = (unsigned long)(arg1); \ |
| 610 | _argvec[2] = (unsigned long)(arg2); \ |
| 611 | _argvec[3] = (unsigned long)(arg3); \ |
| 612 | _argvec[4] = (unsigned long)(arg4); \ |
| 613 | _argvec[5] = (unsigned long)(arg5); \ |
| 614 | _argvec[6] = (unsigned long)(arg6); \ |
| 615 | _argvec[7] = (unsigned long)(arg7); \ |
| 616 | __asm__ volatile( \ |
| 617 | "pushl 28(%%eax)\n\t" \ |
| 618 | "pushl 24(%%eax)\n\t" \ |
| 619 | "pushl 20(%%eax)\n\t" \ |
| 620 | "pushl 16(%%eax)\n\t" \ |
| 621 | "pushl 12(%%eax)\n\t" \ |
| 622 | "pushl 8(%%eax)\n\t" \ |
| 623 | "pushl 4(%%eax)\n\t" \ |
| 624 | "movl (%%eax), %%eax\n\t" /* target->%eax */ \ |
| 625 | VALGRIND_CALL_NOREDIR_EAX \ |
| 626 | "addl $28, %%esp\n" \ |
| 627 | : /*out*/ "=a" (_res) \ |
| 628 | : /*in*/ "a" (&_argvec[0]) \ |
| 629 | : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \ |
| 630 | ); \ |
| 631 | lval = (__typeof__(lval)) _res; \ |
| 632 | } while (0) |
| 633 | |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 634 | #define CALL_FN_W_8W(lval, orig, arg1,arg2,arg3,arg4,arg5,arg6, \ |
| 635 | arg7,arg8) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 636 | do { \ |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 637 | volatile OrigFn _orig = (orig); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 638 | volatile unsigned long _argvec[9]; \ |
| 639 | volatile unsigned long _res; \ |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 640 | _argvec[0] = (unsigned long)_orig.nraddr; \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 641 | _argvec[1] = (unsigned long)(arg1); \ |
| 642 | _argvec[2] = (unsigned long)(arg2); \ |
| 643 | _argvec[3] = (unsigned long)(arg3); \ |
| 644 | _argvec[4] = (unsigned long)(arg4); \ |
| 645 | _argvec[5] = (unsigned long)(arg5); \ |
| 646 | _argvec[6] = (unsigned long)(arg6); \ |
| 647 | _argvec[7] = (unsigned long)(arg7); \ |
| 648 | _argvec[8] = (unsigned long)(arg8); \ |
| 649 | __asm__ volatile( \ |
| 650 | "pushl 32(%%eax)\n\t" \ |
| 651 | "pushl 28(%%eax)\n\t" \ |
| 652 | "pushl 24(%%eax)\n\t" \ |
| 653 | "pushl 20(%%eax)\n\t" \ |
| 654 | "pushl 16(%%eax)\n\t" \ |
| 655 | "pushl 12(%%eax)\n\t" \ |
| 656 | "pushl 8(%%eax)\n\t" \ |
| 657 | "pushl 4(%%eax)\n\t" \ |
| 658 | "movl (%%eax), %%eax\n\t" /* target->%eax */ \ |
| 659 | VALGRIND_CALL_NOREDIR_EAX \ |
| 660 | "addl $32, %%esp\n" \ |
| 661 | : /*out*/ "=a" (_res) \ |
| 662 | : /*in*/ "a" (&_argvec[0]) \ |
| 663 | : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \ |
| 664 | ); \ |
| 665 | lval = (__typeof__(lval)) _res; \ |
| 666 | } while (0) |
| 667 | |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 668 | #define CALL_FN_W_12W(lval, orig, arg1,arg2,arg3,arg4,arg5, \ |
| 669 | arg6,arg7,arg8,arg9,arg10, \ |
| 670 | arg11,arg12) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 671 | do { \ |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 672 | volatile OrigFn _orig = (orig); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 673 | volatile unsigned long _argvec[13]; \ |
| 674 | volatile unsigned long _res; \ |
sewardj | 66226cc | 2006-01-20 15:46:46 +0000 | [diff] [blame] | 675 | _argvec[0] = (unsigned long)_orig.nraddr; \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 676 | _argvec[1] = (unsigned long)(arg1); \ |
| 677 | _argvec[2] = (unsigned long)(arg2); \ |
| 678 | _argvec[3] = (unsigned long)(arg3); \ |
| 679 | _argvec[4] = (unsigned long)(arg4); \ |
| 680 | _argvec[5] = (unsigned long)(arg5); \ |
| 681 | _argvec[6] = (unsigned long)(arg6); \ |
| 682 | _argvec[7] = (unsigned long)(arg7); \ |
| 683 | _argvec[8] = (unsigned long)(arg8); \ |
| 684 | _argvec[9] = (unsigned long)(arg9); \ |
| 685 | _argvec[10] = (unsigned long)(arg10); \ |
| 686 | _argvec[11] = (unsigned long)(arg11); \ |
| 687 | _argvec[12] = (unsigned long)(arg12); \ |
| 688 | __asm__ volatile( \ |
| 689 | "pushl 48(%%eax)\n\t" \ |
| 690 | "pushl 44(%%eax)\n\t" \ |
| 691 | "pushl 40(%%eax)\n\t" \ |
| 692 | "pushl 36(%%eax)\n\t" \ |
| 693 | "pushl 32(%%eax)\n\t" \ |
| 694 | "pushl 28(%%eax)\n\t" \ |
| 695 | "pushl 24(%%eax)\n\t" \ |
| 696 | "pushl 20(%%eax)\n\t" \ |
| 697 | "pushl 16(%%eax)\n\t" \ |
| 698 | "pushl 12(%%eax)\n\t" \ |
| 699 | "pushl 8(%%eax)\n\t" \ |
| 700 | "pushl 4(%%eax)\n\t" \ |
| 701 | "movl (%%eax), %%eax\n\t" /* target->%eax */ \ |
| 702 | VALGRIND_CALL_NOREDIR_EAX \ |
| 703 | "addl $48, %%esp\n" \ |
| 704 | : /*out*/ "=a" (_res) \ |
| 705 | : /*in*/ "a" (&_argvec[0]) \ |
| 706 | : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \ |
| 707 | ); \ |
| 708 | lval = (__typeof__(lval)) _res; \ |
| 709 | } while (0) |
| 710 | |
| 711 | #endif /* ARCH_x86 */ |
| 712 | |
| 713 | /* --------------------------- amd64 --------------------------- */ |
| 714 | |
| 715 | #if defined(ARCH_amd64) |
| 716 | |
| 717 | /* ARGREGS: rdi rsi rdx rcx r8 r9 (the rest on stack in R-to-L order) */ |
| 718 | |
| 719 | /* These regs are trashed by the hidden call. */ |
| 720 | #define __CALLER_SAVED_REGS /*"rax",*/ "rcx", "rdx", "rsi", \ |
| 721 | "rdi", "r8", "r9", "r10", "r11" |
| 722 | |
| 723 | /* These CALL_FN_ macros assume that on amd64-linux, sizeof(unsigned |
| 724 | long) == 8. */ |
| 725 | |
sewardj | c885844 | 2006-01-20 15:17:20 +0000 | [diff] [blame] | 726 | #define CALL_FN_W_v(lval, orig) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 727 | do { \ |
sewardj | c885844 | 2006-01-20 15:17:20 +0000 | [diff] [blame] | 728 | volatile OrigFn _orig = (orig); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 729 | volatile unsigned long _argvec[1]; \ |
| 730 | volatile unsigned long _res; \ |
sewardj | c885844 | 2006-01-20 15:17:20 +0000 | [diff] [blame] | 731 | _argvec[0] = (unsigned long)_orig.nraddr; \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 732 | __asm__ volatile( \ |
| 733 | "movq (%%rax), %%rax\n\t" /* target->%rax */ \ |
| 734 | VALGRIND_CALL_NOREDIR_RAX \ |
| 735 | : /*out*/ "=a" (_res) \ |
| 736 | : /*in*/ "a" (&_argvec[0]) \ |
| 737 | : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \ |
| 738 | ); \ |
| 739 | lval = (__typeof__(lval)) _res; \ |
| 740 | } while (0) |
| 741 | |
sewardj | c885844 | 2006-01-20 15:17:20 +0000 | [diff] [blame] | 742 | #define CALL_FN_W_W(lval, orig, arg1) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 743 | do { \ |
sewardj | c885844 | 2006-01-20 15:17:20 +0000 | [diff] [blame] | 744 | volatile OrigFn _orig = (orig); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 745 | volatile unsigned long _argvec[2]; \ |
| 746 | volatile unsigned long _res; \ |
sewardj | c885844 | 2006-01-20 15:17:20 +0000 | [diff] [blame] | 747 | _argvec[0] = (unsigned long)_orig.nraddr; \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 748 | _argvec[1] = (unsigned long)(arg1); \ |
| 749 | __asm__ volatile( \ |
| 750 | "movq 8(%%rax), %%rdi\n\t" \ |
| 751 | "movq (%%rax), %%rax\n\t" /* target->%rax */ \ |
| 752 | VALGRIND_CALL_NOREDIR_RAX \ |
| 753 | : /*out*/ "=a" (_res) \ |
| 754 | : /*in*/ "a" (&_argvec[0]) \ |
| 755 | : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \ |
| 756 | ); \ |
| 757 | lval = (__typeof__(lval)) _res; \ |
| 758 | } while (0) |
| 759 | |
sewardj | c885844 | 2006-01-20 15:17:20 +0000 | [diff] [blame] | 760 | #define CALL_FN_W_WW(lval, orig, arg1,arg2) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 761 | do { \ |
sewardj | c885844 | 2006-01-20 15:17:20 +0000 | [diff] [blame] | 762 | volatile OrigFn _orig = (orig); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 763 | volatile unsigned long _argvec[3]; \ |
| 764 | volatile unsigned long _res; \ |
sewardj | c885844 | 2006-01-20 15:17:20 +0000 | [diff] [blame] | 765 | _argvec[0] = (unsigned long)_orig.nraddr; \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 766 | _argvec[1] = (unsigned long)(arg1); \ |
| 767 | _argvec[2] = (unsigned long)(arg2); \ |
| 768 | __asm__ volatile( \ |
| 769 | "movq 16(%%rax), %%rsi\n\t" \ |
| 770 | "movq 8(%%rax), %%rdi\n\t" \ |
| 771 | "movq (%%rax), %%rax\n\t" /* target->%rax */ \ |
| 772 | VALGRIND_CALL_NOREDIR_RAX \ |
| 773 | : /*out*/ "=a" (_res) \ |
| 774 | : /*in*/ "a" (&_argvec[0]) \ |
| 775 | : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \ |
| 776 | ); \ |
| 777 | lval = (__typeof__(lval)) _res; \ |
| 778 | } while (0) |
| 779 | |
| 780 | #endif /* ARCH_amd64 */ |
| 781 | |
| 782 | /* --------------------------- ppc32 --------------------------- */ |
| 783 | |
| 784 | #if defined(ARCH_ppc32) |
| 785 | |
| 786 | /* ARGREGS: r3 r4 r5 r6 r7 r8 r9 r10 (the rest on stack somewhere) */ |
| 787 | |
| 788 | /* These regs are trashed by the hidden call. */ |
| 789 | #define __CALLER_SAVED_REGS "lr", \ |
| 790 | "r0", "r2", "r3", "r4", "r5", "r6", \ |
| 791 | "r7", "r8", "r9", "r10", "r11", "r12" |
| 792 | |
| 793 | /* These CALL_FN_ macros assume that on ppc32-linux, sizeof(unsigned |
| 794 | long) == 4. */ |
| 795 | |
sewardj | 38de099 | 2006-01-20 16:46:34 +0000 | [diff] [blame] | 796 | #define CALL_FN_W_v(lval, orig) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 797 | do { \ |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 798 | volatile OrigFn _orig = (orig); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 799 | volatile unsigned long _argvec[1]; \ |
| 800 | volatile unsigned long _res; \ |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 801 | _argvec[0] = (unsigned long)_orig.nraddr; \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 802 | __asm__ volatile( \ |
| 803 | "mr 11,%1\n\t" \ |
| 804 | "lwz 11,0(11)\n\t" /* target->r11 */ \ |
| 805 | VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11 \ |
| 806 | "mr %0,3" \ |
| 807 | : /*out*/ "=r" (_res) \ |
| 808 | : /*in*/ "r" (&_argvec[0]) \ |
| 809 | : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \ |
| 810 | ); \ |
| 811 | lval = (__typeof__(lval)) _res; \ |
| 812 | } while (0) |
| 813 | |
sewardj | 38de099 | 2006-01-20 16:46:34 +0000 | [diff] [blame] | 814 | #define CALL_FN_W_W(lval, orig, arg1) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 815 | do { \ |
sewardj | 38de099 | 2006-01-20 16:46:34 +0000 | [diff] [blame] | 816 | volatile OrigFn _orig = (orig); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 817 | volatile unsigned long _argvec[2]; \ |
| 818 | volatile unsigned long _res; \ |
sewardj | 38de099 | 2006-01-20 16:46:34 +0000 | [diff] [blame] | 819 | _argvec[0] = (unsigned long)_orig.nraddr; \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 820 | _argvec[1] = (unsigned long)arg1; \ |
| 821 | __asm__ volatile( \ |
| 822 | "mr 11,%1\n\t" \ |
| 823 | "lwz 3,4(11)\n\t" /* arg1->r3 */ \ |
| 824 | "lwz 11,0(11)\n\t" /* target->r11 */ \ |
| 825 | VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11 \ |
| 826 | "mr %0,3" \ |
| 827 | : /*out*/ "=r" (_res) \ |
| 828 | : /*in*/ "r" (&_argvec[0]) \ |
| 829 | : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \ |
| 830 | ); \ |
| 831 | lval = (__typeof__(lval)) _res; \ |
| 832 | } while (0) |
| 833 | |
sewardj | 38de099 | 2006-01-20 16:46:34 +0000 | [diff] [blame] | 834 | #define CALL_FN_W_WW(lval, orig, arg1,arg2) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 835 | do { \ |
sewardj | 38de099 | 2006-01-20 16:46:34 +0000 | [diff] [blame] | 836 | volatile OrigFn _orig = (orig); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 837 | volatile unsigned long _argvec[3]; \ |
| 838 | volatile unsigned long _res; \ |
sewardj | 38de099 | 2006-01-20 16:46:34 +0000 | [diff] [blame] | 839 | _argvec[0] = (unsigned long)_orig.nraddr; \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 840 | _argvec[1] = (unsigned long)arg1; \ |
| 841 | _argvec[2] = (unsigned long)arg2; \ |
| 842 | __asm__ volatile( \ |
| 843 | "mr 11,%1\n\t" \ |
| 844 | "lwz 3,4(11)\n\t" /* arg1->r3 */ \ |
| 845 | "lwz 4,8(11)\n\t" \ |
| 846 | "lwz 11,0(11)\n\t" /* target->r11 */ \ |
| 847 | VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11 \ |
| 848 | "mr %0,3" \ |
| 849 | : /*out*/ "=r" (_res) \ |
| 850 | : /*in*/ "r" (&_argvec[0]) \ |
| 851 | : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \ |
| 852 | ); \ |
| 853 | lval = (__typeof__(lval)) _res; \ |
| 854 | } while (0) |
| 855 | |
| 856 | #endif /* ARCH_ppc32 */ |
| 857 | |
| 858 | /* --------------------------- ppc64 --------------------------- */ |
| 859 | |
sewardj | 9734b20 | 2006-01-17 01:49:37 +0000 | [diff] [blame] | 860 | #if defined(ARCH_ppc64) |
| 861 | |
| 862 | /* ARGREGS: r3 r4 r5 r6 r7 r8 r9 r10 (the rest on stack somewhere) */ |
| 863 | |
| 864 | /* These regs are trashed by the hidden call. */ |
| 865 | #define __CALLER_SAVED_REGS "lr", \ |
| 866 | "r0", "r2", "r3", "r4", "r5", "r6", \ |
| 867 | "r7", "r8", "r9", "r10", "r11", "r12" |
| 868 | |
| 869 | /* These CALL_FN_ macros assume that on ppc64-linux, sizeof(unsigned |
| 870 | long) == 8. */ |
| 871 | |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 872 | #define CALL_FN_W_v(lval, orig) \ |
sewardj | 9734b20 | 2006-01-17 01:49:37 +0000 | [diff] [blame] | 873 | do { \ |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 874 | volatile OrigFn _orig = (orig); \ |
| 875 | volatile unsigned long _argvec[3+0]; \ |
sewardj | 9734b20 | 2006-01-17 01:49:37 +0000 | [diff] [blame] | 876 | volatile unsigned long _res; \ |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 877 | /* _argvec[0] holds current r2 across the call */ \ |
| 878 | _argvec[1] = (unsigned long)_orig.r2; \ |
| 879 | _argvec[2] = (unsigned long)_orig.nraddr; \ |
sewardj | 9734b20 | 2006-01-17 01:49:37 +0000 | [diff] [blame] | 880 | __asm__ volatile( \ |
| 881 | "mr 11,%1\n\t" \ |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 882 | "std 2,-16(11)\n\t" /* save tocptr */ \ |
| 883 | "ld 2,-8(11)\n\t" /* use nraddr's tocptr */ \ |
| 884 | "ld 11, 0(11)\n\t" /* target->r11 */ \ |
sewardj | 9734b20 | 2006-01-17 01:49:37 +0000 | [diff] [blame] | 885 | VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11 \ |
| 886 | "mr 11,%1\n\t" \ |
| 887 | "mr %0,3\n\t" \ |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 888 | "ld 2,-16(11)" /* restore tocptr */ \ |
sewardj | 9734b20 | 2006-01-17 01:49:37 +0000 | [diff] [blame] | 889 | : /*out*/ "=r" (_res) \ |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 890 | : /*in*/ "r" (&_argvec[2]) \ |
sewardj | 9734b20 | 2006-01-17 01:49:37 +0000 | [diff] [blame] | 891 | : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \ |
| 892 | ); \ |
| 893 | lval = (__typeof__(lval)) _res; \ |
| 894 | } while (0) |
| 895 | |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 896 | #define CALL_FN_W_W(lval, orig, arg1) \ |
sewardj | 9734b20 | 2006-01-17 01:49:37 +0000 | [diff] [blame] | 897 | do { \ |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 898 | volatile OrigFn _orig = (orig); \ |
| 899 | volatile unsigned long _argvec[3+1]; \ |
sewardj | 9734b20 | 2006-01-17 01:49:37 +0000 | [diff] [blame] | 900 | volatile unsigned long _res; \ |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 901 | /* _argvec[0] holds current r2 across the call */ \ |
| 902 | _argvec[1] = (unsigned long)_orig.r2; \ |
| 903 | _argvec[2] = (unsigned long)_orig.nraddr; \ |
| 904 | _argvec[2+1] = (unsigned long)arg1; \ |
sewardj | 9734b20 | 2006-01-17 01:49:37 +0000 | [diff] [blame] | 905 | __asm__ volatile( \ |
| 906 | "mr 11,%1\n\t" \ |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 907 | "std 2,-16(11)\n\t" /* save tocptr */ \ |
| 908 | "ld 2,-8(11)\n\t" /* use nraddr's tocptr */ \ |
| 909 | "ld 3, 8(11)\n\t" /* arg1->r3 */ \ |
| 910 | "ld 11, 0(11)\n\t" /* target->r11 */ \ |
sewardj | 9734b20 | 2006-01-17 01:49:37 +0000 | [diff] [blame] | 911 | VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11 \ |
| 912 | "mr 11,%1\n\t" \ |
| 913 | "mr %0,3\n\t" \ |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 914 | "ld 2,-16(11)" /* restore tocptr */ \ |
sewardj | 9734b20 | 2006-01-17 01:49:37 +0000 | [diff] [blame] | 915 | : /*out*/ "=r" (_res) \ |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 916 | : /*in*/ "r" (&_argvec[2]) \ |
sewardj | 9734b20 | 2006-01-17 01:49:37 +0000 | [diff] [blame] | 917 | : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \ |
| 918 | ); \ |
| 919 | lval = (__typeof__(lval)) _res; \ |
| 920 | } while (0) |
| 921 | |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 922 | #define CALL_FN_W_WW(lval, orig, arg1,arg2) \ |
sewardj | 9734b20 | 2006-01-17 01:49:37 +0000 | [diff] [blame] | 923 | do { \ |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 924 | volatile OrigFn _orig = (orig); \ |
| 925 | volatile unsigned long _argvec[3+2]; \ |
sewardj | 9734b20 | 2006-01-17 01:49:37 +0000 | [diff] [blame] | 926 | volatile unsigned long _res; \ |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 927 | /* _argvec[0] holds current r2 across the call */ \ |
| 928 | _argvec[1] = (unsigned long)_orig.r2; \ |
| 929 | _argvec[2] = (unsigned long)_orig.nraddr; \ |
| 930 | _argvec[2+1] = (unsigned long)arg1; \ |
| 931 | _argvec[2+2] = (unsigned long)arg2; \ |
sewardj | 9734b20 | 2006-01-17 01:49:37 +0000 | [diff] [blame] | 932 | __asm__ volatile( \ |
| 933 | "mr 11,%1\n\t" \ |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 934 | "std 2,-16(11)\n\t" /* save tocptr */ \ |
| 935 | "ld 2,-8(11)\n\t" /* use nraddr's tocptr */ \ |
| 936 | "ld 3, 8(11)\n\t" /* arg1->r3 */ \ |
| 937 | "ld 4, 16(11)\n\t" /* arg1->r4 */ \ |
| 938 | "ld 11, 0(11)\n\t" /* target->r11 */ \ |
sewardj | 9734b20 | 2006-01-17 01:49:37 +0000 | [diff] [blame] | 939 | VALGRIND_BRANCH_AND_LINK_TO_NOREDIR_R11 \ |
| 940 | "mr 11,%1\n\t" \ |
| 941 | "mr %0,3\n\t" \ |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 942 | "ld 2,-16(11)" /* restore tocptr */ \ |
sewardj | 9734b20 | 2006-01-17 01:49:37 +0000 | [diff] [blame] | 943 | : /*out*/ "=r" (_res) \ |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 944 | : /*in*/ "r" (&_argvec[2]) \ |
sewardj | 9734b20 | 2006-01-17 01:49:37 +0000 | [diff] [blame] | 945 | : /*trash*/ "cc", "memory", __CALLER_SAVED_REGS \ |
| 946 | ); \ |
| 947 | lval = (__typeof__(lval)) _res; \ |
| 948 | } while (0) |
| 949 | |
| 950 | #endif /* ARCH_ppc64 */ |
| 951 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 952 | |
| 953 | /* ------------------------------------------------------------------ */ |
| 954 | /* ARCHITECTURE INDEPENDENT MACROS for CLIENT REQUESTS. */ |
| 955 | /* */ |
njn | 30d76c6 | 2005-06-18 15:07:39 +0000 | [diff] [blame] | 956 | /* ------------------------------------------------------------------ */ |
| 957 | |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 958 | /* Some request codes. There are many more of these, but most are not |
| 959 | exposed to end-user view. These are the public ones, all of the |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 960 | form 0x1000 + small_number. |
njn | d799418 | 2003-10-02 13:44:04 +0000 | [diff] [blame] | 961 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 962 | Core ones are in the range 0x00000000--0x0000ffff. The non-public |
| 963 | ones start at 0x2000. |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 964 | */ |
| 965 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 966 | /* These macros are used by tools -- they must be public, but don't |
| 967 | embed them into other programs. */ |
njn | fc26ff9 | 2004-11-22 19:12:49 +0000 | [diff] [blame] | 968 | #define VG_USERREQ_TOOL_BASE(a,b) \ |
njn | 4c79121 | 2003-05-02 17:53:54 +0000 | [diff] [blame] | 969 | ((unsigned int)(((a)&0xff) << 24 | ((b)&0xff) << 16)) |
njn | fc26ff9 | 2004-11-22 19:12:49 +0000 | [diff] [blame] | 970 | #define VG_IS_TOOL_USERREQ(a, b, v) \ |
| 971 | (VG_USERREQ_TOOL_BASE(a,b) == ((v) & 0xffff0000)) |
sewardj | 3404251 | 2002-10-22 04:14:35 +0000 | [diff] [blame] | 972 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 973 | typedef |
njn | 4c79121 | 2003-05-02 17:53:54 +0000 | [diff] [blame] | 974 | enum { VG_USERREQ__RUNNING_ON_VALGRIND = 0x1001, |
| 975 | VG_USERREQ__DISCARD_TRANSLATIONS = 0x1002, |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 976 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 977 | /* These allow any function to be called from the simulated |
| 978 | CPU but run on the real CPU. Nb: the first arg passed to |
| 979 | the function is always the ThreadId of the running |
| 980 | thread! So CLIENT_CALL0 actually requires a 1 arg |
njn | d4795be | 2004-11-24 11:57:51 +0000 | [diff] [blame] | 981 | function, etc. */ |
njn | 4c79121 | 2003-05-02 17:53:54 +0000 | [diff] [blame] | 982 | VG_USERREQ__CLIENT_CALL0 = 0x1101, |
| 983 | VG_USERREQ__CLIENT_CALL1 = 0x1102, |
| 984 | VG_USERREQ__CLIENT_CALL2 = 0x1103, |
| 985 | VG_USERREQ__CLIENT_CALL3 = 0x1104, |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 986 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 987 | /* Can be useful in regression testing suites -- eg. can |
| 988 | send Valgrind's output to /dev/null and still count |
| 989 | errors. */ |
njn | 4c79121 | 2003-05-02 17:53:54 +0000 | [diff] [blame] | 990 | VG_USERREQ__COUNT_ERRORS = 0x1201, |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 991 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 992 | /* These are useful and can be interpreted by any tool that |
| 993 | tracks malloc() et al, by using vg_replace_malloc.c. */ |
njn | d799418 | 2003-10-02 13:44:04 +0000 | [diff] [blame] | 994 | VG_USERREQ__MALLOCLIKE_BLOCK = 0x1301, |
| 995 | VG_USERREQ__FREELIKE_BLOCK = 0x1302, |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 996 | /* Memory pool support. */ |
| 997 | VG_USERREQ__CREATE_MEMPOOL = 0x1303, |
| 998 | VG_USERREQ__DESTROY_MEMPOOL = 0x1304, |
| 999 | VG_USERREQ__MEMPOOL_ALLOC = 0x1305, |
| 1000 | VG_USERREQ__MEMPOOL_FREE = 0x1306, |
njn | d799418 | 2003-10-02 13:44:04 +0000 | [diff] [blame] | 1001 | |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 1002 | /* Allow printfs to valgrind log. */ |
njn | 30d76c6 | 2005-06-18 15:07:39 +0000 | [diff] [blame] | 1003 | VG_USERREQ__PRINTF = 0x1401, |
rjwalsh | 0140af5 | 2005-06-04 20:42:33 +0000 | [diff] [blame] | 1004 | VG_USERREQ__PRINTF_BACKTRACE = 0x1402, |
| 1005 | |
| 1006 | /* Stack support. */ |
| 1007 | VG_USERREQ__STACK_REGISTER = 0x1501, |
| 1008 | VG_USERREQ__STACK_DEREGISTER = 0x1502, |
| 1009 | VG_USERREQ__STACK_CHANGE = 0x1503, |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1010 | } Vg_ClientRequest; |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 1011 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1012 | #if !defined(__GNUC__) |
| 1013 | # define __extension__ /* */ |
mueller | c9b3655 | 2003-12-31 14:32:23 +0000 | [diff] [blame] | 1014 | #endif |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 1015 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1016 | /* Returns the number of Valgrinds this code is running under. That |
| 1017 | is, 0 if running natively, 1 if running under Valgrind, 2 if |
| 1018 | running under Valgrind which is running under another Valgrind, |
| 1019 | etc. */ |
| 1020 | #define RUNNING_ON_VALGRIND __extension__ \ |
| 1021 | ({unsigned int _qzz_res; \ |
| 1022 | VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0 /* if not */, \ |
| 1023 | VG_USERREQ__RUNNING_ON_VALGRIND, \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 1024 | 0, 0, 0, 0, 0); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1025 | _qzz_res; \ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1026 | }) |
| 1027 | |
| 1028 | |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 1029 | /* Discard translation of code in the range [_qzz_addr .. _qzz_addr + |
| 1030 | _qzz_len - 1]. Useful if you are debugging a JITter or some such, |
| 1031 | since it provides a way to make sure valgrind will retranslate the |
| 1032 | invalidated area. Returns no value. */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1033 | #define VALGRIND_DISCARD_TRANSLATIONS(_qzz_addr,_qzz_len) \ |
| 1034 | {unsigned int _qzz_res; \ |
| 1035 | VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \ |
| 1036 | VG_USERREQ__DISCARD_TRANSLATIONS, \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 1037 | _qzz_addr, _qzz_len, 0, 0, 0); \ |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
njn | 26aba4d | 2005-05-16 13:31:23 +0000 | [diff] [blame] | 1040 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1041 | /* These requests are for getting Valgrind itself to print something. |
| 1042 | Possibly with a backtrace. This is a really ugly hack. */ |
| 1043 | |
| 1044 | #if defined(NVALGRIND) |
| 1045 | |
| 1046 | # define VALGRIND_PRINTF(...) |
| 1047 | # define VALGRIND_PRINTF_BACKTRACE(...) |
njn | 26aba4d | 2005-05-16 13:31:23 +0000 | [diff] [blame] | 1048 | |
| 1049 | #else /* NVALGRIND */ |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 1050 | |
fitzhardinge | a09a1b5 | 2003-11-07 23:09:48 +0000 | [diff] [blame] | 1051 | int VALGRIND_PRINTF(const char *format, ...) |
| 1052 | __attribute__((format(__printf__, 1, 2))); |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 1053 | __attribute__((weak)) |
| 1054 | int |
fitzhardinge | a09a1b5 | 2003-11-07 23:09:48 +0000 | [diff] [blame] | 1055 | VALGRIND_PRINTF(const char *format, ...) |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 1056 | { |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 1057 | unsigned long _qzz_res; |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 1058 | va_list vargs; |
| 1059 | va_start(vargs, format); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1060 | VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, VG_USERREQ__PRINTF, |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 1061 | (unsigned long)format, (unsigned long)vargs, |
| 1062 | 0, 0, 0); |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 1063 | va_end(vargs); |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 1064 | return (int)_qzz_res; |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
fitzhardinge | a09a1b5 | 2003-11-07 23:09:48 +0000 | [diff] [blame] | 1067 | int VALGRIND_PRINTF_BACKTRACE(const char *format, ...) |
| 1068 | __attribute__((format(__printf__, 1, 2))); |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 1069 | __attribute__((weak)) |
| 1070 | int |
fitzhardinge | a09a1b5 | 2003-11-07 23:09:48 +0000 | [diff] [blame] | 1071 | VALGRIND_PRINTF_BACKTRACE(const char *format, ...) |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 1072 | { |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 1073 | unsigned long _qzz_res; |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 1074 | va_list vargs; |
| 1075 | va_start(vargs, format); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1076 | VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, VG_USERREQ__PRINTF_BACKTRACE, |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 1077 | (unsigned long)format, (unsigned long)vargs, |
| 1078 | 0, 0, 0); |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 1079 | va_end(vargs); |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 1080 | return (int)_qzz_res; |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 1083 | #endif /* NVALGRIND */ |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 1084 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1085 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1086 | /* These requests allow control to move from the simulated CPU to the |
| 1087 | real CPU, calling an arbitary function */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1088 | #define VALGRIND_NON_SIMD_CALL0(_qyy_fn) \ |
| 1089 | ({unsigned long _qyy_res; \ |
| 1090 | VALGRIND_DO_CLIENT_REQUEST(_qyy_res, 0 /* default return */, \ |
| 1091 | VG_USERREQ__CLIENT_CALL0, \ |
| 1092 | _qyy_fn, \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 1093 | 0, 0, 0, 0); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1094 | _qyy_res; \ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1095 | }) |
| 1096 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1097 | #define VALGRIND_NON_SIMD_CALL1(_qyy_fn, _qyy_arg1) \ |
| 1098 | ({unsigned long _qyy_res; \ |
| 1099 | VALGRIND_DO_CLIENT_REQUEST(_qyy_res, 0 /* default return */, \ |
| 1100 | VG_USERREQ__CLIENT_CALL1, \ |
| 1101 | _qyy_fn, \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 1102 | _qyy_arg1, 0, 0, 0); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1103 | _qyy_res; \ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1104 | }) |
| 1105 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1106 | #define VALGRIND_NON_SIMD_CALL2(_qyy_fn, _qyy_arg1, _qyy_arg2) \ |
| 1107 | ({unsigned long _qyy_res; \ |
| 1108 | VALGRIND_DO_CLIENT_REQUEST(_qyy_res, 0 /* default return */, \ |
| 1109 | VG_USERREQ__CLIENT_CALL2, \ |
| 1110 | _qyy_fn, \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 1111 | _qyy_arg1, _qyy_arg2, 0, 0); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1112 | _qyy_res; \ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1113 | }) |
| 1114 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1115 | #define VALGRIND_NON_SIMD_CALL3(_qyy_fn, _qyy_arg1, _qyy_arg2, _qyy_arg3) \ |
| 1116 | ({unsigned long _qyy_res; \ |
| 1117 | VALGRIND_DO_CLIENT_REQUEST(_qyy_res, 0 /* default return */, \ |
| 1118 | VG_USERREQ__CLIENT_CALL3, \ |
| 1119 | _qyy_fn, \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 1120 | _qyy_arg1, _qyy_arg2, \ |
| 1121 | _qyy_arg3, 0); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1122 | _qyy_res; \ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1123 | }) |
| 1124 | |
| 1125 | |
nethercote | 7cc9c23 | 2004-01-21 15:08:04 +0000 | [diff] [blame] | 1126 | /* Counts the number of errors that have been recorded by a tool. Nb: |
| 1127 | the tool must record the errors with VG_(maybe_record_error)() or |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 1128 | VG_(unique_error)() for them to be counted. */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1129 | #define VALGRIND_COUNT_ERRORS \ |
| 1130 | ({unsigned int _qyy_res; \ |
| 1131 | VALGRIND_DO_CLIENT_REQUEST(_qyy_res, 0 /* default return */, \ |
| 1132 | VG_USERREQ__COUNT_ERRORS, \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 1133 | 0, 0, 0, 0, 0); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1134 | _qyy_res; \ |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 1135 | }) |
| 1136 | |
njn | d799418 | 2003-10-02 13:44:04 +0000 | [diff] [blame] | 1137 | /* Mark a block of memory as having been allocated by a malloc()-like |
| 1138 | function. `addr' is the start of the usable block (ie. after any |
| 1139 | redzone) `rzB' is redzone size if the allocator can apply redzones; |
| 1140 | use '0' if not. Adding redzones makes it more likely Valgrind will spot |
| 1141 | block overruns. `is_zeroed' indicates if the memory is zeroed, as it is |
| 1142 | for calloc(). Put it immediately after the point where a block is |
| 1143 | allocated. |
| 1144 | |
| 1145 | If you're allocating memory via superblocks, and then handing out small |
| 1146 | chunks of each superblock, if you don't have redzones on your small |
| 1147 | blocks, it's worth marking the superblock with VALGRIND_MAKE_NOACCESS |
| 1148 | when it's created, so that block overruns are detected. But if you can |
| 1149 | put redzones on, it's probably better to not do this, so that messages |
| 1150 | for small overruns are described in terms of the small block rather than |
| 1151 | the superblock (but if you have a big overrun that skips over a redzone, |
| 1152 | you could miss an error this way). See memcheck/tests/custom_alloc.c |
| 1153 | for an example. |
| 1154 | |
| 1155 | Nb: block must be freed via a free()-like function specified |
| 1156 | with VALGRIND_FREELIKE_BLOCK or mismatch errors will occur. */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1157 | #define VALGRIND_MALLOCLIKE_BLOCK(addr, sizeB, rzB, is_zeroed) \ |
| 1158 | {unsigned int _qzz_res; \ |
| 1159 | VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \ |
| 1160 | VG_USERREQ__MALLOCLIKE_BLOCK, \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 1161 | addr, sizeB, rzB, is_zeroed, 0); \ |
njn | d799418 | 2003-10-02 13:44:04 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | /* Mark a block of memory as having been freed by a free()-like function. |
| 1165 | `rzB' is redzone size; it must match that given to |
| 1166 | VALGRIND_MALLOCLIKE_BLOCK. Memory not freed will be detected by the leak |
| 1167 | checker. Put it immediately after the point where the block is freed. */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1168 | #define VALGRIND_FREELIKE_BLOCK(addr, rzB) \ |
| 1169 | {unsigned int _qzz_res; \ |
| 1170 | VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \ |
| 1171 | VG_USERREQ__FREELIKE_BLOCK, \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 1172 | addr, rzB, 0, 0, 0); \ |
njn | d799418 | 2003-10-02 13:44:04 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 1175 | /* Create a memory pool. */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1176 | #define VALGRIND_CREATE_MEMPOOL(pool, rzB, is_zeroed) \ |
| 1177 | {unsigned int _qzz_res; \ |
| 1178 | VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \ |
| 1179 | VG_USERREQ__CREATE_MEMPOOL, \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 1180 | pool, rzB, is_zeroed, 0, 0); \ |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 1181 | } |
| 1182 | |
| 1183 | /* Destroy a memory pool. */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1184 | #define VALGRIND_DESTROY_MEMPOOL(pool) \ |
| 1185 | {unsigned int _qzz_res; \ |
| 1186 | VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \ |
| 1187 | VG_USERREQ__DESTROY_MEMPOOL, \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 1188 | pool, 0, 0, 0, 0); \ |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | /* Associate a piece of memory with a memory pool. */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1192 | #define VALGRIND_MEMPOOL_ALLOC(pool, addr, size) \ |
| 1193 | {unsigned int _qzz_res; \ |
| 1194 | VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \ |
| 1195 | VG_USERREQ__MEMPOOL_ALLOC, \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 1196 | pool, addr, size, 0, 0); \ |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | /* Disassociate a piece of memory from a memory pool. */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1200 | #define VALGRIND_MEMPOOL_FREE(pool, addr) \ |
| 1201 | {unsigned int _qzz_res; \ |
| 1202 | VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \ |
| 1203 | VG_USERREQ__MEMPOOL_FREE, \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 1204 | pool, addr, 0, 0, 0); \ |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 1205 | } |
| 1206 | |
rjwalsh | 0140af5 | 2005-06-04 20:42:33 +0000 | [diff] [blame] | 1207 | /* Mark a piece of memory as being a stack. Returns a stack id. */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1208 | #define VALGRIND_STACK_REGISTER(start, end) \ |
| 1209 | ({unsigned int _qzz_res; \ |
| 1210 | VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \ |
| 1211 | VG_USERREQ__STACK_REGISTER, \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 1212 | start, end, 0, 0, 0); \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1213 | _qzz_res; \ |
rjwalsh | 0140af5 | 2005-06-04 20:42:33 +0000 | [diff] [blame] | 1214 | }) |
| 1215 | |
| 1216 | /* Unmark the piece of memory associated with a stack id as being a |
| 1217 | stack. */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1218 | #define VALGRIND_STACK_DEREGISTER(id) \ |
| 1219 | {unsigned int _qzz_res; \ |
| 1220 | VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \ |
| 1221 | VG_USERREQ__STACK_DEREGISTER, \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 1222 | id, 0, 0, 0, 0); \ |
rjwalsh | 0140af5 | 2005-06-04 20:42:33 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | /* Change the start and end address of the stack id. */ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1226 | #define VALGRIND_STACK_CHANGE(id, start, end) \ |
| 1227 | {unsigned int _qzz_res; \ |
| 1228 | VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \ |
| 1229 | VG_USERREQ__STACK_CHANGE, \ |
sewardj | 9af10a1 | 2006-02-01 14:59:42 +0000 | [diff] [blame] | 1230 | id, start, end, 0, 0); \ |
rjwalsh | 0140af5 | 2005-06-04 20:42:33 +0000 | [diff] [blame] | 1231 | } |
| 1232 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1233 | |
| 1234 | #undef ARCH_x86 |
| 1235 | #undef ARCH_amd64 |
| 1236 | #undef ARCH_ppc32 |
| 1237 | #undef ARCH_ppc64 |
| 1238 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1239 | #endif /* __VALGRIND_H */ |