Page 1 of 1

simple ata pio driver

Posted: Sat Nov 12, 2022 10:58 am
by JustVic
I'm trying to get a simple ATA PIO driver to work. I read https://wiki.osdev.org/ATA_PIO_Mode . But when I try to read(data from disk), I get a PAGE FAULT error. Why could this happen? And how to fix it?
ata.c:

Code: Select all

#define STATUS_BSY 0x80
#define STATUS_RDY 0x40
#define STATUS_DRQ 0x08
#define STATUS_DF 0x20
#define STATUS_ERR 0x01

static void ATA_wait_BSY();
static void ATA_wait_DRQ();
void read_sectors_ATA_PIO(uint32_t target_address, uint32_t LBA, uint8_t count)
{

	ATA_wait_BSY();
	port_outb(0x1F6,0xE0 | ((LBA >>24) & 0xF));
	port_outb(0x1F2,count);
	port_outb(0x1F3, (uint8_t) LBA);
	port_outb(0x1F4, (uint8_t)(LBA >> 8));
	port_outb(0x1F5, (uint8_t)(LBA >> 16)); 
	port_outb(0x1F7,0x20); //Send the read command

	uint16_t *target = (uint16_t*) target_address;

	for (int j =0;j<count;j++)
	{
		ATA_wait_BSY();
		ATA_wait_DRQ();
		for(int i=0;i<256;i++)
			target[i] = port_inw(0x1F0);
		target+=256;
	}
}

void write_sectors_ATA_PIO(uint32_t LBA, uint8_t count, uint32_t* bytes)
{ 
...
page fault:
[Error 14 at ring 0] 2:0H FFFF80000020C54CH

github link(devel branch):https://github.com/JustVic/melisa_kernel/tree/devel

Re: simple ata pio driver

Posted: Sat Nov 12, 2022 11:52 am
by kzinti
What is the value of target_address? Did you map that memory in your paging structures? With what flags?

Re: simple ata pio driver

Posted: Sat Nov 12, 2022 12:00 pm
by iansjack
What is at the faulting memory location? And what type of access caused the fault? What function is running when the fault occurs? The address of the faulting instruction tells you exactly which line of code is being executed.

Re: simple ata pio driver

Posted: Sat Nov 12, 2022 12:06 pm
by JustVic
This line make the page fault:

Code: Select all

target[i] = port_inw(0x1F0);

Re: simple ata pio driver

Posted: Sat Nov 12, 2022 12:14 pm
by iansjack
Then, as kzinti says, you haven’t mapped that address (or you’ve mapped it with the wrong access rights).

Looking further, I can’t see where you have defined the address of your buffer. You declare *target but don’t seem to assign a value to that pointer.

Re: simple ata pio driver

Posted: Sat Nov 12, 2022 12:28 pm
by JustVic
Sorry I understood, I just not allocate memory for pointer target_address. :oops: