2 * Copyright (c) 2013 Gleb Smirnoff <glebius@FreeBSD.org>
3 * Copyright (c) 2010 Juniper Networks, Inc.
4 * Copyright (c) 2009 Robert N. M. Watson
5 * Copyright (c) 2009 Bjoern A. Zeeb <bz@FreeBSD.org>
6 * Copyright (c) 2008 Yahoo!, Inc.
9 * Written by: John Baldwin <jhb@FreeBSD.org>
11 * This software was developed by Robert N. M. Watson under contract
12 * to Juniper Networks, Inc.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. Neither the name of the author nor the names of any co-contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
44 #include <sys/sysctl.h>
49 #include "kvm_private.h"
51 static struct nlist kvm_pcpu_nl[] = {
52 { .n_name = "_cpuid_to_pcpu" },
53 { .n_name = "_mp_maxcpus" },
54 { .n_name = "_mp_ncpus" },
57 #define NL_CPUID_TO_PCPU 0
58 #define NL_MP_MAXCPUS 1
62 * Kernel per-CPU data state. We cache this stuff on the first
65 * XXXRW: Possibly, this (and kvmpcpu_nl) should be per-kvm_t, in case the
66 * consumer has multiple handles in flight to differently configured
69 static void **pcpu_data;
74 _kvm_pcpu_init(kvm_t *kd)
80 if (kvm_nlist(kd, kvm_pcpu_nl) < 0)
82 if (kvm_pcpu_nl[NL_CPUID_TO_PCPU].n_value == 0) {
83 _kvm_err(kd, kd->program, "unable to find cpuid_to_pcpu");
86 if (kvm_pcpu_nl[NL_MP_MAXCPUS].n_value == 0) {
87 _kvm_err(kd, kd->program, "unable to find mp_maxcpus");
90 if (kvm_read(kd, kvm_pcpu_nl[NL_MP_MAXCPUS].n_value, &max,
91 sizeof(max)) != sizeof(max)) {
92 _kvm_err(kd, kd->program, "cannot read mp_maxcpus");
95 if (kvm_pcpu_nl[NL_MP_NCPUS].n_value == 0) {
96 _kvm_err(kd, kd->program, "unable to find mp_ncpus");
99 if (kvm_read(kd, kvm_pcpu_nl[NL_MP_NCPUS].n_value, &mp_ncpus,
100 sizeof(mp_ncpus)) != sizeof(mp_ncpus)) {
101 _kvm_err(kd, kd->program, "cannot read mp_ncpus");
104 len = max * sizeof(void *);
107 _kvm_err(kd, kd->program, "out of memory");
110 if (kvm_read(kd, kvm_pcpu_nl[NL_CPUID_TO_PCPU].n_value, data, len) !=
112 _kvm_err(kd, kd->program, "cannot read cpuid_to_pcpu array");
122 _kvm_pcpu_clear(void)
131 kvm_getpcpu(kvm_t *kd, int cpu)
141 if (_kvm_pcpu_init(kd) < 0)
144 if (cpu >= maxcpu || pcpu_data[cpu] == NULL)
147 buf = malloc(sizeof(struct pcpu));
149 _kvm_err(kd, kd->program, "out of memory");
152 if (kvm_read(kd, (uintptr_t)pcpu_data[cpu], buf,
153 sizeof(struct pcpu)) != sizeof(struct pcpu)) {
154 _kvm_err(kd, kd->program, "unable to read per-CPU data");
162 kvm_getmaxcpu(kvm_t *kd)
171 if (_kvm_pcpu_init(kd) < 0)
177 _kvm_dpcpu_setcpu(kvm_t *kd, u_int cpu, int report_error)
180 if (!kd->dpcpu_initialized) {
182 _kvm_err(kd, kd->program, "%s: not initialized",
186 if (cpu >= kd->dpcpu_maxcpus) {
188 _kvm_err(kd, kd->program, "%s: CPU %u too big",
192 if (kd->dpcpu_off[cpu] == 0) {
194 _kvm_err(kd, kd->program, "%s: CPU %u not found",
198 kd->dpcpu_curcpu = cpu;
199 kd->dpcpu_curoff = kd->dpcpu_off[cpu];
204 * Set up libkvm to handle dynamic per-CPU memory.
207 _kvm_dpcpu_init(kvm_t *kd)
209 struct nlist nl[] = {
210 #define NLIST_START_SET_PCPU 0
211 { .n_name = "___start_" DPCPU_SETNAME },
212 #define NLIST_STOP_SET_PCPU 1
213 { .n_name = "___stop_" DPCPU_SETNAME },
214 #define NLIST_DPCPU_OFF 2
215 { .n_name = "_dpcpu_off" },
216 #define NLIST_MP_MAXCPUS 3
217 { .n_name = "_mp_maxcpus" },
220 uintptr_t *dpcpu_off_buf;
225 * Locate and cache locations of important symbols using the internal
226 * version of _kvm_nlist, turning off initialization to avoid
227 * recursion in case of unresolveable symbols.
229 if (_kvm_nlist(kd, nl, 0) != 0)
231 if (kvm_read(kd, nl[NLIST_MP_MAXCPUS].n_value, &dpcpu_maxcpus,
232 sizeof(dpcpu_maxcpus)) != sizeof(dpcpu_maxcpus))
234 len = dpcpu_maxcpus * sizeof(*dpcpu_off_buf);
235 dpcpu_off_buf = malloc(len);
236 if (dpcpu_off_buf == NULL)
238 if (kvm_read(kd, nl[NLIST_DPCPU_OFF].n_value, dpcpu_off_buf, len) !=
243 kd->dpcpu_start = nl[NLIST_START_SET_PCPU].n_value;
244 kd->dpcpu_stop = nl[NLIST_STOP_SET_PCPU].n_value;
245 kd->dpcpu_maxcpus = dpcpu_maxcpus;
246 kd->dpcpu_off = dpcpu_off_buf;
247 kd->dpcpu_initialized = 1;
248 (void)_kvm_dpcpu_setcpu(kd, 0, 0);
253 * Check whether the dpcpu module has been initialized sucessfully or not,
254 * initialize it if permitted.
257 _kvm_dpcpu_initialized(kvm_t *kd, int intialize)
260 if (kd->dpcpu_initialized || !intialize)
261 return (kd->dpcpu_initialized);
263 (void)_kvm_dpcpu_init(kd);
265 return (kd->dpcpu_initialized);
269 * Check whether the value is within the dpcpu symbol range and only if so
270 * adjust the offset relative to the current offset.
273 _kvm_dpcpu_validaddr(kvm_t *kd, uintptr_t value)
279 if (!kd->dpcpu_initialized)
282 if (value < kd->dpcpu_start || value >= kd->dpcpu_stop)
285 return (kd->dpcpu_curoff + value);
289 kvm_dpcpu_setcpu(kvm_t *kd, u_int cpu)
293 if (!kd->dpcpu_initialized) {
294 ret = _kvm_dpcpu_init(kd);
296 _kvm_err(kd, kd->program, "%s: init failed",
302 return (_kvm_dpcpu_setcpu(kd, cpu, 1));
306 * Obtain a per-CPU copy for given cpu from UMA_ZONE_PCPU allocation.
309 kvm_read_zpcpu(kvm_t *kd, void *buf, u_long base, size_t size, int cpu)
312 return (kvm_read(kd, (uintptr_t)(base + sizeof(struct pcpu) * cpu),
317 * Fetch value of a counter(9).
320 kvm_counter_u64_fetch(kvm_t *kd, u_long base)
325 if (_kvm_pcpu_init(kd) < 0)
329 for (int i = 0; i < mp_ncpus; i++) {
330 if (kvm_read_zpcpu(kd, &c, base, sizeof(c), i) != sizeof(c))