Subversion Repositories HelenOS

Rev

Rev 2921 | Rev 3431 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2921 Rev 3424
1
/*
1
/*
2
 * Copyright (c) 2006 Ondrej Palkovsky
2
 * Copyright (c) 2006 Ondrej Palkovsky
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup amd64debug
29
/** @addtogroup amd64debug
30
 * @{
30
 * @{
31
 */
31
 */
32
/** @file
32
/** @file
33
 */
33
 */
34
 
34
 
35
#include <arch/debugger.h>
35
#include <arch/debugger.h>
36
#include <console/kconsole.h>
36
#include <console/kconsole.h>
37
#include <console/cmd.h>
37
#include <console/cmd.h>
38
#include <symtab.h>
38
#include <symtab.h>
39
#include <print.h>
39
#include <print.h>
40
#include <panic.h>
40
#include <panic.h>
41
#include <interrupt.h>
41
#include <interrupt.h>
42
#include <arch/asm.h>
42
#include <arch/asm.h>
43
#include <arch/cpu.h>
43
#include <arch/cpu.h>
44
#include <debug.h>
44
#include <debug.h>
45
#include <func.h>
45
#include <func.h>
46
#include <smp/ipi.h>
46
#include <smp/ipi.h>
47
 
47
 
48
typedef struct  {
48
typedef struct  {
49
    uintptr_t address;      /**< Breakpoint address */
49
    uintptr_t address;      /**< Breakpoint address */
50
    int flags;              /**< Flags regarding breakpoint */
50
    int flags;              /**< Flags regarding breakpoint */
51
    int counter;            /**< How many times the exception occured */
51
    int counter;            /**< How many times the exception occured */
52
} bpinfo_t;
52
} bpinfo_t;
53
 
53
 
54
static bpinfo_t breakpoints[BKPOINTS_MAX];
54
static bpinfo_t breakpoints[BKPOINTS_MAX];
55
SPINLOCK_INITIALIZE(bkpoint_lock);
55
SPINLOCK_INITIALIZE(bkpoint_lock);
56
 
56
 
57
static int cmd_print_breakpoints(cmd_arg_t *argv);
57
static int cmd_print_breakpoints(cmd_arg_t *argv);
58
static cmd_info_t bkpts_info = {
58
static cmd_info_t bkpts_info = {
59
    .name = "bkpts",
59
    .name = "bkpts",
60
    .description = "Print breakpoint table.",
60
    .description = "Print breakpoint table.",
61
    .func = cmd_print_breakpoints,
61
    .func = cmd_print_breakpoints,
62
    .argc = 0,
62
    .argc = 0,
63
};
63
};
64
 
64
 
65
#ifndef CONFIG_DEBUG_AS_WATCHPOINT
65
#ifndef CONFIG_DEBUG_AS_WATCHPOINT
66
 
66
 
67
static int cmd_del_breakpoint(cmd_arg_t *argv);
67
static int cmd_del_breakpoint(cmd_arg_t *argv);
68
static cmd_arg_t del_argv = {
68
static cmd_arg_t del_argv = {
69
    .type = ARG_TYPE_INT
69
    .type = ARG_TYPE_INT
70
};
70
};
71
static cmd_info_t delbkpt_info = {
71
static cmd_info_t delbkpt_info = {
72
    .name = "delbkpt",
72
    .name = "delbkpt",
73
    .description = "delbkpt <number> - Delete breakpoint.",
73
    .description = "delbkpt <number> - Delete breakpoint.",
74
    .func = cmd_del_breakpoint,
74
    .func = cmd_del_breakpoint,
75
    .argc = 1,
75
    .argc = 1,
76
    .argv = &del_argv
76
    .argv = &del_argv
77
};
77
};
78
 
78
 
