4 #include <minix/callnr.h>
6 #include <minix/config.h>
7 #include <minix/const.h>
9 #include <minix/endpoint.h>
10 #include <minix/minlib.h>
11 #include <minix/type.h>
12 #include <minix/ipc.h>
13 #include <minix/sysutil.h>
14 #include <minix/syslib.h>
15 #include <minix/safecopies.h>
16 #include <minix/bitmap.h>
17 #include <minix/vfsif.h>
19 #include <machine/vmparam.h>
41 endpoint_t caller
; /* KERNEL or process? if NONE, no callback */
42 endpoint_t requestor
; /* on behalf of whom? */
43 int transid
; /* VFS transaction id if valid */
44 struct vmproc
*vmp
; /* target address space */
45 vir_bytes mem
, len
; /* memory range */
46 int wrflag
; /* must it be writable or not */
47 int valid
; /* sanity check */
48 int vfs_avail
; /* may vfs be called to satisfy this range? */
52 static void handle_memory_continue(struct vmproc
*vmp
, message
*m
,
53 void *arg
, void *statearg
);
54 static int handle_memory_step(struct hm_state
*hmstate
);
55 static void handle_memory_final(struct hm_state
*state
, int result
);
57 /*===========================================================================*
59 *===========================================================================*/
60 char *pf_errstr(u32_t err
)
64 sprintf(buf
, "err 0x%lx ", (long)err
);
65 if(PFERR_NOPAGE(err
)) strcat(buf
, "nopage ");
66 if(PFERR_PROT(err
)) strcat(buf
, "protection ");
67 if(PFERR_WRITE(err
)) strcat(buf
, "write");
68 if(PFERR_READ(err
)) strcat(buf
, "read");
73 static void pf_cont(struct vmproc
*vmp
, message
*m
, void *arg
, void *statearg
);
75 static void handle_memory_continue(struct vmproc
*vmp
, message
*m
, void *arg
, void *statearg
);
77 static void handle_pagefault(endpoint_t ep
, vir_bytes addr
, u32_t err
, int retry
)
81 struct vir_region
*region
;
83 int p
, wr
= PFERR_WRITE(err
);
86 if(vm_isokendpt(ep
, &p
) != OK
)
87 panic("handle_pagefault: endpoint wrong: %d", ep
);
90 assert(vmp
->vm_flags
& VMF_INUSE
);
92 /* See if address is valid at all. */
93 if(!(region
= map_lookup(vmp
, addr
, NULL
))) {
95 printf("VM: pagefault: SIGSEGV %d protected addr 0x%lx; %s\n",
96 ep
, addr
, pf_errstr(err
));
98 assert(PFERR_NOPAGE(err
));
99 printf("VM: pagefault: SIGSEGV %d bad addr 0x%lx; %s\n",
100 ep
, addr
, pf_errstr(err
));
101 sys_diagctl_stacktrace(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
);
110 /* If process was writing, see if it's writable. */
111 if(!(region
->flags
& VR_WRITABLE
) && wr
) {
112 printf("VM: pagefault: SIGSEGV %d ro map 0x%lx %s\n",
113 ep
, addr
, pf_errstr(err
));
114 if((s
=sys_kill(vmp
->vm_endpoint
, SIGSEGV
)) != OK
)
115 panic("sys_kill failed: %d", s
);
116 if((s
=sys_vmctl(ep
, VMCTL_CLEAR_PAGEFAULT
, 0 /*unused*/)) != OK
)
117 panic("do_pagefaults: sys_vmctl failed: %d", ep
);
121 assert(addr
>= region
->vaddr
);
122 offset
= addr
- region
->vaddr
;
124 /* Access is allowed; handle it. */
126 result
= map_pf(vmp
, region
, offset
, wr
, NULL
, NULL
, 0, &io
);
127 assert(result
!= SUSPEND
);
129 struct pf_state state
;
133 result
= map_pf(vmp
, region
, offset
, wr
, pf_cont
,
134 &state
, sizeof(state
), &io
);
137 vmp
->vm_major_page_fault
++;
139 vmp
->vm_minor_page_fault
++;
141 if(result
== SUSPEND
) {
146 printf("VM: pagefault: SIGSEGV %d pagefault not handled\n", ep
);
147 if((s
=sys_kill(ep
, SIGSEGV
)) != OK
)
148 panic("sys_kill failed: %d", s
);
149 if((s
=sys_vmctl(ep
, VMCTL_CLEAR_PAGEFAULT
, 0 /*unused*/)) != OK
)
150 panic("do_pagefaults: sys_vmctl failed: %d", ep
);
156 /* Pagefault is handled, so now reactivate the process. */
157 if((s
=sys_vmctl(ep
, VMCTL_CLEAR_PAGEFAULT
, 0 /*unused*/)) != OK
)
158 panic("do_pagefaults: sys_vmctl failed: %d", ep
);
162 static void pf_cont(struct vmproc
*vmp
, message
*m
,
163 void *arg
, void *statearg
)
165 struct pf_state
*state
= statearg
;
167 if(vm_isokendpt(state
->ep
, &p
) != OK
) return; /* signal */
168 handle_pagefault(state
->ep
, state
->vaddr
, state
->err
, 1);
171 static void handle_memory_continue(struct vmproc
*vmp
, message
*m
,
172 void *arg
, void *statearg
)
175 struct hm_state
*state
= statearg
;
177 assert(state
->caller
!= NONE
);
178 assert(state
->valid
== VALID
);
180 if(m
->VMV_RESULT
!= OK
) {
181 printf("VM: handle_memory_continue: vfs request failed\n");
182 handle_memory_final(state
, m
->VMV_RESULT
);
186 r
= handle_memory_step(state
);
188 assert(state
->valid
== VALID
);
194 assert(state
->valid
== VALID
);
196 handle_memory_final(state
, r
);
199 static void handle_memory_final(struct hm_state
*state
, int result
)
204 assert(state
->valid
== VALID
);
206 if(state
->caller
== KERNEL
) {
207 if((r
=sys_vmctl(state
->requestor
, VMCTL_MEMREQ_REPLY
, result
)) != OK
)
208 panic("handle_memory_continue: sys_vmctl failed: %d", r
);
209 } else if(state
->caller
!= NONE
) {
210 /* Send a reply msg */
212 memset(&msg
, 0, sizeof(msg
));
215 if(IS_VFS_FS_TRANSID(state
->transid
)) {
216 assert(state
->caller
== VFS_PROC_NR
);
217 /* If a transaction ID was set, reset it */
218 msg
.m_type
= TRNS_ADD_ID(msg
.m_type
, state
->transid
);
221 if(asynsend3(state
->caller
, &msg
, 0) != OK
) {
222 panic("handle_memory_final: asynsend3 failed");
225 assert(state
->valid
== VALID
);
227 /* fail fast if anyone tries to access this state again */
228 memset(state
, 0, sizeof(*state
));
232 /*===========================================================================*
234 *===========================================================================*/
235 void do_pagefaults(message
*m
)
237 handle_pagefault(m
->m_source
, m
->VPF_ADDR
, m
->VPF_FLAGS
, 0);
240 int handle_memory_once(struct vmproc
*vmp
, vir_bytes mem
, vir_bytes len
,
244 r
= handle_memory_start(vmp
, mem
, len
, wrflag
, NONE
, NONE
, 0, 0);
245 assert(r
!= SUSPEND
);
249 int handle_memory_start(struct vmproc
*vmp
, vir_bytes mem
, vir_bytes len
,
250 int wrflag
, endpoint_t caller
, endpoint_t requestor
, int transid
,
254 struct hm_state state
;
257 if((o
= mem
% PAGE_SIZE
)) {
262 len
= roundup(len
, PAGE_SIZE
);
267 state
.wrflag
= wrflag
;
268 state
.requestor
= requestor
;
269 state
.caller
= caller
;
270 state
.transid
= transid
;
272 state
.vfs_avail
= vfs_avail
;
274 r
= handle_memory_step(&state
);
277 assert(caller
!= NONE
);
280 handle_memory_final(&state
, r
);
286 /*===========================================================================*
288 *===========================================================================*/
291 endpoint_t who
, who_s
, requestor
;
292 vir_bytes mem
, mem_s
;
300 r
= sys_vmctl_get_memreq(&who
, &mem
, &len
, &wrflag
, &who_s
,
309 if(vm_isokendpt(who
, &p
) != OK
)
310 panic("do_memory: bad endpoint: %d", who
);
313 assert(!IS_VFS_FS_TRANSID(transid
));
315 /* is VFS blocked? */
316 if(requestor
== VFS_PROC_NR
) vfs_avail
= 0;
319 handle_memory_start(vmp
, mem
, len
, wrflag
,
320 KERNEL
, requestor
, transid
, vfs_avail
);
331 static int handle_memory_step(struct hm_state
*hmstate
)
333 struct vir_region
*region
;
335 /* Page-align memory and length. */
337 assert(hmstate
->valid
== VALID
);
338 assert(!(hmstate
->mem
% VM_PAGE_SIZE
));
339 assert(!(hmstate
->len
% VM_PAGE_SIZE
));
341 while(hmstate
->len
> 0) {
343 if(!(region
= map_lookup(hmstate
->vmp
, hmstate
->mem
, NULL
))) {
345 map_printmap(hmstate
->vmp
);
346 printf("VM: do_memory: memory doesn't exist\n");
349 } else if(!(region
->flags
& VR_WRITABLE
) && hmstate
->wrflag
) {
351 printf("VM: do_memory: write to unwritable map\n");
355 vir_bytes offset
, sublen
;
356 assert(region
->vaddr
<= hmstate
->mem
);
357 assert(!(region
->vaddr
% VM_PAGE_SIZE
));
358 offset
= hmstate
->mem
- region
->vaddr
;
359 sublen
= hmstate
->len
;
360 if(offset
+ sublen
> region
->length
)
361 sublen
= region
->length
- offset
;
363 if((region
->def_memtype
== &mem_type_mappedfile
&&
364 !hmstate
->vfs_avail
) || hmstate
->caller
== NONE
) {
365 r
= map_handle_memory(hmstate
->vmp
, region
, offset
,
366 sublen
, hmstate
->wrflag
, NULL
, NULL
, 0);
367 assert(r
!= SUSPEND
);
369 r
= map_handle_memory(hmstate
->vmp
, region
, offset
,
370 sublen
, hmstate
->wrflag
, handle_memory_continue
,
371 hmstate
, sizeof(*hmstate
));
374 if(r
!= OK
) return r
;
376 hmstate
->len
-= sublen
;
377 hmstate
->mem
+= sublen
;