diff options
author | James Zhao <jamesz@nvidia.com> | 2013-06-07 15:51:20 -0700 |
---|---|---|
committer | Dan Willemsen <dwillemsen@nvidia.com> | 2013-09-14 13:34:23 -0700 |
commit | 09745b57d5f9b4cc57a9bc6ca0e428ec6e918446 (patch) | |
tree | 4a578eee4d62bb9947fa3e938e881cc969fe4ef2 /security/tlk_driver | |
parent | dac6c1c7844bdfbf901a1af37c6de467130dbfca (diff) |
tlk: New API changes for tlk
- add new parameter passing support for variable number of parameter
- some clean up of naming conventions
Bug 1310292
Change-Id: Ie9669456682fe2b85eb79a3d9cb4cbac9eba8d54
Signed-off-by: James Zhao <jamesz@nvidia.com>
Reviewed-on: http://git-master/r/239104
(cherry picked from commit da4ee985be76b4f02284510d2eb7e851fb50bc0b)
Reviewed-on: http://git-master/r/249870
Reviewed-by: Varun Wadekar <vwadekar@nvidia.com>
Tested-by: Varun Wadekar <vwadekar@nvidia.com>
Tested-by: Aaron Gamble <jgamble@nvidia.com>
Diffstat (limited to 'security/tlk_driver')
-rw-r--r-- | security/tlk_driver/Kconfig | 7 | ||||
-rw-r--r-- | security/tlk_driver/Makefile | 29 | ||||
-rw-r--r-- | security/tlk_driver/ote_comms.c | 372 | ||||
-rw-r--r-- | security/tlk_driver/ote_device.c | 456 | ||||
-rw-r--r-- | security/tlk_driver/ote_fs.c | 242 | ||||
-rw-r--r-- | security/tlk_driver/ote_irq.S | 21 | ||||
-rw-r--r-- | security/tlk_driver/ote_protocol.h | 202 | ||||
-rw-r--r-- | security/tlk_driver/ote_types.h | 79 |
8 files changed, 1408 insertions, 0 deletions
diff --git a/security/tlk_driver/Kconfig b/security/tlk_driver/Kconfig new file mode 100644 index 000000000000..240a3180d7a0 --- /dev/null +++ b/security/tlk_driver/Kconfig @@ -0,0 +1,7 @@ +config TRUSTED_LITTLE_KERNEL + bool "Enable Open Trusted Execution driver" + select TEGRA_USE_SECURE_KERNEL + help + This option adds kernel support for communication with the + Trusted LK secure OS monitor/runtime support. + If you are unsure how to answer this question, answer N. diff --git a/security/tlk_driver/Makefile b/security/tlk_driver/Makefile new file mode 100644 index 000000000000..2fb6a24acc24 --- /dev/null +++ b/security/tlk_driver/Makefile @@ -0,0 +1,29 @@ +# +# Copyright (c) 2013 NVIDIA Corporation. All rights reserved. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# + +plus_sec := $(call as-instr,.arch_extension sec,+sec) +AFLAGS_ote_irq.o :=-Wa,-march=armv7-a$(plus_sec) +CFLAGS_ote_comms.o :=$(call as-instr,.arch_extension sec,-DREQUIRES_SEC=1) +CFLAGS_ote_fs.o :=$(call as-instr,.arch_extension sec,-DREQUIRES_SEC=1) + +tlk_driver-objs += ote_device.o +tlk_driver-objs += ote_comms.o +tlk_driver-objs += ote_fs.o +tlk_driver-objs += ote_irq.o + +obj-$(CONFIG_TRUSTED_LITTLE_KERNEL) += tlk_driver.o diff --git a/security/tlk_driver/ote_comms.c b/security/tlk_driver/ote_comms.c new file mode 100644 index 000000000000..fc9744ad3766 --- /dev/null +++ b/security/tlk_driver/ote_comms.c @@ -0,0 +1,372 @@ +/* + * Copyright (c) 2013 NVIDIA Corporation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include <linux/atomic.h> +#include <linux/uaccess.h> +#include <linux/module.h> +#include <linux/slab.h> +#include <linux/fs.h> +#include <linux/printk.h> +#include <linux/ioctl.h> +#include <linux/sched.h> +#include <linux/mm.h> +#include <linux/pagemap.h> + +#include "ote_protocol.h" + +bool verbose_smc; +core_param(verbose_smc, verbose_smc, bool, 0644); + +#define SET_RESULT(req, r, ro) { req->result = r; req->result_origin = ro; } + +static int te_pin_user_pages(void *buffer, size_t size, + unsigned long *pages_ptr) +{ + int ret = 0; + unsigned int nr_pages; + struct page **pages = NULL; + + nr_pages = (((unsigned int)buffer & (PAGE_SIZE - 1)) + + (size + PAGE_SIZE - 1)) >> PAGE_SHIFT; + + pages = kzalloc(nr_pages * sizeof(struct page *), GFP_KERNEL); + if (!pages) + return -ENOMEM; + + down_read(¤t->mm->mmap_sem); + ret = get_user_pages(current, current->mm, (unsigned long)buffer, + nr_pages, WRITE, 0, pages, NULL); + up_read(¤t->mm->mmap_sem); + + *pages_ptr = (unsigned long) pages; + + return ret; +} + +static struct te_shmem_desc *te_add_shmem_desc(void *buffer, size_t size, + unsigned int nr_pages, struct page **pages, + struct tlk_context *context) +{ + struct te_shmem_desc *shmem_desc = NULL; + shmem_desc = kzalloc(sizeof(struct te_shmem_desc), GFP_KERNEL); + if (shmem_desc) { + INIT_LIST_HEAD(&(shmem_desc->list)); + shmem_desc->buffer = buffer; + shmem_desc->size = size; + shmem_desc->nr_pages = nr_pages; + shmem_desc->pages = pages; + list_add_tail(&shmem_desc->list, &(context->shmem_alloc_list)); + } + + return shmem_desc; +} + +static int te_pin_mem_buffers(void *buffer, size_t size, + struct tlk_context *context) +{ + + unsigned long pages = 0; + struct te_shmem_desc *shmem_desc = NULL; + int ret = 0, nr_pages = 0; + + nr_pages = te_pin_user_pages(buffer, size, &pages); + if (nr_pages <= 0) { + pr_err("%s: te_pin_user_pages Failed\n", __func__); + ret = OTE_ERROR_OUT_OF_MEMORY; + goto error; + } + + shmem_desc = te_add_shmem_desc(buffer, size, + nr_pages, (struct page **)pages, context); + if (!shmem_desc) { + pr_err("%s: te_add_shmem_desc Failed\n", __func__); + ret = OTE_ERROR_OUT_OF_MEMORY; + goto error; + } + + return OTE_SUCCESS; +error: + return ret; +} + +static int te_setup_temp_buffers(struct te_request *request, + struct tlk_context *context) +{ + uint32_t i; + int ret = OTE_SUCCESS; + struct te_oper_param *params = request->params; + + for (i = 0; i < request->params_size; i++) { + switch (params[i].type) { + case TE_PARAM_TYPE_NONE: + case TE_PARAM_TYPE_INT_RO: + case TE_PARAM_TYPE_INT_RW: + break; + case TE_PARAM_TYPE_MEM_RO: + case TE_PARAM_TYPE_MEM_RW: + ret = te_pin_mem_buffers( + params[i].u.Mem.base, + params[i].u.Mem.len, + context); + if (ret < 0) { + pr_err("%s failed with err (%d)\n", + __func__, ret); + ret = OTE_ERROR_BAD_PARAMETERS; + break; + } + break; + default: + pr_err("%s: OTE_ERROR_BAD_PARAMETERS\n", __func__); + ret = OTE_ERROR_BAD_PARAMETERS; + break; + } + } + return ret; +} + +static void te_del_shmem_desc(void *buffer, struct tlk_context *context) +{ + struct te_shmem_desc *shmem_desc, *tmp_shmem_desc; + int i; + + list_for_each_entry_safe(shmem_desc, tmp_shmem_desc, + &(context->shmem_alloc_list), list) { + if (shmem_desc->buffer == buffer) { + list_del(&shmem_desc->list); + for (i = 0; i < shmem_desc->nr_pages; i++) + page_cache_release(shmem_desc->pages[i]); + kfree(shmem_desc->pages); + kfree(shmem_desc); + } + } +} + +/* + * Deregister previously initialized shared memory + */ +void te_unregister_memory(void *buffer, + struct tlk_context *context) +{ + if (!(list_empty(&(context->shmem_alloc_list)))) + te_del_shmem_desc(buffer, context); + else + pr_err("No buffers to unpin\n"); +} + +static void te_unpin_temp_buffers(struct te_request *request, + struct tlk_context *context) +{ + uint32_t i; + struct te_oper_param *params = request->params; + + for (i = 0; i < request->params_size; i++) { + switch (params[i].type) { + case TE_PARAM_TYPE_NONE: + case TE_PARAM_TYPE_INT_RO: + case TE_PARAM_TYPE_INT_RW: + break; + case TE_PARAM_TYPE_MEM_RO: + case TE_PARAM_TYPE_MEM_RW: + te_unregister_memory(params[i].u.Mem.base, context); + break; + default: + pr_err("%s: OTE_ERROR_BAD_PARAMETERS\n", __func__); + break; + } + } +} + +uint32_t __naked tlk_generic_smc(uint32_t arg0, uint32_t arg1, uint32_t arg2) +{ + register uint32_t r0 asm("r0") = arg0; + register uint32_t r1 asm("r1") = arg1; + register uint32_t r2 asm("r2") = arg2; + + asm volatile( + __asmeq("%0", "r0") + __asmeq("%1", "r0") + __asmeq("%2", "r1") + __asmeq("%3", "r2") + "stmfd sp!, {r4-r12} @ save reg state\n" +#ifdef REQUIRES_SEC + ".arch_extension sec\n" +#endif + "smc #0 @ switch to secure world\n" + "ldmfd sp!, {r4-r12} @ restore saved regs\n" + "bx lr" + : "=r" (r0) + : "r" (r0), "r" (r1), "r" (r2) + ); + return r0; +} + +uint32_t __naked tlk_extended_smc(uint32_t *regs) +{ + register uint32_t r0 asm("r0") = (uint32_t)regs; + + /* allows MAX_EXT_SMC_ARGS (r0-r11) to be passed in registers */ + asm volatile( + __asmeq("%0", "r0") + "stmfd sp!, {r4-r12} @ save reg state\n" + "mov r12, r0 @ reg ptr to r12\n" + "ldmia r12, {r0-r11} @ load arg regs\n" +#ifdef REQUIRES_SEC + ".arch_extension sec\n" +#endif + "smc #0 @ switch to secure world\n" + "ldmfd sp!, {r4-r12} @ restore saved regs\n" + "bx lr" + : "=r" (r0) + : "r" (r0) + ); + return r0; +} + +/* + * Do an SMC call + */ +static void do_smc(struct te_request *request) +{ +#ifdef CONFIG_SMP + cpumask_t saved_cpu_mask; +#endif + phys_addr_t smc_args = virt_to_phys(request); + phys_addr_t smc_params = 0; + + if (request->params) + smc_params = virt_to_phys(request->params); + +#ifdef CONFIG_SMP +{ + long ret; + cpumask_t local_cpu_mask = CPU_MASK_NONE; + + cpu_set(0, local_cpu_mask); + cpumask_copy(&saved_cpu_mask, tsk_cpus_allowed(current)); + ret = sched_setaffinity(0, &local_cpu_mask); + if (ret) + pr_err("sched_setaffinity #1 -> 0x%lX", ret); +} +#endif + + tlk_generic_smc(request->type, smc_args, smc_params); + +#ifdef CONFIG_SMP +{ + long ret = sched_setaffinity(0, &saved_cpu_mask); + if (ret) + pr_err("sched_setaffinity #2 -> 0x%lX", ret); +} +#endif +} + +/* + * Open session SMC (supporting client-based te_open_session() calls) + */ +void te_open_session(struct te_opensession *cmd, + struct te_request *request, + struct tlk_context *context) +{ + int ret; + + ret = te_setup_temp_buffers(request, context); + if (ret != OTE_SUCCESS) { + pr_err("te_setup_temp_buffers failed err (0x%x)\n", ret); + SET_RESULT(request, ret, OTE_ERROR_ORIGIN_API); + return; + } + + memcpy(&request->dest_uuid, + &cmd->dest_uuid, + sizeof(struct te_service_id)); + + pr_info("OPEN_CLIENT_SESSION: 0x%x 0x%x 0x%x 0x%x\n", + request->dest_uuid[0], + request->dest_uuid[1], + request->dest_uuid[2], + request->dest_uuid[3]); + + request->type = TE_SMC_OPEN_SESSION; + + do_smc(request); + + te_unpin_temp_buffers(request, context); +} + +/* + * Close session SMC (supporting client-based te_close_session() calls) + */ +void te_close_session(struct te_closesession *cmd, + struct te_request *request) +{ + request->session_id = cmd->session_id; + request->type = TE_SMC_CLOSE_SESSION; + + do_smc(request); + if (request->result) + pr_info("Error closing session: %08x\n", request->result); +} + +/* + * Launch operation SMC (supporting client-based te_launch_operation() calls) + */ +void te_launch_operation(struct te_launchop *cmd, + struct te_request *request, + struct tlk_context *context) +{ + int ret; + + ret = te_setup_temp_buffers(request, context); + if (ret != OTE_SUCCESS) { + pr_err("te_setup_temp_buffers failed err (0x%x)\n", ret); + SET_RESULT(request, ret, OTE_ERROR_ORIGIN_API); + return; + } + + request->session_id = cmd->session_id; + request->command_id = cmd->operation.command; + request->type = TE_SMC_LAUNCH_OPERATION; + + do_smc(request); + + te_unpin_temp_buffers(request, context); +} + +static int __init tlk_register_irq_handler(void) +{ + tlk_generic_smc(0xFFFF1FF0, (unsigned int)tlk_irq_handler, 0); + +#if 0 + asm volatile ( + "mov r1, %0\n" + "movw r0, #0x1FF0\n" + "movt r0, #0xFFFF\n" +#ifdef REQUIRES_SEC + ".arch_extension sec\n" +#endif + "smc #0\n" + "cpsie i\n" + : : "r" (tlk_irq_handler) + : "r0", "r1", "r13", "r14" + ); +#endif + + return 0; +} + +arch_initcall(tlk_register_irq_handler); diff --git a/security/tlk_driver/ote_device.c b/security/tlk_driver/ote_device.c new file mode 100644 index 000000000000..44d8a09971d0 --- /dev/null +++ b/security/tlk_driver/ote_device.c @@ -0,0 +1,456 @@ +/* + * Copyright (c) 2013 NVIDIA Corporation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include <linux/atomic.h> +#include <linux/uaccess.h> +#include <linux/module.h> +#include <linux/slab.h> +#include <linux/fs.h> +#include <linux/printk.h> +#include <linux/ioctl.h> +#include <linux/miscdevice.h> +#include <linux/mm.h> +#include <asm/cacheflush.h> +#include <asm/outercache.h> +#include <linux/list.h> + +#include "ote_protocol.h" + +#define SET_ANSWER(a, r, ro) { a.result = r; a.return_origin = ro; } + +#define CREATE_TRACE_POINTS +#include <trace/events/nvsecurity.h> + +struct tlk_device tlk_dev; + +u32 notrace tegra_read_cycle(void) +{ + u32 cycle_count; + + asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r"(cycle_count)); + + return cycle_count; +} + +/* + * The maximum number of outstanding command requests. + */ +#define TE_CMD_DESC_MAX (PAGE_SIZE / sizeof(struct te_request)) +#define TE_PARAM_MAX (PAGE_SIZE / sizeof(struct te_oper_param)) + +static int te_create_free_cmd_list(struct tlk_device *dev) +{ + struct page *te_cmd_pages; + int cmd_desc_count = 0, ret = 0; + struct te_cmd_req_desc *req_desc; + int bitmap_size; + + te_cmd_pages = alloc_pages(GFP_KERNEL, get_count_order(2)); + if (!te_cmd_pages) { + ret = -ENOMEM; + goto error; + } + + /* first 4KB page is request freelist, second is param freelist */ + dev->req_addr = (unsigned long) page_address(te_cmd_pages); + dev->param_addr = (struct te_oper_param *)(dev->req_addr + PAGE_SIZE); + set_memory_uc(dev->req_addr, 2); + + /* alloc param bitmap allocator */ + bitmap_size = BITS_TO_LONGS(TE_PARAM_MAX) * sizeof(long); + dev->param_bitmap = kzalloc(bitmap_size, GFP_KERNEL); + + for (cmd_desc_count = 0; + cmd_desc_count < TE_CMD_DESC_MAX; cmd_desc_count++) { + + req_desc = kzalloc(sizeof(struct te_cmd_req_desc), GFP_KERNEL); + if (req_desc == NULL) { + pr_err("Failed to allocate cmd req descriptor\n"); + ret = -ENOMEM; + goto error; + } + req_desc->req_addr = dev->req_addr + + sizeof(struct te_request) * cmd_desc_count; + INIT_LIST_HEAD(&(req_desc->list)); + + /* Add the cmd param descriptor to free list */ + list_add_tail(&req_desc->list, &(dev->free_cmd_list)); + } +error: + return ret; + +} + +static struct te_oper_param *te_get_free_params(struct tlk_device *dev, + unsigned int nparams) +{ + struct te_oper_param *params = NULL; + int idx, nbits; + + if (nparams) { + nbits = get_count_order(nparams); + idx = bitmap_find_free_region(dev->param_bitmap, + TE_PARAM_MAX, nbits); + if (idx >= 0) + params = dev->param_addr + idx; + } + return params; +} + +static void te_put_free_params(struct tlk_device *dev, + struct te_oper_param *params, uint32_t nparams) +{ + int idx, nbits; + + idx = (params - dev->param_addr); + nbits = get_count_order(nparams); + bitmap_release_region(dev->param_bitmap, idx, nbits); +} + +static struct te_cmd_req_desc *te_get_free_cmd_desc(struct tlk_device *dev) +{ + struct te_cmd_req_desc *cmd_desc = NULL; + + if (!(list_empty(&(dev->free_cmd_list)))) { + cmd_desc = list_first_entry(&(dev->free_cmd_list), + struct te_cmd_req_desc, list); + list_del(&(cmd_desc->list)); + list_add_tail(&cmd_desc->list, &(dev->used_cmd_list)); + } + return cmd_desc; +} + +static void te_put_used_cmd_desc(struct tlk_device *dev, + struct te_cmd_req_desc *cmd_desc) +{ + struct te_cmd_req_desc *param_desc, *tmp_param_desc; + + if (cmd_desc) { + list_for_each_entry_safe(param_desc, tmp_param_desc, + &(dev->used_cmd_list), list) { + if (cmd_desc->req_addr == param_desc->req_addr) { + list_del(¶m_desc->list); + list_add_tail(¶m_desc->list, + &(dev->free_cmd_list)); + } + } + } +} + +static void __attribute__((unused)) te_print_cmd_list( + struct tlk_device *dev, int used_list) +{ + struct te_cmd_req_desc *param_desc; + + if (!used_list) { + pr_info("Printing free cmd list\n"); + if (!(list_empty(&(dev->free_cmd_list)))) { + list_for_each_entry(param_desc, &(dev->free_cmd_list), + list) + pr_info("Phys addr for cmd req desc (%lx)\n", + param_desc->req_addr); + } + } else { + pr_info("Printing used cmd list\n"); + if (!(list_empty(&(dev->used_cmd_list)))) { + list_for_each_entry(param_desc, &(dev->used_cmd_list), + list) + pr_info("Phys addr for cmd req desc (%lx)\n", + param_desc->req_addr); + } + } +} + +static int tlk_device_open(struct inode *inode, struct file *file) +{ + struct tlk_context *context; + int ret = 0; + + context = kzalloc(sizeof(struct tlk_context), GFP_KERNEL); + if (!context) { + ret = -ENOMEM; + goto error; + } + context->dev = &tlk_dev; + INIT_LIST_HEAD(&(context->shmem_alloc_list)); + + file->private_data = context; + return 0; +error: + return ret; +} + +static int tlk_device_release(struct inode *inode, struct file *file) +{ + kfree(file->private_data); + file->private_data = NULL; + return 0; +} + +static int copy_params_from_user(struct te_request *req, + struct te_operation *operation) +{ + struct te_oper_param *param_array; + struct te_oper_param *user_param; + uint32_t i; + + if (operation->list_count == 0) + return 0; + + param_array = req->params; + if (param_array == NULL) { + pr_err("param_array empty\n"); + return 1; + } + + user_param = operation->list_head; + for (i = 0; i < operation->list_count && user_param != NULL; i++) { + if (copy_from_user(param_array + i, user_param, + sizeof(struct te_oper_param))) { + pr_err("Failed to copy operation parameter:%d, %p, " \ + "list_count: %d\n", + i, user_param, operation->list_count); + return 1; + } + user_param = param_array[i].next_ptr_user; + } + return 0; +} + +static int copy_params_to_user(struct te_request *req, + struct te_operation *operation) +{ + struct te_oper_param *param_array; + struct te_oper_param *user_param; + uint32_t i; + + if (operation->list_count == 0) + return 0; + + param_array = req->params; + if (param_array == NULL) { + pr_err("param_array empty\n"); + return 1; + } + + user_param = operation->list_head; + for (i = 0; i < req->params_size; i++) { + if (copy_to_user(user_param, param_array + i, + sizeof(struct te_oper_param))) { + pr_err("Failed to copy back parameter:%d %p\n", i, + user_param); + return 1; + } + user_param = param_array[i].next_ptr_user; + } + return 0; +} + +static long te_handle_trustedapp_ioctl(struct file *file, + unsigned int ioctl_num, unsigned long ioctl_param) +{ + long err = 0; + union te_cmd cmd; + void *ptr_user_answer = NULL; + struct te_operation *operation = NULL; + struct te_oper_param *params = NULL; + struct te_answer answer; + struct te_request *request; + + struct te_cmd_req_desc *cmd_desc = NULL; + struct tlk_context *context = file->private_data; + struct tlk_device *dev = context->dev; + + if (copy_from_user(&cmd, (void __user *)ioctl_param, + sizeof(union te_cmd))) { + pr_err("Failed to copy command request\n"); + err = -EFAULT; + goto error; + } + + memset(&answer, 0, sizeof(struct te_answer)); + + switch (ioctl_num) { + case TE_IOCTL_OPEN_CLIENT_SESSION: + operation = &cmd.opensession.operation; + ptr_user_answer = (void *)cmd.opensession.answer; + + cmd_desc = te_get_free_cmd_desc(dev); + params = te_get_free_params(dev, operation->list_count); + + if (!cmd_desc || (operation->list_count && !params)) { + SET_ANSWER(answer, + OTE_ERROR_OUT_OF_MEMORY, + OTE_ERROR_ORIGIN_COMMS); + pr_err("failed to get cmd_desc/params\n"); + goto error; + } + + request = (struct te_request *)cmd_desc->req_addr; + memset(request, 0, sizeof(struct te_request)); + + request->params = params; + request->params_size = operation->list_count; + + if (copy_params_from_user(request, operation)) { + err = -EFAULT; + pr_info("failed to copy params from user\n"); + goto error; + } + + te_open_session(&cmd.opensession, request, context); + + SET_ANSWER(answer, request->result, request->result_origin); + answer.session_id = request->session_id; + break; + + case TE_IOCTL_CLOSE_CLIENT_SESSION: + ptr_user_answer = (void *)cmd.closesession.answer; + cmd_desc = te_get_free_cmd_desc(dev); + if (!cmd_desc) { + SET_ANSWER(answer, + OTE_ERROR_OUT_OF_MEMORY, + OTE_ERROR_ORIGIN_COMMS); + pr_err("failed to get cmd_desc\n"); + goto error; + } + + request = (struct te_request *)cmd_desc->req_addr; + memset(request, 0, sizeof(struct te_request)); + + /* close session cannot fail */ + te_close_session(&cmd.closesession, request); + break; + + case TE_IOCTL_LAUNCH_OPERATION: + operation = &cmd.launchop.operation; + ptr_user_answer = (void *)cmd.launchop.answer; + + cmd_desc = te_get_free_cmd_desc(dev); + params = te_get_free_params(dev, operation->list_count); + + if (!cmd_desc || (operation->list_count && !params)) { + SET_ANSWER(answer, + OTE_ERROR_OUT_OF_MEMORY, + OTE_ERROR_ORIGIN_COMMS); + pr_err("failed to get cmd_desc/params\n"); + goto error; + } + + request = (struct te_request *)cmd_desc->req_addr; + memset(request, 0, sizeof(struct te_request)); + + request->params = params; + request->params_size = operation->list_count; + + if (copy_params_from_user(request, operation)) { + err = -EFAULT; + pr_info("failed to copy params from user\n"); + goto error; + } + + te_launch_operation(&cmd.launchop, request, context); + + SET_ANSWER(answer, request->result, request->result_origin); + break; + + default: + pr_err("Invalid IOCTL Cmd\n"); + err = -EINVAL; + goto error; + } + if (ptr_user_answer && !err) { + if (copy_to_user(ptr_user_answer, &answer, + sizeof(struct te_answer))) { + pr_err("Failed to copy answer\n"); + err = -EFAULT; + } + } + if (request->params && !err) { + if (copy_params_to_user(request, operation)) { + pr_err("Failed to copy return params\n"); + err = -EFAULT; + } + } + +error: + if (cmd_desc) + te_put_used_cmd_desc(dev, cmd_desc); + if (params) + te_put_free_params(dev, params, operation->list_count); + return err; +} + +static long tlk_device_ioctl(struct file *file, unsigned int ioctl_num, + unsigned long ioctl_param) +{ + int err; + + switch (ioctl_num) { + case TE_IOCTL_OPEN_CLIENT_SESSION: + case TE_IOCTL_CLOSE_CLIENT_SESSION: + case TE_IOCTL_LAUNCH_OPERATION: + err = te_handle_trustedapp_ioctl(file, ioctl_num, ioctl_param); + break; + + case TE_IOCTL_FILE_NEW_REQ: + case TE_IOCTL_FILE_FILL_BUF: + case TE_IOCTL_FILE_REQ_COMPLETE: + err = te_handle_fs_ioctl(file, ioctl_num, ioctl_param); + break; + + default: + pr_err("%s: Invalid IOCTL (0x%x) id 0x%x max 0x%x\n", __func__, + ioctl_num, _IOC_NR(ioctl_num), TE_IOCTL_MAX_NR); + err = -EINVAL; + } + + return err; +} + +/* + * tlk_driver function definitions. + */ +static const struct file_operations tlk_device_fops = { + .owner = THIS_MODULE, + .open = tlk_device_open, + .release = tlk_device_release, + .unlocked_ioctl = tlk_device_ioctl, +}; + +struct miscdevice tlk_misc_device = { + .minor = MISC_DYNAMIC_MINOR, + .name = "tlk_device", + .fops = &tlk_device_fops, +}; + +static int __init tlk_init(void) +{ + int ret; + + INIT_LIST_HEAD(&(tlk_dev.used_cmd_list)); + INIT_LIST_HEAD(&(tlk_dev.free_cmd_list)); + + ret = te_create_free_cmd_list(&tlk_dev); + if (ret != 0) + return ret; + + return misc_register(&tlk_misc_device); +} + +module_init(tlk_init); diff --git a/security/tlk_driver/ote_fs.c b/security/tlk_driver/ote_fs.c new file mode 100644 index 000000000000..716d004f68bb --- /dev/null +++ b/security/tlk_driver/ote_fs.c @@ -0,0 +1,242 @@ +/* + * Copyright (c) 2013 NVIDIA Corporation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include <linux/slab.h> +#include <linux/syscalls.h> +#include <linux/list.h> +#include <linux/completion.h> +#include <linux/workqueue.h> +#include <linux/freezer.h> +#include <linux/bitops.h> +#include <linux/uaccess.h> + +#include "ote_protocol.h" + +#define TE_SHMEM_FNAME_SZ SZ_64 +#define TE_SHMEM_DATA_SZ SZ_128K +#define TE_FS_READY_BIT 1 + +struct te_file_req_shmem { + char file_name[TE_SHMEM_FNAME_SZ]; + char file_data[TE_SHMEM_DATA_SZ]; +}; + +struct te_file_req_node { + struct list_head node; + struct te_file_req *req; +}; + +static struct list_head req_list; +static DECLARE_COMPLETION(req_ready); +static DECLARE_COMPLETION(req_complete); +static unsigned long secure_error; +static unsigned long fs_ready; + +static void indicate_complete(unsigned long ret) +{ + tlk_generic_smc(0xFFFF1FFF, ret, 0); +} + +int te_handle_fs_ioctl(struct file *file, unsigned int ioctl_num, + unsigned long ioctl_param) +{ + struct te_file_req new_req, *ptr_user_req = NULL; + struct te_file_req_node *req_node; + + switch (ioctl_num) { + case TE_IOCTL_FILE_NEW_REQ: /* new request */ + + ptr_user_req = (struct te_file_req *)ioctl_param; + + set_freezable(); + + set_bit(TE_FS_READY_BIT, &fs_ready); + + /* wait for a new request */ + while (wait_for_completion_interruptible(&req_ready)) + try_to_freeze(); + + /* dequeue new request from the secure world */ + req_node = list_first_entry(&req_list, struct te_file_req_node, + node); + + /* populate request for the non-secure client */ + if (req_node) { + if (copy_to_user(ptr_user_req, req_node->req, + sizeof(struct te_file_req))) { + pr_err("copy_to_user failed for new request\n"); + return -EFAULT; + } + + list_del(&req_node->node); + kfree(req_node); + } else { + pr_err("no request available\n"); + return -ENOMEM; + } + + break; + + case TE_IOCTL_FILE_FILL_BUF: /* pass data to be written to the file */ + + if (copy_from_user(&new_req, (void __user *)ioctl_param, + sizeof(struct te_file_req))) { + pr_err("copy_from_user failed for request\n"); + return -EFAULT; + } + + if (new_req.type != OTE_FILE_REQ_WRITE) + return -EINVAL; + + if (!new_req.kern_data_buf || !new_req.user_data_buf) + return -EINVAL; + + if (copy_to_user(new_req.user_data_buf, new_req.kern_data_buf, + new_req.data_len)) { + pr_err("copy_to_user failed for fill buffer\n"); + return -EFAULT; + } + break; + + case TE_IOCTL_FILE_REQ_COMPLETE: /* request complete */ + + if (copy_from_user(&new_req, (void __user *)ioctl_param, + sizeof(struct te_file_req))) { + pr_err("copy_from_user failed for request\n"); + return -EFAULT; + } + + if (new_req.type == OTE_FILE_REQ_READ && !new_req.error) { + if (copy_from_user(new_req.kern_data_buf, + (void __user *)new_req.user_data_buf, + new_req.data_len)) { + pr_err("copy_from_user failed for request\n"); + return -EFAULT; + } + } + + /* get error code */ + secure_error = (new_req.error) ? OTE_ERROR_NO_DATA + : new_req.result; + + /* signal the producer */ + complete(&req_complete); + break; + } + + return 0; +} + +static void _te_fs_file_operation(const char *name, void *buf, int len, + enum te_file_req_type type) +{ + struct te_file_req *new_req; + struct te_file_req_node *req_node; + + if (!test_and_clear_bit(TE_FS_READY_BIT, &fs_ready)) { + pr_err("%s: daemon not loaded yet\n", __func__); + secure_error = OTE_ERROR_NO_DATA; + goto fail; + } + + BUG_ON(!name); + + if (type == OTE_FILE_REQ_READ || type == OTE_FILE_REQ_WRITE) + BUG_ON(!buf); + + /* allocate te_file_req structure */ + new_req = kzalloc(sizeof(struct te_file_req), GFP_KERNEL); + BUG_ON(!new_req); + + /* prepare a new request */ + strncpy(new_req->name, name, strlen(name)); + new_req->type = type; + new_req->data_len = len; + new_req->result = 0; + new_req->kern_data_buf = buf; + new_req->error = 0; + + req_node = kzalloc(sizeof(struct te_file_req_node), GFP_KERNEL); + BUG_ON(!req_node); + + req_node->req = new_req; + INIT_LIST_HEAD(&req_node->node); + + /* add it to the pending queue and signal the consumer */ + list_add_tail(&req_list, &req_node->node); + complete(&req_ready); + + set_freezable(); + + /* wait for the consumer's signal */ + while (wait_for_completion_interruptible(&req_complete)) + try_to_freeze(); + + kfree(new_req); + +fail: + /* signal completion to the secure world */ + indicate_complete(secure_error); +} + +void tlk_fread(const char *name, void *buf, int len) +{ + if (!buf) + _te_fs_file_operation(name, buf, len, OTE_FILE_REQ_SIZE); + else + _te_fs_file_operation(name, buf, len, OTE_FILE_REQ_READ); +} + +void tlk_fwrite(const char *name, void *buf, int len) +{ + _te_fs_file_operation(name, buf, len, OTE_FILE_REQ_WRITE); +} + +void tlk_fdelete(const char *name) +{ + _te_fs_file_operation(name, NULL, 0, OTE_FILE_REQ_DELETE); +} + +static int __init tlk_fs_register_handlers(void) +{ + struct te_file_req_shmem *shmem_ptr; + uint32_t smc_args[MAX_EXT_SMC_ARGS]; + + shmem_ptr = kzalloc(sizeof(struct te_file_req_shmem), GFP_KERNEL); + if (!shmem_ptr) { + pr_err("%s: no memory available for fs operations\n", __func__); + return -ENOMEM; + } + + INIT_LIST_HEAD(&req_list); + init_completion(&req_ready); + init_completion(&req_complete); + + smc_args[0] = 0xFFFF1FF2; + smc_args[1] = (uint32_t)tlk_fread; + smc_args[2] = (uint32_t)tlk_fwrite; + smc_args[3] = (uint32_t)tlk_fdelete; + smc_args[4] = (uint32_t)shmem_ptr->file_name; + smc_args[5] = (uint32_t)shmem_ptr->file_data; + + tlk_extended_smc(smc_args); + + return 0; +} + +arch_initcall(tlk_fs_register_handlers); diff --git a/security/tlk_driver/ote_irq.S b/security/tlk_driver/ote_irq.S new file mode 100644 index 000000000000..7bfda8861108 --- /dev/null +++ b/security/tlk_driver/ote_irq.S @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2013 NVIDIA Corporation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + */ + +#include <linux/linkage.h> +#include <linux/init.h> + +ENTRY(tlk_irq_handler) + movw r0, #0x1FF1 + movt r0, #0xFFFF + smc #0 +ENDPROC(tlk_irq_handler) diff --git a/security/tlk_driver/ote_protocol.h b/security/tlk_driver/ote_protocol.h new file mode 100644 index 000000000000..79224e1c3669 --- /dev/null +++ b/security/tlk_driver/ote_protocol.h @@ -0,0 +1,202 @@ +/* + * Copyright (c) 2013 NVIDIA Corporation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef __OTE_PROTOCOL_H__ +#define __OTE_PROTOCOL_H__ + +#include "ote_types.h" + +#define TE_IOCTL_MAGIC_NUMBER ('t') +#define TE_IOCTL_OPEN_CLIENT_SESSION \ + _IOWR(TE_IOCTL_MAGIC_NUMBER, 0x10, union te_cmd) +#define TE_IOCTL_CLOSE_CLIENT_SESSION \ + _IOWR(TE_IOCTL_MAGIC_NUMBER, 0x11, union te_cmd) +#define TE_IOCTL_LAUNCH_OPERATION \ + _IOWR(TE_IOCTL_MAGIC_NUMBER, 0x14, union te_cmd) +#define TE_IOCTL_FILE_NEW_REQ \ + _IOR(TE_IOCTL_MAGIC_NUMBER, 0x16, struct te_file_req) +#define TE_IOCTL_FILE_FILL_BUF \ + _IOR(TE_IOCTL_MAGIC_NUMBER, 0x17, struct te_file_req) +#define TE_IOCTL_FILE_REQ_COMPLETE \ + _IOWR(TE_IOCTL_MAGIC_NUMBER, 0x18, struct te_file_req) + +#define TE_IOCTL_MIN_NR _IOC_NR(TE_IOCTL_OPEN_CLIENT_SESSION) +#define TE_IOCTL_MAX_NR _IOC_NR(TE_IOCTL_FILE_REQ_COMPLETE) + +#define MAX_EXT_SMC_ARGS 12 + +extern uint32_t tlk_generic_smc(uint32_t arg0, uint32_t arg1, uint32_t arg2); +extern uint32_t tlk_extended_smc(uint32_t *args); +extern void tlk_irq_handler(void); + +struct tlk_device { + unsigned long req_addr; + struct te_oper_param *param_addr; + unsigned long *param_bitmap; + + struct list_head used_cmd_list; + struct list_head free_cmd_list; +}; + +struct te_cmd_req_desc { + unsigned long req_addr; + struct list_head list; +}; + +struct te_shmem_desc { + struct list_head list; + void *buffer; + size_t size; + unsigned int mem_type; + struct page **pages; + unsigned int nr_pages; +}; + +struct tlk_context { + struct tlk_device *dev; + struct list_head shmem_alloc_list; +}; + +enum { + TE_SMC_OPEN_SESSION = 0xFFFF1004, + TE_SMC_CLOSE_SESSION = 0xFFFF1005, + TE_SMC_LAUNCH_OPERATION = 0xFFFF1000, +}; + +enum { + TE_PARAM_TYPE_NONE = 0, + TE_PARAM_TYPE_INT_RO = 1, + TE_PARAM_TYPE_INT_RW = 2, + TE_PARAM_TYPE_MEM_RO = 3, + TE_PARAM_TYPE_MEM_RW = 4, +}; + +struct te_oper_param { + uint32_t index; + uint32_t type; + union { + struct { + uint32_t val; + } Int; + struct { + void *base; + uint32_t len; + } Mem; + } u; + void *next_ptr_user; +}; + +struct te_operation { + uint32_t command; + struct te_oper_param *list_head; + /* Maintain a pointer to tail of list to easily add new param node */ + struct te_oper_param *list_tail; + uint32_t list_count; + uint32_t status; + uint32_t iterface_side; +}; + +struct te_service_id { + uint32_t time_low; + uint16_t time_mid; + uint16_t time_hi_and_version; + uint8_t clock_seq_and_node[8]; +}; + +/* + * OpenSession + */ +struct te_opensession { + struct te_service_id dest_uuid; + struct te_operation operation; + uint32_t answer; +}; + +/* + * CloseSession + */ +struct te_closesession { + uint32_t session_id; + uint32_t answer; +}; + +/* + * LaunchOperation + */ +struct te_launchop { + uint32_t session_id; + struct te_operation operation; + uint32_t answer; +}; + +union te_cmd { + struct te_opensession opensession; + struct te_closesession closesession; + struct te_launchop launchop; +}; + +struct te_request { + uint32_t type; + uint32_t session_id; + uint32_t command_id; + struct te_oper_param *params; + uint32_t params_size; + uint32_t dest_uuid[4]; + uint32_t result; + uint32_t result_origin; +}; + +struct te_answer { + uint32_t result; + uint32_t session_id; + uint32_t return_origin; +}; + +void te_open_session(struct te_opensession *cmd, + struct te_request *request, + struct tlk_context *context); + +void te_close_session(struct te_closesession *cmd, + struct te_request *request); + +void te_launch_operation(struct te_launchop *cmd, + struct te_request *request, + struct tlk_context *context); + +#define TE_MAX_FILE_NAME_LEN 64 + +enum te_file_req_type { + OTE_FILE_REQ_READ = 0, + OTE_FILE_REQ_WRITE = 1, + OTE_FILE_REQ_DELETE = 2, + OTE_FILE_REQ_SIZE = 3, +}; + +struct te_file_req { + char name[TE_MAX_FILE_NAME_LEN]; + enum te_file_req_type type; + void *user_data_buf; + void *kern_data_buf; + unsigned long data_len; + unsigned long result; + int error; +}; + +int te_handle_fs_ioctl(struct file *file, unsigned int ioctl_num, + unsigned long ioctl_param); +#endif diff --git a/security/tlk_driver/ote_types.h b/security/tlk_driver/ote_types.h new file mode 100644 index 000000000000..ef82d4ffec9d --- /dev/null +++ b/security/tlk_driver/ote_types.h @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2013 NVIDIA Corporation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef __OTE_TYPES_H__ +#define __OTE_TYPES_H__ + +/* + * Return Codes + */ +enum { + /* Success */ + OTE_SUCCESS = 0x00000000, + OTE_ERROR_NO_ERROR = OTE_SUCCESS, + /* Non-specific cause */ + OTE_ERROR_GENERIC = 0xFFFF0000, + /* Access priviledge not sufficient */ + OTE_ERROR_ACCESS_DENIED = 0xFFFF0001, + /* The operation was cancelled */ + OTE_ERROR_CANCEL = 0xFFFF0002, + /* Concurrent accesses conflict */ + OTE_ERROR_ACCESS_CONFLICT = 0xFFFF0003, + /* Too much data for req was passed */ + OTE_ERROR_EXCESS_DATA = 0xFFFF0004, + /* Input data was of invalid format */ + OTE_ERROR_BAD_FORMAT = 0xFFFF0005, + /* Input parameters were invalid */ + OTE_ERROR_BAD_PARAMETERS = 0xFFFF0006, + /* Oper invalid in current state */ + OTE_ERROR_BAD_STATE = 0xFFFF0007, + /* The req data item not found */ + OTE_ERROR_ITEM_NOT_FOUND = 0xFFFF0008, + /* The req oper not implemented */ + OTE_ERROR_NOT_IMPLEMENTED = 0xFFFF0009, + /* The req oper not supported */ + OTE_ERROR_NOT_SUPPORTED = 0xFFFF000A, + /* Expected data was missing */ + OTE_ERROR_NO_DATA = 0xFFFF000B, + /* System ran out of resources */ + OTE_ERROR_OUT_OF_MEMORY = 0xFFFF000C, + /* The system is busy */ + OTE_ERROR_BUSY = 0xFFFF000D, + /* Communication failed */ + OTE_ERROR_COMMUNICATION = 0xFFFF000E, + /* A security fault was detected */ + OTE_ERROR_SECURITY = 0xFFFF000F, + /* The supplied buffer is too short */ + OTE_ERROR_SHORT_BUFFER = 0xFFFF0010, +}; + +/* + * Return Code origins + */ +enum { + /* Originated from OTE Client API */ + OTE_ERROR_ORIGIN_API = 1, + /* Originated from Underlying Communication Stack */ + OTE_ERROR_ORIGIN_COMMS = 2, + /* Originated from Common OTE Code */ + OTE_ERROR_ORIGIN_KERNEL = 3, + /* Originated from Trusted APP Code */ + OTE_ERROR_ORIGIN_TRUSTED_APP = 4, +}; + +#endif |