blob: 2a5d9ef82f31fcc9f73bae26fb7671089b0c17a0 [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
Bill Buzbeed7269912009-11-10 14:31:32 -0800351#if defined(WITH_SELF_VERIFICATION)
352 // Force JIT into blocking, translate everything mode
Bill Buzbeef9f33282009-11-22 12:45:30 -0800353 gDvmJit.threshold = 1;
Bill Buzbeed7269912009-11-10 14:31:32 -0800354 gDvmJit.blockingMode = true;
355#endif
356
Ben Chengba4fc8b2009-06-01 13:00:29 -0700357 // Create the compiler thread and setup miscellaneous chores */
358 res &= dvmCompilerStartup();
359
360 dvmInitMutex(&gDvmJit.tableLock);
361 if (res && gDvm.executionMode == kExecutionModeJit) {
Bill Buzbee716f1202009-07-23 13:22:09 -0700362 JitEntry *pJitTable = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700363 unsigned char *pJitProfTable = NULL;
Ben Cheng3f02aa42009-08-14 13:52:09 -0700364 // Power of 2?
365 assert(gDvmJit.jitTableSize &&
366 !(gDvmJit.jitTableSize & (gDvmJit.jitTableSize - 1)));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700367 dvmLockMutex(&gDvmJit.tableLock);
Bill Buzbee716f1202009-07-23 13:22:09 -0700368 pJitTable = (JitEntry*)
Bill Buzbee27176222009-06-09 09:20:16 -0700369 calloc(gDvmJit.jitTableSize, sizeof(*pJitTable));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700370 if (!pJitTable) {
371 LOGE("jit table allocation failed\n");
372 res = false;
373 goto done;
374 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700375 /*
376 * NOTE: the profile table must only be allocated once, globally.
377 * Profiling is turned on and off by nulling out gDvm.pJitProfTable
378 * and then restoring its original value. However, this action
379 * is not syncronized for speed so threads may continue to hold
380 * and update the profile table after profiling has been turned
381 * off by null'ng the global pointer. Be aware.
382 */
383 pJitProfTable = (unsigned char *)malloc(JIT_PROF_SIZE);
384 if (!pJitProfTable) {
385 LOGE("jit prof table allocation failed\n");
386 res = false;
387 goto done;
388 }
Bill Buzbeed7269912009-11-10 14:31:32 -0800389 memset(pJitProfTable,gDvmJit.threshold,JIT_PROF_SIZE);
Bill Buzbee27176222009-06-09 09:20:16 -0700390 for (i=0; i < gDvmJit.jitTableSize; i++) {
Bill Buzbee716f1202009-07-23 13:22:09 -0700391 pJitTable[i].u.info.chain = gDvmJit.jitTableSize;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700392 }
393 /* Is chain field wide enough for termination pattern? */
Ben Cheng3f02aa42009-08-14 13:52:09 -0700394 assert(pJitTable[0].u.info.chain == gDvmJit.jitTableSize);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700395
396done:
397 gDvmJit.pJitEntryTable = pJitTable;
Bill Buzbee27176222009-06-09 09:20:16 -0700398 gDvmJit.jitTableMask = gDvmJit.jitTableSize - 1;
399 gDvmJit.jitTableEntriesUsed = 0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700400 gDvmJit.pProfTableCopy = gDvmJit.pProfTable = pJitProfTable;
401 dvmUnlockMutex(&gDvmJit.tableLock);
402 }
403 return res;
404}
405
406/*
407 * If one of our fixed tables or the translation buffer fills up,
408 * call this routine to avoid wasting cycles on future translation requests.
409 */
410void dvmJitStopTranslationRequests()
411{
412 /*
413 * Note 1: This won't necessarily stop all translation requests, and
414 * operates on a delayed mechanism. Running threads look to the copy
415 * of this value in their private InterpState structures and won't see
416 * this change until it is refreshed (which happens on interpreter
417 * entry).
418 * Note 2: This is a one-shot memory leak on this table. Because this is a
419 * permanent off switch for Jit profiling, it is a one-time leak of 1K
420 * bytes, and no further attempt will be made to re-allocate it. Can't
421 * free it because some thread may be holding a reference.
422 */
423 gDvmJit.pProfTable = gDvmJit.pProfTableCopy = NULL;
424}
425
426#if defined(EXIT_STATS)
427/* Convenience function to increment counter from assembly code */
Ben Cheng6c10a972009-10-29 14:39:18 -0700428void dvmBumpNoChain(int from)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700429{
Ben Cheng6c10a972009-10-29 14:39:18 -0700430 gDvmJit.noChainExit[from]++;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700431}
432
433/* Convenience function to increment counter from assembly code */
434void dvmBumpNormal()
435{
Ben Cheng6c10a972009-10-29 14:39:18 -0700436 gDvmJit.normalExit++;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700437}
438
439/* Convenience function to increment counter from assembly code */
440void dvmBumpPunt(int from)
441{
Ben Cheng6c10a972009-10-29 14:39:18 -0700442 gDvmJit.puntExit++;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700443}
444#endif
445
446/* Dumps debugging & tuning stats to the log */
447void dvmJitStats()
448{
449 int i;
450 int hit;
451 int not_hit;
452 int chains;
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800453 int stubs;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700454 if (gDvmJit.pJitEntryTable) {
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800455 for (i=0, stubs=chains=hit=not_hit=0;
Bill Buzbee27176222009-06-09 09:20:16 -0700456 i < (int) gDvmJit.jitTableSize;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700457 i++) {
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800458 if (gDvmJit.pJitEntryTable[i].dPC != 0) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700459 hit++;
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800460 if (gDvmJit.pJitEntryTable[i].codeAddress ==
461 gDvmJit.interpretTemplate)
462 stubs++;
463 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -0700464 not_hit++;
Bill Buzbee716f1202009-07-23 13:22:09 -0700465 if (gDvmJit.pJitEntryTable[i].u.info.chain != gDvmJit.jitTableSize)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700466 chains++;
467 }
468 LOGD(
469 "JIT: %d traces, %d slots, %d chains, %d maxQ, %d thresh, %s",
470 hit, not_hit + hit, chains, gDvmJit.compilerMaxQueued,
471 gDvmJit.threshold, gDvmJit.blockingMode ? "Blocking" : "Non-blocking");
472#if defined(EXIT_STATS)
473 LOGD(
Ben Cheng6c10a972009-10-29 14:39:18 -0700474 "JIT: Lookups: %d hits, %d misses; %d normal, %d punt",
Ben Chengba4fc8b2009-06-01 13:00:29 -0700475 gDvmJit.addrLookupsFound, gDvmJit.addrLookupsNotFound,
Ben Cheng6c10a972009-10-29 14:39:18 -0700476 gDvmJit.normalExit, gDvmJit.puntExit);
477 LOGD(
478 "JIT: noChainExit: %d IC miss, %d interp callsite, %d switch overflow",
479 gDvmJit.noChainExit[kInlineCacheMiss],
480 gDvmJit.noChainExit[kCallsiteInterpreted],
481 gDvmJit.noChainExit[kSwitchOverflow]);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700482#endif
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800483 LOGD("JIT: %d Translation chains, %d interp stubs",
484 gDvmJit.translationChains, stubs);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700485#if defined(INVOKE_STATS)
Ben Cheng38329f52009-07-07 14:19:20 -0700486 LOGD("JIT: Invoke: %d chainable, %d pred. chain, %d native, "
487 "%d return",
488 gDvmJit.invokeChain, gDvmJit.invokePredictedChain,
489 gDvmJit.invokeNative, gDvmJit.returnOp);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700490#endif
Ben Chenge80cd942009-07-17 15:54:23 -0700491 if (gDvmJit.profile) {
Bill Buzbee716f1202009-07-23 13:22:09 -0700492 dvmCompilerSortAndPrintTraceProfiles();
Bill Buzbee6e963e12009-06-17 16:56:19 -0700493 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700494 }
495}
496
Bill Buzbee716f1202009-07-23 13:22:09 -0700497
Ben Chengba4fc8b2009-06-01 13:00:29 -0700498/*
499 * Final JIT shutdown. Only do this once, and do not attempt to restart
500 * the JIT later.
501 */
502void dvmJitShutdown(void)
503{
504 /* Shutdown the compiler thread */
Bill Buzbee1465db52009-09-23 17:17:35 -0700505
Ben Chengba4fc8b2009-06-01 13:00:29 -0700506 dvmCompilerShutdown();
507
508 dvmCompilerDumpStats();
509
510 dvmDestroyMutex(&gDvmJit.tableLock);
511
512 if (gDvmJit.pJitEntryTable) {
513 free(gDvmJit.pJitEntryTable);
514 gDvmJit.pJitEntryTable = NULL;
515 }
516
517 if (gDvmJit.pProfTable) {
518 free(gDvmJit.pProfTable);
519 gDvmJit.pProfTable = NULL;
520 }
521}
522
Bill Buzbeed7269912009-11-10 14:31:32 -0800523void setTraceConstruction(JitEntry *slot, bool value)
524{
525
526 JitEntryInfoUnion oldValue;
527 JitEntryInfoUnion newValue;
528 do {
529 oldValue = slot->u;
530 newValue = oldValue;
531 newValue.info.traceConstruction = value;
532 } while (!ATOMIC_CMP_SWAP( &slot->u.infoWord,
533 oldValue.infoWord, newValue.infoWord));
534}
535
536void resetTracehead(InterpState* interpState, JitEntry *slot)
537{
538 slot->codeAddress = gDvmJit.interpretTemplate;
539 setTraceConstruction(slot, false);
540}
541
542/* Clean up any pending trace builds */
543void dvmJitAbortTraceSelect(InterpState* interpState)
544{
545 if (interpState->jitState == kJitTSelect)
546 interpState->jitState = kJitTSelectAbort;
547}
548
Bill Buzbeef9f33282009-11-22 12:45:30 -0800549#if defined(WITH_SELF_VERIFICATION)
550static bool selfVerificationPuntOps(DecodedInstruction *decInsn)
551{
552 OpCode op = decInsn->opCode;
553 int flags = dexGetInstrFlags(gDvm.instrFlags, op);
554 return (op == OP_MONITOR_ENTER || op == OP_MONITOR_EXIT ||
555 op == OP_NEW_INSTANCE || op == OP_NEW_ARRAY ||
556 op == OP_CHECK_CAST || op == OP_MOVE_EXCEPTION ||
557 (flags & kInstrInvoke));
558}
559#endif
560
Ben Chengba4fc8b2009-06-01 13:00:29 -0700561/*
562 * Adds to the current trace request one instruction at a time, just
563 * before that instruction is interpreted. This is the primary trace
564 * selection function. NOTE: return instruction are handled a little
565 * differently. In general, instructions are "proposed" to be added
566 * to the current trace prior to interpretation. If the interpreter
567 * then successfully completes the instruction, is will be considered
568 * part of the request. This allows us to examine machine state prior
569 * to interpretation, and also abort the trace request if the instruction
570 * throws or does something unexpected. However, return instructions
571 * will cause an immediate end to the translation request - which will
572 * be passed to the compiler before the return completes. This is done
573 * in response to special handling of returns by the interpreter (and
574 * because returns cannot throw in a way that causes problems for the
575 * translated code.
576 */
Ben Chengba4fc8b2009-06-01 13:00:29 -0700577int dvmCheckJit(const u2* pc, Thread* self, InterpState* interpState)
578{
579 int flags,i,len;
580 int switchInterp = false;
581 int debugOrProfile = (gDvm.debuggerActive || self->suspendCount
582#if defined(WITH_PROFILER)
583 || gDvm.activeProfilers
584#endif
585 );
Bill Buzbeed7269912009-11-10 14:31:32 -0800586
Ben Cheng79d173c2009-09-29 16:12:51 -0700587 /* Prepare to handle last PC and stage the current PC */
588 const u2 *lastPC = interpState->lastPC;
589 interpState->lastPC = pc;
590
Bill Buzbeef9f33282009-11-22 12:45:30 -0800591#if defined(WITH_SELF_VERIFICATION)
592 /*
593 * We can't allow some instructions to be executed twice, and so they
594 * must not appear in any translations. End the trace before they
595 * are inlcluded.
596 */
597 if (lastPC && interpState->jitState == kJitTSelect) {
598 DecodedInstruction decInsn;
599 dexDecodeInstruction(gDvm.instrFormat, lastPC, &decInsn);
600 if (selfVerificationPuntOps(&decInsn)) {
601 interpState->jitState = kJitTSelectEnd;
602 }
603 }
604#endif
605
Ben Chengba4fc8b2009-06-01 13:00:29 -0700606 switch (interpState->jitState) {
607 char* nopStr;
608 int target;
609 int offset;
610 DecodedInstruction decInsn;
611 case kJitTSelect:
Ben Chengdc84bb22009-10-02 12:58:52 -0700612 /* First instruction - just remember the PC and exit */
613 if (lastPC == NULL) break;
Ben Cheng79d173c2009-09-29 16:12:51 -0700614 /* Grow the trace around the last PC if jitState is kJitTSelect */
615 dexDecodeInstruction(gDvm.instrFormat, lastPC, &decInsn);
Ben Cheng6c10a972009-10-29 14:39:18 -0700616
617 /*
618 * Treat {PACKED,SPARSE}_SWITCH as trace-ending instructions due
619 * to the amount of space it takes to generate the chaining
620 * cells.
621 */
622 if (interpState->totalTraceLen != 0 &&
623 (decInsn.opCode == OP_PACKED_SWITCH ||
624 decInsn.opCode == OP_SPARSE_SWITCH)) {
625 interpState->jitState = kJitTSelectEnd;
626 break;
627 }
628
Bill Buzbeef9f33282009-11-22 12:45:30 -0800629
Ben Chengba4fc8b2009-06-01 13:00:29 -0700630#if defined(SHOW_TRACE)
631 LOGD("TraceGen: adding %s",getOpcodeName(decInsn.opCode));
632#endif
633 flags = dexGetInstrFlags(gDvm.instrFlags, decInsn.opCode);
Ben Cheng79d173c2009-09-29 16:12:51 -0700634 len = dexGetInstrOrTableWidthAbs(gDvm.instrWidth, lastPC);
635 offset = lastPC - interpState->method->insns;
636 assert((unsigned) offset <
637 dvmGetMethodInsnsSize(interpState->method));
638 if (lastPC != interpState->currRunHead + interpState->currRunLen) {
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700639 int currTraceRun;
640 /* We need to start a new trace run */
641 currTraceRun = ++interpState->currTraceRun;
642 interpState->currRunLen = 0;
Ben Cheng79d173c2009-09-29 16:12:51 -0700643 interpState->currRunHead = (u2*)lastPC;
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700644 interpState->trace[currTraceRun].frag.startOffset = offset;
645 interpState->trace[currTraceRun].frag.numInsts = 0;
646 interpState->trace[currTraceRun].frag.runEnd = false;
647 interpState->trace[currTraceRun].frag.hint = kJitHintNone;
648 }
649 interpState->trace[interpState->currTraceRun].frag.numInsts++;
650 interpState->totalTraceLen++;
651 interpState->currRunLen += len;
Ben Cheng79d173c2009-09-29 16:12:51 -0700652
653 /* Will probably never hit this with the current trace buildier */
654 if (interpState->currTraceRun == (MAX_JIT_RUN_LEN - 1)) {
655 interpState->jitState = kJitTSelectEnd;
656 }
657
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700658 if ( ((flags & kInstrUnconditional) == 0) &&
Bill Buzbeef4ce16f2009-07-28 13:28:25 -0700659 /* don't end trace on INVOKE_DIRECT_EMPTY */
660 (decInsn.opCode != OP_INVOKE_DIRECT_EMPTY) &&
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700661 ((flags & (kInstrCanBranch |
662 kInstrCanSwitch |
663 kInstrCanReturn |
664 kInstrInvoke)) != 0)) {
665 interpState->jitState = kJitTSelectEnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700666#if defined(SHOW_TRACE)
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700667 LOGD("TraceGen: ending on %s, basic block end",
668 getOpcodeName(decInsn.opCode));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700669#endif
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700670 }
Bill Buzbee2ce8a6c2009-12-03 15:09:32 -0800671 /* Break on throw or self-loop */
Bill Buzbee324b3ac2009-12-04 13:17:36 -0800672 if ((decInsn.opCode == OP_THROW) || (lastPC == pc)){
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700673 interpState->jitState = kJitTSelectEnd;
674 }
675 if (interpState->totalTraceLen >= JIT_MAX_TRACE_LEN) {
676 interpState->jitState = kJitTSelectEnd;
677 }
678 if (debugOrProfile) {
679 interpState->jitState = kJitTSelectAbort;
680 switchInterp = !debugOrProfile;
681 break;
682 }
683 if ((flags & kInstrCanReturn) != kInstrCanReturn) {
684 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700685 }
686 /* NOTE: intentional fallthrough for returns */
687 case kJitTSelectEnd:
688 {
689 if (interpState->totalTraceLen == 0) {
Bill Buzbeed7269912009-11-10 14:31:32 -0800690 /* Bad trace - mark as untranslatable */
691 dvmJitAbortTraceSelect(interpState);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700692 switchInterp = !debugOrProfile;
693 break;
694 }
695 JitTraceDescription* desc =
696 (JitTraceDescription*)malloc(sizeof(JitTraceDescription) +
697 sizeof(JitTraceRun) * (interpState->currTraceRun+1));
698 if (desc == NULL) {
699 LOGE("Out of memory in trace selection");
700 dvmJitStopTranslationRequests();
701 interpState->jitState = kJitTSelectAbort;
Bill Buzbeed7269912009-11-10 14:31:32 -0800702 dvmJitAbortTraceSelect(interpState);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700703 switchInterp = !debugOrProfile;
704 break;
705 }
706 interpState->trace[interpState->currTraceRun].frag.runEnd =
707 true;
708 interpState->jitState = kJitNormal;
709 desc->method = interpState->method;
710 memcpy((char*)&(desc->trace[0]),
711 (char*)&(interpState->trace[0]),
712 sizeof(JitTraceRun) * (interpState->currTraceRun+1));
713#if defined(SHOW_TRACE)
714 LOGD("TraceGen: trace done, adding to queue");
715#endif
716 dvmCompilerWorkEnqueue(
717 interpState->currTraceHead,kWorkOrderTrace,desc);
Bill Buzbeed7269912009-11-10 14:31:32 -0800718 setTraceConstruction(
719 dvmJitLookupAndAdd(interpState->currTraceHead), false);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700720 if (gDvmJit.blockingMode) {
721 dvmCompilerDrainQueue();
722 }
723 switchInterp = !debugOrProfile;
724 }
725 break;
726 case kJitSingleStep:
727 interpState->jitState = kJitSingleStepEnd;
728 break;
729 case kJitSingleStepEnd:
730 interpState->entryPoint = kInterpEntryResume;
731 switchInterp = !debugOrProfile;
732 break;
733 case kJitTSelectAbort:
734#if defined(SHOW_TRACE)
735 LOGD("TraceGen: trace abort");
736#endif
Bill Buzbeed7269912009-11-10 14:31:32 -0800737 dvmJitAbortTraceSelect(interpState);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700738 interpState->jitState = kJitNormal;
739 switchInterp = !debugOrProfile;
740 break;
741 case kJitNormal:
Ben Cheng38329f52009-07-07 14:19:20 -0700742 switchInterp = !debugOrProfile;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700743 break;
Jeff Hao97319a82009-08-12 16:57:15 -0700744#if defined(WITH_SELF_VERIFICATION)
745 case kJitSelfVerification:
746 if (selfVerificationDebugInterp(pc, self)) {
747 interpState->jitState = kJitNormal;
748 switchInterp = !debugOrProfile;
749 }
750 break;
751#endif
Ben Chenged79ff02009-10-13 13:26:40 -0700752 /* If JIT is off stay out of interpreter selections */
753 case kJitOff:
754 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700755 default:
Ben Cheng9c147b82009-10-07 16:41:46 -0700756 if (!debugOrProfile) {
757 LOGE("Unexpected JIT state: %d", interpState->jitState);
758 dvmAbort();
759 }
760 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700761 }
762 return switchInterp;
763}
764
Ben Chengccd6c012009-10-15 14:52:45 -0700765JitEntry *dvmFindJitEntry(const u2* pc)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700766{
767 int idx = dvmJitHash(pc);
768
769 /* Expect a high hit rate on 1st shot */
770 if (gDvmJit.pJitEntryTable[idx].dPC == pc)
771 return &gDvmJit.pJitEntryTable[idx];
772 else {
Bill Buzbee27176222009-06-09 09:20:16 -0700773 int chainEndMarker = gDvmJit.jitTableSize;
Bill Buzbee716f1202009-07-23 13:22:09 -0700774 while (gDvmJit.pJitEntryTable[idx].u.info.chain != chainEndMarker) {
775 idx = gDvmJit.pJitEntryTable[idx].u.info.chain;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700776 if (gDvmJit.pJitEntryTable[idx].dPC == pc)
777 return &gDvmJit.pJitEntryTable[idx];
778 }
779 }
780 return NULL;
781}
782
Bill Buzbee27176222009-06-09 09:20:16 -0700783/*
Ben Chengba4fc8b2009-06-01 13:00:29 -0700784 * If a translated code address exists for the davik byte code
785 * pointer return it. This routine needs to be fast.
786 */
787void* dvmJitGetCodeAddr(const u2* dPC)
788{
789 int idx = dvmJitHash(dPC);
790
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700791 /* If anything is suspended, don't re-enter the code cache */
792 if (gDvm.sumThreadSuspendCount > 0) {
793 return NULL;
794 }
795
Ben Chengba4fc8b2009-06-01 13:00:29 -0700796 /* Expect a high hit rate on 1st shot */
797 if (gDvmJit.pJitEntryTable[idx].dPC == dPC) {
798#if defined(EXIT_STATS)
799 gDvmJit.addrLookupsFound++;
800#endif
801 return gDvmJit.pJitEntryTable[idx].codeAddress;
802 } else {
Bill Buzbee27176222009-06-09 09:20:16 -0700803 int chainEndMarker = gDvmJit.jitTableSize;
Bill Buzbee716f1202009-07-23 13:22:09 -0700804 while (gDvmJit.pJitEntryTable[idx].u.info.chain != chainEndMarker) {
805 idx = gDvmJit.pJitEntryTable[idx].u.info.chain;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700806 if (gDvmJit.pJitEntryTable[idx].dPC == dPC) {
807#if defined(EXIT_STATS)
808 gDvmJit.addrLookupsFound++;
809#endif
810 return gDvmJit.pJitEntryTable[idx].codeAddress;
811 }
812 }
813 }
814#if defined(EXIT_STATS)
815 gDvmJit.addrLookupsNotFound++;
816#endif
817 return NULL;
818}
819
820/*
Bill Buzbee716f1202009-07-23 13:22:09 -0700821 * Find an entry in the JitTable, creating if necessary.
822 * Returns null if table is full.
823 */
824JitEntry *dvmJitLookupAndAdd(const u2* dPC)
825{
826 u4 chainEndMarker = gDvmJit.jitTableSize;
827 u4 idx = dvmJitHash(dPC);
828
829 /* Walk the bucket chain to find an exact match for our PC */
830 while ((gDvmJit.pJitEntryTable[idx].u.info.chain != chainEndMarker) &&
831 (gDvmJit.pJitEntryTable[idx].dPC != dPC)) {
832 idx = gDvmJit.pJitEntryTable[idx].u.info.chain;
833 }
834
835 if (gDvmJit.pJitEntryTable[idx].dPC != dPC) {
836 /*
837 * No match. Aquire jitTableLock and find the last
838 * slot in the chain. Possibly continue the chain walk in case
839 * some other thread allocated the slot we were looking
840 * at previuosly (perhaps even the dPC we're trying to enter).
841 */
842 dvmLockMutex(&gDvmJit.tableLock);
843 /*
844 * At this point, if .dPC is NULL, then the slot we're
845 * looking at is the target slot from the primary hash
846 * (the simple, and common case). Otherwise we're going
847 * to have to find a free slot and chain it.
848 */
849 MEM_BARRIER(); /* Make sure we reload [].dPC after lock */
850 if (gDvmJit.pJitEntryTable[idx].dPC != NULL) {
851 u4 prev;
852 while (gDvmJit.pJitEntryTable[idx].u.info.chain != chainEndMarker) {
853 if (gDvmJit.pJitEntryTable[idx].dPC == dPC) {
854 /* Another thread got there first for this dPC */
855 dvmUnlockMutex(&gDvmJit.tableLock);
856 return &gDvmJit.pJitEntryTable[idx];
857 }
858 idx = gDvmJit.pJitEntryTable[idx].u.info.chain;
859 }
860 /* Here, idx should be pointing to the last cell of an
861 * active chain whose last member contains a valid dPC */
862 assert(gDvmJit.pJitEntryTable[idx].dPC != NULL);
863 /* Linear walk to find a free cell and add it to the end */
864 prev = idx;
865 while (true) {
866 idx++;
867 if (idx == chainEndMarker)
868 idx = 0; /* Wraparound */
869 if ((gDvmJit.pJitEntryTable[idx].dPC == NULL) ||
870 (idx == prev))
871 break;
872 }
873 if (idx != prev) {
874 JitEntryInfoUnion oldValue;
875 JitEntryInfoUnion newValue;
876 /*
877 * Although we hold the lock so that noone else will
878 * be trying to update a chain field, the other fields
879 * packed into the word may be in use by other threads.
880 */
881 do {
882 oldValue = gDvmJit.pJitEntryTable[prev].u;
883 newValue = oldValue;
884 newValue.info.chain = idx;
885 } while (!ATOMIC_CMP_SWAP(
886 &gDvmJit.pJitEntryTable[prev].u.infoWord,
887 oldValue.infoWord, newValue.infoWord));
888 }
889 }
890 if (gDvmJit.pJitEntryTable[idx].dPC == NULL) {
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800891 /*
892 * Initialize codeAddress and allocate the slot. Must
893 * happen in this order (ince dPC is set, the entry is live.
894 */
Bill Buzbee716f1202009-07-23 13:22:09 -0700895 gDvmJit.pJitEntryTable[idx].dPC = dPC;
896 gDvmJit.jitTableEntriesUsed++;
897 } else {
898 /* Table is full */
899 idx = chainEndMarker;
900 }
901 dvmUnlockMutex(&gDvmJit.tableLock);
902 }
903 return (idx == chainEndMarker) ? NULL : &gDvmJit.pJitEntryTable[idx];
904}
905/*
Ben Chengba4fc8b2009-06-01 13:00:29 -0700906 * Register the translated code pointer into the JitTable.
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800907 * NOTE: Once a codeAddress field transitions from initial state to
Ben Chengba4fc8b2009-06-01 13:00:29 -0700908 * JIT'd code, it must not be altered without first halting all
Bill Buzbee716f1202009-07-23 13:22:09 -0700909 * threads. This routine should only be called by the compiler
910 * thread.
Ben Chengba4fc8b2009-06-01 13:00:29 -0700911 */
Bill Buzbee716f1202009-07-23 13:22:09 -0700912void dvmJitSetCodeAddr(const u2* dPC, void *nPC, JitInstructionSetType set) {
913 JitEntryInfoUnion oldValue;
914 JitEntryInfoUnion newValue;
915 JitEntry *jitEntry = dvmJitLookupAndAdd(dPC);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700916 assert(jitEntry);
Bill Buzbee716f1202009-07-23 13:22:09 -0700917 /* Note: order of update is important */
918 do {
919 oldValue = jitEntry->u;
920 newValue = oldValue;
921 newValue.info.instructionSet = set;
922 } while (!ATOMIC_CMP_SWAP(
923 &jitEntry->u.infoWord,
924 oldValue.infoWord, newValue.infoWord));
925 jitEntry->codeAddress = nPC;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700926}
927
928/*
929 * Determine if valid trace-bulding request is active. Return true
930 * if we need to abort and switch back to the fast interpreter, false
931 * otherwise. NOTE: may be called even when trace selection is not being
932 * requested
933 */
Ben Chengba4fc8b2009-06-01 13:00:29 -0700934bool dvmJitCheckTraceRequest(Thread* self, InterpState* interpState)
935{
Bill Buzbee48f18242009-06-19 16:02:27 -0700936 bool res = false; /* Assume success */
937 int i;
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800938 /*
939 * If previous trace-building attempt failed, force it's head to be
940 * interpret-only.
941 */
Ben Chengba4fc8b2009-06-01 13:00:29 -0700942 if (gDvmJit.pJitEntryTable != NULL) {
Bill Buzbee48f18242009-06-19 16:02:27 -0700943 /* Two-level filtering scheme */
944 for (i=0; i< JIT_TRACE_THRESH_FILTER_SIZE; i++) {
945 if (interpState->pc == interpState->threshFilter[i]) {
946 break;
947 }
948 }
949 if (i == JIT_TRACE_THRESH_FILTER_SIZE) {
950 /*
951 * Use random replacement policy - otherwise we could miss a large
952 * loop that contains more traces than the size of our filter array.
953 */
954 i = rand() % JIT_TRACE_THRESH_FILTER_SIZE;
955 interpState->threshFilter[i] = interpState->pc;
956 res = true;
957 }
Bill Buzbeed7269912009-11-10 14:31:32 -0800958
Bill Buzbeef9f33282009-11-22 12:45:30 -0800959 /* If stress mode (threshold <= 6), always translate */
960 res &= (gDvmJit.threshold > 6);
Bill Buzbeed7269912009-11-10 14:31:32 -0800961
Ben Chengba4fc8b2009-06-01 13:00:29 -0700962 /*
963 * If the compiler is backlogged, or if a debugger or profiler is
964 * active, cancel any JIT actions
965 */
Bill Buzbee9a8c75a2009-11-08 14:31:20 -0800966 if (res || (gDvmJit.compilerQueueLength >= gDvmJit.compilerHighWater)
967 || gDvm.debuggerActive || self->suspendCount
Ben Chengba4fc8b2009-06-01 13:00:29 -0700968#if defined(WITH_PROFILER)
969 || gDvm.activeProfilers
970#endif
971 ) {
972 if (interpState->jitState != kJitOff) {
973 interpState->jitState = kJitNormal;
974 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700975 } else if (interpState->jitState == kJitTSelectRequest) {
Bill Buzbee716f1202009-07-23 13:22:09 -0700976 JitEntry *slot = dvmJitLookupAndAdd(interpState->pc);
977 if (slot == NULL) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700978 /*
Bill Buzbee716f1202009-07-23 13:22:09 -0700979 * Table is full. This should have been
980 * detected by the compiler thread and the table
981 * resized before we run into it here. Assume bad things
982 * are afoot and disable profiling.
Ben Chengba4fc8b2009-06-01 13:00:29 -0700983 */
984 interpState->jitState = kJitTSelectAbort;
Bill Buzbee716f1202009-07-23 13:22:09 -0700985 LOGD("JIT: JitTable full, disabling profiling");
986 dvmJitStopTranslationRequests();
Bill Buzbeed7269912009-11-10 14:31:32 -0800987 } else if (slot->u.info.traceConstruction) {
988 /*
989 * Trace already request in progress, but most likely it
990 * aborted without cleaning up. Assume the worst and
991 * mark trace head as untranslatable. If we're wrong,
992 * the compiler thread will correct the entry when the
993 * translation is completed. The downside here is that
994 * some existing translation may chain to the interpret-only
995 * template instead of the real translation during this
996 * window. Performance, but not correctness, issue.
997 */
998 interpState->jitState = kJitTSelectAbort;
999 resetTracehead(interpState, slot);
1000 } else if (slot->codeAddress) {
1001 /* Nothing to do here - just return */
Bill Buzbee716f1202009-07-23 13:22:09 -07001002 interpState->jitState = kJitTSelectAbort;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001003 } else {
Bill Buzbeed7269912009-11-10 14:31:32 -08001004 /*
1005 * Mark request. Note, we are not guaranteed exclusivity
1006 * here. A window exists for another thread to be
1007 * attempting to build this same trace. Rather than
1008 * bear the cost of locking, we'll just allow that to
1009 * happen. The compiler thread, if it chooses, can
1010 * discard redundant requests.
1011 */
1012 setTraceConstruction(slot, true);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001013 }
1014 }
1015 switch (interpState->jitState) {
1016 case kJitTSelectRequest:
1017 interpState->jitState = kJitTSelect;
1018 interpState->currTraceHead = interpState->pc;
1019 interpState->currTraceRun = 0;
1020 interpState->totalTraceLen = 0;
1021 interpState->currRunHead = interpState->pc;
1022 interpState->currRunLen = 0;
1023 interpState->trace[0].frag.startOffset =
1024 interpState->pc - interpState->method->insns;
1025 interpState->trace[0].frag.numInsts = 0;
1026 interpState->trace[0].frag.runEnd = false;
1027 interpState->trace[0].frag.hint = kJitHintNone;
Ben Cheng79d173c2009-09-29 16:12:51 -07001028 interpState->lastPC = 0;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001029 break;
1030 case kJitTSelect:
1031 case kJitTSelectAbort:
1032 res = true;
1033 case kJitSingleStep:
1034 case kJitSingleStepEnd:
1035 case kJitOff:
1036 case kJitNormal:
Jeff Hao97319a82009-08-12 16:57:15 -07001037#if defined(WITH_SELF_VERIFICATION)
1038 case kJitSelfVerification:
1039#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07001040 break;
1041 default:
Ben Cheng9c147b82009-10-07 16:41:46 -07001042 LOGE("Unexpected JIT state: %d", interpState->jitState);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001043 dvmAbort();
1044 }
1045 }
1046 return res;
1047}
1048
Bill Buzbee27176222009-06-09 09:20:16 -07001049/*
1050 * Resizes the JitTable. Must be a power of 2, and returns true on failure.
1051 * Stops all threads, and thus is a heavyweight operation.
1052 */
1053bool dvmJitResizeJitTable( unsigned int size )
1054{
Bill Buzbee716f1202009-07-23 13:22:09 -07001055 JitEntry *pNewTable;
1056 JitEntry *pOldTable;
Bill Buzbee27176222009-06-09 09:20:16 -07001057 u4 newMask;
Bill Buzbee716f1202009-07-23 13:22:09 -07001058 unsigned int oldSize;
Bill Buzbee27176222009-06-09 09:20:16 -07001059 unsigned int i;
1060
Ben Cheng3f02aa42009-08-14 13:52:09 -07001061 assert(gDvmJit.pJitEntryTable != NULL);
Bill Buzbee27176222009-06-09 09:20:16 -07001062 assert(size && !(size & (size - 1))); /* Is power of 2? */
1063
1064 LOGD("Jit: resizing JitTable from %d to %d", gDvmJit.jitTableSize, size);
1065
1066 newMask = size - 1;
1067
1068 if (size <= gDvmJit.jitTableSize) {
1069 return true;
1070 }
1071
Bill Buzbee716f1202009-07-23 13:22:09 -07001072 pNewTable = (JitEntry*)calloc(size, sizeof(*pNewTable));
Bill Buzbee27176222009-06-09 09:20:16 -07001073 if (pNewTable == NULL) {
1074 return true;
1075 }
1076 for (i=0; i< size; i++) {
Bill Buzbee716f1202009-07-23 13:22:09 -07001077 pNewTable[i].u.info.chain = size; /* Initialize chain termination */
Bill Buzbee27176222009-06-09 09:20:16 -07001078 }
1079
1080 /* Stop all other interpreting/jit'ng threads */
Ben Chenga8e64a72009-10-20 13:01:36 -07001081 dvmSuspendAllThreads(SUSPEND_FOR_TBL_RESIZE);
Bill Buzbee27176222009-06-09 09:20:16 -07001082
Bill Buzbee716f1202009-07-23 13:22:09 -07001083 pOldTable = gDvmJit.pJitEntryTable;
1084 oldSize = gDvmJit.jitTableSize;
Bill Buzbee27176222009-06-09 09:20:16 -07001085
1086 dvmLockMutex(&gDvmJit.tableLock);
Bill Buzbee27176222009-06-09 09:20:16 -07001087 gDvmJit.pJitEntryTable = pNewTable;
1088 gDvmJit.jitTableSize = size;
1089 gDvmJit.jitTableMask = size - 1;
Bill Buzbee716f1202009-07-23 13:22:09 -07001090 gDvmJit.jitTableEntriesUsed = 0;
Bill Buzbee27176222009-06-09 09:20:16 -07001091 dvmUnlockMutex(&gDvmJit.tableLock);
1092
Bill Buzbee716f1202009-07-23 13:22:09 -07001093 for (i=0; i < oldSize; i++) {
1094 if (pOldTable[i].dPC) {
1095 JitEntry *p;
1096 u2 chain;
1097 p = dvmJitLookupAndAdd(pOldTable[i].dPC);
1098 p->dPC = pOldTable[i].dPC;
1099 /*
1100 * Compiler thread may have just updated the new entry's
1101 * code address field, so don't blindly copy null.
1102 */
1103 if (pOldTable[i].codeAddress != NULL) {
1104 p->codeAddress = pOldTable[i].codeAddress;
1105 }
1106 /* We need to preserve the new chain field, but copy the rest */
1107 dvmLockMutex(&gDvmJit.tableLock);
1108 chain = p->u.info.chain;
1109 p->u = pOldTable[i].u;
1110 p->u.info.chain = chain;
1111 dvmUnlockMutex(&gDvmJit.tableLock);
1112 }
1113 }
1114
1115 free(pOldTable);
1116
Bill Buzbee27176222009-06-09 09:20:16 -07001117 /* Restart the world */
Ben Chenga8e64a72009-10-20 13:01:36 -07001118 dvmResumeAllThreads(SUSPEND_FOR_TBL_RESIZE);
Bill Buzbee27176222009-06-09 09:20:16 -07001119
1120 return false;
1121}
1122
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001123/*
1124 * Float/double conversion requires clamping to min and max of integer form. If
1125 * target doesn't support this normally, use these.
1126 */
1127s8 dvmJitd2l(double d)
1128{
Bill Buzbee9727c3d2009-08-01 11:32:36 -07001129 static const double kMaxLong = (double)(s8)0x7fffffffffffffffULL;
1130 static const double kMinLong = (double)(s8)0x8000000000000000ULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001131 if (d >= kMaxLong)
Bill Buzbee9727c3d2009-08-01 11:32:36 -07001132 return (s8)0x7fffffffffffffffULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001133 else if (d <= kMinLong)
Bill Buzbee9727c3d2009-08-01 11:32:36 -07001134 return (s8)0x8000000000000000ULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001135 else if (d != d) // NaN case
1136 return 0;
1137 else
1138 return (s8)d;
1139}
1140
1141s8 dvmJitf2l(float f)
1142{
Bill Buzbee9727c3d2009-08-01 11:32:36 -07001143 static const float kMaxLong = (float)(s8)0x7fffffffffffffffULL;
1144 static const float kMinLong = (float)(s8)0x8000000000000000ULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001145 if (f >= kMaxLong)
Bill Buzbee9727c3d2009-08-01 11:32:36 -07001146 return (s8)0x7fffffffffffffffULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001147 else if (f <= kMinLong)
Bill Buzbee9727c3d2009-08-01 11:32:36 -07001148 return (s8)0x8000000000000000ULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001149 else if (f != f) // NaN case
1150 return 0;
1151 else
1152 return (s8)f;
1153}
1154
Bill Buzbee27176222009-06-09 09:20:16 -07001155
Ben Chengba4fc8b2009-06-01 13:00:29 -07001156#endif /* WITH_JIT */