Subversion Repositories HelenOS

Rev

Rev 3790 | Rev 4101 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3790 Rev 3913
1
/*
1
/*
2
 * Copyright (c) 2005 Ondrej Palkovsky
2
 * Copyright (c) 2005 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 mips32debug
29
/** @addtogroup mips32debug
30
 * @{
30
 * @{
31
 */
31
 */
32
/** @file
32
/** @file
33
 */
33
 */
34
 
34
 
35
#include <arch/debugger.h>
35
#include <arch/debugger.h>
36
#include <arch/barrier.h>
36
#include <arch/barrier.h>
37
#include <memstr.h>
37
#include <memstr.h>
38
#include <console/kconsole.h>
38
#include <console/kconsole.h>
39
#include <console/cmd.h>
39
#include <console/cmd.h>
40
#include <symtab.h>
40
#include <symtab.h>
41
#include <print.h>
41
#include <print.h>
42
#include <panic.h>
42
#include <panic.h>
43
#include <arch.h>
43
#include <arch.h>
44
#include <arch/cp0.h>
44
#include <arch/cp0.h>
45
#include <func.h>
45
#include <func.h>
46
 
46
 
47
bpinfo_t breakpoints[BKPOINTS_MAX];
47
bpinfo_t breakpoints[BKPOINTS_MAX];
48
SPINLOCK_INITIALIZE(bkpoint_lock);
48
SPINLOCK_INITIALIZE(bkpoint_lock);
49
 
49
 
50
#ifdef CONFIG_KCONSOLE
50
#ifdef CONFIG_KCONSOLE
51
 
51
 
52
static int cmd_print_breakpoints(cmd_arg_t *argv);
52
static int cmd_print_breakpoints(cmd_arg_t *argv);
53
static cmd_info_t bkpts_info = {
53
static cmd_info_t bkpts_info = {
54
    .name = "bkpts",
54
    .name = "bkpts",
55
    .description = "Print breakpoint table.",
55
    .description = "Print breakpoint table.",
56
    .func = cmd_print_breakpoints,
56
    .func = cmd_print_breakpoints,
57
    .argc = 0,
57
    .argc = 0,
58
};
58
};
59
 
59
 
60
static int cmd_del_breakpoint(cmd_arg_t *argv);
60
static int cmd_del_breakpoint(cmd_arg_t *argv);
61
static cmd_arg_t del_argv = {
61
static cmd_arg_t del_argv = {
62
    .type = ARG_TYPE_INT
62
    .type = ARG_TYPE_INT
63
};
63
};
64
static cmd_info_t delbkpt_info = {
64
static cmd_info_t delbkpt_info = {
65
    .name = "delbkpt",
65
    .name = "delbkpt",
66
    .description = "delbkpt <number> - Delete breakpoint.",
66
    .description = "delbkpt <number> - Delete breakpoint.",
67
    .func = cmd_del_breakpoint,
67
    .func = cmd_del_breakpoint,
68
    .argc = 1,
68
    .argc = 1,
69
    .argv = &del_argv
69
    .argv = &del_argv
70
};
70
};
71
 
71
 
72
static int cmd_add_breakpoint(cmd_arg_t *argv);
72
static int cmd_add_breakpoint(cmd_arg_t *argv);
73
static cmd_arg_t add_argv = {
73
static cmd_arg_t add_argv = {
74
    .type = ARG_TYPE_INT
74
    .type = ARG_TYPE_INT
75
};
75
};
76
static cmd_info_t addbkpt_info = {
76
static cmd_info_t addbkpt_info = {
77
    .name = "addbkpt",
77
    .name = "addbkpt",
78
    .description = "addbkpt <&symbol> - new bkpoint. Break on J/Branch "
78
    .description = "addbkpt <&symbol> - new bkpoint. Break on J/Branch "
79
        "insts unsupported.",
79
        "insts unsupported.",
80
    .func = cmd_add_breakpoint,
80
    .func = cmd_add_breakpoint,
81
    .argc = 1,
81
    .argc = 1,
82
    .argv = &add_argv
82
    .argv = &add_argv
83
};
83
};
84
 
