Skip to content

Commit

Permalink
[NTOS:KE][HALX86] Implement AP startup code
Browse files Browse the repository at this point in the history
Co-authored-by: Justin Miller <[email protected]>
  • Loading branch information
Extravert-ir and DarkFire01 committed Nov 3, 2023
1 parent 64c5a7e commit 549452d
Show file tree
Hide file tree
Showing 19 changed files with 677 additions and 58 deletions.
7 changes: 6 additions & 1 deletion boot/bootdata/livecd.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ LiveCD="LiveCD"
LiveCD_Debug="LiveCD (Debug)"
LiveCD_Macpi="LiveCD ACPI SMP (Debug)"
LiveCD_Aacpi="LiveCD ACPI APIC (Debug)"
LiveCD_VBoxDebug="LiveCD (VBox Debug)"
LiveCD_Screen="LiveCD (Screen)"
LiveCD_LogFile="LiveCD (Log file)"

Expand Down Expand Up @@ -40,6 +39,12 @@ BootType=Windows2003
SystemPath=\reactos
Options=/DEBUG /DEBUGPORT=VBOX /SOS /MININT

[LiveCD_Aacpi]
BootType=Windows2003
SystemPath=\reactos
Hal=HALAACPI.DLL
Options=/DEBUG /DEBUGPORT=COM1 /BAUDRATE=115200 /SOS /MININT

[LiveCD_Screen]
BootType=Windows2003
SystemPath=\reactos
Expand Down
21 changes: 17 additions & 4 deletions hal/halx86/apic/apicsmp.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
/*
* PROJECT: ReactOS HAL
* LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
* FILE: hal/halx86/apic/apicsmp.c
* PURPOSE: SMP specific APIC code
* PROGRAMMERS: Copyright 2021 Timo Kreuzer ([email protected])
* COPYRIGHT: Copyright 2021 Timo Kreuzer ([email protected])
* Copyright 2021 Justin Miller ([email protected])
*/

/* INCLUDES *******************************************************************/

#include <hal.h>
#include "apicp.h"
#include <smp.h>
#define NDEBUG
#include <debug.h>

Expand Down Expand Up @@ -84,7 +85,6 @@ ApicRequestGlobalInterrupt(

/* SMP SUPPORT FUNCTIONS ******************************************************/

// Should be called by SMP version of HalRequestIpi
VOID
NTAPI
HalpRequestIpi(KAFFINITY TargetProcessors)
Expand All @@ -93,4 +93,17 @@ HalpRequestIpi(KAFFINITY TargetProcessors)
__debugbreak();
}

// APIC specific SMP code here
VOID
ApicStartApplicationProcessor(ULONG NTProcessorNumber, PHYSICAL_ADDRESS StartupLoc)
{
/* Init IPI */
ApicRequestGlobalInterrupt(NTProcessorNumber, 0,
APIC_MT_INIT, APIC_TGM_Edge, APIC_DSH_Destination);

/* Stall execution for a bit to give APIC time */
KeStallExecutionProcessor(1000);

/* Startup IPI */
ApicRequestGlobalInterrupt(NTProcessorNumber, (StartupLoc.LowPart) >> 12,
APIC_MT_Startup, APIC_TGM_Edge, APIC_DSH_Destination);
}
23 changes: 0 additions & 23 deletions hal/halx86/apic/processor.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,6 @@ HalAllProcessorsStarted(VOID)
return TRUE;
}

/*
* @implemented
*/
BOOLEAN
NTAPI
HalStartNextProcessor(IN PLOADER_PARAMETER_BLOCK LoaderBlock,
IN PKPROCESSOR_STATE ProcessorState)
{
/* Ready to start */
return FALSE;
}

/*
* @implemented
*/
Expand All @@ -62,15 +50,4 @@ HalProcessorIdle(VOID)
__halt();
}

/*
* @implemented
*/
VOID
NTAPI
HalRequestIpi(KAFFINITY TargetProcessors)
{
UNIMPLEMENTED;
__debugbreak();
}

