summaryrefslogtreecommitdiffstats
path: root/security/tlk_driver/ote_comms.c
blob: 5a7d750cd243ecc56bfb60f38790a78e3aff334c (plain) (blame)
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
/*
 * Copyright (c) 2012-2019 NVIDIA Corporation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <linux/atomic.h>
#include <linux/uaccess.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/printk.h>
#include <linux/ioctl.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/pagemap.h>
#include <linux/ote_protocol.h>
#include <asm/smp_plat.h>

#include "ote_protocol.h"

bool verbose_smc;
core_param(verbose_smc, verbose_smc, bool, 0644);

#define SET_RESULT(req, r, ro)	{ req->result = r; req->result_origin = ro; }

static struct te_session *te_get_session(struct tlk_context *context,
					uint32_t session_id)
{
	struct te_session *session, *tmp_session;

	list_for_each_entry_safe(session, tmp_session,
		&context->session_list, list) {
		if (session->session_id == session_id)
			return session;
	}

	return NULL;
}

#ifdef CONFIG_SMP
cpumask_t saved_cpu_mask;
static long switch_cpumask_to_cpu0(void)
{
	long ret;
	cpumask_t local_cpu_mask = CPU_MASK_NONE;

	cpumask_set_cpu(0, &local_cpu_mask);
	cpumask_copy(&saved_cpu_mask, &current->cpus_allowed);
	ret = sched_setaffinity(0, &local_cpu_mask);
	if (ret)
		pr_err("%s: sched_setaffinity #1 -> 0x%lX", __func__, ret);

	return ret;
}

static void restore_cpumask(void)
{
	long ret = sched_setaffinity(0, &saved_cpu_mask);
	if (ret)
		pr_err("%s: sched_setaffinity #2 -> 0x%lX", __func__, ret);
}
#else
static inline long switch_cpumask_to_cpu0(void) { return 0; };
static inline void restore_cpumask(void) {};
#endif

struct tlk_smc_work_args {
	uint32_t arg0;
	uintptr_t arg1;
	uint32_t arg2;
};

static long tlk_generic_smc_on_cpu0(void *args)
{
	struct tlk_smc_work_args *work;
	int callback_status = 0;
	uint32_t retval;

	work = (struct tlk_smc_work_args *)args;
	retval = _tlk_generic_smc(work->arg0, work->arg1, work->arg2);

	while (retval == TE_ERROR_PREEMPT_BY_IRQ ||
	       retval == TE_ERROR_PREEMPT_BY_FS) {
		if (retval == TE_ERROR_PREEMPT_BY_FS)
			callback_status = tlk_ss_op();
		retval = _tlk_generic_smc(TE_SMC_RESTART, callback_status, 0);
	}

	/* Print TLK logs if any */
	ote_print_logs();

	return retval;
}

/*
 * This routine is called both from normal threads and worker threads.
 * The worker threads are per-cpu and have PF_NO_SETAFFINITY set, so
 * any calls to sched_setaffinity will fail.
 *
 * If it's a worker thread on CPU0, just invoke the SMC directly. If
 * it's running on a non-CPU0, use work_on_cpu() to schedule the SMC
 * on CPU0.
 */
uint32_t tlk_send_smc(uint32_t arg0, uintptr_t arg1, uintptr_t arg2)
{
	long ret;
	struct tlk_smc_work_args work_args;

	work_args.arg0 = arg0;
	work_args.arg1 = arg1;
	work_args.arg2 = arg2;

	if (current->flags &
	    (PF_WQ_WORKER | PF_NO_SETAFFINITY | PF_KTHREAD)) {
		int cpu = cpu_logical_map(get_cpu());
		put_cpu();

		/* workers don't change CPU. depending on the CPU, execute
		 * directly or sched work */
		if (cpu == 0 && (current->flags & PF_WQ_WORKER))
			return tlk_generic_smc_on_cpu0(&work_args);
		else
			return work_on_cpu(0,
					tlk_generic_smc_on_cpu0, &work_args);
	}

	/* switch to CPU0 */
	ret = switch_cpumask_to_cpu0();
	if (ret) {
		/* not able to switch, schedule work on CPU0 */
		ret = work_on_cpu(0, tlk_generic_smc_on_cpu0, &work_args);
	} else {
		/* switched to CPU0 */
		ret = tlk_generic_smc_on_cpu0(&work_args);
		restore_cpumask();
	}

	return ret;
}

