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,14 @@
#ifndef _KERNEL_PLATFORM_ARM32_CPUINFO_H
#define _KERNEL_PLATFORM_ARM32_CPUINFO_H
#include <libkern/c_attrs.h>
#include <libkern/types.h>
#include <platform/generic/cpu.h>
static inline bool cpuinfo_has_1gb_pages()
{
// Only for 64bits.
return false;
}
#endif // _KERNEL_PLATFORM_ARM32_CPUINFO_H

View File

@@ -0,0 +1,51 @@
#ifndef _KERNEL_PLATFORM_ARM32_FPU_FPUV4_H
#define _KERNEL_PLATFORM_ARM32_FPU_FPUV4_H
#include <drivers/driver_manager.h>
#include <libkern/mask.h>
#include <libkern/types.h>
#include <platform/arm32/interrupts.h>
#include <platform/arm32/registers.h>
#include <platform/arm32/target/cortex-a15/device_settings.h>
#define FPU_STATE_ALIGNMENT (16)
typedef struct {
uint64_t d[32];
} __attribute__((aligned(FPU_STATE_ALIGNMENT))) fpu_state_t;
void fpuv4_install();
void fpu_init_state(fpu_state_t* new_fpu_state);
extern uint32_t read_fpexc();
extern void write_fpexc(uint32_t);
extern void fpu_save(void*);
extern void fpu_restore(void*);
static inline void fpu_enable()
{
write_fpexc(read_fpexc() | (1 << 30));
}
static inline void fpu_disable()
{
write_fpexc(read_fpexc() & (~(1 << 30)));
}
static inline int fpu_is_avail()
{
return (((read_cpacr() >> 20) & 0b1111) == 0b1111);
}
static inline void fpu_make_avail()
{
write_cpacr(read_cpacr() | ((0b1111) << 20));
}
static inline void fpu_make_unavail()
{
// Simply turn it off to make it unavailble.
uint32_t val = read_cpacr() & (~((0b1111) << 20));
write_cpacr(val | ((0b0101) << 20));
}
#endif //_KERNEL_PLATFORM_ARM32_FPU_FPUV4_H

View File

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

View File

@@ -0,0 +1,33 @@
#ifndef _KERNEL_PLATFORM_ARM32_INTERRUPTS_H
#define _KERNEL_PLATFORM_ARM32_INTERRUPTS_H
#include <drivers/irq/irq_api.h>
#include <libkern/mask.h>
#include <libkern/types.h>
void interrupts_setup();
void interrupts_setup_secondary_cpu();
extern char STACK_ABORT_TOP;
extern char STACK_UNDEFINED_TOP;
extern char STACK_IRQ_TOP;
extern char STACK_SVC_TOP;
extern char STACK_TOP;
extern void swi(uint32_t num);
extern void set_svc_stack(uint32_t stack);
extern void set_irq_stack(uint32_t stack);
extern void set_abort_stack(uint32_t stack);
extern void set_undefined_stack(uint32_t stack);
extern void undefined_handler(trapframe_t* tf);
extern void svc_handler(trapframe_t* tf);
extern void prefetch_abort_handler(trapframe_t* tf);
extern void data_abort_handler(trapframe_t* tf);
extern void irq_handler(trapframe_t* tf);
extern void fast_irq_handler(trapframe_t* tf);
void gic_setup();
void gic_setup_secondary_cpu();
#endif /* _KERNEL_PLATFORM_ARM32_INTERRUPTS_H */

View File

@@ -0,0 +1,10 @@
#ifndef _KERNEL_PLATFORM_ARM32_PMM_SETTINGS_H
#define _KERNEL_PLATFORM_ARM32_PMM_SETTINGS_H
#define KERNEL_PM_BASE 0x80100000
#define PMM_BLOCK_SIZE (1024)
#define PMM_BLOCK_SIZE_KB (1)
#define PMM_BLOCKS_PER_BYTE (8)
#endif /* _KERNEL_PLATFORM_ARM32_PMM_SETTINGS_H */

View File

@@ -0,0 +1,175 @@
#ifndef _KERNEL_PLATFORM_ARM32_REGISTERS_H
#define _KERNEL_PLATFORM_ARM32_REGISTERS_H
#include <libkern/types.h>
#include <platform/arm32/system.h>
static inline uint32_t extract_bits(uint32_t a, int bottom, int top)
{
int diff = top - bottom + 1;
return (a >> bottom) & ((1 << (diff)) - 1);
}
static inline uint32_t read_r3()
{
uint32_t val;
asm volatile("mov %0, r3"
: "=r"(val)
:);
return val;
}
static inline uint32_t read_far()
{
uint32_t val;
asm volatile("mrc p15, 0, %0, c6, c0, 0"
: "=r"(val)
:);
return val;
}
static inline uint32_t read_cbar()
{
uint32_t val;
asm volatile("mrc p15, 4, %0, c15, c0, 0"
: "=r"(val)
:);
return val;
}
static inline uint32_t read_ifsr()
{
uint32_t val;
asm volatile("mrc p15, 0, %0, c5, c0, 1"
: "=r"(val)
:);
return val;
}
static inline uint32_t read_dfsr()
{
uint32_t val;
asm volatile("mrc p15, 0, %0, c5, c0, 0"
: "=r"(val)
:);
return val;
}
static inline uint32_t read_cpsr()
{
uint32_t cpsr;
asm volatile("mrs %0, cpsr"
: "=r"(cpsr)
:);
return cpsr;
}
static inline uint32_t read_spsr()
{
uint32_t spsr;
asm volatile("mrs %0, spsr"
: "=r"(spsr)
:);
return spsr;
}
static inline uint32_t read_cpacr()
{
uint32_t cpacr;
asm volatile("mrc p15, 0, %0, c1, c0, 2"
: "=r"(cpacr)
:);
return cpacr;
}
static inline void write_cpacr(uint32_t val)
{
asm volatile("mcr p15, 0, %0, c1, c0, 2"
:
: "r"(val)
: "memory");
system_instruction_barrier();
}
static inline uint32_t read_nsacr()
{
uint32_t cpacr;
asm volatile("mrc p15, 0, %0, c1, c1, 2"
: "=r"(cpacr)
:);
return cpacr;
}
static inline void write_nsacr(uint32_t val)
{
asm volatile("mcr p15, 0, %0, c1, c1, 2"
:
: "r"(val)
: "memory");
system_instruction_barrier();
}
static inline uint32_t read_hcptr()
{
uint32_t cpacr;
asm volatile("mrc p15, 4, %0, c1, c1, 2"
: "=r"(cpacr)
:);
return cpacr;
}
static inline void write_hcptr(uint32_t val)
{
asm volatile("mcr p15, 4, %0, c1, c1, 2"
:
: "r"(val)
: "memory");
system_instruction_barrier();
}
static inline uint32_t read_cpu_id_register()
{
uint32_t res;
asm volatile("mrc p15, 0, %0, c0, c0, 5"
: "=r"(res)
:);
return res;
}
static inline uint32_t read_midr()
{
uint32_t res;
asm volatile("mrc p15, 0, %0, c0, c0, 0"
: "=r"(res)
:);
return res;
}
static inline uint32_t read_actlr()
{
uint32_t res;
asm volatile("mrc p15, 0, %0, c1, c0, 1"
: "=r"(res)
:);
return res;
}
static inline void write_actlr(uint32_t val)
{
asm volatile("mcr p15, 0, %0, c1, c0, 1"
:
: "r"(val)
: "memory");
system_instruction_barrier();
}
static inline void write_iciallu(uint32_t val)
{
asm volatile("mcr p15, 0, %0, c7, c5, 0"
:
: "r"(val)
: "memory");
system_instruction_barrier();
}
#endif /* _KERNEL_PLATFORM_ARM32_REGISTERS_H */

View File

@@ -0,0 +1,17 @@
#ifndef _KERNEL_PLATFORM_ARM32_SYSCALLS_PARAMS_H
#define _KERNEL_PLATFORM_ARM32_SYSCALLS_PARAMS_H
#include <platform/arm32/tasking/trapframe.h>
#define SYSCALL_ID(tf) (tf->r[7])
#define SYSCALL_VAR1(tf) (tf->r[0])
#define SYSCALL_VAR2(tf) (tf->r[1])
#define SYSCALL_VAR3(tf) (tf->r[2])
#define SYSCALL_VAR4(tf) (tf->r[3])
#define SYSCALL_VAR5(tf) (tf->r[4])
#define return_val (tf->r[0])
#define return_with_val(val) \
(return_val = val); \
return
#endif // _KERNEL_PLATFORM_ARM32_SYSCALLS_PARAMS_H

View File

