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);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
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;
|
2009-12-08 14:35:52 +01:00
|
|
|
vir_bytes v;
|
2009-09-21 16:49:49 +02:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
2009-12-08 14:35:52 +01:00
|
|
|
while(len > 0) {
|
|
|
|
int r;
|
|
|
|
if(!(region = map_lookup(vmp, mem))) {
|
|
|
|
map_printmap(vmp);
|
|
|
|
printf("VM: do_memory: memory doesn't exist\n");
|
|
|
|
r = EFAULT;
|
|
|
|
} else if(!(region->flags & VR_WRITABLE) && wrflag) {
|
|
|
|
printf("VM: do_memory: write to unwritable map\n");
|
|
|
|
r = EFAULT;
|
|
|
|
} else {
|
|
|
|
vir_bytes offset, sublen;
|
|
|
|
vm_assert(region->vaddr <= mem);
|
|
|
|
vm_assert(!(region->flags & VR_NOPF));
|
|
|
|
vm_assert(!(region->vaddr % VM_PAGE_SIZE));
|
|
|
|
offset = mem - region->vaddr;
|
|
|
|
sublen = len;
|
|
|
|
if(offset + sublen > region->length)
|
|
|
|
sublen = region->length - offset;
|
|
|
|
|
|
|
|
r = map_handle_memory(vmp, region, offset,
|
|
|
|
sublen, wrflag);
|
|
|
|
|
|
|
|
len -= sublen;
|
|
|
|
mem += sublen;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
return r;
|
|
|
|
}
|
2009-09-21 16:49:49 +02:00
|
|
|
}
|
2009-11-22 14:06:18 +01:00
|
|
|
|
2009-12-08 14:35:52 +01:00
|
|
|
return OK;
|
2009-09-21 16:49:49 +02:00
|
|
|
}
|