forked from keystone-enclave/sm
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpmp.c
513 lines (428 loc) · 12.9 KB
/
pmp.c
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
//******************************************************************************
// Copyright (c) 2018, The Regents of the University of California (Regents).
// All Rights Reserved. See LICENSE for license details.
//------------------------------------------------------------------------------
#include "assert.h"
#include "pmp.h"
#include "cpu.h"
#include "safe_math_util.h"
#include "sm-sbi-opensbi.h"
#include "page.h"
#include "ipi.h"
#include <sbi/sbi_hart.h>
#include <sbi/riscv_asm.h>
#include <sbi/riscv_locks.h>
#include <sbi/riscv_atomic.h>
u32 opensbi_region_cnt = 0;
static int pmp_start;
/* PMP global spin locks */
static spinlock_t pmp_lock = SPIN_LOCK_INITIALIZER;
/* PMP region getter/setters */
static struct pmp_region regions[PMP_MAX_N_REGION];
static uint32_t reg_bitmap = 0;
static uint32_t region_def_bitmap = 0;
static inline int region_register_idx(region_id i)
{
return regions[i].reg_idx;
}
static inline int region_allows_overlap(region_id i)
{
return regions[i].allow_overlap;
}
static inline uintptr_t region_get_addr(region_id i)
{
return regions[i].addr;
}
static inline uint64_t region_get_size(region_id i)
{
return regions[i].size;
}
static inline int region_is_napot(region_id i)
{
return regions[i].addrmode == PMP_A_NAPOT;
}
static inline int region_is_tor(region_id i)
{
return regions[i].addrmode == PMP_A_TOR;
}
static inline int region_needs_two_entries(region_id i)
{
return region_is_tor(i) && regions[i].reg_idx > 0;
}
static inline int region_is_napot_all(region_id i)
{
return regions[i].addr == 0 && regions[i].size == -1UL;
}
static inline uintptr_t region_pmpaddr_val(region_id i)
{
if(region_is_napot_all(i))
return (-1UL);
else if(region_is_napot(i))
return (regions[i].addr | (regions[i].size/2-1)) >> 2;
else if(region_is_tor(i))
return (regions[i].addr + regions[i].size) >> 2;
else
return 0;
}
static inline uintptr_t region_pmpcfg_val(region_id i, pmpreg_id reg_idx, uint8_t perm_bits)
{
return (uintptr_t) (regions[i].addrmode | perm_bits) << (8*(reg_idx%PMP_PER_GROUP));
}
static void region_clear_all(region_id i)
{
regions[i].addr = 0;
regions[i].size = 0;
regions[i].addrmode = 0;
regions[i].allow_overlap = 0;
regions[i].reg_idx = 0;
}
static void region_init(region_id i,
uintptr_t addr,
uint64_t size,
uint8_t addrmode,
int allow_overlap,
pmpreg_id reg_idx)
{
regions[i].addr = addr;
regions[i].size = size;
regions[i].addrmode = addrmode;
regions[i].allow_overlap = allow_overlap;
regions[i].reg_idx = (addrmode == PMP_A_TOR && reg_idx > 0 ? reg_idx + 1 : reg_idx);
}
static int is_pmp_region_valid(region_id region_idx)
{
return TEST_BIT(region_def_bitmap, region_idx);
}
static int search_rightmost_unset(uint32_t bitmap, int max, uint32_t mask)
{
int i = 0;
sm_assert(max < 32);
sm_assert(!((mask + 1) & mask));
while(mask < (1UL << max)) {
if((~bitmap & mask) == mask)
return i;
mask = mask << 1;
i++;
}
return -1;
}
static region_id get_free_region_idx()
{
return search_rightmost_unset(region_def_bitmap, PMP_MAX_N_REGION, 0x1);
}
static pmpreg_id get_free_reg_idx()
{
return search_rightmost_unset(reg_bitmap, PMP_N_REG, 0x1);
}
static pmpreg_id get_conseq_free_reg_idx()
{
return search_rightmost_unset(reg_bitmap, PMP_N_REG, 0x3);
}
/* We do an integery overflow safety check here for the inputs (addr +
* size). We do NOT do a safety check on epm_base + epm_size, since
* only valid region should have been created previously.
*
* On a failed addr + size overflow, we return failure, since this
* cannot be a valid addr and size anyway.
*/
static int detect_region_overlap(uintptr_t addr, uintptr_t size)
{
void* epm_base;
size_t epm_size;
int region_overlap = 0, i;
// Safety check the addr+size
uintptr_t input_end;
if( CHECKED_ADD(addr, size, &input_end)){
return 1;
}
for(i=0; i<PMP_MAX_N_REGION; i++)
{
if(!is_pmp_region_valid(i))
continue;
if(region_allows_overlap(i)) {
continue;
}
epm_base = (void*) region_get_addr(i);
epm_size = region_get_size(i);
// Only looking at valid regions, no need to check epm_base+size
region_overlap |= ((uintptr_t) epm_base < input_end) &&
((uintptr_t) epm_base + epm_size > addr);
}
return region_overlap;
}
int pmp_detect_region_overlap_atomic(uintptr_t addr, uintptr_t size)
{
int region_overlap = 0;
spin_lock(&pmp_lock);
region_overlap = detect_region_overlap(addr, size);
spin_unlock(&pmp_lock);
return region_overlap;
}
/*********************************
*
* External Functions
*
**********************************/
int pmp_unset_global(int region_idx)
{
if(!is_pmp_region_valid(region_idx))
PMP_ERROR(SBI_ERR_SM_PMP_REGION_INVALID, "Invalid PMP region index");
send_and_sync_pmp_ipi(region_idx, SBI_PMP_IPI_TYPE_UNSET, PMP_NO_PERM);
return SBI_ERR_SM_PMP_SUCCESS;
}
/* populate pmp set command to every other hart */
int pmp_set_global(int region_idx, uint8_t perm)
{
if(!is_pmp_region_valid(region_idx))
PMP_ERROR(SBI_ERR_SM_PMP_REGION_INVALID, "Invalid PMP region index");
send_and_sync_pmp_ipi(region_idx, SBI_PMP_IPI_TYPE_SET, perm);
return SBI_ERR_SM_PMP_SUCCESS;
}
void pmp_reg_bitmap_init(void)
{
pmp_start = opensbi_region_cnt;
reg_bitmap = (1 << pmp_start) - 1;
}
void pmp_init()
{
uintptr_t pmpaddr = 0;
int i;
for (i=pmp_start; i < PMP_N_REG; i++)
{
uintptr_t pmpcfg = 0;
switch(i) {
#define X(n,g) case n: { PMP_SET(n, g, pmpaddr, pmpcfg); break; }
LIST_OF_PMP_REGS
#undef X
}
}
}
int pmp_set_keystone(int region_idx, uint8_t perm)
{
if(!is_pmp_region_valid(region_idx))
PMP_ERROR(SBI_ERR_SM_PMP_REGION_INVALID, "Invalid PMP region index");
uint8_t perm_bits = perm & PMP_ALL_PERM;
pmpreg_id reg_idx = region_register_idx(region_idx);
uintptr_t pmpcfg = region_pmpcfg_val(region_idx, reg_idx, perm_bits);
uintptr_t pmpaddr;
pmpaddr = region_pmpaddr_val(region_idx);
int n=reg_idx;
switch(n) {
#define X(n,g) case n: { PMP_SET(n, g, pmpaddr, pmpcfg); break; }
LIST_OF_PMP_REGS
#undef X
default:
sm_assert(false);
}
/* TOR decoding with 2 registers */
if(region_needs_two_entries(region_idx))
{
n--;
pmpcfg = 0;
pmpaddr = region_get_addr(region_idx) >> 2;
switch(n) {
#define X(n,g) case n: { PMP_SET(n, g, pmpaddr, pmpcfg); break; }
LIST_OF_PMP_REGS
#undef X
default:
sm_assert(false);
}
}
return SBI_ERR_SM_PMP_SUCCESS;
}
int pmp_unset(int region_idx)
{
if(!is_pmp_region_valid(region_idx))
PMP_ERROR(SBI_ERR_SM_PMP_REGION_INVALID,"Invalid PMP region index");
pmpreg_id reg_idx = region_register_idx(region_idx);
int n=reg_idx;
switch(n) {
#define X(n,g) case n: { PMP_UNSET(n, g); break;}
LIST_OF_PMP_REGS
#undef X
default:
sm_assert(false);
}
if(region_needs_two_entries(region_idx))
{
n--;
switch(n) {
#define X(n,g) case n: { PMP_UNSET(n,g); break; }
LIST_OF_PMP_REGS
#undef X
default:
sm_assert(false);
}
}
return SBI_ERR_SM_PMP_SUCCESS;
}
int pmp_set_keystone_dump(int region_idx, uint8_t perm, bool dump_setting)
{
if(!is_pmp_region_valid(region_idx))
PMP_ERROR(SBI_ERR_SM_PMP_REGION_INVALID, "Invalid PMP region index");
pmpreg_id reg_idx = region_register_idx(region_idx);
if (dump_setting) {
sbi_printf("[SM] Enforcing PMP: reg[%d], mode[%s], range[0x%lx-0x%llx], perm[0x%x]\r\n",
reg_idx, (region_is_tor(region_idx) ? "TOR":"NAPOT"),
region_get_addr(region_idx),
(unsigned long long)(region_get_addr(region_idx) + region_get_size(region_idx)), perm);
}
return pmp_set_keystone(region_idx, perm);
}
int pmp_region_init_atomic(uintptr_t start, uint64_t size, enum pmp_priority priority, region_id* rid, int allow_overlap)
{
int ret;
spin_lock(&pmp_lock);
ret = pmp_region_init(start, size, priority, rid, allow_overlap);
spin_unlock(&pmp_lock);
return ret;
}
static int tor_region_init(uintptr_t start, uint64_t size, enum pmp_priority priority, region_id* rid, int allow_overlap)
{
pmpreg_id reg_idx = -1;
region_id region_idx = -1;
sm_assert(size);
sm_assert(!(size & (RISCV_PGSIZE-1)));
sm_assert(!(start & (RISCV_PGSIZE-1)));
sm_assert(rid);
sm_assert(priority != PMP_PRI_BOTTOM);
region_idx = get_free_region_idx();
if(region_idx < 0 || region_idx > PMP_MAX_N_REGION)
PMP_ERROR(SBI_ERR_SM_PMP_REGION_MAX_REACHED, "Reached the maximum number of PMP regions");
*rid = region_idx;
switch(priority)
{
case(PMP_PRI_ANY): {
reg_idx = get_conseq_free_reg_idx();
if(reg_idx < 0)
PMP_ERROR(SBI_ERR_SM_PMP_REGION_MAX_REACHED, "No available PMP register");
if(TEST_BIT(reg_bitmap, reg_idx) || TEST_BIT(reg_bitmap, reg_idx + 1) || reg_idx + 1 >= PMP_N_REG)
PMP_ERROR(SBI_ERR_SM_PMP_REGION_MAX_REACHED, "PMP register unavailable");
break;
}
case(PMP_PRI_TOP): {
reg_idx = pmp_start;
if(TEST_BIT(reg_bitmap, reg_idx))
PMP_ERROR(SBI_ERR_SM_PMP_REGION_MAX_REACHED, "PMP register unavailable");
break;
}
default: {
sm_assert(0);
}
}
// initialize the region
region_init(region_idx, start, size, PMP_A_TOR, allow_overlap, reg_idx);
SET_BIT(region_def_bitmap, region_idx);
SET_BIT(reg_bitmap, reg_idx);
if(reg_idx > 0)
SET_BIT(reg_bitmap, reg_idx + 1);
return SBI_ERR_SM_PMP_SUCCESS;
}
static int napot_region_init(uintptr_t start, uint64_t size, enum pmp_priority priority, region_id* rid, int allow_overlap)
{
pmpreg_id reg_idx = -1;
region_id region_idx = -1;
sm_assert(size);
sm_assert(rid);
if(!(size == -1UL && start == 0))
{
sm_assert(!(size & (size-1)));
sm_assert(!(start & (size - 1)));
sm_assert(!(size & (RISCV_PGSIZE-1)));
sm_assert(!(start & (RISCV_PGSIZE-1)));
}
//find avaiable pmp region idx
region_idx = get_free_region_idx();
if(region_idx < 0 || region_idx > PMP_MAX_N_REGION)
PMP_ERROR(SBI_ERR_SM_PMP_REGION_MAX_REACHED, "Reached the maximum number of PMP regions");
*rid = region_idx;
switch(priority)
{
case(PMP_PRI_ANY): {
reg_idx = get_free_reg_idx();
if(reg_idx < 0)
PMP_ERROR(SBI_ERR_SM_PMP_REGION_MAX_REACHED, "No available PMP register");
if(TEST_BIT(reg_bitmap, reg_idx) || reg_idx >= PMP_N_REG)
PMP_ERROR(SBI_ERR_SM_PMP_REGION_MAX_REACHED, "PMP register unavailable");
break;
}
case(PMP_PRI_TOP): {
reg_idx = pmp_start;
if(TEST_BIT(reg_bitmap, reg_idx))
PMP_ERROR(SBI_ERR_SM_PMP_REGION_MAX_REACHED, "PMP register unavailable");
break;
}
case(PMP_PRI_BOTTOM): {
/* the bottom register can be used by multiple regions,
* so we don't check its availability */
reg_idx = PMP_N_REG - 1;
break;
}
default: {
sm_assert(0);
}
}
// initialize the region
region_init(region_idx, start, size, PMP_A_NAPOT, allow_overlap, reg_idx);
SET_BIT(region_def_bitmap, region_idx);
SET_BIT(reg_bitmap, reg_idx);
return SBI_ERR_SM_PMP_SUCCESS;
}
int pmp_region_free_atomic(int region_idx)
{
spin_lock(&pmp_lock);
if(!is_pmp_region_valid(region_idx))
{
spin_unlock(&pmp_lock);
PMP_ERROR(SBI_ERR_SM_PMP_REGION_INVALID, "Invalid PMP region index");
}
pmpreg_id reg_idx = region_register_idx(region_idx);
UNSET_BIT(region_def_bitmap, region_idx);
UNSET_BIT(reg_bitmap, reg_idx);
if(region_needs_two_entries(region_idx))
UNSET_BIT(reg_bitmap, reg_idx - 1);
region_clear_all(region_idx);
spin_unlock(&pmp_lock);
return SBI_ERR_SM_PMP_SUCCESS;
}
int pmp_region_init(uintptr_t start, uint64_t size, enum pmp_priority priority, int* rid, int allow_overlap)
{
if(!size)
PMP_ERROR(SBI_ERR_SM_PMP_REGION_SIZE_INVALID, "Invalid PMP size");
/* overlap detection */
if (!allow_overlap) {
if (detect_region_overlap(start, size)) {
return SBI_ERR_SM_PMP_REGION_OVERLAP;
}
}
/* PMP granularity check */
if(size != -1UL && (size & (RISCV_PGSIZE - 1)))
PMP_ERROR(SBI_ERR_SM_PMP_REGION_NOT_PAGE_GRANULARITY, "PMP granularity is RISCV_PGSIZE");
if(start & (RISCV_PGSIZE - 1))
PMP_ERROR(SBI_ERR_SM_PMP_REGION_NOT_PAGE_GRANULARITY, "PMP granularity is RISCV_PGSIZE");
/* if the address covers the entire RAM or it's NAPOT */
if ((size == -1UL && start == 0) ||
(!(size & (size - 1)) && !(start & (size - 1)))) {
return napot_region_init(start, size, priority, rid, allow_overlap);
}
else
{
if(priority != PMP_PRI_ANY &&
(priority != PMP_PRI_TOP || start != 0)) {
PMP_ERROR(SBI_ERR_SM_PMP_REGION_IMPOSSIBLE_TOR, "The top-priority TOR PMP entry must start from address 0");
}
return tor_region_init(start, size, priority, rid, allow_overlap);
}
}
uintptr_t pmp_region_get_addr(region_id i)
{
if(is_pmp_region_valid(i))
return region_get_addr(i);
return 0;
}
uint64_t pmp_region_get_size(region_id i)
{
if(is_pmp_region_valid(i))
return region_get_size(i);
return 0;
}