84
 
85
static cmd_arg_t adde_argv[] = {
85
static cmd_arg_t adde_argv[] = {
86
    { .type = ARG_TYPE_INT },
86
    { .type = ARG_TYPE_INT },
87
    { .type = ARG_TYPE_INT }
87
    { .type = ARG_TYPE_INT }
88
};
88
};
89
static cmd_info_t addbkpte_info = {
89
static cmd_info_t addbkpte_info = {
90
    .name = "addbkpte",
90
    .name = "addbkpte",
91
    .description = "addebkpte <&symbol> <&func> - new bkpoint. Call "
91
    .description = "addebkpte <&symbol> <&func> - new bkpoint. Call "
92
        "func(or Nothing if 0).",
92
        "func(or Nothing if 0).",
93
    .func = cmd_add_breakpoint,
93
    .func = cmd_add_breakpoint,
94
    .argc = 2,
94
    .argc = 2,
95
    .argv = adde_argv
95
    .argv = adde_argv
96
};
96
};
97
 
97
 
98
static struct {
98
static struct {
99
    uint32_t andmask;
99
    uint32_t andmask;
100
    uint32_t value;
100
    uint32_t value;
101
} jmpinstr[] = {
101
} jmpinstr[] = {
102
    {0xf3ff0000, 0x41000000}, /* BCzF */
102
    {0xf3ff0000, 0x41000000}, /* BCzF */
103
    {0xf3ff0000, 0x41020000}, /* BCzFL */
103
    {0xf3ff0000, 0x41020000}, /* BCzFL */
104
    {0xf3ff0000, 0x41010000}, /* BCzT */
104
    {0xf3ff0000, 0x41010000}, /* BCzT */
105
    {0xf3ff0000, 0x41030000}, /* BCzTL */
105
    {0xf3ff0000, 0x41030000}, /* BCzTL */
106
    {0xfc000000, 0x10000000}, /* BEQ */
106
    {0xfc000000, 0x10000000}, /* BEQ */
107
    {0xfc000000, 0x50000000}, /* BEQL */
107
    {0xfc000000, 0x50000000}, /* BEQL */
108
    {0xfc1f0000, 0x04010000}, /* BEQL */
108
    {0xfc1f0000, 0x04010000}, /* BEQL */
109
    {0xfc1f0000, 0x04110000}, /* BGEZAL */
109
    {0xfc1f0000, 0x04110000}, /* BGEZAL */
110
    {0xfc1f0000, 0x04130000}, /* BGEZALL */
110
    {0xfc1f0000, 0x04130000}, /* BGEZALL */
111
    {0xfc1f0000, 0x04030000}, /* BGEZL */
111
    {0xfc1f0000, 0x04030000}, /* BGEZL */
112
    {0xfc1f0000, 0x1c000000}, /* BGTZ */
112
    {0xfc1f0000, 0x1c000000}, /* BGTZ */
113
    {0xfc1f0000, 0x5c000000}, /* BGTZL */
113
    {0xfc1f0000, 0x5c000000}, /* BGTZL */
114
    {0xfc1f0000, 0x18000000}, /* BLEZ */
114
    {0xfc1f0000, 0x18000000}, /* BLEZ */
115
    {0xfc1f0000, 0x58000000}, /* BLEZL */
115
    {0xfc1f0000, 0x58000000}, /* BLEZL */
116
    {0xfc1f0000, 0x04000000}, /* BLTZ */
116
    {0xfc1f0000, 0x04000000}, /* BLTZ */
117
    {0xfc1f0000, 0x04100000}, /* BLTZAL */
117
    {0xfc1f0000, 0x04100000}, /* BLTZAL */
118
    {0xfc1f0000, 0x04120000}, /* BLTZALL */
118
    {0xfc1f0000, 0x04120000}, /* BLTZALL */
119
    {0xfc1f0000, 0x04020000}, /* BLTZL */
119
    {0xfc1f0000, 0x04020000}, /* BLTZL */
120
    {0xfc000000, 0x14000000}, /* BNE */
120
    {0xfc000000, 0x14000000}, /* BNE */
121
    {0xfc000000, 0x54000000}, /* BNEL */
121
    {0xfc000000, 0x54000000}, /* BNEL */
122
    {0xfc000000, 0x08000000}, /* J */
122
    {0xfc000000, 0x08000000}, /* J */
123
    {0xfc000000, 0x0c000000}, /* JAL */
123
    {0xfc000000, 0x0c000000}, /* JAL */
124
    {0xfc1f07ff, 0x00000009}, /* JALR */
124
    {0xfc1f07ff, 0x00000009}, /* JALR */
125
    {0, 0} /* EndOfTable */
125
    {0, 0} /* EndOfTable */
126
};
126
};
127
 
