Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use some bits of pointer (x86_64) for custom data? And how if 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 pointer size is 64bits. 48bits are used to calculate physical address, and there are 16bits unused.

Can these free 16 bits be used safely?

If yes, then which are they? And how can they be used? Do I always have to bitmask the address, or something else?

Disclaimer: I'm designing low level conventions for programming language that I am gonna make. I need to pass an extra tiny information with some pointers and I would like to squeeze it into pointer if possible.

like image 550
kravemir Avatar asked Jul 20 '15 17:07

kravemir


1 Answers

In current 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 off before you dereference a pointer and you will be fine.

In every OS I'm familiar with, bit 47 is 0 for user mode, so any user mode pointer will have the most-significant 17 bits be 0. This means a simple bit mask operation will turn your custom data into a pointer. If your pointers will be 8-byte aligned, you have an additional 3 low bits that you can use, giving you 20 free bits to do with as you please.

If you don't know whether your pointers will have their high bit set, 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 absolutely safe to use the otherwise-unused bits in pointer. There are just two rules you need to follow:

  1. Never use more bits than allowed. If the OS says 48 bits virtual, that means you can only use the high 16 bits. If some day a new CPU makes it say 50 bits virtual, you would only have 14 bits.

  2. Always produce a canonical pointer when dereferencing. That means the highest 16 bits must all be identical to the 17th bit. If you have 50 bits virtual, you have to make sure the highest 14 bits are identical to the 15th highest bit.

like image 194
Gabe Avatar answered Sep 30 '22 02:09

Gabe