OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 8:58 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: [Solved] AHCI SATA read freezes on VirtualBox
PostPosted: Wed Jan 18, 2017 10:13 am 
Offline
Member
Member
User avatar

Joined: Sat Dec 27, 2014 9:11 am
Posts: 901
Location: Maadi, Cairo, Egypt
The title says it all. I can detect SATA disks, and the kernel log output is sensible:
Code:
[00000039] ahci: detecting AHCI controller...
[0000003E] ahci: found AHCI controller on PCI slot 00:0D:00
[0000003E] PCI device 00:0D:00 MMIO at 0xF0806000, mapped at 0xE0013000 -> 0xE0014FFF
[0000003F] ahci: found SATA device 'VBOX HARDDISK' on AHCI port 10
[00000045] Registered AHCI device, device number 0

But when I read from SATA (using the exact same technique I do to identify), it freezes and the bit I set in the command issue register never clears.
Here's the relevant code:
Code:
   ; clear all nescessary structures
   mov edi, ahci_command_list
   mov ecx, end_ahci_command_list - ahci_command_list
   xor al, al
   rep stosb

   mov edi, ahci_command_table
   mov ecx, end_ahci_command_table - ahci_command_table
   xor al, al
   rep stosb

   ; make the command list
   mov [ahci_command_list.cfis_length], (end_ahci_command_fis-ahci_command_fis+3) / 4
   mov [ahci_command_list.prdt_length], 1
   mov dword[ahci_command_list.command_table], ahci_command_table

   ; the command FIS
   mov [ahci_command_fis.fis_type], AHCI_FIS_H2D
   mov [ahci_command_fis.flags], 0x80
   mov [ahci_command_fis.command], SATA_READ_LBA28
   mov [ahci_command_fis.device], 0xE0
   mov eax, [.count]
   mov [ahci_command_fis.count], ax

   ; LBA...
   mov eax, dword[.lba]
   mov [ahci_command_fis.lba0], al
   shr eax, 8
   mov [ahci_command_fis.lba1], al
   shr eax, 8
   mov [ahci_command_fis.lba2], al
   shr eax, 8
   mov [ahci_command_fis.lba3], al

   ; the PRDT
   mov eax, [.buffer_phys]
   mov dword[ahci_prdt.base], eax
   mov eax, [.count]
   shl eax, 9   ; mul 512
   mov [ahci_prdt.count], eax

   ; send the command to the device
   movzx edi, [.port]
   shl edi, 7
   add edi, AHCI_ABAR_PORT_CONTROL
   add edi, [ahci_abar]

   mov eax, [edi+AHCI_PORT_IRQ_STATUS]
   mov [edi+AHCI_PORT_IRQ_STATUS], eax

   and dword[edi+AHCI_PORT_COMMAND], not 1
   and dword[edi+AHCI_PORT_COMMAND_ISSUE], not 1
   mov dword[edi+AHCI_PORT_COMMAND_LIST], ahci_command_list
   mov dword[edi+AHCI_PORT_COMMAND_LIST+4], 0
   or dword[edi+AHCI_PORT_COMMAND], 1
   or dword[edi+AHCI_PORT_COMMAND_ISSUE], 1

.loop:
   test dword[edi+AHCI_PORT_COMMAND_ISSUE], 1   ; never leaves this loop!!
   jnz .loop

.after_loop:
   ; turn off the command execution
   and dword[edi+AHCI_PORT_COMMAND], not 1
   and dword[edi+AHCI_PORT_COMMAND_ISSUE], not 1


