My Project
Loading...
Searching...
No Matches
ap_main.c
Go to the documentation of this file.
1#include "../../includes/mh.h"
2#include "../../includes/mg.h"
3#include "../../includes/me.h"
4#include "../../assert.h"
5
7extern PROCESSOR cpus[];
8
9extern IDT_PTR PIDT;
10
11static inline uint64_t build_seg(uint32_t base, uint32_t limit, uint8_t access, uint8_t gran) {
12 uint64_t desc = 0;
13 desc = (limit & 0xFFFFull);
14 desc |= (uint64_t)(base & 0xFFFFull) << 16;
15 desc |= (uint64_t)((base >> 16) & 0xFFull) << 32;
16 desc |= (uint64_t)access << 40;
17 uint8_t gran_byte = (uint8_t)(((limit >> 16) & 0x0Fu) | (gran & 0xF0u));
18 desc |= (uint64_t)gran_byte << 48;
19 desc |= (uint64_t)((base >> 24) & 0xFFull) << 56;
20 return desc;
21}
22
23static inline uint8_t get_initial_apic_id(void) {
24 uint32_t eax, ebx, ecx, edx;
25
26 // When EAX=1, CPUID returns processor info.
27 // The initial APIC ID is in bits 31-24 of the EBX register.
28 __asm__ volatile("cpuid"
29 : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
30 : "a"(1), "c"(0));
31
32 return (uint8_t)(ebx >> 24);
33}
34
35void APMain(void) {
36 // First, setup the GDT&TSS, then IDT.
37 int idx = -1;
38 // early map lapic mmio (lapic_init_cpu maps it).
39 uint8_t id = get_initial_apic_id();
40
41 for (int i = 0; i < (int)bootInfo.cpu_count && i < MAX_CPUS; i++) {
42 if (cpus[i].lapic_ID == id) { idx = i; break; }
43 }
44
45 if (idx < 0) {
46 assert(false, "All APs must be initialized fully and successfully.");
47 gop_printf(COLOR_RED, "**Fatal error, AP Failed to initialize, index below 0.**\n");
48 __hlt();
49 }
50 __writemsr(IA32_GS_BASE, (uint64_t)&cpus[idx]);
51
52 // Self invalidate all TLBs
54
55 // Now setup the IDT for the CPU. (load the one setupped by the smp func)
56 __lidt(&PIDT);
57
58 // Initiate per cpu functions.
60
61 // Initialize the MM For current core (init PAT)
63
64 // Initialize the idle thread.
66
67 // mark as online and clear being unavailable
69 InterlockedAndU64(&cpus[idx].flags, ~CPU_UNAVAILABLE); // clear unavailable
70 gop_printf(COLOR_ORANGE, "**Hello From AP CPU! - I'm ID: %d | StackTop: %p | CPU Ptr: %p**\n", id, MeGetCurrentProcessor()->VirtStackTop, MeGetCurrentProcessor());
71 // enable interupts, initiate timer and join scheduler queue
75 __sti();
76 Schedule();
77 for (;;) __hlt();
78}
void APMain(void)
---------------— FUNCTIONS ---------------—
Definition ap_main.c:35
SMP_BOOTINFO bootInfo
Definition smp.c:18
int init_lapic_timer(uint32_t hz)
Definition apic.c:179
void lapic_init_cpu(void)
Definition apic.c:113
void lapic_enable(void)
Definition apic.c:98
#define assert(...)
Definition assert.h:57
FORCEINLINE uint64_t InterlockedAndU64(volatile uint64_t *target, uint64_t value)
Definition atomic.h:134
FORCEINLINE uint64_t InterlockedOrU64(volatile uint64_t *target, uint64_t value)
Definition atomic.h:144
struct _PROCESSOR PROCESSOR
Definition core.h:45
void gop_printf(uint32_t color, const char *fmt,...)
Definition gop.c:694
FORCEINLINE void __writemsr(uint32_t msr, uint64_t value)
Definition intrin.h:200
FORCEINLINE void __hlt(void)
Definition intrin.h:50
FORCEINLINE void __sti(void)
Definition intrin.h:44
FORCEINLINE void __write_cr3(uint64_t val)
Definition intrin.h:83
#define IA32_GS_BASE
Definition intrin.h:20
FORCEINLINE void __lidt(void *idt_ptr)
Definition intrin.h:135
FORCEINLINE uint64_t __read_cr3(void)
Definition intrin.h:78
FORCEINLINE PPROCESSOR MeGetCurrentProcessor(void)
Definition me.h:356
@ CPU_ONLINE
Definition me.h:219
@ CPU_UNAVAILABLE
Definition me.h:222
IDT_PTR PIDT
Definition idt.c:10
void MeInitializeProcessor(IN PPROCESSOR CPU, IN bool InitializeStandardRoutine, IN bool AreYouAP)
Definition meinit.c:164
#define COLOR_RED
Colors definitions for easier access.
Definition mg.h:29
#define COLOR_ORANGE
Definition mg.h:40
struct _IDT_PTR IDT_PTR
uint32_t flags
Definition mh.h:2
#define MAX_CPUS
Definition mh.h:408
struct _SMP_BOOTINFO SMP_BOOTINFO
@ SYSTEM_PHASE_INITIALIZE_PAT_ONLY
Definition mm.h:376
bool MmInitSystem(IN uint8_t Phase, IN PBOOT_INFO BootInformation)
Definition mminit.c:55
NORETURN void Schedule(void)
Definition scheduler.c:105
PROCESSOR cpus[]
Definition smp.c:16
void InitScheduler(void)
Definition scheduler.c:26