PatchworkOS
Loading...
Searching...
No Matches
cpu.c
Go to the documentation of this file.
1#include <kernel/cpu/cpu.h>
2
3#include <kernel/cpu/gdt.h>
4#include <kernel/cpu/idt.h>
6#include <kernel/cpu/simd.h>
8#include <kernel/cpu/tss.h>
11#include <kernel/log/log.h>
12#include <kernel/log/panic.h>
13#include <kernel/mem/vmm.h>
14#include <kernel/sched/sched.h>
15
17{
20
22
23 cpu->id = id;
24 cpu->lapicId = lapic_self_id();
25 tss_init(&cpu->tss);
26 vmm_cpu_ctx_init(&cpu->vmm);
27 gdt_cpu_tss_load(&cpu->tss);
30 timer_ctx_init(&cpu->timer);
31 wait_cpu_ctx_init(&cpu->wait, cpu);
32 sched_cpu_ctx_init(&cpu->sched, cpu);
33
34 if (stack_pointer_init_buffer(&cpu->exceptionStack, cpu->exceptionStackBuffer, CONFIG_INTERRUPT_STACK_PAGES) == ERR)
35 {
36 LOG_ERR("failed to init exception stack for cpu %u\n", cpu->id);
37 return ERR;
38 }
41
42 if (stack_pointer_init_buffer(&cpu->doubleFaultStack, cpu->doubleFaultStackBuffer, CONFIG_INTERRUPT_STACK_PAGES) ==
43 ERR)
44 {
45 LOG_ERR("failed to init double fault stack for cpu %u\n", cpu->id);
47 return ERR;
48 }
51
52 if (stack_pointer_init_buffer(&cpu->interruptStack, cpu->interruptStackBuffer, CONFIG_INTERRUPT_STACK_PAGES) == ERR)
53 {
54 LOG_ERR("failed to init interrupt stack for cpu %u\n", cpu->id);
57 return ERR;
58 }
61
65
66 return 0;
67}
68
70{
72 {
73 panic(NULL, "CPU%u exception stack overflow detected", cpu->id);
74 }
76 {
77 panic(NULL, "CPU%u double fault stack overflow detected", cpu->id);
78 }
80 {
81 panic(NULL, "CPU%u interrupt stack overflow detected", cpu->id);
82 }
83}
static char id[MAX_NAME]
Definition dwm.c:20
void gdt_cpu_tss_load(tss_t *tss)
Load a TSS into the GDT and load it using the ltr instruction on the current CPU.
Definition gdt.c:63
void gdt_cpu_load(void)
Load the GDT on the current CPU.
Definition gdt.c:55
void idt_cpu_load(void)
Load the IDT on the current CPU.
Definition idt.c:47
void interrupt_ctx_init(interrupt_ctx_t *ctx)
Initializes the CLI context.
Definition interrupt.c:19
void simd_cpu_init(void)
Definition simd.c:42
void stack_pointer_deinit_buffer(stack_pointer_t *stack)
Deinitializes a stack pointer structure that was initialized using stack_pointer_init_buffer().
uint64_t stack_pointer_init_buffer(stack_pointer_t *stack, void *buffer, uint64_t pages)
Initializes a stack pointer structure using a provided buffer, does not allocate or map any memory.
void syscalls_cpu_init(void)
Initalize syscalls on the current CPU.
Definition syscalls.c:39
void tss_ist_load(tss_t *tss, tss_ist_t ist, stack_pointer_t *stack)
Load a stack into an IST entry.
Definition tss.c:21
void tss_init(tss_t *tss)
Initialize a TSS structure.
Definition tss.c:5
#define TSS_IST_INTERRUPT
The IST index to use for other interrupts.
Definition tss.h:50
#define TSS_IST_EXCEPTION
The IST index to use for exceptions.
Definition tss.h:40
#define TSS_IST_DOUBLE_FAULT
The IST index to use for double faults.
Definition tss.h:45
void cpu_stacks_overflow_check(cpu_t *cpu)
Checks for CPU stack overflows.
Definition cpu.c:69
uint64_t cpu_init(cpu_t *cpu, cpuid_t id)
Initializes a CPU structure as part of the boot process.
Definition cpu.c:16
#define CPU_STACK_CANARY
CPU stack canary value.
Definition cpu.h:31
uint16_t cpuid_t
Type used to identify a CPU.
Definition cpu_id.h:29
void lapic_cpu_init(void)
Initialize the local apic for the current cpu.
Definition apic.c:147
lapic_id_t lapic_self_id(void)
Get the lapic id of the current cpu.
Definition apic.c:171
void statistics_cpu_ctx_init(statistics_cpu_ctx_t *ctx)
Initializes a per-CPU statistics context.
Definition statistics.c:24
NORETURN void panic(const interrupt_frame_t *frame, const char *format,...)
Panic the kernel, printing a message and halting.
Definition panic.c:362
#define LOG_ERR(format,...)
Definition log.h:89
void vmm_cpu_ctx_init(vmm_cpu_ctx_t *ctx)
Initializes a per-CPU VMM context and performs per-CPU VMM initialization.
Definition vmm.c:110
void wait_cpu_ctx_init(wait_cpu_ctx_t *wait, cpu_t *self)
Initialize per-CPU wait context.
Definition wait.c:95
void sched_cpu_ctx_init(sched_cpu_ctx_t *ctx, cpu_t *self)
Initializes a CPU's scheduling context.
Definition sched.c:83
void timer_ctx_init(timer_ctx_t *ctx)
Initialize per-CPU timer context.
Definition timer.c:51
#define CONFIG_INTERRUPT_STACK_PAGES
Interrupt stack configuration.
Definition config.h:14
#define NULL
Pointer error value.
Definition NULL.h:23
#define ERR
Integer error value.
Definition ERR.h:17
#define MSR_CPU_ID
Definition regs.h:13
static void msr_write(uint32_t msr, uint64_t value)
Definition regs.h:71
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
CPU structure.
Definition cpu.h:42
timer_ctx_t timer
Definition cpu.h:49
cpuid_t id
Definition cpu.h:43
interrupt_ctx_t interrupt
Definition cpu.h:47
wait_cpu_ctx_t wait
Definition cpu.h:50
vmm_cpu_ctx_t vmm
Definition cpu.h:46
sched_cpu_ctx_t sched
Definition cpu.h:51
stack_pointer_t exceptionStack
Definition cpu.h:53
tss_t tss
Definition cpu.h:45
lapic_id_t lapicId
Definition cpu.h:44
statistics_cpu_ctx_t stat
Definition cpu.h:48
stack_pointer_t interruptStack
Definition cpu.h:55
stack_pointer_t doubleFaultStack
Definition cpu.h:54
uintptr_t bottom
The bottom of the stack, this address is inclusive.