ArchInfo.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #ifndef ArchInfo_H
  16. #define ArchInfo_H
  17. #include "memory/Allocator.h"
  18. #include "util/Linker.h"
  19. Linker_require("util/ArchInfo.c")
  20. #include <stdint.h>
  21. enum ArchInfo {
  22. ArchInfo_UNKNOWN = -1
  23. };
  24. /**
  25. * ArchInfo enum (not really an enum)
  26. * NOTE: Value is in host endian order.
  27. *
  28. * 1 2 3
  29. * 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
  30. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  31. * | Architecture Specific | Res |B| Bits | Arch |
  32. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  33. *
  34. * Arch: The type of processor (enum ArchInfo_Arch
  35. * B: 1 if the architecture is Big Endian
  36. * Bits: Number of bits in the architecture (8 times 2**bits)
  37. * Res: Reserved
  38. * Architecture Specific: see enum Arch_<your architecture> for details.
  39. */
  40. #define ArchInfo_Arch_SHIFT 0
  41. #define ArchInfo_Arch_BITS (0xff << ArchInfo_Arch_SHIFT)
  42. enum ArchInfo_Arch {
  43. ArchInfo_Arch_AMD64,
  44. ArchInfo_Arch_I386,
  45. ArchInfo_Arch_ARM,
  46. ArchInfo_Arch_PPC,
  47. ArchInfo_Arch_SPARC,
  48. ArchInfo_Arch_MIPS,
  49. ArchInfo_Arch_UNKNOWN = -1
  50. };
  51. static inline enum ArchInfo_Arch ArchInfo_getArch(enum ArchInfo ai)
  52. {
  53. return ai & ArchInfo_Arch_BITS;
  54. }
  55. #define ArchInfo_Bits_SHIFT 8
  56. #define ArchInfo_Bits_BITS (0xf << ArchInfo_Bits_SHIFT)
  57. enum ArchInfo_Bits
  58. {
  59. ArchInfo_Bits_8 = (1 << ArchInfo_Bits_SHIFT),
  60. ArchInfo_Bits_16 = (2 << ArchInfo_Bits_SHIFT),
  61. ArchInfo_Bits_32 = (3 << ArchInfo_Bits_SHIFT),
  62. ArchInfo_Bits_64 = (4 << ArchInfo_Bits_SHIFT),
  63. ArchInfo_Bits_128 = (5 << ArchInfo_Bits_SHIFT),
  64. ArchInfo_Bits_UNKNOWN = -1
  65. };
  66. static inline int ArchInfo_getBits(enum ArchInfo ai)
  67. {
  68. switch (ai & ArchInfo_Bits_BITS) {
  69. case ArchInfo_Bits_16: return 16;
  70. case ArchInfo_Bits_32: return 32;
  71. case ArchInfo_Bits_64: return 64;
  72. case ArchInfo_Bits_128: return 128;
  73. default: return -1;
  74. }
  75. }
  76. #define ArchInfo_Endian_SHIFT 12
  77. #define ArchInfo_Endian_BITS (1 << ArchInfo_Endian_SHIFT)
  78. static inline int ArchInfo_isBigEndian(enum ArchInfo ai)
  79. {
  80. return !!(ai & ArchInfo_Endian_BITS);
  81. }
  82. #define ArchInfo_ARCH_SPECIFIC_SHIFT 16
  83. #define ArchInfo_ARCH_SPECIFIC_BITS (0xffff << ArchInfo_ARCH_SPECIFIC_SHIFT)
  84. #define ArchInfo_Arm_SUBTYPE_BITS (4 << ArchInfo_ARCH_SPECIFIC_SHIFT)
  85. enum ArchInfo_Arm
  86. {
  87. ArchInfo_Arm_EABI = (1 << ArchInfo_ARCH_SPECIFIC_SHIFT),
  88. ArchInfo_Arm_NEON = (1 << (ArchInfo_ARCH_SPECIFIC_SHIFT+15))
  89. };
  90. #define ArchInfo_Mips_SUBTYPE_BITS (4 << ArchInfo_ARCH_SPECIFIC_SHIFT)
  91. enum ArchInfo_Mips
  92. {
  93. ArchInfo_Mips_OABI = (1 << ArchInfo_ARCH_SPECIFIC_SHIFT),
  94. ArchInfo_Mips_NABI = (2 << ArchInfo_ARCH_SPECIFIC_SHIFT)
  95. };
  96. #define ArchInfo_I386_SUBTYPE_BITS (4 << ArchInfo_ARCH_SPECIFIC_SHIFT)
  97. enum ArchInfo_I386
  98. {
  99. ArchInfo_I386_SSE2 = (1 << (ArchInfo_ARCH_SPECIFIC_SHIFT+15))
  100. };
  101. #define ArchInfo_Sparc_SUBTYPE_BITS (4 << ArchInfo_ARCH_SPECIFIC_SHIFT)
  102. enum ArchInfo_Sparc
  103. {
  104. ArchInfo_Sparc_v8 = (1 << ArchInfo_ARCH_SPECIFIC_SHIFT),
  105. ArchInfo_Sparc_v9 = (2 << ArchInfo_ARCH_SPECIFIC_SHIFT)
  106. };
  107. /**
  108. * Convert the architecture to a linux/audit.h AUDIT_ARCH_* number.
  109. * Returns 0xffffffff in case of error or on a non-linux system.
  110. */
  111. uint32_t ArchInfo_toAuditArch(enum ArchInfo ai);
  112. /** Describe the architecture as a string. */
  113. char* ArchInfo_describe(enum ArchInfo ai, struct Allocator* alloc);
  114. /** Detect the architecture. */
  115. enum ArchInfo ArchInfo_detect();
  116. #endif