NeKernel dev
Loading...
Searching...
No Matches
Processor.h
Go to the documentation of this file.
1/* ========================================
2
3 Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license.
4
5 File: Prcoessor.h
6 Purpose: AMD64 processor abstraction.
7
8 Revision History:
9
10 30/01/24: Added file (amlel)
11
12======================================== */
13
14#pragma once
15
16#ifdef __NE_AMD64__
17
19#include <HALKit/AMD64/Paging.h>
20#include <NeKit/Array.h>
21#include <NeKit/Config.h>
22#include <NeKit/Utils.h>
23
24#include <HALKit/AMD64/CPUID.h>
25
26#define kPITControlPort (0x43)
27#define kPITChannel0Port (0x40)
28#define kPITFrequency (1193180)
29
30#define kPICCommand (0x20)
31#define kPICData (0x21)
32#define kPIC2Command (0xA0)
33#define kPIC2Data (0xA1)
34
35#define kIOAPICRegVal (4)
36#define kIOAPICRegReg (0)
37
38#define rtl_nop_op() asm volatile("nop")
39
41#define kKernelIdtSize (0x100)
42
44#define kKernelInterruptId (0x32)
45
46#define IsActiveLow(FLG) (FLG & 2)
47#define IsLevelTriggered(FLG) (FLG & 8)
48
49#define kInterruptGate (0x8E)
50#define kUserInterruptGate (0xEE)
51#define kTrapGate (0xEF)
52#define kTaskGate (0b10001100)
53#define kIDTSelector (0x08)
54
55namespace Kernel {
56namespace Detail::AMD64 {
57 struct PACKED InterruptDescriptorAMD64 final {
58 UInt16 OffsetLow; // offset bits 0..15
59 UInt16 Selector; // a code segment selector in GDT or LDT
60 UInt8 Ist;
61 UInt8 TypeAttributes;
62 UInt16 OffsetMid;
63 UInt32 OffsetHigh;
64 UInt32 Zero; // reserved
65 };
66} // namespace Detail::AMD64
67} // namespace Kernel
68
69namespace Kernel::HAL {
71enum {
72 kMMFlagsInvalid = 1 << 0,
73 kMMFlagsPresent = 1 << 1,
74 kMMFlagsWr = 1 << 2,
75 kMMFlagsUser = 1 << 3,
76 kMMFlagsNX = 1 << 4,
77 kMMFlagsPCD = 1 << 5,
78 kMMFlagsPwt = 1 << 6,
79 kMMFlagsCount = 6,
80};
81
82struct PACKED Register64 final {
83 UShort Limit;
84 UIntPtr Base;
85};
86
87using RawRegister = UInt64;
88using Reg = RawRegister;
89using InterruptId = UInt16; /* For each element in the IVT */
90
92struct PACKED StackFrame {
93 Reg IP;
94 Reg SP;
95 Reg R8;
96 Reg R9;
97 Reg R10;
98 Reg R11;
99 Reg R12;
100 Reg R13;
101 Reg R14;
102 Reg R15;
103};
104
106
107class InterruptDescriptor final {
108 public:
109 UShort Offset;
110 UShort Selector;
111 UChar Ist;
112 UChar Atrributes;
113
114 UShort SecondOffset;
115 UInt ThirdOffset;
116 UInt Zero;
117
118 operator bool() { return Offset != 0xFFFF; }
119};
120
121using InterruptDescriptorArray = Array<InterruptDescriptor, 256>;
122
123class SegmentDescriptor final {
124 public:
125 UInt16 Base;
126 UInt8 BaseMiddle;
127 UInt8 BaseHigh;
128
129 UShort Limit;
130 UChar Gran;
131 UChar AccessByte;
132};
133
134/***
135 * @brief Segment Boolean operations
136 */
137class SegmentDescriptorComparator final {
138 public:
139 Bool IsValid(SegmentDescriptor& seg) { return seg.Base > seg.Limit; }
140
141 Bool Equals(SegmentDescriptor& seg, SegmentDescriptor& segRight) {
142 return seg.Base == segRight.Base && seg.Limit == segRight.Limit;
143 }
144};
145
146using SegmentArray = Array<SegmentDescriptor, 6>;
147
148class GDTLoader final {
149 public:
150 static Void Load(Register64& gdt);
151 static Void Load(Ref<Register64>& gdt);
152};
153
154class IDTLoader final {
155 public:
156 static Void Load(Register64& idt);
157 static Void Load(Ref<Register64>& idt);
158};
159
160/***********************************************************************************/
163/***********************************************************************************/
164
166
167/***********************************************************************************/
170/***********************************************************************************/
171Void mp_init_cores(VoidPtr vendor_ptr);
172
173/***********************************************************************************/
177/***********************************************************************************/
179
180/***********************************************************************************/
185/***********************************************************************************/
186Void hal_get_msr(UInt32 msr, UInt32* lo, UInt32* hi);
187
192Void hal_set_msr(UInt32 msr, UInt32 lo, UInt32 hi);
193
195namespace Detail {
196 /* @brief TSS struct. */
197 struct NE_TSS final {
198 UInt32 fReserved1;
199 UInt64 fRsp0;
200 UInt64 fRsp1;
201 UInt64 fRsp2;
202 UInt64 fReserved2;
203 UInt64 fIst1;
204 UInt64 fIst2;
205 UInt64 fIst3;
206 UInt64 fIst4;
207 UInt64 fIst5;
208 UInt64 fIst6;
209 UInt64 fIst7;
210 UInt64 fReserved3;
211 UInt16 fReserved4;
212 UInt16 fIopb;
213 };
214
218
219 struct PACKED NE_GDT_ENTRY final {
220 UInt16 fLimitLow;
221 UInt16 fBaseLow;
222 UInt8 fBaseMid;
223 UInt8 fAccessByte;
224 UInt8 fFlags;
225 UInt8 fBaseHigh;
226 };
227} // namespace Detail
228
229class LAPICDmaWrapper final {
230 public:
231 explicit LAPICDmaWrapper(VoidPtr base);
232 ~LAPICDmaWrapper();
233
234 NE_COPY_DEFAULT(LAPICDmaWrapper)
235
236 public:
237 UInt32 Read(UInt16 reg);
238 Void Write(UInt16 reg, UInt32 value);
239
240 private:
241 VoidPtr fApic{nullptr};
242};
243
249EXTERN_C Int32 mm_map_page(VoidPtr virtual_address, VoidPtr physical_address, UInt32 flags,
250 UInt32 level = 2);
251
252EXTERN_C UInt8 rt_in8(UInt16 port);
253EXTERN_C UInt16 rt_in16(UInt16 port);
254EXTERN_C UInt32 rt_in32(UInt16 port);
255
256EXTERN_C Void rt_out16(UShort port, UShort byte);
257EXTERN_C Void rt_out8(UShort port, UChar byte);
258EXTERN_C Void rt_out32(UShort port, UInt byte);
259
260EXTERN_C Void rt_wait_400ns();
266
268
269EXTERN_C Int32 mm_memory_fence(VoidPtr virtual_address);
270} // namespace Kernel::HAL
271
276
277EXTERN_C ATTRIBUTE(naked) Kernel::Void hal_load_idt(Kernel::HAL::Register64 ptr);
278EXTERN_C ATTRIBUTE(naked) Kernel::Void hal_load_gdt(Kernel::HAL::Register64 ptr);
279
280inline Kernel::VoidPtr kKernelBitMpStart = nullptr;
281inline Kernel::UIntPtr kKernelBitMpSize = 0UL;
282
283#endif // __NE_AMD64__ */
EXTERN_C void idt_handle_pf(Kernel::UIntPtr rsp)
Handle page fault.
Definition HalCoreInterruptHandler.cc:47
EXTERN_C void idt_handle_generic(Kernel::UIntPtr rsp)
Handle any generic fault.
Definition HalCoreInterruptHandler.cc:89
EXTERN_C Kernel::Void idt_handle_gpf(Kernel::UIntPtr rsp)
Handle GPF fault.
Definition HalCoreInterruptHandler.cc:34
EXTERN_C void idt_handle_math(Kernel::UIntPtr rsp)
Handle math fault.
Definition HalCoreInterruptHandler.cc:75
#define NE_COPY_DEFAULT(KLASS)
Definition Detail.h:17
#define PACKED
#define ATTRIBUTE(...)
#define EXTERN_C
The handover boot protocol.
REF_TYPE * Ref
Definition System.h:28
void * VoidPtr
Definition Macros.h:59
Definition HalDescriptorLoader.cc:12
Hardware Abstraction Layer.
Definition ArchKit.h:62
UIntPtr Reg
Definition Processor.h:18
StackFrame * StackFramePtr
Definition Processor.h:34
Void hal_set_msr(UInt32 msr, UInt32 lo, UInt32 hi)
Definition HalProcessor.cc:33
Void rt_sti()
Definition HalProcessor.cc:78
Void rt_halt()
Definition HalProcessor.cc:70
Void hal_get_msr(UInt32 msr, UInt32 *lo, UInt32 *hi)
Definition HalProcessor.cc:28
Void rt_cli()
Definition HalProcessor.cc:74
EXTERN_C UIntPtr mm_get_page_addr(VoidPtr virt)
Gets a physical address from a virtual address.
Definition HalPagingMgr.cc:60
Void mp_init_cores(VoidPtr vendor_ptr)
Fetch and enable SMP scheduler.
Definition HalApplicationProcessor.cc:146
Void rt_std()
Definition HalProcessor.cc:86
Bool hal_has_msr()
Definition HalProcessor.cc:19
EXTERN_C Int32 mm_map_page(VoidPtr virtual_address, VoidPtr physical_address, UInt32 flags, UInt32 level)
Maps or allocates a page from virtual_address.
Definition HalPagingMgr.cc:120
EXTERN_C Int32 mm_memory_fence(VoidPtr virtual_address)
clflush+mfence helper function.
Definition HalPagingMgr.cc:104
Bool mp_is_smp(Void)
Is the current config SMP aware?
Definition HalApplicationProcessor.cc:137
Void rt_cld()
Definition HalProcessor.cc:82
UPS inline definitions.
Definition Device.h:12
void Void
Definition Config.h:87
__UINT8_TYPE__ UChar
Definition Config.h:54
__UINT16_TYPE__ UInt16
Definition Config.h:40
__UINT8_TYPE__ UInt8
Definition Config.h:55
__UINT32_TYPE__ UInt32
Definition Config.h:44
void * VoidPtr
Definition Config.h:33
__INT32_TYPE__ Int32
Definition Config.h:38
__UINT16_TYPE__ UShort
Definition Config.h:39
__UINT32_TYPE__ UInt
Definition Config.h:43
__UINTPTR_TYPE__ UIntPtr
Definition Config.h:62
__UINT64_TYPE__ UInt64
Definition Config.h:48
bool Bool
Definition Config.h:50
Stack frame (as retrieved from assembly.).
Definition Processor.h:21