Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out if we are using really 48, 56 or 64 bits pointers

I am using tricks to store extra information in pointers, At the moment some bits are not used in pointers(the highest 16 bits), but this will change in the future. I would like to have a way to detect if we are compiling or running on a platform that will use more than 48 bits for pointers.

related things: Why can't OS use entire 64-bits for addressing? Why only the 48-bits? http://developer.amd.com/wordpress/media/2012/10/24593_APM_v2.pdf

The solution is needed for x86-64, Windows, C/C++, preferably something that can be done compile-time. Solutions for other platforms are also of interest but will not marked as correct answer.

like image 262
Jim Hansson Avatar asked Oct 28 '25 04:10

Jim Hansson


1 Answers

Windows has exactly one switch for 32bit and 64bit programs to determine the top of their virtual-address-space:

IMAGE_FILE_LARGE_ADDRESS_AWARE

For both types, omitting it limits the program to the lower 2 GB of address-space, severely reducing the memory an application can map and thus also reducing effectiveness of Address-Space-Layout-Randomization (ASLR, an attack mitigation mechanism).

There is one upside to it though, and just what you seem to want: Only the lower 31 bits of a pointer can be set, so pointers can be safely round-tripped through int (32 bit integer, sign- or zero-extension).


At run-time the situation is slightly better:

Just use the cpuid-instruction from intel, function EAX=80000008H, and read the maximum number of used address bits for virtual addresses from bits 8-15.

The OS cannot use more than the CPU supports, remember that intel insists on canonical addresses (sign-extended).

See here for how to use cpuid from C++: CPUID implementations in C++

like image 199
Deduplicator Avatar answered Oct 30 '25 22:10

Deduplicator



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!