kvm_pcpu.c revision 249344
1181876Sjhb/*-
2249344Sglebius * Copyright (c) 2013 Gleb Smirnoff <glebius@FreeBSD.org>
3204494Srwatson * Copyright (c) 2010 Juniper Networks, Inc.
4204494Srwatson * Copyright (c) 2009 Robert N. M. Watson
5204494Srwatson * Copyright (c) 2009 Bjoern A. Zeeb <bz@FreeBSD.org>
6181876Sjhb * Copyright (c) 2008 Yahoo!, Inc.
7181876Sjhb * All rights reserved.
8204494Srwatson *
9181876Sjhb * Written by: John Baldwin <jhb@FreeBSD.org>
10181876Sjhb *
11204494Srwatson * This software was developed by Robert N. M. Watson under contract
12204494Srwatson * to Juniper Networks, Inc.
13204494Srwatson *
14181876Sjhb * Redistribution and use in source and binary forms, with or without
15181876Sjhb * modification, are permitted provided that the following conditions
16181876Sjhb * are met:
17181876Sjhb * 1. Redistributions of source code must retain the above copyright
18181876Sjhb *    notice, this list of conditions and the following disclaimer.
19181876Sjhb * 2. Redistributions in binary form must reproduce the above copyright
20181876Sjhb *    notice, this list of conditions and the following disclaimer in the
21181876Sjhb *    documentation and/or other materials provided with the distribution.
22181876Sjhb * 3. Neither the name of the author nor the names of any co-contributors
23181876Sjhb *    may be used to endorse or promote products derived from this software
24181876Sjhb *    without specific prior written permission.
25181876Sjhb *
26181876Sjhb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27181876Sjhb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28181876Sjhb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29181876Sjhb * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30181876Sjhb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31181876Sjhb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32181876Sjhb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33181876Sjhb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34181876Sjhb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35181876Sjhb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36181876Sjhb * SUCH DAMAGE.
37181876Sjhb */
38181876Sjhb
39181876Sjhb#include <sys/cdefs.h>
40181876Sjhb__FBSDID("$FreeBSD: head/lib/libkvm/kvm_pcpu.c 249344 2013-04-10 20:26:53Z glebius $");
41181876Sjhb
42181876Sjhb#include <sys/param.h>
43181876Sjhb#include <sys/pcpu.h>
44181876Sjhb#include <sys/sysctl.h>
45181876Sjhb#include <kvm.h>
46181876Sjhb#include <limits.h>
47181876Sjhb#include <stdlib.h>
48181876Sjhb
49181876Sjhb#include "kvm_private.h"
50181876Sjhb
51181876Sjhbstatic struct nlist kvm_pcpu_nl[] = {
52217744Suqs	{ .n_name = "_cpuid_to_pcpu" },
53217744Suqs	{ .n_name = "_mp_maxcpus" },
54249344Sglebius	{ .n_name = "_mp_ncpus" },
55217744Suqs	{ .n_name = NULL },
56181876Sjhb};
57249344Sglebius#define	NL_CPUID_TO_PCPU	0
58249344Sglebius#define	NL_MP_MAXCPUS		1
59249344Sglebius#define	NL_MP_NCPUS		2
60181876Sjhb
61181876Sjhb/*
62181876Sjhb * Kernel per-CPU data state.  We cache this stuff on the first
63181876Sjhb * access.
64204494Srwatson *
65204494Srwatson * XXXRW: Possibly, this (and kvmpcpu_nl) should be per-kvm_t, in case the
66204494Srwatson * consumer has multiple handles in flight to differently configured
67204494Srwatson * kernels/crashdumps.
68181876Sjhb */
69181876Sjhbstatic void **pcpu_data;
70181876Sjhbstatic int maxcpu;
71249344Sglebiusstatic int mp_ncpus;
72181876Sjhb
73181876Sjhbstatic int
74181876Sjhb_kvm_pcpu_init(kvm_t *kd)
75181876Sjhb{
76181876Sjhb	size_t len;
77181876Sjhb	int max;
78181876Sjhb	void *data;
79181876Sjhb
80181876Sjhb	if (kvm_nlist(kd, kvm_pcpu_nl) < 0)
81181876Sjhb		return (-1);
82181876Sjhb	if (kvm_pcpu_nl[NL_CPUID_TO_PCPU].n_value == 0) {
83181876Sjhb		_kvm_err(kd, kd->program, "unable to find cpuid_to_pcpu");
84181876Sjhb		return (-1);
85181876Sjhb	}
86181876Sjhb	if (kvm_pcpu_nl[NL_MP_MAXCPUS].n_value == 0) {
87181876Sjhb		_kvm_err(kd, kd->program, "unable to find mp_maxcpus");
88181876Sjhb		return (-1);
89181876Sjhb	}
90181876Sjhb	if (kvm_read(kd, kvm_pcpu_nl[NL_MP_MAXCPUS].n_value, &max,
91181876Sjhb	    sizeof(max)) != sizeof(max)) {
92181876Sjhb		_kvm_err(kd, kd->program, "cannot read mp_maxcpus");
93181876Sjhb		return (-1);
94181876Sjhb	}
95249344Sglebius	if (kvm_pcpu_nl[NL_MP_NCPUS].n_value == 0) {
96249344Sglebius		_kvm_err(kd, kd->program, "unable to find mp_ncpus");
97249344Sglebius		return (-1);
98249344Sglebius	}
99249344Sglebius	if (kvm_read(kd, kvm_pcpu_nl[NL_MP_NCPUS].n_value, &mp_ncpus,
100249344Sglebius	    sizeof(mp_ncpus)) != sizeof(mp_ncpus)) {
101249344Sglebius		_kvm_err(kd, kd->program, "cannot read mp_ncpus");
102249344Sglebius		return (-1);
103249344Sglebius	}
104181876Sjhb	len = max * sizeof(void *);
105181876Sjhb	data = malloc(len);
106181876Sjhb	if (data == NULL) {
107181876Sjhb		_kvm_err(kd, kd->program, "out of memory");
108181876Sjhb		return (-1);
109181876Sjhb	}
110181876Sjhb	if (kvm_read(kd, kvm_pcpu_nl[NL_CPUID_TO_PCPU].n_value, data, len) !=
111217744Suqs	   (ssize_t)len) {
112181876Sjhb		_kvm_err(kd, kd->program, "cannot read cpuid_to_pcpu array");
113181876Sjhb		free(data);
114181876Sjhb		return (-1);
115181876Sjhb	}
116181876Sjhb	pcpu_data = data;
117181876Sjhb	maxcpu = max;
118181876Sjhb	return (0);
119181876Sjhb}
120181876Sjhb
121181876Sjhbstatic void
122181876Sjhb_kvm_pcpu_clear(void)
123181876Sjhb{
124181876Sjhb
125181876Sjhb	maxcpu = 0;
126181876Sjhb	free(pcpu_data);
127181876Sjhb	pcpu_data = NULL;
128181876Sjhb}
129181876Sjhb
130181876Sjhbvoid *
131181876Sjhbkvm_getpcpu(kvm_t *kd, int cpu)
132181876Sjhb{
133181876Sjhb	char *buf;
134181876Sjhb
135181876Sjhb	if (kd == NULL) {
136181876Sjhb		_kvm_pcpu_clear();
137181876Sjhb		return (NULL);
138181876Sjhb	}
139181876Sjhb
140181876Sjhb	if (maxcpu == 0)
141181876Sjhb		if (_kvm_pcpu_init(kd) < 0)
142181876Sjhb			return ((void *)-1);
143181876Sjhb
144181876Sjhb	if (cpu >= maxcpu || pcpu_data[cpu] == NULL)
145181876Sjhb		return (NULL);
146181876Sjhb
147181876Sjhb	buf = malloc(sizeof(struct pcpu));
148181876Sjhb	if (buf == NULL) {
149181876Sjhb		_kvm_err(kd, kd->program, "out of memory");
150181876Sjhb		return ((void *)-1);
151181876Sjhb	}
152223758Sattilio	if (kvm_read(kd, (uintptr_t)pcpu_data[cpu], buf,
153223758Sattilio	    sizeof(struct pcpu)) != sizeof(struct pcpu)) {
154181876Sjhb		_kvm_err(kd, kd->program, "unable to read per-CPU data");
155181876Sjhb		free(buf);
156181876Sjhb		return ((void *)-1);
157181876Sjhb	}
158181876Sjhb	return (buf);
159181876Sjhb}
160181876Sjhb
161181876Sjhbint
162181876Sjhbkvm_getmaxcpu(kvm_t *kd)
163181876Sjhb{
164181876Sjhb
165181876Sjhb	if (kd == NULL) {
166181876Sjhb		_kvm_pcpu_clear();
167181876Sjhb		return (0);
168181876Sjhb	}
169181876Sjhb
170181876Sjhb	if (maxcpu == 0)
171181876Sjhb		if (_kvm_pcpu_init(kd) < 0)
172181876Sjhb			return (-1);
173181876Sjhb	return (maxcpu);
174181876Sjhb}
175204494Srwatson
176204494Srwatsonstatic int
177204494Srwatson_kvm_dpcpu_setcpu(kvm_t *kd, u_int cpu, int report_error)
178204494Srwatson{
179204494Srwatson
180204494Srwatson	if (!kd->dpcpu_initialized) {
181204494Srwatson		if (report_error)
182204494Srwatson			_kvm_err(kd, kd->program, "%s: not initialized",
183204494Srwatson			    __func__);
184204494Srwatson		return (-1);
185204494Srwatson	}
186204494Srwatson	if (cpu >= kd->dpcpu_maxcpus) {
187204494Srwatson		if (report_error)
188204494Srwatson			_kvm_err(kd, kd->program, "%s: CPU %u too big",
189204494Srwatson			    __func__, cpu);
190204494Srwatson		return (-1);
191204494Srwatson	}
192204494Srwatson	if (kd->dpcpu_off[cpu] == 0) {
193204494Srwatson		if (report_error)
194204494Srwatson			_kvm_err(kd, kd->program, "%s: CPU %u not found",
195204494Srwatson			    __func__, cpu);
196204494Srwatson		return (-1);
197204494Srwatson	}
198204494Srwatson	kd->dpcpu_curcpu = cpu;
199204494Srwatson	kd->dpcpu_curoff = kd->dpcpu_off[cpu];
200204494Srwatson	return (0);
201204494Srwatson}
202204494Srwatson
203204494Srwatson/*
204204494Srwatson * Set up libkvm to handle dynamic per-CPU memory.
205204494Srwatson */
206204494Srwatsonstatic int
207204494Srwatson_kvm_dpcpu_init(kvm_t *kd)
208204494Srwatson{
209204494Srwatson	struct nlist nl[] = {
210204494Srwatson#define	NLIST_START_SET_PCPU	0
211217744Suqs		{ .n_name = "___start_" DPCPU_SETNAME },
212204494Srwatson#define	NLIST_STOP_SET_PCPU	1
213217744Suqs		{ .n_name = "___stop_" DPCPU_SETNAME },
214204494Srwatson#define	NLIST_DPCPU_OFF		2
215217744Suqs		{ .n_name = "_dpcpu_off" },
216204494Srwatson#define	NLIST_MP_MAXCPUS	3
217217744Suqs		{ .n_name = "_mp_maxcpus" },
218217744Suqs		{ .n_name = NULL },
219204494Srwatson	};
220204494Srwatson	uintptr_t *dpcpu_off_buf;
221204494Srwatson	size_t len;
222204494Srwatson	u_int dpcpu_maxcpus;
223204494Srwatson
224204494Srwatson	/*
225204494Srwatson	 * Locate and cache locations of important symbols using the internal
226204494Srwatson	 * version of _kvm_nlist, turning off initialization to avoid
227204494Srwatson	 * recursion in case of unresolveable symbols.
228204494Srwatson	 */
229204494Srwatson	if (_kvm_nlist(kd, nl, 0) != 0)
230204494Srwatson		return (-1);
231204494Srwatson	if (kvm_read(kd, nl[NLIST_MP_MAXCPUS].n_value, &dpcpu_maxcpus,
232204494Srwatson	    sizeof(dpcpu_maxcpus)) != sizeof(dpcpu_maxcpus))
233204494Srwatson		return (-1);
234204494Srwatson	len = dpcpu_maxcpus * sizeof(*dpcpu_off_buf);
235204494Srwatson	dpcpu_off_buf = malloc(len);
236204494Srwatson	if (dpcpu_off_buf == NULL)
237204494Srwatson		return (-1);
238204494Srwatson	if (kvm_read(kd, nl[NLIST_DPCPU_OFF].n_value, dpcpu_off_buf, len) !=
239217744Suqs	    (ssize_t)len) {
240204494Srwatson		free(dpcpu_off_buf);
241204494Srwatson		return (-1);
242204494Srwatson	}
243204494Srwatson	kd->dpcpu_start = nl[NLIST_START_SET_PCPU].n_value;
244204494Srwatson	kd->dpcpu_stop = nl[NLIST_STOP_SET_PCPU].n_value;
245204494Srwatson	kd->dpcpu_maxcpus = dpcpu_maxcpus;
246204494Srwatson	kd->dpcpu_off = dpcpu_off_buf;
247204494Srwatson	kd->dpcpu_initialized = 1;
248204494Srwatson	(void)_kvm_dpcpu_setcpu(kd, 0, 0);
249204494Srwatson	return (0);
250204494Srwatson}
251204494Srwatson
252204494Srwatson/*
253204494Srwatson * Check whether the dpcpu module has been initialized sucessfully or not,
254204494Srwatson * initialize it if permitted.
255204494Srwatson */
256204494Srwatsonint
257204494Srwatson_kvm_dpcpu_initialized(kvm_t *kd, int intialize)
258204494Srwatson{
259204494Srwatson
260204494Srwatson	if (kd->dpcpu_initialized || !intialize)
261204494Srwatson		return (kd->dpcpu_initialized);
262204494Srwatson
263204494Srwatson	(void)_kvm_dpcpu_init(kd);
264204494Srwatson
265204494Srwatson	return (kd->dpcpu_initialized);
266204494Srwatson}
267204494Srwatson
268204494Srwatson/*
269204494Srwatson * Check whether the value is within the dpcpu symbol range and only if so
270204494Srwatson * adjust the offset relative to the current offset.
271204494Srwatson */
272204494Srwatsonuintptr_t
273204494Srwatson_kvm_dpcpu_validaddr(kvm_t *kd, uintptr_t value)
274204494Srwatson{
275204494Srwatson
276204494Srwatson	if (value == 0)
277204494Srwatson		return (value);
278204494Srwatson
279204494Srwatson	if (!kd->dpcpu_initialized)
280204494Srwatson		return (value);
281204494Srwatson
282204494Srwatson	if (value < kd->dpcpu_start || value >= kd->dpcpu_stop)
283204494Srwatson		return (value);
284204494Srwatson
285204494Srwatson	return (kd->dpcpu_curoff + value);
286204494Srwatson}
287204494Srwatson
288204494Srwatsonint
289204494Srwatsonkvm_dpcpu_setcpu(kvm_t *kd, u_int cpu)
290204494Srwatson{
291204494Srwatson	int ret;
292204494Srwatson
293204494Srwatson	if (!kd->dpcpu_initialized) {
294204494Srwatson		ret = _kvm_dpcpu_init(kd);
295204494Srwatson		if (ret != 0) {
296204494Srwatson			_kvm_err(kd, kd->program, "%s: init failed",
297204494Srwatson			    __func__);
298204494Srwatson			return (ret);
299204494Srwatson		}
300204494Srwatson	}
301204494Srwatson
302204494Srwatson	return (_kvm_dpcpu_setcpu(kd, cpu, 1));
303204494Srwatson}
304249344Sglebius
305249344Sglebius/*
306249344Sglebius * Obtain a per-CPU copy for given cpu from UMA_ZONE_PCPU allocation.
307249344Sglebius */
308249344Sglebiusssize_t
309249344Sglebiuskvm_read_zpcpu(kvm_t *kd, void *buf, u_long base, size_t size, int cpu)
310249344Sglebius{
311249344Sglebius
312249344Sglebius	return (kvm_read(kd, (uintptr_t)(base + sizeof(struct pcpu) * cpu),
313249344Sglebius	    buf, size));
314249344Sglebius}
315249344Sglebius
316249344Sglebius/*
317249344Sglebius * Fetch value of a counter(9).
318249344Sglebius */
319249344Sglebiusuint64_t
320249344Sglebiuskvm_counter_u64_fetch(kvm_t *kd, u_long base)
321249344Sglebius{
322249344Sglebius	uint64_t r, c;
323249344Sglebius
324249344Sglebius	if (mp_ncpus == 0)
325249344Sglebius		if (_kvm_pcpu_init(kd) < 0)
326249344Sglebius			return (0);
327249344Sglebius
328249344Sglebius	r = 0;
329249344Sglebius	for (int i = 0; i < mp_ncpus; i++) {
330249344Sglebius		if (kvm_read_zpcpu(kd, &c, base, sizeof(c), i) != sizeof(c))
331249344Sglebius			return (0);
332249344Sglebius		r += c;
333249344Sglebius	}
334249344Sglebius
335249344Sglebius	return (r);
336249344Sglebius}
337