pgprot.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/export.h>
  3. #include <linux/mm.h>
  4. #include <asm/pgtable.h>
  5. #include <asm/mem_encrypt.h>
  6. static pgprot_t protection_map[16] __ro_after_init = {
  7. [VM_NONE] = PAGE_NONE,
  8. [VM_READ] = PAGE_READONLY,
  9. [VM_WRITE] = PAGE_COPY,
  10. [VM_WRITE | VM_READ] = PAGE_COPY,
  11. [VM_EXEC] = PAGE_READONLY_EXEC,
  12. [VM_EXEC | VM_READ] = PAGE_READONLY_EXEC,
  13. [VM_EXEC | VM_WRITE] = PAGE_COPY_EXEC,
  14. [VM_EXEC | VM_WRITE | VM_READ] = PAGE_COPY_EXEC,
  15. [VM_SHARED] = PAGE_NONE,
  16. [VM_SHARED | VM_READ] = PAGE_READONLY,
  17. [VM_SHARED | VM_WRITE] = PAGE_SHARED,
  18. [VM_SHARED | VM_WRITE | VM_READ] = PAGE_SHARED,
  19. [VM_SHARED | VM_EXEC] = PAGE_READONLY_EXEC,
  20. [VM_SHARED | VM_EXEC | VM_READ] = PAGE_READONLY_EXEC,
  21. [VM_SHARED | VM_EXEC | VM_WRITE] = PAGE_SHARED_EXEC,
  22. [VM_SHARED | VM_EXEC | VM_WRITE | VM_READ] = PAGE_SHARED_EXEC
  23. };
  24. void add_encrypt_protection_map(void)
  25. {
  26. unsigned int i;
  27. for (i = 0; i < ARRAY_SIZE(protection_map); i++)
  28. protection_map[i] = pgprot_encrypted(protection_map[i]);
  29. }
  30. pgprot_t vm_get_page_prot(unsigned long vm_flags)
  31. {
  32. unsigned long val = pgprot_val(protection_map[vm_flags &
  33. (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)]);
  34. #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
  35. /*
  36. * Take the 4 protection key bits out of the vma->vm_flags value and
  37. * turn them in to the bits that we can put in to a pte.
  38. *
  39. * Only override these if Protection Keys are available (which is only
  40. * on 64-bit).
  41. */
  42. if (vm_flags & VM_PKEY_BIT0)
  43. val |= _PAGE_PKEY_BIT0;
  44. if (vm_flags & VM_PKEY_BIT1)
  45. val |= _PAGE_PKEY_BIT1;
  46. if (vm_flags & VM_PKEY_BIT2)
  47. val |= _PAGE_PKEY_BIT2;
  48. if (vm_flags & VM_PKEY_BIT3)
  49. val |= _PAGE_PKEY_BIT3;
  50. #endif
  51. val = __sme_set(val);
  52. if (val & _PAGE_PRESENT)
  53. val &= __supported_pte_mask;
  54. return __pgprot(val);
  55. }
  56. EXPORT_SYMBOL(vm_get_page_prot);