2 /* This file contains some utility routines for VM. */
6 #include <minix/callnr.h>
8 #include <minix/config.h>
9 #include <minix/const.h>
11 #include <minix/endpoint.h>
12 #include <minix/minlib.h>
13 #include <minix/type.h>
14 #include <minix/ipc.h>
15 #include <minix/sysutil.h>
16 #include <minix/syslib.h>
17 #include <minix/type.h>
18 #include <minix/bitmap.h>
24 #include <sys/param.h>
31 #include "sanitycheck.h"
33 #include <machine/archtypes.h>
34 #include "kernel/const.h"
35 #include "kernel/config.h"
36 #include "kernel/type.h"
37 #include "kernel/proc.h"
39 /*===========================================================================*
41 *===========================================================================*/
42 void get_mem_chunks(mem_chunks
)
43 struct memory
*mem_chunks
; /* store mem chunks here */
45 /* Initialize the free memory list from the 'memory' boot variable. Translate
46 * the byte offsets and sizes in this list to clicks, properly truncated.
48 phys_bytes base
, size
, limit
;
52 /* Obtain and parse memory from system environment. */
53 if(env_memory_parse(mem_chunks
, NR_MEMS
) != OK
)
54 panic("couldn't obtain memory chunks");
56 /* Round physical memory to clicks. Round start up, round end down. */
57 for (i
= 0; i
< NR_MEMS
; i
++) {
58 memp
= &mem_chunks
[i
]; /* next mem chunk is stored here */
59 base
= mem_chunks
[i
].base
;
60 size
= mem_chunks
[i
].size
;
62 base
= (phys_bytes
) (CLICK_CEIL(base
));
63 limit
= (phys_bytes
) (CLICK_FLOOR(limit
));
65 memp
->base
= memp
->size
= 0;
67 memp
->base
= base
>> CLICK_SHIFT
;
68 memp
->size
= (limit
- base
) >> CLICK_SHIFT
;
73 /*===========================================================================*
75 *===========================================================================*/
76 int vm_isokendpt(endpoint_t endpoint
, int *proc
)
78 *proc
= _ENDPOINT_P(endpoint
);
79 if(*proc
< 0 || *proc
>= NR_PROCS
)
81 if(*proc
>= 0 && endpoint
!= vmproc
[*proc
].vm_endpoint
)
83 if(*proc
>= 0 && !(vmproc
[*proc
].vm_flags
& VMF_INUSE
))
89 /*===========================================================================*
91 *===========================================================================*/
92 int do_info(message
*m
)
94 struct vm_stats_info vsi
;
95 struct vm_usage_info vui
;
96 static struct vm_region_info vri
[MAX_VRI_COUNT
];
98 vir_bytes addr
, size
, next
, ptr
;
99 int r
, pr
, dummy
, count
, free_pages
, largest_contig
;
101 if (vm_isokendpt(m
->m_source
, &pr
) != OK
)
105 ptr
= (vir_bytes
) m
->VMI_PTR
;
107 switch(m
->VMI_WHAT
) {
109 vsi
.vsi_pagesize
= VM_PAGE_SIZE
;
110 vsi
.vsi_total
= total_pages
;
111 memstats(&dummy
, &free_pages
, &largest_contig
);
112 vsi
.vsi_free
= free_pages
;
113 vsi
.vsi_largest
= largest_contig
;
115 get_stats_info(&vsi
);
117 addr
= (vir_bytes
) &vsi
;
124 get_usage_info_kernel(&vui
);
125 else if (vm_isokendpt(m
->VMI_EP
, &pr
) != OK
)
127 else get_usage_info(&vmproc
[pr
], &vui
);
129 addr
= (vir_bytes
) &vui
;
135 if (vm_isokendpt(m
->VMI_EP
, &pr
) != OK
)
138 count
= MIN(m
->VMI_COUNT
, MAX_VRI_COUNT
);
141 count
= get_region_info(&vmproc
[pr
], vri
, count
, &next
);
143 m
->VMI_COUNT
= count
;
146 addr
= (vir_bytes
) vri
;
147 size
= sizeof(vri
[0]) * count
;
158 /* Make sure that no page faults can occur while copying out. A page
159 * fault would cause the kernel to send a notify to us, while we would
160 * be waiting for the result of the copy system call, resulting in a
161 * deadlock. Note that no memory mapping can be undone without the
162 * involvement of VM, so we are safe until we're done.
164 r
= handle_memory(vmp
, ptr
, size
, 1 /*wrflag*/);
165 if (r
!= OK
) return r
;
167 /* Now that we know the copy out will succeed, perform the actual copy
170 return sys_datacopy(SELF
, addr
,
171 (vir_bytes
) vmp
->vm_endpoint
, ptr
, size
);
174 /*===========================================================================*
176 *===========================================================================*/
177 int swap_proc_slot(struct vmproc
*src_vmp
, struct vmproc
*dst_vmp
)
179 struct vmproc orig_src_vmproc
, orig_dst_vmproc
;
182 printf("VM: swap_proc: swapping %d (%d) and %d (%d)\n",
183 src_vmp
->vm_endpoint
, src_vmp
->vm_slot
,
184 dst_vmp
->vm_endpoint
, dst_vmp
->vm_slot
);
187 /* Save existing data. */
188 orig_src_vmproc
= *src_vmp
;
189 orig_dst_vmproc
= *dst_vmp
;
192 *src_vmp
= orig_dst_vmproc
;
193 *dst_vmp
= orig_src_vmproc
;
195 /* Preserve endpoints and slot numbers. */
196 src_vmp
->vm_endpoint
= orig_src_vmproc
.vm_endpoint
;
197 src_vmp
->vm_slot
= orig_src_vmproc
.vm_slot
;
198 dst_vmp
->vm_endpoint
= orig_dst_vmproc
.vm_endpoint
;
199 dst_vmp
->vm_slot
= orig_dst_vmproc
.vm_slot
;
202 printf("VM: swap_proc: swapped %d (%d) and %d (%d)\n",
203 src_vmp
->vm_endpoint
, src_vmp
->vm_slot
,
204 dst_vmp
->vm_endpoint
, dst_vmp
->vm_slot
);
210 /*===========================================================================*
211 * swap_proc_dyn_data *
212 *===========================================================================*/
213 int swap_proc_dyn_data(struct vmproc
*src_vmp
, struct vmproc
*dst_vmp
)
218 is_vm
= (dst_vmp
->vm_endpoint
== VM_PROC_NR
);
220 /* For VM, transfer memory regions above the stack first. */
223 printf("VM: swap_proc_dyn_data: tranferring regions above the stack from old VM (%d) to new VM (%d)\n",
224 src_vmp
->vm_endpoint
, dst_vmp
->vm_endpoint
);
226 r
= pt_map_in_range(src_vmp
, dst_vmp
, VM_STACKTOP
, 0);
228 printf("swap_proc_dyn_data: pt_map_in_range failed\n");
234 printf("VM: swap_proc_dyn_data: swapping regions' parents for %d (%d) and %d (%d)\n",
235 src_vmp
->vm_endpoint
, src_vmp
->vm_slot
,
236 dst_vmp
->vm_endpoint
, dst_vmp
->vm_slot
);
239 /* Swap vir_regions' parents. */
240 map_setparent(src_vmp
);
241 map_setparent(dst_vmp
);
243 /* For regular processes, transfer regions above the stack now.
244 * In case of rollback, we need to skip this step. To sandbox the
245 * new instance and prevent state corruption on rollback, we share all
246 * the regions between the two instances as COW.
249 struct vir_region
*vr
;
250 vr
= map_lookup(dst_vmp
, VM_STACKTOP
, NULL
);
251 if(vr
&& !map_lookup(src_vmp
, VM_STACKTOP
, NULL
)) {
253 printf("VM: swap_proc_dyn_data: tranferring regions above the stack from %d to %d\n",
254 src_vmp
->vm_endpoint
, dst_vmp
->vm_endpoint
);
256 r
= map_proc_copy_from(src_vmp
, dst_vmp
, vr
);
266 void *minix_mmap(void *addr
, size_t len
, int f
, int f2
, int f3
, off_t o
)
272 assert(!(len
% VM_PAGE_SIZE
));
274 ret
= vm_allocpages(&p
, VMP_SLAB
, len
/VM_PAGE_SIZE
);
276 if(!ret
) return MAP_FAILED
;
281 int minix_munmap(void * addr
, size_t len
)
283 vm_freepages((vir_bytes
) addr
, roundup(len
, VM_PAGE_SIZE
)/VM_PAGE_SIZE
);
289 vir_bytes target
= roundup((vir_bytes
)addr
, VM_PAGE_SIZE
), v
;
291 extern char *_brksize
;
292 static vir_bytes prevbrk
= (vir_bytes
) &_end
;
293 struct vmproc
*vmprocess
= &vmproc
[VM_PROC_NR
];
295 for(v
= roundup(prevbrk
, VM_PAGE_SIZE
); v
< target
;
297 phys_bytes mem
, newpage
= alloc_mem(1, 0);
298 if(newpage
== NO_MEM
) return -1;
299 mem
= CLICK2ABS(newpage
);
300 if(pt_writemap(vmprocess
, &vmprocess
->vm_pt
,
301 v
, mem
, VM_PAGE_SIZE
,
309 free_mem(newpage
, 1);
312 prevbrk
= v
+ VM_PAGE_SIZE
;
315 _brksize
= (char *) addr
;
317 if(sys_vmctl(SELF
, VMCTL_FLUSHTLB
, 0) != OK
)
318 panic("flushtlb failed");