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