PatchworkOS  19e446b
A non-POSIX operating system.
Loading...
Searching...
No Matches
CPU Management

CPU. More...

Collaboration diagram for CPU Management:

Detailed Description

CPU.

Modules

 CLI
 Clear Interrupt Flag (CLI) Handling.
 
 GDT
 Global Descriptor Table.
 
 IDT
 Interrupt Descriptor Table.
 
 Interrupts
 Interrupt Handling.
 
 IPI
 Inter-Processor Interrupts (IPIs)
 
 IRQ
 Interrupt Requests (IRQs)
 
 Per-CPU Data
 Per CPU data.
 
 Port I/O
 I/O port operations and reservations.
 
 SIMD
 SIMD context management.
 
 Stack Pointer
 Helpers for managing stacks.
 
 Syscall
 System Call Interface.
 
 TSS
 Task State Segment.
 

Data Structures

struct  cpu_t
 CPU structure. More...
 

Macros

#define CPU_OFFSET_SELF   0x0
 The offset of the self member in the cpu_t structure.
 
#define CPU_OFFSET_SYSCALL_RSP   0x10
 The offset of the syscall_ctx_t pointer in the cpu_t structure.
 
#define CPU_OFFSET_USER_RSP   0x18
 The offset of the userRsp member in the cpu_t structure.
 
#define CPU_OFFSET_ID   0x8
 The offset of the id member in the cpu_t structure.
 
#define CPU_MAX   UINT8_MAX
 Maximum number of CPUs supported.
 
#define CPU_ID_BOOTSTRAP   0
 ID of the bootstrap CPU.
 
#define CPU_ID_INVALID   UINT16_MAX
 Invalid CPU ID.
 
#define CPU_STACK_CANARY   0x1234567890ABCDEFULL
 CPU stack canary value.
 
#define CPU_FOR_EACH(cpu)
 Macro to iterate over all CPUs.
 

Typedefs

typedef uint16_t cpu_id_t
 Type used to identify a CPU.
 

Functions

void cpu_init (cpu_t *cpu)
 Initializes a CPU structure.
 
void cpu_stacks_overflow_check (void)
 Checks the current CPU for stack overflows.
 
uint64_t cpu_halt_others (void)
 Halts all other CPUs.
 
uintptr_t cpu_interrupt_stack_top (void)
 Gets the top of the interrupt stack for the current CPU.
 
static uint16_t cpu_amount (void)
 Gets the number of identified CPUs.
 
static cpu_tcpu_get_by_id (cpu_id_t id)
 Gets a CPU structure by its ID.
 
static cpu_tcpu_get_next (cpu_t *current)
 Gets the next CPU in the CPU array.
 

Variables

cpu_t_cpus [CPU_MAX]
 Array of pointers to cpu_t structures for each CPU, indexed by CPU ID.
 
uint16_t _cpuAmount
 The number of CPUs currently identified.
 

Macro Definition Documentation

◆ CPU_OFFSET_SELF

#define CPU_OFFSET_SELF   0x0

The offset of the self member in the cpu_t structure.

Definition at line 26 of file cpu.h.

◆ CPU_OFFSET_SYSCALL_RSP

#define CPU_OFFSET_SYSCALL_RSP   0x10

The offset of the syscall_ctx_t pointer in the cpu_t structure.

Definition at line 31 of file cpu.h.

◆ CPU_OFFSET_USER_RSP

#define CPU_OFFSET_USER_RSP   0x18

The offset of the userRsp member in the cpu_t structure.

Definition at line 36 of file cpu.h.

◆ CPU_OFFSET_ID

#define CPU_OFFSET_ID   0x8

The offset of the id member in the cpu_t structure.

Needed to access the CPU ID from assembly code.

Definition at line 45 of file cpu.h.

◆ CPU_MAX

#define CPU_MAX   UINT8_MAX

Maximum number of CPUs supported.

Definition at line 50 of file cpu.h.

◆ CPU_ID_BOOTSTRAP

#define CPU_ID_BOOTSTRAP   0

ID of the bootstrap CPU.

Definition at line 55 of file cpu.h.

