2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2013 Gleb Smirnoff <glebius@FreeBSD.org>
5 * Copyright (c) 2010 Juniper Networks, Inc.
6 * Copyright (c) 2009 Robert N. M. Watson
7 * Copyright (c) 2009 Bjoern A. Zeeb <bz@FreeBSD.org>
8 * Copyright (c) 2008 Yahoo!, Inc.
11 * Written by: John Baldwin <jhb@FreeBSD.org>
13 * This software was developed by Robert N. M. Watson under contract
14 * to Juniper Networks, Inc.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. Neither the name of the author nor the names of any co-contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
46 #include <sys/sysctl.h>
51 #include "kvm_private.h"
54 #define __OFFSET_BY_PCPU
57 static struct nlist kvm_pcpu_nl[] = {
58 { .n_name = "_cpuid_to_pcpu" },
59 { .n_name = "_mp_maxcpus" },
60 { .n_name = "_mp_ncpus" },
61 #ifdef __OFFSET_BY_PCPU
62 { .n_name = "___pcpu" },
66 #define NL_CPUID_TO_PCPU 0
67 #define NL_MP_MAXCPUS 1
72 * Kernel per-CPU data state. We cache this stuff on the first
75 * XXXRW: Possibly, this (and kvmpcpu_nl) should be per-kvm_t, in case the
76 * consumer has multiple handles in flight to differently configured
79 static void **pcpu_data;
82 #ifdef __OFFSET_BY_PCPU
83 static unsigned long __pcpu;
87 _kvm_pcpu_init(kvm_t *kd)
93 if (kvm_nlist(kd, kvm_pcpu_nl) < 0)
95 if (kvm_pcpu_nl[NL_CPUID_TO_PCPU].n_value == 0) {
96 _kvm_err(kd, kd->program, "unable to find cpuid_to_pcpu");
99 if (kvm_pcpu_nl[NL_MP_MAXCPUS].n_value == 0) {
100 _kvm_err(kd, kd->program, "unable to find mp_maxcpus");
103 if (kvm_read(kd, kvm_pcpu_nl[NL_MP_MAXCPUS].n_value, &max,
104 sizeof(max)) != sizeof(max)) {
105 _kvm_err(kd, kd->program, "cannot read mp_maxcpus");
108 if (kvm_pcpu_nl[NL_MP_NCPUS].n_value == 0) {
109 _kvm_err(kd, kd->program, "unable to find mp_ncpus");
112 if (kvm_read(kd, kvm_pcpu_nl[NL_MP_NCPUS].n_value, &mp_ncpus,
113 sizeof(mp_ncpus)) != sizeof(mp_ncpus)) {
114 _kvm_err(kd, kd->program, "cannot read mp_ncpus");
117 #ifdef __OFFSET_BY_PCPU
118 if (kvm_pcpu_nl[NL___PCPU].n_value == 0) {
119 _kvm_err(kd, kd->program, "unable to find __pcpu");
122 if (kvm_read(kd, kvm_pcpu_nl[NL___PCPU].n_value, &__pcpu,
123 sizeof(__pcpu)) != sizeof(__pcpu)) {
124 _kvm_err(kd, kd->program, "cannot read __pcpu");
128 len = max * sizeof(void *);
131 _kvm_err(kd, kd->program, "out of memory");
134 if (kvm_read(kd, kvm_pcpu_nl[NL_CPUID_TO_PCPU].n_value, data, len) !=
136 _kvm_err(kd, kd->program, "cannot read cpuid_to_pcpu array");
146 _kvm_pcpu_clear(void)
155 kvm_getpcpu(kvm_t *kd, int cpu)
165 if (_kvm_pcpu_init(kd) < 0)
168 if (cpu >= maxcpu || pcpu_data[cpu] == NULL)
171 buf = malloc(sizeof(struct pcpu));
173 _kvm_err(kd, kd->program, "out of memory");
176 if (kvm_read(kd, (uintptr_t)pcpu_data[cpu], buf,
177 sizeof(struct pcpu)) != sizeof(struct pcpu)) {
178 _kvm_err(kd, kd->program, "unable to read per-CPU data");
186 kvm_getmaxcpu(kvm_t *kd)
195 if (_kvm_pcpu_init(kd) < 0)
201 kvm_getncpus(kvm_t *kd)
205 if (_kvm_pcpu_init(kd) < 0)
211 _kvm_dpcpu_setcpu(kvm_t *kd, u_int cpu, int report_error)
214 if (!kd->dpcpu_initialized) {
216 _kvm_err(kd, kd->program, "%s: not initialized",
220 if (cpu >= kd->dpcpu_maxcpus) {
222 _kvm_err(kd, kd->program, "%s: CPU %u too big",
226 if (kd->dpcpu_off[cpu] == 0) {
228 _kvm_err(kd, kd->program, "%s: CPU %u not found",
232 kd->dpcpu_curcpu = cpu;
233 kd->dpcpu_curoff = kd->dpcpu_off[cpu];
238 * Set up libkvm to handle dynamic per-CPU memory.
241 _kvm_dpcpu_init(kvm_t *kd)
243 struct kvm_nlist nl[] = {
244 #define NLIST_START_SET_PCPU 0
245 { .n_name = "___start_" DPCPU_SETNAME },
246 #define NLIST_STOP_SET_PCPU 1
247 { .n_name = "___stop_" DPCPU_SETNAME },
248 #define NLIST_DPCPU_OFF 2
249 { .n_name = "_dpcpu_off" },
250 #define NLIST_MP_MAXCPUS 3
251 { .n_name = "_mp_maxcpus" },
254 uintptr_t *dpcpu_off_buf;
259 * XXX: This only works for native kernels for now.
265 * Locate and cache locations of important symbols using the internal
266 * version of _kvm_nlist, turning off initialization to avoid
267 * recursion in case of unresolveable symbols.
269 if (_kvm_nlist(kd, nl, 0) != 0)
271 if (kvm_read(kd, nl[NLIST_MP_MAXCPUS].n_value, &dpcpu_maxcpus,
272 sizeof(dpcpu_maxcpus)) != sizeof(dpcpu_maxcpus))
274 len = dpcpu_maxcpus * sizeof(*dpcpu_off_buf);
275 dpcpu_off_buf = malloc(len);
276 if (dpcpu_off_buf == NULL)
278 if (kvm_read(kd, nl[NLIST_DPCPU_OFF].n_value, dpcpu_off_buf, len) !=
283 kd->dpcpu_start = nl[NLIST_START_SET_PCPU].n_value;
284 kd->dpcpu_stop = nl[NLIST_STOP_SET_PCPU].n_value;
285 kd->dpcpu_maxcpus = dpcpu_maxcpus;
286 kd->dpcpu_off = dpcpu_off_buf;
287 kd->dpcpu_initialized = 1;
288 (void)_kvm_dpcpu_setcpu(kd, 0, 0);
293 * Check whether the dpcpu module has been initialized successfully or not,
294 * initialize it if permitted.
297 _kvm_dpcpu_initialized(kvm_t *kd, int intialize)
300 if (kd->dpcpu_initialized || !intialize)
301 return (kd->dpcpu_initialized);
303 (void)_kvm_dpcpu_init(kd);
305 return (kd->dpcpu_initialized);
309 * Check whether the value is within the dpcpu symbol range and only if so
310 * adjust the offset relative to the current offset.
313 _kvm_dpcpu_validaddr(kvm_t *kd, kvaddr_t value)
319 if (!kd->dpcpu_initialized)
322 if (value < kd->dpcpu_start || value >= kd->dpcpu_stop)
325 return (kd->dpcpu_curoff + value);
329 kvm_dpcpu_setcpu(kvm_t *kd, u_int cpu)
333 if (!kd->dpcpu_initialized) {
334 ret = _kvm_dpcpu_init(kd);
336 _kvm_err(kd, kd->program, "%s: init failed",
342 return (_kvm_dpcpu_setcpu(kd, cpu, 1));
346 * Obtain a per-CPU copy for given cpu from UMA_ZONE_PCPU allocation.
349 kvm_read_zpcpu(kvm_t *kd, u_long base, void *buf, size_t size, int cpu)
355 if (_kvm_pcpu_init(kd) < 0)
358 #ifdef __OFFSET_BY_PCPU
361 return (kvm_read(kd, (uintptr_t)(base + sizeof(struct pcpu) * cpu),
366 * Fetch value of a counter(9).
369 kvm_counter_u64_fetch(kvm_t *kd, u_long base)
374 if (_kvm_pcpu_init(kd) < 0)
378 for (int i = 0; i < mp_ncpus; i++) {
379 if (kvm_read_zpcpu(kd, base, &c, sizeof(c), i) != sizeof(c))