db_machdep.c revision 1.24.12.3 1 /* $NetBSD: db_machdep.c,v 1.24.12.3 2020/04/21 18:42:03 martin Exp $ */
2
3 /*
4 * Copyright (c) 1996 Mark Brinicombe
5 *
6 * Mach Operating System
7 * Copyright (c) 1991,1990 Carnegie Mellon University
8 * All Rights Reserved.
9 *
10 * Permission to use, copy, modify and distribute this software and its
11 * documentation is hereby granted, provided that both the copyright
12 * notice and this permission notice appear in all copies of the
13 * software, derivative works or modified versions, and any portions
14 * thereof, and that both notices appear in supporting documentation.
15 *
16 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
17 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
18 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
19 *
20 * Carnegie Mellon requests users of this software to return to
21 *
22 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
23 * School of Computer Science
24 * Carnegie Mellon University
25 * Pittsburgh PA 15213-3890
26 *
27 * any improvements or extensions that they make and grant Carnegie the
28 * rights to redistribute these changes.
29 */
30
31 #ifdef _KERNEL_OPT
32 #include "opt_cputypes.h"
33 #include "opt_multiprocessor.h"
34 #endif
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: db_machdep.c,v 1.24.12.3 2020/04/21 18:42:03 martin Exp $");
38
39 #include <sys/param.h>
40 #include <sys/cpu.h>
41 #include <sys/proc.h>
42 #include <sys/vnode.h>
43 #include <sys/systm.h>
44
45 #include <arm/arm32/db_machdep.h>
46 #include <arm/cpufunc.h>
47
48 #include <ddb/db_access.h>
49 #include <ddb/db_sym.h>
50 #include <ddb/db_output.h>
51 #include <ddb/db_variables.h>
52 #include <ddb/db_command.h>
53 #include <ddb/db_run.h>
54
55 #ifndef _KERNEL
56 #include <stddef.h>
57 #endif
58
59 #ifdef _KERNEL
60 static long nil;
61
62 void db_md_cpuinfo_cmd(db_expr_t, bool, db_expr_t, const char *);
63
64 int db_access_und_sp(const struct db_variable *, db_expr_t *, int);
65 int db_access_abt_sp(const struct db_variable *, db_expr_t *, int);
66 int db_access_irq_sp(const struct db_variable *, db_expr_t *, int);
67 #endif
68
69 static int
70 ddb_reg_var(const struct db_variable *v, db_expr_t *ep, int op)
71 {
72 register_t * const rp = (register_t *)DDB_REGS;
73 if (op == DB_VAR_SET) {
74 rp[(uintptr_t)v->valuep] = *ep;
75 } else {
76 *ep = rp[(uintptr_t)v->valuep];
77 }
78 return 0;
79 }
80
81
82 #define XO(f) ((long *)(offsetof(db_regs_t, f) / sizeof(register_t)))
83 const struct db_variable db_regs[] = {
84 { "spsr", XO(tf_spsr), ddb_reg_var, NULL },
85 { "r0", XO(tf_r0), ddb_reg_var, NULL },
86 { "r1", XO(tf_r1), ddb_reg_var, NULL },
87 { "r2", XO(tf_r2), ddb_reg_var, NULL },
88 { "r3", XO(tf_r3), ddb_reg_var, NULL },
89 { "r4", XO(tf_r4), ddb_reg_var, NULL },
90 { "r5", XO(tf_r5), ddb_reg_var, NULL },
91 { "r6", XO(tf_r6), ddb_reg_var, NULL },
92 { "r7", XO(tf_r7), ddb_reg_var, NULL },
93 { "r8", XO(tf_r8), ddb_reg_var, NULL },
94 { "r9", XO(tf_r9), ddb_reg_var, NULL },
95 { "r10", XO(tf_r10), ddb_reg_var, NULL },
96 { "r11", XO(tf_r11), ddb_reg_var, NULL },
97 { "r12", XO(tf_r12), ddb_reg_var, NULL },
98 { "usr_sp", XO(tf_usr_sp), ddb_reg_var, NULL },
99 { "usr_lr", XO(tf_usr_lr), ddb_reg_var, NULL },
100 { "svc_sp", XO(tf_svc_sp), ddb_reg_var, NULL },
101 { "svc_lr", XO(tf_svc_lr), ddb_reg_var, NULL },
102 { "pc", XO(tf_pc), ddb_reg_var, NULL },
103 #ifdef _KERNEL
104 { "und_sp", &nil, db_access_und_sp, NULL },
105 { "abt_sp", &nil, db_access_abt_sp, NULL },
106 { "irq_sp", &nil, db_access_irq_sp, NULL },
107 #endif
108 };
109 #undef XO
110
111 const struct db_variable * const db_eregs = db_regs + sizeof(db_regs)/sizeof(db_regs[0]);
112
113 const struct db_command db_machine_command_table[] = {
114 #ifdef _KERNEL
115 #if defined(MULTIPROCESSOR)
116 { DDB_ADD_CMD("cpu", db_switch_cpu_cmd, 0,
117 "switch to a different cpu",
118 NULL,NULL) },
119 #endif /* MULTIPROCESSOR */
120 { DDB_ADD_CMD("cpuinfo", db_md_cpuinfo_cmd, 0,
121 "Displays the cpuinfo",
122 NULL, NULL)
123 },
124 { DDB_ADD_CMD("fault", db_show_fault_cmd, 0,
125 "Displays the fault registers",
126 NULL,NULL) },
127 #endif
128 { DDB_ADD_CMD("frame", db_show_frame_cmd, 0,
129 "Displays the contents of a trapframe",
130 "[address]",
131 " address:\taddress of trapfame to display")},
132 #ifdef _KERNEL
133 #if defined(CPU_CORTEXA5) || defined(CPU_CORTEXA7)
134 { DDB_ADD_CMD("tlb", db_show_tlb_cmd, 0,
135 "Displays the TLB",
136 NULL,NULL) },
137 #endif
138 #endif /* _KERNEL */
139
140 { DDB_ADD_CMD(NULL, NULL, 0,NULL,NULL,NULL) }
141 };
142
143 void
144 db_show_frame_cmd(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif)
145 {
146 struct trapframe *frame;
147
148 if (!have_addr) {
149 db_printf("frame address must be specified\n");
150 return;
151 }
152
153 frame = (struct trapframe *)addr;
154
155 db_printf("frame address = %08x ", (u_int)frame);
156 db_printf("spsr=%08x\n", frame->tf_spsr);
157 db_printf("r0 =%08x r1 =%08x r2 =%08x r3 =%08x\n",
158 frame->tf_r0, frame->tf_r1, frame->tf_r2, frame->tf_r3);
159 db_printf("r4 =%08x r5 =%08x r6 =%08x r7 =%08x\n",
160 frame->tf_r4, frame->tf_r5, frame->tf_r6, frame->tf_r7);
161 db_printf("r8 =%08x r9 =%08x r10=%08x r11=%08x\n",
162 frame->tf_r8, frame->tf_r9, frame->tf_r10, frame->tf_r11);
163 db_printf("r12=%08x r13=%08x r14=%08x r15=%08x\n",
164 frame->tf_r12, frame->tf_usr_sp, frame->tf_usr_lr, frame->tf_pc);
165 db_printf("slr=%08x ssp=%08x\n", frame->tf_svc_lr, frame->tf_svc_sp);
166 }
167
168 #ifdef _KERNEL
169 int
170 db_access_und_sp(const struct db_variable *vp, db_expr_t *valp, int rw)
171 {
172
173 if (rw == DB_VAR_GET)
174 *valp = get_stackptr(PSR_UND32_MODE);
175 return(0);
176 }
177
178 int
179 db_access_abt_sp(const struct db_variable *vp, db_expr_t *valp, int rw)
180 {
181
182 if (rw == DB_VAR_GET)
183 *valp = get_stackptr(PSR_ABT32_MODE);
184 return(0);
185 }
186
187 int
188 db_access_irq_sp(const struct db_variable *vp, db_expr_t *valp, int rw)
189 {
190
191 if (rw == DB_VAR_GET)
192 *valp = get_stackptr(PSR_IRQ32_MODE);
193 return(0);
194 }
195
196 void
197 db_show_fault_cmd(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif)
198 {
199 db_printf("DFAR=%#x DFSR=%#x IFAR=%#x IFSR=%#x\n",
200 armreg_dfar_read(), armreg_dfsr_read(),
201 armreg_ifar_read(), armreg_ifsr_read());
202 db_printf("CONTEXTIDR=%#x TTBCR=%#x TTBR=%#x\n",
203 armreg_contextidr_read(), armreg_ttbcr_read(),
204 armreg_ttbr_read());
205 }
206
207 #if defined(CPU_CORTEXA5) || defined(CPU_CORTEXA7)
208 static void
209 tlb_print_common_header(const char *str)
210 {
211 db_printf("-W/I-- ----VA---- ----PA---- --SIZE-- D AP XN ASD %s\n", str);
212 }
213
214 static void
215 tlb_print_addr(size_t way, size_t va_index, vaddr_t vpn, paddr_t pfn)
216 {
217 db_printf("[%1zu:%02zx] 0x%05lx000 0x%05lx000", way, va_index, vpn, pfn);
218 }
219
220 static void
221 tlb_print_size_domain_prot(const char *sizestr, u_int domain, u_int ap,
222 bool xn_p)
223 {
224 db_printf(" %8s %1x %2d %s", sizestr, domain, ap, (xn_p ? "XN" : "--"));
225 }
226
227 static void
228 tlb_print_asid(bool ng_p, tlb_asid_t asid)
229 {
230 if (ng_p) {
231 db_printf(" %3d", asid);
232 } else {
233 db_printf(" ---");
234 }
235 }
236
237 struct db_tlbinfo {
238 vaddr_t (*dti_decode_vpn)(size_t, uint32_t, uint32_t);
239 void (*dti_print_header)(void);
240 void (*dti_print_entry)(size_t, size_t, uint32_t, uint32_t);
241 u_int dti_index;
242 };
243
244 #if defined(CPU_CORTEXA5)
245 static void
246 tlb_print_cortex_a5_header(void)
247 {
248 tlb_print_common_header(" S TEX C B");
249 }
250
251 static vaddr_t
252 tlb_decode_cortex_a5_vpn(size_t va_index, uint32_t d0, uint32_t d1)
253 {
254 const uint64_t d = ((uint64_t)d1 << 32) | d0;
255
256 const u_int size = __SHIFTOUT(d, ARM_A5_TLBDATA_SIZE);
257 return __SHIFTOUT(d, ARM_A5_TLBDATA_VA) * (ARM_A5_TLBDATAOP_INDEX + 1)
258 + (va_index << (4*size));
259 }
260
261 static void
262 tlb_print_cortex_a5_entry(size_t way, size_t va_index, uint32_t d0, uint32_t d1)
263 {
264 static const char size_strings[4][8] = {
265 " 4KB ", " 64KB ", " 1MB ", " 16MB ",
266 };
267
268 const uint64_t d = ((uint64_t)d1 << 32) | d0;
269
270 const paddr_t pfn = __SHIFTOUT(d, ARM_A5_TLBDATA_PA);
271 const vaddr_t vpn = tlb_decode_cortex_a5_vpn(va_index, d0, d1);
272
273 tlb_print_addr(way, va_index, vpn, pfn);
274
275 const u_int size = __SHIFTOUT(d, ARM_A5_TLBDATA_SIZE);
276 const u_int domain = __SHIFTOUT(d, ARM_A5_TLBDATA_DOM);
277 const u_int ap = __SHIFTOUT(d, ARM_A5_TLBDATA_AP);
278 const bool xn_p = (d & ARM_A5_TLBDATA_XN) != 0;
279
280 tlb_print_size_domain_prot(size_strings[size], domain, ap, xn_p);
281
282 const bool ng_p = (d & ARM_A5_TLBDATA_nG) != 0;
283 const tlb_asid_t asid = __SHIFTOUT(d, ARM_A5_TLBDATA_ASID);
284
285 tlb_print_asid(ng_p, asid);
286
287 const u_int tex = __SHIFTOUT(d, ARM_A5_TLBDATA_TEX);
288 const bool c_p = (d & ARM_A5_TLBDATA_C) != 0;
289 const bool b_p = (d & ARM_A5_TLBDATA_B) != 0;
290 const bool s_p = (d & ARM_A5_TLBDATA_S) != 0;
291
292 db_printf(" %c %d %c %c\n", (s_p ? 'S' : '-'), tex,
293 (c_p ? 'C' : '-'), (b_p ? 'B' : '-'));
294 }
295
296 static const struct db_tlbinfo tlb_cortex_a5_info = {
297 .dti_decode_vpn = tlb_decode_cortex_a5_vpn,
298 .dti_print_header = tlb_print_cortex_a5_header,
299 .dti_print_entry = tlb_print_cortex_a5_entry,
300 .dti_index = ARM_A5_TLBDATAOP_INDEX,
301 };
302 #endif /* CPU_CORTEXA5 */
303
304 #if defined(CPU_CORTEXA7)
305 static const char tlb_cortex_a7_esizes[8][8] = {
306 " 4KB(S)", " 4KB(L)", "64KB(S)", "64KB(L)",
307 " 1MB(S)", " 2MB(L)", "16MB(S)", " 1GB(L)",
308 };
309
310 static void
311 tlb_print_cortex_a7_header(void)
312 {
313 tlb_print_common_header("IS --OS- SH");
314 }
315
316 static inline vaddr_t
317 tlb_decode_cortex_a7_vpn(size_t va_index, uint32_t d0, uint32_t d1)
318 {
319 const u_int size = __SHIFTOUT(d0, ARM_A7_TLBDATA0_SIZE);
320 const u_int shift = (size & 1)
321 ? ((0x12090400 >> (8*size)) & 0x1f)
322 : (2 * size);
323
324 return __SHIFTOUT(d0, ARM_A7_TLBDATA0_VA) * (ARM_A7_TLBDATAOP_INDEX + 1)
325 + (va_index << shift);
326 }
327
328 static void
329 tlb_print_cortex_a7_entry(size_t way, size_t va_index, uint32_t d0, uint32_t d1)
330 {
331 const uint32_t d2 = armreg_tlbdata2_read();
332 const uint64_t d01 = ((uint64_t)d1 << 32) | d0;
333 const uint64_t d12 = ((uint64_t)d2 << 32) | d1;
334
335 const paddr_t pfn = __SHIFTOUT(d12, ARM_A7_TLBDATA12_PA);
336 const vaddr_t vpn = tlb_decode_cortex_a7_vpn(va_index, d0, d1);
337
338 tlb_print_addr(way, va_index, vpn, pfn);
339
340 const u_int size = __SHIFTOUT(d0, ARM_A7_TLBDATA0_SIZE);
341 const u_int domain = __SHIFTOUT(d2, ARM_A7_TLBDATA2_DOM);
342 const u_int ap = __SHIFTOUT(d1, ARM_A7_TLBDATA1_AP);
343 const bool xn_p = (d2 & ARM_A7_TLBDATA2_XN1) != 0;
344
345 tlb_print_size_domain_prot(tlb_cortex_a7_esizes[size], domain, ap, xn_p);
346
347 const bool ng_p = (d1 & ARM_A7_TLBDATA1_nG) != 0;
348 const tlb_asid_t asid = __SHIFTOUT(d01, ARM_A7_TLBDATA01_ASID);
349
350 tlb_print_asid(ng_p, asid);
351
352 const u_int is = __SHIFTOUT(d2, ARM_A7_TLBDATA2_IS);
353 if (is == ARM_A7_TLBDATA2_IS_DSO) {
354 u_int mt = __SHIFTOUT(d2, ARM_A7_TLBDATA2_SDO_MT);
355 switch (mt) {
356 case ARM_A7_TLBDATA2_SDO_MT_D:
357 db_printf(" DV\n");
358 return;
359 case ARM_A7_TLBDATA2_SDO_MT_SO:
360 db_printf(" SO\n");
361 return;
362 default:
363 db_printf(" %02u\n", mt);
364 return;
365 }
366 }
367 const u_int os = __SHIFTOUT(d2, ARM_A7_TLBDATA2_OS);
368 const u_int sh = __SHIFTOUT(d2, ARM_A7_TLBDATA2_SH);
369 static const char is_types[3][3] = { "NC", "WB", "WT" };
370 static const char os_types[4][6] = { "NC", "WB+WA", "WT", "WB" };
371 static const char sh_types[4][3] = { "NS", "na", "OS", "IS" };
372 db_printf(" %2s %5s %2s\n", is_types[is], os_types[os], sh_types[sh]);
373 }
374
375 static const struct db_tlbinfo tlb_cortex_a7_info = {
376 .dti_decode_vpn = tlb_decode_cortex_a7_vpn,
377 .dti_print_header = tlb_print_cortex_a7_header,
378 .dti_print_entry = tlb_print_cortex_a7_entry,
379 .dti_index = ARM_A7_TLBDATAOP_INDEX,
380 };
381 #endif /* CPU_CORTEXA7 */
382
383 static inline const struct db_tlbinfo *
384 tlb_lookup_tlbinfo(void)
385 {
386 #if defined(CPU_CORTEXA5) && defined(CPU_CORTEXA7)
387 const bool cortex_a5_p = CPU_ID_CORTEX_A5_P(curcpu()->ci_arm_cpuid);
388 const bool cortex_a7_p = CPU_ID_CORTEX_A7_P(curcpu()->ci_arm_cpuid);
389 #elif defined(CPU_CORTEXA5)
390 const bool cortex_a5_p = true;
391 #else
392 const bool cortex_a7_p = true;
393 #endif
394 #ifdef CPU_CORTEXA5
395 if (cortex_a5_p) {
396 return &tlb_cortex_a5_info;
397 }
398 #endif
399 #ifdef CPU_CORTEXA7
400 if (cortex_a7_p) {
401 return &tlb_cortex_a7_info;
402 }
403 #endif
404 return NULL;
405 }
406
407 void
408 db_show_tlb_cmd(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif)
409 {
410 const struct db_tlbinfo * const dti = tlb_lookup_tlbinfo();
411
412 if (have_addr) {
413 const vaddr_t vpn = (vaddr_t)addr >> L2_S_SHIFT;
414 const u_int va_index = vpn & dti->dti_index;
415 for (size_t way = 0; way < 2; way++) {
416 armreg_tlbdataop_write(
417 __SHIFTIN(va_index, dti->dti_index)
418 | __SHIFTIN(way, ARM_TLBDATAOP_WAY));
419 arm_isb();
420 const uint32_t d0 = armreg_tlbdata0_read();
421 const uint32_t d1 = armreg_tlbdata1_read();
422 if ((d0 & ARM_TLBDATA_VALID)
423 && vpn == (*dti->dti_decode_vpn)(va_index, d0, d1)) {
424 (*dti->dti_print_header)();
425 (*dti->dti_print_entry)(way, va_index, d0, d1);
426 return;
427 }
428 }
429 db_printf("VA %#"DDB_EXPR_FMT"x not found in TLB\n", addr);
430 return;
431 }
432
433 bool first = true;
434 size_t n = 0;
435 for (size_t va_index = 0; va_index <= dti->dti_index; va_index++) {
436 for (size_t way = 0; way < 2; way++) {
437 armreg_tlbdataop_write(
438 __SHIFTIN(way, ARM_TLBDATAOP_WAY)
439 | __SHIFTIN(va_index, dti->dti_index));
440 arm_isb();
441 const uint32_t d0 = armreg_tlbdata0_read();
442 const uint32_t d1 = armreg_tlbdata1_read();
443 if (d0 & ARM_TLBDATA_VALID) {
444 if (first) {
445 (*dti->dti_print_header)();
446 first = false;
447 }
448 (*dti->dti_print_entry)(way, va_index, d0, d1);
449 n++;
450 }
451 }
452 }
453 db_printf("%zu TLB valid entries found\n", n);
454 }
455 #endif /* CPU_CORTEXA5 || CPU_CORTEXA7 */
456
457 #if defined(MULTIPROCESSOR)
458 void
459 db_switch_cpu_cmd(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif)
460 {
461 if (addr >= maxcpus) {
462 db_printf("cpu %"DDB_EXPR_FMT"d out of range", addr);
463 return;
464 }
465 struct cpu_info *new_ci = cpu_lookup(addr);
466 if (new_ci == NULL) {
467 db_printf("cpu %"DDB_EXPR_FMT"d does not exist", addr);
468 return;
469 }
470 if (DDB_REGS->tf_spsr & PSR_T_bit) {
471 DDB_REGS->tf_pc -= 2; /* XXX */
472 } else {
473 DDB_REGS->tf_pc -= 4;
474 }
475 db_newcpu = new_ci;
476 db_continue_cmd(0, false, 0, "");
477 }
478 #endif
479
480 static void
481 show_cpuinfo(struct cpu_info *kci)
482 {
483 struct cpu_info cpuinfobuf;
484 cpuid_t cpuid;
485 int i;
486
487 db_read_bytes((db_addr_t)kci, sizeof(cpuinfobuf), (char *)&cpuinfobuf);
488
489 struct cpu_info *ci = &cpuinfobuf;
490 cpuid = ci->ci_cpuid;
491 db_printf("cpu_info=%p, cpu_name=%s\n", kci, ci->ci_cpuname);
492 db_printf("%p cpu[%lu].ci_cpuid = %lu\n",
493 &ci->ci_cpuid, cpuid, ci->ci_cpuid);
494 db_printf("%p cpu[%lu].ci_curlwp = %p\n",
495 &ci->ci_curlwp, cpuid, ci->ci_curlwp);
496 for (i = 0; i < SOFTINT_COUNT; i++) {
497 db_printf("%p cpu[%lu].ci_softlwps[%d] = %p\n",
498 &ci->ci_softlwps[i], cpuid, i, ci->ci_softlwps[i]);
499 }
500 db_printf("%p cpu[%lu].ci_lastintr = %" PRIu64 "\n",
501 &ci->ci_lastintr, cpuid, ci->ci_lastintr);
502 db_printf("%p cpu[%lu].ci_want_resched = %d\n",
503 &ci->ci_want_resched, cpuid, ci->ci_want_resched);
504 db_printf("%p cpu[%lu].ci_cpl = %d\n",
505 &ci->ci_cpl, cpuid, ci->ci_cpl);
506 db_printf("%p cpu[%lu].ci_softints = 0x%08x\n",
507 &ci->ci_softints, cpuid, ci->ci_softints);
508 db_printf("%p cpu[%lu].ci_astpending = 0x%08x\n",
509 &ci->ci_astpending, cpuid, ci->ci_astpending);
510 db_printf("%p cpu[%lu].ci_intr_depth = %u\n",
511 &ci->ci_intr_depth, cpuid, ci->ci_intr_depth);
512
513 }
514
515 void
516 db_md_cpuinfo_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
517 const char *modif)
518 {
519 #ifdef MULTIPROCESSOR
520 CPU_INFO_ITERATOR cii;
521 struct cpu_info *ci;
522 bool showall = false;
523
524 if (modif != NULL) {
525 for (; *modif != '\0'; modif++) {
526 switch (*modif) {
527 case 'a':
528 showall = true;
529 break;
530 }
531 }
532 }
533
534 if (showall) {
535 for (CPU_INFO_FOREACH(cii, ci)) {
536 show_cpuinfo(ci);
537 }
538 } else
539 #endif /* MULTIPROCESSOR */
540 show_cpuinfo(curcpu());
541 }
542
543 #endif /* _KERNEL */
544