Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) 2015-2016, Linaro Limited
0003  * All rights reserved.
0004  *
0005  * Redistribution and use in source and binary forms, with or without
0006  * modification, are permitted provided that the following conditions are met:
0007  *
0008  * 1. Redistributions of source code must retain the above copyright notice,
0009  * this list of conditions and the following disclaimer.
0010  *
0011  * 2. Redistributions in binary form must reproduce the above copyright notice,
0012  * this list of conditions and the following disclaimer in the documentation
0013  * and/or other materials provided with the distribution.
0014  *
0015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0018  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
0019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0025  * POSSIBILITY OF SUCH DAMAGE.
0026  */
0027 
0028 #ifndef __TEE_H
0029 #define __TEE_H
0030 
0031 #include <linux/ioctl.h>
0032 #include <linux/types.h>
0033 
0034 /*
0035  * This file describes the API provided by a TEE driver to user space.
0036  *
0037  * Each TEE driver defines a TEE specific protocol which is used for the
0038  * data passed back and forth using TEE_IOC_CMD.
0039  */
0040 
0041 /* Helpers to make the ioctl defines */
0042 #define TEE_IOC_MAGIC   0xa4
0043 #define TEE_IOC_BASE    0
0044 
0045 #define TEE_MAX_ARG_SIZE    1024
0046 
0047 #define TEE_GEN_CAP_GP      (1 << 0)/* GlobalPlatform compliant TEE */
0048 #define TEE_GEN_CAP_PRIVILEGED  (1 << 1)/* Privileged device (for supplicant) */
0049 #define TEE_GEN_CAP_REG_MEM (1 << 2)/* Supports registering shared memory */
0050 #define TEE_GEN_CAP_MEMREF_NULL (1 << 3)/* NULL MemRef support */
0051 
0052 #define TEE_MEMREF_NULL     (__u64)(-1) /* NULL MemRef Buffer */
0053 
0054 /*
0055  * TEE Implementation ID
0056  */
0057 #define TEE_IMPL_ID_OPTEE   1
0058 #define TEE_IMPL_ID_AMDTEE  2
0059 
0060 /*
0061  * OP-TEE specific capabilities
0062  */
0063 #define TEE_OPTEE_CAP_TZ    (1 << 0)
0064 
0065 /**
0066  * struct tee_ioctl_version_data - TEE version
0067  * @impl_id:    [out] TEE implementation id
0068  * @impl_caps:  [out] Implementation specific capabilities
0069  * @gen_caps:   [out] Generic capabilities, defined by TEE_GEN_CAPS_* above
0070  *
0071  * Identifies the TEE implementation, @impl_id is one of TEE_IMPL_ID_* above.
0072  * @impl_caps is implementation specific, for example TEE_OPTEE_CAP_*
0073  * is valid when @impl_id == TEE_IMPL_ID_OPTEE.
0074  */
0075 struct tee_ioctl_version_data {
0076     __u32 impl_id;
0077     __u32 impl_caps;
0078     __u32 gen_caps;
0079 };
0080 
0081 /**
0082  * TEE_IOC_VERSION - query version of TEE
0083  *
0084  * Takes a tee_ioctl_version_data struct and returns with the TEE version
0085  * data filled in.
0086  */
0087 #define TEE_IOC_VERSION     _IOR(TEE_IOC_MAGIC, TEE_IOC_BASE + 0, \
0088                      struct tee_ioctl_version_data)
0089 
0090 /**
0091  * struct tee_ioctl_shm_alloc_data - Shared memory allocate argument
0092  * @size:   [in/out] Size of shared memory to allocate
0093  * @flags:  [in/out] Flags to/from allocation.
0094  * @id:     [out] Identifier of the shared memory
0095  *
0096  * The flags field should currently be zero as input. Updated by the call
0097  * with actual flags as defined by TEE_IOCTL_SHM_* above.
0098  * This structure is used as argument for TEE_IOC_SHM_ALLOC below.
0099  */
0100 struct tee_ioctl_shm_alloc_data {
0101     __u64 size;
0102     __u32 flags;
0103     __s32 id;
0104 };
0105 
0106 /**
0107  * TEE_IOC_SHM_ALLOC - allocate shared memory
0108  *
0109  * Allocates shared memory between the user space process and secure OS.
0110  *
0111  * Returns a file descriptor on success or < 0 on failure
0112  *
0113  * The returned file descriptor is used to map the shared memory into user
0114  * space. The shared memory is freed when the descriptor is closed and the
0115  * memory is unmapped.
0116  */
0117 #define TEE_IOC_SHM_ALLOC   _IOWR(TEE_IOC_MAGIC, TEE_IOC_BASE + 1, \
0118                      struct tee_ioctl_shm_alloc_data)
0119 
0120 /**
0121  * struct tee_ioctl_buf_data - Variable sized buffer
0122  * @buf_ptr:    [in] A __user pointer to a buffer
0123  * @buf_len:    [in] Length of the buffer above
0124  *
0125  * Used as argument for TEE_IOC_OPEN_SESSION, TEE_IOC_INVOKE,
0126  * TEE_IOC_SUPPL_RECV, and TEE_IOC_SUPPL_SEND below.
0127  */
0128 struct tee_ioctl_buf_data {
0129     __u64 buf_ptr;
0130     __u64 buf_len;
0131 };
0132 
0133 /*
0134  * Attributes for struct tee_ioctl_param, selects field in the union
0135  */
0136 #define TEE_IOCTL_PARAM_ATTR_TYPE_NONE      0   /* parameter not used */
0137 
0138 /*
0139  * These defines value parameters (struct tee_ioctl_param_value)
0140  */
0141 #define TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT   1
0142 #define TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT  2
0143 #define TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT   3   /* input and output */
0144 
0145 /*
0146  * These defines shared memory reference parameters (struct
0147  * tee_ioctl_param_memref)
0148  */
0149 #define TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT  5
0150 #define TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT 6
0151 #define TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT  7   /* input and output */
0152 
0153 /*
0154  * Mask for the type part of the attribute, leaves room for more types
0155  */
0156 #define TEE_IOCTL_PARAM_ATTR_TYPE_MASK      0xff
0157 
0158 /* Meta parameter carrying extra information about the message. */
0159 #define TEE_IOCTL_PARAM_ATTR_META       0x100
0160 
0161 /* Mask of all known attr bits */
0162 #define TEE_IOCTL_PARAM_ATTR_MASK \
0163     (TEE_IOCTL_PARAM_ATTR_TYPE_MASK | TEE_IOCTL_PARAM_ATTR_META)
0164 
0165 /*
0166  * Matches TEEC_LOGIN_* in GP TEE Client API
0167  * Are only defined for GP compliant TEEs
0168  */
0169 #define TEE_IOCTL_LOGIN_PUBLIC          0
0170 #define TEE_IOCTL_LOGIN_USER            1
0171 #define TEE_IOCTL_LOGIN_GROUP           2
0172 #define TEE_IOCTL_LOGIN_APPLICATION     4
0173 #define TEE_IOCTL_LOGIN_USER_APPLICATION    5
0174 #define TEE_IOCTL_LOGIN_GROUP_APPLICATION   6
0175 /*
0176  * Disallow user-space to use GP implementation specific login
0177  * method range (0x80000000 - 0xBFFFFFFF). This range is rather
0178  * being reserved for REE kernel clients or TEE implementation.
0179  */
0180 #define TEE_IOCTL_LOGIN_REE_KERNEL_MIN      0x80000000
0181 #define TEE_IOCTL_LOGIN_REE_KERNEL_MAX      0xBFFFFFFF
0182 /* Private login method for REE kernel clients */
0183 #define TEE_IOCTL_LOGIN_REE_KERNEL      0x80000000
0184 
0185 /**
0186  * struct tee_ioctl_param - parameter
0187  * @attr: attributes
0188  * @a: if a memref, offset into the shared memory object, else a value parameter
0189  * @b: if a memref, size of the buffer, else a value parameter
0190  * @c: if a memref, shared memory identifier, else a value parameter
0191  *
0192  * @attr & TEE_PARAM_ATTR_TYPE_MASK indicates if memref or value is used in
0193  * the union. TEE_PARAM_ATTR_TYPE_VALUE_* indicates value and
0194  * TEE_PARAM_ATTR_TYPE_MEMREF_* indicates memref. TEE_PARAM_ATTR_TYPE_NONE
0195  * indicates that none of the members are used.
0196  *
0197  * Shared memory is allocated with TEE_IOC_SHM_ALLOC which returns an
0198  * identifier representing the shared memory object. A memref can reference
0199  * a part of a shared memory by specifying an offset (@a) and size (@b) of
0200  * the object. To supply the entire shared memory object set the offset
0201  * (@a) to 0 and size (@b) to the previously returned size of the object.
0202  *
0203  * A client may need to present a NULL pointer in the argument
0204  * passed to a trusted application in the TEE.
0205  * This is also a requirement in GlobalPlatform Client API v1.0c
0206  * (section 3.2.5 memory references), which can be found at
0207  * http://www.globalplatform.org/specificationsdevice.asp
0208  *
0209  * If a NULL pointer is passed to a TA in the TEE, the (@c)
0210  * IOCTL parameters value must be set to TEE_MEMREF_NULL indicating a NULL
0211  * memory reference.
0212  */
0213 struct tee_ioctl_param {
0214     __u64 attr;
0215     __u64 a;
0216     __u64 b;
0217     __u64 c;
0218 };
0219 
0220 #define TEE_IOCTL_UUID_LEN      16
0221 
0222 /**
0223  * struct tee_ioctl_open_session_arg - Open session argument
0224  * @uuid:   [in] UUID of the Trusted Application
0225  * @clnt_uuid:  [in] UUID of client
0226  * @clnt_login: [in] Login class of client, TEE_IOCTL_LOGIN_* above
0227  * @cancel_id:  [in] Cancellation id, a unique value to identify this request
0228  * @session:    [out] Session id
0229  * @ret:    [out] return value
0230  * @ret_origin  [out] origin of the return value
0231  * @num_params  [in] number of parameters following this struct
0232  */
0233 struct tee_ioctl_open_session_arg {
0234     __u8 uuid[TEE_IOCTL_UUID_LEN];
0235     __u8 clnt_uuid[TEE_IOCTL_UUID_LEN];
0236     __u32 clnt_login;
0237     __u32 cancel_id;
0238     __u32 session;
0239     __u32 ret;
0240     __u32 ret_origin;
0241     __u32 num_params;
0242     /* num_params tells the actual number of element in params */
0243     struct tee_ioctl_param params[];
0244 };
0245 
0246 /**
0247  * TEE_IOC_OPEN_SESSION - opens a session to a Trusted Application
0248  *
0249  * Takes a struct tee_ioctl_buf_data which contains a struct
0250  * tee_ioctl_open_session_arg followed by any array of struct
0251  * tee_ioctl_param
0252  */
0253 #define TEE_IOC_OPEN_SESSION    _IOR(TEE_IOC_MAGIC, TEE_IOC_BASE + 2, \
0254                      struct tee_ioctl_buf_data)
0255 
0256 /**
0257  * struct tee_ioctl_invoke_func_arg - Invokes a function in a Trusted
0258  * Application
0259  * @func:   [in] Trusted Application function, specific to the TA
0260  * @session:    [in] Session id
0261  * @cancel_id:  [in] Cancellation id, a unique value to identify this request
0262  * @ret:    [out] return value
0263  * @ret_origin  [out] origin of the return value
0264  * @num_params  [in] number of parameters following this struct
0265  */
0266 struct tee_ioctl_invoke_arg {
0267     __u32 func;
0268     __u32 session;
0269     __u32 cancel_id;
0270     __u32 ret;
0271     __u32 ret_origin;
0272     __u32 num_params;
0273     /* num_params tells the actual number of element in params */
0274     struct tee_ioctl_param params[];
0275 };
0276 
0277 /**
0278  * TEE_IOC_INVOKE - Invokes a function in a Trusted Application
0279  *
0280  * Takes a struct tee_ioctl_buf_data which contains a struct
0281  * tee_invoke_func_arg followed by any array of struct tee_param
0282  */
0283 #define TEE_IOC_INVOKE      _IOR(TEE_IOC_MAGIC, TEE_IOC_BASE + 3, \
0284                      struct tee_ioctl_buf_data)
0285 
0286 /**
0287  * struct tee_ioctl_cancel_arg - Cancels an open session or invoke ioctl
0288  * @cancel_id:  [in] Cancellation id, a unique value to identify this request
0289  * @session:    [in] Session id, if the session is opened, else set to 0
0290  */
0291 struct tee_ioctl_cancel_arg {
0292     __u32 cancel_id;
0293     __u32 session;
0294 };
0295 
0296 /**
0297  * TEE_IOC_CANCEL - Cancels an open session or invoke
0298  */
0299 #define TEE_IOC_CANCEL      _IOR(TEE_IOC_MAGIC, TEE_IOC_BASE + 4, \
0300                      struct tee_ioctl_cancel_arg)
0301 
0302 /**
0303  * struct tee_ioctl_close_session_arg - Closes an open session
0304  * @session:    [in] Session id
0305  */
0306 struct tee_ioctl_close_session_arg {
0307     __u32 session;
0308 };
0309 
0310 /**
0311  * TEE_IOC_CLOSE_SESSION - Closes a session
0312  */
0313 #define TEE_IOC_CLOSE_SESSION   _IOR(TEE_IOC_MAGIC, TEE_IOC_BASE + 5, \
0314                      struct tee_ioctl_close_session_arg)
0315 
0316 /**
0317  * struct tee_iocl_supp_recv_arg - Receive a request for a supplicant function
0318  * @func:   [in] supplicant function
0319  * @num_params  [in/out] number of parameters following this struct
0320  *
0321  * @num_params is the number of params that tee-supplicant has room to
0322  * receive when input, @num_params is the number of actual params
0323  * tee-supplicant receives when output.
0324  */
0325 struct tee_iocl_supp_recv_arg {
0326     __u32 func;
0327     __u32 num_params;
0328     /* num_params tells the actual number of element in params */
0329     struct tee_ioctl_param params[];
0330 };
0331 
0332 /**
0333  * TEE_IOC_SUPPL_RECV - Receive a request for a supplicant function
0334  *
0335  * Takes a struct tee_ioctl_buf_data which contains a struct
0336  * tee_iocl_supp_recv_arg followed by any array of struct tee_param
0337  */
0338 #define TEE_IOC_SUPPL_RECV  _IOR(TEE_IOC_MAGIC, TEE_IOC_BASE + 6, \
0339                      struct tee_ioctl_buf_data)
0340 
0341 /**
0342  * struct tee_iocl_supp_send_arg - Send a response to a received request
0343  * @ret:    [out] return value
0344  * @num_params  [in] number of parameters following this struct
0345  */
0346 struct tee_iocl_supp_send_arg {
0347     __u32 ret;
0348     __u32 num_params;
0349     /* num_params tells the actual number of element in params */
0350     struct tee_ioctl_param params[];
0351 };
0352 
0353 /**
0354  * TEE_IOC_SUPPL_SEND - Send a response to a received request
0355  *
0356  * Takes a struct tee_ioctl_buf_data which contains a struct
0357  * tee_iocl_supp_send_arg followed by any array of struct tee_param
0358  */
0359 #define TEE_IOC_SUPPL_SEND  _IOR(TEE_IOC_MAGIC, TEE_IOC_BASE + 7, \
0360                      struct tee_ioctl_buf_data)
0361 
0362 /**
0363  * struct tee_ioctl_shm_register_data - Shared memory register argument
0364  * @addr:      [in] Start address of shared memory to register
0365  * @length:    [in/out] Length of shared memory to register
0366  * @flags:     [in/out] Flags to/from registration.
0367  * @id:                [out] Identifier of the shared memory
0368  *
0369  * The flags field should currently be zero as input. Updated by the call
0370  * with actual flags as defined by TEE_IOCTL_SHM_* above.
0371  * This structure is used as argument for TEE_IOC_SHM_REGISTER below.
0372  */
0373 struct tee_ioctl_shm_register_data {
0374     __u64 addr;
0375     __u64 length;
0376     __u32 flags;
0377     __s32 id;
0378 };
0379 
0380 /**
0381  * TEE_IOC_SHM_REGISTER - Register shared memory argument
0382  *
0383  * Registers shared memory between the user space process and secure OS.
0384  *
0385  * Returns a file descriptor on success or < 0 on failure
0386  *
0387  * The shared memory is unregisterred when the descriptor is closed.
0388  */
0389 #define TEE_IOC_SHM_REGISTER   _IOWR(TEE_IOC_MAGIC, TEE_IOC_BASE + 9, \
0390                      struct tee_ioctl_shm_register_data)
0391 /*
0392  * Five syscalls are used when communicating with the TEE driver.
0393  * open(): opens the device associated with the driver
0394  * ioctl(): as described above operating on the file descriptor from open()
0395  * close(): two cases
0396  *   - closes the device file descriptor
0397  *   - closes a file descriptor connected to allocated shared memory
0398  * mmap(): maps shared memory into user space using information from struct
0399  *     tee_ioctl_shm_alloc_data
0400  * munmap(): unmaps previously shared memory
0401  */
0402 
0403 #endif /*__TEE_H*/