79
static int cmd_add_breakpoint(cmd_arg_t *argv);
79
static int cmd_add_breakpoint(cmd_arg_t *argv);
80
static cmd_arg_t add_argv = {
80
static cmd_arg_t add_argv = {
81
    .type = ARG_TYPE_INT
81
    .type = ARG_TYPE_INT
82
};
82
};
83
static cmd_info_t addbkpt_info = {
83
static cmd_info_t addbkpt_info = {
84
    .name = "addbkpt",
84
    .name = "addbkpt",
85
    .description = "addbkpt <&symbol> - new breakpoint.",
85
    .description = "addbkpt <&symbol> - new breakpoint.",
86
    .func = cmd_add_breakpoint,
86
    .func = cmd_add_breakpoint,
87
    .argc = 1,
87
    .argc = 1,
88
    .argv = &add_argv
88
    .argv = &add_argv
89
};
89
};
90
 
90
 
91
static cmd_arg_t addw_argv = {
91
static cmd_arg_t addw_argv = {
92
    .type = ARG_TYPE_INT
92
    .type = ARG_TYPE_INT
93
};
93
};
94
static cmd_info_t addwatchp_info = {
94
static cmd_info_t addwatchp_info = {
95
    .name = "addwatchp",
95
    .name = "addwatchp",
96
    .description = "addbwatchp <&symbol> - new write watchpoint.",
96
    .description = "addbwatchp <&symbol> - new write watchpoint.",
97
    .func = cmd_add_breakpoint,
97
    .func = cmd_add_breakpoint,
98
    .argc = 1,
98
    .argc = 1,
99
    .argv = &addw_argv
99
    .argv = &addw_argv
100
};
100
};
101
 
101
 
102
#endif
102
#endif
103
 
103
 
104
/** Print table of active breakpoints */
104
/** Print table of active breakpoints */
105
int cmd_print_breakpoints(cmd_arg_t *argv __attribute__((unused)))
105
int cmd_print_breakpoints(cmd_arg_t *argv __attribute__((unused)))
106
{
106
{
107
    unsigned int i;
107
    unsigned int i;
108
    char *symbol;
108
    char *symbol;
109
   
109
 
110
    if (sizeof(void *) == 4) {
110
#ifdef __32_BITS__  
111
        printf("#  Count Address    In symbol\n");
111
    printf("#  Count Address    In symbol\n");
112
        printf("-- ----- ---------- ---------\n");
112
    printf("-- ----- ---------- ---------\n");
113
    } else {
113
#endif
-
 
114
 
-
 
115
#ifdef __64_BITS__
114
        printf("#  Count Address            In symbol\n");
116
    printf("#  Count Address            In symbol\n");
115
        printf("-- ----- ------------------ ---------\n");
117
    printf("-- ----- ------------------ ---------\n");
116
    }
118
#endif
117
   
119
   
118
    for (i = 0; i < BKPOINTS_MAX; i++)
120
    for (i = 0; i < BKPOINTS_MAX; i++)
119
        if (breakpoints[i].address) {
121
        if (breakpoints[i].address) {
120
            symbol = get_symtab_entry(breakpoints[i].address);
122
            symbol = get_symtab_entry(breakpoints[i].address);
121
           
123
 
122
            if (sizeof(void *) == 4)
124
#ifdef __32_BITS__
123
                printf("%-2u %-5d %#10zx %s\n", i, breakpoints[i].counter,
125
            printf("%-2u %-5d %#10zx %s\n", i,
124
                    breakpoints[i].address, symbol);
126
                breakpoints[i].counter, breakpoints[i].address,
-
 
127
                symbol);
125
            else
128
#endif
-
 
129
 
-
 
130
#ifdef __64_BITS__
126
                printf("%-2u %-5d %#18zx %s\n", i, breakpoints[i].counter,
131
            printf("%-2u %-5d %#18zx %s\n", i,
127
                    breakpoints[i].address, symbol);
132
                breakpoints[i].counter, breakpoints[i].address,
-
 
133
                symbol);
-
 
134
#endif
-
 
135
 
128
        }
136
        }
129
    return 1;
137
    return 1;
130
}
138
}
131
 
