summaryrefslogtreecommitdiffstats
path: root/lib/libkvm/kvm_pcpu.c
diff options
context:
space:
mode:
authorrwatson <rwatson@FreeBSD.org>2010-03-01 00:27:55 +0000
committerrwatson <rwatson@FreeBSD.org>2010-03-01 00:27:55 +0000
commit2c2940cdf7c5fa033c50f9c80a5b9f860d82d858 (patch)
tree04416be9d793aa3a6797065cfdbdfbee0a289fde /lib/libkvm/kvm_pcpu.c
parent67dd56fb510222481fc77553f9ef25a5fd1f9620 (diff)
downloadFreeBSD-src-2c2940cdf7c5fa033c50f9c80a5b9f860d82d858.zip
FreeBSD-src-2c2940cdf7c5fa033c50f9c80a5b9f860d82d858.tar.gz
A first cut at teaching libkvm how to deal with dynamic per-CPU storage
(DPCPU): A new API, kvm_dpcpu_setcpu(3), selects the active CPU for the purposes of DPCPU. Calls to kvm_nlist(3) will automatically translate DPCPU symbols and return a pointer to the current CPU's version of the data. Consumers needing to read the same symbol on several CPUs will invoke a series of setcpu/nlist calls, one per CPU of interest. This addition makes it possible for tools like netstat(1) to query the values of DPCPU variables during crashdump analysis, and is based on similar code handling virtualized global variables. MFC after: 1 week Sponsored by: Juniper Networks, Inc.
Diffstat (limited to 'lib/libkvm/kvm_pcpu.c')
-rw-r--r--lib/libkvm/kvm_pcpu.c140
1 files changed, 140 insertions, 0 deletions
diff --git a/lib/libkvm/kvm_pcpu.c b/lib/libkvm/kvm_pcpu.c
index e4f8909..484d2ea 100644
--- a/lib/libkvm/kvm_pcpu.c
+++ b/lib/libkvm/kvm_pcpu.c
@@ -1,8 +1,15 @@
/*-
+ * Copyright (c) 2010 Juniper Networks, Inc.
+ * Copyright (c) 2009 Robert N. M. Watson
+ * Copyright (c) 2009 Bjoern A. Zeeb <bz@FreeBSD.org>
* Copyright (c) 2008 Yahoo!, Inc.
* All rights reserved.
+ *
* Written by: John Baldwin <jhb@FreeBSD.org>
*
+ * This software was developed by Robert N. M. Watson under contract
+ * to Juniper Networks, Inc.
+ *
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
@@ -49,6 +56,10 @@ static struct nlist kvm_pcpu_nl[] = {
/*
* Kernel per-CPU data state. We cache this stuff on the first
* access.
+ *
+ * XXXRW: Possibly, this (and kvmpcpu_nl) should be per-kvm_t, in case the
+ * consumer has multiple handles in flight to differently configured
+ * kernels/crashdumps.
*/
static void **pcpu_data;
static int maxcpu;
@@ -150,3 +161,132 @@ kvm_getmaxcpu(kvm_t *kd)
return (-1);
return (maxcpu);
}
+
+static int
+_kvm_dpcpu_setcpu(kvm_t *kd, u_int cpu, int report_error)
+{
+
+ if (!kd->dpcpu_initialized) {
+ if (report_error)
+ _kvm_err(kd, kd->program, "%s: not initialized",
+ __func__);
+ return (-1);
+ }
+ if (cpu >= kd->dpcpu_maxcpus) {
+ if (report_error)
+ _kvm_err(kd, kd->program, "%s: CPU %u too big",
+ __func__, cpu);
+ return (-1);
+ }
+ if (kd->dpcpu_off[cpu] == 0) {
+ if (report_error)
+ _kvm_err(kd, kd->program, "%s: CPU %u not found",
+ __func__, cpu);
+ return (-1);
+ }
+ kd->dpcpu_curcpu = cpu;
+ kd->dpcpu_curoff = kd->dpcpu_off[cpu];
+ return (0);
+}
+
+/*
+ * Set up libkvm to handle dynamic per-CPU memory.
+ */
+static int
+_kvm_dpcpu_init(kvm_t *kd)
+{
+ struct nlist nl[] = {
+#define NLIST_START_SET_PCPU 0
+ { "___start_set_pcpu" },
+#define NLIST_STOP_SET_PCPU 1
+ { "___stop_set_pcpu" },
+#define NLIST_DPCPU_OFF 2
+ { "_dpcpu_off" },
+#define NLIST_MP_MAXCPUS 3
+ { "_mp_maxcpus" },
+ { NULL },
+ };
+ uintptr_t *dpcpu_off_buf;
+ size_t len;
+ u_int dpcpu_maxcpus;
+
+ /*
+ * Locate and cache locations of important symbols using the internal
+ * version of _kvm_nlist, turning off initialization to avoid
+ * recursion in case of unresolveable symbols.
+ */
+ if (_kvm_nlist(kd, nl, 0) != 0)
+ return (-1);
+ if (kvm_read(kd, nl[NLIST_MP_MAXCPUS].n_value, &dpcpu_maxcpus,
+ sizeof(dpcpu_maxcpus)) != sizeof(dpcpu_maxcpus))
+ return (-1);
+ len = dpcpu_maxcpus * sizeof(*dpcpu_off_buf);
+ dpcpu_off_buf = malloc(len);
+ if (dpcpu_off_buf == NULL)
+ return (-1);
+ if (kvm_read(kd, nl[NLIST_DPCPU_OFF].n_value, dpcpu_off_buf, len) !=
+ len) {
+ free(dpcpu_off_buf);
+ return (-1);
+ }
+ kd->dpcpu_start = nl[NLIST_START_SET_PCPU].n_value;
+ kd->dpcpu_stop = nl[NLIST_STOP_SET_PCPU].n_value;
+ kd->dpcpu_maxcpus = dpcpu_maxcpus;
+ kd->dpcpu_off = dpcpu_off_buf;
+ kd->dpcpu_initialized = 1;
+ (void)_kvm_dpcpu_setcpu(kd, 0, 0);
+ return (0);
+}
+
+/*
+ * Check whether the dpcpu module has been initialized sucessfully or not,
+ * initialize it if permitted.
+ */
+int
+_kvm_dpcpu_initialized(kvm_t *kd, int intialize)
+{
+
+ if (kd->dpcpu_initialized || !intialize)
+ return (kd->dpcpu_initialized);
+
+ (void)_kvm_dpcpu_init(kd);
+
+ return (kd->dpcpu_initialized);
+}
+
+/*
+ * Check whether the value is within the dpcpu symbol range and only if so
+ * adjust the offset relative to the current offset.
+ */
+uintptr_t
+_kvm_dpcpu_validaddr(kvm_t *kd, uintptr_t value)
+{
+
+ if (value == 0)
+ return (value);
+
+ if (!kd->dpcpu_initialized)
+ return (value);
+
+ if (value < kd->dpcpu_start || value >= kd->dpcpu_stop)
+ return (value);
+
+ return (kd->dpcpu_curoff + value);
+}
+
+int
+kvm_dpcpu_setcpu(kvm_t *kd, u_int cpu)
+{
+ int ret;
+
+ if (!kd->dpcpu_initialized) {
+ ret = _kvm_dpcpu_init(kd);
+ if (ret != 0) {
+ _kvm_err(kd, kd->program, "%s: init failed",
+ __func__);
+ return (ret);
+ }
+ }
+
+ return (_kvm_dpcpu_setcpu(kd, cpu, 1));
+}
OpenPOWER on IntegriCloud