summaryrefslogtreecommitdiffhomepage
path: root/src/kernel/HALKit/AMD64/Processor.h
blob: ec94a3f161c82739361e1ade3c04e5200413b599 (plain)
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
/* ========================================

  Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license.

  File: Prcoessor.h
  Purpose: AMD64 processor abstraction.

  Revision History:

  30/01/24: Added file (amlel)

======================================== */

#pragma once

#ifdef __NE_AMD64__

#include <FirmwareKit/Handover.h>
#include <HALKit/AMD64/Paging.h>
#include <NeKit/Array.h>
#include <NeKit/Defines.h>
#include <NeKit/Utils.h>

#include <HALKit/AMD64/CPUID.h>

#define kPITControlPort (0x43)
#define kPITChannel0Port (0x40)
#define kPITFrequency (1193180)

#define kPICCommand (0x20)
#define kPICData (0x21)
#define kPIC2Command (0xA0)
#define kPIC2Data (0xA1)

#define kIOAPICRegVal (4)
#define kIOAPICRegReg (0)

#define rtl_nop_op() asm volatile("nop")

/// @brief Maximum entries of the interrupt descriptor table.
#define kKernelIdtSize (0x100)

/// @brief interrupt for system call.
#define kKernelInterruptId (0x32)

#define IsActiveLow(FLG) (FLG & 2)
#define IsLevelTriggered(FLG) (FLG & 8)

#define kInterruptGate (0x8E)
#define kUserInterruptGate (0xEE)
#define kTrapGate (0xEF)
#define kTaskGate (0b10001100)
#define kIDTSelector (0x08)

namespace Kernel {
namespace Detail::AMD64 {
  struct PACKED InterruptDescriptorAMD64 final {
    UInt16 OffsetLow;  // offset bits 0..15
    UInt16 Selector;   // a code segment selector in GDT or LDT
    UInt8  Ist;
    UInt8  TypeAttributes;
    UInt16 OffsetMid;
    UInt32 OffsetHigh;
    UInt32 Zero;  // reserved
  };
}  // namespace Detail::AMD64
}  // namespace Kernel

namespace Kernel::HAL {
/// @brief Memory Manager mapping flags.
enum {
  kMMFlagsInvalid = 1 << 0,
  kMMFlagsPresent = 1 << 1,
  kMMFlagsWr      = 1 << 2,
  kMMFlagsUser    = 1 << 3,
  kMMFlagsNX      = 1 << 4,
  kMMFlagsPCD     = 1 << 5,
  kMMFlagsPwt     = 1 << 6,
  kMMFlagsCount   = 6,
};

struct PACKED Register64 final {
  UShort  Limit;
  UIntPtr Base;
};

using RawRegister = UInt64;
using Reg         = RawRegister;
using InterruptId = UInt16; /* For each element in the IVT */

/// @brief Stack frame (as retrieved from assembly.)
struct PACKED StackFrame {
  Reg IP;
  Reg SP;
  Reg R8;
  Reg R9;
  Reg R10;
  Reg R11;
  Reg R12;
  Reg R13;
  Reg R14;
  Reg R15;
};

typedef StackFrame* StackFramePtr;

class InterruptDescriptor final {
 public:
  UShort Offset;
  UShort Selector;
  UChar  Ist;
  UChar  Atrributes;

  UShort SecondOffset;
  UInt   ThirdOffset;
  UInt   Zero;

  operator bool() { return Offset != 0xFFFF; }
};

using InterruptDescriptorArray = Array<InterruptDescriptor, 256>;

class SegmentDescriptor final {
 public:
  UInt16 Base;
  UInt8  BaseMiddle;
  UInt8  BaseHigh;

  UShort Limit;
  UChar  Gran;
  UChar  AccessByte;
};

/***
 * @brief Segment Boolean operations
 */
class SegmentDescriptorComparator final {
 public:
  Bool IsValid(SegmentDescriptor& seg) { return seg.Base > seg.Limit; }

