Skip to content

Commit

Permalink
Updated enclave stop and exit to deduplicate code and have a context …
Browse files Browse the repository at this point in the history
…switch to host function. This made minor functional changes that should have no effect. Moved the STOP codes to enclave.h since they are enclave status specific
  • Loading branch information
dkohlbre committed Mar 9, 2019
1 parent 7cc44fd commit f9f201f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 39 deletions.
70 changes: 34 additions & 36 deletions enclave.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,23 +191,7 @@ enclave_ret_t exit_enclave(uintptr_t* encl_regs, unsigned long retval)
if(!exitable)
return ENCLAVE_NOT_RUNNING;

// get the running enclave on this SM
struct enclave_t encl = enclaves[eid];

// set PMP
pmp_set(encl.rid, PMP_NO_PERM);
osm_pmp_set(PMP_ALL_PERM);

/* restore host context */
swap_prev_state(&enclaves[eid].threads[0], encl_regs);
swap_prev_stvec(&enclaves[eid].threads[0], 0);
swap_prev_mepc(&enclaves[eid].threads[0], 0);

// switch to host page table
write_csr(satp, encl.host_satp);

// enable timer interrupt
set_csr(mie, MIP_MTIP);
_context_switch_to_host(encl_regs, eid);

// update enclave state
spinlock_lock(&encl_lock);
Expand All @@ -228,33 +212,23 @@ enclave_ret_t stop_enclave(uintptr_t* encl_regs, uint64_t request)

spinlock_lock(&encl_lock);
stoppable = enclaves[eid].state == RUNNING;

spinlock_unlock(&encl_lock);

if(!stoppable)
return ENCLAVE_NOT_RUNNING;

/* TODO: currently enclave cannot have multiple threads */
swap_prev_state(&enclaves[eid].threads[0], encl_regs);
swap_prev_mepc(&enclaves[eid].threads[0], read_csr(mepc));
swap_prev_stvec(&enclaves[eid].threads[0], read_csr(stvec));

struct enclave_t encl = enclaves[eid];

pmp_set(encl.rid, PMP_NO_PERM);
osm_pmp_set(PMP_ALL_PERM);

write_csr(satp, encl.host_satp);
set_csr(mie, MIP_MTIP);
_context_switch_to_host(encl_regs, eid);

switch(request) {
case(STOP_TIMER_INTERRUPT):
return ENCLAVE_INTERRUPTED;
case(STOP_EDGE_CALL_HOST):
return ENCLAVE_EDGE_CALL_HOST;
default:
return ENCLAVE_UNKNOWN_ERROR;
case(STOP_TIMER_INTERRUPT):
return ENCLAVE_INTERRUPTED;
case(STOP_EDGE_CALL_HOST):
return ENCLAVE_EDGE_CALL_HOST;
default:
return ENCLAVE_UNKNOWN_ERROR;
}


}


Expand Down Expand Up @@ -380,6 +354,30 @@ inline enclave_ret_t _context_switch_to_enclave(uintptr_t* regs,
return ENCLAVE_SUCCESS;
}

inline void _context_switch_to_host(uintptr_t* encl_regs,
eid_t eid){
// get the running enclave on this SM
struct enclave_t encl = enclaves[eid];

// set PMP
pmp_set(encl.rid, PMP_NO_PERM);
osm_pmp_set(PMP_ALL_PERM);

/* restore host context */
swap_prev_state(&enclaves[eid].threads[0], encl_regs);
swap_prev_stvec(&enclaves[eid].threads[0], read_csr(stvec));
swap_prev_mepc(&enclaves[eid].threads[0], read_csr(mepc));

// switch to host page table
write_csr(satp, encl.host_satp);

// enable timer interrupt
set_csr(mie, MIP_MTIP);

return;
}


/*
* Init all metadata as needed for keeping track of enclaves
* Called once by the SM on startup
Expand Down
9 changes: 9 additions & 0 deletions enclave.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ typedef enum {
ALLOCATED,
} enclave_state_t;

/* Enclave stop reasons requested */
#define STOP_TIMER_INTERRUPT 0
#define STOP_EDGE_CALL_HOST 1
#define STOP_EXIT_ENCLAVE 2



/* For now, eid's are a simple unsigned int */
typedef unsigned int eid_t;

Expand Down Expand Up @@ -75,6 +82,8 @@ void enclave_init_metadata();
enclave_ret_t _context_switch_to_enclave(uintptr_t* regs,
eid_t eid,
int load_parameters);
void _context_switch_to_host(uintptr_t* encl_regs,
eid_t eid);
enclave_ret_t init_enclave_memory(uintptr_t base, uintptr_t size,
uintptr_t utbase, uintptr_t utsize);
enclave_ret_t encl_satp_to_eid(uintptr_t satp, eid_t* eid);
Expand Down
3 changes: 0 additions & 3 deletions sm.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@
#define PMP_REGION_INVALID 24
#define PMP_REGION_OVERLAP 25

#define STOP_TIMER_INTERRUPT 0
#define STOP_EDGE_CALL_HOST 1

void sm_init(void);

/* platform specific functions */
Expand Down

0 comments on commit f9f201f

Please sign in to comment.