11573Srgrimes/*-
21573Srgrimes * Copyright (c) 1992, 1993
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * This code is derived from software developed by the Computer Systems
61573Srgrimes * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
71573Srgrimes * BG 91-66 and contributed to Berkeley.
81573Srgrimes *
91573Srgrimes * Redistribution and use in source and binary forms, with or without
101573Srgrimes * modification, are permitted provided that the following conditions
111573Srgrimes * are met:
121573Srgrimes * 1. Redistributions of source code must retain the above copyright
131573Srgrimes *    notice, this list of conditions and the following disclaimer.
141573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
151573Srgrimes *    notice, this list of conditions and the following disclaimer in the
161573Srgrimes *    documentation and/or other materials provided with the distribution.
171573Srgrimes * 4. Neither the name of the University nor the names of its contributors
181573Srgrimes *    may be used to endorse or promote products derived from this software
191573Srgrimes *    without specific prior written permission.
201573Srgrimes *
211573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311573Srgrimes * SUCH DAMAGE.
321573Srgrimes *
331573Srgrimes *	@(#)kvm_private.h	8.1 (Berkeley) 6/4/93
3455127Speter * $FreeBSD: stable/11/lib/libkvm/kvm_private.h 312381 2017-01-18 14:13:28Z avg $
351573Srgrimes */
361573Srgrimes
37291406Sjhb#include <sys/endian.h>
38291406Sjhb#include <sys/linker_set.h>
39291406Sjhb#include <gelf.h>
40291406Sjhb
41291406Sjhbstruct kvm_arch {
42291406Sjhb	int	(*ka_probe)(kvm_t *);
43291406Sjhb	int	(*ka_initvtop)(kvm_t *);
44291406Sjhb	void	(*ka_freevtop)(kvm_t *);
45291406Sjhb	int	(*ka_kvatop)(kvm_t *, kvaddr_t, off_t *);
46291406Sjhb	int	(*ka_native)(kvm_t *);
47291406Sjhb};
48291406Sjhb
49291406Sjhb#define	KVM_ARCH(ka)	DATA_SET(kvm_arch, ka)
50291406Sjhb
511573Srgrimesstruct __kvm {
52291406Sjhb	struct kvm_arch *arch;
531573Srgrimes	/*
541573Srgrimes	 * a string to be prepended to error messages
551573Srgrimes	 * provided for compatibility with sun's interface
561573Srgrimes	 * if this value is null, errors are saved in errbuf[]
571573Srgrimes	 */
581573Srgrimes	const char *program;
591573Srgrimes	char	*errp;		/* XXX this can probably go away */
601573Srgrimes	char	errbuf[_POSIX2_LINE_MAX];
611573Srgrimes#define ISALIVE(kd) ((kd)->vmfd >= 0)
621573Srgrimes	int	pmfd;		/* physical memory file (or crashdump) */
631573Srgrimes	int	vmfd;		/* virtual memory file (-1 if crashdump) */
641856Sdg	int	nlfd;		/* namelist file (e.g., /kernel) */
65291406Sjhb	GElf_Ehdr nlehdr;	/* ELF file header for namelist file */
66291406Sjhb	int	(*resolve_symbol)(const char *, kvaddr_t *);
671573Srgrimes	struct kinfo_proc *procbase;
681573Srgrimes	char	*argspc;	/* (dynamic) storage for argv strings */
691573Srgrimes	int	arglen;		/* length of the above */
701573Srgrimes	char	**argv;		/* (dynamic) storage for argv pointers */
711573Srgrimes	int	argc;		/* length of above (not actual # present) */
7212682Speter	char	*argbuf;	/* (dynamic) temporary storage */
731573Srgrimes	/*
741573Srgrimes	 * Kernel virtual address translation state.  This only gets filled
751573Srgrimes	 * in for dead kernels; otherwise, the running kernel (i.e. kmem)
761573Srgrimes	 * will do the translations for us.  It could be big, so we
771573Srgrimes	 * only allocate it if necessary.
781573Srgrimes	 */
791573Srgrimes	struct vmstate *vmst;
80170772Ssimokawa	int	rawdump;	/* raw dump format */
81312381Savg	int	writable;	/* physical memory is writable */
82195838Sbz
83195838Sbz	int		vnet_initialized;	/* vnet fields set up */
84291406Sjhb	kvaddr_t	vnet_start;	/* start of kernel's vnet region */
85291406Sjhb	kvaddr_t	vnet_stop;	/* stop of kernel's vnet region */
86291406Sjhb	kvaddr_t	vnet_current;	/* vnet we're working with */
87291406Sjhb	kvaddr_t	vnet_base;	/* vnet base of current vnet */
88204494Srwatson
89204494Srwatson	/*
90204494Srwatson	 * Dynamic per-CPU kernel memory.  We translate symbols, on-demand,
91204494Srwatson	 * to the data associated with dpcpu_curcpu, set with
92204494Srwatson	 * kvm_dpcpu_setcpu().
93204494Srwatson	 */
94204494Srwatson	int		dpcpu_initialized;	/* dpcpu fields set up */
95291406Sjhb	kvaddr_t	dpcpu_start;	/* start of kernel's dpcpu region */
96291406Sjhb	kvaddr_t	dpcpu_stop;	/* stop of kernel's dpcpu region */
97204494Srwatson	u_int		dpcpu_maxcpus;	/* size of base array */
98204494Srwatson	uintptr_t	*dpcpu_off;	/* base array, indexed by CPU ID */
99204494Srwatson	u_int		dpcpu_curcpu;	/* CPU we're currently working with */
100291406Sjhb	kvaddr_t	dpcpu_curoff;	/* dpcpu base of current CPU */
1011573Srgrimes};
1021573Srgrimes
1031573Srgrimes/*
104291406Sjhb * Page table hash used by minidump backends to map physical addresses
105291406Sjhb * to file offsets.
106291406Sjhb */
107291406Sjhbstruct hpte {
108291406Sjhb	struct hpte	*next;
109291406Sjhb	uint64_t	pa;
110291406Sjhb	off_t		off;
111291406Sjhb};
112291406Sjhb
113291406Sjhb#define HPT_SIZE 1024
114291406Sjhb
115291406Sjhbstruct hpt {
116291406Sjhb	struct hpte	*hpt_head[HPT_SIZE];
117291406Sjhb};
118291406Sjhb
119291406Sjhb/*
1201573Srgrimes * Functions used internally by kvm, but across kvm modules.
1211573Srgrimes */
122291406Sjhbstatic inline uint32_t
123291406Sjhb_kvm32toh(kvm_t *kd, uint32_t val)
124291406Sjhb{
125291406Sjhb
126291406Sjhb	if (kd->nlehdr.e_ident[EI_DATA] == ELFDATA2LSB)
127291406Sjhb		return (le32toh(val));
128291406Sjhb	else
129291406Sjhb		return (be32toh(val));
130291406Sjhb}
131291406Sjhb
132291406Sjhbstatic inline uint64_t
133291406Sjhb_kvm64toh(kvm_t *kd, uint64_t val)
134291406Sjhb{
135291406Sjhb
136291406Sjhb	if (kd->nlehdr.e_ident[EI_DATA] == ELFDATA2LSB)
137291406Sjhb		return (le64toh(val));
138291406Sjhb	else
139291406Sjhb		return (be64toh(val));
140291406Sjhb}
141291406Sjhb
14292941Sobrienvoid	 _kvm_err(kvm_t *kd, const char *program, const char *fmt, ...)
14392941Sobrien	    __printflike(3, 4);
14492917Sobrienvoid	 _kvm_freeprocs(kvm_t *kd);
14592917Sobrienvoid	*_kvm_malloc(kvm_t *kd, size_t);
146291406Sjhbint	 _kvm_nlist(kvm_t *, struct kvm_nlist *, int);
14792917Sobrienvoid	*_kvm_realloc(kvm_t *kd, void *, size_t);
14892941Sobrienvoid	 _kvm_syserr (kvm_t *kd, const char *program, const char *fmt, ...)
14992941Sobrien	    __printflike(3, 4);
150195838Sbzint	 _kvm_vnet_selectpid(kvm_t *, pid_t);
151195838Sbzint	 _kvm_vnet_initialized(kvm_t *, int);
152291406Sjhbkvaddr_t _kvm_vnet_validaddr(kvm_t *, kvaddr_t);
153204494Srwatsonint	 _kvm_dpcpu_initialized(kvm_t *, int);
154291406Sjhbkvaddr_t _kvm_dpcpu_validaddr(kvm_t *, kvaddr_t);
155291406Sjhbint	 _kvm_probe_elf_kernel(kvm_t *, int, int);
156291406Sjhbint	 _kvm_is_minidump(kvm_t *);
157291406Sjhbint	 _kvm_read_core_phdrs(kvm_t *, size_t *, GElf_Phdr **);
158291406Sjhbvoid	 _kvm_hpt_init(kvm_t *, struct hpt *, void *, size_t, off_t, int, int);
159291406Sjhboff_t	 _kvm_hpt_find(struct hpt *, uint64_t);
160291406Sjhbvoid	 _kvm_hpt_free(struct hpt *);
161