trusty-log.c
7.19 KB
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
/*
* Copyright (C) 2015 Google, 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/platform_device.h>
#include <linux/trusty/smcall.h>
#include <linux/trusty/trusty.h>
#include <linux/notifier.h>
#include <linux/slab.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/log2.h>
#include <asm/page.h>
#include "trusty-log.h"
#define TRUSTY_LOG_SIZE (PAGE_SIZE * 2)
#define TRUSTY_LINE_BUFFER_SIZE 256
struct trusty_log_state {
struct device *dev;
struct device *trusty_dev;
/*
* This lock is here to ensure only one consumer will read
* from the log ring buffer at a time.
*/
spinlock_t lock;
struct log_rb *log;
uint32_t get;
struct page *log_pages;
struct notifier_block call_notifier;
struct notifier_block panic_notifier;
char line_buffer[TRUSTY_LINE_BUFFER_SIZE];
};
static int log_read_line(struct trusty_log_state *s, int put, int get)
{
struct log_rb *log = s->log;
int i;
char c = '\0';
size_t max_to_read = min((size_t)(put - get),
sizeof(s->line_buffer) - 1);
size_t mask = log->sz - 1;
for (i = 0; i < max_to_read && c != '\n';)
s->line_buffer[i++] = c = log->data[get++ & mask];
s->line_buffer[i] = '\0';
return i;
}
static void trusty_dump_logs(struct trusty_log_state *s)
{
struct log_rb *log = s->log;
uint32_t get, put, alloc;
int read_chars;
BUG_ON(!is_power_of_2(log->sz));
/*
* For this ring buffer, at any given point, alloc >= put >= get.
* The producer side of the buffer is not locked, so the put and alloc
* pointers must be read in a defined order (put before alloc) so
* that the above condition is maintained. A read barrier is needed
* to make sure the hardware and compiler keep the reads ordered.
*/
get = s->get;
while ((put = log->put) != get) {
/* Make sure that the read of put occurs before the read of log data */
rmb();
/* Read a line from the log */
read_chars = log_read_line(s, put, get);
/* Force the loads from log_read_line to complete. */
rmb();
alloc = log->alloc;
/*
* Discard the line that was just read if the data could
* have been corrupted by the producer.
*/
if (alloc - get > log->sz) {
pr_err("trusty: log overflow.");
get = alloc - log->sz;
continue;
}
pr_info("trusty: %s", s->line_buffer);
get += read_chars;
}
s->get = get;
}
static int trusty_log_call_notify(struct notifier_block *nb,
unsigned long action, void *data)
{
struct trusty_log_state *s;
unsigned long flags;
if (action != TRUSTY_CALL_RETURNED)
return NOTIFY_DONE;
s = container_of(nb, struct trusty_log_state, call_notifier);
spin_lock_irqsave(&s->lock, flags);
trusty_dump_logs(s);
spin_unlock_irqrestore(&s->lock, flags);
return NOTIFY_OK;
}
static int trusty_log_panic_notify(struct notifier_block *nb,
unsigned long action, void *data)
{
struct trusty_log_state *s;
/*
* Don't grab the spin lock to hold up the panic notifier, even
* though this is racy.
*/
s = container_of(nb, struct trusty_log_state, panic_notifier);
pr_info("trusty-log panic notifier - trusty version %s",
trusty_version_str_get(s->trusty_dev));
trusty_dump_logs(s);
return NOTIFY_OK;
}
static bool trusty_supports_logging(struct device *device)
{
int result;
result = trusty_std_call32(device, SMC_SC_SHARED_LOG_VERSION,
TRUSTY_LOG_API_VERSION, 0, 0);
if (result == SM_ERR_UNDEFINED_SMC) {
pr_info("trusty-log not supported on secure side.\n");
return false;
} else if (result < 0) {
pr_err("trusty std call (SMC_SC_SHARED_LOG_VERSION) failed: %d\n",
result);
return false;
}
if (result == TRUSTY_LOG_API_VERSION) {
return true;
} else {
pr_info("trusty-log unsupported api version: %d, supported: %d\n",
result, TRUSTY_LOG_API_VERSION);
return false;
}
}
static int trusty_log_probe(struct platform_device *pdev)
{
struct trusty_log_state *s;
int result;
phys_addr_t pa;
dev_dbg(&pdev->dev, "%s\n", __func__);
if (!trusty_supports_logging(pdev->dev.parent)) {
return -ENXIO;
}
s = kzalloc(sizeof(*s), GFP_KERNEL);
if (!s) {
result = -ENOMEM;
goto error_alloc_state;
}
spin_lock_init(&s->lock);
s->dev = &pdev->dev;
s->trusty_dev = s->dev->parent;
s->get = 0;
s->log_pages = alloc_pages(GFP_KERNEL | __GFP_ZERO,
get_order(TRUSTY_LOG_SIZE));
if (!s->log_pages) {
result = -ENOMEM;
goto error_alloc_log;
}
s->log = page_address(s->log_pages);
pa = page_to_phys(s->log_pages);
result = trusty_std_call32(s->trusty_dev,
SMC_SC_SHARED_LOG_ADD,
(u32)(pa), (u32)(pa >> 32),
TRUSTY_LOG_SIZE);
if (result < 0) {
pr_err("trusty std call (SMC_SC_SHARED_LOG_ADD) failed: %d %pa\n",
result, &pa);
goto error_std_call;
}
/* Disable the Trusty OS UART console to prevent synchronous IO waiting */
result = trusty_std_call32(s->trusty_dev,
SMC_SC_SHARED_CONSOLE_CTL, TRUSTY_CONSOLE_DISABLE, 0, 0);
if (result != 0) {
pr_err("trusty std call SMC_SC_SHARED_CONSOLE_CTL shutdown console failed\n");
}
s->call_notifier.notifier_call = trusty_log_call_notify;
result = trusty_call_notifier_register(s->trusty_dev,
&s->call_notifier);
if (result < 0) {
dev_err(&pdev->dev,
"failed to register trusty call notifier\n");
goto error_call_notifier;
}
s->panic_notifier.notifier_call = trusty_log_panic_notify;
result = atomic_notifier_chain_register(&panic_notifier_list,
&s->panic_notifier);
if (result < 0) {
dev_err(&pdev->dev,
"failed to register panic notifier\n");
goto error_panic_notifier;
}
platform_set_drvdata(pdev, s);
return 0;
error_panic_notifier:
trusty_call_notifier_unregister(s->trusty_dev, &s->call_notifier);
error_call_notifier:
trusty_std_call32(s->trusty_dev, SMC_SC_SHARED_LOG_RM,
(u32)pa, (u32)(pa >> 32), 0);
error_std_call:
__free_pages(s->log_pages, get_order(TRUSTY_LOG_SIZE));
error_alloc_log:
kfree(s);
error_alloc_state:
return result;
}
static int trusty_log_remove(struct platform_device *pdev)
{
int result;
struct trusty_log_state *s = platform_get_drvdata(pdev);
phys_addr_t pa = page_to_phys(s->log_pages);
dev_dbg(&pdev->dev, "%s\n", __func__);
atomic_notifier_chain_unregister(&panic_notifier_list,
&s->panic_notifier);
trusty_call_notifier_unregister(s->trusty_dev, &s->call_notifier);
result = trusty_std_call32(s->trusty_dev, SMC_SC_SHARED_LOG_RM,
(u32)pa, (u32)(pa >> 32), 0);
if (result) {
pr_err("trusty std call (SMC_SC_SHARED_LOG_RM) failed: %d\n",
result);
}
__free_pages(s->log_pages, get_order(TRUSTY_LOG_SIZE));
kfree(s);
return 0;
}
static const struct of_device_id trusty_test_of_match[] = {
{ .compatible = "android,trusty-log-v1", },
{},
};
static struct platform_driver trusty_log_driver = {
.probe = trusty_log_probe,
.remove = trusty_log_remove,
.driver = {
.name = "trusty-log",
.owner = THIS_MODULE,
.of_match_table = trusty_test_of_match,
},
};
module_platform_driver(trusty_log_driver);