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