blob: d9b9caa009d5117e54c1621118825a59f3aa984f [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
26#include "dexdump/OpCodeNames.h"
27#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;
Jeff Hao97319a82009-08-12 16:57:15 -070067 int preBytes = interpState->method->outsSize*4 + sizeof(StackSaveArea);
68 int postBytes = interpState->method->registersSize*4;
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 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
81 // Dynamically grow shadow register space if necessary
82 while (preBytes + postBytes > shadowSpace->registerSpaceSize) {
83 shadowSpace->registerSpaceSize *= 2;
84 free(shadowSpace->registerSpace);
85 shadowSpace->registerSpace =
86 (int*) calloc(shadowSpace->registerSpaceSize, sizeof(int));
87 }
88
89 // Remember original state
90 shadowSpace->startPC = pc;
91 shadowSpace->fp = fp;
Ben Chengccd6c012009-10-15 14:52:45 -070092 shadowSpace->glue = interpState;
93 /*
94 * Store the original method here in case the trace ends with a
95 * return/invoke, the last method.
96 */
97 shadowSpace->method = interpState->method;
Jeff Hao97319a82009-08-12 16:57:15 -070098 shadowSpace->shadowFP = shadowSpace->registerSpace +
99 shadowSpace->registerSpaceSize - postBytes/4;
100
101 // Create a copy of the InterpState
Ben Chengccd6c012009-10-15 14:52:45 -0700102 //shadowSpace->interpState = *interpState;
103 memcpy(&(shadowSpace->interpState), interpState, sizeof(InterpState));
Jeff Hao97319a82009-08-12 16:57:15 -0700104 shadowSpace->interpState.fp = shadowSpace->shadowFP;
105 shadowSpace->interpState.interpStackEnd = (u1*)shadowSpace->registerSpace;
106
107 // Create a copy of the stack
108 memcpy(((char*)shadowSpace->shadowFP)-preBytes, ((char*)fp)-preBytes,
109 preBytes+postBytes);
110
111 // Setup the shadowed heap space
112 shadowSpace->heapSpaceTail = shadowSpace->heapSpace;
113
114 // Reset trace length
115 shadowSpace->traceLength = 0;
116
117 return shadowSpace;
118}
119
120/*
121 * Save ending PC, FP and compiled code exit point to shadow space.
122 * Return a pointer to the shadow space for JIT to restore state.
123 */
124void* dvmSelfVerificationRestoreState(const u2* pc, const void* fp,
125 SelfVerificationState exitPoint)
126{
127 Thread *self = dvmThreadSelf();
128 ShadowSpace *shadowSpace = self->shadowSpace;
129 shadowSpace->endPC = pc;
130 shadowSpace->endShadowFP = fp;
131
132 //LOGD("### selfVerificationRestoreState(%d) pc: 0x%x fp: 0x%x endPC: 0x%x",
133 // self->threadId, (int)shadowSpace->startPC, (int)shadowSpace->fp,
134 // (int)pc);
135
136 if (shadowSpace->selfVerificationState != kSVSStart) {
137 LOGD("~~~ Restore: INCORRECT PREVIOUS STATE(%d): %d",
138 self->threadId, shadowSpace->selfVerificationState);
139 LOGD("********** SHADOW STATE DUMP **********");
Ben Chengccd6c012009-10-15 14:52:45 -0700140 LOGD("Dalvik PC: 0x%x endPC: 0x%x", (int)shadowSpace->startPC,
Jeff Hao97319a82009-08-12 16:57:15 -0700141 (int)shadowSpace->endPC);
Ben Chengccd6c012009-10-15 14:52:45 -0700142 LOGD("Interp FP: 0x%x", (int)shadowSpace->fp);
143 LOGD("Shadow FP: 0x%x endFP: 0x%x", (int)shadowSpace->shadowFP,
Jeff Hao97319a82009-08-12 16:57:15 -0700144 (int)shadowSpace->endShadowFP);
145 }
146
147 // Special case when punting after a single instruction
148 if (exitPoint == kSVSPunt && pc == shadowSpace->startPC) {
149 shadowSpace->selfVerificationState = kSVSIdle;
150 } else {
151 shadowSpace->selfVerificationState = exitPoint;
152 }
153
154 return shadowSpace;
155}
156
157/* Print contents of virtual registers */
Ben Chengccd6c012009-10-15 14:52:45 -0700158static void selfVerificationPrintRegisters(int* addr, int* addrRef,
159 int numWords)
Jeff Hao97319a82009-08-12 16:57:15 -0700160{
161 int i;
162 for (i = 0; i < numWords; i++) {
Ben Chengccd6c012009-10-15 14:52:45 -0700163 LOGD("(v%d) 0x%8x%s", i, addr[i], addr[i] != addrRef[i] ? " X" : "");
Jeff Hao97319a82009-08-12 16:57:15 -0700164 }
165}
166
167/* Print values maintained in shadowSpace */
168static void selfVerificationDumpState(const u2* pc, Thread* self)
169{
170 ShadowSpace* shadowSpace = self->shadowSpace;
171 StackSaveArea* stackSave = SAVEAREA_FROM_FP(self->curFrame);
172 int frameBytes = (int) shadowSpace->registerSpace +
173 shadowSpace->registerSpaceSize*4 -
174 (int) shadowSpace->shadowFP;
175 int localRegs = 0;
176 int frameBytes2 = 0;
177 if (self->curFrame < shadowSpace->fp) {
178 localRegs = (stackSave->method->registersSize -
179 stackSave->method->insSize)*4;
180 frameBytes2 = (int) shadowSpace->fp - (int) self->curFrame - localRegs;
181 }
182 LOGD("********** SHADOW STATE DUMP **********");
Ben Chengccd6c012009-10-15 14:52:45 -0700183 LOGD("CurrentPC: 0x%x, Offset: 0x%04x", (int)pc,
Jeff Hao97319a82009-08-12 16:57:15 -0700184 (int)(pc - stackSave->method->insns));
Ben Chengccd6c012009-10-15 14:52:45 -0700185 LOGD("Class: %s", shadowSpace->method->clazz->descriptor);
186 LOGD("Method: %s", shadowSpace->method->name);
187 LOGD("Dalvik PC: 0x%x endPC: 0x%x", (int)shadowSpace->startPC,
Jeff Hao97319a82009-08-12 16:57:15 -0700188 (int)shadowSpace->endPC);
Ben Chengccd6c012009-10-15 14:52:45 -0700189 LOGD("Interp FP: 0x%x endFP: 0x%x", (int)shadowSpace->fp,
Jeff Hao97319a82009-08-12 16:57:15 -0700190 (int)self->curFrame);
Ben Chengccd6c012009-10-15 14:52:45 -0700191 LOGD("Shadow FP: 0x%x endFP: 0x%x", (int)shadowSpace->shadowFP,
Jeff Hao97319a82009-08-12 16:57:15 -0700192 (int)shadowSpace->endShadowFP);
Ben Chengccd6c012009-10-15 14:52:45 -0700193 LOGD("Frame1 Bytes: %d Frame2 Local: %d Bytes: %d", frameBytes,
Jeff Hao97319a82009-08-12 16:57:15 -0700194 localRegs, frameBytes2);
Ben Chengccd6c012009-10-15 14:52:45 -0700195 LOGD("Trace length: %d State: %d", shadowSpace->traceLength,
Jeff Hao97319a82009-08-12 16:57:15 -0700196 shadowSpace->selfVerificationState);
197}
198
199/* Print decoded instructions in the current trace */
200static void selfVerificationDumpTrace(const u2* pc, Thread* self)
201{
202 ShadowSpace* shadowSpace = self->shadowSpace;
203 StackSaveArea* stackSave = SAVEAREA_FROM_FP(self->curFrame);
Ben Chengbcdc1de2009-08-21 16:18:46 -0700204 int i, addr, offset;
205 DecodedInstruction *decInsn;
Jeff Hao97319a82009-08-12 16:57:15 -0700206
207 LOGD("********** SHADOW TRACE DUMP **********");
208 for (i = 0; i < shadowSpace->traceLength; i++) {
Ben Chengbcdc1de2009-08-21 16:18:46 -0700209 addr = shadowSpace->trace[i].addr;
210 offset = (int)((u2*)addr - stackSave->method->insns);
211 decInsn = &(shadowSpace->trace[i].decInsn);
212 /* Not properly decoding instruction, some registers may be garbage */
Ben Chengccd6c012009-10-15 14:52:45 -0700213 LOGD("0x%x: (0x%04x) %s", addr, offset, getOpcodeName(decInsn->opCode));
Jeff Hao97319a82009-08-12 16:57:15 -0700214 }
215}
216
Ben Chengbcdc1de2009-08-21 16:18:46 -0700217/* Code is forced into this spin loop when a divergence is detected */
Ben Chengccd6c012009-10-15 14:52:45 -0700218static void selfVerificationSpinLoop(ShadowSpace *shadowSpace)
Ben Chengbcdc1de2009-08-21 16:18:46 -0700219{
Ben Chengccd6c012009-10-15 14:52:45 -0700220 const u2 *startPC = shadowSpace->startPC;
221 JitTraceDescription* desc = dvmCopyTraceDescriptor(startPC);
222 if (desc) {
223 dvmCompilerWorkEnqueue(startPC, kWorkOrderTraceDebug, desc);
224 }
Ben Chengbcdc1de2009-08-21 16:18:46 -0700225 gDvmJit.selfVerificationSpin = true;
226 while(gDvmJit.selfVerificationSpin) sleep(10);
227}
228
Jeff Hao97319a82009-08-12 16:57:15 -0700229/* Manage self verification while in the debug interpreter */
230static bool selfVerificationDebugInterp(const u2* pc, Thread* self)
231{
232 ShadowSpace *shadowSpace = self->shadowSpace;
Jeff Hao97319a82009-08-12 16:57:15 -0700233 SelfVerificationState state = shadowSpace->selfVerificationState;
Ben Chengbcdc1de2009-08-21 16:18:46 -0700234
235 DecodedInstruction decInsn;
236 dexDecodeInstruction(gDvm.instrFormat, pc, &decInsn);
237
Jeff Hao97319a82009-08-12 16:57:15 -0700238 //LOGD("### DbgIntp(%d): PC: 0x%x endPC: 0x%x state: %d len: %d %s",
239 // self->threadId, (int)pc, (int)shadowSpace->endPC, state,
Ben Chengbcdc1de2009-08-21 16:18:46 -0700240 // shadowSpace->traceLength, getOpcodeName(decInsn.opCode));
Jeff Hao97319a82009-08-12 16:57:15 -0700241
242 if (state == kSVSIdle || state == kSVSStart) {
243 LOGD("~~~ DbgIntrp: INCORRECT PREVIOUS STATE(%d): %d",
244 self->threadId, state);
245 selfVerificationDumpState(pc, self);
246 selfVerificationDumpTrace(pc, self);
247 }
248
249 /* Skip endPC once when trace has a backward branch */
250 if ((state == kSVSBackwardBranch && pc == shadowSpace->endPC) ||
251 state != kSVSBackwardBranch) {
252 shadowSpace->selfVerificationState = kSVSDebugInterp;
253 }
254
255 /* Check that the current pc is the end of the trace */
256 if ((state == kSVSSingleStep || state == kSVSDebugInterp) &&
257 pc == shadowSpace->endPC) {
258
259 shadowSpace->selfVerificationState = kSVSIdle;
260
261 /* Check register space */
262 int frameBytes = (int) shadowSpace->registerSpace +
263 shadowSpace->registerSpaceSize*4 -
264 (int) shadowSpace->shadowFP;
265 if (memcmp(shadowSpace->fp, shadowSpace->shadowFP, frameBytes)) {
Ben Chengccd6c012009-10-15 14:52:45 -0700266 LOGD("~~~ DbgIntp(%d): REGISTERS DIVERGENCE!", self->threadId);
Jeff Hao97319a82009-08-12 16:57:15 -0700267 selfVerificationDumpState(pc, self);
268 selfVerificationDumpTrace(pc, self);
269 LOGD("*** Interp Registers: addr: 0x%x bytes: %d",
270 (int)shadowSpace->fp, frameBytes);
Ben Chengccd6c012009-10-15 14:52:45 -0700271 selfVerificationPrintRegisters((int*)shadowSpace->fp,
272 (int*)shadowSpace->shadowFP,
273 frameBytes/4);
Jeff Hao97319a82009-08-12 16:57:15 -0700274 LOGD("*** Shadow Registers: addr: 0x%x bytes: %d",
275 (int)shadowSpace->shadowFP, frameBytes);
276 selfVerificationPrintRegisters((int*)shadowSpace->shadowFP,
Ben Chengccd6c012009-10-15 14:52:45 -0700277 (int*)shadowSpace->fp,
278 frameBytes/4);
279 selfVerificationSpinLoop(shadowSpace);
Jeff Hao97319a82009-08-12 16:57:15 -0700280 }
281 /* Check new frame if it exists (invokes only) */
282 if (self->curFrame < shadowSpace->fp) {
283 StackSaveArea* stackSave = SAVEAREA_FROM_FP(self->curFrame);
284 int localRegs = (stackSave->method->registersSize -
285 stackSave->method->insSize)*4;
286 int frameBytes2 = (int) shadowSpace->fp -
287 (int) self->curFrame - localRegs;
288 if (memcmp(((char*)self->curFrame)+localRegs,
289 ((char*)shadowSpace->endShadowFP)+localRegs, frameBytes2)) {
Ben Chengccd6c012009-10-15 14:52:45 -0700290 LOGD("~~~ DbgIntp(%d): REGISTERS (FRAME2) DIVERGENCE!",
Jeff Hao97319a82009-08-12 16:57:15 -0700291 self->threadId);
292 selfVerificationDumpState(pc, self);
293 selfVerificationDumpTrace(pc, self);
294 LOGD("*** Interp Registers: addr: 0x%x l: %d bytes: %d",
295 (int)self->curFrame, localRegs, frameBytes2);
296 selfVerificationPrintRegisters((int*)self->curFrame,
Ben Chengccd6c012009-10-15 14:52:45 -0700297 (int*)shadowSpace->endShadowFP,
298 (frameBytes2+localRegs)/4);
Jeff Hao97319a82009-08-12 16:57:15 -0700299 LOGD("*** Shadow Registers: addr: 0x%x l: %d bytes: %d",
300 (int)shadowSpace->endShadowFP, localRegs, frameBytes2);
301 selfVerificationPrintRegisters((int*)shadowSpace->endShadowFP,
Ben Chengccd6c012009-10-15 14:52:45 -0700302 (int*)self->curFrame,
303 (frameBytes2+localRegs)/4);
304 selfVerificationSpinLoop(shadowSpace);
Jeff Hao97319a82009-08-12 16:57:15 -0700305 }
306 }
307
308 /* Check memory space */
Ben Chengbcdc1de2009-08-21 16:18:46 -0700309 bool memDiff = false;
Jeff Hao97319a82009-08-12 16:57:15 -0700310 ShadowHeap* heapSpacePtr;
311 for (heapSpacePtr = shadowSpace->heapSpace;
312 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
Ben Chengbcdc1de2009-08-21 16:18:46 -0700313 int memData = *((unsigned int*) heapSpacePtr->addr);
314 if (heapSpacePtr->data != memData) {
Ben Chengccd6c012009-10-15 14:52:45 -0700315 LOGD("~~~ DbgIntp(%d): MEMORY DIVERGENCE!", self->threadId);
316 LOGD("Addr: 0x%x Intrp Data: 0x%x Jit Data: 0x%x",
Ben Chengbcdc1de2009-08-21 16:18:46 -0700317 heapSpacePtr->addr, memData, heapSpacePtr->data);
Jeff Hao97319a82009-08-12 16:57:15 -0700318 selfVerificationDumpState(pc, self);
319 selfVerificationDumpTrace(pc, self);
Ben Chengbcdc1de2009-08-21 16:18:46 -0700320 memDiff = true;
Jeff Hao97319a82009-08-12 16:57:15 -0700321 }
322 }
Ben Chengccd6c012009-10-15 14:52:45 -0700323 if (memDiff) selfVerificationSpinLoop(shadowSpace);
Jeff Hao97319a82009-08-12 16:57:15 -0700324 return true;
325
326 /* If end not been reached, make sure max length not exceeded */
327 } else if (shadowSpace->traceLength >= JIT_MAX_TRACE_LEN) {
328 LOGD("~~~ DbgIntp(%d): CONTROL DIVERGENCE!", self->threadId);
Ben Chengccd6c012009-10-15 14:52:45 -0700329 LOGD("startPC: 0x%x endPC: 0x%x currPC: 0x%x",
Jeff Hao97319a82009-08-12 16:57:15 -0700330 (int)shadowSpace->startPC, (int)shadowSpace->endPC, (int)pc);
331 selfVerificationDumpState(pc, self);
332 selfVerificationDumpTrace(pc, self);
Ben Chengccd6c012009-10-15 14:52:45 -0700333 selfVerificationSpinLoop(shadowSpace);
Jeff Hao97319a82009-08-12 16:57:15 -0700334
335 return true;
336 }
Ben Chengbcdc1de2009-08-21 16:18:46 -0700337 /* Log the instruction address and decoded instruction for debug */
Jeff Hao97319a82009-08-12 16:57:15 -0700338 shadowSpace->trace[shadowSpace->traceLength].addr = (int)pc;
Ben Chengbcdc1de2009-08-21 16:18:46 -0700339 shadowSpace->trace[shadowSpace->traceLength].decInsn = decInsn;
Jeff Hao97319a82009-08-12 16:57:15 -0700340 shadowSpace->traceLength++;
341
342 return false;
343}
344#endif
345
Ben Chengba4fc8b2009-06-01 13:00:29 -0700346int dvmJitStartup(void)
347{
348 unsigned int i;
349 bool res = true; /* Assume success */
350
351 // Create the compiler thread and setup miscellaneous chores */
352 res &= dvmCompilerStartup();
353
354 dvmInitMutex(&gDvmJit.tableLock);
355 if (res && gDvm.executionMode == kExecutionModeJit) {
Bill Buzbee716f1202009-07-23 13:22:09 -0700356 JitEntry *pJitTable = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700357 unsigned char *pJitProfTable = NULL;
Ben Cheng3f02aa42009-08-14 13:52:09 -0700358 // Power of 2?
359 assert(gDvmJit.jitTableSize &&
360 !(gDvmJit.jitTableSize & (gDvmJit.jitTableSize - 1)));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700361 dvmLockMutex(&gDvmJit.tableLock);
Bill Buzbee716f1202009-07-23 13:22:09 -0700362 pJitTable = (JitEntry*)
Bill Buzbee27176222009-06-09 09:20:16 -0700363 calloc(gDvmJit.jitTableSize, sizeof(*pJitTable));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700364 if (!pJitTable) {
365 LOGE("jit table allocation failed\n");
366 res = false;
367 goto done;
368 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700369 /*
370 * NOTE: the profile table must only be allocated once, globally.
371 * Profiling is turned on and off by nulling out gDvm.pJitProfTable
372 * and then restoring its original value. However, this action
373 * is not syncronized for speed so threads may continue to hold
374 * and update the profile table after profiling has been turned
375 * off by null'ng the global pointer. Be aware.
376 */
377 pJitProfTable = (unsigned char *)malloc(JIT_PROF_SIZE);
378 if (!pJitProfTable) {
379 LOGE("jit prof table allocation failed\n");
380 res = false;
381 goto done;
382 }
383 memset(pJitProfTable,0,JIT_PROF_SIZE);
Bill Buzbee27176222009-06-09 09:20:16 -0700384 for (i=0; i < gDvmJit.jitTableSize; i++) {
Bill Buzbee716f1202009-07-23 13:22:09 -0700385 pJitTable[i].u.info.chain = gDvmJit.jitTableSize;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700386 }
387 /* Is chain field wide enough for termination pattern? */
Ben Cheng3f02aa42009-08-14 13:52:09 -0700388 assert(pJitTable[0].u.info.chain == gDvmJit.jitTableSize);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700389
390done:
391 gDvmJit.pJitEntryTable = pJitTable;
Bill Buzbee27176222009-06-09 09:20:16 -0700392 gDvmJit.jitTableMask = gDvmJit.jitTableSize - 1;
393 gDvmJit.jitTableEntriesUsed = 0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700394 gDvmJit.pProfTableCopy = gDvmJit.pProfTable = pJitProfTable;
395 dvmUnlockMutex(&gDvmJit.tableLock);
396 }
397 return res;
398}
399
400/*
401 * If one of our fixed tables or the translation buffer fills up,
402 * call this routine to avoid wasting cycles on future translation requests.
403 */
404void dvmJitStopTranslationRequests()
405{
406 /*
407 * Note 1: This won't necessarily stop all translation requests, and
408 * operates on a delayed mechanism. Running threads look to the copy
409 * of this value in their private InterpState structures and won't see
410 * this change until it is refreshed (which happens on interpreter
411 * entry).
412 * Note 2: This is a one-shot memory leak on this table. Because this is a
413 * permanent off switch for Jit profiling, it is a one-time leak of 1K
414 * bytes, and no further attempt will be made to re-allocate it. Can't
415 * free it because some thread may be holding a reference.
416 */
417 gDvmJit.pProfTable = gDvmJit.pProfTableCopy = NULL;
418}
419
420#if defined(EXIT_STATS)
421/* Convenience function to increment counter from assembly code */
Ben Cheng6c10a972009-10-29 14:39:18 -0700422void dvmBumpNoChain(int from)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700423{
Ben Cheng6c10a972009-10-29 14:39:18 -0700424 gDvmJit.noChainExit[from]++;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700425}
426
427/* Convenience function to increment counter from assembly code */
428void dvmBumpNormal()
429{
Ben Cheng6c10a972009-10-29 14:39:18 -0700430 gDvmJit.normalExit++;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700431}
432
433/* Convenience function to increment counter from assembly code */
434void dvmBumpPunt(int from)
435{
Ben Cheng6c10a972009-10-29 14:39:18 -0700436 gDvmJit.puntExit++;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700437}
438#endif
439
440/* Dumps debugging & tuning stats to the log */
441void dvmJitStats()
442{
443 int i;
444 int hit;
445 int not_hit;
446 int chains;
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800447 int stubs;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700448 if (gDvmJit.pJitEntryTable) {
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800449 for (i=0, stubs=chains=hit=not_hit=0;
Bill Buzbee27176222009-06-09 09:20:16 -0700450 i < (int) gDvmJit.jitTableSize;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700451 i++) {
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800452 if (gDvmJit.pJitEntryTable[i].dPC != 0) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700453 hit++;
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800454 if (gDvmJit.pJitEntryTable[i].codeAddress ==
455 gDvmJit.interpretTemplate)
456 stubs++;
457 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -0700458 not_hit++;
Bill Buzbee716f1202009-07-23 13:22:09 -0700459 if (gDvmJit.pJitEntryTable[i].u.info.chain != gDvmJit.jitTableSize)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700460 chains++;
461 }
462 LOGD(
463 "JIT: %d traces, %d slots, %d chains, %d maxQ, %d thresh, %s",
464 hit, not_hit + hit, chains, gDvmJit.compilerMaxQueued,
465 gDvmJit.threshold, gDvmJit.blockingMode ? "Blocking" : "Non-blocking");
466#if defined(EXIT_STATS)
467 LOGD(
Ben Cheng6c10a972009-10-29 14:39:18 -0700468 "JIT: Lookups: %d hits, %d misses; %d normal, %d punt",
Ben Chengba4fc8b2009-06-01 13:00:29 -0700469 gDvmJit.addrLookupsFound, gDvmJit.addrLookupsNotFound,
Ben Cheng6c10a972009-10-29 14:39:18 -0700470 gDvmJit.normalExit, gDvmJit.puntExit);
471 LOGD(
472 "JIT: noChainExit: %d IC miss, %d interp callsite, %d switch overflow",
473 gDvmJit.noChainExit[kInlineCacheMiss],
474 gDvmJit.noChainExit[kCallsiteInterpreted],
475 gDvmJit.noChainExit[kSwitchOverflow]);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700476#endif
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800477 LOGD("JIT: %d Translation chains, %d interp stubs",
478 gDvmJit.translationChains, stubs);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700479#if defined(INVOKE_STATS)
Ben Cheng38329f52009-07-07 14:19:20 -0700480 LOGD("JIT: Invoke: %d chainable, %d pred. chain, %d native, "
481 "%d return",
482 gDvmJit.invokeChain, gDvmJit.invokePredictedChain,
483 gDvmJit.invokeNative, gDvmJit.returnOp);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700484#endif
Ben Chenge80cd942009-07-17 15:54:23 -0700485 if (gDvmJit.profile) {
Bill Buzbee716f1202009-07-23 13:22:09 -0700486 dvmCompilerSortAndPrintTraceProfiles();
Bill Buzbee6e963e12009-06-17 16:56:19 -0700487 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700488 }
489}
490
Bill Buzbee716f1202009-07-23 13:22:09 -0700491
Ben Chengba4fc8b2009-06-01 13:00:29 -0700492/*
493 * Final JIT shutdown. Only do this once, and do not attempt to restart
494 * the JIT later.
495 */
496void dvmJitShutdown(void)
497{
498 /* Shutdown the compiler thread */
Bill Buzbee1465db52009-09-23 17:17:35 -0700499
Ben Chengba4fc8b2009-06-01 13:00:29 -0700500 dvmCompilerShutdown();
501
502 dvmCompilerDumpStats();
503
504 dvmDestroyMutex(&gDvmJit.tableLock);
505
506 if (gDvmJit.pJitEntryTable) {
507 free(gDvmJit.pJitEntryTable);
508 gDvmJit.pJitEntryTable = NULL;
509 }
510
511 if (gDvmJit.pProfTable) {
512 free(gDvmJit.pProfTable);
513 gDvmJit.pProfTable = NULL;
514 }
515}
516
Ben Chengba4fc8b2009-06-01 13:00:29 -0700517/*
518 * Adds to the current trace request one instruction at a time, just
519 * before that instruction is interpreted. This is the primary trace
520 * selection function. NOTE: return instruction are handled a little
521 * differently. In general, instructions are "proposed" to be added
522 * to the current trace prior to interpretation. If the interpreter
523 * then successfully completes the instruction, is will be considered
524 * part of the request. This allows us to examine machine state prior
525 * to interpretation, and also abort the trace request if the instruction
526 * throws or does something unexpected. However, return instructions
527 * will cause an immediate end to the translation request - which will
528 * be passed to the compiler before the return completes. This is done
529 * in response to special handling of returns by the interpreter (and
530 * because returns cannot throw in a way that causes problems for the
531 * translated code.
532 */
Ben Chengba4fc8b2009-06-01 13:00:29 -0700533int dvmCheckJit(const u2* pc, Thread* self, InterpState* interpState)
534{
535 int flags,i,len;
536 int switchInterp = false;
537 int debugOrProfile = (gDvm.debuggerActive || self->suspendCount
538#if defined(WITH_PROFILER)
539 || gDvm.activeProfilers
540#endif
541 );
Ben Cheng79d173c2009-09-29 16:12:51 -0700542 /* Prepare to handle last PC and stage the current PC */
543 const u2 *lastPC = interpState->lastPC;
544 interpState->lastPC = pc;
545
Ben Chengba4fc8b2009-06-01 13:00:29 -0700546 switch (interpState->jitState) {
547 char* nopStr;
548 int target;
549 int offset;
550 DecodedInstruction decInsn;
551 case kJitTSelect:
Ben Chengdc84bb22009-10-02 12:58:52 -0700552 /* First instruction - just remember the PC and exit */
553 if (lastPC == NULL) break;
Ben Cheng79d173c2009-09-29 16:12:51 -0700554 /* Grow the trace around the last PC if jitState is kJitTSelect */
555 dexDecodeInstruction(gDvm.instrFormat, lastPC, &decInsn);
Ben Cheng6c10a972009-10-29 14:39:18 -0700556
557 /*
558 * Treat {PACKED,SPARSE}_SWITCH as trace-ending instructions due
559 * to the amount of space it takes to generate the chaining
560 * cells.
561 */
562 if (interpState->totalTraceLen != 0 &&
563 (decInsn.opCode == OP_PACKED_SWITCH ||
564 decInsn.opCode == OP_SPARSE_SWITCH)) {
565 interpState->jitState = kJitTSelectEnd;
566 break;
567 }
568
Ben Chengba4fc8b2009-06-01 13:00:29 -0700569#if defined(SHOW_TRACE)
570 LOGD("TraceGen: adding %s",getOpcodeName(decInsn.opCode));
571#endif
572 flags = dexGetInstrFlags(gDvm.instrFlags, decInsn.opCode);
Ben Cheng79d173c2009-09-29 16:12:51 -0700573 len = dexGetInstrOrTableWidthAbs(gDvm.instrWidth, lastPC);
574 offset = lastPC - interpState->method->insns;
575 assert((unsigned) offset <
576 dvmGetMethodInsnsSize(interpState->method));
577 if (lastPC != interpState->currRunHead + interpState->currRunLen) {
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700578 int currTraceRun;
579 /* We need to start a new trace run */
580 currTraceRun = ++interpState->currTraceRun;
581 interpState->currRunLen = 0;
Ben Cheng79d173c2009-09-29 16:12:51 -0700582 interpState->currRunHead = (u2*)lastPC;
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700583 interpState->trace[currTraceRun].frag.startOffset = offset;
584 interpState->trace[currTraceRun].frag.numInsts = 0;
585 interpState->trace[currTraceRun].frag.runEnd = false;
586 interpState->trace[currTraceRun].frag.hint = kJitHintNone;
587 }
588 interpState->trace[interpState->currTraceRun].frag.numInsts++;
589 interpState->totalTraceLen++;
590 interpState->currRunLen += len;
Ben Cheng79d173c2009-09-29 16:12:51 -0700591
592 /* Will probably never hit this with the current trace buildier */
593 if (interpState->currTraceRun == (MAX_JIT_RUN_LEN - 1)) {
594 interpState->jitState = kJitTSelectEnd;
595 }
596
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700597 if ( ((flags & kInstrUnconditional) == 0) &&
Bill Buzbeef4ce16f2009-07-28 13:28:25 -0700598 /* don't end trace on INVOKE_DIRECT_EMPTY */
599 (decInsn.opCode != OP_INVOKE_DIRECT_EMPTY) &&
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700600 ((flags & (kInstrCanBranch |
601 kInstrCanSwitch |
602 kInstrCanReturn |
603 kInstrInvoke)) != 0)) {
604 interpState->jitState = kJitTSelectEnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700605#if defined(SHOW_TRACE)
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700606 LOGD("TraceGen: ending on %s, basic block end",
607 getOpcodeName(decInsn.opCode));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700608#endif
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700609 }
610 if (decInsn.opCode == OP_THROW) {
611 interpState->jitState = kJitTSelectEnd;
612 }
613 if (interpState->totalTraceLen >= JIT_MAX_TRACE_LEN) {
614 interpState->jitState = kJitTSelectEnd;
615 }
616 if (debugOrProfile) {
617 interpState->jitState = kJitTSelectAbort;
618 switchInterp = !debugOrProfile;
619 break;
620 }
621 if ((flags & kInstrCanReturn) != kInstrCanReturn) {
622 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700623 }
624 /* NOTE: intentional fallthrough for returns */
625 case kJitTSelectEnd:
626 {
627 if (interpState->totalTraceLen == 0) {
628 switchInterp = !debugOrProfile;
629 break;
630 }
631 JitTraceDescription* desc =
632 (JitTraceDescription*)malloc(sizeof(JitTraceDescription) +
633 sizeof(JitTraceRun) * (interpState->currTraceRun+1));
634 if (desc == NULL) {
635 LOGE("Out of memory in trace selection");
636 dvmJitStopTranslationRequests();
637 interpState->jitState = kJitTSelectAbort;
638 switchInterp = !debugOrProfile;
639 break;
640 }
641 interpState->trace[interpState->currTraceRun].frag.runEnd =
642 true;
643 interpState->jitState = kJitNormal;
644 desc->method = interpState->method;
645 memcpy((char*)&(desc->trace[0]),
646 (char*)&(interpState->trace[0]),
647 sizeof(JitTraceRun) * (interpState->currTraceRun+1));
648#if defined(SHOW_TRACE)
649 LOGD("TraceGen: trace done, adding to queue");
650#endif
651 dvmCompilerWorkEnqueue(
652 interpState->currTraceHead,kWorkOrderTrace,desc);
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800653 interpState->jitTraceInProgress = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700654 if (gDvmJit.blockingMode) {
655 dvmCompilerDrainQueue();
656 }
657 switchInterp = !debugOrProfile;
658 }
659 break;
660 case kJitSingleStep:
661 interpState->jitState = kJitSingleStepEnd;
662 break;
663 case kJitSingleStepEnd:
664 interpState->entryPoint = kInterpEntryResume;
665 switchInterp = !debugOrProfile;
666 break;
667 case kJitTSelectAbort:
668#if defined(SHOW_TRACE)
669 LOGD("TraceGen: trace abort");
670#endif
671 interpState->jitState = kJitNormal;
672 switchInterp = !debugOrProfile;
673 break;
674 case kJitNormal:
Ben Cheng38329f52009-07-07 14:19:20 -0700675 switchInterp = !debugOrProfile;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700676 break;
Jeff Hao97319a82009-08-12 16:57:15 -0700677#if defined(WITH_SELF_VERIFICATION)
678 case kJitSelfVerification:
679 if (selfVerificationDebugInterp(pc, self)) {
680 interpState->jitState = kJitNormal;
681 switchInterp = !debugOrProfile;
682 }
683 break;
684#endif
Ben Chenged79ff02009-10-13 13:26:40 -0700685 /* If JIT is off stay out of interpreter selections */
686 case kJitOff:
687 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700688 default:
Ben Cheng9c147b82009-10-07 16:41:46 -0700689 if (!debugOrProfile) {
690 LOGE("Unexpected JIT state: %d", interpState->jitState);
691 dvmAbort();
692 }
693 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700694 }
695 return switchInterp;
696}
697
Ben Chengccd6c012009-10-15 14:52:45 -0700698JitEntry *dvmFindJitEntry(const u2* pc)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700699{
700 int idx = dvmJitHash(pc);
701
702 /* Expect a high hit rate on 1st shot */
703 if (gDvmJit.pJitEntryTable[idx].dPC == pc)
704 return &gDvmJit.pJitEntryTable[idx];
705 else {
Bill Buzbee27176222009-06-09 09:20:16 -0700706 int chainEndMarker = gDvmJit.jitTableSize;
Bill Buzbee716f1202009-07-23 13:22:09 -0700707 while (gDvmJit.pJitEntryTable[idx].u.info.chain != chainEndMarker) {
708 idx = gDvmJit.pJitEntryTable[idx].u.info.chain;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700709 if (gDvmJit.pJitEntryTable[idx].dPC == pc)
710 return &gDvmJit.pJitEntryTable[idx];
711 }
712 }
713 return NULL;
714}
715
Bill Buzbee27176222009-06-09 09:20:16 -0700716/*
Ben Chengba4fc8b2009-06-01 13:00:29 -0700717 * If a translated code address exists for the davik byte code
718 * pointer return it. This routine needs to be fast.
719 */
720void* dvmJitGetCodeAddr(const u2* dPC)
721{
722 int idx = dvmJitHash(dPC);
723
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700724 /* If anything is suspended, don't re-enter the code cache */
725 if (gDvm.sumThreadSuspendCount > 0) {
726 return NULL;
727 }
728
Ben Chengba4fc8b2009-06-01 13:00:29 -0700729 /* Expect a high hit rate on 1st shot */
730 if (gDvmJit.pJitEntryTable[idx].dPC == dPC) {
731#if defined(EXIT_STATS)
732 gDvmJit.addrLookupsFound++;
733#endif
734 return gDvmJit.pJitEntryTable[idx].codeAddress;
735 } else {
Bill Buzbee27176222009-06-09 09:20:16 -0700736 int chainEndMarker = gDvmJit.jitTableSize;
Bill Buzbee716f1202009-07-23 13:22:09 -0700737 while (gDvmJit.pJitEntryTable[idx].u.info.chain != chainEndMarker) {
738 idx = gDvmJit.pJitEntryTable[idx].u.info.chain;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700739 if (gDvmJit.pJitEntryTable[idx].dPC == dPC) {
740#if defined(EXIT_STATS)
741 gDvmJit.addrLookupsFound++;
742#endif
743 return gDvmJit.pJitEntryTable[idx].codeAddress;
744 }
745 }
746 }
747#if defined(EXIT_STATS)
748 gDvmJit.addrLookupsNotFound++;
749#endif
750 return NULL;
751}
752
753/*
Bill Buzbee716f1202009-07-23 13:22:09 -0700754 * Find an entry in the JitTable, creating if necessary.
755 * Returns null if table is full.
756 */
757JitEntry *dvmJitLookupAndAdd(const u2* dPC)
758{
759 u4 chainEndMarker = gDvmJit.jitTableSize;
760 u4 idx = dvmJitHash(dPC);
761
762 /* Walk the bucket chain to find an exact match for our PC */
763 while ((gDvmJit.pJitEntryTable[idx].u.info.chain != chainEndMarker) &&
764 (gDvmJit.pJitEntryTable[idx].dPC != dPC)) {
765 idx = gDvmJit.pJitEntryTable[idx].u.info.chain;
766 }
767
768 if (gDvmJit.pJitEntryTable[idx].dPC != dPC) {
769 /*
770 * No match. Aquire jitTableLock and find the last
771 * slot in the chain. Possibly continue the chain walk in case
772 * some other thread allocated the slot we were looking
773 * at previuosly (perhaps even the dPC we're trying to enter).
774 */
775 dvmLockMutex(&gDvmJit.tableLock);
776 /*
777 * At this point, if .dPC is NULL, then the slot we're
778 * looking at is the target slot from the primary hash
779 * (the simple, and common case). Otherwise we're going
780 * to have to find a free slot and chain it.
781 */
782 MEM_BARRIER(); /* Make sure we reload [].dPC after lock */
783 if (gDvmJit.pJitEntryTable[idx].dPC != NULL) {
784 u4 prev;
785 while (gDvmJit.pJitEntryTable[idx].u.info.chain != chainEndMarker) {
786 if (gDvmJit.pJitEntryTable[idx].dPC == dPC) {
787 /* Another thread got there first for this dPC */
788 dvmUnlockMutex(&gDvmJit.tableLock);
789 return &gDvmJit.pJitEntryTable[idx];
790 }
791 idx = gDvmJit.pJitEntryTable[idx].u.info.chain;
792 }
793 /* Here, idx should be pointing to the last cell of an
794 * active chain whose last member contains a valid dPC */
795 assert(gDvmJit.pJitEntryTable[idx].dPC != NULL);
796 /* Linear walk to find a free cell and add it to the end */
797 prev = idx;
798 while (true) {
799 idx++;
800 if (idx == chainEndMarker)
801 idx = 0; /* Wraparound */
802 if ((gDvmJit.pJitEntryTable[idx].dPC == NULL) ||
803 (idx == prev))
804 break;
805 }
806 if (idx != prev) {
807 JitEntryInfoUnion oldValue;
808 JitEntryInfoUnion newValue;
809 /*
810 * Although we hold the lock so that noone else will
811 * be trying to update a chain field, the other fields
812 * packed into the word may be in use by other threads.
813 */
814 do {
815 oldValue = gDvmJit.pJitEntryTable[prev].u;
816 newValue = oldValue;
817 newValue.info.chain = idx;
818 } while (!ATOMIC_CMP_SWAP(
819 &gDvmJit.pJitEntryTable[prev].u.infoWord,
820 oldValue.infoWord, newValue.infoWord));
821 }
822 }
823 if (gDvmJit.pJitEntryTable[idx].dPC == NULL) {
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800824 /*
825 * Initialize codeAddress and allocate the slot. Must
826 * happen in this order (ince dPC is set, the entry is live.
827 */
Bill Buzbee716f1202009-07-23 13:22:09 -0700828 gDvmJit.pJitEntryTable[idx].dPC = dPC;
829 gDvmJit.jitTableEntriesUsed++;
830 } else {
831 /* Table is full */
832 idx = chainEndMarker;
833 }
834 dvmUnlockMutex(&gDvmJit.tableLock);
835 }
836 return (idx == chainEndMarker) ? NULL : &gDvmJit.pJitEntryTable[idx];
837}
838/*
Ben Chengba4fc8b2009-06-01 13:00:29 -0700839 * Register the translated code pointer into the JitTable.
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800840 * NOTE: Once a codeAddress field transitions from initial state to
Ben Chengba4fc8b2009-06-01 13:00:29 -0700841 * JIT'd code, it must not be altered without first halting all
Bill Buzbee716f1202009-07-23 13:22:09 -0700842 * threads. This routine should only be called by the compiler
843 * thread.
Ben Chengba4fc8b2009-06-01 13:00:29 -0700844 */
Bill Buzbee716f1202009-07-23 13:22:09 -0700845void dvmJitSetCodeAddr(const u2* dPC, void *nPC, JitInstructionSetType set) {
846 JitEntryInfoUnion oldValue;
847 JitEntryInfoUnion newValue;
848 JitEntry *jitEntry = dvmJitLookupAndAdd(dPC);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700849 assert(jitEntry);
Bill Buzbee716f1202009-07-23 13:22:09 -0700850 /* Note: order of update is important */
851 do {
852 oldValue = jitEntry->u;
853 newValue = oldValue;
854 newValue.info.instructionSet = set;
855 } while (!ATOMIC_CMP_SWAP(
856 &jitEntry->u.infoWord,
857 oldValue.infoWord, newValue.infoWord));
858 jitEntry->codeAddress = nPC;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700859}
860
861/*
862 * Determine if valid trace-bulding request is active. Return true
863 * if we need to abort and switch back to the fast interpreter, false
864 * otherwise. NOTE: may be called even when trace selection is not being
865 * requested
866 */
867
Ben Chengba4fc8b2009-06-01 13:00:29 -0700868bool dvmJitCheckTraceRequest(Thread* self, InterpState* interpState)
869{
Bill Buzbee48f18242009-06-19 16:02:27 -0700870 bool res = false; /* Assume success */
871 int i;
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800872 /*
873 * If previous trace-building attempt failed, force it's head to be
874 * interpret-only.
875 */
876 if (interpState->jitTraceInProgress) {
877 JitEntry *slot = dvmJitLookupAndAdd(interpState->jitTraceInProgress);
878 slot->codeAddress = gDvmJit.interpretTemplate;
879 interpState->jitTraceInProgress = NULL;
880 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700881 if (gDvmJit.pJitEntryTable != NULL) {
Bill Buzbee48f18242009-06-19 16:02:27 -0700882 /* Two-level filtering scheme */
883 for (i=0; i< JIT_TRACE_THRESH_FILTER_SIZE; i++) {
884 if (interpState->pc == interpState->threshFilter[i]) {
885 break;
886 }
887 }
888 if (i == JIT_TRACE_THRESH_FILTER_SIZE) {
889 /*
890 * Use random replacement policy - otherwise we could miss a large
891 * loop that contains more traces than the size of our filter array.
892 */
893 i = rand() % JIT_TRACE_THRESH_FILTER_SIZE;
894 interpState->threshFilter[i] = interpState->pc;
895 res = true;
896 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700897 /*
898 * If the compiler is backlogged, or if a debugger or profiler is
899 * active, cancel any JIT actions
900 */
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800901 if (res || (gDvmJit.compilerQueueLength >= gDvmJit.compilerHighWater)
902 || gDvm.debuggerActive || self->suspendCount
Ben Chengba4fc8b2009-06-01 13:00:29 -0700903#if defined(WITH_PROFILER)
904 || gDvm.activeProfilers
905#endif
906 ) {
907 if (interpState->jitState != kJitOff) {
908 interpState->jitState = kJitNormal;
909 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700910 } else if (interpState->jitState == kJitTSelectRequest) {
Bill Buzbee716f1202009-07-23 13:22:09 -0700911 JitEntry *slot = dvmJitLookupAndAdd(interpState->pc);
912 if (slot == NULL) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700913 /*
Bill Buzbee716f1202009-07-23 13:22:09 -0700914 * Table is full. This should have been
915 * detected by the compiler thread and the table
916 * resized before we run into it here. Assume bad things
917 * are afoot and disable profiling.
Ben Chengba4fc8b2009-06-01 13:00:29 -0700918 */
919 interpState->jitState = kJitTSelectAbort;
Bill Buzbee716f1202009-07-23 13:22:09 -0700920 LOGD("JIT: JitTable full, disabling profiling");
921 dvmJitStopTranslationRequests();
922 } else if (slot->u.info.traceRequested) {
923 /* Trace already requested - revert to interpreter */
924 interpState->jitState = kJitTSelectAbort;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700925 } else {
Bill Buzbee716f1202009-07-23 13:22:09 -0700926 /* Mark request */
927 JitEntryInfoUnion oldValue;
928 JitEntryInfoUnion newValue;
929 do {
930 oldValue = slot->u;
931 newValue = oldValue;
932 newValue.info.traceRequested = true;
933 } while (!ATOMIC_CMP_SWAP( &slot->u.infoWord,
934 oldValue.infoWord, newValue.infoWord));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700935 }
936 }
937 switch (interpState->jitState) {
938 case kJitTSelectRequest:
939 interpState->jitState = kJitTSelect;
940 interpState->currTraceHead = interpState->pc;
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800941 interpState->jitTraceInProgress = interpState->pc;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700942 interpState->currTraceRun = 0;
943 interpState->totalTraceLen = 0;
944 interpState->currRunHead = interpState->pc;
945 interpState->currRunLen = 0;
946 interpState->trace[0].frag.startOffset =
947 interpState->pc - interpState->method->insns;
948 interpState->trace[0].frag.numInsts = 0;
949 interpState->trace[0].frag.runEnd = false;
950 interpState->trace[0].frag.hint = kJitHintNone;
Ben Cheng79d173c2009-09-29 16:12:51 -0700951 interpState->lastPC = 0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700952 break;
953 case kJitTSelect:
954 case kJitTSelectAbort:
955 res = true;
956 case kJitSingleStep:
957 case kJitSingleStepEnd:
958 case kJitOff:
959 case kJitNormal:
Jeff Hao97319a82009-08-12 16:57:15 -0700960#if defined(WITH_SELF_VERIFICATION)
961 case kJitSelfVerification:
962#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -0700963 break;
964 default:
Ben Cheng9c147b82009-10-07 16:41:46 -0700965 LOGE("Unexpected JIT state: %d", interpState->jitState);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700966 dvmAbort();
967 }
968 }
969 return res;
970}
971
Bill Buzbee27176222009-06-09 09:20:16 -0700972/*
973 * Resizes the JitTable. Must be a power of 2, and returns true on failure.
974 * Stops all threads, and thus is a heavyweight operation.
975 */
976bool dvmJitResizeJitTable( unsigned int size )
977{
Bill Buzbee716f1202009-07-23 13:22:09 -0700978 JitEntry *pNewTable;
979 JitEntry *pOldTable;
Bill Buzbee27176222009-06-09 09:20:16 -0700980 u4 newMask;
Bill Buzbee716f1202009-07-23 13:22:09 -0700981 unsigned int oldSize;
Bill Buzbee27176222009-06-09 09:20:16 -0700982 unsigned int i;
983
Ben Cheng3f02aa42009-08-14 13:52:09 -0700984 assert(gDvmJit.pJitEntryTable != NULL);
Bill Buzbee27176222009-06-09 09:20:16 -0700985 assert(size && !(size & (size - 1))); /* Is power of 2? */
986
987 LOGD("Jit: resizing JitTable from %d to %d", gDvmJit.jitTableSize, size);
988
989 newMask = size - 1;
990
991 if (size <= gDvmJit.jitTableSize) {
992 return true;
993 }
994
Bill Buzbee716f1202009-07-23 13:22:09 -0700995 pNewTable = (JitEntry*)calloc(size, sizeof(*pNewTable));
Bill Buzbee27176222009-06-09 09:20:16 -0700996 if (pNewTable == NULL) {
997 return true;
998 }
999 for (i=0; i< size; i++) {
Bill Buzbee716f1202009-07-23 13:22:09 -07001000 pNewTable[i].u.info.chain = size; /* Initialize chain termination */
Bill Buzbee27176222009-06-09 09:20:16 -07001001 }
1002
1003 /* Stop all other interpreting/jit'ng threads */
Ben Chenga8e64a72009-10-20 13:01:36 -07001004 dvmSuspendAllThreads(SUSPEND_FOR_TBL_RESIZE);
Bill Buzbee27176222009-06-09 09:20:16 -07001005
Bill Buzbee716f1202009-07-23 13:22:09 -07001006 pOldTable = gDvmJit.pJitEntryTable;
1007 oldSize = gDvmJit.jitTableSize;
Bill Buzbee27176222009-06-09 09:20:16 -07001008
1009 dvmLockMutex(&gDvmJit.tableLock);
Bill Buzbee27176222009-06-09 09:20:16 -07001010 gDvmJit.pJitEntryTable = pNewTable;
1011 gDvmJit.jitTableSize = size;
1012 gDvmJit.jitTableMask = size - 1;
Bill Buzbee716f1202009-07-23 13:22:09 -07001013 gDvmJit.jitTableEntriesUsed = 0;
Bill Buzbee27176222009-06-09 09:20:16 -07001014 dvmUnlockMutex(&gDvmJit.tableLock);
1015
Bill Buzbee716f1202009-07-23 13:22:09 -07001016 for (i=0; i < oldSize; i++) {
1017 if (pOldTable[i].dPC) {
1018 JitEntry *p;
1019 u2 chain;
1020 p = dvmJitLookupAndAdd(pOldTable[i].dPC);
1021 p->dPC = pOldTable[i].dPC;
1022 /*
1023 * Compiler thread may have just updated the new entry's
1024 * code address field, so don't blindly copy null.
1025 */
1026 if (pOldTable[i].codeAddress != NULL) {
1027 p->codeAddress = pOldTable[i].codeAddress;
1028 }
1029 /* We need to preserve the new chain field, but copy the rest */
1030 dvmLockMutex(&gDvmJit.tableLock);
1031 chain = p->u.info.chain;
1032 p->u = pOldTable[i].u;
1033 p->u.info.chain = chain;
1034 dvmUnlockMutex(&gDvmJit.tableLock);
1035 }
1036 }
1037
1038 free(pOldTable);
1039
Bill Buzbee27176222009-06-09 09:20:16 -07001040 /* Restart the world */
Ben Chenga8e64a72009-10-20 13:01:36 -07001041 dvmResumeAllThreads(SUSPEND_FOR_TBL_RESIZE);
Bill Buzbee27176222009-06-09 09:20:16 -07001042
1043 return false;
1044}
1045
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001046/*
1047 * Float/double conversion requires clamping to min and max of integer form. If
1048 * target doesn't support this normally, use these.
1049 */
1050s8 dvmJitd2l(double d)
1051{
Bill Buzbee9727c3d2009-08-01 11:32:36 -07001052 static const double kMaxLong = (double)(s8)0x7fffffffffffffffULL;
1053 static const double kMinLong = (double)(s8)0x8000000000000000ULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001054 if (d >= kMaxLong)
Bill Buzbee9727c3d2009-08-01 11:32:36 -07001055 return (s8)0x7fffffffffffffffULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001056 else if (d <= kMinLong)
Bill Buzbee9727c3d2009-08-01 11:32:36 -07001057 return (s8)0x8000000000000000ULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001058 else if (d != d) // NaN case
1059 return 0;
1060 else
1061 return (s8)d;
1062}
1063
1064s8 dvmJitf2l(float f)
1065{
Bill Buzbee9727c3d2009-08-01 11:32:36 -07001066 static const float kMaxLong = (float)(s8)0x7fffffffffffffffULL;
1067 static const float kMinLong = (float)(s8)0x8000000000000000ULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001068 if (f >= kMaxLong)
Bill Buzbee9727c3d2009-08-01 11:32:36 -07001069 return (s8)0x7fffffffffffffffULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001070 else if (f <= kMinLong)
Bill Buzbee9727c3d2009-08-01 11:32:36 -07001071 return (s8)0x8000000000000000ULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001072 else if (f != f) // NaN case
1073 return 0;
1074 else
1075 return (s8)f;
1076}
1077
Bill Buzbee27176222009-06-09 09:20:16 -07001078
Ben Chengba4fc8b2009-06-01 13:00:29 -07001079#endif /* WITH_JIT */