Squash commits for public release

This commit is contained in:
2025-02-12 09:54:05 -05:00
commit 7118adc514
1108 changed files with 80873 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
#ifndef _KERNEL_PLATFORM_ARM64_CPUINFO_H
#define _KERNEL_PLATFORM_ARM64_CPUINFO_H
#include <libkern/c_attrs.h>
#include <libkern/types.h>
#include <platform/generic/cpu.h>
static inline bool cpuinfo_has_1gb_pages()
{
return true;
}
#endif // _KERNEL_PLATFORM_ARM64_CPUINFO_H

View File

@@ -0,0 +1,44 @@
#ifndef _KERNEL_PLATFORM_ARM64_FPU_FPU_H
#define _KERNEL_PLATFORM_ARM64_FPU_FPU_H
#include <libkern/kassert.h>
#include <libkern/types.h>
#include <platform/arm64/registers.h>
#define FPU_STATE_ALIGNMENT (32)
typedef struct {
uint64_t halfv[64];
} __attribute__((aligned(FPU_STATE_ALIGNMENT))) fpu_state_t;
void fpu_install();
void fpu_init_state(fpu_state_t* new_fpu_state);
extern void fpu_save(void*);
extern void fpu_restore(void*);
static inline void fpu_enable()
{
}
static inline void fpu_disable()
{
}
static inline int fpu_is_avail()
{
return (((read_cpacr() >> 20) & 0b11) == 0b11);
}
static inline void fpu_make_avail()
{
write_cpacr(read_cpacr() | ((0b11) << 20));
}
static inline void fpu_make_unavail()
{
// Simply turn it off to make it unavailble.
uint64_t val = read_cpacr() & (~((3ull) << 20));
write_cpacr(val | ((0b01) << 20));
}
#endif //_KERNEL_PLATFORM_ARM64_FPU_FPU_H

View File

@@ -0,0 +1,10 @@
#ifndef _KERNEL_PLATFORM_ARM64_INIT_H
#define _KERNEL_PLATFORM_ARM64_INIT_H
#include <libkern/types.h>
void platform_init_boot_cpu();
void platform_setup_boot_cpu();
void platform_setup_secondary_cpu();
#endif /* _KERNEL_PLATFORM_ARM64_INIT_H */

View File

@@ -0,0 +1,14 @@
#ifndef _KERNEL_PLATFORM_ARM64_INTERRUPTS_H
#define _KERNEL_PLATFORM_ARM64_INTERRUPTS_H
#include <drivers/irq/irq_api.h>
#include <libkern/mask.h>
#include <libkern/types.h>
void interrupts_setup();
void interrupts_setup_secondary_cpu();
void gic_setup();
void gic_setup_secondary_cpu();
#endif /* _KERNEL_PLATFORM_ARM64_INTERRUPTS_H */

View File

@@ -0,0 +1,8 @@
#ifndef _KERNEL_PLATFORM_ARM64_PMM_SETTINGS_H
#define _KERNEL_PLATFORM_ARM64_PMM_SETTINGS_H
#define PMM_BLOCK_SIZE (4096)
#define PMM_BLOCK_SIZE_KB (4)
#define PMM_BLOCKS_PER_BYTE (8)
#endif /* _KERNEL_PLATFORM_X86_PMM_SETTINGS_H */

View File

@@ -0,0 +1,63 @@
{
uint64_t el;
asm volatile("mrs %x0, CurrentEL"
: "=r"(el)
:);
return el >> 2;
}
static inline uint64_t read_sp()
{
uint64_t sp;
asm volatile("mov %x0, sp"
: "=r"(sp)
:);
return sp;
}
static inline uint64_t read_fp()
{
uint64_t fp;
asm volatile("mov %x0, x29"
: "=r"(fp)
:);
return fp;
}
static inline uint64_t read_cbar()
{
uint32_t val;
asm volatile("mrs %x0, S3_1_C15_C3_0"
: "=r"(val)
:);
return val;
}
static inline uint64_t read_cpacr()
{
uint64_t cpacr;
asm volatile("mrs %x0, CPACR_EL1"
: "=r"(cpacr)
:);
return cpacr;
}
static inline void write_cpacr(uint64_t val)
{
asm volatile("msr CPACR_EL1, %x0"
:
: "r"(val)
: "memory");
asm volatile("isb");
}
static inline void write_tpidr(uint64_t val)
{
asm volatile("msr TPIDR_EL1, %x0"
:
: "r"(val)
: "memory");
asm volatile("isb");
}
#endif /* _KERNEL_PLATFORM_ARM64_REGISTERS_H */

