vm: use assert() instead of vm_assert(); remove vm_assert().
[minix.git] / servers / vm / fork.c
blobaeafa0cbc6dff072fce48795c13f86f359872d5f
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/debug.h>
17 #include <minix/bitmap.h>
19 #include <string.h>
20 #include <errno.h>
21 #include <env.h>
22 #include <assert.h>
24 #include "glo.h"
25 #include "vm.h"
26 #include "proto.h"
27 #include "util.h"
28 #include "sanitycheck.h"
29 #include "region.h"
30 #include "memory.h"
32 /*===========================================================================*
33 * do_fork *
34 *===========================================================================*/
35 PUBLIC int do_fork(message *msg)
37 int r, proc, s, childproc, fullvm;
38 struct vmproc *vmp, *vmc;
39 pt_t origpt;
40 vir_bytes msgaddr;
42 SANITYCHECK(SCL_FUNCTIONS);
44 if(vm_isokendpt(msg->VMF_ENDPOINT, &proc) != OK) {
45 printf("VM: bogus endpoint VM_FORK %d\n", msg->VMF_ENDPOINT);
46 SANITYCHECK(SCL_FUNCTIONS);
47 return EINVAL;
50 childproc = msg->VMF_SLOTNO;
51 if(childproc < 0 || childproc >= NR_PROCS) {
52 printf("VM: bogus slotno VM_FORK %d\n", msg->VMF_SLOTNO);
53 SANITYCHECK(SCL_FUNCTIONS);
54 return EINVAL;
57 vmp = &vmproc[proc]; /* parent */
58 vmc = &vmproc[childproc]; /* child */
59 assert(vmc->vm_slot == childproc);
61 if(vmp->vm_flags & VMF_HAS_DMA) {
62 printf("VM: %d has DMA memory and may not fork\n", msg->VMF_ENDPOINT);
63 return EINVAL;
66 fullvm = vmp->vm_flags & VMF_HASPT;
68 /* The child is basically a copy of the parent. */
69 origpt = vmc->vm_pt;
70 *vmc = *vmp;
71 vmc->vm_slot = childproc;
72 vmc->vm_regions = NULL;
73 vmc->vm_endpoint = NONE; /* In case someone tries to use it. */
74 vmc->vm_pt = origpt;
75 vmc->vm_flags &= ~VMF_HASPT;
77 #if VMSTATS
78 vmc->vm_bytecopies = 0;
79 #endif
81 if(pt_new(&vmc->vm_pt) != OK) {
82 printf("VM: fork: pt_new failed\n");
83 return ENOMEM;
86 vmc->vm_flags |= VMF_HASPT;
88 if(fullvm) {
89 SANITYCHECK(SCL_DETAIL);
91 if(map_proc_copy(vmc, vmp) != OK) {
92 printf("VM: fork: map_proc_copy failed\n");
93 pt_free(&vmc->vm_pt);
94 return(ENOMEM);
97 if(vmp->vm_heap) {
98 vmc->vm_heap = map_region_lookup_tag(vmc, VRT_HEAP);
99 assert(vmc->vm_heap);
102 SANITYCHECK(SCL_DETAIL);
103 } else {
104 vir_bytes sp;
105 struct vir_region *heap, *stack;
106 vir_bytes text_bytes, data_bytes, stack_bytes, parent_gap_bytes,
107 child_gap_bytes;
109 /* Get SP of new process (using parent). */
110 if(get_stack_ptr(vmp->vm_endpoint, &sp) != OK) {
111 printf("VM: fork: get_stack_ptr failed for %d\n",
112 vmp->vm_endpoint);
113 return ENOMEM;
116 /* Update size of stack segment using current SP. */
117 if(adjust(vmp, vmp->vm_arch.vm_seg[D].mem_len, sp) != OK) {
118 printf("VM: fork: adjust failed for %d\n",
119 vmp->vm_endpoint);
120 return ENOMEM;
123 /* Copy newly adjust()ed stack segment size to child. */
124 vmc->vm_arch.vm_seg[S] = vmp->vm_arch.vm_seg[S];
126 text_bytes = CLICK2ABS(vmc->vm_arch.vm_seg[T].mem_len);
127 data_bytes = CLICK2ABS(vmc->vm_arch.vm_seg[D].mem_len);
128 stack_bytes = CLICK2ABS(vmc->vm_arch.vm_seg[S].mem_len);
130 /* how much space after break and before lower end (which is the
131 * logical top) of stack for the parent
133 parent_gap_bytes = CLICK2ABS(vmc->vm_arch.vm_seg[S].mem_vir -
134 vmc->vm_arch.vm_seg[D].mem_len);
136 /* how much space can the child stack grow downwards, below
137 * the current SP? The rest of the gap is available for the
138 * heap to grow upwards.
140 child_gap_bytes = VM_PAGE_SIZE;
142 if((r=proc_new(vmc, VM_PROCSTART,
143 text_bytes, data_bytes, stack_bytes, child_gap_bytes, 0, 0,
144 CLICK2ABS(vmc->vm_arch.vm_seg[S].mem_vir +
145 vmc->vm_arch.vm_seg[S].mem_len), 1)) != OK) {
146 printf("VM: fork: proc_new failed\n");
147 return r;
150 if(!(heap = map_region_lookup_tag(vmc, VRT_HEAP)))
151 panic("couldn't lookup heap");
152 assert(heap->phys);
153 if(!(stack = map_region_lookup_tag(vmc, VRT_STACK)))
154 panic("couldn't lookup stack");
155 assert(stack->phys);
157 /* Now copy the memory regions. */
159 if(vmc->vm_arch.vm_seg[T].mem_len > 0) {
160 struct vir_region *text;
161 if(!(text = map_region_lookup_tag(vmc, VRT_TEXT)))
162 panic("couldn't lookup text");
163 assert(text->phys);
164 if(copy_abs2region(CLICK2ABS(vmp->vm_arch.vm_seg[T].mem_phys),
165 text, 0, text_bytes) != OK)
166 panic("couldn't copy text");
169 if(copy_abs2region(CLICK2ABS(vmp->vm_arch.vm_seg[D].mem_phys),
170 heap, 0, data_bytes) != OK)
171 panic("couldn't copy heap");
173 if(copy_abs2region(CLICK2ABS(vmp->vm_arch.vm_seg[D].mem_phys +
174 vmc->vm_arch.vm_seg[D].mem_len) + parent_gap_bytes,
175 stack, child_gap_bytes, stack_bytes) != OK)
176 panic("couldn't copy stack");
179 /* Only inherit these flags. */
180 vmc->vm_flags &= (VMF_INUSE|VMF_SEPARATE|VMF_HASPT);
182 /* inherit the priv call bitmaps */
183 memcpy(&vmc->vm_call_mask, &vmp->vm_call_mask, sizeof(vmc->vm_call_mask));
185 /* Tell kernel about the (now successful) FORK. */
186 if((r=sys_fork(vmp->vm_endpoint, childproc,
187 &vmc->vm_endpoint, vmc->vm_arch.vm_seg,
188 PFF_VMINHIBIT, &msgaddr)) != OK) {
189 panic("do_fork can't sys_fork: %d", r);
192 if(fullvm) {
193 vir_bytes vir;
194 /* making these messages writable is an optimisation
195 * and its return value needn't be checked.
197 vir = arch_vir2map(vmc, msgaddr);
198 handle_memory(vmc, vir, sizeof(message), 1);
199 vir = arch_vir2map(vmp, msgaddr);
200 handle_memory(vmp, vir, sizeof(message), 1);
203 if((r=pt_bind(&vmc->vm_pt, vmc)) != OK)
204 panic("fork can't pt_bind: %d", r);
206 /* Inform caller of new child endpoint. */
207 msg->VMF_CHILD_ENDPOINT = vmc->vm_endpoint;
209 SANITYCHECK(SCL_FUNCTIONS);
210 return OK;