Can I use some pointer bits (x86_64) for user data? And how is this possible?

From $ cat /proc/cpuinfo | grep address

:

address sizes   : 39 bits physical, 48 bits virtual
address sizes   : 39 bits physical, 48 bits virtual

      

So, from my calculations, the pointer size is 64 bits. 48 bits are used to calculate the physical address and 16 bits are not used.

Can these free 16 bits be used safely?

If so, what are they? And how can they be used? Do I always need to beat the address mask or something else?

Disclaimer: I am developing low level conventions for the programming language I am about to make. I need to pass some extra tiny information with some pointers and I would like to compress it into a pointer if possible.

+3


source to share


2 answers


In modern architectures, the least significant 48 bits of pointers are used by the CPU, leaving you the 16 most significant bits to use as you wish. All you have to do is mask them before you can find the pointer and you should be fine.

On every OS I am familiar with, bit 47 is 0 for user mode, so any user mode pointer will have the most significant 17 bits at 0. This means that a simple bit mask operation will turn your user data into a pointer. If your pointers are 8 byte aligned, you have 3 more least significant bits that you can use, giving you 20 free bits you like.

If you don't know if your pointers will have their high bit, you can store the pointer in the most significant bits and do an arithmetic right shift to turn the custom value into a canonical pointer.



In other words, it is perfectly safe to use unused bits in the pointer. You only need two rules:

  • Never use more bits than allowed. If the OS says 48 bits virtual

    it means you can only use 16 bits. If some day a new processor makes it say 50 bits virtual

    , you only have 14 bits.

  • Always create a canonical pointer arbitrarily when dereferencing. This means that the maximum 16 bits must be identical to the 17th bit. If you have 50 bits virtual

    , you should make sure that the highest 14 bits are identical to the 15th highest bit.

+4


source


The architecture supports 64-bit addressing, but current processors do not. You can only use 48 bits for addressing, so no, you cannot use those 16 bits. Of course, we're talking about physical addresses here. For virtual addresses, you can actually use 64-bit addressing.

If so, what are they? And how can they be used? Do I always have to bitmask the address or something?



They are the most important. I don't know why you need to hit something. Just don't use these bits.

Related question

-2


source







All Articles