139
 
132
/* Setup DR register according to table */
140
/* Setup DR register according to table */
133
static void setup_dr(int curidx)
141
static void setup_dr(int curidx)
134
{
142
{
135
    unative_t dr7;
143
    unative_t dr7;
136
    bpinfo_t *cur = &breakpoints[curidx];
144
    bpinfo_t *cur = &breakpoints[curidx];
137
    int flags = breakpoints[curidx].flags;
145
    int flags = breakpoints[curidx].flags;
138
 
146
 
139
    /* Disable breakpoint in DR7 */
147
    /* Disable breakpoint in DR7 */
140
    dr7 = read_dr7();
148
    dr7 = read_dr7();
141
    dr7 &= ~(0x2 << (curidx*2));
149
    dr7 &= ~(0x2 << (curidx*2));
142
 
150
 
143
    if (cur->address) { /* Setup DR register */
151
    if (cur->address) { /* Setup DR register */
144
        /* Set breakpoint to debug registers */
152
        /* Set breakpoint to debug registers */
145
        switch (curidx) {
153
        switch (curidx) {
146
        case 0:
154
        case 0:
147
            write_dr0(cur->address);
155
            write_dr0(cur->address);
148
            break;
156
            break;
149
        case 1:
157
        case 1:
150
            write_dr1(cur->address);
158
            write_dr1(cur->address);
151
            break;
159
            break;
152
        case 2:
160
        case 2:
153
            write_dr2(cur->address);
161
            write_dr2(cur->address);
154
            break;
162
            break;
155
        case 3:
163
        case 3:
156
            write_dr3(cur->address);
164
            write_dr3(cur->address);
157
            break;
165
            break;
158
        }
166
        }
159
        /* Set type to requested breakpoint & length*/
167
        /* Set type to requested breakpoint & length*/
160
        dr7 &= ~ (0x3 << (16 + 4*curidx));
168
        dr7 &= ~ (0x3 << (16 + 4*curidx));
161
        dr7 &= ~ (0x3 << (18 + 4*curidx));
169
        dr7 &= ~ (0x3 << (18 + 4*curidx));
162
        if ((flags & BKPOINT_INSTR)) {
170
        if ((flags & BKPOINT_INSTR)) {
163
            ;
171
            ;
164
        } else {
172
        } else {
-
 
173
       
165
            if (sizeof(int) == 4)
174
#ifdef __32_BITS__
166
                dr7 |= ((unative_t) 0x3) << (18 + 4*curidx);
175
            dr7 |= ((unative_t) 0x3) << (18 + 4 * curidx);
-
 
176
#endif
-
 
177
 
167
            else /* 8 */
178
#ifdef __64_BITS__
168
                dr7 |= ((unative_t) 0x2) << (18 + 4*curidx);
179
            dr7 |= ((unative_t) 0x2) << (18 + 4 * curidx);
-
 
180
#endif
169
           
181
           
170
            if ((flags & BKPOINT_WRITE))
182
            if ((flags & BKPOINT_WRITE))
171
                dr7 |= ((unative_t) 0x1) << (16 + 4*curidx);
183
                dr7 |= ((unative_t) 0x1) << (16 + 4 * curidx);
172
            else if ((flags & BKPOINT_READ_WRITE))
184
            else if ((flags & BKPOINT_READ_WRITE))
173
                dr7 |= ((unative_t) 0x3) << (16 + 4*curidx);
185
                dr7 |= ((unative_t) 0x3) << (16 + 4 * curidx);
174
        }
186
        }
175
 
187
 
176
        /* Enable global breakpoint */
188
        /* Enable global breakpoint */
177
        dr7 |= 0x2 << (curidx*2);
189
        dr7 |= 0x2 << (curidx * 2);
178
 
190
 
179
        write_dr7(dr7);
191
        write_dr7(dr7);
180
       
192
       
181
    }
193
    }
182
}
194
}
183
   
