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