~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/arch/powerpc/include/asm/pkeys.h

Version: ~ [ linux-5.9-rc6 ] ~ [ linux-5.8.10 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.66 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.146 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.198 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.236 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.236 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /* SPDX-License-Identifier: GPL-2.0+ */
  2 /*
  3  * PowerPC Memory Protection Keys management
  4  *
  5  * Copyright 2017, Ram Pai, IBM Corporation.
  6  */
  7 
  8 #ifndef _ASM_POWERPC_KEYS_H
  9 #define _ASM_POWERPC_KEYS_H
 10 
 11 #include <linux/jump_label.h>
 12 #include <asm/firmware.h>
 13 
 14 DECLARE_STATIC_KEY_TRUE(pkey_disabled);
 15 extern int pkeys_total; /* total pkeys as per device tree */
 16 extern u32 initial_allocation_mask; /* bits set for reserved keys */
 17 
 18 /*
 19  * Define these here temporarily so we're not dependent on patching linux/mm.h.
 20  * Once it's updated we can drop these.
 21  */
 22 #ifndef VM_PKEY_BIT0
 23 # define VM_PKEY_SHIFT  VM_HIGH_ARCH_BIT_0
 24 # define VM_PKEY_BIT0   VM_HIGH_ARCH_0
 25 # define VM_PKEY_BIT1   VM_HIGH_ARCH_1
 26 # define VM_PKEY_BIT2   VM_HIGH_ARCH_2
 27 # define VM_PKEY_BIT3   VM_HIGH_ARCH_3
 28 # define VM_PKEY_BIT4   VM_HIGH_ARCH_4
 29 #elif !defined(VM_PKEY_BIT4)
 30 # define VM_PKEY_BIT4   VM_HIGH_ARCH_4
 31 #endif
 32 
 33 #define ARCH_VM_PKEY_FLAGS (VM_PKEY_BIT0 | VM_PKEY_BIT1 | VM_PKEY_BIT2 | \
 34                             VM_PKEY_BIT3 | VM_PKEY_BIT4)
 35 
 36 /* Override any generic PKEY permission defines */
 37 #define PKEY_DISABLE_EXECUTE   0x4
 38 #define PKEY_ACCESS_MASK       (PKEY_DISABLE_ACCESS | \
 39                                 PKEY_DISABLE_WRITE  | \
 40                                 PKEY_DISABLE_EXECUTE)
 41 
 42 static inline u64 pkey_to_vmflag_bits(u16 pkey)
 43 {
 44         return (((u64)pkey << VM_PKEY_SHIFT) & ARCH_VM_PKEY_FLAGS);
 45 }
 46 
 47 static inline u64 vmflag_to_pte_pkey_bits(u64 vm_flags)
 48 {
 49         if (static_branch_likely(&pkey_disabled))
 50                 return 0x0UL;
 51 
 52         return (((vm_flags & VM_PKEY_BIT0) ? H_PTE_PKEY_BIT4 : 0x0UL) |
 53                 ((vm_flags & VM_PKEY_BIT1) ? H_PTE_PKEY_BIT3 : 0x0UL) |
 54                 ((vm_flags & VM_PKEY_BIT2) ? H_PTE_PKEY_BIT2 : 0x0UL) |
 55                 ((vm_flags & VM_PKEY_BIT3) ? H_PTE_PKEY_BIT1 : 0x0UL) |
 56                 ((vm_flags & VM_PKEY_BIT4) ? H_PTE_PKEY_BIT0 : 0x0UL));
 57 }
 58 
 59 static inline int vma_pkey(struct vm_area_struct *vma)
 60 {
 61         if (static_branch_likely(&pkey_disabled))
 62                 return 0;
 63         return (vma->vm_flags & ARCH_VM_PKEY_FLAGS) >> VM_PKEY_SHIFT;
 64 }
 65 
 66 #define arch_max_pkey() pkeys_total
 67 
 68 static inline u64 pte_to_hpte_pkey_bits(u64 pteflags)
 69 {
 70         return (((pteflags & H_PTE_PKEY_BIT0) ? HPTE_R_KEY_BIT0 : 0x0UL) |
 71                 ((pteflags & H_PTE_PKEY_BIT1) ? HPTE_R_KEY_BIT1 : 0x0UL) |
 72                 ((pteflags & H_PTE_PKEY_BIT2) ? HPTE_R_KEY_BIT2 : 0x0UL) |
 73                 ((pteflags & H_PTE_PKEY_BIT3) ? HPTE_R_KEY_BIT3 : 0x0UL) |
 74                 ((pteflags & H_PTE_PKEY_BIT4) ? HPTE_R_KEY_BIT4 : 0x0UL));
 75 }
 76 
 77 static inline u16 pte_to_pkey_bits(u64 pteflags)
 78 {
 79         return (((pteflags & H_PTE_PKEY_BIT0) ? 0x10 : 0x0UL) |
 80                 ((pteflags & H_PTE_PKEY_BIT1) ? 0x8 : 0x0UL) |
 81                 ((pteflags & H_PTE_PKEY_BIT2) ? 0x4 : 0x0UL) |
 82                 ((pteflags & H_PTE_PKEY_BIT3) ? 0x2 : 0x0UL) |
 83                 ((pteflags & H_PTE_PKEY_BIT4) ? 0x1 : 0x0UL));
 84 }
 85 
 86 #define pkey_alloc_mask(pkey) (0x1 << pkey)
 87 
 88 #define mm_pkey_allocation_map(mm) (mm->context.pkey_allocation_map)
 89 
 90 #define __mm_pkey_allocated(mm, pkey) { \
 91         mm_pkey_allocation_map(mm) |= pkey_alloc_mask(pkey); \
 92 }
 93 
 94 #define __mm_pkey_free(mm, pkey) {      \
 95         mm_pkey_allocation_map(mm) &= ~pkey_alloc_mask(pkey);   \
 96 }
 97 
 98 #define __mm_pkey_is_allocated(mm, pkey)        \
 99         (mm_pkey_allocation_map(mm) & pkey_alloc_mask(pkey))
