From 3121eec6bd835dc5320fbf0d7282a02bb09b0a2c Mon Sep 17 00:00:00 2001 From: Ben Gras Date: Thu, 18 Dec 2008 15:35:22 +0000 Subject: [PATCH] . map text (kernel's and processes') in readonly . map kernel in non-user . don't map in first pages of kernel code and data if possible these first pages could actually be freed but as the kernel isn't allowed to touch them either we can't reuse them until VM has totally taken over page table management and kernel doesn't rely on identity mapping any more. --- servers/vm/alloc.c | 12 ++++-------- servers/vm/exec.c | 2 +- servers/vm/glo.h | 2 -- servers/vm/i386/pagetable.c | 4 ++-- servers/vm/main.c | 17 +++++++++++++++++ servers/vm/vm.h | 2 +- 6 files changed, 25 insertions(+), 14 deletions(-) diff --git a/servers/vm/alloc.c b/servers/vm/alloc.c index fe97cf39b..e28f4574d 100644 --- a/servers/vm/alloc.c +++ b/servers/vm/alloc.c @@ -426,9 +426,7 @@ PRIVATE PUBLIC phys_bytes alloc_pages(int pages, int memflags) #define ALLOCRETURNCHECK \ availbytes(&avail2, &chunks2); \ vm_assert(avail1 - bytes == avail2); \ - vm_assert(chunks1 == chunks2 || chunks1-1 == chunks2); \ - if(verbosealloc) \ - printf("memory: 0x%lx bytes in %d chunks\n", avail2, chunks2); + vm_assert(chunks1 == chunks2 || chunks1-1 == chunks2); #else #define ALLOCRETURNCHECK #endif @@ -493,11 +491,9 @@ PRIVATE PUBLIC void free_pages(phys_bytes pageno, int npages) #if SANITYCHECKS #define FREERETURNCHECK \ - availbytes(&avail2, &chunks2); \ - vm_assert(avail1 + origsize == avail2); \ - vm_assert(chunks1 == chunks2 || chunks1+1 == chunks2 || chunks1-1 == chunks2); \ - if(verbosealloc) \ - printf("memory: 0x%lx bytes in %d chunks\n", avail2, chunks2); + availbytes(&avail2, &chunks2); \ + vm_assert(avail1 + origsize == avail2); \ + vm_assert(chunks1 == chunks2 || chunks1+1 == chunks2 || chunks1-1 == chunks2); #else #define FREERETURNCHECK #endif diff --git a/servers/vm/exec.c b/servers/vm/exec.c index 06d514b04..e486f1bef 100644 --- a/servers/vm/exec.c +++ b/servers/vm/exec.c @@ -361,7 +361,7 @@ PUBLIC int proc_new(struct vmproc *vmp, vmp->vm_offset = vstart; /* page mapping flags for code */ -#define TEXTFLAGS (PTF_PRESENT | PTF_USER | PTF_WRITE) +#define TEXTFLAGS (PTF_PRESENT | PTF_USER) SANITYCHECK(SCL_DETAIL); if(text_bytes > 0) { if(!map_page_region(vmp, vstart, 0, text_bytes, diff --git a/servers/vm/glo.h b/servers/vm/glo.h index 2566830d8..065bee9c4 100644 --- a/servers/vm/glo.h +++ b/servers/vm/glo.h @@ -20,8 +20,6 @@ u32_t data1[200]; EXTERN long vm_sanitychecklevel; #endif -int verbosealloc; - #define VMP_SYSTEM _NR_PROCS /* vm operation mode state and values */ diff --git a/servers/vm/i386/pagetable.c b/servers/vm/i386/pagetable.c index e80a5e313..bda5f5c4c 100644 --- a/servers/vm/i386/pagetable.c +++ b/servers/vm/i386/pagetable.c @@ -781,12 +781,12 @@ PUBLIC int pt_mapkernel(pt_t *pt) /* Map in text. flags: don't write, supervisor only */ if((r=pt_writemap(pt, KERNEL_TEXT, KERNEL_TEXT, KERNEL_TEXT_LEN, - I386_VM_PRESENT | I386_VM_USER | I386_VM_WRITE, 0)) != OK) + I386_VM_PRESENT, 0)) != OK) return r; /* Map in data. flags: read-write, supervisor only */ if((r=pt_writemap(pt, KERNEL_DATA, KERNEL_DATA, KERNEL_DATA_LEN, - I386_VM_PRESENT | I386_VM_USER | I386_VM_WRITE, 0)) != OK) + I386_VM_PRESENT|I386_VM_WRITE, 0)) != OK) return r; return OK; diff --git a/servers/vm/main.c b/servers/vm/main.c index cec3b0fa2..624a5bb17 100644 --- a/servers/vm/main.c +++ b/servers/vm/main.c @@ -247,6 +247,23 @@ PRIVATE void vm_init(void) */ kernel_top_bytes = find_kernel_top(); + /* Can first kernel pages of code and data be (left) mapped out? + * If so, change the SYSTEM process' memory map to reflect this + * (future mappings of SYSTEM into other processes will not include + * first pages), and free the first pages. + */ + if(vm_paged && sys_vmctl(SELF, VMCTL_NOPAGEZERO, 0) == OK) { + struct vmproc *vmp; + vmp = &vmproc[VMP_SYSTEM]; + if(vmp->vm_arch.vm_seg[T].mem_len > 0) { +#define DIFF CLICKSPERPAGE + vmp->vm_arch.vm_seg[T].mem_phys += DIFF; + vmp->vm_arch.vm_seg[T].mem_len -= DIFF; + } + vmp->vm_arch.vm_seg[D].mem_phys += DIFF; + vmp->vm_arch.vm_seg[D].mem_len -= DIFF; + } + /* Give these processes their own page table. */ for (ip = &image[0]; ip < &image[NR_BOOT_PROCS]; ip++) { int s; diff --git a/servers/vm/vm.h b/servers/vm/vm.h index b305817b8..cdddf1539 100644 --- a/servers/vm/vm.h +++ b/servers/vm/vm.h @@ -12,7 +12,7 @@ #define ABS2CLICK(a) ((a) >> CLICK_SHIFT) /* Compile in asserts and custom sanity checks at all? */ -#define SANITYCHECKS 0 +#define SANITYCHECKS 1 #define VMSTATS 1 /* If so, this level: */ -- 2.44.0