hc
2023-12-04 f33f61bdb7ca6d5ebe7a78f9d8694b91360279ac
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
/*
 * Copyright (c) 2014, STMicroelectronics International N.V.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.
 *
 * 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/slab.h>
#include <linux/device.h>
#include <linux/types.h>
#include <linux/file.h>
#include <linux/atomic.h>
#include <linux/uaccess.h>
#include <linux/sched.h>
 
#include "tee_shm.h"
#include "tee_core_priv.h"
 
 
/**
 * tee_context_dump -    Dump in a buffer the informations (ctx, sess & shm)
 *            associated to a tee.
 */
int tee_context_dump(struct tee *tee, char *buff, size_t len)
{
   struct list_head *ptr_ctx, *ptr_sess, *ptr_shm;
   struct tee_context *ctx;
   struct tee_session *sess;
   struct tee_shm *shm;
   int i = 0;
   int j = 0;
 
   int pos = 0;
 
   BUG_ON(!tee);
 
   if (len < 80 || list_empty(&tee->list_ctx))
       return 0;
 
   mutex_lock(&tee->lock);
 
   list_for_each(ptr_ctx, &tee->list_ctx) {
       ctx = list_entry(ptr_ctx, struct tee_context, entry);
 
       pos += sprintf(buff + pos,
               "[%02d] ctx=%p (refcount=%d) (usr=%d)",
               i, ctx,
               (int)refcount_read(&ctx->refcount.
                   refcount),
               ctx->usr_client);
       pos += sprintf(buff + pos, "name=\"%s\" (tgid=%d)\n",
               ctx->name,
               ctx->tgid);
       if ((len - pos) < 80) {
           pos = 0;
           goto out;
       }
 
       if (list_empty(&ctx->list_sess))
           goto out;
 
       j = 0;
       list_for_each(ptr_sess, &ctx->list_sess) {
           sess = list_entry(ptr_sess,
                   struct tee_session,
                   entry);
 
           pos += sprintf(buff + pos,
                   "[%02d.%d] sess=%p sessid=%08x\n",
                   i, j, sess,
                   sess->sessid);
 
           if ((len - pos) < 80) {
               pos = 0;
               goto out;
           }
 
           j++;
       }
 
       if (list_empty(&ctx->list_shm))
           goto out;
 
       j = 0;
       list_for_each(ptr_shm, &ctx->list_shm) {
           shm = list_entry(ptr_shm, struct tee_shm, entry);
 
           pos += sprintf(buff + pos,
                   "[%02d.%d] shm=%p paddr=%p kaddr=%p",
                   i, j, shm,
                   &shm->paddr,
                   shm->kaddr);
           pos += sprintf(buff + pos,
                   " s=%zu(%zu)\n",
                   shm->size_req,
                   shm->size_alloc);
           if ((len - pos) < 80) {
               pos = 0;
               goto out;
           }
 
           j++;
       }
 
       i++;
   }
 
out:
   mutex_unlock(&tee->lock);
   return pos;
}
 
/**
 * tee_context_create - Allocate and create a new context.
 *            Reference on the back-end is requested.
 */
struct tee_context *tee_context_create(struct tee *tee)
{
   int ret;
   struct tee_context *ctx;
 
   dev_dbg(_DEV(tee), "%s: >\n", __func__);
 
   ctx = devm_kzalloc(_DEV(tee), sizeof(struct tee_context), GFP_KERNEL);
   if (!ctx) {
       dev_err(_DEV(tee), "%s: tee_context allocation failed\n",
           __func__);
       return ERR_PTR(-ENOMEM);
   }
 
   kref_init(&ctx->refcount);
   INIT_LIST_HEAD(&ctx->list_sess);
   INIT_LIST_HEAD(&ctx->list_shm);
 
   ctx->tee = tee;
   snprintf(ctx->name, sizeof(ctx->name), "%s", current->comm);
   ctx->tgid = current->tgid;
 
   ret = tee_get(tee);
   if (ret) {
       devm_kfree(_DEV(tee), ctx);
       return ERR_PTR(ret);
   }
 
   mutex_lock(&tee->lock);
   tee_inc_stats(&tee->stats[TEE_STATS_CONTEXT_IDX]);
   list_add_tail(&ctx->entry, &tee->list_ctx);
   mutex_unlock(&tee->lock);
 
   dev_dbg(_DEV(ctx->tee), "%s: < ctx=%p is created\n", __func__, ctx);
   return ctx;
}
 
/**
 * _tee_context_do_release - Final function to release
 *                           and free a context.
 */