@@ -0,0 +1,132 @@
#ifndef _KERNEL_PLATFORM_ARM32_SYSTEM_H
#define _KERNEL_PLATFORM_ARM32_SYSTEM_H
#include <libkern/c_attrs.h>
#include <libkern/types.h>
/**
* INTS
*/
void system_disable_interrupts();
void system_enable_interrupts();
void system_enable_interrupts_only_counter();
inline static void system_disable_interrupts_no_counter() { asm volatile("cpsid i"); }
inline static void system_enable_interrupts_no_counter() { asm volatile("cpsie i"); }
/**
* PAGING
*/
inline static void system_instruction_barrier()
{
asm volatile("isb");
}
inline static void system_data_synchronise_barrier()
{
asm volatile("dsb ISH");
}
inline static void system_data_memory_barrier()
{
asm volatile("dmb ISH");
}
inline static void system_flush_local_tlb_entry(uintptr_t vaddr)
{
system_data_synchronise_barrier();
asm volatile("mcr p15, 0, %0, c8, c7, 3"
:
: "r"(vaddr)
: "memory");
system_data_synchronise_barrier();
system_instruction_barrier();
}
inline static void system_flush_all_cpus_tlb_entry(uintptr_t vaddr)
{
system_data_synchronise_barrier();
asm volatile("mcr p15, 0, %0, c8, c3, 3"
:
: "r"(vaddr)
: "memory");
system_data_synchronise_barrier();
system_instruction_barrier();
}
inline static void system_flush_whole_tlb()
{
asm volatile("mcr p15, 0, %0, c8, c7, 0"
:
: "r"(0)
: "memory");
system_data_synchronise_barrier();
}
inline static void system_set_pdir(uintptr_t pdir0, uintptr_t pdir1)
{
system_data_synchronise_barrier();
asm volatile("mcr p15, 0, %0, c2, c0, 0"
:
: "r"(pdir0)
: "memory");
system_flush_whole_tlb();
}
inline static void system_enable_write_protect()
{
}
inline static void system_disable_write_protect()
{
}
inline static void system_enable_paging()
{
uint32_t val;
asm volatile("mrc p15, 0, %0, c1, c0, 0"
: "=r"(val));
asm volatile("orr %0, %1, #0x1"
: "=r"(val)
: "r"(val));
asm volatile("mcr p15, 0, %0, c1, c0, 0" ::"r"(val)
: "memory");
system_instruction_barrier();
}
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) { }
}
/**
* CPU
*/
void system_cache_clean_and_invalidate(void* addr, size_t size);
void system_cache_invalidate(void* addr, size_t size);
void system_cache_clean(void* addr, size_t size);
inline static int system_cpu_id()
{
// inlined read_cpu_id_register();
uint32_t res;
asm volatile("mrc p15, 0, %0, c0, c0, 5"
: "=r"(res)
:);
return res & 0x3;
}
#endif /* _KERNEL_PLATFORM_ARM32_SYSTEM_H */

View File

@@ -0,0 +1,43 @@
#ifndef _KERNEL_PLATFORM_ARM32_TARGET_CORTEX_A15_DEVICE_SETTINGS_H
#define _KERNEL_PLATFORM_ARM32_TARGET_CORTEX_A15_DEVICE_SETTINGS_H
/**
* Used devices:
* uart
* gicv2
* sp804
* pl181
* pl111
* pl050
* pl031
*/
/* Base is read from CBAR */
#define GICv2_DISTRIBUTOR_OFFSET 0x1000
#define GICv2_CPU_INTERFACE_OFFSET 0x2000
#define UART_BASE 0x1c090000
#define SP804_BASE 0x1c110000
#define PL181_BASE 0x1c050000
#define PL111_BASE 0x1c1f0000
#define PL050_KEYBOARD_BASE 0x1c060000
#define PL050_MOUSE_BASE 0x1c070000
#define PL031_BASE 0x1c170000
/**
* Interrupt lines:
* SP804 TIMER1: 2nd line in SPI (32+2)
*/
#define SP804_TIMER1_IRQ_LINE (32 + 2)
#define PL050_KEYBOARD_IRQ_LINE (32 + 12)
#define PL050_MOUSE_IRQ_LINE (32 + 13)
#endif /* _KERNEL_PLATFORM_ARM32_TARGET_CORTEX_A15_DEVICE_SETTINGS_H */

View File

@@ -0,0 +1,8 @@
#ifndef _KERNEL_PLATFORM_ARM32_TARGET_CORTEX_A15_MEMMAP_H
#define _KERNEL_PLATFORM_ARM32_TARGET_CORTEX_A15_MEMMAP_H
#define KMALLOC_BASE (KERNEL_BASE + 0x400000)
extern struct memory_map* arm_memmap;
#endif /* _KERNEL_PLATFORM_ARM32_TARGET_CORTEX_A15_MEMMAP_H */

View File

@@ -0,0 +1,6 @@
#ifndef _KERNEL_PLATFORM_ARM32_TARGET_CPU_PART_NUMBERS_H
#define _KERNEL_PLATFORM_ARM32_TARGET_CPU_PART_NUMBERS_H
#define PART_NUMBER_CORTEX_A15 (0xC0F)
#endif // _KERNEL_PLATFORM_ARM32_TARGET_CPU_PART_NUMBERS_H

View File

@@ -0,0 +1,6 @@
#ifndef _KERNEL_PLATFORM_ARM32_TARGET_GENERAL_CORE_SETUP_H
#define _KERNEL_PLATFORM_ARM32_TARGET_GENERAL_CORE_SETUP_H
int cortex_a15_setup();
#endif // _KERNEL_PLATFORM_ARM32_TARGET_GENERAL_CORE_SETUP_H

View File

