blob: 19f2cf658773026bef7c0685dc15213853ef97b9 [file] [log] [blame]
Ben Chengba4fc8b2009-06-01 13:00:29 -07001/*
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 McFaddenc6b25c72010-06-22 11:01:20 -070026#include "libdex/OpCodeNames.h"
Ben Chengba4fc8b2009-06-01 13:00:29 -070027#include <unistd.h>
28#include <pthread.h>
29#include <sys/time.h>
30#include <signal.h>
31#include "compiler/Compiler.h"
Bill Buzbee6e963e12009-06-17 16:56:19 -070032#include "compiler/CompilerUtility.h"
33#include "compiler/CompilerIR.h"
Ben Chengba4fc8b2009-06-01 13:00:29 -070034#include <errno.h>
35
Jeff Hao97319a82009-08-12 16:57:15 -070036#if defined(WITH_SELF_VERIFICATION)
37/* Allocate space for per-thread ShadowSpace data structures */
38void* 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 */
52void 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 */
62void* dvmSelfVerificationSaveState(const u2* pc, const void* fp,
Bill Buzbee9a8c75a2009-11-08 14:31:20 -080063 InterpState* interpState, int targetTrace)
Jeff Hao97319a82009-08-12 16:57:15 -070064{
65 Thread *self = dvmThreadSelf();
66 ShadowSpace *shadowSpace = self->shadowSpace;
Ben Cheng11d8f142010-03-24 15:24:19 -070067 unsigned preBytes = interpState->method->outsSize*4 + sizeof(StackSaveArea);
68 unsigned postBytes = interpState->method->registersSize*4;
Jeff Hao97319a82009-08-12 16:57:15 -070069
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 Chengccd6c012009-10-15 14:52:45 -070077 LOGD("PC: 0x%x FP: 0x%x", (int)pc, (int)fp);
Jeff Hao97319a82009-08-12 16:57:15 -070078 }
79 shadowSpace->selfVerificationState = kSVSStart;
80
Ben Chengd5adae12010-03-26 17:45:28 -070081 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 Hao97319a82009-08-12 16:57:15 -070095 // Dynamically grow shadow register space if necessary
Ben Cheng11d8f142010-03-24 15:24:19 -070096 if (preBytes + postBytes > shadowSpace->registerSpaceSize * sizeof(u4)) {
Jeff Hao97319a82009-08-12 16:57:15 -070097 free(shadowSpace->registerSpace);
Ben Cheng11d8f142010-03-24 15:24:19 -070098 shadowSpace->registerSpaceSize = (preBytes + postBytes) / sizeof(u4);
Jeff Hao97319a82009-08-12 16:57:15 -070099 shadowSpace->registerSpace =
Ben Cheng11d8f142010-03-24 15:24:19 -0700100 (int*) calloc(shadowSpace->registerSpaceSize, sizeof(u4));
Jeff Hao97319a82009-08-12 16:57:15 -0700101 }
102
103 // Remember original state
104 shadowSpace->startPC = pc;
105 shadowSpace->fp = fp;
Ben Chengccd6c012009-10-15 14:52:45 -0700106 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 Hao97319a82009-08-12 16:57:15 -0700112 shadowSpace->shadowFP = shadowSpace->registerSpace +
113 shadowSpace->registerSpaceSize - postBytes/4;
114
115 // Create a copy of the InterpState
Ben Chengccd6c012009-10-15 14:52:45 -0700116 memcpy(&(shadowSpace->interpState), interpState, sizeof(InterpState));
Jeff Hao97319a82009-08-12 16:57:15 -0700117 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 */
137void* dvmSelfVerificationRestoreState(const u2* pc, const void* fp,
Ben Cheng7a2697d2010-06-07 13:44:23 -0700138 SelfVerificationState exitState)
Jeff Hao97319a82009-08-12 16:57:15 -0700139{
140 Thread *self = dvmThreadSelf();
141 ShadowSpace *shadowSpace = self->shadowSpace;
Ben Chengd5adae12010-03-26 17:45:28 -0700142 // Official InterpState structure
143 InterpState *realGlue = shadowSpace->glue;
Jeff Hao97319a82009-08-12 16:57:15 -0700144 shadowSpace->endPC = pc;
145 shadowSpace->endShadowFP = fp;
Ben Cheng7a2697d2010-06-07 13:44:23 -0700146 shadowSpace->jitExitState = exitState;
Jeff Hao97319a82009-08-12 16:57:15 -0700147
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 Chengccd6c012009-10-15 14:52:45 -0700156 LOGD("Dalvik PC: 0x%x endPC: 0x%x", (int)shadowSpace->startPC,
Jeff Hao97319a82009-08-12 16:57:15 -0700157 (int)shadowSpace->endPC);
Ben Chengccd6c012009-10-15 14:52:45 -0700158 LOGD("Interp FP: 0x%x", (int)shadowSpace->fp);
159 LOGD("Shadow FP: 0x%x endFP: 0x%x", (int)shadowSpace->shadowFP,
Jeff Hao97319a82009-08-12 16:57:15 -0700160 (int)shadowSpace->endShadowFP);
161 }
162
Ben Chengd5adae12010-03-26 17:45:28 -0700163 // 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 Cheng7a2697d2010-06-07 13:44:23 -0700165 if (exitState == kSVSSingleStep) {
Ben Chengd5adae12010-03-26 17:45:28 -0700166 realGlue->jitResumeNPC = shadowSpace->interpState.jitResumeNPC;
167 realGlue->jitResumeDPC = shadowSpace->interpState.jitResumeDPC;
168 } else {
169 realGlue->jitResumeNPC = NULL;
170 realGlue->jitResumeDPC = NULL;
171 }
172
Jeff Hao97319a82009-08-12 16:57:15 -0700173 // Special case when punting after a single instruction
Ben Cheng7a2697d2010-06-07 13:44:23 -0700174 if (exitState == kSVSPunt && pc == shadowSpace->startPC) {
Jeff Hao97319a82009-08-12 16:57:15 -0700175 shadowSpace->selfVerificationState = kSVSIdle;
Ben Cheng60c6dbf2010-08-26 12:28:56 -0700176 } 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 Hao97319a82009-08-12 16:57:15 -0700198 } else {
Ben Cheng7a2697d2010-06-07 13:44:23 -0700199 shadowSpace->selfVerificationState = exitState;
Jeff Hao97319a82009-08-12 16:57:15 -0700200 }
201
202 return shadowSpace;
203}
204
205/* Print contents of virtual registers */
Ben Chengccd6c012009-10-15 14:52:45 -0700206static void selfVerificationPrintRegisters(int* addr, int* addrRef,
207 int numWords)
Jeff Hao97319a82009-08-12 16:57:15 -0700208{
209 int i;
210 for (i = 0; i < numWords; i++) {
Ben Chengccd6c012009-10-15 14:52:45 -0700211 LOGD("(v%d) 0x%8x%s", i, addr[i], addr[i] != addrRef[i] ? " X" : "");
Jeff Hao97319a82009-08-12 16:57:15 -0700212 }
213}
214
215/* Print values maintained in shadowSpace */
216static 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 Chengccd6c012009-10-15 14:52:45 -0700231 LOGD("CurrentPC: 0x%x, Offset: 0x%04x", (int)pc,
Jeff Hao97319a82009-08-12 16:57:15 -0700232 (int)(pc - stackSave->method->insns));
Ben Chengccd6c012009-10-15 14:52:45 -0700233 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 Hao97319a82009-08-12 16:57:15 -0700236 (int)shadowSpace->endPC);
Ben Chengccd6c012009-10-15 14:52:45 -0700237 LOGD("Interp FP: 0x%x endFP: 0x%x", (int)shadowSpace->fp,
Jeff Hao97319a82009-08-12 16:57:15 -0700238 (int)self->curFrame);
Ben Chengccd6c012009-10-15 14:52:45 -0700239 LOGD("Shadow FP: 0x%x endFP: 0x%x", (int)shadowSpace->shadowFP,
Jeff Hao97319a82009-08-12 16:57:15 -0700240 (int)shadowSpace->endShadowFP);
Ben Chengccd6c012009-10-15 14:52:45 -0700241 LOGD("Frame1 Bytes: %d Frame2 Local: %d Bytes: %d", frameBytes,
Jeff Hao97319a82009-08-12 16:57:15 -0700242 localRegs, frameBytes2);
Ben Chengccd6c012009-10-15 14:52:45 -0700243 LOGD("Trace length: %d State: %d", shadowSpace->traceLength,
Jeff Hao97319a82009-08-12 16:57:15 -0700244 shadowSpace->selfVerificationState);
245}
246
247/* Print decoded instructions in the current trace */
248static void selfVerificationDumpTrace(const u2* pc, Thread* self)
249{
250 ShadowSpace* shadowSpace = self->shadowSpace;
251 StackSaveArea* stackSave = SAVEAREA_FROM_FP(self->curFrame);
Ben Chengbcdc1de2009-08-21 16:18:46 -0700252 int i, addr, offset;
253 DecodedInstruction *decInsn;
Jeff Hao97319a82009-08-12 16:57:15 -0700254
255 LOGD("********** SHADOW TRACE DUMP **********");
256 for (i = 0; i < shadowSpace->traceLength; i++) {
Ben Chengbcdc1de2009-08-21 16:18:46 -0700257 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 McFaddenc6b25c72010-06-22 11:01:20 -0700261 LOGD("0x%x: (0x%04x) %s",
262 addr, offset, dexGetOpcodeName(decInsn->opCode));
Jeff Hao97319a82009-08-12 16:57:15 -0700263 }
264}
265
Ben Chengbcdc1de2009-08-21 16:18:46 -0700266/* Code is forced into this spin loop when a divergence is detected */
Ben Chengccd6c012009-10-15 14:52:45 -0700267static void selfVerificationSpinLoop(ShadowSpace *shadowSpace)
Ben Chengbcdc1de2009-08-21 16:18:46 -0700268{
Ben Chengccd6c012009-10-15 14:52:45 -0700269 const u2 *startPC = shadowSpace->startPC;
Ben Cheng88a0f972010-02-24 15:00:40 -0800270 JitTraceDescription* desc = dvmCopyTraceDescriptor(startPC, NULL);
Ben Chengccd6c012009-10-15 14:52:45 -0700271 if (desc) {
272 dvmCompilerWorkEnqueue(startPC, kWorkOrderTraceDebug, desc);
Ben Cheng1357e942010-02-10 17:21:39 -0800273 /*
274 * This function effectively terminates the VM right here, so not
275 * freeing the desc pointer when the enqueuing fails is acceptable.
276 */
Ben Chengccd6c012009-10-15 14:52:45 -0700277 }
Ben Chengbcdc1de2009-08-21 16:18:46 -0700278 gDvmJit.selfVerificationSpin = true;
279 while(gDvmJit.selfVerificationSpin) sleep(10);
280}
281
Jeff Hao97319a82009-08-12 16:57:15 -0700282/* Manage self verification while in the debug interpreter */
Ben Chengd5adae12010-03-26 17:45:28 -0700283static bool selfVerificationDebugInterp(const u2* pc, Thread* self,
284 InterpState *interpState)
Jeff Hao97319a82009-08-12 16:57:15 -0700285{
286 ShadowSpace *shadowSpace = self->shadowSpace;
Jeff Hao97319a82009-08-12 16:57:15 -0700287 SelfVerificationState state = shadowSpace->selfVerificationState;
Ben Chengbcdc1de2009-08-21 16:18:46 -0700288
289 DecodedInstruction decInsn;
290 dexDecodeInstruction(gDvm.instrFormat, pc, &decInsn);
291
Jeff Hao97319a82009-08-12 16:57:15 -0700292 //LOGD("### DbgIntp(%d): PC: 0x%x endPC: 0x%x state: %d len: %d %s",
293 // self->threadId, (int)pc, (int)shadowSpace->endPC, state,
Andy McFaddenc6b25c72010-06-22 11:01:20 -0700294 // shadowSpace->traceLength, dexGetOpcodeName(decInsn.opCode));
Jeff Hao97319a82009-08-12 16:57:15 -0700295
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 Chengd5adae12010-03-26 17:45:28 -0700303 /*
304 * Skip endPC once when trace has a backward branch. If the SV state is
305 * single step, keep it that way.
306 */
Jeff Hao97319a82009-08-12 16:57:15 -0700307 if ((state == kSVSBackwardBranch && pc == shadowSpace->endPC) ||
Ben Chengd5adae12010-03-26 17:45:28 -0700308 (state != kSVSBackwardBranch && state != kSVSSingleStep)) {
Jeff Hao97319a82009-08-12 16:57:15 -0700309 shadowSpace->selfVerificationState = kSVSDebugInterp;
310 }
311
312 /* Check that the current pc is the end of the trace */
Ben Chengd5adae12010-03-26 17:45:28 -0700313 if ((state == kSVSDebugInterp || state == kSVSSingleStep) &&
314 pc == shadowSpace->endPC) {
Jeff Hao97319a82009-08-12 16:57:15 -0700315
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 Chengccd6c012009-10-15 14:52:45 -0700323 LOGD("~~~ DbgIntp(%d): REGISTERS DIVERGENCE!", self->threadId);
Jeff Hao97319a82009-08-12 16:57:15 -0700324 selfVerificationDumpState(pc, self);
325 selfVerificationDumpTrace(pc, self);
326 LOGD("*** Interp Registers: addr: 0x%x bytes: %d",
327 (int)shadowSpace->fp, frameBytes);
Ben Chengccd6c012009-10-15 14:52:45 -0700328 selfVerificationPrintRegisters((int*)shadowSpace->fp,
329 (int*)shadowSpace->shadowFP,
330 frameBytes/4);
Jeff Hao97319a82009-08-12 16:57:15 -0700331 LOGD("*** Shadow Registers: addr: 0x%x bytes: %d",
332 (int)shadowSpace->shadowFP, frameBytes);
333 selfVerificationPrintRegisters((int*)shadowSpace->shadowFP,
Ben Chengccd6c012009-10-15 14:52:45 -0700334 (int*)shadowSpace->fp,
335 frameBytes/4);
336 selfVerificationSpinLoop(shadowSpace);
Jeff Hao97319a82009-08-12 16:57:15 -0700337 }
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 Chengccd6c012009-10-15 14:52:45 -0700347 LOGD("~~~ DbgIntp(%d): REGISTERS (FRAME2) DIVERGENCE!",
Jeff Hao97319a82009-08-12 16:57:15 -0700348 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 Chengccd6c012009-10-15 14:52:45 -0700354 (int*)shadowSpace->endShadowFP,
355 (frameBytes2+localRegs)/4);
Jeff Hao97319a82009-08-12 16:57:15 -0700356 LOGD("*** Shadow Registers: addr: 0x%x l: %d bytes: %d",
357 (int)shadowSpace->endShadowFP, localRegs, frameBytes2);
358 selfVerificationPrintRegisters((int*)shadowSpace->endShadowFP,
Ben Chengccd6c012009-10-15 14:52:45 -0700359 (int*)self->curFrame,
360 (frameBytes2+localRegs)/4);
361 selfVerificationSpinLoop(shadowSpace);
Jeff Hao97319a82009-08-12 16:57:15 -0700362 }
363 }
364
365 /* Check memory space */
Ben Chengbcdc1de2009-08-21 16:18:46 -0700366 bool memDiff = false;
Jeff Hao97319a82009-08-12 16:57:15 -0700367 ShadowHeap* heapSpacePtr;
368 for (heapSpacePtr = shadowSpace->heapSpace;
369 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
Ben Chengbcdc1de2009-08-21 16:18:46 -0700370 int memData = *((unsigned int*) heapSpacePtr->addr);
371 if (heapSpacePtr->data != memData) {
Ben Chengccd6c012009-10-15 14:52:45 -0700372 LOGD("~~~ DbgIntp(%d): MEMORY DIVERGENCE!", self->threadId);
373 LOGD("Addr: 0x%x Intrp Data: 0x%x Jit Data: 0x%x",
Ben Chengbcdc1de2009-08-21 16:18:46 -0700374 heapSpacePtr->addr, memData, heapSpacePtr->data);
Jeff Hao97319a82009-08-12 16:57:15 -0700375 selfVerificationDumpState(pc, self);
376 selfVerificationDumpTrace(pc, self);
Ben Chengbcdc1de2009-08-21 16:18:46 -0700377 memDiff = true;
Jeff Hao97319a82009-08-12 16:57:15 -0700378 }
379 }
Ben Chengccd6c012009-10-15 14:52:45 -0700380 if (memDiff) selfVerificationSpinLoop(shadowSpace);
Ben Chengd5adae12010-03-26 17:45:28 -0700381
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 Hao97319a82009-08-12 16:57:15 -0700389 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 Chengccd6c012009-10-15 14:52:45 -0700394 LOGD("startPC: 0x%x endPC: 0x%x currPC: 0x%x",
Jeff Hao97319a82009-08-12 16:57:15 -0700395 (int)shadowSpace->startPC, (int)shadowSpace->endPC, (int)pc);
396 selfVerificationDumpState(pc, self);
397 selfVerificationDumpTrace(pc, self);
Ben Chengccd6c012009-10-15 14:52:45 -0700398 selfVerificationSpinLoop(shadowSpace);
Jeff Hao97319a82009-08-12 16:57:15 -0700399
400 return true;
401 }
Ben Chengbcdc1de2009-08-21 16:18:46 -0700402 /* Log the instruction address and decoded instruction for debug */
Jeff Hao97319a82009-08-12 16:57:15 -0700403 shadowSpace->trace[shadowSpace->traceLength].addr = (int)pc;
Ben Chengbcdc1de2009-08-21 16:18:46 -0700404 shadowSpace->trace[shadowSpace->traceLength].decInsn = decInsn;
Jeff Hao97319a82009-08-12 16:57:15 -0700405 shadowSpace->traceLength++;
406
407 return false;
408}
409#endif
410
Ben Chengba4fc8b2009-06-01 13:00:29 -0700411/*
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 */
415void 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 Buzbeeb1d80442009-12-17 14:55:21 -0800428 gDvmJit.pProfTable = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700429}
430
Ben Cheng978738d2010-05-13 13:45:57 -0700431#if defined(WITH_JIT_TUNING)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700432/* Convenience function to increment counter from assembly code */
Ben Cheng6c10a972009-10-29 14:39:18 -0700433void dvmBumpNoChain(int from)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700434{
Ben Cheng6c10a972009-10-29 14:39:18 -0700435 gDvmJit.noChainExit[from]++;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700436}
437
438/* Convenience function to increment counter from assembly code */
439void dvmBumpNormal()
440{
Ben Cheng6c10a972009-10-29 14:39:18 -0700441 gDvmJit.normalExit++;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700442}
443
444/* Convenience function to increment counter from assembly code */
445void dvmBumpPunt(int from)
446{
Ben Cheng6c10a972009-10-29 14:39:18 -0700447 gDvmJit.puntExit++;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700448}
449#endif
450
451/* Dumps debugging & tuning stats to the log */
452void dvmJitStats()
453{
454 int i;
455 int hit;
456 int not_hit;
457 int chains;
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800458 int stubs;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700459 if (gDvmJit.pJitEntryTable) {
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800460 for (i=0, stubs=chains=hit=not_hit=0;
Bill Buzbee27176222009-06-09 09:20:16 -0700461 i < (int) gDvmJit.jitTableSize;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700462 i++) {
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800463 if (gDvmJit.pJitEntryTable[i].dPC != 0) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700464 hit++;
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800465 if (gDvmJit.pJitEntryTable[i].codeAddress ==
Bill Buzbeebd047242010-05-13 13:02:53 -0700466 dvmCompilerGetInterpretTemplate())
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800467 stubs++;
468 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -0700469 not_hit++;
Bill Buzbee716f1202009-07-23 13:22:09 -0700470 if (gDvmJit.pJitEntryTable[i].u.info.chain != gDvmJit.jitTableSize)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700471 chains++;
472 }
Ben Cheng72621c92010-03-10 13:12:55 -0800473 LOGD("JIT: table size is %d, entries used is %d",
Ben Cheng86717f72010-03-05 15:27:21 -0800474 gDvmJit.jitTableSize, gDvmJit.jitTableEntriesUsed);
Ben Cheng72621c92010-03-10 13:12:55 -0800475 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 Cheng86717f72010-03-05 15:27:21 -0800478
Ben Cheng978738d2010-05-13 13:45:57 -0700479#if defined(WITH_JIT_TUNING)
480 LOGD("JIT: Code cache patches: %d", gDvmJit.codeCachePatches);
481
Ben Cheng72621c92010-03-10 13:12:55 -0800482 LOGD("JIT: Lookups: %d hits, %d misses; %d normal, %d punt",
483 gDvmJit.addrLookupsFound, gDvmJit.addrLookupsNotFound,
484 gDvmJit.normalExit, gDvmJit.puntExit);
Ben Cheng452efba2010-04-30 15:14:00 -0700485
Ben Cheng978738d2010-05-13 13:45:57 -0700486 LOGD("JIT: ICHits: %d", gDvmICHitCount);
487
Ben Cheng72621c92010-03-10 13:12:55 -0800488 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 Cheng86717f72010-03-05 15:27:21 -0800493
Ben Chengb88ec3c2010-05-17 12:50:33 -0700494 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 Cheng452efba2010-04-30 15:14:00 -0700498 gDvmJit.icPatchDropped);
499
Ben Cheng86717f72010-03-05 15:27:21 -0800500 LOGD("JIT: Invoke: %d mono, %d poly, %d native, %d return",
501 gDvmJit.invokeMonomorphic, gDvmJit.invokePolymorphic,
502 gDvmJit.invokeNative, gDvmJit.returnOp);
Ben Cheng7a2697d2010-06-07 13:44:23 -0700503 LOGD("JIT: Inline: %d mgetter, %d msetter, %d pgetter, %d psetter",
504 gDvmJit.invokeMonoGetterInlined, gDvmJit.invokeMonoSetterInlined,
505 gDvmJit.invokePolyGetterInlined, gDvmJit.invokePolySetterInlined);
Ben Cheng86717f72010-03-05 15:27:21 -0800506 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 Chengba4fc8b2009-06-01 13:00:29 -0700509#endif
Ben Cheng86717f72010-03-05 15:27:21 -0800510
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800511 LOGD("JIT: %d Translation chains, %d interp stubs",
512 gDvmJit.translationChains, stubs);
Ben Chenge80cd942009-07-17 15:54:23 -0700513 if (gDvmJit.profile) {
Bill Buzbee716f1202009-07-23 13:22:09 -0700514 dvmCompilerSortAndPrintTraceProfiles();
Bill Buzbee6e963e12009-06-17 16:56:19 -0700515 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700516 }
517}
518
Bill Buzbee716f1202009-07-23 13:22:09 -0700519
Andy McFadden953a0ed2010-09-17 15:48:38 -0700520static void setTraceConstruction(JitEntry *slot, bool value)
Bill Buzbeed7269912009-11-10 14:31:32 -0800521{
522
523 JitEntryInfoUnion oldValue;
524 JitEntryInfoUnion newValue;
525 do {
526 oldValue = slot->u;
527 newValue = oldValue;
528 newValue.info.traceConstruction = value;
Andy McFadden6e10b9a2010-06-14 15:24:39 -0700529 } while (android_atomic_release_cas(oldValue.infoWord, newValue.infoWord,
530 &slot->u.infoWord) != 0);
Bill Buzbeed7269912009-11-10 14:31:32 -0800531}
532
Andy McFadden953a0ed2010-09-17 15:48:38 -0700533static void resetTracehead(InterpState* interpState, JitEntry *slot)
Bill Buzbeed7269912009-11-10 14:31:32 -0800534{
Bill Buzbeebd047242010-05-13 13:02:53 -0700535 slot->codeAddress = dvmCompilerGetInterpretTemplate();
Bill Buzbeed7269912009-11-10 14:31:32 -0800536 setTraceConstruction(slot, false);
537}
538
539/* Clean up any pending trace builds */
540void dvmJitAbortTraceSelect(InterpState* interpState)
541{
542 if (interpState->jitState == kJitTSelect)
Ben Chenga4973592010-03-31 11:59:18 -0700543 interpState->jitState = kJitDone;
Bill Buzbeed7269912009-11-10 14:31:32 -0800544}
545
Ben Chengba4fc8b2009-06-01 13:00:29 -0700546/*
Bill Buzbee964a7b02010-01-28 12:54:19 -0800547 * Find an entry in the JitTable, creating if necessary.
548 * Returns null if table is full.
549 */
550static 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 McFadden6e10b9a2010-06-14 15:24:39 -0700576 ANDROID_MEMBAR_FULL(); /* Make sure we reload [].dPC after lock */
Bill Buzbee964a7b02010-01-28 12:54:19 -0800577 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 McFadden6e10b9a2010-06-14 15:24:39 -0700613 } while (android_atomic_release_cas(oldValue.infoWord,
614 newValue.infoWord,
615 &gDvmJit.pJitEntryTable[prev].u.infoWord) != 0);
Bill Buzbee964a7b02010-01-28 12:54:19 -0800616 }
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 Chenga4973592010-03-31 11:59:18 -0700634
Bill Buzbee964a7b02010-01-28 12:54:19 -0800635/*
Ben Cheng7a2697d2010-06-07 13:44:23 -0700636 * 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 */
642static 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 Chengd44faf52010-06-02 15:33:51 -0700654 * Check if the next instruction following the invoke is a move-result and if
Ben Cheng7a2697d2010-06-07 13:44:23 -0700655 * 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 Chengd44faf52010-06-02 15:33:51 -0700662 *
663 * lastPC, len, offset are all from the preceding invoke instruction
664 */
665static 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 Cheng7a2697d2010-06-07 13:44:23 -0700684 interpState->trace[currTraceRun].frag.isCode = true;
Ben Chengd44faf52010-06-02 15:33:51 -0700685 interpState->totalTraceLen++;
686
687 interpState->currRunLen = dexGetInstrOrTableWidthAbs(gDvm.instrWidth,
688 moveResultPC);
689}
690
691/*
Ben Chengba4fc8b2009-06-01 13:00:29 -0700692 * 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 Cheng7a2697d2010-06-07 13:44:23 -0700707int dvmCheckJit(const u2* pc, Thread* self, InterpState* interpState,
708 const ClassObject* thisClass, const Method* curMethod)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700709{
Carl Shapiroe3c01da2010-05-20 22:54:18 -0700710 int flags, len;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700711 int switchInterp = false;
Ben Chenga4973592010-03-31 11:59:18 -0700712 bool debugOrProfile = dvmDebuggerOrProfilerActive();
Ben Cheng7a2697d2010-06-07 13:44:23 -0700713 /* Stay in the dbg interpreter for the next instruction */
714 bool stayOneMoreInst = false;
Bill Buzbeed7269912009-11-10 14:31:32 -0800715
Ben Cheng1c52e6d2010-07-02 13:00:39 -0700716 /*
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 Cheng79d173c2009-09-29 16:12:51 -0700724 /* Prepare to handle last PC and stage the current PC */
725 const u2 *lastPC = interpState->lastPC;
726 interpState->lastPC = pc;
727
Ben Chengba4fc8b2009-06-01 13:00:29 -0700728 switch (interpState->jitState) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700729 int offset;
730 DecodedInstruction decInsn;
731 case kJitTSelect:
Ben Chengdc84bb22009-10-02 12:58:52 -0700732 /* First instruction - just remember the PC and exit */
733 if (lastPC == NULL) break;
Ben Cheng79d173c2009-09-29 16:12:51 -0700734 /* Grow the trace around the last PC if jitState is kJitTSelect */
735 dexDecodeInstruction(gDvm.instrFormat, lastPC, &decInsn);
Ben Cheng6c10a972009-10-29 14:39:18 -0700736
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 Buzbeef9f33282009-11-22 12:45:30 -0800749
Ben Chengba4fc8b2009-06-01 13:00:29 -0700750#if defined(SHOW_TRACE)
Andy McFaddenc6b25c72010-06-22 11:01:20 -0700751 LOGD("TraceGen: adding %s", dexGetOpcodeName(decInsn.opCode));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700752#endif
753 flags = dexGetInstrFlags(gDvm.instrFlags, decInsn.opCode);
Ben Cheng79d173c2009-09-29 16:12:51 -0700754 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 Buzbee50a6bf22009-07-08 13:08:04 -0700759 int currTraceRun;
760 /* We need to start a new trace run */
761 currTraceRun = ++interpState->currTraceRun;
762 interpState->currRunLen = 0;
Ben Cheng79d173c2009-09-29 16:12:51 -0700763 interpState->currRunHead = (u2*)lastPC;
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700764 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 Cheng7a2697d2010-06-07 13:44:23 -0700768 interpState->trace[currTraceRun].frag.isCode = true;
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700769 }
770 interpState->trace[interpState->currTraceRun].frag.numInsts++;
771 interpState->totalTraceLen++;
772 interpState->currRunLen += len;
Ben Cheng79d173c2009-09-29 16:12:51 -0700773
Ben Chengd44faf52010-06-02 15:33:51 -0700774 /*
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 Cheng79d173c2009-09-29 16:12:51 -0700780 /* Will probably never hit this with the current trace buildier */
Ben Chengd44faf52010-06-02 15:33:51 -0700781 if (interpState->currTraceRun ==
782 (MAX_JIT_RUN_LEN - 1 - needReservedRun)) {
Ben Cheng79d173c2009-09-29 16:12:51 -0700783 interpState->jitState = kJitTSelectEnd;
784 }
785
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700786 if ( ((flags & kInstrUnconditional) == 0) &&
Bill Buzbeef4ce16f2009-07-28 13:28:25 -0700787 /* don't end trace on INVOKE_DIRECT_EMPTY */
788 (decInsn.opCode != OP_INVOKE_DIRECT_EMPTY) &&
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700789 ((flags & (kInstrCanBranch |
790 kInstrCanSwitch |
791 kInstrCanReturn |
792 kInstrInvoke)) != 0)) {
793 interpState->jitState = kJitTSelectEnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700794#if defined(SHOW_TRACE)
Ben Chengd44faf52010-06-02 15:33:51 -0700795 LOGD("TraceGen: ending on %s, basic block end",
Andy McFaddenc6b25c72010-06-22 11:01:20 -0700796 dexGetOpcodeName(decInsn.opCode));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700797#endif
Ben Chengd44faf52010-06-02 15:33:51 -0700798
799 /*
Ben Cheng7a2697d2010-06-07 13:44:23 -0700800 * If the current invoke is a {virtual,interface}, get the
801 * current class/method pair into the trace as well.
Ben Chengd44faf52010-06-02 15:33:51 -0700802 * If the next instruction is a variant of move-result, insert
Ben Cheng7a2697d2010-06-07 13:44:23 -0700803 * it to the trace too.
Ben Chengd44faf52010-06-02 15:33:51 -0700804 */
805 if (flags & kInstrInvoke) {
Ben Cheng7a2697d2010-06-07 13:44:23 -0700806 insertClassMethodInfo(interpState, thisClass, curMethod,
807 &decInsn);
Ben Chengd44faf52010-06-02 15:33:51 -0700808 insertMoveResult(lastPC, len, offset, interpState);
809 }
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700810 }
Bill Buzbee2ce8a6c2009-12-03 15:09:32 -0800811 /* Break on throw or self-loop */
Bill Buzbee324b3ac2009-12-04 13:17:36 -0800812 if ((decInsn.opCode == OP_THROW) || (lastPC == pc)){
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700813 interpState->jitState = kJitTSelectEnd;
814 }
815 if (interpState->totalTraceLen >= JIT_MAX_TRACE_LEN) {
816 interpState->jitState = kJitTSelectEnd;
817 }
Ben Chenga4973592010-03-31 11:59:18 -0700818 /* Abandon the trace request if debugger/profiler is attached */
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700819 if (debugOrProfile) {
Ben Chenga4973592010-03-31 11:59:18 -0700820 interpState->jitState = kJitDone;
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700821 break;
822 }
823 if ((flags & kInstrCanReturn) != kInstrCanReturn) {
824 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700825 }
Ben Cheng7a2697d2010-06-07 13:44:23 -0700826 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 Chengba4fc8b2009-06-01 13:00:29 -0700838 /* NOTE: intentional fallthrough for returns */
839 case kJitTSelectEnd:
840 {
Ben Chenga4973592010-03-31 11:59:18 -0700841 /* Bad trace */
Ben Chengba4fc8b2009-06-01 13:00:29 -0700842 if (interpState->totalTraceLen == 0) {
Bill Buzbeed7269912009-11-10 14:31:32 -0800843 /* Bad trace - mark as untranslatable */
Ben Chenga4973592010-03-31 11:59:18 -0700844 interpState->jitState = kJitDone;
845 switchInterp = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700846 break;
847 }
Ben Cheng7a2697d2010-06-07 13:44:23 -0700848
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 Chengba4fc8b2009-06-01 13:00:29 -0700863 JitTraceDescription* desc =
864 (JitTraceDescription*)malloc(sizeof(JitTraceDescription) +
865 sizeof(JitTraceRun) * (interpState->currTraceRun+1));
Ben Cheng7a2697d2010-06-07 13:44:23 -0700866
Ben Chengba4fc8b2009-06-01 13:00:29 -0700867 if (desc == NULL) {
868 LOGE("Out of memory in trace selection");
869 dvmJitStopTranslationRequests();
Ben Chenga4973592010-03-31 11:59:18 -0700870 interpState->jitState = kJitDone;
871 switchInterp = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700872 break;
873 }
Ben Cheng7a2697d2010-06-07 13:44:23 -0700874
Ben Chengba4fc8b2009-06-01 13:00:29 -0700875 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 Buzbee964a7b02010-01-28 12:54:19 -0800882 if (dvmCompilerWorkEnqueue(
883 interpState->currTraceHead,kWorkOrderTrace,desc)) {
884 /* Work order successfully enqueued */
885 if (gDvmJit.blockingMode) {
886 dvmCompilerDrainQueue();
887 }
Ben Cheng1357e942010-02-10 17:21:39 -0800888 } else {
889 /*
890 * Make sure the descriptor for the abandoned work order is
891 * freed.
892 */
893 free(desc);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700894 }
Bill Buzbee964a7b02010-01-28 12:54:19 -0800895 /*
896 * Reset "trace in progress" flag whether or not we
897 * successfully entered a work order.
898 */
Ben Cheng6999d842010-01-26 16:46:15 -0800899 JitEntry *jitEntry =
900 lookupAndAdd(interpState->currTraceHead, false);
901 if (jitEntry) {
902 setTraceConstruction(jitEntry, false);
903 }
Ben Chenga4973592010-03-31 11:59:18 -0700904 interpState->jitState = kJitDone;
905 switchInterp = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700906 }
907 break;
908 case kJitSingleStep:
909 interpState->jitState = kJitSingleStepEnd;
910 break;
911 case kJitSingleStepEnd:
912 interpState->entryPoint = kInterpEntryResume;
Ben Chenga4973592010-03-31 11:59:18 -0700913 interpState->jitState = kJitDone;
914 switchInterp = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700915 break;
Ben Chenga4973592010-03-31 11:59:18 -0700916 case kJitDone:
917 switchInterp = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700918 break;
Jeff Hao97319a82009-08-12 16:57:15 -0700919#if defined(WITH_SELF_VERIFICATION)
920 case kJitSelfVerification:
Ben Chengd5adae12010-03-26 17:45:28 -0700921 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 Chenga4973592010-03-31 11:59:18 -0700927 interpState->jitState = kJitDone;
928 switchInterp = true;
Ben Chengd5adae12010-03-26 17:45:28 -0700929 }
Jeff Hao97319a82009-08-12 16:57:15 -0700930 }
931 break;
932#endif
Ben Chenga4973592010-03-31 11:59:18 -0700933 case kJitNot:
Ben Cheng1c52e6d2010-07-02 13:00:39 -0700934 switchInterp = !debugOrProfile;
Ben Chenged79ff02009-10-13 13:26:40 -0700935 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700936 default:
Ben Chenga4973592010-03-31 11:59:18 -0700937 LOGE("Unexpected JIT state: %d entry point: %d",
938 interpState->jitState, interpState->entryPoint);
939 dvmAbort();
Ben Cheng9c147b82009-10-07 16:41:46 -0700940 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700941 }
Ben Chenga4973592010-03-31 11:59:18 -0700942 /*
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 Cheng7a2697d2010-06-07 13:44:23 -0700948 return switchInterp && !debugOrProfile && !stayOneMoreInst;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700949}
950
Ben Chengccd6c012009-10-15 14:52:45 -0700951JitEntry *dvmFindJitEntry(const u2* pc)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700952{
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 Buzbee27176222009-06-09 09:20:16 -0700959 int chainEndMarker = gDvmJit.jitTableSize;
Bill Buzbee716f1202009-07-23 13:22:09 -0700960 while (gDvmJit.pJitEntryTable[idx].u.info.chain != chainEndMarker) {
961 idx = gDvmJit.pJitEntryTable[idx].u.info.chain;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700962 if (gDvmJit.pJitEntryTable[idx].dPC == pc)
963 return &gDvmJit.pJitEntryTable[idx];
964 }
965 }
966 return NULL;
967}
968
Bill Buzbee27176222009-06-09 09:20:16 -0700969/*
Ben Chengba4fc8b2009-06-01 13:00:29 -0700970 * If a translated code address exists for the davik byte code
971 * pointer return it. This routine needs to be fast.
972 */
973void* dvmJitGetCodeAddr(const u2* dPC)
974{
975 int idx = dvmJitHash(dPC);
Ben Cheng6999d842010-01-26 16:46:15 -0800976 const u2* npc = gDvmJit.pJitEntryTable[idx].dPC;
Bill Buzbee9797a232010-01-12 12:20:13 -0800977 if (npc != NULL) {
Ben Cheng6999d842010-01-26 16:46:15 -0800978 bool hideTranslation = (gDvm.sumThreadSuspendCount != 0) ||
979 (gDvmJit.codeCacheFull == true) ||
980 (gDvmJit.pProfTable == NULL);
981
Bill Buzbee9797a232010-01-12 12:20:13 -0800982 if (npc == dPC) {
Ben Cheng978738d2010-05-13 13:45:57 -0700983#if defined(WITH_JIT_TUNING)
Bill Buzbee9797a232010-01-12 12:20:13 -0800984 gDvmJit.addrLookupsFound++;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700985#endif
Ben Cheng6999d842010-01-26 16:46:15 -0800986 return hideTranslation ?
987 NULL : gDvmJit.pJitEntryTable[idx].codeAddress;
Bill Buzbee9797a232010-01-12 12:20:13 -0800988 } 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 Cheng978738d2010-05-13 13:45:57 -0700993#if defined(WITH_JIT_TUNING)
Bill Buzbee9797a232010-01-12 12:20:13 -0800994 gDvmJit.addrLookupsFound++;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700995#endif
Ben Cheng6999d842010-01-26 16:46:15 -0800996 return hideTranslation ?
997 NULL : gDvmJit.pJitEntryTable[idx].codeAddress;
Bill Buzbee9797a232010-01-12 12:20:13 -0800998 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700999 }
1000 }
1001 }
Ben Cheng978738d2010-05-13 13:45:57 -07001002#if defined(WITH_JIT_TUNING)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001003 gDvmJit.addrLookupsNotFound++;
1004#endif
1005 return NULL;
1006}
1007
1008/*
1009 * Register the translated code pointer into the JitTable.
Bill Buzbee9a8c75a2009-11-08 14:31:20 -08001010 * NOTE: Once a codeAddress field transitions from initial state to
Ben Chengba4fc8b2009-06-01 13:00:29 -07001011 * JIT'd code, it must not be altered without first halting all
Bill Buzbee716f1202009-07-23 13:22:09 -07001012 * threads. This routine should only be called by the compiler
1013 * thread.
Ben Chengba4fc8b2009-06-01 13:00:29 -07001014 */
Bill Buzbee716f1202009-07-23 13:22:09 -07001015void dvmJitSetCodeAddr(const u2* dPC, void *nPC, JitInstructionSetType set) {
1016 JitEntryInfoUnion oldValue;
1017 JitEntryInfoUnion newValue;
Bill Buzbee964a7b02010-01-28 12:54:19 -08001018 JitEntry *jitEntry = lookupAndAdd(dPC, false);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001019 assert(jitEntry);
Bill Buzbee716f1202009-07-23 13:22:09 -07001020 /* Note: order of update is important */
1021 do {
1022 oldValue = jitEntry->u;
1023 newValue = oldValue;
1024 newValue.info.instructionSet = set;
Andy McFadden6e10b9a2010-06-14 15:24:39 -07001025 } while (android_atomic_release_cas(
1026 oldValue.infoWord, newValue.infoWord,
1027 &jitEntry->u.infoWord) != 0);
Bill Buzbee716f1202009-07-23 13:22:09 -07001028 jitEntry->codeAddress = nPC;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001029}
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 Chenga4973592010-03-31 11:59:18 -07001034 * otherwise.
Ben Chengba4fc8b2009-06-01 13:00:29 -07001035 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001036bool dvmJitCheckTraceRequest(Thread* self, InterpState* interpState)
1037{
Ben Chenga4973592010-03-31 11:59:18 -07001038 bool switchInterp = false; /* Assume success */
Bill Buzbee48f18242009-06-19 16:02:27 -07001039 int i;
buzbee852aacd2010-06-08 16:24:46 -07001040 /*
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 */
buzbeec35294d2010-06-09 14:22:50 -07001088 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 Chenga4973592010-03-31 11:59:18 -07001093 bool debugOrProfile = dvmDebuggerOrProfilerActive();
Ben Cheng40094c12010-02-24 20:58:44 -08001094
Ben Chenga4973592010-03-31 11:59:18 -07001095 /* 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 Cheng40094c12010-02-24 20:58:44 -08001100 /* Two-level filtering scheme */
1101 for (i=0; i< JIT_TRACE_THRESH_FILTER_SIZE; i++) {
1102 if (filterKey == interpState->threshFilter[i]) {
buzbee852aacd2010-06-08 16:24:46 -07001103 interpState->threshFilter[i] = 0; // Reset filter entry
Ben Cheng40094c12010-02-24 20:58:44 -08001104 break;
1105 }
Bill Buzbee48f18242009-06-19 16:02:27 -07001106 }
Ben Cheng40094c12010-02-24 20:58:44 -08001107 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 Chenga4973592010-03-31 11:59:18 -07001115 interpState->jitState = kJitDone;
Ben Cheng40094c12010-02-24 20:58:44 -08001116 }
Ben Chenga4973592010-03-31 11:59:18 -07001117 }
Bill Buzbeed7269912009-11-10 14:31:32 -08001118
Ben Chenga4973592010-03-31 11:59:18 -07001119 /* If the compiler is backlogged, cancel any JIT actions */
1120 if (gDvmJit.compilerQueueLength >= gDvmJit.compilerHighWater) {
1121 interpState->jitState = kJitDone;
Ben Cheng40094c12010-02-24 20:58:44 -08001122 }
Bill Buzbeed7269912009-11-10 14:31:32 -08001123
Ben Chengba4fc8b2009-06-01 13:00:29 -07001124 /*
Ben Chenga4973592010-03-31 11:59:18 -07001125 * Check for additional reasons that might force the trace select
1126 * request to be dropped
Ben Chengba4fc8b2009-06-01 13:00:29 -07001127 */
Ben Chenga4973592010-03-31 11:59:18 -07001128 if (interpState->jitState == kJitTSelectRequest ||
1129 interpState->jitState == kJitTSelectRequestHot) {
Bill Buzbee964a7b02010-01-28 12:54:19 -08001130 JitEntry *slot = lookupAndAdd(interpState->pc, false);
Bill Buzbee716f1202009-07-23 13:22:09 -07001131 if (slot == NULL) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001132 /*
Bill Buzbee716f1202009-07-23 13:22:09 -07001133 * 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 Chengba4fc8b2009-06-01 13:00:29 -07001137 */
Ben Chenga4973592010-03-31 11:59:18 -07001138 interpState->jitState = kJitDone;
Bill Buzbee716f1202009-07-23 13:22:09 -07001139 LOGD("JIT: JitTable full, disabling profiling");
1140 dvmJitStopTranslationRequests();
Bill Buzbeed7269912009-11-10 14:31:32 -08001141 } else if (slot->u.info.traceConstruction) {
1142 /*
Ben Cheng60c24f42010-01-04 12:29:56 -08001143 * Trace request already in progress, but most likely it
Bill Buzbeed7269912009-11-10 14:31:32 -08001144 * 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 Chenga4973592010-03-31 11:59:18 -07001152 interpState->jitState = kJitDone;
Bill Buzbeed7269912009-11-10 14:31:32 -08001153 resetTracehead(interpState, slot);
1154 } else if (slot->codeAddress) {
1155 /* Nothing to do here - just return */
Ben Chenga4973592010-03-31 11:59:18 -07001156 interpState->jitState = kJitDone;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001157 } else {
Bill Buzbeed7269912009-11-10 14:31:32 -08001158 /*
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 Chengba4fc8b2009-06-01 13:00:29 -07001167 }
1168 }
Ben Chenga4973592010-03-31 11:59:18 -07001169
Ben Chengba4fc8b2009-06-01 13:00:29 -07001170 switch (interpState->jitState) {
1171 case kJitTSelectRequest:
Ben Cheng40094c12010-02-24 20:58:44 -08001172 case kJitTSelectRequestHot:
Ben Chenga4973592010-03-31 11:59:18 -07001173 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 Cheng7a2697d2010-06-07 13:44:23 -07001184 interpState->trace[0].frag.isCode = true;
Ben Chenga4973592010-03-31 11:59:18 -07001185 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 Chengba4fc8b2009-06-01 13:00:29 -07001193 break;
1194 default:
Ben Chenga4973592010-03-31 11:59:18 -07001195 LOGE("Unexpected JIT state: %d entry point: %d",
1196 interpState->jitState, interpState->entryPoint);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001197 dvmAbort();
1198 }
Ben Chenga4973592010-03-31 11:59:18 -07001199 } else {
1200 /*
1201 * Cannot build trace this time - ready to leave the dbg interpreter
1202 */
1203 interpState->jitState = kJitDone;
1204 switchInterp = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001205 }
Ben Chenga4973592010-03-31 11:59:18 -07001206
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 Chengba4fc8b2009-06-01 13:00:29 -07001213}
1214
Bill Buzbee27176222009-06-09 09:20:16 -07001215/*
1216 * Resizes the JitTable. Must be a power of 2, and returns true on failure.
Bill Buzbee964a7b02010-01-28 12:54:19 -08001217 * Stops all threads, and thus is a heavyweight operation. May only be called
1218 * by the compiler thread.
Bill Buzbee27176222009-06-09 09:20:16 -07001219 */
1220bool dvmJitResizeJitTable( unsigned int size )
1221{
Bill Buzbee716f1202009-07-23 13:22:09 -07001222 JitEntry *pNewTable;
1223 JitEntry *pOldTable;
Bill Buzbee964a7b02010-01-28 12:54:19 -08001224 JitEntry tempEntry;
Bill Buzbee27176222009-06-09 09:20:16 -07001225 u4 newMask;
Bill Buzbee716f1202009-07-23 13:22:09 -07001226 unsigned int oldSize;
Bill Buzbee27176222009-06-09 09:20:16 -07001227 unsigned int i;
1228
Ben Cheng3f02aa42009-08-14 13:52:09 -07001229 assert(gDvmJit.pJitEntryTable != NULL);
Bill Buzbee27176222009-06-09 09:20:16 -07001230 assert(size && !(size & (size - 1))); /* Is power of 2? */
1231
Ben Chenga4973592010-03-31 11:59:18 -07001232 LOGI("Jit: resizing JitTable from %d to %d", gDvmJit.jitTableSize, size);
Bill Buzbee27176222009-06-09 09:20:16 -07001233
1234 newMask = size - 1;
1235
1236 if (size <= gDvmJit.jitTableSize) {
1237 return true;
1238 }
1239
Bill Buzbee964a7b02010-01-28 12:54:19 -08001240 /* 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 Buzbee716f1202009-07-23 13:22:09 -07001247 pNewTable = (JitEntry*)calloc(size, sizeof(*pNewTable));
Bill Buzbee27176222009-06-09 09:20:16 -07001248 if (pNewTable == NULL) {
1249 return true;
1250 }
1251 for (i=0; i< size; i++) {
Bill Buzbee716f1202009-07-23 13:22:09 -07001252 pNewTable[i].u.info.chain = size; /* Initialize chain termination */
Bill Buzbee27176222009-06-09 09:20:16 -07001253 }
1254
1255 /* Stop all other interpreting/jit'ng threads */
Ben Chenga8e64a72009-10-20 13:01:36 -07001256 dvmSuspendAllThreads(SUSPEND_FOR_TBL_RESIZE);
Bill Buzbee27176222009-06-09 09:20:16 -07001257
Bill Buzbee716f1202009-07-23 13:22:09 -07001258 pOldTable = gDvmJit.pJitEntryTable;
1259 oldSize = gDvmJit.jitTableSize;
Bill Buzbee27176222009-06-09 09:20:16 -07001260
1261 dvmLockMutex(&gDvmJit.tableLock);
Bill Buzbee27176222009-06-09 09:20:16 -07001262 gDvmJit.pJitEntryTable = pNewTable;
1263 gDvmJit.jitTableSize = size;
1264 gDvmJit.jitTableMask = size - 1;
Bill Buzbee716f1202009-07-23 13:22:09 -07001265 gDvmJit.jitTableEntriesUsed = 0;
Bill Buzbee27176222009-06-09 09:20:16 -07001266
Bill Buzbee716f1202009-07-23 13:22:09 -07001267 for (i=0; i < oldSize; i++) {
1268 if (pOldTable[i].dPC) {
1269 JitEntry *p;
1270 u2 chain;
Bill Buzbee964a7b02010-01-28 12:54:19 -08001271 p = lookupAndAdd(pOldTable[i].dPC, true /* holds tableLock*/ );
1272 p->codeAddress = pOldTable[i].codeAddress;
Bill Buzbee716f1202009-07-23 13:22:09 -07001273 /* We need to preserve the new chain field, but copy the rest */
Bill Buzbee716f1202009-07-23 13:22:09 -07001274 chain = p->u.info.chain;
1275 p->u = pOldTable[i].u;
1276 p->u.info.chain = chain;
Bill Buzbee716f1202009-07-23 13:22:09 -07001277 }
1278 }
Bill Buzbee964a7b02010-01-28 12:54:19 -08001279 dvmUnlockMutex(&gDvmJit.tableLock);
Bill Buzbee716f1202009-07-23 13:22:09 -07001280
1281 free(pOldTable);
1282
Bill Buzbee27176222009-06-09 09:20:16 -07001283 /* Restart the world */
Ben Chenga8e64a72009-10-20 13:01:36 -07001284 dvmResumeAllThreads(SUSPEND_FOR_TBL_RESIZE);
Bill Buzbee27176222009-06-09 09:20:16 -07001285
1286 return false;
1287}
1288
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001289/*
Ben Cheng60c24f42010-01-04 12:29:56 -08001290 * Reset the JitTable to the initial clean state.
1291 */
1292void 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 Buzbee50a6bf22009-07-08 13:08:04 -07001308 * Float/double conversion requires clamping to min and max of integer form. If
1309 * target doesn't support this normally, use these.
1310 */
1311s8 dvmJitd2l(double d)
1312{
Bill Buzbee9727c3d2009-08-01 11:32:36 -07001313 static const double kMaxLong = (double)(s8)0x7fffffffffffffffULL;
1314 static const double kMinLong = (double)(s8)0x8000000000000000ULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001315 if (d >= kMaxLong)
Bill Buzbee9727c3d2009-08-01 11:32:36 -07001316 return (s8)0x7fffffffffffffffULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001317 else if (d <= kMinLong)
Bill Buzbee9727c3d2009-08-01 11:32:36 -07001318 return (s8)0x8000000000000000ULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001319 else if (d != d) // NaN case
1320 return 0;
1321 else
1322 return (s8)d;
1323}
1324
1325s8 dvmJitf2l(float f)
1326{
Bill Buzbee9727c3d2009-08-01 11:32:36 -07001327 static const float kMaxLong = (float)(s8)0x7fffffffffffffffULL;
1328 static const float kMinLong = (float)(s8)0x8000000000000000ULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001329 if (f >= kMaxLong)
Bill Buzbee9727c3d2009-08-01 11:32:36 -07001330 return (s8)0x7fffffffffffffffULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001331 else if (f <= kMinLong)
Bill Buzbee9727c3d2009-08-01 11:32:36 -07001332 return (s8)0x8000000000000000ULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001333 else if (f != f) // NaN case
1334 return 0;
1335 else
1336 return (s8)f;
1337}
1338
Ben Chengba4fc8b2009-06-01 13:00:29 -07001339#endif /* WITH_JIT */