Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]

Diff markup

Differences between /linux/kernel/kgdb.c (Version 2.6.31.13) and /linux/kernel/kgdb.c (Version 2.6.11.8)


  1 /*                                                  1 
  2  * KGDB stub.                                     
  3  *                                                
  4  * Maintainer: Jason Wessel <jason.wessel@wind    
  5  *                                                
  6  * Copyright (C) 2000-2001 VERITAS Software Co    
  7  * Copyright (C) 2002-2004 Timesys Corporation    
  8  * Copyright (C) 2003-2004 Amit S. Kale <amitk    
  9  * Copyright (C) 2004 Pavel Machek <pavel@suse    
 10  * Copyright (C) 2004-2006 Tom Rini <trini@ker    
 11  * Copyright (C) 2004-2006 LinSysSoft Technolo    
 12  * Copyright (C) 2005-2008 Wind River Systems,    
 13  * Copyright (C) 2007 MontaVista Software, Inc    
 14  * Copyright (C) 2008 Red Hat, Inc., Ingo Moln    
 15  *                                                
 16  * Contributors at various stages not listed a    
 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 En    
 21  *  Jim Kingdon, Cygnus Support.                  
 22  *                                                
 23  * Original KGDB stub: David Grothe <dave@gcom    
 24  * Tigran Aivazian <tigran@sco.com>               
 25  *                                                
 26  * This file is licensed under the terms of th    
 27  * version 2. This program is licensed "as is"    
 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 #include <asm/unaligned.h>                        
 56                                                   
 57 static int kgdb_break_asap;                       
 58                                                   
 59 #define KGDB_MAX_THREAD_QUERY 17                  
 60 struct kgdb_state {                               
 61         int                     ex_vector;        
 62         int                     signo;            
 63         int                     err_code;         
 64         int                     cpu;              
 65         int                     pass_exception    
 66         unsigned long           thr_query;        
 67         unsigned long           threadid;         
 68         long                    kgdb_usethread    
 69         struct pt_regs          *linux_regs;      
 70 };                                                
 71                                                   
 72 static struct debuggerinfo_struct {               
 73         void                    *debuggerinfo;    
 74         struct task_struct      *task;            
 75 } kgdb_info[NR_CPUS];                             
 76                                                   
 77 /**                                               
 78  * kgdb_connected - Is a host GDB connected to    
 79  */                                               
 80 int                             kgdb_connected    
 81 EXPORT_SYMBOL_GPL(kgdb_connected);                
 82                                                   
 83 /* All the KGDB handlers are installed */         
 84 static int                      kgdb_io_module    
 85                                                   
 86 /* Guard for recursive entry */                   
 87 static int                      exception_leve    
 88                                                   
 89 static struct kgdb_io           *kgdb_io_ops;     
 90 static DEFINE_SPINLOCK(kgdb_registration_lock)    
 91                                                   
 92 /* kgdb console driver is loaded */               
 93 static int kgdb_con_registered;                   
 94 /* determine if kgdb console output should be     
 95 static int kgdb_use_con;                          
 96                                                   
 97 static int __init opt_kgdb_con(char *str)         
 98 {                                                 
 99         kgdb_use_con = 1;                         
100         return 0;                                 
101 }                                                 
102                                                   
103 early_param("kgdbcon", opt_kgdb_con);             
104                                                   
105 module_param(kgdb_use_con, int, 0644);            
106                                                   
107 /*                                                
108  * Holds information about breakpoints in a ke    
109  * added and removed by gdb.                      
110  */                                               
111 static struct kgdb_bkpt         kgdb_break[KGD    
112         [0 ... KGDB_MAX_BREAKPOINTS-1] = { .st    
113 };                                                
114                                                   
115 /*                                                
116  * The CPU# of the active CPU, or -1 if none:     
117  */                                               
118 atomic_t                        kgdb_active =     
119                                                   
120 /*                                                
121  * We use NR_CPUs not PERCPU, in case kgdb is     
122  * bootup code (which might not have percpu se    
123  */                                               
124 static atomic_t                 passive_cpu_wa    
125 static atomic_t                 cpu_in_kgdb[NR    
126 atomic_t                        kgdb_setting_b    
127                                                   
128 struct task_struct              *kgdb_usethrea    
129 struct task_struct              *kgdb_contthre    
130                                                   
131 int                             kgdb_single_st    
132                                                   
133 /* Our I/O buffers. */                            
134 static char                     remcom_in_buff    
135 static char                     remcom_out_buf    
136                                                   
137 /* Storage for the registers, in GDB format. *    
138 static unsigned long            gdb_regs[(NUMR    
139                                         sizeof    
140                                         sizeof    
141                                                   
142 /* to keep track of the CPU which is doing the    
143 atomic_t                        kgdb_cpu_doing    
144                                                   
145 /*                                                
146  * If you are debugging a problem where roundu    
147  * all other CPUs) is a problem [this should b    
148  * then use the nokgdbroundup option to avoid     
149  * the other CPUs might interfere with your de    
150  * use this with care:                            
151  */                                               
152 static int kgdb_do_roundup = 1;                   
153                                                   
154 static int __init opt_nokgdbroundup(char *str)    
155 {                                                 
156         kgdb_do_roundup = 0;                      
157                                                   
158         return 0;                                 
159 }                                                 
160                                                   
161 early_param("nokgdbroundup", opt_nokgdbroundup    
162                                                   
163 /*                                                
164  * Finally, some KGDB code :-)                    
165  */                                               
166                                                   
167 /*                                                
168  * Weak aliases for breakpoint management,        
169  * can be overriden by architectures when need    
170  */                                               
171 int __weak kgdb_arch_set_breakpoint(unsigned l    
172 {                                                 
173         int err;                                  
174                                                   
175         err = probe_kernel_read(saved_instr, (    
176         if (err)                                  
177                 return err;                       
178                                                   
179         return probe_kernel_write((char *)addr    
180                                   BREAK_INSTR_    
181 }                                                 
182                                                   
183 int __weak kgdb_arch_remove_breakpoint(unsigne    
184 {                                                 
185         return probe_kernel_write((char *)addr    
186                                   (char *)bund    
187 }                                                 
188                                                   
189 int __weak kgdb_validate_break_address(unsigne    
190 {                                                 
191         char tmp_variable[BREAK_INSTR_SIZE];      
192         int err;                                  
193         /* Validate setting the breakpoint and    
194          * remove fails, the kernel needs to e    
195          * are deep trouble not being able to     
196          * found them.                            
197          */                                       
198         err = kgdb_arch_set_breakpoint(addr, t    
199         if (err)                                  
200                 return err;                       
201         err = kgdb_arch_remove_breakpoint(addr    
202         if (err)                                  
203                 printk(KERN_ERR "KGDB: Critica    
204                    "memory destroyed at: %lx",    
205         return err;                               
206 }                                                 
207                                                   
208 unsigned long __weak kgdb_arch_pc(int exceptio    
209 {                                                 
210         return instruction_pointer(regs);         
211 }                                                 
212                                                   
213 int __weak kgdb_arch_init(void)                   
214 {                                                 
215         return 0;                                 
216 }                                                 
217                                                   
218 int __weak kgdb_skipexception(int exception, s    
219 {                                                 
220         return 0;                                 
221 }                                                 
222                                                   
223 void __weak                                       
224 kgdb_post_primary_code(struct pt_regs *regs, i    
225 {                                                 
226         return;                                   
227 }                                                 
228                                                   
229 /**                                               
230  *      kgdb_disable_hw_debug - Disable hardwa    
231  *      @regs: Current &struct pt_regs.           
232  *                                                
233  *      This function will be called if the pa    
234  *      disable hardware debugging while it is    
235  *      handling exception.                       
236  */                                               
237 void __weak kgdb_disable_hw_debug(struct pt_re    
238 {                                                 
239 }                                                 
240                                                   
241 /*                                                
242  * GDB remote protocol parser:                    
243  */                                               
244                                                   
245 static int hex(char ch)                           
246 {                                                 
247         if ((ch >= 'a') && (ch <= 'f'))           
248                 return ch - 'a' + 10;             
249         if ((ch >= '') && (ch <= '9'))            
250                 return ch - '';                   
251         if ((ch >= 'A') && (ch <= 'F'))           
252                 return ch - 'A' + 10;             
253         return -1;                                
254 }                                                 
255                                                   
256 /* scan for the sequence $<data>#<checksum> */    
257 static void get_packet(char *buffer)              
258 {                                                 
259         unsigned char checksum;                   
260         unsigned char xmitcsum;                   
261         int count;                                
262         char ch;                                  
263                                                   
264         do {                                      
265                 /*                                
266                  * Spin and wait around for th    
267                  * other characters:              
268                  */                               
269                 while ((ch = (kgdb_io_ops->rea    
270                         /* nothing */;            
271                                                   
272                 kgdb_connected = 1;               
273                 checksum = 0;                     
274                 xmitcsum = -1;                    
275                                                   
276                 count = 0;                        
277                                                   
278                 /*                                
279                  * now, read until a # or end     
280                  */                               
281                 while (count < (BUFMAX - 1)) {    
282                         ch = kgdb_io_ops->read    
283                         if (ch == '#')            
284                                 break;            
285                         checksum = checksum +     
286                         buffer[count] = ch;       
287                         count = count + 1;        
288                 }                                 
289                 buffer[count] = 0;                
290                                                   
291                 if (ch == '#') {                  
292                         xmitcsum = hex(kgdb_io    
293                         xmitcsum += hex(kgdb_i    
294                                                   
295                         if (checksum != xmitcs    
296                                 /* failed chec    
297                                 kgdb_io_ops->w    
298                         else                      
299                                 /* successful     
300                                 kgdb_io_ops->w    
301                         if (kgdb_io_ops->flush    
302                                 kgdb_io_ops->f    
303                 }                                 
304         } while (checksum != xmitcsum);           
305 }                                                 
306                                                   
307 /*                                                
308  * Send the packet in buffer.                     
309  * Check for gdb connection if asked for.         
310  */                                               
311 static void put_packet(char *buffer)              
312 {                                                 
313         unsigned char checksum;                   
314         int count;                                
315         char ch;                                  
316                                                   
317         /*                                        
318          * $<packet info>#<checksum>.             
319          */                                       
320         while (1) {                               
321                 kgdb_io_ops->write_char('$');     
322                 checksum = 0;                     
323                 count = 0;                        
324                                                   
325                 while ((ch = buffer[count])) {    
326                         kgdb_io_ops->write_cha    
327                         checksum += ch;           
328                         count++;                  
329                 }                                 
330                                                   
331                 kgdb_io_ops->write_char('#');     
332                 kgdb_io_ops->write_char(hex_as    
333                 kgdb_io_ops->write_char(hex_as    
334                 if (kgdb_io_ops->flush)           
335                         kgdb_io_ops->flush();     
336                                                   
337                 /* Now see what we get in repl    
338                 ch = kgdb_io_ops->read_char();    
339                                                   
340                 if (ch == 3)                      
341                         ch = kgdb_io_ops->read    
342                                                   
343                 /* If we get an ACK, we are do    
344                 if (ch == '+')                    
345                         return;                   
346                                                   
347                 /*                                
348                  * If we get the start of anot    
349                  * that GDB is attempting to r    
350                  * the packet being sent, and     
351                  * packet.                        
352                  */                               
353                 if (ch == '$') {                  
354                         kgdb_io_ops->write_cha    
355                         if (kgdb_io_ops->flush    
356                                 kgdb_io_ops->f    
357                         return;                   
358                 }                                 
359         }                                         
360 }                                                 
361                                                   
362 /*                                                
363  * Convert the memory pointed to by mem into h    
364  * Return a pointer to the last char put in bu    
365  */                                               
366 int kgdb_mem2hex(char *mem, char *buf, int cou    
367 {                                                 
368         char *tmp;                                
369         int err;                                  
370                                                   
371         /*                                        
372          * We use the upper half of buf as an     
373          * raw memory copy.  Hex conversion wi    
374          */                                       
375         tmp = buf + count;                        
376                                                   
377         err = probe_kernel_read(tmp, mem, coun    
378         if (!err) {                               
379                 while (count > 0) {               
380                         buf = pack_hex_byte(bu    
381                         tmp++;                    
382                         count--;                  
383                 }                                 
384                                                   
385                 *buf = 0;                         
386         }                                         
387                                                   
388         return err;                               
389 }                                                 
390                                                   
391 /*                                                
392  * Copy the binary array pointed to by buf int    
393  * 0x7d escaped with 0x7d.  Return a pointer t    
394  * the last byte written.                         
395  */                                               
396 static int kgdb_ebin2mem(char *buf, char *mem,    
397 {                                                 
398         int err = 0;                              
399         char c;                                   
400                                                   
401         while (count-- > 0) {                     
402                 c = *buf++;                       
403                 if (c == 0x7d)                    
404                         c = *buf++ ^ 0x20;        
405                                                   
406                 err = probe_kernel_write(mem,     
407                 if (err)                          
408                         break;                    
409                                                   
410                 mem++;                            
411         }                                         
412                                                   
413         return err;                               
414 }                                                 
415                                                   
416 /*                                                
417  * Convert the hex array pointed to by buf int    
418  * Return a pointer to the character AFTER the    
419  * May return an error.                           
420  */                                               
421 int kgdb_hex2mem(char *buf, char *mem, int cou    
422 {                                                 
423         char *tmp_raw;                            
424         char *tmp_hex;                            
425                                                   
426         /*                                        
427          * We use the upper half of buf as an     
428          * raw memory that is converted from h    
429          */                                       
430         tmp_raw = buf + count * 2;                
431                                                   
432         tmp_hex = tmp_raw - 1;                    
433         while (tmp_hex >= buf) {                  
434                 tmp_raw--;                        
435                 *tmp_raw = hex(*tmp_hex--);       
436                 *tmp_raw |= hex(*tmp_hex--) <<    
437         }                                         
438                                                   
439         return probe_kernel_write(mem, tmp_raw    
440 }                                                 
441                                                   
442 /*                                                
443  * While we find nice hex chars, build a long_    
444  * Return number of chars processed.              
445  */                                               
446 int kgdb_hex2long(char **ptr, unsigned long *l    
447 {                                                 
448         int hex_val;                              
449         int num = 0;                              
450         int negate = 0;                           
451                                                   
452         *long_val = 0;                            
453                                                   
454         if (**ptr == '-') {                       
455                 negate = 1;                       
456                 (*ptr)++;                         
457         }                                         
458         while (**ptr) {                           
459                 hex_val = hex(**ptr);             
460                 if (hex_val < 0)                  
461                         break;                    
462                                                   
463                 *long_val = (*long_val << 4) |    
464                 num++;                            
465                 (*ptr)++;                         
466         }                                         
467                                                   
468         if (negate)                               
469                 *long_val = -*long_val;           
470                                                   
471         return num;                               
472 }                                                 
473                                                   
474 /* Write memory due to an 'M' or 'X' packet. *    
475 static int write_mem_msg(int binary)              
476 {                                                 
477         char *ptr = &remcom_in_buffer[1];         
478         unsigned long addr;                       
479         unsigned long length;                     
480         int err;                                  
481                                                   
482         if (kgdb_hex2long(&ptr, &addr) > 0 &&     
483             kgdb_hex2long(&ptr, &length) > 0 &    
484                 if (binary)                       
485                         err = kgdb_ebin2mem(pt    
486                 else                              
487                         err = kgdb_hex2mem(ptr    
488                 if (err)                          
489                         return err;               
490                 if (CACHE_FLUSH_IS_SAFE)          
491                         flush_icache_range(add    
492                 return 0;                         
493         }                                         
494                                                   
495         return -EINVAL;                           
496 }                                                 
497                                                   
498 static void error_packet(char *pkt, int error)    
499 {                                                 
500         error = -error;                           
501         pkt[0] = 'E';                             
502         pkt[1] = hex_asc[(error / 10)];           
503         pkt[2] = hex_asc[(error % 10)];           
504         pkt[3] = '\0';                            
505 }                                                 
506                                                   
507 /*                                                
508  * Thread ID accessors. We represent a flat TI    
509  * the per CPU idle threads (which under Linux    
510  * remapped to negative TIDs.                     
511  */                                               
512                                                   
513 #define BUF_THREAD_ID_SIZE      16                
514                                                   
515 static char *pack_threadid(char *pkt, unsigned    
516 {                                                 
517         char *limit;                              
518                                                   
519         limit = pkt + BUF_THREAD_ID_SIZE;         
520         while (pkt < limit)                       
521                 pkt = pack_hex_byte(pkt, *id++    
522                                                   
523         return pkt;                               
524 }                                                 
525                                                   
526 static void int_to_threadref(unsigned char *id    
527 {                                                 
528         unsigned char *scan;                      
529         int i = 4;                                
530                                                   
531         scan = (unsigned char *)id;               
532         while (i--)                               
533                 *scan++ = 0;                      
534         put_unaligned_be32(value, scan);          
535 }                                                 
536                                                   
537 static struct task_struct *getthread(struct pt    
538 {                                                 
539         /*                                        
540          * Non-positive TIDs are remapped to t    
541          */                                       
542         if (tid == 0 || tid == -1)                
543                 tid = -atomic_read(&kgdb_activ    
544         if (tid < 0) {                            
545                 if (kgdb_info[-tid - 2].task)     
546                         return kgdb_info[-tid     
547                 else                              
548                         return idle_task(-tid     
549         }                                         
550                                                   
551         /*                                        
552          * find_task_by_pid_ns() does not take    
553          * but is nicely RCU locked - hence is    
554          * thing to use:                          
555          */                                       
556         return find_task_by_pid_ns(tid, &init_    
557 }                                                 
558                                                   
559 /*                                                
560  * CPU debug state control:                       
561  */                                               
562                                                   
563 #ifdef CONFIG_SMP                                 
564 static void kgdb_wait(struct pt_regs *regs)       
565 {                                                 
566         unsigned long flags;                      
567         int cpu;                                  
568                                                   
569         local_irq_save(flags);                    
570         cpu = raw_smp_processor_id();             
571         kgdb_info[cpu].debuggerinfo = regs;       
572         kgdb_info[cpu].task = current;            
573         /*                                        
574          * Make sure the above info reaches th    
575          * our cpu_in_kgdb[] flag setting does    
576          */                                       
577         smp_wmb();                                
578         atomic_set(&cpu_in_kgdb[cpu], 1);         
579                                                   
580         /* Wait till primary CPU is done with     
581         while (atomic_read(&passive_cpu_wait[c    
582                 cpu_relax();                      
583                                                   
584         kgdb_info[cpu].debuggerinfo = NULL;       
585         kgdb_info[cpu].task = NULL;               
586                                                   
587         /* fix up hardware debug registers on     
588         if (arch_kgdb_ops.correct_hw_break)       
589                 arch_kgdb_ops.correct_hw_break    
590                                                   
591         /* Signal the primary CPU that we are     
592         atomic_set(&cpu_in_kgdb[cpu], 0);         
593         touch_softlockup_watchdog();              
594         clocksource_touch_watchdog();             
595         local_irq_restore(flags);                 
596 }                                                 
597 #endif                                            
598                                                   
599 /*                                                
600  * Some architectures need cache flushes when     
601  * breakpoint:                                    
602  */                                               
603 static void kgdb_flush_swbreak_addr(unsigned l    
604 {                                                 
605         if (!CACHE_FLUSH_IS_SAFE)                 
606                 return;                           
607                                                   
608         if (current->mm && current->mm->mmap_c    
609                 flush_cache_range(current->mm-    
610                                   addr, addr +    
611         }                                         
612         /* Force flush instruction cache if it    
613         flush_icache_range(addr, addr + BREAK_    
614 }                                                 
615                                                   
616 /*                                                
617  * SW breakpoint management:                      
618  */                                               
619 static int kgdb_activate_sw_breakpoints(void)     
620 {                                                 
621         unsigned long addr;                       
622         int error = 0;                            
623         int i;                                    
624                                                   
625         for (i = 0; i < KGDB_MAX_BREAKPOINTS;     
626                 if (kgdb_break[i].state != BP_    
627                         continue;                 
628                                                   
629                 addr = kgdb_break[i].bpt_addr;    
630                 error = kgdb_arch_set_breakpoi    
631                                 kgdb_break[i].    
632                 if (error)                        
633                         return error;             
634                                                   
635                 kgdb_flush_swbreak_addr(addr);    
636                 kgdb_break[i].state = BP_ACTIV    
637         }                                         
638         return 0;                                 
639 }                                                 
640                                                   
641 static int kgdb_set_sw_break(unsigned long add    
642 {                                                 
643         int err = kgdb_validate_break_address(    
644         int breakno = -1;                         
645         int i;                                    
646                                                   
647         if (err)                                  
648                 return err;                       
649                                                   
650         for (i = 0; i < KGDB_MAX_BREAKPOINTS;     
651                 if ((kgdb_break[i].state == BP    
652                                         (kgdb_    
653                         return -EEXIST;           
654         }                                         
655         for (i = 0; i < KGDB_MAX_BREAKPOINTS;     
656                 if (kgdb_break[i].state == BP_    
657                                         kgdb_b    
658                         breakno = i;              
659                         break;                    
660                 }                                 
661         }                                         
662                                                   
663         if (breakno == -1) {                      
664                 for (i = 0; i < KGDB_MAX_BREAK    
665                         if (kgdb_break[i].stat    
666                                 breakno = i;      
667                                 break;            
668                         }                         
669                 }                                 
670         }                                         
671                                                   
672         if (breakno == -1)                        
673                 return -E2BIG;                    
674                                                   
675         kgdb_break[breakno].state = BP_SET;       
676         kgdb_break[breakno].type = BP_BREAKPOI    
677         kgdb_break[breakno].bpt_addr = addr;      
678                                                   
679         return 0;                                 
680 }                                                 
681                                                   
682 static int kgdb_deactivate_sw_breakpoints(void    
683 {                                                 
684         unsigned long addr;                       
685         int error = 0;                            
686         int i;                                    
687                                                   
688         for (i = 0; i < KGDB_MAX_BREAKPOINTS;     
689                 if (kgdb_break[i].state != BP_    
690                         continue;                 
691                 addr = kgdb_break[i].bpt_addr;    
692                 error = kgdb_arch_remove_break    
693                                         kgdb_b    
694                 if (error)                        
695                         return error;             
696                                                   
697                 kgdb_flush_swbreak_addr(addr);    
698                 kgdb_break[i].state = BP_SET;     
699         }                                         
700         return 0;                                 
701 }                                                 
702                                                   
703 static int kgdb_remove_sw_break(unsigned long     
704 {                                                 
705         int i;                                    
706                                                   
707         for (i = 0; i < KGDB_MAX_BREAKPOINTS;     
708                 if ((kgdb_break[i].state == BP    
709                                 (kgdb_break[i]    
710                         kgdb_break[i].state =     
711                         return 0;                 
712                 }                                 
713         }                                         
714         return -ENOENT;                           
715 }                                                 
716                                                   
717 int kgdb_isremovedbreak(unsigned long addr)       
718 {                                                 
719         int i;                                    
720                                                   
721         for (i = 0; i < KGDB_MAX_BREAKPOINTS;     
722                 if ((kgdb_break[i].state == BP    
723                                         (kgdb_    
724                         return 1;                 
725         }                                         
726         return 0;                                 
727 }                                                 
728                                                   
729 static int remove_all_break(void)                 
730 {                                                 
731         unsigned long addr;                       
732         int error;                                
733         int i;                                    
734                                                   
735         /* Clear memory breakpoints. */           
736         for (i = 0; i < KGDB_MAX_BREAKPOINTS;     
737                 if (kgdb_break[i].state != BP_    
738                         goto setundefined;        
739                 addr = kgdb_break[i].bpt_addr;    
740                 error = kgdb_arch_remove_break    
741                                 kgdb_break[i].    
742                 if (error)                        
743                         printk(KERN_ERR "KGDB:    
744                            addr);                 
745 setundefined:                                     
746                 kgdb_break[i].state = BP_UNDEF    
747         }                                         
748                                                   
749         /* Clear hardware breakpoints. */         
750         if (arch_kgdb_ops.remove_all_hw_break)    
751                 arch_kgdb_ops.remove_all_hw_br    
752                                                   
753         return 0;                                 
754 }                                                 
755                                                   
756 /*                                                
757  * Remap normal tasks to their real PID,          
758  * CPU shadow threads are mapped to -CPU - 2      
759  */                                               
760 static inline int shadow_pid(int realpid)         
761 {                                                 
762         if (realpid)                              
763                 return realpid;                   
764                                                   
765         return -raw_smp_processor_id() - 2;       
766 }                                                 
767                                                   
768 static char gdbmsgbuf[BUFMAX + 1];                
769                                                   
770 static void kgdb_msg_write(const char *s, int     
771 {                                                 
772         char *bufptr;                             
773         int wcount;                               
774         int i;                                    
775                                                   
776         /* 'O'utput */                            
777         gdbmsgbuf[0] = 'O';                       
778                                                   
779         /* Fill and send buffers... */            
780         while (len > 0) {                         
781                 bufptr = gdbmsgbuf + 1;           
782                                                   
783                 /* Calculate how many this tim    
784                 if ((len << 1) > (BUFMAX - 2))    
785                         wcount = (BUFMAX - 2)     
786                 else                              
787                         wcount = len;             
788                                                   
789                 /* Pack in hex chars */           
790                 for (i = 0; i < wcount; i++)      
791                         bufptr = pack_hex_byte    
792                 *bufptr = '\0';                   
793                                                   
794                 /* Move up */                     
795                 s += wcount;                      
796                 len -= wcount;                    
797                                                   
798                 /* Write packet */                
799                 put_packet(gdbmsgbuf);            
800         }                                         
801 }                                                 
802                                                   
803 /*                                                
804  * Return true if there is a valid kgdb I/O mo    
805  * debugger is attached a message can be print    
806  * waiting for the debugger to attach.            
807  *                                                
808  * The print_wait argument is only to be true     
809  * the core kgdb_handle_exception, because it     
810  * debugger to attach.                            
811  */                                               
812 static int kgdb_io_ready(int print_wait)          
813 {                                                 
814         if (!kgdb_io_ops)                         
815                 return 0;                         
816         if (kgdb_connected)                       
817                 return 1;                         
818         if (atomic_read(&kgdb_setting_breakpoi    
819                 return 1;                         
820         if (print_wait)                           
821                 printk(KERN_CRIT "KGDB: Waitin    
822         return 1;                                 
823 }                                                 
824                                                   
825 /*                                                
826  * All the functions that start with gdb_cmd a    
827  * operations to implement the handlers for th    
828  * where KGDB is communicating with an externa    
829  */                                               
830                                                   
831 /* Handle the '?' status packets */               
832 static void gdb_cmd_status(struct kgdb_state *    
833 {                                                 
834         /*                                        
835          * We know that this packet is only se    
836          * during initial connect.  So to be s    
837          * we clear out our breakpoints now in    
838          * GDB is reconnecting.                   
839          */                                       
840         remove_all_break();                       
841                                                   
842         remcom_out_buffer[0] = 'S';               
843         pack_hex_byte(&remcom_out_buffer[1], k    
844 }                                                 
845                                                   
846 /* Handle the 'g' get registers request */        
847 static void gdb_cmd_getregs(struct kgdb_state     
848 {                                                 
849         struct task_struct *thread;               
850         void *local_debuggerinfo;                 
851         int i;                                    
852                                                   
853         thread = kgdb_usethread;                  
854         if (!thread) {                            
855                 thread = kgdb_info[ks->cpu].ta    
856                 local_debuggerinfo = kgdb_info    
857         } else {                                  
858                 local_debuggerinfo = NULL;        
859                 for_each_online_cpu(i) {          
860                         /*                        
861                          * Try to find the tas    
862                          * or possibly this no    
863                          * find the matching t    
864                          * to approximate the     
865                          */                       
866                         if (thread == kgdb_inf    
867                                 local_debugger    
868                 }                                 
869         }                                         
870                                                   
871         /*                                        
872          * All threads that don't have debugge    
873          * in __schedule() sleeping, since all    
874          * are in kgdb_wait, and thus have deb    
875          */                                       
876         if (local_debuggerinfo) {                 
877                 pt_regs_to_gdb_regs(gdb_regs,     
878         } else {                                  
879                 /*                                
880                  * Pull stuff saved during swi    
881                  * else is accessible (or even    
882                  * relevant).                     
883                  *                                
884                  * This should be enough for a    
885                  */                               
886                 sleeping_thread_to_gdb_regs(gd    
887         }                                         
888         kgdb_mem2hex((char *)gdb_regs, remcom_    
889 }                                                 
890                                                   
891 /* Handle the 'G' set registers request */        
892 static void gdb_cmd_setregs(struct kgdb_state     
893 {                                                 
894         kgdb_hex2mem(&remcom_in_buffer[1], (ch    
895                                                   
896         if (kgdb_usethread && kgdb_usethread !    
897                 error_packet(remcom_out_buffer    
898         } else {                                  
899                 gdb_regs_to_pt_regs(gdb_regs,     
900                 strcpy(remcom_out_buffer, "OK"    
901         }                                         
902 }                                                 
903                                                   
904 /* Handle the 'm' memory read bytes */            
905 static void gdb_cmd_memread(struct kgdb_state     
906 {                                                 
907         char *ptr = &remcom_in_buffer[1];         
908         unsigned long length;                     
909         unsigned long addr;                       
910         int err;                                  
911                                                   
912         if (kgdb_hex2long(&ptr, &addr) > 0 &&     
913                                         kgdb_h    
914                 err = kgdb_mem2hex((char *)add    
915                 if (err)                          
916                         error_packet(remcom_ou    
917         } else {                                  
918                 error_packet(remcom_out_buffer    
919         }                                         
920 }                                                 
921                                                   
922 /* Handle the 'M' memory write bytes */           
923 static void gdb_cmd_memwrite(struct kgdb_state    
924 {                                                 
925         int err = write_mem_msg(0);               
926                                                   
927         if (err)                                  
928                 error_packet(remcom_out_buffer    
929         else                                      
930                 strcpy(remcom_out_buffer, "OK"    
931 }                                                 
932                                                   
933 /* Handle the 'X' memory binary write bytes */    
934 static void gdb_cmd_binwrite(struct kgdb_state    
935 {                                                 
936         int err = write_mem_msg(1);               
937                                                   
938         if (err)                                  
939                 error_packet(remcom_out_buffer    
940         else                                      
941                 strcpy(remcom_out_buffer, "OK"    
942 }                                                 
943                                                   
944 /* Handle the 'D' or 'k', detach or kill packe    
945 static void gdb_cmd_detachkill(struct kgdb_sta    
946 {                                                 
947         int error;                                
948                                                   
949         /* The detach case */                     
950         if (remcom_in_buffer[0] == 'D') {         
951                 error = remove_all_break();       
952                 if (error < 0) {                  
953                         error_packet(remcom_ou    
954                 } else {                          
955                         strcpy(remcom_out_buff    
956                         kgdb_connected = 0;       
957                 }                                 
958                 put_packet(remcom_out_buffer);    
959         } else {                                  
960                 /*                                
961                  * Assume the kill case, with     
962                  * trying to force detach the     
963                  */                               
964                 remove_all_break();               
965                 kgdb_connected = 0;               
966         }                                         
967 }                                                 
968                                                   
969 /* Handle the 'R' reboot packets */               
970 static int gdb_cmd_reboot(struct kgdb_state *k    
971 {                                                 
972         /* For now, only honor R0 */              
973         if (strcmp(remcom_in_buffer, "R0") ==     
974                 printk(KERN_CRIT "Executing em    
975                 strcpy(remcom_out_buffer, "OK"    
976                 put_packet(remcom_out_buffer);    
977                                                   
978                 /*                                
979                  * Execution should not return    
980                  * machine_emergency_restart()    
981                  */                               
982                 machine_emergency_restart();      
983                 kgdb_connected = 0;               
984                                                   
985                 return 1;                         
986         }                                         
987         return 0;                                 
988 }                                                 
989                                                   
990 /* Handle the 'q' query packets */                
991 static void gdb_cmd_query(struct kgdb_state *k    
992 {                                                 
993         struct task_struct *g;                    
994         struct task_struct *p;                    
995         unsigned char thref[8];                   
996         char *ptr;                                
997         int i;                                    
998         int cpu;                                  
999         int finished = 0;                         
1000                                                  
1001         switch (remcom_in_buffer[1]) {           
1002         case 's':                                
1003         case 'f':                                
1004                 if (memcmp(remcom_in_buffer +    
1005                         error_packet(remcom_o    
1006                         break;                   
1007                 }                                
1008                                                  
1009                 i = 0;                           
1010                 remcom_out_buffer[0] = 'm';      
1011                 ptr = remcom_out_buffer + 1;     
1012                 if (remcom_in_buffer[1] == 'f    
1013                         /* Each cpu is a shad    
1014                         for_each_online_cpu(c    
1015                                 ks->thr_query    
1016                                 int_to_thread    
1017                                 pack_threadid    
1018                                 ptr += BUF_TH    
1019                                 *(ptr++) = ',    
1020                                 i++;             
1021                         }                        
1022                 }                                
1023                                                  
1024                 do_each_thread(g, p) {           
1025                         if (i >= ks->thr_quer    
1026                                 int_to_thread    
1027                                 pack_threadid    
1028                                 ptr += BUF_TH    
1029                                 *(ptr++) = ',    
1030                                 ks->thr_query    
1031                                 if (ks->thr_q    
1032                                         finis    
1033                         }                        
1034                         i++;                     
1035                 } while_each_thread(g, p);       
1036                                                  
1037                 *(--ptr) = '\0';                 
1038                 break;                           
1039                                                  
1040         case 'C':                                
1041                 /* Current thread id */          
1042                 strcpy(remcom_out_buffer, "QC    
1043                 ks->threadid = shadow_pid(cur    
1044                 int_to_threadref(thref, ks->t    
1045                 pack_threadid(remcom_out_buff    
1046                 break;                           
1047         case 'T':                                
1048                 if (memcmp(remcom_in_buffer +    
1049                         error_packet(remcom_o    
1050                         break;                   
1051                 }                                
1052                 ks->threadid = 0;                
1053                 ptr = remcom_in_buffer + 17;     
1054                 kgdb_hex2long(&ptr, &ks->thre    
1055                 if (!getthread(ks->linux_regs    
1056                         error_packet(remcom_o    
1057                         break;                   
1058                 }                                
1059                 if ((int)ks->threadid > 0) {     
1060                         kgdb_mem2hex(getthrea    
1061                                         ks->t    
1062                                         remco    
1063                 } else {                         
1064                         static char tmpstr[23    
1065                                                  
1066                         sprintf(tmpstr, "shad    
1067                                         (int)    
1068                         kgdb_mem2hex(tmpstr,     
1069                 }                                
1070                 break;                           
1071         }                                        
1072 }                                                
1073                                                  
1074 /* Handle the 'H' task query packets */          
1075 static void gdb_cmd_task(struct kgdb_state *k    
1076 {                                                
1077         struct task_struct *thread;              
1078         char *ptr;                               
1079                                                  
1080         switch (remcom_in_buffer[1]) {           
1081         case 'g':                                
1082                 ptr = &remcom_in_buffer[2];      
1083                 kgdb_hex2long(&ptr, &ks->thre    
1084                 thread = getthread(ks->linux_    
1085                 if (!thread && ks->threadid >    
1086                         error_packet(remcom_o    
1087                         break;                   
1088                 }                                
1089                 kgdb_usethread = thread;         
1090                 ks->kgdb_usethreadid = ks->th    
1091                 strcpy(remcom_out_buffer, "OK    
1092                 break;                           
1093         case 'c':                                
1094                 ptr = &remcom_in_buffer[2];      
1095                 kgdb_hex2long(&ptr, &ks->thre    
1096                 if (!ks->threadid) {             
1097                         kgdb_contthread = NUL    
1098                 } else {                         
1099                         thread = getthread(ks    
1100                         if (!thread && ks->th    
1101                                 error_packet(    
1102                                 break;           
1103                         }                        
1104                         kgdb_contthread = thr    
1105                 }                                
1106                 strcpy(remcom_out_buffer, "OK    
1107                 break;                           
1108         }                                        
1109 }                                                
1110                                                  
1111 /* Handle the 'T' thread query packets */        
1112 static void gdb_cmd_thread(struct kgdb_state     
1113 {                                                
1114         char *ptr = &remcom_in_buffer[1];        
1115         struct task_struct *thread;              
1116                                                  
1117         kgdb_hex2long(&ptr, &ks->threadid);      
1118         thread = getthread(ks->linux_regs, ks    
1119         if (thread)                              
1120                 strcpy(remcom_out_buffer, "OK    
1121         else                                     
1122                 error_packet(remcom_out_buffe    
1123 }                                                
1124                                                  
1125 /* Handle the 'z' or 'Z' breakpoint remove or    
1126 static void gdb_cmd_break(struct kgdb_state *    
1127 {                                                
1128         /*                                       
1129          * Since GDB-5.3, it's been drafted t    
1130          * breakpoint, '1' is a hardware brea    
1131          */                                      
1132         char *bpt_type = &remcom_in_buffer[1]    
1133         char *ptr = &remcom_in_buffer[2];        
1134         unsigned long addr;                      
1135         unsigned long length;                    
1136         int error = 0;                           
1137                                                  
1138         if (arch_kgdb_ops.set_hw_breakpoint &    
1139                 /* Unsupported */                
1140                 if (*bpt_type > '4')             
1141                         return;                  
1142         } else {                                 
1143                 if (*bpt_type != '' && *bpt_t    
1144                         /* Unsupported. */       
1145                         return;                  
1146         }                                        
1147                                                  
1148         /*                                       
1149          * Test if this is a hardware breakpo    
1150          * if we support it:                     
1151          */                                      
1152         if (*bpt_type == '1' && !(arch_kgdb_o    
1153                 /* Unsupported. */               
1154                 return;                          
1155                                                  
1156         if (*(ptr++) != ',') {                   
1157                 error_packet(remcom_out_buffe    
1158                 return;                          
1159         }                                        
1160         if (!kgdb_hex2long(&ptr, &addr)) {       
1161                 error_packet(remcom_out_buffe    
1162                 return;                          
1163         }                                        
1164         if (*(ptr++) != ',' ||                   
1165                 !kgdb_hex2long(&ptr, &length)    
1166                 error_packet(remcom_out_buffe    
1167                 return;                          
1168         }                                        
1169                                                  
1170         if (remcom_in_buffer[0] == 'Z' && *bp    
1171                 error = kgdb_set_sw_break(add    
1172         else if (remcom_in_buffer[0] == 'z' &    
1173                 error = kgdb_remove_sw_break(    
1174         else if (remcom_in_buffer[0] == 'Z')     
1175                 error = arch_kgdb_ops.set_hw_    
1176                         (int)length, *bpt_typ    
1177         else if (remcom_in_buffer[0] == 'z')     
1178                 error = arch_kgdb_ops.remove_    
1179                         (int) length, *bpt_ty    
1180                                                  
1181         if (error == 0)                          
1182                 strcpy(remcom_out_buffer, "OK    
1183         else                                     
1184                 error_packet(remcom_out_buffe    
1185 }                                                
1186                                                  
1187 /* Handle the 'C' signal / exception passing     
1188 static int gdb_cmd_exception_pass(struct kgdb    
1189 {                                                
1190         /* C09 == pass exception                 
1191          * C15 == detach kgdb, pass exception    
1192          */                                      
1193         if (remcom_in_buffer[1] == '' && remc    
1194                                                  
1195                 ks->pass_exception = 1;          
1196                 remcom_in_buffer[0] = 'c';       
1197                                                  
1198         } else if (remcom_in_buffer[1] == '1'    
1199                                                  
1200                 ks->pass_exception = 1;          
1201                 remcom_in_buffer[0] = 'D';       
1202                 remove_all_break();              
1203                 kgdb_connected = 0;              
1204                 return 1;                        
1205                                                  
1206         } else {                                 
1207                 error_packet(remcom_out_buffe    
1208                 return 0;                        
1209         }                                        
1210                                                  
1211         /* Indicate fall through */              
1212         return -1;                               
1213 }                                                
1214                                                  
1215 /*                                               
1216  * This function performs all gdbserial comma    
1217  */                                              
1218 static int gdb_serial_stub(struct kgdb_state     
1219 {                                                
1220         int error = 0;                           
1221         int tmp;                                 
1222                                                  
1223         /* Clear the out buffer. */              
1224         memset(remcom_out_buffer, 0, sizeof(r    
1225                                                  
1226         if (kgdb_connected) {                    
1227                 unsigned char thref[8];          
1228                 char *ptr;                       
1229                                                  
1230                 /* Reply to host that an exce    
1231                 ptr = remcom_out_buffer;         
1232                 *ptr++ = 'T';                    
1233                 ptr = pack_hex_byte(ptr, ks->    
1234                 ptr += strlen(strcpy(ptr, "th    
1235                 int_to_threadref(thref, shado    
1236                 ptr = pack_threadid(ptr, thre    
1237                 *ptr++ = ';';                    
1238                 put_packet(remcom_out_buffer)    
1239         }                                        
1240                                                  
1241         kgdb_usethread = kgdb_info[ks->cpu].t    
1242         ks->kgdb_usethreadid = shadow_pid(kgd    
1243         ks->pass_exception = 0;                  
1244                                                  
1245         while (1) {                              
1246                 error = 0;                       
1247                                                  
1248                 /* Clear the out buffer. */      
1249                 memset(remcom_out_buffer, 0,     
1250                                                  
1251                 get_packet(remcom_in_buffer);    
1252                                                  
1253                 switch (remcom_in_buffer[0])     
1254                 case '?': /* gdbserial status    
1255                         gdb_cmd_status(ks);      
1256                         break;                   
1257                 case 'g': /* return the value    
1258                         gdb_cmd_getregs(ks);     
1259                         break;                   
1260                 case 'G': /* set the value of    
1261                         gdb_cmd_setregs(ks);     
1262                         break;                   
1263                 case 'm': /* mAA..AA,LLLL  Re    
1264                         gdb_cmd_memread(ks);     
1265                         break;                   
1266                 case 'M': /* MAA..AA,LLLL: Wr    
1267                         gdb_cmd_memwrite(ks);    
1268                         break;                   
1269                 case 'X': /* XAA..AA,LLLL: Wr    
1270                         gdb_cmd_binwrite(ks);    
1271                         break;                   
1272                         /* kill or detach. KG    
1273                          * continue.             
1274                          */                      
1275                 case 'D': /* Debugger detach     
1276                 case 'k': /* Debugger detach     
1277                         gdb_cmd_detachkill(ks    
1278                         goto default_handle;     
1279                 case 'R': /* Reboot */           
1280                         if (gdb_cmd_reboot(ks    
1281                                 goto default_    
1282                         break;                   
1283                 case 'q': /* query command */    
1284                         gdb_cmd_query(ks);       
1285                         break;                   
1286                 case 'H': /* task related */     
1287                         gdb_cmd_task(ks);        
1288                         break;                   
1289                 case 'T': /* Query thread sta    
1290                         gdb_cmd_thread(ks);      
1291                         break;                   
1292                 case 'z': /* Break point remo    
1293                 case 'Z': /* Break point set     
1294                         gdb_cmd_break(ks);       
1295                         break;                   
1296                 case 'C': /* Exception passin    
1297                         tmp = gdb_cmd_excepti    
1298                         if (tmp > 0)             
1299                                 goto default_    
1300                         if (tmp == 0)            
1301                                 break;           
1302                         /* Fall through on tm    
1303                 case 'c': /* Continue packet     
1304                 case 's': /* Single step pack    
1305                         if (kgdb_contthread &    
1306                                 /* Can't swit    
1307                                 error_packet(    
1308                                 break;           
1309                         }                        
1310                         kgdb_activate_sw_brea    
1311                         /* Fall through to de    
1312                 default:                         
1313 default_handle:                                  
1314                         error = kgdb_arch_han    
1315                                                  
1316                                                  
1317                                                  
1318                                                  
1319                                                  
1320                         /*                       
1321                          * Leave cmd processi    
1322                          * kill, continue, or    
1323                          */                      
1324                         if (error >= 0 || rem    
1325                             remcom_in_buffer[    
1326                                 error = 0;       
1327                                 goto kgdb_exi    
1328                         }                        
1329                                                  
1330                 }                                
1331                                                  
1332                 /* reply to the request */       
1333                 put_packet(remcom_out_buffer)    
1334         }                                        
1335                                                  
1336 kgdb_exit:                                       
1337         if (ks->pass_exception)                  
1338                 error = 1;                       
1339         return error;                            
1340 }                                                
1341                                                  
1342 static int kgdb_reenter_check(struct kgdb_sta    
1343 {                                                
1344         unsigned long addr;                      
1345                                                  
1346         if (atomic_read(&kgdb_active) != raw_    
1347                 return 0;                        
1348                                                  
1349         /* Panic on recursive debugger calls:    
1350         exception_level++;                       
1351         addr = kgdb_arch_pc(ks->ex_vector, ks    
1352         kgdb_deactivate_sw_breakpoints();        
1353                                                  
1354         /*                                       
1355          * If the break point removed ok at t    
1356          * occurred, try to recover and print    
1357          * user because the user planted a br    
1358          * KGDB needs in order to function.      
1359          */                                      
1360         if (kgdb_remove_sw_break(addr) == 0)     
1361                 exception_level = 0;             
1362                 kgdb_skipexception(ks->ex_vec    
1363                 kgdb_activate_sw_breakpoints(    
1364                 printk(KERN_CRIT "KGDB: re-en    
1365                         addr);                   
1366                 WARN_ON_ONCE(1);                 
1367                                                  
1368                 return 1;                        
1369         }                                        
1370         remove_all_break();                      
1371         kgdb_skipexception(ks->ex_vector, ks-    
1372                                                  
1373         if (exception_level > 1) {               
1374                 dump_stack();                    
1375                 panic("Recursive entry to deb    
1376         }                                        
1377                                                  
1378         printk(KERN_CRIT "KGDB: re-enter exce    
1379         dump_stack();                            
1380         panic("Recursive entry to debugger");    
1381                                                  
1382         return 1;                                
1383 }                                                
1384                                                  
1385 /*                                               
1386  * kgdb_handle_exception() - main entry point    
1387  *                                               
1388  * Locking hierarchy:                            
1389  *      interface locks, if any (begin_sessio    
1390  *      kgdb lock (kgdb_active)                  
1391  */                                              
1392 int                                              
1393 kgdb_handle_exception(int evector, int signo,    
1394 {                                                
1395         struct kgdb_state kgdb_var;              
1396         struct kgdb_state *ks = &kgdb_var;       
1397         unsigned long flags;                     
1398         int error = 0;                           
1399         int i, cpu;                              
1400                                                  
1401         ks->cpu                 = raw_smp_pro    
1402         ks->ex_vector           = evector;       
1403         ks->signo               = signo;         
1404         ks->ex_vector           = evector;       
1405         ks->err_code            = ecode;         
1406         ks->kgdb_usethreadid    = 0;             
1407         ks->linux_regs          = regs;          
1408                                                  
1409         if (kgdb_reenter_check(ks))              
1410                 return 0; /* Ouch, double exc    
1411                                                  
1412 acquirelock:                                     
1413         /*                                       
1414          * Interrupts will be restored by the    
1415          * single stepping.                      
1416          */                                      
1417         local_irq_save(flags);                   
1418                                                  
1419         cpu = raw_smp_processor_id();            
1420                                                  
1421         /*                                       
1422          * Acquire the kgdb_active lock:         
1423          */                                      
1424         while (atomic_cmpxchg(&kgdb_active, -    
1425                 cpu_relax();                     
1426                                                  
1427         /*                                       
1428          * Do not start the debugger connecti    
1429          * instance of the exception handler     
1430          * debugger on a different CPU via a     
1431          */                                      
1432         if (atomic_read(&kgdb_cpu_doing_singl    
1433             atomic_read(&kgdb_cpu_doing_singl    
1434                                                  
1435                 atomic_set(&kgdb_active, -1);    
1436                 touch_softlockup_watchdog();     
1437                 clocksource_touch_watchdog();    
1438                 local_irq_restore(flags);        
1439                                                  
1440                 goto acquirelock;                
1441         }                                        
1442                                                  
1443         if (!kgdb_io_ready(1)) {                 
1444                 error = 1;                       
1445                 goto kgdb_restore; /* No I/O     
1446         }                                        
1447                                                  
1448         /*                                       
1449          * Don't enter if we have hit a remov    
1450          */                                      
1451         if (kgdb_skipexception(ks->ex_vector,    
1452                 goto kgdb_restore;               
1453                                                  
1454         /* Call the I/O driver's pre_exceptio    
1455         if (kgdb_io_ops->pre_exception)          
1456                 kgdb_io_ops->pre_exception();    
1457                                                  
1458         kgdb_info[ks->cpu].debuggerinfo = ks-    
1459         kgdb_info[ks->cpu].task = current;       
1460                                                  
1461         kgdb_disable_hw_debug(ks->linux_regs)    
1462                                                  
1463         /*                                       
1464          * Get the passive CPU lock which wil    
1465          * CPU in a spin state while the debu    
1466          */                                      
1467         if (!kgdb_single_step) {                 
1468                 for (i = 0; i < NR_CPUS; i++)    
1469                         atomic_set(&passive_c    
1470         }                                        
1471                                                  
1472         /*                                       
1473          * spin_lock code is good enough as a    
1474          * need one here:                        
1475          */                                      
1476         atomic_set(&cpu_in_kgdb[ks->cpu], 1);    
1477                                                  
1478 #ifdef CONFIG_SMP                                
1479         /* Signal the other CPUs to enter kgd    
1480         if ((!kgdb_single_step) && kgdb_do_ro    
1481                 kgdb_roundup_cpus(flags);        
1482 #endif                                           
1483                                                  
1484         /*                                       
1485          * Wait for the other CPUs to be noti    
1486          */                                      
1487         for_each_online_cpu(i) {                 
1488                 while (!atomic_read(&cpu_in_k    
1489                         cpu_relax();             
1490         }                                        
1491                                                  
1492         /*                                       
1493          * At this point the primary processo    
1494          * in the debugger and all secondary     
1495          */                                      
1496         kgdb_post_primary_code(ks->linux_regs    
1497         kgdb_deactivate_sw_breakpoints();        
1498         kgdb_single_step = 0;                    
1499         kgdb_contthread = current;               
1500         exception_level = 0;                     
1501                                                  
1502         /* Talk to debugger with gdbserial pr    
1503         error = gdb_serial_stub(ks);             
1504                                                  
1505         /* Call the I/O driver's post_excepti    
1506         if (kgdb_io_ops->post_exception)         
1507                 kgdb_io_ops->post_exception()    
1508                                                  
1509         kgdb_info[ks->cpu].debuggerinfo = NUL    
1510         kgdb_info[ks->cpu].task = NULL;          
1511         atomic_set(&cpu_in_kgdb[ks->cpu], 0);    
1512                                                  
1513         if (!kgdb_single_step) {                 
1514                 for (i = NR_CPUS-1; i >= 0; i    
1515                         atomic_set(&passive_c    
1516                 /*                               
1517                  * Wait till all the CPUs hav    
1518                  * from the debugger.            
1519                  */                              
1520                 for_each_online_cpu(i) {         
1521                         while (atomic_read(&c    
1522                                 cpu_relax();     
1523                 }                                
1524         }                                        
1525                                                  
1526 kgdb_restore:                                    
1527         /* Free kgdb_active */                   
1528         atomic_set(&kgdb_active, -1);            
1529         touch_softlockup_watchdog();             
1530         clocksource_touch_watchdog();            
1531         local_irq_restore(flags);                
1532                                                  
1533         return error;                            
1534 }                                                
1535                                                  
1536 int kgdb_nmicallback(int cpu, void *regs)        
1537 {                                                
1538 #ifdef CONFIG_SMP                                
1539         if (!atomic_read(&cpu_in_kgdb[cpu]) &    
1540                         atomic_read(&kgdb_act    
1541                         atomic_read(&cpu_in_k    
1542                 kgdb_wait((struct pt_regs *)r    
1543                 return 0;                        
1544         }                                        
1545 #endif                                           
1546         return 1;                                
1547 }                                                
1548                                                  
1549 static void kgdb_console_write(struct console    
1550    unsigned count)                               
1551 {                                                
1552         unsigned long flags;                     
1553                                                  
1554         /* If we're debugging, or KGDB has no    
1555          * and print. */                         
1556         if (!kgdb_connected || atomic_read(&k    
1557                 return;                          
1558                                                  
1559         local_irq_save(flags);                   
1560         kgdb_msg_write(s, count);                
1561         local_irq_restore(flags);                
1562 }                                                
1563                                                  
1564 static struct console kgdbcons = {               
1565         .name           = "kgdb",                
1566         .write          = kgdb_console_write,    
1567         .flags          = CON_PRINTBUFFER | C    
1568         .index          = -1,                    
1569 };                                               
1570                                                  
1571 #ifdef CONFIG_MAGIC_SYSRQ                        
1572 static void sysrq_handle_gdb(int key, struct     
1573 {                                                
1574         if (!kgdb_io_ops) {                      
1575                 printk(KERN_CRIT "ERROR: No K    
1576                 return;                          
1577         }                                        
1578         if (!kgdb_connected)                     
1579                 printk(KERN_CRIT "Entering KG    
1580                                                  
1581         kgdb_breakpoint();                       
1582 }                                                
1583                                                  
1584 static struct sysrq_key_op sysrq_gdb_op = {      
1585         .handler        = sysrq_handle_gdb,      
1586         .help_msg       = "debug(G)",            
1587         .action_msg     = "DEBUG",               
1588 };                                               
1589 #endif                                           
1590                                                  
1591 static void kgdb_register_callbacks(void)        
1592 {                                                
1593         if (!kgdb_io_module_registered) {        
1594                 kgdb_io_module_registered = 1    
1595                 kgdb_arch_init();                
1596 #ifdef CONFIG_MAGIC_SYSRQ                        
1597                 register_sysrq_key('g', &sysr    
1598 #endif                                           
1599                 if (kgdb_use_con && !kgdb_con    
1600                         register_console(&kgd    
1601                         kgdb_con_registered =    
1602                 }                                
1603         }                                        
1604 }                                                
1605                                                  
1606 static void kgdb_unregister_callbacks(void)      
1607 {                                                
1608         /*                                       
1609          * When this routine is called KGDB s    
1610          * panic handler and clean up, making    
1611          * break exceptions at the time.         
1612          */                                      
1613         if (kgdb_io_module_registered) {         
1614                 kgdb_io_module_registered = 0    
1615                 kgdb_arch_exit();                
1616 #ifdef CONFIG_MAGIC_SYSRQ                        
1617                 unregister_sysrq_key('g', &sy    
1618 #endif                                           
1619                 if (kgdb_con_registered) {       
1620                         unregister_console(&k    
1621                         kgdb_con_registered =    
1622                 }                                
1623         }                                        
1624 }                                                
1625                                                  
1626 static void kgdb_initial_breakpoint(void)        
1627 {                                                
1628         kgdb_break_asap = 0;                     
1629                                                  
1630         printk(KERN_CRIT "kgdb: Waiting for c    
1631         kgdb_breakpoint();                       
1632 }                                                
1633                                                  
1634 /**                                              
1635  *      kgdb_register_io_module - register KG    
1636  *      @new_kgdb_io_ops: the io ops vector      
1637  *                                               
1638  *      Register it with the KGDB core.          
1639  */                                              
1640 int kgdb_register_io_module(struct kgdb_io *n    
1641 {                                                
1642         int err;                                 
1643                                                  
1644         spin_lock(&kgdb_registration_lock);      
1645                                                  
1646         if (kgdb_io_ops) {                       
1647                 spin_unlock(&kgdb_registratio    
1648                                                  
1649                 printk(KERN_ERR "kgdb: Anothe    
1650                                 "registered w    
1651                 return -EBUSY;                   
1652         }                                        
1653                                                  
1654         if (new_kgdb_io_ops->init) {             
1655                 err = new_kgdb_io_ops->init()    
1656                 if (err) {                       
1657                         spin_unlock(&kgdb_reg    
1658                         return err;              
1659                 }                                
1660         }                                        
1661                                                  
1662         kgdb_io_ops = new_kgdb_io_ops;           
1663                                                  
1664         spin_unlock(&kgdb_registration_lock);    
1665                                                  
1666         printk(KERN_INFO "kgdb: Registered I/    
1667                new_kgdb_io_ops->name);           
1668                                                  
1669         /* Arm KGDB now. */                      
1670         kgdb_register_callbacks();               
1671                                                  
1672         if (kgdb_break_asap)                     
1673                 kgdb_initial_breakpoint();       
1674                                                  
1675         return 0;                                
1676 }                                                
1677 EXPORT_SYMBOL_GPL(kgdb_register_io_module);      
1678                                                  
1679 /**                                              
1680  *      kkgdb_unregister_io_module - unregist    
1681  *      @old_kgdb_io_ops: the io ops vector      
1682  *                                               
1683  *      Unregister it with the KGDB core.        
1684  */                                              
1685 void kgdb_unregister_io_module(struct kgdb_io    
1686 {                                                
1687         BUG_ON(kgdb_connected);                  
1688                                                  
1689         /*                                       
1690          * KGDB is no longer able to communic    
1691          * unregister our callbacks and reset    
1692          */                                      
1693         kgdb_unregister_callbacks();             
1694                                                  
1695         spin_lock(&kgdb_registration_lock);      
1696                                                  
1697         WARN_ON_ONCE(kgdb_io_ops != old_kgdb_    
1698         kgdb_io_ops = NULL;                      
1699                                                  
1700         spin_unlock(&kgdb_registration_lock);    
1701                                                  
1702         printk(KERN_INFO                         
1703                 "kgdb: Unregistered I/O drive    
1704                 old_kgdb_io_ops->name);          
1705 }                                                
1706 EXPORT_SYMBOL_GPL(kgdb_unregister_io_module);    
1707                                                  
1708 /**                                              
1709  * kgdb_breakpoint - generate breakpoint exce    
1710  *                                               
1711  * This function will generate a breakpoint e    
1712  * beginning of a program to sync up with a d    
1713  * otherwise as a quick means to stop program    
1714  * the debugger.                                 
1715  */                                              
1716 void kgdb_breakpoint(void)                       
1717 {                                                
1718         atomic_set(&kgdb_setting_breakpoint,     
1719         wmb(); /* Sync point before breakpoin    
1720         arch_kgdb_breakpoint();                  
1721         wmb(); /* Sync point after breakpoint    
1722         atomic_set(&kgdb_setting_breakpoint,     
1723 }                                                
1724 EXPORT_SYMBOL_GPL(kgdb_breakpoint);              
1725                                                  
1726 static int __init opt_kgdb_wait(char *str)       
1727 {                                                
1728         kgdb_break_asap = 1;                     
1729                                                  
1730         if (kgdb_io_module_registered)           
1731                 kgdb_initial_breakpoint();       
1732                                                  
1733         return 0;                                
1734 }                                                
1735                                                  
1736 early_param("kgdbwait", opt_kgdb_wait);          
1737                                                  
  This page was automatically generated by the LXR engine.