mpx-hw.h 3.4 KB

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