  Bool Equals(SegmentDescriptor& seg, SegmentDescriptor& segRight) {
    return seg.Base == segRight.Base && seg.Limit == segRight.Limit;
  }
};

using SegmentArray = Array<SegmentDescriptor, 6>;

class GDTLoader final {
 public:
  static Void Load(Register64& gdt);
  static Void Load(Ref<Register64>& gdt);
};

class IDTLoader final {
 public:
  static Void Load(Register64& idt);
  static Void Load(Ref<Register64>& idt);
};

/***********************************************************************************/
/// @brief Is the current config SMP aware?
/// @return True if YES, False if not.
/***********************************************************************************/

Bool mp_is_smp(Void);

/***********************************************************************************/
/// @brief Fetch and enable SMP scheduler.
/// @param vendor_ptr SMP containing structure.
/***********************************************************************************/
Void mp_init_cores(VoidPtr vendor_ptr);

/***********************************************************************************/
/// @brief Do a cpuid to check if MSR exists on CPU.
/// @retval true it does exists.
/// @retval false it doesn't.
/***********************************************************************************/
Bool hal_has_msr();

/***********************************************************************************/
/// @brief Get Model specific register inside core.
/// @param msr MSR
/// @param lo low byte
/// @param hi high byte
/***********************************************************************************/
Void hal_get_msr(UInt32 msr, UInt32* lo, UInt32* hi);

/// @brief Set Model-specific register.
/// @param msr MSR
/// @param lo low byte
/// @param hi high byte
Void hal_set_msr(UInt32 msr, UInt32 lo, UInt32 hi);

/// @brief Processor specific namespace.
namespace Detail {
  /* @brief TSS struct. */
  struct NE_TSS final {
    UInt32 fReserved1;
    UInt64 fRsp0;
    UInt64 fRsp1;
    UInt64 fRsp2;
    UInt64 fReserved2;
    UInt64 fIst1;
    UInt64 fIst2;
    UInt64 fIst3;
    UInt64 fIst4;
    UInt64 fIst5;
    UInt64 fIst6;
    UInt64 fIst7;
    UInt64 fReserved3;
    UInt16 fReserved4;
    UInt16 fIopb;
  };

  /**
    @brief Global descriptor table entry, either null, code or data.
  */

  struct PACKED NE_GDT_ENTRY final {
    UInt16 fLimitLow;
    UInt16 fBaseLow;
    UInt8  fBaseMid;
    UInt8  fAccessByte;
    UInt8  fFlags;
    UInt8  fBaseHigh;
  };
}  // namespace Detail

class LAPICDmaWrapper final {
 public:
  explicit LAPICDmaWrapper(VoidPtr base);
  ~LAPICDmaWrapper();

  NE_COPY_DEFAULT(LAPICDmaWrapper)

 public:
  UInt32 Read(UInt16 reg);
  Void   Write(UInt16 reg, UInt32 value);

 private:
  VoidPtr fApic{nullptr};
};

/// @brief Set a PTE from pd_base.
/// @param virt_addr a valid virtual address.
/// @param phys_addr point to physical address.
/// @param flags the flags to put on the page.
/// @return Status code of page manip.
EXTERN_C Int32 mm_map_page(VoidPtr virtual_address, VoidPtr physical_address, UInt32 flags,
                           UInt32 level = 2);

EXTERN_C UInt8  rt_in8(UInt16 port);
EXTERN_C UInt16 rt_in16(UInt16 port);
EXTERN_C UInt32 rt_in32(UInt16 port);

EXTERN_C Void rt_out16(UShort port, UShort byte);
EXTERN_C Void rt_out8(UShort port, UChar byte);
EXTERN_C Void rt_out32(UShort port, UInt byte);

EXTERN_C Void rt_wait_400ns();
EXTERN_C Void rt_halt();
EXTERN_C Void rt_cli();
EXTERN_C Void rt_sti();
EXTERN_C Void rt_cld();
EXTERN_C Void rt_std();

EXTERN_C UIntPtr mm_get_page_addr(VoidPtr virtual_address);

EXTERN_C Int32 mm_memory_fence(VoidPtr virtual_address);
}  // namespace Kernel::HAL

EXTERN_C Kernel::Void idt_handle_generic(Kernel::UIntPtr rsp);
EXTERN_C Kernel::Void idt_handle_gpf(Kernel::UIntPtr rsp);
EXTERN_C Kernel::Void idt_handle_math(Kernel::UIntPtr rsp);
EXTERN_C Kernel::Void idt_handle_pf(Kernel::UIntPtr rsp);

EXTERN_C ATTRIBUTE(naked) Kernel::Void hal_load_idt(Kernel::HAL::Register64 ptr);
EXTERN_C ATTRIBUTE(naked) Kernel::Void hal_load_gdt(Kernel::HAL::Register64 ptr);

inline Kernel::VoidPtr kKernelBitMpStart = nullptr;
inline Kernel::UIntPtr kKernelBitMpSize  = 0UL;

#endif  // __NE_AMD64__ */