1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
|
/*
* Copyright (C) 2015 Samsung Electronics, Inc.
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <linux/errno.h> /* cma.h needs this */
#if defined(CONFIG_TZDEV_CMA)
#if defined(CONFIG_DMA_CMA)
#include <linux/dma-mapping.h>
#include <linux/dma-contiguous.h>
#elif !defined(CONFIG_ARCH_MSM)
#include <linux/cma.h>
#endif
#endif
#include <linux/completion.h>
#include <linux/cpu.h>
#include <linux/dma-mapping.h>
#include <linux/file.h>
#include <linux/highmem.h>
#include <linux/interrupt.h>
#include <linux/ioctl.h>
#include <linux/kernel.h>
#include <linux/kfifo.h>
#include <linux/list.h>
#include <linux/migrate.h>
#include <linux/miscdevice.h>
#include <linux/mmzone.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/platform_device.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/syscore_ops.h>
#include <linux/time.h>
#include <linux/uaccess.h>
#include <linux/vmalloc.h>
#include <linux/workqueue.h>
#include "sysdep.h"
#include "tzdev.h"
#include "tzlog.h"
#include "tzpm.h"
#include "tz_core_migration.h"
#include <linux/fs.h>
#include <asm/segment.h>
#include <asm/uaccess.h>
#include <linux/buffer_head.h>
MODULE_AUTHOR("Jaemin Ryu <jm77.ryu@samsung.com>");
MODULE_AUTHOR("Vasily Leonenko <v.leonenko@samsung.com>");
MODULE_AUTHOR("Alex Matveev <alex.matveev@samsung.com>");
MODULE_DESCRIPTION("TZDEV driver");
MODULE_LICENSE("GPL");
int tzdev_verbosity = 0;
struct hrtimer tzdev_get_event_timer;
module_param(tzdev_verbosity, int, 0644);
MODULE_PARM_DESC(tzdev_verbosity, "0: normal, 1: verbose, 2: debug");
static DEFINE_MUTEX(tzdev_fd_mutex);
static int tzdev_sw_init_done = 0;
static int tzdev_fd_open;
static DEFINE_IDR(tzdev_mem_map);
static DEFINE_MUTEX(tzdev_mem_mutex);
static DECLARE_COMPLETION(tzdev_ow_comp);
#if defined(CONFIG_QCOM_SCM_ARMV8)
DEFINE_MUTEX(tzdev_smc_lock);
#endif
#if defined(CONFIG_TZDEV_QC_CRYPTO_CLOCKS_USR_MNG)
unsigned long tzdev_qc_clk = QSEE_CLK_OFF;
static DEFINE_MUTEX(tzdev_qc_clk_mutex);
#else
unsigned long tzdev_qc_clk = QSEE_CLK_ON;
#endif
struct tzdev_mem_reg {
struct mm_struct *mm;
unsigned long nr_pages;
struct page **pages;
};
#if defined(CONFIG_TZDEV_CMA)
#if defined(CONFIG_CMA) && !defined(CONFIG_DMA_CMA)
static struct cma_info tzdev_cma_info;
#elif defined(CONFIG_DMA_CMA)
struct page *tzdev_page = NULL;
#endif
static dma_addr_t tzdev_cma_addr = 0;
#endif
#if defined(CONFIG_TZLOG)
static DEFINE_SPINLOCK(tzdev_log_slock);
static DEFINE_PER_CPU(struct tzio_log_channel *, tzdev_log_channel);
#endif
#ifdef CONFIG_TZDEV_MULTI_CORE_AUX_CHANNELS
static DEFINE_PER_CPU(struct tzio_aux_channel *, tzdev_aux_channel);
#define aux_channel_get(ch) get_cpu_var(ch)
#define aux_channel_put(ch) put_cpu_var(ch)
#define aux_channel_init(ch, cpu) per_cpu(ch, cpu)
#else /* CONFIG_TZDEV_MULTI_CORE_AUX_CHANNELS */
static DEFINE_MUTEX(tzdev_aux_channel_lock);
static struct tzio_aux_channel *tzdev_aux_channel[NR_CPUS];
static struct tzio_aux_channel *aux_channel_get(struct tzio_aux_channel *ch[])
{
mutex_lock(&tzdev_aux_channel_lock);
BUG_ON(smp_processor_id() != 0);
return ch[0];
}
#define aux_channel_put(ch) mutex_unlock(&tzdev_aux_channel_lock)
#define aux_channel_init(ch, cpu) ch[cpu]
#endif /* CONFIG_TZDEV_MULTI_CORE_AUX_CHANNELS */
#if defined(CONFIG_TZLOG)
#define TZDEV_OW_LINE_MAX_LEN 256
#define TZDEV_OW_PREFIX KERN_DEFAULT "SW> "
struct tzdev_log_print_state {
char line[TZDEV_OW_LINE_MAX_LEN + 1]; /* one byte for \0 */
unsigned int line_len;
};
DEFINE_PER_CPU(struct tzdev_log_print_state, tzdev_log_print_state);
static int tzio_log_channel_print_debug(const char *buf, unsigned int count)
{
struct tzdev_log_print_state *ps;
unsigned int count_out, avail, bytes_in, bytes_out, bytes_printed, tmp, wait_dta;
char *p;
ps = &get_cpu_var(tzdev_log_print_state);
count_out = count;
wait_dta = 0;
while (count_out) {
avail = TZDEV_OW_LINE_MAX_LEN - ps->line_len;
p = memchr(buf, '\n', count_out);
if (p) {
if (p - buf > avail) {
bytes_in = avail;
bytes_out = avail;
} else {
bytes_in = p - buf + 1;
bytes_out = p - buf;
}
} else {
if (count_out >= avail) {
bytes_in = avail;
bytes_out = avail;
} else {
bytes_in = count_out;
bytes_out = count_out;
wait_dta = 1;
}
}
memcpy(&ps->line[ps->line_len], buf, bytes_out);
ps->line_len += bytes_out;
if (wait_dta)
break;
ps->line[ps->line_len] = 0;
bytes_printed = 0;
while (bytes_printed < ps->line_len) {
tmp = printk(TZDEV_OW_PREFIX "%s\n", &ps->line[bytes_printed]);
if (!tmp)
break;
bytes_printed += tmp;
}
ps->line_len = 0;
count_out -= bytes_in;
buf += bytes_in;
}
put_cpu_var(tzdev_log_print_state);
return count;
}
void tzio_log_channel_read_debug(void)
{
struct tzio_log_channel *ch;
unsigned int i, bytes, count;
spin_lock(&tzdev_log_slock);
for (i = 0; i < nr_cpu_ids; ++i) {
ch = per_cpu(tzdev_log_channel, i);
if (!ch)
continue;
if (ch->write_count < ch->read_count) {
count = TZDEV_LOG_BUF_SIZE - ch->read_count;
bytes = tzio_log_channel_print_debug(ch->buffer + ch->read_count, count);
if (bytes < count) {
ch->read_count += bytes;
continue;
}
ch->read_count = 0;
}
count = ch->write_count - ch->read_count;
bytes = tzio_log_channel_print_debug(ch->buffer + ch->read_count, count);
ch->read_count += count;
}
spin_unlock(&tzdev_log_slock);
}
static void tzdev_alloc_log_channel(int cpu)
{
struct tzio_log_channel *channel;
struct page *page[CONFIG_TZLOG_PG_CNT];
sk_pfn_t pfns[CONFIG_TZLOG_PG_CNT];
struct tzio_aux_channel *aux_ch;
int i;
/* Allocate non-contiguous buffer to reduce page allocator pressure */
for (i = 0; i < CONFIG_TZLOG_PG_CNT; i++) {
page[i] = alloc_page(GFP_KERNEL);
if (!page[i]) {
tzdev_print(0, "TZDev channel creation failed\n");
goto free_buffer;
}
pfns[i] = (sk_pfn_t)(page_to_phys(page[i]) >> PAGE_SHIFT);
}
channel = vmap(page, CONFIG_TZLOG_PG_CNT, VM_MAP, PAGE_KERNEL);
if (!channel) {
tzdev_print(0, "TZDev channel mapping failed\n");
goto free_buffer;
}
tzdev_print(0, "IRQ Channel[%d] = 0x%p\n", cpu, channel);
/* Push PFNs list into aux channel */
aux_ch = aux_channel_get(tzdev_aux_channel);
memcpy(aux_ch->buffer, &pfns, CONFIG_TZLOG_PG_CNT * sizeof(sk_pfn_t));
if (tzdev_smc_connect(TZDEV_CONNECT_LOG, 0, CONFIG_TZLOG_PG_CNT)) {
aux_channel_put(tzdev_aux_channel);
vunmap(channel);
tzdev_print(0, "TZDev log channel registration failed\n");
goto free_buffer;
}
aux_channel_put(tzdev_aux_channel);
per_cpu(tzdev_log_channel, cpu) = channel;
tzdev_print(0, ">>>>>>>>>>>>>>>>>>>>. TZDEV CPU : %d\n", cpu);
return;
free_buffer:
for (i = 0; (i < CONFIG_TZLOG_PG_CNT) && page[i]; i++)
__free_page(page[i]);
}
#endif
static void tzdev_alloc_aux_channel(int cpu)
{
struct tzio_aux_channel *channel;
struct page *page;
page = alloc_page(GFP_KERNEL);
if (!page) {
tzdev_print(0, "TZDev channel creation failed\n");
return;
}
channel = page_address(page);
tzdev_print(0, "AUX Channel[%d] = 0x%p\n", cpu, channel);
if (tzdev_smc_connect(TZDEV_CONNECT_AUX, page_to_phys(page), 1)) {
__free_page(page);
tzdev_print(0, "TZDev AUX channel registration failed\n");
return;
}
aux_channel_init(tzdev_aux_channel, cpu) = channel;
tzdev_print(0, ">>>>>>>>>>>>>>>>>>>>. TZDEV CPU : %d\n", cpu);
}
#ifdef CONFIG_TZLOG_POLLING
static void tzio_log_bh(struct work_struct *work)
{
tzio_log_channel_read_debug();
schedule_delayed_work(&tzio_log_work,
CONFIG_TZLOG_POLLING_PERIOD * HZ / MSEC_PER_SEC);
}
DECLARE_DELAYED_WORK(tzio_log_work, tzio_log_bh);
#endif
#if CONFIG_TZDEV_IWI_PANIC
static void dump_kernel_panic_bh(struct work_struct *work)
{
tzio_log_channel_read_debug();
}
static DECLARE_WORK(dump_kernel_panic, dump_kernel_panic_bh);
#endif
/* This is the same approach to pinning user memory
* as used in Infiniband drivers.
* Refer to drivers/inifiniband/core/umem.c */
static int tzdev_get_user_pages(struct task_struct *task, struct mm_struct *mm,
unsigned long start, unsigned long nr_pages, int write,
int force, struct page **pages, struct vm_area_struct **vmas)
{
unsigned long i, locked, lock_limit, nr_pinned;
struct page **cur_pages;
int ret, res;
if (!can_do_mlock())
return -EPERM;
down_write(&mm->mmap_sem);
locked = nr_pages + mm->pinned_vm;
lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
up_write(&mm->mmap_sem);
return -ENOMEM;
}
nr_pinned = 0;
cur_pages = pages;
while (nr_pinned < nr_pages) {
res = get_user_pages(task, mm, start, nr_pages - nr_pinned,
write, force, cur_pages, vmas);
if (res < 0) {
up_write(&mm->mmap_sem);
ret = res;
goto fail;
}
start += res * PAGE_SIZE;
nr_pinned += res;
cur_pages += res;
}
mm->pinned_vm = locked;
up_write(&mm->mmap_sem);
return 0;
fail:
for (i = 0; i < nr_pinned; i++)
put_page(pages[i]);
return ret;
}
static void tzdev_put_user_pages(struct mm_struct *mm, struct page **pages,
unsigned long nr_pages)
{
unsigned long i;
for (i = 0; i < nr_pages; i++)
put_page(pages[i]);
down_write(&mm->mmap_sem);
mm->pinned_vm -= nr_pages;
up_write(&mm->mmap_sem);
}
#if defined(CONFIG_TZDEV_PAGE_MIGRATION)
/* migrate_pages() is significantly different between Linux
* versions so, we need to separate it for each version but,
* currently, we consider only both 3.10.xx and 3.4.xx */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)
#define MIGRATE_PAGES(list, alloc) migrate_pages((list), (alloc), 0, MIGRATE_SYNC, MR_MEMORY_FAILURE)
#else
#define MIGRATE_PAGES(list, alloc) migrate_pages((list), (alloc), 0, false, MIGRATE_SYNC)
#endif
int isolate_lru_page(struct page *page);
struct page *tzdev_alloc_kernel_page(struct page *page, unsigned long private, int **x)
{
return alloc_page(GFP_KERNEL);
}
static int tzdev_migrate_pages(struct task_struct *task, struct mm_struct *mm,
unsigned long start, unsigned long nr_pages, int write, struct page **pages)
{
int res, i;
LIST_HEAD(mem_pages_list);
res = tzdev_get_user_pages(task, mm, start, nr_pages, write, 0, pages, NULL);
if (res)
return res;
/*
* In case of enabled migration it is possible that userspace pages
* will be migrated from current physical page to some other
* To avoid fails of CMA migrations we have to move pages to other
* region which can not be inside any CMA region. This is done by
* allocations with GFP_KERNEL flag to point UNMOVABLE memblock
* to be used for such allocations.
*/
migrate_prep();
for (i = 0; i < nr_pages; i++) {
if (get_pageblock_migratetype(pages[i]) != MIGRATE_CMA)
continue;
/* pages should be isolated before migration */
res = isolate_lru_page(pages[i]);
if (res)
continue;
list_add_tail(&pages[i]->lru, &mem_pages_list);
}
tzdev_put_user_pages(mm, pages, nr_pages);
res = MIGRATE_PAGES(&mem_pages_list, tzdev_alloc_kernel_page);
if (res) {
putback_lru_pages(&mem_pages_list);
return -EFAULT;
}
return 0;
}
static int tzdev_verify_migration(struct page **pages, unsigned long nr_pages)
{
int i;
for (i = 0; i < nr_pages; i++) {
if (get_pageblock_migratetype(pages[i]) == MIGRATE_CMA)
return -EFAULT;
}
return 0;
}
#endif
static int __tzdev_mem_free(int id, void *p, void *data)
{
struct tzdev_mem_reg *mem = p;
tzdev_put_user_pages(mem->mm, mem->pages, mem->nr_pages);
mmput(mem->mm);
kfree(mem->pages);
kfree(mem);
return 0;
}
static int tzdev_open(struct inode *inode, struct file *filp)
{
#if defined(CONFIG_TZDEV_CMA)
struct page *p;
void *ptr;
unsigned long pfn;
#endif
int ret = 0;
unsigned int i;
tzdev_migrate();
mutex_lock(&tzdev_fd_mutex);
if (tzdev_fd_open != 0) {
ret = -EBUSY;
goto out;
}
if (!tzdev_sw_init_done) {
/* check kernel and driver version compatibility with quark */
ret = tzdev_smc_check_version();
if (ret) {
tzdev_print(0, "The version of the Linux kernel or TZDev driver is not compatible with Quark secure kernel\n");
goto out;
}
#if defined(CONFIG_TZDEV_CMA)
if (tzdev_cma_addr != 0) {
for (pfn = __phys_to_pfn(tzdev_cma_addr);
pfn < __phys_to_pfn(tzdev_cma_addr +
CONFIG_TZDEV_MEMRESSZ - CONFIG_TZDEV_MEMRESSZPROT);
pfn++) {
p = pfn_to_page(pfn);
ptr = kmap(p);
/*
* We can just invalidate here, but kernel doesn't
* export cache invalidation functions.
*/
__flush_dcache_area(ptr, PAGE_SIZE);
kunmap(p);
}
outer_inv_range(tzdev_cma_addr, tzdev_cma_addr +
CONFIG_TZDEV_MEMRESSZ - CONFIG_TZDEV_MEMRESSZPROT);
ret = tzdev_smc_mem_reg((unsigned long)tzdev_cma_addr,
(unsigned long)(get_order(CONFIG_TZDEV_MEMRESSZ) + PAGE_SHIFT));
if (ret) {
tzdev_print(0, "Registration of CMA region in SW failed;\n");
goto out;
}
}
#endif
for (i = 0; i < nr_cpu_ids; ++i)
tzdev_alloc_aux_channel(i);
#if defined(CONFIG_TZLOG)
for (i = 0; i < nr_cpu_ids; ++i)
tzdev_alloc_log_channel(i);
#endif
#if defined(CONFIG_TZDEV_QC_CRYPTO_CLOCKS_MANAGEMENT)
tzdev_qc_pm_clock_initialize();
#endif
tzdev_sw_init_done = 1;
}
tzdev_fd_open++;
tzdev_smc_nwd_alive();
out:
mutex_unlock(&tzdev_fd_mutex);
return ret;
}
static int tzdev_release(struct inode *inode, struct file *filp)
{
#ifdef CONFIG_TZDEV_NWD_PANIC_ON_CLOSE
panic("tzdev invalid close\n");
#endif
mutex_lock(&tzdev_fd_mutex);
#if defined(CONFIG_TZDEV_QC_CRYPTO_CLOCKS_USR_MNG)
if (tzdev_qc_clk == QSEE_CLK_ON) {
tzdev_qc_pm_clock_disable();
tzdev_qc_clk = QSEE_CLK_OFF;
}
#endif
tzdev_fd_open--;
BUG_ON(tzdev_fd_open);
tzdev_smc_nwd_dead();
mutex_lock(&tzdev_mem_mutex);
idr_for_each(&tzdev_mem_map, __tzdev_mem_free, NULL);
IDR_REMOVE_ALL(&tzdev_mem_map);
idr_destroy(&tzdev_mem_map);
mutex_unlock(&tzdev_mem_mutex);
mutex_unlock(&tzdev_fd_mutex);
return 0;
}
int tzdev_get_access_info(struct tzio_access_info *s)
{
struct task_struct *task;
struct mm_struct *mm;
struct file *exe_file;
rcu_read_lock();
task = find_task_by_vpid(s->pid);
if (!task) {
rcu_read_unlock();
return -ESRCH;
}
get_task_struct(task);
rcu_read_unlock();
s->gid = task->tgid;
mm = get_task_mm(task);
put_task_struct(task);
if (!mm)
return -ESRCH;
exe_file = get_mm_exe_file(mm);
mmput(mm);
if (!exe_file)
return -ESRCH;
strncpy(s->ca_name, exe_file->f_path.dentry->d_name.name, CA_ID_LEN);
fput(exe_file);
return 0;
}
int tzdev_mem_register(struct tzio_mem_register *s)
{
#define TZDEV_PFNS_PER_PAGE (PAGE_SIZE / sizeof(sk_pfn_t))
struct pid *pid;
struct task_struct *task;
struct mm_struct *mm;
struct page **pages;
struct tzdev_mem_reg *mem;
sk_pfn_t *pfns;
struct tzio_aux_channel *ch;
unsigned long start, end, nr_pages, offset;
int ret, res, i, id;
start = (unsigned long)s->ptr >> PAGE_SHIFT;
end = ((unsigned long)s->ptr + s->size + PAGE_SIZE - 1) >> PAGE_SHIFT;
nr_pages = (s->size ? end - start : 0);
offset = ((unsigned long)s->ptr) & ~PAGE_MASK;
pid = find_get_pid(s->pid);
if (!pid)
return -ESRCH;
task = get_pid_task(pid, PIDTYPE_PID);
put_pid(pid);
if (!task)
return -ESRCH;
/* mm reference is kept till mem removal */
mm = get_task_mm(task);
if (!mm) {
ret = -ESRCH;
goto out_task;
}
pages = kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
if (!pages) {
ret = -ENOMEM;
goto out_mm;
}
pfns = kmalloc(nr_pages * sizeof(sk_pfn_t), GFP_KERNEL);
if (!pfns) {
ret = -ENOMEM;
goto out_pages;
}
mem = kmalloc(sizeof(struct tzdev_mem_reg), GFP_KERNEL);
if (!mem) {
ret = -ENOMEM;
goto out_pfns;
}
#if defined(CONFIG_TZDEV_PAGE_MIGRATION)
res = tzdev_migrate_pages(task, mm, (unsigned long)s->ptr,
nr_pages, s->write, pages);
if (res) {
ret = res;
goto out_mem;
}
#endif
res = tzdev_get_user_pages(task, mm, (unsigned long)s->ptr,
nr_pages, 1, !s->write, pages, NULL);
if (res) {
ret = res;
goto out_mem;
}
#if defined(CONFIG_TZDEV_PAGE_MIGRATION)
res = tzdev_verify_migration(pages, nr_pages);
if (res) {
tzdev_print(0, "Failed migration. Some pages are still in CMA\n");
ret = res;
goto out_pin;
}
#endif
for (i = 0; i < nr_pages; i++)
pfns[i] = (sk_pfn_t)page_to_pfn(pages[i]);
mutex_lock(&tzdev_mem_mutex);
res = sysdep_idr_alloc(&tzdev_mem_map, mem);
if (res < 0) {
ret = res;
mutex_unlock(&tzdev_mem_mutex);
goto out_pin;
} else
id = res;
ch = aux_channel_get(tzdev_aux_channel);
for (i = 0; i < nr_pages; i += TZDEV_PFNS_PER_PAGE) {
memcpy(ch->buffer, &pfns[i], min(nr_pages - i, TZDEV_PFNS_PER_PAGE) * sizeof(sk_pfn_t));
if (tzdev_smc_shmem_list_reg(id, nr_pages, s->write)) {
tzdev_smc_shmem_list_rls(id);
aux_channel_put(tzdev_aux_channel);
idr_remove(&tzdev_mem_map, id);
mutex_unlock(&tzdev_mem_mutex);
ret = -EFAULT;
goto out_pin;
}
}
aux_channel_put(tzdev_aux_channel);
mem->mm = mm;
mem->nr_pages = nr_pages;
mem->pages = pages;
mutex_unlock(&tzdev_mem_mutex);
s->id = id;
kfree(pfns);
put_task_struct(task);
return 0;
out_pin:
tzdev_put_user_pages(mm, pages, nr_pages);
out_mem:
kfree(mem);
out_pfns:
kfree(pfns);
out_pages:
kfree(pages);
out_mm:
mmput(mm);
out_task:
put_task_struct(task);
return ret;
}
static int tzdev_mem_release(int id)
{
struct tzdev_mem_reg *mem;
mutex_lock(&tzdev_mem_mutex);
tzdev_smc_shmem_list_rls(id);
mem = idr_find(&tzdev_mem_map, id);
if (!mem) {
mutex_unlock(&tzdev_mem_mutex);
return -EINVAL;
}
idr_remove(&tzdev_mem_map, id);
mutex_unlock(&tzdev_mem_mutex);
__tzdev_mem_free(id, mem, NULL);
return 0;
}
static long tzdev_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
int ret = 0;
tzdev_migrate();
switch (cmd) {
case TZIO_SMC: {
const struct tzio_smc_cmd __user *argp = (const struct tzio_smc_cmd __user *)arg;
struct tzio_smc_cmd s;
if (copy_from_user(&s, argp, sizeof(struct tzio_smc_cmd))) {
ret = -EFAULT;
break;
}
s.ret = tzdev_smc_command(s.args[0], s.args[1]);
if (put_user(s.ret, &argp->ret)) {
ret = -EFAULT;
break;
}
break;
}
case TZIO_GET_ACCESS_INFO: {
struct tzio_access_info __user *argp = (struct tzio_access_info __user *)arg;
struct tzio_access_info s;
if (copy_from_user(&s, argp, sizeof(struct tzio_access_info))) {
ret = -EFAULT;
break;
}
ret = tzdev_get_access_info(&s);
if (ret)
break;
if (copy_to_user(argp, &s, sizeof(struct tzio_access_info))) {
ret = -EFAULT;
break;
}
break;
}
case TZIO_MEM_REGISTER: {
struct tzio_mem_register __user *argp = (struct tzio_mem_register __user *)arg;
struct tzio_mem_register s;
if (copy_from_user(&s, argp, sizeof(struct tzio_mem_register))) {
ret = -EFAULT;
break;
}
ret = tzdev_mem_register(&s);
if (ret)
break;
if (copy_to_user(argp, &s, sizeof(struct tzio_mem_register))) {
ret = -EFAULT;
break;
}
break;
}
case TZIO_MEM_RELEASE: {
ret = tzdev_mem_release(arg);
break;
}
case TZIO_WAIT_EVT: {
ret = wait_for_completion_interruptible(&tzdev_ow_comp);
break;
}
case TZIO_GET_PIPE: {
struct timespec ts;
__s32 dta;
__s32 __user *argp = (__s32 __user *)arg;
getnstimeofday(&ts);
dta = tzdev_smc_get_event(ts.tv_sec, ts.tv_nsec);
if (put_user(dta, argp)) {
ret = -EFAULT;
break;
}
break;
}
case TZIO_MIGRATE: {
tzdev_print(0, "TZIO_MIGRATE: arg = %lu\n", arg);
ret = tzdev_migration_request(arg);
break;
}
#if defined(CONFIG_TZDEV_QC_CRYPTO_CLOCKS_USR_MNG)
case TZIO_SET_QC_CLK: {
unsigned long clk_cmd;
unsigned long __user *argp = (unsigned long __user *)arg;
if (copy_from_user(&clk_cmd, argp, sizeof(clk_cmd))) {
ret = -EFAULT;
break;
}
mutex_lock(&tzdev_qc_clk_mutex);
if ((clk_cmd == QSEE_CLK_ON) && (tzdev_qc_clk == QSEE_CLK_OFF)) {
tzdev_qc_pm_clock_enable();
tzdev_qc_clk = QSEE_CLK_ON;
} else if ((clk_cmd == QSEE_CLK_OFF) && (tzdev_qc_clk == QSEE_CLK_ON)) {
tzdev_qc_pm_clock_disable();
tzdev_qc_clk = QSEE_CLK_OFF;
}
mutex_unlock(&tzdev_qc_clk_mutex);
break;
}
#endif
default:
ret = -ENOTTY;
}
return ret;
}
static void tzdev_shutdown(void)
{
if (tzdev_sw_init_done)
tzdev_smc_shutdown();
}
static const struct file_operations tzdev_fops = {
.owner = THIS_MODULE,
.open = tzdev_open,
.release = tzdev_release,
.unlocked_ioctl = tzdev_unlocked_ioctl,
#ifdef CONFIG_COMPAT
.compat_ioctl = tzdev_unlocked_ioctl,
#endif /* CONFIG_COMPAT */
};
static struct miscdevice tzdev = {
.minor = MISC_DYNAMIC_MINOR,
.name = "tzdev",
.fops = &tzdev_fops,
};
static struct syscore_ops tzdev_syscore_ops = {
.shutdown = tzdev_shutdown
};
#if defined(CONFIG_TZDEV_MSM_CRYPTO_WORKAROUND)
#include <linux/msm_ion.h>
#include "../qseecom_kernel.h"
void msm_crypto_workaround(void)
{
enum qseecom_client_handle_type {
QSEECOM_CLIENT_APP = 1,
QSEECOM_LISTENER_SERVICE,
QSEECOM_SECURE_SERVICE,
QSEECOM_GENERIC,
QSEECOM_UNAVAILABLE_CLIENT_APP,
};
struct qseecom_client_handle {
u32 app_id;
u8 *sb_virt;
s32 sb_phys;
uint32_t user_virt_sb_base;
size_t sb_length;
struct ion_handle *ihandle; /* Retrieve phy addr */
};
struct qseecom_listener_handle {
u32 id;
};
struct qseecom_dev_handle {
enum qseecom_client_handle_type type;
union {
struct qseecom_client_handle client;
struct qseecom_listener_handle listener;
};
bool released;
int abort;
wait_queue_head_t abort_wq;
atomic_t ioctl_count;
bool perf_enabled;
bool fast_load_enabled;
};
/* Bogus handles */
struct qseecom_dev_handle dev_handle;
struct qseecom_handle handle = {
.dev = &dev_handle
};
int ret;
ret = qseecom_set_bandwidth(&handle, 1);
if (ret)
tzdev_print(0, "qseecom_set_bandwidth failed ret = %d\n", ret);
}
#endif
static irqreturn_t tzdev_event_handler(int irq, void *ptr)
{
complete(&tzdev_ow_comp);
return IRQ_HANDLED;
}
#if CONFIG_TZDEV_IWI_PANIC
static irqreturn_t tzdev_panic_handler(int irq, void *ptr)
{
#ifdef CONFIG_TZLOG
schedule_work(&dump_kernel_panic);
#endif
return IRQ_HANDLED;
}
#endif
static enum hrtimer_restart tzdev_get_event_timer_handler(struct hrtimer *timer)
{
complete(&tzdev_ow_comp);
return HRTIMER_NORESTART;
}
static int __init init_tzdev(void)
{
int rc;
#if defined(CONFIG_TZDEV_QC_CRYPTO_CLOCKS_MANAGEMENT)
tzdev_platform_register();
#endif
rc = misc_register(&tzdev);
if (unlikely(rc))
return rc;
if (request_irq(CONFIG_TZDEV_IWI_EVENT, tzdev_event_handler, 0,
"tzdev_iwi_event", NULL))
tzdev_print(0, "TZDEV_IWI_EVENT registration failed\n");
#if CONFIG_TZDEV_IWI_PANIC
if (request_irq(CONFIG_TZDEV_IWI_PANIC, tzdev_panic_handler, 0,
"tzdev_iwi_panic", NULL))
tzdev_print(0, "TZDEV_IWI_PANIC registration failed\n");
#endif
#if defined(CONFIG_TZDEV_CMA)
tzdev_cma_addr = 0;
#if defined(CONFIG_DMA_CMA)
tzdev_page = dma_alloc_from_contiguous(tzdev.this_device,
(CONFIG_TZDEV_MEMRESSZ - CONFIG_TZDEV_MEMRESSZPROT) >> PAGE_SHIFT,
get_order(CONFIG_TZDEV_MEMRESSZ));
if (!tzdev_page) {
tzdev_print(0, "Allocation CMA region failed;"
" Memory will not be registered in SWd\n");
goto out;
}
tzdev_cma_addr = page_to_phys(tzdev_page);
#elif defined(CONFIG_CMA)
if (cma_info(&tzdev_cma_info, tzdev.this_device, NULL) < 0) {
tzdev_print(0, "Getting CMA info failed;"
" Memory will not be registered in SWd\n");
goto out;
}
tzdev_cma_addr = cma_alloc(tzdev.this_device, NULL,
tzdev_cma_info.total_size, 0);
#endif
if (!tzdev_cma_addr || IS_ERR_VALUE(tzdev_cma_addr))
tzdev_print(0, "Allocation CMA region failed;"
" Memory will not be registered in SWd\n");
out:
#endif
tzdev_init_migration();
register_syscore_ops(&tzdev_syscore_ops);
hrtimer_init(&tzdev_get_event_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
tzdev_get_event_timer.function = tzdev_get_event_timer_handler;
#if defined(CONFIG_TZDEV_MSM_CRYPTO_WORKAROUND)
/* Force crypto engine clocks on */
msm_crypto_workaround();
#endif
return rc;
}
static void __exit exit_tzdev(void)
{
misc_deregister(&tzdev);
idr_for_each(&tzdev_mem_map, __tzdev_mem_free, NULL);
IDR_REMOVE_ALL(&tzdev_mem_map);
idr_destroy(&tzdev_mem_map);
#if defined(CONFIG_TZDEV_CMA)
if (tzdev_cma_addr) {
#if defined(CONFIG_DMA_CMA)
dma_release_from_contiguous(tzdev.this_device, tzdev_page,
(CONFIG_TZDEV_MEMRESSZ - CONFIG_TZDEV_MEMRESSZPROT) >> PAGE_SHIFT);
#elif defined(CONFIG_CMA)
cma_free(tzdev_cma_addr);
#endif
}
tzdev_cma_addr = 0;
#endif
hrtimer_cancel(&tzdev_get_event_timer);
unregister_syscore_ops(&tzdev_syscore_ops);
tzdev_fini_migration();
tzdev_shutdown();
#if defined(CONFIG_TZDEV_QC_CRYPTO_CLOCKS_MANAGEMENT)
tzdev_qc_pm_clock_finalize();
tzdev_platform_unregister();
#endif
}
module_init(init_tzdev);
module_exit(exit_tzdev);
|