13fe0778eSJarkko Sakkinen // SPDX-License-Identifier: GPL-2.0
23fe0778eSJarkko Sakkinen /* Copyright(c) 2016-20 Intel Corporation. */
33fe0778eSJarkko Sakkinen
43fe0778eSJarkko Sakkinen #include <linux/lockdep.h>
53fe0778eSJarkko Sakkinen #include <linux/mm.h>
63fe0778eSJarkko Sakkinen #include <linux/mman.h>
73fe0778eSJarkko Sakkinen #include <linux/shmem_fs.h>
83fe0778eSJarkko Sakkinen #include <linux/suspend.h>
93fe0778eSJarkko Sakkinen #include <linux/sched/mm.h>
108ca52cc3SSean Christopherson #include <asm/sgx.h>
113fe0778eSJarkko Sakkinen #include "encl.h"
123fe0778eSJarkko Sakkinen #include "encls.h"
133fe0778eSJarkko Sakkinen #include "sgx.h"
143fe0778eSJarkko Sakkinen
15ee56a283SKristen Carlson Accardi static int sgx_encl_lookup_backing(struct sgx_encl *encl, unsigned long page_index,
16ee56a283SKristen Carlson Accardi struct sgx_backing *backing);
17ee56a283SKristen Carlson Accardi
18af117837SReinette Chatre #define PCMDS_PER_PAGE (PAGE_SIZE / sizeof(struct sgx_pcmd))
19af117837SReinette Chatre /*
20af117837SReinette Chatre * 32 PCMD entries share a PCMD page. PCMD_FIRST_MASK is used to
21af117837SReinette Chatre * determine the page index associated with the first PCMD entry
22af117837SReinette Chatre * within a PCMD page.
23af117837SReinette Chatre */
24af117837SReinette Chatre #define PCMD_FIRST_MASK GENMASK(4, 0)
25af117837SReinette Chatre
26af117837SReinette Chatre /**
27af117837SReinette Chatre * reclaimer_writing_to_pcmd() - Query if any enclave page associated with
28af117837SReinette Chatre * a PCMD page is in process of being reclaimed.
29af117837SReinette Chatre * @encl: Enclave to which PCMD page belongs
30af117837SReinette Chatre * @start_addr: Address of enclave page using first entry within the PCMD page
31af117837SReinette Chatre *
32af117837SReinette Chatre * When an enclave page is reclaimed some Paging Crypto MetaData (PCMD) is
33af117837SReinette Chatre * stored. The PCMD data of a reclaimed enclave page contains enough
34af117837SReinette Chatre * information for the processor to verify the page at the time
35af117837SReinette Chatre * it is loaded back into the Enclave Page Cache (EPC).
36af117837SReinette Chatre *
37af117837SReinette Chatre * The backing storage to which enclave pages are reclaimed is laid out as
38af117837SReinette Chatre * follows:
39af117837SReinette Chatre * Encrypted enclave pages:SECS page:PCMD pages
40af117837SReinette Chatre *
41af117837SReinette Chatre * Each PCMD page contains the PCMD metadata of
42af117837SReinette Chatre * PAGE_SIZE/sizeof(struct sgx_pcmd) enclave pages.
43af117837SReinette Chatre *
44af117837SReinette Chatre * A PCMD page can only be truncated if it is (a) empty, and (b) not in the
45af117837SReinette Chatre * process of getting data (and thus soon being non-empty). (b) is tested with
46af117837SReinette Chatre * a check if an enclave page sharing the PCMD page is in the process of being
47af117837SReinette Chatre * reclaimed.
48af117837SReinette Chatre *
49af117837SReinette Chatre * The reclaimer sets the SGX_ENCL_PAGE_BEING_RECLAIMED flag when it
50af117837SReinette Chatre * intends to reclaim that enclave page - it means that the PCMD page
51af117837SReinette Chatre * associated with that enclave page is about to get some data and thus
52af117837SReinette Chatre * even if the PCMD page is empty, it should not be truncated.
53af117837SReinette Chatre *
54af117837SReinette Chatre * Context: Enclave mutex (&sgx_encl->lock) must be held.
55af117837SReinette Chatre * Return: 1 if the reclaimer is about to write to the PCMD page
56af117837SReinette Chatre * 0 if the reclaimer has no intention to write to the PCMD page
57af117837SReinette Chatre */
reclaimer_writing_to_pcmd(struct sgx_encl * encl,unsigned long start_addr)58af117837SReinette Chatre static int reclaimer_writing_to_pcmd(struct sgx_encl *encl,
59af117837SReinette Chatre unsigned long start_addr)
60af117837SReinette Chatre {
61af117837SReinette Chatre int reclaimed = 0;
62af117837SReinette Chatre int i;
63af117837SReinette Chatre
64af117837SReinette Chatre /*
65af117837SReinette Chatre * PCMD_FIRST_MASK is based on number of PCMD entries within
66af117837SReinette Chatre * PCMD page being 32.
67af117837SReinette Chatre */
68af117837SReinette Chatre BUILD_BUG_ON(PCMDS_PER_PAGE != 32);
69af117837SReinette Chatre
70af117837SReinette Chatre for (i = 0; i < PCMDS_PER_PAGE; i++) {
71af117837SReinette Chatre struct sgx_encl_page *entry;
72af117837SReinette Chatre unsigned long addr;
73af117837SReinette Chatre
74af117837SReinette Chatre addr = start_addr + i * PAGE_SIZE;
75af117837SReinette Chatre
76af117837SReinette Chatre /*
77af117837SReinette Chatre * Stop when reaching the SECS page - it does not
78af117837SReinette Chatre * have a page_array entry and its reclaim is
79af117837SReinette Chatre * started and completed with enclave mutex held so
80af117837SReinette Chatre * it does not use the SGX_ENCL_PAGE_BEING_RECLAIMED
81af117837SReinette Chatre * flag.
82af117837SReinette Chatre */
83af117837SReinette Chatre if (addr == encl->base + encl->size)
84af117837SReinette Chatre break;
85af117837SReinette Chatre
86af117837SReinette Chatre entry = xa_load(&encl->page_array, PFN_DOWN(addr));
87af117837SReinette Chatre if (!entry)
88af117837SReinette Chatre continue;
89af117837SReinette Chatre
90af117837SReinette Chatre /*
91af117837SReinette Chatre * VA page slot ID uses same bit as the flag so it is important
92af117837SReinette Chatre * to ensure that the page is not already in backing store.
93af117837SReinette Chatre */
94af117837SReinette Chatre if (entry->epc_page &&
95af117837SReinette Chatre (entry->desc & SGX_ENCL_PAGE_BEING_RECLAIMED)) {
96af117837SReinette Chatre reclaimed = 1;
97af117837SReinette Chatre break;
98af117837SReinette Chatre }
99af117837SReinette Chatre }
100af117837SReinette Chatre
101af117837SReinette Chatre return reclaimed;
102af117837SReinette Chatre }
103af117837SReinette Chatre
1041728ab54SJarkko Sakkinen /*
10508999b24SJarkko Sakkinen * Calculate byte offset of a PCMD struct associated with an enclave page. PCMD's
10608999b24SJarkko Sakkinen * follow right after the EPC data in the backing storage. In addition to the
10708999b24SJarkko Sakkinen * visible enclave pages, there's one extra page slot for SECS, before PCMD
10808999b24SJarkko Sakkinen * structs.
10908999b24SJarkko Sakkinen */
sgx_encl_get_backing_page_pcmd_offset(struct sgx_encl * encl,unsigned long page_index)11008999b24SJarkko Sakkinen static inline pgoff_t sgx_encl_get_backing_page_pcmd_offset(struct sgx_encl *encl,
11108999b24SJarkko Sakkinen unsigned long page_index)
11208999b24SJarkko Sakkinen {
11308999b24SJarkko Sakkinen pgoff_t epc_end_off = encl->size + sizeof(struct sgx_secs);
11408999b24SJarkko Sakkinen
11508999b24SJarkko Sakkinen return epc_end_off + page_index * sizeof(struct sgx_pcmd);
11608999b24SJarkko Sakkinen }
11708999b24SJarkko Sakkinen
11808999b24SJarkko Sakkinen /*
11908999b24SJarkko Sakkinen * Free a page from the backing storage in the given page index.
12008999b24SJarkko Sakkinen */
sgx_encl_truncate_backing_page(struct sgx_encl * encl,unsigned long page_index)12108999b24SJarkko Sakkinen static inline void sgx_encl_truncate_backing_page(struct sgx_encl *encl, unsigned long page_index)
12208999b24SJarkko Sakkinen {
12308999b24SJarkko Sakkinen struct inode *inode = file_inode(encl->backing);
12408999b24SJarkko Sakkinen
12508999b24SJarkko Sakkinen shmem_truncate_range(inode, PFN_PHYS(page_index), PFN_PHYS(page_index) + PAGE_SIZE - 1);
12608999b24SJarkko Sakkinen }
12708999b24SJarkko Sakkinen
12808999b24SJarkko Sakkinen /*
1291728ab54SJarkko Sakkinen * ELDU: Load an EPC page as unblocked. For more info, see "OS Management of EPC
1301728ab54SJarkko Sakkinen * Pages" in the SDM.
1311728ab54SJarkko Sakkinen */
__sgx_encl_eldu(struct sgx_encl_page * encl_page,struct sgx_epc_page * epc_page,struct sgx_epc_page * secs_page)1321728ab54SJarkko Sakkinen static int __sgx_encl_eldu(struct sgx_encl_page *encl_page,
1331728ab54SJarkko Sakkinen struct sgx_epc_page *epc_page,
1341728ab54SJarkko Sakkinen struct sgx_epc_page *secs_page)
1351728ab54SJarkko Sakkinen {
1361728ab54SJarkko Sakkinen unsigned long va_offset = encl_page->desc & SGX_ENCL_PAGE_VA_OFFSET_MASK;
1371728ab54SJarkko Sakkinen struct sgx_encl *encl = encl_page->encl;
13808999b24SJarkko Sakkinen pgoff_t page_index, page_pcmd_off;
139af117837SReinette Chatre unsigned long pcmd_first_page;
1401728ab54SJarkko Sakkinen struct sgx_pageinfo pginfo;
1411728ab54SJarkko Sakkinen struct sgx_backing b;
14208999b24SJarkko Sakkinen bool pcmd_page_empty;
14308999b24SJarkko Sakkinen u8 *pcmd_page;
1441728ab54SJarkko Sakkinen int ret;
1451728ab54SJarkko Sakkinen
1461728ab54SJarkko Sakkinen if (secs_page)
1471728ab54SJarkko Sakkinen page_index = PFN_DOWN(encl_page->desc - encl_page->encl->base);
1481728ab54SJarkko Sakkinen else
1491728ab54SJarkko Sakkinen page_index = PFN_DOWN(encl->size);
1501728ab54SJarkko Sakkinen
151af117837SReinette Chatre /*
152af117837SReinette Chatre * Address of enclave page using the first entry within the PCMD page.
153af117837SReinette Chatre */
154af117837SReinette Chatre pcmd_first_page = PFN_PHYS(page_index & ~PCMD_FIRST_MASK) + encl->base;
155af117837SReinette Chatre
15608999b24SJarkko Sakkinen page_pcmd_off = sgx_encl_get_backing_page_pcmd_offset(encl, page_index);
15708999b24SJarkko Sakkinen
1580c9782e2SKristen Carlson Accardi ret = sgx_encl_lookup_backing(encl, page_index, &b);
1591728ab54SJarkko Sakkinen if (ret)
1601728ab54SJarkko Sakkinen return ret;
1611728ab54SJarkko Sakkinen
1621728ab54SJarkko Sakkinen pginfo.addr = encl_page->desc & PAGE_MASK;
16389e927bbSKristen Carlson Accardi pginfo.contents = (unsigned long)kmap_local_page(b.contents);
16489e927bbSKristen Carlson Accardi pcmd_page = kmap_local_page(b.pcmd);
16508999b24SJarkko Sakkinen pginfo.metadata = (unsigned long)pcmd_page + b.pcmd_offset;
1661728ab54SJarkko Sakkinen
1671728ab54SJarkko Sakkinen if (secs_page)
1681728ab54SJarkko Sakkinen pginfo.secs = (u64)sgx_get_epc_virt_addr(secs_page);
1691728ab54SJarkko Sakkinen else
1701728ab54SJarkko Sakkinen pginfo.secs = 0;
1711728ab54SJarkko Sakkinen
1721728ab54SJarkko Sakkinen ret = __eldu(&pginfo, sgx_get_epc_virt_addr(epc_page),
1731728ab54SJarkko Sakkinen sgx_get_epc_virt_addr(encl_page->va_page->epc_page) + va_offset);
1741728ab54SJarkko Sakkinen if (ret) {
1751728ab54SJarkko Sakkinen if (encls_failed(ret))
1761728ab54SJarkko Sakkinen ENCLS_WARN(ret, "ELDU");
1771728ab54SJarkko Sakkinen
1781728ab54SJarkko Sakkinen ret = -EFAULT;
1791728ab54SJarkko Sakkinen }
1801728ab54SJarkko Sakkinen
18108999b24SJarkko Sakkinen memset(pcmd_page + b.pcmd_offset, 0, sizeof(struct sgx_pcmd));
1822154e1c1SReinette Chatre set_page_dirty(b.pcmd);
18308999b24SJarkko Sakkinen
18408999b24SJarkko Sakkinen /*
18508999b24SJarkko Sakkinen * The area for the PCMD in the page was zeroed above. Check if the
18608999b24SJarkko Sakkinen * whole page is now empty meaning that all PCMD's have been zeroed:
18708999b24SJarkko Sakkinen */
18808999b24SJarkko Sakkinen pcmd_page_empty = !memchr_inv(pcmd_page, 0, PAGE_SIZE);
18908999b24SJarkko Sakkinen
19089e927bbSKristen Carlson Accardi kunmap_local(pcmd_page);
19189e927bbSKristen Carlson Accardi kunmap_local((void *)(unsigned long)pginfo.contents);
1921728ab54SJarkko Sakkinen
193e3a3bbe3SReinette Chatre get_page(b.pcmd);
1946bd42964SReinette Chatre sgx_encl_put_backing(&b);
1951728ab54SJarkko Sakkinen
19608999b24SJarkko Sakkinen sgx_encl_truncate_backing_page(encl, page_index);
19708999b24SJarkko Sakkinen
198e3a3bbe3SReinette Chatre if (pcmd_page_empty && !reclaimer_writing_to_pcmd(encl, pcmd_first_page)) {
19908999b24SJarkko Sakkinen sgx_encl_truncate_backing_page(encl, PFN_DOWN(page_pcmd_off));
20089e927bbSKristen Carlson Accardi pcmd_page = kmap_local_page(b.pcmd);
201e3a3bbe3SReinette Chatre if (memchr_inv(pcmd_page, 0, PAGE_SIZE))
202e3a3bbe3SReinette Chatre pr_warn("PCMD page not empty after truncate.\n");
20389e927bbSKristen Carlson Accardi kunmap_local(pcmd_page);
204e3a3bbe3SReinette Chatre }
205e3a3bbe3SReinette Chatre
206e3a3bbe3SReinette Chatre put_page(b.pcmd);
20708999b24SJarkko Sakkinen
2081728ab54SJarkko Sakkinen return ret;
2091728ab54SJarkko Sakkinen }
2101728ab54SJarkko Sakkinen
sgx_encl_eldu(struct sgx_encl_page * encl_page,struct sgx_epc_page * secs_page)2111728ab54SJarkko Sakkinen static struct sgx_epc_page *sgx_encl_eldu(struct sgx_encl_page *encl_page,
2121728ab54SJarkko Sakkinen struct sgx_epc_page *secs_page)
2131728ab54SJarkko Sakkinen {
2141728ab54SJarkko Sakkinen
2151728ab54SJarkko Sakkinen unsigned long va_offset = encl_page->desc & SGX_ENCL_PAGE_VA_OFFSET_MASK;
2161728ab54SJarkko Sakkinen struct sgx_encl *encl = encl_page->encl;
2171728ab54SJarkko Sakkinen struct sgx_epc_page *epc_page;
2181728ab54SJarkko Sakkinen int ret;
2191728ab54SJarkko Sakkinen
2201728ab54SJarkko Sakkinen epc_page = sgx_alloc_epc_page(encl_page, false);
2211728ab54SJarkko Sakkinen if (IS_ERR(epc_page))
2221728ab54SJarkko Sakkinen return epc_page;
2231728ab54SJarkko Sakkinen
2241728ab54SJarkko Sakkinen ret = __sgx_encl_eldu(encl_page, epc_page, secs_page);
2251728ab54SJarkko Sakkinen if (ret) {
226b0c7459bSKai Huang sgx_encl_free_epc_page(epc_page);
2271728ab54SJarkko Sakkinen return ERR_PTR(ret);
2281728ab54SJarkko Sakkinen }
2291728ab54SJarkko Sakkinen
2301728ab54SJarkko Sakkinen sgx_free_va_slot(encl_page->va_page, va_offset);
2311728ab54SJarkko Sakkinen list_move(&encl_page->va_page->list, &encl->va_pages);
2321728ab54SJarkko Sakkinen encl_page->desc &= ~SGX_ENCL_PAGE_VA_OFFSET_MASK;
2331728ab54SJarkko Sakkinen encl_page->epc_page = epc_page;
2341728ab54SJarkko Sakkinen
2351728ab54SJarkko Sakkinen return epc_page;
2361728ab54SJarkko Sakkinen }
2371728ab54SJarkko Sakkinen
238*c6c2adcbSHaitao Huang /*
239*c6c2adcbSHaitao Huang * Ensure the SECS page is not swapped out. Must be called with encl->lock
240*c6c2adcbSHaitao Huang * to protect the enclave states including SECS and ensure the SECS page is
241*c6c2adcbSHaitao Huang * not swapped out again while being used.
242*c6c2adcbSHaitao Huang */
sgx_encl_load_secs(struct sgx_encl * encl)243*c6c2adcbSHaitao Huang static struct sgx_epc_page *sgx_encl_load_secs(struct sgx_encl *encl)
244*c6c2adcbSHaitao Huang {
245*c6c2adcbSHaitao Huang struct sgx_epc_page *epc_page = encl->secs.epc_page;
246*c6c2adcbSHaitao Huang
247*c6c2adcbSHaitao Huang if (!epc_page)
248*c6c2adcbSHaitao Huang epc_page = sgx_encl_eldu(&encl->secs, NULL);
249*c6c2adcbSHaitao Huang
250*c6c2adcbSHaitao Huang return epc_page;
251*c6c2adcbSHaitao Huang }
252*c6c2adcbSHaitao Huang
__sgx_encl_load_page(struct sgx_encl * encl,struct sgx_encl_page * entry)253b3fb517dSReinette Chatre static struct sgx_encl_page *__sgx_encl_load_page(struct sgx_encl *encl,
254b3fb517dSReinette Chatre struct sgx_encl_page *entry)
2553fe0778eSJarkko Sakkinen {
2561728ab54SJarkko Sakkinen struct sgx_epc_page *epc_page;
2573fe0778eSJarkko Sakkinen
2583fe0778eSJarkko Sakkinen /* Entry successfully located. */
2591728ab54SJarkko Sakkinen if (entry->epc_page) {
2601728ab54SJarkko Sakkinen if (entry->desc & SGX_ENCL_PAGE_BEING_RECLAIMED)
2611728ab54SJarkko Sakkinen return ERR_PTR(-EBUSY);
2621728ab54SJarkko Sakkinen
2631728ab54SJarkko Sakkinen return entry;
2641728ab54SJarkko Sakkinen }
2651728ab54SJarkko Sakkinen
266*c6c2adcbSHaitao Huang epc_page = sgx_encl_load_secs(encl);
2671728ab54SJarkko Sakkinen if (IS_ERR(epc_page))
2681728ab54SJarkko Sakkinen return ERR_CAST(epc_page);
2691728ab54SJarkko Sakkinen
2701728ab54SJarkko Sakkinen epc_page = sgx_encl_eldu(entry, encl->secs.epc_page);
2711728ab54SJarkko Sakkinen if (IS_ERR(epc_page))
2721728ab54SJarkko Sakkinen return ERR_CAST(epc_page);
2731728ab54SJarkko Sakkinen
2741728ab54SJarkko Sakkinen encl->secs_child_cnt++;
2751728ab54SJarkko Sakkinen sgx_mark_page_reclaimable(entry->epc_page);
2761728ab54SJarkko Sakkinen
2773fe0778eSJarkko Sakkinen return entry;
2783fe0778eSJarkko Sakkinen }
2793fe0778eSJarkko Sakkinen
sgx_encl_load_page_in_vma(struct sgx_encl * encl,unsigned long addr,unsigned long vm_flags)280b3fb517dSReinette Chatre static struct sgx_encl_page *sgx_encl_load_page_in_vma(struct sgx_encl *encl,
281b3fb517dSReinette Chatre unsigned long addr,
282b3fb517dSReinette Chatre unsigned long vm_flags)
283b3fb517dSReinette Chatre {
2844f20566fSKefeng Wang unsigned long vm_prot_bits = vm_flags & VM_ACCESS_FLAGS;
285b3fb517dSReinette Chatre struct sgx_encl_page *entry;
286b3fb517dSReinette Chatre
287b3fb517dSReinette Chatre entry = xa_load(&encl->page_array, PFN_DOWN(addr));
288b3fb517dSReinette Chatre if (!entry)
289b3fb517dSReinette Chatre return ERR_PTR(-EFAULT);
290b3fb517dSReinette Chatre
291b3fb517dSReinette Chatre /*
292b3fb517dSReinette Chatre * Verify that the page has equal or higher build time
293b3fb517dSReinette Chatre * permissions than the VMA permissions (i.e. the subset of {VM_READ,
294b3fb517dSReinette Chatre * VM_WRITE, VM_EXECUTE} in vma->vm_flags).
295b3fb517dSReinette Chatre */
296b3fb517dSReinette Chatre if ((entry->vm_max_prot_bits & vm_prot_bits) != vm_prot_bits)
297b3fb517dSReinette Chatre return ERR_PTR(-EFAULT);
298b3fb517dSReinette Chatre
299b3fb517dSReinette Chatre return __sgx_encl_load_page(encl, entry);
300b3fb517dSReinette Chatre }
301b3fb517dSReinette Chatre
sgx_encl_load_page(struct sgx_encl * encl,unsigned long addr)302b3fb517dSReinette Chatre struct sgx_encl_page *sgx_encl_load_page(struct sgx_encl *encl,
303b3fb517dSReinette Chatre unsigned long addr)
304b3fb517dSReinette Chatre {
305b3fb517dSReinette Chatre struct sgx_encl_page *entry;
306b3fb517dSReinette Chatre
307b3fb517dSReinette Chatre entry = xa_load(&encl->page_array, PFN_DOWN(addr));
308b3fb517dSReinette Chatre if (!entry)
309b3fb517dSReinette Chatre return ERR_PTR(-EFAULT);
310b3fb517dSReinette Chatre
311b3fb517dSReinette Chatre return __sgx_encl_load_page(encl, entry);
312b3fb517dSReinette Chatre }
313b3fb517dSReinette Chatre
3145a90d2c3SReinette Chatre /**
3155a90d2c3SReinette Chatre * sgx_encl_eaug_page() - Dynamically add page to initialized enclave
3165a90d2c3SReinette Chatre * @vma: VMA obtained from fault info from where page is accessed
3175a90d2c3SReinette Chatre * @encl: enclave accessing the page
3185a90d2c3SReinette Chatre * @addr: address that triggered the page fault
3195a90d2c3SReinette Chatre *
3205a90d2c3SReinette Chatre * When an initialized enclave accesses a page with no backing EPC page
3215a90d2c3SReinette Chatre * on a SGX2 system then the EPC can be added dynamically via the SGX2
3225a90d2c3SReinette Chatre * ENCLS[EAUG] instruction.
3235a90d2c3SReinette Chatre *
3245a90d2c3SReinette Chatre * Returns: Appropriate vm_fault_t: VM_FAULT_NOPAGE when PTE was installed
3255a90d2c3SReinette Chatre * successfully, VM_FAULT_SIGBUS or VM_FAULT_OOM as error otherwise.
3265a90d2c3SReinette Chatre */
sgx_encl_eaug_page(struct vm_area_struct * vma,struct sgx_encl * encl,unsigned long addr)3275a90d2c3SReinette Chatre static vm_fault_t sgx_encl_eaug_page(struct vm_area_struct *vma,
3285a90d2c3SReinette Chatre struct sgx_encl *encl, unsigned long addr)
3295a90d2c3SReinette Chatre {
3305a90d2c3SReinette Chatre vm_fault_t vmret = VM_FAULT_SIGBUS;
3315a90d2c3SReinette Chatre struct sgx_pageinfo pginfo = {0};
3325a90d2c3SReinette Chatre struct sgx_encl_page *encl_page;
3335a90d2c3SReinette Chatre struct sgx_epc_page *epc_page;
3345a90d2c3SReinette Chatre struct sgx_va_page *va_page;
3355a90d2c3SReinette Chatre unsigned long phys_addr;
3365a90d2c3SReinette Chatre u64 secinfo_flags;
3375a90d2c3SReinette Chatre int ret;
3385a90d2c3SReinette Chatre
3395a90d2c3SReinette Chatre if (!test_bit(SGX_ENCL_INITIALIZED, &encl->flags))
3405a90d2c3SReinette Chatre return VM_FAULT_SIGBUS;
3415a90d2c3SReinette Chatre
3425a90d2c3SReinette Chatre /*
3435a90d2c3SReinette Chatre * Ignore internal permission checking for dynamically added pages.
3445a90d2c3SReinette Chatre * They matter only for data added during the pre-initialization
3455a90d2c3SReinette Chatre * phase. The enclave decides the permissions by the means of
3465a90d2c3SReinette Chatre * EACCEPT, EACCEPTCOPY and EMODPE.
3475a90d2c3SReinette Chatre */
3485a90d2c3SReinette Chatre secinfo_flags = SGX_SECINFO_R | SGX_SECINFO_W | SGX_SECINFO_X;
3495a90d2c3SReinette Chatre encl_page = sgx_encl_page_alloc(encl, addr - encl->base, secinfo_flags);
3505a90d2c3SReinette Chatre if (IS_ERR(encl_page))
3515a90d2c3SReinette Chatre return VM_FAULT_OOM;
3525a90d2c3SReinette Chatre
3535a90d2c3SReinette Chatre mutex_lock(&encl->lock);
3545a90d2c3SReinette Chatre
355*c6c2adcbSHaitao Huang epc_page = sgx_encl_load_secs(encl);
356*c6c2adcbSHaitao Huang if (IS_ERR(epc_page)) {
357*c6c2adcbSHaitao Huang if (PTR_ERR(epc_page) == -EBUSY)
358*c6c2adcbSHaitao Huang vmret = VM_FAULT_NOPAGE;
359*c6c2adcbSHaitao Huang goto err_out_unlock;
360*c6c2adcbSHaitao Huang }
361*c6c2adcbSHaitao Huang
3625a90d2c3SReinette Chatre epc_page = sgx_alloc_epc_page(encl_page, false);
3635a90d2c3SReinette Chatre if (IS_ERR(epc_page)) {
3645a90d2c3SReinette Chatre if (PTR_ERR(epc_page) == -EBUSY)
3655a90d2c3SReinette Chatre vmret = VM_FAULT_NOPAGE;
3665a90d2c3SReinette Chatre goto err_out_unlock;
3675a90d2c3SReinette Chatre }
3685a90d2c3SReinette Chatre
3695a90d2c3SReinette Chatre va_page = sgx_encl_grow(encl, false);
37081fa6fd1SHaitao Huang if (IS_ERR(va_page)) {
37181fa6fd1SHaitao Huang if (PTR_ERR(va_page) == -EBUSY)
37281fa6fd1SHaitao Huang vmret = VM_FAULT_NOPAGE;
3735a90d2c3SReinette Chatre goto err_out_epc;
37481fa6fd1SHaitao Huang }
3755a90d2c3SReinette Chatre
3765a90d2c3SReinette Chatre if (va_page)
3775a90d2c3SReinette Chatre list_add(&va_page->list, &encl->va_pages);
3785a90d2c3SReinette Chatre
3795a90d2c3SReinette Chatre ret = xa_insert(&encl->page_array, PFN_DOWN(encl_page->desc),
3805a90d2c3SReinette Chatre encl_page, GFP_KERNEL);
3815a90d2c3SReinette Chatre /*
3825a90d2c3SReinette Chatre * If ret == -EBUSY then page was created in another flow while
3835a90d2c3SReinette Chatre * running without encl->lock
3845a90d2c3SReinette Chatre */
3855a90d2c3SReinette Chatre if (ret)
3865a90d2c3SReinette Chatre goto err_out_shrink;
3875a90d2c3SReinette Chatre
3885a90d2c3SReinette Chatre pginfo.secs = (unsigned long)sgx_get_epc_virt_addr(encl->secs.epc_page);
3895a90d2c3SReinette Chatre pginfo.addr = encl_page->desc & PAGE_MASK;
3905a90d2c3SReinette Chatre pginfo.metadata = 0;
3915a90d2c3SReinette Chatre
3925a90d2c3SReinette Chatre ret = __eaug(&pginfo, sgx_get_epc_virt_addr(epc_page));
3935a90d2c3SReinette Chatre if (ret)
3945a90d2c3SReinette Chatre goto err_out;
3955a90d2c3SReinette Chatre
3965a90d2c3SReinette Chatre encl_page->encl = encl;
3975a90d2c3SReinette Chatre encl_page->epc_page = epc_page;
3985a90d2c3SReinette Chatre encl_page->type = SGX_PAGE_TYPE_REG;
3995a90d2c3SReinette Chatre encl->secs_child_cnt++;
4005a90d2c3SReinette Chatre
4015a90d2c3SReinette Chatre sgx_mark_page_reclaimable(encl_page->epc_page);
4025a90d2c3SReinette Chatre
4035a90d2c3SReinette Chatre phys_addr = sgx_get_epc_phys_addr(epc_page);
4045a90d2c3SReinette Chatre /*
4055a90d2c3SReinette Chatre * Do not undo everything when creating PTE entry fails - next #PF
4065a90d2c3SReinette Chatre * would find page ready for a PTE.
4075a90d2c3SReinette Chatre */
4085a90d2c3SReinette Chatre vmret = vmf_insert_pfn(vma, addr, PFN_DOWN(phys_addr));
4095a90d2c3SReinette Chatre if (vmret != VM_FAULT_NOPAGE) {
4105a90d2c3SReinette Chatre mutex_unlock(&encl->lock);
4115a90d2c3SReinette Chatre return VM_FAULT_SIGBUS;
4125a90d2c3SReinette Chatre }
4135a90d2c3SReinette Chatre mutex_unlock(&encl->lock);
4145a90d2c3SReinette Chatre return VM_FAULT_NOPAGE;
4155a90d2c3SReinette Chatre
4165a90d2c3SReinette Chatre err_out:
4175a90d2c3SReinette Chatre xa_erase(&encl->page_array, PFN_DOWN(encl_page->desc));
4185a90d2c3SReinette Chatre
4195a90d2c3SReinette Chatre err_out_shrink:
4205a90d2c3SReinette Chatre sgx_encl_shrink(encl, va_page);
4215a90d2c3SReinette Chatre err_out_epc:
4225a90d2c3SReinette Chatre sgx_encl_free_epc_page(epc_page);
4235a90d2c3SReinette Chatre err_out_unlock:
4245a90d2c3SReinette Chatre mutex_unlock(&encl->lock);
4255a90d2c3SReinette Chatre kfree(encl_page);
4265a90d2c3SReinette Chatre
4275a90d2c3SReinette Chatre return vmret;
4285a90d2c3SReinette Chatre }
4295a90d2c3SReinette Chatre
sgx_vma_fault(struct vm_fault * vmf)4303fe0778eSJarkko Sakkinen static vm_fault_t sgx_vma_fault(struct vm_fault *vmf)
4313fe0778eSJarkko Sakkinen {
4323fe0778eSJarkko Sakkinen unsigned long addr = (unsigned long)vmf->address;
4333fe0778eSJarkko Sakkinen struct vm_area_struct *vma = vmf->vma;
4343fe0778eSJarkko Sakkinen struct sgx_encl_page *entry;
4353fe0778eSJarkko Sakkinen unsigned long phys_addr;
4363fe0778eSJarkko Sakkinen struct sgx_encl *encl;
4373fe0778eSJarkko Sakkinen vm_fault_t ret;
4383fe0778eSJarkko Sakkinen
4393fe0778eSJarkko Sakkinen encl = vma->vm_private_data;
4403fe0778eSJarkko Sakkinen
4411728ab54SJarkko Sakkinen /*
4421728ab54SJarkko Sakkinen * It's very unlikely but possible that allocating memory for the
4431728ab54SJarkko Sakkinen * mm_list entry of a forked process failed in sgx_vma_open(). When
4441728ab54SJarkko Sakkinen * this happens, vm_private_data is set to NULL.
4451728ab54SJarkko Sakkinen */
4461728ab54SJarkko Sakkinen if (unlikely(!encl))
4471728ab54SJarkko Sakkinen return VM_FAULT_SIGBUS;
4481728ab54SJarkko Sakkinen
4495a90d2c3SReinette Chatre /*
4505a90d2c3SReinette Chatre * The page_array keeps track of all enclave pages, whether they
4515a90d2c3SReinette Chatre * are swapped out or not. If there is no entry for this page and
4525a90d2c3SReinette Chatre * the system supports SGX2 then it is possible to dynamically add
4535a90d2c3SReinette Chatre * a new enclave page. This is only possible for an initialized
4545a90d2c3SReinette Chatre * enclave that will be checked for right away.
4555a90d2c3SReinette Chatre */
4565a90d2c3SReinette Chatre if (cpu_feature_enabled(X86_FEATURE_SGX2) &&
4575a90d2c3SReinette Chatre (!xa_load(&encl->page_array, PFN_DOWN(addr))))
4585a90d2c3SReinette Chatre return sgx_encl_eaug_page(vma, encl, addr);
4595a90d2c3SReinette Chatre
4603fe0778eSJarkko Sakkinen mutex_lock(&encl->lock);
4613fe0778eSJarkko Sakkinen
462b3fb517dSReinette Chatre entry = sgx_encl_load_page_in_vma(encl, addr, vma->vm_flags);
4633fe0778eSJarkko Sakkinen if (IS_ERR(entry)) {
4643fe0778eSJarkko Sakkinen mutex_unlock(&encl->lock);
4653fe0778eSJarkko Sakkinen
4661728ab54SJarkko Sakkinen if (PTR_ERR(entry) == -EBUSY)
4671728ab54SJarkko Sakkinen return VM_FAULT_NOPAGE;
4681728ab54SJarkko Sakkinen
4693fe0778eSJarkko Sakkinen return VM_FAULT_SIGBUS;
4703fe0778eSJarkko Sakkinen }
4713fe0778eSJarkko Sakkinen
4723fe0778eSJarkko Sakkinen phys_addr = sgx_get_epc_phys_addr(entry->epc_page);
4733fe0778eSJarkko Sakkinen
4743fe0778eSJarkko Sakkinen ret = vmf_insert_pfn(vma, addr, PFN_DOWN(phys_addr));
4753fe0778eSJarkko Sakkinen if (ret != VM_FAULT_NOPAGE) {
4763fe0778eSJarkko Sakkinen mutex_unlock(&encl->lock);
4773fe0778eSJarkko Sakkinen
4783fe0778eSJarkko Sakkinen return VM_FAULT_SIGBUS;
4793fe0778eSJarkko Sakkinen }
4803fe0778eSJarkko Sakkinen
4811728ab54SJarkko Sakkinen sgx_encl_test_and_clear_young(vma->vm_mm, entry);
4823fe0778eSJarkko Sakkinen mutex_unlock(&encl->lock);
4833fe0778eSJarkko Sakkinen
4843fe0778eSJarkko Sakkinen return VM_FAULT_NOPAGE;
4853fe0778eSJarkko Sakkinen }
4863fe0778eSJarkko Sakkinen
sgx_vma_open(struct vm_area_struct * vma)4871728ab54SJarkko Sakkinen static void sgx_vma_open(struct vm_area_struct *vma)
4881728ab54SJarkko Sakkinen {
4891728ab54SJarkko Sakkinen struct sgx_encl *encl = vma->vm_private_data;
4901728ab54SJarkko Sakkinen
4911728ab54SJarkko Sakkinen /*
4921728ab54SJarkko Sakkinen * It's possible but unlikely that vm_private_data is NULL. This can
4931728ab54SJarkko Sakkinen * happen in a grandchild of a process, when sgx_encl_mm_add() had
4941728ab54SJarkko Sakkinen * failed to allocate memory in this callback.
4951728ab54SJarkko Sakkinen */
4961728ab54SJarkko Sakkinen if (unlikely(!encl))
4971728ab54SJarkko Sakkinen return;
4981728ab54SJarkko Sakkinen
4991728ab54SJarkko Sakkinen if (sgx_encl_mm_add(encl, vma->vm_mm))
5001728ab54SJarkko Sakkinen vma->vm_private_data = NULL;
5011728ab54SJarkko Sakkinen }
5021728ab54SJarkko Sakkinen
5031728ab54SJarkko Sakkinen
5043fe0778eSJarkko Sakkinen /**
5053fe0778eSJarkko Sakkinen * sgx_encl_may_map() - Check if a requested VMA mapping is allowed
5063fe0778eSJarkko Sakkinen * @encl: an enclave pointer
5073fe0778eSJarkko Sakkinen * @start: lower bound of the address range, inclusive
5083fe0778eSJarkko Sakkinen * @end: upper bound of the address range, exclusive
5093fe0778eSJarkko Sakkinen * @vm_flags: VMA flags
5103fe0778eSJarkko Sakkinen *
5113fe0778eSJarkko Sakkinen * Iterate through the enclave pages contained within [@start, @end) to verify
5123fe0778eSJarkko Sakkinen * that the permissions requested by a subset of {VM_READ, VM_WRITE, VM_EXEC}
5133fe0778eSJarkko Sakkinen * do not contain any permissions that are not contained in the build time
5143fe0778eSJarkko Sakkinen * permissions of any of the enclave pages within the given address range.
5153fe0778eSJarkko Sakkinen *
5163fe0778eSJarkko Sakkinen * An enclave creator must declare the strongest permissions that will be
5173fe0778eSJarkko Sakkinen * needed for each enclave page. This ensures that mappings have the identical
5183fe0778eSJarkko Sakkinen * or weaker permissions than the earlier declared permissions.
5193fe0778eSJarkko Sakkinen *
5203fe0778eSJarkko Sakkinen * Return: 0 on success, -EACCES otherwise
5213fe0778eSJarkko Sakkinen */
sgx_encl_may_map(struct sgx_encl * encl,unsigned long start,unsigned long end,unsigned long vm_flags)5223fe0778eSJarkko Sakkinen int sgx_encl_may_map(struct sgx_encl *encl, unsigned long start,
5233fe0778eSJarkko Sakkinen unsigned long end, unsigned long vm_flags)
5243fe0778eSJarkko Sakkinen {
5254f20566fSKefeng Wang unsigned long vm_prot_bits = vm_flags & VM_ACCESS_FLAGS;
5263fe0778eSJarkko Sakkinen struct sgx_encl_page *page;
5273fe0778eSJarkko Sakkinen unsigned long count = 0;
5283fe0778eSJarkko Sakkinen int ret = 0;
5293fe0778eSJarkko Sakkinen
5303fe0778eSJarkko Sakkinen XA_STATE(xas, &encl->page_array, PFN_DOWN(start));
5313fe0778eSJarkko Sakkinen
5327b013e72SReinette Chatre /* Disallow mapping outside enclave's address range. */
5337b013e72SReinette Chatre if (test_bit(SGX_ENCL_INITIALIZED, &encl->flags) &&
5347b013e72SReinette Chatre (start < encl->base || end > encl->base + encl->size))
5357b013e72SReinette Chatre return -EACCES;
5367b013e72SReinette Chatre
5373fe0778eSJarkko Sakkinen /*
5383fe0778eSJarkko Sakkinen * Disallow READ_IMPLIES_EXEC tasks as their VMA permissions might
5393fe0778eSJarkko Sakkinen * conflict with the enclave page permissions.
5403fe0778eSJarkko Sakkinen */
5413fe0778eSJarkko Sakkinen if (current->personality & READ_IMPLIES_EXEC)
5423fe0778eSJarkko Sakkinen return -EACCES;
5433fe0778eSJarkko Sakkinen
5443fe0778eSJarkko Sakkinen mutex_lock(&encl->lock);
5453fe0778eSJarkko Sakkinen xas_lock(&xas);
5463fe0778eSJarkko Sakkinen xas_for_each(&xas, page, PFN_DOWN(end - 1)) {
5473fe0778eSJarkko Sakkinen if (~page->vm_max_prot_bits & vm_prot_bits) {
5483fe0778eSJarkko Sakkinen ret = -EACCES;
5493fe0778eSJarkko Sakkinen break;
5503fe0778eSJarkko Sakkinen }
5513fe0778eSJarkko Sakkinen
5523fe0778eSJarkko Sakkinen /* Reschedule on every XA_CHECK_SCHED iteration. */
5533fe0778eSJarkko Sakkinen if (!(++count % XA_CHECK_SCHED)) {
5543fe0778eSJarkko Sakkinen xas_pause(&xas);
5553fe0778eSJarkko Sakkinen xas_unlock(&xas);
5563fe0778eSJarkko Sakkinen mutex_unlock(&encl->lock);
5573fe0778eSJarkko Sakkinen
5583fe0778eSJarkko Sakkinen cond_resched();
5593fe0778eSJarkko Sakkinen
5603fe0778eSJarkko Sakkinen mutex_lock(&encl->lock);
5613fe0778eSJarkko Sakkinen xas_lock(&xas);
5623fe0778eSJarkko Sakkinen }
5633fe0778eSJarkko Sakkinen }
5643fe0778eSJarkko Sakkinen xas_unlock(&xas);
5653fe0778eSJarkko Sakkinen mutex_unlock(&encl->lock);
5663fe0778eSJarkko Sakkinen
5673fe0778eSJarkko Sakkinen return ret;
5683fe0778eSJarkko Sakkinen }
5693fe0778eSJarkko Sakkinen
sgx_vma_mprotect(struct vm_area_struct * vma,unsigned long start,unsigned long end,unsigned long newflags)5703fe0778eSJarkko Sakkinen static int sgx_vma_mprotect(struct vm_area_struct *vma, unsigned long start,
5713fe0778eSJarkko Sakkinen unsigned long end, unsigned long newflags)
5723fe0778eSJarkko Sakkinen {
5733fe0778eSJarkko Sakkinen return sgx_encl_may_map(vma->vm_private_data, start, end, newflags);
5743fe0778eSJarkko Sakkinen }
5753fe0778eSJarkko Sakkinen
sgx_encl_debug_read(struct sgx_encl * encl,struct sgx_encl_page * page,unsigned long addr,void * data)576947c6e11SJarkko Sakkinen static int sgx_encl_debug_read(struct sgx_encl *encl, struct sgx_encl_page *page,
577947c6e11SJarkko Sakkinen unsigned long addr, void *data)
578947c6e11SJarkko Sakkinen {
579947c6e11SJarkko Sakkinen unsigned long offset = addr & ~PAGE_MASK;
580947c6e11SJarkko Sakkinen int ret;
581947c6e11SJarkko Sakkinen
582947c6e11SJarkko Sakkinen
583947c6e11SJarkko Sakkinen ret = __edbgrd(sgx_get_epc_virt_addr(page->epc_page) + offset, data);
584947c6e11SJarkko Sakkinen if (ret)
585947c6e11SJarkko Sakkinen return -EIO;
586947c6e11SJarkko Sakkinen
587947c6e11SJarkko Sakkinen return 0;
588947c6e11SJarkko Sakkinen }
589947c6e11SJarkko Sakkinen
sgx_encl_debug_write(struct sgx_encl * encl,struct sgx_encl_page * page,unsigned long addr,void * data)590947c6e11SJarkko Sakkinen static int sgx_encl_debug_write(struct sgx_encl *encl, struct sgx_encl_page *page,
591947c6e11SJarkko Sakkinen unsigned long addr, void *data)
592947c6e11SJarkko Sakkinen {
593947c6e11SJarkko Sakkinen unsigned long offset = addr & ~PAGE_MASK;
594947c6e11SJarkko Sakkinen int ret;
595947c6e11SJarkko Sakkinen
596947c6e11SJarkko Sakkinen ret = __edbgwr(sgx_get_epc_virt_addr(page->epc_page) + offset, data);
597947c6e11SJarkko Sakkinen if (ret)
598947c6e11SJarkko Sakkinen return -EIO;
599947c6e11SJarkko Sakkinen
600947c6e11SJarkko Sakkinen return 0;
601947c6e11SJarkko Sakkinen }
602947c6e11SJarkko Sakkinen
603947c6e11SJarkko Sakkinen /*
604947c6e11SJarkko Sakkinen * Load an enclave page to EPC if required, and take encl->lock.
605947c6e11SJarkko Sakkinen */
sgx_encl_reserve_page(struct sgx_encl * encl,unsigned long addr,unsigned long vm_flags)606947c6e11SJarkko Sakkinen static struct sgx_encl_page *sgx_encl_reserve_page(struct sgx_encl *encl,
607947c6e11SJarkko Sakkinen unsigned long addr,
608947c6e11SJarkko Sakkinen unsigned long vm_flags)
609947c6e11SJarkko Sakkinen {
610947c6e11SJarkko Sakkinen struct sgx_encl_page *entry;
611947c6e11SJarkko Sakkinen
612947c6e11SJarkko Sakkinen for ( ; ; ) {
613947c6e11SJarkko Sakkinen mutex_lock(&encl->lock);
614947c6e11SJarkko Sakkinen
615b3fb517dSReinette Chatre entry = sgx_encl_load_page_in_vma(encl, addr, vm_flags);
616947c6e11SJarkko Sakkinen if (PTR_ERR(entry) != -EBUSY)
617947c6e11SJarkko Sakkinen break;
618947c6e11SJarkko Sakkinen
619947c6e11SJarkko Sakkinen mutex_unlock(&encl->lock);
620947c6e11SJarkko Sakkinen }
621947c6e11SJarkko Sakkinen
622947c6e11SJarkko Sakkinen if (IS_ERR(entry))
623947c6e11SJarkko Sakkinen mutex_unlock(&encl->lock);
624947c6e11SJarkko Sakkinen
625947c6e11SJarkko Sakkinen return entry;
626947c6e11SJarkko Sakkinen }
627947c6e11SJarkko Sakkinen
sgx_vma_access(struct vm_area_struct * vma,unsigned long addr,void * buf,int len,int write)628947c6e11SJarkko Sakkinen static int sgx_vma_access(struct vm_area_struct *vma, unsigned long addr,
629947c6e11SJarkko Sakkinen void *buf, int len, int write)
630947c6e11SJarkko Sakkinen {
631947c6e11SJarkko Sakkinen struct sgx_encl *encl = vma->vm_private_data;
632947c6e11SJarkko Sakkinen struct sgx_encl_page *entry = NULL;
633947c6e11SJarkko Sakkinen char data[sizeof(unsigned long)];
634947c6e11SJarkko Sakkinen unsigned long align;
635947c6e11SJarkko Sakkinen int offset;
636947c6e11SJarkko Sakkinen int cnt;
637947c6e11SJarkko Sakkinen int ret = 0;
638947c6e11SJarkko Sakkinen int i;
639947c6e11SJarkko Sakkinen
640947c6e11SJarkko Sakkinen /*
641947c6e11SJarkko Sakkinen * If process was forked, VMA is still there but vm_private_data is set
642947c6e11SJarkko Sakkinen * to NULL.
643947c6e11SJarkko Sakkinen */
644947c6e11SJarkko Sakkinen if (!encl)
645947c6e11SJarkko Sakkinen return -EFAULT;
646947c6e11SJarkko Sakkinen
647947c6e11SJarkko Sakkinen if (!test_bit(SGX_ENCL_DEBUG, &encl->flags))
648947c6e11SJarkko Sakkinen return -EFAULT;
649947c6e11SJarkko Sakkinen
650947c6e11SJarkko Sakkinen for (i = 0; i < len; i += cnt) {
651947c6e11SJarkko Sakkinen entry = sgx_encl_reserve_page(encl, (addr + i) & PAGE_MASK,
652947c6e11SJarkko Sakkinen vma->vm_flags);
653947c6e11SJarkko Sakkinen if (IS_ERR(entry)) {
654947c6e11SJarkko Sakkinen ret = PTR_ERR(entry);
655947c6e11SJarkko Sakkinen break;
656947c6e11SJarkko Sakkinen }
657947c6e11SJarkko Sakkinen
658947c6e11SJarkko Sakkinen align = ALIGN_DOWN(addr + i, sizeof(unsigned long));
659947c6e11SJarkko Sakkinen offset = (addr + i) & (sizeof(unsigned long) - 1);
660947c6e11SJarkko Sakkinen cnt = sizeof(unsigned long) - offset;
661947c6e11SJarkko Sakkinen cnt = min(cnt, len - i);
662947c6e11SJarkko Sakkinen
663947c6e11SJarkko Sakkinen ret = sgx_encl_debug_read(encl, entry, align, data);
664947c6e11SJarkko Sakkinen if (ret)
665947c6e11SJarkko Sakkinen goto out;
666947c6e11SJarkko Sakkinen
667947c6e11SJarkko Sakkinen if (write) {
668947c6e11SJarkko Sakkinen memcpy(data + offset, buf + i, cnt);
669947c6e11SJarkko Sakkinen ret = sgx_encl_debug_write(encl, entry, align, data);
670947c6e11SJarkko Sakkinen if (ret)
671947c6e11SJarkko Sakkinen goto out;
672947c6e11SJarkko Sakkinen } else {
673947c6e11SJarkko Sakkinen memcpy(buf + i, data + offset, cnt);
674947c6e11SJarkko Sakkinen }
675947c6e11SJarkko Sakkinen
676947c6e11SJarkko Sakkinen out:
677947c6e11SJarkko Sakkinen mutex_unlock(&encl->lock);
678947c6e11SJarkko Sakkinen
679947c6e11SJarkko Sakkinen if (ret)
680947c6e11SJarkko Sakkinen break;
681947c6e11SJarkko Sakkinen }
682947c6e11SJarkko Sakkinen
683947c6e11SJarkko Sakkinen return ret < 0 ? ret : i;
684947c6e11SJarkko Sakkinen }
685947c6e11SJarkko Sakkinen
6863fe0778eSJarkko Sakkinen const struct vm_operations_struct sgx_vm_ops = {
6873fe0778eSJarkko Sakkinen .fault = sgx_vma_fault,
6883fe0778eSJarkko Sakkinen .mprotect = sgx_vma_mprotect,
6891728ab54SJarkko Sakkinen .open = sgx_vma_open,
690947c6e11SJarkko Sakkinen .access = sgx_vma_access,
6913fe0778eSJarkko Sakkinen };
6921728ab54SJarkko Sakkinen
6931728ab54SJarkko Sakkinen /**
6941728ab54SJarkko Sakkinen * sgx_encl_release - Destroy an enclave instance
6951d315639SChenXiaoSong * @ref: address of a kref inside &sgx_encl
6961728ab54SJarkko Sakkinen *
6971728ab54SJarkko Sakkinen * Used together with kref_put(). Frees all the resources associated with the
6981728ab54SJarkko Sakkinen * enclave and the instance itself.
6991728ab54SJarkko Sakkinen */
sgx_encl_release(struct kref * ref)7001728ab54SJarkko Sakkinen void sgx_encl_release(struct kref *ref)
7011728ab54SJarkko Sakkinen {
7021728ab54SJarkko Sakkinen struct sgx_encl *encl = container_of(ref, struct sgx_encl, refcount);
7037b72c823SReinette Chatre unsigned long max_page_index = PFN_DOWN(encl->base + encl->size - 1);
7041728ab54SJarkko Sakkinen struct sgx_va_page *va_page;
7051728ab54SJarkko Sakkinen struct sgx_encl_page *entry;
7067b72c823SReinette Chatre unsigned long count = 0;
7071728ab54SJarkko Sakkinen
7087b72c823SReinette Chatre XA_STATE(xas, &encl->page_array, PFN_DOWN(encl->base));
7097b72c823SReinette Chatre
7107b72c823SReinette Chatre xas_lock(&xas);
7117b72c823SReinette Chatre xas_for_each(&xas, entry, max_page_index) {
7121728ab54SJarkko Sakkinen if (entry->epc_page) {
7131728ab54SJarkko Sakkinen /*
7141728ab54SJarkko Sakkinen * The page and its radix tree entry cannot be freed
7151728ab54SJarkko Sakkinen * if the page is being held by the reclaimer.
7161728ab54SJarkko Sakkinen */
7171728ab54SJarkko Sakkinen if (sgx_unmark_page_reclaimable(entry->epc_page))
7181728ab54SJarkko Sakkinen continue;
7191728ab54SJarkko Sakkinen
720b0c7459bSKai Huang sgx_encl_free_epc_page(entry->epc_page);
7211728ab54SJarkko Sakkinen encl->secs_child_cnt--;
7221728ab54SJarkko Sakkinen entry->epc_page = NULL;
7231728ab54SJarkko Sakkinen }
7241728ab54SJarkko Sakkinen
7251728ab54SJarkko Sakkinen kfree(entry);
7267b72c823SReinette Chatre /*
7277b72c823SReinette Chatre * Invoke scheduler on every XA_CHECK_SCHED iteration
7287b72c823SReinette Chatre * to prevent soft lockups.
7297b72c823SReinette Chatre */
7307b72c823SReinette Chatre if (!(++count % XA_CHECK_SCHED)) {
7317b72c823SReinette Chatre xas_pause(&xas);
7327b72c823SReinette Chatre xas_unlock(&xas);
7337b72c823SReinette Chatre
7348795359eSReinette Chatre cond_resched();
7357b72c823SReinette Chatre
7367b72c823SReinette Chatre xas_lock(&xas);
7371728ab54SJarkko Sakkinen }
7387b72c823SReinette Chatre }
7397b72c823SReinette Chatre xas_unlock(&xas);
7401728ab54SJarkko Sakkinen
7411728ab54SJarkko Sakkinen xa_destroy(&encl->page_array);
7421728ab54SJarkko Sakkinen
7431728ab54SJarkko Sakkinen if (!encl->secs_child_cnt && encl->secs.epc_page) {
744b0c7459bSKai Huang sgx_encl_free_epc_page(encl->secs.epc_page);
7451728ab54SJarkko Sakkinen encl->secs.epc_page = NULL;
7461728ab54SJarkko Sakkinen }
7471728ab54SJarkko Sakkinen
7481728ab54SJarkko Sakkinen while (!list_empty(&encl->va_pages)) {
7491728ab54SJarkko Sakkinen va_page = list_first_entry(&encl->va_pages, struct sgx_va_page,
7501728ab54SJarkko Sakkinen list);
7511728ab54SJarkko Sakkinen list_del(&va_page->list);
752b0c7459bSKai Huang sgx_encl_free_epc_page(va_page->epc_page);
7531728ab54SJarkko Sakkinen kfree(va_page);
7541728ab54SJarkko Sakkinen }
7551728ab54SJarkko Sakkinen
7561728ab54SJarkko Sakkinen if (encl->backing)
7571728ab54SJarkko Sakkinen fput(encl->backing);
7581728ab54SJarkko Sakkinen
7591728ab54SJarkko Sakkinen cleanup_srcu_struct(&encl->srcu);
7601728ab54SJarkko Sakkinen
7611728ab54SJarkko Sakkinen WARN_ON_ONCE(!list_empty(&encl->mm_list));
7621728ab54SJarkko Sakkinen
7631728ab54SJarkko Sakkinen /* Detect EPC page leak's. */
7641728ab54SJarkko Sakkinen WARN_ON_ONCE(encl->secs_child_cnt);
7651728ab54SJarkko Sakkinen WARN_ON_ONCE(encl->secs.epc_page);
7661728ab54SJarkko Sakkinen
7671728ab54SJarkko Sakkinen kfree(encl);
7681728ab54SJarkko Sakkinen }
7691728ab54SJarkko Sakkinen
7701728ab54SJarkko Sakkinen /*
7711728ab54SJarkko Sakkinen * 'mm' is exiting and no longer needs mmu notifications.
7721728ab54SJarkko Sakkinen */
sgx_mmu_notifier_release(struct mmu_notifier * mn,struct mm_struct * mm)7731728ab54SJarkko Sakkinen static void sgx_mmu_notifier_release(struct mmu_notifier *mn,
7741728ab54SJarkko Sakkinen struct mm_struct *mm)
7751728ab54SJarkko Sakkinen {
7761728ab54SJarkko Sakkinen struct sgx_encl_mm *encl_mm = container_of(mn, struct sgx_encl_mm, mmu_notifier);
7771728ab54SJarkko Sakkinen struct sgx_encl_mm *tmp = NULL;
7781e327963SJakob Koschel bool found = false;
7791728ab54SJarkko Sakkinen
7801728ab54SJarkko Sakkinen /*
7811728ab54SJarkko Sakkinen * The enclave itself can remove encl_mm. Note, objects can't be moved
7821728ab54SJarkko Sakkinen * off an RCU protected list, but deletion is ok.
7831728ab54SJarkko Sakkinen */
7841728ab54SJarkko Sakkinen spin_lock(&encl_mm->encl->mm_lock);
7851728ab54SJarkko Sakkinen list_for_each_entry(tmp, &encl_mm->encl->mm_list, list) {
7861728ab54SJarkko Sakkinen if (tmp == encl_mm) {
7871728ab54SJarkko Sakkinen list_del_rcu(&encl_mm->list);
7881e327963SJakob Koschel found = true;
7891728ab54SJarkko Sakkinen break;
7901728ab54SJarkko Sakkinen }
7911728ab54SJarkko Sakkinen }
7921728ab54SJarkko Sakkinen spin_unlock(&encl_mm->encl->mm_lock);
7931728ab54SJarkko Sakkinen
7941e327963SJakob Koschel if (found) {
7951728ab54SJarkko Sakkinen synchronize_srcu(&encl_mm->encl->srcu);
7961728ab54SJarkko Sakkinen mmu_notifier_put(mn);
7971728ab54SJarkko Sakkinen }
7981728ab54SJarkko Sakkinen }
7991728ab54SJarkko Sakkinen
sgx_mmu_notifier_free(struct mmu_notifier * mn)8001728ab54SJarkko Sakkinen static void sgx_mmu_notifier_free(struct mmu_notifier *mn)
8011728ab54SJarkko Sakkinen {
8021728ab54SJarkko Sakkinen struct sgx_encl_mm *encl_mm = container_of(mn, struct sgx_encl_mm, mmu_notifier);
8031728ab54SJarkko Sakkinen
8042ade0d60SJarkko Sakkinen /* 'encl_mm' is going away, put encl_mm->encl reference: */
8052ade0d60SJarkko Sakkinen kref_put(&encl_mm->encl->refcount, sgx_encl_release);
8062ade0d60SJarkko Sakkinen
8071728ab54SJarkko Sakkinen kfree(encl_mm);
8081728ab54SJarkko Sakkinen }
8091728ab54SJarkko Sakkinen
8101728ab54SJarkko Sakkinen static const struct mmu_notifier_ops sgx_mmu_notifier_ops = {
8111728ab54SJarkko Sakkinen .release = sgx_mmu_notifier_release,
8121728ab54SJarkko Sakkinen .free_notifier = sgx_mmu_notifier_free,
8131728ab54SJarkko Sakkinen };
8141728ab54SJarkko Sakkinen
sgx_encl_find_mm(struct sgx_encl * encl,struct mm_struct * mm)8151728ab54SJarkko Sakkinen static struct sgx_encl_mm *sgx_encl_find_mm(struct sgx_encl *encl,
8161728ab54SJarkko Sakkinen struct mm_struct *mm)
8171728ab54SJarkko Sakkinen {
8181728ab54SJarkko Sakkinen struct sgx_encl_mm *encl_mm = NULL;
8191728ab54SJarkko Sakkinen struct sgx_encl_mm *tmp;
8201728ab54SJarkko Sakkinen int idx;
8211728ab54SJarkko Sakkinen
8221728ab54SJarkko Sakkinen idx = srcu_read_lock(&encl->srcu);
8231728ab54SJarkko Sakkinen
8241728ab54SJarkko Sakkinen list_for_each_entry_rcu(tmp, &encl->mm_list, list) {
8251728ab54SJarkko Sakkinen if (tmp->mm == mm) {
8261728ab54SJarkko Sakkinen encl_mm = tmp;
8271728ab54SJarkko Sakkinen break;
8281728ab54SJarkko Sakkinen }
8291728ab54SJarkko Sakkinen }
8301728ab54SJarkko Sakkinen
8311728ab54SJarkko Sakkinen srcu_read_unlock(&encl->srcu, idx);
8321728ab54SJarkko Sakkinen
8331728ab54SJarkko Sakkinen return encl_mm;
8341728ab54SJarkko Sakkinen }
8351728ab54SJarkko Sakkinen
sgx_encl_mm_add(struct sgx_encl * encl,struct mm_struct * mm)8361728ab54SJarkko Sakkinen int sgx_encl_mm_add(struct sgx_encl *encl, struct mm_struct *mm)
8371728ab54SJarkko Sakkinen {
8381728ab54SJarkko Sakkinen struct sgx_encl_mm *encl_mm;
8391728ab54SJarkko Sakkinen int ret;
8401728ab54SJarkko Sakkinen
8411728ab54SJarkko Sakkinen /*
8421728ab54SJarkko Sakkinen * Even though a single enclave may be mapped into an mm more than once,
8431728ab54SJarkko Sakkinen * each 'mm' only appears once on encl->mm_list. This is guaranteed by
8441728ab54SJarkko Sakkinen * holding the mm's mmap lock for write before an mm can be added or
8451728ab54SJarkko Sakkinen * remove to an encl->mm_list.
8461728ab54SJarkko Sakkinen */
8471728ab54SJarkko Sakkinen mmap_assert_write_locked(mm);
8481728ab54SJarkko Sakkinen
8491728ab54SJarkko Sakkinen /*
8501728ab54SJarkko Sakkinen * It's possible that an entry already exists in the mm_list, because it
8511728ab54SJarkko Sakkinen * is removed only on VFS release or process exit.
8521728ab54SJarkko Sakkinen */
8531728ab54SJarkko Sakkinen if (sgx_encl_find_mm(encl, mm))
8541728ab54SJarkko Sakkinen return 0;
8551728ab54SJarkko Sakkinen
8561728ab54SJarkko Sakkinen encl_mm = kzalloc(sizeof(*encl_mm), GFP_KERNEL);
8571728ab54SJarkko Sakkinen if (!encl_mm)
8581728ab54SJarkko Sakkinen return -ENOMEM;
8591728ab54SJarkko Sakkinen
8602ade0d60SJarkko Sakkinen /* Grab a refcount for the encl_mm->encl reference: */
8612ade0d60SJarkko Sakkinen kref_get(&encl->refcount);
8621728ab54SJarkko Sakkinen encl_mm->encl = encl;
8631728ab54SJarkko Sakkinen encl_mm->mm = mm;
8641728ab54SJarkko Sakkinen encl_mm->mmu_notifier.ops = &sgx_mmu_notifier_ops;
8651728ab54SJarkko Sakkinen
8661728ab54SJarkko Sakkinen ret = __mmu_notifier_register(&encl_mm->mmu_notifier, mm);
8671728ab54SJarkko Sakkinen if (ret) {
8681728ab54SJarkko Sakkinen kfree(encl_mm);
8691728ab54SJarkko Sakkinen return ret;
8701728ab54SJarkko Sakkinen }
8711728ab54SJarkko Sakkinen
8721728ab54SJarkko Sakkinen spin_lock(&encl->mm_lock);
8731728ab54SJarkko Sakkinen list_add_rcu(&encl_mm->list, &encl->mm_list);
874f89c2f9bSReinette Chatre /* Pairs with smp_rmb() in sgx_zap_enclave_ptes(). */
8751728ab54SJarkko Sakkinen smp_wmb();
8761728ab54SJarkko Sakkinen encl->mm_list_version++;
8771728ab54SJarkko Sakkinen spin_unlock(&encl->mm_lock);
8781728ab54SJarkko Sakkinen
8791728ab54SJarkko Sakkinen return 0;
8801728ab54SJarkko Sakkinen }
8811728ab54SJarkko Sakkinen
8827f391752SReinette Chatre /**
883bdaa8799SReinette Chatre * sgx_encl_cpumask() - Query which CPUs might be accessing the enclave
8847f391752SReinette Chatre * @encl: the enclave
8857f391752SReinette Chatre *
8867f391752SReinette Chatre * Some SGX functions require that no cached linear-to-physical address
8877f391752SReinette Chatre * mappings are present before they can succeed. For example, ENCLS[EWB]
8887f391752SReinette Chatre * copies a page from the enclave page cache to regular main memory but
8897f391752SReinette Chatre * it fails if it cannot ensure that there are no cached
8907f391752SReinette Chatre * linear-to-physical address mappings referring to the page.
8917f391752SReinette Chatre *
8927f391752SReinette Chatre * SGX hardware flushes all cached linear-to-physical mappings on a CPU
8937f391752SReinette Chatre * when an enclave is exited via ENCLU[EEXIT] or an Asynchronous Enclave
8947f391752SReinette Chatre * Exit (AEX). Exiting an enclave will thus ensure cached linear-to-physical
8957f391752SReinette Chatre * address mappings are cleared but coordination with the tracking done within
8967f391752SReinette Chatre * the SGX hardware is needed to support the SGX functions that depend on this
8977f391752SReinette Chatre * cache clearing.
8987f391752SReinette Chatre *
8997f391752SReinette Chatre * When the ENCLS[ETRACK] function is issued on an enclave the hardware
9007f391752SReinette Chatre * tracks threads operating inside the enclave at that time. The SGX
9017f391752SReinette Chatre * hardware tracking require that all the identified threads must have
9027f391752SReinette Chatre * exited the enclave in order to flush the mappings before a function such
9037f391752SReinette Chatre * as ENCLS[EWB] will be permitted
9047f391752SReinette Chatre *
9057f391752SReinette Chatre * The following flow is used to support SGX functions that require that
9067f391752SReinette Chatre * no cached linear-to-physical address mappings are present:
9077f391752SReinette Chatre * 1) Execute ENCLS[ETRACK] to initiate hardware tracking.
908bdaa8799SReinette Chatre * 2) Use this function (sgx_encl_cpumask()) to query which CPUs might be
9097f391752SReinette Chatre * accessing the enclave.
9107f391752SReinette Chatre * 3) Send IPI to identified CPUs, kicking them out of the enclave and
9117f391752SReinette Chatre * thus flushing all locally cached linear-to-physical address mappings.
9127f391752SReinette Chatre * 4) Execute SGX function.
9137f391752SReinette Chatre *
9147f391752SReinette Chatre * Context: It is required to call this function after ENCLS[ETRACK].
9157f391752SReinette Chatre * This will ensure that if any new mm appears (racing with
9167f391752SReinette Chatre * sgx_encl_mm_add()) then the new mm will enter into the
9177f391752SReinette Chatre * enclave with fresh linear-to-physical address mappings.
9187f391752SReinette Chatre *
9197f391752SReinette Chatre * It is required that all IPIs are completed before a new
9207f391752SReinette Chatre * ENCLS[ETRACK] is issued so be sure to protect steps 1 to 3
9217f391752SReinette Chatre * of the above flow with the enclave's mutex.
9227f391752SReinette Chatre *
9237f391752SReinette Chatre * Return: cpumask of CPUs that might be accessing @encl
9247f391752SReinette Chatre */
sgx_encl_cpumask(struct sgx_encl * encl)925bdaa8799SReinette Chatre const cpumask_t *sgx_encl_cpumask(struct sgx_encl *encl)
9267f391752SReinette Chatre {
9277f391752SReinette Chatre cpumask_t *cpumask = &encl->cpumask;
9287f391752SReinette Chatre struct sgx_encl_mm *encl_mm;
9297f391752SReinette Chatre int idx;
9307f391752SReinette Chatre
9317f391752SReinette Chatre cpumask_clear(cpumask);
9327f391752SReinette Chatre
9337f391752SReinette Chatre idx = srcu_read_lock(&encl->srcu);
9347f391752SReinette Chatre
9357f391752SReinette Chatre list_for_each_entry_rcu(encl_mm, &encl->mm_list, list) {
9367f391752SReinette Chatre if (!mmget_not_zero(encl_mm->mm))
9377f391752SReinette Chatre continue;
9387f391752SReinette Chatre
9397f391752SReinette Chatre cpumask_or(cpumask, cpumask, mm_cpumask(encl_mm->mm));
9407f391752SReinette Chatre
9417f391752SReinette Chatre mmput_async(encl_mm->mm);
9427f391752SReinette Chatre }
9437f391752SReinette Chatre
9447f391752SReinette Chatre srcu_read_unlock(&encl->srcu, idx);
9457f391752SReinette Chatre
9467f391752SReinette Chatre return cpumask;
9477f391752SReinette Chatre }
9487f391752SReinette Chatre
sgx_encl_get_backing_page(struct sgx_encl * encl,pgoff_t index)9491728ab54SJarkko Sakkinen static struct page *sgx_encl_get_backing_page(struct sgx_encl *encl,
9501728ab54SJarkko Sakkinen pgoff_t index)
9511728ab54SJarkko Sakkinen {
952235185b8SAl Viro struct address_space *mapping = encl->backing->f_mapping;
9531728ab54SJarkko Sakkinen gfp_t gfpmask = mapping_gfp_mask(mapping);
9541728ab54SJarkko Sakkinen
9551728ab54SJarkko Sakkinen return shmem_read_mapping_page_gfp(mapping, index, gfpmask);
9561728ab54SJarkko Sakkinen }
9571728ab54SJarkko Sakkinen
9581728ab54SJarkko Sakkinen /**
959ee56a283SKristen Carlson Accardi * __sgx_encl_get_backing() - Pin the backing storage
9601728ab54SJarkko Sakkinen * @encl: an enclave pointer
9611728ab54SJarkko Sakkinen * @page_index: enclave page index
9621728ab54SJarkko Sakkinen * @backing: data for accessing backing storage for the page
9631728ab54SJarkko Sakkinen *
9641728ab54SJarkko Sakkinen * Pin the backing storage pages for storing the encrypted contents and Paging
9651728ab54SJarkko Sakkinen * Crypto MetaData (PCMD) of an enclave page.
9661728ab54SJarkko Sakkinen *
9671728ab54SJarkko Sakkinen * Return:
9681728ab54SJarkko Sakkinen * 0 on success,
9691728ab54SJarkko Sakkinen * -errno otherwise.
9701728ab54SJarkko Sakkinen */
__sgx_encl_get_backing(struct sgx_encl * encl,unsigned long page_index,struct sgx_backing * backing)971ee56a283SKristen Carlson Accardi static int __sgx_encl_get_backing(struct sgx_encl *encl, unsigned long page_index,
9721728ab54SJarkko Sakkinen struct sgx_backing *backing)
9731728ab54SJarkko Sakkinen {
97408999b24SJarkko Sakkinen pgoff_t page_pcmd_off = sgx_encl_get_backing_page_pcmd_offset(encl, page_index);
9751728ab54SJarkko Sakkinen struct page *contents;
9761728ab54SJarkko Sakkinen struct page *pcmd;
9771728ab54SJarkko Sakkinen
9781728ab54SJarkko Sakkinen contents = sgx_encl_get_backing_page(encl, page_index);
9791728ab54SJarkko Sakkinen if (IS_ERR(contents))
9801728ab54SJarkko Sakkinen return PTR_ERR(contents);
9811728ab54SJarkko Sakkinen
98208999b24SJarkko Sakkinen pcmd = sgx_encl_get_backing_page(encl, PFN_DOWN(page_pcmd_off));
9831728ab54SJarkko Sakkinen if (IS_ERR(pcmd)) {
9841728ab54SJarkko Sakkinen put_page(contents);
9851728ab54SJarkko Sakkinen return PTR_ERR(pcmd);
9861728ab54SJarkko Sakkinen }
9871728ab54SJarkko Sakkinen
9881728ab54SJarkko Sakkinen backing->contents = contents;
9891728ab54SJarkko Sakkinen backing->pcmd = pcmd;
99008999b24SJarkko Sakkinen backing->pcmd_offset = page_pcmd_off & (PAGE_SIZE - 1);
9911728ab54SJarkko Sakkinen
9921728ab54SJarkko Sakkinen return 0;
9931728ab54SJarkko Sakkinen }
9941728ab54SJarkko Sakkinen
9950c9782e2SKristen Carlson Accardi /*
9960c9782e2SKristen Carlson Accardi * When called from ksgxd, returns the mem_cgroup of a struct mm stored
9970c9782e2SKristen Carlson Accardi * in the enclave's mm_list. When not called from ksgxd, just returns
9980c9782e2SKristen Carlson Accardi * the mem_cgroup of the current task.
9990c9782e2SKristen Carlson Accardi */
sgx_encl_get_mem_cgroup(struct sgx_encl * encl)10000c9782e2SKristen Carlson Accardi static struct mem_cgroup *sgx_encl_get_mem_cgroup(struct sgx_encl *encl)
10010c9782e2SKristen Carlson Accardi {
10020c9782e2SKristen Carlson Accardi struct mem_cgroup *memcg = NULL;
10030c9782e2SKristen Carlson Accardi struct sgx_encl_mm *encl_mm;
10040c9782e2SKristen Carlson Accardi int idx;
10050c9782e2SKristen Carlson Accardi
10060c9782e2SKristen Carlson Accardi /*
10070c9782e2SKristen Carlson Accardi * If called from normal task context, return the mem_cgroup
10080c9782e2SKristen Carlson Accardi * of the current task's mm. The remainder of the handling is for
10090c9782e2SKristen Carlson Accardi * ksgxd.
10100c9782e2SKristen Carlson Accardi */
10110c9782e2SKristen Carlson Accardi if (!current_is_ksgxd())
10120c9782e2SKristen Carlson Accardi return get_mem_cgroup_from_mm(current->mm);
10130c9782e2SKristen Carlson Accardi
10140c9782e2SKristen Carlson Accardi /*
10150c9782e2SKristen Carlson Accardi * Search the enclave's mm_list to find an mm associated with
10160c9782e2SKristen Carlson Accardi * this enclave to charge the allocation to.
10170c9782e2SKristen Carlson Accardi */
10180c9782e2SKristen Carlson Accardi idx = srcu_read_lock(&encl->srcu);
10190c9782e2SKristen Carlson Accardi
10200c9782e2SKristen Carlson Accardi list_for_each_entry_rcu(encl_mm, &encl->mm_list, list) {
10210c9782e2SKristen Carlson Accardi if (!mmget_not_zero(encl_mm->mm))
10220c9782e2SKristen Carlson Accardi continue;
10230c9782e2SKristen Carlson Accardi
10240c9782e2SKristen Carlson Accardi memcg = get_mem_cgroup_from_mm(encl_mm->mm);
10250c9782e2SKristen Carlson Accardi
10260c9782e2SKristen Carlson Accardi mmput_async(encl_mm->mm);
10270c9782e2SKristen Carlson Accardi
10280c9782e2SKristen Carlson Accardi break;
10290c9782e2SKristen Carlson Accardi }
10300c9782e2SKristen Carlson Accardi
10310c9782e2SKristen Carlson Accardi srcu_read_unlock(&encl->srcu, idx);
10320c9782e2SKristen Carlson Accardi
10330c9782e2SKristen Carlson Accardi /*
10340c9782e2SKristen Carlson Accardi * In the rare case that there isn't an mm associated with
10350c9782e2SKristen Carlson Accardi * the enclave, set memcg to the current active mem_cgroup.
10360c9782e2SKristen Carlson Accardi * This will be the root mem_cgroup if there is no active
10370c9782e2SKristen Carlson Accardi * mem_cgroup.
10380c9782e2SKristen Carlson Accardi */
10390c9782e2SKristen Carlson Accardi if (!memcg)
10400c9782e2SKristen Carlson Accardi return get_mem_cgroup_from_mm(NULL);
10410c9782e2SKristen Carlson Accardi
10420c9782e2SKristen Carlson Accardi return memcg;
10430c9782e2SKristen Carlson Accardi }
10440c9782e2SKristen Carlson Accardi
10450c9782e2SKristen Carlson Accardi /**
1046ee56a283SKristen Carlson Accardi * sgx_encl_alloc_backing() - create a new backing storage page
10470c9782e2SKristen Carlson Accardi * @encl: an enclave pointer
10480c9782e2SKristen Carlson Accardi * @page_index: enclave page index
10490c9782e2SKristen Carlson Accardi * @backing: data for accessing backing storage for the page
10500c9782e2SKristen Carlson Accardi *
10510c9782e2SKristen Carlson Accardi * When called from ksgxd, sets the active memcg from one of the
10520c9782e2SKristen Carlson Accardi * mms in the enclave's mm_list prior to any backing page allocation,
10530c9782e2SKristen Carlson Accardi * in order to ensure that shmem page allocations are charged to the
1054ee56a283SKristen Carlson Accardi * enclave. Create a backing page for loading data back into an EPC page with
1055ee56a283SKristen Carlson Accardi * ELDU. This function takes a reference on a new backing page which
1056ee56a283SKristen Carlson Accardi * must be dropped with a corresponding call to sgx_encl_put_backing().
10570c9782e2SKristen Carlson Accardi *
10580c9782e2SKristen Carlson Accardi * Return:
10590c9782e2SKristen Carlson Accardi * 0 on success,
10600c9782e2SKristen Carlson Accardi * -errno otherwise.
10610c9782e2SKristen Carlson Accardi */
sgx_encl_alloc_backing(struct sgx_encl * encl,unsigned long page_index,struct sgx_backing * backing)10620c9782e2SKristen Carlson Accardi int sgx_encl_alloc_backing(struct sgx_encl *encl, unsigned long page_index,
10630c9782e2SKristen Carlson Accardi struct sgx_backing *backing)
10640c9782e2SKristen Carlson Accardi {
10650c9782e2SKristen Carlson Accardi struct mem_cgroup *encl_memcg = sgx_encl_get_mem_cgroup(encl);
10660c9782e2SKristen Carlson Accardi struct mem_cgroup *memcg = set_active_memcg(encl_memcg);
10670c9782e2SKristen Carlson Accardi int ret;
10680c9782e2SKristen Carlson Accardi
1069ee56a283SKristen Carlson Accardi ret = __sgx_encl_get_backing(encl, page_index, backing);
10700c9782e2SKristen Carlson Accardi
10710c9782e2SKristen Carlson Accardi set_active_memcg(memcg);
10720c9782e2SKristen Carlson Accardi mem_cgroup_put(encl_memcg);
10730c9782e2SKristen Carlson Accardi
10740c9782e2SKristen Carlson Accardi return ret;
10750c9782e2SKristen Carlson Accardi }
10760c9782e2SKristen Carlson Accardi
10770c9782e2SKristen Carlson Accardi /**
10780c9782e2SKristen Carlson Accardi * sgx_encl_lookup_backing() - retrieve an existing backing storage page
10790c9782e2SKristen Carlson Accardi * @encl: an enclave pointer
10800c9782e2SKristen Carlson Accardi * @page_index: enclave page index
10810c9782e2SKristen Carlson Accardi * @backing: data for accessing backing storage for the page
10820c9782e2SKristen Carlson Accardi *
10830c9782e2SKristen Carlson Accardi * Retrieve a backing page for loading data back into an EPC page with ELDU.
10840c9782e2SKristen Carlson Accardi * It is the caller's responsibility to ensure that it is appropriate to use
10850c9782e2SKristen Carlson Accardi * sgx_encl_lookup_backing() rather than sgx_encl_alloc_backing(). If lookup is
10860c9782e2SKristen Carlson Accardi * not used correctly, this will cause an allocation which is not accounted for.
1087ee56a283SKristen Carlson Accardi * This function takes a reference on an existing backing page which must be
1088ee56a283SKristen Carlson Accardi * dropped with a corresponding call to sgx_encl_put_backing().
10890c9782e2SKristen Carlson Accardi *
10900c9782e2SKristen Carlson Accardi * Return:
10910c9782e2SKristen Carlson Accardi * 0 on success,
10920c9782e2SKristen Carlson Accardi * -errno otherwise.
10930c9782e2SKristen Carlson Accardi */
sgx_encl_lookup_backing(struct sgx_encl * encl,unsigned long page_index,struct sgx_backing * backing)1094ee56a283SKristen Carlson Accardi static int sgx_encl_lookup_backing(struct sgx_encl *encl, unsigned long page_index,
10950c9782e2SKristen Carlson Accardi struct sgx_backing *backing)
10960c9782e2SKristen Carlson Accardi {
1097ee56a283SKristen Carlson Accardi return __sgx_encl_get_backing(encl, page_index, backing);
10980c9782e2SKristen Carlson Accardi }
10990c9782e2SKristen Carlson Accardi
11001728ab54SJarkko Sakkinen /**
11011728ab54SJarkko Sakkinen * sgx_encl_put_backing() - Unpin the backing storage
11021728ab54SJarkko Sakkinen * @backing: data for accessing backing storage for the page
11031728ab54SJarkko Sakkinen */
sgx_encl_put_backing(struct sgx_backing * backing)11046bd42964SReinette Chatre void sgx_encl_put_backing(struct sgx_backing *backing)
11051728ab54SJarkko Sakkinen {
11061728ab54SJarkko Sakkinen put_page(backing->pcmd);
11071728ab54SJarkko Sakkinen put_page(backing->contents);
11081728ab54SJarkko Sakkinen }
11091728ab54SJarkko Sakkinen
sgx_encl_test_and_clear_young_cb(pte_t * ptep,unsigned long addr,void * data)11101728ab54SJarkko Sakkinen static int sgx_encl_test_and_clear_young_cb(pte_t *ptep, unsigned long addr,
11111728ab54SJarkko Sakkinen void *data)
11121728ab54SJarkko Sakkinen {
11131728ab54SJarkko Sakkinen pte_t pte;
11141728ab54SJarkko Sakkinen int ret;
11151728ab54SJarkko Sakkinen
11161728ab54SJarkko Sakkinen ret = pte_young(*ptep);
11171728ab54SJarkko Sakkinen if (ret) {
11181728ab54SJarkko Sakkinen pte = pte_mkold(*ptep);
11191728ab54SJarkko Sakkinen set_pte_at((struct mm_struct *)data, addr, ptep, pte);
11201728ab54SJarkko Sakkinen }
11211728ab54SJarkko Sakkinen
11221728ab54SJarkko Sakkinen return ret;
11231728ab54SJarkko Sakkinen }
11241728ab54SJarkko Sakkinen
11251728ab54SJarkko Sakkinen /**
11261728ab54SJarkko Sakkinen * sgx_encl_test_and_clear_young() - Test and reset the accessed bit
11271728ab54SJarkko Sakkinen * @mm: mm_struct that is checked
11281728ab54SJarkko Sakkinen * @page: enclave page to be tested for recent access
11291728ab54SJarkko Sakkinen *
11301728ab54SJarkko Sakkinen * Checks the Access (A) bit from the PTE corresponding to the enclave page and
11311728ab54SJarkko Sakkinen * clears it.
11321728ab54SJarkko Sakkinen *
11331728ab54SJarkko Sakkinen * Return: 1 if the page has been recently accessed and 0 if not.
11341728ab54SJarkko Sakkinen */
sgx_encl_test_and_clear_young(struct mm_struct * mm,struct sgx_encl_page * page)11351728ab54SJarkko Sakkinen int sgx_encl_test_and_clear_young(struct mm_struct *mm,
11361728ab54SJarkko Sakkinen struct sgx_encl_page *page)
11371728ab54SJarkko Sakkinen {
11381728ab54SJarkko Sakkinen unsigned long addr = page->desc & PAGE_MASK;
11391728ab54SJarkko Sakkinen struct sgx_encl *encl = page->encl;
11401728ab54SJarkko Sakkinen struct vm_area_struct *vma;
11411728ab54SJarkko Sakkinen int ret;
11421728ab54SJarkko Sakkinen
11431728ab54SJarkko Sakkinen ret = sgx_encl_find(mm, addr, &vma);
11441728ab54SJarkko Sakkinen if (ret)
11451728ab54SJarkko Sakkinen return 0;
11461728ab54SJarkko Sakkinen
11471728ab54SJarkko Sakkinen if (encl != vma->vm_private_data)
11481728ab54SJarkko Sakkinen return 0;
11491728ab54SJarkko Sakkinen
11501728ab54SJarkko Sakkinen ret = apply_to_page_range(vma->vm_mm, addr, PAGE_SIZE,
11511728ab54SJarkko Sakkinen sgx_encl_test_and_clear_young_cb, vma->vm_mm);
11521728ab54SJarkko Sakkinen if (ret < 0)
11531728ab54SJarkko Sakkinen return 0;
11541728ab54SJarkko Sakkinen
11551728ab54SJarkko Sakkinen return ret;
11561728ab54SJarkko Sakkinen }
11571728ab54SJarkko Sakkinen
sgx_encl_page_alloc(struct sgx_encl * encl,unsigned long offset,u64 secinfo_flags)11588123073cSJarkko Sakkinen struct sgx_encl_page *sgx_encl_page_alloc(struct sgx_encl *encl,
11598123073cSJarkko Sakkinen unsigned long offset,
11608123073cSJarkko Sakkinen u64 secinfo_flags)
11618123073cSJarkko Sakkinen {
11628123073cSJarkko Sakkinen struct sgx_encl_page *encl_page;
11638123073cSJarkko Sakkinen unsigned long prot;
11648123073cSJarkko Sakkinen
11658123073cSJarkko Sakkinen encl_page = kzalloc(sizeof(*encl_page), GFP_KERNEL);
11668123073cSJarkko Sakkinen if (!encl_page)
11678123073cSJarkko Sakkinen return ERR_PTR(-ENOMEM);
11688123073cSJarkko Sakkinen
11698123073cSJarkko Sakkinen encl_page->desc = encl->base + offset;
11708123073cSJarkko Sakkinen encl_page->encl = encl;
11718123073cSJarkko Sakkinen
11728123073cSJarkko Sakkinen prot = _calc_vm_trans(secinfo_flags, SGX_SECINFO_R, PROT_READ) |
11738123073cSJarkko Sakkinen _calc_vm_trans(secinfo_flags, SGX_SECINFO_W, PROT_WRITE) |
11748123073cSJarkko Sakkinen _calc_vm_trans(secinfo_flags, SGX_SECINFO_X, PROT_EXEC);
11758123073cSJarkko Sakkinen
11768123073cSJarkko Sakkinen /*
11778123073cSJarkko Sakkinen * TCS pages must always RW set for CPU access while the SECINFO
11788123073cSJarkko Sakkinen * permissions are *always* zero - the CPU ignores the user provided
11798123073cSJarkko Sakkinen * values and silently overwrites them with zero permissions.
11808123073cSJarkko Sakkinen */
11818123073cSJarkko Sakkinen if ((secinfo_flags & SGX_SECINFO_PAGE_TYPE_MASK) == SGX_SECINFO_TCS)
11828123073cSJarkko Sakkinen prot |= PROT_READ | PROT_WRITE;
11838123073cSJarkko Sakkinen
11848123073cSJarkko Sakkinen /* Calculate maximum of the VM flags for the page. */
11858123073cSJarkko Sakkinen encl_page->vm_max_prot_bits = calc_vm_prot_bits(prot, 0);
11868123073cSJarkko Sakkinen
11878123073cSJarkko Sakkinen return encl_page;
11888123073cSJarkko Sakkinen }
11898123073cSJarkko Sakkinen
11901728ab54SJarkko Sakkinen /**
1191f89c2f9bSReinette Chatre * sgx_zap_enclave_ptes() - remove PTEs mapping the address from enclave
1192f89c2f9bSReinette Chatre * @encl: the enclave
1193f89c2f9bSReinette Chatre * @addr: page aligned pointer to single page for which PTEs will be removed
1194f89c2f9bSReinette Chatre *
1195f89c2f9bSReinette Chatre * Multiple VMAs may have an enclave page mapped. Remove the PTE mapping
1196f89c2f9bSReinette Chatre * @addr from each VMA. Ensure that page fault handler is ready to handle
1197f89c2f9bSReinette Chatre * new mappings of @addr before calling this function.
1198f89c2f9bSReinette Chatre */
sgx_zap_enclave_ptes(struct sgx_encl * encl,unsigned long addr)1199f89c2f9bSReinette Chatre void sgx_zap_enclave_ptes(struct sgx_encl *encl, unsigned long addr)
1200f89c2f9bSReinette Chatre {
1201f89c2f9bSReinette Chatre unsigned long mm_list_version;
1202f89c2f9bSReinette Chatre struct sgx_encl_mm *encl_mm;
1203f89c2f9bSReinette Chatre struct vm_area_struct *vma;
1204f89c2f9bSReinette Chatre int idx, ret;
1205f89c2f9bSReinette Chatre
1206f89c2f9bSReinette Chatre do {
1207f89c2f9bSReinette Chatre mm_list_version = encl->mm_list_version;
1208f89c2f9bSReinette Chatre
1209f89c2f9bSReinette Chatre /* Pairs with smp_wmb() in sgx_encl_mm_add(). */
1210f89c2f9bSReinette Chatre smp_rmb();
1211f89c2f9bSReinette Chatre
1212f89c2f9bSReinette Chatre idx = srcu_read_lock(&encl->srcu);
1213f89c2f9bSReinette Chatre
1214f89c2f9bSReinette Chatre list_for_each_entry_rcu(encl_mm, &encl->mm_list, list) {
1215f89c2f9bSReinette Chatre if (!mmget_not_zero(encl_mm->mm))
1216f89c2f9bSReinette Chatre continue;
1217f89c2f9bSReinette Chatre
1218f89c2f9bSReinette Chatre mmap_read_lock(encl_mm->mm);
1219f89c2f9bSReinette Chatre
1220f89c2f9bSReinette Chatre ret = sgx_encl_find(encl_mm->mm, addr, &vma);
1221f89c2f9bSReinette Chatre if (!ret && encl == vma->vm_private_data)
1222f89c2f9bSReinette Chatre zap_vma_ptes(vma, addr, PAGE_SIZE);
1223f89c2f9bSReinette Chatre
1224f89c2f9bSReinette Chatre mmap_read_unlock(encl_mm->mm);
1225f89c2f9bSReinette Chatre
1226f89c2f9bSReinette Chatre mmput_async(encl_mm->mm);
1227f89c2f9bSReinette Chatre }
1228f89c2f9bSReinette Chatre
1229f89c2f9bSReinette Chatre srcu_read_unlock(&encl->srcu, idx);
1230f89c2f9bSReinette Chatre } while (unlikely(encl->mm_list_version != mm_list_version));
1231f89c2f9bSReinette Chatre }
1232f89c2f9bSReinette Chatre
12331728ab54SJarkko Sakkinen /**
12341728ab54SJarkko Sakkinen * sgx_alloc_va_page() - Allocate a Version Array (VA) page
1235a76e7f1fSReinette Chatre * @reclaim: Reclaim EPC pages directly if none available. Enclave
1236a76e7f1fSReinette Chatre * mutex should not be held if this is set.
12371728ab54SJarkko Sakkinen *
12381728ab54SJarkko Sakkinen * Allocate a free EPC page and convert it to a Version Array (VA) page.
12391728ab54SJarkko Sakkinen *
12401728ab54SJarkko Sakkinen * Return:
12411728ab54SJarkko Sakkinen * a VA page,
12421728ab54SJarkko Sakkinen * -errno otherwise
12431728ab54SJarkko Sakkinen */
sgx_alloc_va_page(bool reclaim)1244a76e7f1fSReinette Chatre struct sgx_epc_page *sgx_alloc_va_page(bool reclaim)
12451728ab54SJarkko Sakkinen {
12461728ab54SJarkko Sakkinen struct sgx_epc_page *epc_page;
12471728ab54SJarkko Sakkinen int ret;
12481728ab54SJarkko Sakkinen
1249a76e7f1fSReinette Chatre epc_page = sgx_alloc_epc_page(NULL, reclaim);
12501728ab54SJarkko Sakkinen if (IS_ERR(epc_page))
12511728ab54SJarkko Sakkinen return ERR_CAST(epc_page);
12521728ab54SJarkko Sakkinen
12531728ab54SJarkko Sakkinen ret = __epa(sgx_get_epc_virt_addr(epc_page));
12541728ab54SJarkko Sakkinen if (ret) {
12551728ab54SJarkko Sakkinen WARN_ONCE(1, "EPA returned %d (0x%x)", ret, ret);
1256b0c7459bSKai Huang sgx_encl_free_epc_page(epc_page);
12571728ab54SJarkko Sakkinen return ERR_PTR(-EFAULT);
12581728ab54SJarkko Sakkinen }
12591728ab54SJarkko Sakkinen
12601728ab54SJarkko Sakkinen return epc_page;
12611728ab54SJarkko Sakkinen }
12621728ab54SJarkko Sakkinen
12631728ab54SJarkko Sakkinen /**
12641728ab54SJarkko Sakkinen * sgx_alloc_va_slot - allocate a VA slot
12651728ab54SJarkko Sakkinen * @va_page: a &struct sgx_va_page instance
12661728ab54SJarkko Sakkinen *
12671728ab54SJarkko Sakkinen * Allocates a slot from a &struct sgx_va_page instance.
12681728ab54SJarkko Sakkinen *
12691728ab54SJarkko Sakkinen * Return: offset of the slot inside the VA page
12701728ab54SJarkko Sakkinen */
sgx_alloc_va_slot(struct sgx_va_page * va_page)12711728ab54SJarkko Sakkinen unsigned int sgx_alloc_va_slot(struct sgx_va_page *va_page)
12721728ab54SJarkko Sakkinen {
12731728ab54SJarkko Sakkinen int slot = find_first_zero_bit(va_page->slots, SGX_VA_SLOT_COUNT);
12741728ab54SJarkko Sakkinen
12751728ab54SJarkko Sakkinen if (slot < SGX_VA_SLOT_COUNT)
12761728ab54SJarkko Sakkinen set_bit(slot, va_page->slots);
12771728ab54SJarkko Sakkinen
12781728ab54SJarkko Sakkinen return slot << 3;
12791728ab54SJarkko Sakkinen }
12801728ab54SJarkko Sakkinen
12811728ab54SJarkko Sakkinen /**
12821728ab54SJarkko Sakkinen * sgx_free_va_slot - free a VA slot
12831728ab54SJarkko Sakkinen * @va_page: a &struct sgx_va_page instance
12841728ab54SJarkko Sakkinen * @offset: offset of the slot inside the VA page
12851728ab54SJarkko Sakkinen *
12861728ab54SJarkko Sakkinen * Frees a slot from a &struct sgx_va_page instance.
12871728ab54SJarkko Sakkinen */
sgx_free_va_slot(struct sgx_va_page * va_page,unsigned int offset)12881728ab54SJarkko Sakkinen void sgx_free_va_slot(struct sgx_va_page *va_page, unsigned int offset)
12891728ab54SJarkko Sakkinen {
12901728ab54SJarkko Sakkinen clear_bit(offset >> 3, va_page->slots);
12911728ab54SJarkko Sakkinen }
12921728ab54SJarkko Sakkinen
12931728ab54SJarkko Sakkinen /**
12941728ab54SJarkko Sakkinen * sgx_va_page_full - is the VA page full?
12951728ab54SJarkko Sakkinen * @va_page: a &struct sgx_va_page instance
12961728ab54SJarkko Sakkinen *
12971728ab54SJarkko Sakkinen * Return: true if all slots have been taken
12981728ab54SJarkko Sakkinen */
sgx_va_page_full(struct sgx_va_page * va_page)12991728ab54SJarkko Sakkinen bool sgx_va_page_full(struct sgx_va_page *va_page)
13001728ab54SJarkko Sakkinen {
13011728ab54SJarkko Sakkinen int slot = find_first_zero_bit(va_page->slots, SGX_VA_SLOT_COUNT);
13021728ab54SJarkko Sakkinen
13031728ab54SJarkko Sakkinen return slot == SGX_VA_SLOT_COUNT;
13041728ab54SJarkko Sakkinen }
1305b0c7459bSKai Huang
1306b0c7459bSKai Huang /**
1307b0c7459bSKai Huang * sgx_encl_free_epc_page - free an EPC page assigned to an enclave
1308b0c7459bSKai Huang * @page: EPC page to be freed
1309b0c7459bSKai Huang *
1310b0c7459bSKai Huang * Free an EPC page assigned to an enclave. It does EREMOVE for the page, and
1311b0c7459bSKai Huang * only upon success, it puts the page back to free page list. Otherwise, it
1312b0c7459bSKai Huang * gives a WARNING to indicate page is leaked.
1313b0c7459bSKai Huang */
sgx_encl_free_epc_page(struct sgx_epc_page * page)1314b0c7459bSKai Huang void sgx_encl_free_epc_page(struct sgx_epc_page *page)
1315b0c7459bSKai Huang {
1316b0c7459bSKai Huang int ret;
1317b0c7459bSKai Huang
1318b0c7459bSKai Huang WARN_ON_ONCE(page->flags & SGX_EPC_PAGE_RECLAIMER_TRACKED);
1319b0c7459bSKai Huang
1320b0c7459bSKai Huang ret = __eremove(sgx_get_epc_virt_addr(page));
1321b0c7459bSKai Huang if (WARN_ONCE(ret, EREMOVE_ERROR_MESSAGE, ret, ret))
1322b0c7459bSKai Huang return;
1323b0c7459bSKai Huang
1324b0c7459bSKai Huang sgx_free_epc_page(page);
1325b0c7459bSKai Huang }
1326