include: reduce default stack size
[minix.git] / servers / vm / utility.c
blobb7d19c111c25076b8c4db82da6645f37b8205bf3
2 /* This file contains some utility routines for VM. */
4 #define _SYSTEM 1
6 #define _MINIX 1 /* To get the brk() prototype (as _brk()). */
8 #include <minix/callnr.h>
9 #include <minix/com.h>
10 #include <minix/config.h>
11 #include <minix/const.h>
12 #include <minix/ds.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>
21 #include <string.h>
22 #include <errno.h>
23 #include <env.h>
24 #include <unistd.h>
25 #include <assert.h>
26 #include <sys/param.h>
27 #include <sys/mman.h>
29 #include "proto.h"
30 #include "glo.h"
31 #include "util.h"
32 #include "region.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 /*===========================================================================*
42 * get_mem_chunks *
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;
51 int i;
52 struct memory *memp;
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;
63 limit = base + size;
64 base = (phys_bytes) (CLICK_CEIL(base));
65 limit = (phys_bytes) (CLICK_FLOOR(limit));
66 if (limit <= base) {
67 memp->base = memp->size = 0;
68 } else {
69 memp->base = base >> CLICK_SHIFT;
70 memp->size = (limit - base) >> CLICK_SHIFT;
75 /*===========================================================================*
76 * vm_isokendpt *
77 *===========================================================================*/
78 int vm_isokendpt(endpoint_t endpoint, int *proc)
80 *proc = _ENDPOINT_P(endpoint);
81 if(*proc < 0 || *proc >= NR_PROCS)
82 return EINVAL;
83 if(*proc >= 0 && endpoint != vmproc[*proc].vm_endpoint)
84 return EDEADEPT;
85 if(*proc >= 0 && !(vmproc[*proc].vm_flags & VMF_INUSE))
86 return EDEADEPT;
87 return OK;
91 /*===========================================================================*
92 * do_info *
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];
99 struct vmproc *vmp;
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)
104 return EINVAL;
105 vmp = &vmproc[pr];
107 ptr = (vir_bytes) m->VMI_PTR;
109 switch(m->VMI_WHAT) {
110 case VMIW_STATS:
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;
120 size = sizeof(vsi);
122 break;
124 case VMIW_USAGE:
125 if(m->VMI_EP < 0)
126 get_usage_info_kernel(&vui);
127 else if (vm_isokendpt(m->VMI_EP, &pr) != OK)
128 return EINVAL;
129 else get_usage_info(&vmproc[pr], &vui);
131 addr = (vir_bytes) &vui;
132 size = sizeof(vui);
134 break;
136 case VMIW_REGION:
137 if (vm_isokendpt(m->VMI_EP, &pr) != OK)
138 return EINVAL;
140 count = MIN(m->VMI_COUNT, MAX_VRI_COUNT);
141 next = m->VMI_NEXT;
143 count = get_region_info(&vmproc[pr], vri, count, &next);
145 m->VMI_COUNT = count;
146 m->VMI_NEXT = next;
148 addr = (vir_bytes) vri;
149 size = sizeof(vri[0]) * count;
151 break;
153 default:
154 return EINVAL;
157 if (size == 0)
158 return OK;
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
170 * operation.
172 return sys_datacopy(SELF, addr,
173 (vir_bytes) vmp->vm_endpoint, ptr, size);
176 /*===========================================================================*
177 * swap_proc_slot *
178 *===========================================================================*/
179 int swap_proc_slot(struct vmproc *src_vmp, struct vmproc *dst_vmp)
181 struct vmproc orig_src_vmproc, orig_dst_vmproc;
183 #if LU_DEBUG
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);
187 #endif
189 /* Save existing data. */
190 orig_src_vmproc = *src_vmp;
191 orig_dst_vmproc = *dst_vmp;
193 /* Swap slots. */
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;
203 #if LU_DEBUG
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);
207 #endif
209 return OK;
212 /*===========================================================================*
213 * swap_proc_dyn_data *
214 *===========================================================================*/
215 int swap_proc_dyn_data(struct vmproc *src_vmp, struct vmproc *dst_vmp)
217 int is_vm;
218 int r;
220 is_vm = (dst_vmp->vm_endpoint == VM_PROC_NR);
222 /* For VM, transfer memory regions above the stack first. */
223 if(is_vm) {
224 #if LU_DEBUG
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);
227 #endif
228 r = pt_map_in_range(src_vmp, dst_vmp, VM_STACKTOP, 0);
229 if(r != OK) {
230 printf("swap_proc_dyn_data: pt_map_in_range failed\n");
231 return r;
235 #if LU_DEBUG
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);
239 #endif
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.
250 if(!is_vm) {
251 struct vir_region *vr;
252 vr = map_lookup(dst_vmp, VM_STACKTOP, NULL);
253 if(vr && !map_lookup(src_vmp, VM_STACKTOP, NULL)) {
254 #if LU_DEBUG
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);
257 #endif
258 r = map_proc_copy_from(src_vmp, dst_vmp, vr);
259 if(r != OK) {
260 return r;
265 return OK;
268 void *minix_mmap(void *addr, size_t len, int f, int f2, int f3, off_t o)
270 void *ret;
271 phys_bytes p;
273 assert(!addr);
274 assert(!(len % VM_PAGE_SIZE));
276 ret = vm_allocpages(&p, VMP_SLAB, len/VM_PAGE_SIZE);
278 if(!ret) return MAP_FAILED;
279 memset(ret, 0, len);
280 return ret;
283 int minix_munmap(void * addr, size_t len)
285 vm_freepages((vir_bytes) addr, roundup(len, VM_PAGE_SIZE)/VM_PAGE_SIZE);
286 return 0;
289 int _brk(void *addr)
291 vir_bytes target = roundup((vir_bytes)addr, VM_PAGE_SIZE), v;
292 extern char _end;
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;
298 v += VM_PAGE_SIZE) {
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);
306 return -1;
308 prevbrk = v + VM_PAGE_SIZE;
311 _brksize = (char *) addr;
313 if(sys_vmctl(SELF, VMCTL_FLUSHTLB, 0) != OK)
314 panic("flushtlb failed");
316 return 0;