View File

@@ -0,0 +1,9 @@
#define SYSCALL_VAR3(tf) (tf->x[2])
#define SYSCALL_VAR4(tf) (tf->x[3])
#define SYSCALL_VAR5(tf) (tf->x[4])
#define return_val (tf->x[0])
#define return_with_val(val) \
(return_val = val); \
return
#endif // _KERNEL_PLATFORM_ARM64_SYSCALLS_PARAMS_H

View File

@@ -0,0 +1,111 @@
#ifndef _KERNEL_PLATFORM_ARM64_SYSTEM_H
#define _KERNEL_PLATFORM_ARM64_SYSTEM_H
#include <libkern/c_attrs.h>
#include <libkern/types.h>
#include <platform/generic/registers.h>
/**
* INTS
*/
void system_disable_interrupts();
void system_enable_interrupts();
void system_enable_interrupts_only_counter();
inline static void system_instruction_barrier()
{
asm volatile("isb");
}
inline static void system_data_synchronise_barrier()
{
asm volatile("dsb sy");
}
inline static void system_data_memory_barrier()
{
asm volatile("dmb sy");
}
inline static void system_disable_interrupts_no_counter()
{
asm volatile("msr daifset, #0xf");
system_instruction_barrier();
}
inline static void system_enable_interrupts_no_counter()
{
asm volatile("msr daifclr, #0xf");
system_instruction_barrier();
}
/**
* PAGING
*/
extern void system_set_pdir(uintptr_t pdir0, uintptr_t pdir1);
inline static void system_flush_local_tlb_entry(uintptr_t vaddr)
{
asm volatile("isb");
asm volatile("tlbi vmalle1\n");
asm volatile("dsb sy");
}
inline static void system_flush_all_cpus_tlb_entry(uintptr_t vaddr)
{
asm volatile("isb");
asm volatile("tlbi vmalle1\n");
asm volatile("dsb sy");
}
inline static void system_flush_whole_tlb()
{
asm volatile("isb");
asm volatile("tlbi vmalle1\n");
asm volatile("dsb sy");
}
inline static void system_enable_write_protect()
{
}
inline static void system_disable_write_protect()
{
}
inline static void system_enable_paging()
{
}
inline static void system_disable_paging()
{
}
inline static void system_stop_until_interrupt()
{
asm volatile("wfi");
}
NORETURN inline static void system_stop()
{
system_disable_interrupts();
system_stop_until_interrupt();
while (1) { }
}
void system_cache_invalidate(void* addr, size_t size);
void system_cache_clean_and_invalidate(void* addr, size_t size);
void system_cache_clean(void* addr, size_t size);
/**
* CPU
*/
inline static int system_cpu_id()
{
return 0;
}
#endif /* _KERNEL_PLATFORM_ARM64_SYSTEM_H */

View File

@@ -0,0 +1,22 @@
#ifndef _KERNEL_PLATFORM_ARM64_TASKING_CONTEXT_H
#define _KERNEL_PLATFORM_ARM64_TASKING_CONTEXT_H
#include <libkern/c_attrs.h>
#include <libkern/types.h>
typedef struct {
uint64_t x[22];
uint64_t lr;
} PACKED context_t;
static inline uintptr_t context_get_instruction_pointer(context_t* ctx)
{
return ctx->lr;
}
static inline void context_set_instruction_pointer(context_t* ctx, uintptr_t ip)
{
ctx->lr = ip;
}
#endif // _KERNEL_PLATFORM_ARM64_TASKING_CONTEXT_H

View File

@@ -0,0 +1,12 @@
#ifndef _KERNEL_PLATFORM_ARM64_TASKING_DUMP_IMPL_H
#define _KERNEL_PLATFORM_ARM64_TASKING_DUMP_IMPL_H
#include <libkern/types.h>
#include <tasking/bits/dump.h>
#include <tasking/tasking.h>
int dump_impl(dump_data_t* data);
int dump_kernel_impl(dump_data_t* dump_data, const char* err_desc);
int dump_kernel_impl_from_tf(dump_data_t* dump_data, const char* err_desc, trapframe_t* tf);
#endif // _KERNEL_PLATFORM_ARM64_TASKING_DUMP_IMPL_H

