From 66403739f72fe7d96d3151b4a12001d6b8645286 Mon Sep 17 00:00:00 2001 From: ZhaoXi Date: Wed, 15 Nov 2023 14:00:17 +0800 Subject: [PATCH 1/2] Modify only the code format --- opensbi-1.2/lib/sbi/sm/sm.c | 341 ++++++++++++++++++------------------ 1 file changed, 173 insertions(+), 168 deletions(-) diff --git a/opensbi-1.2/lib/sbi/sm/sm.c b/opensbi-1.2/lib/sbi/sm/sm.c index a93544154..3d4e45149 100644 --- a/opensbi-1.2/lib/sbi/sm/sm.c +++ b/opensbi-1.2/lib/sbi/sm/sm.c @@ -12,213 +12,216 @@ void sm_init() { - printm("[Penglai Monitor] %s invoked\r\n",__func__); - platform_init(); - attest_init(); + printm("[Penglai Monitor] %s invoked\r\n", __func__); + platform_init(); + attest_init(); } uintptr_t sm_mm_init(uintptr_t paddr, unsigned long size) { - uintptr_t retval = 0; + uintptr_t retval = 0; - printm("[Penglai Monitor] %s invoked\r\n",__func__); + printm("[Penglai Monitor] %s invoked\r\n", __func__); - printm("[Penglai Monitor] %s paddr:0x%lx, size:0x%lx\r\n",__func__, paddr, size); - /*DEBUG: Dump PMP registers here */ - dump_pmps(); - retval = mm_init(paddr, size); - /*DEBUG: Dump PMP registers here */ - dump_pmps(); + printm("[Penglai Monitor] %s paddr:0x%lx, size:0x%lx\r\n", __func__, + paddr, size); + /*DEBUG: Dump PMP registers here */ + dump_pmps(); + retval = mm_init(paddr, size); + /*DEBUG: Dump PMP registers here */ + dump_pmps(); - printm("[Penglai Monitor] %s ret:%ld \r\n",__func__, retval); - return retval; + printm("[Penglai Monitor] %s ret:%ld \r\n", __func__, retval); + return retval; } uintptr_t sm_mm_extend(uintptr_t paddr, unsigned long size) { - uintptr_t retval = 0; - printm("[Penglai Monitor] %s invoked\r\n",__func__); + uintptr_t retval = 0; + printm("[Penglai Monitor] %s invoked\r\n", __func__); - retval = mm_init(paddr, size); + retval = mm_init(paddr, size); - printm("[Penglai Monitor] %s return:%ld\r\n",__func__, retval); - return retval; + printm("[Penglai Monitor] %s return:%ld\r\n", __func__, retval); + return retval; } -uintptr_t sm_debug_print(uintptr_t* regs, uintptr_t arg0) +uintptr_t sm_debug_print(uintptr_t *regs, uintptr_t arg0) { - print_buddy_system(); - return 0; + print_buddy_system(); + return 0; } uintptr_t sm_alloc_enclave_mem(uintptr_t mm_alloc_arg) { - struct mm_alloc_arg_t mm_alloc_arg_local; - uintptr_t retval = 0; - - printm("[Penglai Monitor] %s invoked\r\n",__func__); - - retval = copy_from_host(&mm_alloc_arg_local, - (struct mm_alloc_arg_t*)mm_alloc_arg, - sizeof(struct mm_alloc_arg_t)); - if(retval != 0) - { - printm_err("M mode: sm_alloc_enclave_mem: unknown error happended when copy from host\r\n"); - return ENCLAVE_ERROR; - } - - dump_pmps(); - unsigned long resp_size = 0; - void* paddr = mm_alloc(mm_alloc_arg_local.req_size, &resp_size); - if(paddr == NULL) - { - printm("M mode: sm_alloc_enclave_mem: no enough memory\r\n"); - return ENCLAVE_NO_MEMORY; - } - dump_pmps(); - - //grant kernel access to this memory - if(grant_kernel_access(paddr, resp_size) != 0) - { - printm_err("M mode: ERROR: faile to grant kernel access to pa 0x%lx, size 0x%lx\r\n", (unsigned long) paddr, resp_size); - mm_free(paddr, resp_size); - return ENCLAVE_ERROR; - } - - mm_alloc_arg_local.resp_addr = (uintptr_t)paddr; - mm_alloc_arg_local.resp_size = resp_size; - - retval = copy_to_host((struct mm_alloc_arg_t*)mm_alloc_arg, - &mm_alloc_arg_local, - sizeof(struct mm_alloc_arg_t)); - if(retval != 0) - { - printm_err("M mode: sm_alloc_enclave_mem: unknown error happended when copy to host\r\n"); - return ENCLAVE_ERROR; - } - - printm("[Penglai Monitor] %s return:%ld\r\n",__func__, retval); - - return ENCLAVE_SUCCESS; + struct mm_alloc_arg_t mm_alloc_arg_local; + uintptr_t retval = 0; + + printm("[Penglai Monitor] %s invoked\r\n", __func__); + + retval = copy_from_host(&mm_alloc_arg_local, + (struct mm_alloc_arg_t *)mm_alloc_arg, + sizeof(struct mm_alloc_arg_t)); + if (retval != 0) { + printm_err( + "M mode: sm_alloc_enclave_mem: unknown error happended when copy from host\r\n"); + return ENCLAVE_ERROR; + } + + dump_pmps(); + unsigned long resp_size = 0; + void *paddr = mm_alloc(mm_alloc_arg_local.req_size, &resp_size); + if (paddr == NULL) { + printm("M mode: sm_alloc_enclave_mem: no enough memory\r\n"); + return ENCLAVE_NO_MEMORY; + } + dump_pmps(); + + //grant kernel access to this memory + if (grant_kernel_access(paddr, resp_size) != 0) { + printm_err( + "M mode: ERROR: faile to grant kernel access to pa 0x%lx, size 0x%lx\r\n", + (unsigned long)paddr, resp_size); + mm_free(paddr, resp_size); + return ENCLAVE_ERROR; + } + + mm_alloc_arg_local.resp_addr = (uintptr_t)paddr; + mm_alloc_arg_local.resp_size = resp_size; + + retval = copy_to_host((struct mm_alloc_arg_t *)mm_alloc_arg, + &mm_alloc_arg_local, + sizeof(struct mm_alloc_arg_t)); + if (retval != 0) { + printm_err( + "M mode: sm_alloc_enclave_mem: unknown error happended when copy to host\r\n"); + return ENCLAVE_ERROR; + } + + printm("[Penglai Monitor] %s return:%ld\r\n", __func__, retval); + + return ENCLAVE_SUCCESS; } uintptr_t sm_create_enclave(uintptr_t enclave_sbi_param) { - struct enclave_sbi_param_t enclave_sbi_param_local; - uintptr_t retval = 0; - - printm("[Penglai Monitor] %s invoked\r\n",__func__); - - retval = copy_from_host(&enclave_sbi_param_local, - (struct enclave_sbi_param_t*)enclave_sbi_param, - sizeof(struct enclave_sbi_param_t)); - if(retval != 0) - { - printm_err("M mode: sm_create_enclave: unknown error happended when copy from host\r\n"); - return ENCLAVE_ERROR; - } - - void* paddr = (void*)enclave_sbi_param_local.paddr; - unsigned long size = (unsigned long)enclave_sbi_param_local.size; - if(retrieve_kernel_access(paddr, size) != 0)//we always allow kernel access the memory now - { - mm_free(paddr, size); - return -1UL; - } - - retval = create_enclave(enclave_sbi_param_local); - - printm("[Penglai Monitor] %s created return value:%ld \r\n",__func__, retval); - return retval; + struct enclave_sbi_param_t enclave_sbi_param_local; + uintptr_t retval = 0; + + printm("[Penglai Monitor] %s invoked\r\n", __func__); + + retval = copy_from_host(&enclave_sbi_param_local, + (struct enclave_sbi_param_t *)enclave_sbi_param, + sizeof(struct enclave_sbi_param_t)); + if (retval != 0) { + printm_err( + "M mode: sm_create_enclave: unknown error happended when copy from host\r\n"); + return ENCLAVE_ERROR; + } + + void *paddr = (void *)enclave_sbi_param_local.paddr; + unsigned long size = (unsigned long)enclave_sbi_param_local.size; + if (retrieve_kernel_access(paddr, size) != + 0) //we always allow kernel access the memory now + { + mm_free(paddr, size); + return -1UL; + } + + retval = create_enclave(enclave_sbi_param_local); + + printm("[Penglai Monitor] %s created return value:%ld \r\n", __func__, + retval); + return retval; } uintptr_t sm_attest_enclave(uintptr_t eid, uintptr_t report, uintptr_t nonce) { - uintptr_t retval; - printm("[Penglai Monitor] %s invoked, eid:%ld\r\n",__func__, eid); + uintptr_t retval; + printm("[Penglai Monitor] %s invoked, eid:%ld\r\n", __func__, eid); - retval = attest_enclave(eid, report, nonce); + retval = attest_enclave(eid, report, nonce); - printm("[Penglai Monitor] %s return: %ld\r\n",__func__, retval); + printm("[Penglai Monitor] %s return: %ld\r\n", __func__, retval); - return retval; + return retval; } -uintptr_t sm_run_enclave(uintptr_t* regs, unsigned long eid) +uintptr_t sm_run_enclave(uintptr_t *regs, unsigned long eid) { - uintptr_t retval; - printm("[Penglai Monitor] %s invoked, eid:%ld\r\n",__func__, eid); + uintptr_t retval; + printm("[Penglai Monitor] %s invoked, eid:%ld\r\n", __func__, eid); - retval = run_enclave(regs, (unsigned int)eid); + retval = run_enclave(regs, (unsigned int)eid); - printm("[Penglai Monitor] %s return: %ld\r\n",__func__, retval); + printm("[Penglai Monitor] %s return: %ld\r\n", __func__, retval); - return retval; + return retval; } -uintptr_t sm_stop_enclave(uintptr_t* regs, unsigned long eid) +uintptr_t sm_stop_enclave(uintptr_t *regs, unsigned long eid) { - uintptr_t retval; - printm("[Penglai Monitor] %s invoked, eid:%ld\r\n",__func__, eid); + uintptr_t retval; + printm("[Penglai Monitor] %s invoked, eid:%ld\r\n", __func__, eid); - retval = stop_enclave(regs, (unsigned int)eid); + retval = stop_enclave(regs, (unsigned int)eid); - printm("[Penglai Monitor] %s return: %ld\r\n",__func__, retval); - return retval; + printm("[Penglai Monitor] %s return: %ld\r\n", __func__, retval); + return retval; } -uintptr_t sm_resume_enclave(uintptr_t* regs, unsigned long eid) +uintptr_t sm_resume_enclave(uintptr_t *regs, unsigned long eid) { - uintptr_t retval = 0; - uintptr_t resume_func_id = regs[11]; - - switch(resume_func_id) - { - case RESUME_FROM_TIMER_IRQ: - retval = resume_enclave(regs, eid); - break; - case RESUME_FROM_STOP: - retval = resume_from_stop(regs, eid); - break; - case RESUME_FROM_OCALL: - retval = resume_from_ocall(regs, eid); - break; - default: - break; - } - - return retval; + uintptr_t retval = 0; + uintptr_t resume_func_id = regs[11]; + + switch (resume_func_id) { + case RESUME_FROM_TIMER_IRQ: + retval = resume_enclave(regs, eid); + break; + case RESUME_FROM_STOP: + retval = resume_from_stop(regs, eid); + break; + case RESUME_FROM_OCALL: + retval = resume_from_ocall(regs, eid); + break; + default: + break; + } + + return retval; } -uintptr_t sm_exit_enclave(uintptr_t* regs, unsigned long retval) +uintptr_t sm_exit_enclave(uintptr_t *regs, unsigned long retval) { - uintptr_t ret; - printm("[Penglai Monitor] %s invoked\r\n",__func__); + uintptr_t ret; + printm("[Penglai Monitor] %s invoked\r\n", __func__); - ret = exit_enclave(regs, retval); + ret = exit_enclave(regs, retval); - printm("[Penglai Monitor] %s return: %ld\r\n",__func__, ret); + printm("[Penglai Monitor] %s return: %ld\r\n", __func__, ret); - return ret; + return ret; } -uintptr_t sm_enclave_ocall(uintptr_t* regs, uintptr_t ocall_id, uintptr_t arg0, uintptr_t arg1) +uintptr_t sm_enclave_ocall(uintptr_t *regs, uintptr_t ocall_id, uintptr_t arg0, + uintptr_t arg1) { - uintptr_t ret = 0; - switch(ocall_id) - { - case OCALL_SYS_WRITE: - ret = enclave_sys_write(regs); - break; - case OCALL_USER_DEFINED: - ret = enclave_user_defined_ocall(regs, arg0); - break; - default: - printm_err("[Penglai Monitor@%s] wrong ocall_id(%ld)\r\n", __func__, ocall_id); - ret = -1UL; - break; - } - return ret; + uintptr_t ret = 0; + switch (ocall_id) { + case OCALL_SYS_WRITE: + ret = enclave_sys_write(regs); + break; + case OCALL_USER_DEFINED: + ret = enclave_user_defined_ocall(regs, arg0); + break; + default: + printm_err("[Penglai Monitor@%s] wrong ocall_id(%ld)\r\n", + __func__, ocall_id); + ret = -1UL; + break; + } + return ret; } /** @@ -230,14 +233,16 @@ uintptr_t sm_enclave_ocall(uintptr_t* regs, uintptr_t ocall_id, uintptr_t arg0, * \param key_buf_va Key buffer pointer in enclave address space. * \param key_buf_len Key buffer length in bytes. */ -uintptr_t sm_enclave_get_key(uintptr_t* regs, uintptr_t salt_va, uintptr_t salt_len, - uintptr_t key_buf_va, uintptr_t key_buf_len) +uintptr_t sm_enclave_get_key(uintptr_t *regs, uintptr_t salt_va, + uintptr_t salt_len, uintptr_t key_buf_va, + uintptr_t key_buf_len) { - uintptr_t ret = 0; + uintptr_t ret = 0; - ret = enclave_derive_seal_key(regs, salt_va, salt_len, key_buf_va, key_buf_len); + ret = enclave_derive_seal_key(regs, salt_va, salt_len, key_buf_va, + key_buf_len); - return ret; + return ret; } /** @@ -248,23 +253,23 @@ uintptr_t sm_enclave_get_key(uintptr_t* regs, uintptr_t salt_va, uintptr_t salt_ */ uintptr_t sm_destroy_enclave(uintptr_t *regs, uintptr_t enclave_id) { - uintptr_t ret = 0; - printm("[Penglai Monitor] %s invoked\r\n",__func__); + uintptr_t ret = 0; + printm("[Penglai Monitor] %s invoked\r\n", __func__); - ret = destroy_enclave(regs, enclave_id); + ret = destroy_enclave(regs, enclave_id); - printm("[Penglai Monitor] %s return: %ld\r\n",__func__, ret); + printm("[Penglai Monitor] %s return: %ld\r\n", __func__, ret); - return ret; + return ret; } uintptr_t sm_do_timer_irq(uintptr_t *regs, uintptr_t mcause, uintptr_t mepc) { - uintptr_t ret; + uintptr_t ret; - ret = do_timer_irq(regs, mcause, mepc); + ret = do_timer_irq(regs, mcause, mepc); - regs[10] = 0; //no errors in all cases for timer handler - regs[11] = ret; //value - return ret; + regs[10] = 0; //no errors in all cases for timer handler + regs[11] = ret; //value + return ret; } From 2f7afa8463f9610c8c207361fe80dbd978af8c4e Mon Sep 17 00:00:00 2001 From: ZhaoXi Date: Fri, 17 Nov 2023 14:45:57 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=90=9E=20fix(opensbi-1.2):=20Free=20m?= =?UTF-8?q?emory?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a sbi call to reclaim memory and clear pmp settings when uninstalling the fluffy module --- .../include/sm/platform/pmp/enclave_mm.h | 2 + opensbi-1.2/include/sm/print.h | 2 +- opensbi-1.2/include/sm/sm.h | 8 ++ opensbi-1.2/lib/sbi/sbi_ecall_penglai.c | 3 + opensbi-1.2/lib/sbi/sm/enclave.c | 30 ++++-- .../lib/sbi/sm/platform/pmp/enclave_mm.c | 98 +++++++++++++++++++ opensbi-1.2/lib/sbi/sm/sm.c | 49 ++++++++++ 7 files changed, 181 insertions(+), 11 deletions(-) diff --git a/opensbi-1.2/include/sm/platform/pmp/enclave_mm.h b/opensbi-1.2/include/sm/platform/pmp/enclave_mm.h index c563f9fc0..9a6d75d8b 100644 --- a/opensbi-1.2/include/sm/platform/pmp/enclave_mm.h +++ b/opensbi-1.2/include/sm/platform/pmp/enclave_mm.h @@ -75,6 +75,8 @@ void* mm_alloc(unsigned long req_size, unsigned long* resp_size); int mm_free(void* paddr, unsigned long size); +int mm_free_clear(void* paddr, unsigned long size); + void print_buddy_system(); #endif /* _ENCLAVE_MM_H */ diff --git a/opensbi-1.2/include/sm/print.h b/opensbi-1.2/include/sm/print.h index 0c31c278a..18568ddf1 100644 --- a/opensbi-1.2/include/sm/print.h +++ b/opensbi-1.2/include/sm/print.h @@ -2,7 +2,7 @@ #define SM_PRINT_H #include -#define PENGLAI_DEBUG +// #define PENGLAI_DEBUG #ifdef PENGLAI_DEBUG #define printm(...) sbi_printf(__VA_ARGS__) #else diff --git a/opensbi-1.2/include/sm/sm.h b/opensbi-1.2/include/sm/sm.h index 17156b67b..2753bd59e 100644 --- a/opensbi-1.2/include/sm/sm.h +++ b/opensbi-1.2/include/sm/sm.h @@ -29,6 +29,7 @@ extern uintptr_t _fw_start[], _fw_end[]; #define SBI_ALLOC_ENCLAVE_MM 93 #define SBI_MEMORY_EXTEND 92 #define SBI_MEMORY_RECLAIM 91 +#define SBI_FREE_ENCLAVE_MEM 90 #define SBI_DEBUG_PRINT 88 //Enclave SBI numbers @@ -52,6 +53,11 @@ extern uintptr_t _fw_start[], _fw_end[]; #define RESUME_FROM_STOP 2003 #define RESUME_FROM_OCALL 2 +#define FLAG_DESTROY 0 +#define DIRECT_DESTROY 1 +#define FREE_MAX_MEMORY 2 +#define FREE_SPEC_MEMORY 3 + void sm_init(); uintptr_t sm_mm_init(uintptr_t paddr, unsigned long size); @@ -60,6 +66,8 @@ uintptr_t sm_mm_extend(uintptr_t paddr, unsigned long size); 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_attest_enclave(uintptr_t enclave_id, uintptr_t report, uintptr_t nonce); diff --git a/opensbi-1.2/lib/sbi/sbi_ecall_penglai.c b/opensbi-1.2/lib/sbi/sbi_ecall_penglai.c index 74628f1b7..3539c90e5 100644 --- a/opensbi-1.2/lib/sbi/sbi_ecall_penglai.c +++ b/opensbi-1.2/lib/sbi/sbi_ecall_penglai.c @@ -51,6 +51,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_FREE_ENCLAVE_MEM: + ret= sm_free_enclave_mem(regs->a0, regs->a1); + break; default: sbi_printf("[Penglai@Monitor] host interface(funcid:%ld) not supported yet\n", funcid); ret = SBI_ENOTSUPP; diff --git a/opensbi-1.2/lib/sbi/sm/enclave.c b/opensbi-1.2/lib/sbi/sm/enclave.c index abc862d1b..1eb31e35f 100644 --- a/opensbi-1.2/lib/sbi/sm/enclave.c +++ b/opensbi-1.2/lib/sbi/sm/enclave.c @@ -113,7 +113,7 @@ struct link_mem_t* add_link_mem(struct link_mem_t** tail) return new_link_mem; } -int remove_link_mem(struct link_mem_t** head, struct link_mem_t* ptr) +int remove_link_mem(struct link_mem_t** head, struct link_mem_t* ptr,bool clear) { struct link_mem_t *cur_link_mem, *tmp_link_mem; int retval =0; @@ -122,7 +122,12 @@ int remove_link_mem(struct link_mem_t** head, struct link_mem_t* ptr) if (cur_link_mem == ptr) { *head = cur_link_mem->next_link_mem; - mm_free(cur_link_mem, cur_link_mem->mem_size); + if (clear) + { + mm_free_clear(cur_link_mem, cur_link_mem->mem_size); + }else{ + mm_free(cur_link_mem, cur_link_mem->mem_size); + } return 1; } @@ -133,7 +138,12 @@ int remove_link_mem(struct link_mem_t** head, struct link_mem_t* ptr) tmp_link_mem = cur_link_mem->next_link_mem; cur_link_mem->next_link_mem = cur_link_mem->next_link_mem->next_link_mem; //FIXME - mm_free(tmp_link_mem, tmp_link_mem->mem_size); + if (clear) + { + mm_free_clear(tmp_link_mem, tmp_link_mem->mem_size); + }else{ + mm_free(tmp_link_mem, tmp_link_mem->mem_size); + } return retval; } } @@ -207,7 +217,7 @@ static struct enclave_t* alloc_enclave() return enclave; } -static int free_enclave(int eid) +static int free_enclave(int eid, bool clear) { struct link_mem_t *cur; struct enclave_t *enclave = NULL; @@ -226,6 +236,7 @@ static int free_enclave(int eid) enclave->state = INVALID; found = 1; ret_val = 0; + remove_link_mem(&enclave_metadata_head,cur,clear); break; } count += cur->slab_num; @@ -462,7 +473,7 @@ uintptr_t create_enclave(struct enclave_sbi_param_t create_args) spin_unlock(&enclave_metadata_lock); //free enclave struct - free_enclave(eid); //the enclave state will be set INVALID here + free_enclave(eid,0); //the enclave state will be set INVALID here return ENCLAVE_ERROR; } @@ -602,7 +613,7 @@ uintptr_t destroy_enclave(uintptr_t* regs, unsigned int eid) spin_unlock(&enclave_metadata_lock); //free enclave struct - retval = free_enclave(eid); //the enclave state will be set INVALID here + retval = free_enclave(eid,0); //the enclave state will be set INVALID here return retval; } //FIXME: what if the enclave->state is RUNNABLE now? @@ -680,7 +691,7 @@ uintptr_t resume_enclave(uintptr_t* regs, unsigned int eid) spin_unlock(&enclave_metadata_lock); //free enclave struct - free_enclave(eid); //the enclave state will be set INVALID here + free_enclave(eid,0); //the enclave state will be set INVALID here return ENCLAVE_SUCCESS; //this will break the infinite loop in the enclave-driver } @@ -771,8 +782,7 @@ uintptr_t exit_enclave(uintptr_t* regs, unsigned long retval) spin_unlock(&enclave_metadata_lock); //free enclave struct - free_enclave(eid); - + free_enclave(eid, 0); return 0; } @@ -947,7 +957,7 @@ uintptr_t do_timer_irq(uintptr_t *regs, uintptr_t mcause, uintptr_t mepc) spin_unlock(&enclave_metadata_lock); //free enclave struct - retval = free_enclave(eid); //the enclave state will be set INVALID here + retval = free_enclave(eid,0); //the enclave state will be set INVALID here retval = ENCLAVE_SUCCESS; //this means we will not run any more goto timer_irq_out; diff --git a/opensbi-1.2/lib/sbi/sm/platform/pmp/enclave_mm.c b/opensbi-1.2/lib/sbi/sm/platform/pmp/enclave_mm.c index 3db174150..232f79903 100644 --- a/opensbi-1.2/lib/sbi/sm/platform/pmp/enclave_mm.c +++ b/opensbi-1.2/lib/sbi/sm/platform/pmp/enclave_mm.c @@ -1102,6 +1102,104 @@ int mm_free(void* req_paddr, unsigned long free_size) //printm("after mm_free\r\n"); //print_buddy_system(); +mm_free_out: + spin_unlock(&pmp_bitmap_lock); + return ret_val; +} +//TODO:Reserved interfaces for calls to reclaim unused memory +int mm_free_clear(void* req_paddr, unsigned long free_size) +{ + //check this paddr is 2^power aligned + uintptr_t paddr = (uintptr_t)req_paddr; + unsigned long order = ilog2(free_size-1) + 1; + unsigned long size = 1 << order; + if(check_mem_size(paddr, size) < 0) + return -1; + + int ret_val = 0; + int region_idx = 0; + struct mm_list_t* mm_region = PADDR_2_MM_LIST(paddr); + mm_region->order = order; + mm_region->prev_mm = NULL; + mm_region->next_mm = NULL; + + spin_lock(&pmp_bitmap_lock); + + //print_buddy_system(); + + for(region_idx=0; region_idx < N_PMP_REGIONS; ++region_idx) + { + if(mm_regions[region_idx].valid && region_contain(mm_regions[region_idx].paddr, mm_regions[region_idx].size, paddr, size)) + { + break; + } + } + if(region_idx >= N_PMP_REGIONS) + { + printm("mm_free: buddy system doesn't contain memory(addr 0x%lx, order %ld)\r\n", paddr, order); + ret_val = -1; + goto mm_free_out; + } + + //check whether this region overlap with existing free mm_lists + struct mm_list_head_t* mm_list_head = mm_regions[region_idx].mm_list_head; + while(mm_list_head) + { + struct mm_list_t* mm_region = mm_list_head->mm_list; + while(mm_region) + { + uintptr_t region_paddr = (uintptr_t)MM_LIST_2_PADDR(mm_region); + unsigned long region_size = 1 << mm_region->order; + if(region_overlap(paddr, size, region_paddr, region_size)) + { + printm("mm_free: memory(addr 0x%lx order %ld) overlap with free memory(addr 0x%lx order %d)\r\n", paddr, order, region_paddr, mm_region->order); + ret_val = -1; + break; + } + mm_region = mm_region->next_mm; + } + if(mm_region) + break; + + mm_list_head = mm_list_head->next_list_head; + } + if(mm_list_head) + { + goto mm_free_out; + } + + //insert with merge + ret_val = insert_mm_region(region_idx, mm_region, 1); + if(ret_val < 0) + { + printm("mm_free: failed to insert mm(addr 0x%lx, order %ld)\r\n in mm_regions[%d]\r\n", paddr, order, region_idx); + } + + //printm("after mm_free\r\n"); + //print_buddy_system(); + int pmp_idx; + struct pmp_config_t pmp_config; + pmp_idx = REGION_TO_PMP(region_idx); + pmp_config = get_pmp(pmp_idx); + + struct mm_region_t mm_regiont = mm_regions[region_idx]; + mm_list_head = mm_regiont.mm_list_head; + struct mm_list_t *mm_list = mm_list_head->mm_list; + if (((long int *)MM_LIST_2_PADDR(mm_list) == (long int *)pmp_config.paddr)&&((pmp_config.size) == (1<order))) + { + delete_certain_region(region_idx,&mm_list_head,mm_list); + mm_regions[region_idx].valid = 0; + mm_regions[region_idx].paddr = 0; + mm_regions[region_idx].mm_list_head = NULL; + // release_big_emem_lock(__func__); + clear_pmp_and_sync(pmp_idx); + // acquire_big_emem_lock(__func__); + } + + printm("***after mm_free***\r\n"); + print_buddy_system(); + dump_pmps(); + mm_free_out: spin_unlock(&pmp_bitmap_lock); return ret_val; diff --git a/opensbi-1.2/lib/sbi/sm/sm.c b/opensbi-1.2/lib/sbi/sm/sm.c index 3d4e45149..781743f9b 100644 --- a/opensbi-1.2/lib/sbi/sm/sm.c +++ b/opensbi-1.2/lib/sbi/sm/sm.c @@ -273,3 +273,52 @@ uintptr_t sm_do_timer_irq(uintptr_t *regs, uintptr_t mcause, uintptr_t mepc) regs[11] = ret; //value return ret; } + +uintptr_t sm_free_enclave_mem(uintptr_t size_ptr, unsigned long flag) +{ + uintptr_t ret = 0; + unsigned long size = 0; + dump_pmps(); + switch (flag) { + case FREE_MAX_MEMORY: + for (size_t i = NPMP - 2; i >= 0; i--) { + int pmp_idx = i; + struct pmp_config_t pmp_config = get_pmp(pmp_idx); + + if (pmp_config.paddr == 0 || pmp_config.size == 0) { + continue; + } + + if (pmp_idx == 0) { + printm("M mode: sm_memory_reclaim: There is no mem to reclaim\r\n"); + dump_pmps(); + size = 0; + ret = 0; + break; + } + + clear_pmp_and_sync(pmp_idx); + ret = pmp_config.paddr; + size = pmp_config.size; + + break; + } + break; + case FREE_SPEC_MEMORY: + /*free */ + { //TODO:Reserved interfaces for calls to reclaim unused memory + struct pmp_config_t pmp_config = get_pmp(15); + clear_pmp_and_sync(15); + ret = pmp_config.paddr; + size = 0; + } + break; + default: + ret = 0; + size = 0; + break; + } + + copy_to_host((void *)size_ptr, (void *)(&size), sizeof(unsigned long)); + return ret; +} \ No newline at end of file