aboutsummaryrefslogtreecommitdiffstats
path: root/tools/debugger/pdb/linux-2.6-module/debug.c
blob: ae1064ebad8666cc690f241f17fb70b292707c42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
/*
 * debug.c
 * pdb debug functionality for processes.
 */

#include <linux/module.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <asm-i386/kdebug.h>
#include <asm-xen/asm-i386/processor.h>
#include <asm-xen/asm-i386/ptrace.h>
#include <asm-xen/asm-i386/tlbflush.h>
#include <asm-xen/xen-public/xen.h>
#include "pdb_module.h"
#include "pdb_debug.h"


static int pdb_debug_fn (struct pt_regs *regs, long error_code,
                         unsigned int condition);
static int pdb_int3_fn (struct pt_regs *regs, long error_code);
static int pdb_page_fault_fn (struct pt_regs *regs, long error_code,
                              unsigned int condition);

/***********************************************************************/

typedef struct bwcpoint                           /* break/watch/catch point */
{
    struct list_head list;
    memory_t address;
    int length;

    u8  type;                                                     /* BWC_??? */
    u8  mode;                   /* for BWC_PAGE, the current protection mode */
    u32 process;
    u8  error;                /* error occured when enabling: don't disable. */

    /* original values */
    u8    orig_bkpt;                               /* single byte breakpoint */
    pte_t orig_pte;

    struct list_head watchpt_read_list;     /* read watchpoints on this page */
    struct list_head watchpt_write_list;                            /* write */
    struct list_head watchpt_access_list;                          /* access */
    struct list_head watchpt_disabled_list;                      /* disabled */

    struct bwcpoint *parent;             /* watchpoint: bwc_watch (the page) */
    struct bwcpoint *watchpoint;      /* bwc_watch_step: original watchpoint */
} bwcpoint_t, *bwcpoint_p;

static struct list_head bwcpoint_list = LIST_HEAD_INIT(bwcpoint_list);

#define _pdb_bwcpoint_alloc(_var) \
{ \
    if ( (_var = kmalloc(sizeof(bwcpoint_t), GFP_KERNEL)) == NULL ) \
        printk("error: unable to allocate memory %d\n", __LINE__); \
    else { \
        memset(_var, 0, sizeof(bwcpoint_t)); \
        INIT_LIST_HEAD(&_var->watchpt_read_list); \
        INIT_LIST_HEAD(&_var->watchpt_write_list); \
        INIT_LIST_HEAD(&_var->watchpt_access_list); \
        INIT_LIST_HEAD(&_var->watchpt_disabled_list); \
    } \
}

/***********************************************************************/

static void _pdb_bwc_print_list (struct list_head *, char *, int);

static void
_pdb_bwc_print (bwcpoint_p bwc, char *label, int level)
{
    printk("%s%03d 0x%08lx:0x%02x %c\n", label, bwc->type,
           bwc->address, bwc->length, bwc->error ? 'e' : '-');

    if ( !list_empty(&bwc->watchpt_read_list) )
        _pdb_bwc_print_list(&bwc->watchpt_read_list, "r", level);
    if ( !list_empty(&bwc->watchpt_write_list) )
        _pdb_bwc_print_list(&bwc->watchpt_write_list, "w", level);
    if ( !list_empty(&bwc->watchpt_access_list) )
        _pdb_bwc_print_list(&bwc->watchpt_access_list, "a", level);
    if ( !list_empty(&bwc->watchpt_disabled_list) )
        _pdb_bwc_print_list(&bwc->watchpt_disabled_list, "d", level);
}

static void
_pdb_bwc_print_list (struct list_head *bwc_list, char *label, int level)
{
    struct list_head *ptr;
    int counter = 0;

    list_for_each(ptr, bwc_list)
    {
        bwcpoint_p bwc = list_entry(ptr, bwcpoint_t, list);
        printk("  %s[%02d]%s ", level > 0 ? "  " : "", counter++,
                                level > 0 ? "" : "  ");
        _pdb_bwc_print(bwc, label, level+1);
    }

    if (counter == 0)
    {
        printk("  empty list\n");
    }
}