195
   
184
/** Enable hardware breakpoint
196
/** Enable hardware breakpoint
185
 *
197
 *
186
 * @param where Address of HW breakpoint
198
 * @param where Address of HW breakpoint
187
 * @param flags Type of breakpoint (EXECUTE, WRITE)
199
 * @param flags Type of breakpoint (EXECUTE, WRITE)
188
 * @return Debug slot on success, -1 - no available HW breakpoint
200
 * @return Debug slot on success, -1 - no available HW breakpoint
189
 */
201
 */
190
int breakpoint_add(const void *where, const int flags, int curidx)
202
int breakpoint_add(const void *where, const int flags, int curidx)
191
{
203
{
192
    ipl_t ipl;
204
    ipl_t ipl;
193
    int i;
205
    int i;
194
    bpinfo_t *cur;
206
    bpinfo_t *cur;
195
 
207
 
196
    ASSERT(flags & (BKPOINT_INSTR | BKPOINT_WRITE | BKPOINT_READ_WRITE));
208
    ASSERT(flags & (BKPOINT_INSTR | BKPOINT_WRITE | BKPOINT_READ_WRITE));
197
 
209
 
198
    ipl = interrupts_disable();
210
    ipl = interrupts_disable();
199
    spinlock_lock(&bkpoint_lock);
211
    spinlock_lock(&bkpoint_lock);
200
   
212
   
201
    if (curidx == -1) {
213
    if (curidx == -1) {
202
        /* Find free space in slots */
214
        /* Find free space in slots */
203
        for (i = 0; i < BKPOINTS_MAX; i++)
215
        for (i = 0; i < BKPOINTS_MAX; i++)
204
            if (!breakpoints[i].address) {
216
            if (!breakpoints[i].address) {
205
                curidx = i;
217
                curidx = i;
206
                break;
218
                break;
207
            }
219
            }
208
        if (curidx == -1) {
220
        if (curidx == -1) {
209
            /* Too many breakpoints */
221
            /* Too many breakpoints */
210
            spinlock_unlock(&bkpoint_lock);
222
            spinlock_unlock(&bkpoint_lock);
211
            interrupts_restore(ipl);
223
            interrupts_restore(ipl);
212
            return -1;
224
            return -1;
213
        }
225
        }
214
    }
226
    }
215
    cur = &breakpoints[curidx];
227
    cur = &breakpoints[curidx];
216
 
228
 
217
    cur->address = (uintptr_t) where;
229
    cur->address = (uintptr_t) where;
218
    cur->flags = flags;
230
    cur->flags = flags;
219
    cur->counter = 0;
231
    cur->counter = 0;
220
 
232
 
221
    setup_dr(curidx);
233
    setup_dr(curidx);
222
 
234
 
223
    spinlock_unlock(&bkpoint_lock);
235
    spinlock_unlock(&bkpoint_lock);
224
    interrupts_restore(ipl);
236
    interrupts_restore(ipl);
225
 
237
 
226
    /* Send IPI */
238
    /* Send IPI */
227
#ifdef CONFIG_SMP
239
#ifdef CONFIG_SMP
228
//  ipi_broadcast(VECTOR_DEBUG_IPI);    
240
//  ipi_broadcast(VECTOR_DEBUG_IPI);    
229
#endif  
241
#endif  
230
 
242
 
231
    return curidx;
243
    return curidx;
232
}
244
}
233
 
245
 
234
#ifdef amd64
246
#ifdef amd64
235
#   define getip(x) ((x)->rip)
247
#   define getip(x) ((x)->rip)
236
#else
248
#else
237
#   define getip(x) ((x)->eip)
249
#   define getip(x) ((x)->eip)
238
#endif
250
#endif
239
 
251
 
