avr-tdep.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647
  1. /* Target-dependent code for Atmel AVR, for GDB.
  2. Copyright (C) 1996-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. /* Contributed by Theodore A. Roth, troth@openavr.org */
  15. /* Portions of this file were taken from the original gdb-4.18 patch developed
  16. by Denis Chertykov, denisc@overta.ru */
  17. #include "defs.h"
  18. #include "frame.h"
  19. #include "frame-unwind.h"
  20. #include "frame-base.h"
  21. #include "trad-frame.h"
  22. #include "gdbcmd.h"
  23. #include "gdbcore.h"
  24. #include "gdbtypes.h"
  25. #include "inferior.h"
  26. #include "symfile.h"
  27. #include "arch-utils.h"
  28. #include "regcache.h"
  29. #include "dis-asm.h"
  30. #include "objfiles.h"
  31. #include <algorithm>
  32. #include "gdbarch.h"
  33. /* AVR Background:
  34. (AVR micros are pure Harvard Architecture processors.)
  35. The AVR family of microcontrollers have three distinctly different memory
  36. spaces: flash, sram and eeprom. The flash is 16 bits wide and is used for
  37. the most part to store program instructions. The sram is 8 bits wide and is
  38. used for the stack and the heap. Some devices lack sram and some can have
  39. an additional external sram added on as a peripheral.
  40. The eeprom is 8 bits wide and is used to store data when the device is
  41. powered down. Eeprom is not directly accessible, it can only be accessed
  42. via io-registers using a special algorithm. Accessing eeprom via gdb's
  43. remote serial protocol ('m' or 'M' packets) looks difficult to do and is
  44. not included at this time.
  45. [The eeprom could be read manually via ``x/b <eaddr + AVR_EMEM_START>'' or
  46. written using ``set {unsigned char}<eaddr + AVR_EMEM_START>''. For this to
  47. work, the remote target must be able to handle eeprom accesses and perform
  48. the address translation.]
  49. All three memory spaces have physical addresses beginning at 0x0. In
  50. addition, the flash is addressed by gcc/binutils/gdb with respect to 8 bit
  51. bytes instead of the 16 bit wide words used by the real device for the
  52. Program Counter.
  53. In order for remote targets to work correctly, extra bits must be added to
  54. addresses before they are send to the target or received from the target
  55. via the remote serial protocol. The extra bits are the MSBs and are used to
  56. decode which memory space the address is referring to. */
  57. /* Constants: prefixed with AVR_ to avoid name space clashes */
  58. /* Address space flags */
  59. /* We are assigning the TYPE_INSTANCE_FLAG_ADDRESS_CLASS_1 to the flash address
  60. space. */
  61. #define AVR_TYPE_ADDRESS_CLASS_FLASH TYPE_ADDRESS_CLASS_1
  62. #define AVR_TYPE_INSTANCE_FLAG_ADDRESS_CLASS_FLASH \
  63. TYPE_INSTANCE_FLAG_ADDRESS_CLASS_1
  64. enum
  65. {
  66. AVR_REG_W = 24,
  67. AVR_REG_X = 26,
  68. AVR_REG_Y = 28,
  69. AVR_FP_REGNUM = 28,
  70. AVR_REG_Z = 30,
  71. AVR_SREG_REGNUM = 32,
  72. AVR_SP_REGNUM = 33,
  73. AVR_PC_REGNUM = 34,
  74. AVR_NUM_REGS = 32 + 1 /*SREG*/ + 1 /*SP*/ + 1 /*PC*/,
  75. AVR_NUM_REG_BYTES = 32 + 1 /*SREG*/ + 2 /*SP*/ + 4 /*PC*/,
  76. /* Pseudo registers. */
  77. AVR_PSEUDO_PC_REGNUM = 35,
  78. AVR_NUM_PSEUDO_REGS = 1,
  79. AVR_PC_REG_INDEX = 35, /* index into array of registers */
  80. AVR_MAX_PROLOGUE_SIZE = 64, /* bytes */
  81. /* Count of pushed registers. From r2 to r17 (inclusively), r28, r29 */
  82. AVR_MAX_PUSHES = 18,
  83. /* Number of the last pushed register. r17 for current avr-gcc */
  84. AVR_LAST_PUSHED_REGNUM = 17,
  85. AVR_ARG1_REGNUM = 24, /* Single byte argument */
  86. AVR_ARGN_REGNUM = 25, /* Multi byte argments */
  87. AVR_LAST_ARG_REGNUM = 8, /* Last argument register */
  88. AVR_RET1_REGNUM = 24, /* Single byte return value */
  89. AVR_RETN_REGNUM = 25, /* Multi byte return value */
  90. /* FIXME: TRoth/2002-01-??: Can we shift all these memory masks left 8
  91. bits? Do these have to match the bfd vma values? It sure would make
  92. things easier in the future if they didn't need to match.
  93. Note: I chose these values so as to be consistent with bfd vma
  94. addresses.
  95. TRoth/2002-04-08: There is already a conflict with very large programs
  96. in the mega128. The mega128 has 128K instruction bytes (64K words),
  97. thus the Most Significant Bit is 0x10000 which gets masked off my
  98. AVR_MEM_MASK.
  99. The problem manifests itself when trying to set a breakpoint in a
  100. function which resides in the upper half of the instruction space and
  101. thus requires a 17-bit address.
  102. For now, I've just removed the EEPROM mask and changed AVR_MEM_MASK
  103. from 0x00ff0000 to 0x00f00000. Eeprom is not accessible from gdb yet,
  104. but could be for some remote targets by just adding the correct offset
  105. to the address and letting the remote target handle the low-level
  106. details of actually accessing the eeprom. */
  107. AVR_IMEM_START = 0x00000000, /* INSN memory */
  108. AVR_SMEM_START = 0x00800000, /* SRAM memory */
  109. #if 1
  110. /* No eeprom mask defined */
  111. AVR_MEM_MASK = 0x00f00000, /* mask to determine memory space */
  112. #else
  113. AVR_EMEM_START = 0x00810000, /* EEPROM memory */
  114. AVR_MEM_MASK = 0x00ff0000, /* mask to determine memory space */
  115. #endif
  116. };
  117. /* Prologue types:
  118. NORMAL and CALL are the typical types (the -mcall-prologues gcc option
  119. causes the generation of the CALL type prologues). */
  120. enum {
  121. AVR_PROLOGUE_NONE, /* No prologue */
  122. AVR_PROLOGUE_NORMAL,
  123. AVR_PROLOGUE_CALL, /* -mcall-prologues */
  124. AVR_PROLOGUE_MAIN,
  125. AVR_PROLOGUE_INTR, /* interrupt handler */
  126. AVR_PROLOGUE_SIG, /* signal handler */
  127. };
  128. /* Any function with a frame looks like this
  129. ....... <-SP POINTS HERE
  130. LOCALS1 <-FP POINTS HERE
  131. LOCALS0
  132. SAVED FP
  133. SAVED R3
  134. SAVED R2
  135. RET PC
  136. FIRST ARG
  137. SECOND ARG */
  138. struct avr_unwind_cache
  139. {
  140. /* The previous frame's inner most stack address. Used as this
  141. frame ID's stack_addr. */
  142. CORE_ADDR prev_sp;
  143. /* The frame's base, optionally used by the high-level debug info. */
  144. CORE_ADDR base;
  145. int size;
  146. int prologue_type;
  147. /* Table indicating the location of each and every register. */
  148. trad_frame_saved_reg *saved_regs;
  149. };
  150. struct avr_gdbarch_tdep : gdbarch_tdep
  151. {
  152. /* Number of bytes stored to the stack by call instructions.
  153. 2 bytes for avr1-5 and avrxmega1-5, 3 bytes for avr6 and avrxmega6-7. */
  154. int call_length = 0;
  155. /* Type for void. */
  156. struct type *void_type = nullptr;
  157. /* Type for a function returning void. */
  158. struct type *func_void_type = nullptr;
  159. /* Type for a pointer to a function. Used for the type of PC. */
  160. struct type *pc_type = nullptr;
  161. };
  162. /* Lookup the name of a register given it's number. */
  163. static const char *
  164. avr_register_name (struct gdbarch *gdbarch, int regnum)
  165. {
  166. static const char * const register_names[] = {
  167. "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  168. "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  169. "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
  170. "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
  171. "SREG", "SP", "PC2",
  172. "pc"
  173. };
  174. if (regnum < 0)
  175. return NULL;
  176. if (regnum >= (sizeof (register_names) / sizeof (*register_names)))
  177. return NULL;
  178. return register_names[regnum];
  179. }
  180. /* Return the GDB type object for the "standard" data type
  181. of data in register N. */
  182. static struct type *
  183. avr_register_type (struct gdbarch *gdbarch, int reg_nr)
  184. {
  185. if (reg_nr == AVR_PC_REGNUM)
  186. return builtin_type (gdbarch)->builtin_uint32;
  187. avr_gdbarch_tdep *tdep = (avr_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  188. if (reg_nr == AVR_PSEUDO_PC_REGNUM)
  189. return tdep->pc_type;
  190. if (reg_nr == AVR_SP_REGNUM)
  191. return builtin_type (gdbarch)->builtin_data_ptr;
  192. return builtin_type (gdbarch)->builtin_uint8;
  193. }
  194. /* Instruction address checks and convertions. */
  195. static CORE_ADDR
  196. avr_make_iaddr (CORE_ADDR x)
  197. {
  198. return ((x) | AVR_IMEM_START);
  199. }
  200. /* FIXME: TRoth: Really need to use a larger mask for instructions. Some
  201. devices are already up to 128KBytes of flash space.
  202. TRoth/2002-04-8: See comment above where AVR_IMEM_START is defined. */
  203. static CORE_ADDR
  204. avr_convert_iaddr_to_raw (CORE_ADDR x)
  205. {
  206. return ((x) & 0xffffffff);
  207. }
  208. /* SRAM address checks and convertions. */
  209. static CORE_ADDR
  210. avr_make_saddr (CORE_ADDR x)
  211. {
  212. /* Return 0 for NULL. */
  213. if (x == 0)
  214. return 0;
  215. return ((x) | AVR_SMEM_START);
  216. }
  217. static CORE_ADDR
  218. avr_convert_saddr_to_raw (CORE_ADDR x)
  219. {
  220. return ((x) & 0xffffffff);
  221. }
  222. /* EEPROM address checks and convertions. I don't know if these will ever
  223. actually be used, but I've added them just the same. TRoth */
  224. /* TRoth/2002-04-08: Commented out for now to allow fix for problem with large
  225. programs in the mega128. */
  226. /* static CORE_ADDR */
  227. /* avr_make_eaddr (CORE_ADDR x) */
  228. /* { */
  229. /* return ((x) | AVR_EMEM_START); */
  230. /* } */
  231. /* static int */
  232. /* avr_eaddr_p (CORE_ADDR x) */
  233. /* { */
  234. /* return (((x) & AVR_MEM_MASK) == AVR_EMEM_START); */
  235. /* } */
  236. /* static CORE_ADDR */
  237. /* avr_convert_eaddr_to_raw (CORE_ADDR x) */
  238. /* { */
  239. /* return ((x) & 0xffffffff); */
  240. /* } */
  241. /* Convert from address to pointer and vice-versa. */
  242. static void
  243. avr_address_to_pointer (struct gdbarch *gdbarch,
  244. struct type *type, gdb_byte *buf, CORE_ADDR addr)
  245. {
  246. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  247. /* Is it a data address in flash? */
  248. if (AVR_TYPE_ADDRESS_CLASS_FLASH (type))
  249. {
  250. /* A data pointer in flash is byte addressed. */
  251. store_unsigned_integer (buf, TYPE_LENGTH (type), byte_order,
  252. avr_convert_iaddr_to_raw (addr));
  253. }
  254. /* Is it a code address? */
  255. else if (TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_FUNC
  256. || TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_METHOD)
  257. {
  258. /* A code pointer is word (16 bits) addressed. We shift the address down
  259. by 1 bit to convert it to a pointer. */
  260. store_unsigned_integer (buf, TYPE_LENGTH (type), byte_order,
  261. avr_convert_iaddr_to_raw (addr >> 1));
  262. }
  263. else
  264. {
  265. /* Strip off any upper segment bits. */
  266. store_unsigned_integer (buf, TYPE_LENGTH (type), byte_order,
  267. avr_convert_saddr_to_raw (addr));
  268. }
  269. }
  270. static CORE_ADDR
  271. avr_pointer_to_address (struct gdbarch *gdbarch,
  272. struct type *type, const gdb_byte *buf)
  273. {
  274. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  275. CORE_ADDR addr
  276. = extract_unsigned_integer (buf, TYPE_LENGTH (type), byte_order);
  277. /* Is it a data address in flash? */
  278. if (AVR_TYPE_ADDRESS_CLASS_FLASH (type))
  279. {
  280. /* A data pointer in flash is already byte addressed. */
  281. return avr_make_iaddr (addr);
  282. }
  283. /* Is it a code address? */
  284. else if (TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_FUNC
  285. || TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_METHOD
  286. || TYPE_CODE_SPACE (TYPE_TARGET_TYPE (type)))
  287. {
  288. /* A code pointer is word (16 bits) addressed so we shift it up
  289. by 1 bit to convert it to an address. */
  290. return avr_make_iaddr (addr << 1);
  291. }
  292. else
  293. return avr_make_saddr (addr);
  294. }
  295. static CORE_ADDR
  296. avr_integer_to_address (struct gdbarch *gdbarch,
  297. struct type *type, const gdb_byte *buf)
  298. {
  299. ULONGEST addr = unpack_long (type, buf);
  300. if (TYPE_DATA_SPACE (type))
  301. return avr_make_saddr (addr);
  302. else
  303. return avr_make_iaddr (addr);
  304. }
  305. static CORE_ADDR
  306. avr_read_pc (readable_regcache *regcache)
  307. {
  308. ULONGEST pc;
  309. regcache->cooked_read (AVR_PC_REGNUM, &pc);
  310. return avr_make_iaddr (pc);
  311. }
  312. static void
  313. avr_write_pc (struct regcache *regcache, CORE_ADDR val)
  314. {
  315. regcache_cooked_write_unsigned (regcache, AVR_PC_REGNUM,
  316. avr_convert_iaddr_to_raw (val));
  317. }
  318. static enum register_status
  319. avr_pseudo_register_read (struct gdbarch *gdbarch, readable_regcache *regcache,
  320. int regnum, gdb_byte *buf)
  321. {
  322. ULONGEST val;
  323. enum register_status status;
  324. switch (regnum)
  325. {
  326. case AVR_PSEUDO_PC_REGNUM:
  327. status = regcache->raw_read (AVR_PC_REGNUM, &val);
  328. if (status != REG_VALID)
  329. return status;
  330. val >>= 1;
  331. store_unsigned_integer (buf, 4, gdbarch_byte_order (gdbarch), val);
  332. return status;
  333. default:
  334. internal_error (__FILE__, __LINE__, _("invalid regnum"));
  335. }
  336. }
  337. static void
  338. avr_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
  339. int regnum, const gdb_byte *buf)
  340. {
  341. ULONGEST val;
  342. switch (regnum)
  343. {
  344. case AVR_PSEUDO_PC_REGNUM:
  345. val = extract_unsigned_integer (buf, 4, gdbarch_byte_order (gdbarch));
  346. val <<= 1;
  347. regcache_raw_write_unsigned (regcache, AVR_PC_REGNUM, val);
  348. break;
  349. default:
  350. internal_error (__FILE__, __LINE__, _("invalid regnum"));
  351. }
  352. }
  353. /* Function: avr_scan_prologue
  354. This function decodes an AVR function prologue to determine:
  355. 1) the size of the stack frame
  356. 2) which registers are saved on it
  357. 3) the offsets of saved regs
  358. This information is stored in the avr_unwind_cache structure.
  359. Some devices lack the sbiw instruction, so on those replace this:
  360. sbiw r28, XX
  361. with this:
  362. subi r28,lo8(XX)
  363. sbci r29,hi8(XX)
  364. A typical AVR function prologue with a frame pointer might look like this:
  365. push rXX ; saved regs
  366. ...
  367. push r28
  368. push r29
  369. in r28,__SP_L__
  370. in r29,__SP_H__
  371. sbiw r28,<LOCALS_SIZE>
  372. in __tmp_reg__,__SREG__
  373. cli
  374. out __SP_H__,r29
  375. out __SREG__,__tmp_reg__
  376. out __SP_L__,r28
  377. A typical AVR function prologue without a frame pointer might look like
  378. this:
  379. push rXX ; saved regs
  380. ...
  381. A main function prologue looks like this:
  382. ldi r28,lo8(<RAM_ADDR> - <LOCALS_SIZE>)
  383. ldi r29,hi8(<RAM_ADDR> - <LOCALS_SIZE>)
  384. out __SP_H__,r29
  385. out __SP_L__,r28
  386. A signal handler prologue looks like this:
  387. push __zero_reg__
  388. push __tmp_reg__
  389. in __tmp_reg__, __SREG__
  390. push __tmp_reg__
  391. clr __zero_reg__
  392. push rXX ; save registers r18:r27, r30:r31
  393. ...
  394. push r28 ; save frame pointer
  395. push r29
  396. in r28, __SP_L__
  397. in r29, __SP_H__
  398. sbiw r28, <LOCALS_SIZE>
  399. out __SP_H__, r29
  400. out __SP_L__, r28
  401. A interrupt handler prologue looks like this:
  402. sei
  403. push __zero_reg__
  404. push __tmp_reg__
  405. in __tmp_reg__, __SREG__
  406. push __tmp_reg__
  407. clr __zero_reg__
  408. push rXX ; save registers r18:r27, r30:r31
  409. ...
  410. push r28 ; save frame pointer
  411. push r29
  412. in r28, __SP_L__
  413. in r29, __SP_H__
  414. sbiw r28, <LOCALS_SIZE>
  415. cli
  416. out __SP_H__, r29
  417. sei
  418. out __SP_L__, r28
  419. A `-mcall-prologues' prologue looks like this (Note that the megas use a
  420. jmp instead of a rjmp, thus the prologue is one word larger since jmp is a
  421. 32 bit insn and rjmp is a 16 bit insn):
  422. ldi r26,lo8(<LOCALS_SIZE>)
  423. ldi r27,hi8(<LOCALS_SIZE>)
  424. ldi r30,pm_lo8(.L_foo_body)
  425. ldi r31,pm_hi8(.L_foo_body)
  426. rjmp __prologue_saves__+RRR
  427. .L_foo_body: */
  428. /* Not really part of a prologue, but still need to scan for it, is when a
  429. function prologue moves values passed via registers as arguments to new
  430. registers. In this case, all local variables live in registers, so there
  431. may be some register saves. This is what it looks like:
  432. movw rMM, rNN
  433. ...
  434. There could be multiple movw's. If the target doesn't have a movw insn, it
  435. will use two mov insns. This could be done after any of the above prologue
  436. types. */
  437. static CORE_ADDR
  438. avr_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR pc_beg, CORE_ADDR pc_end,
  439. struct avr_unwind_cache *info)
  440. {
  441. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  442. int i;
  443. unsigned short insn;
  444. int scan_stage = 0;
  445. struct bound_minimal_symbol msymbol;
  446. unsigned char prologue[AVR_MAX_PROLOGUE_SIZE];
  447. int vpc = 0;
  448. int len;
  449. len = pc_end - pc_beg;
  450. if (len > AVR_MAX_PROLOGUE_SIZE)
  451. len = AVR_MAX_PROLOGUE_SIZE;
  452. /* FIXME: TRoth/2003-06-11: This could be made more efficient by only
  453. reading in the bytes of the prologue. The problem is that the figuring
  454. out where the end of the prologue is is a bit difficult. The old code
  455. tried to do that, but failed quite often. */
  456. read_memory (pc_beg, prologue, len);
  457. /* Scanning main()'s prologue
  458. ldi r28,lo8(<RAM_ADDR> - <LOCALS_SIZE>)
  459. ldi r29,hi8(<RAM_ADDR> - <LOCALS_SIZE>)
  460. out __SP_H__,r29
  461. out __SP_L__,r28 */
  462. if (len >= 4)
  463. {
  464. CORE_ADDR locals;
  465. static const unsigned char img[] = {
  466. 0xde, 0xbf, /* out __SP_H__,r29 */
  467. 0xcd, 0xbf /* out __SP_L__,r28 */
  468. };
  469. insn = extract_unsigned_integer (&prologue[vpc], 2, byte_order);
  470. /* ldi r28,lo8(<RAM_ADDR> - <LOCALS_SIZE>) */
  471. if ((insn & 0xf0f0) == 0xe0c0)
  472. {
  473. locals = (insn & 0xf) | ((insn & 0x0f00) >> 4);
  474. insn = extract_unsigned_integer (&prologue[vpc + 2], 2, byte_order);
  475. /* ldi r29,hi8(<RAM_ADDR> - <LOCALS_SIZE>) */
  476. if ((insn & 0xf0f0) == 0xe0d0)
  477. {
  478. locals |= ((insn & 0xf) | ((insn & 0x0f00) >> 4)) << 8;
  479. if (vpc + 4 + sizeof (img) < len
  480. && memcmp (prologue + vpc + 4, img, sizeof (img)) == 0)
  481. {
  482. info->prologue_type = AVR_PROLOGUE_MAIN;
  483. info->base = locals;
  484. return pc_beg + 4;
  485. }
  486. }
  487. }
  488. }
  489. /* Scanning `-mcall-prologues' prologue
  490. Classic prologue is 10 bytes, mega prologue is a 12 bytes long */
  491. while (1) /* Using a while to avoid many goto's */
  492. {
  493. int loc_size;
  494. int body_addr;
  495. unsigned num_pushes;
  496. int pc_offset = 0;
  497. /* At least the fifth instruction must have been executed to
  498. modify frame shape. */
  499. if (len < 10)
  500. break;
  501. insn = extract_unsigned_integer (&prologue[vpc], 2, byte_order);
  502. /* ldi r26,<LOCALS_SIZE> */
  503. if ((insn & 0xf0f0) != 0xe0a0)
  504. break;
  505. loc_size = (insn & 0xf) | ((insn & 0x0f00) >> 4);
  506. pc_offset += 2;
  507. insn = extract_unsigned_integer (&prologue[vpc + 2], 2, byte_order);
  508. /* ldi r27,<LOCALS_SIZE> / 256 */
  509. if ((insn & 0xf0f0) != 0xe0b0)
  510. break;
  511. loc_size |= ((insn & 0xf) | ((insn & 0x0f00) >> 4)) << 8;
  512. pc_offset += 2;
  513. insn = extract_unsigned_integer (&prologue[vpc + 4], 2, byte_order);
  514. /* ldi r30,pm_lo8(.L_foo_body) */
  515. if ((insn & 0xf0f0) != 0xe0e0)
  516. break;
  517. body_addr = (insn & 0xf) | ((insn & 0x0f00) >> 4);
  518. pc_offset += 2;
  519. insn = extract_unsigned_integer (&prologue[vpc + 6], 2, byte_order);
  520. /* ldi r31,pm_hi8(.L_foo_body) */
  521. if ((insn & 0xf0f0) != 0xe0f0)
  522. break;
  523. body_addr |= ((insn & 0xf) | ((insn & 0x0f00) >> 4)) << 8;
  524. pc_offset += 2;
  525. msymbol = lookup_minimal_symbol ("__prologue_saves__", NULL, NULL);
  526. if (!msymbol.minsym)
  527. break;
  528. insn = extract_unsigned_integer (&prologue[vpc + 8], 2, byte_order);
  529. /* rjmp __prologue_saves__+RRR */
  530. if ((insn & 0xf000) == 0xc000)
  531. {
  532. /* Extract PC relative offset from RJMP */
  533. i = (insn & 0xfff) | (insn & 0x800 ? (-1 ^ 0xfff) : 0);
  534. /* Convert offset to byte addressable mode */
  535. i *= 2;
  536. /* Destination address */
  537. i += pc_beg + 10;
  538. if (body_addr != (pc_beg + 10)/2)
  539. break;
  540. pc_offset += 2;
  541. }
  542. else if ((insn & 0xfe0e) == 0x940c)
  543. {
  544. /* Extract absolute PC address from JMP */
  545. i = (((insn & 0x1) | ((insn & 0x1f0) >> 3) << 16)
  546. | (extract_unsigned_integer (&prologue[vpc + 10], 2, byte_order)
  547. & 0xffff));
  548. /* Convert address to byte addressable mode */
  549. i *= 2;
  550. if (body_addr != (pc_beg + 12)/2)
  551. break;
  552. pc_offset += 4;
  553. }
  554. else
  555. break;
  556. /* Resolve offset (in words) from __prologue_saves__ symbol.
  557. Which is a pushes count in `-mcall-prologues' mode */
  558. num_pushes = AVR_MAX_PUSHES - (i - BMSYMBOL_VALUE_ADDRESS (msymbol)) / 2;
  559. if (num_pushes > AVR_MAX_PUSHES)
  560. {
  561. gdb_printf (gdb_stderr, _("Num pushes too large: %d\n"),
  562. num_pushes);
  563. num_pushes = 0;
  564. }
  565. if (num_pushes)
  566. {
  567. int from;
  568. info->saved_regs[AVR_FP_REGNUM + 1].set_addr (num_pushes);
  569. if (num_pushes >= 2)
  570. info->saved_regs[AVR_FP_REGNUM].set_addr (num_pushes - 1);
  571. i = 0;
  572. for (from = AVR_LAST_PUSHED_REGNUM + 1 - (num_pushes - 2);
  573. from <= AVR_LAST_PUSHED_REGNUM; ++from)
  574. info->saved_regs [from].set_addr (++i);
  575. }
  576. info->size = loc_size + num_pushes;
  577. info->prologue_type = AVR_PROLOGUE_CALL;
  578. return pc_beg + pc_offset;
  579. }
  580. /* Scan for the beginning of the prologue for an interrupt or signal
  581. function. Note that we have to set the prologue type here since the
  582. third stage of the prologue may not be present (e.g. no saved registered
  583. or changing of the SP register). */
  584. if (1)
  585. {
  586. static const unsigned char img[] = {
  587. 0x78, 0x94, /* sei */
  588. 0x1f, 0x92, /* push r1 */
  589. 0x0f, 0x92, /* push r0 */
  590. 0x0f, 0xb6, /* in r0,0x3f SREG */
  591. 0x0f, 0x92, /* push r0 */
  592. 0x11, 0x24 /* clr r1 */
  593. };
  594. if (len >= sizeof (img)
  595. && memcmp (prologue, img, sizeof (img)) == 0)
  596. {
  597. info->prologue_type = AVR_PROLOGUE_INTR;
  598. vpc += sizeof (img);
  599. info->saved_regs[AVR_SREG_REGNUM].set_addr (3);
  600. info->saved_regs[0].set_addr (2);
  601. info->saved_regs[1].set_addr (1);
  602. info->size += 3;
  603. }
  604. else if (len >= sizeof (img) - 2
  605. && memcmp (img + 2, prologue, sizeof (img) - 2) == 0)
  606. {
  607. info->prologue_type = AVR_PROLOGUE_SIG;
  608. vpc += sizeof (img) - 2;
  609. info->saved_regs[AVR_SREG_REGNUM].set_addr (3);
  610. info->saved_regs[0].set_addr (2);
  611. info->saved_regs[1].set_addr (1);
  612. info->size += 2;
  613. }
  614. }
  615. /* First stage of the prologue scanning.
  616. Scan pushes (saved registers) */
  617. for (; vpc < len; vpc += 2)
  618. {
  619. insn = extract_unsigned_integer (&prologue[vpc], 2, byte_order);
  620. if ((insn & 0xfe0f) == 0x920f) /* push rXX */
  621. {
  622. /* Bits 4-9 contain a mask for registers R0-R32. */
  623. int regno = (insn & 0x1f0) >> 4;
  624. info->size++;
  625. info->saved_regs[regno].set_addr (info->size);
  626. scan_stage = 1;
  627. }
  628. else
  629. break;
  630. }
  631. gdb_assert (vpc < AVR_MAX_PROLOGUE_SIZE);
  632. /* Handle static small stack allocation using rcall or push. */
  633. avr_gdbarch_tdep *tdep = (avr_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  634. while (scan_stage == 1 && vpc < len)
  635. {
  636. insn = extract_unsigned_integer (&prologue[vpc], 2, byte_order);
  637. if (insn == 0xd000) /* rcall .+0 */
  638. {
  639. info->size += tdep->call_length;
  640. vpc += 2;
  641. }
  642. else if (insn == 0x920f || insn == 0x921f) /* push r0 or push r1 */
  643. {
  644. info->size += 1;
  645. vpc += 2;
  646. }
  647. else
  648. break;
  649. }
  650. /* Second stage of the prologue scanning.
  651. Scan:
  652. in r28,__SP_L__
  653. in r29,__SP_H__ */
  654. if (scan_stage == 1 && vpc < len)
  655. {
  656. static const unsigned char img[] = {
  657. 0xcd, 0xb7, /* in r28,__SP_L__ */
  658. 0xde, 0xb7 /* in r29,__SP_H__ */
  659. };
  660. if (vpc + sizeof (img) < len
  661. && memcmp (prologue + vpc, img, sizeof (img)) == 0)
  662. {
  663. vpc += 4;
  664. scan_stage = 2;
  665. }
  666. }
  667. /* Third stage of the prologue scanning. (Really two stages).
  668. Scan for:
  669. sbiw r28,XX or subi r28,lo8(XX)
  670. sbci r29,hi8(XX)
  671. in __tmp_reg__,__SREG__
  672. cli
  673. out __SP_H__,r29
  674. out __SREG__,__tmp_reg__
  675. out __SP_L__,r28 */
  676. if (scan_stage == 2 && vpc < len)
  677. {
  678. int locals_size = 0;
  679. static const unsigned char img[] = {
  680. 0x0f, 0xb6, /* in r0,0x3f */
  681. 0xf8, 0x94, /* cli */
  682. 0xde, 0xbf, /* out 0x3e,r29 ; SPH */
  683. 0x0f, 0xbe, /* out 0x3f,r0 ; SREG */
  684. 0xcd, 0xbf /* out 0x3d,r28 ; SPL */
  685. };
  686. static const unsigned char img_sig[] = {
  687. 0xde, 0xbf, /* out 0x3e,r29 ; SPH */
  688. 0xcd, 0xbf /* out 0x3d,r28 ; SPL */
  689. };
  690. static const unsigned char img_int[] = {
  691. 0xf8, 0x94, /* cli */
  692. 0xde, 0xbf, /* out 0x3e,r29 ; SPH */
  693. 0x78, 0x94, /* sei */
  694. 0xcd, 0xbf /* out 0x3d,r28 ; SPL */
  695. };
  696. insn = extract_unsigned_integer (&prologue[vpc], 2, byte_order);
  697. if ((insn & 0xff30) == 0x9720) /* sbiw r28,XXX */
  698. {
  699. locals_size = (insn & 0xf) | ((insn & 0xc0) >> 2);
  700. vpc += 2;
  701. }
  702. else if ((insn & 0xf0f0) == 0x50c0) /* subi r28,lo8(XX) */
  703. {
  704. locals_size = (insn & 0xf) | ((insn & 0xf00) >> 4);
  705. vpc += 2;
  706. insn = extract_unsigned_integer (&prologue[vpc], 2, byte_order);
  707. vpc += 2;
  708. locals_size += ((insn & 0xf) | ((insn & 0xf00) >> 4)) << 8;
  709. }
  710. else
  711. return pc_beg + vpc;
  712. /* Scan the last part of the prologue. May not be present for interrupt
  713. or signal handler functions, which is why we set the prologue type
  714. when we saw the beginning of the prologue previously. */
  715. if (vpc + sizeof (img_sig) < len
  716. && memcmp (prologue + vpc, img_sig, sizeof (img_sig)) == 0)
  717. {
  718. vpc += sizeof (img_sig);
  719. }
  720. else if (vpc + sizeof (img_int) < len
  721. && memcmp (prologue + vpc, img_int, sizeof (img_int)) == 0)
  722. {
  723. vpc += sizeof (img_int);
  724. }
  725. if (vpc + sizeof (img) < len
  726. && memcmp (prologue + vpc, img, sizeof (img)) == 0)
  727. {
  728. info->prologue_type = AVR_PROLOGUE_NORMAL;
  729. vpc += sizeof (img);
  730. }
  731. info->size += locals_size;
  732. /* Fall through. */
  733. }
  734. /* If we got this far, we could not scan the prologue, so just return the pc
  735. of the frame plus an adjustment for argument move insns. */
  736. for (; vpc < len; vpc += 2)
  737. {
  738. insn = extract_unsigned_integer (&prologue[vpc], 2, byte_order);
  739. if ((insn & 0xff00) == 0x0100) /* movw rXX, rYY */
  740. continue;
  741. else if ((insn & 0xfc00) == 0x2c00) /* mov rXX, rYY */
  742. continue;
  743. else
  744. break;
  745. }
  746. return pc_beg + vpc;
  747. }
  748. static CORE_ADDR
  749. avr_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  750. {
  751. CORE_ADDR func_addr, func_end;
  752. CORE_ADDR post_prologue_pc;
  753. /* See what the symbol table says */
  754. if (!find_pc_partial_function (pc, NULL, &func_addr, &func_end))
  755. return pc;
  756. post_prologue_pc = skip_prologue_using_sal (gdbarch, func_addr);
  757. if (post_prologue_pc != 0)
  758. return std::max (pc, post_prologue_pc);
  759. {
  760. CORE_ADDR prologue_end = pc;
  761. struct avr_unwind_cache info = {0};
  762. trad_frame_saved_reg saved_regs[AVR_NUM_REGS];
  763. info.saved_regs = saved_regs;
  764. /* Need to run the prologue scanner to figure out if the function has a
  765. prologue and possibly skip over moving arguments passed via registers
  766. to other registers. */
  767. prologue_end = avr_scan_prologue (gdbarch, func_addr, func_end, &info);
  768. if (info.prologue_type != AVR_PROLOGUE_NONE)
  769. return prologue_end;
  770. }
  771. /* Either we didn't find the start of this function (nothing we can do),
  772. or there's no line info, or the line after the prologue is after
  773. the end of the function (there probably isn't a prologue). */
  774. return pc;
  775. }
  776. /* Not all avr devices support the BREAK insn. Those that don't should treat
  777. it as a NOP. Thus, it should be ok. Since the avr is currently a remote
  778. only target, this shouldn't be a problem (I hope). TRoth/2003-05-14 */
  779. constexpr gdb_byte avr_break_insn [] = { 0x98, 0x95 };
  780. typedef BP_MANIPULATION (avr_break_insn) avr_breakpoint;
  781. /* Determine, for architecture GDBARCH, how a return value of TYPE
  782. should be returned. If it is supposed to be returned in registers,
  783. and READBUF is non-zero, read the appropriate value from REGCACHE,
  784. and copy it into READBUF. If WRITEBUF is non-zero, write the value
  785. from WRITEBUF into REGCACHE. */
  786. static enum return_value_convention
  787. avr_return_value (struct gdbarch *gdbarch, struct value *function,
  788. struct type *valtype, struct regcache *regcache,
  789. gdb_byte *readbuf, const gdb_byte *writebuf)
  790. {
  791. int i;
  792. /* Single byte are returned in r24.
  793. Otherwise, the MSB of the return value is always in r25, calculate which
  794. register holds the LSB. */
  795. int lsb_reg;
  796. if ((valtype->code () == TYPE_CODE_STRUCT
  797. || valtype->code () == TYPE_CODE_UNION
  798. || valtype->code () == TYPE_CODE_ARRAY)
  799. && TYPE_LENGTH (valtype) > 8)
  800. return RETURN_VALUE_STRUCT_CONVENTION;
  801. if (TYPE_LENGTH (valtype) <= 2)
  802. lsb_reg = 24;
  803. else if (TYPE_LENGTH (valtype) <= 4)
  804. lsb_reg = 22;
  805. else if (TYPE_LENGTH (valtype) <= 8)
  806. lsb_reg = 18;
  807. else
  808. gdb_assert_not_reached ("unexpected type length");
  809. if (writebuf != NULL)
  810. {
  811. for (i = 0; i < TYPE_LENGTH (valtype); i++)
  812. regcache->cooked_write (lsb_reg + i, writebuf + i);
  813. }
  814. if (readbuf != NULL)
  815. {
  816. for (i = 0; i < TYPE_LENGTH (valtype); i++)
  817. regcache->cooked_read (lsb_reg + i, readbuf + i);
  818. }
  819. return RETURN_VALUE_REGISTER_CONVENTION;
  820. }
  821. /* Put here the code to store, into fi->saved_regs, the addresses of
  822. the saved registers of frame described by FRAME_INFO. This
  823. includes special registers such as pc and fp saved in special ways
  824. in the stack frame. sp is even more special: the address we return
  825. for it IS the sp for the next frame. */
  826. static struct avr_unwind_cache *
  827. avr_frame_unwind_cache (struct frame_info *this_frame,
  828. void **this_prologue_cache)
  829. {
  830. CORE_ADDR start_pc, current_pc;
  831. ULONGEST prev_sp;
  832. ULONGEST this_base;
  833. struct avr_unwind_cache *info;
  834. struct gdbarch *gdbarch;
  835. int i;
  836. if (*this_prologue_cache)
  837. return (struct avr_unwind_cache *) *this_prologue_cache;
  838. info = FRAME_OBSTACK_ZALLOC (struct avr_unwind_cache);
  839. *this_prologue_cache = info;
  840. info->saved_regs = trad_frame_alloc_saved_regs (this_frame);
  841. info->size = 0;
  842. info->prologue_type = AVR_PROLOGUE_NONE;
  843. start_pc = get_frame_func (this_frame);
  844. current_pc = get_frame_pc (this_frame);
  845. if ((start_pc > 0) && (start_pc <= current_pc))
  846. avr_scan_prologue (get_frame_arch (this_frame),
  847. start_pc, current_pc, info);
  848. if ((info->prologue_type != AVR_PROLOGUE_NONE)
  849. && (info->prologue_type != AVR_PROLOGUE_MAIN))
  850. {
  851. ULONGEST high_base; /* High byte of FP */
  852. /* The SP was moved to the FP. This indicates that a new frame
  853. was created. Get THIS frame's FP value by unwinding it from
  854. the next frame. */
  855. this_base = get_frame_register_unsigned (this_frame, AVR_FP_REGNUM);
  856. high_base = get_frame_register_unsigned (this_frame, AVR_FP_REGNUM + 1);
  857. this_base += (high_base << 8);
  858. /* The FP points at the last saved register. Adjust the FP back
  859. to before the first saved register giving the SP. */
  860. prev_sp = this_base + info->size;
  861. }
  862. else
  863. {
  864. /* Assume that the FP is this frame's SP but with that pushed
  865. stack space added back. */
  866. this_base = get_frame_register_unsigned (this_frame, AVR_SP_REGNUM);
  867. prev_sp = this_base + info->size;
  868. }
  869. /* Add 1 here to adjust for the post-decrement nature of the push
  870. instruction.*/
  871. info->prev_sp = avr_make_saddr (prev_sp + 1);
  872. info->base = avr_make_saddr (this_base);
  873. gdbarch = get_frame_arch (this_frame);
  874. /* Adjust all the saved registers so that they contain addresses and not
  875. offsets. */
  876. for (i = 0; i < gdbarch_num_regs (gdbarch) - 1; i++)
  877. if (info->saved_regs[i].is_addr ())
  878. info->saved_regs[i].set_addr (info->prev_sp
  879. - info->saved_regs[i].addr ());
  880. /* Except for the main and startup code, the return PC is always saved on
  881. the stack and is at the base of the frame. */
  882. if (info->prologue_type != AVR_PROLOGUE_MAIN)
  883. info->saved_regs[AVR_PC_REGNUM].set_addr (info->prev_sp);
  884. /* The previous frame's SP needed to be computed. Save the computed
  885. value. */
  886. avr_gdbarch_tdep *tdep = (avr_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  887. info->saved_regs[AVR_SP_REGNUM].set_value (info->prev_sp
  888. - 1 + tdep->call_length);
  889. return info;
  890. }
  891. static CORE_ADDR
  892. avr_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
  893. {
  894. ULONGEST pc;
  895. pc = frame_unwind_register_unsigned (next_frame, AVR_PC_REGNUM);
  896. return avr_make_iaddr (pc);
  897. }
  898. static CORE_ADDR
  899. avr_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
  900. {
  901. ULONGEST sp;
  902. sp = frame_unwind_register_unsigned (next_frame, AVR_SP_REGNUM);
  903. return avr_make_saddr (sp);
  904. }
  905. /* Given a GDB frame, determine the address of the calling function's
  906. frame. This will be used to create a new GDB frame struct. */
  907. static void
  908. avr_frame_this_id (struct frame_info *this_frame,
  909. void **this_prologue_cache,
  910. struct frame_id *this_id)
  911. {
  912. struct avr_unwind_cache *info
  913. = avr_frame_unwind_cache (this_frame, this_prologue_cache);
  914. CORE_ADDR base;
  915. CORE_ADDR func;
  916. struct frame_id id;
  917. /* The FUNC is easy. */
  918. func = get_frame_func (this_frame);
  919. /* Hopefully the prologue analysis either correctly determined the
  920. frame's base (which is the SP from the previous frame), or set
  921. that base to "NULL". */
  922. base = info->prev_sp;
  923. if (base == 0)
  924. return;
  925. id = frame_id_build (base, func);
  926. (*this_id) = id;
  927. }
  928. static struct value *
  929. avr_frame_prev_register (struct frame_info *this_frame,
  930. void **this_prologue_cache, int regnum)
  931. {
  932. struct avr_unwind_cache *info
  933. = avr_frame_unwind_cache (this_frame, this_prologue_cache);
  934. if (regnum == AVR_PC_REGNUM || regnum == AVR_PSEUDO_PC_REGNUM)
  935. {
  936. if (info->saved_regs[AVR_PC_REGNUM].is_addr ())
  937. {
  938. /* Reading the return PC from the PC register is slightly
  939. abnormal. register_size(AVR_PC_REGNUM) says it is 4 bytes,
  940. but in reality, only two bytes (3 in upcoming mega256) are
  941. stored on the stack.
  942. Also, note that the value on the stack is an addr to a word
  943. not a byte, so we will need to multiply it by two at some
  944. point.
  945. And to confuse matters even more, the return address stored
  946. on the stack is in big endian byte order, even though most
  947. everything else about the avr is little endian. Ick! */
  948. ULONGEST pc;
  949. int i;
  950. gdb_byte buf[3];
  951. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  952. avr_gdbarch_tdep *tdep = (avr_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  953. read_memory (info->saved_regs[AVR_PC_REGNUM].addr (),
  954. buf, tdep->call_length);
  955. /* Extract the PC read from memory as a big-endian. */
  956. pc = 0;
  957. for (i = 0; i < tdep->call_length; i++)
  958. pc = (pc << 8) | buf[i];
  959. if (regnum == AVR_PC_REGNUM)
  960. pc <<= 1;
  961. return frame_unwind_got_constant (this_frame, regnum, pc);
  962. }
  963. return frame_unwind_got_optimized (this_frame, regnum);
  964. }
  965. return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
  966. }
  967. static const struct frame_unwind avr_frame_unwind = {
  968. "avr prologue",
  969. NORMAL_FRAME,
  970. default_frame_unwind_stop_reason,
  971. avr_frame_this_id,
  972. avr_frame_prev_register,
  973. NULL,
  974. default_frame_sniffer
  975. };
  976. static CORE_ADDR
  977. avr_frame_base_address (struct frame_info *this_frame, void **this_cache)
  978. {
  979. struct avr_unwind_cache *info
  980. = avr_frame_unwind_cache (this_frame, this_cache);
  981. return info->base;
  982. }
  983. static const struct frame_base avr_frame_base = {
  984. &avr_frame_unwind,
  985. avr_frame_base_address,
  986. avr_frame_base_address,
  987. avr_frame_base_address
  988. };
  989. /* Assuming THIS_FRAME is a dummy, return the frame ID of that dummy
  990. frame. The frame ID's base needs to match the TOS value saved by
  991. save_dummy_frame_tos(), and the PC match the dummy frame's breakpoint. */
  992. static struct frame_id
  993. avr_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  994. {
  995. ULONGEST base;
  996. base = get_frame_register_unsigned (this_frame, AVR_SP_REGNUM);
  997. return frame_id_build (avr_make_saddr (base), get_frame_pc (this_frame));
  998. }
  999. /* When arguments must be pushed onto the stack, they go on in reverse
  1000. order. The below implements a FILO (stack) to do this. */
  1001. struct stack_item
  1002. {
  1003. int len;
  1004. struct stack_item *prev;
  1005. gdb_byte *data;
  1006. };
  1007. static struct stack_item *
  1008. push_stack_item (struct stack_item *prev, const bfd_byte *contents, int len)
  1009. {
  1010. struct stack_item *si;
  1011. si = XNEW (struct stack_item);
  1012. si->data = (gdb_byte *) xmalloc (len);
  1013. si->len = len;
  1014. si->prev = prev;
  1015. memcpy (si->data, contents, len);
  1016. return si;
  1017. }
  1018. static struct stack_item *pop_stack_item (struct stack_item *si);
  1019. static struct stack_item *
  1020. pop_stack_item (struct stack_item *si)
  1021. {
  1022. struct stack_item *dead = si;
  1023. si = si->prev;
  1024. xfree (dead->data);
  1025. xfree (dead);
  1026. return si;
  1027. }
  1028. /* Setup the function arguments for calling a function in the inferior.
  1029. On the AVR architecture, there are 18 registers (R25 to R8) which are
  1030. dedicated for passing function arguments. Up to the first 18 arguments
  1031. (depending on size) may go into these registers. The rest go on the stack.
  1032. All arguments are aligned to start in even-numbered registers (odd-sized
  1033. arguments, including char, have one free register above them). For example,
  1034. an int in arg1 and a char in arg2 would be passed as such:
  1035. arg1 -> r25:r24
  1036. arg2 -> r22
  1037. Arguments that are larger than 2 bytes will be split between two or more
  1038. registers as available, but will NOT be split between a register and the
  1039. stack. Arguments that go onto the stack are pushed last arg first (this is
  1040. similar to the d10v). */
  1041. /* NOTE: TRoth/2003-06-17: The rest of this comment is old looks to be
  1042. inaccurate.
  1043. An exceptional case exists for struct arguments (and possibly other
  1044. aggregates such as arrays) -- if the size is larger than WORDSIZE bytes but
  1045. not a multiple of WORDSIZE bytes. In this case the argument is never split
  1046. between the registers and the stack, but instead is copied in its entirety
  1047. onto the stack, AND also copied into as many registers as there is room
  1048. for. In other words, space in registers permitting, two copies of the same
  1049. argument are passed in. As far as I can tell, only the one on the stack is
  1050. used, although that may be a function of the level of compiler
  1051. optimization. I suspect this is a compiler bug. Arguments of these odd
  1052. sizes are left-justified within the word (as opposed to arguments smaller
  1053. than WORDSIZE bytes, which are right-justified).
  1054. If the function is to return an aggregate type such as a struct, the caller
  1055. must allocate space into which the callee will copy the return value. In
  1056. this case, a pointer to the return value location is passed into the callee
  1057. in register R0, which displaces one of the other arguments passed in via
  1058. registers R0 to R2. */
  1059. static CORE_ADDR
  1060. avr_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  1061. struct regcache *regcache, CORE_ADDR bp_addr,
  1062. int nargs, struct value **args, CORE_ADDR sp,
  1063. function_call_return_method return_method,
  1064. CORE_ADDR struct_addr)
  1065. {
  1066. int i;
  1067. gdb_byte buf[3];
  1068. avr_gdbarch_tdep *tdep = (avr_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  1069. int call_length = tdep->call_length;
  1070. CORE_ADDR return_pc = avr_convert_iaddr_to_raw (bp_addr);
  1071. int regnum = AVR_ARGN_REGNUM;
  1072. struct stack_item *si = NULL;
  1073. if (return_method == return_method_struct)
  1074. {
  1075. regcache_cooked_write_unsigned
  1076. (regcache, regnum--, (struct_addr >> 8) & 0xff);
  1077. regcache_cooked_write_unsigned
  1078. (regcache, regnum--, struct_addr & 0xff);
  1079. /* SP being post decremented, we need to reserve one byte so that the
  1080. return address won't overwrite the result (or vice-versa). */
  1081. if (sp == struct_addr)
  1082. sp--;
  1083. }
  1084. for (i = 0; i < nargs; i++)
  1085. {
  1086. int last_regnum;
  1087. int j;
  1088. struct value *arg = args[i];
  1089. struct type *type = check_typedef (value_type (arg));
  1090. const bfd_byte *contents = value_contents (arg).data ();
  1091. int len = TYPE_LENGTH (type);
  1092. /* Calculate the potential last register needed.
  1093. E.g. For length 2, registers regnum and regnum-1 (say 25 and 24)
  1094. shall be used. So, last needed register will be regnum-1(24). */
  1095. last_regnum = regnum - (len + (len & 1)) + 1;
  1096. /* If there are registers available, use them. Once we start putting
  1097. stuff on the stack, all subsequent args go on stack. */
  1098. if ((si == NULL) && (last_regnum >= AVR_LAST_ARG_REGNUM))
  1099. {
  1100. /* Skip a register for odd length args. */
  1101. if (len & 1)
  1102. regnum--;
  1103. /* Write MSB of argument into register and subsequent bytes in
  1104. decreasing register numbers. */
  1105. for (j = 0; j < len; j++)
  1106. regcache_cooked_write_unsigned
  1107. (regcache, regnum--, contents[len - j - 1]);
  1108. }
  1109. /* No registers available, push the args onto the stack. */
  1110. else
  1111. {
  1112. /* From here on, we don't care about regnum. */
  1113. si = push_stack_item (si, contents, len);
  1114. }
  1115. }
  1116. /* Push args onto the stack. */
  1117. while (si)
  1118. {
  1119. sp -= si->len;
  1120. /* Add 1 to sp here to account for post decr nature of pushes. */
  1121. write_memory (sp + 1, si->data, si->len);
  1122. si = pop_stack_item (si);
  1123. }
  1124. /* Set the return address. For the avr, the return address is the BP_ADDR.
  1125. Need to push the return address onto the stack noting that it needs to be
  1126. in big-endian order on the stack. */
  1127. for (i = 1; i <= call_length; i++)
  1128. {
  1129. buf[call_length - i] = return_pc & 0xff;
  1130. return_pc >>= 8;
  1131. }
  1132. sp -= call_length;
  1133. /* Use 'sp + 1' since pushes are post decr ops. */
  1134. write_memory (sp + 1, buf, call_length);
  1135. /* Finally, update the SP register. */
  1136. regcache_cooked_write_unsigned (regcache, AVR_SP_REGNUM,
  1137. avr_convert_saddr_to_raw (sp));
  1138. /* Return SP value for the dummy frame, where the return address hasn't been
  1139. pushed. */
  1140. return sp + call_length;
  1141. }
  1142. /* Unfortunately dwarf2 register for SP is 32. */
  1143. static int
  1144. avr_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
  1145. {
  1146. if (reg >= 0 && reg < 32)
  1147. return reg;
  1148. if (reg == 32)
  1149. return AVR_SP_REGNUM;
  1150. return -1;
  1151. }
  1152. /* Implementation of `address_class_type_flags' gdbarch method.
  1153. This method maps DW_AT_address_class attributes to a
  1154. type_instance_flag_value. */
  1155. static type_instance_flags
  1156. avr_address_class_type_flags (int byte_size, int dwarf2_addr_class)
  1157. {
  1158. /* The value 1 of the DW_AT_address_class attribute corresponds to the
  1159. __flash qualifier. Note that this attribute is only valid with
  1160. pointer types and therefore the flag is set to the pointer type and
  1161. not its target type. */
  1162. if (dwarf2_addr_class == 1 && byte_size == 2)
  1163. return AVR_TYPE_INSTANCE_FLAG_ADDRESS_CLASS_FLASH;
  1164. return 0;
  1165. }
  1166. /* Implementation of `address_class_type_flags_to_name' gdbarch method.
  1167. Convert a type_instance_flag_value to an address space qualifier. */
  1168. static const char*
  1169. avr_address_class_type_flags_to_name (struct gdbarch *gdbarch,
  1170. type_instance_flags type_flags)
  1171. {
  1172. if (type_flags & AVR_TYPE_INSTANCE_FLAG_ADDRESS_CLASS_FLASH)
  1173. return "flash";
  1174. else
  1175. return NULL;
  1176. }
  1177. /* Implementation of `address_class_name_to_type_flags' gdbarch method.
  1178. Convert an address space qualifier to a type_instance_flag_value. */
  1179. static bool
  1180. avr_address_class_name_to_type_flags (struct gdbarch *gdbarch,
  1181. const char* name,
  1182. type_instance_flags *type_flags_ptr)
  1183. {
  1184. if (strcmp (name, "flash") == 0)
  1185. {
  1186. *type_flags_ptr = AVR_TYPE_INSTANCE_FLAG_ADDRESS_CLASS_FLASH;
  1187. return true;
  1188. }
  1189. else
  1190. return false;
  1191. }
  1192. /* Initialize the gdbarch structure for the AVR's. */
  1193. static struct gdbarch *
  1194. avr_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  1195. {
  1196. struct gdbarch *gdbarch;
  1197. struct gdbarch_list *best_arch;
  1198. int call_length;
  1199. /* Avr-6 call instructions save 3 bytes. */
  1200. switch (info.bfd_arch_info->mach)
  1201. {
  1202. case bfd_mach_avr1:
  1203. case bfd_mach_avrxmega1:
  1204. case bfd_mach_avr2:
  1205. case bfd_mach_avrxmega2:
  1206. case bfd_mach_avr3:
  1207. case bfd_mach_avrxmega3:
  1208. case bfd_mach_avr4:
  1209. case bfd_mach_avrxmega4:
  1210. case bfd_mach_avr5:
  1211. case bfd_mach_avrxmega5:
  1212. default:
  1213. call_length = 2;
  1214. break;
  1215. case bfd_mach_avr6:
  1216. case bfd_mach_avrxmega6:
  1217. case bfd_mach_avrxmega7:
  1218. call_length = 3;
  1219. break;
  1220. }
  1221. /* If there is already a candidate, use it. */
  1222. for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
  1223. best_arch != NULL;
  1224. best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
  1225. {
  1226. avr_gdbarch_tdep *tdep
  1227. = (avr_gdbarch_tdep *) gdbarch_tdep (best_arch->gdbarch);
  1228. if (tdep->call_length == call_length)
  1229. return best_arch->gdbarch;
  1230. }
  1231. /* None found, create a new architecture from the information provided. */
  1232. avr_gdbarch_tdep *tdep = new avr_gdbarch_tdep;
  1233. gdbarch = gdbarch_alloc (&info, tdep);
  1234. tdep->call_length = call_length;
  1235. /* Create a type for PC. We can't use builtin types here, as they may not
  1236. be defined. */
  1237. tdep->void_type = arch_type (gdbarch, TYPE_CODE_VOID, TARGET_CHAR_BIT,
  1238. "void");
  1239. tdep->func_void_type = make_function_type (tdep->void_type, NULL);
  1240. tdep->pc_type = arch_pointer_type (gdbarch, 4 * TARGET_CHAR_BIT, NULL,
  1241. tdep->func_void_type);
  1242. set_gdbarch_short_bit (gdbarch, 2 * TARGET_CHAR_BIT);
  1243. set_gdbarch_int_bit (gdbarch, 2 * TARGET_CHAR_BIT);
  1244. set_gdbarch_long_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  1245. set_gdbarch_long_long_bit (gdbarch, 8 * TARGET_CHAR_BIT);
  1246. set_gdbarch_ptr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
  1247. set_gdbarch_addr_bit (gdbarch, 32);
  1248. set_gdbarch_wchar_bit (gdbarch, 2 * TARGET_CHAR_BIT);
  1249. set_gdbarch_wchar_signed (gdbarch, 1);
  1250. set_gdbarch_float_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  1251. set_gdbarch_double_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  1252. set_gdbarch_long_double_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  1253. set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
  1254. set_gdbarch_double_format (gdbarch, floatformats_ieee_single);
  1255. set_gdbarch_long_double_format (gdbarch, floatformats_ieee_single);
  1256. set_gdbarch_read_pc (gdbarch, avr_read_pc);
  1257. set_gdbarch_write_pc (gdbarch, avr_write_pc);
  1258. set_gdbarch_num_regs (gdbarch, AVR_NUM_REGS);
  1259. set_gdbarch_sp_regnum (gdbarch, AVR_SP_REGNUM);
  1260. set_gdbarch_pc_regnum (gdbarch, AVR_PC_REGNUM);
  1261. set_gdbarch_register_name (gdbarch, avr_register_name);
  1262. set_gdbarch_register_type (gdbarch, avr_register_type);
  1263. set_gdbarch_num_pseudo_regs (gdbarch, AVR_NUM_PSEUDO_REGS);
  1264. set_gdbarch_pseudo_register_read (gdbarch, avr_pseudo_register_read);
  1265. set_gdbarch_pseudo_register_write (gdbarch, avr_pseudo_register_write);
  1266. set_gdbarch_return_value (gdbarch, avr_return_value);
  1267. set_gdbarch_push_dummy_call (gdbarch, avr_push_dummy_call);
  1268. set_gdbarch_dwarf2_reg_to_regnum (gdbarch, avr_dwarf_reg_to_regnum);
  1269. set_gdbarch_address_to_pointer (gdbarch, avr_address_to_pointer);
  1270. set_gdbarch_pointer_to_address (gdbarch, avr_pointer_to_address);
  1271. set_gdbarch_integer_to_address (gdbarch, avr_integer_to_address);
  1272. set_gdbarch_skip_prologue (gdbarch, avr_skip_prologue);
  1273. set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  1274. set_gdbarch_breakpoint_kind_from_pc (gdbarch, avr_breakpoint::kind_from_pc);
  1275. set_gdbarch_sw_breakpoint_from_kind (gdbarch, avr_breakpoint::bp_from_kind);
  1276. frame_unwind_append_unwinder (gdbarch, &avr_frame_unwind);
  1277. frame_base_set_default (gdbarch, &avr_frame_base);
  1278. set_gdbarch_dummy_id (gdbarch, avr_dummy_id);
  1279. set_gdbarch_unwind_pc (gdbarch, avr_unwind_pc);
  1280. set_gdbarch_unwind_sp (gdbarch, avr_unwind_sp);
  1281. set_gdbarch_address_class_type_flags (gdbarch, avr_address_class_type_flags);
  1282. set_gdbarch_address_class_name_to_type_flags
  1283. (gdbarch, avr_address_class_name_to_type_flags);
  1284. set_gdbarch_address_class_type_flags_to_name
  1285. (gdbarch, avr_address_class_type_flags_to_name);
  1286. return gdbarch;
  1287. }
  1288. /* Send a query request to the avr remote target asking for values of the io
  1289. registers. If args parameter is not NULL, then the user has requested info
  1290. on a specific io register [This still needs implemented and is ignored for
  1291. now]. The query string should be one of these forms:
  1292. "Ravr.io_reg" -> reply is "NN" number of io registers
  1293. "Ravr.io_reg:addr,len" where addr is first register and len is number of
  1294. registers to be read. The reply should be "<NAME>,VV;" for each io register
  1295. where, <NAME> is a string, and VV is the hex value of the register.
  1296. All io registers are 8-bit. */
  1297. static void
  1298. avr_io_reg_read_command (const char *args, int from_tty)
  1299. {
  1300. char query[400];
  1301. unsigned int nreg = 0;
  1302. unsigned int val;
  1303. /* Find out how many io registers the target has. */
  1304. gdb::optional<gdb::byte_vector> buf
  1305. = target_read_alloc (current_inferior ()->top_target (),
  1306. TARGET_OBJECT_AVR, "avr.io_reg");
  1307. if (!buf)
  1308. {
  1309. gdb_printf (gdb_stderr,
  1310. _("ERR: info io_registers NOT supported "
  1311. "by current target\n"));
  1312. return;
  1313. }
  1314. const char *bufstr = (const char *) buf->data ();
  1315. if (sscanf (bufstr, "%x", &nreg) != 1)
  1316. {
  1317. gdb_printf (gdb_stderr,
  1318. _("Error fetching number of io registers\n"));
  1319. return;
  1320. }
  1321. gdb_printf (_("Target has %u io registers:\n\n"), nreg);
  1322. /* only fetch up to 8 registers at a time to keep the buffer small */
  1323. int step = 8;
  1324. for (int i = 0; i < nreg; i += step)
  1325. {
  1326. /* how many registers this round? */
  1327. int j = step;
  1328. if ((i+j) >= nreg)
  1329. j = nreg - i; /* last block is less than 8 registers */
  1330. snprintf (query, sizeof (query) - 1, "avr.io_reg:%x,%x", i, j);
  1331. buf = target_read_alloc (current_inferior ()->top_target (),
  1332. TARGET_OBJECT_AVR, query);
  1333. if (!buf)
  1334. {
  1335. gdb_printf (gdb_stderr,
  1336. _("ERR: error reading avr.io_reg:%x,%x\n"),
  1337. i, j);
  1338. return;
  1339. }
  1340. const char *p = (const char *) buf->data ();
  1341. for (int k = i; k < (i + j); k++)
  1342. {
  1343. if (sscanf (p, "%[^,],%x;", query, &val) == 2)
  1344. {
  1345. gdb_printf ("[%02x] %-15s : %02x\n", k, query, val);
  1346. while ((*p != ';') && (*p != '\0'))
  1347. p++;
  1348. p++; /* skip over ';' */
  1349. if (*p == '\0')
  1350. break;
  1351. }
  1352. }
  1353. }
  1354. }
  1355. void _initialize_avr_tdep ();
  1356. void
  1357. _initialize_avr_tdep ()
  1358. {
  1359. register_gdbarch_init (bfd_arch_avr, avr_gdbarch_init);
  1360. /* Add a new command to allow the user to query the avr remote target for
  1361. the values of the io space registers in a saner way than just using
  1362. `x/NNNb ADDR`. */
  1363. /* FIXME: TRoth/2002-02-18: This should probably be changed to 'info avr
  1364. io_registers' to signify it is not available on other platforms. */
  1365. add_info ("io_registers", avr_io_reg_read_command,
  1366. _("Query remote AVR target for I/O space register values."));
  1367. }