-1

I'm looking for a way to know if the cpu of my linux system is running in 64 bit mode. I've got to programm it for the university. My idea was to create something like an function that provides me that information. I'm not sure how should I start. Can somebody give me an idea?. My university give me a code, but I'm not able to check the length of the parameter that the main is sending to the funktion is64bit. My code is this:

int main()
{

    uint32_t msr = assembleMSR( 0, 0, 0, 0 );

    printf("assembled MSR is %#x, should be 0\n", msr);
    printf("is64BitMode(&msr) should be 0, is %d\n", is64BitMode(&msr));
    printf("is64BitMode(&msr) should my EXAMPLE, is %d\n", is64BitMode(&msr2));

    enableInts( &msr);

    printf("MSR with ints enabled is %#x, should be 0x8000\n", msr);

    return 0;
}

int is64BitMode ( uint32_t *MSR ) {

    int longi=sizeof(MSR);  

    printf("longi de size of%d\n", longi);
    printf("%d\n", __WORDSIZE);// with this sentence i know that my cpu is running on 64 bits.


    (void) MSR;
    return 0;
    /* TODO */
}
hyde
  • 60,639
  • 21
  • 115
  • 176

2 Answers2

0

If you want to determine CPU's possibilities:

Use:

$ grep flags /proc/cpuinfo

And then you will observe result:

flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm constant_tsc pni monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr lahf_lm
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm constant_tsc pni monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr lahf_lm
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm constant_tsc pni monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr lahf_lm
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm constant_tsc pni monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr lahf_lm

If flag lm is present that means Long mode cpu - 64 bit CPU.

Alternatively you can use command:

lshw

It'll produce clear result, on 32bit kernel e.g:

# 32-bit Kernel
$ lshw -class cpu
  *-cpu:0
   description: CPU
   product: Intel(R) Core(TM)2 CPU          4300  @ 1.80GHz
   vendor: Intel Corp.
   physical id: 400
   bus info: cpu@0
   version: 6.15.2
   serial: 0000-06F2-0000-0000-0000-0000
   slot: Microprocessor
   size: 1800MHz
   width: 64 bits
   clock: 800MHz
   capabilities: boot fpu fpu_exception wp vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe x86-64 constant_tsc pni monitor ds_cpl est tm2 ssse3 cx16 xtpr lahf_lm
   configuration: id=1
 *-logicalcpu:0
      description: Logical CPU
      physical id: 1.1
      width: 64 bits
      capabilities: logical
 *-logicalcpu:1
      description: Logical CPU
      physical id: 1.2
      width: 64 bits
      capabilities: logical

And on 64bit kernel e.g:

# 64-bit Kernel
  *-cpu                   
   description: CPU
   product: Intel(R) Core(TM) i5 CPU       M 560  @ 2.67GHz
   vendor: Intel Corp.
   physical id: 6
   bus info: cpu@0
   version: Intel(R) Core(TM) i5 CPU       M 560  @ 2.67GHz
   slot: None
   size: 1199MHz
   capacity: 1199MHz
   width: 64 bits
   clock: 133MHz
   capabilities: fpu fpu_exception wp vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx rdtscp x86-64 constant_tsc arch_perfmon pebs bts rep_good xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm sse4_1 sse4_2 popcnt aes lahf_lm ida arat tpr_shadow vnmi flexpriority ept vpid cpufreq
   configuration: cores=2 enabledcores=2 threads=4

If you want to determine your kernel properties:

Use solution of Maxime and use either the uname -m or lscpu.

Community
  • 1
  • 1
Zegar
  • 1,005
  • 10
  • 18
  • I'm wondering: if a 64 bit CPU is running on a 32 bit kernel, will the flags remains the same? – Maxime Chéramy Nov 16 '13 at 11:06
  • Well i'm not exactly sure but I suppose that they'll remains the same. `lshw` seems to be an adequate tool also. – Zegar Nov 16 '13 at 11:11
  • I'm a bit confused, what does "width: 64 bits" means on your example of 32 bits kernel? That the processor is 64 bits but in the capabilities it's only 32? – Maxime Chéramy Nov 16 '13 at 11:19
  • Width is exactly the address width of particular device (logicalcpu). – Zegar Nov 16 '13 at 11:23
  • And if they send you a parameter fon another function, can you notice with that parameter if the CPU is running in 64 bits or 32? – ProgramDisciple Nov 16 '13 at 15:02
0

Using uname:

You can check whether your processor is 64bits or not using uname:

uname -m

Example on my system:

x86_64

And with the option -r, you can check if the kernel is 32 or 64 bits:

 % uname -r
 3.2.0-4-amd64

Using lscpu:

You will be able to know the supported modes but not the used mode:

% lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
...

Other solutions:

As Zegar suggested in his answer, you can also use grep flags /proc/cpuinfo and look for the lm flag. Or use lshw.

Community
  • 1
  • 1
Maxime Chéramy
  • 17,761
  • 8
  • 54
  • 75