xref: /freebsd/lib/libkvm/kvm_private.h (revision b957b185946ed5ad96436b50bb118db8c1a24ded)
158f0484fSRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
458f0484fSRodney W. Grimes  * Copyright (c) 1992, 1993
558f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
658f0484fSRodney W. Grimes  *
758f0484fSRodney W. Grimes  * This code is derived from software developed by the Computer Systems
858f0484fSRodney W. Grimes  * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
958f0484fSRodney W. Grimes  * BG 91-66 and contributed to Berkeley.
1058f0484fSRodney W. Grimes  *
1158f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
1258f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
1358f0484fSRodney W. Grimes  * are met:
1458f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1558f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1658f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1758f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1858f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
19fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
2058f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
2158f0484fSRodney W. Grimes  *    without specific prior written permission.
2258f0484fSRodney W. Grimes  *
2358f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2458f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2558f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2658f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2758f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2858f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2958f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3058f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3158f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3258f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3358f0484fSRodney W. Grimes  * SUCH DAMAGE.
3458f0484fSRodney W. Grimes  *
3558f0484fSRodney W. Grimes  *	@(#)kvm_private.h	8.1 (Berkeley) 6/4/93
36c4a7cdb3SPeter Wemm  * $FreeBSD$
3758f0484fSRodney W. Grimes  */
3858f0484fSRodney W. Grimes 
397f911abeSJohn Baldwin #include <sys/endian.h>
407f911abeSJohn Baldwin #include <sys/linker_set.h>
417f911abeSJohn Baldwin #include <gelf.h>
427f911abeSJohn Baldwin 
437f911abeSJohn Baldwin struct kvm_arch {
447f911abeSJohn Baldwin 	int	(*ka_probe)(kvm_t *);
457f911abeSJohn Baldwin 	int	(*ka_initvtop)(kvm_t *);
467f911abeSJohn Baldwin 	void	(*ka_freevtop)(kvm_t *);
477f911abeSJohn Baldwin 	int	(*ka_kvatop)(kvm_t *, kvaddr_t, off_t *);
487f911abeSJohn Baldwin 	int	(*ka_native)(kvm_t *);
49c9057838SWill Andrews 	int	(*ka_walk_pages)(kvm_t *, kvm_walk_pages_cb_t *, void *);
5038cf2a43SLeandro Lupori 	kssize_t (*ka_kerndisp)(kvm_t *);
517f911abeSJohn Baldwin };
527f911abeSJohn Baldwin 
537f911abeSJohn Baldwin #define	KVM_ARCH(ka)	DATA_SET(kvm_arch, ka)
547f911abeSJohn Baldwin 
5558f0484fSRodney W. Grimes struct __kvm {
567f911abeSJohn Baldwin 	struct kvm_arch *arch;
5758f0484fSRodney W. Grimes 	/*
5858f0484fSRodney W. Grimes 	 * a string to be prepended to error messages
5958f0484fSRodney W. Grimes 	 * provided for compatibility with sun's interface
6058f0484fSRodney W. Grimes 	 * if this value is null, errors are saved in errbuf[]
6158f0484fSRodney W. Grimes 	 */
6258f0484fSRodney W. Grimes 	const char *program;
6358f0484fSRodney W. Grimes 	char	*errp;		/* XXX this can probably go away */
6458f0484fSRodney W. Grimes 	char	errbuf[_POSIX2_LINE_MAX];
6558f0484fSRodney W. Grimes #define ISALIVE(kd) ((kd)->vmfd >= 0)
6658f0484fSRodney W. Grimes 	int	pmfd;		/* physical memory file (or crashdump) */
6758f0484fSRodney W. Grimes 	int	vmfd;		/* virtual memory file (-1 if crashdump) */
68b3bfc719SDavid Greenman 	int	nlfd;		/* namelist file (e.g., /kernel) */
697f911abeSJohn Baldwin 	GElf_Ehdr nlehdr;	/* ELF file header for namelist file */
707f911abeSJohn Baldwin 	int	(*resolve_symbol)(const char *, kvaddr_t *);
7158f0484fSRodney W. Grimes 	struct kinfo_proc *procbase;
7258f0484fSRodney W. Grimes 	char	*argspc;	/* (dynamic) storage for argv strings */
7358f0484fSRodney W. Grimes 	int	arglen;		/* length of the above */
7458f0484fSRodney W. Grimes 	char	**argv;		/* (dynamic) storage for argv pointers */
7558f0484fSRodney W. Grimes 	int	argc;		/* length of above (not actual # present) */
7677721f53SPeter Wemm 	char	*argbuf;	/* (dynamic) temporary storage */
7758f0484fSRodney W. Grimes 	/*
7858f0484fSRodney W. Grimes 	 * Kernel virtual address translation state.  This only gets filled
7958f0484fSRodney W. Grimes 	 * in for dead kernels; otherwise, the running kernel (i.e. kmem)
8058f0484fSRodney W. Grimes 	 * will do the translations for us.  It could be big, so we
8158f0484fSRodney W. Grimes 	 * only allocate it if necessary.
8258f0484fSRodney W. Grimes 	 */
8358f0484fSRodney W. Grimes 	struct vmstate *vmst;
84d7dc9f76SHidetoshi Shimokawa 	int	rawdump;	/* raw dump format */
857502cc40SAndriy Gapon 	int	writable;	/* physical memory is writable */
867cf8b4b9SBjoern A. Zeeb 
877cf8b4b9SBjoern A. Zeeb 	int		vnet_initialized;	/* vnet fields set up */
887f911abeSJohn Baldwin 	kvaddr_t	vnet_start;	/* start of kernel's vnet region */
897f911abeSJohn Baldwin 	kvaddr_t	vnet_stop;	/* stop of kernel's vnet region */
907f911abeSJohn Baldwin 	kvaddr_t	vnet_current;	/* vnet we're working with */
917f911abeSJohn Baldwin 	kvaddr_t	vnet_base;	/* vnet base of current vnet */
92ccd8bad0SRobert Watson 
93ccd8bad0SRobert Watson 	/*
94ccd8bad0SRobert Watson 	 * Dynamic per-CPU kernel memory.  We translate symbols, on-demand,
95ccd8bad0SRobert Watson 	 * to the data associated with dpcpu_curcpu, set with
96ccd8bad0SRobert Watson 	 * kvm_dpcpu_setcpu().
97ccd8bad0SRobert Watson 	 */
98ccd8bad0SRobert Watson 	int		dpcpu_initialized;	/* dpcpu fields set up */
997f911abeSJohn Baldwin 	kvaddr_t	dpcpu_start;	/* start of kernel's dpcpu region */
1007f911abeSJohn Baldwin 	kvaddr_t	dpcpu_stop;	/* stop of kernel's dpcpu region */
101ccd8bad0SRobert Watson 	u_int		dpcpu_maxcpus;	/* size of base array */
102ccd8bad0SRobert Watson 	uintptr_t	*dpcpu_off;	/* base array, indexed by CPU ID */
103ccd8bad0SRobert Watson 	u_int		dpcpu_curcpu;	/* CPU we're currently working with */
1047f911abeSJohn Baldwin 	kvaddr_t	dpcpu_curoff;	/* dpcpu base of current CPU */
105ffdeef32SWill Andrews 
106ffdeef32SWill Andrews 	/* Page table lookup structures. */
107ffdeef32SWill Andrews 	uint64_t	*pt_map;
108ffdeef32SWill Andrews 	size_t		pt_map_size;
10900e66147SD Scott Phillips 	uint64_t	*dump_avail;	/* actually word sized */
11000e66147SD Scott Phillips 	size_t		dump_avail_size;
111ffdeef32SWill Andrews 	off_t		pt_sparse_off;
112ffdeef32SWill Andrews 	uint64_t	pt_sparse_size;
113ffdeef32SWill Andrews 	uint32_t	*pt_popcounts;
114ffdeef32SWill Andrews 	unsigned int	pt_page_size;
115c9057838SWill Andrews 
116c9057838SWill Andrews 	/* Page & sparse map structures. */
117c9057838SWill Andrews 	void		*page_map;
118c9057838SWill Andrews 	uint32_t	page_map_size;
119c9057838SWill Andrews 	off_t		page_map_off;
120c9057838SWill Andrews 	void		*sparse_map;
121c9057838SWill Andrews };
122c9057838SWill Andrews 
123c9057838SWill Andrews struct kvm_bitmap {
124c9057838SWill Andrews 	uint8_t *map;
125c9057838SWill Andrews 	u_long size;
1267f911abeSJohn Baldwin };
1277f911abeSJohn Baldwin 
128ffdeef32SWill Andrews /* Page table lookup constants. */
129ffdeef32SWill Andrews #define POPCOUNT_BITS	1024
130ffdeef32SWill Andrews #define BITS_IN(v)	(sizeof(v) * NBBY)
131ffdeef32SWill Andrews #define POPCOUNTS_IN(v)	(POPCOUNT_BITS / BITS_IN(v))
13258f0484fSRodney W. Grimes 
13358f0484fSRodney W. Grimes /*
13458f0484fSRodney W. Grimes  * Functions used internally by kvm, but across kvm modules.
13558f0484fSRodney W. Grimes  */
136f4eb39baSBrandon Bergren static inline uint16_t
137f4eb39baSBrandon Bergren _kvm16toh(kvm_t *kd, uint16_t val)
138f4eb39baSBrandon Bergren {
139f4eb39baSBrandon Bergren 
140f4eb39baSBrandon Bergren 	if (kd->nlehdr.e_ident[EI_DATA] == ELFDATA2LSB)
141f4eb39baSBrandon Bergren 		return (le16toh(val));
142f4eb39baSBrandon Bergren 	else
143f4eb39baSBrandon Bergren 		return (be16toh(val));
144f4eb39baSBrandon Bergren }
145f4eb39baSBrandon Bergren 
1467f911abeSJohn Baldwin static inline uint32_t
1477f911abeSJohn Baldwin _kvm32toh(kvm_t *kd, uint32_t val)
1487f911abeSJohn Baldwin {
1497f911abeSJohn Baldwin 
1507f911abeSJohn Baldwin 	if (kd->nlehdr.e_ident[EI_DATA] == ELFDATA2LSB)
1517f911abeSJohn Baldwin 		return (le32toh(val));
1527f911abeSJohn Baldwin 	else
1537f911abeSJohn Baldwin 		return (be32toh(val));
1547f911abeSJohn Baldwin }
1557f911abeSJohn Baldwin 
1567f911abeSJohn Baldwin static inline uint64_t
1577f911abeSJohn Baldwin _kvm64toh(kvm_t *kd, uint64_t val)
1587f911abeSJohn Baldwin {
1597f911abeSJohn Baldwin 
1607f911abeSJohn Baldwin 	if (kd->nlehdr.e_ident[EI_DATA] == ELFDATA2LSB)
1617f911abeSJohn Baldwin 		return (le64toh(val));
1627f911abeSJohn Baldwin 	else
1637f911abeSJohn Baldwin 		return (be64toh(val));
1647f911abeSJohn Baldwin }
1657f911abeSJohn Baldwin 
16600e66147SD Scott Phillips uint64_t _kvm_pa_bit_id(kvm_t *kd, uint64_t pa, unsigned int page_size);
16700e66147SD Scott Phillips uint64_t _kvm_bit_id_pa(kvm_t *kd, uint64_t bit_id, unsigned int page_size);
16800e66147SD Scott Phillips #define _KVM_PA_INVALID		ULONG_MAX
16900e66147SD Scott Phillips #define _KVM_BIT_ID_INVALID	ULONG_MAX
17000e66147SD Scott Phillips 
171c9057838SWill Andrews int	 _kvm_bitmap_init(struct kvm_bitmap *, u_long, u_long *);
17200e66147SD Scott Phillips void	 _kvm_bitmap_set(struct kvm_bitmap *, u_long);
173c9057838SWill Andrews int	 _kvm_bitmap_next(struct kvm_bitmap *, u_long *);
174c9057838SWill Andrews void	 _kvm_bitmap_deinit(struct kvm_bitmap *);
175c9057838SWill Andrews 
1761372519bSDavid E. O'Brien void	 _kvm_err(kvm_t *kd, const char *program, const char *fmt, ...)
1771372519bSDavid E. O'Brien 	    __printflike(3, 4);
17869160b1eSDavid E. O'Brien void	 _kvm_freeprocs(kvm_t *kd);
17969160b1eSDavid E. O'Brien void	*_kvm_malloc(kvm_t *kd, size_t);
1807f911abeSJohn Baldwin int	 _kvm_nlist(kvm_t *, struct kvm_nlist *, int);
18169160b1eSDavid E. O'Brien void	*_kvm_realloc(kvm_t *kd, void *, size_t);
1821372519bSDavid E. O'Brien void	 _kvm_syserr (kvm_t *kd, const char *program, const char *fmt, ...)
1831372519bSDavid E. O'Brien 	    __printflike(3, 4);
1847cf8b4b9SBjoern A. Zeeb int	 _kvm_vnet_selectpid(kvm_t *, pid_t);
1857cf8b4b9SBjoern A. Zeeb int	 _kvm_vnet_initialized(kvm_t *, int);
1867f911abeSJohn Baldwin kvaddr_t _kvm_vnet_validaddr(kvm_t *, kvaddr_t);
187ccd8bad0SRobert Watson int	 _kvm_dpcpu_initialized(kvm_t *, int);
1887f911abeSJohn Baldwin kvaddr_t _kvm_dpcpu_validaddr(kvm_t *, kvaddr_t);
1897f911abeSJohn Baldwin int	 _kvm_probe_elf_kernel(kvm_t *, int, int);
1907f911abeSJohn Baldwin int	 _kvm_is_minidump(kvm_t *);
1917f911abeSJohn Baldwin int	 _kvm_read_core_phdrs(kvm_t *, size_t *, GElf_Phdr **);
192*b957b185SMark Johnston int	 _kvm_pt_init(kvm_t *, size_t, off_t, size_t, off_t, off_t, int);
193c9057838SWill Andrews off_t	 _kvm_pt_find(kvm_t *, uint64_t, unsigned int);
194c9057838SWill Andrews int	 _kvm_visit_cb(kvm_t *, kvm_walk_pages_cb_t *, void *, u_long,
195c9057838SWill Andrews 	    u_long, u_long, vm_prot_t, size_t, unsigned int);
196c9057838SWill Andrews int	 _kvm_pmap_init(kvm_t *, uint32_t, off_t);
197c9057838SWill Andrews void *	 _kvm_pmap_get(kvm_t *, u_long, size_t);
198c9057838SWill Andrews void *	 _kvm_map_get(kvm_t *, u_long, unsigned int);
199