make vfs & filesystems use failable copying
[minix3.git] / servers / vm / fork.c
blob806fe38a96c0c1ab33b7baba98e657ed7f66fa28
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/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>
18 #include <string.h>
19 #include <errno.h>
20 #include <env.h>
21 #include <assert.h>
23 #include "glo.h"
24 #include "vm.h"
25 #include "proto.h"
26 #include "util.h"
27 #include "sanitycheck.h"
28 #include "region.h"
30 /*===========================================================================*
31 * do_fork *
32 *===========================================================================*/
33 int do_fork(message *msg)
35 int r, proc, childproc;
36 struct vmproc *vmp, *vmc;
37 pt_t origpt;
38 vir_bytes msgaddr;
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);
45 return EINVAL;
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);
52 return EINVAL;
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. */
60 origpt = vmc->vm_pt;
61 *vmc = *vmp;
62 vmc->vm_slot = childproc;
63 region_init(&vmc->vm_regions_avl);
64 vmc->vm_endpoint = NONE; /* In case someone tries to use it. */
65 vmc->vm_pt = origpt;
67 #if VMSTATS
68 vmc->vm_bytecopies = 0;
69 #endif
71 if(pt_new(&vmc->vm_pt) != OK) {
72 return ENOMEM;
75 SANITYCHECK(SCL_DETAIL);
77 if(map_proc_copy(vmc, vmp) != OK) {
78 printf("VM: fork: map_proc_copy failed\n");
79 pt_free(&vmc->vm_pt);
80 return(ENOMEM);
83 /* Only inherit these flags. */
84 vmc->vm_flags &= VMF_INUSE;
86 /* Deal with ACLs. */
87 acl_fork(vmc);
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);
99 vir_bytes vir;
100 /* making these messages writable is an optimisation
101 * and its return value needn't be checked.
103 vir = msgaddr;
104 if (handle_memory_once(vmc, vir, sizeof(message), 1) != OK)
105 panic("do_fork: handle_memory for child failed\n");
106 vir = msgaddr;
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);
115 return OK;