ReactOS 0.4.15-dev-7961-gdcf9eb0
mproc.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4 * PURPOSE: Architecture specific source file to hold multiprocessor functions
5 * COPYRIGHT: Copyright 2023 Justin Miller <justin.miller@reactos.org>
6 * Copyright 2023 Victor Perevertkin <victor.perevertkin@reactos.org>
7 */
8
9/* INCLUDES *****************************************************************/
10
11#include <ntoskrnl.h>
12#define NDEBUG
13#include <debug.h>
14
15typedef struct _APINFO
16{
20 KIPCR Pcr;
22 KTSS Tss;
23 KTSS TssDoubleFault;
24 KTSS TssNMI;
26
27typedef struct _AP_SETUP_STACK
28{
31} AP_SETUP_STACK, *PAP_SETUP_STACK; // Note: expected layout only for 32-bit x86
32
33/* FUNCTIONS *****************************************************************/
34
35CODE_SEG("INIT")
36VOID
39{
40 PVOID KernelStack, DPCStack;
42 PAPINFO APInfo;
43
44 while (TRUE)
45 {
47 KernelStack = NULL;
48 DPCStack = NULL;
49
50 // Allocate structures for a new CPU.
51 APInfo = ExAllocatePoolZero(NonPagedPool, sizeof(*APInfo), TAG_KERNEL);
52 if (!APInfo)
53 break;
54 ASSERT(ALIGN_DOWN_POINTER_BY(APInfo, PAGE_SIZE) == APInfo);
55
56 KernelStack = MmCreateKernelStack(FALSE, 0);
57 if (!KernelStack)
58 break;
59
60 DPCStack = MmCreateKernelStack(FALSE, 0);
61 if (!DPCStack)
62 break;
63
64 // Initalize a new PCR for the specific AP
66 &APInfo->Pcr,
67 &APInfo->Idt[0],
68 &APInfo->Gdt[0],
69 &APInfo->Tss,
70 (PKTHREAD)&APInfo->Thread,
71 DPCStack);
72
73 // Prepare descriptor tables
74 KDESCRIPTOR bspGdt, bspIdt;
75 __sgdt(&bspGdt.Limit);
76 __sidt(&bspIdt.Limit);
77 RtlCopyMemory(&APInfo->Gdt, (PVOID)bspGdt.Base, bspGdt.Limit + 1);
78 RtlCopyMemory(&APInfo->Idt, (PVOID)bspIdt.Base, bspIdt.Limit + 1);
79
80 KiSetGdtDescriptorBase(KiGetGdtEntry(&APInfo->Gdt, KGDT_R0_PCR), (ULONG_PTR)&APInfo->Pcr);
81 KiSetGdtDescriptorBase(KiGetGdtEntry(&APInfo->Gdt, KGDT_DF_TSS), (ULONG_PTR)&APInfo->TssDoubleFault);
82 KiSetGdtDescriptorBase(KiGetGdtEntry(&APInfo->Gdt, KGDT_NMI_TSS), (ULONG_PTR)&APInfo->TssNMI);
83
84 KiSetGdtDescriptorBase(KiGetGdtEntry(&APInfo->Gdt, KGDT_TSS), (ULONG_PTR)&APInfo->Tss);
85 // Clear TSS Busy flag (aka set the type to "TSS (Available)")
86 KiGetGdtEntry(&APInfo->Gdt, KGDT_TSS)->HighWord.Bits.Type = I386_TSS;
87
88 APInfo->TssDoubleFault.Esp0 = (ULONG_PTR)&APInfo->NMIStackData;
89 APInfo->TssDoubleFault.Esp = (ULONG_PTR)&APInfo->NMIStackData;
90
91 APInfo->TssNMI.Esp0 = (ULONG_PTR)&APInfo->NMIStackData;
92 APInfo->TssNMI.Esp = (ULONG_PTR)&APInfo->NMIStackData;
93
94 // Fill the processor state
95 PKPROCESSOR_STATE ProcessorState = &APInfo->Pcr.Prcb->ProcessorState;
96 RtlZeroMemory(ProcessorState, sizeof(*ProcessorState));
97
98 ProcessorState->SpecialRegisters.Cr0 = __readcr0();
99 ProcessorState->SpecialRegisters.Cr3 = __readcr3();
100 ProcessorState->SpecialRegisters.Cr4 = __readcr4();
101
102 ProcessorState->ContextFrame.SegCs = KGDT_R0_CODE;
103 ProcessorState->ContextFrame.SegDs = KGDT_R3_DATA;
104 ProcessorState->ContextFrame.SegEs = KGDT_R3_DATA;
105 ProcessorState->ContextFrame.SegSs = KGDT_R0_DATA;
106 ProcessorState->ContextFrame.SegFs = KGDT_R0_PCR;
107
108 ProcessorState->SpecialRegisters.Gdtr.Base = (ULONG_PTR)APInfo->Gdt;
109 ProcessorState->SpecialRegisters.Gdtr.Limit = sizeof(APInfo->Gdt) - 1;
110 ProcessorState->SpecialRegisters.Idtr.Base = (ULONG_PTR)APInfo->Idt;
111 ProcessorState->SpecialRegisters.Idtr.Limit = sizeof(APInfo->Idt) - 1;
112
113 ProcessorState->SpecialRegisters.Tr = KGDT_TSS;
114
115 ProcessorState->ContextFrame.Esp = (ULONG_PTR)KernelStack;
116 ProcessorState->ContextFrame.Eip = (ULONG_PTR)KiSystemStartup;
117 ProcessorState->ContextFrame.EFlags = __readeflags() & ~EFLAGS_INTERRUPT_MASK;
118
119 ProcessorState->ContextFrame.Esp = (ULONG)((ULONG_PTR)ProcessorState->ContextFrame.Esp - sizeof(AP_SETUP_STACK));
120 PAP_SETUP_STACK ApStack = (PAP_SETUP_STACK)ProcessorState->ContextFrame.Esp;
121 ApStack->KxLoaderBlock = KeLoaderBlock;
122 ApStack->ReturnAddr = NULL;
123
124 // Update the LOADER_PARAMETER_BLOCK structure for the new processor
125 KeLoaderBlock->KernelStack = (ULONG_PTR)KernelStack;
126 KeLoaderBlock->Prcb = (ULONG_PTR)&APInfo->Pcr.Prcb;
127 KeLoaderBlock->Thread = (ULONG_PTR)&APInfo->Pcr.Prcb->IdleThread;
128
129 // Start the CPU
130 DPRINT("Attempting to Start a CPU with number: %u\n", ProcessorCount);
131 if (!HalStartNextProcessor(KeLoaderBlock, ProcessorState))
132 {
133 break;
134 }
135
136 // And wait for it to start
137 while (KeLoaderBlock->Prcb != 0)
138 {
139 //TODO: Add a time out so we don't wait forever
142 }
143 }
144
145 // The last CPU didn't start - clean the data
147
148 if (APInfo)
150 if (KernelStack)
151 MmDeleteKernelStack(KernelStack, FALSE);
152 if (DPCStack)
153 MmDeleteKernelStack(DPCStack, FALSE);
154
155 DPRINT1("KeStartAllProcessors: Successful AP startup count is %u\n", ProcessorCount);
156}
#define CODE_SEG(...)
unsigned char UINT8
FORCEINLINE PKGDTENTRY64 KiGetGdtEntry(PVOID pGdt, USHORT Selector)
Definition: intrin_i.h:13
FORCEINLINE VOID KiSetGdtDescriptorBase(PKGDTENTRY Entry, ULONG64 Base)
Definition: intrin_i.h:30
VOID NTAPI KeStartAllProcessors(VOID)
Definition: mproc.c:20
#define DPRINT1
Definition: precomp.h:8
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
int ProcessorCount
Definition: bus.c:58
#define ULONG_PTR
Definition: config.h:101
#define PAGE_SIZE
Definition: env_spec_w32.h:49
#define NonPagedPool
Definition: env_spec_w32.h:307
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2653
BOOLEAN NTAPI HalStartNextProcessor(IN PLOADER_PARAMETER_BLOCK LoaderBlock, IN PKPROCESSOR_STATE ProcessorState)
Definition: processor.c:71
struct _AP_SETUP_STACK AP_SETUP_STACK
struct _APINFO * PAPINFO
struct _APINFO APINFO
struct _AP_SETUP_STACK * PAP_SETUP_STACK
__INTRIN_INLINE unsigned long __readcr3(void)
Definition: intrin_x86.h:1818
__INTRIN_INLINE unsigned long __readcr4(void)
Definition: intrin_x86.h:1825
__INTRIN_INLINE unsigned long __readcr0(void)
Definition: intrin_x86.h:1804
__INTRIN_INLINE uintptr_t __readeflags(void)
Definition: intrin_x86.h:1674
__INTRIN_INLINE void __sidt(void *Destination)
Definition: intrin_x86.h:2023
PLOADER_PARAMETER_BLOCK KeLoaderBlock
Definition: krnlinit.c:29
#define ASSERT(a)
Definition: mode.c:44
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1109
#define I386_TSS
Definition: ketypes.h:121
#define DOUBLE_FAULT_STACK_SIZE
Definition: ketypes.h:97
#define KGDT_R3_DATA
Definition: ketypes.h:126
#define KGDT_NMI_TSS
Definition: ketypes.h:133
#define KGDT_TSS
Definition: ketypes.h:127
#define KGDT_R0_PCR
Definition: ketypes.h:128
#define KGDT_DF_TSS
Definition: ketypes.h:132
#define KGDT_R0_CODE
Definition: ketypes.h:123
#define KGDT_R0_DATA
Definition: ketypes.h:124
#define DECLSPEC_ALIGN(x)
Definition: ntbasedef.h:251
VOID NTAPI KiInitializePcr(IN ULONG ProcessorNumber, IN PKIPCR Pcr, IN PKIDTENTRY Idt, IN PKGDTENTRY Gdt, IN PKTSS Tss, IN PKTHREAD IdleThread, IN PVOID DpcStack)
Definition: kiinit.c:284
DECLSPEC_NORETURN VOID NTAPI KiSystemStartup(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
Definition: kiinit.c:476
VOID NTAPI MmDeleteKernelStack(PVOID Stack, BOOLEAN GuiStack)
PVOID NTAPI MmCreateKernelStack(BOOLEAN GuiStack, UCHAR Node)
#define YieldProcessor
Definition: ke.h:48
FORCEINLINE VOID KeMemoryBarrier(VOID)
Definition: ke.h:58
#define DPRINT
Definition: sndvol32.h:71
Definition: mproc.c:16
PVOID ReturnAddr
Definition: mproc.c:29
PVOID KxLoaderBlock
Definition: mproc.c:30
ULONG Esp
Definition: nt_native.h:1479
ULONG SegFs
Definition: nt_native.h:1454
ULONG SegSs
Definition: nt_native.h:1480
ULONG Eip
Definition: nt_native.h:1476
ULONG SegCs
Definition: nt_native.h:1477
ULONG SegDs
Definition: nt_native.h:1456
ULONG EFlags
Definition: nt_native.h:1478
ULONG SegEs
Definition: nt_native.h:1455
ULONG Base
Definition: ketypes.h:450
USHORT Limit
Definition: ketypes.h:449
PVOID Base
Definition: ketypes.h:571
USHORT Limit
Definition: ketypes.h:570
KSPECIAL_REGISTERS SpecialRegisters
Definition: ketypes.h:615
CONTEXT ContextFrame
Definition: ketypes.h:616
KDESCRIPTOR Gdtr
Definition: ketypes.h:591
KDESCRIPTOR Idtr
Definition: ketypes.h:592
Definition: ketypes.h:844
ULONG_PTR KernelStack
Definition: arc.h:543
ULONG_PTR Prcb
Definition: arc.h:544
ULONG_PTR Thread
Definition: arc.h:546
#define TAG_KERNEL
Definition: tag.h:42
#define NTAPI
Definition: typedefs.h:36
ULONG_PTR SIZE_T
Definition: typedefs.h:80
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
uint32_t ULONG_PTR
Definition: typedefs.h:65
uint32_t ULONG
Definition: typedefs.h:59
#define ALIGN_DOWN_POINTER_BY(ptr, align)
Definition: umtypes.h:82
struct _KGDTENTRY64::@2310::@2312::@2315 Bits