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
367f911abeSJohn Baldwin #include <sys/endian.h>
377f911abeSJohn Baldwin #include <sys/linker_set.h>
387f911abeSJohn Baldwin #include <gelf.h>
397f911abeSJohn Baldwin
407f911abeSJohn Baldwin struct kvm_arch {
417f911abeSJohn Baldwin int (*ka_probe)(kvm_t *);
427f911abeSJohn Baldwin int (*ka_initvtop)(kvm_t *);
437f911abeSJohn Baldwin void (*ka_freevtop)(kvm_t *);
447f911abeSJohn Baldwin int (*ka_kvatop)(kvm_t *, kvaddr_t, off_t *);
457f911abeSJohn Baldwin int (*ka_native)(kvm_t *);
46c9057838SWill Andrews int (*ka_walk_pages)(kvm_t *, kvm_walk_pages_cb_t *, void *);
4738cf2a43SLeandro Lupori kssize_t (*ka_kerndisp)(kvm_t *);
487f911abeSJohn Baldwin };
497f911abeSJohn Baldwin
507f911abeSJohn Baldwin #define KVM_ARCH(ka) DATA_SET(kvm_arch, ka)
517f911abeSJohn Baldwin
5258f0484fSRodney W. Grimes struct __kvm {
537f911abeSJohn Baldwin struct kvm_arch *arch;
5458f0484fSRodney W. Grimes /*
5558f0484fSRodney W. Grimes * a string to be prepended to error messages
5658f0484fSRodney W. Grimes * provided for compatibility with sun's interface
5758f0484fSRodney W. Grimes * if this value is null, errors are saved in errbuf[]
5858f0484fSRodney W. Grimes */
5958f0484fSRodney W. Grimes const char *program;
6058f0484fSRodney W. Grimes char *errp; /* XXX this can probably go away */
6158f0484fSRodney W. Grimes char errbuf[_POSIX2_LINE_MAX];
6258f0484fSRodney W. Grimes #define ISALIVE(kd) ((kd)->vmfd >= 0)
6358f0484fSRodney W. Grimes int pmfd; /* physical memory file (or crashdump) */
6458f0484fSRodney W. Grimes int vmfd; /* virtual memory file (-1 if crashdump) */
65b3bfc719SDavid Greenman int nlfd; /* namelist file (e.g., /kernel) */
667f911abeSJohn Baldwin GElf_Ehdr nlehdr; /* ELF file header for namelist file */
677f911abeSJohn Baldwin int (*resolve_symbol)(const char *, kvaddr_t *);
6858f0484fSRodney W. Grimes struct kinfo_proc *procbase;
6958f0484fSRodney W. Grimes char *argspc; /* (dynamic) storage for argv strings */
7058f0484fSRodney W. Grimes int arglen; /* length of the above */
7158f0484fSRodney W. Grimes char **argv; /* (dynamic) storage for argv pointers */
7258f0484fSRodney W. Grimes int argc; /* length of above (not actual # present) */
7377721f53SPeter Wemm char *argbuf; /* (dynamic) temporary storage */
7458f0484fSRodney W. Grimes /*
7558f0484fSRodney W. Grimes * Kernel virtual address translation state. This only gets filled
7658f0484fSRodney W. Grimes * in for dead kernels; otherwise, the running kernel (i.e. kmem)
7758f0484fSRodney W. Grimes * will do the translations for us. It could be big, so we
7858f0484fSRodney W. Grimes * only allocate it if necessary.
7958f0484fSRodney W. Grimes */
8058f0484fSRodney W. Grimes struct vmstate *vmst;
81d7dc9f76SHidetoshi Shimokawa int rawdump; /* raw dump format */
827502cc40SAndriy Gapon int writable; /* physical memory is writable */
837cf8b4b9SBjoern A. Zeeb
847cf8b4b9SBjoern A. Zeeb int vnet_initialized; /* vnet fields set up */
857f911abeSJohn Baldwin kvaddr_t vnet_start; /* start of kernel's vnet region */
867f911abeSJohn Baldwin kvaddr_t vnet_stop; /* stop of kernel's vnet region */
877f911abeSJohn Baldwin kvaddr_t vnet_current; /* vnet we're working with */
887f911abeSJohn Baldwin kvaddr_t vnet_base; /* vnet base of current vnet */
89ccd8bad0SRobert Watson
90ccd8bad0SRobert Watson /*
91ccd8bad0SRobert Watson * Dynamic per-CPU kernel memory. We translate symbols, on-demand,
92ccd8bad0SRobert Watson * to the data associated with dpcpu_curcpu, set with
93ccd8bad0SRobert Watson * kvm_dpcpu_setcpu().
94ccd8bad0SRobert Watson */
95ccd8bad0SRobert Watson int dpcpu_initialized; /* dpcpu fields set up */
967f911abeSJohn Baldwin kvaddr_t dpcpu_start; /* start of kernel's dpcpu region */
977f911abeSJohn Baldwin kvaddr_t dpcpu_stop; /* stop of kernel's dpcpu region */
98ccd8bad0SRobert Watson u_int dpcpu_maxcpus; /* size of base array */
99ccd8bad0SRobert Watson uintptr_t *dpcpu_off; /* base array, indexed by CPU ID */
100ccd8bad0SRobert Watson u_int dpcpu_curcpu; /* CPU we're currently working with */
1017f911abeSJohn Baldwin kvaddr_t dpcpu_curoff; /* dpcpu base of current CPU */
102ffdeef32SWill Andrews
103ffdeef32SWill Andrews /* Page table lookup structures. */
104ffdeef32SWill Andrews uint64_t *pt_map;
105ffdeef32SWill Andrews size_t pt_map_size;
10600e66147SD Scott Phillips uint64_t *dump_avail; /* actually word sized */
10700e66147SD Scott Phillips size_t dump_avail_size;
108ffdeef32SWill Andrews off_t pt_sparse_off;
109ffdeef32SWill Andrews uint64_t pt_sparse_size;
110ffdeef32SWill Andrews uint32_t *pt_popcounts;
111ffdeef32SWill Andrews unsigned int pt_page_size;
112c9057838SWill Andrews
113c9057838SWill Andrews /* Page & sparse map structures. */
114c9057838SWill Andrews void *page_map;
115c9057838SWill Andrews uint32_t page_map_size;
116c9057838SWill Andrews off_t page_map_off;
117c9057838SWill Andrews void *sparse_map;
118c9057838SWill Andrews };
119c9057838SWill Andrews
120c9057838SWill Andrews struct kvm_bitmap {
121c9057838SWill Andrews uint8_t *map;
122c9057838SWill Andrews u_long size;
1237f911abeSJohn Baldwin };
1247f911abeSJohn Baldwin
125ffdeef32SWill Andrews /* Page table lookup constants. */
126ffdeef32SWill Andrews #define POPCOUNT_BITS 1024
127ffdeef32SWill Andrews #define BITS_IN(v) (sizeof(v) * NBBY)
128ffdeef32SWill Andrews #define POPCOUNTS_IN(v) (POPCOUNT_BITS / BITS_IN(v))
12958f0484fSRodney W. Grimes
13058f0484fSRodney W. Grimes /*
13158f0484fSRodney W. Grimes * Functions used internally by kvm, but across kvm modules.
13258f0484fSRodney W. Grimes */
133f4eb39baSBrandon Bergren static inline uint16_t
_kvm16toh(kvm_t * kd,uint16_t val)134f4eb39baSBrandon Bergren _kvm16toh(kvm_t *kd, uint16_t val)
135f4eb39baSBrandon Bergren {
136f4eb39baSBrandon Bergren
137f4eb39baSBrandon Bergren if (kd->nlehdr.e_ident[EI_DATA] == ELFDATA2LSB)
138f4eb39baSBrandon Bergren return (le16toh(val));
139f4eb39baSBrandon Bergren else
140f4eb39baSBrandon Bergren return (be16toh(val));
141f4eb39baSBrandon Bergren }
142f4eb39baSBrandon Bergren
1437f911abeSJohn Baldwin static inline uint32_t
_kvm32toh(kvm_t * kd,uint32_t val)1447f911abeSJohn Baldwin _kvm32toh(kvm_t *kd, uint32_t val)
1457f911abeSJohn Baldwin {
1467f911abeSJohn Baldwin
1477f911abeSJohn Baldwin if (kd->nlehdr.e_ident[EI_DATA] == ELFDATA2LSB)
1487f911abeSJohn Baldwin return (le32toh(val));
1497f911abeSJohn Baldwin else
1507f911abeSJohn Baldwin return (be32toh(val));
1517f911abeSJohn Baldwin }
1527f911abeSJohn Baldwin
1537f911abeSJohn Baldwin static inline uint64_t
_kvm64toh(kvm_t * kd,uint64_t val)1547f911abeSJohn Baldwin _kvm64toh(kvm_t *kd, uint64_t val)
1557f911abeSJohn Baldwin {
1567f911abeSJohn Baldwin
1577f911abeSJohn Baldwin if (kd->nlehdr.e_ident[EI_DATA] == ELFDATA2LSB)
1587f911abeSJohn Baldwin return (le64toh(val));
1597f911abeSJohn Baldwin else
1607f911abeSJohn Baldwin return (be64toh(val));
1617f911abeSJohn Baldwin }
1627f911abeSJohn Baldwin
16300e66147SD Scott Phillips uint64_t _kvm_pa_bit_id(kvm_t *kd, uint64_t pa, unsigned int page_size);
16400e66147SD Scott Phillips uint64_t _kvm_bit_id_pa(kvm_t *kd, uint64_t bit_id, unsigned int page_size);
16500e66147SD Scott Phillips #define _KVM_PA_INVALID ULONG_MAX
16600e66147SD Scott Phillips #define _KVM_BIT_ID_INVALID ULONG_MAX
16700e66147SD Scott Phillips
168c9057838SWill Andrews int _kvm_bitmap_init(struct kvm_bitmap *, u_long, u_long *);
16900e66147SD Scott Phillips void _kvm_bitmap_set(struct kvm_bitmap *, u_long);
170c9057838SWill Andrews int _kvm_bitmap_next(struct kvm_bitmap *, u_long *);
171c9057838SWill Andrews void _kvm_bitmap_deinit(struct kvm_bitmap *);
172c9057838SWill Andrews
1731372519bSDavid E. O'Brien void _kvm_err(kvm_t *kd, const char *program, const char *fmt, ...)
1741372519bSDavid E. O'Brien __printflike(3, 4);
17569160b1eSDavid E. O'Brien void _kvm_freeprocs(kvm_t *kd);
17669160b1eSDavid E. O'Brien void *_kvm_malloc(kvm_t *kd, size_t);
1777f911abeSJohn Baldwin int _kvm_nlist(kvm_t *, struct kvm_nlist *, int);
17869160b1eSDavid E. O'Brien void *_kvm_realloc(kvm_t *kd, void *, size_t);
1791372519bSDavid E. O'Brien void _kvm_syserr (kvm_t *kd, const char *program, const char *fmt, ...)
1801372519bSDavid E. O'Brien __printflike(3, 4);
1817cf8b4b9SBjoern A. Zeeb int _kvm_vnet_selectpid(kvm_t *, pid_t);
1827cf8b4b9SBjoern A. Zeeb int _kvm_vnet_initialized(kvm_t *, int);
1837f911abeSJohn Baldwin kvaddr_t _kvm_vnet_validaddr(kvm_t *, kvaddr_t);
184ccd8bad0SRobert Watson int _kvm_dpcpu_initialized(kvm_t *, int);
1857f911abeSJohn Baldwin kvaddr_t _kvm_dpcpu_validaddr(kvm_t *, kvaddr_t);
1867f911abeSJohn Baldwin int _kvm_probe_elf_kernel(kvm_t *, int, int);
1877f911abeSJohn Baldwin int _kvm_is_minidump(kvm_t *);
1887f911abeSJohn Baldwin int _kvm_read_core_phdrs(kvm_t *, size_t *, GElf_Phdr **);
189*b957b185SMark Johnston int _kvm_pt_init(kvm_t *, size_t, off_t, size_t, off_t, off_t, int);
190c9057838SWill Andrews off_t _kvm_pt_find(kvm_t *, uint64_t, unsigned int);
191c9057838SWill Andrews int _kvm_visit_cb(kvm_t *, kvm_walk_pages_cb_t *, void *, u_long,
192c9057838SWill Andrews u_long, u_long, vm_prot_t, size_t, unsigned int);
193c9057838SWill Andrews int _kvm_pmap_init(kvm_t *, uint32_t, off_t);
194c9057838SWill Andrews void * _kvm_pmap_get(kvm_t *, u_long, size_t);
195c9057838SWill Andrews void * _kvm_map_get(kvm_t *, u_long, unsigned int);
196