arch/x86/kernel/cpu/sgx/main.c
Source file repositories/reference/linux-study-clean/arch/x86/kernel/cpu/sgx/main.c
File Facts
- System
- Linux kernel
- Corpus path
arch/x86/kernel/cpu/sgx/main.c- Extension
.c- Size
- 26980 bytes
- Lines
- 1076
- Domain
- Architecture Layer
- Bucket
- arch/x86
- Inferred role
- Architecture Layer: operation-table or driver-model contract
- Status
- pattern implementation candidate
Why This File Exists
CPU and platform-specific kernel glue: boot entry, traps, syscall entry, interrupts, page tables, context switch, and low-level barriers.
- CPU and platform-specific kernel glue: boot entry, traps, syscall entry, interrupts, page tables, context switch, and low-level barriers.
- Defines an operation table; this is where Linux turns generic core objects into subsystem-specific behavior.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/file.hlinux/freezer.hlinux/highmem.hlinux/kthread.hlinux/kvm_types.hlinux/miscdevice.hlinux/node.hlinux/pagemap.hlinux/ratelimit.hlinux/sched/mm.hlinux/sched/signal.hlinux/slab.hlinux/sysfs.hlinux/vmalloc.hasm/cpuid/api.hasm/msr.hasm/sgx.hasm/archrandom.hdriver.hencl.hencls.h
Detected Declarations
function __sgx_sanitize_pagesfunction sgx_free_epc_pagefunction sgx_reclaimer_agefunction list_for_each_entry_rcufunction sgx_reclaimer_blockfunction __sgx_encl_ewbfunction sgx_ipi_cbfunction sgx_reclaimer_writefunction pagesfunction sgx_should_reclaimfunction sgx_reclaim_directfunction ksgxdfunction sgx_page_reclaimer_initfunction current_is_ksgxdfunction __sgx_alloc_epc_pagefunction sgx_mark_page_reclaimablefunction sgx_unmark_page_reclaimablefunction sgx_alloc_epc_pagefunction sgx_free_epc_pagefunction sgx_setup_epc_sectionfunction arch_is_platform_pagefunction arch_memory_failurefunction sgx_calc_section_metricfunction sgx_total_bytes_showfunction arch_node_attr_is_visiblefunction arch_update_sysfs_visibilityfunction arch_update_sysfs_visibilityfunction for_each_online_nodefunction sgx_update_lepubkeyhashfunction sgx_set_attributefunction sgx_update_svnfunction sgx_inc_usage_countfunction sgx_dec_usage_countfunction sgx_initmodule init sgx_initexport arch_is_platform_page
Annotated Snippet
const struct file_operations sgx_provision_fops = {
.owner = THIS_MODULE,
};
static struct miscdevice sgx_dev_provision = {
.minor = MISC_DYNAMIC_MINOR,
.name = "sgx_provision",
.nodename = "sgx_provision",
.fops = &sgx_provision_fops,
};
/**
* sgx_set_attribute() - Update allowed attributes given file descriptor
* @allowed_attributes: Pointer to allowed enclave attributes
* @attribute_fd: File descriptor for specific attribute
*
* Append enclave attribute indicated by file descriptor to allowed
* attributes. Currently only SGX_ATTR_PROVISIONKEY indicated by
* /dev/sgx_provision is supported.
*
* Return:
* -0: SGX_ATTR_PROVISIONKEY is appended to allowed_attributes
* -EINVAL: Invalid, or not supported file descriptor
*/
int sgx_set_attribute(unsigned long *allowed_attributes,
unsigned int attribute_fd)
{
CLASS(fd, f)(attribute_fd);
if (fd_empty(f))
return -EINVAL;
if (fd_file(f)->f_op != &sgx_provision_fops)
return -EINVAL;
*allowed_attributes |= SGX_ATTR_PROVISIONKEY;
return 0;
}
EXPORT_SYMBOL_FOR_KVM(sgx_set_attribute);
/* Counter to count the active SGX users */
static int sgx_usage_count;
/**
* sgx_update_svn() - Attempt to call ENCLS[EUPDATESVN].
*
* This instruction attempts to update CPUSVN to the
* currently loaded microcode update SVN and generate new
* cryptographic assets.
*
* Return:
* * %0: - Success or not supported
* * %-EAGAIN: - Can be safely retried, failure is due to lack of
* * entropy in RNG
* * %-EIO: - Unexpected error, retries are not advisable
*/
static int sgx_update_svn(void)
{
int ret;
/*
* If EUPDATESVN is not available, it is ok to
* silently skip it to comply with legacy behavior.
*/
if (!cpu_feature_enabled(X86_FEATURE_SGX_EUPDATESVN))
return 0;
/*
* EPC is guaranteed to be empty when there are no users.
* Ensure we are on our first user before proceeding further.
*/
WARN(sgx_usage_count, "Elevated usage count when calling EUPDATESVN\n");
for (int i = 0; i < RDRAND_RETRY_LOOPS; i++) {
ret = __eupdatesvn();
/* Stop on success or unexpected errors: */
if (ret != SGX_INSUFFICIENT_ENTROPY)
break;
}
switch (ret) {
case 0:
/*
* SVN successfully updated.
* Let users know when the update was successful.
*/
pr_info("SVN updated successfully\n");
return 0;
case SGX_NO_UPDATE:
Annotation
- Immediate include surface: `linux/file.h`, `linux/freezer.h`, `linux/highmem.h`, `linux/kthread.h`, `linux/kvm_types.h`, `linux/miscdevice.h`, `linux/node.h`, `linux/pagemap.h`.
- Detected declarations: `function __sgx_sanitize_pages`, `function sgx_free_epc_page`, `function sgx_reclaimer_age`, `function list_for_each_entry_rcu`, `function sgx_reclaimer_block`, `function __sgx_encl_ewb`, `function sgx_ipi_cb`, `function sgx_reclaimer_write`, `function pages`, `function sgx_should_reclaim`.
- Atlas domain: Architecture Layer / arch/x86.
- Implementation status: pattern implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.