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.

Dependency Surface

Detected Declarations

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

Implementation Notes