void
pdb_bwc_print_list (void)
{
    _pdb_bwc_print_list(&bwcpoint_list, " ", 0);
}

bwcpoint_p
pdb_search_watchpoint (u32 process, memory_t address)
{
    bwcpoint_p bwc_watch = (bwcpoint_p) 0;
    bwcpoint_p bwc_entry = (bwcpoint_p) 0;
    struct list_head *ptr;

    list_for_each(ptr, &bwcpoint_list)                /* find bwc page entry */
    {
        bwc_watch = list_entry(ptr, bwcpoint_t, list);
        if (bwc_watch->address == (address & PAGE_MASK)) break;
    }

    if ( !bwc_watch )
    {
        return (bwcpoint_p) 0;
    }

#define __pdb_search_watchpoint_list(__list) \
    list_for_each(ptr, (__list))  \
    { \
        bwc_entry = list_entry(ptr, bwcpoint_t, list); \
        if ( bwc_entry->process == process &&          \
             bwc_entry->address <= address &&          \
             bwc_entry->address + bwc_entry->length > address ) \
            return bwc_entry; \
    }

    __pdb_search_watchpoint_list(&bwc_watch->watchpt_read_list);
    __pdb_search_watchpoint_list(&bwc_watch->watchpt_write_list);
    __pdb_search_watchpoint_list(&bwc_watch->watchpt_access_list);

#undef __pdb_search_watchpoint_list

    return (bwcpoint_p) 0;
}

/*************************************************************/

int
pdb_suspend (struct task_struct *target)
{
    u32 rc = 0;

    force_sig(SIGSTOP, target);                    /* force_sig_specific ??? */

    return rc;
}

int
pdb_resume (struct task_struct *target)
{
    int rc = 0;

    wake_up_process(target);

    return rc;
}

/*
 * from linux-2.6.11/arch/i386/kernel/ptrace.c::getreg()
 */
static unsigned long
_pdb_get_register (struct task_struct *target, int reg)
{
    unsigned long result = ~0UL;
    unsigned long offset;
    unsigned char *stack = 0L;

    switch (reg)
    {
    case LINUX_FS:
        result = target->thread.fs;
        break;
    case LINUX_GS:
        result = target->thread.gs;
        break;
    case LINUX_DS:
    case LINUX_ES:
    case LINUX_SS:
    case LINUX_CS:
        result = 0xffff;
        /* fall through */
    default:
        if (reg > LINUX_GS)
            reg -= 2;

        offset = reg * sizeof(long);
        offset -= sizeof(struct pt_regs);
        stack = (unsigned char *)target->thread.esp0;
        stack += offset;
        result &= *((int *)stack);
    }

    return result;
}

/*
 * from linux-2.6.11/arch/i386/kernel/ptrace.c::putreg()
 */
static void
_pdb_set_register (struct task_struct *target, int reg, unsigned long val)
{
    unsigned long offset;
    unsigned char *stack;
    unsigned long value = val;

    switch (reg)
    {
    case LINUX_FS:
        target->thread.fs = value;
        return;
    case LINUX_GS:
        target->thread.gs = value;
        return;
    case LINUX_DS:
    case LINUX_ES:
        value &= 0xffff;
        break;
    case LINUX_SS:
    case LINUX_CS:
        value &= 0xffff;
        break;
    case LINUX_EFL:
        break;
    }

    if (reg > LINUX_GS)
        reg -= 2;
    offset = reg * sizeof(long);
    offset -= sizeof(struct pt_regs);
    stack = (unsigned char *)target->thread.esp0;
    stack += offset;
    *(unsigned long *) stack = value;

    return;
}

