OSDN Git Service

Fix a race condition in JIT state refresh under debugging / misc code cleanup.
[android-x86/dalvik.git] / vm / interp / Jit.c
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifdef WITH_JIT
17
18 /*
19  * Target independent portion of Android's Jit
20  */
21
22 #include "Dalvik.h"
23 #include "Jit.h"
24
25
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"
32 #include "compiler/CompilerUtility.h"
33 #include "compiler/CompilerIR.h"
34 #include <errno.h>
35
36 #if defined(WITH_SELF_VERIFICATION)
37 /* Allocate space for per-thread ShadowSpace data structures */
38 void* dvmSelfVerificationShadowSpaceAlloc(Thread* self)
39 {
40     self->shadowSpace = (ShadowSpace*) calloc(1, sizeof(ShadowSpace));
41     if (self->shadowSpace == NULL)
42         return NULL;
43
44     self->shadowSpace->registerSpaceSize = REG_SPACE;
45     self->shadowSpace->registerSpace =
46         (int*) calloc(self->shadowSpace->registerSpaceSize, sizeof(int));
47
48     return self->shadowSpace->registerSpace;
49 }
50
51 /* Free per-thread ShadowSpace data structures */
52 void dvmSelfVerificationShadowSpaceFree(Thread* self)
53 {
54     free(self->shadowSpace->registerSpace);
55     free(self->shadowSpace);
56 }
57
58 /*
59  * Save out PC, FP, InterpState, and registers to shadow space.
60  * Return a pointer to the shadow space for JIT to use.
61  */
62 void* dvmSelfVerificationSaveState(const u2* pc, const void* fp,
63                                    InterpState* interpState, int targetTrace)
64 {
65     Thread *self = dvmThreadSelf();
66     ShadowSpace *shadowSpace = self->shadowSpace;
67     unsigned preBytes = interpState->method->outsSize*4 + sizeof(StackSaveArea);
68     unsigned 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 **********");
77         LOGD("PC: 0x%x FP: 0x%x", (int)pc, (int)fp);
78     }
79     shadowSpace->selfVerificationState = kSVSStart;
80
81     if (interpState->entryPoint == kInterpEntryResume) {
82         interpState->entryPoint = kInterpEntryInstr;
83 #if 0
84         /* Tracking the success rate of resume after single-stepping */
85         if (interpState->jitResumeDPC == pc) {
86             LOGD("SV single step resumed at %p", pc);
87         }
88         else {
89             LOGD("real %p DPC %p NPC %p", pc, interpState->jitResumeDPC,
90                  interpState->jitResumeNPC);
91         }
92 #endif
93     }
94
95     // Dynamically grow shadow register space if necessary
96     if (preBytes + postBytes > shadowSpace->registerSpaceSize * sizeof(u4)) {
97         free(shadowSpace->registerSpace);
98         shadowSpace->registerSpaceSize = (preBytes + postBytes) / sizeof(u4);
99         shadowSpace->registerSpace =
100             (int*) calloc(shadowSpace->registerSpaceSize, sizeof(u4));
101     }
102
103     // Remember original state
104     shadowSpace->startPC = pc;
105     shadowSpace->fp = fp;
106     shadowSpace->glue = interpState;
107     /*
108      * Store the original method here in case the trace ends with a
109      * return/invoke, the last method.
110      */
111     shadowSpace->method = interpState->method;
112     shadowSpace->shadowFP = shadowSpace->registerSpace +
113                             shadowSpace->registerSpaceSize - postBytes/4;
114
115     // Create a copy of the InterpState
116     memcpy(&(shadowSpace->interpState), interpState, sizeof(InterpState));
117     shadowSpace->interpState.fp = shadowSpace->shadowFP;
118     shadowSpace->interpState.interpStackEnd = (u1*)shadowSpace->registerSpace;
119
120     // Create a copy of the stack
121     memcpy(((char*)shadowSpace->shadowFP)-preBytes, ((char*)fp)-preBytes,
122         preBytes+postBytes);
123
124     // Setup the shadowed heap space
125     shadowSpace->heapSpaceTail = shadowSpace->heapSpace;
126
127     // Reset trace length
128     shadowSpace->traceLength = 0;
129
130     return shadowSpace;
131 }
132
133 /*
134  * Save ending PC, FP and compiled code exit point to shadow space.
135  * Return a pointer to the shadow space for JIT to restore state.
136  */
137 void* dvmSelfVerificationRestoreState(const u2* pc, const void* fp,
138                                       SelfVerificationState exitPoint)
139 {
140     Thread *self = dvmThreadSelf();
141     ShadowSpace *shadowSpace = self->shadowSpace;
142     // Official InterpState structure
143     InterpState *realGlue = shadowSpace->glue;
144     shadowSpace->endPC = pc;
145     shadowSpace->endShadowFP = fp;
146
147     //LOGD("### selfVerificationRestoreState(%d) pc: 0x%x fp: 0x%x endPC: 0x%x",
148     //    self->threadId, (int)shadowSpace->startPC, (int)shadowSpace->fp,
149     //    (int)pc);
150
151     if (shadowSpace->selfVerificationState != kSVSStart) {
152         LOGD("~~~ Restore: INCORRECT PREVIOUS STATE(%d): %d",
153             self->threadId, shadowSpace->selfVerificationState);
154         LOGD("********** SHADOW STATE DUMP **********");
155         LOGD("Dalvik PC: 0x%x endPC: 0x%x", (int)shadowSpace->startPC,
156             (int)shadowSpace->endPC);
157         LOGD("Interp FP: 0x%x", (int)shadowSpace->fp);
158         LOGD("Shadow FP: 0x%x endFP: 0x%x", (int)shadowSpace->shadowFP,
159             (int)shadowSpace->endShadowFP);
160     }
161
162     // Move the resume [ND]PC from the shadow space to the real space so that
163     // the debug interpreter can return to the translation
164     if (exitPoint == kSVSSingleStep) {
165         realGlue->jitResumeNPC = shadowSpace->interpState.jitResumeNPC;
166         realGlue->jitResumeDPC = shadowSpace->interpState.jitResumeDPC;
167     } else {
168         realGlue->jitResumeNPC = NULL;
169         realGlue->jitResumeDPC = NULL;
170     }
171
172     // Special case when punting after a single instruction
173     if (exitPoint == kSVSPunt && pc == shadowSpace->startPC) {
174         shadowSpace->selfVerificationState = kSVSIdle;
175     } else {
176         shadowSpace->selfVerificationState = exitPoint;
177     }
178
179     return shadowSpace;
180 }
181
182 /* Print contents of virtual registers */
183 static void selfVerificationPrintRegisters(int* addr, int* addrRef,
184                                            int numWords)
185 {
186     int i;
187     for (i = 0; i < numWords; i++) {
188         LOGD("(v%d) 0x%8x%s", i, addr[i], addr[i] != addrRef[i] ? " X" : "");
189     }
190 }
191
192 /* Print values maintained in shadowSpace */
193 static void selfVerificationDumpState(const u2* pc, Thread* self)
194 {
195     ShadowSpace* shadowSpace = self->shadowSpace;
196     StackSaveArea* stackSave = SAVEAREA_FROM_FP(self->curFrame);
197     int frameBytes = (int) shadowSpace->registerSpace +
198                      shadowSpace->registerSpaceSize*4 -
199                      (int) shadowSpace->shadowFP;
200     int localRegs = 0;
201     int frameBytes2 = 0;
202     if (self->curFrame < shadowSpace->fp) {
203         localRegs = (stackSave->method->registersSize -
204                      stackSave->method->insSize)*4;
205         frameBytes2 = (int) shadowSpace->fp - (int) self->curFrame - localRegs;
206     }
207     LOGD("********** SHADOW STATE DUMP **********");
208     LOGD("CurrentPC: 0x%x, Offset: 0x%04x", (int)pc,
209         (int)(pc - stackSave->method->insns));
210     LOGD("Class: %s", shadowSpace->method->clazz->descriptor);
211     LOGD("Method: %s", shadowSpace->method->name);
212     LOGD("Dalvik PC: 0x%x endPC: 0x%x", (int)shadowSpace->startPC,
213         (int)shadowSpace->endPC);
214     LOGD("Interp FP: 0x%x endFP: 0x%x", (int)shadowSpace->fp,
215         (int)self->curFrame);
216     LOGD("Shadow FP: 0x%x endFP: 0x%x", (int)shadowSpace->shadowFP,
217         (int)shadowSpace->endShadowFP);
218     LOGD("Frame1 Bytes: %d Frame2 Local: %d Bytes: %d", frameBytes,
219         localRegs, frameBytes2);
220     LOGD("Trace length: %d State: %d", shadowSpace->traceLength,
221         shadowSpace->selfVerificationState);
222 }
223
224 /* Print decoded instructions in the current trace */
225 static void selfVerificationDumpTrace(const u2* pc, Thread* self)
226 {
227     ShadowSpace* shadowSpace = self->shadowSpace;
228     StackSaveArea* stackSave = SAVEAREA_FROM_FP(self->curFrame);
229     int i, addr, offset;
230     DecodedInstruction *decInsn;
231
232     LOGD("********** SHADOW TRACE DUMP **********");
233     for (i = 0; i < shadowSpace->traceLength; i++) {
234         addr = shadowSpace->trace[i].addr;
235         offset =  (int)((u2*)addr - stackSave->method->insns);
236         decInsn = &(shadowSpace->trace[i].decInsn);
237         /* Not properly decoding instruction, some registers may be garbage */
238         LOGD("0x%x: (0x%04x) %s", addr, offset, getOpcodeName(decInsn->opCode));
239     }
240 }
241
242 /* Code is forced into this spin loop when a divergence is detected */
243 static void selfVerificationSpinLoop(ShadowSpace *shadowSpace)
244 {
245     const u2 *startPC = shadowSpace->startPC;
246     JitTraceDescription* desc = dvmCopyTraceDescriptor(startPC, NULL);
247     if (desc) {
248         dvmCompilerWorkEnqueue(startPC, kWorkOrderTraceDebug, desc);
249         /*
250          * This function effectively terminates the VM right here, so not
251          * freeing the desc pointer when the enqueuing fails is acceptable.
252          */
253     }
254     gDvmJit.selfVerificationSpin = true;
255     while(gDvmJit.selfVerificationSpin) sleep(10);
256 }
257
258 /* Manage self verification while in the debug interpreter */
259 static bool selfVerificationDebugInterp(const u2* pc, Thread* self,
260                                         InterpState *interpState)
261 {
262     ShadowSpace *shadowSpace = self->shadowSpace;
263     SelfVerificationState state = shadowSpace->selfVerificationState;
264
265     DecodedInstruction decInsn;
266     dexDecodeInstruction(gDvm.instrFormat, pc, &decInsn);
267
268     //LOGD("### DbgIntp(%d): PC: 0x%x endPC: 0x%x state: %d len: %d %s",
269     //    self->threadId, (int)pc, (int)shadowSpace->endPC, state,
270     //    shadowSpace->traceLength, getOpcodeName(decInsn.opCode));
271
272     if (state == kSVSIdle || state == kSVSStart) {
273         LOGD("~~~ DbgIntrp: INCORRECT PREVIOUS STATE(%d): %d",
274             self->threadId, state);
275         selfVerificationDumpState(pc, self);
276         selfVerificationDumpTrace(pc, self);
277     }
278
279     /*
280      * Skip endPC once when trace has a backward branch. If the SV state is
281      * single step, keep it that way.
282      */
283     if ((state == kSVSBackwardBranch && pc == shadowSpace->endPC) ||
284         (state != kSVSBackwardBranch && state != kSVSSingleStep)) {
285         shadowSpace->selfVerificationState = kSVSDebugInterp;
286     }
287
288     /* Check that the current pc is the end of the trace */
289     if ((state == kSVSDebugInterp || state == kSVSSingleStep) &&
290         pc == shadowSpace->endPC) {
291
292         shadowSpace->selfVerificationState = kSVSIdle;
293
294         /* Check register space */
295         int frameBytes = (int) shadowSpace->registerSpace +
296                          shadowSpace->registerSpaceSize*4 -
297                          (int) shadowSpace->shadowFP;
298         if (memcmp(shadowSpace->fp, shadowSpace->shadowFP, frameBytes)) {
299             LOGD("~~~ DbgIntp(%d): REGISTERS DIVERGENCE!", self->threadId);
300             selfVerificationDumpState(pc, self);
301             selfVerificationDumpTrace(pc, self);
302             LOGD("*** Interp Registers: addr: 0x%x bytes: %d",
303                 (int)shadowSpace->fp, frameBytes);
304             selfVerificationPrintRegisters((int*)shadowSpace->fp,
305                                            (int*)shadowSpace->shadowFP,
306                                            frameBytes/4);
307             LOGD("*** Shadow Registers: addr: 0x%x bytes: %d",
308                 (int)shadowSpace->shadowFP, frameBytes);
309             selfVerificationPrintRegisters((int*)shadowSpace->shadowFP,
310                                            (int*)shadowSpace->fp,
311                                            frameBytes/4);
312             selfVerificationSpinLoop(shadowSpace);
313         }
314         /* Check new frame if it exists (invokes only) */
315         if (self->curFrame < shadowSpace->fp) {
316             StackSaveArea* stackSave = SAVEAREA_FROM_FP(self->curFrame);
317             int localRegs = (stackSave->method->registersSize -
318                              stackSave->method->insSize)*4;
319             int frameBytes2 = (int) shadowSpace->fp -
320                               (int) self->curFrame - localRegs;
321             if (memcmp(((char*)self->curFrame)+localRegs,
322                 ((char*)shadowSpace->endShadowFP)+localRegs, frameBytes2)) {
323                 LOGD("~~~ DbgIntp(%d): REGISTERS (FRAME2) DIVERGENCE!",
324                     self->threadId);
325                 selfVerificationDumpState(pc, self);
326                 selfVerificationDumpTrace(pc, self);
327                 LOGD("*** Interp Registers: addr: 0x%x l: %d bytes: %d",
328                     (int)self->curFrame, localRegs, frameBytes2);
329                 selfVerificationPrintRegisters((int*)self->curFrame,
330                                                (int*)shadowSpace->endShadowFP,
331                                                (frameBytes2+localRegs)/4);
332                 LOGD("*** Shadow Registers: addr: 0x%x l: %d bytes: %d",
333                     (int)shadowSpace->endShadowFP, localRegs, frameBytes2);
334                 selfVerificationPrintRegisters((int*)shadowSpace->endShadowFP,
335                                                (int*)self->curFrame,
336                                                (frameBytes2+localRegs)/4);
337                 selfVerificationSpinLoop(shadowSpace);
338             }
339         }
340
341         /* Check memory space */
342         bool memDiff = false;
343         ShadowHeap* heapSpacePtr;
344         for (heapSpacePtr = shadowSpace->heapSpace;
345              heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
346             int memData = *((unsigned int*) heapSpacePtr->addr);
347             if (heapSpacePtr->data != memData) {
348                 LOGD("~~~ DbgIntp(%d): MEMORY DIVERGENCE!", self->threadId);
349                 LOGD("Addr: 0x%x Intrp Data: 0x%x Jit Data: 0x%x",
350                     heapSpacePtr->addr, memData, heapSpacePtr->data);
351                 selfVerificationDumpState(pc, self);
352                 selfVerificationDumpTrace(pc, self);
353                 memDiff = true;
354             }
355         }
356         if (memDiff) selfVerificationSpinLoop(shadowSpace);
357
358         /*
359          * Switch to JIT single step mode to stay in the debug interpreter for
360          * one more instruction
361          */
362         if (state == kSVSSingleStep) {
363             interpState->jitState = kJitSingleStepEnd;
364         }
365         return true;
366
367     /* If end not been reached, make sure max length not exceeded */
368     } else if (shadowSpace->traceLength >= JIT_MAX_TRACE_LEN) {
369         LOGD("~~~ DbgIntp(%d): CONTROL DIVERGENCE!", self->threadId);
370         LOGD("startPC: 0x%x endPC: 0x%x currPC: 0x%x",
371             (int)shadowSpace->startPC, (int)shadowSpace->endPC, (int)pc);
372         selfVerificationDumpState(pc, self);
373         selfVerificationDumpTrace(pc, self);
374         selfVerificationSpinLoop(shadowSpace);
375
376         return true;
377     }
378     /* Log the instruction address and decoded instruction for debug */
379     shadowSpace->trace[shadowSpace->traceLength].addr = (int)pc;
380     shadowSpace->trace[shadowSpace->traceLength].decInsn = decInsn;
381     shadowSpace->traceLength++;
382
383     return false;
384 }
385 #endif
386
387 /*
388  * If one of our fixed tables or the translation buffer fills up,
389  * call this routine to avoid wasting cycles on future translation requests.
390  */
391 void dvmJitStopTranslationRequests()
392 {
393     /*
394      * Note 1: This won't necessarily stop all translation requests, and
395      * operates on a delayed mechanism.  Running threads look to the copy
396      * of this value in their private InterpState structures and won't see
397      * this change until it is refreshed (which happens on interpreter
398      * entry).
399      * Note 2: This is a one-shot memory leak on this table. Because this is a
400      * permanent off switch for Jit profiling, it is a one-time leak of 1K
401      * bytes, and no further attempt will be made to re-allocate it.  Can't
402      * free it because some thread may be holding a reference.
403      */
404     gDvmJit.pProfTable = NULL;
405 }
406
407 #if defined(JIT_STATS)
408 /* Convenience function to increment counter from assembly code */
409 void dvmBumpNoChain(int from)
410 {
411     gDvmJit.noChainExit[from]++;
412 }
413
414 /* Convenience function to increment counter from assembly code */
415 void dvmBumpNormal()
416 {
417     gDvmJit.normalExit++;
418 }
419
420 /* Convenience function to increment counter from assembly code */
421 void dvmBumpPunt(int from)
422 {
423     gDvmJit.puntExit++;
424 }
425 #endif
426
427 /* Dumps debugging & tuning stats to the log */
428 void dvmJitStats()
429 {
430     int i;
431     int hit;
432     int not_hit;
433     int chains;
434     int stubs;
435     if (gDvmJit.pJitEntryTable) {
436         for (i=0, stubs=chains=hit=not_hit=0;
437              i < (int) gDvmJit.jitTableSize;
438              i++) {
439             if (gDvmJit.pJitEntryTable[i].dPC != 0) {
440                 hit++;
441                 if (gDvmJit.pJitEntryTable[i].codeAddress ==
442                       gDvmJit.interpretTemplate)
443                     stubs++;
444             } else
445                 not_hit++;
446             if (gDvmJit.pJitEntryTable[i].u.info.chain != gDvmJit.jitTableSize)
447                 chains++;
448         }
449         LOGD("JIT: table size is %d, entries used is %d",
450              gDvmJit.jitTableSize,  gDvmJit.jitTableEntriesUsed);
451         LOGD("JIT: %d traces, %d slots, %d chains, %d thresh, %s",
452              hit, not_hit + hit, chains, gDvmJit.threshold,
453              gDvmJit.blockingMode ? "Blocking" : "Non-blocking");
454
455 #if defined(JIT_STATS)
456         LOGD("JIT: Lookups: %d hits, %d misses; %d normal, %d punt",
457              gDvmJit.addrLookupsFound, gDvmJit.addrLookupsNotFound,
458              gDvmJit.normalExit, gDvmJit.puntExit);
459         LOGD("JIT: noChainExit: %d IC miss, %d interp callsite, "
460              "%d switch overflow",
461              gDvmJit.noChainExit[kInlineCacheMiss],
462              gDvmJit.noChainExit[kCallsiteInterpreted],
463              gDvmJit.noChainExit[kSwitchOverflow]);
464
465         LOGD("JIT: Invoke: %d mono, %d poly, %d native, %d return",
466              gDvmJit.invokeMonomorphic, gDvmJit.invokePolymorphic,
467              gDvmJit.invokeNative, gDvmJit.returnOp);
468         LOGD("JIT: Total compilation time: %llu ms", gDvmJit.jitTime / 1000);
469         LOGD("JIT: Avg unit compilation time: %llu us",
470              gDvmJit.jitTime / gDvmJit.numCompilations);
471 #endif
472
473         LOGD("JIT: %d Translation chains, %d interp stubs",
474              gDvmJit.translationChains, stubs);
475         if (gDvmJit.profile) {
476             dvmCompilerSortAndPrintTraceProfiles();
477         }
478     }
479 }
480
481
482 void setTraceConstruction(JitEntry *slot, bool value)
483 {
484
485     JitEntryInfoUnion oldValue;
486     JitEntryInfoUnion newValue;
487     do {
488         oldValue = slot->u;
489         newValue = oldValue;
490         newValue.info.traceConstruction = value;
491     } while (!ATOMIC_CMP_SWAP( &slot->u.infoWord,
492              oldValue.infoWord, newValue.infoWord));
493 }
494
495 void resetTracehead(InterpState* interpState, JitEntry *slot)
496 {
497     slot->codeAddress = gDvmJit.interpretTemplate;
498     setTraceConstruction(slot, false);
499 }
500
501 /* Clean up any pending trace builds */
502 void dvmJitAbortTraceSelect(InterpState* interpState)
503 {
504     if (interpState->jitState == kJitTSelect)
505         interpState->jitState = kJitDone;
506 }
507
508 /*
509  * Find an entry in the JitTable, creating if necessary.
510  * Returns null if table is full.
511  */
512 static JitEntry *lookupAndAdd(const u2* dPC, bool callerLocked)
513 {
514     u4 chainEndMarker = gDvmJit.jitTableSize;
515     u4 idx = dvmJitHash(dPC);
516
517     /* Walk the bucket chain to find an exact match for our PC */
518     while ((gDvmJit.pJitEntryTable[idx].u.info.chain != chainEndMarker) &&
519            (gDvmJit.pJitEntryTable[idx].dPC != dPC)) {
520         idx = gDvmJit.pJitEntryTable[idx].u.info.chain;
521     }
522
523     if (gDvmJit.pJitEntryTable[idx].dPC != dPC) {
524         /*
525          * No match.  Aquire jitTableLock and find the last
526          * slot in the chain. Possibly continue the chain walk in case
527          * some other thread allocated the slot we were looking
528          * at previuosly (perhaps even the dPC we're trying to enter).
529          */
530         if (!callerLocked)
531             dvmLockMutex(&gDvmJit.tableLock);
532         /*
533          * At this point, if .dPC is NULL, then the slot we're
534          * looking at is the target slot from the primary hash
535          * (the simple, and common case).  Otherwise we're going
536          * to have to find a free slot and chain it.
537          */
538         MEM_BARRIER(); /* Make sure we reload [].dPC after lock */
539         if (gDvmJit.pJitEntryTable[idx].dPC != NULL) {
540             u4 prev;
541             while (gDvmJit.pJitEntryTable[idx].u.info.chain != chainEndMarker) {
542                 if (gDvmJit.pJitEntryTable[idx].dPC == dPC) {
543                     /* Another thread got there first for this dPC */
544                     if (!callerLocked)
545                         dvmUnlockMutex(&gDvmJit.tableLock);
546                     return &gDvmJit.pJitEntryTable[idx];
547                 }
548                 idx = gDvmJit.pJitEntryTable[idx].u.info.chain;
549             }
550             /* Here, idx should be pointing to the last cell of an
551              * active chain whose last member contains a valid dPC */
552             assert(gDvmJit.pJitEntryTable[idx].dPC != NULL);
553             /* Linear walk to find a free cell and add it to the end */
554             prev = idx;
555             while (true) {
556                 idx++;
557                 if (idx == chainEndMarker)
558                     idx = 0;  /* Wraparound */
559                 if ((gDvmJit.pJitEntryTable[idx].dPC == NULL) ||
560                     (idx == prev))
561                     break;
562             }
563             if (idx != prev) {
564                 JitEntryInfoUnion oldValue;
565                 JitEntryInfoUnion newValue;
566                 /*
567                  * Although we hold the lock so that noone else will
568                  * be trying to update a chain field, the other fields
569                  * packed into the word may be in use by other threads.
570                  */
571                 do {
572                     oldValue = gDvmJit.pJitEntryTable[prev].u;
573                     newValue = oldValue;
574                     newValue.info.chain = idx;
575                 } while (!ATOMIC_CMP_SWAP(
576                          &gDvmJit.pJitEntryTable[prev].u.infoWord,
577                          oldValue.infoWord, newValue.infoWord));
578             }
579         }
580         if (gDvmJit.pJitEntryTable[idx].dPC == NULL) {
581             /*
582              * Initialize codeAddress and allocate the slot.  Must
583              * happen in this order (since dPC is set, the entry is live.
584              */
585             gDvmJit.pJitEntryTable[idx].dPC = dPC;
586             gDvmJit.jitTableEntriesUsed++;
587         } else {
588             /* Table is full */
589             idx = chainEndMarker;
590         }
591         if (!callerLocked)
592             dvmUnlockMutex(&gDvmJit.tableLock);
593     }
594     return (idx == chainEndMarker) ? NULL : &gDvmJit.pJitEntryTable[idx];
595 }
596
597 /*
598  * Adds to the current trace request one instruction at a time, just
599  * before that instruction is interpreted.  This is the primary trace
600  * selection function.  NOTE: return instruction are handled a little
601  * differently.  In general, instructions are "proposed" to be added
602  * to the current trace prior to interpretation.  If the interpreter
603  * then successfully completes the instruction, is will be considered
604  * part of the request.  This allows us to examine machine state prior
605  * to interpretation, and also abort the trace request if the instruction
606  * throws or does something unexpected.  However, return instructions
607  * will cause an immediate end to the translation request - which will
608  * be passed to the compiler before the return completes.  This is done
609  * in response to special handling of returns by the interpreter (and
610  * because returns cannot throw in a way that causes problems for the
611  * translated code.
612  */
613 int dvmCheckJit(const u2* pc, Thread* self, InterpState* interpState)
614 {
615     int flags,i,len;
616     int switchInterp = false;
617     bool debugOrProfile = dvmDebuggerOrProfilerActive();
618
619     /* Prepare to handle last PC and stage the current PC */
620     const u2 *lastPC = interpState->lastPC;
621     interpState->lastPC = pc;
622
623     switch (interpState->jitState) {
624         char* nopStr;
625         int target;
626         int offset;
627         DecodedInstruction decInsn;
628         case kJitTSelect:
629             /* First instruction - just remember the PC and exit */
630             if (lastPC == NULL) break;
631             /* Grow the trace around the last PC if jitState is kJitTSelect */
632             dexDecodeInstruction(gDvm.instrFormat, lastPC, &decInsn);
633
634             /*
635              * Treat {PACKED,SPARSE}_SWITCH as trace-ending instructions due
636              * to the amount of space it takes to generate the chaining
637              * cells.
638              */
639             if (interpState->totalTraceLen != 0 &&
640                 (decInsn.opCode == OP_PACKED_SWITCH ||
641                  decInsn.opCode == OP_SPARSE_SWITCH)) {
642                 interpState->jitState = kJitTSelectEnd;
643                 break;
644             }
645
646
647 #if defined(SHOW_TRACE)
648             LOGD("TraceGen: adding %s",getOpcodeName(decInsn.opCode));
649 #endif
650             flags = dexGetInstrFlags(gDvm.instrFlags, decInsn.opCode);
651             len = dexGetInstrOrTableWidthAbs(gDvm.instrWidth, lastPC);
652             offset = lastPC - interpState->method->insns;
653             assert((unsigned) offset <
654                    dvmGetMethodInsnsSize(interpState->method));
655             if (lastPC != interpState->currRunHead + interpState->currRunLen) {
656                 int currTraceRun;
657                 /* We need to start a new trace run */
658                 currTraceRun = ++interpState->currTraceRun;
659                 interpState->currRunLen = 0;
660                 interpState->currRunHead = (u2*)lastPC;
661                 interpState->trace[currTraceRun].frag.startOffset = offset;
662                 interpState->trace[currTraceRun].frag.numInsts = 0;
663                 interpState->trace[currTraceRun].frag.runEnd = false;
664                 interpState->trace[currTraceRun].frag.hint = kJitHintNone;
665             }
666             interpState->trace[interpState->currTraceRun].frag.numInsts++;
667             interpState->totalTraceLen++;
668             interpState->currRunLen += len;
669
670             /* Will probably never hit this with the current trace buildier */
671             if (interpState->currTraceRun == (MAX_JIT_RUN_LEN - 1)) {
672                 interpState->jitState = kJitTSelectEnd;
673             }
674
675             if (  ((flags & kInstrUnconditional) == 0) &&
676                   /* don't end trace on INVOKE_DIRECT_EMPTY  */
677                   (decInsn.opCode != OP_INVOKE_DIRECT_EMPTY) &&
678                   ((flags & (kInstrCanBranch |
679                              kInstrCanSwitch |
680                              kInstrCanReturn |
681                              kInstrInvoke)) != 0)) {
682                     interpState->jitState = kJitTSelectEnd;
683 #if defined(SHOW_TRACE)
684             LOGD("TraceGen: ending on %s, basic block end",
685                  getOpcodeName(decInsn.opCode));
686 #endif
687             }
688             /* Break on throw or self-loop */
689             if ((decInsn.opCode == OP_THROW) || (lastPC == pc)){
690                 interpState->jitState = kJitTSelectEnd;
691             }
692             if (interpState->totalTraceLen >= JIT_MAX_TRACE_LEN) {
693                 interpState->jitState = kJitTSelectEnd;
694             }
695              /* Abandon the trace request if debugger/profiler is attached */
696             if (debugOrProfile) {
697                 interpState->jitState = kJitDone;
698                 break;
699             }
700             if ((flags & kInstrCanReturn) != kInstrCanReturn) {
701                 break;
702             }
703             /* NOTE: intentional fallthrough for returns */
704         case kJitTSelectEnd:
705             {
706                 /* Bad trace */
707                 if (interpState->totalTraceLen == 0) {
708                     /* Bad trace - mark as untranslatable */
709                     interpState->jitState = kJitDone;
710                     switchInterp = true;
711                     break;
712                 }
713                 JitTraceDescription* desc =
714                    (JitTraceDescription*)malloc(sizeof(JitTraceDescription) +
715                      sizeof(JitTraceRun) * (interpState->currTraceRun+1));
716                 if (desc == NULL) {
717                     LOGE("Out of memory in trace selection");
718                     dvmJitStopTranslationRequests();
719                     interpState->jitState = kJitDone;
720                     switchInterp = true;
721                     break;
722                 }
723                 interpState->trace[interpState->currTraceRun].frag.runEnd =
724                      true;
725                 desc->method = interpState->method;
726                 memcpy((char*)&(desc->trace[0]),
727                     (char*)&(interpState->trace[0]),
728                     sizeof(JitTraceRun) * (interpState->currTraceRun+1));
729 #if defined(SHOW_TRACE)
730                 LOGD("TraceGen:  trace done, adding to queue");
731 #endif
732                 if (dvmCompilerWorkEnqueue(
733                        interpState->currTraceHead,kWorkOrderTrace,desc)) {
734                     /* Work order successfully enqueued */
735                     if (gDvmJit.blockingMode) {
736                         dvmCompilerDrainQueue();
737                     }
738                 } else {
739                     /*
740                      * Make sure the descriptor for the abandoned work order is
741                      * freed.
742                      */
743                     free(desc);
744                 }
745                 /*
746                  * Reset "trace in progress" flag whether or not we
747                  * successfully entered a work order.
748                  */
749                 JitEntry *jitEntry =
750                     lookupAndAdd(interpState->currTraceHead, false);
751                 if (jitEntry) {
752                     setTraceConstruction(jitEntry, false);
753                 }
754                 interpState->jitState = kJitDone;
755                 switchInterp = true;
756             }
757             break;
758         case kJitSingleStep:
759             interpState->jitState = kJitSingleStepEnd;
760             break;
761         case kJitSingleStepEnd:
762             interpState->entryPoint = kInterpEntryResume;
763             interpState->jitState = kJitDone;
764             switchInterp = true;
765             break;
766         case kJitDone:
767             switchInterp = true;
768             break;
769 #if defined(WITH_SELF_VERIFICATION)
770         case kJitSelfVerification:
771             if (selfVerificationDebugInterp(pc, self, interpState)) {
772                 /*
773                  * If the next state is not single-step end, we can switch
774                  * interpreter now.
775                  */
776                 if (interpState->jitState != kJitSingleStepEnd) {
777                     interpState->jitState = kJitDone;
778                     switchInterp = true;
779                 }
780             }
781             break;
782 #endif
783         /*
784          * If the debug interpreter was entered for non-JIT reasons, check if
785          * the original reason still holds. If not, we have to force the
786          * interpreter switch here and use dvmDebuggerOrProfilerActive instead
787          * of dvmJitDebuggerOrProfilerActive since the latter will alwasy
788          * return true when the debugger/profiler is already detached and the
789          * JIT profiling table is restored.
790          */
791         case kJitNot:
792             switchInterp = !dvmDebuggerOrProfilerActive();
793             break;
794         default:
795             LOGE("Unexpected JIT state: %d entry point: %d",
796                  interpState->jitState, interpState->entryPoint);
797             dvmAbort();
798             break;
799     }
800     /*
801      * Final check to see if we can really switch the interpreter. Make sure
802      * the jitState is kJitDone or kJitNot when switchInterp is set to true.
803      */
804      assert(switchInterp == false || interpState->jitState == kJitDone ||
805             interpState->jitState == kJitNot);
806      return switchInterp && !debugOrProfile;
807 }
808
809 JitEntry *dvmFindJitEntry(const u2* pc)
810 {
811     int idx = dvmJitHash(pc);
812
813     /* Expect a high hit rate on 1st shot */
814     if (gDvmJit.pJitEntryTable[idx].dPC == pc)
815         return &gDvmJit.pJitEntryTable[idx];
816     else {
817         int chainEndMarker = gDvmJit.jitTableSize;
818         while (gDvmJit.pJitEntryTable[idx].u.info.chain != chainEndMarker) {
819             idx = gDvmJit.pJitEntryTable[idx].u.info.chain;
820             if (gDvmJit.pJitEntryTable[idx].dPC == pc)
821                 return &gDvmJit.pJitEntryTable[idx];
822         }
823     }
824     return NULL;
825 }
826
827 /*
828  * If a translated code address exists for the davik byte code
829  * pointer return it.  This routine needs to be fast.
830  */
831 void* dvmJitGetCodeAddr(const u2* dPC)
832 {
833     int idx = dvmJitHash(dPC);
834     const u2* npc = gDvmJit.pJitEntryTable[idx].dPC;
835     if (npc != NULL) {
836         bool hideTranslation = (gDvm.sumThreadSuspendCount != 0) ||
837                                (gDvmJit.codeCacheFull == true) ||
838                                (gDvmJit.pProfTable == NULL);
839
840         if (npc == dPC) {
841 #if defined(JIT_STATS)
842             gDvmJit.addrLookupsFound++;
843 #endif
844             return hideTranslation ?
845                 NULL : gDvmJit.pJitEntryTable[idx].codeAddress;
846         } else {
847             int chainEndMarker = gDvmJit.jitTableSize;
848             while (gDvmJit.pJitEntryTable[idx].u.info.chain != chainEndMarker) {
849                 idx = gDvmJit.pJitEntryTable[idx].u.info.chain;
850                 if (gDvmJit.pJitEntryTable[idx].dPC == dPC) {
851 #if defined(JIT_STATS)
852                     gDvmJit.addrLookupsFound++;
853 #endif
854                     return hideTranslation ?
855                         NULL : gDvmJit.pJitEntryTable[idx].codeAddress;
856                 }
857             }
858         }
859     }
860 #if defined(JIT_STATS)
861     gDvmJit.addrLookupsNotFound++;
862 #endif
863     return NULL;
864 }
865
866 /*
867  * Register the translated code pointer into the JitTable.
868  * NOTE: Once a codeAddress field transitions from initial state to
869  * JIT'd code, it must not be altered without first halting all
870  * threads.  This routine should only be called by the compiler
871  * thread.
872  */
873 void dvmJitSetCodeAddr(const u2* dPC, void *nPC, JitInstructionSetType set) {
874     JitEntryInfoUnion oldValue;
875     JitEntryInfoUnion newValue;
876     JitEntry *jitEntry = lookupAndAdd(dPC, false);
877     assert(jitEntry);
878     /* Note: order of update is important */
879     do {
880         oldValue = jitEntry->u;
881         newValue = oldValue;
882         newValue.info.instructionSet = set;
883     } while (!ATOMIC_CMP_SWAP(
884              &jitEntry->u.infoWord,
885              oldValue.infoWord, newValue.infoWord));
886     jitEntry->codeAddress = nPC;
887 }
888
889 /*
890  * Determine if valid trace-bulding request is active.  Return true
891  * if we need to abort and switch back to the fast interpreter, false
892  * otherwise.
893  */
894 bool dvmJitCheckTraceRequest(Thread* self, InterpState* interpState)
895 {
896     bool switchInterp = false;         /* Assume success */
897     int i;
898     intptr_t filterKey = ((intptr_t) interpState->pc) >>
899                          JIT_TRACE_THRESH_FILTER_GRAN_LOG2;
900     bool debugOrProfile = dvmDebuggerOrProfilerActive();
901
902     /* Check if the JIT request can be handled now */
903     if (gDvmJit.pJitEntryTable != NULL && debugOrProfile == false) {
904         /* Bypass the filter for hot trace requests or during stress mode */
905         if (interpState->jitState == kJitTSelectRequest &&
906             gDvmJit.threshold > 6) {
907             /* Two-level filtering scheme */
908             for (i=0; i< JIT_TRACE_THRESH_FILTER_SIZE; i++) {
909                 if (filterKey == interpState->threshFilter[i]) {
910                     break;
911                 }
912             }
913             if (i == JIT_TRACE_THRESH_FILTER_SIZE) {
914                 /*
915                  * Use random replacement policy - otherwise we could miss a
916                  * large loop that contains more traces than the size of our
917                  * filter array.
918                  */
919                 i = rand() % JIT_TRACE_THRESH_FILTER_SIZE;
920                 interpState->threshFilter[i] = filterKey;
921                 interpState->jitState = kJitDone;
922             }
923         }
924
925         /* If the compiler is backlogged, cancel any JIT actions */
926         if (gDvmJit.compilerQueueLength >= gDvmJit.compilerHighWater) {
927             interpState->jitState = kJitDone;
928         }
929
930         /*
931          * Check for additional reasons that might force the trace select
932          * request to be dropped
933          */
934         if (interpState->jitState == kJitTSelectRequest ||
935             interpState->jitState == kJitTSelectRequestHot) {
936             JitEntry *slot = lookupAndAdd(interpState->pc, false);
937             if (slot == NULL) {
938                 /*
939                  * Table is full.  This should have been
940                  * detected by the compiler thread and the table
941                  * resized before we run into it here.  Assume bad things
942                  * are afoot and disable profiling.
943                  */
944                 interpState->jitState = kJitDone;
945                 LOGD("JIT: JitTable full, disabling profiling");
946                 dvmJitStopTranslationRequests();
947             } else if (slot->u.info.traceConstruction) {
948                 /*
949                  * Trace request already in progress, but most likely it
950                  * aborted without cleaning up.  Assume the worst and
951                  * mark trace head as untranslatable.  If we're wrong,
952                  * the compiler thread will correct the entry when the
953                  * translation is completed.  The downside here is that
954                  * some existing translation may chain to the interpret-only
955                  * template instead of the real translation during this
956                  * window.  Performance, but not correctness, issue.
957                  */
958                 interpState->jitState = kJitDone;
959                 resetTracehead(interpState, slot);
960             } else if (slot->codeAddress) {
961                  /* Nothing to do here - just return */
962                 interpState->jitState = kJitDone;
963             } else {
964                 /*
965                  * Mark request.  Note, we are not guaranteed exclusivity
966                  * here.  A window exists for another thread to be
967                  * attempting to build this same trace.  Rather than
968                  * bear the cost of locking, we'll just allow that to
969                  * happen.  The compiler thread, if it chooses, can
970                  * discard redundant requests.
971                  */
972                 setTraceConstruction(slot, true);
973             }
974         }
975
976         switch (interpState->jitState) {
977             case kJitTSelectRequest:
978             case kJitTSelectRequestHot:
979                 interpState->jitState = kJitTSelect;
980                 interpState->currTraceHead = interpState->pc;
981                 interpState->currTraceRun = 0;
982                 interpState->totalTraceLen = 0;
983                 interpState->currRunHead = interpState->pc;
984                 interpState->currRunLen = 0;
985                 interpState->trace[0].frag.startOffset =
986                      interpState->pc - interpState->method->insns;
987                 interpState->trace[0].frag.numInsts = 0;
988                 interpState->trace[0].frag.runEnd = false;
989                 interpState->trace[0].frag.hint = kJitHintNone;
990                 interpState->lastPC = 0;
991                 break;
992             /*
993              * For JIT's perspective there is no need to stay in the debug
994              * interpreter unless debugger/profiler is attached.
995              */
996             case kJitDone:
997                 switchInterp = true;
998                 break;
999             default:
1000                 LOGE("Unexpected JIT state: %d entry point: %d",
1001                      interpState->jitState, interpState->entryPoint);
1002                 dvmAbort();
1003         }
1004     } else {
1005         /*
1006          * Cannot build trace this time - ready to leave the dbg interpreter
1007          */
1008         interpState->jitState = kJitDone;
1009         switchInterp = true;
1010     }
1011
1012     /*
1013      * Final check to see if we can really switch the interpreter. Make sure
1014      * the jitState is kJitDone when switchInterp is set to true.
1015      */
1016     assert(switchInterp == false || interpState->jitState == kJitDone);
1017     return switchInterp && !debugOrProfile;
1018 }
1019
1020 /*
1021  * Resizes the JitTable.  Must be a power of 2, and returns true on failure.
1022  * Stops all threads, and thus is a heavyweight operation. May only be called
1023  * by the compiler thread.
1024  */
1025 bool dvmJitResizeJitTable( unsigned int size )
1026 {
1027     JitEntry *pNewTable;
1028     JitEntry *pOldTable;
1029     JitEntry tempEntry;
1030     u4 newMask;
1031     unsigned int oldSize;
1032     unsigned int i;
1033
1034     assert(gDvmJit.pJitEntryTable != NULL);
1035     assert(size && !(size & (size - 1)));   /* Is power of 2? */
1036
1037     LOGI("Jit: resizing JitTable from %d to %d", gDvmJit.jitTableSize, size);
1038
1039     newMask = size - 1;
1040
1041     if (size <= gDvmJit.jitTableSize) {
1042         return true;
1043     }
1044
1045     /* Make sure requested size is compatible with chain field width */
1046     tempEntry.u.info.chain = size;
1047     if (tempEntry.u.info.chain != size) {
1048         LOGD("Jit: JitTable request of %d too big", size);
1049         return true;
1050     }
1051
1052     pNewTable = (JitEntry*)calloc(size, sizeof(*pNewTable));
1053     if (pNewTable == NULL) {
1054         return true;
1055     }
1056     for (i=0; i< size; i++) {
1057         pNewTable[i].u.info.chain = size;  /* Initialize chain termination */
1058     }
1059
1060     /* Stop all other interpreting/jit'ng threads */
1061     dvmSuspendAllThreads(SUSPEND_FOR_TBL_RESIZE);
1062
1063     pOldTable = gDvmJit.pJitEntryTable;
1064     oldSize = gDvmJit.jitTableSize;
1065
1066     dvmLockMutex(&gDvmJit.tableLock);
1067     gDvmJit.pJitEntryTable = pNewTable;
1068     gDvmJit.jitTableSize = size;
1069     gDvmJit.jitTableMask = size - 1;
1070     gDvmJit.jitTableEntriesUsed = 0;
1071
1072     for (i=0; i < oldSize; i++) {
1073         if (pOldTable[i].dPC) {
1074             JitEntry *p;
1075             u2 chain;
1076             p = lookupAndAdd(pOldTable[i].dPC, true /* holds tableLock*/ );
1077             p->codeAddress = pOldTable[i].codeAddress;
1078             /* We need to preserve the new chain field, but copy the rest */
1079             chain = p->u.info.chain;
1080             p->u = pOldTable[i].u;
1081             p->u.info.chain = chain;
1082         }
1083     }
1084     dvmUnlockMutex(&gDvmJit.tableLock);
1085
1086     free(pOldTable);
1087
1088     /* Restart the world */
1089     dvmResumeAllThreads(SUSPEND_FOR_TBL_RESIZE);
1090
1091     return false;
1092 }
1093
1094 /*
1095  * Reset the JitTable to the initial clean state.
1096  */
1097 void dvmJitResetTable(void)
1098 {
1099     JitEntry *jitEntry = gDvmJit.pJitEntryTable;
1100     unsigned int size = gDvmJit.jitTableSize;
1101     unsigned int i;
1102
1103     dvmLockMutex(&gDvmJit.tableLock);
1104     memset((void *) jitEntry, 0, sizeof(JitEntry) * size);
1105     for (i=0; i< size; i++) {
1106         jitEntry[i].u.info.chain = size;  /* Initialize chain termination */
1107     }
1108     gDvmJit.jitTableEntriesUsed = 0;
1109     dvmUnlockMutex(&gDvmJit.tableLock);
1110 }
1111
1112 /*
1113  * Float/double conversion requires clamping to min and max of integer form.  If
1114  * target doesn't support this normally, use these.
1115  */
1116 s8 dvmJitd2l(double d)
1117 {
1118     static const double kMaxLong = (double)(s8)0x7fffffffffffffffULL;
1119     static const double kMinLong = (double)(s8)0x8000000000000000ULL;
1120     if (d >= kMaxLong)
1121         return (s8)0x7fffffffffffffffULL;
1122     else if (d <= kMinLong)
1123         return (s8)0x8000000000000000ULL;
1124     else if (d != d) // NaN case
1125         return 0;
1126     else
1127         return (s8)d;
1128 }
1129
1130 s8 dvmJitf2l(float f)
1131 {
1132     static const float kMaxLong = (float)(s8)0x7fffffffffffffffULL;
1133     static const float kMinLong = (float)(s8)0x8000000000000000ULL;
1134     if (f >= kMaxLong)
1135         return (s8)0x7fffffffffffffffULL;
1136     else if (f <= kMinLong)
1137         return (s8)0x8000000000000000ULL;
1138     else if (f != f) // NaN case
1139         return 0;
1140     else
1141         return (s8)f;
1142 }
1143
1144 #endif /* WITH_JIT */