soc/intel: Remove blank lines before '}' and after '{'
[coreboot2.git] / payloads / libpayload / arch / arm64 / gdb.c
blob10ae675fe4f06b2fe466d6db1bcf26d425863e0b
1 /*
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>
16 #include <gdb.h>
17 #include <libpayload.h>
19 struct gdb_regs
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
27 u64 low;
28 u64 high;
29 } __packed v[32];
30 u32 fpsr;
31 u32 fpcr;
32 } __packed;
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()) {
40 u8 signal;
42 if (type >= EXC_SYNC_SPX) {
43 printf("Impossible exception type: %d!\n", type);
44 return 0;
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);
70 exception_set_state_ptr(&sentinel_exception_state);
71 gdb_command_loop(signal);
74 exception_set_state_ptr(&exception_state);
76 return 1;
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)
89 u64 *sp;
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;
106 return 0;
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;