xref: /openbmc/linux/arch/x86/kernel/cpu/sgx/virt.c (revision 3d7d72a3)
1540745ddSSean Christopherson // SPDX-License-Identifier: GPL-2.0
2540745ddSSean Christopherson /*
3540745ddSSean Christopherson  * Device driver to expose SGX enclave memory to KVM guests.
4540745ddSSean Christopherson  *
5540745ddSSean Christopherson  * Copyright(c) 2021 Intel Corporation.
6540745ddSSean Christopherson  */
7540745ddSSean Christopherson 
8540745ddSSean Christopherson #include <linux/miscdevice.h>
9540745ddSSean Christopherson #include <linux/mm.h>
10540745ddSSean Christopherson #include <linux/mman.h>
11540745ddSSean Christopherson #include <linux/sched/mm.h>
12540745ddSSean Christopherson #include <linux/sched/signal.h>
13540745ddSSean Christopherson #include <linux/slab.h>
14540745ddSSean Christopherson #include <linux/xarray.h>
15540745ddSSean Christopherson #include <asm/sgx.h>
16540745ddSSean Christopherson #include <uapi/asm/sgx.h>
17540745ddSSean Christopherson 
18540745ddSSean Christopherson #include "encls.h"
19540745ddSSean Christopherson #include "sgx.h"
20540745ddSSean Christopherson 
21540745ddSSean Christopherson struct sgx_vepc {
22540745ddSSean Christopherson 	struct xarray page_array;
23540745ddSSean Christopherson 	struct mutex lock;
24540745ddSSean Christopherson };
25540745ddSSean Christopherson 
26540745ddSSean Christopherson /*
27540745ddSSean Christopherson  * Temporary SECS pages that cannot be EREMOVE'd due to having child in other
28540745ddSSean Christopherson  * virtual EPC instances, and the lock to protect it.
29540745ddSSean Christopherson  */
30540745ddSSean Christopherson static struct mutex zombie_secs_pages_lock;
31540745ddSSean Christopherson static struct list_head zombie_secs_pages;
32540745ddSSean Christopherson 
__sgx_vepc_fault(struct sgx_vepc * vepc,struct vm_area_struct * vma,unsigned long addr)33540745ddSSean Christopherson static int __sgx_vepc_fault(struct sgx_vepc *vepc,
34540745ddSSean Christopherson 			    struct vm_area_struct *vma, unsigned long addr)
35540745ddSSean Christopherson {
36540745ddSSean Christopherson 	struct sgx_epc_page *epc_page;
37540745ddSSean Christopherson 	unsigned long index, pfn;
38540745ddSSean Christopherson 	int ret;
39540745ddSSean Christopherson 
40540745ddSSean Christopherson 	WARN_ON(!mutex_is_locked(&vepc->lock));
41540745ddSSean Christopherson 
42540745ddSSean Christopherson 	/* Calculate index of EPC page in virtual EPC's page_array */
43540745ddSSean Christopherson 	index = vma->vm_pgoff + PFN_DOWN(addr - vma->vm_start);
44540745ddSSean Christopherson 
45540745ddSSean Christopherson 	epc_page = xa_load(&vepc->page_array, index);
46540745ddSSean Christopherson 	if (epc_page)
47540745ddSSean Christopherson 		return 0;
48540745ddSSean Christopherson 
49540745ddSSean Christopherson 	epc_page = sgx_alloc_epc_page(vepc, false);
50540745ddSSean Christopherson 	if (IS_ERR(epc_page))
51540745ddSSean Christopherson 		return PTR_ERR(epc_page);
52540745ddSSean Christopherson 
53540745ddSSean Christopherson 	ret = xa_err(xa_store(&vepc->page_array, index, epc_page, GFP_KERNEL));
54540745ddSSean Christopherson 	if (ret)
55540745ddSSean Christopherson 		goto err_free;
56540745ddSSean Christopherson 
57540745ddSSean Christopherson 	pfn = PFN_DOWN(sgx_get_epc_phys_addr(epc_page));
58540745ddSSean Christopherson 
59540745ddSSean Christopherson 	ret = vmf_insert_pfn(vma, addr, pfn);
60540745ddSSean Christopherson 	if (ret != VM_FAULT_NOPAGE) {
61540745ddSSean Christopherson 		ret = -EFAULT;
62540745ddSSean Christopherson 		goto err_delete;
63540745ddSSean Christopherson 	}
64540745ddSSean Christopherson 
65540745ddSSean Christopherson 	return 0;
66540745ddSSean Christopherson 
67540745ddSSean Christopherson err_delete:
68540745ddSSean Christopherson 	xa_erase(&vepc->page_array, index);
69540745ddSSean Christopherson err_free:
70540745ddSSean Christopherson 	sgx_free_epc_page(epc_page);
71540745ddSSean Christopherson 	return ret;
72540745ddSSean Christopherson }
73540745ddSSean Christopherson 
sgx_vepc_fault(struct vm_fault * vmf)74540745ddSSean Christopherson static vm_fault_t sgx_vepc_fault(struct vm_fault *vmf)
75540745ddSSean Christopherson {
76540745ddSSean Christopherson 	struct vm_area_struct *vma = vmf->vma;
77540745ddSSean Christopherson 	struct sgx_vepc *vepc = vma->vm_private_data;
78540745ddSSean Christopherson 	int ret;
79540745ddSSean Christopherson 
80540745ddSSean Christopherson 	mutex_lock(&vepc->lock);
81540745ddSSean Christopherson 	ret = __sgx_vepc_fault(vepc, vma, vmf->address);
82540745ddSSean Christopherson 	mutex_unlock(&vepc->lock);
83540745ddSSean Christopherson 
84540745ddSSean Christopherson 	if (!ret)
85540745ddSSean Christopherson 		return VM_FAULT_NOPAGE;
86540745ddSSean Christopherson 
87540745ddSSean Christopherson 	if (ret == -EBUSY && (vmf->flags & FAULT_FLAG_ALLOW_RETRY)) {
88540745ddSSean Christopherson 		mmap_read_unlock(vma->vm_mm);
89540745ddSSean Christopherson 		return VM_FAULT_RETRY;
90540745ddSSean Christopherson 	}
91540745ddSSean Christopherson 
92540745ddSSean Christopherson 	return VM_FAULT_SIGBUS;
93540745ddSSean Christopherson }
94540745ddSSean Christopherson 
95523caed9SWei Yongjun static const struct vm_operations_struct sgx_vepc_vm_ops = {
96540745ddSSean Christopherson 	.fault = sgx_vepc_fault,
97540745ddSSean Christopherson };
98540745ddSSean Christopherson 
sgx_vepc_mmap(struct file * file,struct vm_area_struct * vma)99540745ddSSean Christopherson static int sgx_vepc_mmap(struct file *file, struct vm_area_struct *vma)
100540745ddSSean Christopherson {
101540745ddSSean Christopherson 	struct sgx_vepc *vepc = file->private_data;
102540745ddSSean Christopherson 
103540745ddSSean Christopherson 	if (!(vma->vm_flags & VM_SHARED))
104540745ddSSean Christopherson 		return -EINVAL;
105540745ddSSean Christopherson 
106540745ddSSean Christopherson 	vma->vm_ops = &sgx_vepc_vm_ops;
107540745ddSSean Christopherson 	/* Don't copy VMA in fork() */
1081c71222eSSuren Baghdasaryan 	vm_flags_set(vma, VM_PFNMAP | VM_IO | VM_DONTDUMP | VM_DONTCOPY);
109540745ddSSean Christopherson 	vma->vm_private_data = vepc;
110540745ddSSean Christopherson 
111540745ddSSean Christopherson 	return 0;
112540745ddSSean Christopherson }
113540745ddSSean Christopherson 
sgx_vepc_remove_page(struct sgx_epc_page * epc_page)114fd5128e6SPaolo Bonzini static int sgx_vepc_remove_page(struct sgx_epc_page *epc_page)
115540745ddSSean Christopherson {
116540745ddSSean Christopherson 	/*
117540745ddSSean Christopherson 	 * Take a previously guest-owned EPC page and return it to the
118540745ddSSean Christopherson 	 * general EPC page pool.
119540745ddSSean Christopherson 	 *
120540745ddSSean Christopherson 	 * Guests can not be trusted to have left this page in a good
121540745ddSSean Christopherson 	 * state, so run EREMOVE on the page unconditionally.  In the
122540745ddSSean Christopherson 	 * case that a guest properly EREMOVE'd this page, a superfluous
123540745ddSSean Christopherson 	 * EREMOVE is harmless.
124540745ddSSean Christopherson 	 */
125fd5128e6SPaolo Bonzini 	return __eremove(sgx_get_epc_virt_addr(epc_page));
126fd5128e6SPaolo Bonzini }
127fd5128e6SPaolo Bonzini 
sgx_vepc_free_page(struct sgx_epc_page * epc_page)128fd5128e6SPaolo Bonzini static int sgx_vepc_free_page(struct sgx_epc_page *epc_page)
129fd5128e6SPaolo Bonzini {
130fd5128e6SPaolo Bonzini 	int ret = sgx_vepc_remove_page(epc_page);
131540745ddSSean Christopherson 	if (ret) {
132540745ddSSean Christopherson 		/*
133540745ddSSean Christopherson 		 * Only SGX_CHILD_PRESENT is expected, which is because of
134540745ddSSean Christopherson 		 * EREMOVE'ing an SECS still with child, in which case it can
135540745ddSSean Christopherson 		 * be handled by EREMOVE'ing the SECS again after all pages in
136540745ddSSean Christopherson 		 * virtual EPC have been EREMOVE'd. See comments in below in
137540745ddSSean Christopherson 		 * sgx_vepc_release().
138540745ddSSean Christopherson 		 *
139540745ddSSean Christopherson 		 * The user of virtual EPC (KVM) needs to guarantee there's no
140540745ddSSean Christopherson 		 * logical processor is still running in the enclave in guest,
141540745ddSSean Christopherson 		 * otherwise EREMOVE will get SGX_ENCLAVE_ACT which cannot be
142540745ddSSean Christopherson 		 * handled here.
143540745ddSSean Christopherson 		 */
144540745ddSSean Christopherson 		WARN_ONCE(ret != SGX_CHILD_PRESENT, EREMOVE_ERROR_MESSAGE,
145540745ddSSean Christopherson 			  ret, ret);
146540745ddSSean Christopherson 		return ret;
147540745ddSSean Christopherson 	}
148540745ddSSean Christopherson 
149540745ddSSean Christopherson 	sgx_free_epc_page(epc_page);
150540745ddSSean Christopherson 	return 0;
151540745ddSSean Christopherson }
152540745ddSSean Christopherson 
sgx_vepc_remove_all(struct sgx_vepc * vepc)153ae095b16SPaolo Bonzini static long sgx_vepc_remove_all(struct sgx_vepc *vepc)
154ae095b16SPaolo Bonzini {
155ae095b16SPaolo Bonzini 	struct sgx_epc_page *entry;
156ae095b16SPaolo Bonzini 	unsigned long index;
157ae095b16SPaolo Bonzini 	long failures = 0;
158ae095b16SPaolo Bonzini 
159ae095b16SPaolo Bonzini 	xa_for_each(&vepc->page_array, index, entry) {
160ae095b16SPaolo Bonzini 		int ret = sgx_vepc_remove_page(entry);
161ae095b16SPaolo Bonzini 		if (ret) {
162ae095b16SPaolo Bonzini 			if (ret == SGX_CHILD_PRESENT) {
163ae095b16SPaolo Bonzini 				/* The page is a SECS, userspace will retry.  */
164ae095b16SPaolo Bonzini 				failures++;
165ae095b16SPaolo Bonzini 			} else {
166ae095b16SPaolo Bonzini 				/*
167ae095b16SPaolo Bonzini 				 * Report errors due to #GP or SGX_ENCLAVE_ACT; do not
168ae095b16SPaolo Bonzini 				 * WARN, as userspace can induce said failures by
169ae095b16SPaolo Bonzini 				 * calling the ioctl concurrently on multiple vEPCs or
170ae095b16SPaolo Bonzini 				 * while one or more CPUs is running the enclave.  Only
171ae095b16SPaolo Bonzini 				 * a #PF on EREMOVE indicates a kernel/hardware issue.
172ae095b16SPaolo Bonzini 				 */
173ae095b16SPaolo Bonzini 				WARN_ON_ONCE(encls_faulted(ret) &&
174ae095b16SPaolo Bonzini 					     ENCLS_TRAPNR(ret) != X86_TRAP_GP);
175ae095b16SPaolo Bonzini 				return -EBUSY;
176ae095b16SPaolo Bonzini 			}
177ae095b16SPaolo Bonzini 		}
178ae095b16SPaolo Bonzini 		cond_resched();
179ae095b16SPaolo Bonzini 	}
180ae095b16SPaolo Bonzini 
181ae095b16SPaolo Bonzini 	/*
182ae095b16SPaolo Bonzini 	 * Return the number of SECS pages that failed to be removed, so
183ae095b16SPaolo Bonzini 	 * userspace knows that it has to retry.
184ae095b16SPaolo Bonzini 	 */
185ae095b16SPaolo Bonzini 	return failures;
186ae095b16SPaolo Bonzini }
187ae095b16SPaolo Bonzini 
sgx_vepc_release(struct inode * inode,struct file * file)188540745ddSSean Christopherson static int sgx_vepc_release(struct inode *inode, struct file *file)
189540745ddSSean Christopherson {
190540745ddSSean Christopherson 	struct sgx_vepc *vepc = file->private_data;
191540745ddSSean Christopherson 	struct sgx_epc_page *epc_page, *tmp, *entry;
192540745ddSSean Christopherson 	unsigned long index;
193540745ddSSean Christopherson 
194540745ddSSean Christopherson 	LIST_HEAD(secs_pages);
195540745ddSSean Christopherson 
196540745ddSSean Christopherson 	xa_for_each(&vepc->page_array, index, entry) {
197540745ddSSean Christopherson 		/*
198540745ddSSean Christopherson 		 * Remove all normal, child pages.  sgx_vepc_free_page()
199540745ddSSean Christopherson 		 * will fail if EREMOVE fails, but this is OK and expected on
200540745ddSSean Christopherson 		 * SECS pages.  Those can only be EREMOVE'd *after* all their
201540745ddSSean Christopherson 		 * child pages. Retries below will clean them up.
202540745ddSSean Christopherson 		 */
203540745ddSSean Christopherson 		if (sgx_vepc_free_page(entry))
204540745ddSSean Christopherson 			continue;
205540745ddSSean Christopherson 
206540745ddSSean Christopherson 		xa_erase(&vepc->page_array, index);
207*3d7d72a3SJack Wang 		cond_resched();
208540745ddSSean Christopherson 	}
209540745ddSSean Christopherson 
210540745ddSSean Christopherson 	/*
211540745ddSSean Christopherson 	 * Retry EREMOVE'ing pages.  This will clean up any SECS pages that
212540745ddSSean Christopherson 	 * only had children in this 'epc' area.
213540745ddSSean Christopherson 	 */
214540745ddSSean Christopherson 	xa_for_each(&vepc->page_array, index, entry) {
215540745ddSSean Christopherson 		epc_page = entry;
216540745ddSSean Christopherson 		/*
217540745ddSSean Christopherson 		 * An EREMOVE failure here means that the SECS page still
218540745ddSSean Christopherson 		 * has children.  But, since all children in this 'sgx_vepc'
219540745ddSSean Christopherson 		 * have been removed, the SECS page must have a child on
220540745ddSSean Christopherson 		 * another instance.
221540745ddSSean Christopherson 		 */
222540745ddSSean Christopherson 		if (sgx_vepc_free_page(epc_page))
223540745ddSSean Christopherson 			list_add_tail(&epc_page->list, &secs_pages);
224540745ddSSean Christopherson 
225540745ddSSean Christopherson 		xa_erase(&vepc->page_array, index);
226*3d7d72a3SJack Wang 		cond_resched();
227540745ddSSean Christopherson 	}
228540745ddSSean Christopherson 
229540745ddSSean Christopherson 	/*
230540745ddSSean Christopherson 	 * SECS pages are "pinned" by child pages, and "unpinned" once all
231540745ddSSean Christopherson 	 * children have been EREMOVE'd.  A child page in this instance
232540745ddSSean Christopherson 	 * may have pinned an SECS page encountered in an earlier release(),
233540745ddSSean Christopherson 	 * creating a zombie.  Since some children were EREMOVE'd above,
234540745ddSSean Christopherson 	 * try to EREMOVE all zombies in the hopes that one was unpinned.
235540745ddSSean Christopherson 	 */
236540745ddSSean Christopherson 	mutex_lock(&zombie_secs_pages_lock);
237540745ddSSean Christopherson 	list_for_each_entry_safe(epc_page, tmp, &zombie_secs_pages, list) {
238540745ddSSean Christopherson 		/*
239540745ddSSean Christopherson 		 * Speculatively remove the page from the list of zombies,
240540745ddSSean Christopherson 		 * if the page is successfully EREMOVE'd it will be added to
241540745ddSSean Christopherson 		 * the list of free pages.  If EREMOVE fails, throw the page
242540745ddSSean Christopherson 		 * on the local list, which will be spliced on at the end.
243540745ddSSean Christopherson 		 */
244540745ddSSean Christopherson 		list_del(&epc_page->list);
245540745ddSSean Christopherson 
246540745ddSSean Christopherson 		if (sgx_vepc_free_page(epc_page))
247540745ddSSean Christopherson 			list_add_tail(&epc_page->list, &secs_pages);
248*3d7d72a3SJack Wang 		cond_resched();
249540745ddSSean Christopherson 	}
250540745ddSSean Christopherson 
251540745ddSSean Christopherson 	if (!list_empty(&secs_pages))
252540745ddSSean Christopherson 		list_splice_tail(&secs_pages, &zombie_secs_pages);
253540745ddSSean Christopherson 	mutex_unlock(&zombie_secs_pages_lock);
254540745ddSSean Christopherson 
2554692bc77SKai Huang 	xa_destroy(&vepc->page_array);
256540745ddSSean Christopherson 	kfree(vepc);
257540745ddSSean Christopherson 
258540745ddSSean Christopherson 	return 0;
259540745ddSSean Christopherson }
260540745ddSSean Christopherson 
sgx_vepc_open(struct inode * inode,struct file * file)261540745ddSSean Christopherson static int sgx_vepc_open(struct inode *inode, struct file *file)
262540745ddSSean Christopherson {
263540745ddSSean Christopherson 	struct sgx_vepc *vepc;
264540745ddSSean Christopherson 
265540745ddSSean Christopherson 	vepc = kzalloc(sizeof(struct sgx_vepc), GFP_KERNEL);
266540745ddSSean Christopherson 	if (!vepc)
267540745ddSSean Christopherson 		return -ENOMEM;
268540745ddSSean Christopherson 	mutex_init(&vepc->lock);
269540745ddSSean Christopherson 	xa_init(&vepc->page_array);
270540745ddSSean Christopherson 
271540745ddSSean Christopherson 	file->private_data = vepc;
272540745ddSSean Christopherson 
273540745ddSSean Christopherson 	return 0;
274540745ddSSean Christopherson }
275540745ddSSean Christopherson 
sgx_vepc_ioctl(struct file * file,unsigned int cmd,unsigned long arg)276ae095b16SPaolo Bonzini static long sgx_vepc_ioctl(struct file *file,
277ae095b16SPaolo Bonzini 			   unsigned int cmd, unsigned long arg)
278ae095b16SPaolo Bonzini {
279ae095b16SPaolo Bonzini 	struct sgx_vepc *vepc = file->private_data;
280ae095b16SPaolo Bonzini 
281ae095b16SPaolo Bonzini 	switch (cmd) {
282ae095b16SPaolo Bonzini 	case SGX_IOC_VEPC_REMOVE_ALL:
283ae095b16SPaolo Bonzini 		if (arg)
284ae095b16SPaolo Bonzini 			return -EINVAL;
285ae095b16SPaolo Bonzini 		return sgx_vepc_remove_all(vepc);
286ae095b16SPaolo Bonzini 
287ae095b16SPaolo Bonzini 	default:
288ae095b16SPaolo Bonzini 		return -ENOTTY;
289ae095b16SPaolo Bonzini 	}
290ae095b16SPaolo Bonzini }
291ae095b16SPaolo Bonzini 
292540745ddSSean Christopherson static const struct file_operations sgx_vepc_fops = {
293540745ddSSean Christopherson 	.owner		= THIS_MODULE,
294540745ddSSean Christopherson 	.open		= sgx_vepc_open,
295ae095b16SPaolo Bonzini 	.unlocked_ioctl	= sgx_vepc_ioctl,
296ae095b16SPaolo Bonzini 	.compat_ioctl	= sgx_vepc_ioctl,
297540745ddSSean Christopherson 	.release	= sgx_vepc_release,
298540745ddSSean Christopherson 	.mmap		= sgx_vepc_mmap,
299540745ddSSean Christopherson };
300540745ddSSean Christopherson 
301540745ddSSean Christopherson static struct miscdevice sgx_vepc_dev = {
302540745ddSSean Christopherson 	.minor		= MISC_DYNAMIC_MINOR,
303540745ddSSean Christopherson 	.name		= "sgx_vepc",
304540745ddSSean Christopherson 	.nodename	= "sgx_vepc",
305540745ddSSean Christopherson 	.fops		= &sgx_vepc_fops,
306540745ddSSean Christopherson };
307540745ddSSean Christopherson 
sgx_vepc_init(void)308540745ddSSean Christopherson int __init sgx_vepc_init(void)
309540745ddSSean Christopherson {
310540745ddSSean Christopherson 	/* SGX virtualization requires KVM to work */
311540745ddSSean Christopherson 	if (!cpu_feature_enabled(X86_FEATURE_VMX))
312540745ddSSean Christopherson 		return -ENODEV;
313540745ddSSean Christopherson 
314540745ddSSean Christopherson 	INIT_LIST_HEAD(&zombie_secs_pages);
315540745ddSSean Christopherson 	mutex_init(&zombie_secs_pages_lock);
316540745ddSSean Christopherson 
317540745ddSSean Christopherson 	return misc_register(&sgx_vepc_dev);
318540745ddSSean Christopherson }
319d155030bSSean Christopherson 
320d155030bSSean Christopherson /**
321d155030bSSean Christopherson  * sgx_virt_ecreate() - Run ECREATE on behalf of guest
322d155030bSSean Christopherson  * @pageinfo:	Pointer to PAGEINFO structure
323d155030bSSean Christopherson  * @secs:	Userspace pointer to SECS page
324d155030bSSean Christopherson  * @trapnr:	trap number injected to guest in case of ECREATE error
325d155030bSSean Christopherson  *
326d155030bSSean Christopherson  * Run ECREATE on behalf of guest after KVM traps ECREATE for the purpose
327d155030bSSean Christopherson  * of enforcing policies of guest's enclaves, and return the trap number
328d155030bSSean Christopherson  * which should be injected to guest in case of any ECREATE error.
329d155030bSSean Christopherson  *
330d155030bSSean Christopherson  * Return:
331d155030bSSean Christopherson  * -  0:	ECREATE was successful.
332d155030bSSean Christopherson  * - <0:	on error.
333d155030bSSean Christopherson  */
sgx_virt_ecreate(struct sgx_pageinfo * pageinfo,void __user * secs,int * trapnr)334d155030bSSean Christopherson int sgx_virt_ecreate(struct sgx_pageinfo *pageinfo, void __user *secs,
335d155030bSSean Christopherson 		     int *trapnr)
336d155030bSSean Christopherson {
337d155030bSSean Christopherson 	int ret;
338d155030bSSean Christopherson 
339d155030bSSean Christopherson 	/*
340d155030bSSean Christopherson 	 * @secs is an untrusted, userspace-provided address.  It comes from
341d155030bSSean Christopherson 	 * KVM and is assumed to be a valid pointer which points somewhere in
342d155030bSSean Christopherson 	 * userspace.  This can fault and call SGX or other fault handlers when
343d155030bSSean Christopherson 	 * userspace mapping @secs doesn't exist.
344d155030bSSean Christopherson 	 *
345d155030bSSean Christopherson 	 * Add a WARN() to make sure @secs is already valid userspace pointer
346d155030bSSean Christopherson 	 * from caller (KVM), who should already have handled invalid pointer
347d155030bSSean Christopherson 	 * case (for instance, made by malicious guest).  All other checks,
348d155030bSSean Christopherson 	 * such as alignment of @secs, are deferred to ENCLS itself.
349d155030bSSean Christopherson 	 */
350d155030bSSean Christopherson 	if (WARN_ON_ONCE(!access_ok(secs, PAGE_SIZE)))
351d155030bSSean Christopherson 		return -EINVAL;
352d155030bSSean Christopherson 
353d155030bSSean Christopherson 	__uaccess_begin();
354d155030bSSean Christopherson 	ret = __ecreate(pageinfo, (void *)secs);
355d155030bSSean Christopherson 	__uaccess_end();
356d155030bSSean Christopherson 
357d155030bSSean Christopherson 	if (encls_faulted(ret)) {
358d155030bSSean Christopherson 		*trapnr = ENCLS_TRAPNR(ret);
359d155030bSSean Christopherson 		return -EFAULT;
360d155030bSSean Christopherson 	}
361d155030bSSean Christopherson 
362d155030bSSean Christopherson 	/* ECREATE doesn't return an error code, it faults or succeeds. */
363d155030bSSean Christopherson 	WARN_ON_ONCE(ret);
364d155030bSSean Christopherson 	return 0;
365d155030bSSean Christopherson }
366d155030bSSean Christopherson EXPORT_SYMBOL_GPL(sgx_virt_ecreate);
367d155030bSSean Christopherson 
__sgx_virt_einit(void __user * sigstruct,void __user * token,void __user * secs)368d155030bSSean Christopherson static int __sgx_virt_einit(void __user *sigstruct, void __user *token,
369d155030bSSean Christopherson 			    void __user *secs)
370d155030bSSean Christopherson {
371d155030bSSean Christopherson 	int ret;
372d155030bSSean Christopherson 
373d155030bSSean Christopherson 	/*
374d155030bSSean Christopherson 	 * Make sure all userspace pointers from caller (KVM) are valid.
375d155030bSSean Christopherson 	 * All other checks deferred to ENCLS itself.  Also see comment
376d155030bSSean Christopherson 	 * for @secs in sgx_virt_ecreate().
377d155030bSSean Christopherson 	 */
378d155030bSSean Christopherson #define SGX_EINITTOKEN_SIZE	304
379d155030bSSean Christopherson 	if (WARN_ON_ONCE(!access_ok(sigstruct, sizeof(struct sgx_sigstruct)) ||
380d155030bSSean Christopherson 			 !access_ok(token, SGX_EINITTOKEN_SIZE) ||
381d155030bSSean Christopherson 			 !access_ok(secs, PAGE_SIZE)))
382d155030bSSean Christopherson 		return -EINVAL;
383d155030bSSean Christopherson 
384d155030bSSean Christopherson 	__uaccess_begin();
385d155030bSSean Christopherson 	ret = __einit((void *)sigstruct, (void *)token, (void *)secs);
386d155030bSSean Christopherson 	__uaccess_end();
387d155030bSSean Christopherson 
388d155030bSSean Christopherson 	return ret;
389d155030bSSean Christopherson }
390d155030bSSean Christopherson 
391d155030bSSean Christopherson /**
392d155030bSSean Christopherson  * sgx_virt_einit() - Run EINIT on behalf of guest
393d155030bSSean Christopherson  * @sigstruct:		Userspace pointer to SIGSTRUCT structure
394d155030bSSean Christopherson  * @token:		Userspace pointer to EINITTOKEN structure
395d155030bSSean Christopherson  * @secs:		Userspace pointer to SECS page
396d155030bSSean Christopherson  * @lepubkeyhash:	Pointer to guest's *virtual* SGX_LEPUBKEYHASH MSR values
397d155030bSSean Christopherson  * @trapnr:		trap number injected to guest in case of EINIT error
398d155030bSSean Christopherson  *
399d155030bSSean Christopherson  * Run EINIT on behalf of guest after KVM traps EINIT. If SGX_LC is available
400d155030bSSean Christopherson  * in host, SGX driver may rewrite the hardware values at wish, therefore KVM
401d155030bSSean Christopherson  * needs to update hardware values to guest's virtual MSR values in order to
402d155030bSSean Christopherson  * ensure EINIT is executed with expected hardware values.
403d155030bSSean Christopherson  *
404d155030bSSean Christopherson  * Return:
405d155030bSSean Christopherson  * -  0:	EINIT was successful.
406d155030bSSean Christopherson  * - <0:	on error.
407d155030bSSean Christopherson  */
sgx_virt_einit(void __user * sigstruct,void __user * token,void __user * secs,u64 * lepubkeyhash,int * trapnr)408d155030bSSean Christopherson int sgx_virt_einit(void __user *sigstruct, void __user *token,
409d155030bSSean Christopherson 		   void __user *secs, u64 *lepubkeyhash, int *trapnr)
410d155030bSSean Christopherson {
411d155030bSSean Christopherson 	int ret;
412d155030bSSean Christopherson 
413d155030bSSean Christopherson 	if (!cpu_feature_enabled(X86_FEATURE_SGX_LC)) {
414d155030bSSean Christopherson 		ret = __sgx_virt_einit(sigstruct, token, secs);
415d155030bSSean Christopherson 	} else {
416d155030bSSean Christopherson 		preempt_disable();
417d155030bSSean Christopherson 
418d155030bSSean Christopherson 		sgx_update_lepubkeyhash(lepubkeyhash);
419d155030bSSean Christopherson 
420d155030bSSean Christopherson 		ret = __sgx_virt_einit(sigstruct, token, secs);
421d155030bSSean Christopherson 		preempt_enable();
422d155030bSSean Christopherson 	}
423d155030bSSean Christopherson 
424d155030bSSean Christopherson 	/* Propagate up the error from the WARN_ON_ONCE in __sgx_virt_einit() */
425d155030bSSean Christopherson 	if (ret == -EINVAL)
426d155030bSSean Christopherson 		return ret;
427d155030bSSean Christopherson 
428d155030bSSean Christopherson 	if (encls_faulted(ret)) {
429d155030bSSean Christopherson 		*trapnr = ENCLS_TRAPNR(ret);
430d155030bSSean Christopherson 		return -EFAULT;
431d155030bSSean Christopherson 	}
432d155030bSSean Christopherson 
433d155030bSSean Christopherson 	return ret;
434d155030bSSean Christopherson }
435d155030bSSean Christopherson EXPORT_SYMBOL_GPL(sgx_virt_einit);
436