sgx.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _X86_SGX_H
  3. #define _X86_SGX_H
  4. #include <linux/bitops.h>
  5. #include <linux/err.h>
  6. #include <linux/io.h>
  7. #include <linux/rwsem.h>
  8. #include <linux/types.h>
  9. #include <asm/asm.h>
  10. #include <asm/sgx.h>
  11. #undef pr_fmt
  12. #define pr_fmt(fmt) "sgx: " fmt
  13. #define EREMOVE_ERROR_MESSAGE \
  14. "EREMOVE returned %d (0x%x) and an EPC page was leaked. SGX may become unusable. " \
  15. "Refer to Documentation/x86/sgx.rst for more information."
  16. #define SGX_MAX_EPC_SECTIONS 8
  17. #define SGX_EEXTEND_BLOCK_SIZE 256
  18. #define SGX_NR_TO_SCAN 16
  19. #define SGX_NR_LOW_PAGES 32
  20. #define SGX_NR_HIGH_PAGES 64
  21. /* Pages, which are being tracked by the page reclaimer. */
  22. #define SGX_EPC_PAGE_RECLAIMER_TRACKED BIT(0)
  23. /* Pages on free list */
  24. #define SGX_EPC_PAGE_IS_FREE BIT(1)
  25. struct sgx_epc_page {
  26. unsigned int section;
  27. u16 flags;
  28. u16 poison;
  29. struct sgx_encl_page *owner;
  30. struct list_head list;
  31. };
  32. /*
  33. * Contains the tracking data for NUMA nodes having EPC pages. Most importantly,
  34. * the free page list local to the node is stored here.
  35. */
  36. struct sgx_numa_node {
  37. struct list_head free_page_list;
  38. struct list_head sgx_poison_page_list;
  39. unsigned long size;
  40. spinlock_t lock;
  41. };
  42. /*
  43. * The firmware can define multiple chunks of EPC to the different areas of the
  44. * physical memory e.g. for memory areas of the each node. This structure is
  45. * used to store EPC pages for one EPC section and virtual memory area where
  46. * the pages have been mapped.
  47. */
  48. struct sgx_epc_section {
  49. unsigned long phys_addr;
  50. void *virt_addr;
  51. struct sgx_epc_page *pages;
  52. struct sgx_numa_node *node;
  53. };
  54. extern struct sgx_epc_section sgx_epc_sections[SGX_MAX_EPC_SECTIONS];
  55. static inline unsigned long sgx_get_epc_phys_addr(struct sgx_epc_page *page)
  56. {
  57. struct sgx_epc_section *section = &sgx_epc_sections[page->section];
  58. unsigned long index;
  59. index = ((unsigned long)page - (unsigned long)section->pages) / sizeof(*page);
  60. return section->phys_addr + index * PAGE_SIZE;
  61. }
  62. static inline void *sgx_get_epc_virt_addr(struct sgx_epc_page *page)
  63. {
  64. struct sgx_epc_section *section = &sgx_epc_sections[page->section];
  65. unsigned long index;
  66. index = ((unsigned long)page - (unsigned long)section->pages) / sizeof(*page);
  67. return section->virt_addr + index * PAGE_SIZE;
  68. }
  69. struct sgx_epc_page *__sgx_alloc_epc_page(void);
  70. void sgx_free_epc_page(struct sgx_epc_page *page);
  71. void sgx_reclaim_direct(void);
  72. void sgx_mark_page_reclaimable(struct sgx_epc_page *page);
  73. int sgx_unmark_page_reclaimable(struct sgx_epc_page *page);
  74. struct sgx_epc_page *sgx_alloc_epc_page(void *owner, bool reclaim);
  75. void sgx_ipi_cb(void *info);
  76. #ifdef CONFIG_X86_SGX_KVM
  77. int __init sgx_vepc_init(void);
  78. #else
  79. static inline int __init sgx_vepc_init(void)
  80. {
  81. return -ENODEV;
  82. }
  83. #endif
  84. void sgx_update_lepubkeyhash(u64 *lepubkeyhash);
  85. #endif /* _X86_SGX_H */