From 50dab7b55311900bc68b9190092db39602dde8bd Mon Sep 17 00:00:00 2001 From: TXuian <1163589503@qq.com> Date: Mon, 18 Mar 2024 11:17:55 +0800 Subject: [PATCH] test cache --- .../arch/arm/armv7-a/cortex-a9/core.h | 2 +- .../hardkernel/cache/cache_common_ope.c | 8 ++-- .../intr/arm/armv7-a/cortex-a9/error_debug.c | 8 +++- .../intr/arm/armv7-a/cortex-a9/spinlock.c | 7 ++++ .../XiZi_AIoT/hardkernel/intr/spinlock.h | 3 +- .../mmu/arm/armv7-a/cortex-a9/mmu.c | 6 +++ .../arm/armv7-a/cortex-a9/pagetable_attr.c | 32 ++++++++------- .../XiZi_AIoT/hardkernel/mmu/mmu_common.h | 1 + Ubiquitous/XiZi_AIoT/services/app/init.c | 9 ++--- Ubiquitous/XiZi_AIoT/softkernel/main.c | 39 ++++++++++++------- .../XiZi_AIoT/softkernel/memory/share_page.c | 12 ++++-- .../softkernel/trap/default_irq_handler.c | 3 -- .../softkernel/trap/software_irq_handler.c | 4 +- 13 files changed, 85 insertions(+), 49 deletions(-) diff --git a/Ubiquitous/XiZi_AIoT/hardkernel/arch/arm/armv7-a/cortex-a9/core.h b/Ubiquitous/XiZi_AIoT/hardkernel/arch/arm/armv7-a/cortex-a9/core.h index c877945ce..129f07103 100644 --- a/Ubiquitous/XiZi_AIoT/hardkernel/arch/arm/armv7-a/cortex-a9/core.h +++ b/Ubiquitous/XiZi_AIoT/hardkernel/arch/arm/armv7-a/cortex-a9/core.h @@ -74,7 +74,7 @@ Modification: #include "cortex_a9.h" -#define NR_CPU 4 +#define NR_CPU 1 __attribute__((always_inline)) static inline uint32_t user_mode() { diff --git a/Ubiquitous/XiZi_AIoT/hardkernel/cache/cache_common_ope.c b/Ubiquitous/XiZi_AIoT/hardkernel/cache/cache_common_ope.c index 6f5978f2c..4d5e33d33 100644 --- a/Ubiquitous/XiZi_AIoT/hardkernel/cache/cache_common_ope.c +++ b/Ubiquitous/XiZi_AIoT/hardkernel/cache/cache_common_ope.c @@ -232,15 +232,15 @@ static struct DCacheDone dcache_done = { struct ICacheDone* hardkernel_icache_init(struct TraceTag* hardkernel_tag) { /* init icache */ - // icache_done.enable(); - icache_done.disable(); + icache_done.enable(); + // icache_done.disable(); return &icache_done; } struct DCacheDone* hardkernel_dcache_init(struct TraceTag* hardkernel_tag) { /* init dcache */ - // dcache_done.enable(); - dcache_done.disable(); + dcache_done.enable(); + // dcache_done.disable(); return &dcache_done; } \ No newline at end of file diff --git a/Ubiquitous/XiZi_AIoT/hardkernel/intr/arm/armv7-a/cortex-a9/error_debug.c b/Ubiquitous/XiZi_AIoT/hardkernel/intr/arm/armv7-a/cortex-a9/error_debug.c index 51f99c345..dc1ca496f 100644 --- a/Ubiquitous/XiZi_AIoT/hardkernel/intr/arm/armv7-a/cortex-a9/error_debug.c +++ b/Ubiquitous/XiZi_AIoT/hardkernel/intr/arm/armv7-a/cortex-a9/error_debug.c @@ -95,7 +95,9 @@ void handle_undefined_instruction(struct trapframe* tf) extern void context_switch(struct context**, struct context*); void dabort_handler(struct trapframe* r) { - spinlock_lock(&whole_kernel_lock); + if (!is_spinlock_locked(&whole_kernel_lock)) { + spinlock_lock(&whole_kernel_lock); + } uint32_t dfs, dfa; __asm__ __volatile__("mrc p15, 0, %0, c5, c0, 0" : "=r"(dfs)::); @@ -120,7 +122,9 @@ void dabort_handler(struct trapframe* r) void iabort_handler(struct trapframe* r) { - spinlock_lock(&whole_kernel_lock); + if (!is_spinlock_locked(&whole_kernel_lock)) { + spinlock_lock(&whole_kernel_lock); + } uint32_t ifs, ifa; __asm__ __volatile__("mrc p15, 0, %0, c5, c0, 1" : "=r"(ifs)::); diff --git a/Ubiquitous/XiZi_AIoT/hardkernel/intr/arm/armv7-a/cortex-a9/spinlock.c b/Ubiquitous/XiZi_AIoT/hardkernel/intr/arm/armv7-a/cortex-a9/spinlock.c index 4581ef0e7..e76712945 100644 --- a/Ubiquitous/XiZi_AIoT/hardkernel/intr/arm/armv7-a/cortex-a9/spinlock.c +++ b/Ubiquitous/XiZi_AIoT/hardkernel/intr/arm/armv7-a/cortex-a9/spinlock.c @@ -20,7 +20,9 @@ #include #include "assert.h" +#include "multicores.h" #include "spinlock.h" +#include "task.h" #include "trap_common.h" bool module_spinlock_use_intr_init(void) @@ -55,4 +57,9 @@ void _spinlock_unlock(struct spinlock* lock); void spinlock_unlock(struct spinlock* lock) { _spinlock_unlock(lock); +} + +bool is_spinlock_locked(struct spinlock* lock) +{ + return lock->owner_cpu != SPINLOCK_STATE_UNLOCK; } \ No newline at end of file diff --git a/Ubiquitous/XiZi_AIoT/hardkernel/intr/spinlock.h b/Ubiquitous/XiZi_AIoT/hardkernel/intr/spinlock.h index 7454f9eec..e1e08bb86 100644 --- a/Ubiquitous/XiZi_AIoT/hardkernel/intr/spinlock.h +++ b/Ubiquitous/XiZi_AIoT/hardkernel/intr/spinlock.h @@ -41,4 +41,5 @@ struct spinlock { // Mutex. bool module_spinlock_use_intr_init(void); void spinlock_init(struct spinlock* lock, char* name); void spinlock_lock(struct spinlock* lock); -void spinlock_unlock(struct spinlock* lock); \ No newline at end of file +void spinlock_unlock(struct spinlock* lock); +bool is_spinlock_locked(struct spinlock* lock); \ No newline at end of file diff --git a/Ubiquitous/XiZi_AIoT/hardkernel/mmu/arm/armv7-a/cortex-a9/mmu.c b/Ubiquitous/XiZi_AIoT/hardkernel/mmu/arm/armv7-a/cortex-a9/mmu.c index 7bc5254c2..50d783b90 100644 --- a/Ubiquitous/XiZi_AIoT/hardkernel/mmu/arm/armv7-a/cortex-a9/mmu.c +++ b/Ubiquitous/XiZi_AIoT/hardkernel/mmu/arm/armv7-a/cortex-a9/mmu.c @@ -82,6 +82,11 @@ static void tlb_flush_range(uintptr_t vstart, int len) } } +static void tlb_flush_all() +{ + CLEARTLB(0); +} + static struct MmuCommonDone mmu_common_done = { .MmuDevPteAttr = GetDevPteAttr, .MmuPdeAttr = GetPdeAttr, @@ -91,6 +96,7 @@ static struct MmuCommonDone mmu_common_done = { .LoadPgdirCrit = load_pgdir_critical, .LoadPgdir = load_pgdir, + .TlbFlushAll = tlb_flush_all, .TlbFlush = tlb_flush_range, }; diff --git a/Ubiquitous/XiZi_AIoT/hardkernel/mmu/arm/armv7-a/cortex-a9/pagetable_attr.c b/Ubiquitous/XiZi_AIoT/hardkernel/mmu/arm/armv7-a/cortex-a9/pagetable_attr.c index 455b1d759..477b92a4d 100644 --- a/Ubiquitous/XiZi_AIoT/hardkernel/mmu/arm/armv7-a/cortex-a9/pagetable_attr.c +++ b/Ubiquitous/XiZi_AIoT/hardkernel/mmu/arm/armv7-a/cortex-a9/pagetable_attr.c @@ -30,20 +30,6 @@ Modification: #include "mmu.h" #include "mmu_common.h" -void GetDevPteAttr(uintptr_t* attr) -{ - static char init = 0; - static PageTblEntry dev_pte_attr; - if (init == 0) { - init = 1; - - dev_pte_attr.entry = 0; - dev_pte_attr.desc_type = PAGE_4K; - dev_pte_attr.AP1_0 = AccessPermission_KernelOnly; - } - *attr = dev_pte_attr.entry; -} - void GetUsrPteAttr(uintptr_t* attr) { static char init = 0; @@ -55,6 +41,7 @@ void GetUsrPteAttr(uintptr_t* attr) usr_pte_attr.desc_type = PAGE_4K; usr_pte_attr.B = 1; usr_pte_attr.C = 1; + usr_pte_attr.S = 1; usr_pte_attr.AP1_0 = AccessPermission_KernelUser; } *attr = usr_pte_attr.entry; @@ -68,12 +55,28 @@ void GetUsrDevPteAttr(uintptr_t* attr) init = 1; usr_pte_attr.entry = 0; + usr_pte_attr.S = 1; usr_pte_attr.desc_type = PAGE_4K; usr_pte_attr.AP1_0 = AccessPermission_KernelUser; } *attr = usr_pte_attr.entry; } +void GetDevPteAttr(uintptr_t* attr) +{ + static char init = 0; + static PageTblEntry dev_pte_attr; + if (init == 0) { + init = 1; + + dev_pte_attr.entry = 0; + dev_pte_attr.S = 1; + dev_pte_attr.desc_type = PAGE_4K; + dev_pte_attr.AP1_0 = AccessPermission_KernelOnly; + } + *attr = dev_pte_attr.entry; +} + void GetKernPteAttr(uintptr_t* attr) { static char init = 0; @@ -85,6 +88,7 @@ void GetKernPteAttr(uintptr_t* attr) kern_pte_attr.desc_type = PAGE_4K; kern_pte_attr.B = 1; kern_pte_attr.C = 1; + kern_pte_attr.S = 1; kern_pte_attr.AP1_0 = AccessPermission_KernelOnly; } *attr = kern_pte_attr.entry; diff --git a/Ubiquitous/XiZi_AIoT/hardkernel/mmu/mmu_common.h b/Ubiquitous/XiZi_AIoT/hardkernel/mmu/mmu_common.h index 99aa7d38a..b979f80e8 100644 --- a/Ubiquitous/XiZi_AIoT/hardkernel/mmu/mmu_common.h +++ b/Ubiquitous/XiZi_AIoT/hardkernel/mmu/mmu_common.h @@ -29,6 +29,7 @@ struct MmuCommonDone void (*LoadPgdirCrit)(uintptr_t pgdir_paddr, struct TraceTag*); void (*LoadPgdir)(uintptr_t pgdir_paddr); + void (*TlbFlushAll)(); void (*TlbFlush)(uintptr_t vaddr, int len); }; diff --git a/Ubiquitous/XiZi_AIoT/services/app/init.c b/Ubiquitous/XiZi_AIoT/services/app/init.c index 62a62c401..3c3fc1446 100755 --- a/Ubiquitous/XiZi_AIoT/services/app/init.c +++ b/Ubiquitous/XiZi_AIoT/services/app/init.c @@ -19,11 +19,10 @@ int main(int argc, char* argv[]) { struct Session session; - printf("connecting MemFS\n"); - while (connect_session(&session, "MemFS", 8092) < 0) { - printf("connecting MemFS\n"); - } - printf("connect MemFS success\n"); + printf("init: connecting MemFS\n"); + while (connect_session(&session, "MemFS", 8092) < 0) + ; + printf("init: connect MemFS success\n"); int fd; char* shell_task_param[2] = { "/shell", 0 }; diff --git a/Ubiquitous/XiZi_AIoT/softkernel/main.c b/Ubiquitous/XiZi_AIoT/softkernel/main.c index 22c6d96bf..0c264e7ed 100644 --- a/Ubiquitous/XiZi_AIoT/softkernel/main.c +++ b/Ubiquitous/XiZi_AIoT/softkernel/main.c @@ -58,18 +58,6 @@ void configure_cpu(uint32_t cpu) // arm_icache_enable(); // arm_icache_invalidate(); - // struct TraceTag main_icache_tag, main_dcache_tag; - // AchieveResourceTag(&main_icache_tag, &hardkernel_tag, "icache-ac-resource"); - // AchieveResourceTag(&main_dcache_tag, &hardkernel_tag, "dcache-ac-resource"); - // struct ICacheDone* p_icache_driver = AchieveResource(&main_icache_tag); - // struct DCacheDone* p_dcache_driver = AchieveResource(&main_dcache_tag); - // // p_dcache_driver->enable(); - // // p_dcache_driver->invalidateall(); - // // p_icache_driver->enable(); - // // p_icache_driver->invalidateall(); - // p_dcache_driver->disable(); - // p_icache_driver->disable(); - // Invalidate SCU copy of TAG RAMs scu_secure_invalidate(cpu, all_ways); @@ -138,7 +126,6 @@ int main(void) show_xizi_bar(); int cpu_count = NR_CPU; - ; for (int i = 1; i < cpu_count; i++) { // start secondary cpus cpu_start_secondary(i); @@ -156,12 +143,38 @@ int main(void) assert(AchieveResourceTag(&scheduler_rights.mmu_driver_tag, &hardkernel_tag, "mmu-ac-resource")); assert(AchieveResourceTag(&scheduler_rights.intr_driver_tag, &hardkernel_tag, "intr-ac-resource")); + struct TraceTag main_icache_tag, main_dcache_tag; + AchieveResourceTag(&main_icache_tag, &hardkernel_tag, "icache-ac-resource"); + AchieveResourceTag(&main_dcache_tag, &hardkernel_tag, "dcache-ac-resource"); + struct ICacheDone* p_icache_driver = AchieveResource(&main_icache_tag); + struct DCacheDone* p_dcache_driver = AchieveResource(&main_dcache_tag); + core_init_done |= (1 << cpu_id); LOG_PRINTF("CPU %d init done\n", cpu_id); spinlock_unlock(&whole_kernel_lock); while (core_init_done != (1 << NR_CPU) - 1) ; + DEBUG_PRINTF("%d", cpu_id); + + // scu_enable(); + // configure_cpu(cpu_id); + // p_dcache_driver->enable(); + // p_icache_driver->enable(); + + // spinlock_lock(&whole_kernel_lock); + // p_dcache_driver->flushall(); + // spinlock_unlock(&whole_kernel_lock); + + // while (true) { + // spinlock_lock(&whole_kernel_lock); + // DEBUG("CPU: %d\n", cpu_id); + // secondary_cpu_load_kern_pgdir(&scheduler_rights.mmu_driver_tag, NULL); + // CLEARTLB(0); + // p_dcache_driver->flushall(); + // spinlock_unlock(&whole_kernel_lock); + // } + xizi_task_manager.task_scheduler(scheduler_rights); // never reached diff --git a/Ubiquitous/XiZi_AIoT/softkernel/memory/share_page.c b/Ubiquitous/XiZi_AIoT/softkernel/memory/share_page.c index 329a3a17c..335c28879 100644 --- a/Ubiquitous/XiZi_AIoT/softkernel/memory/share_page.c +++ b/Ubiquitous/XiZi_AIoT/softkernel/memory/share_page.c @@ -105,8 +105,10 @@ static uintptr_t map_task_share_page(struct TaskMicroDescriptor* task, const uin } if (task == cur_cpu()->task) { p_mmu_driver->TlbFlush(vaddr, 2 * nr_pages * PAGE_SIZE); + /// @todo clean range rather than all - p_dcache_done->flushall(); + // p_dcache_done->flushall(); + p_dcache_done->invalidateall(); // p_dcache_done->flush(vaddr, vaddr + 2 * nr_pages * PAGE_SIZE); } return vaddr; @@ -129,8 +131,10 @@ uintptr_t task_map_pages(struct TaskMicroDescriptor* task, const uintptr_t vaddr } if (task == cur_cpu()->task) { p_mmu_driver->TlbFlush(vaddr, nr_pages * PAGE_SIZE); + /// @todo clean range rather than all - p_dcache_done->flushall(); + // p_dcache_done->flushall(); + p_dcache_done->invalidateall(); // p_dcache_done->flush(vaddr, vaddr + nr_pages * PAGE_SIZE); } @@ -147,8 +151,10 @@ void unmap_task_share_pages(struct TaskMicroDescriptor* task, const uintptr_t ta xizi_pager.unmap_pages(task->pgdir.pd_addr, task_vaddr + (nr_pages * PAGE_SIZE), nr_pages * PAGE_SIZE); if (task == cur_cpu()->task) { p_mmu_driver->TlbFlush(task_vaddr, 2 * nr_pages * PAGE_SIZE); + /// @todo clean range rather than all - p_dcache_done->flushall(); + // p_dcache_done->flushall(); + p_dcache_done->invalidateall(); // p_dcache_done->flush(task_vaddr, task_vaddr + 2 * nr_pages * PAGE_SIZE); } } diff --git a/Ubiquitous/XiZi_AIoT/softkernel/trap/default_irq_handler.c b/Ubiquitous/XiZi_AIoT/softkernel/trap/default_irq_handler.c index f88691a99..213ab28df 100644 --- a/Ubiquitous/XiZi_AIoT/softkernel/trap/default_irq_handler.c +++ b/Ubiquitous/XiZi_AIoT/softkernel/trap/default_irq_handler.c @@ -66,8 +66,6 @@ void intr_irq_dispatch(struct trapframe* tf) return; } spinlock_lock(&whole_kernel_lock); - DSB(); - // DEBUG("CPU %d in\n", cur_cpuid()); struct TaskMicroDescriptor* current_task = cur_cpu()->task; if (LIKELY(current_task != NULL)) { @@ -95,7 +93,6 @@ void intr_irq_dispatch(struct trapframe* tf) } assert(current_task == cur_cpu()->task); - // DEBUG("CPU %d out\n", cur_cpuid()); spinlock_unlock(&whole_kernel_lock); p_intr_driver->cpu_irq_enable(); } diff --git a/Ubiquitous/XiZi_AIoT/softkernel/trap/software_irq_handler.c b/Ubiquitous/XiZi_AIoT/softkernel/trap/software_irq_handler.c index b2f71a6bf..623d01309 100644 --- a/Ubiquitous/XiZi_AIoT/softkernel/trap/software_irq_handler.c +++ b/Ubiquitous/XiZi_AIoT/softkernel/trap/software_irq_handler.c @@ -53,8 +53,7 @@ void software_irq_dispatch(struct trapframe* tf) p_intr_driver->cpu_irq_disable(); spinlock_lock(&whole_kernel_lock); - DSB(); - // DEBUG("CPU %d in\n", cur_cpuid()); + // get current task struct TaskMicroDescriptor* cur_task = cur_cpu()->task; /// @todo: Handle dead task @@ -81,7 +80,6 @@ void software_irq_dispatch(struct trapframe* tf) panic("Exit reaches"); } - // DEBUG("CPU %d out\n", cur_cpuid()); spinlock_unlock(&whole_kernel_lock); p_intr_driver->cpu_irq_enable(); } \ No newline at end of file