From 914998cf5238a6405271478e815a4db89e55a2ec Mon Sep 17 00:00:00 2001 From: Tulio A M Mendes Date: Sun, 15 Feb 2026 01:38:44 -0300 Subject: [PATCH] refactor: move kernel_va_map.h to include/arch/x86/, clean virtio_blk.c port I/O MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit - 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) --- include/{ => arch/x86}/kernel_va_map.h | 0 src/arch/x86/acpi.c | 2 +- src/arch/x86/ioapic.c | 2 +- src/arch/x86/lapic.c | 2 +- src/drivers/e1000.c | 2 +- src/drivers/vbe.c | 2 +- src/drivers/virtio_blk.c | 41 +++++--------------------- src/hal/x86/ata_dma.c | 2 +- src/hal/x86/mm.c | 2 +- src/kernel/vdso.c | 2 +- 10 files changed, 16 insertions(+), 41 deletions(-) rename include/{ => arch/x86}/kernel_va_map.h (100%) diff --git a/include/kernel_va_map.h b/include/arch/x86/kernel_va_map.h similarity index 100% rename from include/kernel_va_map.h rename to include/arch/x86/kernel_va_map.h 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" -- 2.43.0