diff --git a/penglai-enclave-driver/penglai-enclave-driver.c b/penglai-enclave-driver/penglai-enclave-driver.c index f9b16034d..30c0c3cd2 100644 --- a/penglai-enclave-driver/penglai-enclave-driver.c +++ b/penglai-enclave-driver/penglai-enclave-driver.c @@ -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; } diff --git a/penglai-enclave-driver/penglai-enclave.c b/penglai-enclave-driver/penglai-enclave.c index 222a3360f..3e270f83f 100644 --- a/penglai-enclave-driver/penglai-enclave.c +++ b/penglai-enclave-driver/penglai-enclave.c @@ -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); @@ -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 @@ -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; } @@ -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; diff --git a/penglai-enclave-driver/penglai-enclave.h b/penglai-enclave-driver/penglai-enclave.h index 616e4a775..02d68ac64 100644 --- a/penglai-enclave-driver/penglai-enclave.h +++ b/penglai-enclave-driver/penglai-enclave.h @@ -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