mman.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __ASM_MMAN_H__
  3. #define __ASM_MMAN_H__
  4. #include <uapi/asm/mman.h>
  5. #ifndef BUILD_VDSO
  6. #include <linux/compiler.h>
  7. #include <linux/fs.h>
  8. #include <linux/hugetlb.h>
  9. #include <linux/shmem_fs.h>
  10. #include <linux/types.h>
  11. static inline vm_flags_t arch_calc_vm_prot_bits(unsigned long prot,
  12. unsigned long pkey)
  13. {
  14. vm_flags_t ret = 0;
  15. if (system_supports_bti() && (prot & PROT_BTI))
  16. ret |= VM_ARM64_BTI;
  17. if (system_supports_mte() && (prot & PROT_MTE))
  18. ret |= VM_MTE;
  19. #ifdef CONFIG_ARCH_HAS_PKEYS
  20. if (system_supports_poe()) {
  21. ret |= pkey & BIT(0) ? VM_PKEY_BIT0 : 0;
  22. ret |= pkey & BIT(1) ? VM_PKEY_BIT1 : 0;
  23. ret |= pkey & BIT(2) ? VM_PKEY_BIT2 : 0;
  24. }
  25. #endif
  26. return ret;
  27. }
  28. #define arch_calc_vm_prot_bits(prot, pkey) arch_calc_vm_prot_bits(prot, pkey)
  29. static inline vm_flags_t arch_calc_vm_flag_bits(struct file *file,
  30. unsigned long flags)
  31. {
  32. /*
  33. * Only allow MTE on anonymous mappings as these are guaranteed to be
  34. * backed by tags-capable memory. The vm_flags may be overridden by a
  35. * filesystem supporting MTE (RAM-based).
  36. */
  37. if (system_supports_mte()) {
  38. if (flags & (MAP_ANONYMOUS | MAP_HUGETLB))
  39. return VM_MTE_ALLOWED;
  40. if (shmem_file(file) || is_file_hugepages(file))
  41. return VM_MTE_ALLOWED;
  42. }
  43. return 0;
  44. }
  45. #define arch_calc_vm_flag_bits(file, flags) arch_calc_vm_flag_bits(file, flags)
  46. static inline bool arch_validate_prot(unsigned long prot,
  47. unsigned long addr __always_unused)
  48. {
  49. unsigned long supported = PROT_READ | PROT_WRITE | PROT_EXEC | PROT_SEM;
  50. if (system_supports_bti())
  51. supported |= PROT_BTI;
  52. if (system_supports_mte())
  53. supported |= PROT_MTE;
  54. return (prot & ~supported) == 0;
  55. }
  56. #define arch_validate_prot(prot, addr) arch_validate_prot(prot, addr)
  57. static inline bool arch_validate_flags(vm_flags_t vm_flags)
  58. {
  59. if (system_supports_mte()) {
  60. /*
  61. * only allow VM_MTE if VM_MTE_ALLOWED has been set
  62. * previously
  63. */
  64. if ((vm_flags & VM_MTE) && !(vm_flags & VM_MTE_ALLOWED))
  65. return false;
  66. }
  67. if (system_supports_gcs() && (vm_flags & VM_SHADOW_STACK)) {
  68. /* An executable GCS isn't a good idea. */
  69. if (vm_flags & VM_EXEC)
  70. return false;
  71. /* The memory management core should prevent this */
  72. VM_WARN_ON(vm_flags & VM_SHARED);
  73. }
  74. return true;
  75. }
  76. #define arch_validate_flags(vm_flags) arch_validate_flags(vm_flags)
  77. #endif /* !BUILD_VDSO */
  78. #endif /* ! __ASM_MMAN_H__ */