Page 1 of 1

Page fault when accessing the GRUB framebuffer

Posted: Tue May 18, 2021 4:58 am
by catOS
Hello,

In my OS (x86_64), I am supporting multiboot2. I've done the normal procedure of parsing the multiboot structure and retrieving a framebuffer and various information. But here's the part that bugs me, if I try the following demo, a page fault will occur

Code: Select all

    uint32_t* fb_addr = (uint32_t*)uheader.framebuffer_addr;

    for(size_t i = 0; i < uheader.framebuffer_width; i++)
    {
        fb_addr[i] = 0x34eb7d;
        fb_addr[i + uheader.framebuffer_width] = 0x34eb7d;
        fb_addr[i + (uheader.framebuffer_width * 2)] = 0x34eb7d;
        fb_addr[i + (uheader.framebuffer_width * 3)] = 0x34eb7d;
        fb_addr[i + (uheader.framebuffer_width * 4)] = 0x34eb7d;
        for(int i = 0; i < 1000000; i++); // latency test
    }

Code: Select all

bootloader_name: GRUB 2.04-1ubuntu26.8
command_line: 
framebuffer: addr=0xfd000000; width=800; height=600

int_num=0xe; error=0x2
kernel panic!
    occurred in file arch/x86_64/features/interrupts.cpp:69:0 in the function 'page_fault_handler()'
    page fault
but if I use Limine the code runs fine without any problems.

I'm not exactly sure what's causing this issue. I thought that maybe it has something to do about being in long mode

https://github.com/ackOS-project/ackOS

Re: Page fault when accessing the GRUB framebuffer

Posted: Tue May 18, 2021 5:09 am
by iansjack
Have you mapped the frame buffer in your page table?

Re: Page fault when accessing the GRUB framebuffer

Posted: Tue May 18, 2021 5:26 am
by klange
catOS wrote:but if I use Limine the code runs fine without any problems.

I'm not exactly sure what's causing this issue. I thought that maybe it has something to do about being in long mode

https://github.com/ackOS-project/ackOS
Your longmode bootstrap appears to be only mapping one 2MiB page, which obviously will not get you the region just below 4GiB where that framebuffer lives. Limine's stivale spec loads an identity map for all of the 32-bit address space.

Re: Page fault when accessing the GRUB framebuffer

Posted: Wed May 19, 2021 4:45 am
by catOS
iansjack wrote:Have you mapped the frame buffer in your page table?
no, but how?

Re: Page fault when accessing the GRUB framebuffer

Posted: Wed May 19, 2021 5:09 am
by iansjack
catOS wrote:
iansjack wrote:Have you mapped the frame buffer in your page table?
no, but how?
The same way that you map any other memory pages. Just make an entry for the memory range in your page table. It's probably easier to identity map the range, but you could assign it a different virtual address if you wish.

When you are in paging mode any memory that you address must be mapped in your page tables.

Re: Page fault when accessing the GRUB framebuffer

Posted: Wed May 19, 2021 12:10 pm
by bzt
catOS wrote:
iansjack wrote:Have you mapped the frame buffer in your page table?
no, but how?
Take a look at BOOTBOOT source code. Unlike limine and Grub, it will map the framebuffer for you. It is MIT licensed, so feel free to study its source.
Mapping framebuffer is special, should be handled as MMIO, and not like normal RAM (for one, always use write-through policy).

Cheers,
bzt