diff --git a/trustzone-awared-vm/Host/itrustee_tzdriver-v1.6.2.patch b/trustzone-awared-vm/Host/itrustee_tzdriver-v1.6.2.patch new file mode 100644 index 0000000000000000000000000000000000000000..286a770f826e6c6ff37a34bcb17d3504a4cba0c3 --- /dev/null +++ b/trustzone-awared-vm/Host/itrustee_tzdriver-v1.6.2.patch @@ -0,0 +1,1267 @@ +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 +@@ -336,11 +336,25 @@ int check_teecd_auth(void) + + +return CHECK_ACCESS_SUCC; + } ++int check_proxy_auth(void) ++{ ++ int ret = check_proc_uid_path(PROXY_PATH_UID_AUTH_CTX); ++ if (ret != 0) { ++ return ret; ++ } ++ ++ return CHECK_ACCESS_SUCC; ++} + #else + int check_teecd_auth(void) + { + return 0; + } ++ ++int check_proxy_auth(void) ++{ ++ return 0; ++} + #endif + + +#ifdef CONFIG_TEE_TELEPORT_AUTH +diff -uprN itrustee_tzdriver/auth/auth_base_impl.h itrustee_tzdriver_new/auth/auth_base_impl.h +--- itrustee_tzdriver/auth/auth_base_impl.h 2024-07-12 11:08:59.353629380 +0800 ++++ itrustee_tzdriver_new/auth/auth_base_impl.h 2024-07-12 10:51:32.885629380 +0800 +@@ -84,6 +84,7 @@ void mutex_crypto_hash_lock(void); + void mutex_crypto_hash_unlock(void); + int check_hidl_auth(void); + int check_teecd_auth(void); ++int check_proxy_auth(void); + #else + + +static inline void free_shash_handle(void) +@@ -100,6 +101,11 @@ int check_teecd_auth(void) + { + return 0; + } ++ ++int check_proxy_auth(void) ++{ ++ return 0; ++} + + +#endif /* CLIENT_AUTH || TEECD_AUTH */ + + +diff -uprN itrustee_tzdriver/core/agent.c itrustee_tzdriver_new/core/agent.c +--- itrustee_tzdriver/core/agent.c 2024-07-12 11:08:59.357629380 +0800 ++++ itrustee_tzdriver_new/core/agent.c 2024-07-12 10:51:32.865629380 +0800 +@@ -298,7 +298,8 @@ int tc_ns_set_native_hash(unsigned long + return ret; + } + + +-int tc_ns_late_init(unsigned long arg) ++int tc_ns_late_init(const struct tc_ns_dev_file *dev_file, ++ unsigned long arg) + { + int ret = 0; + struct tc_ns_smc_cmd smc_cmd = { {0}, 0 }; +@@ -320,6 +321,8 @@ int tc_ns_late_init(unsigned long arg) + smc_cmd.operation_h_phys = + (uint64_t)mailbox_virt_to_phys((uintptr_t)&mb_pack->operation) >> ADDR_TRANS_NUM; + + ++ if (dev_file->isVM) ++ smc_cmd.nsid = dev_file->nsid; + if (tc_ns_smc(&smc_cmd)) { + ret = -EPERM; + tloge("late int failed\n"); +@@ -597,7 +600,8 @@ int tc_ns_wait_event(unsigned int agent_ + return ret; + } + + +-int tc_ns_sync_sys_time(const struct tc_ns_client_time *tc_ns_time) ++int tc_ns_sync_sys_time(const struct tc_ns_dev_file *dev_file, ++ const struct tc_ns_client_time *tc_ns_time) + { + struct tc_ns_smc_cmd smc_cmd = { {0}, 0 }; + int ret = 0; +@@ -623,6 +627,8 @@ int tc_ns_sync_sys_time(const struct tc_ + smc_cmd.operation_phys = mailbox_virt_to_phys((uintptr_t)&mb_pack->operation); + smc_cmd.operation_h_phys = + (uint64_t)mailbox_virt_to_phys((uintptr_t)&mb_pack->operation) >> ADDR_TRANS_NUM; ++ if (dev_file && dev_file->isVM) ++ smc_cmd.nsid = dev_file->nsid; + if (tc_ns_smc(&smc_cmd)) { + tloge("tee adjust time failed, return error\n"); + ret = -EPERM; +@@ -632,7 +638,8 @@ int tc_ns_sync_sys_time(const struct tc_ + return ret; + } + + +-int sync_system_time_from_user(const struct tc_ns_client_time *user_time) ++int sync_system_time_from_user(const struct tc_ns_dev_file *dev_file, ++ const struct tc_ns_client_time *user_time) + { + int ret = 0; + struct tc_ns_client_time time = { 0 }; +@@ -647,7 +654,7 @@ int sync_system_time_from_user(const str + return -EFAULT; + } + + +- ret = tc_ns_sync_sys_time(&time); ++ ret = tc_ns_sync_sys_time(dev_file, &time); + if (ret != 0) + tloge("sync system time from user failed, ret = 0x%x\n", ret); + + +@@ -665,7 +672,7 @@ void sync_system_time_from_kernel(void) + time.seconds = (uint32_t)kernel_time.ts.tv_sec; + time.millis = (uint32_t)(kernel_time.ts.tv_nsec / MS_TO_NS); + + +- ret = tc_ns_sync_sys_time(&time); ++ ret = tc_ns_sync_sys_time(NULL, &time); + if (ret != 0) + tloge("sync system time from kernel failed, ret = 0x%x\n", ret); + + +@@ -949,6 +956,8 @@ int tc_ns_register_agent(struct tc_ns_de + nsid = task_active_pid_ns(current)->ns.inum; + if (dev_file != NULL && dev_file->nsid == 0) + dev_file->nsid = nsid; ++ if (dev_file->isVM) ++ nsid = dev_file->nsid; + #endif + + +if (is_agent_already_exist(agent_id, nsid, &event_data, dev_file, &find_flag)) +diff -uprN itrustee_tzdriver/core/agent.h itrustee_tzdriver_new/core/agent.h +--- itrustee_tzdriver/core/agent.h 2024-07-12 11:08:59.357629380 +0800 ++++ itrustee_tzdriver_new/core/agent.h 2024-07-12 10:51:32.861629380 +0800 +@@ -118,7 +118,8 @@ int agent_process_work(const struct tc_n + unsigned int agent_id, unsigned int nsid); + int is_agent_alive(unsigned int agent_id, unsigned int nsid); + int tc_ns_set_native_hash(unsigned long arg, unsigned int cmd_id); +-int tc_ns_late_init(unsigned long arg); ++int tc_ns_late_init(const struct tc_ns_dev_file *dev_file, ++ unsigned long arg); + int tc_ns_register_agent(struct tc_ns_dev_file *dev_file, unsigned int agent_id, + unsigned int buffer_size, void **buffer, bool user_agent); + int tc_ns_unregister_agent(unsigned int agent_id, unsigned int nsid); +@@ -126,7 +127,8 @@ void send_crashed_event_response_all(con + int tc_ns_wait_event(unsigned int agent_id, unsigned int nsid); + int tc_ns_send_event_response(unsigned int agent_id, unsigned int nsid); + void send_crashed_event_response_single(const struct tc_ns_dev_file *dev_file); +-int sync_system_time_from_user(const struct tc_ns_client_time *user_time); ++int sync_system_time_from_user(const struct tc_ns_dev_file *dev_file, ++ const struct tc_ns_client_time *user_time); + void sync_system_time_from_kernel(void); + int tee_agent_clear_work(struct tc_ns_client_context *context, + unsigned int dev_file_id); +diff -uprN itrustee_tzdriver/core/gp_ops.c itrustee_tzdriver_new/core/gp_ops.c +--- itrustee_tzdriver/core/gp_ops.c 2024-07-12 11:08:59.357629380 +0800 ++++ itrustee_tzdriver_new/core/gp_ops.c 2024-07-12 10:51:32.845629380 +0800 +@@ -231,6 +231,84 @@ int write_to_client(void __user *dest, s + return 0; + } + + ++int read_from_VMclient(void *dest, size_t dest_size, ++ const void __user *src, size_t size, pid_t vm_pid) ++{ ++ struct task_struct *vmp_task; ++ int i_rdlen; ++ int i_index; ++ int ret; ++ ++ if (!dest || !src) { ++ tloge("src or dest is NULL input buffer\n"); ++ return -EINVAL; ++ } ++ ++ if (size > dest_size) { ++ tloge("size is larger than dest_size or size is 0\n"); ++ return -EINVAL; ++ } ++ if (!size) ++ return 0; ++ ++ tlogv("django verbose, execute access_process_vm"); ++ vmp_task = get_pid_task(find_get_pid(vm_pid), PIDTYPE_PID); ++ if (vmp_task == NULL) { ++ tloge("no task for pid %d \n", vm_pid); ++ return -EFAULT; ++ } ++ tlogv("django verbose, task_struct * for pid %d is 0x%px", vm_pid, vmp_task); ++ ++ i_rdlen = access_process_vm(vmp_task, (unsigned long)(src), dest, size, FOLL_FORCE); ++ if (i_rdlen != size) { ++ tloge("only read %d of %ld bytes by access_process_vm \n", i_rdlen, size); ++ return -EFAULT; ++ } ++ tlogv("django verbose, read %d byes by access_process_vm succeed", ++ i_rdlen); ++ for (i_index = 0; i_index < 32 && i_index < size; i_index ++) { ++ tlogv("django verbose, *(dest + i_index) + %d) = %2.2x", ++ i_index, *((char*)dest + i_index)); ++ } ++ return 0; ++} ++ ++int write_to_VMclient(void __user *dest, size_t dest_size, ++ const void *src, size_t size, pid_t vm_pid) ++{ ++ struct task_struct *vmp_task; ++ int i_wtlen; ++ int i_index; ++ int ret; ++ ++ if (!dest || !src) { ++ tloge("src or dest is NULL input buffer\n"); ++ return -EINVAL; ++ } ++ ++ if (size > dest_size) { ++ tloge("size is larger than dest_size or size is 0\n"); ++ return -EINVAL; ++ } ++ if (!size) ++ return 0; ++ ++ vmp_task = get_pid_task(find_get_pid(vm_pid), PIDTYPE_PID); ++ if (vmp_task == NULL) { ++ tloge("no task for pid %d \n", vm_pid); ++ return -EFAULT; ++ } ++ ++ i_wtlen = access_process_vm(vmp_task, (unsigned long)(dest), src, size, FOLL_FORCE | FOLL_WRITE); ++ if (i_wtlen != size) { ++ tloge("only write %d of %ld bytes by access_process_vm \n", i_wtlen, size); ++ return -EFAULT; ++ } ++ tlogv("django verbose, write %d byes by access_process_vm succeed", ++ i_wtlen); ++ return 0; ++} ++ + static bool is_input_tempmem(unsigned int param_type) + { + if (param_type == TEEC_MEMREF_TEMP_INPUT || +@@ -240,7 +318,8 @@ static bool is_input_tempmem(unsigned in + return false; + } + + +-static int update_input_data(const union tc_ns_client_param *client_param, ++static int update_input_data(const struct tc_call_params *call_params, ++ const union tc_ns_client_param *client_param, + uint32_t buffer_size, void *temp_buf, + unsigned int param_type, uint8_t kernel_params) + { +@@ -250,11 +329,22 @@ static int update_input_data(const union + + +buffer_addr = client_param->memref.buffer | + ((uint64_t)client_param->memref.buffer_h_addr << ADDR_TRANS_NUM); +- if (read_from_client(temp_buf, buffer_size, +- (void *)(uintptr_t)buffer_addr, +- buffer_size, kernel_params) != 0) { +- tloge("copy memref buffer failed\n"); +- return -EFAULT; ++ if (call_params->dev->isVM && !kernel_params) { ++ tlogd("is VM\n"); ++ if (read_from_VMclient(temp_buf, buffer_size, ++ (void *)(uintptr_t)buffer_addr, ++ buffer_size, call_params->dev->vmpid) != 0) { ++ tloge("copy memref buffer failed\n"); ++ return -EFAULT; ++ } ++ } else { ++ tlogd("is not VM\n"); ++ if (read_from_client(temp_buf, buffer_size, ++ (void *)(uintptr_t)buffer_addr, ++ buffer_size, kernel_params) != 0) { ++ tloge("copy memref buffer failed\n"); ++ return -EFAULT; ++ } + } + return 0; + } +@@ -312,7 +402,7 @@ static int alloc_for_tmp_mem(const struc + op_params->local_tmpbuf[index].temp_buffer = temp_buf; + op_params->local_tmpbuf[index].size = buffer_size; + + +- if (update_input_data(client_param, buffer_size, temp_buf, ++ if (update_input_data(call_params, client_param, buffer_size, temp_buf, + param_type, kernel_params) != 0) + return -EFAULT; + + +@@ -324,8 +414,9 @@ static int alloc_for_tmp_mem(const struc + return 0; + } + + +-static int check_buffer_for_ref(uint32_t *buffer_size, +- const union tc_ns_client_param *client_param, uint8_t kernel_params) ++static int check_buffer_for_ref(const struct tc_call_params *call_params, ++ uint32_t *buffer_size, const union tc_ns_client_param *client_param, ++ uint8_t kernel_params) + { + uint64_t size_addr = client_param->memref.size_addr | + ((uint64_t)client_param->memref.size_h_addr << ADDR_TRANS_NUM); +@@ -416,7 +507,7 @@ static int alloc_for_ref_mem(const struc + return -EINVAL; + + +client_param = &(call_params->context->params[index]); +- if (check_buffer_for_ref(&buffer_size, client_param, kernel_params) != 0) ++ if (check_buffer_for_ref(call_params, &buffer_size, client_param, kernel_params) != 0) + return -EINVAL; + + +op_params->mb_pack->operation.params[index].memref.buffer = 0; +@@ -491,6 +582,134 @@ static int check_buffer_for_sharedmem(ui + return 0; + } + + ++typedef union { ++ struct{ ++ uint64_t user_addr; ++ uint64_t page_num; ++ }block; ++ struct{ ++ uint64_t vm_page_size; ++ uint64_t shared_mem_size; ++ }share; ++}struct_page_block; ++ ++int fill_vm_shared_mem_info_block(uint64_t block_buf, uint32_t block_nums, ++ uint32_t offset, uint32_t buffer_size, uint64_t info_addr, uint32_t vm_page_size,pid_t vm_pid) ++{ ++ struct pagelist_info *page_info = NULL; ++ struct page **host_pages = NULL; ++ uint64_t *phys_addr = NULL; ++ uint32_t host_page_num; ++ uint32_t i; ++ uint32_t j; ++ uint32_t k; ++ uint32_t block_page_total_no = 0; ++ struct task_struct *vmp_task; ++ uint32_t vm_pages_no = 0; ++ uint32_t host_pages_no = 0; ++ uint32_t host_offset = 0; ++ uint64_t vm_start_vaddr; ++ void *host_start_vaddr; ++ uint32_t page_total_no = 0; ++ uint32_t vm_pages_total_size = 0; ++ vmp_task = get_pid_task(find_get_pid(vm_pid), PIDTYPE_PID); ++ if (vmp_task == NULL) { ++ tloge("no task for pid %d", vm_pid); ++ return -EFAULT; ++ } ++ uint32_t expect_page_num = PAGE_ALIGN(buffer_size + (offset & (~PAGE_MASK))) / PAGE_SIZE; ++ struct_page_block *page_block = (struct_page_block *)(uintptr_t)block_buf; ++ for (i = 0; i < block_nums; i++){ ++ vm_start_vaddr = page_block[i].block.user_addr; ++ vm_pages_no = page_block[i].block.page_num; ++ ++ if (i==0 && vm_page_size > PAGE_SIZE) { ++ vm_start_vaddr += (offset & PAGE_MASK); ++ vm_pages_total_size = vm_pages_no * vm_page_size - (offset & PAGE_MASK); ++ } else { ++ vm_pages_total_size = vm_pages_no * vm_page_size; ++ } ++ ++ host_offset = ((uint32_t)(uintptr_t)vm_start_vaddr) & (~PAGE_MASK); ++ host_start_vaddr = (void *)(((uint64_t)vm_start_vaddr) & PAGE_MASK); ++ host_pages_no = PAGE_ALIGN(host_offset + vm_pages_total_size) / PAGE_SIZE; ++ if (i== block_nums -1 && vm_page_size > PAGE_SIZE) ++ host_pages_no = expect_page_num - page_total_no; ++ ++ host_pages = (struct page **)vmalloc(host_pages_no * sizeof(uint64_t)); ++ if (host_pages == NULL) ++ return -EFAULT; ++ tlogd("page_block[%u].block.user_addr = %llx, page_block[%u].block.page_num = %llx\n", i, vm_start_vaddr, i, vm_pages_no); ++ ++ #if (KERNEL_VERSION(6, 5, 0) <= LINUX_VERSION_CODE) ++ host_page_num = get_user_pages_remote(vmp_task->mm, host_start_vaddr, ++ (unsigned long)host_pages_no, ++ FOLL_FORCE, host_pages, NULL); ++ #elif (KERNEL_VERSION(5, 9, 0) <= LINUX_VERSION_CODE) ++ host_page_num = get_user_pages_remote(vmp_task->mm, host_start_vaddr, ++ (unsigned long)host_pages_no, ++ FOLL_FORCE, host_pages, ++ NULL, NULL); ++ #elif (KERNEL_VERSION(4, 10, 0) <= LINUX_VERSION_CODE) ++ host_page_num = get_user_pages_remote(vmp_task, vmp_task->mm, ++ host_start_vaddr, (unsigned long)host_pages_no, FOLL_FORCE, ++ host_pages, NULL, NULL); ++ #elif (KERNEL_VERSION(4, 9, 0) <= LINUX_VERSION_CODE) ++ host_page_num = get_user_pages_remote(vmp_task, vmp_task->mm, ++ host_start_vaddr, (unsigned long)host_pages_no, ++ FOLL_FORCE, host_pages, NULL); ++ #else ++ host_page_num = get_user_pages_remote(vmp_task, vmp_task->mm, ++ host_start_vaddr, (unsigned long)host_pages_no, ++ 1, 1, host_pages, NULL); ++ #endif ++ if (host_page_num != host_pages_no) { ++ tloge("get pages failed, page_num = %u, expect %u\n", host_page_num, host_pages_no); ++ if (host_page_num > 0) { ++ release_pages(host_pages, host_page_num); ++ } ++ vfree(host_pages); ++ return -EFAULT; ++ } ++ ++ phys_addr = (uint64_t *)(uintptr_t)info_addr + (sizeof(*page_info) / sizeof(uint64_t)); ++ phys_addr = (uint64_t *)((char *)phys_addr + page_total_no * sizeof(uint64_t)); ++ block_page_total_no = 0; ++ for (j = 0; j < host_pages_no; j++) { ++ struct page *page = NULL; ++ page = host_pages[j]; ++ if (page == NULL) { ++ release_pages(host_pages, host_page_num); ++ vfree(host_pages); ++ tloge("page == NULL \n"); ++ return -EFAULT; ++ } ++ void *host_page_phy = (uintptr_t)page_to_phys(page); ++ if (vm_page_size < PAGE_SIZE) { ++ if (j !=0) ++ host_offset = 0; ++ uint32_t litil_page_num = (PAGE_SIZE - host_offset) / vm_page_size; ++ uint64_t host_page_start_addr = (uint64_t)host_page_phy + host_offset; ++ for (k = 0; k < litil_page_num && block_page_total_no < vm_pages_no;k++) { ++ phys_addr[block_page_total_no++] = host_page_start_addr + k * vm_page_size; ++ } ++ } else if (vm_page_size >= PAGE_SIZE){ ++ phys_addr[j] = (uintptr_t)page_to_phys(page); ++ } ++ } ++ page_total_no += (vm_page_size >= PAGE_SIZE ? host_pages_no : vm_pages_no); ++ vfree(host_pages); ++ } ++ ++ page_info = (struct pagelist_info *)(uintptr_t)info_addr; ++ page_info->page_num = page_total_no; ++ page_info->page_size = (vm_page_size > PAGE_SIZE ? PAGE_SIZE : vm_page_size); ++ page_info->sharedmem_offset = offset & (~PAGE_MASK); ++ page_info->sharedmem_size = buffer_size; ++ ++ return 0; ++} ++ + static int transfer_shared_mem(const struct tc_call_params *call_params, + struct tc_op_params *op_params, uint8_t kernel_params, + uint32_t param_type, unsigned int index) +@@ -499,10 +718,11 @@ static int transfer_shared_mem(const str + void *start_vaddr = NULL; + union tc_ns_client_param *client_param = NULL; + uint32_t buffer_size; +- uint32_t pages_no; ++ uint32_t pages_no = 0; + uint32_t offset; + uint32_t buff_len; + uint64_t buffer_addr; ++ uint32_t i; + + +if (index >= TEE_PARAM_NUM) + return -EINVAL; +@@ -513,19 +733,57 @@ static int transfer_shared_mem(const str + + +buffer_addr = client_param->memref.buffer | + ((uint64_t)client_param->memref.buffer_h_addr << ADDR_TRANS_NUM); +- buff = (void *)(uint64_t)(buffer_addr + client_param->memref.offset); +- start_vaddr = (void *)(((uint64_t)buff) & PAGE_MASK); +- offset = ((uint32_t)(uintptr_t)buff) & (~PAGE_MASK); +- pages_no = PAGE_ALIGN(offset + buffer_size) / PAGE_SIZE; +- +- buff_len = sizeof(struct pagelist_info) + (sizeof(uint64_t) * pages_no); +- buff = mailbox_alloc(buff_len, MB_FLAG_ZERO); +- if (buff == NULL) +- return -EFAULT; + + +- if (fill_shared_mem_info((uint64_t)start_vaddr, pages_no, offset, buffer_size, (uint64_t)buff)) { +- mailbox_free(buff); +- return -EFAULT; ++ if (call_params->dev->isVM) { ++ uint32_t block_buf_size = buffer_size - sizeof(struct_page_block); ++ void *tmp_buf = kzalloc(buffer_size, GFP_KERNEL); ++ if (read_from_client(tmp_buf, buffer_size, buffer_addr, buffer_size, 0)) { ++ tloge("copy blocks failed\n"); ++ return -EFAULT; ++ } ++ struct_page_block *block_buf = (struct_page_block *)((char *)tmp_buf + sizeof(struct_page_block)); ++ uint32_t block_nums = block_buf_size / sizeof(struct_page_block); ++ uint32_t share_mem_size = ((struct_page_block *)tmp_buf)->share.shared_mem_size; ++ uint32_t vm_page_size = ((struct_page_block *)tmp_buf)->share.vm_page_size; ++ ++ call_params->dev->vm_page_size = vm_page_size; ++ offset = (uint64_t)(client_param->memref.h_offset + client_param->memref.offset); ++ for(i = 0;i < block_nums; i++){ ++ pages_no += block_buf[i].block.page_num; ++ } ++ if (vm_page_size > PAGE_SIZE){ ++ buff = (void *)(uint64_t)(client_param->memref.h_offset + client_param->memref.offset); ++ pages_no = PAGE_ALIGN((((uint32_t)(uintptr_t)buff) & (~PAGE_MASK)) + share_mem_size) / PAGE_SIZE; ++ tlogd("page_no = %u \n", pages_no); ++ } ++ ++ buff_len = sizeof(struct pagelist_info) + (sizeof(uint64_t) * pages_no); ++ buff = mailbox_alloc(buff_len, MB_FLAG_ZERO); ++ if (buff == NULL) { ++ kfree(tmp_buf); ++ return -EFAULT; ++ } ++ if (fill_vm_shared_mem_info_block((uint64_t)block_buf, block_nums, offset, ++ share_mem_size, (uint64_t)buff, vm_page_size, call_params->dev->vmpid)) { ++ kfree(tmp_buf); ++ mailbox_free(buff); ++ return -EFAULT; ++ } ++ kfree(tmp_buf); ++ } else { ++ buff = (void *)(uint64_t)(buffer_addr + client_param->memref.offset); ++ start_vaddr = (void *)(((uint64_t)buff) & PAGE_MASK); ++ offset = ((uint32_t)(uintptr_t)buff) & (~PAGE_MASK); ++ pages_no = PAGE_ALIGN(offset + buffer_size) / PAGE_SIZE; ++ ++ buff_len = sizeof(struct pagelist_info) + (sizeof(uint64_t) * pages_no); ++ buff = mailbox_alloc(buff_len, MB_FLAG_ZERO); ++ if (buff == NULL) ++ return -EFAULT; ++ if (fill_shared_mem_info((uint64_t)start_vaddr, pages_no, offset, buffer_size, (uint64_t)buff)) { ++ mailbox_free(buff); ++ return -EFAULT; ++ } + } + + +op_params->local_tmpbuf[index].temp_buffer = buff; +@@ -695,13 +953,24 @@ static int update_tmp_mem(const struct t + if (buffer_size == 0) + return 0; + /* Only update the buffer when the buffer size is valid in complete case */ +- if (write_to_client((void *)(uintptr_t)buffer_addr, +- operation->params[index].memref.size, +- op_params->local_tmpbuf[index].temp_buffer, +- operation->params[index].memref.size, +- call_params->dev->kernel_api) != 0) { +- tloge("copy tempbuf failed\n"); +- return -ENOMEM; ++ if (call_params->dev->isVM && !call_params->dev->kernel_api) { ++ if (write_to_VMclient((void *)(uintptr_t)buffer_addr, ++ operation->params[index].memref.size, ++ op_params->local_tmpbuf[index].temp_buffer, ++ operation->params[index].memref.size, ++ call_params->dev->vmpid) != 0) { ++ tloge("copy tempbuf failed\n"); ++ return -ENOMEM; ++ } ++ } else { ++ if (write_to_client((void *)(uintptr_t)buffer_addr, ++ operation->params[index].memref.size, ++ op_params->local_tmpbuf[index].temp_buffer, ++ operation->params[index].memref.size, ++ call_params->dev->kernel_api) != 0) { ++ tloge("copy tempbuf failed\n"); ++ return -ENOMEM; ++ } + } + return 0; + } +@@ -871,7 +1140,10 @@ static void free_operation_params(const + #ifdef CONFIG_NOCOPY_SHAREDMEM + tlogd("free_operation_params release nocopy or register shm\n"); + temp_buf = local_tmpbuf[index].temp_buffer; +- if (temp_buf != NULL) { ++ if (temp_buf != NULL && call_params->dev->isVM) { ++ release_vm_shared_mem_page(temp_buf, local_tmpbuf[index].size, call_params->dev->vm_page_size); ++ mailbox_free(temp_buf); ++ } else if (temp_buf != NULL && !call_params->dev->isVM) { + release_shared_mem_page(temp_buf, local_tmpbuf[index].size); + mailbox_free(temp_buf); + } +diff -uprN itrustee_tzdriver/core/gp_ops.h itrustee_tzdriver_new/core/gp_ops.h +--- itrustee_tzdriver/core/gp_ops.h 2024-07-12 11:08:59.357629380 +0800 ++++ itrustee_tzdriver_new/core/gp_ops.h 2024-07-12 10:51:32.841629380 +0800 +@@ -28,5 +28,9 @@ int tc_client_call(const struct tc_call_ + bool is_tmp_mem(uint32_t param_type); + bool is_ref_mem(uint32_t param_type); + bool is_val_param(uint32_t param_type); ++int write_to_VMclient(void __user *dest, size_t dest_size, ++ const void *src, size_t size, pid_t vm_pid); ++int read_from_VMclient(void *dest, size_t dest_size, ++ const void __user *src, size_t size, pid_t vm_pid); + + +#endif +diff -uprN itrustee_tzdriver/core/session_manager.c itrustee_tzdriver_new/core/session_manager.c +--- itrustee_tzdriver/core/session_manager.c 2024-07-12 11:08:59.357629380 +0800 ++++ itrustee_tzdriver_new/core/session_manager.c 2024-07-12 10:51:32.801629380 +0800 +@@ -589,7 +589,7 @@ static struct tc_ns_service *tc_ref_serv + } + + +static int tc_ns_service_init(const unsigned char *uuid, uint32_t uuid_len, +- struct tc_ns_service **new_service) ++ struct tc_ns_service **new_service, uint32_t nsid) + { + int ret = 0; + struct tc_ns_service *service = NULL; +@@ -610,7 +610,7 @@ static int tc_ns_service_init(const unsi + } + + +#ifdef CONFIG_CONFIDENTIAL_CONTAINER +- service->nsid = task_active_pid_ns(current)->ns.inum; ++ service->nsid = nsid; + #else + service->nsid = PROC_PID_INIT_INO; + #endif +@@ -648,7 +648,11 @@ static struct tc_ns_service *find_servic + struct tc_ns_service *service = NULL; + bool is_full = false; + #ifdef CONFIG_CONFIDENTIAL_CONTAINER +- unsigned int nsid = task_active_pid_ns(current)->ns.inum; ++ unsigned int nsid; ++ if (dev_file->isVM) ++ nsid = dev_file->nsid; ++ else ++ nsid = task_active_pid_ns(current)->ns.inum; + #else + unsigned int nsid = PROC_PID_INIT_INO; + #endif +@@ -677,7 +681,7 @@ static struct tc_ns_service *find_servic + goto add_service; + } + /* Create a new service if we couldn't find it in list */ +- ret = tc_ns_service_init(context->uuid, UUID_LEN, &service); ++ ret = tc_ns_service_init(context->uuid, UUID_LEN, &service, nsid); + /* unlock after init to make sure find service from all is correct */ + mutex_unlock(&g_service_list_lock); + if (ret != 0) { +@@ -791,10 +795,19 @@ static int32_t load_image_copy_file(stru + } + return 0; + } +- if (copy_from_user(params->mb_load_mem + sizeof(load_flag), +- (const void __user *)params->file_buffer + loaded_size, load_size)) { +- tloge("file buf get fail\n"); +- return -EFAULT; ++ if (params->dev_file->isVM) { ++ if (read_from_VMclient(params->mb_load_mem + sizeof(load_flag), ++ load_size, (const void __user *)(params->file_buffer + loaded_size), ++ load_size, (pid_t)params->dev_file->vmpid)) { ++ tloge("file buf get failed \n"); ++ return -EFAULT; ++ } ++ } else { ++ if (copy_from_user(params->mb_load_mem + sizeof(load_flag), ++ (const void __user *)(params->file_buffer + loaded_size), load_size)) { ++ tloge("file buf get failed \n"); ++ return -EFAULT; ++ } + } + return 0; + } +@@ -1387,10 +1400,109 @@ find_session: + return ret; + } + + ++static int process_vm_ref(struct tc_ns_dev_file *dev_file, ++ struct tc_ns_client_context *context, unsigned long long *vm_buffers) ++{ ++ struct tc_ns_shared_mem *shared_mem = NULL; ++ int index = 0; ++ uint32_t buffer_size; ++ unsigned int offset = 0; ++ void *buffer_addr = NULL; ++ void *size_addr = NULL; ++ unsigned long long vm_hvas[TEE_PARAM_NUM]={0}; ++ ++ if (!dev_file->isVM || !context->file_buffer) ++ return 0; ++ ++ if (copy_from_user(vm_hvas, context->file_buffer, context->file_size) != 0) { ++ tloge("copy from user failed\n"); ++ return -EFAULT; ++ } ++ ++ mutex_lock(&dev_file->shared_mem_lock); ++ list_for_each_entry(shared_mem, &dev_file->shared_mem_list, head) { ++ for (index = 0; index < TEE_PARAM_NUM; index++) { ++ buffer_addr = (void *)(uintptr_t)(context->params[index].memref.buffer | ++ ((uint64_t)context->params[index].memref.buffer_h_addr << ADDR_TRANS_NUM)); ++ if (shared_mem->user_addr == buffer_addr) { ++ buffer_addr = (void *)(uintptr_t)(shared_mem->kernel_addr); ++ size_addr = (void *)(uintptr_t)(context->params[index].memref.size_addr | ++ ((uint64_t)context->params[index].memref.size_h_addr << ADDR_TRANS_NUM)); ++ offset = context->params[index].memref.offset; ++ ++ if (copy_from_user(&buffer_size, size_addr, sizeof(uint32_t))) { ++ tloge("copy memref.size_addr failed\n"); ++ return -EFAULT; ++ } ++ ++ if (read_from_VMclient(buffer_addr + offset, buffer_size, ++ (uint32_t __user *)(uintptr_t)(vm_hvas[index] + offset), ++ buffer_size, dev_file->vmpid)) { ++ tloge("copy memref.buffer failed\n"); ++ return -EFAULT; ++ } ++ vm_buffers[index] = vm_hvas[index]; ++ } ++ } ++ } ++ mutex_unlock(&dev_file->shared_mem_lock); ++ return 0; ++} ++ ++static int process_vm_ref_end(struct tc_ns_dev_file *dev_file, ++ struct tc_ns_client_context *context, unsigned long long *vm_buffers) ++{ ++ int ret = 0; ++ struct tc_ns_shared_mem *shared_mem = NULL; ++ int index = 0; ++ uint32_t buffer_size; ++ unsigned int offset = 0; ++ void *buffer_addr = NULL; ++ void *size_addr = NULL; ++ ++ if (!dev_file->isVM) ++ return 0; ++ ++ mutex_lock(&dev_file->shared_mem_lock); ++ list_for_each_entry(shared_mem, &dev_file->shared_mem_list, head) { ++ for (index = 0; index < TEE_PARAM_NUM; index++) { ++ buffer_addr = (void *)(uintptr_t)(context->params[index].memref.buffer | ++ ((uint64_t)context->params[index].memref.buffer_h_addr << ADDR_TRANS_NUM)); ++ if (shared_mem->user_addr == buffer_addr) { ++ buffer_addr = (void *)(uintptr_t)(shared_mem->kernel_addr); ++ size_addr = (void *)(uintptr_t)(context->params[index].memref.size_addr | ++ ((uint64_t)context->params[index].memref.size_h_addr << ADDR_TRANS_NUM)); ++ offset = context->params[index].memref.offset; ++ ++ if (copy_from_user(&buffer_size, size_addr, sizeof(uint32_t))) { ++ tloge("copy memref.size_addr failed\n"); ++ return -EFAULT; ++ } ++ ++ if (write_to_VMclient((void *)(uintptr_t)(vm_buffers[index] + offset), ++ buffer_size, (void *)(uintptr_t)(buffer_addr + offset), ++ buffer_size, dev_file->vmpid)) { ++ tloge("copy buf size failed\n"); ++ return -EFAULT; ++ } ++ } ++ } ++ } ++ mutex_unlock(&dev_file->shared_mem_lock); ++ return ret; ++} ++ + static int ioctl_session_send_cmd(struct tc_ns_dev_file *dev_file, + struct tc_ns_client_context *context, void *argp) + { + int ret; ++ unsigned long long vm_buffers[TEE_PARAM_NUM]={0}; ++ ++ if (dev_file->isVM && ++ process_vm_ref(dev_file, context, vm_buffers)) { ++ tloge("copy from VM memref failed\n"); ++ return -EFAULT; ++ } + + +ret = tc_ns_send_cmd(dev_file, context); + if (ret != 0) +@@ -1399,6 +1511,11 @@ static int ioctl_session_send_cmd(struct + if (ret == 0) + ret = -EFAULT; + } ++ if (ret ==0 && dev_file->isVM && ++ process_vm_ref_end(dev_file, context, vm_buffers)) { ++ tloge("copy to VM memref failed\n"); ++ return -EFAULT; ++ } + return ret; + } + + +diff -uprN itrustee_tzdriver/core/shared_mem.c itrustee_tzdriver_new/core/shared_mem.c +--- itrustee_tzdriver/core/shared_mem.c 2024-07-12 11:08:59.357629380 +0800 ++++ itrustee_tzdriver_new/core/shared_mem.c 2024-07-12 10:51:32.793629380 +0800 +@@ -120,6 +120,36 @@ void release_shared_mem_page(uint64_t bu + put_page(page); + } + } ++ ++ ++void release_vm_shared_mem_page(uint64_t buf, uint32_t buf_size, uint32_t vm_page_size) ++{ ++ uint32_t i; ++ uint64_t *phys_addr = NULL; ++ struct pagelist_info *page_info = NULL; ++ struct page *page = NULL; ++ struct page *last_page = NULL; ++ ++ page_info = (struct pagelist_info *)(uintptr_t)buf; ++ phys_addr = (uint64_t *)(uintptr_t)buf + (sizeof(*page_info) / sizeof(uint64_t)); ++ ++ if (buf_size != sizeof(*page_info) + sizeof(uint64_t) * page_info->page_num) { ++ tloge("bad size, cannot release page\n"); ++ return; ++ } ++ ++ for (i = 0; i < page_info->page_num; i++) { ++ page = (struct page *)(uintptr_t)phys_to_page(phys_addr[i]); ++ if (page == NULL) ++ continue; ++ if (last_page != page) { ++ set_bit(PG_dirty, &page->flags); ++ put_page(page); ++ } ++ last_page = page; ++ } ++} ++ + #else + + +int fill_shared_mem_info(uint64_t start_vaddr, uint32_t pages_no, +diff -uprN itrustee_tzdriver/core/shared_mem.h itrustee_tzdriver_new/core/shared_mem.h +--- itrustee_tzdriver/core/shared_mem.h 2024-07-12 11:08:59.357629380 +0800 ++++ itrustee_tzdriver_new/core/shared_mem.h 2024-07-12 10:51:32.789629380 +0800 +@@ -64,5 +64,5 @@ void free_spi_mem(uint64_t spi_vaddr); + int fill_shared_mem_info(uint64_t start_vaddr, uint32_t pages_no, + uint32_t offset, uint32_t buffer_size, uint64_t info_addr); + void release_shared_mem_page(uint64_t buf, uint32_t buf_size); +- ++void release_vm_shared_mem_page(uint64_t buf, uint32_t buf_size, uint32_t vm_page_size); + #endif +diff -uprN itrustee_tzdriver/core/tc_client_driver.c itrustee_tzdriver_new/core/tc_client_driver.c +--- itrustee_tzdriver/core/tc_client_driver.c 2024-07-12 11:08:59.357629380 +0800 ++++ itrustee_tzdriver_new/core/tc_client_driver.c 2024-07-12 10:51:32.749629380 +0800 +@@ -186,6 +186,8 @@ static int tc_ns_get_tee_version(const s + smc_cmd.operation_h_phys = + (uint64_t)mailbox_virt_to_phys((uintptr_t)&mb_pack->operation) >> ADDR_TRANS_NUM; + + ++ if (dev_file->isVM) ++ smc_cmd.nsid = dev_file->nsid; + if (tc_ns_smc(&smc_cmd) != 0) { + ret = -EPERM; + tloge("smc call returns error ret 0x%x\n", smc_cmd.ret_val); +@@ -312,10 +314,14 @@ static int client_login_prepare(uint8_t + + +static int tc_login_check(const struct tc_ns_dev_file *dev_file) + { +- int ret = check_teecd_auth(); +- if (ret != 0) { +- tloge("teec auth failed, ret %d\n", ret); +- return -EACCES; ++ int ret; ++ ret = check_proxy_auth(); ++ if (ret) { ++ ret = check_teecd_auth(); ++ if (ret != 0) { ++ tloge("teec auth failed, ret %d\n", ret); ++ return -EACCES; ++ } + } + + +if (!dev_file) +@@ -718,12 +724,55 @@ static int ioctl_check_is_ccos(void __us + return ret; + } + + ++static int copy_buf_to_VM(unsigned int agent_id, unsigned int nsid, ++ unsigned long buffer_addr, unsigned int vmpid) ++{ ++ int ret = 0; ++ struct smc_event_data *event_data = NULL; ++ ++ event_data = find_event_control(agent_id, nsid); ++ if (!event_data) ++ return -EINVAL; ++ ++ if (write_to_VMclient((void *)(uintptr_t)buffer_addr, ++ event_data->agent_buff_size, ++ event_data->agent_buff_kernel, ++ event_data->agent_buff_size, ++ vmpid) != 0) { ++ tloge("copy agent buffer failed\n"); ++ return -ENOMEM; ++ } ++ return ret; ++} ++ ++static int copy_buf_from_VM(unsigned int agent_id, unsigned int nsid, ++ unsigned long buffer_addr, unsigned int vmpid) ++{ ++ int ret = 0; ++ struct smc_event_data *event_data = NULL; ++ ++ event_data = find_event_control(agent_id, nsid); ++ if (!event_data) ++ return -EINVAL; ++ ++ if (read_from_VMclient(event_data->agent_buff_kernel, ++ event_data->agent_buff_size, ++ (void *)(uintptr_t)buffer_addr, ++ event_data->agent_buff_size, ++ vmpid) != 0) { ++ tloge("copy agent buffer failed\n"); ++ return -EFAULT; ++ } ++ return ret; ++} ++ + /* ioctls for the secure storage daemon */ + int public_ioctl(const struct file *file, unsigned int cmd, unsigned long arg, bool is_from_client_node) + { + int ret = -EINVAL; + struct tc_ns_dev_file *dev_file = NULL; + uint32_t nsid = get_nsid(); ++ unsigned long tmp[2]; + void *argp = (void __user *)(uintptr_t)arg; + if (file == NULL || file->private_data == NULL) { + tloge("invalid params\n"); +@@ -731,18 +780,34 @@ int public_ioctl(const struct file *file + } + dev_file = file->private_data; + #ifdef CONFIG_CONFIDENTIAL_CONTAINER +- dev_file->nsid = nsid; ++ if (dev_file != NULL && dev_file->nsid == 0) ++ dev_file->nsid = nsid; ++ if (dev_file->isVM) ++ nsid = dev_file->nsid; + #endif + + ++ if (dev_file->isVM) { ++ if (copy_from_user(tmp, (void *)(uintptr_t)arg, sizeof(tmp)) != 0) { ++ tloge("copy agent args failed\n"); ++ return -EFAULT; ++ } ++ arg = tmp[0]; ++ } + switch (cmd) { + case TC_NS_CLIENT_IOCTL_WAIT_EVENT: + if (ioctl_check_agent_owner(dev_file, (unsigned int)arg, nsid) != 0) + return -EINVAL; + ret = tc_ns_wait_event((unsigned int)arg, nsid); ++ if (!ret && dev_file->isVM) { ++ ret = copy_buf_to_VM(tmp[0], nsid, tmp[1], dev_file->vmpid); ++ } + break; + case TC_NS_CLIENT_IOCTL_SEND_EVENT_RESPONSE: + if (ioctl_check_agent_owner(dev_file, (unsigned int)arg, nsid) != 0) + return -EINVAL; ++ if (dev_file->isVM) { ++ ret = copy_buf_from_VM(tmp[0], nsid, tmp[1], dev_file->vmpid); ++ } + ret = tc_ns_send_event_response((unsigned int)arg, nsid); + break; + case TC_NS_CLIENT_IOCTL_REGISTER_AGENT: +@@ -839,6 +904,14 @@ static int tc_client_agent_ioctl(const s + return ret; + } + + ++int set_vm_flag(struct tc_ns_dev_file *dev_file, int vmid) ++{ ++ dev_file->nsid = vmid; ++ dev_file->vmpid = vmid; ++ tlogd(" dev_file->vmpid %d\n", (int)dev_file->vmpid); ++ return 0; ++} ++ + void handle_cmd_prepare(unsigned int cmd) + { + if (cmd != TC_NS_CLIENT_IOCTL_WAIT_EVENT && +@@ -858,6 +931,10 @@ static long tc_private_ioctl(struct file + { + int ret = -EFAULT; + void *argp = (void __user *)(uintptr_t)arg; ++ if (cmd == TC_NS_CLIENT_IOCTL_SET_VM_FLAG) { ++ tlogd(" before set_vm_flag \n"); ++ return set_vm_flag(file->private_data, (int)arg); ++ } + handle_cmd_prepare(cmd); + switch (cmd) { + case TC_NS_CLIENT_IOCTL_GET_TEE_VERSION: +@@ -872,10 +949,10 @@ static long tc_private_ioctl(struct file + mutex_unlock(&g_set_ca_hash_lock); + break; + case TC_NS_CLIENT_IOCTL_LATEINIT: +- ret = tc_ns_late_init(arg); ++ ret = tc_ns_late_init(file->private_data, arg); + break; + case TC_NS_CLIENT_IOCTL_SYC_SYS_TIME: +- ret = sync_system_time_from_user( ++ ret = sync_system_time_from_user(file->private_data, + (struct tc_ns_client_time *)(uintptr_t)arg); + break; + default: +@@ -894,6 +971,10 @@ static long tc_client_ioctl(struct file + int ret = -EFAULT; + void *argp = (void __user *)(uintptr_t)arg; + + ++ if (cmd == TC_NS_CLIENT_IOCTL_SET_VM_FLAG) { ++ tlogd(" before set_vm_flag \n"); ++ return set_vm_flag(file->private_data, (int)arg); ++ } + handle_cmd_prepare(cmd); + switch (cmd) { + case TC_NS_CLIENT_IOCTL_SES_OPEN_REQ: +@@ -925,20 +1006,30 @@ static int tc_client_open(struct inode * + { + int ret; + struct tc_ns_dev_file *dev = NULL; ++ int vm = 0; + (void)inode; + + +- ret = check_teecd_auth(); +- if (ret != 0) { +- tloge("teec auth failed, ret %d\n", ret); +- return -EACCES; ++ ret =check_proxy_auth(); ++ if (ret) { ++ ret = check_teecd_auth(); ++ if (ret != 0) { ++ tloge("teec auth failed, ret %d\n", ret); ++ return -EACCES; ++ } ++ } else { ++ vm = 1; + } + + +file->private_data = NULL; + ret = tc_ns_client_open(&dev, TEE_REQ_FROM_USER_MODE); +- if (ret == 0) ++ if (ret == 0) { + file->private_data = dev; ++ if (vm) ++ dev->isVM = true; ++ } + #ifdef CONFIG_TEE_REBOOT +- get_teecd_pid(); ++ if (!vm && check_teecd_auth() == 0) ++ get_teecd_pid(); + #endif + return ret; + } +diff -uprN itrustee_tzdriver/core/tc_client_driver.h itrustee_tzdriver_new/core/tc_client_driver.h +--- itrustee_tzdriver/core/tc_client_driver.h 2024-07-12 11:08:59.357629380 +0800 ++++ itrustee_tzdriver_new/core/tc_client_driver.h 2024-07-12 10:51:32.741629380 +0800 +@@ -47,6 +47,7 @@ int tc_ns_client_open(struct tc_ns_dev_f + int tc_ns_client_close(struct tc_ns_dev_file *dev); + int is_agent_alive(unsigned int agent_id, unsigned int nsid); + int tc_ns_register_host_nsid(void); ++int set_vm_flag(struct tc_ns_dev_file *dev_file, int vmid); + int init_dev_node(struct dev_node *node, const char *node_name, + struct class *driver_class, const struct file_operations *fops); + void destory_dev_node(struct dev_node *node, struct class *driver_class); +diff -uprN itrustee_tzdriver/core/tc_cvm_driver.c itrustee_tzdriver_new/core/tc_cvm_driver.c +--- itrustee_tzdriver/core/tc_cvm_driver.c 2024-07-12 11:08:59.357629380 +0800 ++++ itrustee_tzdriver_new/core/tc_cvm_driver.c 2024-07-12 10:51:32.737629380 +0800 +@@ -104,6 +104,10 @@ static long tc_cvm_ioctl(struct file *fi + { + int ret = -EFAULT; + void *argp = (void __user *)(uintptr_t)arg; ++ if (cmd == TC_NS_CLIENT_IOCTL_SET_VM_FLAG) { ++ tlogd(" before set_vm_flag \n"); ++ return set_vm_flag(file->private_data, (int)arg); ++ } + handle_cmd_prepare(cmd); + + +switch (cmd) { +diff -uprN itrustee_tzdriver/Makefile itrustee_tzdriver_new/Makefile +--- itrustee_tzdriver/Makefile 2024-07-12 11:08:59.353629380 +0800 ++++ itrustee_tzdriver_new/Makefile 2024-07-12 10:51:32.641629380 +0800 +@@ -54,6 +54,7 @@ EXTRA_CFLAGS += -DCONFIG_CPU_AFF_NR=0 -D + EXTRA_CFLAGS += -DCONFIG_TEE_LOG_ACHIVE_PATH="/var/log/tee/last_teemsg" + EXTRA_CFLAGS += -DNOT_TRIGGER_AP_RESET -DLAST_TEE_MSG_ROOT_GID -DCONFIG_NOCOPY_SHAREDMEM -DCONFIG_REGISTER_SHAREDMEM -DCONFIG_TA_AFFINITY=y -DCONFIG_TA_AFFINITY_CPU_NUMS=128 + EXTRA_CFLAGS += -DTEECD_PATH_UID_AUTH_CTX="/usr/bin/teecd:0" ++EXTRA_CFLAGS += -DPROXY_PATH_UID_AUTH_CTX="/usr/bin/vtz_proxy:0" + EXTRA_CFLAGS += -DCONFIG_AUTH_SUPPORT_UNAME -DCONFIG_AUTH_HASH -std=gnu99 + EXTRA_CFLAGS += -DCONFIG_TEE_UPGRADE -DCONFIG_TEE_REBOOT -DCONFIG_CONFIDENTIAL_TEE + EXTRA_CFLAGS += -I$(PWD)/tzdriver_internal/tee_reboot +diff -uprN itrustee_tzdriver/modules.order itrustee_tzdriver_new/modules.order +--- itrustee_tzdriver/modules.order 1970-01-01 08:00:00.000000000 +0800 ++++ itrustee_tzdriver_new/modules.order 2024-07-12 10:54:21.957629380 +0800 +@@ -0,0 +1 @@ ++kernel//home/z50040113/target/itrustee_tzdriver/tzdriver.ko +diff -uprN itrustee_tzdriver/tc_ns_client.h itrustee_tzdriver_new/tc_ns_client.h +--- itrustee_tzdriver/tc_ns_client.h 2024-07-12 11:08:59.361629380 +0800 ++++ itrustee_tzdriver_new/tc_ns_client.h 2024-07-12 10:51:32.625629380 +0800 +@@ -230,6 +230,8 @@ struct tc_ns_log_pool { + #define TC_NS_CLIENT_IOCTL_GET_TEE_INFO \ + _IOWR(TC_NS_CLIENT_IOC_MAGIC, 26, struct tc_ns_tee_info) + + ++#define TC_NS_CLIENT_IOCTL_SET_VM_FLAG \ ++ _IOWR(TC_NS_CLIENT_IOC_MAGIC, 27, int) + #define TC_NS_CLIENT_IOCTL_CHECK_CCOS \ + _IOWR(TC_NS_CLIENT_IOC_MAGIC, 32, unsigned int) + + +diff -uprN itrustee_tzdriver/teek_ns_client.h itrustee_tzdriver_new/teek_ns_client.h +--- itrustee_tzdriver/teek_ns_client.h 2024-07-12 11:08:59.361629380 +0800 ++++ itrustee_tzdriver_new/teek_ns_client.h 2024-07-12 10:51:32.597629380 +0800 +@@ -129,6 +129,9 @@ struct tc_ns_dev_file { + int load_app_flag; + #ifdef CONFIG_CONFIDENTIAL_CONTAINER + uint32_t nsid; ++ uint32_t vmpid; ++ bool isVM; ++ uint32_t vm_page_size; + #endif + struct completion close_comp; /* for kthread close unclosed session */ + #ifdef CONFIG_TEE_TELEPORT_SUPPORT +diff -uprN itrustee_tzdriver/tlogger/tlogger.c itrustee_tzdriver_new/tlogger/tlogger.c +--- itrustee_tzdriver/tlogger/tlogger.c 2024-07-12 11:08:59.361629380 +0800 ++++ itrustee_tzdriver_new/tlogger/tlogger.c 2024-07-12 10:51:32.581629380 +0800 +@@ -61,6 +61,7 @@ + #define SET_TLOGCAT_STAT_BASE 7 + #define GET_TLOGCAT_STAT_BASE 8 + #define GET_TEE_INFO_BASE 9 ++#define SET_VM_FLAG 10 + + +/* get tee verison */ + #define MAX_TEE_VERSION_LEN 256 +@@ -75,6 +76,8 @@ + _IO(LOGGERIOCTL, GET_TLOGCAT_STAT_BASE) + #define TEELOGGER_GET_TEE_INFO \ + _IOR(LOGGERIOCTL, GET_TEE_INFO_BASE, struct tc_ns_tee_info) ++#define TEELOGGER_SET_VM_FLAG \ ++ _IOR(LOGGERIOCTL, SET_VM_FLAG, int) + + +int g_tlogcat_f = 0; + + +@@ -515,7 +518,7 @@ void recycle_tlogcat_processes(void) + } + #endif + + +-static struct tlogger_group *get_tlogger_group(void) ++static struct tlogger_group *get_tlogger_group(uint32_t vmpid) + { + struct tlogger_group *group = NULL; + #ifdef CONFIG_CONFIDENTIAL_CONTAINER +@@ -524,6 +527,9 @@ static struct tlogger_group *get_tlogger + uint32_t nsid = PROC_PID_INIT_INO; + #endif + + ++ if (vmpid) ++ nsid = vmpid; ++ + list_for_each_entry(group, &g_reader_group_list, node) { + if (group->nsid == nsid) + return group; +@@ -596,7 +602,7 @@ static int process_tlogger_open(struct i + return -ENODEV; + + +mutex_lock(&g_reader_group_mutex); +- group = get_tlogger_group(); ++ group = get_tlogger_group(0); + if (group == NULL) { + group = kzalloc(sizeof(*group), GFP_KERNEL); + if (ZERO_OR_NULL_PTR((unsigned long)(uintptr_t)group)) { +@@ -808,6 +814,35 @@ static int get_teeos_version(uint32_t cm + return 0; + } + + ++int set_tlog_vm_flag(struct file *file, uint32_t vmpid) ++{ ++ struct tlogger_reader *reader = NULL; ++ struct tlogger_group *group = NULL; ++ ++ if (!file || !file->private_data) { ++ return -1; ++ } ++ ++ reader = file->private_data; ++ mutex_lock(&g_reader_group_mutex); ++ group = get_tlogger_group(vmpid); ++ if (group == NULL) { ++ group = kzalloc(sizeof(*group), GFP_KERNEL); ++ if (ZERO_OR_NULL_PTR((unsigned long)(uintptr_t)group)) { ++ mutex_unlock(&g_reader_group_mutex); ++ return -ENOMEM; ++ } ++ init_tlogger_group(group); ++ group->nsid = vmpid; ++ list_add_tail(&group->node, &g_reader_group_list); ++ } else { ++ group->reader_cnt++; ++ } ++ mutex_unlock(&g_reader_group_mutex); ++ reader->group = group; ++ return 0; ++} ++ + static long process_tlogger_ioctl(struct file *file, + unsigned int cmd, unsigned long arg) + { +@@ -845,6 +880,9 @@ static long process_tlogger_ioctl(struct + case TEELOGGER_GET_TEE_INFO: + ret = tc_ns_get_tee_info(file, (void *)(uintptr_t)arg); + break; ++ case TEELOGGER_SET_VM_FLAG: ++ ret = set_tlog_vm_flag(file, (int)arg); ++ break; + default: + tloge("ioctl error default\n"); + break; +@@ -1023,11 +1061,13 @@ static int write_part_log_to_msg(struct + + +while (next_item && read_off <= read_off_end) { + item_len = next_item->buffer_len + sizeof(*next_item); +- write_len = kernel_write(filep, next_item->log_buffer, +- next_item->real_len, pos); +- if (write_len < 0) { +- tloge("Failed to write last teemsg %zd\n", write_len); +- return -1; ++ if (next_item->nsid == 0) { ++ write_len = kernel_write(filep, next_item->log_buffer, ++ next_item->real_len, pos); ++ if (write_len < 0) { ++ tloge("Failed to write last teemsg %zd\n", write_len); ++ return -1; ++ } + } + + +tlogd("Succeed to Write last teemsg, len=%zd\n", write_len); \ No newline at end of file diff --git a/trustzone-awared-vm/Host/itrustee_tzdriver_new.zip b/trustzone-awared-vm/Host/itrustee_tzdriver_new.zip deleted file mode 100644 index 0d44284fc870dd7918355d89e0dad91b4f396ada..0000000000000000000000000000000000000000 Binary files a/trustzone-awared-vm/Host/itrustee_tzdriver_new.zip and /dev/null differ diff --git a/trustzone-awared-vm/Host/itrustee_tzdriver.patch b/trustzone-awared-vm/Host/itrustee_tzdriver_v1.5.0-r1.patch similarity index 100% rename from trustzone-awared-vm/Host/itrustee_tzdriver.patch rename to trustzone-awared-vm/Host/itrustee_tzdriver_v1.5.0-r1.patch diff --git a/trustzone-awared-vm/Host/qemu.patch b/trustzone-awared-vm/Host/qemu-v5.0.0.patch similarity index 100% rename from trustzone-awared-vm/Host/qemu.patch rename to trustzone-awared-vm/Host/qemu-v5.0.0.patch diff --git a/trustzone-awared-vm/Host/qemu-v6.2.0.patch b/trustzone-awared-vm/Host/qemu-v6.2.0.patch new file mode 100644 index 0000000000000000000000000000000000000000..2458e2ef0493fd5d1f8d35dd4d49668eaa9fe6aa --- /dev/null +++ b/trustzone-awared-vm/Host/qemu-v6.2.0.patch @@ -0,0 +1,887 @@ +diff -ruNp qemu/hw/char/tc_ns_client.h qemu-after/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 +@@ -0,0 +1,162 @@ ++/* ++ * Copyright (c) Huawei Technologies Co., Ltd. 2012-2023. All rights reserved. ++ * Licensed under the Mulan PSL v2. ++ * You can use this software according to the terms and conditions of the Mulan PSL v2. ++ * You may obtain a copy of Mulan PSL v2 at: ++ * http://license.coscl.org.cn/MulanPSL2 ++ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR ++ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR ++ * PURPOSE. ++ * See the Mulan PSL v2 for more details. ++ */ ++ ++#ifndef _TC_NS_CLIENT_H_ ++#define _TC_NS_CLIENT_H_ ++#include "tee_client_type.h" ++#define TC_DEBUG ++ ++#define INVALID_TYPE 0x00 ++#define TEECD_CONNECT 0x01 ++#ifndef ZERO_SIZE_PTR ++#define ZERO_SIZE_PTR ((void *)16) ++#define ZERO_OR_NULL_PTR(x) ((unsigned long)(x) <= (unsigned long)ZERO_SIZE_PTR) ++#endif ++ ++#define UUID_SIZE 16 ++ ++#define TC_NS_CLIENT_IOC_MAGIC 't' ++#define TC_NS_CLIENT_DEV "tc_ns_client" ++#define TC_NS_CLIENT_DEV_NAME "/dev/tc_ns_client" ++#define TC_TEECD_PRIVATE_DEV_NAME "/dev/tc_private" ++#define TC_NS_CVM_DEV_NAME "/dev/tc_ns_cvm" ++ ++enum ConnectCmd { ++ GET_FD, ++ GET_TEEVERSION, ++ SET_SYS_XML, ++ GET_TEECD_VERSION, ++}; ++ ++typedef struct { ++ unsigned int method; ++ unsigned int mdata; ++} TC_NS_ClientLogin; ++ ++typedef union { ++ struct { ++ unsigned long long buffer; ++ unsigned long long offset; ++ unsigned long long size_addr; ++ } memref; ++ struct { ++ unsigned long long a_addr; ++ unsigned long long b_addr; ++ } value; ++} TC_NS_ClientParam; ++ ++typedef struct { ++ unsigned int code; ++ unsigned int origin; ++} TC_NS_ClientReturn; ++ ++typedef struct { ++ unsigned char uuid[UUID_SIZE]; ++ unsigned int session_id; ++ unsigned int cmd_id; ++ TC_NS_ClientReturn returns; ++ TC_NS_ClientLogin login; ++ TC_NS_ClientParam params[TEEC_PARAM_NUM]; ++ unsigned int paramTypes; ++ bool started; ++ unsigned int callingPid; ++ unsigned int file_size; ++ union { ++ char *file_buffer; ++ struct { ++ uint32_t file_addr; ++ uint32_t file_h_addr; ++ } memref; ++ }; ++} TC_NS_ClientContext; ++ ++typedef struct { ++ uint32_t seconds; ++ uint32_t millis; ++} TC_NS_Time; ++ ++typedef struct { ++ uint16_t tzdriver_version_major; ++ uint16_t tzdriver_version_minor; ++ uint32_t reserved[15]; ++} TC_NS_TEE_Info; ++ ++enum SecFileType { ++ LOAD_TA = 0, ++ LOAD_SERVICE, ++ LOAD_LIB, ++ LOAD_DYNAMIC_DRV, ++ LOAD_PATCH, ++ LOAD_TYPE_MAX ++}; ++ ++struct SecFileInfo { ++ enum SecFileType fileType; ++ uint32_t fileSize; ++ int32_t secLoadErr; ++}; ++ ++struct SecLoadIoctlStruct { ++ struct SecFileInfo secFileInfo; ++ TEEC_UUID uuid; ++ union { ++ char *fileBuffer; ++ struct { ++ uint32_t file_addr; ++ uint32_t file_h_addr; ++ } memref; ++ }; ++}__attribute__((packed)); ++ ++struct AgentIoctlArgs { ++ uint32_t id; ++ uint32_t bufferSize; ++ union { ++ void *buffer; ++ unsigned long long addr; ++ }; ++}; ++ ++#define TC_NS_CLIENT_IOCTL_SES_OPEN_REQ _IOW(TC_NS_CLIENT_IOC_MAGIC, 1, TC_NS_ClientContext) ++#define TC_NS_CLIENT_IOCTL_SES_CLOSE_REQ _IOWR(TC_NS_CLIENT_IOC_MAGIC, 2, TC_NS_ClientContext) ++#define TC_NS_CLIENT_IOCTL_SEND_CMD_REQ _IOWR(TC_NS_CLIENT_IOC_MAGIC, 3, TC_NS_ClientContext) ++#define TC_NS_CLIENT_IOCTL_SHRD_MEM_RELEASE _IOWR(TC_NS_CLIENT_IOC_MAGIC, 4, unsigned int) ++#define TC_NS_CLIENT_IOCTL_WAIT_EVENT _IOWR(TC_NS_CLIENT_IOC_MAGIC, 5, unsigned int) ++#define TC_NS_CLIENT_IOCTL_SEND_EVENT_RESPONSE _IOWR(TC_NS_CLIENT_IOC_MAGIC, 6, unsigned int) ++#define TC_NS_CLIENT_IOCTL_REGISTER_AGENT _IOWR(TC_NS_CLIENT_IOC_MAGIC, 7, struct AgentIoctlArgs) ++#define TC_NS_CLIENT_IOCTL_UNREGISTER_AGENT _IOWR(TC_NS_CLIENT_IOC_MAGIC, 8, unsigned int) ++#define TC_NS_CLIENT_IOCTL_LOAD_APP_REQ _IOWR(TC_NS_CLIENT_IOC_MAGIC, 9, struct SecLoadIoctlStruct) ++#define TC_NS_CLIENT_IOCTL_NEED_LOAD_APP _IOWR(TC_NS_CLIENT_IOC_MAGIC, 10, TC_NS_ClientContext) ++#define TC_NS_CLIENT_IOCTL_LOAD_APP_EXCEPT _IOWR(TC_NS_CLIENT_IOC_MAGIC, 11, unsigned int) ++#define TC_NS_CLIENT_IOCTL_CANCEL_CMD_REQ _IOWR(TC_NS_CLIENT_IOC_MAGIC, 13, TC_NS_ClientContext) ++#define TC_NS_CLIENT_IOCTL_LOGIN _IOWR(TC_NS_CLIENT_IOC_MAGIC, 14, int) ++#define TC_NS_CLIENT_IOCTL_TST_CMD_REQ _IOWR(TC_NS_CLIENT_IOC_MAGIC, 15, int) ++#define TC_NS_CLIENT_IOCTL_TUI_EVENT _IOWR(TC_NS_CLIENT_IOC_MAGIC, 16, int) ++#define TC_NS_CLIENT_IOCTL_SYC_SYS_TIME _IOWR(TC_NS_CLIENT_IOC_MAGIC, 17, TC_NS_Time) ++#define TC_NS_CLIENT_IOCTL_SET_NATIVE_IDENTITY _IOWR(TC_NS_CLIENT_IOC_MAGIC, 18, int) ++#define TC_NS_CLIENT_IOCTL_LOAD_TTF_FILE_AND_NOTCH_HEIGHT _IOWR(TC_NS_CLIENT_IOC_MAGIC, 19, unsigned int) ++#define TC_NS_CLIENT_IOCTL_LATEINIT _IOWR(TC_NS_CLIENT_IOC_MAGIC, 20, unsigned int) ++#define TC_NS_CLIENT_IOCTL_GET_TEE_VERSION _IOWR(TC_NS_CLIENT_IOC_MAGIC, 21, unsigned int) ++#ifdef CONFIG_CMS_SIGNATURE ++#define TC_NS_CLIENT_IOCTL_UPDATE_TA_CRL _IOWR(TC_NS_CLIENT_IOC_MAGIC, 22, struct TC_NS_ClientCrl) ++#endif ++#ifdef CONFIG_TEE_TELEPORT_SUPPORT ++#define TC_NS_CLIENT_IOCTL_PORTAL_REGISTER _IOWR(TC_NS_CLIENT_IOC_MAGIC, 24, struct AgentIoctlArgs) ++#define TC_NS_CLIENT_IOCTL_PORTAL_WORK _IOWR(TC_NS_CLIENT_IOC_MAGIC, 25, struct AgentIoctlArgs) ++#endif ++#define TC_NS_CLIENT_IOCTL_GET_TEE_INFO _IOWR(TC_NS_CLIENT_IOC_MAGIC, 26, TC_NS_TEE_Info) ++#define TC_NS_CLIENT_IOCTL_SET_VM_FLAG _IOWR(TC_NS_CLIENT_IOC_MAGIC, 27, int) ++ ++TEEC_Result TEEC_CheckOperation(const TEEC_Operation *operation); ++#endif ++ ++ +diff -ruNp qemu/hw/char/tee_client_constants.h qemu-after/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 @@ ++/* ++ * Copyright (c) Huawei Technologies Co., Ltd. 2013-2022. All rights reserved. ++ * Licensed under the Mulan PSL v2. ++ * You can use this software according to the terms and conditions of the Mulan PSL v2. ++ * You may obtain a copy of Mulan PSL v2 at: ++ * http://license.coscl.org.cn/MulanPSL2 ++ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR ++ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR ++ * PURPOSE. ++ * See the Mulan PSL v2 for more details. ++ */ ++ ++#ifndef _TEE_CLIENT_CONSTANTS_H_ ++#define _TEE_CLIENT_CONSTANTS_H_ ++ ++enum TEEC_ReturnCode { ++ TEEC_SUCCESS = 0x0, /* success */ ++ TEEC_ERROR_INVALID_CMD, /* invalid command */ ++ TEEC_ERROR_SERVICE_NOT_EXIST, /* target service is not exist */ ++ TEEC_ERROR_SESSION_NOT_EXIST, /* session between client and service is not exist */ ++ TEEC_ERROR_SESSION_MAXIMUM, /* exceed max num of sessions */ ++ TEEC_ERROR_REGISTER_EXIST_SERVICE, /* cannot register the service which already exist */ ++ TEEC_ERROR_TAGET_DEAD_FATAL, /* system error occurs in TEE */ ++ TEEC_ERROR_READ_DATA, /* failed to read data in file */ ++ TEEC_ERROR_WRITE_DATA, /* failed to write data to file */ ++ TEEC_ERROR_TRUNCATE_OBJECT, /* data is truncated */ ++ TEEC_ERROR_SEEK_DATA, /* failed to seek data in file */ ++ TEEC_ERROR_FSYNC_DATA, /* failed to sync data in file */ ++ TEEC_ERROR_RENAME_OBJECT, /* failed to rename file */ ++ TEEC_ERROR_TRUSTED_APP_LOAD_ERROR, /* failed to load Trusted Application */ ++ TEEC_ERROR_GENERIC = 0xFFFF0000, /* generic error occurs */ ++ TEEC_ERROR_ACCESS_DENIED = 0xFFFF0001, /* permission check failed, in initilize context or ++ open session or invoke commnad */ ++ TEEC_ERROR_CANCEL = 0xFFFF0002, /* operation is already canceled */ ++ TEEC_ERROR_ACCESS_CONFLICT = 0xFFFF0003, /* confilct occurs in concurrent access to data, ++ error occurs in file operaions generally */ ++ TEEC_ERROR_EXCESS_DATA = 0xFFFF0004, /* exceed max data to be handled by system */ ++ TEEC_ERROR_BAD_FORMAT = 0xFFFF0005, /* data format is invalid, Trusted Application cannot ++ handle it */ ++ TEEC_ERROR_BAD_PARAMETERS = 0xFFFF0006, /* invalid parameters */ ++ TEEC_ERROR_BAD_STATE = 0xFFFF0007, /* operation failed in current state, when try to access ++ storage without initilize storage service */ ++ TEEC_ERROR_ITEM_NOT_FOUND = 0xFFFF0008, /* cannot find target item */ ++ TEEC_ERROR_NOT_IMPLEMENTED = 0xFFFF0009, /* request operation is not implemented */ ++ TEEC_ERROR_NOT_SUPPORTED = 0xFFFF000A, /* request operation is not supported */ ++ TEEC_ERROR_NO_DATA = 0xFFFF000B, /* no data present for current operation */ ++ TEEC_ERROR_OUT_OF_MEMORY = 0xFFFF000C, /* system resource if out of use */ ++ TEEC_ERROR_BUSY = 0xFFFF000D, /* system is too busy to handle current operation */ ++ TEEC_ERROR_COMMUNICATION = 0xFFFF000E, /* error occurs when client try to communicate ++ with Trusted Application */ ++ TEEC_ERROR_SECURITY = 0xFFFF000F, /* security error occurs */ ++ TEEC_ERROR_SHORT_BUFFER = 0xFFFF0010, /* out buffer is not enough for current request */ ++ TEEC_ERROR_MAC_INVALID = 0xFFFF3071, /* MAC value check failed */ ++ TEEC_ERROR_TARGET_DEAD = 0xFFFF3024, /* Trusted Application is crashed */ ++ TEEC_FAIL = 0xFFFF5002, /* common error */ ++ TEEC_ERROR_EXTERNAL_CANCEL = 0xFFFF0011, /* used by adapt only, event caused User Interface operation aborted */ ++ TEEC_ERROR_OVERFLOW = 0xFFFF300F, /* used by adapt only */ ++ TEEC_ERROR_STORAGE_NO_SPACE = 0xFFFF3041, /* used by adapt only */ ++ TEEC_ERROR_SIGNATURE_INVALID = 0xFFFF3072, /* used by adapt only */ ++ TEEC_ERROR_TIME_NOT_SET = 0xFFFF5000, /* used by adapt only */ ++ TEEC_ERROR_TIME_NEEDS_RESET = 0xFFFF5001, /* used by adapt only */ ++ TEEC_ERROR_IPC_OVERFLOW = 0xFFFF9114 /* ipc overflow */ ++}; ++ ++enum TEEC_ReturnCodeOrigin { ++ TEEC_ORIGIN_API = 0x1, /* error occurs in handling client API */ ++ TEEC_ORIGIN_COMMS = 0x2, /* error occurs in communicating between REE and TEE */ ++ TEEC_ORIGIN_TEE = 0x3, /* error occurs in TEE */ ++ TEEC_ORIGIN_TRUSTED_APP = 0x4, /* error occurs in Trusted Application */ ++}; ++ ++enum TEEC_SharedMemCtl { ++ TEEC_MEM_INPUT = 0x1, /* input type of memroy */ ++ TEEC_MEM_OUTPUT = 0x2, /* output type of memory */ ++ TEEC_MEM_INOUT = 0x3, /* memory is used as both input and output */ ++ TEEC_MEM_SHARED_INOUT = 0x4, /* no copy shared memory */ ++}; ++ ++enum TEEC_ParamType { ++ TEEC_NONE = 0x0, /* unused parameter */ ++ TEEC_VALUE_INPUT = 0x01, /* input type of value, refer TEEC_Value */ ++ TEEC_VALUE_OUTPUT = 0x02, /* output type of value, refer TEEC_Value */ ++ TEEC_VALUE_INOUT = 0x03, /* value is used as both input and output, refer TEEC_Value */ ++ TEEC_MEMREF_TEMP_INPUT = 0x05, /* input type of temp memory reference, refer TEEC_TempMemoryReference */ ++ TEEC_MEMREF_TEMP_OUTPUT = 0x06, /* output type of temp memory reference, refer TEEC_TempMemoryReference */ ++ TEEC_MEMREF_TEMP_INOUT = 0x07, /* temp memory reference used as both input and output, ++ refer TEEC_TempMemoryReference */ ++ 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_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 */ ++ TEEC_MEMREF_PARTIAL_INOUT = 0xf /* memory reference used as both input and output, ++ refer TEEC_RegisteredMemoryReference */ ++}; ++ ++/**************************************************** ++ * Session Login Methods ++ ****************************************************/ ++enum TEEC_LoginMethod { ++ TEEC_LOGIN_PUBLIC = 0x0, /* no Login data is provided */ ++ TEEC_LOGIN_USER, /* Login data about the user running the ++ Client Application process is provided */ ++ TEEC_LOGIN_GROUP, /* Login data about the group running ++ the Client Application process is provided */ ++ TEEC_LOGIN_APPLICATION = 0x4, /* Login data about the running Client ++ Application itself is provided */ ++ TEEC_LOGIN_USER_APPLICATION = 0x5, /* Login data about the user running the ++ Client Application and about the ++ Client Application itself is provided */ ++ TEEC_LOGIN_GROUP_APPLICATION = 0x6, /* Login data about the group running ++ the Client Application and about the ++ Client Application itself is provided */ ++ TEEC_LOGIN_IDENTIFY = 0x7, /* Login data is provided by REE system */ ++}; ++enum TST_CMD_ID { ++ TST_CMD_ID_01 = 1, ++ TST_CMD_ID_02, ++ TST_CMD_ID_03, ++ TST_CMD_ID_04, ++ TST_CMD_ID_05 ++}; ++ ++#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 +--- 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 +@@ -0,0 +1,101 @@ ++/* ++ * Copyright (c) Huawei Technologies Co., Ltd. 2013-2021. All rights reserved. ++ * iTrustee licensed under the Mulan PSL v2. ++ * You can use this software according to the terms and conditions of the Mulan PSL v2. ++ * You may obtain a copy of Mulan PSL v2 at: ++ * http://license.coscl.org.cn/MulanPSL2 ++ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR ++ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR ++ * PURPOSE. ++ * See the Mulan PSL v2 for more details. ++ */ ++ ++#ifndef TEE_CLIENT_LIST_H ++#define TEE_CLIENT_LIST_H ++ ++struct ListNode { ++ struct ListNode *next; /* point to next node */ ++ struct ListNode *prev; /* point to prev node */ ++}; ++ ++#define OFFSET_OF(type, member) (unsigned long)(&(((type *)0)->member)) ++#define CONTAINER_OF(pos, type, member) (type *)(((char *)(pos)) - OFFSET_OF(type, member)) ++ ++#define LIST_DECLARE(name) \ ++ struct ListNode name = { \ ++ .next = &name, \ ++ .prev = &name, \ ++ } ++ ++static inline void ListInit(struct ListNode *list) ++{ ++ list->next = list; ++ list->prev = list; ++} ++ ++#define LIST_HEAD(list) ((list)->next) ++#define LIST_TAIL(list) ((list)->prev) ++#define LIST_EMPTY(list) ((list) == (list)->next) ++ ++static inline void ListInsertHead(struct ListNode *list, struct ListNode *entry) ++{ ++ list->next->prev = entry; ++ entry->next = list->next; ++ entry->prev = list; ++ list->next = entry; ++} ++ ++static inline void ListInsertTail(struct ListNode *list, struct ListNode *entry) ++{ ++ entry->next = list; ++ entry->prev = list->prev; ++ list->prev->next = entry; ++ list->prev = entry; ++} ++ ++static inline void ListRemoveEntry(struct ListNode *entry) ++{ ++ entry->prev->next = entry->next; ++ entry->next->prev = entry->prev; ++} ++ ++static inline struct ListNode *ListRemoveHead(struct ListNode *list) ++{ ++ struct ListNode *entry = NULL; ++ if (!LIST_EMPTY(list)) { ++ entry = list->next; ++ ListRemoveEntry(entry); ++ } ++ return entry; ++} ++ ++static inline struct ListNode *ListRemoveTail(struct ListNode *list) ++{ ++ struct ListNode *entry = NULL; ++ if (!LIST_EMPTY(list)) { ++ entry = list->prev; ++ ListRemoveEntry(entry); ++ } ++ return entry; ++} ++ ++#define LIST_ENTRY(ptr, type, member) \ ++ ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member))) ++ ++#define LIST_FOR_EACH(pos, list) \ ++ for (pos = (list)->next; pos != (list); pos = pos->next) ++ ++#define LIST_FOR_EACH_SAFE(pos, n, list) \ ++ for ((pos) = (list)->next, (n) = (pos)->next; (pos) != (list); (pos) = (n), (n) = (pos)->next) ++ ++#define LIST_FOR_EACH_ENTRY(pos, list, member) \ ++ for (pos = LIST_ENTRY((list)->next, typeof(*pos), member); &pos->member != (list); \ ++ pos = LIST_ENTRY(pos->member.next, typeof(*pos), member)) ++ ++#define LIST_FOR_EACH_ENTRY_SAFE(pos, n, list, member) \ ++ for (pos = LIST_ENTRY((list)->next, typeof(*pos), member), n = LIST_ENTRY(pos->member.next, typeof(*pos), \ ++ member); &pos->member != (list); pos = n, n = LIST_ENTRY(n->member.next, typeof(*n), member)) ++ ++#endif ++ ++ +diff -ruNp qemu/hw/char/tee_client_type.h qemu-after/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 +@@ -0,0 +1,134 @@ ++/* ++ * Copyright (c) Huawei Technologies Co., Ltd. 2013-2022. All rights reserved. ++ * Licensed under the Mulan PSL v2. ++ * You can use this software according to the terms and conditions of the Mulan PSL v2. ++ * You may obtain a copy of Mulan PSL v2 at: ++ * http://license.coscl.org.cn/MulanPSL2 ++ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR ++ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR ++ * PURPOSE. ++ * See the Mulan PSL v2 for more details. ++ */ ++ ++#ifndef _TEE_CLIENT_TYPE_H_ ++#define _TEE_CLIENT_TYPE_H_ ++ ++#include ++#include ++#include ++#include ++#include ++#include "tee_client_list.h" ++#include "tee_client_constants.h" ++ ++typedef enum TEEC_ReturnCode TEEC_Result; ++ ++typedef struct { ++ uint32_t timeLow; ++ uint16_t timeMid; ++ uint16_t timeHiAndVersion; ++ uint8_t clockSeqAndNode[8]; ++} TEEC_UUID; ++ ++typedef struct { ++ int32_t fd; ++ uint8_t *ta_path; ++ struct ListNode session_list; ++ struct ListNode shrd_mem_list; ++ union { ++ struct { ++ void *buffer; ++ sem_t buffer_barrier; ++ } share_buffer; ++ uint64_t imp; /* for adapt */ ++ }; ++} TEEC_Context; ++ ++typedef struct { ++ uint32_t session_id; ++ TEEC_UUID service_id; ++ uint32_t ops_cnt; ++ union { ++ struct ListNode head; ++ uint64_t imp; /* for adapt */ ++ }; ++ TEEC_Context *context; ++} TEEC_Session; ++ ++typedef struct { ++ void *buffer; ++ uint32_t size; ++ uint32_t flags; /* reference to TEEC_SharedMemCtl */ ++ uint32_t ops_cnt; ++ bool is_allocated; /* identify whether the memory is registered or allocated */ ++ union { ++ struct ListNode head; ++ void* imp; /* for adapt, imp is not used by system CA, only for vendor CA */ ++ }; ++ TEEC_Context *context; ++} TEEC_SharedMemory; ++ ++/* ++ * the corresponding param types are ++ * TEEC_MEMREF_TEMP_INPUT/TEEC_MEMREF_TEMP_OUTPUT/TEEC_MEMREF_TEMP_INOUT ++ */ ++typedef struct { ++ void *buffer; ++ uint32_t size; ++} TEEC_TempMemoryReference; ++ ++/* ++ * the corresponding param types are ++ * TEEC_MEMREF_WHOLE/TEEC_MEMREF_PARTIAL_INPUT ++ * TEEC_MEMREF_PARTIAL_OUTPUT/TEEC_MEMREF_PARTIAL_INOUT ++ */ ++typedef struct { ++ TEEC_SharedMemory *parent; ++ uint32_t size; ++ uint32_t offset; ++} TEEC_RegisteredMemoryReference; ++ ++/* ++ * the corresponding param types are ++ * TEEC_VALUE_INPUT/TEEC_VALUE_OUTPUT/TEEC_VALUE_INOUT ++ */ ++typedef struct { ++ uint32_t a; ++ uint32_t b; ++} TEEC_Value; ++ ++typedef struct { ++ int ion_share_fd; ++ uint32_t ion_size; ++} TEEC_IonReference; ++ ++typedef union { ++ TEEC_TempMemoryReference tmpref; ++ TEEC_RegisteredMemoryReference memref; ++ TEEC_Value value; ++ TEEC_IonReference ionref; ++} TEEC_Parameter; ++ ++typedef struct { ++ uint32_t event_type; /* Tui event type */ ++ uint32_t value; /* return value, is keycode if tui event is getKeycode */ ++ uint32_t notch; /* notch size of the screen for tui */ ++ uint32_t width; /* width of foldable screen */ ++ uint32_t height; /* height of foldable screen */ ++ uint32_t fold_state; /* state of foldable screen */ ++ uint32_t display_state; /* one state of folded state */ ++ uint32_t phy_width; /* real width of the mobile */ ++ uint32_t phy_height; /* real height of the mobile */ ++} TEEC_TUI_Parameter; ++ ++typedef struct { ++ uint32_t started; /* 0 means cancel this operation, others mean to perform this operation */ ++ uint32_t paramTypes; /* use TEEC_PARAM_TYPES to construct this value */ ++ TEEC_Parameter params[TEEC_PARAM_NUM]; ++ TEEC_Session *session; ++ bool cancel_flag; ++} TEEC_Operation; ++ ++#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 +@@ -22,6 +22,13 @@ + #include "qapi/qapi-events-char.h" + #include "qom/object.h" + + ++#include "hw/core/cpu.h" ++#include "sysemu/hw_accel.h" ++#include "monitor/monitor.h" ++#include ++#include ++#include "tc_ns_client.h" ++ + #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 + return FALSE; + } + + ++//#define DEBUG 1 ++ ++#ifdef DEBUG ++static void debug(const char *fmt, ...) ++{ ++ va_list args; ++ ++ va_start(args, fmt); ++ vfprintf(stderr, fmt, args); ++ va_end(args); ++} ++ ++#define PRINTF_SIZE 16 ++static void dump_buff(const char *buffer, size_t bufLen) ++{ ++ size_t i; ++ if (buffer == NULL || bufLen == 0) { ++ return; ++ } ++ ++ // printf("\n--------------------------------------------------\n"); ++ printf("--------------------------------------------------\n"); ++ printf("bufLen = %d\n", (int)bufLen); ++ for (i = 0; i < bufLen; i++) { ++ if (i % PRINTF_SIZE == 0 && i != 0) { ++ printf("\n"); ++ } ++ printf("%02x ", *(buffer + i)); ++ } ++ printf("\n--------------------------------------------------\n"); ++ return; ++} ++#else ++#define debug(fmt, ...) do { } while (0) ++ ++#define dump_buff(buffer, bufLen) do { } while (0) ++#endif ++ ++#define VTZF_OPEN_TZD 15 ++#define VTZF_OPEN_SESSION 31 ++#define VTZF_SEND_CMD 33 ++#define VTZF_FS_REGISTER_AGENT 45 ++#define VTZF_LOAD_SEC 53 ++ ++#define TEEC_PARAM_NUM 4 /* teec param max number */ ++ ++#define IS_TEMP_MEM(paramType) \ ++ (((paramType) == TEEC_MEMREF_TEMP_INPUT) || ((paramType) == TEEC_MEMREF_TEMP_OUTPUT) || \ ++ ((paramType) == TEEC_MEMREF_TEMP_INOUT)) ++ ++#define IS_PARTIAL_MEM(paramType) \ ++ (((paramType) == TEEC_MEMREF_WHOLE) || ((paramType) == TEEC_MEMREF_PARTIAL_INPUT) || \ ++ ((paramType) == TEEC_MEMREF_PARTIAL_OUTPUT) || ((paramType) == TEEC_MEMREF_PARTIAL_INOUT)) ++ ++#define IS_VALUE_MEM(paramType) \ ++ (((paramType) == TEEC_VALUE_INPUT) || ((paramType) == TEEC_VALUE_OUTPUT) || ((paramType) == TEEC_VALUE_INOUT)) ++ ++#define TEEC_PARAM_TYPE_GET(paramTypes, index) \ ++ (((paramTypes) >> (4*(index))) & 0x0F) ++ ++typedef struct { ++ uint32_t packet_size; ++ uint32_t cmd; ++ uint32_t seq_num; ++ uint32_t vmid; ++ uint32_t flag; ++} struct_packet_cmd_open_tzd; ++ ++typedef struct { ++ uint32_t packet_size; ++ uint32_t cmd; ++ uint32_t seq_num; ++ int32_t ptzfd; ++ void *vmaddr; ++ struct AgentIoctlArgs args; ++} struct_packet_cmd_regagent; ++ ++typedef struct { ++ uint32_t packet_size; ++ uint32_t cmd; ++ uint32_t seq_num; ++ int32_t ptzfd; ++ int32_t cpu_index; ++ struct SecLoadIoctlStruct ioctlArg; ++} struct_packet_cmd_load_sec; ++ ++typedef struct { ++ uint32_t packet_size; ++ uint32_t cmd; ++ uint32_t seq_num; ++ int32_t ptzfd; ++ int32_t cpu_index; ++ TC_NS_ClientContext cliContext; ++} struct_packet_cmd_session; ++ ++typedef struct { ++ uint32_t packet_size; ++ uint32_t cmd; ++ uint32_t seq_num; ++ int32_t ptzfd; ++ 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_send_cmd; ++ ++typedef struct { ++ uint32_t packet_size; ++ uint32_t cmd; ++ uint32_t seq_num; ++ int32_t ptzfd; ++ uint64_t buffer; ++ uint32_t size; ++ uint32_t offset; ++} struct_packet_cmd_mmap; ++ ++#define FRAG_FLAG 0xAEAE ++ ++typedef struct { ++ uint64_t phy_addr; ++ uint32_t page_num; ++ uint32_t frag_flag; ++}struct_page_block; ++ + /* Callback function that's called when the guest sends us data */ + static ssize_t flush_buf(VirtIOSerialPort *port, + const uint8_t *buf, ssize_t len) + { + VirtConsole *vcon = VIRTIO_CONSOLE(port); + ssize_t ret; +- ++ int i = 0; ++ uint32_t j = 0; ++ uint32_t fragment_block_num = 0; ++ struct_page_block *page_block; ++ hwaddr gpa_param; ++ Error *local_err = NULL; ++ MemoryRegion *mr = NULL; ++ void *ptr_hva = NULL; ++ uint32_t offset = sizeof(struct_packet_cmd_send_cmd); + if (!qemu_chr_fe_backend_connected(&vcon->chr)) { + /* If there's no backend, we can just say we consumed all data. */ + return len; + } + + ++ debug("\n"); ++ debug("debug, %s, %s, %d \n", __FILE__, __func__, __LINE__); ++ debug(" virtio-console virtserialport name = %s, id = %d \n", port->name, (int)port->id); ++ debug(" have_data flush_buf, buflen = %d \n", len); ++ dump_buff((char *)buf, 0); ++ ++ if ( len >= 8 ) { ++ uint32_t ui32_cmd = 0; ++ ui32_cmd = *(uint32_t *)((char *)buf + sizeof(uint32_t)); ++ switch( ui32_cmd ) { ++ case VTZF_OPEN_TZD: ++ debug(" command is VTZF_OPEN_TZD \n"); ++ if ( len >= sizeof(struct_packet_cmd_open_tzd)) { ++ struct_packet_cmd_open_tzd* vtzf_packet_cmd = (struct_packet_cmd_open_tzd *)buf; ++ pid_t qemu_pid = getpid(); ++ debug(" qemu_pid = 0x%016lx, %d \n",qemu_pid, qemu_pid); ++ vtzf_packet_cmd->vmid = qemu_pid; ++ } ++ break; ++ case VTZF_LOAD_SEC: ++ debug(" command is VTZF_LOAD_SEC \n"); ++ if (len >= sizeof(struct_packet_cmd_load_sec)) { ++ struct_packet_cmd_load_sec* vtzf_packet_cmd = (struct_packet_cmd_load_sec *)buf; ++ debug(" vtzf_packet_cmd->cliContext.file_buffer = 0x%016lx \n", vtzf_packet_cmd->ioctlArg.fileBuffer); ++ hwaddr gpa = (uint64_t)vtzf_packet_cmd->ioctlArg.fileBuffer; ++ ptr_hva = gpa2hva(&mr, gpa, 1, &local_err); ++ if (local_err) { ++ debug(" gpa2hva failed \n"); ++ } else { ++ debug(" host virtual address of file_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->ioctlArg.fileBuffer = (void *)ui64_hva; ++ } ++ } ++ break; ++ case VTZF_FS_REGISTER_AGENT: ++ debug(" command is VTZF_FS_REGISTER_AGENT \n"); ++ if (len >= sizeof(struct_packet_cmd_regagent)) { ++ struct_packet_cmd_regagent* vtzf_packet_cmd = (struct_packet_cmd_regagent *)buf; ++ debug(" vtzf_packet_cmd->cliContext.file_buffer = 0x%016lx \n", vtzf_packet_cmd->vmaddr); ++ hwaddr gpa = (uint64_t)vtzf_packet_cmd->vmaddr; ++ ptr_hva = gpa2hva(&mr, gpa, 1, &local_err); ++ if (local_err) { ++ debug(" gpa2hva failed \n"); ++ } else { ++ debug(" host virtual address of vmaddr = 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->vmaddr = (void *)ui64_hva; ++ } ++ } ++ break; ++ case VTZF_OPEN_SESSION: ++ debug(" command is VTZF_OPEN_SESSION \n"); ++ debug("sizeof(struct_packet_cmd_session) =%d \n", sizeof(struct_packet_cmd_session)); ++ 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); ++ hwaddr gpa = (uint64_t)vtzf_packet_cmd->cliContext.file_buffer; ++ ptr_hva = gpa2hva(&mr, gpa, 1, &local_err); ++ if (local_err) { ++ debug(" gpa2hva failed \n"); ++ } else { ++ debug(" host virtual address of file_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.file_buffer = (void *)ui64_hva; ++ } ++ } ++ break; ++ case VTZF_SEND_CMD: ++ debug(" command is VTZF_SEND_CMD \n"); ++ if ( len >= sizeof(struct_packet_cmd_send_cmd) ) { ++ struct_packet_cmd_send_cmd* vtzf_packet_cmd = (struct_packet_cmd_send_cmd *)buf; ++ 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; ++ } ++ 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) { ++ /* 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 + offset); ++ 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; ++ } ++ } ++ } ++ }//end if ++ break; ++ default: ++ if (len >= sizeof(struct_page_block)) { ++ page_block = (struct_page_block *)buf; ++ if (page_block->frag_flag == FRAG_FLAG && len % sizeof(struct_page_block) == 0) { ++ fragment_block_num = len / sizeof(struct_page_block); ++ page_block->frag_flag = 0; ++ 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; ++ } ++ } ++ } ++ } ++ debug(" other command \n"); ++ } ++ ++ } // end of if ( len >= 4 ) ++ + ret = qemu_chr_fe_write(&vcon->chr, buf, len); + trace_virtio_console_flush_buf(port->id, len, ret); \ No newline at end of file