Debian Codegraph @

src/cpu/cpu.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: cpu.cpp,v 1.116 2009-03-16 18:10:08 c2woody Exp $ */
20
21 #include <assert.h>
22 #include <sstream>
23 #include "dosbox.h"
24 #include "cpu.h"
25 #include "memory.h"
26 #include "debug.h"
27 #include "mapper.h"
28 #include "setup.h"
29 #include "programs.h"
30 #include "paging.h"
31 #include "lazyflags.h"
32 #include "support.h"
33
34 Bitu DEBUG_EnableDebugger(void);
35 extern void GFX_SetTitle(Bit32s cycles ,Bits frameskip,bool paused);
36
37 #if 1
38 #undef LOG
39 #if defined (_MSC_VER)
40 #define LOG(X,Y)
41 #else
42 #define LOG(X,Y) CPU_LOG
43 #define CPU_LOG(...)
44 #endif
45 #endif
46
47 CPU_Regs cpu_regs;
48 CPUBlock cpu;
49 Segments Segs;
50
51 Bit32s CPU_Cycles = 0;
52 Bit32s CPU_CycleLeft = 3000;
53 Bit32s CPU_CycleMax = 3000;
54 Bit32s CPU_OldCycleMax = 3000;
55 Bit32s CPU_CyclePercUsed = 100;
56 Bit32s CPU_CycleLimit = -1;
57 Bit32s CPU_CycleUp = 0;
58 Bit32s CPU_CycleDown = 0;
59 Bit64s CPU_IODelayRemoved = 0;
60 CPU_Decoder * cpudecoder;
61 bool CPU_CycleAutoAdjust = false;
62 bool CPU_SkipCycleAutoAdjust = false;
63 Bitu CPU_AutoDetermineMode = 0;
64
65 Bitu CPU_ArchitectureType = CPU_ARCHTYPE_MIXED;
66
67 Bitu CPU_flag_id_toggle=0;
68
69 Bitu CPU_PrefetchQueueSize=0;
70
71 void CPU_Core_Full_Init(void);
72 void CPU_Core_Normal_Init(void);
73 void CPU_Core_Simple_Init(void);
74 #if (C_DYNAMIC_X86)
75 void CPU_Core_Dyn_X86_Init(void);
76 void CPU_Core_Dyn_X86_Cache_Init(bool enable_cache);
77 void CPU_Core_Dyn_X86_Cache_Close(void);
78 void CPU_Core_Dyn_X86_SetFPUMode(bool dh_fpu);
79 #elif (C_DYNREC)
80 void CPU_Core_Dynrec_Init(void);
81 void CPU_Core_Dynrec_Cache_Init(bool enable_cache);
82 void CPU_Core_Dynrec_Cache_Close(void);
83 #endif
84
85 /* In debug mode exceptions are tested and dosbox exits when
86 * a unhandled exception state is detected.
87 * USE CHECK_EXCEPT to raise an exception in that case to see if that exception
88 * solves the problem.
89 *
90 * In non-debug mode dosbox doesn't do detection (and hence doesn't crash at
91 * that point). (game might crash later due to the unhandled exception) */
92
93 #if C_DEBUG
94 // #define CPU_CHECK_EXCEPT 1
95 // #define CPU_CHECK_IGNORE 1
96 /* Use CHECK_EXCEPT when something doesn't work to see if a exception is
97 * needed that isn't enabled by default.*/
98 #else
99 /* NORMAL NO CHECKING => More Speed */
100 #define CPU_CHECK_IGNORE 1
101 #endif /* C_DEBUG */
102
103 #if defined(CPU_CHECK_IGNORE)
104 #define CPU_CHECK_COND(cond,msg,exc,sel) { \
105 if (cond) do {} while (0); \
106 }
107 #elif defined(CPU_CHECK_EXCEPT)
108 #define CPU_CHECK_COND(cond,msg,exc,sel) { \
109 if (cond) { \
110 CPU_Exception(exc,sel); \
111 return; \
112 } \
113 }
114 #else
115 #define CPU_CHECK_COND(cond,msg,exc,sel) { \
116 if (cond) E_Exit(msg); \
117 }
118 #endif
119
120
121 void Descriptor::Load(PhysPt address) {
122 cpu.mpl=0;
123 Bit32u* data = (Bit32u*)&saved;
124 *data = mem_readd(address);
125 *(data+1) = mem_readd(address+4);
126 cpu.mpl=3;
127 }
128 void Descriptor:: Save(PhysPt address) {
129 cpu.mpl=0;
130 Bit32u* data = (Bit32u*)&saved;
131 mem_writed(address,*data);
132 mem_writed(address+4,*(data+1));
133 cpu.mpl=03;
134 }
135
136
137 void CPU_Push16(Bitu value) {
138 Bit32u new_esp=(reg_esp&cpu.stack.notmask)|((reg_esp-2)&cpu.stack.mask);
139 mem_writew(SegPhys(ss) + (new_esp & cpu.stack.mask) ,value);
140 reg_esp=new_esp;
141 }
142
143 void CPU_Push32(Bitu value) {
144 Bit32u new_esp=(reg_esp&cpu.stack.notmask)|((reg_esp-4)&cpu.stack.mask);
145 mem_writed(SegPhys(ss) + (new_esp & cpu.stack.mask) ,value);
146 reg_esp=new_esp;
147 }
148
149 Bitu CPU_Pop16(void) {
150 Bitu val=mem_readw(SegPhys(ss) + (reg_esp & cpu.stack.mask));
151 reg_esp=(reg_esp&cpu.stack.notmask)|((reg_esp+2)&cpu.stack.mask);
152 return val;
153 }
154
155 Bitu CPU_Pop32(void) {
156 Bitu val=mem_readd(SegPhys(ss) + (reg_esp & cpu.stack.mask));
157 reg_esp=(reg_esp&cpu.stack.notmask)|((reg_esp+4)&cpu.stack.mask);
158 return val;
159 }
160
161 PhysPt SelBase(Bitu sel) {
162 if (cpu.cr0 & CR0_PROTECTION) {
163 Descriptor desc;
164 cpu.gdt.GetDescriptor(sel,desc);
165 return desc.GetBase();
166 } else {
167 return sel<<4;
168 }
169 }
170
171
172 void CPU_SetFlags(Bitu word,Bitu mask) {
173 mask|=CPU_flag_id_toggle; // ID-flag can be toggled on cpuid-supporting CPUs
174 reg_flags=(reg_flags & ~mask)|(word & mask)|2;
175 cpu.direction=1-((reg_flags & FLAG_DF) >> 9);
176 }
177
178 bool CPU_PrepareException(Bitu which,Bitu error) {
179 cpu.exception.which=which;
180 cpu.exception.error=error;
181 return true;
182 }
183
184 bool CPU_CLI(void) {
185 if (cpu.pmode && ((!GETFLAG(VM) && (GETFLAG_IOPL<cpu.cpl)) || (GETFLAG(VM) && (GETFLAG_IOPL<3)))) {
186 return CPU_PrepareException(EXCEPTION_GP,0);
187 } else {
188 SETFLAGBIT(IF,false);
189 return false;
190 }
191 }
192
193 bool CPU_STI(void) {
194 if (cpu.pmode && ((!GETFLAG(VM) && (GETFLAG_IOPL<cpu.cpl)) || (GETFLAG(VM) && (GETFLAG_IOPL<3)))) {
195 return CPU_PrepareException(EXCEPTION_GP,0);
196 } else {
197 SETFLAGBIT(IF,true);
198 return false;
199 }
200 }
201
202 bool CPU_POPF(Bitu use32) {
203 if (cpu.pmode && GETFLAG(VM) && (GETFLAG(IOPL)!=FLAG_IOPL)) {
204 /* Not enough privileges to execute POPF */
205 return CPU_PrepareException(EXCEPTION_GP,0);
206 }
207 Bitu mask=FMASK_ALL;
208 /* IOPL field can only be modified when CPL=0 or in real mode: */
209 if (cpu.pmode && (cpu.cpl>0)) mask &= (~FLAG_IOPL);
210 if (cpu.pmode && !GETFLAG(VM) && (GETFLAG_IOPL<cpu.cpl)) mask &= (~FLAG_IF);
211 if (use32)
212 CPU_SetFlags(CPU_Pop32(),mask);
213 else CPU_SetFlags(CPU_Pop16(),mask & 0xffff);
214 DestroyConditionFlags();
215 return false;
216 }
217
218 bool CPU_PUSHF(Bitu use32) {
219 if (cpu.pmode && GETFLAG(VM) && (GETFLAG(IOPL)!=FLAG_IOPL)) {
220 /* Not enough privileges to execute PUSHF */
221 return CPU_PrepareException(EXCEPTION_GP,0);
222 }
223 FillFlags();
224 if (use32)
225 CPU_Push32(reg_flags & 0xfcffff);
226 else CPU_Push16(reg_flags);
227 return false;
228 }
229
230 void CPU_CheckSegments(void) {
231 bool needs_invalidation=false;
232 Descriptor desc;
233 if (!cpu.gdt.GetDescriptor(SegValue(es),desc)) needs_invalidation=true;
234 else switch (desc.Type()) {
235 case DESC_DATA_EU_RO_NA: case DESC_DATA_EU_RO_A: case DESC_DATA_EU_RW_NA: case DESC_DATA_EU_RW_A:
236 case DESC_DATA_ED_RO_NA: case DESC_DATA_ED_RO_A: case DESC_DATA_ED_RW_NA: case DESC_DATA_ED_RW_A:
237 case DESC_CODE_N_NC_A: case DESC_CODE_N_NC_NA: case DESC_CODE_R_NC_A: case DESC_CODE_R_NC_NA:
238 if (cpu.cpl>desc.DPL()) needs_invalidation=true; break;
239 default: break; }
240 if (needs_invalidation) CPU_SetSegGeneral(es,0);
241
242 needs_invalidation=false;
243 if (!cpu.gdt.GetDescriptor(SegValue(ds),desc)) needs_invalidation=true;
244 else switch (desc.Type()) {
245 case DESC_DATA_EU_RO_NA: case DESC_DATA_EU_RO_A: case DESC_DATA_EU_RW_NA: case DESC_DATA_EU_RW_A:
246 case DESC_DATA_ED_RO_NA: case DESC_DATA_ED_RO_A: case DESC_DATA_ED_RW_NA: case DESC_DATA_ED_RW_A:
247 case DESC_CODE_N_NC_A: case DESC_CODE_N_NC_NA: case DESC_CODE_R_NC_A: case DESC_CODE_R_NC_NA:
248 if (cpu.cpl>desc.DPL()) needs_invalidation=true; break;
249 default: break; }
250 if (needs_invalidation) CPU_SetSegGeneral(ds,0);
251
252 needs_invalidation=false;
253 if (!cpu.gdt.GetDescriptor(SegValue(fs),desc)) needs_invalidation=true;
254 else switch (desc.Type()) {
255 case DESC_DATA_EU_RO_NA: case DESC_DATA_EU_RO_A: case DESC_DATA_EU_RW_NA: case DESC_DATA_EU_RW_A:
256 case DESC_DATA_ED_RO_NA: case DESC_DATA_ED_RO_A: case DESC_DATA_ED_RW_NA: case DESC_DATA_ED_RW_A:
257 case DESC_CODE_N_NC_A: case DESC_CODE_N_NC_NA: case DESC_CODE_R_NC_A: case DESC_CODE_R_NC_NA:
258 if (cpu.cpl>desc.DPL()) needs_invalidation=true; break;
259 default: break; }
260 if (needs_invalidation) CPU_SetSegGeneral(fs,0);
261
262 needs_invalidation=false;
263 if (!cpu.gdt.GetDescriptor(SegValue(gs),desc)) needs_invalidation=true;
264 else switch (desc.Type()) {
265 case DESC_DATA_EU_RO_NA: case DESC_DATA_EU_RO_A: case DESC_DATA_EU_RW_NA: case DESC_DATA_EU_RW_A:
266 case DESC_DATA_ED_RO_NA: case DESC_DATA_ED_RO_A: case DESC_DATA_ED_RW_NA: case DESC_DATA_ED_RW_A:
267 case DESC_CODE_N_NC_A: case DESC_CODE_N_NC_NA: case DESC_CODE_R_NC_A: case DESC_CODE_R_NC_NA:
268 if (cpu.cpl>desc.DPL()) needs_invalidation=true; break;
269 default: break; }
270 if (needs_invalidation) CPU_SetSegGeneral(gs,0);
271 }
272
273
274 class TaskStateSegment {
275 public:
276 TaskStateSegment() {
277 valid=false;
278 }
279 bool IsValid(void) {
280 return valid;
281 }
282 Bitu Get_back(void) {
283 cpu.mpl=0;
284 Bit16u backlink=mem_readw(base);
285 cpu.mpl=3;
286 return backlink;
287 }
288 void SaveSelector(void) {
289 cpu.gdt.SetDescriptor(selector,desc);
290 }
291 void Get_SSx_ESPx(Bitu level,Bitu & _ss,Bitu & _esp) {
292 cpu.mpl=0;
293 if (is386) {
294 PhysPt where=base+offsetof(TSS_32,esp0)+level*8;
295 _esp=mem_readd(where);
296 _ss=mem_readw(where+4);
297 } else {
298 PhysPt where=base+offsetof(TSS_16,sp0)+level*4;
299 _esp=mem_readw(where);
300 _ss=mem_readw(where+2);
301 }
302 cpu.mpl=3;
303 }
304 bool SetSelector(Bitu new_sel) {
305 valid=false;
306 if ((new_sel & 0xfffc)==0) {
307 selector=0;
308 base=0;
309 limit=0;
310 is386=1;
311 return true;
312 }
313 if (new_sel&4) return false;
314 if (!cpu.gdt.GetDescriptor(new_sel,desc)) return false;
315 switch (desc.Type()) {
316 case DESC_286_TSS_A: case DESC_286_TSS_B:
317 case DESC_386_TSS_A: case DESC_386_TSS_B:
318 break;
319 default:
320 return false;
321 }
322 if (!desc.saved.seg.p) return false;
323 selector=new_sel;
324 valid=true;
325 base=desc.GetBase();
326 limit=desc.GetLimit();
327 is386=desc.Is386();
328 return true;
329 }
330 TSS_Descriptor desc;
331 Bitu selector;
332 PhysPt base;
333 Bitu limit;
334 Bitu is386;
335 bool valid;
336 };
337
338 TaskStateSegment cpu_tss;
339
340 enum TSwitchType {
341 TSwitch_JMP,TSwitch_CALL_INT,TSwitch_IRET
342 };
343
344 bool CPU_SwitchTask(Bitu new_tss_selector,TSwitchType tstype,Bitu old_eip) {
345 FillFlags();
346 TaskStateSegment new_tss;
347 if (!new_tss.SetSelector(new_tss_selector))
348 E_Exit("Illegal TSS for switch, selector=%x, switchtype=%x",new_tss_selector,tstype);
349 if (tstype==TSwitch_IRET) {
350 if (!new_tss.desc.IsBusy())
351 E_Exit("TSS not busy for IRET");
352 } else {
353 if (new_tss.desc.IsBusy())
354 E_Exit("TSS busy for JMP/CALL/INT");
355 }
356 Bitu new_cr3=0;
357 Bitu new_eax,new_ebx,new_ecx,new_edx,new_esp,new_ebp,new_esi,new_edi;
358 Bitu new_es,new_cs,new_ss,new_ds,new_fs,new_gs;
359 Bitu new_ldt,new_eip,new_eflags;
360 /* Read new context from new TSS */
361 if (new_tss.is386) {
362 new_cr3=mem_readd(new_tss.base+offsetof(TSS_32,cr3));
363 new_eip=mem_readd(new_tss.base+offsetof(TSS_32,eip));
364 new_eflags=mem_readd(new_tss.base+offsetof(TSS_32,eflags));
365 new_eax=mem_readd(new_tss.base+offsetof(TSS_32,eax));
366 new_ecx=mem_readd(new_tss.base+offsetof(TSS_32,ecx));
367 new_edx=mem_readd(new_tss.base+offsetof(TSS_32,edx));
368 new_ebx=mem_readd(new_tss.base+offsetof(TSS_32,ebx));
369 new_esp=mem_readd(new_tss.base+offsetof(TSS_32,esp));
370 new_ebp=mem_readd(new_tss.base+offsetof(TSS_32,ebp));
371 new_edi=mem_readd(new_tss.base+offsetof(TSS_32,edi));
372 new_esi=mem_readd(new_tss.base+offsetof(TSS_32,esi));
373
374 new_es=mem_readw(new_tss.base+offsetof(TSS_32,es));
375 new_cs=mem_readw(new_tss.base+offsetof(TSS_32,cs));
376 new_ss=mem_readw(new_tss.base+offsetof(TSS_32,ss));
377 new_ds=mem_readw(new_tss.base+offsetof(TSS_32,ds));
378 new_fs=mem_readw(new_tss.base+offsetof(TSS_32,fs));
379 new_gs=mem_readw(new_tss.base+offsetof(TSS_32,gs));
380 new_ldt=mem_readw(new_tss.base+offsetof(TSS_32,ldt));
381 } else {
382 E_Exit("286 task switch");
383 new_cr3=0;
384 new_eip=0;
385 new_eflags=0;
386 new_eax=0; new_ecx=0; new_edx=0; new_ebx=0;
387 new_esp=0; new_ebp=0; new_edi=0; new_esi=0;
388
389 new_es=0; new_cs=0; new_ss=0; new_ds=0; new_fs=0; new_gs=0;
390 new_ldt=0;
391 }
392
393 /* Check if we need to clear busy bit of old TASK */
394 if (tstype==TSwitch_JMP || tstype==TSwitch_IRET) {
395 cpu_tss.desc.SetBusy(false);
396 cpu_tss.SaveSelector();
397 }
398 Bit32u old_flags = reg_flags;
399 if (tstype==TSwitch_IRET) old_flags &= (~FLAG_NT);
400
401 /* Save current context in current TSS */
402 if (cpu_tss.is386) {
403 mem_writed(cpu_tss.base+offsetof(TSS_32,eflags),old_flags);
404 mem_writed(cpu_tss.base+offsetof(TSS_32,eip),old_eip);
405
406 mem_writed(cpu_tss.base+offsetof(TSS_32,eax),reg_eax);
407 mem_writed(cpu_tss.base+offsetof(TSS_32,ecx),reg_ecx);
408 mem_writed(cpu_tss.base+offsetof(TSS_32,edx),reg_edx);
409 mem_writed(cpu_tss.base+offsetof(TSS_32,ebx),reg_ebx);
410 mem_writed(cpu_tss.base+offsetof(TSS_32,esp),reg_esp);
411 mem_writed(cpu_tss.base+offsetof(TSS_32,ebp),reg_ebp);
412 mem_writed(cpu_tss.base+offsetof(TSS_32,esi),reg_esi);
413 mem_writed(cpu_tss.base+offsetof(TSS_32,edi),reg_edi);
414
415 mem_writed(cpu_tss.base+offsetof(TSS_32,es),SegValue(es));
416 mem_writed(cpu_tss.base+offsetof(TSS_32,cs),SegValue(cs));
417 mem_writed(cpu_tss.base+offsetof(TSS_32,ss),SegValue(ss));
418 mem_writed(cpu_tss.base+offsetof(TSS_32,ds),SegValue(ds));
419 mem_writed(cpu_tss.base+offsetof(TSS_32,fs),SegValue(fs));
420 mem_writed(cpu_tss.base+offsetof(TSS_32,gs),SegValue(gs));
421 } else {
422 E_Exit("286 task switch");
423 }
424
425 /* Setup a back link to the old TSS in new TSS */
426 if (tstype==TSwitch_CALL_INT) {
427 if (new_tss.is386) {
428 mem_writed(new_tss.base+offsetof(TSS_32,back),cpu_tss.selector);
429 } else {
430 mem_writew(new_tss.base+offsetof(TSS_16,back),cpu_tss.selector);
431 }
432 /* And make the new task's eflag have the nested task bit */
433 new_eflags|=FLAG_NT;
434 }
435 /* Set the busy bit in the new task */
436 if (tstype==TSwitch_JMP || tstype==TSwitch_CALL_INT) {
437 new_tss.desc.SetBusy(true);
438 new_tss.SaveSelector();
439 }
440
441 // cpu.cr0|=CR0_TASKSWITCHED;
442 if (new_tss_selector == cpu_tss.selector) {
443 reg_eip = old_eip;
444 new_cs = SegValue(cs);
445 new_ss = SegValue(ss);
446 new_ds = SegValue(ds);
447 new_es = SegValue(es);
448 new_fs = SegValue(fs);
449 new_gs = SegValue(gs);
450 } else {
451
452 /* Setup the new cr3 */
453 PAGING_SetDirBase(new_cr3);
454
455 /* Load new context */
456 if (new_tss.is386) {
457 reg_eip=new_eip;
458 CPU_SetFlags(new_eflags,FMASK_ALL | FLAG_VM);
459 reg_eax=new_eax;
460 reg_ecx=new_ecx;
461 reg_edx=new_edx;
462 reg_ebx=new_ebx;
463 reg_esp=new_esp;
464 reg_ebp=new_ebp;
465 reg_edi=new_edi;
466 reg_esi=new_esi;
467
468 // new_cs=mem_readw(new_tss.base+offsetof(TSS_32,cs));
469 } else {
470 E_Exit("286 task switch");
471 }
472 }
473 /* Load the new selectors */
474 if (reg_flags & FLAG_VM) {
475 SegSet16(cs,new_cs);
476 cpu.code.big=false;
477 cpu.cpl=3; //We don't have segment caches so this will do
478 } else {
479 /* Protected mode task */
480 if (new_ldt!=0) CPU_LLDT(new_ldt);
481 /* Load the new CS*/
482 Descriptor cs_desc;
483 cpu.cpl=new_cs & 3;
484 if (!cpu.gdt.GetDescriptor(new_cs,cs_desc))
485 E_Exit("Task switch with CS beyond limits");
486 if (!cs_desc.saved.seg.p)
487 E_Exit("Task switch with non present code-segment");
488 switch (cs_desc.Type()) {
489 case DESC_CODE_N_NC_A: case DESC_CODE_N_NC_NA:
490 case DESC_CODE_R_NC_A: case DESC_CODE_R_NC_NA:
491 if (cpu.cpl != cs_desc.DPL()) E_Exit("Task CS RPL != DPL");
492 goto doconforming;
493 case DESC_CODE_N_C_A: case DESC_CODE_N_C_NA:
494 case DESC_CODE_R_C_A: case DESC_CODE_R_C_NA:
495 if (cpu.cpl < cs_desc.DPL()) E_Exit("Task CS RPL < DPL");
496 doconforming:
497 Segs.phys[cs]=cs_desc.GetBase();
498 cpu.code.big=cs_desc.Big()>0;
499 Segs.val[cs]=new_cs;
500 break;
501 default:
502 E_Exit("Task switch CS Type %d",cs_desc.Type());
503 }
504 }
505 CPU_SetSegGeneral(es,new_es);
506 CPU_SetSegGeneral(ss,new_ss);
507 CPU_SetSegGeneral(ds,new_ds);
508 CPU_SetSegGeneral(fs,new_fs);
509 CPU_SetSegGeneral(gs,new_gs);
510 if (!cpu_tss.SetSelector(new_tss_selector)) {
511 LOG(LOG_CPU,LOG_NORMAL)("TaskSwitch: set tss selector %X failed",new_tss_selector);
512 }
513 // cpu_tss.desc.SetBusy(true);
514 // cpu_tss.SaveSelector();
515 // LOG_MSG("Task CPL %X CS:%X IP:%X SS:%X SP:%X eflags %x",cpu.cpl,SegValue(cs),reg_eip,SegValue(ss),reg_esp,reg_flags);
516 return true;
517 }
518
519 bool CPU_IO_Exception(Bitu port,Bitu size) {
520 if (cpu.pmode && ((GETFLAG_IOPL<cpu.cpl) || GETFLAG(VM))) {
521 cpu.mpl=0;
522 if (!cpu_tss.is386) goto doexception;
523 PhysPt bwhere=cpu_tss.base+0x66;
524 Bitu ofs=mem_readw(bwhere);
525 if (ofs>cpu_tss.limit) goto doexception;
526 bwhere=cpu_tss.base+ofs+(port/8);
527 Bitu map=mem_readw(bwhere);
528 Bitu mask=(0xffff>>(16-size)) << (port&7);
529 if (map & mask) goto doexception;
530 cpu.mpl=3;
531 }
532 return false;
533 doexception:
534 cpu.mpl=3;
535 LOG(LOG_CPU,LOG_NORMAL)("IO Exception port %X",port);
536 return CPU_PrepareException(EXCEPTION_GP,0);
537 }
538
539 void CPU_Exception(Bitu which,Bitu error ) {
540 // LOG_MSG("Exception %d error %x",which,error);
541 cpu.exception.error=error;
542 CPU_Interrupt(which,CPU_INT_EXCEPTION | ((which>=8) ? CPU_INT_HAS_ERROR : 0),reg_eip);
543 }
544
545 Bit8u lastint;
546 void CPU_Interrupt(Bitu num,Bitu type,Bitu oldeip) {
547 lastint=num;
548 FillFlags();
549 #if C_DEBUG
550 switch (num) {
551 case 0xcd:
552 #if C_HEAVY_DEBUG
553 LOG(LOG_CPU,LOG_ERROR)("Call to interrupt 0xCD this is BAD");
554 DEBUG_HeavyWriteLogInstruction();
555 E_Exit("Call to interrupt 0xCD this is BAD");
556 #endif
557 break;
558 case 0x03:
559 if (DEBUG_Breakpoint()) {
560 CPU_Cycles=0;
561 return;
562 }
563 };
564 #endif
565 if (!cpu.pmode) {
566 /* Save everything on a 16-bit stack */
567 CPU_Push16(reg_flags & 0xffff);
568 CPU_Push16(SegValue(cs));
569 CPU_Push16(oldeip);
570 SETFLAGBIT(IF,false);
571 SETFLAGBIT(TF,false);
572 /* Get the new CS:IP from vector table */
573 PhysPt base=cpu.idt.GetBase();
574 reg_eip=mem_readw(base+(num << 2));
575 Segs.val[cs]=mem_readw(base+(num << 2)+2);
576 Segs.phys[cs]=Segs.val[cs]<<4;
577 cpu.code.big=false;
578 return;
579 } else {
580 /* Protected Mode Interrupt */
581 if ((reg_flags & FLAG_VM) && (type&CPU_INT_SOFTWARE) && !(type&CPU_INT_NOIOPLCHECK)) {
582 // LOG_MSG("Software int in v86, AH %X IOPL %x",reg_ah,(reg_flags & FLAG_IOPL) >>12);
583 if ((reg_flags & FLAG_IOPL)!=FLAG_IOPL) {
584 CPU_Exception(EXCEPTION_GP,0);
585 return;
586 }
587 }
588
589 Descriptor gate;
590 if (!cpu.idt.GetDescriptor(num<<3,gate)) {
591 // zone66
592 CPU_Exception(EXCEPTION_GP,num*8+2+(type&CPU_INT_SOFTWARE)?0:1);
593 return;
594 }
595
596 if ((type&CPU_INT_SOFTWARE) && (gate.DPL()<cpu.cpl)) {
597 // zone66, win3.x e
598 CPU_Exception(EXCEPTION_GP,num*8+2);
599 return;
600 }
601
602
603 switch (gate.Type()) {
604 case DESC_286_INT_GATE: case DESC_386_INT_GATE:
605 case DESC_286_TRAP_GATE: case DESC_386_TRAP_GATE:
606 {
607 CPU_CHECK_COND(!gate.saved.seg.p,
608 "INT:Gate segment not present",
609 EXCEPTION_NP,num*8+2+(type&CPU_INT_SOFTWARE)?0:1)
610
611 Descriptor cs_desc;
612 Bitu gate_sel=gate.GetSelector();
613 Bitu gate_off=gate.GetOffset();
614 CPU_CHECK_COND((gate_sel & 0xfffc)==0,
615 "INT:Gate with CS zero selector",
616 EXCEPTION_GP,(type&CPU_INT_SOFTWARE)?0:1)
617 CPU_CHECK_COND(!cpu.gdt.GetDescriptor(gate_sel,cs_desc),
618 "INT:Gate with CS beyond limit",
619 EXCEPTION_GP,(gate_sel & 0xfffc)+(type&CPU_INT_SOFTWARE)?0:1)
620
621 Bitu cs_dpl=cs_desc.DPL();
622 CPU_CHECK_COND(cs_dpl>cpu.cpl,
623 "Interrupt to higher privilege",
624 EXCEPTION_GP,(gate_sel & 0xfffc)+(type&CPU_INT_SOFTWARE)?0:1)
625 switch (cs_desc.Type()) {
626 case DESC_CODE_N_NC_A: case DESC_CODE_N_NC_NA:
627 case DESC_CODE_R_NC_A: case DESC_CODE_R_NC_NA:
628 if (cs_dpl<cpu.cpl) {
629 /* Prepare for gate to inner level */
630 CPU_CHECK_COND(!cs_desc.saved.seg.p,
631 "INT:Inner level:CS segment not present",
632 EXCEPTION_NP,(gate_sel & 0xfffc)+(type&CPU_INT_SOFTWARE)?0:1)
633 CPU_CHECK_COND((reg_flags & FLAG_VM) && (cs_dpl!=0),
634 "V86 interrupt calling codesegment with DPL>0",
635 EXCEPTION_GP,gate_sel & 0xfffc)
636
637 Bitu n_ss,n_esp;
638 Bitu o_ss,o_esp;
639 o_ss=SegValue(ss);
640 o_esp=reg_esp;
641 cpu_tss.Get_SSx_ESPx(cs_dpl,n_ss,n_esp);
642 CPU_CHECK_COND((n_ss & 0xfffc)==0,
643 "INT:Gate with SS zero selector",
644 EXCEPTION_TS,(type&CPU_INT_SOFTWARE)?0:1)
645 Descriptor n_ss_desc;
646 CPU_CHECK_COND(!cpu.gdt.GetDescriptor(n_ss,n_ss_desc),
647 "INT:Gate with SS beyond limit",
648 EXCEPTION_TS,(n_ss & 0xfffc)+(type&CPU_INT_SOFTWARE)?0:1)
649 CPU_CHECK_COND(((n_ss & 3)!=cs_dpl) || (n_ss_desc.DPL()!=cs_dpl),
650 "INT:Inner level with CS_DPL!=SS_DPL and SS_RPL",
651 EXCEPTION_TS,(n_ss & 0xfffc)+(type&CPU_INT_SOFTWARE)?0:1)
652
653 // check if stack segment is a writable data segment
654 switch (n_ss_desc.Type()) {
655 case DESC_DATA_EU_RW_NA: case DESC_DATA_EU_RW_A:
656 case DESC_DATA_ED_RW_NA: case DESC_DATA_ED_RW_A:
657 break;
658 default:
659 E_Exit("INT:Inner level:Stack segment not writable."); // or #TS(ss_sel+EXT)
660 }
661 CPU_CHECK_COND(!n_ss_desc.saved.seg.p,
662 "INT:Inner level with nonpresent SS",
663 EXCEPTION_SS,(n_ss & 0xfffc)+(type&CPU_INT_SOFTWARE)?0:1)
664
665 // commit point
666 Segs.phys[ss]=n_ss_desc.GetBase();
667 Segs.val[ss]=n_ss;
668 if (n_ss_desc.Big()) {
669 cpu.stack.big=true;
670 cpu.stack.mask=0xffffffff;
671 cpu.stack.notmask=0;
672 reg_esp=n_esp;
673 } else {
674 cpu.stack.big=false;
675 cpu.stack.mask=0xffff;
676 cpu.stack.notmask=0xffff0000;
677 reg_sp=n_esp & 0xffff;
678 }
679
680 cpu.cpl=cs_dpl;
681 if (gate.Type() & 0x8) { /* 32-bit Gate */
682 if (reg_flags & FLAG_VM) {
683 CPU_Push32(SegValue(gs));SegSet16(gs,0x0);
684 CPU_Push32(SegValue(fs));SegSet16(fs,0x0);
685 CPU_Push32(SegValue(ds));SegSet16(ds,0x0);
686 CPU_Push32(SegValue(es));SegSet16(es,0x0);
687 }
688 CPU_Push32(o_ss);
689 CPU_Push32(o_esp);
690 } else { /* 16-bit Gate */
691 if (reg_flags & FLAG_VM) E_Exit("V86 to 16-bit gate");
692 CPU_Push16(o_ss);
693 CPU_Push16(o_esp);
694 }
695 // LOG_MSG("INT:Gate to inner level SS:%X SP:%X",n_ss,n_esp);
696 goto do_interrupt;
697 }
698 if (cs_dpl!=cpu.cpl)
699 E_Exit("Non-conforming intra privilege INT with DPL!=CPL");
700 case DESC_CODE_N_C_A: case DESC_CODE_N_C_NA:
701 case DESC_CODE_R_C_A: case DESC_CODE_R_C_NA:
702 /* Prepare stack for gate to same priviledge */
703 CPU_CHECK_COND(!cs_desc.saved.seg.p,
704 "INT:Same level:CS segment not present",
705 EXCEPTION_NP,(gate_sel & 0xfffc)+(type&CPU_INT_SOFTWARE)?0:1)
706 if ((reg_flags & FLAG_VM) && (cs_dpl<cpu.cpl))
707 E_Exit("V86 interrupt doesn't change to pl0"); // or #GP(cs_sel)
708
709 // commit point
710 do_interrupt:
711 if (gate.Type() & 0x8) { /* 32-bit Gate */
712 CPU_Push32(reg_flags);
713 CPU_Push32(SegValue(cs));
714 CPU_Push32(oldeip);
715 if (type & CPU_INT_HAS_ERROR) CPU_Push32(cpu.exception.error);
716 } else { /* 16-bit gate */
717 CPU_Push16(reg_flags & 0xffff);
718 CPU_Push16(SegValue(cs));
719 CPU_Push16(oldeip);
720 if (type & CPU_INT_HAS_ERROR) CPU_Push16(cpu.exception.error);
721 }
722 break;
723 default:
724 E_Exit("INT:Gate Selector points to illegal descriptor with type %x",cs_desc.Type());
725 }
726
727 Segs.val[cs]=(gate_sel&0xfffc) | cpu.cpl;
728 Segs.phys[cs]=cs_desc.GetBase();
729 cpu.code.big=cs_desc.Big()>0;
730 reg_eip=gate_off;
731
732 if (!(gate.Type()&1)) {
733 SETFLAGBIT(IF,false);
734 }
735 SETFLAGBIT(TF,false);
736 SETFLAGBIT(NT,false);
737 SETFLAGBIT(VM,false);
738 LOG(LOG_CPU,LOG_NORMAL)("INT:Gate to %X:%X big %d %s",gate_sel,gate_off,cs_desc.Big(),gate.Type() & 0x8 ? "386" : "286");
739 return;
740 }
741 case DESC_TASK_GATE:
742 CPU_CHECK_COND(!gate.saved.seg.p,
743 "INT:Gate segment not present",
744 EXCEPTION_NP,num*8+2+(type&CPU_INT_SOFTWARE)?0:1)
745
746 CPU_SwitchTask(gate.GetSelector(),TSwitch_CALL_INT,oldeip);
747 if (type & CPU_INT_HAS_ERROR) {
748 //TODO Be sure about this, seems somewhat unclear
749 if (cpu_tss.is386) CPU_Push32(cpu.exception.error);
750 else CPU_Push16(cpu.exception.error);
751 }
752 return;
753 default:
754 E_Exit("Illegal descriptor type %X for int %X",gate.Type(),num);
755 }
756 }
757 assert(1);
758 return ; // make compiler happy
759 }
760
761
762 void CPU_IRET(bool use32,Bitu oldeip) {
763 if (!cpu.pmode) { /* RealMode IRET */
764 if (use32) {
765 reg_eip=CPU_Pop32();
766 SegSet16(cs,CPU_Pop32());
767 CPU_SetFlags(CPU_Pop32(),FMASK_ALL);
768 } else {
769 reg_eip=CPU_Pop16();
770 SegSet16(cs,CPU_Pop16());
771 CPU_SetFlags(CPU_Pop16(),FMASK_ALL & 0xffff);
772 }
773 cpu.code.big=false;
774 DestroyConditionFlags();
775 return;
776 } else { /* Protected mode IRET */
777 if (reg_flags & FLAG_VM) {
778 if ((reg_flags & FLAG_IOPL)!=FLAG_IOPL) {
779 // win3.x e
780 CPU_Exception(EXCEPTION_GP,0);
781 return;
782 } else {
783 if (use32) {
784 Bit32u new_eip=mem_readd(SegPhys(ss) + (reg_esp & cpu.stack.mask));
785 Bit32u tempesp=(reg_esp&cpu.stack.notmask)|((reg_esp+4)&cpu.stack.mask);
786 Bit32u new_cs=mem_readd(SegPhys(ss) + (tempesp & cpu.stack.mask));
787 tempesp=(tempesp&cpu.stack.notmask)|((tempesp+4)&cpu.stack.mask);
788 Bit32u new_flags=mem_readd(SegPhys(ss) + (tempesp & cpu.stack.mask));
789 reg_esp=(tempesp&cpu.stack.notmask)|((tempesp+4)&cpu.stack.mask);
790
791 reg_eip=new_eip;
792 SegSet16(cs,(Bit16u)(new_cs&0xffff));
793 /* IOPL can not be modified in v86 mode by IRET */
794 CPU_SetFlags(new_flags,FMASK_NORMAL|FLAG_NT);
795 } else {
796 Bit16u new_eip=mem_readw(SegPhys(ss) + (reg_esp & cpu.stack.mask));
797 Bit32u tempesp=(reg_esp&cpu.stack.notmask)|((reg_esp+2)&cpu.stack.mask);
798 Bit16u new_cs=mem_readw(SegPhys(ss) + (tempesp & cpu.stack.mask));
799 tempesp=(tempesp&cpu.stack.notmask)|((tempesp+2)&cpu.stack.mask);
800 Bit16u new_flags=mem_readw(SegPhys(ss) + (tempesp & cpu.stack.mask));
801 reg_esp=(tempesp&cpu.stack.notmask)|((tempesp+2)&cpu.stack.mask);
802
803 reg_eip=(Bit32u)new_eip;
804 SegSet16(cs,new_cs);
805 /* IOPL can not be modified in v86 mode by IRET */
806 CPU_SetFlags(new_flags,FMASK_NORMAL|FLAG_NT);
807 }
808 cpu.code.big=false;
809 DestroyConditionFlags();
810 return;
811 }
812 }
813 /* Check if this is task IRET */
814 if (GETFLAG(NT)) {
815 if (GETFLAG(VM)) E_Exit("Pmode IRET with VM bit set");
816 CPU_CHECK_COND(!cpu_tss.IsValid(),
817 "TASK Iret without valid TSS",
818 EXCEPTION_TS,cpu_tss.selector & 0xfffc)
819 if (!cpu_tss.desc.IsBusy()) {
820 LOG(LOG_CPU,LOG_ERROR)("TASK Iret:TSS not busy");
821 }
822 Bitu back_link=cpu_tss.Get_back();
823 CPU_SwitchTask(back_link,TSwitch_IRET,oldeip);
824 return;
825 }
826 Bitu n_cs_sel,n_eip,n_flags;
827 Bit32u tempesp;
828 if (use32) {
829 n_eip=mem_readd(SegPhys(ss) + (reg_esp & cpu.stack.mask));
830 tempesp=(reg_esp&cpu.stack.notmask)|((reg_esp+4)&cpu.stack.mask);
831 n_cs_sel=mem_readd(SegPhys(ss) + (tempesp & cpu.stack.mask)) & 0xffff;
832 tempesp=(tempesp&cpu.stack.notmask)|((tempesp+4)&cpu.stack.mask);
833 n_flags=mem_readd(SegPhys(ss) + (tempesp & cpu.stack.mask));
834 tempesp=(tempesp&cpu.stack.notmask)|((tempesp+4)&cpu.stack.mask);
835
836 if ((n_flags & FLAG_VM) && (cpu.cpl==0)) {
837 // commit point
838 reg_esp=tempesp;
839 reg_eip=n_eip & 0xffff;
840 Bitu n_ss,n_esp,n_es,n_ds,n_fs,n_gs;
841 n_esp=CPU_Pop32();
842 n_ss=CPU_Pop32() & 0xffff;
843 n_es=CPU_Pop32() & 0xffff;
844 n_ds=CPU_Pop32() & 0xffff;
845 n_fs=CPU_Pop32() & 0xffff;
846 n_gs=CPU_Pop32() & 0xffff;
847
848 CPU_SetFlags(n_flags,FMASK_ALL | FLAG_VM);
849 DestroyConditionFlags();
850 cpu.cpl=3;
851
852 CPU_SetSegGeneral(ss,n_ss);
853 CPU_SetSegGeneral(es,n_es);
854 CPU_SetSegGeneral(ds,n_ds);
855 CPU_SetSegGeneral(fs,n_fs);
856 CPU_SetSegGeneral(gs,n_gs);
857 reg_esp=n_esp;
858 cpu.code.big=false;
859 SegSet16(cs,n_cs_sel);
860 LOG(LOG_CPU,LOG_NORMAL)("IRET:Back to V86: CS:%X IP %X SS:%X SP %X FLAGS:%X",SegValue(cs),reg_eip,SegValue(ss),reg_esp,reg_flags);
861 return;
862 }
863 if (n_flags & FLAG_VM) E_Exit("IRET from pmode to v86 with CPL!=0");
864 } else {
865 n_eip=mem_readw(SegPhys(ss) + (reg_esp & cpu.stack.mask));
866 tempesp=(reg_esp&cpu.stack.notmask)|((reg_esp+2)&cpu.stack.mask);
867 n_cs_sel=mem_readw(SegPhys(ss) + (tempesp & cpu.stack.mask));
868 tempesp=(tempesp&cpu.stack.notmask)|((tempesp+2)&cpu.stack.mask);
869 n_flags=mem_readw(SegPhys(ss) + (tempesp & cpu.stack.mask));
870 n_flags|=(reg_flags & 0xffff0000);
871 tempesp=(tempesp&cpu.stack.notmask)|((tempesp+2)&cpu.stack.mask);
872
873 if (n_flags & FLAG_VM) E_Exit("VM Flag in 16-bit iret");
874 }
875 CPU_CHECK_COND((n_cs_sel & 0xfffc)==0,
876 "IRET:CS selector zero",
877 EXCEPTION_GP,0)
878 Bitu n_cs_rpl=n_cs_sel & 3;
879 Descriptor n_cs_desc;
880 CPU_CHECK_COND(!cpu.gdt.GetDescriptor(n_cs_sel,n_cs_desc),
881 "IRET:CS selector beyond limits",
882 EXCEPTION_GP,n_cs_sel & 0xfffc)
883 CPU_CHECK_COND(n_cs_rpl<cpu.cpl,
884 "IRET to lower privilege",
885 EXCEPTION_GP,n_cs_sel & 0xfffc)
886
887 switch (n_cs_desc.Type()) {
888 case DESC_CODE_N_NC_A: case DESC_CODE_N_NC_NA:
889 case DESC_CODE_R_NC_A: case DESC_CODE_R_NC_NA:
890 CPU_CHECK_COND(n_cs_rpl!=n_cs_desc.DPL(),
891 "IRET:NC:DPL!=RPL",
892 EXCEPTION_GP,n_cs_sel & 0xfffc)
893 break;
894 case DESC_CODE_N_C_A: case DESC_CODE_N_C_NA:
895 case DESC_CODE_R_C_A: case DESC_CODE_R_C_NA:
896 CPU_CHECK_COND(n_cs_desc.DPL()>n_cs_rpl,
897 "IRET:C:DPL>RPL",
898 EXCEPTION_GP,n_cs_sel & 0xfffc)
899 break;
900 default:
901 E_Exit("IRET:Illegal descriptor type %X",n_cs_desc.Type());
902 }
903 CPU_CHECK_COND(!n_cs_desc.saved.seg.p,
904 "IRET with nonpresent code segment",
905 EXCEPTION_NP,n_cs_sel & 0xfffc)
906
907 if (n_cs_rpl==cpu.cpl) {
908 /* Return to same level */
909
910 // commit point
911 reg_esp=tempesp;
912 Segs.phys[cs]=n_cs_desc.GetBase();
913 cpu.code.big=n_cs_desc.Big()>0;
914 Segs.val[cs]=n_cs_sel;
915 reg_eip=n_eip;
916
917 Bitu mask=cpu.cpl ? (FMASK_NORMAL | FLAG_NT) : FMASK_ALL;
918 if (GETFLAG_IOPL<cpu.cpl) mask &= (~FLAG_IF);
919 CPU_SetFlags(n_flags,mask);
920 DestroyConditionFlags();
921 LOG(LOG_CPU,LOG_NORMAL)("IRET:Same level:%X:%X big %d",n_cs_sel,n_eip,cpu.code.big);
922 } else {
923 /* Return to outer level */
924 Bitu n_ss,n_esp;
925 if (use32) {
926 n_esp=mem_readd(SegPhys(ss) + (tempesp & cpu.stack.mask));
927 tempesp=(tempesp&cpu.stack.notmask)|((tempesp+4)&cpu.stack.mask);
928 n_ss=mem_readd(SegPhys(ss) + (tempesp & cpu.stack.mask)) & 0xffff;
929 } else {
930 n_esp=mem_readw(SegPhys(ss) + (tempesp & cpu.stack.mask));
931 tempesp=(tempesp&cpu.stack.notmask)|((tempesp+2)&cpu.stack.mask);
932 n_ss=mem_readw(SegPhys(ss) + (tempesp & cpu.stack.mask));
933 }
934 CPU_CHECK_COND((n_ss & 0xfffc)==0,
935 "IRET:Outer level:SS selector zero",
936 EXCEPTION_GP,0)
937 CPU_CHECK_COND((n_ss & 3)!=n_cs_rpl,
938 "IRET:Outer level:SS rpl!=CS rpl",
939 EXCEPTION_GP,n_ss & 0xfffc)
940 Descriptor n_ss_desc;
941 CPU_CHECK_COND(!cpu.gdt.GetDescriptor(n_ss,n_ss_desc),
942 "IRET:Outer level:SS beyond limit",
943 EXCEPTION_GP,n_ss & 0xfffc)
944 CPU_CHECK_COND(n_ss_desc.DPL()!=n_cs_rpl,
945 "IRET:Outer level:SS dpl!=CS rpl",
946 EXCEPTION_GP,n_ss & 0xfffc)
947
948 // check if stack segment is a writable data segment
949 switch (n_ss_desc.Type()) {
950 case DESC_DATA_EU_RW_NA: case DESC_DATA_EU_RW_A:
951 case DESC_DATA_ED_RW_NA: case DESC_DATA_ED_RW_A:
952 break;
953 default:
954 E_Exit("IRET:Outer level:Stack segment not writable"); // or #GP(ss_sel)
955 }
956 CPU_CHECK_COND(!n_ss_desc.saved.seg.p,
957 "IRET:Outer level:Stack segment not present",
958 EXCEPTION_NP,n_ss & 0xfffc)
959
960 // commit point
961
962 Segs.phys[cs]=n_cs_desc.GetBase();
963 cpu.code.big=n_cs_desc.Big()>0;
964 Segs.val[cs]=n_cs_sel;
965
966 Bitu mask=cpu.cpl ? (FMASK_NORMAL | FLAG_NT) : FMASK_ALL;
967 if (GETFLAG_IOPL<cpu.cpl) mask &= (~FLAG_IF);
968 CPU_SetFlags(n_flags,mask);
969 DestroyConditionFlags();
970
971 cpu.cpl=n_cs_rpl;
972 reg_eip=n_eip;
973
974 Segs.val[ss]=n_ss;
975 Segs.phys[ss]=n_ss_desc.GetBase();
976 if (n_ss_desc.Big()) {
977 cpu.stack.big=true;
978 cpu.stack.mask=0xffffffff;
979 cpu.stack.notmask=0;
980 reg_esp=n_esp;
981 } else {
982 cpu.stack.big=false;
983 cpu.stack.mask=0xffff;
984 cpu.stack.notmask=0xffff0000;
985 reg_sp=n_esp & 0xffff;
986 }
987
988 // borland extender, zrdx
989 CPU_CheckSegments();
990
991 LOG(LOG_CPU,LOG_NORMAL)("IRET:Outer level:%X:%X big %d",n_cs_sel,n_eip,cpu.code.big);
992 }
993 return;
994 }
995 }
996
997
998 void CPU_JMP(bool use32,Bitu selector,Bitu offset,Bitu oldeip) {
999 if (!cpu.pmode || (reg_flags & FLAG_VM)) {
1000 if (!use32) {
1001 reg_eip=offset&0xffff;
1002 } else {
1003 reg_eip=offset;
1004 }
1005 SegSet16(cs,selector);
1006 cpu.code.big=false;
1007 return;
1008 } else {
1009 CPU_CHECK_COND((selector & 0xfffc)==0,
1010 "JMP:CS selector zero",
1011 EXCEPTION_GP,0)
1012 Bitu rpl=selector & 3;
1013 Descriptor desc;
1014 CPU_CHECK_COND(!cpu.gdt.GetDescriptor(selector,desc),
1015 "JMP:CS beyond limits",
1016 EXCEPTION_GP,selector & 0xfffc)
1017 switch (desc.Type()) {
1018 case DESC_CODE_N_NC_A: case DESC_CODE_N_NC_NA:
1019 case DESC_CODE_R_NC_A: case DESC_CODE_R_NC_NA:
1020 CPU_CHECK_COND(rpl>cpu.cpl,
1021 "JMP:NC:RPL>CPL",
1022 EXCEPTION_GP,selector & 0xfffc)
1023 CPU_CHECK_COND(cpu.cpl!=desc.DPL(),
1024 "JMP:NC:RPL != DPL",
1025 EXCEPTION_GP,selector & 0xfffc)
1026 LOG(LOG_CPU,LOG_NORMAL)("JMP:Code:NC to %X:%X big %d",selector,offset,desc.Big());
1027 goto CODE_jmp;
1028 case DESC_CODE_N_C_A: case DESC_CODE_N_C_NA:
1029 case DESC_CODE_R_C_A: case DESC_CODE_R_C_NA:
1030 LOG(LOG_CPU,LOG_NORMAL)("JMP:Code:C to %X:%X big %d",selector,offset,desc.Big());
1031 CPU_CHECK_COND(cpu.cpl<desc.DPL(),
1032 "JMP:C:CPL < DPL",
1033 EXCEPTION_GP,selector & 0xfffc)
1034 CODE_jmp:
1035 if (!desc.saved.seg.p) {
1036 // win
1037 CPU_Exception(EXCEPTION_NP,selector & 0xfffc);
1038 return;
1039 }
1040
1041 /* Normal jump to another selector:offset */
1042 Segs.phys[cs]=desc.GetBase();
1043 cpu.code.big=desc.Big()>0;
1044 Segs.val[cs]=(selector & 0xfffc) | cpu.cpl;
1045 reg_eip=offset;
1046 return;
1047 case DESC_386_TSS_A:
1048 CPU_CHECK_COND(desc.DPL()<cpu.cpl,
1049 "JMP:TSS:dpl<cpl",
1050 EXCEPTION_GP,selector & 0xfffc)
1051 CPU_CHECK_COND(desc.DPL()<rpl,
1052 "JMP:TSS:dpl<rpl",
1053 EXCEPTION_GP,selector & 0xfffc)
1054 LOG(LOG_CPU,LOG_NORMAL)("JMP:TSS to %X",selector);
1055 CPU_SwitchTask(selector,TSwitch_JMP,oldeip);
1056 break;
1057 default:
1058 E_Exit("JMP Illegal descriptor type %X",desc.Type());
1059 }
1060 }
1061 assert(1);
1062 }
1063
1064
1065 void CPU_CALL(bool use32,Bitu selector,Bitu offset,Bitu oldeip) {
1066 if (!cpu.pmode || (reg_flags & FLAG_VM)) {
1067 if (!use32) {
1068 CPU_Push16(SegValue(cs));
1069 CPU_Push16(oldeip);
1070 reg_eip=offset&0xffff;
1071 } else {
1072 CPU_Push32(SegValue(cs));
1073 CPU_Push32(oldeip);
1074 reg_eip=offset;
1075 }
1076 cpu.code.big=false;
1077 SegSet16(cs,selector);
1078 return;
1079 } else {
1080 CPU_CHECK_COND((selector & 0xfffc)==0,
1081 "CALL:CS selector zero",
1082 EXCEPTION_GP,0)
1083 Bitu rpl=selector & 3;
1084 Descriptor call;
1085 CPU_CHECK_COND(!cpu.gdt.GetDescriptor(selector,call),
1086 "CALL:CS beyond limits",
1087 EXCEPTION_GP,selector & 0xfffc)
1088 /* Check for type of far call */
1089 switch (call.Type()) {
1090 case DESC_CODE_N_NC_A:case DESC_CODE_N_NC_NA:
1091 case DESC_CODE_R_NC_A:case DESC_CODE_R_NC_NA:
1092 CPU_CHECK_COND(rpl>cpu.cpl,
1093 "CALL:CODE:NC:RPL>CPL",
1094 EXCEPTION_GP,selector & 0xfffc)
1095 CPU_CHECK_COND(call.DPL()!=cpu.cpl,
1096 "CALL:CODE:NC:DPL!=CPL",
1097 EXCEPTION_GP,selector & 0xfffc)
1098 LOG(LOG_CPU,LOG_NORMAL)("CALL:CODE:NC to %X:%X",selector,offset);
1099 goto call_code;
1100 case DESC_CODE_N_C_A:case DESC_CODE_N_C_NA:
1101 case DESC_CODE_R_C_A:case DESC_CODE_R_C_NA:
1102 CPU_CHECK_COND(call.DPL()>cpu.cpl,
1103 "CALL:CODE:C:DPL>CPL",
1104 EXCEPTION_GP,selector & 0xfffc)
1105 LOG(LOG_CPU,LOG_NORMAL)("CALL:CODE:C to %X:%X",selector,offset);
1106 call_code:
1107 if (!call.saved.seg.p) {
1108 // borland extender (RTM)
1109 CPU_Exception(EXCEPTION_NP,selector & 0xfffc);
1110 return;
1111 }
1112 // commit point
1113 if (!use32) {
1114 CPU_Push16(SegValue(cs));
1115 CPU_Push16(oldeip);
1116 reg_eip=offset & 0xffff;
1117 } else {
1118 CPU_Push32(SegValue(cs));
1119 CPU_Push32(oldeip);
1120 reg_eip=offset;
1121 }
1122 Segs.phys[cs]=call.GetBase();
1123 cpu.code.big=call.Big()>0;
1124 Segs.val[cs]=(selector & 0xfffc) | cpu.cpl;
1125 return;
1126 case DESC_386_CALL_GATE:
1127 case DESC_286_CALL_GATE:
1128 {
1129 CPU_CHECK_COND(call.DPL()<cpu.cpl,
1130 "CALL:Gate:Gate DPL<CPL",
1131 EXCEPTION_GP,selector & 0xfffc)
1132 CPU_CHECK_COND(call.DPL()<rpl,
1133 "CALL:Gate:Gate DPL<RPL",
1134 EXCEPTION_GP,selector & 0xfffc)
1135 CPU_CHECK_COND(!call.saved.seg.p,
1136 "CALL:Gate:Segment not present",
1137 EXCEPTION_NP,selector & 0xfffc)
1138 Descriptor n_cs_desc;
1139 Bitu n_cs_sel=call.GetSelector();
1140
1141 CPU_CHECK_COND((n_cs_sel & 0xfffc)==0,
1142 "CALL:Gate:CS selector zero",
1143 EXCEPTION_GP,0)
1144 CPU_CHECK_COND(!cpu.gdt.GetDescriptor(n_cs_sel,n_cs_desc),
1145 "CALL:Gate:CS beyond limits",
1146 EXCEPTION_GP,n_cs_sel & 0xfffc)
1147 Bitu n_cs_dpl = n_cs_desc.DPL();
1148 CPU_CHECK_COND(n_cs_dpl>cpu.cpl,
1149 "CALL:Gate:CS DPL>CPL",
1150 EXCEPTION_GP,n_cs_sel & 0xfffc)
1151
1152 CPU_CHECK_COND(!n_cs_desc.saved.seg.p,
1153 "CALL:Gate:CS not present",
1154 EXCEPTION_NP,n_cs_sel & 0xfffc)
1155
1156 Bitu n_eip = call.GetOffset();
1157 switch (n_cs_desc.Type()) {
1158 case DESC_CODE_N_NC_A:case DESC_CODE_N_NC_NA:
1159 case DESC_CODE_R_NC_A:case DESC_CODE_R_NC_NA:
1160 /* Check if we goto inner priviledge */
1161 if (n_cs_dpl < cpu.cpl) {
1162 /* Get new SS:ESP out of TSS */
1163 Bitu n_ss_sel,n_esp;
1164 Descriptor n_ss_desc;
1165 cpu_tss.Get_SSx_ESPx(n_cs_dpl,n_ss_sel,n_esp);
1166 CPU_CHECK_COND((n_ss_sel & 0xfffc)==0,
1167 "CALL:Gate:NC:SS selector zero",
1168 EXCEPTION_TS,0)
1169 CPU_CHECK_COND(!cpu.gdt.GetDescriptor(n_ss_sel,n_ss_desc),
1170 "CALL:Gate:Invalid SS selector",
1171 EXCEPTION_TS,n_ss_sel & 0xfffc)
1172 CPU_CHECK_COND(((n_ss_sel & 3)!=n_cs_desc.DPL()) || (n_ss_desc.DPL()!=n_cs_desc.DPL()),
1173 "CALL:Gate:Invalid SS selector privileges",
1174 EXCEPTION_TS,n_ss_sel & 0xfffc)
1175
1176 switch (n_ss_desc.Type()) {
1177 case DESC_DATA_EU_RW_NA: case DESC_DATA_EU_RW_A:
1178 case DESC_DATA_ED_RW_NA: case DESC_DATA_ED_RW_A:
1179 // writable data segment
1180 break;
1181 default:
1182 E_Exit("Call:Gate:SS no writable data segment"); // or #TS(ss_sel)
1183 }
1184 CPU_CHECK_COND(!n_ss_desc.saved.seg.p,
1185 "CALL:Gate:Stack segment not present",
1186 EXCEPTION_SS,n_ss_sel & 0xfffc)
1187
1188 /* Load the new SS:ESP and save data on it */
1189 Bitu o_esp = reg_esp;
1190 Bitu o_ss = SegValue(ss);
1191 PhysPt o_stack = SegPhys(ss)+(reg_esp & cpu.stack.mask);
1192
1193
1194 // catch pagefaults
1195 if (call.saved.gate.paramcount&31) {
1196 if (call.Type()==DESC_386_CALL_GATE) {
1197 for (Bits i=(call.saved.gate.paramcount&31)-1;i>=0;i--)
1198 mem_readd(o_stack+i*4);
1199 } else {
1200 for (Bits i=(call.saved.gate.paramcount&31)-1;i>=0;i--)
1201 mem_readw(o_stack+i*2);
1202 }
1203 }
1204
1205 // commit point
1206 Segs.val[ss]=n_ss_sel;
1207 Segs.phys[ss]=n_ss_desc.GetBase();
1208 if (n_ss_desc.Big()) {
1209 cpu.stack.big=true;
1210 cpu.stack.mask=0xffffffff;
1211 cpu.stack.notmask=0;
1212 reg_esp=n_esp;
1213 } else {
1214 cpu.stack.big=false;
1215 cpu.stack.mask=0xffff;
1216 cpu.stack.notmask=0xffff0000;
1217 reg_sp=n_esp & 0xffff;
1218 }
1219
1220 cpu.cpl = n_cs_desc.DPL();
1221 Bit16u oldcs = SegValue(cs);
1222 /* Switch to new CS:EIP */
1223 Segs.phys[cs] = n_cs_desc.GetBase();
1224 Segs.val[cs] = (n_cs_sel & 0xfffc) | cpu.cpl;
1225 cpu.code.big = n_cs_desc.Big()>0;
1226 reg_eip = n_eip;
1227 if (!use32) reg_eip&=0xffff;
1228
1229 if (call.Type()==DESC_386_CALL_GATE) {
1230 CPU_Push32(o_ss); //save old stack
1231 CPU_Push32(o_esp);
1232 if (call.saved.gate.paramcount&31)
1233 for (Bits i=(call.saved.gate.paramcount&31)-1;i>=0;i--)
1234 CPU_Push32(mem_readd(o_stack+i*4));
1235 CPU_Push32(oldcs);
1236 CPU_Push32(oldeip);
1237 } else {
1238 CPU_Push16(o_ss); //save old stack
1239 CPU_Push16(o_esp);
1240 if (call.saved.gate.paramcount&31)
1241 for (Bits i=(call.saved.gate.paramcount&31)-1;i>=0;i--)
1242 CPU_Push16(mem_readw(o_stack+i*2));
1243 CPU_Push16(oldcs);
1244 CPU_Push16(oldeip);
1245 }
1246
1247 break;
1248 } else if (n_cs_dpl > cpu.cpl)
1249 E_Exit("CALL:GATE:CS DPL>CPL"); // or #GP(sel)
1250 case DESC_CODE_N_C_A:case DESC_CODE_N_C_NA:
1251 case DESC_CODE_R_C_A:case DESC_CODE_R_C_NA:
1252 // zrdx extender
1253
1254 if (call.Type()==DESC_386_CALL_GATE) {
1255 CPU_Push32(SegValue(cs));
1256 CPU_Push32(oldeip);
1257 } else {
1258 CPU_Push16(SegValue(cs));
1259 CPU_Push16(oldeip);
1260 }
1261
1262 /* Switch to new CS:EIP */
1263 Segs.phys[cs] = n_cs_desc.GetBase();
1264 Segs.val[cs] = (n_cs_sel & 0xfffc) | cpu.cpl;
1265 cpu.code.big = n_cs_desc.Big()>0;
1266 reg_eip = n_eip;
1267 if (!use32) reg_eip&=0xffff;
1268 break;
1269 default:
1270 E_Exit("CALL:GATE:CS no executable segment");
1271 }
1272 } /* Call Gates */
1273 break;
1274 case DESC_386_TSS_A:
1275 CPU_CHECK_COND(call.DPL()<cpu.cpl,
1276 "CALL:TSS:dpl<cpl",
1277 EXCEPTION_GP,selector & 0xfffc)
1278 CPU_CHECK_COND(call.DPL()<rpl,
1279 "CALL:TSS:dpl<rpl",
1280 EXCEPTION_GP,selector & 0xfffc)
1281
1282 CPU_CHECK_COND(!call.saved.seg.p,
1283 "CALL:TSS:Segment not present",
1284 EXCEPTION_NP,selector & 0xfffc)
1285
1286 LOG(LOG_CPU,LOG_NORMAL)("CALL:TSS to %X",selector);
1287 CPU_SwitchTask(selector,TSwitch_CALL_INT,oldeip);
1288 break;
1289 case DESC_DATA_EU_RW_NA: // vbdos
1290 case DESC_INVALID: // used by some installers
1291 CPU_Exception(EXCEPTION_GP,selector & 0xfffc);
1292 return;
1293 default:
1294 E_Exit("CALL:Descriptor type %x unsupported",call.Type());
1295 }
1296 }
1297 assert(1);
1298 }
1299
1300
1301 void CPU_RET(bool use32,Bitu bytes,Bitu oldeip) {
1302 if (!cpu.pmode || (reg_flags & FLAG_VM)) {
1303 Bitu new_ip,new_cs;
1304 if (!use32) {
1305 new_ip=CPU_Pop16();
1306 new_cs=CPU_Pop16();
1307 } else {
1308 new_ip=CPU_Pop32();
1309 new_cs=CPU_Pop32() & 0xffff;
1310 }
1311 reg_esp+=bytes;
1312 SegSet16(cs,new_cs);
1313 reg_eip=new_ip;
1314 cpu.code.big=false;
1315 return;
1316 } else {
1317 Bitu offset,selector;
1318 if (!use32) selector = mem_readw(SegPhys(ss) + (reg_esp & cpu.stack.mask) + 2);
1319 else selector = mem_readd(SegPhys(ss) + (reg_esp & cpu.stack.mask) + 4) & 0xffff;
1320
1321 Descriptor desc;
1322 Bitu rpl=selector & 3;
1323 if(rpl < cpu.cpl) {
1324 // win setup
1325 CPU_Exception(EXCEPTION_GP,selector & 0xfffc);
1326 return;
1327 }
1328
1329 CPU_CHECK_COND((selector & 0xfffc)==0,
1330 "RET:CS selector zero",
1331 EXCEPTION_GP,0)
1332 CPU_CHECK_COND(!cpu.gdt.GetDescriptor(selector,desc),
1333 "RET:CS beyond limits",
1334 EXCEPTION_GP,selector & 0xfffc)
1335
1336 if (cpu.cpl==rpl) {
1337 /* Return to same level */
1338 switch (desc.Type()) {
1339 case DESC_CODE_N_NC_A:case DESC_CODE_N_NC_NA:
1340 case DESC_CODE_R_NC_A:case DESC_CODE_R_NC_NA:
1341 CPU_CHECK_COND(cpu.cpl!=desc.DPL(),
1342 "RET to NC segment of other privilege",
1343 EXCEPTION_GP,selector & 0xfffc)
1344 goto RET_same_level;
1345 case DESC_CODE_N_C_A:case DESC_CODE_N_C_NA:
1346 case DESC_CODE_R_C_A:case DESC_CODE_R_C_NA:
1347 CPU_CHECK_COND(desc.DPL()>cpu.cpl,
1348 "RET to C segment of higher privilege",
1349 EXCEPTION_GP,selector & 0xfffc)
1350 break;
1351 default:
1352 E_Exit("RET from illegal descriptor type %X",desc.Type());
1353 }
1354 RET_same_level:
1355 if (!desc.saved.seg.p) {
1356 // borland extender (RTM)
1357 CPU_Exception(EXCEPTION_NP,selector & 0xfffc);
1358 return;
1359 }
1360
1361 // commit point
1362 if (!use32) {
1363 offset=CPU_Pop16();
1364 selector=CPU_Pop16();
1365 } else {
1366 offset=CPU_Pop32();
1367 selector=CPU_Pop32() & 0xffff;
1368 }
1369
1370 Segs.phys[cs]=desc.GetBase();
1371 cpu.code.big=desc.Big()>0;
1372 Segs.val[cs]=selector;
1373 reg_eip=offset;
1374 if (cpu.stack.big) {
1375 reg_esp+=bytes;
1376 } else {
1377 reg_sp+=bytes;
1378 }
1379 LOG(LOG_CPU,LOG_NORMAL)("RET - Same level to %X:%X RPL %X DPL %X",selector,offset,rpl,desc.DPL());
1380 return;
1381 } else {
1382 /* Return to outer level */
1383 switch (desc.Type()) {
1384 case DESC_CODE_N_NC_A:case DESC_CODE_N_NC_NA:
1385 case DESC_CODE_R_NC_A:case DESC_CODE_R_NC_NA:
1386 CPU_CHECK_COND(desc.DPL()!=rpl,
1387 "RET to outer NC segment with DPL!=RPL",
1388 EXCEPTION_GP,selector & 0xfffc)
1389 break;
1390 case DESC_CODE_N_C_A:case DESC_CODE_N_C_NA:
1391 case DESC_CODE_R_C_A:case DESC_CODE_R_C_NA:
1392 CPU_CHECK_COND(desc.DPL()>rpl,
1393 "RET to outer C segment with DPL>RPL",
1394 EXCEPTION_GP,selector & 0xfffc)
1395 break;
1396 default:
1397 E_Exit("RET from illegal descriptor type %X",desc.Type()); // or #GP(selector)
1398 }
1399
1400 CPU_CHECK_COND(!desc.saved.seg.p,
1401 "RET:Outer level:CS not present",
1402 EXCEPTION_NP,selector & 0xfffc)
1403
1404 // commit point
1405 Bitu n_esp,n_ss;
1406 if (use32) {
1407 offset=CPU_Pop32();
1408 selector=CPU_Pop32() & 0xffff;
1409 reg_esp+=bytes;
1410 n_esp = CPU_Pop32();
1411 n_ss = CPU_Pop32() & 0xffff;
1412 } else {
1413 offset=CPU_Pop16();
1414 selector=CPU_Pop16();
1415 reg_esp+=bytes;
1416 n_esp = CPU_Pop16();
1417 n_ss = CPU_Pop16();
1418 }
1419
1420 CPU_CHECK_COND((n_ss & 0xfffc)==0,
1421 "RET to outer level with SS selector zero",
1422 EXCEPTION_GP,0)
1423
1424 Descriptor n_ss_desc;
1425 CPU_CHECK_COND(!cpu.gdt.GetDescriptor(n_ss,n_ss_desc),
1426 "RET:SS beyond limits",
1427 EXCEPTION_GP,n_ss & 0xfffc)
1428
1429 CPU_CHECK_COND(((n_ss & 3)!=rpl) || (n_ss_desc.DPL()!=rpl),
1430 "RET to outer segment with invalid SS privileges",
1431 EXCEPTION_GP,n_ss & 0xfffc)
1432 switch (n_ss_desc.Type()) {
1433 case DESC_DATA_EU_RW_NA: case DESC_DATA_EU_RW_A:
1434 case DESC_DATA_ED_RW_NA: case DESC_DATA_ED_RW_A:
1435 break;
1436 default:
1437 E_Exit("RET:SS selector type no writable data segment"); // or #GP(selector)
1438 }
1439 CPU_CHECK_COND(!n_ss_desc.saved.seg.p,
1440 "RET:Stack segment not present",
1441 EXCEPTION_SS,n_ss & 0xfffc)
1442
1443 cpu.cpl = rpl;
1444 Segs.phys[cs]=desc.GetBase();
1445 cpu.code.big=desc.Big()>0;
1446 Segs.val[cs]=(selector&0xfffc) | cpu.cpl;
1447 reg_eip=offset;
1448
1449 Segs.val[ss]=n_ss;
1450 Segs.phys[ss]=n_ss_desc.GetBase();
1451 if (n_ss_desc.Big()) {
1452 cpu.stack.big=true;
1453 cpu.stack.mask=0xffffffff;
1454 cpu.stack.notmask=0;
1455 reg_esp=n_esp+bytes;
1456 } else {
1457 cpu.stack.big=false;
1458 cpu.stack.mask=0xffff;
1459 cpu.stack.notmask=0xffff0000;
1460 reg_sp=(n_esp & 0xffff)+bytes;
1461 }
1462
1463 CPU_CheckSegments();
1464
1465 // LOG(LOG_MISC,LOG_ERROR)("RET - Higher level to %X:%X RPL %X DPL %X",selector,offset,rpl,desc.DPL());
1466 return;
1467 }
1468 LOG(LOG_CPU,LOG_NORMAL)("Prot ret %X:%X",selector,offset);
1469 return;
1470 }
1471 assert(1);
1472 }
1473
1474
1475 Bitu CPU_SLDT(void) {
1476 return cpu.gdt.SLDT();
1477 }
1478
1479 bool CPU_LLDT(Bitu selector) {
1480 if (!cpu.gdt.LLDT(selector)) {
1481 LOG(LOG_CPU,LOG_ERROR)("LLDT failed, selector=%X",selector);
1482 return true;
1483 }
1484 LOG(LOG_CPU,LOG_NORMAL)("LDT Set to %X",selector);
1485 return false;
1486 }
1487
1488 Bitu CPU_STR(void) {
1489 return cpu_tss.selector;
1490 }
1491
1492 bool CPU_LTR(Bitu selector) {
1493 if ((selector & 0xfffc)==0) {
1494 cpu_tss.SetSelector(selector);
1495 return false;
1496 }
1497 TSS_Descriptor desc;
1498 if ((selector & 4) || (!cpu.gdt.GetDescriptor(selector,desc))) {
1499 LOG(LOG_CPU,LOG_ERROR)("LTR failed, selector=%X",selector);
1500 return CPU_PrepareException(EXCEPTION_GP,selector);
1501 }
1502
1503 if ((desc.Type()==DESC_286_TSS_A) || (desc.Type()==DESC_386_TSS_A)) {
1504 if (!desc.saved.seg.p) {
1505 LOG(LOG_CPU,LOG_ERROR)("LTR failed, selector=%X (not present)",selector);
1506 return CPU_PrepareException(EXCEPTION_NP,selector);
1507 }
1508 if (!cpu_tss.SetSelector(selector)) E_Exit("LTR failed, selector=%X",selector);
1509 cpu_tss.desc.SetBusy(true);
1510 cpu_tss.SaveSelector();
1511 } else {
1512 /* Descriptor was no available TSS descriptor */
1513 LOG(LOG_CPU,LOG_NORMAL)("LTR failed, selector=%X (type=%X)",selector,desc.Type());
1514 return CPU_PrepareException(EXCEPTION_GP,selector);
1515 }
1516 return false;
1517 }
1518
1519 void CPU_LGDT(Bitu limit,Bitu base) {
1520 LOG(LOG_CPU,LOG_NORMAL)("GDT Set to base:%X limit:%X",base,limit);
1521 cpu.gdt.SetLimit(limit);
1522 cpu.gdt.SetBase(base);
1523 }
1524
1525 void CPU_LIDT(Bitu limit,Bitu base) {
1526 LOG(LOG_CPU,LOG_NORMAL)("IDT Set to base:%X limit:%X",base,limit);
1527 cpu.idt.SetLimit(limit);
1528 cpu.idt.SetBase(base);
1529 }
1530
1531 Bitu CPU_SGDT_base(void) {
1532 return cpu.gdt.GetBase();
1533 }
1534 Bitu CPU_SGDT_limit(void) {
1535 return cpu.gdt.GetLimit();
1536 }
1537
1538 Bitu CPU_SIDT_base(void) {
1539 return cpu.idt.GetBase();
1540 }
1541 Bitu CPU_SIDT_limit(void) {
1542 return cpu.idt.GetLimit();
1543 }
1544
1545 static bool printed_cycles_auto_info = false;
1546 void CPU_SET_CRX(Bitu cr,Bitu value) {
1547 switch (cr) {
1548 case 0:
1549 {
1550 Bitu changed=cpu.cr0 ^ value;
1551 if (!changed) return;
1552 cpu.cr0=value;
1553 if (value & CR0_PROTECTION) {
1554 cpu.pmode=true;
1555 LOG(LOG_CPU,LOG_NORMAL)("Protected mode");
1556 PAGING_Enable((value & CR0_PAGING)>0);
1557
1558 if (!(CPU_AutoDetermineMode&CPU_AUTODETERMINE_MASK)) break;
1559
1560 if (CPU_AutoDetermineMode&CPU_AUTODETERMINE_CYCLES) {
1561 CPU_CycleAutoAdjust=true;
1562 CPU_CycleLeft=0;
1563 CPU_Cycles=0;
1564 CPU_OldCycleMax=CPU_CycleMax;
1565 GFX_SetTitle(CPU_CyclePercUsed,-1,false);
1566 if(!printed_cycles_auto_info) {
1567 printed_cycles_auto_info = true;
1568 LOG_MSG("DOSBox switched to max cycles, because of the setting: cycles=auto. If the game runs too fast try a fixed cycles amount in DOSBox's options.");
1569 }
1570 } else {
1571 GFX_SetTitle(-1,-1,false);
1572 }
1573 #if (C_DYNAMIC_X86)
1574 if (CPU_AutoDetermineMode&CPU_AUTODETERMINE_CORE) {
1575 CPU_Core_Dyn_X86_Cache_Init(true);
1576 cpudecoder=&CPU_Core_Dyn_X86_Run;
1577 }
1578 #elif (C_DYNREC)
1579 if (CPU_AutoDetermineMode&CPU_AUTODETERMINE_CORE) {
1580 CPU_Core_Dynrec_Cache_Init(true);
1581 cpudecoder=&CPU_Core_Dynrec_Run;
1582 }
1583 #endif
1584 CPU_AutoDetermineMode<<=CPU_AUTODETERMINE_SHIFT;
1585 } else {
1586 cpu.pmode=false;
1587 if (value & CR0_PAGING) LOG_MSG("Paging requested without PE=1");
1588 PAGING_Enable(false);
1589 LOG(LOG_CPU,LOG_NORMAL)("Real mode");
1590 }
1591 break;
1592 }
1593 case 2:
1594 paging.cr2=value;
1595 break;
1596 case 3:
1597 PAGING_SetDirBase(value);
1598 break;
1599 default:
1600 LOG(LOG_CPU,LOG_ERROR)("Unhandled MOV CR%d,%X",cr,value);
1601 break;
1602 }
1603 }
1604
1605 bool CPU_WRITE_CRX(Bitu cr,Bitu value) {
1606 /* Check if privileged to access control registers */
1607 if (cpu.pmode && (cpu.cpl>0)) return CPU_PrepareException(EXCEPTION_GP,0);
1608 if ((cr==1) || (cr>4)) return CPU_PrepareException(EXCEPTION_UD,0);
1609 if (CPU_ArchitectureType<CPU_ARCHTYPE_486OLDSLOW) {
1610 if (cr==4) return CPU_PrepareException(EXCEPTION_UD,0);
1611 }
1612 CPU_SET_CRX(cr,value);
1613 return false;
1614 }
1615
1616 Bitu CPU_GET_CRX(Bitu cr) {
1617 switch (cr) {
1618 case 0:
1619 if (CPU_ArchitectureType>=CPU_ARCHTYPE_PENTIUMSLOW) return cpu.cr0;
1620 else if (CPU_ArchitectureType>=CPU_ARCHTYPE_486OLDSLOW) return (cpu.cr0 & 0xe005003f);
1621 else return (cpu.cr0 | 0x7ffffff0);
1622 case 2:
1623 return paging.cr2;
1624 case 3:
1625 return PAGING_GetDirBase() & 0xfffff000;
1626 default:
1627 LOG(LOG_CPU,LOG_ERROR)("Unhandled MOV XXX, CR%d",cr);
1628 break;
1629 }
1630 return 0;
1631 }
1632
1633 bool CPU_READ_CRX(Bitu cr,Bit32u & retvalue) {
1634 /* Check if privileged to access control registers */
1635 if (cpu.pmode && (cpu.cpl>0)) return CPU_PrepareException(EXCEPTION_GP,0);
1636 if ((cr==1) || (cr>4)) return CPU_PrepareException(EXCEPTION_UD,0);
1637 retvalue=CPU_GET_CRX(cr);
1638 return false;
1639 }
1640
1641
1642 bool CPU_WRITE_DRX(Bitu dr,Bitu value) {
1643 /* Check if privileged to access control registers */
1644 if (cpu.pmode && (cpu.cpl>0)) return CPU_PrepareException(EXCEPTION_GP,0);
1645 switch (dr) {
1646 case 0:
1647 case 1:
1648 case 2:
1649 case 3:
1650 cpu.drx[dr]=value;
1651 break;
1652 case 4:
1653 case 6:
1654 cpu.drx[6]=(value|0xffff0ff0) & 0xffffefff;
1655 break;
1656 case 5:
1657 case 7:
1658 if (CPU_ArchitectureType<CPU_ARCHTYPE_PENTIUMSLOW) {
1659 cpu.drx[7]=(value|0x400) & 0xffff2fff;
1660 } else {
1661 cpu.drx[7]=(value|0x400);
1662 }
1663 break;
1664 default:
1665 LOG(LOG_CPU,LOG_ERROR)("Unhandled MOV DR%d,%X",dr,value);
1666 break;
1667 }
1668 return false;
1669 }
1670
1671 bool CPU_READ_DRX(Bitu dr,Bit32u & retvalue) {
1672 /* Check if privileged to access control registers */
1673 if (cpu.pmode && (cpu.cpl>0)) return CPU_PrepareException(EXCEPTION_GP,0);
1674 switch (dr) {
1675 case 0:
1676 case 1:
1677 case 2:
1678 case 3:
1679 case 6:
1680 case 7:
1681 retvalue=cpu.drx[dr];
1682 break;
1683 case 4:
1684 retvalue=cpu.drx[6];
1685 break;
1686 case 5:
1687 retvalue=cpu.drx[7];
1688 break;
1689 default:
1690 LOG(LOG_CPU,LOG_ERROR)("Unhandled MOV XXX, DR%d",dr);
1691 retvalue=0;
1692 break;
1693 }
1694 return false;
1695 }
1696
1697 bool CPU_WRITE_TRX(Bitu tr,Bitu value) {
1698 /* Check if privileged to access control registers */
1699 if (cpu.pmode && (cpu.cpl>0)) return CPU_PrepareException(EXCEPTION_GP,0);
1700 switch (tr) {
1701 // case 3:
1702 case 6:
1703 case 7:
1704 cpu.trx[tr]=value;
1705 return false;
1706 default:
1707 LOG(LOG_CPU,LOG_ERROR)("Unhandled MOV TR%d,%X",tr,value);
1708 break;
1709 }
1710 return CPU_PrepareException(EXCEPTION_UD,0);
1711 }
1712
1713 bool CPU_READ_TRX(Bitu tr,Bit32u & retvalue) {
1714 /* Check if privileged to access control registers */
1715 if (cpu.pmode && (cpu.cpl>0)) return CPU_PrepareException(EXCEPTION_GP,0);
1716 switch (tr) {
1717 // case 3:
1718 case 6:
1719 case 7:
1720 retvalue=cpu.trx[tr];
1721 return false;
1722 default:
1723 LOG(LOG_CPU,LOG_ERROR)("Unhandled MOV XXX, TR%d",tr);
1724 break;
1725 }
1726 return CPU_PrepareException(EXCEPTION_UD,0);
1727 }
1728
1729
1730 Bitu CPU_SMSW(void) {
1731 return cpu.cr0;
1732 }
1733
1734 bool CPU_LMSW(Bitu word) {
1735 if (cpu.pmode && (cpu.cpl>0)) return CPU_PrepareException(EXCEPTION_GP,0);
1736 word&=0xf;
1737 if (cpu.cr0 & 1) word|=1;
1738 word|=(cpu.cr0&0xfffffff0);
1739 CPU_SET_CRX(0,word);
1740 return false;
1741 }
1742
1743 void CPU_ARPL(Bitu & dest_sel,Bitu src_sel) {
1744 FillFlags();
1745 if ((dest_sel & 3) < (src_sel & 3)) {
1746 dest_sel=(dest_sel & 0xfffc) + (src_sel & 3);
1747 // dest_sel|=0xff3f0000;
1748 SETFLAGBIT(ZF,true);
1749 } else {
1750 SETFLAGBIT(ZF,false);
1751 }
1752 }
1753
1754 void CPU_LAR(Bitu selector,Bitu & ar) {
1755 FillFlags();
1756 if (selector == 0) {
1757 SETFLAGBIT(ZF,false);
1758 return;
1759 }
1760 Descriptor desc;Bitu rpl=selector & 3;
1761 if (!cpu.gdt.GetDescriptor(selector,desc)){
1762 SETFLAGBIT(ZF,false);
1763 return;
1764 }
1765 switch (desc.Type()){
1766 case DESC_CODE_N_C_A: case DESC_CODE_N_C_NA:
1767 case DESC_CODE_R_C_A: case DESC_CODE_R_C_NA:
1768 break;
1769
1770 case DESC_286_INT_GATE: case DESC_286_TRAP_GATE: {
1771 case DESC_386_INT_GATE: case DESC_386_TRAP_GATE:
1772 SETFLAGBIT(ZF,false);
1773 return;
1774 }
1775
1776 case DESC_LDT:
1777 case DESC_TASK_GATE:
1778
1779 case DESC_286_TSS_A: case DESC_286_TSS_B:
1780 case DESC_286_CALL_GATE:
1781
1782 case DESC_386_TSS_A: case DESC_386_TSS_B:
1783 case DESC_386_CALL_GATE:
1784
1785
1786 case DESC_DATA_EU_RO_NA: case DESC_DATA_EU_RO_A:
1787 case DESC_DATA_EU_RW_NA: case DESC_DATA_EU_RW_A:
1788 case DESC_DATA_ED_RO_NA: case DESC_DATA_ED_RO_A:
1789 case DESC_DATA_ED_RW_NA: case DESC_DATA_ED_RW_A:
1790 case DESC_CODE_N_NC_A: case DESC_CODE_N_NC_NA:
1791 case DESC_CODE_R_NC_A: case DESC_CODE_R_NC_NA:
1792 if (desc.DPL()<cpu.cpl || desc.DPL() < rpl) {
1793 SETFLAGBIT(ZF,false);
1794 return;
1795 }
1796 break;
1797 default:
1798 SETFLAGBIT(ZF,false);
1799 return;
1800 }
1801 /* Valid descriptor */
1802 ar=desc.saved.fill[1] & 0x00ffff00;
1803 SETFLAGBIT(ZF,true);
1804 }
1805
1806 void CPU_LSL(Bitu selector,Bitu & limit) {
1807 FillFlags();
1808 if (selector == 0) {
1809 SETFLAGBIT(ZF,false);
1810 return;
1811 }
1812 Descriptor desc;Bitu rpl=selector & 3;
1813 if (!cpu.gdt.GetDescriptor(selector,desc)){
1814 SETFLAGBIT(ZF,false);
1815 return;
1816 }
1817 switch (desc.Type()){
1818 case DESC_CODE_N_C_A: case DESC_CODE_N_C_NA:
1819 case DESC_CODE_R_C_A: case DESC_CODE_R_C_NA:
1820 break;
1821
1822 case DESC_LDT:
1823 case DESC_286_TSS_A:
1824 case DESC_286_TSS_B:
1825
1826 case DESC_386_TSS_A:
1827 case DESC_386_TSS_B:
1828
1829 case DESC_DATA_EU_RO_NA: case DESC_DATA_EU_RO_A:
1830 case DESC_DATA_EU_RW_NA: case DESC_DATA_EU_RW_A:
1831 case DESC_DATA_ED_RO_NA: case DESC_DATA_ED_RO_A:
1832 case DESC_DATA_ED_RW_NA: case DESC_DATA_ED_RW_A:
1833
1834 case DESC_CODE_N_NC_A: case DESC_CODE_N_NC_NA:
1835 case DESC_CODE_R_NC_A: case DESC_CODE_R_NC_NA:
1836 if (desc.DPL()<cpu.cpl || desc.DPL() < rpl) {
1837 SETFLAGBIT(ZF,false);
1838 return;
1839 }
1840 break;
1841 default:
1842 SETFLAGBIT(ZF,false);
1843 return;
1844 }
1845 limit=desc.GetLimit();
1846 SETFLAGBIT(ZF,true);
1847 }
1848
1849 void CPU_VERR(Bitu selector) {
1850 FillFlags();
1851 if (selector == 0) {
1852 SETFLAGBIT(ZF,false);
1853 return;
1854 }
1855 Descriptor desc;Bitu rpl=selector & 3;
1856 if (!cpu.gdt.GetDescriptor(selector,desc)){
1857 SETFLAGBIT(ZF,false);
1858 return;
1859 }
1860 switch (desc.Type()){
1861 case DESC_CODE_R_C_A: case DESC_CODE_R_C_NA:
1862 //Conforming readable code segments can be always read
1863 break;
1864 case DESC_DATA_EU_RO_NA: case DESC_DATA_EU_RO_A:
1865 case DESC_DATA_EU_RW_NA: case DESC_DATA_EU_RW_A:
1866 case DESC_DATA_ED_RO_NA: case DESC_DATA_ED_RO_A:
1867 case DESC_DATA_ED_RW_NA: case DESC_DATA_ED_RW_A:
1868
1869 case DESC_CODE_R_NC_A: case DESC_CODE_R_NC_NA:
1870 if (desc.DPL()<cpu.cpl || desc.DPL() < rpl) {
1871 SETFLAGBIT(ZF,false);
1872 return;
1873 }
1874 break;
1875 default:
1876 SETFLAGBIT(ZF,false);
1877 return;
1878 }
1879 SETFLAGBIT(ZF,true);
1880 }
1881
1882 void CPU_VERW(Bitu selector) {
1883 FillFlags();
1884 if (selector == 0) {
1885 SETFLAGBIT(ZF,false);
1886 return;
1887 }
1888 Descriptor desc;Bitu rpl=selector & 3;
1889 if (!cpu.gdt.GetDescriptor(selector,desc)){
1890 SETFLAGBIT(ZF,false);
1891 return;
1892 }
1893 switch (desc.Type()){
1894 case DESC_DATA_EU_RW_NA: case DESC_DATA_EU_RW_A:
1895 case DESC_DATA_ED_RW_NA: case DESC_DATA_ED_RW_A:
1896 if (desc.DPL()<cpu.cpl || desc.DPL() < rpl) {
1897 SETFLAGBIT(ZF,false);
1898 return;
1899 }
1900 break;
1901 default:
1902 SETFLAGBIT(ZF,false);
1903 return;
1904 }
1905 SETFLAGBIT(ZF,true);
1906 }
1907
1908 bool CPU_SetSegGeneral(SegNames seg,Bitu value) {
1909 value &= 0xffff;
1910 if (!cpu.pmode || (reg_flags & FLAG_VM)) {
1911 Segs.val[seg]=value;
1912 Segs.phys[seg]=value << 4;
1913 if (seg==ss) {
1914 cpu.stack.big=false;
1915 cpu.stack.mask=0xffff;
1916 cpu.stack.notmask=0xffff0000;
1917 }
1918 return false;
1919 } else {
1920 if (seg==ss) {
1921 // Stack needs to be non-zero
1922 if ((value & 0xfffc)==0) {
1923 E_Exit("CPU_SetSegGeneral: Stack segment zero");
1924 // return CPU_PrepareException(EXCEPTION_GP,0);
1925 }
1926 Descriptor desc;
1927 if (!cpu.gdt.GetDescriptor(value,desc)) {
1928 E_Exit("CPU_SetSegGeneral: Stack segment beyond limits");
1929 // return CPU_PrepareException(EXCEPTION_GP,value & 0xfffc);
1930 }
1931 if (((value & 3)!=cpu.cpl) || (desc.DPL()!=cpu.cpl)) {
1932 E_Exit("CPU_SetSegGeneral: Stack segment with invalid privileges");
1933 // return CPU_PrepareException(EXCEPTION_GP,value & 0xfffc);
1934 }
1935
1936 switch (desc.Type()) {
1937 case DESC_DATA_EU_RW_NA: case DESC_DATA_EU_RW_A:
1938 case DESC_DATA_ED_RW_NA: case DESC_DATA_ED_RW_A:
1939 break;
1940 default:
1941 //Earth Siege 1
1942 return CPU_PrepareException(EXCEPTION_GP,value & 0xfffc);
1943 }
1944
1945 if (!desc.saved.seg.p) {
1946 // E_Exit("CPU_SetSegGeneral: Stack segment not present"); // or #SS(sel)
1947 return CPU_PrepareException(EXCEPTION_SS,value & 0xfffc);
1948 }
1949
1950 Segs.val[seg]=value;
1951 Segs.phys[seg]=desc.GetBase();
1952 if (desc.Big()) {
1953 cpu.stack.big=true;
1954 cpu.stack.mask=0xffffffff;
1955 cpu.stack.notmask=0;
1956 } else {
1957 cpu.stack.big=false;
1958 cpu.stack.mask=0xffff;
1959 cpu.stack.notmask=0xffff0000;
1960 }
1961 } else {
1962 if ((value & 0xfffc)==0) {
1963 Segs.val[seg]=value;
1964 Segs.phys[seg]=0; // ??
1965 return false;
1966 }
1967 Descriptor desc;
1968 if (!cpu.gdt.GetDescriptor(value,desc)) {
1969 return CPU_PrepareException(EXCEPTION_GP,value & 0xfffc);
1970 }
1971 switch (desc.Type()) {
1972 case DESC_DATA_EU_RO_NA: case DESC_DATA_EU_RO_A:
1973 case DESC_DATA_EU_RW_NA: case DESC_DATA_EU_RW_A:
1974 case DESC_DATA_ED_RO_NA: case DESC_DATA_ED_RO_A:
1975 case DESC_DATA_ED_RW_NA: case DESC_DATA_ED_RW_A:
1976 case DESC_CODE_R_NC_A: case DESC_CODE_R_NC_NA:
1977 if (((value & 3)>desc.DPL()) || (cpu.cpl>desc.DPL())) {
1978 // extreme pinball
1979 return CPU_PrepareException(EXCEPTION_GP,value & 0xfffc);
1980 }
1981 break;
1982 case DESC_CODE_R_C_A: case DESC_CODE_R_C_NA:
1983 break;
1984 default:
1985 // gabriel knight
1986 return CPU_PrepareException(EXCEPTION_GP,value & 0xfffc);
1987
1988 }
1989 if (!desc.saved.seg.p) {
1990 // win
1991 return CPU_PrepareException(EXCEPTION_NP,value & 0xfffc);
1992 }
1993
1994 Segs.val[seg]=value;
1995 Segs.phys[seg]=desc.GetBase();
1996 }
1997
1998 return false;
1999 }
2000 }
2001
2002 bool CPU_PopSeg(SegNames seg,bool use32) {
2003 Bitu val=mem_readw(SegPhys(ss) + (reg_esp & cpu.stack.mask));
2004 if (CPU_SetSegGeneral(seg,val)) return true;
2005 Bitu addsp=use32?0x04:0x02;
2006 reg_esp=(reg_esp&cpu.stack.notmask)|((reg_esp+addsp)&cpu.stack.mask);
2007 return false;
2008 }
2009
2010 bool CPU_CPUID(void) {
2011 if (CPU_ArchitectureType<CPU_ARCHTYPE_486NEWSLOW) return false;
2012 switch (reg_eax) {
2013 case 0: /* Vendor ID String and maximum level? */
2014 reg_eax=1; /* Maximum level */
2015 reg_ebx='G' | ('e' << 8) | ('n' << 16) | ('u'<< 24);
2016 reg_edx='i' | ('n' << 8) | ('e' << 16) | ('I'<< 24);
2017 reg_ecx='n' | ('t' << 8) | ('e' << 16) | ('l'<< 24);
2018 break;
2019 case 1: /* get processor type/family/model/stepping and feature flags */
2020 if ((CPU_ArchitectureType==CPU_ARCHTYPE_486NEWSLOW) ||
2021 (CPU_ArchitectureType==CPU_ARCHTYPE_MIXED)) {
2022 reg_eax=0x402; /* intel 486dx */
2023 reg_ebx=0; /* Not Supported */
2024 reg_ecx=0; /* No features */
2025 reg_edx=0x00000001; /* FPU */
2026 } else if (CPU_ArchitectureType==CPU_ARCHTYPE_PENTIUMSLOW) {
2027 reg_eax=0x513; /* intel pentium */
2028 reg_ebx=0; /* Not Supported */
2029 reg_ecx=0; /* No features */
2030 reg_edx=0x00000011; /* FPU+TimeStamp/RDTSC */
2031 } else {
2032 return false;
2033 }
2034 break;
2035 default:
2036 LOG(LOG_CPU,LOG_ERROR)("Unhandled CPUID Function %x",reg_eax);
2037 reg_eax=0;
2038 reg_ebx=0;
2039 reg_ecx=0;
2040 reg_edx=0;
2041 break;
2042 }
2043 return true;
2044 }
2045
2046 static Bits HLT_Decode(void) {
2047 /* Once an interrupt occurs, it should change cpu core */
2048 if (reg_eip!=cpu.hlt.eip || SegValue(cs) != cpu.hlt.cs) {
2049 cpudecoder=cpu.hlt.old_decoder;
2050 } else {
2051 CPU_Cycles=0;
2052 }
2053 return 0;
2054 }
2055
2056 void CPU_HLT(Bitu oldeip) {
2057 reg_eip=oldeip;
2058 CPU_Cycles=0;
2059 cpu.hlt.cs=SegValue(cs);
2060 cpu.hlt.eip=reg_eip;
2061 cpu.hlt.old_decoder=cpudecoder;
2062 cpudecoder=&HLT_Decode;
2063 }
2064
2065 void CPU_ENTER(bool use32,Bitu bytes,Bitu level) {
2066 level&=0x1f;
2067 Bitu sp_index=reg_esp&cpu.stack.mask;
2068 Bitu bp_index=reg_ebp&cpu.stack.mask;
2069 if (!use32) {
2070 sp_index-=2;
2071 mem_writew(SegPhys(ss)+sp_index,reg_bp);
2072 reg_bp=(Bit16u)(reg_esp-2);
2073 if (level) {
2074 for (Bitu i=1;i<level;i++) {
2075 sp_index-=2;bp_index-=2;
2076 mem_writew(SegPhys(ss)+sp_index,mem_readw(SegPhys(ss)+bp_index));
2077 }
2078 sp_index-=2;
2079 mem_writew(SegPhys(ss)+sp_index,reg_bp);
2080 }
2081 } else {
2082 sp_index-=4;
2083 mem_writed(SegPhys(ss)+sp_index,reg_ebp);
2084 reg_ebp=(reg_esp-4);
2085 if (level) {
2086 for (Bitu i=1;i<level;i++) {
2087 sp_index-=4;bp_index-=4;
2088 mem_writed(SegPhys(ss)+sp_index,mem_readd(SegPhys(ss)+bp_index));
2089 }
2090 sp_index-=4;
2091 mem_writed(SegPhys(ss)+sp_index,reg_ebp);
2092 }
2093 }
2094 sp_index-=bytes;
2095 reg_esp=(reg_esp&cpu.stack.notmask)|((sp_index)&cpu.stack.mask);
2096 }
2097
2098 static void CPU_CycleIncrease(bool pressed) {
2099 if (!pressed) return;
2100 if (CPU_CycleAutoAdjust) {
2101 CPU_CyclePercUsed+=5;
2102 if (CPU_CyclePercUsed>105) CPU_CyclePercUsed=105;
2103 LOG_MSG("CPU speed: max %d percent.",CPU_CyclePercUsed);
2104 GFX_SetTitle(CPU_CyclePercUsed,-1,false);
2105 } else {
2106 Bit32s old_cycles=CPU_CycleMax;
2107 if (CPU_CycleUp < 100) {
2108 CPU_CycleMax = (Bit32s)(CPU_CycleMax * (1 + (float)CPU_CycleUp / 100.0));
2109 } else {
2110 CPU_CycleMax = (Bit32s)(CPU_CycleMax + CPU_CycleUp);
2111 }
2112
2113 CPU_CycleLeft=0;CPU_Cycles=0;
2114 if (CPU_CycleMax==old_cycles) CPU_CycleMax++;
2115 if(CPU_CycleMax > 15000 )
2116 LOG_MSG("CPU speed: fixed %d cycles. If you need more than 20000, try core=dynamic in DOSBox's options.",CPU_CycleMax);
2117 else
2118 LOG_MSG("CPU speed: fixed %d cycles.",CPU_CycleMax);
2119 GFX_SetTitle(CPU_CycleMax,-1,false);
2120 }
2121 }
2122
2123 static void CPU_CycleDecrease(bool pressed) {
2124 if (!pressed) return;
2125 if (CPU_CycleAutoAdjust) {
2126 CPU_CyclePercUsed-=5;
2127 if (CPU_CyclePercUsed<=0) CPU_CyclePercUsed=1;
2128 if(CPU_CyclePercUsed <=70)
2129 LOG_MSG("CPU speed: max %d percent. If the game runs too fast, try a fixed cycles amount in DOSBox's options.",CPU_CyclePercUsed);
2130 else
2131 LOG_MSG("CPU speed: max %d percent.",CPU_CyclePercUsed);
2132 GFX_SetTitle(CPU_CyclePercUsed,-1,false);
2133 } else {
2134 if (CPU_CycleDown < 100) {
2135 CPU_CycleMax = (Bit32s)(CPU_CycleMax / (1 + (float)CPU_CycleDown / 100.0));
2136 } else {
2137 CPU_CycleMax = (Bit32s)(CPU_CycleMax - CPU_CycleDown);
2138 }
2139 CPU_CycleLeft=0;CPU_Cycles=0;
2140 if (CPU_CycleMax <= 0) CPU_CycleMax=1;
2141 LOG_MSG("CPU speed: fixed %d cycles.",CPU_CycleMax);
2142 GFX_SetTitle(CPU_CycleMax,-1,false);
2143 }
2144 }
2145
2146 void CPU_Enable_SkipAutoAdjust(void) {
2147 if (CPU_CycleAutoAdjust) {
2148 CPU_CycleMax /= 2;
2149 if (CPU_CycleMax < CPU_CYCLES_LOWER_LIMIT)
2150 CPU_CycleMax = CPU_CYCLES_LOWER_LIMIT;
2151 }
2152 CPU_SkipCycleAutoAdjust=true;
2153 }
2154
2155 void CPU_Disable_SkipAutoAdjust(void) {
2156 CPU_SkipCycleAutoAdjust=false;
2157 }
2158
2159
2160 extern Bit32s ticksDone;
2161 extern Bit32u ticksScheduled;
2162
2163 void CPU_Reset_AutoAdjust(void) {
2164 CPU_IODelayRemoved = 0;
2165 ticksDone = 0;
2166 ticksScheduled = 0;
2167 }
2168
2169 class CPU: public Module_base {
2170 private:
2171 static bool inited;
2172 public:
2173 CPU(Section* configuration):Module_base(configuration) {
2174 if(inited) {
2175 Change_Config(configuration);
2176 return;
2177 }
2178 // Section_prop * section=static_cast<Section_prop *>(configuration);
2179 inited=true;
2180 reg_eax=0;
2181 reg_ebx=0;
2182 reg_ecx=0;
2183 reg_edx=0;
2184 reg_edi=0;
2185 reg_esi=0;
2186 reg_ebp=0;
2187 reg_esp=0;
2188
2189 SegSet16(cs,0);
2190 SegSet16(ds,0);
2191 SegSet16(es,0);
2192 SegSet16(fs,0);
2193 SegSet16(gs,0);
2194 SegSet16(ss,0);
2195
2196 CPU_SetFlags(FLAG_IF,FMASK_ALL); //Enable interrupts
2197 cpu.cr0=0xffffffff;
2198 CPU_SET_CRX(0,0); //Initialize
2199 cpu.code.big=false;
2200 cpu.stack.mask=0xffff;
2201 cpu.stack.notmask=0xffff0000;
2202 cpu.stack.big=false;
2203 cpu.trap_skip=false;
2204 cpu.idt.SetBase(0);
2205 cpu.idt.SetLimit(1023);
2206
2207 for (Bitu i=0; i<7; i++) {
2208 cpu.drx[i]=0;
2209 cpu.trx[i]=0;
2210 }
2211 if (CPU_ArchitectureType==CPU_ARCHTYPE_PENTIUMSLOW) {
2212 cpu.drx[6]=0xffff0ff0;
2213 } else {
2214 cpu.drx[6]=0xffff1ff0;
2215 }
2216 cpu.drx[7]=0x00000400;
2217
2218 /* Init the cpu cores */
2219 CPU_Core_Normal_Init();
2220 CPU_Core_Simple_Init();
2221 CPU_Core_Full_Init();
2222 #if (C_DYNAMIC_X86)
2223 CPU_Core_Dyn_X86_Init();
2224 #elif (C_DYNREC)
2225 CPU_Core_Dynrec_Init();
2226 #endif
2227 MAPPER_AddHandler(CPU_CycleDecrease,MK_f11,MMOD1,"cycledown","Dec Cycles");
2228 MAPPER_AddHandler(CPU_CycleIncrease,MK_f12,MMOD1,"cycleup" ,"Inc Cycles");
2229 Change_Config(configuration);
2230 CPU_JMP(false,0,0,0); //Setup the first cpu core
2231 }
2232 bool Change_Config(Section* newconfig){
2233 Section_prop * section=static_cast<Section_prop *>(newconfig);
2234 CPU_AutoDetermineMode=CPU_AUTODETERMINE_NONE;
2235 //CPU_CycleLeft=0;//needed ?
2236 CPU_Cycles=0;
2237 CPU_SkipCycleAutoAdjust=false;
2238
2239 Prop_multival* p = section->Get_multival("cycles");
2240 std::string type = p->GetSection()->Get_string("type");
2241 std::string str ;
2242 CommandLine cmd(0,p->GetSection()->Get_string("parameters"));
2243 if (type=="max") {
2244 CPU_CycleMax=0;
2245 CPU_CyclePercUsed=100;
2246 CPU_CycleAutoAdjust=true;
2247 CPU_CycleLimit=-1;
2248 for (Bitu cmdnum=1; cmdnum<=cmd.GetCount(); cmdnum++) {
2249 if (cmd.FindCommand(cmdnum,str)) {
2250 if (str.find('%')==str.length()-1) {
2251 str.erase(str.find('%'));
2252 int percval=0;
2253 std::istringstream stream(str);
2254 stream >> percval;
2255 if ((percval>0) && (percval<=105)) CPU_CyclePercUsed=(Bit32s)percval;
2256 } else if (str=="limit") {
2257 cmdnum++;
2258 if (cmd.FindCommand(cmdnum,str)) {
2259 int cyclimit=0;
2260 std::istringstream stream(str);
2261 stream >> cyclimit;
2262 if (cyclimit>0) CPU_CycleLimit=cyclimit;
2263 }
2264 }
2265 }
2266 }
2267 } else {
2268 if (type=="auto") {
2269 CPU_AutoDetermineMode|=CPU_AUTODETERMINE_CYCLES;
2270 CPU_CycleMax=3000;
2271 CPU_OldCycleMax=3000;
2272 CPU_CyclePercUsed=100;
2273 for (Bitu cmdnum=0; cmdnum<=cmd.GetCount(); cmdnum++) {
2274 if (cmd.FindCommand(cmdnum,str)) {
2275 if (str.find('%')==str.length()-1) {
2276 str.erase(str.find('%'));
2277 int percval=0;
2278 std::istringstream stream(str);
2279 stream >> percval;
2280 if ((percval>0) && (percval<=105)) CPU_CyclePercUsed=(Bit32s)percval;
2281 } else if (str=="limit") {
2282 cmdnum++;
2283 if (cmd.FindCommand(cmdnum,str)) {
2284 int cyclimit=0;
2285 std::istringstream stream(str);
2286 stream >> cyclimit;
2287 if (cyclimit>0) CPU_CycleLimit=cyclimit;
2288 }
2289 } else {
2290 int rmdval=0;
2291 std::istringstream stream(str);
2292 stream >> rmdval;
2293 if (rmdval>0) {
2294 CPU_CycleMax=(Bit32s)rmdval;
2295 CPU_OldCycleMax=(Bit32s)rmdval;
2296 }
2297 }
2298 }
2299 }
2300 } else if(type =="fixed") {
2301 cmd.FindCommand(1,str);
2302 int rmdval=0;
2303 std::istringstream stream(str);
2304 stream >> rmdval;
2305 CPU_CycleMax=(Bit32s)rmdval;
2306 } else {
2307 std::istringstream stream(type);
2308 int rmdval=0;
2309 stream >> rmdval;
2310 if(rmdval) CPU_CycleMax=(Bit32s)rmdval;
2311 }
2312 CPU_CycleAutoAdjust=false;
2313 }
2314
2315 CPU_CycleUp=section->Get_int("cycleup");
2316 CPU_CycleDown=section->Get_int("cycledown");
2317 std::string core(section->Get_string("core"));
2318 cpudecoder=&CPU_Core_Normal_Run;
2319 if (core == "normal") {
2320 cpudecoder=&CPU_Core_Normal_Run;
2321 } else if (core =="simple") {
2322 cpudecoder=&CPU_Core_Simple_Run;
2323 } else if (core == "full") {
2324 cpudecoder=&CPU_Core_Full_Run;
2325 } else if (core == "auto") {
2326 cpudecoder=&CPU_Core_Normal_Run;
2327 #if (C_DYNAMIC_X86)
2328 CPU_AutoDetermineMode|=CPU_AUTODETERMINE_CORE;
2329 }
2330 else if (core == "dynamic") {
2331 cpudecoder=&CPU_Core_Dyn_X86_Run;
2332 CPU_Core_Dyn_X86_SetFPUMode(true);
2333 } else if (core == "dynamic_nodhfpu") {
2334 cpudecoder=&CPU_Core_Dyn_X86_Run;
2335 CPU_Core_Dyn_X86_SetFPUMode(false);
2336 #elif (C_DYNREC)
2337 CPU_AutoDetermineMode|=CPU_AUTODETERMINE_CORE;
2338 }
2339 else if (core == "dynamic") {
2340 cpudecoder=&CPU_Core_Dynrec_Run;
2341 #else
2342
2343 #endif
2344 }
2345
2346 #if (C_DYNAMIC_X86)
2347 CPU_Core_Dyn_X86_Cache_Init((core == "dynamic") || (core == "dynamic_nodhfpu"));
2348 #elif (C_DYNREC)
2349 CPU_Core_Dynrec_Cache_Init( core == "dynamic" );
2350 #endif
2351
2352 CPU_ArchitectureType = CPU_ARCHTYPE_MIXED;
2353 std::string cputype(section->Get_string("cputype"));
2354 if (cputype == "auto") {
2355 CPU_ArchitectureType = CPU_ARCHTYPE_MIXED;
2356 } else if (cputype == "386") {
2357 CPU_ArchitectureType = CPU_ARCHTYPE_386FAST;
2358 } else if (cputype == "386_prefetch") {
2359 CPU_ArchitectureType = CPU_ARCHTYPE_386FAST;
2360 if (core == "normal") {
2361 cpudecoder=&CPU_Core_Prefetch_Run;
2362 CPU_PrefetchQueueSize = 16;
2363 } else if (core == "auto") {
2364 cpudecoder=&CPU_Core_Prefetch_Run;
2365 CPU_PrefetchQueueSize = 16;
2366 CPU_AutoDetermineMode&=(~CPU_AUTODETERMINE_CORE);
2367 } else {
2368 E_Exit("prefetch queue emulation requires the normal core setting.");
2369 }
2370 } else if (cputype == "386_slow") {
2371 CPU_ArchitectureType = CPU_ARCHTYPE_386SLOW;
2372 } else if (cputype == "486_slow") {
2373 CPU_ArchitectureType = CPU_ARCHTYPE_486NEWSLOW;
2374 } else if (cputype == "486_prefetch") {
2375 CPU_ArchitectureType = CPU_ARCHTYPE_486NEWSLOW;
2376 if (core == "normal") {
2377 cpudecoder=&CPU_Core_Prefetch_Run;
2378 CPU_PrefetchQueueSize = 32;
2379 } else if (core == "auto") {
2380 cpudecoder=&CPU_Core_Prefetch_Run;
2381 CPU_PrefetchQueueSize = 32;
2382 CPU_AutoDetermineMode&=(~CPU_AUTODETERMINE_CORE);
2383 } else {
2384 E_Exit("prefetch queue emulation requires the normal core setting.");
2385 }
2386 } else if (cputype == "pentium_slow") {
2387 CPU_ArchitectureType = CPU_ARCHTYPE_PENTIUMSLOW;
2388 }
2389
2390 if (CPU_ArchitectureType>=CPU_ARCHTYPE_486NEWSLOW) CPU_flag_id_toggle=FLAG_ID;
2391 else CPU_flag_id_toggle=0;
2392
2393
2394 if(CPU_CycleMax <= 0) CPU_CycleMax = 3000;
2395 if(CPU_CycleUp <= 0) CPU_CycleUp = 500;
2396 if(CPU_CycleDown <= 0) CPU_CycleDown = 20;
2397 if (CPU_CycleAutoAdjust) GFX_SetTitle(CPU_CyclePercUsed,-1,false);
2398 else GFX_SetTitle(CPU_CycleMax,-1,false);
2399 return true;
2400 }
2401 ~CPU(){ /* empty */};
2402 };
2403
2404 static CPU * test;
2405
2406 void CPU_ShutDown(Section* sec) {
2407 #if (C_DYNAMIC_X86)
2408 CPU_Core_Dyn_X86_Cache_Close();
2409 #elif (C_DYNREC)
2410 CPU_Core_Dynrec_Cache_Close();
2411 #endif
2412 delete test;
2413 }
2414
2415 void CPU_Init(Section* sec) {
2416 test = new CPU(sec);
2417 sec->AddDestroyFunction(&CPU_ShutDown,true);
2418 }
2419 //initialize static members
2420 bool CPU::inited=false;