4 #include <minix/callnr.h>
6 #include <minix/config.h>
7 #include <minix/const.h>
9 #include <minix/endpoint.h>
10 #include <minix/minlib.h>
11 #include <minix/type.h>
12 #include <minix/ipc.h>
13 #include <minix/sysutil.h>
14 #include <minix/syslib.h>
15 #include <minix/debug.h>
16 #include <minix/bitmap.h>
27 #include "sanitycheck.h"
30 /*===========================================================================*
32 *===========================================================================*/
33 int do_fork(message
*msg
)
35 int r
, proc
, childproc
;
36 struct vmproc
*vmp
, *vmc
;
40 SANITYCHECK(SCL_FUNCTIONS
);
42 if(vm_isokendpt(msg
->VMF_ENDPOINT
, &proc
) != OK
) {
43 printf("VM: bogus endpoint VM_FORK %d\n", msg
->VMF_ENDPOINT
);
44 SANITYCHECK(SCL_FUNCTIONS
);
48 childproc
= msg
->VMF_SLOTNO
;
49 if(childproc
< 0 || childproc
>= NR_PROCS
) {
50 printf("VM: bogus slotno VM_FORK %d\n", msg
->VMF_SLOTNO
);
51 SANITYCHECK(SCL_FUNCTIONS
);
55 vmp
= &vmproc
[proc
]; /* parent */
56 vmc
= &vmproc
[childproc
]; /* child */
57 assert(vmc
->vm_slot
== childproc
);
59 /* The child is basically a copy of the parent. */
62 vmc
->vm_slot
= childproc
;
63 region_init(&vmc
->vm_regions_avl
);
64 vmc
->vm_endpoint
= NONE
; /* In case someone tries to use it. */
68 vmc
->vm_bytecopies
= 0;
71 if(pt_new(&vmc
->vm_pt
) != OK
) {
75 SANITYCHECK(SCL_DETAIL
);
77 if(map_proc_copy(vmc
, vmp
) != OK
) {
78 printf("VM: fork: map_proc_copy failed\n");
83 /* Only inherit these flags. */
84 vmc
->vm_flags
&= VMF_INUSE
;
89 /* Tell kernel about the (now successful) FORK. */
90 if((r
=sys_fork(vmp
->vm_endpoint
, childproc
,
91 &vmc
->vm_endpoint
, PFF_VMINHIBIT
, &msgaddr
)) != OK
) {
92 panic("do_fork can't sys_fork: %d", r
);
95 if((r
=pt_bind(&vmc
->vm_pt
, vmc
)) != OK
)
96 panic("fork can't pt_bind: %d", r
);
100 /* making these messages writable is an optimisation
101 * and its return value needn't be checked.
104 if (handle_memory_once(vmc
, vir
, sizeof(message
), 1) != OK
)
105 panic("do_fork: handle_memory for child failed\n");
107 if (handle_memory_once(vmp
, vir
, sizeof(message
), 1) != OK
)
108 panic("do_fork: handle_memory for parent failed\n");
111 /* Inform caller of new child endpoint. */
112 msg
->VMF_CHILD_ENDPOINT
= vmc
->vm_endpoint
;
114 SANITYCHECK(SCL_FUNCTIONS
);