1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2011 NetApp, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 31 #include <sys/param.h> 32 #include <sys/kernel.h> 33 #include <sys/systm.h> 34 #include <sys/malloc.h> 35 36 #include <machine/machdep.h> 37 #include <machine/vmm.h> 38 #include "vmm_stat.h" 39 40 /* 41 * 'vst_num_elems' is the total number of addressable statistic elements 42 * 'vst_num_types' is the number of unique statistic types 43 * 44 * It is always true that 'vst_num_elems' is greater than or equal to 45 * 'vst_num_types'. This is because a stat type may represent more than 46 * one element (for e.g. VMM_STAT_ARRAY). 47 */ 48 static int vst_num_elems, vst_num_types; 49 static struct vmm_stat_type *vsttab[MAX_VMM_STAT_ELEMS]; 50 51 static MALLOC_DEFINE(M_VMM_STAT, "vmm stat", "vmm stat"); 52 53 #define vst_size ((size_t)vst_num_elems * sizeof(uint64_t)) 54 55 void 56 vmm_stat_register(void *arg) 57 { 58 struct vmm_stat_type *vst = arg; 59 60 /* We require all stats to identify themselves with a description */ 61 if (vst->desc == NULL) 62 return; 63 64 if (vst_num_elems + vst->nelems >= MAX_VMM_STAT_ELEMS) { 65 printf("Cannot accommodate vmm stat type \"%s\"!\n", vst->desc); 66 return; 67 } 68 69 vst->index = vst_num_elems; 70 vst_num_elems += vst->nelems; 71 72 vsttab[vst_num_types++] = vst; 73 } 74 75 int 76 vmm_stat_copy(struct vcpu *vcpu, int index, int count, int *num_stats, 77 uint64_t *buf) 78 { 79 struct vmm_stat_type *vst; 80 uint64_t *stats; 81 int i, tocopy; 82 83 if (index < 0 || count < 0) 84 return (EINVAL); 85 86 if (index > vst_num_elems) 87 return (ENOENT); 88 89 if (index == vst_num_elems) { 90 *num_stats = 0; 91 return (0); 92 } 93 94 tocopy = min(vst_num_elems - index, count); 95 96 /* Let stats functions update their counters */ 97 for (i = 0; i < vst_num_types; i++) { 98 vst = vsttab[i]; 99 if (vst->func != NULL) 100 (*vst->func)(vcpu, vst); 101 } 102 103 /* Copy over the stats */ 104 stats = vcpu_stats(vcpu); 105 memcpy(buf, stats + index, tocopy * sizeof(stats[0])); 106 *num_stats = tocopy; 107 return (0); 108 } 109 110 void * 111 vmm_stat_alloc(void) 112 { 113 114 return (malloc(vst_size, M_VMM_STAT, M_WAITOK)); 115 } 116 117 void 118 vmm_stat_init(void *vp) 119 { 120 121 bzero(vp, vst_size); 122 } 123 124 void 125 vmm_stat_free(void *vp) 126 { 127 free(vp, M_VMM_STAT); 128 } 129 130 int 131 vmm_stat_desc_copy(int index, char *buf, int bufsize) 132 { 133 int i; 134 struct vmm_stat_type *vst; 135 136 for (i = 0; i < vst_num_types; i++) { 137 vst = vsttab[i]; 138 if (index >= vst->index && index < vst->index + vst->nelems) { 139 if (vst->nelems > 1) { 140 snprintf(buf, bufsize, "%s[%d]", 141 vst->desc, index - vst->index); 142 } else { 143 strlcpy(buf, vst->desc, bufsize); 144 } 145 return (0); /* found it */ 146 } 147 } 148 149 return (EINVAL); 150 } 151 152 /* global statistics */ 153 VMM_STAT(VMEXIT_COUNT, "total number of vm exits"); 154 VMM_STAT(VMEXIT_UNKNOWN, "number of vmexits for the unknown exception"); 155 VMM_STAT(VMEXIT_WFI, "number of times wfi was intercepted"); 156 VMM_STAT(VMEXIT_WFE, "number of times wfe was intercepted"); 157 VMM_STAT(VMEXIT_HVC, "number of times hvc was intercepted"); 158 VMM_STAT(VMEXIT_MSR, "number of times msr/mrs was intercepted"); 159 VMM_STAT(VMEXIT_DATA_ABORT, "number of vmexits for a data abort"); 160 VMM_STAT(VMEXIT_INSN_ABORT, "number of vmexits for an instruction abort"); 161 VMM_STAT(VMEXIT_UNHANDLED_SYNC, "number of vmexits for an unhandled synchronous exception"); 162 VMM_STAT(VMEXIT_IRQ, "number of vmexits for an irq"); 163 VMM_STAT(VMEXIT_FIQ, "number of vmexits for an interrupt"); 164 VMM_STAT(VMEXIT_UNHANDLED_EL2, "number of vmexits for an unhandled EL2 exception"); 165 VMM_STAT(VMEXIT_UNHANDLED, "number of vmexits for an unhandled exception"); 166