xref: /minix/minix/servers/vm/exit.c (revision 7f5f010b)
1 
2 #define _SYSTEM 1
3 
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/bitmap.h>
16 
17 #include <errno.h>
18 #include <assert.h>
19 #include <env.h>
20 
21 #include "glo.h"
22 #include "proto.h"
23 #include "util.h"
24 #include "sanitycheck.h"
25 
26 static void reset_vm_rusage(struct vmproc *vmp)
27 {
28 	vmp->vm_total = 0;
29 	vmp->vm_total_max = 0;
30 	vmp->vm_minor_page_fault = 0;
31 	vmp->vm_major_page_fault = 0;
32 }
33 
34 void free_proc(struct vmproc *vmp)
35 {
36 	map_free_proc(vmp);
37 	pt_free(&vmp->vm_pt);
38 	region_init(&vmp->vm_regions_avl);
39 #if VMSTATS
40 	vmp->vm_bytecopies = 0;
41 #endif
42 	vmp->vm_region_top = 0;
43 	reset_vm_rusage(vmp);
44 }
45 
46 void clear_proc(struct vmproc *vmp)
47 {
48 	region_init(&vmp->vm_regions_avl);
49 	acl_clear(vmp);
50 	vmp->vm_flags = 0;		/* Clear INUSE, so slot is free. */
51 #if VMSTATS
52 	vmp->vm_bytecopies = 0;
53 #endif
54 	vmp->vm_region_top = 0;
55 	reset_vm_rusage(vmp);
56 }
57 
58 /*===========================================================================*
59  *				do_exit					     *
60  *===========================================================================*/
61 int do_exit(message *msg)
62 {
63 	int proc;
64 	struct vmproc *vmp;
65 
66 SANITYCHECK(SCL_FUNCTIONS);
67 
68 	if(vm_isokendpt(msg->VME_ENDPOINT, &proc) != OK) {
69 		printf("VM: bogus endpoint VM_EXIT %d\n", msg->VME_ENDPOINT);
70 		return EINVAL;
71 	}
72 	vmp = &vmproc[proc];
73 
74 	if(!(vmp->vm_flags & VMF_EXITING)) {
75 		printf("VM: unannounced VM_EXIT %d\n", msg->VME_ENDPOINT);
76 		return EINVAL;
77 	}
78 
79 	{
80 		/* Free pagetable and pages allocated by pt code. */
81 SANITYCHECK(SCL_DETAIL);
82 		free_proc(vmp);
83 SANITYCHECK(SCL_DETAIL);
84 	}
85 SANITYCHECK(SCL_DETAIL);
86 
87 	/* Reset process slot fields. */
88 	clear_proc(vmp);
89 
90 SANITYCHECK(SCL_FUNCTIONS);
91 	return OK;
92 }
93 
94 /*===========================================================================*
95  *				do_willexit				     *
96  *===========================================================================*/
97 int do_willexit(message *msg)
98 {
99 	int proc;
100 	struct vmproc *vmp;
101 
102 	if(vm_isokendpt(msg->VMWE_ENDPOINT, &proc) != OK) {
103 		printf("VM: bogus endpoint VM_EXITING %d\n",
104 			msg->VMWE_ENDPOINT);
105 		return EINVAL;
106 	}
107 	vmp = &vmproc[proc];
108 
109 	vmp->vm_flags |= VMF_EXITING;
110 
111 	return OK;
112 }
113 
114 int do_procctl(message *msg, int transid)
115 {
116 	endpoint_t proc;
117 	struct vmproc *vmp;
118 
119 	if(vm_isokendpt(msg->VMPCTL_WHO, &proc) != OK) {
120 		printf("VM: bogus endpoint VM_PROCCTL %ld\n",
121 			msg->VMPCTL_WHO);
122 		return EINVAL;
123 	}
124 	vmp = &vmproc[proc];
125 
126 	switch(msg->VMPCTL_PARAM) {
127 		case VMPPARAM_CLEAR:
128 			if(msg->m_source != RS_PROC_NR
129 				&& msg->m_source != VFS_PROC_NR)
130 				return EPERM;
131 			free_proc(vmp);
132 			if(pt_new(&vmp->vm_pt) != OK)
133 				panic("VMPPARAM_CLEAR: pt_new failed");
134 			pt_bind(&vmp->vm_pt, vmp);
135 			return OK;
136 		case VMPPARAM_HANDLEMEM:
137 		{
138 			if(msg->m_source != VFS_PROC_NR)
139 				return EPERM;
140 
141                         handle_memory_start(vmp, msg->VMPCTL_M1,
142 				msg->VMPCTL_LEN, msg->VMPCTL_FLAGS,
143                                 VFS_PROC_NR, VFS_PROC_NR, transid, 1);
144 
145 			return SUSPEND;
146 		}
147 		default:
148 			return EINVAL;
149 	}
150 
151 	return OK;
152 }
153 
154