Page 1 of 1

Page fault when trying to scan for AHCI sata drives

Posted: Mon Jun 14, 2021 4:48 am
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?

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

Posted: Mon Jun 14, 2021 7:04 am
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?

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

Posted: Tue Jun 15, 2021 1:07 am
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<

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

Posted: Tue Jun 15, 2021 8:36 am
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.

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

Posted: Tue Jun 22, 2021 1:28 am
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!