1e7e05452SSean Christopherson /* SPDX-License-Identifier: GPL-2.0 */ 2e7e05452SSean Christopherson #ifndef _X86_SGX_H 3e7e05452SSean Christopherson #define _X86_SGX_H 4e7e05452SSean Christopherson 5e7e05452SSean Christopherson #include <linux/bitops.h> 6e7e05452SSean Christopherson #include <linux/err.h> 7e7e05452SSean Christopherson #include <linux/io.h> 8e7e05452SSean Christopherson #include <linux/rwsem.h> 9e7e05452SSean Christopherson #include <linux/types.h> 10e7e05452SSean Christopherson #include <asm/asm.h> 118ca52cc3SSean Christopherson #include <asm/sgx.h> 12e7e05452SSean Christopherson 13e7e05452SSean Christopherson #undef pr_fmt 14e7e05452SSean Christopherson #define pr_fmt(fmt) "sgx: " fmt 15e7e05452SSean Christopherson 16b0c7459bSKai Huang #define EREMOVE_ERROR_MESSAGE \ 17b0c7459bSKai Huang "EREMOVE returned %d (0x%x) and an EPC page was leaked. SGX may become unusable. " \ 18b0c7459bSKai Huang "Refer to Documentation/x86/sgx.rst for more information." 19b0c7459bSKai Huang 20e7e05452SSean Christopherson #define SGX_MAX_EPC_SECTIONS 8 21c6d26d37SJarkko Sakkinen #define SGX_EEXTEND_BLOCK_SIZE 256 221728ab54SJarkko Sakkinen #define SGX_NR_TO_SCAN 16 231728ab54SJarkko Sakkinen #define SGX_NR_LOW_PAGES 32 241728ab54SJarkko Sakkinen #define SGX_NR_HIGH_PAGES 64 251728ab54SJarkko Sakkinen 261728ab54SJarkko Sakkinen /* Pages, which are being tracked by the page reclaimer. */ 271728ab54SJarkko Sakkinen #define SGX_EPC_PAGE_RECLAIMER_TRACKED BIT(0) 28e7e05452SSean Christopherson 29d6d261bdSTony Luck /* Pages on free list */ 30d6d261bdSTony Luck #define SGX_EPC_PAGE_IS_FREE BIT(1) 31d6d261bdSTony Luck 32e7e05452SSean Christopherson struct sgx_epc_page { 33e7e05452SSean Christopherson unsigned int section; 34*992801aeSTony Luck u16 flags; 35*992801aeSTony Luck u16 poison; 361728ab54SJarkko Sakkinen struct sgx_encl_page *owner; 37e7e05452SSean Christopherson struct list_head list; 38e7e05452SSean Christopherson }; 39e7e05452SSean Christopherson 40e7e05452SSean Christopherson /* 41901ddbb9SJarkko Sakkinen * Contains the tracking data for NUMA nodes having EPC pages. Most importantly, 42901ddbb9SJarkko Sakkinen * the free page list local to the node is stored here. 43901ddbb9SJarkko Sakkinen */ 44901ddbb9SJarkko Sakkinen struct sgx_numa_node { 45901ddbb9SJarkko Sakkinen struct list_head free_page_list; 46*992801aeSTony Luck struct list_head sgx_poison_page_list; 47901ddbb9SJarkko Sakkinen spinlock_t lock; 48901ddbb9SJarkko Sakkinen }; 49901ddbb9SJarkko Sakkinen 50901ddbb9SJarkko Sakkinen /* 51e7e05452SSean Christopherson * The firmware can define multiple chunks of EPC to the different areas of the 52e7e05452SSean Christopherson * physical memory e.g. for memory areas of the each node. This structure is 53e7e05452SSean Christopherson * used to store EPC pages for one EPC section and virtual memory area where 54e7e05452SSean Christopherson * the pages have been mapped. 55e7e05452SSean Christopherson */ 56e7e05452SSean Christopherson struct sgx_epc_section { 57e7e05452SSean Christopherson unsigned long phys_addr; 58e7e05452SSean Christopherson void *virt_addr; 59e7e05452SSean Christopherson struct sgx_epc_page *pages; 60901ddbb9SJarkko Sakkinen struct sgx_numa_node *node; 61e7e05452SSean Christopherson }; 62e7e05452SSean Christopherson 63e7e05452SSean Christopherson extern struct sgx_epc_section sgx_epc_sections[SGX_MAX_EPC_SECTIONS]; 64e7e05452SSean Christopherson 65e7e05452SSean Christopherson static inline unsigned long sgx_get_epc_phys_addr(struct sgx_epc_page *page) 66e7e05452SSean Christopherson { 67e7e05452SSean Christopherson struct sgx_epc_section *section = &sgx_epc_sections[page->section]; 68e7e05452SSean Christopherson unsigned long index; 69e7e05452SSean Christopherson 70e7e05452SSean Christopherson index = ((unsigned long)page - (unsigned long)section->pages) / sizeof(*page); 71e7e05452SSean Christopherson 72e7e05452SSean Christopherson return section->phys_addr + index * PAGE_SIZE; 73e7e05452SSean Christopherson } 74e7e05452SSean Christopherson 75e7e05452SSean Christopherson static inline void *sgx_get_epc_virt_addr(struct sgx_epc_page *page) 76e7e05452SSean Christopherson { 77e7e05452SSean Christopherson struct sgx_epc_section *section = &sgx_epc_sections[page->section]; 78e7e05452SSean Christopherson unsigned long index; 79e7e05452SSean Christopherson 80e7e05452SSean Christopherson index = ((unsigned long)page - (unsigned long)section->pages) / sizeof(*page); 81e7e05452SSean Christopherson 82e7e05452SSean Christopherson return section->virt_addr + index * PAGE_SIZE; 83e7e05452SSean Christopherson } 84e7e05452SSean Christopherson 85d2285493SJarkko Sakkinen struct sgx_epc_page *__sgx_alloc_epc_page(void); 86d2285493SJarkko Sakkinen void sgx_free_epc_page(struct sgx_epc_page *page); 87d2285493SJarkko Sakkinen 881728ab54SJarkko Sakkinen void sgx_mark_page_reclaimable(struct sgx_epc_page *page); 891728ab54SJarkko Sakkinen int sgx_unmark_page_reclaimable(struct sgx_epc_page *page); 901728ab54SJarkko Sakkinen struct sgx_epc_page *sgx_alloc_epc_page(void *owner, bool reclaim); 911728ab54SJarkko Sakkinen 92540745ddSSean Christopherson #ifdef CONFIG_X86_SGX_KVM 93540745ddSSean Christopherson int __init sgx_vepc_init(void); 94540745ddSSean Christopherson #else 95540745ddSSean Christopherson static inline int __init sgx_vepc_init(void) 96540745ddSSean Christopherson { 97540745ddSSean Christopherson return -ENODEV; 98540745ddSSean Christopherson } 99540745ddSSean Christopherson #endif 100540745ddSSean Christopherson 10173916b6aSKai Huang void sgx_update_lepubkeyhash(u64 *lepubkeyhash); 10273916b6aSKai Huang 103e7e05452SSean Christopherson #endif /* _X86_SGX_H */ 104