xref: /netbsd-src/lib/libkvm/kvm_m68k.c (revision 6a9b3088d8d2341ca1454531d365c15fe9c1c589)
1 /*	$NetBSD: kvm_m68k.c,v 1.18 2010/09/20 23:23:16 jym Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Gordon W. Ross and Jason R. Thorpe.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Run-time kvm dispatcher for m68k machines.
34  * The actual MD code is in the files:
35  * kvm_m68k_cmn.c kvm_sun3.c ...
36  *
37  * Note: This file has to build on ALL m68k machines,
38  * so do NOT include any <machine/[*].h> files here.
39  */
40 
41 #include <sys/param.h>
42 #include <sys/exec.h>
43 #include <sys/kcore.h>
44 #include <sys/sysctl.h>
45 #include <sys/types.h>
46 
47 #include <stdio.h>
48 #include <string.h>
49 #include <stdlib.h>
50 
51 #include <unistd.h>
52 #include <limits.h>
53 #include <nlist.h>
54 #include <kvm.h>
55 #include <db.h>
56 
57 #include <m68k/kcore.h>
58 
59 #include "kvm_private.h"
60 #include "kvm_m68k.h"
61 
62 struct name_ops {
63 	const char *name;
64 	struct kvm_ops *ops;
65 };
66 
67 /*
68  * Match specific kcore types first, falling into a default.
69  */
70 static struct name_ops optbl[] = {
71 	{ "sun2",	&_kvm_ops_sun2 },
72 	{ "sun3",	&_kvm_ops_sun3 },
73 	{ "sun3x",	&_kvm_ops_sun3x },
74 	{ NULL,		&_kvm_ops_cmn },
75 };
76 
77 /*
78  * Prepare for translation of kernel virtual addresses into offsets
79  * into crash dump files.  This is where we do the dispatch work.
80  */
81 int
82 _kvm_initvtop(kvm_t *kd)
83 {
84 	cpu_kcore_hdr_t *h;
85 	struct name_ops *nop;
86 	struct vmstate *vm;
87 
88 	vm = (struct vmstate *)_kvm_malloc(kd, sizeof (*vm));
89 	if (vm == 0)
90 		return (-1);
91 
92 	kd->vmst = vm;
93 
94 	/*
95 	 * Use the machine name in the kcore header to determine
96 	 * our ops vector.  When we reach an ops vector with
97 	 * no name, we've found a default.
98 	 */
99 	h = kd->cpu_data;
100 	h->name[sizeof(h->name) - 1] = '\0';	/* sanity */
101 	for (nop = optbl; nop->name != NULL; nop++)
102 		if (strcmp(nop->name, h->name) == 0)
103 			break;
104 
105 	vm->ops = nop->ops;
106 
107 	/*
108 	 * Compute pgshift and pgofset.
109 	 */
110 	for (vm->pgshift = 0; (1 << vm->pgshift) < h->page_size; vm->pgshift++)
111 		/* nothing */ ;
112 	if ((1 << vm->pgshift) != h->page_size)
113 		goto bad;
114 	vm->pgofset = h->page_size - 1;
115 
116 	if ((vm->ops->initvtop)(kd) < 0)
117 		goto bad;
118 
119 	return (0);
120 
121  bad:
122 	kd->vmst = NULL;
123 	free(vm);
124 	return (-1);
125 }
126 
127 void
128 _kvm_freevtop(kvm_t *kd)
129 {
130 	(kd->vmst->ops->freevtop)(kd);
131 	free(kd->vmst);
132 }
133 
134 int
135 _kvm_kvatop(kvm_t *kd, vaddr_t va, paddr_t *pap)
136 {
137 	return ((kd->vmst->ops->kvatop)(kd, va, pap));
138 }
139 
140 off_t
141 _kvm_pa2off(kvm_t *kd, paddr_t pa)
142 {
143 	return ((kd->vmst->ops->pa2off)(kd, pa));
144 }
145 
146 /*
147  * Machine-dependent initialization for ALL open kvm descriptors,
148  * not just those for a kernel crash dump.  Some architectures
149  * have to deal with these NOT being constants!  (i.e. m68k)
150  */
151 int
152 _kvm_mdopen(kvm_t *kd)
153 {
154 	u_long max_uva;
155 	extern struct ps_strings *__ps_strings;
156 
157 #if 0	/* XXX - These vary across m68k machines... */
158 	kd->usrstack = USRSTACK;
159 	kd->min_uva = VM_MIN_ADDRESS;
160 	kd->max_uva = VM_MAXUSER_ADDRESS;
161 #endif
162 	/* This is somewhat hack-ish, but it works. */
163 	max_uva = (u_long) (__ps_strings + 1);
164 	kd->usrstack = max_uva;
165 	kd->max_uva  = max_uva;
166 	kd->min_uva  = 0;
167 
168 	return (0);
169 }
170