From: Tulio A M Mendes Date: Thu, 5 Feb 2026 20:47:39 +0000 (-0300) Subject: include: move x86-specific headers under include/arch/x86 and add shims X-Git-Url: https://projects.tadryanom.me/sitemap.xml?a=commitdiff_plain;h=bd34cd27e2f0790c1e11a62ec03f3e645a6b31a2;p=AdrOS.git include: move x86-specific headers under include/arch/x86 and add shims --- diff --git a/include/arch/x86/idt.h b/include/arch/x86/idt.h new file mode 100644 index 0000000..3319dee --- /dev/null +++ b/include/arch/x86/idt.h @@ -0,0 +1,36 @@ +#ifndef ARCH_X86_IDT_H +#define ARCH_X86_IDT_H + +#include + +/* IDT Entry (Gate Descriptor) */ +struct idt_entry { + uint16_t base_lo; // Lower 16 bits of handler address + uint16_t sel; // Kernel segment selector + uint8_t always0; // This must always be zero + uint8_t flags; // Type and attributes + uint16_t base_hi; // Upper 16 bits of handler address +} __attribute__((packed)); + +/* IDT Pointer (Loaded into IDTR) */ +struct idt_ptr { + uint16_t limit; + uint32_t base; +} __attribute__((packed)); + +/* Registers saved by our assembly ISR stub */ +struct registers { + uint32_t ds; // Data segment selector + uint32_t edi, esi, ebp, esp, ebx, edx, ecx, eax; // Pushed by pusha + uint32_t int_no, err_code; // Interrupt number and error code + uint32_t eip, cs, eflags, useresp, ss; // Pushed by the processor automatically +}; + +// Initialize IDT and PIC +void idt_init(void); + +// Register a custom handler for a specific interrupt +typedef void (*isr_handler_t)(struct registers*); +void register_interrupt_handler(uint8_t n, isr_handler_t handler); + +#endif diff --git a/include/arch/x86/io.h b/include/arch/x86/io.h new file mode 100644 index 0000000..2e89507 --- /dev/null +++ b/include/arch/x86/io.h @@ -0,0 +1,36 @@ +#ifndef ARCH_X86_IO_H +#define ARCH_X86_IO_H + +#include + +static inline void outb(uint16_t port, uint8_t val) { + __asm__ volatile ("outb %0, %1" : : "a"(val), "Nd"(port)); +} + +static inline void outw(uint16_t port, uint16_t val) { + __asm__ volatile ("outw %0, %1" : : "a"(val), "Nd"(port)); +} + +static inline void outl(uint16_t port, uint32_t val) { + __asm__ volatile ("outl %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 uint16_t inw(uint16_t port) { + uint16_t ret; + __asm__ volatile ("inw %1, %0" : "=a"(ret) : "Nd"(port)); + return ret; +} + +static inline uint32_t inl(uint16_t port) { + uint32_t ret; + __asm__ volatile ("inl %1, %0" : "=a"(ret) : "Nd"(port)); + return ret; +} + +#endif diff --git a/include/arch/x86/multiboot2.h b/include/arch/x86/multiboot2.h new file mode 100644 index 0000000..c412097 --- /dev/null +++ b/include/arch/x86/multiboot2.h @@ -0,0 +1,80 @@ +#ifndef ARCH_X86_MULTIBOOT2_H +#define ARCH_X86_MULTIBOOT2_H + +#include + +/* How many bytes from the start of the file we search for the header. */ +#define MULTIBOOT_SEARCH 32768 +#define MULTIBOOT_HEADER_ALIGN 8 + +/* The magic field should contain this. */ +#define MULTIBOOT2_HEADER_MAGIC 0xe85250d6 + +/* This should be in %eax. */ +#define MULTIBOOT2_BOOTLOADER_MAGIC 0x36d76289 + +/* Alignment of multiboot modules. */ +#define MULTIBOOT_MOD_ALIGN 0x00001000 + +/* Alignment of the multiboot info structure. */ +#define MULTIBOOT_INFO_ALIGN 0x00000008 + +/* Tags found in the Multiboot2 information structure */ +#define MULTIBOOT_TAG_TYPE_END 0 +#define MULTIBOOT_TAG_TYPE_CMDLINE 1 +#define MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME 2 +#define MULTIBOOT_TAG_TYPE_MODULE 3 +#define MULTIBOOT_TAG_TYPE_BASIC_MEMINFO 4 +#define MULTIBOOT_TAG_TYPE_BOOTDEV 5 +#define MULTIBOOT_TAG_TYPE_MMAP 6 +#define MULTIBOOT_TAG_TYPE_VBE 7 +#define MULTIBOOT_TAG_TYPE_FRAMEBUFFER 8 + +struct multiboot_tag { + uint32_t type; + uint32_t size; +}; + +struct multiboot_tag_string { + uint32_t type; + uint32_t size; + char string[0]; +}; + +struct multiboot_tag_basic_meminfo { + uint32_t type; + uint32_t size; + uint32_t mem_lower; + uint32_t mem_upper; +}; + +struct multiboot_tag_mmap { + uint32_t type; + uint32_t size; + uint32_t entry_size; + uint32_t entry_version; + struct multiboot_mmap_entry entries[0]; +}; + +struct multiboot_tag_module { + uint32_t type; + uint32_t size; + uint32_t mod_start; + uint32_t mod_end; + char string[0]; +}; + +struct multiboot_mmap_entry { + uint64_t addr; + uint64_t len; + uint32_t type; + uint32_t zero; +}; + +#define MULTIBOOT_MEMORY_AVAILABLE 1 +#define MULTIBOOT_MEMORY_RESERVED 2 +#define MULTIBOOT_MEMORY_ACPI_RECLAIMABLE 3 +#define MULTIBOOT_MEMORY_NVS 4 +#define MULTIBOOT_MEMORY_BADRAM 5 + +#endif diff --git a/include/idt.h b/include/idt.h index 3ae501d..2897e40 100644 --- a/include/idt.h +++ b/include/idt.h @@ -3,34 +3,25 @@ #include -/* IDT Entry (Gate Descriptor) */ -struct idt_entry { - uint16_t base_lo; // Lower 16 bits of handler address - uint16_t sel; // Kernel segment selector - uint8_t always0; // This must always be zero - uint8_t flags; // Type and attributes - uint16_t base_hi; // Upper 16 bits of handler address -} __attribute__((packed)); +#if defined(__i386__) || defined(__x86_64__) +#include "arch/x86/idt.h" +#else -/* IDT Pointer (Loaded into IDTR) */ -struct idt_ptr { - uint16_t limit; - uint32_t base; -} __attribute__((packed)); - -/* Registers saved by our assembly ISR stub */ +/* Non-x86: provide a minimal compatibility surface. + Interrupt controller specifics live in arch code. */ struct registers { - uint32_t ds; // Data segment selector - uint32_t edi, esi, ebp, esp, ebx, edx, ecx, eax; // Pushed by pusha - uint32_t int_no, err_code; // Interrupt number and error code - uint32_t eip, cs, eflags, useresp, ss; // Pushed by the processor automatically + uint32_t int_no; + uint32_t err_code; }; -// Initialize IDT and PIC -void idt_init(void); - -// Register a custom handler for a specific interrupt typedef void (*isr_handler_t)(struct registers*); -void register_interrupt_handler(uint8_t n, isr_handler_t handler); + +static inline void idt_init(void) { } +static inline void register_interrupt_handler(uint8_t n, isr_handler_t handler) { + (void)n; + (void)handler; +} + +#endif #endif diff --git a/include/io.h b/include/io.h index f44840e..b059bb8 100644 --- a/include/io.h +++ b/include/io.h @@ -3,34 +3,19 @@ #include -/* x86 I/O Port Wrappers */ -#if defined(__i386__) || defined(__x86_64__) - -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; -} - +/* Generic MMIO helpers (ARM/RISC-V/MIPS and also usable on x86) */ static inline void mmio_write8(uintptr_t addr, uint8_t val) { volatile uint8_t* ptr = (uint8_t*)addr; *ptr = val; } -static inline uint8_t mmio_read8(uintptr_t addr) { - volatile uint8_t* ptr = (uint8_t*)addr; - return *ptr; +static inline void mmio_write16(uintptr_t addr, uint16_t val) { + volatile uint16_t* ptr = (uint16_t*)addr; + *ptr = val; } -#else - -/* MMIO for ARM/RISC-V */ -static inline void mmio_write8(uintptr_t addr, uint8_t val) { - volatile uint8_t* ptr = (uint8_t*)addr; +static inline void mmio_write32(uintptr_t addr, uint32_t val) { + volatile uint32_t* ptr = (uint32_t*)addr; *ptr = val; } @@ -39,15 +24,19 @@ static inline uint8_t mmio_read8(uintptr_t addr) { return *ptr; } -/* Fallback for port I/O on architectures that don't have it (mapped to MMIO or no-op) */ -static inline void outb(uint16_t port, uint8_t val) { - (void)port; (void)val; // No-op +static inline uint16_t mmio_read16(uintptr_t addr) { + volatile uint16_t* ptr = (uint16_t*)addr; + return *ptr; } -static inline uint8_t inb(uint16_t port) { - (void)port; return 0; +static inline uint32_t mmio_read32(uintptr_t addr) { + volatile uint32_t* ptr = (uint32_t*)addr; + return *ptr; } +/* x86 port I/O lives under include/arch/x86/io.h */ +#if defined(__i386__) || defined(__x86_64__) +#include "arch/x86/io.h" #endif #endif diff --git a/include/multiboot2.h b/include/multiboot2.h index c28535e..4162c40 100644 --- a/include/multiboot2.h +++ b/include/multiboot2.h @@ -3,78 +3,7 @@ #include -/* How many bytes from the start of the file we search for the header. */ -#define MULTIBOOT_SEARCH 32768 -#define MULTIBOOT_HEADER_ALIGN 8 - -/* The magic field should contain this. */ -#define MULTIBOOT2_HEADER_MAGIC 0xe85250d6 - -/* This should be in %eax. */ -#define MULTIBOOT2_BOOTLOADER_MAGIC 0x36d76289 - -/* Alignment of multiboot modules. */ -#define MULTIBOOT_MOD_ALIGN 0x00001000 - -/* Alignment of the multiboot info structure. */ -#define MULTIBOOT_INFO_ALIGN 0x00000008 - -/* Tags found in the Multiboot2 information structure */ -#define MULTIBOOT_TAG_TYPE_END 0 -#define MULTIBOOT_TAG_TYPE_CMDLINE 1 -#define MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME 2 -#define MULTIBOOT_TAG_TYPE_MODULE 3 -#define MULTIBOOT_TAG_TYPE_BASIC_MEMINFO 4 -#define MULTIBOOT_TAG_TYPE_BOOTDEV 5 -#define MULTIBOOT_TAG_TYPE_MMAP 6 -#define MULTIBOOT_TAG_TYPE_VBE 7 -#define MULTIBOOT_TAG_TYPE_FRAMEBUFFER 8 - -struct multiboot_tag { - uint32_t type; - uint32_t size; -}; - -struct multiboot_tag_string { - uint32_t type; - uint32_t size; - char string[0]; -}; - -struct multiboot_tag_basic_meminfo { - uint32_t type; - uint32_t size; - uint32_t mem_lower; - uint32_t mem_upper; -}; - -struct multiboot_tag_mmap { - uint32_t type; - uint32_t size; - uint32_t entry_size; - uint32_t entry_version; - struct multiboot_mmap_entry entries[0]; -}; - -struct multiboot_tag_module { - uint32_t type; - uint32_t size; - uint32_t mod_start; - uint32_t mod_end; - char string[0]; -}; - -struct multiboot_mmap_entry { - uint64_t addr; - uint64_t len; - uint32_t type; - uint32_t zero; -}; - -#define MULTIBOOT_MEMORY_AVAILABLE 1 -#define MULTIBOOT_MEMORY_RESERVED 2 -#define MULTIBOOT_MEMORY_ACPI_RECLAIMABLE 3 -#define MULTIBOOT_MEMORY_NVS 4 -#define MULTIBOOT_MEMORY_BADRAM 5 - +#if defined(__i386__) || defined(__x86_64__) +#include "arch/x86/multiboot2.h" +#endif #endif /* MULTIBOOT2_H */