mem_encrypt.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Implementation of the memory encryption/decryption API.
  4. *
  5. * Since the low-level details of the operation depend on the
  6. * Confidential Computing environment (e.g. pKVM, CCA, ...), this just
  7. * acts as a top-level dispatcher to whatever hooks may have been
  8. * registered.
  9. *
  10. * Author: Will Deacon <will@kernel.org>
  11. * Copyright (C) 2024 Google LLC
  12. *
  13. * "Hello, boils and ghouls!"
  14. */
  15. #include <linux/bug.h>
  16. #include <linux/compiler.h>
  17. #include <linux/err.h>
  18. #include <linux/mm.h>
  19. #include <asm/mem_encrypt.h>
  20. static const struct arm64_mem_crypt_ops *crypt_ops;
  21. int arm64_mem_crypt_ops_register(const struct arm64_mem_crypt_ops *ops)
  22. {
  23. if (WARN_ON(crypt_ops))
  24. return -EBUSY;
  25. crypt_ops = ops;
  26. return 0;
  27. }
  28. int set_memory_encrypted(unsigned long addr, int numpages)
  29. {
  30. if (likely(!crypt_ops) || WARN_ON(!PAGE_ALIGNED(addr)))
  31. return 0;
  32. return crypt_ops->encrypt(addr, numpages);
  33. }
  34. EXPORT_SYMBOL_GPL(set_memory_encrypted);
  35. int set_memory_decrypted(unsigned long addr, int numpages)
  36. {
  37. if (likely(!crypt_ops) || WARN_ON(!PAGE_ALIGNED(addr)))
  38. return 0;
  39. return crypt_ops->decrypt(addr, numpages);
  40. }
  41. EXPORT_SYMBOL_GPL(set_memory_decrypted);