@@ -0,0 +1,22 @@
#ifndef _KERNEL_PLATFORM_ARM32_TASKING_CONTEXT_H
#define _KERNEL_PLATFORM_ARM32_TASKING_CONTEXT_H
#include <libkern/c_attrs.h>
#include <libkern/types.h>
typedef struct {
uint32_t r[9];
uint32_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_ARM32_TASKING_CONTEXT_H

View File

@@ -0,0 +1,12 @@
#ifndef _KERNEL_PLATFORM_ARM32_TASKING_DUMP_IMPL_H
#define _KERNEL_PLATFORM_ARM32_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_ARM32_TASKING_DUMP_IMPL_H

View File

@@ -0,0 +1,10 @@
#ifndef _KERNEL_PLATFORM_ARM32_TASKING_SIGNAL_IMPL_H
#define _KERNEL_PLATFORM_ARM32_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_ARM32_TASKING_SIGNAL_IMPL_H

View File

@@ -0,0 +1,107 @@
#ifndef _KERNEL_PLATFORM_ARM32_TASKING_TRAPFRAME_H
#define _KERNEL_PLATFORM_ARM32_TASKING_TRAPFRAME_H
#include <libkern/c_attrs.h>
#include <libkern/log.h>
#include <libkern/types.h>
#define CPSR_M_USR 0x10U
#define CPSR_M_FIQ 0x11U
#define CPSR_M_IRQ 0x12U
#define CPSR_M_SVC 0x13U
#define CPSR_M_MON 0x16U
#define CPSR_M_ABT 0x17U
#define CPSR_M_HYP 0x1AU
#define CPSR_M_UND 0x1BU
#define CPSR_M_SYS 0x1FU
typedef struct {
uint32_t user_flags;
uint32_t user_sp;
uint32_t user_lr;
uint32_t r[13];
uint32_t user_ip;
} PACKED trapframe_t;
static inline uintptr_t get_stack_pointer(trapframe_t* tf)
{
return tf->user_sp;
}
static inline void set_stack_pointer(trapframe_t* tf, uintptr_t sp)
{
tf->user_sp = sp;
}
static inline uintptr_t get_frame_pointer(trapframe_t* tf)
{
return 0;
}
static inline void set_frame_pointer(trapframe_t* tf, uintptr_t bp)
{
}
static inline uintptr_t get_instruction_pointer(trapframe_t* tf)
{
return tf->user_ip;
}
static inline void set_instruction_pointer(trapframe_t* tf, uintptr_t ip)
{
tf->user_ip = ip;
}
static inline uint32_t get_syscall_result(trapframe_t* tf)
{
return tf->r[0];
}
static inline void set_syscall_result(trapframe_t* tf, uintptr_t val)
{
tf->r[0] = val;
}
/**
* STACK FUNCTIONS
*/
static inline void tf_push_to_stack(trapframe_t* tf, uintptr_t val)
{
tf->user_sp -= sizeof(uintptr_t);
*((uintptr_t*)tf->user_sp) = val;
}
static inline uintptr_t tf_pop_to_stack(trapframe_t* tf)
{
uintptr_t val = *((uintptr_t*)tf->user_sp);
tf->user_sp += sizeof(uintptr_t);
return val;
}
static inline void tf_move_stack_pointer(trapframe_t* tf, int32_t val)
{
tf->user_sp += val;
}
static inline void tf_setup_as_user_thread(trapframe_t* tf)
{
tf->user_flags = 0x60000100 | CPSR_M_USR;
}
static inline void tf_setup_as_kernel_thread(trapframe_t* tf)
{
tf->user_flags = 0x60000100 | CPSR_M_SYS;
}
static void dump_tf(trapframe_t* tf)
{
for (int i = 0; i < 13; i++) {
log("r[%d]: %x", i, tf->r[i]);
}
log("sp: %x", tf->user_sp);
log("ip: %x", tf->user_ip);
log("fl: %x", tf->user_flags);
}
#endif // _KERNEL_PLATFORM_ARM32_TASKING_TRAPFRAME_H

View File

@@ -0,0 +1,21 @@
#define VMM_LV0_ENTITY_COUNT (256)
#define VMM_LV1_ENTITY_COUNT (4096)
#define VMM_PAGE_SIZE (4096)
#define VMM_OFFSET_IN_DIRECTORY(a) (((a) >> 20) & 0xfff)
#define VMM_OFFSET_IN_TABLE(a) (((a) >> 12) & 0xff)
#define VMM_OFFSET_IN_PAGE(a) ((a)&0xfff)
#define TABLE_START(vaddr) ((vaddr >> 20) << 20)
#define PAGE_START(vaddr) ((vaddr >> 12) << 12)
#define FRAME(addr) (addr / VMM_PAGE_SIZE)
#define PTABLE_TOP_KERNEL_OFFSET 3072
#define PTABLE_LV_TOP (1)
#define PTABLE_LV0_VADDR_OFFSET (12)
#define PTABLE_LV1_VADDR_OFFSET (20)
#define USER_HIGH 0xbfffffff
#define KERNEL_BASE 0xc0000000
#endif //_KERNEL_PLATFORM_ARM32_VMM_CONSTS_H

View File

@@ -0,0 +1,21 @@
typedef uint32_t ptable_entity_t;
typedef uint32_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_ARM32_VMM_MMU_H

View File

@@ -0,0 +1,26 @@
#ifndef _KERNEL_PLATFORM_ARM32_VMM_PDE_H
#define _KERNEL_PLATFORM_ARM32_VMM_PDE_H
#include <libkern/c_attrs.h>
#include <libkern/types.h>
struct PACKED table_desc {
union {
struct {
int valid : 1; /* Valid mapping */
int zero1 : 1;
int zero2 : 1;
int ns : 1;
int zero3 : 1;
int domain : 4;
int imp : 1;
int baddr : 22;
};
uint32_t data;
};
};
typedef struct table_desc table_desc_t;
#define TABLE_DESC_FRAME_OFFSET 10
#endif //_KERNEL_PLATFORM_ARM32_VMM_PDE_H

View File

@@ -0,0 +1,31 @@
#ifndef _KERNEL_PLATFORM_ARM32_VMM_PTE_H
#define _KERNEL_PLATFORM_ARM32_VMM_PTE_H
#include <libkern/c_attrs.h>
#include <libkern/types.h>
#include <mem/bits/mmu.h>
typedef uint32_t ptable_entity_t;
struct PACKED page_desc {
union {
struct {
unsigned int xn : 1; // Execute never. Stops execution of page.
unsigned int one : 1; // Always one for tables
unsigned int b : 1; // cacheable
unsigned int c : 1; // Cacheable
unsigned int ap1 : 2;
unsigned int tex : 3;
unsigned int ap2 : 1;
unsigned int s : 1;
unsigned int ng : 1;
unsigned int baddr : 20;
};
uint32_t data;
};
};
typedef struct page_desc page_desc_t;
#define PAGE_DESC_FRAME_OFFSET 12
#endif //_KERNEL_PLATFORM_ARM32_VMM_PTE_H

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

View File

@@ -0,0 +1,67 @@
#ifndef _KERNEL_PLATFORM_GENERIC_CPU_H
#define _KERNEL_PLATFORM_GENERIC_CPU_H
#include <libkern/types.h>
#include <mem/kmemzone.h>
#include <mem/vmm.h>
#include <platform/generic/fpu/fpu.h>
#include <platform/generic/tasking/context.h>
#include <tasking/bits/sched.h>
#define MAX_CPU_CNT 4
#define THIS_CPU (&cpus[system_cpu_id()])
// TODO: Add support for RiscV FPU.
#ifndef __riscv
#define FPU_ENABLED
#endif
typedef uint32_t cpufeat_flags_t;
struct thread;
typedef int cpu_state_t;
enum CPU_STATE {
CPU_IN_KERNEL,
CPU_IN_USERLAND,
};
typedef int data_access_type_t;
enum DATA_ACCESS_TYPE {
DATA_ACCESS_REGULAR,
DATA_ACCESS_KERNEL, // Allows umem_copy on kernel addresses.
};
typedef struct {
int id;
int int_depth_counter;
vm_address_space_t* active_address_space;
kmemzone_t sched_stack_zone;
context_t* sched_context; // context of sched's registers
struct thread* running_thread;
cpu_state_t current_state;
data_access_type_t data_access_type;
struct thread* idle_thread;
sched_data_t sched;
/* Stat */
time_t stat_ticks_since_boot;
time_t stat_system_and_idle_ticks;
time_t stat_user_ticks;
#ifdef FPU_ENABLED
// Information about current state of fpu.
struct thread* fpu_for_thread;
pid_t fpu_for_pid;
#endif // FPU_ENABLED
#ifdef KASAN_ENABLED
int kasan_depth_counter;
#endif
cpufeat_flags_t cpufeat;
} cpu_t;
extern cpu_t cpus[MAX_CPU_CNT];
#endif // _KERNEL_PLATFORM_GENERIC_CPU_H

View File

@@ -0,0 +1,9 @@
#if defined(__i386__) || defined(__x86_64__)
#include <platform/x86/cpuinfo.h>
#elif __arm__
#include <platform/arm32/cpuinfo.h>
#elif __aarch64__
#include <platform/arm64/cpuinfo.h>
#elif defined(__riscv) && (__riscv_xlen == 64)
#include <platform/riscv64/cpuinfo.h>
#endif

View File

@@ -0,0 +1,7 @@
#if defined(__i386__) || defined(__x86_64__)
#include <platform/x86/fpu/fpu.h>
#elif __arm__
#include <platform/arm32/fpu/fpuv4.h>
#elif __aarch64__
#include <platform/arm64/fpu/fpu.h>
#endif

View File

@@ -0,0 +1,9 @@
#if defined(__i386__) || defined(__x86_64__)
#include <platform/x86/init.h>
#elif __arm__
#include <platform/arm32/init.h>
#elif __aarch64__
#include <platform/arm64/init.h>
#elif defined(__riscv) && (__riscv_xlen == 64)
#include <platform/riscv64/init.h>
#endif

View File

@@ -0,0 +1,9 @@
#if defined(__i386__) || defined(__x86_64__)
#include <platform/x86/pmm/settings.h>
#elif __arm__
#include <platform/arm32/pmm/settings.h>
#elif __aarch64__
#include <platform/arm64/pmm/settings.h>
#elif defined(__riscv) && (__riscv_xlen == 64)
#include <platform/riscv64/pmm/settings.h>
#endif

View File

@@ -0,0 +1,9 @@
#if defined(__i386__) || defined(__x86_64__)
#include <platform/x86/registers.h>
#elif __arm__
#include <platform/arm32/registers.h>
#elif __aarch64__
#include <platform/arm64/registers.h>
#elif defined(__riscv) && (__riscv_xlen == 64)
#include <platform/riscv64/registers.h>
#endif

View File

@@ -0,0 +1,9 @@
#if defined(__i386__) || defined(__x86_64__)
#include <platform/x86/syscalls/params.h>
#elif __arm__
#include <platform/arm32/syscalls/params.h>
#elif __aarch64__
#include <platform/arm64/syscalls/params.h>
#elif defined(__riscv) && (__riscv_xlen == 64)
#include <platform/riscv64/syscalls/params.h>
#endif

View File

@@ -0,0 +1,9 @@
#if defined(__i386__) || defined(__x86_64__)
#include <platform/x86/system.h>
#elif __arm__
#include <platform/arm32/system.h>
#elif __aarch64__
#include <platform/arm64/system.h>
#elif defined(__riscv) && (__riscv_xlen == 64)
#include <platform/riscv64/system.h>
#endif

View File

@@ -0,0 +1,9 @@
#if defined(__i386__) || defined(__x86_64__)
#include <platform/x86/tasking/context.h>
#elif __arm__
#include <platform/arm32/tasking/context.h>
#elif __aarch64__
#include <platform/arm64/tasking/context.h>
#elif defined(__riscv) && (__riscv_xlen == 64)
#include <platform/riscv64/tasking/context.h>
#endif

View File

@@ -0,0 +1,9 @@
#if defined(__i386__) || defined(__x86_64__)
#include <platform/x86/tasking/dump_impl.h>
#elif __arm__
#include <platform/arm32/tasking/dump_impl.h>
#elif __aarch64__
#include <platform/arm64/tasking/dump_impl.h>
#elif defined(__riscv) && (__riscv_xlen == 64)
#include <platform/riscv64/tasking/dump_impl.h>
#endif

View File

@@ -0,0 +1,9 @@
#if defined(__i386__) || defined(__x86_64__)
#include <platform/x86/tasking/signal_impl.h>
#elif __arm__
#include <platform/arm32/tasking/signal_impl.h>
#elif __aarch64__
#include <platform/arm64/tasking/signal_impl.h>
#elif defined(__riscv) && (__riscv_xlen == 64)
#include <platform/riscv64/tasking/signal_impl.h>
#endif

View File

@@ -0,0 +1,9 @@
#if defined(__i386__) || defined(__x86_64__)
#include <platform/x86/tasking/trapframe.h>
#elif __arm__
#include <platform/arm32/tasking/trapframe.h>
#elif __aarch64__
#include <platform/arm64/tasking/trapframe.h>
#elif defined(__riscv) && (__riscv_xlen == 64)
#include <platform/riscv64/tasking/trapframe.h>
#endif

View File

@@ -0,0 +1,9 @@
#if defined(__i386__) || defined(__x86_64__)
#include <platform/x86/vmm/consts.h>
#elif __arm__
#include <platform/arm32/vmm/consts.h>
#elif __aarch64__
#include <platform/arm64/vmm/consts.h>
#elif defined(__riscv) && (__riscv_xlen == 64)
#include <platform/riscv64/vmm/consts.h>
#endif

View File

@@ -0,0 +1,17 @@
#ifndef _KERNEL_PLATFORM_GENERIC_VMM_MAPPING_TABLE_H
#define _KERNEL_PLATFORM_GENERIC_VMM_MAPPING_TABLE_H
#include <libkern/types.h>
struct mapping_entry {
uintptr_t paddr;
uintptr_t vaddr;
size_t pages;
uint32_t flags;
uint32_t last; // 1 if an element is the last.
};
typedef struct mapping_entry mapping_entry_t;
extern mapping_entry_t extern_mapping_table[]; // Maps after kernel tables are ready, so can be outside kernelspace
#endif // _KERNEL_PLATFORM_GENERIC_VMM_MAPPING_TABLE_H

View File

@@ -0,0 +1,9 @@
#if defined(__i386__) || defined(__x86_64__)
#include <platform/x86/vmm/mmu.h>
#elif __arm__
#include <platform/arm32/vmm/mmu.h>
#elif __aarch64__
#include <platform/arm64/vmm/mmu.h>
#elif defined(__riscv) && (__riscv_xlen == 64)
#include <platform/riscv64/vmm/mmu.h>
#endif

View File

@@ -0,0 +1,13 @@
#ifndef _KERNEL_PLATFORM_RISCV64_CPUINFO_H
#define _KERNEL_PLATFORM_RISCV64_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_RISCV64_CPUINFO_H

View File

@@ -0,0 +1,36 @@
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_RISCV64_FPU_FPU_H

View File

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

View File

@@ -0,0 +1,13 @@
#ifndef _KERNEL_PLATFORM_RISCV64_INTERRUPTS_H
#define _KERNEL_PLATFORM_RISCV64_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 plic_setup();
#endif /* _KERNEL_PLATFORM_RISCV64_INTERRUPTS_H */

View File

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

View File

@@ -0,0 +1,40 @@
#ifndef _KERNEL_PLATFORM_RISCV64_REGISTERS_H
#define _KERNEL_PLATFORM_RISCV64_REGISTERS_H
#include <libkern/types.h>
#include <platform/riscv64/system.h>
extern uint64_t read_ip();
static inline uint64_t read_scause()
{
uint64_t x;
asm volatile("csrr %0, scause"
: "=r"(x));
return x;
}
static inline uint64_t read_stval()
{
uint64_t x;
asm volatile("csrr %0, stval"
: "=r"(x));
return x;
}
static inline uint64_t read_sip()
{
uint64_t x;
asm volatile("csrr %0, sip"
: "=r"(x));
return x;
}
static inline void write_sip(uint64_t val)
{
asm volatile("csrw sip, %0"
:
: "r"(val)
: "memory");
}
#endif /* _KERNEL_PLATFORM_RISCV64_REGISTERS_H */

View File

@@ -0,0 +1,17 @@
#ifndef _KERNEL_PLATFORM_RISCV64_SYSCALLS_PARAMS_H
#define _KERNEL_PLATFORM_RISCV64_SYSCALLS_PARAMS_H
#include <platform/riscv64/tasking/trapframe.h>
#define SYSCALL_ID(tf) (tf->a7)
#define SYSCALL_VAR1(tf) (tf->a0)
#define SYSCALL_VAR2(tf) (tf->a1)
#define SYSCALL_VAR3(tf) (tf->a2)
#define SYSCALL_VAR4(tf) (tf->a3)
#define SYSCALL_VAR5(tf) (tf->a4)
#define return_val (tf->a0)
#define return_with_val(val) \
(return_val = val); \
return
#endif // _KERNEL_PLATFORM_RISCV64_SYSCALLS_PARAMS_H

View File

@@ -0,0 +1,132 @@
#ifndef _KERNEL_PLATFORM_RISCV64_SYSTEM_H
#define _KERNEL_PLATFORM_RISCV64_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("fence.i"
:
:
: "memory");
}
inline static void system_data_synchronise_barrier()
{
asm volatile("fence.i"
:
:
: "memory");
}
inline static void system_data_memory_barrier()
{
asm volatile("fence"
:
:
: "memory");
}
inline static void system_disable_interrupts_no_counter()
{
system_instruction_barrier();
asm volatile("csrc sstatus, 0x3");
system_instruction_barrier();
}
inline static void system_enable_interrupts_no_counter()
{
system_instruction_barrier();
asm volatile("csrs sstatus, 0x3");
system_instruction_barrier();
}
/**
* PAGING
*/
inline static void system_set_pdir(uintptr_t pdir0, uintptr_t pdir1)
{
system_data_synchronise_barrier();
asm volatile("csrw satp, %0"
:
: "r"((9L << 60) | (pdir0 >> 12)));
system_instruction_barrier();
}
inline static void system_flush_local_tlb_entry(uintptr_t vaddr)
{
asm volatile("sfence.vma %0, zero"
:
: "r"(vaddr)
: "memory");
}
inline static void system_flush_all_cpus_tlb_entry(uintptr_t vaddr)
{
asm volatile("sfence.vma %0, zero"
:
: "r"(vaddr)
: "memory");
}
inline static void system_flush_whole_tlb()
{
asm volatile("sfence.vma %0, zero"
:
: "r"(-1)
: "memory");
}
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_RISCV64_SYSTEM_H */