static void _tee_context_do_release(struct kref *kref)
{
   struct tee_context *ctx;
   struct tee *tee;
 
   ctx = container_of(kref, struct tee_context, refcount);
 
   BUG_ON(!ctx || !ctx->tee);
 
   tee = ctx->tee;
 
   dev_dbg(_DEV(tee), "%s: > ctx=%p\n", __func__, ctx);
 
   tee_dec_stats(&tee->stats[TEE_STATS_CONTEXT_IDX]);
   list_del(&ctx->entry);
 
   devm_kfree(_DEV(tee), ctx);
   tee_put(tee);
 
   dev_dbg(_DEV(tee), "%s: < ctx=%p is destroyed\n", __func__, ctx);
}
 
/**
 * tee_context_get - Increase the reference count of
 *                   the context.
 */
void tee_context_get(struct tee_context *ctx)
{
   BUG_ON(!ctx || !ctx->tee);
 
   kref_get(&ctx->refcount);
 
   dev_dbg(_DEV(ctx->tee), "%s: ctx=%p, kref=%d\n", __func__,
       ctx, (int)refcount_read(&ctx->refcount.refcount));
}
 
static int is_in_list(struct tee *tee, struct list_head *entry)
{
   int present = 1;
 
   if ((entry->next == LIST_POISON1) && (entry->prev == LIST_POISON2))
       present = 0;
   return present;
}
 
/**
 * tee_context_put - Decreases the reference count of
 *                   the context. If 0, the final
 *                   release function is called.
 */
void tee_context_put(struct tee_context *ctx)
{
   struct tee_context *_ctx = ctx;
   struct tee *tee;
 
   BUG_ON(!ctx || !ctx->tee);
   tee = ctx->tee;
 
   if (!is_in_list(tee, &ctx->entry))
       return;
 
   kref_put(&ctx->refcount, _tee_context_do_release);
 
   dev_dbg(_DEV(tee), "%s: ctx=%p, kref=%d\n", __func__,
       _ctx, (int)refcount_read(&ctx->refcount.refcount));
}
 
/**
 * tee_context_destroy - Request to destroy a context.
 */
void tee_context_destroy(struct tee_context *ctx)
{
   struct tee *tee;
 
   if (!ctx || !ctx->tee)
       return;
 
   tee = ctx->tee;
 
   dev_dbg(_DEV(tee), "%s: ctx=%p\n", __func__, ctx);
 
   mutex_lock(&tee->lock);
   tee_context_put(ctx);
   mutex_unlock(&tee->lock);
}
 
int tee_context_copy_from_client(const struct tee_context *ctx,
                void *dest, const void *src, size_t size)
{
   int res = 0;
 
   if (dest && src && (size > 0)) {
       if (ctx->usr_client)
           res = copy_from_user(dest, src, size);
       else
           memcpy(dest, src, size);
   }
   return res;
}
 
struct tee_shm *tee_context_alloc_shm_tmp(struct tee_context *ctx,
                     size_t size, const void *src,
                     int type)
{
   struct tee_shm *shm;
 
   type &= (TEEC_MEM_INPUT | TEEC_MEM_OUTPUT);
 
   shm = rk_tee_shm_alloc(ctx->tee, size,
           TEE_SHM_MAPPED | TEE_SHM_TEMP | type);
   if (IS_ERR_OR_NULL(shm)) {
       dev_err(_DEV(ctx->tee), "%s: buffer allocation failed (%ld)\n",
           __func__, PTR_ERR(shm));
       return shm;
   }
 
   shm->ctx = ctx;
 
   if (type & TEEC_MEM_INPUT) {
       if (tee_context_copy_from_client(ctx, shm->kaddr, src, size)) {
           dev_err(_DEV(ctx->tee),
               "%s: tee_context_copy_from_client failed\n",
               __func__);
           rk_tee_shm_free(shm);
           shm = NULL;
       }
   }
   return shm;
}
 
struct tee_shm *tee_context_create_tmpref_buffer(struct tee_context *ctx,
                        size_t size,
                        const void *buffer, int type)
{
   struct tee_shm *shm = NULL;
   int flags;
 
   switch (type) {
   case TEEC_MEMREF_TEMP_OUTPUT:
       flags = TEEC_MEM_OUTPUT;
       break;
   case TEEC_MEMREF_TEMP_INPUT:
       flags = TEEC_MEM_INPUT;
       break;
   case TEEC_MEMREF_TEMP_INOUT:
       flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
       break;
   default:
       BUG_ON(1);
   };
   shm = tee_context_alloc_shm_tmp(ctx, size, buffer, flags);
   return shm;
}