100 
101 #define __mm_pkey_is_reserved(pkey) (initial_allocation_mask & \
102                                        pkey_alloc_mask(pkey))
103 
104 static inline bool mm_pkey_is_allocated(struct mm_struct *mm, int pkey)
105 {
106         /* A reserved key is never considered as 'explicitly allocated' */
107         return ((pkey < arch_max_pkey()) &&
108                 !__mm_pkey_is_reserved(pkey) &&
109                 __mm_pkey_is_allocated(mm, pkey));
110 }
111 
112 extern void __arch_activate_pkey(int pkey);
113 extern void __arch_deactivate_pkey(int pkey);
114 /*
115  * Returns a positive, 5-bit key on success, or -1 on failure.
116  * Relies on the mmap_sem to protect against concurrency in mm_pkey_alloc() and
117  * mm_pkey_free().
118  */
119 static inline int mm_pkey_alloc(struct mm_struct *mm)
120 {
121         /*
122          * Note: this is the one and only place we make sure that the pkey is
123          * valid as far as the hardware is concerned. The rest of the kernel
124          * trusts that only good, valid pkeys come out of here.
125          */
126         u32 all_pkeys_mask = (u32)(~(0x0));
127         int ret;
128 
129         if (static_branch_likely(&pkey_disabled))
130                 return -1;
131 
132         /*
133          * Are we out of pkeys? We must handle this specially because ffz()
134          * behavior is undefined if there are no zeros.
135          */
136         if (mm_pkey_allocation_map(mm) == all_pkeys_mask)
137                 return -1;
138 
139         ret = ffz((u32)mm_pkey_allocation_map(mm));
140         __mm_pkey_allocated(mm, ret);
141 
142         /*
143          * Enable the key in the hardware
144          */
145         if (ret > 0)
146                 __arch_activate_pkey(ret);
147         return ret;
148 }
149 
150 static inline int mm_pkey_free(struct mm_struct *mm, int pkey)
151 {
152         if (static_branch_likely(&pkey_disabled))
153                 return -1;
154 
155         if (!mm_pkey_is_allocated(mm, pkey))
156                 return -EINVAL;
157 
158         /*
159          * Disable the key in the hardware
160          */
161         __arch_deactivate_pkey(pkey);
162         __mm_pkey_free(mm, pkey);
163 
164         return 0;
165 }
166 
167 /*
168  * Try to dedicate one of the protection keys to be used as an
169  * execute-only protection key.
170  */
171 extern int __execute_only_pkey(struct mm_struct *mm);
172 static inline int execute_only_pkey(struct mm_struct *mm)
173 {
174         if (static_branch_likely(&pkey_disabled))
175                 return -1;
176 
177         return __execute_only_pkey(mm);
178 }
179 
180 extern int __arch_override_mprotect_pkey(struct vm_area_struct *vma,
181                                          int prot, int pkey);
182 static inline int arch_override_mprotect_pkey(struct vm_area_struct *vma,
183                                               int prot, int pkey)
184 {
185         if (static_branch_likely(&pkey_disabled))
186                 return 0;
187 
188         /*
189          * Is this an mprotect_pkey() call? If so, never override the value that
190          * came from the user.
191          */
192         if (pkey != -1)
193                 return pkey;
194 
195         return __arch_override_mprotect_pkey(vma, prot, pkey);
196 }
197 
198 extern int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
199                                        unsigned long init_val);
200 static inline int arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
201                                             unsigned long init_val)
202 {
203         if (static_branch_likely(&pkey_disabled))
204                 return -EINVAL;
205         return __arch_set_user_pkey_access(tsk, pkey, init_val);
206 }
207 
208 static inline bool arch_pkeys_enabled(void)
209 {
210         return !static_branch_likely(&pkey_disabled);
211 }
212 
213 extern void pkey_mm_init(struct mm_struct *mm);
214 extern bool arch_supports_pkeys(int cap);
215 extern unsigned int arch_usable_pkeys(void);
216 extern void thread_pkey_regs_save(struct thread_struct *thread);
217 extern void thread_pkey_regs_restore(struct thread_struct *new_thread,
218                                      struct thread_struct *old_thread);
219 extern void thread_pkey_regs_init(struct thread_struct *thread);
220 #endif /*_ASM_POWERPC_KEYS_H */
221 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp