--- /dev/null
+#ifndef ARCH_X86_IDT_H
+#define ARCH_X86_IDT_H
+
+#include <stdint.h>
+
+/* 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
--- /dev/null
+#ifndef ARCH_X86_IO_H
+#define ARCH_X86_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 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
--- /dev/null
+#ifndef ARCH_X86_MULTIBOOT2_H
+#define ARCH_X86_MULTIBOOT2_H
+
+#include <stdint.h>
+
+/* 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
#include <stdint.h>
-/* 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
#include <stdint.h>
-/* 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;
}
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
#include <stdint.h>
-/* 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 */