From 3a9e7889aeadaf9b57ec238ab4332bb3d6139d53 Mon Sep 17 00:00:00 2001 From: notsomeidiot123 Date: Sat, 28 Feb 2026 21:48:45 -0500 Subject: [PATCH] Add locking and fix race condition for ata_read() --- makefile | 2 +- src/kmodules/disk_driver.c | 22 +++++++++++++++++----- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/makefile b/makefile index 43fccec..852a956 100644 --- a/makefile +++ b/makefile @@ -19,7 +19,7 @@ all: bootloader tools kernel # sudo mkdir mount/sys # for system files # ./diskwrite -v -o image.bin # ./diskwrite -v -l kernel.elf idm.elf -o image.bin - qemu-system-i386 -hda image.bin -hdc test.bin --no-reboot --no-shutdown -m 32m -smp 2 -serial mon:stdio -D intlog.txt -d int + qemu-system-i386 -hda image.bin -hdb test.bin --no-reboot --no-shutdown -m 32m -smp 2 -serial mon:stdio -D intlog.txt -d int sudo umount mount tools: diff --git a/src/kmodules/disk_driver.c b/src/kmodules/disk_driver.c index 235a78f..8184c8b 100644 --- a/src/kmodules/disk_driver.c +++ b/src/kmodules/disk_driver.c @@ -142,6 +142,7 @@ uint32_t recieved_ints = 0; uint32_t volatile transferring_pid = 0; uint8_t volatile locked = 0; + typedef struct drive_desc{ uint32_t BARs[8]; enum DRIVE_TYPE type; @@ -293,6 +294,17 @@ int ata_read(vfile_t *file, uint8_t *ptr, uint32_t offset, uint32_t count) { uint16_t ctrl_base = drive.BARs[1] &0xfffe; uint16_t bm_base = drive.BARs[4] & ~3; + if(!is_interrupt(api)){ + asm("cli"); + while(locked){ + asm("sti"); + puts(api, "KIDM", "Locked\n"); + asm("int $32"); + asm("cli"); + } + locked = 1; + asm("sti"); + } PRD_T *prdt = drive.PRDT; api(MODULE_API_PRINT, MODULE_NAME, "%x, %x, %x, %x\n", io_base, ctrl_base, bm_base, api(MODULE_API_PADDR, prdt)); @@ -353,10 +365,14 @@ int ata_read(vfile_t *file, uint8_t *ptr, uint32_t offset, uint32_t count) { expected_ints = pages; recieved_ints = 0; + transferring_disk_index = file->mount_id; + uint32_t cpid = api(MODULE_API_GET_CPID); + // api(MODULE_API_PRINT, MODULE_NAME, "Cpid: %x", cpid); + api(MODULE_API_BLOCK_PID, cpid); + transferring_pid = cpid; outb(bm_base, 0x09); - transferring_disk_index = file->mount_id; uint8_t status = inb(ctrl_base); uint8_t bm_status = inb(bm_base + 2); @@ -371,10 +387,6 @@ int ata_read(vfile_t *file, uint8_t *ptr, uint32_t offset, uint32_t count) { // status = inb(io_base + ATA_STATUS); // } // outb(bm_base, 0x00); - uint32_t cpid = api(MODULE_API_GET_CPID); - // api(MODULE_API_PRINT, MODULE_NAME, "Cpid: %x", cpid); - api(MODULE_API_BLOCK_PID, cpid); - transferring_pid = cpid; if(!is_interrupt){ asm("int $32\n"); }