240
static void handle_exception(int slot, istate_t *istate)
252
static void handle_exception(int slot, istate_t *istate)
241
{
253
{
242
    ASSERT(breakpoints[slot].address);
254
    ASSERT(breakpoints[slot].address);
243
 
255
 
244
    /* Handle zero checker */
256
    /* Handle zero checker */
245
    if (! (breakpoints[slot].flags & BKPOINT_INSTR)) {
257
    if (! (breakpoints[slot].flags & BKPOINT_INSTR)) {
246
        if ((breakpoints[slot].flags & BKPOINT_CHECK_ZERO)) {
258
        if ((breakpoints[slot].flags & BKPOINT_CHECK_ZERO)) {
247
            if (*((unative_t *) breakpoints[slot].address) != 0)
259
            if (*((unative_t *) breakpoints[slot].address) != 0)
248
                return;
260
                return;
249
            printf("**** Found ZERO on address %lx (slot %d) ****\n",
261
            printf("*** Found ZERO on address %lx (slot %d) ***\n",
250
                breakpoints[slot].address, slot);
262
                breakpoints[slot].address, slot);
251
        } else {
263
        } else {
252
            printf("Data watchpoint - new data: %lx\n",
264
            printf("Data watchpoint - new data: %lx\n",
253
                   *((unative_t *) breakpoints[slot].address));
265
                *((unative_t *) breakpoints[slot].address));
254
        }
266
        }
255
    }
267
    }
256
    printf("Reached breakpoint %d:%lx(%s)\n", slot, getip(istate),
268
    printf("Reached breakpoint %d:%lx(%s)\n", slot, getip(istate),
257
           get_symtab_entry(getip(istate)));
269
        get_symtab_entry(getip(istate)));
258
    printf("***Type 'exit' to exit kconsole.\n");
270
    printf("***Type 'exit' to exit kconsole.\n");
259
    atomic_set(&haltstate,1);
271
    atomic_set(&haltstate,1);
260
    kconsole((void *) "debug");
272
    kconsole((void *) "debug");
261
    atomic_set(&haltstate,0);
273
    atomic_set(&haltstate,0);
262
}
274
}
263
 
275
 
264
void breakpoint_del(int slot)
276
void breakpoint_del(int slot)
265
{
277
{
266
    bpinfo_t *cur;
278
    bpinfo_t *cur;
267
    ipl_t ipl;
279
    ipl_t ipl;
268
 
280
 
269
    ipl = interrupts_disable();
281
    ipl = interrupts_disable();
270
    spinlock_lock(&bkpoint_lock);
282
    spinlock_lock(&bkpoint_lock);
271
 
283
 
272
    cur = &breakpoints[slot];
284
    cur = &breakpoints[slot];
273
    if (!cur->address) {
285
    if (!cur->address) {
274
        spinlock_unlock(&bkpoint_lock);
286
        spinlock_unlock(&bkpoint_lock);
275
        interrupts_restore(ipl);
287
        interrupts_restore(ipl);
276
        return;
288
        return;
277
    }
289
    }
278
 
290
 
279
    cur->address = NULL;
291
    cur->address = NULL;
280
 
292
 
281
    setup_dr(slot);
293
    setup_dr(slot);
282
 
294
 
283
    spinlock_unlock(&bkpoint_lock);
295
    spinlock_unlock(&bkpoint_lock);
284
    interrupts_restore(ipl);
296
    interrupts_restore(ipl);
285
#ifdef CONFIG_SMP
297
#ifdef CONFIG_SMP
286
//  ipi_broadcast(VECTOR_DEBUG_IPI);    
298
//  ipi_broadcast(VECTOR_DEBUG_IPI);    
287
#endif
299
#endif
288
}
300
}
289
 
301
 
290
#ifndef CONFIG_DEBUG_AS_WATCHPOINT
302
#ifndef CONFIG_DEBUG_AS_WATCHPOINT
291
 
303
 
