From: Tulio A M Mendes Date: Sun, 15 Feb 2026 04:38:44 +0000 (-0300) Subject: refactor: move kernel_va_map.h to include/arch/x86/, clean virtio_blk.c port I/O X-Git-Url: https://projects.tadryanom.me/docs/static/gitweb.css?a=commitdiff_plain;h=914998cf5238a6405271478e815a4db89e55a2ec;p=AdrOS.git refactor: move kernel_va_map.h to include/arch/x86/, clean virtio_blk.c port I/O - kernel_va_map.h: moved from include/ to include/arch/x86/ since it contains x86-specific VA layout (IOAPIC, LAPIC, ATA DMA, E1000) - Updated all 8 include sites to use new path - virtio_blk.c: removed duplicated port I/O inline asm, now uses io.h → arch/x86/io.h (outb/inb/outw/inw/outl/inl) - Renamed outb_port/inb_port to standard outb/inb Deep search results — agnostic areas verified clean: - src/kernel/: no arch-specific code - src/mm/: no arch-specific code - src/drivers/: no arch-specific code (after virtio_blk fix) - src/net/: no arch-specific code - include/ (excl arch/): only dispatcher-pattern #includes remain (io.h, interrupts.h, arch_types.h, arch_syscall.h, spinlock.h) --- diff --git a/include/arch/x86/kernel_va_map.h b/include/arch/x86/kernel_va_map.h new file mode 100644 index 0000000..b5c3505 --- /dev/null +++ b/include/arch/x86/kernel_va_map.h @@ -0,0 +1,64 @@ +#ifndef KERNEL_VA_MAP_H +#define KERNEL_VA_MAP_H + +/* + * Centralized kernel virtual address map for MMIO / DMA / special regions. + * + * All fixed-VA allocations MUST be listed here to prevent collisions. + * The kernel image is loaded at KERNEL_VIRT_BASE (0xC0000000) and BSS + * can extend past 0xC0200000 with large static pools (lwIP, FAT, etc.). + * + * Layout (sorted by VA): + * + * 0xC0000000 .. ~0xC0203000 Kernel .text/.data/.bss (variable) + * 0xC0201000 IOAPIC MMIO (1 page) + * 0xC0280000 vDSO shared page (1 page) + * 0xC0300000 .. 0xC030FFFF ACPI temp window (16 pages) + * 0xC0320000 ATA DMA PRDT (1 page) + * 0xC0321000 ATA DMA bounce buffer (1 page) + * 0xC0330000 .. 0xC034FFFF E1000 MMIO (32 pages, 128 KB) + * 0xC0350000 E1000 TX descriptor ring (1 page) + * 0xC0351000 E1000 RX descriptor ring (1 page) + * 0xC0352000 .. 0xC0361FFF E1000 TX buffers (16 pages) + * 0xC0362000 .. 0xC0371FFF E1000 RX buffers (16 pages) + * 0xC0400000 LAPIC MMIO (1 page) + * 0xC8000000 .. Kernel stacks (guard + 8KB per thread) + * 0xD0000000 .. Kernel heap (10 MB) + * 0xDC000000 .. Initrd / generic phys mapping (up to 64 MB) + * 0xE0000000 .. Framebuffer mapping (up to 16 MB) + */ + +/* IOAPIC (arch/x86/ioapic.c) */ +#define KVA_IOAPIC 0xC0201000U + +/* vDSO shared page (kernel/vdso.c) */ +#define KVA_VDSO 0xC0280000U + +/* ACPI temp mapping window — 16 pages (arch/x86/acpi.c) */ +#define KVA_ACPI_TMP_BASE 0xC0300000U +#define KVA_ACPI_TMP_PAGES 16 + +/* ATA DMA (hal/x86/ata_dma.c) — two pages per channel (PRDT + bounce buf) */ +#define KVA_ATA_DMA_PRDT_PRI 0xC0320000U +#define KVA_ATA_DMA_BUF_PRI 0xC0321000U +#define KVA_ATA_DMA_PRDT_SEC 0xC0322000U +#define KVA_ATA_DMA_BUF_SEC 0xC0323000U + +/* E1000 NIC (drivers/e1000.c) */ +#define KVA_E1000_MMIO 0xC0330000U +#define KVA_E1000_MMIO_PAGES 32 +#define KVA_E1000_TX_DESC 0xC0350000U +#define KVA_E1000_RX_DESC 0xC0351000U +#define KVA_E1000_TX_BUF 0xC0352000U +#define KVA_E1000_RX_BUF 0xC0362000U + +/* LAPIC (arch/x86/lapic.c) */ +#define KVA_LAPIC 0xC0400000U + +/* Initrd / generic physical range mapping (hal/x86/mm.c) */ +#define KVA_PHYS_MAP 0xDC000000U + +/* Framebuffer (drivers/vbe.c) — up to 16 MB for large resolutions */ +#define KVA_FRAMEBUFFER 0xE0000000U + +#endif diff --git a/include/kernel_va_map.h b/include/kernel_va_map.h deleted file mode 100644 index b5c3505..0000000 --- a/include/kernel_va_map.h +++ /dev/null @@ -1,64 +0,0 @@ -#ifndef KERNEL_VA_MAP_H -#define KERNEL_VA_MAP_H - -/* - * Centralized kernel virtual address map for MMIO / DMA / special regions. - * - * All fixed-VA allocations MUST be listed here to prevent collisions. - * The kernel image is loaded at KERNEL_VIRT_BASE (0xC0000000) and BSS - * can extend past 0xC0200000 with large static pools (lwIP, FAT, etc.). - * - * Layout (sorted by VA): - * - * 0xC0000000 .. ~0xC0203000 Kernel .text/.data/.bss (variable) - * 0xC0201000 IOAPIC MMIO (1 page) - * 0xC0280000 vDSO shared page (1 page) - * 0xC0300000 .. 0xC030FFFF ACPI temp window (16 pages) - * 0xC0320000 ATA DMA PRDT (1 page) - * 0xC0321000 ATA DMA bounce buffer (1 page) - * 0xC0330000 .. 0xC034FFFF E1000 MMIO (32 pages, 128 KB) - * 0xC0350000 E1000 TX descriptor ring (1 page) - * 0xC0351000 E1000 RX descriptor ring (1 page) - * 0xC0352000 .. 0xC0361FFF E1000 TX buffers (16 pages) - * 0xC0362000 .. 0xC0371FFF E1000 RX buffers (16 pages) - * 0xC0400000 LAPIC MMIO (1 page) - * 0xC8000000 .. Kernel stacks (guard + 8KB per thread) - * 0xD0000000 .. Kernel heap (10 MB) - * 0xDC000000 .. Initrd / generic phys mapping (up to 64 MB) - * 0xE0000000 .. Framebuffer mapping (up to 16 MB) - */ - -/* IOAPIC (arch/x86/ioapic.c) */ -#define KVA_IOAPIC 0xC0201000U - -/* vDSO shared page (kernel/vdso.c) */ -#define KVA_VDSO 0xC0280000U - -/* ACPI temp mapping window — 16 pages (arch/x86/acpi.c) */ -#define KVA_ACPI_TMP_BASE 0xC0300000U -#define KVA_ACPI_TMP_PAGES 16 - -/* ATA DMA (hal/x86/ata_dma.c) — two pages per channel (PRDT + bounce buf) */ -#define KVA_ATA_DMA_PRDT_PRI 0xC0320000U -#define KVA_ATA_DMA_BUF_PRI 0xC0321000U -#define KVA_ATA_DMA_PRDT_SEC 0xC0322000U -#define KVA_ATA_DMA_BUF_SEC 0xC0323000U - -/* E1000 NIC (drivers/e1000.c) */ -#define KVA_E1000_MMIO 0xC0330000U -#define KVA_E1000_MMIO_PAGES 32 -#define KVA_E1000_TX_DESC 0xC0350000U -#define KVA_E1000_RX_DESC 0xC0351000U -#define KVA_E1000_TX_BUF 0xC0352000U -#define KVA_E1000_RX_BUF 0xC0362000U - -/* LAPIC (arch/x86/lapic.c) */ -#define KVA_LAPIC 0xC0400000U - -/* Initrd / generic physical range mapping (hal/x86/mm.c) */ -#define KVA_PHYS_MAP 0xDC000000U - -/* Framebuffer (drivers/vbe.c) — up to 16 MB for large resolutions */ -#define KVA_FRAMEBUFFER 0xE0000000U - -#endif diff --git a/src/arch/x86/acpi.c b/src/arch/x86/acpi.c index eef6da6..2762f90 100644 --- a/src/arch/x86/acpi.c +++ b/src/arch/x86/acpi.c @@ -12,7 +12,7 @@ static int g_acpi_valid = 0; /* The first 16MB is identity-mapped during early boot (boot.S maps 0-16MB). * For addresses < 16MB we can use phys + 0xC0000000. * For addresses >= 16MB we must temporarily map them via VMM. */ -#include "kernel_va_map.h" +#include "arch/x86/kernel_va_map.h" #define KERNEL_VIRT_BASE 0xC0000000U #define IDENTITY_LIMIT 0x01000000U /* 16MB */ diff --git a/src/arch/x86/ioapic.c b/src/arch/x86/ioapic.c index 5f8173b..5db30fb 100644 --- a/src/arch/x86/ioapic.c +++ b/src/arch/x86/ioapic.c @@ -1,6 +1,6 @@ #include "arch/x86/ioapic.h" #include "arch/x86/lapic.h" -#include "kernel_va_map.h" +#include "arch/x86/kernel_va_map.h" #include "vmm.h" #include "console.h" #include "utils.h" diff --git a/src/arch/x86/lapic.c b/src/arch/x86/lapic.c index 2d6a0d8..c26e134 100644 --- a/src/arch/x86/lapic.c +++ b/src/arch/x86/lapic.c @@ -1,5 +1,5 @@ #include "arch/x86/lapic.h" -#include "kernel_va_map.h" +#include "arch/x86/kernel_va_map.h" #include "hal/cpu_features.h" #include "vmm.h" #include "io.h" diff --git a/src/drivers/e1000.c b/src/drivers/e1000.c index 2665771..9e1f65d 100644 --- a/src/drivers/e1000.c +++ b/src/drivers/e1000.c @@ -1,6 +1,6 @@ #include "e1000.h" #include "hal/driver.h" -#include "kernel_va_map.h" +#include "arch/x86/kernel_va_map.h" #include "pci.h" #include "vmm.h" #include "pmm.h" diff --git a/src/drivers/vbe.c b/src/drivers/vbe.c index 55cace1..adf9985 100644 --- a/src/drivers/vbe.c +++ b/src/drivers/vbe.c @@ -6,7 +6,7 @@ #include "uaccess.h" #include "console.h" #include "utils.h" -#include "kernel_va_map.h" +#include "arch/x86/kernel_va_map.h" #include diff --git a/src/drivers/virtio_blk.c b/src/drivers/virtio_blk.c index fd15c98..f96233a 100644 --- a/src/drivers/virtio_blk.c +++ b/src/drivers/virtio_blk.c @@ -18,6 +18,7 @@ #include "vmm.h" #include "interrupts.h" #include "hal/driver.h" +#include "io.h" #include @@ -99,32 +100,6 @@ static uint8_t vblk_status_byte __attribute__((aligned(4))); static spinlock_t vblk_lock = {0}; -/* ---- Port I/O helpers ---- */ -static inline void outl(uint16_t port, uint32_t val) { - __asm__ volatile("outl %0, %w1" :: "a"(val), "Nd"(port)); -} -static inline uint32_t inl(uint16_t port) { - uint32_t val; - __asm__ volatile("inl %w1, %0" : "=a"(val) : "Nd"(port)); - return val; -} -static inline void outw(uint16_t port, uint16_t val) { - __asm__ volatile("outw %0, %w1" :: "a"(val), "Nd"(port)); -} -static inline uint16_t inw(uint16_t port) { - uint16_t val; - __asm__ volatile("inw %w1, %0" : "=a"(val) : "Nd"(port)); - return val; -} -static inline void outb_port(uint16_t port, uint8_t val) { - __asm__ volatile("outb %0, %w1" :: "a"(val), "Nd"(port)); -} -static inline uint8_t inb_port(uint16_t port) { - uint8_t val; - __asm__ volatile("inb %w1, %0" : "=a"(val) : "Nd"(port)); - return val; -} - /* ---- Vring size calculation (legacy) ---- */ static uint32_t vring_size(uint32_t num) { /* desc table + avail ring (aligned to page) + used ring */ @@ -156,11 +131,11 @@ int virtio_blk_init(void) { pci_config_write(dev->bus, dev->slot, dev->func, 0x04, cmd); /* Reset device */ - outb_port(vblk_iobase + VIRTIO_PCI_STATUS, 0); + outb(vblk_iobase + VIRTIO_PCI_STATUS, 0); /* Acknowledge */ - outb_port(vblk_iobase + VIRTIO_PCI_STATUS, VIRTIO_STATUS_ACK); - outb_port(vblk_iobase + VIRTIO_PCI_STATUS, + outb(vblk_iobase + VIRTIO_PCI_STATUS, VIRTIO_STATUS_ACK); + outb(vblk_iobase + VIRTIO_PCI_STATUS, VIRTIO_STATUS_ACK | VIRTIO_STATUS_DRIVER); /* Read host features, accept none for simplicity */ @@ -177,7 +152,7 @@ int virtio_blk_init(void) { vblk_queue_size = inw(vblk_iobase + VIRTIO_PCI_QUEUE_SIZE); if (vblk_queue_size == 0) { kprintf("[VIRTIO-BLK] Queue size is 0.\n"); - outb_port(vblk_iobase + VIRTIO_PCI_STATUS, VIRTIO_STATUS_FAILED); + outb(vblk_iobase + VIRTIO_PCI_STATUS, VIRTIO_STATUS_FAILED); return -1; } @@ -191,7 +166,7 @@ int virtio_blk_init(void) { void* frame = pmm_alloc_page(); if (!frame) { kprintf("[VIRTIO-BLK] Failed to alloc vring page.\n"); - outb_port(vblk_iobase + VIRTIO_PCI_STATUS, VIRTIO_STATUS_FAILED); + outb(vblk_iobase + VIRTIO_PCI_STATUS, VIRTIO_STATUS_FAILED); return -1; } vmm_map_page((uint64_t)(uintptr_t)frame, @@ -214,7 +189,7 @@ int virtio_blk_init(void) { outl(vblk_iobase + VIRTIO_PCI_QUEUE_PFN, vring_phys / 4096U); /* Mark driver ready */ - outb_port(vblk_iobase + VIRTIO_PCI_STATUS, + outb(vblk_iobase + VIRTIO_PCI_STATUS, VIRTIO_STATUS_ACK | VIRTIO_STATUS_DRIVER | VIRTIO_STATUS_DRIVER_OK); vblk_ready = 1; @@ -284,7 +259,7 @@ static int vblk_do_request(uint32_t type, uint64_t sector, vblk_last_used_idx++; /* Read ISR to clear interrupt */ - (void)inb_port(vblk_iobase + VIRTIO_PCI_ISR); + (void)inb(vblk_iobase + VIRTIO_PCI_ISR); int ret = (vblk_status_byte == 0) ? 0 : -1; spin_unlock_irqrestore(&vblk_lock, fl); diff --git a/src/hal/x86/ata_dma.c b/src/hal/x86/ata_dma.c index e69048b..8989af7 100644 --- a/src/hal/x86/ata_dma.c +++ b/src/hal/x86/ata_dma.c @@ -1,5 +1,5 @@ #include "ata_dma.h" -#include "kernel_va_map.h" +#include "arch/x86/kernel_va_map.h" #include "pci.h" #include "io.h" #include "pmm.h" diff --git a/src/hal/x86/mm.c b/src/hal/x86/mm.c index c927adc..a853299 100644 --- a/src/hal/x86/mm.c +++ b/src/hal/x86/mm.c @@ -1,7 +1,7 @@ #include "hal/mm.h" #include "vmm.h" -#include "kernel_va_map.h" +#include "arch/x86/kernel_va_map.h" #include diff --git a/src/kernel/vdso.c b/src/kernel/vdso.c index ed09350..c4cde81 100644 --- a/src/kernel/vdso.c +++ b/src/kernel/vdso.c @@ -1,5 +1,5 @@ #include "vdso.h" -#include "kernel_va_map.h" +#include "arch/x86/kernel_va_map.h" #include "pmm.h" #include "vmm.h" #include "utils.h"