| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #ifdef WITH_JIT |
| 17 | |
| 18 | /* |
| 19 | * Target independent portion of Android's Jit |
| 20 | */ |
| 21 | |
| 22 | #include "Dalvik.h" |
| 23 | #include "Jit.h" |
| 24 | |
| 25 | |
| Andy McFadden | c6b25c7 | 2010-06-22 11:01:20 -0700 | [diff] [blame] | 26 | #include "libdex/OpCodeNames.h" |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 27 | #include <unistd.h> |
| 28 | #include <pthread.h> |
| 29 | #include <sys/time.h> |
| 30 | #include <signal.h> |
| 31 | #include "compiler/Compiler.h" |
| Bill Buzbee | 6e963e1 | 2009-06-17 16:56:19 -0700 | [diff] [blame] | 32 | #include "compiler/CompilerUtility.h" |
| 33 | #include "compiler/CompilerIR.h" |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 34 | #include <errno.h> |
| 35 | |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 36 | #if defined(WITH_SELF_VERIFICATION) |
| 37 | /* Allocate space for per-thread ShadowSpace data structures */ |
| 38 | void* dvmSelfVerificationShadowSpaceAlloc(Thread* self) |
| 39 | { |
| 40 | self->shadowSpace = (ShadowSpace*) calloc(1, sizeof(ShadowSpace)); |
| 41 | if (self->shadowSpace == NULL) |
| 42 | return NULL; |
| 43 | |
| 44 | self->shadowSpace->registerSpaceSize = REG_SPACE; |
| 45 | self->shadowSpace->registerSpace = |
| 46 | (int*) calloc(self->shadowSpace->registerSpaceSize, sizeof(int)); |
| 47 | |
| 48 | return self->shadowSpace->registerSpace; |
| 49 | } |
| 50 | |
| 51 | /* Free per-thread ShadowSpace data structures */ |
| 52 | void dvmSelfVerificationShadowSpaceFree(Thread* self) |
| 53 | { |
| 54 | free(self->shadowSpace->registerSpace); |
| 55 | free(self->shadowSpace); |
| 56 | } |
| 57 | |
| 58 | /* |
| 59 | * Save out PC, FP, InterpState, and registers to shadow space. |
| 60 | * Return a pointer to the shadow space for JIT to use. |
| 61 | */ |
| 62 | void* dvmSelfVerificationSaveState(const u2* pc, const void* fp, |
| Bill Buzbee | 9a8c75a | 2009-11-08 14:31:20 -0800 | [diff] [blame] | 63 | InterpState* interpState, int targetTrace) |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 64 | { |
| 65 | Thread *self = dvmThreadSelf(); |
| 66 | ShadowSpace *shadowSpace = self->shadowSpace; |
| Ben Cheng | 11d8f14 | 2010-03-24 15:24:19 -0700 | [diff] [blame] | 67 | unsigned preBytes = interpState->method->outsSize*4 + sizeof(StackSaveArea); |
| 68 | unsigned postBytes = interpState->method->registersSize*4; |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 69 | |
| 70 | //LOGD("### selfVerificationSaveState(%d) pc: 0x%x fp: 0x%x", |
| 71 | // self->threadId, (int)pc, (int)fp); |
| 72 | |
| 73 | if (shadowSpace->selfVerificationState != kSVSIdle) { |
| 74 | LOGD("~~~ Save: INCORRECT PREVIOUS STATE(%d): %d", |
| 75 | self->threadId, shadowSpace->selfVerificationState); |
| 76 | LOGD("********** SHADOW STATE DUMP **********"); |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 77 | LOGD("PC: 0x%x FP: 0x%x", (int)pc, (int)fp); |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 78 | } |
| 79 | shadowSpace->selfVerificationState = kSVSStart; |
| 80 | |
| Ben Cheng | d5adae1 | 2010-03-26 17:45:28 -0700 | [diff] [blame] | 81 | if (interpState->entryPoint == kInterpEntryResume) { |
| 82 | interpState->entryPoint = kInterpEntryInstr; |
| 83 | #if 0 |
| 84 | /* Tracking the success rate of resume after single-stepping */ |
| 85 | if (interpState->jitResumeDPC == pc) { |
| 86 | LOGD("SV single step resumed at %p", pc); |
| 87 | } |
| 88 | else { |
| 89 | LOGD("real %p DPC %p NPC %p", pc, interpState->jitResumeDPC, |
| 90 | interpState->jitResumeNPC); |
| 91 | } |
| 92 | #endif |
| 93 | } |
| 94 | |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 95 | // Dynamically grow shadow register space if necessary |
| Ben Cheng | 11d8f14 | 2010-03-24 15:24:19 -0700 | [diff] [blame] | 96 | if (preBytes + postBytes > shadowSpace->registerSpaceSize * sizeof(u4)) { |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 97 | free(shadowSpace->registerSpace); |
| Ben Cheng | 11d8f14 | 2010-03-24 15:24:19 -0700 | [diff] [blame] | 98 | shadowSpace->registerSpaceSize = (preBytes + postBytes) / sizeof(u4); |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 99 | shadowSpace->registerSpace = |
| Ben Cheng | 11d8f14 | 2010-03-24 15:24:19 -0700 | [diff] [blame] | 100 | (int*) calloc(shadowSpace->registerSpaceSize, sizeof(u4)); |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | // Remember original state |
| 104 | shadowSpace->startPC = pc; |
| 105 | shadowSpace->fp = fp; |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 106 | shadowSpace->glue = interpState; |
| 107 | /* |
| 108 | * Store the original method here in case the trace ends with a |
| 109 | * return/invoke, the last method. |
| 110 | */ |
| 111 | shadowSpace->method = interpState->method; |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 112 | shadowSpace->shadowFP = shadowSpace->registerSpace + |
| 113 | shadowSpace->registerSpaceSize - postBytes/4; |
| 114 | |
| 115 | // Create a copy of the InterpState |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 116 | memcpy(&(shadowSpace->interpState), interpState, sizeof(InterpState)); |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 117 | shadowSpace->interpState.fp = shadowSpace->shadowFP; |
| 118 | shadowSpace->interpState.interpStackEnd = (u1*)shadowSpace->registerSpace; |
| 119 | |
| 120 | // Create a copy of the stack |
| 121 | memcpy(((char*)shadowSpace->shadowFP)-preBytes, ((char*)fp)-preBytes, |
| 122 | preBytes+postBytes); |
| 123 | |
| 124 | // Setup the shadowed heap space |
| 125 | shadowSpace->heapSpaceTail = shadowSpace->heapSpace; |
| 126 | |
| 127 | // Reset trace length |
| 128 | shadowSpace->traceLength = 0; |
| 129 | |
| 130 | return shadowSpace; |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Save ending PC, FP and compiled code exit point to shadow space. |
| 135 | * Return a pointer to the shadow space for JIT to restore state. |
| 136 | */ |
| 137 | void* dvmSelfVerificationRestoreState(const u2* pc, const void* fp, |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 138 | SelfVerificationState exitState) |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 139 | { |
| 140 | Thread *self = dvmThreadSelf(); |
| 141 | ShadowSpace *shadowSpace = self->shadowSpace; |
| Ben Cheng | d5adae1 | 2010-03-26 17:45:28 -0700 | [diff] [blame] | 142 | // Official InterpState structure |
| 143 | InterpState *realGlue = shadowSpace->glue; |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 144 | shadowSpace->endPC = pc; |
| 145 | shadowSpace->endShadowFP = fp; |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 146 | shadowSpace->jitExitState = exitState; |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 147 | |
| 148 | //LOGD("### selfVerificationRestoreState(%d) pc: 0x%x fp: 0x%x endPC: 0x%x", |
| 149 | // self->threadId, (int)shadowSpace->startPC, (int)shadowSpace->fp, |
| 150 | // (int)pc); |
| 151 | |
| 152 | if (shadowSpace->selfVerificationState != kSVSStart) { |
| 153 | LOGD("~~~ Restore: INCORRECT PREVIOUS STATE(%d): %d", |
| 154 | self->threadId, shadowSpace->selfVerificationState); |
| 155 | LOGD("********** SHADOW STATE DUMP **********"); |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 156 | LOGD("Dalvik PC: 0x%x endPC: 0x%x", (int)shadowSpace->startPC, |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 157 | (int)shadowSpace->endPC); |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 158 | LOGD("Interp FP: 0x%x", (int)shadowSpace->fp); |
| 159 | LOGD("Shadow FP: 0x%x endFP: 0x%x", (int)shadowSpace->shadowFP, |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 160 | (int)shadowSpace->endShadowFP); |
| 161 | } |
| 162 | |
| Ben Cheng | d5adae1 | 2010-03-26 17:45:28 -0700 | [diff] [blame] | 163 | // Move the resume [ND]PC from the shadow space to the real space so that |
| 164 | // the debug interpreter can return to the translation |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 165 | if (exitState == kSVSSingleStep) { |
| Ben Cheng | d5adae1 | 2010-03-26 17:45:28 -0700 | [diff] [blame] | 166 | realGlue->jitResumeNPC = shadowSpace->interpState.jitResumeNPC; |
| 167 | realGlue->jitResumeDPC = shadowSpace->interpState.jitResumeDPC; |
| 168 | } else { |
| 169 | realGlue->jitResumeNPC = NULL; |
| 170 | realGlue->jitResumeDPC = NULL; |
| 171 | } |
| 172 | |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 173 | // Special case when punting after a single instruction |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 174 | if (exitState == kSVSPunt && pc == shadowSpace->startPC) { |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 175 | shadowSpace->selfVerificationState = kSVSIdle; |
| Ben Cheng | 60c6dbf | 2010-08-26 12:28:56 -0700 | [diff] [blame] | 176 | } else if (exitState == kSVSBackwardBranch && pc != shadowSpace->startPC) { |
| 177 | /* |
| 178 | * Consider a loop which contains the following instructions: |
| 179 | * 1: .. |
| 180 | * 2: .. |
| 181 | * 3: ..(single-stepped) |
| 182 | * 4: .. |
| 183 | * 5: Goto 1 |
| 184 | * |
| 185 | * The state comparisons are conducted in two batches: 1,2 as the |
| 186 | * first batch and 4,5 as the second batch. If the execution in the |
| 187 | * JIT'ed code is resumed from 4, the start PC for self-verification |
| 188 | * will be set to 4. The exit point for 5 is a backward branch and |
| 189 | * will suggest the end PC to be 1, and will do the state comparison |
| 190 | * when 1 is hit for the second time in the interpreter. It is incorrect |
| 191 | * in this case as the work for 1,2 from the current iteration have been |
| 192 | * committed and the state comparison has been conducted. |
| 193 | * |
| 194 | * So we alter the state from BackwardBranch to Normal and state |
| 195 | * comparison will be issued immediate following the goto. |
| 196 | */ |
| 197 | shadowSpace->selfVerificationState = kSVSNormal; |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 198 | } else { |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 199 | shadowSpace->selfVerificationState = exitState; |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | return shadowSpace; |
| 203 | } |
| 204 | |
| 205 | /* Print contents of virtual registers */ |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 206 | static void selfVerificationPrintRegisters(int* addr, int* addrRef, |
| 207 | int numWords) |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 208 | { |
| 209 | int i; |
| 210 | for (i = 0; i < numWords; i++) { |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 211 | LOGD("(v%d) 0x%8x%s", i, addr[i], addr[i] != addrRef[i] ? " X" : ""); |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
| 215 | /* Print values maintained in shadowSpace */ |
| 216 | static void selfVerificationDumpState(const u2* pc, Thread* self) |
| 217 | { |
| 218 | ShadowSpace* shadowSpace = self->shadowSpace; |
| 219 | StackSaveArea* stackSave = SAVEAREA_FROM_FP(self->curFrame); |
| 220 | int frameBytes = (int) shadowSpace->registerSpace + |
| 221 | shadowSpace->registerSpaceSize*4 - |
| 222 | (int) shadowSpace->shadowFP; |
| 223 | int localRegs = 0; |
| 224 | int frameBytes2 = 0; |
| 225 | if (self->curFrame < shadowSpace->fp) { |
| 226 | localRegs = (stackSave->method->registersSize - |
| 227 | stackSave->method->insSize)*4; |
| 228 | frameBytes2 = (int) shadowSpace->fp - (int) self->curFrame - localRegs; |
| 229 | } |
| 230 | LOGD("********** SHADOW STATE DUMP **********"); |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 231 | LOGD("CurrentPC: 0x%x, Offset: 0x%04x", (int)pc, |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 232 | (int)(pc - stackSave->method->insns)); |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 233 | LOGD("Class: %s", shadowSpace->method->clazz->descriptor); |
| 234 | LOGD("Method: %s", shadowSpace->method->name); |
| 235 | LOGD("Dalvik PC: 0x%x endPC: 0x%x", (int)shadowSpace->startPC, |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 236 | (int)shadowSpace->endPC); |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 237 | LOGD("Interp FP: 0x%x endFP: 0x%x", (int)shadowSpace->fp, |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 238 | (int)self->curFrame); |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 239 | LOGD("Shadow FP: 0x%x endFP: 0x%x", (int)shadowSpace->shadowFP, |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 240 | (int)shadowSpace->endShadowFP); |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 241 | LOGD("Frame1 Bytes: %d Frame2 Local: %d Bytes: %d", frameBytes, |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 242 | localRegs, frameBytes2); |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 243 | LOGD("Trace length: %d State: %d", shadowSpace->traceLength, |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 244 | shadowSpace->selfVerificationState); |
| 245 | } |
| 246 | |
| 247 | /* Print decoded instructions in the current trace */ |
| 248 | static void selfVerificationDumpTrace(const u2* pc, Thread* self) |
| 249 | { |
| 250 | ShadowSpace* shadowSpace = self->shadowSpace; |
| 251 | StackSaveArea* stackSave = SAVEAREA_FROM_FP(self->curFrame); |
| Ben Cheng | bcdc1de | 2009-08-21 16:18:46 -0700 | [diff] [blame] | 252 | int i, addr, offset; |
| 253 | DecodedInstruction *decInsn; |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 254 | |
| 255 | LOGD("********** SHADOW TRACE DUMP **********"); |
| 256 | for (i = 0; i < shadowSpace->traceLength; i++) { |
| Ben Cheng | bcdc1de | 2009-08-21 16:18:46 -0700 | [diff] [blame] | 257 | addr = shadowSpace->trace[i].addr; |
| 258 | offset = (int)((u2*)addr - stackSave->method->insns); |
| 259 | decInsn = &(shadowSpace->trace[i].decInsn); |
| 260 | /* Not properly decoding instruction, some registers may be garbage */ |
| Andy McFadden | c6b25c7 | 2010-06-22 11:01:20 -0700 | [diff] [blame] | 261 | LOGD("0x%x: (0x%04x) %s", |
| 262 | addr, offset, dexGetOpcodeName(decInsn->opCode)); |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
| Ben Cheng | bcdc1de | 2009-08-21 16:18:46 -0700 | [diff] [blame] | 266 | /* Code is forced into this spin loop when a divergence is detected */ |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 267 | static void selfVerificationSpinLoop(ShadowSpace *shadowSpace) |
| Ben Cheng | bcdc1de | 2009-08-21 16:18:46 -0700 | [diff] [blame] | 268 | { |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 269 | const u2 *startPC = shadowSpace->startPC; |
| Ben Cheng | 88a0f97 | 2010-02-24 15:00:40 -0800 | [diff] [blame] | 270 | JitTraceDescription* desc = dvmCopyTraceDescriptor(startPC, NULL); |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 271 | if (desc) { |
| 272 | dvmCompilerWorkEnqueue(startPC, kWorkOrderTraceDebug, desc); |
| Ben Cheng | 1357e94 | 2010-02-10 17:21:39 -0800 | [diff] [blame] | 273 | /* |
| 274 | * This function effectively terminates the VM right here, so not |
| 275 | * freeing the desc pointer when the enqueuing fails is acceptable. |
| 276 | */ |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 277 | } |
| Ben Cheng | bcdc1de | 2009-08-21 16:18:46 -0700 | [diff] [blame] | 278 | gDvmJit.selfVerificationSpin = true; |
| 279 | while(gDvmJit.selfVerificationSpin) sleep(10); |
| 280 | } |
| 281 | |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 282 | /* Manage self verification while in the debug interpreter */ |
| Ben Cheng | d5adae1 | 2010-03-26 17:45:28 -0700 | [diff] [blame] | 283 | static bool selfVerificationDebugInterp(const u2* pc, Thread* self, |
| 284 | InterpState *interpState) |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 285 | { |
| 286 | ShadowSpace *shadowSpace = self->shadowSpace; |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 287 | SelfVerificationState state = shadowSpace->selfVerificationState; |
| Ben Cheng | bcdc1de | 2009-08-21 16:18:46 -0700 | [diff] [blame] | 288 | |
| 289 | DecodedInstruction decInsn; |
| 290 | dexDecodeInstruction(gDvm.instrFormat, pc, &decInsn); |
| 291 | |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 292 | //LOGD("### DbgIntp(%d): PC: 0x%x endPC: 0x%x state: %d len: %d %s", |
| 293 | // self->threadId, (int)pc, (int)shadowSpace->endPC, state, |
| Andy McFadden | c6b25c7 | 2010-06-22 11:01:20 -0700 | [diff] [blame] | 294 | // shadowSpace->traceLength, dexGetOpcodeName(decInsn.opCode)); |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 295 | |
| 296 | if (state == kSVSIdle || state == kSVSStart) { |
| 297 | LOGD("~~~ DbgIntrp: INCORRECT PREVIOUS STATE(%d): %d", |
| 298 | self->threadId, state); |
| 299 | selfVerificationDumpState(pc, self); |
| 300 | selfVerificationDumpTrace(pc, self); |
| 301 | } |
| 302 | |
| Ben Cheng | d5adae1 | 2010-03-26 17:45:28 -0700 | [diff] [blame] | 303 | /* |
| 304 | * Skip endPC once when trace has a backward branch. If the SV state is |
| 305 | * single step, keep it that way. |
| 306 | */ |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 307 | if ((state == kSVSBackwardBranch && pc == shadowSpace->endPC) || |
| Ben Cheng | d5adae1 | 2010-03-26 17:45:28 -0700 | [diff] [blame] | 308 | (state != kSVSBackwardBranch && state != kSVSSingleStep)) { |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 309 | shadowSpace->selfVerificationState = kSVSDebugInterp; |
| 310 | } |
| 311 | |
| 312 | /* Check that the current pc is the end of the trace */ |
| Ben Cheng | d5adae1 | 2010-03-26 17:45:28 -0700 | [diff] [blame] | 313 | if ((state == kSVSDebugInterp || state == kSVSSingleStep) && |
| 314 | pc == shadowSpace->endPC) { |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 315 | |
| 316 | shadowSpace->selfVerificationState = kSVSIdle; |
| 317 | |
| 318 | /* Check register space */ |
| 319 | int frameBytes = (int) shadowSpace->registerSpace + |
| 320 | shadowSpace->registerSpaceSize*4 - |
| 321 | (int) shadowSpace->shadowFP; |
| 322 | if (memcmp(shadowSpace->fp, shadowSpace->shadowFP, frameBytes)) { |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 323 | LOGD("~~~ DbgIntp(%d): REGISTERS DIVERGENCE!", self->threadId); |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 324 | selfVerificationDumpState(pc, self); |
| 325 | selfVerificationDumpTrace(pc, self); |
| 326 | LOGD("*** Interp Registers: addr: 0x%x bytes: %d", |
| 327 | (int)shadowSpace->fp, frameBytes); |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 328 | selfVerificationPrintRegisters((int*)shadowSpace->fp, |
| 329 | (int*)shadowSpace->shadowFP, |
| 330 | frameBytes/4); |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 331 | LOGD("*** Shadow Registers: addr: 0x%x bytes: %d", |
| 332 | (int)shadowSpace->shadowFP, frameBytes); |
| 333 | selfVerificationPrintRegisters((int*)shadowSpace->shadowFP, |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 334 | (int*)shadowSpace->fp, |
| 335 | frameBytes/4); |
| 336 | selfVerificationSpinLoop(shadowSpace); |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 337 | } |
| 338 | /* Check new frame if it exists (invokes only) */ |
| 339 | if (self->curFrame < shadowSpace->fp) { |
| 340 | StackSaveArea* stackSave = SAVEAREA_FROM_FP(self->curFrame); |
| 341 | int localRegs = (stackSave->method->registersSize - |
| 342 | stackSave->method->insSize)*4; |
| 343 | int frameBytes2 = (int) shadowSpace->fp - |
| 344 | (int) self->curFrame - localRegs; |
| 345 | if (memcmp(((char*)self->curFrame)+localRegs, |
| 346 | ((char*)shadowSpace->endShadowFP)+localRegs, frameBytes2)) { |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 347 | LOGD("~~~ DbgIntp(%d): REGISTERS (FRAME2) DIVERGENCE!", |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 348 | self->threadId); |
| 349 | selfVerificationDumpState(pc, self); |
| 350 | selfVerificationDumpTrace(pc, self); |
| 351 | LOGD("*** Interp Registers: addr: 0x%x l: %d bytes: %d", |
| 352 | (int)self->curFrame, localRegs, frameBytes2); |
| 353 | selfVerificationPrintRegisters((int*)self->curFrame, |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 354 | (int*)shadowSpace->endShadowFP, |
| 355 | (frameBytes2+localRegs)/4); |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 356 | LOGD("*** Shadow Registers: addr: 0x%x l: %d bytes: %d", |
| 357 | (int)shadowSpace->endShadowFP, localRegs, frameBytes2); |
| 358 | selfVerificationPrintRegisters((int*)shadowSpace->endShadowFP, |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 359 | (int*)self->curFrame, |
| 360 | (frameBytes2+localRegs)/4); |
| 361 | selfVerificationSpinLoop(shadowSpace); |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
| 365 | /* Check memory space */ |
| Ben Cheng | bcdc1de | 2009-08-21 16:18:46 -0700 | [diff] [blame] | 366 | bool memDiff = false; |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 367 | ShadowHeap* heapSpacePtr; |
| 368 | for (heapSpacePtr = shadowSpace->heapSpace; |
| 369 | heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) { |
| Ben Cheng | bcdc1de | 2009-08-21 16:18:46 -0700 | [diff] [blame] | 370 | int memData = *((unsigned int*) heapSpacePtr->addr); |
| 371 | if (heapSpacePtr->data != memData) { |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 372 | LOGD("~~~ DbgIntp(%d): MEMORY DIVERGENCE!", self->threadId); |
| 373 | LOGD("Addr: 0x%x Intrp Data: 0x%x Jit Data: 0x%x", |
| Ben Cheng | bcdc1de | 2009-08-21 16:18:46 -0700 | [diff] [blame] | 374 | heapSpacePtr->addr, memData, heapSpacePtr->data); |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 375 | selfVerificationDumpState(pc, self); |
| 376 | selfVerificationDumpTrace(pc, self); |
| Ben Cheng | bcdc1de | 2009-08-21 16:18:46 -0700 | [diff] [blame] | 377 | memDiff = true; |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 378 | } |
| 379 | } |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 380 | if (memDiff) selfVerificationSpinLoop(shadowSpace); |
| Ben Cheng | d5adae1 | 2010-03-26 17:45:28 -0700 | [diff] [blame] | 381 | |
| 382 | /* |
| 383 | * Switch to JIT single step mode to stay in the debug interpreter for |
| 384 | * one more instruction |
| 385 | */ |
| 386 | if (state == kSVSSingleStep) { |
| 387 | interpState->jitState = kJitSingleStepEnd; |
| 388 | } |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 389 | return true; |
| 390 | |
| 391 | /* If end not been reached, make sure max length not exceeded */ |
| 392 | } else if (shadowSpace->traceLength >= JIT_MAX_TRACE_LEN) { |
| 393 | LOGD("~~~ DbgIntp(%d): CONTROL DIVERGENCE!", self->threadId); |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 394 | LOGD("startPC: 0x%x endPC: 0x%x currPC: 0x%x", |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 395 | (int)shadowSpace->startPC, (int)shadowSpace->endPC, (int)pc); |
| 396 | selfVerificationDumpState(pc, self); |
| 397 | selfVerificationDumpTrace(pc, self); |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 398 | selfVerificationSpinLoop(shadowSpace); |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 399 | |
| 400 | return true; |
| 401 | } |
| Ben Cheng | bcdc1de | 2009-08-21 16:18:46 -0700 | [diff] [blame] | 402 | /* Log the instruction address and decoded instruction for debug */ |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 403 | shadowSpace->trace[shadowSpace->traceLength].addr = (int)pc; |
| Ben Cheng | bcdc1de | 2009-08-21 16:18:46 -0700 | [diff] [blame] | 404 | shadowSpace->trace[shadowSpace->traceLength].decInsn = decInsn; |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 405 | shadowSpace->traceLength++; |
| 406 | |
| 407 | return false; |
| 408 | } |
| 409 | #endif |
| 410 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 411 | /* |
| 412 | * If one of our fixed tables or the translation buffer fills up, |
| 413 | * call this routine to avoid wasting cycles on future translation requests. |
| 414 | */ |
| 415 | void dvmJitStopTranslationRequests() |
| 416 | { |
| 417 | /* |
| 418 | * Note 1: This won't necessarily stop all translation requests, and |
| 419 | * operates on a delayed mechanism. Running threads look to the copy |
| 420 | * of this value in their private InterpState structures and won't see |
| 421 | * this change until it is refreshed (which happens on interpreter |
| 422 | * entry). |
| 423 | * Note 2: This is a one-shot memory leak on this table. Because this is a |
| 424 | * permanent off switch for Jit profiling, it is a one-time leak of 1K |
| 425 | * bytes, and no further attempt will be made to re-allocate it. Can't |
| 426 | * free it because some thread may be holding a reference. |
| 427 | */ |
| Bill Buzbee | b1d8044 | 2009-12-17 14:55:21 -0800 | [diff] [blame] | 428 | gDvmJit.pProfTable = NULL; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 429 | } |
| 430 | |
| Ben Cheng | 978738d | 2010-05-13 13:45:57 -0700 | [diff] [blame] | 431 | #if defined(WITH_JIT_TUNING) |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 432 | /* Convenience function to increment counter from assembly code */ |
| Ben Cheng | 6c10a97 | 2009-10-29 14:39:18 -0700 | [diff] [blame] | 433 | void dvmBumpNoChain(int from) |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 434 | { |
| Ben Cheng | 6c10a97 | 2009-10-29 14:39:18 -0700 | [diff] [blame] | 435 | gDvmJit.noChainExit[from]++; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | /* Convenience function to increment counter from assembly code */ |
| 439 | void dvmBumpNormal() |
| 440 | { |
| Ben Cheng | 6c10a97 | 2009-10-29 14:39:18 -0700 | [diff] [blame] | 441 | gDvmJit.normalExit++; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | /* Convenience function to increment counter from assembly code */ |
| 445 | void dvmBumpPunt(int from) |
| 446 | { |
| Ben Cheng | 6c10a97 | 2009-10-29 14:39:18 -0700 | [diff] [blame] | 447 | gDvmJit.puntExit++; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 448 | } |
| 449 | #endif |
| 450 | |
| 451 | /* Dumps debugging & tuning stats to the log */ |
| 452 | void dvmJitStats() |
| 453 | { |
| 454 | int i; |
| 455 | int hit; |
| 456 | int not_hit; |
| 457 | int chains; |
| Bill Buzbee | 9a8c75a | 2009-11-08 14:31:20 -0800 | [diff] [blame] | 458 | int stubs; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 459 | if (gDvmJit.pJitEntryTable) { |
| Bill Buzbee | 9a8c75a | 2009-11-08 14:31:20 -0800 | [diff] [blame] | 460 | for (i=0, stubs=chains=hit=not_hit=0; |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 461 | i < (int) gDvmJit.jitTableSize; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 462 | i++) { |
| Bill Buzbee | 9a8c75a | 2009-11-08 14:31:20 -0800 | [diff] [blame] | 463 | if (gDvmJit.pJitEntryTable[i].dPC != 0) { |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 464 | hit++; |
| Bill Buzbee | 9a8c75a | 2009-11-08 14:31:20 -0800 | [diff] [blame] | 465 | if (gDvmJit.pJitEntryTable[i].codeAddress == |
| Bill Buzbee | bd04724 | 2010-05-13 13:02:53 -0700 | [diff] [blame] | 466 | dvmCompilerGetInterpretTemplate()) |
| Bill Buzbee | 9a8c75a | 2009-11-08 14:31:20 -0800 | [diff] [blame] | 467 | stubs++; |
| 468 | } else |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 469 | not_hit++; |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 470 | if (gDvmJit.pJitEntryTable[i].u.info.chain != gDvmJit.jitTableSize) |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 471 | chains++; |
| 472 | } |
| Ben Cheng | 72621c9 | 2010-03-10 13:12:55 -0800 | [diff] [blame] | 473 | LOGD("JIT: table size is %d, entries used is %d", |
| Ben Cheng | 86717f7 | 2010-03-05 15:27:21 -0800 | [diff] [blame] | 474 | gDvmJit.jitTableSize, gDvmJit.jitTableEntriesUsed); |
| Ben Cheng | 72621c9 | 2010-03-10 13:12:55 -0800 | [diff] [blame] | 475 | LOGD("JIT: %d traces, %d slots, %d chains, %d thresh, %s", |
| 476 | hit, not_hit + hit, chains, gDvmJit.threshold, |
| 477 | gDvmJit.blockingMode ? "Blocking" : "Non-blocking"); |
| Ben Cheng | 86717f7 | 2010-03-05 15:27:21 -0800 | [diff] [blame] | 478 | |
| Ben Cheng | 978738d | 2010-05-13 13:45:57 -0700 | [diff] [blame] | 479 | #if defined(WITH_JIT_TUNING) |
| 480 | LOGD("JIT: Code cache patches: %d", gDvmJit.codeCachePatches); |
| 481 | |
| Ben Cheng | 72621c9 | 2010-03-10 13:12:55 -0800 | [diff] [blame] | 482 | LOGD("JIT: Lookups: %d hits, %d misses; %d normal, %d punt", |
| 483 | gDvmJit.addrLookupsFound, gDvmJit.addrLookupsNotFound, |
| 484 | gDvmJit.normalExit, gDvmJit.puntExit); |
| Ben Cheng | 452efba | 2010-04-30 15:14:00 -0700 | [diff] [blame] | 485 | |
| Ben Cheng | 978738d | 2010-05-13 13:45:57 -0700 | [diff] [blame] | 486 | LOGD("JIT: ICHits: %d", gDvmICHitCount); |
| 487 | |
| Ben Cheng | 72621c9 | 2010-03-10 13:12:55 -0800 | [diff] [blame] | 488 | LOGD("JIT: noChainExit: %d IC miss, %d interp callsite, " |
| 489 | "%d switch overflow", |
| 490 | gDvmJit.noChainExit[kInlineCacheMiss], |
| 491 | gDvmJit.noChainExit[kCallsiteInterpreted], |
| 492 | gDvmJit.noChainExit[kSwitchOverflow]); |
| Ben Cheng | 86717f7 | 2010-03-05 15:27:21 -0800 | [diff] [blame] | 493 | |
| Ben Cheng | b88ec3c | 2010-05-17 12:50:33 -0700 | [diff] [blame] | 494 | LOGD("JIT: ICPatch: %d init, %d rejected, %d lock-free, %d queued, " |
| 495 | "%d dropped", |
| 496 | gDvmJit.icPatchInit, gDvmJit.icPatchRejected, |
| 497 | gDvmJit.icPatchLockFree, gDvmJit.icPatchQueued, |
| Ben Cheng | 452efba | 2010-04-30 15:14:00 -0700 | [diff] [blame] | 498 | gDvmJit.icPatchDropped); |
| 499 | |
| Ben Cheng | 86717f7 | 2010-03-05 15:27:21 -0800 | [diff] [blame] | 500 | LOGD("JIT: Invoke: %d mono, %d poly, %d native, %d return", |
| 501 | gDvmJit.invokeMonomorphic, gDvmJit.invokePolymorphic, |
| 502 | gDvmJit.invokeNative, gDvmJit.returnOp); |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 503 | LOGD("JIT: Inline: %d mgetter, %d msetter, %d pgetter, %d psetter", |
| 504 | gDvmJit.invokeMonoGetterInlined, gDvmJit.invokeMonoSetterInlined, |
| 505 | gDvmJit.invokePolyGetterInlined, gDvmJit.invokePolySetterInlined); |
| Ben Cheng | 86717f7 | 2010-03-05 15:27:21 -0800 | [diff] [blame] | 506 | LOGD("JIT: Total compilation time: %llu ms", gDvmJit.jitTime / 1000); |
| 507 | LOGD("JIT: Avg unit compilation time: %llu us", |
| 508 | gDvmJit.jitTime / gDvmJit.numCompilations); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 509 | #endif |
| Ben Cheng | 86717f7 | 2010-03-05 15:27:21 -0800 | [diff] [blame] | 510 | |
| Bill Buzbee | 9a8c75a | 2009-11-08 14:31:20 -0800 | [diff] [blame] | 511 | LOGD("JIT: %d Translation chains, %d interp stubs", |
| 512 | gDvmJit.translationChains, stubs); |
| Ben Cheng | e80cd94 | 2009-07-17 15:54:23 -0700 | [diff] [blame] | 513 | if (gDvmJit.profile) { |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 514 | dvmCompilerSortAndPrintTraceProfiles(); |
| Bill Buzbee | 6e963e1 | 2009-06-17 16:56:19 -0700 | [diff] [blame] | 515 | } |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 519 | |
| Andy McFadden | 953a0ed | 2010-09-17 15:48:38 -0700 | [diff] [blame^] | 520 | static void setTraceConstruction(JitEntry *slot, bool value) |
| Bill Buzbee | d726991 | 2009-11-10 14:31:32 -0800 | [diff] [blame] | 521 | { |
| 522 | |
| 523 | JitEntryInfoUnion oldValue; |
| 524 | JitEntryInfoUnion newValue; |
| 525 | do { |
| 526 | oldValue = slot->u; |
| 527 | newValue = oldValue; |
| 528 | newValue.info.traceConstruction = value; |
| Andy McFadden | 6e10b9a | 2010-06-14 15:24:39 -0700 | [diff] [blame] | 529 | } while (android_atomic_release_cas(oldValue.infoWord, newValue.infoWord, |
| 530 | &slot->u.infoWord) != 0); |
| Bill Buzbee | d726991 | 2009-11-10 14:31:32 -0800 | [diff] [blame] | 531 | } |
| 532 | |
| Andy McFadden | 953a0ed | 2010-09-17 15:48:38 -0700 | [diff] [blame^] | 533 | static void resetTracehead(InterpState* interpState, JitEntry *slot) |
| Bill Buzbee | d726991 | 2009-11-10 14:31:32 -0800 | [diff] [blame] | 534 | { |
| Bill Buzbee | bd04724 | 2010-05-13 13:02:53 -0700 | [diff] [blame] | 535 | slot->codeAddress = dvmCompilerGetInterpretTemplate(); |
| Bill Buzbee | d726991 | 2009-11-10 14:31:32 -0800 | [diff] [blame] | 536 | setTraceConstruction(slot, false); |
| 537 | } |
| 538 | |
| 539 | /* Clean up any pending trace builds */ |
| 540 | void dvmJitAbortTraceSelect(InterpState* interpState) |
| 541 | { |
| 542 | if (interpState->jitState == kJitTSelect) |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 543 | interpState->jitState = kJitDone; |
| Bill Buzbee | d726991 | 2009-11-10 14:31:32 -0800 | [diff] [blame] | 544 | } |
| 545 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 546 | /* |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 547 | * Find an entry in the JitTable, creating if necessary. |
| 548 | * Returns null if table is full. |
| 549 | */ |
| 550 | static JitEntry *lookupAndAdd(const u2* dPC, bool callerLocked) |
| 551 | { |
| 552 | u4 chainEndMarker = gDvmJit.jitTableSize; |
| 553 | u4 idx = dvmJitHash(dPC); |
| 554 | |
| 555 | /* Walk the bucket chain to find an exact match for our PC */ |
| 556 | while ((gDvmJit.pJitEntryTable[idx].u.info.chain != chainEndMarker) && |
| 557 | (gDvmJit.pJitEntryTable[idx].dPC != dPC)) { |
| 558 | idx = gDvmJit.pJitEntryTable[idx].u.info.chain; |
| 559 | } |
| 560 | |
| 561 | if (gDvmJit.pJitEntryTable[idx].dPC != dPC) { |
| 562 | /* |
| 563 | * No match. Aquire jitTableLock and find the last |
| 564 | * slot in the chain. Possibly continue the chain walk in case |
| 565 | * some other thread allocated the slot we were looking |
| 566 | * at previuosly (perhaps even the dPC we're trying to enter). |
| 567 | */ |
| 568 | if (!callerLocked) |
| 569 | dvmLockMutex(&gDvmJit.tableLock); |
| 570 | /* |
| 571 | * At this point, if .dPC is NULL, then the slot we're |
| 572 | * looking at is the target slot from the primary hash |
| 573 | * (the simple, and common case). Otherwise we're going |
| 574 | * to have to find a free slot and chain it. |
| 575 | */ |
| Andy McFadden | 6e10b9a | 2010-06-14 15:24:39 -0700 | [diff] [blame] | 576 | ANDROID_MEMBAR_FULL(); /* Make sure we reload [].dPC after lock */ |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 577 | if (gDvmJit.pJitEntryTable[idx].dPC != NULL) { |
| 578 | u4 prev; |
| 579 | while (gDvmJit.pJitEntryTable[idx].u.info.chain != chainEndMarker) { |
| 580 | if (gDvmJit.pJitEntryTable[idx].dPC == dPC) { |
| 581 | /* Another thread got there first for this dPC */ |
| 582 | if (!callerLocked) |
| 583 | dvmUnlockMutex(&gDvmJit.tableLock); |
| 584 | return &gDvmJit.pJitEntryTable[idx]; |
| 585 | } |
| 586 | idx = gDvmJit.pJitEntryTable[idx].u.info.chain; |
| 587 | } |
| 588 | /* Here, idx should be pointing to the last cell of an |
| 589 | * active chain whose last member contains a valid dPC */ |
| 590 | assert(gDvmJit.pJitEntryTable[idx].dPC != NULL); |
| 591 | /* Linear walk to find a free cell and add it to the end */ |
| 592 | prev = idx; |
| 593 | while (true) { |
| 594 | idx++; |
| 595 | if (idx == chainEndMarker) |
| 596 | idx = 0; /* Wraparound */ |
| 597 | if ((gDvmJit.pJitEntryTable[idx].dPC == NULL) || |
| 598 | (idx == prev)) |
| 599 | break; |
| 600 | } |
| 601 | if (idx != prev) { |
| 602 | JitEntryInfoUnion oldValue; |
| 603 | JitEntryInfoUnion newValue; |
| 604 | /* |
| 605 | * Although we hold the lock so that noone else will |
| 606 | * be trying to update a chain field, the other fields |
| 607 | * packed into the word may be in use by other threads. |
| 608 | */ |
| 609 | do { |
| 610 | oldValue = gDvmJit.pJitEntryTable[prev].u; |
| 611 | newValue = oldValue; |
| 612 | newValue.info.chain = idx; |
| Andy McFadden | 6e10b9a | 2010-06-14 15:24:39 -0700 | [diff] [blame] | 613 | } while (android_atomic_release_cas(oldValue.infoWord, |
| 614 | newValue.infoWord, |
| 615 | &gDvmJit.pJitEntryTable[prev].u.infoWord) != 0); |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 616 | } |
| 617 | } |
| 618 | if (gDvmJit.pJitEntryTable[idx].dPC == NULL) { |
| 619 | /* |
| 620 | * Initialize codeAddress and allocate the slot. Must |
| 621 | * happen in this order (since dPC is set, the entry is live. |
| 622 | */ |
| 623 | gDvmJit.pJitEntryTable[idx].dPC = dPC; |
| 624 | gDvmJit.jitTableEntriesUsed++; |
| 625 | } else { |
| 626 | /* Table is full */ |
| 627 | idx = chainEndMarker; |
| 628 | } |
| 629 | if (!callerLocked) |
| 630 | dvmUnlockMutex(&gDvmJit.tableLock); |
| 631 | } |
| 632 | return (idx == chainEndMarker) ? NULL : &gDvmJit.pJitEntryTable[idx]; |
| 633 | } |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 634 | |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 635 | /* |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 636 | * Append the class ptr of "this" and the current method ptr to the current |
| 637 | * trace. That is, the trace runs will contain the following components: |
| 638 | * + trace run that ends with an invoke (existing entry) |
| 639 | * + thisClass (new) |
| 640 | * + calleeMethod (new) |
| 641 | */ |
| 642 | static void insertClassMethodInfo(InterpState* interpState, |
| 643 | const ClassObject* thisClass, |
| 644 | const Method* calleeMethod, |
| 645 | const DecodedInstruction* insn) |
| 646 | { |
| 647 | int currTraceRun = ++interpState->currTraceRun; |
| 648 | interpState->trace[currTraceRun].meta = (void *) thisClass; |
| 649 | currTraceRun = ++interpState->currTraceRun; |
| 650 | interpState->trace[currTraceRun].meta = (void *) calleeMethod; |
| 651 | } |
| 652 | |
| 653 | /* |
| Ben Cheng | d44faf5 | 2010-06-02 15:33:51 -0700 | [diff] [blame] | 654 | * Check if the next instruction following the invoke is a move-result and if |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 655 | * so add it to the trace. That is, this will add the trace run that includes |
| 656 | * the move-result to the trace list. |
| 657 | * |
| 658 | * + trace run that ends with an invoke (existing entry) |
| 659 | * + thisClass (existing entry) |
| 660 | * + calleeMethod (existing entry) |
| 661 | * + move result (new) |
| Ben Cheng | d44faf5 | 2010-06-02 15:33:51 -0700 | [diff] [blame] | 662 | * |
| 663 | * lastPC, len, offset are all from the preceding invoke instruction |
| 664 | */ |
| 665 | static void insertMoveResult(const u2 *lastPC, int len, int offset, |
| 666 | InterpState *interpState) |
| 667 | { |
| 668 | DecodedInstruction nextDecInsn; |
| 669 | const u2 *moveResultPC = lastPC + len; |
| 670 | |
| 671 | dexDecodeInstruction(gDvm.instrFormat, moveResultPC, &nextDecInsn); |
| 672 | if ((nextDecInsn.opCode != OP_MOVE_RESULT) && |
| 673 | (nextDecInsn.opCode != OP_MOVE_RESULT_WIDE) && |
| 674 | (nextDecInsn.opCode != OP_MOVE_RESULT_OBJECT)) |
| 675 | return; |
| 676 | |
| 677 | /* We need to start a new trace run */ |
| 678 | int currTraceRun = ++interpState->currTraceRun; |
| 679 | interpState->currRunHead = moveResultPC; |
| 680 | interpState->trace[currTraceRun].frag.startOffset = offset + len; |
| 681 | interpState->trace[currTraceRun].frag.numInsts = 1; |
| 682 | interpState->trace[currTraceRun].frag.runEnd = false; |
| 683 | interpState->trace[currTraceRun].frag.hint = kJitHintNone; |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 684 | interpState->trace[currTraceRun].frag.isCode = true; |
| Ben Cheng | d44faf5 | 2010-06-02 15:33:51 -0700 | [diff] [blame] | 685 | interpState->totalTraceLen++; |
| 686 | |
| 687 | interpState->currRunLen = dexGetInstrOrTableWidthAbs(gDvm.instrWidth, |
| 688 | moveResultPC); |
| 689 | } |
| 690 | |
| 691 | /* |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 692 | * Adds to the current trace request one instruction at a time, just |
| 693 | * before that instruction is interpreted. This is the primary trace |
| 694 | * selection function. NOTE: return instruction are handled a little |
| 695 | * differently. In general, instructions are "proposed" to be added |
| 696 | * to the current trace prior to interpretation. If the interpreter |
| 697 | * then successfully completes the instruction, is will be considered |
| 698 | * part of the request. This allows us to examine machine state prior |
| 699 | * to interpretation, and also abort the trace request if the instruction |
| 700 | * throws or does something unexpected. However, return instructions |
| 701 | * will cause an immediate end to the translation request - which will |
| 702 | * be passed to the compiler before the return completes. This is done |
| 703 | * in response to special handling of returns by the interpreter (and |
| 704 | * because returns cannot throw in a way that causes problems for the |
| 705 | * translated code. |
| 706 | */ |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 707 | int dvmCheckJit(const u2* pc, Thread* self, InterpState* interpState, |
| 708 | const ClassObject* thisClass, const Method* curMethod) |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 709 | { |
| Carl Shapiro | e3c01da | 2010-05-20 22:54:18 -0700 | [diff] [blame] | 710 | int flags, len; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 711 | int switchInterp = false; |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 712 | bool debugOrProfile = dvmDebuggerOrProfilerActive(); |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 713 | /* Stay in the dbg interpreter for the next instruction */ |
| 714 | bool stayOneMoreInst = false; |
| Bill Buzbee | d726991 | 2009-11-10 14:31:32 -0800 | [diff] [blame] | 715 | |
| Ben Cheng | 1c52e6d | 2010-07-02 13:00:39 -0700 | [diff] [blame] | 716 | /* |
| 717 | * Bug 2710533 - dalvik crash when disconnecting debugger |
| 718 | * |
| 719 | * Reset the entry point to the default value. If needed it will be set to a |
| 720 | * specific value in the corresponding case statement (eg kJitSingleStepEnd) |
| 721 | */ |
| 722 | interpState->entryPoint = kInterpEntryInstr; |
| 723 | |
| Ben Cheng | 79d173c | 2009-09-29 16:12:51 -0700 | [diff] [blame] | 724 | /* Prepare to handle last PC and stage the current PC */ |
| 725 | const u2 *lastPC = interpState->lastPC; |
| 726 | interpState->lastPC = pc; |
| 727 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 728 | switch (interpState->jitState) { |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 729 | int offset; |
| 730 | DecodedInstruction decInsn; |
| 731 | case kJitTSelect: |
| Ben Cheng | dc84bb2 | 2009-10-02 12:58:52 -0700 | [diff] [blame] | 732 | /* First instruction - just remember the PC and exit */ |
| 733 | if (lastPC == NULL) break; |
| Ben Cheng | 79d173c | 2009-09-29 16:12:51 -0700 | [diff] [blame] | 734 | /* Grow the trace around the last PC if jitState is kJitTSelect */ |
| 735 | dexDecodeInstruction(gDvm.instrFormat, lastPC, &decInsn); |
| Ben Cheng | 6c10a97 | 2009-10-29 14:39:18 -0700 | [diff] [blame] | 736 | |
| 737 | /* |
| 738 | * Treat {PACKED,SPARSE}_SWITCH as trace-ending instructions due |
| 739 | * to the amount of space it takes to generate the chaining |
| 740 | * cells. |
| 741 | */ |
| 742 | if (interpState->totalTraceLen != 0 && |
| 743 | (decInsn.opCode == OP_PACKED_SWITCH || |
| 744 | decInsn.opCode == OP_SPARSE_SWITCH)) { |
| 745 | interpState->jitState = kJitTSelectEnd; |
| 746 | break; |
| 747 | } |
| 748 | |
| Bill Buzbee | f9f3328 | 2009-11-22 12:45:30 -0800 | [diff] [blame] | 749 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 750 | #if defined(SHOW_TRACE) |
| Andy McFadden | c6b25c7 | 2010-06-22 11:01:20 -0700 | [diff] [blame] | 751 | LOGD("TraceGen: adding %s", dexGetOpcodeName(decInsn.opCode)); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 752 | #endif |
| 753 | flags = dexGetInstrFlags(gDvm.instrFlags, decInsn.opCode); |
| Ben Cheng | 79d173c | 2009-09-29 16:12:51 -0700 | [diff] [blame] | 754 | len = dexGetInstrOrTableWidthAbs(gDvm.instrWidth, lastPC); |
| 755 | offset = lastPC - interpState->method->insns; |
| 756 | assert((unsigned) offset < |
| 757 | dvmGetMethodInsnsSize(interpState->method)); |
| 758 | if (lastPC != interpState->currRunHead + interpState->currRunLen) { |
| Bill Buzbee | 50a6bf2 | 2009-07-08 13:08:04 -0700 | [diff] [blame] | 759 | int currTraceRun; |
| 760 | /* We need to start a new trace run */ |
| 761 | currTraceRun = ++interpState->currTraceRun; |
| 762 | interpState->currRunLen = 0; |
| Ben Cheng | 79d173c | 2009-09-29 16:12:51 -0700 | [diff] [blame] | 763 | interpState->currRunHead = (u2*)lastPC; |
| Bill Buzbee | 50a6bf2 | 2009-07-08 13:08:04 -0700 | [diff] [blame] | 764 | interpState->trace[currTraceRun].frag.startOffset = offset; |
| 765 | interpState->trace[currTraceRun].frag.numInsts = 0; |
| 766 | interpState->trace[currTraceRun].frag.runEnd = false; |
| 767 | interpState->trace[currTraceRun].frag.hint = kJitHintNone; |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 768 | interpState->trace[currTraceRun].frag.isCode = true; |
| Bill Buzbee | 50a6bf2 | 2009-07-08 13:08:04 -0700 | [diff] [blame] | 769 | } |
| 770 | interpState->trace[interpState->currTraceRun].frag.numInsts++; |
| 771 | interpState->totalTraceLen++; |
| 772 | interpState->currRunLen += len; |
| Ben Cheng | 79d173c | 2009-09-29 16:12:51 -0700 | [diff] [blame] | 773 | |
| Ben Cheng | d44faf5 | 2010-06-02 15:33:51 -0700 | [diff] [blame] | 774 | /* |
| 775 | * If the last instruction is an invoke, we will try to sneak in |
| 776 | * the move-result* (if existent) into a separate trace run. |
| 777 | */ |
| 778 | int needReservedRun = (flags & kInstrInvoke) ? 1 : 0; |
| 779 | |
| Ben Cheng | 79d173c | 2009-09-29 16:12:51 -0700 | [diff] [blame] | 780 | /* Will probably never hit this with the current trace buildier */ |
| Ben Cheng | d44faf5 | 2010-06-02 15:33:51 -0700 | [diff] [blame] | 781 | if (interpState->currTraceRun == |
| 782 | (MAX_JIT_RUN_LEN - 1 - needReservedRun)) { |
| Ben Cheng | 79d173c | 2009-09-29 16:12:51 -0700 | [diff] [blame] | 783 | interpState->jitState = kJitTSelectEnd; |
| 784 | } |
| 785 | |
| Bill Buzbee | 50a6bf2 | 2009-07-08 13:08:04 -0700 | [diff] [blame] | 786 | if ( ((flags & kInstrUnconditional) == 0) && |
| Bill Buzbee | f4ce16f | 2009-07-28 13:28:25 -0700 | [diff] [blame] | 787 | /* don't end trace on INVOKE_DIRECT_EMPTY */ |
| 788 | (decInsn.opCode != OP_INVOKE_DIRECT_EMPTY) && |
| Bill Buzbee | 50a6bf2 | 2009-07-08 13:08:04 -0700 | [diff] [blame] | 789 | ((flags & (kInstrCanBranch | |
| 790 | kInstrCanSwitch | |
| 791 | kInstrCanReturn | |
| 792 | kInstrInvoke)) != 0)) { |
| 793 | interpState->jitState = kJitTSelectEnd; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 794 | #if defined(SHOW_TRACE) |
| Ben Cheng | d44faf5 | 2010-06-02 15:33:51 -0700 | [diff] [blame] | 795 | LOGD("TraceGen: ending on %s, basic block end", |
| Andy McFadden | c6b25c7 | 2010-06-22 11:01:20 -0700 | [diff] [blame] | 796 | dexGetOpcodeName(decInsn.opCode)); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 797 | #endif |
| Ben Cheng | d44faf5 | 2010-06-02 15:33:51 -0700 | [diff] [blame] | 798 | |
| 799 | /* |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 800 | * If the current invoke is a {virtual,interface}, get the |
| 801 | * current class/method pair into the trace as well. |
| Ben Cheng | d44faf5 | 2010-06-02 15:33:51 -0700 | [diff] [blame] | 802 | * If the next instruction is a variant of move-result, insert |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 803 | * it to the trace too. |
| Ben Cheng | d44faf5 | 2010-06-02 15:33:51 -0700 | [diff] [blame] | 804 | */ |
| 805 | if (flags & kInstrInvoke) { |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 806 | insertClassMethodInfo(interpState, thisClass, curMethod, |
| 807 | &decInsn); |
| Ben Cheng | d44faf5 | 2010-06-02 15:33:51 -0700 | [diff] [blame] | 808 | insertMoveResult(lastPC, len, offset, interpState); |
| 809 | } |
| Bill Buzbee | 50a6bf2 | 2009-07-08 13:08:04 -0700 | [diff] [blame] | 810 | } |
| Bill Buzbee | 2ce8a6c | 2009-12-03 15:09:32 -0800 | [diff] [blame] | 811 | /* Break on throw or self-loop */ |
| Bill Buzbee | 324b3ac | 2009-12-04 13:17:36 -0800 | [diff] [blame] | 812 | if ((decInsn.opCode == OP_THROW) || (lastPC == pc)){ |
| Bill Buzbee | 50a6bf2 | 2009-07-08 13:08:04 -0700 | [diff] [blame] | 813 | interpState->jitState = kJitTSelectEnd; |
| 814 | } |
| 815 | if (interpState->totalTraceLen >= JIT_MAX_TRACE_LEN) { |
| 816 | interpState->jitState = kJitTSelectEnd; |
| 817 | } |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 818 | /* Abandon the trace request if debugger/profiler is attached */ |
| Bill Buzbee | 50a6bf2 | 2009-07-08 13:08:04 -0700 | [diff] [blame] | 819 | if (debugOrProfile) { |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 820 | interpState->jitState = kJitDone; |
| Bill Buzbee | 50a6bf2 | 2009-07-08 13:08:04 -0700 | [diff] [blame] | 821 | break; |
| 822 | } |
| 823 | if ((flags & kInstrCanReturn) != kInstrCanReturn) { |
| 824 | break; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 825 | } |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 826 | else { |
| 827 | /* |
| 828 | * Last instruction is a return - stay in the dbg interpreter |
| 829 | * for one more instruction if it is a non-void return, since |
| 830 | * we don't want to start a trace with move-result as the first |
| 831 | * instruction (which is already included in the trace |
| 832 | * containing the invoke. |
| 833 | */ |
| 834 | if (decInsn.opCode != OP_RETURN_VOID) { |
| 835 | stayOneMoreInst = true; |
| 836 | } |
| 837 | } |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 838 | /* NOTE: intentional fallthrough for returns */ |
| 839 | case kJitTSelectEnd: |
| 840 | { |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 841 | /* Bad trace */ |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 842 | if (interpState->totalTraceLen == 0) { |
| Bill Buzbee | d726991 | 2009-11-10 14:31:32 -0800 | [diff] [blame] | 843 | /* Bad trace - mark as untranslatable */ |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 844 | interpState->jitState = kJitDone; |
| 845 | switchInterp = true; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 846 | break; |
| 847 | } |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 848 | |
| 849 | int lastTraceDesc = interpState->currTraceRun; |
| 850 | |
| 851 | /* Extend a new empty desc if the last slot is meta info */ |
| 852 | if (!interpState->trace[lastTraceDesc].frag.isCode) { |
| 853 | lastTraceDesc = ++interpState->currTraceRun; |
| 854 | interpState->trace[lastTraceDesc].frag.startOffset = 0; |
| 855 | interpState->trace[lastTraceDesc].frag.numInsts = 0; |
| 856 | interpState->trace[lastTraceDesc].frag.hint = kJitHintNone; |
| 857 | interpState->trace[lastTraceDesc].frag.isCode = true; |
| 858 | } |
| 859 | |
| 860 | /* Mark the end of the trace runs */ |
| 861 | interpState->trace[lastTraceDesc].frag.runEnd = true; |
| 862 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 863 | JitTraceDescription* desc = |
| 864 | (JitTraceDescription*)malloc(sizeof(JitTraceDescription) + |
| 865 | sizeof(JitTraceRun) * (interpState->currTraceRun+1)); |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 866 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 867 | if (desc == NULL) { |
| 868 | LOGE("Out of memory in trace selection"); |
| 869 | dvmJitStopTranslationRequests(); |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 870 | interpState->jitState = kJitDone; |
| 871 | switchInterp = true; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 872 | break; |
| 873 | } |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 874 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 875 | desc->method = interpState->method; |
| 876 | memcpy((char*)&(desc->trace[0]), |
| 877 | (char*)&(interpState->trace[0]), |
| 878 | sizeof(JitTraceRun) * (interpState->currTraceRun+1)); |
| 879 | #if defined(SHOW_TRACE) |
| 880 | LOGD("TraceGen: trace done, adding to queue"); |
| 881 | #endif |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 882 | if (dvmCompilerWorkEnqueue( |
| 883 | interpState->currTraceHead,kWorkOrderTrace,desc)) { |
| 884 | /* Work order successfully enqueued */ |
| 885 | if (gDvmJit.blockingMode) { |
| 886 | dvmCompilerDrainQueue(); |
| 887 | } |
| Ben Cheng | 1357e94 | 2010-02-10 17:21:39 -0800 | [diff] [blame] | 888 | } else { |
| 889 | /* |
| 890 | * Make sure the descriptor for the abandoned work order is |
| 891 | * freed. |
| 892 | */ |
| 893 | free(desc); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 894 | } |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 895 | /* |
| 896 | * Reset "trace in progress" flag whether or not we |
| 897 | * successfully entered a work order. |
| 898 | */ |
| Ben Cheng | 6999d84 | 2010-01-26 16:46:15 -0800 | [diff] [blame] | 899 | JitEntry *jitEntry = |
| 900 | lookupAndAdd(interpState->currTraceHead, false); |
| 901 | if (jitEntry) { |
| 902 | setTraceConstruction(jitEntry, false); |
| 903 | } |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 904 | interpState->jitState = kJitDone; |
| 905 | switchInterp = true; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 906 | } |
| 907 | break; |
| 908 | case kJitSingleStep: |
| 909 | interpState->jitState = kJitSingleStepEnd; |
| 910 | break; |
| 911 | case kJitSingleStepEnd: |
| 912 | interpState->entryPoint = kInterpEntryResume; |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 913 | interpState->jitState = kJitDone; |
| 914 | switchInterp = true; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 915 | break; |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 916 | case kJitDone: |
| 917 | switchInterp = true; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 918 | break; |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 919 | #if defined(WITH_SELF_VERIFICATION) |
| 920 | case kJitSelfVerification: |
| Ben Cheng | d5adae1 | 2010-03-26 17:45:28 -0700 | [diff] [blame] | 921 | if (selfVerificationDebugInterp(pc, self, interpState)) { |
| 922 | /* |
| 923 | * If the next state is not single-step end, we can switch |
| 924 | * interpreter now. |
| 925 | */ |
| 926 | if (interpState->jitState != kJitSingleStepEnd) { |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 927 | interpState->jitState = kJitDone; |
| 928 | switchInterp = true; |
| Ben Cheng | d5adae1 | 2010-03-26 17:45:28 -0700 | [diff] [blame] | 929 | } |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 930 | } |
| 931 | break; |
| 932 | #endif |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 933 | case kJitNot: |
| Ben Cheng | 1c52e6d | 2010-07-02 13:00:39 -0700 | [diff] [blame] | 934 | switchInterp = !debugOrProfile; |
| Ben Cheng | ed79ff0 | 2009-10-13 13:26:40 -0700 | [diff] [blame] | 935 | break; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 936 | default: |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 937 | LOGE("Unexpected JIT state: %d entry point: %d", |
| 938 | interpState->jitState, interpState->entryPoint); |
| 939 | dvmAbort(); |
| Ben Cheng | 9c147b8 | 2009-10-07 16:41:46 -0700 | [diff] [blame] | 940 | break; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 941 | } |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 942 | /* |
| 943 | * Final check to see if we can really switch the interpreter. Make sure |
| 944 | * the jitState is kJitDone or kJitNot when switchInterp is set to true. |
| 945 | */ |
| 946 | assert(switchInterp == false || interpState->jitState == kJitDone || |
| 947 | interpState->jitState == kJitNot); |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 948 | return switchInterp && !debugOrProfile && !stayOneMoreInst; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 949 | } |
| 950 | |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 951 | JitEntry *dvmFindJitEntry(const u2* pc) |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 952 | { |
| 953 | int idx = dvmJitHash(pc); |
| 954 | |
| 955 | /* Expect a high hit rate on 1st shot */ |
| 956 | if (gDvmJit.pJitEntryTable[idx].dPC == pc) |
| 957 | return &gDvmJit.pJitEntryTable[idx]; |
| 958 | else { |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 959 | int chainEndMarker = gDvmJit.jitTableSize; |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 960 | while (gDvmJit.pJitEntryTable[idx].u.info.chain != chainEndMarker) { |
| 961 | idx = gDvmJit.pJitEntryTable[idx].u.info.chain; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 962 | if (gDvmJit.pJitEntryTable[idx].dPC == pc) |
| 963 | return &gDvmJit.pJitEntryTable[idx]; |
| 964 | } |
| 965 | } |
| 966 | return NULL; |
| 967 | } |
| 968 | |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 969 | /* |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 970 | * If a translated code address exists for the davik byte code |
| 971 | * pointer return it. This routine needs to be fast. |
| 972 | */ |
| 973 | void* dvmJitGetCodeAddr(const u2* dPC) |
| 974 | { |
| 975 | int idx = dvmJitHash(dPC); |
| Ben Cheng | 6999d84 | 2010-01-26 16:46:15 -0800 | [diff] [blame] | 976 | const u2* npc = gDvmJit.pJitEntryTable[idx].dPC; |
| Bill Buzbee | 9797a23 | 2010-01-12 12:20:13 -0800 | [diff] [blame] | 977 | if (npc != NULL) { |
| Ben Cheng | 6999d84 | 2010-01-26 16:46:15 -0800 | [diff] [blame] | 978 | bool hideTranslation = (gDvm.sumThreadSuspendCount != 0) || |
| 979 | (gDvmJit.codeCacheFull == true) || |
| 980 | (gDvmJit.pProfTable == NULL); |
| 981 | |
| Bill Buzbee | 9797a23 | 2010-01-12 12:20:13 -0800 | [diff] [blame] | 982 | if (npc == dPC) { |
| Ben Cheng | 978738d | 2010-05-13 13:45:57 -0700 | [diff] [blame] | 983 | #if defined(WITH_JIT_TUNING) |
| Bill Buzbee | 9797a23 | 2010-01-12 12:20:13 -0800 | [diff] [blame] | 984 | gDvmJit.addrLookupsFound++; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 985 | #endif |
| Ben Cheng | 6999d84 | 2010-01-26 16:46:15 -0800 | [diff] [blame] | 986 | return hideTranslation ? |
| 987 | NULL : gDvmJit.pJitEntryTable[idx].codeAddress; |
| Bill Buzbee | 9797a23 | 2010-01-12 12:20:13 -0800 | [diff] [blame] | 988 | } else { |
| 989 | int chainEndMarker = gDvmJit.jitTableSize; |
| 990 | while (gDvmJit.pJitEntryTable[idx].u.info.chain != chainEndMarker) { |
| 991 | idx = gDvmJit.pJitEntryTable[idx].u.info.chain; |
| 992 | if (gDvmJit.pJitEntryTable[idx].dPC == dPC) { |
| Ben Cheng | 978738d | 2010-05-13 13:45:57 -0700 | [diff] [blame] | 993 | #if defined(WITH_JIT_TUNING) |
| Bill Buzbee | 9797a23 | 2010-01-12 12:20:13 -0800 | [diff] [blame] | 994 | gDvmJit.addrLookupsFound++; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 995 | #endif |
| Ben Cheng | 6999d84 | 2010-01-26 16:46:15 -0800 | [diff] [blame] | 996 | return hideTranslation ? |
| 997 | NULL : gDvmJit.pJitEntryTable[idx].codeAddress; |
| Bill Buzbee | 9797a23 | 2010-01-12 12:20:13 -0800 | [diff] [blame] | 998 | } |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 999 | } |
| 1000 | } |
| 1001 | } |
| Ben Cheng | 978738d | 2010-05-13 13:45:57 -0700 | [diff] [blame] | 1002 | #if defined(WITH_JIT_TUNING) |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1003 | gDvmJit.addrLookupsNotFound++; |
| 1004 | #endif |
| 1005 | return NULL; |
| 1006 | } |
| 1007 | |
| 1008 | /* |
| 1009 | * Register the translated code pointer into the JitTable. |
| Bill Buzbee | 9a8c75a | 2009-11-08 14:31:20 -0800 | [diff] [blame] | 1010 | * NOTE: Once a codeAddress field transitions from initial state to |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1011 | * JIT'd code, it must not be altered without first halting all |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1012 | * threads. This routine should only be called by the compiler |
| 1013 | * thread. |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1014 | */ |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1015 | void dvmJitSetCodeAddr(const u2* dPC, void *nPC, JitInstructionSetType set) { |
| 1016 | JitEntryInfoUnion oldValue; |
| 1017 | JitEntryInfoUnion newValue; |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 1018 | JitEntry *jitEntry = lookupAndAdd(dPC, false); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1019 | assert(jitEntry); |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1020 | /* Note: order of update is important */ |
| 1021 | do { |
| 1022 | oldValue = jitEntry->u; |
| 1023 | newValue = oldValue; |
| 1024 | newValue.info.instructionSet = set; |
| Andy McFadden | 6e10b9a | 2010-06-14 15:24:39 -0700 | [diff] [blame] | 1025 | } while (android_atomic_release_cas( |
| 1026 | oldValue.infoWord, newValue.infoWord, |
| 1027 | &jitEntry->u.infoWord) != 0); |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1028 | jitEntry->codeAddress = nPC; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | /* |
| 1032 | * Determine if valid trace-bulding request is active. Return true |
| 1033 | * if we need to abort and switch back to the fast interpreter, false |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 1034 | * otherwise. |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1035 | */ |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1036 | bool dvmJitCheckTraceRequest(Thread* self, InterpState* interpState) |
| 1037 | { |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 1038 | bool switchInterp = false; /* Assume success */ |
| Bill Buzbee | 48f1824 | 2009-06-19 16:02:27 -0700 | [diff] [blame] | 1039 | int i; |
| buzbee | 852aacd | 2010-06-08 16:24:46 -0700 | [diff] [blame] | 1040 | /* |
| 1041 | * A note on trace "hotness" filtering: |
| 1042 | * |
| 1043 | * Our first level trigger is intentionally loose - we need it to |
| 1044 | * fire easily not just to identify potential traces to compile, but |
| 1045 | * also to allow re-entry into the code cache. |
| 1046 | * |
| 1047 | * The 2nd level filter (done here) exists to be selective about |
| 1048 | * what we actually compile. It works by requiring the same |
| 1049 | * trace head "key" (defined as filterKey below) to appear twice in |
| 1050 | * a relatively short period of time. The difficulty is defining the |
| 1051 | * shape of the filterKey. Unfortunately, there is no "one size fits |
| 1052 | * all" approach. |
| 1053 | * |
| 1054 | * For spiky execution profiles dominated by a smallish |
| 1055 | * number of very hot loops, we would want the second-level filter |
| 1056 | * to be very selective. A good selective filter is requiring an |
| 1057 | * exact match of the Dalvik PC. In other words, defining filterKey as: |
| 1058 | * intptr_t filterKey = (intptr_t)interpState->pc |
| 1059 | * |
| 1060 | * However, for flat execution profiles we do best when aggressively |
| 1061 | * translating. A heuristically decent proxy for this is to use |
| 1062 | * the value of the method pointer containing the trace as the filterKey. |
| 1063 | * Intuitively, this is saying that once any trace in a method appears hot, |
| 1064 | * immediately translate any other trace from that same method that |
| 1065 | * survives the first-level filter. Here, filterKey would be defined as: |
| 1066 | * intptr_t filterKey = (intptr_t)interpState->method |
| 1067 | * |
| 1068 | * The problem is that we can't easily detect whether we're dealing |
| 1069 | * with a spiky or flat profile. If we go with the "pc" match approach, |
| 1070 | * flat profiles perform poorly. If we go with the loose "method" match, |
| 1071 | * we end up generating a lot of useless translations. Probably the |
| 1072 | * best approach in the future will be to retain profile information |
| 1073 | * across runs of each application in order to determine it's profile, |
| 1074 | * and then choose once we have enough history. |
| 1075 | * |
| 1076 | * However, for now we've decided to chose a compromise filter scheme that |
| 1077 | * includes elements of both. The high order bits of the filter key |
| 1078 | * are drawn from the enclosing method, and are combined with a slice |
| 1079 | * of the low-order bits of the Dalvik pc of the trace head. The |
| 1080 | * looseness of the filter can be adjusted by changing with width of |
| 1081 | * the Dalvik pc slice (JIT_TRACE_THRESH_FILTER_PC_BITS). The wider |
| 1082 | * the slice, the tighter the filter. |
| 1083 | * |
| 1084 | * Note: the fixed shifts in the function below reflect assumed word |
| 1085 | * alignment for method pointers, and half-word alignment of the Dalvik pc. |
| 1086 | * for method pointers and half-word alignment for dalvik pc. |
| 1087 | */ |
| buzbee | c35294d | 2010-06-09 14:22:50 -0700 | [diff] [blame] | 1088 | u4 methodKey = (u4)interpState->method << |
| 1089 | (JIT_TRACE_THRESH_FILTER_PC_BITS - 2); |
| 1090 | u4 pcKey = ((u4)interpState->pc >> 1) & |
| 1091 | ((1 << JIT_TRACE_THRESH_FILTER_PC_BITS) - 1); |
| 1092 | intptr_t filterKey = (intptr_t)(methodKey | pcKey); |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 1093 | bool debugOrProfile = dvmDebuggerOrProfilerActive(); |
| Ben Cheng | 40094c1 | 2010-02-24 20:58:44 -0800 | [diff] [blame] | 1094 | |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 1095 | /* Check if the JIT request can be handled now */ |
| 1096 | if (gDvmJit.pJitEntryTable != NULL && debugOrProfile == false) { |
| 1097 | /* Bypass the filter for hot trace requests or during stress mode */ |
| 1098 | if (interpState->jitState == kJitTSelectRequest && |
| 1099 | gDvmJit.threshold > 6) { |
| Ben Cheng | 40094c1 | 2010-02-24 20:58:44 -0800 | [diff] [blame] | 1100 | /* Two-level filtering scheme */ |
| 1101 | for (i=0; i< JIT_TRACE_THRESH_FILTER_SIZE; i++) { |
| 1102 | if (filterKey == interpState->threshFilter[i]) { |
| buzbee | 852aacd | 2010-06-08 16:24:46 -0700 | [diff] [blame] | 1103 | interpState->threshFilter[i] = 0; // Reset filter entry |
| Ben Cheng | 40094c1 | 2010-02-24 20:58:44 -0800 | [diff] [blame] | 1104 | break; |
| 1105 | } |
| Bill Buzbee | 48f1824 | 2009-06-19 16:02:27 -0700 | [diff] [blame] | 1106 | } |
| Ben Cheng | 40094c1 | 2010-02-24 20:58:44 -0800 | [diff] [blame] | 1107 | if (i == JIT_TRACE_THRESH_FILTER_SIZE) { |
| 1108 | /* |
| 1109 | * Use random replacement policy - otherwise we could miss a |
| 1110 | * large loop that contains more traces than the size of our |
| 1111 | * filter array. |
| 1112 | */ |
| 1113 | i = rand() % JIT_TRACE_THRESH_FILTER_SIZE; |
| 1114 | interpState->threshFilter[i] = filterKey; |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 1115 | interpState->jitState = kJitDone; |
| Ben Cheng | 40094c1 | 2010-02-24 20:58:44 -0800 | [diff] [blame] | 1116 | } |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 1117 | } |
| Bill Buzbee | d726991 | 2009-11-10 14:31:32 -0800 | [diff] [blame] | 1118 | |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 1119 | /* If the compiler is backlogged, cancel any JIT actions */ |
| 1120 | if (gDvmJit.compilerQueueLength >= gDvmJit.compilerHighWater) { |
| 1121 | interpState->jitState = kJitDone; |
| Ben Cheng | 40094c1 | 2010-02-24 20:58:44 -0800 | [diff] [blame] | 1122 | } |
| Bill Buzbee | d726991 | 2009-11-10 14:31:32 -0800 | [diff] [blame] | 1123 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1124 | /* |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 1125 | * Check for additional reasons that might force the trace select |
| 1126 | * request to be dropped |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1127 | */ |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 1128 | if (interpState->jitState == kJitTSelectRequest || |
| 1129 | interpState->jitState == kJitTSelectRequestHot) { |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 1130 | JitEntry *slot = lookupAndAdd(interpState->pc, false); |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1131 | if (slot == NULL) { |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1132 | /* |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1133 | * Table is full. This should have been |
| 1134 | * detected by the compiler thread and the table |
| 1135 | * resized before we run into it here. Assume bad things |
| 1136 | * are afoot and disable profiling. |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1137 | */ |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 1138 | interpState->jitState = kJitDone; |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1139 | LOGD("JIT: JitTable full, disabling profiling"); |
| 1140 | dvmJitStopTranslationRequests(); |
| Bill Buzbee | d726991 | 2009-11-10 14:31:32 -0800 | [diff] [blame] | 1141 | } else if (slot->u.info.traceConstruction) { |
| 1142 | /* |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 1143 | * Trace request already in progress, but most likely it |
| Bill Buzbee | d726991 | 2009-11-10 14:31:32 -0800 | [diff] [blame] | 1144 | * aborted without cleaning up. Assume the worst and |
| 1145 | * mark trace head as untranslatable. If we're wrong, |
| 1146 | * the compiler thread will correct the entry when the |
| 1147 | * translation is completed. The downside here is that |
| 1148 | * some existing translation may chain to the interpret-only |
| 1149 | * template instead of the real translation during this |
| 1150 | * window. Performance, but not correctness, issue. |
| 1151 | */ |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 1152 | interpState->jitState = kJitDone; |
| Bill Buzbee | d726991 | 2009-11-10 14:31:32 -0800 | [diff] [blame] | 1153 | resetTracehead(interpState, slot); |
| 1154 | } else if (slot->codeAddress) { |
| 1155 | /* Nothing to do here - just return */ |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 1156 | interpState->jitState = kJitDone; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1157 | } else { |
| Bill Buzbee | d726991 | 2009-11-10 14:31:32 -0800 | [diff] [blame] | 1158 | /* |
| 1159 | * Mark request. Note, we are not guaranteed exclusivity |
| 1160 | * here. A window exists for another thread to be |
| 1161 | * attempting to build this same trace. Rather than |
| 1162 | * bear the cost of locking, we'll just allow that to |
| 1163 | * happen. The compiler thread, if it chooses, can |
| 1164 | * discard redundant requests. |
| 1165 | */ |
| 1166 | setTraceConstruction(slot, true); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1167 | } |
| 1168 | } |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 1169 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1170 | switch (interpState->jitState) { |
| 1171 | case kJitTSelectRequest: |
| Ben Cheng | 40094c1 | 2010-02-24 20:58:44 -0800 | [diff] [blame] | 1172 | case kJitTSelectRequestHot: |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 1173 | interpState->jitState = kJitTSelect; |
| 1174 | interpState->currTraceHead = interpState->pc; |
| 1175 | interpState->currTraceRun = 0; |
| 1176 | interpState->totalTraceLen = 0; |
| 1177 | interpState->currRunHead = interpState->pc; |
| 1178 | interpState->currRunLen = 0; |
| 1179 | interpState->trace[0].frag.startOffset = |
| 1180 | interpState->pc - interpState->method->insns; |
| 1181 | interpState->trace[0].frag.numInsts = 0; |
| 1182 | interpState->trace[0].frag.runEnd = false; |
| 1183 | interpState->trace[0].frag.hint = kJitHintNone; |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 1184 | interpState->trace[0].frag.isCode = true; |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 1185 | interpState->lastPC = 0; |
| 1186 | break; |
| 1187 | /* |
| 1188 | * For JIT's perspective there is no need to stay in the debug |
| 1189 | * interpreter unless debugger/profiler is attached. |
| 1190 | */ |
| 1191 | case kJitDone: |
| 1192 | switchInterp = true; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1193 | break; |
| 1194 | default: |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 1195 | LOGE("Unexpected JIT state: %d entry point: %d", |
| 1196 | interpState->jitState, interpState->entryPoint); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1197 | dvmAbort(); |
| 1198 | } |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 1199 | } else { |
| 1200 | /* |
| 1201 | * Cannot build trace this time - ready to leave the dbg interpreter |
| 1202 | */ |
| 1203 | interpState->jitState = kJitDone; |
| 1204 | switchInterp = true; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1205 | } |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 1206 | |
| 1207 | /* |
| 1208 | * Final check to see if we can really switch the interpreter. Make sure |
| 1209 | * the jitState is kJitDone when switchInterp is set to true. |
| 1210 | */ |
| 1211 | assert(switchInterp == false || interpState->jitState == kJitDone); |
| 1212 | return switchInterp && !debugOrProfile; |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1213 | } |
| 1214 | |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 1215 | /* |
| 1216 | * Resizes the JitTable. Must be a power of 2, and returns true on failure. |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 1217 | * Stops all threads, and thus is a heavyweight operation. May only be called |
| 1218 | * by the compiler thread. |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 1219 | */ |
| 1220 | bool dvmJitResizeJitTable( unsigned int size ) |
| 1221 | { |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1222 | JitEntry *pNewTable; |
| 1223 | JitEntry *pOldTable; |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 1224 | JitEntry tempEntry; |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 1225 | u4 newMask; |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1226 | unsigned int oldSize; |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 1227 | unsigned int i; |
| 1228 | |
| Ben Cheng | 3f02aa4 | 2009-08-14 13:52:09 -0700 | [diff] [blame] | 1229 | assert(gDvmJit.pJitEntryTable != NULL); |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 1230 | assert(size && !(size & (size - 1))); /* Is power of 2? */ |
| 1231 | |
| Ben Cheng | a497359 | 2010-03-31 11:59:18 -0700 | [diff] [blame] | 1232 | LOGI("Jit: resizing JitTable from %d to %d", gDvmJit.jitTableSize, size); |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 1233 | |
| 1234 | newMask = size - 1; |
| 1235 | |
| 1236 | if (size <= gDvmJit.jitTableSize) { |
| 1237 | return true; |
| 1238 | } |
| 1239 | |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 1240 | /* Make sure requested size is compatible with chain field width */ |
| 1241 | tempEntry.u.info.chain = size; |
| 1242 | if (tempEntry.u.info.chain != size) { |
| 1243 | LOGD("Jit: JitTable request of %d too big", size); |
| 1244 | return true; |
| 1245 | } |
| 1246 | |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1247 | pNewTable = (JitEntry*)calloc(size, sizeof(*pNewTable)); |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 1248 | if (pNewTable == NULL) { |
| 1249 | return true; |
| 1250 | } |
| 1251 | for (i=0; i< size; i++) { |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1252 | pNewTable[i].u.info.chain = size; /* Initialize chain termination */ |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | /* Stop all other interpreting/jit'ng threads */ |
| Ben Cheng | a8e64a7 | 2009-10-20 13:01:36 -0700 | [diff] [blame] | 1256 | dvmSuspendAllThreads(SUSPEND_FOR_TBL_RESIZE); |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 1257 | |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1258 | pOldTable = gDvmJit.pJitEntryTable; |
| 1259 | oldSize = gDvmJit.jitTableSize; |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 1260 | |
| 1261 | dvmLockMutex(&gDvmJit.tableLock); |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 1262 | gDvmJit.pJitEntryTable = pNewTable; |
| 1263 | gDvmJit.jitTableSize = size; |
| 1264 | gDvmJit.jitTableMask = size - 1; |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1265 | gDvmJit.jitTableEntriesUsed = 0; |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 1266 | |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1267 | for (i=0; i < oldSize; i++) { |
| 1268 | if (pOldTable[i].dPC) { |
| 1269 | JitEntry *p; |
| 1270 | u2 chain; |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 1271 | p = lookupAndAdd(pOldTable[i].dPC, true /* holds tableLock*/ ); |
| 1272 | p->codeAddress = pOldTable[i].codeAddress; |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1273 | /* We need to preserve the new chain field, but copy the rest */ |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1274 | chain = p->u.info.chain; |
| 1275 | p->u = pOldTable[i].u; |
| 1276 | p->u.info.chain = chain; |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1277 | } |
| 1278 | } |
| Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 1279 | dvmUnlockMutex(&gDvmJit.tableLock); |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1280 | |
| 1281 | free(pOldTable); |
| 1282 | |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 1283 | /* Restart the world */ |
| Ben Cheng | a8e64a7 | 2009-10-20 13:01:36 -0700 | [diff] [blame] | 1284 | dvmResumeAllThreads(SUSPEND_FOR_TBL_RESIZE); |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 1285 | |
| 1286 | return false; |
| 1287 | } |
| 1288 | |
| Bill Buzbee | 50a6bf2 | 2009-07-08 13:08:04 -0700 | [diff] [blame] | 1289 | /* |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 1290 | * Reset the JitTable to the initial clean state. |
| 1291 | */ |
| 1292 | void dvmJitResetTable(void) |
| 1293 | { |
| 1294 | JitEntry *jitEntry = gDvmJit.pJitEntryTable; |
| 1295 | unsigned int size = gDvmJit.jitTableSize; |
| 1296 | unsigned int i; |
| 1297 | |
| 1298 | dvmLockMutex(&gDvmJit.tableLock); |
| 1299 | memset((void *) jitEntry, 0, sizeof(JitEntry) * size); |
| 1300 | for (i=0; i< size; i++) { |
| 1301 | jitEntry[i].u.info.chain = size; /* Initialize chain termination */ |
| 1302 | } |
| 1303 | gDvmJit.jitTableEntriesUsed = 0; |
| 1304 | dvmUnlockMutex(&gDvmJit.tableLock); |
| 1305 | } |
| 1306 | |
| 1307 | /* |
| Bill Buzbee | 50a6bf2 | 2009-07-08 13:08:04 -0700 | [diff] [blame] | 1308 | * Float/double conversion requires clamping to min and max of integer form. If |
| 1309 | * target doesn't support this normally, use these. |
| 1310 | */ |
| 1311 | s8 dvmJitd2l(double d) |
| 1312 | { |
| Bill Buzbee | 9727c3d | 2009-08-01 11:32:36 -0700 | [diff] [blame] | 1313 | static const double kMaxLong = (double)(s8)0x7fffffffffffffffULL; |
| 1314 | static const double kMinLong = (double)(s8)0x8000000000000000ULL; |
| Bill Buzbee | 50a6bf2 | 2009-07-08 13:08:04 -0700 | [diff] [blame] | 1315 | if (d >= kMaxLong) |
| Bill Buzbee | 9727c3d | 2009-08-01 11:32:36 -0700 | [diff] [blame] | 1316 | return (s8)0x7fffffffffffffffULL; |
| Bill Buzbee | 50a6bf2 | 2009-07-08 13:08:04 -0700 | [diff] [blame] | 1317 | else if (d <= kMinLong) |
| Bill Buzbee | 9727c3d | 2009-08-01 11:32:36 -0700 | [diff] [blame] | 1318 | return (s8)0x8000000000000000ULL; |
| Bill Buzbee | 50a6bf2 | 2009-07-08 13:08:04 -0700 | [diff] [blame] | 1319 | else if (d != d) // NaN case |
| 1320 | return 0; |
| 1321 | else |
| 1322 | return (s8)d; |
| 1323 | } |
| 1324 | |
| 1325 | s8 dvmJitf2l(float f) |
| 1326 | { |
| Bill Buzbee | 9727c3d | 2009-08-01 11:32:36 -0700 | [diff] [blame] | 1327 | static const float kMaxLong = (float)(s8)0x7fffffffffffffffULL; |
| 1328 | static const float kMinLong = (float)(s8)0x8000000000000000ULL; |
| Bill Buzbee | 50a6bf2 | 2009-07-08 13:08:04 -0700 | [diff] [blame] | 1329 | if (f >= kMaxLong) |
| Bill Buzbee | 9727c3d | 2009-08-01 11:32:36 -0700 | [diff] [blame] | 1330 | return (s8)0x7fffffffffffffffULL; |
| Bill Buzbee | 50a6bf2 | 2009-07-08 13:08:04 -0700 | [diff] [blame] | 1331 | else if (f <= kMinLong) |
| Bill Buzbee | 9727c3d | 2009-08-01 11:32:36 -0700 | [diff] [blame] | 1332 | return (s8)0x8000000000000000ULL; |
| Bill Buzbee | 50a6bf2 | 2009-07-08 13:08:04 -0700 | [diff] [blame] | 1333 | else if (f != f) // NaN case |
| 1334 | return 0; |
| 1335 | else |
| 1336 | return (s8)f; |
| 1337 | } |
| 1338 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1339 | #endif /* WITH_JIT */ |