It works on QEMU, though. Is there something obvious I'm missing because I've been awake for a long time? [-o<

_________________
You know your OS is advanced when you stop using the Intel programming guide as a reference.


Last edited by BrightLight on Thu Jan 19, 2017 8:19 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: AHCI SATA read freezes on VirtualBox
PostPosted: Wed Jan 18, 2017 10:23 am 
Offline
Member
Member
User avatar

Joined: Sun Dec 25, 2016 1:54 am
Posts: 204
Code:
; the PRDT
   mov eax, [.buffer_phys]
   mov dword[ahci_prdt.base], eax
   mov eax, [.count]
   shl eax, 9   ; mul 512
   mov [ahci_prdt.count], eax


I am no expert but isn't it multiply by 512 and then subtract 1? (4.2.3.3: DBC is number of bytes - 1)

_________________
Plagiarize. Plagiarize. Let not one line escape thine eyes...


Top
 Profile  
 
 Post subject: Re: AHCI SATA read freezes on VirtualBox
PostPosted: Wed Jan 18, 2017 10:27 am 
Offline
Member
Member
User avatar

Joined: Sun Dec 25, 2016 1:54 am
Posts: 204
Code:
.loop:
   test dword[edi+AHCI_PORT_COMMAND_ISSUE], 1   ; never leaves this loop!!
   jnz .loop


also shouldn't you also be moving the flag to eax to test?

Code:
.loop:
    mov eax, [edi+AHCI_PORT_COMMAND_ISSUE]
    test eax, eax
    jnz .loop


Again I am no expert but worth a try...

_________________
Plagiarize. Plagiarize. Let not one line escape thine eyes...


Top
 Profile  
 
 Post subject: Re: AHCI SATA read freezes on VirtualBox
PostPosted: Wed Jan 18, 2017 10:28 am 
Offline
Member
Member
User avatar

Joined: Sat Dec 27, 2014 9:11 am
Posts: 901
Location: Maadi, Cairo, Egypt
dchapiesky wrote:
I am no expert but isn't it multiply by 512 and then subtract 1? (4.2.3.3: DBC is number of bytes - 1)

Thanks, but that didn't help. The task file register contains 0x41, in case it's relevant.

_________________
You know your OS is advanced when you stop using the Intel programming guide as a reference.


Top
 Profile  
 
 Post subject: Re: AHCI SATA read freezes on VirtualBox
PostPosted: Wed Jan 18, 2017 10:32 am 
Offline
Member
Member
User avatar

Joined: Sun Dec 25, 2016 1:54 am
Posts: 204
Lest I seem knowledgeable I am simply comparing against BareMetal-OS...

https://github.com/ReturnInfinity/BareM ... e/ahci.asm

readsectors_poll:

_________________
Plagiarize. Plagiarize. Let not one line escape thine eyes...


Top
 Profile  
 
 Post subject: Re: AHCI SATA read freezes on VirtualBox
PostPosted: Wed Jan 18, 2017 10:37 am 
Offline
Member
Member
User avatar

Joined: Sat Dec 27, 2014 9:11 am
Posts: 901
Location: Maadi, Cairo, Egypt
I've found the problem. I was sending command byte 0x20 (PIO LBA28 read) while, obviously, it should have been 0xC8 (DMA LBA28 read). Thanks! :)

_________________
You know your OS is advanced when you stop using the Intel programming guide as a reference.


Top
 Profile  
 
 Post subject: Re: AHCI SATA read freezes on VirtualBox
PostPosted: Thu Jan 19, 2017 4:15 am 
Offline
Member
Member
User avatar

Joined: Thu Jul 12, 2012 7:29 am
Posts: 723
Location: Tallinn, Estonia
Mark the topic [Solved], Omar!

_________________
Learn to read.


Top
 Profile  
 
 Post subject: Re: AHCI SATA read freezes on VirtualBox
PostPosted: Thu Jan 19, 2017 9:21 am 
Offline
Member
Member
User avatar

Joined: Mon Jul 28, 2008 9:46 am
Posts: 325
Location: Ontario, Canada
BareMetal to the rescue! :P

dchapiesky wrote:
Lest I seem knowledgeable I am simply comparing against BareMetal-OS...

https://github.com/ReturnInfinity/BareM ... e/ahci.asm

readsectors_poll:

_________________
BareMetal OS - http://www.returninfinity.com/
Mono-tasking 64-bit OS for x86-64 based computers, written entirely in Assembly


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], DotBot [Bot] and 63 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group