/* EOF */
32 changes: 32 additions & 0 deletions hal/halx86/generic/up.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* PROJECT: ReactOS Kernel
* LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
* PURPOSE: Core source file for UP alternative functions
* COPYRIGHT: Copyright 2021 Justin Miller <[email protected]>
*/

/* INCLUDES ******************************************************************/

#include <hal.h>
#define NDEBUG
#include <debug.h>

/* FUNCTIONS *****************************************************************/

VOID
NTAPI
HalRequestIpi(KAFFINITY TargetProcessors)
{
/* This should never be called in UP mode */
__debugbreak();
}

BOOLEAN
NTAPI
HalStartNextProcessor(
IN PLOADER_PARAMETER_BLOCK LoaderBlock,
IN PKPROCESSOR_STATE ProcessorState)
{
/* Always return false on UP systems */
return FALSE;
}
11 changes: 10 additions & 1 deletion hal/halx86/include/smp.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* PROJECT: ReactOS Kernel
* LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
* PURPOSE: Header File for SMP support
* PURPOSE: Public Header File for SMP
* COPYRIGHT: Copyright 2021 Justin Miller <[email protected]>
*/

Expand Down Expand Up @@ -41,3 +41,12 @@ HalpSetupProcessorsTable(

VOID
HalpPrintApicTables(VOID);

/* APIC specific functions inside apic/apicsmp.c */

VOID
ApicStartApplicationProcessor(ULONG NTProcessorNumber, PHYSICAL_ADDRESS StartupLoc);

VOID
NTAPI
HalpRequestIpi(KAFFINITY TargetProcessors);
23 changes: 0 additions & 23 deletions hal/halx86/pic/processor.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,6 @@ HalAllProcessorsStarted(VOID)
return TRUE;
}

/*
* @implemented
*/
BOOLEAN
NTAPI
HalStartNextProcessor(IN PLOADER_PARAMETER_BLOCK LoaderBlock,
IN PKPROCESSOR_STATE ProcessorState)
{
/* Ready to start */
return FALSE;
}

/*
* @implemented
*/
Expand All @@ -62,15 +50,4 @@ HalProcessorIdle(VOID)
__halt();
}

/*
* @implemented
*/
VOID
NTAPI
HalRequestIpi(KAFFINITY TargetProcessors)
{
/* Not implemented on UP */
__debugbreak();
}

/* EOF */
11 changes: 9 additions & 2 deletions hal/halx86/smp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@
list(APPEND HAL_SMP_SOURCE
generic/buildtype.c
generic/spinlock.c
smp/smp.c)
smp/smp.c
smp/ipi.c)

add_library(lib_hal_smp OBJECT ${HAL_SMP_SOURCE})
if(ARCH STREQUAL "i386")
list(APPEND HAL_SMP_ASM_SOURCE
smp/i386/apentry.S)
endif()

add_asm_files(lib_hal_smp_asm ${HAL_SMP_ASM_SOURCE})
add_library(lib_hal_smp OBJECT ${HAL_SMP_SOURCE} ${lib_hal_smp_asm})
add_dependencies(lib_hal_smp bugcodes xdk)
target_compile_definitions(lib_hal_smp PRIVATE CONFIG_SMP)
181 changes: 181 additions & 0 deletions hal/halx86/smp/i386/apentry.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
/*
* PROJECT: ReactOS HAL
* LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
* PURPOSE: Application processor startup code for i386
* COPYRIGHT: Copyright 2021 Justin Miller <[email protected]>
* Copyright 2021 Victor Perevertkin <[email protected]>
*/

#include <asm.inc>
#include <ks386.inc>

#define ZERO_OFFSET(f) (f - _APEntry16)
#define PS(f) (f - _APEntryCpuState)

PUBLIC _APEntry16
PUBLIC _APEntry16End
PUBLIC _APEntryJump32Offset
PUBLIC _APEntryJump32Segment
PUBLIC _TempPageTableAddr
PUBLIC _APEntryCpuState
PUBLIC _APEntry32

.code16
_APEntry16:
cli

