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>
26 #include "sanitycheck.h"
29 /*===========================================================================*
31 *===========================================================================*/
32 int do_fork(message
*msg
)
34 int r
, proc
, childproc
;
35 struct vmproc
*vmp
, *vmc
;
39 SANITYCHECK(SCL_FUNCTIONS
);
41 if(vm_isokendpt(msg
->VMF_ENDPOINT
, &proc
) != OK
) {
42 printf("VM: bogus endpoint VM_FORK %d\n", msg
->VMF_ENDPOINT
);
43 SANITYCHECK(SCL_FUNCTIONS
);
47 childproc
= msg
->VMF_SLOTNO
;
48 if(childproc
< 0 || childproc
>= NR_PROCS
) {
49 printf("VM: bogus slotno VM_FORK %d\n", msg
->VMF_SLOTNO
);
50 SANITYCHECK(SCL_FUNCTIONS
);
54 vmp
= &vmproc
[proc
]; /* parent */
55 vmc
= &vmproc
[childproc
]; /* child */
56 assert(vmc
->vm_slot
== childproc
);
58 /* The child is basically a copy of the parent. */
61 vmc
->vm_slot
= childproc
;
62 region_init(&vmc
->vm_regions_avl
);
63 vmc
->vm_endpoint
= NONE
; /* In case someone tries to use it. */
67 vmc
->vm_bytecopies
= 0;
70 if(pt_new(&vmc
->vm_pt
) != OK
) {
74 SANITYCHECK(SCL_DETAIL
);
76 if(map_proc_copy(vmc
, vmp
) != OK
) {
77 printf("VM: fork: map_proc_copy failed\n");
82 /* Only inherit these flags. */
83 vmc
->vm_flags
&= VMF_INUSE
;
88 /* Tell kernel about the (now successful) FORK. */
89 if((r
=sys_fork(vmp
->vm_endpoint
, childproc
,
90 &vmc
->vm_endpoint
, PFF_VMINHIBIT
, &msgaddr
)) != OK
) {
91 panic("do_fork can't sys_fork: %d", r
);
94 if((r
=pt_bind(&vmc
->vm_pt
, vmc
)) != OK
)
95 panic("fork can't pt_bind: %d", r
);
99 /* making these messages writable is an optimisation
100 * and its return value needn't be checked.
103 if (handle_memory_once(vmc
, vir
, sizeof(message
), 1) != OK
)
104 panic("do_fork: handle_memory for child failed\n");
106 if (handle_memory_once(vmp
, vir
, sizeof(message
), 1) != OK
)
107 panic("do_fork: handle_memory for parent failed\n");
110 /* Inform caller of new child endpoint. */
111 msg
->VMF_CHILD_ENDPOINT
= vmc
->vm_endpoint
;
113 SANITYCHECK(SCL_FUNCTIONS
);