This repository was archived by the owner on Nov 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcpu.hpp
1084 lines (995 loc) · 26.4 KB
/
cpu.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef __FENNIX_KERNEL_CPU_H__
#define __FENNIX_KERNEL_CPU_H__
#include <types.h>
#include <cpu/x86/cpuid_intel.hpp>
#include <cpu/x86/cpuid_amd.hpp>
#include <cpu/x86/x32/cr.hpp>
#include <cpu/x86/x32/msr.hpp>
#include <cpu/x86/x64/cr.hpp>
#include <cpu/x86/x64/msr.hpp>
#include <cpu/x86/exceptions.hpp>
#include <cpu/x86/interrupts.hpp>
#include <cpu/signatures.hpp>
#include <cpu/membar.hpp>
#include <cstring>
/**
* @brief CPU related functions.
*/
namespace CPU
{
/**
* @brief Enum for CPU::Interrupts() function.
*/
enum InterruptsType
{
/**
* @brief Check if interrupts are enabled.
*/
Check,
/**
* @brief Enable interrupts.
*/
Enable,
/**
* @brief Disable interrupts.
*/
Disable
};
enum x86SIMDType
{
SIMD_NONE = (1 << 0),
SIMD_SSE = (1 << 1),
SIMD_SSE2 = (1 << 2),
SIMD_SSE3 = (1 << 3),
SIMD_SSSE3 = (1 << 4),
SIMD_SSE41 = (1 << 5),
SIMD_SSE42 = (1 << 6),
SIMD_AVX = (1 << 7),
SIMD_AVX2 = (1 << 8),
SIMD_AVX512 = (1 << 9),
SIMD_AVX512F = (1 << 10),
SIMD_AVX512CD = (1 << 11),
SIMD_AVX512ER = (1 << 12),
SIMD_AVX512PF = (1 << 13),
SIMD_AVX512VL = (1 << 14),
SIMD_AVX512DQ = (1 << 16),
SIMD_AVX512BW = (1 << 15),
SIMD_AVX512IFMA = (1 << 17),
SIMD_AVX512VBMI = (1 << 18),
SIMD_AVX5124VNNIW = (1 << 19),
SIMD_AVX5124FMAPS = (1 << 20),
SIMD_AVX512VPOPCNTDQ = (1 << 21),
SIMD_AVX512VNNI = (1 << 22),
SIMD_AVX512VBMI2 = (1 << 23),
SIMD_AVX512BITALG = (1 << 24),
SIMD_AVX512VP2INTERSECT = (1 << 25),
SIMD_AVX512GFNI = (1 << 26),
SIMD_AVX512VPCLMULQDQ = (1 << 27),
SIMD_AVX512VAES = (1 << 28),
};
/**
* @brief Get CPU vendor identifier.
*
* @return CPU Vendor ID.
*/
const char *Vendor();
/**
* @brief Get CPU name.
*
* @return CPU Name.
*/
const char *Name();
/**
* @brief Get CPU hypervisor vendor.
*
* @return Hypervisor vendor.
*/
const char *Hypervisor();
/**
* @brief Check SIMD support. It will return the highest supported SIMD type.
*
* @return x86SIMDType flags.
*/
uint64_t CheckSIMD();
/**
* @brief Check SIMD support.
*
* @param Type SIMD type.
* @return true if supported.
* @return false if not supported.
*/
bool CheckSIMD(x86SIMDType Type);
/**
* @brief Pause the CPU
*/
nsa static __always_inline inline void Pause(bool Loop = false)
{
do
{
#if defined(a86)
asmv("pause");
#elif defined(aa64)
asmv("yield");
#endif
} while (Loop);
}
/**
* @brief Stop the CPU (infinite loop)
*/
nsa __noreturn __used inline void Stop()
{
#if defined(a86)
asmv("CPUStopLoop:\n"
"cli\n"
"hlt\n"
"jmp CPUStopLoop");
#elif defined(aa64)
asmv("CPUStopLoop:\n"
"cpsid i\n"
"wfe\n"
"wfi\n"
"b CPUStopLoop");
#endif
__builtin_unreachable();
}
/**
* @brief Halt the CPU
*/
nsa static __always_inline inline void Halt(bool Loop = false)
{
do
{
#if defined(a86)
asmv("hlt");
#elif defined(aa64)
asmv("wfe");
#endif
} while (Loop);
}
/**
* @brief Check if interrupts are enabled
*
* @return true If InterruptsType::Check and interrupts are enabled, or if other InterruptsType were executed successfully
* @return false If InterruptsType::Check and interrupts are disabled, or if other InterruptsType failed
*/
bool Interrupts(InterruptsType Type = Check);
/**
* @brief Get/Set the CPU's page table
*
* @param PT The new page table, if empty, the current page table will be returned
* @return Get: The current page table
* @return Set: The old page table
*/
void *PageTable(void *PT = nullptr);
#define thisPageTable (Memory::PageTable *)CPU::PageTable()
/** @brief To be used only once. */
void InitializeFeatures(int Core);
/** @brief Get CPU counter value. */
uint64_t Counter();
namespace x32
{
/**
* @brief MSR_APIC_BASE structure
* @see MSR_APIC_BASE
*/
typedef union
{
struct
{
/** @brief Reserved */
uint32_t Reserved0 : 8;
/**
* @brief BSP Flag
* @details If the BSP flag is set to 1, the processor is the bootstrap processor.
*/
uint32_t BSP : 1;
/** @brief Reserved */
uint32_t Reserved1 : 1;
/** @brief Enable x2APIC mode */
uint32_t EXTD : 1;
/** @brief APIC Global Enable */
uint32_t EN : 1;
/** @brief APIC Base Low Address */
uint32_t ApicBaseLo : 20;
/** @brief APIC Base High Address */
uint32_t ApicBaseHi : 32;
};
uint64_t raw;
} __packed APIC_BASE;
typedef union
{
struct
{
/** @brief Carry Flag */
uint32_t CF : 1;
/** @brief Reserved */
uint32_t AlwaysOne : 1;
/** @brief Parity Flag */
uint32_t PF : 1;
/** @brief Reserved */
uint32_t Reserved0 : 1;
/** @brief Auxiliary Carry Flag */
uint32_t AF : 1;
/** @brief Reserved */
uint32_t Reserved1 : 1;
/** @brief Zero Flag */
uint32_t ZF : 1;
/** @brief Sign Flag */
uint32_t SF : 1;
/** @brief Trap Flag */
uint32_t TF : 1;
/** @brief Interrupt Enable Flag */
uint32_t IF : 1;
/** @brief Direction Flag */
uint32_t DF : 1;
/** @brief Overflow Flag */
uint32_t OF : 1;
/** @brief I/O Privilege Level */
uint32_t IOPL : 2;
/** @brief Nested Task */
uint32_t NT : 1;
/** @brief Reserved */
uint32_t Reserved2 : 1;
/** @brief Resume Flag */
uint32_t RF : 1;
/** @brief Virtual 8086 Mode */
uint32_t VM : 1;
/** @brief Alignment Check */
uint32_t AC : 1;
/** @brief Virtual Interrupt Flag */
uint32_t VIF : 1;
/** @brief Virtual Interrupt Pending */
uint32_t VIP : 1;
/** @brief ID Flag */
uint32_t ID : 1;
};
uint32_t raw;
} EFLAGS;
struct TrapFrame
{
uint32_t edi; // Destination index for string operations
uint32_t esi; // Source index for string operations
uint32_t ebp; // Base Pointer (meant for stack frames)
uint32_t esp; // Stack Pointer
uint32_t ebx; // Base
uint32_t edx; // Data (commonly extends the A register)
uint32_t ecx; // Counter
uint32_t eax; // Accumulator
uint32_t InterruptNumber; // Interrupt Number
uint32_t ErrorCode; // Error code
uint32_t eip; // Instruction Pointer
uint32_t cs; // Code Segment
EFLAGS eflags; // Register Flags
uint32_t r3_esp; // Stack Pointer
uint32_t r3_ss; // Stack Segment
};
typedef union DR6
{
struct
{
/** @brief Breakpoint #0 Condition Detected */
uint64_t B0 : 1;
/** @brief Breakpoint #1 Condition Detected */
uint64_t B1 : 1;
/** @brief Breakpoint #2 Condition Detected */
uint64_t B2 : 1;
/** @brief Breakpoint #3 Condition Detected */
uint64_t B3 : 1;
/** @brief Reserved */
uint64_t Reserved0 : 8;
/** @brief Reserved */
uint64_t Reserved1 : 1;
/** @brief Breakpoint Debug Access Detected */
uint64_t BD : 1;
/** @brief Breakpoint Single Step */
uint64_t BS : 1;
/** @brief Breakpoint Task Switch */
uint64_t BT : 1;
/** @brief Reserved */
uint64_t Reserved2 : 15;
};
uint64_t raw;
} DR6;
typedef union DR7
{
struct
{
/** @brief Local Exact Breakpoint #0 Enabled */
uint32_t L0 : 1;
/** @brief Global Exact Breakpoint #0 Enabled */
uint32_t G0 : 1;
/** @brief Local Exact Breakpoint #1 Enabled */
uint32_t L1 : 1;
/** @brief Global Exact Breakpoint #1 Enabled */
uint32_t G1 : 1;
/** @brief Local Exact Breakpoint #2 Enabled */
uint32_t L2 : 1;
/** @brief Global Exact Breakpoint #2 Enabled */
uint32_t G2 : 1;
/** @brief Local Exact Breakpoint #3 Enabled */
uint32_t L3 : 1;
/** @brief Global Exact Breakpoint #3 Enabled */
uint32_t G3 : 1;
/** @brief Local Exact Breakpoint Enabled */
uint32_t LE : 1;
/** @brief Global Exact Breakpoint Enabled */
uint32_t GE : 1;
/** @brief Reserved */
uint32_t Reserved0 : 1;
/** @brief Reserved */
uint32_t Reserved1 : 2;
/** @brief General Detect Enabled */
uint32_t GD : 1;
/** @brief Reserved */
uint32_t Reserved2 : 2;
/** @brief Type of Transaction(s) to Trap */
uint32_t RW0 : 2;
/** @brief Length of Breakpoint #0 */
uint32_t LEN0 : 2;
/** @brief Type of Transaction(s) to Trap */
uint32_t RW1 : 2;
/** @brief Length of Breakpoint #1 */
uint32_t LEN1 : 2;
/** @brief Type of Transaction(s) to Trap */
uint32_t RW2 : 2;
/** @brief Length of Breakpoint #2 */
uint32_t LEN2 : 2;
/** @brief Type of Transaction(s) to Trap */
uint32_t RW3 : 2;
/** @brief Length of Breakpoint #3 */
uint32_t LEN3 : 2;
};
uint32_t raw;
} DR7;
struct FXState
{
/** @brief FPU control word */
uint16_t fcw;
/** @brief FPU status word */
uint16_t fsw;
/** @brief FPU tag words */
uint8_t ftw;
/** @brief Reserved (zero) */
uint8_t Reserved;
/** @brief FPU opcode */
uint16_t fop;
/** @brief PFU instruction pointer */
uint64_t rip;
/** @brief FPU data pointer */
uint64_t rdp;
/** @brief SSE control register */
uint32_t mxcsr;
/** @brief SSE control register mask */
uint32_t mxcsrmask;
/** @brief FPU registers (last 6 bytes reserved) */
uint8_t st[8][16];
/** @brief XMM registers */
uint8_t xmm[16][16];
} __packed;
/**
* @brief CPUID
*
* @param Function Leaf
* @param eax EAX
* @param ebx EBX
* @param ecx ECX
* @param edx EDX
*/
static inline void cpuid(uint32_t Function, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx)
{
#ifdef a32
asmv("cpuid"
: "=a"(*eax), "=b"(*ebx), "=c"(*ecx), "=d"(*edx)
: "a"(Function));
#else
UNUSED(Function);
UNUSED(eax);
UNUSED(ebx);
UNUSED(ecx);
UNUSED(edx);
#endif
}
nsa static inline void lgdt(void *gdt)
{
#ifdef a32
asmv("lgdt (%0)"
:
: "r"(gdt));
#else
UNUSED(gdt);
#endif
}
nsa static inline void lidt(void *idt)
{
#ifdef a32
asmv("lidt (%0)"
:
: "r"(idt));
#else
UNUSED(idt);
#endif
}
nsa static inline void ltr(uint16_t Segment)
{
#ifdef a32
asmv("ltr %0"
:
: "r"(Segment));
#else
UNUSED(Segment);
#endif
}
nsa static inline void invlpg(void *Address)
{
#ifdef a32
asmv("invlpg (%0)"
:
: "r"(Address)
: "memory");
#else
UNUSED(Address);
#endif
}
nsa static inline void fxsave(void *FXSaveArea)
{
#ifdef a32
if (!FXSaveArea)
return;
asmv("fxsave (%0)"
:
: "r"(FXSaveArea)
: "memory");
#else
UNUSED(FXSaveArea);
#endif
}
nsa static inline void fxrstor(void *FXRstorArea)
{
#ifdef a32
if (!FXRstorArea)
return;
asmv("fxrstor (%0)"
:
: "r"(FXRstorArea)
: "memory");
#else
UNUSED(FXRstorArea);
#endif
}
}
namespace x64
{
/**
* @brief MSR_APIC_BASE structure
* @see MSR_APIC_BASE
*/
typedef union
{
struct
{
/** Reserved */
uint64_t Reserved0 : 8;
/** Boot Strap CPU Core */
uint64_t BSC : 1;
/** Reserved */
uint64_t Reserved1 : 1;
/** x2APIC Mode Enable */
uint64_t EXTD : 1;
/** APIC Enable */
uint64_t AE : 1;
/** @brief APIC Base Low Address */
uint64_t ABALow : 20;
/** @brief APIC Base High Address */
uint64_t ABAHigh : 32;
/** Reserved */
uint64_t Reserved2 : 12;
};
uint64_t raw;
} __packed APIC_BASE;
typedef union
{
struct
{
uint64_t Reserved : 24;
uint64_t AID : 8;
};
uint32_t raw;
} __packed APIC_ID;
typedef union
{
struct
{
/** @brief Carry Flag */
uint64_t CF : 1;
/** @brief Reserved */
uint64_t AlwaysOne : 1;
/** @brief Parity Flag */
uint64_t PF : 1;
/** @brief Reserved */
uint64_t Reserved0 : 1;
/** @brief Auxiliary Carry Flag */
uint64_t AF : 1;
/** @brief Reserved */
uint64_t Reserved1 : 1;
/** @brief Zero Flag */
uint64_t ZF : 1;
/** @brief Sign Flag */
uint64_t SF : 1;
/** @brief Trap Flag */
uint64_t TF : 1;
/** @brief Interrupt Enable Flag */
uint64_t IF : 1;
/** @brief Direction Flag */
uint64_t DF : 1;
/** @brief Overflow Flag */
uint64_t OF : 1;
/** @brief I/O Privilege Level */
uint64_t IOPL : 2;
/** @brief Nested Task */
uint64_t NT : 1;
/** @brief Reserved */
uint64_t Reserved2 : 1;
/** @brief Resume Flag */
uint64_t RF : 1;
/** @brief Virtual 8086 Mode */
uint64_t VM : 1;
/** @brief Alignment Check */
uint64_t AC : 1;
/** @brief Virtual Interrupt Flag */
uint64_t VIF : 1;
/** @brief Virtual Interrupt Pending */
uint64_t VIP : 1;
/** @brief ID Flag (Allow using CPUID instruction) */
uint64_t ID : 1;
/** @brief Reserved */
uint64_t Reserved3 : 10;
};
uint64_t raw;
} RFLAGS;
struct TrapFrame
{
uint64_t r15; /* General purpose */
uint64_t r14; /* General purpose */
uint64_t r13; /* General purpose */
uint64_t r12; /* General purpose */
uint64_t r11; /* General purpose */
uint64_t r10; /* General purpose */
uint64_t r9; /* General purpose */
uint64_t r8; /* General purpose */
uint64_t rbp; /* Base Pointer (meant for stack frames) */
uint64_t rdi; /* Destination index for string operations */
uint64_t rsi; /* Source index for string operations */
uint64_t rdx; /* Data (commonly extends the A register) */
uint64_t rcx; /* Counter */
uint64_t rbx; /* Base */
uint64_t rax; /* Accumulator */
uint64_t InterruptNumber; /* Interrupt Number */
uint64_t ErrorCode; /* Error code */
uint64_t rip; /* Instruction Pointer */
uint64_t cs; /* Code Segment */
RFLAGS rflags; /* Register Flags */
uint64_t rsp; /* Stack Pointer */
uint64_t ss; /* Stack Segment */
};
struct SchedulerFrame
{
uint64_t ppt; /* Process Page Table */
uint64_t opt; /* Original Page Table */
uint64_t r15; /* General purpose */
uint64_t r14; /* General purpose */
uint64_t r13; /* General purpose */
uint64_t r12; /* General purpose */
uint64_t r11; /* General purpose */
uint64_t r10; /* General purpose */
uint64_t r9; /* General purpose */
uint64_t r8; /* General purpose */
uint64_t rbp; /* Base Pointer (meant for stack frames) */
uint64_t rdi; /* Destination index for string operations */
uint64_t rsi; /* Source index for string operations */
uint64_t rdx; /* Data (commonly extends the A register) */
uint64_t rcx; /* Counter */
uint64_t rbx; /* Base */
uint64_t rax; /* Accumulator */
uint64_t InterruptNumber; /* Interrupt Number */
uint64_t ErrorCode; /* Error code */
uint64_t rip; /* Instruction Pointer */
uint64_t cs; /* Code Segment */
RFLAGS rflags; /* Register Flags */
uint64_t rsp; /* Stack Pointer */
uint64_t ss; /* Stack Segment */
};
struct ExceptionFrame
{
uint64_t cr0; /* Control Register 0 (system control) */
uint64_t cr2; /* Control Register 2 (page fault linear address) */
uint64_t cr3; /* Control Register 3 (page directory base) */
uint64_t cr4; /* Control Register 4 (system control) */
uint64_t cr8; /* Control Register 8 (task priority) */
uint64_t dr0; /* Debug Register */
uint64_t dr1; /* Debug Register */
uint64_t dr2; /* Debug Register */
uint64_t dr3; /* Debug Register */
uint64_t dr6; /* Debug Register */
uint64_t dr7; /* Debug Register */
uint64_t gs; /* General purpose */
uint64_t fs; /* General purpose */
uint64_t es; /* Extra Segment */
uint64_t ds; /* Data Segment */
uint64_t r15; /* General purpose */
uint64_t r14; /* General purpose */
uint64_t r13; /* General purpose */
uint64_t r12; /* General purpose */
uint64_t r11; /* General purpose */
uint64_t r10; /* General purpose */
uint64_t r9; /* General purpose */
uint64_t r8; /* General purpose */
uint64_t rbp; /* Base Pointer (meant for stack frames) */
uint64_t rdi; /* Destination index for string operations */
uint64_t rsi; /* Source index for string operations */
uint64_t rdx; /* Data (commonly extends the A register) */
uint64_t rcx; /* Counter */
uint64_t rbx; /* Base */
uint64_t rax; /* Accumulator */
uint64_t InterruptNumber; /* Interrupt Number */
uint64_t ErrorCode; /* Error code */
uint64_t rip; /* Instruction Pointer */
uint64_t cs; /* Code Segment */
RFLAGS rflags; /* Register Flags */
uint64_t rsp; /* Stack Pointer */
uint64_t ss; /* Stack Segment */
};
typedef union EFER
{
struct
{
/** @brief Enable syscall & sysret instructions in 64-bit mode. */
uint64_t SCE : 1;
/** @brief Reserved */
uint64_t Reserved0 : 7;
/** @brief Enable long mode. */
uint64_t LME : 1;
/** @brief Reserved */
uint64_t Reserved1 : 1;
/** @brief Indicates long. */
uint64_t LMA : 1;
/** @brief Enable No-Execute Bit */
uint64_t NXE : 1;
/** @brief Enable Secure Virtual Machine */
uint64_t SVME : 1;
/** @brief Enable Long Mode Segment Limit */
uint64_t LMSLE : 1;
/** @brief Enable Fast FXSAVE/FXRSTOR */
uint64_t FFXSR : 1;
/** @brief Enable Translation Cache Extension */
uint64_t TCE : 1;
/** @brief Reserved */
uint64_t Reserved2 : 32;
};
uint64_t raw;
} __packed EFER;
typedef union DR6
{
struct
{
/** @brief Breakpoint #0 Condition Detected */
uint64_t B0 : 1;
/** @brief Breakpoint #1 Condition Detected */
uint64_t B1 : 1;
/** @brief Breakpoint #2 Condition Detected */
uint64_t B2 : 1;
/** @brief Breakpoint #3 Condition Detected */
uint64_t B3 : 1;
/** @brief Reserved */
uint64_t Reserved0 : 8;
/** @brief Reserved */
uint64_t Reserved1 : 1;
/** @brief Breakpoint Debug Access Detected */
uint64_t BD : 1;
/** @brief Breakpoint Single Step */
uint64_t BS : 1;
/** @brief Breakpoint Task Switch */
uint64_t BT : 1;
/** @brief Reserved */
uint64_t Reserved2 : 15;
/** @brief Reserved */
uint64_t Reserved3 : 32;
};
uint64_t raw;
} DR6;
typedef union DR7
{
struct
{
/** @brief Local Exact Breakpoint #0 Enabled */
uint64_t L0 : 1;
/** @brief Global Exact Breakpoint #0 Enabled */
uint64_t G0 : 1;
/** @brief Local Exact Breakpoint #1 Enabled */
uint64_t L1 : 1;
/** @brief Global Exact Breakpoint #1 Enabled */
uint64_t G1 : 1;
/** @brief Local Exact Breakpoint #2 Enabled */
uint64_t L2 : 1;
/** @brief Global Exact Breakpoint #2 Enabled */
uint64_t G2 : 1;
/** @brief Local Exact Breakpoint #3 Enabled */
uint64_t L3 : 1;
/** @brief Global Exact Breakpoint #3 Enabled */
uint64_t G3 : 1;
/** @brief Local Exact Breakpoint Enabled */
uint64_t LE : 1;
/** @brief Global Exact Breakpoint Enabled */
uint64_t GE : 1;
/** @brief Reserved */
uint64_t Reserved0 : 1;
/** @brief Reserved */
uint64_t Reserved1 : 2;
/** @brief General Detect Enabled */
uint64_t GD : 1;
/** @brief Reserved */
uint64_t Reserved2 : 2;
/** @brief Type of Transaction(s) to Trap */
uint64_t RW0 : 2;
/** @brief Length of Breakpoint #0 */
uint64_t LEN0 : 2;
/** @brief Type of Transaction(s) to Trap */
uint64_t RW1 : 2;
/** @brief Length of Breakpoint #1 */
uint64_t LEN1 : 2;
/** @brief Type of Transaction(s) to Trap */
uint64_t RW2 : 2;
/** @brief Length of Breakpoint #2 */
uint64_t LEN2 : 2;
/** @brief Type of Transaction(s) to Trap */
uint64_t RW3 : 2;
/** @brief Length of Breakpoint #3 */
uint64_t LEN3 : 2;
/** @brief Reserved */
uint64_t Reserved3 : 32;
};
uint64_t raw;
} DR7;
typedef union PageFaultErrorCode
{
struct
{
/** @brief When set, the page fault was caused by a page-protection violation. When not set, it was caused by a non-present page. */
uint64_t P : 1;
/** @brief When set, the page fault was caused by a write access. When not set, it was caused by a read access. */
uint64_t W : 1;
/** @brief When set, the page fault was caused while CPL = 3. This does not necessarily mean that the page fault was a privilege violation. */
uint64_t U : 1;
/** @brief When set, one or more page directory entries contain reserved bits which are set to 1. This only applies when the PSE or PAE flags in CR4 are set to 1. */
uint64_t R : 1;
/** @brief When set, the page fault was caused by an instruction fetch. This only applies when the No-Execute bit is supported and enabled. */
uint64_t I : 1;
/** @brief When set, the page fault was caused by a protection-key violation. The PKRU register (for user-mode accesses) or PKRS MSR (for supervisor-mode accesses) specifies the protection key rights. */
uint64_t PK : 1;
/** @brief When set, the page fault was caused by a shadow stack access. */
uint64_t SS : 1;
/** @brief Reserved */
uint64_t Reserved0 : 8;
/** @brief When set, the fault was due to an SGX violation. The fault is unrelated to ordinary paging. */
uint64_t SGX : 1;
/** @brief Reserved */
uint64_t Reserved1 : 16;
};
uint64_t raw;
} PageFaultErrorCode;
// ! TODO: UNTESTED!
typedef union SelectorErrorCode
{
struct
{
/** @brief When set, the exception originated externally to the processor. */
uint64_t External : 1;
/** @brief IDT/GDT/LDT Table
* @details 0b00 - The Selector Index references a descriptor in the GDT.
* @details 0b01 - The Selector Index references a descriptor in the IDT.
* @details 0b10 - The Selector Index references a descriptor in the LDT.
* @details 0b11 - The Selector Index references a descriptor in the IDT.
*/
uint64_t Table : 2;
/** @brief The index in the GDT, IDT or LDT. */
uint64_t Idx : 13;
/** @brief Reserved */
uint64_t Reserved : 16;
};
uint64_t raw;
} SelectorErrorCode;
struct FXState
{
/** @brief FPU control word */
uint16_t fcw;
/** @brief FPU status word */
uint16_t fsw;
/** @brief FPU tag words */
uint8_t ftw;
/** @brief Reserved (zero) */
uint8_t Reserved;
/** @brief FPU opcode */
uint16_t fop;
/** @brief PFU instruction pointer */
uint64_t rip;
/** @brief FPU data pointer */
uint64_t rdp;
/** @brief SSE control register */
uint32_t mxcsr;
/** @brief SSE control register mask */
uint32_t mxcsrmask;
/** @brief FPU registers (last 6 bytes reserved) */
uint8_t st[8][16];
/** @brief XMM registers */
uint8_t xmm[16][16];
} __packed __aligned(16);
nsa static inline void lgdt(void *gdt)
{
#ifdef a64
asmv("lgdt (%0)"
:
: "r"(gdt));
#endif
}
nsa static inline void lidt(void *idt)
{
#ifdef a64
asmv("lidt (%0)"
:
: "r"(idt));
#endif
}
nsa static inline void ltr(uint16_t Segment)
{
#ifdef a64
asmv("ltr %0"
:
: "r"(Segment));
#endif
}
nsa static inline void invlpg(void *Address)
{
#ifdef a64
asmv("invlpg (%0)"
:
: "r"(Address)
: "memory");
#endif
}
/**
* @brief CPUID
*
* @param Function Leaf
* @param eax EAX
* @param ebx EBX
* @param ecx ECX
* @param edx EDX
*/
nsa static inline void cpuid(uint32_t Function, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx)
{
#ifdef a64
asmv("cpuid"
: "=a"(*eax), "=b"(*ebx), "=c"(*ecx), "=d"(*edx)
: "a"(Function));
#endif
}
/**
* @brief Get the highest leaf function supported by CPUID
*
* @example if (GetHighestLeaf() < 0x15) { error("CPU doesn't support leaf 0x15!"); }
*
* @return uint32_t
*/
nsa static inline uint32_t GetHighestLeaf()
{
uint32_t eax, ebx, ecx, edx;
cpuid(0x0, &eax, &ebx, &ecx, &edx);
return eax;
}
nsa static inline void fxsave(void *FXSaveArea)
{
#ifdef a64
if (!FXSaveArea || FXSaveArea >= (char *)0xfffffffffffff000)
return;
asmv("fxsaveq (%0)"
:
: "r"(FXSaveArea)
: "memory");
#endif
}
nsa static inline void fxrstor(void *FXRstorArea)
{
#ifdef a64
if (!FXRstorArea || FXRstorArea >= (char *)0xfffffffffffff000)
return;