MonkOS  v0.1
A simple 64-bit operating system (x86_64)
cpu.h
Go to the documentation of this file.
1 //============================================================================
2 /// @file cpu.h
3 /// @brief x86 CPU-specific function implementations.
4 //
5 // Copyright 2016 Brett Vickers.
6 // Use of this source code is governed by a BSD-style license
7 // that can be found in the MonkOS LICENSE file.
8 //============================================================================
9 
10 #pragma once
11 
12 #include <core.h>
13 
14 // CPU EFLAGS register values
15 #define CPU_EFLAGS_CARRY (1 << 0)
16 #define CPU_EFLAGS_PARITY (1 << 2)
17 #define CPU_EFLAGS_ADJUST (1 << 4)
18 #define CPU_EFLAGS_ZERO (1 << 6)
19 #define CPU_EFLAGS_SIGN (1 << 7)
20 #define CPU_EFLAGS_TRAP (1 << 8)
21 #define CPU_EFLAGS_INTERRUPT (1 << 9)
22 #define CPU_EFLAGS_DIRECTION (1 << 10)
23 #define CPU_EFLAGS_OVERFLOW (1 << 11)
24 #define CPU_EFLAGS_IOPL1 (1 << 12)
25 #define CPU_EFLAGS_IOPL0 (1 << 13)
26 #define CPU_EFLAGS_NESTED (1 << 14)
27 #define CPU_EFLAGS_RESUME (1 << 16)
28 #define CPU_EFLAGS_V8086 (1 << 17)
29 #define CPU_EFLAGS_ALIGNCHECK (1 << 18)
30 #define CPU_EFLAGS_VINTERRUPT (1 << 19)
31 #define CPU_EFLAGS_VPENDING (1 << 20)
32 #define CPU_EFLAGS_CPUID (1 << 21)
33 
34 //----------------------------------------------------------------------------
35 // @struct registers_t
36 /// @brief A record describing all 64-bit general-purpose registers.
37 //----------------------------------------------------------------------------
38 typedef struct registers
39 {
40  uint64_t rax;
41  uint64_t rbx;
42  uint64_t rcx;
43  uint64_t rdx;
44  uint64_t rsi;
45  uint64_t rdi;
46  uint64_t rbp;
47  uint64_t r8;
48  uint64_t r9;
49  uint64_t r10;
50  uint64_t r11;
51  uint64_t r12;
52  uint64_t r13;
53  uint64_t r14;
54  uint64_t r15;
55 } registers_t;
56 
57 //----------------------------------------------------------------------------
58 // @struct registers4_t
59 /// @brief A record describing the first 4 general-purpose registers.
60 //----------------------------------------------------------------------------
61 typedef struct registers4
62 {
63  uint64_t rax;
64  uint64_t rbx;
65  uint64_t rcx;
66  uint64_t rdx;
67 } registers4_t;
68 
69 
70 #ifdef __NO_INLINE__
71 
72 //----------------------------------------------------------------------------
73 // @function cpuid
74 /// @brief Return the results of the CPUID instruction.
75 /// @param[in] code The cpuid group code.
76 /// @param[out] regs The contents of registers rax, rbx, rcx, and rdx.
77 //----------------------------------------------------------------------------
78 void
79 cpuid(uint32_t code, registers4_t *regs);
80 
81 //----------------------------------------------------------------------------
82 // @function rdmsr
83 /// @brief Read the model-specific register and return the result.
84 /// @param[in] id The register id to read.
85 /// @returns The contents of the requested MSR.
86 //----------------------------------------------------------------------------
87 uint64_t
88 rdmsr(uint32_t id);
89 
90 //----------------------------------------------------------------------------
91 // @function wrmsr
92 /// @brief Write to the model-specific register.
93 /// @param[in] id The register id to write.
94 /// @param[in] value The value to write.
95 //----------------------------------------------------------------------------
96 void
97 wrmsr(uint32_t id, uint64_t value);
98 
99 //----------------------------------------------------------------------------
100 // @function io_inb
101 /// @brief Retrieve a byte value from an input port.
102 /// @param[in] port Port number (0-65535).
103 /// @returns value Byte value read from the port.
104 //----------------------------------------------------------------------------
105 uint8_t
106 io_inb(uint16_t port);
107 
108 //----------------------------------------------------------------------------
109 // @function io_outb
110 /// @brief Write a byte value to an output port.
111 /// @param[in] port Port number (0-65535).
112 /// @param[in] value Byte value to write to the port.
113 //----------------------------------------------------------------------------
114 void
115 io_outb(uint16_t port, uint8_t value);
116 
117 //----------------------------------------------------------------------------
118 // @function io_inw
119 /// @brief Retrieve a 16-bit word value from an input port.
120 /// @param[in] port Port number (0-65535).
121 /// @returns value Word value read from the port.
122 //----------------------------------------------------------------------------
123 uint16_t
124 io_inw(uint16_t port);
125 
126 //----------------------------------------------------------------------------
127 // @function io_outw
128 /// @brief Write a 16-bit word value to an output port.
129 /// @param[in] port Port number (0-65535).
130 /// @param[in] value Word value to write to the port.
131 //----------------------------------------------------------------------------
132 void
133 io_outw(uint16_t port, uint16_t value);
134 
135 //----------------------------------------------------------------------------
136 // @function io_ind
137 /// @brief Retrieve a 32-bit dword value from an input port.
138 /// @param[in] port Port number (0-65535).
139 /// @returns value Dword value read from the port.
140 //----------------------------------------------------------------------------
141 uint32_t
142 io_ind(uint16_t port);
143 
144 //----------------------------------------------------------------------------
145 // @function io_outd
146 /// @brief Write a 32-bit dword value to an output port.
147 /// @param[in] port Port number (0-65535).
148 /// @param[in] value Dword value to write to the port.
149 //----------------------------------------------------------------------------
150 void
151 io_outd(uint16_t port, uint32_t value);
152 
153 //----------------------------------------------------------------------------
154 // @function set_pagetable
155 /// @brief Update the CPU's page table register.
156 /// @param[in] paddr The physical address containing the new pagetable.
157 //----------------------------------------------------------------------------
158 void
159 set_pagetable(uint64_t paddr);
160 
161 //-----------------------------------------------------------------------------
162 // @function invalidate_page
163 /// @brief Invalidate the page containing a virtual address.
164 /// @reg[in] vaddr The virtual address of the page to invalidate.
165 //-----------------------------------------------------------------------------
166 void
167 invalidate_page(void *vaddr);
168 
169 //----------------------------------------------------------------------------
170 // @function enable_interrupts
171 /// @brief Enable interrupts.
172 //----------------------------------------------------------------------------
173 void
175 
176 //----------------------------------------------------------------------------
177 // @function disable_interrupts
178 /// @brief Disable interrupts.
179 //----------------------------------------------------------------------------
180 void
182 
183 //----------------------------------------------------------------------------
184 // @function halt
185 /// @brief Halt the CPU until an interrupt occurs.
186 //----------------------------------------------------------------------------
187 void
188 halt();
189 
190 //----------------------------------------------------------------------------
191 // @function invalid_opcode
192 /// @brief Raise an invalid opcode exception.
193 //----------------------------------------------------------------------------
194 void
196 
197 //----------------------------------------------------------------------------
198 // @function fatal
199 /// @brief Raise a fatal interrupt that hangs the system.
200 //----------------------------------------------------------------------------
201 void
202 fatal();
203 #endif // __NO_INLINE__
204 
205 
206 #include "cpu_inl.h"
uint64_t rbx
Definition: cpu.h:64
__forceinline uint8_t io_inb(uint16_t port)
Definition: cpu_inl.h:47
__forceinline void io_outb(uint16_t port, uint8_t value)
Definition: cpu_inl.h:59
__forceinline uint64_t rdmsr(uint32_t id)
Definition: cpu_inl.h:27
__forceinline void halt()
Definition: cpu_inl.h:146
__forceinline void invalid_opcode()
Definition: cpu_inl.h:152
__forceinline void io_outd(uint16_t port, uint32_t value)
Definition: cpu_inl.h:103
__forceinline void set_pagetable(uint64_t paddr)
Definition: cpu_inl.h:113
uint64_t r14
Definition: cpu.h:53
__forceinline void wrmsr(uint32_t id, uint64_t value)
Definition: cpu_inl.h:38
Core include file.
__forceinline void enable_interrupts()
Definition: cpu_inl.h:134
A record describing all 64-bit general-purpose registers.
Definition: cpu.h:38
__forceinline void cpuid(uint32_t code, registers4_t *regs)
Definition: cpu_inl.h:17
uint64_t rdx
Definition: cpu.h:43
uint64_t rsi
Definition: cpu.h:44
uint64_t rcx
Definition: cpu.h:42
uint64_t rcx
Definition: cpu.h:65
__forceinline void fatal()
Definition: cpu_inl.h:158
__forceinline void disable_interrupts()
Definition: cpu_inl.h:140
uint64_t r10
Definition: cpu.h:49
uint64_t r15
Definition: cpu.h:54
uint64_t rbx
Definition: cpu.h:41
uint64_t r8
Definition: cpu.h:47
uint64_t rax
Definition: cpu.h:40
__forceinline uint16_t io_inw(uint16_t port)
Definition: cpu_inl.h:69
uint64_t r9
Definition: cpu.h:48
__forceinline uint32_t io_ind(uint16_t port)
Definition: cpu_inl.h:91
x86 CPU-specific function implementations – inline assembly.
uint64_t r12
Definition: cpu.h:51
__forceinline void invalidate_page(void *vaddr)
Definition: cpu_inl.h:124
uint64_t rax
Definition: cpu.h:63
uint64_t rbp
Definition: cpu.h:46
A record describing the first 4 general-purpose registers.
Definition: cpu.h:61
uint64_t rdi
Definition: cpu.h:45
uint64_t r13
Definition: cpu.h:52
uint64_t rdx
Definition: cpu.h:66
__forceinline void io_outw(uint16_t port, uint16_t value)
Definition: cpu_inl.h:81
uint64_t r11
Definition: cpu.h:50