/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */1/*2* Copyright(c) 2016-20 Intel Corporation.3*/4#ifndef _UAPI_ASM_X86_SGX_H5#define _UAPI_ASM_X86_SGX_H67#include <linux/types.h>8#include <linux/ioctl.h>910/**11* enum sgx_page_flags - page control flags12* %SGX_PAGE_MEASURE: Measure the page contents with a sequence of13* ENCLS[EEXTEND] operations.14*/15enum sgx_page_flags {16SGX_PAGE_MEASURE = 0x01,17};1819#define SGX_MAGIC 0xA42021#define SGX_IOC_ENCLAVE_CREATE \22_IOW(SGX_MAGIC, 0x00, struct sgx_enclave_create)23#define SGX_IOC_ENCLAVE_ADD_PAGES \24_IOWR(SGX_MAGIC, 0x01, struct sgx_enclave_add_pages)25#define SGX_IOC_ENCLAVE_INIT \26_IOW(SGX_MAGIC, 0x02, struct sgx_enclave_init)27#define SGX_IOC_ENCLAVE_PROVISION \28_IOW(SGX_MAGIC, 0x03, struct sgx_enclave_provision)29#define SGX_IOC_VEPC_REMOVE_ALL \30_IO(SGX_MAGIC, 0x04)31#define SGX_IOC_ENCLAVE_RESTRICT_PERMISSIONS \32_IOWR(SGX_MAGIC, 0x05, struct sgx_enclave_restrict_permissions)33#define SGX_IOC_ENCLAVE_MODIFY_TYPES \34_IOWR(SGX_MAGIC, 0x06, struct sgx_enclave_modify_types)35#define SGX_IOC_ENCLAVE_REMOVE_PAGES \36_IOWR(SGX_MAGIC, 0x07, struct sgx_enclave_remove_pages)3738/**39* struct sgx_enclave_create - parameter structure for the40* %SGX_IOC_ENCLAVE_CREATE ioctl41* @src: address for the SECS page data42*/43struct sgx_enclave_create {44__u64 src;45};4647/**48* struct sgx_enclave_add_pages - parameter structure for the49* %SGX_IOC_ENCLAVE_ADD_PAGE ioctl50* @src: start address for the page data51* @offset: starting page offset52* @length: length of the data (multiple of the page size)53* @secinfo: address for the SECINFO data54* @flags: page control flags55* @count: number of bytes added (multiple of the page size)56*/57struct sgx_enclave_add_pages {58__u64 src;59__u64 offset;60__u64 length;61__u64 secinfo;62__u64 flags;63__u64 count;64};6566/**67* struct sgx_enclave_init - parameter structure for the68* %SGX_IOC_ENCLAVE_INIT ioctl69* @sigstruct: address for the SIGSTRUCT data70*/71struct sgx_enclave_init {72__u64 sigstruct;73};7475/**76* struct sgx_enclave_provision - parameter structure for the77* %SGX_IOC_ENCLAVE_PROVISION ioctl78* @fd: file handle of /dev/sgx_provision79*/80struct sgx_enclave_provision {81__u64 fd;82};8384/**85* struct sgx_enclave_restrict_permissions - parameters for ioctl86* %SGX_IOC_ENCLAVE_RESTRICT_PERMISSIONS87* @offset: starting page offset (page aligned relative to enclave base88* address defined in SECS)89* @length: length of memory (multiple of the page size)90* @permissions:new permission bits for pages in range described by @offset91* and @length92* @result: (output) SGX result code of ENCLS[EMODPR] function93* @count: (output) bytes successfully changed (multiple of page size)94*/95struct sgx_enclave_restrict_permissions {96__u64 offset;97__u64 length;98__u64 permissions;99__u64 result;100__u64 count;101};102103/**104* struct sgx_enclave_modify_types - parameters for ioctl105* %SGX_IOC_ENCLAVE_MODIFY_TYPES106* @offset: starting page offset (page aligned relative to enclave base107* address defined in SECS)108* @length: length of memory (multiple of the page size)109* @page_type: new type for pages in range described by @offset and @length110* @result: (output) SGX result code of ENCLS[EMODT] function111* @count: (output) bytes successfully changed (multiple of page size)112*/113struct sgx_enclave_modify_types {114__u64 offset;115__u64 length;116__u64 page_type;117__u64 result;118__u64 count;119};120121/**122* struct sgx_enclave_remove_pages - %SGX_IOC_ENCLAVE_REMOVE_PAGES parameters123* @offset: starting page offset (page aligned relative to enclave base124* address defined in SECS)125* @length: length of memory (multiple of the page size)126* @count: (output) bytes successfully changed (multiple of page size)127*128* Regular (PT_REG) or TCS (PT_TCS) can be removed from an initialized129* enclave if the system supports SGX2. First, the %SGX_IOC_ENCLAVE_MODIFY_TYPES130* ioctl() should be used to change the page type to PT_TRIM. After that131* succeeds ENCLU[EACCEPT] should be run from within the enclave and then132* %SGX_IOC_ENCLAVE_REMOVE_PAGES can be used to complete the page removal.133*/134struct sgx_enclave_remove_pages {135__u64 offset;136__u64 length;137__u64 count;138};139140struct sgx_enclave_run;141142/**143* typedef sgx_enclave_user_handler_t - Exit handler function accepted by144* __vdso_sgx_enter_enclave()145* @run: The run instance given by the caller146*147* The register parameters contain the snapshot of their values at enclave148* exit. An invalid ENCLU function number will cause -EINVAL to be returned149* to the caller.150*151* Return:152* - <= 0: The given value is returned back to the caller.153* - > 0: ENCLU function to invoke, either EENTER or ERESUME.154*/155typedef int (*sgx_enclave_user_handler_t)(long rdi, long rsi, long rdx,156long rsp, long r8, long r9,157struct sgx_enclave_run *run);158159/**160* struct sgx_enclave_run - the execution context of __vdso_sgx_enter_enclave()161* @tcs: TCS used to enter the enclave162* @function: The last seen ENCLU function (EENTER, ERESUME or EEXIT)163* @exception_vector: The interrupt vector of the exception164* @exception_error_code: The exception error code pulled out of the stack165* @exception_addr: The address that triggered the exception166* @user_handler: User provided callback run on exception167* @user_data: Data passed to the user handler168* @reserved Reserved for future extensions169*170* If @user_handler is provided, the handler will be invoked on all return paths171* of the normal flow. The user handler may transfer control, e.g. via a172* longjmp() call or a C++ exception, without returning to173* __vdso_sgx_enter_enclave().174*/175struct sgx_enclave_run {176__u64 tcs;177__u32 function;178__u16 exception_vector;179__u16 exception_error_code;180__u64 exception_addr;181__u64 user_handler;182__u64 user_data;183__u8 reserved[216];184};185186/**187* typedef vdso_sgx_enter_enclave_t - Prototype for __vdso_sgx_enter_enclave(),188* a vDSO function to enter an SGX enclave.189* @rdi: Pass-through value for RDI190* @rsi: Pass-through value for RSI191* @rdx: Pass-through value for RDX192* @function: ENCLU function, must be EENTER or ERESUME193* @r8: Pass-through value for R8194* @r9: Pass-through value for R9195* @run: struct sgx_enclave_run, must be non-NULL196*197* NOTE: __vdso_sgx_enter_enclave() does not ensure full compliance with the198* x86-64 ABI, e.g. doesn't handle XSAVE state. Except for non-volatile199* general purpose registers, EFLAGS.DF, and RSP alignment, preserving/setting200* state in accordance with the x86-64 ABI is the responsibility of the enclave201* and its runtime, i.e. __vdso_sgx_enter_enclave() cannot be called from C202* code without careful consideration by both the enclave and its runtime.203*204* All general purpose registers except RAX, RBX and RCX are passed as-is to the205* enclave. RAX, RBX and RCX are consumed by EENTER and ERESUME and are loaded206* with @function, asynchronous exit pointer, and @run.tcs respectively.207*208* RBP and the stack are used to anchor __vdso_sgx_enter_enclave() to the209* pre-enclave state, e.g. to retrieve @run.exception and @run.user_handler210* after an enclave exit. All other registers are available for use by the211* enclave and its runtime, e.g. an enclave can push additional data onto the212* stack (and modify RSP) to pass information to the optional user handler (see213* below).214*215* Most exceptions reported on ENCLU, including those that occur within the216* enclave, are fixed up and reported synchronously instead of being delivered217* via a standard signal. Debug Exceptions (#DB) and Breakpoints (#BP) are218* never fixed up and are always delivered via standard signals. On synchronously219* reported exceptions, -EFAULT is returned and details about the exception are220* recorded in @run.exception, the optional sgx_enclave_exception struct.221*222* Return:223* - 0: ENCLU function was successfully executed.224* - -EINVAL: Invalid ENCL number (neither EENTER nor ERESUME).225*/226typedef int (*vdso_sgx_enter_enclave_t)(unsigned long rdi, unsigned long rsi,227unsigned long rdx, unsigned int function,228unsigned long r8, unsigned long r9,229struct sgx_enclave_run *run);230231#endif /* _UAPI_ASM_X86_SGX_H */232233234