2 * Copyright 2014 Google Inc.
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.
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.
15 #include <exception.h>
17 #include <libpayload.h>
22 u64 sp
; /* shares encoding 0b11111 with XZR in insns */
23 u64 pc
; /* no longer encoded as a GPR in AArch64! */
34 /* Scratch value to write reentrant exception states to. We never read it. */
35 static struct exception_state sentinel_exception_state
;
37 static int gdb_exception_hook(u32 type
)
39 if (!gdb_handle_reentrant_exception()) {
42 if (type
>= EXC_SYNC_SPX
) {
43 printf("Impossible exception type: %d!\n", type
);
47 if (type
== EXC_IRQ_SP0
|| type
== EXC_FIQ_SP0
)
49 else switch (exception_state
.ec
) {
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 */
59 /* We mostly expect INSN_ABT, DATA_ABT and SERROR here,
60 but it makes for a good catchall signal anyway. */
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
);
70 exception_set_state_ptr(&sentinel_exception_state
);
71 gdb_command_loop(signal
);
74 exception_set_state_ptr(&exception_state
);
79 void gdb_arch_init(void)
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 */
87 void gdb_arch_enter(void)
91 asm volatile ("mov %0, sp" : "=r"(sp
) );
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");
102 int gdb_arch_set_single_step(int on
)
104 raw_write_mdscr_el1(MDSCR_KDE
| (on
? MDSCR_SS
: 0));
105 exception_state
.pstate
.ss
= !!on
;
109 void gdb_arch_encode_regs(struct gdb_message
*message
)
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
));
123 void gdb_arch_decode_regs(int offset
, struct gdb_message
*message
)
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;