Subversion Repositories HelenOS

Rev

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

Rev Author Line No. Line
2924 svoboda 1
/*
2
 * Copyright (c) 2008 Jiri Svoboda
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
 
29
/** @addtogroup debug
30
 * @{
31
 */
32
/** @file
33
 */
34
 
35
#include <stdio.h>
36
#include <stdlib.h>
3093 svoboda 37
#include <assert.h>
2924 svoboda 38
#include <sys/types.h>
3099 svoboda 39
#include <errno.h>
2924 svoboda 40
#include <udebug.h>
41
 
2936 svoboda 42
#include "../../../cons.h"
2924 svoboda 43
#include "../../../main.h"
3005 svoboda 44
#include "../../../breakpoint.h"
2924 svoboda 45
#include "../../../include/arch.h"
3108 svoboda 46
#include "../../../genarch/idec/idec.h"
2924 svoboda 47
 
48
#define OPCODE_BREAK        0x0000000d
49
 
3012 svoboda 50
static istate_t istate;
3005 svoboda 51
 
3099 svoboda 52
typedef enum {
3100 svoboda 53
    /* Branches (conditional) */
54
    OP_BCzF,
55
    OP_BCzFL,
56
    OP_BCzT,
57
    OP_BCzTL,
58
    OP_BEQ,
59
    OP_BEQL,
60
    OP_BGEZ,
61
    OP_BGEZAL,
62
    OP_BGEZALL,
63
    OP_BGEZL,
64
    OP_BGTZ,
65
    OP_BGTZL,
66
    OP_BLEZ,
67
    OP_BLEZL,
68
    OP_BLTZ,
69
    OP_BLTZAL,
70
    OP_BLTZALL,
71
    OP_BLTZL,
72
    OP_BNE,
73
    OP_BNEL,
74
 
75
    /* Jumps (unconditional) */
3099 svoboda 76
    OP_J,
77
    OP_JAL,
78
    OP_JALR,
79
    OP_JR
80
} op_t;
81
 
82
typedef struct {
83
    uint32_t mask;
84
    uint32_t value;
85
    op_t op;
86
} instr_desc_t;
87
 
88
static instr_desc_t decoding_table[] = {
3100 svoboda 89
    { 0xf3ff0000, 0x41000000, OP_BCzF },
90
    { 0xf3ff0000, 0x41020000, OP_BCzFL },
91
    { 0xf3ff0000, 0x41010000, OP_BCzT },
92
    { 0xf3ff0000, 0x41030000, OP_BCzTL },
93
    { 0xfc000000, 0x10000000, OP_BEQ },
94
    { 0xfc000000, 0x50000000, OP_BEQL },
95
    { 0xfc1f0000, 0x04010000, OP_BGEZ },
96
    { 0xfc1f0000, 0x04110000, OP_BGEZAL },
97
    { 0xfc1f0000, 0x04130000, OP_BGEZALL },
98
    { 0xfc1f0000, 0x04030000, OP_BGEZL },
99
    { 0xfc1f0000, 0x1c000000, OP_BGTZ },
100
    { 0xfc1f0000, 0x5c000000, OP_BGTZL },
101
    { 0xfc1f0000, 0x18000000, OP_BLEZ },
102
    { 0xfc1f0000, 0x58000000, OP_BLEZL },
103
    { 0xfc1f0000, 0x04000000, OP_BLTZ },
104
    { 0xfc1f0000, 0x04100000, OP_BLTZAL },
105
    { 0xfc1f0000, 0x04120000, OP_BLTZALL },
106
    { 0xfc1f0000, 0x04020000, OP_BLTZL },
107
    { 0xfc000000, 0x14000000, OP_BNE },
108
    { 0xfc000000, 0x54000000, OP_BNEL },
109
 
3099 svoboda 110
    { 0xfc000000, 0x08000000, OP_J },
111
    { 0xfc000000, 0x0c000000, OP_JAL },
112
    { 0xfc1f07ff, 0x00000009, OP_JALR },
113
    { 0xfc1fffff, 0x00000008, OP_JR },
3100 svoboda 114
 
3099 svoboda 115
    { 0, 0, -1 }
116
};
117
 
3100 svoboda 118
void arch_dthread_initialize(dthread_t *dt)
119
{
120
    dt->arch.singlestep = false;
121
 
122
    bstore_initialize(&dt->arch.cur);
123
    bstore_initialize(&dt->arch.next[0]);
124
    bstore_initialize(&dt->arch.next[1]);
125
}
126
 
3005 svoboda 127
int arch_breakpoint_set(breakpoint_t *b)
2924 svoboda 128
{
3108 svoboda 129
    return idec_breakpoint_set(b);
2924 svoboda 130
}
131
 
3005 svoboda 132
int arch_breakpoint_remove(breakpoint_t *b)
133
{
3108 svoboda 134
    return idec_breakpoint_remove(b);
3005 svoboda 135
}
136
 
3099 svoboda 137
static int islot_read(uintptr_t addr, uint32_t *instr)
138
{
139
    int rc;
140
 
141
    rc = udebug_mem_read(app_phone, instr, addr, sizeof(uint32_t));
142
    if (rc != EOK) {
143
        cons_printf("Error reading memory address 0x%zx\n", addr);
144
    }
145
 
146
    return rc;
147
}
148
 
