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