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 if(vmp->vm_flags & VMF_VM_INSTANCE) { 79 vmp->vm_flags &= ~VMF_VM_INSTANCE; 80 num_vm_instances--; 81 } 82 83 { 84 /* Free pagetable and pages allocated by pt code. */ 85 SANITYCHECK(SCL_DETAIL); 86 free_proc(vmp); 87 SANITYCHECK(SCL_DETAIL); 88 } 89 SANITYCHECK(SCL_DETAIL); 90 91 /* Reset process slot fields. */ 92 clear_proc(vmp); 93 94 SANITYCHECK(SCL_FUNCTIONS); 95 return OK; 96 } 97 98 /*===========================================================================* 99 * do_willexit * 100 *===========================================================================*/ 101 int do_willexit(message *msg) 102 { 103 int proc; 104 struct vmproc *vmp; 105 106 if(vm_isokendpt(msg->VMWE_ENDPOINT, &proc) != OK) { 107 printf("VM: bogus endpoint VM_EXITING %d\n", 108 msg->VMWE_ENDPOINT); 109 return EINVAL; 110 } 111 vmp = &vmproc[proc]; 112 113 vmp->vm_flags |= VMF_EXITING; 114 115 return OK; 116 } 117 118 int do_procctl(message *msg, int transid) 119 { 120 endpoint_t proc; 121 struct vmproc *vmp; 122 123 if(vm_isokendpt(msg->VMPCTL_WHO, &proc) != OK) { 124 printf("VM: bogus endpoint VM_PROCCTL %ld\n", 125 msg->VMPCTL_WHO); 126 return EINVAL; 127 } 128 vmp = &vmproc[proc]; 129 130 switch(msg->VMPCTL_PARAM) { 131 case VMPPARAM_CLEAR: 132 if(msg->m_source != RS_PROC_NR 133 && msg->m_source != VFS_PROC_NR) 134 return EPERM; 135 free_proc(vmp); 136 if(pt_new(&vmp->vm_pt) != OK) 137 panic("VMPPARAM_CLEAR: pt_new failed"); 138 pt_bind(&vmp->vm_pt, vmp); 139 return OK; 140 case VMPPARAM_HANDLEMEM: 141 { 142 if(msg->m_source != VFS_PROC_NR) 143 return EPERM; 144 145 handle_memory_start(vmp, msg->VMPCTL_M1, 146 msg->VMPCTL_LEN, msg->VMPCTL_FLAGS, 147 VFS_PROC_NR, VFS_PROC_NR, transid, 1); 148 149 return SUSPEND; 150 } 151 default: 152 return EINVAL; 153 } 154 155 return OK; 156 } 157 158