PatchworkOS  3984a1d
A non-POSIX operating system.
Loading...
Searching...
No Matches
trampoline.h
Go to the documentation of this file.
1#pragma once
2
3#ifndef __ASSEMBLER__
4#include <kernel/cpu/cpu.h>
5
7
8#include <stdint.h>
9#include <sys/proc.h>
10#endif
11
12/**
13 * @brief Trampoline for CPU initialization
14 * @defgroup modules_smp_trampoline Trampoline
15 * @ingroup modules_smp
16 *
17 * The trampoline is a small piece of code used during the initialization of other CPUs in a multiprocessor system. The
18 * code itself must be position-independent and fit within a single memory page, this is why we do all the weird offset
19 * stuff.
20 *
21 * @{
22 */
23
24/**
25 * @brief The physical address where the trampoline code will be copied to and executed from.
26 */
27#define TRAMPOLINE_BASE_ADDR 0x8000
28
29/**
30 * @brief The offset within the trampoline page where we can store data.
31 *
32 * This is used to pass data to the trampoline code, such as the stack pointer to use and the entry point to jump to. As
33 * it cannot access virtual memory yet.
34 */
35#define TRAMPOLINE_DATA_OFFSET 0x0F00
36
37/**
38 * @brief Offset within the trampoline page where the PML4 address is stored.
39 */
40#define TRAMPOLINE_PML4_OFFSET (TRAMPOLINE_DATA_OFFSET + 0x00)
41
42/**
43 * @brief Offset within the trampoline page where the entry point to jump to is stored.
44 */
45#define TRAMPOLINE_ENTRY_OFFSET (TRAMPOLINE_DATA_OFFSET + 0x08)
46
47/**
48 * @brief Offset within the trampoline page where the CPU structure pointer is stored.
49 */
50#define TRAMPOLINE_CPU_OFFSET (TRAMPOLINE_DATA_OFFSET + 0x10)
51
52/**
53 * @brief Offset within the trampoline page where the stack pointer for the trampoline is stored.
54 */
55#define TRAMPOLINE_STACK_OFFSET (TRAMPOLINE_DATA_OFFSET + 0x18)
56
57#ifndef __ASSEMBLER__
58
59/**
60 * @brief The start of the trampoline code, defined in `trampoline.s`.
61 */
62extern void trampoline_start(void);
63
64/**
65 * @brief The end of the trampoline code, defined in `trampoline.s`.
66 */
67extern void trampoline_end(void);
68
69/**
70 * @brief The size of the trampoline code.
71 */
72#define TRAMPOLINE_SIZE ((uintptr_t)trampoline_end - (uintptr_t)trampoline_start)
73
74/**
75 * @brief Initializes the trampoline by copying the trampoline code to its designated memory location.
76 *
77 * Will also backup the original contents of the trampoline memory location and restore it when `trampoline_deinit()` is
78 * called.
79 */
80void trampoline_init(void);
81
82/**
83 * @brief Deinitializes the trampoline by restoring the original contents of the trampoline memory location.
84 */
85void trampoline_deinit(void);
86
87/**
88 * @brief Sends the startup IPI to a CPU to start it up.
89 *
90 * @param cpu The CPU structure to be initalized as the new CPU.
91 * @param lapicId The LAPIC ID of the CPU to start.
92 */
94
95/**
96 * @brief Waits for the currently starting CPU to signal that it is ready.
97 *
98 * @param timeout The maximum time to wait in clock ticks.
99 * @return On success, `0`. On timeout, `ERR` and `errno` is set.
100 */
102
103/**
104 * @brief After the trampoline is done with basic initialization, it calls this C entry point to continue CPU
105 * initialization.
106 *
107 * When this function is called the trampoline's stack is still being used, after cpu initialization is done we perform
108 * a jump to the idle thread of the CPU.
109 *
110 * @param self Pointer to the CPU structure of the current CPU, will still be uninitialized.
111 */
113
114#endif
115
116/** @} */
__UINT64_TYPE__ clock_t
A nanosecond time.
Definition clock_t.h:13
uint8_t lapic_id_t
Local APIC ID type.
Definition lapic.h:32
void trampoline_send_startup_ipi(cpu_t *cpu, lapic_id_t lapicId)
Sends the startup IPI to a CPU to start it up.
Definition trampoline.c:73
void trampoline_start(void)
The start of the trampoline code, defined in trampoline.s.
void trampoline_end(void)
The end of the trampoline code, defined in trampoline.s.
void trampoline_deinit(void)
Deinitializes the trampoline by restoring the original contents of the trampoline memory location.
Definition trampoline.c:58
uint64_t trampoline_wait_ready(clock_t timeout)
Waits for the currently starting CPU to signal that it is ready.
Definition trampoline.c:86
_NORETURN void trampoline_c_entry(cpu_t *self)
After the trampoline is done with basic initialization, it calls this C entry point to continue CPU i...
Definition trampoline.c:110
void trampoline_init(void)
Initializes the trampoline by copying the trampoline code to its designated memory location.
Definition trampoline.c:23
#define _NORETURN
Definition config.h:28
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
CPU structure.
Definition cpu.h:122