1*104ea677Schristos /* $NetBSD: kvm_m68k.c,v 1.20 2022/01/10 19:51:30 christos Exp $ */
2346e67f8Sthorpej
354774f92Scgd /*-
495883471Sgwr * Copyright (c) 1997 The NetBSD Foundation, Inc.
595883471Sgwr * All rights reserved.
654774f92Scgd *
795883471Sgwr * This code is derived from software contributed to The NetBSD Foundation
8b6c0c9a2Sthorpej * by Gordon W. Ross and Jason R. Thorpe.
954774f92Scgd *
1054774f92Scgd * Redistribution and use in source and binary forms, with or without
1154774f92Scgd * modification, are permitted provided that the following conditions
1254774f92Scgd * are met:
1354774f92Scgd * 1. Redistributions of source code must retain the above copyright
1454774f92Scgd * notice, this list of conditions and the following disclaimer.
1554774f92Scgd * 2. Redistributions in binary form must reproduce the above copyright
1654774f92Scgd * notice, this list of conditions and the following disclaimer in the
1754774f92Scgd * documentation and/or other materials provided with the distribution.
1854774f92Scgd *
1995883471Sgwr * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2095883471Sgwr * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2195883471Sgwr * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2295883471Sgwr * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2395883471Sgwr * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2495883471Sgwr * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2595883471Sgwr * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2695883471Sgwr * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2795883471Sgwr * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2895883471Sgwr * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2995883471Sgwr * POSSIBILITY OF SUCH DAMAGE.
3054774f92Scgd */
3154774f92Scgd
3254774f92Scgd /*
3395883471Sgwr * Run-time kvm dispatcher for m68k machines.
3495883471Sgwr * The actual MD code is in the files:
3595883471Sgwr * kvm_m68k_cmn.c kvm_sun3.c ...
3604e1e909Sgwr *
3704e1e909Sgwr * Note: This file has to build on ALL m68k machines,
38f37e7bc5Sveego * so do NOT include any <machine/[*].h> files here.
3954774f92Scgd */
4054774f92Scgd
4154774f92Scgd #include <sys/param.h>
42f6385749Sgwr #include <sys/exec.h>
43b6c0c9a2Sthorpej #include <sys/kcore.h>
44f6385749Sgwr #include <sys/sysctl.h>
45962a341dSjym #include <sys/types.h>
469c2128ecSleo
4795883471Sgwr #include <stdio.h>
4895883471Sgwr #include <string.h>
4995883471Sgwr #include <stdlib.h>
509c2128ecSleo
5154774f92Scgd #include <unistd.h>
529c2128ecSleo #include <limits.h>
5354774f92Scgd #include <nlist.h>
5454774f92Scgd #include <kvm.h>
5554774f92Scgd #include <db.h>
5654774f92Scgd
5704e1e909Sgwr #include <m68k/kcore.h>
58b6c0c9a2Sthorpej
5954774f92Scgd #include "kvm_private.h"
6095883471Sgwr #include "kvm_m68k.h"
6154774f92Scgd
62*104ea677Schristos __RCSID("$NetBSD: kvm_m68k.c,v 1.20 2022/01/10 19:51:30 christos Exp $");
630deb66dfSmatt
6495883471Sgwr struct name_ops {
65b6c0c9a2Sthorpej const char *name;
6695883471Sgwr struct kvm_ops *ops;
6795883471Sgwr };
6854774f92Scgd
69b6c0c9a2Sthorpej /*
70b6c0c9a2Sthorpej * Match specific kcore types first, falling into a default.
71b6c0c9a2Sthorpej */
7295883471Sgwr static struct name_ops optbl[] = {
73f82a63dfSfredette { "sun2", &_kvm_ops_sun2 },
7495883471Sgwr { "sun3", &_kvm_ops_sun3 },
7595883471Sgwr { "sun3x", &_kvm_ops_sun3x },
76b6c0c9a2Sthorpej { NULL, &_kvm_ops_cmn },
7795883471Sgwr };
7895883471Sgwr
7995883471Sgwr /*
8095883471Sgwr * Prepare for translation of kernel virtual addresses into offsets
8195883471Sgwr * into crash dump files. This is where we do the dispatch work.
8295883471Sgwr */
8395883471Sgwr int
_kvm_initvtop(kvm_t * kd)846dc46b92Sjym _kvm_initvtop(kvm_t *kd)
8595883471Sgwr {
86b6c0c9a2Sthorpej cpu_kcore_hdr_t *h;
8795883471Sgwr struct name_ops *nop;
88b6c0c9a2Sthorpej struct vmstate *vm;
8995883471Sgwr
90b6c0c9a2Sthorpej vm = (struct vmstate *)_kvm_malloc(kd, sizeof (*vm));
91b6c0c9a2Sthorpej if (vm == 0)
9295883471Sgwr return (-1);
9395883471Sgwr
94b6c0c9a2Sthorpej kd->vmst = vm;
9595883471Sgwr
96b6c0c9a2Sthorpej /*
97b6c0c9a2Sthorpej * Use the machine name in the kcore header to determine
98b6c0c9a2Sthorpej * our ops vector. When we reach an ops vector with
99b6c0c9a2Sthorpej * no name, we've found a default.
100b6c0c9a2Sthorpej */
101b6c0c9a2Sthorpej h = kd->cpu_data;
102b6c0c9a2Sthorpej h->name[sizeof(h->name) - 1] = '\0'; /* sanity */
103b6c0c9a2Sthorpej for (nop = optbl; nop->name != NULL; nop++)
104b6c0c9a2Sthorpej if (strcmp(nop->name, h->name) == 0)
105b6c0c9a2Sthorpej break;
106b6c0c9a2Sthorpej
107b6c0c9a2Sthorpej vm->ops = nop->ops;
108b6c0c9a2Sthorpej
109b6c0c9a2Sthorpej /*
110b6c0c9a2Sthorpej * Compute pgshift and pgofset.
111b6c0c9a2Sthorpej */
112b6c0c9a2Sthorpej for (vm->pgshift = 0; (1 << vm->pgshift) < h->page_size; vm->pgshift++)
113b6c0c9a2Sthorpej /* nothing */ ;
114b6c0c9a2Sthorpej if ((1 << vm->pgshift) != h->page_size)
115b6c0c9a2Sthorpej goto bad;
116b6c0c9a2Sthorpej vm->pgofset = h->page_size - 1;
117b6c0c9a2Sthorpej
118b6c0c9a2Sthorpej if ((vm->ops->initvtop)(kd) < 0)
119b6c0c9a2Sthorpej goto bad;
120b6c0c9a2Sthorpej
121b6c0c9a2Sthorpej return (0);
122b6c0c9a2Sthorpej
123b6c0c9a2Sthorpej bad:
124b6c0c9a2Sthorpej kd->vmst = NULL;
125b6c0c9a2Sthorpej free(vm);
126b6c0c9a2Sthorpej return (-1);
12795883471Sgwr }
12854774f92Scgd
12954774f92Scgd void
_kvm_freevtop(kvm_t * kd)1306dc46b92Sjym _kvm_freevtop(kvm_t *kd)
13154774f92Scgd {
132b6c0c9a2Sthorpej (kd->vmst->ops->freevtop)(kd);
133b6c0c9a2Sthorpej free(kd->vmst);
13454774f92Scgd }
13554774f92Scgd
13654774f92Scgd int
_kvm_kvatop(kvm_t * kd,vaddr_t va,paddr_t * pap)137962a341dSjym _kvm_kvatop(kvm_t *kd, vaddr_t va, paddr_t *pap)
13854774f92Scgd {
139b6c0c9a2Sthorpej return ((kd->vmst->ops->kvatop)(kd, va, pap));
14082118b75Sgwr }
14182118b75Sgwr
14282118b75Sgwr off_t
_kvm_pa2off(kvm_t * kd,paddr_t pa)143962a341dSjym _kvm_pa2off(kvm_t *kd, paddr_t pa)
14482118b75Sgwr {
145b6c0c9a2Sthorpej return ((kd->vmst->ops->pa2off)(kd, pa));
14654774f92Scgd }
147f6385749Sgwr
148f6385749Sgwr /*
149f6385749Sgwr * Machine-dependent initialization for ALL open kvm descriptors,
150f6385749Sgwr * not just those for a kernel crash dump. Some architectures
151f6385749Sgwr * have to deal with these NOT being constants! (i.e. m68k)
152f6385749Sgwr */
153f6385749Sgwr int
_kvm_mdopen(kvm_t * kd)1546dc46b92Sjym _kvm_mdopen(kvm_t *kd)
155f6385749Sgwr {
156f6385749Sgwr u_long max_uva;
157f6385749Sgwr extern struct ps_strings *__ps_strings;
158f6385749Sgwr
159f6385749Sgwr #if 0 /* XXX - These vary across m68k machines... */
160f6385749Sgwr kd->min_uva = VM_MIN_ADDRESS;
161f6385749Sgwr kd->max_uva = VM_MAXUSER_ADDRESS;
162f6385749Sgwr #endif
163f6385749Sgwr /* This is somewhat hack-ish, but it works. */
164f6385749Sgwr max_uva = (u_long) (__ps_strings + 1);
165f6385749Sgwr kd->max_uva = max_uva;
166f6385749Sgwr kd->min_uva = 0;
167f6385749Sgwr
168f6385749Sgwr return (0);
169f6385749Sgwr }
170