我可以使用一些位指针(x86_64)来定制数据吗?如果可以的话,如何使用?

问题描述 投票:3回答:2

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. ...$ cat /proc/cpuinfo | grep addressIn 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.

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

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:

Never

use more bits than allowed. If the OS says
pointers x86-64
2个回答
5
投票

Always produce a canonical pointer when dereferencing. That means the highest 16 bits must all be identical to the 17th bit. If you have

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

  1. Architecture supports 64-bit addressing, but current CPUs do not. You can only use 48 bits for addressing, so no, you can't use those 16 bits. Of course we're talking about physical addresses here. For virtual addresses you can indeed use 64-bit addressing. 48 bits virtualIf yes, then which are they? And how can they be used? Do I always have to bitmask the address, or something else?50 bits virtualThey're the most significant ones. I don't know why you need to bitmask anything. Just don't use those bits.

  2. Related question 50 bits virtual:

所以,根据我的计算,指针大小是64bits。48bits用于计算物理地址,还有16bits没有使用。

-3
投票

如果可以,那么它们是哪些位?它们又该如何使用呢?我是否总是要对地址进行位掩码,或者其他什么?

声明:我正在为我要做的编程语言设计低级的约定。我需要用一些指针传递一个额外的小信息,如果可能的话,我想把它压缩到指针中。

从 $ cat proccpuinfo

© www.soinside.com 2019 - 2024. All rights reserved.