| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | /* |
| 17 | * Jit control |
| 18 | */ |
| 19 | #ifndef _DALVIK_INTERP_JIT |
| 20 | #define _DALVIK_INTERP_JIT |
| 21 | |
| 22 | #include "InterpDefs.h" |
| Ben Cheng | 7b133ef | 2010-02-04 16:15:59 -0800 | [diff] [blame] | 23 | #include "mterp/common/jit-config.h" |
| Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 24 | |
| 25 | #define JIT_MAX_TRACE_LEN 100 |
| 26 | |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 27 | #if defined (WITH_SELF_VERIFICATION) |
| 28 | |
| 29 | #define REG_SPACE 256 /* default size of shadow space */ |
| 30 | #define HEAP_SPACE JIT_MAX_TRACE_LEN /* default size of heap space */ |
| 31 | |
| 32 | typedef struct ShadowHeap { |
| 33 | int addr; |
| 34 | int data; |
| 35 | } ShadowHeap; |
| 36 | |
| 37 | typedef struct InstructionTrace { |
| 38 | int addr; |
| Ben Cheng | bcdc1de | 2009-08-21 16:18:46 -0700 | [diff] [blame] | 39 | DecodedInstruction decInsn; |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 40 | } InstructionTrace; |
| 41 | |
| 42 | typedef struct ShadowSpace { |
| 43 | const u2* startPC; /* starting pc of jitted region */ |
| buzbee | 9f601a9 | 2011-02-11 17:48:20 -0800 | [diff] [blame] | 44 | u4* fp; /* starting fp of jitted region */ |
| 45 | const Method *method; |
| 46 | DvmDex* methodClassDex; |
| 47 | JValue retval; |
| 48 | const u1* interpStackEnd; |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 49 | SelfVerificationState jitExitState; /* exit point for JIT'ed code */ |
| 50 | SelfVerificationState selfVerificationState; /* current SV running state */ |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 51 | const u2* endPC; /* ending pc of jitted region */ |
| 52 | void* shadowFP; /* pointer to fp in shadow space */ |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 53 | int* registerSpace; /* copy of register state */ |
| 54 | int registerSpaceSize; /* current size of register space */ |
| 55 | ShadowHeap heapSpace[HEAP_SPACE]; /* copy of heap space */ |
| 56 | ShadowHeap* heapSpaceTail; /* tail pointer to heapSpace */ |
| 57 | const void* endShadowFP; /* ending fp in shadow space */ |
| 58 | InstructionTrace trace[JIT_MAX_TRACE_LEN]; /* opcode trace for debugging */ |
| 59 | int traceLength; /* counter for current trace length */ |
| 60 | } ShadowSpace; |
| 61 | |
| 62 | /* |
| 63 | * Self verification functions. |
| 64 | */ |
| 65 | void* dvmSelfVerificationShadowSpaceAlloc(Thread* self); |
| 66 | void dvmSelfVerificationShadowSpaceFree(Thread* self); |
| buzbee | 9f601a9 | 2011-02-11 17:48:20 -0800 | [diff] [blame] | 67 | void* dvmSelfVerificationSaveState(const u2* pc, u4* fp, |
| 68 | Thread* self, |
| Bill Buzbee | 9a8c75a | 2009-11-08 14:31:20 -0800 | [diff] [blame] | 69 | int targetTrace); |
| buzbee | 9f601a9 | 2011-02-11 17:48:20 -0800 | [diff] [blame] | 70 | void* dvmSelfVerificationRestoreState(const u2* pc, u4* fp, |
| 71 | SelfVerificationState exitPoint, |
| 72 | Thread *self); |
| buzbee | 9a3147c | 2011-03-02 15:43:48 -0800 | [diff] [blame] | 73 | void dvmCheckSelfVerification(const u2* pc, Thread* self); |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 74 | #endif |
| 75 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 76 | /* |
| Ben Cheng | 385828e | 2011-03-04 16:48:33 -0800 | [diff] [blame] | 77 | * Offsets for metadata in the trace run array from the trace that ends with |
| 78 | * invoke instructions. |
| 79 | */ |
| 80 | #define JIT_TRACE_CLASS_DESC 1 |
| 81 | #define JIT_TRACE_CLASS_LOADER 2 |
| 82 | #define JIT_TRACE_CUR_METHOD 3 |
| 83 | |
| 84 | /* |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 85 | * JitTable hash function. |
| 86 | */ |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 87 | |
| 88 | static inline u4 dvmJitHashMask( const u2* p, u4 mask ) { |
| 89 | return ((((u4)p>>12)^(u4)p)>>1) & (mask); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 92 | static inline u4 dvmJitHash( const u2* p ) { |
| 93 | return dvmJitHashMask( p, gDvmJit.jitTableMask ); |
| 94 | } |
| 95 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 96 | /* |
| buzbee | 2e152ba | 2010-12-15 16:32:35 -0800 | [diff] [blame] | 97 | * The width of the chain field in JitEntryInfo sets the upper |
| 98 | * bound on the number of translations. Be careful if changing |
| 99 | * the size of JitEntry struct - the Dalvik PC to JitEntry |
| 100 | * hash functions have built-in knowledge of the size. |
| 101 | */ |
| 102 | #define JIT_ENTRY_CHAIN_WIDTH 2 |
| 103 | #define JIT_MAX_ENTRIES (1 << (JIT_ENTRY_CHAIN_WIDTH * 8)) |
| 104 | |
| 105 | /* |
| 106 | * The trace profiling counters are allocated in blocks and individual |
| 107 | * counters must not move so long as any referencing trace exists. |
| 108 | */ |
| 109 | #define JIT_PROF_BLOCK_ENTRIES 1024 |
| 110 | #define JIT_PROF_BLOCK_BUCKETS (JIT_MAX_ENTRIES / JIT_PROF_BLOCK_ENTRIES) |
| 111 | |
| 112 | typedef s4 JitTraceCounter_t; |
| 113 | |
| 114 | typedef struct JitTraceProfCounters { |
| 115 | unsigned int next; |
| 116 | JitTraceCounter_t *buckets[JIT_PROF_BLOCK_BUCKETS]; |
| 117 | } JitTraceProfCounters; |
| 118 | |
| 119 | /* |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 120 | * Entries in the JIT's address lookup hash table. |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 121 | * Fields which may be updated by multiple threads packed into a |
| 122 | * single 32-bit word to allow use of atomic update. |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 123 | */ |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 124 | |
| 125 | typedef struct JitEntryInfo { |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 126 | unsigned int isMethodEntry:1; |
| 127 | unsigned int inlineCandidate:1; |
| 128 | unsigned int profileEnabled:1; |
| Bill Buzbee | 1b3da59 | 2011-02-03 07:38:22 -0800 | [diff] [blame] | 129 | JitInstructionSetType instructionSet:3; |
| 130 | unsigned int profileOffset:5; |
| 131 | unsigned int unused:5; |
| Bill Buzbee | d726991 | 2009-11-10 14:31:32 -0800 | [diff] [blame] | 132 | u2 chain; /* Index of next in chain */ |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 133 | } JitEntryInfo; |
| 134 | |
| 135 | typedef union JitEntryInfoUnion { |
| 136 | JitEntryInfo info; |
| 137 | volatile int infoWord; |
| 138 | } JitEntryInfoUnion; |
| 139 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 140 | typedef struct JitEntry { |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 141 | JitEntryInfoUnion u; |
| 142 | const u2* dPC; /* Dalvik code address */ |
| 143 | void* codeAddress; /* Code address of native translation */ |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 144 | } JitEntry; |
| 145 | |
| buzbee | 9a3147c | 2011-03-02 15:43:48 -0800 | [diff] [blame] | 146 | void dvmCheckJit(const u2* pc, Thread* self); |
| Ben Cheng | cfdeca3 | 2011-01-14 11:36:46 -0800 | [diff] [blame] | 147 | void* dvmJitGetTraceAddr(const u2* dPC); |
| 148 | void* dvmJitGetMethodAddr(const u2* dPC); |
| buzbee | 9a3147c | 2011-03-02 15:43:48 -0800 | [diff] [blame] | 149 | void dvmJitCheckTraceRequest(Thread* self); |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 150 | void dvmJitStopTranslationRequests(void); |
| 151 | void dvmJitStats(void); |
| 152 | bool dvmJitResizeJitTable(unsigned int size); |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 153 | void dvmJitResetTable(void); |
| Bill Buzbee | 1b3da59 | 2011-02-03 07:38:22 -0800 | [diff] [blame] | 154 | struct JitEntry *dvmJitFindEntry(const u2* pc, bool isMethodEntry); |
| Bill Buzbee | 50a6bf2 | 2009-07-08 13:08:04 -0700 | [diff] [blame] | 155 | s8 dvmJitd2l(double d); |
| 156 | s8 dvmJitf2l(float f); |
| buzbee | 2e152ba | 2010-12-15 16:32:35 -0800 | [diff] [blame] | 157 | void dvmJitSetCodeAddr(const u2* dPC, void *nPC, JitInstructionSetType set, |
| Ben Cheng | cfdeca3 | 2011-01-14 11:36:46 -0800 | [diff] [blame] | 158 | bool isMethodEntry, int profilePrefixSize); |
| buzbee | 9a3147c | 2011-03-02 15:43:48 -0800 | [diff] [blame] | 159 | void dvmJitEndTraceSelect(Thread* self, const u2* dPC); |
| buzbee | 2e152ba | 2010-12-15 16:32:35 -0800 | [diff] [blame] | 160 | JitTraceCounter_t *dvmJitNextTraceCounter(void); |
| 161 | void dvmJitTraceProfilingOff(void); |
| 162 | void dvmJitTraceProfilingOn(void); |
| 163 | void dvmJitChangeProfileMode(TraceProfilingModes newState); |
| buzbee | d82cebc | 2011-03-14 12:25:24 -0700 | [diff] [blame] | 164 | void dvmJitDumpTraceDesc(JitTraceDescription *trace); |
| buzbee | 9a3147c | 2011-03-02 15:43:48 -0800 | [diff] [blame] | 165 | void dvmJitUpdateState(void); |
| 166 | void dvmJitResumeTranslation(Thread* self, const u2* pc, const u4* fp); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 167 | |
| 168 | #endif /*_DALVIK_INTERP_JIT*/ |