summaryrefslogtreecommitdiffhomepage
path: root/dev/kernel/HALKit/AMD64/HalApplicationProcessor.cc
blob: 0791b28f72388a6fb453fa70d360b20a7b833aaf (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
/* -------------------------------------------

	Copyright (C) 2024-2025, Amlal EL Mahrouss, all rights reserved.

------------------------------------------- */

#include <modules/ACPI/ACPIFactoryInterface.h>
#include <KernelKit/ProcessScheduler.h>
#include <HALKit/AMD64/Processor.h>
#include <ArchKit/ArchKit.h>
#include <KernelKit/BinaryMutex.h>
#include <KernelKit/ProcessScheduler.h>
#include <KernelKit/Timer.h>
#include <modules/CoreGfx/TextMgr.h>
#include <NewKit/KernelPanic.h>
#include <KernelKit/HardwareThreadScheduler.h>

#define kApicSignature "APIC"

#define kAPIC_ICR_Low	  0x300
#define kAPIC_ICR_High	  0x310
#define kAPIC_SIPI_Vector 0x00500
#define kAPIC_EIPI_Vector 0x00400

#define kAPIC_BASE_MSR		  0x1B
#define kAPIC_BASE_MSR_BSP	  0x100
#define kAPIC_BASE_MSR_ENABLE 0x800

/// @note: _hal_switch_context is internal

///////////////////////////////////////////////////////////////////////////////////////

/// @brief The **HAL** namespace.

///////////////////////////////////////////////////////////////////////////////////////

namespace Kernel::HAL
{
	struct PROCESS_APIC_MADT;
	struct PROCESS_CONTROL_BLOCK;

	struct PROCESS_CONTROL_BLOCK final
	{
		HAL::StackFramePtr mFrame;
	};

	STATIC struct PROCESS_APIC_MADT* kMADTBlock = nullptr;
	STATIC Bool						 kSMPAware	= false;
	STATIC Int64					 kSMPCount	= 0;

	STATIC UIntPtr kApicBaseAddress = 0UL;

	STATIC Int32   kSMPInterrupt						   = 0;
	STATIC UInt64  kAPICLocales[kSchedProcessLimitPerTeam] = {0};
	STATIC VoidPtr kRawMADT								   = nullptr;

	/// @brief Multiple APIC Descriptor Table.
	struct PROCESS_APIC_MADT final SDT_OBJECT
	{
		UInt32 Address; // Madt address
		UInt8  Flags;	// Madt flags

		struct
		{
			UInt8 Type;
			UInt8 Len;

			union {
				struct
				{
					UInt8  IoID;
					UInt8  Zero;
					UInt32 IoAddress;
					UInt32 GISBase;
				} IOAPIC;

				struct
				{
					UInt8  Source;
					UInt8  IRQSource;
					UInt32 GSI;
					UInt16 Flags;
				} LApicNMI;

				struct
				{
					UInt8  ProcessorID;
					UInt16 Flags;
					UInt8  LINT;
				} LAPIC;

				struct
				{
					UInt16 Reserved;
					UInt64 Address;
				} LApicOverride;

				struct
				{
					UInt16 Reserved;
					UInt32 x2APICID;
					UInt32 Flags;
					UInt32 AcpiID;
				} LApic;
			};
		} List[]; // Records List
	};

	///////////////////////////////////////////////////////////////////////////////////////

	/***********************************************************************************/
	/// @brief Send IPI command to APIC.
	/// @param apic_id programmable interrupt controller id.
	/// @param vector vector interrupt.
	/// @param target target APIC adress.
	/// @return
	/***********************************************************************************/

	Void hal_send_start_ipi(UInt32 target, UInt32 apic_id)
	{
		Kernel::ke_dma_write<UInt32>(target, kAPIC_ICR_High, apic_id << 24);
		Kernel::ke_dma_write<UInt32>(target, kAPIC_ICR_Low, 0x00000500 | 0x00004000 | 0x00000000);

		while (Kernel::ke_dma_read<UInt32>(target, kAPIC_ICR_Low) & 0x1000)
		{
			;
		}
	}

	/***********************************************************************************/
	/// @brief Send end IPI for CPU.
	/// @param apic_id
	/// @param vector
	/// @param target
	/// @return
	/***********************************************************************************/
	Void hal_send_sipi(UInt32 target, UInt32 apic_id, UInt8 vector)
	{
		Kernel::ke_dma_write<UInt32>(target, kAPIC_ICR_High, apic_id << 24);
		Kernel::ke_dma_write<UInt32>(target, kAPIC_ICR_Low, 0x00000600 | 0x00004000 | 0x00000000 | vector);

		while (Kernel::ke_dma_read<UInt32>(target, kAPIC_ICR_Low) & 0x1000)
		{
			NE_UNUSED(0);
		}
	}

	STATIC PROCESS_CONTROL_BLOCK kProcessBlocks[kSchedProcessLimitPerTeam] = {0};

	EXTERN_C HAL::StackFramePtr mp_get_current_context(Int64 pid)
	{
		const auto process_index = pid % kSchedProcessLimitPerTeam;
		return kProcessBlocks[process_index].mFrame;
	}

	EXTERN_C BOOL mp_register_process(HAL::StackFramePtr stack_frame, ProcessID pid)
	{
		MUST_PASS(stack_frame);

		const auto process_index = pid % kSchedProcessLimitPerTeam;

		kProcessBlocks[process_index].mFrame = stack_frame;

		auto first_id = kAPICLocales[0];

		hal_send_sipi(kApicBaseAddress, first_id, (UInt8)(((UIntPtr)stack_frame->BP) >> 12));

		return YES;
	}

	/***********************************************************************************/
	/// @brief Is the current config SMP aware?
	/// @return True if YES, False if not.
	/***********************************************************************************/
	Bool mp_is_smp(Void) noexcept
	{
		return kSMPAware;
	}

	/***********************************************************************************/
	/// @brief Assembly symbol to bootstrap AP.
	/***********************************************************************************/
	EXTERN_C Char* hal_ap_blob_start;

	/***********************************************************************************/
	/// @brief Assembly symbol to bootstrap AP.
	/***********************************************************************************/
	EXTERN_C Char* hal_ap_blob_end;

	/***********************************************************************************/
	/// @brief Fetch and enable SMP scheduler.
	/// @param vendor_ptr SMP containing structure.
	/***********************************************************************************/
	Void mp_get_cores(VoidPtr vendor_ptr) noexcept
	{
		if (!vendor_ptr)
			return;

		if (!kHandoverHeader->f_HardwareTables.f_MultiProcessingEnabled)
		{
			kSMPAware = NO;
			return;
		}

		auto hw_and_pow_int = PowerFactoryInterface(vendor_ptr);
		kRawMADT			= hw_and_pow_int.Find(kApicSignature).Leak().Leak();

		kMADTBlock = reinterpret_cast<PROCESS_APIC_MADT*>(kRawMADT);
		kSMPAware  = NO;

		if (kMADTBlock)
		{
			SizeT index = 0;

			kSMPInterrupt = 0;
			kSMPCount	  = 0;

			kout << "SMP: Starting APs...\r";

			UInt32 eax, edx;
			kApicBaseAddress = kMADTBlock->Address;

			while (Yes)
			{
				if (kMADTBlock->List[index].Type > 9 ||
					kSMPCount > kSchedProcessLimitPerTeam)
					break;

				switch (kMADTBlock->List[index].Type)
				{
				case 0x00: {
					if (kMADTBlock->List[kSMPCount].LAPIC.ProcessorID < 1)
						break;

					kAPICLocales[kSMPCount] = kMADTBlock->List[kSMPCount].LAPIC.ProcessorID;
					kout << "SMP: APIC ID: " << number(kAPICLocales[kSMPCount]) << kendl;

					// I'll just make the AP start from scratch here.

					hal_send_start_ipi(kApicBaseAddress, kAPICLocales[kSMPCount]);

					HardwareTimer timer(Kernel::rtl_ms(10));
					timer.Wait();

					/// TODO: HAL helper to create an address.

					hal_send_sipi(kApicBaseAddress, kAPICLocales[kSMPCount], (UInt8)(((UIntPtr)hal_ap_blob_start) >> 12));

					++kSMPCount;
					break;
				}
				default:
					break;
				}

				++index;
			}

			kout << "SMP: number of APs: " << number(kSMPCount) << kendl;

			// Kernel is now SMP aware.
			// That means that the scheduler is now available (on MP Kernels)

			kSMPAware = true;

			/// TODO: Notify Boot AP that it must start.
		}
	}
} // namespace Kernel::HAL

///////////////////////////////////////////////////////////////////////////////////////