4 #include <minix/callnr.h>
6 #include <minix/config.h>
7 #include <minix/const.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>
32 /*===========================================================================*
34 *===========================================================================*/
35 char *pf_errstr(u32_t err
)
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");
48 /*===========================================================================*
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
;
59 struct vir_region
*region
;
61 int p
, wr
= PFERR_WRITE(err
);
63 if(vm_isokendpt(ep
, &p
) != OK
)
64 panic("do_pagefaults: endpoint wrong: %d", ep
);
67 assert(vmp
->vm_flags
& VMF_INUSE
);
69 /* See if address is valid at all. */
70 if(!(region
= map_lookup(vmp
, addr
, NULL
))) {
72 printf("VM: pagefault: SIGSEGV %d protected addr 0x%x; %s\n",
73 ep
, addr
, pf_errstr(err
));
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
);
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
);
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
);
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 /*===========================================================================*
117 *===========================================================================*/
120 endpoint_t who
, who_s
, requestor
;
121 vir_bytes mem
, mem_s
;
129 r
= sys_vmctl_get_memreq(&who
, &mem
, &len
, &wrflag
, &who_s
,
134 if(vm_isokendpt(who
, &p
) != OK
)
135 panic("do_memory: bad endpoint: %d", who
);
138 r
= handle_memory(vmp
, mem
, len
, wrflag
);
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
;
154 /* Page-align memory and length. */
155 o
= mem
% VM_PAGE_SIZE
;
158 o
= len
% VM_PAGE_SIZE
;
159 if(o
> 0) len
+= VM_PAGE_SIZE
- o
;
163 if(!(region
= map_lookup(vmp
, mem
, NULL
))) {
166 printf("VM: do_memory: memory doesn't exist\n");
169 } else if(!(region
->flags
& VR_WRITABLE
) && wrflag
) {
171 printf("VM: do_memory: write to unwritable map\n");
175 vir_bytes offset
, sublen
;
176 assert(region
->vaddr
<= mem
);
177 assert(!(region
->vaddr
% VM_PAGE_SIZE
));
178 offset
= mem
- region
->vaddr
;
180 if(offset
+ sublen
> region
->length
)
181 sublen
= region
->length
- offset
;
183 r
= map_handle_memory(vmp
, region
, offset
,
192 printf("VM: memory range 0x%lx-0x%lx not available in %d\n",
193 mem
, mem
+len
, vmp
->vm_endpoint
);