[Solved] AHCI SATA read freezes on VirtualBox

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
User avatar
BrightLight
Member
Member
Posts: 901
Joined: Sat Dec 27, 2014 9:11 am
Location: Maadi, Cairo, Egypt
Contact:

[Solved] AHCI SATA read freezes on VirtualBox

Post by BrightLight »

The title says it all. I can detect SATA disks, and the kernel log output is sensible:

Code: Select all

[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: Select all

	; 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<
Last edited by BrightLight on Thu Jan 19, 2017 8:19 am, edited 1 time in total.
You know your OS is advanced when you stop using the Intel programming guide as a reference.
User avatar
dchapiesky
Member
Member
Posts: 204
Joined: Sun Dec 25, 2016 1:54 am
Libera.chat IRC: dchapiesky

Re: AHCI SATA read freezes on VirtualBox

Post by dchapiesky »

Code: Select all

; 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...
User avatar
dchapiesky
Member
Member
Posts: 204
Joined: Sun Dec 25, 2016 1:54 am
Libera.chat IRC: dchapiesky

Re: AHCI SATA read freezes on VirtualBox

Post by dchapiesky »

Code: Select all

.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: Select all

.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...
User avatar
BrightLight
Member
Member
Posts: 901
Joined: Sat Dec 27, 2014 9:11 am
Location: Maadi, Cairo, Egypt
Contact:

Re: AHCI SATA read freezes on VirtualBox

Post by BrightLight »

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.
User avatar
dchapiesky
Member
Member
Posts: 204
Joined: Sun Dec 25, 2016 1:54 am
Libera.chat IRC: dchapiesky

Re: AHCI SATA read freezes on VirtualBox

Post by dchapiesky »

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...
User avatar
BrightLight
Member
Member
Posts: 901
Joined: Sat Dec 27, 2014 9:11 am
Location: Maadi, Cairo, Egypt
Contact:

Re: AHCI SATA read freezes on VirtualBox

Post by BrightLight »

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.
User avatar
dozniak
Member
Member
Posts: 723
Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia

Re: AHCI SATA read freezes on VirtualBox

Post by dozniak »

Mark the topic [Solved], Omar!
Learn to read.
User avatar
IanSeyler
Member
Member
Posts: 326
Joined: Mon Jul 28, 2008 9:46 am
Location: Ontario, Canada
Contact:

Re: AHCI SATA read freezes on VirtualBox

Post by IanSeyler »

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
Post Reply