2 /* This file contains some utility routines for VM. */
6 #define _MINIX 1 /* To get the brk() prototype (as _brk()). */
8 #include <minix/callnr.h>
10 #include <minix/config.h>
11 #include <minix/const.h>
13 #include <minix/endpoint.h>
14 #include <minix/minlib.h>
15 #include <minix/type.h>
16 #include <minix/ipc.h>
17 #include <minix/sysutil.h>
18 #include <minix/syslib.h>
19 #include <minix/type.h>
20 #include <minix/bitmap.h>
26 #include <sys/param.h>
33 #include "sanitycheck.h"
35 #include <machine/archtypes.h>
36 #include "kernel/const.h"
37 #include "kernel/config.h"
38 #include "kernel/type.h"
39 #include "kernel/proc.h"
41 /*===========================================================================*
43 *===========================================================================*/
44 void get_mem_chunks(mem_chunks
)
45 struct memory
*mem_chunks
; /* store mem chunks here */
47 /* Initialize the free memory list from the 'memory' boot variable. Translate
48 * the byte offsets and sizes in this list to clicks, properly truncated.
50 phys_bytes base
, size
, limit
;
54 /* Obtain and parse memory from system environment. */
55 if(env_memory_parse(mem_chunks
, NR_MEMS
) != OK
)
56 panic("couldn't obtain memory chunks");
58 /* Round physical memory to clicks. Round start up, round end down. */
59 for (i
= 0; i
< NR_MEMS
; i
++) {
60 memp
= &mem_chunks
[i
]; /* next mem chunk is stored here */
61 base
= mem_chunks
[i
].base
;
62 size
= mem_chunks
[i
].size
;
64 base
= (phys_bytes
) (CLICK_CEIL(base
));
65 limit
= (phys_bytes
) (CLICK_FLOOR(limit
));
67 memp
->base
= memp
->size
= 0;
69 memp
->base
= base
>> CLICK_SHIFT
;
70 memp
->size
= (limit
- base
) >> CLICK_SHIFT
;
75 /*===========================================================================*
77 *===========================================================================*/
78 int vm_isokendpt(endpoint_t endpoint
, int *proc
)
80 *proc
= _ENDPOINT_P(endpoint
);
81 if(*proc
< 0 || *proc
>= NR_PROCS
)
83 if(*proc
>= 0 && endpoint
!= vmproc
[*proc
].vm_endpoint
)
85 if(*proc
>= 0 && !(vmproc
[*proc
].vm_flags
& VMF_INUSE
))
91 /*===========================================================================*
93 *===========================================================================*/
94 int do_info(message
*m
)
96 struct vm_stats_info vsi
;
97 struct vm_usage_info vui
;
98 static struct vm_region_info vri
[MAX_VRI_COUNT
];
100 vir_bytes addr
, size
, next
, ptr
;
101 int r
, pr
, dummy
, count
, free_pages
, largest_contig
;
103 if (vm_isokendpt(m
->m_source
, &pr
) != OK
)
107 ptr
= (vir_bytes
) m
->VMI_PTR
;
109 switch(m
->VMI_WHAT
) {
111 vsi
.vsi_pagesize
= VM_PAGE_SIZE
;
112 vsi
.vsi_total
= total_pages
;
113 memstats(&dummy
, &free_pages
, &largest_contig
);
114 vsi
.vsi_free
= free_pages
;
115 vsi
.vsi_largest
= largest_contig
;
117 get_stats_info(&vsi
);
119 addr
= (vir_bytes
) &vsi
;
126 get_usage_info_kernel(&vui
);
127 else if (vm_isokendpt(m
->VMI_EP
, &pr
) != OK
)
129 else get_usage_info(&vmproc
[pr
], &vui
);
131 addr
= (vir_bytes
) &vui
;
137 if (vm_isokendpt(m
->VMI_EP
, &pr
) != OK
)
140 count
= MIN(m
->VMI_COUNT
, MAX_VRI_COUNT
);
143 count
= get_region_info(&vmproc
[pr
], vri
, count
, &next
);
145 m
->VMI_COUNT
= count
;
148 addr
= (vir_bytes
) vri
;
149 size
= sizeof(vri
[0]) * count
;
160 /* Make sure that no page faults can occur while copying out. A page
161 * fault would cause the kernel to send a notify to us, while we would
162 * be waiting for the result of the copy system call, resulting in a
163 * deadlock. Note that no memory mapping can be undone without the
164 * involvement of VM, so we are safe until we're done.
166 r
= handle_memory(vmp
, ptr
, size
, 1 /*wrflag*/);
167 if (r
!= OK
) return r
;
169 /* Now that we know the copy out will succeed, perform the actual copy
172 return sys_datacopy(SELF
, addr
,
173 (vir_bytes
) vmp
->vm_endpoint
, ptr
, size
);
176 /*===========================================================================*
178 *===========================================================================*/
179 int swap_proc_slot(struct vmproc
*src_vmp
, struct vmproc
*dst_vmp
)
181 struct vmproc orig_src_vmproc
, orig_dst_vmproc
;
184 printf("VM: swap_proc: swapping %d (%d) and %d (%d)\n",
185 src_vmp
->vm_endpoint
, src_vmp
->vm_slot
,
186 dst_vmp
->vm_endpoint
, dst_vmp
->vm_slot
);
189 /* Save existing data. */
190 orig_src_vmproc
= *src_vmp
;
191 orig_dst_vmproc
= *dst_vmp
;
194 *src_vmp
= orig_dst_vmproc
;
195 *dst_vmp
= orig_src_vmproc
;
197 /* Preserve endpoints and slot numbers. */
198 src_vmp
->vm_endpoint
= orig_src_vmproc
.vm_endpoint
;
199 src_vmp
->vm_slot
= orig_src_vmproc
.vm_slot
;
200 dst_vmp
->vm_endpoint
= orig_dst_vmproc
.vm_endpoint
;
201 dst_vmp
->vm_slot
= orig_dst_vmproc
.vm_slot
;
204 printf("VM: swap_proc: swapped %d (%d) and %d (%d)\n",
205 src_vmp
->vm_endpoint
, src_vmp
->vm_slot
,
206 dst_vmp
->vm_endpoint
, dst_vmp
->vm_slot
);
212 /*===========================================================================*
213 * swap_proc_dyn_data *
214 *===========================================================================*/
215 int swap_proc_dyn_data(struct vmproc
*src_vmp
, struct vmproc
*dst_vmp
)
220 is_vm
= (dst_vmp
->vm_endpoint
== VM_PROC_NR
);
222 /* For VM, transfer memory regions above the stack first. */
225 printf("VM: swap_proc_dyn_data: tranferring regions above the stack from old VM (%d) to new VM (%d)\n",
226 src_vmp
->vm_endpoint
, dst_vmp
->vm_endpoint
);
228 r
= pt_map_in_range(src_vmp
, dst_vmp
, VM_STACKTOP
, 0);
230 printf("swap_proc_dyn_data: pt_map_in_range failed\n");
236 printf("VM: swap_proc_dyn_data: swapping regions' parents for %d (%d) and %d (%d)\n",
237 src_vmp
->vm_endpoint
, src_vmp
->vm_slot
,
238 dst_vmp
->vm_endpoint
, dst_vmp
->vm_slot
);
241 /* Swap vir_regions' parents. */
242 map_setparent(src_vmp
);
243 map_setparent(dst_vmp
);
245 /* For regular processes, transfer regions above the stack now.
246 * In case of rollback, we need to skip this step. To sandbox the
247 * new instance and prevent state corruption on rollback, we share all
248 * the regions between the two instances as COW.
251 struct vir_region
*vr
;
252 vr
= map_lookup(dst_vmp
, VM_STACKTOP
, NULL
);
253 if(vr
&& !map_lookup(src_vmp
, VM_STACKTOP
, NULL
)) {
255 printf("VM: swap_proc_dyn_data: tranferring regions above the stack from %d to %d\n",
256 src_vmp
->vm_endpoint
, dst_vmp
->vm_endpoint
);
258 r
= map_proc_copy_from(src_vmp
, dst_vmp
, vr
);
268 void *minix_mmap(void *addr
, size_t len
, int f
, int f2
, int f3
, off_t o
)
274 assert(!(len
% VM_PAGE_SIZE
));
276 ret
= vm_allocpages(&p
, VMP_SLAB
, len
/VM_PAGE_SIZE
);
278 if(!ret
) return MAP_FAILED
;
283 int minix_munmap(void * addr
, size_t len
)
285 vm_freepages((vir_bytes
) addr
, roundup(len
, VM_PAGE_SIZE
)/VM_PAGE_SIZE
);
291 vir_bytes target
= roundup((vir_bytes
)addr
, VM_PAGE_SIZE
), v
;
293 extern char *_brksize
;
294 static vir_bytes prevbrk
= (vir_bytes
) &_end
;
295 struct vmproc
*vmprocess
= &vmproc
[VM_PROC_NR
];
297 for(v
= roundup(prevbrk
, VM_PAGE_SIZE
); v
< target
;
299 phys_bytes mem
, newpage
= alloc_mem(1, 0);
300 if(newpage
== NO_MEM
) return -1;
301 mem
= CLICK2ABS(newpage
);
302 if(pt_writemap(vmprocess
, &vmprocess
->vm_pt
,
303 v
, mem
, VM_PAGE_SIZE
,
304 ARCH_VM_PTE_PRESENT
| ARCH_VM_PTE_USER
| ARCH_VM_PTE_RW
, 0) != OK
) {
305 free_mem(newpage
, 1);
308 prevbrk
= v
+ VM_PAGE_SIZE
;
311 _brksize
= (char *) addr
;
313 if(sys_vmctl(SELF
, VMCTL_FLUSHTLB
, 0) != OK
)
314 panic("flushtlb failed");