View File

@@ -0,0 +1,23 @@
#ifndef _KERNEL_PLATFORM_RISCV64_TASKING_CONTEXT_H
#define _KERNEL_PLATFORM_RISCV64_TASKING_CONTEXT_H
#include <libkern/c_attrs.h>
#include <libkern/types.h>
typedef struct {
uint64_t ra;
uint64_t sp;
uint64_t s[12];
} PACKED context_t;
static inline uintptr_t context_get_instruction_pointer(context_t* ctx)
{
return ctx->ra;
}
static inline void context_set_instruction_pointer(context_t* ctx, uintptr_t ip)
{
ctx->ra = ip;
}
#endif // _KERNEL_PLATFORM_RISCV64_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,126 @@
#ifndef _KERNEL_PLATFORM_RISCV64_TASKING_TRAPFRAME_H
#define _KERNEL_PLATFORM_RISCV64_TASKING_TRAPFRAME_H
#include <libkern/c_attrs.h>
#include <libkern/log.h>
#include <libkern/types.h>
typedef struct {
uint64_t ra;
uint64_t sp;
uint64_t gp;
uint64_t tp;
uint64_t t0;
uint64_t t1;
uint64_t t2;
uint64_t s0;
uint64_t s1;
uint64_t a0;
uint64_t a1;
uint64_t a2;
uint64_t a3;
uint64_t a4;
uint64_t a5;
uint64_t a6;
uint64_t a7;
uint64_t s2;
uint64_t s3;
uint64_t s4;
uint64_t s5;
uint64_t s6;
uint64_t s7;
uint64_t s8;
uint64_t s9;
uint64_t s10;
uint64_t s11;
uint64_t t3;
uint64_t t4;
uint64_t t5;
uint64_t t6;
uint64_t sstatus;
uint64_t epc;
} 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 0;
}
static inline void set_frame_pointer(trapframe_t* tf, uintptr_t bp)
{
}
static inline uintptr_t get_instruction_pointer(trapframe_t* tf)
{
return tf->epc;
}
static inline void set_instruction_pointer(trapframe_t* tf, uintptr_t ip)
{
tf->epc = ip;
}
static inline uintptr_t get_syscall_result(trapframe_t* tf)
{
return tf->a0;
}
static inline void set_syscall_result(trapframe_t* tf, uintptr_t val)
{
tf->a0 = 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;
}
#define SSTATUS_VM (1L << 18) // VM
#define SSTATUS_SPP (1L << 8) // Previous mode, 1=Supervisor, 0=User
#define SSTATUS_SPIE (1L << 5) // Supervisor Previous Interrupt Enable
#define SSTATUS_UPIE (1L << 4) // User Previous Interrupt Enable
#define SSTATUS_SIE (1L << 1) // Supervisor Interrupt Enable
#define SSTATUS_UIE (1L << 0) // User Interrupt Enable
static inline void tf_setup_as_user_thread(trapframe_t* tf)
{
tf->sstatus = SSTATUS_VM | SSTATUS_UPIE | SSTATUS_SPIE;
}
static inline void tf_setup_as_kernel_thread(trapframe_t* tf)
{
tf->sstatus = SSTATUS_VM | SSTATUS_SPP | SSTATUS_SPIE;
}
static void dump_tf(trapframe_t* tf)
{
}
#endif // _KERNEL_PLATFORM_RISCV64_TASKING_TRAPFRAME_H

