| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 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; |
| 23 |
u64 pc; |
| 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 |
|
| 35 |
static struct exception_state sentinel_exception_state; |
| 36 |
|
| 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: |
| 54 |
case ESR_EC_SS_SAME: |
| 55 |
case ESR_EC_BKPT_64: |
| 56 |
signal = GDB_SIGTRAP; |
| 57 |
break; |
| 58 |
default: |
| 59 |
|
| 60 |
|
| 61 |
signal = GDB_SIGSEGV; |
| 62 |
|
| 63 |
|
| 64 |
|
| 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 |
|
| 79 |
void gdb_arch_init(void) |
| 80 |
{ |
| 81 |
exception_install_hook(&gdb_exception_hook); |
| 82 |
raw_write_oslar_el1(0); |
| 83 |
raw_write_mdcr_el2(MDCR_TDE); |
| 84 |
raw_write_mdscr_el1(MDSCR_KDE); |
| 85 |
} |
| 86 |
|
| 87 |
void gdb_arch_enter(void) |
| 88 |
{ |
| 89 |
u64 *sp; |
| 90 |
|
| 91 |
asm volatile ("mov %0, sp" : "=r"(sp) ); |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
if (sp >= exception_stack && sp <= exception_stack_end) |
| 97 |
gdb_exception_hook(EXC_SYNC_SP0); |
| 98 |
else |
| 99 |
asm volatile ("svc #0"); |
| 100 |
} |
| 101 |
|
| 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 |
|
| 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 |
|
| 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 |
} |