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