• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2014 Google Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but without any warranty; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14 
15 #include <exception.h>
16 #include <gdb.h>
17 #include <libpayload.h>
18 
19 struct gdb_regs
20 {
21 	u64 x[31];
22 	u64 sp;		/* shares encoding 0b11111 with XZR in insns */
23 	u64 pc;		/* no longer encoded as a GPR in AArch64! */
24 	u32 spsr;
25 	struct fp_reg
26 	{
27 		u64 low;
28 		u64 high;
29 	} __packed v[32];
30 	u32 fpsr;
31 	u32 fpcr;
32 } __packed;
33 
34 /* Scratch value to write reentrant exception states to. We never read it. */
35 static struct exception_state sentinel_exception_state;
36 
gdb_exception_hook(u32 type)37 static int gdb_exception_hook(u32 type)
38 {
39 	if (!gdb_handle_reentrant_exception()) {
40 		u8 signal;
41 
42 		if (type >= EXC_SYNC_SPX) {
43 			printf("Impossible exception type: %d!\n", type);
44 			return 0;
45 		}
46 
47 		if (type == EXC_IRQ_SP0 || type == EXC_FIQ_SP0)
48 			signal = GDB_SIGINT;
49 		else switch (exception_state.ec) {
50 		case ESR_EC_UNKNOWN:
51 			signal = GDB_SIGILL;
52 			break;
53 		case ESR_EC_SVC_64:	/* gdb_arch_enter() uses SVC */
54 		case ESR_EC_SS_SAME:	/* single-step causes this one */
55 		case ESR_EC_BKPT_64:	/* GDB itself likes to insert BRKs */
56 			signal = GDB_SIGTRAP;
57 			break;
58 		default:
59 			/* We mostly expect INSN_ABT, DATA_ABT and SERROR here,
60 			   but it makes for a good catchall signal anyway. */
61 			signal = GDB_SIGSEGV;
62 			/* GDB itself doesn't read out the ESR, so print it to
63 			   help people understand unexpected exceptions. But we
64 			   can't print anything if GDB is not connected yet. */
65 			if (gdb_state.connected)
66 				printf("Remote-GDB Exception %d, ESR: %#08x\n",
67 				       type, (uint32_t)exception_state.esr);
68 		}
69 
70 		exception_set_state_ptr(&sentinel_exception_state);
71 		gdb_command_loop(signal);
72 	}
73 
74 	exception_set_state_ptr(&exception_state);
75 
76 	return 1;
77 }
78 
gdb_arch_init(void)79 void gdb_arch_init(void)
80 {
81 	exception_install_hook(&gdb_exception_hook);
82 	raw_write_oslar_el1(0);		/* Disable OS lock (whatever that is) */
83 	raw_write_mdcr_el2(MDCR_TDE);	/* Route debug exceptions to EL2 */
84 	raw_write_mdscr_el1(MDSCR_KDE);	/* Enable debugging of current EL */
85 }
86 
gdb_arch_enter(void)87 void gdb_arch_enter(void)
88 {
89 	u64 *sp;
90 
91 	asm volatile ("mov %0, sp" : "=r"(sp) );
92 
93 	/* Avoid reentrant exceptions, just call the hook if in one already.
94 	   This is mostly important when gdb_enter() is called as result of an
95 	   exception (as part of the halt() at the end). */
96 	if (sp >= exception_stack && sp <= exception_stack_end)
97 		gdb_exception_hook(EXC_SYNC_SP0);
98 	else	/* BRK doesn't adjust ELR, so using SVC makes things easier. */
99 		asm volatile ("svc #0");
100 }
101 
gdb_arch_set_single_step(int on)102 int gdb_arch_set_single_step(int on)
103 {
104 	raw_write_mdscr_el1(MDSCR_KDE | (on ? MDSCR_SS : 0));
105 	exception_state.pstate.ss = !!on;
106 	return 0;
107 }
108 
gdb_arch_encode_regs(struct gdb_message * message)109 void gdb_arch_encode_regs(struct gdb_message *message)
110 {
111 	gdb_message_encode_bytes(message, &exception_state.regs,
112 				 sizeof(exception_state.regs));
113 	gdb_message_encode_bytes(message, &exception_state.sp,
114 				 sizeof(exception_state.sp));
115 	gdb_message_encode_bytes(message, &exception_state.elr,
116 				 sizeof(exception_state.elr));
117 	gdb_message_encode_bytes(message, &exception_state.spsr,
118 				 sizeof(exception_state.spsr));
119 	gdb_message_encode_zero_bytes(message,
120 		sizeof(struct gdb_regs) - offsetof(struct gdb_regs, v));
121 }
122 
gdb_arch_decode_regs(int offset,struct gdb_message * message)123 void gdb_arch_decode_regs(int offset, struct gdb_message *message)
124 {
125 	gdb_message_decode_bytes(message, offset,
126 			&exception_state.regs, sizeof(exception_state.regs));
127 	offset += sizeof(exception_state.regs) * 2;
128 	gdb_message_decode_bytes(message, offset,
129 			&exception_state.sp, sizeof(exception_state.sp));
130 	offset += sizeof(exception_state.sp) * 2;
131 	gdb_message_decode_bytes(message, offset,
132 			&exception_state.elr, sizeof(exception_state.elr));
133 	offset += sizeof(exception_state.elr) * 2;
134 	gdb_message_decode_bytes(message, offset,
135 			&exception_state.spsr, sizeof(exception_state.spsr));
136 	offset += sizeof(exception_state.spsr) * 2;
137 }
138