127
 
128
 
128
 
129
/** Test, if the given instruction is a jump or branch instruction
129
/** Test, if the given instruction is a jump or branch instruction
130
 *
130
 *
131
 * @param instr Instruction code
131
 * @param instr Instruction code
132
 * @return true - it is jump instruction, false otherwise
132
 * @return true - it is jump instruction, false otherwise
133
 *
133
 *
134
 */
134
 */
135
static bool is_jump(unative_t instr)
135
static bool is_jump(unative_t instr)
136
{
136
{
137
    int i;
137
    int i;
138
 
138
 
139
    for (i = 0; jmpinstr[i].andmask; i++) {
139
    for (i = 0; jmpinstr[i].andmask; i++) {
140
        if ((instr & jmpinstr[i].andmask) == jmpinstr[i].value)
140
        if ((instr & jmpinstr[i].andmask) == jmpinstr[i].value)
141
            return true;
141
            return true;
142
    }
142
    }
143
 
143
 
144
    return false;
144
    return false;
145
}
145
}
146
 
146
 
147
/** Add new breakpoint to table */
147
/** Add new breakpoint to table */
148
int cmd_add_breakpoint(cmd_arg_t *argv)
148
int cmd_add_breakpoint(cmd_arg_t *argv)
149
{
149
{
150
    bpinfo_t *cur = NULL;
150
    bpinfo_t *cur = NULL;
151
    ipl_t ipl;
151
    ipl_t ipl;
152
    int i;
152
    int i;
153
 
153
 
154
    if (argv->intval & 0x3) {
154
    if (argv->intval & 0x3) {
155
        printf("Not aligned instruction, forgot to use &symbol?\n");
155
        printf("Not aligned instruction, forgot to use &symbol?\n");
156
        return 1;
156
        return 1;
157
    }
157
    }
158
    ipl = interrupts_disable();
158
    ipl = interrupts_disable();
159
    spinlock_lock(&bkpoint_lock);
159
    spinlock_lock(&bkpoint_lock);
160
 
160
 
161
    /* Check, that the breakpoints do not conflict */
161
    /* Check, that the breakpoints do not conflict */
162
    for (i = 0; i < BKPOINTS_MAX; i++) {
162
    for (i = 0; i < BKPOINTS_MAX; i++) {
163
        if (breakpoints[i].address == (uintptr_t)argv->intval) {
163
        if (breakpoints[i].address == (uintptr_t)argv->intval) {
164
            printf("Duplicate breakpoint %d.\n", i);
164
            printf("Duplicate breakpoint %d.\n", i);
165
            spinlock_unlock(&bkpoints_lock);
165
            spinlock_unlock(&bkpoint_lock);
166
            return 0;
166
            return 0;
167
        } else if (breakpoints[i].address == (uintptr_t)argv->intval +
167
        } else if (breakpoints[i].address == (uintptr_t)argv->intval +
168
            sizeof(unative_t) || breakpoints[i].address ==
168
            sizeof(unative_t) || breakpoints[i].address ==
169
            (uintptr_t)argv->intval - sizeof(unative_t)) {
169
            (uintptr_t)argv->intval - sizeof(unative_t)) {
170
            printf("Adjacent breakpoints not supported, conflict "
170
            printf("Adjacent breakpoints not supported, conflict "
171
                "with %d.\n", i);
171
                "with %d.\n", i);
172
            spinlock_unlock(&bkpoints_lock);
172
            spinlock_unlock(&bkpoint_lock);
173
            return 0;
173
            return 0;
174
        }
174
        }
175
           
175
       
176
    }
176
    }
177
 
177
 
178
    for (i = 0; i < BKPOINTS_MAX; i++)
178
    for (i = 0; i < BKPOINTS_MAX; i++)
179
        if (!breakpoints[i].address) {
179
        if (!breakpoints[i].address) {
180
            cur = &breakpoints[i];
180
            cur = &breakpoints[i];
181
            break;
181
            break;
182
        }
182
        }
183
    if (!cur) {
183
    if (!cur) {
184
        printf("Too many breakpoints.\n");
184
        printf("Too many breakpoints.\n");
185
        spinlock_unlock(&bkpoint_lock);
185
        spinlock_unlock(&bkpoint_lock);
186
        interrupts_restore(ipl);
186
        interrupts_restore(ipl);
187
        return 0;
187
        return 0;
188
    }
188
    }
189
    cur->address = (uintptr_t) argv->intval;
189
    cur->address = (uintptr_t) argv->intval;
190
    printf("Adding breakpoint on address: %p\n", argv->intval);
190
    printf("Adding breakpoint on address: %p\n", argv->intval);
191
    cur->instruction = ((unative_t *)cur->address)[0];
191
    cur->instruction = ((unative_t *)cur->address)[0];
192
    cur->nextinstruction = ((unative_t *)cur->address)[1];
192
    cur->nextinstruction = ((unative_t *)cur->address)[1];
193
    if (argv == &add_argv) {
193
    if (argv == &add_argv) {
194
        cur->flags = 0;
194
        cur->flags = 0;
195
    } else { /* We are add extended */
195
    } else { /* We are add extended */
196
        cur->flags = BKPOINT_FUNCCALL;
196
        cur->flags = BKPOINT_FUNCCALL;
197
        cur->bkfunc = (void (*)(void *, istate_t *)) argv[1].intval;
197
        cur->bkfunc = (void (*)(void *, istate_t *)) argv[1].intval;
198
    }
198
    }
199
    if (is_jump(cur->instruction))
199
    if (is_jump(cur->instruction))
200
        cur->flags |= BKPOINT_ONESHOT;
200
        cur->flags |= BKPOINT_ONESHOT;
201
    cur->counter = 0;
201
    cur->counter = 0;
202
 
202
 
203
    /* Set breakpoint */
203
    /* Set breakpoint */
204
    *((unative_t *)cur->address) = 0x0d;
204
    *((unative_t *)cur->address) = 0x0d;
205
    smc_coherence(cur->address);
205
    smc_coherence(cur->address);
206
 
206
 
207
    spinlock_unlock(&bkpoint_lock);
207
    spinlock_unlock(&bkpoint_lock);
208
    interrupts_restore(ipl);
208
    interrupts_restore(ipl);
209
 
209
 
210
    return 1;
210
    return 1;
211
}
211
}
212
 
