encl.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /**
  3. * Copyright(c) 2016-20 Intel Corporation.
  4. *
  5. * Contains the software defined data structures for enclaves.
  6. */
  7. #ifndef _X86_ENCL_H
  8. #define _X86_ENCL_H
  9. #include <linux/cpumask.h>
  10. #include <linux/kref.h>
  11. #include <linux/list.h>
  12. #include <linux/mm_types.h>
  13. #include <linux/mmu_notifier.h>
  14. #include <linux/mutex.h>
  15. #include <linux/notifier.h>
  16. #include <linux/srcu.h>
  17. #include <linux/workqueue.h>
  18. #include <linux/xarray.h>
  19. #include "sgx.h"
  20. /* 'desc' bits holding the offset in the VA (version array) page. */
  21. #define SGX_ENCL_PAGE_VA_OFFSET_MASK GENMASK_ULL(11, 3)
  22. /* 'desc' bit marking that the page is being reclaimed. */
  23. #define SGX_ENCL_PAGE_BEING_RECLAIMED BIT(3)
  24. struct sgx_encl_page {
  25. unsigned long desc;
  26. unsigned long vm_max_prot_bits:8;
  27. enum sgx_page_type type:16;
  28. struct sgx_epc_page *epc_page;
  29. struct sgx_encl *encl;
  30. struct sgx_va_page *va_page;
  31. };
  32. enum sgx_encl_flags {
  33. SGX_ENCL_IOCTL = BIT(0),
  34. SGX_ENCL_DEBUG = BIT(1),
  35. SGX_ENCL_CREATED = BIT(2),
  36. SGX_ENCL_INITIALIZED = BIT(3),
  37. };
  38. struct sgx_encl_mm {
  39. struct sgx_encl *encl;
  40. struct mm_struct *mm;
  41. struct list_head list;
  42. struct mmu_notifier mmu_notifier;
  43. };
  44. struct sgx_encl {
  45. unsigned long base;
  46. unsigned long size;
  47. unsigned long flags;
  48. unsigned int page_cnt;
  49. unsigned int secs_child_cnt;
  50. struct mutex lock;
  51. struct xarray page_array;
  52. struct sgx_encl_page secs;
  53. unsigned long attributes;
  54. unsigned long attributes_mask;
  55. cpumask_t cpumask;
  56. struct file *backing;
  57. struct kref refcount;
  58. struct list_head va_pages;
  59. unsigned long mm_list_version;
  60. struct list_head mm_list;
  61. spinlock_t mm_lock;
  62. struct srcu_struct srcu;
  63. };
  64. #define SGX_VA_SLOT_COUNT 512
  65. struct sgx_va_page {
  66. struct sgx_epc_page *epc_page;
  67. DECLARE_BITMAP(slots, SGX_VA_SLOT_COUNT);
  68. struct list_head list;
  69. };
  70. struct sgx_backing {
  71. struct page *contents;
  72. struct page *pcmd;
  73. unsigned long pcmd_offset;
  74. };
  75. extern const struct vm_operations_struct sgx_vm_ops;
  76. static inline int sgx_encl_find(struct mm_struct *mm, unsigned long addr,
  77. struct vm_area_struct **vma)
  78. {
  79. struct vm_area_struct *result;
  80. result = vma_lookup(mm, addr);
  81. if (!result || result->vm_ops != &sgx_vm_ops)
  82. return -EINVAL;
  83. *vma = result;
  84. return 0;
  85. }
  86. int sgx_encl_may_map(struct sgx_encl *encl, unsigned long start,
  87. unsigned long end, unsigned long vm_flags);
  88. bool current_is_ksgxd(void);
  89. void sgx_encl_release(struct kref *ref);
  90. int sgx_encl_mm_add(struct sgx_encl *encl, struct mm_struct *mm);
  91. const cpumask_t *sgx_encl_cpumask(struct sgx_encl *encl);
  92. int sgx_encl_alloc_backing(struct sgx_encl *encl, unsigned long page_index,
  93. struct sgx_backing *backing);
  94. void sgx_encl_put_backing(struct sgx_backing *backing);
  95. int sgx_encl_test_and_clear_young(struct mm_struct *mm,
  96. struct sgx_encl_page *page);
  97. struct sgx_encl_page *sgx_encl_page_alloc(struct sgx_encl *encl,
  98. unsigned long offset,
  99. u64 secinfo_flags);
  100. void sgx_zap_enclave_ptes(struct sgx_encl *encl, unsigned long addr);
  101. struct sgx_epc_page *sgx_alloc_va_page(bool reclaim);
  102. unsigned int sgx_alloc_va_slot(struct sgx_va_page *va_page);
  103. void sgx_free_va_slot(struct sgx_va_page *va_page, unsigned int offset);
  104. bool sgx_va_page_full(struct sgx_va_page *va_page);
  105. void sgx_encl_free_epc_page(struct sgx_epc_page *page);
  106. struct sgx_encl_page *sgx_encl_load_page(struct sgx_encl *encl,
  107. unsigned long addr);
  108. struct sgx_va_page *sgx_encl_grow(struct sgx_encl *encl, bool reclaim);
  109. void sgx_encl_shrink(struct sgx_encl *encl, struct sgx_va_page *va_page);
  110. #endif /* _X86_ENCL_H */