int
pdb_read_register (struct task_struct *target, pdb_op_rd_reg_p op)
{
    int rc = 0;

    switch (op->reg)
    {
    case  0: op->value = _pdb_get_register(target, LINUX_EAX); break;
    case  1: op->value = _pdb_get_register(target, LINUX_ECX); break;
    case  2: op->value = _pdb_get_register(target, LINUX_EDX); break;
    case  3: op->value = _pdb_get_register(target, LINUX_EBX); break;
    case  4: op->value = _pdb_get_register(target, LINUX_ESP); break;
    case  5: op->value = _pdb_get_register(target, LINUX_EBP); break;
    case  6: op->value = _pdb_get_register(target, LINUX_ESI); break;
    case  7: op->value = _pdb_get_register(target, LINUX_EDI); break;
    case  8: op->value = _pdb_get_register(target, LINUX_EIP); break;
    case  9: op->value = _pdb_get_register(target, LINUX_EFL); break;

    case 10: op->value = _pdb_get_register(target, LINUX_CS); break;
    case 11: op->value = _pdb_get_register(target, LINUX_SS); break;
    case 12: op->value = _pdb_get_register(target, LINUX_DS); break;
    case 13: op->value = _pdb_get_register(target, LINUX_ES); break;
    case 14: op->value = _pdb_get_register(target, LINUX_FS); break;
    case 15: op->value = _pdb_get_register(target, LINUX_GS); break;
    }

    return rc;
}

int
pdb_read_registers (struct task_struct *target, pdb_op_rd_regs_p op)
{
    int rc = 0;

    op->reg[ 0] = _pdb_get_register(target, LINUX_EAX);
    op->reg[ 1] = _pdb_get_register(target, LINUX_ECX);
    op->reg[ 2] = _pdb_get_register(target, LINUX_EDX);
    op->reg[ 3] = _pdb_get_register(target, LINUX_EBX);
    op->reg[ 4] = _pdb_get_register(target, LINUX_ESP);
    op->reg[ 5] = _pdb_get_register(target, LINUX_EBP);
    op->reg[ 6] = _pdb_get_register(target, LINUX_ESI);
    op->reg[ 7] = _pdb_get_register(target, LINUX_EDI);
    op->reg[ 8] = _pdb_get_register(target, LINUX_EIP);
    op->reg[ 9] = _pdb_get_register(target, LINUX_EFL);

    op->reg[10] = _pdb_get_register(target, LINUX_CS);
    op->reg[11] = _pdb_get_register(target, LINUX_SS);
    op->reg[12] = _pdb_get_register(target, LINUX_DS);
    op->reg[13] = _pdb_get_register(target, LINUX_ES);
    op->reg[14] = _pdb_get_register(target, LINUX_FS);
    op->reg[15] = _pdb_get_register(target, LINUX_GS);

    return rc;
}

int
pdb_write_register (struct task_struct *target, pdb_op_wr_reg_p op)
{
    int rc = 0;

    _pdb_set_register(target, op->reg, op->value);

    return rc;
}

int
pdb_access_memory (struct task_struct *target, unsigned long address, 
                   void *buffer, int length, int write)
{
    int rc = 0;

    access_process_vm(target, address, buffer, length, write);

    return rc;
}

int
pdb_continue (struct task_struct *target)
{
    int rc = 0;
    unsigned long eflags;

    eflags = _pdb_get_register(target, LINUX_EFL);
    eflags &= ~X86_EFLAGS_TF;
    _pdb_set_register(target, LINUX_EFL, eflags);

    wake_up_process(target);

    return rc;
}

