csky-tdep.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248
  1. /* Target-dependent code for the CSKY architecture, for GDB.
  2. Copyright (C) 2010-2022 Free Software Foundation, Inc.
  3. Contributed by C-SKY Microsystems and Mentor Graphics.
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "defs.h"
  16. #include "gdbsupport/gdb_assert.h"
  17. #include "frame.h"
  18. #include "inferior.h"
  19. #include "symtab.h"
  20. #include "value.h"
  21. #include "gdbcmd.h"
  22. #include "language.h"
  23. #include "gdbcore.h"
  24. #include "symfile.h"
  25. #include "objfiles.h"
  26. #include "gdbtypes.h"
  27. #include "target.h"
  28. #include "arch-utils.h"
  29. #include "regcache.h"
  30. #include "osabi.h"
  31. #include "block.h"
  32. #include "reggroups.h"
  33. #include "elf/csky.h"
  34. #include "elf-bfd.h"
  35. #include "symcat.h"
  36. #include "sim-regno.h"
  37. #include "dis-asm.h"
  38. #include "frame-unwind.h"
  39. #include "frame-base.h"
  40. #include "trad-frame.h"
  41. #include "infcall.h"
  42. #include "floatformat.h"
  43. #include "remote.h"
  44. #include "target-descriptions.h"
  45. #include "dwarf2/frame.h"
  46. #include "user-regs.h"
  47. #include "valprint.h"
  48. #include "csky-tdep.h"
  49. #include "regset.h"
  50. #include "opcode/csky.h"
  51. #include <algorithm>
  52. #include <vector>
  53. /* Control debugging information emitted in this file. */
  54. static bool csky_debug = false;
  55. static const reggroup *cr_reggroup;
  56. static const reggroup *fr_reggroup;
  57. static const reggroup *vr_reggroup;
  58. static const reggroup *mmu_reggroup;
  59. static const reggroup *prof_reggroup;
  60. /* Convenience function to print debug messages in prologue analysis. */
  61. static void
  62. print_savedreg_msg (int regno, int offsets[], bool print_continuing)
  63. {
  64. gdb_printf (gdb_stdlog, "csky: r%d saved at offset 0x%x\n",
  65. regno, offsets[regno]);
  66. if (print_continuing)
  67. gdb_printf (gdb_stdlog, "csky: continuing\n");
  68. }
  69. /* Check whether the instruction at ADDR is 16-bit or not. */
  70. static int
  71. csky_pc_is_csky16 (struct gdbarch *gdbarch, CORE_ADDR addr)
  72. {
  73. gdb_byte target_mem[2];
  74. int status;
  75. unsigned int insn;
  76. int ret = 1;
  77. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  78. status = target_read_memory (addr, target_mem, 2);
  79. /* Assume a 16-bit instruction if we can't read memory. */
  80. if (status)
  81. return 1;
  82. /* Get instruction from memory. */
  83. insn = extract_unsigned_integer (target_mem, 2, byte_order);
  84. if ((insn & CSKY_32_INSN_MASK) == CSKY_32_INSN_MASK)
  85. ret = 0;
  86. else if (insn == CSKY_BKPT_INSN)
  87. {
  88. /* Check for 32-bit bkpt instruction which is all 0. */
  89. status = target_read_memory (addr + 2, target_mem, 2);
  90. if (status)
  91. return 1;
  92. insn = extract_unsigned_integer (target_mem, 2, byte_order);
  93. if (insn == CSKY_BKPT_INSN)
  94. ret = 0;
  95. }
  96. return ret;
  97. }
  98. /* Get one instruction at ADDR and store it in INSN. Return 2 for
  99. a 16-bit instruction or 4 for a 32-bit instruction. */
  100. static int
  101. csky_get_insn (struct gdbarch *gdbarch, CORE_ADDR addr, unsigned int *insn)
  102. {
  103. gdb_byte target_mem[2];
  104. unsigned int insn_type;
  105. int status;
  106. int insn_len = 2;
  107. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  108. status = target_read_memory (addr, target_mem, 2);
  109. if (status)
  110. memory_error (TARGET_XFER_E_IO, addr);
  111. insn_type = extract_unsigned_integer (target_mem, 2, byte_order);
  112. if (CSKY_32_INSN_MASK == (insn_type & CSKY_32_INSN_MASK))
  113. {
  114. status = target_read_memory (addr + 2, target_mem, 2);
  115. if (status)
  116. memory_error (TARGET_XFER_E_IO, addr);
  117. insn_type = ((insn_type << 16)
  118. | extract_unsigned_integer (target_mem, 2, byte_order));
  119. insn_len = 4;
  120. }
  121. *insn = insn_type;
  122. return insn_len;
  123. }
  124. /* Implement the read_pc gdbarch method. */
  125. static CORE_ADDR
  126. csky_read_pc (readable_regcache *regcache)
  127. {
  128. ULONGEST pc;
  129. regcache->cooked_read (CSKY_PC_REGNUM, &pc);
  130. return pc;
  131. }
  132. /* Implement the write_pc gdbarch method. */
  133. static void
  134. csky_write_pc (regcache *regcache, CORE_ADDR val)
  135. {
  136. regcache_cooked_write_unsigned (regcache, CSKY_PC_REGNUM, val);
  137. }
  138. /* C-Sky ABI register names. */
  139. static const char * const csky_register_names[] =
  140. {
  141. /* General registers 0 - 31. */
  142. "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  143. "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  144. "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
  145. "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
  146. /* DSP hilo registers 36 and 37. */
  147. "", "", "", "", "hi", "lo", "", "",
  148. /* FPU/VPU general registers 40 - 71. */
  149. "fr0", "fr1", "fr2", "fr3", "fr4", "fr5", "fr6", "fr7",
  150. "fr8", "fr9", "fr10", "fr11", "fr12", "fr13", "fr14", "fr15",
  151. "vr0", "vr1", "vr2", "vr3", "vr4", "vr5", "vr6", "vr7",
  152. "vr8", "vr9", "vr10", "vr11", "vr12", "vr13", "vr14", "vr15",
  153. /* Program counter 72. */
  154. "pc",
  155. /* Optional registers (ar) 73 - 88. */
  156. "ar0", "ar1", "ar2", "ar3", "ar4", "ar5", "ar6", "ar7",
  157. "ar8", "ar9", "ar10", "ar11", "ar12", "ar13", "ar14", "ar15",
  158. /* Control registers (cr) 89 - 119. */
  159. "psr", "vbr", "epsr", "fpsr", "epc", "fpc", "ss0", "ss1",
  160. "ss2", "ss3", "ss4", "gcr", "gsr", "cr13", "cr14", "cr15",
  161. "cr16", "cr17", "cr18", "cr19", "cr20", "cr21", "cr22", "cr23",
  162. "cr24", "cr25", "cr26", "cr27", "cr28", "cr29", "cr30", "cr31",
  163. /* FPU/VPU control registers 121 ~ 123. */
  164. /* User sp 127. */
  165. "fid", "fcr", "fesr", "", "", "", "usp",
  166. /* MMU control registers: 128 - 136. */
  167. "mcr0", "mcr2", "mcr3", "mcr4", "mcr6", "mcr8", "mcr29", "mcr30",
  168. "mcr31", "", "", "",
  169. /* Profiling control registers 140 - 143. */
  170. /* Profiling software general registers 144 - 157. */
  171. "profcr0", "profcr1", "profcr2", "profcr3", "profsgr0", "profsgr1",
  172. "profsgr2", "profsgr3", "profsgr4", "profsgr5", "profsgr6", "profsgr7",
  173. "profsgr8", "profsgr9", "profsgr10","profsgr11","profsgr12", "profsgr13",
  174. "", "",
  175. /* Profiling architecture general registers 160 - 174. */
  176. "profagr0", "profagr1", "profagr2", "profagr3", "profagr4", "profagr5",
  177. "profagr6", "profagr7", "profagr8", "profagr9", "profagr10","profagr11",
  178. "profagr12","profagr13","profagr14", "",
  179. /* Profiling extension general registers 176 - 188. */
  180. "profxgr0", "profxgr1", "profxgr2", "profxgr3", "profxgr4", "profxgr5",
  181. "profxgr6", "profxgr7", "profxgr8", "profxgr9", "profxgr10","profxgr11",
  182. "profxgr12",
  183. /* Control registers in bank1. */
  184. "", "", "", "", "", "", "", "",
  185. "", "", "", "", "", "", "", "",
  186. "cp1cr16", "cp1cr17", "cp1cr18", "cp1cr19", "cp1cr20", "", "", "",
  187. "", "", "", "", "", "", "", "",
  188. /* Control registers in bank3 (ICE). */
  189. "sepsr", "sevbr", "seepsr", "", "seepc", "", "nsssp", "seusp",
  190. "sedcr", "", "", "", "", "", "", "",
  191. "", "", "", "", "", "", "", "",
  192. "", "", "", "", "", "", "", ""
  193. };
  194. /* Implement the register_name gdbarch method. */
  195. static const char *
  196. csky_register_name (struct gdbarch *gdbarch, int reg_nr)
  197. {
  198. if (reg_nr < 0)
  199. return NULL;
  200. if (reg_nr >= gdbarch_num_regs (gdbarch))
  201. return NULL;
  202. return csky_register_names[reg_nr];
  203. }
  204. /* Construct vector type for vrx registers. */
  205. static struct type *
  206. csky_vector_type (struct gdbarch *gdbarch)
  207. {
  208. const struct builtin_type *bt = builtin_type (gdbarch);
  209. struct type *t;
  210. t = arch_composite_type (gdbarch, "__gdb_builtin_type_vec128i",
  211. TYPE_CODE_UNION);
  212. append_composite_type_field (t, "u32",
  213. init_vector_type (bt->builtin_int32, 4));
  214. append_composite_type_field (t, "u16",
  215. init_vector_type (bt->builtin_int16, 8));
  216. append_composite_type_field (t, "u8",
  217. init_vector_type (bt->builtin_int8, 16));
  218. t->set_is_vector (true);
  219. t->set_name ("builtin_type_vec128i");
  220. return t;
  221. }
  222. /* Return the GDB type object for the "standard" data type
  223. of data in register N. */
  224. static struct type *
  225. csky_register_type (struct gdbarch *gdbarch, int reg_nr)
  226. {
  227. /* PC, EPC, FPC is a text pointer. */
  228. if ((reg_nr == CSKY_PC_REGNUM) || (reg_nr == CSKY_EPC_REGNUM)
  229. || (reg_nr == CSKY_FPC_REGNUM))
  230. return builtin_type (gdbarch)->builtin_func_ptr;
  231. /* VBR is a data pointer. */
  232. if (reg_nr == CSKY_VBR_REGNUM)
  233. return builtin_type (gdbarch)->builtin_data_ptr;
  234. /* Float register has 64 bits, and only in ck810. */
  235. if ((reg_nr >=CSKY_FR0_REGNUM) && (reg_nr <= CSKY_FR0_REGNUM + 15))
  236. return arch_float_type (gdbarch, 64, "builtin_type_csky_ext",
  237. floatformats_ieee_double);
  238. /* Vector register has 128 bits, and only in ck810. */
  239. if ((reg_nr >= CSKY_VR0_REGNUM) && (reg_nr <= CSKY_VR0_REGNUM + 15))
  240. return csky_vector_type (gdbarch);
  241. /* Profiling general register has 48 bits, we use 64bit. */
  242. if ((reg_nr >= CSKY_PROFGR_REGNUM) && (reg_nr <= CSKY_PROFGR_REGNUM + 44))
  243. return builtin_type (gdbarch)->builtin_uint64;
  244. if (reg_nr == CSKY_SP_REGNUM)
  245. return builtin_type (gdbarch)->builtin_data_ptr;
  246. /* Others are 32 bits. */
  247. return builtin_type (gdbarch)->builtin_int32;
  248. }
  249. /* Data structure to marshall items in a dummy stack frame when
  250. calling a function in the inferior. */
  251. struct stack_item
  252. {
  253. stack_item (int len_, const gdb_byte *data_)
  254. : len (len_), data (data_)
  255. {}
  256. int len;
  257. const gdb_byte *data;
  258. };
  259. /* Implement the push_dummy_call gdbarch method. */
  260. static CORE_ADDR
  261. csky_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  262. struct regcache *regcache, CORE_ADDR bp_addr,
  263. int nargs, struct value **args, CORE_ADDR sp,
  264. function_call_return_method return_method,
  265. CORE_ADDR struct_addr)
  266. {
  267. int argnum;
  268. int argreg = CSKY_ABI_A0_REGNUM;
  269. int last_arg_regnum = CSKY_ABI_LAST_ARG_REGNUM;
  270. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  271. std::vector<stack_item> stack_items;
  272. /* Set the return address. For CSKY, the return breakpoint is
  273. always at BP_ADDR. */
  274. regcache_cooked_write_unsigned (regcache, CSKY_LR_REGNUM, bp_addr);
  275. /* The struct_return pointer occupies the first parameter
  276. passing register. */
  277. if (return_method == return_method_struct)
  278. {
  279. if (csky_debug)
  280. {
  281. gdb_printf (gdb_stdlog,
  282. "csky: struct return in %s = %s\n",
  283. gdbarch_register_name (gdbarch, argreg),
  284. paddress (gdbarch, struct_addr));
  285. }
  286. regcache_cooked_write_unsigned (regcache, argreg, struct_addr);
  287. argreg++;
  288. }
  289. /* Put parameters into argument registers in REGCACHE.
  290. In ABI argument registers are r0 through r3. */
  291. for (argnum = 0; argnum < nargs; argnum++)
  292. {
  293. int len;
  294. struct type *arg_type;
  295. const gdb_byte *val;
  296. arg_type = check_typedef (value_type (args[argnum]));
  297. len = TYPE_LENGTH (arg_type);
  298. val = value_contents (args[argnum]).data ();
  299. /* Copy the argument to argument registers or the dummy stack.
  300. Large arguments are split between registers and stack.
  301. If len < 4, there is no need to worry about endianness since
  302. the arguments will always be stored in the low address. */
  303. if (len < 4)
  304. {
  305. CORE_ADDR regval
  306. = extract_unsigned_integer (val, len, byte_order);
  307. regcache_cooked_write_unsigned (regcache, argreg, regval);
  308. argreg++;
  309. }
  310. else
  311. {
  312. while (len > 0)
  313. {
  314. int partial_len = len < 4 ? len : 4;
  315. if (argreg <= last_arg_regnum)
  316. {
  317. /* The argument is passed in an argument register. */
  318. CORE_ADDR regval
  319. = extract_unsigned_integer (val, partial_len,
  320. byte_order);
  321. if (byte_order == BFD_ENDIAN_BIG)
  322. regval <<= (4 - partial_len) * 8;
  323. /* Put regval into register in REGCACHE. */
  324. regcache_cooked_write_unsigned (regcache, argreg,
  325. regval);
  326. argreg++;
  327. }
  328. else
  329. {
  330. /* The argument should be pushed onto the dummy stack. */
  331. stack_items.emplace_back (4, val);
  332. }
  333. len -= partial_len;
  334. val += partial_len;
  335. }
  336. }
  337. }
  338. /* Transfer the dummy stack frame to the target. */
  339. std::vector<stack_item>::reverse_iterator iter;
  340. for (iter = stack_items.rbegin (); iter != stack_items.rend (); ++iter)
  341. {
  342. sp -= iter->len;
  343. write_memory (sp, iter->data, iter->len);
  344. }
  345. /* Finally, update the SP register. */
  346. regcache_cooked_write_unsigned (regcache, CSKY_SP_REGNUM, sp);
  347. return sp;
  348. }
  349. /* Implement the return_value gdbarch method. */
  350. static enum return_value_convention
  351. csky_return_value (struct gdbarch *gdbarch, struct value *function,
  352. struct type *valtype, struct regcache *regcache,
  353. gdb_byte *readbuf, const gdb_byte *writebuf)
  354. {
  355. CORE_ADDR regval;
  356. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  357. int len = TYPE_LENGTH (valtype);
  358. unsigned int ret_regnum = CSKY_RET_REGNUM;
  359. /* Csky abi specifies that return values larger than 8 bytes
  360. are put on the stack. */
  361. if (len > 8)
  362. return RETURN_VALUE_STRUCT_CONVENTION;
  363. else
  364. {
  365. if (readbuf != NULL)
  366. {
  367. ULONGEST tmp;
  368. /* By using store_unsigned_integer we avoid having to do
  369. anything special for small big-endian values. */
  370. regcache->cooked_read (ret_regnum, &tmp);
  371. store_unsigned_integer (readbuf, (len > 4 ? 4 : len),
  372. byte_order, tmp);
  373. if (len > 4)
  374. {
  375. regcache->cooked_read (ret_regnum + 1, &tmp);
  376. store_unsigned_integer (readbuf + 4, 4, byte_order, tmp);
  377. }
  378. }
  379. if (writebuf != NULL)
  380. {
  381. regval = extract_unsigned_integer (writebuf, len > 4 ? 4 : len,
  382. byte_order);
  383. regcache_cooked_write_unsigned (regcache, ret_regnum, regval);
  384. if (len > 4)
  385. {
  386. regval = extract_unsigned_integer ((gdb_byte *) writebuf + 4,
  387. 4, byte_order);
  388. regcache_cooked_write_unsigned (regcache, ret_regnum + 1,
  389. regval);
  390. }
  391. }
  392. return RETURN_VALUE_REGISTER_CONVENTION;
  393. }
  394. }
  395. /* Implement the frame_align gdbarch method.
  396. Adjust the address downward (direction of stack growth) so that it
  397. is correctly aligned for a new stack frame. */
  398. static CORE_ADDR
  399. csky_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
  400. {
  401. return align_down (addr, 4);
  402. }
  403. /* Unwind cache used for gdbarch fallback unwinder. */
  404. struct csky_unwind_cache
  405. {
  406. /* The stack pointer at the time this frame was created; i.e. the
  407. caller's stack pointer when this function was called. It is used
  408. to identify this frame. */
  409. CORE_ADDR prev_sp;
  410. /* The frame base for this frame is just prev_sp - frame size.
  411. FRAMESIZE is the distance from the frame pointer to the
  412. initial stack pointer. */
  413. int framesize;
  414. /* The register used to hold the frame pointer for this frame. */
  415. int framereg;
  416. /* Saved register offsets. */
  417. trad_frame_saved_reg *saved_regs;
  418. };
  419. /* Do prologue analysis, returning the PC of the first instruction
  420. after the function prologue. */
  421. static CORE_ADDR
  422. csky_analyze_prologue (struct gdbarch *gdbarch,
  423. CORE_ADDR start_pc,
  424. CORE_ADDR limit_pc,
  425. CORE_ADDR end_pc,
  426. struct frame_info *this_frame,
  427. struct csky_unwind_cache *this_cache,
  428. lr_type_t lr_type)
  429. {
  430. CORE_ADDR addr;
  431. unsigned int insn, rn;
  432. int framesize = 0;
  433. int stacksize = 0;
  434. int register_offsets[CSKY_NUM_GREGS_SAVED_GREGS];
  435. int insn_len;
  436. /* For adjusting fp. */
  437. int is_fp_saved = 0;
  438. int adjust_fp = 0;
  439. /* REGISTER_OFFSETS will contain offsets from the top of the frame
  440. (NOT the frame pointer) for the various saved registers, or -1
  441. if the register is not saved. */
  442. for (rn = 0; rn < CSKY_NUM_GREGS_SAVED_GREGS; rn++)
  443. register_offsets[rn] = -1;
  444. /* Analyze the prologue. Things we determine from analyzing the
  445. prologue include the size of the frame and which registers are
  446. saved (and where). */
  447. if (csky_debug)
  448. {
  449. gdb_printf (gdb_stdlog,
  450. "csky: Scanning prologue: start_pc = 0x%x,"
  451. "limit_pc = 0x%x\n", (unsigned int) start_pc,
  452. (unsigned int) limit_pc);
  453. }
  454. /* Default to 16 bit instruction. */
  455. insn_len = 2;
  456. stacksize = 0;
  457. for (addr = start_pc; addr < limit_pc; addr += insn_len)
  458. {
  459. /* Get next insn. */
  460. insn_len = csky_get_insn (gdbarch, addr, &insn);
  461. /* Check if 32 bit. */
  462. if (insn_len == 4)
  463. {
  464. /* subi32 sp,sp oimm12. */
  465. if (CSKY_32_IS_SUBI0 (insn))
  466. {
  467. /* Got oimm12. */
  468. int offset = CSKY_32_SUBI_IMM (insn);
  469. if (csky_debug)
  470. {
  471. gdb_printf (gdb_stdlog,
  472. "csky: got subi sp,%d; continuing\n",
  473. offset);
  474. }
  475. stacksize += offset;
  476. continue;
  477. }
  478. /* stm32 ry-rz,(sp). */
  479. else if (CSKY_32_IS_STMx0 (insn))
  480. {
  481. /* Spill register(s). */
  482. int start_register;
  483. int reg_count;
  484. int offset;
  485. /* BIG WARNING! The CKCore ABI does not restrict functions
  486. to taking only one stack allocation. Therefore, when
  487. we save a register, we record the offset of where it was
  488. saved relative to the current stacksize. This will
  489. then give an offset from the SP upon entry to our
  490. function. Remember, stacksize is NOT constant until
  491. we're done scanning the prologue. */
  492. start_register = CSKY_32_STM_VAL_REGNUM (insn);
  493. reg_count = CSKY_32_STM_SIZE (insn);
  494. if (csky_debug)
  495. {
  496. gdb_printf (gdb_stdlog,
  497. "csky: got stm r%d-r%d,(sp)\n",
  498. start_register,
  499. start_register + reg_count);
  500. }
  501. for (rn = start_register, offset = 0;
  502. rn <= start_register + reg_count;
  503. rn++, offset += 4)
  504. {
  505. register_offsets[rn] = stacksize - offset;
  506. if (csky_debug)
  507. {
  508. gdb_printf (gdb_stdlog,
  509. "csky: r%d saved at 0x%x"
  510. " (offset %d)\n",
  511. rn, register_offsets[rn],
  512. offset);
  513. }
  514. }
  515. if (csky_debug)
  516. gdb_printf (gdb_stdlog, "csky: continuing\n");
  517. continue;
  518. }
  519. /* stw ry,(sp,disp). */
  520. else if (CSKY_32_IS_STWx0 (insn))
  521. {
  522. /* Spill register: see note for IS_STM above. */
  523. int disp;
  524. rn = CSKY_32_ST_VAL_REGNUM (insn);
  525. disp = CSKY_32_ST_OFFSET (insn);
  526. register_offsets[rn] = stacksize - disp;
  527. if (csky_debug)
  528. print_savedreg_msg (rn, register_offsets, true);
  529. continue;
  530. }
  531. else if (CSKY_32_IS_MOV_FP_SP (insn))
  532. {
  533. /* SP is saved to FP reg, means code afer prologue may
  534. modify SP. */
  535. is_fp_saved = 1;
  536. adjust_fp = stacksize;
  537. continue;
  538. }
  539. else if (CSKY_32_IS_MFCR_EPSR (insn))
  540. {
  541. unsigned int insn2;
  542. addr += 4;
  543. int mfcr_regnum = insn & 0x1f;
  544. insn_len = csky_get_insn (gdbarch, addr, &insn2);
  545. if (insn_len == 2)
  546. {
  547. int stw_regnum = (insn2 >> 5) & 0x7;
  548. if (CSKY_16_IS_STWx0 (insn2) && (mfcr_regnum == stw_regnum))
  549. {
  550. int offset;
  551. /* CSKY_EPSR_REGNUM. */
  552. rn = CSKY_NUM_GREGS;
  553. offset = CSKY_16_STWx0_OFFSET (insn2);
  554. register_offsets[rn] = stacksize - offset;
  555. if (csky_debug)
  556. print_savedreg_msg (rn, register_offsets, true);
  557. continue;
  558. }
  559. break;
  560. }
  561. else
  562. {
  563. /* INSN_LEN == 4. */
  564. int stw_regnum = (insn2 >> 21) & 0x1f;
  565. if (CSKY_32_IS_STWx0 (insn2) && (mfcr_regnum == stw_regnum))
  566. {
  567. int offset;
  568. /* CSKY_EPSR_REGNUM. */
  569. rn = CSKY_NUM_GREGS;
  570. offset = CSKY_32_ST_OFFSET (insn2);
  571. register_offsets[rn] = framesize - offset;
  572. if (csky_debug)
  573. print_savedreg_msg (rn, register_offsets, true);
  574. continue;
  575. }
  576. break;
  577. }
  578. }
  579. else if (CSKY_32_IS_MFCR_FPSR (insn))
  580. {
  581. unsigned int insn2;
  582. addr += 4;
  583. int mfcr_regnum = insn & 0x1f;
  584. insn_len = csky_get_insn (gdbarch, addr, &insn2);
  585. if (insn_len == 2)
  586. {
  587. int stw_regnum = (insn2 >> 5) & 0x7;
  588. if (CSKY_16_IS_STWx0 (insn2) && (mfcr_regnum
  589. == stw_regnum))
  590. {
  591. int offset;
  592. /* CSKY_FPSR_REGNUM. */
  593. rn = CSKY_NUM_GREGS + 1;
  594. offset = CSKY_16_STWx0_OFFSET (insn2);
  595. register_offsets[rn] = stacksize - offset;
  596. if (csky_debug)
  597. print_savedreg_msg (rn, register_offsets, true);
  598. continue;
  599. }
  600. break;
  601. }
  602. else
  603. {
  604. /* INSN_LEN == 4. */
  605. int stw_regnum = (insn2 >> 21) & 0x1f;
  606. if (CSKY_32_IS_STWx0 (insn2) && (mfcr_regnum == stw_regnum))
  607. {
  608. int offset;
  609. /* CSKY_FPSR_REGNUM. */
  610. rn = CSKY_NUM_GREGS + 1;
  611. offset = CSKY_32_ST_OFFSET (insn2);
  612. register_offsets[rn] = framesize - offset;
  613. if (csky_debug)
  614. print_savedreg_msg (rn, register_offsets, true);
  615. continue;
  616. }
  617. break;
  618. }
  619. }
  620. else if (CSKY_32_IS_MFCR_EPC (insn))
  621. {
  622. unsigned int insn2;
  623. addr += 4;
  624. int mfcr_regnum = insn & 0x1f;
  625. insn_len = csky_get_insn (gdbarch, addr, &insn2);
  626. if (insn_len == 2)
  627. {
  628. int stw_regnum = (insn2 >> 5) & 0x7;
  629. if (CSKY_16_IS_STWx0 (insn2) && (mfcr_regnum == stw_regnum))
  630. {
  631. int offset;
  632. /* CSKY_EPC_REGNUM. */
  633. rn = CSKY_NUM_GREGS + 2;
  634. offset = CSKY_16_STWx0_OFFSET (insn2);
  635. register_offsets[rn] = stacksize - offset;
  636. if (csky_debug)
  637. print_savedreg_msg (rn, register_offsets, true);
  638. continue;
  639. }
  640. break;
  641. }
  642. else
  643. {
  644. /* INSN_LEN == 4. */
  645. int stw_regnum = (insn2 >> 21) & 0x1f;
  646. if (CSKY_32_IS_STWx0 (insn2) && (mfcr_regnum == stw_regnum))
  647. {
  648. int offset;
  649. /* CSKY_EPC_REGNUM. */
  650. rn = CSKY_NUM_GREGS + 2;
  651. offset = CSKY_32_ST_OFFSET (insn2);
  652. register_offsets[rn] = framesize - offset;
  653. if (csky_debug)
  654. print_savedreg_msg (rn, register_offsets, true);
  655. continue;
  656. }
  657. break;
  658. }
  659. }
  660. else if (CSKY_32_IS_MFCR_FPC (insn))
  661. {
  662. unsigned int insn2;
  663. addr += 4;
  664. int mfcr_regnum = insn & 0x1f;
  665. insn_len = csky_get_insn (gdbarch, addr, &insn2);
  666. if (insn_len == 2)
  667. {
  668. int stw_regnum = (insn2 >> 5) & 0x7;
  669. if (CSKY_16_IS_STWx0 (insn2) && (mfcr_regnum == stw_regnum))
  670. {
  671. int offset;
  672. /* CSKY_FPC_REGNUM. */
  673. rn = CSKY_NUM_GREGS + 3;
  674. offset = CSKY_16_STWx0_OFFSET (insn2);
  675. register_offsets[rn] = stacksize - offset;
  676. if (csky_debug)
  677. print_savedreg_msg (rn, register_offsets, true);
  678. continue;
  679. }
  680. break;
  681. }
  682. else
  683. {
  684. /* INSN_LEN == 4. */
  685. int stw_regnum = (insn2 >> 21) & 0x1f;
  686. if (CSKY_32_IS_STWx0 (insn2) && (mfcr_regnum == stw_regnum))
  687. {
  688. int offset;
  689. /* CSKY_FPC_REGNUM. */
  690. rn = CSKY_NUM_GREGS + 3;
  691. offset = CSKY_32_ST_OFFSET (insn2);
  692. register_offsets[rn] = framesize - offset;
  693. if (csky_debug)
  694. print_savedreg_msg (rn, register_offsets, true);
  695. continue;
  696. }
  697. break;
  698. }
  699. }
  700. else if (CSKY_32_IS_PUSH (insn))
  701. {
  702. /* Push for 32_bit. */
  703. if (CSKY_32_IS_PUSH_R29 (insn))
  704. {
  705. stacksize += 4;
  706. register_offsets[29] = stacksize;
  707. if (csky_debug)
  708. print_savedreg_msg (29, register_offsets, false);
  709. }
  710. if (CSKY_32_PUSH_LIST2 (insn))
  711. {
  712. int num = CSKY_32_PUSH_LIST2 (insn);
  713. int tmp = 0;
  714. stacksize += num * 4;
  715. if (csky_debug)
  716. {
  717. gdb_printf (gdb_stdlog,
  718. "csky: push regs_array: r16-r%d\n",
  719. 16 + num - 1);
  720. }
  721. for (rn = 16; rn <= 16 + num - 1; rn++)
  722. {
  723. register_offsets[rn] = stacksize - tmp;
  724. if (csky_debug)
  725. {
  726. gdb_printf (gdb_stdlog,
  727. "csky: r%d saved at 0x%x"
  728. " (offset %d)\n", rn,
  729. register_offsets[rn], tmp);
  730. }
  731. tmp += 4;
  732. }
  733. }
  734. if (CSKY_32_IS_PUSH_R15 (insn))
  735. {
  736. stacksize += 4;
  737. register_offsets[15] = stacksize;
  738. if (csky_debug)
  739. print_savedreg_msg (15, register_offsets, false);
  740. }
  741. if (CSKY_32_PUSH_LIST1 (insn))
  742. {
  743. int num = CSKY_32_PUSH_LIST1 (insn);
  744. int tmp = 0;
  745. stacksize += num * 4;
  746. if (csky_debug)
  747. {
  748. gdb_printf (gdb_stdlog,
  749. "csky: push regs_array: r4-r%d\n",
  750. 4 + num - 1);
  751. }
  752. for (rn = 4; rn <= 4 + num - 1; rn++)
  753. {
  754. register_offsets[rn] = stacksize - tmp;
  755. if (csky_debug)
  756. {
  757. gdb_printf (gdb_stdlog,
  758. "csky: r%d saved at 0x%x"
  759. " (offset %d)\n", rn,
  760. register_offsets[rn], tmp);
  761. }
  762. tmp += 4;
  763. }
  764. }
  765. framesize = stacksize;
  766. if (csky_debug)
  767. gdb_printf (gdb_stdlog, "csky: continuing\n");
  768. continue;
  769. }
  770. else if (CSKY_32_IS_LRW4 (insn) || CSKY_32_IS_MOVI4 (insn)
  771. || CSKY_32_IS_MOVIH4 (insn) || CSKY_32_IS_BMASKI4 (insn))
  772. {
  773. int adjust = 0;
  774. int offset = 0;
  775. unsigned int insn2;
  776. if (csky_debug)
  777. {
  778. gdb_printf (gdb_stdlog,
  779. "csky: looking at large frame\n");
  780. }
  781. if (CSKY_32_IS_LRW4 (insn))
  782. {
  783. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  784. int literal_addr = (addr + ((insn & 0xffff) << 2))
  785. & 0xfffffffc;
  786. adjust = read_memory_unsigned_integer (literal_addr, 4,
  787. byte_order);
  788. }
  789. else if (CSKY_32_IS_MOVI4 (insn))
  790. adjust = (insn & 0xffff);
  791. else if (CSKY_32_IS_MOVIH4 (insn))
  792. adjust = (insn & 0xffff) << 16;
  793. else
  794. {
  795. /* CSKY_32_IS_BMASKI4 (insn). */
  796. adjust = (1 << (((insn & 0x3e00000) >> 21) + 1)) - 1;
  797. }
  798. if (csky_debug)
  799. {
  800. gdb_printf (gdb_stdlog,
  801. "csky: base stacksize=0x%x\n", adjust);
  802. /* May have zero or more insns which modify r4. */
  803. gdb_printf (gdb_stdlog,
  804. "csky: looking for r4 adjusters...\n");
  805. }
  806. offset = 4;
  807. insn_len = csky_get_insn (gdbarch, addr + offset, &insn2);
  808. while (CSKY_IS_R4_ADJUSTER (insn2))
  809. {
  810. if (CSKY_32_IS_ADDI4 (insn2))
  811. {
  812. int imm = (insn2 & 0xfff) + 1;
  813. adjust += imm;
  814. if (csky_debug)
  815. {
  816. gdb_printf (gdb_stdlog,
  817. "csky: addi r4,%d\n", imm);
  818. }
  819. }
  820. else if (CSKY_32_IS_SUBI4 (insn2))
  821. {
  822. int imm = (insn2 & 0xfff) + 1;
  823. adjust -= imm;
  824. if (csky_debug)
  825. {
  826. gdb_printf (gdb_stdlog,
  827. "csky: subi r4,%d\n", imm);
  828. }
  829. }
  830. else if (CSKY_32_IS_NOR4 (insn2))
  831. {
  832. adjust = ~adjust;
  833. if (csky_debug)
  834. {
  835. gdb_printf (gdb_stdlog,
  836. "csky: nor r4,r4,r4\n");
  837. }
  838. }
  839. else if (CSKY_32_IS_ROTLI4 (insn2))
  840. {
  841. int imm = ((insn2 >> 21) & 0x1f);
  842. int temp = adjust >> (32 - imm);
  843. adjust <<= imm;
  844. adjust |= temp;
  845. if (csky_debug)
  846. {
  847. gdb_printf (gdb_stdlog,
  848. "csky: rotli r4,r4,%d\n", imm);
  849. }
  850. }
  851. else if (CSKY_32_IS_LISI4 (insn2))
  852. {
  853. int imm = ((insn2 >> 21) & 0x1f);
  854. adjust <<= imm;
  855. if (csky_debug)
  856. {
  857. gdb_printf (gdb_stdlog,
  858. "csky: lsli r4,r4,%d\n", imm);
  859. }
  860. }
  861. else if (CSKY_32_IS_BSETI4 (insn2))
  862. {
  863. int imm = ((insn2 >> 21) & 0x1f);
  864. adjust |= (1 << imm);
  865. if (csky_debug)
  866. {
  867. gdb_printf (gdb_stdlog,
  868. "csky: bseti r4,r4 %d\n", imm);
  869. }
  870. }
  871. else if (CSKY_32_IS_BCLRI4 (insn2))
  872. {
  873. int imm = ((insn2 >> 21) & 0x1f);
  874. adjust &= ~(1 << imm);
  875. if (csky_debug)
  876. {
  877. gdb_printf (gdb_stdlog,
  878. "csky: bclri r4,r4 %d\n", imm);
  879. }
  880. }
  881. else if (CSKY_32_IS_IXH4 (insn2))
  882. {
  883. adjust *= 3;
  884. if (csky_debug)
  885. {
  886. gdb_printf (gdb_stdlog,
  887. "csky: ixh r4,r4,r4\n");
  888. }
  889. }
  890. else if (CSKY_32_IS_IXW4 (insn2))
  891. {
  892. adjust *= 5;
  893. if (csky_debug)
  894. {
  895. gdb_printf (gdb_stdlog,
  896. "csky: ixw r4,r4,r4\n");
  897. }
  898. }
  899. else if (CSKY_16_IS_ADDI4 (insn2))
  900. {
  901. int imm = (insn2 & 0xff) + 1;
  902. adjust += imm;
  903. if (csky_debug)
  904. {
  905. gdb_printf (gdb_stdlog,
  906. "csky: addi r4,%d\n", imm);
  907. }
  908. }
  909. else if (CSKY_16_IS_SUBI4 (insn2))
  910. {
  911. int imm = (insn2 & 0xff) + 1;
  912. adjust -= imm;
  913. if (csky_debug)
  914. {
  915. gdb_printf (gdb_stdlog,
  916. "csky: subi r4,%d\n", imm);
  917. }
  918. }
  919. else if (CSKY_16_IS_NOR4 (insn2))
  920. {
  921. adjust = ~adjust;
  922. if (csky_debug)
  923. {
  924. gdb_printf (gdb_stdlog,
  925. "csky: nor r4,r4\n");
  926. }
  927. }
  928. else if (CSKY_16_IS_BSETI4 (insn2))
  929. {
  930. int imm = (insn2 & 0x1f);
  931. adjust |= (1 << imm);
  932. if (csky_debug)
  933. {
  934. gdb_printf (gdb_stdlog,
  935. "csky: bseti r4, %d\n", imm);
  936. }
  937. }
  938. else if (CSKY_16_IS_BCLRI4 (insn2))
  939. {
  940. int imm = (insn2 & 0x1f);
  941. adjust &= ~(1 << imm);
  942. if (csky_debug)
  943. {
  944. gdb_printf (gdb_stdlog,
  945. "csky: bclri r4, %d\n", imm);
  946. }
  947. }
  948. else if (CSKY_16_IS_LSLI4 (insn2))
  949. {
  950. int imm = (insn2 & 0x1f);
  951. adjust <<= imm;
  952. if (csky_debug)
  953. {
  954. gdb_printf (gdb_stdlog,
  955. "csky: lsli r4,r4, %d\n", imm);
  956. }
  957. }
  958. offset += insn_len;
  959. insn_len = csky_get_insn (gdbarch, addr + offset, &insn2);
  960. };
  961. if (csky_debug)
  962. {
  963. gdb_printf (gdb_stdlog, "csky: done looking for"
  964. " r4 adjusters\n");
  965. }
  966. /* If the next insn adjusts the stack pointer, we keep
  967. everything; if not, we scrap it and we've found the
  968. end of the prologue. */
  969. if (CSKY_IS_SUBU4 (insn2))
  970. {
  971. addr += offset;
  972. stacksize += adjust;
  973. if (csky_debug)
  974. {
  975. gdb_printf (gdb_stdlog,
  976. "csky: found stack adjustment of"
  977. " 0x%x bytes.\n", adjust);
  978. gdb_printf (gdb_stdlog,
  979. "csky: skipping to new address %s\n",
  980. core_addr_to_string_nz (addr));
  981. gdb_printf (gdb_stdlog,
  982. "csky: continuing\n");
  983. }
  984. continue;
  985. }
  986. /* None of these instructions are prologue, so don't touch
  987. anything. */
  988. if (csky_debug)
  989. {
  990. gdb_printf (gdb_stdlog,
  991. "csky: no subu sp,sp,r4; NOT altering"
  992. " stacksize.\n");
  993. }
  994. break;
  995. }
  996. }
  997. else
  998. {
  999. /* insn_len != 4. */
  1000. /* subi.sp sp,disp. */
  1001. if (CSKY_16_IS_SUBI0 (insn))
  1002. {
  1003. int offset = CSKY_16_SUBI_IMM (insn);
  1004. if (csky_debug)
  1005. {
  1006. gdb_printf (gdb_stdlog,
  1007. "csky: got subi r0,%d; continuing\n",
  1008. offset);
  1009. }
  1010. stacksize += offset;
  1011. continue;
  1012. }
  1013. /* stw.16 rz,(sp,disp). */
  1014. else if (CSKY_16_IS_STWx0 (insn))
  1015. {
  1016. /* Spill register: see note for IS_STM above. */
  1017. int disp;
  1018. rn = CSKY_16_ST_VAL_REGNUM (insn);
  1019. disp = CSKY_16_ST_OFFSET (insn);
  1020. register_offsets[rn] = stacksize - disp;
  1021. if (csky_debug)
  1022. print_savedreg_msg (rn, register_offsets, true);
  1023. continue;
  1024. }
  1025. else if (CSKY_16_IS_MOV_FP_SP (insn))
  1026. {
  1027. /* SP is saved to FP reg, means prologue may modify SP. */
  1028. is_fp_saved = 1;
  1029. adjust_fp = stacksize;
  1030. continue;
  1031. }
  1032. else if (CSKY_16_IS_PUSH (insn))
  1033. {
  1034. /* Push for 16_bit. */
  1035. int offset = 0;
  1036. if (CSKY_16_IS_PUSH_R15 (insn))
  1037. {
  1038. stacksize += 4;
  1039. register_offsets[15] = stacksize;
  1040. if (csky_debug)
  1041. print_savedreg_msg (15, register_offsets, false);
  1042. offset += 4;
  1043. }
  1044. if (CSKY_16_PUSH_LIST1 (insn))
  1045. {
  1046. int num = CSKY_16_PUSH_LIST1 (insn);
  1047. int tmp = 0;
  1048. stacksize += num * 4;
  1049. offset += num * 4;
  1050. if (csky_debug)
  1051. {
  1052. gdb_printf (gdb_stdlog,
  1053. "csky: push regs_array: r4-r%d\n",
  1054. 4 + num - 1);
  1055. }
  1056. for (rn = 4; rn <= 4 + num - 1; rn++)
  1057. {
  1058. register_offsets[rn] = stacksize - tmp;
  1059. if (csky_debug)
  1060. {
  1061. gdb_printf (gdb_stdlog,
  1062. "csky: r%d saved at 0x%x"
  1063. " (offset %d)\n", rn,
  1064. register_offsets[rn], offset);
  1065. }
  1066. tmp += 4;
  1067. }
  1068. }
  1069. framesize = stacksize;
  1070. if (csky_debug)
  1071. gdb_printf (gdb_stdlog, "csky: continuing\n");
  1072. continue;
  1073. }
  1074. else if (CSKY_16_IS_LRW4 (insn) || CSKY_16_IS_MOVI4 (insn))
  1075. {
  1076. int adjust = 0;
  1077. unsigned int insn2;
  1078. if (csky_debug)
  1079. {
  1080. gdb_printf (gdb_stdlog,
  1081. "csky: looking at large frame\n");
  1082. }
  1083. if (CSKY_16_IS_LRW4 (insn))
  1084. {
  1085. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1086. int offset = ((insn & 0x300) >> 3) | (insn & 0x1f);
  1087. int literal_addr = (addr + ( offset << 2)) & 0xfffffffc;
  1088. adjust = read_memory_unsigned_integer (literal_addr, 4,
  1089. byte_order);
  1090. }
  1091. else
  1092. {
  1093. /* CSKY_16_IS_MOVI4 (insn). */
  1094. adjust = (insn & 0xff);
  1095. }
  1096. if (csky_debug)
  1097. {
  1098. gdb_printf (gdb_stdlog,
  1099. "csky: base stacksize=0x%x\n", adjust);
  1100. }
  1101. /* May have zero or more instructions which modify r4. */
  1102. if (csky_debug)
  1103. {
  1104. gdb_printf (gdb_stdlog,
  1105. "csky: looking for r4 adjusters...\n");
  1106. }
  1107. int offset = 2;
  1108. insn_len = csky_get_insn (gdbarch, addr + offset, &insn2);
  1109. while (CSKY_IS_R4_ADJUSTER (insn2))
  1110. {
  1111. if (CSKY_32_IS_ADDI4 (insn2))
  1112. {
  1113. int imm = (insn2 & 0xfff) + 1;
  1114. adjust += imm;
  1115. if (csky_debug)
  1116. {
  1117. gdb_printf (gdb_stdlog,
  1118. "csky: addi r4,%d\n", imm);
  1119. }
  1120. }
  1121. else if (CSKY_32_IS_SUBI4 (insn2))
  1122. {
  1123. int imm = (insn2 & 0xfff) + 1;
  1124. adjust -= imm;
  1125. if (csky_debug)
  1126. {
  1127. gdb_printf (gdb_stdlog,
  1128. "csky: subi r4,%d\n", imm);
  1129. }
  1130. }
  1131. else if (CSKY_32_IS_NOR4 (insn2))
  1132. {
  1133. adjust = ~adjust;
  1134. if (csky_debug)
  1135. {
  1136. gdb_printf (gdb_stdlog,
  1137. "csky: nor r4,r4,r4\n");
  1138. }
  1139. }
  1140. else if (CSKY_32_IS_ROTLI4 (insn2))
  1141. {
  1142. int imm = ((insn2 >> 21) & 0x1f);
  1143. int temp = adjust >> (32 - imm);
  1144. adjust <<= imm;
  1145. adjust |= temp;
  1146. if (csky_debug)
  1147. {
  1148. gdb_printf (gdb_stdlog,
  1149. "csky: rotli r4,r4,%d\n", imm);
  1150. }
  1151. }
  1152. else if (CSKY_32_IS_LISI4 (insn2))
  1153. {
  1154. int imm = ((insn2 >> 21) & 0x1f);
  1155. adjust <<= imm;
  1156. if (csky_debug)
  1157. {
  1158. gdb_printf (gdb_stdlog,
  1159. "csky: lsli r4,r4,%d\n", imm);
  1160. }
  1161. }
  1162. else if (CSKY_32_IS_BSETI4 (insn2))
  1163. {
  1164. int imm = ((insn2 >> 21) & 0x1f);
  1165. adjust |= (1 << imm);
  1166. if (csky_debug)
  1167. {
  1168. gdb_printf (gdb_stdlog,
  1169. "csky: bseti r4,r4 %d\n", imm);
  1170. }
  1171. }
  1172. else if (CSKY_32_IS_BCLRI4 (insn2))
  1173. {
  1174. int imm = ((insn2 >> 21) & 0x1f);
  1175. adjust &= ~(1 << imm);
  1176. if (csky_debug)
  1177. {
  1178. gdb_printf (gdb_stdlog,
  1179. "csky: bclri r4,r4 %d\n", imm);
  1180. }
  1181. }
  1182. else if (CSKY_32_IS_IXH4 (insn2))
  1183. {
  1184. adjust *= 3;
  1185. if (csky_debug)
  1186. {
  1187. gdb_printf (gdb_stdlog,
  1188. "csky: ixh r4,r4,r4\n");
  1189. }
  1190. }
  1191. else if (CSKY_32_IS_IXW4 (insn2))
  1192. {
  1193. adjust *= 5;
  1194. if (csky_debug)
  1195. {
  1196. gdb_printf (gdb_stdlog,
  1197. "csky: ixw r4,r4,r4\n");
  1198. }
  1199. }
  1200. else if (CSKY_16_IS_ADDI4 (insn2))
  1201. {
  1202. int imm = (insn2 & 0xff) + 1;
  1203. adjust += imm;
  1204. if (csky_debug)
  1205. {
  1206. gdb_printf (gdb_stdlog,
  1207. "csky: addi r4,%d\n", imm);
  1208. }
  1209. }
  1210. else if (CSKY_16_IS_SUBI4 (insn2))
  1211. {
  1212. int imm = (insn2 & 0xff) + 1;
  1213. adjust -= imm;
  1214. if (csky_debug)
  1215. {
  1216. gdb_printf (gdb_stdlog,
  1217. "csky: subi r4,%d\n", imm);
  1218. }
  1219. }
  1220. else if (CSKY_16_IS_NOR4 (insn2))
  1221. {
  1222. adjust = ~adjust;
  1223. if (csky_debug)
  1224. {
  1225. gdb_printf (gdb_stdlog,
  1226. "csky: nor r4,r4\n");
  1227. }
  1228. }
  1229. else if (CSKY_16_IS_BSETI4 (insn2))
  1230. {
  1231. int imm = (insn2 & 0x1f);
  1232. adjust |= (1 << imm);
  1233. if (csky_debug)
  1234. {
  1235. gdb_printf (gdb_stdlog,
  1236. "csky: bseti r4, %d\n", imm);
  1237. }
  1238. }
  1239. else if (CSKY_16_IS_BCLRI4 (insn2))
  1240. {
  1241. int imm = (insn2 & 0x1f);
  1242. adjust &= ~(1 << imm);
  1243. if (csky_debug)
  1244. {
  1245. gdb_printf (gdb_stdlog,
  1246. "csky: bclri r4, %d\n", imm);
  1247. }
  1248. }
  1249. else if (CSKY_16_IS_LSLI4 (insn2))
  1250. {
  1251. int imm = (insn2 & 0x1f);
  1252. adjust <<= imm;
  1253. if (csky_debug)
  1254. {
  1255. gdb_printf (gdb_stdlog,
  1256. "csky: lsli r4,r4, %d\n", imm);
  1257. }
  1258. }
  1259. offset += insn_len;
  1260. insn_len = csky_get_insn (gdbarch, addr + offset, &insn2);
  1261. };
  1262. if (csky_debug)
  1263. {
  1264. gdb_printf (gdb_stdlog, "csky: "
  1265. "done looking for r4 adjusters\n");
  1266. }
  1267. /* If the next instruction adjusts the stack pointer, we keep
  1268. everything; if not, we scrap it and we've found the end
  1269. of the prologue. */
  1270. if (CSKY_IS_SUBU4 (insn2))
  1271. {
  1272. addr += offset;
  1273. stacksize += adjust;
  1274. if (csky_debug)
  1275. {
  1276. gdb_printf (gdb_stdlog, "csky: "
  1277. "found stack adjustment of 0x%x"
  1278. " bytes.\n", adjust);
  1279. gdb_printf (gdb_stdlog, "csky: "
  1280. "skipping to new address %s\n",
  1281. core_addr_to_string_nz (addr));
  1282. gdb_printf (gdb_stdlog, "csky: continuing\n");
  1283. }
  1284. continue;
  1285. }
  1286. /* None of these instructions are prologue, so don't touch
  1287. anything. */
  1288. if (csky_debug)
  1289. {
  1290. gdb_printf (gdb_stdlog, "csky: no subu sp,r4; "
  1291. "NOT altering stacksize.\n");
  1292. }
  1293. break;
  1294. }
  1295. }
  1296. /* This is not a prologue instruction, so stop here. */
  1297. if (csky_debug)
  1298. {
  1299. gdb_printf (gdb_stdlog, "csky: insn is not a prologue"
  1300. " insn -- ending scan\n");
  1301. }
  1302. break;
  1303. }
  1304. if (this_cache)
  1305. {
  1306. CORE_ADDR unwound_fp;
  1307. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1308. this_cache->framesize = framesize;
  1309. if (is_fp_saved)
  1310. {
  1311. this_cache->framereg = CSKY_FP_REGNUM;
  1312. unwound_fp = get_frame_register_unsigned (this_frame,
  1313. this_cache->framereg);
  1314. this_cache->prev_sp = unwound_fp + adjust_fp;
  1315. }
  1316. else
  1317. {
  1318. this_cache->framereg = CSKY_SP_REGNUM;
  1319. unwound_fp = get_frame_register_unsigned (this_frame,
  1320. this_cache->framereg);
  1321. this_cache->prev_sp = unwound_fp + stacksize;
  1322. }
  1323. /* Note where saved registers are stored. The offsets in
  1324. REGISTER_OFFSETS are computed relative to the top of the frame. */
  1325. for (rn = 0; rn < CSKY_NUM_GREGS; rn++)
  1326. {
  1327. if (register_offsets[rn] >= 0)
  1328. {
  1329. this_cache->saved_regs[rn].set_addr (this_cache->prev_sp
  1330. - register_offsets[rn]);
  1331. if (csky_debug)
  1332. {
  1333. CORE_ADDR rn_value = read_memory_unsigned_integer (
  1334. this_cache->saved_regs[rn].addr (), 4, byte_order);
  1335. gdb_printf (gdb_stdlog, "Saved register %s "
  1336. "stored at 0x%08lx, value=0x%08lx\n",
  1337. csky_register_names[rn],
  1338. (unsigned long)
  1339. this_cache->saved_regs[rn].addr (),
  1340. (unsigned long) rn_value);
  1341. }
  1342. }
  1343. }
  1344. if (lr_type == LR_TYPE_EPC)
  1345. {
  1346. /* rte || epc . */
  1347. this_cache->saved_regs[CSKY_PC_REGNUM]
  1348. = this_cache->saved_regs[CSKY_EPC_REGNUM];
  1349. }
  1350. else if (lr_type == LR_TYPE_FPC)
  1351. {
  1352. /* rfi || fpc . */
  1353. this_cache->saved_regs[CSKY_PC_REGNUM]
  1354. = this_cache->saved_regs[CSKY_FPC_REGNUM];
  1355. }
  1356. else
  1357. {
  1358. this_cache->saved_regs[CSKY_PC_REGNUM]
  1359. = this_cache->saved_regs[CSKY_LR_REGNUM];
  1360. }
  1361. }
  1362. return addr;
  1363. }
  1364. /* Detect whether PC is at a point where the stack frame has been
  1365. destroyed. */
  1366. static int
  1367. csky_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
  1368. {
  1369. unsigned int insn;
  1370. CORE_ADDR addr;
  1371. CORE_ADDR func_start, func_end;
  1372. if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
  1373. return 0;
  1374. bool fp_saved = false;
  1375. int insn_len;
  1376. for (addr = func_start; addr < func_end; addr += insn_len)
  1377. {
  1378. /* Get next insn. */
  1379. insn_len = csky_get_insn (gdbarch, addr, &insn);
  1380. if (insn_len == 2)
  1381. {
  1382. /* Is sp is saved to fp. */
  1383. if (CSKY_16_IS_MOV_FP_SP (insn))
  1384. fp_saved = true;
  1385. /* If sp was saved to fp and now being restored from
  1386. fp then it indicates the start of epilog. */
  1387. else if (fp_saved && CSKY_16_IS_MOV_SP_FP (insn))
  1388. return pc >= addr;
  1389. }
  1390. }
  1391. return 0;
  1392. }
  1393. /* Implement the skip_prologue gdbarch hook. */
  1394. static CORE_ADDR
  1395. csky_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  1396. {
  1397. CORE_ADDR func_addr, func_end;
  1398. const int default_search_limit = 128;
  1399. /* See if we can find the end of the prologue using the symbol table. */
  1400. if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
  1401. {
  1402. CORE_ADDR post_prologue_pc
  1403. = skip_prologue_using_sal (gdbarch, func_addr);
  1404. if (post_prologue_pc != 0)
  1405. return std::max (pc, post_prologue_pc);
  1406. }
  1407. else
  1408. func_end = pc + default_search_limit;
  1409. /* Find the end of prologue. Default lr_type. */
  1410. return csky_analyze_prologue (gdbarch, pc, func_end, func_end,
  1411. NULL, NULL, LR_TYPE_R15);
  1412. }
  1413. /* Implement the breakpoint_kind_from_pc gdbarch method. */
  1414. static int
  1415. csky_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr)
  1416. {
  1417. if (csky_pc_is_csky16 (gdbarch, *pcptr))
  1418. return CSKY_INSN_SIZE16;
  1419. else
  1420. return CSKY_INSN_SIZE32;
  1421. }
  1422. /* Implement the sw_breakpoint_from_kind gdbarch method. */
  1423. static const gdb_byte *
  1424. csky_sw_breakpoint_from_kind (struct gdbarch *gdbarch, int kind, int *size)
  1425. {
  1426. *size = kind;
  1427. if (kind == CSKY_INSN_SIZE16)
  1428. {
  1429. static gdb_byte csky_16_breakpoint[] = { 0, 0 };
  1430. return csky_16_breakpoint;
  1431. }
  1432. else
  1433. {
  1434. static gdb_byte csky_32_breakpoint[] = { 0, 0, 0, 0 };
  1435. return csky_32_breakpoint;
  1436. }
  1437. }
  1438. /* Implement the memory_insert_breakpoint gdbarch method. */
  1439. static int
  1440. csky_memory_insert_breakpoint (struct gdbarch *gdbarch,
  1441. struct bp_target_info *bp_tgt)
  1442. {
  1443. int val;
  1444. const unsigned char *bp;
  1445. gdb_byte bp_write_record1[] = { 0, 0, 0, 0 };
  1446. gdb_byte bp_write_record2[] = { 0, 0, 0, 0 };
  1447. gdb_byte bp_record[] = { 0, 0, 0, 0 };
  1448. /* Sanity-check bp_address. */
  1449. if (bp_tgt->reqstd_address % 2)
  1450. warning (_("Invalid breakpoint address 0x%x is an odd number."),
  1451. (unsigned int) bp_tgt->reqstd_address);
  1452. scoped_restore restore_memory
  1453. = make_scoped_restore_show_memory_breakpoints (1);
  1454. /* Determine appropriate breakpoint_kind for this address. */
  1455. bp_tgt->kind = csky_breakpoint_kind_from_pc (gdbarch,
  1456. &bp_tgt->reqstd_address);
  1457. /* Save the memory contents. */
  1458. bp_tgt->shadow_len = bp_tgt->kind;
  1459. /* Fill bp_tgt->placed_address. */
  1460. bp_tgt->placed_address = bp_tgt->reqstd_address;
  1461. if (bp_tgt->kind == CSKY_INSN_SIZE16)
  1462. {
  1463. if ((bp_tgt->reqstd_address % 4) == 0)
  1464. {
  1465. /* Read two bytes. */
  1466. val = target_read_memory (bp_tgt->reqstd_address,
  1467. bp_tgt->shadow_contents, 2);
  1468. if (val)
  1469. return val;
  1470. /* Read two bytes. */
  1471. val = target_read_memory (bp_tgt->reqstd_address + 2,
  1472. bp_record, 2);
  1473. if (val)
  1474. return val;
  1475. /* Write the breakpoint. */
  1476. bp_write_record1[2] = bp_record[0];
  1477. bp_write_record1[3] = bp_record[1];
  1478. bp = bp_write_record1;
  1479. val = target_write_raw_memory (bp_tgt->reqstd_address, bp,
  1480. CSKY_WR_BKPT_MODE);
  1481. }
  1482. else
  1483. {
  1484. val = target_read_memory (bp_tgt->reqstd_address,
  1485. bp_tgt->shadow_contents, 2);
  1486. if (val)
  1487. return val;
  1488. val = target_read_memory (bp_tgt->reqstd_address - 2,
  1489. bp_record, 2);
  1490. if (val)
  1491. return val;
  1492. /* Write the breakpoint. */
  1493. bp_write_record1[0] = bp_record[0];
  1494. bp_write_record1[1] = bp_record[1];
  1495. bp = bp_write_record1;
  1496. val = target_write_raw_memory (bp_tgt->reqstd_address - 2,
  1497. bp, CSKY_WR_BKPT_MODE);
  1498. }
  1499. }
  1500. else
  1501. {
  1502. if (bp_tgt->placed_address % 4 == 0)
  1503. {
  1504. val = target_read_memory (bp_tgt->reqstd_address,
  1505. bp_tgt->shadow_contents,
  1506. CSKY_WR_BKPT_MODE);
  1507. if (val)
  1508. return val;
  1509. /* Write the breakpoint. */
  1510. bp = bp_write_record1;
  1511. val = target_write_raw_memory (bp_tgt->reqstd_address,
  1512. bp, CSKY_WR_BKPT_MODE);
  1513. }
  1514. else
  1515. {
  1516. val = target_read_memory (bp_tgt->reqstd_address,
  1517. bp_tgt->shadow_contents,
  1518. CSKY_WR_BKPT_MODE);
  1519. if (val)
  1520. return val;
  1521. val = target_read_memory (bp_tgt->reqstd_address - 2,
  1522. bp_record, 2);
  1523. if (val)
  1524. return val;
  1525. val = target_read_memory (bp_tgt->reqstd_address + 4,
  1526. bp_record + 2, 2);
  1527. if (val)
  1528. return val;
  1529. bp_write_record1[0] = bp_record[0];
  1530. bp_write_record1[1] = bp_record[1];
  1531. bp_write_record2[2] = bp_record[2];
  1532. bp_write_record2[3] = bp_record[3];
  1533. /* Write the breakpoint. */
  1534. bp = bp_write_record1;
  1535. val = target_write_raw_memory (bp_tgt->reqstd_address - 2, bp,
  1536. CSKY_WR_BKPT_MODE);
  1537. if (val)
  1538. return val;
  1539. /* Write the breakpoint. */
  1540. bp = bp_write_record2;
  1541. val = target_write_raw_memory (bp_tgt->reqstd_address + 2, bp,
  1542. CSKY_WR_BKPT_MODE);
  1543. }
  1544. }
  1545. return val;
  1546. }
  1547. /* Restore the breakpoint shadow_contents to the target. */
  1548. static int
  1549. csky_memory_remove_breakpoint (struct gdbarch *gdbarch,
  1550. struct bp_target_info *bp_tgt)
  1551. {
  1552. int val;
  1553. gdb_byte bp_record[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  1554. /* Different for shadow_len 2 or 4. */
  1555. if (bp_tgt->shadow_len == 2)
  1556. {
  1557. /* Do word-sized writes on word-aligned boundaries and read
  1558. padding bytes as necessary. */
  1559. if (bp_tgt->reqstd_address % 4 == 0)
  1560. {
  1561. val = target_read_memory (bp_tgt->reqstd_address + 2,
  1562. bp_record + 2, 2);
  1563. if (val)
  1564. return val;
  1565. bp_record[0] = bp_tgt->shadow_contents[0];
  1566. bp_record[1] = bp_tgt->shadow_contents[1];
  1567. return target_write_raw_memory (bp_tgt->reqstd_address,
  1568. bp_record, CSKY_WR_BKPT_MODE);
  1569. }
  1570. else
  1571. {
  1572. val = target_read_memory (bp_tgt->reqstd_address - 2,
  1573. bp_record, 2);
  1574. if (val)
  1575. return val;
  1576. bp_record[2] = bp_tgt->shadow_contents[0];
  1577. bp_record[3] = bp_tgt->shadow_contents[1];
  1578. return target_write_raw_memory (bp_tgt->reqstd_address - 2,
  1579. bp_record, CSKY_WR_BKPT_MODE);
  1580. }
  1581. }
  1582. else
  1583. {
  1584. /* Do word-sized writes on word-aligned boundaries and read
  1585. padding bytes as necessary. */
  1586. if (bp_tgt->placed_address % 4 == 0)
  1587. {
  1588. return target_write_raw_memory (bp_tgt->reqstd_address,
  1589. bp_tgt->shadow_contents,
  1590. CSKY_WR_BKPT_MODE);
  1591. }
  1592. else
  1593. {
  1594. val = target_read_memory (bp_tgt->reqstd_address - 2,
  1595. bp_record, 2);
  1596. if (val)
  1597. return val;
  1598. val = target_read_memory (bp_tgt->reqstd_address + 4,
  1599. bp_record+6, 2);
  1600. if (val)
  1601. return val;
  1602. bp_record[2] = bp_tgt->shadow_contents[0];
  1603. bp_record[3] = bp_tgt->shadow_contents[1];
  1604. bp_record[4] = bp_tgt->shadow_contents[2];
  1605. bp_record[5] = bp_tgt->shadow_contents[3];
  1606. return target_write_raw_memory (bp_tgt->reqstd_address - 2,
  1607. bp_record,
  1608. CSKY_WR_BKPT_MODE * 2);
  1609. }
  1610. }
  1611. }
  1612. /* Determine link register type. */
  1613. static lr_type_t
  1614. csky_analyze_lr_type (struct gdbarch *gdbarch,
  1615. CORE_ADDR start_pc, CORE_ADDR end_pc)
  1616. {
  1617. CORE_ADDR addr;
  1618. unsigned int insn, insn_len;
  1619. insn_len = 2;
  1620. for (addr = start_pc; addr < end_pc; addr += insn_len)
  1621. {
  1622. insn_len = csky_get_insn (gdbarch, addr, &insn);
  1623. if (insn_len == 4)
  1624. {
  1625. if (CSKY_32_IS_MFCR_EPSR (insn) || CSKY_32_IS_MFCR_EPC (insn)
  1626. || CSKY_32_IS_RTE (insn))
  1627. return LR_TYPE_EPC;
  1628. }
  1629. else if (CSKY_32_IS_MFCR_FPSR (insn) || CSKY_32_IS_MFCR_FPC (insn)
  1630. || CSKY_32_IS_RFI (insn))
  1631. return LR_TYPE_FPC;
  1632. else if (CSKY_32_IS_JMP (insn) || CSKY_32_IS_BR (insn)
  1633. || CSKY_32_IS_JMPIX (insn) || CSKY_32_IS_JMPI (insn))
  1634. return LR_TYPE_R15;
  1635. else
  1636. {
  1637. /* 16 bit instruction. */
  1638. if (CSKY_16_IS_JMP (insn) || CSKY_16_IS_BR (insn)
  1639. || CSKY_16_IS_JMPIX (insn))
  1640. return LR_TYPE_R15;
  1641. }
  1642. }
  1643. return LR_TYPE_R15;
  1644. }
  1645. /* Heuristic unwinder. */
  1646. static struct csky_unwind_cache *
  1647. csky_frame_unwind_cache (struct frame_info *this_frame)
  1648. {
  1649. CORE_ADDR prologue_start, prologue_end, func_end, prev_pc, block_addr;
  1650. struct csky_unwind_cache *cache;
  1651. const struct block *bl;
  1652. unsigned long func_size = 0;
  1653. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1654. unsigned int sp_regnum = CSKY_SP_REGNUM;
  1655. /* Default lr type is r15. */
  1656. lr_type_t lr_type = LR_TYPE_R15;
  1657. cache = FRAME_OBSTACK_ZALLOC (struct csky_unwind_cache);
  1658. cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
  1659. /* Assume there is no frame until proven otherwise. */
  1660. cache->framereg = sp_regnum;
  1661. cache->framesize = 0;
  1662. prev_pc = get_frame_pc (this_frame);
  1663. block_addr = get_frame_address_in_block (this_frame);
  1664. if (find_pc_partial_function (block_addr, NULL, &prologue_start,
  1665. &func_end) == 0)
  1666. /* We couldn't find a function containing block_addr, so bail out
  1667. and hope for the best. */
  1668. return cache;
  1669. /* Get the (function) symbol matching prologue_start. */
  1670. bl = block_for_pc (prologue_start);
  1671. if (bl != NULL)
  1672. func_size = bl->endaddr - bl->startaddr;
  1673. else
  1674. {
  1675. struct bound_minimal_symbol msymbol
  1676. = lookup_minimal_symbol_by_pc (prologue_start);
  1677. if (msymbol.minsym != NULL)
  1678. func_size = MSYMBOL_SIZE (msymbol.minsym);
  1679. }
  1680. /* If FUNC_SIZE is 0 we may have a special-case use of lr
  1681. e.g. exception or interrupt. */
  1682. if (func_size == 0)
  1683. lr_type = csky_analyze_lr_type (gdbarch, prologue_start, func_end);
  1684. prologue_end = std::min (func_end, prev_pc);
  1685. /* Analyze the function prologue. */
  1686. csky_analyze_prologue (gdbarch, prologue_start, prologue_end,
  1687. func_end, this_frame, cache, lr_type);
  1688. /* gdbarch_sp_regnum contains the value and not the address. */
  1689. cache->saved_regs[sp_regnum].set_value (cache->prev_sp);
  1690. return cache;
  1691. }
  1692. /* Implement the this_id function for the normal unwinder. */
  1693. static void
  1694. csky_frame_this_id (struct frame_info *this_frame,
  1695. void **this_prologue_cache, struct frame_id *this_id)
  1696. {
  1697. struct csky_unwind_cache *cache;
  1698. struct frame_id id;
  1699. if (*this_prologue_cache == NULL)
  1700. *this_prologue_cache = csky_frame_unwind_cache (this_frame);
  1701. cache = (struct csky_unwind_cache *) *this_prologue_cache;
  1702. /* This marks the outermost frame. */
  1703. if (cache->prev_sp == 0)
  1704. return;
  1705. id = frame_id_build (cache->prev_sp, get_frame_func (this_frame));
  1706. *this_id = id;
  1707. }
  1708. /* Implement the prev_register function for the normal unwinder. */
  1709. static struct value *
  1710. csky_frame_prev_register (struct frame_info *this_frame,
  1711. void **this_prologue_cache, int regnum)
  1712. {
  1713. struct csky_unwind_cache *cache;
  1714. if (*this_prologue_cache == NULL)
  1715. *this_prologue_cache = csky_frame_unwind_cache (this_frame);
  1716. cache = (struct csky_unwind_cache *) *this_prologue_cache;
  1717. return trad_frame_get_prev_register (this_frame, cache->saved_regs,
  1718. regnum);
  1719. }
  1720. /* Data structures for the normal prologue-analysis-based
  1721. unwinder. */
  1722. static const struct frame_unwind csky_unwind_cache = {
  1723. "cski prologue",
  1724. NORMAL_FRAME,
  1725. default_frame_unwind_stop_reason,
  1726. csky_frame_this_id,
  1727. csky_frame_prev_register,
  1728. NULL,
  1729. default_frame_sniffer,
  1730. NULL,
  1731. NULL
  1732. };
  1733. static int
  1734. csky_stub_unwind_sniffer (const struct frame_unwind *self,
  1735. struct frame_info *this_frame,
  1736. void **this_prologue_cache)
  1737. {
  1738. CORE_ADDR addr_in_block;
  1739. addr_in_block = get_frame_address_in_block (this_frame);
  1740. if (find_pc_partial_function (addr_in_block, NULL, NULL, NULL) == 0
  1741. || in_plt_section (addr_in_block))
  1742. return 1;
  1743. return 0;
  1744. }
  1745. static struct csky_unwind_cache *
  1746. csky_make_stub_cache (struct frame_info *this_frame)
  1747. {
  1748. struct csky_unwind_cache *cache;
  1749. cache = FRAME_OBSTACK_ZALLOC (struct csky_unwind_cache);
  1750. cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
  1751. cache->prev_sp = get_frame_register_unsigned (this_frame, CSKY_SP_REGNUM);
  1752. return cache;
  1753. }
  1754. static void
  1755. csky_stub_this_id (struct frame_info *this_frame,
  1756. void **this_cache,
  1757. struct frame_id *this_id)
  1758. {
  1759. struct csky_unwind_cache *cache;
  1760. if (*this_cache == NULL)
  1761. *this_cache = csky_make_stub_cache (this_frame);
  1762. cache = (struct csky_unwind_cache *) *this_cache;
  1763. /* Our frame ID for a stub frame is the current SP and LR. */
  1764. *this_id = frame_id_build (cache->prev_sp, get_frame_pc (this_frame));
  1765. }
  1766. static struct value *
  1767. csky_stub_prev_register (struct frame_info *this_frame,
  1768. void **this_cache,
  1769. int prev_regnum)
  1770. {
  1771. struct csky_unwind_cache *cache;
  1772. if (*this_cache == NULL)
  1773. *this_cache = csky_make_stub_cache (this_frame);
  1774. cache = (struct csky_unwind_cache *) *this_cache;
  1775. /* If we are asked to unwind the PC, then return the LR. */
  1776. if (prev_regnum == CSKY_PC_REGNUM)
  1777. {
  1778. CORE_ADDR lr;
  1779. lr = frame_unwind_register_unsigned (this_frame, CSKY_LR_REGNUM);
  1780. return frame_unwind_got_constant (this_frame, prev_regnum, lr);
  1781. }
  1782. if (prev_regnum == CSKY_SP_REGNUM)
  1783. return frame_unwind_got_constant (this_frame, prev_regnum, cache->prev_sp);
  1784. return trad_frame_get_prev_register (this_frame, cache->saved_regs,
  1785. prev_regnum);
  1786. }
  1787. static frame_unwind csky_stub_unwind = {
  1788. "csky stub",
  1789. NORMAL_FRAME,
  1790. default_frame_unwind_stop_reason,
  1791. csky_stub_this_id,
  1792. csky_stub_prev_register,
  1793. NULL,
  1794. csky_stub_unwind_sniffer
  1795. };
  1796. /* Implement the this_base, this_locals, and this_args hooks
  1797. for the normal unwinder. */
  1798. static CORE_ADDR
  1799. csky_frame_base_address (struct frame_info *this_frame, void **this_cache)
  1800. {
  1801. struct csky_unwind_cache *cache;
  1802. if (*this_cache == NULL)
  1803. *this_cache = csky_frame_unwind_cache (this_frame);
  1804. cache = (struct csky_unwind_cache *) *this_cache;
  1805. return cache->prev_sp - cache->framesize;
  1806. }
  1807. static const struct frame_base csky_frame_base = {
  1808. &csky_unwind_cache,
  1809. csky_frame_base_address,
  1810. csky_frame_base_address,
  1811. csky_frame_base_address
  1812. };
  1813. /* Initialize register access method. */
  1814. static void
  1815. csky_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
  1816. struct dwarf2_frame_state_reg *reg,
  1817. struct frame_info *this_frame)
  1818. {
  1819. if (regnum == gdbarch_pc_regnum (gdbarch))
  1820. reg->how = DWARF2_FRAME_REG_RA;
  1821. else if (regnum == gdbarch_sp_regnum (gdbarch))
  1822. reg->how = DWARF2_FRAME_REG_CFA;
  1823. }
  1824. /* Create csky register groups. */
  1825. static void
  1826. csky_init_reggroup ()
  1827. {
  1828. cr_reggroup = reggroup_new ("cr", USER_REGGROUP);
  1829. fr_reggroup = reggroup_new ("fr", USER_REGGROUP);
  1830. vr_reggroup = reggroup_new ("vr", USER_REGGROUP);
  1831. mmu_reggroup = reggroup_new ("mmu", USER_REGGROUP);
  1832. prof_reggroup = reggroup_new ("profiling", USER_REGGROUP);
  1833. }
  1834. /* Add register groups into reggroup list. */
  1835. static void
  1836. csky_add_reggroups (struct gdbarch *gdbarch)
  1837. {
  1838. reggroup_add (gdbarch, cr_reggroup);
  1839. reggroup_add (gdbarch, fr_reggroup);
  1840. reggroup_add (gdbarch, vr_reggroup);
  1841. reggroup_add (gdbarch, mmu_reggroup);
  1842. reggroup_add (gdbarch, prof_reggroup);
  1843. }
  1844. /* Return the groups that a CSKY register can be categorised into. */
  1845. static int
  1846. csky_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
  1847. const struct reggroup *reggroup)
  1848. {
  1849. int raw_p;
  1850. if (gdbarch_register_name (gdbarch, regnum) == NULL
  1851. || gdbarch_register_name (gdbarch, regnum)[0] == '\0')
  1852. return 0;
  1853. if (reggroup == all_reggroup)
  1854. return 1;
  1855. raw_p = regnum < gdbarch_num_regs (gdbarch);
  1856. if (reggroup == save_reggroup || reggroup == restore_reggroup)
  1857. return raw_p;
  1858. if (((regnum >= CSKY_R0_REGNUM) && (regnum <= CSKY_R0_REGNUM + 31))
  1859. && (reggroup == general_reggroup))
  1860. return 1;
  1861. if (((regnum == CSKY_PC_REGNUM)
  1862. || ((regnum >= CSKY_CR0_REGNUM)
  1863. && (regnum <= CSKY_CR0_REGNUM + 30)))
  1864. && (reggroup == cr_reggroup))
  1865. return 2;
  1866. if ((((regnum >= CSKY_VR0_REGNUM) && (regnum <= CSKY_VR0_REGNUM + 15))
  1867. || ((regnum >= CSKY_VCR0_REGNUM)
  1868. && (regnum <= CSKY_VCR0_REGNUM + 2)))
  1869. && (reggroup == vr_reggroup))
  1870. return 3;
  1871. if (((regnum >= CSKY_MMU_REGNUM) && (regnum <= CSKY_MMU_REGNUM + 8))
  1872. && (reggroup == mmu_reggroup))
  1873. return 4;
  1874. if (((regnum >= CSKY_PROFCR_REGNUM)
  1875. && (regnum <= CSKY_PROFCR_REGNUM + 48))
  1876. && (reggroup == prof_reggroup))
  1877. return 5;
  1878. if ((((regnum >= CSKY_FR0_REGNUM) && (regnum <= CSKY_FR0_REGNUM + 15))
  1879. || ((regnum >= CSKY_VCR0_REGNUM) && (regnum <= CSKY_VCR0_REGNUM + 2)))
  1880. && (reggroup == fr_reggroup))
  1881. return 6;
  1882. return 0;
  1883. }
  1884. /* Implement the dwarf2_reg_to_regnum gdbarch method. */
  1885. static int
  1886. csky_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int dw_reg)
  1887. {
  1888. if (dw_reg < 0 || dw_reg >= CSKY_NUM_REGS)
  1889. return -1;
  1890. return dw_reg;
  1891. }
  1892. /* Override interface for command: info register. */
  1893. static void
  1894. csky_print_registers_info (struct gdbarch *gdbarch, struct ui_file *file,
  1895. struct frame_info *frame, int regnum, int all)
  1896. {
  1897. /* Call default print_registers_info function. */
  1898. default_print_registers_info (gdbarch, file, frame, regnum, all);
  1899. /* For command: info register. */
  1900. if (regnum == -1 && all == 0)
  1901. {
  1902. default_print_registers_info (gdbarch, file, frame,
  1903. CSKY_PC_REGNUM, 0);
  1904. default_print_registers_info (gdbarch, file, frame,
  1905. CSKY_EPC_REGNUM, 0);
  1906. default_print_registers_info (gdbarch, file, frame,
  1907. CSKY_CR0_REGNUM, 0);
  1908. default_print_registers_info (gdbarch, file, frame,
  1909. CSKY_EPSR_REGNUM, 0);
  1910. }
  1911. return;
  1912. }
  1913. /* Initialize the current architecture based on INFO. If possible,
  1914. re-use an architecture from ARCHES, which is a list of
  1915. architectures already created during this debugging session.
  1916. Called at program startup, when reading a core file, and when
  1917. reading a binary file. */
  1918. static struct gdbarch *
  1919. csky_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  1920. {
  1921. struct gdbarch *gdbarch;
  1922. /* Find a candidate among the list of pre-declared architectures. */
  1923. arches = gdbarch_list_lookup_by_info (arches, &info);
  1924. if (arches != NULL)
  1925. return arches->gdbarch;
  1926. /* None found, create a new architecture from the information
  1927. provided. */
  1928. csky_gdbarch_tdep *tdep = new csky_gdbarch_tdep;
  1929. gdbarch = gdbarch_alloc (&info, tdep);
  1930. /* Target data types. */
  1931. set_gdbarch_ptr_bit (gdbarch, 32);
  1932. set_gdbarch_addr_bit (gdbarch, 32);
  1933. set_gdbarch_short_bit (gdbarch, 16);
  1934. set_gdbarch_int_bit (gdbarch, 32);
  1935. set_gdbarch_long_bit (gdbarch, 32);
  1936. set_gdbarch_long_long_bit (gdbarch, 64);
  1937. set_gdbarch_float_bit (gdbarch, 32);
  1938. set_gdbarch_double_bit (gdbarch, 64);
  1939. set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
  1940. set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
  1941. /* Information about the target architecture. */
  1942. set_gdbarch_return_value (gdbarch, csky_return_value);
  1943. set_gdbarch_breakpoint_kind_from_pc (gdbarch, csky_breakpoint_kind_from_pc);
  1944. set_gdbarch_sw_breakpoint_from_kind (gdbarch, csky_sw_breakpoint_from_kind);
  1945. /* Register architecture. */
  1946. set_gdbarch_num_regs (gdbarch, CSKY_NUM_REGS);
  1947. set_gdbarch_pc_regnum (gdbarch, CSKY_PC_REGNUM);
  1948. set_gdbarch_sp_regnum (gdbarch, CSKY_SP_REGNUM);
  1949. set_gdbarch_register_name (gdbarch, csky_register_name);
  1950. set_gdbarch_register_type (gdbarch, csky_register_type);
  1951. set_gdbarch_read_pc (gdbarch, csky_read_pc);
  1952. set_gdbarch_write_pc (gdbarch, csky_write_pc);
  1953. set_gdbarch_print_registers_info (gdbarch, csky_print_registers_info);
  1954. csky_add_reggroups (gdbarch);
  1955. set_gdbarch_register_reggroup_p (gdbarch, csky_register_reggroup_p);
  1956. set_gdbarch_stab_reg_to_regnum (gdbarch, csky_dwarf_reg_to_regnum);
  1957. set_gdbarch_dwarf2_reg_to_regnum (gdbarch, csky_dwarf_reg_to_regnum);
  1958. dwarf2_frame_set_init_reg (gdbarch, csky_dwarf2_frame_init_reg);
  1959. /* Functions to analyze frames. */
  1960. frame_base_set_default (gdbarch, &csky_frame_base);
  1961. set_gdbarch_skip_prologue (gdbarch, csky_skip_prologue);
  1962. set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  1963. set_gdbarch_frame_align (gdbarch, csky_frame_align);
  1964. set_gdbarch_stack_frame_destroyed_p (gdbarch, csky_stack_frame_destroyed_p);
  1965. /* Functions handling dummy frames. */
  1966. set_gdbarch_push_dummy_call (gdbarch, csky_push_dummy_call);
  1967. /* Frame unwinders. Use DWARF debug info if available,
  1968. otherwise use our own unwinder. */
  1969. dwarf2_append_unwinders (gdbarch);
  1970. frame_unwind_append_unwinder (gdbarch, &csky_stub_unwind);
  1971. frame_unwind_append_unwinder (gdbarch, &csky_unwind_cache);
  1972. /* Breakpoints. */
  1973. set_gdbarch_memory_insert_breakpoint (gdbarch,
  1974. csky_memory_insert_breakpoint);
  1975. set_gdbarch_memory_remove_breakpoint (gdbarch,
  1976. csky_memory_remove_breakpoint);
  1977. /* Hook in ABI-specific overrides, if they have been registered. */
  1978. gdbarch_init_osabi (info, gdbarch);
  1979. /* Support simple overlay manager. */
  1980. set_gdbarch_overlay_update (gdbarch, simple_overlay_update);
  1981. set_gdbarch_char_signed (gdbarch, 0);
  1982. return gdbarch;
  1983. }
  1984. void _initialize_csky_tdep ();
  1985. void
  1986. _initialize_csky_tdep ()
  1987. {
  1988. register_gdbarch_init (bfd_arch_csky, csky_gdbarch_init);
  1989. csky_init_reggroup ();
  1990. /* Allow debugging this file's internals. */
  1991. add_setshow_boolean_cmd ("csky", class_maintenance, &csky_debug,
  1992. _("Set C-Sky debugging."),
  1993. _("Show C-Sky debugging."),
  1994. _("When on, C-Sky specific debugging is enabled."),
  1995. NULL,
  1996. NULL,
  1997. &setdebuglist, &showdebuglist);
  1998. }