PatchworkOS
Loading...
Searching...
No Matches
regs.h
Go to the documentation of this file.
1#pragma once
2
3#include <stdint.h>
4
5#define XCR0_XSAVE_SAVE_X87 (1 << 0)
6#define XCR0_XSAVE_SAVE_SSE (1 << 1)
7#define XCR0_AVX_ENABLE (1 << 2)
8#define XCR0_AVX512_ENABLE (1 << 5)
9#define XCR0_ZMM0_15_ENABLE (1 << 6)
10#define XCR0_ZMM16_32_ENABLE (1 << 7)
11
12#define MSR_LAPIC 0x1B
13#define MSR_CPU_ID 0xC0000103 // IA32_TSC_AUX
14#define MSR_EFER 0xC0000080
15#define MSR_STAR 0xC0000081
16#define MSR_LSTAR 0xC0000082
17#define MSR_SYSCALL_FLAG_MASK 0xC0000084
18#define MSR_GS_BASE 0xC0000101
19#define MSR_KERNEL_GS_BASE 0xc0000102
20
21#define EFER_SYSCALL_ENABLE 1
22
23#define RFLAGS_CARRY (1 << 0)
24#define RFLAGS_ALWAYS_SET (1 << 1)
25#define RFLAGS_PARITY (1 << 2)
26#define RFLAGS_RESERVED1 (1 << 3)
27#define RFLAGS_AUX_CARRY (1 << 4)
28#define RFLAGS_RESERVED2 (1 << 5)
29#define RFLAGS_ZERO (1 << 6)
30#define RFLAGS_SIGN (1 << 7)
31#define RFLAGS_TRAP (1 << 8)
32#define RFLAGS_INTERRUPT_ENABLE (1 << 9)
33#define RFLAGS_DIRECTION (1 << 10)
34#define RFLAGS_OVERFLOW (1 << 11)
35#define RFLAGS_IOPL (1 << 12 | 1 << 13)
36#define RFLAGS_NESTED_TASK (1 << 14)
37#define RFLAGS_MODE (1 << 15)
38
39#define CR0_PROTECTED_MODE_ENABLE (1 << 0)
40#define CR0_MONITOR_CO_PROCESSOR (1 << 1)
41#define CR0_EMULATION (1 << 2)
42#define CR0_TASK_SWITCHED (1 << 3)
43#define CR0_EXTENSION_TYPE (1 << 4)
44#define CR0_NUMERIC_ERROR_ENABLE (1 << 5)
45#define CR0_WRITE_PROTECT (1 << 16)
46#define CR0_ALIGNMENT_MASK (1 << 18)
47#define CR0_NOT_WRITE_THROUGH (1 << 29)
48#define CR0_CACHE_DISABLE (1 << 30)
49#define CR0_PAGING_ENABLE (1 << 31)
50
51#define CR4_PAGE_GLOBAL_ENABLE (1 << 7)
52#define CR4_FXSR_ENABLE (1 << 9)
53#define CR4_SIMD_EXCEPTION (1 << 10)
54#define CR4_XSAVE_ENABLE (1 << 18)
55
56static inline void xcr0_write(uint32_t xcr, uint64_t value)
57{
58 uint32_t eax = (uint32_t)value;
59 uint32_t edx = value >> 32;
60 asm volatile("xsetbv" : : "a"(eax), "d"(edx), "c"(xcr) : "memory");
61}
62
63static inline uint64_t msr_read(uint32_t msr)
64{
65 uint32_t low;
66 uint32_t high;
67 asm volatile("rdmsr" : "=a"(low), "=d"(high) : "c"(msr));
68 return ((uint64_t)high << 32) | (uint64_t)low;
69}
70
71static inline void msr_write(uint32_t msr, uint64_t value)
72{
73 uint32_t low = (uint32_t)(value & 0xFFFFFFFF);
74 uint32_t high = (uint32_t)(value >> 32);
75 asm volatile("wrmsr" : : "c"(msr), "a"(low), "d"(high));
76}
77
78static inline uint64_t rflags_read()
79{
80 uint64_t rflags;
81 asm volatile("pushfq; pop %0" : "=r"(rflags));
82 return rflags;
83}
84
85static inline void rflags_write(uint64_t value)
86{
87 asm volatile("push %0; popfq" : : "r"(value));
88}
89
90static inline uint64_t cr4_read()
91{
92 uint64_t cr4;
93 asm volatile("mov %%cr4, %0" : "=r"(cr4));
94 return cr4;
95}
96
97static inline void cr4_write(uint64_t value)
98{
99 asm volatile("mov %0, %%cr4" : : "r"(value));
100}
101
102static inline uint64_t cr3_read()
103{
104 uint64_t cr3;
105 asm volatile("mov %%cr3, %0" : "=r"(cr3));
106 return cr3;
107}
108
109static inline void cr3_write(uint64_t value)
110{
111 asm volatile("mov %0, %%cr3" : : "r"(value));
112}
113
114static inline uint64_t cr2_read()
115{
116 uint64_t cr2;
117 asm volatile("mov %%cr2, %0" : "=r"(cr2));
118 return cr2;
119}
120
121static inline void cr2_write(uint64_t value)
122{
123 asm volatile("mov %0, %%cr2" : : "r"(value));
124}
125
126static inline uint64_t cr0_read()
127{
128 uint64_t cr0;
129 asm volatile("mov %%cr0, %0" : "=r"(cr0));
130 return cr0;
131}
132
133static inline void cr0_write(uint64_t value)
134{
135 asm volatile("mov %0, %%cr0" : : "r"(value));
136}
137
138static inline uint64_t rsp_read()
139{
140 uint64_t rsp;
141 asm volatile("mov %%rsp, %0" : "=r"(rsp));
142 return rsp;
143}
144
145static inline void rsp_write(uint64_t value)
146{
147 asm volatile("mov %0, %%rsp" : : "r"(value));
148}
149
150static inline uint64_t rbp_read()
151{
152 uint64_t rbp;
153 asm volatile("mov %%rbp, %0" : "=r"(rbp));
154 return rbp;
155}
156
157static inline void rbp_write(uint64_t value)
158{
159 asm volatile("mov %0, %%rbp" : : "r"(value));
160}
static void cr3_write(uint64_t value)
Definition regs.h:109
static uint64_t rbp_read()
Definition regs.h:150
static void cr2_write(uint64_t value)
Definition regs.h:121
static void msr_write(uint32_t msr, uint64_t value)
Definition regs.h:71
static void rflags_write(uint64_t value)
Definition regs.h:85
static uint64_t cr2_read()
Definition regs.h:114
static uint64_t msr_read(uint32_t msr)
Definition regs.h:63
static void rbp_write(uint64_t value)
Definition regs.h:157
static uint64_t cr3_read()
Definition regs.h:102
static void rsp_write(uint64_t value)
Definition regs.h:145
static void cr4_write(uint64_t value)
Definition regs.h:97
static void cr0_write(uint64_t value)
Definition regs.h:133
static uint64_t rsp_read()
Definition regs.h:138
static void xcr0_write(uint32_t xcr, uint64_t value)
Definition regs.h:56
static uint64_t rflags_read()
Definition regs.h:78
static uint64_t cr0_read()
Definition regs.h:126
static uint64_t cr4_read()
Definition regs.h:90
__UINT32_TYPE__ uint32_t
Definition stdint.h:15
__UINT64_TYPE__ uint64_t
Definition stdint.h:17