212
 
213
/** Remove breakpoint from table */
213
/** Remove breakpoint from table */
214
int cmd_del_breakpoint(cmd_arg_t *argv)
214
int cmd_del_breakpoint(cmd_arg_t *argv)
215
{
215
{
216
    bpinfo_t *cur;
216
    bpinfo_t *cur;
217
    ipl_t ipl;
217
    ipl_t ipl;
218
 
218
 
219
    if (argv->intval > BKPOINTS_MAX) {
219
    if (argv->intval > BKPOINTS_MAX) {
220
        printf("Invalid breakpoint number.\n");
220
        printf("Invalid breakpoint number.\n");
221
        return 0;
221
        return 0;
222
    }
222
    }
223
    ipl = interrupts_disable();
223
    ipl = interrupts_disable();
224
    spinlock_lock(&bkpoint_lock);
224
    spinlock_lock(&bkpoint_lock);
225
 
225
 
226
    cur = &breakpoints[argv->intval];
226
    cur = &breakpoints[argv->intval];
227
    if (!cur->address) {
227
    if (!cur->address) {
228
        printf("Breakpoint does not exist.\n");
228
        printf("Breakpoint does not exist.\n");
229
        spinlock_unlock(&bkpoint_lock);
229
        spinlock_unlock(&bkpoint_lock);
230
        interrupts_restore(ipl);
230
        interrupts_restore(ipl);
231
        return 0;
231
        return 0;
232
    }
232
    }
233
    if ((cur->flags & BKPOINT_INPROG) && (cur->flags & BKPOINT_ONESHOT)) {
233
    if ((cur->flags & BKPOINT_INPROG) && (cur->flags & BKPOINT_ONESHOT)) {
234
        printf("Cannot remove one-shot breakpoint in-progress\n");
234
        printf("Cannot remove one-shot breakpoint in-progress\n");
235
        spinlock_unlock(&bkpoint_lock);
235
        spinlock_unlock(&bkpoint_lock);
236
        interrupts_restore(ipl);
236
        interrupts_restore(ipl);
237
        return 0;
237
        return 0;
238
    }
238
    }
239
    ((uint32_t *)cur->address)[0] = cur->instruction;
239
    ((uint32_t *)cur->address)[0] = cur->instruction;
240
    smc_coherence(((uint32_t *)cur->address)[0]);
240
    smc_coherence(((uint32_t *)cur->address)[0]);
241
    ((uint32_t *)cur->address)[1] = cur->nextinstruction;
241
    ((uint32_t *)cur->address)[1] = cur->nextinstruction;
242
    smc_coherence(((uint32_t *)cur->address)[1]);
242
    smc_coherence(((uint32_t *)cur->address)[1]);
243
 
243
 
244
    cur->address = NULL;
244
    cur->address = NULL;
245
 
245
 
246
    spinlock_unlock(&bkpoint_lock);
246
    spinlock_unlock(&bkpoint_lock);
247
    interrupts_restore(ipl);
247
    interrupts_restore(ipl);
248
    return 1;
248
    return 1;
249
}
249
}
250
 
