From e2564d112ba03b432f9f44f445cbc4a3ed85751a Mon Sep 17 00:00:00 2001 From: Richard Qin Date: Fri, 17 Apr 2026 01:05:31 +0800 Subject: feat: add initial RISC-V architecture support and core kernel functionality - Implement MMU hardware primitives for RISC-V SV39 in mmu.h - Create trap vector control primitives in trap.h - Define trap frame structure for RISC-V in trapframe.h - Add assembly routines for context switching in swtch.S - Implement trap vector entry/exit in trap_vector.S - Introduce basic error number definitions in errno.h - Provide placement new/delete operators in new.h - Define kernel panic handler in panic.h and implement in panic.cc - Establish basic type definitions in types.h - Create the kernel main entry point in main.cc with global initialization - Add synchronization primitives using spinlocks in spinlock.h and spinlock.cc - Define per-CPU state tracking for interrupts in cpu_state.h - Create a linker script for the kernel in linker.ld - Update module makefiles for kernel and library components --- na/include/lume/errno.h | 43 +++++++++++++++++++++++++++++++++++++++++++ na/include/lume/new.h | 18 ++++++++++++++++++ na/include/lume/panic.h | 16 ++++++++++++++++ na/include/lume/types.h | 39 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 116 insertions(+) create mode 100644 na/include/lume/errno.h create mode 100644 na/include/lume/new.h create mode 100644 na/include/lume/panic.h create mode 100644 na/include/lume/types.h (limited to 'na/include') diff --git a/na/include/lume/errno.h b/na/include/lume/errno.h new file mode 100644 index 0000000..e0786ce --- /dev/null +++ b/na/include/lume/errno.h @@ -0,0 +1,43 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2026 Richard Qin */ +/* from linux kernel linux/include/uapi/asm-generic/errno-base.h*/ + +#ifndef _ASM_GENERIC_ERRNO_BASE_H +#define _ASM_GENERIC_ERRNO_BASE_H + +#define EPERM 1 /* Operation not permitted */ +#define ENOENT 2 /* No such file or directory */ +#define ESRCH 3 /* No such process */ +#define EINTR 4 /* Interrupted system call */ +#define EIO 5 /* I/O error */ +#define ENXIO 6 /* No such device or address */ +#define E2BIG 7 /* Argument list too long */ +#define ENOEXEC 8 /* Exec format error */ +#define EBADF 9 /* Bad file number */ +#define ECHILD 10 /* No child processes */ +#define EAGAIN 11 /* Try again */ +#define ENOMEM 12 /* Out of memory */ +#define EACCES 13 /* Permission denied */ +#define EFAULT 14 /* Bad address */ +#define ENOTBLK 15 /* Block device required */ +#define EBUSY 16 /* Device or resource busy */ +#define EEXIST 17 /* File exists */ +#define EXDEV 18 /* Cross-device link */ +#define ENODEV 19 /* No such device */ +#define ENOTDIR 20 /* Not a directory */ +#define EISDIR 21 /* Is a directory */ +#define EINVAL 22 /* Invalid argument */ +#define ENFILE 23 /* File table overflow */ +#define EMFILE 24 /* Too many open files */ +#define ENOTTY 25 /* Not a typewriter */ +#define ETXTBSY 26 /* Text file busy */ +#define EFBIG 27 /* File too large */ +#define ENOSPC 28 /* No space left on device */ +#define ESPIPE 29 /* Illegal seek */ +#define EROFS 30 /* Read-only file system */ +#define EMLINK 31 /* Too many links */ +#define EPIPE 32 /* Broken pipe */ +#define EDOM 33 /* Math argument out of domain of func */ +#define ERANGE 34 /* Math result not representable */ + +#endif \ No newline at end of file diff --git a/na/include/lume/new.h b/na/include/lume/new.h new file mode 100644 index 0000000..cdcacb8 --- /dev/null +++ b/na/include/lume/new.h @@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* Copyright (C) 2026 Richard Qin */ + +#pragma once + +/* + * Placement New for Freestanding Environments + * + * In -ffreestanding -nostdlib mode, may not be available. + * LumeOS provides its own placement new/delete operators. + * + * Usage: g_obj = new (g_obj_buf) MyClass(args...); + * + * Reference: docs/specs/global_init.md §3.1 + */ + +inline void* operator new(decltype(sizeof(0)), void* ptr) noexcept { return ptr; } +inline void operator delete(void*, void*) noexcept {} diff --git a/na/include/lume/panic.h b/na/include/lume/panic.h new file mode 100644 index 0000000..6316c28 --- /dev/null +++ b/na/include/lume/panic.h @@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* Copyright (C) 2026 Richard Qin */ + +#pragma once + +/* + * Kernel Panic — fatal error handler. + * + * Prints the message (if UART is ready), disables interrupts, + * and enters an infinite wfi loop. Never returns. + * + * Implementation will be provided by lib/kprintf.cc or a + * dedicated panic module. For now this header declares the interface. + */ + +extern "C" [[noreturn]] void kernel_panic(const char* msg, const char* detail = nullptr); diff --git a/na/include/lume/types.h b/na/include/lume/types.h new file mode 100644 index 0000000..a8440f6 --- /dev/null +++ b/na/include/lume/types.h @@ -0,0 +1,39 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* Copyright (C) 2026 Richard Qin */ + +#pragma once + +/* + * LumeOS Base Type Definitions + * + * All kernel code should use these types instead of + * stdint.h (which may not exist in freestanding mode). + */ + +using uint8 = unsigned char; +using uint16 = unsigned short; +using uint32 = unsigned int; +using uint64 = unsigned long long; + +using int8 = signed char; +using int16 = signed short; +using int32 = signed int; +using int64 = signed long long; + +using size_t = uint64; +using ssize_t = int64; +using uintptr = uint64; + +/* Null pointer constant */ +#ifndef nullptr +/* C++11 and later have nullptr built-in */ +#endif + +/* Max CPUs supported */ +inline constexpr int kMaxCPUs = 8; + +/* Page size */ +inline constexpr uint64 kPageSize = 4096; + +/* Kernel virtual address offset */ +inline constexpr uint64 kPageOffset = 0xFFFFFFC000000000ULL; -- cgit v1.2.3