int
pdb_step (struct task_struct *target)
{
    int rc = 0;
    unsigned long eflags;
    bwcpoint_p bkpt;
    
    eflags = _pdb_get_register(target, LINUX_EFL);
    eflags |= X86_EFLAGS_TF;
    _pdb_set_register(target, LINUX_EFL, eflags);

    _pdb_bwcpoint_alloc(bkpt);
    if ( bkpt == NULL )  return -1;

    bkpt->process = target->pid;
    bkpt->address = 0;
    bkpt->type    = BWC_DEBUG;
    
    list_add_tail(&bkpt->list, &bwcpoint_list);

    wake_up_process(target);

    return rc;
}

int
pdb_insert_memory_breakpoint (struct task_struct *target, 
                              memory_t address, u32 length)
{
    int rc = 0;
    bwcpoint_p bkpt;
    u8 breakpoint_opcode = 0xcc;

    printk("insert breakpoint %d:%lx len: %d\n", target->pid, address, length);

    if ( length != 1 )
    {
        printk("error: breakpoint length should be 1\n");
        return -1;
    }

    _pdb_bwcpoint_alloc(bkpt);
    if ( bkpt == NULL ) return -1;

    bkpt->process = target->pid;
    bkpt->address = address;
    bkpt->type    = BWC_INT3;

    pdb_access_memory(target, address, &bkpt->orig_bkpt, 1, PDB_MEM_READ);
    pdb_access_memory(target, address, &breakpoint_opcode, 1, PDB_MEM_WRITE);
    
    list_add_tail(&bkpt->list, &bwcpoint_list);

    printk("breakpoint_set %d:%lx  OLD: 0x%x\n",
           target->pid, address, bkpt->orig_bkpt);
    pdb_bwc_print_list();

    return rc;
}

int
pdb_remove_memory_breakpoint (struct task_struct *target,
                              memory_t address, u32 length)
{
    int rc = 0;
    bwcpoint_p bkpt = NULL;

    printk ("remove breakpoint %d:%lx\n", target->pid, address);

    struct list_head *entry;
    list_for_each(entry, &bwcpoint_list)
    {
        bkpt = list_entry(entry, bwcpoint_t, list);
        if ( target->pid == bkpt->process && 
             address == bkpt->address     &&
             bkpt->type == BWC_INT3 )
            break;
    }
    
    if (entry == &bwcpoint_list)
    {
        printk ("error: no breakpoint found\n");
        return -1;
    }

    pdb_access_memory(target, address, &bkpt->orig_bkpt, 1, PDB_MEM_WRITE);

    list_del(&bkpt->list);
    kfree(bkpt);

    pdb_bwc_print_list();

    return rc;
}

#define PDB_PTE_UPDATE   1
#define PDB_PTE_RESTORE  2

int
pdb_change_pte (struct task_struct *target, bwcpoint_p bwc, int mode)
{
    int rc = 0;
    pgd_t *pgd;
    pud_t *pud;
    pmd_t *pmd;
    pte_t *ptep;

    pgd = pgd_offset(target->mm, bwc->address);
    if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))  return -1;

    pud = pud_offset(pgd, bwc->address);
    if (pud_none(*pud) || unlikely(pud_bad(*pud))) return -2;

    pmd = pmd_offset(pud, bwc->address);
    if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd))) return -3;

    ptep = pte_offset_map(pmd, bwc->address);
    if (!ptep)  return -4;

    switch ( mode )
    {
    case PDB_PTE_UPDATE:      /* added or removed a watchpoint.  update pte. */
    {
        pte_t new_pte;

        if ( pte_val(bwc->parent->orig_pte) == 0 )    /* new watchpoint page */
        {
            bwc->parent->orig_pte = *ptep;
        }

        new_pte = bwc->parent->orig_pte;

        if ( !list_empty(&bwc->parent->watchpt_read_list)  || 
             !list_empty(&bwc->parent->watchpt_access_list) )
        {
            new_pte = pte_rdprotect(new_pte);
        }

        if ( !list_empty(&bwc->parent->watchpt_write_list) ||
             !list_empty(&bwc->parent->watchpt_access_list) )
        {
            new_pte = pte_wrprotect(new_pte);
        }
        
        if ( pte_val(new_pte) != pte_val(*ptep) )
        {
            *ptep = new_pte;
            flush_tlb_mm(target->mm);
        }
        break;
    }
    case PDB_PTE_RESTORE :   /* suspend watchpoint by restoring original pte */
    {
        *ptep = bwc->parent->orig_pte;
        flush_tlb_mm(target->mm);
        break;
    }
    default :
    {
        printk("(linux) unknown mode %d %d\n", mode, __LINE__);
        break;
    }
    }

    pte_unmap(ptep);                /* can i flush the tlb before pte_unmap? */

    return rc;
}

