Initial commit: SUB-OS 64-Bit Production Monolithic Operating System with LazyBox userland suite

This commit is contained in:
subhobhai943 committed 2026-08-16 21:23:52 +05:30
commit cd9c786d14
88 files changed
+7132

No files matched your search

+30
View File
@@ -0,0 +1,30 @@
#ifndef _ARCH_X86_64_CPUID_H
#define _ARCH_X86_64_CPUID_H
#include <stdint.h>
#include <stdbool.h>
typedef struct {
char vendor[13];
char model[49];
uint32_t family;
uint32_t model_id;
uint32_t stepping;
bool has_fpu;
bool has_tsc;
bool has_msr;
bool has_pae;
bool has_apic;
bool has_sse;
bool has_sse2;
bool has_sse3;
bool has_sse4_1;
bool has_sse4_2;
bool has_avx;
bool has_rdrand;
} cpu_info_t;
void cpuid_detect(cpu_info_t* info);
const cpu_info_t* cpuid_get_info(void);
#endif // _ARCH_X86_64_CPUID_H
+47
View File
@@ -0,0 +1,47 @@
#ifndef _ARCH_X86_64_GDT_H
#define _ARCH_X86_64_GDT_H
#include <stdint.h>
#define GDT_KERNEL_CODE 0x08
#define GDT_KERNEL_DATA 0x10
#define GDT_USER_CODE 0x18
#define GDT_USER_DATA 0x20
#define GDT_TSS 0x28
typedef struct {
uint16_t limit_low;
uint16_t base_low;
uint8_t base_middle;
uint8_t access;
uint8_t granularity;
uint8_t base_high;
} __attribute__((packed)) gdt_entry_t;
typedef struct {
uint16_t limit;
uint64_t base;
} __attribute__((packed)) gdt_ptr_t;
typedef struct {
uint32_t reserved0;
uint64_t rsp0;
uint64_t rsp1;
uint64_t rsp2;
uint64_t reserved1;
uint64_t ist1;
uint64_t ist2;
uint64_t ist3;
uint64_t ist4;
uint64_t ist5;
uint64_t ist6;
uint64_t ist7;
uint64_t reserved2;
uint16_t reserved3;
uint16_t iomap_base;
} __attribute__((packed)) tss_entry_t;
void gdt_init(void);
void gdt_set_kernel_stack(uint64_t stack);
#endif // _ARCH_X86_64_GDT_H
+28
View File
@@ -0,0 +1,28 @@
#ifndef _ARCH_X86_64_IDT_H
#define _ARCH_X86_64_IDT_H
#include <stdint.h>
#define IDT_ENTRIES 256
typedef struct {
uint16_t isr_low;
uint16_t kernel_cs;
uint8_t ist;
uint8_t attributes;
uint16_t isr_mid;
uint32_t isr_high;
uint32_t reserved;
} __attribute__((packed)) idt_entry_t;
typedef struct {
uint16_t limit;
uint64_t base;
} __attribute__((packed)) idt_ptr_t;
void idt_init(void);
void idt_set_gate(uint8_t vector, void* isr, uint8_t flags);
void idt_enable_gate(uint8_t vector);
void idt_disable_gate(uint8_t vector);
#endif // _ARCH_X86_64_IDT_H
+52
View File
@@ -0,0 +1,52 @@
#ifndef _ARCH_X86_64_IO_H
#define _ARCH_X86_64_IO_H
#include <stdint.h>
static inline void outb(uint16_t port, uint8_t val) {
__asm__ volatile ("outb %0, %1" : : "a"(val), "Nd"(port));
}
static inline uint8_t inb(uint16_t port) {
uint8_t ret;
__asm__ volatile ("inb %1, %0" : "=a"(ret) : "Nd"(port));
return ret;
}
static inline void outw(uint16_t port, uint16_t val) {
__asm__ volatile ("outw %0, %1" : : "a"(val), "Nd"(port));
}
static inline uint16_t inw(uint16_t port) {
uint16_t ret;
__asm__ volatile ("inw %1, %0" : "=a"(ret) : "Nd"(port));
return ret;
}
static inline void outl(uint16_t port, uint32_t val) {
__asm__ volatile ("outl %0, %1" : : "a"(val), "Nd"(port));
}
static inline uint32_t inl(uint16_t port) {
uint32_t ret;
__asm__ volatile ("inl %1, %0" : "=a"(ret) : "Nd"(port));
return ret;
}
static inline void io_wait(void) {
outb(0x80, 0);
}
static inline void cli(void) {
__asm__ volatile ("cli");
}
static inline void sti(void) {
__asm__ volatile ("sti");
}
static inline void hlt(void) {
__asm__ volatile ("hlt");
}
#endif // _ARCH_X86_64_IO_H
+19
View File
@@ -0,0 +1,19 @@
#ifndef _ARCH_X86_64_ISR_H
#define _ARCH_X86_64_ISR_H
#include <stdint.h>
typedef struct {
uint64_t r15, r14, r13, r12, r11, r10, r9, r8;
uint64_t rbp, rdi, rsi, rdx, rcx, rbx, rax;
uint64_t int_no, error_code;
uint64_t rip, cs, rflags, rsp, ss;
} __attribute__((packed)) registers_t;
typedef void (*isr_handler_t)(registers_t*);
void isr_init(void);
void isr_register_handler(uint8_t int_no, isr_handler_t handler);
void isr_unregister_handler(uint8_t int_no);
#endif // _ARCH_X86_64_ISR_H
+25
View File
@@ -0,0 +1,25 @@
#ifndef _ARCH_X86_64_PAGING_H
#define _ARCH_X86_64_PAGING_H
#include <stdint.h>
#include <stdbool.h>
#define PAGE_SIZE 4096
#define PAGE_PRESENT (1ULL << 0)
#define PAGE_WRITABLE (1ULL << 1)
#define PAGE_USER (1ULL << 2)
#define PAGE_HUGE (1ULL << 7)
#define PAGE_NX (1ULL << 63)
typedef uint64_t pml4e_t;
typedef uint64_t pdpte_t;
typedef uint64_t pde_t;
typedef uint64_t pte_t;
void paging_init(void);
void paging_map_page(uint64_t virt, uint64_t phys, uint64_t flags);
void paging_unmap_page(uint64_t virt);
uint64_t paging_get_phys(uint64_t virt);
void paging_flush_tlb_single(uint64_t virt);
#endif // _ARCH_X86_64_PAGING_H
+12
View File
@@ -0,0 +1,12 @@
#ifndef _ARCH_X86_64_PIC_H
#define _ARCH_X86_64_PIC_H
#include <stdint.h>
void pic_init(void);
void pic_send_eoi(uint8_t irq);
void pic_set_mask(uint8_t irq);
void pic_clear_mask(uint8_t irq);
void pic_disable(void);
#endif // _ARCH_X86_64_PIC_H
+11
View File
@@ -0,0 +1,11 @@
#ifndef _ARCH_X86_64_PIT_H
#define _ARCH_X86_64_PIT_H
#include <stdint.h>
void pit_init(uint32_t frequency);
uint64_t pit_get_ticks(void);
uint64_t pit_get_uptime_ms(void);
void pit_sleep(uint32_t milliseconds);
#endif // _ARCH_X86_64_PIT_H
+42
View File
@@ -0,0 +1,42 @@
#ifndef _CRYPTO_CRYPTO_H
#define _CRYPTO_CRYPTO_H
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
// CRC32 API
uint32_t crc32(uint32_t crc, const void* buf, size_t size);
// MD5 API
typedef struct {
uint32_t state[4];
uint32_t count[2];
uint8_t buffer[64];
} md5_ctx_t;
void md5_init(md5_ctx_t* ctx);
void md5_update(md5_ctx_t* ctx, const void* input, size_t input_len);
void md5_final(uint8_t digest[16], md5_ctx_t* ctx);
void md5_string(const char* str, char output[33]);
// SHA-256 API
typedef struct {
uint32_t state[8];
uint64_t bitlen;
uint8_t buffer[64];
size_t datalen;
} sha256_ctx_t;
void sha256_init(sha256_ctx_t* ctx);
void sha256_update(sha256_ctx_t* ctx, const void* data, size_t len);
void sha256_final(uint8_t hash[32], sha256_ctx_t* ctx);
void sha256_string(const char* str, char output[65]);
// PRNG (Cryptographic Pseudo-Random Number Generator)
void prng_seed(uint64_t seed1, uint64_t seed2);
uint64_t prng_rand64(void);
uint32_t prng_rand32(void);
void prng_get_bytes(void* buf, size_t len);
#endif // _CRYPTO_CRYPTO_H
+28
View File
@@ -0,0 +1,28 @@
#ifndef _DRIVERS_ATA_H
#define _DRIVERS_ATA_H
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#define ATA_SECTOR_SIZE 512
#define ATA_PRIMARY_IO 0x1F0
#define ATA_PRIMARY_CTRL 0x3F6
#define ATA_SECONDARY_IO 0x170
#define ATA_SECONDARY_CTRL 0x376
typedef struct {
bool present;
bool is_master;
uint32_t sector_count;
char model[41];
char serial[21];
} ata_device_t;
bool ata_init(void);
bool ata_read_sectors(uint32_t lba, uint8_t count, void* buffer);
bool ata_write_sectors(uint32_t lba, uint8_t count, const void* buffer);
const ata_device_t* ata_get_primary_master(void);
#endif // _DRIVERS_ATA_H
+22
View File
@@ -0,0 +1,22 @@
#ifndef _DRIVERS_E1000_H
#define _DRIVERS_E1000_H
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#define E1000_VENDOR_ID 0x8086
#define E1000_DEV_82540EM 0x100E
#define E1000_DEV_82545EM 0x100F
bool e1000_init(void);
void e1000_send(const void* data, uint16_t len);
void e1000_get_mac(uint8_t* mac_out);
bool e1000_is_link_up(void);
uint64_t e1000_get_rx_packets(void);
uint64_t e1000_get_tx_packets(void);
uint64_t e1000_get_rx_bytes(void);
uint64_t e1000_get_tx_bytes(void);
#endif // _DRIVERS_E1000_H
+39
View File
@@ -0,0 +1,39 @@
#ifndef _DRIVERS_KEYBOARD_H
#define _DRIVERS_KEYBOARD_H
#include <stdint.h>
#include <stdbool.h>
#define KEY_SPECIAL_FLAG 0x0100
enum special_keys {
KEY_UP = KEY_SPECIAL_FLAG | 0x01,
KEY_DOWN = KEY_SPECIAL_FLAG | 0x02,
KEY_LEFT = KEY_SPECIAL_FLAG | 0x03,
KEY_RIGHT = KEY_SPECIAL_FLAG | 0x04,
KEY_HOME = KEY_SPECIAL_FLAG | 0x05,
KEY_END = KEY_SPECIAL_FLAG | 0x06,
KEY_PAGE_UP = KEY_SPECIAL_FLAG | 0x07,
KEY_PAGE_DOWN= KEY_SPECIAL_FLAG | 0x08,
KEY_INSERT = KEY_SPECIAL_FLAG | 0x09,
KEY_DELETE = KEY_SPECIAL_FLAG | 0x0A,
KEY_F1 = KEY_SPECIAL_FLAG | 0x11,
KEY_F2 = KEY_SPECIAL_FLAG | 0x12,
KEY_F3 = KEY_SPECIAL_FLAG | 0x13,
KEY_F4 = KEY_SPECIAL_FLAG | 0x14,
KEY_F5 = KEY_SPECIAL_FLAG | 0x15,
KEY_F6 = KEY_SPECIAL_FLAG | 0x16,
KEY_F7 = KEY_SPECIAL_FLAG | 0x17,
KEY_F8 = KEY_SPECIAL_FLAG | 0x18,
KEY_F9 = KEY_SPECIAL_FLAG | 0x19,
KEY_F10 = KEY_SPECIAL_FLAG | 0x1A,
KEY_F11 = KEY_SPECIAL_FLAG | 0x1B,
KEY_F12 = KEY_SPECIAL_FLAG | 0x1C,
};
void keyboard_init(void);
uint16_t keyboard_get_key(void);
bool keyboard_has_key(void);
char keyboard_get_char(void);
#endif // _DRIVERS_KEYBOARD_H
+46
View File
@@ -0,0 +1,46 @@
#ifndef _DRIVERS_PCI_H
#define _DRIVERS_PCI_H
#include <stdint.h>
#include <stdbool.h>
#define PCI_CONFIG_ADDRESS 0xCF8
#define PCI_CONFIG_DATA 0xCFC
#define PCI_CLASS_NETWORK 0x02
#define PCI_CLASS_DISPLAY 0x03
#define PCI_CLASS_STORAGE 0x01
#define PCI_SUBCLASS_ETHERNET 0x00
#define PCI_SUBCLASS_IDE 0x01
#define PCI_SUBCLASS_SATA 0x06
typedef struct pci_device {
uint8_t bus;
uint8_t slot;
uint8_t function;
uint16_t vendor_id;
uint16_t device_id;
uint8_t class_id;
uint8_t subclass_id;
uint8_t prog_if;
uint8_t revision;
uint8_t irq;
uint32_t bar[6];
uint8_t bar_type[6];
struct pci_device* next;
} pci_device_t;
void pci_init(void);
pci_device_t* pci_get_devices(void);
pci_device_t* pci_find_device(uint16_t vendor_id, uint16_t device_id);
pci_device_t* pci_find_class(uint8_t class_id, uint8_t subclass_id);
void pci_enable_bus_mastering(pci_device_t* dev);
uint32_t pci_config_read32(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset);
uint16_t pci_config_read16(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset);
uint8_t pci_config_read8(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset);
void pci_config_write32(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset, uint32_t val);
void pci_config_write16(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset, uint16_t val);
#endif // _DRIVERS_PCI_H
+16
View File
@@ -0,0 +1,16 @@
#ifndef _DRIVERS_SERIAL_H
#define _DRIVERS_SERIAL_H
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#define COM1_PORT 0x3F8
void serial_init(void);
void serial_write_char(char c);
void serial_write_string(const char* str);
char serial_read_char(void);
bool serial_received(void);
#endif // _DRIVERS_SERIAL_H
+10
View File
@@ -0,0 +1,10 @@
#ifndef _DRIVERS_SPEAKER_H
#define _DRIVERS_SPEAKER_H
#include <stdint.h>
void speaker_on(uint32_t frequency);
void speaker_off(void);
void speaker_beep(uint32_t frequency, uint32_t duration_ms);
#endif // _DRIVERS_SPEAKER_H
+20
View File
@@ -0,0 +1,20 @@
#ifndef _DRIVERS_TTY_H
#define _DRIVERS_TTY_H
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#define MAX_TTYS 4
#define TTY_WIDTH 80
#define TTY_HEIGHT 25
void tty_init(void);
void tty_switch(int index);
int tty_get_current(void);
void tty_write(const char* data, size_t size);
void tty_write_string(const char* data);
void tty_clear(void);
void tty_set_cursor(int row, int col);
#endif // _DRIVERS_TTY_H
+48
View File
@@ -0,0 +1,48 @@
#ifndef _DRIVERS_VGA_H
#define _DRIVERS_VGA_H
#include <stdint.h>
#include <stddef.h>
#define VGA_WIDTH 80
#define VGA_HEIGHT 25
#define VGA_MEMORY 0xB8000
enum vga_color {
VGA_COLOR_BLACK = 0,
VGA_COLOR_BLUE = 1,
VGA_COLOR_GREEN = 2,
VGA_COLOR_CYAN = 3,
VGA_COLOR_RED = 4,
VGA_COLOR_MAGENTA = 5,
VGA_COLOR_BROWN = 6,
VGA_COLOR_LIGHT_GREY = 7,
VGA_COLOR_DARK_GREY = 8,
VGA_COLOR_LIGHT_BLUE = 9,
VGA_COLOR_LIGHT_GREEN = 10,
VGA_COLOR_LIGHT_CYAN = 11,
VGA_COLOR_LIGHT_RED = 12,
VGA_COLOR_LIGHT_MAGENTA = 13,
VGA_COLOR_LIGHT_BROWN = 14,
VGA_COLOR_WHITE = 15,
};
static inline uint8_t vga_entry_color(enum vga_color fg, enum vga_color bg) {
return fg | (bg << 4);
}
static inline uint16_t vga_entry(unsigned char uc, uint8_t color) {
return (uint16_t)uc | ((uint16_t)color << 8);
}
void vga_init(void);
void vga_clear(void);
void vga_putc(char c);
void vga_write(const char* str);
void vga_set_color(enum vga_color fg, enum vga_color bg);
void vga_set_cursor(int row, int col);
void vga_get_cursor(int* row, int* col);
void vga_enable_cursor(uint8_t cursor_start, uint8_t cursor_end);
void vga_disable_cursor(void);
#endif // _DRIVERS_VGA_H
+10
View File
@@ -0,0 +1,10 @@
#ifndef _FS_DEVFS_H
#define _FS_DEVFS_H
#include "vfs.h"
void devfs_init(void);
vfs_node_t* devfs_get_root(void);
int devfs_register_device(const char* name, vfs_node_t* dev_node);
#endif // _FS_DEVFS_H
+9
View File
@@ -0,0 +1,9 @@
#ifndef _FS_PROCFS_H
#define _FS_PROCFS_H
#include "vfs.h"
void procfs_init(void);
vfs_node_t* procfs_get_root(void);
#endif // _FS_PROCFS_H
+10
View File
@@ -0,0 +1,10 @@
#ifndef _FS_RAMFS_H
#define _FS_RAMFS_H
#include "vfs.h"
vfs_node_t* ramfs_create_root(void);
vfs_node_t* ramfs_add_file(vfs_node_t* parent, const char* name, const void* content, size_t size);
vfs_node_t* ramfs_add_dir(vfs_node_t* parent, const char* name);
#endif // _FS_RAMFS_H
+85
View File
@@ -0,0 +1,85 @@
#ifndef _FS_VFS_H
#define _FS_VFS_H
#include <kernel/types.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#define FS_FILE 0x01
#define FS_DIRECTORY 0x02
#define FS_CHARDEVICE 0x04
#define FS_BLOCKDEVICE 0x08
#define FS_PIPE 0x10
#define FS_SYMLINK 0x20
#define FS_MOUNTPOINT 0x40
#define O_RDONLY 0x0000
#define O_WRONLY 0x0001
#define O_RDWR 0x0002
#define O_CREAT 0x0040
#define O_TRUNC 0x0200
#define O_APPEND 0x0400
#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2
struct vfs_node;
typedef ssize_t (*vfs_read_fn_t)(struct vfs_node* node, off_t offset, size_t size, uint8_t* buffer);
typedef ssize_t (*vfs_write_fn_t)(struct vfs_node* node, off_t offset, size_t size, const uint8_t* buffer);
typedef int (*vfs_open_fn_t)(struct vfs_node* node, uint32_t flags);
typedef int (*vfs_close_fn_t)(struct vfs_node* node);
typedef struct vfs_dirent* (*vfs_readdir_fn_t)(struct vfs_node* node, uint32_t index);
typedef struct vfs_node* (*vfs_finddir_fn_t)(struct vfs_node* node, const char* name);
typedef int (*vfs_mkdir_fn_t)(struct vfs_node* node, const char* name, mode_t mode);
typedef int (*vfs_create_fn_t)(struct vfs_node* node, const char* name, mode_t mode);
typedef struct vfs_node {
char name[128];
uint32_t flags;
uint32_t mode;
uid_t uid;
gid_t gid;
size_t length;
ino_t inode;
vfs_read_fn_t read;
vfs_write_fn_t write;
vfs_open_fn_t open;
vfs_close_fn_t close;
vfs_readdir_fn_t readdir;
vfs_finddir_fn_t finddir;
vfs_mkdir_fn_t mkdir;
vfs_create_fn_t create;
struct vfs_node* ptr; // Used for mountpoints / device nodes
} vfs_node_t;
typedef struct vfs_dirent {
char name[128];
ino_t inode;
uint32_t type;
} vfs_dirent_t;
typedef struct {
vfs_node_t* node;
off_t offset;
uint32_t flags;
} file_descriptor_t;
void vfs_init(void);
vfs_node_t* vfs_namei(const char* path);
int vfs_open(const char* path, uint32_t flags);
ssize_t vfs_read(int fd, void* buffer, size_t size);
ssize_t vfs_write(int fd, const void* buffer, size_t size);
off_t vfs_lseek(int fd, off_t offset, int whence);
int vfs_close(int fd);
int vfs_mkdir(const char* path, mode_t mode);
int vfs_create(const char* path, mode_t mode);
vfs_node_t* vfs_get_root(void);
int vfs_mount(const char* path, vfs_node_t* fs_root);
#endif // _FS_VFS_H
+15
View File
@@ -0,0 +1,15 @@
#ifndef _KERNEL_MAIN_H
#define _KERNEL_MAIN_H
#include "types.h"
#include "printk.h"
#include <arch/x86_64/io.h>
#define KERNEL_NAME "SUB-OS"
#define KERNEL_VERSION "0.2.0-lts"
#define KERNEL_ARCH "x86_64"
#define KERNEL_AUTHOR "SUB-OS Development Team"
void kernel_main(void* memory_map, uint64_t memory_map_count);
#endif // _KERNEL_MAIN_H
+9
View File
@@ -0,0 +1,9 @@
#ifndef _KERNEL_PANIC_H
#define _KERNEL_PANIC_H
#include <arch/x86_64/isr.h>
void panic(const char* message);
void panic_with_regs(const char* message, const registers_t* regs);
#endif // _KERNEL_PANIC_H
+48
View File
@@ -0,0 +1,48 @@
#ifndef _KERNEL_PRINTK_H
#define _KERNEL_PRINTK_H
#include <stddef.h>
#include <stdint.h>
#include <stdarg.h>
// Linux-compatible log level prefixes
#define KERN_EMERG "\0010" /* system is unusable */
#define KERN_ALERT "\0011" /* action must be taken immediately */
#define KERN_CRIT "\0012" /* critical conditions */
#define KERN_ERR "\0013" /* error conditions */
#define KERN_WARNING "\0014" /* warning conditions */
#define KERN_NOTICE "\0015" /* normal but significant condition */
#define KERN_INFO "\0016" /* informational */
#define KERN_DEBUG "\0017" /* debug-level messages */
// ANSI Color Escape Sequences
#define ANSI_RESET "\033[0m"
#define ANSI_BOLD "\033[1m"
#define ANSI_BLACK "\033[30m"
#define ANSI_RED "\033[31m"
#define ANSI_GREEN "\033[32m"
#define ANSI_YELLOW "\033[33m"
#define ANSI_BLUE "\033[34m"
#define ANSI_MAGENTA "\033[35m"
#define ANSI_CYAN "\033[36m"
#define ANSI_WHITE "\033[37m"
#define ANSI_BRIGHT_BLACK "\033[90m"
#define ANSI_BRIGHT_RED "\033[91m"
#define ANSI_BRIGHT_GREEN "\033[92m"
#define ANSI_BRIGHT_YELLOW "\033[93m"
#define ANSI_BRIGHT_BLUE "\033[94m"
#define ANSI_BRIGHT_MAGENTA "\033[95m"
#define ANSI_BRIGHT_CYAN "\033[96m"
#define ANSI_BRIGHT_WHITE "\033[97m"
// Printk API
void printk_init(void);
int printk(const char* fmt, ...);
int vprintk(const char* fmt, va_list args);
int kprintf(const char* fmt, ...); // Alias for compatibility
// Kernel Dmesg Ring Buffer API
void dmesg_dump(void);
const char* dmesg_get_buffer(size_t* size_out);
#endif // _KERNEL_PRINTK_H
+13
View File
@@ -0,0 +1,13 @@
#ifndef _KERNEL_SCHED_H
#define _KERNEL_SCHED_H
#include "task.h"
void sched_init(void);
void sched_yield(void);
void sched_schedule(void);
void sched_tick(void);
void sched_add_task(task_t* task);
void sched_remove_task(task_t* task);
#endif // _KERNEL_SCHED_H
+36
View File
@@ -0,0 +1,36 @@
#ifndef _KERNEL_SYNC_H
#define _KERNEL_SYNC_H
#include <stdint.h>
#include <stdbool.h>
typedef struct {
volatile uint32_t lock;
} spinlock_t;
#define SPINLOCK_INIT { .lock = 0 }
static inline void spinlock_init(spinlock_t* lock) {
lock->lock = 0;
}
static inline void spin_lock(spinlock_t* lock) {
while (__atomic_test_and_set(&lock->lock, __ATOMIC_ACQUIRE)) {
__asm__ volatile ("pause");
}
}
static inline void spin_unlock(spinlock_t* lock) {
__atomic_clear(&lock->lock, __ATOMIC_RELEASE);
}
typedef struct {
spinlock_t lock;
volatile int count;
} semaphore_t;
void sem_init(semaphore_t* sem, int value);
void sem_wait(semaphore_t* sem);
void sem_post(semaphore_t* sem);
#endif // _KERNEL_SYNC_H
+36
View File
@@ -0,0 +1,36 @@
#ifndef _KERNEL_TASK_H
#define _KERNEL_TASK_H
#include "types.h"
#include <lib/list.h>
typedef enum {
TASK_STATE_READY,
TASK_STATE_RUNNING,
TASK_STATE_BLOCKED,
TASK_STATE_SLEEPING,
TASK_STATE_ZOMBIE,
TASK_STATE_DEAD
} task_state_t;
typedef struct task_struct {
pid_t pid;
char name[32];
task_state_t state;
uint64_t rsp;
uint64_t stack_base;
size_t stack_size;
uint64_t sleep_until_ticks;
int priority;
int time_slice;
struct list_head list;
} task_t;
typedef void (*task_entry_fn_t)(void* arg);
task_t* task_create(const char* name, task_entry_fn_t entry, void* arg, int priority);
void task_exit(int exit_code);
task_t* task_current(void);
pid_t task_get_pid(void);
#endif // _KERNEL_TASK_H
+30
View File
@@ -0,0 +1,30 @@
#ifndef _KERNEL_TYPES_H
#define _KERNEL_TYPES_H
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
typedef int32_t pid_t;
typedef int32_t tid_t;
typedef uint32_t uid_t;
typedef uint32_t gid_t;
typedef int64_t off_t;
typedef int64_t ssize_t;
typedef uint32_t mode_t;
typedef uint32_t dev_t;
typedef uint64_t ino_t;
typedef uint64_t time_t;
#ifndef NULL
#define NULL ((void*)0)
#endif
#define ALIGN_UP(addr, align) (((addr) + (align) - 1) & ~((align) - 1))
#define ALIGN_DOWN(addr, align) ((addr) & ~((align) - 1))
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#endif // _KERNEL_TYPES_H
+26
View File
@@ -0,0 +1,26 @@
#ifndef _LIB_BITMAP_H
#define _LIB_BITMAP_H
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
typedef uint64_t bitmap_word_t;
#define BITS_PER_WORD (sizeof(bitmap_word_t) * 8)
static inline void bitmap_set(bitmap_word_t* map, size_t bit) {
map[bit / BITS_PER_WORD] |= ((bitmap_word_t)1 << (bit % BITS_PER_WORD));
}
static inline void bitmap_clear(bitmap_word_t* map, size_t bit) {
map[bit / BITS_PER_WORD] &= ~((bitmap_word_t)1 << (bit % BITS_PER_WORD));
}
static inline bool bitmap_test(const bitmap_word_t* map, size_t bit) {
return (map[bit / BITS_PER_WORD] & ((bitmap_word_t)1 << (bit % BITS_PER_WORD))) != 0;
}
int64_t bitmap_find_first_free(const bitmap_word_t* map, size_t total_bits);
int64_t bitmap_find_contiguous_free(const bitmap_word_t* map, size_t total_bits, size_t count);
#endif // _LIB_BITMAP_H
+56
View File
@@ -0,0 +1,56 @@
#ifndef _LIB_LIST_H
#define _LIB_LIST_H
#include <stddef.h>
struct list_head {
struct list_head *next, *prev;
};
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name)
static inline void INIT_LIST_HEAD(struct list_head *list) {
list->next = list;
list->prev = list;
}
static inline void __list_add(struct list_head *new_node,
struct list_head *prev,
struct list_head *next) {
next->prev = new_node;
new_node->next = next;
new_node->prev = prev;
prev->next = new_node;
}
static inline void list_add(struct list_head *new_node, struct list_head *head) {
__list_add(new_node, head, head->next);
}
static inline void list_add_tail(struct list_head *new_node, struct list_head *head) {
__list_add(new_node, head->prev, head);
}
static inline void list_del(struct list_head *entry) {
entry->next->prev = entry->prev;
entry->prev->next = entry->next;
entry->next = NULL;
entry->prev = NULL;
}
static inline int list_empty(const struct list_head *head) {
return head->next == head;
}
#define list_entry(ptr, type, member) \
((type *)((char *)(ptr) - offsetof(type, member)))
#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)
#define list_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next)
#endif // _LIB_LIST_H
+12
View File
@@ -0,0 +1,12 @@
#ifndef _LIB_PRINTF_H
#define _LIB_PRINTF_H
#include <stddef.h>
#include <stdarg.h>
int sprintf(char* buf, const char* fmt, ...);
int snprintf(char* buf, size_t size, const char* fmt, ...);
int vsprintf(char* buf, const char* fmt, va_list args);
int vsnprintf(char* buf, size_t size, const char* fmt, va_list args);
#endif // _LIB_PRINTF_H
+33
View File
@@ -0,0 +1,33 @@
#ifndef _LIB_STRING_H
#define _LIB_STRING_H
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
// Memory functions
void* memset(void* dest, int val, size_t count);
void* memcpy(void* dest, const void* src, size_t count);
void* memmove(void* dest, const void* src, size_t count);
int memcmp(const void* ptr1, const void* ptr2, size_t num);
// String functions
size_t strlen(const char* str);
int strcmp(const char* str1, const char* str2);
int strncmp(const char* str1, const char* str2, size_t num);
char* strcpy(char* dest, const char* src);
char* strncpy(char* dest, const char* src, size_t num);
char* strcat(char* dest, const char* src);
char* strncat(char* dest, const char* src, size_t num);
char* strchr(const char* str, int c);
char* strrchr(const char* str, int c);
char* strstr(const char* haystack, const char* needle);
char* strdup(const char* s);
// Conversion functions
char* itoa(int value, char* str, int base);
char* itoa_hex(uint64_t value, char* str);
int atoi(const char* str);
long strtol(const char* str, char** endptr, int base);
#endif // _LIB_STRING_H
+18
View File
@@ -0,0 +1,18 @@
#ifndef _MM_KMALLOC_H
#define _MM_KMALLOC_H
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
void heap_init(void);
void* kmalloc(size_t size);
void* kzalloc(size_t size);
void* krealloc(void* ptr, size_t new_size);
void kfree(void* ptr);
size_t heap_get_used_bytes(void);
size_t heap_get_free_bytes(void);
size_t heap_get_total_bytes(void);
#endif // _MM_KMALLOC_H
+36
View File
@@ -0,0 +1,36 @@
#ifndef _MM_PMM_H
#define _MM_PMM_H
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#define PMM_PAGE_SIZE 4096
// E820 Memory Map entry structure
typedef struct {
uint64_t base;
uint64_t length;
uint32_t type;
uint32_t acpi_attr;
} __attribute__((packed)) e820_entry_t;
#define E820_TYPE_USABLE 1
#define E820_TYPE_RESERVED 2
#define E820_TYPE_ACPI_RECLAIM 3
#define E820_TYPE_ACPI_NVS 4
#define E820_TYPE_BAD 5
void pmm_init(void* memory_map, uint64_t memory_map_count);
void* pmm_alloc_page(void);
void* pmm_alloc_pages(size_t count);
void pmm_free_page(void* page);
void pmm_free_pages(void* page, size_t count);
uint64_t pmm_get_total_pages(void);
uint64_t pmm_get_used_pages(void);
uint64_t pmm_get_free_pages(void);
uint64_t pmm_get_total_memory(void);
uint64_t pmm_get_usable_memory(void);
#endif // _MM_PMM_H
+107
View File
@@ -0,0 +1,107 @@
#ifndef _NET_NET_H
#define _NET_NET_H
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#define ETHERTYPE_IPV4 0x0800
#define ETHERTYPE_ARP 0x0806
#define IP_PROTO_ICMP 1
#define IP_PROTO_TCP 6
#define IP_PROTO_UDP 17
static inline uint16_t htons(uint16_t host) {
return (uint16_t)((host << 8) | (host >> 8));
}
static inline uint16_t ntohs(uint16_t net) {
return htons(net);
}
static inline uint32_t htonl(uint32_t host) {
return (uint32_t)(((host & 0x000000FF) << 24) |
((host & 0x0000FF00) << 8) |
((host & 0x00FF0000) >> 8) |
((host & 0xFF000000) >> 24));
}
static inline uint32_t ntohl(uint32_t net) {
return htonl(net);
}
typedef struct {
uint8_t dst_mac[6];
uint8_t src_mac[6];
uint16_t ethertype;
} __attribute__((packed)) eth_header_t;
typedef struct {
uint16_t htype;
uint16_t ptype;
uint8_t hlen;
uint8_t plen;
uint16_t opcode;
uint8_t sender_mac[6];
uint32_t sender_ip;
uint8_t target_mac[6];
uint32_t target_ip;
} __attribute__((packed)) arp_header_t;
typedef struct {
uint8_t ihl_version;
uint8_t tos;
uint16_t total_length;
uint16_t id;
uint16_t flags_fragment;
uint8_t ttl;
uint8_t protocol;
uint16_t checksum;
uint32_t src_ip;
uint32_t dst_ip;
} __attribute__((packed)) ip_header_t;
typedef struct {
uint8_t type;
uint8_t code;
uint16_t checksum;
uint16_t id;
uint16_t sequence;
} __attribute__((packed)) icmp_header_t;
typedef struct {
uint16_t src_port;
uint16_t dst_port;
uint16_t length;
uint16_t checksum;
} __attribute__((packed)) udp_header_t;
typedef struct {
char name[16];
uint8_t mac[6];
uint32_t ip;
uint32_t subnet;
uint32_t gateway;
uint32_t dns;
bool is_up;
} net_if_t;
typedef struct {
uint32_t ip;
uint8_t mac[6];
bool valid;
} arp_entry_t;
void net_init(void);
void net_receive(const uint8_t* packet, uint16_t length);
void net_send_eth(const uint8_t* dst_mac, uint16_t ethertype, const void* payload, uint16_t len);
void net_send_ip(uint32_t dst_ip, uint8_t protocol, const void* payload, uint16_t len);
void net_send_arp_req(uint32_t target_ip);
int net_ping(uint32_t target_ip, uint32_t count, uint32_t timeout_ms);
net_if_t* net_get_primary_if(void);
arp_entry_t* net_get_arp_table(int* count_out);
uint32_t ip_parse(const char* ip_str);
void ip_to_str(uint32_t ip, char* buf);
void mac_to_str(const uint8_t* mac, char* buf);
#endif // _NET_NET_H
+26
View File
@@ -0,0 +1,26 @@
#ifndef _USERLAND_LAZYBOX_H
#define _USERLAND_LAZYBOX_H
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#define LAZYBOX_VERSION "2.0.0-pro"
typedef int (*applet_fn_t)(int argc, char** argv);
typedef struct {
const char* name;
applet_fn_t func;
const char* usage;
const char* desc;
const char* category;
} lazybox_applet_t;
void lazybox_init(void);
int lazybox_main(int argc, char** argv);
bool lazybox_has_applet(const char* name);
int lazybox_run_applet(const char* name, int argc, char** argv);
const lazybox_applet_t* lazybox_get_applets(int* count_out);
#endif // _USERLAND_LAZYBOX_H
+9
View File
@@ -0,0 +1,9 @@
#ifndef _USERLAND_SHELL_H
#define _USERLAND_SHELL_H
#define MAX_COMMAND_LENGTH 256
void shell_init(void);
void shell_run(void);
#endif // _USERLAND_SHELL_H