250
 
251
/** Print table of active breakpoints */
251
/** Print table of active breakpoints */
252
int cmd_print_breakpoints(cmd_arg_t *argv)
252
int cmd_print_breakpoints(cmd_arg_t *argv)
253
{
253
{
254
    unsigned int i;
254
    unsigned int i;
255
    char *symbol;
255
    char *symbol;
256
   
256
   
257
    printf("#  Count Address    INPROG ONESHOT FUNCCALL In symbol\n");
257
    printf("#  Count Address    INPROG ONESHOT FUNCCALL In symbol\n");
258
    printf("-- ----- ---------- ------ ------- -------- ---------\n");
258
    printf("-- ----- ---------- ------ ------- -------- ---------\n");
259
   
259
   
260
    for (i = 0; i < BKPOINTS_MAX; i++)
260
    for (i = 0; i < BKPOINTS_MAX; i++)
261
        if (breakpoints[i].address) {
261
        if (breakpoints[i].address) {
262
            symbol = get_symtab_entry(breakpoints[i].address);
262
            symbol = get_symtab_entry(breakpoints[i].address);
263
           
263
           
264
            printf("%-2u %-5d %#10zx %-6s %-7s %-8s %s\n", i,
264
            printf("%-2u %-5d %#10zx %-6s %-7s %-8s %s\n", i,
265
                breakpoints[i].counter, breakpoints[i].address,
265
                breakpoints[i].counter, breakpoints[i].address,
266
                ((breakpoints[i].flags & BKPOINT_INPROG) ? "true" :
266
                ((breakpoints[i].flags & BKPOINT_INPROG) ? "true" :
267
                "false"), ((breakpoints[i].flags & BKPOINT_ONESHOT)
267
                "false"), ((breakpoints[i].flags & BKPOINT_ONESHOT)
268
                ? "true" : "false"), ((breakpoints[i].flags &
268
                ? "true" : "false"), ((breakpoints[i].flags &
269
                BKPOINT_FUNCCALL) ? "true" : "false"), symbol);
269
                BKPOINT_FUNCCALL) ? "true" : "false"), symbol);
270
        }
270
        }
271
    return 1;
271
    return 1;
272
}
272
}
273
 
