sparc64-tdep.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187
  1. /* Target-dependent code for UltraSPARC.
  2. Copyright (C) 2003-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "arch-utils.h"
  16. #include "dwarf2/frame.h"
  17. #include "frame.h"
  18. #include "frame-base.h"
  19. #include "frame-unwind.h"
  20. #include "gdbcore.h"
  21. #include "gdbtypes.h"
  22. #include "inferior.h"
  23. #include "symtab.h"
  24. #include "objfiles.h"
  25. #include "osabi.h"
  26. #include "regcache.h"
  27. #include "target-descriptions.h"
  28. #include "target.h"
  29. #include "value.h"
  30. #include "sparc64-tdep.h"
  31. #include <forward_list>
  32. /* This file implements the SPARC 64-bit ABI as defined by the
  33. section "Low-Level System Information" of the SPARC Compliance
  34. Definition (SCD) 2.4.1, which is the 64-bit System V psABI for
  35. SPARC. */
  36. /* Please use the sparc32_-prefix for 32-bit specific code, the
  37. sparc64_-prefix for 64-bit specific code and the sparc_-prefix for
  38. code can handle both. */
  39. /* The M7 processor supports an Application Data Integrity (ADI) feature
  40. that detects invalid data accesses. When software allocates memory and
  41. enables ADI on the allocated memory, it chooses a 4-bit version number,
  42. sets the version in the upper 4 bits of the 64-bit pointer to that data,
  43. and stores the 4-bit version in every cacheline of the object. Hardware
  44. saves the latter in spare bits in the cache and memory hierarchy. On each
  45. load and store, the processor compares the upper 4 VA (virtual address) bits
  46. to the cacheline's version. If there is a mismatch, the processor generates
  47. a version mismatch trap which can be either precise or disrupting.
  48. The trap is an error condition which the kernel delivers to the process
  49. as a SIGSEGV signal.
  50. The upper 4 bits of the VA represent a version and are not part of the
  51. true address. The processor clears these bits and sign extends bit 59
  52. to generate the true address.
  53. Note that 32-bit applications cannot use ADI. */
  54. #include <algorithm>
  55. #include "cli/cli-utils.h"
  56. #include "gdbcmd.h"
  57. #include "auxv.h"
  58. #define MAX_PROC_NAME_SIZE sizeof("/proc/99999/lwp/9999/adi/lstatus")
  59. /* ELF Auxiliary vectors */
  60. #ifndef AT_ADI_BLKSZ
  61. #define AT_ADI_BLKSZ 34
  62. #endif
  63. #ifndef AT_ADI_NBITS
  64. #define AT_ADI_NBITS 35
  65. #endif
  66. #ifndef AT_ADI_UEONADI
  67. #define AT_ADI_UEONADI 36
  68. #endif
  69. /* ADI command list. */
  70. static struct cmd_list_element *sparc64adilist = NULL;
  71. /* ADI stat settings. */
  72. struct adi_stat_t
  73. {
  74. /* The ADI block size. */
  75. unsigned long blksize;
  76. /* Number of bits used for an ADI version tag which can be
  77. used together with the shift value for an ADI version tag
  78. to encode or extract the ADI version value in a pointer. */
  79. unsigned long nbits;
  80. /* The maximum ADI version tag value supported. */
  81. int max_version;
  82. /* ADI version tag file. */
  83. int tag_fd = 0;
  84. /* ADI availability check has been done. */
  85. bool checked_avail = false;
  86. /* ADI is available. */
  87. bool is_avail = false;
  88. };
  89. /* Per-process ADI stat info. */
  90. struct sparc64_adi_info
  91. {
  92. sparc64_adi_info (pid_t pid_)
  93. : pid (pid_)
  94. {}
  95. /* The process identifier. */
  96. pid_t pid;
  97. /* The ADI stat. */
  98. adi_stat_t stat = {};
  99. };
  100. static std::forward_list<sparc64_adi_info> adi_proc_list;
  101. /* Get ADI info for process PID, creating one if it doesn't exist. */
  102. static sparc64_adi_info *
  103. get_adi_info_proc (pid_t pid)
  104. {
  105. auto found = std::find_if (adi_proc_list.begin (), adi_proc_list.end (),
  106. [&pid] (const sparc64_adi_info &info)
  107. {
  108. return info.pid == pid;
  109. });
  110. if (found == adi_proc_list.end ())
  111. {
  112. adi_proc_list.emplace_front (pid);
  113. return &adi_proc_list.front ();
  114. }
  115. else
  116. {
  117. return &(*found);
  118. }
  119. }
  120. static adi_stat_t
  121. get_adi_info (pid_t pid)
  122. {
  123. sparc64_adi_info *proc;
  124. proc = get_adi_info_proc (pid);
  125. return proc->stat;
  126. }
  127. /* Is called when GDB is no longer debugging process PID. It
  128. deletes data structure that keeps track of the ADI stat. */
  129. void
  130. sparc64_forget_process (pid_t pid)
  131. {
  132. int target_errno;
  133. for (auto pit = adi_proc_list.before_begin (),
  134. it = std::next (pit);
  135. it != adi_proc_list.end ();
  136. )
  137. {
  138. if ((*it).pid == pid)
  139. {
  140. if ((*it).stat.tag_fd > 0)
  141. target_fileio_close ((*it).stat.tag_fd, &target_errno);
  142. adi_proc_list.erase_after (pit);
  143. break;
  144. }
  145. else
  146. pit = it++;
  147. }
  148. }
  149. /* Read attributes of a maps entry in /proc/[pid]/adi/maps. */
  150. static void
  151. read_maps_entry (const char *line,
  152. ULONGEST *addr, ULONGEST *endaddr)
  153. {
  154. const char *p = line;
  155. *addr = strtoulst (p, &p, 16);
  156. if (*p == '-')
  157. p++;
  158. *endaddr = strtoulst (p, &p, 16);
  159. }
  160. /* Check if ADI is available. */
  161. static bool
  162. adi_available (void)
  163. {
  164. pid_t pid = inferior_ptid.pid ();
  165. sparc64_adi_info *proc = get_adi_info_proc (pid);
  166. CORE_ADDR value;
  167. if (proc->stat.checked_avail)
  168. return proc->stat.is_avail;
  169. proc->stat.checked_avail = true;
  170. if (target_auxv_search (current_inferior ()->top_target (),
  171. AT_ADI_BLKSZ, &value) <= 0)
  172. return false;
  173. proc->stat.blksize = value;
  174. target_auxv_search (current_inferior ()->top_target (),
  175. AT_ADI_NBITS, &value);
  176. proc->stat.nbits = value;
  177. proc->stat.max_version = (1 << proc->stat.nbits) - 2;
  178. proc->stat.is_avail = true;
  179. return proc->stat.is_avail;
  180. }
  181. /* Normalize a versioned address - a VA with ADI bits (63-60) set. */
  182. static CORE_ADDR
  183. adi_normalize_address (CORE_ADDR addr)
  184. {
  185. adi_stat_t ast = get_adi_info (inferior_ptid.pid ());
  186. if (ast.nbits)
  187. {
  188. /* Clear upper bits. */
  189. addr &= ((uint64_t) -1) >> ast.nbits;
  190. /* Sign extend. */
  191. CORE_ADDR signbit = (uint64_t) 1 << (64 - ast.nbits - 1);
  192. return (addr ^ signbit) - signbit;
  193. }
  194. return addr;
  195. }
  196. /* Align a normalized address - a VA with bit 59 sign extended into
  197. ADI bits. */
  198. static CORE_ADDR
  199. adi_align_address (CORE_ADDR naddr)
  200. {
  201. adi_stat_t ast = get_adi_info (inferior_ptid.pid ());
  202. return (naddr - (naddr % ast.blksize)) / ast.blksize;
  203. }
  204. /* Convert a byte count to count at a ratio of 1:adi_blksz. */
  205. static int
  206. adi_convert_byte_count (CORE_ADDR naddr, int nbytes, CORE_ADDR locl)
  207. {
  208. adi_stat_t ast = get_adi_info (inferior_ptid.pid ());
  209. return ((naddr + nbytes + ast.blksize - 1) / ast.blksize) - locl;
  210. }
  211. /* The /proc/[pid]/adi/tags file, which allows gdb to get/set ADI
  212. version in a target process, maps linearly to the address space
  213. of the target process at a ratio of 1:adi_blksz.
  214. A read (or write) at offset K in the file returns (or modifies)
  215. the ADI version tag stored in the cacheline containing address
  216. K * adi_blksz, encoded as 1 version tag per byte. The allowed
  217. version tag values are between 0 and adi_stat.max_version. */
  218. static int
  219. adi_tag_fd (void)
  220. {
  221. pid_t pid = inferior_ptid.pid ();
  222. sparc64_adi_info *proc = get_adi_info_proc (pid);
  223. if (proc->stat.tag_fd != 0)
  224. return proc->stat.tag_fd;
  225. char cl_name[MAX_PROC_NAME_SIZE];
  226. snprintf (cl_name, sizeof(cl_name), "/proc/%ld/adi/tags", (long) pid);
  227. int target_errno;
  228. proc->stat.tag_fd = target_fileio_open (NULL, cl_name, O_RDWR|O_EXCL,
  229. false, 0, &target_errno);
  230. return proc->stat.tag_fd;
  231. }
  232. /* Check if an address set is ADI enabled, using /proc/[pid]/adi/maps
  233. which was exported by the kernel and contains the currently ADI
  234. mapped memory regions and their access permissions. */
  235. static bool
  236. adi_is_addr_mapped (CORE_ADDR vaddr, size_t cnt)
  237. {
  238. char filename[MAX_PROC_NAME_SIZE];
  239. size_t i = 0;
  240. pid_t pid = inferior_ptid.pid ();
  241. snprintf (filename, sizeof filename, "/proc/%ld/adi/maps", (long) pid);
  242. gdb::unique_xmalloc_ptr<char> data
  243. = target_fileio_read_stralloc (NULL, filename);
  244. if (data)
  245. {
  246. adi_stat_t adi_stat = get_adi_info (pid);
  247. char *saveptr;
  248. for (char *line = strtok_r (data.get (), "\n", &saveptr);
  249. line;
  250. line = strtok_r (NULL, "\n", &saveptr))
  251. {
  252. ULONGEST addr, endaddr;
  253. read_maps_entry (line, &addr, &endaddr);
  254. while (((vaddr + i) * adi_stat.blksize) >= addr
  255. && ((vaddr + i) * adi_stat.blksize) < endaddr)
  256. {
  257. if (++i == cnt)
  258. return true;
  259. }
  260. }
  261. }
  262. else
  263. warning (_("unable to open /proc file '%s'"), filename);
  264. return false;
  265. }
  266. /* Read ADI version tag value for memory locations starting at "VADDR"
  267. for "SIZE" number of bytes. */
  268. static int
  269. adi_read_versions (CORE_ADDR vaddr, size_t size, gdb_byte *tags)
  270. {
  271. int fd = adi_tag_fd ();
  272. if (fd == -1)
  273. return -1;
  274. if (!adi_is_addr_mapped (vaddr, size))
  275. {
  276. adi_stat_t ast = get_adi_info (inferior_ptid.pid ());
  277. error(_("Address at %s is not in ADI maps"),
  278. paddress (target_gdbarch (), vaddr * ast.blksize));
  279. }
  280. int target_errno;
  281. return target_fileio_pread (fd, tags, size, vaddr, &target_errno);
  282. }
  283. /* Write ADI version tag for memory locations starting at "VADDR" for
  284. "SIZE" number of bytes to "TAGS". */
  285. static int
  286. adi_write_versions (CORE_ADDR vaddr, size_t size, unsigned char *tags)
  287. {
  288. int fd = adi_tag_fd ();
  289. if (fd == -1)
  290. return -1;
  291. if (!adi_is_addr_mapped (vaddr, size))
  292. {
  293. adi_stat_t ast = get_adi_info (inferior_ptid.pid ());
  294. error(_("Address at %s is not in ADI maps"),
  295. paddress (target_gdbarch (), vaddr * ast.blksize));
  296. }
  297. int target_errno;
  298. return target_fileio_pwrite (fd, tags, size, vaddr, &target_errno);
  299. }
  300. /* Print ADI version tag value in "TAGS" for memory locations starting
  301. at "VADDR" with number of "CNT". */
  302. static void
  303. adi_print_versions (CORE_ADDR vaddr, size_t cnt, gdb_byte *tags)
  304. {
  305. int v_idx = 0;
  306. const int maxelts = 8; /* # of elements per line */
  307. adi_stat_t adi_stat = get_adi_info (inferior_ptid.pid ());
  308. while (cnt > 0)
  309. {
  310. QUIT;
  311. gdb_printf ("%s:\t",
  312. paddress (target_gdbarch (), vaddr * adi_stat.blksize));
  313. for (int i = maxelts; i > 0 && cnt > 0; i--, cnt--)
  314. {
  315. if (tags[v_idx] == 0xff) /* no version tag */
  316. gdb_printf ("-");
  317. else
  318. gdb_printf ("%1X", tags[v_idx]);
  319. if (cnt > 1)
  320. gdb_printf (" ");
  321. ++v_idx;
  322. }
  323. gdb_printf ("\n");
  324. vaddr += maxelts;
  325. }
  326. }
  327. static void
  328. do_examine (CORE_ADDR start, int bcnt)
  329. {
  330. CORE_ADDR vaddr = adi_normalize_address (start);
  331. CORE_ADDR vstart = adi_align_address (vaddr);
  332. int cnt = adi_convert_byte_count (vaddr, bcnt, vstart);
  333. gdb::def_vector<gdb_byte> buf (cnt);
  334. int read_cnt = adi_read_versions (vstart, cnt, buf.data ());
  335. if (read_cnt == -1)
  336. error (_("No ADI information"));
  337. else if (read_cnt < cnt)
  338. error(_("No ADI information at %s"), paddress (target_gdbarch (), vaddr));
  339. adi_print_versions (vstart, cnt, buf.data ());
  340. }
  341. static void
  342. do_assign (CORE_ADDR start, size_t bcnt, int version)
  343. {
  344. CORE_ADDR vaddr = adi_normalize_address (start);
  345. CORE_ADDR vstart = adi_align_address (vaddr);
  346. int cnt = adi_convert_byte_count (vaddr, bcnt, vstart);
  347. std::vector<unsigned char> buf (cnt, version);
  348. int set_cnt = adi_write_versions (vstart, cnt, buf.data ());
  349. if (set_cnt == -1)
  350. error (_("No ADI information"));
  351. else if (set_cnt < cnt)
  352. error(_("No ADI information at %s"), paddress (target_gdbarch (), vaddr));
  353. }
  354. /* ADI examine version tag command.
  355. Command syntax:
  356. adi (examine|x)[/COUNT] [ADDR] */
  357. static void
  358. adi_examine_command (const char *args, int from_tty)
  359. {
  360. /* make sure program is active and adi is available */
  361. if (!target_has_execution ())
  362. error (_("ADI command requires a live process/thread"));
  363. if (!adi_available ())
  364. error (_("No ADI information"));
  365. int cnt = 1;
  366. const char *p = args;
  367. if (p && *p == '/')
  368. {
  369. p++;
  370. cnt = get_number (&p);
  371. }
  372. CORE_ADDR next_address = 0;
  373. if (p != 0 && *p != 0)
  374. next_address = parse_and_eval_address (p);
  375. if (!cnt || !next_address)
  376. error (_("Usage: adi examine|x[/COUNT] [ADDR]"));
  377. do_examine (next_address, cnt);
  378. }
  379. /* ADI assign version tag command.
  380. Command syntax:
  381. adi (assign|a)[/COUNT] ADDR = VERSION */
  382. static void
  383. adi_assign_command (const char *args, int from_tty)
  384. {
  385. static const char *adi_usage
  386. = N_("Usage: adi assign|a[/COUNT] ADDR = VERSION");
  387. /* make sure program is active and adi is available */
  388. if (!target_has_execution ())
  389. error (_("ADI command requires a live process/thread"));
  390. if (!adi_available ())
  391. error (_("No ADI information"));
  392. const char *exp = args;
  393. if (exp == 0)
  394. error_no_arg (_(adi_usage));
  395. char *q = (char *) strchr (exp, '=');
  396. if (q)
  397. *q++ = 0;
  398. else
  399. error ("%s", _(adi_usage));
  400. size_t cnt = 1;
  401. const char *p = args;
  402. if (exp && *exp == '/')
  403. {
  404. p = exp + 1;
  405. cnt = get_number (&p);
  406. }
  407. CORE_ADDR next_address = 0;
  408. if (p != 0 && *p != 0)
  409. next_address = parse_and_eval_address (p);
  410. else
  411. error ("%s", _(adi_usage));
  412. int version = 0;
  413. if (q != NULL) /* parse version tag */
  414. {
  415. adi_stat_t ast = get_adi_info (inferior_ptid.pid ());
  416. version = parse_and_eval_long (q);
  417. if (version < 0 || version > ast.max_version)
  418. error (_("Invalid ADI version tag %d"), version);
  419. }
  420. do_assign (next_address, cnt, version);
  421. }
  422. void _initialize_sparc64_adi_tdep ();
  423. void
  424. _initialize_sparc64_adi_tdep ()
  425. {
  426. add_basic_prefix_cmd ("adi", class_support,
  427. _("ADI version related commands."),
  428. &sparc64adilist, 0, &cmdlist);
  429. cmd_list_element *adi_examine_cmd
  430. = add_cmd ("examine", class_support, adi_examine_command,
  431. _("Examine ADI versions."), &sparc64adilist);
  432. add_alias_cmd ("x", adi_examine_cmd, no_class, 1, &sparc64adilist);
  433. add_cmd ("assign", class_support, adi_assign_command,
  434. _("Assign ADI versions."), &sparc64adilist);
  435. }
  436. /* The functions on this page are intended to be used to classify
  437. function arguments. */
  438. /* Check whether TYPE is "Integral or Pointer". */
  439. static int
  440. sparc64_integral_or_pointer_p (const struct type *type)
  441. {
  442. switch (type->code ())
  443. {
  444. case TYPE_CODE_INT:
  445. case TYPE_CODE_BOOL:
  446. case TYPE_CODE_CHAR:
  447. case TYPE_CODE_ENUM:
  448. case TYPE_CODE_RANGE:
  449. {
  450. int len = TYPE_LENGTH (type);
  451. gdb_assert (len == 1 || len == 2 || len == 4 || len == 8);
  452. }
  453. return 1;
  454. case TYPE_CODE_PTR:
  455. case TYPE_CODE_REF:
  456. case TYPE_CODE_RVALUE_REF:
  457. {
  458. int len = TYPE_LENGTH (type);
  459. gdb_assert (len == 8);
  460. }
  461. return 1;
  462. default:
  463. break;
  464. }
  465. return 0;
  466. }
  467. /* Check whether TYPE is "Floating". */
  468. static int
  469. sparc64_floating_p (const struct type *type)
  470. {
  471. switch (type->code ())
  472. {
  473. case TYPE_CODE_FLT:
  474. {
  475. int len = TYPE_LENGTH (type);
  476. gdb_assert (len == 4 || len == 8 || len == 16);
  477. }
  478. return 1;
  479. default:
  480. break;
  481. }
  482. return 0;
  483. }
  484. /* Check whether TYPE is "Complex Floating". */
  485. static int
  486. sparc64_complex_floating_p (const struct type *type)
  487. {
  488. switch (type->code ())
  489. {
  490. case TYPE_CODE_COMPLEX:
  491. {
  492. int len = TYPE_LENGTH (type);
  493. gdb_assert (len == 8 || len == 16 || len == 32);
  494. }
  495. return 1;
  496. default:
  497. break;
  498. }
  499. return 0;
  500. }
  501. /* Check whether TYPE is "Structure or Union".
  502. In terms of Ada subprogram calls, arrays are treated the same as
  503. struct and union types. So this function also returns non-zero
  504. for array types. */
  505. static int
  506. sparc64_structure_or_union_p (const struct type *type)
  507. {
  508. switch (type->code ())
  509. {
  510. case TYPE_CODE_STRUCT:
  511. case TYPE_CODE_UNION:
  512. case TYPE_CODE_ARRAY:
  513. return 1;
  514. default:
  515. break;
  516. }
  517. return 0;
  518. }
  519. /* Construct types for ISA-specific registers. */
  520. static struct type *
  521. sparc64_pstate_type (struct gdbarch *gdbarch)
  522. {
  523. sparc_gdbarch_tdep *tdep = (sparc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  524. if (!tdep->sparc64_pstate_type)
  525. {
  526. struct type *type;
  527. type = arch_flags_type (gdbarch, "builtin_type_sparc64_pstate", 64);
  528. append_flags_type_flag (type, 0, "AG");
  529. append_flags_type_flag (type, 1, "IE");
  530. append_flags_type_flag (type, 2, "PRIV");
  531. append_flags_type_flag (type, 3, "AM");
  532. append_flags_type_flag (type, 4, "PEF");
  533. append_flags_type_flag (type, 5, "RED");
  534. append_flags_type_flag (type, 8, "TLE");
  535. append_flags_type_flag (type, 9, "CLE");
  536. append_flags_type_flag (type, 10, "PID0");
  537. append_flags_type_flag (type, 11, "PID1");
  538. tdep->sparc64_pstate_type = type;
  539. }
  540. return tdep->sparc64_pstate_type;
  541. }
  542. static struct type *
  543. sparc64_ccr_type (struct gdbarch *gdbarch)
  544. {
  545. sparc_gdbarch_tdep *tdep = (sparc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  546. if (tdep->sparc64_ccr_type == NULL)
  547. {
  548. struct type *type;
  549. type = arch_flags_type (gdbarch, "builtin_type_sparc64_ccr", 64);
  550. append_flags_type_flag (type, 0, "icc.c");
  551. append_flags_type_flag (type, 1, "icc.v");
  552. append_flags_type_flag (type, 2, "icc.z");
  553. append_flags_type_flag (type, 3, "icc.n");
  554. append_flags_type_flag (type, 4, "xcc.c");
  555. append_flags_type_flag (type, 5, "xcc.v");
  556. append_flags_type_flag (type, 6, "xcc.z");
  557. append_flags_type_flag (type, 7, "xcc.n");
  558. tdep->sparc64_ccr_type = type;
  559. }
  560. return tdep->sparc64_ccr_type;
  561. }
  562. static struct type *
  563. sparc64_fsr_type (struct gdbarch *gdbarch)
  564. {
  565. sparc_gdbarch_tdep *tdep = (sparc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  566. if (!tdep->sparc64_fsr_type)
  567. {
  568. struct type *type;
  569. type = arch_flags_type (gdbarch, "builtin_type_sparc64_fsr", 64);
  570. append_flags_type_flag (type, 0, "NXC");
  571. append_flags_type_flag (type, 1, "DZC");
  572. append_flags_type_flag (type, 2, "UFC");
  573. append_flags_type_flag (type, 3, "OFC");
  574. append_flags_type_flag (type, 4, "NVC");
  575. append_flags_type_flag (type, 5, "NXA");
  576. append_flags_type_flag (type, 6, "DZA");
  577. append_flags_type_flag (type, 7, "UFA");
  578. append_flags_type_flag (type, 8, "OFA");
  579. append_flags_type_flag (type, 9, "NVA");
  580. append_flags_type_flag (type, 22, "NS");
  581. append_flags_type_flag (type, 23, "NXM");
  582. append_flags_type_flag (type, 24, "DZM");
  583. append_flags_type_flag (type, 25, "UFM");
  584. append_flags_type_flag (type, 26, "OFM");
  585. append_flags_type_flag (type, 27, "NVM");
  586. tdep->sparc64_fsr_type = type;
  587. }
  588. return tdep->sparc64_fsr_type;
  589. }
  590. static struct type *
  591. sparc64_fprs_type (struct gdbarch *gdbarch)
  592. {
  593. sparc_gdbarch_tdep *tdep = (sparc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  594. if (!tdep->sparc64_fprs_type)
  595. {
  596. struct type *type;
  597. type = arch_flags_type (gdbarch, "builtin_type_sparc64_fprs", 64);
  598. append_flags_type_flag (type, 0, "DL");
  599. append_flags_type_flag (type, 1, "DU");
  600. append_flags_type_flag (type, 2, "FEF");
  601. tdep->sparc64_fprs_type = type;
  602. }
  603. return tdep->sparc64_fprs_type;
  604. }
  605. /* Register information. */
  606. #define SPARC64_FPU_REGISTERS \
  607. "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7", \
  608. "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15", \
  609. "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23", \
  610. "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31", \
  611. "f32", "f34", "f36", "f38", "f40", "f42", "f44", "f46", \
  612. "f48", "f50", "f52", "f54", "f56", "f58", "f60", "f62"
  613. #define SPARC64_CP0_REGISTERS \
  614. "pc", "npc", \
  615. /* FIXME: Give "state" a name until we start using register groups. */ \
  616. "state", \
  617. "fsr", \
  618. "fprs", \
  619. "y"
  620. static const char * const sparc64_fpu_register_names[] = {
  621. SPARC64_FPU_REGISTERS
  622. };
  623. static const char * const sparc64_cp0_register_names[] = {
  624. SPARC64_CP0_REGISTERS
  625. };
  626. static const char * const sparc64_register_names[] =
  627. {
  628. SPARC_CORE_REGISTERS,
  629. SPARC64_FPU_REGISTERS,
  630. SPARC64_CP0_REGISTERS
  631. };
  632. /* Total number of registers. */
  633. #define SPARC64_NUM_REGS ARRAY_SIZE (sparc64_register_names)
  634. /* We provide the aliases %d0..%d62 and %q0..%q60 for the floating
  635. registers as "psuedo" registers. */
  636. static const char * const sparc64_pseudo_register_names[] =
  637. {
  638. "cwp", "pstate", "asi", "ccr",
  639. "d0", "d2", "d4", "d6", "d8", "d10", "d12", "d14",
  640. "d16", "d18", "d20", "d22", "d24", "d26", "d28", "d30",
  641. "d32", "d34", "d36", "d38", "d40", "d42", "d44", "d46",
  642. "d48", "d50", "d52", "d54", "d56", "d58", "d60", "d62",
  643. "q0", "q4", "q8", "q12", "q16", "q20", "q24", "q28",
  644. "q32", "q36", "q40", "q44", "q48", "q52", "q56", "q60",
  645. };
  646. /* Total number of pseudo registers. */
  647. #define SPARC64_NUM_PSEUDO_REGS ARRAY_SIZE (sparc64_pseudo_register_names)
  648. /* Return the name of pseudo register REGNUM. */
  649. static const char *
  650. sparc64_pseudo_register_name (struct gdbarch *gdbarch, int regnum)
  651. {
  652. regnum -= gdbarch_num_regs (gdbarch);
  653. if (regnum < SPARC64_NUM_PSEUDO_REGS)
  654. return sparc64_pseudo_register_names[regnum];
  655. internal_error (__FILE__, __LINE__,
  656. _("sparc64_pseudo_register_name: bad register number %d"),
  657. regnum);
  658. }
  659. /* Return the name of register REGNUM. */
  660. static const char *
  661. sparc64_register_name (struct gdbarch *gdbarch, int regnum)
  662. {
  663. if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
  664. return tdesc_register_name (gdbarch, regnum);
  665. if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch))
  666. return sparc64_register_names[regnum];
  667. return sparc64_pseudo_register_name (gdbarch, regnum);
  668. }
  669. /* Return the GDB type object for the "standard" data type of data in
  670. pseudo register REGNUM. */
  671. static struct type *
  672. sparc64_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
  673. {
  674. regnum -= gdbarch_num_regs (gdbarch);
  675. if (regnum == SPARC64_CWP_REGNUM)
  676. return builtin_type (gdbarch)->builtin_int64;
  677. if (regnum == SPARC64_PSTATE_REGNUM)
  678. return sparc64_pstate_type (gdbarch);
  679. if (regnum == SPARC64_ASI_REGNUM)
  680. return builtin_type (gdbarch)->builtin_int64;
  681. if (regnum == SPARC64_CCR_REGNUM)
  682. return sparc64_ccr_type (gdbarch);
  683. if (regnum >= SPARC64_D0_REGNUM && regnum <= SPARC64_D62_REGNUM)
  684. return builtin_type (gdbarch)->builtin_double;
  685. if (regnum >= SPARC64_Q0_REGNUM && regnum <= SPARC64_Q60_REGNUM)
  686. return builtin_type (gdbarch)->builtin_long_double;
  687. internal_error (__FILE__, __LINE__,
  688. _("sparc64_pseudo_register_type: bad register number %d"),
  689. regnum);
  690. }
  691. /* Return the GDB type object for the "standard" data type of data in
  692. register REGNUM. */
  693. static struct type *
  694. sparc64_register_type (struct gdbarch *gdbarch, int regnum)
  695. {
  696. if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
  697. return tdesc_register_type (gdbarch, regnum);
  698. /* Raw registers. */
  699. if (regnum == SPARC_SP_REGNUM || regnum == SPARC_FP_REGNUM)
  700. return builtin_type (gdbarch)->builtin_data_ptr;
  701. if (regnum >= SPARC_G0_REGNUM && regnum <= SPARC_I7_REGNUM)
  702. return builtin_type (gdbarch)->builtin_int64;
  703. if (regnum >= SPARC_F0_REGNUM && regnum <= SPARC_F31_REGNUM)
  704. return builtin_type (gdbarch)->builtin_float;
  705. if (regnum >= SPARC64_F32_REGNUM && regnum <= SPARC64_F62_REGNUM)
  706. return builtin_type (gdbarch)->builtin_double;
  707. if (regnum == SPARC64_PC_REGNUM || regnum == SPARC64_NPC_REGNUM)
  708. return builtin_type (gdbarch)->builtin_func_ptr;
  709. /* This raw register contains the contents of %cwp, %pstate, %asi
  710. and %ccr as laid out in a %tstate register. */
  711. if (regnum == SPARC64_STATE_REGNUM)
  712. return builtin_type (gdbarch)->builtin_int64;
  713. if (regnum == SPARC64_FSR_REGNUM)
  714. return sparc64_fsr_type (gdbarch);
  715. if (regnum == SPARC64_FPRS_REGNUM)
  716. return sparc64_fprs_type (gdbarch);
  717. /* "Although Y is a 64-bit register, its high-order 32 bits are
  718. reserved and always read as 0." */
  719. if (regnum == SPARC64_Y_REGNUM)
  720. return builtin_type (gdbarch)->builtin_int64;
  721. /* Pseudo registers. */
  722. if (regnum >= gdbarch_num_regs (gdbarch))
  723. return sparc64_pseudo_register_type (gdbarch, regnum);
  724. internal_error (__FILE__, __LINE__, _("invalid regnum"));
  725. }
  726. static enum register_status
  727. sparc64_pseudo_register_read (struct gdbarch *gdbarch,
  728. readable_regcache *regcache,
  729. int regnum, gdb_byte *buf)
  730. {
  731. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  732. enum register_status status;
  733. regnum -= gdbarch_num_regs (gdbarch);
  734. if (regnum >= SPARC64_D0_REGNUM && regnum <= SPARC64_D30_REGNUM)
  735. {
  736. regnum = SPARC_F0_REGNUM + 2 * (regnum - SPARC64_D0_REGNUM);
  737. status = regcache->raw_read (regnum, buf);
  738. if (status == REG_VALID)
  739. status = regcache->raw_read (regnum + 1, buf + 4);
  740. return status;
  741. }
  742. else if (regnum >= SPARC64_D32_REGNUM && regnum <= SPARC64_D62_REGNUM)
  743. {
  744. regnum = SPARC64_F32_REGNUM + (regnum - SPARC64_D32_REGNUM);
  745. return regcache->raw_read (regnum, buf);
  746. }
  747. else if (regnum >= SPARC64_Q0_REGNUM && regnum <= SPARC64_Q28_REGNUM)
  748. {
  749. regnum = SPARC_F0_REGNUM + 4 * (regnum - SPARC64_Q0_REGNUM);
  750. status = regcache->raw_read (regnum, buf);
  751. if (status == REG_VALID)
  752. status = regcache->raw_read (regnum + 1, buf + 4);
  753. if (status == REG_VALID)
  754. status = regcache->raw_read (regnum + 2, buf + 8);
  755. if (status == REG_VALID)
  756. status = regcache->raw_read (regnum + 3, buf + 12);
  757. return status;
  758. }
  759. else if (regnum >= SPARC64_Q32_REGNUM && regnum <= SPARC64_Q60_REGNUM)
  760. {
  761. regnum = SPARC64_F32_REGNUM + 2 * (regnum - SPARC64_Q32_REGNUM);
  762. status = regcache->raw_read (regnum, buf);
  763. if (status == REG_VALID)
  764. status = regcache->raw_read (regnum + 1, buf + 8);
  765. return status;
  766. }
  767. else if (regnum == SPARC64_CWP_REGNUM
  768. || regnum == SPARC64_PSTATE_REGNUM
  769. || regnum == SPARC64_ASI_REGNUM
  770. || regnum == SPARC64_CCR_REGNUM)
  771. {
  772. ULONGEST state;
  773. status = regcache->raw_read (SPARC64_STATE_REGNUM, &state);
  774. if (status != REG_VALID)
  775. return status;
  776. switch (regnum)
  777. {
  778. case SPARC64_CWP_REGNUM:
  779. state = (state >> 0) & ((1 << 5) - 1);
  780. break;
  781. case SPARC64_PSTATE_REGNUM:
  782. state = (state >> 8) & ((1 << 12) - 1);
  783. break;
  784. case SPARC64_ASI_REGNUM:
  785. state = (state >> 24) & ((1 << 8) - 1);
  786. break;
  787. case SPARC64_CCR_REGNUM:
  788. state = (state >> 32) & ((1 << 8) - 1);
  789. break;
  790. }
  791. store_unsigned_integer (buf, 8, byte_order, state);
  792. }
  793. return REG_VALID;
  794. }
  795. static void
  796. sparc64_pseudo_register_write (struct gdbarch *gdbarch,
  797. struct regcache *regcache,
  798. int regnum, const gdb_byte *buf)
  799. {
  800. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  801. regnum -= gdbarch_num_regs (gdbarch);
  802. if (regnum >= SPARC64_D0_REGNUM && regnum <= SPARC64_D30_REGNUM)
  803. {
  804. regnum = SPARC_F0_REGNUM + 2 * (regnum - SPARC64_D0_REGNUM);
  805. regcache->raw_write (regnum, buf);
  806. regcache->raw_write (regnum + 1, buf + 4);
  807. }
  808. else if (regnum >= SPARC64_D32_REGNUM && regnum <= SPARC64_D62_REGNUM)
  809. {
  810. regnum = SPARC64_F32_REGNUM + (regnum - SPARC64_D32_REGNUM);
  811. regcache->raw_write (regnum, buf);
  812. }
  813. else if (regnum >= SPARC64_Q0_REGNUM && regnum <= SPARC64_Q28_REGNUM)
  814. {
  815. regnum = SPARC_F0_REGNUM + 4 * (regnum - SPARC64_Q0_REGNUM);
  816. regcache->raw_write (regnum, buf);
  817. regcache->raw_write (regnum + 1, buf + 4);
  818. regcache->raw_write (regnum + 2, buf + 8);
  819. regcache->raw_write (regnum + 3, buf + 12);
  820. }
  821. else if (regnum >= SPARC64_Q32_REGNUM && regnum <= SPARC64_Q60_REGNUM)
  822. {
  823. regnum = SPARC64_F32_REGNUM + 2 * (regnum - SPARC64_Q32_REGNUM);
  824. regcache->raw_write (regnum, buf);
  825. regcache->raw_write (regnum + 1, buf + 8);
  826. }
  827. else if (regnum == SPARC64_CWP_REGNUM
  828. || regnum == SPARC64_PSTATE_REGNUM
  829. || regnum == SPARC64_ASI_REGNUM
  830. || regnum == SPARC64_CCR_REGNUM)
  831. {
  832. ULONGEST state, bits;
  833. regcache_raw_read_unsigned (regcache, SPARC64_STATE_REGNUM, &state);
  834. bits = extract_unsigned_integer (buf, 8, byte_order);
  835. switch (regnum)
  836. {
  837. case SPARC64_CWP_REGNUM:
  838. state |= ((bits & ((1 << 5) - 1)) << 0);
  839. break;
  840. case SPARC64_PSTATE_REGNUM:
  841. state |= ((bits & ((1 << 12) - 1)) << 8);
  842. break;
  843. case SPARC64_ASI_REGNUM:
  844. state |= ((bits & ((1 << 8) - 1)) << 24);
  845. break;
  846. case SPARC64_CCR_REGNUM:
  847. state |= ((bits & ((1 << 8) - 1)) << 32);
  848. break;
  849. }
  850. regcache_raw_write_unsigned (regcache, SPARC64_STATE_REGNUM, state);
  851. }
  852. }
  853. /* Return PC of first real instruction of the function starting at
  854. START_PC. */
  855. static CORE_ADDR
  856. sparc64_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
  857. {
  858. struct symtab_and_line sal;
  859. CORE_ADDR func_start, func_end;
  860. struct sparc_frame_cache cache;
  861. /* This is the preferred method, find the end of the prologue by
  862. using the debugging information. */
  863. if (find_pc_partial_function (start_pc, NULL, &func_start, &func_end))
  864. {
  865. sal = find_pc_line (func_start, 0);
  866. if (sal.end < func_end
  867. && start_pc <= sal.end)
  868. return sal.end;
  869. }
  870. return sparc_analyze_prologue (gdbarch, start_pc, 0xffffffffffffffffULL,
  871. &cache);
  872. }
  873. /* Normal frames. */
  874. static struct sparc_frame_cache *
  875. sparc64_frame_cache (struct frame_info *this_frame, void **this_cache)
  876. {
  877. return sparc_frame_cache (this_frame, this_cache);
  878. }
  879. static void
  880. sparc64_frame_this_id (struct frame_info *this_frame, void **this_cache,
  881. struct frame_id *this_id)
  882. {
  883. struct sparc_frame_cache *cache =
  884. sparc64_frame_cache (this_frame, this_cache);
  885. /* This marks the outermost frame. */
  886. if (cache->base == 0)
  887. return;
  888. (*this_id) = frame_id_build (cache->base, cache->pc);
  889. }
  890. static struct value *
  891. sparc64_frame_prev_register (struct frame_info *this_frame, void **this_cache,
  892. int regnum)
  893. {
  894. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  895. struct sparc_frame_cache *cache =
  896. sparc64_frame_cache (this_frame, this_cache);
  897. if (regnum == SPARC64_PC_REGNUM || regnum == SPARC64_NPC_REGNUM)
  898. {
  899. CORE_ADDR pc = (regnum == SPARC64_NPC_REGNUM) ? 4 : 0;
  900. regnum =
  901. (cache->copied_regs_mask & 0x80) ? SPARC_I7_REGNUM : SPARC_O7_REGNUM;
  902. pc += get_frame_register_unsigned (this_frame, regnum) + 8;
  903. return frame_unwind_got_constant (this_frame, regnum, pc);
  904. }
  905. /* Handle StackGhost. */
  906. {
  907. ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
  908. if (wcookie != 0 && !cache->frameless_p && regnum == SPARC_I7_REGNUM)
  909. {
  910. CORE_ADDR addr = cache->base + (regnum - SPARC_L0_REGNUM) * 8;
  911. ULONGEST i7;
  912. /* Read the value in from memory. */
  913. i7 = get_frame_memory_unsigned (this_frame, addr, 8);
  914. return frame_unwind_got_constant (this_frame, regnum, i7 ^ wcookie);
  915. }
  916. }
  917. /* The previous frame's `local' and `in' registers may have been saved
  918. in the register save area. */
  919. if (regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM
  920. && (cache->saved_regs_mask & (1 << (regnum - SPARC_L0_REGNUM))))
  921. {
  922. CORE_ADDR addr = cache->base + (regnum - SPARC_L0_REGNUM) * 8;
  923. return frame_unwind_got_memory (this_frame, regnum, addr);
  924. }
  925. /* The previous frame's `out' registers may be accessible as the current
  926. frame's `in' registers. */
  927. if (regnum >= SPARC_O0_REGNUM && regnum <= SPARC_O7_REGNUM
  928. && (cache->copied_regs_mask & (1 << (regnum - SPARC_O0_REGNUM))))
  929. regnum += (SPARC_I0_REGNUM - SPARC_O0_REGNUM);
  930. return frame_unwind_got_register (this_frame, regnum, regnum);
  931. }
  932. static const struct frame_unwind sparc64_frame_unwind =
  933. {
  934. "sparc64 prologue",
  935. NORMAL_FRAME,
  936. default_frame_unwind_stop_reason,
  937. sparc64_frame_this_id,
  938. sparc64_frame_prev_register,
  939. NULL,
  940. default_frame_sniffer
  941. };
  942. static CORE_ADDR
  943. sparc64_frame_base_address (struct frame_info *this_frame, void **this_cache)
  944. {
  945. struct sparc_frame_cache *cache =
  946. sparc64_frame_cache (this_frame, this_cache);
  947. return cache->base;
  948. }
  949. static const struct frame_base sparc64_frame_base =
  950. {
  951. &sparc64_frame_unwind,
  952. sparc64_frame_base_address,
  953. sparc64_frame_base_address,
  954. sparc64_frame_base_address
  955. };
  956. /* Check whether TYPE must be 16-byte aligned. */
  957. static int
  958. sparc64_16_byte_align_p (struct type *type)
  959. {
  960. if (type->code () == TYPE_CODE_ARRAY)
  961. {
  962. struct type *t = check_typedef (TYPE_TARGET_TYPE (type));
  963. if (sparc64_floating_p (t))
  964. return 1;
  965. }
  966. if (sparc64_floating_p (type) && TYPE_LENGTH (type) == 16)
  967. return 1;
  968. if (sparc64_structure_or_union_p (type))
  969. {
  970. int i;
  971. for (i = 0; i < type->num_fields (); i++)
  972. {
  973. struct type *subtype = check_typedef (type->field (i).type ());
  974. if (sparc64_16_byte_align_p (subtype))
  975. return 1;
  976. }
  977. }
  978. return 0;
  979. }
  980. /* Store floating fields of element ELEMENT of an "parameter array"
  981. that has type TYPE and is stored at BITPOS in VALBUF in the
  982. appropriate registers of REGCACHE. This function can be called
  983. recursively and therefore handles floating types in addition to
  984. structures. */
  985. static void
  986. sparc64_store_floating_fields (struct regcache *regcache, struct type *type,
  987. const gdb_byte *valbuf, int element, int bitpos)
  988. {
  989. struct gdbarch *gdbarch = regcache->arch ();
  990. int len = TYPE_LENGTH (type);
  991. gdb_assert (element < 16);
  992. if (type->code () == TYPE_CODE_ARRAY)
  993. {
  994. gdb_byte buf[8];
  995. int regnum = SPARC_F0_REGNUM + element * 2 + bitpos / 32;
  996. valbuf += bitpos / 8;
  997. if (len < 8)
  998. {
  999. memset (buf, 0, 8 - len);
  1000. memcpy (buf + 8 - len, valbuf, len);
  1001. valbuf = buf;
  1002. len = 8;
  1003. }
  1004. for (int n = 0; n < (len + 3) / 4; n++)
  1005. regcache->cooked_write (regnum + n, valbuf + n * 4);
  1006. }
  1007. else if (sparc64_floating_p (type)
  1008. || (sparc64_complex_floating_p (type) && len <= 16))
  1009. {
  1010. int regnum;
  1011. if (len == 16)
  1012. {
  1013. gdb_assert (bitpos == 0);
  1014. gdb_assert ((element % 2) == 0);
  1015. regnum = gdbarch_num_regs (gdbarch) + SPARC64_Q0_REGNUM + element / 2;
  1016. regcache->cooked_write (regnum, valbuf);
  1017. }
  1018. else if (len == 8)
  1019. {
  1020. gdb_assert (bitpos == 0 || bitpos == 64);
  1021. regnum = gdbarch_num_regs (gdbarch) + SPARC64_D0_REGNUM
  1022. + element + bitpos / 64;
  1023. regcache->cooked_write (regnum, valbuf + (bitpos / 8));
  1024. }
  1025. else
  1026. {
  1027. gdb_assert (len == 4);
  1028. gdb_assert (bitpos % 32 == 0 && bitpos >= 0 && bitpos < 128);
  1029. regnum = SPARC_F0_REGNUM + element * 2 + bitpos / 32;
  1030. regcache->cooked_write (regnum, valbuf + (bitpos / 8));
  1031. }
  1032. }
  1033. else if (sparc64_structure_or_union_p (type))
  1034. {
  1035. int i;
  1036. for (i = 0; i < type->num_fields (); i++)
  1037. {
  1038. struct type *subtype = check_typedef (type->field (i).type ());
  1039. int subpos = bitpos + type->field (i).loc_bitpos ();
  1040. sparc64_store_floating_fields (regcache, subtype, valbuf,
  1041. element, subpos);
  1042. }
  1043. /* GCC has an interesting bug. If TYPE is a structure that has
  1044. a single `float' member, GCC doesn't treat it as a structure
  1045. at all, but rather as an ordinary `float' argument. This
  1046. argument will be stored in %f1, as required by the psABI.
  1047. However, as a member of a structure the psABI requires it to
  1048. be stored in %f0. This bug is present in GCC 3.3.2, but
  1049. probably in older releases to. To appease GCC, if a
  1050. structure has only a single `float' member, we store its
  1051. value in %f1 too (we already have stored in %f0). */
  1052. if (type->num_fields () == 1)
  1053. {
  1054. struct type *subtype = check_typedef (type->field (0).type ());
  1055. if (sparc64_floating_p (subtype) && TYPE_LENGTH (subtype) == 4)
  1056. regcache->cooked_write (SPARC_F1_REGNUM, valbuf);
  1057. }
  1058. }
  1059. }
  1060. /* Fetch floating fields from a variable of type TYPE from the
  1061. appropriate registers for BITPOS in REGCACHE and store it at BITPOS
  1062. in VALBUF. This function can be called recursively and therefore
  1063. handles floating types in addition to structures. */
  1064. static void
  1065. sparc64_extract_floating_fields (struct regcache *regcache, struct type *type,
  1066. gdb_byte *valbuf, int bitpos)
  1067. {
  1068. struct gdbarch *gdbarch = regcache->arch ();
  1069. if (type->code () == TYPE_CODE_ARRAY)
  1070. {
  1071. int len = TYPE_LENGTH (type);
  1072. int regnum = SPARC_F0_REGNUM + bitpos / 32;
  1073. valbuf += bitpos / 8;
  1074. if (len < 4)
  1075. {
  1076. gdb_byte buf[4];
  1077. regcache->cooked_read (regnum, buf);
  1078. memcpy (valbuf, buf + 4 - len, len);
  1079. }
  1080. else
  1081. for (int i = 0; i < (len + 3) / 4; i++)
  1082. regcache->cooked_read (regnum + i, valbuf + i * 4);
  1083. }
  1084. else if (sparc64_floating_p (type))
  1085. {
  1086. int len = TYPE_LENGTH (type);
  1087. int regnum;
  1088. if (len == 16)
  1089. {
  1090. gdb_assert (bitpos == 0 || bitpos == 128);
  1091. regnum = gdbarch_num_regs (gdbarch) + SPARC64_Q0_REGNUM
  1092. + bitpos / 128;
  1093. regcache->cooked_read (regnum, valbuf + (bitpos / 8));
  1094. }
  1095. else if (len == 8)
  1096. {
  1097. gdb_assert (bitpos % 64 == 0 && bitpos >= 0 && bitpos < 256);
  1098. regnum = gdbarch_num_regs (gdbarch) + SPARC64_D0_REGNUM + bitpos / 64;
  1099. regcache->cooked_read (regnum, valbuf + (bitpos / 8));
  1100. }
  1101. else
  1102. {
  1103. gdb_assert (len == 4);
  1104. gdb_assert (bitpos % 32 == 0 && bitpos >= 0 && bitpos < 256);
  1105. regnum = SPARC_F0_REGNUM + bitpos / 32;
  1106. regcache->cooked_read (regnum, valbuf + (bitpos / 8));
  1107. }
  1108. }
  1109. else if (sparc64_structure_or_union_p (type))
  1110. {
  1111. int i;
  1112. for (i = 0; i < type->num_fields (); i++)
  1113. {
  1114. struct type *subtype = check_typedef (type->field (i).type ());
  1115. int subpos = bitpos + type->field (i).loc_bitpos ();
  1116. sparc64_extract_floating_fields (regcache, subtype, valbuf, subpos);
  1117. }
  1118. }
  1119. }
  1120. /* Store the NARGS arguments ARGS and STRUCT_ADDR (if STRUCT_RETURN is
  1121. non-zero) in REGCACHE and on the stack (starting from address SP). */
  1122. static CORE_ADDR
  1123. sparc64_store_arguments (struct regcache *regcache, int nargs,
  1124. struct value **args, CORE_ADDR sp,
  1125. function_call_return_method return_method,
  1126. CORE_ADDR struct_addr)
  1127. {
  1128. struct gdbarch *gdbarch = regcache->arch ();
  1129. /* Number of extended words in the "parameter array". */
  1130. int num_elements = 0;
  1131. int element = 0;
  1132. int i;
  1133. /* Take BIAS into account. */
  1134. sp += BIAS;
  1135. /* First we calculate the number of extended words in the "parameter
  1136. array". While doing so we also convert some of the arguments. */
  1137. if (return_method == return_method_struct)
  1138. num_elements++;
  1139. for (i = 0; i < nargs; i++)
  1140. {
  1141. struct type *type = value_type (args[i]);
  1142. int len = TYPE_LENGTH (type);
  1143. if (sparc64_structure_or_union_p (type)
  1144. || (sparc64_complex_floating_p (type) && len == 32))
  1145. {
  1146. /* Structure or Union arguments. */
  1147. if (len <= 16)
  1148. {
  1149. if (num_elements % 2 && sparc64_16_byte_align_p (type))
  1150. num_elements++;
  1151. num_elements += ((len + 7) / 8);
  1152. }
  1153. else
  1154. {
  1155. /* The psABI says that "Structures or unions larger than
  1156. sixteen bytes are copied by the caller and passed
  1157. indirectly; the caller will pass the address of a
  1158. correctly aligned structure value. This sixty-four
  1159. bit address will occupy one word in the parameter
  1160. array, and may be promoted to an %o register like any
  1161. other pointer value." Allocate memory for these
  1162. values on the stack. */
  1163. sp -= len;
  1164. /* Use 16-byte alignment for these values. That's
  1165. always correct, and wasting a few bytes shouldn't be
  1166. a problem. */
  1167. sp &= ~0xf;
  1168. write_memory (sp, value_contents (args[i]).data (), len);
  1169. args[i] = value_from_pointer (lookup_pointer_type (type), sp);
  1170. num_elements++;
  1171. }
  1172. }
  1173. else if (sparc64_floating_p (type) || sparc64_complex_floating_p (type))
  1174. {
  1175. /* Floating arguments. */
  1176. if (len == 16)
  1177. {
  1178. /* The psABI says that "Each quad-precision parameter
  1179. value will be assigned to two extended words in the
  1180. parameter array. */
  1181. num_elements += 2;
  1182. /* The psABI says that "Long doubles must be
  1183. quad-aligned, and thus a hole might be introduced
  1184. into the parameter array to force alignment." Skip
  1185. an element if necessary. */
  1186. if ((num_elements % 2) && sparc64_16_byte_align_p (type))
  1187. num_elements++;
  1188. }
  1189. else
  1190. num_elements++;
  1191. }
  1192. else
  1193. {
  1194. /* Integral and pointer arguments. */
  1195. gdb_assert (sparc64_integral_or_pointer_p (type));
  1196. /* The psABI says that "Each argument value of integral type
  1197. smaller than an extended word will be widened by the
  1198. caller to an extended word according to the signed-ness
  1199. of the argument type." */
  1200. if (len < 8)
  1201. args[i] = value_cast (builtin_type (gdbarch)->builtin_int64,
  1202. args[i]);
  1203. num_elements++;
  1204. }
  1205. }
  1206. /* Allocate the "parameter array". */
  1207. sp -= num_elements * 8;
  1208. /* The psABI says that "Every stack frame must be 16-byte aligned." */
  1209. sp &= ~0xf;
  1210. /* Now we store the arguments in to the "parameter array". Some
  1211. Integer or Pointer arguments and Structure or Union arguments
  1212. will be passed in %o registers. Some Floating arguments and
  1213. floating members of structures are passed in floating-point
  1214. registers. However, for functions with variable arguments,
  1215. floating arguments are stored in an %0 register, and for
  1216. functions without a prototype floating arguments are stored in
  1217. both a floating-point and an %o registers, or a floating-point
  1218. register and memory. To simplify the logic here we always pass
  1219. arguments in memory, an %o register, and a floating-point
  1220. register if appropriate. This should be no problem since the
  1221. contents of any unused memory or registers in the "parameter
  1222. array" are undefined. */
  1223. if (return_method == return_method_struct)
  1224. {
  1225. regcache_cooked_write_unsigned (regcache, SPARC_O0_REGNUM, struct_addr);
  1226. element++;
  1227. }
  1228. for (i = 0; i < nargs; i++)
  1229. {
  1230. const gdb_byte *valbuf = value_contents (args[i]).data ();
  1231. struct type *type = value_type (args[i]);
  1232. int len = TYPE_LENGTH (type);
  1233. int regnum = -1;
  1234. gdb_byte buf[16];
  1235. if (sparc64_structure_or_union_p (type)
  1236. || (sparc64_complex_floating_p (type) && len == 32))
  1237. {
  1238. /* Structure, Union or long double Complex arguments. */
  1239. gdb_assert (len <= 16);
  1240. memset (buf, 0, sizeof (buf));
  1241. memcpy (buf, valbuf, len);
  1242. valbuf = buf;
  1243. if (element % 2 && sparc64_16_byte_align_p (type))
  1244. element++;
  1245. if (element < 6)
  1246. {
  1247. regnum = SPARC_O0_REGNUM + element;
  1248. if (len > 8 && element < 5)
  1249. regcache->cooked_write (regnum + 1, valbuf + 8);
  1250. }
  1251. if (element < 16)
  1252. sparc64_store_floating_fields (regcache, type, valbuf, element, 0);
  1253. }
  1254. else if (sparc64_complex_floating_p (type))
  1255. {
  1256. /* Float Complex or double Complex arguments. */
  1257. if (element < 16)
  1258. {
  1259. regnum = gdbarch_num_regs (gdbarch) + SPARC64_D0_REGNUM + element;
  1260. if (len == 16)
  1261. {
  1262. if (regnum < gdbarch_num_regs (gdbarch) + SPARC64_D30_REGNUM)
  1263. regcache->cooked_write (regnum + 1, valbuf + 8);
  1264. if (regnum < gdbarch_num_regs (gdbarch) + SPARC64_D10_REGNUM)
  1265. regcache->cooked_write (SPARC_O0_REGNUM + element + 1,
  1266. valbuf + 8);
  1267. }
  1268. }
  1269. }
  1270. else if (sparc64_floating_p (type))
  1271. {
  1272. /* Floating arguments. */
  1273. if (len == 16)
  1274. {
  1275. if (element % 2)
  1276. element++;
  1277. if (element < 16)
  1278. regnum = gdbarch_num_regs (gdbarch) + SPARC64_Q0_REGNUM
  1279. + element / 2;
  1280. }
  1281. else if (len == 8)
  1282. {
  1283. if (element < 16)
  1284. regnum = gdbarch_num_regs (gdbarch) + SPARC64_D0_REGNUM
  1285. + element;
  1286. }
  1287. else if (len == 4)
  1288. {
  1289. /* The psABI says "Each single-precision parameter value
  1290. will be assigned to one extended word in the
  1291. parameter array, and right-justified within that
  1292. word; the left half (even float register) is
  1293. undefined." Even though the psABI says that "the
  1294. left half is undefined", set it to zero here. */
  1295. memset (buf, 0, 4);
  1296. memcpy (buf + 4, valbuf, 4);
  1297. valbuf = buf;
  1298. len = 8;
  1299. if (element < 16)
  1300. regnum = gdbarch_num_regs (gdbarch) + SPARC64_D0_REGNUM
  1301. + element;
  1302. }
  1303. }
  1304. else
  1305. {
  1306. /* Integral and pointer arguments. */
  1307. gdb_assert (len == 8);
  1308. if (element < 6)
  1309. regnum = SPARC_O0_REGNUM + element;
  1310. }
  1311. if (regnum != -1)
  1312. {
  1313. regcache->cooked_write (regnum, valbuf);
  1314. /* If we're storing the value in a floating-point register,
  1315. also store it in the corresponding %0 register(s). */
  1316. if (regnum >= gdbarch_num_regs (gdbarch))
  1317. {
  1318. regnum -= gdbarch_num_regs (gdbarch);
  1319. if (regnum >= SPARC64_D0_REGNUM && regnum <= SPARC64_D10_REGNUM)
  1320. {
  1321. gdb_assert (element < 6);
  1322. regnum = SPARC_O0_REGNUM + element;
  1323. regcache->cooked_write (regnum, valbuf);
  1324. }
  1325. else if (regnum >= SPARC64_Q0_REGNUM && regnum <= SPARC64_Q8_REGNUM)
  1326. {
  1327. gdb_assert (element < 5);
  1328. regnum = SPARC_O0_REGNUM + element;
  1329. regcache->cooked_write (regnum, valbuf);
  1330. regcache->cooked_write (regnum + 1, valbuf + 8);
  1331. }
  1332. }
  1333. }
  1334. /* Always store the argument in memory. */
  1335. write_memory (sp + element * 8, valbuf, len);
  1336. element += ((len + 7) / 8);
  1337. }
  1338. gdb_assert (element == num_elements);
  1339. /* Take BIAS into account. */
  1340. sp -= BIAS;
  1341. return sp;
  1342. }
  1343. static CORE_ADDR
  1344. sparc64_frame_align (struct gdbarch *gdbarch, CORE_ADDR address)
  1345. {
  1346. /* The ABI requires 16-byte alignment. */
  1347. return address & ~0xf;
  1348. }
  1349. static CORE_ADDR
  1350. sparc64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  1351. struct regcache *regcache, CORE_ADDR bp_addr,
  1352. int nargs, struct value **args, CORE_ADDR sp,
  1353. function_call_return_method return_method,
  1354. CORE_ADDR struct_addr)
  1355. {
  1356. /* Set return address. */
  1357. regcache_cooked_write_unsigned (regcache, SPARC_O7_REGNUM, bp_addr - 8);
  1358. /* Set up function arguments. */
  1359. sp = sparc64_store_arguments (regcache, nargs, args, sp, return_method,
  1360. struct_addr);
  1361. /* Allocate the register save area. */
  1362. sp -= 16 * 8;
  1363. /* Stack should be 16-byte aligned at this point. */
  1364. gdb_assert ((sp + BIAS) % 16 == 0);
  1365. /* Finally, update the stack pointer. */
  1366. regcache_cooked_write_unsigned (regcache, SPARC_SP_REGNUM, sp);
  1367. return sp + BIAS;
  1368. }
  1369. /* Extract from an array REGBUF containing the (raw) register state, a
  1370. function return value of TYPE, and copy that into VALBUF. */
  1371. static void
  1372. sparc64_extract_return_value (struct type *type, struct regcache *regcache,
  1373. gdb_byte *valbuf)
  1374. {
  1375. int len = TYPE_LENGTH (type);
  1376. gdb_byte buf[32];
  1377. int i;
  1378. if (sparc64_structure_or_union_p (type))
  1379. {
  1380. /* Structure or Union return values. */
  1381. gdb_assert (len <= 32);
  1382. for (i = 0; i < ((len + 7) / 8); i++)
  1383. regcache->cooked_read (SPARC_O0_REGNUM + i, buf + i * 8);
  1384. if (type->code () != TYPE_CODE_UNION)
  1385. sparc64_extract_floating_fields (regcache, type, buf, 0);
  1386. memcpy (valbuf, buf, len);
  1387. }
  1388. else if (sparc64_floating_p (type) || sparc64_complex_floating_p (type))
  1389. {
  1390. /* Floating return values. */
  1391. for (i = 0; i < len / 4; i++)
  1392. regcache->cooked_read (SPARC_F0_REGNUM + i, buf + i * 4);
  1393. memcpy (valbuf, buf, len);
  1394. }
  1395. else if (type->code () == TYPE_CODE_ARRAY)
  1396. {
  1397. /* Small arrays are returned the same way as small structures. */
  1398. gdb_assert (len <= 32);
  1399. for (i = 0; i < ((len + 7) / 8); i++)
  1400. regcache->cooked_read (SPARC_O0_REGNUM + i, buf + i * 8);
  1401. memcpy (valbuf, buf, len);
  1402. }
  1403. else
  1404. {
  1405. /* Integral and pointer return values. */
  1406. gdb_assert (sparc64_integral_or_pointer_p (type));
  1407. /* Just stripping off any unused bytes should preserve the
  1408. signed-ness just fine. */
  1409. regcache->cooked_read (SPARC_O0_REGNUM, buf);
  1410. memcpy (valbuf, buf + 8 - len, len);
  1411. }
  1412. }
  1413. /* Write into the appropriate registers a function return value stored
  1414. in VALBUF of type TYPE. */
  1415. static void
  1416. sparc64_store_return_value (struct type *type, struct regcache *regcache,
  1417. const gdb_byte *valbuf)
  1418. {
  1419. int len = TYPE_LENGTH (type);
  1420. gdb_byte buf[16];
  1421. int i;
  1422. if (sparc64_structure_or_union_p (type))
  1423. {
  1424. /* Structure or Union return values. */
  1425. gdb_assert (len <= 32);
  1426. /* Simplify matters by storing the complete value (including
  1427. floating members) into %o0 and %o1. Floating members are
  1428. also store in the appropriate floating-point registers. */
  1429. memset (buf, 0, sizeof (buf));
  1430. memcpy (buf, valbuf, len);
  1431. for (i = 0; i < ((len + 7) / 8); i++)
  1432. regcache->cooked_write (SPARC_O0_REGNUM + i, buf + i * 8);
  1433. if (type->code () != TYPE_CODE_UNION)
  1434. sparc64_store_floating_fields (regcache, type, buf, 0, 0);
  1435. }
  1436. else if (sparc64_floating_p (type) || sparc64_complex_floating_p (type))
  1437. {
  1438. /* Floating return values. */
  1439. memcpy (buf, valbuf, len);
  1440. for (i = 0; i < len / 4; i++)
  1441. regcache->cooked_write (SPARC_F0_REGNUM + i, buf + i * 4);
  1442. }
  1443. else if (type->code () == TYPE_CODE_ARRAY)
  1444. {
  1445. /* Small arrays are returned the same way as small structures. */
  1446. gdb_assert (len <= 32);
  1447. memset (buf, 0, sizeof (buf));
  1448. memcpy (buf, valbuf, len);
  1449. for (i = 0; i < ((len + 7) / 8); i++)
  1450. regcache->cooked_write (SPARC_O0_REGNUM + i, buf + i * 8);
  1451. }
  1452. else
  1453. {
  1454. /* Integral and pointer return values. */
  1455. gdb_assert (sparc64_integral_or_pointer_p (type));
  1456. /* ??? Do we need to do any sign-extension here? */
  1457. memset (buf, 0, 8);
  1458. memcpy (buf + 8 - len, valbuf, len);
  1459. regcache->cooked_write (SPARC_O0_REGNUM, buf);
  1460. }
  1461. }
  1462. static enum return_value_convention
  1463. sparc64_return_value (struct gdbarch *gdbarch, struct value *function,
  1464. struct type *type, struct regcache *regcache,
  1465. gdb_byte *readbuf, const gdb_byte *writebuf)
  1466. {
  1467. if (TYPE_LENGTH (type) > 32)
  1468. return RETURN_VALUE_STRUCT_CONVENTION;
  1469. if (readbuf)
  1470. sparc64_extract_return_value (type, regcache, readbuf);
  1471. if (writebuf)
  1472. sparc64_store_return_value (type, regcache, writebuf);
  1473. return RETURN_VALUE_REGISTER_CONVENTION;
  1474. }
  1475. static void
  1476. sparc64_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
  1477. struct dwarf2_frame_state_reg *reg,
  1478. struct frame_info *this_frame)
  1479. {
  1480. switch (regnum)
  1481. {
  1482. case SPARC_G0_REGNUM:
  1483. /* Since %g0 is always zero, there is no point in saving it, and
  1484. people will be inclined omit it from the CFI. Make sure we
  1485. don't warn about that. */
  1486. reg->how = DWARF2_FRAME_REG_SAME_VALUE;
  1487. break;
  1488. case SPARC_SP_REGNUM:
  1489. reg->how = DWARF2_FRAME_REG_CFA;
  1490. break;
  1491. case SPARC64_PC_REGNUM:
  1492. reg->how = DWARF2_FRAME_REG_RA_OFFSET;
  1493. reg->loc.offset = 8;
  1494. break;
  1495. case SPARC64_NPC_REGNUM:
  1496. reg->how = DWARF2_FRAME_REG_RA_OFFSET;
  1497. reg->loc.offset = 12;
  1498. break;
  1499. }
  1500. }
  1501. /* sparc64_addr_bits_remove - remove useless address bits */
  1502. static CORE_ADDR
  1503. sparc64_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR addr)
  1504. {
  1505. return adi_normalize_address (addr);
  1506. }
  1507. void
  1508. sparc64_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  1509. {
  1510. sparc_gdbarch_tdep *tdep = (sparc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  1511. tdep->pc_regnum = SPARC64_PC_REGNUM;
  1512. tdep->npc_regnum = SPARC64_NPC_REGNUM;
  1513. tdep->fpu_register_names = sparc64_fpu_register_names;
  1514. tdep->fpu_registers_num = ARRAY_SIZE (sparc64_fpu_register_names);
  1515. tdep->cp0_register_names = sparc64_cp0_register_names;
  1516. tdep->cp0_registers_num = ARRAY_SIZE (sparc64_cp0_register_names);
  1517. /* This is what all the fuss is about. */
  1518. set_gdbarch_long_bit (gdbarch, 64);
  1519. set_gdbarch_long_long_bit (gdbarch, 64);
  1520. set_gdbarch_ptr_bit (gdbarch, 64);
  1521. set_gdbarch_wchar_bit (gdbarch, 16);
  1522. set_gdbarch_wchar_signed (gdbarch, 0);
  1523. set_gdbarch_num_regs (gdbarch, SPARC64_NUM_REGS);
  1524. set_gdbarch_register_name (gdbarch, sparc64_register_name);
  1525. set_gdbarch_register_type (gdbarch, sparc64_register_type);
  1526. set_gdbarch_num_pseudo_regs (gdbarch, SPARC64_NUM_PSEUDO_REGS);
  1527. set_tdesc_pseudo_register_name (gdbarch, sparc64_pseudo_register_name);
  1528. set_tdesc_pseudo_register_type (gdbarch, sparc64_pseudo_register_type);
  1529. set_gdbarch_pseudo_register_read (gdbarch, sparc64_pseudo_register_read);
  1530. set_gdbarch_pseudo_register_write (gdbarch, sparc64_pseudo_register_write);
  1531. /* Register numbers of various important registers. */
  1532. set_gdbarch_pc_regnum (gdbarch, SPARC64_PC_REGNUM); /* %pc */
  1533. /* Call dummy code. */
  1534. set_gdbarch_frame_align (gdbarch, sparc64_frame_align);
  1535. set_gdbarch_call_dummy_location (gdbarch, AT_ENTRY_POINT);
  1536. set_gdbarch_push_dummy_code (gdbarch, NULL);
  1537. set_gdbarch_push_dummy_call (gdbarch, sparc64_push_dummy_call);
  1538. set_gdbarch_return_value (gdbarch, sparc64_return_value);
  1539. set_gdbarch_stabs_argument_has_addr
  1540. (gdbarch, default_stabs_argument_has_addr);
  1541. set_gdbarch_skip_prologue (gdbarch, sparc64_skip_prologue);
  1542. set_gdbarch_stack_frame_destroyed_p (gdbarch, sparc_stack_frame_destroyed_p);
  1543. /* Hook in the DWARF CFI frame unwinder. */
  1544. dwarf2_frame_set_init_reg (gdbarch, sparc64_dwarf2_frame_init_reg);
  1545. /* FIXME: kettenis/20050423: Don't enable the unwinder until the
  1546. StackGhost issues have been resolved. */
  1547. frame_unwind_append_unwinder (gdbarch, &sparc64_frame_unwind);
  1548. frame_base_set_default (gdbarch, &sparc64_frame_base);
  1549. set_gdbarch_addr_bits_remove (gdbarch, sparc64_addr_bits_remove);
  1550. }
  1551. /* Helper functions for dealing with register sets. */
  1552. #define TSTATE_CWP 0x000000000000001fULL
  1553. #define TSTATE_ICC 0x0000000f00000000ULL
  1554. #define TSTATE_XCC 0x000000f000000000ULL
  1555. #define PSR_S 0x00000080
  1556. #ifndef PSR_ICC
  1557. #define PSR_ICC 0x00f00000
  1558. #endif
  1559. #define PSR_VERS 0x0f000000
  1560. #ifndef PSR_IMPL
  1561. #define PSR_IMPL 0xf0000000
  1562. #endif
  1563. #define PSR_V8PLUS 0xff000000
  1564. #define PSR_XCC 0x000f0000
  1565. void
  1566. sparc64_supply_gregset (const struct sparc_gregmap *gregmap,
  1567. struct regcache *regcache,
  1568. int regnum, const void *gregs)
  1569. {
  1570. struct gdbarch *gdbarch = regcache->arch ();
  1571. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1572. int sparc32 = (gdbarch_ptr_bit (gdbarch) == 32);
  1573. const gdb_byte *regs = (const gdb_byte *) gregs;
  1574. gdb_byte zero[8] = { 0 };
  1575. int i;
  1576. if (sparc32)
  1577. {
  1578. if (regnum == SPARC32_PSR_REGNUM || regnum == -1)
  1579. {
  1580. int offset = gregmap->r_tstate_offset;
  1581. ULONGEST tstate, psr;
  1582. gdb_byte buf[4];
  1583. tstate = extract_unsigned_integer (regs + offset, 8, byte_order);
  1584. psr = ((tstate & TSTATE_CWP) | PSR_S | ((tstate & TSTATE_ICC) >> 12)
  1585. | ((tstate & TSTATE_XCC) >> 20) | PSR_V8PLUS);
  1586. store_unsigned_integer (buf, 4, byte_order, psr);
  1587. regcache->raw_supply (SPARC32_PSR_REGNUM, buf);
  1588. }
  1589. if (regnum == SPARC32_PC_REGNUM || regnum == -1)
  1590. regcache->raw_supply (SPARC32_PC_REGNUM,
  1591. regs + gregmap->r_pc_offset + 4);
  1592. if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
  1593. regcache->raw_supply (SPARC32_NPC_REGNUM,
  1594. regs + gregmap->r_npc_offset + 4);
  1595. if (regnum == SPARC32_Y_REGNUM || regnum == -1)
  1596. {
  1597. int offset = gregmap->r_y_offset + 8 - gregmap->r_y_size;
  1598. regcache->raw_supply (SPARC32_Y_REGNUM, regs + offset);
  1599. }
  1600. }
  1601. else
  1602. {
  1603. if (regnum == SPARC64_STATE_REGNUM || regnum == -1)
  1604. regcache->raw_supply (SPARC64_STATE_REGNUM,
  1605. regs + gregmap->r_tstate_offset);
  1606. if (regnum == SPARC64_PC_REGNUM || regnum == -1)
  1607. regcache->raw_supply (SPARC64_PC_REGNUM,
  1608. regs + gregmap->r_pc_offset);
  1609. if (regnum == SPARC64_NPC_REGNUM || regnum == -1)
  1610. regcache->raw_supply (SPARC64_NPC_REGNUM,
  1611. regs + gregmap->r_npc_offset);
  1612. if (regnum == SPARC64_Y_REGNUM || regnum == -1)
  1613. {
  1614. gdb_byte buf[8];
  1615. memset (buf, 0, 8);
  1616. memcpy (buf + 8 - gregmap->r_y_size,
  1617. regs + gregmap->r_y_offset, gregmap->r_y_size);
  1618. regcache->raw_supply (SPARC64_Y_REGNUM, buf);
  1619. }
  1620. if ((regnum == SPARC64_FPRS_REGNUM || regnum == -1)
  1621. && gregmap->r_fprs_offset != -1)
  1622. regcache->raw_supply (SPARC64_FPRS_REGNUM,
  1623. regs + gregmap->r_fprs_offset);
  1624. }
  1625. if (regnum == SPARC_G0_REGNUM || regnum == -1)
  1626. regcache->raw_supply (SPARC_G0_REGNUM, &zero);
  1627. if ((regnum >= SPARC_G1_REGNUM && regnum <= SPARC_O7_REGNUM) || regnum == -1)
  1628. {
  1629. int offset = gregmap->r_g1_offset;
  1630. if (sparc32)
  1631. offset += 4;
  1632. for (i = SPARC_G1_REGNUM; i <= SPARC_O7_REGNUM; i++)
  1633. {
  1634. if (regnum == i || regnum == -1)
  1635. regcache->raw_supply (i, regs + offset);
  1636. offset += 8;
  1637. }
  1638. }
  1639. if ((regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM) || regnum == -1)
  1640. {
  1641. /* Not all of the register set variants include Locals and
  1642. Inputs. For those that don't, we read them off the stack. */
  1643. if (gregmap->r_l0_offset == -1)
  1644. {
  1645. ULONGEST sp;
  1646. regcache_cooked_read_unsigned (regcache, SPARC_SP_REGNUM, &sp);
  1647. sparc_supply_rwindow (regcache, sp, regnum);
  1648. }
  1649. else
  1650. {
  1651. int offset = gregmap->r_l0_offset;
  1652. if (sparc32)
  1653. offset += 4;
  1654. for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
  1655. {
  1656. if (regnum == i || regnum == -1)
  1657. regcache->raw_supply (i, regs + offset);
  1658. offset += 8;
  1659. }
  1660. }
  1661. }
  1662. }
  1663. void
  1664. sparc64_collect_gregset (const struct sparc_gregmap *gregmap,
  1665. const struct regcache *regcache,
  1666. int regnum, void *gregs)
  1667. {
  1668. struct gdbarch *gdbarch = regcache->arch ();
  1669. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1670. int sparc32 = (gdbarch_ptr_bit (gdbarch) == 32);
  1671. gdb_byte *regs = (gdb_byte *) gregs;
  1672. int i;
  1673. if (sparc32)
  1674. {
  1675. if (regnum == SPARC32_PSR_REGNUM || regnum == -1)
  1676. {
  1677. int offset = gregmap->r_tstate_offset;
  1678. ULONGEST tstate, psr;
  1679. gdb_byte buf[8];
  1680. tstate = extract_unsigned_integer (regs + offset, 8, byte_order);
  1681. regcache->raw_collect (SPARC32_PSR_REGNUM, buf);
  1682. psr = extract_unsigned_integer (buf, 4, byte_order);
  1683. tstate |= (psr & PSR_ICC) << 12;
  1684. if ((psr & (PSR_VERS | PSR_IMPL)) == PSR_V8PLUS)
  1685. tstate |= (psr & PSR_XCC) << 20;
  1686. store_unsigned_integer (buf, 8, byte_order, tstate);
  1687. memcpy (regs + offset, buf, 8);
  1688. }
  1689. if (regnum == SPARC32_PC_REGNUM || regnum == -1)
  1690. regcache->raw_collect (SPARC32_PC_REGNUM,
  1691. regs + gregmap->r_pc_offset + 4);
  1692. if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
  1693. regcache->raw_collect (SPARC32_NPC_REGNUM,
  1694. regs + gregmap->r_npc_offset + 4);
  1695. if (regnum == SPARC32_Y_REGNUM || regnum == -1)
  1696. {
  1697. int offset = gregmap->r_y_offset + 8 - gregmap->r_y_size;
  1698. regcache->raw_collect (SPARC32_Y_REGNUM, regs + offset);
  1699. }
  1700. }
  1701. else
  1702. {
  1703. if (regnum == SPARC64_STATE_REGNUM || regnum == -1)
  1704. regcache->raw_collect (SPARC64_STATE_REGNUM,
  1705. regs + gregmap->r_tstate_offset);
  1706. if (regnum == SPARC64_PC_REGNUM || regnum == -1)
  1707. regcache->raw_collect (SPARC64_PC_REGNUM,
  1708. regs + gregmap->r_pc_offset);
  1709. if (regnum == SPARC64_NPC_REGNUM || regnum == -1)
  1710. regcache->raw_collect (SPARC64_NPC_REGNUM,
  1711. regs + gregmap->r_npc_offset);
  1712. if (regnum == SPARC64_Y_REGNUM || regnum == -1)
  1713. {
  1714. gdb_byte buf[8];
  1715. regcache->raw_collect (SPARC64_Y_REGNUM, buf);
  1716. memcpy (regs + gregmap->r_y_offset,
  1717. buf + 8 - gregmap->r_y_size, gregmap->r_y_size);
  1718. }
  1719. if ((regnum == SPARC64_FPRS_REGNUM || regnum == -1)
  1720. && gregmap->r_fprs_offset != -1)
  1721. regcache->raw_collect (SPARC64_FPRS_REGNUM,
  1722. regs + gregmap->r_fprs_offset);
  1723. }
  1724. if ((regnum >= SPARC_G1_REGNUM && regnum <= SPARC_O7_REGNUM) || regnum == -1)
  1725. {
  1726. int offset = gregmap->r_g1_offset;
  1727. if (sparc32)
  1728. offset += 4;
  1729. /* %g0 is always zero. */
  1730. for (i = SPARC_G1_REGNUM; i <= SPARC_O7_REGNUM; i++)
  1731. {
  1732. if (regnum == i || regnum == -1)
  1733. regcache->raw_collect (i, regs + offset);
  1734. offset += 8;
  1735. }
  1736. }
  1737. if ((regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM) || regnum == -1)
  1738. {
  1739. /* Not all of the register set variants include Locals and
  1740. Inputs. For those that don't, we read them off the stack. */
  1741. if (gregmap->r_l0_offset != -1)
  1742. {
  1743. int offset = gregmap->r_l0_offset;
  1744. if (sparc32)
  1745. offset += 4;
  1746. for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
  1747. {
  1748. if (regnum == i || regnum == -1)
  1749. regcache->raw_collect (i, regs + offset);
  1750. offset += 8;
  1751. }
  1752. }
  1753. }
  1754. }
  1755. void
  1756. sparc64_supply_fpregset (const struct sparc_fpregmap *fpregmap,
  1757. struct regcache *regcache,
  1758. int regnum, const void *fpregs)
  1759. {
  1760. int sparc32 = (gdbarch_ptr_bit (regcache->arch ()) == 32);
  1761. const gdb_byte *regs = (const gdb_byte *) fpregs;
  1762. int i;
  1763. for (i = 0; i < 32; i++)
  1764. {
  1765. if (regnum == (SPARC_F0_REGNUM + i) || regnum == -1)
  1766. regcache->raw_supply (SPARC_F0_REGNUM + i,
  1767. regs + fpregmap->r_f0_offset + (i * 4));
  1768. }
  1769. if (sparc32)
  1770. {
  1771. if (regnum == SPARC32_FSR_REGNUM || regnum == -1)
  1772. regcache->raw_supply (SPARC32_FSR_REGNUM,
  1773. regs + fpregmap->r_fsr_offset);
  1774. }
  1775. else
  1776. {
  1777. for (i = 0; i < 16; i++)
  1778. {
  1779. if (regnum == (SPARC64_F32_REGNUM + i) || regnum == -1)
  1780. regcache->raw_supply
  1781. (SPARC64_F32_REGNUM + i,
  1782. regs + fpregmap->r_f0_offset + (32 * 4) + (i * 8));
  1783. }
  1784. if (regnum == SPARC64_FSR_REGNUM || regnum == -1)
  1785. regcache->raw_supply (SPARC64_FSR_REGNUM,
  1786. regs + fpregmap->r_fsr_offset);
  1787. }
  1788. }
  1789. void
  1790. sparc64_collect_fpregset (const struct sparc_fpregmap *fpregmap,
  1791. const struct regcache *regcache,
  1792. int regnum, void *fpregs)
  1793. {
  1794. int sparc32 = (gdbarch_ptr_bit (regcache->arch ()) == 32);
  1795. gdb_byte *regs = (gdb_byte *) fpregs;
  1796. int i;
  1797. for (i = 0; i < 32; i++)
  1798. {
  1799. if (regnum == (SPARC_F0_REGNUM + i) || regnum == -1)
  1800. regcache->raw_collect (SPARC_F0_REGNUM + i,
  1801. regs + fpregmap->r_f0_offset + (i * 4));
  1802. }
  1803. if (sparc32)
  1804. {
  1805. if (regnum == SPARC32_FSR_REGNUM || regnum == -1)
  1806. regcache->raw_collect (SPARC32_FSR_REGNUM,
  1807. regs + fpregmap->r_fsr_offset);
  1808. }
  1809. else
  1810. {
  1811. for (i = 0; i < 16; i++)
  1812. {
  1813. if (regnum == (SPARC64_F32_REGNUM + i) || regnum == -1)
  1814. regcache->raw_collect (SPARC64_F32_REGNUM + i,
  1815. (regs + fpregmap->r_f0_offset
  1816. + (32 * 4) + (i * 8)));
  1817. }
  1818. if (regnum == SPARC64_FSR_REGNUM || regnum == -1)
  1819. regcache->raw_collect (SPARC64_FSR_REGNUM,
  1820. regs + fpregmap->r_fsr_offset);
  1821. }
  1822. }
  1823. const struct sparc_fpregmap sparc64_bsd_fpregmap =
  1824. {
  1825. 0 * 8, /* %f0 */
  1826. 32 * 8, /* %fsr */
  1827. };