xtensa-tdep.c 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279
  1. /* Target-dependent code for the Xtensa port of GDB, the GNU debugger.
  2. Copyright (C) 2003-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "frame.h"
  16. #include "solib-svr4.h"
  17. #include "symtab.h"
  18. #include "gdbtypes.h"
  19. #include "gdbcore.h"
  20. #include "value.h"
  21. #include "osabi.h"
  22. #include "regcache.h"
  23. #include "reggroups.h"
  24. #include "regset.h"
  25. #include "dwarf2/frame.h"
  26. #include "frame-base.h"
  27. #include "frame-unwind.h"
  28. #include "arch-utils.h"
  29. #include "gdbarch.h"
  30. #include "command.h"
  31. #include "gdbcmd.h"
  32. #include "xtensa-isa.h"
  33. #include "xtensa-tdep.h"
  34. #include "xtensa-config.h"
  35. #include <algorithm>
  36. static unsigned int xtensa_debug_level = 0;
  37. #define DEBUGWARN(args...) \
  38. if (xtensa_debug_level > 0) \
  39. gdb_printf (gdb_stdlog, "(warn ) " args)
  40. #define DEBUGINFO(args...) \
  41. if (xtensa_debug_level > 1) \
  42. gdb_printf (gdb_stdlog, "(info ) " args)
  43. #define DEBUGTRACE(args...) \
  44. if (xtensa_debug_level > 2) \
  45. gdb_printf (gdb_stdlog, "(trace) " args)
  46. #define DEBUGVERB(args...) \
  47. if (xtensa_debug_level > 3) \
  48. gdb_printf (gdb_stdlog, "(verb ) " args)
  49. /* According to the ABI, the SP must be aligned to 16-byte boundaries. */
  50. #define SP_ALIGNMENT 16
  51. /* On Windowed ABI, we use a6 through a11 for passing arguments
  52. to a function called by GDB because CALL4 is used. */
  53. #define ARGS_NUM_REGS 6
  54. #define REGISTER_SIZE 4
  55. /* Extract the call size from the return address or PS register. */
  56. #define PS_CALLINC_SHIFT 16
  57. #define PS_CALLINC_MASK 0x00030000
  58. #define CALLINC(ps) (((ps) & PS_CALLINC_MASK) >> PS_CALLINC_SHIFT)
  59. #define WINSIZE(ra) (4 * (( (ra) >> 30) & 0x3))
  60. /* On TX, hardware can be configured without Exception Option.
  61. There is no PS register in this case. Inside XT-GDB, let us treat
  62. it as a virtual read-only register always holding the same value. */
  63. #define TX_PS 0x20
  64. /* ABI-independent macros. */
  65. #define ARG_NOF(tdep) \
  66. (tdep->call_abi \
  67. == CallAbiCall0Only ? C0_NARGS : (ARGS_NUM_REGS))
  68. #define ARG_1ST(tdep) \
  69. (tdep->call_abi == CallAbiCall0Only \
  70. ? (tdep->a0_base + C0_ARGS) \
  71. : (tdep->a0_base + 6))
  72. /* XTENSA_IS_ENTRY tests whether the first byte of an instruction
  73. indicates that the instruction is an ENTRY instruction. */
  74. #define XTENSA_IS_ENTRY(gdbarch, op1) \
  75. ((gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG) \
  76. ? ((op1) == 0x6c) : ((op1) == 0x36))
  77. #define XTENSA_ENTRY_LENGTH 3
  78. /* windowing_enabled() returns true, if windowing is enabled.
  79. WOE must be set to 1; EXCM to 0.
  80. Note: We assume that EXCM is always 0 for XEA1. */
  81. #define PS_WOE (1<<18)
  82. #define PS_EXC (1<<4)
  83. /* Big enough to hold the size of the largest register in bytes. */
  84. #define XTENSA_MAX_REGISTER_SIZE 64
  85. static int
  86. windowing_enabled (struct gdbarch *gdbarch, unsigned int ps)
  87. {
  88. xtensa_gdbarch_tdep *tdep = (xtensa_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  89. /* If we know CALL0 ABI is set explicitly, say it is Call0. */
  90. if (tdep->call_abi == CallAbiCall0Only)
  91. return 0;
  92. return ((ps & PS_EXC) == 0 && (ps & PS_WOE) != 0);
  93. }
  94. /* Convert a live A-register number to the corresponding AR-register
  95. number. */
  96. static int
  97. arreg_number (struct gdbarch *gdbarch, int a_regnum, ULONGEST wb)
  98. {
  99. xtensa_gdbarch_tdep *tdep = (xtensa_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  100. int arreg;
  101. arreg = a_regnum - tdep->a0_base;
  102. arreg += (wb & ((tdep->num_aregs - 1) >> 2)) << WB_SHIFT;
  103. arreg &= tdep->num_aregs - 1;
  104. return arreg + tdep->ar_base;
  105. }
  106. /* Convert a live AR-register number to the corresponding A-register order
  107. number in a range [0..15]. Return -1, if AR_REGNUM is out of WB window. */
  108. static int
  109. areg_number (struct gdbarch *gdbarch, int ar_regnum, unsigned int wb)
  110. {
  111. xtensa_gdbarch_tdep *tdep = (xtensa_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  112. int areg;
  113. areg = ar_regnum - tdep->ar_base;
  114. if (areg < 0 || areg >= tdep->num_aregs)
  115. return -1;
  116. areg = (areg - wb * 4) & (tdep->num_aregs - 1);
  117. return (areg > 15) ? -1 : areg;
  118. }
  119. /* Read Xtensa register directly from the hardware. */
  120. static unsigned long
  121. xtensa_read_register (int regnum)
  122. {
  123. ULONGEST value;
  124. regcache_raw_read_unsigned (get_current_regcache (), regnum, &value);
  125. return (unsigned long) value;
  126. }
  127. /* Write Xtensa register directly to the hardware. */
  128. static void
  129. xtensa_write_register (int regnum, ULONGEST value)
  130. {
  131. regcache_raw_write_unsigned (get_current_regcache (), regnum, value);
  132. }
  133. /* Return the window size of the previous call to the function from which we
  134. have just returned.
  135. This function is used to extract the return value after a called function
  136. has returned to the caller. On Xtensa, the register that holds the return
  137. value (from the perspective of the caller) depends on what call
  138. instruction was used. For now, we are assuming that the call instruction
  139. precedes the current address, so we simply analyze the call instruction.
  140. If we are in a dummy frame, we simply return 4 as we used a 'pseudo-call4'
  141. method to call the inferior function. */
  142. static int
  143. extract_call_winsize (struct gdbarch *gdbarch, CORE_ADDR pc)
  144. {
  145. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  146. int winsize = 4;
  147. int insn;
  148. gdb_byte buf[4];
  149. DEBUGTRACE ("extract_call_winsize (pc = 0x%08x)\n", (int) pc);
  150. /* Read the previous instruction (should be a call[x]{4|8|12}. */
  151. read_memory (pc-3, buf, 3);
  152. insn = extract_unsigned_integer (buf, 3, byte_order);
  153. /* Decode call instruction:
  154. Little Endian
  155. call{0,4,8,12} OFFSET || {00,01,10,11} || 0101
  156. callx{0,4,8,12} OFFSET || 11 || {00,01,10,11} || 0000
  157. Big Endian
  158. call{0,4,8,12} 0101 || {00,01,10,11} || OFFSET
  159. callx{0,4,8,12} 0000 || {00,01,10,11} || 11 || OFFSET. */
  160. if (byte_order == BFD_ENDIAN_LITTLE)
  161. {
  162. if (((insn & 0xf) == 0x5) || ((insn & 0xcf) == 0xc0))
  163. winsize = (insn & 0x30) >> 2; /* 0, 4, 8, 12. */
  164. }
  165. else
  166. {
  167. if (((insn >> 20) == 0x5) || (((insn >> 16) & 0xf3) == 0x03))
  168. winsize = (insn >> 16) & 0xc; /* 0, 4, 8, 12. */
  169. }
  170. return winsize;
  171. }
  172. /* REGISTER INFORMATION */
  173. /* Find register by name. */
  174. static int
  175. xtensa_find_register_by_name (struct gdbarch *gdbarch, const char *name)
  176. {
  177. int i;
  178. xtensa_gdbarch_tdep *tdep = (xtensa_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  179. for (i = 0; i < gdbarch_num_cooked_regs (gdbarch); i++)
  180. if (strcasecmp (tdep->regmap[i].name, name) == 0)
  181. return i;
  182. return -1;
  183. }
  184. /* Returns the name of a register. */
  185. static const char *
  186. xtensa_register_name (struct gdbarch *gdbarch, int regnum)
  187. {
  188. xtensa_gdbarch_tdep *tdep = (xtensa_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  189. /* Return the name stored in the register map. */
  190. if (regnum >= 0 && regnum < gdbarch_num_cooked_regs (gdbarch))
  191. return tdep->regmap[regnum].name;
  192. internal_error (__FILE__, __LINE__, _("invalid register %d"), regnum);
  193. return 0;
  194. }
  195. /* Return the type of a register. Create a new type, if necessary. */
  196. static struct type *
  197. xtensa_register_type (struct gdbarch *gdbarch, int regnum)
  198. {
  199. xtensa_gdbarch_tdep *tdep = (xtensa_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  200. /* Return signed integer for ARx and Ax registers. */
  201. if ((regnum >= tdep->ar_base
  202. && regnum < tdep->ar_base + tdep->num_aregs)
  203. || (regnum >= tdep->a0_base
  204. && regnum < tdep->a0_base + 16))
  205. return builtin_type (gdbarch)->builtin_int;
  206. if (regnum == gdbarch_pc_regnum (gdbarch)
  207. || regnum == tdep->a0_base + 1)
  208. return builtin_type (gdbarch)->builtin_data_ptr;
  209. /* Return the stored type for all other registers. */
  210. else if (regnum >= 0 && regnum < gdbarch_num_cooked_regs (gdbarch))
  211. {
  212. xtensa_register_t* reg = &tdep->regmap[regnum];
  213. /* Set ctype for this register (only the first time). */
  214. if (reg->ctype == 0)
  215. {
  216. struct ctype_cache *tp;
  217. int size = reg->byte_size;
  218. /* We always use the memory representation,
  219. even if the register width is smaller. */
  220. switch (size)
  221. {
  222. case 1:
  223. reg->ctype = builtin_type (gdbarch)->builtin_uint8;
  224. break;
  225. case 2:
  226. reg->ctype = builtin_type (gdbarch)->builtin_uint16;
  227. break;
  228. case 4:
  229. reg->ctype = builtin_type (gdbarch)->builtin_uint32;
  230. break;
  231. case 8:
  232. reg->ctype = builtin_type (gdbarch)->builtin_uint64;
  233. break;
  234. case 16:
  235. reg->ctype = builtin_type (gdbarch)->builtin_uint128;
  236. break;
  237. default:
  238. for (tp = tdep->type_entries; tp != NULL; tp = tp->next)
  239. if (tp->size == size)
  240. break;
  241. if (tp == NULL)
  242. {
  243. std::string name = string_printf ("int%d", size * 8);
  244. tp = XNEW (struct ctype_cache);
  245. tp->next = tdep->type_entries;
  246. tdep->type_entries = tp;
  247. tp->size = size;
  248. tp->virtual_type
  249. = arch_integer_type (gdbarch, size * 8, 1, name.c_str ());
  250. }
  251. reg->ctype = tp->virtual_type;
  252. }
  253. }
  254. return reg->ctype;
  255. }
  256. internal_error (__FILE__, __LINE__, _("invalid register number %d"), regnum);
  257. return 0;
  258. }
  259. /* Return the 'local' register number for stubs, dwarf2, etc.
  260. The debugging information enumerates registers starting from 0 for A0
  261. to n for An. So, we only have to add the base number for A0. */
  262. static int
  263. xtensa_reg_to_regnum (struct gdbarch *gdbarch, int regnum)
  264. {
  265. int i;
  266. xtensa_gdbarch_tdep *tdep = (xtensa_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  267. if (regnum >= 0 && regnum < 16)
  268. return tdep->a0_base + regnum;
  269. for (i = 0; i < gdbarch_num_cooked_regs (gdbarch); i++)
  270. if (regnum == tdep->regmap[i].target_number)
  271. return i;
  272. return -1;
  273. }
  274. /* Write the bits of a masked register to the various registers.
  275. Only the masked areas of these registers are modified; the other
  276. fields are untouched. The size of masked registers is always less
  277. than or equal to 32 bits. */
  278. static void
  279. xtensa_register_write_masked (struct regcache *regcache,
  280. xtensa_register_t *reg, const gdb_byte *buffer)
  281. {
  282. unsigned int value[(XTENSA_MAX_REGISTER_SIZE + 3) / 4];
  283. const xtensa_mask_t *mask = reg->mask;
  284. int shift = 0; /* Shift for next mask (mod 32). */
  285. int start, size; /* Start bit and size of current mask. */
  286. unsigned int *ptr = value;
  287. unsigned int regval, m, mem = 0;
  288. int bytesize = reg->byte_size;
  289. int bitsize = bytesize * 8;
  290. int i, r;
  291. DEBUGTRACE ("xtensa_register_write_masked ()\n");
  292. /* Copy the masked register to host byte-order. */
  293. if (gdbarch_byte_order (regcache->arch ()) == BFD_ENDIAN_BIG)
  294. for (i = 0; i < bytesize; i++)
  295. {
  296. mem >>= 8;
  297. mem |= (buffer[bytesize - i - 1] << 24);
  298. if ((i & 3) == 3)
  299. *ptr++ = mem;
  300. }
  301. else
  302. for (i = 0; i < bytesize; i++)
  303. {
  304. mem >>= 8;
  305. mem |= (buffer[i] << 24);
  306. if ((i & 3) == 3)
  307. *ptr++ = mem;
  308. }
  309. /* We might have to shift the final value:
  310. bytesize & 3 == 0 -> nothing to do, we use the full 32 bits,
  311. bytesize & 3 == x -> shift (4-x) * 8. */
  312. *ptr = mem >> (((0 - bytesize) & 3) * 8);
  313. ptr = value;
  314. mem = *ptr;
  315. /* Write the bits to the masked areas of the other registers. */
  316. for (i = 0; i < mask->count; i++)
  317. {
  318. start = mask->mask[i].bit_start;
  319. size = mask->mask[i].bit_size;
  320. regval = mem >> shift;
  321. if ((shift += size) > bitsize)
  322. error (_("size of all masks is larger than the register"));
  323. if (shift >= 32)
  324. {
  325. mem = *(++ptr);
  326. shift -= 32;
  327. bitsize -= 32;
  328. if (shift > 0)
  329. regval |= mem << (size - shift);
  330. }
  331. /* Make sure we have a valid register. */
  332. r = mask->mask[i].reg_num;
  333. if (r >= 0 && size > 0)
  334. {
  335. /* Don't overwrite the unmasked areas. */
  336. ULONGEST old_val;
  337. regcache_cooked_read_unsigned (regcache, r, &old_val);
  338. m = 0xffffffff >> (32 - size) << start;
  339. regval <<= start;
  340. regval = (regval & m) | (old_val & ~m);
  341. regcache_cooked_write_unsigned (regcache, r, regval);
  342. }
  343. }
  344. }
  345. /* Read a tie state or mapped registers. Read the masked areas
  346. of the registers and assemble them into a single value. */
  347. static enum register_status
  348. xtensa_register_read_masked (readable_regcache *regcache,
  349. xtensa_register_t *reg, gdb_byte *buffer)
  350. {
  351. unsigned int value[(XTENSA_MAX_REGISTER_SIZE + 3) / 4];
  352. const xtensa_mask_t *mask = reg->mask;
  353. int shift = 0;
  354. int start, size;
  355. unsigned int *ptr = value;
  356. unsigned int regval, mem = 0;
  357. int bytesize = reg->byte_size;
  358. int bitsize = bytesize * 8;
  359. int i;
  360. DEBUGTRACE ("xtensa_register_read_masked (reg \"%s\", ...)\n",
  361. reg->name == 0 ? "" : reg->name);
  362. /* Assemble the register from the masked areas of other registers. */
  363. for (i = 0; i < mask->count; i++)
  364. {
  365. int r = mask->mask[i].reg_num;
  366. if (r >= 0)
  367. {
  368. enum register_status status;
  369. ULONGEST val;
  370. status = regcache->cooked_read (r, &val);
  371. if (status != REG_VALID)
  372. return status;
  373. regval = (unsigned int) val;
  374. }
  375. else
  376. regval = 0;
  377. start = mask->mask[i].bit_start;
  378. size = mask->mask[i].bit_size;
  379. regval >>= start;
  380. if (size < 32)
  381. regval &= (0xffffffff >> (32 - size));
  382. mem |= regval << shift;
  383. if ((shift += size) > bitsize)
  384. error (_("size of all masks is larger than the register"));
  385. if (shift >= 32)
  386. {
  387. *ptr++ = mem;
  388. bitsize -= 32;
  389. shift -= 32;
  390. if (shift == 0)
  391. mem = 0;
  392. else
  393. mem = regval >> (size - shift);
  394. }
  395. }
  396. if (shift > 0)
  397. *ptr = mem;
  398. /* Copy value to target byte order. */
  399. ptr = value;
  400. mem = *ptr;
  401. if (gdbarch_byte_order (regcache->arch ()) == BFD_ENDIAN_BIG)
  402. for (i = 0; i < bytesize; i++)
  403. {
  404. if ((i & 3) == 0)
  405. mem = *ptr++;
  406. buffer[bytesize - i - 1] = mem & 0xff;
  407. mem >>= 8;
  408. }
  409. else
  410. for (i = 0; i < bytesize; i++)
  411. {
  412. if ((i & 3) == 0)
  413. mem = *ptr++;
  414. buffer[i] = mem & 0xff;
  415. mem >>= 8;
  416. }
  417. return REG_VALID;
  418. }
  419. /* Read pseudo registers. */
  420. static enum register_status
  421. xtensa_pseudo_register_read (struct gdbarch *gdbarch,
  422. readable_regcache *regcache,
  423. int regnum,
  424. gdb_byte *buffer)
  425. {
  426. DEBUGTRACE ("xtensa_pseudo_register_read (... regnum = %d (%s) ...)\n",
  427. regnum, xtensa_register_name (gdbarch, regnum));
  428. xtensa_gdbarch_tdep *tdep = (xtensa_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  429. /* Read aliases a0..a15, if this is a Windowed ABI. */
  430. if (tdep->isa_use_windowed_registers
  431. && (regnum >= tdep->a0_base)
  432. && (regnum <= tdep->a0_base + 15))
  433. {
  434. ULONGEST value;
  435. enum register_status status;
  436. status = regcache->raw_read (tdep->wb_regnum,
  437. &value);
  438. if (status != REG_VALID)
  439. return status;
  440. regnum = arreg_number (gdbarch, regnum, value);
  441. }
  442. /* We can always read non-pseudo registers. */
  443. if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch))
  444. return regcache->raw_read (regnum, buffer);
  445. /* We have to find out how to deal with priveleged registers.
  446. Let's treat them as pseudo-registers, but we cannot read/write them. */
  447. else if (tdep->call_abi == CallAbiCall0Only
  448. || regnum < tdep->a0_base)
  449. {
  450. buffer[0] = (gdb_byte)0;
  451. buffer[1] = (gdb_byte)0;
  452. buffer[2] = (gdb_byte)0;
  453. buffer[3] = (gdb_byte)0;
  454. return REG_VALID;
  455. }
  456. /* Pseudo registers. */
  457. else if (regnum >= 0 && regnum < gdbarch_num_cooked_regs (gdbarch))
  458. {
  459. xtensa_register_t *reg = &tdep->regmap[regnum];
  460. xtensa_register_type_t type = reg->type;
  461. int flags = tdep->target_flags;
  462. /* We cannot read Unknown or Unmapped registers. */
  463. if (type == xtRegisterTypeUnmapped || type == xtRegisterTypeUnknown)
  464. {
  465. if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
  466. {
  467. warning (_("cannot read register %s"),
  468. xtensa_register_name (gdbarch, regnum));
  469. return REG_VALID;
  470. }
  471. }
  472. /* Some targets cannot read TIE register files. */
  473. else if (type == xtRegisterTypeTieRegfile)
  474. {
  475. /* Use 'fetch' to get register? */
  476. if (flags & xtTargetFlagsUseFetchStore)
  477. {
  478. warning (_("cannot read register"));
  479. return REG_VALID;
  480. }
  481. /* On some targets (esp. simulators), we can always read the reg. */
  482. else if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
  483. {
  484. warning (_("cannot read register"));
  485. return REG_VALID;
  486. }
  487. }
  488. /* We can always read mapped registers. */
  489. else if (type == xtRegisterTypeMapped || type == xtRegisterTypeTieState)
  490. return xtensa_register_read_masked (regcache, reg, buffer);
  491. /* Assume that we can read the register. */
  492. return regcache->raw_read (regnum, buffer);
  493. }
  494. else
  495. internal_error (__FILE__, __LINE__,
  496. _("invalid register number %d"), regnum);
  497. }
  498. /* Write pseudo registers. */
  499. static void
  500. xtensa_pseudo_register_write (struct gdbarch *gdbarch,
  501. struct regcache *regcache,
  502. int regnum,
  503. const gdb_byte *buffer)
  504. {
  505. DEBUGTRACE ("xtensa_pseudo_register_write (... regnum = %d (%s) ...)\n",
  506. regnum, xtensa_register_name (gdbarch, regnum));
  507. xtensa_gdbarch_tdep *tdep = (xtensa_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  508. /* Renumber register, if aliases a0..a15 on Windowed ABI. */
  509. if (tdep->isa_use_windowed_registers
  510. && (regnum >= tdep->a0_base)
  511. && (regnum <= tdep->a0_base + 15))
  512. {
  513. ULONGEST value;
  514. regcache_raw_read_unsigned (regcache,
  515. tdep->wb_regnum, &value);
  516. regnum = arreg_number (gdbarch, regnum, value);
  517. }
  518. /* We can always write 'core' registers.
  519. Note: We might have converted Ax->ARy. */
  520. if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch))
  521. regcache->raw_write (regnum, buffer);
  522. /* We have to find out how to deal with priveleged registers.
  523. Let's treat them as pseudo-registers, but we cannot read/write them. */
  524. else if (regnum < tdep->a0_base)
  525. {
  526. return;
  527. }
  528. /* Pseudo registers. */
  529. else if (regnum >= 0 && regnum < gdbarch_num_cooked_regs (gdbarch))
  530. {
  531. xtensa_register_t *reg = &tdep->regmap[regnum];
  532. xtensa_register_type_t type = reg->type;
  533. int flags = tdep->target_flags;
  534. /* On most targets, we cannot write registers
  535. of type "Unknown" or "Unmapped". */
  536. if (type == xtRegisterTypeUnmapped || type == xtRegisterTypeUnknown)
  537. {
  538. if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
  539. {
  540. warning (_("cannot write register %s"),
  541. xtensa_register_name (gdbarch, regnum));
  542. return;
  543. }
  544. }
  545. /* Some targets cannot read TIE register files. */
  546. else if (type == xtRegisterTypeTieRegfile)
  547. {
  548. /* Use 'store' to get register? */
  549. if (flags & xtTargetFlagsUseFetchStore)
  550. {
  551. warning (_("cannot write register"));
  552. return;
  553. }
  554. /* On some targets (esp. simulators), we can always write
  555. the register. */
  556. else if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
  557. {
  558. warning (_("cannot write register"));
  559. return;
  560. }
  561. }
  562. /* We can always write mapped registers. */
  563. else if (type == xtRegisterTypeMapped || type == xtRegisterTypeTieState)
  564. {
  565. xtensa_register_write_masked (regcache, reg, buffer);
  566. return;
  567. }
  568. /* Assume that we can write the register. */
  569. regcache->raw_write (regnum, buffer);
  570. }
  571. else
  572. internal_error (__FILE__, __LINE__,
  573. _("invalid register number %d"), regnum);
  574. }
  575. static const reggroup *xtensa_ar_reggroup;
  576. static const reggroup *xtensa_user_reggroup;
  577. static const reggroup *xtensa_vectra_reggroup;
  578. static const reggroup *xtensa_cp[XTENSA_MAX_COPROCESSOR];
  579. static void
  580. xtensa_init_reggroups (void)
  581. {
  582. int i;
  583. xtensa_ar_reggroup = reggroup_new ("ar", USER_REGGROUP);
  584. xtensa_user_reggroup = reggroup_new ("user", USER_REGGROUP);
  585. xtensa_vectra_reggroup = reggroup_new ("vectra", USER_REGGROUP);
  586. for (i = 0; i < XTENSA_MAX_COPROCESSOR; i++)
  587. xtensa_cp[i] = reggroup_new (xstrprintf ("cp%d", i).release (),
  588. USER_REGGROUP);
  589. }
  590. static void
  591. xtensa_add_reggroups (struct gdbarch *gdbarch)
  592. {
  593. /* Xtensa-specific groups. */
  594. reggroup_add (gdbarch, xtensa_ar_reggroup);
  595. reggroup_add (gdbarch, xtensa_user_reggroup);
  596. reggroup_add (gdbarch, xtensa_vectra_reggroup);
  597. for (int i = 0; i < XTENSA_MAX_COPROCESSOR; i++)
  598. reggroup_add (gdbarch, xtensa_cp[i]);
  599. }
  600. static int
  601. xtensa_coprocessor_register_group (const struct reggroup *group)
  602. {
  603. int i;
  604. for (i = 0; i < XTENSA_MAX_COPROCESSOR; i++)
  605. if (group == xtensa_cp[i])
  606. return i;
  607. return -1;
  608. }
  609. #define SAVE_REST_FLAGS (XTENSA_REGISTER_FLAGS_READABLE \
  610. | XTENSA_REGISTER_FLAGS_WRITABLE \
  611. | XTENSA_REGISTER_FLAGS_VOLATILE)
  612. #define SAVE_REST_VALID (XTENSA_REGISTER_FLAGS_READABLE \
  613. | XTENSA_REGISTER_FLAGS_WRITABLE)
  614. static int
  615. xtensa_register_reggroup_p (struct gdbarch *gdbarch,
  616. int regnum,
  617. const struct reggroup *group)
  618. {
  619. xtensa_gdbarch_tdep *tdep = (xtensa_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  620. xtensa_register_t* reg = &tdep->regmap[regnum];
  621. xtensa_register_type_t type = reg->type;
  622. xtensa_register_group_t rg = reg->group;
  623. int cp_number;
  624. if (group == save_reggroup)
  625. /* Every single register should be included into the list of registers
  626. to be watched for changes while using -data-list-changed-registers. */
  627. return 1;
  628. /* First, skip registers that are not visible to this target
  629. (unknown and unmapped registers when not using ISS). */
  630. if (type == xtRegisterTypeUnmapped || type == xtRegisterTypeUnknown)
  631. return 0;
  632. if (group == all_reggroup)
  633. return 1;
  634. if (group == xtensa_ar_reggroup)
  635. return rg & xtRegisterGroupAddrReg;
  636. if (group == xtensa_user_reggroup)
  637. return rg & xtRegisterGroupUser;
  638. if (group == float_reggroup)
  639. return rg & xtRegisterGroupFloat;
  640. if (group == general_reggroup)
  641. return rg & xtRegisterGroupGeneral;
  642. if (group == system_reggroup)
  643. return rg & xtRegisterGroupState;
  644. if (group == vector_reggroup || group == xtensa_vectra_reggroup)
  645. return rg & xtRegisterGroupVectra;
  646. if (group == restore_reggroup)
  647. return (regnum < gdbarch_num_regs (gdbarch)
  648. && (reg->flags & SAVE_REST_FLAGS) == SAVE_REST_VALID);
  649. cp_number = xtensa_coprocessor_register_group (group);
  650. if (cp_number >= 0)
  651. return rg & (xtRegisterGroupCP0 << cp_number);
  652. else
  653. return 1;
  654. }
  655. /* Supply register REGNUM from the buffer specified by GREGS and LEN
  656. in the general-purpose register set REGSET to register cache
  657. REGCACHE. If REGNUM is -1 do this for all registers in REGSET. */
  658. static void
  659. xtensa_supply_gregset (const struct regset *regset,
  660. struct regcache *rc,
  661. int regnum,
  662. const void *gregs,
  663. size_t len)
  664. {
  665. const xtensa_elf_gregset_t *regs = (const xtensa_elf_gregset_t *) gregs;
  666. struct gdbarch *gdbarch = rc->arch ();
  667. xtensa_gdbarch_tdep *tdep = (xtensa_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  668. int i;
  669. DEBUGTRACE ("xtensa_supply_gregset (..., regnum==%d, ...)\n", regnum);
  670. if (regnum == gdbarch_pc_regnum (gdbarch) || regnum == -1)
  671. rc->raw_supply (gdbarch_pc_regnum (gdbarch), (char *) &regs->pc);
  672. if (regnum == gdbarch_ps_regnum (gdbarch) || regnum == -1)
  673. rc->raw_supply (gdbarch_ps_regnum (gdbarch), (char *) &regs->ps);
  674. if (regnum == tdep->wb_regnum || regnum == -1)
  675. rc->raw_supply (tdep->wb_regnum,
  676. (char *) &regs->windowbase);
  677. if (regnum == tdep->ws_regnum || regnum == -1)
  678. rc->raw_supply (tdep->ws_regnum,
  679. (char *) &regs->windowstart);
  680. if (regnum == tdep->lbeg_regnum || regnum == -1)
  681. rc->raw_supply (tdep->lbeg_regnum,
  682. (char *) &regs->lbeg);
  683. if (regnum == tdep->lend_regnum || regnum == -1)
  684. rc->raw_supply (tdep->lend_regnum,
  685. (char *) &regs->lend);
  686. if (regnum == tdep->lcount_regnum || regnum == -1)
  687. rc->raw_supply (tdep->lcount_regnum,
  688. (char *) &regs->lcount);
  689. if (regnum == tdep->sar_regnum || regnum == -1)
  690. rc->raw_supply (tdep->sar_regnum,
  691. (char *) &regs->sar);
  692. if (regnum >=tdep->ar_base
  693. && regnum < tdep->ar_base
  694. + tdep->num_aregs)
  695. rc->raw_supply
  696. (regnum, (char *) &regs->ar[regnum - tdep->ar_base]);
  697. else if (regnum == -1)
  698. {
  699. for (i = 0; i < tdep->num_aregs; ++i)
  700. rc->raw_supply (tdep->ar_base + i,
  701. (char *) &regs->ar[i]);
  702. }
  703. }
  704. /* Xtensa register set. */
  705. static struct regset
  706. xtensa_gregset =
  707. {
  708. NULL,
  709. xtensa_supply_gregset
  710. };
  711. /* Iterate over supported core file register note sections. */
  712. static void
  713. xtensa_iterate_over_regset_sections (struct gdbarch *gdbarch,
  714. iterate_over_regset_sections_cb *cb,
  715. void *cb_data,
  716. const struct regcache *regcache)
  717. {
  718. DEBUGTRACE ("xtensa_iterate_over_regset_sections\n");
  719. cb (".reg", sizeof (xtensa_elf_gregset_t), sizeof (xtensa_elf_gregset_t),
  720. &xtensa_gregset, NULL, cb_data);
  721. }
  722. /* Handling frames. */
  723. /* Number of registers to save in case of Windowed ABI. */
  724. #define XTENSA_NUM_SAVED_AREGS 12
  725. /* Frame cache part for Windowed ABI. */
  726. typedef struct xtensa_windowed_frame_cache
  727. {
  728. int wb; /* WINDOWBASE of the previous frame. */
  729. int callsize; /* Call size of this frame. */
  730. int ws; /* WINDOWSTART of the previous frame. It keeps track of
  731. life windows only. If there is no bit set for the
  732. window, that means it had been already spilled
  733. because of window overflow. */
  734. /* Addresses of spilled A-registers.
  735. AREGS[i] == -1, if corresponding AR is alive. */
  736. CORE_ADDR aregs[XTENSA_NUM_SAVED_AREGS];
  737. } xtensa_windowed_frame_cache_t;
  738. /* Call0 ABI Definitions. */
  739. #define C0_MAXOPDS 3 /* Maximum number of operands for prologue
  740. analysis. */
  741. #define C0_CLESV 12 /* Callee-saved registers are here and up. */
  742. #define C0_SP 1 /* Register used as SP. */
  743. #define C0_FP 15 /* Register used as FP. */
  744. #define C0_RA 0 /* Register used as return address. */
  745. #define C0_ARGS 2 /* Register used as first arg/retval. */
  746. #define C0_NARGS 6 /* Number of A-regs for args/retvals. */
  747. /* Each element of xtensa_call0_frame_cache.c0_rt[] describes for each
  748. A-register where the current content of the reg came from (in terms
  749. of an original reg and a constant). Negative values of c0_rt[n].fp_reg
  750. mean that the original content of the register was saved to the stack.
  751. c0_rt[n].fr.ofs is NOT the offset from the frame base because we don't
  752. know where SP will end up until the entire prologue has been analyzed. */
  753. #define C0_CONST -1 /* fr_reg value if register contains a constant. */
  754. #define C0_INEXP -2 /* fr_reg value if inexpressible as reg + offset. */
  755. #define C0_NOSTK -1 /* to_stk value if register has not been stored. */
  756. extern xtensa_isa xtensa_default_isa;
  757. typedef struct xtensa_c0reg
  758. {
  759. int fr_reg; /* original register from which register content
  760. is derived, or C0_CONST, or C0_INEXP. */
  761. int fr_ofs; /* constant offset from reg, or immediate value. */
  762. int to_stk; /* offset from original SP to register (4-byte aligned),
  763. or C0_NOSTK if register has not been saved. */
  764. } xtensa_c0reg_t;
  765. /* Frame cache part for Call0 ABI. */
  766. typedef struct xtensa_call0_frame_cache
  767. {
  768. int c0_frmsz; /* Stack frame size. */
  769. int c0_hasfp; /* Current frame uses frame pointer. */
  770. int fp_regnum; /* A-register used as FP. */
  771. int c0_fp; /* Actual value of frame pointer. */
  772. int c0_fpalign; /* Dynamic adjustment for the stack
  773. pointer. It's an AND mask. Zero,
  774. if alignment was not adjusted. */
  775. int c0_old_sp; /* In case of dynamic adjustment, it is
  776. a register holding unaligned sp.
  777. C0_INEXP, when undefined. */
  778. int c0_sp_ofs; /* If "c0_old_sp" was spilled it's a
  779. stack offset. C0_NOSTK otherwise. */
  780. xtensa_c0reg_t c0_rt[C0_NREGS]; /* Register tracking information. */
  781. } xtensa_call0_frame_cache_t;
  782. typedef struct xtensa_frame_cache
  783. {
  784. CORE_ADDR base; /* Stack pointer of this frame. */
  785. CORE_ADDR pc; /* PC of this frame at the function entry point. */
  786. CORE_ADDR ra; /* The raw return address of this frame. */
  787. CORE_ADDR ps; /* The PS register of the previous (older) frame. */
  788. CORE_ADDR prev_sp; /* Stack Pointer of the previous (older) frame. */
  789. int call0; /* It's a call0 framework (else windowed). */
  790. union
  791. {
  792. xtensa_windowed_frame_cache_t wd; /* call0 == false. */
  793. xtensa_call0_frame_cache_t c0; /* call0 == true. */
  794. };
  795. } xtensa_frame_cache_t;
  796. static struct xtensa_frame_cache *
  797. xtensa_alloc_frame_cache (int windowed)
  798. {
  799. xtensa_frame_cache_t *cache;
  800. int i;
  801. DEBUGTRACE ("xtensa_alloc_frame_cache ()\n");
  802. cache = FRAME_OBSTACK_ZALLOC (xtensa_frame_cache_t);
  803. cache->base = 0;
  804. cache->pc = 0;
  805. cache->ra = 0;
  806. cache->ps = 0;
  807. cache->prev_sp = 0;
  808. cache->call0 = !windowed;
  809. if (cache->call0)
  810. {
  811. cache->c0.c0_frmsz = -1;
  812. cache->c0.c0_hasfp = 0;
  813. cache->c0.fp_regnum = -1;
  814. cache->c0.c0_fp = -1;
  815. cache->c0.c0_fpalign = 0;
  816. cache->c0.c0_old_sp = C0_INEXP;
  817. cache->c0.c0_sp_ofs = C0_NOSTK;
  818. for (i = 0; i < C0_NREGS; i++)
  819. {
  820. cache->c0.c0_rt[i].fr_reg = i;
  821. cache->c0.c0_rt[i].fr_ofs = 0;
  822. cache->c0.c0_rt[i].to_stk = C0_NOSTK;
  823. }
  824. }
  825. else
  826. {
  827. cache->wd.wb = 0;
  828. cache->wd.ws = 0;
  829. cache->wd.callsize = -1;
  830. for (i = 0; i < XTENSA_NUM_SAVED_AREGS; i++)
  831. cache->wd.aregs[i] = -1;
  832. }
  833. return cache;
  834. }
  835. static CORE_ADDR
  836. xtensa_frame_align (struct gdbarch *gdbarch, CORE_ADDR address)
  837. {
  838. return address & ~15;
  839. }
  840. static CORE_ADDR
  841. xtensa_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
  842. {
  843. gdb_byte buf[8];
  844. CORE_ADDR pc;
  845. DEBUGTRACE ("xtensa_unwind_pc (next_frame = %s)\n",
  846. host_address_to_string (next_frame));
  847. frame_unwind_register (next_frame, gdbarch_pc_regnum (gdbarch), buf);
  848. pc = extract_typed_address (buf, builtin_type (gdbarch)->builtin_func_ptr);
  849. DEBUGINFO ("[xtensa_unwind_pc] pc = 0x%08x\n", (unsigned int) pc);
  850. return pc;
  851. }
  852. static struct frame_id
  853. xtensa_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  854. {
  855. CORE_ADDR pc, fp;
  856. xtensa_gdbarch_tdep *tdep = (xtensa_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  857. /* THIS-FRAME is a dummy frame. Return a frame ID of that frame. */
  858. pc = get_frame_pc (this_frame);
  859. fp = get_frame_register_unsigned
  860. (this_frame, tdep->a0_base + 1);
  861. /* Make dummy frame ID unique by adding a constant. */
  862. return frame_id_build (fp + SP_ALIGNMENT, pc);
  863. }
  864. /* Returns true, if instruction to execute next is unique to Xtensa Window
  865. Interrupt Handlers. It can only be one of L32E, S32E, RFWO, or RFWU. */
  866. static int
  867. xtensa_window_interrupt_insn (struct gdbarch *gdbarch, CORE_ADDR pc)
  868. {
  869. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  870. unsigned int insn = read_memory_integer (pc, 4, byte_order);
  871. unsigned int code;
  872. if (byte_order == BFD_ENDIAN_BIG)
  873. {
  874. /* Check, if this is L32E or S32E. */
  875. code = insn & 0xf000ff00;
  876. if ((code == 0x00009000) || (code == 0x00009400))
  877. return 1;
  878. /* Check, if this is RFWU or RFWO. */
  879. code = insn & 0xffffff00;
  880. return ((code == 0x00430000) || (code == 0x00530000));
  881. }
  882. else
  883. {
  884. /* Check, if this is L32E or S32E. */
  885. code = insn & 0x00ff000f;
  886. if ((code == 0x090000) || (code == 0x490000))
  887. return 1;
  888. /* Check, if this is RFWU or RFWO. */
  889. code = insn & 0x00ffffff;
  890. return ((code == 0x00003400) || (code == 0x00003500));
  891. }
  892. }
  893. /* Returns the best guess about which register is a frame pointer
  894. for the function containing CURRENT_PC. */
  895. #define XTENSA_ISA_BSZ 32 /* Instruction buffer size. */
  896. #define XTENSA_ISA_BADPC ((CORE_ADDR)0) /* Bad PC value. */
  897. static unsigned int
  898. xtensa_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR current_pc)
  899. {
  900. #define RETURN_FP goto done
  901. xtensa_gdbarch_tdep *tdep = (xtensa_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  902. unsigned int fp_regnum = tdep->a0_base + 1;
  903. CORE_ADDR start_addr;
  904. xtensa_isa isa;
  905. xtensa_insnbuf ins, slot;
  906. gdb_byte ibuf[XTENSA_ISA_BSZ];
  907. CORE_ADDR ia, bt, ba;
  908. xtensa_format ifmt;
  909. int ilen, islots, is;
  910. xtensa_opcode opc;
  911. const char *opcname;
  912. find_pc_partial_function (current_pc, NULL, &start_addr, NULL);
  913. if (start_addr == 0)
  914. return fp_regnum;
  915. isa = xtensa_default_isa;
  916. gdb_assert (XTENSA_ISA_BSZ >= xtensa_isa_maxlength (isa));
  917. ins = xtensa_insnbuf_alloc (isa);
  918. slot = xtensa_insnbuf_alloc (isa);
  919. ba = 0;
  920. for (ia = start_addr, bt = ia; ia < current_pc ; ia += ilen)
  921. {
  922. if (ia + xtensa_isa_maxlength (isa) > bt)
  923. {
  924. ba = ia;
  925. bt = (ba + XTENSA_ISA_BSZ) < current_pc
  926. ? ba + XTENSA_ISA_BSZ : current_pc;
  927. if (target_read_memory (ba, ibuf, bt - ba) != 0)
  928. RETURN_FP;
  929. }
  930. xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
  931. ifmt = xtensa_format_decode (isa, ins);
  932. if (ifmt == XTENSA_UNDEFINED)
  933. RETURN_FP;
  934. ilen = xtensa_format_length (isa, ifmt);
  935. if (ilen == XTENSA_UNDEFINED)
  936. RETURN_FP;
  937. islots = xtensa_format_num_slots (isa, ifmt);
  938. if (islots == XTENSA_UNDEFINED)
  939. RETURN_FP;
  940. for (is = 0; is < islots; ++is)
  941. {
  942. if (xtensa_format_get_slot (isa, ifmt, is, ins, slot))
  943. RETURN_FP;
  944. opc = xtensa_opcode_decode (isa, ifmt, is, slot);
  945. if (opc == XTENSA_UNDEFINED)
  946. RETURN_FP;
  947. opcname = xtensa_opcode_name (isa, opc);
  948. if (strcasecmp (opcname, "mov.n") == 0
  949. || strcasecmp (opcname, "or") == 0)
  950. {
  951. unsigned int register_operand;
  952. /* Possible candidate for setting frame pointer
  953. from A1. This is what we are looking for. */
  954. if (xtensa_operand_get_field (isa, opc, 1, ifmt,
  955. is, slot, &register_operand) != 0)
  956. RETURN_FP;
  957. if (xtensa_operand_decode (isa, opc, 1, &register_operand) != 0)
  958. RETURN_FP;
  959. if (register_operand == 1) /* Mov{.n} FP A1. */
  960. {
  961. if (xtensa_operand_get_field (isa, opc, 0, ifmt, is, slot,
  962. &register_operand) != 0)
  963. RETURN_FP;
  964. if (xtensa_operand_decode (isa, opc, 0,
  965. &register_operand) != 0)
  966. RETURN_FP;
  967. fp_regnum
  968. = tdep->a0_base + register_operand;
  969. RETURN_FP;
  970. }
  971. }
  972. if (
  973. /* We have problems decoding the memory. */
  974. opcname == NULL
  975. || strcasecmp (opcname, "ill") == 0
  976. || strcasecmp (opcname, "ill.n") == 0
  977. /* Hit planted breakpoint. */
  978. || strcasecmp (opcname, "break") == 0
  979. || strcasecmp (opcname, "break.n") == 0
  980. /* Flow control instructions finish prologue. */
  981. || xtensa_opcode_is_branch (isa, opc) > 0
  982. || xtensa_opcode_is_jump (isa, opc) > 0
  983. || xtensa_opcode_is_loop (isa, opc) > 0
  984. || xtensa_opcode_is_call (isa, opc) > 0
  985. || strcasecmp (opcname, "simcall") == 0
  986. || strcasecmp (opcname, "syscall") == 0)
  987. /* Can not continue analysis. */
  988. RETURN_FP;
  989. }
  990. }
  991. done:
  992. xtensa_insnbuf_free(isa, slot);
  993. xtensa_insnbuf_free(isa, ins);
  994. return fp_regnum;
  995. }
  996. /* The key values to identify the frame using "cache" are
  997. cache->base = SP (or best guess about FP) of this frame;
  998. cache->pc = entry-PC (entry point of the frame function);
  999. cache->prev_sp = SP of the previous frame. */
  1000. static void
  1001. call0_frame_cache (struct frame_info *this_frame,
  1002. xtensa_frame_cache_t *cache, CORE_ADDR pc);
  1003. static void
  1004. xtensa_window_interrupt_frame_cache (struct frame_info *this_frame,
  1005. xtensa_frame_cache_t *cache,
  1006. CORE_ADDR pc);
  1007. static struct xtensa_frame_cache *
  1008. xtensa_frame_cache (struct frame_info *this_frame, void **this_cache)
  1009. {
  1010. xtensa_frame_cache_t *cache;
  1011. CORE_ADDR ra, wb, ws, pc, sp, ps;
  1012. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1013. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1014. unsigned int fp_regnum;
  1015. int windowed, ps_regnum;
  1016. if (*this_cache)
  1017. return (struct xtensa_frame_cache *) *this_cache;
  1018. pc = get_frame_register_unsigned (this_frame, gdbarch_pc_regnum (gdbarch));
  1019. ps_regnum = gdbarch_ps_regnum (gdbarch);
  1020. ps = (ps_regnum >= 0
  1021. ? get_frame_register_unsigned (this_frame, ps_regnum) : TX_PS);
  1022. windowed = windowing_enabled (gdbarch, ps);
  1023. /* Get pristine xtensa-frame. */
  1024. cache = xtensa_alloc_frame_cache (windowed);
  1025. *this_cache = cache;
  1026. if (windowed)
  1027. {
  1028. LONGEST op1;
  1029. xtensa_gdbarch_tdep *tdep = (xtensa_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  1030. /* Get WINDOWBASE, WINDOWSTART, and PS registers. */
  1031. wb = get_frame_register_unsigned (this_frame,
  1032. tdep->wb_regnum);
  1033. ws = get_frame_register_unsigned (this_frame,
  1034. tdep->ws_regnum);
  1035. if (safe_read_memory_integer (pc, 1, byte_order, &op1)
  1036. && XTENSA_IS_ENTRY (gdbarch, op1))
  1037. {
  1038. int callinc = CALLINC (ps);
  1039. ra = get_frame_register_unsigned
  1040. (this_frame, tdep->a0_base + callinc * 4);
  1041. /* ENTRY hasn't been executed yet, therefore callsize is still 0. */
  1042. cache->wd.callsize = 0;
  1043. cache->wd.wb = wb;
  1044. cache->wd.ws = ws;
  1045. cache->prev_sp = get_frame_register_unsigned
  1046. (this_frame, tdep->a0_base + 1);
  1047. /* This only can be the outermost frame since we are
  1048. just about to execute ENTRY. SP hasn't been set yet.
  1049. We can assume any frame size, because it does not
  1050. matter, and, let's fake frame base in cache. */
  1051. cache->base = cache->prev_sp - 16;
  1052. cache->pc = pc;
  1053. cache->ra = (cache->pc & 0xc0000000) | (ra & 0x3fffffff);
  1054. cache->ps = (ps & ~PS_CALLINC_MASK)
  1055. | ((WINSIZE(ra)/4) << PS_CALLINC_SHIFT);
  1056. return cache;
  1057. }
  1058. else
  1059. {
  1060. fp_regnum = xtensa_scan_prologue (gdbarch, pc);
  1061. ra = get_frame_register_unsigned (this_frame,
  1062. tdep->a0_base);
  1063. cache->wd.callsize = WINSIZE (ra);
  1064. cache->wd.wb = (wb - cache->wd.callsize / 4)
  1065. & (tdep->num_aregs / 4 - 1);
  1066. cache->wd.ws = ws & ~(1 << wb);
  1067. cache->pc = get_frame_func (this_frame);
  1068. cache->ra = (pc & 0xc0000000) | (ra & 0x3fffffff);
  1069. cache->ps = (ps & ~PS_CALLINC_MASK)
  1070. | ((WINSIZE(ra)/4) << PS_CALLINC_SHIFT);
  1071. }
  1072. if (cache->wd.ws == 0)
  1073. {
  1074. int i;
  1075. /* Set A0...A3. */
  1076. sp = get_frame_register_unsigned
  1077. (this_frame, tdep->a0_base + 1) - 16;
  1078. for (i = 0; i < 4; i++, sp += 4)
  1079. {
  1080. cache->wd.aregs[i] = sp;
  1081. }
  1082. if (cache->wd.callsize > 4)
  1083. {
  1084. /* Set A4...A7/A11. */
  1085. /* Get the SP of the frame previous to the previous one.
  1086. To achieve this, we have to dereference SP twice. */
  1087. sp = (CORE_ADDR) read_memory_integer (sp - 12, 4, byte_order);
  1088. sp = (CORE_ADDR) read_memory_integer (sp - 12, 4, byte_order);
  1089. sp -= cache->wd.callsize * 4;
  1090. for ( i = 4; i < cache->wd.callsize; i++, sp += 4)
  1091. {
  1092. cache->wd.aregs[i] = sp;
  1093. }
  1094. }
  1095. }
  1096. if ((cache->prev_sp == 0) && ( ra != 0 ))
  1097. /* If RA is equal to 0 this frame is an outermost frame. Leave
  1098. cache->prev_sp unchanged marking the boundary of the frame stack. */
  1099. {
  1100. if ((cache->wd.ws & (1 << cache->wd.wb)) == 0)
  1101. {
  1102. /* Register window overflow already happened.
  1103. We can read caller's SP from the proper spill location. */
  1104. sp = get_frame_register_unsigned
  1105. (this_frame, tdep->a0_base + 1);
  1106. cache->prev_sp = read_memory_integer (sp - 12, 4, byte_order);
  1107. }
  1108. else
  1109. {
  1110. /* Read caller's frame SP directly from the previous window. */
  1111. int regnum = arreg_number
  1112. (gdbarch, tdep->a0_base + 1,
  1113. cache->wd.wb);
  1114. cache->prev_sp = xtensa_read_register (regnum);
  1115. }
  1116. }
  1117. }
  1118. else if (xtensa_window_interrupt_insn (gdbarch, pc))
  1119. {
  1120. /* Execution stopped inside Xtensa Window Interrupt Handler. */
  1121. xtensa_window_interrupt_frame_cache (this_frame, cache, pc);
  1122. /* Everything was set already, including cache->base. */
  1123. return cache;
  1124. }
  1125. else /* Call0 framework. */
  1126. {
  1127. call0_frame_cache (this_frame, cache, pc);
  1128. fp_regnum = cache->c0.fp_regnum;
  1129. }
  1130. cache->base = get_frame_register_unsigned (this_frame, fp_regnum);
  1131. return cache;
  1132. }
  1133. static int xtensa_session_once_reported = 1;
  1134. /* Report a problem with prologue analysis while doing backtracing.
  1135. But, do it only once to avoid annoying repeated messages. */
  1136. static void
  1137. warning_once (void)
  1138. {
  1139. if (xtensa_session_once_reported == 0)
  1140. warning (_("\
  1141. \nUnrecognised function prologue. Stack trace cannot be resolved. \
  1142. This message will not be repeated in this session.\n"));
  1143. xtensa_session_once_reported = 1;
  1144. }
  1145. static void
  1146. xtensa_frame_this_id (struct frame_info *this_frame,
  1147. void **this_cache,
  1148. struct frame_id *this_id)
  1149. {
  1150. struct xtensa_frame_cache *cache =
  1151. xtensa_frame_cache (this_frame, this_cache);
  1152. if (cache->prev_sp == 0)
  1153. return;
  1154. (*this_id) = frame_id_build (cache->prev_sp, cache->pc);
  1155. }
  1156. static struct value *
  1157. xtensa_frame_prev_register (struct frame_info *this_frame,
  1158. void **this_cache,
  1159. int regnum)
  1160. {
  1161. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1162. struct xtensa_frame_cache *cache;
  1163. ULONGEST saved_reg = 0;
  1164. int done = 1;
  1165. xtensa_gdbarch_tdep *tdep = (xtensa_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  1166. if (*this_cache == NULL)
  1167. *this_cache = xtensa_frame_cache (this_frame, this_cache);
  1168. cache = (struct xtensa_frame_cache *) *this_cache;
  1169. if (regnum ==gdbarch_pc_regnum (gdbarch))
  1170. saved_reg = cache->ra;
  1171. else if (regnum == tdep->a0_base + 1)
  1172. saved_reg = cache->prev_sp;
  1173. else if (!cache->call0)
  1174. {
  1175. if (regnum == tdep->ws_regnum)
  1176. saved_reg = cache->wd.ws;
  1177. else if (regnum == tdep->wb_regnum)
  1178. saved_reg = cache->wd.wb;
  1179. else if (regnum == gdbarch_ps_regnum (gdbarch))
  1180. saved_reg = cache->ps;
  1181. else
  1182. done = 0;
  1183. }
  1184. else
  1185. done = 0;
  1186. if (done)
  1187. return frame_unwind_got_constant (this_frame, regnum, saved_reg);
  1188. if (!cache->call0) /* Windowed ABI. */
  1189. {
  1190. /* Convert A-register numbers to AR-register numbers,
  1191. if we deal with A-register. */
  1192. if (regnum >= tdep->a0_base
  1193. && regnum <= tdep->a0_base + 15)
  1194. regnum = arreg_number (gdbarch, regnum, cache->wd.wb);
  1195. /* Check, if we deal with AR-register saved on stack. */
  1196. if (regnum >= tdep->ar_base
  1197. && regnum <= (tdep->ar_base
  1198. + tdep->num_aregs))
  1199. {
  1200. int areg = areg_number (gdbarch, regnum, cache->wd.wb);
  1201. if (areg >= 0
  1202. && areg < XTENSA_NUM_SAVED_AREGS
  1203. && cache->wd.aregs[areg] != -1)
  1204. return frame_unwind_got_memory (this_frame, regnum,
  1205. cache->wd.aregs[areg]);
  1206. }
  1207. }
  1208. else /* Call0 ABI. */
  1209. {
  1210. int reg = (regnum >= tdep->ar_base
  1211. && regnum <= (tdep->ar_base
  1212. + C0_NREGS))
  1213. ? regnum - tdep->ar_base : regnum;
  1214. if (reg < C0_NREGS)
  1215. {
  1216. CORE_ADDR spe;
  1217. int stkofs;
  1218. /* If register was saved in the prologue, retrieve it. */
  1219. stkofs = cache->c0.c0_rt[reg].to_stk;
  1220. if (stkofs != C0_NOSTK)
  1221. {
  1222. /* Determine SP on entry based on FP. */
  1223. spe = cache->c0.c0_fp
  1224. - cache->c0.c0_rt[cache->c0.fp_regnum].fr_ofs;
  1225. return frame_unwind_got_memory (this_frame, regnum,
  1226. spe + stkofs);
  1227. }
  1228. }
  1229. }
  1230. /* All other registers have been either saved to
  1231. the stack or are still alive in the processor. */
  1232. return frame_unwind_got_register (this_frame, regnum, regnum);
  1233. }
  1234. static const struct frame_unwind
  1235. xtensa_unwind =
  1236. {
  1237. "xtensa prologue",
  1238. NORMAL_FRAME,
  1239. default_frame_unwind_stop_reason,
  1240. xtensa_frame_this_id,
  1241. xtensa_frame_prev_register,
  1242. NULL,
  1243. default_frame_sniffer
  1244. };
  1245. static CORE_ADDR
  1246. xtensa_frame_base_address (struct frame_info *this_frame, void **this_cache)
  1247. {
  1248. struct xtensa_frame_cache *cache =
  1249. xtensa_frame_cache (this_frame, this_cache);
  1250. return cache->base;
  1251. }
  1252. static const struct frame_base
  1253. xtensa_frame_base =
  1254. {
  1255. &xtensa_unwind,
  1256. xtensa_frame_base_address,
  1257. xtensa_frame_base_address,
  1258. xtensa_frame_base_address
  1259. };
  1260. static void
  1261. xtensa_extract_return_value (struct type *type,
  1262. struct regcache *regcache,
  1263. void *dst)
  1264. {
  1265. struct gdbarch *gdbarch = regcache->arch ();
  1266. bfd_byte *valbuf = (bfd_byte *) dst;
  1267. int len = TYPE_LENGTH (type);
  1268. ULONGEST pc, wb;
  1269. int callsize, areg;
  1270. int offset = 0;
  1271. DEBUGTRACE ("xtensa_extract_return_value (...)\n");
  1272. gdb_assert(len > 0);
  1273. xtensa_gdbarch_tdep *tdep = (xtensa_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  1274. if (tdep->call_abi != CallAbiCall0Only)
  1275. {
  1276. /* First, we have to find the caller window in the register file. */
  1277. regcache_raw_read_unsigned (regcache, gdbarch_pc_regnum (gdbarch), &pc);
  1278. callsize = extract_call_winsize (gdbarch, pc);
  1279. /* On Xtensa, we can return up to 4 words (or 2 for call12). */
  1280. if (len > (callsize > 8 ? 8 : 16))
  1281. internal_error (__FILE__, __LINE__,
  1282. _("cannot extract return value of %d bytes long"),
  1283. len);
  1284. /* Get the register offset of the return
  1285. register (A2) in the caller window. */
  1286. regcache_raw_read_unsigned
  1287. (regcache, tdep->wb_regnum, &wb);
  1288. areg = arreg_number (gdbarch,
  1289. tdep->a0_base + 2 + callsize, wb);
  1290. }
  1291. else
  1292. {
  1293. /* No windowing hardware - Call0 ABI. */
  1294. areg = tdep->a0_base + C0_ARGS;
  1295. }
  1296. DEBUGINFO ("[xtensa_extract_return_value] areg %d len %d\n", areg, len);
  1297. if (len < 4 && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  1298. offset = 4 - len;
  1299. for (; len > 0; len -= 4, areg++, valbuf += 4)
  1300. {
  1301. if (len < 4)
  1302. regcache->raw_read_part (areg, offset, len, valbuf);
  1303. else
  1304. regcache->raw_read (areg, valbuf);
  1305. }
  1306. }
  1307. static void
  1308. xtensa_store_return_value (struct type *type,
  1309. struct regcache *regcache,
  1310. const void *dst)
  1311. {
  1312. struct gdbarch *gdbarch = regcache->arch ();
  1313. const bfd_byte *valbuf = (const bfd_byte *) dst;
  1314. unsigned int areg;
  1315. ULONGEST pc, wb;
  1316. int callsize;
  1317. int len = TYPE_LENGTH (type);
  1318. int offset = 0;
  1319. DEBUGTRACE ("xtensa_store_return_value (...)\n");
  1320. xtensa_gdbarch_tdep *tdep = (xtensa_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  1321. if (tdep->call_abi != CallAbiCall0Only)
  1322. {
  1323. regcache_raw_read_unsigned
  1324. (regcache, tdep->wb_regnum, &wb);
  1325. regcache_raw_read_unsigned (regcache, gdbarch_pc_regnum (gdbarch), &pc);
  1326. callsize = extract_call_winsize (gdbarch, pc);
  1327. if (len > (callsize > 8 ? 8 : 16))
  1328. internal_error (__FILE__, __LINE__,
  1329. _("unimplemented for this length: %s"),
  1330. pulongest (TYPE_LENGTH (type)));
  1331. areg = arreg_number (gdbarch,
  1332. tdep->a0_base + 2 + callsize, wb);
  1333. DEBUGTRACE ("[xtensa_store_return_value] callsize %d wb %d\n",
  1334. callsize, (int) wb);
  1335. }
  1336. else
  1337. {
  1338. areg = tdep->a0_base + C0_ARGS;
  1339. }
  1340. if (len < 4 && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  1341. offset = 4 - len;
  1342. for (; len > 0; len -= 4, areg++, valbuf += 4)
  1343. {
  1344. if (len < 4)
  1345. regcache->raw_write_part (areg, offset, len, valbuf);
  1346. else
  1347. regcache->raw_write (areg, valbuf);
  1348. }
  1349. }
  1350. static enum return_value_convention
  1351. xtensa_return_value (struct gdbarch *gdbarch,
  1352. struct value *function,
  1353. struct type *valtype,
  1354. struct regcache *regcache,
  1355. gdb_byte *readbuf,
  1356. const gdb_byte *writebuf)
  1357. {
  1358. /* Structures up to 16 bytes are returned in registers. */
  1359. int struct_return = ((valtype->code () == TYPE_CODE_STRUCT
  1360. || valtype->code () == TYPE_CODE_UNION
  1361. || valtype->code () == TYPE_CODE_ARRAY)
  1362. && TYPE_LENGTH (valtype) > 16);
  1363. if (struct_return)
  1364. return RETURN_VALUE_STRUCT_CONVENTION;
  1365. DEBUGTRACE ("xtensa_return_value(...)\n");
  1366. if (writebuf != NULL)
  1367. {
  1368. xtensa_store_return_value (valtype, regcache, writebuf);
  1369. }
  1370. if (readbuf != NULL)
  1371. {
  1372. gdb_assert (!struct_return);
  1373. xtensa_extract_return_value (valtype, regcache, readbuf);
  1374. }
  1375. return RETURN_VALUE_REGISTER_CONVENTION;
  1376. }
  1377. /* DUMMY FRAME */
  1378. static CORE_ADDR
  1379. xtensa_push_dummy_call (struct gdbarch *gdbarch,
  1380. struct value *function,
  1381. struct regcache *regcache,
  1382. CORE_ADDR bp_addr,
  1383. int nargs,
  1384. struct value **args,
  1385. CORE_ADDR sp,
  1386. function_call_return_method return_method,
  1387. CORE_ADDR struct_addr)
  1388. {
  1389. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1390. xtensa_gdbarch_tdep *tdep = (xtensa_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  1391. int size, onstack_size;
  1392. gdb_byte *buf = (gdb_byte *) alloca (16);
  1393. CORE_ADDR ra, ps;
  1394. struct argument_info
  1395. {
  1396. const bfd_byte *contents;
  1397. int length;
  1398. int onstack; /* onstack == 0 => in reg */
  1399. int align; /* alignment */
  1400. union
  1401. {
  1402. int offset; /* stack offset if on stack. */
  1403. int regno; /* regno if in register. */
  1404. } u;
  1405. };
  1406. struct argument_info *arg_info =
  1407. (struct argument_info *) alloca (nargs * sizeof (struct argument_info));
  1408. CORE_ADDR osp = sp;
  1409. DEBUGTRACE ("xtensa_push_dummy_call (...)\n");
  1410. if (xtensa_debug_level > 3)
  1411. {
  1412. DEBUGINFO ("[xtensa_push_dummy_call] nargs = %d\n", nargs);
  1413. DEBUGINFO ("[xtensa_push_dummy_call] sp=0x%x, return_method=%d, "
  1414. "struct_addr=0x%x\n",
  1415. (int) sp, (int) return_method, (int) struct_addr);
  1416. for (int i = 0; i < nargs; i++)
  1417. {
  1418. struct value *arg = args[i];
  1419. struct type *arg_type = check_typedef (value_type (arg));
  1420. gdb_printf (gdb_stdlog, "%2d: %s %3s ", i,
  1421. host_address_to_string (arg),
  1422. pulongest (TYPE_LENGTH (arg_type)));
  1423. switch (arg_type->code ())
  1424. {
  1425. case TYPE_CODE_INT:
  1426. gdb_printf (gdb_stdlog, "int");
  1427. break;
  1428. case TYPE_CODE_STRUCT:
  1429. gdb_printf (gdb_stdlog, "struct");
  1430. break;
  1431. default:
  1432. gdb_printf (gdb_stdlog, "%3d", arg_type->code ());
  1433. break;
  1434. }
  1435. gdb_printf (gdb_stdlog, " %s\n",
  1436. host_address_to_string (value_contents (arg).data ()));
  1437. }
  1438. }
  1439. /* First loop: collect information.
  1440. Cast into type_long. (This shouldn't happen often for C because
  1441. GDB already does this earlier.) It's possible that GDB could
  1442. do it all the time but it's harmless to leave this code here. */
  1443. size = 0;
  1444. onstack_size = 0;
  1445. if (return_method == return_method_struct)
  1446. size = REGISTER_SIZE;
  1447. for (int i = 0; i < nargs; i++)
  1448. {
  1449. struct argument_info *info = &arg_info[i];
  1450. struct value *arg = args[i];
  1451. struct type *arg_type = check_typedef (value_type (arg));
  1452. switch (arg_type->code ())
  1453. {
  1454. case TYPE_CODE_INT:
  1455. case TYPE_CODE_BOOL:
  1456. case TYPE_CODE_CHAR:
  1457. case TYPE_CODE_RANGE:
  1458. case TYPE_CODE_ENUM:
  1459. /* Cast argument to long if necessary as the mask does it too. */
  1460. if (TYPE_LENGTH (arg_type)
  1461. < TYPE_LENGTH (builtin_type (gdbarch)->builtin_long))
  1462. {
  1463. arg_type = builtin_type (gdbarch)->builtin_long;
  1464. arg = value_cast (arg_type, arg);
  1465. }
  1466. /* Aligment is equal to the type length for the basic types. */
  1467. info->align = TYPE_LENGTH (arg_type);
  1468. break;
  1469. case TYPE_CODE_FLT:
  1470. /* Align doubles correctly. */
  1471. if (TYPE_LENGTH (arg_type)
  1472. == TYPE_LENGTH (builtin_type (gdbarch)->builtin_double))
  1473. info->align = TYPE_LENGTH (builtin_type (gdbarch)->builtin_double);
  1474. else
  1475. info->align = TYPE_LENGTH (builtin_type (gdbarch)->builtin_long);
  1476. break;
  1477. case TYPE_CODE_STRUCT:
  1478. default:
  1479. info->align = TYPE_LENGTH (builtin_type (gdbarch)->builtin_long);
  1480. break;
  1481. }
  1482. info->length = TYPE_LENGTH (arg_type);
  1483. info->contents = value_contents (arg).data ();
  1484. /* Align size and onstack_size. */
  1485. size = (size + info->align - 1) & ~(info->align - 1);
  1486. onstack_size = (onstack_size + info->align - 1) & ~(info->align - 1);
  1487. if (size + info->length > REGISTER_SIZE * ARG_NOF (tdep))
  1488. {
  1489. info->onstack = 1;
  1490. info->u.offset = onstack_size;
  1491. onstack_size += info->length;
  1492. }
  1493. else
  1494. {
  1495. info->onstack = 0;
  1496. info->u.regno = ARG_1ST (tdep) + size / REGISTER_SIZE;
  1497. }
  1498. size += info->length;
  1499. }
  1500. /* Adjust the stack pointer and align it. */
  1501. sp = align_down (sp - onstack_size, SP_ALIGNMENT);
  1502. /* Simulate MOVSP, if Windowed ABI. */
  1503. if ((tdep->call_abi != CallAbiCall0Only)
  1504. && (sp != osp))
  1505. {
  1506. read_memory (osp - 16, buf, 16);
  1507. write_memory (sp - 16, buf, 16);
  1508. }
  1509. /* Second Loop: Load arguments. */
  1510. if (return_method == return_method_struct)
  1511. {
  1512. store_unsigned_integer (buf, REGISTER_SIZE, byte_order, struct_addr);
  1513. regcache->cooked_write (ARG_1ST (tdep), buf);
  1514. }
  1515. for (int i = 0; i < nargs; i++)
  1516. {
  1517. struct argument_info *info = &arg_info[i];
  1518. if (info->onstack)
  1519. {
  1520. int n = info->length;
  1521. CORE_ADDR offset = sp + info->u.offset;
  1522. /* Odd-sized structs are aligned to the lower side of a memory
  1523. word in big-endian mode and require a shift. This only
  1524. applies for structures smaller than one word. */
  1525. if (n < REGISTER_SIZE
  1526. && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  1527. offset += (REGISTER_SIZE - n);
  1528. write_memory (offset, info->contents, info->length);
  1529. }
  1530. else
  1531. {
  1532. int n = info->length;
  1533. const bfd_byte *cp = info->contents;
  1534. int r = info->u.regno;
  1535. /* Odd-sized structs are aligned to the lower side of registers in
  1536. big-endian mode and require a shift. The odd-sized leftover will
  1537. be at the end. Note that this is only true for structures smaller
  1538. than REGISTER_SIZE; for larger odd-sized structures the excess
  1539. will be left-aligned in the register on both endiannesses. */
  1540. if (n < REGISTER_SIZE && byte_order == BFD_ENDIAN_BIG)
  1541. {
  1542. ULONGEST v;
  1543. v = extract_unsigned_integer (cp, REGISTER_SIZE, byte_order);
  1544. v = v >> ((REGISTER_SIZE - n) * TARGET_CHAR_BIT);
  1545. store_unsigned_integer (buf, REGISTER_SIZE, byte_order, v);
  1546. regcache->cooked_write (r, buf);
  1547. cp += REGISTER_SIZE;
  1548. n -= REGISTER_SIZE;
  1549. r++;
  1550. }
  1551. else
  1552. while (n > 0)
  1553. {
  1554. regcache->cooked_write (r, cp);
  1555. cp += REGISTER_SIZE;
  1556. n -= REGISTER_SIZE;
  1557. r++;
  1558. }
  1559. }
  1560. }
  1561. /* Set the return address of dummy frame to the dummy address.
  1562. The return address for the current function (in A0) is
  1563. saved in the dummy frame, so we can safely overwrite A0 here. */
  1564. if (tdep->call_abi != CallAbiCall0Only)
  1565. {
  1566. ULONGEST val;
  1567. ra = (bp_addr & 0x3fffffff) | 0x40000000;
  1568. regcache_raw_read_unsigned (regcache, gdbarch_ps_regnum (gdbarch), &val);
  1569. ps = (unsigned long) val & ~0x00030000;
  1570. regcache_cooked_write_unsigned
  1571. (regcache, tdep->a0_base + 4, ra);
  1572. regcache_cooked_write_unsigned (regcache,
  1573. gdbarch_ps_regnum (gdbarch),
  1574. ps | 0x00010000);
  1575. /* All the registers have been saved. After executing
  1576. dummy call, they all will be restored. So it's safe
  1577. to modify WINDOWSTART register to make it look like there
  1578. is only one register window corresponding to WINDOWEBASE. */
  1579. regcache->raw_read (tdep->wb_regnum, buf);
  1580. regcache_cooked_write_unsigned
  1581. (regcache, tdep->ws_regnum,
  1582. 1 << extract_unsigned_integer (buf, 4, byte_order));
  1583. }
  1584. else
  1585. {
  1586. /* Simulate CALL0: write RA into A0 register. */
  1587. regcache_cooked_write_unsigned
  1588. (regcache, tdep->a0_base, bp_addr);
  1589. }
  1590. /* Set new stack pointer and return it. */
  1591. regcache_cooked_write_unsigned (regcache,
  1592. tdep->a0_base + 1, sp);
  1593. /* Make dummy frame ID unique by adding a constant. */
  1594. return sp + SP_ALIGNMENT;
  1595. }
  1596. /* Implement the breakpoint_kind_from_pc gdbarch method. */
  1597. static int
  1598. xtensa_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr)
  1599. {
  1600. xtensa_gdbarch_tdep *tdep = (xtensa_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  1601. if (tdep->isa_use_density_instructions)
  1602. return 2;
  1603. else
  1604. return 4;
  1605. }
  1606. /* Return a breakpoint for the current location of PC. We always use
  1607. the density version if we have density instructions (regardless of the
  1608. current instruction at PC), and use regular instructions otherwise. */
  1609. #define BIG_BREAKPOINT { 0x00, 0x04, 0x00 }
  1610. #define LITTLE_BREAKPOINT { 0x00, 0x40, 0x00 }
  1611. #define DENSITY_BIG_BREAKPOINT { 0xd2, 0x0f }
  1612. #define DENSITY_LITTLE_BREAKPOINT { 0x2d, 0xf0 }
  1613. /* Implement the sw_breakpoint_from_kind gdbarch method. */
  1614. static const gdb_byte *
  1615. xtensa_sw_breakpoint_from_kind (struct gdbarch *gdbarch, int kind, int *size)
  1616. {
  1617. *size = kind;
  1618. if (kind == 4)
  1619. {
  1620. static unsigned char big_breakpoint[] = BIG_BREAKPOINT;
  1621. static unsigned char little_breakpoint[] = LITTLE_BREAKPOINT;
  1622. if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  1623. return big_breakpoint;
  1624. else
  1625. return little_breakpoint;
  1626. }
  1627. else
  1628. {
  1629. static unsigned char density_big_breakpoint[] = DENSITY_BIG_BREAKPOINT;
  1630. static unsigned char density_little_breakpoint[]
  1631. = DENSITY_LITTLE_BREAKPOINT;
  1632. if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  1633. return density_big_breakpoint;
  1634. else
  1635. return density_little_breakpoint;
  1636. }
  1637. }
  1638. /* Call0 ABI support routines. */
  1639. /* Return true, if PC points to "ret" or "ret.n". */
  1640. static int
  1641. call0_ret (CORE_ADDR start_pc, CORE_ADDR finish_pc)
  1642. {
  1643. #define RETURN_RET goto done
  1644. xtensa_isa isa;
  1645. xtensa_insnbuf ins, slot;
  1646. gdb_byte ibuf[XTENSA_ISA_BSZ];
  1647. CORE_ADDR ia, bt, ba;
  1648. xtensa_format ifmt;
  1649. int ilen, islots, is;
  1650. xtensa_opcode opc;
  1651. const char *opcname;
  1652. int found_ret = 0;
  1653. isa = xtensa_default_isa;
  1654. gdb_assert (XTENSA_ISA_BSZ >= xtensa_isa_maxlength (isa));
  1655. ins = xtensa_insnbuf_alloc (isa);
  1656. slot = xtensa_insnbuf_alloc (isa);
  1657. ba = 0;
  1658. for (ia = start_pc, bt = ia; ia < finish_pc ; ia += ilen)
  1659. {
  1660. if (ia + xtensa_isa_maxlength (isa) > bt)
  1661. {
  1662. ba = ia;
  1663. bt = (ba + XTENSA_ISA_BSZ) < finish_pc
  1664. ? ba + XTENSA_ISA_BSZ : finish_pc;
  1665. if (target_read_memory (ba, ibuf, bt - ba) != 0 )
  1666. RETURN_RET;
  1667. }
  1668. xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
  1669. ifmt = xtensa_format_decode (isa, ins);
  1670. if (ifmt == XTENSA_UNDEFINED)
  1671. RETURN_RET;
  1672. ilen = xtensa_format_length (isa, ifmt);
  1673. if (ilen == XTENSA_UNDEFINED)
  1674. RETURN_RET;
  1675. islots = xtensa_format_num_slots (isa, ifmt);
  1676. if (islots == XTENSA_UNDEFINED)
  1677. RETURN_RET;
  1678. for (is = 0; is < islots; ++is)
  1679. {
  1680. if (xtensa_format_get_slot (isa, ifmt, is, ins, slot))
  1681. RETURN_RET;
  1682. opc = xtensa_opcode_decode (isa, ifmt, is, slot);
  1683. if (opc == XTENSA_UNDEFINED)
  1684. RETURN_RET;
  1685. opcname = xtensa_opcode_name (isa, opc);
  1686. if ((strcasecmp (opcname, "ret.n") == 0)
  1687. || (strcasecmp (opcname, "ret") == 0))
  1688. {
  1689. found_ret = 1;
  1690. RETURN_RET;
  1691. }
  1692. }
  1693. }
  1694. done:
  1695. xtensa_insnbuf_free(isa, slot);
  1696. xtensa_insnbuf_free(isa, ins);
  1697. return found_ret;
  1698. }
  1699. /* Call0 opcode class. Opcodes are preclassified according to what they
  1700. mean for Call0 prologue analysis, and their number of significant operands.
  1701. The purpose of this is to simplify prologue analysis by separating
  1702. instruction decoding (libisa) from the semantics of prologue analysis. */
  1703. typedef enum
  1704. {
  1705. c0opc_illegal, /* Unknown to libisa (invalid) or 'ill' opcode. */
  1706. c0opc_uninteresting, /* Not interesting for Call0 prologue analysis. */
  1707. c0opc_flow, /* Flow control insn. */
  1708. c0opc_entry, /* ENTRY indicates non-Call0 prologue. */
  1709. c0opc_break, /* Debugger software breakpoints. */
  1710. c0opc_add, /* Adding two registers. */
  1711. c0opc_addi, /* Adding a register and an immediate. */
  1712. c0opc_and, /* Bitwise "and"-ing two registers. */
  1713. c0opc_sub, /* Subtracting a register from a register. */
  1714. c0opc_mov, /* Moving a register to a register. */
  1715. c0opc_movi, /* Moving an immediate to a register. */
  1716. c0opc_l32r, /* Loading a literal. */
  1717. c0opc_s32i, /* Storing word at fixed offset from a base register. */
  1718. c0opc_rwxsr, /* RSR, WRS, or XSR instructions. */
  1719. c0opc_l32e, /* L32E instruction. */
  1720. c0opc_s32e, /* S32E instruction. */
  1721. c0opc_rfwo, /* RFWO instruction. */
  1722. c0opc_rfwu, /* RFWU instruction. */
  1723. c0opc_NrOf /* Number of opcode classifications. */
  1724. } xtensa_insn_kind;
  1725. /* Return true, if OPCNAME is RSR, WRS, or XSR instruction. */
  1726. static int
  1727. rwx_special_register (const char *opcname)
  1728. {
  1729. char ch = *opcname++;
  1730. if ((ch != 'r') && (ch != 'w') && (ch != 'x'))
  1731. return 0;
  1732. if (*opcname++ != 's')
  1733. return 0;
  1734. if (*opcname++ != 'r')
  1735. return 0;
  1736. if (*opcname++ != '.')
  1737. return 0;
  1738. return 1;
  1739. }
  1740. /* Classify an opcode based on what it means for Call0 prologue analysis. */
  1741. static xtensa_insn_kind
  1742. call0_classify_opcode (xtensa_isa isa, xtensa_opcode opc)
  1743. {
  1744. const char *opcname;
  1745. xtensa_insn_kind opclass = c0opc_uninteresting;
  1746. DEBUGTRACE ("call0_classify_opcode (..., opc = %d)\n", opc);
  1747. /* Get opcode name and handle special classifications. */
  1748. opcname = xtensa_opcode_name (isa, opc);
  1749. if (opcname == NULL
  1750. || strcasecmp (opcname, "ill") == 0
  1751. || strcasecmp (opcname, "ill.n") == 0)
  1752. opclass = c0opc_illegal;
  1753. else if (strcasecmp (opcname, "break") == 0
  1754. || strcasecmp (opcname, "break.n") == 0)
  1755. opclass = c0opc_break;
  1756. else if (strcasecmp (opcname, "entry") == 0)
  1757. opclass = c0opc_entry;
  1758. else if (strcasecmp (opcname, "rfwo") == 0)
  1759. opclass = c0opc_rfwo;
  1760. else if (strcasecmp (opcname, "rfwu") == 0)
  1761. opclass = c0opc_rfwu;
  1762. else if (xtensa_opcode_is_branch (isa, opc) > 0
  1763. || xtensa_opcode_is_jump (isa, opc) > 0
  1764. || xtensa_opcode_is_loop (isa, opc) > 0
  1765. || xtensa_opcode_is_call (isa, opc) > 0
  1766. || strcasecmp (opcname, "simcall") == 0
  1767. || strcasecmp (opcname, "syscall") == 0)
  1768. opclass = c0opc_flow;
  1769. /* Also, classify specific opcodes that need to be tracked. */
  1770. else if (strcasecmp (opcname, "add") == 0
  1771. || strcasecmp (opcname, "add.n") == 0)
  1772. opclass = c0opc_add;
  1773. else if (strcasecmp (opcname, "and") == 0)
  1774. opclass = c0opc_and;
  1775. else if (strcasecmp (opcname, "addi") == 0
  1776. || strcasecmp (opcname, "addi.n") == 0
  1777. || strcasecmp (opcname, "addmi") == 0)
  1778. opclass = c0opc_addi;
  1779. else if (strcasecmp (opcname, "sub") == 0)
  1780. opclass = c0opc_sub;
  1781. else if (strcasecmp (opcname, "mov.n") == 0
  1782. || strcasecmp (opcname, "or") == 0) /* Could be 'mov' asm macro. */
  1783. opclass = c0opc_mov;
  1784. else if (strcasecmp (opcname, "movi") == 0
  1785. || strcasecmp (opcname, "movi.n") == 0)
  1786. opclass = c0opc_movi;
  1787. else if (strcasecmp (opcname, "l32r") == 0)
  1788. opclass = c0opc_l32r;
  1789. else if (strcasecmp (opcname, "s32i") == 0
  1790. || strcasecmp (opcname, "s32i.n") == 0)
  1791. opclass = c0opc_s32i;
  1792. else if (strcasecmp (opcname, "l32e") == 0)
  1793. opclass = c0opc_l32e;
  1794. else if (strcasecmp (opcname, "s32e") == 0)
  1795. opclass = c0opc_s32e;
  1796. else if (rwx_special_register (opcname))
  1797. opclass = c0opc_rwxsr;
  1798. return opclass;
  1799. }
  1800. /* Tracks register movement/mutation for a given operation, which may
  1801. be within a bundle. Updates the destination register tracking info
  1802. accordingly. The pc is needed only for pc-relative load instructions
  1803. (eg. l32r). The SP register number is needed to identify stores to
  1804. the stack frame. Returns 0, if analysis was successful, non-zero
  1805. otherwise. */
  1806. static int
  1807. call0_track_op (struct gdbarch *gdbarch, xtensa_c0reg_t dst[], xtensa_c0reg_t src[],
  1808. xtensa_insn_kind opclass, int nods, unsigned odv[],
  1809. CORE_ADDR pc, int spreg, xtensa_frame_cache_t *cache)
  1810. {
  1811. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1812. unsigned litbase, litaddr, litval;
  1813. xtensa_gdbarch_tdep *tdep = (xtensa_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  1814. switch (opclass)
  1815. {
  1816. case c0opc_addi:
  1817. /* 3 operands: dst, src, imm. */
  1818. gdb_assert (nods == 3);
  1819. dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
  1820. dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs + odv[2];
  1821. break;
  1822. case c0opc_add:
  1823. /* 3 operands: dst, src1, src2. */
  1824. gdb_assert (nods == 3);
  1825. if (src[odv[1]].fr_reg == C0_CONST)
  1826. {
  1827. dst[odv[0]].fr_reg = src[odv[2]].fr_reg;
  1828. dst[odv[0]].fr_ofs = src[odv[2]].fr_ofs + src[odv[1]].fr_ofs;
  1829. }
  1830. else if (src[odv[2]].fr_reg == C0_CONST)
  1831. {
  1832. dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
  1833. dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs + src[odv[2]].fr_ofs;
  1834. }
  1835. else dst[odv[0]].fr_reg = C0_INEXP;
  1836. break;
  1837. case c0opc_and:
  1838. /* 3 operands: dst, src1, src2. */
  1839. gdb_assert (nods == 3);
  1840. if (cache->c0.c0_fpalign == 0)
  1841. {
  1842. /* Handle dynamic stack alignment. */
  1843. if ((src[odv[0]].fr_reg == spreg) && (src[odv[1]].fr_reg == spreg))
  1844. {
  1845. if (src[odv[2]].fr_reg == C0_CONST)
  1846. cache->c0.c0_fpalign = src[odv[2]].fr_ofs;
  1847. break;
  1848. }
  1849. else if ((src[odv[0]].fr_reg == spreg)
  1850. && (src[odv[2]].fr_reg == spreg))
  1851. {
  1852. if (src[odv[1]].fr_reg == C0_CONST)
  1853. cache->c0.c0_fpalign = src[odv[1]].fr_ofs;
  1854. break;
  1855. }
  1856. /* else fall through. */
  1857. }
  1858. if (src[odv[1]].fr_reg == C0_CONST)
  1859. {
  1860. dst[odv[0]].fr_reg = src[odv[2]].fr_reg;
  1861. dst[odv[0]].fr_ofs = src[odv[2]].fr_ofs & src[odv[1]].fr_ofs;
  1862. }
  1863. else if (src[odv[2]].fr_reg == C0_CONST)
  1864. {
  1865. dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
  1866. dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs & src[odv[2]].fr_ofs;
  1867. }
  1868. else dst[odv[0]].fr_reg = C0_INEXP;
  1869. break;
  1870. case c0opc_sub:
  1871. /* 3 operands: dst, src1, src2. */
  1872. gdb_assert (nods == 3);
  1873. if (src[odv[2]].fr_reg == C0_CONST)
  1874. {
  1875. dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
  1876. dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs - src[odv[2]].fr_ofs;
  1877. }
  1878. else dst[odv[0]].fr_reg = C0_INEXP;
  1879. break;
  1880. case c0opc_mov:
  1881. /* 2 operands: dst, src [, src]. */
  1882. gdb_assert (nods == 2);
  1883. /* First, check if it's a special case of saving unaligned SP
  1884. to a spare register in case of dynamic stack adjustment.
  1885. But, only do it one time. The second time could be initializing
  1886. frame pointer. We don't want to overwrite the first one. */
  1887. if ((odv[1] == spreg) && (cache->c0.c0_old_sp == C0_INEXP))
  1888. cache->c0.c0_old_sp = odv[0];
  1889. dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
  1890. dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs;
  1891. break;
  1892. case c0opc_movi:
  1893. /* 2 operands: dst, imm. */
  1894. gdb_assert (nods == 2);
  1895. dst[odv[0]].fr_reg = C0_CONST;
  1896. dst[odv[0]].fr_ofs = odv[1];
  1897. break;
  1898. case c0opc_l32r:
  1899. /* 2 operands: dst, literal offset. */
  1900. gdb_assert (nods == 2);
  1901. /* litbase = xtensa_get_litbase (pc); can be also used. */
  1902. litbase = (tdep->litbase_regnum == -1)
  1903. ? 0 : xtensa_read_register
  1904. (tdep->litbase_regnum);
  1905. litaddr = litbase & 1
  1906. ? (litbase & ~1) + (signed)odv[1]
  1907. : (pc + 3 + (signed)odv[1]) & ~3;
  1908. litval = read_memory_integer (litaddr, 4, byte_order);
  1909. dst[odv[0]].fr_reg = C0_CONST;
  1910. dst[odv[0]].fr_ofs = litval;
  1911. break;
  1912. case c0opc_s32i:
  1913. /* 3 operands: value, base, offset. */
  1914. gdb_assert (nods == 3 && spreg >= 0 && spreg < C0_NREGS);
  1915. /* First, check if it's a spill for saved unaligned SP,
  1916. when dynamic stack adjustment was applied to this frame. */
  1917. if ((cache->c0.c0_fpalign != 0) /* Dynamic stack adjustment. */
  1918. && (odv[1] == spreg) /* SP usage indicates spill. */
  1919. && (odv[0] == cache->c0.c0_old_sp)) /* Old SP register spilled. */
  1920. cache->c0.c0_sp_ofs = odv[2];
  1921. if (src[odv[1]].fr_reg == spreg /* Store to stack frame. */
  1922. && (src[odv[1]].fr_ofs & 3) == 0 /* Alignment preserved. */
  1923. && src[odv[0]].fr_reg >= 0 /* Value is from a register. */
  1924. && src[odv[0]].fr_ofs == 0 /* Value hasn't been modified. */
  1925. && src[src[odv[0]].fr_reg].to_stk == C0_NOSTK) /* First time. */
  1926. {
  1927. /* ISA encoding guarantees alignment. But, check it anyway. */
  1928. gdb_assert ((odv[2] & 3) == 0);
  1929. dst[src[odv[0]].fr_reg].to_stk = src[odv[1]].fr_ofs + odv[2];
  1930. }
  1931. break;
  1932. /* If we end up inside Window Overflow / Underflow interrupt handler
  1933. report an error because these handlers should have been handled
  1934. already in a different way. */
  1935. case c0opc_l32e:
  1936. case c0opc_s32e:
  1937. case c0opc_rfwo:
  1938. case c0opc_rfwu:
  1939. return 1;
  1940. default:
  1941. return 1;
  1942. }
  1943. return 0;
  1944. }
  1945. /* Analyze prologue of the function at start address to determine if it uses
  1946. the Call0 ABI, and if so track register moves and linear modifications
  1947. in the prologue up to the PC or just beyond the prologue, whichever is
  1948. first. An 'entry' instruction indicates non-Call0 ABI and the end of the
  1949. prologue. The prologue may overlap non-prologue instructions but is
  1950. guaranteed to end by the first flow-control instruction (jump, branch,
  1951. call or return). Since an optimized function may move information around
  1952. and change the stack frame arbitrarily during the prologue, the information
  1953. is guaranteed valid only at the point in the function indicated by the PC.
  1954. May be used to skip the prologue or identify the ABI, w/o tracking.
  1955. Returns: Address of first instruction after prologue, or PC (whichever
  1956. is first), or 0, if decoding failed (in libisa).
  1957. Input args:
  1958. start Start address of function/prologue.
  1959. pc Program counter to stop at. Use 0 to continue to end of prologue.
  1960. If 0, avoids infinite run-on in corrupt code memory by bounding
  1961. the scan to the end of the function if that can be determined.
  1962. nregs Number of general registers to track.
  1963. InOut args:
  1964. cache Xtensa frame cache.
  1965. Note that these may produce useful results even if decoding fails
  1966. because they begin with default assumptions that analysis may change. */
  1967. static CORE_ADDR
  1968. call0_analyze_prologue (struct gdbarch *gdbarch,
  1969. CORE_ADDR start, CORE_ADDR pc,
  1970. int nregs, xtensa_frame_cache_t *cache)
  1971. {
  1972. CORE_ADDR ia; /* Current insn address in prologue. */
  1973. CORE_ADDR ba = 0; /* Current address at base of insn buffer. */
  1974. CORE_ADDR bt; /* Current address at top+1 of insn buffer. */
  1975. gdb_byte ibuf[XTENSA_ISA_BSZ];/* Instruction buffer for decoding prologue. */
  1976. xtensa_isa isa; /* libisa ISA handle. */
  1977. xtensa_insnbuf ins, slot; /* libisa handle to decoded insn, slot. */
  1978. xtensa_format ifmt; /* libisa instruction format. */
  1979. int ilen, islots, is; /* Instruction length, nbr slots, current slot. */
  1980. xtensa_opcode opc; /* Opcode in current slot. */
  1981. xtensa_insn_kind opclass; /* Opcode class for Call0 prologue analysis. */
  1982. int nods; /* Opcode number of operands. */
  1983. unsigned odv[C0_MAXOPDS]; /* Operand values in order provided by libisa. */
  1984. xtensa_c0reg_t *rtmp; /* Register tracking info snapshot. */
  1985. int j; /* General loop counter. */
  1986. int fail = 0; /* Set non-zero and exit, if decoding fails. */
  1987. CORE_ADDR body_pc; /* The PC for the first non-prologue insn. */
  1988. CORE_ADDR end_pc; /* The PC for the lust function insn. */
  1989. struct symtab_and_line prologue_sal;
  1990. DEBUGTRACE ("call0_analyze_prologue (start = 0x%08x, pc = 0x%08x, ...)\n",
  1991. (int)start, (int)pc);
  1992. /* Try to limit the scan to the end of the function if a non-zero pc
  1993. arg was not supplied to avoid probing beyond the end of valid memory.
  1994. If memory is full of garbage that classifies as c0opc_uninteresting.
  1995. If this fails (eg. if no symbols) pc ends up 0 as it was.
  1996. Initialize the Call0 frame and register tracking info.
  1997. Assume it's Call0 until an 'entry' instruction is encountered.
  1998. Assume we may be in the prologue until we hit a flow control instr. */
  1999. rtmp = NULL;
  2000. body_pc = UINT_MAX;
  2001. end_pc = 0;
  2002. /* Find out, if we have an information about the prologue from DWARF. */
  2003. prologue_sal = find_pc_line (start, 0);
  2004. if (prologue_sal.line != 0) /* Found debug info. */
  2005. body_pc = prologue_sal.end;
  2006. /* If we are going to analyze the prologue in general without knowing about
  2007. the current PC, make the best assumption for the end of the prologue. */
  2008. if (pc == 0)
  2009. {
  2010. find_pc_partial_function (start, 0, NULL, &end_pc);
  2011. body_pc = std::min (end_pc, body_pc);
  2012. }
  2013. else
  2014. body_pc = std::min (pc, body_pc);
  2015. cache->call0 = 1;
  2016. rtmp = (xtensa_c0reg_t*) alloca(nregs * sizeof(xtensa_c0reg_t));
  2017. isa = xtensa_default_isa;
  2018. gdb_assert (XTENSA_ISA_BSZ >= xtensa_isa_maxlength (isa));
  2019. ins = xtensa_insnbuf_alloc (isa);
  2020. slot = xtensa_insnbuf_alloc (isa);
  2021. for (ia = start, bt = ia; ia < body_pc ; ia += ilen)
  2022. {
  2023. /* (Re)fill instruction buffer from memory if necessary, but do not
  2024. read memory beyond PC to be sure we stay within text section
  2025. (this protection only works if a non-zero pc is supplied). */
  2026. if (ia + xtensa_isa_maxlength (isa) > bt)
  2027. {
  2028. ba = ia;
  2029. bt = (ba + XTENSA_ISA_BSZ) < body_pc ? ba + XTENSA_ISA_BSZ : body_pc;
  2030. if (target_read_memory (ba, ibuf, bt - ba) != 0 )
  2031. error (_("Unable to read target memory ..."));
  2032. }
  2033. /* Decode format information. */
  2034. xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
  2035. ifmt = xtensa_format_decode (isa, ins);
  2036. if (ifmt == XTENSA_UNDEFINED)
  2037. {
  2038. fail = 1;
  2039. goto done;
  2040. }
  2041. ilen = xtensa_format_length (isa, ifmt);
  2042. if (ilen == XTENSA_UNDEFINED)
  2043. {
  2044. fail = 1;
  2045. goto done;
  2046. }
  2047. islots = xtensa_format_num_slots (isa, ifmt);
  2048. if (islots == XTENSA_UNDEFINED)
  2049. {
  2050. fail = 1;
  2051. goto done;
  2052. }
  2053. /* Analyze a bundle or a single instruction, using a snapshot of
  2054. the register tracking info as input for the entire bundle so that
  2055. register changes do not take effect within this bundle. */
  2056. for (j = 0; j < nregs; ++j)
  2057. rtmp[j] = cache->c0.c0_rt[j];
  2058. for (is = 0; is < islots; ++is)
  2059. {
  2060. /* Decode a slot and classify the opcode. */
  2061. fail = xtensa_format_get_slot (isa, ifmt, is, ins, slot);
  2062. if (fail)
  2063. goto done;
  2064. opc = xtensa_opcode_decode (isa, ifmt, is, slot);
  2065. DEBUGVERB ("[call0_analyze_prologue] instr addr = 0x%08x, opc = %d\n",
  2066. (unsigned)ia, opc);
  2067. if (opc == XTENSA_UNDEFINED)
  2068. opclass = c0opc_illegal;
  2069. else
  2070. opclass = call0_classify_opcode (isa, opc);
  2071. /* Decide whether to track this opcode, ignore it, or bail out. */
  2072. switch (opclass)
  2073. {
  2074. case c0opc_illegal:
  2075. case c0opc_break:
  2076. fail = 1;
  2077. goto done;
  2078. case c0opc_uninteresting:
  2079. continue;
  2080. case c0opc_flow: /* Flow control instructions stop analysis. */
  2081. case c0opc_rwxsr: /* RSR, WSR, XSR instructions stop analysis. */
  2082. goto done;
  2083. case c0opc_entry:
  2084. cache->call0 = 0;
  2085. ia += ilen; /* Skip over 'entry' insn. */
  2086. goto done;
  2087. default:
  2088. cache->call0 = 1;
  2089. }
  2090. /* Only expected opcodes should get this far. */
  2091. /* Extract and decode the operands. */
  2092. nods = xtensa_opcode_num_operands (isa, opc);
  2093. if (nods == XTENSA_UNDEFINED)
  2094. {
  2095. fail = 1;
  2096. goto done;
  2097. }
  2098. for (j = 0; j < nods && j < C0_MAXOPDS; ++j)
  2099. {
  2100. fail = xtensa_operand_get_field (isa, opc, j, ifmt,
  2101. is, slot, &odv[j]);
  2102. if (fail)
  2103. goto done;
  2104. fail = xtensa_operand_decode (isa, opc, j, &odv[j]);
  2105. if (fail)
  2106. goto done;
  2107. }
  2108. /* Check operands to verify use of 'mov' assembler macro. */
  2109. if (opclass == c0opc_mov && nods == 3)
  2110. {
  2111. if (odv[2] == odv[1])
  2112. {
  2113. nods = 2;
  2114. if ((odv[0] == 1) && (odv[1] != 1))
  2115. /* OR A1, An, An , where n != 1.
  2116. This means we are inside epilogue already. */
  2117. goto done;
  2118. }
  2119. else
  2120. {
  2121. opclass = c0opc_uninteresting;
  2122. continue;
  2123. }
  2124. }
  2125. /* Track register movement and modification for this operation. */
  2126. fail = call0_track_op (gdbarch, cache->c0.c0_rt, rtmp,
  2127. opclass, nods, odv, ia, 1, cache);
  2128. if (fail)
  2129. goto done;
  2130. }
  2131. }
  2132. done:
  2133. DEBUGVERB ("[call0_analyze_prologue] stopped at instr addr 0x%08x, %s\n",
  2134. (unsigned)ia, fail ? "failed" : "succeeded");
  2135. xtensa_insnbuf_free(isa, slot);
  2136. xtensa_insnbuf_free(isa, ins);
  2137. return fail ? XTENSA_ISA_BADPC : ia;
  2138. }
  2139. /* Initialize frame cache for the current frame in CALL0 ABI. */
  2140. static void
  2141. call0_frame_cache (struct frame_info *this_frame,
  2142. xtensa_frame_cache_t *cache, CORE_ADDR pc)
  2143. {
  2144. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  2145. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  2146. CORE_ADDR start_pc; /* The beginning of the function. */
  2147. CORE_ADDR body_pc=UINT_MAX; /* PC, where prologue analysis stopped. */
  2148. CORE_ADDR sp, fp, ra;
  2149. int fp_regnum = C0_SP, c0_hasfp = 0, c0_frmsz = 0, prev_sp = 0, to_stk;
  2150. xtensa_gdbarch_tdep *tdep = (xtensa_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  2151. sp = get_frame_register_unsigned
  2152. (this_frame, tdep->a0_base + 1);
  2153. fp = sp; /* Assume FP == SP until proven otherwise. */
  2154. /* Find the beginning of the prologue of the function containing the PC
  2155. and analyze it up to the PC or the end of the prologue. */
  2156. if (find_pc_partial_function (pc, NULL, &start_pc, NULL))
  2157. {
  2158. body_pc = call0_analyze_prologue (gdbarch, start_pc, pc, C0_NREGS, cache);
  2159. if (body_pc == XTENSA_ISA_BADPC)
  2160. {
  2161. warning_once ();
  2162. ra = 0;
  2163. goto finish_frame_analysis;
  2164. }
  2165. }
  2166. /* Get the frame information and FP (if used) at the current PC.
  2167. If PC is in the prologue, the prologue analysis is more reliable
  2168. than DWARF info. We don't not know for sure, if PC is in the prologue,
  2169. but we do know no calls have yet taken place, so we can almost
  2170. certainly rely on the prologue analysis. */
  2171. if (body_pc <= pc)
  2172. {
  2173. /* Prologue analysis was successful up to the PC.
  2174. It includes the cases when PC == START_PC. */
  2175. c0_hasfp = cache->c0.c0_rt[C0_FP].fr_reg == C0_SP;
  2176. /* c0_hasfp == true means there is a frame pointer because
  2177. we analyzed the prologue and found that cache->c0.c0_rt[C0_FP]
  2178. was derived from SP. Otherwise, it would be C0_FP. */
  2179. fp_regnum = c0_hasfp ? C0_FP : C0_SP;
  2180. c0_frmsz = - cache->c0.c0_rt[fp_regnum].fr_ofs;
  2181. fp_regnum += tdep->a0_base;
  2182. }
  2183. else /* No data from the prologue analysis. */
  2184. {
  2185. c0_hasfp = 0;
  2186. fp_regnum = tdep->a0_base + C0_SP;
  2187. c0_frmsz = 0;
  2188. start_pc = pc;
  2189. }
  2190. if (cache->c0.c0_fpalign)
  2191. {
  2192. /* This frame has a special prologue with a dynamic stack adjustment
  2193. to force an alignment, which is bigger than standard 16 bytes. */
  2194. CORE_ADDR unaligned_sp;
  2195. if (cache->c0.c0_old_sp == C0_INEXP)
  2196. /* This can't be. Prologue code should be consistent.
  2197. Unaligned stack pointer should be saved in a spare register. */
  2198. {
  2199. warning_once ();
  2200. ra = 0;
  2201. goto finish_frame_analysis;
  2202. }
  2203. if (cache->c0.c0_sp_ofs == C0_NOSTK)
  2204. /* Saved unaligned value of SP is kept in a register. */
  2205. unaligned_sp = get_frame_register_unsigned
  2206. (this_frame, tdep->a0_base + cache->c0.c0_old_sp);
  2207. else
  2208. /* Get the value from stack. */
  2209. unaligned_sp = (CORE_ADDR)
  2210. read_memory_integer (fp + cache->c0.c0_sp_ofs, 4, byte_order);
  2211. prev_sp = unaligned_sp + c0_frmsz;
  2212. }
  2213. else
  2214. prev_sp = fp + c0_frmsz;
  2215. /* Frame size from debug info or prologue tracking does not account for
  2216. alloca() and other dynamic allocations. Adjust frame size by FP - SP. */
  2217. if (c0_hasfp)
  2218. {
  2219. fp = get_frame_register_unsigned (this_frame, fp_regnum);
  2220. /* Update the stack frame size. */
  2221. c0_frmsz += fp - sp;
  2222. }
  2223. /* Get the return address (RA) from the stack if saved,
  2224. or try to get it from a register. */
  2225. to_stk = cache->c0.c0_rt[C0_RA].to_stk;
  2226. if (to_stk != C0_NOSTK)
  2227. ra = (CORE_ADDR)
  2228. read_memory_integer (sp + c0_frmsz + cache->c0.c0_rt[C0_RA].to_stk,
  2229. 4, byte_order);
  2230. else if (cache->c0.c0_rt[C0_RA].fr_reg == C0_CONST
  2231. && cache->c0.c0_rt[C0_RA].fr_ofs == 0)
  2232. {
  2233. /* Special case for terminating backtrace at a function that wants to
  2234. be seen as the outermost one. Such a function will clear it's RA (A0)
  2235. register to 0 in the prologue instead of saving its original value. */
  2236. ra = 0;
  2237. }
  2238. else
  2239. {
  2240. /* RA was copied to another register or (before any function call) may
  2241. still be in the original RA register. This is not always reliable:
  2242. even in a leaf function, register tracking stops after prologue, and
  2243. even in prologue, non-prologue instructions (not tracked) may overwrite
  2244. RA or any register it was copied to. If likely in prologue or before
  2245. any call, use retracking info and hope for the best (compiler should
  2246. have saved RA in stack if not in a leaf function). If not in prologue,
  2247. too bad. */
  2248. int i;
  2249. for (i = 0;
  2250. (i < C0_NREGS)
  2251. && (i == C0_RA || cache->c0.c0_rt[i].fr_reg != C0_RA);
  2252. ++i);
  2253. if (i >= C0_NREGS && cache->c0.c0_rt[C0_RA].fr_reg == C0_RA)
  2254. i = C0_RA;
  2255. if (i < C0_NREGS)
  2256. {
  2257. ra = get_frame_register_unsigned
  2258. (this_frame,
  2259. tdep->a0_base + cache->c0.c0_rt[i].fr_reg);
  2260. }
  2261. else ra = 0;
  2262. }
  2263. finish_frame_analysis:
  2264. cache->pc = start_pc;
  2265. cache->ra = ra;
  2266. /* RA == 0 marks the outermost frame. Do not go past it. */
  2267. cache->prev_sp = (ra != 0) ? prev_sp : 0;
  2268. cache->c0.fp_regnum = fp_regnum;
  2269. cache->c0.c0_frmsz = c0_frmsz;
  2270. cache->c0.c0_hasfp = c0_hasfp;
  2271. cache->c0.c0_fp = fp;
  2272. }
  2273. static CORE_ADDR a0_saved;
  2274. static CORE_ADDR a7_saved;
  2275. static CORE_ADDR a11_saved;
  2276. static int a0_was_saved;
  2277. static int a7_was_saved;
  2278. static int a11_was_saved;
  2279. /* Simulate L32E instruction: AT <-- ref (AS + offset). */
  2280. static void
  2281. execute_l32e (struct gdbarch *gdbarch, int at, int as, int offset, CORE_ADDR wb)
  2282. {
  2283. xtensa_gdbarch_tdep *tdep = (xtensa_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  2284. int atreg = arreg_number (gdbarch, tdep->a0_base + at, wb);
  2285. int asreg = arreg_number (gdbarch, tdep->a0_base + as, wb);
  2286. CORE_ADDR addr = xtensa_read_register (asreg) + offset;
  2287. unsigned int spilled_value
  2288. = read_memory_unsigned_integer (addr, 4, gdbarch_byte_order (gdbarch));
  2289. if ((at == 0) && !a0_was_saved)
  2290. {
  2291. a0_saved = xtensa_read_register (atreg);
  2292. a0_was_saved = 1;
  2293. }
  2294. else if ((at == 7) && !a7_was_saved)
  2295. {
  2296. a7_saved = xtensa_read_register (atreg);
  2297. a7_was_saved = 1;
  2298. }
  2299. else if ((at == 11) && !a11_was_saved)
  2300. {
  2301. a11_saved = xtensa_read_register (atreg);
  2302. a11_was_saved = 1;
  2303. }
  2304. xtensa_write_register (atreg, spilled_value);
  2305. }
  2306. /* Simulate S32E instruction: AT --> ref (AS + offset). */
  2307. static void
  2308. execute_s32e (struct gdbarch *gdbarch, int at, int as, int offset, CORE_ADDR wb)
  2309. {
  2310. xtensa_gdbarch_tdep *tdep = (xtensa_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  2311. int atreg = arreg_number (gdbarch, tdep->a0_base + at, wb);
  2312. int asreg = arreg_number (gdbarch, tdep->a0_base + as, wb);
  2313. CORE_ADDR addr = xtensa_read_register (asreg) + offset;
  2314. ULONGEST spilled_value = xtensa_read_register (atreg);
  2315. write_memory_unsigned_integer (addr, 4,
  2316. gdbarch_byte_order (gdbarch),
  2317. spilled_value);
  2318. }
  2319. #define XTENSA_MAX_WINDOW_INTERRUPT_HANDLER_LEN 200
  2320. typedef enum
  2321. {
  2322. xtWindowOverflow,
  2323. xtWindowUnderflow,
  2324. xtNoExceptionHandler
  2325. } xtensa_exception_handler_t;
  2326. /* Execute instruction stream from current PC until hitting RFWU or RFWO.
  2327. Return type of Xtensa Window Interrupt Handler on success. */
  2328. static xtensa_exception_handler_t
  2329. execute_code (struct gdbarch *gdbarch, CORE_ADDR current_pc, CORE_ADDR wb)
  2330. {
  2331. xtensa_isa isa;
  2332. xtensa_insnbuf ins, slot;
  2333. gdb_byte ibuf[XTENSA_ISA_BSZ];
  2334. CORE_ADDR ia, bt, ba;
  2335. xtensa_format ifmt;
  2336. int ilen, islots, is;
  2337. xtensa_opcode opc;
  2338. int insn_num = 0;
  2339. void (*func) (struct gdbarch *, int, int, int, CORE_ADDR);
  2340. xtensa_gdbarch_tdep *tdep = (xtensa_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  2341. uint32_t at, as, offset;
  2342. /* WindowUnderflow12 = true, when inside _WindowUnderflow12. */
  2343. int WindowUnderflow12 = (current_pc & 0x1ff) >= 0x140;
  2344. isa = xtensa_default_isa;
  2345. gdb_assert (XTENSA_ISA_BSZ >= xtensa_isa_maxlength (isa));
  2346. ins = xtensa_insnbuf_alloc (isa);
  2347. slot = xtensa_insnbuf_alloc (isa);
  2348. ba = 0;
  2349. ia = current_pc;
  2350. bt = ia;
  2351. a0_was_saved = 0;
  2352. a7_was_saved = 0;
  2353. a11_was_saved = 0;
  2354. while (insn_num++ < XTENSA_MAX_WINDOW_INTERRUPT_HANDLER_LEN)
  2355. {
  2356. if (ia + xtensa_isa_maxlength (isa) > bt)
  2357. {
  2358. ba = ia;
  2359. bt = (ba + XTENSA_ISA_BSZ);
  2360. if (target_read_memory (ba, ibuf, bt - ba) != 0)
  2361. return xtNoExceptionHandler;
  2362. }
  2363. xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
  2364. ifmt = xtensa_format_decode (isa, ins);
  2365. if (ifmt == XTENSA_UNDEFINED)
  2366. return xtNoExceptionHandler;
  2367. ilen = xtensa_format_length (isa, ifmt);
  2368. if (ilen == XTENSA_UNDEFINED)
  2369. return xtNoExceptionHandler;
  2370. islots = xtensa_format_num_slots (isa, ifmt);
  2371. if (islots == XTENSA_UNDEFINED)
  2372. return xtNoExceptionHandler;
  2373. for (is = 0; is < islots; ++is)
  2374. {
  2375. if (xtensa_format_get_slot (isa, ifmt, is, ins, slot))
  2376. return xtNoExceptionHandler;
  2377. opc = xtensa_opcode_decode (isa, ifmt, is, slot);
  2378. if (opc == XTENSA_UNDEFINED)
  2379. return xtNoExceptionHandler;
  2380. switch (call0_classify_opcode (isa, opc))
  2381. {
  2382. case c0opc_illegal:
  2383. case c0opc_flow:
  2384. case c0opc_entry:
  2385. case c0opc_break:
  2386. /* We expect none of them here. */
  2387. return xtNoExceptionHandler;
  2388. case c0opc_l32e:
  2389. func = execute_l32e;
  2390. break;
  2391. case c0opc_s32e:
  2392. func = execute_s32e;
  2393. break;
  2394. case c0opc_rfwo: /* RFWO. */
  2395. /* Here, we return from WindowOverflow handler and,
  2396. if we stopped at the very beginning, which means
  2397. A0 was saved, we have to restore it now. */
  2398. if (a0_was_saved)
  2399. {
  2400. int arreg = arreg_number (gdbarch,
  2401. tdep->a0_base,
  2402. wb);
  2403. xtensa_write_register (arreg, a0_saved);
  2404. }
  2405. return xtWindowOverflow;
  2406. case c0opc_rfwu: /* RFWU. */
  2407. /* Here, we return from WindowUnderflow handler.
  2408. Let's see if either A7 or A11 has to be restored. */
  2409. if (WindowUnderflow12)
  2410. {
  2411. if (a11_was_saved)
  2412. {
  2413. int arreg = arreg_number (gdbarch,
  2414. tdep->a0_base + 11,
  2415. wb);
  2416. xtensa_write_register (arreg, a11_saved);
  2417. }
  2418. }
  2419. else if (a7_was_saved)
  2420. {
  2421. int arreg = arreg_number (gdbarch,
  2422. tdep->a0_base + 7,
  2423. wb);
  2424. xtensa_write_register (arreg, a7_saved);
  2425. }
  2426. return xtWindowUnderflow;
  2427. default: /* Simply skip this insns. */
  2428. continue;
  2429. }
  2430. /* Decode arguments for L32E / S32E and simulate their execution. */
  2431. if ( xtensa_opcode_num_operands (isa, opc) != 3 )
  2432. return xtNoExceptionHandler;
  2433. if (xtensa_operand_get_field (isa, opc, 0, ifmt, is, slot, &at))
  2434. return xtNoExceptionHandler;
  2435. if (xtensa_operand_decode (isa, opc, 0, &at))
  2436. return xtNoExceptionHandler;
  2437. if (xtensa_operand_get_field (isa, opc, 1, ifmt, is, slot, &as))
  2438. return xtNoExceptionHandler;
  2439. if (xtensa_operand_decode (isa, opc, 1, &as))
  2440. return xtNoExceptionHandler;
  2441. if (xtensa_operand_get_field (isa, opc, 2, ifmt, is, slot, &offset))
  2442. return xtNoExceptionHandler;
  2443. if (xtensa_operand_decode (isa, opc, 2, &offset))
  2444. return xtNoExceptionHandler;
  2445. (*func) (gdbarch, at, as, offset, wb);
  2446. }
  2447. ia += ilen;
  2448. }
  2449. return xtNoExceptionHandler;
  2450. }
  2451. /* Handle Window Overflow / Underflow exception frames. */
  2452. static void
  2453. xtensa_window_interrupt_frame_cache (struct frame_info *this_frame,
  2454. xtensa_frame_cache_t *cache,
  2455. CORE_ADDR pc)
  2456. {
  2457. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  2458. CORE_ADDR ps, wb, ws, ra;
  2459. int epc1_regnum, i, regnum;
  2460. xtensa_exception_handler_t eh_type;
  2461. xtensa_gdbarch_tdep *tdep = (xtensa_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  2462. /* Read PS, WB, and WS from the hardware. Note that PS register
  2463. must be present, if Windowed ABI is supported. */
  2464. ps = xtensa_read_register (gdbarch_ps_regnum (gdbarch));
  2465. wb = xtensa_read_register (tdep->wb_regnum);
  2466. ws = xtensa_read_register (tdep->ws_regnum);
  2467. /* Execute all the remaining instructions from Window Interrupt Handler
  2468. by simulating them on the remote protocol level. On return, set the
  2469. type of Xtensa Window Interrupt Handler, or report an error. */
  2470. eh_type = execute_code (gdbarch, pc, wb);
  2471. if (eh_type == xtNoExceptionHandler)
  2472. error (_("\
  2473. Unable to decode Xtensa Window Interrupt Handler's code."));
  2474. cache->ps = ps ^ PS_EXC; /* Clear the exception bit in PS. */
  2475. cache->call0 = 0; /* It's Windowed ABI. */
  2476. /* All registers for the cached frame will be alive. */
  2477. for (i = 0; i < XTENSA_NUM_SAVED_AREGS; i++)
  2478. cache->wd.aregs[i] = -1;
  2479. if (eh_type == xtWindowOverflow)
  2480. cache->wd.ws = ws ^ (1 << wb);
  2481. else /* eh_type == xtWindowUnderflow. */
  2482. cache->wd.ws = ws | (1 << wb);
  2483. cache->wd.wb = (ps & 0xf00) >> 8; /* Set WB to OWB. */
  2484. regnum = arreg_number (gdbarch, tdep->a0_base,
  2485. cache->wd.wb);
  2486. ra = xtensa_read_register (regnum);
  2487. cache->wd.callsize = WINSIZE (ra);
  2488. cache->prev_sp = xtensa_read_register (regnum + 1);
  2489. /* Set regnum to a frame pointer of the frame being cached. */
  2490. regnum = xtensa_scan_prologue (gdbarch, pc);
  2491. regnum = arreg_number (gdbarch,
  2492. tdep->a0_base + regnum,
  2493. cache->wd.wb);
  2494. cache->base = get_frame_register_unsigned (this_frame, regnum);
  2495. /* Read PC of interrupted function from EPC1 register. */
  2496. epc1_regnum = xtensa_find_register_by_name (gdbarch,"epc1");
  2497. if (epc1_regnum < 0)
  2498. error(_("Unable to read Xtensa register EPC1"));
  2499. cache->ra = xtensa_read_register (epc1_regnum);
  2500. cache->pc = get_frame_func (this_frame);
  2501. }
  2502. /* Skip function prologue.
  2503. Return the pc of the first instruction after prologue. GDB calls this to
  2504. find the address of the first line of the function or (if there is no line
  2505. number information) to skip the prologue for planting breakpoints on
  2506. function entries. Use debug info (if present) or prologue analysis to skip
  2507. the prologue to achieve reliable debugging behavior. For windowed ABI,
  2508. only the 'entry' instruction is skipped. It is not strictly necessary to
  2509. skip the prologue (Call0) or 'entry' (Windowed) because xt-gdb knows how to
  2510. backtrace at any point in the prologue, however certain potential hazards
  2511. are avoided and a more "normal" debugging experience is ensured by
  2512. skipping the prologue (can be disabled by defining DONT_SKIP_PROLOG).
  2513. For example, if we don't skip the prologue:
  2514. - Some args may not yet have been saved to the stack where the debug
  2515. info expects to find them (true anyway when only 'entry' is skipped);
  2516. - Software breakpoints ('break' instrs) may not have been unplanted
  2517. when the prologue analysis is done on initializing the frame cache,
  2518. and breaks in the prologue will throw off the analysis.
  2519. If we have debug info ( line-number info, in particular ) we simply skip
  2520. the code associated with the first function line effectively skipping
  2521. the prologue code. It works even in cases like
  2522. int main()
  2523. { int local_var = 1;
  2524. ....
  2525. }
  2526. because, for this source code, both Xtensa compilers will generate two
  2527. separate entries ( with the same line number ) in dwarf line-number
  2528. section to make sure there is a boundary between the prologue code and
  2529. the rest of the function.
  2530. If there is no debug info, we need to analyze the code. */
  2531. /* #define DONT_SKIP_PROLOGUE */
  2532. static CORE_ADDR
  2533. xtensa_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
  2534. {
  2535. struct symtab_and_line prologue_sal;
  2536. CORE_ADDR body_pc;
  2537. DEBUGTRACE ("xtensa_skip_prologue (start_pc = 0x%08x)\n", (int) start_pc);
  2538. #if DONT_SKIP_PROLOGUE
  2539. return start_pc;
  2540. #endif
  2541. /* Try to find first body line from debug info. */
  2542. prologue_sal = find_pc_line (start_pc, 0);
  2543. if (prologue_sal.line != 0) /* Found debug info. */
  2544. {
  2545. /* In Call0, it is possible to have a function with only one instruction
  2546. ('ret') resulting from a one-line optimized function that does nothing.
  2547. In that case, prologue_sal.end may actually point to the start of the
  2548. next function in the text section, causing a breakpoint to be set at
  2549. the wrong place. Check, if the end address is within a different
  2550. function, and if so return the start PC. We know we have symbol
  2551. information. */
  2552. CORE_ADDR end_func;
  2553. xtensa_gdbarch_tdep *tdep = (xtensa_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  2554. if ((tdep->call_abi == CallAbiCall0Only)
  2555. && call0_ret (start_pc, prologue_sal.end))
  2556. return start_pc;
  2557. find_pc_partial_function (prologue_sal.end, NULL, &end_func, NULL);
  2558. if (end_func != start_pc)
  2559. return start_pc;
  2560. return prologue_sal.end;
  2561. }
  2562. /* No debug line info. Analyze prologue for Call0 or simply skip ENTRY. */
  2563. body_pc = call0_analyze_prologue (gdbarch, start_pc, 0, 0,
  2564. xtensa_alloc_frame_cache (0));
  2565. return body_pc != 0 ? body_pc : start_pc;
  2566. }
  2567. /* Verify the current configuration. */
  2568. static void
  2569. xtensa_verify_config (struct gdbarch *gdbarch)
  2570. {
  2571. xtensa_gdbarch_tdep *tdep = (xtensa_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  2572. string_file log;
  2573. /* Verify that we got a reasonable number of AREGS. */
  2574. if ((tdep->num_aregs & -tdep->num_aregs) != tdep->num_aregs)
  2575. log.printf (_("\
  2576. \n\tnum_aregs: Number of AR registers (%d) is not a power of two!"),
  2577. tdep->num_aregs);
  2578. /* Verify that certain registers exist. */
  2579. if (tdep->pc_regnum == -1)
  2580. log.printf (_("\n\tpc_regnum: No PC register"));
  2581. if (tdep->isa_use_exceptions && tdep->ps_regnum == -1)
  2582. log.printf (_("\n\tps_regnum: No PS register"));
  2583. if (tdep->isa_use_windowed_registers)
  2584. {
  2585. if (tdep->wb_regnum == -1)
  2586. log.printf (_("\n\twb_regnum: No WB register"));
  2587. if (tdep->ws_regnum == -1)
  2588. log.printf (_("\n\tws_regnum: No WS register"));
  2589. if (tdep->ar_base == -1)
  2590. log.printf (_("\n\tar_base: No AR registers"));
  2591. }
  2592. if (tdep->a0_base == -1)
  2593. log.printf (_("\n\ta0_base: No Ax registers"));
  2594. if (!log.empty ())
  2595. internal_error (__FILE__, __LINE__,
  2596. _("the following are invalid: %s"), log.c_str ());
  2597. }
  2598. /* Derive specific register numbers from the array of registers. */
  2599. static void
  2600. xtensa_derive_tdep (xtensa_gdbarch_tdep *tdep)
  2601. {
  2602. xtensa_register_t* rmap;
  2603. int n, max_size = 4;
  2604. tdep->num_regs = 0;
  2605. tdep->num_nopriv_regs = 0;
  2606. /* Special registers 0..255 (core). */
  2607. #define XTENSA_DBREGN_SREG(n) (0x0200+(n))
  2608. /* User registers 0..255. */
  2609. #define XTENSA_DBREGN_UREG(n) (0x0300+(n))
  2610. for (rmap = tdep->regmap, n = 0; rmap->target_number != -1; n++, rmap++)
  2611. {
  2612. if (rmap->target_number == 0x0020)
  2613. tdep->pc_regnum = n;
  2614. else if (rmap->target_number == 0x0100)
  2615. tdep->ar_base = n;
  2616. else if (rmap->target_number == 0x0000)
  2617. tdep->a0_base = n;
  2618. else if (rmap->target_number == XTENSA_DBREGN_SREG(72))
  2619. tdep->wb_regnum = n;
  2620. else if (rmap->target_number == XTENSA_DBREGN_SREG(73))
  2621. tdep->ws_regnum = n;
  2622. else if (rmap->target_number == XTENSA_DBREGN_SREG(233))
  2623. tdep->debugcause_regnum = n;
  2624. else if (rmap->target_number == XTENSA_DBREGN_SREG(232))
  2625. tdep->exccause_regnum = n;
  2626. else if (rmap->target_number == XTENSA_DBREGN_SREG(238))
  2627. tdep->excvaddr_regnum = n;
  2628. else if (rmap->target_number == XTENSA_DBREGN_SREG(0))
  2629. tdep->lbeg_regnum = n;
  2630. else if (rmap->target_number == XTENSA_DBREGN_SREG(1))
  2631. tdep->lend_regnum = n;
  2632. else if (rmap->target_number == XTENSA_DBREGN_SREG(2))
  2633. tdep->lcount_regnum = n;
  2634. else if (rmap->target_number == XTENSA_DBREGN_SREG(3))
  2635. tdep->sar_regnum = n;
  2636. else if (rmap->target_number == XTENSA_DBREGN_SREG(5))
  2637. tdep->litbase_regnum = n;
  2638. else if (rmap->target_number == XTENSA_DBREGN_SREG(230))
  2639. tdep->ps_regnum = n;
  2640. else if (rmap->target_number == XTENSA_DBREGN_UREG(231))
  2641. tdep->threadptr_regnum = n;
  2642. #if 0
  2643. else if (rmap->target_number == XTENSA_DBREGN_SREG(226))
  2644. tdep->interrupt_regnum = n;
  2645. else if (rmap->target_number == XTENSA_DBREGN_SREG(227))
  2646. tdep->interrupt2_regnum = n;
  2647. else if (rmap->target_number == XTENSA_DBREGN_SREG(224))
  2648. tdep->cpenable_regnum = n;
  2649. #endif
  2650. if (rmap->byte_size > max_size)
  2651. max_size = rmap->byte_size;
  2652. if (rmap->mask != 0 && tdep->num_regs == 0)
  2653. tdep->num_regs = n;
  2654. if ((rmap->flags & XTENSA_REGISTER_FLAGS_PRIVILEGED) != 0
  2655. && tdep->num_nopriv_regs == 0)
  2656. tdep->num_nopriv_regs = n;
  2657. }
  2658. if (tdep->num_regs == 0)
  2659. tdep->num_regs = tdep->num_nopriv_regs;
  2660. /* Number of pseudo registers. */
  2661. tdep->num_pseudo_regs = n - tdep->num_regs;
  2662. /* Empirically determined maximum sizes. */
  2663. tdep->max_register_raw_size = max_size;
  2664. tdep->max_register_virtual_size = max_size;
  2665. }
  2666. /* Module "constructor" function. */
  2667. extern xtensa_gdbarch_tdep xtensa_tdep;
  2668. static struct gdbarch *
  2669. xtensa_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  2670. {
  2671. struct gdbarch *gdbarch;
  2672. DEBUGTRACE ("gdbarch_init()\n");
  2673. if (!xtensa_default_isa)
  2674. xtensa_default_isa = xtensa_isa_init (0, 0);
  2675. /* We have to set the byte order before we call gdbarch_alloc. */
  2676. info.byte_order = XCHAL_HAVE_BE ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
  2677. xtensa_gdbarch_tdep *tdep = &xtensa_tdep;
  2678. gdbarch = gdbarch_alloc (&info, tdep);
  2679. xtensa_derive_tdep (tdep);
  2680. /* Verify our configuration. */
  2681. xtensa_verify_config (gdbarch);
  2682. xtensa_session_once_reported = 0;
  2683. set_gdbarch_wchar_bit (gdbarch, 2 * TARGET_CHAR_BIT);
  2684. set_gdbarch_wchar_signed (gdbarch, 0);
  2685. /* Pseudo-Register read/write. */
  2686. set_gdbarch_pseudo_register_read (gdbarch, xtensa_pseudo_register_read);
  2687. set_gdbarch_pseudo_register_write (gdbarch, xtensa_pseudo_register_write);
  2688. /* Set target information. */
  2689. set_gdbarch_num_regs (gdbarch, tdep->num_regs);
  2690. set_gdbarch_num_pseudo_regs (gdbarch, tdep->num_pseudo_regs);
  2691. set_gdbarch_sp_regnum (gdbarch, tdep->a0_base + 1);
  2692. set_gdbarch_pc_regnum (gdbarch, tdep->pc_regnum);
  2693. set_gdbarch_ps_regnum (gdbarch, tdep->ps_regnum);
  2694. /* Renumber registers for known formats (stabs and dwarf2). */
  2695. set_gdbarch_stab_reg_to_regnum (gdbarch, xtensa_reg_to_regnum);
  2696. set_gdbarch_dwarf2_reg_to_regnum (gdbarch, xtensa_reg_to_regnum);
  2697. /* We provide our own function to get register information. */
  2698. set_gdbarch_register_name (gdbarch, xtensa_register_name);
  2699. set_gdbarch_register_type (gdbarch, xtensa_register_type);
  2700. /* To call functions from GDB using dummy frame. */
  2701. set_gdbarch_push_dummy_call (gdbarch, xtensa_push_dummy_call);
  2702. set_gdbarch_believe_pcc_promotion (gdbarch, 1);
  2703. set_gdbarch_return_value (gdbarch, xtensa_return_value);
  2704. /* Advance PC across any prologue instructions to reach "real" code. */
  2705. set_gdbarch_skip_prologue (gdbarch, xtensa_skip_prologue);
  2706. /* Stack grows downward. */
  2707. set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  2708. /* Set breakpoints. */
  2709. set_gdbarch_breakpoint_kind_from_pc (gdbarch,
  2710. xtensa_breakpoint_kind_from_pc);
  2711. set_gdbarch_sw_breakpoint_from_kind (gdbarch,
  2712. xtensa_sw_breakpoint_from_kind);
  2713. /* After breakpoint instruction or illegal instruction, pc still
  2714. points at break instruction, so don't decrement. */
  2715. set_gdbarch_decr_pc_after_break (gdbarch, 0);
  2716. /* We don't skip args. */
  2717. set_gdbarch_frame_args_skip (gdbarch, 0);
  2718. set_gdbarch_unwind_pc (gdbarch, xtensa_unwind_pc);
  2719. set_gdbarch_frame_align (gdbarch, xtensa_frame_align);
  2720. set_gdbarch_dummy_id (gdbarch, xtensa_dummy_id);
  2721. /* Frame handling. */
  2722. frame_base_set_default (gdbarch, &xtensa_frame_base);
  2723. frame_unwind_append_unwinder (gdbarch, &xtensa_unwind);
  2724. dwarf2_append_unwinders (gdbarch);
  2725. set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
  2726. xtensa_add_reggroups (gdbarch);
  2727. set_gdbarch_register_reggroup_p (gdbarch, xtensa_register_reggroup_p);
  2728. set_gdbarch_iterate_over_regset_sections
  2729. (gdbarch, xtensa_iterate_over_regset_sections);
  2730. set_solib_svr4_fetch_link_map_offsets
  2731. (gdbarch, svr4_ilp32_fetch_link_map_offsets);
  2732. /* Hook in the ABI-specific overrides, if they have been registered. */
  2733. gdbarch_init_osabi (info, gdbarch);
  2734. return gdbarch;
  2735. }
  2736. static void
  2737. xtensa_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
  2738. {
  2739. error (_("xtensa_dump_tdep(): not implemented"));
  2740. }
  2741. void _initialize_xtensa_tdep ();
  2742. void
  2743. _initialize_xtensa_tdep ()
  2744. {
  2745. gdbarch_register (bfd_arch_xtensa, xtensa_gdbarch_init, xtensa_dump_tdep);
  2746. xtensa_init_reggroups ();
  2747. add_setshow_zuinteger_cmd ("xtensa",
  2748. class_maintenance,
  2749. &xtensa_debug_level,
  2750. _("Set Xtensa debugging."),
  2751. _("Show Xtensa debugging."), _("\
  2752. When non-zero, Xtensa-specific debugging is enabled. \
  2753. Can be 1, 2, 3, or 4 indicating the level of debugging."),
  2754. NULL,
  2755. NULL,
  2756. &setdebuglist, &showdebuglist);
  2757. }