Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Dave Hansen | 66d3757 | 2016-02-12 13:02:32 -0800 | [diff] [blame] | 2 | #ifndef _ASM_X86_PKEYS_H |
| 3 | #define _ASM_X86_PKEYS_H |
| 4 | |
Dave Hansen | e8c24d3 | 2016-07-29 09:30:15 -0700 | [diff] [blame] | 5 | #define arch_max_pkey() (boot_cpu_has(X86_FEATURE_OSPKE) ? 16 : 1) |
Dave Hansen | 66d3757 | 2016-02-12 13:02:32 -0800 | [diff] [blame] | 6 | |
Dave Hansen | 8459429 | 2016-02-12 13:02:36 -0800 | [diff] [blame] | 7 | extern int arch_set_user_pkey_access(struct task_struct *tsk, int pkey, |
| 8 | unsigned long init_val); |
| 9 | |
Dave Hansen | 62b5f7d | 2016-02-12 13:02:40 -0800 | [diff] [blame] | 10 | /* |
| 11 | * Try to dedicate one of the protection keys to be used as an |
| 12 | * execute-only protection key. |
| 13 | */ |
Dave Hansen | 62b5f7d | 2016-02-12 13:02:40 -0800 | [diff] [blame] | 14 | extern int __execute_only_pkey(struct mm_struct *mm); |
| 15 | static inline int execute_only_pkey(struct mm_struct *mm) |
| 16 | { |
| 17 | if (!boot_cpu_has(X86_FEATURE_OSPKE)) |
| 18 | return 0; |
| 19 | |
| 20 | return __execute_only_pkey(mm); |
| 21 | } |
| 22 | |
| 23 | extern int __arch_override_mprotect_pkey(struct vm_area_struct *vma, |
| 24 | int prot, int pkey); |
| 25 | static inline int arch_override_mprotect_pkey(struct vm_area_struct *vma, |
| 26 | int prot, int pkey) |
| 27 | { |
| 28 | if (!boot_cpu_has(X86_FEATURE_OSPKE)) |
| 29 | return 0; |
| 30 | |
| 31 | return __arch_override_mprotect_pkey(vma, prot, pkey); |
| 32 | } |
| 33 | |
Dave Hansen | 7d06d9c | 2016-07-29 09:30:12 -0700 | [diff] [blame] | 34 | extern int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey, |
| 35 | unsigned long init_val); |
| 36 | |
Dave Hansen | a8502b6 | 2016-07-29 09:30:13 -0700 | [diff] [blame] | 37 | #define ARCH_VM_PKEY_FLAGS (VM_PKEY_BIT0 | VM_PKEY_BIT1 | VM_PKEY_BIT2 | VM_PKEY_BIT3) |
| 38 | |
Dave Hansen | e8c24d3 | 2016-07-29 09:30:15 -0700 | [diff] [blame] | 39 | #define mm_pkey_allocation_map(mm) (mm->context.pkey_allocation_map) |
| 40 | #define mm_set_pkey_allocated(mm, pkey) do { \ |
| 41 | mm_pkey_allocation_map(mm) |= (1U << pkey); \ |
| 42 | } while (0) |
| 43 | #define mm_set_pkey_free(mm, pkey) do { \ |
| 44 | mm_pkey_allocation_map(mm) &= ~(1U << pkey); \ |
| 45 | } while (0) |
| 46 | |
| 47 | static inline |
| 48 | bool mm_pkey_is_allocated(struct mm_struct *mm, int pkey) |
| 49 | { |
Dave Hansen | 58ab9a0 | 2017-02-23 14:26:03 -0800 | [diff] [blame] | 50 | /* |
| 51 | * "Allocated" pkeys are those that have been returned |
| 52 | * from pkey_alloc(). pkey 0 is special, and never |
| 53 | * returned from pkey_alloc(). |
| 54 | */ |
| 55 | if (pkey <= 0) |
| 56 | return false; |
| 57 | if (pkey >= arch_max_pkey()) |
| 58 | return false; |
Dave Hansen | e8c24d3 | 2016-07-29 09:30:15 -0700 | [diff] [blame] | 59 | return mm_pkey_allocation_map(mm) & (1U << pkey); |
| 60 | } |
| 61 | |
| 62 | /* |
| 63 | * Returns a positive, 4-bit key on success, or -1 on failure. |
| 64 | */ |
| 65 | static inline |
| 66 | int mm_pkey_alloc(struct mm_struct *mm) |
| 67 | { |
| 68 | /* |
| 69 | * Note: this is the one and only place we make sure |
| 70 | * that the pkey is valid as far as the hardware is |
| 71 | * concerned. The rest of the kernel trusts that |
| 72 | * only good, valid pkeys come out of here. |
| 73 | */ |
| 74 | u16 all_pkeys_mask = ((1U << arch_max_pkey()) - 1); |
| 75 | int ret; |
| 76 | |
| 77 | /* |
| 78 | * Are we out of pkeys? We must handle this specially |
| 79 | * because ffz() behavior is undefined if there are no |
| 80 | * zeros. |
| 81 | */ |
| 82 | if (mm_pkey_allocation_map(mm) == all_pkeys_mask) |
| 83 | return -1; |
| 84 | |
| 85 | ret = ffz(mm_pkey_allocation_map(mm)); |
| 86 | |
| 87 | mm_set_pkey_allocated(mm, ret); |
| 88 | |
| 89 | return ret; |
| 90 | } |
| 91 | |
| 92 | static inline |
| 93 | int mm_pkey_free(struct mm_struct *mm, int pkey) |
| 94 | { |
Dave Hansen | e8c24d3 | 2016-07-29 09:30:15 -0700 | [diff] [blame] | 95 | if (!mm_pkey_is_allocated(mm, pkey)) |
| 96 | return -EINVAL; |
| 97 | |
| 98 | mm_set_pkey_free(mm, pkey); |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | extern int arch_set_user_pkey_access(struct task_struct *tsk, int pkey, |
| 104 | unsigned long init_val); |
| 105 | extern int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey, |
| 106 | unsigned long init_val); |
Dave Hansen | acd547b | 2016-07-29 09:30:21 -0700 | [diff] [blame] | 107 | extern void copy_init_pkru_to_fpregs(void); |
Dave Hansen | e8c24d3 | 2016-07-29 09:30:15 -0700 | [diff] [blame] | 108 | |
Dave Hansen | 66d3757 | 2016-02-12 13:02:32 -0800 | [diff] [blame] | 109 | #endif /*_ASM_X86_PKEYS_H */ |