vm, kernel, top: report memory usage of vm, kernel
[minix.git] / servers / vm / fork.c
blob16e4dd1160a3d65822b3fdd8a9c087d39d333c27
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"
30 #include "memory.h"
32 /*===========================================================================*
33 * do_fork *
34 *===========================================================================*/
35 int do_fork(message *msg)
37 int r, proc, childproc;
38 struct vmproc *vmp, *vmc;
39 pt_t origpt;
40 vir_bytes msgaddr;
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);
47 return EINVAL;
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);
54 return EINVAL;
57 vmp = &vmproc[proc]; /* parent */
58 vmc = &vmproc[childproc]; /* child */
59 assert(vmc->vm_slot == childproc);
61 /* The child is basically a copy of the parent. */
62 origpt = vmc->vm_pt;
63 *vmc = *vmp;
64 vmc->vm_slot = childproc;
65 region_init(&vmc->vm_regions_avl);
66 vmc->vm_endpoint = NONE; /* In case someone tries to use it. */
67 vmc->vm_pt = origpt;
69 #if VMSTATS
70 vmc->vm_bytecopies = 0;
71 #endif
73 if(pt_new(&vmc->vm_pt) != OK) {
74 printf("VM: fork: pt_new failed\n");
75 return ENOMEM;
78 SANITYCHECK(SCL_DETAIL);
80 if(map_proc_copy(vmc, vmp) != OK) {
81 printf("VM: fork: map_proc_copy failed\n");
82 pt_free(&vmc->vm_pt);
83 return(ENOMEM);
86 /* Only inherit these flags. */
87 vmc->vm_flags &= VMF_INUSE;
89 /* inherit the priv call bitmaps */
90 memcpy(&vmc->vm_call_mask, &vmp->vm_call_mask, sizeof(vmc->vm_call_mask));
92 /* Tell kernel about the (now successful) FORK. */
93 if((r=sys_fork(vmp->vm_endpoint, childproc,
94 &vmc->vm_endpoint, PFF_VMINHIBIT, &msgaddr)) != OK) {
95 panic("do_fork can't sys_fork: %d", r);
98 if((r=pt_bind(&vmc->vm_pt, vmc)) != OK)
99 panic("fork can't pt_bind: %d", r);
102 vir_bytes vir;
103 /* making these messages writable is an optimisation
104 * and its return value needn't be checked.
106 vir = msgaddr;
107 if (handle_memory(vmc, vir, sizeof(message), 1) != OK)
108 panic("do_fork: handle_memory for child failed\n");
109 vir = msgaddr;
110 if (handle_memory(vmp, vir, sizeof(message), 1) != OK)
111 panic("do_fork: handle_memory for parent failed\n");
114 /* Inform caller of new child endpoint. */
115 msg->VMF_CHILD_ENDPOINT = vmc->vm_endpoint;
117 SANITYCHECK(SCL_FUNCTIONS);
118 return OK;