feat: initial scalable x86_64 microkernel skeleton with Limine, Rust, C HAL, and C++20 IPC
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
#pragma once
|
||||
|
||||
#include "types.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* IPC Protocol Flags */
|
||||
#define IPC_FLAG_FAST_REGISTERS (1 << 0) /* Message fits entirely in registers */
|
||||
#define IPC_FLAG_SHM_ATTACHED (1 << 1) /* Attached shared memory buffer */
|
||||
#define IPC_FLAG_CAP_TRANSFER (1 << 2) /* Delegating capability in message */
|
||||
#define IPC_FLAG_NONBLOCKING (1 << 3) /* Return immediately if receiver not ready */
|
||||
|
||||
/* Shared Memory Descriptor for Zero-Copy Transfers */
|
||||
typedef struct ipc_shm_desc {
|
||||
cap_t shm_cap; /* Capability to shared memory region */
|
||||
uint64_t offset; /* Offset within SHM buffer */
|
||||
uint64_t length; /* Length of data payload */
|
||||
uint32_t permissions; /* Read/Write flags */
|
||||
uint32_t _reserved;
|
||||
} __attribute__((packed)) ipc_shm_desc_t;
|
||||
|
||||
/* IPC Message Payload Representation */
|
||||
typedef struct ipc_msg {
|
||||
uint64_t badge; /* Sender authentication badge (injected by kernel) */
|
||||
uint32_t opcode; /* Interface Method / Function ID */
|
||||
uint32_t flags; /* IPC_FLAG_* */
|
||||
|
||||
/* Fast-Path Register Payload (4 x 64-bit = 32 bytes directly passed) */
|
||||
uint64_t args[4];
|
||||
|
||||
/* Zero-Copy Extended Descriptor */
|
||||
ipc_shm_desc_t shm;
|
||||
} __attribute__((aligned(16))) ipc_msg_t;
|
||||
|
||||
/* Standard Common IPC Opcodes */
|
||||
#define IPC_OP_PING 0x0001
|
||||
#define IPC_OP_PONG 0x0002
|
||||
#define IPC_OP_REGISTER_SERVER 0x0010
|
||||
#define IPC_OP_LOOKUP_SERVER 0x0011
|
||||
#define IPC_OP_NOTIFY_EVENT 0x0020
|
||||
#define IPC_OP_READ 0x0100
|
||||
#define IPC_OP_WRITE 0x0101
|
||||
#define IPC_OP_IOCTL 0x0102
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,73 @@
|
||||
#pragma once
|
||||
|
||||
#include "types.h"
|
||||
|
||||
/* Syscall Numbers (passed in RAX) */
|
||||
#define SYS_IPC_CALL 1 /* Synchronous call: send message & wait for reply */
|
||||
#define SYS_IPC_REPLY_RECV 2 /* Server fast-path: reply to current and wait next */
|
||||
#define SYS_IPC_SEND 3 /* Asynchronous/non-blocking send */
|
||||
#define SYS_IPC_RECV 4 /* Block waiting for incoming message */
|
||||
#define SYS_MEM_MAP 5 /* Map memory / shared memory region */
|
||||
#define SYS_CAP_GRANT 6 /* Delegate capability to CNode */
|
||||
#define SYS_THREAD_YIELD 7 /* Yield CPU slice */
|
||||
#define SYS_THREAD_EXIT 8 /* Terminate current thread */
|
||||
#define SYS_LOG_DEBUG 9 /* Direct Ring 0 debug print */
|
||||
|
||||
#ifndef __ASSEMBLER__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Low-level inline syscall invocations adhering to System V AMD64 ABI + Fast-Path */
|
||||
|
||||
static inline sysret_t sys_ipc_call_raw(cap_t dest_cap, uint64_t msg_code,
|
||||
uint64_t arg0, uint64_t arg1,
|
||||
uint64_t arg2, uint64_t arg3,
|
||||
uint64_t *out0, uint64_t *out1) {
|
||||
register uint64_t rax __asm__("rax") = SYS_IPC_CALL;
|
||||
register uint64_t rdi __asm__("rdi") = dest_cap;
|
||||
register uint64_t rsi __asm__("rsi") = msg_code;
|
||||
register uint64_t rdx __asm__("rdx") = arg0;
|
||||
register uint64_t r8 __asm__("r8") = arg1;
|
||||
register uint64_t r9 __asm__("r9") = arg2;
|
||||
register uint64_t r10 __asm__("r10") = arg3;
|
||||
|
||||
__asm__ volatile(
|
||||
"syscall"
|
||||
: "+r"(rax), "+r"(rdx), "+r"(r8)
|
||||
: "r"(rdi), "r"(rsi), "r"(r9), "r"(r10)
|
||||
: "rcx", "r11", "memory"
|
||||
);
|
||||
|
||||
if (out0) *out0 = rdx;
|
||||
if (out1) *out1 = r8;
|
||||
return (sysret_t)rax;
|
||||
}
|
||||
|
||||
static inline sysret_t sys_yield(void) {
|
||||
register uint64_t rax __asm__("rax") = SYS_THREAD_YIELD;
|
||||
__asm__ volatile("syscall" : "+r"(rax) : : "rcx", "r11", "memory");
|
||||
return (sysret_t)rax;
|
||||
}
|
||||
|
||||
static inline void sys_exit(int code) {
|
||||
register uint64_t rax __asm__("rax") = SYS_THREAD_EXIT;
|
||||
register uint64_t rdi __asm__("rdi") = (uint64_t)code;
|
||||
__asm__ volatile("syscall" : "+r"(rax) : "r"(rdi) : "rcx", "r11", "memory");
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
static inline sysret_t sys_log_debug(const char *msg, size_t len) {
|
||||
register uint64_t rax __asm__("rax") = SYS_LOG_DEBUG;
|
||||
register uint64_t rdi __asm__("rdi") = (uint64_t)msg;
|
||||
register uint64_t rsi __asm__("rsi") = (uint64_t)len;
|
||||
__asm__ volatile("syscall" : "+r"(rax) : "r"(rdi), "r"(rsi) : "rcx", "r11", "memory");
|
||||
return (sysret_t)rax;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __ASSEMBLER__ */
|
||||
@@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef __ASSEMBLER__
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#else
|
||||
#define UINT64_C(c) c
|
||||
#endif
|
||||
|
||||
/* Standard Type Definitions */
|
||||
typedef uint64_t cap_t; /* Capability handle */
|
||||
typedef uint64_t vaddr_t; /* Virtual Address */
|
||||
typedef uint64_t paddr_t; /* Physical Address */
|
||||
typedef int64_t sysret_t; /* Syscall Return Code */
|
||||
typedef uint32_t pid_t; /* Process ID */
|
||||
typedef uint32_t tid_t; /* Thread ID */
|
||||
|
||||
/* Standard Return / Error Codes */
|
||||
#define SYS_OK 0
|
||||
#define SYS_ERR_INVALID_ARG -1
|
||||
#define SYS_ERR_INVALID_CAP -2
|
||||
#define SYS_ERR_NO_MEMORY -3
|
||||
#define SYS_ERR_IPC_TIMEOUT -4
|
||||
#define SYS_ERR_IPC_DEADLOCK -5
|
||||
#define SYS_ERR_PERMISSION -6
|
||||
#define SYS_ERR_NOT_FOUND -7
|
||||
#define SYS_ERR_ALREADY_EXISTS -8
|
||||
#define SYS_ERR_BUSY -9
|
||||
|
||||
/* Standard Special Capabilities */
|
||||
#define CAP_NULL 0
|
||||
#define CAP_SELF_TASK 1
|
||||
#define CAP_SELF_THREAD 2
|
||||
#define CAP_SELF_VMSPACE 3
|
||||
#define CAP_ROOT_CSPACE 4
|
||||
#define CAP_ROOT_IRQ 5
|
||||
#define CAP_ROOT_IO 6
|
||||
Reference in New Issue
Block a user