View File

@@ -0,0 +1,37 @@
#ifndef _KERNEL_PLATFORM_RISCV64_VMM_CONSTS_H
#define _KERNEL_PLATFORM_RISCV64_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 (3)
#define PTABLE_LV0_VADDR_OFFSET (12)
#define PTABLE_LV1_VADDR_OFFSET (21)
#define PTABLE_LV2_VADDR_OFFSET (30)
#define PTABLE_LV3_VADDR_OFFSET (39)
#define PTABLE_TOP_KERNEL_OFFSET (VMM_LV3_ENTITY_COUNT / 2)
#define USER_HIGH 0x7fffffffffff
#define KERNEL_BASE 0xffff800000000000
#define KERNEL_PADDR_BASE 0xffffffff00000000 // TODO(x64): up to 4gbs are supported.
#endif // _KERNEL_PLATFORM_RISCV64_VMM_CONSTS_H

View File

@@ -0,0 +1,30 @@
#ifndef _KERNEL_PLATFORM_RISCV64_VMM_MMU_H
#define _KERNEL_PLATFORM_RISCV64_VMM_MMU_H
#include <libkern/types.h>
#include <mem/bits/mmu.h>
// riscv64 uses one table to manage virtual space.
#undef 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_RISCV64_VMM_MMU_H

View File

@@ -0,0 +1,31 @@
#ifndef _KERNEL_PLATFORM_X86_CPUINFO_H
#define _KERNEL_PLATFORM_X86_CPUINFO_H
#include <libkern/c_attrs.h>
#include <libkern/types.h>
#include <platform/generic/cpu.h>
enum CPUFEAT_FLAGS {
CPUFEAT_FPU = (1 << 0),
CPUFEAT_PSE = (1 << 1),
CPUFEAT_PAE = (1 << 2),
CPUFEAT_CLFSH = (1 << 3),
CPUFEAT_SSE = (1 << 4),
CPUFEAT_SSE2 = (1 << 5),
CPUFEAT_SSE3 = (1 << 6),
CPUFEAT_SSSE3 = (1 << 7),
CPUFEAT_SSE4_1 = (1 << 8),
CPUFEAT_SSE4_2 = (1 << 9),
CPUFEAT_XSAVE = (1 << 10),
CPUFEAT_AVX = (1 << 11),
CPUFEAT_PDPE1GB = (1 << 12),
};
void cpuinfo_init();
static inline bool cpuinfo_has_1gb_pages()
{
return TEST_FLAG(THIS_CPU->cpufeat, CPUFEAT_PDPE1GB);
}
#endif // _KERNEL_PLATFORM_X86_CPUINFO_H

View File

@@ -0,0 +1,51 @@
#ifndef _KERNEL_PLATFORM_X86_FPU_FPU_H
#define _KERNEL_PLATFORM_X86_FPU_FPU_H
#include <libkern/types.h>
#include <platform/x86/registers.h>
#define FPU_STATE_ALIGNMENT (16)
typedef struct {
uint8_t buffer[512];
#ifdef __x86_64__
// Space for YMM.
uint8_t ext_save_area[256];
#endif
} __attribute__((aligned(FPU_STATE_ALIGNMENT))) fpu_state_t;
void fpu_handler();
void fpu_init();
void fpu_init_state(fpu_state_t* new_fpu_state);
static inline void fpu_save(fpu_state_t* fpu_state)
{
asm volatile("fxsave %0"
: "=m"(*fpu_state));
}
static inline void fpu_restore(fpu_state_t* fpu_state)
{
asm volatile("fxrstor %0"
:
: "m"(*fpu_state));
}
static inline int fpu_is_avail()
{
return (((read_cr0() >> 3) & 0b1) == 0b0);
}
static inline void fpu_make_avail()
{
asm volatile("clts");
}
static inline void fpu_make_unavail()
{
// Set TS bit of cr0 to 1.
uintptr_t cr0 = read_cr0() | (1 << 3);
write_cr0(cr0);
}
#endif //_KERNEL_PLATFORM_X86_FPU_FPU_H

View File

@@ -0,0 +1,125 @@
#ifndef _KERNEL_PLATFORM_X86_GDT_H
#define _KERNEL_PLATFORM_X86_GDT_H
#include <libkern/c_attrs.h>
#include <libkern/types.h>
#ifdef __x86_64__
#define GDT_MAX_ENTRIES 7 // TSS takes 2 entries.
// Marking that code segment contains native 64-bit code.
#define GDT_LONGMODE_FLAG 1
#define GDT_DB_FLAG 0
#else
#define GDT_MAX_ENTRIES 6
#define GDT_LONGMODE_FLAG 0
#define GDT_DB_FLAG 1
#endif
#define GDT_SEG_NULL 0 // kernel code
#define GDT_SEG_KCODE 1 // kernel code
#define GDT_SEG_KDATA 2 // kernel data+stack
#define GDT_SEG_UCODE 3 // user code
#define GDT_SEG_UDATA 4 // user data+stack
#define GDT_SEG_TSS 5 // task state NOT USED CURRENTLY
#define GDT_SEGF_X 0x8 // exec
#define GDT_SEGF_A 0x1 // accessed
#define GDT_SEGF_R 0x2 // readable (if exec)
#define GDT_SEGF_C 0x4 // conforming seg (if exec)
#define GDT_SEGF_W 0x2 // writeable (if non-exec)
#define GDT_SEGF_D 0x4 // grows down (if non-exec)
#define FL_IF 0x00000200
#define DPL_KERN 0x0
#define DPL_USER 0x3
struct PACKED gdt_desc {
union {
struct {
uint32_t lim_15_0 : 16;
uint32_t base_15_0 : 16;
uint32_t base_23_16 : 8;
uint32_t type : 4;
uint32_t dt : 1;
uint32_t dpl : 2;
uint32_t p : 1;
uint32_t lim_19_16 : 4;
uint32_t avl : 1;
uint32_t l : 1;
uint32_t db : 1;
uint32_t g : 1;
uint32_t base_31_24 : 8;
};
uint32_t raw;
};
};
typedef struct gdt_desc gdt_desc_t;
extern gdt_desc_t gdt[GDT_MAX_ENTRIES];
#define GDT_SEG_CODE_DESC(vtype, vbase, vlimit, vdpl) \
(gdt_desc_t) \
{ \
.lim_15_0 = ((vlimit) >> 12) & 0xffff, \
.base_15_0 = (uint32_t)(vbase)&0xffff, \
.base_23_16 = ((uint32_t)(vbase) >> 16) & 0xff, \
.type = vtype, \
.dt = 1, \
.dpl = vdpl, \
.p = 1, \
.lim_19_16 = ((uint32_t)(vlimit) >> 28), \
.avl = 0, \
.l = GDT_LONGMODE_FLAG, \
.db = GDT_DB_FLAG, \
.g = 1, \
.base_31_24 = (uint32_t)(vbase) >> 24 \
}
#define GDT_SEG_DATA_DESC(vtype, vbase, vlimit, vdpl) \
(gdt_desc_t) \
{ \
.lim_15_0 = ((vlimit) >> 12) & 0xffff, \
.base_15_0 = (uint32_t)(vbase)&0xffff, \
.base_23_16 = ((uint32_t)(vbase) >> 16) & 0xff, \
.type = vtype, \
.dt = 1, \
.dpl = vdpl, \
.p = 1, \
.lim_19_16 = ((uint32_t)(vlimit) >> 28), \
.avl = 0, \
.l = 0, \
.db = 1, \
.g = 1, \
.base_31_24 = (uint32_t)(vbase) >> 24 \
}
#define GDT_SEG_TSS_DESC(vtype, vbase, vlimit, vdpl) \
(gdt_desc_t) \
{ \
.lim_15_0 = ((vlimit)) & 0xffff, \
.base_15_0 = (uint32_t)(vbase)&0xffff, \
.base_23_16 = ((uint32_t)(vbase) >> 16) & 0xff, \
.type = vtype, \
.dt = 0, \
.dpl = vdpl, \
.p = 1, \
.lim_19_16 = ((uint32_t)(vlimit) >> 16), \
.avl = 0, \
.l = 0, \
.db = 0, \
.g = 0, \
.base_31_24 = (uint32_t)(vbase) >> 24 \
}
#define GDT_SEG_SET_RAW(rawvalue) \
(gdt_desc_t) \
{ \
.raw = rawvalue \
}
void gdt_setup();
#endif // _KERNEL_PLATFORM_X86_GDT_H

View File