292
/** Remove breakpoint from table */
304
/** Remove breakpoint from table */
293
int cmd_del_breakpoint(cmd_arg_t *argv)
305
int cmd_del_breakpoint(cmd_arg_t *argv)
294
{
306
{
295
    unative_t bpno = argv->intval;
307
    unative_t bpno = argv->intval;
296
    if (bpno > BKPOINTS_MAX) {
308
    if (bpno > BKPOINTS_MAX) {
297
        printf("Invalid breakpoint number.\n");
309
        printf("Invalid breakpoint number.\n");
298
        return 0;
310
        return 0;
299
    }
311
    }
300
    breakpoint_del(argv->intval);
312
    breakpoint_del(argv->intval);
301
    return 1;
313
    return 1;
302
}
314
}
303
 
315
 
304
/** Add new breakpoint to table */
316
/** Add new breakpoint to table */
305
static int cmd_add_breakpoint(cmd_arg_t *argv)
317
static int cmd_add_breakpoint(cmd_arg_t *argv)
306
{
318
{
307
    int flags;
319
    int flags;
308
    int id;
320
    int id;
309
 
321
 
310
    if (argv == &add_argv) {
322
    if (argv == &add_argv) {
311
        flags = BKPOINT_INSTR;
323
        flags = BKPOINT_INSTR;
312
    } else { /* addwatchp */
324
    } else { /* addwatchp */
313
        flags = BKPOINT_WRITE;
325
        flags = BKPOINT_WRITE;
314
    }
326
    }
315
    printf("Adding breakpoint on address: %p\n", argv->intval);
327
    printf("Adding breakpoint on address: %p\n", argv->intval);
316
    id = breakpoint_add((void *)argv->intval, flags, -1);
328
    id = breakpoint_add((void *)argv->intval, flags, -1);
317
    if (id < 0)
329
    if (id < 0)
318
        printf("Add breakpoint failed.\n");
330
        printf("Add breakpoint failed.\n");
319
    else
331
    else
320
        printf("Added breakpoint %d.\n", id);
332
        printf("Added breakpoint %d.\n", id);
321
   
333
   
322
    return 1;
334
    return 1;
323
}
335
}
324
#endif
336
#endif
325
 
337
 
326
static void debug_exception(int n __attribute__((unused)), istate_t *istate)
338
static void debug_exception(int n __attribute__((unused)), istate_t *istate)
327
{
339
{
328
    unative_t dr6;
340
    unative_t dr6;
329
    int i;
341
    int i;
330
   
342
   
331
    dr6 = read_dr6();
343
    dr6 = read_dr6();
332
 
344
 
333
    if (dr6 & DR6_BS) {
345
    if (dr6 & DR6_BS) {
334
        if (THREAD && istate_from_uspace(istate)) {
346
        if (THREAD && istate_from_uspace(istate)) {
335
            /*
347
            /*
336
             * The userspace thread has TF set and executed
348
             * The userspace thread has TF set and executed
337
             * an instruction
349
             * an instruction
338
             */
350
             */
339
            udebug_trap_event(istate->eip);
351
            udebug_trap_event(istate->eip);
340
            return;
352
            return;
341
        }
353
        }
342
    }
354
    }
343
 
355
 
344
    /* Set RF to restart the instruction  */
356
    /* Set RF to restart the instruction  */
345
#ifdef amd64       
357
#ifdef amd64       
346
    istate->rflags |= RFLAGS_RF;
358
    istate->rflags |= RFLAGS_RF;
347
#else
359
#else
348
    istate->eflags |= EFLAGS_RF;
360
    istate->eflags |= EFLAGS_RF;
349
#endif
361
#endif
350
 
362
 
351
    for (i=0; i < BKPOINTS_MAX; i++) {
363
    for (i=0; i < BKPOINTS_MAX; i++) {
352
        if (dr6 & (1 << i)) {
364
        if (dr6 & (1 << i)) {
353
            dr6 &= ~ (1 << i);
365
            dr6 &= ~ (1 << i);
354
            write_dr6(dr6);
366
            write_dr6(dr6);
355
           
367
           
356
            handle_exception(i, istate);
368
            handle_exception(i, istate);
357
        }
369
        }
358
    }
370
    }
359
}
371
}
360
 
