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
|
/*
* Copyright (C) 2012-2019, Samsung Electronics Co., Ltd.
*
* 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/atomic.h>
#include <linux/err.h>
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/uaccess.h>
#include <tzdev/tee_client_api.h>
#include "teec/iw_messages.h"
#include "teec/misc.h"
#include "teec/pmf_defs.h"
#include "core/cdev.h"
#include "core/iwsock.h"
#include "core/log.h"
#include "core/notifier.h"
#include "debug/pmf.h"
static atomic_t tz_pmf_init_done = ATOMIC_INIT(0);
static struct sock_desc *sd;
static uint32_t serial;
static pid_t tz_pmf_pid;
static struct pmf_data *pmf_sample = NULL;
static uint32_t tzdev_teec_pmf_init(struct tzio_pmf_init *s, uint32_t *origin)
{
struct cmd_init_pmf cmd;
struct cmd_reply_init_pmf ack;
uint32_t result;
int ret;
*origin = TEEC_ORIGIN_API;
serial = current->pid;
cmd.base.cmd = CMD_INIT_PMF;
cmd.base.serial = serial;
cmd.pmf_shmem_id = s->id;
ret = tzdev_teec_send_then_recv(sd,
&cmd, sizeof(cmd), 0x0,
&ack, sizeof(ack), 0x0,
&result, origin);
if (ret < 0) {
log_error(tzdev_pmf, "Failed to pass pmf init command, ret = %d\n", ret);
goto out;
}
ret = tzdev_teec_check_reply(&ack.base, CMD_REPLY_INIT_PMF,
serial, &result, origin);
if (ret) {
log_error(tzdev_pmf, "Failed to check pmf init command, ret = %d\n", ret);
goto out;
}
result = ack.base.result;
*origin = ack.base.origin;
out:
tzdev_teec_fixup_origin(result, origin);
return result;
}
static uint32_t tzdev_teec_pmf_fini(uint32_t *origin)
{
struct cmd_init_pmf cmd;
struct cmd_reply_init_pmf ack;
uint32_t result;
int ret;
*origin = TEEC_ORIGIN_API;
cmd.base.cmd = CMD_FINI_PMF;
cmd.base.serial = current->pid;
ret = tzdev_teec_send_then_recv(sd,
&cmd, sizeof(cmd), 0x0,
&ack, sizeof(ack), 0x0,
&result, origin);
if (ret < 0) {
log_error(tzdev_pmf, "Failed to pass pmf fini command, ret = %d\n", ret);
goto out;
}
ret = tzdev_teec_check_reply(&ack.base, CMD_REPLY_FINI_PMF,
serial, &result, origin);
if (ret) {
log_error(tzdev_pmf, "Failed to check pmf fini command, ret = %d\n", ret);
goto out;
}
result = ack.base.result;
*origin = ack.base.origin;
out:
serial = 0;
tzdev_teec_fixup_origin(result, origin);
return result;
}
static int tzdev_pmf_get_ticks(struct file *filp, unsigned long arg)
{
enum pmf_id __user id = (enum pmf_id)arg;
(void) filp;
tzdev_pmf_stamp(id);
return 0;
}
static int tzdev_pmf_init(struct file *filp, unsigned long arg)
{
int ret;
uint32_t result;
uint32_t origin;
struct tzio_pmf_init __user *argp = (struct tzio_pmf_init __user *)arg;
struct tzio_pmf_init s;
(void) filp;
if (copy_from_user(&s, argp, sizeof(struct tzio_pmf_init)))
return -EFAULT;
sd = tz_iwsock_socket(1, TZ_NON_INTERRUPTIBLE);
if (IS_ERR(sd))
goto out;
ret = tzdev_teec_connect(sd, ROOT_TASK_SOCK, &result, &origin);
if (ret < 0) {
log_error(tzdev_pmf, "Failed to connect to root_task socket, ret = %d\n", ret);
goto out_close_conn;
}
result = tzdev_teec_pmf_init(&s, &origin);
if (result != TEEC_SUCCESS) {
log_error(tzdev_pmf, "Failed to initialize pmf, result = %#x\n", result);
goto out_close_conn;
}
tz_pmf_pid = current->pid;
pmf_sample = (struct pmf_data *)s.ptr;
return result;
out_close_conn:
tzdev_teec_disconnect(sd);
sd = NULL;
out:
return result;
}
static int tzdev_pmf_fini(struct file *filp, unsigned long arg)
{
uint32_t result;
uint32_t origin;
(void) filp;
(void) arg;
if (sd == NULL) {
result = TEEC_ERROR_BAD_STATE;
goto out;
}
result = tzdev_teec_pmf_fini(&origin);
if (result != TEEC_SUCCESS)
log_error(tzdev_pmf, "Failed to fini pmf, result = %#x", result);
tzdev_teec_disconnect(sd);
out:
tz_pmf_pid = 0;
pmf_sample = NULL;
return result;
}
static long tzdev_pmf_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
switch (cmd) {
case TZIO_PMF_INIT:
return tzdev_pmf_init(filp, arg);
case TZIO_PMF_FINI:
return tzdev_pmf_fini(filp, arg);
case TZIO_PMF_GET_TICKS:
return tzdev_pmf_get_ticks(filp, arg);
default:
return -ENOTTY;
}
}
static const struct file_operations tzdev_pmf_fops = {
.owner = THIS_MODULE,
.unlocked_ioctl = tzdev_pmf_ioctl,
#ifdef CONFIG_COMPAT
.compat_ioctl = tzdev_pmf_ioctl,
#endif /* CONFIG_COMPAT */
};
static struct tz_cdev tz_pmf_cdev = {
.name = "tz_pmf",
.fops = &tzdev_pmf_fops,
.owner = THIS_MODULE,
};
void tzdev_pmf_stamp(enum pmf_id id)
{
uint64_t ticks = read_ticks();
if (current->pid != tz_pmf_pid)
return;
if (copy_to_user(&pmf_sample->samples[id], &ticks, sizeof(uint64_t)))
log_error(tzdev_pmf, "Unable to copy the ticks.\n");
}
static int tz_pmf_init_call(struct notifier_block *cb, unsigned long code, void *unused)
{
int rc;
(void)cb;
(void)code;
(void)unused;
rc = tz_cdev_register(&tz_pmf_cdev);
if (rc) {
log_info(tzdev_pmf, "Failed to create pmf device, error=%d\n", rc);
return NOTIFY_DONE;
}
atomic_set(&tz_pmf_init_done, 1);
log_info(tzdev_pmf, "PMF initialization done.");
return NOTIFY_DONE;
}
static int tz_pmf_fini_call(struct notifier_block *cb, unsigned long code, void *unused)
{
(void)cb;
(void)code;
(void)unused;
if (!atomic_cmpxchg(&tz_pmf_init_done, 1, 0)) {
log_info(tzdev_pmf, "PMF not initialized.");
return NOTIFY_DONE;
}
tz_cdev_unregister(&tz_pmf_cdev);
log_info(tzdev_pmf, "PMF finalization done.");
return NOTIFY_DONE;
}
static struct notifier_block tz_pmf_init_notifier = {
.notifier_call = tz_pmf_init_call,
};
static struct notifier_block tz_pmf_fini_notifier = {
.notifier_call = tz_pmf_fini_call,
};
static int __init tz_pmf_init(void)
{
int rc;
rc = tzdev_blocking_notifier_register(TZDEV_INIT_NOTIFIER, &tz_pmf_init_notifier);
if (rc) {
log_error(tzdev_pmf, "Failed to register init notifier, error=%d\n", rc);
return rc;
}
rc = tzdev_blocking_notifier_register(TZDEV_FINI_NOTIFIER, &tz_pmf_fini_notifier);
if (rc) {
tzdev_blocking_notifier_unregister(TZDEV_INIT_NOTIFIER, &tz_pmf_init_notifier);
log_error(tzdev_pmf, "Failed to register fini notifier, error=%d\n", rc);
return rc;
}
log_info(tzdev_pmf, "PMF callbacks registration done\n");
return 0;
}
early_initcall(tz_pmf_init);
|