@@ -0,0 +1,26 @@
#ifndef _KERNEL_PLATFORM_X86_I386_TASKING_CONTEXT_H
#define _KERNEL_PLATFORM_X86_I386_TASKING_CONTEXT_H
#include <libkern/c_attrs.h>
#include <libkern/types.h>
struct PACKED context {
uint32_t edi;
uint32_t esi;
uint32_t ebx;
uint32_t ebp;
uint32_t eip;
};
typedef struct context context_t;
static inline uintptr_t context_get_instruction_pointer(context_t* ctx)
{
return ctx->eip;
}
static inline void context_set_instruction_pointer(context_t* ctx, uintptr_t ip)
{
ctx->eip = ip;
}
#endif // _KERNEL_PLATFORM_X86_I386_TASKING_CONTEXT_H

View File

@@ -0,0 +1,125 @@
#ifndef _KERNEL_PLATFORM_X86_I386_TASKING_TRAPFRAME_H
#define _KERNEL_PLATFORM_X86_I386_TASKING_TRAPFRAME_H
#include <libkern/c_attrs.h>
#include <libkern/types.h>
#include <platform/x86/gdt.h>
#include <platform/x86/tasking/tss.h>
struct PACKED trapframe {
// registers as pushed by pusha
uint32_t edi;
uint32_t esi;
uint32_t ebp;
uint32_t oesp; // useless & ignored
uint32_t ebx;
uint32_t edx;
uint32_t ecx;
uint32_t eax;
// rest of trap frame
uint16_t gs;
uint16_t padding1;
uint16_t fs;
uint16_t padding2;
uint16_t es;
uint16_t padding3;
uint16_t ds;
uint16_t padding4;
uint32_t int_no;
// below here defined by x86 hardware
uint32_t err;
uint32_t eip;
uint16_t cs;
uint16_t padding5;
uint32_t eflags;
// below here only when crossing rings, such as from user to kernel
uint32_t esp;
uint16_t ss;
uint16_t padding6;
};
typedef struct trapframe trapframe_t;
static inline uintptr_t get_stack_pointer(trapframe_t* tf)
{
return tf->esp;
}
static inline void set_stack_pointer(trapframe_t* tf, uintptr_t sp)
{
tf->esp = sp;
}
static inline uintptr_t get_frame_pointer(trapframe_t* tf)
{
return tf->ebp;
}
static inline void set_frame_pointer(trapframe_t* tf, uintptr_t bp)
{
tf->ebp = bp;
}
static inline uintptr_t get_instruction_pointer(trapframe_t* tf)
{
return tf->eip;
}
static inline void set_instruction_pointer(trapframe_t* tf, uintptr_t ip)
{
tf->eip = ip;
}
static inline uintptr_t get_syscall_result(trapframe_t* tf)
{
return tf->eax;
}
static inline void set_syscall_result(trapframe_t* tf, uintptr_t val)
{
tf->eax = val;
}
/**
* STACK FUNCTIONS
*/
static inline void tf_push_to_stack(trapframe_t* tf, uintptr_t val)
{
tf->esp -= sizeof(uintptr_t);
*((uintptr_t*)tf->esp) = val;
}
static inline uint32_t tf_pop_to_stack(trapframe_t* tf)
{
uintptr_t val = *((uintptr_t*)tf->esp);
tf->esp += sizeof(uintptr_t);
return val;
}
static inline void tf_move_stack_pointer(trapframe_t* tf, int32_t val)
{
tf->esp += val;
}
static inline void tf_setup_as_user_thread(trapframe_t* tf)
{
tf->cs = (GDT_SEG_UCODE << 3) | DPL_USER;
tf->ds = (GDT_SEG_UDATA << 3) | DPL_USER;
tf->es = tf->ds;
tf->ss = tf->ds;
tf->eflags = FL_IF;
}
static inline void tf_setup_as_kernel_thread(trapframe_t* tf)
{
tf->cs = (GDT_SEG_KCODE << 3);
tf->ds = (GDT_SEG_KDATA << 3);
tf->es = tf->ds;
tf->ss = tf->ds;
tf->eflags = FL_IF;
}
#endif // _KERNEL_PLATFORM_X86_I386_TASKING_TRAPFRAME_H

View File

@@ -0,0 +1,29 @@
#ifndef _KERNEL_PLATFORM_X86_I386_VMM_CONSTS_H
#define _KERNEL_PLATFORM_X86_I386_VMM_CONSTS_H
static inline int vm_page_mask()
{
return 0xfff;
}
#define VMM_LV0_ENTITY_COUNT (1024)
#define VMM_LV1_ENTITY_COUNT (1024)
#define VMM_PAGE_SIZE (4096)
#define VMM_OFFSET_IN_DIRECTORY(a) (((a) >> 22) & 0x3ff)
#define VMM_OFFSET_IN_TABLE(a) (((a) >> 12) & 0x3ff)
#define VMM_OFFSET_IN_PAGE(a) ((a)&0xfff)
#define TABLE_START(vaddr) ((vaddr >> 22) << 22)
#define PAGE_START(vaddr) ((vaddr >> 12) << 12)
#define FRAME(addr) (addr / VMM_PAGE_SIZE)
#define PTABLE_TOP_KERNEL_OFFSET 768
#define PTABLE_LV_TOP (1)
#define PTABLE_LV0_VADDR_OFFSET (12)
#define PTABLE_LV1_VADDR_OFFSET (22)
#define USER_HIGH 0xbfffffff
#define KERNEL_BASE 0xc0000000
#endif //_KERNEL_PLATFORM_X86_I386_VMM_CONSTS_H

View File

@@ -0,0 +1,29 @@
#ifndef _KERNEL_PLATFORM_X86_I386_VMM_MMU_H
#define _KERNEL_PLATFORM_X86_I386_VMM_MMU_H
#include <libkern/types.h>
#include <mem/bits/mmu.h>
#include <platform/x86/i386/vmm/pde.h>
#include <platform/x86/i386/vmm/pte.h>
typedef uint32_t ptable_entity_t;
typedef uint32_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_X86_I386_VMM_MMU_H

View File

@@ -0,0 +1,25 @@
#ifndef _KERNEL_PLATFORM_X86_I386_VMM_PDE_H
#define _KERNEL_PLATFORM_X86_I386_VMM_PDE_H
#include <libkern/types.h>
#define table_desc_t uint32_t
#define pde_t uint32_t
#define TABLE_DESC_FRAME_OFFSET 12
enum TABLE_DESC_PAGE_FLAGS {
TABLE_DESC_PRESENT = 0x1,
TABLE_DESC_WRITABLE = 0x2,
TABLE_DESC_USER = 0x4,
TABLE_DESC_PWT = 0x8,
TABLE_DESC_PCD = 0x10,
TABLE_DESC_ACCESSED = 0x20,
TABLE_DESC_DIRTY = 0x40,
TABLE_DESC_4MB = 0x80,
TABLE_DESC_CPU_GLOBAL = 0x100,
TABLE_DESC_LV4_GLOBAL = 0x200,
TABLE_DESC_COPY_ON_WRITE = 0x400,
};
#endif //_KERNEL_PLATFORM_X86_I386_VMM_PDE_H

View File

@@ -0,0 +1,23 @@
#ifndef _KERNEL_PLATFORM_X86_I386_VMM_PTE_H
#define _KERNEL_PLATFORM_X86_I386_VMM_PTE_H
#include <libkern/types.h>
#include <mem/bits/mmu.h>
#define PAGE_DESC_FRAME_OFFSET 12
enum PAGE_DESC_PAGE_FLAGS {
PAGE_DESC_PRESENT = 0x1,
PAGE_DESC_WRITABLE = 0x2,
PAGE_DESC_USER = 0x4,
PAGE_DESC_WRITETHOUGH = 0x8,
PAGE_DESC_NOT_CACHEABLE = 0x10,
PAGE_DESC_ACCESSED = 0x20,
PAGE_DESC_DIRTY = 0x40,
PAGE_DESC_PAT = 0x80,
PAGE_DESC_CPU_GLOBAL = 0x100,
PAGE_DESC_LV4_GLOBAL = 0x200,
PAGE_DESC_COPY_ON_WRITE = 0x400,
};
#endif //_KERNEL_PLATFORM_X86_I386_VMM_PTE_H

View File

