Subversion Repositories HelenOS

Rev

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