◆ CPU_ID_INVALID

#define CPU_ID_INVALID   UINT16_MAX

Invalid CPU ID.

Definition at line 60 of file cpu.h.

◆ CPU_STACK_CANARY

#define CPU_STACK_CANARY   0x1234567890ABCDEFULL

CPU stack canary value.

Placed at the bottom of CPU stacks, we then check in the interrupt handler if any of the stacks have overflowed by checking if its canary has been modified.

Definition at line 73 of file cpu.h.

◆ CPU_FOR_EACH

#define CPU_FOR_EACH (   cpu)
Value:
for (cpu_id_t _cpuId = 0; _cpuId < _cpuAmount; _cpuId++) \
for (cpu = _cpus[_cpuId]; cpu != NULL; cpu = NULL)
uint16_t cpu_id_t
Type used to identify a CPU.
Definition cpu.h:65
cpu_t * _cpus[CPU_MAX]
Array of pointers to cpu_t structures for each CPU, indexed by CPU ID.
Definition cpu.c:24
uint16_t _cpuAmount
The number of CPUs currently identified.
Definition cpu.c:25
#define NULL
Pointer error value.
Definition NULL.h:25

Macro to iterate over all CPUs.

The main reason for using this macro is to avoid changes the the internal implementation of how CPUs are stored affecting other parts of the code.

Parameters
cpuLoop variable, a pointer to the current cpu_t.

Definition at line 214 of file cpu.h.

Typedef Documentation

◆ cpu_id_t

typedef uint16_t cpu_id_t

Type used to identify a CPU.

Definition at line 65 of file cpu.h.

Function Documentation

◆ cpu_init()

void cpu_init ( cpu_t cpu)

Initializes a CPU structure.

Will note initialize percpu data, use percpu_update() after calling this function.

Parameters
cpuThe CPU structure to initialize.

Definition at line 27 of file cpu.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cpu_stacks_overflow_check()

void cpu_stacks_overflow_check ( void  )

Checks the current CPU for stack overflows.

Checks the canary values at the bottom of each CPU stack and if its been modified panics.

Definition at line 83 of file cpu.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cpu_halt_others()

uint64_t cpu_halt_others ( void  )

Halts all other CPUs.

Returns
On success, 0. On failure, ERR and errno is set.

Definition at line 115 of file cpu.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cpu_interrupt_stack_top()

uintptr_t cpu_interrupt_stack_top ( void  )

Gets the top of the interrupt stack for the current CPU.

Usefull as we might need to retrieve the interrupt stack in assembly, so this avoid code duplication.

Returns
The top of the interrupt stack.

Definition at line 124 of file cpu.c.

◆ cpu_amount()

static uint16_t cpu_amount ( void  )
inlinestatic

Gets the number of identified CPUs.

Use this over _cpuAmount.

Returns
The number of identified CPUs.

Definition at line 168 of file cpu.h.

Here is the caller graph for this function:

◆ cpu_get_by_id()

static cpu_t * cpu_get_by_id ( cpu_id_t  id)
inlinestatic

Gets a CPU structure by its ID.

Parameters
idThe ID of the CPU to get.
Returns
A pointer to the CPU structure, or NULL if no CPU with the given ID exists.

Definition at line 179 of file cpu.h.

Here is the caller graph for this function:

◆ cpu_get_next()

static cpu_t * cpu_get_next ( cpu_t current)
inlinestatic

Gets the next CPU in the CPU array.

Wraps around to the first CPU if the current CPU is the last one.

Parameters
currentThe current CPU.
Returns
A pointer to the next CPU.

Definition at line 196 of file cpu.h.

Variable Documentation

◆ _cpus

cpu_t* _cpus[CPU_MAX]
extern

Array of pointers to cpu_t structures for each CPU, indexed by CPU ID.

We make this global since its accessed very frequently, so its a slight optimization.

Definition at line 24 of file cpu.c.

◆ _cpuAmount

uint16_t _cpuAmount
extern

The number of CPUs currently identified.

Use cpu_amount() over this variable.

Definition at line 25 of file cpu.c.