简体   繁体   中英

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.

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++

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM