MOS Source Code
Loading...
Searching...
No Matches
platform.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#pragma once
4
5#include "mos/interrupt/ipi.h"
6#include "mos/misc/cmdline.h"
9#include "mos/platform/platform_defs.h"
10
13#include <mos/mm/mm_types.h>
14#include <mos/mos_global.h>
16
17// clang-format off
18#if MOS_CONFIG(MOS_SMP)
19#define PER_CPU_DECLARE(type, name) struct name { type percpu_value[MOS_MAX_CPU_COUNT]; } name
20#define PER_CPU_VAR_INIT { .percpu_value = { 0 } }
21#define per_cpu(var) (&(var.percpu_value[platform_current_cpu_id()]))
22#else
23#define PER_CPU_DECLARE(type, name) struct name { type percpu_value[1]; } name
24#define PER_CPU_VAR_INIT { .percpu_value = { 0 } }
25#define per_cpu(var) (&(var.percpu_value[0]))
26#endif
27// clang-format on
28
29#define current_cpu per_cpu(platform_info->cpu)
30#define current_thread (current_cpu->thread)
31#define current_process (current_thread->owner)
32#define current_mm (current_cpu->mm_context)
33
34typedef void (*irq_handler)(u32 irq);
35
36typedef struct _thread thread_t;
37typedef struct _console console_t;
38
39typedef enum
40{
44
45 VM_USER = 1 << 3,
48 VM_GLOBAL = 1 << 6,
49
50 // composite flags (for convenience)
58} vm_flags;
59
69
76
78
85
86typedef struct _platform_regs platform_regs_t;
87
98
108
109typedef struct
110{
114
116 ptr_t k_basevaddr; // virtual address of the kernel base (i.e. the start of the kernel image)
117
119
122
126
127 ptr_t direct_map_base; // direct mapping to all physical memory
128
131
133
136
137#define MOS_KERNEL_PFN(vaddr) ((ALIGN_DOWN_TO_PAGE((vaddr) - (platform_info->k_basevaddr)) / MOS_PAGE_SIZE) + (platform_info->k_basepfn))
138
140
141typedef struct _platform_process_options platform_process_options_t;
142typedef struct _platform_thread_options platform_thread_options_t;
143
145
146// should be defined in platform's linker script
147extern const char __MOS_KERNEL_CODE_START[], __MOS_KERNEL_CODE_END[]; // Kernel text
148extern const char __MOS_KERNEL_RODATA_START[], __MOS_KERNEL_RODATA_END[]; // Kernel rodata
149extern const char __MOS_KERNEL_RW_START[], __MOS_KERNEL_RW_END[]; // Kernel read-write data
150extern const char __MOS_KERNEL_END[]; // Kernel end
151
152extern void mos_start_kernel(void);
153
154#define platform_alias(name) __attribute__((alias("platform_default_" #name)))
155
156// Platform Startup APIs
157void platform_ap_entry(u64 arg);
161
162// Platform Machine APIs
163// default implementation panics
164[[noreturn]] void platform_shutdown(void);
165// default implementations do nothing for 4 functions below
170
171// Platform Timer/Clock APIs
172// default implementation does nothing
174
175// Platform CPU APIs
176// default implementation loops forever
177[[noreturn]] void platform_halt_cpu(void);
178// default implementation does nothing for 4 functions below
181void platform_cpu_idle(void);
183
184typedef char datetime_str_t[32];
186
187// Platform Interrupt APIs
188// default implementation does nothing
191
192// Platform Page Table APIs
193// no default implementation, platform-specific implementations must be provided
194pfn_t platform_pml1e_get_pfn(const pml1e_t *pml1); // returns the physical address contained in the pmlx entry,
195void platform_pml1e_set_pfn(pml1e_t *pml1, pfn_t pfn); // -- which can be a pfn for either a page or another page table
196bool platform_pml1e_get_present(const pml1e_t *pml1); // returns if an entry in this page table is present
197void platform_pml1e_set_flags(pml1e_t *pml1, vm_flags flags); // set bits in the flags field of the pmlx entry
198vm_flags platform_pml1e_get_flags(const pml1e_t *pml1e); // get bits in the flags field of the pmlx entry
199
200#if MOS_PLATFORM_PAGING_LEVELS >= 2
201pml1_t platform_pml2e_get_pml1(const pml2e_t *pml2);
202void platform_pml2e_set_pml1(pml2e_t *pml2, pml1_t pml1, pfn_t pml1_pfn);
203bool platform_pml2e_get_present(const pml2e_t *pml2);
204void platform_pml2e_set_flags(pml2e_t *pml2, vm_flags flags);
205vm_flags platform_pml2e_get_flags(const pml2e_t *pml2e);
206#if MOS_CONFIG(PML2_HUGE_CAPABLE)
207bool platform_pml2e_is_huge(const pml2e_t *pml2);
208void platform_pml2e_set_huge(pml2e_t *pml2, pfn_t pfn);
209pfn_t platform_pml2e_get_huge_pfn(const pml2e_t *pml2);
210#endif
211#endif
212
213#if MOS_PLATFORM_PAGING_LEVELS >= 3
214pml2_t platform_pml3e_get_pml2(const pml3e_t *pml3);
215void platform_pml3e_set_pml2(pml3e_t *pml3, pml2_t pml2, pfn_t pml2_pfn);
216bool platform_pml3e_get_present(const pml3e_t *pml3);
217void platform_pml3e_set_flags(pml3e_t *pml3, vm_flags flags);
219#if MOS_CONFIG(PML3_HUGE_CAPABLE)
220bool platform_pml3e_is_huge(const pml3e_t *pml3);
221void platform_pml3e_set_huge(pml3e_t *pml3, pfn_t pfn);
223#endif
224#endif
225
226#if MOS_PLATFORM_PAGING_LEVELS >= 4
227pml3_t platform_pml4e_get_pml3(const pml4e_t *pml4);
228void platform_pml4e_set_pml3(pml4e_t *pml4, pml3_t pml3, pfn_t pml3_pfn);
229bool platform_pml4e_get_present(const pml4e_t *pml4);
230void platform_pml4e_set_flags(pml4e_t *pml4, vm_flags flags);
232#if MOS_CONFIG(PML4_HUGE_CAPABLE)
233bool platform_pml4e_is_huge(const pml4e_t *pml4);
234void platform_pml4e_set_huge(pml4e_t *pml4, pfn_t pfn);
235pfn_t platform_pml4e_get_huge_pfn(const pml4e_t *pml4);
236#endif
237#endif
238
239// Platform Thread / Process APIs
240// no default implementation, platform-specific implementations must be provided
242void platform_context_setup_main_thread(thread_t *thread, ptr_t entry, ptr_t sp, int argc, ptr_t argv, ptr_t envp);
243void platform_context_setup_child_thread(thread_t *thread, thread_entry_t entry, void *arg);
244void platform_context_clone(const thread_t *from, thread_t *to);
246
247// Platform Context Switching APIs
248// no default implementation, platform-specific implementations must be provided
249void platform_switch_mm(const mm_context_t *new_mm);
250void platform_switch_to_thread(thread_t *current, thread_t *new_thread, switch_flags_t switch_flags);
251[[noreturn]] void platform_return_to_userspace(platform_regs_t *regs);
252
253// Platform-Specific syscall APIs
254// default implementation does nothing
255u64 platform_arch_syscall(u64 syscall, u64 arg1, u64 arg2, u64 arg3, u64 arg4);
256
257// Platform-Specific IPI (Inter-Processor Interrupt) APIs
258// default implementation does nothing
259void platform_ipi_send(u8 target_cpu, ipi_type_t type);
260
261// Signal Handler APIs
262// the 4 function below has default implementations that panic if not implemented
263typedef struct _sigreturn_data sigreturn_data_t;
264[[noreturn]] void platform_jump_to_signal_handler(const platform_regs_t *regs, const sigreturn_data_t *sigreturn_data, const sigaction_t *sa);
265[[noreturn]] void platform_restore_from_signal_handler(void *sp);
268
void platform_pml3e_set_pml2(pml3e_t *pml3, pml2_t pml2, pfn_t pml2_pfn)
Definition mm.c:162
void platform_pml2e_set_flags(pml2e_t *pml2, vm_flags flags)
Definition mm.c:125
void platform_pml4e_set_flags(pml4e_t *pml4, vm_flags flags)
Definition mm.c:225
bool platform_pml4e_get_present(const pml4e_t *pml4)
Definition mm.c:220
vm_flags platform_pml3e_get_flags(const pml3e_t *pml3e)
Definition mm.c:181
bool platform_pml3e_get_present(const pml3e_t *pml3)
Definition mm.c:170
vm_flags platform_pml4e_get_flags(const pml4e_t *pml4e)
Definition mm.c:231
pml1_t platform_pml2e_get_pml1(const pml2e_t *pml2)
Definition mm.c:107
bool platform_pml2e_get_present(const pml2e_t *pml2)
Definition mm.c:120
void platform_pml2e_set_pml1(pml2e_t *pml2, pml1_t pml1, pfn_t pml1_pfn)
Definition mm.c:112
void platform_pml3e_set_flags(pml3e_t *pml3, vm_flags flags)
Definition mm.c:175
vm_flags platform_pml2e_get_flags(const pml2e_t *pml2e)
Definition mm.c:131
void platform_pml4e_set_pml3(pml4e_t *pml4, pml3_t pml3, pfn_t pml3_pfn)
Definition mm.c:212
pml3_t platform_pml4e_get_pml3(const pml4e_t *pml4)
Definition mm.c:207
pml2_t platform_pml3e_get_pml2(const pml3e_t *pml3)
Definition mm.c:157
bool platform_pml3e_is_huge(const pml3e_t *pml3e)
Definition paging.c:244
pfn_t platform_pml2e_get_huge_pfn(const pml2e_t *pml2)
Definition paging.c:181
void platform_pml2e_set_huge(pml2e_t *pml2e, pfn_t pfn)
Definition paging.c:172
bool platform_pml2e_is_huge(const pml2e_t *pml2e)
Definition paging.c:166
void platform_pml3e_set_huge(pml3e_t *pml3e, pfn_t pfn)
Definition paging.c:250
pfn_t platform_pml3e_get_huge_pfn(const pml3e_t *pml3)
Definition paging.c:259
#define MOS_MAX_MEMREGIONS
Definition autoconf.h:25
#define MOS_MAX_CMDLINE_COUNT
Definition autoconf.h:24
MOSAPI void(1, 2) fatal_abort(const char *fmt
list_node_t list_head
A linked list head.
Definition list.h:23
ipi_type_t
The type of IPI to send.
Definition ipi.h:12
const char ** argv
Definition kmain.c:44
size_t argc
Definition kmain.c:43
@ MEM_PERM_EXEC
Definition mm_types.h:11
@ MEM_PERM_READ
Definition mm_types.h:9
@ MEM_PERM_WRITE
Definition mm_types.h:10
#define __END_DECLS
Definition mos_global.h:23
#define MOS_ENUM_OPERATORS(_enum)
Definition mos_global.h:173
#define __BEGIN_DECLS
Definition mos_global.h:22
#define current
void platform_interrupt_disable(void)
u32 platform_current_cpu_id(void)
void platform_context_setup_child_thread(thread_t *thread, thread_entry_t entry, void *arg)
void platform_switch_mm(const mm_context_t *new_mm)
void platform_dump_regs(platform_regs_t *regs)
const char __MOS_KERNEL_RODATA_START[]
u64 platform_arch_syscall(u64 syscall, u64 arg1, u64 arg2, u64 arg3, u64 arg4)
void(* irq_handler)(u32 irq)
Definition platform.h:34
void platform_return_to_userspace(platform_regs_t *regs)
void platform_pml1e_set_pfn(pml1e_t *pml1, pfn_t pfn)
Definition mm.c:83
void platform_startup_setup_kernel_mm()
#define PER_CPU_DECLARE(type, name)
Definition platform.h:23
void platform_interrupt_enable(void)
__BEGIN_DECLS const char __MOS_KERNEL_CODE_START[]
void platform_syscall_setup_restart_context(platform_regs_t *regs, reg_t syscall_nr)
void platform_restore_from_signal_handler(void *sp)
void platform_context_clone(const thread_t *from, thread_t *to)
void platform_dump_thread_kernel_stack(const thread_t *thread)
void platform_shutdown(void)
vm_flags platform_pml1e_get_flags(const pml1e_t *pml1e)
Definition mm.c:101
__BEGIN_DECLS const char __MOS_KERNEL_CODE_END[]
Definition platform.h:147
void platform_jump_to_signal_handler(const platform_regs_t *regs, const sigreturn_data_t *sigreturn_data, const sigaction_t *sa)
void platform_ap_entry(u64 arg)
Definition ap_entry.c:23
u64 platform_get_timestamp(void)
const char __MOS_KERNEL_RW_START[]
void platform_startup_late()
platform_regs_t * platform_thread_regs(const thread_t *thread)
void platform_invalidate_tlb(ptr_t vaddr)
Definition mm.c:72
const char __MOS_KERNEL_RODATA_END[]
Definition platform.h:148
void platform_ipi_send(u8 target_cpu, ipi_type_t type)
vm_flags
Definition platform.h:40
@ VM_USER_RW
Definition platform.h:54
@ VM_USER_RX
Definition platform.h:55
@ VM_GLOBAL
Definition platform.h:48
@ VM_RX
Definition platform.h:52
@ VM_CACHE_DISABLED
Definition platform.h:47
@ VM_READ
Definition platform.h:41
@ VM_RWX
Definition platform.h:53
@ VM_WRITE_THROUGH
Definition platform.h:46
@ VM_USER_RWX
Definition platform.h:57
@ VM_EXEC
Definition platform.h:43
@ VM_WRITE
Definition platform.h:42
@ VM_RW
Definition platform.h:51
@ VM_USER_RO
Definition platform.h:56
@ VM_USER
Definition platform.h:45
pfn_t platform_pml1e_get_pfn(const pml1e_t *pml1)
Definition mm.c:78
void platform_pml1e_set_flags(pml1e_t *pml1, vm_flags flags)
Definition mm.c:95
datetime_str_t * platform_get_datetime_str(void)
void platform_syscall_store_retval(platform_regs_t *regs, reg_t result)
void platform_cpu_idle(void)
void platform_get_time(timeval_t *val)
bool platform_pml1e_get_present(const pml1e_t *pml1)
Definition mm.c:90
void mos_start_kernel(void)
Definition kmain.c:102
char datetime_str_t[32]
Definition platform.h:184
void platform_startup_early()
void platform_context_setup_main_thread(thread_t *thread, ptr_t entry, ptr_t sp, int argc, ptr_t argv, ptr_t envp)
thread_state_t
Definition platform.h:61
@ THREAD_STATE_READY
thread can be scheduled
Definition platform.h:63
@ THREAD_STATE_RUNNING
thread is currently running
Definition platform.h:64
@ THREAD_STATE_NONINTERRUPTIBLE
thread is blocked, and cannot be interrupted
Definition platform.h:66
@ THREAD_STATE_DEAD
thread is dead, and will be cleaned up soon by the scheduler
Definition platform.h:67
@ THREAD_STATE_BLOCKED
thread is blocked by a wait condition
Definition platform.h:65
@ THREAD_STATE_CREATED
created or forked, but not ever started
Definition platform.h:62
void platform_context_cleanup(thread_t *thread)
mos_platform_info_t *const platform_info
void platform_halt_cpu(void)
Definition mm.c:65
const char __MOS_KERNEL_END[]
void platform_dump_stack(platform_regs_t *regs)
void platform_dump_current_stack()
switch_flags_t
Definition platform.h:71
@ SWITCH_REGULAR
Definition platform.h:72
@ SWITCH_TO_NEW_KERNEL_THREAD
Definition platform.h:74
@ SWITCH_TO_NEW_USER_THREAD
Definition platform.h:73
void platform_switch_to_thread(thread_t *current, thread_t *new_thread, switch_flags_t switch_flags)
const char __MOS_KERNEL_RW_END[]
Definition platform.h:149
pml3e_t pml4e_t
Definition pml_types.h:72
pml2e_t pml3e_t
Definition pml_types.h:60
thread_t * thread
Definition platform.h:91
platform_regs_t * interrupt_regs
the registers of whatever interrupted this CPU
Definition platform.h:94
mm_context_t * mm_context
Definition platform.h:93
ptr_t scheduler_stack
Definition platform.h:92
thread_t * idle_thread
idle thread for this CPU
Definition platform.h:96
u32 id
Definition platform.h:90
platform_cpuinfo_t cpuinfo
Definition platform.h:95
spinlock_t mm_lock
protects [pgd] and the [mmaps] list (the list itself, not the vmap_t objects)
Definition platform.h:81
list_head mmaps
Definition platform.h:83
pgd_t pgd
Definition platform.h:82
console_t * boot_console
Definition platform.h:134
platform_arch_info_t arch_info
Definition platform.h:132
mm_context_t * kernel_mm
Definition platform.h:118
uintn reg_t
Definition types.h:51
unsigned int u32
Definition types.h:21
unsigned long long pfn_t
Definition types.h:41
void(* thread_entry_t)(void *arg)
Definition types.h:109
unsigned short u16
Definition types.h:20
unsigned long ptr_t
Definition types.h:25
unsigned long long u64
Definition types.h:23
unsigned char u8
Definition types.h:19