Thread: Inline GCC Help with reading CR4 register

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    19

    Inline GCC Help with reading CR4 register

    Hi, this is an attempt to read the CR4 register into the A register (rax for 64 bit) on fedora 11 64 bit. The movq operation produces a segment fault.

    Code:
    typedef unsigned long long UINT64;
    typedef union
    {
            UINT64 value;
            struct
            {
                     UINT64 VME0:1; 
                    UINT64 PVI1:1; 
                    UINT64 TSDB2:1;
                    UINT64 DE3:1; 
                    UINT64 PSE4:1;
                    UINT64 PAE5:1;
                    UINT64 MCE6:1;
                    UINT64 PGE7:1; 
                    UINT64 PCE8:1; 
                    UINT64 OSFXSR9:1; 
                    UINT64 OSXMMEXCPT10:1; 
                    UINT64 bit1112:2; // reserved
                    UINT64 VMXE13:1; 
                    UINT64 SMXE14:1; 
                    UINT64 bit1517:3; // reserved
                    UINT64 OSXSAVE18:1; 
                    UINT64 bit1931:13; // reserved
                    UINT64 bit3264:32; // reserved should be 0
            }bits;
    }CR4;
    
    int main()
    {
      unsigned long long result;
      //CR4 cr4_reg;
      printf("\n size of long long is %x ", sizeof(result));
      fflush(NULL);
      __asm__("movq   %%cr4, %%rax\n"
            : "=a"(result) : "a"
      );
     printf("\n Value of CR4 = %x", result);
     return 0;
    }
    Thanks for helping me out

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Have you examined the generated code to ensure that the instruction is being assembled as you expect?

    If it looks correct, then the only reason you'd get a segfault would be that access to the CR4 register is a privileged operation. I don't remember off the top of my head if that's the case.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    19
    Yes, you are right, I figured it later on after much sweating. It cannot be accessed from an application and can be accessed through the kernel.

    thanks for the reply

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a 16 bit Register
    By Bladactania in forum C Programming
    Replies: 7
    Last Post: 07-04-2009, 03:15 PM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. gcc inline asm: illegal instruction (core dump)
    By Sargnagel in forum C Programming
    Replies: 4
    Last Post: 10-28-2003, 01:41 PM
  4. register variables
    By Draco in forum C Programming
    Replies: 7
    Last Post: 08-21-2002, 01:01 PM
  5. Register
    By sean345 in forum C Programming
    Replies: 7
    Last Post: 05-08-2002, 03:06 PM

Tags for this Thread