/*
 * Do an SMC call
 */
static void do_smc(struct te_request *request, struct tlk_device *dev)
{
	uint32_t smc_args;
	uint32_t smc_params = 0;
	uint32_t retval = 0;

	smc_args = (uintptr_t)request - (uintptr_t)dev->req_addr;
	if (request->params) {
		smc_params =
			(uintptr_t)request->params - (uintptr_t)dev->req_addr;
	}

	retval = tlk_send_smc(request->type, smc_args, smc_params);

	/**
	 * Check for return code from TLK kernel and propagate to NS userspace
	 *
	 * Under certain situations, TLK kernel may return an error code
	 * _without_ updating the shared request buffer.
	 *
	 * This could give a false impression to the user-app about status of
	 * the request (if default value of request->result is OTE_SUCCESS).
	 *
	 * Propagate the error code to NS user-app in case of such scenario.
	 */
	if ((retval != OTE_SUCCESS) && (request->result == OTE_SUCCESS)) {
		pr_err("%s: overriding result_origin field\n", __func__);
		request->result = retval;
		request->result_origin = OTE_RESULT_ORIGIN_KERNEL;
	}
}

void tlk_restore_keyslots(void)
{
	uint32_t retval;

	if (!te_is_secos_dev_enabled())
		return;

	/* Share the same lock used when request is send from user side */
	mutex_lock(&smc_lock);

	retval = tlk_send_smc(TE_SMC_TA_EVENT, TA_EVENT_RESTORE_KEYS, 0);

	mutex_unlock(&smc_lock);

	if (retval != OTE_SUCCESS) {
		pr_err("%s: smc failed err (0x%x)\n", __func__, retval);
	}
}
EXPORT_SYMBOL(tlk_restore_keyslots);

/*
 * Open session SMC (supporting client-based te_open_session() calls)
 */
void te_open_session(struct te_opensession *cmd,
		    struct te_request *request,
		    struct tlk_context *context)
{
	struct te_session *session;
	int ret;

	session = kzalloc(sizeof(struct te_session), GFP_KERNEL);
	if (!session) {
		SET_RESULT(request, OTE_ERROR_OUT_OF_MEMORY,
			OTE_RESULT_ORIGIN_API);
		return;
	}

	INIT_LIST_HEAD(&session->list);
	INIT_LIST_HEAD(&session->temp_shmem_list);
	INIT_LIST_HEAD(&session->inactive_persist_shmem_list);
	INIT_LIST_HEAD(&session->persist_shmem_list);

	request->type = TE_SMC_OPEN_SESSION;

	ret = te_prep_mem_buffers(request, session);
	if (ret != OTE_SUCCESS) {
		pr_err("%s: te_prep_mem_buffers failed err (0x%x)\n",
			__func__, ret);
		SET_RESULT(request, ret, OTE_RESULT_ORIGIN_API);
		kfree(session);
		return;
	}

	memcpy(&request->dest_uuid,
	       &cmd->dest_uuid,
	       sizeof(struct te_service_id));

	pr_debug("OPEN_CLIENT_SESSION: 0x%x 0x%x 0x%x 0x%x\n",
		request->dest_uuid[0],
		request->dest_uuid[1],
		request->dest_uuid[2],
		request->dest_uuid[3]);

	do_smc(request, context->dev);

	if (request->result)
		goto error;

	/* release temporary mem buffers */
	te_release_mem_buffers(&session->temp_shmem_list);

	/* move persistent mem buffers to active list */
	te_activate_persist_mem_buffers(session);

	/* save off session_id and add to list */
	session->session_id = request->session_id;
	list_add_tail(&session->list, &context->session_list);

	return;
error:
	/* release buffers allocated in te_prep_mem_buffers */
	te_release_mem_buffers(&session->temp_shmem_list);
	te_release_mem_buffers(&session->inactive_persist_shmem_list);

	kfree(session);
}

/*
 * Close session SMC (supporting client-based te_close_session() calls)
 */
void te_close_session(struct te_closesession *cmd,
		     struct te_request *request,
		     struct tlk_context *context)
{
	struct te_session *session;

