vm: merge i386 and arm pagetable code
[minix.git] / servers / vm / fork.c
blob39dcd9d8b4b7244bcb6fa855940835fc7e2fa162
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"
31 /*===========================================================================*
32 * do_fork *
33 *===========================================================================*/
34 int do_fork(message *msg)
36 int r, proc, childproc;
37 struct vmproc *vmp, *vmc;
38 pt_t origpt;
39 vir_bytes msgaddr;
41 SANITYCHECK(SCL_FUNCTIONS);
43 if(vm_isokendpt(msg->VMF_ENDPOINT, &proc) != OK) {
44 printf("VM: bogus endpoint VM_FORK %d\n", msg->VMF_ENDPOINT);
45 SANITYCHECK(SCL_FUNCTIONS);
46 return EINVAL;
49 childproc = msg->VMF_SLOTNO;
50 if(childproc < 0 || childproc >= NR_PROCS) {
51 printf("VM: bogus slotno VM_FORK %d\n", msg->VMF_SLOTNO);
52 SANITYCHECK(SCL_FUNCTIONS);
53 return EINVAL;
56 vmp = &vmproc[proc]; /* parent */
57 vmc = &vmproc[childproc]; /* child */
58 assert(vmc->vm_slot == childproc);
60 /* The child is basically a copy of the parent. */
61 origpt = vmc->vm_pt;
62 *vmc = *vmp;
63 vmc->vm_slot = childproc;
64 region_init(&vmc->vm_regions_avl);
65 vmc->vm_endpoint = NONE; /* In case someone tries to use it. */
66 vmc->vm_pt = origpt;
68 #if VMSTATS
69 vmc->vm_bytecopies = 0;
70 #endif
72 if(pt_new(&vmc->vm_pt) != OK) {
73 return ENOMEM;
76 SANITYCHECK(SCL_DETAIL);
78 if(map_proc_copy(vmc, vmp) != OK) {
79 printf("VM: fork: map_proc_copy failed\n");
80 pt_free(&vmc->vm_pt);
81 return(ENOMEM);
84 /* Only inherit these flags. */
85 vmc->vm_flags &= VMF_INUSE;
87 /* inherit the priv call bitmaps */
88 memcpy(&vmc->vm_call_mask, &vmp->vm_call_mask, sizeof(vmc->vm_call_mask));
90 /* Tell kernel about the (now successful) FORK. */
91 if((r=sys_fork(vmp->vm_endpoint, childproc,
92 &vmc->vm_endpoint, PFF_VMINHIBIT, &msgaddr)) != OK) {
93 panic("do_fork can't sys_fork: %d", r);
96 if((r=pt_bind(&vmc->vm_pt, vmc)) != OK)
97 panic("fork can't pt_bind: %d", r);
100 vir_bytes vir;
101 /* making these messages writable is an optimisation
102 * and its return value needn't be checked.
104 vir = msgaddr;
105 if (handle_memory(vmc, vir, sizeof(message), 1) != OK)
106 panic("do_fork: handle_memory for child failed\n");
107 vir = msgaddr;
108 if (handle_memory(vmp, vir, sizeof(message), 1) != OK)
109 panic("do_fork: handle_memory for parent failed\n");
112 /* Inform caller of new child endpoint. */
113 msg->VMF_CHILD_ENDPOINT = vmc->vm_endpoint;
115 SANITYCHECK(SCL_FUNCTIONS);
116 return OK;