/* Load final descriptor tables */
#ifdef _USE_ML
data32 lgdt fword ptr cs:[ZERO_OFFSET(PS_Gdtr)]
data32 lidt fword ptr cs:[ZERO_OFFSET(PS_Idtr)]
#else
data32 lgdt cs:[ZERO_OFFSET(PS_Gdtr)]
data32 lidt cs:[ZERO_OFFSET(PS_Idtr)]
#endif

/* Processor state base address */
mov ebx, cs:[ZERO_OFFSET(PS_SelfPtr)]
mov esi, cs:[ZERO_OFFSET(PS_SegDs)]

/* Load temp page table */
mov eax, cs:[ZERO_OFFSET(_TempPageTableAddr)]
mov cr3, eax

/* Enable paging and protected mode */
mov eax, cs:[ZERO_OFFSET(PS_Cr0)]
mov cr0, eax

/* Long jump, 32bit address */
.byte HEX(66)
.byte HEX(EA)
_APEntryJump32Offset:
.long 0
_APEntryJump32Segment:
.word 0

_APEntry16End:

.align 4
_TempPageTableAddr:
.long 0
// Processor state
_APEntryCpuState:
PS_SelfPtr:
.long 0 // self pointer (for use in protected mode)
PS_Esp:
.long 0
PS_Eip:
.long 0
PS_Eflags:
.long 0
PS_SegCs:
.long 0
PS_SegDs:
.long 0
PS_SegEs:
.long 0
PS_SegSs:
.long 0
PS_SegFs:
.long 0
PS_SegGs:
.long 0
// KSPECIAL_REGISTERS
PS_Cr0:
.long 0
PS_Cr2:
.long 0
PS_Cr3:
.long 0
PS_Cr4:
.long 0
PS_KernelDr0:
.long 0
PS_KernelDr1:
.long 0
PS_KernelDr2:
.long 0
PS_KernelDr3:
.long 0
PS_KernelDr6:
.long 0
PS_KernelDr7:
.long 0
.space 2
PS_Gdtr:
.word 0
.long 0
.space 2
PS_Idtr:
.word 0
.long 0
PS_Tr:
.word 0
PS_Ldtr:
.word 0
.space 4*6 // reserved
.endcode16

.code32
_APEntry32:
cli

/* Load segment registers from ProcessorState values */
/* DS should be the first one */
mov ds, esi

mov eax, [ebx + PS(PS_SegEs)]
mov es, eax
mov eax, [ebx + PS(PS_SegSs)]
mov ss, eax
mov eax, [ebx + PS(PS_SegFs)]
mov fs, eax
mov eax, [ebx + PS(PS_SegGs)]
mov gs, eax

/* Write CR registers with ProcessorState values */
mov eax, [ebx + PS(PS_Cr3)]
mov cr3, eax
mov eax, [ebx + PS(PS_Cr4)]
mov cr4, eax

/* Load debug registers */
mov eax, [ebx + PS(PS_KernelDr0)]
mov dr0, eax
mov eax, [ebx + PS(PS_KernelDr1)]
mov dr1, eax
mov eax, [ebx + PS(PS_KernelDr2)]
mov dr2, eax
mov eax, [ebx + PS(PS_KernelDr3)]
mov dr3, eax
mov eax, [ebx + PS(PS_KernelDr6)]
mov dr6, eax
mov eax, [ebx + PS(PS_KernelDr7)]
mov dr7, eax

/* Load TSS */
ltr word ptr [ebx + PS(PS_Tr)]

/* Load AP Stack*/
mov esp, [ebx + PS(PS_Esp)]

/* Load Eip and push it as a "return" address */
mov eax, [ebx + PS(PS_Eip)]
push eax

/* Load flags */
mov eax, [ebx + PS(PS_Eflags)]
sahf

/* Set up all GP registers */
xor edi, edi
xor esi, esi
xor ebp, ebp
xor ebx, ebx
xor edx, edx
xor ecx, ecx
xor eax, eax

/* Jump into the kernel */
ret
END
Loading

0 comments on commit 549452d

Please sign in to comment.