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 
121 void IntelCore::logState(CPUState *state) const
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 
134 void 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 
142 volatile 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 
150 volatile 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 
158 void IntelCore::writeCR3(u32 cr3) const
159 {
160  asm volatile("mov %0, %%eax\n"
161  "mov %%eax, %%cr3" :: "r" (cr3));
162 }
INTEL_DEVERR
#define INTEL_DEVERR
Definition: IntelCore.h:114
INTEL_FLOATERR
#define INTEL_FLOATERR
Definition: IntelCore.h:122
CPURegs::esp0
u32 esp0
Definition: IntelCore.h:202
INTEL_DEBUGEX
#define INTEL_DEBUGEX
Definition: IntelCore.h:108
CPURegs::ebp
u32 ebp
Definition: IntelCore.h:202
INTEL_GENERR
#define INTEL_GENERR
Definition: IntelCore.h:120
IntelCore::writeCR3
void writeCR3(u32 cr3) const
Write the CR3 register.
Definition: IntelCore.cpp:158
CPURegs::eax
u32 eax
Definition: IntelCore.h:202
String
Abstraction of strings.
Definition: String.h:41
INTEL_DIVZERO
#define INTEL_DIVZERO
Definition: IntelCore.h:107
IntelCore::logState
void logState(CPUState *state) const
Log the CPU state.
Definition: IntelCore.cpp:121
INTEL_SIMD
#define INTEL_SIMD
Definition: IntelCore.h:125
IntelCore::logRegister
void logRegister(const char *name, u32 reg) const
Log a register.
Definition: IntelCore.cpp:134
INTEL_COSEG
#define INTEL_COSEG
Definition: IntelCore.h:116
Number::Dec
@ Dec
Definition: Types.h:170
CPURegs::ebx
u32 ebx
Definition: IntelCore.h:202
CPURegs::edi
u32 edi
Definition: IntelCore.h:202
IntelCore::readCR3
volatile u32 readCR3() const
Read the CR3 register.
Definition: IntelCore.cpp:150
Log.h
INTEL_TSSERR
#define INTEL_TSSERR
Definition: IntelCore.h:117
CPUState::irq
IRQRegs3 irq
Definition: IntelCore.h:250
INTEL_ALIGNERR
#define INTEL_ALIGNERR
Definition: IntelCore.h:123
CPURegs::esi
u32 esi
Definition: IntelCore.h:202
INTEL_BREAKP
#define INTEL_BREAKP
Definition: IntelCore.h:110
CPURegs::edx
u32 edx
Definition: IntelCore.h:202
INTEL_BOUNDS
#define INTEL_BOUNDS
Definition: IntelCore.h:112
INTEL_PAGEFAULT
#define INTEL_PAGEFAULT
Definition: IntelCore.h:121
INTEL_OPCODE
#define INTEL_OPCODE
Definition: IntelCore.h:113
INTEL_SEGERR
#define INTEL_SEGERR
Definition: IntelCore.h:118
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
INTEL_STACKERR
#define INTEL_STACKERR
Definition: IntelCore.h:119
CPUState::regs
CPURegs regs
Definition: IntelCore.h:244
INTEL_VIRTERR
#define INTEL_VIRTERR
Definition: IntelCore.h:126
CPUState
Contains all the CPU registers.
Definition: ARMCore.h:243
IntelCore.h
ERROR
#define ERROR(msg)
Output an error message.
Definition: Log.h:61
IntelCore::readCR2
volatile u32 readCR2() const
Read the CR2 register.
Definition: IntelCore.cpp:142
CPUState::error
u32 error
Definition: IntelCore.h:247
INTEL_OVERFLOW
#define INTEL_OVERFLOW
Definition: IntelCore.h:111
INTEL_MACHCHK
#define INTEL_MACHCHK
Definition: IntelCore.h:124
String.h
CPURegs::ecx
u32 ecx
Definition: IntelCore.h:202
INTEL_NMI
#define INTEL_NMI
Definition: IntelCore.h:109
CPUState::vector
u32 vector
Definition: IntelCore.h:247
INTEL_DOUBLEF
#define INTEL_DOUBLEF
Definition: IntelCore.h:115
Number::Hex
@ Hex
Decimal: 0-10.
Definition: Types.h:171
IRQRegs3::eip
u32 eip
Definition: IntelCore.h:231