/* * This file implement the SMC ABI used when communicating with secure world * OP-TEE OS via raw SMCs. * This file is divided into the following sections: * 1. Convert between struct tee_param and struct optee_msg_param * 2. Low level support functions to register shared memory in secure world * 3. Dynamic shared memory pool based on alloc_pages() * 4. Do a normal scheduled call into secure world * 5. Asynchronous notification * 6. Driver initialization.
*/
/* * A typical OP-TEE private shm allocation is 224 bytes (argument struct * with 6 parameters, needed for open session). So with an alignment of 512 * we'll waste a bit more than 50%. However, it's only expected that we'll * have a handful of these structs allocated at a time. Most memory will * be allocated aligned to the page size, So all in all this should scale * up and down quite well.
*/ #define OPTEE_MIN_STATIC_POOL_ALIGN 9 /* 512 bytes aligned */
/* SMC ABI considers at most a single TEE firmware */ staticunsignedint pcpu_irq_num;
/** * optee_from_msg_param() - convert from OPTEE_MSG parameters to * struct tee_param * @optee: main service struct * @params: subsystem internal parameter representation * @num_params: number of elements in the parameter arrays * @msg_params: OPTEE_MSG parameters * Returns 0 on success or <0 on failure
*/ staticint optee_from_msg_param(struct optee *optee, struct tee_param *params,
size_t num_params, conststruct optee_msg_param *msg_params)
{ int rc;
size_t n;
switch (attr) { case OPTEE_MSG_ATTR_TYPE_NONE:
p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_NONE;
memset(&p->u, 0, sizeof(p->u)); break; case OPTEE_MSG_ATTR_TYPE_VALUE_INPUT: case OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT: case OPTEE_MSG_ATTR_TYPE_VALUE_INOUT:
optee_from_msg_param_value(p, attr, mp); break; case OPTEE_MSG_ATTR_TYPE_TMEM_INPUT: case OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT: case OPTEE_MSG_ATTR_TYPE_TMEM_INOUT:
rc = from_msg_param_tmp_mem(p, attr, mp); if (rc) return rc; break; case OPTEE_MSG_ATTR_TYPE_RMEM_INPUT: case OPTEE_MSG_ATTR_TYPE_RMEM_OUTPUT: case OPTEE_MSG_ATTR_TYPE_RMEM_INOUT:
from_msg_param_reg_mem(p, attr, mp); break;
/** * optee_to_msg_param() - convert from struct tee_params to OPTEE_MSG parameters * @optee: main service struct * @msg_params: OPTEE_MSG parameters * @num_params: number of elements in the parameter arrays * @params: subsystem itnernal parameter representation * Returns 0 on success or <0 on failure
*/ staticint optee_to_msg_param(struct optee *optee, struct optee_msg_param *msg_params,
size_t num_params, conststruct tee_param *params)
{ int rc;
size_t n;
switch (p->attr) { case TEE_IOCTL_PARAM_ATTR_TYPE_NONE:
mp->attr = TEE_IOCTL_PARAM_ATTR_TYPE_NONE;
memset(&mp->u, 0, sizeof(mp->u)); break; case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT: case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT: case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
optee_to_msg_param_value(mp, p); break; case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT: case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT: case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT: if (tee_shm_is_dynamic(p->u.memref.shm))
rc = to_msg_param_reg_mem(mp, p); else
rc = to_msg_param_tmp_mem(mp, p); if (rc) return rc; break; default: return -EINVAL;
}
} return 0;
}
/* * 2. Low level support functions to register shared memory in secure world * * Functions to enable/disable shared memory caching in secure world, that * is, lazy freeing of previously allocated shared memory. Freeing is * performed when a request has been compled. * * Functions to register and unregister shared memory both for normal * clients and for tee-supplicant.
*/
/** * optee_enable_shm_cache() - Enables caching of some shared memory allocation * in OP-TEE * @optee: main service struct
*/ staticvoid optee_enable_shm_cache(struct optee *optee)
{ struct optee_call_waiter w;
/* We need to retry until secure world isn't busy. */
optee_cq_wait_init(&optee->call_queue, &w, false); while (true) { struct arm_smccc_res res;
/** * __optee_disable_shm_cache() - Disables caching of some shared memory * allocation in OP-TEE * @optee: main service struct * @is_mapped: true if the cached shared memory addresses were mapped by this * kernel, are safe to dereference, and should be freed
*/ staticvoid __optee_disable_shm_cache(struct optee *optee, bool is_mapped)
{ struct optee_call_waiter w;
/* We need to retry until secure world isn't busy. */
optee_cq_wait_init(&optee->call_queue, &w, false); while (true) { union { struct arm_smccc_res smccc; struct optee_smc_disable_shm_cache_result result;
} res;
optee->smc.invoke_fn(OPTEE_SMC_DISABLE_SHM_CACHE,
0, 0, 0, 0, 0, 0, 0, &res.smccc); if (res.result.status == OPTEE_SMC_RETURN_ENOTAVAIL) break; /* All shm's freed */ if (res.result.status == OPTEE_SMC_RETURN_OK) { struct tee_shm *shm;
/* * Shared memory references that were not mapped by * this kernel must be ignored to prevent a crash.
*/ if (!is_mapped) continue;
/** * optee_disable_shm_cache() - Disables caching of mapped shared memory * allocations in OP-TEE * @optee: main service struct
*/ staticvoid optee_disable_shm_cache(struct optee *optee)
{ return __optee_disable_shm_cache(optee, true);
}
/** * optee_disable_unmapped_shm_cache() - Disables caching of shared memory * allocations in OP-TEE which are not * currently mapped * @optee: main service struct
*/ staticvoid optee_disable_unmapped_shm_cache(struct optee *optee)
{ return __optee_disable_shm_cache(optee, false);
}
/* * The final entry in each pagelist page is a pointer to the next * pagelist page.
*/ static size_t get_pages_list_size(size_t num_entries)
{ int pages = DIV_ROUND_UP(num_entries, PAGELIST_ENTRIES_PER_PAGE);
/** * optee_fill_pages_list() - write list of user pages to given shared * buffer. * * @dst: page-aligned buffer where list of pages will be stored * @pages: array of pages that represents shared buffer * @num_pages: number of entries in @pages * @page_offset: offset of user buffer from page start * * @dst should be big enough to hold list of user page addresses and * links to the next pages of buffer
*/ staticvoid optee_fill_pages_list(u64 *dst, struct page **pages, int num_pages,
size_t page_offset)
{ int n = 0;
phys_addr_t optee_page; /* * Refer to OPTEE_MSG_ATTR_NONCONTIG description in optee_msg.h * for details.
*/ struct {
u64 pages_list[PAGELIST_ENTRIES_PER_PAGE];
u64 next_page_data;
} *pages_data;
/* * Currently OP-TEE uses 4k page size and it does not looks * like this will change in the future. On other hand, there are * no know ARM architectures with page size < 4k. * Thus the next built assert looks redundant. But the following * code heavily relies on this assumption, so it is better be * safe than sorry.
*/
BUILD_BUG_ON(PAGE_SIZE < OPTEE_MSG_NONCONTIG_PAGE_SIZE);
pages_data = (void *)dst; /* * If linux page is bigger than 4k, and user buffer offset is * larger than 4k/8k/12k/etc this will skip first 4k pages, * because they bear no value data for OP-TEE.
*/
optee_page = page_to_phys(*pages) +
round_down(page_offset, OPTEE_MSG_NONCONTIG_PAGE_SIZE);
while (true) {
pages_data->pages_list[n++] = optee_page;
if (n == PAGELIST_ENTRIES_PER_PAGE) {
pages_data->next_page_data =
virt_to_phys(pages_data + 1);
pages_data++;
n = 0;
}
optee_page += OPTEE_MSG_NONCONTIG_PAGE_SIZE; if (!(optee_page & ~PAGE_MASK)) { if (!--num_pages) break;
pages++;
optee_page = page_to_phys(*pages);
}
}
}
rc = optee_check_mem_type(start, num_pages); if (rc) return rc;
pages_list = optee_allocate_pages_list(num_pages); if (!pages_list) return -ENOMEM;
/* * We're about to register shared memory we can't register shared * memory for this request or there's a catch-22. * * So in this we'll have to do the good old temporary private * allocation instead of using optee_get_msg_arg().
*/
sz = optee_msg_arg_size(optee->rpc_param_count);
shm_arg = tee_shm_alloc_priv_buf(ctx, sz); if (IS_ERR(shm_arg)) {
rc = PTR_ERR(shm_arg); goto out;
}
msg_arg = tee_shm_get_va(shm_arg, 0); if (IS_ERR(msg_arg)) {
rc = PTR_ERR(msg_arg); goto out;
}
memset(msg_arg, 0, OPTEE_MSG_GET_ARG_SIZE(1));
msg_arg->num_params = 1;
msg_arg->cmd = OPTEE_MSG_CMD_REGISTER_SHM;
msg_arg->params->attr = OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT |
OPTEE_MSG_ATTR_NONCONTIG;
msg_arg->params->u.tmem.shm_ref = (unsignedlong)shm;
msg_arg->params->u.tmem.size = tee_shm_get_size(shm); /* * In the least bits of msg_arg->params->u.tmem.buf_ptr we * store buffer offset from 4k page, as described in OP-TEE ABI.
*/
msg_arg->params->u.tmem.buf_ptr = virt_to_phys(pages_list) |
(tee_shm_get_page_offset(shm) & (OPTEE_MSG_NONCONTIG_PAGE_SIZE - 1));
/* * We're about to unregister shared memory and we may not be able * register shared memory for this request in case we're called * from optee_shm_arg_cache_uninit(). * * So in order to keep things simple in this function just as in * optee_shm_register() we'll use temporary private allocation * instead of using optee_get_msg_arg().
*/
sz = optee_msg_arg_size(optee->rpc_param_count);
shm_arg = tee_shm_alloc_priv_buf(ctx, sz); if (IS_ERR(shm_arg)) return PTR_ERR(shm_arg);
msg_arg = tee_shm_get_va(shm_arg, 0); if (IS_ERR(msg_arg)) {
rc = PTR_ERR(msg_arg); goto out;
}
staticint optee_shm_register_supp(struct tee_context *ctx, struct tee_shm *shm, struct page **pages, size_t num_pages, unsignedlong start)
{ /* * We don't want to register supplicant memory in OP-TEE. * Instead information about it will be passed in RPC code.
*/ return optee_check_mem_type(start, num_pages);
}
/* * 3. Dynamic shared memory pool based on alloc_pages() * * Implements an OP-TEE specific shared memory pool which is used * when dynamic shared memory is supported by secure world. * * The main function is optee_shm_pool_alloc_pages().
*/
staticint pool_op_alloc(struct tee_shm_pool *pool, struct tee_shm *shm, size_t size, size_t align)
{ /* * Shared memory private to the OP-TEE driver doesn't need * to be registered with OP-TEE.
*/ if (shm->flags & TEE_SHM_PRIV) return tee_dyn_shm_alloc_helper(shm, size, align, NULL);
/** * optee_shm_pool_alloc_pages() - create page-based allocator pool * * This pool is used when OP-TEE supports dymanic SHM. In this case * command buffers and such are allocated from kernel's own memory.
*/ staticstruct tee_shm_pool *optee_shm_pool_alloc_pages(void)
{ struct tee_shm_pool *pool = kzalloc(sizeof(*pool), GFP_KERNEL);
if (!pool) return ERR_PTR(-ENOMEM);
pool->ops = &pool_ops;
return pool;
}
/* * 4. Do a normal scheduled call into secure world * * The function optee_smc_do_call_with_arg() performs a normal scheduled * call into secure world. During this call may normal world request help * from normal world using RPCs, Remote Procedure Calls. This includes * delivery of non-secure interrupts to for instance allow rescheduling of * the current task.
*/
if (IS_ERR(shm)) {
arg->ret = TEEC_ERROR_OUT_OF_MEMORY; return;
}
/* * If there are pages it's dynamically allocated shared memory (not * from the reserved shared memory pool) and needs to be * registered.
*/
pages = tee_shm_get_pages(shm, &page_count); if (pages) {
u64 *pages_list;
arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT |
OPTEE_MSG_ATTR_NONCONTIG; /* * In the least bits of u.tmem.buf_ptr we store buffer offset * from 4k page, as described in OP-TEE ABI.
*/
arg->params[0].u.tmem.buf_ptr = virt_to_phys(pages_list) |
(tee_shm_get_page_offset(shm) &
(OPTEE_MSG_NONCONTIG_PAGE_SIZE - 1));
/** * optee_handle_rpc() - handle RPC from secure world * @ctx: context doing the RPC * @rpc_arg: pointer to RPC arguments if any, or NULL if none * @param: value of registers for the RPC * @call_ctx: call context. Preserved during one OP-TEE invocation * * Result of RPC is written back into @param.
*/ staticvoid optee_handle_rpc(struct tee_context *ctx, struct optee_msg_arg *rpc_arg, struct optee_rpc_param *param, struct optee_call_ctx *call_ctx)
{ struct tee_device *teedev = ctx->teedev; struct optee *optee = tee_get_drvdata(teedev); struct optee_msg_arg *arg; struct tee_shm *shm;
phys_addr_t pa;
switch (OPTEE_SMC_RETURN_GET_RPC_FUNC(param->a0)) { case OPTEE_SMC_RPC_FUNC_ALLOC:
shm = tee_shm_alloc_priv_buf(optee->ctx, param->a1); if (!IS_ERR(shm) && !tee_shm_get_pa(shm, 0, &pa)) {
reg_pair_from_64(¶m->a1, ¶m->a2, pa);
reg_pair_from_64(¶m->a4, ¶m->a5,
(unsignedlong)shm);
} else {
param->a1 = 0;
param->a2 = 0;
param->a4 = 0;
param->a5 = 0;
}
kmemleak_not_leak(shm); break; case OPTEE_SMC_RPC_FUNC_FREE:
shm = reg_pair_to_ptr(param->a1, param->a2);
tee_shm_free(shm); break; case OPTEE_SMC_RPC_FUNC_FOREIGN_INTR: /* * A foreign interrupt was raised while secure world was * executing, since they are handled in Linux a dummy RPC is * performed to let Linux take the interrupt through the normal * vector.
*/ break; case OPTEE_SMC_RPC_FUNC_CMD: if (rpc_arg) {
arg = rpc_arg;
} else {
shm = reg_pair_to_ptr(param->a1, param->a2);
arg = tee_shm_get_va(shm, 0); if (IS_ERR(arg)) {
pr_err("%s: tee_shm_get_va %p failed\n",
__func__, shm); break;
}
}
/** * optee_smc_do_call_with_arg() - Do an SMC to OP-TEE in secure world * @ctx: calling context * @shm: shared memory holding the message to pass to secure world * @offs: offset of the message in @shm * @system_thread: true if caller requests TEE system thread support * * Does and SMC to OP-TEE in secure world and handles eventual resulting * Remote Procedure Calls (RPC) from OP-TEE. * * Returns return code from secure world, 0 is OK
*/ staticint optee_smc_do_call_with_arg(struct tee_context *ctx, struct tee_shm *shm, u_int offs, bool system_thread)
{ struct optee *optee = tee_get_drvdata(ctx->teedev); struct optee_call_waiter w; struct optee_rpc_param param = { }; struct optee_call_ctx call_ctx = { }; struct optee_msg_arg *rpc_arg = NULL; int rc;
if (optee->rpc_param_count) { struct optee_msg_arg *arg; unsignedint rpc_arg_offs;
arg = tee_shm_get_va(shm, offs); if (IS_ERR(arg)) return PTR_ERR(arg);
if (res.a0 == OPTEE_SMC_RETURN_ETHREAD_LIMIT) { /* * Out of threads in secure world, wait for a thread * become available.
*/
optee_cq_wait_for_completion(&optee->call_queue, &w);
} elseif (OPTEE_SMC_RETURN_IS_RPC(res.a0)) {
cond_resched();
param.a0 = res.a0;
param.a1 = res.a1;
param.a2 = res.a2;
param.a3 = res.a3;
optee_handle_rpc(ctx, rpc_arg, ¶m, &call_ctx);
} else {
rc = res.a0; break;
}
}
optee_rpc_finalize_call(&call_ctx); /* * We're done with our thread in secure world, if there's any * thread waiters wake up one.
*/
optee_cq_wait_final(&optee->call_queue, &w);
staticvoid optee_smc_notif_uninit_irq(struct optee *optee)
{ if (optee->smc.sec_caps & OPTEE_SMC_SEC_CAP_ASYNC_NOTIF) {
optee_stop_async_notif(optee->ctx); if (optee->smc.notif_irq) { if (irq_is_percpu_devid(optee->smc.notif_irq))
uninit_pcpu_irq(optee); else
free_irq(optee->smc.notif_irq, optee);
irq_dispose_mapping(optee->smc.notif_irq);
}
}
}
/* * 6. Driver initialization * * During driver initialization is secure world probed to find out which * features it supports so the driver can be initialized with a matching * configuration. This involves for instance support for dynamic shared * memory instead of a static memory carvout.
*/
/* * TODO This isn't enough to tell if it's UP system (from kernel * point of view) or not, is_smp() returns the information * needed, but can't be called directly from here.
*/ if (!IS_ENABLED(CONFIG_SMP) || nr_cpu_ids == 1)
a1 |= OPTEE_SMC_NSEC_CAP_UNIPROCESSOR;
/* optee_remove - Device Removal Routine * @pdev: platform device information struct * * optee_remove is called by platform subsystem to alert the driver * that it should release the device
*/ staticvoid optee_smc_remove(struct platform_device *pdev)
{ struct optee *optee = platform_get_drvdata(pdev);
/* * Ask OP-TEE to free all cached shared memory objects to decrease * reference counters and also avoid wild pointers in secure world * into the old shared memory range.
*/ if (!optee->rpc_param_count)
optee_disable_shm_cache(optee);
optee_smc_notif_uninit_irq(optee);
optee_remove_common(optee);
if (optee->smc.memremaped_shm)
memunmap(optee->smc.memremaped_shm);
kfree(optee);
}
/* optee_shutdown - Device Removal Routine * @pdev: platform device information struct * * platform_shutdown is called by the platform subsystem to alert * the driver that a shutdown, reboot, or kexec is happening and * device must be disabled.
*/ staticvoid optee_shutdown(struct platform_device *pdev)
{ struct optee *optee = platform_get_drvdata(pdev);
if (!optee->rpc_param_count)
optee_disable_shm_cache(optee);
}
#ifdef CONFIG_OPTEE_INSECURE_LOAD_IMAGE
#define OPTEE_FW_IMAGE "optee/tee.bin"
static optee_invoke_fn *cpuhp_invoke_fn;
staticint optee_cpuhp_probe(unsignedint cpu)
{ /* * Invoking a call on a CPU will cause OP-TEE to perform the required * setup for that CPU. Just invoke the call to get the UID since that * has no side effects.
*/ if (optee_msg_api_uid_is_optee_api(cpuhp_invoke_fn)) return 0; else return -EINVAL;
}
if (!optee_msg_api_uid_is_optee_image_load(invoke_fn)) return 0;
rc = request_firmware(&fw, OPTEE_FW_IMAGE, &pdev->dev); if (rc) { /* * The firmware in the rootfs will not be accessible until we * are in the SYSTEM_RUNNING state, so return EPROBE_DEFER until * that point.
*/ if (system_state < SYSTEM_RUNNING) return -EPROBE_DEFER; goto fw_err;
}
data_size = fw->size; /* * This uses the GFP_DMA flag to ensure we are allocated memory in the * 32-bit space since TF-A cannot map memory beyond the 32-bit boundary.
*/
data_buf = kmemdup(fw->data, fw->size, GFP_KERNEL | GFP_DMA); if (!data_buf) {
rc = -ENOMEM; goto fw_err;
}
data_pa = virt_to_phys(data_buf);
reg_pair_from_64(&data_pa_high, &data_pa_low, data_pa);
reg_pair_from_64(&data_size_high, &data_size_low, data_size); goto fw_load;
fw_load: /* * Always invoke the SMC, even if loading the image fails, to indicate * to EL3 that we have passed the point where it should allow invoking * this SMC.
*/
pr_warn("OP-TEE image loaded from kernel, this can be insecure");
invoke_fn(OPTEE_SMC_CALL_LOAD_IMAGE, data_size_high, data_size_low,
data_pa_high, data_pa_low, 0, 0, 0, &res); if (!rc)
rc = res.a0;
release_firmware(fw);
kfree(data_buf);
if (!rc) { /* * We need to initialize OP-TEE on all other running cores as * well. Any cores that aren't running yet will get initialized * when they are brought up by the power management functions in * TF-A which are registered by the OP-TEE SPD. Due to that we * can un-register the callback right after registering it.
*/
cpuhp_invoke_fn = invoke_fn;
hp_state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "optee:probe",
optee_cpuhp_probe, NULL); if (hp_state < 0) {
pr_warn("Failed with CPU hotplug setup for OP-TEE"); return -EINVAL;
}
cpuhp_remove_state(hp_state);
cpuhp_invoke_fn = NULL;
}
/* * Try to use dynamic shared memory if possible
*/ if (sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM) { /* * If we have OPTEE_SMC_SEC_CAP_RPC_ARG we can ask * optee_get_msg_arg() to pre-register (by having * OPTEE_SHM_ARG_ALLOC_PRIV cleared) the page used to pass * an argument struct. * * With the page is pre-registered we can use a non-zero * offset for argument struct, this is indicated with * OPTEE_SHM_ARG_SHARED. * * This means that optee_smc_do_call_with_arg() will use * OPTEE_SMC_CALL_WITH_REGD_ARG for pre-registered pages.
*/ if (sec_caps & OPTEE_SMC_SEC_CAP_RPC_ARG)
arg_cache_flags = OPTEE_SHM_ARG_SHARED; else
arg_cache_flags = OPTEE_SHM_ARG_ALLOC_PRIV;
pool = optee_shm_pool_alloc_pages();
}
/* * If dynamic shared memory is not available or failed - try static one
*/ if (IS_ERR(pool) && (sec_caps & OPTEE_SMC_SEC_CAP_HAVE_RESERVED_SHM)) { /* * The static memory pool can use non-zero page offsets so * let optee_get_msg_arg() know that with OPTEE_SHM_ARG_SHARED. * * optee_get_msg_arg() should not pre-register the * allocated page used to pass an argument struct, this is * indicated with OPTEE_SHM_ARG_ALLOC_PRIV. * * This means that optee_smc_do_call_with_arg() will use * OPTEE_SMC_CALL_WITH_ARG if rpc_param_count is 0, else * OPTEE_SMC_CALL_WITH_RPC_ARG.
*/
arg_cache_flags = OPTEE_SHM_ARG_SHARED |
OPTEE_SHM_ARG_ALLOC_PRIV;
pool = optee_config_shm_memremap(invoke_fn, &memremaped_shm);
}
/* * Ensure that there are no pre-existing shm objects before enabling * the shm cache so that there's no chance of receiving an invalid * address during shutdown. This could occur, for example, if we're * kexec booting from an older kernel that did not properly cleanup the * shm cache.
*/
optee_disable_unmapped_shm_cache(optee);
/* * Only enable the shm cache in case we're not able to pass the RPC * arg struct right after the normal arg struct.
*/ if (!optee->rpc_param_count)
optee_enable_shm_cache(optee);
if (optee->smc.sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM)
pr_info("dynamic shared memory is enabled\n");
rc = optee_enumerate_devices(PTA_CMD_GET_DEVICES); if (rc) goto err_disable_shm_cache;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.