Page fault when trying to scan for AHCI sata drives

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
C0dR
Posts: 3
Joined: Mon Jun 14, 2021 4:39 am

Page fault when trying to scan for AHCI sata drives

Post by C0dR »

Hello,

i am currently working on AHCI implementation. I use the codebase from "MonkOS" (https://github.com/beevik/MonkOS) and tried to implement AHCI based on the code from https://github.com/ethan4984/rock/blob/ ... ers/ahci.c

getting pci bar information does work but as soon as I start reading from that memory region I get a page fault.
Here is my AHCI_init code so far:

Code: Select all

void ahci_init(struct pci_device *device) {
    switch(device->prog_if) {
        case 0:
            kprint("[AHCI] detected a vendor specific interface (get a new pc)\n");
            return;
        case 1:
            kprint("[AHCI] detetced an AHCI 1.0 compatiable device\n");
            break;
        case 2:
            kprint("[AHCI] detetced a serial storage bus\n");
            return;
    }
    
    
    pci_become_bus_master(device);


    struct pci_bar bar;
    if(pci_get_bar(device, &bar, 5) == -1)
        return;

    pmap_add(PAGE_ALIGN_DOWN(bar.base), PAGE_SIZE, PMEMTYPE_UNCACHED);  //<-- this might be wrong?

    volatile struct GHC *GHC = (volatile struct GHC*)((size_t)bar.base);

    for(int i = 0; i < 32; i++) {
        if(GHC->pi & (1 << i)) {
            volatile struct port_regs *regs = (volatile struct port_regs*)&GHC->port[i];

            switch(regs->sig) {
                case SATA_ATA:
                    kprintf("[AHCI] sata drive found on port %d\n", i);
                    init_sata_device(regs);
                    break;
                case SATA_ATAPI:
                    kprintf("[AHCI] ATAPI drive found on port %d\n", i); 
                    break;
                case SATA_SEMB:
                    kprintf("[AHCI] enclosure management bridge found on port %d\n", i); 
                    break;
                case SATA_PM:
                    kprintf("[AHCI] port multipler found on port %d\n", i); 
                    break;
            }
        }
    }
}
I assume I am doing something wrong with memory mapping. When I put the line "pmap_add(PAGE_ALIGN_DOWN(bar.base), PAGE_SIZE, PMEMTYPE_UNCACHED);" with hardcoded adress values inside the function "pmap_init()" (https://github.com/beevik/MonkOS/blob/m ... map.c#L292) i dont get a page fault. I assume the problem is my AHCI initialization happens after the call to pmap_init()?

Maybe I am misunderstanding the memory management in the codebase of "MonkOS"? Might someone lead me in the right direction?
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Page fault when trying to scan for AHCI sata drives

Post by iansjack »

A page fault ought to be easy enough to find. Have you run under a debugger, watching what happens when you map the page?
C0dR
Posts: 3
Joined: Mon Jun 14, 2021 4:39 am

Re: Page fault when trying to scan for AHCI sata drives

Post by C0dR »

iansjack wrote:A page fault ought to be easy enough to find. Have you run under a debugger, watching what happens when you map the page?
no not yet, I will try as soon as I get a working GDB connection to qemu... :roll:


//EDIT: Ok I managed to attach gdb to the VM but I still dont get the problem. The Page Fault error occures as soon as i try to access the GHC values. Its this line:

Code: Select all

if(GHC->pi & (1 << i)) {
I have the feeling I am completly misunderstanding the memory management implementation of the MonkOS codebase. Could someone maybe explain to me the memory management implementation of monkOS? [-o<
Octocontrabass
Member
Member
Posts: 5567
Joined: Mon Mar 25, 2013 7:01 pm

Re: Page fault when trying to scan for AHCI sata drives

Post by Octocontrabass »

C0dR wrote:I have the feeling I am completly misunderstanding the memory management implementation of the MonkOS codebase. Could someone maybe explain to me the memory management implementation of monkOS? [-o<
It looks like MonkOS is only capable of mapping usable RAM in the page tables. You'll have to write new code to handle mapping and unmapping MMIO.
C0dR
Posts: 3
Joined: Mon Jun 14, 2021 4:39 am

Re: Page fault when trying to scan for AHCI sata drives

Post by C0dR »

Octocontrabass wrote:
C0dR wrote:I have the feeling I am completly misunderstanding the memory management implementation of the MonkOS codebase. Could someone maybe explain to me the memory management implementation of monkOS? [-o<
It looks like MonkOS is only capable of mapping usable RAM in the page tables. You'll have to write new code to handle mapping and unmapping MMIO.
OK I see, that explains. Thank you, that led me in the right direction!
Post Reply