int
pdb_insert_watchpoint (struct task_struct *target, pdb_op_watchpt_p watchpt)
{
    int rc = 0;

    bwcpoint_p bwc_watch;
    bwcpoint_p bwc_entry;
    struct list_head *ptr;
    unsigned long page = watchpt->address & PAGE_MASK;
    struct list_head *watchpoint_list;
    
    printk("insert watchpoint: %d %x %x\n", 
           watchpt->type, watchpt->address, watchpt->length);

    list_for_each(ptr, &bwcpoint_list) /* find existing bwc page entry */
    {
        bwc_watch = list_entry(ptr, bwcpoint_t, list);

        if (bwc_watch->address == page)  goto got_bwc_watch;
    }

    _pdb_bwcpoint_alloc(bwc_watch);                  /* create new bwc:watch */
    if ( bwc_watch == NULL ) return -1;

    bwc_watch->type    = BWC_WATCH;
    bwc_watch->process = target->pid;
    bwc_watch->address = page;

    list_add_tail(&bwc_watch->list, &bwcpoint_list);

 got_bwc_watch:

    switch (watchpt->type)
    {
    case BWC_WATCH_READ:
        watchpoint_list = &bwc_watch->watchpt_read_list; break;
    case BWC_WATCH_WRITE: 
        watchpoint_list = &bwc_watch->watchpt_write_list; break;
    case BWC_WATCH_ACCESS:
        watchpoint_list = &bwc_watch->watchpt_access_list; break;
    default:
        printk("unknown type %d\n", watchpt->type); return -2;
    }

    _pdb_bwcpoint_alloc(bwc_entry);                  /* create new bwc:entry */
    if ( bwc_entry == NULL ) return -1;

    bwc_entry->process = target->pid;
    bwc_entry->address = watchpt->address;
    bwc_entry->length  = watchpt->length;
    bwc_entry->type    = watchpt->type;
    bwc_entry->parent  = bwc_watch;

    list_add_tail(&bwc_entry->list, watchpoint_list);
    pdb_change_pte(target, bwc_entry, PDB_PTE_UPDATE);

    pdb_bwc_print_list();

    return rc;
}

