The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
cpu.h
Go to the documentation of this file.
1#pragma once
2
3/**
4 * @file cpu.h
5 * @brief the data of each core (per-CPU data). each core has a cpu_t,
6 * and a segment base points at it (arch/arch_cpu.h), so
7 * cpu_current() is one load. the state that belongs to "the core
8 * that runs this code", not to a process, is here.
9 *
10 * the boot core starts the other cores (smp.c). each core runs processes
11 * from its own run queue (scheduler.c).
12 */
13
14#include "arch/arch_cpu.h"
15#include "common/stdint.h"
16
17/** the most cores that ginnOS uses */
18#define CPU_MAX 64
19
20struct process;
21struct user_space;
22
23/**
24 * the timer ticks of a core since boot, as the "cpu" lines of Linux's
25 * /proc/stat. only the core itself adds to them, in its timer interrupt.
26 * another core that reads them on i686 can see a torn value when the low
27 * half overflows (after 497 days), which is only for display.
28 */
29typedef struct
30{
31 uint64_t user; // user code of a process with nice 0 or less
32 uint64_t nice; // user code of a process with nice 1 or more
33 uint64_t system; // kernel code for a process
34 uint64_t idle; // the idle task
36
37/** the data of one core */
38typedef struct cpu
39{
40 struct cpu *self; // CPU_SELF: points at this cpu_t (arch_cpu_self reads it)
41 uintptr_t kernel_sp; // CPU_KERNEL_SP: the kernel stack of the syscall entry (x86_64)
42 uintptr_t user_sp; // CPU_USER_SP: the user stack while the syscall entry runs
43 uint32_t index; // 0 for the boot core
44 struct process *current; // the process that runs on this core
45 struct process *switch_prev; // the process this core switches away from, until the switch ends
46 struct process *idle; // the idle task, or NULL if the core runs no processes
47 struct process *run_head; // the run queue of the core (sched_lock)
50 uint32_t ticks_remaining; // timer ticks before the scheduler switches
51 uint32_t balance_ticks; // timer ticks since the last check of the balance
52 uint32_t pulls; // the processes this core took from other cores
53 cpu_times_t times; // the ticks of this core since boot (process_account_tick)
54 uint32_t apic_id; // the local APIC id of the core
55 uintptr_t idle_stack; // the bottom of the stack the core starts on (0: boot core)
56 volatile bool online; // set by the core when it has started
57 struct user_space *active_space; // the user space whose page tables are loaded, or NULL
58 volatile bool tlb_flush_pending; // another core asks for a TLB flush (tlb.c)
59 uint32_t tlb_flushes; // the flushes done for other cores
61
62/** the offsets that the assembly uses (asm64/syscall.asm); cpu.c checks them */
63#define CPU_SELF 0
64#define CPU_KERNEL_SP (1 * (int)sizeof(uintptr_t))
65#define CPU_USER_SP (2 * (int)sizeof(uintptr_t))
66
67/** the data of all cores; index 0 is the boot core */
68extern cpu_t cpus[CPU_MAX];
69
70/** true once cpu_init_boot() has run, so cpu_current() is valid */
71extern bool cpu_ready;
72
73/** the number of cores that run: cpus[0] to cpus[cpu_count - 1] */
74extern uint32_t cpu_count;
75
76/**
77 * set up the cpu_t of the boot core and point the per-CPU segment at it.
78 * call it once, right after the GDT is loaded, before anything uses
79 * cpu_current().
80 */
81void cpu_init_boot(void);
82
83/** the data of the core that runs the call */
84static inline cpu_t *cpu_current(void)
85{
86 return arch_cpu_self();
87}
88
89/**
90 * start the other cores that the ACPI MADT gives, one at a time. the
91 * kernel command line can limit them: "nosmp" keeps only the boot core,
92 * "maxcpus=N" starts at most N cores, the boot core included. call it on
93 * the boot core with interrupts on, after apic_init.
94 */
95void smp_init(void);
96
97/**
98 * the rest of the start of a core other than the boot core, after the
99 * arch code has set up its registers, tables and local APIC. it marks the
100 * core online and becomes the idle task of the core, which runs processes
101 * from the run queue of the core.
102 * @param cpu the cpu_t of the core that runs the call.
103 * @param has_timer true if the core has a timer. without one the
104 * scheduler cannot end a time slice on the core, so the core only
105 * halts and runs no processes.
106 */
107void cpu_ap_main(cpu_t *cpu, bool has_timer) __attribute__((noreturn));
the arch part of per-CPU data: a segment base that points at the cpu_t of the core (kernel/cpu/cpu....
cpu_t cpus[CPU_MAX]
the data of all cores; index 0 is the boot core
Definition cpu.c:14
void cpu_init_boot(void)
set up the cpu_t of the boot core and point the per-CPU segment at it.
Definition cpu.c:18
uint32_t cpu_count
the number of cores that run: cpus[0] to cpus[cpu_count - 1]
Definition cpu.c:16
bool cpu_ready
true once cpu_init_boot() has run, so cpu_current() is valid
Definition cpu.c:15
struct cpu cpu_t
the data of one core
#define CPU_MAX
the most cores that ginnOS uses
Definition cpu.h:18
void smp_init(void)
start the other cores that the ACPI MADT gives, one at a time.
Definition smp.c:44
void cpu_ap_main(cpu_t *cpu, bool has_timer) __attribute__((noreturn))
the rest of the start of a core other than the boot core, after the arch code has set up its register...
Definition smp.c:95
unsigned int uint32_t
Definition fb.h:17
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINTPTR_TYPE__ uintptr_t
Definition stdint.h:19
ELF32 file header.
Definition elf.h:51
the timer ticks of a core since boot, as the "cpu" lines of Linux's /proc/stat.
Definition cpu.h:30
uint64_t nice
Definition cpu.h:32
uint64_t system
Definition cpu.h:33
uint64_t user
Definition cpu.h:31
uint64_t idle
Definition cpu.h:34
the data of one core
Definition cpu.h:39
uint32_t index
Definition cpu.h:43
struct process * run_tail
Definition cpu.h:48
uintptr_t user_sp
Definition cpu.h:42
uint32_t pulls
Definition cpu.h:52
volatile bool online
Definition cpu.h:56
uint32_t run_count
Definition cpu.h:49
struct cpu * self
Definition cpu.h:40
struct user_space * active_space
Definition cpu.h:57
uint32_t ticks_remaining
Definition cpu.h:50
uint32_t apic_id
Definition cpu.h:54
struct process * current
Definition cpu.h:44
struct process * run_head
Definition cpu.h:47
uintptr_t idle_stack
Definition cpu.h:55
struct process * switch_prev
Definition cpu.h:45
struct process * idle
Definition cpu.h:46
volatile bool tlb_flush_pending
Definition cpu.h:58
uintptr_t kernel_sp
Definition cpu.h:41
cpu_times_t times
Definition cpu.h:53
uint32_t tlb_flushes
Definition cpu.h:59
uint32_t balance_ticks
Definition cpu.h:51
process control block.
Definition process.h:69
the user half of an address space: the page tables, the areas (vma.h) and the program break.
Definition user_space.h:16