273
 
274
#endif
274
#endif
275
 
275
 
276
/** Initialize debugger */
276
/** Initialize debugger */
277
void debugger_init()
277
void debugger_init()
278
{
278
{
279
    int i;
279
    int i;
280
 
280
 
281
    for (i = 0; i < BKPOINTS_MAX; i++)
281
    for (i = 0; i < BKPOINTS_MAX; i++)
282
        breakpoints[i].address = NULL;
282
        breakpoints[i].address = NULL;
283
 
283
 
284
#ifdef CONFIG_KCONSOLE
284
#ifdef CONFIG_KCONSOLE
285
    cmd_initialize(&bkpts_info);
285
    cmd_initialize(&bkpts_info);
286
    if (!cmd_register(&bkpts_info))
286
    if (!cmd_register(&bkpts_info))
287
        printf("Cannot register command %s\n", bkpts_info.name);
287
        printf("Cannot register command %s\n", bkpts_info.name);
288
 
288
 
289
    cmd_initialize(&delbkpt_info);
289
    cmd_initialize(&delbkpt_info);
290
    if (!cmd_register(&delbkpt_info))
290
    if (!cmd_register(&delbkpt_info))
291
        printf("Cannot register command %s\n", delbkpt_info.name);
291
        printf("Cannot register command %s\n", delbkpt_info.name);
292
 
292
 
293
    cmd_initialize(&addbkpt_info);
293
    cmd_initialize(&addbkpt_info);
294
    if (!cmd_register(&addbkpt_info))
294
    if (!cmd_register(&addbkpt_info))
295
        printf("Cannot register command %s\n", addbkpt_info.name);
295
        printf("Cannot register command %s\n", addbkpt_info.name);
296
 
296
 
297
    cmd_initialize(&addbkpte_info);
297
    cmd_initialize(&addbkpte_info);
298
    if (!cmd_register(&addbkpte_info))
298
    if (!cmd_register(&addbkpte_info))
299
        printf("Cannot register command %s\n", addbkpte_info.name);
299
        printf("Cannot register command %s\n", addbkpte_info.name);
300
#endif
300
#endif
301
}
301
}
302
 
302
 
303
/** Handle breakpoint
303
/** Handle breakpoint
304
 *
304
 *
305
 * Find breakpoint in breakpoint table.
305
 * Find breakpoint in breakpoint table.
306
 * If found, call kconsole, set break on next instruction and reexecute.
306
 * If found, call kconsole, set break on next instruction and reexecute.
307
 * If we are on "next instruction", set it back on the first and reexecute.
307
 * If we are on "next instruction", set it back on the first and reexecute.
308
 * If breakpoint not found in breakpoint table, call kconsole and start
308
 * If breakpoint not found in breakpoint table, call kconsole and start
309
 * next instruction.
309
 * next instruction.
310
 */
310
 */
