1 /*- 2 * Copyright (c) 2008 Yahoo!, Inc. 3 * All rights reserved. 4 * Written by: John Baldwin <jhb@FreeBSD.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. Neither the name of the author nor the names of any co-contributors 15 * may be used to endorse or promote products derived from this software 16 * without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include <sys/param.h> 35 #include <sys/pcpu.h> 36 #include <sys/sysctl.h> 37 #include <kvm.h> 38 #include <limits.h> 39 #include <stdlib.h> 40 41 #include "kvm_private.h" 42 43 static struct nlist kvm_pcpu_nl[] = { 44 { "_cpuid_to_pcpu" }, 45 { "_mp_maxcpus" }, 46 { NULL }, 47 }; 48 49 /* 50 * Kernel per-CPU data state. We cache this stuff on the first 51 * access. 52 */ 53 static void **pcpu_data; 54 static int maxcpu; 55 56 #define NL_CPUID_TO_PCPU 0 57 #define NL_MP_MAXCPUS 1 58 59 static int 60 _kvm_pcpu_init(kvm_t *kd) 61 { 62 size_t len; 63 int max; 64 void *data; 65 66 if (kvm_nlist(kd, kvm_pcpu_nl) < 0) 67 return (-1); 68 if (kvm_pcpu_nl[NL_CPUID_TO_PCPU].n_value == 0) { 69 _kvm_err(kd, kd->program, "unable to find cpuid_to_pcpu"); 70 return (-1); 71 } 72 if (kvm_pcpu_nl[NL_MP_MAXCPUS].n_value == 0) { 73 _kvm_err(kd, kd->program, "unable to find mp_maxcpus"); 74 return (-1); 75 } 76 if (kvm_read(kd, kvm_pcpu_nl[NL_MP_MAXCPUS].n_value, &max, 77 sizeof(max)) != sizeof(max)) { 78 _kvm_err(kd, kd->program, "cannot read mp_maxcpus"); 79 return (-1); 80 } 81 len = max * sizeof(void *); 82 data = malloc(len); 83 if (data == NULL) { 84 _kvm_err(kd, kd->program, "out of memory"); 85 return (-1); 86 } 87 if (kvm_read(kd, kvm_pcpu_nl[NL_CPUID_TO_PCPU].n_value, data, len) != 88 len) { 89 _kvm_err(kd, kd->program, "cannot read cpuid_to_pcpu array"); 90 free(data); 91 return (-1); 92 } 93 pcpu_data = data; 94 maxcpu = max; 95 return (0); 96 } 97 98 static void 99 _kvm_pcpu_clear(void) 100 { 101 102 maxcpu = 0; 103 free(pcpu_data); 104 pcpu_data = NULL; 105 } 106 107 void * 108 kvm_getpcpu(kvm_t *kd, int cpu) 109 { 110 char *buf; 111 int i; 112 113 if (kd == NULL) { 114 _kvm_pcpu_clear(); 115 return (NULL); 116 } 117 118 if (maxcpu == 0) 119 if (_kvm_pcpu_init(kd) < 0) 120 return ((void *)-1); 121 122 if (cpu >= maxcpu || pcpu_data[cpu] == NULL) 123 return (NULL); 124 125 buf = malloc(sizeof(struct pcpu)); 126 if (buf == NULL) { 127 _kvm_err(kd, kd->program, "out of memory"); 128 return ((void *)-1); 129 } 130 if (kvm_read(kd, (uintptr_t)pcpu_data[cpu], buf, sizeof(struct pcpu)) != 131 sizeof(struct pcpu)) { 132 _kvm_err(kd, kd->program, "unable to read per-CPU data"); 133 free(buf); 134 return ((void *)-1); 135 } 136 return (buf); 137 } 138 139 int 140 kvm_getmaxcpu(kvm_t *kd) 141 { 142 143 if (kd == NULL) { 144 _kvm_pcpu_clear(); 145 return (0); 146 } 147 148 if (maxcpu == 0) 149 if (_kvm_pcpu_init(kd) < 0) 150 return (-1); 151 return (maxcpu); 152 } 153