phys addr arg of 0 must be possible for pt_writemap too (instead of meaning
[minix.git] / servers / vm / exec.c
blobe486f1befaa67098bf97facb6af94bc8069cba7e
2 #define _SYSTEM 1
4 #include <minix/callnr.h>
5 #include <minix/com.h>
6 #include <minix/config.h>
7 #include <minix/const.h>
8 #include <minix/ds.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/const.h>
18 #include <errno.h>
19 #include <assert.h>
20 #include <env.h>
21 #include <pagetable.h>
23 #include "glo.h"
24 #include "proto.h"
25 #include "util.h"
26 #include "vm.h"
27 #include "region.h"
28 #include "sanitycheck.h"
30 #include "memory.h"
32 FORWARD _PROTOTYPE( int new_mem, (struct vmproc *vmp, struct vmproc *sh_vmp,
33 vir_bytes text_bytes, vir_bytes data_bytes, vir_bytes bss_bytes,
34 vir_bytes stk_bytes, phys_bytes tot_bytes) );
36 /*===========================================================================*
37 * find_share *
38 *===========================================================================*/
39 PUBLIC struct vmproc *find_share(vmp_ign, ino, dev, ctime)
40 struct vmproc *vmp_ign; /* process that should not be looked at */
41 ino_t ino; /* parameters that uniquely identify a file */
42 dev_t dev;
43 time_t ctime;
45 /* Look for a process that is the file <ino, dev, ctime> in execution. Don't
46 * accidentally "find" vmp_ign, because it is the process on whose behalf this
47 * call is made.
49 struct vmproc *vmp;
50 for (vmp = &vmproc[0]; vmp < &vmproc[NR_PROCS]; vmp++) {
51 if (!(vmp->vm_flags & VMF_INUSE)) continue;
52 if (!(vmp->vm_flags & VMF_SEPARATE)) continue;
53 if (vmp->vm_flags & VMF_HASPT) continue;
54 if (vmp == vmp_ign) continue;
55 if (vmp->vm_ino != ino) continue;
56 if (vmp->vm_dev != dev) continue;
57 if (vmp->vm_ctime != ctime) continue;
58 return vmp;
60 return(NULL);
64 /*===========================================================================*
65 * exec_newmem *
66 *===========================================================================*/
67 PUBLIC int do_exec_newmem(message *msg)
69 int r, proc_e, proc_n;
70 vir_bytes stack_top;
71 vir_clicks tc, dc, sc, totc, dvir, s_vir;
72 struct vmproc *vmp, *sh_mp;
73 char *ptr;
74 struct exec_newmem args;
76 SANITYCHECK(SCL_FUNCTIONS);
78 proc_e= msg->VMEN_ENDPOINT;
79 if (vm_isokendpt(proc_e, &proc_n) != OK)
81 printf("VM:exec_newmem: bad endpoint %d from %d\n",
82 proc_e, msg->m_source);
83 return ESRCH;
85 vmp= &vmproc[proc_n];
86 ptr= msg->VMEN_ARGSPTR;
88 if(msg->VMEN_ARGSSIZE != sizeof(args)) {
89 printf("VM:exec_newmem: args size %d != %ld\n",
90 msg->VMEN_ARGSSIZE, sizeof(args));
91 return EINVAL;
93 SANITYCHECK(SCL_DETAIL);
95 r= sys_datacopy(msg->m_source, (vir_bytes)ptr,
96 SELF, (vir_bytes)&args, sizeof(args));
97 if (r != OK)
98 vm_panic("exec_newmem: sys_datacopy failed", r);
100 /* Check to see if segment sizes are feasible. */
101 tc = ((unsigned long) args.text_bytes + CLICK_SIZE - 1) >> CLICK_SHIFT;
102 dc = (args.data_bytes+args.bss_bytes + CLICK_SIZE - 1) >> CLICK_SHIFT;
103 totc = (args.tot_bytes + CLICK_SIZE - 1) >> CLICK_SHIFT;
104 sc = (args.args_bytes + CLICK_SIZE - 1) >> CLICK_SHIFT;
105 if (dc >= totc) return(ENOEXEC); /* stack must be at least 1 click */
107 dvir = (args.sep_id ? 0 : tc);
108 s_vir = dvir + (totc - sc);
109 r = (dvir + dc > s_vir) ? ENOMEM : OK;
110 if (r != OK)
111 return r;
113 /* Can the process' text be shared with that of one already running? */
114 if(!vm_paged) {
115 sh_mp = find_share(vmp, args.st_ino, args.st_dev, args.st_ctime);
116 } else {
117 sh_mp = NULL;
120 /* Allocate new memory and release old memory. Fix map and tell
121 * kernel.
123 r = new_mem(vmp, sh_mp, args.text_bytes, args.data_bytes,
124 args.bss_bytes, args.args_bytes, args.tot_bytes);
125 if (r != OK) return(r);
127 /* Save file identification to allow it to be shared. */
128 vmp->vm_ino = args.st_ino;
129 vmp->vm_dev = args.st_dev;
130 vmp->vm_ctime = args.st_ctime;
132 stack_top= ((vir_bytes)vmp->vm_arch.vm_seg[S].mem_vir << CLICK_SHIFT) +
133 ((vir_bytes)vmp->vm_arch.vm_seg[S].mem_len << CLICK_SHIFT);
135 /* set/clear separate I&D flag */
136 if (args.sep_id)
137 vmp->vm_flags |= VMF_SEPARATE;
138 else
139 vmp->vm_flags &= ~VMF_SEPARATE;
142 msg->VMEN_STACK_TOP = (void *) stack_top;
143 msg->VMEN_FLAGS = 0;
144 if (!sh_mp) /* Load text if sh_mp = NULL */
145 msg->VMEN_FLAGS |= EXC_NM_RF_LOAD_TEXT;
147 return OK;
150 /*===========================================================================*
151 * new_mem *
152 *===========================================================================*/
153 PRIVATE int new_mem(rmp, sh_mp, text_bytes, data_bytes,
154 bss_bytes,stk_bytes,tot_bytes)
155 struct vmproc *rmp; /* process to get a new memory map */
156 struct vmproc *sh_mp; /* text can be shared with this process */
157 vir_bytes text_bytes; /* text segment size in bytes */
158 vir_bytes data_bytes; /* size of initialized data in bytes */
159 vir_bytes bss_bytes; /* size of bss in bytes */
160 vir_bytes stk_bytes; /* size of initial stack segment in bytes */
161 phys_bytes tot_bytes; /* total memory to allocate, including gap */
163 /* Allocate new memory and release the old memory. Change the map and report
164 * the new map to the kernel. Zero the new core image's bss, gap and stack.
167 vir_clicks text_clicks, data_clicks, gap_clicks, stack_clicks, tot_clicks;
168 phys_bytes bytes, base, bss_offset;
169 int s, r2;
171 SANITYCHECK(SCL_FUNCTIONS);
173 /* No need to allocate text if it can be shared. */
174 if (sh_mp != NULL) {
175 text_bytes = 0;
176 vm_assert(!vm_paged);
179 /* Acquire the new memory. Each of the 4 parts: text, (data+bss), gap,
180 * and stack occupies an integral number of clicks, starting at click
181 * boundary. The data and bss parts are run together with no space.
183 text_clicks = ((unsigned long) text_bytes + CLICK_SIZE - 1) >> CLICK_SHIFT;
184 data_clicks = (data_bytes + bss_bytes + CLICK_SIZE - 1) >> CLICK_SHIFT;
185 stack_clicks = (stk_bytes + CLICK_SIZE - 1) >> CLICK_SHIFT;
186 tot_clicks = (tot_bytes + CLICK_SIZE - 1) >> CLICK_SHIFT;
187 gap_clicks = tot_clicks - data_clicks - stack_clicks;
188 if ( (int) gap_clicks < 0) return(ENOMEM);
190 SANITYCHECK(SCL_DETAIL);
193 /* We've got memory for the new core image. Release the old one. */
195 if(rmp->vm_flags & VMF_HASPT) {
196 /* Free page table and memory allocated by pagetable functions. */
197 rmp->vm_flags &= ~VMF_HASPT;
198 free_proc(rmp);
199 } else {
201 if (find_share(rmp, rmp->vm_ino, rmp->vm_dev, rmp->vm_ctime) == NULL) {
202 /* No other process shares the text segment, so free it. */
203 FREE_MEM(rmp->vm_arch.vm_seg[T].mem_phys, rmp->vm_arch.vm_seg[T].mem_len);
206 /* Free the data and stack segments. */
207 FREE_MEM(rmp->vm_arch.vm_seg[D].mem_phys,
208 rmp->vm_arch.vm_seg[S].mem_vir
209 + rmp->vm_arch.vm_seg[S].mem_len
210 - rmp->vm_arch.vm_seg[D].mem_vir);
213 /* We have now passed the point of no return. The old core image has been
214 * forever lost, memory for a new core image has been allocated. Set up
215 * and report new map.
218 if(vm_paged) {
219 if(pt_new(&rmp->vm_pt) != OK)
220 vm_panic("exec_newmem: no new pagetable", NO_NUM);
222 SANITYCHECK(SCL_DETAIL);
223 proc_new(rmp,
224 kernel_top_bytes, /* where to start the process in the page table */
225 CLICK2ABS(text_clicks),/* how big is the text in bytes, page-aligned */
226 CLICK2ABS(data_clicks),/* how big is data+bss, page-aligned */
227 CLICK2ABS(stack_clicks),/* how big is stack, page-aligned */
228 CLICK2ABS(gap_clicks), /* how big is gap, page-aligned */
229 0,0, /* not preallocated */
230 VM_STACKTOP /* regular stack top */
232 SANITYCHECK(SCL_DETAIL);
233 } else {
234 phys_clicks new_base;
236 new_base = ALLOC_MEM(text_clicks + tot_clicks, 0);
237 if (new_base == NO_MEM) return(ENOMEM);
239 if (sh_mp != NULL) {
240 /* Share the text segment. */
241 rmp->vm_arch.vm_seg[T] = sh_mp->vm_arch.vm_seg[T];
242 } else {
243 rmp->vm_arch.vm_seg[T].mem_phys = new_base;
244 rmp->vm_arch.vm_seg[T].mem_vir = 0;
245 rmp->vm_arch.vm_seg[T].mem_len = text_clicks;
247 if (text_clicks > 0)
249 /* Zero the last click of the text segment. Otherwise the
250 * part of that click may remain unchanged.
252 base = (phys_bytes)(new_base+text_clicks-1) << CLICK_SHIFT;
253 if ((s= sys_memset(0, base, CLICK_SIZE)) != OK)
254 vm_panic("new_mem: sys_memset failed", s);
258 /* No paging stuff. */
259 rmp->vm_flags &= ~VMF_HASPT;
260 rmp->vm_regions = NULL;
262 rmp->vm_arch.vm_seg[D].mem_phys = new_base + text_clicks;
263 rmp->vm_arch.vm_seg[D].mem_vir = 0;
264 rmp->vm_arch.vm_seg[D].mem_len = data_clicks;
265 rmp->vm_arch.vm_seg[S].mem_phys = rmp->vm_arch.vm_seg[D].mem_phys +
266 data_clicks + gap_clicks;
267 rmp->vm_arch.vm_seg[S].mem_vir = rmp->vm_arch.vm_seg[D].mem_vir +
268 data_clicks + gap_clicks;
269 rmp->vm_arch.vm_seg[S].mem_len = stack_clicks;
270 rmp->vm_stacktop =
271 CLICK2ABS(rmp->vm_arch.vm_seg[S].mem_vir +
272 rmp->vm_arch.vm_seg[S].mem_len);
274 rmp->vm_arch.vm_data_top =
275 (rmp->vm_arch.vm_seg[S].mem_vir +
276 rmp->vm_arch.vm_seg[S].mem_len) << CLICK_SHIFT;
278 if((r2=sys_newmap(rmp->vm_endpoint, rmp->vm_arch.vm_seg)) != OK) {
279 /* report new map to the kernel */
280 vm_panic("sys_newmap failed", r2);
283 /* Zero the bss, gap, and stack segment. */
284 bytes = (phys_bytes)(data_clicks + gap_clicks + stack_clicks) << CLICK_SHIFT;
285 base = (phys_bytes) rmp->vm_arch.vm_seg[D].mem_phys << CLICK_SHIFT;
286 bss_offset = (data_bytes >> CLICK_SHIFT) << CLICK_SHIFT;
287 base += bss_offset;
288 bytes -= bss_offset;
290 if ((s=sys_memset(0, base, bytes)) != OK) {
291 vm_panic("new_mem can't zero", s);
294 /* Tell kernel this thing has no page table. */
295 if((s=pt_bind(NULL, rmp)) != OK)
296 vm_panic("exec_newmem: pt_bind failed", s);
299 SANITYCHECK(SCL_FUNCTIONS);
301 return(OK);
304 /*===========================================================================*
305 * find_kernel_top *
306 *===========================================================================*/
307 PUBLIC phys_bytes find_kernel_top(void)
309 /* Find out where the kernel is, so we know where to start mapping
310 * user processes.
312 u32_t kernel_top = 0;
313 #define MEMTOP(v, i) \
314 (vmproc[v].vm_arch.vm_seg[i].mem_phys + vmproc[v].vm_arch.vm_seg[i].mem_len)
315 vm_assert(vmproc[VMP_SYSTEM].vm_flags & VMF_INUSE);
316 kernel_top = MEMTOP(VMP_SYSTEM, T);
317 kernel_top = MAX(kernel_top, MEMTOP(VMP_SYSTEM, D));
318 kernel_top = MAX(kernel_top, MEMTOP(VMP_SYSTEM, S));
319 vm_assert(kernel_top);
321 return CLICK2ABS(kernel_top);
324 /*===========================================================================*
325 * proc_new *
326 *===========================================================================*/
327 PUBLIC int proc_new(struct vmproc *vmp,
328 phys_bytes vstart, /* where to start the process in page table */
329 phys_bytes text_bytes, /* how much code, in bytes but page aligned */
330 phys_bytes data_bytes, /* how much data + bss, in bytes but page aligned */
331 phys_bytes stack_bytes, /* stack space to reserve, in bytes, page aligned */
332 phys_bytes gap_bytes, /* gap bytes, page aligned */
333 phys_bytes text_start, /* text starts here, if preallocated, otherwise 0 */
334 phys_bytes data_start, /* data starts here, if preallocated, otherwise 0 */
335 phys_bytes stacktop
338 int s;
339 vir_bytes hole_bytes;
340 int prealloc;
342 vm_assert(!(vstart % VM_PAGE_SIZE));
343 vm_assert(!(text_bytes % VM_PAGE_SIZE));
344 vm_assert(!(data_bytes % VM_PAGE_SIZE));
345 vm_assert(!(stack_bytes % VM_PAGE_SIZE));
346 vm_assert(!(gap_bytes % VM_PAGE_SIZE));
347 vm_assert(!(text_start % VM_PAGE_SIZE));
348 vm_assert(!(data_start % VM_PAGE_SIZE));
349 vm_assert((!text_start && !data_start) || (text_start && data_start));
351 if(!map_proc_kernel(vmp)) {
352 printf("VM: exec: map_proc_kernel failed\n");
353 return ENOMEM;
356 /* Place text at start of process. */
357 vmp->vm_arch.vm_seg[T].mem_phys = ABS2CLICK(vstart);
358 vmp->vm_arch.vm_seg[T].mem_vir = 0;
359 vmp->vm_arch.vm_seg[T].mem_len = ABS2CLICK(text_bytes);
361 vmp->vm_offset = vstart;
363 /* page mapping flags for code */
364 #define TEXTFLAGS (PTF_PRESENT | PTF_USER)
365 SANITYCHECK(SCL_DETAIL);
366 if(text_bytes > 0) {
367 if(!map_page_region(vmp, vstart, 0, text_bytes,
368 text_start ? text_start : MAP_NONE,
369 VR_ANON | VR_WRITABLE, text_start ? 0 : MF_PREALLOC)) {
370 SANITYCHECK(SCL_DETAIL);
371 printf("VM: proc_new: map_page_region failed (text)\n");
372 return(ENOMEM);
374 SANITYCHECK(SCL_DETAIL);
376 SANITYCHECK(SCL_DETAIL);
378 /* Allocate memory for data (including bss, but not including gap
379 * or stack), make sure it's cleared, and map it in after text
380 * (if any).
382 if(!(vmp->vm_heap = map_page_region(vmp, vstart + text_bytes, 0,
383 data_bytes, data_start ? data_start : MAP_NONE, VR_ANON | VR_WRITABLE,
384 data_start ? 0 : MF_PREALLOC))) {
385 printf("VM: exec: map_page_region for data failed\n");
386 return ENOMEM;
389 /* Tag the heap so brk() call knows which region to extend. */
390 map_region_set_tag(vmp->vm_heap, VRT_HEAP);
392 /* How many address space clicks between end of data
393 * and start of stack?
394 * stacktop is the first address after the stack, as addressed
395 * from within the user process.
397 hole_bytes = stacktop - data_bytes - stack_bytes - gap_bytes;
399 if(!map_page_region(vmp, vstart + text_bytes + data_bytes + hole_bytes,
400 0, stack_bytes + gap_bytes, MAP_NONE,
401 VR_ANON | VR_WRITABLE, 0) != OK) {
402 vm_panic("map_page_region failed for stack", NO_NUM);
405 vmp->vm_arch.vm_seg[D].mem_phys = ABS2CLICK(vstart + text_bytes);
406 vmp->vm_arch.vm_seg[D].mem_vir = 0;
407 vmp->vm_arch.vm_seg[D].mem_len = ABS2CLICK(data_bytes);
409 vmp->vm_arch.vm_seg[S].mem_phys = ABS2CLICK(vstart +
410 text_bytes + data_bytes + gap_bytes + hole_bytes);
411 vmp->vm_arch.vm_seg[S].mem_vir = ABS2CLICK(data_bytes + gap_bytes + hole_bytes);
413 /* Pretend the stack is the full size of the data segment, so
414 * we get a full-sized data segment, up to VM_DATATOP.
415 * After sys_newmap(), change the stack to what we know the
416 * stack to be (up to stacktop).
418 vmp->vm_arch.vm_seg[S].mem_len = (VM_DATATOP >> CLICK_SHIFT) -
419 vmp->vm_arch.vm_seg[S].mem_vir - ABS2CLICK(vstart) - ABS2CLICK(text_bytes);
421 /* Where are we allowed to start using the rest of the virtual
422 * address space?
424 vmp->vm_stacktop = stacktop;
426 /* What is the final size of the data segment in bytes? */
427 vmp->vm_arch.vm_data_top =
428 (vmp->vm_arch.vm_seg[S].mem_vir +
429 vmp->vm_arch.vm_seg[S].mem_len) << CLICK_SHIFT;
431 vmp->vm_flags |= VMF_HASPT;
433 if((s=sys_newmap(vmp->vm_endpoint, vmp->vm_arch.vm_seg)) != OK) {
434 vm_panic("sys_newmap (vm) failed", s);
438 /* This is the real stack clicks. */
439 vmp->vm_arch.vm_seg[S].mem_len = ABS2CLICK(stack_bytes);
441 if((s=pt_bind(&vmp->vm_pt, vmp)) != OK)
442 vm_panic("exec_newmem: pt_bind failed", s);
444 return OK;