1/*	$NetBSD: db_machdep.c,v 1.4 2011/12/31 08:34:49 skrll Exp $	*/
2
3/*-
4 * Copyright (c) 2010 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Nick Hudson
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__KERNEL_RCSID(0, "$NetBSD: db_machdep.c,v 1.4 2011/12/31 08:34:49 skrll Exp $");
34
35#include <sys/param.h>
36#include <sys/lwp.h>
37
38#include <machine/db_machdep.h>
39
40#include <ddb/db_command.h>
41#include <ddb/db_output.h>
42#include <ddb/db_variables.h>
43#include <ddb/db_access.h>
44
45db_regs_t	ddb_regs;
46const struct db_variable db_regs[] = {
47	{ "flags",	(long *)&ddb_regs.tf_flags,	FCN_NULL,	NULL },
48	{ "r1",		(long *)&ddb_regs.tf_r1,	FCN_NULL,	NULL },
49	{ "r2(rp)",	(long *)&ddb_regs.tf_rp,	FCN_NULL,	NULL },
50	{ "r3(fp)",	(long *)&ddb_regs.tf_r3,	FCN_NULL,	NULL },
51	{ "r4",		(long *)&ddb_regs.tf_r4,	FCN_NULL,	NULL },
52	{ "r5",		(long *)&ddb_regs.tf_r5,	FCN_NULL,	NULL },
53	{ "r6",		(long *)&ddb_regs.tf_r6,	FCN_NULL,	NULL },
54	{ "r7",		(long *)&ddb_regs.tf_r7,	FCN_NULL,	NULL },
55	{ "r8",		(long *)&ddb_regs.tf_r8,	FCN_NULL,	NULL },
56	{ "r9",		(long *)&ddb_regs.tf_r9,	FCN_NULL,	NULL },
57	{ "r10",	(long *)&ddb_regs.tf_r10,	FCN_NULL,	NULL },
58	{ "r11",	(long *)&ddb_regs.tf_r11,	FCN_NULL,	NULL },
59	{ "r12",	(long *)&ddb_regs.tf_r12,	FCN_NULL,	NULL },
60	{ "r13",	(long *)&ddb_regs.tf_r13,	FCN_NULL,	NULL },
61	{ "r14",	(long *)&ddb_regs.tf_r14,	FCN_NULL,	NULL },
62	{ "r15",	(long *)&ddb_regs.tf_r15,	FCN_NULL,	NULL },
63	{ "r16",	(long *)&ddb_regs.tf_r16,	FCN_NULL,	NULL },
64	{ "r17",	(long *)&ddb_regs.tf_r17,	FCN_NULL,	NULL },
65	{ "r18",	(long *)&ddb_regs.tf_r18,	FCN_NULL,	NULL },
66	{ "r19(t4)",	(long *)&ddb_regs.tf_t4,	FCN_NULL,	NULL },
67	{ "r20(t3)",	(long *)&ddb_regs.tf_t3,	FCN_NULL,	NULL },
68	{ "r21(t2)",	(long *)&ddb_regs.tf_t2,	FCN_NULL,	NULL },
69	{ "r22(t1)",	(long *)&ddb_regs.tf_t1,	FCN_NULL,	NULL },
70	{ "r23(arg3)",	(long *)&ddb_regs.tf_arg3,	FCN_NULL,	NULL },
71	{ "r24(arg2)",	(long *)&ddb_regs.tf_arg2,	FCN_NULL,	NULL },
72	{ "r25(arg1)",	(long *)&ddb_regs.tf_arg1,	FCN_NULL,	NULL },
73	{ "r26(arg0)",	(long *)&ddb_regs.tf_arg0,	FCN_NULL,	NULL },
74	{ "r27(dp)",	(long *)&ddb_regs.tf_dp,	FCN_NULL,	NULL },
75	{ "r28(ret0)",	(long *)&ddb_regs.tf_ret0,	FCN_NULL,	NULL },
76	{ "r29(ret1)",	(long *)&ddb_regs.tf_ret1,	FCN_NULL,	NULL },
77	{ "r30(sp)",	(long *)&ddb_regs.tf_sp,	FCN_NULL,	NULL },
78	{ "r31",	(long *)&ddb_regs.tf_r31,	FCN_NULL,	NULL },
79
80	{ "sar",	(long *)&ddb_regs.tf_sar,	FCN_NULL,	NULL },
81
82	{ "eirr",	(long *)&ddb_regs.tf_eirr,	FCN_NULL,	NULL },
83	{ "eiem",	(long *)&ddb_regs.tf_eiem,	FCN_NULL,	NULL },
84	{ "iir",	(long *)&ddb_regs.tf_iir,	FCN_NULL,	NULL },
85	{ "isr",	(long *)&ddb_regs.tf_isr,	FCN_NULL,	NULL },
86	{ "ior",	(long *)&ddb_regs.tf_ior,	FCN_NULL,	NULL },
87	{ "ipsw",	(long *)&ddb_regs.tf_ipsw,	FCN_NULL,	NULL },
88	{ "iisqh",	(long *)&ddb_regs.tf_iisq_head,	FCN_NULL,	NULL },
89	{ "iioqh",	(long *)&ddb_regs.tf_iioq_head,	FCN_NULL,	NULL },
90	{ "iisqt",	(long *)&ddb_regs.tf_iisq_tail,	FCN_NULL,	NULL },
91	{ "iioqt",	(long *)&ddb_regs.tf_iioq_tail,	FCN_NULL,	NULL },
92
93	{ "sr0",	(long *)&ddb_regs.tf_sr0,	FCN_NULL,	NULL },
94	{ "sr1",	(long *)&ddb_regs.tf_sr1,	FCN_NULL,	NULL },
95	{ "sr2",	(long *)&ddb_regs.tf_sr2,	FCN_NULL,	NULL },
96	{ "sr3",	(long *)&ddb_regs.tf_sr3,	FCN_NULL,	NULL },
97	{ "sr4",	(long *)&ddb_regs.tf_sr4,	FCN_NULL,	NULL },
98	{ "sr5",	(long *)&ddb_regs.tf_sr5,	FCN_NULL,	NULL },
99	{ "sr6",	(long *)&ddb_regs.tf_sr6,	FCN_NULL,	NULL },
100	{ "sr7",	(long *)&ddb_regs.tf_sr7,	FCN_NULL,	NULL },
101
102	{ "pidr1",	(long *)&ddb_regs.tf_pidr1,	FCN_NULL,	NULL },
103	{ "pidr2",	(long *)&ddb_regs.tf_pidr2,	FCN_NULL,	NULL },
104#ifdef pbably_not_worth_it
105	{ "pidr3",	(long *)&ddb_regs.tf_pidr3,	FCN_NULL,	NULL },
106	{ "pidr4",	(long *)&ddb_regs.tf_pidr4,	FCN_NULL,	NULL },
107#endif
108
109	{ "vtop",	(long *)&ddb_regs.tf_vtop,	FCN_NULL,	NULL },
110	{ "cr24",	(long *)&ddb_regs.tf_cr24,	FCN_NULL,	NULL },
111	{ "cr27",	(long *)&ddb_regs.tf_cr27,	FCN_NULL,	NULL },
112	{ "cr28",	(long *)&ddb_regs.tf_cr28,	FCN_NULL,	NULL },
113	{ "cr30",	(long *)&ddb_regs.tf_cr30,	FCN_NULL,	NULL },
114};
115const struct db_variable * const db_eregs = db_regs + sizeof(db_regs)/sizeof(db_regs[0]);
116
117const struct db_command db_machine_command_table[] = {
118	{ DDB_ADD_CMD("frame",	db_dump_trap,	0,
119	  "Displays the contents of a trapframe",
120	  "[/l] [addr]",
121	  "   addr:\tdisplay this trap frame (current kernel frame otherwise)\n"
122	  "   /l:\tdisplay the trap frame from lwp") },
123	{ DDB_ADD_CMD(NULL,	NULL,		0, NULL, NULL, NULL) }
124};
125
126void
127db_dump_trap(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif)
128{
129	struct trapframe *tf, *ktf, ltf;
130	const char *cp = modif;
131	bool lwpaddr = false;
132	char c;
133
134	tf = DDB_REGS;
135	while ((c = *cp++) != 0) {
136		if (c == 'l')
137			lwpaddr = true;
138	}
139
140	/* Or an arbitrary trapframe */
141	if (have_addr) {
142		if (lwpaddr) {
143			lwp_t l;
144
145			db_read_bytes(addr, sizeof(l), (char *)&l);
146			ktf = (struct trapframe *)l.l_md.md_regs;
147		} else {
148			ktf = (struct trapframe *)addr;
149		}
150		db_read_bytes((db_addr_t)ktf, sizeof(ltf), (char *)&ltf);
151		tf = &ltf;
152	}
153
154	db_printf("General registers\n");
155	db_printf("r00-03  %08x %08x %08x %08x\n",
156	     0, tf->tf_r1, tf->tf_rp, tf->tf_r3);
157	db_printf("r04-07  %08x %08x %08x %08x\n",
158	     tf->tf_r4, tf->tf_r5, tf->tf_r6, tf->tf_r7);
159	db_printf("r08-11  %08x %08x %08x %08x\n",
160	     tf->tf_r8, tf->tf_r9, tf->tf_r10, tf->tf_r11);
161	db_printf("r12-15  %08x %08x %08x %08x\n",
162	     tf->tf_r12, tf->tf_r13, tf->tf_r14, tf->tf_r15);
163	db_printf("r16-19  %08x %08x %08x %08x\n",
164	     tf->tf_r16, tf->tf_r17, tf->tf_r18, tf->tf_t4);
165	db_printf("r20-23  %08x %08x %08x %08x\n",
166	     tf->tf_t3, tf->tf_t2, tf->tf_t1, tf->tf_arg3);
167	db_printf("r24-27  %08x %08x %08x %08x\n",
168	     tf->tf_arg2, tf->tf_arg1, tf->tf_arg0, tf->tf_dp);
169	db_printf("r28-31  %08x %08x %08x %08x\n",
170	     tf->tf_ret0, tf->tf_ret1, tf->tf_sp, tf->tf_r31);
171	db_printf("\n");
172	db_printf("Space registers\n");
173	db_printf("s00-03  %08x %08x %08x %08x\n",
174	     tf->tf_sr0, tf->tf_sr1, tf->tf_sr2, tf->tf_sr3);
175	db_printf("s04-07  %08x %08x %08x %08x\n",
176	     tf->tf_sr4, tf->tf_sr5, tf->tf_sr6, tf->tf_sr7);
177	db_printf("\n");
178	db_printf("Instruction queues\n");
179	db_printf("iisq:   %08x %08x\niioq:   %08x %08x\n",
180	    tf->tf_iisq_head, tf->tf_iisq_tail, tf->tf_iioq_head,
181	    tf->tf_iioq_tail);
182	db_printf("\n");
183	db_printf("Interrupt state\n");
184	db_printf("isr:    %08x\nior:    %08x\niir:    %08x\n",
185	    tf->tf_isr, tf->tf_ior, tf->tf_iir);
186	db_printf("\n");
187	db_printf("Other state\n");
188	db_printf("eiem:   %08x\n", tf->tf_eiem);
189	db_printf("ipsw:   %08x\n", tf->tf_ipsw);
190	db_printf("flags:  %08x\n", tf->tf_flags);
191	db_printf("sar:    %08x\n", tf->tf_sar);
192	db_printf("pidr1:  %08x\n", tf->tf_pidr1);	/* cr8 */
193	db_printf("pidr2:  %08x\n", tf->tf_pidr2);	/* cr9 */
194#if pbably_not_worth_it
195	db_printf("pidr3:  %08x\n", tf->tf_pidr3);	/* cr12 */
196	db_printf("pidr4:  %08x\n", tf->tf_pidr4);	/* cr13 */
197#endif
198	db_printf("rctr:   %08x\n", tf->tf_rctr);	/* cr0 */
199	db_printf("ccr:    %08x\n", tf->tf_ccr);	/* cr10 */
200	db_printf("eirr:   %08x\n", tf->tf_eirr);	/* cr23 - DDB */
201	db_printf("cr24:   %08x\n", tf->tf_cr24);	/* cr24 - DDB */
202	db_printf("vtop:   %08x\n", tf->tf_vtop);	/* cr25 - DDB */
203	db_printf("cr27:   %08x\n", tf->tf_cr27);	/*      - DDB */
204	db_printf("cr28:   %08x\n", tf->tf_cr28);	/*      - DDB */
205	db_printf("cr30:   %08x\n", tf->tf_cr30);	/* uaddr */
206}
207