mpx-hw.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #ifndef _MPX_HW_H
  2. #define _MPX_HW_H
  3. #include <assert.h>
  4. /* Describe the MPX Hardware Layout in here */
  5. #define NR_MPX_BOUNDS_REGISTERS 4
  6. #ifdef __i386__
  7. #define MPX_BOUNDS_TABLE_ENTRY_SIZE_BYTES 16 /* 4 * 32-bits */
  8. #define MPX_BOUNDS_TABLE_SIZE_BYTES (1ULL << 14) /* 16k */
  9. #define MPX_BOUNDS_DIR_ENTRY_SIZE_BYTES 4
  10. #define MPX_BOUNDS_DIR_SIZE_BYTES (1ULL << 22) /* 4MB */
  11. #define MPX_BOUNDS_TABLE_BOTTOM_BIT 2
  12. #define MPX_BOUNDS_TABLE_TOP_BIT 11
  13. #define MPX_BOUNDS_DIR_BOTTOM_BIT 12
  14. #define MPX_BOUNDS_DIR_TOP_BIT 31
  15. #else
  16. /*
  17. * Linear Address of "pointer" (LAp)
  18. * 0 -> 2: ignored
  19. * 3 -> 19: index in to bounds table
  20. * 20 -> 47: index in to bounds directory
  21. * 48 -> 63: ignored
  22. */
  23. #define MPX_BOUNDS_TABLE_ENTRY_SIZE_BYTES 32
  24. #define MPX_BOUNDS_TABLE_SIZE_BYTES (1ULL << 22) /* 4MB */
  25. #define MPX_BOUNDS_DIR_ENTRY_SIZE_BYTES 8
  26. #define MPX_BOUNDS_DIR_SIZE_BYTES (1ULL << 31) /* 2GB */
  27. #define MPX_BOUNDS_TABLE_BOTTOM_BIT 3
  28. #define MPX_BOUNDS_TABLE_TOP_BIT 19
  29. #define MPX_BOUNDS_DIR_BOTTOM_BIT 20
  30. #define MPX_BOUNDS_DIR_TOP_BIT 47
  31. #endif
  32. #define MPX_BOUNDS_DIR_NR_ENTRIES \
  33. (MPX_BOUNDS_DIR_SIZE_BYTES/MPX_BOUNDS_DIR_ENTRY_SIZE_BYTES)
  34. #define MPX_BOUNDS_TABLE_NR_ENTRIES \
  35. (MPX_BOUNDS_TABLE_SIZE_BYTES/MPX_BOUNDS_TABLE_ENTRY_SIZE_BYTES)
  36. #define MPX_BOUNDS_TABLE_ENTRY_VALID_BIT 0x1
  37. struct mpx_bd_entry {
  38. union {
  39. char x[MPX_BOUNDS_DIR_ENTRY_SIZE_BYTES];
  40. void *contents[1];
  41. };
  42. } __attribute__((packed));
  43. struct mpx_bt_entry {
  44. union {
  45. char x[MPX_BOUNDS_TABLE_ENTRY_SIZE_BYTES];
  46. unsigned long contents[1];
  47. };
  48. } __attribute__((packed));
  49. struct mpx_bounds_dir {
  50. struct mpx_bd_entry entries[MPX_BOUNDS_DIR_NR_ENTRIES];
  51. } __attribute__((packed));
  52. struct mpx_bounds_table {
  53. struct mpx_bt_entry entries[MPX_BOUNDS_TABLE_NR_ENTRIES];
  54. } __attribute__((packed));
  55. static inline unsigned long GET_BITS(unsigned long val, int bottombit, int topbit)
  56. {
  57. int total_nr_bits = topbit - bottombit;
  58. unsigned long mask = (1UL << total_nr_bits)-1;
  59. return (val >> bottombit) & mask;
  60. }
  61. static inline unsigned long __vaddr_bounds_table_index(void *vaddr)
  62. {
  63. return GET_BITS((unsigned long)vaddr, MPX_BOUNDS_TABLE_BOTTOM_BIT,
  64. MPX_BOUNDS_TABLE_TOP_BIT);
  65. }
  66. static inline unsigned long __vaddr_bounds_directory_index(void *vaddr)
  67. {
  68. return GET_BITS((unsigned long)vaddr, MPX_BOUNDS_DIR_BOTTOM_BIT,
  69. MPX_BOUNDS_DIR_TOP_BIT);
  70. }
  71. static inline struct mpx_bd_entry *mpx_vaddr_to_bd_entry(void *vaddr,
  72. struct mpx_bounds_dir *bounds_dir)
  73. {
  74. unsigned long index = __vaddr_bounds_directory_index(vaddr);
  75. return &bounds_dir->entries[index];
  76. }
  77. static inline int bd_entry_valid(struct mpx_bd_entry *bounds_dir_entry)
  78. {
  79. unsigned long __bd_entry = (unsigned long)bounds_dir_entry->contents;
  80. return (__bd_entry & MPX_BOUNDS_TABLE_ENTRY_VALID_BIT);
  81. }
  82. static inline struct mpx_bounds_table *
  83. __bd_entry_to_bounds_table(struct mpx_bd_entry *bounds_dir_entry)
  84. {
  85. unsigned long __bd_entry = (unsigned long)bounds_dir_entry->contents;
  86. assert(__bd_entry & MPX_BOUNDS_TABLE_ENTRY_VALID_BIT);
  87. __bd_entry &= ~MPX_BOUNDS_TABLE_ENTRY_VALID_BIT;
  88. return (struct mpx_bounds_table *)__bd_entry;
  89. }
  90. static inline struct mpx_bt_entry *
  91. mpx_vaddr_to_bt_entry(void *vaddr, struct mpx_bounds_dir *bounds_dir)
  92. {
  93. struct mpx_bd_entry *bde = mpx_vaddr_to_bd_entry(vaddr, bounds_dir);
  94. struct mpx_bounds_table *bt = __bd_entry_to_bounds_table(bde);
  95. unsigned long index = __vaddr_bounds_table_index(vaddr);
  96. return &bt->entries[index];
  97. }
  98. #endif /* _MPX_HW_H */