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"
53 static struct nlist kvm_pcpu_nl[] = {
54 { .n_name = "_cpuid_to_pcpu" },
55 { .n_name = "_mp_maxcpus" },
56 { .n_name = "_mp_ncpus" },
59 #define NL_CPUID_TO_PCPU 0
60 #define NL_MP_MAXCPUS 1
64 * Kernel per-CPU data state. We cache this stuff on the first
67 * XXXRW: Possibly, this (and kvmpcpu_nl) should be per-kvm_t, in case the
68 * consumer has multiple handles in flight to differently configured
71 static void **pcpu_data;
76 _kvm_pcpu_init(kvm_t *kd)
82 if (kvm_nlist(kd, kvm_pcpu_nl) < 0)
84 if (kvm_pcpu_nl[NL_CPUID_TO_PCPU].n_value == 0) {
85 _kvm_err(kd, kd->program, "unable to find cpuid_to_pcpu");
88 if (kvm_pcpu_nl[NL_MP_MAXCPUS].n_value == 0) {
89 _kvm_err(kd, kd->program, "unable to find mp_maxcpus");
92 if (kvm_read(kd, kvm_pcpu_nl[NL_MP_MAXCPUS].n_value, &max,
93 sizeof(max)) != sizeof(max)) {
94 _kvm_err(kd, kd->program, "cannot read mp_maxcpus");
97 if (kvm_pcpu_nl[NL_MP_NCPUS].n_value == 0) {
98 _kvm_err(kd, kd->program, "unable to find mp_ncpus");
101 if (kvm_read(kd, kvm_pcpu_nl[NL_MP_NCPUS].n_value, &mp_ncpus,
102 sizeof(mp_ncpus)) != sizeof(mp_ncpus)) {
103 _kvm_err(kd, kd->program, "cannot read mp_ncpus");
106 len = max * sizeof(void *);
109 _kvm_err(kd, kd->program, "out of memory");
112 if (kvm_read(kd, kvm_pcpu_nl[NL_CPUID_TO_PCPU].n_value, data, len) !=
114 _kvm_err(kd, kd->program, "cannot read cpuid_to_pcpu array");
124 _kvm_pcpu_clear(void)
133 kvm_getpcpu(kvm_t *kd, int cpu)
143 if (_kvm_pcpu_init(kd) < 0)
146 if (cpu >= maxcpu || pcpu_data[cpu] == NULL)
149 buf = malloc(sizeof(struct pcpu));
151 _kvm_err(kd, kd->program, "out of memory");
154 if (kvm_read(kd, (uintptr_t)pcpu_data[cpu], buf,
155 sizeof(struct pcpu)) != sizeof(struct pcpu)) {
156 _kvm_err(kd, kd->program, "unable to read per-CPU data");
164 kvm_getmaxcpu(kvm_t *kd)
173 if (_kvm_pcpu_init(kd) < 0)
179 kvm_getncpus(kvm_t *kd)
183 if (_kvm_pcpu_init(kd) < 0)
189 _kvm_dpcpu_setcpu(kvm_t *kd, u_int cpu, int report_error)
192 if (!kd->dpcpu_initialized) {
194 _kvm_err(kd, kd->program, "%s: not initialized",
198 if (cpu >= kd->dpcpu_maxcpus) {
200 _kvm_err(kd, kd->program, "%s: CPU %u too big",
204 if (kd->dpcpu_off[cpu] == 0) {
206 _kvm_err(kd, kd->program, "%s: CPU %u not found",
210 kd->dpcpu_curcpu = cpu;
211 kd->dpcpu_curoff = kd->dpcpu_off[cpu];
216 * Set up libkvm to handle dynamic per-CPU memory.
219 _kvm_dpcpu_init(kvm_t *kd)
221 struct kvm_nlist nl[] = {
222 #define NLIST_START_SET_PCPU 0
223 { .n_name = "___start_" DPCPU_SETNAME },
224 #define NLIST_STOP_SET_PCPU 1
225 { .n_name = "___stop_" DPCPU_SETNAME },
226 #define NLIST_DPCPU_OFF 2
227 { .n_name = "_dpcpu_off" },
228 #define NLIST_MP_MAXCPUS 3
229 { .n_name = "_mp_maxcpus" },
232 uintptr_t *dpcpu_off_buf;
237 * XXX: This only works for native kernels for now.
243 * Locate and cache locations of important symbols using the internal
244 * version of _kvm_nlist, turning off initialization to avoid
245 * recursion in case of unresolveable symbols.
247 if (_kvm_nlist(kd, nl, 0) != 0)
249 if (kvm_read(kd, nl[NLIST_MP_MAXCPUS].n_value, &dpcpu_maxcpus,
250 sizeof(dpcpu_maxcpus)) != sizeof(dpcpu_maxcpus))
252 len = dpcpu_maxcpus * sizeof(*dpcpu_off_buf);
253 dpcpu_off_buf = malloc(len);
254 if (dpcpu_off_buf == NULL)
256 if (kvm_read(kd, nl[NLIST_DPCPU_OFF].n_value, dpcpu_off_buf, len) !=
261 kd->dpcpu_start = nl[NLIST_START_SET_PCPU].n_value;
262 kd->dpcpu_stop = nl[NLIST_STOP_SET_PCPU].n_value;
263 kd->dpcpu_maxcpus = dpcpu_maxcpus;
264 kd->dpcpu_off = dpcpu_off_buf;
265 kd->dpcpu_initialized = 1;
266 (void)_kvm_dpcpu_setcpu(kd, 0, 0);
271 * Check whether the dpcpu module has been initialized successfully or not,
272 * initialize it if permitted.
275 _kvm_dpcpu_initialized(kvm_t *kd, int intialize)
278 if (kd->dpcpu_initialized || !intialize)
279 return (kd->dpcpu_initialized);
281 (void)_kvm_dpcpu_init(kd);
283 return (kd->dpcpu_initialized);
287 * Check whether the value is within the dpcpu symbol range and only if so
288 * adjust the offset relative to the current offset.
291 _kvm_dpcpu_validaddr(kvm_t *kd, kvaddr_t value)
297 if (!kd->dpcpu_initialized)
300 if (value < kd->dpcpu_start || value >= kd->dpcpu_stop)
303 return (kd->dpcpu_curoff + value);
307 kvm_dpcpu_setcpu(kvm_t *kd, u_int cpu)
311 if (!kd->dpcpu_initialized) {
312 ret = _kvm_dpcpu_init(kd);
314 _kvm_err(kd, kd->program, "%s: init failed",
320 return (_kvm_dpcpu_setcpu(kd, cpu, 1));
324 * Obtain a per-CPU copy for given cpu from UMA_ZONE_PCPU allocation.
327 kvm_read_zpcpu(kvm_t *kd, u_long base, void *buf, size_t size, int cpu)
332 return (kvm_read(kd, (uintptr_t)(base + sizeof(struct pcpu) * cpu),
337 * Fetch value of a counter(9).
340 kvm_counter_u64_fetch(kvm_t *kd, u_long base)
345 if (_kvm_pcpu_init(kd) < 0)
349 for (int i = 0; i < mp_ncpus; i++) {
350 if (kvm_read_zpcpu(kd, base, &c, sizeof(c), i) != sizeof(c))