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

Modify penglai-driver to support oe-23 #97

Merged
merged 2 commits into from
Oct 10, 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
2 changes: 2 additions & 0 deletions penglai-enclave-driver/penglai-enclave-elfloader.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ int penglai_enclave_loadelf(enclave_mem_t*enclave_mem, void* __user elf_ptr, uns
printk("KERNEL MODULE: elf_hdr copy_from_user failed\n");
return -1;
}

*elf_entry_point = elf_hdr.e_entry;
elf_sect_ptr = (vaddr_t) elf_ptr + elf_hdr.e_shoff;

Expand Down Expand Up @@ -127,6 +128,7 @@ int penglai_enclave_elfmemsize(void* __user elf_ptr, int* size)
printk("[Penglai Driver@%s] elf_hdr copy_from_user failed\n", __func__);
return -1;
}

elf_sect_ptr = (vaddr_t) elf_ptr + elf_hdr.e_shoff;

for (i = 0; i < elf_hdr.e_shnum;i++)
Expand Down
35 changes: 20 additions & 15 deletions penglai-enclave-driver/penglai-enclave-ioctl.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "penglai-enclave-ioctl.h"
#include "syscall.h"

#define PAGE_UP(addr) (((addr)+((PAGE_SIZE)-1))&(~((PAGE_SIZE)-1)))

//now we just acqure a big lock before allocating enclave mem, and release the lock
//after initializing mem and returning it back to sm
DEFINE_SPINLOCK(enclave_big_lock);
Expand Down Expand Up @@ -95,18 +97,18 @@ int check_eapp_memory_size(long elf_size, long stack_size, long untrusted_mem_si
int penglai_enclave_create(struct file * filep, unsigned long args)
{
struct penglai_enclave_user_param* enclave_param = (struct penglai_enclave_user_param*)args;
void *elf_ptr = (void*)enclave_param->elf_ptr;
void *elf_ptr = (void*)enclave_param->elf_ptr;
int elf_size = 0;
if(penglai_enclave_elfmemsize(elf_ptr, &elf_size) < 0)
{
printk("KERNEL MODULE: calculate elf_size failed\n");
return -1;
}
long stack_size = enclave_param->stack_size;
long untrusted_mem_size = enclave_param->untrusted_mem_size;
unsigned long untrusted_mem_ptr = enclave_param->untrusted_mem_ptr;
long stack_size = enclave_param->stack_size; //DEFAULT_STACK_SIZE=1MB
long untrusted_mem_size = enclave_param->untrusted_mem_size; //DEFAULT_UNTRUSTED_SIZE=8KB
unsigned long untrusted_mem_ptr = enclave_param->untrusted_mem_ptr; //0
unsigned long kbuffer_ptr = ENCLAVE_DEFAULT_KBUFFER;
struct penglai_enclave_sbi_param enclave_sbi_param;
struct penglai_enclave_sbi_param *enclave_sbi_param = kmalloc(sizeof(struct penglai_enclave_sbi_param), GFP_KERNEL);
enclave_t* enclave;
unsigned int total_pages = total_enclave_page(elf_size, stack_size);
unsigned long free_mem, elf_entry;
Expand All @@ -120,9 +122,8 @@ int penglai_enclave_create(struct file * filep, unsigned long args)
return -1;
}

acquire_big_lock(__func__);

enclave = create_enclave(total_pages);
enclave = create_enclave(total_pages); //May sleep
if(!enclave)
{
printk("KERNEL MODULE: cannot create enclave\n");
Expand All @@ -131,7 +132,7 @@ int penglai_enclave_create(struct file * filep, unsigned long args)

elf_entry = 0;
if(penglai_enclave_eapp_preprare(enclave->enclave_mem, elf_ptr, elf_size,
&elf_entry, STACK_POINT, stack_size))
&elf_entry, STACK_POINT, stack_size)) //May sleep
{
printk("KERNEL MODULE: penglai_enclave_eapp_preprare is failed\n");;
goto destroy_enclave;
Expand All @@ -145,20 +146,20 @@ int penglai_enclave_create(struct file * filep, unsigned long args)
untrusted_mem_size = 0x1 << (ilog2(untrusted_mem_size - 1) + 1);
if((untrusted_mem_ptr == 0) && (untrusted_mem_size > 0))
{
alloc_untrusted_mem(untrusted_mem_size, &untrusted_mem_ptr, enclave);
alloc_untrusted_mem(untrusted_mem_size, &untrusted_mem_ptr, enclave); //May sleep
}
enclave->untrusted_mem->addr = (vaddr_t)untrusted_mem_ptr;
enclave->untrusted_mem->size = untrusted_mem_size;
printk("[Penglai Driver@%s] untrusted_mem->addr:0x%lx untrusted_mem->size:0x%lx\n",
__func__, (vaddr_t)untrusted_mem_ptr, untrusted_mem_size);

alloc_kbuffer(ENCLAVE_DEFAULT_KBUFFER_SIZE, &kbuffer_ptr, enclave);
alloc_kbuffer(ENCLAVE_DEFAULT_KBUFFER_SIZE, &kbuffer_ptr, enclave); //May sleep
enclave->kbuffer = (vaddr_t)kbuffer_ptr;
enclave->kbuffer_size = ENCLAVE_DEFAULT_KBUFFER_SIZE;

free_mem = get_free_mem(&(enclave->enclave_mem->free_mem));

create_sbi_param(enclave, &enclave_sbi_param,
acquire_big_lock(__func__);
create_sbi_param(enclave, enclave_sbi_param,
(unsigned long)(enclave->enclave_mem->paddr),
enclave->enclave_mem->size, elf_entry, DEFAULT_UNTRUSTED_PTR,
untrusted_mem_size, __pa(free_mem));
Expand All @@ -167,7 +168,7 @@ int penglai_enclave_create(struct file * filep, unsigned long args)
__func__, (unsigned long)(enclave->enclave_mem->paddr),
enclave->enclave_mem->size);

ret = SBI_CALL_1(SBI_SM_CREATE_ENCLAVE, __pa(&enclave_sbi_param));
ret = SBI_CALL_1(SBI_SM_CREATE_ENCLAVE, __pa(enclave_sbi_param));

//if(ret < 0)
if(ret.error)
Expand All @@ -181,6 +182,7 @@ int penglai_enclave_create(struct file * filep, unsigned long args)
enclave->is_running = 0; //clear the flag

release_big_lock(__func__);
kfree(enclave_sbi_param);

return ret.value;

Expand All @@ -190,6 +192,7 @@ int penglai_enclave_create(struct file * filep, unsigned long args)
destroy_enclave(enclave);
}
release_big_lock(__func__);
if(enclave_sbi_param) kfree(enclave_sbi_param);

