1 /*
2  * Copyright (C) 2022 Huawei Technologies Co., Ltd.
3  * Decription: function for proc open,close session and invoke.
4  *
5  * This software is licensed under the terms of the GNU General Public
6  * License version 2, as published by the Free Software Foundation, and
7  * may be copied, distributed, and modified under those terms.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  */
14 #include "tc_client_driver.h"
15 #include <linux/slab.h>
16 #include <linux/types.h>
17 #include <linux/fs.h>
18 #include <linux/platform_device.h>
19 #include <linux/spinlock_types.h>
20 #include <linux/spinlock.h>
21 #include <linux/uaccess.h>
22 #include <linux/sched.h>
23 #include <asm/cacheflush.h>
24 #include <linux/kthread.h>
25 #include <linux/module.h>
26 #include <linux/of.h>
27 #include <linux/of_address.h>
28 #include <linux/of_device.h>
29 #include <linux/of_platform.h>
30 #include <linux/of_irq.h>
31 #include <linux/of_reserved_mem.h>
32 #include <linux/atomic.h>
33 #include <linux/interrupt.h>
34 #include <linux/version.h>
35 #include <linux/vmalloc.h>
36 #include <linux/pid.h>
37 #include <linux/security.h>
38 #include <linux/cred.h>
39 #include <linux/namei.h>
40 #include <linux/thread_info.h>
41 #include <linux/highmem.h>
42 #include <linux/mm.h>
43 #include <linux/kernel.h>
44 #include <linux/file.h>
45 #include <linux/err.h>
46 #include <linux/slab.h>
47 #include <linux/types.h>
48 #include <linux/security.h>
49 #if (KERNEL_VERSION(4, 14, 0) <= LINUX_VERSION_CODE)
50 #include <linux/sched/mm.h>
51 #include <linux/sched/signal.h>
52 #include <linux/sched/task.h>
53 #endif
54 #include <linux/acpi.h>
55 #include <linux/completion.h>
56 #include <linux/bitmap.h>
57 #include <securec.h>
58 #include "smc_smp.h"
59 #include "teek_client_constants.h"
60 #include "agent.h"
61 #include "mem.h"
62 #include "gp_ops.h"
63 #include "tc_ns_log.h"
64 #include "tc_ns_client.h"
65 #include "mailbox_mempool.h"
66 #include "shared_mem.h"
67 #include "tz_spi_notify.h"
68 #include "client_hash_auth.h"
69 #include "auth_base_impl.h"
70 #include "tlogger.h"
71 #include "tzdebug.h"
72 #include "session_manager.h"
73 #include "internal_functions.h"
74 #include "ko_adapt.h"
75 #include "tz_pm.h"
76 #include "tui.h"
77 #include "dynamic_ion_mem.h"
78 #include "static_ion_mem.h"
79 #include "reserved_mempool.h"
80 #ifdef CONFIG_CMS_SIGNATURE
81 #include "tz_update_crl.h"
82 #endif
83 #ifdef CONFIG_TEE_REBOOT
84 #include "reboot.h"
85 #endif
86 
87 #ifdef CONFIG_FFA_SUPPORT
88 #include "ffa_abi.h"
89 #endif
90 
91 static struct class *g_driver_class;
92 static struct device_node *g_dev_node;
93 
94 struct dev_node g_tc_client;
95 struct dev_node g_tc_private;
96 
97 #ifdef CONFIG_ACPI
98 static int g_acpi_irq;
99 #endif
100 
101 static DEFINE_MUTEX(g_set_ca_hash_lock);
102 
103 /* dev_file_id rangde in (0, 32767), 32768 use 4k bitmap */
104 #define DEV_FILE_ID_MAX 32768u
105 
106 unsigned long *g_dev_bit_map = NULL;
107 static DEFINE_MUTEX(g_dev_bit_map_lock);
108 
alloc_dev_bitmap(void)109 static int alloc_dev_bitmap(void)
110 {
111 	mutex_lock(&g_dev_bit_map_lock);
112 	if (g_dev_bit_map == NULL) {
113 		g_dev_bit_map = bitmap_alloc(DEV_FILE_ID_MAX, GFP_KERNEL | __GFP_ZERO);
114 		if (g_dev_bit_map == NULL) {
115 			tloge("alloc bit map failed\n");
116 			mutex_unlock(&g_dev_bit_map_lock);
117 			return -1;
118 		}
119 	}
120 
121 	mutex_unlock(&g_dev_bit_map_lock);
122 	return 0;
123 }
124 
free_dev_bitmap(void)125 static void free_dev_bitmap(void)
126 {
127 	mutex_lock(&g_dev_bit_map_lock);
128 	if (g_dev_bit_map != NULL) {
129 		bitmap_free(g_dev_bit_map);
130 		g_dev_bit_map = NULL;
131 	}
132 	mutex_unlock(&g_dev_bit_map_lock);
133 }
134 
alloc_dev_file_id(unsigned int *dev_file_id)135 static bool alloc_dev_file_id(unsigned int *dev_file_id)
136 {
137 	int pos;
138 
139 	mutex_lock(&g_dev_bit_map_lock);
140 	if (dev_file_id == NULL || g_dev_bit_map == NULL) {
141 		tloge("invalid param\n");
142 		mutex_unlock(&g_dev_bit_map_lock);
143 		return false;
144 	}
145 
146 	pos = bitmap_find_free_region(g_dev_bit_map, DEV_FILE_ID_MAX, 0);
147 	if (pos < 0) {
148 		tloge("dev file fd full, alloc failed, error = %d\n", pos);
149 		mutex_unlock(&g_dev_bit_map_lock);
150 		return false;
151 	}
152 
153 	*dev_file_id = (unsigned int)pos;
154 	tlogd("alloc dev file id = %u", *dev_file_id);
155 	mutex_unlock(&g_dev_bit_map_lock);
156 	return true;
157 }
158 
free_dev_file_id(unsigned int dev_file_id)159 static void free_dev_file_id(unsigned int dev_file_id)
160 {
161 	mutex_lock(&g_dev_bit_map_lock);
162 	if (g_dev_bit_map == NULL) {
163 		tloge("dev file fd bitmap is null\n");
164 		mutex_unlock(&g_dev_bit_map_lock);
165 		return;
166 	}
167 
168 	if (dev_file_id >= DEV_FILE_ID_MAX) {
169 		tloge("dev file fd invalid\n");
170 		mutex_unlock(&g_dev_bit_map_lock);
171 		return;
172 	}
173 
174 	/* clear dev_file_id bit for reuse */
175 	bitmap_release_region(g_dev_bit_map, dev_file_id, 0);
176 	tlogd("clear dev file id %u\n", dev_file_id);
177 	mutex_unlock(&g_dev_bit_map_lock);
178 }
179 
180 /* dev node list and itself has mutex to avoid race */
181 struct tc_ns_dev_list g_tc_ns_dev_list;
182 
183 static bool g_init_succ = false;
184 
set_tz_init_flag(void)185 static void set_tz_init_flag(void)
186 {
187 	g_init_succ = true;
188 }
189 
clear_tz_init_flag(void)190 static void clear_tz_init_flag(void)
191 {
192 	g_init_succ = false;
193 }
194 
get_tz_init_flag(void)195 bool get_tz_init_flag(void)
196 {
197 	return g_init_succ;
198 }
199 
get_dev_list(void)200 struct tc_ns_dev_list *get_dev_list(void)
201 {
202 	return &g_tc_ns_dev_list;
203 }
204 
tc_ns_get_tee_version(const struct tc_ns_dev_file *dev_file, void __user *argp)205 static int tc_ns_get_tee_version(const struct tc_ns_dev_file *dev_file,
206 	void __user *argp)
207 {
208 	unsigned int version;
209 	struct tc_ns_smc_cmd smc_cmd = { {0}, 0 };
210 	int ret = 0;
211 	struct mb_cmd_pack *mb_pack = NULL;
212 
213 	if (!argp) {
214 		tloge("error input parameter\n");
215 		return -EINVAL;
216 	}
217 
218 	mb_pack = mailbox_alloc_cmd_pack();
219 	if (!mb_pack) {
220 		tloge("alloc mb pack failed\n");
221 		return -ENOMEM;
222 	}
223 
224 	mb_pack->operation.paramtypes = TEEC_VALUE_OUTPUT;
225 	smc_cmd.cmd_type = CMD_TYPE_GLOBAL;
226 	smc_cmd.cmd_id = GLOBAL_CMD_ID_GET_TEE_VERSION;
227 	smc_cmd.dev_file_id = dev_file->dev_file_id;
228 	smc_cmd.operation_phys = mailbox_virt_to_phys((uintptr_t)&mb_pack->operation);
229 	smc_cmd.operation_h_phys =
230 		(uint64_t)mailbox_virt_to_phys((uintptr_t)&mb_pack->operation) >> ADDR_TRANS_NUM;
231 
232 	if (tc_ns_smc(&smc_cmd) != 0) {
233 		ret = -EPERM;
234 		tloge("smc call returns error ret 0x%x\n", smc_cmd.ret_val);
235 	}
236 
237 	version = mb_pack->operation.params[0].value.a;
238 	if (copy_to_user(argp, &version, sizeof(unsigned int)) != 0)
239 		ret = -EFAULT;
240 	mailbox_free(mb_pack);
241 
242 	return ret;
243 }
244 
245 /*
246  * This is the login information
247  * and is set teecd when client opens a new session
248  */
249 #define MAX_BUF_LEN 4096
250 
get_pack_name_len(struct tc_ns_dev_file *dev_file, const uint8_t *cert_buffer)251 static int get_pack_name_len(struct tc_ns_dev_file *dev_file,
252 	const uint8_t *cert_buffer)
253 {
254 	uint32_t tmp_len = 0;
255 
256 	dev_file->pkg_name_len = 0;
257 	if (memcpy_s(&tmp_len, sizeof(tmp_len), cert_buffer, sizeof(tmp_len)) != 0)
258 		return -EFAULT;
259 
260 	if (tmp_len == 0 || tmp_len >= MAX_PACKAGE_NAME_LEN) {
261 		tloge("invalid pack name len: %u\n", tmp_len);
262 		return -EINVAL;
263 	}
264 	dev_file->pkg_name_len = tmp_len;
265 	tlogd("package name len is %u\n", dev_file->pkg_name_len);
266 
267 	return 0;
268 }
269 
get_public_key_len(struct tc_ns_dev_file *dev_file, const uint8_t *cert_buffer)270 static int get_public_key_len(struct tc_ns_dev_file *dev_file,
271 	const uint8_t *cert_buffer)
272 {
273 	uint32_t tmp_len = 0;
274 
275 	dev_file->pub_key_len = 0;
276 	if (memcpy_s(&tmp_len, sizeof(tmp_len), cert_buffer, sizeof(tmp_len)) != 0)
277 		return -EFAULT;
278 
279 	if (tmp_len > MAX_PUBKEY_LEN) {
280 		tloge("invalid public key len: %u\n", tmp_len);
281 		return -EINVAL;
282 	}
283 	dev_file->pub_key_len = tmp_len;
284 	tlogd("publick key len is %u\n", dev_file->pub_key_len);
285 
286 	return 0;
287 }
288 
get_public_key(struct tc_ns_dev_file *dev_file, const uint8_t *cert_buffer)289 static int get_public_key(struct tc_ns_dev_file *dev_file,
290 	const uint8_t *cert_buffer)
291 {
292 	/* get public key */
293 	if (dev_file->pub_key_len == 0)
294 		return 0;
295 
296 	if (memcpy_s(dev_file->pub_key, MAX_PUBKEY_LEN, cert_buffer,
297 		dev_file->pub_key_len) != 0) {
298 		tloge("failed to copy pub key len\n");
299 		return -EINVAL;
300 	}
301 
302 	return 0;
303 }
304 
is_cert_buffer_size_valid(int cert_buffer_size)305 static bool is_cert_buffer_size_valid(int cert_buffer_size)
306 {
307 	/*
308 	 * GET PACKAGE NAME AND APP CERTIFICATE:
309 	 * The proc_info format is as follows:
310 	 * package_name_len(4 bytes) || package_name ||
311 	 * apk_cert_len(4 bytes) || apk_cert.
312 	 * or package_name_len(4 bytes) || package_name
313 	 * || exe_uid_len(4 bytes) || exe_uid.
314 	 * The apk certificate format is as follows:
315 	 * modulus_size(4bytes) ||modulus buffer
316 	 * || exponent size || exponent buffer
317 	 */
318 	if (cert_buffer_size > MAX_BUF_LEN || cert_buffer_size == 0) {
319 		tloge("cert buffer size is invalid!\n");
320 		return false;
321 	}
322 
323 	return true;
324 }
325 
alloc_login_buf(struct tc_ns_dev_file *dev_file, uint8_t **cert_buffer, unsigned int *cert_buffer_size)326 static int alloc_login_buf(struct tc_ns_dev_file *dev_file,
327 	uint8_t **cert_buffer, unsigned int *cert_buffer_size)
328 {
329 	*cert_buffer_size = (unsigned int)(MAX_PACKAGE_NAME_LEN +
330 		MAX_PUBKEY_LEN + sizeof(dev_file->pkg_name_len) +
331 		sizeof(dev_file->pub_key_len));
332 
333 	*cert_buffer = kmalloc(*cert_buffer_size, GFP_KERNEL);
334 	if (ZERO_OR_NULL_PTR((unsigned long)(uintptr_t)(*cert_buffer))) {
335 		tloge("failed to allocate login buffer!");
336 		return -ENOMEM;
337 	}
338 
339 	return 0;
340 }
341 
client_login_prepare(uint8_t *cert_buffer, const void __user *buffer, unsigned int cert_buffer_size)342 static int client_login_prepare(uint8_t *cert_buffer,
343 	const void __user *buffer, unsigned int cert_buffer_size)
344 {
345 	if (!is_cert_buffer_size_valid(cert_buffer_size))
346 		return -EINVAL;
347 
348 	if (copy_from_user(cert_buffer, buffer, cert_buffer_size) != 0) {
349 		tloge("Failed to get user login info!\n");
350 		return -EINVAL;
351 	}
352 
353 	return 0;
354 }
355 
tc_login_check(const struct tc_ns_dev_file *dev_file)356 static int tc_login_check(const struct tc_ns_dev_file *dev_file)
357 {
358 	int ret = check_teecd_auth();
359 #ifdef CONFIG_CADAEMON_AUTH
360 	if (ret != 0)
361 		ret = check_cadaemon_auth();
362 #endif
363 	if (ret != 0) {
364 		tloge("teec auth failed, ret %d\n", ret);
365 		return -EACCES;
366 	}
367 
368 	if (!dev_file)
369 		return -EINVAL;
370 
371 	return 0;
372 }
373 
tc_ns_client_login_func(struct tc_ns_dev_file *dev_file, const void __user *buffer)374 static int tc_ns_client_login_func(struct tc_ns_dev_file *dev_file,
375 	const void __user *buffer)
376 {
377 	int ret;
378 	uint8_t *cert_buffer = NULL;
379 	uint8_t *temp_cert_buffer = NULL;
380 	unsigned int cert_buffer_size = 0;
381 
382 	if (tc_login_check(dev_file) != 0)
383 		return -EFAULT;
384 
385 	if (!buffer) {
386 		/*
387 		 * We accept no debug information
388 		 * because the daemon might  have failed
389 		 */
390 		dev_file->pkg_name_len = 0;
391 		dev_file->pub_key_len = 0;
392 		return 0;
393 	}
394 
395 	mutex_lock(&dev_file->login_setup_lock);
396 	if (dev_file->login_setup) {
397 		tloge("login information cannot be set twice!\n");
398 		mutex_unlock(&dev_file->login_setup_lock);
399 		return -EINVAL;
400 	}
401 
402 	ret = alloc_login_buf(dev_file, &cert_buffer, &cert_buffer_size);
403 	if (ret != 0) {
404 		mutex_unlock(&dev_file->login_setup_lock);
405 		return ret;
406 	}
407 
408 	temp_cert_buffer = cert_buffer;
409 	if (client_login_prepare(cert_buffer, buffer, cert_buffer_size) != 0) {
410 		ret = -EINVAL;
411 		goto error;
412 	}
413 
414 	ret = get_pack_name_len(dev_file, cert_buffer);
415 	if (ret != 0)
416 		goto error;
417 	cert_buffer += sizeof(dev_file->pkg_name_len);
418 
419 	if (strncpy_s(dev_file->pkg_name, MAX_PACKAGE_NAME_LEN, cert_buffer,
420 		dev_file->pkg_name_len) != 0) {
421 		ret = -ENOMEM;
422 		goto error;
423 	}
424 	cert_buffer += dev_file->pkg_name_len;
425 
426 	ret = get_public_key_len(dev_file, cert_buffer);
427 	if (ret != 0)
428 		goto error;
429 	cert_buffer += sizeof(dev_file->pub_key_len);
430 
431 	ret = get_public_key(dev_file, cert_buffer);
432 	dev_file->login_setup = true;
433 
434 error:
435 	kfree(temp_cert_buffer);
436 	mutex_unlock(&dev_file->login_setup_lock);
437 	return ret;
438 }
439 
tc_ns_client_open(struct tc_ns_dev_file **dev_file, uint8_t kernel_api)440 int tc_ns_client_open(struct tc_ns_dev_file **dev_file, uint8_t kernel_api)
441 {
442 	struct tc_ns_dev_file *dev = NULL;
443 
444 	tlogd("tc_client_open\n");
445 	if (!dev_file) {
446 		tloge("dev_file is NULL\n");
447 		return -EINVAL;
448 	}
449 
450 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
451 	if (ZERO_OR_NULL_PTR((unsigned long)(uintptr_t)dev)) {
452 		tloge("dev malloc failed\n");
453 		return -ENOMEM;
454 	}
455 
456 	if (!alloc_dev_file_id(&(dev->dev_file_id))) {
457 		kfree(dev);
458 		return -ENOMEM;
459 	}
460 
461 	mutex_lock(&g_tc_ns_dev_list.dev_lock);
462 	list_add_tail(&dev->head, &g_tc_ns_dev_list.dev_file_list);
463 	mutex_unlock(&g_tc_ns_dev_list.dev_lock);
464 	INIT_LIST_HEAD(&dev->shared_mem_list);
465 	dev->login_setup = 0;
466 #ifdef CONFIG_AUTH_HASH
467 	dev->cainfo_hash_setup = 0;
468 #endif
469 	dev->kernel_api = kernel_api;
470 	dev->load_app_flag = 0;
471 	mutex_init(&dev->service_lock);
472 	mutex_init(&dev->shared_mem_lock);
473 	mutex_init(&dev->login_setup_lock);
474 #ifdef CONFIG_AUTH_HASH
475 	mutex_init(&dev->cainfo_hash_setup_lock);
476 #endif
477 	init_completion(&dev->close_comp);
478 	*dev_file = dev;
479 
480 	return 0;
481 }
482 
del_dev_node(struct tc_ns_dev_file *dev)483 static void del_dev_node(struct tc_ns_dev_file *dev)
484 {
485 	if (!dev)
486 		return;
487 
488 	mutex_lock(&g_tc_ns_dev_list.dev_lock);
489 	list_del(&dev->head);
490 	mutex_unlock(&g_tc_ns_dev_list.dev_lock);
491 }
492 
free_dev(struct tc_ns_dev_file *dev)493 void free_dev(struct tc_ns_dev_file *dev)
494 {
495 	del_dev_node(dev);
496 	tee_agent_clear_dev_owner(dev);
497 	free_dev_file_id(dev->dev_file_id);
498 	if (memset_s(dev, sizeof(*dev), 0, sizeof(*dev)) != 0)
499 		tloge("Caution, memset dev fail!\n");
500 	kfree(dev);
501 }
502 
tc_ns_client_close(struct tc_ns_dev_file *dev)503 int tc_ns_client_close(struct tc_ns_dev_file *dev)
504 {
505 	if (!dev) {
506 		tloge("invalid dev(null)\n");
507 		return -EINVAL;
508 	}
509 
510 	close_unclosed_session_in_kthread(dev);
511 
512 	if (dev->dev_file_id == tui_attach_device())
513 		free_tui_caller_info();
514 
515 	kill_ion_by_cafd(dev->dev_file_id);
516 	/* for thirdparty agent, code runs here only when agent crashed */
517 	send_crashed_event_response_all(dev);
518 	free_dev(dev);
519 
520 	return 0;
521 }
522 
shared_vma_open(struct vm_area_struct *vma)523 void shared_vma_open(struct vm_area_struct *vma)
524 {
525 	(void)vma;
526 }
527 
shared_vma_close(struct vm_area_struct *vma)528 void shared_vma_close(struct vm_area_struct *vma)
529 {
530 	struct tc_ns_shared_mem *shared_mem = NULL;
531 	struct tc_ns_shared_mem *shared_mem_temp = NULL;
532 	bool find = false;
533 	struct tc_ns_dev_file *dev_file = NULL;
534 	if (!vma) {
535 		tloge("vma is null\n");
536 		return;
537 	}
538 	dev_file = vma->vm_private_data;
539 	if (!dev_file) {
540 		tloge("vm private data is null\n");
541 		return;
542 	}
543 
544 	mutex_lock(&dev_file->shared_mem_lock);
545 	list_for_each_entry_safe(shared_mem, shared_mem_temp,
546 			&dev_file->shared_mem_list, head) {
547 		if (shared_mem) {
548 			if (shared_mem->user_addr ==
549 				(void *)(uintptr_t)vma->vm_start) {
550 				shared_mem->user_addr = INVALID_MAP_ADDR;
551 				find = true;
552 			} else if (shared_mem->user_addr_ca ==
553 				(void *)(uintptr_t)vma->vm_start) {
554 				shared_mem->user_addr_ca = INVALID_MAP_ADDR;
555 				find = true;
556 			}
557 
558 			if ((shared_mem->user_addr == INVALID_MAP_ADDR) &&
559 				(shared_mem->user_addr_ca == INVALID_MAP_ADDR))
560 				list_del(&shared_mem->head);
561 
562 			/* pair with tc client mmap */
563 			if (find) {
564 				put_sharemem_struct(shared_mem);
565 				break;
566 			}
567 		}
568 	}
569 	mutex_unlock(&dev_file->shared_mem_lock);
570 }
571 
572 static struct vm_operations_struct g_shared_remap_vm_ops = {
573 	.open = shared_vma_open,
574 	.close = shared_vma_close,
575 };
576 
find_sharedmem( const struct vm_area_struct *vma, const struct tc_ns_dev_file *dev_file, bool *only_remap)577 static struct tc_ns_shared_mem *find_sharedmem(
578 	const struct vm_area_struct *vma,
579 	const struct tc_ns_dev_file *dev_file, bool *only_remap)
580 {
581 	struct tc_ns_shared_mem *shm_tmp = NULL;
582 	unsigned long len = vma->vm_end - vma->vm_start;
583 
584 	/*
585 	 * using vma->vm_pgoff as share_mem index
586 	 * check if aready allocated
587 	 */
588 	list_for_each_entry(shm_tmp, &dev_file->shared_mem_list, head) {
589 		if ((unsigned long)atomic_read(&shm_tmp->offset) == vma->vm_pgoff) {
590 			tlogd("sharemem already alloc, shm tmp->offset=%d\n",
591 				atomic_read(&shm_tmp->offset));
592 			/*
593 			 * args check:
594 			 * 1. this shared mem is already mapped
595 			 * 2. remap a different size shared_mem
596 			 */
597 			if ((shm_tmp->user_addr_ca != INVALID_MAP_ADDR) ||
598 				(vma->vm_end - vma->vm_start != shm_tmp->len)) {
599 				tloge("already remap once!\n");
600 				return NULL;
601 			}
602 			/* return the same sharedmem specified by vm_pgoff */
603 			*only_remap = true;
604 			get_sharemem_struct(shm_tmp);
605 			return shm_tmp;
606 		}
607 	}
608 
609 	/* if not find, alloc a new sharemem */
610 	return tc_mem_allocate(len);
611 }
612 
remap_shared_mem(struct vm_area_struct *vma, const struct tc_ns_shared_mem *shared_mem)613 static int remap_shared_mem(struct vm_area_struct *vma,
614 	const struct tc_ns_shared_mem *shared_mem)
615 {
616 	int ret;
617 #ifdef CONFIG_LIBLINUX
618 	unsigned long pa = virt_to_phys(shared_mem->kernel_addr);
619 	unsigned long va = vma->vm_start;
620 	unsigned long size = vma->vm_end - vma->vm_start;
621 
622 	pgprot_t pro;
623 	pro.pgprot = VM_READ | VM_WRITE;
624 	ret = remap_pfn_range(NULL, va, pa >> PAGE_SHIFT, size, pro);
625 #else
626 	if (shared_mem->mem_type == RESERVED_TYPE) {
627 		unsigned long pfn = res_mem_virt_to_phys((uintptr_t)(shared_mem->kernel_addr)) >> PAGE_SHIFT;
628 		unsigned long size = vma->vm_end - vma->vm_start;
629 		ret = remap_pfn_range(vma, vma->vm_start, pfn, size, vma->vm_page_prot); // PAGE_SHARED
630 		if (ret != 0)
631 			tloge("remap pfn for user failed, ret %d", ret);
632 		return ret;
633 	}
634 #if (KERNEL_VERSION(6, 4, 0) <= LINUX_VERSION_CODE)
635 	vma->__vm_flags |= VM_USERMAP;
636 #else
637 	vma->vm_flags |= VM_USERMAP;
638 #endif
639 	ret = remap_vmalloc_range(vma, shared_mem->kernel_addr, 0);
640 #endif
641 	if (ret != 0)
642 		tloge("can't remap to user, ret = %d\n", ret);
643 
644 	return ret;
645 }
646 
647 /*
648  * in this func, we need to deal with follow cases:
649  * vendor CA alloc sharedmem (alloc and remap);
650  * HIDL alloc sharedmem (alloc and remap);
651  * system CA alloc sharedmem (only just remap);
652  */
tc_client_mmap(struct file *filp, struct vm_area_struct *vma)653 static int tc_client_mmap(struct file *filp, struct vm_area_struct *vma)
654 {
655 	int ret;
656 	struct tc_ns_dev_file *dev_file = NULL;
657 	struct tc_ns_shared_mem *shared_mem = NULL;
658 	bool only_remap = false;
659 
660 	if (!filp || !vma || !filp->private_data) {
661 		tloge("invalid args for tc mmap\n");
662 		return -EINVAL;
663 	}
664 	dev_file = filp->private_data;
665 
666 	mutex_lock(&dev_file->shared_mem_lock);
667 	shared_mem = find_sharedmem(vma, dev_file, &only_remap);
668 	if (IS_ERR_OR_NULL(shared_mem)) {
669 		tloge("alloc shared mem failed\n");
670 		mutex_unlock(&dev_file->shared_mem_lock);
671 		return -ENOMEM;
672 	}
673 
674 	ret = remap_shared_mem(vma, shared_mem);
675 	if (ret != 0) {
676 		if (only_remap)
677 			put_sharemem_struct(shared_mem);
678 		else
679 			tc_mem_free(shared_mem);
680 		mutex_unlock(&dev_file->shared_mem_lock);
681 		return ret;
682 	}
683 #if (KERNEL_VERSION(6, 4, 0) <= LINUX_VERSION_CODE)
684 	vma->__vm_flags |= VM_DONTCOPY;
685 #else
686 	vma->vm_flags |= VM_DONTCOPY;
687 #endif
688 	vma->vm_ops = &g_shared_remap_vm_ops;
689 	shared_vma_open(vma);
690 	vma->vm_private_data = (void *)dev_file;
691 
692 	if (only_remap) {
693 		shared_mem->user_addr_ca = (void *)(uintptr_t)vma->vm_start;
694 		mutex_unlock(&dev_file->shared_mem_lock);
695 		return ret;
696 	}
697 	shared_mem->user_addr = (void *)(uintptr_t)vma->vm_start;
698 	atomic_set(&shared_mem->offset, vma->vm_pgoff);
699 	get_sharemem_struct(shared_mem);
700 	list_add_tail(&shared_mem->head, &dev_file->shared_mem_list);
701 	mutex_unlock(&dev_file->shared_mem_lock);
702 
703 	return ret;
704 }
705 
ioctl_register_agent(struct tc_ns_dev_file *dev_file, unsigned long arg)706 static int ioctl_register_agent(struct tc_ns_dev_file *dev_file, unsigned long arg)
707 {
708 	int ret;
709 	struct agent_ioctl_args args;
710 
711 	if (arg == 0) {
712 		tloge("arg is NULL\n");
713 		return -EFAULT;
714 	}
715 
716 	if (copy_from_user(&args, (void *)(uintptr_t)arg, sizeof(args)) != 0) {
717 		tloge("copy agent args failed\n");
718 		return -EFAULT;
719 	}
720 
721 	ret = tc_ns_register_agent(dev_file, args.id, args.buffer_size,
722 		&args.buffer, true);
723 	if (ret == 0) {
724 		if (copy_to_user((void *)(uintptr_t)arg, &args, sizeof(args)) != 0)
725 			tloge("copy agent user addr failed\n");
726 	}
727 
728 	return ret;
729 }
730 
ioctl_check_agent_owner(const struct tc_ns_dev_file *dev_file, unsigned int agent_id)731 static int ioctl_check_agent_owner(const struct tc_ns_dev_file *dev_file,
732 	unsigned int agent_id)
733 {
734 	struct smc_event_data *event_data = NULL;
735 
736 	event_data = find_event_control(agent_id);
737 	if (event_data == NULL) {
738 		tloge("invalid agent id\n");
739 		return -EINVAL;
740 	}
741 
742 	if (event_data->owner != dev_file) {
743 		tloge("invalid request, access denied!\n");
744 		put_agent_event(event_data);
745 		return -EPERM;
746 	}
747 
748 	put_agent_event(event_data);
749 	return 0;
750 }
751 
752 /* ioctls for the secure storage daemon */
public_ioctl(const struct file *file, unsigned int cmd, unsigned long arg, bool is_from_client_node)753 static int public_ioctl(const struct file *file, unsigned int cmd, unsigned long arg, bool is_from_client_node)
754 {
755 	int ret = -EINVAL;
756 	struct tc_ns_dev_file *dev_file = file->private_data;
757 	void *argp = (void __user *)(uintptr_t)arg;
758 	if (!dev_file) {
759 		tloge("invalid params\n");
760 		return -EINVAL;
761 	}
762 
763 	switch (cmd) {
764 	case TC_NS_CLIENT_IOCTL_WAIT_EVENT:
765 		if (ioctl_check_agent_owner(dev_file, (unsigned int)arg) != 0)
766 			return -EINVAL;
767 		ret = tc_ns_wait_event((unsigned int)arg);
768 		break;
769 	case TC_NS_CLIENT_IOCTL_SEND_EVENT_RESPONSE:
770 		if (ioctl_check_agent_owner(dev_file, (unsigned int)arg) != 0)
771 			return -EINVAL;
772 		ret = tc_ns_send_event_response((unsigned int)arg);
773 		break;
774 	case TC_NS_CLIENT_IOCTL_REGISTER_AGENT:
775 		ret = ioctl_register_agent(dev_file, arg);
776 		break;
777 	case TC_NS_CLIENT_IOCTL_UNREGISTER_AGENT:
778 		if (ioctl_check_agent_owner(dev_file, (unsigned int)arg) != 0)
779 			return -EINVAL;
780 		ret = tc_ns_unregister_agent((unsigned int)arg);
781 		break;
782 	case TC_NS_CLIENT_IOCTL_LOAD_APP_REQ:
783 		ret = tc_ns_load_secfile(file->private_data, argp, is_from_client_node);
784 		break;
785 	default:
786 		tloge("invalid cmd!");
787 		return ret;
788 	}
789 	tlogd("client ioctl ret = 0x%x\n", ret);
790 	return ret;
791 }
792 
tc_ns_send_cancel_cmd(struct tc_ns_dev_file *dev_file, void *argp)793 static int tc_ns_send_cancel_cmd(struct tc_ns_dev_file *dev_file, void *argp)
794 {
795 	struct tc_ns_client_context client_context = {{0}};
796 	(void)dev_file;
797 
798 	if (!argp) {
799 		tloge("argp is NULL input buffer\n");
800 		return -EINVAL;
801 	}
802 	if (copy_from_user(&client_context, argp, sizeof(client_context)) != 0) {
803 		tloge("copy from user failed\n");
804 		return -ENOMEM;
805 	}
806 
807 	client_context.returns.code = TEEC_ERROR_GENERIC;
808 	client_context.returns.origin = TEEC_ORIGIN_COMMS;
809 	tloge("not support send cancel cmd now\n");
810 	if (copy_to_user(argp, &client_context, sizeof(client_context)) != 0)
811 		return -EFAULT;
812 
813 	return 0;
814 }
815 
get_agent_id(unsigned long arg, unsigned int cmd, uint32_t *agent_id)816 static int get_agent_id(unsigned long arg, unsigned int cmd, uint32_t *agent_id)
817 {
818 	struct agent_ioctl_args args;
819 	switch (cmd) {
820 	case TC_NS_CLIENT_IOCTL_WAIT_EVENT:
821 	case TC_NS_CLIENT_IOCTL_SEND_EVENT_RESPONSE:
822 	case TC_NS_CLIENT_IOCTL_UNREGISTER_AGENT:
823 		*agent_id = (unsigned int)arg;
824 		break;
825 	case TC_NS_CLIENT_IOCTL_REGISTER_AGENT:
826 		if (copy_from_user(&args, (void *)(uintptr_t)arg, sizeof(args))) {
827 			tloge("copy agent args failed\n");
828 			return -EFAULT;
829 		}
830 		*agent_id = args.id;
831 		break;
832 	default:
833 		return -EFAULT;
834 	}
835 	return 0;
836 }
837 
tc_client_agent_ioctl(const struct file *file, unsigned int cmd, unsigned long arg)838 static int tc_client_agent_ioctl(const struct file *file, unsigned int cmd,
839 	unsigned long arg)
840 {
841 	int ret = -EFAULT;
842 	uint32_t agent_id;
843 
844 	switch (cmd) {
845 	case TC_NS_CLIENT_IOCTL_SEND_EVENT_RESPONSE:
846 	case TC_NS_CLIENT_IOCTL_WAIT_EVENT:
847 	case TC_NS_CLIENT_IOCTL_REGISTER_AGENT:
848 	case TC_NS_CLIENT_IOCTL_UNREGISTER_AGENT:
849 		if (get_agent_id(arg, cmd, &agent_id) != 0)
850 			return ret;
851 		if (check_ext_agent_access(agent_id) != 0) {
852 			tloge("the agent is not access\n");
853 			return -EPERM;
854 		}
855 		ret = public_ioctl(file, cmd, arg, true);
856 		break;
857 	default:
858 		tloge("invalid cmd 0x%x!", cmd);
859 		break;
860 	}
861 
862 	return ret;
863 }
864 
handle_cmd_prepare(unsigned int cmd)865 static void handle_cmd_prepare(unsigned int cmd)
866 {
867 	if (cmd != TC_NS_CLIENT_IOCTL_WAIT_EVENT &&
868 		cmd != TC_NS_CLIENT_IOCTL_SEND_EVENT_RESPONSE)
869 		livepatch_down_read_sem();
870 }
871 
handle_cmd_finish(unsigned int cmd)872 static void handle_cmd_finish(unsigned int cmd)
873 {
874 	if (cmd != TC_NS_CLIENT_IOCTL_WAIT_EVENT &&
875 		cmd != TC_NS_CLIENT_IOCTL_SEND_EVENT_RESPONSE)
876 		livepatch_up_read_sem();
877 }
878 
tc_private_ioctl(struct file *file, unsigned int cmd, unsigned long arg)879 static long tc_private_ioctl(struct file *file, unsigned int cmd,
880 	unsigned long arg)
881 {
882 	int ret = -EFAULT;
883 	void *argp = (void __user *)(uintptr_t)arg;
884 	handle_cmd_prepare(cmd);
885 	switch (cmd) {
886 	case TC_NS_CLIENT_IOCTL_GET_TEE_VERSION:
887 		ret = tc_ns_get_tee_version(file->private_data, argp);
888 		break;
889 	case TC_NS_CLIENT_IOCTL_SET_NATIVECA_IDENTITY:
890 		mutex_lock(&g_set_ca_hash_lock);
891 		ret = tc_ns_set_native_hash((unsigned long)(uintptr_t)argp, GLOBAL_CMD_ID_SET_CA_HASH);
892 		mutex_unlock(&g_set_ca_hash_lock);
893 		break;
894 	case TC_NS_CLIENT_IOCTL_LATEINIT:
895 		ret = tc_ns_late_init(arg);
896 		break;
897 	case TC_NS_CLIENT_IOCTL_SYC_SYS_TIME:
898 		ret = sync_system_time_from_user(
899 			(struct tc_ns_client_time *)(uintptr_t)arg);
900 		break;
901 	default:
902 		ret = public_ioctl(file, cmd, arg, false);
903 		break;
904 	}
905 
906 	handle_cmd_finish(cmd);
907 
908 	return ret;
909 }
910 
tc_client_ioctl(struct file *file, unsigned int cmd, unsigned long arg)911 static long tc_client_ioctl(struct file *file, unsigned int cmd,
912 	unsigned long arg)
913 {
914 	int ret = -EFAULT;
915 	void *argp = (void __user *)(uintptr_t)arg;
916 
917 	handle_cmd_prepare(cmd);
918 	switch (cmd) {
919 	case TC_NS_CLIENT_IOCTL_SES_OPEN_REQ:
920 	case TC_NS_CLIENT_IOCTL_SES_CLOSE_REQ:
921 	case TC_NS_CLIENT_IOCTL_SEND_CMD_REQ:
922 		ret = tc_client_session_ioctl(file, cmd, arg);
923 		break;
924 	case TC_NS_CLIENT_IOCTL_CANCEL_CMD_REQ:
925 		ret = tc_ns_send_cancel_cmd(file->private_data, argp);
926 		break;
927 	case TC_NS_CLIENT_IOCTL_LOGIN:
928 		ret = tc_ns_client_login_func(file->private_data, argp);
929 		break;
930 	case TC_NS_CLIENT_IOCTL_LOAD_APP_REQ:
931 		ret = public_ioctl(file, cmd, arg, true);
932 		break;
933 #ifdef CONFIG_TEE_TUI
934 	case TC_NS_CLIENT_IOCTL_TUI_EVENT:
935 		ret = tc_ns_tui_event(file->private_data, argp);
936 		break;
937 #endif
938 #ifdef CONFIG_CMS_SIGNATURE
939 	case TC_NS_CLIENT_IOCTL_UPDATE_TA_CRL:
940 		ret = tc_ns_update_ta_crl(file->private_data, argp);
941 		break;
942 #endif
943 	default: {
944 		if (check_teecd_auth() == 0)
945 			ret = tc_private_ioctl(file, cmd, arg);
946 		else
947 			ret = tc_client_agent_ioctl(file, cmd, arg);
948 		break;
949 	}
950 	}
951 
952 	handle_cmd_finish(cmd);
953 
954 	tlogd("tc client ioctl ret = 0x%x\n", ret);
955 	return (long)ret;
956 }
957 
tc_client_open(struct inode *inode, struct file *file)958 static int tc_client_open(struct inode *inode, struct file *file)
959 {
960 	int ret;
961 	struct tc_ns_dev_file *dev = NULL;
962 	(void)inode;
963 
964 	ret = check_teecd_auth();
965 #ifdef CONFIG_CADAEMON_AUTH
966 	if (ret != 0)
967 		ret = check_cadaemon_auth();
968 #endif
969 	if (ret != 0) {
970 		tloge("teec auth failed, ret %d\n", ret);
971 		return -EACCES;
972 	}
973 
974 	file->private_data = NULL;
975 	ret = tc_ns_client_open(&dev, TEE_REQ_FROM_USER_MODE);
976 	if (ret == 0)
977 		file->private_data = dev;
978 #ifdef CONFIG_TEE_REBOOT
979 	get_teecd_pid();
980 #endif
981 	return ret;
982 }
983 
tc_client_close(struct inode *inode, struct file *file)984 static int tc_client_close(struct inode *inode, struct file *file)
985 {
986 	int ret = 0;
987 	struct tc_ns_dev_file *dev = file->private_data;
988 #ifdef CONFIG_TEE_TUI
989 	/* release tui resource */
990 	struct teec_tui_parameter tui_param = {0};
991 #endif
992 	(void)inode;
993 
994 #ifdef CONFIG_TEE_TUI
995 	if (dev->dev_file_id == tui_attach_device()) {
996 		ret = tui_send_event(TUI_POLL_CANCEL, &tui_param);
997 		/* tee tui service is dead, but we need release the buffer in ree */
998 		if (ret == TEEC_ERROR_TUI_NOT_AVAILABLE)
999 			do_ns_tui_release();
1000 	}
1001 #endif
1002 
1003 	livepatch_down_read_sem();
1004 	ret = tc_ns_client_close(dev);
1005 	livepatch_up_read_sem();
1006 	file->private_data = NULL;
1007 
1008 	return ret;
1009 }
1010 
tc_private_close(struct inode *inode, struct file *file)1011 static int tc_private_close(struct inode *inode, struct file *file)
1012 {
1013 	struct tc_ns_dev_file *dev = file->private_data;
1014 	(void)inode;
1015 
1016 	/* for teecd close fd */
1017 	if (is_system_agent(dev)) {
1018 		/* for teecd agent close fd */
1019 		send_event_response_single(dev);
1020 		free_dev(dev);
1021 	} else {
1022 		/* for ca damon close fd */
1023 		free_dev(dev);
1024 	}
1025 	file->private_data = NULL;
1026 
1027 	return 0;
1028 }
1029 
tc_find_dev_file(unsigned int dev_file_id)1030 struct tc_ns_dev_file *tc_find_dev_file(unsigned int dev_file_id)
1031 {
1032 	struct tc_ns_dev_file *dev_file = NULL;
1033 
1034 	mutex_lock(&g_tc_ns_dev_list.dev_lock);
1035 	list_for_each_entry(dev_file, &g_tc_ns_dev_list.dev_file_list, head) {
1036 		if (dev_file->dev_file_id == dev_file_id) {
1037 			mutex_unlock(&g_tc_ns_dev_list.dev_lock);
1038 			return dev_file;
1039 		}
1040 	}
1041 	mutex_unlock(&g_tc_ns_dev_list.dev_lock);
1042 	return NULL;
1043 }
1044 
1045 #ifdef CONFIG_COMPAT
tc_compat_client_ioctl(struct file *file, unsigned int cmd, unsigned long arg)1046 long tc_compat_client_ioctl(struct file *file, unsigned int cmd,
1047 	unsigned long arg)
1048 {
1049 	long ret;
1050 
1051 	if (!file)
1052 		return -EINVAL;
1053 
1054 	arg = (unsigned long)(uintptr_t)compat_ptr(arg);
1055 	ret = tc_client_ioctl(file, cmd, arg);
1056 	return ret;
1057 }
1058 
tc_compat_private_ioctl(struct file *file, unsigned int cmd, unsigned long arg)1059 long tc_compat_private_ioctl(struct file *file, unsigned int cmd,
1060 	unsigned long arg)
1061 {
1062 	long ret;
1063 
1064 	if (!file)
1065 		return -EINVAL;
1066 
1067 	arg = (unsigned long)(uintptr_t)compat_ptr(arg);
1068 	ret = tc_private_ioctl(file, cmd, arg);
1069 	return ret;
1070 }
1071 #endif
1072 
1073 static const struct file_operations g_tc_ns_client_fops = {
1074 	.owner = THIS_MODULE,
1075 	.open = tc_client_open,
1076 	.release = tc_client_close,
1077 	.unlocked_ioctl = tc_client_ioctl,
1078 	.mmap = tc_client_mmap,
1079 #ifdef CONFIG_COMPAT
1080 	.compat_ioctl = tc_compat_client_ioctl,
1081 #endif
1082 };
1083 
1084 static const struct file_operations g_teecd_fops = {
1085 	.owner = THIS_MODULE,
1086 	.open = tc_client_open,
1087 	.release = tc_private_close,
1088 	.unlocked_ioctl = tc_private_ioctl,
1089 #ifdef CONFIG_COMPAT
1090 	.compat_ioctl = tc_compat_private_ioctl,
1091 #endif
1092 };
1093 #ifdef CONFIG_ACPI
1094 
tzdriver_probe(struct platform_device *pdev)1095 static int tzdriver_probe(struct platform_device *pdev)
1096 {
1097 	tlogd("tzdriver probe is running");
1098 	g_acpi_irq = platform_get_irq(pdev, 0);
1099 	if (g_acpi_irq < 0) {
1100 		dev_err(&pdev->dev, "get irq fail; irq:%d\n", g_acpi_irq);
1101 		return g_acpi_irq;
1102 	}
1103 
1104 	return 0;
1105 }
1106 
get_acpi_tz_irq(void)1107 int get_acpi_tz_irq(void)
1108 {
1109 	return g_acpi_irq;
1110 }
1111 
1112 static const struct acpi_device_id g_tzdriver_acpi_match[] = {
1113 	{ "HISI03C1", 0 },
1114 	{},
1115 };
1116 
1117 MODULE_DEVICE_TABLE(acpi, g_tzdriver_acpi_match);
1118 
1119 #else
1120 
tzdriver_probe(struct platform_device *pdev)1121 static int tzdriver_probe(struct platform_device *pdev)
1122 {
1123 	(void)pdev;
1124 	return 0;
1125 }
1126 
1127 struct of_device_id g_tzdriver_platform_match[] = {
1128 	{ .compatible = "trusted_core" },
1129 	{},
1130 };
1131 
1132 MODULE_DEVICE_TABLE(of, g_tzdriver_platform_match);
1133 
1134 #endif
1135 
1136 const struct dev_pm_ops g_tzdriver_pm_ops = {
1137 	.freeze_noirq = tc_s4_pm_suspend,
1138 	.restore_noirq = tc_s4_pm_resume,
1139 };
1140 
1141 static struct platform_driver g_tz_platform_driver = {
1142 	.driver = {
1143 		.name		= "trusted_core",
1144 		.owner		= THIS_MODULE,
1145 #ifdef CONFIG_ACPI
1146 		.acpi_match_table = ACPI_PTR(g_tzdriver_acpi_match),
1147 #else
1148 		.of_match_table = of_match_ptr(g_tzdriver_platform_match),
1149 #endif
1150 		.pm = &g_tzdriver_pm_ops,
1151 	},
1152 	.probe = tzdriver_probe,
1153 };
1154 
load_hw_info(void)1155 static int load_hw_info(void)
1156 {
1157 	if (platform_driver_register(&g_tz_platform_driver) != 0) {
1158 		tloge("platform register driver failed\n");
1159 		return -EFAULT;
1160 	}
1161 
1162 	/* load hardware info from dts and acpi */
1163 	g_dev_node = of_find_compatible_node(NULL, NULL, "trusted_core");
1164 	if (!g_dev_node) {
1165 		tloge("no trusted_core compatible node found\n");
1166 #ifndef CONFIG_ACPI
1167 		platform_driver_unregister(&g_tz_platform_driver);
1168 		return -ENODEV;
1169 #endif
1170 	}
1171 
1172 	return 0;
1173 }
1174 
create_dev_node(struct dev_node *node)1175 static int create_dev_node(struct dev_node *node)
1176 {
1177 	int ret;
1178 	if (!node || !(node->node_name)) {
1179 		tloge("node or member is null\n");
1180 		return -EFAULT;
1181 	}
1182 	if (alloc_chrdev_region(&(node->devt), 0, 1,
1183 		node->node_name) != 0) {
1184 		tloge("alloc chrdev region failed");
1185 		ret = -EFAULT;
1186 		return ret;
1187 	}
1188 	node->class_dev = device_create(node->driver_class, NULL, node->devt,
1189 		NULL, node->node_name);
1190 	if (IS_ERR_OR_NULL(node->class_dev)) {
1191 		tloge("class device create failed");
1192 		ret = -ENOMEM;
1193 		goto chrdev_region_unregister;
1194 	}
1195 	node->class_dev->of_node = g_dev_node;
1196 
1197 	cdev_init(&(node->char_dev), node->fops);
1198 	(node->char_dev).owner = THIS_MODULE;
1199 
1200 	return 0;
1201 
1202 chrdev_region_unregister:
1203 	unregister_chrdev_region(node->devt, 1);
1204 	return ret;
1205 }
1206 
init_dev_node(struct dev_node *node, char *node_name, struct class *driver_class, const struct file_operations *fops)1207 static int init_dev_node(struct dev_node *node, char *node_name,
1208 	struct class *driver_class, const struct file_operations *fops)
1209 {
1210 	int ret = -1;
1211 	if (!node) {
1212 		tloge("node is NULL\n");
1213 		return ret;
1214 	}
1215 	node->node_name = node_name;
1216 	node->driver_class = driver_class;
1217 	node->fops = fops;
1218 
1219 	ret = create_dev_node(node);
1220 	return ret;
1221 }
1222 
destory_dev_node(struct dev_node *node, struct class *driver_class)1223 static void destory_dev_node(struct dev_node *node, struct class *driver_class)
1224 {
1225 	device_destroy(driver_class, node->devt);
1226 	unregister_chrdev_region(node->devt, 1);
1227 	return;
1228 }
1229 
enable_dev_nodes(void)1230 static int enable_dev_nodes(void)
1231 {
1232 	int ret;
1233 
1234 	ret = cdev_add(&(g_tc_private.char_dev),
1235 		MKDEV(MAJOR(g_tc_private.devt), 0), 1);
1236 	if (ret < 0) {
1237 		tloge("cdev add failed %d", ret);
1238 		return ret;
1239 	}
1240 
1241 	ret = cdev_add(&(g_tc_client.char_dev),
1242 		MKDEV(MAJOR(g_tc_client.devt), 0), 1);
1243 	if (ret < 0) {
1244 		tloge("cdev add failed %d", ret);
1245 		cdev_del(&(g_tc_private.char_dev));
1246 		return ret;
1247 	}
1248 
1249 	return 0;
1250 }
1251 
tc_ns_client_init(void)1252 static int tc_ns_client_init(void)
1253 {
1254 	int ret;
1255 	ret = load_hw_info();
1256 	if (ret != 0)
1257 		return ret;
1258 
1259 	ret = load_reserved_mem();
1260 	if (ret != 0)
1261 		return ret;
1262 
1263 	ret = load_tz_shared_mem(g_dev_node);
1264 	if (ret != 0)
1265 		goto unmap_res_mem;
1266 #if LINUX_VERSION_CODE < KERNEL_VERSION(6, 6, 0)
1267 	g_driver_class = class_create(THIS_MODULE, TC_NS_CLIENT_DEV);
1268 #else
1269 	g_driver_class = class_create(TC_NS_CLIENT_DEV);
1270 #endif
1271 	if (IS_ERR_OR_NULL(g_driver_class)) {
1272 		tloge("class create failed");
1273 		ret = -ENOMEM;
1274 		goto unmap_res_mem;
1275 	}
1276 
1277 	ret = init_dev_node(&g_tc_client, TC_NS_CLIENT_DEV, g_driver_class, &g_tc_ns_client_fops);
1278 	if (ret != 0) {
1279 		class_destroy(g_driver_class);
1280 		goto unmap_res_mem;
1281 	}
1282 	ret = init_dev_node(&g_tc_private, TC_PRIV_DEV, g_driver_class, &g_teecd_fops);
1283 	if (ret != 0) {
1284 		destory_dev_node(&g_tc_client, g_driver_class);
1285 		class_destroy(g_driver_class);
1286 		goto unmap_res_mem;
1287 	}
1288 
1289 	INIT_LIST_HEAD(&g_tc_ns_dev_list.dev_file_list);
1290 	mutex_init(&g_tc_ns_dev_list.dev_lock);
1291 	init_crypto_hash_lock();
1292 	init_srvc_list();
1293 	return ret;
1294 unmap_res_mem:
1295 	unmap_res_mem();
1296 	return ret;
1297 }
1298 
tc_teeos_init(struct device *class_dev)1299 static int tc_teeos_init(struct device *class_dev)
1300 {
1301 	int ret;
1302 
1303 	ret = smc_context_init(class_dev);
1304 	if (ret != 0) {
1305 		tloge("smc context init failed\n");
1306 		return ret;
1307 	}
1308 
1309 	ret = tee_init_reboot_thread();
1310 	if (ret != 0) {
1311 		tloge("init reboot thread failed\n");
1312 		goto smc_data_free;
1313 	}
1314 
1315 	ret = reserved_mempool_init();
1316 	if (ret != 0) {
1317 		tloge("reserved memory init failed\n");
1318 		goto reboot_thread_free;
1319 	}
1320 
1321 	ret = mailbox_mempool_init();
1322 	if (ret != 0) {
1323 		tloge("tz mailbox init failed\n");
1324 		goto release_resmem;
1325 	}
1326 
1327 	ret = tz_spi_init(class_dev, g_dev_node);
1328 	if (ret != 0) {
1329 		tloge("tz spi init failed\n");
1330 		goto release_mempool;
1331 	}
1332 
1333 	return 0;
1334 release_mempool:
1335 	free_mailbox_mempool();
1336 release_resmem:
1337 	free_reserved_mempool();
1338 reboot_thread_free:
1339 	free_reboot_thread();
1340 smc_data_free:
1341 	free_smc_data();
1342 	return ret;
1343 }
1344 
tc_re_init(const struct device *class_dev)1345 static void tc_re_init(const struct device *class_dev)
1346 {
1347 	int ret;
1348 
1349 	agent_init();
1350 	ret = tc_ns_register_ion_mem();
1351 	if (ret != 0)
1352 		tloge("Failed to register ion mem in tee\n");
1353 
1354 #ifdef CONFIG_TZDRIVER_MODULE
1355 	ret = init_tlogger_service();
1356 	if (ret != 0)
1357 		tloge("tlogger init failed\n");
1358 #endif
1359 	if (tzdebug_init() != 0)
1360 		tloge("tzdebug init failed\n");
1361 
1362 	ret = init_tui(class_dev);
1363 	if (ret != 0)
1364 		tloge("init_tui failed 0x%x\n", ret);
1365 
1366 #ifndef CONFIG_DISABLE_SVC
1367 	if (init_smc_svc_thread() != 0) {
1368 		tloge("init svc thread\n");
1369 		ret = -EFAULT;
1370 	}
1371 #endif
1372 
1373 	if (init_dynamic_mem() != 0) {
1374 		tloge("init dynamic mem Failed\n");
1375 		ret = -EFAULT;
1376 	}
1377 
1378 #ifdef CONFIG_LIVEPATCH_ENABLE
1379 	/*
1380 	 * access this sys node only after this function is initialized
1381 	 */
1382 	if (livepatch_init(class_dev)) {
1383 		tloge("livepatch init failed\n");
1384 		ret = -EFAULT;
1385 	}
1386 #endif
1387 
1388 	if (ret != 0)
1389 		tloge("Caution! Running environment init failed!\n");
1390 }
1391 
tc_init(void)1392 static __init int tc_init(void)
1393 {
1394 	int ret = 0;
1395 
1396 	init_kthread_cpumask();
1397 	ret = tc_ns_client_init();
1398 	if (ret != 0)
1399 		return ret;
1400 
1401 #ifdef CONFIG_FFA_SUPPORT
1402 	ffa_abi_register();
1403 #endif
1404 
1405 	ret = tc_teeos_init(g_tc_client.class_dev);
1406 	if (ret != 0) {
1407 		tloge("tc teeos init failed\n");
1408 		goto class_device_destroy;
1409 	}
1410 	/* run-time environment init failure don't block tzdriver init proc */
1411 	tc_re_init(g_tc_client.class_dev);
1412 
1413 	/*
1414 	 * Note: the enable_dev_nodes function must be called
1415 	 * at the end of tc_init
1416 	 */
1417 	ret = enable_dev_nodes();
1418 	if (ret != 0) {
1419 		tloge("enable dev nodes failed\n");
1420 		goto class_device_destroy;
1421 	}
1422 
1423 	ret = alloc_dev_bitmap();
1424 	if (ret != 0) {
1425 		tloge("alloc dev file id bitmap failed\n");
1426 		goto class_device_destroy;
1427 	}
1428 
1429 	set_tz_init_flag();
1430 	return 0;
1431 
1432 class_device_destroy:
1433 	free_dev_bitmap();
1434 	destory_dev_node(&g_tc_client, g_driver_class);
1435 	destory_dev_node(&g_tc_private, g_driver_class);
1436 	class_destroy(g_driver_class);
1437 	platform_driver_unregister(&g_tz_platform_driver);
1438 	return ret;
1439 }
1440 
free_dev_list(void)1441 static void free_dev_list(void)
1442 {
1443 	struct tc_ns_dev_file *dev_file = NULL, *temp = NULL;
1444 
1445 	mutex_lock(&g_tc_ns_dev_list.dev_lock);
1446 	list_for_each_entry_safe(dev_file, temp, &g_tc_ns_dev_list.dev_file_list, head) {
1447 		list_del(&dev_file->head);
1448 		kfree(dev_file);
1449 	}
1450 	mutex_unlock(&g_tc_ns_dev_list.dev_lock);
1451 }
1452 
tc_exit(void)1453 static void tc_exit(void)
1454 {
1455 	tlogi("tz client exit");
1456 	clear_tz_init_flag();
1457 	/*
1458 	 * You should first execute "cdev_del" to
1459 	 * prevent access to the device node when uninstalling "tzdriver".
1460 	 */
1461 	cdev_del(&(g_tc_private.char_dev));
1462 	cdev_del(&(g_tc_client.char_dev));
1463 	free_agent();
1464 	free_reboot_thread();
1465 	free_tui();
1466 	free_tz_spi(g_tc_client.class_dev);
1467 	/* run-time environment exit should before teeos exit */
1468 
1469 	destory_dev_node(&g_tc_client, g_driver_class);
1470 	destory_dev_node(&g_tc_private, g_driver_class);
1471 	free_dev_bitmap();
1472 	platform_driver_unregister(&g_tz_platform_driver);
1473 	class_destroy(g_driver_class);
1474 	free_smc_data();
1475 	free_event_mem();
1476 #ifdef CONFIG_TZDRIVER_MODULE
1477 	free_tzdebug();
1478 	free_tlogger_service();
1479 #endif
1480 	free_interrupt_trace();
1481 	free_mailbox_mempool();
1482 	free_reserved_mempool();
1483 	free_shash_handle();
1484 	fault_monitor_end();
1485 	free_livepatch();
1486 	free_all_session();
1487 	free_dev_list();
1488 #ifdef CONFIG_FFA_SUPPORT
1489 	ffa_abi_unregister();
1490 #endif
1491 	tlogi("tz client exit finished");
1492 }
1493 
1494 MODULE_AUTHOR("iTrustee");
1495 MODULE_DESCRIPTION("TrustCore ns-client driver");
1496 MODULE_VERSION("1.10");
1497 
1498 #ifdef CONFIG_TZDRIVER_MODULE
1499 module_init(tc_init);
1500 #else
1501 fs_initcall_sync(tc_init);
1502 #endif
1503 module_exit(tc_exit);
1504 MODULE_LICENSE("GPL");
1505