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