Debian Codegraph @

include/cpu.h

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.h,v 1.57 2009-05-27 09:15:40 qbix79 Exp $ */
20
21 #ifndef DOSBOX_CPU_H
22 #define DOSBOX_CPU_H
23
24 #ifndef DOSBOX_DOSBOX_H
25 #include "dosbox.h"
26 #endif
27 #ifndef DOSBOX_REGS_H
28 #include "regs.h"
29 #endif
30 #ifndef DOSBOX_MEM_H
31 #include "mem.h"
32 #endif
33
34 #define CPU_AUTODETERMINE_NONE 0x00
35 #define CPU_AUTODETERMINE_CORE 0x01
36 #define CPU_AUTODETERMINE_CYCLES 0x02
37
38 #define CPU_AUTODETERMINE_SHIFT 0x02
39 #define CPU_AUTODETERMINE_MASK 0x03
40
41 #define CPU_CYCLES_LOWER_LIMIT 200
42
43
44 #define CPU_ARCHTYPE_MIXED 0xff
45 #define CPU_ARCHTYPE_386SLOW 0x30
46 #define CPU_ARCHTYPE_386FAST 0x35
47 #define CPU_ARCHTYPE_486OLDSLOW 0x40
48 #define CPU_ARCHTYPE_486NEWSLOW 0x45
49 #define CPU_ARCHTYPE_PENTIUMSLOW 0x50
50
51 /* CPU Cycle Timing */
52 extern Bit32s CPU_Cycles;
53 extern Bit32s CPU_CycleLeft;
54 extern Bit32s CPU_CycleMax;
55 extern Bit32s CPU_OldCycleMax;
56 extern Bit32s CPU_CyclePercUsed;
57 extern Bit32s CPU_CycleLimit;
58 extern Bit64s CPU_IODelayRemoved;
59 extern bool CPU_CycleAutoAdjust;
60 extern bool CPU_SkipCycleAutoAdjust;
61 extern Bitu CPU_AutoDetermineMode;
62
63 extern Bitu CPU_ArchitectureType;
64
65 extern Bitu CPU_PrefetchQueueSize;
66
67 /* Some common Defines */
68 /* A CPU Handler */
69 typedef Bits (CPU_Decoder)(void);
70 extern CPU_Decoder * cpudecoder;
71
72 Bits CPU_Core_Normal_Run(void);
73 Bits CPU_Core_Normal_Trap_Run(void);
74 Bits CPU_Core_Simple_Run(void);
75 Bits CPU_Core_Full_Run(void);
76 Bits CPU_Core_Dyn_X86_Run(void);
77 Bits CPU_Core_Dyn_X86_Trap_Run(void);
78 Bits CPU_Core_Dynrec_Run(void);
79 Bits CPU_Core_Dynrec_Trap_Run(void);
80 Bits CPU_Core_Prefetch_Run(void);
81 Bits CPU_Core_Prefetch_Trap_Run(void);
82
83 void CPU_Enable_SkipAutoAdjust(void);
84 void CPU_Disable_SkipAutoAdjust(void);
85 void CPU_Reset_AutoAdjust(void);
86
87
88 //CPU Stuff
89
90 extern Bit16u parity_lookup[256];
91
92 bool CPU_LLDT(Bitu selector);
93 bool CPU_LTR(Bitu selector);
94 void CPU_LIDT(Bitu limit,Bitu base);
95 void CPU_LGDT(Bitu limit,Bitu base);
96
97 Bitu CPU_STR(void);
98 Bitu CPU_SLDT(void);
99 Bitu CPU_SIDT_base(void);
100 Bitu CPU_SIDT_limit(void);
101 Bitu CPU_SGDT_base(void);
102 Bitu CPU_SGDT_limit(void);
103
104 void CPU_ARPL(Bitu & dest_sel,Bitu src_sel);
105 void CPU_LAR(Bitu selector,Bitu & ar);
106 void CPU_LSL(Bitu selector,Bitu & limit);
107
108 void CPU_SET_CRX(Bitu cr,Bitu value);
109 bool CPU_WRITE_CRX(Bitu cr,Bitu value);
110 Bitu CPU_GET_CRX(Bitu cr);
111 bool CPU_READ_CRX(Bitu cr,Bit32u & retvalue);
112
113 bool CPU_WRITE_DRX(Bitu dr,Bitu value);
114 bool CPU_READ_DRX(Bitu dr,Bit32u & retvalue);
115
116 bool CPU_WRITE_TRX(Bitu dr,Bitu value);
117 bool CPU_READ_TRX(Bitu dr,Bit32u & retvalue);
118
119 Bitu CPU_SMSW(void);
120 bool CPU_LMSW(Bitu word);
121
122 void CPU_VERR(Bitu selector);
123 void CPU_VERW(Bitu selector);
124
125 void CPU_JMP(bool use32,Bitu selector,Bitu offset,Bitu oldeip);
126 void CPU_CALL(bool use32,Bitu selector,Bitu offset,Bitu oldeip);
127 void CPU_RET(bool use32,Bitu bytes,Bitu oldeip);
128 void CPU_IRET(bool use32,Bitu oldeip);
129 void CPU_HLT(Bitu oldeip);
130
131 bool CPU_POPF(Bitu use32);
132 bool CPU_PUSHF(Bitu use32);
133 bool CPU_CLI(void);
134 bool CPU_STI(void);
135
136 bool CPU_IO_Exception(Bitu port,Bitu size);
137 void CPU_RunException(void);
138
139 void CPU_ENTER(bool use32,Bitu bytes,Bitu level);
140
141 #define CPU_INT_SOFTWARE 0x1
142 #define CPU_INT_EXCEPTION 0x2
143 #define CPU_INT_HAS_ERROR 0x4
144 #define CPU_INT_NOIOPLCHECK 0x8
145
146 void CPU_Interrupt(Bitu num,Bitu type,Bitu oldeip);
147 static INLINE void CPU_HW_Interrupt(Bitu num) {
148 CPU_Interrupt(num,0,reg_eip);
149 }
150 static INLINE void CPU_SW_Interrupt(Bitu num,Bitu oldeip) {
151 CPU_Interrupt(num,CPU_INT_SOFTWARE,oldeip);
152 }
153 static INLINE void CPU_SW_Interrupt_NoIOPLCheck(Bitu num,Bitu oldeip) {
154 CPU_Interrupt(num,CPU_INT_SOFTWARE|CPU_INT_NOIOPLCHECK,oldeip);
155 }
156
157 bool CPU_PrepareException(Bitu which,Bitu error);
158 void CPU_Exception(Bitu which,Bitu error=0);
159
160 bool CPU_SetSegGeneral(SegNames seg,Bitu value);
161 bool CPU_PopSeg(SegNames seg,bool use32);
162
163 bool CPU_CPUID(void);
164 Bitu CPU_Pop16(void);
165 Bitu CPU_Pop32(void);
166 void CPU_Push16(Bitu value);
167 void CPU_Push32(Bitu value);
168
169 void CPU_SetFlags(Bitu word,Bitu mask);
170
171
172 #define EXCEPTION_UD 6
173 #define EXCEPTION_TS 10
174 #define EXCEPTION_NP 11
175 #define EXCEPTION_SS 12
176 #define EXCEPTION_GP 13
177 #define EXCEPTION_PF 14
178
179 #define CR0_PROTECTION 0x00000001
180 #define CR0_MONITORPROCESSOR 0x00000002
181 #define CR0_FPUEMULATION 0x00000004
182 #define CR0_TASKSWITCH 0x00000008
183 #define CR0_FPUPRESENT 0x00000010
184 #define CR0_PAGING 0x80000000
185
186
187 // *********************************************************************
188 // Descriptor
189 // *********************************************************************
190
191 #define DESC_INVALID 0x00
192 #define DESC_286_TSS_A 0x01
193 #define DESC_LDT 0x02
194 #define DESC_286_TSS_B 0x03
195 #define DESC_286_CALL_GATE 0x04
196 #define DESC_TASK_GATE 0x05
197 #define DESC_286_INT_GATE 0x06
198 #define DESC_286_TRAP_GATE 0x07
199
200 #define DESC_386_TSS_A 0x09
201 #define DESC_386_TSS_B 0x0b
202 #define DESC_386_CALL_GATE 0x0c
203 #define DESC_386_INT_GATE 0x0e
204 #define DESC_386_TRAP_GATE 0x0f
205
206 /* EU/ED Expand UP/DOWN RO/RW Read Only/Read Write NA/A Accessed */
207 #define DESC_DATA_EU_RO_NA 0x10
208 #define DESC_DATA_EU_RO_A 0x11
209 #define DESC_DATA_EU_RW_NA 0x12
210 #define DESC_DATA_EU_RW_A 0x13
211 #define DESC_DATA_ED_RO_NA 0x14
212 #define DESC_DATA_ED_RO_A 0x15
213 #define DESC_DATA_ED_RW_NA 0x16
214 #define DESC_DATA_ED_RW_A 0x17
215
216 /* N/R Readable NC/C Confirming A/NA Accessed */
217 #define DESC_CODE_N_NC_A 0x18
218 #define DESC_CODE_N_NC_NA 0x19
219 #define DESC_CODE_R_NC_A 0x1a
220 #define DESC_CODE_R_NC_NA 0x1b
221 #define DESC_CODE_N_C_A 0x1c
222 #define DESC_CODE_N_C_NA 0x1d
223 #define DESC_CODE_R_C_A 0x1e
224 #define DESC_CODE_R_C_NA 0x1f
225
226 #ifdef _MSC_VER
227 #pragma pack (1)
228 #endif
229
230 struct S_Descriptor {
231 #ifdef WORDS_BIGENDIAN
232 Bit32u base_0_15 :16;
233 Bit32u limit_0_15 :16;
234 Bit32u base_24_31 :8;
235 Bit32u g :1;
236 Bit32u big :1;
237 Bit32u r :1;
238 Bit32u avl :1;
239 Bit32u limit_16_19 :4;
240 Bit32u p :1;
241 Bit32u dpl :2;
242 Bit32u type :5;
243 Bit32u base_16_23 :8;
244 #else
245 Bit32u limit_0_15 :16;
246 Bit32u base_0_15 :16;
247 Bit32u base_16_23 :8;
248 Bit32u type :5;
249 Bit32u dpl :2;
250 Bit32u p :1;
251 Bit32u limit_16_19 :4;
252 Bit32u avl :1;
253 Bit32u r :1;
254 Bit32u big :1;
255 Bit32u g :1;
256 Bit32u base_24_31 :8;
257 #endif
258 }GCC_ATTRIBUTE(packed);
259
260 struct G_Descriptor {
261 #ifdef WORDS_BIGENDIAN
262 Bit32u selector: 16;
263 Bit32u offset_0_15 :16;
264 Bit32u offset_16_31 :16;
265 Bit32u p :1;
266 Bit32u dpl :2;
267 Bit32u type :5;
268 Bit32u reserved :3;
269 Bit32u paramcount :5;
270 #else
271 Bit32u offset_0_15 :16;
272 Bit32u selector :16;
273 Bit32u paramcount :5;
274 Bit32u reserved :3;
275 Bit32u type :5;
276 Bit32u dpl :2;
277 Bit32u p :1;
278 Bit32u offset_16_31 :16;
279 #endif
280 } GCC_ATTRIBUTE(packed);
281
282 struct TSS_16 {
283 Bit16u back; /* Back link to other task */
284 Bit16u sp0; /* The CK stack pointer */
285 Bit16u ss0; /* The CK stack selector */
286 Bit16u sp1; /* The parent KL stack pointer */
287 Bit16u ss1; /* The parent KL stack selector */
288 Bit16u sp2; /* Unused */
289 Bit16u ss2; /* Unused */
290 Bit16u ip; /* The instruction pointer */
291 Bit16u flags; /* The flags */
292 Bit16u ax, cx, dx, bx; /* The general purpose registers */
293 Bit16u sp, bp, si, di; /* The special purpose registers */
294 Bit16u es; /* The extra selector */
295 Bit16u cs; /* The code selector */
296 Bit16u ss; /* The application stack selector */
297 Bit16u ds; /* The data selector */
298 Bit16u ldt; /* The local descriptor table */
299 } GCC_ATTRIBUTE(packed);
300
301 struct TSS_32 {
302 Bit32u back; /* Back link to other task */
303 Bit32u esp0; /* The CK stack pointer */
304 Bit32u ss0; /* The CK stack selector */
305 Bit32u esp1; /* The parent KL stack pointer */
306 Bit32u ss1; /* The parent KL stack selector */
307 Bit32u esp2; /* Unused */
308 Bit32u ss2; /* Unused */
309 Bit32u cr3; /* The page directory pointer */
310 Bit32u eip; /* The instruction pointer */
311 Bit32u eflags; /* The flags */
312 Bit32u eax, ecx, edx, ebx; /* The general purpose registers */
313 Bit32u esp, ebp, esi, edi; /* The special purpose registers */
314 Bit32u es; /* The extra selector */
315 Bit32u cs; /* The code selector */
316 Bit32u ss; /* The application stack selector */
317 Bit32u ds; /* The data selector */
318 Bit32u fs; /* And another extra selector */
319 Bit32u gs; /* ... and another one */
320 Bit32u ldt; /* The local descriptor table */
321 } GCC_ATTRIBUTE(packed);
322
323 #ifdef _MSC_VER
324 #pragma pack()
325 #endif
326 class Descriptor
327 {
328 public:
329 Descriptor() { saved.fill[0]=saved.fill[1]=0; }
330
331 void Load(PhysPt address);
332 void Save(PhysPt address);
333
334 PhysPt GetBase (void) {
335 return (saved.seg.base_24_31<<24) | (saved.seg.base_16_23<<16) | saved.seg.base_0_15;
336 }
337 Bitu GetLimit (void) {
338 Bitu limit = (saved.seg.limit_16_19<<16) | saved.seg.limit_0_15;
339 if (saved.seg.g) return (limit<<12) | 0xFFF;
340 return limit;
341 }
342 Bitu GetOffset(void) {
343 return (saved.gate.offset_16_31 << 16) | saved.gate.offset_0_15;
344 }
345 Bitu GetSelector(void) {
346 return saved.gate.selector;
347 }
348 Bitu Type(void) {
349 return saved.seg.type;
350 }
351 Bitu Conforming(void) {
352 return saved.seg.type & 8;
353 }
354 Bitu DPL(void) {
355 return saved.seg.dpl;
356 }
357 Bitu Big(void) {
358 return saved.seg.big;
359 }
360 public:
361 union {
362 S_Descriptor seg;
363 G_Descriptor gate;
364 Bit32u fill[2];
365 } saved;
366 };
367
368 class DescriptorTable {
369 public:
370 PhysPt GetBase (void) { return table_base; }
371 Bitu GetLimit (void) { return table_limit; }
372 void SetBase (PhysPt _base) { table_base = _base; }
373 void SetLimit (Bitu _limit) { table_limit= _limit; }
374
375 bool GetDescriptor (Bitu selector, Descriptor& desc) {
376 selector&=~7;
377 if (selector>=table_limit) return false;
378 desc.Load(table_base+(selector));
379 return true;
380 }
381 protected:
382 PhysPt table_base;
383 Bitu table_limit;
384 };
385
386 class GDTDescriptorTable : public DescriptorTable {
387 public:
388 bool GetDescriptor(Bitu selector, Descriptor& desc) {
389 Bitu address=selector & ~7;
390 if (selector & 4) {
391 if (address>=ldt_limit) return false;
392 desc.Load(ldt_base+address);
393 return true;
394 } else {
395 if (address>=table_limit) return false;
396 desc.Load(table_base+address);
397 return true;
398 }
399 }
400 bool SetDescriptor(Bitu selector, Descriptor& desc) {
401 Bitu address=selector & ~7;
402 if (selector & 4) {
403 if (address>=ldt_limit) return false;
404 desc.Save(ldt_base+address);
405 return true;
406 } else {
407 if (address>=table_limit) return false;
408 desc.Save(table_base+address);
409 return true;
410 }
411 }
412 Bitu SLDT(void) {
413 return ldt_value;
414 }
415 bool LLDT(Bitu value) {
416 if ((value&0xfffc)==0) {
417 ldt_value=0;
418 ldt_base=0;
419 ldt_limit=0;
420 return true;
421 }
422 Descriptor desc;
423 if (!GetDescriptor(value,desc)) return !CPU_PrepareException(EXCEPTION_GP,value);
424 if (desc.Type()!=DESC_LDT) return !CPU_PrepareException(EXCEPTION_GP,value);
425 if (!desc.saved.seg.p) return !CPU_PrepareException(EXCEPTION_NP,value);
426 ldt_base=desc.GetBase();
427 ldt_limit=desc.GetLimit();
428 ldt_value=value;
429 return true;
430 }
431 private:
432 PhysPt ldt_base;
433 Bitu ldt_limit;
434 Bitu ldt_value;
435 };
436
437 class TSS_Descriptor : public Descriptor {
438 public:
439 Bitu IsBusy(void) {
440 return saved.seg.type & 2;
441 }
442 Bitu Is386(void) {
443 return saved.seg.type & 8;
444 }
445 void SetBusy(bool busy) {
446 if (busy) saved.seg.type|=2;
447 else saved.seg.type&=~2;
448 }
449 };
450
451
452 struct CPUBlock {
453 Bitu cpl; /* Current Privilege */
454 Bitu mpl;
455 Bitu cr0;
456 bool pmode; /* Is Protected mode enabled */
457 GDTDescriptorTable gdt;
458 DescriptorTable idt;
459 struct {
460 Bitu mask,notmask;
461 bool big;
462 } stack;
463 struct {
464 bool big;
465 } code;
466 struct {
467 Bitu cs,eip;
468 CPU_Decoder * old_decoder;
469 } hlt;
470 struct {
471 Bitu which,error;
472 } exception;
473 Bits direction;
474 bool trap_skip;
475 Bit32u drx[8];
476 Bit32u trx[8];
477 };
478
479 extern CPUBlock cpu;
480
481 static INLINE void CPU_SetFlagsd(Bitu word) {
482 Bitu mask=cpu.cpl ? FMASK_NORMAL : FMASK_ALL;
483 CPU_SetFlags(word,mask);
484 }
485
486 static INLINE void CPU_SetFlagsw(Bitu word) {
487 Bitu mask=(cpu.cpl ? FMASK_NORMAL : FMASK_ALL) & 0xffff;
488 CPU_SetFlags(word,mask);
489 }
490
491
492 #endif