| 1 | /* |
| 2 | * ========== Copyright Header Begin ========================================== |
| 3 | * |
| 4 | * OpenSPARC T2 Processor File: SS_PythonTracer.h |
| 5 | * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. |
| 6 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES. |
| 7 | * |
| 8 | * The above named program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public |
| 10 | * License version 2 as published by the Free Software Foundation. |
| 11 | * |
| 12 | * The above named program is distributed in the hope that it will be |
| 13 | * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public |
| 18 | * License along with this work; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | * |
| 21 | * ========== Copyright Header End ============================================ |
| 22 | */ |
| 23 | |
| 24 | #ifndef __SS_PythonTracer_h__ |
| 25 | #define __SS_PythonTracer_h__ |
| 26 | |
| 27 | #include "SS_Tracer.h" |
| 28 | |
| 29 | class SS_PythonTracer : public SS_Tracer |
| 30 | { |
| 31 | public: |
| 32 | SS_PythonTracer() |
| 33 | : |
| 34 | SS_Tracer(), |
| 35 | py_exe_instr(0), |
| 36 | py_reg_value(0), |
| 37 | py_trap(0), |
| 38 | py_mem_access(0), |
| 39 | py_tlb_update(0), |
| 40 | py_end_instr(0) |
| 41 | {} |
| 42 | |
| 43 | static void py_hook_exe_instr( SS_Tracer* trc, SS_Vaddr pc, SS_Tte* tte, SS_Instr* i ) |
| 44 | { |
| 45 | PyGILState_STATE gstate; |
| 46 | gstate = PyGILState_Ensure(); |
| 47 | |
| 48 | SS_PythonTracer* py_trc = (SS_PythonTracer*)trc; |
| 49 | PyObject *func, *args, *rslt; |
| 50 | func = (PyObject *)py_trc->py_exe_instr; |
| 51 | args = Py_BuildValue((char*)"Kll",pc,tte,i); |
| 52 | rslt = PyEval_CallObject(func,args); |
| 53 | Py_DECREF(args); |
| 54 | if (rslt) |
| 55 | Py_DECREF(rslt); |
| 56 | |
| 57 | PyGILState_Release(gstate); |
| 58 | } |
| 59 | |
| 60 | static void py_hook_reg_value( SS_Tracer* trc, SS_Registers::Index index, uint64_t value ) |
| 61 | { |
| 62 | PyGILState_STATE gstate; |
| 63 | gstate = PyGILState_Ensure(); |
| 64 | |
| 65 | SS_PythonTracer* py_trc = (SS_PythonTracer*)trc; |
| 66 | PyObject *func, *args, *rslt; |
| 67 | func = (PyObject *)py_trc->py_reg_value; |
| 68 | args = Py_BuildValue((char*)"iK",index,value); |
| 69 | rslt = PyEval_CallObject(func,args); |
| 70 | Py_DECREF(args); |
| 71 | if (rslt) |
| 72 | Py_DECREF(rslt); |
| 73 | |
| 74 | PyGILState_Release(gstate); |
| 75 | } |
| 76 | |
| 77 | static void py_hook_trap( SS_Tracer* trc, SS_Trap::Type type, TrapMode mode, SS_Vaddr addr ) |
| 78 | { |
| 79 | PyGILState_STATE gstate; |
| 80 | gstate = PyGILState_Ensure(); |
| 81 | |
| 82 | SS_PythonTracer* py_trc = (SS_PythonTracer*)trc; |
| 83 | PyObject *func, *args, *rslt; |
| 84 | func = (PyObject *)py_trc->py_trap; |
| 85 | args = Py_BuildValue((char*)"iiK",type,mode,addr); |
| 86 | rslt = PyEval_CallObject(func,args); |
| 87 | Py_DECREF(args); |
| 88 | if (rslt) |
| 89 | Py_DECREF(rslt); |
| 90 | |
| 91 | PyGILState_Release(gstate); |
| 92 | } |
| 93 | |
| 94 | static void py_hook_mem_access( SS_Tracer* trc, MemAccess type, SS_Vaddr va, SS_Tte* tte, uint_t size, uint64_t* data ) |
| 95 | { |
| 96 | PyGILState_STATE gstate; |
| 97 | gstate = PyGILState_Ensure(); |
| 98 | |
| 99 | SS_PythonTracer* py_trc = (SS_PythonTracer*)trc; |
| 100 | PyObject *func, *args, *rslt; |
| 101 | func = (PyObject *)py_trc->py_mem_access; |
| 102 | switch (type) |
| 103 | { |
| 104 | case LD_CODE: |
| 105 | case LD_DATA: |
| 106 | case ST_DATA: |
| 107 | case LD_SWAP: |
| 108 | case ST_SWAP: |
| 109 | case LD_CAS: |
| 110 | case ST_LDST: |
| 111 | case LD_LDST: |
| 112 | switch (size) |
| 113 | { |
| 114 | case 1: |
| 115 | case 2: |
| 116 | case 4: |
| 117 | case 8: |
| 118 | args = Py_BuildValue((char*)"iKli[K]",type,va,tte,size,data[0]); |
| 119 | break; |
| 120 | case 16: |
| 121 | args = Py_BuildValue((char*)"iKli[K,K]",type,va,tte,size,data[0],data[1]); |
| 122 | break; |
| 123 | case 64: |
| 124 | args = Py_BuildValue((char*)"iKli[K,K,K,K,K,K,K,K]",type,va,tte,size, |
| 125 | data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7]); |
| 126 | break; |
| 127 | default: |
| 128 | assert(0); |
| 129 | } |
| 130 | break; |
| 131 | case ST_PART: |
| 132 | case ST_CAS: |
| 133 | args = Py_BuildValue((char*)"iKli[K,K]",type,va,tte,size,data[0],data[1]); |
| 134 | break; |
| 135 | case PREFETCH: |
| 136 | case FLUSH: |
| 137 | args = Py_BuildValue((char*)"iKli[]",type,va,tte,size); |
| 138 | break; |
| 139 | default: |
| 140 | assert(0); |
| 141 | } |
| 142 | rslt = PyEval_CallObject(func,args); |
| 143 | Py_DECREF(args); |
| 144 | if (rslt) |
| 145 | Py_DECREF(rslt); |
| 146 | |
| 147 | PyGILState_Release(gstate); |
| 148 | } |
| 149 | |
| 150 | static void py_hook_tlb_update( SS_Tracer* trc, bool insert, SS_Tlb* tlb, uint_t index, SS_Tte* tte ) |
| 151 | { |
| 152 | PyGILState_STATE gstate; |
| 153 | gstate = PyGILState_Ensure(); |
| 154 | |
| 155 | SS_PythonTracer* py_trc = (SS_PythonTracer*)trc; |
| 156 | PyObject *func, *args, *rslt; |
| 157 | func = (PyObject *)py_trc->py_tlb_update; |
| 158 | args = Py_BuildValue((char*)"ilil",insert,tlb,index,tte); |
| 159 | rslt = PyEval_CallObject(func,args); |
| 160 | Py_DECREF(args); |
| 161 | if (rslt) |
| 162 | Py_DECREF(rslt); |
| 163 | |
| 164 | PyGILState_Release(gstate); |
| 165 | } |
| 166 | |
| 167 | static void py_hook_end_instr( SS_Tracer* trc ) |
| 168 | { |
| 169 | PyGILState_STATE gstate; |
| 170 | gstate = PyGILState_Ensure(); |
| 171 | |
| 172 | SS_PythonTracer* py_trc = (SS_PythonTracer*)trc; |
| 173 | PyObject *func, *args, *rslt; |
| 174 | func = (PyObject *)py_trc->py_end_instr; |
| 175 | args = Py_BuildValue((char*)""); |
| 176 | rslt = PyEval_CallObject(func,args); |
| 177 | Py_DECREF(args); |
| 178 | if (rslt) |
| 179 | Py_DECREF(rslt); |
| 180 | |
| 181 | PyGILState_Release(gstate); |
| 182 | } |
| 183 | |
| 184 | void clr_exe_instr() |
| 185 | { |
| 186 | if (py_exe_instr) |
| 187 | Py_DECREF(py_exe_instr); |
| 188 | py_exe_instr = 0; |
| 189 | hook_exe_instr = 0; |
| 190 | } |
| 191 | void set_exe_instr( PyObject* func ) |
| 192 | { |
| 193 | Py_INCREF(func); |
| 194 | if (py_exe_instr) |
| 195 | Py_DECREF(py_exe_instr); |
| 196 | py_exe_instr = func; |
| 197 | hook_exe_instr = &py_hook_exe_instr; |
| 198 | } |
| 199 | |
| 200 | void clr_reg_value() |
| 201 | { |
| 202 | if (py_reg_value) |
| 203 | Py_DECREF(py_reg_value); |
| 204 | py_reg_value = 0; |
| 205 | hook_reg_value = 0; |
| 206 | } |
| 207 | void set_reg_value( PyObject* func ) |
| 208 | { |
| 209 | Py_INCREF(func); |
| 210 | if (py_reg_value) |
| 211 | Py_DECREF(py_reg_value); |
| 212 | py_reg_value = func; |
| 213 | hook_reg_value = &py_hook_reg_value; |
| 214 | } |
| 215 | |
| 216 | void clr_trap() |
| 217 | { |
| 218 | if (py_trap) |
| 219 | Py_DECREF(py_trap); |
| 220 | py_trap = 0; |
| 221 | hook_trap = 0; |
| 222 | } |
| 223 | void set_trap( PyObject* func ) |
| 224 | { |
| 225 | Py_INCREF(func); |
| 226 | if (py_trap) |
| 227 | Py_DECREF(py_trap); |
| 228 | py_trap = func; |
| 229 | hook_trap = &py_hook_trap; |
| 230 | } |
| 231 | |
| 232 | void clr_mem_access() |
| 233 | { |
| 234 | if (py_mem_access) |
| 235 | Py_DECREF(py_mem_access); |
| 236 | py_mem_access = 0; |
| 237 | hook_mem_access = 0; |
| 238 | } |
| 239 | void set_mem_access( PyObject* func ) |
| 240 | { |
| 241 | Py_INCREF(func); |
| 242 | if (py_mem_access) |
| 243 | Py_DECREF(py_mem_access); |
| 244 | py_mem_access = func; |
| 245 | hook_mem_access = &py_hook_mem_access; |
| 246 | } |
| 247 | |
| 248 | void clr_tlb_update() |
| 249 | { |
| 250 | if (py_tlb_update) |
| 251 | Py_DECREF(py_tlb_update); |
| 252 | py_tlb_update = 0; |
| 253 | hook_tlb_update = 0; |
| 254 | } |
| 255 | void set_tlb_update( PyObject* func ) |
| 256 | { |
| 257 | Py_INCREF(func); |
| 258 | if (py_tlb_update) |
| 259 | Py_DECREF(py_tlb_update); |
| 260 | py_tlb_update = func; |
| 261 | hook_tlb_update = &py_hook_tlb_update; |
| 262 | } |
| 263 | |
| 264 | void clr_end_instr() |
| 265 | { |
| 266 | if (py_end_instr) |
| 267 | Py_DECREF(py_end_instr); |
| 268 | py_end_instr = 0; |
| 269 | hook_end_instr = 0; |
| 270 | } |
| 271 | void set_end_instr( PyObject* func ) |
| 272 | { |
| 273 | Py_INCREF(func); |
| 274 | if (py_end_instr) |
| 275 | Py_DECREF(py_end_instr); |
| 276 | py_end_instr = func; |
| 277 | hook_end_instr = &py_hook_end_instr; |
| 278 | } |
| 279 | |
| 280 | |
| 281 | private: |
| 282 | PyObject* py_exe_instr; |
| 283 | PyObject* py_reg_value; |
| 284 | PyObject* py_trap; |
| 285 | PyObject* py_mem_access; |
| 286 | PyObject* py_tlb_update; |
| 287 | PyObject* py_end_instr; |
| 288 | }; |
| 289 | |
| 290 | #endif |