gdbstub.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921
  1. /*
  2. * gdb server stub
  3. *
  4. * Copyright (c) 2003-2005 Fabrice Bellard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "config.h"
  20. #include "qemu-common.h"
  21. #ifdef CONFIG_USER_ONLY
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <stdarg.h>
  25. #include <string.h>
  26. #include <errno.h>
  27. #include <unistd.h>
  28. #include <fcntl.h>
  29. #include "qemu.h"
  30. #else
  31. #include "monitor.h"
  32. #include "qemu-char.h"
  33. #include "sysemu.h"
  34. #include "gdbstub.h"
  35. #endif
  36. #define MAX_PACKET_LENGTH 4096
  37. #include "cpu.h"
  38. #include "qemu_socket.h"
  39. #include "kvm.h"
  40. #ifndef TARGET_CPU_MEMORY_RW_DEBUG
  41. static inline int target_memory_rw_debug(CPUState *env, target_ulong addr,
  42. uint8_t *buf, int len, int is_write)
  43. {
  44. return cpu_memory_rw_debug(env, addr, buf, len, is_write);
  45. }
  46. #else
  47. /* target_memory_rw_debug() defined in cpu.h */
  48. #endif
  49. enum {
  50. GDB_SIGNAL_0 = 0,
  51. GDB_SIGNAL_INT = 2,
  52. GDB_SIGNAL_QUIT = 3,
  53. GDB_SIGNAL_TRAP = 5,
  54. GDB_SIGNAL_ABRT = 6,
  55. GDB_SIGNAL_ALRM = 14,
  56. GDB_SIGNAL_IO = 23,
  57. GDB_SIGNAL_XCPU = 24,
  58. GDB_SIGNAL_UNKNOWN = 143
  59. };
  60. #ifdef CONFIG_USER_ONLY
  61. /* Map target signal numbers to GDB protocol signal numbers and vice
  62. * versa. For user emulation's currently supported systems, we can
  63. * assume most signals are defined.
  64. */
  65. static int gdb_signal_table[] = {
  66. 0,
  67. TARGET_SIGHUP,
  68. TARGET_SIGINT,
  69. TARGET_SIGQUIT,
  70. TARGET_SIGILL,
  71. TARGET_SIGTRAP,
  72. TARGET_SIGABRT,
  73. -1, /* SIGEMT */
  74. TARGET_SIGFPE,
  75. TARGET_SIGKILL,
  76. TARGET_SIGBUS,
  77. TARGET_SIGSEGV,
  78. TARGET_SIGSYS,
  79. TARGET_SIGPIPE,
  80. TARGET_SIGALRM,
  81. TARGET_SIGTERM,
  82. TARGET_SIGURG,
  83. TARGET_SIGSTOP,
  84. TARGET_SIGTSTP,
  85. TARGET_SIGCONT,
  86. TARGET_SIGCHLD,
  87. TARGET_SIGTTIN,
  88. TARGET_SIGTTOU,
  89. TARGET_SIGIO,
  90. TARGET_SIGXCPU,
  91. TARGET_SIGXFSZ,
  92. TARGET_SIGVTALRM,
  93. TARGET_SIGPROF,
  94. TARGET_SIGWINCH,
  95. -1, /* SIGLOST */
  96. TARGET_SIGUSR1,
  97. TARGET_SIGUSR2,
  98. #ifdef TARGET_SIGPWR
  99. TARGET_SIGPWR,
  100. #else
  101. -1,
  102. #endif
  103. -1, /* SIGPOLL */
  104. -1,
  105. -1,
  106. -1,
  107. -1,
  108. -1,
  109. -1,
  110. -1,
  111. -1,
  112. -1,
  113. -1,
  114. -1,
  115. #ifdef __SIGRTMIN
  116. __SIGRTMIN + 1,
  117. __SIGRTMIN + 2,
  118. __SIGRTMIN + 3,
  119. __SIGRTMIN + 4,
  120. __SIGRTMIN + 5,
  121. __SIGRTMIN + 6,
  122. __SIGRTMIN + 7,
  123. __SIGRTMIN + 8,
  124. __SIGRTMIN + 9,
  125. __SIGRTMIN + 10,
  126. __SIGRTMIN + 11,
  127. __SIGRTMIN + 12,
  128. __SIGRTMIN + 13,
  129. __SIGRTMIN + 14,
  130. __SIGRTMIN + 15,
  131. __SIGRTMIN + 16,
  132. __SIGRTMIN + 17,
  133. __SIGRTMIN + 18,
  134. __SIGRTMIN + 19,
  135. __SIGRTMIN + 20,
  136. __SIGRTMIN + 21,
  137. __SIGRTMIN + 22,
  138. __SIGRTMIN + 23,
  139. __SIGRTMIN + 24,
  140. __SIGRTMIN + 25,
  141. __SIGRTMIN + 26,
  142. __SIGRTMIN + 27,
  143. __SIGRTMIN + 28,
  144. __SIGRTMIN + 29,
  145. __SIGRTMIN + 30,
  146. __SIGRTMIN + 31,
  147. -1, /* SIGCANCEL */
  148. __SIGRTMIN,
  149. __SIGRTMIN + 32,
  150. __SIGRTMIN + 33,
  151. __SIGRTMIN + 34,
  152. __SIGRTMIN + 35,
  153. __SIGRTMIN + 36,
  154. __SIGRTMIN + 37,
  155. __SIGRTMIN + 38,
  156. __SIGRTMIN + 39,
  157. __SIGRTMIN + 40,
  158. __SIGRTMIN + 41,
  159. __SIGRTMIN + 42,
  160. __SIGRTMIN + 43,
  161. __SIGRTMIN + 44,
  162. __SIGRTMIN + 45,
  163. __SIGRTMIN + 46,
  164. __SIGRTMIN + 47,
  165. __SIGRTMIN + 48,
  166. __SIGRTMIN + 49,
  167. __SIGRTMIN + 50,
  168. __SIGRTMIN + 51,
  169. __SIGRTMIN + 52,
  170. __SIGRTMIN + 53,
  171. __SIGRTMIN + 54,
  172. __SIGRTMIN + 55,
  173. __SIGRTMIN + 56,
  174. __SIGRTMIN + 57,
  175. __SIGRTMIN + 58,
  176. __SIGRTMIN + 59,
  177. __SIGRTMIN + 60,
  178. __SIGRTMIN + 61,
  179. __SIGRTMIN + 62,
  180. __SIGRTMIN + 63,
  181. __SIGRTMIN + 64,
  182. __SIGRTMIN + 65,
  183. __SIGRTMIN + 66,
  184. __SIGRTMIN + 67,
  185. __SIGRTMIN + 68,
  186. __SIGRTMIN + 69,
  187. __SIGRTMIN + 70,
  188. __SIGRTMIN + 71,
  189. __SIGRTMIN + 72,
  190. __SIGRTMIN + 73,
  191. __SIGRTMIN + 74,
  192. __SIGRTMIN + 75,
  193. __SIGRTMIN + 76,
  194. __SIGRTMIN + 77,
  195. __SIGRTMIN + 78,
  196. __SIGRTMIN + 79,
  197. __SIGRTMIN + 80,
  198. __SIGRTMIN + 81,
  199. __SIGRTMIN + 82,
  200. __SIGRTMIN + 83,
  201. __SIGRTMIN + 84,
  202. __SIGRTMIN + 85,
  203. __SIGRTMIN + 86,
  204. __SIGRTMIN + 87,
  205. __SIGRTMIN + 88,
  206. __SIGRTMIN + 89,
  207. __SIGRTMIN + 90,
  208. __SIGRTMIN + 91,
  209. __SIGRTMIN + 92,
  210. __SIGRTMIN + 93,
  211. __SIGRTMIN + 94,
  212. __SIGRTMIN + 95,
  213. -1, /* SIGINFO */
  214. -1, /* UNKNOWN */
  215. -1, /* DEFAULT */
  216. -1,
  217. -1,
  218. -1,
  219. -1,
  220. -1,
  221. -1
  222. #endif
  223. };
  224. #else
  225. /* In system mode we only need SIGINT and SIGTRAP; other signals
  226. are not yet supported. */
  227. enum {
  228. TARGET_SIGINT = 2,
  229. TARGET_SIGTRAP = 5
  230. };
  231. static int gdb_signal_table[] = {
  232. -1,
  233. -1,
  234. TARGET_SIGINT,
  235. -1,
  236. -1,
  237. TARGET_SIGTRAP
  238. };
  239. #endif
  240. #ifdef CONFIG_USER_ONLY
  241. static int target_signal_to_gdb (int sig)
  242. {
  243. int i;
  244. for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++)
  245. if (gdb_signal_table[i] == sig)
  246. return i;
  247. return GDB_SIGNAL_UNKNOWN;
  248. }
  249. #endif
  250. static int gdb_signal_to_target (int sig)
  251. {
  252. if (sig < ARRAY_SIZE (gdb_signal_table))
  253. return gdb_signal_table[sig];
  254. else
  255. return -1;
  256. }
  257. //#define DEBUG_GDB
  258. typedef struct GDBRegisterState {
  259. int base_reg;
  260. int num_regs;
  261. gdb_reg_cb get_reg;
  262. gdb_reg_cb set_reg;
  263. const char *xml;
  264. struct GDBRegisterState *next;
  265. } GDBRegisterState;
  266. enum RSState {
  267. RS_INACTIVE,
  268. RS_IDLE,
  269. RS_GETLINE,
  270. RS_CHKSUM1,
  271. RS_CHKSUM2,
  272. RS_SYSCALL,
  273. };
  274. typedef struct GDBState {
  275. CPUState *c_cpu; /* current CPU for step/continue ops */
  276. CPUState *g_cpu; /* current CPU for other ops */
  277. CPUState *query_cpu; /* for q{f|s}ThreadInfo */
  278. enum RSState state; /* parsing state */
  279. char line_buf[MAX_PACKET_LENGTH];
  280. int line_buf_index;
  281. int line_csum;
  282. uint8_t last_packet[MAX_PACKET_LENGTH + 4];
  283. int last_packet_len;
  284. int signal;
  285. #ifdef CONFIG_USER_ONLY
  286. int fd;
  287. int running_state;
  288. #else
  289. CharDriverState *chr;
  290. CharDriverState *mon_chr;
  291. #endif
  292. } GDBState;
  293. /* By default use no IRQs and no timers while single stepping so as to
  294. * make single stepping like an ICE HW step.
  295. */
  296. static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
  297. static GDBState *gdbserver_state;
  298. /* This is an ugly hack to cope with both new and old gdb.
  299. If gdb sends qXfer:features:read then assume we're talking to a newish
  300. gdb that understands target descriptions. */
  301. static int gdb_has_xml;
  302. #ifdef CONFIG_USER_ONLY
  303. /* XXX: This is not thread safe. Do we care? */
  304. static int gdbserver_fd = -1;
  305. static int get_char(GDBState *s)
  306. {
  307. uint8_t ch;
  308. int ret;
  309. for(;;) {
  310. ret = qemu_recv(s->fd, &ch, 1, 0);
  311. if (ret < 0) {
  312. if (errno == ECONNRESET)
  313. s->fd = -1;
  314. if (errno != EINTR && errno != EAGAIN)
  315. return -1;
  316. } else if (ret == 0) {
  317. close(s->fd);
  318. s->fd = -1;
  319. return -1;
  320. } else {
  321. break;
  322. }
  323. }
  324. return ch;
  325. }
  326. #endif
  327. static gdb_syscall_complete_cb gdb_current_syscall_cb;
  328. static enum {
  329. GDB_SYS_UNKNOWN,
  330. GDB_SYS_ENABLED,
  331. GDB_SYS_DISABLED,
  332. } gdb_syscall_mode;
  333. /* If gdb is connected when the first semihosting syscall occurs then use
  334. remote gdb syscalls. Otherwise use native file IO. */
  335. int use_gdb_syscalls(void)
  336. {
  337. if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
  338. gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED
  339. : GDB_SYS_DISABLED);
  340. }
  341. return gdb_syscall_mode == GDB_SYS_ENABLED;
  342. }
  343. /* Resume execution. */
  344. static inline void gdb_continue(GDBState *s)
  345. {
  346. #ifdef CONFIG_USER_ONLY
  347. s->running_state = 1;
  348. #else
  349. vm_start();
  350. #endif
  351. }
  352. static void put_buffer(GDBState *s, const uint8_t *buf, int len)
  353. {
  354. #ifdef CONFIG_USER_ONLY
  355. int ret;
  356. while (len > 0) {
  357. ret = send(s->fd, buf, len, 0);
  358. if (ret < 0) {
  359. if (errno != EINTR && errno != EAGAIN)
  360. return;
  361. } else {
  362. buf += ret;
  363. len -= ret;
  364. }
  365. }
  366. #else
  367. qemu_chr_fe_write(s->chr, buf, len);
  368. #endif
  369. }
  370. static inline int fromhex(int v)
  371. {
  372. if (v >= '0' && v <= '9')
  373. return v - '0';
  374. else if (v >= 'A' && v <= 'F')
  375. return v - 'A' + 10;
  376. else if (v >= 'a' && v <= 'f')
  377. return v - 'a' + 10;
  378. else
  379. return 0;
  380. }
  381. static inline int tohex(int v)
  382. {
  383. if (v < 10)
  384. return v + '0';
  385. else
  386. return v - 10 + 'a';
  387. }
  388. static void memtohex(char *buf, const uint8_t *mem, int len)
  389. {
  390. int i, c;
  391. char *q;
  392. q = buf;
  393. for(i = 0; i < len; i++) {
  394. c = mem[i];
  395. *q++ = tohex(c >> 4);
  396. *q++ = tohex(c & 0xf);
  397. }
  398. *q = '\0';
  399. }
  400. static void hextomem(uint8_t *mem, const char *buf, int len)
  401. {
  402. int i;
  403. for(i = 0; i < len; i++) {
  404. mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
  405. buf += 2;
  406. }
  407. }
  408. /* return -1 if error, 0 if OK */
  409. static int put_packet_binary(GDBState *s, const char *buf, int len)
  410. {
  411. int csum, i;
  412. uint8_t *p;
  413. for(;;) {
  414. p = s->last_packet;
  415. *(p++) = '$';
  416. memcpy(p, buf, len);
  417. p += len;
  418. csum = 0;
  419. for(i = 0; i < len; i++) {
  420. csum += buf[i];
  421. }
  422. *(p++) = '#';
  423. *(p++) = tohex((csum >> 4) & 0xf);
  424. *(p++) = tohex((csum) & 0xf);
  425. s->last_packet_len = p - s->last_packet;
  426. put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
  427. #ifdef CONFIG_USER_ONLY
  428. i = get_char(s);
  429. if (i < 0)
  430. return -1;
  431. if (i == '+')
  432. break;
  433. #else
  434. break;
  435. #endif
  436. }
  437. return 0;
  438. }
  439. /* return -1 if error, 0 if OK */
  440. static int put_packet(GDBState *s, const char *buf)
  441. {
  442. #ifdef DEBUG_GDB
  443. printf("reply='%s'\n", buf);
  444. #endif
  445. return put_packet_binary(s, buf, strlen(buf));
  446. }
  447. /* The GDB remote protocol transfers values in target byte order. This means
  448. we can use the raw memory access routines to access the value buffer.
  449. Conveniently, these also handle the case where the buffer is mis-aligned.
  450. */
  451. #define GET_REG8(val) do { \
  452. stb_p(mem_buf, val); \
  453. return 1; \
  454. } while(0)
  455. #define GET_REG16(val) do { \
  456. stw_p(mem_buf, val); \
  457. return 2; \
  458. } while(0)
  459. #define GET_REG32(val) do { \
  460. stl_p(mem_buf, val); \
  461. return 4; \
  462. } while(0)
  463. #define GET_REG64(val) do { \
  464. stq_p(mem_buf, val); \
  465. return 8; \
  466. } while(0)
  467. #if TARGET_LONG_BITS == 64
  468. #define GET_REGL(val) GET_REG64(val)
  469. #define ldtul_p(addr) ldq_p(addr)
  470. #else
  471. #define GET_REGL(val) GET_REG32(val)
  472. #define ldtul_p(addr) ldl_p(addr)
  473. #endif
  474. #if defined(TARGET_I386)
  475. #ifdef TARGET_X86_64
  476. static const int gpr_map[16] = {
  477. R_EAX, R_EBX, R_ECX, R_EDX, R_ESI, R_EDI, R_EBP, R_ESP,
  478. 8, 9, 10, 11, 12, 13, 14, 15
  479. };
  480. #else
  481. #define gpr_map gpr_map32
  482. #endif
  483. static const int gpr_map32[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
  484. #define NUM_CORE_REGS (CPU_NB_REGS * 2 + 25)
  485. #define IDX_IP_REG CPU_NB_REGS
  486. #define IDX_FLAGS_REG (IDX_IP_REG + 1)
  487. #define IDX_SEG_REGS (IDX_FLAGS_REG + 1)
  488. #define IDX_FP_REGS (IDX_SEG_REGS + 6)
  489. #define IDX_XMM_REGS (IDX_FP_REGS + 16)
  490. #define IDX_MXCSR_REG (IDX_XMM_REGS + CPU_NB_REGS)
  491. static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
  492. {
  493. if (n < CPU_NB_REGS) {
  494. if (TARGET_LONG_BITS == 64 && env->hflags & HF_CS64_MASK) {
  495. GET_REG64(env->regs[gpr_map[n]]);
  496. } else if (n < CPU_NB_REGS32) {
  497. GET_REG32(env->regs[gpr_map32[n]]);
  498. }
  499. } else if (n >= IDX_FP_REGS && n < IDX_FP_REGS + 8) {
  500. #ifdef USE_X86LDOUBLE
  501. /* FIXME: byteswap float values - after fixing fpregs layout. */
  502. memcpy(mem_buf, &env->fpregs[n - IDX_FP_REGS], 10);
  503. #else
  504. memset(mem_buf, 0, 10);
  505. #endif
  506. return 10;
  507. } else if (n >= IDX_XMM_REGS && n < IDX_XMM_REGS + CPU_NB_REGS) {
  508. n -= IDX_XMM_REGS;
  509. if (n < CPU_NB_REGS32 ||
  510. (TARGET_LONG_BITS == 64 && env->hflags & HF_CS64_MASK)) {
  511. stq_p(mem_buf, env->xmm_regs[n].XMM_Q(0));
  512. stq_p(mem_buf + 8, env->xmm_regs[n].XMM_Q(1));
  513. return 16;
  514. }
  515. } else {
  516. switch (n) {
  517. case IDX_IP_REG:
  518. if (TARGET_LONG_BITS == 64 && env->hflags & HF_CS64_MASK) {
  519. GET_REG64(env->eip);
  520. } else {
  521. GET_REG32(env->eip);
  522. }
  523. case IDX_FLAGS_REG: GET_REG32(env->eflags);
  524. case IDX_SEG_REGS: GET_REG32(env->segs[R_CS].selector);
  525. case IDX_SEG_REGS + 1: GET_REG32(env->segs[R_SS].selector);
  526. case IDX_SEG_REGS + 2: GET_REG32(env->segs[R_DS].selector);
  527. case IDX_SEG_REGS + 3: GET_REG32(env->segs[R_ES].selector);
  528. case IDX_SEG_REGS + 4: GET_REG32(env->segs[R_FS].selector);
  529. case IDX_SEG_REGS + 5: GET_REG32(env->segs[R_GS].selector);
  530. case IDX_FP_REGS + 8: GET_REG32(env->fpuc);
  531. case IDX_FP_REGS + 9: GET_REG32((env->fpus & ~0x3800) |
  532. (env->fpstt & 0x7) << 11);
  533. case IDX_FP_REGS + 10: GET_REG32(0); /* ftag */
  534. case IDX_FP_REGS + 11: GET_REG32(0); /* fiseg */
  535. case IDX_FP_REGS + 12: GET_REG32(0); /* fioff */
  536. case IDX_FP_REGS + 13: GET_REG32(0); /* foseg */
  537. case IDX_FP_REGS + 14: GET_REG32(0); /* fooff */
  538. case IDX_FP_REGS + 15: GET_REG32(0); /* fop */
  539. case IDX_MXCSR_REG: GET_REG32(env->mxcsr);
  540. }
  541. }
  542. return 0;
  543. }
  544. static int cpu_x86_gdb_load_seg(CPUState *env, int sreg, uint8_t *mem_buf)
  545. {
  546. uint16_t selector = ldl_p(mem_buf);
  547. if (selector != env->segs[sreg].selector) {
  548. #if defined(CONFIG_USER_ONLY)
  549. cpu_x86_load_seg(env, sreg, selector);
  550. #else
  551. unsigned int limit, flags;
  552. target_ulong base;
  553. if (!(env->cr[0] & CR0_PE_MASK) || (env->eflags & VM_MASK)) {
  554. base = selector << 4;
  555. limit = 0xffff;
  556. flags = 0;
  557. } else {
  558. if (!cpu_x86_get_descr_debug(env, selector, &base, &limit, &flags))
  559. return 4;
  560. }
  561. cpu_x86_load_seg_cache(env, sreg, selector, base, limit, flags);
  562. #endif
  563. }
  564. return 4;
  565. }
  566. static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
  567. {
  568. uint32_t tmp;
  569. if (n < CPU_NB_REGS) {
  570. if (TARGET_LONG_BITS == 64 && env->hflags & HF_CS64_MASK) {
  571. env->regs[gpr_map[n]] = ldtul_p(mem_buf);
  572. return sizeof(target_ulong);
  573. } else if (n < CPU_NB_REGS32) {
  574. n = gpr_map32[n];
  575. env->regs[n] &= ~0xffffffffUL;
  576. env->regs[n] |= (uint32_t)ldl_p(mem_buf);
  577. return 4;
  578. }
  579. } else if (n >= IDX_FP_REGS && n < IDX_FP_REGS + 8) {
  580. #ifdef USE_X86LDOUBLE
  581. /* FIXME: byteswap float values - after fixing fpregs layout. */
  582. memcpy(&env->fpregs[n - IDX_FP_REGS], mem_buf, 10);
  583. #endif
  584. return 10;
  585. } else if (n >= IDX_XMM_REGS && n < IDX_XMM_REGS + CPU_NB_REGS) {
  586. n -= IDX_XMM_REGS;
  587. if (n < CPU_NB_REGS32 ||
  588. (TARGET_LONG_BITS == 64 && env->hflags & HF_CS64_MASK)) {
  589. env->xmm_regs[n].XMM_Q(0) = ldq_p(mem_buf);
  590. env->xmm_regs[n].XMM_Q(1) = ldq_p(mem_buf + 8);
  591. return 16;
  592. }
  593. } else {
  594. switch (n) {
  595. case IDX_IP_REG:
  596. if (TARGET_LONG_BITS == 64 && env->hflags & HF_CS64_MASK) {
  597. env->eip = ldq_p(mem_buf);
  598. return 8;
  599. } else {
  600. env->eip &= ~0xffffffffUL;
  601. env->eip |= (uint32_t)ldl_p(mem_buf);
  602. return 4;
  603. }
  604. case IDX_FLAGS_REG:
  605. env->eflags = ldl_p(mem_buf);
  606. return 4;
  607. case IDX_SEG_REGS: return cpu_x86_gdb_load_seg(env, R_CS, mem_buf);
  608. case IDX_SEG_REGS + 1: return cpu_x86_gdb_load_seg(env, R_SS, mem_buf);
  609. case IDX_SEG_REGS + 2: return cpu_x86_gdb_load_seg(env, R_DS, mem_buf);
  610. case IDX_SEG_REGS + 3: return cpu_x86_gdb_load_seg(env, R_ES, mem_buf);
  611. case IDX_SEG_REGS + 4: return cpu_x86_gdb_load_seg(env, R_FS, mem_buf);
  612. case IDX_SEG_REGS + 5: return cpu_x86_gdb_load_seg(env, R_GS, mem_buf);
  613. case IDX_FP_REGS + 8:
  614. env->fpuc = ldl_p(mem_buf);
  615. return 4;
  616. case IDX_FP_REGS + 9:
  617. tmp = ldl_p(mem_buf);
  618. env->fpstt = (tmp >> 11) & 7;
  619. env->fpus = tmp & ~0x3800;
  620. return 4;
  621. case IDX_FP_REGS + 10: /* ftag */ return 4;
  622. case IDX_FP_REGS + 11: /* fiseg */ return 4;
  623. case IDX_FP_REGS + 12: /* fioff */ return 4;
  624. case IDX_FP_REGS + 13: /* foseg */ return 4;
  625. case IDX_FP_REGS + 14: /* fooff */ return 4;
  626. case IDX_FP_REGS + 15: /* fop */ return 4;
  627. case IDX_MXCSR_REG:
  628. env->mxcsr = ldl_p(mem_buf);
  629. return 4;
  630. }
  631. }
  632. /* Unrecognised register. */
  633. return 0;
  634. }
  635. #elif defined (TARGET_PPC)
  636. /* Old gdb always expects FP registers. Newer (xml-aware) gdb only
  637. expects whatever the target description contains. Due to a
  638. historical mishap the FP registers appear in between core integer
  639. regs and PC, MSR, CR, and so forth. We hack round this by giving the
  640. FP regs zero size when talking to a newer gdb. */
  641. #define NUM_CORE_REGS 71
  642. #if defined (TARGET_PPC64)
  643. #define GDB_CORE_XML "power64-core.xml"
  644. #else
  645. #define GDB_CORE_XML "power-core.xml"
  646. #endif
  647. static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
  648. {
  649. if (n < 32) {
  650. /* gprs */
  651. GET_REGL(env->gpr[n]);
  652. } else if (n < 64) {
  653. /* fprs */
  654. if (gdb_has_xml)
  655. return 0;
  656. stfq_p(mem_buf, env->fpr[n-32]);
  657. return 8;
  658. } else {
  659. switch (n) {
  660. case 64: GET_REGL(env->nip);
  661. case 65: GET_REGL(env->msr);
  662. case 66:
  663. {
  664. uint32_t cr = 0;
  665. int i;
  666. for (i = 0; i < 8; i++)
  667. cr |= env->crf[i] << (32 - ((i + 1) * 4));
  668. GET_REG32(cr);
  669. }
  670. case 67: GET_REGL(env->lr);
  671. case 68: GET_REGL(env->ctr);
  672. case 69: GET_REGL(env->xer);
  673. case 70:
  674. {
  675. if (gdb_has_xml)
  676. return 0;
  677. GET_REG32(env->fpscr);
  678. }
  679. }
  680. }
  681. return 0;
  682. }
  683. static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
  684. {
  685. if (n < 32) {
  686. /* gprs */
  687. env->gpr[n] = ldtul_p(mem_buf);
  688. return sizeof(target_ulong);
  689. } else if (n < 64) {
  690. /* fprs */
  691. if (gdb_has_xml)
  692. return 0;
  693. env->fpr[n-32] = ldfq_p(mem_buf);
  694. return 8;
  695. } else {
  696. switch (n) {
  697. case 64:
  698. env->nip = ldtul_p(mem_buf);
  699. return sizeof(target_ulong);
  700. case 65:
  701. ppc_store_msr(env, ldtul_p(mem_buf));
  702. return sizeof(target_ulong);
  703. case 66:
  704. {
  705. uint32_t cr = ldl_p(mem_buf);
  706. int i;
  707. for (i = 0; i < 8; i++)
  708. env->crf[i] = (cr >> (32 - ((i + 1) * 4))) & 0xF;
  709. return 4;
  710. }
  711. case 67:
  712. env->lr = ldtul_p(mem_buf);
  713. return sizeof(target_ulong);
  714. case 68:
  715. env->ctr = ldtul_p(mem_buf);
  716. return sizeof(target_ulong);
  717. case 69:
  718. env->xer = ldtul_p(mem_buf);
  719. return sizeof(target_ulong);
  720. case 70:
  721. /* fpscr */
  722. if (gdb_has_xml)
  723. return 0;
  724. return 4;
  725. }
  726. }
  727. return 0;
  728. }
  729. #elif defined (TARGET_SPARC)
  730. #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
  731. #define NUM_CORE_REGS 86
  732. #else
  733. #define NUM_CORE_REGS 72
  734. #endif
  735. #ifdef TARGET_ABI32
  736. #define GET_REGA(val) GET_REG32(val)
  737. #else
  738. #define GET_REGA(val) GET_REGL(val)
  739. #endif
  740. static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
  741. {
  742. if (n < 8) {
  743. /* g0..g7 */
  744. GET_REGA(env->gregs[n]);
  745. }
  746. if (n < 32) {
  747. /* register window */
  748. GET_REGA(env->regwptr[n - 8]);
  749. }
  750. #if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
  751. if (n < 64) {
  752. /* fprs */
  753. if (n & 1) {
  754. GET_REG32(env->fpr[(n - 32) / 2].l.lower);
  755. } else {
  756. GET_REG32(env->fpr[(n - 32) / 2].l.upper);
  757. }
  758. }
  759. /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
  760. switch (n) {
  761. case 64: GET_REGA(env->y);
  762. case 65: GET_REGA(cpu_get_psr(env));
  763. case 66: GET_REGA(env->wim);
  764. case 67: GET_REGA(env->tbr);
  765. case 68: GET_REGA(env->pc);
  766. case 69: GET_REGA(env->npc);
  767. case 70: GET_REGA(env->fsr);
  768. case 71: GET_REGA(0); /* csr */
  769. default: GET_REGA(0);
  770. }
  771. #else
  772. if (n < 64) {
  773. /* f0-f31 */
  774. if (n & 1) {
  775. GET_REG32(env->fpr[(n - 32) / 2].l.lower);
  776. } else {
  777. GET_REG32(env->fpr[(n - 32) / 2].l.upper);
  778. }
  779. }
  780. if (n < 80) {
  781. /* f32-f62 (double width, even numbers only) */
  782. GET_REG64(env->fpr[(n - 32) / 2].ll);
  783. }
  784. switch (n) {
  785. case 80: GET_REGL(env->pc);
  786. case 81: GET_REGL(env->npc);
  787. case 82: GET_REGL((cpu_get_ccr(env) << 32) |
  788. ((env->asi & 0xff) << 24) |
  789. ((env->pstate & 0xfff) << 8) |
  790. cpu_get_cwp64(env));
  791. case 83: GET_REGL(env->fsr);
  792. case 84: GET_REGL(env->fprs);
  793. case 85: GET_REGL(env->y);
  794. }
  795. #endif
  796. return 0;
  797. }
  798. static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
  799. {
  800. #if defined(TARGET_ABI32)
  801. abi_ulong tmp;
  802. tmp = ldl_p(mem_buf);
  803. #else
  804. target_ulong tmp;
  805. tmp = ldtul_p(mem_buf);
  806. #endif
  807. if (n < 8) {
  808. /* g0..g7 */
  809. env->gregs[n] = tmp;
  810. } else if (n < 32) {
  811. /* register window */
  812. env->regwptr[n - 8] = tmp;
  813. }
  814. #if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
  815. else if (n < 64) {
  816. /* fprs */
  817. /* f0-f31 */
  818. if (n & 1) {
  819. env->fpr[(n - 32) / 2].l.lower = tmp;
  820. } else {
  821. env->fpr[(n - 32) / 2].l.upper = tmp;
  822. }
  823. } else {
  824. /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
  825. switch (n) {
  826. case 64: env->y = tmp; break;
  827. case 65: cpu_put_psr(env, tmp); break;
  828. case 66: env->wim = tmp; break;
  829. case 67: env->tbr = tmp; break;
  830. case 68: env->pc = tmp; break;
  831. case 69: env->npc = tmp; break;
  832. case 70: env->fsr = tmp; break;
  833. default: return 0;
  834. }
  835. }
  836. return 4;
  837. #else
  838. else if (n < 64) {
  839. /* f0-f31 */
  840. tmp = ldl_p(mem_buf);
  841. if (n & 1) {
  842. env->fpr[(n - 32) / 2].l.lower = tmp;
  843. } else {
  844. env->fpr[(n - 32) / 2].l.upper = tmp;
  845. }
  846. return 4;
  847. } else if (n < 80) {
  848. /* f32-f62 (double width, even numbers only) */
  849. env->fpr[(n - 32) / 2].ll = tmp;
  850. } else {
  851. switch (n) {
  852. case 80: env->pc = tmp; break;
  853. case 81: env->npc = tmp; break;
  854. case 82:
  855. cpu_put_ccr(env, tmp >> 32);
  856. env->asi = (tmp >> 24) & 0xff;
  857. env->pstate = (tmp >> 8) & 0xfff;
  858. cpu_put_cwp64(env, tmp & 0xff);
  859. break;
  860. case 83: env->fsr = tmp; break;
  861. case 84: env->fprs = tmp; break;
  862. case 85: env->y = tmp; break;
  863. default: return 0;
  864. }
  865. }
  866. return 8;
  867. #endif
  868. }
  869. #elif defined (TARGET_ARM)
  870. /* Old gdb always expect FPA registers. Newer (xml-aware) gdb only expect
  871. whatever the target description contains. Due to a historical mishap
  872. the FPA registers appear in between core integer regs and the CPSR.
  873. We hack round this by giving the FPA regs zero size when talking to a
  874. newer gdb. */
  875. #define NUM_CORE_REGS 26
  876. #define GDB_CORE_XML "arm-core.xml"
  877. static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
  878. {
  879. if (n < 16) {
  880. /* Core integer register. */
  881. GET_REG32(env->regs[n]);
  882. }
  883. if (n < 24) {
  884. /* FPA registers. */
  885. if (gdb_has_xml)
  886. return 0;
  887. memset(mem_buf, 0, 12);
  888. return 12;
  889. }
  890. switch (n) {
  891. case 24:
  892. /* FPA status register. */
  893. if (gdb_has_xml)
  894. return 0;
  895. GET_REG32(0);
  896. case 25:
  897. /* CPSR */
  898. GET_REG32(cpsr_read(env));
  899. }
  900. /* Unknown register. */
  901. return 0;
  902. }
  903. static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
  904. {
  905. uint32_t tmp;
  906. tmp = ldl_p(mem_buf);
  907. /* Mask out low bit of PC to workaround gdb bugs. This will probably
  908. cause problems if we ever implement the Jazelle DBX extensions. */
  909. if (n == 15)
  910. tmp &= ~1;
  911. if (n < 16) {
  912. /* Core integer register. */
  913. env->regs[n] = tmp;
  914. return 4;
  915. }
  916. if (n < 24) { /* 16-23 */
  917. /* FPA registers (ignored). */
  918. if (gdb_has_xml)
  919. return 0;
  920. return 12;
  921. }
  922. switch (n) {
  923. case 24:
  924. /* FPA status register (ignored). */
  925. if (gdb_has_xml)
  926. return 0;
  927. return 4;
  928. case 25:
  929. /* CPSR */
  930. cpsr_write (env, tmp, 0xffffffff);
  931. return 4;
  932. }
  933. /* Unknown register. */
  934. return 0;
  935. }
  936. #elif defined (TARGET_M68K)
  937. #define NUM_CORE_REGS 18
  938. #define GDB_CORE_XML "cf-core.xml"
  939. static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
  940. {
  941. if (n < 8) {
  942. /* D0-D7 */
  943. GET_REG32(env->dregs[n]);
  944. } else if (n < 16) {
  945. /* A0-A7 */
  946. GET_REG32(env->aregs[n - 8]);
  947. } else {
  948. switch (n) {
  949. case 16: GET_REG32(env->sr);
  950. case 17: GET_REG32(env->pc);
  951. }
  952. }
  953. /* FP registers not included here because they vary between
  954. ColdFire and m68k. Use XML bits for these. */
  955. return 0;
  956. }
  957. static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
  958. {
  959. uint32_t tmp;
  960. tmp = ldl_p(mem_buf);
  961. if (n < 8) {
  962. /* D0-D7 */
  963. env->dregs[n] = tmp;
  964. } else if (n < 16) {
  965. /* A0-A7 */
  966. env->aregs[n - 8] = tmp;
  967. } else {
  968. switch (n) {
  969. case 16: env->sr = tmp; break;
  970. case 17: env->pc = tmp; break;
  971. default: return 0;
  972. }
  973. }
  974. return 4;
  975. }
  976. #elif defined (TARGET_MIPS)
  977. #define NUM_CORE_REGS 73
  978. static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
  979. {
  980. if (n < 32) {
  981. GET_REGL(env->active_tc.gpr[n]);
  982. }
  983. if (env->CP0_Config1 & (1 << CP0C1_FP)) {
  984. if (n >= 38 && n < 70) {
  985. if (env->CP0_Status & (1 << CP0St_FR))
  986. GET_REGL(env->active_fpu.fpr[n - 38].d);
  987. else
  988. GET_REGL(env->active_fpu.fpr[n - 38].w[FP_ENDIAN_IDX]);
  989. }
  990. switch (n) {
  991. case 70: GET_REGL((int32_t)env->active_fpu.fcr31);
  992. case 71: GET_REGL((int32_t)env->active_fpu.fcr0);
  993. }
  994. }
  995. switch (n) {
  996. case 32: GET_REGL((int32_t)env->CP0_Status);
  997. case 33: GET_REGL(env->active_tc.LO[0]);
  998. case 34: GET_REGL(env->active_tc.HI[0]);
  999. case 35: GET_REGL(env->CP0_BadVAddr);
  1000. case 36: GET_REGL((int32_t)env->CP0_Cause);
  1001. case 37: GET_REGL(env->active_tc.PC | !!(env->hflags & MIPS_HFLAG_M16));
  1002. case 72: GET_REGL(0); /* fp */
  1003. case 89: GET_REGL((int32_t)env->CP0_PRid);
  1004. }
  1005. if (n >= 73 && n <= 88) {
  1006. /* 16 embedded regs. */
  1007. GET_REGL(0);
  1008. }
  1009. return 0;
  1010. }
  1011. /* convert MIPS rounding mode in FCR31 to IEEE library */
  1012. static unsigned int ieee_rm[] =
  1013. {
  1014. float_round_nearest_even,
  1015. float_round_to_zero,
  1016. float_round_up,
  1017. float_round_down
  1018. };
  1019. #define RESTORE_ROUNDING_MODE \
  1020. set_float_rounding_mode(ieee_rm[env->active_fpu.fcr31 & 3], &env->active_fpu.fp_status)
  1021. static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
  1022. {
  1023. target_ulong tmp;
  1024. tmp = ldtul_p(mem_buf);
  1025. if (n < 32) {
  1026. env->active_tc.gpr[n] = tmp;
  1027. return sizeof(target_ulong);
  1028. }
  1029. if (env->CP0_Config1 & (1 << CP0C1_FP)
  1030. && n >= 38 && n < 73) {
  1031. if (n < 70) {
  1032. if (env->CP0_Status & (1 << CP0St_FR))
  1033. env->active_fpu.fpr[n - 38].d = tmp;
  1034. else
  1035. env->active_fpu.fpr[n - 38].w[FP_ENDIAN_IDX] = tmp;
  1036. }
  1037. switch (n) {
  1038. case 70:
  1039. env->active_fpu.fcr31 = tmp & 0xFF83FFFF;
  1040. /* set rounding mode */
  1041. RESTORE_ROUNDING_MODE;
  1042. break;
  1043. case 71: env->active_fpu.fcr0 = tmp; break;
  1044. }
  1045. return sizeof(target_ulong);
  1046. }
  1047. switch (n) {
  1048. case 32: env->CP0_Status = tmp; break;
  1049. case 33: env->active_tc.LO[0] = tmp; break;
  1050. case 34: env->active_tc.HI[0] = tmp; break;
  1051. case 35: env->CP0_BadVAddr = tmp; break;
  1052. case 36: env->CP0_Cause = tmp; break;
  1053. case 37:
  1054. env->active_tc.PC = tmp & ~(target_ulong)1;
  1055. if (tmp & 1) {
  1056. env->hflags |= MIPS_HFLAG_M16;
  1057. } else {
  1058. env->hflags &= ~(MIPS_HFLAG_M16);
  1059. }
  1060. break;
  1061. case 72: /* fp, ignored */ break;
  1062. default:
  1063. if (n > 89)
  1064. return 0;
  1065. /* Other registers are readonly. Ignore writes. */
  1066. break;
  1067. }
  1068. return sizeof(target_ulong);
  1069. }
  1070. #elif defined (TARGET_SH4)
  1071. /* Hint: Use "set architecture sh4" in GDB to see fpu registers */
  1072. /* FIXME: We should use XML for this. */
  1073. #define NUM_CORE_REGS 59
  1074. static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
  1075. {
  1076. if (n < 8) {
  1077. if ((env->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB)) {
  1078. GET_REGL(env->gregs[n + 16]);
  1079. } else {
  1080. GET_REGL(env->gregs[n]);
  1081. }
  1082. } else if (n < 16) {
  1083. GET_REGL(env->gregs[n]);
  1084. } else if (n >= 25 && n < 41) {
  1085. GET_REGL(env->fregs[(n - 25) + ((env->fpscr & FPSCR_FR) ? 16 : 0)]);
  1086. } else if (n >= 43 && n < 51) {
  1087. GET_REGL(env->gregs[n - 43]);
  1088. } else if (n >= 51 && n < 59) {
  1089. GET_REGL(env->gregs[n - (51 - 16)]);
  1090. }
  1091. switch (n) {
  1092. case 16: GET_REGL(env->pc);
  1093. case 17: GET_REGL(env->pr);
  1094. case 18: GET_REGL(env->gbr);
  1095. case 19: GET_REGL(env->vbr);
  1096. case 20: GET_REGL(env->mach);
  1097. case 21: GET_REGL(env->macl);
  1098. case 22: GET_REGL(env->sr);
  1099. case 23: GET_REGL(env->fpul);
  1100. case 24: GET_REGL(env->fpscr);
  1101. case 41: GET_REGL(env->ssr);
  1102. case 42: GET_REGL(env->spc);
  1103. }
  1104. return 0;
  1105. }
  1106. static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
  1107. {
  1108. uint32_t tmp;
  1109. tmp = ldl_p(mem_buf);
  1110. if (n < 8) {
  1111. if ((env->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB)) {
  1112. env->gregs[n + 16] = tmp;
  1113. } else {
  1114. env->gregs[n] = tmp;
  1115. }
  1116. return 4;
  1117. } else if (n < 16) {
  1118. env->gregs[n] = tmp;
  1119. return 4;
  1120. } else if (n >= 25 && n < 41) {
  1121. env->fregs[(n - 25) + ((env->fpscr & FPSCR_FR) ? 16 : 0)] = tmp;
  1122. return 4;
  1123. } else if (n >= 43 && n < 51) {
  1124. env->gregs[n - 43] = tmp;
  1125. return 4;
  1126. } else if (n >= 51 && n < 59) {
  1127. env->gregs[n - (51 - 16)] = tmp;
  1128. return 4;
  1129. }
  1130. switch (n) {
  1131. case 16: env->pc = tmp; break;
  1132. case 17: env->pr = tmp; break;
  1133. case 18: env->gbr = tmp; break;
  1134. case 19: env->vbr = tmp; break;
  1135. case 20: env->mach = tmp; break;
  1136. case 21: env->macl = tmp; break;
  1137. case 22: env->sr = tmp; break;
  1138. case 23: env->fpul = tmp; break;
  1139. case 24: env->fpscr = tmp; break;
  1140. case 41: env->ssr = tmp; break;
  1141. case 42: env->spc = tmp; break;
  1142. default: return 0;
  1143. }
  1144. return 4;
  1145. }
  1146. #elif defined (TARGET_MICROBLAZE)
  1147. #define NUM_CORE_REGS (32 + 5)
  1148. static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
  1149. {
  1150. if (n < 32) {
  1151. GET_REG32(env->regs[n]);
  1152. } else {
  1153. GET_REG32(env->sregs[n - 32]);
  1154. }
  1155. return 0;
  1156. }
  1157. static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
  1158. {
  1159. uint32_t tmp;
  1160. if (n > NUM_CORE_REGS)
  1161. return 0;
  1162. tmp = ldl_p(mem_buf);
  1163. if (n < 32) {
  1164. env->regs[n] = tmp;
  1165. } else {
  1166. env->sregs[n - 32] = tmp;
  1167. }
  1168. return 4;
  1169. }
  1170. #elif defined (TARGET_CRIS)
  1171. #define NUM_CORE_REGS 49
  1172. static int
  1173. read_register_crisv10(CPUState *env, uint8_t *mem_buf, int n)
  1174. {
  1175. if (n < 15) {
  1176. GET_REG32(env->regs[n]);
  1177. }
  1178. if (n == 15) {
  1179. GET_REG32(env->pc);
  1180. }
  1181. if (n < 32) {
  1182. switch (n) {
  1183. case 16:
  1184. GET_REG8(env->pregs[n - 16]);
  1185. break;
  1186. case 17:
  1187. GET_REG8(env->pregs[n - 16]);
  1188. break;
  1189. case 20:
  1190. case 21:
  1191. GET_REG16(env->pregs[n - 16]);
  1192. break;
  1193. default:
  1194. if (n >= 23) {
  1195. GET_REG32(env->pregs[n - 16]);
  1196. }
  1197. break;
  1198. }
  1199. }
  1200. return 0;
  1201. }
  1202. static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
  1203. {
  1204. uint8_t srs;
  1205. if (env->pregs[PR_VR] < 32)
  1206. return read_register_crisv10(env, mem_buf, n);
  1207. srs = env->pregs[PR_SRS];
  1208. if (n < 16) {
  1209. GET_REG32(env->regs[n]);
  1210. }
  1211. if (n >= 21 && n < 32) {
  1212. GET_REG32(env->pregs[n - 16]);
  1213. }
  1214. if (n >= 33 && n < 49) {
  1215. GET_REG32(env->sregs[srs][n - 33]);
  1216. }
  1217. switch (n) {
  1218. case 16: GET_REG8(env->pregs[0]);
  1219. case 17: GET_REG8(env->pregs[1]);
  1220. case 18: GET_REG32(env->pregs[2]);
  1221. case 19: GET_REG8(srs);
  1222. case 20: GET_REG16(env->pregs[4]);
  1223. case 32: GET_REG32(env->pc);
  1224. }
  1225. return 0;
  1226. }
  1227. static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
  1228. {
  1229. uint32_t tmp;
  1230. if (n > 49)
  1231. return 0;
  1232. tmp = ldl_p(mem_buf);
  1233. if (n < 16) {
  1234. env->regs[n] = tmp;
  1235. }
  1236. if (n >= 21 && n < 32) {
  1237. env->pregs[n - 16] = tmp;
  1238. }
  1239. /* FIXME: Should support function regs be writable? */
  1240. switch (n) {
  1241. case 16: return 1;
  1242. case 17: return 1;
  1243. case 18: env->pregs[PR_PID] = tmp; break;
  1244. case 19: return 1;
  1245. case 20: return 2;
  1246. case 32: env->pc = tmp; break;
  1247. }
  1248. return 4;
  1249. }
  1250. #elif defined (TARGET_ALPHA)
  1251. #define NUM_CORE_REGS 67
  1252. static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
  1253. {
  1254. uint64_t val;
  1255. CPU_DoubleU d;
  1256. switch (n) {
  1257. case 0 ... 30:
  1258. val = env->ir[n];
  1259. break;
  1260. case 32 ... 62:
  1261. d.d = env->fir[n - 32];
  1262. val = d.ll;
  1263. break;
  1264. case 63:
  1265. val = cpu_alpha_load_fpcr(env);
  1266. break;
  1267. case 64:
  1268. val = env->pc;
  1269. break;
  1270. case 66:
  1271. val = env->unique;
  1272. break;
  1273. case 31:
  1274. case 65:
  1275. /* 31 really is the zero register; 65 is unassigned in the
  1276. gdb protocol, but is still required to occupy 8 bytes. */
  1277. val = 0;
  1278. break;
  1279. default:
  1280. return 0;
  1281. }
  1282. GET_REGL(val);
  1283. }
  1284. static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
  1285. {
  1286. target_ulong tmp = ldtul_p(mem_buf);
  1287. CPU_DoubleU d;
  1288. switch (n) {
  1289. case 0 ... 30:
  1290. env->ir[n] = tmp;
  1291. break;
  1292. case 32 ... 62:
  1293. d.ll = tmp;
  1294. env->fir[n - 32] = d.d;
  1295. break;
  1296. case 63:
  1297. cpu_alpha_store_fpcr(env, tmp);
  1298. break;
  1299. case 64:
  1300. env->pc = tmp;
  1301. break;
  1302. case 66:
  1303. env->unique = tmp;
  1304. break;
  1305. case 31:
  1306. case 65:
  1307. /* 31 really is the zero register; 65 is unassigned in the
  1308. gdb protocol, but is still required to occupy 8 bytes. */
  1309. break;
  1310. default:
  1311. return 0;
  1312. }
  1313. return 8;
  1314. }
  1315. #elif defined (TARGET_S390X)
  1316. #define NUM_CORE_REGS S390_NUM_TOTAL_REGS
  1317. static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
  1318. {
  1319. switch (n) {
  1320. case S390_PSWM_REGNUM: GET_REGL(env->psw.mask); break;
  1321. case S390_PSWA_REGNUM: GET_REGL(env->psw.addr); break;
  1322. case S390_R0_REGNUM ... S390_R15_REGNUM:
  1323. GET_REGL(env->regs[n-S390_R0_REGNUM]); break;
  1324. case S390_A0_REGNUM ... S390_A15_REGNUM:
  1325. GET_REG32(env->aregs[n-S390_A0_REGNUM]); break;
  1326. case S390_FPC_REGNUM: GET_REG32(env->fpc); break;
  1327. case S390_F0_REGNUM ... S390_F15_REGNUM:
  1328. /* XXX */
  1329. break;
  1330. case S390_PC_REGNUM: GET_REGL(env->psw.addr); break;
  1331. case S390_CC_REGNUM:
  1332. env->cc_op = calc_cc(env, env->cc_op, env->cc_src, env->cc_dst,
  1333. env->cc_vr);
  1334. GET_REG32(env->cc_op);
  1335. break;
  1336. }
  1337. return 0;
  1338. }
  1339. static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
  1340. {
  1341. target_ulong tmpl;
  1342. uint32_t tmp32;
  1343. int r = 8;
  1344. tmpl = ldtul_p(mem_buf);
  1345. tmp32 = ldl_p(mem_buf);
  1346. switch (n) {
  1347. case S390_PSWM_REGNUM: env->psw.mask = tmpl; break;
  1348. case S390_PSWA_REGNUM: env->psw.addr = tmpl; break;
  1349. case S390_R0_REGNUM ... S390_R15_REGNUM:
  1350. env->regs[n-S390_R0_REGNUM] = tmpl; break;
  1351. case S390_A0_REGNUM ... S390_A15_REGNUM:
  1352. env->aregs[n-S390_A0_REGNUM] = tmp32; r=4; break;
  1353. case S390_FPC_REGNUM: env->fpc = tmp32; r=4; break;
  1354. case S390_F0_REGNUM ... S390_F15_REGNUM:
  1355. /* XXX */
  1356. break;
  1357. case S390_PC_REGNUM: env->psw.addr = tmpl; break;
  1358. case S390_CC_REGNUM: env->cc_op = tmp32; r=4; break;
  1359. }
  1360. return r;
  1361. }
  1362. #elif defined (TARGET_LM32)
  1363. #include "hw/lm32_pic.h"
  1364. #define NUM_CORE_REGS (32 + 7)
  1365. static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
  1366. {
  1367. if (n < 32) {
  1368. GET_REG32(env->regs[n]);
  1369. } else {
  1370. switch (n) {
  1371. case 32:
  1372. GET_REG32(env->pc);
  1373. break;
  1374. /* FIXME: put in right exception ID */
  1375. case 33:
  1376. GET_REG32(0);
  1377. break;
  1378. case 34:
  1379. GET_REG32(env->eba);
  1380. break;
  1381. case 35:
  1382. GET_REG32(env->deba);
  1383. break;
  1384. case 36:
  1385. GET_REG32(env->ie);
  1386. break;
  1387. case 37:
  1388. GET_REG32(lm32_pic_get_im(env->pic_state));
  1389. break;
  1390. case 38:
  1391. GET_REG32(lm32_pic_get_ip(env->pic_state));
  1392. break;
  1393. }
  1394. }
  1395. return 0;
  1396. }
  1397. static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
  1398. {
  1399. uint32_t tmp;
  1400. if (n > NUM_CORE_REGS) {
  1401. return 0;
  1402. }
  1403. tmp = ldl_p(mem_buf);
  1404. if (n < 32) {
  1405. env->regs[n] = tmp;
  1406. } else {
  1407. switch (n) {
  1408. case 32:
  1409. env->pc = tmp;
  1410. break;
  1411. case 34:
  1412. env->eba = tmp;
  1413. break;
  1414. case 35:
  1415. env->deba = tmp;
  1416. break;
  1417. case 36:
  1418. env->ie = tmp;
  1419. break;
  1420. case 37:
  1421. lm32_pic_set_im(env->pic_state, tmp);
  1422. break;
  1423. case 38:
  1424. lm32_pic_set_ip(env->pic_state, tmp);
  1425. break;
  1426. }
  1427. }
  1428. return 4;
  1429. }
  1430. #elif defined(TARGET_XTENSA)
  1431. /* Use num_core_regs to see only non-privileged registers in an unmodified gdb.
  1432. * Use num_regs to see all registers. gdb modification is required for that:
  1433. * reset bit 0 in the 'flags' field of the registers definitions in the
  1434. * gdb/xtensa-config.c inside gdb source tree or inside gdb overlay.
  1435. */
  1436. #define NUM_CORE_REGS (env->config->gdb_regmap.num_regs)
  1437. #define num_g_regs NUM_CORE_REGS
  1438. static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
  1439. {
  1440. const XtensaGdbReg *reg = env->config->gdb_regmap.reg + n;
  1441. if (n < 0 || n >= env->config->gdb_regmap.num_regs) {
  1442. return 0;
  1443. }
  1444. switch (reg->type) {
  1445. case 9: /*pc*/
  1446. GET_REG32(env->pc);
  1447. break;
  1448. case 1: /*ar*/
  1449. xtensa_sync_phys_from_window(env);
  1450. GET_REG32(env->phys_regs[(reg->targno & 0xff) % env->config->nareg]);
  1451. break;
  1452. case 2: /*SR*/
  1453. GET_REG32(env->sregs[reg->targno & 0xff]);
  1454. break;
  1455. case 3: /*UR*/
  1456. GET_REG32(env->uregs[reg->targno & 0xff]);
  1457. break;
  1458. case 8: /*a*/
  1459. GET_REG32(env->regs[reg->targno & 0x0f]);
  1460. break;
  1461. default:
  1462. qemu_log("%s from reg %d of unsupported type %d\n",
  1463. __func__, n, reg->type);
  1464. return 0;
  1465. }
  1466. }
  1467. static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
  1468. {
  1469. uint32_t tmp;
  1470. const XtensaGdbReg *reg = env->config->gdb_regmap.reg + n;
  1471. if (n < 0 || n >= env->config->gdb_regmap.num_regs) {
  1472. return 0;
  1473. }
  1474. tmp = ldl_p(mem_buf);
  1475. switch (reg->type) {
  1476. case 9: /*pc*/
  1477. env->pc = tmp;
  1478. break;
  1479. case 1: /*ar*/
  1480. env->phys_regs[(reg->targno & 0xff) % env->config->nareg] = tmp;
  1481. xtensa_sync_window_from_phys(env);
  1482. break;
  1483. case 2: /*SR*/
  1484. env->sregs[reg->targno & 0xff] = tmp;
  1485. break;
  1486. case 3: /*UR*/
  1487. env->uregs[reg->targno & 0xff] = tmp;
  1488. break;
  1489. case 8: /*a*/
  1490. env->regs[reg->targno & 0x0f] = tmp;
  1491. break;
  1492. default:
  1493. qemu_log("%s to reg %d of unsupported type %d\n",
  1494. __func__, n, reg->type);
  1495. return 0;
  1496. }
  1497. return 4;
  1498. }
  1499. #else
  1500. #define NUM_CORE_REGS 0
  1501. static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
  1502. {
  1503. return 0;
  1504. }
  1505. static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
  1506. {
  1507. return 0;
  1508. }
  1509. #endif
  1510. #if !defined(TARGET_XTENSA)
  1511. static int num_g_regs = NUM_CORE_REGS;
  1512. #endif
  1513. #ifdef GDB_CORE_XML
  1514. /* Encode data using the encoding for 'x' packets. */
  1515. static int memtox(char *buf, const char *mem, int len)
  1516. {
  1517. char *p = buf;
  1518. char c;
  1519. while (len--) {
  1520. c = *(mem++);
  1521. switch (c) {
  1522. case '#': case '$': case '*': case '}':
  1523. *(p++) = '}';
  1524. *(p++) = c ^ 0x20;
  1525. break;
  1526. default:
  1527. *(p++) = c;
  1528. break;
  1529. }
  1530. }
  1531. return p - buf;
  1532. }
  1533. static const char *get_feature_xml(const char *p, const char **newp)
  1534. {
  1535. size_t len;
  1536. int i;
  1537. const char *name;
  1538. static char target_xml[1024];
  1539. len = 0;
  1540. while (p[len] && p[len] != ':')
  1541. len++;
  1542. *newp = p + len;
  1543. name = NULL;
  1544. if (strncmp(p, "target.xml", len) == 0) {
  1545. /* Generate the XML description for this CPU. */
  1546. if (!target_xml[0]) {
  1547. GDBRegisterState *r;
  1548. snprintf(target_xml, sizeof(target_xml),
  1549. "<?xml version=\"1.0\"?>"
  1550. "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
  1551. "<target>"
  1552. "<xi:include href=\"%s\"/>",
  1553. GDB_CORE_XML);
  1554. for (r = first_cpu->gdb_regs; r; r = r->next) {
  1555. pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
  1556. pstrcat(target_xml, sizeof(target_xml), r->xml);
  1557. pstrcat(target_xml, sizeof(target_xml), "\"/>");
  1558. }
  1559. pstrcat(target_xml, sizeof(target_xml), "</target>");
  1560. }
  1561. return target_xml;
  1562. }
  1563. for (i = 0; ; i++) {
  1564. name = xml_builtin[i][0];
  1565. if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
  1566. break;
  1567. }
  1568. return name ? xml_builtin[i][1] : NULL;
  1569. }
  1570. #endif
  1571. static int gdb_read_register(CPUState *env, uint8_t *mem_buf, int reg)
  1572. {
  1573. GDBRegisterState *r;
  1574. if (reg < NUM_CORE_REGS)
  1575. return cpu_gdb_read_register(env, mem_buf, reg);
  1576. for (r = env->gdb_regs; r; r = r->next) {
  1577. if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
  1578. return r->get_reg(env, mem_buf, reg - r->base_reg);
  1579. }
  1580. }
  1581. return 0;
  1582. }
  1583. static int gdb_write_register(CPUState *env, uint8_t *mem_buf, int reg)
  1584. {
  1585. GDBRegisterState *r;
  1586. if (reg < NUM_CORE_REGS)
  1587. return cpu_gdb_write_register(env, mem_buf, reg);
  1588. for (r = env->gdb_regs; r; r = r->next) {
  1589. if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
  1590. return r->set_reg(env, mem_buf, reg - r->base_reg);
  1591. }
  1592. }
  1593. return 0;
  1594. }
  1595. #if !defined(TARGET_XTENSA)
  1596. /* Register a supplemental set of CPU registers. If g_pos is nonzero it
  1597. specifies the first register number and these registers are included in
  1598. a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
  1599. gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
  1600. */
  1601. void gdb_register_coprocessor(CPUState * env,
  1602. gdb_reg_cb get_reg, gdb_reg_cb set_reg,
  1603. int num_regs, const char *xml, int g_pos)
  1604. {
  1605. GDBRegisterState *s;
  1606. GDBRegisterState **p;
  1607. static int last_reg = NUM_CORE_REGS;
  1608. p = &env->gdb_regs;
  1609. while (*p) {
  1610. /* Check for duplicates. */
  1611. if (strcmp((*p)->xml, xml) == 0)
  1612. return;
  1613. p = &(*p)->next;
  1614. }
  1615. s = g_new0(GDBRegisterState, 1);
  1616. s->base_reg = last_reg;
  1617. s->num_regs = num_regs;
  1618. s->get_reg = get_reg;
  1619. s->set_reg = set_reg;
  1620. s->xml = xml;
  1621. /* Add to end of list. */
  1622. last_reg += num_regs;
  1623. *p = s;
  1624. if (g_pos) {
  1625. if (g_pos != s->base_reg) {
  1626. fprintf(stderr, "Error: Bad gdb register numbering for '%s'\n"
  1627. "Expected %d got %d\n", xml, g_pos, s->base_reg);
  1628. } else {
  1629. num_g_regs = last_reg;
  1630. }
  1631. }
  1632. }
  1633. #endif
  1634. #ifndef CONFIG_USER_ONLY
  1635. static const int xlat_gdb_type[] = {
  1636. [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
  1637. [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
  1638. [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
  1639. };
  1640. #endif
  1641. static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type)
  1642. {
  1643. CPUState *env;
  1644. int err = 0;
  1645. if (kvm_enabled())
  1646. return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
  1647. switch (type) {
  1648. case GDB_BREAKPOINT_SW:
  1649. case GDB_BREAKPOINT_HW:
  1650. for (env = first_cpu; env != NULL; env = env->next_cpu) {
  1651. err = cpu_breakpoint_insert(env, addr, BP_GDB, NULL);
  1652. if (err)
  1653. break;
  1654. }
  1655. return err;
  1656. #ifndef CONFIG_USER_ONLY
  1657. case GDB_WATCHPOINT_WRITE:
  1658. case GDB_WATCHPOINT_READ:
  1659. case GDB_WATCHPOINT_ACCESS:
  1660. for (env = first_cpu; env != NULL; env = env->next_cpu) {
  1661. err = cpu_watchpoint_insert(env, addr, len, xlat_gdb_type[type],
  1662. NULL);
  1663. if (err)
  1664. break;
  1665. }
  1666. return err;
  1667. #endif
  1668. default:
  1669. return -ENOSYS;
  1670. }
  1671. }
  1672. static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type)
  1673. {
  1674. CPUState *env;
  1675. int err = 0;
  1676. if (kvm_enabled())
  1677. return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
  1678. switch (type) {
  1679. case GDB_BREAKPOINT_SW:
  1680. case GDB_BREAKPOINT_HW:
  1681. for (env = first_cpu; env != NULL; env = env->next_cpu) {
  1682. err = cpu_breakpoint_remove(env, addr, BP_GDB);
  1683. if (err)
  1684. break;
  1685. }
  1686. return err;
  1687. #ifndef CONFIG_USER_ONLY
  1688. case GDB_WATCHPOINT_WRITE:
  1689. case GDB_WATCHPOINT_READ:
  1690. case GDB_WATCHPOINT_ACCESS:
  1691. for (env = first_cpu; env != NULL; env = env->next_cpu) {
  1692. err = cpu_watchpoint_remove(env, addr, len, xlat_gdb_type[type]);
  1693. if (err)
  1694. break;
  1695. }
  1696. return err;
  1697. #endif
  1698. default:
  1699. return -ENOSYS;
  1700. }
  1701. }
  1702. static void gdb_breakpoint_remove_all(void)
  1703. {
  1704. CPUState *env;
  1705. if (kvm_enabled()) {
  1706. kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
  1707. return;
  1708. }
  1709. for (env = first_cpu; env != NULL; env = env->next_cpu) {
  1710. cpu_breakpoint_remove_all(env, BP_GDB);
  1711. #ifndef CONFIG_USER_ONLY
  1712. cpu_watchpoint_remove_all(env, BP_GDB);
  1713. #endif
  1714. }
  1715. }
  1716. static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
  1717. {
  1718. #if defined(TARGET_I386)
  1719. cpu_synchronize_state(s->c_cpu);
  1720. s->c_cpu->eip = pc;
  1721. #elif defined (TARGET_PPC)
  1722. s->c_cpu->nip = pc;
  1723. #elif defined (TARGET_SPARC)
  1724. s->c_cpu->pc = pc;
  1725. s->c_cpu->npc = pc + 4;
  1726. #elif defined (TARGET_ARM)
  1727. s->c_cpu->regs[15] = pc;
  1728. #elif defined (TARGET_SH4)
  1729. s->c_cpu->pc = pc;
  1730. #elif defined (TARGET_MIPS)
  1731. s->c_cpu->active_tc.PC = pc & ~(target_ulong)1;
  1732. if (pc & 1) {
  1733. s->c_cpu->hflags |= MIPS_HFLAG_M16;
  1734. } else {
  1735. s->c_cpu->hflags &= ~(MIPS_HFLAG_M16);
  1736. }
  1737. #elif defined (TARGET_MICROBLAZE)
  1738. s->c_cpu->sregs[SR_PC] = pc;
  1739. #elif defined (TARGET_CRIS)
  1740. s->c_cpu->pc = pc;
  1741. #elif defined (TARGET_ALPHA)
  1742. s->c_cpu->pc = pc;
  1743. #elif defined (TARGET_S390X)
  1744. cpu_synchronize_state(s->c_cpu);
  1745. s->c_cpu->psw.addr = pc;
  1746. #elif defined (TARGET_LM32)
  1747. s->c_cpu->pc = pc;
  1748. #elif defined(TARGET_XTENSA)
  1749. s->c_cpu->pc = pc;
  1750. #endif
  1751. }
  1752. static inline int gdb_id(CPUState *env)
  1753. {
  1754. #if defined(CONFIG_USER_ONLY) && defined(CONFIG_USE_NPTL)
  1755. return env->host_tid;
  1756. #else
  1757. return env->cpu_index + 1;
  1758. #endif
  1759. }
  1760. static CPUState *find_cpu(uint32_t thread_id)
  1761. {
  1762. CPUState *env;
  1763. for (env = first_cpu; env != NULL; env = env->next_cpu) {
  1764. if (gdb_id(env) == thread_id) {
  1765. return env;
  1766. }
  1767. }
  1768. return NULL;
  1769. }
  1770. static int gdb_handle_packet(GDBState *s, const char *line_buf)
  1771. {
  1772. CPUState *env;
  1773. const char *p;
  1774. uint32_t thread;
  1775. int ch, reg_size, type, res;
  1776. char buf[MAX_PACKET_LENGTH];
  1777. uint8_t mem_buf[MAX_PACKET_LENGTH];
  1778. uint8_t *registers;
  1779. target_ulong addr, len;
  1780. #ifdef DEBUG_GDB
  1781. printf("command='%s'\n", line_buf);
  1782. #endif
  1783. p = line_buf;
  1784. ch = *p++;
  1785. switch(ch) {
  1786. case '?':
  1787. /* TODO: Make this return the correct value for user-mode. */
  1788. snprintf(buf, sizeof(buf), "T%02xthread:%02x;", GDB_SIGNAL_TRAP,
  1789. gdb_id(s->c_cpu));
  1790. put_packet(s, buf);
  1791. /* Remove all the breakpoints when this query is issued,
  1792. * because gdb is doing and initial connect and the state
  1793. * should be cleaned up.
  1794. */
  1795. gdb_breakpoint_remove_all();
  1796. break;
  1797. case 'c':
  1798. if (*p != '\0') {
  1799. addr = strtoull(p, (char **)&p, 16);
  1800. gdb_set_cpu_pc(s, addr);
  1801. }
  1802. s->signal = 0;
  1803. gdb_continue(s);
  1804. return RS_IDLE;
  1805. case 'C':
  1806. s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16));
  1807. if (s->signal == -1)
  1808. s->signal = 0;
  1809. gdb_continue(s);
  1810. return RS_IDLE;
  1811. case 'v':
  1812. if (strncmp(p, "Cont", 4) == 0) {
  1813. int res_signal, res_thread;
  1814. p += 4;
  1815. if (*p == '?') {
  1816. put_packet(s, "vCont;c;C;s;S");
  1817. break;
  1818. }
  1819. res = 0;
  1820. res_signal = 0;
  1821. res_thread = 0;
  1822. while (*p) {
  1823. int action, signal;
  1824. if (*p++ != ';') {
  1825. res = 0;
  1826. break;
  1827. }
  1828. action = *p++;
  1829. signal = 0;
  1830. if (action == 'C' || action == 'S') {
  1831. signal = strtoul(p, (char **)&p, 16);
  1832. } else if (action != 'c' && action != 's') {
  1833. res = 0;
  1834. break;
  1835. }
  1836. thread = 0;
  1837. if (*p == ':') {
  1838. thread = strtoull(p+1, (char **)&p, 16);
  1839. }
  1840. action = tolower(action);
  1841. if (res == 0 || (res == 'c' && action == 's')) {
  1842. res = action;
  1843. res_signal = signal;
  1844. res_thread = thread;
  1845. }
  1846. }
  1847. if (res) {
  1848. if (res_thread != -1 && res_thread != 0) {
  1849. env = find_cpu(res_thread);
  1850. if (env == NULL) {
  1851. put_packet(s, "E22");
  1852. break;
  1853. }
  1854. s->c_cpu = env;
  1855. }
  1856. if (res == 's') {
  1857. cpu_single_step(s->c_cpu, sstep_flags);
  1858. }
  1859. s->signal = res_signal;
  1860. gdb_continue(s);
  1861. return RS_IDLE;
  1862. }
  1863. break;
  1864. } else {
  1865. goto unknown_command;
  1866. }
  1867. case 'k':
  1868. /* Kill the target */
  1869. fprintf(stderr, "\nQEMU: Terminated via GDBstub\n");
  1870. exit(0);
  1871. case 'D':
  1872. /* Detach packet */
  1873. gdb_breakpoint_remove_all();
  1874. gdb_syscall_mode = GDB_SYS_DISABLED;
  1875. gdb_continue(s);
  1876. put_packet(s, "OK");
  1877. break;
  1878. case 's':
  1879. if (*p != '\0') {
  1880. addr = strtoull(p, (char **)&p, 16);
  1881. gdb_set_cpu_pc(s, addr);
  1882. }
  1883. cpu_single_step(s->c_cpu, sstep_flags);
  1884. gdb_continue(s);
  1885. return RS_IDLE;
  1886. case 'F':
  1887. {
  1888. target_ulong ret;
  1889. target_ulong err;
  1890. ret = strtoull(p, (char **)&p, 16);
  1891. if (*p == ',') {
  1892. p++;
  1893. err = strtoull(p, (char **)&p, 16);
  1894. } else {
  1895. err = 0;
  1896. }
  1897. if (*p == ',')
  1898. p++;
  1899. type = *p;
  1900. if (gdb_current_syscall_cb)
  1901. gdb_current_syscall_cb(s->c_cpu, ret, err);
  1902. if (type == 'C') {
  1903. put_packet(s, "T02");
  1904. } else {
  1905. gdb_continue(s);
  1906. }
  1907. }
  1908. break;
  1909. case 'g':
  1910. cpu_synchronize_state(s->g_cpu);
  1911. env = s->g_cpu;
  1912. len = 0;
  1913. for (addr = 0; addr < num_g_regs; addr++) {
  1914. reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr);
  1915. len += reg_size;
  1916. }
  1917. memtohex(buf, mem_buf, len);
  1918. put_packet(s, buf);
  1919. break;
  1920. case 'G':
  1921. cpu_synchronize_state(s->g_cpu);
  1922. env = s->g_cpu;
  1923. registers = mem_buf;
  1924. len = strlen(p) / 2;
  1925. hextomem((uint8_t *)registers, p, len);
  1926. for (addr = 0; addr < num_g_regs && len > 0; addr++) {
  1927. reg_size = gdb_write_register(s->g_cpu, registers, addr);
  1928. len -= reg_size;
  1929. registers += reg_size;
  1930. }
  1931. put_packet(s, "OK");
  1932. break;
  1933. case 'm':
  1934. addr = strtoull(p, (char **)&p, 16);
  1935. if (*p == ',')
  1936. p++;
  1937. len = strtoull(p, NULL, 16);
  1938. if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, 0) != 0) {
  1939. put_packet (s, "E14");
  1940. } else {
  1941. memtohex(buf, mem_buf, len);
  1942. put_packet(s, buf);
  1943. }
  1944. break;
  1945. case 'M':
  1946. addr = strtoull(p, (char **)&p, 16);
  1947. if (*p == ',')
  1948. p++;
  1949. len = strtoull(p, (char **)&p, 16);
  1950. if (*p == ':')
  1951. p++;
  1952. hextomem(mem_buf, p, len);
  1953. if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, 1) != 0) {
  1954. put_packet(s, "E14");
  1955. } else {
  1956. put_packet(s, "OK");
  1957. }
  1958. break;
  1959. case 'p':
  1960. /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
  1961. This works, but can be very slow. Anything new enough to
  1962. understand XML also knows how to use this properly. */
  1963. if (!gdb_has_xml)
  1964. goto unknown_command;
  1965. addr = strtoull(p, (char **)&p, 16);
  1966. reg_size = gdb_read_register(s->g_cpu, mem_buf, addr);
  1967. if (reg_size) {
  1968. memtohex(buf, mem_buf, reg_size);
  1969. put_packet(s, buf);
  1970. } else {
  1971. put_packet(s, "E14");
  1972. }
  1973. break;
  1974. case 'P':
  1975. if (!gdb_has_xml)
  1976. goto unknown_command;
  1977. addr = strtoull(p, (char **)&p, 16);
  1978. if (*p == '=')
  1979. p++;
  1980. reg_size = strlen(p) / 2;
  1981. hextomem(mem_buf, p, reg_size);
  1982. gdb_write_register(s->g_cpu, mem_buf, addr);
  1983. put_packet(s, "OK");
  1984. break;
  1985. case 'Z':
  1986. case 'z':
  1987. type = strtoul(p, (char **)&p, 16);
  1988. if (*p == ',')
  1989. p++;
  1990. addr = strtoull(p, (char **)&p, 16);
  1991. if (*p == ',')
  1992. p++;
  1993. len = strtoull(p, (char **)&p, 16);
  1994. if (ch == 'Z')
  1995. res = gdb_breakpoint_insert(addr, len, type);
  1996. else
  1997. res = gdb_breakpoint_remove(addr, len, type);
  1998. if (res >= 0)
  1999. put_packet(s, "OK");
  2000. else if (res == -ENOSYS)
  2001. put_packet(s, "");
  2002. else
  2003. put_packet(s, "E22");
  2004. break;
  2005. case 'H':
  2006. type = *p++;
  2007. thread = strtoull(p, (char **)&p, 16);
  2008. if (thread == -1 || thread == 0) {
  2009. put_packet(s, "OK");
  2010. break;
  2011. }
  2012. env = find_cpu(thread);
  2013. if (env == NULL) {
  2014. put_packet(s, "E22");
  2015. break;
  2016. }
  2017. switch (type) {
  2018. case 'c':
  2019. s->c_cpu = env;
  2020. put_packet(s, "OK");
  2021. break;
  2022. case 'g':
  2023. s->g_cpu = env;
  2024. put_packet(s, "OK");
  2025. break;
  2026. default:
  2027. put_packet(s, "E22");
  2028. break;
  2029. }
  2030. break;
  2031. case 'T':
  2032. thread = strtoull(p, (char **)&p, 16);
  2033. env = find_cpu(thread);
  2034. if (env != NULL) {
  2035. put_packet(s, "OK");
  2036. } else {
  2037. put_packet(s, "E22");
  2038. }
  2039. break;
  2040. case 'q':
  2041. case 'Q':
  2042. /* parse any 'q' packets here */
  2043. if (!strcmp(p,"qemu.sstepbits")) {
  2044. /* Query Breakpoint bit definitions */
  2045. snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
  2046. SSTEP_ENABLE,
  2047. SSTEP_NOIRQ,
  2048. SSTEP_NOTIMER);
  2049. put_packet(s, buf);
  2050. break;
  2051. } else if (strncmp(p,"qemu.sstep",10) == 0) {
  2052. /* Display or change the sstep_flags */
  2053. p += 10;
  2054. if (*p != '=') {
  2055. /* Display current setting */
  2056. snprintf(buf, sizeof(buf), "0x%x", sstep_flags);
  2057. put_packet(s, buf);
  2058. break;
  2059. }
  2060. p++;
  2061. type = strtoul(p, (char **)&p, 16);
  2062. sstep_flags = type;
  2063. put_packet(s, "OK");
  2064. break;
  2065. } else if (strcmp(p,"C") == 0) {
  2066. /* "Current thread" remains vague in the spec, so always return
  2067. * the first CPU (gdb returns the first thread). */
  2068. put_packet(s, "QC1");
  2069. break;
  2070. } else if (strcmp(p,"fThreadInfo") == 0) {
  2071. s->query_cpu = first_cpu;
  2072. goto report_cpuinfo;
  2073. } else if (strcmp(p,"sThreadInfo") == 0) {
  2074. report_cpuinfo:
  2075. if (s->query_cpu) {
  2076. snprintf(buf, sizeof(buf), "m%x", gdb_id(s->query_cpu));
  2077. put_packet(s, buf);
  2078. s->query_cpu = s->query_cpu->next_cpu;
  2079. } else
  2080. put_packet(s, "l");
  2081. break;
  2082. } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) {
  2083. thread = strtoull(p+16, (char **)&p, 16);
  2084. env = find_cpu(thread);
  2085. if (env != NULL) {
  2086. cpu_synchronize_state(env);
  2087. len = snprintf((char *)mem_buf, sizeof(mem_buf),
  2088. "CPU#%d [%s]", env->cpu_index,
  2089. env->halted ? "halted " : "running");
  2090. memtohex(buf, mem_buf, len);
  2091. put_packet(s, buf);
  2092. }
  2093. break;
  2094. }
  2095. #ifdef CONFIG_USER_ONLY
  2096. else if (strncmp(p, "Offsets", 7) == 0) {
  2097. TaskState *ts = s->c_cpu->opaque;
  2098. snprintf(buf, sizeof(buf),
  2099. "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
  2100. ";Bss=" TARGET_ABI_FMT_lx,
  2101. ts->info->code_offset,
  2102. ts->info->data_offset,
  2103. ts->info->data_offset);
  2104. put_packet(s, buf);
  2105. break;
  2106. }
  2107. #else /* !CONFIG_USER_ONLY */
  2108. else if (strncmp(p, "Rcmd,", 5) == 0) {
  2109. int len = strlen(p + 5);
  2110. if ((len % 2) != 0) {
  2111. put_packet(s, "E01");
  2112. break;
  2113. }
  2114. hextomem(mem_buf, p + 5, len);
  2115. len = len / 2;
  2116. mem_buf[len++] = 0;
  2117. qemu_chr_be_write(s->mon_chr, mem_buf, len);
  2118. put_packet(s, "OK");
  2119. break;
  2120. }
  2121. #endif /* !CONFIG_USER_ONLY */
  2122. if (strncmp(p, "Supported", 9) == 0) {
  2123. snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH);
  2124. #ifdef GDB_CORE_XML
  2125. pstrcat(buf, sizeof(buf), ";qXfer:features:read+");
  2126. #endif
  2127. put_packet(s, buf);
  2128. break;
  2129. }
  2130. #ifdef GDB_CORE_XML
  2131. if (strncmp(p, "Xfer:features:read:", 19) == 0) {
  2132. const char *xml;
  2133. target_ulong total_len;
  2134. gdb_has_xml = 1;
  2135. p += 19;
  2136. xml = get_feature_xml(p, &p);
  2137. if (!xml) {
  2138. snprintf(buf, sizeof(buf), "E00");
  2139. put_packet(s, buf);
  2140. break;
  2141. }
  2142. if (*p == ':')
  2143. p++;
  2144. addr = strtoul(p, (char **)&p, 16);
  2145. if (*p == ',')
  2146. p++;
  2147. len = strtoul(p, (char **)&p, 16);
  2148. total_len = strlen(xml);
  2149. if (addr > total_len) {
  2150. snprintf(buf, sizeof(buf), "E00");
  2151. put_packet(s, buf);
  2152. break;
  2153. }
  2154. if (len > (MAX_PACKET_LENGTH - 5) / 2)
  2155. len = (MAX_PACKET_LENGTH - 5) / 2;
  2156. if (len < total_len - addr) {
  2157. buf[0] = 'm';
  2158. len = memtox(buf + 1, xml + addr, len);
  2159. } else {
  2160. buf[0] = 'l';
  2161. len = memtox(buf + 1, xml + addr, total_len - addr);
  2162. }
  2163. put_packet_binary(s, buf, len + 1);
  2164. break;
  2165. }
  2166. #endif
  2167. /* Unrecognised 'q' command. */
  2168. goto unknown_command;
  2169. default:
  2170. unknown_command:
  2171. /* put empty packet */
  2172. buf[0] = '\0';
  2173. put_packet(s, buf);
  2174. break;
  2175. }
  2176. return RS_IDLE;
  2177. }
  2178. void gdb_set_stop_cpu(CPUState *env)
  2179. {
  2180. gdbserver_state->c_cpu = env;
  2181. gdbserver_state->g_cpu = env;
  2182. }
  2183. #ifndef CONFIG_USER_ONLY
  2184. static void gdb_vm_state_change(void *opaque, int running, RunState state)
  2185. {
  2186. GDBState *s = gdbserver_state;
  2187. CPUState *env = s->c_cpu;
  2188. char buf[256];
  2189. const char *type;
  2190. int ret;
  2191. if (running || s->state == RS_INACTIVE || s->state == RS_SYSCALL) {
  2192. return;
  2193. }
  2194. switch (state) {
  2195. case RUN_STATE_DEBUG:
  2196. if (env->watchpoint_hit) {
  2197. switch (env->watchpoint_hit->flags & BP_MEM_ACCESS) {
  2198. case BP_MEM_READ:
  2199. type = "r";
  2200. break;
  2201. case BP_MEM_ACCESS:
  2202. type = "a";
  2203. break;
  2204. default:
  2205. type = "";
  2206. break;
  2207. }
  2208. snprintf(buf, sizeof(buf),
  2209. "T%02xthread:%02x;%swatch:" TARGET_FMT_lx ";",
  2210. GDB_SIGNAL_TRAP, gdb_id(env), type,
  2211. env->watchpoint_hit->vaddr);
  2212. env->watchpoint_hit = NULL;
  2213. goto send_packet;
  2214. }
  2215. tb_flush(env);
  2216. ret = GDB_SIGNAL_TRAP;
  2217. break;
  2218. case RUN_STATE_PAUSED:
  2219. ret = GDB_SIGNAL_INT;
  2220. break;
  2221. case RUN_STATE_SHUTDOWN:
  2222. ret = GDB_SIGNAL_QUIT;
  2223. break;
  2224. case RUN_STATE_IO_ERROR:
  2225. ret = GDB_SIGNAL_IO;
  2226. break;
  2227. case RUN_STATE_WATCHDOG:
  2228. ret = GDB_SIGNAL_ALRM;
  2229. break;
  2230. case RUN_STATE_INTERNAL_ERROR:
  2231. ret = GDB_SIGNAL_ABRT;
  2232. break;
  2233. case RUN_STATE_SAVE_VM:
  2234. case RUN_STATE_RESTORE_VM:
  2235. return;
  2236. case RUN_STATE_FINISH_MIGRATE:
  2237. ret = GDB_SIGNAL_XCPU;
  2238. break;
  2239. default:
  2240. ret = GDB_SIGNAL_UNKNOWN;
  2241. break;
  2242. }
  2243. snprintf(buf, sizeof(buf), "T%02xthread:%02x;", ret, gdb_id(env));
  2244. send_packet:
  2245. put_packet(s, buf);
  2246. /* disable single step if it was enabled */
  2247. cpu_single_step(env, 0);
  2248. }
  2249. #endif
  2250. /* Send a gdb syscall request.
  2251. This accepts limited printf-style format specifiers, specifically:
  2252. %x - target_ulong argument printed in hex.
  2253. %lx - 64-bit argument printed in hex.
  2254. %s - string pointer (target_ulong) and length (int) pair. */
  2255. void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
  2256. {
  2257. va_list va;
  2258. char buf[256];
  2259. char *p;
  2260. target_ulong addr;
  2261. uint64_t i64;
  2262. GDBState *s;
  2263. s = gdbserver_state;
  2264. if (!s)
  2265. return;
  2266. gdb_current_syscall_cb = cb;
  2267. s->state = RS_SYSCALL;
  2268. #ifndef CONFIG_USER_ONLY
  2269. vm_stop(RUN_STATE_DEBUG);
  2270. #endif
  2271. s->state = RS_IDLE;
  2272. va_start(va, fmt);
  2273. p = buf;
  2274. *(p++) = 'F';
  2275. while (*fmt) {
  2276. if (*fmt == '%') {
  2277. fmt++;
  2278. switch (*fmt++) {
  2279. case 'x':
  2280. addr = va_arg(va, target_ulong);
  2281. p += snprintf(p, &buf[sizeof(buf)] - p, TARGET_FMT_lx, addr);
  2282. break;
  2283. case 'l':
  2284. if (*(fmt++) != 'x')
  2285. goto bad_format;
  2286. i64 = va_arg(va, uint64_t);
  2287. p += snprintf(p, &buf[sizeof(buf)] - p, "%" PRIx64, i64);
  2288. break;
  2289. case 's':
  2290. addr = va_arg(va, target_ulong);
  2291. p += snprintf(p, &buf[sizeof(buf)] - p, TARGET_FMT_lx "/%x",
  2292. addr, va_arg(va, int));
  2293. break;
  2294. default:
  2295. bad_format:
  2296. fprintf(stderr, "gdbstub: Bad syscall format string '%s'\n",
  2297. fmt - 1);
  2298. break;
  2299. }
  2300. } else {
  2301. *(p++) = *(fmt++);
  2302. }
  2303. }
  2304. *p = 0;
  2305. va_end(va);
  2306. put_packet(s, buf);
  2307. #ifdef CONFIG_USER_ONLY
  2308. gdb_handlesig(s->c_cpu, 0);
  2309. #else
  2310. cpu_exit(s->c_cpu);
  2311. #endif
  2312. }
  2313. static void gdb_read_byte(GDBState *s, int ch)
  2314. {
  2315. int i, csum;
  2316. uint8_t reply;
  2317. #ifndef CONFIG_USER_ONLY
  2318. if (s->last_packet_len) {
  2319. /* Waiting for a response to the last packet. If we see the start
  2320. of a new command then abandon the previous response. */
  2321. if (ch == '-') {
  2322. #ifdef DEBUG_GDB
  2323. printf("Got NACK, retransmitting\n");
  2324. #endif
  2325. put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
  2326. }
  2327. #ifdef DEBUG_GDB
  2328. else if (ch == '+')
  2329. printf("Got ACK\n");
  2330. else
  2331. printf("Got '%c' when expecting ACK/NACK\n", ch);
  2332. #endif
  2333. if (ch == '+' || ch == '$')
  2334. s->last_packet_len = 0;
  2335. if (ch != '$')
  2336. return;
  2337. }
  2338. if (runstate_is_running()) {
  2339. /* when the CPU is running, we cannot do anything except stop
  2340. it when receiving a char */
  2341. vm_stop(RUN_STATE_PAUSED);
  2342. } else
  2343. #endif
  2344. {
  2345. switch(s->state) {
  2346. case RS_IDLE:
  2347. if (ch == '$') {
  2348. s->line_buf_index = 0;
  2349. s->state = RS_GETLINE;
  2350. }
  2351. break;
  2352. case RS_GETLINE:
  2353. if (ch == '#') {
  2354. s->state = RS_CHKSUM1;
  2355. } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
  2356. s->state = RS_IDLE;
  2357. } else {
  2358. s->line_buf[s->line_buf_index++] = ch;
  2359. }
  2360. break;
  2361. case RS_CHKSUM1:
  2362. s->line_buf[s->line_buf_index] = '\0';
  2363. s->line_csum = fromhex(ch) << 4;
  2364. s->state = RS_CHKSUM2;
  2365. break;
  2366. case RS_CHKSUM2:
  2367. s->line_csum |= fromhex(ch);
  2368. csum = 0;
  2369. for(i = 0; i < s->line_buf_index; i++) {
  2370. csum += s->line_buf[i];
  2371. }
  2372. if (s->line_csum != (csum & 0xff)) {
  2373. reply = '-';
  2374. put_buffer(s, &reply, 1);
  2375. s->state = RS_IDLE;
  2376. } else {
  2377. reply = '+';
  2378. put_buffer(s, &reply, 1);
  2379. s->state = gdb_handle_packet(s, s->line_buf);
  2380. }
  2381. break;
  2382. default:
  2383. abort();
  2384. }
  2385. }
  2386. }
  2387. /* Tell the remote gdb that the process has exited. */
  2388. void gdb_exit(CPUState *env, int code)
  2389. {
  2390. GDBState *s;
  2391. char buf[4];
  2392. s = gdbserver_state;
  2393. if (!s) {
  2394. return;
  2395. }
  2396. #ifdef CONFIG_USER_ONLY
  2397. if (gdbserver_fd < 0 || s->fd < 0) {
  2398. return;
  2399. }
  2400. #endif
  2401. snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
  2402. put_packet(s, buf);
  2403. #ifndef CONFIG_USER_ONLY
  2404. if (s->chr) {
  2405. qemu_chr_delete(s->chr);
  2406. }
  2407. #endif
  2408. }
  2409. #ifdef CONFIG_USER_ONLY
  2410. int
  2411. gdb_queuesig (void)
  2412. {
  2413. GDBState *s;
  2414. s = gdbserver_state;
  2415. if (gdbserver_fd < 0 || s->fd < 0)
  2416. return 0;
  2417. else
  2418. return 1;
  2419. }
  2420. int
  2421. gdb_handlesig (CPUState *env, int sig)
  2422. {
  2423. GDBState *s;
  2424. char buf[256];
  2425. int n;
  2426. s = gdbserver_state;
  2427. if (gdbserver_fd < 0 || s->fd < 0)
  2428. return sig;
  2429. /* disable single step if it was enabled */
  2430. cpu_single_step(env, 0);
  2431. tb_flush(env);
  2432. if (sig != 0)
  2433. {
  2434. snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb (sig));
  2435. put_packet(s, buf);
  2436. }
  2437. /* put_packet() might have detected that the peer terminated the
  2438. connection. */
  2439. if (s->fd < 0)
  2440. return sig;
  2441. sig = 0;
  2442. s->state = RS_IDLE;
  2443. s->running_state = 0;
  2444. while (s->running_state == 0) {
  2445. n = read (s->fd, buf, 256);
  2446. if (n > 0)
  2447. {
  2448. int i;
  2449. for (i = 0; i < n; i++)
  2450. gdb_read_byte (s, buf[i]);
  2451. }
  2452. else if (n == 0 || errno != EAGAIN)
  2453. {
  2454. /* XXX: Connection closed. Should probably wait for annother
  2455. connection before continuing. */
  2456. return sig;
  2457. }
  2458. }
  2459. sig = s->signal;
  2460. s->signal = 0;
  2461. return sig;
  2462. }
  2463. /* Tell the remote gdb that the process has exited due to SIG. */
  2464. void gdb_signalled(CPUState *env, int sig)
  2465. {
  2466. GDBState *s;
  2467. char buf[4];
  2468. s = gdbserver_state;
  2469. if (gdbserver_fd < 0 || s->fd < 0)
  2470. return;
  2471. snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb (sig));
  2472. put_packet(s, buf);
  2473. }
  2474. static void gdb_accept(void)
  2475. {
  2476. GDBState *s;
  2477. struct sockaddr_in sockaddr;
  2478. socklen_t len;
  2479. int val, fd;
  2480. for(;;) {
  2481. len = sizeof(sockaddr);
  2482. fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
  2483. if (fd < 0 && errno != EINTR) {
  2484. perror("accept");
  2485. return;
  2486. } else if (fd >= 0) {
  2487. #ifndef _WIN32
  2488. fcntl(fd, F_SETFD, FD_CLOEXEC);
  2489. #endif
  2490. break;
  2491. }
  2492. }
  2493. /* set short latency */
  2494. val = 1;
  2495. setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
  2496. s = g_malloc0(sizeof(GDBState));
  2497. s->c_cpu = first_cpu;
  2498. s->g_cpu = first_cpu;
  2499. s->fd = fd;
  2500. gdb_has_xml = 0;
  2501. gdbserver_state = s;
  2502. fcntl(fd, F_SETFL, O_NONBLOCK);
  2503. }
  2504. static int gdbserver_open(int port)
  2505. {
  2506. struct sockaddr_in sockaddr;
  2507. int fd, val, ret;
  2508. fd = socket(PF_INET, SOCK_STREAM, 0);
  2509. if (fd < 0) {
  2510. perror("socket");
  2511. return -1;
  2512. }
  2513. #ifndef _WIN32
  2514. fcntl(fd, F_SETFD, FD_CLOEXEC);
  2515. #endif
  2516. /* allow fast reuse */
  2517. val = 1;
  2518. setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val));
  2519. sockaddr.sin_family = AF_INET;
  2520. sockaddr.sin_port = htons(port);
  2521. sockaddr.sin_addr.s_addr = 0;
  2522. ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
  2523. if (ret < 0) {
  2524. perror("bind");
  2525. return -1;
  2526. }
  2527. ret = listen(fd, 0);
  2528. if (ret < 0) {
  2529. perror("listen");
  2530. return -1;
  2531. }
  2532. return fd;
  2533. }
  2534. int gdbserver_start(int port)
  2535. {
  2536. gdbserver_fd = gdbserver_open(port);
  2537. if (gdbserver_fd < 0)
  2538. return -1;
  2539. /* accept connections */
  2540. gdb_accept();
  2541. return 0;
  2542. }
  2543. /* Disable gdb stub for child processes. */
  2544. void gdbserver_fork(CPUState *env)
  2545. {
  2546. GDBState *s = gdbserver_state;
  2547. if (gdbserver_fd < 0 || s->fd < 0)
  2548. return;
  2549. close(s->fd);
  2550. s->fd = -1;
  2551. cpu_breakpoint_remove_all(env, BP_GDB);
  2552. cpu_watchpoint_remove_all(env, BP_GDB);
  2553. }
  2554. #else
  2555. static int gdb_chr_can_receive(void *opaque)
  2556. {
  2557. /* We can handle an arbitrarily large amount of data.
  2558. Pick the maximum packet size, which is as good as anything. */
  2559. return MAX_PACKET_LENGTH;
  2560. }
  2561. static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
  2562. {
  2563. int i;
  2564. for (i = 0; i < size; i++) {
  2565. gdb_read_byte(gdbserver_state, buf[i]);
  2566. }
  2567. }
  2568. static void gdb_chr_event(void *opaque, int event)
  2569. {
  2570. switch (event) {
  2571. case CHR_EVENT_OPENED:
  2572. vm_stop(RUN_STATE_PAUSED);
  2573. gdb_has_xml = 0;
  2574. break;
  2575. default:
  2576. break;
  2577. }
  2578. }
  2579. static void gdb_monitor_output(GDBState *s, const char *msg, int len)
  2580. {
  2581. char buf[MAX_PACKET_LENGTH];
  2582. buf[0] = 'O';
  2583. if (len > (MAX_PACKET_LENGTH/2) - 1)
  2584. len = (MAX_PACKET_LENGTH/2) - 1;
  2585. memtohex(buf + 1, (uint8_t *)msg, len);
  2586. put_packet(s, buf);
  2587. }
  2588. static int gdb_monitor_write(CharDriverState *chr, const uint8_t *buf, int len)
  2589. {
  2590. const char *p = (const char *)buf;
  2591. int max_sz;
  2592. max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
  2593. for (;;) {
  2594. if (len <= max_sz) {
  2595. gdb_monitor_output(gdbserver_state, p, len);
  2596. break;
  2597. }
  2598. gdb_monitor_output(gdbserver_state, p, max_sz);
  2599. p += max_sz;
  2600. len -= max_sz;
  2601. }
  2602. return len;
  2603. }
  2604. #ifndef _WIN32
  2605. static void gdb_sigterm_handler(int signal)
  2606. {
  2607. if (runstate_is_running()) {
  2608. vm_stop(RUN_STATE_PAUSED);
  2609. }
  2610. }
  2611. #endif
  2612. int gdbserver_start(const char *device)
  2613. {
  2614. GDBState *s;
  2615. char gdbstub_device_name[128];
  2616. CharDriverState *chr = NULL;
  2617. CharDriverState *mon_chr;
  2618. if (!device)
  2619. return -1;
  2620. if (strcmp(device, "none") != 0) {
  2621. if (strstart(device, "tcp:", NULL)) {
  2622. /* enforce required TCP attributes */
  2623. snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
  2624. "%s,nowait,nodelay,server", device);
  2625. device = gdbstub_device_name;
  2626. }
  2627. #ifndef _WIN32
  2628. else if (strcmp(device, "stdio") == 0) {
  2629. struct sigaction act;
  2630. memset(&act, 0, sizeof(act));
  2631. act.sa_handler = gdb_sigterm_handler;
  2632. sigaction(SIGINT, &act, NULL);
  2633. }
  2634. #endif
  2635. chr = qemu_chr_new("gdb", device, NULL);
  2636. if (!chr)
  2637. return -1;
  2638. qemu_chr_add_handlers(chr, gdb_chr_can_receive, gdb_chr_receive,
  2639. gdb_chr_event, NULL);
  2640. }
  2641. s = gdbserver_state;
  2642. if (!s) {
  2643. s = g_malloc0(sizeof(GDBState));
  2644. gdbserver_state = s;
  2645. qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
  2646. /* Initialize a monitor terminal for gdb */
  2647. mon_chr = g_malloc0(sizeof(*mon_chr));
  2648. mon_chr->chr_write = gdb_monitor_write;
  2649. monitor_init(mon_chr, 0);
  2650. } else {
  2651. if (s->chr)
  2652. qemu_chr_delete(s->chr);
  2653. mon_chr = s->mon_chr;
  2654. memset(s, 0, sizeof(GDBState));
  2655. }
  2656. s->c_cpu = first_cpu;
  2657. s->g_cpu = first_cpu;
  2658. s->chr = chr;
  2659. s->state = chr ? RS_IDLE : RS_INACTIVE;
  2660. s->mon_chr = mon_chr;
  2661. return 0;
  2662. }
  2663. #endif