View File

@@ -0,0 +1,10 @@
#ifndef _KERNEL_PLATFORM_ARM64_TASKING_SIGNAL_IMPL_H
#define _KERNEL_PLATFORM_ARM64_TASKING_SIGNAL_IMPL_H
#include <libkern/types.h>
struct thread;
int signal_impl_prepare_stack(struct thread* thread, int signo, uintptr_t old_sp, uintptr_t magic);
int signal_impl_restore_stack(struct thread* thread, uintptr_t* old_sp, uintptr_t* magic);
#endif // _KERNEL_PLATFORM_ARM64_TASKING_SIGNAL_IMPL_H

View File

@@ -0,0 +1,103 @@
#ifndef _KERNEL_PLATFORM_ARM64_TASKING_TRAPFRAME_H
#define _KERNEL_PLATFORM_ARM64_TASKING_TRAPFRAME_H
#include <libkern/c_attrs.h>
#include <libkern/log.h>
#include <libkern/types.h>
typedef struct {
uint64_t x[31];
uint64_t esr;
uint64_t elr;
uint64_t far;
uint64_t spsr;
uint64_t sp;
} PACKED trapframe_t;
static inline uintptr_t get_stack_pointer(trapframe_t* tf)
{
return tf->sp;
}
static inline void set_stack_pointer(trapframe_t* tf, uintptr_t sp)
{
tf->sp = sp;
}
static inline uintptr_t get_frame_pointer(trapframe_t* tf)
{
return tf->x[29];
}
static inline void set_frame_pointer(trapframe_t* tf, uintptr_t bp)
{
tf->x[29] = bp;
}
static inline uintptr_t get_instruction_pointer(trapframe_t* tf)
{
return tf->elr;
}
static inline void set_instruction_pointer(trapframe_t* tf, uintptr_t ip)
{
tf->elr = ip;
}
static inline uintptr_t get_syscall_result(trapframe_t* tf)
{
return tf->x[0];
}
static inline void set_syscall_result(trapframe_t* tf, uintptr_t val)
{
tf->x[0] = val;
}
/**
* STACK FUNCTIONS
*/
static inline void tf_push_to_stack(trapframe_t* tf, uintptr_t val)
{
tf->sp -= sizeof(uintptr_t);
*((uintptr_t*)tf->sp) = val;
}
static inline uintptr_t tf_pop_to_stack(trapframe_t* tf)
{
uintptr_t val = *((uintptr_t*)tf->sp);
tf->sp += sizeof(uintptr_t);
return val;
}
static inline void tf_move_stack_pointer(trapframe_t* tf, int32_t val)
{
tf->sp += val;
}
static inline void tf_setup_as_user_thread(trapframe_t* tf)
{
tf->spsr = 0x0;
}
static inline void tf_setup_as_kernel_thread(trapframe_t* tf)
{
tf->spsr = 0x4;
}
static void dump_tf(trapframe_t* tf)
{
for (int i = 0; i < 31; i++) {
log("x[%d]: %zx", i, tf->x[i]);
}
log("tf: %p", tf);
log("sp: %zx", tf->sp);
log("ip: %zx", tf->elr);
log("fl: %zx", tf->spsr);
log("far: %zx", tf->far);
log("esr: %zx", tf->esr);
}
#endif // _KERNEL_PLATFORM_ARM64_TASKING_TRAPFRAME_H

View File

