2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
#define _SYSTEM 1
|
|
|
|
|
|
|
|
#include <minix/callnr.h>
|
|
|
|
#include <minix/com.h>
|
|
|
|
#include <minix/config.h>
|
|
|
|
#include <minix/const.h>
|
|
|
|
#include <minix/ds.h>
|
|
|
|
#include <minix/endpoint.h>
|
|
|
|
#include <minix/keymap.h>
|
|
|
|
#include <minix/minlib.h>
|
|
|
|
#include <minix/type.h>
|
|
|
|
#include <minix/ipc.h>
|
|
|
|
#include <minix/sysutil.h>
|
|
|
|
#include <minix/syslib.h>
|
|
|
|
#include <minix/safecopies.h>
|
2009-09-21 16:49:49 +02:00
|
|
|
#include <minix/bitmap.h>
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <env.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
#include <pagefaults.h>
|
|
|
|
|
|
|
|
#include "glo.h"
|
|
|
|
#include "proto.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "region.h"
|
|
|
|
|
2008-12-08 17:43:20 +01:00
|
|
|
/*===========================================================================*
|
|
|
|
* pf_errstr *
|
|
|
|
*===========================================================================*/
|
|
|
|
char *pf_errstr(u32_t err)
|
2008-11-19 13:26:10 +01:00
|
|
|
{
|
|
|
|
static char buf[100];
|
|
|
|
|
|
|
|
sprintf(buf, "err 0x%lx ", err);
|
|
|
|
if(PFERR_NOPAGE(err)) strcat(buf, "nopage ");
|
|
|
|
if(PFERR_PROT(err)) strcat(buf, "protection ");
|
|
|
|
if(PFERR_WRITE(err)) strcat(buf, "write");
|
|
|
|
if(PFERR_READ(err)) strcat(buf, "read");
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
2009-04-22 14:39:29 +02:00
|
|
|
* do_pagefaults *
|
2008-11-19 13:26:10 +01:00
|
|
|
*===========================================================================*/
|
2009-04-22 14:39:29 +02:00
|
|
|
PUBLIC void do_pagefaults(void)
|
2008-11-19 13:26:10 +01:00
|
|
|
{
|
|
|
|
endpoint_t ep;
|
|
|
|
u32_t addr, err;
|
|
|
|
struct vmproc *vmp;
|
|
|
|
int r, s;
|
|
|
|
|
|
|
|
while((r=arch_get_pagefault(&ep, &addr, &err)) == OK) {
|
|
|
|
struct vir_region *region;
|
|
|
|
vir_bytes offset;
|
|
|
|
int p, wr = PFERR_WRITE(err);
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
#if 0
|
|
|
|
printf("VM: pagefault: ep %d 0x%lx %s\n",
|
|
|
|
ep, arch_map2vir(vmp, addr), pf_errstr(err));
|
|
|
|
#endif
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
if(vm_isokendpt(ep, &p) != OK)
|
2009-04-22 14:39:29 +02:00
|
|
|
vm_panic("do_pagefaults: endpoint wrong", ep);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
vmp = &vmproc[p];
|
|
|
|
vm_assert(vmp->vm_flags & VMF_INUSE);
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
#if 0
|
|
|
|
map_printmap(vmp);
|
|
|
|
#endif
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
/* See if address is valid at all. */
|
|
|
|
if(!(region = map_lookup(vmp, addr))) {
|
|
|
|
vm_assert(PFERR_NOPAGE(err));
|
2008-12-16 15:33:53 +01:00
|
|
|
printf("VM: pagefault: SIGSEGV %d bad addr 0x%lx %s\n",
|
2008-12-08 17:43:20 +01:00
|
|
|
ep, arch_map2vir(vmp, addr), pf_errstr(err));
|
2009-01-27 13:54:33 +01:00
|
|
|
sys_sysctl_stacktrace(vmp->vm_endpoint);
|
2008-11-19 13:26:10 +01:00
|
|
|
if((s=sys_kill(vmp->vm_endpoint, SIGSEGV)) != OK)
|
|
|
|
vm_panic("sys_kill failed", s);
|
2009-09-21 16:49:49 +02:00
|
|
|
if((s=sys_vmctl(ep, VMCTL_CLEAR_PAGEFAULT, r)) != OK)
|
|
|
|
vm_panic("do_pagefaults: sys_vmctl failed", ep);
|
2008-11-19 13:26:10 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure this isn't a region that isn't supposed
|
|
|
|
* to cause pagefaults.
|
|
|
|
*/
|
|
|
|
vm_assert(!(region->flags & VR_NOPF));
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
/* We do not allow shared memory to cause pagefaults.
|
|
|
|
* These pages have to be pre-allocated.
|
|
|
|
*/
|
|
|
|
vm_assert(!(region->flags & VR_SHARED));
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
/* If process was writing, see if it's writable. */
|
|
|
|
if(!(region->flags & VR_WRITABLE) && wr) {
|
2008-12-16 15:33:53 +01:00
|
|
|
printf("VM: pagefault: SIGSEGV %d ro map 0x%lx %s\n",
|
2008-12-08 17:43:20 +01:00
|
|
|
ep, arch_map2vir(vmp, addr), pf_errstr(err));
|
2009-01-27 13:54:33 +01:00
|
|
|
sys_sysctl_stacktrace(vmp->vm_endpoint);
|
2008-11-19 13:26:10 +01:00
|
|
|
if((s=sys_kill(vmp->vm_endpoint, SIGSEGV)) != OK)
|
|
|
|
vm_panic("sys_kill failed", s);
|
2009-09-21 16:49:49 +02:00
|
|
|
if((s=sys_vmctl(ep, VMCTL_CLEAR_PAGEFAULT, r)) != OK)
|
|
|
|
vm_panic("do_pagefaults: sys_vmctl failed", ep);
|
2008-11-19 13:26:10 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-09-28 03:23:11 +02:00
|
|
|
vm_assert(addr >= region->vaddr);
|
2008-11-19 13:26:10 +01:00
|
|
|
offset = addr - region->vaddr;
|
|
|
|
|
|
|
|
/* Access is allowed; handle it. */
|
2009-04-22 14:39:29 +02:00
|
|
|
if((r=map_pf(vmp, region, offset, wr)) != OK) {
|
2008-12-16 15:33:53 +01:00
|
|
|
printf("VM: pagefault: SIGSEGV %d pagefault not handled\n", ep);
|
2009-01-27 13:54:33 +01:00
|
|
|
sys_sysctl_stacktrace(vmp->vm_endpoint);
|
2008-11-19 13:26:10 +01:00
|
|
|
if((s=sys_kill(vmp->vm_endpoint, SIGSEGV)) != OK)
|
|
|
|
vm_panic("sys_kill failed", s);
|
2009-09-21 16:49:49 +02:00
|
|
|
if((s=sys_vmctl(ep, VMCTL_CLEAR_PAGEFAULT, r)) != OK)
|
|
|
|
vm_panic("do_pagefaults: sys_vmctl failed", ep);
|
2008-11-19 13:26:10 +01:00
|
|
|
continue;
|
|
|
|
}
|
2009-09-21 16:49:49 +02:00
|
|
|
#if 0
|
|
|
|
printf("VM: map_pf done; ep %d 0x%lx %s\n",
|
|
|
|
ep, arch_map2vir(vmp, addr), pf_errstr(err));
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
printf("VM: handling pagefault OK: %d addr 0x%lx %s\n",
|
|
|
|
ep, arch_map2vir(vmp, addr), pf_errstr(err));
|
|
|
|
#endif
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
/* Pagefault is handled, so now reactivate the process. */
|
|
|
|
if((s=sys_vmctl(ep, VMCTL_CLEAR_PAGEFAULT, r)) != OK)
|
2009-04-22 14:39:29 +02:00
|
|
|
vm_panic("do_pagefaults: sys_vmctl failed", ep);
|
2009-09-21 16:49:49 +02:00
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
2009-04-22 14:39:29 +02:00
|
|
|
* do_memory *
|
2008-11-19 13:26:10 +01:00
|
|
|
*===========================================================================*/
|
2009-04-22 14:39:29 +02:00
|
|
|
PUBLIC void do_memory(void)
|
2008-11-19 13:26:10 +01:00
|
|
|
{
|
|
|
|
int r, s;
|
2009-09-21 16:49:49 +02:00
|
|
|
endpoint_t who, requestor;
|
2008-11-19 13:26:10 +01:00
|
|
|
vir_bytes mem;
|
|
|
|
vir_bytes len;
|
|
|
|
int wrflag;
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
while((r=sys_vmctl_get_memreq(&who, &mem, &len, &wrflag, &requestor))
|
|
|
|
== OK) {
|
2008-11-19 13:26:10 +01:00
|
|
|
int p, r = OK;
|
|
|
|
struct vmproc *vmp;
|
|
|
|
|
|
|
|
if(vm_isokendpt(who, &p) != OK)
|
2009-04-22 14:39:29 +02:00
|
|
|
vm_panic("do_memory: endpoint wrong", who);
|
2008-11-19 13:26:10 +01:00
|
|
|
vmp = &vmproc[p];
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
r = handle_memory(vmp, mem, len, wrflag);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
if(sys_vmctl(requestor, VMCTL_MEMREQ_REPLY, r) != OK)
|
2009-04-22 14:39:29 +02:00
|
|
|
vm_panic("do_memory: sys_vmctl failed", r);
|
2009-09-21 16:49:49 +02:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
printf("VM: handling memory request %d done OK\n",
|
|
|
|
who);
|
|
|
|
#endif
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
int handle_memory(struct vmproc *vmp, vir_bytes mem, vir_bytes len, int wrflag)
|
|
|
|
{
|
|
|
|
struct vir_region *region;
|
|
|
|
vir_bytes o;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
printf("VM: handling memory request: %d, 0x%lx-0x%lx, wr %d\n",
|
|
|
|
vmp->vm_endpoint, mem, mem+len, wrflag);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Page-align memory and length. */
|
|
|
|
o = mem % VM_PAGE_SIZE;
|
|
|
|
mem -= o;
|
|
|
|
len += o;
|
|
|
|
o = len % VM_PAGE_SIZE;
|
|
|
|
if(o > 0) len += VM_PAGE_SIZE - o;
|
|
|
|
|
|
|
|
if(!(region = map_lookup(vmp, mem))) {
|
|
|
|
map_printmap(vmp);
|
|
|
|
printf("VM: do_memory: memory doesn't exist\n");
|
|
|
|
r = EFAULT;
|
|
|
|
} else if(mem + len > region->vaddr + region->length) {
|
|
|
|
vm_assert(region->vaddr <= mem);
|
|
|
|
vm_panic("do_memory: not contained", NO_NUM);
|
|
|
|
} else if(!(region->flags & VR_WRITABLE) && wrflag) {
|
|
|
|
printf("VM: do_memory: write to unwritable map\n");
|
|
|
|
r = EFAULT;
|
|
|
|
} else {
|
|
|
|
vir_bytes offset;
|
|
|
|
vm_assert(region->vaddr <= mem);
|
|
|
|
vm_assert(!(region->flags & VR_NOPF));
|
|
|
|
vm_assert(!(region->vaddr % VM_PAGE_SIZE));
|
|
|
|
offset = mem - region->vaddr;
|
|
|
|
|
|
|
|
r = map_handle_memory(vmp, region, offset, len, wrflag);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(r != OK) {
|
|
|
|
printf("VM: memory range 0x%lx-0x%lx not available in %d\n",
|
|
|
|
arch_map2vir(vmp, mem), arch_map2vir(vmp, mem+len),
|
|
|
|
vmp->vm_endpoint);
|
|
|
|
}
|
|
|
|
}
|