blob: be7b33b73d3001eef1b676ad1efde4f318824902 [file] [log] [blame]
Jason Wessel5d5314d2010-05-20 21:04:20 -05001/*
2 * Created by: Jason Wessel <jason.wessel@windriver.com>
3 *
4 * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved.
5 *
6 * This file is licensed under the terms of the GNU General Public
7 * License version 2. This program is licensed "as is" without any
8 * warranty of any kind, whether express or implied.
9 */
10
11#include <linux/kgdb.h>
12#include <linux/kdb.h>
13#include <linux/kdebug.h>
Paul Gortmaker6e5fdee2011-05-26 16:00:52 -040014#include <linux/export.h>
Anton Vorontsovb10d22d2012-07-30 04:58:10 -070015#include <linux/hardirq.h>
Jason Wessel5d5314d2010-05-20 21:04:20 -050016#include "kdb_private.h"
17#include "../debug_core.h"
18
19/*
20 * KDB interface to KGDB internals
21 */
22get_char_func kdb_poll_funcs[] = {
23 dbg_io_get_char,
24 NULL,
Jason Wesself5316b42010-05-20 21:04:22 -050025 NULL,
26 NULL,
27 NULL,
28 NULL,
Jason Wessel5d5314d2010-05-20 21:04:20 -050029};
Jason Wesself5316b42010-05-20 21:04:22 -050030EXPORT_SYMBOL_GPL(kdb_poll_funcs);
31
32int kdb_poll_idx = 1;
33EXPORT_SYMBOL_GPL(kdb_poll_idx);
Jason Wessel5d5314d2010-05-20 21:04:20 -050034
Jason Wesself679c492011-05-23 13:17:41 -050035static struct kgdb_state *kdb_ks;
36
Jason Wessel5d5314d2010-05-20 21:04:20 -050037int kdb_stub(struct kgdb_state *ks)
38{
39 int error = 0;
40 kdb_bp_t *bp;
41 unsigned long addr = kgdb_arch_pc(ks->ex_vector, ks->linux_regs);
42 kdb_reason_t reason = KDB_REASON_OOPS;
43 kdb_dbtrap_t db_result = KDB_DB_NOBPT;
44 int i;
45
Jason Wesself679c492011-05-23 13:17:41 -050046 kdb_ks = ks;
Jason Wessel5d5314d2010-05-20 21:04:20 -050047 if (KDB_STATE(REENTRY)) {
48 reason = KDB_REASON_SWITCH;
49 KDB_STATE_CLEAR(REENTRY);
50 addr = instruction_pointer(ks->linux_regs);
51 }
52 ks->pass_exception = 0;
53 if (atomic_read(&kgdb_setting_breakpoint))
54 reason = KDB_REASON_KEYBOARD;
55
Anton Vorontsovb10d22d2012-07-30 04:58:10 -070056 if (in_nmi())
57 reason = KDB_REASON_NMI;
58
Jason Wessel5d5314d2010-05-20 21:04:20 -050059 for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; i++, bp++) {
60 if ((bp->bp_enabled) && (bp->bp_addr == addr)) {
61 reason = KDB_REASON_BREAK;
62 db_result = KDB_DB_BPT;
63 if (addr != instruction_pointer(ks->linux_regs))
64 kgdb_arch_set_pc(ks->linux_regs, addr);
65 break;
66 }
67 }
68 if (reason == KDB_REASON_BREAK || reason == KDB_REASON_SWITCH) {
69 for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; i++, bp++) {
70 if (bp->bp_free)
71 continue;
72 if (bp->bp_addr == addr) {
73 bp->bp_delay = 1;
74 bp->bp_delayed = 1;
75 /*
76 * SSBPT is set when the kernel debugger must single step a
77 * task in order to re-establish an instruction breakpoint
78 * which uses the instruction replacement mechanism. It is
79 * cleared by any action that removes the need to single-step
80 * the breakpoint.
81 */
82 reason = KDB_REASON_BREAK;
83 db_result = KDB_DB_BPT;
84 KDB_STATE_SET(SSBPT);
85 break;
86 }
87 }
88 }
89
90 if (reason != KDB_REASON_BREAK && ks->ex_vector == 0 &&
91 ks->signo == SIGTRAP) {
92 reason = KDB_REASON_SSTEP;
93 db_result = KDB_DB_BPT;
94 }
95 /* Set initial kdb state variables */
96 KDB_STATE_CLEAR(KGDB_TRANS);
Jason Wessel495363d2010-05-21 08:46:00 -050097 kdb_initial_cpu = atomic_read(&kgdb_active);
Jason Wessel5d5314d2010-05-20 21:04:20 -050098 kdb_current_task = kgdb_info[ks->cpu].task;
99 kdb_current_regs = kgdb_info[ks->cpu].debuggerinfo;
100 /* Remove any breakpoints as needed by kdb and clear single step */
101 kdb_bp_remove();
102 KDB_STATE_CLEAR(DOING_SS);
103 KDB_STATE_CLEAR(DOING_SSB);
Jason Wesself5316b42010-05-20 21:04:22 -0500104 KDB_STATE_SET(PAGER);
Jason Wessel5d5314d2010-05-20 21:04:20 -0500105 /* zero out any offline cpu data */
106 for_each_present_cpu(i) {
107 if (!cpu_online(i)) {
108 kgdb_info[i].debuggerinfo = NULL;
109 kgdb_info[i].task = NULL;
110 }
111 }
112 if (ks->err_code == DIE_OOPS || reason == KDB_REASON_OOPS) {
113 ks->pass_exception = 1;
114 KDB_FLAG_SET(CATASTROPHIC);
115 }
Jason Wessel5d5314d2010-05-20 21:04:20 -0500116 if (KDB_STATE(SSBPT) && reason == KDB_REASON_SSTEP) {
117 KDB_STATE_CLEAR(SSBPT);
118 KDB_STATE_CLEAR(DOING_SS);
119 } else {
120 /* Start kdb main loop */
121 error = kdb_main_loop(KDB_REASON_ENTER, reason,
122 ks->err_code, db_result, ks->linux_regs);
123 }
124 /*
125 * Upon exit from the kdb main loop setup break points and restart
126 * the system based on the requested continue state
127 */
128 kdb_initial_cpu = -1;
129 kdb_current_task = NULL;
130 kdb_current_regs = NULL;
Jason Wesself5316b42010-05-20 21:04:22 -0500131 KDB_STATE_CLEAR(PAGER);
Jason Wessel5d5314d2010-05-20 21:04:20 -0500132 kdbnearsym_cleanup();
133 if (error == KDB_CMD_KGDB) {
Jason Wesseld613d822011-05-23 13:22:54 -0500134 if (KDB_STATE(DOING_KGDB))
Jason Wessel5d5314d2010-05-20 21:04:20 -0500135 KDB_STATE_CLEAR(DOING_KGDB);
Jason Wessel5d5314d2010-05-20 21:04:20 -0500136 return DBG_PASS_EVENT;
137 }
138 kdb_bp_install(ks->linux_regs);
139 dbg_activate_sw_breakpoints();
140 /* Set the exit state to a single step or a continue */
141 if (KDB_STATE(DOING_SS))
142 gdbstub_state(ks, "s");
143 else
144 gdbstub_state(ks, "c");
145
146 KDB_FLAG_CLEAR(CATASTROPHIC);
147
148 /* Invoke arch specific exception handling prior to system resume */
149 kgdb_info[ks->cpu].ret_state = gdbstub_state(ks, "e");
150 if (ks->pass_exception)
151 kgdb_info[ks->cpu].ret_state = 1;
152 if (error == KDB_CMD_CPU) {
153 KDB_STATE_SET(REENTRY);
154 /*
155 * Force clear the single step bit because kdb emulates this
156 * differently vs the gdbstub
157 */
158 kgdb_single_step = 0;
159 dbg_deactivate_sw_breakpoints();
160 return DBG_SWITCH_CPU_EVENT;
161 }
162 return kgdb_info[ks->cpu].ret_state;
163}
164
Jason Wesself679c492011-05-23 13:17:41 -0500165void kdb_gdb_state_pass(char *buf)
166{
167 gdbstub_state(kdb_ks, buf);
168}