372
 
361
#ifdef CONFIG_SMP
373
#ifdef CONFIG_SMP
-
 
374
static void
-
 
375
debug_ipi(int n __attribute__((unused)),
362
static void debug_ipi(int n __attribute__((unused)), istate_t *istate __attribute__((unused)))
376
    istate_t *istate __attribute__((unused)))
363
{
377
{
364
    int i;
378
    int i;
365
 
379
 
366
    spinlock_lock(&bkpoint_lock);
380
    spinlock_lock(&bkpoint_lock);
367
    for (i = 0; i < BKPOINTS_MAX; i++)
381
    for (i = 0; i < BKPOINTS_MAX; i++)
368
        setup_dr(i);
382
        setup_dr(i);
369
    spinlock_unlock(&bkpoint_lock);
383
    spinlock_unlock(&bkpoint_lock);
370
}
384
}
371
#endif
385
#endif
372
 
386
 
373
/** Initialize debugger */
387
/** Initialize debugger */
374
void debugger_init()
388
void debugger_init()
375
{
389
{
376
    int i;
390
    int i;
377
 
391
 
378
    for (i=0; i<BKPOINTS_MAX; i++)
392
    for (i = 0; i < BKPOINTS_MAX; i++)
379
        breakpoints[i].address = NULL;
393
        breakpoints[i].address = NULL;
380
   
394
   
381
    cmd_initialize(&bkpts_info);
395
    cmd_initialize(&bkpts_info);
382
    if (!cmd_register(&bkpts_info))
396
    if (!cmd_register(&bkpts_info))
383
        panic("could not register command %s\n", bkpts_info.name);
397
        panic("could not register command %s\n", bkpts_info.name);
384
 
398
 
385
#ifndef CONFIG_DEBUG_AS_WATCHPOINT
399
#ifndef CONFIG_DEBUG_AS_WATCHPOINT
386
    cmd_initialize(&delbkpt_info);
400
    cmd_initialize(&delbkpt_info);
387
    if (!cmd_register(&delbkpt_info))
401
    if (!cmd_register(&delbkpt_info))
388
        panic("could not register command %s\n", delbkpt_info.name);
402
        panic("could not register command %s\n", delbkpt_info.name);
389
 
403
 
390
    cmd_initialize(&addbkpt_info);
404
    cmd_initialize(&addbkpt_info);
391
    if (!cmd_register(&addbkpt_info))
405
    if (!cmd_register(&addbkpt_info))
392
        panic("could not register command %s\n", addbkpt_info.name);
406
        panic("could not register command %s\n", addbkpt_info.name);
393
 
407
 
394
    cmd_initialize(&addwatchp_info);
408
    cmd_initialize(&addwatchp_info);
395
    if (!cmd_register(&addwatchp_info))
409
    if (!cmd_register(&addwatchp_info))
396
        panic("could not register command %s\n", addwatchp_info.name);
410
        panic("could not register command %s\n", addwatchp_info.name);
397
#endif
411
#endif
398
   
412
   
399
    exc_register(VECTOR_DEBUG, "debugger",
413
    exc_register(VECTOR_DEBUG, "debugger", debug_exception);
400
             debug_exception);
-
 
401
#ifdef CONFIG_SMP
414
#ifdef CONFIG_SMP
402
    exc_register(VECTOR_DEBUG_IPI, "debugger_smp",
415
    exc_register(VECTOR_DEBUG_IPI, "debugger_smp", debug_ipi);
403
             debug_ipi);
-
 
404
#endif
416
#endif
405
}
417
}
406
 
418
 
407
/** @}
419
/** @}
408
 */
420
 */
409
 
421