149
static op_t instr_decode(uint32_t instr)
150
{
151
    instr_desc_t *idesc;
152
 
153
    idesc = &decoding_table[0];
154
    while (idesc->op >= 0) {
155
        if ((instr & idesc->mask) == idesc->value)
156
            return idesc->op;
157
        ++idesc;
158
    }
159
 
160
    return -1;
161
}
162
 
3108 svoboda 163
static int get_reg(dthread_t *dt, int reg_no, uint32_t *value)
3099 svoboda 164
{
3108 svoboda 165
    int rc;
166
 
3099 svoboda 167
    cons_printf("get_reg...\n");
168
 
169
    if (reg_no == 0) {
170
        *value = 0;
171
        return 0;
172
    }
173
 
3108 svoboda 174
    rc = udebug_regs_read(app_phone, dt->hash, &istate);
175
    if (rc < 0) return rc;
176
 
3099 svoboda 177
    /* FIXME: ugly */
178
    *value = ((uint32_t *)&istate)[reg_no - 1];
179
    printf("get_reg ok (0x%08x)\n", *value);
180
 
181
    return 0;
182
}
183
 
184
/** Get address of the instruction that will be executed after the current one.
185
 *
186
 * Assumptions: addr == PC, *addr is not covered by a BREAK.
187
 *
3108 svoboda 188
 * @param dt        Dthread on which to operate.
3100 svoboda 189
 * @param addr      Address of an instruction.
190
 * @param buffer    Buffer for storing up to 2 addresses.
191
 * @return      Number of stored addresses or negative error code.
3099 svoboda 192
 */
3108 svoboda 193
int get_next_addr(dthread_t *dt, uintptr_t addr, uintptr_t *buffer)
3099 svoboda 194
{
195
    /* TODO: J[AL]R, branches and delay slots */
196
    uint32_t instr;
3100 svoboda 197
    int32_t offset;
3099 svoboda 198
    op_t op;
199
    int rc;
3100 svoboda 200
    int n;
3099 svoboda 201
 
202
    rc = islot_read(addr, &instr);
203
    if (rc != 0) return rc;
204
 
205
    op = instr_decode(instr);
206
 
207
    switch (op) {
3100 svoboda 208
    case OP_BCzF:
209
    case OP_BCzFL:
210
    case OP_BCzT:
211
    case OP_BCzTL:
212
    case OP_BEQ:
213
    case OP_BEQL:
214
    case OP_BGEZ:
215
    case OP_BGEZAL:
216
    case OP_BGEZALL:
217
    case OP_BGEZL:
218
    case OP_BGTZ:
219
    case OP_BGTZL:
220
    case OP_BLEZ:
221
    case OP_BLTZ:
222
    case OP_BLTZAL:
223
    case OP_BLTZALL:
224
    case OP_BLTZL:
225
    case OP_BNE:
226
    case OP_BNEL:
227
        /* Branch */
228
        offset = (int32_t)(int16_t)(instr & 0x0000ffff) << 2;
229
        buffer[0] = (addr + 4) + offset;    /* taken */
230
        buffer[1] = addr + 8;           /* not taken */
231
        n = 2;
232
        break;
233
 
3099 svoboda 234
    case OP_J:
235
    case OP_JAL:
3100 svoboda 236
        /* Immediate jump */
237
        buffer[0] =
3099 svoboda 238
            ((addr + 4) & 0xf0000000) |
239
            ((instr & 0x03ffffff) << 2);
3100 svoboda 240
        n = 1;
3099 svoboda 241
        break;
242
    case OP_JR:
243
    case OP_JALR:
3100 svoboda 244
        /* Register jump */
3108 svoboda 245
        rc = get_reg(dt, (instr >> 21) & 0x1f, &buffer[0]);
3100 svoboda 246
        n = 1;
3099 svoboda 247
        break;
248
    default:
249
        /* Regular instruction */  
3100 svoboda 250
        buffer[0] = addr + 4;
251
        n = 1;
3099 svoboda 252
        break;
253
    }
254
 
3100 svoboda 255
    return n;
3099 svoboda 256
}
257
 
3093 svoboda 258
void arch_event_breakpoint(thash_t thread_hash)
259
{
3108 svoboda 260
    idec_event_breakpoint(thread_hash);
3093 svoboda 261
}
262
 
2942 svoboda 263
void arch_event_trap(dthread_t *dt)
2924 svoboda 264
{
265
    /* Unused */
2942 svoboda 266
    (void)dt;
2924 svoboda 267
}
268
 
2941 svoboda 269
void arch_dump_regs(thash_t thash)
270
{
3093 svoboda 271
    /* TODO */
2941 svoboda 272
}
273
 
3093 svoboda 274
void arch_singlestep(dthread_t *dt)
2942 svoboda 275
{
3108 svoboda 276
    idec_singlestep(dt);
2942 svoboda 277
}
278
 
2924 svoboda 279
/** @}
280
 */