Debian Codegraph @

src/cpu/core_dyn_x86.cpp

raw symbols SCIP
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_dyn_x86.cpp,v 1.36 2009-07-20 17:55:52 c2woody Exp $ */
20
21 #include "dosbox.h"
22
23 #if (C_DYNAMIC_X86)
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 "fpu.h"
54
55 #define CACHE_MAXSIZE (4096*3)
56 #define CACHE_TOTAL (1024*1024*8)
57 #define CACHE_PAGES (512)
58 #define CACHE_BLOCKS (64*1024)
59 #define CACHE_ALIGN (16)
60 #define DYN_HASH_SHIFT (4)
61 #define DYN_PAGE_HASH (4096>>DYN_HASH_SHIFT)
62 #define DYN_LINKS (16)
63
64 //#define DYN_LOG 1 //Turn logging on
65
66
67 #if C_FPU
68 #define CPU_FPU 1 //Enable FPU escape instructions
69 #endif
70
71 enum {
72 G_EAX,G_ECX,G_EDX,G_EBX,
73 G_ESP,G_EBP,G_ESI,G_EDI,
74 G_ES,G_CS,G_SS,G_DS,G_FS,G_GS,
75 G_FLAGS,G_NEWESP,G_EIP,
76 G_EA,G_STACK,G_CYCLES,
77 G_TMPB,G_TMPW,G_SHIFT,
78 G_EXIT,
79 G_MAX,
80 };
81
82 enum SingleOps {
83 SOP_INC,SOP_DEC,
84 SOP_NOT,SOP_NEG,
85 };
86
87 enum DualOps {
88 DOP_ADD,DOP_ADC,
89 DOP_SUB,DOP_SBB,
90 DOP_CMP,DOP_XOR,
91 DOP_AND,DOP_OR,
92 DOP_TEST,
93 DOP_MOV,
94 DOP_XCHG,
95 };
96
97 enum ShiftOps {
98 SHIFT_ROL,SHIFT_ROR,
99 SHIFT_RCL,SHIFT_RCR,
100 SHIFT_SHL,SHIFT_SHR,
101 SHIFT_SAL,SHIFT_SAR,
102 };
103
104 enum BranchTypes {
105 BR_O,BR_NO,BR_B,BR_NB,
106 BR_Z,BR_NZ,BR_BE,BR_NBE,
107 BR_S,BR_NS,BR_P,BR_NP,
108 BR_L,BR_NL,BR_LE,BR_NLE
109 };
110
111
112 enum BlockReturn {
113 BR_Normal=0,
114 BR_Cycles,
115 BR_Link1,BR_Link2,
116 BR_Opcode,
117 #if (C_DEBUG)
118 BR_OpcodeFull,
119 #endif
120 BR_Iret,
121 BR_CallBack,
122 BR_SMCBlock
123 };
124
125 #define SMC_CURRENT_BLOCK 0xffff
126
127
128 #define DYNFLG_HAS16 0x1 //Would like 8-bit host reg support
129 #define DYNFLG_HAS8 0x2 //Would like 16-bit host reg support
130 #define DYNFLG_LOAD 0x4 //Load value when accessed
131 #define DYNFLG_SAVE 0x8 //Needs to be saved back at the end of block
132 #define DYNFLG_CHANGED 0x10 //Value is in a register and changed from load
133 #define DYNFLG_ACTIVE 0x20 //Register has an active value
134
135 class GenReg;
136 class CodePageHandler;
137
138 struct DynReg {
139 Bitu flags;
140 GenReg * genreg;
141 void * data;
142 };
143
144 enum DynAccess {
145 DA_d,DA_w,
146 DA_bh,DA_bl
147 };
148
149 enum ByteCombo {
150 BC_ll,BC_lh,
151 BC_hl,BC_hh,
152 };
153
154 static DynReg DynRegs[G_MAX];
155 #define DREG(_WHICH_) &DynRegs[G_ ## _WHICH_ ]
156
157 static struct {
158 Bitu ea,tmpb,tmpd,stack,shift,newesp;
159 } extra_regs;
160
161 static void IllegalOption(const char* msg) {
162 E_Exit("DynCore: illegal option in %s",msg);
163 }
164
165 #include "core_dyn_x86/cache.h"
166
167 static struct {
168 Bitu callback;
169 Bit32u readdata;
170 } core_dyn;
171
172 static struct {
173 Bit32u state[32];
174 FPU_P_Reg temp,temp2;
175 Bit32u dh_fpu_enabled;
176 Bit32u state_used;
177 Bit32u cw,host_cw;
178 Bit8u temp_state[128];
179 } dyn_dh_fpu;
180
181
182 #include "core_dyn_x86/risc_x86.h"
183
184 struct DynState {
185 DynReg regs[G_MAX];
186 };
187
188 static void dyn_flags_host_to_gen(void) {
189 gen_dop_word(DOP_MOV,true,DREG(EXIT),DREG(FLAGS));
190 gen_dop_word_imm(DOP_AND,true,DREG(EXIT),FMASK_TEST);
191 gen_load_flags(DREG(EXIT));
192 gen_releasereg(DREG(EXIT));
193 gen_releasereg(DREG(FLAGS));
194 }
195
196 static void dyn_flags_gen_to_host(void) {
197 gen_save_flags(DREG(EXIT));
198 gen_dop_word_imm(DOP_AND,true,DREG(EXIT),FMASK_TEST);
199 gen_dop_word_imm(DOP_AND,true,DREG(FLAGS),~FMASK_TEST);
200 gen_dop_word(DOP_OR,true,DREG(FLAGS),DREG(EXIT)); //flags are marked for save
201 gen_releasereg(DREG(EXIT));
202 gen_releasereg(DREG(FLAGS));
203 }
204
205 static void dyn_savestate(DynState * state) {
206 for (Bitu i=0;i<G_MAX;i++) {
207 state->regs[i].flags=DynRegs[i].flags;
208 state->regs[i].genreg=DynRegs[i].genreg;
209 }
210 }
211
212 static void dyn_loadstate(DynState * state) {
213 for (Bitu i=0;i<G_MAX;i++) {
214 gen_setupreg(&DynRegs[i],&state->regs[i]);
215 }
216 }
217
218 static void dyn_synchstate(DynState * state) {
219 for (Bitu i=0;i<G_MAX;i++) {
220 gen_synchreg(&DynRegs[i],&state->regs[i]);
221 }
222 }
223
224 static void dyn_saveregister(DynReg * src_reg, DynReg * dst_reg) {
225 dst_reg->flags=src_reg->flags;
226 dst_reg->genreg=src_reg->genreg;
227 }
228
229 static void dyn_restoreregister(DynReg * src_reg, DynReg * dst_reg) {
230 dst_reg->flags=src_reg->flags;
231 dst_reg->genreg=src_reg->genreg;
232 dst_reg->genreg->dynreg=dst_reg; // necessary when register has been released
233 }
234
235 #include "core_dyn_x86/decoder.h"
236
237 #if defined (_MSC_VER)
238 #define DH_FPU_SAVE_REINIT \
239 { \
240 __asm { \
241 __asm fnsave dyn_dh_fpu.state[0] \
242 } \
243 dyn_dh_fpu.state_used=false; \
244 dyn_dh_fpu.state[0]|=0x3f; \
245 }
246 #else
247 #define DH_FPU_SAVE_REINIT \
248 { \
249 __asm__ volatile ( \
250 "fnsave %0 \n" \
251 : "=m" (dyn_dh_fpu.state[0]) \
252 : \
253 : "memory" \
254 ); \
255 dyn_dh_fpu.state_used=false; \
256 dyn_dh_fpu.state[0]|=0x3f; \
257 }
258 #endif
259
260
261 Bits CPU_Core_Dyn_X86_Run(void) {
262 /* Determine the linear address of CS:EIP */
263 restart_core:
264 PhysPt ip_point=SegPhys(cs)+reg_eip;
265 #if C_HEAVY_DEBUG
266 if (DEBUG_HeavyIsBreakpoint()) return debugCallback;
267 #endif
268 CodePageHandler * chandler=0;
269 if (GCC_UNLIKELY(MakeCodePage(ip_point,chandler))) {
270 CPU_Exception(cpu.exception.which,cpu.exception.error);
271 goto restart_core;
272 }
273 if (!chandler) {
274 if (dyn_dh_fpu.state_used) DH_FPU_SAVE_REINIT
275 return CPU_Core_Normal_Run();
276 }
277 /* Find correct Dynamic Block to run */
278 CacheBlock * block=chandler->FindCacheBlock(ip_point&4095);
279 if (!block) {
280 if (!chandler->invalidation_map || (chandler->invalidation_map[ip_point&4095]<4)) {
281 block=CreateCacheBlock(chandler,ip_point,32);
282 } else {
283 Bitu old_cycles=CPU_Cycles;
284 CPU_Cycles=1;
285 Bits nc_retcode=CPU_Core_Normal_Run();
286 if (dyn_dh_fpu.state_used) DH_FPU_SAVE_REINIT
287 if (!nc_retcode) {
288 CPU_Cycles=old_cycles-1;
289 goto restart_core;
290 }
291 CPU_CycleLeft+=old_cycles;
292 return nc_retcode;
293 }
294 }
295 run_block:
296 cache.block.running=0;
297 BlockReturn ret=gen_runcode(block->cache.start);
298 switch (ret) {
299 case BR_Iret:
300 #if C_HEAVY_DEBUG
301 if (DEBUG_HeavyIsBreakpoint()) {
302 if (dyn_dh_fpu.state_used) DH_FPU_SAVE_REINIT
303 return debugCallback;
304 }
305 #endif
306 if (!GETFLAG(TF)) {
307 if (GETFLAG(IF) && PIC_IRQCheck) {
308 if (dyn_dh_fpu.state_used) DH_FPU_SAVE_REINIT
309 return CBRET_NONE;
310 }
311 goto restart_core;
312 }
313 cpudecoder=CPU_Core_Dyn_X86_Trap_Run;
314 if (!dyn_dh_fpu.state_used) return CBRET_NONE;
315 DH_FPU_SAVE_REINIT
316 return CBRET_NONE;
317 case BR_Normal:
318 /* Maybe check if we staying in the same page? */
319 #if C_HEAVY_DEBUG
320 if (DEBUG_HeavyIsBreakpoint()) return debugCallback;
321 #endif
322 goto restart_core;
323 case BR_Cycles:
324 #if C_HEAVY_DEBUG
325 if (DEBUG_HeavyIsBreakpoint()) return debugCallback;
326 #endif
327 if (!dyn_dh_fpu.state_used) return CBRET_NONE;
328 DH_FPU_SAVE_REINIT
329 return CBRET_NONE;
330 case BR_CallBack:
331 if (!dyn_dh_fpu.state_used) return core_dyn.callback;
332 DH_FPU_SAVE_REINIT
333 return core_dyn.callback;
334 case BR_SMCBlock:
335 // LOG_MSG("selfmodification of running block at %x:%x",SegValue(cs),reg_eip);
336 cpu.exception.which=0;
337 // fallthrough, let the normal core handle the block-modifying instruction
338 case BR_Opcode:
339 CPU_CycleLeft+=CPU_Cycles;
340 CPU_Cycles=1;
341 if (dyn_dh_fpu.state_used) DH_FPU_SAVE_REINIT
342 return CPU_Core_Normal_Run();
343 #if (C_DEBUG)
344 case BR_OpcodeFull:
345 CPU_CycleLeft+=CPU_Cycles;
346 CPU_Cycles=1;
347 if (dyn_dh_fpu.state_used) DH_FPU_SAVE_REINIT
348 return CPU_Core_Full_Run();
349 #endif
350 case BR_Link1:
351 case BR_Link2:
352 {
353 Bitu temp_ip=SegPhys(cs)+reg_eip;
354 CodePageHandler * temp_handler=(CodePageHandler *)get_tlb_readhandler(temp_ip);
355 if (temp_handler->flags & PFLAG_HASCODE) {
356 block=temp_handler->FindCacheBlock(temp_ip & 4095);
357 if (!block) goto restart_core;
358 cache.block.running->LinkTo(ret==BR_Link2,block);
359 goto run_block;
360 }
361 }
362 goto restart_core;
363 }
364 if (dyn_dh_fpu.state_used) DH_FPU_SAVE_REINIT
365 return CBRET_NONE;
366 }
367
368 Bits CPU_Core_Dyn_X86_Trap_Run(void) {
369 Bits oldCycles = CPU_Cycles;
370 CPU_Cycles = 1;
371 cpu.trap_skip = false;
372
373 Bits ret=CPU_Core_Normal_Run();
374 if (!cpu.trap_skip) CPU_HW_Interrupt(1);
375 CPU_Cycles = oldCycles-1;
376 cpudecoder = &CPU_Core_Dyn_X86_Run;
377
378 return ret;
379 }
380
381 void CPU_Core_Dyn_X86_Init(void) {
382 Bits i;
383 /* Setup the global registers and their flags */
384 for (i=0;i<G_MAX;i++) DynRegs[i].genreg=0;
385 DynRegs[G_EAX].data=&reg_eax;
386 DynRegs[G_EAX].flags=DYNFLG_HAS8|DYNFLG_HAS16|DYNFLG_LOAD|DYNFLG_SAVE;
387 DynRegs[G_ECX].data=&reg_ecx;
388 DynRegs[G_ECX].flags=DYNFLG_HAS8|DYNFLG_HAS16|DYNFLG_LOAD|DYNFLG_SAVE;
389 DynRegs[G_EDX].data=&reg_edx;
390 DynRegs[G_EDX].flags=DYNFLG_HAS8|DYNFLG_HAS16|DYNFLG_LOAD|DYNFLG_SAVE;
391 DynRegs[G_EBX].data=&reg_ebx;
392 DynRegs[G_EBX].flags=DYNFLG_HAS8|DYNFLG_HAS16|DYNFLG_LOAD|DYNFLG_SAVE;
393
394 DynRegs[G_EBP].data=&reg_ebp;
395 DynRegs[G_EBP].flags=DYNFLG_HAS16|DYNFLG_LOAD|DYNFLG_SAVE;
396 DynRegs[G_ESP].data=&reg_esp;
397 DynRegs[G_ESP].flags=DYNFLG_HAS16|DYNFLG_LOAD|DYNFLG_SAVE;
398 DynRegs[G_EDI].data=&reg_edi;
399 DynRegs[G_EDI].flags=DYNFLG_HAS16|DYNFLG_LOAD|DYNFLG_SAVE;
400 DynRegs[G_ESI].data=&reg_esi;
401 DynRegs[G_ESI].flags=DYNFLG_HAS16|DYNFLG_LOAD|DYNFLG_SAVE;
402
403 DynRegs[G_ES].data=&Segs.phys[es];
404 DynRegs[G_ES].flags=DYNFLG_LOAD|DYNFLG_SAVE;
405 DynRegs[G_CS].data=&Segs.phys[cs];
406 DynRegs[G_CS].flags=DYNFLG_LOAD|DYNFLG_SAVE;
407 DynRegs[G_SS].data=&Segs.phys[ss];
408 DynRegs[G_SS].flags=DYNFLG_LOAD|DYNFLG_SAVE;
409 DynRegs[G_DS].data=&Segs.phys[ds];
410 DynRegs[G_DS].flags=DYNFLG_LOAD|DYNFLG_SAVE;
411 DynRegs[G_FS].data=&Segs.phys[fs];
412 DynRegs[G_FS].flags=DYNFLG_LOAD|DYNFLG_SAVE;
413 DynRegs[G_GS].data=&Segs.phys[gs];
414 DynRegs[G_GS].flags=DYNFLG_LOAD|DYNFLG_SAVE;
415
416 DynRegs[G_FLAGS].data=&reg_flags;
417 DynRegs[G_FLAGS].flags=DYNFLG_LOAD|DYNFLG_SAVE;
418
419 DynRegs[G_NEWESP].data=&extra_regs.newesp;
420 DynRegs[G_NEWESP].flags=0;
421
422 DynRegs[G_EIP].data=&reg_eip;
423 DynRegs[G_EIP].flags=DYNFLG_LOAD|DYNFLG_SAVE;
424
425 DynRegs[G_EA].data=&extra_regs.ea;
426 DynRegs[G_EA].flags=0;
427 DynRegs[G_STACK].data=&extra_regs.stack;
428 DynRegs[G_STACK].flags=0;
429 DynRegs[G_CYCLES].data=&CPU_Cycles;
430 DynRegs[G_CYCLES].flags=DYNFLG_LOAD|DYNFLG_SAVE;
431 DynRegs[G_TMPB].data=&extra_regs.tmpb;
432 DynRegs[G_TMPB].flags=DYNFLG_HAS8|DYNFLG_HAS16;
433 DynRegs[G_TMPW].data=&extra_regs.tmpd;
434 DynRegs[G_TMPW].flags=DYNFLG_HAS16;
435 DynRegs[G_SHIFT].data=&extra_regs.shift;
436 DynRegs[G_SHIFT].flags=DYNFLG_HAS8|DYNFLG_HAS16;
437 DynRegs[G_EXIT].data=0;
438 DynRegs[G_EXIT].flags=DYNFLG_HAS16;
439 /* Init the generator */
440 gen_init();
441
442 /* Init the fpu state */
443 dyn_dh_fpu.dh_fpu_enabled=true;
444 dyn_dh_fpu.state_used=false;
445 dyn_dh_fpu.cw=0x37f;
446 #if defined (_MSC_VER)
447 __asm {
448 __asm finit
449 __asm fsave dyn_dh_fpu.state[0]
450 __asm fstcw dyn_dh_fpu.host_cw
451 }
452 #else
453 __asm__ volatile (
454 "finit \n"
455 "fsave %0 \n"
456 "fstcw %1 \n"
457 : "=m" (dyn_dh_fpu.state[0]), "=m" (dyn_dh_fpu.host_cw)
458 :
459 : "memory"
460 );
461 #endif
462
463 return;
464 }
465
466 void CPU_Core_Dyn_X86_Cache_Init(bool enable_cache) {
467 /* Initialize code cache and dynamic blocks */
468 cache_init(enable_cache);
469 }
470
471 void CPU_Core_Dyn_X86_Cache_Close(void) {
472 cache_close();
473 }
474
475 void CPU_Core_Dyn_X86_Cache_Reset(void) {
476 cache_reset();
477 }
478
479 void CPU_Core_Dyn_X86_SetFPUMode(bool dh_fpu) {
480 dyn_dh_fpu.dh_fpu_enabled=dh_fpu;
481 }
482
483 Bit32u fpu_state[32];
484
485 void CPU_Core_Dyn_X86_SaveDHFPUState(void) {
486 if (dyn_dh_fpu.dh_fpu_enabled) {
487 if (dyn_dh_fpu.state_used!=0) {
488 #if defined (_MSC_VER)
489 __asm {
490 __asm fsave fpu_state[0]
491 __asm finit
492 }
493 #else
494 __asm__ volatile (
495 "fsave %0 \n"
496 "finit \n"
497 : "=m" (fpu_state[0])
498 :
499 : "memory"
500 );
501 #endif
502 }
503 }
504 }
505
506 void CPU_Core_Dyn_X86_RestoreDHFPUState(void) {
507 if (dyn_dh_fpu.dh_fpu_enabled) {
508 if (dyn_dh_fpu.state_used!=0) {
509 #if defined (_MSC_VER)
510 __asm {
511 __asm frstor fpu_state[0]
512 }
513 #else
514 __asm__ volatile (
515 "frstor %0 \n"
516 :
517 : "m" (fpu_state[0])
518 :
519 );
520 #endif
521 }
522 }
523 }
524
525 #else
526
527 void CPU_Core_Dyn_X86_SaveDHFPUState(void) {
528 }
529
530 void CPU_Core_Dyn_X86_RestoreDHFPUState(void) {
531 }
532
533 #endif