@@ -0,0 +1,103 @@
#ifndef _KERNEL_PLATFORM_X86_IDT_H
#define _KERNEL_PLATFORM_X86_IDT_H
#include <drivers/irq/irq_api.h>
#include <drivers/irq/x86/pic.h>
#include <libkern/c_attrs.h>
#include <libkern/types.h>
#include <platform/x86/port.h>
#include <platform/x86/tasking/trapframe.h>
#define IDT_ENTRIES 256
#define IRQ_MASTER_OFFSET 32
#define IRQ_SLAVE_OFFSET 40
struct PACKED idt_entry {
uint16_t offset_lower; // bits 0..15
uint16_t segment;
uint8_t zero;
uint8_t type;
uint16_t offset_upper; // bits 16..31
#ifdef __x86_64__
uint32_t offset_long; // bits 32..63
uint32_t zero2;
#endif
};
typedef struct idt_entry idt_entry_t;
void interrupts_setup();
/* ISRs reserved for CPU exceptions */
extern void isr0();
extern void isr1();
extern void isr2();
extern void isr3();
extern void isr4();
extern void isr5();
extern void isr6();
extern void isr7();
extern void isr8();
extern void isr9();
extern void isr10();
extern void isr11();
extern void isr12();
extern void isr13();
extern void isr14();
extern void isr15();
extern void isr16();
extern void isr17();
extern void isr18();
extern void isr19();
extern void isr20();
extern void isr21();
extern void isr22();
extern void isr23();
extern void isr24();
extern void isr25();
extern void isr26();
extern void isr27();
extern void isr28();
extern void isr29();
extern void isr30();
extern void isr31();
/* IRQ definitions */
extern void irq0();
extern void irq1();
extern void irq2();
extern void irq3();
extern void irq4();
extern void irq5();
extern void irq6();
extern void irq7();
extern void irq8();
extern void irq9();
extern void irq10();
extern void irq11();
extern void irq12();
extern void irq13();
extern void irq14();
extern void irq15();
extern void irq_null();
extern void irq_empty_handler();
extern void syscall();
#define IRQ0 32
#define IRQ1 33
#define IRQ2 34
#define IRQ3 35
#define IRQ4 36
#define IRQ5 37
#define IRQ6 38
#define IRQ7 39
#define IRQ8 40
#define IRQ9 41
#define IRQ10 42
#define IRQ11 43
#define IRQ12 44
#define IRQ13 45
#define IRQ14 46
#define IRQ15 47
#endif // _KERNEL_PLATFORM_X86_IDT_H

View File

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

View File

@@ -0,0 +1,9 @@
#ifndef _KERNEL_PLATFORM_X86_IRQ_HANDLER_H
#define _KERNEL_PLATFORM_X86_IRQ_HANDLER_H
#include <platform/x86/idt.h>
void irq_handler(trapframe_t* tf);
void irq_empty_handler();
#endif

View File

@@ -0,0 +1,9 @@
#ifndef _KERNEL_PLATFORM_X86_ISR_HANDLER_H
#define _KERNEL_PLATFORM_X86_ISR_HANDLER_H
#include <libkern/types.h>
#include <platform/x86/idt.h>
void isr_handler(trapframe_t* tf);
#endif

View File

@@ -0,0 +1,8 @@
#ifndef _KERNEL_PLATFORM_X86_PMM_SETTINGS_H
#define _KERNEL_PLATFORM_X86_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,20 @@
#ifndef _KERNEL_PLATFORM_X86_PORT_H
#define _KERNEL_PLATFORM_X86_PORT_H
#include <libkern/types.h>
uint8_t port_read8(uint16_t port);
void port_write8(uint16_t port, uint8_t data);
uint16_t port_read16(uint16_t port);
void port_write16(uint16_t port, uint16_t data);
uint32_t port_read32(uint16_t port);
void port_write32(uint16_t port, uint32_t data);
uint8_t port_read8(uint16_t port);
void port_write8(uint16_t port, uint8_t data);
uint16_t port_read16(uint16_t port);
void port_write16(uint16_t port, uint16_t data);
uint32_t port_read32(uint16_t port);
void port_write32(uint16_t port, uint32_t data);
void port_wait_io();
#endif

View File

@@ -0,0 +1,65 @@
#ifndef _KERNEL_PLATFORM_X86_REGISTERS_H
#define _KERNEL_PLATFORM_X86_REGISTERS_H
#include <libkern/types.h>
extern uintptr_t read_ip();
static inline uintptr_t read_cr2()
{
uintptr_t val;
asm volatile("mov %%cr2, %0"
: "=r"(val));
return val;
}
static inline uintptr_t read_cr3()
{
uintptr_t val;
asm volatile("mov %%cr3, %0"
: "=r"(val));
return val;
}
static inline uintptr_t read_sp()
{
uintptr_t val;
#ifdef BITS32
asm volatile("mov %%esp, %0"
: "=r"(val));
#else
asm volatile("mov %%rsp, %0"
: "=r"(val));
#endif
return val;
}
static inline uintptr_t read_bp()
{
uintptr_t val;
#ifdef BITS32
asm volatile("mov %%ebp, %0"
: "=r"(val));
#else
asm volatile("mov %%rbp, %0"
: "=r"(val));
#endif
return val;
}
static inline uintptr_t read_cr0()
{
uintptr_t val;
asm volatile("mov %%cr0, %0"
: "=r"(val));
return val;
}
static inline void write_cr0(uintptr_t val)
{
asm volatile("mov %0, %%cr0"
:
: "r"(val)
: "memory");
}
#endif /* _KERNEL_PLATFORM_X86_REGISTERS_H */

View File

@@ -0,0 +1,32 @@
#ifndef _KERNEL_PLATFORM_X86_SYSCALLS_PARAMS_H
#define _KERNEL_PLATFORM_X86_SYSCALLS_PARAMS_H
#include <platform/x86/tasking/trapframe.h>
#define SYSCALL_HANDLER_NO 0x80
#ifdef __i386__
#define SYSCALL_ID(tf) (tf->eax)
#define SYSCALL_VAR1(tf) (tf->ebx)
#define SYSCALL_VAR2(tf) (tf->ecx)
#define SYSCALL_VAR3(tf) (tf->edx)
#define SYSCALL_VAR4(tf) (tf->esi)
#define SYSCALL_VAR5(tf) (tf->edi)
#define return_val (tf->eax)
#define return_with_val(val) \
(return_val = val); \
return
#elif __x86_64__
#define SYSCALL_ID(tf) (tf->rax)
#define SYSCALL_VAR1(tf) (tf->rdi)
#define SYSCALL_VAR2(tf) (tf->rsi)
#define SYSCALL_VAR3(tf) (tf->rdx)
#define SYSCALL_VAR4(tf) (tf->r10)
#define SYSCALL_VAR5(tf) (tf->r8)
#define return_val (tf->rax)
#define return_with_val(val) \
(return_val = val); \
return
#endif
#endif // _KERNEL_PLATFORM_X86_SYSCALLS_PARAMS_H

View File

@@ -0,0 +1,100 @@
#ifndef _KERNEL_PLATFORM_X86_SYSTEM_H
#define _KERNEL_PLATFORM_X86_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_disable_interrupts_no_counter() { asm volatile("cli"); }
inline static void system_enable_interrupts_no_counter() { asm volatile("sti"); }
/**
* PAGING
*/
inline static void system_set_pdir(uintptr_t pdir0, uintptr_t pdir1)
{
asm volatile("mov %0, %%cr3"
:
: "r"(pdir0)
: "memory");
}
inline static void system_flush_local_tlb_entry(uintptr_t vaddr)
{
asm volatile("invlpg (%0)" ::"r"(vaddr)
: "memory");
}
inline static void system_flush_all_cpus_tlb_entry(uintptr_t vaddr)
{
system_flush_local_tlb_entry(vaddr);
// TODO: Send inter-processor messages.
}
inline static void system_flush_whole_tlb()
{
system_set_pdir(read_cr3(), 0x0);
}
inline static void system_enable_write_protect()
{
uintptr_t cr = read_cr0();
cr |= 0x10000;
write_cr0(cr);
}
inline static void system_disable_write_protect()
{
uintptr_t cr = read_cr0();
cr &= 0xfffeffff;
write_cr0(cr);
}
inline static void system_enable_paging()
{
uintptr_t cr = read_cr0();
cr |= 0x80000000;
write_cr0(cr);
}
inline static void system_disable_paging()
{
uintptr_t cr = read_cr0();
cr &= 0x7fffffff;
write_cr0(cr);
}
inline static void system_stop_until_interrupt()
{
asm volatile("hlt");
}
NORETURN inline static void system_stop()
{
system_disable_interrupts();
system_stop_until_interrupt();
while (1) { }
}
/**
* CPU
*/
void system_cache_clean_and_invalidate(void* addr, size_t size);
void system_cache_invalidate(void* addr, size_t size);
void system_cache_clean(void* addr, size_t size);
inline static int system_cpu_id()
{
return 0;
}
#endif /* _KERNEL_PLATFORM_X86_SYSTEM_H */

View File

@@ -0,0 +1,5 @@
#ifdef __i386__
#include <platform/x86/i386/tasking/context.h>
#elif __x86_64__
#include <platform/x86/x86_64/tasking/context.h>
#endif

View File

@@ -0,0 +1,12 @@
#ifndef _KERNEL_PLATFORM_X86_TASKING_DUMP_IMPL_H
#define _KERNEL_PLATFORM_X86_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_X86_TASKING_DUMP_IMPL_H

View File

@@ -0,0 +1,10 @@
#ifndef _KERNEL_PLATFORM_X86_TASKING_SIGNAL_IMPL_H
#define _KERNEL_PLATFORM_X86_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_X86_TASKING_SIGNAL_IMPL_H

View File

