Skip to content

Commit

Permalink
Merge pull request #104 from Fly0307/kernel-panic-3
Browse files Browse the repository at this point in the history
🐞 fix(penglai-enclave-driver)
  • Loading branch information
fengerhu1 authored Nov 21, 2023
2 parents 055b9d0 + e63215e commit 9a5e2dc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 20 deletions.
43 changes: 30 additions & 13 deletions penglai-enclave-driver/penglai-enclave-driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,39 @@ int enclave_ioctl_init(void)
//static void enclave_ioctl_exit(void)
void enclave_ioctl_exit(void)
{
//unsigned long size, addr, order, count;
unsigned long addr, order, count;
unsigned long *size = kmalloc(sizeof(unsigned long), GFP_KERNEL);
;
struct sbiret sbiret;
printk("enclave_ioctl_exit...\n");

//TODO: free SM memory
/*while((addr = SBI_CALL_2(SBI_SM_FREE_ENCLAVE_MEM, &size, FREE_MAX_MEMORY)))
{
order = ilog2(size-1) + 1;
count = 0x1 << order;
if(count != size)
{
printk("KERNEL MODULE: the number of free pages is not exponential times of two\n");
return;
}
free_pages((long unsigned int)__va(addr), order);
}*/

sbiret = SBI_CALL_2(SBI_SM_FREE_ENCLAVE_MEM, __pa(size), FREE_MAX_MEMORY);

addr = (unsigned long)(sbiret.value);
while (addr)
{
order = ilog2((*size) - 1) + 1;
count = 0x1 << order;
if (count != (*size) && (*size > 0))
{
printk("KERNEL MODULE: the number of free pages is not exponential times of two\n");
kfree(size);
return;
}
printk("KERNEL MODULE: free secmem:paddr:%lx, vaddr:%lx, order:%lu\n", addr, __va(addr), order);
if ((*size) > 0)
{
free_pages((long unsigned int)__va(addr), (order - RISCV_PGSHIFT));
}

sbiret = SBI_CALL_2(SBI_SM_FREE_ENCLAVE_MEM, __pa(size), FREE_MAX_MEMORY);

addr = (unsigned long)(sbiret.value);
}

deregister_device:
kfree(size);
misc_deregister(&enclave_dev);
return;
}
Expand Down
11 changes: 5 additions & 6 deletions penglai-enclave-driver/penglai-enclave.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ enclave_t* create_enclave(int total_pages)
while(ret.value == ENCLAVE_NO_MEMORY)
{
//TODO: allocate certain memory region like sm_init instead of allocating size of one enclave
addr = __get_free_pages(GFP_HIGHUSER, order);
addr = __get_free_pages(GFP_ATOMIC, order);
if(!addr)
{
printk("KERNEL MODULE: can not get free page which order is 0x%lx", order);
Expand Down Expand Up @@ -113,7 +113,7 @@ enclave_t* create_enclave(int total_pages)
enclave->enclave_mem = enclave_mem;
enclave->untrusted_mem = untrusted_mem;

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

Expand All @@ -124,7 +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);
if(require_sec_memory) kfree(require_sec_memory);

return NULL;
}
Expand All @@ -143,9 +143,8 @@ int destroy_enclave(enclave_t* enclave)
enclave_mem = enclave->enclave_mem;
untrusted_mem = enclave->untrusted_mem;
enclave_mem_destroy(enclave_mem);

kfree(enclave_mem);
kfree(untrusted_mem);
if(enclave_mem)kfree(enclave_mem);
if(untrusted_mem)kfree(untrusted_mem);
kfree(enclave);

return 0;
Expand Down
3 changes: 2 additions & 1 deletion penglai-enclave-driver/penglai-enclave.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
#define SBI_SM_DESTROY_ENCLAVE 94
#define SBI_SM_ALLOC_ENCLAVE_MEM 93
#define SBI_SM_MEMORY_EXTEND 92
#define SBI_SM_FREE_ENCLAVE_MEM 91
#define SBI_SM_MEMORY_RECLAIM 91
#define SBI_SM_FREE_ENCLAVE_MEM 90
#define SBI_SM_DEBUG_PRINT 88

//Error codes of SBI_SM_ALLOC_ENCLAVE_MEM
Expand Down

0 comments on commit 9a5e2dc

Please sign in to comment.