linuxソース分析のビット長定義--bitsperlong.h

1852 ワード

Linuxカーネルでは,異なるCPUでは異なるCPUのバイトシーケンス定義が異なることが知られている.
本節の年の内容は主に、CPUによって、それぞれのビット長の定義が異なるということです.
今回分析に使用するLinuxカーネルのバージョンは、linux--3.0です.0-12.
 arch/XXX/include/asm/bitsperlong.h:異なるCPU(XXX)のビット長定義
1)ARM(XXX=arm):
 #include 

(2)PowerPC(XXX=powerpc)
 #ifndef __ASM_POWERPC_BITSPERLONG_H
 #define __ASM_POWERPC_BITSPERLONG_H
   
 #if defined(__powerpc64__)
 # define __BITS_PER_LONG 64
 #else
 # define __BITS_PER_LONG 32
 #endif
   
 #include 
 
 #endif /* __ASM_POWERPC_BITSPERLONG_H */

(3)X86(XXX=x86)
#ifndef __ASM_X86_BITSPERLONG_H
#define __ASM_X86_BITSPERLONG_H
   
#ifdef __x86_64__
# define __BITS_PER_LONG 64
#else
# define __BITS_PER_LONG 32
#endif
   
#include 
 
#endif /* __ASM_X86_BITSPERLONG_H */

以上の3つの例から,3つの異なるCPUがそれぞれのビット長定義に対して異なることが分かる.
次にasm-generic/bitsperlongを見てみましょう.h,ソースコードは以下の通りである.
#ifndef __ASM_GENERIC_BITS_PER_LONG
#define __ASM_GENERIC_BITS_PER_LONG
   
/*
 * There seems to be no way of detecting this automatically from user
 * space, so 64 bit architectures should override this in their
 * bitsperlong.h. In particular, an architecture that supports
 * both 32 and 64 bit user space must not rely on CONFIG_64BIT
 * to decide it, but rather check a compiler provided macro.
 */
#ifndef __BITS_PER_LONG
#define __BITS_PER_LONG 32
#endif
 
#endif /* __ASM_GENERIC_BITS_PER_LONG */

このヘッダファイルの説明:32ビットと64ビットのユーザースペースをサポートするアーキテクチャは、CONFIGに依存することはできません.64 BITはビット長を決定するのではなく、コンパイラが提供するマクロをチェックする.
まとめ:arch/XXX/include/asm/bitsperlongを先にチェックする.h、CPUがビット長を定義か否かを見て、定義がない場合はasm-generic/bitsperlongに従う.hで決める.
(この例はarch/XXX/include/asm/bitsperlong.hに定義されていない限り、asm-generic/bitsperlong.hはビット長32を定義する)