1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2016-20 Intel Corporation. */
4 #include <linux/file.h>
5 #include <linux/freezer.h>
6 #include <linux/highmem.h>
7 #include <linux/kthread.h>
8 #include <linux/miscdevice.h>
9 #include <linux/node.h>
10 #include <linux/pagemap.h>
11 #include <linux/ratelimit.h>
12 #include <linux/sched/mm.h>
13 #include <linux/sched/signal.h>
14 #include <linux/slab.h>
15 #include <linux/sysfs.h>
21 struct sgx_epc_section sgx_epc_sections[SGX_MAX_EPC_SECTIONS];
22 static int sgx_nr_epc_sections;
23 static struct task_struct *ksgxd_tsk;
24 static DECLARE_WAIT_QUEUE_HEAD(ksgxd_waitq);
25 static DEFINE_XARRAY(sgx_epc_address_space);
28 * These variables are part of the state of the reclaimer, and must be accessed
29 * with sgx_reclaimer_lock acquired.
31 static LIST_HEAD(sgx_active_page_list);
32 static DEFINE_SPINLOCK(sgx_reclaimer_lock);
34 static atomic_long_t sgx_nr_free_pages = ATOMIC_LONG_INIT(0);
36 /* Nodes with one or more EPC sections. */
37 static nodemask_t sgx_numa_mask;
40 * Array with one list_head for each possible NUMA node. Each
41 * list contains all the sgx_epc_section's which are on that
44 static struct sgx_numa_node *sgx_numa_nodes;
46 static LIST_HEAD(sgx_dirty_page_list);
49 * Reset post-kexec EPC pages to the uninitialized state. The pages are removed
50 * from the input list, and made available for the page allocator. SECS pages
51 * prepending their children in the input list are left intact.
53 static void __sgx_sanitize_pages(struct list_head *dirty_page_list)
55 struct sgx_epc_page *page;
59 /* dirty_page_list is thread-local, no need for a lock: */
60 while (!list_empty(dirty_page_list)) {
61 if (kthread_should_stop())
64 page = list_first_entry(dirty_page_list, struct sgx_epc_page, list);
67 * Checking page->poison without holding the node->lock
68 * is racy, but losing the race (i.e. poison is set just
69 * after the check) just means __eremove() will be uselessly
70 * called for a page that sgx_free_epc_page() will put onto
71 * the node->sgx_poison_page_list later.
74 struct sgx_epc_section *section = &sgx_epc_sections[page->section];
75 struct sgx_numa_node *node = section->node;
77 spin_lock(&node->lock);
78 list_move(&page->list, &node->sgx_poison_page_list);
79 spin_unlock(&node->lock);
84 ret = __eremove(sgx_get_epc_virt_addr(page));
87 * page is now sanitized. Make it available via the SGX
90 list_del(&page->list);
91 sgx_free_epc_page(page);
93 /* The page is not yet clean - move to the dirty list. */
94 list_move_tail(&page->list, &dirty);
100 list_splice(&dirty, dirty_page_list);
103 static bool sgx_reclaimer_age(struct sgx_epc_page *epc_page)
105 struct sgx_encl_page *page = epc_page->owner;
106 struct sgx_encl *encl = page->encl;
107 struct sgx_encl_mm *encl_mm;
111 idx = srcu_read_lock(&encl->srcu);
113 list_for_each_entry_rcu(encl_mm, &encl->mm_list, list) {
114 if (!mmget_not_zero(encl_mm->mm))
117 mmap_read_lock(encl_mm->mm);
118 ret = !sgx_encl_test_and_clear_young(encl_mm->mm, page);
119 mmap_read_unlock(encl_mm->mm);
121 mmput_async(encl_mm->mm);
127 srcu_read_unlock(&encl->srcu, idx);
135 static void sgx_reclaimer_block(struct sgx_epc_page *epc_page)
137 struct sgx_encl_page *page = epc_page->owner;
138 unsigned long addr = page->desc & PAGE_MASK;
139 struct sgx_encl *encl = page->encl;
142 sgx_zap_enclave_ptes(encl, addr);
144 mutex_lock(&encl->lock);
146 ret = __eblock(sgx_get_epc_virt_addr(epc_page));
147 if (encls_failed(ret))
148 ENCLS_WARN(ret, "EBLOCK");
150 mutex_unlock(&encl->lock);
153 static int __sgx_encl_ewb(struct sgx_epc_page *epc_page, void *va_slot,
154 struct sgx_backing *backing)
156 struct sgx_pageinfo pginfo;
162 pginfo.contents = (unsigned long)kmap_atomic(backing->contents);
163 pginfo.metadata = (unsigned long)kmap_atomic(backing->pcmd) +
164 backing->pcmd_offset;
166 ret = __ewb(&pginfo, sgx_get_epc_virt_addr(epc_page), va_slot);
167 set_page_dirty(backing->pcmd);
168 set_page_dirty(backing->contents);
170 kunmap_atomic((void *)(unsigned long)(pginfo.metadata -
171 backing->pcmd_offset));
172 kunmap_atomic((void *)(unsigned long)pginfo.contents);
177 void sgx_ipi_cb(void *info)
182 * Swap page to the regular memory transformed to the blocked state by using
183 * EBLOCK, which means that it can no longer be referenced (no new TLB entries).
185 * The first trial just tries to write the page assuming that some other thread
186 * has reset the count for threads inside the enclave by using ETRACK, and
187 * previous thread count has been zeroed out. The second trial calls ETRACK
188 * before EWB. If that fails we kick all the HW threads out, and then do EWB,
189 * which should be guaranteed the succeed.
191 static void sgx_encl_ewb(struct sgx_epc_page *epc_page,
192 struct sgx_backing *backing)
194 struct sgx_encl_page *encl_page = epc_page->owner;
195 struct sgx_encl *encl = encl_page->encl;
196 struct sgx_va_page *va_page;
197 unsigned int va_offset;
201 encl_page->desc &= ~SGX_ENCL_PAGE_BEING_RECLAIMED;
203 va_page = list_first_entry(&encl->va_pages, struct sgx_va_page,
205 va_offset = sgx_alloc_va_slot(va_page);
206 va_slot = sgx_get_epc_virt_addr(va_page->epc_page) + va_offset;
207 if (sgx_va_page_full(va_page))
208 list_move_tail(&va_page->list, &encl->va_pages);
210 ret = __sgx_encl_ewb(epc_page, va_slot, backing);
211 if (ret == SGX_NOT_TRACKED) {
212 ret = __etrack(sgx_get_epc_virt_addr(encl->secs.epc_page));
214 if (encls_failed(ret))
215 ENCLS_WARN(ret, "ETRACK");
218 ret = __sgx_encl_ewb(epc_page, va_slot, backing);
219 if (ret == SGX_NOT_TRACKED) {
221 * Slow path, send IPIs to kick cpus out of the
222 * enclave. Note, it's imperative that the cpu
223 * mask is generated *after* ETRACK, else we'll
224 * miss cpus that entered the enclave between
225 * generating the mask and incrementing epoch.
227 on_each_cpu_mask(sgx_encl_cpumask(encl),
228 sgx_ipi_cb, NULL, 1);
229 ret = __sgx_encl_ewb(epc_page, va_slot, backing);
234 if (encls_failed(ret))
235 ENCLS_WARN(ret, "EWB");
237 sgx_free_va_slot(va_page, va_offset);
239 encl_page->desc |= va_offset;
240 encl_page->va_page = va_page;
244 static void sgx_reclaimer_write(struct sgx_epc_page *epc_page,
245 struct sgx_backing *backing)
247 struct sgx_encl_page *encl_page = epc_page->owner;
248 struct sgx_encl *encl = encl_page->encl;
249 struct sgx_backing secs_backing;
252 mutex_lock(&encl->lock);
254 sgx_encl_ewb(epc_page, backing);
255 encl_page->epc_page = NULL;
256 encl->secs_child_cnt--;
257 sgx_encl_put_backing(backing);
259 if (!encl->secs_child_cnt && test_bit(SGX_ENCL_INITIALIZED, &encl->flags)) {
260 ret = sgx_encl_alloc_backing(encl, PFN_DOWN(encl->size),
265 sgx_encl_ewb(encl->secs.epc_page, &secs_backing);
267 sgx_encl_free_epc_page(encl->secs.epc_page);
268 encl->secs.epc_page = NULL;
270 sgx_encl_put_backing(&secs_backing);
274 mutex_unlock(&encl->lock);
278 * Take a fixed number of pages from the head of the active page pool and
279 * reclaim them to the enclave's private shmem files. Skip the pages, which have
280 * been accessed since the last scan. Move those pages to the tail of active
281 * page pool so that the pages get scanned in LRU like fashion.
283 * Batch process a chunk of pages (at the moment 16) in order to degrade amount
284 * of IPI's and ETRACK's potentially required. sgx_encl_ewb() does degrade a bit
285 * among the HW threads with three stage EWB pipeline (EWB, ETRACK + EWB and IPI
286 * + EWB) but not sufficiently. Reclaiming one page at a time would also be
287 * problematic as it would increase the lock contention too much, which would
288 * halt forward progress.
290 static void sgx_reclaim_pages(void)
292 struct sgx_epc_page *chunk[SGX_NR_TO_SCAN];
293 struct sgx_backing backing[SGX_NR_TO_SCAN];
294 struct sgx_encl_page *encl_page;
295 struct sgx_epc_page *epc_page;
301 spin_lock(&sgx_reclaimer_lock);
302 for (i = 0; i < SGX_NR_TO_SCAN; i++) {
303 if (list_empty(&sgx_active_page_list))
306 epc_page = list_first_entry(&sgx_active_page_list,
307 struct sgx_epc_page, list);
308 list_del_init(&epc_page->list);
309 encl_page = epc_page->owner;
311 if (kref_get_unless_zero(&encl_page->encl->refcount) != 0)
312 chunk[cnt++] = epc_page;
314 /* The owner is freeing the page. No need to add the
315 * page back to the list of reclaimable pages.
317 epc_page->flags &= ~SGX_EPC_PAGE_RECLAIMER_TRACKED;
319 spin_unlock(&sgx_reclaimer_lock);
321 for (i = 0; i < cnt; i++) {
323 encl_page = epc_page->owner;
325 if (!sgx_reclaimer_age(epc_page))
328 page_index = PFN_DOWN(encl_page->desc - encl_page->encl->base);
330 mutex_lock(&encl_page->encl->lock);
331 ret = sgx_encl_alloc_backing(encl_page->encl, page_index, &backing[i]);
333 mutex_unlock(&encl_page->encl->lock);
337 encl_page->desc |= SGX_ENCL_PAGE_BEING_RECLAIMED;
338 mutex_unlock(&encl_page->encl->lock);
342 spin_lock(&sgx_reclaimer_lock);
343 list_add_tail(&epc_page->list, &sgx_active_page_list);
344 spin_unlock(&sgx_reclaimer_lock);
346 kref_put(&encl_page->encl->refcount, sgx_encl_release);
351 for (i = 0; i < cnt; i++) {
354 sgx_reclaimer_block(epc_page);
357 for (i = 0; i < cnt; i++) {
362 encl_page = epc_page->owner;
363 sgx_reclaimer_write(epc_page, &backing[i]);
365 kref_put(&encl_page->encl->refcount, sgx_encl_release);
366 epc_page->flags &= ~SGX_EPC_PAGE_RECLAIMER_TRACKED;
368 sgx_free_epc_page(epc_page);
372 static bool sgx_should_reclaim(unsigned long watermark)
374 return atomic_long_read(&sgx_nr_free_pages) < watermark &&
375 !list_empty(&sgx_active_page_list);
379 * sgx_reclaim_direct() should be called (without enclave's mutex held)
380 * in locations where SGX memory resources might be low and might be
381 * needed in order to make forward progress.
383 void sgx_reclaim_direct(void)
385 if (sgx_should_reclaim(SGX_NR_LOW_PAGES))
389 static int ksgxd(void *p)
394 * Sanitize pages in order to recover from kexec(). The 2nd pass is
395 * required for SECS pages, whose child pages blocked EREMOVE.
397 __sgx_sanitize_pages(&sgx_dirty_page_list);
398 __sgx_sanitize_pages(&sgx_dirty_page_list);
401 WARN_ON(!list_empty(&sgx_dirty_page_list));
403 while (!kthread_should_stop()) {
407 wait_event_freezable(ksgxd_waitq,
408 kthread_should_stop() ||
409 sgx_should_reclaim(SGX_NR_HIGH_PAGES));
411 if (sgx_should_reclaim(SGX_NR_HIGH_PAGES))
420 static bool __init sgx_page_reclaimer_init(void)
422 struct task_struct *tsk;
424 tsk = kthread_run(ksgxd, NULL, "ksgxd");
433 bool current_is_ksgxd(void)
435 return current == ksgxd_tsk;
438 static struct sgx_epc_page *__sgx_alloc_epc_page_from_node(int nid)
440 struct sgx_numa_node *node = &sgx_numa_nodes[nid];
441 struct sgx_epc_page *page = NULL;
443 spin_lock(&node->lock);
445 if (list_empty(&node->free_page_list)) {
446 spin_unlock(&node->lock);
450 page = list_first_entry(&node->free_page_list, struct sgx_epc_page, list);
451 list_del_init(&page->list);
454 spin_unlock(&node->lock);
455 atomic_long_dec(&sgx_nr_free_pages);
461 * __sgx_alloc_epc_page() - Allocate an EPC page
463 * Iterate through NUMA nodes and reserve ia free EPC page to the caller. Start
464 * from the NUMA node, where the caller is executing.
467 * - an EPC page: A borrowed EPC pages were available.
468 * - NULL: Out of EPC pages.
470 struct sgx_epc_page *__sgx_alloc_epc_page(void)
472 struct sgx_epc_page *page;
473 int nid_of_current = numa_node_id();
474 int nid = nid_of_current;
476 if (node_isset(nid_of_current, sgx_numa_mask)) {
477 page = __sgx_alloc_epc_page_from_node(nid_of_current);
482 /* Fall back to the non-local NUMA nodes: */
484 nid = next_node_in(nid, sgx_numa_mask);
485 if (nid == nid_of_current)
488 page = __sgx_alloc_epc_page_from_node(nid);
493 return ERR_PTR(-ENOMEM);
497 * sgx_mark_page_reclaimable() - Mark a page as reclaimable
500 * Mark a page as reclaimable and add it to the active page list. Pages
501 * are automatically removed from the active list when freed.
503 void sgx_mark_page_reclaimable(struct sgx_epc_page *page)
505 spin_lock(&sgx_reclaimer_lock);
506 page->flags |= SGX_EPC_PAGE_RECLAIMER_TRACKED;
507 list_add_tail(&page->list, &sgx_active_page_list);
508 spin_unlock(&sgx_reclaimer_lock);
512 * sgx_unmark_page_reclaimable() - Remove a page from the reclaim list
515 * Clear the reclaimable flag and remove the page from the active page list.
519 * -EBUSY if the page is in the process of being reclaimed
521 int sgx_unmark_page_reclaimable(struct sgx_epc_page *page)
523 spin_lock(&sgx_reclaimer_lock);
524 if (page->flags & SGX_EPC_PAGE_RECLAIMER_TRACKED) {
525 /* The page is being reclaimed. */
526 if (list_empty(&page->list)) {
527 spin_unlock(&sgx_reclaimer_lock);
531 list_del(&page->list);
532 page->flags &= ~SGX_EPC_PAGE_RECLAIMER_TRACKED;
534 spin_unlock(&sgx_reclaimer_lock);
540 * sgx_alloc_epc_page() - Allocate an EPC page
541 * @owner: the owner of the EPC page
542 * @reclaim: reclaim pages if necessary
544 * Iterate through EPC sections and borrow a free EPC page to the caller. When a
545 * page is no longer needed it must be released with sgx_free_epc_page(). If
546 * @reclaim is set to true, directly reclaim pages when we are out of pages. No
547 * mm's can be locked when @reclaim is set to true.
549 * Finally, wake up ksgxd when the number of pages goes below the watermark
550 * before returning back to the caller.
556 struct sgx_epc_page *sgx_alloc_epc_page(void *owner, bool reclaim)
558 struct sgx_epc_page *page;
561 page = __sgx_alloc_epc_page();
567 if (list_empty(&sgx_active_page_list))
568 return ERR_PTR(-ENOMEM);
571 page = ERR_PTR(-EBUSY);
575 if (signal_pending(current)) {
576 page = ERR_PTR(-ERESTARTSYS);
584 if (sgx_should_reclaim(SGX_NR_LOW_PAGES))
585 wake_up(&ksgxd_waitq);
591 * sgx_free_epc_page() - Free an EPC page
594 * Put the EPC page back to the list of free pages. It's the caller's
595 * responsibility to make sure that the page is in uninitialized state. In other
596 * words, do EREMOVE, EWB or whatever operation is necessary before calling
599 void sgx_free_epc_page(struct sgx_epc_page *page)
601 struct sgx_epc_section *section = &sgx_epc_sections[page->section];
602 struct sgx_numa_node *node = section->node;
604 spin_lock(&node->lock);
608 list_add(&page->list, &node->sgx_poison_page_list);
610 list_add_tail(&page->list, &node->free_page_list);
611 page->flags = SGX_EPC_PAGE_IS_FREE;
613 spin_unlock(&node->lock);
614 atomic_long_inc(&sgx_nr_free_pages);
617 static bool __init sgx_setup_epc_section(u64 phys_addr, u64 size,
619 struct sgx_epc_section *section)
621 unsigned long nr_pages = size >> PAGE_SHIFT;
624 section->virt_addr = memremap(phys_addr, size, MEMREMAP_WB);
625 if (!section->virt_addr)
628 section->pages = vmalloc(nr_pages * sizeof(struct sgx_epc_page));
629 if (!section->pages) {
630 memunmap(section->virt_addr);
634 section->phys_addr = phys_addr;
635 xa_store_range(&sgx_epc_address_space, section->phys_addr,
636 phys_addr + size - 1, section, GFP_KERNEL);
638 for (i = 0; i < nr_pages; i++) {
639 section->pages[i].section = index;
640 section->pages[i].flags = 0;
641 section->pages[i].owner = NULL;
642 section->pages[i].poison = 0;
643 list_add_tail(§ion->pages[i].list, &sgx_dirty_page_list);
649 bool arch_is_platform_page(u64 paddr)
651 return !!xa_load(&sgx_epc_address_space, paddr);
653 EXPORT_SYMBOL_GPL(arch_is_platform_page);
655 static struct sgx_epc_page *sgx_paddr_to_page(u64 paddr)
657 struct sgx_epc_section *section;
659 section = xa_load(&sgx_epc_address_space, paddr);
663 return §ion->pages[PFN_DOWN(paddr - section->phys_addr)];
667 * Called in process context to handle a hardware reported
668 * error in an SGX EPC page.
669 * If the MF_ACTION_REQUIRED bit is set in flags, then the
670 * context is the task that consumed the poison data. Otherwise
671 * this is called from a kernel thread unrelated to the page.
673 int arch_memory_failure(unsigned long pfn, int flags)
675 struct sgx_epc_page *page = sgx_paddr_to_page(pfn << PAGE_SHIFT);
676 struct sgx_epc_section *section;
677 struct sgx_numa_node *node;
680 * mm/memory-failure.c calls this routine for all errors
681 * where there isn't a "struct page" for the address. But that
682 * includes other address ranges besides SGX.
688 * If poison was consumed synchronously. Send a SIGBUS to
689 * the task. Hardware has already exited the SGX enclave and
690 * will not allow re-entry to an enclave that has a memory
691 * error. The signal may help the task understand why the
694 if (flags & MF_ACTION_REQUIRED)
697 section = &sgx_epc_sections[page->section];
698 node = section->node;
700 spin_lock(&node->lock);
702 /* Already poisoned? Nothing more to do */
709 * If the page is on a free list, move it to the per-node
712 if (page->flags & SGX_EPC_PAGE_IS_FREE) {
713 list_move(&page->list, &node->sgx_poison_page_list);
718 * TBD: Add additional plumbing to enable pre-emptive
719 * action for asynchronous poison notification. Until
720 * then just hope that the poison:
721 * a) is not accessed - sgx_free_epc_page() will deal with it
722 * when the user gives it back
723 * b) results in a recoverable machine check rather than
727 spin_unlock(&node->lock);
732 * A section metric is concatenated in a way that @low bits 12-31 define the
733 * bits 12-31 of the metric and @high bits 0-19 define the bits 32-51 of the
736 static inline u64 __init sgx_calc_section_metric(u64 low, u64 high)
738 return (low & GENMASK_ULL(31, 12)) +
739 ((high & GENMASK_ULL(19, 0)) << 32);
743 static ssize_t sgx_total_bytes_show(struct device *dev, struct device_attribute *attr, char *buf)
745 return sysfs_emit(buf, "%lu\n", sgx_numa_nodes[dev->id].size);
747 static DEVICE_ATTR_RO(sgx_total_bytes);
749 static umode_t arch_node_attr_is_visible(struct kobject *kobj,
750 struct attribute *attr, int idx)
752 /* Make all x86/ attributes invisible when SGX is not initialized: */
753 if (nodes_empty(sgx_numa_mask))
759 static struct attribute *arch_node_dev_attrs[] = {
760 &dev_attr_sgx_total_bytes.attr,
764 const struct attribute_group arch_node_dev_group = {
766 .attrs = arch_node_dev_attrs,
767 .is_visible = arch_node_attr_is_visible,
770 static void __init arch_update_sysfs_visibility(int nid)
772 struct node *node = node_devices[nid];
775 ret = sysfs_update_group(&node->dev.kobj, &arch_node_dev_group);
778 pr_err("sysfs update failed (%d), files may be invisible", ret);
780 #else /* !CONFIG_NUMA */
781 static void __init arch_update_sysfs_visibility(int nid) {}
784 static bool __init sgx_page_cache_init(void)
786 u32 eax, ebx, ecx, edx, type;
791 sgx_numa_nodes = kmalloc_array(num_possible_nodes(), sizeof(*sgx_numa_nodes), GFP_KERNEL);
795 for (i = 0; i < ARRAY_SIZE(sgx_epc_sections); i++) {
796 cpuid_count(SGX_CPUID, i + SGX_CPUID_EPC, &eax, &ebx, &ecx, &edx);
798 type = eax & SGX_CPUID_EPC_MASK;
799 if (type == SGX_CPUID_EPC_INVALID)
802 if (type != SGX_CPUID_EPC_SECTION) {
803 pr_err_once("Unknown EPC section type: %u\n", type);
807 pa = sgx_calc_section_metric(eax, ebx);
808 size = sgx_calc_section_metric(ecx, edx);
810 pr_info("EPC section 0x%llx-0x%llx\n", pa, pa + size - 1);
812 if (!sgx_setup_epc_section(pa, size, i, &sgx_epc_sections[i])) {
813 pr_err("No free memory for an EPC section\n");
817 nid = numa_map_to_online_node(phys_to_target_node(pa));
818 if (nid == NUMA_NO_NODE) {
819 /* The physical address is already printed above. */
820 pr_warn(FW_BUG "Unable to map EPC section to online node. Fallback to the NUMA node 0.\n");
824 if (!node_isset(nid, sgx_numa_mask)) {
825 spin_lock_init(&sgx_numa_nodes[nid].lock);
826 INIT_LIST_HEAD(&sgx_numa_nodes[nid].free_page_list);
827 INIT_LIST_HEAD(&sgx_numa_nodes[nid].sgx_poison_page_list);
828 node_set(nid, sgx_numa_mask);
829 sgx_numa_nodes[nid].size = 0;
831 /* Make SGX-specific node sysfs files visible: */
832 arch_update_sysfs_visibility(nid);
835 sgx_epc_sections[i].node = &sgx_numa_nodes[nid];
836 sgx_numa_nodes[nid].size += size;
838 sgx_nr_epc_sections++;
841 if (!sgx_nr_epc_sections) {
842 pr_err("There are zero EPC sections.\n");
850 * Update the SGX_LEPUBKEYHASH MSRs to the values specified by caller.
851 * Bare-metal driver requires to update them to hash of enclave's signer
852 * before EINIT. KVM needs to update them to guest's virtual MSR values
853 * before doing EINIT from guest.
855 void sgx_update_lepubkeyhash(u64 *lepubkeyhash)
859 WARN_ON_ONCE(preemptible());
861 for (i = 0; i < 4; i++)
862 wrmsrl(MSR_IA32_SGXLEPUBKEYHASH0 + i, lepubkeyhash[i]);
865 const struct file_operations sgx_provision_fops = {
866 .owner = THIS_MODULE,
869 static struct miscdevice sgx_dev_provision = {
870 .minor = MISC_DYNAMIC_MINOR,
871 .name = "sgx_provision",
872 .nodename = "sgx_provision",
873 .fops = &sgx_provision_fops,
877 * sgx_set_attribute() - Update allowed attributes given file descriptor
878 * @allowed_attributes: Pointer to allowed enclave attributes
879 * @attribute_fd: File descriptor for specific attribute
881 * Append enclave attribute indicated by file descriptor to allowed
882 * attributes. Currently only SGX_ATTR_PROVISIONKEY indicated by
883 * /dev/sgx_provision is supported.
886 * -0: SGX_ATTR_PROVISIONKEY is appended to allowed_attributes
887 * -EINVAL: Invalid, or not supported file descriptor
889 int sgx_set_attribute(unsigned long *allowed_attributes,
890 unsigned int attribute_fd)
894 file = fget(attribute_fd);
898 if (file->f_op != &sgx_provision_fops) {
903 *allowed_attributes |= SGX_ATTR_PROVISIONKEY;
908 EXPORT_SYMBOL_GPL(sgx_set_attribute);
910 static int __init sgx_init(void)
915 if (!cpu_feature_enabled(X86_FEATURE_SGX))
918 if (!sgx_page_cache_init())
921 if (!sgx_page_reclaimer_init()) {
926 ret = misc_register(&sgx_dev_provision);
931 * Always try to initialize the native *and* KVM drivers.
932 * The KVM driver is less picky than the native one and
933 * can function if the native one is not supported on the
934 * current system or fails to initialize.
936 * Error out only if both fail to initialize.
938 ret = sgx_drv_init();
940 if (sgx_vepc_init() && ret)
946 misc_deregister(&sgx_dev_provision);
949 kthread_stop(ksgxd_tsk);
952 for (i = 0; i < sgx_nr_epc_sections; i++) {
953 vfree(sgx_epc_sections[i].pages);
954 memunmap(sgx_epc_sections[i].virt_addr);
960 device_initcall(sgx_init);