Iros
 
Loading...
Searching...
No Matches
system_instructions.h
Go to the documentation of this file.
1#pragma once
2
3#include "di/assert/prelude.h"
4#include "di/types/prelude.h"
6
7namespace iris::x86::amd64 {
8struct [[gnu::packed]] IDTR {
11};
12
13static inline void load_idt(IDTR descriptor) {
14 asm("lidtq %0" : : "m"(descriptor));
15}
16
17struct [[gnu::packed]] GDTR {
20};
21
22static inline void load_gdt(GDTR descriptor) {
23 asm("lgdt %0" : : "m"(descriptor));
24}
25
26static inline void load_tr(u16 selector) {
27 asm("ltr %0" : : "m"(selector));
28}
29
30static inline auto read_cr0() -> u64 {
31 u64 cr0;
32 asm volatile("mov %%cr0, %0" : "=r"(cr0));
33 return cr0;
34}
35
36static inline void load_cr0(u64 cr0) {
37 asm volatile("mov %0, %%cr0" : : "r"(cr0));
38}
39
40static inline auto read_cr2() -> u64 {
41 u64 cr2;
42 asm volatile("mov %%cr2, %0" : "=r"(cr2));
43 return cr2;
44}
45
46static inline auto read_cr3() -> u64 {
47 u64 cr3;
48 asm volatile("mov %%cr3, %0" : "=r"(cr3));
49 return cr3;
50}
51
52static inline void load_cr3(u64 cr3) {
53 asm volatile("mov %0, %%cr3" : : "r"(cr3) : "memory");
54}
55
56static inline auto read_cr4() -> u64 {
57 u64 cr4;
58 asm volatile("mov %%cr4, %0" : "=r"(cr4));
59 return cr4;
60}
61
62static inline void load_cr4(u64 cr4) {
63 asm volatile("mov %0, %%cr4" : : "r"(cr4));
64}
65
69static inline void invlpg(mm::VirtualAddress address) {
70 asm volatile("invlpg (%0)" : : "r"(address.raw_value()) : "memory");
71}
72
74static inline void fninit() {
75 asm volatile("fninit");
76}
77
81static inline void fxsave(byte* state) {
82 ASSERT(reinterpret_cast<uptr>(state) % 16 == 0);
83 asm volatile("fxsave64 %0" : : "m"(*state));
84}
85
89static inline void fxrstor(byte* state) {
90 ASSERT(reinterpret_cast<uptr>(state) % 16 == 0);
91 asm volatile("fxrstor64 %0" : : "m"(*state));
92}
93
97static inline void xsave(byte* state) {
98 ASSERT(reinterpret_cast<uptr>(state) % 64 == 0);
99 asm volatile("xsave %0" ::"m"(*state));
100}
101
105static inline void xrstor(byte* state) {
106 ASSERT(reinterpret_cast<uptr>(state) % 64 == 0);
107 asm volatile("xrstor %0" : : "m"(*state));
108}
109
113static inline void xsetbv(u32 reg, u64 value) {
114 auto low = u32(value);
115 auto high = u32(value >> 32);
116 asm volatile("xsetbv" : : "a"(low), "c"(reg), "d"(high));
117}
118
122static inline void write_fs_base(u64 value) {
123 asm volatile("wrfsbase %0" : : "r"(value));
124}
125
129static inline void write_gs_base(u64 value) {
130 asm volatile("wrgsbase %0" : : "r"(value));
131}
132
137static inline void swapgs() {
138 asm volatile("swapgs" ::: "memory");
139}
140}
#define ASSERT
Definition assert_bool.h:16
constexpr auto value
Definition value.h:34
uintptr_t uptr
Definition integers.h:36
__UINT64_TYPE__ u64
Definition integers.h:12
__UINT32_TYPE__ u32
Definition integers.h:11
__UINT16_TYPE__ u16
Definition integers.h:10
di::StrongInt< VirtualAddressTag > VirtualAddress
Definition virtual_address.h:25
Definition gdt.cpp:38
Definition system_instructions.h:17
u64 virtual_address
Definition system_instructions.h:19
u16 size
Definition system_instructions.h:18
Definition system_instructions.h:8
u16 size
Definition system_instructions.h:9
u64 virtual_address
Definition system_instructions.h:10