xref: /netbsd-src/lib/libkvm/kvm_arm.c (revision b5677b36047b601b9addaaa494a58ceae82c2a6c)
1 /* $NetBSD: kvm_arm.c,v 1.4 2008/01/15 13:57:42 ad Exp $	 */
2 
3 /*-
4  * Copyright (C) 1996 Wolfgang Solfrank.
5  * Copyright (C) 1996 TooLs GmbH.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by TooLs GmbH.
19  * 4. The name of TooLs GmbH may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  *	from: kvm_powerpc.c,v 1.3 1997/09/19 04:00:23 thorpej Exp
34  */
35 
36 /*
37  * arm32 machine dependent routines for kvm.
38  */
39 
40 #include <sys/cdefs.h>
41 #if defined(LIBC_SCCS) && !defined(lint)
42 __RCSID("$NetBSD: kvm_arm.c,v 1.4 2008/01/15 13:57:42 ad Exp $");
43 #endif				/* LIBC_SCCS and not lint */
44 
45 #include <sys/param.h>
46 #include <sys/exec.h>
47 #include <sys/kcore.h>
48 #include <arm/kcore.h>
49 #include <arm/arm32/pte.h>
50 
51 #include <stdlib.h>
52 #include <db.h>
53 #include <limits.h>
54 #include <kvm.h>
55 
56 #include <unistd.h>
57 
58 #include "kvm_private.h"
59 
60 void
61 _kvm_freevtop(kvm_t * kd)
62 {
63 	if (kd->vmst != 0)
64 		free(kd->vmst);
65 }
66 
67 int
68 _kvm_initvtop(kvm_t * kd)
69 {
70 	return 0;
71 }
72 
73 int
74 _kvm_kvatop(kvm_t * kd, u_long va, u_long * pa)
75 {
76 	cpu_kcore_hdr_t *cpu_kh;
77 	pd_entry_t      pde;
78 	pt_entry_t      pte;
79 	uint32_t        pde_pa, pte_pa;
80 
81 	if (ISALIVE(kd)) {
82 		_kvm_err(kd, 0, "vatop called in live kernel!");
83 		return (0);
84 	}
85 	cpu_kh = kd->cpu_data;
86 
87 	if (cpu_kh->version != 1) {
88 		_kvm_err(kd, 0, "unsupported kcore structure version");
89 		return 0;
90 	}
91 	if (cpu_kh->flags != 0) {
92 		_kvm_err(kd, 0, "kcore flags not supported");
93 		return 0;
94 	}
95 	/*
96 	 * work out which L1 table we need
97 	 */
98 	if (va >= (cpu_kh->UserL1TableSize << 17))
99 		pde_pa = cpu_kh->PAKernelL1Table;
100 	else
101 		pde_pa = cpu_kh->PAUserL1Table;
102 
103 	/*
104 	 * work out the offset into the L1 Table
105 	 */
106 	pde_pa += ((va >> 20) * sizeof(pd_entry_t));
107 
108 	if (_kvm_pread(kd, kd->pmfd, (void *) &pde, sizeof(pd_entry_t),
109 		  _kvm_pa2off(kd, pde_pa)) != sizeof(pd_entry_t)) {
110 		_kvm_syserr(kd, 0, "could not read L1 entry");
111 		return (0);
112 	}
113 	/*
114 	 * next work out what kind of record it is
115 	 */
116 	switch (pde & L1_TYPE_MASK) {
117 	case L1_TYPE_S:
118 		*pa = (pde & L1_S_FRAME) | (va & L1_S_OFFSET);
119 		return L1_S_SIZE - (va & L1_S_OFFSET);
120 	case L1_TYPE_C:
121 		pte_pa = (pde & L1_C_ADDR_MASK)
122 			| ((va & 0xff000) >> 10);
123 		break;
124 	case L1_TYPE_F:
125 		pte_pa = (pde & L1_S_ADDR_MASK)
126 			| ((va & 0xffc00) >> 8);
127 		break;
128 	default:
129 		_kvm_syserr(kd, 0, "L1 entry is invalid");
130 		return (0);
131 	}
132 
133 	/*
134 	 * locate the pte and load it
135 	 */
136 	if (_kvm_pread(kd, kd->pmfd, (void *) &pte, sizeof(pt_entry_t),
137 		  _kvm_pa2off(kd, pte_pa)) != sizeof(pt_entry_t)) {
138 		_kvm_syserr(kd, 0, "could not read L2 entry");
139 		return (0);
140 	}
141 	switch (pte & L2_TYPE_MASK) {
142 	case L2_TYPE_L:
143 		*pa = (pte & L2_L_FRAME) | (va & L2_L_OFFSET);
144 		return (L2_L_SIZE - (va & L2_L_OFFSET));
145 	case L2_TYPE_S:
146 		*pa = (pte & L2_S_FRAME) | (va & L2_S_OFFSET);
147 		return (L2_S_SIZE - (va & L2_S_OFFSET));
148 	case L2_TYPE_T:
149 		*pa = (pte & L2_T_FRAME) | (va & L2_T_OFFSET);
150 		return (L2_T_SIZE - (va & L2_T_OFFSET));
151 	default:
152 		_kvm_syserr(kd, 0, "L2 entry is invalid");
153 		return (0);
154 	}
155 
156 	_kvm_err(kd, 0, "vatop not yet implemented!");
157 	return 0;
158 }
159 
160 off_t
161 _kvm_pa2off(kvm_t * kd, u_long pa)
162 {
163 	cpu_kcore_hdr_t *cpu_kh;
164 	phys_ram_seg_t *ramsegs;
165 	off_t           off;
166 	int             i;
167 
168 	cpu_kh = kd->cpu_data;
169 	ramsegs = (void *) ((char *) (void *) cpu_kh + cpu_kh->omemsegs);
170 
171 	off = 0;
172 	for (i = 0; i < cpu_kh->nmemsegs; i++) {
173 		if (pa >= ramsegs[i].start &&
174 		    (pa - ramsegs[i].start) < ramsegs[i].size) {
175 			off += (pa - ramsegs[i].start);
176 			break;
177 		}
178 		off += ramsegs[i].size;
179 	}
180 	return (kd->dump_off + off);
181 }
182 
183 /*
184  * Machine-dependent initialization for ALL open kvm descriptors,
185  * not just those for a kernel crash dump.  Some architectures
186  * have to deal with these NOT being constants!  (i.e. arm)
187  */
188 int
189 _kvm_mdopen(kvm_t * kd)
190 {
191 	uintptr_t       max_uva;
192 	extern struct ps_strings *__ps_strings;
193 
194 #if 0				/* XXX - These vary across arm machines... */
195 	kd->usrstack = USRSTACK;
196 	kd->min_uva = VM_MIN_ADDRESS;
197 	kd->max_uva = VM_MAXUSER_ADDRESS;
198 #endif
199 	/* This is somewhat hack-ish, but it works. */
200 	max_uva = (uintptr_t) (__ps_strings + 1);
201 	kd->usrstack = max_uva;
202 	kd->max_uva = max_uva;
203 	kd->min_uva = 0;
204 
205 	return (0);
206 }
207