| 1 |
/* |
| 2 |
* Copyright (C) 2002-2010 The DOSBox Team |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; either version 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program; if not, write to the Free Software |
| 16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 |
*/ |
| 18 |
|
| 19 |
/* $Id: core_dynrec.cpp,v 1.15 2009-08-02 16:52:33 c2woody Exp $ */ |
| 20 |
|
| 21 |
#include "dosbox.h" |
| 22 |
|
| 23 |
#if (C_DYNREC) |
| 24 |
|
| 25 |
#include <assert.h> |
| 26 |
#include <stdarg.h> |
| 27 |
#include <stdio.h> |
| 28 |
#include <string.h> |
| 29 |
#include <stddef.h> |
| 30 |
#include <stdlib.h> |
| 31 |
|
| 32 |
#if defined (WIN32) |
| 33 |
#include <windows.h> |
| 34 |
#include <winbase.h> |
| 35 |
#endif |
| 36 |
|
| 37 |
#if (C_HAVE_MPROTECT) |
| 38 |
#include <sys/mman.h> |
| 39 |
|
| 40 |
#include <limits.h> |
| 41 |
#ifndef PAGESIZE |
| 42 |
#define PAGESIZE 4096 |
| 43 |
#endif |
| 44 |
#endif /* C_HAVE_MPROTECT */ |
| 45 |
|
| 46 |
#include "callback.h" |
| 47 |
#include "regs.h" |
| 48 |
#include "mem.h" |
| 49 |
#include "cpu.h" |
| 50 |
#include "debug.h" |
| 51 |
#include "paging.h" |
| 52 |
#include "inout.h" |
| 53 |
#include "lazyflags.h" |
| 54 |
#include "pic.h" |
| 55 |
|
| 56 |
#define CACHE_MAXSIZE (4096*2) |
| 57 |
#define CACHE_TOTAL (1024*1024*8) |
| 58 |
#define CACHE_PAGES (512) |
| 59 |
#define CACHE_BLOCKS (128*1024) |
| 60 |
#define CACHE_ALIGN (16) |
| 61 |
#define DYN_HASH_SHIFT (4) |
| 62 |
#define DYN_PAGE_HASH (4096>>DYN_HASH_SHIFT) |
| 63 |
#define DYN_LINKS (16) |
| 64 |
|
| 65 |
|
| 66 |
//#define DYN_LOG 1 //Turn Logging on. |
| 67 |
|
| 68 |
|
| 69 |
#if C_FPU |
| 70 |
#define CPU_FPU 1 //Enable FPU escape instructions |
| 71 |
#endif |
| 72 |
|
| 73 |
|
| 74 |
// the emulated x86 registers |
| 75 |
#define DRC_REG_EAX 0 |
| 76 |
#define DRC_REG_ECX 1 |
| 77 |
#define DRC_REG_EDX 2 |
| 78 |
#define DRC_REG_EBX 3 |
| 79 |
#define DRC_REG_ESP 4 |
| 80 |
#define DRC_REG_EBP 5 |
| 81 |
#define DRC_REG_ESI 6 |
| 82 |
#define DRC_REG_EDI 7 |
| 83 |
|
| 84 |
// the emulated x86 segment registers |
| 85 |
#define DRC_SEG_ES 0 |
| 86 |
#define DRC_SEG_CS 1 |
| 87 |
#define DRC_SEG_SS 2 |
| 88 |
#define DRC_SEG_DS 3 |
| 89 |
#define DRC_SEG_FS 4 |
| 90 |
#define DRC_SEG_GS 5 |
| 91 |
|
| 92 |
|
| 93 |
// access to a general register |
| 94 |
#define DRCD_REG_VAL(reg) (&cpu_regs.regs[reg].dword) |
| 95 |
// access to a segment register |
| 96 |
#define DRCD_SEG_VAL(seg) (&Segs.val[seg]) |
| 97 |
// access to the physical value of a segment register/selector |
| 98 |
#define DRCD_SEG_PHYS(seg) (&Segs.phys[seg]) |
| 99 |
|
| 100 |
// access to an 8bit general register |
| 101 |
#define DRCD_REG_BYTE(reg,idx) (&cpu_regs.regs[reg].byte[idx?BH_INDEX:BL_INDEX]) |
| 102 |
// access to 16/32bit general registers |
| 103 |
#define DRCD_REG_WORD(reg,dwrd) ((dwrd)?((void*)(&cpu_regs.regs[reg].dword[DW_INDEX])):((void*)(&cpu_regs.regs[reg].word[W_INDEX]))) |
| 104 |
|
| 105 |
|
| 106 |
enum BlockReturn { |
| 107 |
BR_Normal=0, |
| 108 |
BR_Cycles, |
| 109 |
BR_Link1,BR_Link2, |
| 110 |
BR_Opcode, |
| 111 |
#if (C_DEBUG) |
| 112 |
BR_OpcodeFull, |
| 113 |
#endif |
| 114 |
BR_Iret, |
| 115 |
BR_CallBack, |
| 116 |
BR_SMCBlock |
| 117 |
}; |
| 118 |
|
| 119 |
// identificator to signal self-modification of the currently executed block |
| 120 |
#define SMC_CURRENT_BLOCK 0xffff |
| 121 |
|
| 122 |
|
| 123 |
static void IllegalOptionDynrec(const char* msg) { |
| 124 |
E_Exit("DynrecCore: illegal option in %s",msg); |
| 125 |
} |
| 126 |
|
| 127 |
static struct { |
| 128 |
BlockReturn (*runcode)(Bit8u*); // points to code that can start a block |
| 129 |
Bitu callback; // the occurred callback |
| 130 |
Bitu readdata; // spare space used when reading from memory |
| 131 |
Bit32u protected_regs[8]; // space to save/restore register values |
| 132 |
} core_dynrec; |
| 133 |
|
| 134 |
|
| 135 |
#include "core_dynrec/cache.h" |
| 136 |
|
| 137 |
#define X86 0x01 |
| 138 |
#define X86_64 0x02 |
| 139 |
#define MIPSEL 0x03 |
| 140 |
#define ARMV4LE 0x04 |
| 141 |
#define POWERPC 0x04 |
| 142 |
|
| 143 |
#if C_TARGETCPU == X86_64 |
| 144 |
#include "core_dynrec/risc_x64.h" |
| 145 |
#elif C_TARGETCPU == X86 |
| 146 |
#include "core_dynrec/risc_x86.h" |
| 147 |
#elif C_TARGETCPU == MIPSEL |
| 148 |
#include "core_dynrec/risc_mipsel32.h" |
| 149 |
#elif C_TARGETCPU == ARMV4LE |
| 150 |
#include "core_dynrec/risc_armv4le.h" |
| 151 |
#elif C_TARGETCPU == POWERPC |
| 152 |
#include "core_dynrec/risc_ppc.h" |
| 153 |
#endif |
| 154 |
|
| 155 |
#include "core_dynrec/decoder.h" |
| 156 |
|
| 157 |
CacheBlockDynRec * LinkBlocks(BlockReturn ret) { |
| 158 |
CacheBlockDynRec * block=NULL; |
| 159 |
// the last instruction was a control flow modifying instruction |
| 160 |
Bitu temp_ip=SegPhys(cs)+reg_eip; |
| 161 |
CodePageHandlerDynRec * temp_handler=(CodePageHandlerDynRec *)get_tlb_readhandler(temp_ip); |
| 162 |
if (temp_handler->flags & PFLAG_HASCODE) { |
| 163 |
// see if the target is an already translated block |
| 164 |
block=temp_handler->FindCacheBlock(temp_ip & 4095); |
| 165 |
if (!block) return NULL; |
| 166 |
|
| 167 |
// found it, link the current block to |
| 168 |
cache.block.running->LinkTo(ret==BR_Link2,block); |
| 169 |
return block; |
| 170 |
} |
| 171 |
return NULL; |
| 172 |
} |
| 173 |
|
| 174 |
/* |
| 175 |
The core tries to find the block that should be executed next. |
| 176 |
If such a block is found, it is run, otherwise the instruction |
| 177 |
stream starting at ip_point is translated (see decoder.h) and |
| 178 |
makes up a new code block that will be run. |
| 179 |
When control is returned to CPU_Core_Dynrec_Run (which might |
| 180 |
be right after the block is run, or somewhen long after that |
| 181 |
due to the direct cacheblock linking) the returncode decides |
| 182 |
the next action. This might be continuing the translation and |
| 183 |
execution process, or returning from the core etc. |
| 184 |
*/ |
| 185 |
|
| 186 |
Bits CPU_Core_Dynrec_Run(void) { |
| 187 |
for (;;) { |
| 188 |
// Determine the linear address of CS:EIP |
| 189 |
PhysPt ip_point=SegPhys(cs)+reg_eip; |
| 190 |
#if C_HEAVY_DEBUG |
| 191 |
if (DEBUG_HeavyIsBreakpoint()) return debugCallback; |
| 192 |
#endif |
| 193 |
|
| 194 |
CodePageHandlerDynRec * chandler=0; |
| 195 |
// see if the current page is present and contains code |
| 196 |
if (GCC_UNLIKELY(MakeCodePage(ip_point,chandler))) { |
| 197 |
// page not present, throw the exception |
| 198 |
CPU_Exception(cpu.exception.which,cpu.exception.error); |
| 199 |
continue; |
| 200 |
} |
| 201 |
|
| 202 |
// page doesn't contain code or is special |
| 203 |
if (GCC_UNLIKELY(!chandler)) return CPU_Core_Normal_Run(); |
| 204 |
|
| 205 |
// find correct Dynamic Block to run |
| 206 |
CacheBlockDynRec * block=chandler->FindCacheBlock(ip_point&4095); |
| 207 |
if (!block) { |
| 208 |
// no block found, thus translate the instruction stream |
| 209 |
// unless the instruction is known to be modified |
| 210 |
if (!chandler->invalidation_map || (chandler->invalidation_map[ip_point&4095]<4)) { |
| 211 |
// translate up to 32 instructions |
| 212 |
block=CreateCacheBlock(chandler,ip_point,32); |
| 213 |
} else { |
| 214 |
// let the normal core handle this instruction to avoid zero-sized blocks |
| 215 |
Bitu old_cycles=CPU_Cycles; |
| 216 |
CPU_Cycles=1; |
| 217 |
Bits nc_retcode=CPU_Core_Normal_Run(); |
| 218 |
if (!nc_retcode) { |
| 219 |
CPU_Cycles=old_cycles-1; |
| 220 |
continue; |
| 221 |
} |
| 222 |
CPU_CycleLeft+=old_cycles; |
| 223 |
return nc_retcode; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
run_block: |
| 228 |
cache.block.running=0; |
| 229 |
// now we're ready to run the dynamic code block |
| 230 |
// BlockReturn ret=((BlockReturn (*)(void))(block->cache.start))(); |
| 231 |
BlockReturn ret=core_dynrec.runcode(block->cache.start); |
| 232 |
|
| 233 |
switch (ret) { |
| 234 |
case BR_Iret: |
| 235 |
#if C_HEAVY_DEBUG |
| 236 |
if (DEBUG_HeavyIsBreakpoint()) return debugCallback; |
| 237 |
#endif |
| 238 |
if (!GETFLAG(TF)) { |
| 239 |
if (GETFLAG(IF) && PIC_IRQCheck) return CBRET_NONE; |
| 240 |
break; |
| 241 |
} |
| 242 |
// trapflag is set, switch to the trap-aware decoder |
| 243 |
cpudecoder=CPU_Core_Dynrec_Trap_Run; |
| 244 |
return CBRET_NONE; |
| 245 |
|
| 246 |
case BR_Normal: |
| 247 |
// the block was exited due to a non-predictable control flow |
| 248 |
// modifying instruction (like ret) or some nontrivial cpu state |
| 249 |
// changing instruction (for example switch to/from pmode), |
| 250 |
// or the maximal number of instructions to translate was reached |
| 251 |
#if C_HEAVY_DEBUG |
| 252 |
if (DEBUG_HeavyIsBreakpoint()) return debugCallback; |
| 253 |
#endif |
| 254 |
break; |
| 255 |
|
| 256 |
case BR_Cycles: |
| 257 |
// cycles went negative, return from the core to handle |
| 258 |
// external events, schedule the pic... |
| 259 |
#if C_HEAVY_DEBUG |
| 260 |
if (DEBUG_HeavyIsBreakpoint()) return debugCallback; |
| 261 |
#endif |
| 262 |
return CBRET_NONE; |
| 263 |
|
| 264 |
case BR_CallBack: |
| 265 |
// the callback code is executed in dosbox.conf, return the callback number |
| 266 |
FillFlags(); |
| 267 |
return core_dynrec.callback; |
| 268 |
|
| 269 |
case BR_SMCBlock: |
| 270 |
// LOG_MSG("selfmodification of running block at %x:%x",SegValue(cs),reg_eip); |
| 271 |
cpu.exception.which=0; |
| 272 |
// fallthrough, let the normal core handle the block-modifying instruction |
| 273 |
case BR_Opcode: |
| 274 |
// some instruction has been encountered that could not be translated |
| 275 |
// (thus it is not part of the code block), the normal core will |
| 276 |
// handle this instruction |
| 277 |
CPU_CycleLeft+=CPU_Cycles; |
| 278 |
CPU_Cycles=1; |
| 279 |
return CPU_Core_Normal_Run(); |
| 280 |
|
| 281 |
#if (C_DEBUG) |
| 282 |
case BR_OpcodeFull: |
| 283 |
CPU_CycleLeft+=CPU_Cycles; |
| 284 |
CPU_Cycles=1; |
| 285 |
return CPU_Core_Full_Run(); |
| 286 |
#endif |
| 287 |
|
| 288 |
case BR_Link1: |
| 289 |
case BR_Link2: |
| 290 |
block=LinkBlocks(ret); |
| 291 |
if (block) goto run_block; |
| 292 |
break; |
| 293 |
|
| 294 |
default: |
| 295 |
E_Exit("Invalid return code %d", ret); |
| 296 |
} |
| 297 |
} |
| 298 |
return CBRET_NONE; |
| 299 |
} |
| 300 |
|
| 301 |
Bits CPU_Core_Dynrec_Trap_Run(void) { |
| 302 |
Bits oldCycles = CPU_Cycles; |
| 303 |
CPU_Cycles = 1; |
| 304 |
cpu.trap_skip = false; |
| 305 |
|
| 306 |
// let the normal core execute the next (only one!) instruction |
| 307 |
Bits ret=CPU_Core_Normal_Run(); |
| 308 |
|
| 309 |
// trap to int1 unless the last instruction deferred this |
| 310 |
// (allows hardware interrupts to be served without interaction) |
| 311 |
if (!cpu.trap_skip) CPU_HW_Interrupt(1); |
| 312 |
|
| 313 |
CPU_Cycles = oldCycles-1; |
| 314 |
// continue (either the trapflag was clear anyways, or the int1 cleared it) |
| 315 |
cpudecoder = &CPU_Core_Dynrec_Run; |
| 316 |
|
| 317 |
return ret; |
| 318 |
} |
| 319 |
|
| 320 |
void CPU_Core_Dynrec_Init(void) { |
| 321 |
} |
| 322 |
|
| 323 |
void CPU_Core_Dynrec_Cache_Init(bool enable_cache) { |
| 324 |
// Initialize code cache and dynamic blocks |
| 325 |
cache_init(enable_cache); |
| 326 |
} |
| 327 |
|
| 328 |
void CPU_Core_Dynrec_Cache_Close(void) { |
| 329 |
cache_close(); |
| 330 |
} |
| 331 |
|
| 332 |
#endif |