311
void debugger_bpoint(istate_t *istate)
311
void debugger_bpoint(istate_t *istate)
312
{
312
{
313
    bpinfo_t *cur = NULL;
313
    bpinfo_t *cur = NULL;
314
    uintptr_t fireaddr = istate->epc;
314
    uintptr_t fireaddr = istate->epc;
315
    int i;
315
    int i;
316
 
316
 
317
    /* test branch delay slot */
317
    /* test branch delay slot */
318
    if (cp0_cause_read() & 0x80000000)
318
    if (cp0_cause_read() & 0x80000000)
319
        panic("Breakpoint in branch delay slot not supported.");
319
        panic("Breakpoint in branch delay slot not supported.");
320
 
320
 
321
    spinlock_lock(&bkpoint_lock);
321
    spinlock_lock(&bkpoint_lock);
322
    for (i = 0; i < BKPOINTS_MAX; i++) {
322
    for (i = 0; i < BKPOINTS_MAX; i++) {
323
        /* Normal breakpoint */
323
        /* Normal breakpoint */
324
        if (fireaddr == breakpoints[i].address &&
324
        if (fireaddr == breakpoints[i].address &&
325
            !(breakpoints[i].flags & BKPOINT_REINST)) {
325
            !(breakpoints[i].flags & BKPOINT_REINST)) {
326
            cur = &breakpoints[i];
326
            cur = &breakpoints[i];
327
            break;
327
            break;
328
        }
328
        }
329
        /* Reinst only breakpoint */
329
        /* Reinst only breakpoint */
330
        if ((breakpoints[i].flags & BKPOINT_REINST) &&
330
        if ((breakpoints[i].flags & BKPOINT_REINST) &&
331
            (fireaddr == breakpoints[i].address + sizeof(unative_t))) {
331
            (fireaddr == breakpoints[i].address + sizeof(unative_t))) {
332
            cur = &breakpoints[i];
332
            cur = &breakpoints[i];
333
            break;
333
            break;
334
        }
334
        }
335
    }
335
    }
336
    if (cur) {
336
    if (cur) {
337
        if (cur->flags & BKPOINT_REINST) {
337
        if (cur->flags & BKPOINT_REINST) {
338
            /* Set breakpoint on first instruction */
338
            /* Set breakpoint on first instruction */
339
            ((uint32_t *)cur->address)[0] = 0x0d;
339
            ((uint32_t *)cur->address)[0] = 0x0d;
340
            smc_coherence(((uint32_t *)cur->address)[0]);
340
            smc_coherence(((uint32_t *)cur->address)[0]);
341
            /* Return back the second */
341
            /* Return back the second */
342
            ((uint32_t *)cur->address)[1] = cur->nextinstruction;
342
            ((uint32_t *)cur->address)[1] = cur->nextinstruction;
343
            smc_coherence(((uint32_t *)cur->address)[1]);
343
            smc_coherence(((uint32_t *)cur->address)[1]);
344
            cur->flags &= ~BKPOINT_REINST;
344
            cur->flags &= ~BKPOINT_REINST;
345
            spinlock_unlock(&bkpoint_lock);
345
            spinlock_unlock(&bkpoint_lock);
346
            return;
346
            return;
347
        }
347
        }
348
        if (cur->flags & BKPOINT_INPROG)
348
        if (cur->flags & BKPOINT_INPROG)
349
            printf("Warning: breakpoint recursion\n");
349
            printf("Warning: breakpoint recursion\n");
350
       
350
       
351
        if (!(cur->flags & BKPOINT_FUNCCALL))
351
        if (!(cur->flags & BKPOINT_FUNCCALL))
352
            printf("***Breakpoint %d: %p in %s.\n", i, fireaddr,
352
            printf("***Breakpoint %d: %p in %s.\n", i, fireaddr,
353
                get_symtab_entry(istate->epc));
353
                get_symtab_entry(istate->epc));
354
 
354
 
355
        /* Return first instruction back */
355
        /* Return first instruction back */
356
        ((uint32_t *)cur->address)[0] = cur->instruction;
356
        ((uint32_t *)cur->address)[0] = cur->instruction;
357
        smc_coherence(cur->address);
357
        smc_coherence(cur->address);
358
 
358
 
359
        if (! (cur->flags & BKPOINT_ONESHOT)) {
359
        if (! (cur->flags & BKPOINT_ONESHOT)) {
360
            /* Set Breakpoint on next instruction */
360
            /* Set Breakpoint on next instruction */
361
            ((uint32_t *)cur->address)[1] = 0x0d;
361
            ((uint32_t *)cur->address)[1] = 0x0d;
362
            cur->flags |= BKPOINT_REINST;
362
            cur->flags |= BKPOINT_REINST;
363
        }
363
        }
364
        cur->flags |= BKPOINT_INPROG;
364
        cur->flags |= BKPOINT_INPROG;
365
    } else {
365
    } else {
366
        printf("***Breakpoint %p in %s.\n", fireaddr,
366
        printf("***Breakpoint %p in %s.\n", fireaddr,
367
               get_symtab_entry(fireaddr));
367
               get_symtab_entry(fireaddr));
368
        /* Move on to next instruction */
368
        /* Move on to next instruction */
369
        istate->epc += 4;
369
        istate->epc += 4;
370
    }
370
    }
371
    if (cur)
371
    if (cur)
372
        cur->counter++;
372
        cur->counter++;
373
    if (cur && (cur->flags & BKPOINT_FUNCCALL)) {
373
    if (cur && (cur->flags & BKPOINT_FUNCCALL)) {
374
        /* Allow zero bkfunc, just for counting */
374
        /* Allow zero bkfunc, just for counting */
375
        if (cur->bkfunc)
375
        if (cur->bkfunc)
376
            cur->bkfunc(cur, istate);
376
            cur->bkfunc(cur, istate);
377
    } else {
377
    } else {
378
#ifdef CONFIG_KCONSOLE
378
#ifdef CONFIG_KCONSOLE
379
        /* This disables all other processors - we are not SMP,
379
        /* This disables all other processors - we are not SMP,
380
         * actually this gets us to cpu_halt, if scheduler() is run
380
         * actually this gets us to cpu_halt, if scheduler() is run
381
         * - we generally do not want scheduler to be run from debug,
381
         * - we generally do not want scheduler to be run from debug,
382
         *   so this is a good idea
382
         *   so this is a good idea
383
         */
383
         */
384
        atomic_set(&haltstate, 1);
384
        atomic_set(&haltstate, 1);
385
        spinlock_unlock(&bkpoint_lock);
385
        spinlock_unlock(&bkpoint_lock);
386
       
386
       
387
        kconsole("debug", "Debug console ready (type 'exit' to continue)\n", false);
387
        kconsole("debug", "Debug console ready (type 'exit' to continue)\n", false);
388
       
388
       
389
        spinlock_lock(&bkpoint_lock);
389
        spinlock_lock(&bkpoint_lock);
390
        atomic_set(&haltstate, 0);
390
        atomic_set(&haltstate, 0);
391
#endif
391
#endif
392
    }
392
    }
393
    if (cur && cur->address == fireaddr && (cur->flags & BKPOINT_INPROG)) {
393
    if (cur && cur->address == fireaddr && (cur->flags & BKPOINT_INPROG)) {
394
        /* Remove one-shot breakpoint */
394
        /* Remove one-shot breakpoint */
395
        if ((cur->flags & BKPOINT_ONESHOT))
395
        if ((cur->flags & BKPOINT_ONESHOT))
396
            cur->address = NULL;
396
            cur->address = NULL;
397
        /* Remove in-progress flag */
397
        /* Remove in-progress flag */
398
        cur->flags &= ~BKPOINT_INPROG;
398
        cur->flags &= ~BKPOINT_INPROG;
399
    }
399
    }
400
    spinlock_unlock(&bkpoint_lock);
400
    spinlock_unlock(&bkpoint_lock);
401
}
401
}
402
 
402
 
403
/** @}
403
/** @}
404
 */
404
 */
405
 
405