2 * Copyright (c) 2008 Yahoo!, Inc.
4 * Written by: John Baldwin <jhb@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
36 #include <sys/sysctl.h>
41 #include "kvm_private.h"
43 static struct nlist kvm_pcpu_nl[] = {
50 * Kernel per-CPU data state. We cache this stuff on the first
53 static void **pcpu_data;
56 #define NL_CPUID_TO_PCPU 0
57 #define NL_MP_MAXCPUS 1
60 _kvm_pcpu_init(kvm_t *kd)
66 if (kvm_nlist(kd, kvm_pcpu_nl) < 0)
68 if (kvm_pcpu_nl[NL_CPUID_TO_PCPU].n_value == 0) {
69 _kvm_err(kd, kd->program, "unable to find cpuid_to_pcpu");
72 if (kvm_pcpu_nl[NL_MP_MAXCPUS].n_value == 0) {
73 _kvm_err(kd, kd->program, "unable to find mp_maxcpus");
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");
81 len = max * sizeof(void *);
84 _kvm_err(kd, kd->program, "out of memory");
87 if (kvm_read(kd, kvm_pcpu_nl[NL_CPUID_TO_PCPU].n_value, data, len) !=
89 _kvm_err(kd, kd->program, "cannot read cpuid_to_pcpu array");
108 kvm_getpcpu(kvm_t *kd, int cpu)
119 if (_kvm_pcpu_init(kd) < 0)
122 if (cpu >= maxcpu || pcpu_data[cpu] == NULL)
125 buf = malloc(sizeof(struct pcpu));
127 _kvm_err(kd, kd->program, "out of memory");
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");
140 kvm_getmaxcpu(kvm_t *kd)
149 if (_kvm_pcpu_init(kd) < 0)