Subversion Repositories HelenOS

Rev

Rev 4101 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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