riscv-tdep.c 132 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135
  1. /* Target-dependent code for the RISC-V architecture, for GDB.
  2. Copyright (C) 2018-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 "inferior.h"
  17. #include "symtab.h"
  18. #include "value.h"
  19. #include "gdbcmd.h"
  20. #include "language.h"
  21. #include "gdbcore.h"
  22. #include "symfile.h"
  23. #include "objfiles.h"
  24. #include "gdbtypes.h"
  25. #include "target.h"
  26. #include "arch-utils.h"
  27. #include "regcache.h"
  28. #include "osabi.h"
  29. #include "riscv-tdep.h"
  30. #include "block.h"
  31. #include "reggroups.h"
  32. #include "opcode/riscv.h"
  33. #include "elf/riscv.h"
  34. #include "elf-bfd.h"
  35. #include "symcat.h"
  36. #include "dis-asm.h"
  37. #include "frame-unwind.h"
  38. #include "frame-base.h"
  39. #include "trad-frame.h"
  40. #include "infcall.h"
  41. #include "floatformat.h"
  42. #include "remote.h"
  43. #include "target-descriptions.h"
  44. #include "dwarf2/frame.h"
  45. #include "user-regs.h"
  46. #include "valprint.h"
  47. #include "gdbsupport/common-defs.h"
  48. #include "opcode/riscv-opc.h"
  49. #include "cli/cli-decode.h"
  50. #include "observable.h"
  51. #include "prologue-value.h"
  52. #include "arch/riscv.h"
  53. #include "riscv-ravenscar-thread.h"
  54. /* The stack must be 16-byte aligned. */
  55. #define SP_ALIGNMENT 16
  56. /* The biggest alignment that the target supports. */
  57. #define BIGGEST_ALIGNMENT 16
  58. /* Define a series of is_XXX_insn functions to check if the value INSN
  59. is an instance of instruction XXX. */
  60. #define DECLARE_INSN(INSN_NAME, INSN_MATCH, INSN_MASK) \
  61. static inline bool is_ ## INSN_NAME ## _insn (long insn) \
  62. { \
  63. return (insn & INSN_MASK) == INSN_MATCH; \
  64. }
  65. #include "opcode/riscv-opc.h"
  66. #undef DECLARE_INSN
  67. /* When this is set to non-zero debugging information about breakpoint
  68. kinds will be printed. */
  69. static unsigned int riscv_debug_breakpoints = 0;
  70. /* When this is set to non-zero debugging information about inferior calls
  71. will be printed. */
  72. static unsigned int riscv_debug_infcall = 0;
  73. /* When this is set to non-zero debugging information about stack unwinding
  74. will be printed. */
  75. static unsigned int riscv_debug_unwinder = 0;
  76. /* When this is set to non-zero debugging information about gdbarch
  77. initialisation will be printed. */
  78. static unsigned int riscv_debug_gdbarch = 0;
  79. /* The names of the RISC-V target description features. */
  80. const char *riscv_feature_name_csr = "org.gnu.gdb.riscv.csr";
  81. static const char *riscv_feature_name_cpu = "org.gnu.gdb.riscv.cpu";
  82. static const char *riscv_feature_name_fpu = "org.gnu.gdb.riscv.fpu";
  83. static const char *riscv_feature_name_virtual = "org.gnu.gdb.riscv.virtual";
  84. static const char *riscv_feature_name_vector = "org.gnu.gdb.riscv.vector";
  85. /* The current set of options to be passed to the disassembler. */
  86. static char *riscv_disassembler_options;
  87. /* Cached information about a frame. */
  88. struct riscv_unwind_cache
  89. {
  90. /* The register from which we can calculate the frame base. This is
  91. usually $sp or $fp. */
  92. int frame_base_reg;
  93. /* The offset from the current value in register FRAME_BASE_REG to the
  94. actual frame base address. */
  95. int frame_base_offset;
  96. /* Information about previous register values. */
  97. trad_frame_saved_reg *regs;
  98. /* The id for this frame. */
  99. struct frame_id this_id;
  100. /* The base (stack) address for this frame. This is the stack pointer
  101. value on entry to this frame before any adjustments are made. */
  102. CORE_ADDR frame_base;
  103. };
  104. /* RISC-V specific register group for CSRs. */
  105. static const reggroup *csr_reggroup = nullptr;
  106. /* Callback function for user_reg_add. */
  107. static struct value *
  108. value_of_riscv_user_reg (struct frame_info *frame, const void *baton)
  109. {
  110. const int *reg_p = (const int *) baton;
  111. return value_of_register (*reg_p, frame);
  112. }
  113. /* Information about a register alias that needs to be set up for this
  114. target. These are collected when the target's XML description is
  115. analysed, and then processed later, once the gdbarch has been created. */
  116. class riscv_pending_register_alias
  117. {
  118. public:
  119. /* Constructor. */
  120. riscv_pending_register_alias (const char *name, const void *baton)
  121. : m_name (name),
  122. m_baton (baton)
  123. { /* Nothing. */ }
  124. /* Convert this into a user register for GDBARCH. */
  125. void create (struct gdbarch *gdbarch) const
  126. {
  127. user_reg_add (gdbarch, m_name, value_of_riscv_user_reg, m_baton);
  128. }
  129. private:
  130. /* The name for this alias. */
  131. const char *m_name;
  132. /* The baton value for passing to user_reg_add. This must point to some
  133. data that will live for at least as long as the gdbarch object to
  134. which the user register is attached. */
  135. const void *m_baton;
  136. };
  137. /* A set of registers that we expect to find in a tdesc_feature. These
  138. are use in RISCV_GDBARCH_INIT when processing the target description. */
  139. struct riscv_register_feature
  140. {
  141. explicit riscv_register_feature (const char *feature_name)
  142. : m_feature_name (feature_name)
  143. { /* Delete. */ }
  144. riscv_register_feature () = delete;
  145. DISABLE_COPY_AND_ASSIGN (riscv_register_feature);
  146. /* Information for a single register. */
  147. struct register_info
  148. {
  149. /* The GDB register number for this register. */
  150. int regnum;
  151. /* List of names for this register. The first name in this list is the
  152. preferred name, the name GDB should use when describing this
  153. register. */
  154. std::vector<const char *> names;
  155. /* Look in FEATURE for a register with a name from this classes names
  156. list. If the register is found then register its number with
  157. TDESC_DATA and add all its aliases to the ALIASES list.
  158. PREFER_FIRST_NAME_P is used when deciding which aliases to create. */
  159. bool check (struct tdesc_arch_data *tdesc_data,
  160. const struct tdesc_feature *feature,
  161. bool prefer_first_name_p,
  162. std::vector<riscv_pending_register_alias> *aliases) const;
  163. };
  164. /* Return the name of this feature. */
  165. const char *name () const
  166. { return m_feature_name; }
  167. protected:
  168. /* Return a target description feature extracted from TDESC for this
  169. register feature. Will return nullptr if there is no feature in TDESC
  170. with the name M_FEATURE_NAME. */
  171. const struct tdesc_feature *tdesc_feature (const struct target_desc *tdesc) const
  172. {
  173. return tdesc_find_feature (tdesc, name ());
  174. }
  175. /* List of all the registers that we expect that we might find in this
  176. register set. */
  177. std::vector<struct register_info> m_registers;
  178. private:
  179. /* The name for this feature. This is the name used to find this feature
  180. within the target description. */
  181. const char *m_feature_name;
  182. };
  183. /* See description in the class declaration above. */
  184. bool
  185. riscv_register_feature::register_info::check
  186. (struct tdesc_arch_data *tdesc_data,
  187. const struct tdesc_feature *feature,
  188. bool prefer_first_name_p,
  189. std::vector<riscv_pending_register_alias> *aliases) const
  190. {
  191. for (const char *name : this->names)
  192. {
  193. bool found = tdesc_numbered_register (feature, tdesc_data,
  194. this->regnum, name);
  195. if (found)
  196. {
  197. /* We know that the target description mentions this
  198. register. In RISCV_REGISTER_NAME we ensure that GDB
  199. always uses the first name for each register, so here we
  200. add aliases for all of the remaining names. */
  201. int start_index = prefer_first_name_p ? 1 : 0;
  202. for (int i = start_index; i < this->names.size (); ++i)
  203. {
  204. const char *alias = this->names[i];
  205. if (alias == name && !prefer_first_name_p)
  206. continue;
  207. aliases->emplace_back (alias, (void *) &this->regnum);
  208. }
  209. return true;
  210. }
  211. }
  212. return false;
  213. }
  214. /* Class representing the x-registers feature set. */
  215. struct riscv_xreg_feature : public riscv_register_feature
  216. {
  217. riscv_xreg_feature ()
  218. : riscv_register_feature (riscv_feature_name_cpu)
  219. {
  220. m_registers = {
  221. { RISCV_ZERO_REGNUM + 0, { "zero", "x0" } },
  222. { RISCV_ZERO_REGNUM + 1, { "ra", "x1" } },
  223. { RISCV_ZERO_REGNUM + 2, { "sp", "x2" } },
  224. { RISCV_ZERO_REGNUM + 3, { "gp", "x3" } },
  225. { RISCV_ZERO_REGNUM + 4, { "tp", "x4" } },
  226. { RISCV_ZERO_REGNUM + 5, { "t0", "x5" } },
  227. { RISCV_ZERO_REGNUM + 6, { "t1", "x6" } },
  228. { RISCV_ZERO_REGNUM + 7, { "t2", "x7" } },
  229. { RISCV_ZERO_REGNUM + 8, { "fp", "x8", "s0" } },
  230. { RISCV_ZERO_REGNUM + 9, { "s1", "x9" } },
  231. { RISCV_ZERO_REGNUM + 10, { "a0", "x10" } },
  232. { RISCV_ZERO_REGNUM + 11, { "a1", "x11" } },
  233. { RISCV_ZERO_REGNUM + 12, { "a2", "x12" } },
  234. { RISCV_ZERO_REGNUM + 13, { "a3", "x13" } },
  235. { RISCV_ZERO_REGNUM + 14, { "a4", "x14" } },
  236. { RISCV_ZERO_REGNUM + 15, { "a5", "x15" } },
  237. { RISCV_ZERO_REGNUM + 16, { "a6", "x16" } },
  238. { RISCV_ZERO_REGNUM + 17, { "a7", "x17" } },
  239. { RISCV_ZERO_REGNUM + 18, { "s2", "x18" } },
  240. { RISCV_ZERO_REGNUM + 19, { "s3", "x19" } },
  241. { RISCV_ZERO_REGNUM + 20, { "s4", "x20" } },
  242. { RISCV_ZERO_REGNUM + 21, { "s5", "x21" } },
  243. { RISCV_ZERO_REGNUM + 22, { "s6", "x22" } },
  244. { RISCV_ZERO_REGNUM + 23, { "s7", "x23" } },
  245. { RISCV_ZERO_REGNUM + 24, { "s8", "x24" } },
  246. { RISCV_ZERO_REGNUM + 25, { "s9", "x25" } },
  247. { RISCV_ZERO_REGNUM + 26, { "s10", "x26" } },
  248. { RISCV_ZERO_REGNUM + 27, { "s11", "x27" } },
  249. { RISCV_ZERO_REGNUM + 28, { "t3", "x28" } },
  250. { RISCV_ZERO_REGNUM + 29, { "t4", "x29" } },
  251. { RISCV_ZERO_REGNUM + 30, { "t5", "x30" } },
  252. { RISCV_ZERO_REGNUM + 31, { "t6", "x31" } },
  253. { RISCV_ZERO_REGNUM + 32, { "pc" } }
  254. };
  255. }
  256. /* Return the preferred name for the register with gdb register number
  257. REGNUM, which must be in the inclusive range RISCV_ZERO_REGNUM to
  258. RISCV_PC_REGNUM. */
  259. const char *register_name (int regnum) const
  260. {
  261. gdb_assert (regnum >= RISCV_ZERO_REGNUM && regnum <= m_registers.size ());
  262. return m_registers[regnum].names[0];
  263. }
  264. /* Check this feature within TDESC, record the registers from this
  265. feature into TDESC_DATA and update ALIASES and FEATURES. */
  266. bool check (const struct target_desc *tdesc,
  267. struct tdesc_arch_data *tdesc_data,
  268. std::vector<riscv_pending_register_alias> *aliases,
  269. struct riscv_gdbarch_features *features) const
  270. {
  271. const struct tdesc_feature *feature_cpu = tdesc_feature (tdesc);
  272. if (feature_cpu == nullptr)
  273. return false;
  274. bool seen_an_optional_reg_p = false;
  275. for (const auto &reg : m_registers)
  276. {
  277. bool found = reg.check (tdesc_data, feature_cpu, true, aliases);
  278. bool is_optional_reg_p = (reg.regnum >= RISCV_ZERO_REGNUM + 16
  279. && reg.regnum < RISCV_ZERO_REGNUM + 32);
  280. if (!found && (!is_optional_reg_p || seen_an_optional_reg_p))
  281. return false;
  282. else if (found && is_optional_reg_p)
  283. seen_an_optional_reg_p = true;
  284. }
  285. /* Check that all of the core cpu registers have the same bitsize. */
  286. int xlen_bitsize = tdesc_register_bitsize (feature_cpu, "pc");
  287. bool valid_p = true;
  288. for (auto &tdesc_reg : feature_cpu->registers)
  289. valid_p &= (tdesc_reg->bitsize == xlen_bitsize);
  290. features->xlen = (xlen_bitsize / 8);
  291. features->embedded = !seen_an_optional_reg_p;
  292. return valid_p;
  293. }
  294. };
  295. /* An instance of the x-register feature set. */
  296. static const struct riscv_xreg_feature riscv_xreg_feature;
  297. /* Class representing the f-registers feature set. */
  298. struct riscv_freg_feature : public riscv_register_feature
  299. {
  300. riscv_freg_feature ()
  301. : riscv_register_feature (riscv_feature_name_fpu)
  302. {
  303. m_registers = {
  304. { RISCV_FIRST_FP_REGNUM + 0, { "ft0", "f0" } },
  305. { RISCV_FIRST_FP_REGNUM + 1, { "ft1", "f1" } },
  306. { RISCV_FIRST_FP_REGNUM + 2, { "ft2", "f2" } },
  307. { RISCV_FIRST_FP_REGNUM + 3, { "ft3", "f3" } },
  308. { RISCV_FIRST_FP_REGNUM + 4, { "ft4", "f4" } },
  309. { RISCV_FIRST_FP_REGNUM + 5, { "ft5", "f5" } },
  310. { RISCV_FIRST_FP_REGNUM + 6, { "ft6", "f6" } },
  311. { RISCV_FIRST_FP_REGNUM + 7, { "ft7", "f7" } },
  312. { RISCV_FIRST_FP_REGNUM + 8, { "fs0", "f8" } },
  313. { RISCV_FIRST_FP_REGNUM + 9, { "fs1", "f9" } },
  314. { RISCV_FIRST_FP_REGNUM + 10, { "fa0", "f10" } },
  315. { RISCV_FIRST_FP_REGNUM + 11, { "fa1", "f11" } },
  316. { RISCV_FIRST_FP_REGNUM + 12, { "fa2", "f12" } },
  317. { RISCV_FIRST_FP_REGNUM + 13, { "fa3", "f13" } },
  318. { RISCV_FIRST_FP_REGNUM + 14, { "fa4", "f14" } },
  319. { RISCV_FIRST_FP_REGNUM + 15, { "fa5", "f15" } },
  320. { RISCV_FIRST_FP_REGNUM + 16, { "fa6", "f16" } },
  321. { RISCV_FIRST_FP_REGNUM + 17, { "fa7", "f17" } },
  322. { RISCV_FIRST_FP_REGNUM + 18, { "fs2", "f18" } },
  323. { RISCV_FIRST_FP_REGNUM + 19, { "fs3", "f19" } },
  324. { RISCV_FIRST_FP_REGNUM + 20, { "fs4", "f20" } },
  325. { RISCV_FIRST_FP_REGNUM + 21, { "fs5", "f21" } },
  326. { RISCV_FIRST_FP_REGNUM + 22, { "fs6", "f22" } },
  327. { RISCV_FIRST_FP_REGNUM + 23, { "fs7", "f23" } },
  328. { RISCV_FIRST_FP_REGNUM + 24, { "fs8", "f24" } },
  329. { RISCV_FIRST_FP_REGNUM + 25, { "fs9", "f25" } },
  330. { RISCV_FIRST_FP_REGNUM + 26, { "fs10", "f26" } },
  331. { RISCV_FIRST_FP_REGNUM + 27, { "fs11", "f27" } },
  332. { RISCV_FIRST_FP_REGNUM + 28, { "ft8", "f28" } },
  333. { RISCV_FIRST_FP_REGNUM + 29, { "ft9", "f29" } },
  334. { RISCV_FIRST_FP_REGNUM + 30, { "ft10", "f30" } },
  335. { RISCV_FIRST_FP_REGNUM + 31, { "ft11", "f31" } },
  336. { RISCV_CSR_FFLAGS_REGNUM, { "fflags", "csr1" } },
  337. { RISCV_CSR_FRM_REGNUM, { "frm", "csr2" } },
  338. { RISCV_CSR_FCSR_REGNUM, { "fcsr", "csr3" } },
  339. };
  340. }
  341. /* Return the preferred name for the register with gdb register number
  342. REGNUM, which must be in the inclusive range RISCV_FIRST_FP_REGNUM to
  343. RISCV_LAST_FP_REGNUM. */
  344. const char *register_name (int regnum) const
  345. {
  346. gdb_static_assert (RISCV_LAST_FP_REGNUM == RISCV_FIRST_FP_REGNUM + 31);
  347. gdb_assert (regnum >= RISCV_FIRST_FP_REGNUM
  348. && regnum <= RISCV_LAST_FP_REGNUM);
  349. regnum -= RISCV_FIRST_FP_REGNUM;
  350. return m_registers[regnum].names[0];
  351. }
  352. /* Check this feature within TDESC, record the registers from this
  353. feature into TDESC_DATA and update ALIASES and FEATURES. */
  354. bool check (const struct target_desc *tdesc,
  355. struct tdesc_arch_data *tdesc_data,
  356. std::vector<riscv_pending_register_alias> *aliases,
  357. struct riscv_gdbarch_features *features) const
  358. {
  359. const struct tdesc_feature *feature_fpu = tdesc_feature (tdesc);
  360. /* It's fine if this feature is missing. Update the architecture
  361. feature set and return. */
  362. if (feature_fpu == nullptr)
  363. {
  364. features->flen = 0;
  365. return true;
  366. }
  367. /* Check all of the floating pointer registers are present. We also
  368. check that the floating point CSRs are present too, though if these
  369. are missing this is not fatal. */
  370. for (const auto &reg : m_registers)
  371. {
  372. bool found = reg.check (tdesc_data, feature_fpu, true, aliases);
  373. bool is_ctrl_reg_p = reg.regnum > RISCV_LAST_FP_REGNUM;
  374. if (!found && !is_ctrl_reg_p)
  375. return false;
  376. }
  377. /* Look through all of the floating point registers (not the FP CSRs
  378. though), and check they all have the same bitsize. Use this bitsize
  379. to update the feature set for this gdbarch. */
  380. int fp_bitsize = -1;
  381. for (const auto &reg : m_registers)
  382. {
  383. /* Stop once we get to the CSRs which are at the end of the
  384. M_REGISTERS list. */
  385. if (reg.regnum > RISCV_LAST_FP_REGNUM)
  386. break;
  387. int reg_bitsize = -1;
  388. for (const char *name : reg.names)
  389. {
  390. if (tdesc_unnumbered_register (feature_fpu, name))
  391. {
  392. reg_bitsize = tdesc_register_bitsize (feature_fpu, name);
  393. break;
  394. }
  395. }
  396. gdb_assert (reg_bitsize != -1);
  397. if (fp_bitsize == -1)
  398. fp_bitsize = reg_bitsize;
  399. else if (fp_bitsize != reg_bitsize)
  400. return false;
  401. }
  402. features->flen = (fp_bitsize / 8);
  403. return true;
  404. }
  405. };
  406. /* An instance of the f-register feature set. */
  407. static const struct riscv_freg_feature riscv_freg_feature;
  408. /* Class representing the virtual registers. These are not physical
  409. registers on the hardware, but might be available from the target.
  410. These are not pseudo registers, reading these really does result in a
  411. register read from the target, it is just that there might not be a
  412. physical register backing the result. */
  413. struct riscv_virtual_feature : public riscv_register_feature
  414. {
  415. riscv_virtual_feature ()
  416. : riscv_register_feature (riscv_feature_name_virtual)
  417. {
  418. m_registers = {
  419. { RISCV_PRIV_REGNUM, { "priv" } }
  420. };
  421. }
  422. bool check (const struct target_desc *tdesc,
  423. struct tdesc_arch_data *tdesc_data,
  424. std::vector<riscv_pending_register_alias> *aliases,
  425. struct riscv_gdbarch_features *features) const
  426. {
  427. const struct tdesc_feature *feature_virtual = tdesc_feature (tdesc);
  428. /* It's fine if this feature is missing. */
  429. if (feature_virtual == nullptr)
  430. return true;
  431. /* We don't check the return value from the call to check here, all the
  432. registers in this feature are optional. */
  433. for (const auto &reg : m_registers)
  434. reg.check (tdesc_data, feature_virtual, true, aliases);
  435. return true;
  436. }
  437. };
  438. /* An instance of the virtual register feature. */
  439. static const struct riscv_virtual_feature riscv_virtual_feature;
  440. /* Class representing the CSR feature. */
  441. struct riscv_csr_feature : public riscv_register_feature
  442. {
  443. riscv_csr_feature ()
  444. : riscv_register_feature (riscv_feature_name_csr)
  445. {
  446. m_registers = {
  447. #define DECLARE_CSR(NAME,VALUE,CLASS,DEFINE_VER,ABORT_VER) \
  448. { RISCV_ ## VALUE ## _REGNUM, { # NAME } },
  449. #include "opcode/riscv-opc.h"
  450. #undef DECLARE_CSR
  451. };
  452. riscv_create_csr_aliases ();
  453. }
  454. bool check (const struct target_desc *tdesc,
  455. struct tdesc_arch_data *tdesc_data,
  456. std::vector<riscv_pending_register_alias> *aliases,
  457. struct riscv_gdbarch_features *features) const
  458. {
  459. const struct tdesc_feature *feature_csr = tdesc_feature (tdesc);
  460. /* It's fine if this feature is missing. */
  461. if (feature_csr == nullptr)
  462. return true;
  463. /* We don't check the return value from the call to check here, all the
  464. registers in this feature are optional. */
  465. for (const auto &reg : m_registers)
  466. reg.check (tdesc_data, feature_csr, true, aliases);
  467. return true;
  468. }
  469. private:
  470. /* Complete RISCV_CSR_FEATURE, building the CSR alias names and adding them
  471. to the name list for each register. */
  472. void
  473. riscv_create_csr_aliases ()
  474. {
  475. for (auto &reg : m_registers)
  476. {
  477. int csr_num = reg.regnum - RISCV_FIRST_CSR_REGNUM;
  478. gdb::unique_xmalloc_ptr<char> alias = xstrprintf ("csr%d", csr_num);
  479. reg.names.push_back (alias.release ());
  480. }
  481. }
  482. };
  483. /* An instance of the csr register feature. */
  484. static const struct riscv_csr_feature riscv_csr_feature;
  485. /* Class representing the v-registers feature set. */
  486. struct riscv_vector_feature : public riscv_register_feature
  487. {
  488. riscv_vector_feature ()
  489. : riscv_register_feature (riscv_feature_name_vector)
  490. {
  491. m_registers = {
  492. { RISCV_V0_REGNUM + 0, { "v0" } },
  493. { RISCV_V0_REGNUM + 1, { "v1" } },
  494. { RISCV_V0_REGNUM + 2, { "v2" } },
  495. { RISCV_V0_REGNUM + 3, { "v3" } },
  496. { RISCV_V0_REGNUM + 4, { "v4" } },
  497. { RISCV_V0_REGNUM + 5, { "v5" } },
  498. { RISCV_V0_REGNUM + 6, { "v6" } },
  499. { RISCV_V0_REGNUM + 7, { "v7" } },
  500. { RISCV_V0_REGNUM + 8, { "v8" } },
  501. { RISCV_V0_REGNUM + 9, { "v9" } },
  502. { RISCV_V0_REGNUM + 10, { "v10" } },
  503. { RISCV_V0_REGNUM + 11, { "v11" } },
  504. { RISCV_V0_REGNUM + 12, { "v12" } },
  505. { RISCV_V0_REGNUM + 13, { "v13" } },
  506. { RISCV_V0_REGNUM + 14, { "v14" } },
  507. { RISCV_V0_REGNUM + 15, { "v15" } },
  508. { RISCV_V0_REGNUM + 16, { "v16" } },
  509. { RISCV_V0_REGNUM + 17, { "v17" } },
  510. { RISCV_V0_REGNUM + 18, { "v18" } },
  511. { RISCV_V0_REGNUM + 19, { "v19" } },
  512. { RISCV_V0_REGNUM + 20, { "v20" } },
  513. { RISCV_V0_REGNUM + 21, { "v21" } },
  514. { RISCV_V0_REGNUM + 22, { "v22" } },
  515. { RISCV_V0_REGNUM + 23, { "v23" } },
  516. { RISCV_V0_REGNUM + 24, { "v24" } },
  517. { RISCV_V0_REGNUM + 25, { "v25" } },
  518. { RISCV_V0_REGNUM + 26, { "v26" } },
  519. { RISCV_V0_REGNUM + 27, { "v27" } },
  520. { RISCV_V0_REGNUM + 28, { "v28" } },
  521. { RISCV_V0_REGNUM + 29, { "v29" } },
  522. { RISCV_V0_REGNUM + 30, { "v30" } },
  523. { RISCV_V0_REGNUM + 31, { "v31" } },
  524. };
  525. }
  526. /* Return the preferred name for the register with gdb register number
  527. REGNUM, which must be in the inclusive range RISCV_V0_REGNUM to
  528. RISCV_V0_REGNUM + 31. */
  529. const char *register_name (int regnum) const
  530. {
  531. gdb_assert (regnum >= RISCV_V0_REGNUM
  532. && regnum <= RISCV_V0_REGNUM + 31);
  533. regnum -= RISCV_V0_REGNUM;
  534. return m_registers[regnum].names[0];
  535. }
  536. /* Check this feature within TDESC, record the registers from this
  537. feature into TDESC_DATA and update ALIASES and FEATURES. */
  538. bool check (const struct target_desc *tdesc,
  539. struct tdesc_arch_data *tdesc_data,
  540. std::vector<riscv_pending_register_alias> *aliases,
  541. struct riscv_gdbarch_features *features) const
  542. {
  543. const struct tdesc_feature *feature_vector = tdesc_feature (tdesc);
  544. /* It's fine if this feature is missing. Update the architecture
  545. feature set and return. */
  546. if (feature_vector == nullptr)
  547. {
  548. features->vlen = 0;
  549. return true;
  550. }
  551. /* Check all of the vector registers are present. */
  552. for (const auto &reg : m_registers)
  553. {
  554. if (!reg.check (tdesc_data, feature_vector, true, aliases))
  555. return false;
  556. }
  557. /* Look through all of the vector registers and check they all have the
  558. same bitsize. Use this bitsize to update the feature set for this
  559. gdbarch. */
  560. int vector_bitsize = -1;
  561. for (const auto &reg : m_registers)
  562. {
  563. int reg_bitsize = -1;
  564. for (const char *name : reg.names)
  565. {
  566. if (tdesc_unnumbered_register (feature_vector, name))
  567. {
  568. reg_bitsize = tdesc_register_bitsize (feature_vector, name);
  569. break;
  570. }
  571. }
  572. gdb_assert (reg_bitsize != -1);
  573. if (vector_bitsize == -1)
  574. vector_bitsize = reg_bitsize;
  575. else if (vector_bitsize != reg_bitsize)
  576. return false;
  577. }
  578. features->vlen = (vector_bitsize / 8);
  579. return true;
  580. }
  581. };
  582. /* An instance of the v-register feature set. */
  583. static const struct riscv_vector_feature riscv_vector_feature;
  584. /* Controls whether we place compressed breakpoints or not. When in auto
  585. mode GDB tries to determine if the target supports compressed
  586. breakpoints, and uses them if it does. */
  587. static enum auto_boolean use_compressed_breakpoints;
  588. /* The show callback for 'show riscv use-compressed-breakpoints'. */
  589. static void
  590. show_use_compressed_breakpoints (struct ui_file *file, int from_tty,
  591. struct cmd_list_element *c,
  592. const char *value)
  593. {
  594. gdb_printf (file,
  595. _("Debugger's use of compressed breakpoints is set "
  596. "to %s.\n"), value);
  597. }
  598. /* The set and show lists for 'set riscv' and 'show riscv' prefixes. */
  599. static struct cmd_list_element *setriscvcmdlist = NULL;
  600. static struct cmd_list_element *showriscvcmdlist = NULL;
  601. /* The set and show lists for 'set riscv' and 'show riscv' prefixes. */
  602. static struct cmd_list_element *setdebugriscvcmdlist = NULL;
  603. static struct cmd_list_element *showdebugriscvcmdlist = NULL;
  604. /* The show callback for all 'show debug riscv VARNAME' variables. */
  605. static void
  606. show_riscv_debug_variable (struct ui_file *file, int from_tty,
  607. struct cmd_list_element *c,
  608. const char *value)
  609. {
  610. gdb_printf (file,
  611. _("RiscV debug variable `%s' is set to: %s\n"),
  612. c->name, value);
  613. }
  614. /* See riscv-tdep.h. */
  615. int
  616. riscv_isa_xlen (struct gdbarch *gdbarch)
  617. {
  618. riscv_gdbarch_tdep *tdep = (riscv_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  619. return tdep->isa_features.xlen;
  620. }
  621. /* See riscv-tdep.h. */
  622. int
  623. riscv_abi_xlen (struct gdbarch *gdbarch)
  624. {
  625. riscv_gdbarch_tdep *tdep = (riscv_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  626. return tdep->abi_features.xlen;
  627. }
  628. /* See riscv-tdep.h. */
  629. int
  630. riscv_isa_flen (struct gdbarch *gdbarch)
  631. {
  632. riscv_gdbarch_tdep *tdep = (riscv_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  633. return tdep->isa_features.flen;
  634. }
  635. /* See riscv-tdep.h. */
  636. int
  637. riscv_abi_flen (struct gdbarch *gdbarch)
  638. {
  639. riscv_gdbarch_tdep *tdep = (riscv_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  640. return tdep->abi_features.flen;
  641. }
  642. /* See riscv-tdep.h. */
  643. bool
  644. riscv_abi_embedded (struct gdbarch *gdbarch)
  645. {
  646. riscv_gdbarch_tdep *tdep = (riscv_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  647. return tdep->abi_features.embedded;
  648. }
  649. /* Return true if the target for GDBARCH has floating point hardware. */
  650. static bool
  651. riscv_has_fp_regs (struct gdbarch *gdbarch)
  652. {
  653. return (riscv_isa_flen (gdbarch) > 0);
  654. }
  655. /* Return true if GDBARCH is using any of the floating point hardware ABIs. */
  656. static bool
  657. riscv_has_fp_abi (struct gdbarch *gdbarch)
  658. {
  659. riscv_gdbarch_tdep *tdep = (riscv_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  660. return tdep->abi_features.flen > 0;
  661. }
  662. /* Return true if REGNO is a floating pointer register. */
  663. static bool
  664. riscv_is_fp_regno_p (int regno)
  665. {
  666. return (regno >= RISCV_FIRST_FP_REGNUM
  667. && regno <= RISCV_LAST_FP_REGNUM);
  668. }
  669. /* Implement the breakpoint_kind_from_pc gdbarch method. */
  670. static int
  671. riscv_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr)
  672. {
  673. if (use_compressed_breakpoints == AUTO_BOOLEAN_AUTO)
  674. {
  675. bool unaligned_p = false;
  676. gdb_byte buf[1];
  677. /* Some targets don't support unaligned reads. The address can only
  678. be unaligned if the C extension is supported. So it is safe to
  679. use a compressed breakpoint in this case. */
  680. if (*pcptr & 0x2)
  681. unaligned_p = true;
  682. else
  683. {
  684. /* Read the opcode byte to determine the instruction length. If
  685. the read fails this may be because we tried to set the
  686. breakpoint at an invalid address, in this case we provide a
  687. fake result which will give a breakpoint length of 4.
  688. Hopefully when we try to actually insert the breakpoint we
  689. will see a failure then too which will be reported to the
  690. user. */
  691. if (target_read_code (*pcptr, buf, 1) == -1)
  692. buf[0] = 0;
  693. }
  694. if (riscv_debug_breakpoints)
  695. {
  696. const char *bp = (unaligned_p || riscv_insn_length (buf[0]) == 2
  697. ? "C.EBREAK" : "EBREAK");
  698. gdb_printf (gdb_stdlog, "Using %s for breakpoint at %s ",
  699. bp, paddress (gdbarch, *pcptr));
  700. if (unaligned_p)
  701. gdb_printf (gdb_stdlog, "(unaligned address)\n");
  702. else
  703. gdb_printf (gdb_stdlog, "(instruction length %d)\n",
  704. riscv_insn_length (buf[0]));
  705. }
  706. if (unaligned_p || riscv_insn_length (buf[0]) == 2)
  707. return 2;
  708. else
  709. return 4;
  710. }
  711. else if (use_compressed_breakpoints == AUTO_BOOLEAN_TRUE)
  712. return 2;
  713. else
  714. return 4;
  715. }
  716. /* Implement the sw_breakpoint_from_kind gdbarch method. */
  717. static const gdb_byte *
  718. riscv_sw_breakpoint_from_kind (struct gdbarch *gdbarch, int kind, int *size)
  719. {
  720. static const gdb_byte ebreak[] = { 0x73, 0x00, 0x10, 0x00, };
  721. static const gdb_byte c_ebreak[] = { 0x02, 0x90 };
  722. *size = kind;
  723. switch (kind)
  724. {
  725. case 2:
  726. return c_ebreak;
  727. case 4:
  728. return ebreak;
  729. default:
  730. gdb_assert_not_reached ("unhandled breakpoint kind");
  731. }
  732. }
  733. /* Implement the register_name gdbarch method. This is used instead of
  734. the function supplied by calling TDESC_USE_REGISTERS so that we can
  735. ensure the preferred names are offered for x-regs and f-regs. */
  736. static const char *
  737. riscv_register_name (struct gdbarch *gdbarch, int regnum)
  738. {
  739. /* Lookup the name through the target description. If we get back NULL
  740. then this is an unknown register. If we do get a name back then we
  741. look up the registers preferred name below. */
  742. const char *name = tdesc_register_name (gdbarch, regnum);
  743. if (name == NULL || name[0] == '\0')
  744. return NULL;
  745. /* We want GDB to use the ABI names for registers even if the target
  746. gives us a target description with the architectural name. For
  747. example we want to see 'ra' instead of 'x1' whatever the target
  748. description called it. */
  749. if (regnum >= RISCV_ZERO_REGNUM && regnum < RISCV_FIRST_FP_REGNUM)
  750. return riscv_xreg_feature.register_name (regnum);
  751. /* Like with the x-regs we prefer the abi names for the floating point
  752. registers. */
  753. if (regnum >= RISCV_FIRST_FP_REGNUM && regnum <= RISCV_LAST_FP_REGNUM)
  754. {
  755. if (riscv_has_fp_regs (gdbarch))
  756. return riscv_freg_feature.register_name (regnum);
  757. else
  758. return NULL;
  759. }
  760. /* Some targets (QEMU) are reporting these three registers twice, once
  761. in the FPU feature, and once in the CSR feature. Both of these read
  762. the same underlying state inside the target, but naming the register
  763. twice in the target description results in GDB having two registers
  764. with the same name, only one of which can ever be accessed, but both
  765. will show up in 'info register all'. Unless, we identify the
  766. duplicate copies of these registers (in riscv_tdesc_unknown_reg) and
  767. then hide the registers here by giving them no name. */
  768. riscv_gdbarch_tdep *tdep = (riscv_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  769. if (tdep->duplicate_fflags_regnum == regnum)
  770. return NULL;
  771. if (tdep->duplicate_frm_regnum == regnum)
  772. return NULL;
  773. if (tdep->duplicate_fcsr_regnum == regnum)
  774. return NULL;
  775. /* The remaining registers are different. For all other registers on the
  776. machine we prefer to see the names that the target description
  777. provides. This is particularly important for CSRs which might be
  778. renamed over time. If GDB keeps track of the "latest" name, but a
  779. particular target provides an older name then we don't want to force
  780. users to see the newer name in register output.
  781. The other case that reaches here are any registers that the target
  782. provided that GDB is completely unaware of. For these we have no
  783. choice but to accept the target description name.
  784. Just accept whatever name TDESC_REGISTER_NAME returned. */
  785. return name;
  786. }
  787. /* Construct a type for 64-bit FP registers. */
  788. static struct type *
  789. riscv_fpreg_d_type (struct gdbarch *gdbarch)
  790. {
  791. riscv_gdbarch_tdep *tdep = (riscv_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  792. if (tdep->riscv_fpreg_d_type == nullptr)
  793. {
  794. const struct builtin_type *bt = builtin_type (gdbarch);
  795. /* The type we're building is this: */
  796. #if 0
  797. union __gdb_builtin_type_fpreg_d
  798. {
  799. float f;
  800. double d;
  801. };
  802. #endif
  803. struct type *t;
  804. t = arch_composite_type (gdbarch,
  805. "__gdb_builtin_type_fpreg_d", TYPE_CODE_UNION);
  806. append_composite_type_field (t, "float", bt->builtin_float);
  807. append_composite_type_field (t, "double", bt->builtin_double);
  808. t->set_is_vector (true);
  809. t->set_name ("builtin_type_fpreg_d");
  810. tdep->riscv_fpreg_d_type = t;
  811. }
  812. return tdep->riscv_fpreg_d_type;
  813. }
  814. /* Implement the register_type gdbarch method. This is installed as an
  815. for the override setup by TDESC_USE_REGISTERS, for most registers we
  816. delegate the type choice to the target description, but for a few
  817. registers we try to improve the types if the target description has
  818. taken a simplistic approach. */
  819. static struct type *
  820. riscv_register_type (struct gdbarch *gdbarch, int regnum)
  821. {
  822. struct type *type = tdesc_register_type (gdbarch, regnum);
  823. int xlen = riscv_isa_xlen (gdbarch);
  824. /* We want to perform some specific type "fixes" in cases where we feel
  825. that we really can do better than the target description. For all
  826. other cases we just return what the target description says. */
  827. if (riscv_is_fp_regno_p (regnum))
  828. {
  829. /* This spots the case for RV64 where the double is defined as
  830. either 'ieee_double' or 'float' (which is the generic name that
  831. converts to 'double' on 64-bit). In these cases its better to
  832. present the registers using a union type. */
  833. int flen = riscv_isa_flen (gdbarch);
  834. if (flen == 8
  835. && type->code () == TYPE_CODE_FLT
  836. && TYPE_LENGTH (type) == flen
  837. && (strcmp (type->name (), "builtin_type_ieee_double") == 0
  838. || strcmp (type->name (), "double") == 0))
  839. type = riscv_fpreg_d_type (gdbarch);
  840. }
  841. if ((regnum == gdbarch_pc_regnum (gdbarch)
  842. || regnum == RISCV_RA_REGNUM
  843. || regnum == RISCV_FP_REGNUM
  844. || regnum == RISCV_SP_REGNUM
  845. || regnum == RISCV_GP_REGNUM
  846. || regnum == RISCV_TP_REGNUM)
  847. && type->code () == TYPE_CODE_INT
  848. && TYPE_LENGTH (type) == xlen)
  849. {
  850. /* This spots the case where some interesting registers are defined
  851. as simple integers of the expected size, we force these registers
  852. to be pointers as we believe that is more useful. */
  853. if (regnum == gdbarch_pc_regnum (gdbarch)
  854. || regnum == RISCV_RA_REGNUM)
  855. type = builtin_type (gdbarch)->builtin_func_ptr;
  856. else if (regnum == RISCV_FP_REGNUM
  857. || regnum == RISCV_SP_REGNUM
  858. || regnum == RISCV_GP_REGNUM
  859. || regnum == RISCV_TP_REGNUM)
  860. type = builtin_type (gdbarch)->builtin_data_ptr;
  861. }
  862. return type;
  863. }
  864. /* Helper for riscv_print_registers_info, prints info for a single register
  865. REGNUM. */
  866. static void
  867. riscv_print_one_register_info (struct gdbarch *gdbarch,
  868. struct ui_file *file,
  869. struct frame_info *frame,
  870. int regnum)
  871. {
  872. const char *name = gdbarch_register_name (gdbarch, regnum);
  873. struct value *val;
  874. struct type *regtype;
  875. int print_raw_format;
  876. enum tab_stops { value_column_1 = 15 };
  877. gdb_puts (name, file);
  878. print_spaces (value_column_1 - strlen (name), file);
  879. try
  880. {
  881. val = value_of_register (regnum, frame);
  882. regtype = value_type (val);
  883. }
  884. catch (const gdb_exception_error &ex)
  885. {
  886. /* Handle failure to read a register without interrupting the entire
  887. 'info registers' flow. */
  888. gdb_printf (file, "%s\n", ex.what ());
  889. return;
  890. }
  891. print_raw_format = (value_entirely_available (val)
  892. && !value_optimized_out (val));
  893. if (regtype->code () == TYPE_CODE_FLT
  894. || (regtype->code () == TYPE_CODE_UNION
  895. && regtype->num_fields () == 2
  896. && regtype->field (0).type ()->code () == TYPE_CODE_FLT
  897. && regtype->field (1).type ()->code () == TYPE_CODE_FLT)
  898. || (regtype->code () == TYPE_CODE_UNION
  899. && regtype->num_fields () == 3
  900. && regtype->field (0).type ()->code () == TYPE_CODE_FLT
  901. && regtype->field (1).type ()->code () == TYPE_CODE_FLT
  902. && regtype->field (2).type ()->code () == TYPE_CODE_FLT))
  903. {
  904. struct value_print_options opts;
  905. const gdb_byte *valaddr = value_contents_for_printing (val).data ();
  906. enum bfd_endian byte_order = type_byte_order (regtype);
  907. get_user_print_options (&opts);
  908. opts.deref_ref = 1;
  909. common_val_print (val, file, 0, &opts, current_language);
  910. if (print_raw_format)
  911. {
  912. gdb_printf (file, "\t(raw ");
  913. print_hex_chars (file, valaddr, TYPE_LENGTH (regtype), byte_order,
  914. true);
  915. gdb_printf (file, ")");
  916. }
  917. }
  918. else
  919. {
  920. struct value_print_options opts;
  921. /* Print the register in hex. */
  922. get_formatted_print_options (&opts, 'x');
  923. opts.deref_ref = 1;
  924. common_val_print (val, file, 0, &opts, current_language);
  925. if (print_raw_format)
  926. {
  927. if (regnum == RISCV_CSR_MSTATUS_REGNUM)
  928. {
  929. LONGEST d;
  930. int size = register_size (gdbarch, regnum);
  931. unsigned xlen;
  932. /* The SD field is always in the upper bit of MSTATUS, regardless
  933. of the number of bits in MSTATUS. */
  934. d = value_as_long (val);
  935. xlen = size * 8;
  936. gdb_printf (file,
  937. "\tSD:%X VM:%02X MXR:%X PUM:%X MPRV:%X XS:%X "
  938. "FS:%X MPP:%x HPP:%X SPP:%X MPIE:%X HPIE:%X "
  939. "SPIE:%X UPIE:%X MIE:%X HIE:%X SIE:%X UIE:%X",
  940. (int) ((d >> (xlen - 1)) & 0x1),
  941. (int) ((d >> 24) & 0x1f),
  942. (int) ((d >> 19) & 0x1),
  943. (int) ((d >> 18) & 0x1),
  944. (int) ((d >> 17) & 0x1),
  945. (int) ((d >> 15) & 0x3),
  946. (int) ((d >> 13) & 0x3),
  947. (int) ((d >> 11) & 0x3),
  948. (int) ((d >> 9) & 0x3),
  949. (int) ((d >> 8) & 0x1),
  950. (int) ((d >> 7) & 0x1),
  951. (int) ((d >> 6) & 0x1),
  952. (int) ((d >> 5) & 0x1),
  953. (int) ((d >> 4) & 0x1),
  954. (int) ((d >> 3) & 0x1),
  955. (int) ((d >> 2) & 0x1),
  956. (int) ((d >> 1) & 0x1),
  957. (int) ((d >> 0) & 0x1));
  958. }
  959. else if (regnum == RISCV_CSR_MISA_REGNUM)
  960. {
  961. int base;
  962. unsigned xlen, i;
  963. LONGEST d;
  964. int size = register_size (gdbarch, regnum);
  965. /* The MXL field is always in the upper two bits of MISA,
  966. regardless of the number of bits in MISA. Mask out other
  967. bits to ensure we have a positive value. */
  968. d = value_as_long (val);
  969. base = (d >> ((size * 8) - 2)) & 0x3;
  970. xlen = 16;
  971. for (; base > 0; base--)
  972. xlen *= 2;
  973. gdb_printf (file, "\tRV%d", xlen);
  974. for (i = 0; i < 26; i++)
  975. {
  976. if (d & (1 << i))
  977. gdb_printf (file, "%c", 'A' + i);
  978. }
  979. }
  980. else if (regnum == RISCV_CSR_FCSR_REGNUM
  981. || regnum == RISCV_CSR_FFLAGS_REGNUM
  982. || regnum == RISCV_CSR_FRM_REGNUM)
  983. {
  984. LONGEST d;
  985. d = value_as_long (val);
  986. gdb_printf (file, "\t");
  987. if (regnum != RISCV_CSR_FRM_REGNUM)
  988. gdb_printf (file,
  989. "RD:%01X NV:%d DZ:%d OF:%d UF:%d NX:%d",
  990. (int) ((d >> 5) & 0x7),
  991. (int) ((d >> 4) & 0x1),
  992. (int) ((d >> 3) & 0x1),
  993. (int) ((d >> 2) & 0x1),
  994. (int) ((d >> 1) & 0x1),
  995. (int) ((d >> 0) & 0x1));
  996. if (regnum != RISCV_CSR_FFLAGS_REGNUM)
  997. {
  998. static const char * const sfrm[] =
  999. {
  1000. "RNE (round to nearest; ties to even)",
  1001. "RTZ (Round towards zero)",
  1002. "RDN (Round down towards -INF)",
  1003. "RUP (Round up towards +INF)",
  1004. "RMM (Round to nearest; ties to max magnitude)",
  1005. "INVALID[5]",
  1006. "INVALID[6]",
  1007. "dynamic rounding mode",
  1008. };
  1009. int frm = ((regnum == RISCV_CSR_FCSR_REGNUM)
  1010. ? (d >> 5) : d) & 0x3;
  1011. gdb_printf (file, "%sFRM:%i [%s]",
  1012. (regnum == RISCV_CSR_FCSR_REGNUM
  1013. ? " " : ""),
  1014. frm, sfrm[frm]);
  1015. }
  1016. }
  1017. else if (regnum == RISCV_PRIV_REGNUM)
  1018. {
  1019. LONGEST d;
  1020. uint8_t priv;
  1021. d = value_as_long (val);
  1022. priv = d & 0xff;
  1023. if (priv < 4)
  1024. {
  1025. static const char * const sprv[] =
  1026. {
  1027. "User/Application",
  1028. "Supervisor",
  1029. "Hypervisor",
  1030. "Machine"
  1031. };
  1032. gdb_printf (file, "\tprv:%d [%s]",
  1033. priv, sprv[priv]);
  1034. }
  1035. else
  1036. gdb_printf (file, "\tprv:%d [INVALID]", priv);
  1037. }
  1038. else
  1039. {
  1040. /* If not a vector register, print it also according to its
  1041. natural format. */
  1042. if (regtype->is_vector () == 0)
  1043. {
  1044. get_user_print_options (&opts);
  1045. opts.deref_ref = 1;
  1046. gdb_printf (file, "\t");
  1047. common_val_print (val, file, 0, &opts, current_language);
  1048. }
  1049. }
  1050. }
  1051. }
  1052. gdb_printf (file, "\n");
  1053. }
  1054. /* Return true if REGNUM is a valid CSR register. The CSR register space
  1055. is sparsely populated, so not every number is a named CSR. */
  1056. static bool
  1057. riscv_is_regnum_a_named_csr (int regnum)
  1058. {
  1059. gdb_assert (regnum >= RISCV_FIRST_CSR_REGNUM
  1060. && regnum <= RISCV_LAST_CSR_REGNUM);
  1061. switch (regnum)
  1062. {
  1063. #define DECLARE_CSR(name, num, class, define_ver, abort_ver) case RISCV_ ## num ## _REGNUM:
  1064. #include "opcode/riscv-opc.h"
  1065. #undef DECLARE_CSR
  1066. return true;
  1067. default:
  1068. return false;
  1069. }
  1070. }
  1071. /* Return true if REGNUM is an unknown CSR identified in
  1072. riscv_tdesc_unknown_reg for GDBARCH. */
  1073. static bool
  1074. riscv_is_unknown_csr (struct gdbarch *gdbarch, int regnum)
  1075. {
  1076. riscv_gdbarch_tdep *tdep = (riscv_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  1077. return (regnum >= tdep->unknown_csrs_first_regnum
  1078. && regnum < (tdep->unknown_csrs_first_regnum
  1079. + tdep->unknown_csrs_count));
  1080. }
  1081. /* Implement the register_reggroup_p gdbarch method. Is REGNUM a member
  1082. of REGGROUP? */
  1083. static int
  1084. riscv_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
  1085. const struct reggroup *reggroup)
  1086. {
  1087. /* Used by 'info registers' and 'info registers <groupname>'. */
  1088. if (gdbarch_register_name (gdbarch, regnum) == NULL
  1089. || gdbarch_register_name (gdbarch, regnum)[0] == '\0')
  1090. return 0;
  1091. if (regnum > RISCV_LAST_REGNUM)
  1092. {
  1093. /* Any extra registers from the CSR tdesc_feature (identified in
  1094. riscv_tdesc_unknown_reg) are removed from the save/restore groups
  1095. as some targets (QEMU) report CSRs which then can't be read and
  1096. having unreadable registers in the save/restore group breaks
  1097. things like inferior calls.
  1098. The unknown CSRs are also removed from the general group, and
  1099. added into both the csr and system group. This is inline with the
  1100. known CSRs (see below). */
  1101. if (riscv_is_unknown_csr (gdbarch, regnum))
  1102. {
  1103. if (reggroup == restore_reggroup || reggroup == save_reggroup
  1104. || reggroup == general_reggroup)
  1105. return 0;
  1106. else if (reggroup == system_reggroup || reggroup == csr_reggroup)
  1107. return 1;
  1108. }
  1109. /* This is some other unknown register from the target description.
  1110. In this case we trust whatever the target description says about
  1111. which groups this register should be in. */
  1112. int ret = tdesc_register_in_reggroup_p (gdbarch, regnum, reggroup);
  1113. if (ret != -1)
  1114. return ret;
  1115. return default_register_reggroup_p (gdbarch, regnum, reggroup);
  1116. }
  1117. if (reggroup == all_reggroup)
  1118. {
  1119. if (regnum < RISCV_FIRST_CSR_REGNUM || regnum >= RISCV_PRIV_REGNUM)
  1120. return 1;
  1121. if (riscv_is_regnum_a_named_csr (regnum))
  1122. return 1;
  1123. return 0;
  1124. }
  1125. else if (reggroup == float_reggroup)
  1126. return (riscv_is_fp_regno_p (regnum)
  1127. || regnum == RISCV_CSR_FCSR_REGNUM
  1128. || regnum == RISCV_CSR_FFLAGS_REGNUM
  1129. || regnum == RISCV_CSR_FRM_REGNUM);
  1130. else if (reggroup == general_reggroup)
  1131. return regnum < RISCV_FIRST_FP_REGNUM;
  1132. else if (reggroup == restore_reggroup || reggroup == save_reggroup)
  1133. {
  1134. if (riscv_has_fp_regs (gdbarch))
  1135. return (regnum <= RISCV_LAST_FP_REGNUM
  1136. || regnum == RISCV_CSR_FCSR_REGNUM
  1137. || regnum == RISCV_CSR_FFLAGS_REGNUM
  1138. || regnum == RISCV_CSR_FRM_REGNUM);
  1139. else
  1140. return regnum < RISCV_FIRST_FP_REGNUM;
  1141. }
  1142. else if (reggroup == system_reggroup || reggroup == csr_reggroup)
  1143. {
  1144. if (regnum == RISCV_PRIV_REGNUM)
  1145. return 1;
  1146. if (regnum < RISCV_FIRST_CSR_REGNUM || regnum > RISCV_LAST_CSR_REGNUM)
  1147. return 0;
  1148. if (riscv_is_regnum_a_named_csr (regnum))
  1149. return 1;
  1150. return 0;
  1151. }
  1152. else if (reggroup == vector_reggroup)
  1153. return (regnum >= RISCV_V0_REGNUM && regnum <= RISCV_V31_REGNUM);
  1154. else
  1155. return 0;
  1156. }
  1157. /* Implement the print_registers_info gdbarch method. This is used by
  1158. 'info registers' and 'info all-registers'. */
  1159. static void
  1160. riscv_print_registers_info (struct gdbarch *gdbarch,
  1161. struct ui_file *file,
  1162. struct frame_info *frame,
  1163. int regnum, int print_all)
  1164. {
  1165. if (regnum != -1)
  1166. {
  1167. /* Print one specified register. */
  1168. if (gdbarch_register_name (gdbarch, regnum) == NULL
  1169. || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
  1170. error (_("Not a valid register for the current processor type"));
  1171. riscv_print_one_register_info (gdbarch, file, frame, regnum);
  1172. }
  1173. else
  1174. {
  1175. const struct reggroup *reggroup;
  1176. if (print_all)
  1177. reggroup = all_reggroup;
  1178. else
  1179. reggroup = general_reggroup;
  1180. for (regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); ++regnum)
  1181. {
  1182. /* Zero never changes, so might as well hide by default. */
  1183. if (regnum == RISCV_ZERO_REGNUM && !print_all)
  1184. continue;
  1185. /* Registers with no name are not valid on this ISA. */
  1186. if (gdbarch_register_name (gdbarch, regnum) == NULL
  1187. || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
  1188. continue;
  1189. /* Is the register in the group we're interested in? */
  1190. if (!gdbarch_register_reggroup_p (gdbarch, regnum, reggroup))
  1191. continue;
  1192. riscv_print_one_register_info (gdbarch, file, frame, regnum);
  1193. }
  1194. }
  1195. }
  1196. /* Class that handles one decoded RiscV instruction. */
  1197. class riscv_insn
  1198. {
  1199. public:
  1200. /* Enum of all the opcodes that GDB cares about during the prologue scan. */
  1201. enum opcode
  1202. {
  1203. /* Unknown value is used at initialisation time. */
  1204. UNKNOWN = 0,
  1205. /* These instructions are all the ones we are interested in during the
  1206. prologue scan. */
  1207. ADD,
  1208. ADDI,
  1209. ADDIW,
  1210. ADDW,
  1211. AUIPC,
  1212. LUI,
  1213. SD,
  1214. SW,
  1215. LD,
  1216. LW,
  1217. MV,
  1218. /* These are needed for software breakpoint support. */
  1219. JAL,
  1220. JALR,
  1221. BEQ,
  1222. BNE,
  1223. BLT,
  1224. BGE,
  1225. BLTU,
  1226. BGEU,
  1227. /* These are needed for stepping over atomic sequences. */
  1228. LR,
  1229. SC,
  1230. /* This instruction is used to do a syscall. */
  1231. ECALL,
  1232. /* Other instructions are not interesting during the prologue scan, and
  1233. are ignored. */
  1234. OTHER
  1235. };
  1236. riscv_insn ()
  1237. : m_length (0),
  1238. m_opcode (OTHER),
  1239. m_rd (0),
  1240. m_rs1 (0),
  1241. m_rs2 (0)
  1242. {
  1243. /* Nothing. */
  1244. }
  1245. void decode (struct gdbarch *gdbarch, CORE_ADDR pc);
  1246. /* Get the length of the instruction in bytes. */
  1247. int length () const
  1248. { return m_length; }
  1249. /* Get the opcode for this instruction. */
  1250. enum opcode opcode () const
  1251. { return m_opcode; }
  1252. /* Get destination register field for this instruction. This is only
  1253. valid if the OPCODE implies there is such a field for this
  1254. instruction. */
  1255. int rd () const
  1256. { return m_rd; }
  1257. /* Get the RS1 register field for this instruction. This is only valid
  1258. if the OPCODE implies there is such a field for this instruction. */
  1259. int rs1 () const
  1260. { return m_rs1; }
  1261. /* Get the RS2 register field for this instruction. This is only valid
  1262. if the OPCODE implies there is such a field for this instruction. */
  1263. int rs2 () const
  1264. { return m_rs2; }
  1265. /* Get the immediate for this instruction in signed form. This is only
  1266. valid if the OPCODE implies there is such a field for this
  1267. instruction. */
  1268. int imm_signed () const
  1269. { return m_imm.s; }
  1270. private:
  1271. /* Extract 5 bit register field at OFFSET from instruction OPCODE. */
  1272. int decode_register_index (unsigned long opcode, int offset)
  1273. {
  1274. return (opcode >> offset) & 0x1F;
  1275. }
  1276. /* Extract 5 bit register field at OFFSET from instruction OPCODE. */
  1277. int decode_register_index_short (unsigned long opcode, int offset)
  1278. {
  1279. return ((opcode >> offset) & 0x7) + 8;
  1280. }
  1281. /* Helper for DECODE, decode 32-bit R-type instruction. */
  1282. void decode_r_type_insn (enum opcode opcode, ULONGEST ival)
  1283. {
  1284. m_opcode = opcode;
  1285. m_rd = decode_register_index (ival, OP_SH_RD);
  1286. m_rs1 = decode_register_index (ival, OP_SH_RS1);
  1287. m_rs2 = decode_register_index (ival, OP_SH_RS2);
  1288. }
  1289. /* Helper for DECODE, decode 16-bit compressed R-type instruction. */
  1290. void decode_cr_type_insn (enum opcode opcode, ULONGEST ival)
  1291. {
  1292. m_opcode = opcode;
  1293. m_rd = m_rs1 = decode_register_index (ival, OP_SH_CRS1S);
  1294. m_rs2 = decode_register_index (ival, OP_SH_CRS2);
  1295. }
  1296. /* Helper for DECODE, decode 32-bit I-type instruction. */
  1297. void decode_i_type_insn (enum opcode opcode, ULONGEST ival)
  1298. {
  1299. m_opcode = opcode;
  1300. m_rd = decode_register_index (ival, OP_SH_RD);
  1301. m_rs1 = decode_register_index (ival, OP_SH_RS1);
  1302. m_imm.s = EXTRACT_ITYPE_IMM (ival);
  1303. }
  1304. /* Helper for DECODE, decode 16-bit compressed I-type instruction. */
  1305. void decode_ci_type_insn (enum opcode opcode, ULONGEST ival)
  1306. {
  1307. m_opcode = opcode;
  1308. m_rd = m_rs1 = decode_register_index (ival, OP_SH_CRS1S);
  1309. m_imm.s = EXTRACT_CITYPE_IMM (ival);
  1310. }
  1311. /* Helper for DECODE, decode 16-bit compressed CL-type instruction. */
  1312. void decode_cl_type_insn (enum opcode opcode, ULONGEST ival)
  1313. {
  1314. m_opcode = opcode;
  1315. m_rd = decode_register_index_short (ival, OP_SH_CRS2S);
  1316. m_rs1 = decode_register_index_short (ival, OP_SH_CRS1S);
  1317. m_imm.s = EXTRACT_CLTYPE_IMM (ival);
  1318. }
  1319. /* Helper for DECODE, decode 32-bit S-type instruction. */
  1320. void decode_s_type_insn (enum opcode opcode, ULONGEST ival)
  1321. {
  1322. m_opcode = opcode;
  1323. m_rs1 = decode_register_index (ival, OP_SH_RS1);
  1324. m_rs2 = decode_register_index (ival, OP_SH_RS2);
  1325. m_imm.s = EXTRACT_STYPE_IMM (ival);
  1326. }
  1327. /* Helper for DECODE, decode 16-bit CS-type instruction. The immediate
  1328. encoding is different for each CS format instruction, so extracting
  1329. the immediate is left up to the caller, who should pass the extracted
  1330. immediate value through in IMM. */
  1331. void decode_cs_type_insn (enum opcode opcode, ULONGEST ival, int imm)
  1332. {
  1333. m_opcode = opcode;
  1334. m_imm.s = imm;
  1335. m_rs1 = decode_register_index_short (ival, OP_SH_CRS1S);
  1336. m_rs2 = decode_register_index_short (ival, OP_SH_CRS2S);
  1337. }
  1338. /* Helper for DECODE, decode 16-bit CSS-type instruction. The immediate
  1339. encoding is different for each CSS format instruction, so extracting
  1340. the immediate is left up to the caller, who should pass the extracted
  1341. immediate value through in IMM. */
  1342. void decode_css_type_insn (enum opcode opcode, ULONGEST ival, int imm)
  1343. {
  1344. m_opcode = opcode;
  1345. m_imm.s = imm;
  1346. m_rs1 = RISCV_SP_REGNUM;
  1347. /* Not a compressed register number in this case. */
  1348. m_rs2 = decode_register_index (ival, OP_SH_CRS2);
  1349. }
  1350. /* Helper for DECODE, decode 32-bit U-type instruction. */
  1351. void decode_u_type_insn (enum opcode opcode, ULONGEST ival)
  1352. {
  1353. m_opcode = opcode;
  1354. m_rd = decode_register_index (ival, OP_SH_RD);
  1355. m_imm.s = EXTRACT_UTYPE_IMM (ival);
  1356. }
  1357. /* Helper for DECODE, decode 32-bit J-type instruction. */
  1358. void decode_j_type_insn (enum opcode opcode, ULONGEST ival)
  1359. {
  1360. m_opcode = opcode;
  1361. m_rd = decode_register_index (ival, OP_SH_RD);
  1362. m_imm.s = EXTRACT_JTYPE_IMM (ival);
  1363. }
  1364. /* Helper for DECODE, decode 32-bit J-type instruction. */
  1365. void decode_cj_type_insn (enum opcode opcode, ULONGEST ival)
  1366. {
  1367. m_opcode = opcode;
  1368. m_imm.s = EXTRACT_CJTYPE_IMM (ival);
  1369. }
  1370. void decode_b_type_insn (enum opcode opcode, ULONGEST ival)
  1371. {
  1372. m_opcode = opcode;
  1373. m_rs1 = decode_register_index (ival, OP_SH_RS1);
  1374. m_rs2 = decode_register_index (ival, OP_SH_RS2);
  1375. m_imm.s = EXTRACT_BTYPE_IMM (ival);
  1376. }
  1377. void decode_cb_type_insn (enum opcode opcode, ULONGEST ival)
  1378. {
  1379. m_opcode = opcode;
  1380. m_rs1 = decode_register_index_short (ival, OP_SH_CRS1S);
  1381. m_imm.s = EXTRACT_CBTYPE_IMM (ival);
  1382. }
  1383. /* Fetch instruction from target memory at ADDR, return the content of
  1384. the instruction, and update LEN with the instruction length. */
  1385. static ULONGEST fetch_instruction (struct gdbarch *gdbarch,
  1386. CORE_ADDR addr, int *len);
  1387. /* The length of the instruction in bytes. Should be 2 or 4. */
  1388. int m_length;
  1389. /* The instruction opcode. */
  1390. enum opcode m_opcode;
  1391. /* The three possible registers an instruction might reference. Not
  1392. every instruction fills in all of these registers. Which fields are
  1393. valid depends on the opcode. The naming of these fields matches the
  1394. naming in the riscv isa manual. */
  1395. int m_rd;
  1396. int m_rs1;
  1397. int m_rs2;
  1398. /* Possible instruction immediate. This is only valid if the instruction
  1399. format contains an immediate, not all instruction, whether this is
  1400. valid depends on the opcode. Despite only having one format for now
  1401. the immediate is packed into a union, later instructions might require
  1402. an unsigned formatted immediate, having the union in place now will
  1403. reduce the need for code churn later. */
  1404. union riscv_insn_immediate
  1405. {
  1406. riscv_insn_immediate ()
  1407. : s (0)
  1408. {
  1409. /* Nothing. */
  1410. }
  1411. int s;
  1412. } m_imm;
  1413. };
  1414. /* Fetch instruction from target memory at ADDR, return the content of the
  1415. instruction, and update LEN with the instruction length. */
  1416. ULONGEST
  1417. riscv_insn::fetch_instruction (struct gdbarch *gdbarch,
  1418. CORE_ADDR addr, int *len)
  1419. {
  1420. enum bfd_endian byte_order = gdbarch_byte_order_for_code (gdbarch);
  1421. gdb_byte buf[8];
  1422. int instlen, status;
  1423. /* All insns are at least 16 bits. */
  1424. status = target_read_memory (addr, buf, 2);
  1425. if (status)
  1426. memory_error (TARGET_XFER_E_IO, addr);
  1427. /* If we need more, grab it now. */
  1428. instlen = riscv_insn_length (buf[0]);
  1429. gdb_assert (instlen <= sizeof (buf));
  1430. *len = instlen;
  1431. if (instlen > 2)
  1432. {
  1433. status = target_read_memory (addr + 2, buf + 2, instlen - 2);
  1434. if (status)
  1435. memory_error (TARGET_XFER_E_IO, addr + 2);
  1436. }
  1437. return extract_unsigned_integer (buf, instlen, byte_order);
  1438. }
  1439. /* Fetch from target memory an instruction at PC and decode it. This can
  1440. throw an error if the memory access fails, callers are responsible for
  1441. handling this error if that is appropriate. */
  1442. void
  1443. riscv_insn::decode (struct gdbarch *gdbarch, CORE_ADDR pc)
  1444. {
  1445. ULONGEST ival;
  1446. /* Fetch the instruction, and the instructions length. */
  1447. ival = fetch_instruction (gdbarch, pc, &m_length);
  1448. if (m_length == 4)
  1449. {
  1450. if (is_add_insn (ival))
  1451. decode_r_type_insn (ADD, ival);
  1452. else if (is_addw_insn (ival))
  1453. decode_r_type_insn (ADDW, ival);
  1454. else if (is_addi_insn (ival))
  1455. decode_i_type_insn (ADDI, ival);
  1456. else if (is_addiw_insn (ival))
  1457. decode_i_type_insn (ADDIW, ival);
  1458. else if (is_auipc_insn (ival))
  1459. decode_u_type_insn (AUIPC, ival);
  1460. else if (is_lui_insn (ival))
  1461. decode_u_type_insn (LUI, ival);
  1462. else if (is_sd_insn (ival))
  1463. decode_s_type_insn (SD, ival);
  1464. else if (is_sw_insn (ival))
  1465. decode_s_type_insn (SW, ival);
  1466. else if (is_jal_insn (ival))
  1467. decode_j_type_insn (JAL, ival);
  1468. else if (is_jalr_insn (ival))
  1469. decode_i_type_insn (JALR, ival);
  1470. else if (is_beq_insn (ival))
  1471. decode_b_type_insn (BEQ, ival);
  1472. else if (is_bne_insn (ival))
  1473. decode_b_type_insn (BNE, ival);
  1474. else if (is_blt_insn (ival))
  1475. decode_b_type_insn (BLT, ival);
  1476. else if (is_bge_insn (ival))
  1477. decode_b_type_insn (BGE, ival);
  1478. else if (is_bltu_insn (ival))
  1479. decode_b_type_insn (BLTU, ival);
  1480. else if (is_bgeu_insn (ival))
  1481. decode_b_type_insn (BGEU, ival);
  1482. else if (is_lr_w_insn (ival))
  1483. decode_r_type_insn (LR, ival);
  1484. else if (is_lr_d_insn (ival))
  1485. decode_r_type_insn (LR, ival);
  1486. else if (is_sc_w_insn (ival))
  1487. decode_r_type_insn (SC, ival);
  1488. else if (is_sc_d_insn (ival))
  1489. decode_r_type_insn (SC, ival);
  1490. else if (is_ecall_insn (ival))
  1491. decode_i_type_insn (ECALL, ival);
  1492. else if (is_ld_insn (ival))
  1493. decode_i_type_insn (LD, ival);
  1494. else if (is_lw_insn (ival))
  1495. decode_i_type_insn (LW, ival);
  1496. else
  1497. /* None of the other fields are valid in this case. */
  1498. m_opcode = OTHER;
  1499. }
  1500. else if (m_length == 2)
  1501. {
  1502. int xlen = riscv_isa_xlen (gdbarch);
  1503. /* C_ADD and C_JALR have the same opcode. If RS2 is 0, then this is a
  1504. C_JALR. So must try to match C_JALR first as it has more bits in
  1505. mask. */
  1506. if (is_c_jalr_insn (ival))
  1507. decode_cr_type_insn (JALR, ival);
  1508. else if (is_c_add_insn (ival))
  1509. decode_cr_type_insn (ADD, ival);
  1510. /* C_ADDW is RV64 and RV128 only. */
  1511. else if (xlen != 4 && is_c_addw_insn (ival))
  1512. decode_cr_type_insn (ADDW, ival);
  1513. else if (is_c_addi_insn (ival))
  1514. decode_ci_type_insn (ADDI, ival);
  1515. /* C_ADDIW and C_JAL have the same opcode. C_ADDIW is RV64 and RV128
  1516. only and C_JAL is RV32 only. */
  1517. else if (xlen != 4 && is_c_addiw_insn (ival))
  1518. decode_ci_type_insn (ADDIW, ival);
  1519. else if (xlen == 4 && is_c_jal_insn (ival))
  1520. decode_cj_type_insn (JAL, ival);
  1521. /* C_ADDI16SP and C_LUI have the same opcode. If RD is 2, then this is a
  1522. C_ADDI16SP. So must try to match C_ADDI16SP first as it has more bits
  1523. in mask. */
  1524. else if (is_c_addi16sp_insn (ival))
  1525. {
  1526. m_opcode = ADDI;
  1527. m_rd = m_rs1 = decode_register_index (ival, OP_SH_RD);
  1528. m_imm.s = EXTRACT_CITYPE_ADDI16SP_IMM (ival);
  1529. }
  1530. else if (is_c_addi4spn_insn (ival))
  1531. {
  1532. m_opcode = ADDI;
  1533. m_rd = decode_register_index_short (ival, OP_SH_CRS2S);
  1534. m_rs1 = RISCV_SP_REGNUM;
  1535. m_imm.s = EXTRACT_CIWTYPE_ADDI4SPN_IMM (ival);
  1536. }
  1537. else if (is_c_lui_insn (ival))
  1538. {
  1539. m_opcode = LUI;
  1540. m_rd = decode_register_index (ival, OP_SH_CRS1S);
  1541. m_imm.s = EXTRACT_CITYPE_LUI_IMM (ival);
  1542. }
  1543. /* C_SD and C_FSW have the same opcode. C_SD is RV64 and RV128 only,
  1544. and C_FSW is RV32 only. */
  1545. else if (xlen != 4 && is_c_sd_insn (ival))
  1546. decode_cs_type_insn (SD, ival, EXTRACT_CLTYPE_LD_IMM (ival));
  1547. else if (is_c_sw_insn (ival))
  1548. decode_cs_type_insn (SW, ival, EXTRACT_CLTYPE_LW_IMM (ival));
  1549. else if (is_c_swsp_insn (ival))
  1550. decode_css_type_insn (SW, ival, EXTRACT_CSSTYPE_SWSP_IMM (ival));
  1551. else if (xlen != 4 && is_c_sdsp_insn (ival))
  1552. decode_css_type_insn (SD, ival, EXTRACT_CSSTYPE_SDSP_IMM (ival));
  1553. /* C_JR and C_MV have the same opcode. If RS2 is 0, then this is a C_JR.
  1554. So must try to match C_JR first as it has more bits in mask. */
  1555. else if (is_c_jr_insn (ival))
  1556. decode_cr_type_insn (JALR, ival);
  1557. else if (is_c_mv_insn (ival))
  1558. decode_cr_type_insn (MV, ival);
  1559. else if (is_c_j_insn (ival))
  1560. decode_cj_type_insn (JAL, ival);
  1561. else if (is_c_beqz_insn (ival))
  1562. decode_cb_type_insn (BEQ, ival);
  1563. else if (is_c_bnez_insn (ival))
  1564. decode_cb_type_insn (BNE, ival);
  1565. else if (is_c_ld_insn (ival))
  1566. decode_cl_type_insn (LD, ival);
  1567. else if (is_c_lw_insn (ival))
  1568. decode_cl_type_insn (LW, ival);
  1569. else
  1570. /* None of the other fields of INSN are valid in this case. */
  1571. m_opcode = OTHER;
  1572. }
  1573. else
  1574. {
  1575. /* This must be a 6 or 8 byte instruction, we don't currently decode
  1576. any of these, so just ignore it. */
  1577. gdb_assert (m_length == 6 || m_length == 8);
  1578. m_opcode = OTHER;
  1579. }
  1580. }
  1581. /* The prologue scanner. This is currently only used for skipping the
  1582. prologue of a function when the DWARF information is not sufficient.
  1583. However, it is written with filling of the frame cache in mind, which
  1584. is why different groups of stack setup instructions are split apart
  1585. during the core of the inner loop. In the future, the intention is to
  1586. extend this function to fully support building up a frame cache that
  1587. can unwind register values when there is no DWARF information. */
  1588. static CORE_ADDR
  1589. riscv_scan_prologue (struct gdbarch *gdbarch,
  1590. CORE_ADDR start_pc, CORE_ADDR end_pc,
  1591. struct riscv_unwind_cache *cache)
  1592. {
  1593. CORE_ADDR cur_pc, next_pc, after_prologue_pc;
  1594. CORE_ADDR end_prologue_addr = 0;
  1595. /* Find an upper limit on the function prologue using the debug
  1596. information. If the debug information could not be used to provide
  1597. that bound, then use an arbitrary large number as the upper bound. */
  1598. after_prologue_pc = skip_prologue_using_sal (gdbarch, start_pc);
  1599. if (after_prologue_pc == 0)
  1600. after_prologue_pc = start_pc + 100; /* Arbitrary large number. */
  1601. if (after_prologue_pc < end_pc)
  1602. end_pc = after_prologue_pc;
  1603. pv_t regs[RISCV_NUM_INTEGER_REGS]; /* Number of GPR. */
  1604. for (int regno = 0; regno < RISCV_NUM_INTEGER_REGS; regno++)
  1605. regs[regno] = pv_register (regno, 0);
  1606. pv_area stack (RISCV_SP_REGNUM, gdbarch_addr_bit (gdbarch));
  1607. if (riscv_debug_unwinder)
  1608. gdb_printf
  1609. (gdb_stdlog,
  1610. "Prologue scan for function starting at %s (limit %s)\n",
  1611. core_addr_to_string (start_pc),
  1612. core_addr_to_string (end_pc));
  1613. for (next_pc = cur_pc = start_pc; cur_pc < end_pc; cur_pc = next_pc)
  1614. {
  1615. struct riscv_insn insn;
  1616. /* Decode the current instruction, and decide where the next
  1617. instruction lives based on the size of this instruction. */
  1618. insn.decode (gdbarch, cur_pc);
  1619. gdb_assert (insn.length () > 0);
  1620. next_pc = cur_pc + insn.length ();
  1621. /* Look for common stack adjustment insns. */
  1622. if ((insn.opcode () == riscv_insn::ADDI
  1623. || insn.opcode () == riscv_insn::ADDIW)
  1624. && insn.rd () == RISCV_SP_REGNUM
  1625. && insn.rs1 () == RISCV_SP_REGNUM)
  1626. {
  1627. /* Handle: addi sp, sp, -i
  1628. or: addiw sp, sp, -i */
  1629. gdb_assert (insn.rd () < RISCV_NUM_INTEGER_REGS);
  1630. gdb_assert (insn.rs1 () < RISCV_NUM_INTEGER_REGS);
  1631. regs[insn.rd ()]
  1632. = pv_add_constant (regs[insn.rs1 ()], insn.imm_signed ());
  1633. }
  1634. else if ((insn.opcode () == riscv_insn::SW
  1635. || insn.opcode () == riscv_insn::SD)
  1636. && (insn.rs1 () == RISCV_SP_REGNUM
  1637. || insn.rs1 () == RISCV_FP_REGNUM))
  1638. {
  1639. /* Handle: sw reg, offset(sp)
  1640. or: sd reg, offset(sp)
  1641. or: sw reg, offset(s0)
  1642. or: sd reg, offset(s0) */
  1643. /* Instruction storing a register onto the stack. */
  1644. gdb_assert (insn.rs1 () < RISCV_NUM_INTEGER_REGS);
  1645. gdb_assert (insn.rs2 () < RISCV_NUM_INTEGER_REGS);
  1646. stack.store (pv_add_constant (regs[insn.rs1 ()], insn.imm_signed ()),
  1647. (insn.opcode () == riscv_insn::SW ? 4 : 8),
  1648. regs[insn.rs2 ()]);
  1649. }
  1650. else if (insn.opcode () == riscv_insn::ADDI
  1651. && insn.rd () == RISCV_FP_REGNUM
  1652. && insn.rs1 () == RISCV_SP_REGNUM)
  1653. {
  1654. /* Handle: addi s0, sp, size */
  1655. /* Instructions setting up the frame pointer. */
  1656. gdb_assert (insn.rd () < RISCV_NUM_INTEGER_REGS);
  1657. gdb_assert (insn.rs1 () < RISCV_NUM_INTEGER_REGS);
  1658. regs[insn.rd ()]
  1659. = pv_add_constant (regs[insn.rs1 ()], insn.imm_signed ());
  1660. }
  1661. else if ((insn.opcode () == riscv_insn::ADD
  1662. || insn.opcode () == riscv_insn::ADDW)
  1663. && insn.rd () == RISCV_FP_REGNUM
  1664. && insn.rs1 () == RISCV_SP_REGNUM
  1665. && insn.rs2 () == RISCV_ZERO_REGNUM)
  1666. {
  1667. /* Handle: add s0, sp, 0
  1668. or: addw s0, sp, 0 */
  1669. /* Instructions setting up the frame pointer. */
  1670. gdb_assert (insn.rd () < RISCV_NUM_INTEGER_REGS);
  1671. gdb_assert (insn.rs1 () < RISCV_NUM_INTEGER_REGS);
  1672. regs[insn.rd ()] = pv_add_constant (regs[insn.rs1 ()], 0);
  1673. }
  1674. else if ((insn.opcode () == riscv_insn::ADDI
  1675. && insn.rd () == RISCV_ZERO_REGNUM
  1676. && insn.rs1 () == RISCV_ZERO_REGNUM
  1677. && insn.imm_signed () == 0))
  1678. {
  1679. /* Handle: add x0, x0, 0 (NOP) */
  1680. }
  1681. else if (insn.opcode () == riscv_insn::AUIPC)
  1682. {
  1683. gdb_assert (insn.rd () < RISCV_NUM_INTEGER_REGS);
  1684. regs[insn.rd ()] = pv_constant (cur_pc + insn.imm_signed ());
  1685. }
  1686. else if (insn.opcode () == riscv_insn::LUI)
  1687. {
  1688. /* Handle: lui REG, n
  1689. Where REG is not gp register. */
  1690. gdb_assert (insn.rd () < RISCV_NUM_INTEGER_REGS);
  1691. regs[insn.rd ()] = pv_constant (insn.imm_signed ());
  1692. }
  1693. else if (insn.opcode () == riscv_insn::ADDI)
  1694. {
  1695. /* Handle: addi REG1, REG2, IMM */
  1696. gdb_assert (insn.rd () < RISCV_NUM_INTEGER_REGS);
  1697. gdb_assert (insn.rs1 () < RISCV_NUM_INTEGER_REGS);
  1698. regs[insn.rd ()]
  1699. = pv_add_constant (regs[insn.rs1 ()], insn.imm_signed ());
  1700. }
  1701. else if (insn.opcode () == riscv_insn::ADD)
  1702. {
  1703. /* Handle: add REG1, REG2, REG3 */
  1704. gdb_assert (insn.rd () < RISCV_NUM_INTEGER_REGS);
  1705. gdb_assert (insn.rs1 () < RISCV_NUM_INTEGER_REGS);
  1706. gdb_assert (insn.rs2 () < RISCV_NUM_INTEGER_REGS);
  1707. regs[insn.rd ()] = pv_add (regs[insn.rs1 ()], regs[insn.rs2 ()]);
  1708. }
  1709. else if (insn.opcode () == riscv_insn::LD
  1710. || insn.opcode () == riscv_insn::LW)
  1711. {
  1712. /* Handle: ld reg, offset(rs1)
  1713. or: c.ld reg, offset(rs1)
  1714. or: lw reg, offset(rs1)
  1715. or: c.lw reg, offset(rs1) */
  1716. gdb_assert (insn.rd () < RISCV_NUM_INTEGER_REGS);
  1717. gdb_assert (insn.rs1 () < RISCV_NUM_INTEGER_REGS);
  1718. regs[insn.rd ()]
  1719. = stack.fetch (pv_add_constant (regs[insn.rs1 ()],
  1720. insn.imm_signed ()),
  1721. (insn.opcode () == riscv_insn::LW ? 4 : 8));
  1722. }
  1723. else if (insn.opcode () == riscv_insn::MV)
  1724. {
  1725. /* Handle: c.mv RD, RS2 */
  1726. gdb_assert (insn.rd () < RISCV_NUM_INTEGER_REGS);
  1727. gdb_assert (insn.rs2 () < RISCV_NUM_INTEGER_REGS);
  1728. gdb_assert (insn.rs2 () > 0);
  1729. regs[insn.rd ()] = regs[insn.rs2 ()];
  1730. }
  1731. else
  1732. {
  1733. end_prologue_addr = cur_pc;
  1734. break;
  1735. }
  1736. }
  1737. if (end_prologue_addr == 0)
  1738. end_prologue_addr = cur_pc;
  1739. if (riscv_debug_unwinder)
  1740. gdb_printf (gdb_stdlog, "End of prologue at %s\n",
  1741. core_addr_to_string (end_prologue_addr));
  1742. if (cache != NULL)
  1743. {
  1744. /* Figure out if it is a frame pointer or just a stack pointer. Also
  1745. the offset held in the pv_t is from the original register value to
  1746. the current value, which for a grows down stack means a negative
  1747. value. The FRAME_BASE_OFFSET is the negation of this, how to get
  1748. from the current value to the original value. */
  1749. if (pv_is_register (regs[RISCV_FP_REGNUM], RISCV_SP_REGNUM))
  1750. {
  1751. cache->frame_base_reg = RISCV_FP_REGNUM;
  1752. cache->frame_base_offset = -regs[RISCV_FP_REGNUM].k;
  1753. }
  1754. else
  1755. {
  1756. cache->frame_base_reg = RISCV_SP_REGNUM;
  1757. cache->frame_base_offset = -regs[RISCV_SP_REGNUM].k;
  1758. }
  1759. /* Assign offset from old SP to all saved registers. As we don't
  1760. have the previous value for the frame base register at this
  1761. point, we store the offset as the address in the trad_frame, and
  1762. then convert this to an actual address later. */
  1763. for (int i = 0; i <= RISCV_NUM_INTEGER_REGS; i++)
  1764. {
  1765. CORE_ADDR offset;
  1766. if (stack.find_reg (gdbarch, i, &offset))
  1767. {
  1768. if (riscv_debug_unwinder)
  1769. {
  1770. /* Display OFFSET as a signed value, the offsets are from
  1771. the frame base address to the registers location on
  1772. the stack, with a descending stack this means the
  1773. offsets are always negative. */
  1774. gdb_printf (gdb_stdlog,
  1775. "Register $%s at stack offset %s\n",
  1776. gdbarch_register_name (gdbarch, i),
  1777. plongest ((LONGEST) offset));
  1778. }
  1779. cache->regs[i].set_addr (offset);
  1780. }
  1781. }
  1782. }
  1783. return end_prologue_addr;
  1784. }
  1785. /* Implement the riscv_skip_prologue gdbarch method. */
  1786. static CORE_ADDR
  1787. riscv_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  1788. {
  1789. CORE_ADDR func_addr;
  1790. /* See if we can determine the end of the prologue via the symbol
  1791. table. If so, then return either PC, or the PC after the
  1792. prologue, whichever is greater. */
  1793. if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
  1794. {
  1795. CORE_ADDR post_prologue_pc
  1796. = skip_prologue_using_sal (gdbarch, func_addr);
  1797. if (post_prologue_pc != 0)
  1798. return std::max (pc, post_prologue_pc);
  1799. }
  1800. /* Can't determine prologue from the symbol table, need to examine
  1801. instructions. Pass -1 for the end address to indicate the prologue
  1802. scanner can scan as far as it needs to find the end of the prologue. */
  1803. return riscv_scan_prologue (gdbarch, pc, ((CORE_ADDR) -1), NULL);
  1804. }
  1805. /* Implement the gdbarch push dummy code callback. */
  1806. static CORE_ADDR
  1807. riscv_push_dummy_code (struct gdbarch *gdbarch, CORE_ADDR sp,
  1808. CORE_ADDR funaddr, struct value **args, int nargs,
  1809. struct type *value_type, CORE_ADDR *real_pc,
  1810. CORE_ADDR *bp_addr, struct regcache *regcache)
  1811. {
  1812. /* A nop instruction is 'add x0, x0, 0'. */
  1813. static const gdb_byte nop_insn[] = { 0x13, 0x00, 0x00, 0x00 };
  1814. /* Allocate space for a breakpoint, and keep the stack correctly
  1815. aligned. The space allocated here must be at least big enough to
  1816. accommodate the NOP_INSN defined above. */
  1817. sp -= 16;
  1818. *bp_addr = sp;
  1819. *real_pc = funaddr;
  1820. /* When we insert a breakpoint we select whether to use a compressed
  1821. breakpoint or not based on the existing contents of the memory.
  1822. If the breakpoint is being placed onto the stack as part of setting up
  1823. for an inferior call from GDB, then the existing stack contents may
  1824. randomly appear to be a compressed instruction, causing GDB to insert
  1825. a compressed breakpoint. If this happens on a target that does not
  1826. support compressed instructions then this could cause problems.
  1827. To prevent this issue we write an uncompressed nop onto the stack at
  1828. the location where the breakpoint will be inserted. In this way we
  1829. ensure that we always use an uncompressed breakpoint, which should
  1830. work on all targets.
  1831. We call TARGET_WRITE_MEMORY here so that if the write fails we don't
  1832. throw an exception. Instead we ignore the error and move on. The
  1833. assumption is that either GDB will error later when actually trying to
  1834. insert a software breakpoint, or GDB will use hardware breakpoints and
  1835. there will be no need to write to memory later. */
  1836. int status = target_write_memory (*bp_addr, nop_insn, sizeof (nop_insn));
  1837. if (riscv_debug_breakpoints || riscv_debug_infcall)
  1838. gdb_printf (gdb_stdlog,
  1839. "Writing %s-byte nop instruction to %s: %s\n",
  1840. plongest (sizeof (nop_insn)),
  1841. paddress (gdbarch, *bp_addr),
  1842. (status == 0 ? "success" : "failed"));
  1843. return sp;
  1844. }
  1845. /* Implement the gdbarch type alignment method, overrides the generic
  1846. alignment algorithm for anything that is RISC-V specific. */
  1847. static ULONGEST
  1848. riscv_type_align (gdbarch *gdbarch, type *type)
  1849. {
  1850. type = check_typedef (type);
  1851. if (type->code () == TYPE_CODE_ARRAY && type->is_vector ())
  1852. return std::min (TYPE_LENGTH (type), (ULONGEST) BIGGEST_ALIGNMENT);
  1853. /* Anything else will be aligned by the generic code. */
  1854. return 0;
  1855. }
  1856. /* Holds information about a single argument either being passed to an
  1857. inferior function, or returned from an inferior function. This includes
  1858. information about the size, type, etc of the argument, and also
  1859. information about how the argument will be passed (or returned). */
  1860. struct riscv_arg_info
  1861. {
  1862. /* Contents of the argument. */
  1863. const gdb_byte *contents;
  1864. /* Length of argument. */
  1865. int length;
  1866. /* Alignment required for an argument of this type. */
  1867. int align;
  1868. /* The type for this argument. */
  1869. struct type *type;
  1870. /* Each argument can have either 1 or 2 locations assigned to it. Each
  1871. location describes where part of the argument will be placed. The
  1872. second location is valid based on the LOC_TYPE and C_LENGTH fields
  1873. of the first location (which is always valid). */
  1874. struct location
  1875. {
  1876. /* What type of location this is. */
  1877. enum location_type
  1878. {
  1879. /* Argument passed in a register. */
  1880. in_reg,
  1881. /* Argument passed as an on stack argument. */
  1882. on_stack,
  1883. /* Argument passed by reference. The second location is always
  1884. valid for a BY_REF argument, and describes where the address
  1885. of the BY_REF argument should be placed. */
  1886. by_ref
  1887. } loc_type;
  1888. /* Information that depends on the location type. */
  1889. union
  1890. {
  1891. /* Which register number to use. */
  1892. int regno;
  1893. /* The offset into the stack region. */
  1894. int offset;
  1895. } loc_data;
  1896. /* The length of contents covered by this location. If this is less
  1897. than the total length of the argument, then the second location
  1898. will be valid, and will describe where the rest of the argument
  1899. will go. */
  1900. int c_length;
  1901. /* The offset within CONTENTS for this part of the argument. This can
  1902. be non-zero even for the first part (the first field of a struct can
  1903. have a non-zero offset due to padding). For the second part of the
  1904. argument, this might be the C_LENGTH value of the first part,
  1905. however, if we are passing a structure in two registers, and there's
  1906. is padding between the first and second field, then this offset
  1907. might be greater than the length of the first argument part. When
  1908. the second argument location is not holding part of the argument
  1909. value, but is instead holding the address of a reference argument,
  1910. then this offset will be set to 0. */
  1911. int c_offset;
  1912. } argloc[2];
  1913. /* TRUE if this is an unnamed argument. */
  1914. bool is_unnamed;
  1915. };
  1916. /* Information about a set of registers being used for passing arguments as
  1917. part of a function call. The register set must be numerically
  1918. sequential from NEXT_REGNUM to LAST_REGNUM. The register set can be
  1919. disabled from use by setting NEXT_REGNUM greater than LAST_REGNUM. */
  1920. struct riscv_arg_reg
  1921. {
  1922. riscv_arg_reg (int first, int last)
  1923. : next_regnum (first),
  1924. last_regnum (last)
  1925. {
  1926. /* Nothing. */
  1927. }
  1928. /* The GDB register number to use in this set. */
  1929. int next_regnum;
  1930. /* The last GDB register number to use in this set. */
  1931. int last_regnum;
  1932. };
  1933. /* Arguments can be passed as on stack arguments, or by reference. The
  1934. on stack arguments must be in a continuous region starting from $sp,
  1935. while the by reference arguments can be anywhere, but we'll put them
  1936. on the stack after (at higher address) the on stack arguments.
  1937. This might not be the right approach to take. The ABI is clear that
  1938. an argument passed by reference can be modified by the callee, which
  1939. us placing the argument (temporarily) onto the stack will not achieve
  1940. (changes will be lost). There's also the possibility that very large
  1941. arguments could overflow the stack.
  1942. This struct is used to track offset into these two areas for where
  1943. arguments are to be placed. */
  1944. struct riscv_memory_offsets
  1945. {
  1946. riscv_memory_offsets ()
  1947. : arg_offset (0),
  1948. ref_offset (0)
  1949. {
  1950. /* Nothing. */
  1951. }
  1952. /* Offset into on stack argument area. */
  1953. int arg_offset;
  1954. /* Offset into the pass by reference area. */
  1955. int ref_offset;
  1956. };
  1957. /* Holds information about where arguments to a call will be placed. This
  1958. is updated as arguments are added onto the call, and can be used to
  1959. figure out where the next argument should be placed. */
  1960. struct riscv_call_info
  1961. {
  1962. riscv_call_info (struct gdbarch *gdbarch)
  1963. : int_regs (RISCV_A0_REGNUM, RISCV_A0_REGNUM + 7),
  1964. float_regs (RISCV_FA0_REGNUM, RISCV_FA0_REGNUM + 7)
  1965. {
  1966. xlen = riscv_abi_xlen (gdbarch);
  1967. flen = riscv_abi_flen (gdbarch);
  1968. /* Reduce the number of integer argument registers when using the
  1969. embedded abi (i.e. rv32e). */
  1970. if (riscv_abi_embedded (gdbarch))
  1971. int_regs.last_regnum = RISCV_A0_REGNUM + 5;
  1972. /* Disable use of floating point registers if needed. */
  1973. if (!riscv_has_fp_abi (gdbarch))
  1974. float_regs.next_regnum = float_regs.last_regnum + 1;
  1975. }
  1976. /* Track the memory areas used for holding in-memory arguments to a
  1977. call. */
  1978. struct riscv_memory_offsets memory;
  1979. /* Holds information about the next integer register to use for passing
  1980. an argument. */
  1981. struct riscv_arg_reg int_regs;
  1982. /* Holds information about the next floating point register to use for
  1983. passing an argument. */
  1984. struct riscv_arg_reg float_regs;
  1985. /* The XLEN and FLEN are copied in to this structure for convenience, and
  1986. are just the results of calling RISCV_ABI_XLEN and RISCV_ABI_FLEN. */
  1987. int xlen;
  1988. int flen;
  1989. };
  1990. /* Return the number of registers available for use as parameters in the
  1991. register set REG. Returned value can be 0 or more. */
  1992. static int
  1993. riscv_arg_regs_available (struct riscv_arg_reg *reg)
  1994. {
  1995. if (reg->next_regnum > reg->last_regnum)
  1996. return 0;
  1997. return (reg->last_regnum - reg->next_regnum + 1);
  1998. }
  1999. /* If there is at least one register available in the register set REG then
  2000. the next register from REG is assigned to LOC and the length field of
  2001. LOC is updated to LENGTH. The register set REG is updated to indicate
  2002. that the assigned register is no longer available and the function
  2003. returns true.
  2004. If there are no registers available in REG then the function returns
  2005. false, and LOC and REG are unchanged. */
  2006. static bool
  2007. riscv_assign_reg_location (struct riscv_arg_info::location *loc,
  2008. struct riscv_arg_reg *reg,
  2009. int length, int offset)
  2010. {
  2011. if (reg->next_regnum <= reg->last_regnum)
  2012. {
  2013. loc->loc_type = riscv_arg_info::location::in_reg;
  2014. loc->loc_data.regno = reg->next_regnum;
  2015. reg->next_regnum++;
  2016. loc->c_length = length;
  2017. loc->c_offset = offset;
  2018. return true;
  2019. }
  2020. return false;
  2021. }
  2022. /* Assign LOC a location as the next stack parameter, and update MEMORY to
  2023. record that an area of stack has been used to hold the parameter
  2024. described by LOC.
  2025. The length field of LOC is updated to LENGTH, the length of the
  2026. parameter being stored, and ALIGN is the alignment required by the
  2027. parameter, which will affect how memory is allocated out of MEMORY. */
  2028. static void
  2029. riscv_assign_stack_location (struct riscv_arg_info::location *loc,
  2030. struct riscv_memory_offsets *memory,
  2031. int length, int align)
  2032. {
  2033. loc->loc_type = riscv_arg_info::location::on_stack;
  2034. memory->arg_offset
  2035. = align_up (memory->arg_offset, align);
  2036. loc->loc_data.offset = memory->arg_offset;
  2037. memory->arg_offset += length;
  2038. loc->c_length = length;
  2039. /* Offset is always 0, either we're the first location part, in which
  2040. case we're reading content from the start of the argument, or we're
  2041. passing the address of a reference argument, so 0. */
  2042. loc->c_offset = 0;
  2043. }
  2044. /* Update AINFO, which describes an argument that should be passed or
  2045. returned using the integer ABI. The argloc fields within AINFO are
  2046. updated to describe the location in which the argument will be passed to
  2047. a function, or returned from a function.
  2048. The CINFO structure contains the ongoing call information, the holds
  2049. information such as which argument registers are remaining to be
  2050. assigned to parameter, and how much memory has been used by parameters
  2051. so far.
  2052. By examining the state of CINFO a suitable location can be selected,
  2053. and assigned to AINFO. */
  2054. static void
  2055. riscv_call_arg_scalar_int (struct riscv_arg_info *ainfo,
  2056. struct riscv_call_info *cinfo)
  2057. {
  2058. if (ainfo->length > (2 * cinfo->xlen))
  2059. {
  2060. /* Argument is going to be passed by reference. */
  2061. ainfo->argloc[0].loc_type
  2062. = riscv_arg_info::location::by_ref;
  2063. cinfo->memory.ref_offset
  2064. = align_up (cinfo->memory.ref_offset, ainfo->align);
  2065. ainfo->argloc[0].loc_data.offset = cinfo->memory.ref_offset;
  2066. cinfo->memory.ref_offset += ainfo->length;
  2067. ainfo->argloc[0].c_length = ainfo->length;
  2068. /* The second location for this argument is given over to holding the
  2069. address of the by-reference data. Pass 0 for the offset as this
  2070. is not part of the actual argument value. */
  2071. if (!riscv_assign_reg_location (&ainfo->argloc[1],
  2072. &cinfo->int_regs,
  2073. cinfo->xlen, 0))
  2074. riscv_assign_stack_location (&ainfo->argloc[1],
  2075. &cinfo->memory, cinfo->xlen,
  2076. cinfo->xlen);
  2077. }
  2078. else
  2079. {
  2080. int len = std::min (ainfo->length, cinfo->xlen);
  2081. int align = std::max (ainfo->align, cinfo->xlen);
  2082. /* Unnamed arguments in registers that require 2*XLEN alignment are
  2083. passed in an aligned register pair. */
  2084. if (ainfo->is_unnamed && (align == cinfo->xlen * 2)
  2085. && cinfo->int_regs.next_regnum & 1)
  2086. cinfo->int_regs.next_regnum++;
  2087. if (!riscv_assign_reg_location (&ainfo->argloc[0],
  2088. &cinfo->int_regs, len, 0))
  2089. riscv_assign_stack_location (&ainfo->argloc[0],
  2090. &cinfo->memory, len, align);
  2091. if (len < ainfo->length)
  2092. {
  2093. len = ainfo->length - len;
  2094. if (!riscv_assign_reg_location (&ainfo->argloc[1],
  2095. &cinfo->int_regs, len,
  2096. cinfo->xlen))
  2097. riscv_assign_stack_location (&ainfo->argloc[1],
  2098. &cinfo->memory, len, cinfo->xlen);
  2099. }
  2100. }
  2101. }
  2102. /* Like RISCV_CALL_ARG_SCALAR_INT, except the argument described by AINFO
  2103. is being passed with the floating point ABI. */
  2104. static void
  2105. riscv_call_arg_scalar_float (struct riscv_arg_info *ainfo,
  2106. struct riscv_call_info *cinfo)
  2107. {
  2108. if (ainfo->length > cinfo->flen || ainfo->is_unnamed)
  2109. return riscv_call_arg_scalar_int (ainfo, cinfo);
  2110. else
  2111. {
  2112. if (!riscv_assign_reg_location (&ainfo->argloc[0],
  2113. &cinfo->float_regs,
  2114. ainfo->length, 0))
  2115. return riscv_call_arg_scalar_int (ainfo, cinfo);
  2116. }
  2117. }
  2118. /* Like RISCV_CALL_ARG_SCALAR_INT, except the argument described by AINFO
  2119. is a complex floating point argument, and is therefore handled
  2120. differently to other argument types. */
  2121. static void
  2122. riscv_call_arg_complex_float (struct riscv_arg_info *ainfo,
  2123. struct riscv_call_info *cinfo)
  2124. {
  2125. if (ainfo->length <= (2 * cinfo->flen)
  2126. && riscv_arg_regs_available (&cinfo->float_regs) >= 2
  2127. && !ainfo->is_unnamed)
  2128. {
  2129. bool result;
  2130. int len = ainfo->length / 2;
  2131. result = riscv_assign_reg_location (&ainfo->argloc[0],
  2132. &cinfo->float_regs, len, 0);
  2133. gdb_assert (result);
  2134. result = riscv_assign_reg_location (&ainfo->argloc[1],
  2135. &cinfo->float_regs, len, len);
  2136. gdb_assert (result);
  2137. }
  2138. else
  2139. return riscv_call_arg_scalar_int (ainfo, cinfo);
  2140. }
  2141. /* A structure used for holding information about a structure type within
  2142. the inferior program. The RiscV ABI has special rules for handling some
  2143. structures with a single field or with two fields. The counting of
  2144. fields here is done after flattening out all nested structures. */
  2145. class riscv_struct_info
  2146. {
  2147. public:
  2148. riscv_struct_info ()
  2149. : m_number_of_fields (0),
  2150. m_types { nullptr, nullptr },
  2151. m_offsets { 0, 0 }
  2152. {
  2153. /* Nothing. */
  2154. }
  2155. /* Analyse TYPE descending into nested structures, count the number of
  2156. scalar fields and record the types of the first two fields found. */
  2157. void analyse (struct type *type)
  2158. {
  2159. analyse_inner (type, 0);
  2160. }
  2161. /* The number of scalar fields found in the analysed type. This is
  2162. currently only accurate if the value returned is 0, 1, or 2 as the
  2163. analysis stops counting when the number of fields is 3. This is
  2164. because the RiscV ABI only has special cases for 1 or 2 fields,
  2165. anything else we just don't care about. */
  2166. int number_of_fields () const
  2167. { return m_number_of_fields; }
  2168. /* Return the type for scalar field INDEX within the analysed type. Will
  2169. return nullptr if there is no field at that index. Only INDEX values
  2170. 0 and 1 can be requested as the RiscV ABI only has special cases for
  2171. structures with 1 or 2 fields. */
  2172. struct type *field_type (int index) const
  2173. {
  2174. gdb_assert (index < (sizeof (m_types) / sizeof (m_types[0])));
  2175. return m_types[index];
  2176. }
  2177. /* Return the offset of scalar field INDEX within the analysed type. Will
  2178. return 0 if there is no field at that index. Only INDEX values 0 and
  2179. 1 can be requested as the RiscV ABI only has special cases for
  2180. structures with 1 or 2 fields. */
  2181. int field_offset (int index) const
  2182. {
  2183. gdb_assert (index < (sizeof (m_offsets) / sizeof (m_offsets[0])));
  2184. return m_offsets[index];
  2185. }
  2186. private:
  2187. /* The number of scalar fields found within the structure after recursing
  2188. into nested structures. */
  2189. int m_number_of_fields;
  2190. /* The types of the first two scalar fields found within the structure
  2191. after recursing into nested structures. */
  2192. struct type *m_types[2];
  2193. /* The offsets of the first two scalar fields found within the structure
  2194. after recursing into nested structures. */
  2195. int m_offsets[2];
  2196. /* Recursive core for ANALYSE, the OFFSET parameter tracks the byte
  2197. offset from the start of the top level structure being analysed. */
  2198. void analyse_inner (struct type *type, int offset);
  2199. };
  2200. /* See description in class declaration. */
  2201. void
  2202. riscv_struct_info::analyse_inner (struct type *type, int offset)
  2203. {
  2204. unsigned int count = type->num_fields ();
  2205. unsigned int i;
  2206. for (i = 0; i < count; ++i)
  2207. {
  2208. if (type->field (i).loc_kind () != FIELD_LOC_KIND_BITPOS)
  2209. continue;
  2210. struct type *field_type = type->field (i).type ();
  2211. field_type = check_typedef (field_type);
  2212. int field_offset
  2213. = offset + type->field (i).loc_bitpos () / TARGET_CHAR_BIT;
  2214. switch (field_type->code ())
  2215. {
  2216. case TYPE_CODE_STRUCT:
  2217. analyse_inner (field_type, field_offset);
  2218. break;
  2219. default:
  2220. /* RiscV only flattens out structures. Anything else does not
  2221. need to be flattened, we just record the type, and when we
  2222. look at the analysis results we'll realise this is not a
  2223. structure we can special case, and pass the structure in
  2224. memory. */
  2225. if (m_number_of_fields < 2)
  2226. {
  2227. m_types[m_number_of_fields] = field_type;
  2228. m_offsets[m_number_of_fields] = field_offset;
  2229. }
  2230. m_number_of_fields++;
  2231. break;
  2232. }
  2233. /* RiscV only has special handling for structures with 1 or 2 scalar
  2234. fields, any more than that and the structure is just passed in
  2235. memory. We can safely drop out early when we find 3 or more
  2236. fields then. */
  2237. if (m_number_of_fields > 2)
  2238. return;
  2239. }
  2240. }
  2241. /* Like RISCV_CALL_ARG_SCALAR_INT, except the argument described by AINFO
  2242. is a structure. Small structures on RiscV have some special case
  2243. handling in order that the structure might be passed in register.
  2244. Larger structures are passed in memory. After assigning location
  2245. information to AINFO, CINFO will have been updated. */
  2246. static void
  2247. riscv_call_arg_struct (struct riscv_arg_info *ainfo,
  2248. struct riscv_call_info *cinfo)
  2249. {
  2250. if (riscv_arg_regs_available (&cinfo->float_regs) >= 1)
  2251. {
  2252. struct riscv_struct_info sinfo;
  2253. sinfo.analyse (ainfo->type);
  2254. if (sinfo.number_of_fields () == 1
  2255. && sinfo.field_type(0)->code () == TYPE_CODE_COMPLEX)
  2256. {
  2257. /* The following is similar to RISCV_CALL_ARG_COMPLEX_FLOAT,
  2258. except we use the type of the complex field instead of the
  2259. type from AINFO, and the first location might be at a non-zero
  2260. offset. */
  2261. if (TYPE_LENGTH (sinfo.field_type (0)) <= (2 * cinfo->flen)
  2262. && riscv_arg_regs_available (&cinfo->float_regs) >= 2
  2263. && !ainfo->is_unnamed)
  2264. {
  2265. bool result;
  2266. int len = TYPE_LENGTH (sinfo.field_type (0)) / 2;
  2267. int offset = sinfo.field_offset (0);
  2268. result = riscv_assign_reg_location (&ainfo->argloc[0],
  2269. &cinfo->float_regs, len,
  2270. offset);
  2271. gdb_assert (result);
  2272. result = riscv_assign_reg_location (&ainfo->argloc[1],
  2273. &cinfo->float_regs, len,
  2274. (offset + len));
  2275. gdb_assert (result);
  2276. }
  2277. else
  2278. riscv_call_arg_scalar_int (ainfo, cinfo);
  2279. return;
  2280. }
  2281. if (sinfo.number_of_fields () == 1
  2282. && sinfo.field_type(0)->code () == TYPE_CODE_FLT)
  2283. {
  2284. /* The following is similar to RISCV_CALL_ARG_SCALAR_FLOAT,
  2285. except we use the type of the first scalar field instead of
  2286. the type from AINFO. Also the location might be at a non-zero
  2287. offset. */
  2288. if (TYPE_LENGTH (sinfo.field_type (0)) > cinfo->flen
  2289. || ainfo->is_unnamed)
  2290. riscv_call_arg_scalar_int (ainfo, cinfo);
  2291. else
  2292. {
  2293. int offset = sinfo.field_offset (0);
  2294. int len = TYPE_LENGTH (sinfo.field_type (0));
  2295. if (!riscv_assign_reg_location (&ainfo->argloc[0],
  2296. &cinfo->float_regs,
  2297. len, offset))
  2298. riscv_call_arg_scalar_int (ainfo, cinfo);
  2299. }
  2300. return;
  2301. }
  2302. if (sinfo.number_of_fields () == 2
  2303. && sinfo.field_type(0)->code () == TYPE_CODE_FLT
  2304. && TYPE_LENGTH (sinfo.field_type (0)) <= cinfo->flen
  2305. && sinfo.field_type(1)->code () == TYPE_CODE_FLT
  2306. && TYPE_LENGTH (sinfo.field_type (1)) <= cinfo->flen
  2307. && riscv_arg_regs_available (&cinfo->float_regs) >= 2)
  2308. {
  2309. int len0 = TYPE_LENGTH (sinfo.field_type (0));
  2310. int offset = sinfo.field_offset (0);
  2311. if (!riscv_assign_reg_location (&ainfo->argloc[0],
  2312. &cinfo->float_regs, len0, offset))
  2313. error (_("failed during argument setup"));
  2314. int len1 = TYPE_LENGTH (sinfo.field_type (1));
  2315. offset = sinfo.field_offset (1);
  2316. gdb_assert (len1 <= (TYPE_LENGTH (ainfo->type)
  2317. - TYPE_LENGTH (sinfo.field_type (0))));
  2318. if (!riscv_assign_reg_location (&ainfo->argloc[1],
  2319. &cinfo->float_regs,
  2320. len1, offset))
  2321. error (_("failed during argument setup"));
  2322. return;
  2323. }
  2324. if (sinfo.number_of_fields () == 2
  2325. && riscv_arg_regs_available (&cinfo->int_regs) >= 1
  2326. && (sinfo.field_type(0)->code () == TYPE_CODE_FLT
  2327. && TYPE_LENGTH (sinfo.field_type (0)) <= cinfo->flen
  2328. && is_integral_type (sinfo.field_type (1))
  2329. && TYPE_LENGTH (sinfo.field_type (1)) <= cinfo->xlen))
  2330. {
  2331. int len0 = TYPE_LENGTH (sinfo.field_type (0));
  2332. int offset = sinfo.field_offset (0);
  2333. if (!riscv_assign_reg_location (&ainfo->argloc[0],
  2334. &cinfo->float_regs, len0, offset))
  2335. error (_("failed during argument setup"));
  2336. int len1 = TYPE_LENGTH (sinfo.field_type (1));
  2337. offset = sinfo.field_offset (1);
  2338. gdb_assert (len1 <= cinfo->xlen);
  2339. if (!riscv_assign_reg_location (&ainfo->argloc[1],
  2340. &cinfo->int_regs, len1, offset))
  2341. error (_("failed during argument setup"));
  2342. return;
  2343. }
  2344. if (sinfo.number_of_fields () == 2
  2345. && riscv_arg_regs_available (&cinfo->int_regs) >= 1
  2346. && (is_integral_type (sinfo.field_type (0))
  2347. && TYPE_LENGTH (sinfo.field_type (0)) <= cinfo->xlen
  2348. && sinfo.field_type(1)->code () == TYPE_CODE_FLT
  2349. && TYPE_LENGTH (sinfo.field_type (1)) <= cinfo->flen))
  2350. {
  2351. int len0 = TYPE_LENGTH (sinfo.field_type (0));
  2352. int len1 = TYPE_LENGTH (sinfo.field_type (1));
  2353. gdb_assert (len0 <= cinfo->xlen);
  2354. gdb_assert (len1 <= cinfo->flen);
  2355. int offset = sinfo.field_offset (0);
  2356. if (!riscv_assign_reg_location (&ainfo->argloc[0],
  2357. &cinfo->int_regs, len0, offset))
  2358. error (_("failed during argument setup"));
  2359. offset = sinfo.field_offset (1);
  2360. if (!riscv_assign_reg_location (&ainfo->argloc[1],
  2361. &cinfo->float_regs,
  2362. len1, offset))
  2363. error (_("failed during argument setup"));
  2364. return;
  2365. }
  2366. }
  2367. /* Non of the structure flattening cases apply, so we just pass using
  2368. the integer ABI. */
  2369. riscv_call_arg_scalar_int (ainfo, cinfo);
  2370. }
  2371. /* Assign a location to call (or return) argument AINFO, the location is
  2372. selected from CINFO which holds information about what call argument
  2373. locations are available for use next. The TYPE is the type of the
  2374. argument being passed, this information is recorded into AINFO (along
  2375. with some additional information derived from the type). IS_UNNAMED
  2376. is true if this is an unnamed (stdarg) argument, this info is also
  2377. recorded into AINFO.
  2378. After assigning a location to AINFO, CINFO will have been updated. */
  2379. static void
  2380. riscv_arg_location (struct gdbarch *gdbarch,
  2381. struct riscv_arg_info *ainfo,
  2382. struct riscv_call_info *cinfo,
  2383. struct type *type, bool is_unnamed)
  2384. {
  2385. ainfo->type = type;
  2386. ainfo->length = TYPE_LENGTH (ainfo->type);
  2387. ainfo->align = type_align (ainfo->type);
  2388. ainfo->is_unnamed = is_unnamed;
  2389. ainfo->contents = nullptr;
  2390. ainfo->argloc[0].c_length = 0;
  2391. ainfo->argloc[1].c_length = 0;
  2392. switch (ainfo->type->code ())
  2393. {
  2394. case TYPE_CODE_INT:
  2395. case TYPE_CODE_BOOL:
  2396. case TYPE_CODE_CHAR:
  2397. case TYPE_CODE_RANGE:
  2398. case TYPE_CODE_ENUM:
  2399. case TYPE_CODE_PTR:
  2400. case TYPE_CODE_FIXED_POINT:
  2401. if (ainfo->length <= cinfo->xlen)
  2402. {
  2403. ainfo->type = builtin_type (gdbarch)->builtin_long;
  2404. ainfo->length = cinfo->xlen;
  2405. }
  2406. else if (ainfo->length <= (2 * cinfo->xlen))
  2407. {
  2408. ainfo->type = builtin_type (gdbarch)->builtin_long_long;
  2409. ainfo->length = 2 * cinfo->xlen;
  2410. }
  2411. /* Recalculate the alignment requirement. */
  2412. ainfo->align = type_align (ainfo->type);
  2413. riscv_call_arg_scalar_int (ainfo, cinfo);
  2414. break;
  2415. case TYPE_CODE_FLT:
  2416. riscv_call_arg_scalar_float (ainfo, cinfo);
  2417. break;
  2418. case TYPE_CODE_COMPLEX:
  2419. riscv_call_arg_complex_float (ainfo, cinfo);
  2420. break;
  2421. case TYPE_CODE_STRUCT:
  2422. riscv_call_arg_struct (ainfo, cinfo);
  2423. break;
  2424. default:
  2425. riscv_call_arg_scalar_int (ainfo, cinfo);
  2426. break;
  2427. }
  2428. }
  2429. /* Used for printing debug information about the call argument location in
  2430. INFO to STREAM. The addresses in SP_REFS and SP_ARGS are the base
  2431. addresses for the location of pass-by-reference and
  2432. arguments-on-the-stack memory areas. */
  2433. static void
  2434. riscv_print_arg_location (ui_file *stream, struct gdbarch *gdbarch,
  2435. struct riscv_arg_info *info,
  2436. CORE_ADDR sp_refs, CORE_ADDR sp_args)
  2437. {
  2438. gdb_printf (stream, "type: '%s', length: 0x%x, alignment: 0x%x",
  2439. TYPE_SAFE_NAME (info->type), info->length, info->align);
  2440. switch (info->argloc[0].loc_type)
  2441. {
  2442. case riscv_arg_info::location::in_reg:
  2443. gdb_printf
  2444. (stream, ", register %s",
  2445. gdbarch_register_name (gdbarch, info->argloc[0].loc_data.regno));
  2446. if (info->argloc[0].c_length < info->length)
  2447. {
  2448. switch (info->argloc[1].loc_type)
  2449. {
  2450. case riscv_arg_info::location::in_reg:
  2451. gdb_printf
  2452. (stream, ", register %s",
  2453. gdbarch_register_name (gdbarch,
  2454. info->argloc[1].loc_data.regno));
  2455. break;
  2456. case riscv_arg_info::location::on_stack:
  2457. gdb_printf (stream, ", on stack at offset 0x%x",
  2458. info->argloc[1].loc_data.offset);
  2459. break;
  2460. case riscv_arg_info::location::by_ref:
  2461. default:
  2462. /* The second location should never be a reference, any
  2463. argument being passed by reference just places its address
  2464. in the first location and is done. */
  2465. error (_("invalid argument location"));
  2466. break;
  2467. }
  2468. if (info->argloc[1].c_offset > info->argloc[0].c_length)
  2469. gdb_printf (stream, " (offset 0x%x)",
  2470. info->argloc[1].c_offset);
  2471. }
  2472. break;
  2473. case riscv_arg_info::location::on_stack:
  2474. gdb_printf (stream, ", on stack at offset 0x%x",
  2475. info->argloc[0].loc_data.offset);
  2476. break;
  2477. case riscv_arg_info::location::by_ref:
  2478. gdb_printf
  2479. (stream, ", by reference, data at offset 0x%x (%s)",
  2480. info->argloc[0].loc_data.offset,
  2481. core_addr_to_string (sp_refs + info->argloc[0].loc_data.offset));
  2482. if (info->argloc[1].loc_type
  2483. == riscv_arg_info::location::in_reg)
  2484. gdb_printf
  2485. (stream, ", address in register %s",
  2486. gdbarch_register_name (gdbarch, info->argloc[1].loc_data.regno));
  2487. else
  2488. {
  2489. gdb_assert (info->argloc[1].loc_type
  2490. == riscv_arg_info::location::on_stack);
  2491. gdb_printf
  2492. (stream, ", address on stack at offset 0x%x (%s)",
  2493. info->argloc[1].loc_data.offset,
  2494. core_addr_to_string (sp_args + info->argloc[1].loc_data.offset));
  2495. }
  2496. break;
  2497. default:
  2498. gdb_assert_not_reached ("unknown argument location type");
  2499. }
  2500. }
  2501. /* Wrapper around REGCACHE->cooked_write. Places the LEN bytes of DATA
  2502. into a buffer that is at least as big as the register REGNUM, padding
  2503. out the DATA with either 0x00, or 0xff. For floating point registers
  2504. 0xff is used, for everyone else 0x00 is used. */
  2505. static void
  2506. riscv_regcache_cooked_write (int regnum, const gdb_byte *data, int len,
  2507. struct regcache *regcache, int flen)
  2508. {
  2509. gdb_byte tmp [sizeof (ULONGEST)];
  2510. /* FP values in FP registers must be NaN-boxed. */
  2511. if (riscv_is_fp_regno_p (regnum) && len < flen)
  2512. memset (tmp, -1, sizeof (tmp));
  2513. else
  2514. memset (tmp, 0, sizeof (tmp));
  2515. memcpy (tmp, data, len);
  2516. regcache->cooked_write (regnum, tmp);
  2517. }
  2518. /* Implement the push dummy call gdbarch callback. */
  2519. static CORE_ADDR
  2520. riscv_push_dummy_call (struct gdbarch *gdbarch,
  2521. struct value *function,
  2522. struct regcache *regcache,
  2523. CORE_ADDR bp_addr,
  2524. int nargs,
  2525. struct value **args,
  2526. CORE_ADDR sp,
  2527. function_call_return_method return_method,
  2528. CORE_ADDR struct_addr)
  2529. {
  2530. int i;
  2531. CORE_ADDR sp_args, sp_refs;
  2532. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  2533. struct riscv_arg_info *arg_info =
  2534. (struct riscv_arg_info *) alloca (nargs * sizeof (struct riscv_arg_info));
  2535. struct riscv_call_info call_info (gdbarch);
  2536. CORE_ADDR osp = sp;
  2537. struct type *ftype = check_typedef (value_type (function));
  2538. if (ftype->code () == TYPE_CODE_PTR)
  2539. ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
  2540. /* We'll use register $a0 if we're returning a struct. */
  2541. if (return_method == return_method_struct)
  2542. ++call_info.int_regs.next_regnum;
  2543. for (i = 0; i < nargs; ++i)
  2544. {
  2545. struct value *arg_value;
  2546. struct type *arg_type;
  2547. struct riscv_arg_info *info = &arg_info[i];
  2548. arg_value = args[i];
  2549. arg_type = check_typedef (value_type (arg_value));
  2550. riscv_arg_location (gdbarch, info, &call_info, arg_type,
  2551. ftype->has_varargs () && i >= ftype->num_fields ());
  2552. if (info->type != arg_type)
  2553. arg_value = value_cast (info->type, arg_value);
  2554. info->contents = value_contents (arg_value).data ();
  2555. }
  2556. /* Adjust the stack pointer and align it. */
  2557. sp = sp_refs = align_down (sp - call_info.memory.ref_offset, SP_ALIGNMENT);
  2558. sp = sp_args = align_down (sp - call_info.memory.arg_offset, SP_ALIGNMENT);
  2559. if (riscv_debug_infcall > 0)
  2560. {
  2561. gdb_printf (gdb_stdlog, "dummy call args:\n");
  2562. gdb_printf (gdb_stdlog, ": floating point ABI %s in use\n",
  2563. (riscv_has_fp_abi (gdbarch) ? "is" : "is not"));
  2564. gdb_printf (gdb_stdlog, ": xlen: %d\n: flen: %d\n",
  2565. call_info.xlen, call_info.flen);
  2566. if (return_method == return_method_struct)
  2567. gdb_printf (gdb_stdlog,
  2568. "[*] struct return pointer in register $A0\n");
  2569. for (i = 0; i < nargs; ++i)
  2570. {
  2571. struct riscv_arg_info *info = &arg_info [i];
  2572. gdb_printf (gdb_stdlog, "[%2d] ", i);
  2573. riscv_print_arg_location (gdb_stdlog, gdbarch, info, sp_refs, sp_args);
  2574. gdb_printf (gdb_stdlog, "\n");
  2575. }
  2576. if (call_info.memory.arg_offset > 0
  2577. || call_info.memory.ref_offset > 0)
  2578. {
  2579. gdb_printf (gdb_stdlog, " Original sp: %s\n",
  2580. core_addr_to_string (osp));
  2581. gdb_printf (gdb_stdlog, "Stack required (for args): 0x%x\n",
  2582. call_info.memory.arg_offset);
  2583. gdb_printf (gdb_stdlog, "Stack required (for refs): 0x%x\n",
  2584. call_info.memory.ref_offset);
  2585. gdb_printf (gdb_stdlog, " Stack allocated: %s\n",
  2586. core_addr_to_string_nz (osp - sp));
  2587. }
  2588. }
  2589. /* Now load the argument into registers, or onto the stack. */
  2590. if (return_method == return_method_struct)
  2591. {
  2592. gdb_byte buf[sizeof (LONGEST)];
  2593. store_unsigned_integer (buf, call_info.xlen, byte_order, struct_addr);
  2594. regcache->cooked_write (RISCV_A0_REGNUM, buf);
  2595. }
  2596. for (i = 0; i < nargs; ++i)
  2597. {
  2598. CORE_ADDR dst;
  2599. int second_arg_length = 0;
  2600. const gdb_byte *second_arg_data;
  2601. struct riscv_arg_info *info = &arg_info [i];
  2602. gdb_assert (info->length > 0);
  2603. switch (info->argloc[0].loc_type)
  2604. {
  2605. case riscv_arg_info::location::in_reg:
  2606. {
  2607. gdb_assert (info->argloc[0].c_length <= info->length);
  2608. riscv_regcache_cooked_write (info->argloc[0].loc_data.regno,
  2609. (info->contents
  2610. + info->argloc[0].c_offset),
  2611. info->argloc[0].c_length,
  2612. regcache, call_info.flen);
  2613. second_arg_length =
  2614. (((info->argloc[0].c_length + info->argloc[0].c_offset) < info->length)
  2615. ? info->argloc[1].c_length : 0);
  2616. second_arg_data = info->contents + info->argloc[1].c_offset;
  2617. }
  2618. break;
  2619. case riscv_arg_info::location::on_stack:
  2620. dst = sp_args + info->argloc[0].loc_data.offset;
  2621. write_memory (dst, info->contents, info->length);
  2622. second_arg_length = 0;
  2623. break;
  2624. case riscv_arg_info::location::by_ref:
  2625. dst = sp_refs + info->argloc[0].loc_data.offset;
  2626. write_memory (dst, info->contents, info->length);
  2627. second_arg_length = call_info.xlen;
  2628. second_arg_data = (gdb_byte *) &dst;
  2629. break;
  2630. default:
  2631. gdb_assert_not_reached ("unknown argument location type");
  2632. }
  2633. if (second_arg_length > 0)
  2634. {
  2635. switch (info->argloc[1].loc_type)
  2636. {
  2637. case riscv_arg_info::location::in_reg:
  2638. {
  2639. gdb_assert ((riscv_is_fp_regno_p (info->argloc[1].loc_data.regno)
  2640. && second_arg_length <= call_info.flen)
  2641. || second_arg_length <= call_info.xlen);
  2642. riscv_regcache_cooked_write (info->argloc[1].loc_data.regno,
  2643. second_arg_data,
  2644. second_arg_length,
  2645. regcache, call_info.flen);
  2646. }
  2647. break;
  2648. case riscv_arg_info::location::on_stack:
  2649. {
  2650. CORE_ADDR arg_addr;
  2651. arg_addr = sp_args + info->argloc[1].loc_data.offset;
  2652. write_memory (arg_addr, second_arg_data, second_arg_length);
  2653. break;
  2654. }
  2655. case riscv_arg_info::location::by_ref:
  2656. default:
  2657. /* The second location should never be a reference, any
  2658. argument being passed by reference just places its address
  2659. in the first location and is done. */
  2660. error (_("invalid argument location"));
  2661. break;
  2662. }
  2663. }
  2664. }
  2665. /* Set the dummy return value to bp_addr.
  2666. A dummy breakpoint will be setup to execute the call. */
  2667. if (riscv_debug_infcall > 0)
  2668. gdb_printf (gdb_stdlog, ": writing $ra = %s\n",
  2669. core_addr_to_string (bp_addr));
  2670. regcache_cooked_write_unsigned (regcache, RISCV_RA_REGNUM, bp_addr);
  2671. /* Finally, update the stack pointer. */
  2672. if (riscv_debug_infcall > 0)
  2673. gdb_printf (gdb_stdlog, ": writing $sp = %s\n",
  2674. core_addr_to_string (sp));
  2675. regcache_cooked_write_unsigned (regcache, RISCV_SP_REGNUM, sp);
  2676. return sp;
  2677. }
  2678. /* Implement the return_value gdbarch method. */
  2679. static enum return_value_convention
  2680. riscv_return_value (struct gdbarch *gdbarch,
  2681. struct value *function,
  2682. struct type *type,
  2683. struct regcache *regcache,
  2684. gdb_byte *readbuf,
  2685. const gdb_byte *writebuf)
  2686. {
  2687. struct riscv_call_info call_info (gdbarch);
  2688. struct riscv_arg_info info;
  2689. struct type *arg_type;
  2690. arg_type = check_typedef (type);
  2691. riscv_arg_location (gdbarch, &info, &call_info, arg_type, false);
  2692. if (riscv_debug_infcall > 0)
  2693. {
  2694. gdb_printf (gdb_stdlog, "riscv return value:\n");
  2695. gdb_printf (gdb_stdlog, "[R] ");
  2696. riscv_print_arg_location (gdb_stdlog, gdbarch, &info, 0, 0);
  2697. gdb_printf (gdb_stdlog, "\n");
  2698. }
  2699. if (readbuf != nullptr || writebuf != nullptr)
  2700. {
  2701. unsigned int arg_len;
  2702. struct value *abi_val;
  2703. gdb_byte *old_readbuf = nullptr;
  2704. int regnum;
  2705. /* We only do one thing at a time. */
  2706. gdb_assert (readbuf == nullptr || writebuf == nullptr);
  2707. /* In some cases the argument is not returned as the declared type,
  2708. and we need to cast to or from the ABI type in order to
  2709. correctly access the argument. When writing to the machine we
  2710. do the cast here, when reading from the machine the cast occurs
  2711. later, after extracting the value. As the ABI type can be
  2712. larger than the declared type, then the read or write buffers
  2713. passed in might be too small. Here we ensure that we are using
  2714. buffers of sufficient size. */
  2715. if (writebuf != nullptr)
  2716. {
  2717. struct value *arg_val;
  2718. if (is_fixed_point_type (arg_type))
  2719. {
  2720. /* Convert the argument to the type used to pass
  2721. the return value, but being careful to preserve
  2722. the fact that the value needs to be returned
  2723. unscaled. */
  2724. gdb_mpz unscaled;
  2725. unscaled.read (gdb::make_array_view (writebuf,
  2726. TYPE_LENGTH (arg_type)),
  2727. type_byte_order (arg_type),
  2728. arg_type->is_unsigned ());
  2729. abi_val = allocate_value (info.type);
  2730. unscaled.write (value_contents_raw (abi_val),
  2731. type_byte_order (info.type),
  2732. info.type->is_unsigned ());
  2733. }
  2734. else
  2735. {
  2736. arg_val = value_from_contents (arg_type, writebuf);
  2737. abi_val = value_cast (info.type, arg_val);
  2738. }
  2739. writebuf = value_contents_raw (abi_val).data ();
  2740. }
  2741. else
  2742. {
  2743. abi_val = allocate_value (info.type);
  2744. old_readbuf = readbuf;
  2745. readbuf = value_contents_raw (abi_val).data ();
  2746. }
  2747. arg_len = TYPE_LENGTH (info.type);
  2748. switch (info.argloc[0].loc_type)
  2749. {
  2750. /* Return value in register(s). */
  2751. case riscv_arg_info::location::in_reg:
  2752. {
  2753. regnum = info.argloc[0].loc_data.regno;
  2754. gdb_assert (info.argloc[0].c_length <= arg_len);
  2755. gdb_assert (info.argloc[0].c_length
  2756. <= register_size (gdbarch, regnum));
  2757. if (readbuf)
  2758. {
  2759. gdb_byte *ptr = readbuf + info.argloc[0].c_offset;
  2760. regcache->cooked_read_part (regnum, 0,
  2761. info.argloc[0].c_length,
  2762. ptr);
  2763. }
  2764. if (writebuf)
  2765. {
  2766. const gdb_byte *ptr = writebuf + info.argloc[0].c_offset;
  2767. riscv_regcache_cooked_write (regnum, ptr,
  2768. info.argloc[0].c_length,
  2769. regcache, call_info.flen);
  2770. }
  2771. /* A return value in register can have a second part in a
  2772. second register. */
  2773. if (info.argloc[1].c_length > 0)
  2774. {
  2775. switch (info.argloc[1].loc_type)
  2776. {
  2777. case riscv_arg_info::location::in_reg:
  2778. regnum = info.argloc[1].loc_data.regno;
  2779. gdb_assert ((info.argloc[0].c_length
  2780. + info.argloc[1].c_length) <= arg_len);
  2781. gdb_assert (info.argloc[1].c_length
  2782. <= register_size (gdbarch, regnum));
  2783. if (readbuf)
  2784. {
  2785. readbuf += info.argloc[1].c_offset;
  2786. regcache->cooked_read_part (regnum, 0,
  2787. info.argloc[1].c_length,
  2788. readbuf);
  2789. }
  2790. if (writebuf)
  2791. {
  2792. const gdb_byte *ptr
  2793. = writebuf + info.argloc[1].c_offset;
  2794. riscv_regcache_cooked_write
  2795. (regnum, ptr, info.argloc[1].c_length,
  2796. regcache, call_info.flen);
  2797. }
  2798. break;
  2799. case riscv_arg_info::location::by_ref:
  2800. case riscv_arg_info::location::on_stack:
  2801. default:
  2802. error (_("invalid argument location"));
  2803. break;
  2804. }
  2805. }
  2806. }
  2807. break;
  2808. /* Return value by reference will have its address in A0. */
  2809. case riscv_arg_info::location::by_ref:
  2810. {
  2811. ULONGEST addr;
  2812. regcache_cooked_read_unsigned (regcache, RISCV_A0_REGNUM,
  2813. &addr);
  2814. if (readbuf != nullptr)
  2815. read_memory (addr, readbuf, info.length);
  2816. if (writebuf != nullptr)
  2817. write_memory (addr, writebuf, info.length);
  2818. }
  2819. break;
  2820. case riscv_arg_info::location::on_stack:
  2821. default:
  2822. error (_("invalid argument location"));
  2823. break;
  2824. }
  2825. /* This completes the cast from abi type back to the declared type
  2826. in the case that we are reading from the machine. See the
  2827. comment at the head of this block for more details. */
  2828. if (readbuf != nullptr)
  2829. {
  2830. struct value *arg_val;
  2831. if (is_fixed_point_type (arg_type))
  2832. {
  2833. /* Convert abi_val to the actual return type, but
  2834. being careful to preserve the fact that abi_val
  2835. is unscaled. */
  2836. gdb_mpz unscaled;
  2837. unscaled.read (value_contents (abi_val),
  2838. type_byte_order (info.type),
  2839. info.type->is_unsigned ());
  2840. arg_val = allocate_value (arg_type);
  2841. unscaled.write (value_contents_raw (arg_val),
  2842. type_byte_order (arg_type),
  2843. arg_type->is_unsigned ());
  2844. }
  2845. else
  2846. arg_val = value_cast (arg_type, abi_val);
  2847. memcpy (old_readbuf, value_contents_raw (arg_val).data (),
  2848. TYPE_LENGTH (arg_type));
  2849. }
  2850. }
  2851. switch (info.argloc[0].loc_type)
  2852. {
  2853. case riscv_arg_info::location::in_reg:
  2854. return RETURN_VALUE_REGISTER_CONVENTION;
  2855. case riscv_arg_info::location::by_ref:
  2856. return RETURN_VALUE_ABI_PRESERVES_ADDRESS;
  2857. case riscv_arg_info::location::on_stack:
  2858. default:
  2859. error (_("invalid argument location"));
  2860. }
  2861. }
  2862. /* Implement the frame_align gdbarch method. */
  2863. static CORE_ADDR
  2864. riscv_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
  2865. {
  2866. return align_down (addr, 16);
  2867. }
  2868. /* Generate, or return the cached frame cache for the RiscV frame
  2869. unwinder. */
  2870. static struct riscv_unwind_cache *
  2871. riscv_frame_cache (struct frame_info *this_frame, void **this_cache)
  2872. {
  2873. CORE_ADDR pc, start_addr;
  2874. struct riscv_unwind_cache *cache;
  2875. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  2876. int numregs, regno;
  2877. if ((*this_cache) != NULL)
  2878. return (struct riscv_unwind_cache *) *this_cache;
  2879. cache = FRAME_OBSTACK_ZALLOC (struct riscv_unwind_cache);
  2880. cache->regs = trad_frame_alloc_saved_regs (this_frame);
  2881. (*this_cache) = cache;
  2882. /* Scan the prologue, filling in the cache. */
  2883. start_addr = get_frame_func (this_frame);
  2884. pc = get_frame_pc (this_frame);
  2885. riscv_scan_prologue (gdbarch, start_addr, pc, cache);
  2886. /* We can now calculate the frame base address. */
  2887. cache->frame_base
  2888. = (get_frame_register_unsigned (this_frame, cache->frame_base_reg)
  2889. + cache->frame_base_offset);
  2890. if (riscv_debug_unwinder)
  2891. gdb_printf (gdb_stdlog, "Frame base is %s ($%s + 0x%x)\n",
  2892. core_addr_to_string (cache->frame_base),
  2893. gdbarch_register_name (gdbarch,
  2894. cache->frame_base_reg),
  2895. cache->frame_base_offset);
  2896. /* The prologue scanner sets the address of registers stored to the stack
  2897. as the offset of that register from the frame base. The prologue
  2898. scanner doesn't know the actual frame base value, and so is unable to
  2899. compute the exact address. We do now know the frame base value, so
  2900. update the address of registers stored to the stack. */
  2901. numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
  2902. for (regno = 0; regno < numregs; ++regno)
  2903. {
  2904. if (cache->regs[regno].is_addr ())
  2905. cache->regs[regno].set_addr (cache->regs[regno].addr ()
  2906. + cache->frame_base);
  2907. }
  2908. /* The previous $pc can be found wherever the $ra value can be found.
  2909. The previous $ra value is gone, this would have been stored be the
  2910. previous frame if required. */
  2911. cache->regs[gdbarch_pc_regnum (gdbarch)] = cache->regs[RISCV_RA_REGNUM];
  2912. cache->regs[RISCV_RA_REGNUM].set_unknown ();
  2913. /* Build the frame id. */
  2914. cache->this_id = frame_id_build (cache->frame_base, start_addr);
  2915. /* The previous $sp value is the frame base value. */
  2916. cache->regs[gdbarch_sp_regnum (gdbarch)].set_value (cache->frame_base);
  2917. return cache;
  2918. }
  2919. /* Implement the this_id callback for RiscV frame unwinder. */
  2920. static void
  2921. riscv_frame_this_id (struct frame_info *this_frame,
  2922. void **prologue_cache,
  2923. struct frame_id *this_id)
  2924. {
  2925. struct riscv_unwind_cache *cache;
  2926. try
  2927. {
  2928. cache = riscv_frame_cache (this_frame, prologue_cache);
  2929. *this_id = cache->this_id;
  2930. }
  2931. catch (const gdb_exception_error &ex)
  2932. {
  2933. /* Ignore errors, this leaves the frame id as the predefined outer
  2934. frame id which terminates the backtrace at this point. */
  2935. }
  2936. }
  2937. /* Implement the prev_register callback for RiscV frame unwinder. */
  2938. static struct value *
  2939. riscv_frame_prev_register (struct frame_info *this_frame,
  2940. void **prologue_cache,
  2941. int regnum)
  2942. {
  2943. struct riscv_unwind_cache *cache;
  2944. cache = riscv_frame_cache (this_frame, prologue_cache);
  2945. return trad_frame_get_prev_register (this_frame, cache->regs, regnum);
  2946. }
  2947. /* Structure defining the RiscV normal frame unwind functions. Since we
  2948. are the fallback unwinder (DWARF unwinder is used first), we use the
  2949. default frame sniffer, which always accepts the frame. */
  2950. static const struct frame_unwind riscv_frame_unwind =
  2951. {
  2952. /*.name =*/ "riscv prologue",
  2953. /*.type =*/ NORMAL_FRAME,
  2954. /*.stop_reason =*/ default_frame_unwind_stop_reason,
  2955. /*.this_id =*/ riscv_frame_this_id,
  2956. /*.prev_register =*/ riscv_frame_prev_register,
  2957. /*.unwind_data =*/ NULL,
  2958. /*.sniffer =*/ default_frame_sniffer,
  2959. /*.dealloc_cache =*/ NULL,
  2960. /*.prev_arch =*/ NULL,
  2961. };
  2962. /* Extract a set of required target features out of ABFD. If ABFD is
  2963. nullptr then a RISCV_GDBARCH_FEATURES is returned in its default state. */
  2964. static struct riscv_gdbarch_features
  2965. riscv_features_from_bfd (const bfd *abfd)
  2966. {
  2967. struct riscv_gdbarch_features features;
  2968. /* Now try to improve on the defaults by looking at the binary we are
  2969. going to execute. We assume the user knows what they are doing and
  2970. that the target will match the binary. Remember, this code path is
  2971. only used at all if the target hasn't given us a description, so this
  2972. is really a last ditched effort to do something sane before giving
  2973. up. */
  2974. if (abfd != nullptr && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
  2975. {
  2976. unsigned char eclass = elf_elfheader (abfd)->e_ident[EI_CLASS];
  2977. int e_flags = elf_elfheader (abfd)->e_flags;
  2978. if (eclass == ELFCLASS32)
  2979. features.xlen = 4;
  2980. else if (eclass == ELFCLASS64)
  2981. features.xlen = 8;
  2982. else
  2983. internal_error (__FILE__, __LINE__,
  2984. _("unknown ELF header class %d"), eclass);
  2985. if (e_flags & EF_RISCV_FLOAT_ABI_DOUBLE)
  2986. features.flen = 8;
  2987. else if (e_flags & EF_RISCV_FLOAT_ABI_SINGLE)
  2988. features.flen = 4;
  2989. if (e_flags & EF_RISCV_RVE)
  2990. {
  2991. if (features.xlen == 8)
  2992. {
  2993. warning (_("64-bit ELF with RV32E flag set! Assuming 32-bit"));
  2994. features.xlen = 4;
  2995. }
  2996. features.embedded = true;
  2997. }
  2998. }
  2999. return features;
  3000. }
  3001. /* Find a suitable default target description. Use the contents of INFO,
  3002. specifically the bfd object being executed, to guide the selection of a
  3003. suitable default target description. */
  3004. static const struct target_desc *
  3005. riscv_find_default_target_description (const struct gdbarch_info info)
  3006. {
  3007. /* Extract desired feature set from INFO. */
  3008. struct riscv_gdbarch_features features
  3009. = riscv_features_from_bfd (info.abfd);
  3010. /* If the XLEN field is still 0 then we got nothing useful from INFO.BFD,
  3011. maybe there was no bfd object. In this case we fall back to a minimal
  3012. useful target with no floating point, the x-register size is selected
  3013. based on the architecture from INFO. */
  3014. if (features.xlen == 0)
  3015. features.xlen = info.bfd_arch_info->bits_per_word == 32 ? 4 : 8;
  3016. /* Now build a target description based on the feature set. */
  3017. return riscv_lookup_target_description (features);
  3018. }
  3019. /* Add all the RISC-V specific register groups into GDBARCH. */
  3020. static void
  3021. riscv_add_reggroups (struct gdbarch *gdbarch)
  3022. {
  3023. reggroup_add (gdbarch, csr_reggroup);
  3024. }
  3025. /* Implement the "dwarf2_reg_to_regnum" gdbarch method. */
  3026. static int
  3027. riscv_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
  3028. {
  3029. if (reg < RISCV_DWARF_REGNUM_X31)
  3030. return RISCV_ZERO_REGNUM + (reg - RISCV_DWARF_REGNUM_X0);
  3031. else if (reg < RISCV_DWARF_REGNUM_F31)
  3032. return RISCV_FIRST_FP_REGNUM + (reg - RISCV_DWARF_REGNUM_F0);
  3033. else if (reg >= RISCV_DWARF_FIRST_CSR && reg <= RISCV_DWARF_LAST_CSR)
  3034. return RISCV_FIRST_CSR_REGNUM + (reg - RISCV_DWARF_FIRST_CSR);
  3035. else if (reg >= RISCV_DWARF_REGNUM_V0 && reg <= RISCV_DWARF_REGNUM_V31)
  3036. return RISCV_V0_REGNUM + (reg - RISCV_DWARF_REGNUM_V0);
  3037. return -1;
  3038. }
  3039. /* Implement the gcc_target_options method. We have to select the arch and abi
  3040. from the feature info. We have enough feature info to select the abi, but
  3041. not enough info for the arch given all of the possible architecture
  3042. extensions. So choose reasonable defaults for now. */
  3043. static std::string
  3044. riscv_gcc_target_options (struct gdbarch *gdbarch)
  3045. {
  3046. int isa_xlen = riscv_isa_xlen (gdbarch);
  3047. int isa_flen = riscv_isa_flen (gdbarch);
  3048. int abi_xlen = riscv_abi_xlen (gdbarch);
  3049. int abi_flen = riscv_abi_flen (gdbarch);
  3050. std::string target_options;
  3051. target_options = "-march=rv";
  3052. if (isa_xlen == 8)
  3053. target_options += "64";
  3054. else
  3055. target_options += "32";
  3056. if (isa_flen == 8)
  3057. target_options += "gc";
  3058. else if (isa_flen == 4)
  3059. target_options += "imafc";
  3060. else
  3061. target_options += "imac";
  3062. target_options += " -mabi=";
  3063. if (abi_xlen == 8)
  3064. target_options += "lp64";
  3065. else
  3066. target_options += "ilp32";
  3067. if (abi_flen == 8)
  3068. target_options += "d";
  3069. else if (abi_flen == 4)
  3070. target_options += "f";
  3071. /* The gdb loader doesn't handle link-time relaxation relocations. */
  3072. target_options += " -mno-relax";
  3073. return target_options;
  3074. }
  3075. /* Call back from tdesc_use_registers, called for each unknown register
  3076. found in the target description.
  3077. See target-description.h (typedef tdesc_unknown_register_ftype) for a
  3078. discussion of the arguments and return values. */
  3079. static int
  3080. riscv_tdesc_unknown_reg (struct gdbarch *gdbarch, tdesc_feature *feature,
  3081. const char *reg_name, int possible_regnum)
  3082. {
  3083. /* At one point in time GDB had an incorrect default target description
  3084. that duplicated the fflags, frm, and fcsr registers in both the FPU
  3085. and CSR register sets.
  3086. Some targets (QEMU) copied these target descriptions into their source
  3087. tree, and so we're currently stuck working with some targets that
  3088. declare the same registers twice.
  3089. There's not much we can do about this any more. Assuming the target
  3090. will direct a request for either register number to the correct
  3091. underlying hardware register then it doesn't matter which one GDB
  3092. uses, so long as we (GDB) are consistent (so that we don't end up with
  3093. invalid cache misses).
  3094. As we always scan the FPU registers first, then the CSRs, if the
  3095. target has included the offending registers in both sets then we will
  3096. always see the FPU copies here, as the CSR versions will replace them
  3097. in the register list.
  3098. To prevent these duplicates showing up in any of the register list,
  3099. record their register numbers here. */
  3100. if (strcmp (tdesc_feature_name (feature), riscv_freg_feature.name ()) == 0)
  3101. {
  3102. riscv_gdbarch_tdep *tdep = (riscv_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  3103. int *regnum_ptr = nullptr;
  3104. if (strcmp (reg_name, "fflags") == 0)
  3105. regnum_ptr = &tdep->duplicate_fflags_regnum;
  3106. else if (strcmp (reg_name, "frm") == 0)
  3107. regnum_ptr = &tdep->duplicate_frm_regnum;
  3108. else if (strcmp (reg_name, "fcsr") == 0)
  3109. regnum_ptr = &tdep->duplicate_fcsr_regnum;
  3110. if (regnum_ptr != nullptr)
  3111. {
  3112. /* This means the register appears more than twice in the target
  3113. description. Just let GDB add this as another register.
  3114. We'll have duplicates in the register name list, but there's
  3115. not much more we can do. */
  3116. if (*regnum_ptr != -1)
  3117. return -1;
  3118. /* Record the number assigned to this register, then return the
  3119. number (so it actually gets assigned to this register). */
  3120. *regnum_ptr = possible_regnum;
  3121. return possible_regnum;
  3122. }
  3123. }
  3124. /* Any unknown registers in the CSR feature are recorded within a single
  3125. block so we can easily identify these registers when making choices
  3126. about register groups in riscv_register_reggroup_p. */
  3127. if (strcmp (tdesc_feature_name (feature), riscv_csr_feature.name ()) == 0)
  3128. {
  3129. riscv_gdbarch_tdep *tdep = (riscv_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  3130. if (tdep->unknown_csrs_first_regnum == -1)
  3131. tdep->unknown_csrs_first_regnum = possible_regnum;
  3132. gdb_assert (tdep->unknown_csrs_first_regnum
  3133. + tdep->unknown_csrs_count == possible_regnum);
  3134. tdep->unknown_csrs_count++;
  3135. return possible_regnum;
  3136. }
  3137. /* Some other unknown register. Don't assign this a number now, it will
  3138. be assigned a number automatically later by the target description
  3139. handling code. */
  3140. return -1;
  3141. }
  3142. /* Implement the gnu_triplet_regexp method. A single compiler supports both
  3143. 32-bit and 64-bit code, and may be named riscv32 or riscv64 or (not
  3144. recommended) riscv. */
  3145. static const char *
  3146. riscv_gnu_triplet_regexp (struct gdbarch *gdbarch)
  3147. {
  3148. return "riscv(32|64)?";
  3149. }
  3150. /* Initialize the current architecture based on INFO. If possible,
  3151. re-use an architecture from ARCHES, which is a list of
  3152. architectures already created during this debugging session.
  3153. Called e.g. at program startup, when reading a core file, and when
  3154. reading a binary file. */
  3155. static struct gdbarch *
  3156. riscv_gdbarch_init (struct gdbarch_info info,
  3157. struct gdbarch_list *arches)
  3158. {
  3159. struct gdbarch *gdbarch;
  3160. struct riscv_gdbarch_features features;
  3161. const struct target_desc *tdesc = info.target_desc;
  3162. /* Ensure we always have a target description. */
  3163. if (!tdesc_has_registers (tdesc))
  3164. tdesc = riscv_find_default_target_description (info);
  3165. gdb_assert (tdesc != nullptr);
  3166. if (riscv_debug_gdbarch)
  3167. gdb_printf (gdb_stdlog, "Have got a target description\n");
  3168. tdesc_arch_data_up tdesc_data = tdesc_data_alloc ();
  3169. std::vector<riscv_pending_register_alias> pending_aliases;
  3170. bool valid_p = (riscv_xreg_feature.check (tdesc, tdesc_data.get (),
  3171. &pending_aliases, &features)
  3172. && riscv_freg_feature.check (tdesc, tdesc_data.get (),
  3173. &pending_aliases, &features)
  3174. && riscv_virtual_feature.check (tdesc, tdesc_data.get (),
  3175. &pending_aliases, &features)
  3176. && riscv_csr_feature.check (tdesc, tdesc_data.get (),
  3177. &pending_aliases, &features)
  3178. && riscv_vector_feature.check (tdesc, tdesc_data.get (),
  3179. &pending_aliases, &features));
  3180. if (!valid_p)
  3181. {
  3182. if (riscv_debug_gdbarch)
  3183. gdb_printf (gdb_stdlog, "Target description is not valid\n");
  3184. return NULL;
  3185. }
  3186. /* Have a look at what the supplied (if any) bfd object requires of the
  3187. target, then check that this matches with what the target is
  3188. providing. */
  3189. struct riscv_gdbarch_features abi_features
  3190. = riscv_features_from_bfd (info.abfd);
  3191. /* If the ABI_FEATURES xlen is 0 then this indicates we got no useful abi
  3192. features from the INFO object. In this case we just treat the
  3193. hardware features as defining the abi. */
  3194. if (abi_features.xlen == 0)
  3195. abi_features = features;
  3196. /* In theory a binary compiled for RV32 could run on an RV64 target,
  3197. however, this has not been tested in GDB yet, so for now we require
  3198. that the requested xlen match the targets xlen. */
  3199. if (abi_features.xlen != features.xlen)
  3200. error (_("bfd requires xlen %d, but target has xlen %d"),
  3201. abi_features.xlen, features.xlen);
  3202. /* We do support running binaries compiled for 32-bit float on targets
  3203. with 64-bit float, so we only complain if the binary requires more
  3204. than the target has available. */
  3205. if (abi_features.flen > features.flen)
  3206. error (_("bfd requires flen %d, but target has flen %d"),
  3207. abi_features.flen, features.flen);
  3208. /* Find a candidate among the list of pre-declared architectures. */
  3209. for (arches = gdbarch_list_lookup_by_info (arches, &info);
  3210. arches != NULL;
  3211. arches = gdbarch_list_lookup_by_info (arches->next, &info))
  3212. {
  3213. /* Check that the feature set of the ARCHES matches the feature set
  3214. we are looking for. If it doesn't then we can't reuse this
  3215. gdbarch. */
  3216. riscv_gdbarch_tdep *other_tdep
  3217. = (riscv_gdbarch_tdep *) gdbarch_tdep (arches->gdbarch);
  3218. if (other_tdep->isa_features != features
  3219. || other_tdep->abi_features != abi_features)
  3220. continue;
  3221. break;
  3222. }
  3223. if (arches != NULL)
  3224. return arches->gdbarch;
  3225. /* None found, so create a new architecture from the information provided. */
  3226. riscv_gdbarch_tdep *tdep = new riscv_gdbarch_tdep;
  3227. gdbarch = gdbarch_alloc (&info, tdep);
  3228. tdep->isa_features = features;
  3229. tdep->abi_features = abi_features;
  3230. /* Target data types. */
  3231. set_gdbarch_short_bit (gdbarch, 16);
  3232. set_gdbarch_int_bit (gdbarch, 32);
  3233. set_gdbarch_long_bit (gdbarch, riscv_isa_xlen (gdbarch) * 8);
  3234. set_gdbarch_long_long_bit (gdbarch, 64);
  3235. set_gdbarch_float_bit (gdbarch, 32);
  3236. set_gdbarch_double_bit (gdbarch, 64);
  3237. set_gdbarch_long_double_bit (gdbarch, 128);
  3238. set_gdbarch_long_double_format (gdbarch, floatformats_ieee_quad);
  3239. set_gdbarch_ptr_bit (gdbarch, riscv_isa_xlen (gdbarch) * 8);
  3240. set_gdbarch_char_signed (gdbarch, 0);
  3241. set_gdbarch_type_align (gdbarch, riscv_type_align);
  3242. /* Information about the target architecture. */
  3243. set_gdbarch_return_value (gdbarch, riscv_return_value);
  3244. set_gdbarch_breakpoint_kind_from_pc (gdbarch, riscv_breakpoint_kind_from_pc);
  3245. set_gdbarch_sw_breakpoint_from_kind (gdbarch, riscv_sw_breakpoint_from_kind);
  3246. set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
  3247. /* Functions to analyze frames. */
  3248. set_gdbarch_skip_prologue (gdbarch, riscv_skip_prologue);
  3249. set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  3250. set_gdbarch_frame_align (gdbarch, riscv_frame_align);
  3251. /* Functions handling dummy frames. */
  3252. set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
  3253. set_gdbarch_push_dummy_code (gdbarch, riscv_push_dummy_code);
  3254. set_gdbarch_push_dummy_call (gdbarch, riscv_push_dummy_call);
  3255. /* Frame unwinders. Use DWARF debug info if available, otherwise use our own
  3256. unwinder. */
  3257. dwarf2_append_unwinders (gdbarch);
  3258. frame_unwind_append_unwinder (gdbarch, &riscv_frame_unwind);
  3259. /* Register architecture. */
  3260. riscv_add_reggroups (gdbarch);
  3261. /* Internal <-> external register number maps. */
  3262. set_gdbarch_dwarf2_reg_to_regnum (gdbarch, riscv_dwarf_reg_to_regnum);
  3263. /* We reserve all possible register numbers for the known registers.
  3264. This means the target description mechanism will add any target
  3265. specific registers after this number. This helps make debugging GDB
  3266. just a little easier. */
  3267. set_gdbarch_num_regs (gdbarch, RISCV_LAST_REGNUM + 1);
  3268. /* We don't have to provide the count of 0 here (its the default) but
  3269. include this line to make it explicit that, right now, we don't have
  3270. any pseudo registers on RISC-V. */
  3271. set_gdbarch_num_pseudo_regs (gdbarch, 0);
  3272. /* Some specific register numbers GDB likes to know about. */
  3273. set_gdbarch_sp_regnum (gdbarch, RISCV_SP_REGNUM);
  3274. set_gdbarch_pc_regnum (gdbarch, RISCV_PC_REGNUM);
  3275. set_gdbarch_print_registers_info (gdbarch, riscv_print_registers_info);
  3276. /* Finalise the target description registers. */
  3277. tdesc_use_registers (gdbarch, tdesc, std::move (tdesc_data),
  3278. riscv_tdesc_unknown_reg);
  3279. /* Override the register type callback setup by the target description
  3280. mechanism. This allows us to provide special type for floating point
  3281. registers. */
  3282. set_gdbarch_register_type (gdbarch, riscv_register_type);
  3283. /* Override the register name callback setup by the target description
  3284. mechanism. This allows us to force our preferred names for the
  3285. registers, no matter what the target description called them. */
  3286. set_gdbarch_register_name (gdbarch, riscv_register_name);
  3287. /* Override the register group callback setup by the target description
  3288. mechanism. This allows us to force registers into the groups we
  3289. want, ignoring what the target tells us. */
  3290. set_gdbarch_register_reggroup_p (gdbarch, riscv_register_reggroup_p);
  3291. /* Create register aliases for alternative register names. We only
  3292. create aliases for registers which were mentioned in the target
  3293. description. */
  3294. for (const auto &alias : pending_aliases)
  3295. alias.create (gdbarch);
  3296. /* Compile command hooks. */
  3297. set_gdbarch_gcc_target_options (gdbarch, riscv_gcc_target_options);
  3298. set_gdbarch_gnu_triplet_regexp (gdbarch, riscv_gnu_triplet_regexp);
  3299. /* Disassembler options support. */
  3300. set_gdbarch_valid_disassembler_options (gdbarch,
  3301. disassembler_options_riscv ());
  3302. set_gdbarch_disassembler_options (gdbarch, &riscv_disassembler_options);
  3303. /* Hook in OS ABI-specific overrides, if they have been registered. */
  3304. gdbarch_init_osabi (info, gdbarch);
  3305. register_riscv_ravenscar_ops (gdbarch);
  3306. return gdbarch;
  3307. }
  3308. /* This decodes the current instruction and determines the address of the
  3309. next instruction. */
  3310. static CORE_ADDR
  3311. riscv_next_pc (struct regcache *regcache, CORE_ADDR pc)
  3312. {
  3313. struct gdbarch *gdbarch = regcache->arch ();
  3314. const riscv_gdbarch_tdep *tdep
  3315. = (riscv_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  3316. struct riscv_insn insn;
  3317. CORE_ADDR next_pc;
  3318. insn.decode (gdbarch, pc);
  3319. next_pc = pc + insn.length ();
  3320. if (insn.opcode () == riscv_insn::JAL)
  3321. next_pc = pc + insn.imm_signed ();
  3322. else if (insn.opcode () == riscv_insn::JALR)
  3323. {
  3324. LONGEST source;
  3325. regcache->cooked_read (insn.rs1 (), &source);
  3326. next_pc = (source + insn.imm_signed ()) & ~(CORE_ADDR) 0x1;
  3327. }
  3328. else if (insn.opcode () == riscv_insn::BEQ)
  3329. {
  3330. LONGEST src1, src2;
  3331. regcache->cooked_read (insn.rs1 (), &src1);
  3332. regcache->cooked_read (insn.rs2 (), &src2);
  3333. if (src1 == src2)
  3334. next_pc = pc + insn.imm_signed ();
  3335. }
  3336. else if (insn.opcode () == riscv_insn::BNE)
  3337. {
  3338. LONGEST src1, src2;
  3339. regcache->cooked_read (insn.rs1 (), &src1);
  3340. regcache->cooked_read (insn.rs2 (), &src2);
  3341. if (src1 != src2)
  3342. next_pc = pc + insn.imm_signed ();
  3343. }
  3344. else if (insn.opcode () == riscv_insn::BLT)
  3345. {
  3346. LONGEST src1, src2;
  3347. regcache->cooked_read (insn.rs1 (), &src1);
  3348. regcache->cooked_read (insn.rs2 (), &src2);
  3349. if (src1 < src2)
  3350. next_pc = pc + insn.imm_signed ();
  3351. }
  3352. else if (insn.opcode () == riscv_insn::BGE)
  3353. {
  3354. LONGEST src1, src2;
  3355. regcache->cooked_read (insn.rs1 (), &src1);
  3356. regcache->cooked_read (insn.rs2 (), &src2);
  3357. if (src1 >= src2)
  3358. next_pc = pc + insn.imm_signed ();
  3359. }
  3360. else if (insn.opcode () == riscv_insn::BLTU)
  3361. {
  3362. ULONGEST src1, src2;
  3363. regcache->cooked_read (insn.rs1 (), &src1);
  3364. regcache->cooked_read (insn.rs2 (), &src2);
  3365. if (src1 < src2)
  3366. next_pc = pc + insn.imm_signed ();
  3367. }
  3368. else if (insn.opcode () == riscv_insn::BGEU)
  3369. {
  3370. ULONGEST src1, src2;
  3371. regcache->cooked_read (insn.rs1 (), &src1);
  3372. regcache->cooked_read (insn.rs2 (), &src2);
  3373. if (src1 >= src2)
  3374. next_pc = pc + insn.imm_signed ();
  3375. }
  3376. else if (insn.opcode () == riscv_insn::ECALL)
  3377. {
  3378. if (tdep->syscall_next_pc != nullptr)
  3379. next_pc = tdep->syscall_next_pc (get_current_frame ());
  3380. }
  3381. return next_pc;
  3382. }
  3383. /* We can't put a breakpoint in the middle of a lr/sc atomic sequence, so look
  3384. for the end of the sequence and put the breakpoint there. */
  3385. static bool
  3386. riscv_next_pc_atomic_sequence (struct regcache *regcache, CORE_ADDR pc,
  3387. CORE_ADDR *next_pc)
  3388. {
  3389. struct gdbarch *gdbarch = regcache->arch ();
  3390. struct riscv_insn insn;
  3391. CORE_ADDR cur_step_pc = pc;
  3392. CORE_ADDR last_addr = 0;
  3393. /* First instruction has to be a load reserved. */
  3394. insn.decode (gdbarch, cur_step_pc);
  3395. if (insn.opcode () != riscv_insn::LR)
  3396. return false;
  3397. cur_step_pc = cur_step_pc + insn.length ();
  3398. /* Next instruction should be branch to exit. */
  3399. insn.decode (gdbarch, cur_step_pc);
  3400. if (insn.opcode () != riscv_insn::BNE)
  3401. return false;
  3402. last_addr = cur_step_pc + insn.imm_signed ();
  3403. cur_step_pc = cur_step_pc + insn.length ();
  3404. /* Next instruction should be store conditional. */
  3405. insn.decode (gdbarch, cur_step_pc);
  3406. if (insn.opcode () != riscv_insn::SC)
  3407. return false;
  3408. cur_step_pc = cur_step_pc + insn.length ();
  3409. /* Next instruction should be branch to start. */
  3410. insn.decode (gdbarch, cur_step_pc);
  3411. if (insn.opcode () != riscv_insn::BNE)
  3412. return false;
  3413. if (pc != (cur_step_pc + insn.imm_signed ()))
  3414. return false;
  3415. cur_step_pc = cur_step_pc + insn.length ();
  3416. /* We should now be at the end of the sequence. */
  3417. if (cur_step_pc != last_addr)
  3418. return false;
  3419. *next_pc = cur_step_pc;
  3420. return true;
  3421. }
  3422. /* This is called just before we want to resume the inferior, if we want to
  3423. single-step it but there is no hardware or kernel single-step support. We
  3424. find the target of the coming instruction and breakpoint it. */
  3425. std::vector<CORE_ADDR>
  3426. riscv_software_single_step (struct regcache *regcache)
  3427. {
  3428. CORE_ADDR pc, next_pc;
  3429. pc = regcache_read_pc (regcache);
  3430. if (riscv_next_pc_atomic_sequence (regcache, pc, &next_pc))
  3431. return {next_pc};
  3432. next_pc = riscv_next_pc (regcache, pc);
  3433. return {next_pc};
  3434. }
  3435. /* Create RISC-V specific reggroups. */
  3436. static void
  3437. riscv_init_reggroups ()
  3438. {
  3439. csr_reggroup = reggroup_new ("csr", USER_REGGROUP);
  3440. }
  3441. /* See riscv-tdep.h. */
  3442. void
  3443. riscv_supply_regset (const struct regset *regset,
  3444. struct regcache *regcache, int regnum,
  3445. const void *regs, size_t len)
  3446. {
  3447. regcache->supply_regset (regset, regnum, regs, len);
  3448. if (regnum == -1 || regnum == RISCV_ZERO_REGNUM)
  3449. regcache->raw_supply_zeroed (RISCV_ZERO_REGNUM);
  3450. if (regnum == -1 || regnum == RISCV_CSR_FFLAGS_REGNUM
  3451. || regnum == RISCV_CSR_FRM_REGNUM)
  3452. {
  3453. int fcsr_regnum = RISCV_CSR_FCSR_REGNUM;
  3454. /* Ensure that FCSR has been read into REGCACHE. */
  3455. if (regnum != -1)
  3456. regcache->supply_regset (regset, fcsr_regnum, regs, len);
  3457. /* Grab the FCSR value if it is now in the regcache. We must check
  3458. the status first as, if the register was not supplied by REGSET,
  3459. this call will trigger a recursive attempt to fetch the
  3460. registers. */
  3461. if (regcache->get_register_status (fcsr_regnum) == REG_VALID)
  3462. {
  3463. ULONGEST fcsr_val;
  3464. regcache->raw_read (fcsr_regnum, &fcsr_val);
  3465. /* Extract the fflags and frm values. */
  3466. ULONGEST fflags_val = fcsr_val & 0x1f;
  3467. ULONGEST frm_val = (fcsr_val >> 5) & 0x7;
  3468. /* And supply these if needed. */
  3469. if (regnum == -1 || regnum == RISCV_CSR_FFLAGS_REGNUM)
  3470. regcache->raw_supply_integer (RISCV_CSR_FFLAGS_REGNUM,
  3471. (gdb_byte *) &fflags_val,
  3472. sizeof (fflags_val),
  3473. /* is_signed */ false);
  3474. if (regnum == -1 || regnum == RISCV_CSR_FRM_REGNUM)
  3475. regcache->raw_supply_integer (RISCV_CSR_FRM_REGNUM,
  3476. (gdb_byte *)&frm_val,
  3477. sizeof (fflags_val),
  3478. /* is_signed */ false);
  3479. }
  3480. }
  3481. }
  3482. void _initialize_riscv_tdep ();
  3483. void
  3484. _initialize_riscv_tdep ()
  3485. {
  3486. riscv_init_reggroups ();
  3487. gdbarch_register (bfd_arch_riscv, riscv_gdbarch_init, NULL);
  3488. /* Add root prefix command for all "set debug riscv" and "show debug
  3489. riscv" commands. */
  3490. add_setshow_prefix_cmd ("riscv", no_class,
  3491. _("RISC-V specific debug commands."),
  3492. _("RISC-V specific debug commands."),
  3493. &setdebugriscvcmdlist, &showdebugriscvcmdlist,
  3494. &setdebuglist, &showdebuglist);
  3495. add_setshow_zuinteger_cmd ("breakpoints", class_maintenance,
  3496. &riscv_debug_breakpoints, _("\
  3497. Set riscv breakpoint debugging."), _("\
  3498. Show riscv breakpoint debugging."), _("\
  3499. When non-zero, print debugging information for the riscv specific parts\n\
  3500. of the breakpoint mechanism."),
  3501. NULL,
  3502. show_riscv_debug_variable,
  3503. &setdebugriscvcmdlist, &showdebugriscvcmdlist);
  3504. add_setshow_zuinteger_cmd ("infcall", class_maintenance,
  3505. &riscv_debug_infcall, _("\
  3506. Set riscv inferior call debugging."), _("\
  3507. Show riscv inferior call debugging."), _("\
  3508. When non-zero, print debugging information for the riscv specific parts\n\
  3509. of the inferior call mechanism."),
  3510. NULL,
  3511. show_riscv_debug_variable,
  3512. &setdebugriscvcmdlist, &showdebugriscvcmdlist);
  3513. add_setshow_zuinteger_cmd ("unwinder", class_maintenance,
  3514. &riscv_debug_unwinder, _("\
  3515. Set riscv stack unwinding debugging."), _("\
  3516. Show riscv stack unwinding debugging."), _("\
  3517. When non-zero, print debugging information for the riscv specific parts\n\
  3518. of the stack unwinding mechanism."),
  3519. NULL,
  3520. show_riscv_debug_variable,
  3521. &setdebugriscvcmdlist, &showdebugriscvcmdlist);
  3522. add_setshow_zuinteger_cmd ("gdbarch", class_maintenance,
  3523. &riscv_debug_gdbarch, _("\
  3524. Set riscv gdbarch initialisation debugging."), _("\
  3525. Show riscv gdbarch initialisation debugging."), _("\
  3526. When non-zero, print debugging information for the riscv gdbarch\n\
  3527. initialisation process."),
  3528. NULL,
  3529. show_riscv_debug_variable,
  3530. &setdebugriscvcmdlist, &showdebugriscvcmdlist);
  3531. /* Add root prefix command for all "set riscv" and "show riscv" commands. */
  3532. add_setshow_prefix_cmd ("riscv", no_class,
  3533. _("RISC-V specific commands."),
  3534. _("RISC-V specific commands."),
  3535. &setriscvcmdlist, &showriscvcmdlist,
  3536. &setlist, &showlist);
  3537. use_compressed_breakpoints = AUTO_BOOLEAN_AUTO;
  3538. add_setshow_auto_boolean_cmd ("use-compressed-breakpoints", no_class,
  3539. &use_compressed_breakpoints,
  3540. _("\
  3541. Set debugger's use of compressed breakpoints."), _(" \
  3542. Show debugger's use of compressed breakpoints."), _("\
  3543. Debugging compressed code requires compressed breakpoints to be used. If\n\
  3544. left to 'auto' then gdb will use them if the existing instruction is a\n\
  3545. compressed instruction. If that doesn't give the correct behavior, then\n\
  3546. this option can be used."),
  3547. NULL,
  3548. show_use_compressed_breakpoints,
  3549. &setriscvcmdlist,
  3550. &showriscvcmdlist);
  3551. }