dda120add8504b2a4e31f094005c178bbcc4b096
[riscv-isa-sim.git] / riscv / gdbserver.cc
1 #include <arpa/inet.h>
2 #include <errno.h>
3 #include <fcntl.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <sys/socket.h>
7 #include <sys/types.h>
8 #include <unistd.h>
9
10 #include <algorithm>
11 #include <cassert>
12 #include <cinttypes>
13 #include <cstdio>
14 #include <vector>
15
16 #include "disasm.h"
17 #include "sim.h"
18 #include "gdbserver.h"
19 #include "mmu.h"
20 #include "encoding.h"
21
22 //////////////////////////////////////// Utility Functions
23
24 #undef DEBUG
25 #ifdef DEBUG
26 # define D(x) x
27 #else
28 # define D(x)
29 #endif // DEBUG
30
31 void die(const char* msg)
32 {
33 fprintf(stderr, "gdbserver code died: %s\n", msg);
34 abort();
35 }
36
37 // gdb's register list is defined in riscv_gdb_reg_names gdb/riscv-tdep.c in
38 // its source tree. We must interpret the numbers the same here.
39 enum {
40 REG_XPR0 = 0,
41 REG_XPR31 = 31,
42 REG_PC = 32,
43 REG_FPR0 = 33,
44 REG_FPR31 = 64,
45 REG_CSR0 = 65,
46 REG_MSTATUS = CSR_MSTATUS + REG_CSR0,
47 REG_CSR4095 = 4160,
48 REG_PRIV = 4161
49 };
50
51 //////////////////////////////////////// Functions to generate RISC-V opcodes.
52
53 // TODO: Does this already exist somewhere?
54
55 #define ZERO 0
56 // Using regnames.cc as source. The RVG Calling Convention of the 2.0 RISC-V
57 // spec says it should be 2 and 3.
58 #define S0 8
59 #define S1 9
60 static uint32_t bits(uint32_t value, unsigned int hi, unsigned int lo) {
61 return (value >> lo) & ((1 << (hi+1-lo)) - 1);
62 }
63
64 static uint32_t bit(uint32_t value, unsigned int b) {
65 return (value >> b) & 1;
66 }
67
68 static uint32_t jal(unsigned int rd, uint32_t imm) {
69 return (bit(imm, 20) << 31) |
70 (bits(imm, 10, 1) << 21) |
71 (bit(imm, 11) << 20) |
72 (bits(imm, 19, 12) << 12) |
73 (rd << 7) |
74 MATCH_JAL;
75 }
76
77 static uint32_t csrsi(unsigned int csr, uint16_t imm) {
78 return (csr << 20) |
79 (bits(imm, 4, 0) << 15) |
80 MATCH_CSRRSI;
81 }
82
83 static uint32_t csrci(unsigned int csr, uint16_t imm) {
84 return (csr << 20) |
85 (bits(imm, 4, 0) << 15) |
86 MATCH_CSRRCI;
87 }
88
89 static uint32_t csrr(unsigned int rd, unsigned int csr) {
90 return (csr << 20) | (rd << 7) | MATCH_CSRRS;
91 }
92
93 static uint32_t csrw(unsigned int source, unsigned int csr) {
94 return (csr << 20) | (source << 15) | MATCH_CSRRW;
95 }
96
97 static uint32_t fence_i()
98 {
99 return MATCH_FENCE_I;
100 }
101
102 static uint32_t sb(unsigned int src, unsigned int base, uint16_t offset)
103 {
104 return (bits(offset, 11, 5) << 25) |
105 (src << 20) |
106 (base << 15) |
107 (bits(offset, 4, 0) << 7) |
108 MATCH_SB;
109 }
110
111 static uint32_t sh(unsigned int src, unsigned int base, uint16_t offset)
112 {
113 return (bits(offset, 11, 5) << 25) |
114 (src << 20) |
115 (base << 15) |
116 (bits(offset, 4, 0) << 7) |
117 MATCH_SH;
118 }
119
120 static uint32_t sw(unsigned int src, unsigned int base, uint16_t offset)
121 {
122 return (bits(offset, 11, 5) << 25) |
123 (src << 20) |
124 (base << 15) |
125 (bits(offset, 4, 0) << 7) |
126 MATCH_SW;
127 }
128
129 static uint32_t sd(unsigned int src, unsigned int base, uint16_t offset)
130 {
131 return (bits(offset, 11, 5) << 25) |
132 (bits(src, 4, 0) << 20) |
133 (base << 15) |
134 (bits(offset, 4, 0) << 7) |
135 MATCH_SD;
136 }
137
138 static uint32_t sq(unsigned int src, unsigned int base, uint16_t offset)
139 {
140 #if 0
141 return (bits(offset, 11, 5) << 25) |
142 (bits(src, 4, 0) << 20) |
143 (base << 15) |
144 (bits(offset, 4, 0) << 7) |
145 MATCH_SQ;
146 #else
147 abort();
148 #endif
149 }
150
151 static uint32_t lq(unsigned int rd, unsigned int base, uint16_t offset)
152 {
153 #if 0
154 return (bits(offset, 11, 0) << 20) |
155 (base << 15) |
156 (bits(rd, 4, 0) << 7) |
157 MATCH_LQ;
158 #else
159 abort();
160 #endif
161 }
162
163 static uint32_t ld(unsigned int rd, unsigned int base, uint16_t offset)
164 {
165 return (bits(offset, 11, 0) << 20) |
166 (base << 15) |
167 (bits(rd, 4, 0) << 7) |
168 MATCH_LD;
169 }
170
171 static uint32_t lw(unsigned int rd, unsigned int base, uint16_t offset)
172 {
173 return (bits(offset, 11, 0) << 20) |
174 (base << 15) |
175 (bits(rd, 4, 0) << 7) |
176 MATCH_LW;
177 }
178
179 static uint32_t lh(unsigned int rd, unsigned int base, uint16_t offset)
180 {
181 return (bits(offset, 11, 0) << 20) |
182 (base << 15) |
183 (bits(rd, 4, 0) << 7) |
184 MATCH_LH;
185 }
186
187 static uint32_t lb(unsigned int rd, unsigned int base, uint16_t offset)
188 {
189 return (bits(offset, 11, 0) << 20) |
190 (base << 15) |
191 (bits(rd, 4, 0) << 7) |
192 MATCH_LB;
193 }
194
195 static uint32_t fsw(unsigned int src, unsigned int base, uint16_t offset)
196 {
197 return (bits(offset, 11, 5) << 25) |
198 (bits(src, 4, 0) << 20) |
199 (base << 15) |
200 (bits(offset, 4, 0) << 7) |
201 MATCH_FSW;
202 }
203
204 static uint32_t fsd(unsigned int src, unsigned int base, uint16_t offset)
205 {
206 return (bits(offset, 11, 5) << 25) |
207 (bits(src, 4, 0) << 20) |
208 (base << 15) |
209 (bits(offset, 4, 0) << 7) |
210 MATCH_FSD;
211 }
212
213 static uint32_t flw(unsigned int dest, unsigned int base, uint16_t offset)
214 {
215 return (bits(offset, 11, 0) << 20) |
216 (base << 15) |
217 (bits(dest, 4, 0) << 7) |
218 MATCH_FLW;
219 }
220
221 static uint32_t fld(unsigned int dest, unsigned int base, uint16_t offset)
222 {
223 return (bits(offset, 11, 0) << 20) |
224 (base << 15) |
225 (bits(dest, 4, 0) << 7) |
226 MATCH_FLD;
227 }
228
229 static uint32_t addi(unsigned int dest, unsigned int src, uint16_t imm)
230 {
231 return (bits(imm, 11, 0) << 20) |
232 (src << 15) |
233 (dest << 7) |
234 MATCH_ADDI;
235 }
236
237 static uint32_t ori(unsigned int dest, unsigned int src, uint16_t imm)
238 {
239 return (bits(imm, 11, 0) << 20) |
240 (src << 15) |
241 (dest << 7) |
242 MATCH_ORI;
243 }
244
245 static uint32_t xori(unsigned int dest, unsigned int src, uint16_t imm)
246 {
247 return (bits(imm, 11, 0) << 20) |
248 (src << 15) |
249 (dest << 7) |
250 MATCH_XORI;
251 }
252
253 static uint32_t srli(unsigned int dest, unsigned int src, uint8_t shamt)
254 {
255 return (bits(shamt, 4, 0) << 20) |
256 (src << 15) |
257 (dest << 7) |
258 MATCH_SRLI;
259 }
260
261
262 static uint32_t nop()
263 {
264 return addi(0, 0, 0);
265 }
266
267 template <typename T>
268 unsigned int circular_buffer_t<T>::size() const
269 {
270 if (end >= start)
271 return end - start;
272 else
273 return end + capacity - start;
274 }
275
276 template <typename T>
277 void circular_buffer_t<T>::consume(unsigned int bytes)
278 {
279 start = (start + bytes) % capacity;
280 }
281
282 template <typename T>
283 unsigned int circular_buffer_t<T>::contiguous_empty_size() const
284 {
285 if (end >= start)
286 if (start == 0)
287 return capacity - end - 1;
288 else
289 return capacity - end;
290 else
291 return start - end - 1;
292 }
293
294 template <typename T>
295 unsigned int circular_buffer_t<T>::contiguous_data_size() const
296 {
297 if (end >= start)
298 return end - start;
299 else
300 return capacity - start;
301 }
302
303 template <typename T>
304 void circular_buffer_t<T>::data_added(unsigned int bytes)
305 {
306 end += bytes;
307 assert(end <= capacity);
308 if (end == capacity)
309 end = 0;
310 }
311
312 template <typename T>
313 void circular_buffer_t<T>::reset()
314 {
315 start = 0;
316 end = 0;
317 }
318
319 template <typename T>
320 void circular_buffer_t<T>::append(const T *src, unsigned int count)
321 {
322 unsigned int copy = std::min(count, contiguous_empty_size());
323 memcpy(contiguous_empty(), src, copy * sizeof(T));
324 data_added(copy);
325 count -= copy;
326 if (count > 0) {
327 assert(count < contiguous_empty_size());
328 memcpy(contiguous_empty(), src, count * sizeof(T));
329 data_added(count);
330 }
331 }
332
333 ////////////////////////////// Debug Operations
334
335 class halt_op_t : public operation_t
336 {
337 public:
338 halt_op_t(gdbserver_t& gdbserver, bool send_status=false) :
339 operation_t(gdbserver), send_status(send_status),
340 state(ST_ENTER) {};
341
342 void write_dpc_program() {
343 gs.dr_write32(0, csrsi(CSR_DCSR, DCSR_HALT));
344 gs.dr_write32(1, csrr(S0, CSR_DPC));
345 gs.dr_write_store(2, S0, SLOT_DATA0);
346 gs.dr_write_jump(3);
347 gs.set_interrupt(0);
348 }
349
350 bool perform_step(unsigned int step) {
351 switch (state) {
352 gs.tselect_valid = false;
353 case ST_ENTER:
354 if (gs.xlen == 0) {
355 gs.dr_write32(0, xori(S1, ZERO, -1));
356 gs.dr_write32(1, srli(S1, S1, 31));
357 // 0x00000001 0x00000001:ffffffff 0x00000001:ffffffff:ffffffff:ffffffff
358 gs.dr_write32(2, sw(S1, ZERO, DEBUG_RAM_START));
359 gs.dr_write32(3, srli(S1, S1, 31));
360 // 0x00000000 0x00000000:00000003 0x00000000:00000003:ffffffff:ffffffff
361 gs.dr_write32(4, sw(S1, ZERO, DEBUG_RAM_START + 4));
362 gs.dr_write_jump(5);
363 gs.set_interrupt(0);
364 state = ST_XLEN;
365
366 } else {
367 write_dpc_program();
368 state = ST_DPC;
369 }
370 return false;
371
372 case ST_XLEN:
373 {
374 uint32_t word0 = gs.dr_read32(0);
375 uint32_t word1 = gs.dr_read32(1);
376
377 if (word0 == 1 && word1 == 0) {
378 gs.xlen = 32;
379 } else if (word0 == 0xffffffff && word1 == 3) {
380 gs.xlen = 64;
381 } else if (word0 == 0xffffffff && word1 == 0xffffffff) {
382 gs.xlen = 128;
383 }
384
385 write_dpc_program();
386 state = ST_DPC;
387 return false;
388 }
389
390 case ST_DPC:
391 gs.dpc = gs.dr_read(SLOT_DATA0);
392 gs.dr_write32(0, csrr(S0, CSR_MSTATUS));
393 gs.dr_write_store(1, S0, SLOT_DATA0);
394 gs.dr_write_jump(2);
395 gs.set_interrupt(0);
396 state = ST_MSTATUS;
397 return false;
398
399 case ST_MSTATUS:
400 gs.mstatus = gs.dr_read(SLOT_DATA0);
401 gs.mstatus_dirty = false;
402 gs.dr_write32(0, csrr(S0, CSR_DCSR));
403 gs.dr_write32(1, sw(S0, 0, (uint16_t) DEBUG_RAM_START + 16));
404 gs.dr_write_jump(2);
405 gs.set_interrupt(0);
406 state = ST_DCSR;
407 return false;
408
409 case ST_DCSR:
410 gs.dcsr = gs.dr_read32(4);
411
412 gs.sptbr_valid = false;
413 gs.pte_cache.clear();
414
415 if (send_status) {
416 switch (get_field(gs.dcsr, DCSR_CAUSE)) {
417 case DCSR_CAUSE_NONE:
418 fprintf(stderr, "Internal error. Processor halted without reason.\n");
419 abort();
420
421 case DCSR_CAUSE_DEBUGINT:
422 gs.send_packet("S02"); // Pretend program received SIGINT.
423 break;
424
425 case DCSR_CAUSE_HWBP:
426 case DCSR_CAUSE_STEP:
427 case DCSR_CAUSE_HALT:
428 // There's no gdb code for this.
429 gs.send_packet("T05");
430 break;
431 case DCSR_CAUSE_SWBP:
432 gs.send_packet("T05swbreak:;");
433 break;
434 }
435 }
436
437 return true;
438
439 default:
440 assert(0);
441 }
442 }
443
444 private:
445 bool send_status;
446 enum {
447 ST_ENTER,
448 ST_XLEN,
449 ST_DPC,
450 ST_MSTATUS,
451 ST_DCSR
452 } state;
453 };
454
455 class continue_op_t : public operation_t
456 {
457 public:
458 continue_op_t(gdbserver_t& gdbserver, bool single_step) :
459 operation_t(gdbserver), single_step(single_step) {};
460
461 bool perform_step(unsigned int step) {
462 D(fprintf(stderr, "continue step %d\n", step));
463 switch (step) {
464 case 0:
465 gs.dr_write_load(0, S0, SLOT_DATA0);
466 gs.dr_write32(1, csrw(S0, CSR_DPC));
467 // TODO: Isn't there a fence.i in Debug ROM already?
468 if (gs.fence_i_required) {
469 gs.dr_write32(2, fence_i());
470 gs.dr_write_jump(3);
471 gs.fence_i_required = false;
472 } else {
473 gs.dr_write_jump(2);
474 }
475 gs.dr_write(SLOT_DATA0, gs.dpc);
476 gs.set_interrupt(0);
477 return false;
478
479 case 1:
480 gs.dr_write_load(0, S0, SLOT_DATA0);
481 gs.dr_write32(1, csrw(S0, CSR_MSTATUS));
482 gs.dr_write_jump(2);
483 gs.dr_write(SLOT_DATA0, gs.mstatus);
484 gs.set_interrupt(0);
485 return false;
486
487 case 2:
488 gs.dr_write32(0, lw(S0, 0, (uint16_t) DEBUG_RAM_START+16));
489 gs.dr_write32(1, csrw(S0, CSR_DCSR));
490 gs.dr_write_jump(2);
491
492 reg_t dcsr = set_field(gs.dcsr, DCSR_HALT, 0);
493 dcsr = set_field(dcsr, DCSR_STEP, single_step);
494 // Software breakpoints should go here.
495 dcsr = set_field(dcsr, DCSR_EBREAKM, 1);
496 dcsr = set_field(dcsr, DCSR_EBREAKH, 1);
497 dcsr = set_field(dcsr, DCSR_EBREAKS, 1);
498 dcsr = set_field(dcsr, DCSR_EBREAKU, 1);
499 gs.dr_write32(4, dcsr);
500
501 gs.set_interrupt(0);
502 return true;
503 }
504 return false;
505 }
506
507 private:
508 bool single_step;
509 };
510
511 class general_registers_read_op_t : public operation_t
512 {
513 // Register order that gdb expects is:
514 // "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
515 // "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
516 // "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
517 // "x24", "x25", "x26", "x27", "x28", "x29", "x30", "x31",
518
519 // Each byte of register data is described by two hex digits. The bytes with
520 // the register are transmitted in target byte order. The size of each
521 // register and their position within the ‘g’ packet are determined by the
522 // gdb internal gdbarch functions DEPRECATED_REGISTER_RAW_SIZE and
523 // gdbarch_register_name.
524
525 public:
526 general_registers_read_op_t(gdbserver_t& gdbserver) :
527 operation_t(gdbserver) {};
528
529 bool perform_step(unsigned int step)
530 {
531 D(fprintf(stderr, "register_read step %d\n", step));
532 if (step == 0) {
533 gs.start_packet();
534
535 // x0 is always zero.
536 if (gs.xlen == 32) {
537 gs.send((uint32_t) 0);
538 } else {
539 gs.send((uint64_t) 0);
540 }
541
542 gs.dr_write_store(0, 1, SLOT_DATA0);
543 gs.dr_write_store(1, 2, SLOT_DATA1);
544 gs.dr_write_jump(2);
545 gs.set_interrupt(0);
546 return false;
547 }
548
549 if (gs.xlen == 32) {
550 gs.send((uint32_t) gs.dr_read(SLOT_DATA0));
551 } else {
552 gs.send((uint64_t) gs.dr_read(SLOT_DATA0));
553 }
554 if (step >= 16) {
555 gs.end_packet();
556 return true;
557 }
558
559 if (gs.xlen == 32) {
560 gs.send((uint32_t) gs.dr_read(SLOT_DATA1));
561 } else {
562 gs.send((uint64_t) gs.dr_read(SLOT_DATA1));
563 }
564
565 unsigned int current_reg = 2 * step + 1;
566 unsigned int i = 0;
567 if (current_reg == S1) {
568 gs.dr_write_load(i++, S1, SLOT_DATA_LAST);
569 }
570 gs.dr_write_store(i++, current_reg, SLOT_DATA0);
571 if (current_reg + 1 == S0) {
572 gs.dr_write32(i++, csrr(S0, CSR_DSCRATCH));
573 }
574 if (step < 15) {
575 gs.dr_write_store(i++, current_reg+1, SLOT_DATA1);
576 }
577 gs.dr_write_jump(i);
578 gs.set_interrupt(0);
579
580 return false;
581 }
582 };
583
584 class register_read_op_t : public operation_t
585 {
586 public:
587 register_read_op_t(gdbserver_t& gdbserver, unsigned int reg) :
588 operation_t(gdbserver), reg(reg) {};
589
590 bool perform_step(unsigned int step)
591 {
592 switch (step) {
593 case 0:
594 if (reg >= REG_XPR0 && reg <= REG_XPR31) {
595 if (gs.xlen == 32) {
596 gs.dr_write32(0, sw(reg - REG_XPR0, 0, (uint16_t) DEBUG_RAM_START + 16));
597 } else {
598 gs.dr_write32(0, sd(reg - REG_XPR0, 0, (uint16_t) DEBUG_RAM_START + 16));
599 }
600 gs.dr_write_jump(1);
601 } else if (reg == REG_PC) {
602 gs.start_packet();
603 if (gs.xlen == 32) {
604 gs.send((uint32_t) gs.dpc);
605 } else {
606 gs.send(gs.dpc);
607 }
608 gs.end_packet();
609 return true;
610 } else if (reg >= REG_FPR0 && reg <= REG_FPR31) {
611 gs.dr_write_load(0, S0, SLOT_DATA1);
612 gs.dr_write(SLOT_DATA1, set_field(gs.mstatus, MSTATUS_FS, 1));
613 gs.dr_write32(1, csrw(S0, CSR_MSTATUS));
614 gs.mstatus_dirty = true;
615 if (gs.xlen == 32) {
616 gs.dr_write32(2, fsw(reg - REG_FPR0, 0, (uint16_t) DEBUG_RAM_START + 16));
617 } else {
618 gs.dr_write32(2, fsd(reg - REG_FPR0, 0, (uint16_t) DEBUG_RAM_START + 16));
619 }
620 gs.dr_write_jump(3);
621 } else if (reg == REG_MSTATUS) {
622 gs.start_packet();
623 if (gs.xlen == 32) {
624 gs.send((uint32_t) gs.mstatus);
625 } else {
626 gs.send(gs.mstatus);
627 }
628 gs.end_packet();
629 return true;
630 } else if (reg >= REG_CSR0 && reg <= REG_CSR4095) {
631 gs.dr_write32(0, csrr(S0, reg - REG_CSR0));
632 gs.dr_write_store(1, S0, SLOT_DATA0);
633 gs.dr_write_jump(2);
634 // If we hit an exception reading the CSR, we'll end up returning ~0 as
635 // the register's value, which is what we want. (Right?)
636 gs.dr_write(SLOT_DATA0, ~(uint64_t) 0);
637 } else if (reg == REG_PRIV) {
638 gs.start_packet();
639 gs.send((uint8_t) get_field(gs.dcsr, DCSR_PRV));
640 gs.end_packet();
641 return true;
642 } else {
643 gs.send_packet("E02");
644 return true;
645 }
646 gs.set_interrupt(0);
647 return false;
648
649 case 1:
650 {
651 unsigned result = gs.dr_read(SLOT_DATA_LAST);
652 if (result) {
653 gs.send_packet("E03");
654 return true;
655 }
656 gs.start_packet();
657 if (gs.xlen == 32) {
658 gs.send(gs.dr_read32(4));
659 } else {
660 gs.send(gs.dr_read(SLOT_DATA0));
661 }
662 gs.end_packet();
663 return true;
664 }
665 }
666 return false;
667 }
668
669 private:
670 unsigned int reg;
671 };
672
673 class register_write_op_t : public operation_t
674 {
675 public:
676 register_write_op_t(gdbserver_t& gdbserver, unsigned int reg, reg_t value) :
677 operation_t(gdbserver), reg(reg), value(value) {};
678
679 bool perform_step(unsigned int step)
680 {
681 switch (step) {
682 case 0:
683 gs.dr_write_load(0, S0, SLOT_DATA0);
684 gs.dr_write(SLOT_DATA0, value);
685 if (reg == S0) {
686 gs.dr_write32(1, csrw(S0, CSR_DSCRATCH));
687 gs.dr_write_jump(2);
688 } else if (reg == S1) {
689 gs.dr_write_store(1, S0, SLOT_DATA_LAST);
690 gs.dr_write_jump(2);
691 } else if (reg >= REG_XPR0 && reg <= REG_XPR31) {
692 gs.dr_write32(1, addi(reg, S0, 0));
693 gs.dr_write_jump(2);
694 } else if (reg == REG_PC) {
695 gs.dpc = value;
696 return true;
697 } else if (reg >= REG_FPR0 && reg <= REG_FPR31) {
698 gs.dr_write_load(0, S0, SLOT_DATA1);
699 gs.dr_write(SLOT_DATA1, set_field(gs.mstatus, MSTATUS_FS, 1));
700 gs.dr_write32(1, csrw(S0, CSR_MSTATUS));
701 gs.mstatus_dirty = true;
702 if (gs.xlen == 32) {
703 gs.dr_write32(2, flw(reg - REG_FPR0, 0, (uint16_t) DEBUG_RAM_START + 16));
704 } else {
705 gs.dr_write32(2, fld(reg - REG_FPR0, 0, (uint16_t) DEBUG_RAM_START + 16));
706 }
707 gs.dr_write_jump(3);
708 } else if (reg == REG_MSTATUS) {
709 gs.mstatus = value;
710 gs.mstatus_dirty = true;
711 return true;
712 } else if (reg >= REG_CSR0 && reg <= REG_CSR4095) {
713 gs.dr_write32(1, csrw(S0, reg - REG_CSR0));
714 gs.dr_write_jump(2);
715 if (reg == REG_CSR0 + CSR_SPTBR) {
716 gs.sptbr = value;
717 gs.sptbr_valid = true;
718 }
719 } else if (reg == REG_PRIV) {
720 gs.dcsr = set_field(gs.dcsr, DCSR_PRV, value);
721 return true;
722 } else {
723 gs.send_packet("E02");
724 return true;
725 }
726 gs.set_interrupt(0);
727 return false;
728
729 case 1:
730 {
731 unsigned result = gs.dr_read(SLOT_DATA_LAST);
732 if (result) {
733 gs.send_packet("E03");
734 return true;
735 }
736 gs.send_packet("OK");
737 return true;
738 }
739 }
740
741 assert(0);
742 }
743
744 private:
745 unsigned int reg;
746 reg_t value;
747 };
748
749 class memory_read_op_t : public operation_t
750 {
751 public:
752 // Read length bytes from vaddr, storing the result into data.
753 // If data is NULL, send the result straight to gdb.
754 memory_read_op_t(gdbserver_t& gdbserver, reg_t vaddr, unsigned int length,
755 unsigned char *data=NULL) :
756 operation_t(gdbserver), vaddr(vaddr), length(length), data(data), index(0)
757 {
758 buf = new uint8_t[length];
759 };
760
761 ~memory_read_op_t()
762 {
763 delete[] buf;
764 }
765
766 bool perform_step(unsigned int step)
767 {
768 if (step == 0) {
769 // address goes in S0
770 paddr = gs.translate(vaddr);
771 access_size = gs.find_access_size(paddr, length);
772
773 gs.dr_write_load(0, S0, SLOT_DATA0);
774 switch (access_size) {
775 case 1:
776 gs.dr_write32(1, lb(S1, S0, 0));
777 break;
778 case 2:
779 gs.dr_write32(1, lh(S1, S0, 0));
780 break;
781 case 4:
782 gs.dr_write32(1, lw(S1, S0, 0));
783 break;
784 case 8:
785 gs.dr_write32(1, ld(S1, S0, 0));
786 break;
787 }
788 gs.dr_write_store(2, S1, SLOT_DATA1);
789 gs.dr_write_jump(3);
790 gs.dr_write(SLOT_DATA0, paddr);
791 gs.set_interrupt(0);
792
793 return false;
794 }
795
796 if (gs.dr_read32(DEBUG_RAM_SIZE / 4 - 1)) {
797 // Note that OpenOCD doesn't report this error to gdb by default. They
798 // think it can mess up stack tracing. So far I haven't seen any
799 // problems.
800 gs.send_packet("E99");
801 return true;
802 }
803
804 reg_t value = gs.dr_read(SLOT_DATA1);
805 for (unsigned int i = 0; i < access_size; i++) {
806 if (data) {
807 *(data++) = value & 0xff;
808 D(fprintf(stderr, "%02x", (unsigned int) (value & 0xff)));
809 } else {
810 buf[index++] = value & 0xff;
811 }
812 value >>= 8;
813 }
814 if (data) {
815 D(fprintf(stderr, "\n"));
816 }
817 length -= access_size;
818 paddr += access_size;
819
820 if (length == 0) {
821 if (!data) {
822 gs.start_packet();
823 char buffer[3];
824 for (unsigned int i = 0; i < index; i++) {
825 sprintf(buffer, "%02x", (unsigned int) buf[i]);
826 gs.send(buffer);
827 }
828 gs.end_packet();
829 }
830 return true;
831 } else {
832 gs.dr_write(SLOT_DATA0, paddr);
833 gs.set_interrupt(0);
834 return false;
835 }
836 }
837
838 private:
839 reg_t vaddr;
840 unsigned int length;
841 unsigned char* data;
842 reg_t paddr;
843 unsigned int access_size;
844 unsigned int index;
845 uint8_t *buf;
846 };
847
848 class memory_write_op_t : public operation_t
849 {
850 public:
851 memory_write_op_t(gdbserver_t& gdbserver, reg_t vaddr, unsigned int length,
852 const unsigned char *data) :
853 operation_t(gdbserver), vaddr(vaddr), offset(0), length(length), data(data) {};
854
855 ~memory_write_op_t() {
856 delete[] data;
857 }
858
859 bool perform_step(unsigned int step)
860 {
861 reg_t paddr = gs.translate(vaddr);
862
863 unsigned int data_offset;
864 switch (gs.xlen) {
865 case 32:
866 data_offset = slot_offset32[SLOT_DATA1];
867 break;
868 case 64:
869 data_offset = slot_offset64[SLOT_DATA1];
870 break;
871 case 128:
872 data_offset = slot_offset128[SLOT_DATA1];
873 break;
874 default:
875 abort();
876 }
877
878 if (step == 0) {
879 access_size = gs.find_access_size(paddr, length);
880
881 D(fprintf(stderr, "write to 0x%lx -> 0x%lx (access=%d): ", vaddr, paddr,
882 access_size));
883 for (unsigned int i = 0; i < length; i++) {
884 D(fprintf(stderr, "%02x", data[i]));
885 }
886 D(fprintf(stderr, "\n"));
887
888 // address goes in S0
889 gs.dr_write_load(0, S0, SLOT_DATA0);
890 switch (access_size) {
891 case 1:
892 gs.dr_write32(1, lb(S1, 0, (uint16_t) DEBUG_RAM_START + 4*data_offset));
893 gs.dr_write32(2, sb(S1, S0, 0));
894 gs.dr_write32(data_offset, data[0]);
895 break;
896 case 2:
897 gs.dr_write32(1, lh(S1, 0, (uint16_t) DEBUG_RAM_START + 4*data_offset));
898 gs.dr_write32(2, sh(S1, S0, 0));
899 gs.dr_write32(data_offset, data[0] | (data[1] << 8));
900 break;
901 case 4:
902 gs.dr_write32(1, lw(S1, 0, (uint16_t) DEBUG_RAM_START + 4*data_offset));
903 gs.dr_write32(2, sw(S1, S0, 0));
904 gs.dr_write32(data_offset, data[0] | (data[1] << 8) |
905 (data[2] << 16) | (data[3] << 24));
906 break;
907 case 8:
908 gs.dr_write32(1, ld(S1, 0, (uint16_t) DEBUG_RAM_START + 4*data_offset));
909 gs.dr_write32(2, sd(S1, S0, 0));
910 gs.dr_write32(data_offset, data[0] | (data[1] << 8) |
911 (data[2] << 16) | (data[3] << 24));
912 gs.dr_write32(data_offset+1, data[4] | (data[5] << 8) |
913 (data[6] << 16) | (data[7] << 24));
914 break;
915 default:
916 fprintf(stderr, "gdbserver error: write %d bytes to 0x%016" PRIx64
917 " -> 0x%016" PRIx64 "; access_size=%d\n",
918 length, vaddr, paddr, access_size);
919 gs.send_packet("E12");
920 return true;
921 }
922 gs.dr_write_jump(3);
923 gs.dr_write(SLOT_DATA0, paddr);
924 gs.set_interrupt(0);
925
926 return false;
927 }
928
929 if (gs.dr_read32(DEBUG_RAM_SIZE / 4 - 1)) {
930 gs.send_packet("E98");
931 return true;
932 }
933
934 offset += access_size;
935 if (offset >= length) {
936 gs.send_packet("OK");
937 return true;
938 } else {
939 const unsigned char *d = data + offset;
940 switch (access_size) {
941 case 1:
942 gs.dr_write32(data_offset, d[0]);
943 break;
944 case 2:
945 gs.dr_write32(data_offset, d[0] | (d[1] << 8));
946 break;
947 case 4:
948 gs.dr_write32(data_offset, d[0] | (d[1] << 8) |
949 (d[2] << 16) | (d[3] << 24));
950 break;
951 case 8:
952 gs.dr_write32(data_offset, d[0] | (d[1] << 8) |
953 (d[2] << 16) | (d[3] << 24));
954 gs.dr_write32(data_offset+1, d[4] | (d[5] << 8) |
955 (d[6] << 16) | (d[7] << 24));
956 break;
957 default:
958 gs.send_packet("E13");
959 return true;
960 }
961 gs.dr_write(SLOT_DATA0, paddr + offset);
962 gs.set_interrupt(0);
963 return false;
964 }
965 }
966
967 private:
968 reg_t vaddr;
969 unsigned int offset;
970 unsigned int length;
971 unsigned int access_size;
972 const unsigned char *data;
973 };
974
975 class collect_translation_info_op_t : public operation_t
976 {
977 public:
978 // Read sufficient information from the target into gdbserver structures so
979 // that it's possible to translate vaddr, vaddr+length, and all addresses
980 // in between to physical addresses.
981 collect_translation_info_op_t(gdbserver_t& gdbserver, reg_t vaddr, size_t length) :
982 operation_t(gdbserver), state(STATE_START), vaddr(vaddr), length(length) {};
983
984 bool perform_step(unsigned int step)
985 {
986 unsigned int vm = gs.virtual_memory();
987
988 if (step == 0) {
989 switch (vm) {
990 case VM_MBARE:
991 // Nothing to be done.
992 return true;
993
994 case VM_SV32:
995 levels = 2;
996 ptidxbits = 10;
997 ptesize = 4;
998 break;
999 case VM_SV39:
1000 levels = 3;
1001 ptidxbits = 9;
1002 ptesize = 8;
1003 break;
1004 case VM_SV48:
1005 levels = 4;
1006 ptidxbits = 9;
1007 ptesize = 8;
1008 break;
1009
1010 default:
1011 {
1012 char buf[100];
1013 sprintf(buf, "VM mode %d is not supported by gdbserver.cc.", vm);
1014 die(buf);
1015 return true; // die doesn't return, but gcc doesn't know that.
1016 }
1017 }
1018 }
1019
1020 // Perform any reads from the just-completed action.
1021 switch (state) {
1022 case STATE_START:
1023 break;
1024 case STATE_READ_SPTBR:
1025 gs.sptbr = gs.dr_read(SLOT_DATA0);
1026 gs.sptbr_valid = true;
1027 break;
1028 case STATE_READ_PTE:
1029 if (ptesize == 4) {
1030 gs.pte_cache[pte_addr] = gs.dr_read32(4);
1031 } else {
1032 gs.pte_cache[pte_addr] = ((uint64_t) gs.dr_read32(5) << 32) |
1033 gs.dr_read32(4);
1034 }
1035 D(fprintf(stderr, "pte_cache[0x%lx] = 0x%lx\n", pte_addr, gs.pte_cache[pte_addr]));
1036 break;
1037 }
1038
1039 // Set up the next action.
1040 // We only get here for VM_SV32/39/38.
1041
1042 if (!gs.sptbr_valid) {
1043 state = STATE_READ_SPTBR;
1044 gs.dr_write32(0, csrr(S0, CSR_SPTBR));
1045 gs.dr_write_store(1, S0, SLOT_DATA0);
1046 gs.dr_write_jump(2);
1047 gs.set_interrupt(0);
1048 return false;
1049 }
1050
1051 reg_t base = gs.sptbr << PGSHIFT;
1052 int ptshift = (levels - 1) * ptidxbits;
1053 for (unsigned int i = 0; i < levels; i++, ptshift -= ptidxbits) {
1054 reg_t idx = (vaddr >> (PGSHIFT + ptshift)) & ((1 << ptidxbits) - 1);
1055
1056 pte_addr = base + idx * ptesize;
1057 auto it = gs.pte_cache.find(pte_addr);
1058 if (it == gs.pte_cache.end()) {
1059 state = STATE_READ_PTE;
1060 if (ptesize == 4) {
1061 gs.dr_write32(0, lw(S0, 0, (uint16_t) DEBUG_RAM_START + 16));
1062 gs.dr_write32(1, lw(S1, S0, 0));
1063 gs.dr_write32(2, sw(S1, 0, (uint16_t) DEBUG_RAM_START + 16));
1064 } else {
1065 assert(gs.xlen >= 64);
1066 gs.dr_write32(0, ld(S0, 0, (uint16_t) DEBUG_RAM_START + 16));
1067 gs.dr_write32(1, ld(S1, S0, 0));
1068 gs.dr_write32(2, sd(S1, 0, (uint16_t) DEBUG_RAM_START + 16));
1069 }
1070 gs.dr_write_jump(3);
1071 gs.dr_write32(4, pte_addr);
1072 gs.dr_write32(5, pte_addr >> 32);
1073 gs.set_interrupt(0);
1074 return false;
1075 }
1076
1077 reg_t pte = gs.pte_cache[pte_addr];
1078 reg_t ppn = pte >> PTE_PPN_SHIFT;
1079
1080 if (PTE_TABLE(pte)) { // next level of page table
1081 base = ppn << PGSHIFT;
1082 } else {
1083 // We've collected all the data required for the translation.
1084 return true;
1085 }
1086 }
1087 fprintf(stderr,
1088 "ERROR: gdbserver couldn't find appropriate PTEs to translate 0x%016" PRIx64 "\n",
1089 vaddr);
1090 return true;
1091 }
1092
1093 private:
1094 enum {
1095 STATE_START,
1096 STATE_READ_SPTBR,
1097 STATE_READ_PTE
1098 } state;
1099 reg_t vaddr;
1100 size_t length;
1101 unsigned int levels;
1102 unsigned int ptidxbits;
1103 unsigned int ptesize;
1104 reg_t pte_addr;
1105 };
1106
1107 class hardware_breakpoint_insert_op_t : public operation_t
1108 {
1109 public:
1110 hardware_breakpoint_insert_op_t(gdbserver_t& gdbserver,
1111 hardware_breakpoint_t bp) :
1112 operation_t(gdbserver), state(STATE_START), bp(bp) {};
1113
1114 void write_new_index_program()
1115 {
1116 gs.dr_write_load(0, S0, SLOT_DATA1);
1117 gs.dr_write32(1, csrw(S0, CSR_TSELECT));
1118 gs.dr_write32(2, csrr(S0, CSR_TSELECT));
1119 gs.dr_write_store(3, S0, SLOT_DATA1);
1120 gs.dr_write_jump(4);
1121 gs.dr_write(SLOT_DATA1, bp.index);
1122 }
1123
1124 bool perform_step(unsigned int step)
1125 {
1126 switch (state) {
1127 case STATE_START:
1128 bp.index = 0;
1129 write_new_index_program();
1130 state = STATE_CHECK_INDEX;
1131 break;
1132
1133 case STATE_CHECK_INDEX:
1134 if (gs.dr_read(SLOT_DATA1) != bp.index) {
1135 // We've exhausted breakpoints without finding an appropriate one.
1136 gs.send_packet("E58");
1137 return true;
1138 }
1139
1140 gs.dr_write32(0, csrr(S0, CSR_TDATA1));
1141 gs.dr_write_store(1, S0, SLOT_DATA0);
1142 gs.dr_write_jump(2);
1143 state = STATE_CHECK_MCONTROL;
1144 break;
1145
1146 case STATE_CHECK_MCONTROL:
1147 {
1148 reg_t mcontrol = gs.dr_read(SLOT_DATA0);
1149 unsigned int type = mcontrol >> (gs.xlen - 4);
1150 if (type == 0) {
1151 // We've exhausted breakpoints without finding an appropriate one.
1152 gs.send_packet("E58");
1153 return true;
1154 }
1155
1156 if (type == 2 &&
1157 !get_field(mcontrol, MCONTROL_EXECUTE) &&
1158 !get_field(mcontrol, MCONTROL_LOAD) &&
1159 !get_field(mcontrol, MCONTROL_STORE)) {
1160 // Found an unused trigger.
1161 gs.dr_write_load(0, S0, SLOT_DATA1);
1162 gs.dr_write32(1, csrw(S0, CSR_TDATA1));
1163 gs.dr_write_jump(2);
1164 mcontrol = set_field(0, MCONTROL_ACTION, MCONTROL_ACTION_DEBUG_MODE);
1165 mcontrol = set_field(mcontrol, MCONTROL_DMODE(gs.xlen), 1);
1166 mcontrol = set_field(mcontrol, MCONTROL_MATCH, MCONTROL_MATCH_EQUAL);
1167 mcontrol = set_field(mcontrol, MCONTROL_M, 1);
1168 mcontrol = set_field(mcontrol, MCONTROL_H, 1);
1169 mcontrol = set_field(mcontrol, MCONTROL_S, 1);
1170 mcontrol = set_field(mcontrol, MCONTROL_U, 1);
1171 mcontrol = set_field(mcontrol, MCONTROL_EXECUTE, bp.execute);
1172 mcontrol = set_field(mcontrol, MCONTROL_LOAD, bp.load);
1173 mcontrol = set_field(mcontrol, MCONTROL_STORE, bp.store);
1174 // For store triggers it's nicer to fire just before the
1175 // instruction than just after. However, gdb doesn't clear the
1176 // breakpoints and step before resuming from a store trigger.
1177 // That means that without extra code, you'll keep hitting the
1178 // same watchpoint over and over again. That's not useful at all.
1179 // Instead of fixing this the right way, just set timing=1 for
1180 // those triggers.
1181 if (bp.load || bp.store)
1182 mcontrol = set_field(mcontrol, MCONTROL_TIMING, 1);
1183
1184 gs.dr_write(SLOT_DATA1, mcontrol);
1185 state = STATE_WRITE_ADDRESS;
1186 } else {
1187 bp.index++;
1188 write_new_index_program();
1189 state = STATE_CHECK_INDEX;
1190 }
1191 }
1192 break;
1193
1194 case STATE_WRITE_ADDRESS:
1195 {
1196 gs.dr_write_load(0, S0, SLOT_DATA1);
1197 gs.dr_write32(1, csrw(S0, CSR_TDATA2));
1198 gs.dr_write_jump(2);
1199 gs.dr_write(SLOT_DATA1, bp.vaddr);
1200 gs.set_interrupt(0);
1201 gs.send_packet("OK");
1202
1203 gs.hardware_breakpoints.insert(bp);
1204
1205 return true;
1206 }
1207 }
1208
1209 gs.set_interrupt(0);
1210 return false;
1211 }
1212
1213 private:
1214 enum {
1215 STATE_START,
1216 STATE_CHECK_INDEX,
1217 STATE_CHECK_MCONTROL,
1218 STATE_WRITE_ADDRESS
1219 } state;
1220 hardware_breakpoint_t bp;
1221 };
1222
1223 class maybe_save_tselect_op_t : public operation_t
1224 {
1225 public:
1226 maybe_save_tselect_op_t(gdbserver_t& gdbserver) : operation_t(gdbserver) {};
1227 bool perform_step(unsigned int step) {
1228 if (gs.tselect_valid)
1229 return true;
1230
1231 switch (step) {
1232 case 0:
1233 gs.dr_write32(0, csrr(S0, CSR_TDATA1));
1234 gs.dr_write_store(1, S0, SLOT_DATA0);
1235 gs.dr_write_jump(2);
1236 gs.set_interrupt(0);
1237 return false;
1238 case 1:
1239 gs.tselect = gs.dr_read(SLOT_DATA0);
1240 gs.tselect_valid = true;
1241 break;
1242 }
1243 return true;
1244 }
1245 };
1246
1247 class maybe_restore_tselect_op_t : public operation_t
1248 {
1249 public:
1250 maybe_restore_tselect_op_t(gdbserver_t& gdbserver) : operation_t(gdbserver) {};
1251 bool perform_step(unsigned int step) {
1252 if (gs.tselect_valid) {
1253 gs.dr_write_load(0, S0, SLOT_DATA1);
1254 gs.dr_write32(1, csrw(S0, CSR_TSELECT));
1255 gs.dr_write_jump(2);
1256 gs.dr_write(SLOT_DATA1, gs.tselect);
1257 }
1258 return true;
1259 }
1260 };
1261
1262 class maybe_restore_mstatus_op_t : public operation_t
1263 {
1264 public:
1265 maybe_restore_mstatus_op_t(gdbserver_t& gdbserver) : operation_t(gdbserver) {};
1266 bool perform_step(unsigned int step) {
1267 if (gs.mstatus_dirty) {
1268 gs.dr_write_load(0, S0, SLOT_DATA1);
1269 gs.dr_write32(1, csrw(S0, CSR_MSTATUS));
1270 gs.dr_write_jump(2);
1271 gs.dr_write(SLOT_DATA1, gs.mstatus);
1272 }
1273 return true;
1274 }
1275 };
1276
1277 class hardware_breakpoint_remove_op_t : public operation_t
1278 {
1279 public:
1280 hardware_breakpoint_remove_op_t(gdbserver_t& gdbserver,
1281 hardware_breakpoint_t bp) :
1282 operation_t(gdbserver), bp(bp) {};
1283
1284 bool perform_step(unsigned int step) {
1285 gs.dr_write32(0, addi(S0, ZERO, bp.index));
1286 gs.dr_write32(1, csrw(S0, CSR_TSELECT));
1287 gs.dr_write32(2, csrw(ZERO, CSR_TDATA1));
1288 gs.dr_write_jump(3);
1289 gs.set_interrupt(0);
1290 return true;
1291 }
1292
1293 private:
1294 hardware_breakpoint_t bp;
1295 };
1296
1297 ////////////////////////////// gdbserver itself
1298
1299 gdbserver_t::gdbserver_t(uint16_t port, sim_t *sim) :
1300 xlen(0),
1301 sim(sim),
1302 client_fd(0),
1303 // gdb likes to send 0x100000 bytes at once when downloading.
1304 recv_buf(0x180000), send_buf(64 * 1024)
1305 {
1306 socket_fd = socket(AF_INET, SOCK_STREAM, 0);
1307 if (socket_fd == -1) {
1308 fprintf(stderr, "failed to make socket: %s (%d)\n", strerror(errno), errno);
1309 abort();
1310 }
1311
1312 fcntl(socket_fd, F_SETFL, O_NONBLOCK);
1313 int reuseaddr = 1;
1314 if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr,
1315 sizeof(int)) == -1) {
1316 fprintf(stderr, "failed setsockopt: %s (%d)\n", strerror(errno), errno);
1317 abort();
1318 }
1319
1320 struct sockaddr_in addr;
1321 memset(&addr, 0, sizeof(addr));
1322 addr.sin_family = AF_INET;
1323 addr.sin_addr.s_addr = INADDR_ANY;
1324 addr.sin_port = htons(port);
1325
1326 if (bind(socket_fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
1327 fprintf(stderr, "failed to bind socket: %s (%d)\n", strerror(errno), errno);
1328 abort();
1329 }
1330
1331 if (listen(socket_fd, 1) == -1) {
1332 fprintf(stderr, "failed to listen on socket: %s (%d)\n", strerror(errno), errno);
1333 abort();
1334 }
1335 }
1336
1337 unsigned int gdbserver_t::find_access_size(reg_t address, int length)
1338 {
1339 reg_t composite = address | length;
1340 if ((composite & 0x7) == 0 && xlen >= 64)
1341 return 8;
1342 if ((composite & 0x3) == 0)
1343 return 4;
1344 return 1;
1345 }
1346
1347 reg_t gdbserver_t::translate(reg_t vaddr)
1348 {
1349 unsigned int vm = virtual_memory();
1350 unsigned int levels, ptidxbits, ptesize;
1351
1352 switch (vm) {
1353 case VM_MBARE:
1354 return vaddr;
1355
1356 case VM_SV32:
1357 levels = 2;
1358 ptidxbits = 10;
1359 ptesize = 4;
1360 break;
1361 case VM_SV39:
1362 levels = 3;
1363 ptidxbits = 9;
1364 ptesize = 8;
1365 break;
1366 case VM_SV48:
1367 levels = 4;
1368 ptidxbits = 9;
1369 ptesize = 8;
1370 break;
1371
1372 default:
1373 {
1374 char buf[100];
1375 sprintf(buf, "VM mode %d is not supported by gdbserver.cc.", vm);
1376 die(buf);
1377 return true; // die doesn't return, but gcc doesn't know that.
1378 }
1379 }
1380
1381 // Handle page tables here. There's a bunch of duplicated code with
1382 // collect_translation_info_op_t. :-(
1383 reg_t base = sptbr << PGSHIFT;
1384 int ptshift = (levels - 1) * ptidxbits;
1385 for (unsigned int i = 0; i < levels; i++, ptshift -= ptidxbits) {
1386 reg_t idx = (vaddr >> (PGSHIFT + ptshift)) & ((1 << ptidxbits) - 1);
1387
1388 reg_t pte_addr = base + idx * ptesize;
1389 auto it = pte_cache.find(pte_addr);
1390 if (it == pte_cache.end()) {
1391 fprintf(stderr, "ERROR: gdbserver tried to translate 0x%016" PRIx64
1392 " without first collecting the relevant PTEs.\n", vaddr);
1393 die("gdbserver_t::translate()");
1394 }
1395
1396 reg_t pte = pte_cache[pte_addr];
1397 reg_t ppn = pte >> PTE_PPN_SHIFT;
1398
1399 if (PTE_TABLE(pte)) { // next level of page table
1400 base = ppn << PGSHIFT;
1401 } else {
1402 // We've collected all the data required for the translation.
1403 reg_t vpn = vaddr >> PGSHIFT;
1404 reg_t paddr = (ppn | (vpn & ((reg_t(1) << ptshift) - 1))) << PGSHIFT;
1405 paddr += vaddr & (PGSIZE-1);
1406 D(fprintf(stderr, "gdbserver translate 0x%lx -> 0x%lx\n", vaddr, paddr));
1407 return paddr;
1408 }
1409 }
1410
1411 fprintf(stderr, "ERROR: gdbserver tried to translate 0x%016" PRIx64
1412 " but the relevant PTEs are invalid.\n", vaddr);
1413 // TODO: Is it better to throw an exception here?
1414 return -1;
1415 }
1416
1417 unsigned int gdbserver_t::privilege_mode()
1418 {
1419 unsigned int mode = get_field(dcsr, DCSR_PRV);
1420 if (get_field(mstatus, MSTATUS_MPRV))
1421 mode = get_field(mstatus, MSTATUS_MPP);
1422 return mode;
1423 }
1424
1425 unsigned int gdbserver_t::virtual_memory()
1426 {
1427 unsigned int mode = privilege_mode();
1428 if (mode == PRV_M)
1429 return VM_MBARE;
1430 return get_field(mstatus, MSTATUS_VM);
1431 }
1432
1433 void gdbserver_t::dr_write32(unsigned int index, uint32_t value)
1434 {
1435 sim->debug_module.ram_write32(index, value);
1436 }
1437
1438 void gdbserver_t::dr_write64(unsigned int index, uint64_t value)
1439 {
1440 dr_write32(index, value);
1441 dr_write32(index+1, value >> 32);
1442 }
1443
1444 void gdbserver_t::dr_write(enum slot slot, uint64_t value)
1445 {
1446 switch (xlen) {
1447 case 32:
1448 dr_write32(slot_offset32[slot], value);
1449 break;
1450 case 64:
1451 dr_write64(slot_offset64[slot], value);
1452 break;
1453 case 128:
1454 default:
1455 abort();
1456 }
1457 }
1458
1459 void gdbserver_t::dr_write_jump(unsigned int index)
1460 {
1461 dr_write32(index, jal(0,
1462 (uint32_t) (DEBUG_ROM_RESUME - (DEBUG_RAM_START + 4*index))));
1463 }
1464
1465 void gdbserver_t::dr_write_store(unsigned int index, unsigned int reg, enum slot slot)
1466 {
1467 assert(slot != SLOT_INST0 || index > 2);
1468 assert(slot != SLOT_DATA0 || index < 4 || index > 6);
1469 assert(slot != SLOT_DATA1 || index < 5 || index > 10);
1470 assert(slot != SLOT_DATA_LAST || index < 6 || index > 14);
1471 switch (xlen) {
1472 case 32:
1473 return dr_write32(index,
1474 sw(reg, 0, (uint16_t) DEBUG_RAM_START + 4 * slot_offset32[slot]));
1475 case 64:
1476 return dr_write32(index,
1477 sd(reg, 0, (uint16_t) DEBUG_RAM_START + 4 * slot_offset64[slot]));
1478 case 128:
1479 return dr_write32(index,
1480 sq(reg, 0, (uint16_t) DEBUG_RAM_START + 4 * slot_offset128[slot]));
1481 default:
1482 fprintf(stderr, "xlen is %d!\n", xlen);
1483 abort();
1484 }
1485 }
1486
1487 void gdbserver_t::dr_write_load(unsigned int index, unsigned int reg, enum slot slot)
1488 {
1489 switch (xlen) {
1490 case 32:
1491 return dr_write32(index,
1492 lw(reg, 0, (uint16_t) DEBUG_RAM_START + 4 * slot_offset32[slot]));
1493 case 64:
1494 return dr_write32(index,
1495 ld(reg, 0, (uint16_t) DEBUG_RAM_START + 4 * slot_offset64[slot]));
1496 case 128:
1497 return dr_write32(index,
1498 lq(reg, 0, (uint16_t) DEBUG_RAM_START + 4 * slot_offset128[slot]));
1499 default:
1500 fprintf(stderr, "xlen is %d!\n", xlen);
1501 abort();
1502 }
1503 }
1504
1505 uint32_t gdbserver_t::dr_read32(unsigned int index)
1506 {
1507 uint32_t value = sim->debug_module.ram_read32(index);
1508 D(fprintf(stderr, "read32(%d) -> 0x%x\n", index, value));
1509 return value;
1510 }
1511
1512 uint64_t gdbserver_t::dr_read64(unsigned int index)
1513 {
1514 return ((uint64_t) dr_read32(index+1) << 32) | dr_read32(index);
1515 }
1516
1517 uint64_t gdbserver_t::dr_read(enum slot slot)
1518 {
1519 switch (xlen) {
1520 case 32:
1521 return dr_read32(slot_offset32[slot]);
1522 case 64:
1523 return dr_read64(slot_offset64[slot]);
1524 case 128:
1525 abort();
1526 default:
1527 abort();
1528 }
1529 }
1530
1531 void gdbserver_t::add_operation(operation_t* operation)
1532 {
1533 operation_queue.push(operation);
1534 }
1535
1536 void gdbserver_t::accept()
1537 {
1538 client_fd = ::accept(socket_fd, NULL, NULL);
1539 if (client_fd == -1) {
1540 if (errno == EAGAIN) {
1541 // No client waiting to connect right now.
1542 } else {
1543 fprintf(stderr, "failed to accept on socket: %s (%d)\n", strerror(errno),
1544 errno);
1545 abort();
1546 }
1547 } else {
1548 fcntl(client_fd, F_SETFL, O_NONBLOCK);
1549
1550 expect_ack = false;
1551 extended_mode = false;
1552
1553 // gdb wants the core to be halted when it attaches.
1554 add_operation(new halt_op_t(*this));
1555 }
1556 }
1557
1558 void gdbserver_t::read()
1559 {
1560 // Reading from a non-blocking socket still blocks if there is no data
1561 // available.
1562
1563 size_t count = recv_buf.contiguous_empty_size();
1564 ssize_t bytes = ::read(client_fd, recv_buf.contiguous_empty(), count);
1565 if (bytes == -1) {
1566 if (errno == EAGAIN) {
1567 // We'll try again the next call.
1568 } else {
1569 fprintf(stderr, "failed to read on socket: %s (%d)\n", strerror(errno), errno);
1570 abort();
1571 }
1572 } else if (bytes == 0) {
1573 // The remote disconnected.
1574 client_fd = 0;
1575 processor_t *p = sim->get_core(0);
1576 // TODO p->set_halted(false, HR_NONE);
1577 recv_buf.reset();
1578 send_buf.reset();
1579 } else {
1580 recv_buf.data_added(bytes);
1581 }
1582 }
1583
1584 void gdbserver_t::write()
1585 {
1586 if (send_buf.empty())
1587 return;
1588
1589 while (!send_buf.empty()) {
1590 unsigned int count = send_buf.contiguous_data_size();
1591 assert(count > 0);
1592 ssize_t bytes = ::write(client_fd, send_buf.contiguous_data(), count);
1593 if (bytes == -1) {
1594 fprintf(stderr, "failed to write to socket: %s (%d)\n", strerror(errno), errno);
1595 abort();
1596 } else if (bytes == 0) {
1597 // Client can't take any more data right now.
1598 break;
1599 } else {
1600 D(fprintf(stderr, "wrote %ld bytes: ", bytes));
1601 for (unsigned int i = 0; i < bytes; i++) {
1602 D(fprintf(stderr, "%c", send_buf[i]));
1603 }
1604 D(fprintf(stderr, "\n"));
1605 send_buf.consume(bytes);
1606 }
1607 }
1608 }
1609
1610 void print_packet(const std::vector<uint8_t> &packet)
1611 {
1612 for (uint8_t c : packet) {
1613 if (c >= ' ' and c <= '~')
1614 fprintf(stderr, "%c", c);
1615 else
1616 fprintf(stderr, "\\x%02x", c);
1617 }
1618 fprintf(stderr, "\n");
1619 }
1620
1621 uint8_t compute_checksum(const std::vector<uint8_t> &packet)
1622 {
1623 uint8_t checksum = 0;
1624 for (auto i = packet.begin() + 1; i != packet.end() - 3; i++ ) {
1625 checksum += *i;
1626 }
1627 return checksum;
1628 }
1629
1630 uint8_t character_hex_value(uint8_t character)
1631 {
1632 if (character >= '0' && character <= '9')
1633 return character - '0';
1634 if (character >= 'a' && character <= 'f')
1635 return 10 + character - 'a';
1636 if (character >= 'A' && character <= 'F')
1637 return 10 + character - 'A';
1638 return 0xff;
1639 }
1640
1641 uint8_t extract_checksum(const std::vector<uint8_t> &packet)
1642 {
1643 return character_hex_value(*(packet.end() - 1)) +
1644 16 * character_hex_value(*(packet.end() - 2));
1645 }
1646
1647 void gdbserver_t::process_requests()
1648 {
1649 // See https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html
1650
1651 while (!recv_buf.empty()) {
1652 std::vector<uint8_t> packet;
1653 for (unsigned int i = 0; i < recv_buf.size(); i++) {
1654 uint8_t b = recv_buf[i];
1655
1656 if (packet.empty() && expect_ack && b == '+') {
1657 recv_buf.consume(1);
1658 break;
1659 }
1660
1661 if (packet.empty() && b == 3) {
1662 D(fprintf(stderr, "Received interrupt\n"));
1663 recv_buf.consume(1);
1664 handle_interrupt();
1665 break;
1666 }
1667
1668 if (b == '$') {
1669 // Start of new packet.
1670 if (!packet.empty()) {
1671 fprintf(stderr, "Received malformed %ld-byte packet from debug client: ",
1672 packet.size());
1673 print_packet(packet);
1674 recv_buf.consume(i);
1675 break;
1676 }
1677 }
1678
1679 packet.push_back(b);
1680
1681 // Packets consist of $<packet-data>#<checksum>
1682 // where <checksum> is
1683 if (packet.size() >= 4 &&
1684 packet[packet.size()-3] == '#') {
1685 handle_packet(packet);
1686 recv_buf.consume(i+1);
1687 break;
1688 }
1689 }
1690 // There's a partial packet in the buffer. Wait until we get more data to
1691 // process it.
1692 if (packet.size()) {
1693 break;
1694 }
1695 }
1696
1697 if (recv_buf.full()) {
1698 fprintf(stderr,
1699 "Receive buffer is full, but no complete packet was found!\n");
1700 for (unsigned line = 0; line < 8; line++) {
1701 for (unsigned i = 0; i < 16; i++) {
1702 fprintf(stderr, "%02x ", recv_buf.entry(line * 16 + i));
1703 }
1704 for (unsigned i = 0; i < 16; i++) {
1705 uint8_t e = recv_buf.entry(line * 16 + i);
1706 if (e >= ' ' && e <= '~')
1707 fprintf(stderr, "%c", e);
1708 else
1709 fprintf(stderr, ".");
1710 }
1711 fprintf(stderr, "\n");
1712 }
1713 assert(!recv_buf.full());
1714 }
1715 }
1716
1717 void gdbserver_t::handle_halt_reason(const std::vector<uint8_t> &packet)
1718 {
1719 send_packet("S00");
1720 }
1721
1722 void gdbserver_t::handle_general_registers_read(const std::vector<uint8_t> &packet)
1723 {
1724 add_operation(new general_registers_read_op_t(*this));
1725 }
1726
1727 void gdbserver_t::set_interrupt(uint32_t hartid) {
1728 sim->debug_module.set_interrupt(hartid);
1729 }
1730
1731 // First byte is the most-significant one.
1732 // Eg. "08675309" becomes 0x08675309.
1733 uint64_t consume_hex_number(std::vector<uint8_t>::const_iterator &iter,
1734 std::vector<uint8_t>::const_iterator end)
1735 {
1736 uint64_t value = 0;
1737
1738 while (iter != end) {
1739 uint8_t c = *iter;
1740 uint64_t c_value = character_hex_value(c);
1741 if (c_value > 15)
1742 break;
1743 iter++;
1744 value <<= 4;
1745 value += c_value;
1746 }
1747 return value;
1748 }
1749
1750 // First byte is the least-significant one.
1751 // Eg. "08675309" becomes 0x09536708
1752 uint64_t gdbserver_t::consume_hex_number_le(
1753 std::vector<uint8_t>::const_iterator &iter,
1754 std::vector<uint8_t>::const_iterator end)
1755 {
1756 uint64_t value = 0;
1757 unsigned int shift = 4;
1758
1759 while (iter != end) {
1760 uint8_t c = *iter;
1761 uint64_t c_value = character_hex_value(c);
1762 if (c_value > 15)
1763 break;
1764 iter++;
1765 value |= c_value << shift;
1766 if ((shift % 8) == 0)
1767 shift += 12;
1768 else
1769 shift -= 4;
1770 }
1771 if (shift > (xlen+4)) {
1772 fprintf(stderr,
1773 "gdb sent too many data bytes. That means it thinks XLEN is greater "
1774 "than %d.\nTo fix that, tell gdb: set arch riscv:rv%d\n",
1775 xlen, xlen);
1776 }
1777 return value;
1778 }
1779
1780 void consume_string(std::string &str, std::vector<uint8_t>::const_iterator &iter,
1781 std::vector<uint8_t>::const_iterator end, uint8_t separator)
1782 {
1783 while (iter != end && *iter != separator) {
1784 str.append(1, (char) *iter);
1785 iter++;
1786 }
1787 }
1788
1789 void gdbserver_t::handle_register_read(const std::vector<uint8_t> &packet)
1790 {
1791 // p n
1792
1793 std::vector<uint8_t>::const_iterator iter = packet.begin() + 2;
1794 unsigned int n = consume_hex_number(iter, packet.end());
1795 if (*iter != '#')
1796 return send_packet("E01");
1797
1798 add_operation(new register_read_op_t(*this, n));
1799 }
1800
1801 void gdbserver_t::handle_register_write(const std::vector<uint8_t> &packet)
1802 {
1803 // P n...=r...
1804
1805 std::vector<uint8_t>::const_iterator iter = packet.begin() + 2;
1806 unsigned int n = consume_hex_number(iter, packet.end());
1807 if (*iter != '=')
1808 return send_packet("E05");
1809 iter++;
1810
1811 reg_t value = consume_hex_number_le(iter, packet.end());
1812 if (*iter != '#')
1813 return send_packet("E06");
1814
1815 processor_t *p = sim->get_core(0);
1816
1817 add_operation(new register_write_op_t(*this, n, value));
1818
1819 return send_packet("OK");
1820 }
1821
1822 void gdbserver_t::handle_memory_read(const std::vector<uint8_t> &packet)
1823 {
1824 // m addr,length
1825 std::vector<uint8_t>::const_iterator iter = packet.begin() + 2;
1826 reg_t address = consume_hex_number(iter, packet.end());
1827 if (*iter != ',')
1828 return send_packet("E10");
1829 iter++;
1830 reg_t length = consume_hex_number(iter, packet.end());
1831 if (*iter != '#')
1832 return send_packet("E11");
1833
1834 add_operation(new collect_translation_info_op_t(*this, address, length));
1835 add_operation(new memory_read_op_t(*this, address, length));
1836 }
1837
1838 void gdbserver_t::handle_memory_binary_write(const std::vector<uint8_t> &packet)
1839 {
1840 // X addr,length:XX...
1841 std::vector<uint8_t>::const_iterator iter = packet.begin() + 2;
1842 reg_t address = consume_hex_number(iter, packet.end());
1843 if (*iter != ',')
1844 return send_packet("E20");
1845 iter++;
1846 reg_t length = consume_hex_number(iter, packet.end());
1847 if (*iter != ':')
1848 return send_packet("E21");
1849 iter++;
1850
1851 if (length == 0) {
1852 return send_packet("OK");
1853 }
1854
1855 unsigned char *data = new unsigned char[length];
1856 for (unsigned int i = 0; i < length; i++) {
1857 if (iter == packet.end()) {
1858 return send_packet("E22");
1859 }
1860 uint8_t c = *iter;
1861 iter++;
1862 if (c == '}') {
1863 // The binary data representation uses 7d (ascii ‘}’) as an escape
1864 // character. Any escaped byte is transmitted as the escape character
1865 // followed by the original character XORed with 0x20. For example, the
1866 // byte 0x7d would be transmitted as the two bytes 0x7d 0x5d. The bytes
1867 // 0x23 (ascii ‘#’), 0x24 (ascii ‘$’), and 0x7d (ascii ‘}’) must always
1868 // be escaped.
1869 if (iter == packet.end()) {
1870 return send_packet("E23");
1871 }
1872 c = (*iter) ^ 0x20;
1873 iter++;
1874 }
1875 data[i] = c;
1876 }
1877 if (*iter != '#')
1878 return send_packet("E4b"); // EOVERFLOW
1879
1880 add_operation(new collect_translation_info_op_t(*this, address, length));
1881 add_operation(new memory_write_op_t(*this, address, length, data));
1882 }
1883
1884 void gdbserver_t::handle_continue(const std::vector<uint8_t> &packet)
1885 {
1886 // c [addr]
1887 processor_t *p = sim->get_core(0);
1888 if (packet[2] != '#') {
1889 std::vector<uint8_t>::const_iterator iter = packet.begin() + 2;
1890 dpc = consume_hex_number(iter, packet.end());
1891 if (*iter != '#')
1892 return send_packet("E30");
1893 }
1894
1895 add_operation(new maybe_restore_tselect_op_t(*this));
1896 add_operation(new maybe_restore_mstatus_op_t(*this));
1897 add_operation(new continue_op_t(*this, false));
1898 }
1899
1900 void gdbserver_t::handle_step(const std::vector<uint8_t> &packet)
1901 {
1902 // s [addr]
1903 if (packet[2] != '#') {
1904 std::vector<uint8_t>::const_iterator iter = packet.begin() + 2;
1905 die("handle_step");
1906 //p->state.pc = consume_hex_number(iter, packet.end());
1907 if (*iter != '#')
1908 return send_packet("E40");
1909 }
1910
1911 add_operation(new maybe_restore_tselect_op_t(*this));
1912 add_operation(new continue_op_t(*this, true));
1913 }
1914
1915 void gdbserver_t::handle_kill(const std::vector<uint8_t> &packet)
1916 {
1917 // k
1918 // The exact effect of this packet is not specified.
1919 // Looks like OpenOCD disconnects?
1920 // TODO
1921 }
1922
1923 void gdbserver_t::handle_extended(const std::vector<uint8_t> &packet)
1924 {
1925 // Enable extended mode. In extended mode, the remote server is made
1926 // persistent. The ‘R’ packet is used to restart the program being debugged.
1927 send_packet("OK");
1928 extended_mode = true;
1929 }
1930
1931 void gdbserver_t::software_breakpoint_insert(reg_t vaddr, unsigned int size)
1932 {
1933 fence_i_required = true;
1934 add_operation(new collect_translation_info_op_t(*this, vaddr, size));
1935 unsigned char* inst = new unsigned char[4];
1936 if (size == 2) {
1937 inst[0] = MATCH_C_EBREAK & 0xff;
1938 inst[1] = (MATCH_C_EBREAK >> 8) & 0xff;
1939 } else {
1940 inst[0] = MATCH_EBREAK & 0xff;
1941 inst[1] = (MATCH_EBREAK >> 8) & 0xff;
1942 inst[2] = (MATCH_EBREAK >> 16) & 0xff;
1943 inst[3] = (MATCH_EBREAK >> 24) & 0xff;
1944 }
1945
1946 software_breakpoint_t bp = {
1947 .vaddr = vaddr,
1948 .size = size
1949 };
1950 software_breakpoints[vaddr] = bp;
1951 add_operation(new memory_read_op_t(*this, bp.vaddr, bp.size,
1952 software_breakpoints[bp.vaddr].instruction));
1953 add_operation(new memory_write_op_t(*this, bp.vaddr, bp.size, inst));
1954 }
1955
1956 void gdbserver_t::software_breakpoint_remove(reg_t vaddr, unsigned int size)
1957 {
1958 fence_i_required = true;
1959 add_operation(new collect_translation_info_op_t(*this, vaddr, size));
1960
1961 software_breakpoint_t found_bp = software_breakpoints[vaddr];
1962 unsigned char* instruction = new unsigned char[4];
1963 memcpy(instruction, found_bp.instruction, 4);
1964 add_operation(new memory_write_op_t(*this, found_bp.vaddr,
1965 found_bp.size, instruction));
1966 software_breakpoints.erase(vaddr);
1967 }
1968
1969 void gdbserver_t::hardware_breakpoint_insert(const hardware_breakpoint_t &bp)
1970 {
1971 add_operation(new maybe_save_tselect_op_t(*this));
1972 add_operation(new hardware_breakpoint_insert_op_t(*this, bp));
1973 }
1974
1975 void gdbserver_t::hardware_breakpoint_remove(const hardware_breakpoint_t &bp)
1976 {
1977 add_operation(new maybe_save_tselect_op_t(*this));
1978 hardware_breakpoint_t found = *hardware_breakpoints.find(bp);
1979 add_operation(new hardware_breakpoint_remove_op_t(*this, found));
1980 }
1981
1982 void gdbserver_t::handle_breakpoint(const std::vector<uint8_t> &packet)
1983 {
1984 // insert: Z type,addr,length
1985 // remove: z type,addr,length
1986
1987 // type: 0 - software breakpoint, 1 - hardware breakpoint, 2 - write
1988 // watchpoint, 3 - read watchpoint, 4 - access watchpoint; addr is address;
1989 // length is in bytes. For a software breakpoint, length specifies the size
1990 // of the instruction to be patched. For hardware breakpoints and watchpoints
1991 // length specifies the memory region to be monitored. To avoid potential
1992 // problems with duplicate packets, the operations should be implemented in
1993 // an idempotent way.
1994
1995 bool insert = (packet[1] == 'Z');
1996 std::vector<uint8_t>::const_iterator iter = packet.begin() + 2;
1997 gdb_breakpoint_type_t type = static_cast<gdb_breakpoint_type_t>(
1998 consume_hex_number(iter, packet.end()));
1999 if (*iter != ',')
2000 return send_packet("E50");
2001 iter++;
2002 reg_t address = consume_hex_number(iter, packet.end());
2003 if (*iter != ',')
2004 return send_packet("E51");
2005 iter++;
2006 unsigned int size = consume_hex_number(iter, packet.end());
2007 // There may be more options after a ; here, but we don't support that.
2008 if (*iter != '#')
2009 return send_packet("E52");
2010
2011 switch (type) {
2012 case GB_SOFTWARE:
2013 if (size != 2 && size != 4) {
2014 return send_packet("E53");
2015 }
2016 if (insert) {
2017 software_breakpoint_insert(address, size);
2018 } else {
2019 software_breakpoint_remove(address, size);
2020 }
2021 break;
2022
2023 case GB_HARDWARE:
2024 case GB_WRITE:
2025 case GB_READ:
2026 case GB_ACCESS:
2027 {
2028 hardware_breakpoint_t bp = {
2029 .vaddr = address,
2030 .size = size
2031 };
2032 bp.load = (type == GB_READ || type == GB_ACCESS);
2033 bp.store = (type == GB_WRITE || type == GB_ACCESS);
2034 bp.execute = (type == GB_HARDWARE || type == GB_ACCESS);
2035 if (insert) {
2036 hardware_breakpoint_insert(bp);
2037 // Insert might fail if there's no space, so the insert operation will
2038 // send its own OK (or not).
2039 return;
2040 } else {
2041 hardware_breakpoint_remove(bp);
2042 }
2043 }
2044 break;
2045
2046 default:
2047 return send_packet("E56");
2048 }
2049
2050 return send_packet("OK");
2051 }
2052
2053 void gdbserver_t::handle_query(const std::vector<uint8_t> &packet)
2054 {
2055 std::string name;
2056 std::vector<uint8_t>::const_iterator iter = packet.begin() + 2;
2057
2058 consume_string(name, iter, packet.end(), ':');
2059 if (iter != packet.end())
2060 iter++;
2061 if (name == "Supported") {
2062 start_packet();
2063 while (iter != packet.end()) {
2064 std::string feature;
2065 consume_string(feature, iter, packet.end(), ';');
2066 if (iter != packet.end())
2067 iter++;
2068 if (feature == "swbreak+") {
2069 send("swbreak+;");
2070 }
2071 }
2072 send("PacketSize=131072;");
2073 return end_packet();
2074 }
2075
2076 D(fprintf(stderr, "Unsupported query %s\n", name.c_str()));
2077 return send_packet("");
2078 }
2079
2080 void gdbserver_t::handle_packet(const std::vector<uint8_t> &packet)
2081 {
2082 if (compute_checksum(packet) != extract_checksum(packet)) {
2083 fprintf(stderr, "Received %ld-byte packet with invalid checksum\n", packet.size());
2084 fprintf(stderr, "Computed checksum: %x\n", compute_checksum(packet));
2085 print_packet(packet);
2086 send("-");
2087 return;
2088 }
2089
2090 D(fprintf(stderr, "Received %ld-byte packet from debug client: ", packet.size()));
2091 D(print_packet(packet));
2092 send("+");
2093
2094 switch (packet[1]) {
2095 case '!':
2096 return handle_extended(packet);
2097 case '?':
2098 return handle_halt_reason(packet);
2099 case 'g':
2100 return handle_general_registers_read(packet);
2101 // case 'k':
2102 // return handle_kill(packet);
2103 case 'm':
2104 return handle_memory_read(packet);
2105 // case 'M':
2106 // return handle_memory_write(packet);
2107 case 'X':
2108 return handle_memory_binary_write(packet);
2109 case 'p':
2110 return handle_register_read(packet);
2111 case 'P':
2112 return handle_register_write(packet);
2113 case 'c':
2114 return handle_continue(packet);
2115 case 's':
2116 return handle_step(packet);
2117 case 'z':
2118 case 'Z':
2119 return handle_breakpoint(packet);
2120 case 'q':
2121 case 'Q':
2122 return handle_query(packet);
2123 }
2124
2125 // Not supported.
2126 D(fprintf(stderr, "** Unsupported packet: "));
2127 D(print_packet(packet));
2128 send_packet("");
2129 }
2130
2131 void gdbserver_t::handle_interrupt()
2132 {
2133 processor_t *p = sim->get_core(0);
2134 add_operation(new halt_op_t(*this, true));
2135 }
2136
2137 void gdbserver_t::handle()
2138 {
2139 if (client_fd > 0) {
2140 processor_t *p = sim->get_core(0);
2141
2142 bool interrupt = sim->debug_module.get_interrupt(0);
2143
2144 if (!interrupt && !operation_queue.empty()) {
2145 operation_t *operation = operation_queue.front();
2146 if (operation->step()) {
2147 operation_queue.pop();
2148 delete operation;
2149 }
2150 }
2151
2152 bool halt_notification = sim->debug_module.get_halt_notification(0);
2153 if (halt_notification) {
2154 sim->debug_module.clear_halt_notification(0);
2155 add_operation(new halt_op_t(*this, true));
2156 }
2157
2158 this->read();
2159 this->write();
2160
2161 } else {
2162 this->accept();
2163 }
2164
2165 if (operation_queue.empty()) {
2166 this->process_requests();
2167 }
2168 }
2169
2170 void gdbserver_t::send(const char* msg)
2171 {
2172 unsigned int length = strlen(msg);
2173 for (const char *c = msg; *c; c++)
2174 running_checksum += *c;
2175 send_buf.append((const uint8_t *) msg, length);
2176 }
2177
2178 void gdbserver_t::send(uint64_t value)
2179 {
2180 char buffer[3];
2181 for (unsigned int i = 0; i < 8; i++) {
2182 sprintf(buffer, "%02x", (int) (value & 0xff));
2183 send(buffer);
2184 value >>= 8;
2185 }
2186 }
2187
2188 void gdbserver_t::send(uint32_t value)
2189 {
2190 char buffer[3];
2191 for (unsigned int i = 0; i < 4; i++) {
2192 sprintf(buffer, "%02x", (int) (value & 0xff));
2193 send(buffer);
2194 value >>= 8;
2195 }
2196 }
2197
2198 void gdbserver_t::send(uint8_t value)
2199 {
2200 char buffer[3];
2201 sprintf(buffer, "%02x", (int) value);
2202 send(buffer);
2203 }
2204
2205 void gdbserver_t::send_packet(const char* data)
2206 {
2207 start_packet();
2208 send(data);
2209 end_packet();
2210 expect_ack = true;
2211 }
2212
2213 void gdbserver_t::start_packet()
2214 {
2215 send("$");
2216 running_checksum = 0;
2217 }
2218
2219 void gdbserver_t::end_packet(const char* data)
2220 {
2221 if (data) {
2222 send(data);
2223 }
2224
2225 char checksum_string[4];
2226 sprintf(checksum_string, "#%02x", running_checksum);
2227 send(checksum_string);
2228 expect_ack = true;
2229 }