aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Documentation/DocBook/Makefile2
-rw-r--r--Documentation/DocBook/kgdb.tmpl447
-rw-r--r--Documentation/kernel-parameters.txt5
-rw-r--r--MAINTAINERS6
-rw-r--r--arch/x86/Kconfig1
-rw-r--r--arch/x86/kernel/Makefile1
-rw-r--r--arch/x86/kernel/kgdb.c571
-rw-r--r--arch/x86/kernel/setup64.c16
-rw-r--r--arch/x86/kernel/traps_32.c2
-rw-r--r--arch/x86/kernel/traps_64.c9
-rw-r--r--drivers/char/tty_io.c47
-rw-r--r--drivers/misc/Makefile1
-rw-r--r--drivers/misc/kgdbts.c1090
-rw-r--r--drivers/serial/8250.c58
-rw-r--r--drivers/serial/Kconfig3
-rw-r--r--drivers/serial/Makefile1
-rw-r--r--drivers/serial/amba-pl011.c30
-rw-r--r--drivers/serial/kgdboc.c168
-rw-r--r--drivers/serial/serial_core.c74
-rw-r--r--include/asm-x86/kdebug.h1
-rw-r--r--include/asm-x86/kgdb.h81
-rw-r--r--include/linux/clocksource.h1
-rw-r--r--include/linux/kgdb.h281
-rw-r--r--include/linux/serial_core.h4
-rw-r--r--include/linux/tty_driver.h12
-rw-r--r--include/linux/uaccess.h22
-rw-r--r--kernel/Makefile1
-rw-r--r--kernel/kgdb.c1700
-rw-r--r--kernel/time/clocksource.c12
-rw-r--r--lib/Kconfig.debug2
-rw-r--r--lib/Kconfig.kgdb58
-rw-r--r--mm/Makefile2
-rw-r--r--mm/maccess.c55
33 files changed, 4757 insertions, 7 deletions
diff --git a/Documentation/DocBook/Makefile b/Documentation/DocBook/Makefile
index 300e1707893f..e471bc466a7e 100644
--- a/Documentation/DocBook/Makefile
+++ b/Documentation/DocBook/Makefile
@@ -9,7 +9,7 @@
9DOCBOOKS := wanbook.xml z8530book.xml mcabook.xml videobook.xml \ 9DOCBOOKS := wanbook.xml z8530book.xml mcabook.xml videobook.xml \
10 kernel-hacking.xml kernel-locking.xml deviceiobook.xml \ 10 kernel-hacking.xml kernel-locking.xml deviceiobook.xml \
11 procfs-guide.xml writing_usb_driver.xml networking.xml \ 11 procfs-guide.xml writing_usb_driver.xml networking.xml \
12 kernel-api.xml filesystems.xml lsm.xml usb.xml \ 12 kernel-api.xml filesystems.xml lsm.xml usb.xml kgdb.xml \
13 gadget.xml libata.xml mtdnand.xml librs.xml rapidio.xml \ 13 gadget.xml libata.xml mtdnand.xml librs.xml rapidio.xml \
14 genericirq.xml s390-drivers.xml uio-howto.xml scsi.xml 14 genericirq.xml s390-drivers.xml uio-howto.xml scsi.xml
15 15
diff --git a/Documentation/DocBook/kgdb.tmpl b/Documentation/DocBook/kgdb.tmpl
new file mode 100644
index 000000000000..97618bed4d65
--- /dev/null
+++ b/Documentation/DocBook/kgdb.tmpl
@@ -0,0 +1,447 @@
1<?xml version="1.0" encoding="UTF-8"?>
2<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
3 "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" []>
4
5<book id="kgdbOnLinux">
6 <bookinfo>
7 <title>Using kgdb and the kgdb Internals</title>
8
9 <authorgroup>
10 <author>
11 <firstname>Jason</firstname>
12 <surname>Wessel</surname>
13 <affiliation>
14 <address>
15 <email>jason.wessel@windriver.com</email>
16 </address>
17 </affiliation>
18 </author>
19 </authorgroup>
20
21 <authorgroup>
22 <author>
23 <firstname>Tom</firstname>
24 <surname>Rini</surname>
25 <affiliation>
26 <address>
27 <email>trini@kernel.crashing.org</email>
28 </address>
29 </affiliation>
30 </author>
31 </authorgroup>
32
33 <authorgroup>
34 <author>
35 <firstname>Amit S.</firstname>
36 <surname>Kale</surname>
37 <affiliation>
38 <address>
39 <email>amitkale@linsyssoft.com</email>
40 </address>
41 </affiliation>
42 </author>
43 </authorgroup>
44
45 <copyright>
46 <year>2008</year>
47 <holder>Wind River Systems, Inc.</holder>
48 </copyright>
49 <copyright>
50 <year>2004-2005</year>
51 <holder>MontaVista Software, Inc.</holder>
52 </copyright>
53 <copyright>
54 <year>2004</year>
55 <holder>Amit S. Kale</holder>
56 </copyright>
57
58 <legalnotice>
59 <para>
60 This file is licensed under the terms of the GNU General Public License
61 version 2. This program is licensed "as is" without any warranty of any
62 kind, whether express or implied.
63 </para>
64
65 </legalnotice>
66 </bookinfo>
67
68<toc></toc>
69 <chapter id="Introduction">
70 <title>Introduction</title>
71 <para>
72 kgdb is a source level debugger for linux kernel. It is used along
73 with gdb to debug a linux kernel. The expectation is that gdb can
74 be used to "break in" to the kernel to inspect memory, variables
75 and look through a cal stack information similar to what an
76 application developer would use gdb for. It is possible to place
77 breakpoints in kernel code and perform some limited execution
78 stepping.
79 </para>
80 <para>
81 Two machines are required for using kgdb. One of these machines is a
82 development machine and the other is a test machine. The kernel
83 to be debugged runs on the test machine. The development machine
84 runs an instance of gdb against the vmlinux file which contains
85 the symbols (not boot image such as bzImage, zImage, uImage...).
86 In gdb the developer specifies the connection parameters and
87 connects to kgdb. Depending on which kgdb I/O modules exist in
88 the kernel for a given architecture, it may be possible to debug
89 the test machine's kernel with the development machine using a
90 rs232 or ethernet connection.
91 </para>
92 </chapter>
93 <chapter id="CompilingAKernel">
94 <title>Compiling a kernel</title>
95 <para>
96 To enable <symbol>CONFIG_KGDB</symbol>, look under the "Kernel debugging"
97 and then select "KGDB: kernel debugging with remote gdb".
98 </para>
99 <para>
100 Next you should choose one of more I/O drivers to interconnect debugging
101 host and debugged target. Early boot debugging requires a KGDB
102 I/O driver that supports early debugging and the driver must be
103 built into the kernel directly. Kgdb I/O driver configuration
104 takes place via kernel or module parameters, see following
105 chapter.
106 </para>
107 <para>
108 The kgdb test compile options are described in the kgdb test suite chapter.
109 </para>
110
111 </chapter>
112 <chapter id="EnableKGDB">
113 <title>Enable kgdb for debugging</title>
114 <para>
115 In order to use kgdb you must activate it by passing configuration
116 information to one of the kgdb I/O drivers. If you do not pass any
117 configuration information kgdb will not do anything at all. Kgdb
118 will only actively hook up to the kernel trap hooks if a kgdb I/O
119 driver is loaded and configured. If you unconfigure a kgdb I/O
120 driver, kgdb will unregister all the kernel hook points.
121 </para>
122 <para>
123 All drivers can be reconfigured at run time, if
124 <symbol>CONFIG_SYSFS</symbol> and <symbol>CONFIG_MODULES</symbol>
125 are enabled, by echo'ing a new config string to
126 <constant>/sys/module/&lt;driver&gt;/parameter/&lt;option&gt;</constant>.
127 The driver can be unconfigured by passing an empty string. You cannot
128 change the configuration while the debugger is attached. Make sure
129 to detach the debugger with the <constant>detach</constant> command
130 prior to trying unconfigure a kgdb I/O driver.
131 </para>
132 <sect1 id="kgdbwait">
133 <title>Kernel parameter: kgdbwait</title>
134 <para>
135 The Kernel command line option <constant>kgdbwait</constant> makes
136 kgdb wait for a debugger connection during booting of a kernel. You
137 can only use this option you compiled a kgdb I/O driver into the
138 kernel and you specified the I/O driver configuration as a kernel
139 command line option. The kgdbwait parameter should always follow the
140 configuration parameter for the kgdb I/O driver in the kernel
141 command line else the I/O driver will not be configured prior to
142 asking the kernel to use it to wait.
143 </para>
144 <para>
145 The kernel will stop and wait as early as the I/O driver and
146 architecture will allow when you use this option. If you build the
147 kgdb I/O driver as a kernel module kgdbwait will not do anything.
148 </para>
149 </sect1>
150 <sect1 id="kgdboc">
151 <title>Kernel parameter: kgdboc</title>
152 <para>
153 The kgdboc driver was originally an abbreviation meant to stand for
154 "kgdb over console". Kgdboc is designed to work with a single
155 serial port. It was meant to cover the circumstance
156 where you wanted to use a serial console as your primary console as
157 well as using it to perform kernel debugging. Of course you can
158 also use kgdboc without assigning a console to the same port.
159 </para>
160 <sect2 id="UsingKgdboc">
161 <title>Using kgdboc</title>
162 <para>
163 You can configure kgdboc via sysfs or a module or kernel boot line
164 parameter depending on if you build with CONFIG_KGDBOC as a module
165 or built-in.
166 <orderedlist>
167 <listitem><para>From the module load or build-in</para>
168 <para><constant>kgdboc=&lt;tty-device&gt;,[baud]</constant></para>
169 <para>
170 The example here would be if your console port was typically ttyS0, you would use something like <constant>kgdboc=ttyS0,115200</constant> or on the ARM Versatile AB you would likely use <constant>kgdboc=ttyAMA0,115200</constant>
171 </para>
172 </listitem>
173 <listitem><para>From sysfs</para>
174 <para><constant>echo ttyS0 &gt; /sys/module/kgdboc/parameters/kgdboc</constant></para>
175 </listitem>
176 </orderedlist>
177 </para>
178 <para>
179 NOTE: Kgdboc does not support interrupting the target via the
180 gdb remote protocol. You must manually send a sysrq-g unless you
181 have a proxy that splits console output to a terminal problem and
182 has a separate port for the debugger to connect to that sends the
183 sysrq-g for you.
184 </para>
185 <para>When using kgdboc with no debugger proxy, you can end up
186 connecting the debugger for one of two entry points. If an
187 exception occurs after you have loaded kgdboc a message should print
188 on the console stating it is waiting for the debugger. In case you
189 disconnect your terminal program and then connect the debugger in
190 its place. If you want to interrupt the target system and forcibly
191 enter a debug session you have to issue a Sysrq sequence and then
192 type the letter <constant>g</constant>. Then you disconnect the
193 terminal session and connect gdb. Your options if you don't like
194 this are to hack gdb to send the sysrq-g for you as well as on the
195 initial connect, or to use a debugger proxy that allows an
196 unmodified gdb to do the debugging.
197 </para>
198 </sect2>
199 </sect1>
200 <sect1 id="kgdbcon">
201 <title>Kernel parameter: kgdbcon</title>
202 <para>
203 Kgdb supports using the gdb serial protocol to send console messages
204 to the debugger when the debugger is connected and running. There
205 are two ways to activate this feature.
206 <orderedlist>
207 <listitem><para>Activate with the kernel command line option:</para>
208 <para><constant>kgdbcon</constant></para>
209 </listitem>
210 <listitem><para>Use sysfs before configuring an io driver</para>
211 <para>
212 <constant>echo 1 &gt; /sys/module/kgdb/parameters/kgdb_use_con</constant>
213 </para>
214 <para>
215 NOTE: If you do this after you configure the kgdb I/O driver, the
216 setting will not take effect until the next point the I/O is
217 reconfigured.
218 </para>
219 </listitem>
220 </orderedlist>
221 </para>
222 <para>
223 IMPORTANT NOTE: Using this option with kgdb over the console
224 (kgdboc) or kgdb over ethernet (kgdboe) is not supported.
225 </para>
226 </sect1>
227 </chapter>
228 <chapter id="ConnectingGDB">
229 <title>Connecting gdb</title>
230 <para>
231 If you are using kgdboc, you need to have used kgdbwait as a boot
232 argument, issued a sysrq-g, or the system you are going to debug
233 has already taken an exception and is waiting for the debugger to
234 attach before you can connect gdb.
235 </para>
236 <para>
237 If you are not using different kgdb I/O driver other than kgdboc,
238 you should be able to connect and the target will automatically
239 respond.
240 </para>
241 <para>
242 Example (using a serial port):
243 </para>
244 <programlisting>
245 % gdb ./vmlinux
246 (gdb) set remotebaud 115200
247 (gdb) target remote /dev/ttyS0
248 </programlisting>
249 <para>
250 Example (kgdb to a terminal server):
251 </para>
252 <programlisting>
253 % gdb ./vmlinux
254 (gdb) target remote udp:192.168.2.2:6443
255 </programlisting>
256 <para>
257 Example (kgdb over ethernet):
258 </para>
259 <programlisting>
260 % gdb ./vmlinux
261 (gdb) target remote udp:192.168.2.2:6443
262 </programlisting>
263 <para>
264 Once connected, you can debug a kernel the way you would debug an
265 application program.
266 </para>
267 <para>
268 If you are having problems connecting or something is going
269 seriously wrong while debugging, it will most often be the case
270 that you want to enable gdb to be verbose about its target
271 communications. You do this prior to issuing the <constant>target
272 remote</constant> command by typing in: <constant>set remote debug 1</constant>
273 </para>
274 </chapter>
275 <chapter id="KGDBTestSuite">
276 <title>kgdb Test Suite</title>
277 <para>
278 When kgdb is enabled in the kernel config you can also elect to
279 enable the config parameter KGDB_TESTS. Turning this on will
280 enable a special kgdb I/O module which is designed to test the
281 kgdb internal functions.
282 </para>
283 <para>
284 The kgdb tests are mainly intended for developers to test the kgdb
285 internals as well as a tool for developing a new kgdb architecture
286 specific implementation. These tests are not really for end users
287 of the Linux kernel. The primary source of documentation would be
288 to look in the drivers/misc/kgdbts.c file.
289 </para>
290 <para>
291 The kgdb test suite can also be configured at compile time to run
292 the core set of tests by setting the kernel config parameter
293 KGDB_TESTS_ON_BOOT. This particular option is aimed at automated
294 regression testing and does not require modifying the kernel boot
295 config arguments. If this is turned on, the kgdb test suite can
296 be disabled by specifying "kgdbts=" as a kernel boot argument.
297 </para>
298 </chapter>
299 <chapter id="CommonBackEndReq">
300 <title>KGDB Internals</title>
301 <sect1 id="kgdbArchitecture">
302 <title>Architecture Specifics</title>
303 <para>
304 Kgdb is organized into three basic components:
305 <orderedlist>
306 <listitem><para>kgdb core</para>
307 <para>
308 The kgdb core is found in kernel/kgdb.c. It contains:
309 <itemizedlist>
310 <listitem><para>All the logic to implement the gdb serial protocol</para></listitem>
311 <listitem><para>A generic OS exception handler which includes sync'ing the processors into a stopped state on an multi cpu system.</para></listitem>
312 <listitem><para>The API to talk to the kgdb I/O drivers</para></listitem>
313 <listitem><para>The API to make calls to the arch specific kgdb implementation</para></listitem>
314 <listitem><para>The logic to perform safe memory reads and writes to memory while using the debugger</para></listitem>
315 <listitem><para>A full implementation for software breakpoints unless overridden by the arch</para></listitem>
316 </itemizedlist>
317 </para>
318 </listitem>
319 <listitem><para>kgdb arch specific implementation</para>
320 <para>
321 This implementation is generally found in arch/*/kernel/kgdb.c.
322 As an example, arch/x86/kernel/kgdb.c contains the specifics to
323 implement HW breakpoint as well as the initialization to
324 dynamically register and unregister for the trap handlers on
325 this architecture. The arch specific portion implements:
326 <itemizedlist>
327 <listitem><para>contains an arch specific trap catcher which
328 invokes kgdb_handle_exception() to start kgdb about doing its
329 work</para></listitem>
330 <listitem><para>translation to and from gdb specific packet format to pt_regs</para></listitem>
331 <listitem><para>Registration and unregistration of architecture specific trap hooks</para></listitem>
332 <listitem><para>Any special exception handling and cleanup</para></listitem>
333 <listitem><para>NMI exception handling and cleanup</para></listitem>
334 <listitem><para>(optional)HW breakpoints</para></listitem>
335 </itemizedlist>
336 </para>
337 </listitem>
338 <listitem><para>kgdb I/O driver</para>
339 <para>
340 Each kgdb I/O driver has to provide an implemenation for the following:
341 <itemizedlist>
342 <listitem><para>configuration via builtin or module</para></listitem>
343 <listitem><para>dynamic configuration and kgdb hook registration calls</para></listitem>
344 <listitem><para>read and write character interface</para></listitem>
345 <listitem><para>A cleanup handler for unconfiguring from the kgdb core</para></listitem>
346 <listitem><para>(optional) Early debug methodology</para></listitem>
347 </itemizedlist>
348 Any given kgdb I/O driver has to operate very closely with the
349 hardware and must do it in such a way that does not enable
350 interrupts or change other parts of the system context without
351 completely restoring them. The kgdb core will repeatedly "poll"
352 a kgdb I/O driver for characters when it needs input. The I/O
353 driver is expected to return immediately if there is no data
354 available. Doing so allows for the future possibility to touch
355 watch dog hardware in such a way as to have a target system not
356 reset when these are enabled.
357 </para>
358 </listitem>
359 </orderedlist>
360 </para>
361 <para>
362 If you are intent on adding kgdb architecture specific support
363 for a new architecture, the architecture should define
364 <constant>HAVE_ARCH_KGDB</constant> in the architecture specific
365 Kconfig file. This will enable kgdb for the architecture, and
366 at that point you must create an architecture specific kgdb
367 implementation.
368 </para>
369 <para>
370 There are a few flags which must be set on every architecture in
371 their &lt;asm/kgdb.h&gt; file. These are:
372 <itemizedlist>
373 <listitem>
374 <para>
375 NUMREGBYTES: The size in bytes of all of the registers, so
376 that we can ensure they will all fit into a packet.
377 </para>
378 <para>
379 BUFMAX: The size in bytes of the buffer GDB will read into.
380 This must be larger than NUMREGBYTES.
381 </para>
382 <para>
383 CACHE_FLUSH_IS_SAFE: Set to 1 if it is always safe to call
384 flush_cache_range or flush_icache_range. On some architectures,
385 these functions may not be safe to call on SMP since we keep other
386 CPUs in a holding pattern.
387 </para>
388 </listitem>
389 </itemizedlist>
390 </para>
391 <para>
392 There are also the following functions for the common backend,
393 found in kernel/kgdb.c, that must be supplied by the
394 architecture-specific backend unless marked as (optional), in
395 which case a default function maybe used if the architecture
396 does not need to provide a specific implementation.
397 </para>
398!Iinclude/linux/kgdb.h
399 </sect1>
400 <sect1 id="kgdbocDesign">
401 <title>kgdboc internals</title>
402 <para>
403 The kgdboc driver is actually a very thin driver that relies on the
404 underlying low level to the hardware driver having "polling hooks"
405 which the to which the tty driver is attached. In the initial
406 implementation of kgdboc it the serial_core was changed to expose a
407 low level uart hook for doing polled mode reading and writing of a
408 single character while in an atomic context. When kgdb makes an I/O
409 request to the debugger, kgdboc invokes a call back in the serial
410 core which in turn uses the call back in the uart driver. It is
411 certainly possible to extend kgdboc to work with non-uart based
412 consoles in the future.
413 </para>
414 <para>
415 When using kgdboc with a uart, the uart driver must implement two callbacks in the <constant>struct uart_ops</constant>. Example from drivers/8250.c:<programlisting>
416#ifdef CONFIG_CONSOLE_POLL
417 .poll_get_char = serial8250_get_poll_char,
418 .poll_put_char = serial8250_put_poll_char,
419#endif
420 </programlisting>
421 Any implementation specifics around creating a polling driver use the
422 <constant>#ifdef CONFIG_CONSOLE_POLL</constant>, as shown above.
423 Keep in mind that polling hooks have to be implemented in such a way
424 that they can be called from an atomic context and have to restore
425 the state of the uart chip on return such that the system can return
426 to normal when the debugger detaches. You need to be very careful
427 with any kind of lock you consider, because failing here is most
428 going to mean pressing the reset button.
429 </para>
430 </sect1>
431 </chapter>
432 <chapter id="credits">
433 <title>Credits</title>
434 <para>
435 The following people have contributed to this document:
436 <orderedlist>
437 <listitem><para>Amit Kale<email>amitkale@linsyssoft.com</email></para></listitem>
438 <listitem><para>Tom Rini<email>trini@kernel.crashing.org</email></para></listitem>
439 </orderedlist>
440 In March 2008 this document was completely rewritten by:
441 <itemizedlist>
442 <listitem><para>Jason Wessel<email>jason.wessel@windriver.com</email></para></listitem>
443 </itemizedlist>
444 </para>
445 </chapter>
446</book>
447
diff --git a/Documentation/kernel-parameters.txt b/Documentation/kernel-parameters.txt
index f9ea0803d5d6..c867f506366d 100644
--- a/Documentation/kernel-parameters.txt
+++ b/Documentation/kernel-parameters.txt
@@ -941,6 +941,11 @@ and is between 256 and 4096 characters. It is defined in the file
941 kstack=N [X86-32,X86-64] Print N words from the kernel stack 941 kstack=N [X86-32,X86-64] Print N words from the kernel stack
942 in oops dumps. 942 in oops dumps.
943 943
944 kgdboc= [HW] kgdb over consoles.
945 Requires a tty driver that supports console polling.
946 (only serial suported for now)
947 Format: <serial_device>[,baud]
948
944 l2cr= [PPC] 949 l2cr= [PPC]
945 950
946 lapic [X86-32,APIC] Enable the local APIC even if BIOS 951 lapic [X86-32,APIC] Enable the local APIC even if BIOS
diff --git a/MAINTAINERS b/MAINTAINERS
index e46775868019..3eceebb48c92 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2319,6 +2319,12 @@ L: linux-kernel@vger.kernel.org
2319L: kexec@lists.infradead.org 2319L: kexec@lists.infradead.org
2320S: Maintained 2320S: Maintained
2321 2321
2322KGDB
2323P: Jason Wessel
2324M: jason.wessel@windriver.com
2325L: kgdb-bugreport@lists.sourceforge.net
2326S: Maintained
2327
2322KPROBES 2328KPROBES
2323P: Ananth N Mavinakayanahalli 2329P: Ananth N Mavinakayanahalli
2324M: ananth@in.ibm.com 2330M: ananth@in.ibm.com
diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index 701c4a27a731..2a59dbb28248 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -23,6 +23,7 @@ config X86
23 select HAVE_KPROBES 23 select HAVE_KPROBES
24 select HAVE_KRETPROBES 24 select HAVE_KRETPROBES
25 select HAVE_KVM if ((X86_32 && !X86_VOYAGER && !X86_VISWS && !X86_NUMAQ) || X86_64) 25 select HAVE_KVM if ((X86_32 && !X86_VOYAGER && !X86_VISWS && !X86_NUMAQ) || X86_64)
26 select HAVE_ARCH_KGDB
26 27
27 28
28config GENERIC_LOCKBREAK 29config GENERIC_LOCKBREAK
diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile
index 530ed6a4a031..c3920ea8ac56 100644
--- a/arch/x86/kernel/Makefile
+++ b/arch/x86/kernel/Makefile
@@ -67,6 +67,7 @@ obj-$(CONFIG_MODULES) += module_$(BITS).o
67obj-$(CONFIG_ACPI_SRAT) += srat_32.o 67obj-$(CONFIG_ACPI_SRAT) += srat_32.o
68obj-$(CONFIG_EFI) += efi.o efi_$(BITS).o efi_stub_$(BITS).o 68obj-$(CONFIG_EFI) += efi.o efi_$(BITS).o efi_stub_$(BITS).o
69obj-$(CONFIG_DOUBLEFAULT) += doublefault_32.o 69obj-$(CONFIG_DOUBLEFAULT) += doublefault_32.o
70obj-$(CONFIG_KGDB) += kgdb.o
70obj-$(CONFIG_VM86) += vm86_32.o 71obj-$(CONFIG_VM86) += vm86_32.o
71obj-$(CONFIG_EARLY_PRINTK) += early_printk.o 72obj-$(CONFIG_EARLY_PRINTK) += early_printk.o
72 73
diff --git a/arch/x86/kernel/kgdb.c b/arch/x86/kernel/kgdb.c
new file mode 100644
index 000000000000..8c7e555f6d39
--- /dev/null
+++ b/arch/x86/kernel/kgdb.c
@@ -0,0 +1,571 @@
1/*
2 * This program is free software; you can redistribute it and/or modify it
3 * under the terms of the GNU General Public License as published by the
4 * Free Software Foundation; either version 2, or (at your option) any
5 * later version.
6 *
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
11 *
12 */
13
14/*
15 * Copyright (C) 2004 Amit S. Kale <amitkale@linsyssoft.com>
16 * Copyright (C) 2000-2001 VERITAS Software Corporation.
17 * Copyright (C) 2002 Andi Kleen, SuSE Labs
18 * Copyright (C) 2004 LinSysSoft Technologies Pvt. Ltd.
19 * Copyright (C) 2007 MontaVista Software, Inc.
20 * Copyright (C) 2007-2008 Jason Wessel, Wind River Systems, Inc.
21 */
22/****************************************************************************
23 * Contributor: Lake Stevens Instrument Division$
24 * Written by: Glenn Engel $
25 * Updated by: Amit Kale<akale@veritas.com>
26 * Updated by: Tom Rini <trini@kernel.crashing.org>
27 * Updated by: Jason Wessel <jason.wessel@windriver.com>
28 * Modified for 386 by Jim Kingdon, Cygnus Support.
29 * Origianl kgdb, compatibility with 2.1.xx kernel by
30 * David Grothe <dave@gcom.com>
31 * Integrated into 2.2.5 kernel by Tigran Aivazian <tigran@sco.com>
32 * X86_64 changes from Andi Kleen's patch merged by Jim Houston
33 */
34#include <linux/spinlock.h>
35#include <linux/kdebug.h>
36#include <linux/string.h>
37#include <linux/kernel.h>
38#include <linux/ptrace.h>
39#include <linux/sched.h>
40#include <linux/delay.h>
41#include <linux/kgdb.h>
42#include <linux/init.h>
43#include <linux/smp.h>
44#include <linux/nmi.h>
45
46#include <asm/apicdef.h>
47#include <asm/system.h>
48
49#ifdef CONFIG_X86_32
50# include <mach_ipi.h>
51#else
52# include <asm/mach_apic.h>
53#endif
54
55/*
56 * Put the error code here just in case the user cares:
57 */
58static int gdb_x86errcode;
59
60/*
61 * Likewise, the vector number here (since GDB only gets the signal
62 * number through the usual means, and that's not very specific):
63 */
64static int gdb_x86vector = -1;
65
66/**
67 * pt_regs_to_gdb_regs - Convert ptrace regs to GDB regs
68 * @gdb_regs: A pointer to hold the registers in the order GDB wants.
69 * @regs: The &struct pt_regs of the current process.
70 *
71 * Convert the pt_regs in @regs into the format for registers that
72 * GDB expects, stored in @gdb_regs.
73 */
74void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs)
75{
76 gdb_regs[GDB_AX] = regs->ax;
77 gdb_regs[GDB_BX] = regs->bx;
78 gdb_regs[GDB_CX] = regs->cx;
79 gdb_regs[GDB_DX] = regs->dx;
80 gdb_regs[GDB_SI] = regs->si;
81 gdb_regs[GDB_DI] = regs->di;
82 gdb_regs[GDB_BP] = regs->bp;
83 gdb_regs[GDB_PS] = regs->flags;
84 gdb_regs[GDB_PC] = regs->ip;
85#ifdef CONFIG_X86_32
86 gdb_regs[GDB_DS] = regs->ds;
87 gdb_regs[GDB_ES] = regs->es;
88 gdb_regs[GDB_CS] = regs->cs;
89 gdb_regs[GDB_SS] = __KERNEL_DS;
90 gdb_regs[GDB_FS] = 0xFFFF;
91 gdb_regs[GDB_GS] = 0xFFFF;
92#else
93 gdb_regs[GDB_R8] = regs->r8;
94 gdb_regs[GDB_R9] = regs->r9;
95 gdb_regs[GDB_R10] = regs->r10;
96 gdb_regs[GDB_R11] = regs->r11;
97 gdb_regs[GDB_R12] = regs->r12;
98 gdb_regs[GDB_R13] = regs->r13;
99 gdb_regs[GDB_R14] = regs->r14;
100 gdb_regs[GDB_R15] = regs->r15;
101#endif
102 gdb_regs[GDB_SP] = regs->sp;
103}
104
105/**
106 * sleeping_thread_to_gdb_regs - Convert ptrace regs to GDB regs
107 * @gdb_regs: A pointer to hold the registers in the order GDB wants.
108 * @p: The &struct task_struct of the desired process.
109 *
110 * Convert the register values of the sleeping process in @p to
111 * the format that GDB expects.
112 * This function is called when kgdb does not have access to the
113 * &struct pt_regs and therefore it should fill the gdb registers
114 * @gdb_regs with what has been saved in &struct thread_struct
115 * thread field during switch_to.
116 */
117void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
118{
119 gdb_regs[GDB_AX] = 0;
120 gdb_regs[GDB_BX] = 0;
121 gdb_regs[GDB_CX] = 0;
122 gdb_regs[GDB_DX] = 0;
123 gdb_regs[GDB_SI] = 0;
124 gdb_regs[GDB_DI] = 0;
125 gdb_regs[GDB_BP] = *(unsigned long *)p->thread.sp;
126#ifdef CONFIG_X86_32
127 gdb_regs[GDB_DS] = __KERNEL_DS;
128 gdb_regs[GDB_ES] = __KERNEL_DS;
129 gdb_regs[GDB_PS] = 0;
130 gdb_regs[GDB_CS] = __KERNEL_CS;
131 gdb_regs[GDB_PC] = p->thread.ip;
132 gdb_regs[GDB_SS] = __KERNEL_DS;
133 gdb_regs[GDB_FS] = 0xFFFF;
134 gdb_regs[GDB_GS] = 0xFFFF;
135#else
136 gdb_regs[GDB_PS] = *(unsigned long *)(p->thread.sp + 8);
137 gdb_regs[GDB_PC] = 0;
138 gdb_regs[GDB_R8] = 0;
139 gdb_regs[GDB_R9] = 0;
140 gdb_regs[GDB_R10] = 0;
141 gdb_regs[GDB_R11] = 0;
142 gdb_regs[GDB_R12] = 0;
143 gdb_regs[GDB_R13] = 0;
144 gdb_regs[GDB_R14] = 0;
145 gdb_regs[GDB_R15] = 0;
146#endif
147 gdb_regs[GDB_SP] = p->thread.sp;
148}
149
150/**
151 * gdb_regs_to_pt_regs - Convert GDB regs to ptrace regs.
152 * @gdb_regs: A pointer to hold the registers we've received from GDB.
153 * @regs: A pointer to a &struct pt_regs to hold these values in.
154 *
155 * Convert the GDB regs in @gdb_regs into the pt_regs, and store them
156 * in @regs.
157 */
158void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs)
159{
160 regs->ax = gdb_regs[GDB_AX];
161 regs->bx = gdb_regs[GDB_BX];
162 regs->cx = gdb_regs[GDB_CX];
163 regs->dx = gdb_regs[GDB_DX];
164 regs->si = gdb_regs[GDB_SI];
165 regs->di = gdb_regs[GDB_DI];
166 regs->bp = gdb_regs[GDB_BP];
167 regs->flags = gdb_regs[GDB_PS];
168 regs->ip = gdb_regs[GDB_PC];
169#ifdef CONFIG_X86_32
170 regs->ds = gdb_regs[GDB_DS];
171 regs->es = gdb_regs[GDB_ES];
172 regs->cs = gdb_regs[GDB_CS];
173#else
174 regs->r8 = gdb_regs[GDB_R8];
175 regs->r9 = gdb_regs[GDB_R9];
176 regs->r10 = gdb_regs[GDB_R10];
177 regs->r11 = gdb_regs[GDB_R11];
178 regs->r12 = gdb_regs[GDB_R12];
179 regs->r13 = gdb_regs[GDB_R13];
180 regs->r14 = gdb_regs[GDB_R14];
181 regs->r15 = gdb_regs[GDB_R15];
182#endif
183}
184
185static struct hw_breakpoint {
186 unsigned enabled;
187 unsigned type;
188 unsigned len;
189 unsigned long addr;
190} breakinfo[4];
191
192static void kgdb_correct_hw_break(void)
193{
194 unsigned long dr7;
195 int correctit = 0;
196 int breakbit;
197 int breakno;
198
199 get_debugreg(dr7, 7);
200 for (breakno = 0; breakno < 4; breakno++) {
201 breakbit = 2 << (breakno << 1);
202 if (!(dr7 & breakbit) && breakinfo[breakno].enabled) {
203 correctit = 1;
204 dr7 |= breakbit;
205 dr7 &= ~(0xf0000 << (breakno << 2));
206 dr7 |= ((breakinfo[breakno].len << 2) |
207 breakinfo[breakno].type) <<
208 ((breakno << 2) + 16);
209 if (breakno >= 0 && breakno <= 3)
210 set_debugreg(breakinfo[breakno].addr, breakno);
211
212 } else {
213 if ((dr7 & breakbit) && !breakinfo[breakno].enabled) {
214 correctit = 1;
215 dr7 &= ~breakbit;
216 dr7 &= ~(0xf0000 << (breakno << 2));
217 }
218 }
219 }
220 if (correctit)
221 set_debugreg(dr7, 7);
222}
223
224static int
225kgdb_remove_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
226{
227 int i;
228
229 for (i = 0; i < 4; i++)
230 if (breakinfo[i].addr == addr && breakinfo[i].enabled)
231 break;
232 if (i == 4)
233 return -1;
234
235 breakinfo[i].enabled = 0;
236
237 return 0;
238}
239
240static void kgdb_remove_all_hw_break(void)
241{
242 int i;
243
244 for (i = 0; i < 4; i++)
245 memset(&breakinfo[i], 0, sizeof(struct hw_breakpoint));
246}
247
248static int
249kgdb_set_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
250{
251 unsigned type;
252 int i;
253
254 for (i = 0; i < 4; i++)
255 if (!breakinfo[i].enabled)
256 break;
257 if (i == 4)
258 return -1;
259
260 switch (bptype) {
261 case BP_HARDWARE_BREAKPOINT:
262 type = 0;
263 len = 1;
264 break;
265 case BP_WRITE_WATCHPOINT:
266 type = 1;
267 break;
268 case BP_ACCESS_WATCHPOINT:
269 type = 3;
270 break;
271 default:
272 return -1;
273 }
274
275 if (len == 1 || len == 2 || len == 4)
276 breakinfo[i].len = len - 1;
277 else
278 return -1;
279
280 breakinfo[i].enabled = 1;
281 breakinfo[i].addr = addr;
282 breakinfo[i].type = type;
283
284 return 0;
285}
286
287/**
288 * kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb.
289 * @regs: Current &struct pt_regs.
290 *
291 * This function will be called if the particular architecture must
292 * disable hardware debugging while it is processing gdb packets or
293 * handling exception.
294 */
295void kgdb_disable_hw_debug(struct pt_regs *regs)
296{
297 /* Disable hardware debugging while we are in kgdb: */
298 set_debugreg(0UL, 7);
299}
300
301/**
302 * kgdb_post_primary_code - Save error vector/code numbers.
303 * @regs: Original pt_regs.
304 * @e_vector: Original error vector.
305 * @err_code: Original error code.
306 *
307 * This is needed on architectures which support SMP and KGDB.
308 * This function is called after all the slave cpus have been put
309 * to a know spin state and the primary CPU has control over KGDB.
310 */
311void kgdb_post_primary_code(struct pt_regs *regs, int e_vector, int err_code)
312{
313 /* primary processor is completely in the debugger */
314 gdb_x86vector = e_vector;
315 gdb_x86errcode = err_code;
316}
317
318#ifdef CONFIG_SMP
319/**
320 * kgdb_roundup_cpus - Get other CPUs into a holding pattern
321 * @flags: Current IRQ state
322 *
323 * On SMP systems, we need to get the attention of the other CPUs
324 * and get them be in a known state. This should do what is needed
325 * to get the other CPUs to call kgdb_wait(). Note that on some arches,
326 * the NMI approach is not used for rounding up all the CPUs. For example,
327 * in case of MIPS, smp_call_function() is used to roundup CPUs. In
328 * this case, we have to make sure that interrupts are enabled before
329 * calling smp_call_function(). The argument to this function is
330 * the flags that will be used when restoring the interrupts. There is
331 * local_irq_save() call before kgdb_roundup_cpus().
332 *
333 * On non-SMP systems, this is not called.
334 */
335void kgdb_roundup_cpus(unsigned long flags)
336{
337 send_IPI_allbutself(APIC_DM_NMI);
338}
339#endif
340
341/**
342 * kgdb_arch_handle_exception - Handle architecture specific GDB packets.
343 * @vector: The error vector of the exception that happened.
344 * @signo: The signal number of the exception that happened.
345 * @err_code: The error code of the exception that happened.
346 * @remcom_in_buffer: The buffer of the packet we have read.
347 * @remcom_out_buffer: The buffer of %BUFMAX bytes to write a packet into.
348 * @regs: The &struct pt_regs of the current process.
349 *
350 * This function MUST handle the 'c' and 's' command packets,
351 * as well packets to set / remove a hardware breakpoint, if used.
352 * If there are additional packets which the hardware needs to handle,
353 * they are handled here. The code should return -1 if it wants to
354 * process more packets, and a %0 or %1 if it wants to exit from the
355 * kgdb callback.
356 */
357int kgdb_arch_handle_exception(int e_vector, int signo, int err_code,
358 char *remcomInBuffer, char *remcomOutBuffer,
359 struct pt_regs *linux_regs)
360{
361 unsigned long addr;
362 unsigned long dr6;
363 char *ptr;
364 int newPC;
365
366 switch (remcomInBuffer[0]) {
367 case 'c':
368 case 's':
369 /* try to read optional parameter, pc unchanged if no parm */
370 ptr = &remcomInBuffer[1];
371 if (kgdb_hex2long(&ptr, &addr))
372 linux_regs->ip = addr;
373 case 'D':
374 case 'k':
375 newPC = linux_regs->ip;
376
377 /* clear the trace bit */
378 linux_regs->flags &= ~TF_MASK;
379 atomic_set(&kgdb_cpu_doing_single_step, -1);
380
381 /* set the trace bit if we're stepping */
382 if (remcomInBuffer[0] == 's') {
383 linux_regs->flags |= TF_MASK;
384 kgdb_single_step = 1;
385 if (kgdb_contthread) {
386 atomic_set(&kgdb_cpu_doing_single_step,
387 raw_smp_processor_id());
388 }
389 }
390
391 get_debugreg(dr6, 6);
392 if (!(dr6 & 0x4000)) {
393 int breakno;
394
395 for (breakno = 0; breakno < 4; breakno++) {
396 if (dr6 & (1 << breakno) &&
397 breakinfo[breakno].type == 0) {
398 /* Set restore flag: */
399 linux_regs->flags |= X86_EFLAGS_RF;
400 break;
401 }
402 }
403 }
404 set_debugreg(0UL, 6);
405 kgdb_correct_hw_break();
406
407 return 0;
408 }
409
410 /* this means that we do not want to exit from the handler: */
411 return -1;
412}
413
414static inline int
415single_step_cont(struct pt_regs *regs, struct die_args *args)
416{
417 /*
418 * Single step exception from kernel space to user space so
419 * eat the exception and continue the process:
420 */
421 printk(KERN_ERR "KGDB: trap/step from kernel to user space, "
422 "resuming...\n");
423 kgdb_arch_handle_exception(args->trapnr, args->signr,
424 args->err, "c", "", regs);
425
426 return NOTIFY_STOP;
427}
428
429static int was_in_debug_nmi[NR_CPUS];
430
431static int __kgdb_notify(struct die_args *args, unsigned long cmd)
432{
433 struct pt_regs *regs = args->regs;
434
435 switch (cmd) {
436 case DIE_NMI:
437 if (atomic_read(&kgdb_active) != -1) {
438 /* KGDB CPU roundup */
439 kgdb_nmicallback(raw_smp_processor_id(), regs);
440 was_in_debug_nmi[raw_smp_processor_id()] = 1;
441 touch_nmi_watchdog();
442 return NOTIFY_STOP;
443 }
444 return NOTIFY_DONE;
445
446 case DIE_NMI_IPI:
447 if (atomic_read(&kgdb_active) != -1) {
448 /* KGDB CPU roundup */
449 kgdb_nmicallback(raw_smp_processor_id(), regs);
450 was_in_debug_nmi[raw_smp_processor_id()] = 1;
451 touch_nmi_watchdog();
452 }
453 return NOTIFY_DONE;
454
455 case DIE_NMIUNKNOWN:
456 if (was_in_debug_nmi[raw_smp_processor_id()]) {
457 was_in_debug_nmi[raw_smp_processor_id()] = 0;
458 return NOTIFY_STOP;
459 }
460 return NOTIFY_DONE;
461
462 case DIE_NMIWATCHDOG:
463 if (atomic_read(&kgdb_active) != -1) {
464 /* KGDB CPU roundup: */
465 kgdb_nmicallback(raw_smp_processor_id(), regs);
466 return NOTIFY_STOP;
467 }
468 /* Enter debugger: */
469 break;
470
471 case DIE_DEBUG:
472 if (atomic_read(&kgdb_cpu_doing_single_step) ==
473 raw_smp_processor_id() &&
474 user_mode(regs))
475 return single_step_cont(regs, args);
476 /* fall through */
477 default:
478 if (user_mode(regs))
479 return NOTIFY_DONE;
480 }
481
482 if (kgdb_handle_exception(args->trapnr, args->signr, args->err, regs))
483 return NOTIFY_DONE;
484
485 /* Must touch watchdog before return to normal operation */
486 touch_nmi_watchdog();
487 return NOTIFY_STOP;
488}
489
490static int
491kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
492{
493 unsigned long flags;
494 int ret;
495
496 local_irq_save(flags);
497 ret = __kgdb_notify(ptr, cmd);
498 local_irq_restore(flags);
499
500 return ret;
501}
502
503static struct notifier_block kgdb_notifier = {
504 .notifier_call = kgdb_notify,
505
506 /*
507 * Lowest-prio notifier priority, we want to be notified last:
508 */
509 .priority = -INT_MAX,
510};
511
512/**
513 * kgdb_arch_init - Perform any architecture specific initalization.
514 *
515 * This function will handle the initalization of any architecture
516 * specific callbacks.
517 */
518int kgdb_arch_init(void)
519{
520 return register_die_notifier(&kgdb_notifier);
521}
522
523/**
524 * kgdb_arch_exit - Perform any architecture specific uninitalization.
525 *
526 * This function will handle the uninitalization of any architecture
527 * specific callbacks, for dynamic registration and unregistration.
528 */
529void kgdb_arch_exit(void)
530{
531 unregister_die_notifier(&kgdb_notifier);
532}
533
534/**
535 *
536 * kgdb_skipexception - Bail out of KGDB when we've been triggered.
537 * @exception: Exception vector number
538 * @regs: Current &struct pt_regs.
539 *
540 * On some architectures we need to skip a breakpoint exception when
541 * it occurs after a breakpoint has been removed.
542 *
543 * Skip an int3 exception when it occurs after a breakpoint has been
544 * removed. Backtrack eip by 1 since the int3 would have caused it to
545 * increment by 1.
546 */
547int kgdb_skipexception(int exception, struct pt_regs *regs)
548{
549 if (exception == 3 && kgdb_isremovedbreak(regs->ip - 1)) {
550 regs->ip -= 1;
551 return 1;
552 }
553 return 0;
554}
555
556unsigned long kgdb_arch_pc(int exception, struct pt_regs *regs)
557{
558 if (exception == 3)
559 return instruction_pointer(regs) - 1;
560 return instruction_pointer(regs);
561}
562
563struct kgdb_arch arch_kgdb_ops = {
564 /* Breakpoint instruction: */
565 .gdb_bpt_instr = { 0xcc },
566 .flags = KGDB_HW_BREAKPOINT,
567 .set_hw_breakpoint = kgdb_set_hw_break,
568 .remove_hw_breakpoint = kgdb_remove_hw_break,
569 .remove_all_hw_break = kgdb_remove_all_hw_break,
570 .correct_hw_break = kgdb_correct_hw_break,
571};
diff --git a/arch/x86/kernel/setup64.c b/arch/x86/kernel/setup64.c
index 4be499cd6a0d..9042fb0e36f5 100644
--- a/arch/x86/kernel/setup64.c
+++ b/arch/x86/kernel/setup64.c
@@ -11,6 +11,7 @@
11#include <linux/bootmem.h> 11#include <linux/bootmem.h>
12#include <linux/bitops.h> 12#include <linux/bitops.h>
13#include <linux/module.h> 13#include <linux/module.h>
14#include <linux/kgdb.h>
14#include <asm/pda.h> 15#include <asm/pda.h>
15#include <asm/pgtable.h> 16#include <asm/pgtable.h>
16#include <asm/processor.h> 17#include <asm/processor.h>
@@ -251,6 +252,17 @@ void __cpuinit cpu_init (void)
251 load_TR_desc(); 252 load_TR_desc();
252 load_LDT(&init_mm.context); 253 load_LDT(&init_mm.context);
253 254
255#ifdef CONFIG_KGDB
256 /*
257 * If the kgdb is connected no debug regs should be altered. This
258 * is only applicable when KGDB and a KGDB I/O module are built
259 * into the kernel and you are using early debugging with
260 * kgdbwait. KGDB will control the kernel HW breakpoint registers.
261 */
262 if (kgdb_connected && arch_kgdb_ops.correct_hw_break)
263 arch_kgdb_ops.correct_hw_break();
264 else {
265#endif
254 /* 266 /*
255 * Clear all 6 debug registers: 267 * Clear all 6 debug registers:
256 */ 268 */
@@ -261,6 +273,10 @@ void __cpuinit cpu_init (void)
261 set_debugreg(0UL, 3); 273 set_debugreg(0UL, 3);
262 set_debugreg(0UL, 6); 274 set_debugreg(0UL, 6);
263 set_debugreg(0UL, 7); 275 set_debugreg(0UL, 7);
276#ifdef CONFIG_KGDB
277 /* If the kgdb is connected no debug regs should be altered. */
278 }
279#endif
264 280
265 fpu_init(); 281 fpu_init();
266 282
diff --git a/arch/x86/kernel/traps_32.c b/arch/x86/kernel/traps_32.c
index bb9107c56ff5..65791ca2824a 100644
--- a/arch/x86/kernel/traps_32.c
+++ b/arch/x86/kernel/traps_32.c
@@ -730,6 +730,8 @@ io_check_error(unsigned char reason, struct pt_regs *regs)
730static __kprobes void 730static __kprobes void
731unknown_nmi_error(unsigned char reason, struct pt_regs *regs) 731unknown_nmi_error(unsigned char reason, struct pt_regs *regs)
732{ 732{
733 if (notify_die(DIE_NMIUNKNOWN, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
734 return;
733#ifdef CONFIG_MCA 735#ifdef CONFIG_MCA
734 /* 736 /*
735 * Might actually be able to figure out what the guilty party 737 * Might actually be able to figure out what the guilty party
diff --git a/arch/x86/kernel/traps_64.c b/arch/x86/kernel/traps_64.c
index 33292ac814f4..79aa6fc0815c 100644
--- a/arch/x86/kernel/traps_64.c
+++ b/arch/x86/kernel/traps_64.c
@@ -602,8 +602,13 @@ void die(const char * str, struct pt_regs * regs, long err)
602 602
603void __kprobes die_nmi(char *str, struct pt_regs *regs, int do_panic) 603void __kprobes die_nmi(char *str, struct pt_regs *regs, int do_panic)
604{ 604{
605 unsigned long flags = oops_begin(); 605 unsigned long flags;
606 606
607 if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) ==
608 NOTIFY_STOP)
609 return;
610
611 flags = oops_begin();
607 /* 612 /*
608 * We are in trouble anyway, lets at least try 613 * We are in trouble anyway, lets at least try
609 * to get a message out. 614 * to get a message out.
@@ -808,6 +813,8 @@ io_check_error(unsigned char reason, struct pt_regs * regs)
808static __kprobes void 813static __kprobes void
809unknown_nmi_error(unsigned char reason, struct pt_regs * regs) 814unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
810{ 815{
816 if (notify_die(DIE_NMIUNKNOWN, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
817 return;
811 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n", 818 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
812 reason); 819 reason);
813 printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n"); 820 printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
diff --git a/drivers/char/tty_io.c b/drivers/char/tty_io.c
index 613ec816ce60..4d3c7018f0c3 100644
--- a/drivers/char/tty_io.c
+++ b/drivers/char/tty_io.c
@@ -1155,6 +1155,48 @@ static struct tty_driver *get_tty_driver(dev_t device, int *index)
1155 return NULL; 1155 return NULL;
1156} 1156}
1157 1157
1158#ifdef CONFIG_CONSOLE_POLL
1159
1160/**
1161 * tty_find_polling_driver - find device of a polled tty
1162 * @name: name string to match
1163 * @line: pointer to resulting tty line nr
1164 *
1165 * This routine returns a tty driver structure, given a name
1166 * and the condition that the tty driver is capable of polled
1167 * operation.
1168 */
1169struct tty_driver *tty_find_polling_driver(char *name, int *line)
1170{
1171 struct tty_driver *p, *res = NULL;
1172 int tty_line = 0;
1173 char *str;
1174
1175 mutex_lock(&tty_mutex);
1176 /* Search through the tty devices to look for a match */
1177 list_for_each_entry(p, &tty_drivers, tty_drivers) {
1178 str = name + strlen(p->name);
1179 tty_line = simple_strtoul(str, &str, 10);
1180 if (*str == ',')
1181 str++;
1182 if (*str == '\0')
1183 str = 0;
1184
1185 if (tty_line >= 0 && tty_line <= p->num && p->poll_init &&
1186 !p->poll_init(p, tty_line, str)) {
1187
1188 res = p;
1189 *line = tty_line;
1190 break;
1191 }
1192 }
1193 mutex_unlock(&tty_mutex);
1194
1195 return res;
1196}
1197EXPORT_SYMBOL_GPL(tty_find_polling_driver);
1198#endif
1199
1158/** 1200/**
1159 * tty_check_change - check for POSIX terminal changes 1201 * tty_check_change - check for POSIX terminal changes
1160 * @tty: tty to check 1202 * @tty: tty to check
@@ -3850,6 +3892,11 @@ void tty_set_operations(struct tty_driver *driver,
3850 driver->write_proc = op->write_proc; 3892 driver->write_proc = op->write_proc;
3851 driver->tiocmget = op->tiocmget; 3893 driver->tiocmget = op->tiocmget;
3852 driver->tiocmset = op->tiocmset; 3894 driver->tiocmset = op->tiocmset;
3895#ifdef CONFIG_CONSOLE_POLL
3896 driver->poll_init = op->poll_init;
3897 driver->poll_get_char = op->poll_get_char;
3898 driver->poll_put_char = op->poll_put_char;
3899#endif
3853} 3900}
3854 3901
3855 3902
diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
index 3b12f5da8562..bbc69fdd1b9d 100644
--- a/drivers/misc/Makefile
+++ b/drivers/misc/Makefile
@@ -22,3 +22,4 @@ obj-$(CONFIG_FUJITSU_LAPTOP) += fujitsu-laptop.o
22obj-$(CONFIG_EEPROM_93CX6) += eeprom_93cx6.o 22obj-$(CONFIG_EEPROM_93CX6) += eeprom_93cx6.o
23obj-$(CONFIG_INTEL_MENLOW) += intel_menlow.o 23obj-$(CONFIG_INTEL_MENLOW) += intel_menlow.o
24obj-$(CONFIG_ENCLOSURE_SERVICES) += enclosure.o 24obj-$(CONFIG_ENCLOSURE_SERVICES) += enclosure.o
25obj-$(CONFIG_KGDB_TESTS) += kgdbts.o
diff --git a/drivers/misc/kgdbts.c b/drivers/misc/kgdbts.c
new file mode 100644
index 000000000000..6d6286c4eeac
--- /dev/null
+++ b/drivers/misc/kgdbts.c
@@ -0,0 +1,1090 @@
1/*
2 * kgdbts is a test suite for kgdb for the sole purpose of validating
3 * that key pieces of the kgdb internals are working properly such as
4 * HW/SW breakpoints, single stepping, and NMI.
5 *
6 * Created by: Jason Wessel <jason.wessel@windriver.com>
7 *
8 * Copyright (c) 2008 Wind River Systems, Inc.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 * See the GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23/* Information about the kgdb test suite.
24 * -------------------------------------
25 *
26 * The kgdb test suite is designed as a KGDB I/O module which
27 * simulates the communications that a debugger would have with kgdb.
28 * The tests are broken up in to a line by line and referenced here as
29 * a "get" which is kgdb requesting input and "put" which is kgdb
30 * sending a response.
31 *
32 * The kgdb suite can be invoked from the kernel command line
33 * arguments system or executed dynamically at run time. The test
34 * suite uses the variable "kgdbts" to obtain the information about
35 * which tests to run and to configure the verbosity level. The
36 * following are the various characters you can use with the kgdbts=
37 * line:
38 *
39 * When using the "kgdbts=" you only choose one of the following core
40 * test types:
41 * A = Run all the core tests silently
42 * V1 = Run all the core tests with minimal output
43 * V2 = Run all the core tests in debug mode
44 *
45 * You can also specify optional tests:
46 * N## = Go to sleep with interrupts of for ## seconds
47 * to test the HW NMI watchdog
48 * F## = Break at do_fork for ## iterations
49 * S## = Break at sys_open for ## iterations
50 *
51 * NOTE: that the do_fork and sys_open tests are mutually exclusive.
52 *
53 * To invoke the kgdb test suite from boot you use a kernel start
54 * argument as follows:
55 * kgdbts=V1 kgdbwait
56 * Or if you wanted to perform the NMI test for 6 seconds and do_fork
57 * test for 100 forks, you could use:
58 * kgdbts=V1N6F100 kgdbwait
59 *
60 * The test suite can also be invoked at run time with:
61 * echo kgdbts=V1N6F100 > /sys/module/kgdbts/parameters/kgdbts
62 * Or as another example:
63 * echo kgdbts=V2 > /sys/module/kgdbts/parameters/kgdbts
64 *
65 * When developing a new kgdb arch specific implementation or
66 * using these tests for the purpose of regression testing,
67 * several invocations are required.
68 *
69 * 1) Boot with the test suite enabled by using the kernel arguments
70 * "kgdbts=V1F100 kgdbwait"
71 * ## If kgdb arch specific implementation has NMI use
72 * "kgdbts=V1N6F100
73 *
74 * 2) After the system boot run the basic test.
75 * echo kgdbts=V1 > /sys/module/kgdbts/parameters/kgdbts
76 *
77 * 3) Run the concurrency tests. It is best to use n+1
78 * while loops where n is the number of cpus you have
79 * in your system. The example below uses only two
80 * loops.
81 *
82 * ## This tests break points on sys_open
83 * while [ 1 ] ; do find / > /dev/null 2>&1 ; done &
84 * while [ 1 ] ; do find / > /dev/null 2>&1 ; done &
85 * echo kgdbts=V1S10000 > /sys/module/kgdbts/parameters/kgdbts
86 * fg # and hit control-c
87 * fg # and hit control-c
88 * ## This tests break points on do_fork
89 * while [ 1 ] ; do date > /dev/null ; done &
90 * while [ 1 ] ; do date > /dev/null ; done &
91 * echo kgdbts=V1F1000 > /sys/module/kgdbts/parameters/kgdbts
92 * fg # and hit control-c
93 *
94 */
95
96#include <linux/kernel.h>
97#include <linux/kgdb.h>
98#include <linux/ctype.h>
99#include <linux/uaccess.h>
100#include <linux/syscalls.h>
101#include <linux/nmi.h>
102#include <linux/delay.h>
103#include <linux/kthread.h>
104#include <linux/delay.h>
105
106#define v1printk(a...) do { \
107 if (verbose) \
108 printk(KERN_INFO a); \
109 } while (0)
110#define v2printk(a...) do { \
111 if (verbose > 1) \
112 printk(KERN_INFO a); \
113 touch_nmi_watchdog(); \
114 } while (0)
115#define eprintk(a...) do { \
116 printk(KERN_ERR a); \
117 WARN_ON(1); \
118 } while (0)
119#define MAX_CONFIG_LEN 40
120
121static const char hexchars[] = "0123456789abcdef";
122static struct kgdb_io kgdbts_io_ops;
123static char get_buf[BUFMAX];
124static int get_buf_cnt;
125static char put_buf[BUFMAX];
126static int put_buf_cnt;
127static char scratch_buf[BUFMAX];
128static int verbose;
129static int repeat_test;
130static int test_complete;
131static int send_ack;
132static int final_ack;
133static int hw_break_val;
134static int hw_break_val2;
135#if defined(CONFIG_ARM) || defined(CONFIG_MIPS)
136static int arch_needs_sstep_emulation = 1;
137#else
138static int arch_needs_sstep_emulation;
139#endif
140static unsigned long sstep_addr;
141static int sstep_state;
142
143/* Storage for the registers, in GDB format. */
144static unsigned long kgdbts_gdb_regs[(NUMREGBYTES +
145 sizeof(unsigned long) - 1) /
146 sizeof(unsigned long)];
147static struct pt_regs kgdbts_regs;
148
149/* -1 = init not run yet, 0 = unconfigured, 1 = configured. */
150static int configured = -1;
151
152#ifdef CONFIG_KGDB_TESTS_BOOT_STRING
153static char config[MAX_CONFIG_LEN] = CONFIG_KGDB_TESTS_BOOT_STRING;
154#else
155static char config[MAX_CONFIG_LEN];
156#endif
157static struct kparam_string kps = {
158 .string = config,
159 .maxlen = MAX_CONFIG_LEN,
160};
161
162static void fill_get_buf(char *buf);
163
164struct test_struct {
165 char *get;
166 char *put;
167 void (*get_handler)(char *);
168 int (*put_handler)(char *, char *);
169};
170
171struct test_state {
172 char *name;
173 struct test_struct *tst;
174 int idx;
175 int (*run_test) (int, int);
176 int (*validate_put) (char *);
177};
178
179static struct test_state ts;
180
181static int kgdbts_unreg_thread(void *ptr)
182{
183 /* Wait until the tests are complete and then ungresiter the I/O
184 * driver.
185 */
186 while (!final_ack)
187 msleep_interruptible(1500);
188
189 if (configured)
190 kgdb_unregister_io_module(&kgdbts_io_ops);
191 configured = 0;
192
193 return 0;
194}
195
196/* This is noinline such that it can be used for a single location to
197 * place a breakpoint
198 */
199static noinline void kgdbts_break_test(void)
200{
201 v2printk("kgdbts: breakpoint complete\n");
202}
203
204/* Lookup symbol info in the kernel */
205static unsigned long lookup_addr(char *arg)
206{
207 unsigned long addr = 0;
208
209 if (!strcmp(arg, "kgdbts_break_test"))
210 addr = (unsigned long)kgdbts_break_test;
211 else if (!strcmp(arg, "sys_open"))
212 addr = (unsigned long)sys_open;
213 else if (!strcmp(arg, "do_fork"))
214 addr = (unsigned long)do_fork;
215 else if (!strcmp(arg, "hw_break_val"))
216 addr = (unsigned long)&hw_break_val;
217 return addr;
218}
219
220static void break_helper(char *bp_type, char *arg, unsigned long vaddr)
221{
222 unsigned long addr;
223
224 if (arg)
225 addr = lookup_addr(arg);
226 else
227 addr = vaddr;
228
229 sprintf(scratch_buf, "%s,%lx,%i", bp_type, addr,
230 BREAK_INSTR_SIZE);
231 fill_get_buf(scratch_buf);
232}
233
234static void sw_break(char *arg)
235{
236 break_helper("Z0", arg, 0);
237}
238
239static void sw_rem_break(char *arg)
240{
241 break_helper("z0", arg, 0);
242}
243
244static void hw_break(char *arg)
245{
246 break_helper("Z1", arg, 0);
247}
248
249static void hw_rem_break(char *arg)
250{
251 break_helper("z1", arg, 0);
252}
253
254static void hw_write_break(char *arg)
255{
256 break_helper("Z2", arg, 0);
257}
258
259static void hw_rem_write_break(char *arg)
260{
261 break_helper("z2", arg, 0);
262}
263
264static void hw_access_break(char *arg)
265{
266 break_helper("Z4", arg, 0);
267}
268
269static void hw_rem_access_break(char *arg)
270{
271 break_helper("z4", arg, 0);
272}
273
274static void hw_break_val_access(void)
275{
276 hw_break_val2 = hw_break_val;
277}
278
279static void hw_break_val_write(void)
280{
281 hw_break_val++;
282}
283
284static int check_and_rewind_pc(char *put_str, char *arg)
285{
286 unsigned long addr = lookup_addr(arg);
287 int offset = 0;
288
289 kgdb_hex2mem(&put_str[1], (char *)kgdbts_gdb_regs,
290 NUMREGBYTES);
291 gdb_regs_to_pt_regs(kgdbts_gdb_regs, &kgdbts_regs);
292 v2printk("Stopped at IP: %lx\n", instruction_pointer(&kgdbts_regs));
293#ifdef CONFIG_X86
294 /* On x86 a breakpoint stop requires it to be decremented */
295 if (addr + 1 == kgdbts_regs.ip)
296 offset = -1;
297#endif
298 if (strcmp(arg, "silent") &&
299 instruction_pointer(&kgdbts_regs) + offset != addr) {
300 eprintk("kgdbts: BP mismatch %lx expected %lx\n",
301 instruction_pointer(&kgdbts_regs) + offset, addr);
302 return 1;
303 }
304#ifdef CONFIG_X86
305 /* On x86 adjust the instruction pointer if needed */
306 kgdbts_regs.ip += offset;
307#endif
308 return 0;
309}
310
311static int check_single_step(char *put_str, char *arg)
312{
313 unsigned long addr = lookup_addr(arg);
314 /*
315 * From an arch indepent point of view the instruction pointer
316 * should be on a different instruction
317 */
318 kgdb_hex2mem(&put_str[1], (char *)kgdbts_gdb_regs,
319 NUMREGBYTES);
320 gdb_regs_to_pt_regs(kgdbts_gdb_regs, &kgdbts_regs);
321 v2printk("Singlestep stopped at IP: %lx\n",
322 instruction_pointer(&kgdbts_regs));
323 if (instruction_pointer(&kgdbts_regs) == addr) {
324 eprintk("kgdbts: SingleStep failed at %lx\n",
325 instruction_pointer(&kgdbts_regs));
326 return 1;
327 }
328
329 return 0;
330}
331
332static void write_regs(char *arg)
333{
334 memset(scratch_buf, 0, sizeof(scratch_buf));
335 scratch_buf[0] = 'G';
336 pt_regs_to_gdb_regs(kgdbts_gdb_regs, &kgdbts_regs);
337 kgdb_mem2hex((char *)kgdbts_gdb_regs, &scratch_buf[1], NUMREGBYTES);
338 fill_get_buf(scratch_buf);
339}
340
341static void skip_back_repeat_test(char *arg)
342{
343 int go_back = simple_strtol(arg, NULL, 10);
344
345 repeat_test--;
346 if (repeat_test <= 0)
347 ts.idx++;
348 else
349 ts.idx -= go_back;
350 fill_get_buf(ts.tst[ts.idx].get);
351}
352
353static int got_break(char *put_str, char *arg)
354{
355 test_complete = 1;
356 if (!strncmp(put_str+1, arg, 2)) {
357 if (!strncmp(arg, "T0", 2))
358 test_complete = 2;
359 return 0;
360 }
361 return 1;
362}
363
364static void emul_sstep_get(char *arg)
365{
366 if (!arch_needs_sstep_emulation) {
367 fill_get_buf(arg);
368 return;
369 }
370 switch (sstep_state) {
371 case 0:
372 v2printk("Emulate single step\n");
373 /* Start by looking at the current PC */
374 fill_get_buf("g");
375 break;
376 case 1:
377 /* set breakpoint */
378 break_helper("Z0", 0, sstep_addr);
379 break;
380 case 2:
381 /* Continue */
382 fill_get_buf("c");
383 break;
384 case 3:
385 /* Clear breakpoint */
386 break_helper("z0", 0, sstep_addr);
387 break;
388 default:
389 eprintk("kgdbts: ERROR failed sstep get emulation\n");
390 }
391 sstep_state++;
392}
393
394static int emul_sstep_put(char *put_str, char *arg)
395{
396 if (!arch_needs_sstep_emulation) {
397 if (!strncmp(put_str+1, arg, 2))
398 return 0;
399 return 1;
400 }
401 switch (sstep_state) {
402 case 1:
403 /* validate the "g" packet to get the IP */
404 kgdb_hex2mem(&put_str[1], (char *)kgdbts_gdb_regs,
405 NUMREGBYTES);
406 gdb_regs_to_pt_regs(kgdbts_gdb_regs, &kgdbts_regs);
407 v2printk("Stopped at IP: %lx\n",
408 instruction_pointer(&kgdbts_regs));
409 /* Want to stop at IP + break instruction size by default */
410 sstep_addr = instruction_pointer(&kgdbts_regs) +
411 BREAK_INSTR_SIZE;
412 break;
413 case 2:
414 if (strncmp(put_str, "$OK", 3)) {
415 eprintk("kgdbts: failed sstep break set\n");
416 return 1;
417 }
418 break;
419 case 3:
420 if (strncmp(put_str, "$T0", 3)) {
421 eprintk("kgdbts: failed continue sstep\n");
422 return 1;
423 }
424 break;
425 case 4:
426 if (strncmp(put_str, "$OK", 3)) {
427 eprintk("kgdbts: failed sstep break unset\n");
428 return 1;
429 }
430 /* Single step is complete so continue on! */
431 sstep_state = 0;
432 return 0;
433 default:
434 eprintk("kgdbts: ERROR failed sstep put emulation\n");
435 }
436
437 /* Continue on the same test line until emulation is complete */
438 ts.idx--;
439 return 0;
440}
441
442static int final_ack_set(char *put_str, char *arg)
443{
444 if (strncmp(put_str+1, arg, 2))
445 return 1;
446 final_ack = 1;
447 return 0;
448}
449/*
450 * Test to plant a breakpoint and detach, which should clear out the
451 * breakpoint and restore the original instruction.
452 */
453static struct test_struct plant_and_detach_test[] = {
454 { "?", "S0*" }, /* Clear break points */
455 { "kgdbts_break_test", "OK", sw_break, }, /* set sw breakpoint */
456 { "D", "OK" }, /* Detach */
457 { "", "" },
458};
459
460/*
461 * Simple test to write in a software breakpoint, check for the
462 * correct stop location and detach.
463 */
464static struct test_struct sw_breakpoint_test[] = {
465 { "?", "S0*" }, /* Clear break points */
466 { "kgdbts_break_test", "OK", sw_break, }, /* set sw breakpoint */
467 { "c", "T0*", }, /* Continue */
468 { "g", "kgdbts_break_test", 0, check_and_rewind_pc },
469 { "write", "OK", write_regs },
470 { "kgdbts_break_test", "OK", sw_rem_break }, /*remove breakpoint */
471 { "D", "OK" }, /* Detach */
472 { "D", "OK", 0, got_break }, /* If the test worked we made it here */
473 { "", "" },
474};
475
476/*
477 * Test a known bad memory read location to test the fault handler and
478 * read bytes 1-8 at the bad address
479 */
480static struct test_struct bad_read_test[] = {
481 { "?", "S0*" }, /* Clear break points */
482 { "m0,1", "E*" }, /* read 1 byte at address 1 */
483 { "m0,2", "E*" }, /* read 1 byte at address 2 */
484 { "m0,3", "E*" }, /* read 1 byte at address 3 */
485 { "m0,4", "E*" }, /* read 1 byte at address 4 */
486 { "m0,5", "E*" }, /* read 1 byte at address 5 */
487 { "m0,6", "E*" }, /* read 1 byte at address 6 */
488 { "m0,7", "E*" }, /* read 1 byte at address 7 */
489 { "m0,8", "E*" }, /* read 1 byte at address 8 */
490 { "D", "OK" }, /* Detach which removes all breakpoints and continues */
491 { "", "" },
492};
493
494/*
495 * Test for hitting a breakpoint, remove it, single step, plant it
496 * again and detach.
497 */
498static struct test_struct singlestep_break_test[] = {
499 { "?", "S0*" }, /* Clear break points */
500 { "kgdbts_break_test", "OK", sw_break, }, /* set sw breakpoint */
501 { "c", "T0*", }, /* Continue */
502 { "g", "kgdbts_break_test", 0, check_and_rewind_pc },
503 { "write", "OK", write_regs }, /* Write registers */
504 { "kgdbts_break_test", "OK", sw_rem_break }, /*remove breakpoint */
505 { "s", "T0*", emul_sstep_get, emul_sstep_put }, /* Single step */
506 { "g", "kgdbts_break_test", 0, check_single_step },
507 { "kgdbts_break_test", "OK", sw_break, }, /* set sw breakpoint */
508 { "c", "T0*", }, /* Continue */
509 { "g", "kgdbts_break_test", 0, check_and_rewind_pc },
510 { "write", "OK", write_regs }, /* Write registers */
511 { "D", "OK" }, /* Remove all breakpoints and continues */
512 { "", "" },
513};
514
515/*
516 * Test for hitting a breakpoint at do_fork for what ever the number
517 * of iterations required by the variable repeat_test.
518 */
519static struct test_struct do_fork_test[] = {
520 { "?", "S0*" }, /* Clear break points */
521 { "do_fork", "OK", sw_break, }, /* set sw breakpoint */
522 { "c", "T0*", }, /* Continue */
523 { "g", "do_fork", 0, check_and_rewind_pc }, /* check location */
524 { "write", "OK", write_regs }, /* Write registers */
525 { "do_fork", "OK", sw_rem_break }, /*remove breakpoint */
526 { "s", "T0*", emul_sstep_get, emul_sstep_put }, /* Single step */
527 { "g", "do_fork", 0, check_single_step },
528 { "do_fork", "OK", sw_break, }, /* set sw breakpoint */
529 { "7", "T0*", skip_back_repeat_test }, /* Loop based on repeat_test */
530 { "D", "OK", 0, final_ack_set }, /* detach and unregister I/O */
531 { "", "" },
532};
533
534/* Test for hitting a breakpoint at sys_open for what ever the number
535 * of iterations required by the variable repeat_test.
536 */
537static struct test_struct sys_open_test[] = {
538 { "?", "S0*" }, /* Clear break points */
539 { "sys_open", "OK", sw_break, }, /* set sw breakpoint */
540 { "c", "T0*", }, /* Continue */
541 { "g", "sys_open", 0, check_and_rewind_pc }, /* check location */
542 { "write", "OK", write_regs }, /* Write registers */
543 { "sys_open", "OK", sw_rem_break }, /*remove breakpoint */
544 { "s", "T0*", emul_sstep_get, emul_sstep_put }, /* Single step */
545 { "g", "sys_open", 0, check_single_step },
546 { "sys_open", "OK", sw_break, }, /* set sw breakpoint */
547 { "7", "T0*", skip_back_repeat_test }, /* Loop based on repeat_test */
548 { "D", "OK", 0, final_ack_set }, /* detach and unregister I/O */
549 { "", "" },
550};
551
552/*
553 * Test for hitting a simple hw breakpoint
554 */
555static struct test_struct hw_breakpoint_test[] = {
556 { "?", "S0*" }, /* Clear break points */
557 { "kgdbts_break_test", "OK", hw_break, }, /* set hw breakpoint */
558 { "c", "T0*", }, /* Continue */
559 { "g", "kgdbts_break_test", 0, check_and_rewind_pc },
560 { "write", "OK", write_regs },
561 { "kgdbts_break_test", "OK", hw_rem_break }, /*remove breakpoint */
562 { "D", "OK" }, /* Detach */
563 { "D", "OK", 0, got_break }, /* If the test worked we made it here */
564 { "", "" },
565};
566
567/*
568 * Test for hitting a hw write breakpoint
569 */
570static struct test_struct hw_write_break_test[] = {
571 { "?", "S0*" }, /* Clear break points */
572 { "hw_break_val", "OK", hw_write_break, }, /* set hw breakpoint */
573 { "c", "T0*", 0, got_break }, /* Continue */
574 { "g", "silent", 0, check_and_rewind_pc },
575 { "write", "OK", write_regs },
576 { "hw_break_val", "OK", hw_rem_write_break }, /*remove breakpoint */
577 { "D", "OK" }, /* Detach */
578 { "D", "OK", 0, got_break }, /* If the test worked we made it here */
579 { "", "" },
580};
581
582/*
583 * Test for hitting a hw access breakpoint
584 */
585static struct test_struct hw_access_break_test[] = {
586 { "?", "S0*" }, /* Clear break points */
587 { "hw_break_val", "OK", hw_access_break, }, /* set hw breakpoint */
588 { "c", "T0*", 0, got_break }, /* Continue */
589 { "g", "silent", 0, check_and_rewind_pc },
590 { "write", "OK", write_regs },
591 { "hw_break_val", "OK", hw_rem_access_break }, /*remove breakpoint */
592 { "D", "OK" }, /* Detach */
593 { "D", "OK", 0, got_break }, /* If the test worked we made it here */
594 { "", "" },
595};
596
597/*
598 * Test for hitting a hw access breakpoint
599 */
600static struct test_struct nmi_sleep_test[] = {
601 { "?", "S0*" }, /* Clear break points */
602 { "c", "T0*", 0, got_break }, /* Continue */
603 { "D", "OK" }, /* Detach */
604 { "D", "OK", 0, got_break }, /* If the test worked we made it here */
605 { "", "" },
606};
607
608static void fill_get_buf(char *buf)
609{
610 unsigned char checksum = 0;
611 int count = 0;
612 char ch;
613
614 strcpy(get_buf, "$");
615 strcat(get_buf, buf);
616 while ((ch = buf[count])) {
617 checksum += ch;
618 count++;
619 }
620 strcat(get_buf, "#");
621 get_buf[count + 2] = hexchars[checksum >> 4];
622 get_buf[count + 3] = hexchars[checksum & 0xf];
623 get_buf[count + 4] = '\0';
624 v2printk("get%i: %s\n", ts.idx, get_buf);
625}
626
627static int validate_simple_test(char *put_str)
628{
629 char *chk_str;
630
631 if (ts.tst[ts.idx].put_handler)
632 return ts.tst[ts.idx].put_handler(put_str,
633 ts.tst[ts.idx].put);
634
635 chk_str = ts.tst[ts.idx].put;
636 if (*put_str == '$')
637 put_str++;
638
639 while (*chk_str != '\0' && *put_str != '\0') {
640 /* If someone does a * to match the rest of the string, allow
641 * it, or stop if the recieved string is complete.
642 */
643 if (*put_str == '#' || *chk_str == '*')
644 return 0;
645 if (*put_str != *chk_str)
646 return 1;
647
648 chk_str++;
649 put_str++;
650 }
651 if (*chk_str == '\0' && (*put_str == '\0' || *put_str == '#'))
652 return 0;
653
654 return 1;
655}
656
657static int run_simple_test(int is_get_char, int chr)
658{
659 int ret = 0;
660 if (is_get_char) {
661 /* Send an ACK on the get if a prior put completed and set the
662 * send ack variable
663 */
664 if (send_ack) {
665 send_ack = 0;
666 return '+';
667 }
668 /* On the first get char, fill the transmit buffer and then
669 * take from the get_string.
670 */
671 if (get_buf_cnt == 0) {
672 if (ts.tst[ts.idx].get_handler)
673 ts.tst[ts.idx].get_handler(ts.tst[ts.idx].get);
674 else
675 fill_get_buf(ts.tst[ts.idx].get);
676 }
677
678 if (get_buf[get_buf_cnt] == '\0') {
679 eprintk("kgdbts: ERROR GET: EOB on '%s' at %i\n",
680 ts.name, ts.idx);
681 get_buf_cnt = 0;
682 fill_get_buf("D");
683 }
684 ret = get_buf[get_buf_cnt];
685 get_buf_cnt++;
686 return ret;
687 }
688
689 /* This callback is a put char which is when kgdb sends data to
690 * this I/O module.
691 */
692 if (ts.tst[ts.idx].get[0] == '\0' &&
693 ts.tst[ts.idx].put[0] == '\0') {
694 eprintk("kgdbts: ERROR: beyond end of test on"
695 " '%s' line %i\n", ts.name, ts.idx);
696 return 0;
697 }
698
699 if (put_buf_cnt >= BUFMAX) {
700 eprintk("kgdbts: ERROR: put buffer overflow on"
701 " '%s' line %i\n", ts.name, ts.idx);
702 put_buf_cnt = 0;
703 return 0;
704 }
705 /* Ignore everything until the first valid packet start '$' */
706 if (put_buf_cnt == 0 && chr != '$')
707 return 0;
708
709 put_buf[put_buf_cnt] = chr;
710 put_buf_cnt++;
711
712 /* End of packet == #XX so look for the '#' */
713 if (put_buf_cnt > 3 && put_buf[put_buf_cnt - 3] == '#') {
714 put_buf[put_buf_cnt] = '\0';
715 v2printk("put%i: %s\n", ts.idx, put_buf);
716 /* Trigger check here */
717 if (ts.validate_put && ts.validate_put(put_buf)) {
718 eprintk("kgdbts: ERROR PUT: end of test "
719 "buffer on '%s' line %i expected %s got %s\n",
720 ts.name, ts.idx, ts.tst[ts.idx].put, put_buf);
721 }
722 ts.idx++;
723 put_buf_cnt = 0;
724 get_buf_cnt = 0;
725 send_ack = 1;
726 }
727 return 0;
728}
729
730static void init_simple_test(void)
731{
732 memset(&ts, 0, sizeof(ts));
733 ts.run_test = run_simple_test;
734 ts.validate_put = validate_simple_test;
735}
736
737static void run_plant_and_detach_test(int is_early)
738{
739 char before[BREAK_INSTR_SIZE];
740 char after[BREAK_INSTR_SIZE];
741
742 probe_kernel_read(before, (char *)kgdbts_break_test,
743 BREAK_INSTR_SIZE);
744 init_simple_test();
745 ts.tst = plant_and_detach_test;
746 ts.name = "plant_and_detach_test";
747 /* Activate test with initial breakpoint */
748 if (!is_early)
749 kgdb_breakpoint();
750 probe_kernel_read(after, (char *)kgdbts_break_test,
751 BREAK_INSTR_SIZE);
752 if (memcmp(before, after, BREAK_INSTR_SIZE)) {
753 printk(KERN_CRIT "kgdbts: ERROR kgdb corrupted memory\n");
754 panic("kgdb memory corruption");
755 }
756
757 /* complete the detach test */
758 if (!is_early)
759 kgdbts_break_test();
760}
761
762static void run_breakpoint_test(int is_hw_breakpoint)
763{
764 test_complete = 0;
765 init_simple_test();
766 if (is_hw_breakpoint) {
767 ts.tst = hw_breakpoint_test;
768 ts.name = "hw_breakpoint_test";
769 } else {
770 ts.tst = sw_breakpoint_test;
771 ts.name = "sw_breakpoint_test";
772 }
773 /* Activate test with initial breakpoint */
774 kgdb_breakpoint();
775 /* run code with the break point in it */
776 kgdbts_break_test();
777 kgdb_breakpoint();
778
779 if (test_complete)
780 return;
781
782 eprintk("kgdbts: ERROR %s test failed\n", ts.name);
783}
784
785static void run_hw_break_test(int is_write_test)
786{
787 test_complete = 0;
788 init_simple_test();
789 if (is_write_test) {
790 ts.tst = hw_write_break_test;
791 ts.name = "hw_write_break_test";
792 } else {
793 ts.tst = hw_access_break_test;
794 ts.name = "hw_access_break_test";
795 }
796 /* Activate test with initial breakpoint */
797 kgdb_breakpoint();
798 hw_break_val_access();
799 if (is_write_test) {
800 if (test_complete == 2)
801 eprintk("kgdbts: ERROR %s broke on access\n",
802 ts.name);
803 hw_break_val_write();
804 }
805 kgdb_breakpoint();
806
807 if (test_complete == 1)
808 return;
809
810 eprintk("kgdbts: ERROR %s test failed\n", ts.name);
811}
812
813static void run_nmi_sleep_test(int nmi_sleep)
814{
815 unsigned long flags;
816
817 init_simple_test();
818 ts.tst = nmi_sleep_test;
819 ts.name = "nmi_sleep_test";
820 /* Activate test with initial breakpoint */
821 kgdb_breakpoint();
822 local_irq_save(flags);
823 mdelay(nmi_sleep*1000);
824 touch_nmi_watchdog();
825 local_irq_restore(flags);
826 if (test_complete != 2)
827 eprintk("kgdbts: ERROR nmi_test did not hit nmi\n");
828 kgdb_breakpoint();
829 if (test_complete == 1)
830 return;
831
832 eprintk("kgdbts: ERROR %s test failed\n", ts.name);
833}
834
835static void run_bad_read_test(void)
836{
837 init_simple_test();
838 ts.tst = bad_read_test;
839 ts.name = "bad_read_test";
840 /* Activate test with initial breakpoint */
841 kgdb_breakpoint();
842}
843
844static void run_do_fork_test(void)
845{
846 init_simple_test();
847 ts.tst = do_fork_test;
848 ts.name = "do_fork_test";
849 /* Activate test with initial breakpoint */
850 kgdb_breakpoint();
851}
852
853static void run_sys_open_test(void)
854{
855 init_simple_test();
856 ts.tst = sys_open_test;
857 ts.name = "sys_open_test";
858 /* Activate test with initial breakpoint */
859 kgdb_breakpoint();
860}
861
862static void run_singlestep_break_test(void)
863{
864 init_simple_test();
865 ts.tst = singlestep_break_test;
866 ts.name = "singlestep_breakpoint_test";
867 /* Activate test with initial breakpoint */
868 kgdb_breakpoint();
869 kgdbts_break_test();
870 kgdbts_break_test();
871}
872
873static void kgdbts_run_tests(void)
874{
875 char *ptr;
876 int fork_test = 0;
877 int sys_open_test = 0;
878 int nmi_sleep = 0;
879
880 ptr = strstr(config, "F");
881 if (ptr)
882 fork_test = simple_strtol(ptr+1, NULL, 10);
883 ptr = strstr(config, "S");
884 if (ptr)
885 sys_open_test = simple_strtol(ptr+1, NULL, 10);
886 ptr = strstr(config, "N");
887 if (ptr)
888 nmi_sleep = simple_strtol(ptr+1, NULL, 10);
889
890 /* required internal KGDB tests */
891 v1printk("kgdbts:RUN plant and detach test\n");
892 run_plant_and_detach_test(0);
893 v1printk("kgdbts:RUN sw breakpoint test\n");
894 run_breakpoint_test(0);
895 v1printk("kgdbts:RUN bad memory access test\n");
896 run_bad_read_test();
897 v1printk("kgdbts:RUN singlestep breakpoint test\n");
898 run_singlestep_break_test();
899
900 /* ===Optional tests=== */
901
902 /* All HW break point tests */
903 if (arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT) {
904 v1printk("kgdbts:RUN hw breakpoint test\n");
905 run_breakpoint_test(1);
906 v1printk("kgdbts:RUN hw write breakpoint test\n");
907 run_hw_break_test(1);
908 v1printk("kgdbts:RUN access write breakpoint test\n");
909 run_hw_break_test(0);
910 }
911
912 if (nmi_sleep) {
913 v1printk("kgdbts:RUN NMI sleep %i seconds test\n", nmi_sleep);
914 run_nmi_sleep_test(nmi_sleep);
915 }
916
917 /* If the do_fork test is run it will be the last test that is
918 * executed because a kernel thread will be spawned at the very
919 * end to unregister the debug hooks.
920 */
921 if (fork_test) {
922 repeat_test = fork_test;
923 printk(KERN_INFO "kgdbts:RUN do_fork for %i breakpoints\n",
924 repeat_test);
925 kthread_run(kgdbts_unreg_thread, 0, "kgdbts_unreg");
926 run_do_fork_test();
927 return;
928 }
929
930 /* If the sys_open test is run it will be the last test that is
931 * executed because a kernel thread will be spawned at the very
932 * end to unregister the debug hooks.
933 */
934 if (sys_open_test) {
935 repeat_test = sys_open_test;
936 printk(KERN_INFO "kgdbts:RUN sys_open for %i breakpoints\n",
937 repeat_test);
938 kthread_run(kgdbts_unreg_thread, 0, "kgdbts_unreg");
939 run_sys_open_test();
940 return;
941 }
942 /* Shutdown and unregister */
943 kgdb_unregister_io_module(&kgdbts_io_ops);
944 configured = 0;
945}
946
947static int kgdbts_option_setup(char *opt)
948{
949 if (strlen(opt) > MAX_CONFIG_LEN) {
950 printk(KERN_ERR "kgdbts: config string too long\n");
951 return -ENOSPC;
952 }
953 strcpy(config, opt);
954
955 verbose = 0;
956 if (strstr(config, "V1"))
957 verbose = 1;
958 if (strstr(config, "V2"))
959 verbose = 2;
960
961 return 0;
962}
963
964__setup("kgdbts=", kgdbts_option_setup);
965
966static int configure_kgdbts(void)
967{
968 int err = 0;
969
970 if (!strlen(config) || isspace(config[0]))
971 goto noconfig;
972 err = kgdbts_option_setup(config);
973 if (err)
974 goto noconfig;
975
976 final_ack = 0;
977 run_plant_and_detach_test(1);
978
979 err = kgdb_register_io_module(&kgdbts_io_ops);
980 if (err) {
981 configured = 0;
982 return err;
983 }
984 configured = 1;
985 kgdbts_run_tests();
986
987 return err;
988
989noconfig:
990 config[0] = 0;
991 configured = 0;
992
993 return err;
994}
995
996static int __init init_kgdbts(void)
997{
998 /* Already configured? */
999 if (configured == 1)
1000 return 0;
1001
1002 return configure_kgdbts();
1003}
1004
1005static void cleanup_kgdbts(void)
1006{
1007 if (configured == 1)
1008 kgdb_unregister_io_module(&kgdbts_io_ops);
1009}
1010
1011static int kgdbts_get_char(void)
1012{
1013 int val = 0;
1014
1015 if (ts.run_test)
1016 val = ts.run_test(1, 0);
1017
1018 return val;
1019}
1020
1021static void kgdbts_put_char(u8 chr)
1022{
1023 if (ts.run_test)
1024 ts.run_test(0, chr);
1025}
1026
1027static int param_set_kgdbts_var(const char *kmessage, struct kernel_param *kp)
1028{
1029 int len = strlen(kmessage);
1030
1031 if (len >= MAX_CONFIG_LEN) {
1032 printk(KERN_ERR "kgdbts: config string too long\n");
1033 return -ENOSPC;
1034 }
1035
1036 /* Only copy in the string if the init function has not run yet */
1037 if (configured < 0) {
1038 strcpy(config, kmessage);
1039 return 0;
1040 }
1041
1042 if (kgdb_connected) {
1043 printk(KERN_ERR
1044 "kgdbts: Cannot reconfigure while KGDB is connected.\n");
1045
1046 return -EBUSY;
1047 }
1048
1049 strcpy(config, kmessage);
1050 /* Chop out \n char as a result of echo */
1051 if (config[len - 1] == '\n')
1052 config[len - 1] = '\0';
1053
1054 if (configured == 1)
1055 cleanup_kgdbts();
1056
1057 /* Go and configure with the new params. */
1058 return configure_kgdbts();
1059}
1060
1061static void kgdbts_pre_exp_handler(void)
1062{
1063 /* Increment the module count when the debugger is active */
1064 if (!kgdb_connected)
1065 try_module_get(THIS_MODULE);
1066}
1067
1068static void kgdbts_post_exp_handler(void)
1069{
1070 /* decrement the module count when the debugger detaches */
1071 if (!kgdb_connected)
1072 module_put(THIS_MODULE);
1073}
1074
1075static struct kgdb_io kgdbts_io_ops = {
1076 .name = "kgdbts",
1077 .read_char = kgdbts_get_char,
1078 .write_char = kgdbts_put_char,
1079 .pre_exception = kgdbts_pre_exp_handler,
1080 .post_exception = kgdbts_post_exp_handler,
1081};
1082
1083module_init(init_kgdbts);
1084module_exit(cleanup_kgdbts);
1085module_param_call(kgdbts, param_set_kgdbts_var, param_get_string, &kps, 0644);
1086MODULE_PARM_DESC(kgdbts, "<A|V1|V2>[F#|S#][N#]");
1087MODULE_DESCRIPTION("KGDB Test Suite");
1088MODULE_LICENSE("GPL");
1089MODULE_AUTHOR("Wind River Systems, Inc.");
1090
diff --git a/drivers/serial/8250.c b/drivers/serial/8250.c
index 77f7a7f0646e..96a585e1cee8 100644
--- a/drivers/serial/8250.c
+++ b/drivers/serial/8250.c
@@ -1740,6 +1740,60 @@ static inline void wait_for_xmitr(struct uart_8250_port *up, int bits)
1740 } 1740 }
1741} 1741}
1742 1742
1743#ifdef CONFIG_CONSOLE_POLL
1744/*
1745 * Console polling routines for writing and reading from the uart while
1746 * in an interrupt or debug context.
1747 */
1748
1749static int serial8250_get_poll_char(struct uart_port *port)
1750{
1751 struct uart_8250_port *up = (struct uart_8250_port *)port;
1752 unsigned char lsr = serial_inp(up, UART_LSR);
1753
1754 while (!(lsr & UART_LSR_DR))
1755 lsr = serial_inp(up, UART_LSR);
1756
1757 return serial_inp(up, UART_RX);
1758}
1759
1760
1761static void serial8250_put_poll_char(struct uart_port *port,
1762 unsigned char c)
1763{
1764 unsigned int ier;
1765 struct uart_8250_port *up = (struct uart_8250_port *)port;
1766
1767 /*
1768 * First save the IER then disable the interrupts
1769 */
1770 ier = serial_in(up, UART_IER);
1771 if (up->capabilities & UART_CAP_UUE)
1772 serial_out(up, UART_IER, UART_IER_UUE);
1773 else
1774 serial_out(up, UART_IER, 0);
1775
1776 wait_for_xmitr(up, BOTH_EMPTY);
1777 /*
1778 * Send the character out.
1779 * If a LF, also do CR...
1780 */
1781 serial_out(up, UART_TX, c);
1782 if (c == 10) {
1783 wait_for_xmitr(up, BOTH_EMPTY);
1784 serial_out(up, UART_TX, 13);
1785 }
1786
1787 /*
1788 * Finally, wait for transmitter to become empty
1789 * and restore the IER
1790 */
1791 wait_for_xmitr(up, BOTH_EMPTY);
1792 serial_out(up, UART_IER, ier);
1793}
1794
1795#endif /* CONFIG_CONSOLE_POLL */
1796
1743static int serial8250_startup(struct uart_port *port) 1797static int serial8250_startup(struct uart_port *port)
1744{ 1798{
1745 struct uart_8250_port *up = (struct uart_8250_port *)port; 1799 struct uart_8250_port *up = (struct uart_8250_port *)port;
@@ -2386,6 +2440,10 @@ static struct uart_ops serial8250_pops = {
2386 .request_port = serial8250_request_port, 2440 .request_port = serial8250_request_port,
2387 .config_port = serial8250_config_port, 2441 .config_port = serial8250_config_port,
2388 .verify_port = serial8250_verify_port, 2442 .verify_port = serial8250_verify_port,
2443#ifdef CONFIG_CONSOLE_POLL
2444 .poll_get_char = serial8250_get_poll_char,
2445 .poll_put_char = serial8250_put_poll_char,
2446#endif
2389}; 2447};
2390 2448
2391static struct uart_8250_port serial8250_ports[UART_NR]; 2449static struct uart_8250_port serial8250_ports[UART_NR];
diff --git a/drivers/serial/Kconfig b/drivers/serial/Kconfig
index cf627cd1b4c8..f7cd9504d811 100644
--- a/drivers/serial/Kconfig
+++ b/drivers/serial/Kconfig
@@ -961,6 +961,9 @@ config SERIAL_CORE
961config SERIAL_CORE_CONSOLE 961config SERIAL_CORE_CONSOLE
962 bool 962 bool
963 963
964config CONSOLE_POLL
965 bool
966
964config SERIAL_68328 967config SERIAL_68328
965 bool "68328 serial support" 968 bool "68328 serial support"
966 depends on M68328 || M68EZ328 || M68VZ328 969 depends on M68328 || M68EZ328 || M68VZ328
diff --git a/drivers/serial/Makefile b/drivers/serial/Makefile
index 640cfe44a56d..3cbea5494724 100644
--- a/drivers/serial/Makefile
+++ b/drivers/serial/Makefile
@@ -66,4 +66,5 @@ obj-$(CONFIG_SERIAL_UARTLITE) += uartlite.o
66obj-$(CONFIG_SERIAL_NETX) += netx-serial.o 66obj-$(CONFIG_SERIAL_NETX) += netx-serial.o
67obj-$(CONFIG_SERIAL_OF_PLATFORM) += of_serial.o 67obj-$(CONFIG_SERIAL_OF_PLATFORM) += of_serial.o
68obj-$(CONFIG_SERIAL_KS8695) += serial_ks8695.o 68obj-$(CONFIG_SERIAL_KS8695) += serial_ks8695.o
69obj-$(CONFIG_KGDB_SERIAL_CONSOLE) += kgdboc.o
69obj-$(CONFIG_SERIAL_QE) += ucc_uart.o 70obj-$(CONFIG_SERIAL_QE) += ucc_uart.o
diff --git a/drivers/serial/amba-pl011.c b/drivers/serial/amba-pl011.c
index 40604a092921..08adc1de4a79 100644
--- a/drivers/serial/amba-pl011.c
+++ b/drivers/serial/amba-pl011.c
@@ -314,6 +314,32 @@ static void pl011_break_ctl(struct uart_port *port, int break_state)
314 spin_unlock_irqrestore(&uap->port.lock, flags); 314 spin_unlock_irqrestore(&uap->port.lock, flags);
315} 315}
316 316
317#ifdef CONFIG_CONSOLE_POLL
318static int pl010_get_poll_char(struct uart_port *port)
319{
320 struct uart_amba_port *uap = (struct uart_amba_port *)port;
321 unsigned int status;
322
323 do {
324 status = readw(uap->port.membase + UART01x_FR);
325 } while (status & UART01x_FR_RXFE);
326
327 return readw(uap->port.membase + UART01x_DR);
328}
329
330static void pl010_put_poll_char(struct uart_port *port,
331 unsigned char ch)
332{
333 struct uart_amba_port *uap = (struct uart_amba_port *)port;
334
335 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
336 barrier();
337
338 writew(ch, uap->port.membase + UART01x_DR);
339}
340
341#endif /* CONFIG_CONSOLE_POLL */
342
317static int pl011_startup(struct uart_port *port) 343static int pl011_startup(struct uart_port *port)
318{ 344{
319 struct uart_amba_port *uap = (struct uart_amba_port *)port; 345 struct uart_amba_port *uap = (struct uart_amba_port *)port;
@@ -572,6 +598,10 @@ static struct uart_ops amba_pl011_pops = {
572 .request_port = pl010_request_port, 598 .request_port = pl010_request_port,
573 .config_port = pl010_config_port, 599 .config_port = pl010_config_port,
574 .verify_port = pl010_verify_port, 600 .verify_port = pl010_verify_port,
601#ifdef CONFIG_CONSOLE_POLL
602 .poll_get_char = pl010_get_poll_char,
603 .poll_put_char = pl010_put_poll_char,
604#endif
575}; 605};
576 606
577static struct uart_amba_port *amba_ports[UART_NR]; 607static struct uart_amba_port *amba_ports[UART_NR];
diff --git a/drivers/serial/kgdboc.c b/drivers/serial/kgdboc.c
new file mode 100644
index 000000000000..9cf03327386a
--- /dev/null
+++ b/drivers/serial/kgdboc.c
@@ -0,0 +1,168 @@
1/*
2 * Based on the same principle as kgdboe using the NETPOLL api, this
3 * driver uses a console polling api to implement a gdb serial inteface
4 * which is multiplexed on a console port.
5 *
6 * Maintainer: Jason Wessel <jason.wessel@windriver.com>
7 *
8 * 2007-2008 (c) Jason Wessel - Wind River Systems, Inc.
9 *
10 * This file is licensed under the terms of the GNU General Public
11 * License version 2. This program is licensed "as is" without any
12 * warranty of any kind, whether express or implied.
13 */
14#include <linux/kernel.h>
15#include <linux/ctype.h>
16#include <linux/kgdb.h>
17#include <linux/tty.h>
18
19#define MAX_CONFIG_LEN 40
20
21static struct kgdb_io kgdboc_io_ops;
22
23/* -1 = init not run yet, 0 = unconfigured, 1 = configured. */
24static int configured = -1;
25
26static char config[MAX_CONFIG_LEN];
27static struct kparam_string kps = {
28 .string = config,
29 .maxlen = MAX_CONFIG_LEN,
30};
31
32static struct tty_driver *kgdb_tty_driver;
33static int kgdb_tty_line;
34
35static int kgdboc_option_setup(char *opt)
36{
37 if (strlen(opt) > MAX_CONFIG_LEN) {
38 printk(KERN_ERR "kgdboc: config string too long\n");
39 return -ENOSPC;
40 }
41 strcpy(config, opt);
42
43 return 0;
44}
45
46__setup("kgdboc=", kgdboc_option_setup);
47
48static int configure_kgdboc(void)
49{
50 struct tty_driver *p;
51 int tty_line = 0;
52 int err;
53
54 err = kgdboc_option_setup(config);
55 if (err || !strlen(config) || isspace(config[0]))
56 goto noconfig;
57
58 err = -ENODEV;
59
60 p = tty_find_polling_driver(config, &tty_line);
61 if (!p)
62 goto noconfig;
63
64 kgdb_tty_driver = p;
65 kgdb_tty_line = tty_line;
66
67 err = kgdb_register_io_module(&kgdboc_io_ops);
68 if (err)
69 goto noconfig;
70
71 configured = 1;
72
73 return 0;
74
75noconfig:
76 config[0] = 0;
77 configured = 0;
78
79 return err;
80}
81
82static int __init init_kgdboc(void)
83{
84 /* Already configured? */
85 if (configured == 1)
86 return 0;
87
88 return configure_kgdboc();
89}
90
91static void cleanup_kgdboc(void)
92{
93 if (configured == 1)
94 kgdb_unregister_io_module(&kgdboc_io_ops);
95}
96
97static int kgdboc_get_char(void)
98{
99 return kgdb_tty_driver->poll_get_char(kgdb_tty_driver, kgdb_tty_line);
100}
101
102static void kgdboc_put_char(u8 chr)
103{
104 kgdb_tty_driver->poll_put_char(kgdb_tty_driver, kgdb_tty_line, chr);
105}
106
107static int param_set_kgdboc_var(const char *kmessage, struct kernel_param *kp)
108{
109 int len = strlen(kmessage);
110
111 if (len >= MAX_CONFIG_LEN) {
112 printk(KERN_ERR "kgdboc: config string too long\n");
113 return -ENOSPC;
114 }
115
116 /* Only copy in the string if the init function has not run yet */
117 if (configured < 0) {
118 strcpy(config, kmessage);
119 return 0;
120 }
121
122 if (kgdb_connected) {
123 printk(KERN_ERR
124 "kgdboc: Cannot reconfigure while KGDB is connected.\n");
125
126 return -EBUSY;
127 }
128
129 strcpy(config, kmessage);
130 /* Chop out \n char as a result of echo */
131 if (config[len - 1] == '\n')
132 config[len - 1] = '\0';
133
134 if (configured == 1)
135 cleanup_kgdboc();
136
137 /* Go and configure with the new params. */
138 return configure_kgdboc();
139}
140
141static void kgdboc_pre_exp_handler(void)
142{
143 /* Increment the module count when the debugger is active */
144 if (!kgdb_connected)
145 try_module_get(THIS_MODULE);
146}
147
148static void kgdboc_post_exp_handler(void)
149{
150 /* decrement the module count when the debugger detaches */
151 if (!kgdb_connected)
152 module_put(THIS_MODULE);
153}
154
155static struct kgdb_io kgdboc_io_ops = {
156 .name = "kgdboc",
157 .read_char = kgdboc_get_char,
158 .write_char = kgdboc_put_char,
159 .pre_exception = kgdboc_pre_exp_handler,
160 .post_exception = kgdboc_post_exp_handler,
161};
162
163module_init(init_kgdboc);
164module_exit(cleanup_kgdboc);
165module_param_call(kgdboc, param_set_kgdboc_var, param_get_string, &kps, 0644);
166MODULE_PARM_DESC(kgdboc, "<serial_device>[,baud]");
167MODULE_DESCRIPTION("KGDB Console TTY Driver");
168MODULE_LICENSE("GPL");
diff --git a/drivers/serial/serial_core.c b/drivers/serial/serial_core.c
index 0f5a17987cca..c32c1ca75f63 100644
--- a/drivers/serial/serial_core.c
+++ b/drivers/serial/serial_core.c
@@ -1771,7 +1771,7 @@ static int uart_read_proc(char *page, char **start, off_t off,
1771} 1771}
1772#endif 1772#endif
1773 1773
1774#ifdef CONFIG_SERIAL_CORE_CONSOLE 1774#if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
1775/* 1775/*
1776 * uart_console_write - write a console message to a serial port 1776 * uart_console_write - write a console message to a serial port
1777 * @port: the port to write the message 1777 * @port: the port to write the message
@@ -1827,7 +1827,7 @@ uart_get_console(struct uart_port *ports, int nr, struct console *co)
1827 * options. The format of the string is <baud><parity><bits><flow>, 1827 * options. The format of the string is <baud><parity><bits><flow>,
1828 * eg: 115200n8r 1828 * eg: 115200n8r
1829 */ 1829 */
1830void __init 1830void
1831uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow) 1831uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1832{ 1832{
1833 char *s = options; 1833 char *s = options;
@@ -1842,6 +1842,7 @@ uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1842 if (*s) 1842 if (*s)
1843 *flow = *s; 1843 *flow = *s;
1844} 1844}
1845EXPORT_SYMBOL_GPL(uart_parse_options);
1845 1846
1846struct baud_rates { 1847struct baud_rates {
1847 unsigned int rate; 1848 unsigned int rate;
@@ -1872,7 +1873,7 @@ static const struct baud_rates baud_rates[] = {
1872 * @bits: number of data bits 1873 * @bits: number of data bits
1873 * @flow: flow control character - 'r' (rts) 1874 * @flow: flow control character - 'r' (rts)
1874 */ 1875 */
1875int __init 1876int
1876uart_set_options(struct uart_port *port, struct console *co, 1877uart_set_options(struct uart_port *port, struct console *co,
1877 int baud, int parity, int bits, int flow) 1878 int baud, int parity, int bits, int flow)
1878{ 1879{
@@ -1924,10 +1925,16 @@ uart_set_options(struct uart_port *port, struct console *co,
1924 port->mctrl |= TIOCM_DTR; 1925 port->mctrl |= TIOCM_DTR;
1925 1926
1926 port->ops->set_termios(port, &termios, &dummy); 1927 port->ops->set_termios(port, &termios, &dummy);
1927 co->cflag = termios.c_cflag; 1928 /*
1929 * Allow the setting of the UART parameters with a NULL console
1930 * too:
1931 */
1932 if (co)
1933 co->cflag = termios.c_cflag;
1928 1934
1929 return 0; 1935 return 0;
1930} 1936}
1937EXPORT_SYMBOL_GPL(uart_set_options);
1931#endif /* CONFIG_SERIAL_CORE_CONSOLE */ 1938#endif /* CONFIG_SERIAL_CORE_CONSOLE */
1932 1939
1933static void uart_change_pm(struct uart_state *state, int pm_state) 1940static void uart_change_pm(struct uart_state *state, int pm_state)
@@ -2182,6 +2189,60 @@ uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2182 } 2189 }
2183} 2190}
2184 2191
2192#ifdef CONFIG_CONSOLE_POLL
2193
2194static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2195{
2196 struct uart_driver *drv = driver->driver_state;
2197 struct uart_state *state = drv->state + line;
2198 struct uart_port *port;
2199 int baud = 9600;
2200 int bits = 8;
2201 int parity = 'n';
2202 int flow = 'n';
2203
2204 if (!state || !state->port)
2205 return -1;
2206
2207 port = state->port;
2208 if (!(port->ops->poll_get_char && port->ops->poll_put_char))
2209 return -1;
2210
2211 if (options) {
2212 uart_parse_options(options, &baud, &parity, &bits, &flow);
2213 return uart_set_options(port, NULL, baud, parity, bits, flow);
2214 }
2215
2216 return 0;
2217}
2218
2219static int uart_poll_get_char(struct tty_driver *driver, int line)
2220{
2221 struct uart_driver *drv = driver->driver_state;
2222 struct uart_state *state = drv->state + line;
2223 struct uart_port *port;
2224
2225 if (!state || !state->port)
2226 return -1;
2227
2228 port = state->port;
2229 return port->ops->poll_get_char(port);
2230}
2231
2232static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2233{
2234 struct uart_driver *drv = driver->driver_state;
2235 struct uart_state *state = drv->state + line;
2236 struct uart_port *port;
2237
2238 if (!state || !state->port)
2239 return;
2240
2241 port = state->port;
2242 port->ops->poll_put_char(port, ch);
2243}
2244#endif
2245
2185static const struct tty_operations uart_ops = { 2246static const struct tty_operations uart_ops = {
2186 .open = uart_open, 2247 .open = uart_open,
2187 .close = uart_close, 2248 .close = uart_close,
@@ -2206,6 +2267,11 @@ static const struct tty_operations uart_ops = {
2206#endif 2267#endif
2207 .tiocmget = uart_tiocmget, 2268 .tiocmget = uart_tiocmget,
2208 .tiocmset = uart_tiocmset, 2269 .tiocmset = uart_tiocmset,
2270#ifdef CONFIG_CONSOLE_POLL
2271 .poll_init = uart_poll_init,
2272 .poll_get_char = uart_poll_get_char,
2273 .poll_put_char = uart_poll_put_char,
2274#endif
2209}; 2275};
2210 2276
2211/** 2277/**
diff --git a/include/asm-x86/kdebug.h b/include/asm-x86/kdebug.h
index 0c4175390dab..96651bb59ba1 100644
--- a/include/asm-x86/kdebug.h
+++ b/include/asm-x86/kdebug.h
@@ -20,6 +20,7 @@ enum die_val {
20 DIE_CALL, 20 DIE_CALL,
21 DIE_NMI_IPI, 21 DIE_NMI_IPI,
22 DIE_PAGE_FAULT, 22 DIE_PAGE_FAULT,
23 DIE_NMIUNKNOWN,
23}; 24};
24 25
25extern void printk_address(unsigned long address, int reliable); 26extern void printk_address(unsigned long address, int reliable);
diff --git a/include/asm-x86/kgdb.h b/include/asm-x86/kgdb.h
new file mode 100644
index 000000000000..484c47554f3b
--- /dev/null
+++ b/include/asm-x86/kgdb.h
@@ -0,0 +1,81 @@
1#ifndef _ASM_KGDB_H_
2#define _ASM_KGDB_H_
3
4/*
5 * Copyright (C) 2001-2004 Amit S. Kale
6 * Copyright (C) 2008 Wind River Systems, Inc.
7 */
8
9/*
10 * BUFMAX defines the maximum number of characters in inbound/outbound
11 * buffers at least NUMREGBYTES*2 are needed for register packets
12 * Longer buffer is needed to list all threads
13 */
14#define BUFMAX 1024
15
16/*
17 * Note that this register image is in a different order than
18 * the register image that Linux produces at interrupt time.
19 *
20 * Linux's register image is defined by struct pt_regs in ptrace.h.
21 * Just why GDB uses a different order is a historical mystery.
22 */
23#ifdef CONFIG_X86_32
24enum regnames {
25 GDB_AX, /* 0 */
26 GDB_CX, /* 1 */
27 GDB_DX, /* 2 */
28 GDB_BX, /* 3 */
29 GDB_SP, /* 4 */
30 GDB_BP, /* 5 */
31 GDB_SI, /* 6 */
32 GDB_DI, /* 7 */
33 GDB_PC, /* 8 also known as eip */
34 GDB_PS, /* 9 also known as eflags */
35 GDB_CS, /* 10 */
36 GDB_SS, /* 11 */
37 GDB_DS, /* 12 */
38 GDB_ES, /* 13 */
39 GDB_FS, /* 14 */
40 GDB_GS, /* 15 */
41};
42#else /* ! CONFIG_X86_32 */
43enum regnames {
44 GDB_AX, /* 0 */
45 GDB_DX, /* 1 */
46 GDB_CX, /* 2 */
47 GDB_BX, /* 3 */
48 GDB_SI, /* 4 */
49 GDB_DI, /* 5 */
50 GDB_BP, /* 6 */
51 GDB_SP, /* 7 */
52 GDB_R8, /* 8 */
53 GDB_R9, /* 9 */
54 GDB_R10, /* 10 */
55 GDB_R11, /* 11 */
56 GDB_R12, /* 12 */
57 GDB_R13, /* 13 */
58 GDB_R14, /* 14 */
59 GDB_R15, /* 15 */
60 GDB_PC, /* 16 */
61 GDB_PS, /* 17 */
62};
63#endif /* CONFIG_X86_32 */
64
65/*
66 * Number of bytes of registers:
67 */
68#ifdef CONFIG_X86_32
69# define NUMREGBYTES 64
70#else
71# define NUMREGBYTES ((GDB_PS+1)*8)
72#endif
73
74static inline void arch_kgdb_breakpoint(void)
75{
76 asm(" int $3");
77}
78#define BREAK_INSTR_SIZE 1
79#define CACHE_FLUSH_IS_SAFE 1
80
81#endif /* _ASM_KGDB_H_ */
diff --git a/include/linux/clocksource.h b/include/linux/clocksource.h
index 85778a4b1209..35094479ca55 100644
--- a/include/linux/clocksource.h
+++ b/include/linux/clocksource.h
@@ -216,6 +216,7 @@ static inline void clocksource_calculate_interval(struct clocksource *c,
216/* used to install a new clocksource */ 216/* used to install a new clocksource */
217extern int clocksource_register(struct clocksource*); 217extern int clocksource_register(struct clocksource*);
218extern void clocksource_unregister(struct clocksource*); 218extern void clocksource_unregister(struct clocksource*);
219extern void clocksource_touch_watchdog(void);
219extern struct clocksource* clocksource_get_next(void); 220extern struct clocksource* clocksource_get_next(void);
220extern void clocksource_change_rating(struct clocksource *cs, int rating); 221extern void clocksource_change_rating(struct clocksource *cs, int rating);
221extern void clocksource_resume(void); 222extern void clocksource_resume(void);
diff --git a/include/linux/kgdb.h b/include/linux/kgdb.h
new file mode 100644
index 000000000000..9757b1a6d9dc
--- /dev/null
+++ b/include/linux/kgdb.h
@@ -0,0 +1,281 @@
1/*
2 * This provides the callbacks and functions that KGDB needs to share between
3 * the core, I/O and arch-specific portions.
4 *
5 * Author: Amit Kale <amitkale@linsyssoft.com> and
6 * Tom Rini <trini@kernel.crashing.org>
7 *
8 * 2001-2004 (c) Amit S. Kale and 2003-2005 (c) MontaVista Software, Inc.
9 * This file is licensed under the terms of the GNU General Public License
10 * version 2. This program is licensed "as is" without any warranty of any
11 * kind, whether express or implied.
12 */
13#ifndef _KGDB_H_
14#define _KGDB_H_
15
16#include <linux/serial_8250.h>
17#include <linux/linkage.h>
18#include <linux/init.h>
19
20#include <asm/atomic.h>
21#include <asm/kgdb.h>
22
23struct pt_regs;
24
25/**
26 * kgdb_skipexception - (optional) exit kgdb_handle_exception early
27 * @exception: Exception vector number
28 * @regs: Current &struct pt_regs.
29 *
30 * On some architectures it is required to skip a breakpoint
31 * exception when it occurs after a breakpoint has been removed.
32 * This can be implemented in the architecture specific portion of
33 * for kgdb.
34 */
35extern int kgdb_skipexception(int exception, struct pt_regs *regs);
36
37/**
38 * kgdb_post_primary_code - (optional) Save error vector/code numbers.
39 * @regs: Original pt_regs.
40 * @e_vector: Original error vector.
41 * @err_code: Original error code.
42 *
43 * This is usually needed on architectures which support SMP and
44 * KGDB. This function is called after all the secondary cpus have
45 * been put to a know spin state and the primary CPU has control over
46 * KGDB.
47 */
48extern void kgdb_post_primary_code(struct pt_regs *regs, int e_vector,
49 int err_code);
50
51/**
52 * kgdb_disable_hw_debug - (optional) Disable hardware debugging hook
53 * @regs: Current &struct pt_regs.
54 *
55 * This function will be called if the particular architecture must
56 * disable hardware debugging while it is processing gdb packets or
57 * handling exception.
58 */
59extern void kgdb_disable_hw_debug(struct pt_regs *regs);
60
61struct tasklet_struct;
62struct task_struct;
63struct uart_port;
64
65/**
66 * kgdb_breakpoint - compiled in breakpoint
67 *
68 * This will be impelmented a static inline per architecture. This
69 * function is called by the kgdb core to execute an architecture
70 * specific trap to cause kgdb to enter the exception processing.
71 *
72 */
73void kgdb_breakpoint(void);
74
75extern int kgdb_connected;
76
77extern atomic_t kgdb_setting_breakpoint;
78extern atomic_t kgdb_cpu_doing_single_step;
79
80extern struct task_struct *kgdb_usethread;
81extern struct task_struct *kgdb_contthread;
82
83enum kgdb_bptype {
84 BP_BREAKPOINT = 0,
85 BP_HARDWARE_BREAKPOINT,
86 BP_WRITE_WATCHPOINT,
87 BP_READ_WATCHPOINT,
88 BP_ACCESS_WATCHPOINT
89};
90
91enum kgdb_bpstate {
92 BP_UNDEFINED = 0,
93 BP_REMOVED,
94 BP_SET,
95 BP_ACTIVE
96};
97
98struct kgdb_bkpt {
99 unsigned long bpt_addr;
100 unsigned char saved_instr[BREAK_INSTR_SIZE];
101 enum kgdb_bptype type;
102 enum kgdb_bpstate state;
103};
104
105#ifndef KGDB_MAX_BREAKPOINTS
106# define KGDB_MAX_BREAKPOINTS 1000
107#endif
108
109#define KGDB_HW_BREAKPOINT 1
110
111/*
112 * Functions each KGDB-supporting architecture must provide:
113 */
114
115/**
116 * kgdb_arch_init - Perform any architecture specific initalization.
117 *
118 * This function will handle the initalization of any architecture
119 * specific callbacks.
120 */
121extern int kgdb_arch_init(void);
122
123/**
124 * kgdb_arch_exit - Perform any architecture specific uninitalization.
125 *
126 * This function will handle the uninitalization of any architecture
127 * specific callbacks, for dynamic registration and unregistration.
128 */
129extern void kgdb_arch_exit(void);
130
131/**
132 * pt_regs_to_gdb_regs - Convert ptrace regs to GDB regs
133 * @gdb_regs: A pointer to hold the registers in the order GDB wants.
134 * @regs: The &struct pt_regs of the current process.
135 *
136 * Convert the pt_regs in @regs into the format for registers that
137 * GDB expects, stored in @gdb_regs.
138 */
139extern void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs);
140
141/**
142 * sleeping_thread_to_gdb_regs - Convert ptrace regs to GDB regs
143 * @gdb_regs: A pointer to hold the registers in the order GDB wants.
144 * @p: The &struct task_struct of the desired process.
145 *
146 * Convert the register values of the sleeping process in @p to
147 * the format that GDB expects.
148 * This function is called when kgdb does not have access to the
149 * &struct pt_regs and therefore it should fill the gdb registers
150 * @gdb_regs with what has been saved in &struct thread_struct
151 * thread field during switch_to.
152 */
153extern void
154sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p);
155
156/**
157 * gdb_regs_to_pt_regs - Convert GDB regs to ptrace regs.
158 * @gdb_regs: A pointer to hold the registers we've received from GDB.
159 * @regs: A pointer to a &struct pt_regs to hold these values in.
160 *
161 * Convert the GDB regs in @gdb_regs into the pt_regs, and store them
162 * in @regs.
163 */
164extern void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs);
165
166/**
167 * kgdb_arch_handle_exception - Handle architecture specific GDB packets.
168 * @vector: The error vector of the exception that happened.
169 * @signo: The signal number of the exception that happened.
170 * @err_code: The error code of the exception that happened.
171 * @remcom_in_buffer: The buffer of the packet we have read.
172 * @remcom_out_buffer: The buffer of %BUFMAX bytes to write a packet into.
173 * @regs: The &struct pt_regs of the current process.
174 *
175 * This function MUST handle the 'c' and 's' command packets,
176 * as well packets to set / remove a hardware breakpoint, if used.
177 * If there are additional packets which the hardware needs to handle,
178 * they are handled here. The code should return -1 if it wants to
179 * process more packets, and a %0 or %1 if it wants to exit from the
180 * kgdb callback.
181 */
182extern int
183kgdb_arch_handle_exception(int vector, int signo, int err_code,
184 char *remcom_in_buffer,
185 char *remcom_out_buffer,
186 struct pt_regs *regs);
187
188/**
189 * kgdb_roundup_cpus - Get other CPUs into a holding pattern
190 * @flags: Current IRQ state
191 *
192 * On SMP systems, we need to get the attention of the other CPUs
193 * and get them be in a known state. This should do what is needed
194 * to get the other CPUs to call kgdb_wait(). Note that on some arches,
195 * the NMI approach is not used for rounding up all the CPUs. For example,
196 * in case of MIPS, smp_call_function() is used to roundup CPUs. In
197 * this case, we have to make sure that interrupts are enabled before
198 * calling smp_call_function(). The argument to this function is
199 * the flags that will be used when restoring the interrupts. There is
200 * local_irq_save() call before kgdb_roundup_cpus().
201 *
202 * On non-SMP systems, this is not called.
203 */
204extern void kgdb_roundup_cpus(unsigned long flags);
205
206/* Optional functions. */
207extern int kgdb_validate_break_address(unsigned long addr);
208extern int kgdb_arch_set_breakpoint(unsigned long addr, char *saved_instr);
209extern int kgdb_arch_remove_breakpoint(unsigned long addr, char *bundle);
210
211/**
212 * struct kgdb_arch - Describe architecture specific values.
213 * @gdb_bpt_instr: The instruction to trigger a breakpoint.
214 * @flags: Flags for the breakpoint, currently just %KGDB_HW_BREAKPOINT.
215 * @set_breakpoint: Allow an architecture to specify how to set a software
216 * breakpoint.
217 * @remove_breakpoint: Allow an architecture to specify how to remove a
218 * software breakpoint.
219 * @set_hw_breakpoint: Allow an architecture to specify how to set a hardware
220 * breakpoint.
221 * @remove_hw_breakpoint: Allow an architecture to specify how to remove a
222 * hardware breakpoint.
223 * @remove_all_hw_break: Allow an architecture to specify how to remove all
224 * hardware breakpoints.
225 * @correct_hw_break: Allow an architecture to specify how to correct the
226 * hardware debug registers.
227 */
228struct kgdb_arch {
229 unsigned char gdb_bpt_instr[BREAK_INSTR_SIZE];
230 unsigned long flags;
231
232 int (*set_breakpoint)(unsigned long, char *);
233 int (*remove_breakpoint)(unsigned long, char *);
234 int (*set_hw_breakpoint)(unsigned long, int, enum kgdb_bptype);
235 int (*remove_hw_breakpoint)(unsigned long, int, enum kgdb_bptype);
236 void (*remove_all_hw_break)(void);
237 void (*correct_hw_break)(void);
238};
239
240/**
241 * struct kgdb_io - Describe the interface for an I/O driver to talk with KGDB.
242 * @name: Name of the I/O driver.
243 * @read_char: Pointer to a function that will return one char.
244 * @write_char: Pointer to a function that will write one char.
245 * @flush: Pointer to a function that will flush any pending writes.
246 * @init: Pointer to a function that will initialize the device.
247 * @pre_exception: Pointer to a function that will do any prep work for
248 * the I/O driver.
249 * @post_exception: Pointer to a function that will do any cleanup work
250 * for the I/O driver.
251 */
252struct kgdb_io {
253 const char *name;
254 int (*read_char) (void);
255 void (*write_char) (u8);
256 void (*flush) (void);
257 int (*init) (void);
258 void (*pre_exception) (void);
259 void (*post_exception) (void);
260};
261
262extern struct kgdb_arch arch_kgdb_ops;
263
264extern int kgdb_register_io_module(struct kgdb_io *local_kgdb_io_ops);
265extern void kgdb_unregister_io_module(struct kgdb_io *local_kgdb_io_ops);
266
267extern int kgdb_hex2long(char **ptr, long *long_val);
268extern int kgdb_mem2hex(char *mem, char *buf, int count);
269extern int kgdb_hex2mem(char *buf, char *mem, int count);
270
271extern int kgdb_isremovedbreak(unsigned long addr);
272
273extern int
274kgdb_handle_exception(int ex_vector, int signo, int err_code,
275 struct pt_regs *regs);
276extern int kgdb_nmicallback(int cpu, void *regs);
277
278extern int kgdb_single_step;
279extern atomic_t kgdb_active;
280
281#endif /* _KGDB_H_ */
diff --git a/include/linux/serial_core.h b/include/linux/serial_core.h
index 289942fc6655..7cb094a82456 100644
--- a/include/linux/serial_core.h
+++ b/include/linux/serial_core.h
@@ -213,6 +213,10 @@ struct uart_ops {
213 void (*config_port)(struct uart_port *, int); 213 void (*config_port)(struct uart_port *, int);
214 int (*verify_port)(struct uart_port *, struct serial_struct *); 214 int (*verify_port)(struct uart_port *, struct serial_struct *);
215 int (*ioctl)(struct uart_port *, unsigned int, unsigned long); 215 int (*ioctl)(struct uart_port *, unsigned int, unsigned long);
216#ifdef CONFIG_CONSOLE_POLL
217 void (*poll_put_char)(struct uart_port *, unsigned char);
218 int (*poll_get_char)(struct uart_port *);
219#endif
216}; 220};
217 221
218#define UART_CONFIG_TYPE (1 << 0) 222#define UART_CONFIG_TYPE (1 << 0)
diff --git a/include/linux/tty_driver.h b/include/linux/tty_driver.h
index 85c95cd39bc3..21f69aca4505 100644
--- a/include/linux/tty_driver.h
+++ b/include/linux/tty_driver.h
@@ -125,6 +125,7 @@
125#include <linux/cdev.h> 125#include <linux/cdev.h>
126 126
127struct tty_struct; 127struct tty_struct;
128struct tty_driver;
128 129
129struct tty_operations { 130struct tty_operations {
130 int (*open)(struct tty_struct * tty, struct file * filp); 131 int (*open)(struct tty_struct * tty, struct file * filp);
@@ -157,6 +158,11 @@ struct tty_operations {
157 int (*tiocmget)(struct tty_struct *tty, struct file *file); 158 int (*tiocmget)(struct tty_struct *tty, struct file *file);
158 int (*tiocmset)(struct tty_struct *tty, struct file *file, 159 int (*tiocmset)(struct tty_struct *tty, struct file *file,
159 unsigned int set, unsigned int clear); 160 unsigned int set, unsigned int clear);
161#ifdef CONFIG_CONSOLE_POLL
162 int (*poll_init)(struct tty_driver *driver, int line, char *options);
163 int (*poll_get_char)(struct tty_driver *driver, int line);
164 void (*poll_put_char)(struct tty_driver *driver, int line, char ch);
165#endif
160}; 166};
161 167
162struct tty_driver { 168struct tty_driver {
@@ -220,6 +226,11 @@ struct tty_driver {
220 int (*tiocmget)(struct tty_struct *tty, struct file *file); 226 int (*tiocmget)(struct tty_struct *tty, struct file *file);
221 int (*tiocmset)(struct tty_struct *tty, struct file *file, 227 int (*tiocmset)(struct tty_struct *tty, struct file *file,
222 unsigned int set, unsigned int clear); 228 unsigned int set, unsigned int clear);
229#ifdef CONFIG_CONSOLE_POLL
230 int (*poll_init)(struct tty_driver *driver, int line, char *options);
231 int (*poll_get_char)(struct tty_driver *driver, int line);
232 void (*poll_put_char)(struct tty_driver *driver, int line, char ch);
233#endif
223 234
224 struct list_head tty_drivers; 235 struct list_head tty_drivers;
225}; 236};
@@ -230,6 +241,7 @@ struct tty_driver *alloc_tty_driver(int lines);
230void put_tty_driver(struct tty_driver *driver); 241void put_tty_driver(struct tty_driver *driver);
231void tty_set_operations(struct tty_driver *driver, 242void tty_set_operations(struct tty_driver *driver,
232 const struct tty_operations *op); 243 const struct tty_operations *op);
244extern struct tty_driver *tty_find_polling_driver(char *name, int *line);
233 245
234/* tty driver magic number */ 246/* tty driver magic number */
235#define TTY_DRIVER_MAGIC 0x5402 247#define TTY_DRIVER_MAGIC 0x5402
diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h
index 975c963e5789..fec6decfb983 100644
--- a/include/linux/uaccess.h
+++ b/include/linux/uaccess.h
@@ -84,4 +84,26 @@ static inline unsigned long __copy_from_user_nocache(void *to,
84 ret; \ 84 ret; \
85 }) 85 })
86 86
87/*
88 * probe_kernel_read(): safely attempt to read from a location
89 * @dst: pointer to the buffer that shall take the data
90 * @src: address to read from
91 * @size: size of the data chunk
92 *
93 * Safely read from address @src to the buffer at @dst. If a kernel fault
94 * happens, handle that and return -EFAULT.
95 */
96extern long probe_kernel_read(void *dst, void *src, size_t size);
97
98/*
99 * probe_kernel_write(): safely attempt to write to a location
100 * @dst: address to write to
101 * @src: pointer to the data that shall be written
102 * @size: size of the data chunk
103 *
104 * Safely write to address @dst from the buffer at @src. If a kernel fault
105 * happens, handle that and return -EFAULT.
106 */
107extern long probe_kernel_write(void *dst, void *src, size_t size);
108
87#endif /* __LINUX_UACCESS_H__ */ 109#endif /* __LINUX_UACCESS_H__ */
diff --git a/kernel/Makefile b/kernel/Makefile
index f45c69e69688..6c5f081132a4 100644
--- a/kernel/Makefile
+++ b/kernel/Makefile
@@ -53,6 +53,7 @@ obj-$(CONFIG_AUDIT) += audit.o auditfilter.o
53obj-$(CONFIG_AUDITSYSCALL) += auditsc.o 53obj-$(CONFIG_AUDITSYSCALL) += auditsc.o
54obj-$(CONFIG_AUDIT_TREE) += audit_tree.o 54obj-$(CONFIG_AUDIT_TREE) += audit_tree.o
55obj-$(CONFIG_KPROBES) += kprobes.o 55obj-$(CONFIG_KPROBES) += kprobes.o
56obj-$(CONFIG_KGDB) += kgdb.o
56obj-$(CONFIG_DETECT_SOFTLOCKUP) += softlockup.o 57obj-$(CONFIG_DETECT_SOFTLOCKUP) += softlockup.o
57obj-$(CONFIG_GENERIC_HARDIRQS) += irq/ 58obj-$(CONFIG_GENERIC_HARDIRQS) += irq/
58obj-$(CONFIG_SECCOMP) += seccomp.o 59obj-$(CONFIG_SECCOMP) += seccomp.o
diff --git a/kernel/kgdb.c b/kernel/kgdb.c
new file mode 100644
index 000000000000..1bd0ec1c80b2
--- /dev/null
+++ b/kernel/kgdb.c
@@ -0,0 +1,1700 @@
1/*
2 * KGDB stub.
3 *
4 * Maintainer: Jason Wessel <jason.wessel@windriver.com>
5 *
6 * Copyright (C) 2000-2001 VERITAS Software Corporation.
7 * Copyright (C) 2002-2004 Timesys Corporation
8 * Copyright (C) 2003-2004 Amit S. Kale <amitkale@linsyssoft.com>
9 * Copyright (C) 2004 Pavel Machek <pavel@suse.cz>
10 * Copyright (C) 2004-2006 Tom Rini <trini@kernel.crashing.org>
11 * Copyright (C) 2004-2006 LinSysSoft Technologies Pvt. Ltd.
12 * Copyright (C) 2005-2008 Wind River Systems, Inc.
13 * Copyright (C) 2007 MontaVista Software, Inc.
14 * Copyright (C) 2008 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
15 *
16 * Contributors at various stages not listed above:
17 * Jason Wessel ( jason.wessel@windriver.com )
18 * George Anzinger <george@mvista.com>
19 * Anurekh Saxena (anurekh.saxena@timesys.com)
20 * Lake Stevens Instrument Division (Glenn Engel)
21 * Jim Kingdon, Cygnus Support.
22 *
23 * Original KGDB stub: David Grothe <dave@gcom.com>,
24 * Tigran Aivazian <tigran@sco.com>
25 *
26 * This file is licensed under the terms of the GNU General Public License
27 * version 2. This program is licensed "as is" without any warranty of any
28 * kind, whether express or implied.
29 */
30#include <linux/pid_namespace.h>
31#include <linux/clocksource.h>
32#include <linux/interrupt.h>
33#include <linux/spinlock.h>
34#include <linux/console.h>
35#include <linux/threads.h>
36#include <linux/uaccess.h>
37#include <linux/kernel.h>
38#include <linux/module.h>
39#include <linux/ptrace.h>
40#include <linux/reboot.h>
41#include <linux/string.h>
42#include <linux/delay.h>
43#include <linux/sched.h>
44#include <linux/sysrq.h>
45#include <linux/init.h>
46#include <linux/kgdb.h>
47#include <linux/pid.h>
48#include <linux/smp.h>
49#include <linux/mm.h>
50
51#include <asm/cacheflush.h>
52#include <asm/byteorder.h>
53#include <asm/atomic.h>
54#include <asm/system.h>
55
56static int kgdb_break_asap;
57
58struct kgdb_state {
59 int ex_vector;
60 int signo;
61 int err_code;
62 int cpu;
63 int pass_exception;
64 long threadid;
65 long kgdb_usethreadid;
66 struct pt_regs *linux_regs;
67};
68
69static struct debuggerinfo_struct {
70 void *debuggerinfo;
71 struct task_struct *task;
72} kgdb_info[NR_CPUS];
73
74/**
75 * kgdb_connected - Is a host GDB connected to us?
76 */
77int kgdb_connected;
78EXPORT_SYMBOL_GPL(kgdb_connected);
79
80/* All the KGDB handlers are installed */
81static int kgdb_io_module_registered;
82
83/* Guard for recursive entry */
84static int exception_level;
85
86static struct kgdb_io *kgdb_io_ops;
87static DEFINE_SPINLOCK(kgdb_registration_lock);
88
89/* kgdb console driver is loaded */
90static int kgdb_con_registered;
91/* determine if kgdb console output should be used */
92static int kgdb_use_con;
93
94static int __init opt_kgdb_con(char *str)
95{
96 kgdb_use_con = 1;
97 return 0;
98}
99
100early_param("kgdbcon", opt_kgdb_con);
101
102module_param(kgdb_use_con, int, 0644);
103
104/*
105 * Holds information about breakpoints in a kernel. These breakpoints are
106 * added and removed by gdb.
107 */
108static struct kgdb_bkpt kgdb_break[KGDB_MAX_BREAKPOINTS] = {
109 [0 ... KGDB_MAX_BREAKPOINTS-1] = { .state = BP_UNDEFINED }
110};
111
112/*
113 * The CPU# of the active CPU, or -1 if none:
114 */
115atomic_t kgdb_active = ATOMIC_INIT(-1);
116
117/*
118 * We use NR_CPUs not PERCPU, in case kgdb is used to debug early
119 * bootup code (which might not have percpu set up yet):
120 */
121static atomic_t passive_cpu_wait[NR_CPUS];
122static atomic_t cpu_in_kgdb[NR_CPUS];
123atomic_t kgdb_setting_breakpoint;
124
125struct task_struct *kgdb_usethread;
126struct task_struct *kgdb_contthread;
127
128int kgdb_single_step;
129
130/* Our I/O buffers. */
131static char remcom_in_buffer[BUFMAX];
132static char remcom_out_buffer[BUFMAX];
133
134/* Storage for the registers, in GDB format. */
135static unsigned long gdb_regs[(NUMREGBYTES +
136 sizeof(unsigned long) - 1) /
137 sizeof(unsigned long)];
138
139/* to keep track of the CPU which is doing the single stepping*/
140atomic_t kgdb_cpu_doing_single_step = ATOMIC_INIT(-1);
141
142/*
143 * If you are debugging a problem where roundup (the collection of
144 * all other CPUs) is a problem [this should be extremely rare],
145 * then use the nokgdbroundup option to avoid roundup. In that case
146 * the other CPUs might interfere with your debugging context, so
147 * use this with care:
148 */
149int kgdb_do_roundup = 1;
150
151static int __init opt_nokgdbroundup(char *str)
152{
153 kgdb_do_roundup = 0;
154
155 return 0;
156}
157
158early_param("nokgdbroundup", opt_nokgdbroundup);
159
160/*
161 * Finally, some KGDB code :-)
162 */
163
164/*
165 * Weak aliases for breakpoint management,
166 * can be overriden by architectures when needed:
167 */
168int __weak kgdb_validate_break_address(unsigned long addr)
169{
170 char tmp_variable[BREAK_INSTR_SIZE];
171
172 return probe_kernel_read(tmp_variable, (char *)addr, BREAK_INSTR_SIZE);
173}
174
175int __weak kgdb_arch_set_breakpoint(unsigned long addr, char *saved_instr)
176{
177 int err;
178
179 err = probe_kernel_read(saved_instr, (char *)addr, BREAK_INSTR_SIZE);
180 if (err)
181 return err;
182
183 return probe_kernel_write((char *)addr, arch_kgdb_ops.gdb_bpt_instr,
184 BREAK_INSTR_SIZE);
185}
186
187int __weak kgdb_arch_remove_breakpoint(unsigned long addr, char *bundle)
188{
189 return probe_kernel_write((char *)addr,
190 (char *)bundle, BREAK_INSTR_SIZE);
191}
192
193unsigned long __weak kgdb_arch_pc(int exception, struct pt_regs *regs)
194{
195 return instruction_pointer(regs);
196}
197
198int __weak kgdb_arch_init(void)
199{
200 return 0;
201}
202
203int __weak kgdb_skipexception(int exception, struct pt_regs *regs)
204{
205 return 0;
206}
207
208void __weak
209kgdb_post_primary_code(struct pt_regs *regs, int e_vector, int err_code)
210{
211 return;
212}
213
214/**
215 * kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb.
216 * @regs: Current &struct pt_regs.
217 *
218 * This function will be called if the particular architecture must
219 * disable hardware debugging while it is processing gdb packets or
220 * handling exception.
221 */
222void __weak kgdb_disable_hw_debug(struct pt_regs *regs)
223{
224}
225
226/*
227 * GDB remote protocol parser:
228 */
229
230static const char hexchars[] = "0123456789abcdef";
231
232static int hex(char ch)
233{
234 if ((ch >= 'a') && (ch <= 'f'))
235 return ch - 'a' + 10;
236 if ((ch >= '0') && (ch <= '9'))
237 return ch - '0';
238 if ((ch >= 'A') && (ch <= 'F'))
239 return ch - 'A' + 10;
240 return -1;
241}
242
243/* scan for the sequence $<data>#<checksum> */
244static void get_packet(char *buffer)
245{
246 unsigned char checksum;
247 unsigned char xmitcsum;
248 int count;
249 char ch;
250
251 do {
252 /*
253 * Spin and wait around for the start character, ignore all
254 * other characters:
255 */
256 while ((ch = (kgdb_io_ops->read_char())) != '$')
257 /* nothing */;
258
259 kgdb_connected = 1;
260 checksum = 0;
261 xmitcsum = -1;
262
263 count = 0;
264
265 /*
266 * now, read until a # or end of buffer is found:
267 */
268 while (count < (BUFMAX - 1)) {
269 ch = kgdb_io_ops->read_char();
270 if (ch == '#')
271 break;
272 checksum = checksum + ch;
273 buffer[count] = ch;
274 count = count + 1;
275 }
276 buffer[count] = 0;
277
278 if (ch == '#') {
279 xmitcsum = hex(kgdb_io_ops->read_char()) << 4;
280 xmitcsum += hex(kgdb_io_ops->read_char());
281
282 if (checksum != xmitcsum)
283 /* failed checksum */
284 kgdb_io_ops->write_char('-');
285 else
286 /* successful transfer */
287 kgdb_io_ops->write_char('+');
288 if (kgdb_io_ops->flush)
289 kgdb_io_ops->flush();
290 }
291 } while (checksum != xmitcsum);
292}
293
294/*
295 * Send the packet in buffer.
296 * Check for gdb connection if asked for.
297 */
298static void put_packet(char *buffer)
299{
300 unsigned char checksum;
301 int count;
302 char ch;
303
304 /*
305 * $<packet info>#<checksum>.
306 */
307 while (1) {
308 kgdb_io_ops->write_char('$');
309 checksum = 0;
310 count = 0;
311
312 while ((ch = buffer[count])) {
313 kgdb_io_ops->write_char(ch);
314 checksum += ch;
315 count++;
316 }
317
318 kgdb_io_ops->write_char('#');
319 kgdb_io_ops->write_char(hexchars[checksum >> 4]);
320 kgdb_io_ops->write_char(hexchars[checksum & 0xf]);
321 if (kgdb_io_ops->flush)
322 kgdb_io_ops->flush();
323
324 /* Now see what we get in reply. */
325 ch = kgdb_io_ops->read_char();
326
327 if (ch == 3)
328 ch = kgdb_io_ops->read_char();
329
330 /* If we get an ACK, we are done. */
331 if (ch == '+')
332 return;
333
334 /*
335 * If we get the start of another packet, this means
336 * that GDB is attempting to reconnect. We will NAK
337 * the packet being sent, and stop trying to send this
338 * packet.
339 */
340 if (ch == '$') {
341 kgdb_io_ops->write_char('-');
342 if (kgdb_io_ops->flush)
343 kgdb_io_ops->flush();
344 return;
345 }
346 }
347}
348
349static char *pack_hex_byte(char *pkt, u8 byte)
350{
351 *pkt++ = hexchars[byte >> 4];
352 *pkt++ = hexchars[byte & 0xf];
353
354 return pkt;
355}
356
357/*
358 * Convert the memory pointed to by mem into hex, placing result in buf.
359 * Return a pointer to the last char put in buf (null). May return an error.
360 */
361int kgdb_mem2hex(char *mem, char *buf, int count)
362{
363 char *tmp;
364 int err;
365
366 /*
367 * We use the upper half of buf as an intermediate buffer for the
368 * raw memory copy. Hex conversion will work against this one.
369 */
370 tmp = buf + count;
371
372 err = probe_kernel_read(tmp, mem, count);
373 if (!err) {
374 while (count > 0) {
375 buf = pack_hex_byte(buf, *tmp);
376 tmp++;
377 count--;
378 }
379
380 *buf = 0;
381 }
382
383 return err;
384}
385
386/*
387 * Copy the binary array pointed to by buf into mem. Fix $, #, and
388 * 0x7d escaped with 0x7d. Return a pointer to the character after
389 * the last byte written.
390 */
391static int kgdb_ebin2mem(char *buf, char *mem, int count)
392{
393 int err = 0;
394 char c;
395
396 while (count-- > 0) {
397 c = *buf++;
398 if (c == 0x7d)
399 c = *buf++ ^ 0x20;
400
401 err = probe_kernel_write(mem, &c, 1);
402 if (err)
403 break;
404
405 mem++;
406 }
407
408 return err;
409}
410
411/*
412 * Convert the hex array pointed to by buf into binary to be placed in mem.
413 * Return a pointer to the character AFTER the last byte written.
414 * May return an error.
415 */
416int kgdb_hex2mem(char *buf, char *mem, int count)
417{
418 char *tmp_raw;
419 char *tmp_hex;
420
421 /*
422 * We use the upper half of buf as an intermediate buffer for the
423 * raw memory that is converted from hex.
424 */
425 tmp_raw = buf + count * 2;
426
427 tmp_hex = tmp_raw - 1;
428 while (tmp_hex >= buf) {
429 tmp_raw--;
430 *tmp_raw = hex(*tmp_hex--);
431 *tmp_raw |= hex(*tmp_hex--) << 4;
432 }
433
434 return probe_kernel_write(mem, tmp_raw, count);
435}
436
437/*
438 * While we find nice hex chars, build a long_val.
439 * Return number of chars processed.
440 */
441int kgdb_hex2long(char **ptr, long *long_val)
442{
443 int hex_val;
444 int num = 0;
445
446 *long_val = 0;
447
448 while (**ptr) {
449 hex_val = hex(**ptr);
450 if (hex_val < 0)
451 break;
452
453 *long_val = (*long_val << 4) | hex_val;
454 num++;
455 (*ptr)++;
456 }
457
458 return num;
459}
460
461/* Write memory due to an 'M' or 'X' packet. */
462static int write_mem_msg(int binary)
463{
464 char *ptr = &remcom_in_buffer[1];
465 unsigned long addr;
466 unsigned long length;
467 int err;
468
469 if (kgdb_hex2long(&ptr, &addr) > 0 && *(ptr++) == ',' &&
470 kgdb_hex2long(&ptr, &length) > 0 && *(ptr++) == ':') {
471 if (binary)
472 err = kgdb_ebin2mem(ptr, (char *)addr, length);
473 else
474 err = kgdb_hex2mem(ptr, (char *)addr, length);
475 if (err)
476 return err;
477 if (CACHE_FLUSH_IS_SAFE)
478 flush_icache_range(addr, addr + length + 1);
479 return 0;
480 }
481
482 return -EINVAL;
483}
484
485static void error_packet(char *pkt, int error)
486{
487 error = -error;
488 pkt[0] = 'E';
489 pkt[1] = hexchars[(error / 10)];
490 pkt[2] = hexchars[(error % 10)];
491 pkt[3] = '\0';
492}
493
494/*
495 * Thread ID accessors. We represent a flat TID space to GDB, where
496 * the per CPU idle threads (which under Linux all have PID 0) are
497 * remapped to negative TIDs.
498 */
499
500#define BUF_THREAD_ID_SIZE 16
501
502static char *pack_threadid(char *pkt, unsigned char *id)
503{
504 char *limit;
505
506 limit = pkt + BUF_THREAD_ID_SIZE;
507 while (pkt < limit)
508 pkt = pack_hex_byte(pkt, *id++);
509
510 return pkt;
511}
512
513static void int_to_threadref(unsigned char *id, int value)
514{
515 unsigned char *scan;
516 int i = 4;
517
518 scan = (unsigned char *)id;
519 while (i--)
520 *scan++ = 0;
521 *scan++ = (value >> 24) & 0xff;
522 *scan++ = (value >> 16) & 0xff;
523 *scan++ = (value >> 8) & 0xff;
524 *scan++ = (value & 0xff);
525}
526
527static struct task_struct *getthread(struct pt_regs *regs, int tid)
528{
529 /*
530 * Non-positive TIDs are remapped idle tasks:
531 */
532 if (tid <= 0)
533 return idle_task(-tid);
534
535 /*
536 * find_task_by_pid_ns() does not take the tasklist lock anymore
537 * but is nicely RCU locked - hence is a pretty resilient
538 * thing to use:
539 */
540 return find_task_by_pid_ns(tid, &init_pid_ns);
541}
542
543/*
544 * CPU debug state control:
545 */
546
547#ifdef CONFIG_SMP
548static void kgdb_wait(struct pt_regs *regs)
549{
550 unsigned long flags;
551 int cpu;
552
553 local_irq_save(flags);
554 cpu = raw_smp_processor_id();
555 kgdb_info[cpu].debuggerinfo = regs;
556 kgdb_info[cpu].task = current;
557 /*
558 * Make sure the above info reaches the primary CPU before
559 * our cpu_in_kgdb[] flag setting does:
560 */
561 smp_wmb();
562 atomic_set(&cpu_in_kgdb[cpu], 1);
563
564 /* Wait till primary CPU is done with debugging */
565 while (atomic_read(&passive_cpu_wait[cpu]))
566 cpu_relax();
567
568 kgdb_info[cpu].debuggerinfo = NULL;
569 kgdb_info[cpu].task = NULL;
570
571 /* fix up hardware debug registers on local cpu */
572 if (arch_kgdb_ops.correct_hw_break)
573 arch_kgdb_ops.correct_hw_break();
574
575 /* Signal the primary CPU that we are done: */
576 atomic_set(&cpu_in_kgdb[cpu], 0);
577 clocksource_touch_watchdog();
578 local_irq_restore(flags);
579}
580#endif
581
582/*
583 * Some architectures need cache flushes when we set/clear a
584 * breakpoint:
585 */
586static void kgdb_flush_swbreak_addr(unsigned long addr)
587{
588 if (!CACHE_FLUSH_IS_SAFE)
589 return;
590
591 if (current->mm && current->mm->mmap_cache) {
592 flush_cache_range(current->mm->mmap_cache,
593 addr, addr + BREAK_INSTR_SIZE);
594 }
595 /* Force flush instruction cache if it was outside the mm */
596 flush_icache_range(addr, addr + BREAK_INSTR_SIZE);
597}
598
599/*
600 * SW breakpoint management:
601 */
602static int kgdb_activate_sw_breakpoints(void)
603{
604 unsigned long addr;
605 int error = 0;
606 int i;
607
608 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
609 if (kgdb_break[i].state != BP_SET)
610 continue;
611
612 addr = kgdb_break[i].bpt_addr;
613 error = kgdb_arch_set_breakpoint(addr,
614 kgdb_break[i].saved_instr);
615 if (error)
616 return error;
617
618 kgdb_flush_swbreak_addr(addr);
619 kgdb_break[i].state = BP_ACTIVE;
620 }
621 return 0;
622}
623
624static int kgdb_set_sw_break(unsigned long addr)
625{
626 int err = kgdb_validate_break_address(addr);
627 int breakno = -1;
628 int i;
629
630 if (err)
631 return err;
632
633 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
634 if ((kgdb_break[i].state == BP_SET) &&
635 (kgdb_break[i].bpt_addr == addr))
636 return -EEXIST;
637 }
638 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
639 if (kgdb_break[i].state == BP_REMOVED &&
640 kgdb_break[i].bpt_addr == addr) {
641 breakno = i;
642 break;
643 }
644 }
645
646 if (breakno == -1) {
647 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
648 if (kgdb_break[i].state == BP_UNDEFINED) {
649 breakno = i;
650 break;
651 }
652 }
653 }
654
655 if (breakno == -1)
656 return -E2BIG;
657
658 kgdb_break[breakno].state = BP_SET;
659 kgdb_break[breakno].type = BP_BREAKPOINT;
660 kgdb_break[breakno].bpt_addr = addr;
661
662 return 0;
663}
664
665static int kgdb_deactivate_sw_breakpoints(void)
666{
667 unsigned long addr;
668 int error = 0;
669 int i;
670
671 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
672 if (kgdb_break[i].state != BP_ACTIVE)
673 continue;
674 addr = kgdb_break[i].bpt_addr;
675 error = kgdb_arch_remove_breakpoint(addr,
676 kgdb_break[i].saved_instr);
677 if (error)
678 return error;
679
680 kgdb_flush_swbreak_addr(addr);
681 kgdb_break[i].state = BP_SET;
682 }
683 return 0;
684}
685
686static int kgdb_remove_sw_break(unsigned long addr)
687{
688 int i;
689
690 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
691 if ((kgdb_break[i].state == BP_SET) &&
692 (kgdb_break[i].bpt_addr == addr)) {
693 kgdb_break[i].state = BP_REMOVED;
694 return 0;
695 }
696 }
697 return -ENOENT;
698}
699
700int kgdb_isremovedbreak(unsigned long addr)
701{
702 int i;
703
704 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
705 if ((kgdb_break[i].state == BP_REMOVED) &&
706 (kgdb_break[i].bpt_addr == addr))
707 return 1;
708 }
709 return 0;
710}
711
712int remove_all_break(void)
713{
714 unsigned long addr;
715 int error;
716 int i;
717
718 /* Clear memory breakpoints. */
719 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
720 if (kgdb_break[i].state != BP_ACTIVE)
721 goto setundefined;
722 addr = kgdb_break[i].bpt_addr;
723 error = kgdb_arch_remove_breakpoint(addr,
724 kgdb_break[i].saved_instr);
725 if (error)
726 printk(KERN_ERR "KGDB: breakpoint remove failed: %lx\n",
727 addr);
728setundefined:
729 kgdb_break[i].state = BP_UNDEFINED;
730 }
731
732 /* Clear hardware breakpoints. */
733 if (arch_kgdb_ops.remove_all_hw_break)
734 arch_kgdb_ops.remove_all_hw_break();
735
736 return 0;
737}
738
739/*
740 * Remap normal tasks to their real PID, idle tasks to -1 ... -NR_CPUs:
741 */
742static inline int shadow_pid(int realpid)
743{
744 if (realpid)
745 return realpid;
746
747 return -1-raw_smp_processor_id();
748}
749
750static char gdbmsgbuf[BUFMAX + 1];
751
752static void kgdb_msg_write(const char *s, int len)
753{
754 char *bufptr;
755 int wcount;
756 int i;
757
758 /* 'O'utput */
759 gdbmsgbuf[0] = 'O';
760
761 /* Fill and send buffers... */
762 while (len > 0) {
763 bufptr = gdbmsgbuf + 1;
764
765 /* Calculate how many this time */
766 if ((len << 1) > (BUFMAX - 2))
767 wcount = (BUFMAX - 2) >> 1;
768 else
769 wcount = len;
770
771 /* Pack in hex chars */
772 for (i = 0; i < wcount; i++)
773 bufptr = pack_hex_byte(bufptr, s[i]);
774 *bufptr = '\0';
775
776 /* Move up */
777 s += wcount;
778 len -= wcount;
779
780 /* Write packet */
781 put_packet(gdbmsgbuf);
782 }
783}
784
785/*
786 * Return true if there is a valid kgdb I/O module. Also if no
787 * debugger is attached a message can be printed to the console about
788 * waiting for the debugger to attach.
789 *
790 * The print_wait argument is only to be true when called from inside
791 * the core kgdb_handle_exception, because it will wait for the
792 * debugger to attach.
793 */
794static int kgdb_io_ready(int print_wait)
795{
796 if (!kgdb_io_ops)
797 return 0;
798 if (kgdb_connected)
799 return 1;
800 if (atomic_read(&kgdb_setting_breakpoint))
801 return 1;
802 if (print_wait)
803 printk(KERN_CRIT "KGDB: Waiting for remote debugger\n");
804 return 1;
805}
806
807/*
808 * All the functions that start with gdb_cmd are the various
809 * operations to implement the handlers for the gdbserial protocol
810 * where KGDB is communicating with an external debugger
811 */
812
813/* Handle the '?' status packets */
814static void gdb_cmd_status(struct kgdb_state *ks)
815{
816 /*
817 * We know that this packet is only sent
818 * during initial connect. So to be safe,
819 * we clear out our breakpoints now in case
820 * GDB is reconnecting.
821 */
822 remove_all_break();
823
824 remcom_out_buffer[0] = 'S';
825 pack_hex_byte(&remcom_out_buffer[1], ks->signo);
826}
827
828/* Handle the 'g' get registers request */
829static void gdb_cmd_getregs(struct kgdb_state *ks)
830{
831 struct task_struct *thread;
832 void *local_debuggerinfo;
833 int i;
834
835 thread = kgdb_usethread;
836 if (!thread) {
837 thread = kgdb_info[ks->cpu].task;
838 local_debuggerinfo = kgdb_info[ks->cpu].debuggerinfo;
839 } else {
840 local_debuggerinfo = NULL;
841 for (i = 0; i < NR_CPUS; i++) {
842 /*
843 * Try to find the task on some other
844 * or possibly this node if we do not
845 * find the matching task then we try
846 * to approximate the results.
847 */
848 if (thread == kgdb_info[i].task)
849 local_debuggerinfo = kgdb_info[i].debuggerinfo;
850 }
851 }
852
853 /*
854 * All threads that don't have debuggerinfo should be
855 * in __schedule() sleeping, since all other CPUs
856 * are in kgdb_wait, and thus have debuggerinfo.
857 */
858 if (local_debuggerinfo) {
859 pt_regs_to_gdb_regs(gdb_regs, local_debuggerinfo);
860 } else {
861 /*
862 * Pull stuff saved during switch_to; nothing
863 * else is accessible (or even particularly
864 * relevant).
865 *
866 * This should be enough for a stack trace.
867 */
868 sleeping_thread_to_gdb_regs(gdb_regs, thread);
869 }
870 kgdb_mem2hex((char *)gdb_regs, remcom_out_buffer, NUMREGBYTES);
871}
872
873/* Handle the 'G' set registers request */
874static void gdb_cmd_setregs(struct kgdb_state *ks)
875{
876 kgdb_hex2mem(&remcom_in_buffer[1], (char *)gdb_regs, NUMREGBYTES);
877
878 if (kgdb_usethread && kgdb_usethread != current) {
879 error_packet(remcom_out_buffer, -EINVAL);
880 } else {
881 gdb_regs_to_pt_regs(gdb_regs, ks->linux_regs);
882 strcpy(remcom_out_buffer, "OK");
883 }
884}
885
886/* Handle the 'm' memory read bytes */
887static void gdb_cmd_memread(struct kgdb_state *ks)
888{
889 char *ptr = &remcom_in_buffer[1];
890 unsigned long length;
891 unsigned long addr;
892 int err;
893
894 if (kgdb_hex2long(&ptr, &addr) > 0 && *ptr++ == ',' &&
895 kgdb_hex2long(&ptr, &length) > 0) {
896 err = kgdb_mem2hex((char *)addr, remcom_out_buffer, length);
897 if (err)
898 error_packet(remcom_out_buffer, err);
899 } else {
900 error_packet(remcom_out_buffer, -EINVAL);
901 }
902}
903
904/* Handle the 'M' memory write bytes */
905static void gdb_cmd_memwrite(struct kgdb_state *ks)
906{
907 int err = write_mem_msg(0);
908
909 if (err)
910 error_packet(remcom_out_buffer, err);
911 else
912 strcpy(remcom_out_buffer, "OK");
913}
914
915/* Handle the 'X' memory binary write bytes */
916static void gdb_cmd_binwrite(struct kgdb_state *ks)
917{
918 int err = write_mem_msg(1);
919
920 if (err)
921 error_packet(remcom_out_buffer, err);
922 else
923 strcpy(remcom_out_buffer, "OK");
924}
925
926/* Handle the 'D' or 'k', detach or kill packets */
927static void gdb_cmd_detachkill(struct kgdb_state *ks)
928{
929 int error;
930
931 /* The detach case */
932 if (remcom_in_buffer[0] == 'D') {
933 error = remove_all_break();
934 if (error < 0) {
935 error_packet(remcom_out_buffer, error);
936 } else {
937 strcpy(remcom_out_buffer, "OK");
938 kgdb_connected = 0;
939 }
940 put_packet(remcom_out_buffer);
941 } else {
942 /*
943 * Assume the kill case, with no exit code checking,
944 * trying to force detach the debugger:
945 */
946 remove_all_break();
947 kgdb_connected = 0;
948 }
949}
950
951/* Handle the 'R' reboot packets */
952static int gdb_cmd_reboot(struct kgdb_state *ks)
953{
954 /* For now, only honor R0 */
955 if (strcmp(remcom_in_buffer, "R0") == 0) {
956 printk(KERN_CRIT "Executing emergency reboot\n");
957 strcpy(remcom_out_buffer, "OK");
958 put_packet(remcom_out_buffer);
959
960 /*
961 * Execution should not return from
962 * machine_emergency_restart()
963 */
964 machine_emergency_restart();
965 kgdb_connected = 0;
966
967 return 1;
968 }
969 return 0;
970}
971
972/* Handle the 'q' query packets */
973static void gdb_cmd_query(struct kgdb_state *ks)
974{
975 struct task_struct *thread;
976 unsigned char thref[8];
977 char *ptr;
978 int i;
979
980 switch (remcom_in_buffer[1]) {
981 case 's':
982 case 'f':
983 if (memcmp(remcom_in_buffer + 2, "ThreadInfo", 10)) {
984 error_packet(remcom_out_buffer, -EINVAL);
985 break;
986 }
987
988 if (remcom_in_buffer[1] == 'f')
989 ks->threadid = 1;
990
991 remcom_out_buffer[0] = 'm';
992 ptr = remcom_out_buffer + 1;
993
994 for (i = 0; i < 17; ks->threadid++) {
995 thread = getthread(ks->linux_regs, ks->threadid);
996 if (thread) {
997 int_to_threadref(thref, ks->threadid);
998 pack_threadid(ptr, thref);
999 ptr += BUF_THREAD_ID_SIZE;
1000 *(ptr++) = ',';
1001 i++;
1002 }
1003 }
1004 *(--ptr) = '\0';
1005 break;
1006
1007 case 'C':
1008 /* Current thread id */
1009 strcpy(remcom_out_buffer, "QC");
1010 ks->threadid = shadow_pid(current->pid);
1011 int_to_threadref(thref, ks->threadid);
1012 pack_threadid(remcom_out_buffer + 2, thref);
1013 break;
1014 case 'T':
1015 if (memcmp(remcom_in_buffer + 1, "ThreadExtraInfo,", 16)) {
1016 error_packet(remcom_out_buffer, -EINVAL);
1017 break;
1018 }
1019 ks->threadid = 0;
1020 ptr = remcom_in_buffer + 17;
1021 kgdb_hex2long(&ptr, &ks->threadid);
1022 if (!getthread(ks->linux_regs, ks->threadid)) {
1023 error_packet(remcom_out_buffer, -EINVAL);
1024 break;
1025 }
1026 if (ks->threadid > 0) {
1027 kgdb_mem2hex(getthread(ks->linux_regs,
1028 ks->threadid)->comm,
1029 remcom_out_buffer, 16);
1030 } else {
1031 static char tmpstr[23 + BUF_THREAD_ID_SIZE];
1032
1033 sprintf(tmpstr, "Shadow task %d for pid 0",
1034 (int)(-ks->threadid-1));
1035 kgdb_mem2hex(tmpstr, remcom_out_buffer, strlen(tmpstr));
1036 }
1037 break;
1038 }
1039}
1040
1041/* Handle the 'H' task query packets */
1042static void gdb_cmd_task(struct kgdb_state *ks)
1043{
1044 struct task_struct *thread;
1045 char *ptr;
1046
1047 switch (remcom_in_buffer[1]) {
1048 case 'g':
1049 ptr = &remcom_in_buffer[2];
1050 kgdb_hex2long(&ptr, &ks->threadid);
1051 thread = getthread(ks->linux_regs, ks->threadid);
1052 if (!thread && ks->threadid > 0) {
1053 error_packet(remcom_out_buffer, -EINVAL);
1054 break;
1055 }
1056 kgdb_usethread = thread;
1057 ks->kgdb_usethreadid = ks->threadid;
1058 strcpy(remcom_out_buffer, "OK");
1059 break;
1060 case 'c':
1061 ptr = &remcom_in_buffer[2];
1062 kgdb_hex2long(&ptr, &ks->threadid);
1063 if (!ks->threadid) {
1064 kgdb_contthread = NULL;
1065 } else {
1066 thread = getthread(ks->linux_regs, ks->threadid);
1067 if (!thread && ks->threadid > 0) {
1068 error_packet(remcom_out_buffer, -EINVAL);
1069 break;
1070 }
1071 kgdb_contthread = thread;
1072 }
1073 strcpy(remcom_out_buffer, "OK");
1074 break;
1075 }
1076}
1077
1078/* Handle the 'T' thread query packets */
1079static void gdb_cmd_thread(struct kgdb_state *ks)
1080{
1081 char *ptr = &remcom_in_buffer[1];
1082 struct task_struct *thread;
1083
1084 kgdb_hex2long(&ptr, &ks->threadid);
1085 thread = getthread(ks->linux_regs, ks->threadid);
1086 if (thread)
1087 strcpy(remcom_out_buffer, "OK");
1088 else
1089 error_packet(remcom_out_buffer, -EINVAL);
1090}
1091
1092/* Handle the 'z' or 'Z' breakpoint remove or set packets */
1093static void gdb_cmd_break(struct kgdb_state *ks)
1094{
1095 /*
1096 * Since GDB-5.3, it's been drafted that '0' is a software
1097 * breakpoint, '1' is a hardware breakpoint, so let's do that.
1098 */
1099 char *bpt_type = &remcom_in_buffer[1];
1100 char *ptr = &remcom_in_buffer[2];
1101 unsigned long addr;
1102 unsigned long length;
1103 int error = 0;
1104
1105 if (arch_kgdb_ops.set_hw_breakpoint && *bpt_type >= '1') {
1106 /* Unsupported */
1107 if (*bpt_type > '4')
1108 return;
1109 } else {
1110 if (*bpt_type != '0' && *bpt_type != '1')
1111 /* Unsupported. */
1112 return;
1113 }
1114
1115 /*
1116 * Test if this is a hardware breakpoint, and
1117 * if we support it:
1118 */
1119 if (*bpt_type == '1' && !(arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT))
1120 /* Unsupported. */
1121 return;
1122
1123 if (*(ptr++) != ',') {
1124 error_packet(remcom_out_buffer, -EINVAL);
1125 return;
1126 }
1127 if (!kgdb_hex2long(&ptr, &addr)) {
1128 error_packet(remcom_out_buffer, -EINVAL);
1129 return;
1130 }
1131 if (*(ptr++) != ',' ||
1132 !kgdb_hex2long(&ptr, &length)) {
1133 error_packet(remcom_out_buffer, -EINVAL);
1134 return;
1135 }
1136
1137 if (remcom_in_buffer[0] == 'Z' && *bpt_type == '0')
1138 error = kgdb_set_sw_break(addr);
1139 else if (remcom_in_buffer[0] == 'z' && *bpt_type == '0')
1140 error = kgdb_remove_sw_break(addr);
1141 else if (remcom_in_buffer[0] == 'Z')
1142 error = arch_kgdb_ops.set_hw_breakpoint(addr,
1143 (int)length, *bpt_type - '0');
1144 else if (remcom_in_buffer[0] == 'z')
1145 error = arch_kgdb_ops.remove_hw_breakpoint(addr,
1146 (int) length, *bpt_type - '0');
1147
1148 if (error == 0)
1149 strcpy(remcom_out_buffer, "OK");
1150 else
1151 error_packet(remcom_out_buffer, error);
1152}
1153
1154/* Handle the 'C' signal / exception passing packets */
1155static int gdb_cmd_exception_pass(struct kgdb_state *ks)
1156{
1157 /* C09 == pass exception
1158 * C15 == detach kgdb, pass exception
1159 */
1160 if (remcom_in_buffer[1] == '0' && remcom_in_buffer[2] == '9') {
1161
1162 ks->pass_exception = 1;
1163 remcom_in_buffer[0] = 'c';
1164
1165 } else if (remcom_in_buffer[1] == '1' && remcom_in_buffer[2] == '5') {
1166
1167 ks->pass_exception = 1;
1168 remcom_in_buffer[0] = 'D';
1169 remove_all_break();
1170 kgdb_connected = 0;
1171 return 1;
1172
1173 } else {
1174 error_packet(remcom_out_buffer, -EINVAL);
1175 return 0;
1176 }
1177
1178 /* Indicate fall through */
1179 return -1;
1180}
1181
1182/*
1183 * This function performs all gdbserial command procesing
1184 */
1185static int gdb_serial_stub(struct kgdb_state *ks)
1186{
1187 int error = 0;
1188 int tmp;
1189
1190 /* Clear the out buffer. */
1191 memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
1192
1193 if (kgdb_connected) {
1194 unsigned char thref[8];
1195 char *ptr;
1196
1197 /* Reply to host that an exception has occurred */
1198 ptr = remcom_out_buffer;
1199 *ptr++ = 'T';
1200 ptr = pack_hex_byte(ptr, ks->signo);
1201 ptr += strlen(strcpy(ptr, "thread:"));
1202 int_to_threadref(thref, shadow_pid(current->pid));
1203 ptr = pack_threadid(ptr, thref);
1204 *ptr++ = ';';
1205 put_packet(remcom_out_buffer);
1206 }
1207
1208 kgdb_usethread = kgdb_info[ks->cpu].task;
1209 ks->kgdb_usethreadid = shadow_pid(kgdb_info[ks->cpu].task->pid);
1210 ks->pass_exception = 0;
1211
1212 while (1) {
1213 error = 0;
1214
1215 /* Clear the out buffer. */
1216 memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
1217
1218 get_packet(remcom_in_buffer);
1219
1220 switch (remcom_in_buffer[0]) {
1221 case '?': /* gdbserial status */
1222 gdb_cmd_status(ks);
1223 break;
1224 case 'g': /* return the value of the CPU registers */
1225 gdb_cmd_getregs(ks);
1226 break;
1227 case 'G': /* set the value of the CPU registers - return OK */
1228 gdb_cmd_setregs(ks);
1229 break;
1230 case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
1231 gdb_cmd_memread(ks);
1232 break;
1233 case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA..AA */
1234 gdb_cmd_memwrite(ks);
1235 break;
1236 case 'X': /* XAA..AA,LLLL: Write LLLL bytes at address AA..AA */
1237 gdb_cmd_binwrite(ks);
1238 break;
1239 /* kill or detach. KGDB should treat this like a
1240 * continue.
1241 */
1242 case 'D': /* Debugger detach */
1243 case 'k': /* Debugger detach via kill */
1244 gdb_cmd_detachkill(ks);
1245 goto default_handle;
1246 case 'R': /* Reboot */
1247 if (gdb_cmd_reboot(ks))
1248 goto default_handle;
1249 break;
1250 case 'q': /* query command */
1251 gdb_cmd_query(ks);
1252 break;
1253 case 'H': /* task related */
1254 gdb_cmd_task(ks);
1255 break;
1256 case 'T': /* Query thread status */
1257 gdb_cmd_thread(ks);
1258 break;
1259 case 'z': /* Break point remove */
1260 case 'Z': /* Break point set */
1261 gdb_cmd_break(ks);
1262 break;
1263 case 'C': /* Exception passing */
1264 tmp = gdb_cmd_exception_pass(ks);
1265 if (tmp > 0)
1266 goto default_handle;
1267 if (tmp == 0)
1268 break;
1269 /* Fall through on tmp < 0 */
1270 case 'c': /* Continue packet */
1271 case 's': /* Single step packet */
1272 if (kgdb_contthread && kgdb_contthread != current) {
1273 /* Can't switch threads in kgdb */
1274 error_packet(remcom_out_buffer, -EINVAL);
1275 break;
1276 }
1277 kgdb_activate_sw_breakpoints();
1278 /* Fall through to default processing */
1279 default:
1280default_handle:
1281 error = kgdb_arch_handle_exception(ks->ex_vector,
1282 ks->signo,
1283 ks->err_code,
1284 remcom_in_buffer,
1285 remcom_out_buffer,
1286 ks->linux_regs);
1287 /*
1288 * Leave cmd processing on error, detach,
1289 * kill, continue, or single step.
1290 */
1291 if (error >= 0 || remcom_in_buffer[0] == 'D' ||
1292 remcom_in_buffer[0] == 'k') {
1293 error = 0;
1294 goto kgdb_exit;
1295 }
1296
1297 }
1298
1299 /* reply to the request */
1300 put_packet(remcom_out_buffer);
1301 }
1302
1303kgdb_exit:
1304 if (ks->pass_exception)
1305 error = 1;
1306 return error;
1307}
1308
1309static int kgdb_reenter_check(struct kgdb_state *ks)
1310{
1311 unsigned long addr;
1312
1313 if (atomic_read(&kgdb_active) != raw_smp_processor_id())
1314 return 0;
1315
1316 /* Panic on recursive debugger calls: */
1317 exception_level++;
1318 addr = kgdb_arch_pc(ks->ex_vector, ks->linux_regs);
1319 kgdb_deactivate_sw_breakpoints();
1320
1321 /*
1322 * If the break point removed ok at the place exception
1323 * occurred, try to recover and print a warning to the end
1324 * user because the user planted a breakpoint in a place that
1325 * KGDB needs in order to function.
1326 */
1327 if (kgdb_remove_sw_break(addr) == 0) {
1328 exception_level = 0;
1329 kgdb_skipexception(ks->ex_vector, ks->linux_regs);
1330 kgdb_activate_sw_breakpoints();
1331 printk(KERN_CRIT "KGDB: re-enter error: breakpoint removed %lx\n",
1332 addr);
1333 WARN_ON_ONCE(1);
1334
1335 return 1;
1336 }
1337 remove_all_break();
1338 kgdb_skipexception(ks->ex_vector, ks->linux_regs);
1339
1340 if (exception_level > 1) {
1341 dump_stack();
1342 panic("Recursive entry to debugger");
1343 }
1344
1345 printk(KERN_CRIT "KGDB: re-enter exception: ALL breakpoints killed\n");
1346 dump_stack();
1347 panic("Recursive entry to debugger");
1348
1349 return 1;
1350}
1351
1352/*
1353 * kgdb_handle_exception() - main entry point from a kernel exception
1354 *
1355 * Locking hierarchy:
1356 * interface locks, if any (begin_session)
1357 * kgdb lock (kgdb_active)
1358 */
1359int
1360kgdb_handle_exception(int evector, int signo, int ecode, struct pt_regs *regs)
1361{
1362 struct kgdb_state kgdb_var;
1363 struct kgdb_state *ks = &kgdb_var;
1364 unsigned long flags;
1365 int error = 0;
1366 int i, cpu;
1367
1368 ks->cpu = raw_smp_processor_id();
1369 ks->ex_vector = evector;
1370 ks->signo = signo;
1371 ks->ex_vector = evector;
1372 ks->err_code = ecode;
1373 ks->kgdb_usethreadid = 0;
1374 ks->linux_regs = regs;
1375
1376 if (kgdb_reenter_check(ks))
1377 return 0; /* Ouch, double exception ! */
1378
1379acquirelock:
1380 /*
1381 * Interrupts will be restored by the 'trap return' code, except when
1382 * single stepping.
1383 */
1384 local_irq_save(flags);
1385
1386 cpu = raw_smp_processor_id();
1387
1388 /*
1389 * Acquire the kgdb_active lock:
1390 */
1391 while (atomic_cmpxchg(&kgdb_active, -1, cpu) != -1)
1392 cpu_relax();
1393
1394 /*
1395 * Do not start the debugger connection on this CPU if the last
1396 * instance of the exception handler wanted to come into the
1397 * debugger on a different CPU via a single step
1398 */
1399 if (atomic_read(&kgdb_cpu_doing_single_step) != -1 &&
1400 atomic_read(&kgdb_cpu_doing_single_step) != cpu) {
1401
1402 atomic_set(&kgdb_active, -1);
1403 clocksource_touch_watchdog();
1404 local_irq_restore(flags);
1405
1406 goto acquirelock;
1407 }
1408
1409 if (!kgdb_io_ready(1)) {
1410 error = 1;
1411 goto kgdb_restore; /* No I/O connection, so resume the system */
1412 }
1413
1414 /*
1415 * Don't enter if we have hit a removed breakpoint.
1416 */
1417 if (kgdb_skipexception(ks->ex_vector, ks->linux_regs))
1418 goto kgdb_restore;
1419
1420 /* Call the I/O driver's pre_exception routine */
1421 if (kgdb_io_ops->pre_exception)
1422 kgdb_io_ops->pre_exception();
1423
1424 kgdb_info[ks->cpu].debuggerinfo = ks->linux_regs;
1425 kgdb_info[ks->cpu].task = current;
1426
1427 kgdb_disable_hw_debug(ks->linux_regs);
1428
1429 /*
1430 * Get the passive CPU lock which will hold all the non-primary
1431 * CPU in a spin state while the debugger is active
1432 */
1433 if (!kgdb_single_step || !kgdb_contthread) {
1434 for (i = 0; i < NR_CPUS; i++)
1435 atomic_set(&passive_cpu_wait[i], 1);
1436 }
1437
1438 /*
1439 * spin_lock code is good enough as a barrier so we don't
1440 * need one here:
1441 */
1442 atomic_set(&cpu_in_kgdb[ks->cpu], 1);
1443
1444#ifdef CONFIG_SMP
1445 /* Signal the other CPUs to enter kgdb_wait() */
1446 if ((!kgdb_single_step || !kgdb_contthread) && kgdb_do_roundup)
1447 kgdb_roundup_cpus(flags);
1448#endif
1449
1450 /*
1451 * Wait for the other CPUs to be notified and be waiting for us:
1452 */
1453 for_each_online_cpu(i) {
1454 while (!atomic_read(&cpu_in_kgdb[i]))
1455 cpu_relax();
1456 }
1457
1458 /*
1459 * At this point the primary processor is completely
1460 * in the debugger and all secondary CPUs are quiescent
1461 */
1462 kgdb_post_primary_code(ks->linux_regs, ks->ex_vector, ks->err_code);
1463 kgdb_deactivate_sw_breakpoints();
1464 kgdb_single_step = 0;
1465 kgdb_contthread = NULL;
1466 exception_level = 0;
1467
1468 /* Talk to debugger with gdbserial protocol */
1469 error = gdb_serial_stub(ks);
1470
1471 /* Call the I/O driver's post_exception routine */
1472 if (kgdb_io_ops->post_exception)
1473 kgdb_io_ops->post_exception();
1474
1475 kgdb_info[ks->cpu].debuggerinfo = NULL;
1476 kgdb_info[ks->cpu].task = NULL;
1477 atomic_set(&cpu_in_kgdb[ks->cpu], 0);
1478
1479 if (!kgdb_single_step || !kgdb_contthread) {
1480 for (i = NR_CPUS-1; i >= 0; i--)
1481 atomic_set(&passive_cpu_wait[i], 0);
1482 /*
1483 * Wait till all the CPUs have quit
1484 * from the debugger.
1485 */
1486 for_each_online_cpu(i) {
1487 while (atomic_read(&cpu_in_kgdb[i]))
1488 cpu_relax();
1489 }
1490 }
1491
1492kgdb_restore:
1493 /* Free kgdb_active */
1494 atomic_set(&kgdb_active, -1);
1495 clocksource_touch_watchdog();
1496 local_irq_restore(flags);
1497
1498 return error;
1499}
1500
1501int kgdb_nmicallback(int cpu, void *regs)
1502{
1503#ifdef CONFIG_SMP
1504 if (!atomic_read(&cpu_in_kgdb[cpu]) &&
1505 atomic_read(&kgdb_active) != cpu &&
1506 atomic_read(&cpu_in_kgdb[atomic_read(&kgdb_active)])) {
1507 kgdb_wait((struct pt_regs *)regs);
1508 return 0;
1509 }
1510#endif
1511 return 1;
1512}
1513
1514void kgdb_console_write(struct console *co, const char *s, unsigned count)
1515{
1516 unsigned long flags;
1517
1518 /* If we're debugging, or KGDB has not connected, don't try
1519 * and print. */
1520 if (!kgdb_connected || atomic_read(&kgdb_active) != -1)
1521 return;
1522
1523 local_irq_save(flags);
1524 kgdb_msg_write(s, count);
1525 local_irq_restore(flags);
1526}
1527
1528static struct console kgdbcons = {
1529 .name = "kgdb",
1530 .write = kgdb_console_write,
1531 .flags = CON_PRINTBUFFER | CON_ENABLED,
1532 .index = -1,
1533};
1534
1535#ifdef CONFIG_MAGIC_SYSRQ
1536static void sysrq_handle_gdb(int key, struct tty_struct *tty)
1537{
1538 if (!kgdb_io_ops) {
1539 printk(KERN_CRIT "ERROR: No KGDB I/O module available\n");
1540 return;
1541 }
1542 if (!kgdb_connected)
1543 printk(KERN_CRIT "Entering KGDB\n");
1544
1545 kgdb_breakpoint();
1546}
1547
1548static struct sysrq_key_op sysrq_gdb_op = {
1549 .handler = sysrq_handle_gdb,
1550 .help_msg = "Gdb",
1551 .action_msg = "GDB",
1552};
1553#endif
1554
1555static void kgdb_register_callbacks(void)
1556{
1557 if (!kgdb_io_module_registered) {
1558 kgdb_io_module_registered = 1;
1559 kgdb_arch_init();
1560#ifdef CONFIG_MAGIC_SYSRQ
1561 register_sysrq_key('g', &sysrq_gdb_op);
1562#endif
1563 if (kgdb_use_con && !kgdb_con_registered) {
1564 register_console(&kgdbcons);
1565 kgdb_con_registered = 1;
1566 }
1567 }
1568}
1569
1570static void kgdb_unregister_callbacks(void)
1571{
1572 /*
1573 * When this routine is called KGDB should unregister from the
1574 * panic handler and clean up, making sure it is not handling any
1575 * break exceptions at the time.
1576 */
1577 if (kgdb_io_module_registered) {
1578 kgdb_io_module_registered = 0;
1579 kgdb_arch_exit();
1580#ifdef CONFIG_MAGIC_SYSRQ
1581 unregister_sysrq_key('g', &sysrq_gdb_op);
1582#endif
1583 if (kgdb_con_registered) {
1584 unregister_console(&kgdbcons);
1585 kgdb_con_registered = 0;
1586 }
1587 }
1588}
1589
1590static void kgdb_initial_breakpoint(void)
1591{
1592 kgdb_break_asap = 0;
1593
1594 printk(KERN_CRIT "kgdb: Waiting for connection from remote gdb...\n");
1595 kgdb_breakpoint();
1596}
1597
1598/**
1599 * kgdb_register_io_module - register KGDB IO module
1600 * @new_kgdb_io_ops: the io ops vector
1601 *
1602 * Register it with the KGDB core.
1603 */
1604int kgdb_register_io_module(struct kgdb_io *new_kgdb_io_ops)
1605{
1606 int err;
1607
1608 spin_lock(&kgdb_registration_lock);
1609
1610 if (kgdb_io_ops) {
1611 spin_unlock(&kgdb_registration_lock);
1612
1613 printk(KERN_ERR "kgdb: Another I/O driver is already "
1614 "registered with KGDB.\n");
1615 return -EBUSY;
1616 }
1617
1618 if (new_kgdb_io_ops->init) {
1619 err = new_kgdb_io_ops->init();
1620 if (err) {
1621 spin_unlock(&kgdb_registration_lock);
1622 return err;
1623 }
1624 }
1625
1626 kgdb_io_ops = new_kgdb_io_ops;
1627
1628 spin_unlock(&kgdb_registration_lock);
1629
1630 printk(KERN_INFO "kgdb: Registered I/O driver %s.\n",
1631 new_kgdb_io_ops->name);
1632
1633 /* Arm KGDB now. */
1634 kgdb_register_callbacks();
1635
1636 if (kgdb_break_asap)
1637 kgdb_initial_breakpoint();
1638
1639 return 0;
1640}
1641EXPORT_SYMBOL_GPL(kgdb_register_io_module);
1642
1643/**
1644 * kkgdb_unregister_io_module - unregister KGDB IO module
1645 * @old_kgdb_io_ops: the io ops vector
1646 *
1647 * Unregister it with the KGDB core.
1648 */
1649void kgdb_unregister_io_module(struct kgdb_io *old_kgdb_io_ops)
1650{
1651 BUG_ON(kgdb_connected);
1652
1653 /*
1654 * KGDB is no longer able to communicate out, so
1655 * unregister our callbacks and reset state.
1656 */
1657 kgdb_unregister_callbacks();
1658
1659 spin_lock(&kgdb_registration_lock);
1660
1661 WARN_ON_ONCE(kgdb_io_ops != old_kgdb_io_ops);
1662 kgdb_io_ops = NULL;
1663
1664 spin_unlock(&kgdb_registration_lock);
1665
1666 printk(KERN_INFO
1667 "kgdb: Unregistered I/O driver %s, debugger disabled.\n",
1668 old_kgdb_io_ops->name);
1669}
1670EXPORT_SYMBOL_GPL(kgdb_unregister_io_module);
1671
1672/**
1673 * kgdb_breakpoint - generate breakpoint exception
1674 *
1675 * This function will generate a breakpoint exception. It is used at the
1676 * beginning of a program to sync up with a debugger and can be used
1677 * otherwise as a quick means to stop program execution and "break" into
1678 * the debugger.
1679 */
1680void kgdb_breakpoint(void)
1681{
1682 atomic_set(&kgdb_setting_breakpoint, 1);
1683 wmb(); /* Sync point before breakpoint */
1684 arch_kgdb_breakpoint();
1685 wmb(); /* Sync point after breakpoint */
1686 atomic_set(&kgdb_setting_breakpoint, 0);
1687}
1688EXPORT_SYMBOL_GPL(kgdb_breakpoint);
1689
1690static int __init opt_kgdb_wait(char *str)
1691{
1692 kgdb_break_asap = 1;
1693
1694 if (kgdb_io_module_registered)
1695 kgdb_initial_breakpoint();
1696
1697 return 0;
1698}
1699
1700early_param("kgdbwait", opt_kgdb_wait);
diff --git a/kernel/time/clocksource.c b/kernel/time/clocksource.c
index 7f60097d443a..f61402b1f2d0 100644
--- a/kernel/time/clocksource.c
+++ b/kernel/time/clocksource.c
@@ -222,6 +222,18 @@ void clocksource_resume(void)
222} 222}
223 223
224/** 224/**
225 * clocksource_touch_watchdog - Update watchdog
226 *
227 * Update the watchdog after exception contexts such as kgdb so as not
228 * to incorrectly trip the watchdog.
229 *
230 */
231void clocksource_touch_watchdog(void)
232{
233 clocksource_resume_watchdog();
234}
235
236/**
225 * clocksource_get_next - Returns the selected clocksource 237 * clocksource_get_next - Returns the selected clocksource
226 * 238 *
227 */ 239 */
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index 80db357b0a42..aaba784332e0 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -612,3 +612,5 @@ config PROVIDE_OHCI1394_DMA_INIT
612 See Documentation/debugging-via-ohci1394.txt for more information. 612 See Documentation/debugging-via-ohci1394.txt for more information.
613 613
614source "samples/Kconfig" 614source "samples/Kconfig"
615
616source "lib/Kconfig.kgdb"
diff --git a/lib/Kconfig.kgdb b/lib/Kconfig.kgdb
new file mode 100644
index 000000000000..f2e01ac5ab09
--- /dev/null
+++ b/lib/Kconfig.kgdb
@@ -0,0 +1,58 @@
1
2menuconfig KGDB
3 bool "KGDB: kernel debugging with remote gdb"
4 select FRAME_POINTER
5 depends on HAVE_ARCH_KGDB
6 depends on DEBUG_KERNEL && EXPERIMENTAL
7 help
8 If you say Y here, it will be possible to remotely debug the
9 kernel using gdb. Documentation of kernel debugger is available
10 at http://kgdb.sourceforge.net as well as in DocBook form
11 in Documentation/DocBook/. If unsure, say N.
12
13config HAVE_ARCH_KGDB_SHADOW_INFO
14 bool
15
16config HAVE_ARCH_KGDB
17 bool
18
19config KGDB_SERIAL_CONSOLE
20 tristate "KGDB: use kgdb over the serial console"
21 depends on KGDB
22 select CONSOLE_POLL
23 select MAGIC_SYSRQ
24 default y
25 help
26 Share a serial console with kgdb. Sysrq-g must be used
27 to break in initially.
28
29config KGDB_TESTS
30 bool "KGDB: internal test suite"
31 depends on KGDB
32 default n
33 help
34 This is a kgdb I/O module specifically designed to test
35 kgdb's internal functions. This kgdb I/O module is
36 intended to for the development of new kgdb stubs
37 as well as regression testing the kgdb internals.
38 See the drivers/misc/kgdbts.c for the details about
39 the tests. The most basic of this I/O module is to boot
40 a kernel boot arguments "kgdbwait kgdbts=V1F100"
41
42config KGDB_TESTS_ON_BOOT
43 bool "KGDB: Run tests on boot"
44 depends on KGDB_TESTS
45 default n
46 help
47 Run the kgdb tests on boot up automatically without the need
48 to pass in a kernel parameter
49
50config KGDB_TESTS_BOOT_STRING
51 string "KGDB: which internal kgdb tests to run"
52 depends on KGDB_TESTS_ON_BOOT
53 default "V1F100"
54 help
55 This is the command string to send the kgdb test suite on
56 boot. See the drivers/misc/kgdbts.c for detailed
57 information about other strings you could use beyond the
58 default of V1F100.
diff --git a/mm/Makefile b/mm/Makefile
index a5b0dd93427a..18c143b3c46c 100644
--- a/mm/Makefile
+++ b/mm/Makefile
@@ -8,7 +8,7 @@ mmu-$(CONFIG_MMU) := fremap.o highmem.o madvise.o memory.o mincore.o \
8 vmalloc.o 8 vmalloc.o
9 9
10obj-y := bootmem.o filemap.o mempool.o oom_kill.o fadvise.o \ 10obj-y := bootmem.o filemap.o mempool.o oom_kill.o fadvise.o \
11 page_alloc.o page-writeback.o pdflush.o \ 11 maccess.o page_alloc.o page-writeback.o pdflush.o \
12 readahead.o swap.o truncate.o vmscan.o \ 12 readahead.o swap.o truncate.o vmscan.o \
13 prio_tree.o util.o mmzone.o vmstat.o backing-dev.o \ 13 prio_tree.o util.o mmzone.o vmstat.o backing-dev.o \
14 page_isolation.o $(mmu-y) 14 page_isolation.o $(mmu-y)
diff --git a/mm/maccess.c b/mm/maccess.c
new file mode 100644
index 000000000000..ac40796cfb15
--- /dev/null
+++ b/mm/maccess.c
@@ -0,0 +1,55 @@
1/*
2 * Access kernel memory without faulting.
3 */
4#include <linux/uaccess.h>
5#include <linux/module.h>
6#include <linux/mm.h>
7
8/**
9 * probe_kernel_read(): safely attempt to read from a location
10 * @dst: pointer to the buffer that shall take the data
11 * @src: address to read from
12 * @size: size of the data chunk
13 *
14 * Safely read from address @src to the buffer at @dst. If a kernel fault
15 * happens, handle that and return -EFAULT.
16 */
17long probe_kernel_read(void *dst, void *src, size_t size)
18{
19 long ret;
20 mm_segment_t old_fs = get_fs();
21
22 set_fs(KERNEL_DS);
23 pagefault_disable();
24 ret = __copy_from_user_inatomic(dst,
25 (__force const void __user *)src, size);
26 pagefault_enable();
27 set_fs(old_fs);
28
29 return ret ? -EFAULT : 0;
30}
31EXPORT_SYMBOL_GPL(probe_kernel_read);
32
33/**
34 * probe_kernel_write(): safely attempt to write to a location
35 * @dst: address to write to
36 * @src: pointer to the data that shall be written
37 * @size: size of the data chunk
38 *
39 * Safely write to address @dst from the buffer at @src. If a kernel fault
40 * happens, handle that and return -EFAULT.
41 */
42long probe_kernel_write(void *dst, void *src, size_t size)
43{
44 long ret;
45 mm_segment_t old_fs = get_fs();
46
47 set_fs(KERNEL_DS);
48 pagefault_disable();
49 ret = __copy_to_user_inatomic((__force void __user *)dst, src, size);
50 pagefault_enable();
51 set_fs(old_fs);
52
53 return ret ? -EFAULT : 0;
54}
55EXPORT_SYMBOL_GPL(probe_kernel_write);