4 #include <minix/callnr.h>
6 #include <minix/config.h>
7 #include <minix/const.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>
28 #include "sanitycheck.h"
32 /*===========================================================================*
34 *===========================================================================*/
35 PUBLIC
int do_fork(message
*msg
)
37 int r
, proc
, s
, childproc
, fullvm
;
38 struct vmproc
*vmp
, *vmc
;
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
);
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
);
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
);
66 fullvm
= vmp
->vm_flags
& VMF_HASPT
;
68 /* The child is basically a copy of the parent. */
71 vmc
->vm_slot
= childproc
;
72 vmc
->vm_regions
= NULL
;
73 vmc
->vm_endpoint
= NONE
; /* In case someone tries to use it. */
75 vmc
->vm_flags
&= ~VMF_HASPT
;
78 vmc
->vm_bytecopies
= 0;
81 if(pt_new(&vmc
->vm_pt
) != OK
) {
82 printf("VM: fork: pt_new failed\n");
86 vmc
->vm_flags
|= VMF_HASPT
;
89 SANITYCHECK(SCL_DETAIL
);
91 if(map_proc_copy(vmc
, vmp
) != OK
) {
92 printf("VM: fork: map_proc_copy failed\n");
98 vmc
->vm_heap
= map_region_lookup_tag(vmc
, VRT_HEAP
);
102 SANITYCHECK(SCL_DETAIL
);
105 struct vir_region
*heap
, *stack
;
106 vir_bytes text_bytes
, data_bytes
, stack_bytes
, parent_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",
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",
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");
150 if(!(heap
= map_region_lookup_tag(vmc
, VRT_HEAP
)))
151 panic("couldn't lookup heap");
153 if(!(stack
= map_region_lookup_tag(vmc
, VRT_STACK
)))
154 panic("couldn't lookup stack");
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");
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
);
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
);