vm: merge i386 and arm pagetable code
[minix.git] / servers / vm / pagefaults.c
blob97d9678da4d7290b2893b18df885a175acc06103
2 #define _SYSTEM 1
4 #include <minix/callnr.h>
5 #include <minix/com.h>
6 #include <minix/config.h>
7 #include <minix/const.h>
8 #include <minix/ds.h>
9 #include <minix/endpoint.h>
10 #include <minix/keymap.h>
11 #include <minix/minlib.h>
12 #include <minix/type.h>
13 #include <minix/ipc.h>
14 #include <minix/sysutil.h>
15 #include <minix/syslib.h>
16 #include <minix/safecopies.h>
17 #include <minix/bitmap.h>
19 #include <errno.h>
20 #include <string.h>
21 #include <env.h>
22 #include <stdio.h>
23 #include <fcntl.h>
24 #include <signal.h>
25 #include <assert.h>
27 #include "glo.h"
28 #include "proto.h"
29 #include "util.h"
30 #include "region.h"
32 /*===========================================================================*
33 * pf_errstr *
34 *===========================================================================*/
35 char *pf_errstr(u32_t err)
37 static char buf[100];
39 sprintf(buf, "err 0x%lx ", (long)err);
40 if(PFERR_NOPAGE(err)) strcat(buf, "nopage ");
41 if(PFERR_PROT(err)) strcat(buf, "protection ");
42 if(PFERR_WRITE(err)) strcat(buf, "write");
43 if(PFERR_READ(err)) strcat(buf, "read");
45 return buf;
48 /*===========================================================================*
49 * do_pagefaults *
50 *===========================================================================*/
51 void do_pagefaults(message *m)
53 endpoint_t ep = m->m_source;
54 u32_t addr = m->VPF_ADDR;
55 u32_t err = m->VPF_FLAGS;
56 struct vmproc *vmp;
57 int s;
59 struct vir_region *region;
60 vir_bytes offset;
61 int p, wr = PFERR_WRITE(err);
63 if(vm_isokendpt(ep, &p) != OK)
64 panic("do_pagefaults: endpoint wrong: %d", ep);
66 vmp = &vmproc[p];
67 assert(vmp->vm_flags & VMF_INUSE);
69 /* See if address is valid at all. */
70 if(!(region = map_lookup(vmp, addr, NULL))) {
71 if(PFERR_PROT(err)) {
72 printf("VM: pagefault: SIGSEGV %d protected addr 0x%x; %s\n",
73 ep, addr, pf_errstr(err));
74 } else {
75 assert(PFERR_NOPAGE(err));
76 printf("VM: pagefault: SIGSEGV %d bad addr 0x%x; %s\n",
77 ep, addr, pf_errstr(err));
79 if((s=sys_kill(vmp->vm_endpoint, SIGSEGV)) != OK)
80 panic("sys_kill failed: %d", s);
81 if((s=sys_vmctl(ep, VMCTL_CLEAR_PAGEFAULT, 0 /*unused*/)) != OK)
82 panic("do_pagefaults: sys_vmctl failed: %d", ep);
83 return;
86 /* If process was writing, see if it's writable. */
87 if(!(region->flags & VR_WRITABLE) && wr) {
88 printf("VM: pagefault: SIGSEGV %d ro map 0x%x %s\n",
89 ep, addr, pf_errstr(err));
90 if((s=sys_kill(vmp->vm_endpoint, SIGSEGV)) != OK)
91 panic("sys_kill failed: %d", s);
92 if((s=sys_vmctl(ep, VMCTL_CLEAR_PAGEFAULT, 0 /*unused*/)) != OK)
93 panic("do_pagefaults: sys_vmctl failed: %d", ep);
94 return;
97 assert(addr >= region->vaddr);
98 offset = addr - region->vaddr;
100 /* Access is allowed; handle it. */
101 if((map_pf(vmp, region, offset, wr)) != OK) {
102 printf("VM: pagefault: SIGSEGV %d pagefault not handled\n", ep);
103 if((s=sys_kill(vmp->vm_endpoint, SIGSEGV)) != OK)
104 panic("sys_kill failed: %d", s);
105 if((s=sys_vmctl(ep, VMCTL_CLEAR_PAGEFAULT, 0 /*unused*/)) != OK)
106 panic("do_pagefaults: sys_vmctl failed: %d", ep);
107 return;
110 /* Pagefault is handled, so now reactivate the process. */
111 if((s=sys_vmctl(ep, VMCTL_CLEAR_PAGEFAULT, 0 /*unused*/)) != OK)
112 panic("do_pagefaults: sys_vmctl failed: %d", ep);
115 /*===========================================================================*
116 * do_memory *
117 *===========================================================================*/
118 void do_memory(void)
120 endpoint_t who, who_s, requestor;
121 vir_bytes mem, mem_s;
122 vir_bytes len;
123 int wrflag;
125 while(1) {
126 int p, r = OK;
127 struct vmproc *vmp;
129 r = sys_vmctl_get_memreq(&who, &mem, &len, &wrflag, &who_s,
130 &mem_s, &requestor);
132 switch(r) {
133 case VMPTYPE_CHECK:
134 if(vm_isokendpt(who, &p) != OK)
135 panic("do_memory: bad endpoint: %d", who);
136 vmp = &vmproc[p];
138 r = handle_memory(vmp, mem, len, wrflag);
139 break;
140 default:
141 return;
144 if(sys_vmctl(requestor, VMCTL_MEMREQ_REPLY, r) != OK)
145 panic("do_memory: sys_vmctl failed: %d", r);
149 int handle_memory(struct vmproc *vmp, vir_bytes mem, vir_bytes len, int wrflag)
151 struct vir_region *region;
152 vir_bytes o;
154 /* Page-align memory and length. */
155 o = mem % VM_PAGE_SIZE;
156 mem -= o;
157 len += o;
158 o = len % VM_PAGE_SIZE;
159 if(o > 0) len += VM_PAGE_SIZE - o;
161 while(len > 0) {
162 int r;
163 if(!(region = map_lookup(vmp, mem, NULL))) {
164 #if VERBOSE
165 map_printmap(vmp);
166 printf("VM: do_memory: memory doesn't exist\n");
167 #endif
168 r = EFAULT;
169 } else if(!(region->flags & VR_WRITABLE) && wrflag) {
170 #if VERBOSE
171 printf("VM: do_memory: write to unwritable map\n");
172 #endif
173 r = EFAULT;
174 } else {
175 vir_bytes offset, sublen;
176 assert(region->vaddr <= mem);
177 assert(!(region->vaddr % VM_PAGE_SIZE));
178 offset = mem - region->vaddr;
179 sublen = len;
180 if(offset + sublen > region->length)
181 sublen = region->length - offset;
183 r = map_handle_memory(vmp, region, offset,
184 sublen, wrflag);
186 len -= sublen;
187 mem += sublen;
190 if(r != OK) {
191 #if VERBOSE
192 printf("VM: memory range 0x%lx-0x%lx not available in %d\n",
193 mem, mem+len, vmp->vm_endpoint);
194 #endif
195 return r;
199 return OK;