kernel: separate headers into kernel and user headers

all kernel headers have been moved from include/mango to include/kernel
and include definitions that are only relevant to kernel-space.

any definitions that are relevant to both kernel- and user-space
(i.e. type definitions, syscall IDs) have been moved to
include/mango within libmango.
This commit is contained in:
2026-02-19 18:54:48 +00:00
parent e3dd48a0fa
commit 6019c9307d
117 changed files with 1361 additions and 3845 deletions

View File

@@ -1,5 +1,5 @@
#include <unistd.h>
#include <mango/machine/cpu.h>
#include <kernel/machine/cpu.h>
int ml_init_bootcpu(void)
{

View File

@@ -1,5 +1,5 @@
#include <mango/machine/hwlock.h>
#include <mango/compiler.h>
#include <kernel/machine/hwlock.h>
#include <kernel/compiler.h>
void ml_hwlock_lock(ml_hwlock_t *lck)
{

View File

@@ -1,11 +1,11 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <mango/init.h>
#include <mango/memblock.h>
#include <mango/vm.h>
#include <mango/object.h>
#include <mango/printk.h>
#include <kernel/init.h>
#include <kernel/memblock.h>
#include <kernel/vm.h>
#include <kernel/object.h>
#include <kernel/printk.h>
#include <arch/stdcon.h>
#include <sys/mman.h>

View File

@@ -1,4 +1,4 @@
#include <mango/init.h>
#include <kernel/init.h>
#ifdef __APPLE__
extern char __start_initcall0[] __asm("section$start$__DATA$__initcall0.init");

View File

@@ -1,10 +1,10 @@
#include <mango/libc/string.h>
#include <mango/libc/ctype.h>
#include <kernel/libc/string.h>
#include <kernel/libc/ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <mango/console.h>
#include <mango/vm.h>
#include <mango/printk.h>
#include <kernel/console.h>
#include <kernel/vm.h>
#include <kernel/printk.h>
static void stdcon_write(struct console *con, const char *s, unsigned int len)
{

View File

@@ -1,5 +1,5 @@
#include <mango/sched.h>
#include <mango/compiler.h>
#include <kernel/sched.h>
#include <kernel/compiler.h>
//size_t THREAD_sp = offsetof(struct thread, tr_sp);

View File

@@ -1,5 +1,5 @@
#include <arch/msr.h>
#include <mango/machine/cpu.h>
#include <kernel/machine/cpu.h>
int ml_cpu_block_init(ml_cpu_block *p)
{

View File

@@ -1,8 +1,8 @@
#include "mango/types.h"
#include <mango/memblock.h>
#include <mango/printk.h>
#include <mango/util.h>
#include <arch/e820.h>
#include <kernel/memblock.h>
#include <kernel/printk.h>
#include <kernel/types.h>
#include <kernel/util.h>
void e820_scan(multiboot_memory_map_t *mmap, size_t len)
{

View File

@@ -1,7 +1,7 @@
#include <arch/gdt.h>
#include <arch/tss.h>
#include <mango/libc/string.h>
#include <mango/types.h>
#include <kernel/libc/string.h>
#include <kernel/types.h>
#include <stddef.h>
static void init_entry(struct gdt_entry *entry, int access, int flags)

View File

@@ -2,7 +2,7 @@
#define ARCH_GDT_H_
#include <arch/tss.h>
#include <mango/compiler.h>
#include <kernel/compiler.h>
#include <stdint.h>
#ifdef __cplusplus

View File

@@ -1,8 +1,8 @@
#ifndef ARCH_IRQ_H_
#define ARCH_IRQ_H_
#include <mango/compiler.h>
#include <mango/queue.h>
#include <kernel/compiler.h>
#include <kernel/queue.h>
#include <stdint.h>
#ifdef __cplusplus

View File

@@ -1,8 +1,8 @@
#ifndef ARCH_PAGING_H_
#define ARCH_PAGING_H_
#include <mango/types.h>
#include <mango/compiler.h>
#include <kernel/types.h>
#include <kernel/compiler.h>
#ifdef __cplusplus
extern "C" {

View File

@@ -1,8 +1,8 @@
#ifndef ARCH_TSS_H_
#define ARCH_TSS_H_
#include <mango/compiler.h>
#include <mango/types.h>
#include <kernel/compiler.h>
#include <kernel/types.h>
#include <stdint.h>
#define TSS_GDT_INDEX 5

View File

@@ -1,73 +0,0 @@
#ifndef MANGO_X86_64_CPU_H_
#define MANGO_X86_64_CPU_H_
#include <arch/gdt.h>
#include <arch/irq.h>
#include <arch/tss.h>
#include <mango/types.h>
#ifdef __cplusplus
extern "C" {
#endif
#define ML_BIG_ENDIAN 0
#define ml_cpu_block_get_id(p) ((p)->c_cpu_id)
#define ml_cpu_block_get_data(p) ((p)->c_data)
#if 0
#define ml_read_sp(sp, bp) \
asm volatile("mov %%rsp, %0" : "=r"(sp)); \
asm volatile("mov %%rbp, %0" : "=r"(bp));
#endif
struct cpu_data;
typedef struct ml_cpu_block {
struct ml_cpu_block *c_this;
struct gdt c_gdt;
struct gdt_ptr c_gdt_ptr;
struct tss c_tss;
struct tss_ptr c_tss_ptr;
struct idt_ptr c_idt_ptr;
unsigned int c_cpu_id;
struct cpu_data *c_data;
} ml_cpu_block;
struct ml_cpu_context {
uint64_t r15, r14, r13, r12, r11, r10, r9, r8;
uint64_t rdi, rsi, rbp, unused_rsp, rbx, rdx, rcx, rax;
uint64_t int_no, err_no;
uint64_t rip, cs, rflags, rsp, ss;
} __packed;
#define ml_cpu_pause() __asm__ __volatile__("hlt")
#define ml_cpu_relax() __asm__ __volatile__("pause")
#define ml_int_disable() __asm__ __volatile__("cli")
#define ml_int_enable() __asm__ __volatile__("sti")
extern int ml_init_bootcpu(void);
extern int ml_cpu_block_init(ml_cpu_block *p);
extern int ml_cpu_block_use(ml_cpu_block *p);
extern virt_addr_t ml_cpu_block_get_ustack(ml_cpu_block *p);
extern virt_addr_t ml_cpu_block_get_kstack(ml_cpu_block *p);
extern void ml_cpu_block_set_ustack(ml_cpu_block *p, virt_addr_t sp);
extern void ml_cpu_block_set_kstack(ml_cpu_block *p, virt_addr_t sp);
/* defined in cpu_ctrl.S */
extern void ml_halt_cpu(void);
extern ml_cpu_block *ml_this_cpu(void);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -1,22 +0,0 @@
#ifndef MANGO_X86_64_HWLOCK_H_
#define MANGO_X86_64_HWLOCK_H_
#define ML_HWLOCK_INIT (0)
#ifdef __cplusplus
extern "C" {
#endif
typedef int ml_hwlock_t;
extern void ml_hwlock_lock(ml_hwlock_t *lck);
extern void ml_hwlock_unlock(ml_hwlock_t *lck);
extern void ml_hwlock_lock_irqsave(ml_hwlock_t *lck, unsigned long *flags);
extern void ml_hwlock_unlock_irqrestore(ml_hwlock_t *lck, unsigned long flags);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -1,28 +0,0 @@
#ifndef MANGO_X86_64_INIT_H_
#define MANGO_X86_64_INIT_H_
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
#define __X2(x) #x
#define __X(x) __X2(x)
#define __define_initcall(fn, id) \
static initcall_t __initcall_##fn##id __used __section( \
".initcall" __X(id) ".init") \
= (fn)
extern int ml_init(uintptr_t arg);
extern const struct framebuffer_varinfo *bootfb_varinfo(void);
extern const struct framebuffer_fixedinfo *bootfb_fixedinfo(void);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -1,5 +0,0 @@
#ifndef MANGO_X86_64_IRQ_H_
#define MANGO_X86_64_IRQ_H_
#endif

View File

@@ -1,12 +0,0 @@
#ifndef MANGO_X86_64_PANIC_H_
#define MANGO_X86_64_PANIC_H_
#include <stdint.h>
struct ml_cpu_context;
extern void ml_print_cpu_state(struct ml_cpu_context *ctx);
extern void ml_print_stack_trace(uintptr_t ip);
extern void ml_print_stack_trace_irq(struct ml_cpu_context *ctx);
#endif

View File

@@ -1,11 +0,0 @@
#ifndef MANGO_X86_64_PMAP_H_
#define MANGO_X86_64_PMAP_H_
#include <arch/paging.h>
#define ML_PMAP_INVALID ((uintptr_t)-1)
typedef pml4t_ptr_t ml_pmap_t;
typedef uint64_t ml_pfn_t;
#endif

View File

@@ -1,29 +0,0 @@
#ifndef MANGO_X86_64_THREAD_H_
#define MANGO_X86_64_THREAD_H_
#include <mango/sched.h>
struct ml_cpu_context;
/* switch from one thread to another. the stack of the `to` thread must have
* been prepared in one of two ways:
* 1) a previous call to ml_thread_switch where it was the `from` thread.
* 2) a call to ml_thread_prepare_kernel_context
* the switch occurs entirely with kernel mode. a further return from an
* interrupt context is then used to return to usermode if necessary.
*/
extern void ml_thread_switch(struct thread *from, struct thread *to);
/* perform the initial transition to userspace. the stack must be prepared using
* ml_thread_prepare_user_context before this function can be used */
extern void ml_thread_switch_user(void);
/* prepare the stack so that ml_thread_switch can jump to the specified IP/SP */
extern void ml_thread_prepare_kernel_context(uintptr_t ip, uintptr_t *sp);
/* prepare the stack so that ml_thread_switch_user can jump to usermode
* with the specified IP/user SP */
extern void ml_thread_prepare_user_context(
virt_addr_t ip,
virt_addr_t user_sp,
virt_addr_t *kernel_sp);
#endif

View File

@@ -1,30 +0,0 @@
#ifndef MANGO_X86_64_VM_H_
#define MANGO_X86_64_VM_H_
/* kernel higher-half base virtual address. */
#define VM_KERNEL_VOFFSET 0xFFFFFFFF80000000
/* direct page-mapping region.
NOTE that these are the maximum bounds of this region.
the actual size depends on the amount of physical
memory present. */
#define VM_PAGEMAP_BASE 0xFFFF888000000000
#define VM_PAGEMAP_LIMIT 0xFFFFC87FFFFFFFFF
#define VM_PAGE_SIZE 0x1000
#define VM_PAGE_MASK (VM_PAGE_SIZE - 1)
#define VM_PAGE_SHIFT 12
#define VM_PAGE_MIN_ORDER VM_PAGE_4K
#define VM_PAGE_MAX_ORDER VM_PAGE_8M
#define VM_ZONE_MIN VM_ZONE_DMA
#define VM_ZONE_MAX VM_ZONE_NORMAL
#define VM_USER_BASE 0x0000000000100000
#define VM_USER_LIMIT 0x00007fffffffffff
#define VM_KERNEL_BASE 0XFFFF800000000000
#define VM_KERNEL_LIMIT 0XFFFFFFFFFFFFFFFF
#endif

View File

@@ -2,21 +2,21 @@
#include <arch/pit.h>
#include <arch/serial.h>
#include <arch/vgacon.h>
#include <mango/arg.h>
#include <mango/bsp.h>
#include <mango/clock.h>
#include <mango/console.h>
#include <mango/cpu.h>
#include <mango/init.h>
#include <mango/libc/stdio.h>
#include <mango/machine/cpu.h>
#include <mango/memblock.h>
#include <mango/object.h>
#include <mango/percpu.h>
#include <mango/pmap.h>
#include <mango/printk.h>
#include <mango/types.h>
#include <mango/vm.h>
#include <kernel/arg.h>
#include <kernel/bsp.h>
#include <kernel/clock.h>
#include <kernel/console.h>
#include <kernel/cpu.h>
#include <kernel/init.h>
#include <kernel/libc/stdio.h>
#include <kernel/machine/cpu.h>
#include <kernel/memblock.h>
#include <kernel/object.h>
#include <kernel/percpu.h>
#include <kernel/pmap.h>
#include <kernel/printk.h>
#include <kernel/types.h>
#include <kernel/vm.h>
#define PTR32(x) ((void *)((uintptr_t)(x)))

View File

@@ -1,4 +1,4 @@
#include <mango/init.h>
#include <kernel/init.h>
extern char __initcall0_start[];
extern char __initcall1_start[];

View File

@@ -1,13 +1,13 @@
#include <arch/irq.h>
#include <arch/msr.h>
#include <arch/ports.h>
#include <mango/cpu.h>
#include <mango/libc/string.h>
#include <mango/machine/cpu.h>
#include <mango/machine/irq.h>
#include <mango/panic.h>
#include <mango/sched.h>
#include <mango/syscall.h>
#include <kernel/cpu.h>
#include <kernel/libc/string.h>
#include <kernel/machine/cpu.h>
#include <kernel/machine/irq.h>
#include <kernel/panic.h>
#include <kernel/sched.h>
#include <kernel/syscall.h>
#include <stddef.h>
#define MAX_ISR_HANDLERS 16
@@ -31,7 +31,7 @@ static uintptr_t int_entry_points[NR_IDT_ENTRIES];
static void set_syscall_gate(uintptr_t rip)
{
uint64_t user_cs = 0x13;
uint64_t kernel_cs = 0x8;
uint64_t kernel_cs = 0x08;
uintptr_t star_reg = 0xC0000081;
uintptr_t lstar_reg = 0xC0000082;
@@ -97,11 +97,7 @@ static void pf_handler(struct ml_cpu_context *regs)
virt_addr_t fault_ptr = pf_faultptr();
kern_status_t status = KERN_FATAL_ERROR;
if (regs->err_no & PF_USER) {
status = pmap_handle_fault(fault_ptr, fault_flags);
}
kern_status_t status = pmap_handle_fault(fault_ptr, fault_flags);
if (status == KERN_OK) {
return;

View File

@@ -1,9 +1,9 @@
#include <arch/irq.h>
#include <mango/libc/stdio.h>
#include <mango/machine/cpu.h>
#include <mango/machine/panic.h>
#include <mango/printk.h>
#include <mango/vm.h>
#include <kernel/libc/stdio.h>
#include <kernel/machine/cpu.h>
#include <kernel/machine/panic.h>
#include <kernel/printk.h>
#include <kernel/vm.h>
#define R_CF 0
#define R_PF 2

View File

@@ -1,8 +1,8 @@
#include <arch/irq.h>
#include <arch/ports.h>
#include <mango/clock.h>
#include <mango/cpu.h>
#include <mango/printk.h>
#include <kernel/clock.h>
#include <kernel/cpu.h>
#include <kernel/printk.h>
#define PIT_COUNTER0 0x40
#define PIT_CMD 0x43

View File

@@ -1,14 +1,14 @@
#include <mango/compiler.h>
#include <mango/libc/stdio.h>
#include <mango/memblock.h>
#include <mango/pmap.h>
#include <mango/printk.h>
#include <mango/sched.h>
#include <kernel/compiler.h>
#include <kernel/libc/stdio.h>
#include <kernel/memblock.h>
#include <kernel/pmap.h>
#include <kernel/printk.h>
#include <kernel/sched.h>
#include <kernel/types.h>
#include <kernel/vm-object.h>
#include <kernel/vm-region.h>
#include <kernel/vm.h>
#include <mango/status.h>
#include <mango/types.h>
#include <mango/vm-object.h>
#include <mango/vm-region.h>
#include <mango/vm.h>
/* some helpful datasize constants */
#define C_1GiB 0x40000000ULL
@@ -43,7 +43,7 @@ static pmap_t alloc_pmap(void)
return vm_virt_to_phys(p);
}
static pte_t make_pte(pfn_t pfn, enum vm_prot prot, enum page_size size)
static pte_t make_pte(pfn_t pfn, vm_prot_t prot, enum page_size size)
{
pte_t v = pfn;
@@ -139,7 +139,7 @@ static kern_status_t do_pmap_add(
pmap_t pmap,
virt_addr_t pv,
pfn_t pfn,
enum vm_prot prot,
vm_prot_t prot,
enum page_size size)
{
unsigned int pml4t_index = BAD_INDEX, pdpt_index = BAD_INDEX,
@@ -371,7 +371,7 @@ kern_status_t pmap_add(
pmap_t pmap,
virt_addr_t p,
pfn_t pfn,
enum vm_prot prot,
vm_prot_t prot,
enum pmap_flags flags)
{
enum page_size ps = PS_4K;
@@ -387,7 +387,7 @@ kern_status_t pmap_add_block(
virt_addr_t p,
pfn_t pfn,
size_t len,
enum vm_prot prot,
vm_prot_t prot,
enum pmap_flags flags)
{
return KERN_OK;

View File

@@ -1,8 +1,8 @@
#include <arch/irq.h>
#include <arch/ports.h>
#include <arch/serial.h>
#include <mango/libc/stdio.h>
#include <mango/printk.h>
#include <kernel/libc/stdio.h>
#include <kernel/printk.h>
#define COM1 0x3F8
#define COM2 0x2F8

View File

@@ -1,5 +1,5 @@
#include <mango/machine/cpu.h>
#include <mango/machine/thread.h>
#include <kernel/machine/cpu.h>
#include <kernel/machine/thread.h>
/* this is the context information restored by ml_thread_switch.
* since ml_thread_switch only jumps to kernel-mode, IRETQ isn't used,

View File

@@ -2,7 +2,7 @@
#include <arch/gdt.h>
#include <arch/tss.h>
#include <mango/libc/string.h>
#include <kernel/libc/string.h>
static void tss_flush(int index)
{

View File

@@ -1,9 +1,9 @@
#include <arch/irq.h>
#include <arch/ports.h>
#include <arch/serial.h>
#include <mango/libc/stdio.h>
#include <mango/machine/vm.h>
#include <mango/printk.h>
#include <kernel/libc/stdio.h>
#include <kernel/machine/vm.h>
#include <kernel/printk.h>
struct vga_console {
uint16_t *vga_framebuffer;