@@ -0,0 +1,9 @@
#ifndef _KERNEL_PLATFORM_X86_TASKING_SWITCHVM_H
#define _KERNEL_PLATFORM_X86_TASKING_SWITCHVM_H
#include <libkern/types.h>
#include <tasking/tasking.h>
void switch_uthreads(thread_t* thread);
#endif

View File

@@ -0,0 +1,5 @@
#ifdef __i386__
#include <platform/x86/i386/tasking/trapframe.h>
#elif __x86_64__
#include <platform/x86/x86_64/tasking/trapframe.h>
#endif

View File

@@ -0,0 +1,76 @@
#ifndef _KERNEL_PLATFORM_X86_TASKING_TSS_H
#define _KERNEL_PLATFORM_X86_TASKING_TSS_H
#include <libkern/c_attrs.h>
#include <libkern/types.h>
#define SEGTSS_TYPE 0x9 // defined in the Intel's manual 3a
#ifdef __i386__
struct PACKED tss {
uint32_t back_link : 16; // back link to prev tss
uint32_t zero1 : 16; // always zero
uint32_t esp0 : 32; // stack pointer at ring 0
uint32_t ss0 : 16; // stack segment at ring 0
uint32_t zero2 : 16; // always zero
uint32_t esp1 : 32; // stack pointer at ring 1
uint32_t ss1 : 16; // stack segment at ring 1
uint32_t zero3 : 16; // always zero
uint32_t esp2 : 32; // stack pointer at ring 2
uint32_t ss2 : 16; // stack segment at ring 2
uint32_t zero4 : 16; // always zero
uint32_t cr3 : 32;
uint32_t eip : 32;
uint32_t eflag : 32;
uint32_t eax : 32;
uint32_t ecx : 32;
uint32_t edx : 32;
uint32_t ebx : 32;
uint32_t esp : 32;
uint32_t ebp : 32;
uint32_t esi : 32;
uint32_t edi : 32;
uint32_t es : 16;
uint32_t zero5 : 16; // always zero
uint32_t cs : 16;
uint32_t zero6 : 16; // always zero
uint32_t ss : 16;
uint32_t zero7 : 16; // always zero
uint32_t ds : 16;
uint32_t zero8 : 16; // always zero
uint32_t fs : 16;
uint32_t zero9 : 16; // always zero
uint32_t gs : 16;
uint32_t zero10 : 16; // always zero
uint32_t ldt_selector : 16;
uint32_t zero11 : 16; // always zero
uint32_t t : 1;
uint32_t zero12 : 15; // always zero
uint32_t iomap_offset : 16;
};
#elif __x86_64__
struct PACKED tss {
uint32_t res1;
uint64_t rsp0;
uint64_t rsp1;
uint64_t rsp2;
uint64_t res2;
uint64_t ist1;
uint64_t ist2;
uint64_t ist3;
uint64_t ist4;
uint64_t ist5;
uint64_t ist6;
uint64_t ist7;
uint64_t res3;
uint16_t res4;
uint16_t iomap_offset;
};
#endif
typedef struct tss tss_t;
extern tss_t tss;
void set_ltr(uint16_t seg);
#endif //_KERNEL_PLATFORM_X86_TASKING_TSS_H

View File

@@ -0,0 +1,5 @@
#ifdef __i386__
#include <platform/x86/i386/vmm/consts.h>
#elif __x86_64__
#include <platform/x86/x86_64/vmm/consts.h>
#endif

View File

@@ -0,0 +1,5 @@
#ifdef __i386__
#include <platform/x86/i386/vmm/mmu.h>
#elif __x86_64__
#include <platform/x86/x86_64/vmm/mmu.h>
#endif

View File

@@ -0,0 +1,30 @@
#ifndef _KERNEL_PLATFORM_X86_X86_64_TASKING_CONTEXT_H
#define _KERNEL_PLATFORM_X86_X86_64_TASKING_CONTEXT_H
#include <libkern/c_attrs.h>
#include <libkern/types.h>
struct PACKED context {
uint64_t r15;
uint64_t r14;
uint64_t r13;
uint64_t r12;
uint64_t r11;
uint64_t r10;
uint64_t rbx;
uint64_t rbp;
uint64_t rip;
};
typedef struct context context_t;
static inline uintptr_t context_get_instruction_pointer(context_t* ctx)
{
return ctx->rip;
}
static inline void context_set_instruction_pointer(context_t* ctx, uintptr_t ip)
{
ctx->rip = ip;
}
#endif // _KERNEL_PLATFORM_X86_X86_64_TASKING_CONTEXT_H

View File

@@ -0,0 +1,120 @@
#ifndef _KERNEL_PLATFORM_X86_X86_64_TASKING_TRAPFRAME_H
#define _KERNEL_PLATFORM_X86_X86_64_TASKING_TRAPFRAME_H
#include <libkern/c_attrs.h>
#include <libkern/types.h>
#include <platform/x86/gdt.h>
#include <platform/x86/tasking/tss.h>
struct PACKED trapframe {
// all registers
uint64_t r15;
uint64_t r14;
uint64_t r13;
uint64_t r12;
uint64_t r11;
uint64_t r10;
uint64_t r9;
uint64_t r8;
uint64_t rbp;
uint64_t rdi;
uint64_t rsi;
uint64_t rdx;
uint64_t rcx;
uint64_t rbx;
uint64_t rax;
// rest of trap frame
uint64_t gs;
uint64_t fs;
uint64_t int_no;
// below here defined by x86 hardware
uint64_t err;
uint64_t rip;
uint64_t cs;
// below here only when crossing rings, such as from user to kernel
uint64_t rflags;
uint64_t rsp;
uint64_t ss;
};
typedef struct trapframe trapframe_t;
static inline uintptr_t get_stack_pointer(trapframe_t* tf)
{
return tf->rsp;
}
static inline void set_stack_pointer(trapframe_t* tf, uintptr_t sp)
{
tf->rsp = sp;
}
static inline uintptr_t get_frame_pointer(trapframe_t* tf)
{
return tf->rbp;
}
static inline void set_frame_pointer(trapframe_t* tf, uintptr_t bp)
{
tf->rbp = bp;
}
static inline uintptr_t get_instruction_pointer(trapframe_t* tf)
{
return tf->rip;
}
static inline void set_instruction_pointer(trapframe_t* tf, uintptr_t ip)
{
tf->rip = ip;
}
static inline uintptr_t get_syscall_result(trapframe_t* tf)
{
return tf->rax;
}
static inline void set_syscall_result(trapframe_t* tf, uintptr_t val)
{
tf->rax = val;
}
/**
* STACK FUNCTIONS
*/
static inline void tf_push_to_stack(trapframe_t* tf, uintptr_t val)
{
tf->rsp -= sizeof(uintptr_t);
*((uintptr_t*)tf->rsp) = val;
}
static inline uintptr_t tf_pop_to_stack(trapframe_t* tf)
{
uintptr_t val = *((uintptr_t*)tf->rsp);
tf->rsp += sizeof(uintptr_t);
return val;
}
static inline void tf_move_stack_pointer(trapframe_t* tf, int32_t val)
{
tf->rsp += val;
}
static inline void tf_setup_as_user_thread(trapframe_t* tf)
{
tf->cs = (GDT_SEG_UCODE << 3) | DPL_USER;
tf->ss = (GDT_SEG_UDATA << 3) | DPL_USER;
tf->rflags = FL_IF;
}
static inline void tf_setup_as_kernel_thread(trapframe_t* tf)
{
tf->cs = (GDT_SEG_KCODE << 3);
tf->ss = (GDT_SEG_KDATA << 3);
tf->rflags = FL_IF;
}
#endif // _KERNEL_PLATFORM_X86_X86_64_TASKING_TRAPFRAME_H

View File

@@ -0,0 +1,35 @@
#ifndef _KERNEL_PLATFORM_X86_X86_64_VMM_CONSTS_H
#define _KERNEL_PLATFORM_X86_X86_64_VMM_CONSTS_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 (3)
#define PTABLE_LV0_VADDR_OFFSET (12)
#define PTABLE_LV1_VADDR_OFFSET (21)
#define PTABLE_LV2_VADDR_OFFSET (30)
#define PTABLE_LV3_VADDR_OFFSET (39)
#define PTABLE_TOP_KERNEL_OFFSET (VMM_LV3_ENTITY_COUNT / 2)
#define USER_HIGH 0x7fffffffffff
#define KERNEL_BASE 0xffff800000000000
#define KERNEL_PADDR_BASE 0xffffffff00000000 // TODO(x64): up to 4gbs are supported.
#endif //_KERNEL_PLATFORM_X86_X86_64_VMM_CONSTS_H

View File

@@ -0,0 +1,30 @@
#ifndef _KERNEL_PLATFORM_X86_X86_64_VMM_MMU_H
#define _KERNEL_PLATFORM_X86_X86_64_VMM_MMU_H
#include <libkern/types.h>
#include <mem/bits/mmu.h>
// x86_64 uses one table to manage virtual space.
#undef 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_X86_X86_64_VMM_MMU_H