ppc-linux-nat.c 101 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233
  1. /* PPC GNU/Linux native support.
  2. Copyright (C) 1988-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 "gdbthread.h"
  18. #include "gdbcore.h"
  19. #include "regcache.h"
  20. #include "regset.h"
  21. #include "target.h"
  22. #include "linux-nat.h"
  23. #include <sys/types.h>
  24. #include <signal.h>
  25. #include <sys/user.h>
  26. #include <sys/ioctl.h>
  27. #include <sys/uio.h>
  28. #include "gdbsupport/gdb_wait.h"
  29. #include <fcntl.h>
  30. #include <sys/procfs.h>
  31. #include "nat/gdb_ptrace.h"
  32. #include "nat/linux-ptrace.h"
  33. #include "inf-ptrace.h"
  34. #include <algorithm>
  35. #include <unordered_map>
  36. #include <list>
  37. /* Prototypes for supply_gregset etc. */
  38. #include "gregset.h"
  39. #include "ppc-tdep.h"
  40. #include "ppc-linux-tdep.h"
  41. /* Required when using the AUXV. */
  42. #include "elf/common.h"
  43. #include "auxv.h"
  44. #include "arch/ppc-linux-common.h"
  45. #include "arch/ppc-linux-tdesc.h"
  46. #include "nat/ppc-linux.h"
  47. #include "linux-tdep.h"
  48. #include "expop.h"
  49. /* Similarly for the hardware watchpoint support. These requests are used
  50. when the PowerPC HWDEBUG ptrace interface is not available. */
  51. #ifndef PTRACE_GET_DEBUGREG
  52. #define PTRACE_GET_DEBUGREG 25
  53. #endif
  54. #ifndef PTRACE_SET_DEBUGREG
  55. #define PTRACE_SET_DEBUGREG 26
  56. #endif
  57. #ifndef PTRACE_GETSIGINFO
  58. #define PTRACE_GETSIGINFO 0x4202
  59. #endif
  60. /* These requests are used when the PowerPC HWDEBUG ptrace interface is
  61. available. It exposes the debug facilities of PowerPC processors, as well
  62. as additional features of BookE processors, such as ranged breakpoints and
  63. watchpoints and hardware-accelerated condition evaluation. */
  64. #ifndef PPC_PTRACE_GETHWDBGINFO
  65. /* Not having PPC_PTRACE_GETHWDBGINFO defined means that the PowerPC HWDEBUG
  66. ptrace interface is not present in ptrace.h, so we'll have to pretty much
  67. include it all here so that the code at least compiles on older systems. */
  68. #define PPC_PTRACE_GETHWDBGINFO 0x89
  69. #define PPC_PTRACE_SETHWDEBUG 0x88
  70. #define PPC_PTRACE_DELHWDEBUG 0x87
  71. struct ppc_debug_info
  72. {
  73. uint32_t version; /* Only version 1 exists to date. */
  74. uint32_t num_instruction_bps;
  75. uint32_t num_data_bps;
  76. uint32_t num_condition_regs;
  77. uint32_t data_bp_alignment;
  78. uint32_t sizeof_condition; /* size of the DVC register. */
  79. uint64_t features;
  80. };
  81. /* Features will have bits indicating whether there is support for: */
  82. #define PPC_DEBUG_FEATURE_INSN_BP_RANGE 0x1
  83. #define PPC_DEBUG_FEATURE_INSN_BP_MASK 0x2
  84. #define PPC_DEBUG_FEATURE_DATA_BP_RANGE 0x4
  85. #define PPC_DEBUG_FEATURE_DATA_BP_MASK 0x8
  86. struct ppc_hw_breakpoint
  87. {
  88. uint32_t version; /* currently, version must be 1 */
  89. uint32_t trigger_type; /* only some combinations allowed */
  90. uint32_t addr_mode; /* address match mode */
  91. uint32_t condition_mode; /* break/watchpoint condition flags */
  92. uint64_t addr; /* break/watchpoint address */
  93. uint64_t addr2; /* range end or mask */
  94. uint64_t condition_value; /* contents of the DVC register */
  95. };
  96. /* Trigger type. */
  97. #define PPC_BREAKPOINT_TRIGGER_EXECUTE 0x1
  98. #define PPC_BREAKPOINT_TRIGGER_READ 0x2
  99. #define PPC_BREAKPOINT_TRIGGER_WRITE 0x4
  100. #define PPC_BREAKPOINT_TRIGGER_RW 0x6
  101. /* Address mode. */
  102. #define PPC_BREAKPOINT_MODE_EXACT 0x0
  103. #define PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE 0x1
  104. #define PPC_BREAKPOINT_MODE_RANGE_EXCLUSIVE 0x2
  105. #define PPC_BREAKPOINT_MODE_MASK 0x3
  106. /* Condition mode. */
  107. #define PPC_BREAKPOINT_CONDITION_NONE 0x0
  108. #define PPC_BREAKPOINT_CONDITION_AND 0x1
  109. #define PPC_BREAKPOINT_CONDITION_EXACT 0x1
  110. #define PPC_BREAKPOINT_CONDITION_OR 0x2
  111. #define PPC_BREAKPOINT_CONDITION_AND_OR 0x3
  112. #define PPC_BREAKPOINT_CONDITION_BE_ALL 0x00ff0000
  113. #define PPC_BREAKPOINT_CONDITION_BE_SHIFT 16
  114. #define PPC_BREAKPOINT_CONDITION_BE(n) \
  115. (1<<((n)+PPC_BREAKPOINT_CONDITION_BE_SHIFT))
  116. #endif /* PPC_PTRACE_GETHWDBGINFO */
  117. /* Feature defined on Linux kernel v3.9: DAWR interface, that enables wider
  118. watchpoint (up to 512 bytes). */
  119. #ifndef PPC_DEBUG_FEATURE_DATA_BP_DAWR
  120. #define PPC_DEBUG_FEATURE_DATA_BP_DAWR 0x10
  121. #endif /* PPC_DEBUG_FEATURE_DATA_BP_DAWR */
  122. /* Feature defined on Linux kernel v5.1: Second watchpoint support. */
  123. #ifndef PPC_DEBUG_FEATURE_DATA_BP_ARCH_31
  124. #define PPC_DEBUG_FEATURE_DATA_BP_ARCH_31 0x20
  125. #endif /* PPC_DEBUG_FEATURE_DATA_BP_ARCH_31 */
  126. /* The version of the PowerPC HWDEBUG kernel interface that we will use, if
  127. available. */
  128. #define PPC_DEBUG_CURRENT_VERSION 1
  129. /* Similarly for the general-purpose (gp0 -- gp31)
  130. and floating-point registers (fp0 -- fp31). */
  131. #ifndef PTRACE_GETREGS
  132. #define PTRACE_GETREGS 12
  133. #endif
  134. #ifndef PTRACE_SETREGS
  135. #define PTRACE_SETREGS 13
  136. #endif
  137. #ifndef PTRACE_GETFPREGS
  138. #define PTRACE_GETFPREGS 14
  139. #endif
  140. #ifndef PTRACE_SETFPREGS
  141. #define PTRACE_SETFPREGS 15
  142. #endif
  143. /* This oddity is because the Linux kernel defines elf_vrregset_t as
  144. an array of 33 16 bytes long elements. I.e. it leaves out vrsave.
  145. However the PTRACE_GETVRREGS and PTRACE_SETVRREGS requests return
  146. the vrsave as an extra 4 bytes at the end. I opted for creating a
  147. flat array of chars, so that it is easier to manipulate for gdb.
  148. There are 32 vector registers 16 bytes longs, plus a VSCR register
  149. which is only 4 bytes long, but is fetched as a 16 bytes
  150. quantity. Up to here we have the elf_vrregset_t structure.
  151. Appended to this there is space for the VRSAVE register: 4 bytes.
  152. Even though this vrsave register is not included in the regset
  153. typedef, it is handled by the ptrace requests.
  154. The layout is like this (where x is the actual value of the vscr reg): */
  155. /* *INDENT-OFF* */
  156. /*
  157. Big-Endian:
  158. |.|.|.|.|.....|.|.|.|.||.|.|.|x||.|
  159. <-------> <-------><-------><->
  160. VR0 VR31 VSCR VRSAVE
  161. Little-Endian:
  162. |.|.|.|.|.....|.|.|.|.||X|.|.|.||.|
  163. <-------> <-------><-------><->
  164. VR0 VR31 VSCR VRSAVE
  165. */
  166. /* *INDENT-ON* */
  167. typedef char gdb_vrregset_t[PPC_LINUX_SIZEOF_VRREGSET];
  168. /* This is the layout of the POWER7 VSX registers and the way they overlap
  169. with the existing FPR and VMX registers.
  170. VSR doubleword 0 VSR doubleword 1
  171. ----------------------------------------------------------------
  172. VSR[0] | FPR[0] | |
  173. ----------------------------------------------------------------
  174. VSR[1] | FPR[1] | |
  175. ----------------------------------------------------------------
  176. | ... | |
  177. | ... | |
  178. ----------------------------------------------------------------
  179. VSR[30] | FPR[30] | |
  180. ----------------------------------------------------------------
  181. VSR[31] | FPR[31] | |
  182. ----------------------------------------------------------------
  183. VSR[32] | VR[0] |
  184. ----------------------------------------------------------------
  185. VSR[33] | VR[1] |
  186. ----------------------------------------------------------------
  187. | ... |
  188. | ... |
  189. ----------------------------------------------------------------
  190. VSR[62] | VR[30] |
  191. ----------------------------------------------------------------
  192. VSR[63] | VR[31] |
  193. ----------------------------------------------------------------
  194. VSX has 64 128bit registers. The first 32 registers overlap with
  195. the FP registers (doubleword 0) and hence extend them with additional
  196. 64 bits (doubleword 1). The other 32 regs overlap with the VMX
  197. registers. */
  198. typedef char gdb_vsxregset_t[PPC_LINUX_SIZEOF_VSXREGSET];
  199. /* On PPC processors that support the Signal Processing Extension
  200. (SPE) APU, the general-purpose registers are 64 bits long.
  201. However, the ordinary Linux kernel PTRACE_PEEKUSER / PTRACE_POKEUSER
  202. ptrace calls only access the lower half of each register, to allow
  203. them to behave the same way they do on non-SPE systems. There's a
  204. separate pair of calls, PTRACE_GETEVRREGS / PTRACE_SETEVRREGS, that
  205. read and write the top halves of all the general-purpose registers
  206. at once, along with some SPE-specific registers.
  207. GDB itself continues to claim the general-purpose registers are 32
  208. bits long. It has unnamed raw registers that hold the upper halves
  209. of the gprs, and the full 64-bit SIMD views of the registers,
  210. 'ev0' -- 'ev31', are pseudo-registers that splice the top and
  211. bottom halves together.
  212. This is the structure filled in by PTRACE_GETEVRREGS and written to
  213. the inferior's registers by PTRACE_SETEVRREGS. */
  214. struct gdb_evrregset_t
  215. {
  216. unsigned long evr[32];
  217. unsigned long long acc;
  218. unsigned long spefscr;
  219. };
  220. /* Non-zero if our kernel may support the PTRACE_GETVSXREGS and
  221. PTRACE_SETVSXREGS requests, for reading and writing the VSX
  222. POWER7 registers 0 through 31. Zero if we've tried one of them and
  223. gotten an error. Note that VSX registers 32 through 63 overlap
  224. with VR registers 0 through 31. */
  225. int have_ptrace_getsetvsxregs = 1;
  226. /* Non-zero if our kernel may support the PTRACE_GETVRREGS and
  227. PTRACE_SETVRREGS requests, for reading and writing the Altivec
  228. registers. Zero if we've tried one of them and gotten an
  229. error. */
  230. int have_ptrace_getvrregs = 1;
  231. /* Non-zero if our kernel may support the PTRACE_GETEVRREGS and
  232. PTRACE_SETEVRREGS requests, for reading and writing the SPE
  233. registers. Zero if we've tried one of them and gotten an
  234. error. */
  235. int have_ptrace_getsetevrregs = 1;
  236. /* Non-zero if our kernel may support the PTRACE_GETREGS and
  237. PTRACE_SETREGS requests, for reading and writing the
  238. general-purpose registers. Zero if we've tried one of
  239. them and gotten an error. */
  240. int have_ptrace_getsetregs = 1;
  241. /* Non-zero if our kernel may support the PTRACE_GETFPREGS and
  242. PTRACE_SETFPREGS requests, for reading and writing the
  243. floating-pointers registers. Zero if we've tried one of
  244. them and gotten an error. */
  245. int have_ptrace_getsetfpregs = 1;
  246. /* Private arch info associated with each thread lwp_info object, used
  247. for debug register handling. */
  248. struct arch_lwp_info
  249. {
  250. /* When true, indicates that the debug registers installed in the
  251. thread no longer correspond to the watchpoints and breakpoints
  252. requested by GDB. */
  253. bool debug_regs_stale;
  254. /* We need a back-reference to the PTID of the thread so that we can
  255. cleanup the debug register state of the thread in
  256. low_delete_thread. */
  257. ptid_t lwp_ptid;
  258. };
  259. /* Class used to detect which set of ptrace requests that
  260. ppc_linux_nat_target will use to install and remove hardware
  261. breakpoints and watchpoints.
  262. The interface is only detected once, testing the ptrace calls. The
  263. result can indicate that no interface is available.
  264. The Linux kernel provides two different sets of ptrace requests to
  265. handle hardware watchpoints and breakpoints for Power:
  266. - PPC_PTRACE_GETHWDBGINFO, PPC_PTRACE_SETHWDEBUG, and
  267. PPC_PTRACE_DELHWDEBUG.
  268. Or
  269. - PTRACE_SET_DEBUGREG and PTRACE_GET_DEBUGREG
  270. The first set is the more flexible one and allows setting watchpoints
  271. with a variable watched region length and, for BookE processors,
  272. multiple types of debug registers (e.g. hardware breakpoints and
  273. hardware-assisted conditions for watchpoints). The second one only
  274. allows setting one debug register, a watchpoint, so we only use it if
  275. the first one is not available. */
  276. class ppc_linux_dreg_interface
  277. {
  278. public:
  279. ppc_linux_dreg_interface ()
  280. : m_interface (), m_hwdebug_info ()
  281. {
  282. };
  283. DISABLE_COPY_AND_ASSIGN (ppc_linux_dreg_interface);
  284. /* One and only one of these three functions returns true, indicating
  285. whether the corresponding interface is the one we detected. The
  286. interface must already have been detected as a precontidion. */
  287. bool hwdebug_p ()
  288. {
  289. gdb_assert (detected_p ());
  290. return *m_interface == HWDEBUG;
  291. }
  292. bool debugreg_p ()
  293. {
  294. gdb_assert (detected_p ());
  295. return *m_interface == DEBUGREG;
  296. }
  297. bool unavailable_p ()
  298. {
  299. gdb_assert (detected_p ());
  300. return *m_interface == UNAVAILABLE;
  301. }
  302. /* Returns the debug register capabilities of the target. Should only
  303. be called if the interface is HWDEBUG. */
  304. const struct ppc_debug_info &hwdebug_info ()
  305. {
  306. gdb_assert (hwdebug_p ());
  307. return m_hwdebug_info;
  308. }
  309. /* Returns true if the interface has already been detected. This is
  310. useful for cases when we know there is no work to be done if the
  311. interface hasn't been detected yet. */
  312. bool detected_p ()
  313. {
  314. return m_interface.has_value ();
  315. }
  316. /* Detect the available interface, if any, if it hasn't been detected
  317. before, using PTID for the necessary ptrace calls. */
  318. void detect (const ptid_t &ptid)
  319. {
  320. if (m_interface.has_value ())
  321. return;
  322. gdb_assert (ptid.lwp_p ());
  323. bool no_features = false;
  324. if (ptrace (PPC_PTRACE_GETHWDBGINFO, ptid.lwp (), 0, &m_hwdebug_info)
  325. >= 0)
  326. {
  327. /* If there are no advertised features, we don't use the
  328. HWDEBUG interface and try the DEBUGREG interface instead.
  329. It shouldn't be necessary to do this, however, when the
  330. kernel is configured without CONFIG_HW_BREAKPOINTS (selected
  331. by CONFIG_PERF_EVENTS), there is a bug that causes
  332. watchpoints installed with the HWDEBUG interface not to
  333. trigger. When this is the case, features will be zero,
  334. which we use as an indicator to fall back to the DEBUGREG
  335. interface. */
  336. if (m_hwdebug_info.features != 0)
  337. {
  338. m_interface.emplace (HWDEBUG);
  339. return;
  340. }
  341. else
  342. no_features = true;
  343. }
  344. /* EIO indicates that the request is invalid, so we try DEBUGREG
  345. next. Technically, it can also indicate other failures, but we
  346. can't differentiate those.
  347. Other errors could happen for various reasons. We could get an
  348. ESRCH if the traced thread was killed by a signal. Trying to
  349. detect the interface with another thread in the future would be
  350. complicated, as callers would have to handle an "unknown
  351. interface" case. It's also unclear if raising an exception
  352. here would be safe.
  353. Other errors, such as ENODEV, could be more permanent and cause
  354. a failure for any thread.
  355. For simplicity, with all errors other than EIO, we set the
  356. interface to UNAVAILABLE and don't try DEBUGREG. If DEBUGREG
  357. fails too, we'll also set the interface to UNAVAILABLE. It's
  358. unlikely that trying the DEBUGREG interface with this same thread
  359. would work, for errors other than EIO. This means that these
  360. errors will cause hardware watchpoints and breakpoints to become
  361. unavailable throughout a GDB session. */
  362. if (no_features || errno == EIO)
  363. {
  364. unsigned long wp;
  365. if (ptrace (PTRACE_GET_DEBUGREG, ptid.lwp (), 0, &wp) >= 0)
  366. {
  367. m_interface.emplace (DEBUGREG);
  368. return;
  369. }
  370. }
  371. if (errno != EIO)
  372. warning (_("Error when detecting the debug register interface. "
  373. "Debug registers will be unavailable."));
  374. m_interface.emplace (UNAVAILABLE);
  375. return;
  376. }
  377. private:
  378. /* HWDEBUG represents the set of calls PPC_PTRACE_GETHWDBGINFO,
  379. PPC_PTRACE_SETHWDEBUG and PPC_PTRACE_DELHWDEBUG.
  380. DEBUGREG represents the set of calls PTRACE_SET_DEBUGREG and
  381. PTRACE_GET_DEBUGREG.
  382. UNAVAILABLE can indicate that the kernel doesn't support any of the
  383. two sets of requests or that there was an error when we tried to
  384. detect wich interface is available. */
  385. enum debug_reg_interface
  386. {
  387. UNAVAILABLE,
  388. HWDEBUG,
  389. DEBUGREG
  390. };
  391. /* The interface option. Initialized if has_value () returns true. */
  392. gdb::optional<enum debug_reg_interface> m_interface;
  393. /* The info returned by the kernel with PPC_PTRACE_GETHWDBGINFO. Only
  394. valid if we determined that the interface is HWDEBUG. */
  395. struct ppc_debug_info m_hwdebug_info;
  396. };
  397. /* Per-process information. This includes the hardware watchpoints and
  398. breakpoints that GDB requested to this target. */
  399. struct ppc_linux_process_info
  400. {
  401. /* The list of hardware watchpoints and breakpoints that GDB requested
  402. for this process.
  403. Only used when the interface is HWDEBUG. */
  404. std::list<struct ppc_hw_breakpoint> requested_hw_bps;
  405. /* The watchpoint value that GDB requested for this process.
  406. Only used when the interface is DEBUGREG. */
  407. gdb::optional<long> requested_wp_val;
  408. };
  409. struct ppc_linux_nat_target final : public linux_nat_target
  410. {
  411. /* Add our register access methods. */
  412. void fetch_registers (struct regcache *, int) override;
  413. void store_registers (struct regcache *, int) override;
  414. /* Add our breakpoint/watchpoint methods. */
  415. int can_use_hw_breakpoint (enum bptype, int, int) override;
  416. int insert_hw_breakpoint (struct gdbarch *, struct bp_target_info *)
  417. override;
  418. int remove_hw_breakpoint (struct gdbarch *, struct bp_target_info *)
  419. override;
  420. int region_ok_for_hw_watchpoint (CORE_ADDR, int) override;
  421. int insert_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
  422. struct expression *) override;
  423. int remove_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
  424. struct expression *) override;
  425. int insert_mask_watchpoint (CORE_ADDR, CORE_ADDR, enum target_hw_bp_type)
  426. override;
  427. int remove_mask_watchpoint (CORE_ADDR, CORE_ADDR, enum target_hw_bp_type)
  428. override;
  429. bool watchpoint_addr_within_range (CORE_ADDR, CORE_ADDR, int) override;
  430. bool can_accel_watchpoint_condition (CORE_ADDR, int, int, struct expression *)
  431. override;
  432. int masked_watch_num_registers (CORE_ADDR, CORE_ADDR) override;
  433. int ranged_break_num_registers () override;
  434. const struct target_desc *read_description () override;
  435. int auxv_parse (gdb_byte **readptr,
  436. gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
  437. override;
  438. /* Override linux_nat_target low methods. */
  439. bool low_stopped_by_watchpoint () override;
  440. bool low_stopped_data_address (CORE_ADDR *) override;
  441. void low_new_thread (struct lwp_info *lp) override;
  442. void low_delete_thread (arch_lwp_info *) override;
  443. void low_new_fork (struct lwp_info *, pid_t) override;
  444. void low_new_clone (struct lwp_info *, pid_t) override;
  445. void low_forget_process (pid_t pid) override;
  446. void low_prepare_to_resume (struct lwp_info *) override;
  447. private:
  448. void copy_thread_dreg_state (const ptid_t &parent_ptid,
  449. const ptid_t &child_ptid);
  450. void mark_thread_stale (struct lwp_info *lp);
  451. void mark_debug_registers_changed (pid_t pid);
  452. void register_hw_breakpoint (pid_t pid,
  453. const struct ppc_hw_breakpoint &bp);
  454. void clear_hw_breakpoint (pid_t pid,
  455. const struct ppc_hw_breakpoint &a);
  456. void register_wp (pid_t pid, long wp_value);
  457. void clear_wp (pid_t pid);
  458. bool can_use_watchpoint_cond_accel (void);
  459. void calculate_dvc (CORE_ADDR addr, int len,
  460. CORE_ADDR data_value,
  461. uint32_t *condition_mode,
  462. uint64_t *condition_value);
  463. int check_condition (CORE_ADDR watch_addr,
  464. struct expression *cond,
  465. CORE_ADDR *data_value, int *len);
  466. int num_memory_accesses (const std::vector<value_ref_ptr> &chain);
  467. int get_trigger_type (enum target_hw_bp_type type);
  468. void create_watchpoint_request (struct ppc_hw_breakpoint *p,
  469. CORE_ADDR addr,
  470. int len,
  471. enum target_hw_bp_type type,
  472. struct expression *cond,
  473. int insert);
  474. bool hwdebug_point_cmp (const struct ppc_hw_breakpoint &a,
  475. const struct ppc_hw_breakpoint &b);
  476. void init_arch_lwp_info (struct lwp_info *lp);
  477. arch_lwp_info *get_arch_lwp_info (struct lwp_info *lp);
  478. /* The ptrace interface we'll use to install hardware watchpoints and
  479. breakpoints (debug registers). */
  480. ppc_linux_dreg_interface m_dreg_interface;
  481. /* A map from pids to structs containing info specific to each
  482. process. */
  483. std::unordered_map<pid_t, ppc_linux_process_info> m_process_info;
  484. /* Callable object to hash ptids by their lwp number. */
  485. struct ptid_hash
  486. {
  487. std::size_t operator() (const ptid_t &ptid) const
  488. {
  489. return std::hash<long>{} (ptid.lwp ());
  490. }
  491. };
  492. /* A map from ptid_t objects to a list of pairs of slots and hardware
  493. breakpoint objects. This keeps track of which hardware breakpoints
  494. and watchpoints were last installed in each slot of each thread.
  495. Only used when the interface is HWDEBUG. */
  496. std::unordered_map <ptid_t,
  497. std::list<std::pair<long, ppc_hw_breakpoint>>,
  498. ptid_hash> m_installed_hw_bps;
  499. };
  500. static ppc_linux_nat_target the_ppc_linux_nat_target;
  501. /* *INDENT-OFF* */
  502. /* registers layout, as presented by the ptrace interface:
  503. PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5, PT_R6, PT_R7,
  504. PT_R8, PT_R9, PT_R10, PT_R11, PT_R12, PT_R13, PT_R14, PT_R15,
  505. PT_R16, PT_R17, PT_R18, PT_R19, PT_R20, PT_R21, PT_R22, PT_R23,
  506. PT_R24, PT_R25, PT_R26, PT_R27, PT_R28, PT_R29, PT_R30, PT_R31,
  507. PT_FPR0, PT_FPR0 + 2, PT_FPR0 + 4, PT_FPR0 + 6,
  508. PT_FPR0 + 8, PT_FPR0 + 10, PT_FPR0 + 12, PT_FPR0 + 14,
  509. PT_FPR0 + 16, PT_FPR0 + 18, PT_FPR0 + 20, PT_FPR0 + 22,
  510. PT_FPR0 + 24, PT_FPR0 + 26, PT_FPR0 + 28, PT_FPR0 + 30,
  511. PT_FPR0 + 32, PT_FPR0 + 34, PT_FPR0 + 36, PT_FPR0 + 38,
  512. PT_FPR0 + 40, PT_FPR0 + 42, PT_FPR0 + 44, PT_FPR0 + 46,
  513. PT_FPR0 + 48, PT_FPR0 + 50, PT_FPR0 + 52, PT_FPR0 + 54,
  514. PT_FPR0 + 56, PT_FPR0 + 58, PT_FPR0 + 60, PT_FPR0 + 62,
  515. PT_NIP, PT_MSR, PT_CCR, PT_LNK, PT_CTR, PT_XER, PT_MQ */
  516. /* *INDENT_ON * */
  517. static int
  518. ppc_register_u_addr (struct gdbarch *gdbarch, int regno)
  519. {
  520. int u_addr = -1;
  521. ppc_gdbarch_tdep *tdep = (ppc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  522. /* NOTE: cagney/2003-11-25: This is the word size used by the ptrace
  523. interface, and not the wordsize of the program's ABI. */
  524. int wordsize = sizeof (long);
  525. /* General purpose registers occupy 1 slot each in the buffer. */
  526. if (regno >= tdep->ppc_gp0_regnum
  527. && regno < tdep->ppc_gp0_regnum + ppc_num_gprs)
  528. u_addr = ((regno - tdep->ppc_gp0_regnum + PT_R0) * wordsize);
  529. /* Floating point regs: eight bytes each in both 32- and 64-bit
  530. ptrace interfaces. Thus, two slots each in 32-bit interface, one
  531. slot each in 64-bit interface. */
  532. if (tdep->ppc_fp0_regnum >= 0
  533. && regno >= tdep->ppc_fp0_regnum
  534. && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)
  535. u_addr = (PT_FPR0 * wordsize) + ((regno - tdep->ppc_fp0_regnum) * 8);
  536. /* UISA special purpose registers: 1 slot each. */
  537. if (regno == gdbarch_pc_regnum (gdbarch))
  538. u_addr = PT_NIP * wordsize;
  539. if (regno == tdep->ppc_lr_regnum)
  540. u_addr = PT_LNK * wordsize;
  541. if (regno == tdep->ppc_cr_regnum)
  542. u_addr = PT_CCR * wordsize;
  543. if (regno == tdep->ppc_xer_regnum)
  544. u_addr = PT_XER * wordsize;
  545. if (regno == tdep->ppc_ctr_regnum)
  546. u_addr = PT_CTR * wordsize;
  547. #ifdef PT_MQ
  548. if (regno == tdep->ppc_mq_regnum)
  549. u_addr = PT_MQ * wordsize;
  550. #endif
  551. if (regno == tdep->ppc_ps_regnum)
  552. u_addr = PT_MSR * wordsize;
  553. if (regno == PPC_ORIG_R3_REGNUM)
  554. u_addr = PT_ORIG_R3 * wordsize;
  555. if (regno == PPC_TRAP_REGNUM)
  556. u_addr = PT_TRAP * wordsize;
  557. if (tdep->ppc_fpscr_regnum >= 0
  558. && regno == tdep->ppc_fpscr_regnum)
  559. {
  560. /* NOTE: cagney/2005-02-08: On some 64-bit GNU/Linux systems the
  561. kernel headers incorrectly contained the 32-bit definition of
  562. PT_FPSCR. For the 32-bit definition, floating-point
  563. registers occupy two 32-bit "slots", and the FPSCR lives in
  564. the second half of such a slot-pair (hence +1). For 64-bit,
  565. the FPSCR instead occupies the full 64-bit 2-word-slot and
  566. hence no adjustment is necessary. Hack around this. */
  567. if (wordsize == 8 && PT_FPSCR == (48 + 32 + 1))
  568. u_addr = (48 + 32) * wordsize;
  569. /* If the FPSCR is 64-bit wide, we need to fetch the whole 64-bit
  570. slot and not just its second word. The PT_FPSCR supplied when
  571. GDB is compiled as a 32-bit app doesn't reflect this. */
  572. else if (wordsize == 4 && register_size (gdbarch, regno) == 8
  573. && PT_FPSCR == (48 + 2*32 + 1))
  574. u_addr = (48 + 2*32) * wordsize;
  575. else
  576. u_addr = PT_FPSCR * wordsize;
  577. }
  578. return u_addr;
  579. }
  580. /* The Linux kernel ptrace interface for POWER7 VSX registers uses the
  581. registers set mechanism, as opposed to the interface for all the
  582. other registers, that stores/fetches each register individually. */
  583. static void
  584. fetch_vsx_registers (struct regcache *regcache, int tid, int regno)
  585. {
  586. int ret;
  587. gdb_vsxregset_t regs;
  588. const struct regset *vsxregset = ppc_linux_vsxregset ();
  589. ret = ptrace (PTRACE_GETVSXREGS, tid, 0, &regs);
  590. if (ret < 0)
  591. {
  592. if (errno == EIO)
  593. {
  594. have_ptrace_getsetvsxregs = 0;
  595. return;
  596. }
  597. perror_with_name (_("Unable to fetch VSX registers"));
  598. }
  599. vsxregset->supply_regset (vsxregset, regcache, regno, &regs,
  600. PPC_LINUX_SIZEOF_VSXREGSET);
  601. }
  602. /* The Linux kernel ptrace interface for AltiVec registers uses the
  603. registers set mechanism, as opposed to the interface for all the
  604. other registers, that stores/fetches each register individually. */
  605. static void
  606. fetch_altivec_registers (struct regcache *regcache, int tid,
  607. int regno)
  608. {
  609. int ret;
  610. gdb_vrregset_t regs;
  611. struct gdbarch *gdbarch = regcache->arch ();
  612. const struct regset *vrregset = ppc_linux_vrregset (gdbarch);
  613. ret = ptrace (PTRACE_GETVRREGS, tid, 0, &regs);
  614. if (ret < 0)
  615. {
  616. if (errno == EIO)
  617. {
  618. have_ptrace_getvrregs = 0;
  619. return;
  620. }
  621. perror_with_name (_("Unable to fetch AltiVec registers"));
  622. }
  623. vrregset->supply_regset (vrregset, regcache, regno, &regs,
  624. PPC_LINUX_SIZEOF_VRREGSET);
  625. }
  626. /* Fetch the top 32 bits of TID's general-purpose registers and the
  627. SPE-specific registers, and place the results in EVRREGSET. If we
  628. don't support PTRACE_GETEVRREGS, then just fill EVRREGSET with
  629. zeros.
  630. All the logic to deal with whether or not the PTRACE_GETEVRREGS and
  631. PTRACE_SETEVRREGS requests are supported is isolated here, and in
  632. set_spe_registers. */
  633. static void
  634. get_spe_registers (int tid, struct gdb_evrregset_t *evrregset)
  635. {
  636. if (have_ptrace_getsetevrregs)
  637. {
  638. if (ptrace (PTRACE_GETEVRREGS, tid, 0, evrregset) >= 0)
  639. return;
  640. else
  641. {
  642. /* EIO means that the PTRACE_GETEVRREGS request isn't supported;
  643. we just return zeros. */
  644. if (errno == EIO)
  645. have_ptrace_getsetevrregs = 0;
  646. else
  647. /* Anything else needs to be reported. */
  648. perror_with_name (_("Unable to fetch SPE registers"));
  649. }
  650. }
  651. memset (evrregset, 0, sizeof (*evrregset));
  652. }
  653. /* Supply values from TID for SPE-specific raw registers: the upper
  654. halves of the GPRs, the accumulator, and the spefscr. REGNO must
  655. be the number of an upper half register, acc, spefscr, or -1 to
  656. supply the values of all registers. */
  657. static void
  658. fetch_spe_register (struct regcache *regcache, int tid, int regno)
  659. {
  660. struct gdbarch *gdbarch = regcache->arch ();
  661. ppc_gdbarch_tdep *tdep = (ppc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  662. struct gdb_evrregset_t evrregs;
  663. gdb_assert (sizeof (evrregs.evr[0])
  664. == register_size (gdbarch, tdep->ppc_ev0_upper_regnum));
  665. gdb_assert (sizeof (evrregs.acc)
  666. == register_size (gdbarch, tdep->ppc_acc_regnum));
  667. gdb_assert (sizeof (evrregs.spefscr)
  668. == register_size (gdbarch, tdep->ppc_spefscr_regnum));
  669. get_spe_registers (tid, &evrregs);
  670. if (regno == -1)
  671. {
  672. int i;
  673. for (i = 0; i < ppc_num_gprs; i++)
  674. regcache->raw_supply (tdep->ppc_ev0_upper_regnum + i, &evrregs.evr[i]);
  675. }
  676. else if (tdep->ppc_ev0_upper_regnum <= regno
  677. && regno < tdep->ppc_ev0_upper_regnum + ppc_num_gprs)
  678. regcache->raw_supply (regno,
  679. &evrregs.evr[regno - tdep->ppc_ev0_upper_regnum]);
  680. if (regno == -1
  681. || regno == tdep->ppc_acc_regnum)
  682. regcache->raw_supply (tdep->ppc_acc_regnum, &evrregs.acc);
  683. if (regno == -1
  684. || regno == tdep->ppc_spefscr_regnum)
  685. regcache->raw_supply (tdep->ppc_spefscr_regnum, &evrregs.spefscr);
  686. }
  687. /* Use ptrace to fetch all registers from the register set with note
  688. type REGSET_ID, size REGSIZE, and layout described by REGSET, from
  689. process/thread TID and supply their values to REGCACHE. If ptrace
  690. returns ENODATA to indicate the regset is unavailable, mark the
  691. registers as unavailable in REGCACHE. */
  692. static void
  693. fetch_regset (struct regcache *regcache, int tid,
  694. int regset_id, int regsetsize, const struct regset *regset)
  695. {
  696. void *buf = alloca (regsetsize);
  697. struct iovec iov;
  698. iov.iov_base = buf;
  699. iov.iov_len = regsetsize;
  700. if (ptrace (PTRACE_GETREGSET, tid, regset_id, &iov) < 0)
  701. {
  702. if (errno == ENODATA)
  703. regset->supply_regset (regset, regcache, -1, NULL, regsetsize);
  704. else
  705. perror_with_name (_("Couldn't get register set"));
  706. }
  707. else
  708. regset->supply_regset (regset, regcache, -1, buf, regsetsize);
  709. }
  710. /* Use ptrace to store register REGNUM of the regset with note type
  711. REGSET_ID, size REGSETSIZE, and layout described by REGSET, from
  712. REGCACHE back to process/thread TID. If REGNUM is -1 all registers
  713. in the set are collected and stored. */
  714. static void
  715. store_regset (const struct regcache *regcache, int tid, int regnum,
  716. int regset_id, int regsetsize, const struct regset *regset)
  717. {
  718. void *buf = alloca (regsetsize);
  719. struct iovec iov;
  720. iov.iov_base = buf;
  721. iov.iov_len = regsetsize;
  722. /* Make sure that the buffer that will be stored has up to date values
  723. for the registers that won't be collected. */
  724. if (ptrace (PTRACE_GETREGSET, tid, regset_id, &iov) < 0)
  725. perror_with_name (_("Couldn't get register set"));
  726. regset->collect_regset (regset, regcache, regnum, buf, regsetsize);
  727. if (ptrace (PTRACE_SETREGSET, tid, regset_id, &iov) < 0)
  728. perror_with_name (_("Couldn't set register set"));
  729. }
  730. /* Check whether the kernel provides a register set with number
  731. REGSET_ID of size REGSETSIZE for process/thread TID. */
  732. static bool
  733. check_regset (int tid, int regset_id, int regsetsize)
  734. {
  735. void *buf = alloca (regsetsize);
  736. struct iovec iov;
  737. iov.iov_base = buf;
  738. iov.iov_len = regsetsize;
  739. if (ptrace (PTRACE_GETREGSET, tid, regset_id, &iov) >= 0
  740. || errno == ENODATA)
  741. return true;
  742. else
  743. return false;
  744. }
  745. static void
  746. fetch_register (struct regcache *regcache, int tid, int regno)
  747. {
  748. struct gdbarch *gdbarch = regcache->arch ();
  749. ppc_gdbarch_tdep *tdep = (ppc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  750. /* This isn't really an address. But ptrace thinks of it as one. */
  751. CORE_ADDR regaddr = ppc_register_u_addr (gdbarch, regno);
  752. int bytes_transferred;
  753. gdb_byte buf[PPC_MAX_REGISTER_SIZE];
  754. if (altivec_register_p (gdbarch, regno))
  755. {
  756. /* If this is the first time through, or if it is not the first
  757. time through, and we have confirmed that there is kernel
  758. support for such a ptrace request, then go and fetch the
  759. register. */
  760. if (have_ptrace_getvrregs)
  761. {
  762. fetch_altivec_registers (regcache, tid, regno);
  763. return;
  764. }
  765. /* If we have discovered that there is no ptrace support for
  766. AltiVec registers, fall through and return zeroes, because
  767. regaddr will be -1 in this case. */
  768. }
  769. else if (vsx_register_p (gdbarch, regno))
  770. {
  771. if (have_ptrace_getsetvsxregs)
  772. {
  773. fetch_vsx_registers (regcache, tid, regno);
  774. return;
  775. }
  776. }
  777. else if (spe_register_p (gdbarch, regno))
  778. {
  779. fetch_spe_register (regcache, tid, regno);
  780. return;
  781. }
  782. else if (regno == PPC_DSCR_REGNUM)
  783. {
  784. gdb_assert (tdep->ppc_dscr_regnum != -1);
  785. fetch_regset (regcache, tid, NT_PPC_DSCR,
  786. PPC_LINUX_SIZEOF_DSCRREGSET,
  787. &ppc32_linux_dscrregset);
  788. return;
  789. }
  790. else if (regno == PPC_PPR_REGNUM)
  791. {
  792. gdb_assert (tdep->ppc_ppr_regnum != -1);
  793. fetch_regset (regcache, tid, NT_PPC_PPR,
  794. PPC_LINUX_SIZEOF_PPRREGSET,
  795. &ppc32_linux_pprregset);
  796. return;
  797. }
  798. else if (regno == PPC_TAR_REGNUM)
  799. {
  800. gdb_assert (tdep->ppc_tar_regnum != -1);
  801. fetch_regset (regcache, tid, NT_PPC_TAR,
  802. PPC_LINUX_SIZEOF_TARREGSET,
  803. &ppc32_linux_tarregset);
  804. return;
  805. }
  806. else if (PPC_IS_EBB_REGNUM (regno))
  807. {
  808. gdb_assert (tdep->have_ebb);
  809. fetch_regset (regcache, tid, NT_PPC_EBB,
  810. PPC_LINUX_SIZEOF_EBBREGSET,
  811. &ppc32_linux_ebbregset);
  812. return;
  813. }
  814. else if (PPC_IS_PMU_REGNUM (regno))
  815. {
  816. gdb_assert (tdep->ppc_mmcr0_regnum != -1);
  817. fetch_regset (regcache, tid, NT_PPC_PMU,
  818. PPC_LINUX_SIZEOF_PMUREGSET,
  819. &ppc32_linux_pmuregset);
  820. return;
  821. }
  822. else if (PPC_IS_TMSPR_REGNUM (regno))
  823. {
  824. gdb_assert (tdep->have_htm_spr);
  825. fetch_regset (regcache, tid, NT_PPC_TM_SPR,
  826. PPC_LINUX_SIZEOF_TM_SPRREGSET,
  827. &ppc32_linux_tm_sprregset);
  828. return;
  829. }
  830. else if (PPC_IS_CKPTGP_REGNUM (regno))
  831. {
  832. gdb_assert (tdep->have_htm_core);
  833. const struct regset *cgprregset = ppc_linux_cgprregset (gdbarch);
  834. fetch_regset (regcache, tid, NT_PPC_TM_CGPR,
  835. (tdep->wordsize == 4?
  836. PPC32_LINUX_SIZEOF_CGPRREGSET
  837. : PPC64_LINUX_SIZEOF_CGPRREGSET),
  838. cgprregset);
  839. return;
  840. }
  841. else if (PPC_IS_CKPTFP_REGNUM (regno))
  842. {
  843. gdb_assert (tdep->have_htm_fpu);
  844. fetch_regset (regcache, tid, NT_PPC_TM_CFPR,
  845. PPC_LINUX_SIZEOF_CFPRREGSET,
  846. &ppc32_linux_cfprregset);
  847. return;
  848. }
  849. else if (PPC_IS_CKPTVMX_REGNUM (regno))
  850. {
  851. gdb_assert (tdep->have_htm_altivec);
  852. const struct regset *cvmxregset = ppc_linux_cvmxregset (gdbarch);
  853. fetch_regset (regcache, tid, NT_PPC_TM_CVMX,
  854. PPC_LINUX_SIZEOF_CVMXREGSET,
  855. cvmxregset);
  856. return;
  857. }
  858. else if (PPC_IS_CKPTVSX_REGNUM (regno))
  859. {
  860. gdb_assert (tdep->have_htm_vsx);
  861. fetch_regset (regcache, tid, NT_PPC_TM_CVSX,
  862. PPC_LINUX_SIZEOF_CVSXREGSET,
  863. &ppc32_linux_cvsxregset);
  864. return;
  865. }
  866. else if (regno == PPC_CPPR_REGNUM)
  867. {
  868. gdb_assert (tdep->ppc_cppr_regnum != -1);
  869. fetch_regset (regcache, tid, NT_PPC_TM_CPPR,
  870. PPC_LINUX_SIZEOF_CPPRREGSET,
  871. &ppc32_linux_cpprregset);
  872. return;
  873. }
  874. else if (regno == PPC_CDSCR_REGNUM)
  875. {
  876. gdb_assert (tdep->ppc_cdscr_regnum != -1);
  877. fetch_regset (regcache, tid, NT_PPC_TM_CDSCR,
  878. PPC_LINUX_SIZEOF_CDSCRREGSET,
  879. &ppc32_linux_cdscrregset);
  880. return;
  881. }
  882. else if (regno == PPC_CTAR_REGNUM)
  883. {
  884. gdb_assert (tdep->ppc_ctar_regnum != -1);
  885. fetch_regset (regcache, tid, NT_PPC_TM_CTAR,
  886. PPC_LINUX_SIZEOF_CTARREGSET,
  887. &ppc32_linux_ctarregset);
  888. return;
  889. }
  890. if (regaddr == -1)
  891. {
  892. memset (buf, '\0', register_size (gdbarch, regno)); /* Supply zeroes */
  893. regcache->raw_supply (regno, buf);
  894. return;
  895. }
  896. /* Read the raw register using sizeof(long) sized chunks. On a
  897. 32-bit platform, 64-bit floating-point registers will require two
  898. transfers. */
  899. for (bytes_transferred = 0;
  900. bytes_transferred < register_size (gdbarch, regno);
  901. bytes_transferred += sizeof (long))
  902. {
  903. long l;
  904. errno = 0;
  905. l = ptrace (PTRACE_PEEKUSER, tid, (PTRACE_TYPE_ARG3) regaddr, 0);
  906. regaddr += sizeof (long);
  907. if (errno != 0)
  908. {
  909. char message[128];
  910. xsnprintf (message, sizeof (message), "reading register %s (#%d)",
  911. gdbarch_register_name (gdbarch, regno), regno);
  912. perror_with_name (message);
  913. }
  914. memcpy (&buf[bytes_transferred], &l, sizeof (l));
  915. }
  916. /* Now supply the register. Keep in mind that the regcache's idea
  917. of the register's size may not be a multiple of sizeof
  918. (long). */
  919. if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
  920. {
  921. /* Little-endian values are always found at the left end of the
  922. bytes transferred. */
  923. regcache->raw_supply (regno, buf);
  924. }
  925. else if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  926. {
  927. /* Big-endian values are found at the right end of the bytes
  928. transferred. */
  929. size_t padding = (bytes_transferred - register_size (gdbarch, regno));
  930. regcache->raw_supply (regno, buf + padding);
  931. }
  932. else
  933. internal_error (__FILE__, __LINE__,
  934. _("fetch_register: unexpected byte order: %d"),
  935. gdbarch_byte_order (gdbarch));
  936. }
  937. /* This function actually issues the request to ptrace, telling
  938. it to get all general-purpose registers and put them into the
  939. specified regset.
  940. If the ptrace request does not exist, this function returns 0
  941. and properly sets the have_ptrace_* flag. If the request fails,
  942. this function calls perror_with_name. Otherwise, if the request
  943. succeeds, then the regcache gets filled and 1 is returned. */
  944. static int
  945. fetch_all_gp_regs (struct regcache *regcache, int tid)
  946. {
  947. gdb_gregset_t gregset;
  948. if (ptrace (PTRACE_GETREGS, tid, 0, (void *) &gregset) < 0)
  949. {
  950. if (errno == EIO)
  951. {
  952. have_ptrace_getsetregs = 0;
  953. return 0;
  954. }
  955. perror_with_name (_("Couldn't get general-purpose registers."));
  956. }
  957. supply_gregset (regcache, (const gdb_gregset_t *) &gregset);
  958. return 1;
  959. }
  960. /* This is a wrapper for the fetch_all_gp_regs function. It is
  961. responsible for verifying if this target has the ptrace request
  962. that can be used to fetch all general-purpose registers at one
  963. shot. If it doesn't, then we should fetch them using the
  964. old-fashioned way, which is to iterate over the registers and
  965. request them one by one. */
  966. static void
  967. fetch_gp_regs (struct regcache *regcache, int tid)
  968. {
  969. struct gdbarch *gdbarch = regcache->arch ();
  970. ppc_gdbarch_tdep *tdep = (ppc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  971. int i;
  972. if (have_ptrace_getsetregs)
  973. if (fetch_all_gp_regs (regcache, tid))
  974. return;
  975. /* If we've hit this point, it doesn't really matter which
  976. architecture we are using. We just need to read the
  977. registers in the "old-fashioned way". */
  978. for (i = 0; i < ppc_num_gprs; i++)
  979. fetch_register (regcache, tid, tdep->ppc_gp0_regnum + i);
  980. }
  981. /* This function actually issues the request to ptrace, telling
  982. it to get all floating-point registers and put them into the
  983. specified regset.
  984. If the ptrace request does not exist, this function returns 0
  985. and properly sets the have_ptrace_* flag. If the request fails,
  986. this function calls perror_with_name. Otherwise, if the request
  987. succeeds, then the regcache gets filled and 1 is returned. */
  988. static int
  989. fetch_all_fp_regs (struct regcache *regcache, int tid)
  990. {
  991. gdb_fpregset_t fpregs;
  992. if (ptrace (PTRACE_GETFPREGS, tid, 0, (void *) &fpregs) < 0)
  993. {
  994. if (errno == EIO)
  995. {
  996. have_ptrace_getsetfpregs = 0;
  997. return 0;
  998. }
  999. perror_with_name (_("Couldn't get floating-point registers."));
  1000. }
  1001. supply_fpregset (regcache, (const gdb_fpregset_t *) &fpregs);
  1002. return 1;
  1003. }
  1004. /* This is a wrapper for the fetch_all_fp_regs function. It is
  1005. responsible for verifying if this target has the ptrace request
  1006. that can be used to fetch all floating-point registers at one
  1007. shot. If it doesn't, then we should fetch them using the
  1008. old-fashioned way, which is to iterate over the registers and
  1009. request them one by one. */
  1010. static void
  1011. fetch_fp_regs (struct regcache *regcache, int tid)
  1012. {
  1013. struct gdbarch *gdbarch = regcache->arch ();
  1014. ppc_gdbarch_tdep *tdep = (ppc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  1015. int i;
  1016. if (have_ptrace_getsetfpregs)
  1017. if (fetch_all_fp_regs (regcache, tid))
  1018. return;
  1019. /* If we've hit this point, it doesn't really matter which
  1020. architecture we are using. We just need to read the
  1021. registers in the "old-fashioned way". */
  1022. for (i = 0; i < ppc_num_fprs; i++)
  1023. fetch_register (regcache, tid, tdep->ppc_fp0_regnum + i);
  1024. }
  1025. static void
  1026. fetch_ppc_registers (struct regcache *regcache, int tid)
  1027. {
  1028. struct gdbarch *gdbarch = regcache->arch ();
  1029. ppc_gdbarch_tdep *tdep = (ppc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  1030. fetch_gp_regs (regcache, tid);
  1031. if (tdep->ppc_fp0_regnum >= 0)
  1032. fetch_fp_regs (regcache, tid);
  1033. fetch_register (regcache, tid, gdbarch_pc_regnum (gdbarch));
  1034. if (tdep->ppc_ps_regnum != -1)
  1035. fetch_register (regcache, tid, tdep->ppc_ps_regnum);
  1036. if (tdep->ppc_cr_regnum != -1)
  1037. fetch_register (regcache, tid, tdep->ppc_cr_regnum);
  1038. if (tdep->ppc_lr_regnum != -1)
  1039. fetch_register (regcache, tid, tdep->ppc_lr_regnum);
  1040. if (tdep->ppc_ctr_regnum != -1)
  1041. fetch_register (regcache, tid, tdep->ppc_ctr_regnum);
  1042. if (tdep->ppc_xer_regnum != -1)
  1043. fetch_register (regcache, tid, tdep->ppc_xer_regnum);
  1044. if (tdep->ppc_mq_regnum != -1)
  1045. fetch_register (regcache, tid, tdep->ppc_mq_regnum);
  1046. if (ppc_linux_trap_reg_p (gdbarch))
  1047. {
  1048. fetch_register (regcache, tid, PPC_ORIG_R3_REGNUM);
  1049. fetch_register (regcache, tid, PPC_TRAP_REGNUM);
  1050. }
  1051. if (tdep->ppc_fpscr_regnum != -1)
  1052. fetch_register (regcache, tid, tdep->ppc_fpscr_regnum);
  1053. if (have_ptrace_getvrregs)
  1054. if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1)
  1055. fetch_altivec_registers (regcache, tid, -1);
  1056. if (have_ptrace_getsetvsxregs)
  1057. if (tdep->ppc_vsr0_upper_regnum != -1)
  1058. fetch_vsx_registers (regcache, tid, -1);
  1059. if (tdep->ppc_ev0_upper_regnum >= 0)
  1060. fetch_spe_register (regcache, tid, -1);
  1061. if (tdep->ppc_ppr_regnum != -1)
  1062. fetch_regset (regcache, tid, NT_PPC_PPR,
  1063. PPC_LINUX_SIZEOF_PPRREGSET,
  1064. &ppc32_linux_pprregset);
  1065. if (tdep->ppc_dscr_regnum != -1)
  1066. fetch_regset (regcache, tid, NT_PPC_DSCR,
  1067. PPC_LINUX_SIZEOF_DSCRREGSET,
  1068. &ppc32_linux_dscrregset);
  1069. if (tdep->ppc_tar_regnum != -1)
  1070. fetch_regset (regcache, tid, NT_PPC_TAR,
  1071. PPC_LINUX_SIZEOF_TARREGSET,
  1072. &ppc32_linux_tarregset);
  1073. if (tdep->have_ebb)
  1074. fetch_regset (regcache, tid, NT_PPC_EBB,
  1075. PPC_LINUX_SIZEOF_EBBREGSET,
  1076. &ppc32_linux_ebbregset);
  1077. if (tdep->ppc_mmcr0_regnum != -1)
  1078. fetch_regset (regcache, tid, NT_PPC_PMU,
  1079. PPC_LINUX_SIZEOF_PMUREGSET,
  1080. &ppc32_linux_pmuregset);
  1081. if (tdep->have_htm_spr)
  1082. fetch_regset (regcache, tid, NT_PPC_TM_SPR,
  1083. PPC_LINUX_SIZEOF_TM_SPRREGSET,
  1084. &ppc32_linux_tm_sprregset);
  1085. if (tdep->have_htm_core)
  1086. {
  1087. const struct regset *cgprregset = ppc_linux_cgprregset (gdbarch);
  1088. fetch_regset (regcache, tid, NT_PPC_TM_CGPR,
  1089. (tdep->wordsize == 4?
  1090. PPC32_LINUX_SIZEOF_CGPRREGSET
  1091. : PPC64_LINUX_SIZEOF_CGPRREGSET),
  1092. cgprregset);
  1093. }
  1094. if (tdep->have_htm_fpu)
  1095. fetch_regset (regcache, tid, NT_PPC_TM_CFPR,
  1096. PPC_LINUX_SIZEOF_CFPRREGSET,
  1097. &ppc32_linux_cfprregset);
  1098. if (tdep->have_htm_altivec)
  1099. {
  1100. const struct regset *cvmxregset = ppc_linux_cvmxregset (gdbarch);
  1101. fetch_regset (regcache, tid, NT_PPC_TM_CVMX,
  1102. PPC_LINUX_SIZEOF_CVMXREGSET,
  1103. cvmxregset);
  1104. }
  1105. if (tdep->have_htm_vsx)
  1106. fetch_regset (regcache, tid, NT_PPC_TM_CVSX,
  1107. PPC_LINUX_SIZEOF_CVSXREGSET,
  1108. &ppc32_linux_cvsxregset);
  1109. if (tdep->ppc_cppr_regnum != -1)
  1110. fetch_regset (regcache, tid, NT_PPC_TM_CPPR,
  1111. PPC_LINUX_SIZEOF_CPPRREGSET,
  1112. &ppc32_linux_cpprregset);
  1113. if (tdep->ppc_cdscr_regnum != -1)
  1114. fetch_regset (regcache, tid, NT_PPC_TM_CDSCR,
  1115. PPC_LINUX_SIZEOF_CDSCRREGSET,
  1116. &ppc32_linux_cdscrregset);
  1117. if (tdep->ppc_ctar_regnum != -1)
  1118. fetch_regset (regcache, tid, NT_PPC_TM_CTAR,
  1119. PPC_LINUX_SIZEOF_CTARREGSET,
  1120. &ppc32_linux_ctarregset);
  1121. }
  1122. /* Fetch registers from the child process. Fetch all registers if
  1123. regno == -1, otherwise fetch all general registers or all floating
  1124. point registers depending upon the value of regno. */
  1125. void
  1126. ppc_linux_nat_target::fetch_registers (struct regcache *regcache, int regno)
  1127. {
  1128. pid_t tid = get_ptrace_pid (regcache->ptid ());
  1129. if (regno == -1)
  1130. fetch_ppc_registers (regcache, tid);
  1131. else
  1132. fetch_register (regcache, tid, regno);
  1133. }
  1134. static void
  1135. store_vsx_registers (const struct regcache *regcache, int tid, int regno)
  1136. {
  1137. int ret;
  1138. gdb_vsxregset_t regs;
  1139. const struct regset *vsxregset = ppc_linux_vsxregset ();
  1140. ret = ptrace (PTRACE_GETVSXREGS, tid, 0, &regs);
  1141. if (ret < 0)
  1142. {
  1143. if (errno == EIO)
  1144. {
  1145. have_ptrace_getsetvsxregs = 0;
  1146. return;
  1147. }
  1148. perror_with_name (_("Unable to fetch VSX registers"));
  1149. }
  1150. vsxregset->collect_regset (vsxregset, regcache, regno, &regs,
  1151. PPC_LINUX_SIZEOF_VSXREGSET);
  1152. ret = ptrace (PTRACE_SETVSXREGS, tid, 0, &regs);
  1153. if (ret < 0)
  1154. perror_with_name (_("Unable to store VSX registers"));
  1155. }
  1156. static void
  1157. store_altivec_registers (const struct regcache *regcache, int tid,
  1158. int regno)
  1159. {
  1160. int ret;
  1161. gdb_vrregset_t regs;
  1162. struct gdbarch *gdbarch = regcache->arch ();
  1163. const struct regset *vrregset = ppc_linux_vrregset (gdbarch);
  1164. ret = ptrace (PTRACE_GETVRREGS, tid, 0, &regs);
  1165. if (ret < 0)
  1166. {
  1167. if (errno == EIO)
  1168. {
  1169. have_ptrace_getvrregs = 0;
  1170. return;
  1171. }
  1172. perror_with_name (_("Unable to fetch AltiVec registers"));
  1173. }
  1174. vrregset->collect_regset (vrregset, regcache, regno, &regs,
  1175. PPC_LINUX_SIZEOF_VRREGSET);
  1176. ret = ptrace (PTRACE_SETVRREGS, tid, 0, &regs);
  1177. if (ret < 0)
  1178. perror_with_name (_("Unable to store AltiVec registers"));
  1179. }
  1180. /* Assuming TID refers to an SPE process, set the top halves of TID's
  1181. general-purpose registers and its SPE-specific registers to the
  1182. values in EVRREGSET. If we don't support PTRACE_SETEVRREGS, do
  1183. nothing.
  1184. All the logic to deal with whether or not the PTRACE_GETEVRREGS and
  1185. PTRACE_SETEVRREGS requests are supported is isolated here, and in
  1186. get_spe_registers. */
  1187. static void
  1188. set_spe_registers (int tid, struct gdb_evrregset_t *evrregset)
  1189. {
  1190. if (have_ptrace_getsetevrregs)
  1191. {
  1192. if (ptrace (PTRACE_SETEVRREGS, tid, 0, evrregset) >= 0)
  1193. return;
  1194. else
  1195. {
  1196. /* EIO means that the PTRACE_SETEVRREGS request isn't
  1197. supported; we fail silently, and don't try the call
  1198. again. */
  1199. if (errno == EIO)
  1200. have_ptrace_getsetevrregs = 0;
  1201. else
  1202. /* Anything else needs to be reported. */
  1203. perror_with_name (_("Unable to set SPE registers"));
  1204. }
  1205. }
  1206. }
  1207. /* Write GDB's value for the SPE-specific raw register REGNO to TID.
  1208. If REGNO is -1, write the values of all the SPE-specific
  1209. registers. */
  1210. static void
  1211. store_spe_register (const struct regcache *regcache, int tid, int regno)
  1212. {
  1213. struct gdbarch *gdbarch = regcache->arch ();
  1214. ppc_gdbarch_tdep *tdep = (ppc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  1215. struct gdb_evrregset_t evrregs;
  1216. gdb_assert (sizeof (evrregs.evr[0])
  1217. == register_size (gdbarch, tdep->ppc_ev0_upper_regnum));
  1218. gdb_assert (sizeof (evrregs.acc)
  1219. == register_size (gdbarch, tdep->ppc_acc_regnum));
  1220. gdb_assert (sizeof (evrregs.spefscr)
  1221. == register_size (gdbarch, tdep->ppc_spefscr_regnum));
  1222. if (regno == -1)
  1223. /* Since we're going to write out every register, the code below
  1224. should store to every field of evrregs; if that doesn't happen,
  1225. make it obvious by initializing it with suspicious values. */
  1226. memset (&evrregs, 42, sizeof (evrregs));
  1227. else
  1228. /* We can only read and write the entire EVR register set at a
  1229. time, so to write just a single register, we do a
  1230. read-modify-write maneuver. */
  1231. get_spe_registers (tid, &evrregs);
  1232. if (regno == -1)
  1233. {
  1234. int i;
  1235. for (i = 0; i < ppc_num_gprs; i++)
  1236. regcache->raw_collect (tdep->ppc_ev0_upper_regnum + i,
  1237. &evrregs.evr[i]);
  1238. }
  1239. else if (tdep->ppc_ev0_upper_regnum <= regno
  1240. && regno < tdep->ppc_ev0_upper_regnum + ppc_num_gprs)
  1241. regcache->raw_collect (regno,
  1242. &evrregs.evr[regno - tdep->ppc_ev0_upper_regnum]);
  1243. if (regno == -1
  1244. || regno == tdep->ppc_acc_regnum)
  1245. regcache->raw_collect (tdep->ppc_acc_regnum,
  1246. &evrregs.acc);
  1247. if (regno == -1
  1248. || regno == tdep->ppc_spefscr_regnum)
  1249. regcache->raw_collect (tdep->ppc_spefscr_regnum,
  1250. &evrregs.spefscr);
  1251. /* Write back the modified register set. */
  1252. set_spe_registers (tid, &evrregs);
  1253. }
  1254. static void
  1255. store_register (const struct regcache *regcache, int tid, int regno)
  1256. {
  1257. struct gdbarch *gdbarch = regcache->arch ();
  1258. ppc_gdbarch_tdep *tdep = (ppc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  1259. /* This isn't really an address. But ptrace thinks of it as one. */
  1260. CORE_ADDR regaddr = ppc_register_u_addr (gdbarch, regno);
  1261. int i;
  1262. size_t bytes_to_transfer;
  1263. gdb_byte buf[PPC_MAX_REGISTER_SIZE];
  1264. if (altivec_register_p (gdbarch, regno))
  1265. {
  1266. store_altivec_registers (regcache, tid, regno);
  1267. return;
  1268. }
  1269. else if (vsx_register_p (gdbarch, regno))
  1270. {
  1271. store_vsx_registers (regcache, tid, regno);
  1272. return;
  1273. }
  1274. else if (spe_register_p (gdbarch, regno))
  1275. {
  1276. store_spe_register (regcache, tid, regno);
  1277. return;
  1278. }
  1279. else if (regno == PPC_DSCR_REGNUM)
  1280. {
  1281. gdb_assert (tdep->ppc_dscr_regnum != -1);
  1282. store_regset (regcache, tid, regno, NT_PPC_DSCR,
  1283. PPC_LINUX_SIZEOF_DSCRREGSET,
  1284. &ppc32_linux_dscrregset);
  1285. return;
  1286. }
  1287. else if (regno == PPC_PPR_REGNUM)
  1288. {
  1289. gdb_assert (tdep->ppc_ppr_regnum != -1);
  1290. store_regset (regcache, tid, regno, NT_PPC_PPR,
  1291. PPC_LINUX_SIZEOF_PPRREGSET,
  1292. &ppc32_linux_pprregset);
  1293. return;
  1294. }
  1295. else if (regno == PPC_TAR_REGNUM)
  1296. {
  1297. gdb_assert (tdep->ppc_tar_regnum != -1);
  1298. store_regset (regcache, tid, regno, NT_PPC_TAR,
  1299. PPC_LINUX_SIZEOF_TARREGSET,
  1300. &ppc32_linux_tarregset);
  1301. return;
  1302. }
  1303. else if (PPC_IS_EBB_REGNUM (regno))
  1304. {
  1305. gdb_assert (tdep->have_ebb);
  1306. store_regset (regcache, tid, regno, NT_PPC_EBB,
  1307. PPC_LINUX_SIZEOF_EBBREGSET,
  1308. &ppc32_linux_ebbregset);
  1309. return;
  1310. }
  1311. else if (PPC_IS_PMU_REGNUM (regno))
  1312. {
  1313. gdb_assert (tdep->ppc_mmcr0_regnum != -1);
  1314. store_regset (regcache, tid, regno, NT_PPC_PMU,
  1315. PPC_LINUX_SIZEOF_PMUREGSET,
  1316. &ppc32_linux_pmuregset);
  1317. return;
  1318. }
  1319. else if (PPC_IS_TMSPR_REGNUM (regno))
  1320. {
  1321. gdb_assert (tdep->have_htm_spr);
  1322. store_regset (regcache, tid, regno, NT_PPC_TM_SPR,
  1323. PPC_LINUX_SIZEOF_TM_SPRREGSET,
  1324. &ppc32_linux_tm_sprregset);
  1325. return;
  1326. }
  1327. else if (PPC_IS_CKPTGP_REGNUM (regno))
  1328. {
  1329. gdb_assert (tdep->have_htm_core);
  1330. const struct regset *cgprregset = ppc_linux_cgprregset (gdbarch);
  1331. store_regset (regcache, tid, regno, NT_PPC_TM_CGPR,
  1332. (tdep->wordsize == 4?
  1333. PPC32_LINUX_SIZEOF_CGPRREGSET
  1334. : PPC64_LINUX_SIZEOF_CGPRREGSET),
  1335. cgprregset);
  1336. return;
  1337. }
  1338. else if (PPC_IS_CKPTFP_REGNUM (regno))
  1339. {
  1340. gdb_assert (tdep->have_htm_fpu);
  1341. store_regset (regcache, tid, regno, NT_PPC_TM_CFPR,
  1342. PPC_LINUX_SIZEOF_CFPRREGSET,
  1343. &ppc32_linux_cfprregset);
  1344. return;
  1345. }
  1346. else if (PPC_IS_CKPTVMX_REGNUM (regno))
  1347. {
  1348. gdb_assert (tdep->have_htm_altivec);
  1349. const struct regset *cvmxregset = ppc_linux_cvmxregset (gdbarch);
  1350. store_regset (regcache, tid, regno, NT_PPC_TM_CVMX,
  1351. PPC_LINUX_SIZEOF_CVMXREGSET,
  1352. cvmxregset);
  1353. return;
  1354. }
  1355. else if (PPC_IS_CKPTVSX_REGNUM (regno))
  1356. {
  1357. gdb_assert (tdep->have_htm_vsx);
  1358. store_regset (regcache, tid, regno, NT_PPC_TM_CVSX,
  1359. PPC_LINUX_SIZEOF_CVSXREGSET,
  1360. &ppc32_linux_cvsxregset);
  1361. return;
  1362. }
  1363. else if (regno == PPC_CPPR_REGNUM)
  1364. {
  1365. gdb_assert (tdep->ppc_cppr_regnum != -1);
  1366. store_regset (regcache, tid, regno, NT_PPC_TM_CPPR,
  1367. PPC_LINUX_SIZEOF_CPPRREGSET,
  1368. &ppc32_linux_cpprregset);
  1369. return;
  1370. }
  1371. else if (regno == PPC_CDSCR_REGNUM)
  1372. {
  1373. gdb_assert (tdep->ppc_cdscr_regnum != -1);
  1374. store_regset (regcache, tid, regno, NT_PPC_TM_CDSCR,
  1375. PPC_LINUX_SIZEOF_CDSCRREGSET,
  1376. &ppc32_linux_cdscrregset);
  1377. return;
  1378. }
  1379. else if (regno == PPC_CTAR_REGNUM)
  1380. {
  1381. gdb_assert (tdep->ppc_ctar_regnum != -1);
  1382. store_regset (regcache, tid, regno, NT_PPC_TM_CTAR,
  1383. PPC_LINUX_SIZEOF_CTARREGSET,
  1384. &ppc32_linux_ctarregset);
  1385. return;
  1386. }
  1387. if (regaddr == -1)
  1388. return;
  1389. /* First collect the register. Keep in mind that the regcache's
  1390. idea of the register's size may not be a multiple of sizeof
  1391. (long). */
  1392. memset (buf, 0, sizeof buf);
  1393. bytes_to_transfer = align_up (register_size (gdbarch, regno), sizeof (long));
  1394. if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
  1395. {
  1396. /* Little-endian values always sit at the left end of the buffer. */
  1397. regcache->raw_collect (regno, buf);
  1398. }
  1399. else if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  1400. {
  1401. /* Big-endian values sit at the right end of the buffer. */
  1402. size_t padding = (bytes_to_transfer - register_size (gdbarch, regno));
  1403. regcache->raw_collect (regno, buf + padding);
  1404. }
  1405. for (i = 0; i < bytes_to_transfer; i += sizeof (long))
  1406. {
  1407. long l;
  1408. memcpy (&l, &buf[i], sizeof (l));
  1409. errno = 0;
  1410. ptrace (PTRACE_POKEUSER, tid, (PTRACE_TYPE_ARG3) regaddr, l);
  1411. regaddr += sizeof (long);
  1412. if (errno == EIO
  1413. && (regno == tdep->ppc_fpscr_regnum
  1414. || regno == PPC_ORIG_R3_REGNUM
  1415. || regno == PPC_TRAP_REGNUM))
  1416. {
  1417. /* Some older kernel versions don't allow fpscr, orig_r3
  1418. or trap to be written. */
  1419. continue;
  1420. }
  1421. if (errno != 0)
  1422. {
  1423. char message[128];
  1424. xsnprintf (message, sizeof (message), "writing register %s (#%d)",
  1425. gdbarch_register_name (gdbarch, regno), regno);
  1426. perror_with_name (message);
  1427. }
  1428. }
  1429. }
  1430. /* This function actually issues the request to ptrace, telling
  1431. it to store all general-purpose registers present in the specified
  1432. regset.
  1433. If the ptrace request does not exist, this function returns 0
  1434. and properly sets the have_ptrace_* flag. If the request fails,
  1435. this function calls perror_with_name. Otherwise, if the request
  1436. succeeds, then the regcache is stored and 1 is returned. */
  1437. static int
  1438. store_all_gp_regs (const struct regcache *regcache, int tid, int regno)
  1439. {
  1440. gdb_gregset_t gregset;
  1441. if (ptrace (PTRACE_GETREGS, tid, 0, (void *) &gregset) < 0)
  1442. {
  1443. if (errno == EIO)
  1444. {
  1445. have_ptrace_getsetregs = 0;
  1446. return 0;
  1447. }
  1448. perror_with_name (_("Couldn't get general-purpose registers."));
  1449. }
  1450. fill_gregset (regcache, &gregset, regno);
  1451. if (ptrace (PTRACE_SETREGS, tid, 0, (void *) &gregset) < 0)
  1452. {
  1453. if (errno == EIO)
  1454. {
  1455. have_ptrace_getsetregs = 0;
  1456. return 0;
  1457. }
  1458. perror_with_name (_("Couldn't set general-purpose registers."));
  1459. }
  1460. return 1;
  1461. }
  1462. /* This is a wrapper for the store_all_gp_regs function. It is
  1463. responsible for verifying if this target has the ptrace request
  1464. that can be used to store all general-purpose registers at one
  1465. shot. If it doesn't, then we should store them using the
  1466. old-fashioned way, which is to iterate over the registers and
  1467. store them one by one. */
  1468. static void
  1469. store_gp_regs (const struct regcache *regcache, int tid, int regno)
  1470. {
  1471. struct gdbarch *gdbarch = regcache->arch ();
  1472. ppc_gdbarch_tdep *tdep = (ppc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  1473. int i;
  1474. if (have_ptrace_getsetregs)
  1475. if (store_all_gp_regs (regcache, tid, regno))
  1476. return;
  1477. /* If we hit this point, it doesn't really matter which
  1478. architecture we are using. We just need to store the
  1479. registers in the "old-fashioned way". */
  1480. for (i = 0; i < ppc_num_gprs; i++)
  1481. store_register (regcache, tid, tdep->ppc_gp0_regnum + i);
  1482. }
  1483. /* This function actually issues the request to ptrace, telling
  1484. it to store all floating-point registers present in the specified
  1485. regset.
  1486. If the ptrace request does not exist, this function returns 0
  1487. and properly sets the have_ptrace_* flag. If the request fails,
  1488. this function calls perror_with_name. Otherwise, if the request
  1489. succeeds, then the regcache is stored and 1 is returned. */
  1490. static int
  1491. store_all_fp_regs (const struct regcache *regcache, int tid, int regno)
  1492. {
  1493. gdb_fpregset_t fpregs;
  1494. if (ptrace (PTRACE_GETFPREGS, tid, 0, (void *) &fpregs) < 0)
  1495. {
  1496. if (errno == EIO)
  1497. {
  1498. have_ptrace_getsetfpregs = 0;
  1499. return 0;
  1500. }
  1501. perror_with_name (_("Couldn't get floating-point registers."));
  1502. }
  1503. fill_fpregset (regcache, &fpregs, regno);
  1504. if (ptrace (PTRACE_SETFPREGS, tid, 0, (void *) &fpregs) < 0)
  1505. {
  1506. if (errno == EIO)
  1507. {
  1508. have_ptrace_getsetfpregs = 0;
  1509. return 0;
  1510. }
  1511. perror_with_name (_("Couldn't set floating-point registers."));
  1512. }
  1513. return 1;
  1514. }
  1515. /* This is a wrapper for the store_all_fp_regs function. It is
  1516. responsible for verifying if this target has the ptrace request
  1517. that can be used to store all floating-point registers at one
  1518. shot. If it doesn't, then we should store them using the
  1519. old-fashioned way, which is to iterate over the registers and
  1520. store them one by one. */
  1521. static void
  1522. store_fp_regs (const struct regcache *regcache, int tid, int regno)
  1523. {
  1524. struct gdbarch *gdbarch = regcache->arch ();
  1525. ppc_gdbarch_tdep *tdep = (ppc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  1526. int i;
  1527. if (have_ptrace_getsetfpregs)
  1528. if (store_all_fp_regs (regcache, tid, regno))
  1529. return;
  1530. /* If we hit this point, it doesn't really matter which
  1531. architecture we are using. We just need to store the
  1532. registers in the "old-fashioned way". */
  1533. for (i = 0; i < ppc_num_fprs; i++)
  1534. store_register (regcache, tid, tdep->ppc_fp0_regnum + i);
  1535. }
  1536. static void
  1537. store_ppc_registers (const struct regcache *regcache, int tid)
  1538. {
  1539. struct gdbarch *gdbarch = regcache->arch ();
  1540. ppc_gdbarch_tdep *tdep = (ppc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  1541. store_gp_regs (regcache, tid, -1);
  1542. if (tdep->ppc_fp0_regnum >= 0)
  1543. store_fp_regs (regcache, tid, -1);
  1544. store_register (regcache, tid, gdbarch_pc_regnum (gdbarch));
  1545. if (tdep->ppc_ps_regnum != -1)
  1546. store_register (regcache, tid, tdep->ppc_ps_regnum);
  1547. if (tdep->ppc_cr_regnum != -1)
  1548. store_register (regcache, tid, tdep->ppc_cr_regnum);
  1549. if (tdep->ppc_lr_regnum != -1)
  1550. store_register (regcache, tid, tdep->ppc_lr_regnum);
  1551. if (tdep->ppc_ctr_regnum != -1)
  1552. store_register (regcache, tid, tdep->ppc_ctr_regnum);
  1553. if (tdep->ppc_xer_regnum != -1)
  1554. store_register (regcache, tid, tdep->ppc_xer_regnum);
  1555. if (tdep->ppc_mq_regnum != -1)
  1556. store_register (regcache, tid, tdep->ppc_mq_regnum);
  1557. if (tdep->ppc_fpscr_regnum != -1)
  1558. store_register (regcache, tid, tdep->ppc_fpscr_regnum);
  1559. if (ppc_linux_trap_reg_p (gdbarch))
  1560. {
  1561. store_register (regcache, tid, PPC_ORIG_R3_REGNUM);
  1562. store_register (regcache, tid, PPC_TRAP_REGNUM);
  1563. }
  1564. if (have_ptrace_getvrregs)
  1565. if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1)
  1566. store_altivec_registers (regcache, tid, -1);
  1567. if (have_ptrace_getsetvsxregs)
  1568. if (tdep->ppc_vsr0_upper_regnum != -1)
  1569. store_vsx_registers (regcache, tid, -1);
  1570. if (tdep->ppc_ev0_upper_regnum >= 0)
  1571. store_spe_register (regcache, tid, -1);
  1572. if (tdep->ppc_ppr_regnum != -1)
  1573. store_regset (regcache, tid, -1, NT_PPC_PPR,
  1574. PPC_LINUX_SIZEOF_PPRREGSET,
  1575. &ppc32_linux_pprregset);
  1576. if (tdep->ppc_dscr_regnum != -1)
  1577. store_regset (regcache, tid, -1, NT_PPC_DSCR,
  1578. PPC_LINUX_SIZEOF_DSCRREGSET,
  1579. &ppc32_linux_dscrregset);
  1580. if (tdep->ppc_tar_regnum != -1)
  1581. store_regset (regcache, tid, -1, NT_PPC_TAR,
  1582. PPC_LINUX_SIZEOF_TARREGSET,
  1583. &ppc32_linux_tarregset);
  1584. if (tdep->ppc_mmcr0_regnum != -1)
  1585. store_regset (regcache, tid, -1, NT_PPC_PMU,
  1586. PPC_LINUX_SIZEOF_PMUREGSET,
  1587. &ppc32_linux_pmuregset);
  1588. if (tdep->have_htm_spr)
  1589. store_regset (regcache, tid, -1, NT_PPC_TM_SPR,
  1590. PPC_LINUX_SIZEOF_TM_SPRREGSET,
  1591. &ppc32_linux_tm_sprregset);
  1592. /* Because the EBB and checkpointed HTM registers can be
  1593. unavailable, attempts to store them here would cause this
  1594. function to fail most of the time, so we ignore them. */
  1595. }
  1596. void
  1597. ppc_linux_nat_target::store_registers (struct regcache *regcache, int regno)
  1598. {
  1599. pid_t tid = get_ptrace_pid (regcache->ptid ());
  1600. if (regno >= 0)
  1601. store_register (regcache, tid, regno);
  1602. else
  1603. store_ppc_registers (regcache, tid);
  1604. }
  1605. /* Functions for transferring registers between a gregset_t or fpregset_t
  1606. (see sys/ucontext.h) and gdb's regcache. The word size is that used
  1607. by the ptrace interface, not the current program's ABI. Eg. if a
  1608. powerpc64-linux gdb is being used to debug a powerpc32-linux app, we
  1609. read or write 64-bit gregsets. This is to suit the host libthread_db. */
  1610. void
  1611. supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
  1612. {
  1613. const struct regset *regset = ppc_linux_gregset (sizeof (long));
  1614. ppc_supply_gregset (regset, regcache, -1, gregsetp, sizeof (*gregsetp));
  1615. }
  1616. void
  1617. fill_gregset (const struct regcache *regcache,
  1618. gdb_gregset_t *gregsetp, int regno)
  1619. {
  1620. const struct regset *regset = ppc_linux_gregset (sizeof (long));
  1621. if (regno == -1)
  1622. memset (gregsetp, 0, sizeof (*gregsetp));
  1623. ppc_collect_gregset (regset, regcache, regno, gregsetp, sizeof (*gregsetp));
  1624. }
  1625. void
  1626. supply_fpregset (struct regcache *regcache, const gdb_fpregset_t * fpregsetp)
  1627. {
  1628. const struct regset *regset = ppc_linux_fpregset ();
  1629. ppc_supply_fpregset (regset, regcache, -1,
  1630. fpregsetp, sizeof (*fpregsetp));
  1631. }
  1632. void
  1633. fill_fpregset (const struct regcache *regcache,
  1634. gdb_fpregset_t *fpregsetp, int regno)
  1635. {
  1636. const struct regset *regset = ppc_linux_fpregset ();
  1637. ppc_collect_fpregset (regset, regcache, regno,
  1638. fpregsetp, sizeof (*fpregsetp));
  1639. }
  1640. int
  1641. ppc_linux_nat_target::auxv_parse (gdb_byte **readptr,
  1642. gdb_byte *endptr, CORE_ADDR *typep,
  1643. CORE_ADDR *valp)
  1644. {
  1645. int tid = inferior_ptid.lwp ();
  1646. if (tid == 0)
  1647. tid = inferior_ptid.pid ();
  1648. int sizeof_auxv_field = ppc_linux_target_wordsize (tid);
  1649. enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  1650. gdb_byte *ptr = *readptr;
  1651. if (endptr == ptr)
  1652. return 0;
  1653. if (endptr - ptr < sizeof_auxv_field * 2)
  1654. return -1;
  1655. *typep = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
  1656. ptr += sizeof_auxv_field;
  1657. *valp = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
  1658. ptr += sizeof_auxv_field;
  1659. *readptr = ptr;
  1660. return 1;
  1661. }
  1662. const struct target_desc *
  1663. ppc_linux_nat_target::read_description ()
  1664. {
  1665. int tid = inferior_ptid.pid ();
  1666. if (have_ptrace_getsetevrregs)
  1667. {
  1668. struct gdb_evrregset_t evrregset;
  1669. if (ptrace (PTRACE_GETEVRREGS, tid, 0, &evrregset) >= 0)
  1670. return tdesc_powerpc_e500l;
  1671. /* EIO means that the PTRACE_GETEVRREGS request isn't supported.
  1672. Anything else needs to be reported. */
  1673. else if (errno != EIO)
  1674. perror_with_name (_("Unable to fetch SPE registers"));
  1675. }
  1676. struct ppc_linux_features features = ppc_linux_no_features;
  1677. features.wordsize = ppc_linux_target_wordsize (tid);
  1678. CORE_ADDR hwcap = linux_get_hwcap (current_inferior ()->top_target ());
  1679. CORE_ADDR hwcap2 = linux_get_hwcap2 (current_inferior ()->top_target ());
  1680. if (have_ptrace_getsetvsxregs
  1681. && (hwcap & PPC_FEATURE_HAS_VSX))
  1682. {
  1683. gdb_vsxregset_t vsxregset;
  1684. if (ptrace (PTRACE_GETVSXREGS, tid, 0, &vsxregset) >= 0)
  1685. features.vsx = true;
  1686. /* EIO means that the PTRACE_GETVSXREGS request isn't supported.
  1687. Anything else needs to be reported. */
  1688. else if (errno != EIO)
  1689. perror_with_name (_("Unable to fetch VSX registers"));
  1690. }
  1691. if (have_ptrace_getvrregs
  1692. && (hwcap & PPC_FEATURE_HAS_ALTIVEC))
  1693. {
  1694. gdb_vrregset_t vrregset;
  1695. if (ptrace (PTRACE_GETVRREGS, tid, 0, &vrregset) >= 0)
  1696. features.altivec = true;
  1697. /* EIO means that the PTRACE_GETVRREGS request isn't supported.
  1698. Anything else needs to be reported. */
  1699. else if (errno != EIO)
  1700. perror_with_name (_("Unable to fetch AltiVec registers"));
  1701. }
  1702. features.isa205 = ppc_linux_has_isa205 (hwcap);
  1703. if ((hwcap2 & PPC_FEATURE2_DSCR)
  1704. && check_regset (tid, NT_PPC_PPR, PPC_LINUX_SIZEOF_PPRREGSET)
  1705. && check_regset (tid, NT_PPC_DSCR, PPC_LINUX_SIZEOF_DSCRREGSET))
  1706. {
  1707. features.ppr_dscr = true;
  1708. if ((hwcap2 & PPC_FEATURE2_ARCH_2_07)
  1709. && (hwcap2 & PPC_FEATURE2_TAR)
  1710. && (hwcap2 & PPC_FEATURE2_EBB)
  1711. && check_regset (tid, NT_PPC_TAR, PPC_LINUX_SIZEOF_TARREGSET)
  1712. && check_regset (tid, NT_PPC_EBB, PPC_LINUX_SIZEOF_EBBREGSET)
  1713. && check_regset (tid, NT_PPC_PMU, PPC_LINUX_SIZEOF_PMUREGSET))
  1714. {
  1715. features.isa207 = true;
  1716. if ((hwcap2 & PPC_FEATURE2_HTM)
  1717. && check_regset (tid, NT_PPC_TM_SPR,
  1718. PPC_LINUX_SIZEOF_TM_SPRREGSET))
  1719. features.htm = true;
  1720. }
  1721. }
  1722. return ppc_linux_match_description (features);
  1723. }
  1724. /* Routines for installing hardware watchpoints and breakpoints. When
  1725. GDB requests a hardware watchpoint or breakpoint to be installed, we
  1726. register the request for the pid of inferior_ptid in a map with one
  1727. entry per process. We then issue a stop request to all the threads of
  1728. this process, and mark a per-thread flag indicating that their debug
  1729. registers should be updated. Right before they are next resumed, we
  1730. remove all previously installed debug registers and install all the
  1731. ones GDB requested. We then update a map with one entry per thread
  1732. that keeps track of what debug registers were last installed in each
  1733. thread.
  1734. We use this second map to remove installed registers before installing
  1735. the ones requested by GDB, and to copy the debug register state after
  1736. a thread clones or forks, since depending on the kernel configuration,
  1737. debug registers can be inherited. */
  1738. /* Check if we support and have enough resources to install a hardware
  1739. watchpoint or breakpoint. See the description in target.h. */
  1740. int
  1741. ppc_linux_nat_target::can_use_hw_breakpoint (enum bptype type, int cnt,
  1742. int ot)
  1743. {
  1744. int total_hw_wp, total_hw_bp;
  1745. m_dreg_interface.detect (inferior_ptid);
  1746. if (m_dreg_interface.unavailable_p ())
  1747. return 0;
  1748. if (m_dreg_interface.hwdebug_p ())
  1749. {
  1750. /* When PowerPC HWDEBUG ptrace interface is available, the number of
  1751. available hardware watchpoints and breakpoints is stored at the
  1752. hwdebug_info struct. */
  1753. total_hw_bp = m_dreg_interface.hwdebug_info ().num_instruction_bps;
  1754. total_hw_wp = m_dreg_interface.hwdebug_info ().num_data_bps;
  1755. }
  1756. else
  1757. {
  1758. gdb_assert (m_dreg_interface.debugreg_p ());
  1759. /* With the DEBUGREG ptrace interface, we should consider having 1
  1760. hardware watchpoint and no hardware breakpoints. */
  1761. total_hw_bp = 0;
  1762. total_hw_wp = 1;
  1763. }
  1764. if (type == bp_hardware_watchpoint || type == bp_read_watchpoint
  1765. || type == bp_access_watchpoint || type == bp_watchpoint)
  1766. {
  1767. if (total_hw_wp == 0)
  1768. return 0;
  1769. else if (cnt + ot > total_hw_wp)
  1770. return -1;
  1771. else
  1772. return 1;
  1773. }
  1774. else if (type == bp_hardware_breakpoint)
  1775. {
  1776. if (total_hw_bp == 0)
  1777. return 0;
  1778. else if (cnt > total_hw_bp)
  1779. return -1;
  1780. else
  1781. return 1;
  1782. }
  1783. return 0;
  1784. }
  1785. /* Returns 1 if we can watch LEN bytes at address ADDR, 0 otherwise. */
  1786. int
  1787. ppc_linux_nat_target::region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
  1788. {
  1789. /* Handle sub-8-byte quantities. */
  1790. if (len <= 0)
  1791. return 0;
  1792. m_dreg_interface.detect (inferior_ptid);
  1793. if (m_dreg_interface.unavailable_p ())
  1794. return 0;
  1795. /* The PowerPC HWDEBUG ptrace interface tells if there are alignment
  1796. restrictions for watchpoints in the processors. In that case, we use that
  1797. information to determine the hardcoded watchable region for
  1798. watchpoints. */
  1799. if (m_dreg_interface.hwdebug_p ())
  1800. {
  1801. const struct ppc_debug_info &hwdebug_info = (m_dreg_interface
  1802. .hwdebug_info ());
  1803. int region_size = hwdebug_info.data_bp_alignment;
  1804. int region_align = region_size;
  1805. /* Embedded DAC-based processors, like the PowerPC 440 have ranged
  1806. watchpoints and can watch any access within an arbitrary memory
  1807. region. This is useful to watch arrays and structs, for instance. It
  1808. takes two hardware watchpoints though. */
  1809. if (len > 1
  1810. && hwdebug_info.features & PPC_DEBUG_FEATURE_DATA_BP_RANGE
  1811. && (linux_get_hwcap (current_inferior ()->top_target ())
  1812. & PPC_FEATURE_BOOKE))
  1813. return 2;
  1814. /* Check if the processor provides DAWR interface. */
  1815. if (hwdebug_info.features & PPC_DEBUG_FEATURE_DATA_BP_DAWR)
  1816. {
  1817. /* DAWR interface allows to watch up to 512 byte wide ranges. */
  1818. region_size = 512;
  1819. /* DAWR interface allows to watch up to 512 byte wide ranges which
  1820. can't cross a 512 byte bondary on machines that doesn't have a
  1821. second DAWR (P9 or less). */
  1822. if (!(hwdebug_info.features & PPC_DEBUG_FEATURE_DATA_BP_ARCH_31))
  1823. region_align = 512;
  1824. }
  1825. /* Server processors provide one hardware watchpoint and addr+len should
  1826. fall in the watchable region provided by the ptrace interface. */
  1827. if (region_align
  1828. && (addr + len > (addr & ~(region_align - 1)) + region_size))
  1829. return 0;
  1830. }
  1831. /* addr+len must fall in the 8 byte watchable region for DABR-based
  1832. processors (i.e., server processors). Without the new PowerPC HWDEBUG
  1833. ptrace interface, DAC-based processors (i.e., embedded processors) will
  1834. use addresses aligned to 4-bytes due to the way the read/write flags are
  1835. passed in the old ptrace interface. */
  1836. else
  1837. {
  1838. gdb_assert (m_dreg_interface.debugreg_p ());
  1839. if (((linux_get_hwcap (current_inferior ()->top_target ())
  1840. & PPC_FEATURE_BOOKE)
  1841. && (addr + len) > (addr & ~3) + 4)
  1842. || (addr + len) > (addr & ~7) + 8)
  1843. return 0;
  1844. }
  1845. return 1;
  1846. }
  1847. /* This function compares two ppc_hw_breakpoint structs
  1848. field-by-field. */
  1849. bool
  1850. ppc_linux_nat_target::hwdebug_point_cmp (const struct ppc_hw_breakpoint &a,
  1851. const struct ppc_hw_breakpoint &b)
  1852. {
  1853. return (a.trigger_type == b.trigger_type
  1854. && a.addr_mode == b.addr_mode
  1855. && a.condition_mode == b.condition_mode
  1856. && a.addr == b.addr
  1857. && a.addr2 == b.addr2
  1858. && a.condition_value == b.condition_value);
  1859. }
  1860. /* Return the number of registers needed for a ranged breakpoint. */
  1861. int
  1862. ppc_linux_nat_target::ranged_break_num_registers ()
  1863. {
  1864. m_dreg_interface.detect (inferior_ptid);
  1865. return ((m_dreg_interface.hwdebug_p ()
  1866. && (m_dreg_interface.hwdebug_info ().features
  1867. & PPC_DEBUG_FEATURE_INSN_BP_RANGE))?
  1868. 2 : -1);
  1869. }
  1870. /* Register the hardware breakpoint described by BP_TGT, to be inserted
  1871. when the threads of inferior_ptid are resumed. Returns 0 for success,
  1872. or -1 if the HWDEBUG interface that we need for hardware breakpoints
  1873. is not available. */
  1874. int
  1875. ppc_linux_nat_target::insert_hw_breakpoint (struct gdbarch *gdbarch,
  1876. struct bp_target_info *bp_tgt)
  1877. {
  1878. struct ppc_hw_breakpoint p;
  1879. m_dreg_interface.detect (inferior_ptid);
  1880. if (!m_dreg_interface.hwdebug_p ())
  1881. return -1;
  1882. p.version = PPC_DEBUG_CURRENT_VERSION;
  1883. p.trigger_type = PPC_BREAKPOINT_TRIGGER_EXECUTE;
  1884. p.condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
  1885. p.addr = (uint64_t) (bp_tgt->placed_address = bp_tgt->reqstd_address);
  1886. p.condition_value = 0;
  1887. if (bp_tgt->length)
  1888. {
  1889. p.addr_mode = PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE;
  1890. /* The breakpoint will trigger if the address of the instruction is
  1891. within the defined range, as follows: p.addr <= address < p.addr2. */
  1892. p.addr2 = (uint64_t) bp_tgt->placed_address + bp_tgt->length;
  1893. }
  1894. else
  1895. {
  1896. p.addr_mode = PPC_BREAKPOINT_MODE_EXACT;
  1897. p.addr2 = 0;
  1898. }
  1899. register_hw_breakpoint (inferior_ptid.pid (), p);
  1900. return 0;
  1901. }
  1902. /* Clear a registration for the hardware breakpoint given by type BP_TGT.
  1903. It will be removed from the threads of inferior_ptid when they are
  1904. next resumed. Returns 0 for success, or -1 if the HWDEBUG interface
  1905. that we need for hardware breakpoints is not available. */
  1906. int
  1907. ppc_linux_nat_target::remove_hw_breakpoint (struct gdbarch *gdbarch,
  1908. struct bp_target_info *bp_tgt)
  1909. {
  1910. struct ppc_hw_breakpoint p;
  1911. m_dreg_interface.detect (inferior_ptid);
  1912. if (!m_dreg_interface.hwdebug_p ())
  1913. return -1;
  1914. p.version = PPC_DEBUG_CURRENT_VERSION;
  1915. p.trigger_type = PPC_BREAKPOINT_TRIGGER_EXECUTE;
  1916. p.condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
  1917. p.addr = (uint64_t) bp_tgt->placed_address;
  1918. p.condition_value = 0;
  1919. if (bp_tgt->length)
  1920. {
  1921. p.addr_mode = PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE;
  1922. /* The breakpoint will trigger if the address of the instruction is within
  1923. the defined range, as follows: p.addr <= address < p.addr2. */
  1924. p.addr2 = (uint64_t) bp_tgt->placed_address + bp_tgt->length;
  1925. }
  1926. else
  1927. {
  1928. p.addr_mode = PPC_BREAKPOINT_MODE_EXACT;
  1929. p.addr2 = 0;
  1930. }
  1931. clear_hw_breakpoint (inferior_ptid.pid (), p);
  1932. return 0;
  1933. }
  1934. /* Return the trigger value to set in a ppc_hw_breakpoint object for a
  1935. given hardware watchpoint TYPE. We assume type is not hw_execute. */
  1936. int
  1937. ppc_linux_nat_target::get_trigger_type (enum target_hw_bp_type type)
  1938. {
  1939. int t;
  1940. if (type == hw_read)
  1941. t = PPC_BREAKPOINT_TRIGGER_READ;
  1942. else if (type == hw_write)
  1943. t = PPC_BREAKPOINT_TRIGGER_WRITE;
  1944. else
  1945. t = PPC_BREAKPOINT_TRIGGER_READ | PPC_BREAKPOINT_TRIGGER_WRITE;
  1946. return t;
  1947. }
  1948. /* Register a new masked watchpoint at ADDR using the mask MASK, to be
  1949. inserted when the threads of inferior_ptid are resumed. RW may be
  1950. hw_read for a read watchpoint, hw_write for a write watchpoint or
  1951. hw_access for an access watchpoint. */
  1952. int
  1953. ppc_linux_nat_target::insert_mask_watchpoint (CORE_ADDR addr, CORE_ADDR mask,
  1954. target_hw_bp_type rw)
  1955. {
  1956. struct ppc_hw_breakpoint p;
  1957. gdb_assert (m_dreg_interface.hwdebug_p ());
  1958. p.version = PPC_DEBUG_CURRENT_VERSION;
  1959. p.trigger_type = get_trigger_type (rw);
  1960. p.addr_mode = PPC_BREAKPOINT_MODE_MASK;
  1961. p.condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
  1962. p.addr = addr;
  1963. p.addr2 = mask;
  1964. p.condition_value = 0;
  1965. register_hw_breakpoint (inferior_ptid.pid (), p);
  1966. return 0;
  1967. }
  1968. /* Clear a registration for a masked watchpoint at ADDR with the mask
  1969. MASK. It will be removed from the threads of inferior_ptid when they
  1970. are next resumed. RW may be hw_read for a read watchpoint, hw_write
  1971. for a write watchpoint or hw_access for an access watchpoint. */
  1972. int
  1973. ppc_linux_nat_target::remove_mask_watchpoint (CORE_ADDR addr, CORE_ADDR mask,
  1974. target_hw_bp_type rw)
  1975. {
  1976. struct ppc_hw_breakpoint p;
  1977. gdb_assert (m_dreg_interface.hwdebug_p ());
  1978. p.version = PPC_DEBUG_CURRENT_VERSION;
  1979. p.trigger_type = get_trigger_type (rw);
  1980. p.addr_mode = PPC_BREAKPOINT_MODE_MASK;
  1981. p.condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
  1982. p.addr = addr;
  1983. p.addr2 = mask;
  1984. p.condition_value = 0;
  1985. clear_hw_breakpoint (inferior_ptid.pid (), p);
  1986. return 0;
  1987. }
  1988. /* Check whether we have at least one free DVC register for the threads
  1989. of the pid of inferior_ptid. */
  1990. bool
  1991. ppc_linux_nat_target::can_use_watchpoint_cond_accel (void)
  1992. {
  1993. m_dreg_interface.detect (inferior_ptid);
  1994. if (!m_dreg_interface.hwdebug_p ())
  1995. return false;
  1996. int cnt = m_dreg_interface.hwdebug_info ().num_condition_regs;
  1997. if (cnt == 0)
  1998. return false;
  1999. auto process_it = m_process_info.find (inferior_ptid.pid ());
  2000. /* No breakpoints or watchpoints have been requested for this process,
  2001. we have at least one free DVC register. */
  2002. if (process_it == m_process_info.end ())
  2003. return true;
  2004. for (const ppc_hw_breakpoint &bp : process_it->second.requested_hw_bps)
  2005. if (bp.condition_mode != PPC_BREAKPOINT_CONDITION_NONE)
  2006. cnt--;
  2007. if (cnt <= 0)
  2008. return false;
  2009. return true;
  2010. }
  2011. /* Calculate the enable bits and the contents of the Data Value Compare
  2012. debug register present in BookE processors.
  2013. ADDR is the address to be watched, LEN is the length of watched data
  2014. and DATA_VALUE is the value which will trigger the watchpoint.
  2015. On exit, CONDITION_MODE will hold the enable bits for the DVC, and
  2016. CONDITION_VALUE will hold the value which should be put in the
  2017. DVC register. */
  2018. void
  2019. ppc_linux_nat_target::calculate_dvc (CORE_ADDR addr, int len,
  2020. CORE_ADDR data_value,
  2021. uint32_t *condition_mode,
  2022. uint64_t *condition_value)
  2023. {
  2024. const struct ppc_debug_info &hwdebug_info = (m_dreg_interface.
  2025. hwdebug_info ());
  2026. int i, num_byte_enable, align_offset, num_bytes_off_dvc,
  2027. rightmost_enabled_byte;
  2028. CORE_ADDR addr_end_data, addr_end_dvc;
  2029. /* The DVC register compares bytes within fixed-length windows which
  2030. are word-aligned, with length equal to that of the DVC register.
  2031. We need to calculate where our watch region is relative to that
  2032. window and enable comparison of the bytes which fall within it. */
  2033. align_offset = addr % hwdebug_info.sizeof_condition;
  2034. addr_end_data = addr + len;
  2035. addr_end_dvc = (addr - align_offset
  2036. + hwdebug_info.sizeof_condition);
  2037. num_bytes_off_dvc = (addr_end_data > addr_end_dvc)?
  2038. addr_end_data - addr_end_dvc : 0;
  2039. num_byte_enable = len - num_bytes_off_dvc;
  2040. /* Here, bytes are numbered from right to left. */
  2041. rightmost_enabled_byte = (addr_end_data < addr_end_dvc)?
  2042. addr_end_dvc - addr_end_data : 0;
  2043. *condition_mode = PPC_BREAKPOINT_CONDITION_AND;
  2044. for (i = 0; i < num_byte_enable; i++)
  2045. *condition_mode
  2046. |= PPC_BREAKPOINT_CONDITION_BE (i + rightmost_enabled_byte);
  2047. /* Now we need to match the position within the DVC of the comparison
  2048. value with where the watch region is relative to the window
  2049. (i.e., the ALIGN_OFFSET). */
  2050. *condition_value = ((uint64_t) data_value >> num_bytes_off_dvc * 8
  2051. << rightmost_enabled_byte * 8);
  2052. }
  2053. /* Return the number of memory locations that need to be accessed to
  2054. evaluate the expression which generated the given value chain.
  2055. Returns -1 if there's any register access involved, or if there are
  2056. other kinds of values which are not acceptable in a condition
  2057. expression (e.g., lval_computed or lval_internalvar). */
  2058. int
  2059. ppc_linux_nat_target::num_memory_accesses (const std::vector<value_ref_ptr>
  2060. &chain)
  2061. {
  2062. int found_memory_cnt = 0;
  2063. /* The idea here is that evaluating an expression generates a series
  2064. of values, one holding the value of every subexpression. (The
  2065. expression a*b+c has five subexpressions: a, b, a*b, c, and
  2066. a*b+c.) GDB's values hold almost enough information to establish
  2067. the criteria given above --- they identify memory lvalues,
  2068. register lvalues, computed values, etcetera. So we can evaluate
  2069. the expression, and then scan the chain of values that leaves
  2070. behind to determine the memory locations involved in the evaluation
  2071. of an expression.
  2072. However, I don't think that the values returned by inferior
  2073. function calls are special in any way. So this function may not
  2074. notice that an expression contains an inferior function call.
  2075. FIXME. */
  2076. for (const value_ref_ptr &iter : chain)
  2077. {
  2078. struct value *v = iter.get ();
  2079. /* Constants and values from the history are fine. */
  2080. if (VALUE_LVAL (v) == not_lval || deprecated_value_modifiable (v) == 0)
  2081. continue;
  2082. else if (VALUE_LVAL (v) == lval_memory)
  2083. {
  2084. /* A lazy memory lvalue is one that GDB never needed to fetch;
  2085. we either just used its address (e.g., `a' in `a.b') or
  2086. we never needed it at all (e.g., `a' in `a,b'). */
  2087. if (!value_lazy (v))
  2088. found_memory_cnt++;
  2089. }
  2090. /* Other kinds of values are not fine. */
  2091. else
  2092. return -1;
  2093. }
  2094. return found_memory_cnt;
  2095. }
  2096. /* Verifies whether the expression COND can be implemented using the
  2097. DVC (Data Value Compare) register in BookE processors. The expression
  2098. must test the watch value for equality with a constant expression.
  2099. If the function returns 1, DATA_VALUE will contain the constant against
  2100. which the watch value should be compared and LEN will contain the size
  2101. of the constant. */
  2102. int
  2103. ppc_linux_nat_target::check_condition (CORE_ADDR watch_addr,
  2104. struct expression *cond,
  2105. CORE_ADDR *data_value, int *len)
  2106. {
  2107. int num_accesses_left, num_accesses_right;
  2108. struct value *left_val, *right_val;
  2109. std::vector<value_ref_ptr> left_chain, right_chain;
  2110. expr::equal_operation *eqop
  2111. = dynamic_cast<expr::equal_operation *> (cond->op.get ());
  2112. if (eqop == nullptr)
  2113. return 0;
  2114. expr::operation *lhs = eqop->get_lhs ();
  2115. expr::operation *rhs = eqop->get_rhs ();
  2116. fetch_subexp_value (cond, lhs, &left_val, NULL, &left_chain, false);
  2117. num_accesses_left = num_memory_accesses (left_chain);
  2118. if (left_val == NULL || num_accesses_left < 0)
  2119. return 0;
  2120. fetch_subexp_value (cond, rhs, &right_val, NULL, &right_chain, false);
  2121. num_accesses_right = num_memory_accesses (right_chain);
  2122. if (right_val == NULL || num_accesses_right < 0)
  2123. return 0;
  2124. if (num_accesses_left == 1 && num_accesses_right == 0
  2125. && VALUE_LVAL (left_val) == lval_memory
  2126. && value_address (left_val) == watch_addr)
  2127. {
  2128. *data_value = value_as_long (right_val);
  2129. /* DATA_VALUE is the constant in RIGHT_VAL, but actually has
  2130. the same type as the memory region referenced by LEFT_VAL. */
  2131. *len = TYPE_LENGTH (check_typedef (value_type (left_val)));
  2132. }
  2133. else if (num_accesses_left == 0 && num_accesses_right == 1
  2134. && VALUE_LVAL (right_val) == lval_memory
  2135. && value_address (right_val) == watch_addr)
  2136. {
  2137. *data_value = value_as_long (left_val);
  2138. /* DATA_VALUE is the constant in LEFT_VAL, but actually has
  2139. the same type as the memory region referenced by RIGHT_VAL. */
  2140. *len = TYPE_LENGTH (check_typedef (value_type (right_val)));
  2141. }
  2142. else
  2143. return 0;
  2144. return 1;
  2145. }
  2146. /* Return true if the target is capable of using hardware to evaluate the
  2147. condition expression, thus only triggering the watchpoint when it is
  2148. true. */
  2149. bool
  2150. ppc_linux_nat_target::can_accel_watchpoint_condition (CORE_ADDR addr,
  2151. int len, int rw,
  2152. struct expression *cond)
  2153. {
  2154. CORE_ADDR data_value;
  2155. m_dreg_interface.detect (inferior_ptid);
  2156. return (m_dreg_interface.hwdebug_p ()
  2157. && (m_dreg_interface.hwdebug_info ().num_condition_regs > 0)
  2158. && check_condition (addr, cond, &data_value, &len));
  2159. }
  2160. /* Set up P with the parameters necessary to request a watchpoint covering
  2161. LEN bytes starting at ADDR and if possible with condition expression COND
  2162. evaluated by hardware. INSERT tells if we are creating a request for
  2163. inserting or removing the watchpoint. */
  2164. void
  2165. ppc_linux_nat_target::create_watchpoint_request (struct ppc_hw_breakpoint *p,
  2166. CORE_ADDR addr, int len,
  2167. enum target_hw_bp_type type,
  2168. struct expression *cond,
  2169. int insert)
  2170. {
  2171. const struct ppc_debug_info &hwdebug_info = (m_dreg_interface
  2172. .hwdebug_info ());
  2173. if (len == 1
  2174. || !(hwdebug_info.features & PPC_DEBUG_FEATURE_DATA_BP_RANGE))
  2175. {
  2176. int use_condition;
  2177. CORE_ADDR data_value;
  2178. use_condition = (insert? can_use_watchpoint_cond_accel ()
  2179. : hwdebug_info.num_condition_regs > 0);
  2180. if (cond && use_condition && check_condition (addr, cond,
  2181. &data_value, &len))
  2182. calculate_dvc (addr, len, data_value, &p->condition_mode,
  2183. &p->condition_value);
  2184. else
  2185. {
  2186. p->condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
  2187. p->condition_value = 0;
  2188. }
  2189. p->addr_mode = PPC_BREAKPOINT_MODE_EXACT;
  2190. p->addr2 = 0;
  2191. }
  2192. else
  2193. {
  2194. p->addr_mode = PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE;
  2195. p->condition_mode = PPC_BREAKPOINT_CONDITION_NONE;
  2196. p->condition_value = 0;
  2197. /* The watchpoint will trigger if the address of the memory access is
  2198. within the defined range, as follows: p->addr <= address < p->addr2.
  2199. Note that the above sentence just documents how ptrace interprets
  2200. its arguments; the watchpoint is set to watch the range defined by
  2201. the user _inclusively_, as specified by the user interface. */
  2202. p->addr2 = (uint64_t) addr + len;
  2203. }
  2204. p->version = PPC_DEBUG_CURRENT_VERSION;
  2205. p->trigger_type = get_trigger_type (type);
  2206. p->addr = (uint64_t) addr;
  2207. }
  2208. /* Register a watchpoint, to be inserted when the threads of the group of
  2209. inferior_ptid are next resumed. Returns 0 on success, and -1 if there
  2210. is no ptrace interface available to install the watchpoint. */
  2211. int
  2212. ppc_linux_nat_target::insert_watchpoint (CORE_ADDR addr, int len,
  2213. enum target_hw_bp_type type,
  2214. struct expression *cond)
  2215. {
  2216. m_dreg_interface.detect (inferior_ptid);
  2217. if (m_dreg_interface.unavailable_p ())
  2218. return -1;
  2219. if (m_dreg_interface.hwdebug_p ())
  2220. {
  2221. struct ppc_hw_breakpoint p;
  2222. create_watchpoint_request (&p, addr, len, type, cond, 1);
  2223. register_hw_breakpoint (inferior_ptid.pid (), p);
  2224. }
  2225. else
  2226. {
  2227. gdb_assert (m_dreg_interface.debugreg_p ());
  2228. long wp_value;
  2229. long read_mode, write_mode;
  2230. if (linux_get_hwcap (current_inferior ()->top_target ())
  2231. & PPC_FEATURE_BOOKE)
  2232. {
  2233. /* PowerPC 440 requires only the read/write flags to be passed
  2234. to the kernel. */
  2235. read_mode = 1;
  2236. write_mode = 2;
  2237. }
  2238. else
  2239. {
  2240. /* PowerPC 970 and other DABR-based processors are required to pass
  2241. the Breakpoint Translation bit together with the flags. */
  2242. read_mode = 5;
  2243. write_mode = 6;
  2244. }
  2245. wp_value = addr & ~(read_mode | write_mode);
  2246. switch (type)
  2247. {
  2248. case hw_read:
  2249. /* Set read and translate bits. */
  2250. wp_value |= read_mode;
  2251. break;
  2252. case hw_write:
  2253. /* Set write and translate bits. */
  2254. wp_value |= write_mode;
  2255. break;
  2256. case hw_access:
  2257. /* Set read, write and translate bits. */
  2258. wp_value |= read_mode | write_mode;
  2259. break;
  2260. }
  2261. register_wp (inferior_ptid.pid (), wp_value);
  2262. }
  2263. return 0;
  2264. }
  2265. /* Clear a registration for a hardware watchpoint. It will be removed
  2266. from the threads of the group of inferior_ptid when they are next
  2267. resumed. */
  2268. int
  2269. ppc_linux_nat_target::remove_watchpoint (CORE_ADDR addr, int len,
  2270. enum target_hw_bp_type type,
  2271. struct expression *cond)
  2272. {
  2273. gdb_assert (!m_dreg_interface.unavailable_p ());
  2274. if (m_dreg_interface.hwdebug_p ())
  2275. {
  2276. struct ppc_hw_breakpoint p;
  2277. create_watchpoint_request (&p, addr, len, type, cond, 0);
  2278. clear_hw_breakpoint (inferior_ptid.pid (), p);
  2279. }
  2280. else
  2281. {
  2282. gdb_assert (m_dreg_interface.debugreg_p ());
  2283. clear_wp (inferior_ptid.pid ());
  2284. }
  2285. return 0;
  2286. }
  2287. /* Clean up the per-process info associated with PID. When using the
  2288. HWDEBUG interface, we also erase the per-thread state of installed
  2289. debug registers for all the threads that belong to the group of PID.
  2290. Usually the thread state is cleaned up by low_delete_thread. We also
  2291. do it here because low_new_thread is not called for the initial LWP,
  2292. so low_delete_thread won't be able to clean up this state. */
  2293. void
  2294. ppc_linux_nat_target::low_forget_process (pid_t pid)
  2295. {
  2296. if ((!m_dreg_interface.detected_p ())
  2297. || (m_dreg_interface.unavailable_p ()))
  2298. return;
  2299. ptid_t pid_ptid (pid, 0, 0);
  2300. m_process_info.erase (pid);
  2301. if (m_dreg_interface.hwdebug_p ())
  2302. {
  2303. for (auto it = m_installed_hw_bps.begin ();
  2304. it != m_installed_hw_bps.end ();)
  2305. {
  2306. if (it->first.matches (pid_ptid))
  2307. it = m_installed_hw_bps.erase (it);
  2308. else
  2309. it++;
  2310. }
  2311. }
  2312. }
  2313. /* Copy the per-process state associated with the pid of PARENT to the
  2314. sate of CHILD_PID. GDB expects that a forked process will have the
  2315. same hardware breakpoints and watchpoints as the parent.
  2316. If we're using the HWDEBUG interface, also copy the thread debug
  2317. register state for the ptid of PARENT to the state for CHILD_PID.
  2318. Like for clone events, we assume the kernel will copy the debug
  2319. registers from the parent thread to the child. The
  2320. low_prepare_to_resume function is made to work even if it doesn't.
  2321. We copy the thread state here and not in low_new_thread since we don't
  2322. have the pid of the parent in low_new_thread. Even if we did,
  2323. low_new_thread might not be called immediately after the fork event is
  2324. detected. For instance, with the checkpointing system (see
  2325. linux-fork.c), the thread won't be added until GDB decides to switch
  2326. to a new checkpointed process. At that point, the debug register
  2327. state of the parent thread is unlikely to correspond to the state it
  2328. had at the point when it forked. */
  2329. void
  2330. ppc_linux_nat_target::low_new_fork (struct lwp_info *parent,
  2331. pid_t child_pid)
  2332. {
  2333. if ((!m_dreg_interface.detected_p ())
  2334. || (m_dreg_interface.unavailable_p ()))
  2335. return;
  2336. auto process_it = m_process_info.find (parent->ptid.pid ());
  2337. if (process_it != m_process_info.end ())
  2338. m_process_info[child_pid] = m_process_info[parent->ptid.pid ()];
  2339. if (m_dreg_interface.hwdebug_p ())
  2340. {
  2341. ptid_t child_ptid (child_pid, child_pid, 0);
  2342. copy_thread_dreg_state (parent->ptid, child_ptid);
  2343. }
  2344. }
  2345. /* Copy the thread debug register state from the PARENT thread to the the
  2346. state for CHILD_LWP, if we're using the HWDEBUG interface. We assume
  2347. the kernel copies the debug registers from one thread to another after
  2348. a clone event. The low_prepare_to_resume function is made to work
  2349. even if it doesn't. */
  2350. void
  2351. ppc_linux_nat_target::low_new_clone (struct lwp_info *parent,
  2352. pid_t child_lwp)
  2353. {
  2354. if ((!m_dreg_interface.detected_p ())
  2355. || (m_dreg_interface.unavailable_p ()))
  2356. return;
  2357. if (m_dreg_interface.hwdebug_p ())
  2358. {
  2359. ptid_t child_ptid (parent->ptid.pid (), child_lwp, 0);
  2360. copy_thread_dreg_state (parent->ptid, child_ptid);
  2361. }
  2362. }
  2363. /* Initialize the arch-specific thread state for LP so that it contains
  2364. the ptid for lp, so that we can use it in low_delete_thread. Mark the
  2365. new thread LP as stale so that we update its debug registers before
  2366. resuming it. This is not called for the initial thread. */
  2367. void
  2368. ppc_linux_nat_target::low_new_thread (struct lwp_info *lp)
  2369. {
  2370. init_arch_lwp_info (lp);
  2371. mark_thread_stale (lp);
  2372. }
  2373. /* Delete the per-thread debug register stale flag. */
  2374. void
  2375. ppc_linux_nat_target::low_delete_thread (struct arch_lwp_info
  2376. *lp_arch_info)
  2377. {
  2378. if (lp_arch_info != NULL)
  2379. {
  2380. if (m_dreg_interface.detected_p ()
  2381. && m_dreg_interface.hwdebug_p ())
  2382. m_installed_hw_bps.erase (lp_arch_info->lwp_ptid);
  2383. xfree (lp_arch_info);
  2384. }
  2385. }
  2386. /* Install or delete debug registers in thread LP so that it matches what
  2387. GDB requested before it is resumed. */
  2388. void
  2389. ppc_linux_nat_target::low_prepare_to_resume (struct lwp_info *lp)
  2390. {
  2391. if ((!m_dreg_interface.detected_p ())
  2392. || (m_dreg_interface.unavailable_p ()))
  2393. return;
  2394. /* We have to re-install or clear the debug registers if we set the
  2395. stale flag.
  2396. In addition, some kernels configurations can disable a hardware
  2397. watchpoint after it is hit. Usually, GDB will remove and re-install
  2398. a hardware watchpoint when the thread stops if "breakpoint
  2399. always-inserted" is off, or to single-step a watchpoint. But so
  2400. that we don't rely on this behavior, if we stop due to a hardware
  2401. breakpoint or watchpoint, we also refresh our debug registers. */
  2402. arch_lwp_info *lp_arch_info = get_arch_lwp_info (lp);
  2403. bool stale_dregs = (lp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT
  2404. || lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT
  2405. || lp_arch_info->debug_regs_stale);
  2406. if (!stale_dregs)
  2407. return;
  2408. gdb_assert (lp->ptid.lwp_p ());
  2409. auto process_it = m_process_info.find (lp->ptid.pid ());
  2410. if (m_dreg_interface.hwdebug_p ())
  2411. {
  2412. /* First, delete any hardware watchpoint or breakpoint installed in
  2413. the inferior and update the thread state. */
  2414. auto installed_it = m_installed_hw_bps.find (lp->ptid);
  2415. if (installed_it != m_installed_hw_bps.end ())
  2416. {
  2417. auto &bp_list = installed_it->second;
  2418. for (auto bp_it = bp_list.begin (); bp_it != bp_list.end ();)
  2419. {
  2420. /* We ignore ENOENT to account for various possible kernel
  2421. behaviors, e.g. the kernel might or might not copy debug
  2422. registers across forks and clones, and we always copy
  2423. the debug register state when fork and clone events are
  2424. detected. */
  2425. if (ptrace (PPC_PTRACE_DELHWDEBUG, lp->ptid.lwp (), 0,
  2426. bp_it->first) < 0)
  2427. if (errno != ENOENT)
  2428. perror_with_name (_("Error deleting hardware "
  2429. "breakpoint or watchpoint"));
  2430. /* We erase the entries one at a time after successfuly
  2431. removing the corresponding slot form the thread so that
  2432. if we throw an exception above in a future iteration the
  2433. map remains consistent. */
  2434. bp_it = bp_list.erase (bp_it);
  2435. }
  2436. gdb_assert (bp_list.empty ());
  2437. }
  2438. /* Now we install all the requested hardware breakpoints and
  2439. watchpoints and update the thread state. */
  2440. if (process_it != m_process_info.end ())
  2441. {
  2442. auto &bp_list = m_installed_hw_bps[lp->ptid];
  2443. for (ppc_hw_breakpoint bp
  2444. : process_it->second.requested_hw_bps)
  2445. {
  2446. long slot = ptrace (PPC_PTRACE_SETHWDEBUG, lp->ptid.lwp (),
  2447. 0, &bp);
  2448. if (slot < 0)
  2449. perror_with_name (_("Error setting hardware "
  2450. "breakpoint or watchpoint"));
  2451. /* Keep track of which slots we installed in this
  2452. thread. */
  2453. bp_list.emplace (bp_list.begin (), slot, bp);
  2454. }
  2455. }
  2456. }
  2457. else
  2458. {
  2459. gdb_assert (m_dreg_interface.debugreg_p ());
  2460. /* Passing 0 to PTRACE_SET_DEBUGREG will clear the watchpoint. We
  2461. always clear the watchpoint instead of just overwriting it, in
  2462. case there is a request for a new watchpoint, because on some
  2463. older kernel versions and configurations simply overwriting the
  2464. watchpoint after it was hit would not re-enable it. */
  2465. if (ptrace (PTRACE_SET_DEBUGREG, lp->ptid.lwp (), 0, 0) < 0)
  2466. perror_with_name (_("Error clearing hardware watchpoint"));
  2467. /* GDB requested a watchpoint to be installed. */
  2468. if (process_it != m_process_info.end ()
  2469. && process_it->second.requested_wp_val.has_value ())
  2470. {
  2471. long wp = *(process_it->second.requested_wp_val);
  2472. if (ptrace (PTRACE_SET_DEBUGREG, lp->ptid.lwp (), 0, wp) < 0)
  2473. perror_with_name (_("Error setting hardware watchpoint"));
  2474. }
  2475. }
  2476. lp_arch_info->debug_regs_stale = false;
  2477. }
  2478. /* Return true if INFERIOR_PTID is known to have been stopped by a
  2479. hardware watchpoint, false otherwise. If true is returned, write the
  2480. address that the kernel reported as causing the SIGTRAP in ADDR_P. */
  2481. bool
  2482. ppc_linux_nat_target::low_stopped_data_address (CORE_ADDR *addr_p)
  2483. {
  2484. siginfo_t siginfo;
  2485. if (!linux_nat_get_siginfo (inferior_ptid, &siginfo))
  2486. return false;
  2487. if (siginfo.si_signo != SIGTRAP
  2488. || (siginfo.si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
  2489. return false;
  2490. gdb_assert (!m_dreg_interface.unavailable_p ());
  2491. /* Check if this signal corresponds to a hardware breakpoint. We only
  2492. need to check this if we're using the HWDEBUG interface, since the
  2493. DEBUGREG interface only allows setting one hardware watchpoint. */
  2494. if (m_dreg_interface.hwdebug_p ())
  2495. {
  2496. /* The index (or slot) of the *point is passed in the si_errno
  2497. field. Currently, this is only the case if the kernel was
  2498. configured with CONFIG_PPC_ADV_DEBUG_REGS. If not, we assume
  2499. the kernel will set si_errno to a value that doesn't correspond
  2500. to any real slot. */
  2501. int slot = siginfo.si_errno;
  2502. auto installed_it = m_installed_hw_bps.find (inferior_ptid);
  2503. /* We must have installed slots for the thread if it got a
  2504. TRAP_HWBKPT signal. */
  2505. gdb_assert (installed_it != m_installed_hw_bps.end ());
  2506. for (const auto & slot_bp_pair : installed_it->second)
  2507. if (slot_bp_pair.first == slot
  2508. && (slot_bp_pair.second.trigger_type
  2509. == PPC_BREAKPOINT_TRIGGER_EXECUTE))
  2510. return false;
  2511. }
  2512. *addr_p = (CORE_ADDR) (uintptr_t) siginfo.si_addr;
  2513. return true;
  2514. }
  2515. /* Return true if INFERIOR_PTID is known to have been stopped by a
  2516. hardware watchpoint, false otherwise. */
  2517. bool
  2518. ppc_linux_nat_target::low_stopped_by_watchpoint ()
  2519. {
  2520. CORE_ADDR addr;
  2521. return low_stopped_data_address (&addr);
  2522. }
  2523. bool
  2524. ppc_linux_nat_target::watchpoint_addr_within_range (CORE_ADDR addr,
  2525. CORE_ADDR start,
  2526. int length)
  2527. {
  2528. gdb_assert (!m_dreg_interface.unavailable_p ());
  2529. int mask;
  2530. if (m_dreg_interface.hwdebug_p ()
  2531. && (linux_get_hwcap (current_inferior ()->top_target ())
  2532. & PPC_FEATURE_BOOKE))
  2533. return start <= addr && start + length >= addr;
  2534. else if (linux_get_hwcap (current_inferior ()->top_target ())
  2535. & PPC_FEATURE_BOOKE)
  2536. mask = 3;
  2537. else
  2538. mask = 7;
  2539. addr &= ~mask;
  2540. /* Check whether [start, start+length-1] intersects [addr, addr+mask]. */
  2541. return start <= addr + mask && start + length - 1 >= addr;
  2542. }
  2543. /* Return the number of registers needed for a masked hardware watchpoint. */
  2544. int
  2545. ppc_linux_nat_target::masked_watch_num_registers (CORE_ADDR addr,
  2546. CORE_ADDR mask)
  2547. {
  2548. m_dreg_interface.detect (inferior_ptid);
  2549. if (!m_dreg_interface.hwdebug_p ()
  2550. || (m_dreg_interface.hwdebug_info ().features
  2551. & PPC_DEBUG_FEATURE_DATA_BP_MASK) == 0)
  2552. return -1;
  2553. else if ((mask & 0xC0000000) != 0xC0000000)
  2554. {
  2555. warning (_("The given mask covers kernel address space "
  2556. "and cannot be used.\n"));
  2557. return -2;
  2558. }
  2559. else
  2560. return 2;
  2561. }
  2562. /* Copy the per-thread debug register state, if any, from thread
  2563. PARENT_PTID to thread CHILD_PTID, if the debug register being used is
  2564. HWDEBUG. */
  2565. void
  2566. ppc_linux_nat_target::copy_thread_dreg_state (const ptid_t &parent_ptid,
  2567. const ptid_t &child_ptid)
  2568. {
  2569. gdb_assert (m_dreg_interface.hwdebug_p ());
  2570. auto installed_it = m_installed_hw_bps.find (parent_ptid);
  2571. if (installed_it != m_installed_hw_bps.end ())
  2572. m_installed_hw_bps[child_ptid] = m_installed_hw_bps[parent_ptid];
  2573. }
  2574. /* Mark the debug register stale flag for the new thread, if we have
  2575. already detected which debug register interface we use. */
  2576. void
  2577. ppc_linux_nat_target::mark_thread_stale (struct lwp_info *lp)
  2578. {
  2579. if ((!m_dreg_interface.detected_p ())
  2580. || (m_dreg_interface.unavailable_p ()))
  2581. return;
  2582. arch_lwp_info *lp_arch_info = get_arch_lwp_info (lp);
  2583. lp_arch_info->debug_regs_stale = true;
  2584. }
  2585. /* Mark all the threads of the group of PID as stale with respect to
  2586. debug registers and issue a stop request to each such thread that
  2587. isn't already stopped. */
  2588. void
  2589. ppc_linux_nat_target::mark_debug_registers_changed (pid_t pid)
  2590. {
  2591. /* We do this in two passes to make sure all threads are marked even if
  2592. we get an exception when stopping one of them. */
  2593. iterate_over_lwps (ptid_t (pid),
  2594. [this] (struct lwp_info *lp) -> int {
  2595. this->mark_thread_stale (lp);
  2596. return 0;
  2597. });
  2598. iterate_over_lwps (ptid_t (pid),
  2599. [] (struct lwp_info *lp) -> int {
  2600. if (!lwp_is_stopped (lp))
  2601. linux_stop_lwp (lp);
  2602. return 0;
  2603. });
  2604. }
  2605. /* Register a hardware breakpoint or watchpoint BP for the pid PID, then
  2606. mark the stale flag for all threads of the group of PID, and issue a
  2607. stop request for them. The breakpoint or watchpoint will be installed
  2608. the next time each thread is resumed. Should only be used if the
  2609. debug register interface is HWDEBUG. */
  2610. void
  2611. ppc_linux_nat_target::register_hw_breakpoint (pid_t pid,
  2612. const struct
  2613. ppc_hw_breakpoint &bp)
  2614. {
  2615. gdb_assert (m_dreg_interface.hwdebug_p ());
  2616. m_process_info[pid].requested_hw_bps.push_back (bp);
  2617. mark_debug_registers_changed (pid);
  2618. }
  2619. /* Clear a registration for a hardware breakpoint or watchpoint BP for
  2620. the pid PID, then mark the stale flag for all threads of the group of
  2621. PID, and issue a stop request for them. The breakpoint or watchpoint
  2622. will be removed the next time each thread is resumed. Should only be
  2623. used if the debug register interface is HWDEBUG. */
  2624. void
  2625. ppc_linux_nat_target::clear_hw_breakpoint (pid_t pid,
  2626. const struct ppc_hw_breakpoint &bp)
  2627. {
  2628. gdb_assert (m_dreg_interface.hwdebug_p ());
  2629. auto process_it = m_process_info.find (pid);
  2630. gdb_assert (process_it != m_process_info.end ());
  2631. auto bp_it = std::find_if (process_it->second.requested_hw_bps.begin (),
  2632. process_it->second.requested_hw_bps.end (),
  2633. [&bp, this]
  2634. (const struct ppc_hw_breakpoint &curr)
  2635. { return hwdebug_point_cmp (bp, curr); }
  2636. );
  2637. /* If GDB is removing a watchpoint, it must have been inserted. */
  2638. gdb_assert (bp_it != process_it->second.requested_hw_bps.end ());
  2639. process_it->second.requested_hw_bps.erase (bp_it);
  2640. mark_debug_registers_changed (pid);
  2641. }
  2642. /* Register the hardware watchpoint value WP_VALUE for the pid PID,
  2643. then mark the stale flag for all threads of the group of PID, and
  2644. issue a stop request for them. The breakpoint or watchpoint will be
  2645. installed the next time each thread is resumed. Should only be used
  2646. if the debug register interface is DEBUGREG. */
  2647. void
  2648. ppc_linux_nat_target::register_wp (pid_t pid, long wp_value)
  2649. {
  2650. gdb_assert (m_dreg_interface.debugreg_p ());
  2651. /* Our other functions should have told GDB that we only have one
  2652. hardware watchpoint with this interface. */
  2653. gdb_assert (!m_process_info[pid].requested_wp_val.has_value ());
  2654. m_process_info[pid].requested_wp_val.emplace (wp_value);
  2655. mark_debug_registers_changed (pid);
  2656. }
  2657. /* Clear the hardware watchpoint registration for the pid PID, then mark
  2658. the stale flag for all threads of the group of PID, and issue a stop
  2659. request for them. The breakpoint or watchpoint will be installed the
  2660. next time each thread is resumed. Should only be used if the debug
  2661. register interface is DEBUGREG. */
  2662. void
  2663. ppc_linux_nat_target::clear_wp (pid_t pid)
  2664. {
  2665. gdb_assert (m_dreg_interface.debugreg_p ());
  2666. auto process_it = m_process_info.find (pid);
  2667. gdb_assert (process_it != m_process_info.end ());
  2668. gdb_assert (process_it->second.requested_wp_val.has_value ());
  2669. process_it->second.requested_wp_val.reset ();
  2670. mark_debug_registers_changed (pid);
  2671. }
  2672. /* Initialize the arch-specific thread state for LWP, if it not already
  2673. created. */
  2674. void
  2675. ppc_linux_nat_target::init_arch_lwp_info (struct lwp_info *lp)
  2676. {
  2677. if (lwp_arch_private_info (lp) == NULL)
  2678. {
  2679. lwp_set_arch_private_info (lp, XCNEW (struct arch_lwp_info));
  2680. lwp_arch_private_info (lp)->debug_regs_stale = false;
  2681. lwp_arch_private_info (lp)->lwp_ptid = lp->ptid;
  2682. }
  2683. }
  2684. /* Get the arch-specific thread state for LWP, creating it if
  2685. necessary. */
  2686. arch_lwp_info *
  2687. ppc_linux_nat_target::get_arch_lwp_info (struct lwp_info *lp)
  2688. {
  2689. init_arch_lwp_info (lp);
  2690. return lwp_arch_private_info (lp);
  2691. }
  2692. void _initialize_ppc_linux_nat ();
  2693. void
  2694. _initialize_ppc_linux_nat ()
  2695. {
  2696. linux_target = &the_ppc_linux_nat_target;
  2697. /* Register the target. */
  2698. add_inf_child_target (linux_target);
  2699. }