int 
pdb_remove_watchpoint (struct task_struct *target, pdb_op_watchpt_p watchpt)
{
    int rc = 0;
    bwcpoint_p bwc_watch = (bwcpoint_p) NULL;
    bwcpoint_p bwc_entry = (bwcpoint_p) NULL;
    unsigned long page = watchpt->address & PAGE_MASK;
    struct list_head *ptr;
    struct list_head *watchpoint_list;

    printk("remove watchpoint: %d %x %x\n", 
           watchpt->type, watchpt->address, watchpt->length);

    list_for_each(ptr, &bwcpoint_list)                /* find bwc page entry */
    {
        bwc_watch = list_entry(ptr, bwcpoint_t, list);
        if (bwc_watch->address == page) break;
    }

    if ( !bwc_watch )
    {
        printk("(linux) delete watchpoint: can't find bwc page 0x%08x\n",
               watchpt->address);
        return -1;
    }

    switch (watchpt->type)
    {
    case BWC_WATCH_READ:
        watchpoint_list = &bwc_watch->watchpt_read_list; break;
    case BWC_WATCH_WRITE:
        watchpoint_list = &bwc_watch->watchpt_write_list; break;
    case BWC_WATCH_ACCESS:
        watchpoint_list = &bwc_watch->watchpt_access_list; break;
    default:
        printk("unknown type %d\n", watchpt->type); return -2;
    }

    list_for_each(ptr, watchpoint_list)                   /* find watchpoint */
    {
        bwc_entry = list_entry(ptr, bwcpoint_t, list);
        if ( bwc_entry->address == watchpt->address &&
             bwc_entry->length  == watchpt->length ) break;
    }

    if ( !bwc_entry )                           /* or ptr == watchpoint_list */
    {
        printk("(linux) delete watchpoint: can't find watchpoint 0x%08x\n",
               watchpt->address);
        return -1;
    }
    
    list_del(&bwc_entry->list);
    pdb_change_pte(target, bwc_entry, PDB_PTE_UPDATE);
    kfree(bwc_entry);


    if ( list_empty(&bwc_watch->watchpt_read_list)  &&
         list_empty(&bwc_watch->watchpt_write_list) &&
         list_empty(&bwc_watch->watchpt_access_list) )
    {
        list_del(&bwc_watch->list);
        kfree(bwc_watch);
    }

    pdb_bwc_print_list();

    return rc;
}


/***************************************************************/

int
pdb_exceptions_notify (struct notifier_block *self, unsigned long val,
                       void *data)
{
    struct die_args *args = (struct die_args *)data;

	switch (val) 
    {
	case DIE_DEBUG:
		if ( pdb_debug_fn(args->regs, args->trapnr, args->err) )
			return NOTIFY_STOP;
		break;
    case DIE_TRAP:
		if ( args->trapnr == 3 && pdb_int3_fn(args->regs, args->err) )
			return NOTIFY_STOP;
        break;
	case DIE_INT3:          /* without kprobes, we should never see DIE_INT3 */
		if ( pdb_int3_fn(args->regs, args->err) )
			return NOTIFY_STOP;
		break;
	case DIE_PAGE_FAULT:
		if ( pdb_page_fault_fn(args->regs, args->trapnr, args->err) )
			return NOTIFY_STOP;
		break;
	case DIE_GPF:
        printk("---------------GPF\n");
        break;
	default:
		break;
	}

	return NOTIFY_DONE;
}


static int
pdb_debug_fn (struct pt_regs *regs, long error_code, 
                   unsigned int condition)
{
    pdb_response_t resp;
    bwcpoint_p bkpt = NULL;
    struct list_head *entry;

    printk("pdb_debug_fn\n");

    list_for_each(entry, &bwcpoint_list)
    {
        bkpt = list_entry(entry, bwcpoint_t, list);
        if ( current->pid == bkpt->process && 
             (bkpt->type == BWC_DEBUG ||                      /* single step */
              bkpt->type == BWC_WATCH_STEP))  /* single step over watchpoint */
            break;
    }
    
    if (entry == &bwcpoint_list)
    {
        printk("not my debug  0x%x 0x%lx\n", current->pid, regs->eip);
        return 0;
    }

    pdb_suspend(current);

    printk("(pdb) %s  pid: %d, eip: 0x%08lx\n", 
           bkpt->type == BWC_DEBUG ? "debug" : "watch-step",
           current->pid, regs->eip);

    regs->eflags &= ~X86_EFLAGS_TF;
	set_tsk_thread_flag(current, TIF_SINGLESTEP);

    switch (bkpt->type)
    {
    case BWC_DEBUG:
        resp.operation = PDB_OPCODE_STEP;
        break;
    case BWC_WATCH_STEP:
    {
        struct list_head *watchpoint_list;
        bwcpoint_p watch_page = bkpt->watchpoint->parent;

        switch (bkpt->watchpoint->type)
        {
        case BWC_WATCH_READ:
            watchpoint_list = &watch_page->watchpt_read_list; break;
        case BWC_WATCH_WRITE: 
            watchpoint_list = &watch_page->watchpt_write_list; break;
        case BWC_WATCH_ACCESS:
            watchpoint_list = &watch_page->watchpt_access_list; break;
        default:
            printk("unknown type %d\n", bkpt->watchpoint->type); return 0;
        }

        resp.operation = PDB_OPCODE_WATCHPOINT;
        list_del_init(&bkpt->watchpoint->list);
        list_add_tail(&bkpt->watchpoint->list, watchpoint_list);
        pdb_change_pte(current, bkpt->watchpoint, PDB_PTE_UPDATE);
        pdb_bwc_print_list();
        break;
    }
    default:
        printk("unknown breakpoint type %d %d\n", __LINE__, bkpt->type);
        return 0;
    }

    resp.process   = current->pid;
    resp.status    = PDB_RESPONSE_OKAY;

    pdb_send_response(&resp);

    list_del(&bkpt->list);
    kfree(bkpt);

    return 1;
}


