Page 1 of 1

How do I get a cr4 register using C?

Posted: Tue Jun 02, 2020 1:27 pm
by mrjbom
Hi.
I try to get it with this kind of code

Code: Select all

uint32_t cr4 = 0;
__asm__ (
    "mov %%cr4, %0" : "=r"(cr4)
);
dprintf("cr4 = %I\n", cr4);
for(int i = 0; i < 32; ++i) {
    dprintf("%I", (cr4 >> i) & 1);
}
but I get 0, for cr0 it works.

Re: How do I get a cr4 register using C?

Posted: Tue Jun 02, 2020 2:17 pm
by kzinti
Could it be that CR4 is indeed 0?

Re: How do I get a cr4 register using C?

Posted: Tue Jun 02, 2020 2:19 pm
by mrjbom
kzinti wrote:Could it be that CR4 is indeed 0?
No, I have PAE enabled(GRUB includes it), so 5 bits should be equal to 1.

Re: How do I get a cr4 register using C?

Posted: Tue Jun 02, 2020 3:04 pm
by iansjack
Are you sure? I'm probably wrong, but I thought PAE was something that was enabled in the kernel, not the boot loader.

Re: How do I get a cr4 register using C?

Posted: Tue Jun 02, 2020 3:08 pm
by mrjbom
iansjack wrote:Are you sure? I'm probably wrong, but I thought PAE was something that was enabled in the kernel, not the boot loader.
Hm.
the memory_map that GRUB gives me sees memory above 4 GB, I think it does it with PAE and expect to see it with this call.
Perhaps it is really disabled in the core.

Re: How do I get a cr4 register using C?

Posted: Tue Jun 02, 2020 3:39 pm
by kzinti
No, Grub doesn't enable PAE for you. In fact, paging is not even enabled.

The fact that it reports memory above 4 GB has nothing to do with PAE being enabled or not.

You have to setup page tables for PAE and enable it yourself if you want to use it.

See https://www.pvv.ntnu.no/~steinarh/grub/ ... html#SEC12:
Paging must be turned off.

Re: How do I get a cr4 register using C?

Posted: Tue Jun 02, 2020 4:03 pm
by mrjbom
kzinti wrote:No, Grub doesn't enable PAE for you. In fact, paging is not even enabled.

The fact that it reports memory above 4 GB has nothing to do with PAE being enabled or not.

You have to setup page tables for PAE and enable it yourself if you want to use it.

See https://www.pvv.ntnu.no/~steinarh/grub/ ... html#SEC12:
Paging must be turned off.
I understood. Thanks.