FreeNOS
IntelCore.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Niek Linnenbank
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 3 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, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef __LIBARCH_INTEL_CPU_H
19 #define __LIBARCH_INTEL_CPU_H
20 
21 #include <Types.h>
22 #include <Macros.h>
23 #include "IntelIO.h"
24 
41 inline u64 timestamp()
42 {
43  unsigned long long val;
44  asm volatile ("rdtsc\n" : "=A"(val));
45  return val;
46 }
47 
51 #define cpu_reboot() \
52 ({ \
53  IntelIO io; \
54  io.outb(0x64, 0xfe); \
55 })
56 
64 #define cpu_shutdown() \
65 ({ \
66  IntelIO io; \
67  io.outw(0xB004, 0x0 | 0x2000); \
68 })
69 
73 #define idle() \
74  asm volatile ("hlt");
75 
81 #define ltr(sel) \
82 ({ \
83  u16 tr = sel; \
84  asm volatile ("ltr %0\n" :: "r"(tr)); \
85 })
86 
92 #define tlb_flush(addr) \
93  asm volatile("invlpg (%0)" ::"r" (addr) : "memory")
94 
98 #define tlb_flush_all() \
99  asm volatile("mov %cr3, %eax\n" \
100  "mov %eax, %cr3\n")
101 
107 #define INTEL_DIVZERO 0
108 #define INTEL_DEBUGEX 1
109 #define INTEL_NMI 2
110 #define INTEL_BREAKP 3
111 #define INTEL_OVERFLOW 4
112 #define INTEL_BOUNDS 5
113 #define INTEL_OPCODE 6
114 #define INTEL_DEVERR 7
115 #define INTEL_DOUBLEF 8
116 #define INTEL_COSEG 9
117 #define INTEL_TSSERR 10
118 #define INTEL_SEGERR 11
119 #define INTEL_STACKERR 12
120 #define INTEL_GENERR 13
121 #define INTEL_PAGEFAULT 14
122 #define INTEL_FLOATERR 16
123 #define INTEL_ALIGNERR 17
124 #define INTEL_MACHCHK 18
125 #define INTEL_SIMD 19
126 #define INTEL_VIRTERR 20
127 
137 #define INTEL_EFLAGS_DEFAULT (1 << 1)
138 #define INTEL_EFLAGS_IRQ (1 << 9)
139 
152 typedef struct TSS
153 {
164  u32 es, cs, ss, ds, fs, gs;
167 }
168 TSS;
169 
173 typedef struct Segment
174 {
184 }
185 Segment;
186 
190 typedef struct SegRegs
191 {
192  /* Segments. */
193  u32 gs, fs, es, ds, ss0;
194 }
195 SegRegs;
196 
200 typedef struct CPURegs
201 {
203 }
204 CPURegs;
205 
212 typedef struct IRQRegs0
213 {
215 }
216 IRQRegs0;
217 
229 typedef struct IRQRegs3
230 {
232 }
233 IRQRegs3;
234 
238 typedef struct CPUState
239 {
240  /* Segments. */
242 
243  /* By pusha */
245 
246  /* Vector/error arguments. */
248 
249  /* Pushed by processor. */
251 }
252 CPUState;
253 
258 {
259  public:
260 
266  void logException(CPUState *state) const;
267 
273  void logState(CPUState *state) const;
274 
281  void logRegister(const char *name, u32 reg) const;
282 
286  volatile u32 readCR2() const;
287 
291  volatile u32 readCR3() const;
292 
296  void writeCR3(u32 cr3) const;
297 };
298 
299 #ifdef __KERNEL__
300 
302 extern Segment gdt[];
303 
305 extern TSS kernelTss;
306 
308 extern Address kernelPageDir[];
309 
310 #endif /* __KERNEL__ */
311 
318 #endif /* __LIBARCH_INTEL_CPU_H */
IRQRegs3::esp3
u32 esp3
Definition: IntelCore.h:231
TSS::ldt
u32 ldt
Definition: IntelCore.h:165
Segment::type
u32 type
Definition: IntelCore.h:178
SegRegs::gs
u32 gs
Definition: IntelCore.h:193
IntelIO.h
CPURegs::esp0
u32 esp0
Definition: IntelCore.h:202
CPURegs::ebp
u32 ebp
Definition: IntelCore.h:202
Macros.h
TSS::es
u32 es
Definition: IntelCore.h:164
IntelCore::writeCR3
void writeCR3(u32 cr3) const
Write the CR3 register.
Definition: IntelCore.cpp:158
Types.h
kernelPageDir
Address kernelPageDir[]
Kernel page directory.
CPURegs::eax
u32 eax
Definition: IntelCore.h:202
SegRegs::es
u32 es
Definition: IntelCore.h:193
Segment::limitHigh
u32 limitHigh
Definition: IntelCore.h:181
Segment
struct Segment Segment
Segment descriptor used in the GDT.
IRQRegs3
struct IRQRegs3 IRQRegs3
Unprivileged Interrupt Registers (ring 3)
timestamp
u64 timestamp()
Reads the CPU's timestamp counter.
Definition: IntelCore.h:41
TSS::fs
u32 fs
Definition: IntelCore.h:164
IntelCore::logState
void logState(CPUState *state) const
Log the CPU state.
Definition: IntelCore.cpp:121
CPURegs
struct CPURegs CPURegs
Structure represents the pusha/popa format.
SegRegs::fs
u32 fs
Definition: IntelCore.h:193
IntelCore::logRegister
void logRegister(const char *name, u32 reg) const
Log a register.
Definition: IntelCore.cpp:134
IRQRegs0::eflags
u32 eflags
Definition: IntelCore.h:214
TSS::esi
u32 esi
Definition: IntelCore.h:163
SegRegs
struct SegRegs SegRegs
Segmentation registers.
Address
unsigned long Address
A memory address.
Definition: Types.h:131
CPURegs::ebx
u32 ebx
Definition: IntelCore.h:202
IRQRegs3::cs
u32 cs
Definition: IntelCore.h:231
CPURegs::edi
u32 edi
Definition: IntelCore.h:202
TSS::ss0
u32 ss0
Definition: IntelCore.h:155
IntelCore::readCR3
volatile u32 readCR3() const
Read the CR3 register.
Definition: IntelCore.cpp:150
TSS::edi
u32 edi
Definition: IntelCore.h:163
kernelTss
TSS kernelTss
Task State Segment.
TSS::backlink
u32 backlink
Definition: IntelCore.h:154
CPUState::irq
IRQRegs3 irq
Definition: IntelCore.h:250
TSS::eax
u32 eax
Definition: IntelCore.h:161
IRQRegs0::eip
u32 eip
Definition: IntelCore.h:214
CPUState::seg
SegRegs seg
Definition: IntelCore.h:241
TSS
Intel's Task State Segment.
Definition: IntelCore.h:152
u64
unsigned long long u64
Unsigned 64-bit number.
Definition: Types.h:50
TSS::ss1
u32 ss1
Definition: IntelCore.h:156
CPURegs::esi
u32 esi
Definition: IntelCore.h:202
CPURegs::edx
u32 edx
Definition: IntelCore.h:202
CPURegs
Structure represents the pusha/popa format.
Definition: IntelCore.h:200
Segment::baseLow
u32 baseLow
Definition: IntelCore.h:176
TSS::esp2
u32 esp2
Definition: IntelCore.h:157
TSS::ecx
u32 ecx
Definition: IntelCore.h:161
u32
unsigned int u32
Unsigned 32-bit number.
Definition: Types.h:53
IntelCore::logException
void logException(CPUState *state) const
Log a CPU exception.
Definition: IntelCore.cpp:26
TSS::cs
u32 cs
Definition: IntelCore.h:164
TSS::esp1
u32 esp1
Definition: IntelCore.h:156
Segment::present
u32 present
Definition: IntelCore.h:180
TSS::edx
u32 edx
Definition: IntelCore.h:161
CPUState::regs
CPURegs regs
Definition: IntelCore.h:244
SegRegs::ds
u32 ds
Definition: IntelCore.h:193
CPUState
Contains all the CPU registers.
Definition: ARMCore.h:243
TSS::esp0
u32 esp0
Definition: IntelCore.h:155
TSS::ss2
u32 ss2
Definition: IntelCore.h:157
TSS::esp
u32 esp
Definition: IntelCore.h:162
IRQRegs0
Privileged Interrupt Registers (ring 0)
Definition: IntelCore.h:212
SegRegs
Segmentation registers.
Definition: IntelCore.h:190
TSS::ss
u32 ss
Definition: IntelCore.h:164
Segment::limitLow
u32 limitLow
Definition: IntelCore.h:175
IntelCore::readCR2
volatile u32 readCR2() const
Read the CR2 register.
Definition: IntelCore.cpp:142
Segment
Segment descriptor used in the GDT.
Definition: IntelCore.h:173
CPUState::error
u32 error
Definition: IntelCore.h:247
TSS::cr3
u32 cr3
Definition: IntelCore.h:158
TSS::gs
u32 gs
Definition: IntelCore.h:164
SegRegs::ss0
u32 ss0
Definition: IntelCore.h:193
IRQRegs3::eflags
u32 eflags
Definition: IntelCore.h:231
TSS::ebp
u32 ebp
Definition: IntelCore.h:162
IRQRegs3
Unprivileged Interrupt Registers (ring 3)
Definition: IntelCore.h:229
TSS::bitmap
u32 bitmap
Definition: IntelCore.h:166
CPUState
struct CPUState CPUState
Contains all the CPU registers.
gdt
Segment gdt[]
Global Descriptor Table.
TSS::eip
u32 eip
Definition: IntelCore.h:159
CPURegs::ecx
u32 ecx
Definition: IntelCore.h:202
CPUState::vector
u32 vector
Definition: IntelCore.h:247
TSS
struct TSS TSS
Intel's Task State Segment.
IRQRegs0
struct IRQRegs0 IRQRegs0
Privileged Interrupt Registers (ring 0)
Segment::privilege
u32 privilege
Definition: IntelCore.h:179
IntelCore
Intel CPU Core.
Definition: IntelCore.h:257
TSS::ds
u32 ds
Definition: IntelCore.h:164
Segment::baseMid
u32 baseMid
Definition: IntelCore.h:177
IRQRegs3::ss3
u32 ss3
Definition: IntelCore.h:231
TSS::ebx
u32 ebx
Definition: IntelCore.h:161
Segment::baseHigh
u32 baseHigh
Definition: IntelCore.h:183
IRQRegs0::cs
u32 cs
Definition: IntelCore.h:214
Segment::granularity
u32 granularity
Definition: IntelCore.h:182
TSS::eflags
u32 eflags
Definition: IntelCore.h:160
IRQRegs3::eip
u32 eip
Definition: IntelCore.h:231