Commit | Line | Data |
---|---|---|
49d45b20 JB |
1 | /* Target-dependent code for FT32. |
2 | ||
3 | Copyright (C) 2009-2015 Free Software Foundation, Inc. | |
4 | ||
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 3 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | #include "defs.h" | |
21 | #include "frame.h" | |
22 | #include "frame-unwind.h" | |
23 | #include "frame-base.h" | |
24 | #include "symtab.h" | |
25 | #include "gdbtypes.h" | |
26 | #include "gdbcmd.h" | |
27 | #include "gdbcore.h" | |
28 | #include "value.h" | |
29 | #include "inferior.h" | |
30 | #include "symfile.h" | |
31 | #include "objfiles.h" | |
32 | #include "osabi.h" | |
33 | #include "language.h" | |
34 | #include "arch-utils.h" | |
35 | #include "regcache.h" | |
36 | #include "trad-frame.h" | |
37 | #include "dis-asm.h" | |
38 | #include "record.h" | |
39 | ||
40 | #include "gdb_assert.h" | |
41 | ||
42 | #include "ft32-tdep.h" | |
43 | #include "gdb/sim-ft32.h" | |
44 | ||
45 | #define RAM_BIAS 0x800000 /* Bias added to RAM addresses. */ | |
46 | ||
47 | /* Local functions. */ | |
48 | ||
49 | extern void _initialize_ft32_tdep (void); | |
50 | ||
51 | /* Use an invalid address -1 as 'not available' marker. */ | |
52 | enum { REG_UNAVAIL = (CORE_ADDR) (-1) }; | |
53 | ||
54 | struct ft32_frame_cache | |
55 | { | |
56 | /* Base address of the frame */ | |
57 | CORE_ADDR base; | |
58 | /* Function this frame belongs to */ | |
59 | CORE_ADDR pc; | |
60 | /* Total size of this frame */ | |
61 | LONGEST framesize; | |
62 | /* Saved registers in this frame */ | |
63 | CORE_ADDR saved_regs[FT32_NUM_REGS]; | |
64 | /* Saved SP in this frame */ | |
65 | CORE_ADDR saved_sp; | |
66 | /* Has the new frame been LINKed. */ | |
67 | bfd_boolean established; | |
68 | }; | |
69 | ||
70 | /* Implement the "frame_align" gdbarch method. */ | |
71 | ||
72 | static CORE_ADDR | |
73 | ft32_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp) | |
74 | { | |
75 | /* Align to the size of an instruction (so that they can safely be | |
76 | pushed onto the stack. */ | |
77 | return sp & ~1; | |
78 | } | |
79 | ||
80 | /* Implement the "breakpoint_from_pc" gdbarch method. */ | |
81 | ||
82 | static const unsigned char * | |
83 | ft32_breakpoint_from_pc (struct gdbarch *gdbarch, | |
84 | CORE_ADDR *pcptr, int *lenptr) | |
85 | { | |
86 | static const gdb_byte breakpoint[] = { 0x02, 0x00, 0x34, 0x00 }; | |
87 | ||
88 | *lenptr = sizeof (breakpoint); | |
89 | return breakpoint; | |
90 | } | |
91 | ||
92 | /* FT32 register names. */ | |
93 | ||
94 | static const char *const ft32_register_names[] = | |
95 | { | |
96 | "fp", "sp", | |
97 | "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", | |
98 | "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15", | |
99 | "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23", | |
100 | "r24", "r25", "r26", "r27", "r28", "cc", | |
101 | "pc" | |
102 | }; | |
103 | ||
104 | /* Implement the "register_name" gdbarch method. */ | |
105 | ||
106 | static const char * | |
107 | ft32_register_name (struct gdbarch *gdbarch, int reg_nr) | |
108 | { | |
109 | if (reg_nr < 0) | |
110 | return NULL; | |
111 | if (reg_nr >= FT32_NUM_REGS) | |
112 | return NULL; | |
113 | return ft32_register_names[reg_nr]; | |
114 | } | |
115 | ||
116 | /* Implement the "register_type" gdbarch method. */ | |
117 | ||
118 | static struct type * | |
119 | ft32_register_type (struct gdbarch *gdbarch, int reg_nr) | |
120 | { | |
121 | if (reg_nr == FT32_PC_REGNUM) | |
122 | return builtin_type (gdbarch)->builtin_func_ptr; | |
123 | else if (reg_nr == FT32_SP_REGNUM || reg_nr == FT32_FP_REGNUM) | |
124 | return builtin_type (gdbarch)->builtin_data_ptr; | |
125 | else | |
126 | return builtin_type (gdbarch)->builtin_int32; | |
127 | } | |
128 | ||
129 | /* Write into appropriate registers a function return value | |
130 | of type TYPE, given in virtual format. */ | |
131 | ||
132 | static void | |
133 | ft32_store_return_value (struct type *type, struct regcache *regcache, | |
134 | const gdb_byte *valbuf) | |
135 | { | |
136 | struct gdbarch *gdbarch = get_regcache_arch (regcache); | |
137 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); | |
138 | CORE_ADDR regval; | |
139 | int len = TYPE_LENGTH (type); | |
140 | ||
141 | /* Things always get returned in RET1_REGNUM, RET2_REGNUM. */ | |
142 | regval = extract_unsigned_integer (valbuf, len > 4 ? 4 : len, byte_order); | |
143 | regcache_cooked_write_unsigned (regcache, FT32_R0_REGNUM, regval); | |
144 | if (len > 4) | |
145 | { | |
146 | regval = extract_unsigned_integer (valbuf + 4, | |
147 | len - 4, byte_order); | |
148 | regcache_cooked_write_unsigned (regcache, FT32_R1_REGNUM, regval); | |
149 | } | |
150 | } | |
151 | ||
152 | /* Decode the instructions within the given address range. Decide | |
153 | when we must have reached the end of the function prologue. If a | |
154 | frame_info pointer is provided, fill in its saved_regs etc. | |
155 | ||
156 | Returns the address of the first instruction after the prologue. */ | |
157 | ||
158 | #define IS_PUSH(inst) (((inst) & 0xfff00000) == 0x84000000) | |
159 | #define PUSH_REG(inst) (FT32_R0_REGNUM + (((inst) >> 15) & 0x1f)) | |
160 | #define IS_LINK(inst) (((inst) & 0xffff0000) == 0x95d00000) | |
161 | #define LINK_SIZE(inst) ((inst) & 0xffff) | |
162 | ||
163 | static CORE_ADDR | |
164 | ft32_analyze_prologue (CORE_ADDR start_addr, CORE_ADDR end_addr, | |
165 | struct ft32_frame_cache *cache, | |
166 | struct gdbarch *gdbarch) | |
167 | { | |
168 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); | |
169 | CORE_ADDR next_addr; | |
170 | ULONGEST inst, inst2; | |
171 | LONGEST offset; | |
172 | int regnum; | |
173 | ||
174 | cache->saved_regs[FT32_PC_REGNUM] = 0; | |
175 | cache->framesize = 0; | |
176 | ||
177 | if (start_addr >= end_addr) | |
178 | return end_addr; | |
179 | ||
180 | cache->established = 0; | |
181 | for (next_addr = start_addr; next_addr < end_addr; ) | |
182 | { | |
183 | inst = read_memory_unsigned_integer (next_addr, 4, byte_order); | |
184 | ||
185 | if (IS_PUSH (inst)) | |
186 | { | |
187 | regnum = PUSH_REG (inst); | |
188 | cache->framesize += 4; | |
189 | cache->saved_regs[regnum] = cache->framesize; | |
190 | next_addr += 4; | |
191 | } | |
192 | else | |
193 | break; | |
194 | } | |
195 | for (regnum = FT32_R0_REGNUM; regnum < FT32_PC_REGNUM; regnum++) | |
196 | { | |
197 | if (cache->saved_regs[regnum] != REG_UNAVAIL) | |
198 | cache->saved_regs[regnum] = cache->framesize - cache->saved_regs[regnum]; | |
199 | } | |
200 | cache->saved_regs[FT32_PC_REGNUM] = cache->framesize; | |
201 | ||
202 | /* It is a LINK? */ | |
203 | if (next_addr < end_addr) | |
204 | { | |
205 | inst = read_memory_unsigned_integer (next_addr, 4, byte_order); | |
206 | if (IS_LINK (inst)) | |
207 | { | |
208 | cache->established = 1; | |
209 | for (regnum = FT32_R0_REGNUM; regnum < FT32_PC_REGNUM; regnum++) | |
210 | { | |
211 | if (cache->saved_regs[regnum] != REG_UNAVAIL) | |
212 | cache->saved_regs[regnum] += 4; | |
213 | } | |
214 | cache->saved_regs[FT32_PC_REGNUM] = cache->framesize + 4; | |
215 | cache->saved_regs[FT32_FP_REGNUM] = 0; | |
216 | cache->framesize += LINK_SIZE (inst); | |
217 | next_addr += 4; | |
218 | } | |
219 | } | |
220 | ||
221 | return next_addr; | |
222 | } | |
223 | ||
224 | /* Find the end of function prologue. */ | |
225 | ||
226 | static CORE_ADDR | |
227 | ft32_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc) | |
228 | { | |
229 | CORE_ADDR func_addr = 0, func_end = 0; | |
230 | const char *func_name; | |
231 | ||
232 | /* See if we can determine the end of the prologue via the symbol table. | |
233 | If so, then return either PC, or the PC after the prologue, whichever | |
234 | is greater. */ | |
235 | if (find_pc_partial_function (pc, &func_name, &func_addr, &func_end)) | |
236 | { | |
237 | CORE_ADDR post_prologue_pc | |
238 | = skip_prologue_using_sal (gdbarch, func_addr); | |
239 | if (post_prologue_pc != 0) | |
240 | return max (pc, post_prologue_pc); | |
241 | else | |
242 | { | |
243 | /* Can't determine prologue from the symbol table, need to examine | |
244 | instructions. */ | |
245 | struct symtab_and_line sal; | |
246 | struct symbol *sym; | |
247 | struct ft32_frame_cache cache; | |
248 | CORE_ADDR plg_end; | |
249 | ||
250 | memset (&cache, 0, sizeof cache); | |
251 | ||
252 | plg_end = ft32_analyze_prologue (func_addr, | |
253 | func_end, &cache, gdbarch); | |
254 | /* Found a function. */ | |
255 | sym = lookup_symbol (func_name, NULL, VAR_DOMAIN, NULL); | |
256 | /* Don't use line number debug info for assembly source files. */ | |
257 | if ((sym != NULL) && SYMBOL_LANGUAGE (sym) != language_asm) | |
258 | { | |
259 | sal = find_pc_line (func_addr, 0); | |
260 | if (sal.end && sal.end < func_end) | |
261 | { | |
262 | /* Found a line number, use it as end of prologue. */ | |
263 | return sal.end; | |
264 | } | |
265 | } | |
266 | /* No useable line symbol. Use result of prologue parsing method. */ | |
267 | return plg_end; | |
268 | } | |
269 | } | |
270 | ||
271 | /* No function symbol -- just return the PC. */ | |
272 | return pc; | |
273 | } | |
274 | ||
275 | /* Implement the "read_pc" gdbarch method. */ | |
276 | ||
277 | static CORE_ADDR | |
278 | ft32_read_pc (struct regcache *regcache) | |
279 | { | |
280 | ULONGEST pc; | |
281 | ||
282 | regcache_cooked_read_unsigned (regcache, FT32_PC_REGNUM, &pc); | |
283 | return pc; | |
284 | } | |
285 | ||
286 | /* Implement the "write_pc" gdbarch method. */ | |
287 | ||
288 | static void | |
289 | ft32_write_pc (struct regcache *regcache, CORE_ADDR val) | |
290 | { | |
291 | regcache_cooked_write_unsigned (regcache, FT32_PC_REGNUM, val); | |
292 | } | |
293 | ||
294 | /* Implement the "unwind_sp" gdbarch method. */ | |
295 | ||
296 | static CORE_ADDR | |
297 | ft32_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame) | |
298 | { | |
299 | return frame_unwind_register_unsigned (next_frame, FT32_SP_REGNUM); | |
300 | } | |
301 | ||
302 | /* Given a return value in `regbuf' with a type `valtype', | |
303 | extract and copy its value into `valbuf'. */ | |
304 | ||
305 | static void | |
306 | ft32_extract_return_value (struct type *type, struct regcache *regcache, | |
307 | gdb_byte *dst) | |
308 | { | |
309 | struct gdbarch *gdbarch = get_regcache_arch (regcache); | |
310 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); | |
311 | bfd_byte *valbuf = dst; | |
312 | int len = TYPE_LENGTH (type); | |
313 | ULONGEST tmp; | |
314 | ||
315 | /* By using store_unsigned_integer we avoid having to do | |
316 | anything special for small big-endian values. */ | |
317 | regcache_cooked_read_unsigned (regcache, FT32_R0_REGNUM, &tmp); | |
318 | store_unsigned_integer (valbuf, (len > 4 ? len - 4 : len), byte_order, tmp); | |
319 | ||
320 | /* Ignore return values more than 8 bytes in size because the ft32 | |
321 | returns anything more than 8 bytes in the stack. */ | |
322 | if (len > 4) | |
323 | { | |
324 | regcache_cooked_read_unsigned (regcache, FT32_R1_REGNUM, &tmp); | |
325 | store_unsigned_integer (valbuf + len - 4, 4, byte_order, tmp); | |
326 | } | |
327 | } | |
328 | ||
329 | /* Implement the "return_value" gdbarch method. */ | |
330 | ||
331 | static enum return_value_convention | |
332 | ft32_return_value (struct gdbarch *gdbarch, struct value *function, | |
333 | struct type *valtype, struct regcache *regcache, | |
334 | gdb_byte *readbuf, const gdb_byte *writebuf) | |
335 | { | |
336 | if (TYPE_LENGTH (valtype) > 8) | |
337 | return RETURN_VALUE_STRUCT_CONVENTION; | |
338 | else | |
339 | { | |
340 | if (readbuf != NULL) | |
341 | ft32_extract_return_value (valtype, regcache, readbuf); | |
342 | if (writebuf != NULL) | |
343 | ft32_store_return_value (valtype, regcache, writebuf); | |
344 | return RETURN_VALUE_REGISTER_CONVENTION; | |
345 | } | |
346 | } | |
347 | ||
348 | /* Allocate and initialize a ft32_frame_cache object. */ | |
349 | ||
350 | static struct ft32_frame_cache * | |
351 | ft32_alloc_frame_cache (void) | |
352 | { | |
353 | struct ft32_frame_cache *cache; | |
354 | int i; | |
355 | ||
356 | cache = FRAME_OBSTACK_ZALLOC (struct ft32_frame_cache); | |
357 | ||
358 | for (i = 0; i < FT32_NUM_REGS; ++i) | |
359 | cache->saved_regs[i] = REG_UNAVAIL; | |
360 | ||
361 | return cache; | |
362 | } | |
363 | ||
364 | /* Populate a ft32_frame_cache object for this_frame. */ | |
365 | ||
366 | static struct ft32_frame_cache * | |
367 | ft32_frame_cache (struct frame_info *this_frame, void **this_cache) | |
368 | { | |
369 | struct ft32_frame_cache *cache; | |
370 | CORE_ADDR current_pc; | |
371 | int i; | |
372 | ||
373 | if (*this_cache) | |
374 | return *this_cache; | |
375 | ||
376 | cache = ft32_alloc_frame_cache (); | |
377 | *this_cache = cache; | |
378 | ||
379 | cache->base = get_frame_register_unsigned (this_frame, FT32_FP_REGNUM); | |
380 | if (cache->base == 0) | |
381 | return cache; | |
382 | ||
383 | cache->pc = get_frame_func (this_frame); | |
384 | current_pc = get_frame_pc (this_frame); | |
385 | if (cache->pc) | |
386 | { | |
387 | struct gdbarch *gdbarch = get_frame_arch (this_frame); | |
388 | ||
389 | ft32_analyze_prologue (cache->pc, current_pc, cache, gdbarch); | |
390 | if (!cache->established) | |
391 | cache->base = get_frame_register_unsigned (this_frame, FT32_SP_REGNUM); | |
392 | } | |
393 | ||
394 | cache->saved_sp = cache->base - 4; | |
395 | ||
396 | for (i = 0; i < FT32_NUM_REGS; ++i) | |
397 | if (cache->saved_regs[i] != REG_UNAVAIL) | |
398 | cache->saved_regs[i] = cache->base + cache->saved_regs[i]; | |
399 | ||
400 | return cache; | |
401 | } | |
402 | ||
403 | /* Implement the "unwind_pc" gdbarch method. */ | |
404 | ||
405 | static CORE_ADDR | |
406 | ft32_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame) | |
407 | { | |
408 | return frame_unwind_register_unsigned (next_frame, FT32_PC_REGNUM); | |
409 | } | |
410 | ||
411 | /* Given a GDB frame, determine the address of the calling function's | |
412 | frame. This will be used to create a new GDB frame struct. */ | |
413 | ||
414 | static void | |
415 | ft32_frame_this_id (struct frame_info *this_frame, | |
416 | void **this_prologue_cache, struct frame_id *this_id) | |
417 | { | |
418 | struct ft32_frame_cache *cache = ft32_frame_cache (this_frame, | |
419 | this_prologue_cache); | |
420 | ||
421 | /* This marks the outermost frame. */ | |
422 | if (cache->base == 0) | |
423 | return; | |
424 | ||
425 | *this_id = frame_id_build (cache->saved_sp, cache->pc); | |
426 | } | |
427 | ||
428 | /* Get the value of register regnum in the previous stack frame. */ | |
429 | ||
430 | static struct value * | |
431 | ft32_frame_prev_register (struct frame_info *this_frame, | |
432 | void **this_prologue_cache, int regnum) | |
433 | { | |
434 | struct ft32_frame_cache *cache = ft32_frame_cache (this_frame, | |
435 | this_prologue_cache); | |
436 | ||
437 | gdb_assert (regnum >= 0); | |
438 | ||
439 | if (regnum == FT32_SP_REGNUM && cache->saved_sp) | |
440 | return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp); | |
441 | ||
442 | if (regnum < FT32_NUM_REGS && cache->saved_regs[regnum] != REG_UNAVAIL) | |
443 | return frame_unwind_got_memory (this_frame, regnum, | |
444 | RAM_BIAS | cache->saved_regs[regnum]); | |
445 | ||
446 | return frame_unwind_got_register (this_frame, regnum, regnum); | |
447 | } | |
448 | ||
449 | static const struct frame_unwind ft32_frame_unwind = | |
450 | { | |
451 | NORMAL_FRAME, | |
452 | default_frame_unwind_stop_reason, | |
453 | ft32_frame_this_id, | |
454 | ft32_frame_prev_register, | |
455 | NULL, | |
456 | default_frame_sniffer | |
457 | }; | |
458 | ||
459 | /* Return the base address of this_frame. */ | |
460 | ||
461 | static CORE_ADDR | |
462 | ft32_frame_base_address (struct frame_info *this_frame, void **this_cache) | |
463 | { | |
464 | struct ft32_frame_cache *cache = ft32_frame_cache (this_frame, | |
465 | this_cache); | |
466 | ||
467 | return cache->base; | |
468 | } | |
469 | ||
470 | static const struct frame_base ft32_frame_base = | |
471 | { | |
472 | &ft32_frame_unwind, | |
473 | ft32_frame_base_address, | |
474 | ft32_frame_base_address, | |
475 | ft32_frame_base_address | |
476 | }; | |
477 | ||
478 | static struct frame_id | |
479 | ft32_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame) | |
480 | { | |
481 | CORE_ADDR sp = get_frame_register_unsigned (this_frame, FT32_SP_REGNUM); | |
482 | ||
483 | return frame_id_build (sp, get_frame_pc (this_frame)); | |
484 | } | |
485 | ||
486 | /* Allocate and initialize the ft32 gdbarch object. */ | |
487 | ||
488 | static struct gdbarch * | |
489 | ft32_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches) | |
490 | { | |
491 | struct gdbarch *gdbarch; | |
492 | struct gdbarch_tdep *tdep; | |
493 | ||
494 | /* If there is already a candidate, use it. */ | |
495 | arches = gdbarch_list_lookup_by_info (arches, &info); | |
496 | if (arches != NULL) | |
497 | return arches->gdbarch; | |
498 | ||
499 | /* Allocate space for the new architecture. */ | |
500 | tdep = XNEW (struct gdbarch_tdep); | |
501 | gdbarch = gdbarch_alloc (&info, tdep); | |
502 | ||
503 | set_gdbarch_read_pc (gdbarch, ft32_read_pc); | |
504 | set_gdbarch_write_pc (gdbarch, ft32_write_pc); | |
505 | set_gdbarch_unwind_sp (gdbarch, ft32_unwind_sp); | |
506 | ||
507 | set_gdbarch_num_regs (gdbarch, FT32_NUM_REGS); | |
508 | set_gdbarch_sp_regnum (gdbarch, FT32_SP_REGNUM); | |
509 | set_gdbarch_pc_regnum (gdbarch, FT32_PC_REGNUM); | |
510 | set_gdbarch_register_name (gdbarch, ft32_register_name); | |
511 | set_gdbarch_register_type (gdbarch, ft32_register_type); | |
512 | ||
513 | set_gdbarch_return_value (gdbarch, ft32_return_value); | |
514 | ||
515 | set_gdbarch_skip_prologue (gdbarch, ft32_skip_prologue); | |
516 | set_gdbarch_inner_than (gdbarch, core_addr_lessthan); | |
517 | set_gdbarch_breakpoint_from_pc (gdbarch, ft32_breakpoint_from_pc); | |
518 | set_gdbarch_frame_align (gdbarch, ft32_frame_align); | |
519 | ||
520 | frame_base_set_default (gdbarch, &ft32_frame_base); | |
521 | ||
522 | /* Methods for saving / extracting a dummy frame's ID. The ID's | |
523 | stack address must match the SP value returned by | |
524 | PUSH_DUMMY_CALL, and saved by generic_save_dummy_frame_tos. */ | |
525 | set_gdbarch_dummy_id (gdbarch, ft32_dummy_id); | |
526 | ||
527 | set_gdbarch_unwind_pc (gdbarch, ft32_unwind_pc); | |
528 | ||
529 | set_gdbarch_print_insn (gdbarch, print_insn_ft32); | |
530 | ||
531 | /* Hook in ABI-specific overrides, if they have been registered. */ | |
532 | gdbarch_init_osabi (info, gdbarch); | |
533 | ||
534 | /* Hook in the default unwinders. */ | |
535 | frame_unwind_append_unwinder (gdbarch, &ft32_frame_unwind); | |
536 | ||
537 | /* Support simple overlay manager. */ | |
538 | set_gdbarch_overlay_update (gdbarch, simple_overlay_update); | |
539 | ||
540 | return gdbarch; | |
541 | } | |
542 | ||
543 | /* Register this machine's init routine. */ | |
544 | ||
545 | void | |
546 | _initialize_ft32_tdep (void) | |
547 | { | |
548 | register_gdbarch_init (bfd_arch_ft32, ft32_gdbarch_init); | |
549 | } |