diff --git a/trustzone-awared-vm/Host/itrustee_tzdriver.patch b/trustzone-awared-vm/Host/itrustee_tzdriver.patch index 9b42f69f8e53a839a1ab171fd9ae08c3c0ff13de..271203306db766165e7c2975201346a2bc8ff2c5 100644 --- a/trustzone-awared-vm/Host/itrustee_tzdriver.patch +++ b/trustzone-awared-vm/Host/itrustee_tzdriver.patch @@ -1,20 +1,3 @@ -From 011ed576f4405a930b0cffa31a984fb4f03146e3 Mon Sep 17 00:00:00 2001 -From: Ju Fu -Date: Tue, 25 Jun 2024 15:15:51 +0800 -Subject: [PATCH OLK-5.10] cvm: delete dead code and resolve macro definition - holes - -virtcca inclusion -category: feature -bugzilla: https://gitee.com/openeuler/kernel/issues/I9TM0T - --------------------------------- - -delete dead code and resolve macro definition holes - -Signed-off-by: Ju Fu ---- - diff -uprN itrustee_tzdriver/auth/auth_base_impl.c itrustee_tzdriver_new/auth/auth_base_impl.c --- itrustee_tzdriver/auth/auth_base_impl.c 2024-07-12 11:08:59.353629380 +0800 +++ itrustee_tzdriver_new/auth/auth_base_impl.c 2024-07-12 10:51:32.889629380 +0800 diff --git a/trustzone-awared-vm/Host/qemu.patch b/trustzone-awared-vm/Host/qemu.patch index 68b87eb2391b44d426d5d5c95ffa55285735b66b..ee6074c0debbc21cce65f22cc2deea093c67891f 100644 --- a/trustzone-awared-vm/Host/qemu.patch +++ b/trustzone-awared-vm/Host/qemu.patch @@ -1,23 +1,6 @@ -From 011ed576f4405a930b0cffa31a984fb4f03146e3 Mon Sep 17 00:00:00 2001 -From: Ju Fu -Date: Tue, 25 Jun 2024 15:15:51 +0800 -Subject: [PATCH OLK-5.10] cvm: delete dead code and resolve macro definition - holes - -virtcca inclusion -category: feature -bugzilla: https://gitee.com/openeuler/kernel/issues/I9TM0T - --------------------------------- - -delete dead code and resolve macro definition holes - -Signed-off-by: Ju Fu ---- - -diff -ruNp qemu/hw/char/tc_ns_client.h qemu-after/hw/char/tc_ns_client.h +diff -purN qemu/hw/char/tc_ns_client.h qemu_new/hw/char/tc_ns_client.h --- qemu/hw/char/tc_ns_client.h 1970-01-01 08:00:00.000000000 +0800 -+++ qemu-after/hw/char/tc_ns_client.h 2024-07-12 17:36:25.179832480 +0800 ++++ qemu_new/hw/char/tc_ns_client.h 2024-09-10 11:48:21.650599250 +0800 @@ -0,0 +1,162 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2012-2023. All rights reserved. @@ -181,10 +164,10 @@ diff -ruNp qemu/hw/char/tc_ns_client.h qemu-after/hw/char/tc_ns_client.h +#endif + + -diff -ruNp qemu/hw/char/tee_client_constants.h qemu-after/hw/char/tee_client_constants.h +diff -purN qemu/hw/char/tee_client_constants.h qemu_new/hw/char/tee_client_constants.h --- qemu/hw/char/tee_client_constants.h 1970-01-01 08:00:00.000000000 +0800 -+++ qemu-after/hw/char/tee_client_constants.h 2024-07-12 17:36:25.171832480 +0800 -@@ -0,0 +1,126 @@ ++++ qemu_new/hw/char/tee_client_constants.h 2024-09-10 11:50:22.282599250 +0800 +@@ -0,0 +1,127 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2013-2022. All rights reserved. + * Licensed under the Mulan PSL v2. @@ -275,6 +258,7 @@ diff -ruNp qemu/hw/char/tee_client_constants.h qemu-after/hw/char/tee_client_con + TEEC_ION_INPUT = 0x08, /* input type of icon memory reference, refer TEEC_IonReference */ + TEEC_ION_SGLIST_INPUT = 0x09, /* input type of ion memory block reference, refer TEEC_IonSglistReference */ + TEEC_MEMREF_SHARED_INOUT = 0x0a, /* no copy mem */ ++ TEEC_MEMREF_REGISTER_INOUT = 0x0b, + TEEC_MEMREF_WHOLE = 0xc, /* use whole memory block, refer TEEC_RegisteredMemoryReference */ + TEEC_MEMREF_PARTIAL_INPUT = 0xd, /* input type of memory reference, refer TEEC_RegisteredMemoryReference */ + TEEC_MEMREF_PARTIAL_OUTPUT = 0xe, /* output type of memory reference, refer TEEC_RegisteredMemoryReference */ @@ -311,9 +295,9 @@ diff -ruNp qemu/hw/char/tee_client_constants.h qemu-after/hw/char/tee_client_con + +#define TEEC_PARAM_NUM 4 /* teec param max number */ +#endif -diff -ruNp qemu/hw/char/tee_client_list.h qemu-after/hw/char/tee_client_list.h +diff -purN qemu/hw/char/tee_client_list.h qemu_new/hw/char/tee_client_list.h --- qemu/hw/char/tee_client_list.h 1970-01-01 08:00:00.000000000 +0800 -+++ qemu-after/hw/char/tee_client_list.h 2024-07-12 17:36:25.163832480 +0800 ++++ qemu_new/hw/char/tee_client_list.h 2024-09-10 11:48:21.650599250 +0800 @@ -0,0 +1,101 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2013-2021. All rights reserved. @@ -416,9 +400,9 @@ diff -ruNp qemu/hw/char/tee_client_list.h qemu-after/hw/char/tee_client_list.h +#endif + + -diff -ruNp qemu/hw/char/tee_client_type.h qemu-after/hw/char/tee_client_type.h +diff -purN qemu/hw/char/tee_client_type.h qemu_new/hw/char/tee_client_type.h --- qemu/hw/char/tee_client_type.h 1970-01-01 08:00:00.000000000 +0800 -+++ qemu-after/hw/char/tee_client_type.h 2024-07-12 17:36:25.155832480 +0800 ++++ qemu_new/hw/char/tee_client_type.h 2024-09-10 11:48:21.650599250 +0800 @@ -0,0 +1,134 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2013-2022. All rights reserved. @@ -554,9 +538,9 @@ diff -ruNp qemu/hw/char/tee_client_type.h qemu-after/hw/char/tee_client_type.h +#endif + + -diff -ruNp qemu/hw/char/virtio-console.c qemu-after/hw/char/virtio-console.c ---- qemu/hw/char/virtio-console.c 2024-07-12 17:36:25.127832480 +0800 -+++ qemu-after/hw/char/virtio-console.c 2024-07-12 17:36:25.151832480 +0800 +diff -purN qemu/hw/char/virtio-console.c qemu_new/hw/char/virtio-console.c +--- qemu/hw/char/virtio-console.c 2024-09-10 11:56:07.870599250 +0800 ++++ qemu_new/hw/char/virtio-console.c 2024-09-10 11:52:50.618599250 +0800 @@ -22,6 +22,13 @@ #include "qapi/qapi-events-char.h" #include "qom/object.h" @@ -571,7 +555,7 @@ diff -ruNp qemu/hw/char/virtio-console.c qemu-after/hw/char/virtio-console.c #define TYPE_VIRTIO_CONSOLE_SERIAL_PORT "virtserialport" typedef struct VirtConsole VirtConsole; DECLARE_INSTANCE_CHECKER(VirtConsole, VIRTIO_CONSOLE, -@@ -48,18 +55,327 @@ static gboolean chr_write_unblocked(void +@@ -48,18 +55,395 @@ static gboolean chr_write_unblocked(void return FALSE; } @@ -667,6 +651,13 @@ diff -ruNp qemu/hw/char/virtio-console.c qemu-after/hw/char/virtio-console.c + uint32_t seq_num; + int32_t ptzfd; + int32_t cpu_index; ++ int32_t err_flag; ++ int32_t is_fragment; ++ uint32_t fragment_block_num; ++ uint32_t vm_page_size;; ++ uint64_t block_addrs[TEEC_PARAM_NUM];//qemu and proxy don't use ++ uint32_t block_size[TEEC_PARAM_NUM]; ++ unsigned long long addrs[TEEC_PARAM_NUM]; //used by ref mem mmap + TC_NS_ClientContext cliContext; +} struct_packet_cmd_session; + @@ -785,9 +776,12 @@ diff -ruNp qemu/hw/char/virtio-console.c qemu-after/hw/char/virtio-console.c + debug("sizeof(TC_NS_ClientContext) =%d \n", sizeof(TC_NS_ClientContext)); + if ( len >= sizeof(struct_packet_cmd_session) ) { + struct_packet_cmd_session* vtzf_packet_cmd = (struct_packet_cmd_session *)buf; -+ debug(" vtzf_packet_cmd->cliContext.file_size = 0x%08x, %d \n", vtzf_packet_cmd->cliContext.file_size, -+ vtzf_packet_cmd->cliContext.file_size); -+ debug(" vtzf_packet_cmd->cliContext.file_buffer = 0x%016lx \n", vtzf_packet_cmd->cliContext.file_buffer); ++ uint32_t packet_size =vtzf_packet_cmd->packet_size; ++ if (len != packet_size && !vtzf_packet_cmd->fragment_block_num) { ++ debug("err ,len != packet_size \n"); ++ vtzf_packet_cmd->err_flag = 1; ++ break; ++ } + hwaddr gpa = (uint64_t)vtzf_packet_cmd->cliContext.file_buffer; + ptr_hva = gpa2hva(&mr, gpa, 1, &local_err); + if (local_err) { @@ -799,6 +793,64 @@ diff -ruNp qemu/hw/char/virtio-console.c qemu-after/hw/char/virtio-console.c + ui64_hva = (uint64_t)ptr_hva; + vtzf_packet_cmd->cliContext.file_buffer = (void *)ui64_hva; + } ++ ++ uint32_t param_type; ++ bool check_value; ++ ++ for (i = 0; i < TEEC_PARAM_NUM; i++) { ++ param_type = TEEC_PARAM_TYPE_GET(vtzf_packet_cmd->cliContext.paramTypes, i); ++ check_value = (param_type == TEEC_ION_INPUT || param_type == TEEC_ION_SGLIST_INPUT); ++ if (IS_TEMP_MEM(param_type)) { ++ gpa_param = (uint64_t)vtzf_packet_cmd->cliContext.params[i].memref.buffer; ++ ptr_hva = gpa2hva(&mr, gpa_param, 1, &local_err); ++ if (local_err) { ++ debug(" gpa2hva params[%d].memref.buffer failed \n", i); ++ } else { ++ debug(" host virtual address of memref.buffer = 0x%016lx, %p \n", (uint64_t)ptr_hva, ptr_hva); ++ memory_region_unref(mr); ++ uint64_t ui64_hva; ++ ui64_hva = (uint64_t)ptr_hva; ++ vtzf_packet_cmd->cliContext.params[i].memref.buffer = ui64_hva; ++ } ++ } else if (IS_PARTIAL_MEM(param_type)) { ++ gpa_param = (uint64_t)vtzf_packet_cmd->cliContext.params[i].memref.buffer; ++ ptr_hva = gpa2hva(&mr, gpa_param, 1, &local_err); ++ if (local_err) { ++ debug(" gpa2hva params[%d].memref.buffer failed \n", i); ++ } else { ++ debug(" host virtual address of memref.buffer = 0x%016lx, %p \n", (uint64_t)ptr_hva, ptr_hva); ++ memory_region_unref(mr); ++ uint64_t ui64_hva; ++ ui64_hva = (uint64_t)ptr_hva; ++ vtzf_packet_cmd->cliContext.params[i].memref.buffer = ui64_hva; ++ } ++ } else if (param_type == TEEC_MEMREF_SHARED_INOUT || param_type == TEEC_MEMREF_REGISTER_INOUT) { ++ /* do nothing */ ++ } else if (IS_VALUE_MEM(param_type) || check_value) { ++ /* do nothing */ ++ } else { ++ /* if type is none, ignore it */ ++ } ++ }// end for ++ ++ fragment_block_num = vtzf_packet_cmd->fragment_block_num; ++ if (fragment_block_num != 0) { ++ page_block = (struct_page_block *)((char *)vtzf_packet_cmd + sizeof(struct_packet_cmd_session)); ++ for(j = 0; j < fragment_block_num; j++){ ++ gpa_param = page_block[j].phy_addr; ++ debug("page_block[%d].phy_addr = %llx\n", j, page_block[j].phy_addr); ++ ptr_hva = gpa2hva(&mr, gpa_param, 1, &local_err); ++ if (local_err) { ++ debug(" gpa2hva params[%d].memref.buffer failed \n", i); ++ } else { ++ debug(" host virtual address of memref.buffer = 0x%016lx, %p \n", (uint64_t)ptr_hva, ptr_hva); ++ memory_region_unref(mr); ++ uint64_t ui64_hva; ++ ui64_hva = (uint64_t)ptr_hva; ++ page_block[j].phy_addr = ui64_hva; ++ } ++ } ++ } + } + break; + case VTZF_SEND_CMD: @@ -841,7 +893,7 @@ diff -ruNp qemu/hw/char/virtio-console.c qemu-after/hw/char/virtio-console.c + ui64_hva = (uint64_t)ptr_hva; + vtzf_packet_cmd->cliContext.params[i].memref.buffer = ui64_hva; + } -+ } else if (param_type == TEEC_MEMREF_SHARED_INOUT) { ++ } else if (param_type == TEEC_MEMREF_SHARED_INOUT || param_type == TEEC_MEMREF_REGISTER_INOUT) { + /* do nothing */ + } else if (IS_VALUE_MEM(param_type) || check_value) { + /* do nothing */ diff --git a/trustzone-awared-vm/Host/vtzb_proxy/agent.c b/trustzone-awared-vm/Host/vtzb_proxy/agent.c index 019d3cc4e0493888d4e21f0351a065d0fe61fe47..b8e6da7fbda29ff1f9989070e340e2d68e7f678a 100644 --- a/trustzone-awared-vm/Host/vtzb_proxy/agent.c +++ b/trustzone-awared-vm/Host/vtzb_proxy/agent.c @@ -89,7 +89,7 @@ END: packet_rsp.args = packet_cmd->args; ret = send_to_vm(serial_port, &packet_rsp, sizeof(packet_rsp)); if (ret != sizeof(packet_rsp)) { - tloge("send to VM failed \n"); + tloge("register_agent send to VM failed \n"); } } @@ -128,7 +128,7 @@ void wait_event(struct_packet_cmd_event *packet_cmd, ret = send_to_vm(serial_port, &packet_rsp, sizeof(packet_rsp)); if (ret != sizeof(packet_rsp)) { - tloge("send to VM failed \n"); + tloge("wait_event send to VM failed \n"); } } @@ -165,6 +165,6 @@ void sent_event_response(struct_packet_cmd_event *packet_cmd, ret = send_to_vm(serial_port, &packet_rsp, sizeof(packet_rsp)); if (ret != sizeof(packet_rsp)) { - tloge("send to VM failed \n"); + tloge("sent_event_response send to VM failed \n"); } } \ No newline at end of file diff --git a/trustzone-awared-vm/Host/vtzb_proxy/comm_structs.h b/trustzone-awared-vm/Host/vtzb_proxy/comm_structs.h index f75141b8c7e1974482c0f958f24abeac6d033c06..582caacb55711cced9d2e7d6050ca83d74d757eb 100644 --- a/trustzone-awared-vm/Host/vtzb_proxy/comm_structs.h +++ b/trustzone-awared-vm/Host/vtzb_proxy/comm_structs.h @@ -205,6 +205,13 @@ typedef struct { uint32_t seq_num; int32_t ptzfd; __s32 cpu_index; + int32_t err_flag; + int32_t is_fragment; + uint32_t fragment_block_num; + uint32_t vm_page_size; + uint64_t block_addrs[4];//qemu and proxy don't use + uint32_t block_size[4]; + unsigned long long addrs[4]; //used by ref mem mmap TC_NS_ClientContext cliContext; } struct_packet_cmd_session; diff --git a/trustzone-awared-vm/Host/vtzb_proxy/include/tee_client_constants.h b/trustzone-awared-vm/Host/vtzb_proxy/include/tee_client_constants.h index 2a7e31f678d2c9ad31a2ac44d9b4e7cc58b6700a..f9bb672244e85063b7e2be0474f9a0df57d91ae1 100644 --- a/trustzone-awared-vm/Host/vtzb_proxy/include/tee_client_constants.h +++ b/trustzone-awared-vm/Host/vtzb_proxy/include/tee_client_constants.h @@ -88,6 +88,7 @@ enum TEEC_ParamType { TEEC_ION_INPUT = 0x08, /* input type of icon memory reference, refer TEEC_IonReference */ TEEC_ION_SGLIST_INPUT = 0x09, /* input type of ion memory block reference, refer TEEC_IonSglistReference */ TEEC_MEMREF_SHARED_INOUT = 0x0a, /* no copy mem */ + TEEC_MEMREF_REGISTER_INOUT = 0x0b, /* no copy mem */ TEEC_MEMREF_WHOLE = 0xc, /* use whole memory block, refer TEEC_RegisteredMemoryReference */ TEEC_MEMREF_PARTIAL_INPUT = 0xd, /* input type of memory reference, refer TEEC_RegisteredMemoryReference */ TEEC_MEMREF_PARTIAL_OUTPUT = 0xe, /* output type of memory reference, refer TEEC_RegisteredMemoryReference */ diff --git a/trustzone-awared-vm/Host/vtzb_proxy/serial_port.c b/trustzone-awared-vm/Host/vtzb_proxy/serial_port.c index 62cf283711efa4197c20934dbee6f685d3fc9f93..2884b2cbe86a602b4b452f4f06764f57ac71f339 100644 --- a/trustzone-awared-vm/Host/vtzb_proxy/serial_port.c +++ b/trustzone-awared-vm/Host/vtzb_proxy/serial_port.c @@ -84,10 +84,35 @@ void serial_port_list_destroy() int send_to_vm(struct serial_port_file *serial_port, void *packet_rsp, size_t size_rsp) { int ret = 0; + int index; if (!serial_port || serial_port->sock <= 0 || !packet_rsp) return -1; pthread_mutex_lock(&serial_port->lock); - ret = write(serial_port->sock, packet_rsp, size_rsp); + ret = send(serial_port->sock, packet_rsp, size_rsp, MSG_NOSIGNAL); + if (ret == -1) { + if (errno == EPIPE) { + // 处理 EPIPE 错误 + close(serial_port->sock); + for (index = 0; index < g_pollfd_len; index++) { + if (g_pollfd[index].fd == serial_port->sock) { + break; + } + } + serial_port->sock = 0; + g_serial_array[index] = g_serial_array[g_pollfd_len - 1]; + g_pollfd[index] = g_pollfd[g_pollfd_len - 1]; + g_pollfd_len--; + serial_port->opened = false; + if (serial_port->vm_file) { + destroy_vm_file(serial_port->vm_file); + } + serial_port->vm_file = NULL; + tloge("Send failed with EPIPE: Broken pipe, socket closed\n"); + } else { + + tloge("Send failed, errno: %d\n", errno); + } + } pthread_mutex_unlock(&serial_port->lock); return ret; } diff --git a/trustzone-awared-vm/Host/vtzb_proxy/vtzb_proxy.c b/trustzone-awared-vm/Host/vtzb_proxy/vtzb_proxy.c index 212e22b380a1d7de4d85cd28fafecf283711642c..2b42939f1c23829c78bddcf15c2ff52248ace11f 100644 --- a/trustzone-awared-vm/Host/vtzb_proxy/vtzb_proxy.c +++ b/trustzone-awared-vm/Host/vtzb_proxy/vtzb_proxy.c @@ -121,7 +121,7 @@ static void close_tzdriver(struct_packet_cmd_close_tzd *packet_cmd, } if (send_to_vm(serial_port, &packet_rsp, sizeof(packet_rsp)) != sizeof(packet_rsp)) - tloge("close ptzfd send to VM failed \n"); + tloge("close_tzdriver send to VM failed \n"); } static void log_in_NonHidl(struct_packet_cmd_login_non *packet_cmd, @@ -136,7 +136,7 @@ static void log_in_NonHidl(struct_packet_cmd_login_non *packet_cmd, ret = send_to_vm(serial_port, &packet_rsp, sizeof(packet_rsp)); if (ret != sizeof(packet_rsp)) - tloge("send to VM failed \n"); + tloge("log_in_NonHidl send to VM failed \n"); } static void log_in(struct_packet_cmd_login *packet_cmd, @@ -150,7 +150,7 @@ static void log_in(struct_packet_cmd_login *packet_cmd, packet_rsp.ret = ret; ret = send_to_vm(serial_port, &packet_rsp, sizeof(packet_rsp)); if (ret != sizeof(packet_rsp)) { - tloge("send to VM failed \n"); + tloge("log_in send to VM failed \n"); } } @@ -165,7 +165,7 @@ static void get_tee_ver(struct_packet_cmd_getteever *packet_cmd, packet_rsp.ret = ret; ret = send_to_vm(serial_port, &packet_rsp, sizeof(packet_rsp)); if (ret != sizeof(packet_rsp)) - tloge("send to VM failed \n"); + tloge("get_tee_ver send to VM failed \n"); } static void get_tee_info(struct_packet_cmd_getteeinfo *packet_cmd, @@ -183,7 +183,7 @@ static void get_tee_info(struct_packet_cmd_getteeinfo *packet_cmd, packet_rsp.ret = ret; ret = send_to_vm(serial_port, &packet_rsp, sizeof(packet_rsp)); if (ret != sizeof(packet_rsp)) - tloge("send to VM failed \n"); + tloge("get_tee_info send to VM failed \n"); } static void sync_sys_time(struct_packet_cmd_synctime *packet_cmd, @@ -197,19 +197,31 @@ static void sync_sys_time(struct_packet_cmd_synctime *packet_cmd, packet_rsp.ret = ret; ret = send_to_vm(serial_port, &packet_rsp, sizeof(packet_rsp)); if (ret != sizeof(packet_rsp)) - tloge("send to VM failed \n"); + tloge("sync_sys_time send to VM failed \n"); } +static int process_address_sess(struct_packet_cmd_session *packet_cmd, + ClientParam params[], struct vm_file *vm_fp); +static void process_address_end_sess(struct_packet_cmd_session *packet_cmd, ClientParam params[]); +static void set_thread_id(int ptzfd, unsigned int session_id, int flag, struct vm_file *vm_fp); + static void open_session(struct_packet_cmd_session *packet_cmd, struct serial_port_file *serial_port) { - int ret; + int ret = -1; int index; struct_packet_rsp_session packet_rsp; + ClientParam params[TEEC_PARAM_NUM]; packet_rsp.seq_num = packet_cmd->seq_num + 1; packet_rsp.packet_size = sizeof(packet_rsp); index = set_start_time(pthread_self(), packet_cmd->seq_num, serial_port); - ret = ioctl(packet_cmd->ptzfd, TC_NS_CLIENT_IOCTL_SES_OPEN_REQ, &packet_cmd->cliContext); + if (!process_address_sess(packet_cmd, params, serial_port->vm_file)) { + set_thread_id(packet_cmd->ptzfd, packet_cmd->cliContext.session_id, 1, serial_port->vm_file); + ret = ioctl(packet_cmd->ptzfd, TC_NS_CLIENT_IOCTL_SES_OPEN_REQ, &packet_cmd->cliContext); + + set_thread_id(packet_cmd->ptzfd, packet_cmd->cliContext.session_id, 0, serial_port->vm_file); + process_address_end_sess(packet_cmd, params); + } remove_start_time(index); packet_rsp.ret = ret; packet_rsp.cliContext = packet_cmd->cliContext; @@ -218,7 +230,7 @@ static void open_session(struct_packet_cmd_session *packet_cmd, ret = send_to_vm(serial_port, &packet_rsp, sizeof(packet_rsp)); if (ret != sizeof(packet_rsp)) - tloge("send to VM failed \n"); + tloge("open_session send to VM failed \n"); } static void close_session(struct_packet_cmd_session *packet_cmd, @@ -234,7 +246,7 @@ static void close_session(struct_packet_cmd_session *packet_cmd, packet_rsp.ret = ret; ret = send_to_vm(serial_port, &packet_rsp, sizeof(packet_rsp)); if (ret != sizeof(packet_rsp)) - tloge("send to VM failed \n"); + tloge("close_session send to VM failed \n"); remove_session(packet_cmd->ptzfd, packet_cmd->cliContext.session_id, serial_port->vm_file); } @@ -336,6 +348,104 @@ static int process_address(struct_packet_cmd_send_cmd *packet_cmd, return ret; } +static int process_address_sess(struct_packet_cmd_session *packet_cmd, + ClientParam params[], struct vm_file *vm_fp) +{ + int index; + int icount = 0; + int ret = 0; + uint32_t paramTypes[TEEC_PARAM_NUM]; + uint64_t *vm_hvas = (uint64_t *)packet_cmd->cliContext.file_buffer; + uint32_t offset = sizeof(struct_packet_cmd_session); + for (index = 0; index < TEEC_PARAM_NUM; index++) { + paramTypes[index] = + TEEC_PARAM_TYPE_GET(packet_cmd->cliContext.paramTypes, index); + if (IS_PARTIAL_MEM(paramTypes[index])) { + void *vm_buffer = (void *)packet_cmd->addrs[index]; + bool b_found = false; + struct ListNode *ptr = NULL; + + params[index].memref.buf_size = packet_cmd->cliContext.params[index].memref.size_addr; + packet_cmd->cliContext.params[index].memref.size_addr = + (unsigned int)((uintptr_t)¶ms[index].memref.buf_size); + packet_cmd->cliContext.params[index].memref.size_h_addr = + (unsigned int)((uint64_t)¶ms[index].memref.buf_size >> H_OFFSET); + + pthread_mutex_lock(&vm_fp->shrd_mem_lock); + if (!LIST_EMPTY(&vm_fp->shrd_mem_head)) { + LIST_FOR_EACH(ptr, &vm_fp->shrd_mem_head) { + struct_shrd_mem *shrd_mem = + CONTAINER_OF(ptr, struct_shrd_mem, node); + if (shrd_mem->vm_buffer == vm_buffer) { + vm_hvas[index] = packet_cmd->cliContext.params[index].memref.buffer + | (uint64_t)packet_cmd->cliContext.params[index].memref.buffer_h_addr << H_OFFSET; + /* Switch to the user address corresponding to the mmap space on the host. */ + packet_cmd->cliContext.params[index].memref.buffer = + (unsigned int)(uintptr_t)shrd_mem->buffer; + packet_cmd->cliContext.params[index].memref.buffer_h_addr = + ((unsigned long long)(uintptr_t)shrd_mem->buffer) >> H_OFFSET; + icount++; + b_found = true; + break; + } + } + } + pthread_mutex_unlock(&vm_fp->shrd_mem_lock); + if (b_found == false) { + tloge("can't find mmap buffer %p \n", vm_buffer); + ret = -1; + return ret; + } + } else if (IS_TEMP_MEM(paramTypes[index])) { + params[index].memref.buf_size = packet_cmd->cliContext.params[index].memref.size_addr; + packet_cmd->cliContext.params[index].memref.size_addr = + (unsigned int)((uintptr_t)¶ms[index].memref.buf_size); + packet_cmd->cliContext.params[index].memref.size_h_addr = + (unsigned int)((uint64_t)¶ms[index].memref.buf_size >> H_OFFSET); + } else if (IS_VALUE_MEM(paramTypes[index])) { + params[index].value.val_a = packet_cmd->cliContext.params[index].value.a_addr; + params[index].value.val_b = packet_cmd->cliContext.params[index].value.b_addr; + + packet_cmd->cliContext.params[index].value.a_addr = + (unsigned int)(uintptr_t)¶ms[index].value.val_a; + packet_cmd->cliContext.params[index].value.a_h_addr = + (unsigned int)((uint64_t)¶ms[index].value.val_a >> H_OFFSET); + packet_cmd->cliContext.params[index].value.b_addr = + (unsigned int)(uintptr_t)¶ms[index].value.val_b; + packet_cmd->cliContext.params[index].value.b_h_addr = + (unsigned int)((uint64_t)¶ms[index].value.val_b >> H_OFFSET); + } else if(IS_SHARED_MEM(paramTypes[index])) { + uint32_t share_mem_size = packet_cmd->cliContext.params[index].memref.size_addr; + struct_page_block *page_block = (struct_page_block *)((char *)packet_cmd + offset); + uint32_t block_buf_size = packet_cmd->block_size[index]; + uint32_t tmp_buf_size = sizeof(struct_page_block) + packet_cmd->block_size[index]; + params[index].share.buf_size = tmp_buf_size; + offset += packet_cmd->block_size[index]; + void *tmp_buf = malloc(tmp_buf_size); + if (!tmp_buf) { + tloge("malloc failed \n"); + return -ENOMEM; + } + ((struct_page_block *)tmp_buf)->share.shared_mem_size = share_mem_size; + ((struct_page_block *)tmp_buf)->share.vm_page_size = packet_cmd->vm_page_size; + struct_page_block *block_buf = (struct_page_block *)((char *)tmp_buf + sizeof(struct_page_block)); + if (memcpy_s((void *)block_buf, block_buf_size, (void *)page_block, block_buf_size) != 0) { + tloge("memcpy_s failed \n"); + return -EFAULT; + } + params[index].share.buf = tmp_buf; + packet_cmd->cliContext.params[index].memref.buffer = (unsigned int)(uintptr_t)tmp_buf; + packet_cmd->cliContext.params[index].memref.buffer_h_addr = (unsigned int)((uint64_t)tmp_buf >> H_OFFSET); + packet_cmd->cliContext.params[index].memref.size_addr = (unsigned int)(uintptr_t)&(params[index].share.buf_size); + packet_cmd->cliContext.params[index].memref.size_h_addr = (unsigned int)((uint64_t)&(params[index].share.buf_size) >> H_OFFSET); + } + }// end for + if (icount ==0) { + // packet_cmd->cliContext.file_buffer = NULL; + } + return ret; +} + static void process_address_end(struct_packet_cmd_send_cmd *packet_cmd, ClientParam params[]) { int index; @@ -359,6 +469,29 @@ static void process_address_end(struct_packet_cmd_send_cmd *packet_cmd, ClientPa } } +static void process_address_end_sess(struct_packet_cmd_session *packet_cmd, ClientParam params[]) +{ + int index; + uint32_t paramTypes[TEEC_PARAM_NUM]; + + for (index = 0; index < TEEC_PARAM_NUM; index++) { + paramTypes[index] = + TEEC_PARAM_TYPE_GET(packet_cmd->cliContext.paramTypes, index); + if (IS_PARTIAL_MEM(paramTypes[index])) { + packet_cmd->cliContext.params[index].memref.size_addr = params[index].memref.buf_size; + } else if (IS_TEMP_MEM(paramTypes[index])) { + packet_cmd->cliContext.params[index].memref.size_addr = params[index].memref.buf_size; + } else if (IS_VALUE_MEM(paramTypes[index])) { + packet_cmd->cliContext.params[index].value.a_addr = params[index].value.val_a; + packet_cmd->cliContext.params[index].value.b_addr = params[index].value.val_b; + } else if(IS_SHARED_MEM(paramTypes[index])) { + if (params[index].share.buf) { + free(params[index].share.buf); + } + } + } +} + static void do_set_thread_id(struct fd_file *fd_p, unsigned int session_id, int flag) { struct ListNode *ptr = NULL; @@ -420,7 +553,7 @@ static void send_cmd(struct_packet_cmd_send_cmd *packet_cmd, packet_rsp.cliContext = packet_cmd->cliContext; ret = send_to_vm(serial_port, &packet_rsp, sizeof(packet_rsp)); if (ret != sizeof(packet_rsp)) { - tloge("send to VM failed \n"); + tloge("send_cmd send to VM failed \n"); } } @@ -436,7 +569,7 @@ static void load_sec_file(struct_packet_cmd_load_sec *packet_cmd, packet_rsp.ioctlArg = packet_cmd->ioctlArg; ret = send_to_vm(serial_port, &packet_rsp, sizeof(packet_rsp)); if (ret != sizeof(packet_rsp)) - tloge("send to VM failed \n"); + tloge("load_sec_file send to VM failed \n"); } static void vtz_dommap(struct_packet_cmd_mmap *packet_cmd, @@ -469,7 +602,7 @@ static void vtz_dommap(struct_packet_cmd_mmap *packet_cmd, END: ret = send_to_vm(serial_port, &packet_rsp, sizeof(packet_rsp)); if (ret != sizeof(packet_rsp)) { - tloge("send to VM failed \n"); + tloge("vtz_dommap send to VM failed \n"); pthread_mutex_lock(&serial_port->vm_file->shrd_mem_lock); ListRemoveEntry(&(tmp->node)); pthread_mutex_unlock(&serial_port->vm_file->shrd_mem_lock); @@ -512,7 +645,7 @@ static void vtz_dounmmap(struct_packet_cmd_mmap *packet_cmd, packet_rsp.ret = ret; ret = send_to_vm(serial_port, &packet_rsp, sizeof(packet_rsp)); if (ret != sizeof(packet_rsp)) { - tloge("send to VM failed \n"); + tloge("vtz_dounmmap send to VM failed \n"); } } @@ -536,7 +669,7 @@ static void vtz_nothing(struct_packet_cmd_nothing *packet_cmd, packet_rsp.ret = 0; ret = send_to_vm(serial_port, &packet_rsp, sizeof(packet_rsp)); if (ret != sizeof(packet_rsp)) { - tloge("send to VM failed \n"); + tloge("vtz_nothing send to VM failed \n"); } } @@ -548,6 +681,7 @@ void *thread_entry(void *args) struct serial_port_file *serial_port = (struct serial_port_file *)u64; char *rd_buf = (char *)(args) + sizeof(uint64_t); ui32_cmd = *(uint32_t *)(rd_buf + sizeof(uint32_t)); + tloge("CMD is %d\n", ui32_cmd); if (ui32_cmd == VTZ_OPEN_TZD) { (void)open_tzdriver((struct_packet_cmd_open_tzd *)rd_buf, serial_port); diff --git a/trustzone-awared-vm/Host/vtzb_proxy/vtzb_proxy.h b/trustzone-awared-vm/Host/vtzb_proxy/vtzb_proxy.h index 3cb0046ed46d66a2fd4b5db24a5ca43cba79d76d..f8eaf495cf1771ecd16ea7967f2e65bdf554048b 100644 --- a/trustzone-awared-vm/Host/vtzb_proxy/vtzb_proxy.h +++ b/trustzone-awared-vm/Host/vtzb_proxy/vtzb_proxy.h @@ -39,7 +39,7 @@ (((paramType) == TEEC_VALUE_INPUT) || ((paramType) == TEEC_VALUE_OUTPUT) || ((paramType) == TEEC_VALUE_INOUT)) #define IS_SHARED_MEM(paramType) \ - ((paramType) == TEEC_MEMREF_SHARED_INOUT) + (((paramType) == TEEC_MEMREF_SHARED_INOUT) || ((paramType) == TEEC_MEMREF_REGISTER_INOUT)) #define PAGE_SIZE getpagesize() diff --git a/trustzone-awared-vm/VM/vtzdriver/comm_structs.h b/trustzone-awared-vm/VM/vtzdriver/comm_structs.h index 7622b9486b1cec5214f5c38a94ca3d8ffa094f41..6af3a8d24fa697892d452556255705293a0f6ba1 100644 --- a/trustzone-awared-vm/VM/vtzdriver/comm_structs.h +++ b/trustzone-awared-vm/VM/vtzdriver/comm_structs.h @@ -232,6 +232,13 @@ typedef struct { uint32_t seq_num; int32_t ptzfd; __s32 cpu_index; + int32_t err_flag; + int32_t is_fragment; + uint32_t fragment_block_num; + uint32_t vm_page_size; + uint64_t block_addrs[TEE_PARAM_NUM]; + uint32_t block_size[TEE_PARAM_NUM]; + unsigned long long addrs[TEE_PARAM_NUM]; //used by ref mem mmap struct tc_ns_client_context cliContext; } struct_packet_cmd_session; diff --git a/trustzone-awared-vm/VM/vtzdriver/inc/teek_client_constants.h b/trustzone-awared-vm/VM/vtzdriver/inc/teek_client_constants.h index 6b0b32ac0854a06bc1825538d5f4710ccd4e8ce7..b3f97c2be4bf49cc3c8e4514c5f278fa4eb7f48c 100644 --- a/trustzone-awared-vm/VM/vtzdriver/inc/teek_client_constants.h +++ b/trustzone-awared-vm/VM/vtzdriver/inc/teek_client_constants.h @@ -146,6 +146,7 @@ enum TEEC_ParamType { TEEC_ION_INPUT = 0x08, TEEC_ION_SGLIST_INPUT = 0x09, TEEC_MEMREF_SHARED_INOUT = 0x0a, + TEEC_MEMREF_REGISTER_INOUT = 0x0b, TEEC_MEMREF_WHOLE = 0xc, TEEC_MEMREF_PARTIAL_INPUT = 0xd, TEEC_MEMREF_PARTIAL_OUTPUT = 0xe, @@ -163,6 +164,7 @@ enum TEE_ParamType { TEE_PARAM_TYPE_ION_INPUT = 0x8, TEE_PARAM_TYPE_ION_SGLIST_INPUT = 0x9, TEE_PARAM_TYPE_MEMREF_SHARED_INOUT = 0x0a, + TEE_PARAM_TYPE_MEMREF_REGISTER_INOUT = 0x0b, TEE_PARAM_TYPE_RESMEM_INPUT = 0xc, TEE_PARAM_TYPE_RESMEM_OUTPUT = 0xd, TEE_PARAM_TYPE_RESMEM_INOUT = 0xe diff --git a/trustzone-awared-vm/VM/vtzdriver/vtzf.c b/trustzone-awared-vm/VM/vtzdriver/vtzf.c index 6ffb1b2d9f221636ffc36fde1c61692eee86c465..33719dab4e3d9f39965b2ca1e9d95f2f2ad9ef15 100644 --- a/trustzone-awared-vm/VM/vtzdriver/vtzf.c +++ b/trustzone-awared-vm/VM/vtzdriver/vtzf.c @@ -688,18 +688,28 @@ static long tc_client_ioctl(struct file *file, unsigned int cmd, unsigned long a tlogd("tc client ioctl ret = 0x%x\n", ret); return (long)ret; } - +static int alloc_for_params_sess(struct vtzf_dev_file *dev_file, + struct_packet_cmd_session *packet_cmd, uintptr_t addrs[][3]); +static void update_free_params_sess(struct tc_ns_client_context *clicontext, + struct tc_ns_client_context *context, uintptr_t addrs[4][3]); +static void free_for_params(struct tc_ns_client_context *clicontext, + uintptr_t addrs[4][3]); + static int tc_ns_open_session(struct vtzf_dev_file *dev_file, struct tc_ns_client_context *clicontext) { - int ret; + int ret = -EINVAL; + int i = 0; + uint32_t offset = 0; + uint32_t total_buf_size = 0; + void *cmd_buf = NULL; uint32_t seq_num = get_seq_num(0); struct_packet_cmd_session packet_cmd = {0}; struct_packet_rsp_session packet_rsp = {0}; size_t file_size = 0; char *buffer = NULL; char *tmp_buffer = NULL; - + uintptr_t addrs[4][3]; if (!clicontext || !dev_file || dev_file->ptzfd <= 0) { tloge("invalid params\n"); return -EINVAL; @@ -730,20 +740,74 @@ static int tc_ns_open_session(struct vtzf_dev_file *dev_file, goto END; } - ret = send_to_proxy(&packet_cmd, sizeof(packet_cmd), &packet_rsp, sizeof(packet_rsp), seq_num); + ret = alloc_for_params_sess(dev_file, &packet_cmd, addrs); + if (ret) { + tloge("alloc for params failed \n"); + return ret; + } + for (i = 0;i < TEE_PARAM_NUM; i++) { + total_buf_size += packet_cmd.block_size[i]; + } + packet_cmd.fragment_block_num = total_buf_size / sizeof(struct page_block); + total_buf_size += sizeof(packet_cmd); + packet_cmd.packet_size = total_buf_size; + cmd_buf = kzalloc(total_buf_size, GFP_KERNEL); + if (!cmd_buf) { + tloge("cmd_buf malloc failed\n"); + ret = -ENOMEM; + goto err2; + } + + if (memcpy_s(cmd_buf, sizeof(packet_cmd), &packet_cmd, sizeof(packet_cmd)) != 0) { + ret = -EFAULT; + goto err1; + } + offset = sizeof(packet_cmd); + + for (i = 0; i < TEE_PARAM_NUM; i++) { + if (packet_cmd.block_size[i] != 0 && + memcpy_s(cmd_buf + offset, packet_cmd.block_size[i], + (void *)packet_cmd.block_addrs[i], packet_cmd.block_size[i]) != 0) { + ret = -EFAULT; + goto err1; + } + offset += packet_cmd.block_size[i]; + } + + ret = send_to_proxy(cmd_buf, total_buf_size, &packet_rsp, sizeof(packet_rsp), seq_num); if (!ret) { ret = packet_rsp.ret; tlogd(" opensession ret =%d \n", ret); if (!ret) { packet_rsp.cliContext.file_buffer = tmp_buffer; + update_free_params_sess(&packet_rsp.cliContext, clicontext, addrs); *clicontext = packet_rsp.cliContext; } else { tloge("open session failed ret is %d\n", ret); clicontext->returns = packet_rsp.cliContext.returns; + free_for_params(&packet_cmd.cliContext, addrs); } } else { tloge("send to proxy failed ret is %d\n", ret); + free_for_params(&packet_cmd.cliContext, addrs); } + kfree(cmd_buf); + for (i = 0; i < TEE_PARAM_NUM; i++) { + if (packet_cmd.block_size[i] != 0 && packet_cmd.block_addrs[i]) { + kfree((void *)packet_cmd.block_addrs[i]); + } + } + dealloc_res_shm(buffer); + return ret; +err1: + kfree(cmd_buf); + for (i = 0; i < TEE_PARAM_NUM; i++) { + if (packet_cmd.block_size[i] != 0 && packet_cmd.block_addrs[i]) { + kfree((void *)packet_cmd.block_addrs[i]); + } + } +err2: + free_for_params(&packet_cmd.cliContext, addrs); END: dealloc_res_shm(buffer); return ret; @@ -893,6 +957,54 @@ static int alloc_for_ref_mem(struct vtzf_dev_file *dev_file, return 0; } +static int alloc_for_ref_mem_sess(struct vtzf_dev_file *dev_file, + struct_packet_cmd_session *packet_cmd, int index, uintptr_t addrs[][3]) +{ + uintptr_t user_size_addr; + struct tc_ns_client_context *clicontext = &packet_cmd->cliContext; + bool b_found = false; + struct vtzf_shared_mem *shared_mem = NULL; + struct vtzf_shared_mem *shared_mem_temp = NULL; + void *user_buffer = NULL; + uintptr_t phy_buffer; + uint32_t buf_size; + + user_size_addr = (uintptr_t)(clicontext->params[index].memref.size_addr + | (uint64_t)clicontext->params[index].memref.size_h_addr << ADDR_TRANS_NUM); + user_buffer = (void *)(clicontext->params[index].memref.buffer + | (uint64_t)clicontext->params[index].memref.buffer_h_addr << ADDR_TRANS_NUM); + + if (copy_from_user(&buf_size, (void *)user_size_addr, sizeof(uint32_t)) != 0) { + tloge("copy from user failed\n"); + return -EFAULT; + } + + mutex_lock(&dev_file->shared_mem_lock); + list_for_each_entry_safe(shared_mem, shared_mem_temp, &dev_file->shared_mem_list, head) { + if (shared_mem) { + if (shared_mem->user_addr == user_buffer) { + tlogv("found the mapped shared_mem for cliContext.params[index].memref.buffer\n"); + phy_buffer = (uintptr_t)shared_mem->phy_addr; + clicontext->params[index].memref.buffer = + (unsigned int)(uintptr_t)phy_buffer; + clicontext->params[index].memref.buffer_h_addr = + ((unsigned long long)(uintptr_t)phy_buffer) >> ADDR_TRANS_NUM; + + packet_cmd->addrs[index] = (unsigned long long)user_buffer; + b_found = true; + break; + } + } + } + mutex_unlock(&dev_file->shared_mem_lock); + if (!b_found) { + tloge("can't found the mapped shared_mem for cliContext.params[index].memref.buffer \n"); + return -EFAULT; + } + clicontext->params[index].memref.size_addr = buf_size; + return 0; +} + static int check_buffer_for_sharedmem(uint32_t *buffer_size, struct_packet_cmd_send_cmd *packet_cmd, int index) { @@ -922,6 +1034,35 @@ static int check_buffer_for_sharedmem(uint32_t *buffer_size, return 0; } +static int check_buffer_for_sharedmem_sess(uint32_t *buffer_size, + struct_packet_cmd_session *packet_cmd, int index) +{ + uintptr_t user_size_addr; + void *user_buffer = NULL; + struct tc_ns_client_context *clicontext = &packet_cmd->cliContext; + user_size_addr = (uintptr_t)(clicontext->params[index].memref.size_addr + | (uint64_t)clicontext->params[index].memref.size_h_addr << ADDR_TRANS_NUM); + user_buffer = (void *)(clicontext->params[index].memref.buffer + | (uint64_t)clicontext->params[index].memref.buffer_h_addr << ADDR_TRANS_NUM); + + if (copy_from_user(buffer_size, (void *)user_size_addr, sizeof(uint32_t)) != 0) { + tloge("copy from user failed\n"); + return -EFAULT; + } + + if (*buffer_size == 0 || *buffer_size > SZ_256M) { + tloge("invalid buffer size\n"); + return -ENOMEM; + } + + if ((packet_cmd->cliContext.params[index].memref.offset >= SZ_256M) || + (UINT64_MAX - (uint64_t)user_buffer <= packet_cmd->cliContext.params[index].memref.offset)) { + tloge("invalid buff or offset\n"); + return -EFAULT; + } + return 0; +} + static int alloc_for_share_mem(struct vtzf_dev_file *dev_file, struct_packet_cmd_send_cmd *packet_cmd, int index, uintptr_t addrs[][3]) { @@ -964,6 +1105,48 @@ static int alloc_for_share_mem(struct vtzf_dev_file *dev_file, return 0; } +static int alloc_for_share_mem_sess(struct vtzf_dev_file *dev_file, + struct_packet_cmd_session *packet_cmd, int index, uintptr_t addrs[][3]) +{ + struct tc_ns_client_context *clicontext = &packet_cmd->cliContext; + void *user_buffer = NULL; + uint32_t user_buf_size = 0; + void *block_buf = NULL; + uint32_t block_buf_size = 0; + void *pages_buf = NULL; + uint32_t pages_buf_size = 0; + int block_count; + uint32_t offset; + + user_buffer = (void *)(clicontext->params[index].memref.buffer + | (uint64_t)clicontext->params[index].memref.buffer_h_addr << ADDR_TRANS_NUM); + tlogd(" user_buffer = %p \n", user_buffer); + if (check_buffer_for_sharedmem_sess(&user_buf_size, packet_cmd, index)) + return -EINVAL; + + tlogd("share mem buf size = %u\n", user_buf_size); + if (get_page_block(user_buffer, user_buf_size, &block_buf, &block_buf_size, &block_count, &pages_buf, &pages_buf_size) != 0) { + tloge("get_page_block failed \n"); + return -EFAULT; + } + tlogd("alloc for share mem \n"); + tlogd("block_buf = %llx \n", (uint64_t)block_buf); + tlogd("block_buf_size = %u \n", block_buf_size); + //dump_page_blocks(block_count, (uint64_t)block_buf); + addrs[index][1] = (uintptr_t)pages_buf; + addrs[index][0] = (uintptr_t)pages_buf_size; + packet_cmd->block_addrs[index] = (uint64_t)block_buf; + packet_cmd->block_size[index] = block_buf_size; + packet_cmd->vm_page_size = PAGE_SIZE; + clicontext->params[index].memref.size_addr = user_buf_size; + offset = ((uint32_t)(uintptr_t)user_buffer) & (~PAGE_MASK); + /*memref.h_offset 保存首个PAGE内部的偏移, memref.offset用户buffer的偏移*/ + clicontext->params[index].memref.h_offset = offset; + tlogd("clicontext->params[index].memref.h_offset = %u, 0x%x\n", + clicontext->params[index].memref.h_offset, clicontext->params[index].memref.h_offset); + return 0; +} + static int alloc_for_params(struct vtzf_dev_file *dev_file, struct_packet_cmd_send_cmd *packet_cmd, uintptr_t addrs[][3]) { @@ -981,7 +1164,8 @@ static int alloc_for_params(struct vtzf_dev_file *dev_file, ret = alloc_for_ref_mem(dev_file , packet_cmd, index, addrs); else if (teec_value_type(param_type, INOUT) || checkValue) ret = alloc_for_val_mem(&packet_cmd->cliContext, index, addrs); - else if (param_type == TEEC_MEMREF_SHARED_INOUT) + else if (param_type == TEEC_MEMREF_SHARED_INOUT || + param_type == TEEC_MEMREF_REGISTER_INOUT) ret = alloc_for_share_mem(dev_file , packet_cmd, index, addrs); else tlogd("param type = TEEC_NONE\n"); @@ -996,6 +1180,39 @@ ERR: } +static int alloc_for_params_sess(struct vtzf_dev_file *dev_file, + struct_packet_cmd_session *packet_cmd, uintptr_t addrs[][3]) +{ + int ret; + int index; + uint32_t param_type; + bool checkValue; + for (index = 0; index < TEE_PARAM_NUM; index++) { + param_type = teec_param_type_get(packet_cmd->cliContext.param_types, index); + checkValue = (param_type == TEEC_ION_INPUT || param_type == TEEC_ION_SGLIST_INPUT); + tlogd("param %u type is %x\n", index, param_type); + if (teec_tmpmem_type(param_type, INOUT)) + ret = alloc_for_tmp_mem(&packet_cmd->cliContext, index, addrs); + else if (teec_memref_type(param_type, INOUT)) + ret = alloc_for_ref_mem_sess(dev_file , packet_cmd, index, addrs); + else if (teec_value_type(param_type, INOUT) || checkValue) + ret = alloc_for_val_mem(&packet_cmd->cliContext, index, addrs); + else if (param_type == TEEC_MEMREF_SHARED_INOUT || + param_type == TEEC_MEMREF_REGISTER_INOUT) + ret = alloc_for_share_mem_sess(dev_file , packet_cmd, index, addrs); + else + tlogd("param type = TEEC_NONE\n"); + if (ret != 0) { + goto ERR; + } + } + + return 0; +ERR: + return ret; + +} + static void update_free_params(struct tc_ns_client_context *clicontext, struct tc_ns_client_context *context, uintptr_t addrs[4][3]) { @@ -1048,7 +1265,75 @@ static void update_free_params(struct tc_ns_client_context *clicontext, ret = -EFAULT; if (copy_to_user((void *)user_addr_val_b, &val_b, sizeof(uint32_t)) != 0) ret = -EFAULT; - } else if (param_type == TEEC_MEMREF_SHARED_INOUT){ + } else if (param_type == TEEC_MEMREF_SHARED_INOUT || + param_type == TEEC_MEMREF_REGISTER_INOUT){ + pages_buf = (void *)addrs[index][1]; + pages_buf_size = (uint32_t)addrs[index][0]; + release_shared_mem_page((uint64_t)pages_buf, pages_buf_size); + } else { + /* nothing */ + } + + if (ret) { + tloge(" ret =%d \n", ret); + } + } +} + +static void update_free_params_sess(struct tc_ns_client_context *clicontext, + struct tc_ns_client_context *context, uintptr_t addrs[4][3]) +{ + int ret = 0; + int index; + uint32_t param_type; + bool checkValue; + uintptr_t buf; + uintptr_t user_addr_size, user_addr_buf; + uintptr_t user_addr_val_a, user_addr_val_b; + uint32_t buf_size; + uint32_t val_a, val_b; + void *pages_buf = NULL; + uint32_t pages_buf_size = 0; + for (index = 0; index < 2; index++) { + param_type = teec_param_type_get(clicontext->param_types, index); + checkValue = (param_type == TEEC_ION_INPUT || param_type == TEEC_ION_SGLIST_INPUT); + if (teec_tmpmem_type(param_type, INOUT)) { + buf_size = clicontext->params[index].memref.size_addr; + buf = addrs[index][1]; + + user_addr_size = (uintptr_t)(context->params[index].memref.size_addr + | (uint64_t)context->params[index].memref.size_h_addr << ADDR_TRANS_NUM); + user_addr_buf = (uintptr_t)(context->params[index].memref.buffer + | (uint64_t)context->params[index].memref.buffer_h_addr << ADDR_TRANS_NUM); + + if (copy_to_user((void *)user_addr_size, &buf_size, sizeof(uint32_t)) != 0) + ret = -EFAULT; + if (copy_to_user((void *)user_addr_buf, (void *)buf, buf_size) != 0) + ret = -EFAULT; + dealloc_res_shm((void *)buf); + + } else if(teec_memref_type(param_type, INOUT)) { + buf_size = clicontext->params[index].memref.size_addr; + + user_addr_size = (uintptr_t)(context->params[index].memref.size_addr + | (uint64_t)context->params[index].memref.size_h_addr << ADDR_TRANS_NUM); + if (copy_to_user((void *)user_addr_size, &buf_size, sizeof(uint32_t)) != 0) + ret = -EFAULT; + } else if(teec_value_type(param_type, INOUT) || checkValue) { + val_a = clicontext->params[index].value.a_addr; + val_b = clicontext->params[index].value.b_addr; + + user_addr_val_a = (uintptr_t)(context->params[index].value.a_addr + | (uint64_t)context->params[index].value.a_h_addr << ADDR_TRANS_NUM); + user_addr_val_b = (uintptr_t)(context->params[index].value.b_addr + | (uint64_t)context->params[index].value.b_h_addr << ADDR_TRANS_NUM); + + if (copy_to_user((void *)user_addr_val_a, &val_a, sizeof(uint32_t)) != 0) + ret = -EFAULT; + if (copy_to_user((void *)user_addr_val_b, &val_b, sizeof(uint32_t)) != 0) + ret = -EFAULT; + } else if (param_type == TEEC_MEMREF_SHARED_INOUT || + param_type == TEEC_MEMREF_REGISTER_INOUT){ pages_buf = (void *)addrs[index][1]; pages_buf_size = (uint32_t)addrs[index][0]; release_shared_mem_page((uint64_t)pages_buf, pages_buf_size); @@ -1076,7 +1361,8 @@ static void free_for_params(struct tc_ns_client_context *clicontext, if (teec_tmpmem_type(param_type, INOUT) && addrs[index][1]) { buf = addrs[index][1]; dealloc_res_shm((void *)buf); - }else if (param_type == TEEC_MEMREF_SHARED_INOUT){ + }else if (param_type == TEEC_MEMREF_SHARED_INOUT || + param_type == TEEC_MEMREF_REGISTER_INOUT){ pages_buf = (void *)addrs[index][1]; pages_buf_size = (uint32_t)addrs[index][0]; release_shared_mem_page((uint64_t)pages_buf, pages_buf_size); @@ -1091,9 +1377,9 @@ static int tc_ns_send_cmd(struct vtzf_dev_file *dev_file, { int ret = -EINVAL; int i = 0; - uint32_t offset =0; + uint32_t offset = 0; uint32_t total_buf_size = 0; - void *cmd_buf =NULL; + void *cmd_buf = NULL; uint32_t seq_num = get_seq_num(0); struct_packet_cmd_send_cmd packet_cmd = {0}; struct_packet_rsp_send_cmd packet_rsp = {0};