From 31098d3d756647f324106d71a4697aab448c4031 Mon Sep 17 00:00:00 2001 From: simin Date: Sun, 11 Nov 2012 22:31:32 +0900 Subject: [PATCH] add nocache for mmap. usage: void *va = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS | 0x40, -1, 0); --- kernel/include/process.h | 6 +++ kernel/process.c | 87 +++++++++++++++++++++++++++++++++++++++- kernel/syscall.c | 27 ++++++++++--- 3 files changed, 113 insertions(+), 7 deletions(-) diff --git a/kernel/include/process.h b/kernel/include/process.h index 24779c7f..e3a026af 100644 --- a/kernel/include/process.h +++ b/kernel/include/process.h @@ -103,6 +103,12 @@ unsigned long extend_process_region(struct process *proc, unsigned long start, unsigned long end, unsigned long address); +#ifdef USE_NOCACHE_MMAP +unsigned long extend_process_nocache_region(struct process *proc, + unsigned long start, unsigned long end, + unsigned long address); +#endif + void schedule(void); void runq_add_proc(struct process *proc, int cpu_id); void runq_del_proc(struct process *proc, int cpu_id); diff --git a/kernel/process.c b/kernel/process.c index af25959d..089ff9ba 100644 --- a/kernel/process.c +++ b/kernel/process.c @@ -9,7 +9,7 @@ #include #include -#define DEBUG_PRINT_PROCESS +//#define DEBUG_PRINT_PROCESS #ifdef DEBUG_PRINT_PROCESS #define dkprintf kprintf @@ -353,6 +353,91 @@ unsigned long extend_process_region(struct process *proc, return address; } +#ifdef USE_NOCACHE_MMAP +unsigned long extend_process_nocache_region(struct process *proc, + unsigned long start, unsigned long end, + unsigned long address) +{ + unsigned long aligned_end, aligned_new_end; + void *p; + + if (!address || address < start || address >= USER_END) { + return end; + } + + aligned_end = ((end + PAGE_SIZE - 1) & PAGE_MASK); + + if (aligned_end >= address) { + return address; + } + + aligned_new_end = (address + PAGE_SIZE - 1) & PAGE_MASK; + +#ifdef USE_LARGE_PAGES + if (aligned_new_end - aligned_end >= LARGE_PAGE_SIZE) { + unsigned long p_aligned; + unsigned long old_aligned_end = aligned_end; + + if ((aligned_end & (LARGE_PAGE_SIZE - 1)) != 0) { + + aligned_end = (aligned_end + (LARGE_PAGE_SIZE - 1)) & LARGE_PAGE_MASK; + /* Fill in the gap between old_aligned_end and aligned_end + * with regular pages */ + p = allocate_pages((aligned_end - old_aligned_end) >> PAGE_SHIFT, 0); + add_process_memory_range(proc, old_aligned_end, aligned_end, + virt_to_phys(p), 0); + + dkprintf("filled in gap for LARGE_PAGE_SIZE aligned start: 0x%lX -> 0x%lX\n", + old_aligned_end, aligned_end); + } + + /* Add large region for the actual mapping */ + aligned_new_end = (aligned_new_end + (aligned_end - old_aligned_end) + + (LARGE_PAGE_SIZE - 1)) & LARGE_PAGE_MASK; + address = aligned_new_end; + + p = allocate_pages((aligned_new_end - aligned_end + LARGE_PAGE_SIZE) + >> PAGE_SHIFT, 0); + + p_aligned = ((unsigned long)p + (LARGE_PAGE_SIZE - 1)) & LARGE_PAGE_MASK; + + if (p_aligned > (unsigned long)p) { + free_pages(p, (p_aligned - (unsigned long)p) >> PAGE_SHIFT); + } + + add_process_memory_range(proc, aligned_end, aligned_new_end, + virt_to_phys((void *)p_aligned), VR_IO_NOCACHE); + + dkprintf("largePTE area(no cache): 0x%lX - 0x%lX (s: %lu) -> 0x%lX - \n", + aligned_end, aligned_new_end, + (aligned_new_end - aligned_end), + virt_to_phys((void *)p_aligned)); + + return address; + } +#endif + + p = allocate_pages((aligned_new_end - aligned_end) >> PAGE_SHIFT, 0); + + // kprintf("process.c,p=%lx\n", p); + if (!p) { + // kprintf("process.c,p==0\n"); + return end; + } + + add_process_memory_range(proc, aligned_end, aligned_new_end, + virt_to_phys(p), VR_IO_NOCACHE); + dkprintf("extend area(no cache): 0x%lX - 0x%lX (s: %lu) -> 0x%lX - \n", + aligned_end, aligned_new_end, + (aligned_new_end - aligned_end), + virt_to_phys(p)); + + + return address; +} +#endif + +// Original version retained because dcfa (src/mccmd/client/ibmic/main.c) calls this int remove_process_region(struct process *proc, unsigned long start, unsigned long end) { diff --git a/kernel/syscall.c b/kernel/syscall.c index b74aa9b8..5a4eafb8 100644 --- a/kernel/syscall.c +++ b/kernel/syscall.c @@ -25,7 +25,7 @@ #define SYSCALL_BY_IKC -#define DEBUG_PRINT_SC +//#define DEBUG_PRINT_SC #ifdef DEBUG_PRINT_SC #define dkprintf kprintf @@ -435,12 +435,27 @@ SYSCALL_DECLARE(mmap) kprintf("syscall.c,!MAP_FIXED,MAP_ANONYMOUS\n"); unsigned long flags = aal_mc_spinlock_lock(&cpu_local_var(current)->vm->memory_range_lock); unsigned long s = (region->map_end + PAGE_SIZE - 1) & PAGE_MASK; + unsigned long map_end_aligned = region->map_end; unsigned long len = (aal_mc_syscall_arg1(ctx) + PAGE_SIZE - 1) & PAGE_MASK; - region->map_end = - extend_process_region(cpu_local_var(current), - region->map_start, - region->map_end, - s + len); + dkprintf("SC(%d),syscall.c,mmap,len=%lx", cpuid, len); + +#ifdef USE_NOCACHE_MMAP + if ((aal_mc_syscall_arg3(ctx) & 0x40) == 0x40) { + dkprintf("SC(%d),syscall.c,mmap,nocache,len=%lx\n", cpuid, len); + region->map_end = extend_process_nocache_region( + cpu_local_var(current), region->map_start, map_end_aligned, + s + len); + } + else +#endif + { + region->map_end = + extend_process_region(cpu_local_var(current), + region->map_start, + map_end_aligned, + s + len); + } + aal_mc_spinlock_unlock(&cpu_local_var(current)->vm->memory_range_lock, flags); dkprintf("syscall.c,mmap,map_end=%lx,s+len=%lx\n", region->map_end, s+len); #ifdef USE_LARGE_PAGES