return -EFAULT;
}
Expand Down Expand Up @@ -290,7 +293,6 @@ int penglai_enclave_run(struct file *filep, unsigned long args)
int retval = 0;
int resume_id = 0;

printk("[Penglai Driver@%s] begin\n", __func__);
acquire_big_lock(__func__);

enclave = get_enclave_by_id(eid);
Expand Down Expand Up @@ -410,6 +412,7 @@ int penglai_enclave_run(struct file *filep, unsigned long args)
int penglai_enclave_attest(struct file * filep, unsigned long args)
{
struct penglai_enclave_ioctl_attest_enclave * enclave_param = (struct penglai_enclave_ioctl_attest_enclave*) args;
struct report_t *report = kmalloc(sizeof(struct report_t), GFP_KERNEL);
unsigned long eid = enclave_param->eid;
enclave_t * enclave;
struct sbiret ret = {0};
Expand All @@ -424,11 +427,13 @@ int penglai_enclave_attest(struct file * filep, unsigned long args)
goto out;
}

ret = SBI_CALL_3(SBI_SM_ATTEST_ENCLAVE, enclave->eid, __pa(&(enclave_param->report)), enclave_param->nonce);
ret = SBI_CALL_3(SBI_SM_ATTEST_ENCLAVE, enclave->eid, __pa(report), enclave_param->nonce);
enclave_param->report = *report;
retval = ret.value;

out:
release_big_lock(__func__);
kfree(report);
return retval;
}

Expand Down
29 changes: 18 additions & 11 deletions penglai-enclave-driver/penglai-enclave.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "penglai-enclave.h"
DEFINE_IDR(idr_enclave);
DEFINE_SPINLOCK(idr_enclave_lock);