@@ -0,0 +1,66 @@
#ifndef _KERNEL_PLATFORM_ARM64_VMM_CONSTS_H
#define _KERNEL_PLATFORM_ARM64_VMM_CONSTS_H
#include <libkern/types.h>
static inline int vm_page_size()
{
return 0x1000;
}
static inline int vm_page_mask()
{
return 0xfff;
}
#define VMM_LV0_ENTITY_COUNT (512)
#define VMM_LV1_ENTITY_COUNT (512)
#define VMM_LV2_ENTITY_COUNT (512)
#define VMM_LV3_ENTITY_COUNT (512)
#define VMM_PAGE_SIZE (vm_page_size())
#define PAGE_START(vaddr) ((vaddr & (~(uintptr_t)vm_page_mask())))
#define FRAME(addr) (addr / VMM_PAGE_SIZE)
#define PTABLE_LV_TOP (2)
#define PTABLE_LV0_VADDR_OFFSET (12)
#define PTABLE_LV1_VADDR_OFFSET (21)
#define PTABLE_LV2_VADDR_OFFSET (30)
#define PTABLE_LV3_VADDR_OFFSET (39)
// Since arm64 uses double-table setup, this values are not used.
#define PTABLE_TOP_KERNEL_OFFSET VMM_LV0_ENTITY_COUNT
#define USER_HIGH 0x1fffffffff
#define KERNEL_BASE 0xffffff8000000000
#define KERNEL_PADDR_BASE 0xffffffff00000000 // up to 4gbs are supported.
#define KERNEL_KASAN_BASE 0xfffffff000000000
#define KERNEL_KASAN_SIZE (128 << 20) // 128MB for kasan covers 1GB of kernel space. For current need this is more than enough.
// For Apl
// static inline int get_page_size()
// {
// return 0x4000;
// }
// static inline int get_page_mask()
// {
// return 0x3fff;
// }
// #define VMM_LV0_ENTITY_COUNT (2048)
// #define VMM_LV1_ENTITY_COUNT (2048)
// #define VMM_LV2_ENTITY_COUNT (2048)
// #define VMM_LV3_ENTITY_COUNT (2)
// #define VMM_PAGE_SIZE (get_page_size())
// #define PAGE_START(vaddr) ((vaddr & ~(uintptr_t)get_page_mask())
// #define FRAME(addr) (addr / VMM_PAGE_SIZE)
// #define PTABLE_LV_TOP (3)
// #define PTABLE_LV0_VADDR_OFFSET (14)
// #define PTABLE_LV1_VADDR_OFFSET (25)
// #define PTABLE_LV2_VADDR_OFFSET (36)
// #define PTABLE_LV3_VADDR_OFFSET (47)
#endif //_KERNEL_PLATFORM_ARM64_VMM_CONSTS_H

View File

@@ -0,0 +1,30 @@
#ifndef _KERNEL_PLATFORM_ARM64_VMM_MMU_H
#define _KERNEL_PLATFORM_ARM64_VMM_MMU_H
#include <libkern/types.h>
#include <mem/bits/mmu.h>
// arm64 uses 2 tables to manage virtual space.
#define DOUBLE_TABLE_PAGING
typedef uint64_t ptable_entity_t;
typedef uint64_t arch_pf_info_t;
ptable_entity_t vm_mmu_to_arch_flags(mmu_flags_t mmu_flags, ptable_lv_t lv);
mmu_flags_t vm_arch_to_mmu_flags(ptable_entity_t* entity, ptable_lv_t lv);
mmu_pf_info_flags_t vm_arch_parse_pf_info(arch_pf_info_t info);
void vm_ptable_entity_set_default_flags(ptable_entity_t* entity, ptable_lv_t lv);
void vm_ptable_entity_allocated(ptable_entity_t* entity, ptable_lv_t lv);
void vm_ptable_entity_invalidate(ptable_entity_t* entity, ptable_lv_t lv);
void vm_ptable_entity_set_mmu_flags(ptable_entity_t* entity, ptable_lv_t lv, mmu_flags_t mmu_flags);
void vm_ptable_entity_rm_mmu_flags(ptable_entity_t* entity, ptable_lv_t lv, mmu_flags_t mmu_flags);
void vm_ptable_entity_set_frame(ptable_entity_t* entity, ptable_lv_t lv, uintptr_t frame);
uintptr_t vm_ptable_entity_get_frame(ptable_entity_t* entity, ptable_lv_t lv);
ptable_state_t vm_ptable_entity_state(ptable_entity_t* entity, ptable_lv_t lv);
bool vm_ptable_entity_is_present(ptable_entity_t* entity, ptable_lv_t lv);
bool vm_ptable_entity_is_only_allocated(ptable_entity_t* entity, ptable_lv_t lv);
#endif // _KERNEL_PLATFORM_ARM64_VMM_MMU_H