static int
pdb_int3_fn (struct pt_regs *regs, long error_code)
{
    pdb_response_t resp;
    bwcpoint_p bkpt = NULL;
    memory_t address = regs->eip - 1;

    struct list_head *entry;
    list_for_each(entry, &bwcpoint_list)
    {
        bkpt = list_entry(entry, bwcpoint_t, list);
        if ( current->pid == bkpt->process && 
             address == bkpt->address      &&
             bkpt->type == BWC_INT3 )
            break;
    }
    
    if (entry == &bwcpoint_list)
    {
        printk("not my int3 bkpt  0x%x 0x%lx\n", current->pid, address);
        return 0;
    }

    printk("(pdb) int3  pid: %d, eip: 0x%08lx\n", current->pid, address);

    pdb_suspend(current);

    resp.operation = PDB_OPCODE_CONTINUE;
    resp.process   = current->pid;
    resp.status    = PDB_RESPONSE_OKAY;

    pdb_send_response(&resp);

    return 1;
}

static int
pdb_page_fault_fn (struct pt_regs *regs, long error_code, 
                   unsigned int condition)
{
    unsigned long cr2;
    unsigned long cr3;
    bwcpoint_p bwc;
    bwcpoint_p watchpt;
    bwcpoint_p bkpt;

    __asm__ __volatile__ ("movl %%cr3,%0" : "=r" (cr3) : );
    __asm__ __volatile__ ("movl %%cr2,%0" : "=r" (cr2) : );

    bwc = pdb_search_watchpoint(current->pid, cr2);
    if ( !bwc )
    {
        return 0;                                                /* not mine */
    }

    printk("page_fault cr2:%08lx err:%lx eip:%08lx\n", 
           cr2, error_code, regs->eip);

    /* disable the watchpoint */
    watchpt = bwc->watchpoint;
    list_del_init(&bwc->list);
    list_add_tail(&bwc->list, &bwc->parent->watchpt_disabled_list);
    pdb_change_pte(current, bwc, PDB_PTE_RESTORE);

    /* single step the faulting instruction */
    regs->eflags |= X86_EFLAGS_TF;

    /* create a bwcpoint entry so we know what to do once we regain control */
    _pdb_bwcpoint_alloc(bkpt);
    if ( bkpt == NULL )  return -1;

    bkpt->process    = current->pid;
    bkpt->address    = 0;
    bkpt->type       = BWC_WATCH_STEP;
    bkpt->watchpoint = bwc;

    /* add to head so we see it first the next time we break */
    list_add(&bkpt->list, &bwcpoint_list);                

    pdb_bwc_print_list();
    return 1;
}


/*
 * Local variables:
 * mode: C
 * c-set-style: "BSD"
 * c-basic-offset: 4
 * tab-width: 4
 * indent-tabs-mode: nil
 * End:
 */