	request->session_id = cmd->session_id;
	request->type = TE_SMC_CLOSE_SESSION;

	do_smc(request, context->dev);
	if (request->result)
		pr_info("%s: error closing session: 0x%08x\n",
			__func__, request->result);

	session = te_get_session(context, cmd->session_id);
	if (!session) {
		pr_info("%s: session_id not found: 0x%x\n",
			__func__, cmd->session_id);
		return;
	}

	/* free session state */
	te_release_mem_buffers(&session->persist_shmem_list);
	list_del(&session->list);
	kfree(session);
}

/*
 * Launch operation SMC (supporting client-based te_launch_operation() calls)
 */
void te_launch_operation(struct te_launchop *cmd,
			struct te_request *request,
			struct tlk_context *context)
{
	struct te_session *session;
	int ret;

	session = te_get_session(context, cmd->session_id);
	if (!session) {
		pr_info("%s: session_id not found: 0x%x\n",
			__func__, cmd->session_id);
		SET_RESULT(request, OTE_ERROR_BAD_PARAMETERS,
				OTE_RESULT_ORIGIN_API);
		return;
	}

	request->session_id = cmd->session_id;
	request->command_id = cmd->operation.command;
	request->type = TE_SMC_LAUNCH_OPERATION;

	ret = te_prep_mem_buffers(request, session);
	if (ret != OTE_SUCCESS) {
		pr_err("%s: te_prep_mem_buffers failed err (0x%x)\n",
			__func__, ret);
		SET_RESULT(request, ret, OTE_RESULT_ORIGIN_API);
		return;
	}

	do_smc(request, context->dev);

	if (request->result)
		goto error;

	/* move persistent mem buffers to active list */
	te_activate_persist_mem_buffers(session);

	/* release temporary mem buffers */
	te_release_mem_buffers(&session->temp_shmem_list);

	return;

error:
	/* release buffers allocated in te_prep_mem_buffers */
	te_release_mem_buffers(&session->temp_shmem_list);
	te_release_mem_buffers(&session->inactive_persist_shmem_list);
}

/*
 * Command to open a session with the trusted app.
 * This API should only be called from the kernel space.
 * Takes UUID of the TA and size as argument
 * Returns Session ID if success or ERR when failure
 */
int te_open_trusted_session_tlk(u32 *ta_uuid, u32 uuid_size,
				u32 *session_id)
{
	struct te_request *request;
	struct te_cmd_req_desc *cmd_desc = NULL;

	mutex_lock(&smc_lock);

	/* Open & submit the work to SMC */
	cmd_desc = te_get_free_cmd_desc(&tlk_dev);

	if (!cmd_desc) {
		pr_err("%s: failed to get cmd_desc\n", __func__);
		goto error;
	}

	request = cmd_desc->req_addr;
	memset(request, 0, sizeof(struct te_request));
	request->type = TE_SMC_OPEN_SESSION;

	if (uuid_size != sizeof(request->dest_uuid)) {
		pr_err("%s: Invalid size sent!\n", __func__);
		goto error;
	}

	if (ta_uuid == NULL || session_id == NULL) {
		pr_err("%s: Null parameters sent!\n", __func__);
		goto error;
	}

	memcpy(request->dest_uuid, ta_uuid, sizeof(*ta_uuid)*4);

	do_smc(request, &tlk_dev);

	if (request->result) {
		pr_err("%s: error opening session: 0x%08x, 0x%08x\n",
		__func__, request->result, request->result_origin);
		goto error;
	}

	*session_id = request->session_id;
	if (cmd_desc)
		te_put_used_cmd_desc(&tlk_dev, cmd_desc);

	mutex_unlock(&smc_lock);
	return OTE_SUCCESS;

error:
	if (cmd_desc)
		te_put_used_cmd_desc(&tlk_dev, cmd_desc);

	mutex_unlock(&smc_lock);
	return OTE_ERROR_GENERIC;
}
EXPORT_SYMBOL(te_open_trusted_session_tlk);

/*
 * Command to close session opened with the trusted app.
 * This API should only be called from the kernel space.
 * Takes session Id and UUID of the TA as arguments
 */