DEFINE_SPINLOCK(kmalloc_enclave_lock);
/*
* ACK (DD): the idr_alloc function is learned from keystone :)
* */
Expand All @@ -10,7 +10,7 @@ unsigned int enclave_idr_alloc(enclave_t* enclave)
unsigned int ueid;

spin_lock_bh(&idr_enclave_lock);
ueid = idr_alloc(&idr_enclave, enclave, ENCLAVE_IDR_MIN, ENCLAVE_IDR_MAX, GFP_KERNEL);
ueid = idr_alloc(&idr_enclave, enclave, ENCLAVE_IDR_MIN, ENCLAVE_IDR_MAX, GFP_ATOMIC);
spin_unlock_bh(&idr_enclave_lock);

if (ueid < ENCLAVE_IDR_MIN || ueid >= ENCLAVE_IDR_MAX) {
Expand Down Expand Up @@ -50,8 +50,8 @@ enclave_t* create_enclave(int total_pages)
enclave_t* enclave = kmalloc(sizeof(enclave_t), GFP_KERNEL);
enclave_mem_t* enclave_mem = kmalloc(sizeof(enclave_mem_t), GFP_KERNEL);
untrusted_mem_t* untrusted_mem = kmalloc(sizeof(untrusted_mem_t), GFP_KERNEL);
require_sec_memory_t require_sec_memory;

require_sec_memory_t* require_sec_memory = kmalloc(sizeof(require_sec_memory_t), GFP_KERNEL);
spin_lock_bh(&kmalloc_enclave_lock);
int size;
struct sbiret ret;
unsigned long order = ilog2(total_pages-1) + 1;
Expand All @@ -65,9 +65,10 @@ enclave_t* create_enclave(int total_pages)
printk("[Penglai Driver@%s] total_pages:%d order:%ld\n",
__func__, total_pages, order);
//Note: SBI_SM_ALLOC_ENCLAVE_MEM's arg is the num of bytes instead of pages
require_sec_memory.size = total_pages << RISCV_PGSHIFT;
ret = SBI_CALL_1(SBI_SM_ALLOC_ENCLAVE_MEM, __pa(&require_sec_memory));
pa = require_sec_memory.paddr;
require_sec_memory->size = total_pages << RISCV_PGSHIFT;

ret = SBI_CALL_1(SBI_SM_ALLOC_ENCLAVE_MEM, __pa(require_sec_memory));
pa = require_sec_memory->paddr;

if (ret.error){
printk("[Penglai SDK Driver Error@%s] alloc_enclave_mem error\n", __func__);
Expand All @@ -90,9 +91,9 @@ enclave_t* create_enclave(int total_pages)
}

//FIXME: use physical address
//ret = SBI_CALL_1(SBI_SM_ALLOC_ENCLAVE_MEM, &require_sec_memory);
ret = SBI_CALL_1(SBI_SM_ALLOC_ENCLAVE_MEM, __pa(&require_sec_memory));
pa = require_sec_memory.paddr;
//ret = SBI_CALL_1(SBI_SM_ALLOC_ENCLAVE_MEM, require_sec_memory);
ret = SBI_CALL_1(SBI_SM_ALLOC_ENCLAVE_MEM, __pa(require_sec_memory));
pa = require_sec_memory->paddr;
}

//if(ret < 0 && ret != ENCLAVE_NO_MEMORY)
Expand All @@ -103,12 +104,17 @@ enclave_t* create_enclave(int total_pages)
}

addr = (vaddr_t)__va(pa);
size = require_sec_memory.resp_size;
size = require_sec_memory->resp_size;
INIT_LIST_HEAD(&enclave_mem->free_mem);
spin_unlock_bh(&kmalloc_enclave_lock);
enclave_mem_int(enclave_mem, addr, size, __pa(addr));
spin_lock_bh(&kmalloc_enclave_lock);

enclave->enclave_mem = enclave_mem;
enclave->untrusted_mem = untrusted_mem;

kfree(untrusted_mem);
spin_unlock_bh(&kmalloc_enclave_lock);
//TODO: create untrusted mem

return enclave;
Expand All @@ -118,6 +124,7 @@ enclave_t* create_enclave(int total_pages)
if(enclave) kfree(enclave);
if(enclave_mem) kfree(enclave_mem);
if(untrusted_mem) kfree(untrusted_mem);
if(require_sec_memory) kfree(untrusted_mem);

return NULL;
}
Expand Down