FreeNOS
IntelCore.cpp
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#include <Log.h>
19#include <String.h>
20#include "IntelCore.h"
21
22#pragma clang optimize off
23#pragma GCC push_options
24#pragma GCC optimize ("O0")
25
27{
28 String s;
29
30 switch (state->vector)
31 {
32 case INTEL_DIVZERO:
33 ERROR("Divide Error Exception");
34 break;
35
36 case INTEL_DEBUGEX:
37 ERROR("Debug Exception");
38 break;
39
40 case INTEL_NMI:
41 ERROR("Non-Maskable Interrupt Exception");
42 break;
43
44 case INTEL_BREAKP:
45 ERROR("Breakpoint Exception");
46 break;
47
48 case INTEL_OVERFLOW:
49 ERROR("Overflow Exception");
50 break;
51
52 case INTEL_BOUNDS:
53 ERROR("BOUND Range Exceeded Exception");
54 break;
55
56 case INTEL_OPCODE:
57 ERROR("Invalid Opcode Exception");
58 break;
59
60 case INTEL_DEVERR:
61 ERROR("Device Not Available Exception");
62 break;
63
64 case INTEL_DOUBLEF:
65 ERROR("Double Fault Exception");
66 break;
67
68 case INTEL_COSEG:
69 ERROR("Coprocessor Segment Overrun Exception");
70 break;
71
72 case INTEL_TSSERR:
73 ERROR("Invalid Task State Segment Exception");
74 break;
75
76 case INTEL_SEGERR:
77 ERROR("Segment Not Present Exception");
78 break;
79
80 case INTEL_STACKERR:
81 ERROR("Stack Fault Exception");
82 break;
83
84 case INTEL_GENERR:
85 ERROR("General Protection Fault Exception");
86 break;
87
88 case INTEL_PAGEFAULT:
89 ERROR("Page Fault Exception");
90 s << "Error " << state->error << " at " << Number::Hex << readCR2();
91 ERROR(*s);
92 break;
93
94 case INTEL_FLOATERR:
95 ERROR("Floating Point Error Exception");
96 break;
97
98 case INTEL_ALIGNERR:
99 ERROR("Alignment Check Exception");
100 break;
101
102 case INTEL_MACHCHK:
103 ERROR("Machine Check Exception");
104 break;
105
106 case INTEL_SIMD:
107 ERROR("SIMD Floating Point Exception");
108 break;
109
110 case INTEL_VIRTERR:
111 ERROR("Virtualization Exception");
112 break;
113
114 default:
115 ERROR("Unknown Exception: " << state->vector);
116 break;
117 }
118 logState(state);
119}
120
122{
123 logRegister("eip", state->irq.eip);
124 logRegister("eax", state->regs.eax);
125 logRegister("ebx", state->regs.ebx);
126 logRegister("ecx", state->regs.ecx);
127 logRegister("edx", state->regs.edx);
128 logRegister("esi", state->regs.esi);
129 logRegister("edi", state->regs.edi);
130 logRegister("ebp", state->regs.ebp);
131 logRegister("esp", state->regs.esp0);
132}
133
134void IntelCore::logRegister(const char *name, u32 reg) const
135{
136 String s;
137 s << Number::Hex << name << " = " << reg << Number::Dec << " (" << reg << ")";
138
139 ERROR(*s);
140}
141
142volatile u32 IntelCore::readCR2() const
143{
144 volatile u32 cr2;
145 asm volatile("mov %%cr2, %%eax\n"
146 "mov %%eax, %0\n" : "=r" (cr2));
147 return cr2;
148}
149
150volatile u32 IntelCore::readCR3() const
151{
152 volatile u32 cr3;
153 asm volatile("mov %%cr3, %%eax\n"
154 "mov %%eax, %0\n" : "=r" (cr3));
155 return cr3;
156}
157
159{
160 asm volatile("mov %0, %%eax\n"
161 "mov %%eax, %%cr3" :: "r" (cr3));
162}
volatile u32 readCR3() const
Read the CR3 register.
void writeCR3(u32 cr3) const
Write the CR3 register.
void logState(CPUState *state) const
Log the CPU state.
void logRegister(const char *name, u32 reg) const
Log a register.
void logException(CPUState *state) const
Log a CPU exception.
Definition IntelCore.cpp:26
volatile u32 readCR2() const
Read the CR2 register.
Abstraction of strings.
Definition String.h:42
#define INTEL_SIMD
Definition IntelCore.h:125
#define INTEL_GENERR
Definition IntelCore.h:120
#define INTEL_STACKERR
Definition IntelCore.h:119
#define INTEL_DOUBLEF
Definition IntelCore.h:115
#define INTEL_PAGEFAULT
Definition IntelCore.h:121
#define INTEL_COSEG
Definition IntelCore.h:116
#define INTEL_OPCODE
Definition IntelCore.h:113
#define INTEL_DEBUGEX
Definition IntelCore.h:108
#define INTEL_ALIGNERR
Definition IntelCore.h:123
#define INTEL_NMI
Definition IntelCore.h:109
#define INTEL_BREAKP
Definition IntelCore.h:110
#define INTEL_SEGERR
Definition IntelCore.h:118
#define INTEL_VIRTERR
Definition IntelCore.h:126
#define INTEL_MACHCHK
Definition IntelCore.h:124
#define INTEL_DIVZERO
Definition IntelCore.h:107
#define INTEL_FLOATERR
Definition IntelCore.h:122
#define INTEL_BOUNDS
Definition IntelCore.h:112
#define INTEL_DEVERR
Definition IntelCore.h:114
#define INTEL_TSSERR
Definition IntelCore.h:117
#define INTEL_OVERFLOW
Definition IntelCore.h:111
unsigned int u32
Unsigned 32-bit number.
Definition Types.h:53
#define ERROR(msg)
Output an error message.
Definition Log.h:61
@ Hex
Decimal: 0-10.
Definition Types.h:171
@ Dec
Definition Types.h:170
u32 ebp
Definition IntelCore.h:202
u32 eax
Definition IntelCore.h:202
u32 esi
Definition IntelCore.h:202
u32 esp0
Definition IntelCore.h:202
u32 edi
Definition IntelCore.h:202
u32 edx
Definition IntelCore.h:202
u32 ebx
Definition IntelCore.h:202
u32 ecx
Definition IntelCore.h:202
Contains all the CPU registers.
Definition ARMCore.h:244
u32 vector
Definition IntelCore.h:247
IRQRegs3 irq
Definition IntelCore.h:250
u32 error
Definition IntelCore.h:247
CPURegs regs
Definition IntelCore.h:244