void te_close_trusted_session_tlk(u32 session_id, u32 *ta_uuid,
				u32 uuid_size)
{
	struct te_request *request;
	struct te_cmd_req_desc *cmd_desc = NULL;

	mutex_lock(&smc_lock);

	/* Open & submit the work to SMC */
	cmd_desc = te_get_free_cmd_desc(&tlk_dev);

	if (!cmd_desc) {
		pr_err("%s: failed to get cmd_desc\n", __func__);
		goto error;
	}

	/* Close the session */
	request = cmd_desc->req_addr;
	memset(request, 0, sizeof(struct te_request));
	request->type = TE_SMC_CLOSE_SESSION;
	request->session_id = session_id;

	if (uuid_size != sizeof(request->dest_uuid)) {
		pr_err("%s: Invalid size sent!\n", __func__);
		goto error;
	}

	memcpy(request->dest_uuid, ta_uuid, sizeof(*ta_uuid)*4);

	do_smc(request, &tlk_dev);

	if (request->result) {
		pr_err("%s: error closing session: 0x%08x, 0x%08x\n",
		__func__, request->result, request->result_origin);
		goto error;
	}

error:
	if (cmd_desc)
		te_put_used_cmd_desc(&tlk_dev, cmd_desc);
	mutex_unlock(&smc_lock);
}
EXPORT_SYMBOL(te_close_trusted_session_tlk);

/*
 * Command to launch operations from the linux kernel to
 * the trusted app.
 * This API should be called only from the kernel space.
 * The pointer to the buffer from the kernel, length of thus buffer,
 * session Id, UUID of the TA and the command requested from the
 * TA should be passed as arguments
 * Returns SUCCESS or FAILURE
 */
int te_launch_trusted_oper_tlk(u8 *buf_ptr, u32 buf_len, u32 session_id,
			u32 *ta_uuid, u32 ta_cmd, u32 uuid_size)
{
	u32 i;
	struct te_request *request;
	struct te_oper_param user_param;
	struct te_oper_param *param_array;
	struct te_oper_param *params = NULL;
	struct te_cmd_req_desc *cmd_desc = NULL;
	u32 no_of_params = 1;

	mutex_lock(&smc_lock);

	/* Open & submit the work to SMC */
	cmd_desc = te_get_free_cmd_desc(&tlk_dev);
	params = te_get_free_params(&tlk_dev, no_of_params);

	if (!cmd_desc || !params) {
		pr_err("%s: failed to get cmd_desc/params\n", __func__);
		goto error;
	}

	/* launch operation */
	request = cmd_desc->req_addr;
	memset(request, 0, sizeof(struct te_request));
	request->params = (uintptr_t)params;
	request->params_size = no_of_params;
	request->session_id = session_id;
	request->command_id = ta_cmd;
	request->type = TE_SMC_LAUNCH_OPERATION;
	user_param.index = 0;
	user_param.u.Mem.len = buf_len;
	user_param.type = TE_PARAM_TYPE_MEM_RW;
	user_param.u.Mem.type = TE_MEM_TYPE_NS_KERNEL;
	user_param.u.Mem.base = (uint64_t)(uintptr_t)buf_ptr;

	if (uuid_size != sizeof(request->dest_uuid)) {
		pr_err("%s: Invalid size sent\n", __func__);
		goto error;
	}

	memcpy(request->dest_uuid, ta_uuid, sizeof(*ta_uuid)*4);

	param_array = (struct te_oper_param *)(uintptr_t)request->params;

	for (i = 0; i < no_of_params; i++)
		memcpy(param_array + i, &user_param,
		sizeof(struct te_oper_param));

	do_smc(request, &tlk_dev);

	if (request->result) {
		pr_err("%s: error launching session: 0x%08x, 0x%08x\n",
		__func__, request->result, request->result_origin);
		goto error;
	} else {
		if (cmd_desc)
			te_put_used_cmd_desc(&tlk_dev, cmd_desc);

		if (params)
			te_put_free_params(&tlk_dev, params, no_of_params);

		mutex_unlock(&smc_lock);
		return OTE_SUCCESS;
	}
error:
	if (cmd_desc)
		te_put_used_cmd_desc(&tlk_dev, cmd_desc);

	if (params)
		te_put_free_params(&tlk_dev, params, no_of_params);

	mutex_unlock(&smc_lock);
	return OTE_ERROR_GENERIC;
}
EXPORT_SYMBOL(te_launch_trusted_oper_tlk);