-
Notifications
You must be signed in to change notification settings - Fork 0
/
rt_util.c
75 lines (65 loc) · 1.84 KB
/
rt_util.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
//******************************************************************************
// Copyright (c) 2018, The Regents of the University of California (Regents).
// All Rights Reserved. See LICENSE for license details.
//------------------------------------------------------------------------------
#include "mm.h"
#include "rt_util.h"
#include "printf.h"
#include "uaccess.h"
#include "vm.h"
// Statically allocated copy-buffer
unsigned char rt_copy_buffer_1[RISCV_PAGE_SIZE];
unsigned char rt_copy_buffer_2[RISCV_PAGE_SIZE];
size_t rt_util_getrandom(void* vaddr, size_t buflen){
size_t remaining = buflen;
uintptr_t rnd;
uintptr_t* next = (uintptr_t*)vaddr;
// Get data
while(remaining > sizeof(uintptr_t)){
rnd = sbi_random();
ALLOW_USER_ACCESS( *next = rnd );
remaining -= sizeof(uintptr_t);
next++;
}
// Cleanup
if( remaining > 0 ){
rnd = sbi_random();
copy_to_user(next, &rnd, remaining);
}
size_t ret = buflen;
return ret;
}
void rt_util_misc_fatal(){
//Better hope we can debug it!
sbi_exit_enclave(-1);
}
void not_implemented_fatal(struct encl_ctx* ctx){
#ifdef FATAL_DEBUG
unsigned long addr, cause, pc;
pc = ctx->regs.sepc;
addr = ctx->sbadaddr;
cause = ctx->scause;
printf("[runtime] non-handlable interrupt/exception at 0x%lx on 0x%lx (scause: 0x%lx)\r\n", pc, addr, cause);
#endif
// Bail to m-mode
__asm__ volatile("csrr a0, scause\r\nli a7, 1111\r\n ecall");
return;
}
void rt_page_fault(struct encl_ctx* ctx)
{
#ifdef FATAL_DEBUG
unsigned long addr, cause, pc;
pc = ctx->regs.sepc;
addr = ctx->sbadaddr;
cause = ctx->scause;
printf("[runtime] page fault at 0x%lx on 0x%lx (scause: 0x%lx)\r\n", pc, addr, cause);
#endif
sbi_exit_enclave(-1);
/* never reach here */
assert(false);
return;
}
void tlb_flush(void)
{
__asm__ volatile("fence.i\t\nsfence.vma\t\n");
}