mem_encrypt.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Implementation of the memory encryption/decryption API.
  4. *
  5. * Amusingly, no crypto is actually performed. Rather, we call into the
  6. * hypervisor component of KVM to expose pages selectively to the host
  7. * for virtio "DMA" operations. In other words, "encrypted" pages are
  8. * not accessible to the host, whereas "decrypted" pages are.
  9. *
  10. * Author: Will Deacon <[email protected]>
  11. */
  12. #include <linux/arm-smccc.h>
  13. #include <linux/mem_encrypt.h>
  14. #include <linux/memory.h>
  15. #include <linux/mm.h>
  16. #include <linux/set_memory.h>
  17. #include <linux/types.h>
  18. #include <asm/hypervisor.h>
  19. #ifndef ARM_SMCCC_KVM_FUNC_HYP_MEMINFO
  20. #define ARM_SMCCC_KVM_FUNC_HYP_MEMINFO 2
  21. #define ARM_SMCCC_VENDOR_HYP_KVM_HYP_MEMINFO_FUNC_ID \
  22. ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \
  23. ARM_SMCCC_SMC_64, \
  24. ARM_SMCCC_OWNER_VENDOR_HYP, \
  25. ARM_SMCCC_KVM_FUNC_HYP_MEMINFO)
  26. #endif /* ARM_SMCCC_KVM_FUNC_HYP_MEMINFO */
  27. #ifndef ARM_SMCCC_KVM_FUNC_MEM_SHARE
  28. #define ARM_SMCCC_KVM_FUNC_MEM_SHARE 3
  29. #define ARM_SMCCC_VENDOR_HYP_KVM_MEM_SHARE_FUNC_ID \
  30. ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \
  31. ARM_SMCCC_SMC_64, \
  32. ARM_SMCCC_OWNER_VENDOR_HYP, \
  33. ARM_SMCCC_KVM_FUNC_MEM_SHARE)
  34. #endif /* ARM_SMCCC_KVM_FUNC_MEM_SHARE */
  35. #ifndef ARM_SMCCC_KVM_FUNC_MEM_UNSHARE
  36. #define ARM_SMCCC_KVM_FUNC_MEM_UNSHARE 4
  37. #define ARM_SMCCC_VENDOR_HYP_KVM_MEM_UNSHARE_FUNC_ID \
  38. ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \
  39. ARM_SMCCC_SMC_64, \
  40. ARM_SMCCC_OWNER_VENDOR_HYP, \
  41. ARM_SMCCC_KVM_FUNC_MEM_UNSHARE)
  42. #endif /* ARM_SMCCC_KVM_FUNC_MEM_UNSHARE */
  43. static unsigned long memshare_granule_sz;
  44. bool mem_encrypt_active(void)
  45. {
  46. return memshare_granule_sz;
  47. }
  48. EXPORT_SYMBOL(mem_encrypt_active);
  49. void kvm_init_memshare_services(void)
  50. {
  51. int i;
  52. struct arm_smccc_res res;
  53. const u32 funcs[] = {
  54. ARM_SMCCC_KVM_FUNC_HYP_MEMINFO,
  55. ARM_SMCCC_KVM_FUNC_MEM_SHARE,
  56. ARM_SMCCC_KVM_FUNC_MEM_UNSHARE,
  57. };
  58. for (i = 0; i < ARRAY_SIZE(funcs); ++i) {
  59. if (!kvm_arm_hyp_service_available(funcs[i]))
  60. return;
  61. }
  62. arm_smccc_1_1_invoke(ARM_SMCCC_VENDOR_HYP_KVM_HYP_MEMINFO_FUNC_ID,
  63. 0, 0, 0, &res);
  64. if (res.a0 > PAGE_SIZE) /* Includes error codes */
  65. return;
  66. memshare_granule_sz = res.a0;
  67. }
  68. static int arm_smccc_share_unshare_page(u32 func_id, phys_addr_t phys)
  69. {
  70. phys_addr_t end = phys + PAGE_SIZE;
  71. while (phys < end) {
  72. struct arm_smccc_res res;
  73. arm_smccc_1_1_invoke(func_id, phys, 0, 0, &res);
  74. if (res.a0 != SMCCC_RET_SUCCESS)
  75. return -EPERM;
  76. phys += memshare_granule_sz;
  77. }
  78. return 0;
  79. }
  80. static int set_memory_xcrypted(u32 func_id, unsigned long start, int numpages)
  81. {
  82. void *addr = (void *)start, *end = addr + numpages * PAGE_SIZE;
  83. while (addr < end) {
  84. int err;
  85. err = arm_smccc_share_unshare_page(func_id, virt_to_phys(addr));
  86. if (err)
  87. return err;
  88. addr += PAGE_SIZE;
  89. }
  90. return 0;
  91. }
  92. int set_memory_encrypted(unsigned long addr, int numpages)
  93. {
  94. if (!memshare_granule_sz || WARN_ON(!PAGE_ALIGNED(addr)))
  95. return 0;
  96. return set_memory_xcrypted(ARM_SMCCC_VENDOR_HYP_KVM_MEM_UNSHARE_FUNC_ID,
  97. addr, numpages);
  98. }
  99. EXPORT_SYMBOL_GPL(set_memory_encrypted);
  100. int set_memory_decrypted(unsigned long addr, int numpages)
  101. {
  102. if (!memshare_granule_sz || WARN_ON(!PAGE_ALIGNED(addr)))
  103. return 0;
  104. return set_memory_xcrypted(ARM_SMCCC_VENDOR_HYP_KVM_MEM_SHARE_FUNC_ID,
  105. addr, numpages);
  106. }
  107. EXPORT_SYMBOL_GPL(set_memory_decrypted);