hc
2023-11-06 1622ff3442ff6aecc1f538cda437379d1f6a4a93
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
/*
 * 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/kernel.h>
#include <linux/slab.h>
#include <linux/stat.h>
#include <linux/atomic.h>
#include <asm/page.h>
 
#include "tee_core_priv.h"
 
static ssize_t dump_show(struct device *device,
            struct device_attribute *attr, char *buf)
{
   struct tee *tee = dev_get_drvdata(device);
   int len;
   char *tmp_buf;
 
   tmp_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
   if (!tmp_buf) {
       printk(KERN_ALERT "%s : Unable to get buf memory\n", __func__);
       return -ENOMEM;
   }
 
   len = tee_context_dump(tee, tmp_buf, PAGE_SIZE - 128);
 
   if (len > 0)
       len = snprintf(buf, PAGE_SIZE, "%s", tmp_buf);
   kfree(tmp_buf);
 
   return len;
}
 
static ssize_t stat_show(struct device *device,
            struct device_attribute *attr, char *buf)
{
   struct tee *tee = dev_get_drvdata(device);
 
   return snprintf(buf, PAGE_SIZE, "%d/%d %d/%d %d/%d %d/%d\n",
           atomic_read(&tee->refcount),
           tee->max_refcount,
           tee->stats[TEE_STATS_CONTEXT_IDX].count,
           tee->stats[TEE_STATS_CONTEXT_IDX].max,
           tee->stats[TEE_STATS_SESSION_IDX].count,
           tee->stats[TEE_STATS_SESSION_IDX].max,
           tee->stats[TEE_STATS_SHM_IDX].count,
           tee->stats[TEE_STATS_SHM_IDX].max);
}
 
static ssize_t info_show(struct device *device,
            struct device_attribute *attr, char *buf)
{
   struct tee *tee = dev_get_drvdata(device);
 
   return snprintf(buf, PAGE_SIZE, "%s iminor=%d dev=\"%s\" state=%d\n",
           dev_name(tee->dev), tee->miscdev.minor,
           dev_name(tee->miscdev.this_device), tee->state);
}
 
static ssize_t name_show(struct device *device,
            struct device_attribute *attr, char *buf)
{
   struct tee *tee = dev_get_drvdata(device);
 
   return snprintf(buf, PAGE_SIZE, "%s\n", tee->name);
}
 
static ssize_t type_show(struct device *device,
            struct device_attribute *attr, char *buf)
{
   struct tee *tee = dev_get_drvdata(device);
 
   return snprintf(buf, PAGE_SIZE, "%s\n", tee->ops->type);
}
 
static ssize_t refcount_show(struct device *device,
                struct device_attribute *attr, char *buf)
{
   struct tee *tee = dev_get_drvdata(device);
 
   return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&tee->refcount));
}
 
static ssize_t conf_show(struct device *device,
            struct device_attribute *attr, char *buf)
{
   struct tee *tee = dev_get_drvdata(device);
 
   return snprintf(buf, PAGE_SIZE, "0x%08x\n", tee->conf);
}
 
static ssize_t test_show(struct device *device,
            struct device_attribute *attr, char *buf)
{
   struct tee *tee = dev_get_drvdata(device);
 
   return snprintf(buf, PAGE_SIZE, "%08X\n", tee->test);
}
 
static ssize_t test_store(struct device *device,
             struct device_attribute *attr, const char *buf,
             size_t count)
{
   struct tee *tee = dev_get_drvdata(device);
   unsigned long val;
   int status;
 
   status = kstrtoul(buf, 0, &val);
   if (status)
       return status;
 
   if ((tee->conf & TEE_CONF_TEST_MODE) == TEE_CONF_TEST_MODE)
       tee->test = val;
 
   return count;
}
 
/*
 * A state-to-string lookup table, for exposing a human readable state
 * via sysfs. Always keep in sync with enum tee_state
 */
static const char *const tee_state_string[] = {
   "offline",
   "online",
   "suspended",
   "running",
   "crashed",
   "invalid",
};
 
static ssize_t tee_show_state(struct device *device,
             struct device_attribute *attr, char *buf)
{
   struct tee *tee = dev_get_drvdata(device);
 
   int state = tee->state > TEE_LAST ? TEE_LAST : tee->state;
 
   return snprintf(buf, PAGE_SIZE, "%s (%d)\n", tee_state_string[state],
           tee->state);
}
 
/*
 * In the following, 0660 is (S_IWUGO | S_IRUGO)
 */
static struct device_attribute device_attrs[] = {
   __ATTR_RO(dump),
   __ATTR_RO(stat),
   __ATTR_RO(info),
   __ATTR(test, (0660), test_show, test_store),
   __ATTR(state, S_IRUGO, tee_show_state, NULL),
   __ATTR(name, S_IRUGO, name_show, NULL),
   __ATTR(refcount, S_IRUGO, refcount_show, NULL),
   __ATTR(type, S_IRUGO, type_show, NULL),
   __ATTR(conf, S_IRUGO, conf_show, NULL),
};
 
void tee_init_sysfs(struct tee *tee)
{
   int i, error = 0;
 
   if (!tee)
       return;
 
   if (dev_get_drvdata(tee->miscdev.this_device) != tee) {
       dev_err(_DEV(tee), "drvdata is not valid\n");
       return;
   }
 
   for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
       error =
           device_create_file(tee->miscdev.this_device,
                      &device_attrs[i]);
       if (error)
           break;
   }
 
   if (error) {
       while (--i >= 0)
           device_remove_file(tee->miscdev.this_device,
                      &device_attrs[i]);
   }
   /* location /sys/class/<class name>/<dev_name()>/<name> ->
    * /sys/class/misc/teelx00/info */
}
 
void tee_cleanup_sysfs(struct tee *tee)
{
   int i;
 
   if (!tee)
       return;
 
   for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
       device_remove_file(tee->miscdev.this_device, &device_attrs[i]);
}