microblaze-tdep.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. /* Target-dependent code for Xilinx MicroBlaze.
  2. Copyright (C) 2009-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "arch-utils.h"
  16. #include "dis-asm.h"
  17. #include "frame.h"
  18. #include "trad-frame.h"
  19. #include "symtab.h"
  20. #include "value.h"
  21. #include "gdbcmd.h"
  22. #include "breakpoint.h"
  23. #include "inferior.h"
  24. #include "regcache.h"
  25. #include "target.h"
  26. #include "frame-base.h"
  27. #include "frame-unwind.h"
  28. #include "dwarf2/frame.h"
  29. #include "osabi.h"
  30. #include "target-descriptions.h"
  31. #include "opcodes/microblaze-opcm.h"
  32. #include "opcodes/microblaze-dis.h"
  33. #include "microblaze-tdep.h"
  34. #include "remote.h"
  35. #include "features/microblaze-with-stack-protect.c"
  36. #include "features/microblaze.c"
  37. /* Instruction macros used for analyzing the prologue. */
  38. /* This set of instruction macros need to be changed whenever the
  39. prologue generated by the compiler could have more instructions or
  40. different type of instructions.
  41. This set also needs to be verified if it is complete. */
  42. #define IS_RETURN(op) (op == rtsd || op == rtid)
  43. #define IS_UPDATE_SP(op, rd, ra) \
  44. ((op == addik || op == addi) && rd == REG_SP && ra == REG_SP)
  45. #define IS_SPILL_SP(op, rd, ra) \
  46. ((op == swi || op == sw) && rd == REG_SP && ra == REG_SP)
  47. #define IS_SPILL_REG(op, rd, ra) \
  48. ((op == swi || op == sw) && rd != REG_SP && ra == REG_SP)
  49. #define IS_ALSO_SPILL_REG(op, rd, ra, rb) \
  50. ((op == swi || op == sw) && rd != REG_SP && ra == 0 && rb == REG_SP)
  51. #define IS_SETUP_FP(op, ra, rb) \
  52. ((op == add || op == addik || op == addk) && ra == REG_SP && rb == 0)
  53. #define IS_SPILL_REG_FP(op, rd, ra, fpregnum) \
  54. ((op == swi || op == sw) && rd != REG_SP && ra == fpregnum && ra != 0)
  55. #define IS_SAVE_HIDDEN_PTR(op, rd, ra, rb) \
  56. ((op == add || op == addik) && ra == MICROBLAZE_FIRST_ARGREG && rb == 0)
  57. /* The registers of the Xilinx microblaze processor. */
  58. static const char * const microblaze_register_names[] =
  59. {
  60. "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  61. "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  62. "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
  63. "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
  64. "rpc", "rmsr", "rear", "resr", "rfsr", "rbtr",
  65. "rpvr0", "rpvr1", "rpvr2", "rpvr3", "rpvr4", "rpvr5", "rpvr6",
  66. "rpvr7", "rpvr8", "rpvr9", "rpvr10", "rpvr11",
  67. "redr", "rpid", "rzpr", "rtlbx", "rtlbsx", "rtlblo", "rtlbhi",
  68. "rslr", "rshr"
  69. };
  70. #define MICROBLAZE_NUM_REGS ARRAY_SIZE (microblaze_register_names)
  71. static unsigned int microblaze_debug_flag = 0;
  72. #define microblaze_debug(fmt, ...) \
  73. debug_prefixed_printf_cond_nofunc (microblaze_debug_flag, "MICROBLAZE", \
  74. fmt, ## __VA_ARGS__)
  75. /* Return the name of register REGNUM. */
  76. static const char *
  77. microblaze_register_name (struct gdbarch *gdbarch, int regnum)
  78. {
  79. if (regnum >= 0 && regnum < MICROBLAZE_NUM_REGS)
  80. return microblaze_register_names[regnum];
  81. return NULL;
  82. }
  83. static struct type *
  84. microblaze_register_type (struct gdbarch *gdbarch, int regnum)
  85. {
  86. if (regnum == MICROBLAZE_SP_REGNUM)
  87. return builtin_type (gdbarch)->builtin_data_ptr;
  88. if (regnum == MICROBLAZE_PC_REGNUM)
  89. return builtin_type (gdbarch)->builtin_func_ptr;
  90. return builtin_type (gdbarch)->builtin_int;
  91. }
  92. /* Fetch the instruction at PC. */
  93. static unsigned long
  94. microblaze_fetch_instruction (CORE_ADDR pc)
  95. {
  96. enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  97. gdb_byte buf[4];
  98. /* If we can't read the instruction at PC, return zero. */
  99. if (target_read_code (pc, buf, sizeof (buf)))
  100. return 0;
  101. return extract_unsigned_integer (buf, 4, byte_order);
  102. }
  103. constexpr gdb_byte microblaze_break_insn[] = MICROBLAZE_BREAKPOINT;
  104. typedef BP_MANIPULATION (microblaze_break_insn) microblaze_breakpoint;
  105. /* Allocate and initialize a frame cache. */
  106. static struct microblaze_frame_cache *
  107. microblaze_alloc_frame_cache (void)
  108. {
  109. struct microblaze_frame_cache *cache;
  110. cache = FRAME_OBSTACK_ZALLOC (struct microblaze_frame_cache);
  111. /* Base address. */
  112. cache->base = 0;
  113. cache->pc = 0;
  114. /* Frameless until proven otherwise. */
  115. cache->frameless_p = 1;
  116. return cache;
  117. }
  118. /* The base of the current frame is actually in the stack pointer.
  119. This happens when there is no frame pointer (microblaze ABI does not
  120. require a frame pointer) or when we're stopped in the prologue or
  121. epilogue itself. In these cases, microblaze_analyze_prologue will need
  122. to update fi->frame before returning or analyzing the register
  123. save instructions. */
  124. #define MICROBLAZE_MY_FRAME_IN_SP 0x1
  125. /* The base of the current frame is in a frame pointer register.
  126. This register is noted in frame_extra_info->fp_regnum.
  127. Note that the existance of an FP might also indicate that the
  128. function has called alloca. */
  129. #define MICROBLAZE_MY_FRAME_IN_FP 0x2
  130. /* Function prologues on the Xilinx microblaze processors consist of:
  131. - adjustments to the stack pointer (r1) (addi r1, r1, imm)
  132. - making a copy of r1 into another register (a "frame" pointer)
  133. (add r?, r1, r0)
  134. - store word/multiples that use r1 or the frame pointer as the
  135. base address (swi r?, r1, imm OR swi r?, fp, imm)
  136. Note that microblaze really doesn't have a real frame pointer.
  137. Instead, the compiler may copy the SP into a register (usually
  138. r19) to act as an arg pointer. For our target-dependent purposes,
  139. the frame info's "frame" member will be the beginning of the
  140. frame. The SP could, in fact, point below this.
  141. The prologue ends when an instruction fails to meet either of
  142. these criteria. */
  143. /* Analyze the prologue to determine where registers are saved,
  144. the end of the prologue, etc. Return the address of the first line
  145. of "real" code (i.e., the end of the prologue). */
  146. static CORE_ADDR
  147. microblaze_analyze_prologue (struct gdbarch *gdbarch, CORE_ADDR pc,
  148. CORE_ADDR current_pc,
  149. struct microblaze_frame_cache *cache)
  150. {
  151. const char *name;
  152. CORE_ADDR func_addr, func_end, addr, stop, prologue_end_addr = 0;
  153. unsigned long insn;
  154. int rd, ra, rb, imm;
  155. enum microblaze_instr op;
  156. int save_hidden_pointer_found = 0;
  157. int non_stack_instruction_found = 0;
  158. /* Find the start of this function. */
  159. find_pc_partial_function (pc, &name, &func_addr, &func_end);
  160. if (func_addr < pc)
  161. pc = func_addr;
  162. if (current_pc < pc)
  163. return current_pc;
  164. /* Initialize info about frame. */
  165. cache->framesize = 0;
  166. cache->fp_regnum = MICROBLAZE_SP_REGNUM;
  167. cache->frameless_p = 1;
  168. /* Start decoding the prologue. We start by checking two special cases:
  169. 1. We're about to return
  170. 2. We're at the first insn of the prologue.
  171. If we're about to return, our frame has already been deallocated.
  172. If we are stopped at the first instruction of a prologue,
  173. then our frame has not yet been set up. */
  174. /* Get the first insn from memory. */
  175. insn = microblaze_fetch_instruction (pc);
  176. op = microblaze_decode_insn (insn, &rd, &ra, &rb, &imm);
  177. if (IS_RETURN(op))
  178. return pc;
  179. /* Start at beginning of function and analyze until we get to the
  180. current pc, or the end of the function, whichever is first. */
  181. stop = (current_pc < func_end ? current_pc : func_end);
  182. microblaze_debug ("Scanning prologue: name=%s, func_addr=%s, stop=%s\n",
  183. name, paddress (gdbarch, func_addr),
  184. paddress (gdbarch, stop));
  185. for (addr = func_addr; addr < stop; addr += INST_WORD_SIZE)
  186. {
  187. insn = microblaze_fetch_instruction (addr);
  188. op = microblaze_decode_insn (insn, &rd, &ra, &rb, &imm);
  189. microblaze_debug ("%s %08lx\n", paddress (gdbarch, pc), insn);
  190. /* This code is very sensitive to what functions are present in the
  191. prologue. It assumes that the (addi, addik, swi, sw) can be the
  192. only instructions in the prologue. */
  193. if (IS_UPDATE_SP(op, rd, ra))
  194. {
  195. microblaze_debug ("got addi r1,r1,%d; contnuing\n", imm);
  196. if (cache->framesize)
  197. break; /* break if framesize already computed. */
  198. cache->framesize = -imm; /* stack grows towards low memory. */
  199. cache->frameless_p = 0; /* Frame found. */
  200. save_hidden_pointer_found = 0;
  201. non_stack_instruction_found = 0;
  202. continue;
  203. }
  204. else if (IS_SPILL_SP(op, rd, ra))
  205. {
  206. /* Spill stack pointer. */
  207. cache->register_offsets[rd] = imm; /* SP spilled before updating. */
  208. microblaze_debug ("swi r1 r1 %d, continuing\n", imm);
  209. save_hidden_pointer_found = 0;
  210. if (!cache->framesize)
  211. non_stack_instruction_found = 0;
  212. continue;
  213. }
  214. else if (IS_SPILL_REG(op, rd, ra))
  215. {
  216. /* Spill register. */
  217. cache->register_offsets[rd] = imm - cache->framesize;
  218. microblaze_debug ("swi %d r1 %d, continuing\n", rd, imm);
  219. save_hidden_pointer_found = 0;
  220. if (!cache->framesize)
  221. non_stack_instruction_found = 0;
  222. continue;
  223. }
  224. else if (IS_ALSO_SPILL_REG(op, rd, ra, rb))
  225. {
  226. /* Spill register. */
  227. cache->register_offsets[rd] = 0 - cache->framesize;
  228. microblaze_debug ("sw %d r0 r1, continuing\n", rd);
  229. save_hidden_pointer_found = 0;
  230. if (!cache->framesize)
  231. non_stack_instruction_found = 0;
  232. continue;
  233. }
  234. else if (IS_SETUP_FP(op, ra, rb))
  235. {
  236. /* We have a frame pointer. Note the register which is
  237. acting as the frame pointer. */
  238. cache->fp_regnum = rd;
  239. microblaze_debug ("Found a frame pointer: r%d\n", cache->fp_regnum);
  240. save_hidden_pointer_found = 0;
  241. if (!cache->framesize)
  242. non_stack_instruction_found = 0;
  243. continue;
  244. }
  245. else if (IS_SPILL_REG_FP(op, rd, ra, cache->fp_regnum))
  246. {
  247. /* reg spilled after updating. */
  248. cache->register_offsets[rd] = imm - cache->framesize;
  249. microblaze_debug ("swi %d %d %d, continuing\n", rd, ra, imm);
  250. save_hidden_pointer_found = 0;
  251. if (!cache->framesize)
  252. non_stack_instruction_found = 0;
  253. continue;
  254. }
  255. else if (IS_SAVE_HIDDEN_PTR(op, rd, ra, rb))
  256. {
  257. /* If the first argument is a hidden pointer to the area where the
  258. return structure is to be saved, then it is saved as part of the
  259. prologue. */
  260. microblaze_debug ("add %d %d %d, continuing\n", rd, ra, rb);
  261. save_hidden_pointer_found = 1;
  262. if (!cache->framesize)
  263. non_stack_instruction_found = 0;
  264. continue;
  265. }
  266. /* As a result of the modification in the next step where we continue
  267. to analyze the prologue till we reach a control flow instruction,
  268. we need another variable to store when exactly a non-stack
  269. instruction was encountered, which is the current definition
  270. of a prologue. */
  271. if (!non_stack_instruction_found)
  272. prologue_end_addr = addr;
  273. non_stack_instruction_found = 1;
  274. /* When optimizations are enabled, it is not guaranteed that prologue
  275. instructions are not mixed in with other instructions from the
  276. program. Some programs show this behavior at -O2. This can be
  277. avoided by adding -fno-schedule-insns2 switch as of now (edk 8.1)
  278. In such cases, we scan the function until we see the first control
  279. instruction. */
  280. {
  281. unsigned ctrl_op = (unsigned)insn >> 26;
  282. /* continue if not control flow (branch, return). */
  283. if (ctrl_op != 0x26 && ctrl_op != 0x27 && ctrl_op != 0x2d
  284. && ctrl_op != 0x2e && ctrl_op != 0x2f)
  285. continue;
  286. else if (ctrl_op == 0x2c)
  287. continue; /* continue if imm. */
  288. }
  289. /* This is not a prologue insn, so stop here. */
  290. microblaze_debug ("insn is not a prologue insn -- ending scan\n");
  291. break;
  292. }
  293. microblaze_debug ("done analyzing prologue\n");
  294. microblaze_debug ("prologue end = 0x%x\n", (int) addr);
  295. /* If the last instruction was an add rd, r5, r0 then don't count it as
  296. part of the prologue. */
  297. if (save_hidden_pointer_found)
  298. prologue_end_addr -= INST_WORD_SIZE;
  299. return prologue_end_addr;
  300. }
  301. static CORE_ADDR
  302. microblaze_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
  303. {
  304. gdb_byte buf[4];
  305. CORE_ADDR pc;
  306. frame_unwind_register (next_frame, MICROBLAZE_PC_REGNUM, buf);
  307. pc = extract_typed_address (buf, builtin_type (gdbarch)->builtin_func_ptr);
  308. /* For sentinel frame, return address is actual PC. For other frames,
  309. return address is pc+8. This is a workaround because gcc does not
  310. generate correct return address in CIE. */
  311. if (frame_relative_level (next_frame) >= 0)
  312. pc += 8;
  313. return pc;
  314. }
  315. /* Return PC of first real instruction of the function starting at
  316. START_PC. */
  317. static CORE_ADDR
  318. microblaze_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
  319. {
  320. struct symtab_and_line sal;
  321. CORE_ADDR func_start, func_end, ostart_pc;
  322. struct microblaze_frame_cache cache;
  323. /* This is the preferred method, find the end of the prologue by
  324. using the debugging information. Debugging info does not always
  325. give the right answer since parameters are stored on stack after this.
  326. Always analyze the prologue. */
  327. if (find_pc_partial_function (start_pc, NULL, &func_start, &func_end))
  328. {
  329. sal = find_pc_line (func_start, 0);
  330. if (sal.end < func_end
  331. && start_pc <= sal.end)
  332. start_pc = sal.end;
  333. }
  334. ostart_pc = microblaze_analyze_prologue (gdbarch, func_start, 0xffffffffUL,
  335. &cache);
  336. if (ostart_pc > start_pc)
  337. return ostart_pc;
  338. return start_pc;
  339. }
  340. /* Normal frames. */
  341. static struct microblaze_frame_cache *
  342. microblaze_frame_cache (struct frame_info *next_frame, void **this_cache)
  343. {
  344. struct microblaze_frame_cache *cache;
  345. struct gdbarch *gdbarch = get_frame_arch (next_frame);
  346. int rn;
  347. if (*this_cache)
  348. return (struct microblaze_frame_cache *) *this_cache;
  349. cache = microblaze_alloc_frame_cache ();
  350. *this_cache = cache;
  351. cache->saved_regs = trad_frame_alloc_saved_regs (next_frame);
  352. /* Clear offsets to saved regs in frame. */
  353. for (rn = 0; rn < gdbarch_num_regs (gdbarch); rn++)
  354. cache->register_offsets[rn] = -1;
  355. /* Call for side effects. */
  356. get_frame_func (next_frame);
  357. cache->pc = get_frame_address_in_block (next_frame);
  358. return cache;
  359. }
  360. static void
  361. microblaze_frame_this_id (struct frame_info *next_frame, void **this_cache,
  362. struct frame_id *this_id)
  363. {
  364. struct microblaze_frame_cache *cache =
  365. microblaze_frame_cache (next_frame, this_cache);
  366. /* This marks the outermost frame. */
  367. if (cache->base == 0)
  368. return;
  369. (*this_id) = frame_id_build (cache->base, cache->pc);
  370. }
  371. static struct value *
  372. microblaze_frame_prev_register (struct frame_info *this_frame,
  373. void **this_cache, int regnum)
  374. {
  375. struct microblaze_frame_cache *cache =
  376. microblaze_frame_cache (this_frame, this_cache);
  377. if (cache->frameless_p)
  378. {
  379. if (regnum == MICROBLAZE_PC_REGNUM)
  380. regnum = 15;
  381. if (regnum == MICROBLAZE_SP_REGNUM)
  382. regnum = 1;
  383. return trad_frame_get_prev_register (this_frame,
  384. cache->saved_regs, regnum);
  385. }
  386. else
  387. return trad_frame_get_prev_register (this_frame, cache->saved_regs,
  388. regnum);
  389. }
  390. static const struct frame_unwind microblaze_frame_unwind =
  391. {
  392. "microblaze prologue",
  393. NORMAL_FRAME,
  394. default_frame_unwind_stop_reason,
  395. microblaze_frame_this_id,
  396. microblaze_frame_prev_register,
  397. NULL,
  398. default_frame_sniffer
  399. };
  400. static CORE_ADDR
  401. microblaze_frame_base_address (struct frame_info *next_frame,
  402. void **this_cache)
  403. {
  404. struct microblaze_frame_cache *cache =
  405. microblaze_frame_cache (next_frame, this_cache);
  406. return cache->base;
  407. }
  408. static const struct frame_base microblaze_frame_base =
  409. {
  410. &microblaze_frame_unwind,
  411. microblaze_frame_base_address,
  412. microblaze_frame_base_address,
  413. microblaze_frame_base_address
  414. };
  415. /* Extract from an array REGBUF containing the (raw) register state, a
  416. function return value of TYPE, and copy that into VALBUF. */
  417. static void
  418. microblaze_extract_return_value (struct type *type, struct regcache *regcache,
  419. gdb_byte *valbuf)
  420. {
  421. gdb_byte buf[8];
  422. /* Copy the return value (starting) in RETVAL_REGNUM to VALBUF. */
  423. switch (TYPE_LENGTH (type))
  424. {
  425. case 1: /* return last byte in the register. */
  426. regcache->cooked_read (MICROBLAZE_RETVAL_REGNUM, buf);
  427. memcpy(valbuf, buf + MICROBLAZE_REGISTER_SIZE - 1, 1);
  428. return;
  429. case 2: /* return last 2 bytes in register. */
  430. regcache->cooked_read (MICROBLAZE_RETVAL_REGNUM, buf);
  431. memcpy(valbuf, buf + MICROBLAZE_REGISTER_SIZE - 2, 2);
  432. return;
  433. case 4: /* for sizes 4 or 8, copy the required length. */
  434. case 8:
  435. regcache->cooked_read (MICROBLAZE_RETVAL_REGNUM, buf);
  436. regcache->cooked_read (MICROBLAZE_RETVAL_REGNUM + 1, buf+4);
  437. memcpy (valbuf, buf, TYPE_LENGTH (type));
  438. return;
  439. default:
  440. internal_error (__FILE__, __LINE__,
  441. _("Unsupported return value size requested"));
  442. }
  443. }
  444. /* Store the return value in VALBUF (of type TYPE) where the caller
  445. expects to see it.
  446. Integers up to four bytes are stored in r3.
  447. Longs are stored in r3 (most significant word) and r4 (least
  448. significant word).
  449. Small structures are always returned on stack. */
  450. static void
  451. microblaze_store_return_value (struct type *type, struct regcache *regcache,
  452. const gdb_byte *valbuf)
  453. {
  454. int len = TYPE_LENGTH (type);
  455. gdb_byte buf[8];
  456. memset (buf, 0, sizeof(buf));
  457. /* Integral and pointer return values. */
  458. if (len > 4)
  459. {
  460. gdb_assert (len == 8);
  461. memcpy (buf, valbuf, 8);
  462. regcache->cooked_write (MICROBLAZE_RETVAL_REGNUM+1, buf + 4);
  463. }
  464. else
  465. /* ??? Do we need to do any sign-extension here? */
  466. memcpy (buf + 4 - len, valbuf, len);
  467. regcache->cooked_write (MICROBLAZE_RETVAL_REGNUM, buf);
  468. }
  469. static enum return_value_convention
  470. microblaze_return_value (struct gdbarch *gdbarch, struct value *function,
  471. struct type *type, struct regcache *regcache,
  472. gdb_byte *readbuf, const gdb_byte *writebuf)
  473. {
  474. if (readbuf)
  475. microblaze_extract_return_value (type, regcache, readbuf);
  476. if (writebuf)
  477. microblaze_store_return_value (type, regcache, writebuf);
  478. return RETURN_VALUE_REGISTER_CONVENTION;
  479. }
  480. static int
  481. microblaze_stabs_argument_has_addr (struct gdbarch *gdbarch, struct type *type)
  482. {
  483. return (TYPE_LENGTH (type) == 16);
  484. }
  485. static int dwarf2_to_reg_map[78] =
  486. { 0 /* r0 */, 1 /* r1 */, 2 /* r2 */, 3 /* r3 */, /* 0- 3 */
  487. 4 /* r4 */, 5 /* r5 */, 6 /* r6 */, 7 /* r7 */, /* 4- 7 */
  488. 8 /* r8 */, 9 /* r9 */, 10 /* r10 */, 11 /* r11 */, /* 8-11 */
  489. 12 /* r12 */, 13 /* r13 */, 14 /* r14 */, 15 /* r15 */, /* 12-15 */
  490. 16 /* r16 */, 17 /* r17 */, 18 /* r18 */, 19 /* r19 */, /* 16-19 */
  491. 20 /* r20 */, 21 /* r21 */, 22 /* r22 */, 23 /* r23 */, /* 20-23 */
  492. 24 /* r24 */, 25 /* r25 */, 26 /* r26 */, 27 /* r27 */, /* 24-25 */
  493. 28 /* r28 */, 29 /* r29 */, 30 /* r30 */, 31 /* r31 */, /* 28-31 */
  494. -1 /* $f0 */, -1 /* $f1 */, -1 /* $f2 */, -1 /* $f3 */, /* 32-35 */
  495. -1 /* $f4 */, -1 /* $f5 */, -1 /* $f6 */, -1 /* $f7 */, /* 36-39 */
  496. -1 /* $f8 */, -1 /* $f9 */, -1 /* $f10 */, -1 /* $f11 */, /* 40-43 */
  497. -1 /* $f12 */, -1 /* $f13 */, -1 /* $f14 */, -1 /* $f15 */, /* 44-47 */
  498. -1 /* $f16 */, -1 /* $f17 */, -1 /* $f18 */, -1 /* $f19 */, /* 48-51 */
  499. -1 /* $f20 */, -1 /* $f21 */, -1 /* $f22 */, -1 /* $f23 */, /* 52-55 */
  500. -1 /* $f24 */, -1 /* $f25 */, -1 /* $f26 */, -1 /* $f27 */, /* 56-59 */
  501. -1 /* $f28 */, -1 /* $f29 */, -1 /* $f30 */, -1 /* $f31 */, /* 60-63 */
  502. -1 /* hi */, -1 /* lo */, -1 /* accum*/, 33 /* rmsr */, /* 64-67 */
  503. -1 /* $fcc1*/, -1 /* $fcc2*/, -1 /* $fcc3*/, -1 /* $fcc4*/, /* 68-71 */
  504. -1 /* $fcc5*/, -1 /* $fcc6*/, -1 /* $fcc7*/, -1 /* $ap */, /* 72-75 */
  505. -1 /* $rap */, -1 /* $frp */ /* 76-77 */
  506. };
  507. static int
  508. microblaze_dwarf2_reg_to_regnum (struct gdbarch *gdbarch, int reg)
  509. {
  510. if (reg >= 0 && reg < sizeof (dwarf2_to_reg_map))
  511. return dwarf2_to_reg_map[reg];
  512. return -1;
  513. }
  514. static void
  515. microblaze_register_g_packet_guesses (struct gdbarch *gdbarch)
  516. {
  517. register_remote_g_packet_guess (gdbarch,
  518. 4 * MICROBLAZE_NUM_CORE_REGS,
  519. tdesc_microblaze);
  520. register_remote_g_packet_guess (gdbarch,
  521. 4 * MICROBLAZE_NUM_REGS,
  522. tdesc_microblaze_with_stack_protect);
  523. }
  524. static struct gdbarch *
  525. microblaze_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  526. {
  527. struct gdbarch *gdbarch;
  528. tdesc_arch_data_up tdesc_data;
  529. const struct target_desc *tdesc = info.target_desc;
  530. /* If there is already a candidate, use it. */
  531. arches = gdbarch_list_lookup_by_info (arches, &info);
  532. if (arches != NULL)
  533. return arches->gdbarch;
  534. if (tdesc == NULL)
  535. tdesc = tdesc_microblaze;
  536. /* Check any target description for validity. */
  537. if (tdesc_has_registers (tdesc))
  538. {
  539. const struct tdesc_feature *feature;
  540. int valid_p;
  541. int i;
  542. feature = tdesc_find_feature (tdesc,
  543. "org.gnu.gdb.microblaze.core");
  544. if (feature == NULL)
  545. return NULL;
  546. tdesc_data = tdesc_data_alloc ();
  547. valid_p = 1;
  548. for (i = 0; i < MICROBLAZE_NUM_CORE_REGS; i++)
  549. valid_p &= tdesc_numbered_register (feature, tdesc_data.get (), i,
  550. microblaze_register_names[i]);
  551. feature = tdesc_find_feature (tdesc,
  552. "org.gnu.gdb.microblaze.stack-protect");
  553. if (feature != NULL)
  554. {
  555. valid_p = 1;
  556. valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
  557. MICROBLAZE_SLR_REGNUM,
  558. "rslr");
  559. valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
  560. MICROBLAZE_SHR_REGNUM,
  561. "rshr");
  562. }
  563. if (!valid_p)
  564. return NULL;
  565. }
  566. /* Allocate space for the new architecture. */
  567. microblaze_gdbarch_tdep *tdep = new microblaze_gdbarch_tdep;
  568. gdbarch = gdbarch_alloc (&info, tdep);
  569. set_gdbarch_long_double_bit (gdbarch, 128);
  570. set_gdbarch_num_regs (gdbarch, MICROBLAZE_NUM_REGS);
  571. set_gdbarch_register_name (gdbarch, microblaze_register_name);
  572. set_gdbarch_register_type (gdbarch, microblaze_register_type);
  573. /* Register numbers of various important registers. */
  574. set_gdbarch_sp_regnum (gdbarch, MICROBLAZE_SP_REGNUM);
  575. set_gdbarch_pc_regnum (gdbarch, MICROBLAZE_PC_REGNUM);
  576. /* Map Dwarf2 registers to GDB registers. */
  577. set_gdbarch_dwarf2_reg_to_regnum (gdbarch, microblaze_dwarf2_reg_to_regnum);
  578. /* Call dummy code. */
  579. set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
  580. set_gdbarch_return_value (gdbarch, microblaze_return_value);
  581. set_gdbarch_stabs_argument_has_addr
  582. (gdbarch, microblaze_stabs_argument_has_addr);
  583. set_gdbarch_skip_prologue (gdbarch, microblaze_skip_prologue);
  584. /* Stack grows downward. */
  585. set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  586. set_gdbarch_breakpoint_kind_from_pc (gdbarch,
  587. microblaze_breakpoint::kind_from_pc);
  588. set_gdbarch_sw_breakpoint_from_kind (gdbarch,
  589. microblaze_breakpoint::bp_from_kind);
  590. set_gdbarch_frame_args_skip (gdbarch, 8);
  591. set_gdbarch_unwind_pc (gdbarch, microblaze_unwind_pc);
  592. microblaze_register_g_packet_guesses (gdbarch);
  593. frame_base_set_default (gdbarch, &microblaze_frame_base);
  594. /* Hook in ABI-specific overrides, if they have been registered. */
  595. gdbarch_init_osabi (info, gdbarch);
  596. /* Unwind the frame. */
  597. dwarf2_append_unwinders (gdbarch);
  598. frame_unwind_append_unwinder (gdbarch, &microblaze_frame_unwind);
  599. frame_base_append_sniffer (gdbarch, dwarf2_frame_base_sniffer);
  600. if (tdesc_data != NULL)
  601. tdesc_use_registers (gdbarch, tdesc, std::move (tdesc_data));
  602. return gdbarch;
  603. }
  604. void _initialize_microblaze_tdep ();
  605. void
  606. _initialize_microblaze_tdep ()
  607. {
  608. register_gdbarch_init (bfd_arch_microblaze, microblaze_gdbarch_init);
  609. initialize_tdesc_microblaze_with_stack_protect ();
  610. initialize_tdesc_microblaze ();
  611. /* Debug this files internals. */
  612. add_setshow_zuinteger_cmd ("microblaze", class_maintenance,
  613. &microblaze_debug_flag, _("\
  614. Set microblaze debugging."), _("\
  615. Show microblaze debugging."), _("\
  616. When non-zero, microblaze specific debugging is enabled."),
  617. NULL,
  618. NULL,
  619. &setdebuglist, &showdebuglist);
  620. }