How do I get a cr4 register using C?

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
User avatar
mrjbom
Member
Member
Posts: 317
Joined: Sun Jul 21, 2019 7:34 am

How do I get a cr4 register using C?

Post 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.
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

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

Post by kzinti »

Could it be that CR4 is indeed 0?
User avatar
mrjbom
Member
Member
Posts: 317
Joined: Sun Jul 21, 2019 7:34 am

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

Post 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.
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

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

Post 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.
User avatar
mrjbom
Member
Member
Posts: 317
Joined: Sun Jul 21, 2019 7:34 am

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

Post 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.
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

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

Post 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.
User avatar
mrjbom
Member
Member
Posts: 317
Joined: Sun Jul 21, 2019 7:34 am

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

Post 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.
Post Reply