The manual
ginnOS
The reference: libc, the libraries and the kernel
Toggle main menu visibility
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
20
struct
process
;
21
struct
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
*/
29
typedef
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
35
}
cpu_times_t
;
36
37
/** the data of one core */
38
typedef
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)
48
struct
process
*
run_tail
;
49
uint32_t
run_count
;
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
60
}
cpu_t
;
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 */
68
extern
cpu_t
cpus
[
CPU_MAX
];
69
70
/** true once cpu_init_boot() has run, so cpu_current() is valid */
71
extern
bool
cpu_ready
;
72
73
/** the number of cores that run: cpus[0] to cpus[cpu_count - 1] */
74
extern
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
*/
81
void
cpu_init_boot
(
void
);
82
83
/** the data of the core that runs the call */
84
static
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
*/
95
void
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
*/
107
void
cpu_ap_main
(
cpu_t
*
cpu
,
bool
has_timer)
__attribute__
((noreturn));
arch_cpu.h
the arch part of per-CPU data: a segment base that points at the cpu_t of the core (kernel/cpu/cpu....
stdint.h
cpus
cpu_t cpus[CPU_MAX]
the data of all cores; index 0 is the boot core
Definition
cpu.c:14
cpu_init_boot
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
cpu_count
uint32_t cpu_count
the number of cores that run: cpus[0] to cpus[cpu_count - 1]
Definition
cpu.c:16
cpu_ready
bool cpu_ready
true once cpu_init_boot() has run, so cpu_current() is valid
Definition
cpu.c:15
cpu_t
struct cpu cpu_t
the data of one core
CPU_MAX
#define CPU_MAX
the most cores that ginnOS uses
Definition
cpu.h:18
smp_init
void smp_init(void)
start the other cores that the ACPI MADT gives, one at a time.
Definition
smp.c:44
cpu_ap_main
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
uint32_t
unsigned int uint32_t
Definition
fb.h:17
uint64_t
__UINT64_TYPE__ uint64_t
Definition
stdint.h:17
uintptr_t
__UINTPTR_TYPE__ uintptr_t
Definition
stdint.h:19
__attribute__
ELF32 file header.
Definition
elf.h:51
cpu_times_t
the timer ticks of a core since boot, as the "cpu" lines of Linux's /proc/stat.
Definition
cpu.h:30
cpu_times_t::nice
uint64_t nice
Definition
cpu.h:32
cpu_times_t::system
uint64_t system
Definition
cpu.h:33
cpu_times_t::user
uint64_t user
Definition
cpu.h:31
cpu_times_t::idle
uint64_t idle
Definition
cpu.h:34
cpu
the data of one core
Definition
cpu.h:39
cpu::index
uint32_t index
Definition
cpu.h:43
cpu::run_tail
struct process * run_tail
Definition
cpu.h:48
cpu::user_sp
uintptr_t user_sp
Definition
cpu.h:42
cpu::pulls
uint32_t pulls
Definition
cpu.h:52
cpu::online
volatile bool online
Definition
cpu.h:56
cpu::run_count
uint32_t run_count
Definition
cpu.h:49
cpu::self
struct cpu * self
Definition
cpu.h:40
cpu::active_space
struct user_space * active_space
Definition
cpu.h:57
cpu::ticks_remaining
uint32_t ticks_remaining
Definition
cpu.h:50
cpu::apic_id
uint32_t apic_id
Definition
cpu.h:54
cpu::current
struct process * current
Definition
cpu.h:44
cpu::run_head
struct process * run_head
Definition
cpu.h:47
cpu::idle_stack
uintptr_t idle_stack
Definition
cpu.h:55
cpu::switch_prev
struct process * switch_prev
Definition
cpu.h:45
cpu::idle
struct process * idle
Definition
cpu.h:46
cpu::tlb_flush_pending
volatile bool tlb_flush_pending
Definition
cpu.h:58
cpu::kernel_sp
uintptr_t kernel_sp
Definition
cpu.h:41
cpu::times
cpu_times_t times
Definition
cpu.h:53
cpu::tlb_flushes
uint32_t tlb_flushes
Definition
cpu.h:59
cpu::balance_ticks
uint32_t balance_ticks
Definition
cpu.h:51
process
process control block.
Definition
process.h:69
user_space
the user half of an address space: the page tables, the areas (vma.h) and the program break.
Definition
user_space.h:16
src
kernel
cpu
cpu.h
Generated by
1.18.0