2 * Copyright (c) 2009 Robert N. M. Watson
3 * Copyright (c) 2009 Bjoern A. Zeeb <bz@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
37 #include <sys/_lock.h>
38 #include <sys/_mutex.h>
39 #include <sys/_task.h>
42 #include <sys/types.h>
52 #include "kvm_private.h"
55 * Set up libkvm to handle virtual network stack symbols by selecting a
59 _kvm_vnet_selectpid(kvm_t *kd, pid_t pid)
67 * Note: kvm_nlist strips the first '_' so add an extra one
68 * here to __{start,stop}_set_vnet.
70 #define NLIST_START_VNET 0
71 { .n_name = "___start_" VNET_SETNAME },
72 #define NLIST_STOP_VNET 1
73 { .n_name = "___stop_" VNET_SETNAME },
74 #define NLIST_VNET_HEAD 2
75 { .n_name = "vnet_head" },
76 #define NLIST_ALLPROC 3
77 { .n_name = "allproc" },
78 #define NLIST_DUMPTID 4
79 { .n_name = "dumptid" },
81 { .n_name = "proc0" },
84 uintptr_t procp, credp;
85 #define VMCORE_VNET_OF_PROC0
86 #ifndef VMCORE_VNET_OF_PROC0
93 * Locate and cache locations of important symbols
94 * using the internal version of _kvm_nlist, turning
95 * off initialization to avoid recursion in case of
96 * unresolveable symbols.
98 if (_kvm_nlist(kd, nl, 0) != 0) {
100 * XXX-BZ: ___start_/___stop_VNET_SETNAME may fail.
101 * For now do not report an error here as we are called
102 * internally and in `void context' until we merge the
103 * functionality to optionally activate this into programs.
104 * By that time we can properly fail and let the callers
107 /* _kvm_err(kd, kd->program, "%s: no namelist", __func__); */
112 * Auto-detect if this is a crashdump by reading dumptid.
115 if (nl[NLIST_DUMPTID].n_value) {
116 if (kvm_read(kd, nl[NLIST_DUMPTID].n_value, &dumptid,
117 sizeof(dumptid)) != sizeof(dumptid)) {
118 _kvm_err(kd, kd->program, "%s: dumptid", __func__);
124 * First, find the process for this pid. If we are working on a
125 * dump, either locate the thread dumptid is refering to or proc0.
126 * Based on either, take the address of the ucred.
130 procp = nl[NLIST_ALLPROC].n_value;
131 #ifdef VMCORE_VNET_OF_PROC0
133 procp = nl[NLIST_PROC0].n_value;
138 if (kvm_read(kd, procp, &proc, sizeof(proc)) != sizeof(proc)) {
139 _kvm_err(kd, kd->program, "%s: proc", __func__);
142 #ifndef VMCORE_VNET_OF_PROC0
144 tdp = (uintptr_t)TAILQ_FIRST(&proc.p_threads);
146 if (kvm_read(kd, tdp, &td, sizeof(td)) !=
148 _kvm_err(kd, kd->program, "%s: thread",
152 if (td.td_tid == dumptid) {
153 credp = (uintptr_t)td.td_ucred;
156 tdp = (uintptr_t)TAILQ_NEXT(&td, td_plist);
160 if (proc.p_pid == pid)
161 credp = (uintptr_t)proc.p_ucred;
164 procp = (uintptr_t)LIST_NEXT(&proc, p_list);
167 _kvm_err(kd, kd->program, "%s: pid/tid not found", __func__);
170 if (kvm_read(kd, (uintptr_t)credp, &cred, sizeof(cred)) !=
172 _kvm_err(kd, kd->program, "%s: cred", __func__);
175 if (cred.cr_prison == NULL) {
176 _kvm_err(kd, kd->program, "%s: no jail", __func__);
179 if (kvm_read(kd, (uintptr_t)cred.cr_prison, &prison, sizeof(prison)) !=
181 _kvm_err(kd, kd->program, "%s: prison", __func__);
184 if (prison.pr_vnet == NULL) {
185 _kvm_err(kd, kd->program, "%s: no vnet", __func__);
188 if (kvm_read(kd, (uintptr_t)prison.pr_vnet, &vnet, sizeof(vnet)) !=
190 _kvm_err(kd, kd->program, "%s: vnet", __func__);
193 if (vnet.vnet_magic_n != VNET_MAGIC_N) {
194 _kvm_err(kd, kd->program, "%s: invalid vnet magic#", __func__);
197 kd->vnet_initialized = 1;
198 kd->vnet_start = nl[NLIST_START_VNET].n_value;
199 kd->vnet_stop = nl[NLIST_STOP_VNET].n_value;
200 kd->vnet_current = (uintptr_t)prison.pr_vnet;
201 kd->vnet_base = vnet.vnet_data_base;
206 * Check whether the vnet module has been initialized sucessfully
207 * or not, intialize it if permitted.
210 _kvm_vnet_initialized(kvm_t *kd, int intialize)
213 if (kd->vnet_initialized || !intialize)
214 return (kd->vnet_initialized);
216 (void) _kvm_vnet_selectpid(kd, getpid());
218 return (kd->vnet_initialized);
222 * Check whether the value is within the vnet symbol range and
223 * only if so adjust the offset relative to the current base.
226 _kvm_vnet_validaddr(kvm_t *kd, uintptr_t value)
232 if (!kd->vnet_initialized)
235 if (value < kd->vnet_start || value >= kd->vnet_stop)
238 return (kd->vnet_base + value);