simple ata pio driver

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
JustVic
Posts: 17
Joined: Tue Jul 07, 2020 3:18 pm

simple ata pio driver

Post 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
Last edited by JustVic on Sat Nov 12, 2022 12:06 pm, edited 1 time in total.
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: simple ata pio driver

Post by kzinti »

What is the value of target_address? Did you map that memory in your paging structures? With what flags?
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: simple ata pio driver

Post 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.
JustVic
Posts: 17
Joined: Tue Jul 07, 2020 3:18 pm

Re: simple ata pio driver

Post by JustVic »

This line make the page fault:

Code: Select all

target[i] = port_inw(0x1F0);
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: simple ata pio driver

Post 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.
JustVic
Posts: 17
Joined: Tue Jul 07, 2020 3:18 pm

Re: simple ata pio driver

Post by JustVic »

Sorry I understood, I just not allocate memory for pointer target_address. :oops:
Post Reply