Issues with loading ACPI tables with LAI

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
YDeeps1
Member
Member
Posts: 69
Joined: Tue Aug 31, 2021 7:25 am
Discord: speedy.dev
Contact:

Issues with loading ACPI tables with LAI

Post by YDeeps1 »

I have integrated the LAI core AML interpreter (https://github.com/managarm/lai) into my OS but I am having a few problems.
I have tried to create a namespace with:

Code: Select all

lai_set_acpi_revision(rsdp->Revision);
lai_create_namespace();
These are the logs that have occurred when trying to create a namespace:
[ACPI] Discovered table with signature FACP
[ACPI] Discovered table with signature APIC
[ACPI] Discovered table with signature HPET
[ACPI LAI] loaded AML table 'DSDT', total 1634628864 bytes of AML code.
[ACPI LAI WARN] undefined reference PSDT in object mode, aborting
[ACPI LAI WARN] lai_exec_run() failed in lai_populate()
[ACPI LAI] loaded AML table 'SSDT', total 1146310656 bytes of AML code.
[ACPI LAI PANIC] assertion failed: parse_mode == LAI_OBJECT_MODE at src/c/lai/exec.c:3083
I don't know too much about LAI so I don't know how to interpret them, hoping someone with more experience can help here.
But the fact the "total bytes for the DSDT table" is about 1.6GB points to a serious problem.

Details:
qemu i386 emulator
32-bit kernel booted with GRUB
Using cross-compiler with libgcc
C++ kernel with C compiled with i686 gcc and C++ compiled with i686 g++
No paging enabled or handled

laihost_scan implementation (all structs such as RSDT are from the forum page):

Code: Select all

void* locate_rsdp() {
    // Start address of EBDA.
    uint8_t* ebda = (uint8_t*)0x9FC00;
    
    // Look for RSDP in the bios extended area.
    for (uint32_t i = 0; i < 64; i++) {
        uint8_t* segment = (ebda + (i * 16));
        
        // Verify signature.
        if (verify_signature(segment, 8, "RSD PTR ") == true) {
            
            // RSDP found, return address.
            return segment;
        }
    }
    
    // Address of possible RSDP area.
    uint8_t* mba = (uint8_t*)0x000E0000;
    
    for (uint32_t i = 0; i < 8192; i++) {
        uint8_t* segment = mba + (i * 16);
        
        // Verify signature.
        if (verify_signature(segment, 8, "RSD PTR ") == true) {
            
            // RSDP found, return address.
            return segment;
        }
    }

    return NULL;
}

void* laihost_scan(const char* signature, size_t index) {
    struct RSDT* rsdp = locate_rsdp();

    uint32_t entries = (rsdp->h.Length - sizeof(rsdp->h)) / 4;
    uint32_t current_index = 0;
 
    for (uint32_t i = 0; i < entries; i++) {
        // Get table of header.
        struct SDTHeader* header = rsdp->table_pointers[i];

        // Check if signature matches requested table.
        if (verify_signature(header, 4, (char*)signature) == true) {
            if (index == current_index) return header;
            else current_index++;
        }
    }

    return NULL;
}
I should also mention the code which finds the tables works fine as I have tried running them on their own and they locate the relevant tables without a problem.

Thanks!
davmac314
Member
Member
Posts: 121
Joined: Mon Jul 05, 2021 6:57 pm

Re: Issues with loading ACPI tables with LAI

Post by davmac314 »

Code: Select all

[ACPI LAI] loaded AML table 'DSDT', total 1634628864 bytes of AML code.
I don't much about LAI, but that seems like way too much AML code to be right.

Is it possible the ACPI tables aren't all mapped in correctly or that LAI isn't aware of the proper physical-to-virtual mapping?
YDeeps1
Member
Member
Posts: 69
Joined: Tue Aug 31, 2021 7:25 am
Discord: speedy.dev
Contact:

Re: Issues with loading ACPI tables with LAI

Post by YDeeps1 »

davmac314 wrote:

Code: Select all

[ACPI LAI] loaded AML table 'DSDT', total 1634628864 bytes of AML code.
I don't much about LAI, but that seems like way too much AML code to be right.

Is it possible the ACPI tables aren't all mapped in correctly or that LAI isn't aware of the proper physical-to-virtual mapping?
Are you talking about paging? If so I don't have it enabled.

I also tried another emulator like virtualbox and the same occurred, it appears to be reading more memory than it should - long shot of a theory but maybe my kernel is positioned too low or too high? Or something else in my code such as the heap is positioned where the ACPI tables are?
davmac314
Member
Member
Posts: 121
Joined: Mon Jul 05, 2021 6:57 pm

Re: Issues with loading ACPI tables with LAI

Post by davmac314 »

YDeeps1 wrote:Are you talking about paging? If so I don't have it enabled.
I was; ok, that's ruled out.
But the fact the "total bytes for the DSDT table" is about 1.6GB points to a serious problem
I somehow missed that when I read it the first time. Yeah, it's not right.
I also tried another emulator like virtualbox and the same occurred, it appears to be reading more memory than it should - long shot of a theory but maybe my kernel is positioned too low or too high? Or something else in my code such as the heap is positioned where the ACPI tables are?
There's only really two possible causes that I can think of: the table pointer you're returning is incorrect (your code looks fine, is perhaps the table structure definition not right?) or the data has been corrupted somehow (overwritten).
YDeeps1
Member
Member
Posts: 69
Joined: Tue Aug 31, 2021 7:25 am
Discord: speedy.dev
Contact:

Re: Issues with loading ACPI tables with LAI

Post by YDeeps1 »

I did check and everything should be positioned correctly so I don't know what the issue may be.
I have included a brief AML dump in case anyone would want to see it:
Image
davmac314
Member
Member
Posts: 121
Joined: Mon Jul 05, 2021 6:57 pm

Re: Issues with loading ACPI tables with LAI

Post by davmac314 »

What does the DSDT header look like? Is the signature correct? What does the length field contain?
YDeeps1
Member
Member
Posts: 69
Joined: Tue Aug 31, 2021 7:25 am
Discord: speedy.dev
Contact:

Re: Issues with loading ACPI tables with LAI

Post by YDeeps1 »

davmac314 wrote:What does the DSDT header look like? Is the signature correct? What does the length field contain?
Sure.
Image
The header seems completely correct. The issue may perhaps be pointing to my malloc/free implementation? But I have tested it many times so I don't think it's that.
YDeeps1
Member
Member
Posts: 69
Joined: Tue Aug 31, 2021 7:25 am
Discord: speedy.dev
Contact:

Re: Issues with loading ACPI tables with LAI

Post by YDeeps1 »

I found the problem and it's a proper facepalm moment.
I was digging around the LAI source code trying to figure out the possible issue and then looked at some old code I wrote to retrieve the tables and went "hold on since when does the RSDP have a pointer to the RSDT?".
I took a long break from osdev and completely forget the structure you search for initially is the RSDP which points to the RSDT and not the RSDT itself.

=D>
davmac314
Member
Member
Posts: 121
Joined: Mon Jul 05, 2021 6:57 pm

Re: Issues with loading ACPI tables with LAI

Post by davmac314 »

Ahh. I should have noticed that. Easy mistake to make, though.
Post Reply