Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/geert/linux...
[linux-3.10.git] / kernel / debug / kdb / kdb_debugger.c
1 /*
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>
14 #include <linux/export.h>
15 #include "kdb_private.h"
16 #include "../debug_core.h"
17
18 /*
19  * KDB interface to KGDB internals
20  */
21 get_char_func kdb_poll_funcs[] = {
22         dbg_io_get_char,
23         NULL,
24         NULL,
25         NULL,
26         NULL,
27         NULL,
28 };
29 EXPORT_SYMBOL_GPL(kdb_poll_funcs);
30
31 int kdb_poll_idx = 1;
32 EXPORT_SYMBOL_GPL(kdb_poll_idx);
33
34 static struct kgdb_state *kdb_ks;
35
36 int kdb_stub(struct kgdb_state *ks)
37 {
38         int error = 0;
39         kdb_bp_t *bp;
40         unsigned long addr = kgdb_arch_pc(ks->ex_vector, ks->linux_regs);
41         kdb_reason_t reason = KDB_REASON_OOPS;
42         kdb_dbtrap_t db_result = KDB_DB_NOBPT;
43         int i;
44
45         kdb_ks = ks;
46         if (KDB_STATE(REENTRY)) {
47                 reason = KDB_REASON_SWITCH;
48                 KDB_STATE_CLEAR(REENTRY);
49                 addr = instruction_pointer(ks->linux_regs);
50         }
51         ks->pass_exception = 0;
52         if (atomic_read(&kgdb_setting_breakpoint))
53                 reason = KDB_REASON_KEYBOARD;
54
55         for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; i++, bp++) {
56                 if ((bp->bp_enabled) && (bp->bp_addr == addr)) {
57                         reason = KDB_REASON_BREAK;
58                         db_result = KDB_DB_BPT;
59                         if (addr != instruction_pointer(ks->linux_regs))
60                                 kgdb_arch_set_pc(ks->linux_regs, addr);
61                         break;
62                 }
63         }
64         if (reason == KDB_REASON_BREAK || reason == KDB_REASON_SWITCH) {
65                 for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; i++, bp++) {
66                         if (bp->bp_free)
67                                 continue;
68                         if (bp->bp_addr == addr) {
69                                 bp->bp_delay = 1;
70                                 bp->bp_delayed = 1;
71         /*
72          * SSBPT is set when the kernel debugger must single step a
73          * task in order to re-establish an instruction breakpoint
74          * which uses the instruction replacement mechanism.  It is
75          * cleared by any action that removes the need to single-step
76          * the breakpoint.
77          */
78                                 reason = KDB_REASON_BREAK;
79                                 db_result = KDB_DB_BPT;
80                                 KDB_STATE_SET(SSBPT);
81                                 break;
82                         }
83                 }
84         }
85
86         if (reason != KDB_REASON_BREAK && ks->ex_vector == 0 &&
87                 ks->signo == SIGTRAP) {
88                 reason = KDB_REASON_SSTEP;
89                 db_result = KDB_DB_BPT;
90         }
91         /* Set initial kdb state variables */
92         KDB_STATE_CLEAR(KGDB_TRANS);
93         kdb_initial_cpu = atomic_read(&kgdb_active);
94         kdb_current_task = kgdb_info[ks->cpu].task;
95         kdb_current_regs = kgdb_info[ks->cpu].debuggerinfo;
96         /* Remove any breakpoints as needed by kdb and clear single step */
97         kdb_bp_remove();
98         KDB_STATE_CLEAR(DOING_SS);
99         KDB_STATE_CLEAR(DOING_SSB);
100         KDB_STATE_SET(PAGER);
101         /* zero out any offline cpu data */
102         for_each_present_cpu(i) {
103                 if (!cpu_online(i)) {
104                         kgdb_info[i].debuggerinfo = NULL;
105                         kgdb_info[i].task = NULL;
106                 }
107         }
108         if (ks->err_code == DIE_OOPS || reason == KDB_REASON_OOPS) {
109                 ks->pass_exception = 1;
110                 KDB_FLAG_SET(CATASTROPHIC);
111         }
112         if (KDB_STATE(SSBPT) && reason == KDB_REASON_SSTEP) {
113                 KDB_STATE_CLEAR(SSBPT);
114                 KDB_STATE_CLEAR(DOING_SS);
115         } else {
116                 /* Start kdb main loop */
117                 error = kdb_main_loop(KDB_REASON_ENTER, reason,
118                                       ks->err_code, db_result, ks->linux_regs);
119         }
120         /*
121          * Upon exit from the kdb main loop setup break points and restart
122          * the system based on the requested continue state
123          */
124         kdb_initial_cpu = -1;
125         kdb_current_task = NULL;
126         kdb_current_regs = NULL;
127         KDB_STATE_CLEAR(PAGER);
128         kdbnearsym_cleanup();
129         if (error == KDB_CMD_KGDB) {
130                 if (KDB_STATE(DOING_KGDB))
131                         KDB_STATE_CLEAR(DOING_KGDB);
132                 return DBG_PASS_EVENT;
133         }
134         kdb_bp_install(ks->linux_regs);
135         dbg_activate_sw_breakpoints();
136         /* Set the exit state to a single step or a continue */
137         if (KDB_STATE(DOING_SS))
138                 gdbstub_state(ks, "s");
139         else
140                 gdbstub_state(ks, "c");
141
142         KDB_FLAG_CLEAR(CATASTROPHIC);
143
144         /* Invoke arch specific exception handling prior to system resume */
145         kgdb_info[ks->cpu].ret_state = gdbstub_state(ks, "e");
146         if (ks->pass_exception)
147                 kgdb_info[ks->cpu].ret_state = 1;
148         if (error == KDB_CMD_CPU) {
149                 KDB_STATE_SET(REENTRY);
150                 /*
151                  * Force clear the single step bit because kdb emulates this
152                  * differently vs the gdbstub
153                  */
154                 kgdb_single_step = 0;
155                 dbg_deactivate_sw_breakpoints();
156                 return DBG_SWITCH_CPU_EVENT;
157         }
158         return kgdb_info[ks->cpu].ret_state;
159 }
160
161 void kdb_gdb_state_pass(char *buf)
162 {
163         gdbstub_state(kdb_ks, buf);
164 }