Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐞 fix(opensbi-1.2): free metadata, secure mem and pmp #108

Merged
merged 2 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ backup/
-smp
host.txt
Image
opensbi-0.9/build-oe/
1 change: 1 addition & 0 deletions opensbi-1.2/include/sbi/riscv_locks.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <sbi/sbi_types.h>

#define LOCK_DEBUG 0
#define TICKET_SHIFT 16

typedef struct {
Expand Down
2 changes: 2 additions & 0 deletions opensbi-1.2/include/sbi/sbi_console.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ int __printf(3, 4) sbi_snprintf(char *out, u32 out_sz, const char *format, ...);

int __printf(1, 2) sbi_printf(const char *format, ...);

int __printf(1, 2) sbi_printf_nolock(const char *format, ...);

int __printf(1, 2) sbi_dprintf(const char *format, ...);

void __printf(1, 2) __attribute__((noreturn)) sbi_panic(const char *format, ...);
Expand Down
2 changes: 2 additions & 0 deletions opensbi-1.2/include/sbi/sbi_hart.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

#include <sbi/sbi_types.h>

#define MAX_HARTS 8

/** Possible privileged specification versions of a hart */
enum sbi_hart_priv_versions {
/** Unknown privileged specification */
Expand Down
5 changes: 5 additions & 0 deletions opensbi-1.2/include/sbi/sbi_ipi.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
#define SBI_IPI_EVENT_MAX __riscv_xlen

/* clang-format on */
#define SYNC_DEBUG 0
#define MAX_HARTS 8
#define IPI_NONE 0
#define IPI_TLB 1
#define IPI_PMP 2

/** IPI hardware device */
struct sbi_ipi_device {
Expand Down
5 changes: 4 additions & 1 deletion opensbi-1.2/include/sm/enclave.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ struct enclave_t

//shared mem with kernel
unsigned long kbuffer;
unsigned long kbuffer_paddr;
unsigned long kbuffer_size;

unsigned long* ocall_func_id;
Expand All @@ -74,6 +75,7 @@ struct enclave_t

//shared memory with host
unsigned long untrusted_ptr;
unsigned long untrusted_ptr_paddr;
unsigned long untrusted_size;
// enclave measurement
unsigned char hash[HASH_SIZE];
Expand All @@ -91,7 +93,7 @@ struct cpu_state_t
int eid;
};

uintptr_t create_enclave(struct enclave_sbi_param_t create_args);
uintptr_t create_enclave(struct enclave_sbi_param_t create_args, int retry);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need the parameter "retry"?

uintptr_t run_enclave(uintptr_t* regs, unsigned int eid);
uintptr_t stop_enclave(uintptr_t* regs, unsigned int eid);
uintptr_t destroy_enclave(uintptr_t* regs, unsigned int eid);
Expand All @@ -100,6 +102,7 @@ uintptr_t resume_from_stop(uintptr_t* regs, unsigned int eid);
uintptr_t attest_enclave(uintptr_t eid, uintptr_t report_ptr, uintptr_t nonce);
uintptr_t exit_enclave(uintptr_t* regs, unsigned long retval);
uintptr_t do_timer_irq(uintptr_t* regs, uintptr_t mcause, uintptr_t mepc);
uintptr_t free_enclave_metadata();

uintptr_t resume_from_ocall(uintptr_t* regs, unsigned int eid);
uintptr_t enclave_sys_write(uintptr_t *regs);
Expand Down
11 changes: 10 additions & 1 deletion opensbi-1.2/include/sm/enclave_args.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ struct mm_alloc_arg_t
unsigned long resp_size;
};

struct mm_reclaim_arg_t
{
unsigned long req_size;
uintptr_t req_addr;
unsigned long resp_size;
};

// Attestation-related report
struct sm_report_t
{
Expand Down Expand Up @@ -66,15 +73,17 @@ struct signature_t
*/
struct enclave_sbi_param_t
{
unsigned int *eid_ptr;
unsigned int * eid_ptr;
unsigned long paddr;
unsigned long size;
unsigned long entry_point;
unsigned long untrusted_ptr;
unsigned long untrusted_paddr;
unsigned long untrusted_size;
unsigned long free_mem;
//enclave shared mem with kernel
unsigned long kbuffer;
unsigned long kbuffer_paddr;
unsigned long kbuffer_size;
unsigned long *ecall_arg0;
unsigned long *ecall_arg1;
Expand Down
2 changes: 2 additions & 0 deletions opensbi-1.2/include/sm/platform/pmp/enclave_mm.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ void* mm_alloc(unsigned long req_size, unsigned long* resp_size);

int mm_free(void* paddr, unsigned long size);

int memory_reclaim(unsigned long* resp_size);

int mm_free_clear(void* paddr, unsigned long size);

void print_buddy_system();
Expand Down
7 changes: 6 additions & 1 deletion opensbi-1.2/include/sm/sm.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ extern uintptr_t _fw_start[], _fw_end[];
#define SBI_GET_KEY 88

//Error code of SBI_ALLOC_ENCLAVE_MEM
#define RETRY_SPIN_LOCK -3
#define ENCLAVE_NO_MEMORY -2
#define ENCLAVE_ERROR -1
#define ENCLAVE_SUCCESS 0
Expand All @@ -58,6 +59,8 @@ extern uintptr_t _fw_start[], _fw_end[];
#define FREE_MAX_MEMORY 2
#define FREE_SPEC_MEMORY 3

#define RETRY_TIMES 5

void sm_init();

uintptr_t sm_mm_init(uintptr_t paddr, unsigned long size);
Expand All @@ -68,7 +71,9 @@ uintptr_t sm_alloc_enclave_mem(uintptr_t mm_alloc_arg);

uintptr_t sm_free_enclave_mem(uintptr_t size_ptr,unsigned long flag);

uintptr_t sm_create_enclave(uintptr_t enclave_create_args);
uintptr_t sm_memory_reclaim(uintptr_t enclave_id, unsigned long eid);

uintptr_t sm_create_enclave(uintptr_t enclave_create_args, bool retry);

uintptr_t sm_attest_enclave(uintptr_t enclave_id, uintptr_t report, uintptr_t nonce);

Expand Down
14 changes: 13 additions & 1 deletion opensbi-1.2/lib/sbi/riscv_locks.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

#include <sbi/riscv_barrier.h>
#include <sbi/riscv_locks.h>
#include <sbi/riscv_asm.h>
#include <sm/print.h>
#define MAX_HARTS 8
volatile long waiting_for_spinlock[MAX_HARTS] = { 0 };

static inline bool spin_lock_unlocked(spinlock_t lock)
{
Expand Down Expand Up @@ -47,10 +51,14 @@ bool spin_trylock(spinlock_t *lock)

void spin_lock(spinlock_t *lock)
{
unsigned long inc = 1u << TICKET_SHIFT;
//for lock debug
unsigned long inc = 1u << TICKET_SHIFT;
unsigned long mask = 0xffffu;
u32 l0, tmp1, tmp2;
ulong hartid = csr_read(CSR_MHARTID);

// 正在尝试获取锁之前标记
waiting_for_spinlock[hartid] = 1;
__asm__ __volatile__(
/* Atomically increment the next ticket. */
" amoadd.w.aqrl %0, %4, %3\n"
Expand All @@ -69,9 +77,13 @@ void spin_lock(spinlock_t *lock)
: "=&r"(l0), "=&r"(tmp1), "=&r"(tmp2), "+A"(*lock)
: "r"(inc), "r"(mask), "I"(TICKET_SHIFT)
: "memory");

waiting_for_spinlock[hartid] = 0;
}

void spin_unlock(spinlock_t *lock)
{
ulong hartid = csr_read(CSR_MHARTID);
waiting_for_spinlock[hartid] = 0;
__smp_store_release(&lock->owner, lock->owner + 1);
}
29 changes: 21 additions & 8 deletions opensbi-1.2/lib/sbi/sbi_ecall_penglai.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@
#include <sbi/riscv_asm.h>
#include <sbi/sbi_console.h>
#include <sm/sm.h>
#include <sbi/riscv_locks.h>

// static spinlock_t sm_big_lock = SPIN_LOCK_INITIALIZER;

static int sbi_ecall_penglai_host_handler(unsigned long extid, unsigned long funcid,
const struct sbi_trap_regs *regs, unsigned long *out_val,
struct sbi_trap_info *out_trap)
{
{
uintptr_t ret = 0;
printm("[Penglai KModule] %s invoked,funcid=%ld\r\n",__func__,funcid);
printm("[Penglai KModule@%u] %s invoked,funcid=%ld\r\n",
current_hartid(), __func__, funcid);
//csr_write(CSR_MEPC, regs->mepc + 4);
((struct sbi_trap_regs *)regs)->mepc += 4;
// spin_lock(&sm_big_lock);
switch (funcid) {
// The following is the Penglai's Handler
case SBI_MM_INIT:
Expand All @@ -34,7 +38,7 @@ static int sbi_ecall_penglai_host_handler(unsigned long extid, unsigned long fun
ret = sm_alloc_enclave_mem(regs->a0);
break;
case SBI_CREATE_ENCLAVE:
ret = sm_create_enclave(regs->a0);
ret = sm_create_enclave(regs->a0, regs->a1);
break;
case SBI_RUN_ENCLAVE:
ret = sm_run_enclave((uintptr_t *)regs, regs->a0);
Expand All @@ -51,6 +55,9 @@ static int sbi_ecall_penglai_host_handler(unsigned long extid, unsigned long fun
case SBI_DESTROY_ENCLAVE:
ret = sm_destroy_enclave((uintptr_t *)regs, regs->a0);
break;
case SBI_MEMORY_RECLAIM:
ret=sm_memory_reclaim(regs->a0, regs->a1);
break;
case SBI_FREE_ENCLAVE_MEM:
ret= sm_free_enclave_mem(regs->a0, regs->a1);
break;
Expand All @@ -61,6 +68,9 @@ static int sbi_ecall_penglai_host_handler(unsigned long extid, unsigned long fun
//((struct sbi_trap_regs *)regs)->mepc = csr_read(CSR_MEPC);
//((struct sbi_trap_regs *)regs)->mstatus = csr_read(CSR_MSTATUS);
*out_val = ret;
// spin_unlock(&sm_big_lock);
printm("[Penglai KModule@%u] %s return %ld, funcid=%ld\r\n",
current_hartid(), __func__, ret, funcid);
return ret;
}

Expand All @@ -75,25 +85,28 @@ static int sbi_ecall_penglai_enclave_handler(unsigned long extid, unsigned long
struct sbi_trap_info *out_trap)
{
uintptr_t ret = 0;

// spin_lock(&sm_big_lock);
//csr_write(CSR_MEPC, regs->mepc + 4);
((struct sbi_trap_regs *)regs)->mepc += 4;

printm("[Penglai KModule@%u] %s invoked,funcid=%ld\r\n",
current_hartid(), __func__, funcid);
switch (funcid) {
// The following is the Penglai's Handler
case SBI_EXIT_ENCLAVE:
case SBI_EXIT_ENCLAVE://99
ret = sm_exit_enclave((uintptr_t *)regs, regs->a0);
break;
case SBI_ENCLAVE_OCALL:
case SBI_ENCLAVE_OCALL://98
ret = sm_enclave_ocall((uintptr_t *)regs, regs->a0, regs->a1, regs->a2);
break;
case SBI_GET_KEY:
case SBI_GET_KEY://88
ret = sm_enclave_get_key((uintptr_t *)regs, regs->a0, regs->a1, regs->a2, regs->a3);
break;
default:
sbi_printf("[Penglai@Monitor] enclave interface(funcid:%ld) not supported yet\n", funcid);
ret = SBI_ENOTSUPP;
}
printm("[Penglai KModule@%u] %s return %ld,funcid=%ld\r\n", current_hartid(), __func__,ret , funcid);
// spin_unlock(&sm_big_lock);
*out_val = ret;
return ret;
}
Expand Down
8 changes: 6 additions & 2 deletions opensbi-1.2/lib/sbi/sbi_ipi.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
#include <sbi/sbi_string.h>
#include <sbi/sbi_tlb.h>

volatile unsigned long wait_for_sync[MAX_HARTS] = { IPI_NONE };
volatile unsigned long skip_for_wait[MAX_HARTS][MAX_HARTS] = {{0}};

struct sbi_ipi_data {
unsigned long ipi_type;
};
Expand Down Expand Up @@ -64,9 +67,10 @@ static int sbi_ipi_send(struct sbi_scratch *scratch, u32 remote_hartid,
atomic_raw_set_bit(event, &ipi_data->ipi_type);
smp_wmb();

if (ipi_dev && ipi_dev->ipi_send)
ipi_dev->ipi_send(remote_hartid);
if (ipi_dev && ipi_dev->ipi_send) {

ipi_dev->ipi_send(remote_hartid);
}
sbi_pmu_ctr_incr_fw(SBI_PMU_FW_IPI_SENT);

if (ipi_ops->sync)
Expand Down
Loading
Loading