6 #include <sys/socket.h>
18 #include "gdbserver.h"
21 #define C_EBREAK 0x9002
22 #define EBREAK 0x00100073
24 //////////////////////////////////////// Utility Functions
33 void die(const char* msg
)
35 fprintf(stderr
, "gdbserver code died: %s\n", msg
);
39 // gdb's register list is defined in riscv_gdb_reg_names gdb/riscv-tdep.c in
40 // its source tree. We must interpret the numbers the same here.
52 //////////////////////////////////////// Functions to generate RISC-V opcodes.
54 // TODO: Does this already exist somewhere?
57 // Using regnames.cc as source. The RVG Calling Convention of the 2.0 RISC-V
58 // spec says it should be 2 and 3.
61 static uint32_t bits(uint32_t value
, unsigned int hi
, unsigned int lo
) {
62 return (value
>> lo
) & ((1 << (hi
+1-lo
)) - 1);
65 static uint32_t bit(uint32_t value
, unsigned int b
) {
66 return (value
>> b
) & 1;
69 static uint32_t jal(unsigned int rd
, uint32_t imm
) {
70 return (bit(imm
, 20) << 31) |
71 (bits(imm
, 10, 1) << 21) |
72 (bit(imm
, 11) << 20) |
73 (bits(imm
, 19, 12) << 12) |
78 static uint32_t csrsi(unsigned int csr
, uint16_t imm
) {
80 (bits(imm
, 4, 0) << 15) |
84 static uint32_t csrci(unsigned int csr
, uint16_t imm
) {
86 (bits(imm
, 4, 0) << 15) |
90 static uint32_t csrr(unsigned int rd
, unsigned int csr
) {
91 return (csr
<< 20) | (rd
<< 7) | MATCH_CSRRS
;
94 static uint32_t csrw(unsigned int source
, unsigned int csr
) {
95 return (csr
<< 20) | (source
<< 15) | MATCH_CSRRW
;
98 static uint32_t fence_i()
100 return MATCH_FENCE_I
;
103 static uint32_t sb(unsigned int src
, unsigned int base
, uint16_t offset
)
105 return (bits(offset
, 11, 5) << 25) |
108 (bits(offset
, 4, 0) << 7) |
112 static uint32_t sh(unsigned int src
, unsigned int base
, uint16_t offset
)
114 return (bits(offset
, 11, 5) << 25) |
117 (bits(offset
, 4, 0) << 7) |
121 static uint32_t sw(unsigned int src
, unsigned int base
, uint16_t offset
)
123 return (bits(offset
, 11, 5) << 25) |
126 (bits(offset
, 4, 0) << 7) |
130 static uint32_t sd(unsigned int src
, unsigned int base
, uint16_t offset
)
132 return (bits(offset
, 11, 5) << 25) |
133 (bits(src
, 4, 0) << 20) |
135 (bits(offset
, 4, 0) << 7) |
139 static uint32_t sq(unsigned int src
, unsigned int base
, uint16_t offset
)
142 return (bits(offset
, 11, 5) << 25) |
143 (bits(src
, 4, 0) << 20) |
145 (bits(offset
, 4, 0) << 7) |
152 static uint32_t lq(unsigned int rd
, unsigned int base
, uint16_t offset
)
155 return (bits(offset
, 11, 0) << 20) |
157 (bits(rd
, 4, 0) << 7) |
164 static uint32_t ld(unsigned int rd
, unsigned int base
, uint16_t offset
)
166 return (bits(offset
, 11, 0) << 20) |
168 (bits(rd
, 4, 0) << 7) |
172 static uint32_t lw(unsigned int rd
, unsigned int base
, uint16_t offset
)
174 return (bits(offset
, 11, 0) << 20) |
176 (bits(rd
, 4, 0) << 7) |
180 static uint32_t lh(unsigned int rd
, unsigned int base
, uint16_t offset
)
182 return (bits(offset
, 11, 0) << 20) |
184 (bits(rd
, 4, 0) << 7) |
188 static uint32_t lb(unsigned int rd
, unsigned int base
, uint16_t offset
)
190 return (bits(offset
, 11, 0) << 20) |
192 (bits(rd
, 4, 0) << 7) |
196 static uint32_t fsw(unsigned int src
, unsigned int base
, uint16_t offset
)
198 return (bits(offset
, 11, 5) << 25) |
199 (bits(src
, 4, 0) << 20) |
201 (bits(offset
, 4, 0) << 7) |
205 static uint32_t fsd(unsigned int src
, unsigned int base
, uint16_t offset
)
207 return (bits(offset
, 11, 5) << 25) |
208 (bits(src
, 4, 0) << 20) |
210 (bits(offset
, 4, 0) << 7) |
214 static uint32_t flw(unsigned int src
, unsigned int base
, uint16_t offset
)
216 return (bits(offset
, 11, 5) << 25) |
217 (bits(src
, 4, 0) << 20) |
219 (bits(offset
, 4, 0) << 7) |
223 static uint32_t fld(unsigned int src
, unsigned int base
, uint16_t offset
)
225 return (bits(offset
, 11, 5) << 25) |
226 (bits(src
, 4, 0) << 20) |
228 (bits(offset
, 4, 0) << 7) |
232 static uint32_t addi(unsigned int dest
, unsigned int src
, uint16_t imm
)
234 return (bits(imm
, 11, 0) << 20) |
240 static uint32_t ori(unsigned int dest
, unsigned int src
, uint16_t imm
)
242 return (bits(imm
, 11, 0) << 20) |
248 static uint32_t xori(unsigned int dest
, unsigned int src
, uint16_t imm
)
250 return (bits(imm
, 11, 0) << 20) |
256 static uint32_t srli(unsigned int dest
, unsigned int src
, uint8_t shamt
)
258 return (bits(shamt
, 4, 0) << 20) |
265 static uint32_t nop()
267 return addi(0, 0, 0);
270 template <typename T
>
271 unsigned int circular_buffer_t
<T
>::size() const
276 return end
+ capacity
- start
;
279 template <typename T
>
280 void circular_buffer_t
<T
>::consume(unsigned int bytes
)
282 start
= (start
+ bytes
) % capacity
;
285 template <typename T
>
286 unsigned int circular_buffer_t
<T
>::contiguous_empty_size() const
290 return capacity
- end
- 1;
292 return capacity
- end
;
294 return start
- end
- 1;
297 template <typename T
>
298 unsigned int circular_buffer_t
<T
>::contiguous_data_size() const
303 return capacity
- start
;
306 template <typename T
>
307 void circular_buffer_t
<T
>::data_added(unsigned int bytes
)
310 assert(end
<= capacity
);
315 template <typename T
>
316 void circular_buffer_t
<T
>::reset()
322 template <typename T
>
323 void circular_buffer_t
<T
>::append(const T
*src
, unsigned int count
)
325 unsigned int copy
= std::min(count
, contiguous_empty_size());
326 memcpy(contiguous_empty(), src
, copy
* sizeof(T
));
330 assert(count
< contiguous_empty_size());
331 memcpy(contiguous_empty(), src
, count
* sizeof(T
));
336 ////////////////////////////// Debug Operations
338 class halt_op_t
: public operation_t
341 halt_op_t(gdbserver_t
& gdbserver
, bool send_status
=false) :
342 operation_t(gdbserver
), send_status(send_status
),
345 void write_dpc_program() {
346 gs
.dr_write32(0, csrsi(CSR_DCSR
, DCSR_HALT
));
347 gs
.dr_write32(1, csrr(S0
, CSR_DPC
));
348 gs
.dr_write_store(2, S0
, SLOT_DATA0
);
353 bool perform_step(unsigned int step
) {
355 gs
.tselect_valid
= false;
358 gs
.dr_write32(0, xori(S1
, ZERO
, -1));
359 gs
.dr_write32(1, srli(S1
, S1
, 31));
360 // 0x00000001 0x00000001:ffffffff 0x00000001:ffffffff:ffffffff:ffffffff
361 gs
.dr_write32(2, sw(S1
, ZERO
, DEBUG_RAM_START
));
362 gs
.dr_write32(3, srli(S1
, S1
, 31));
363 // 0x00000000 0x00000000:00000003 0x00000000:00000003:ffffffff:ffffffff
364 gs
.dr_write32(4, sw(S1
, ZERO
, DEBUG_RAM_START
+ 4));
377 uint32_t word0
= gs
.dr_read32(0);
378 uint32_t word1
= gs
.dr_read32(1);
380 if (word0
== 1 && word1
== 0) {
382 } else if (word0
== 0xffffffff && word1
== 3) {
384 } else if (word0
== 0xffffffff && word1
== 0xffffffff) {
394 gs
.dpc
= gs
.dr_read(SLOT_DATA0
);
395 gs
.dr_write32(0, csrr(S0
, CSR_MSTATUS
));
396 gs
.dr_write_store(1, S0
, SLOT_DATA0
);
403 gs
.mstatus
= gs
.dr_read(SLOT_DATA0
);
404 gs
.dr_write32(0, csrr(S0
, CSR_DCSR
));
405 gs
.dr_write32(1, sw(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
412 gs
.dcsr
= gs
.dr_read32(4);
414 gs
.sptbr_valid
= false;
415 gs
.pte_cache
.clear();
418 switch (get_field(gs
.dcsr
, DCSR_CAUSE
)) {
419 case DCSR_CAUSE_NONE
:
420 fprintf(stderr
, "Internal error. Processor halted without reason.\n");
423 case DCSR_CAUSE_DEBUGINT
:
424 gs
.send_packet("S02"); // Pretend program received SIGINT.
427 case DCSR_CAUSE_HWBP
:
428 case DCSR_CAUSE_STEP
:
429 case DCSR_CAUSE_HALT
:
430 // There's no gdb code for this.
431 gs
.send_packet("T05");
433 case DCSR_CAUSE_SWBP
:
434 gs
.send_packet("T05swbreak:;");
456 class continue_op_t
: public operation_t
459 continue_op_t(gdbserver_t
& gdbserver
, bool single_step
) :
460 operation_t(gdbserver
), single_step(single_step
) {};
462 bool perform_step(unsigned int step
) {
463 D(fprintf(stderr
, "continue step %d\n", step
));
466 gs
.dr_write_load(0, S0
, SLOT_DATA0
);
467 gs
.dr_write32(1, csrw(S0
, CSR_DPC
));
468 // TODO: Isn't there a fence.i in Debug ROM already?
469 if (gs
.fence_i_required
) {
470 gs
.dr_write32(2, fence_i());
472 gs
.fence_i_required
= false;
476 gs
.dr_write(SLOT_DATA0
, gs
.dpc
);
481 gs
.dr_write_load(0, S0
, SLOT_DATA0
);
482 gs
.dr_write32(1, csrw(S0
, CSR_MSTATUS
));
484 gs
.dr_write(SLOT_DATA0
, gs
.mstatus
);
489 gs
.dr_write32(0, lw(S0
, 0, (uint16_t) DEBUG_RAM_START
+16));
490 gs
.dr_write32(1, csrw(S0
, CSR_DCSR
));
493 reg_t dcsr
= set_field(gs
.dcsr
, DCSR_HALT
, 0);
494 dcsr
= set_field(dcsr
, DCSR_STEP
, single_step
);
495 // Software breakpoints should go here.
496 dcsr
= set_field(dcsr
, DCSR_EBREAKM
, 1);
497 dcsr
= set_field(dcsr
, DCSR_EBREAKH
, 1);
498 dcsr
= set_field(dcsr
, DCSR_EBREAKS
, 1);
499 dcsr
= set_field(dcsr
, DCSR_EBREAKU
, 1);
500 gs
.dr_write32(4, dcsr
);
512 class general_registers_read_op_t
: public operation_t
514 // Register order that gdb expects is:
515 // "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
516 // "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
517 // "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
518 // "x24", "x25", "x26", "x27", "x28", "x29", "x30", "x31",
520 // Each byte of register data is described by two hex digits. The bytes with
521 // the register are transmitted in target byte order. The size of each
522 // register and their position within the ‘g’ packet are determined by the
523 // gdb internal gdbarch functions DEPRECATED_REGISTER_RAW_SIZE and
524 // gdbarch_register_name.
527 general_registers_read_op_t(gdbserver_t
& gdbserver
) :
528 operation_t(gdbserver
) {};
530 bool perform_step(unsigned int step
)
532 D(fprintf(stderr
, "register_read step %d\n", step
));
536 // x0 is always zero.
538 gs
.send((uint32_t) 0);
540 gs
.send((uint64_t) 0);
543 gs
.dr_write_store(0, 1, SLOT_DATA0
);
544 gs
.dr_write_store(1, 2, SLOT_DATA1
);
551 gs
.send((uint32_t) gs
.dr_read(SLOT_DATA0
));
553 gs
.send((uint64_t) gs
.dr_read(SLOT_DATA0
));
561 gs
.send((uint32_t) gs
.dr_read(SLOT_DATA1
));
563 gs
.send((uint64_t) gs
.dr_read(SLOT_DATA1
));
566 unsigned int current_reg
= 2 * step
+ 1;
568 if (current_reg
== S1
) {
569 gs
.dr_write_load(i
++, S1
, SLOT_DATA_LAST
);
571 gs
.dr_write_store(i
++, current_reg
, SLOT_DATA0
);
572 if (current_reg
+ 1 == S0
) {
573 gs
.dr_write32(i
++, csrr(S0
, CSR_DSCRATCH
));
576 gs
.dr_write_store(i
++, current_reg
+1, SLOT_DATA1
);
585 class register_read_op_t
: public operation_t
588 register_read_op_t(gdbserver_t
& gdbserver
, unsigned int reg
) :
589 operation_t(gdbserver
), reg(reg
) {};
591 bool perform_step(unsigned int step
)
595 if (reg
>= REG_XPR0
&& reg
<= REG_XPR31
) {
597 gs
.dr_write32(0, sw(reg
- REG_XPR0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
599 gs
.dr_write32(0, sd(reg
- REG_XPR0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
602 } else if (reg
== REG_PC
) {
605 gs
.send((uint32_t) gs
.dpc
);
611 } else if (reg
>= REG_FPR0
&& reg
<= REG_FPR31
) {
612 // send(p->state.FPR[reg - REG_FPR0]);
614 gs
.dr_write32(0, fsw(reg
- REG_FPR0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
616 gs
.dr_write32(0, fsd(reg
- REG_FPR0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
619 } else if (reg
>= REG_CSR0
&& reg
<= REG_CSR4095
) {
620 gs
.dr_write32(0, csrr(S0
, reg
- REG_CSR0
));
621 gs
.dr_write_store(1, S0
, SLOT_DATA0
);
623 // If we hit an exception reading the CSR, we'll end up returning ~0 as
624 // the register's value, which is what we want. (Right?)
625 gs
.dr_write(SLOT_DATA0
, ~(uint64_t) 0);
626 } else if (reg
== REG_PRIV
) {
628 gs
.send((uint8_t) get_field(gs
.dcsr
, DCSR_PRV
));
632 gs
.send_packet("E02");
641 gs
.send(gs
.dr_read32(4));
643 gs
.send(gs
.dr_read(SLOT_DATA0
));
655 class register_write_op_t
: public operation_t
658 register_write_op_t(gdbserver_t
& gdbserver
, unsigned int reg
, reg_t value
) :
659 operation_t(gdbserver
), reg(reg
), value(value
) {};
661 bool perform_step(unsigned int step
)
663 gs
.dr_write_load(0, S0
, SLOT_DATA0
);
664 gs
.dr_write(SLOT_DATA0
, value
);
666 gs
.dr_write32(1, csrw(S0
, CSR_DSCRATCH
));
668 } else if (reg
== S1
) {
669 gs
.dr_write_store(1, S0
, SLOT_DATA_LAST
);
671 } else if (reg
>= REG_XPR0
&& reg
<= REG_XPR31
) {
672 gs
.dr_write32(1, addi(reg
, S0
, 0));
674 } else if (reg
== REG_PC
) {
677 } else if (reg
>= REG_FPR0
&& reg
<= REG_FPR31
) {
679 gs
.dr_write32(0, flw(reg
- REG_FPR0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
681 gs
.dr_write32(0, fld(reg
- REG_FPR0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
684 } else if (reg
>= REG_CSR0
&& reg
<= REG_CSR4095
) {
685 gs
.dr_write32(1, csrw(S0
, reg
- REG_CSR0
));
687 if (reg
== REG_CSR0
+ CSR_SPTBR
) {
689 gs
.sptbr_valid
= true;
691 } else if (reg
== REG_PRIV
) {
692 gs
.dcsr
= set_field(gs
.dcsr
, DCSR_PRV
, value
);
695 gs
.send_packet("E02");
699 gs
.send_packet("OK");
708 class memory_read_op_t
: public operation_t
711 // Read length bytes from vaddr, storing the result into data.
712 // If data is NULL, send the result straight to gdb.
713 memory_read_op_t(gdbserver_t
& gdbserver
, reg_t vaddr
, unsigned int length
,
714 unsigned char *data
=NULL
) :
715 operation_t(gdbserver
), vaddr(vaddr
), length(length
), data(data
), index(0)
717 buf
= new uint8_t[length
];
725 bool perform_step(unsigned int step
)
728 // address goes in S0
729 paddr
= gs
.translate(vaddr
);
730 access_size
= gs
.find_access_size(paddr
, length
);
732 gs
.dr_write_load(0, S0
, SLOT_DATA0
);
733 switch (access_size
) {
735 gs
.dr_write32(1, lb(S1
, S0
, 0));
738 gs
.dr_write32(1, lh(S1
, S0
, 0));
741 gs
.dr_write32(1, lw(S1
, S0
, 0));
744 gs
.dr_write32(1, ld(S1
, S0
, 0));
747 gs
.dr_write_store(2, S1
, SLOT_DATA1
);
749 gs
.dr_write(SLOT_DATA0
, paddr
);
755 if (gs
.dr_read32(DEBUG_RAM_SIZE
/ 4 - 1)) {
756 // Note that OpenOCD doesn't report this error to gdb by default. They
757 // think it can mess up stack tracing. So far I haven't seen any
759 gs
.send_packet("E99");
763 reg_t value
= gs
.dr_read(SLOT_DATA1
);
764 for (unsigned int i
= 0; i
< access_size
; i
++) {
766 *(data
++) = value
& 0xff;
767 D(fprintf(stderr
, "%02x", (unsigned int) (value
& 0xff)));
769 buf
[index
++] = value
& 0xff;
774 D(fprintf(stderr
, "\n"));
776 length
-= access_size
;
777 paddr
+= access_size
;
783 for (unsigned int i
= 0; i
< index
; i
++) {
784 sprintf(buffer
, "%02x", (unsigned int) buf
[i
]);
791 gs
.dr_write(SLOT_DATA0
, paddr
);
802 unsigned int access_size
;
807 class memory_write_op_t
: public operation_t
810 memory_write_op_t(gdbserver_t
& gdbserver
, reg_t vaddr
, unsigned int length
,
811 const unsigned char *data
) :
812 operation_t(gdbserver
), vaddr(vaddr
), offset(0), length(length
), data(data
) {};
814 ~memory_write_op_t() {
818 bool perform_step(unsigned int step
)
820 reg_t paddr
= gs
.translate(vaddr
);
822 unsigned int data_offset
;
825 data_offset
= slot_offset32
[SLOT_DATA1
];
828 data_offset
= slot_offset64
[SLOT_DATA1
];
831 data_offset
= slot_offset128
[SLOT_DATA1
];
838 access_size
= gs
.find_access_size(paddr
, length
);
840 D(fprintf(stderr
, "write to 0x%lx -> 0x%lx (access=%d): ", vaddr
, paddr
,
842 for (unsigned int i
= 0; i
< length
; i
++) {
843 D(fprintf(stderr
, "%02x", data
[i
]));
845 D(fprintf(stderr
, "\n"));
847 // address goes in S0
848 gs
.dr_write_load(0, S0
, SLOT_DATA0
);
849 switch (access_size
) {
851 gs
.dr_write32(1, lb(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 4*data_offset
));
852 gs
.dr_write32(2, sb(S1
, S0
, 0));
853 gs
.dr_write32(data_offset
, data
[0]);
856 gs
.dr_write32(1, lh(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 4*data_offset
));
857 gs
.dr_write32(2, sh(S1
, S0
, 0));
858 gs
.dr_write32(data_offset
, data
[0] | (data
[1] << 8));
861 gs
.dr_write32(1, lw(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 4*data_offset
));
862 gs
.dr_write32(2, sw(S1
, S0
, 0));
863 gs
.dr_write32(data_offset
, data
[0] | (data
[1] << 8) |
864 (data
[2] << 16) | (data
[3] << 24));
867 gs
.dr_write32(1, ld(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 4*data_offset
));
868 gs
.dr_write32(2, sd(S1
, S0
, 0));
869 gs
.dr_write32(data_offset
, data
[0] | (data
[1] << 8) |
870 (data
[2] << 16) | (data
[3] << 24));
871 gs
.dr_write32(data_offset
+1, data
[4] | (data
[5] << 8) |
872 (data
[6] << 16) | (data
[7] << 24));
875 fprintf(stderr
, "gdbserver error: write %d bytes to 0x%016" PRIx64
876 " -> 0x%016" PRIx64
"; access_size=%d\n",
877 length
, vaddr
, paddr
, access_size
);
878 gs
.send_packet("E12");
882 gs
.dr_write(SLOT_DATA0
, paddr
);
888 if (gs
.dr_read32(DEBUG_RAM_SIZE
/ 4 - 1)) {
889 gs
.send_packet("E98");
893 offset
+= access_size
;
894 if (offset
>= length
) {
895 gs
.send_packet("OK");
898 const unsigned char *d
= data
+ offset
;
899 switch (access_size
) {
901 gs
.dr_write32(data_offset
, d
[0]);
904 gs
.dr_write32(data_offset
, d
[0] | (d
[1] << 8));
907 gs
.dr_write32(data_offset
, d
[0] | (d
[1] << 8) |
908 (d
[2] << 16) | (d
[3] << 24));
911 gs
.dr_write32(data_offset
, d
[0] | (d
[1] << 8) |
912 (d
[2] << 16) | (d
[3] << 24));
913 gs
.dr_write32(data_offset
+1, d
[4] | (d
[5] << 8) |
914 (d
[6] << 16) | (d
[7] << 24));
917 gs
.send_packet("E13");
920 gs
.dr_write(SLOT_DATA0
, paddr
+ offset
);
930 unsigned int access_size
;
931 const unsigned char *data
;
934 class collect_translation_info_op_t
: public operation_t
937 // Read sufficient information from the target into gdbserver structures so
938 // that it's possible to translate vaddr, vaddr+length, and all addresses
939 // in between to physical addresses.
940 collect_translation_info_op_t(gdbserver_t
& gdbserver
, reg_t vaddr
, size_t length
) :
941 operation_t(gdbserver
), state(STATE_START
), vaddr(vaddr
), length(length
) {};
943 bool perform_step(unsigned int step
)
945 unsigned int vm
= gs
.virtual_memory();
950 // Nothing to be done.
972 sprintf(buf
, "VM mode %d is not supported by gdbserver.cc.", vm
);
974 return true; // die doesn't return, but gcc doesn't know that.
979 // Perform any reads from the just-completed action.
983 case STATE_READ_SPTBR
:
984 gs
.sptbr
= gs
.dr_read(SLOT_DATA0
);
985 gs
.sptbr_valid
= true;
989 gs
.pte_cache
[pte_addr
] = gs
.dr_read32(4);
991 gs
.pte_cache
[pte_addr
] = ((uint64_t) gs
.dr_read32(5) << 32) |
994 D(fprintf(stderr
, "pte_cache[0x%lx] = 0x%lx\n", pte_addr
, gs
.pte_cache
[pte_addr
]));
998 // Set up the next action.
999 // We only get here for VM_SV32/39/38.
1001 if (!gs
.sptbr_valid
) {
1002 state
= STATE_READ_SPTBR
;
1003 gs
.dr_write32(0, csrr(S0
, CSR_SPTBR
));
1004 gs
.dr_write_store(1, S0
, SLOT_DATA0
);
1005 gs
.dr_write_jump(2);
1006 gs
.set_interrupt(0);
1010 reg_t base
= gs
.sptbr
<< PGSHIFT
;
1011 int ptshift
= (levels
- 1) * ptidxbits
;
1012 for (unsigned int i
= 0; i
< levels
; i
++, ptshift
-= ptidxbits
) {
1013 reg_t idx
= (vaddr
>> (PGSHIFT
+ ptshift
)) & ((1 << ptidxbits
) - 1);
1015 pte_addr
= base
+ idx
* ptesize
;
1016 auto it
= gs
.pte_cache
.find(pte_addr
);
1017 if (it
== gs
.pte_cache
.end()) {
1018 state
= STATE_READ_PTE
;
1020 gs
.dr_write32(0, lw(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
1021 gs
.dr_write32(1, lw(S1
, S0
, 0));
1022 gs
.dr_write32(2, sw(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
1024 assert(gs
.xlen
>= 64);
1025 gs
.dr_write32(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
1026 gs
.dr_write32(1, ld(S1
, S0
, 0));
1027 gs
.dr_write32(2, sd(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
1029 gs
.dr_write32(3, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*3))));
1030 gs
.dr_write32(4, pte_addr
);
1031 gs
.dr_write32(5, pte_addr
>> 32);
1032 gs
.set_interrupt(0);
1036 reg_t pte
= gs
.pte_cache
[pte_addr
];
1037 reg_t ppn
= pte
>> PTE_PPN_SHIFT
;
1039 if (PTE_TABLE(pte
)) { // next level of page table
1040 base
= ppn
<< PGSHIFT
;
1042 // We've collected all the data required for the translation.
1047 "ERROR: gdbserver couldn't find appropriate PTEs to translate 0x%016" PRIx64
"\n",
1060 unsigned int levels
;
1061 unsigned int ptidxbits
;
1062 unsigned int ptesize
;
1066 class hardware_breakpoint_insert_op_t
: public operation_t
1069 hardware_breakpoint_insert_op_t(gdbserver_t
& gdbserver
,
1070 hardware_breakpoint_t bp
) :
1071 operation_t(gdbserver
), state(STATE_START
), bp(bp
) {};
1073 void write_new_index_program()
1075 gs
.dr_write_load(0, S0
, SLOT_DATA1
);
1076 gs
.dr_write32(1, csrw(S0
, CSR_TSELECT
));
1077 gs
.dr_write32(2, csrr(S0
, CSR_TSELECT
));
1078 gs
.dr_write_store(3, S0
, SLOT_DATA1
);
1079 gs
.dr_write_jump(4);
1080 gs
.dr_write(SLOT_DATA1
, bp
.index
);
1083 bool perform_step(unsigned int step
)
1088 write_new_index_program();
1089 state
= STATE_CHECK_INDEX
;
1092 case STATE_CHECK_INDEX
:
1093 if (gs
.dr_read(SLOT_DATA1
) != bp
.index
) {
1094 // We've exhausted breakpoints without finding an appropriate one.
1095 gs
.send_packet("E58");
1099 gs
.dr_write32(0, csrr(S0
, CSR_TDATA1
));
1100 gs
.dr_write_store(1, S0
, SLOT_DATA0
);
1101 gs
.dr_write_jump(2);
1102 state
= STATE_CHECK_MCONTROL
;
1105 case STATE_CHECK_MCONTROL
:
1107 reg_t mcontrol
= gs
.dr_read(SLOT_DATA0
);
1108 unsigned int type
= mcontrol
>> (gs
.xlen
- 4);
1110 // We've exhausted breakpoints without finding an appropriate one.
1111 gs
.send_packet("E58");
1116 !get_field(mcontrol
, MCONTROL_EXECUTE
) &&
1117 !get_field(mcontrol
, MCONTROL_LOAD
) &&
1118 !get_field(mcontrol
, MCONTROL_STORE
)) {
1119 // Found an unused trigger.
1120 gs
.dr_write_load(0, S0
, SLOT_DATA1
);
1121 gs
.dr_write32(1, csrw(S0
, CSR_TDATA1
));
1122 gs
.dr_write_jump(2);
1123 mcontrol
= set_field(0, MCONTROL_ACTION
, MCONTROL_ACTION_DEBUG_MODE
);
1124 mcontrol
= set_field(mcontrol
, MCONTROL_DMODE(gs
.xlen
), 1);
1125 mcontrol
= set_field(mcontrol
, MCONTROL_MATCH
, MCONTROL_MATCH_EQUAL
);
1126 mcontrol
= set_field(mcontrol
, MCONTROL_M
, 1);
1127 mcontrol
= set_field(mcontrol
, MCONTROL_H
, 1);
1128 mcontrol
= set_field(mcontrol
, MCONTROL_S
, 1);
1129 mcontrol
= set_field(mcontrol
, MCONTROL_U
, 1);
1130 mcontrol
= set_field(mcontrol
, MCONTROL_EXECUTE
, bp
.execute
);
1131 mcontrol
= set_field(mcontrol
, MCONTROL_LOAD
, bp
.load
);
1132 mcontrol
= set_field(mcontrol
, MCONTROL_STORE
, bp
.store
);
1133 // For store triggers it's nicer to fire just before the
1134 // instruction than just after. However, gdb doesn't clear the
1135 // breakpoints and step before resuming from a store trigger.
1136 // That means that without extra code, you'll keep hitting the
1137 // same watchpoint over and over again. That's not useful at all.
1138 // Instead of fixing this the right way, just set timing=1 for
1140 if (bp
.load
|| bp
.store
)
1141 mcontrol
= set_field(mcontrol
, MCONTROL_TIMING
, 1);
1143 gs
.dr_write(SLOT_DATA1
, mcontrol
);
1144 state
= STATE_WRITE_ADDRESS
;
1147 write_new_index_program();
1148 state
= STATE_CHECK_INDEX
;
1153 case STATE_WRITE_ADDRESS
:
1155 gs
.dr_write_load(0, S0
, SLOT_DATA1
);
1156 gs
.dr_write32(1, csrw(S0
, CSR_TDATA2
));
1157 gs
.dr_write_jump(2);
1158 gs
.dr_write(SLOT_DATA1
, bp
.vaddr
);
1159 gs
.set_interrupt(0);
1160 gs
.send_packet("OK");
1162 gs
.hardware_breakpoints
.insert(bp
);
1168 gs
.set_interrupt(0);
1176 STATE_CHECK_MCONTROL
,
1179 hardware_breakpoint_t bp
;
1182 class maybe_save_tselect_op_t
: public operation_t
1185 maybe_save_tselect_op_t(gdbserver_t
& gdbserver
) : operation_t(gdbserver
) {};
1186 bool perform_step(unsigned int step
) {
1187 if (gs
.tselect_valid
)
1192 gs
.dr_write32(0, csrr(S0
, CSR_TDATA1
));
1193 gs
.dr_write_store(1, S0
, SLOT_DATA0
);
1194 gs
.dr_write_jump(2);
1195 gs
.set_interrupt(0);
1198 gs
.tselect
= gs
.dr_read(SLOT_DATA0
);
1199 gs
.tselect_valid
= true;
1206 class maybe_restore_tselect_op_t
: public operation_t
1209 maybe_restore_tselect_op_t(gdbserver_t
& gdbserver
) : operation_t(gdbserver
) {};
1210 bool perform_step(unsigned int step
) {
1211 if (gs
.tselect_valid
) {
1212 gs
.dr_write_load(0, S0
, SLOT_DATA1
);
1213 gs
.dr_write32(1, csrw(S0
, CSR_TSELECT
));
1214 gs
.dr_write_jump(2);
1215 gs
.dr_write(SLOT_DATA1
, gs
.tselect
);
1221 class hardware_breakpoint_remove_op_t
: public operation_t
1224 hardware_breakpoint_remove_op_t(gdbserver_t
& gdbserver
,
1225 hardware_breakpoint_t bp
) :
1226 operation_t(gdbserver
), bp(bp
) {};
1228 bool perform_step(unsigned int step
) {
1229 gs
.dr_write32(0, addi(S0
, ZERO
, bp
.index
));
1230 gs
.dr_write32(1, csrw(S0
, CSR_TSELECT
));
1231 gs
.dr_write32(2, csrw(ZERO
, CSR_TDATA1
));
1232 gs
.dr_write_jump(3);
1233 gs
.set_interrupt(0);
1238 hardware_breakpoint_t bp
;
1241 ////////////////////////////// gdbserver itself
1243 gdbserver_t::gdbserver_t(uint16_t port
, sim_t
*sim
) :
1247 // gdb likes to send 0x100000 bytes at once when downloading.
1248 recv_buf(0x180000), send_buf(64 * 1024)
1250 socket_fd
= socket(AF_INET
, SOCK_STREAM
, 0);
1251 if (socket_fd
== -1) {
1252 fprintf(stderr
, "failed to make socket: %s (%d)\n", strerror(errno
), errno
);
1256 fcntl(socket_fd
, F_SETFL
, O_NONBLOCK
);
1258 if (setsockopt(socket_fd
, SOL_SOCKET
, SO_REUSEADDR
, &reuseaddr
,
1259 sizeof(int)) == -1) {
1260 fprintf(stderr
, "failed setsockopt: %s (%d)\n", strerror(errno
), errno
);
1264 struct sockaddr_in addr
;
1265 memset(&addr
, 0, sizeof(addr
));
1266 addr
.sin_family
= AF_INET
;
1267 addr
.sin_addr
.s_addr
= INADDR_ANY
;
1268 addr
.sin_port
= htons(port
);
1270 if (bind(socket_fd
, (struct sockaddr
*) &addr
, sizeof(addr
)) == -1) {
1271 fprintf(stderr
, "failed to bind socket: %s (%d)\n", strerror(errno
), errno
);
1275 if (listen(socket_fd
, 1) == -1) {
1276 fprintf(stderr
, "failed to listen on socket: %s (%d)\n", strerror(errno
), errno
);
1281 unsigned int gdbserver_t::find_access_size(reg_t address
, int length
)
1283 reg_t composite
= address
| length
;
1284 if ((composite
& 0x7) == 0 && xlen
>= 64)
1286 if ((composite
& 0x3) == 0)
1291 reg_t
gdbserver_t::translate(reg_t vaddr
)
1293 unsigned int vm
= virtual_memory();
1294 unsigned int levels
, ptidxbits
, ptesize
;
1319 sprintf(buf
, "VM mode %d is not supported by gdbserver.cc.", vm
);
1321 return true; // die doesn't return, but gcc doesn't know that.
1325 // Handle page tables here. There's a bunch of duplicated code with
1326 // collect_translation_info_op_t. :-(
1327 reg_t base
= sptbr
<< PGSHIFT
;
1328 int ptshift
= (levels
- 1) * ptidxbits
;
1329 for (unsigned int i
= 0; i
< levels
; i
++, ptshift
-= ptidxbits
) {
1330 reg_t idx
= (vaddr
>> (PGSHIFT
+ ptshift
)) & ((1 << ptidxbits
) - 1);
1332 reg_t pte_addr
= base
+ idx
* ptesize
;
1333 auto it
= pte_cache
.find(pte_addr
);
1334 if (it
== pte_cache
.end()) {
1335 fprintf(stderr
, "ERROR: gdbserver tried to translate 0x%016" PRIx64
1336 " without first collecting the relevant PTEs.\n", vaddr
);
1337 die("gdbserver_t::translate()");
1340 reg_t pte
= pte_cache
[pte_addr
];
1341 reg_t ppn
= pte
>> PTE_PPN_SHIFT
;
1343 if (PTE_TABLE(pte
)) { // next level of page table
1344 base
= ppn
<< PGSHIFT
;
1346 // We've collected all the data required for the translation.
1347 reg_t vpn
= vaddr
>> PGSHIFT
;
1348 reg_t paddr
= (ppn
| (vpn
& ((reg_t(1) << ptshift
) - 1))) << PGSHIFT
;
1349 paddr
+= vaddr
& (PGSIZE
-1);
1350 D(fprintf(stderr
, "gdbserver translate 0x%lx -> 0x%lx\n", vaddr
, paddr
));
1355 fprintf(stderr
, "ERROR: gdbserver tried to translate 0x%016" PRIx64
1356 " but the relevant PTEs are invalid.\n", vaddr
);
1357 // TODO: Is it better to throw an exception here?
1361 unsigned int gdbserver_t::privilege_mode()
1363 unsigned int mode
= get_field(dcsr
, DCSR_PRV
);
1364 if (get_field(mstatus
, MSTATUS_MPRV
))
1365 mode
= get_field(mstatus
, MSTATUS_MPP
);
1369 unsigned int gdbserver_t::virtual_memory()
1371 unsigned int mode
= privilege_mode();
1374 return get_field(mstatus
, MSTATUS_VM
);
1377 void gdbserver_t::dr_write32(unsigned int index
, uint32_t value
)
1379 sim
->debug_module
.ram_write32(index
, value
);
1382 void gdbserver_t::dr_write64(unsigned int index
, uint64_t value
)
1384 dr_write32(index
, value
);
1385 dr_write32(index
+1, value
>> 32);
1388 void gdbserver_t::dr_write(enum slot slot
, uint64_t value
)
1392 dr_write32(slot_offset32
[slot
], value
);
1395 dr_write64(slot_offset64
[slot
], value
);
1403 void gdbserver_t::dr_write_jump(unsigned int index
)
1405 dr_write32(index
, jal(0,
1406 (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*index
))));
1409 void gdbserver_t::dr_write_store(unsigned int index
, unsigned int reg
, enum slot slot
)
1411 assert(slot
!= SLOT_INST0
|| index
> 2);
1412 assert(slot
!= SLOT_DATA0
|| index
< 4 || index
> 6);
1413 assert(slot
!= SLOT_DATA1
|| index
< 5 || index
> 10);
1414 assert(slot
!= SLOT_DATA_LAST
|| index
< 6 || index
> 14);
1417 return dr_write32(index
,
1418 sw(reg
, 0, (uint16_t) DEBUG_RAM_START
+ 4 * slot_offset32
[slot
]));
1420 return dr_write32(index
,
1421 sd(reg
, 0, (uint16_t) DEBUG_RAM_START
+ 4 * slot_offset64
[slot
]));
1423 return dr_write32(index
,
1424 sq(reg
, 0, (uint16_t) DEBUG_RAM_START
+ 4 * slot_offset128
[slot
]));
1426 fprintf(stderr
, "xlen is %d!\n", xlen
);
1431 void gdbserver_t::dr_write_load(unsigned int index
, unsigned int reg
, enum slot slot
)
1435 return dr_write32(index
,
1436 lw(reg
, 0, (uint16_t) DEBUG_RAM_START
+ 4 * slot_offset32
[slot
]));
1438 return dr_write32(index
,
1439 ld(reg
, 0, (uint16_t) DEBUG_RAM_START
+ 4 * slot_offset64
[slot
]));
1441 return dr_write32(index
,
1442 lq(reg
, 0, (uint16_t) DEBUG_RAM_START
+ 4 * slot_offset128
[slot
]));
1444 fprintf(stderr
, "xlen is %d!\n", xlen
);
1449 uint32_t gdbserver_t::dr_read32(unsigned int index
)
1451 uint32_t value
= sim
->debug_module
.ram_read32(index
);
1452 D(fprintf(stderr
, "read32(%d) -> 0x%x\n", index
, value
));
1456 uint64_t gdbserver_t::dr_read64(unsigned int index
)
1458 return ((uint64_t) dr_read32(index
+1) << 32) | dr_read32(index
);
1461 uint64_t gdbserver_t::dr_read(enum slot slot
)
1465 return dr_read32(slot_offset32
[slot
]);
1467 return dr_read64(slot_offset64
[slot
]);
1475 void gdbserver_t::add_operation(operation_t
* operation
)
1477 operation_queue
.push(operation
);
1480 void gdbserver_t::accept()
1482 client_fd
= ::accept(socket_fd
, NULL
, NULL
);
1483 if (client_fd
== -1) {
1484 if (errno
== EAGAIN
) {
1485 // No client waiting to connect right now.
1487 fprintf(stderr
, "failed to accept on socket: %s (%d)\n", strerror(errno
),
1492 fcntl(client_fd
, F_SETFL
, O_NONBLOCK
);
1495 extended_mode
= false;
1497 // gdb wants the core to be halted when it attaches.
1498 add_operation(new halt_op_t(*this));
1502 void gdbserver_t::read()
1504 // Reading from a non-blocking socket still blocks if there is no data
1507 size_t count
= recv_buf
.contiguous_empty_size();
1508 ssize_t bytes
= ::read(client_fd
, recv_buf
.contiguous_empty(), count
);
1510 if (errno
== EAGAIN
) {
1511 // We'll try again the next call.
1513 fprintf(stderr
, "failed to read on socket: %s (%d)\n", strerror(errno
), errno
);
1516 } else if (bytes
== 0) {
1517 // The remote disconnected.
1519 processor_t
*p
= sim
->get_core(0);
1520 // TODO p->set_halted(false, HR_NONE);
1524 recv_buf
.data_added(bytes
);
1528 void gdbserver_t::write()
1530 if (send_buf
.empty())
1533 while (!send_buf
.empty()) {
1534 unsigned int count
= send_buf
.contiguous_data_size();
1536 ssize_t bytes
= ::write(client_fd
, send_buf
.contiguous_data(), count
);
1538 fprintf(stderr
, "failed to write to socket: %s (%d)\n", strerror(errno
), errno
);
1540 } else if (bytes
== 0) {
1541 // Client can't take any more data right now.
1544 D(fprintf(stderr
, "wrote %ld bytes: ", bytes
));
1545 for (unsigned int i
= 0; i
< bytes
; i
++) {
1546 D(fprintf(stderr
, "%c", send_buf
[i
]));
1548 D(fprintf(stderr
, "\n"));
1549 send_buf
.consume(bytes
);
1554 void print_packet(const std::vector
<uint8_t> &packet
)
1556 for (uint8_t c
: packet
) {
1557 if (c
>= ' ' and c
<= '~')
1558 fprintf(stderr
, "%c", c
);
1560 fprintf(stderr
, "\\x%02x", c
);
1562 fprintf(stderr
, "\n");
1565 uint8_t compute_checksum(const std::vector
<uint8_t> &packet
)
1567 uint8_t checksum
= 0;
1568 for (auto i
= packet
.begin() + 1; i
!= packet
.end() - 3; i
++ ) {
1574 uint8_t character_hex_value(uint8_t character
)
1576 if (character
>= '0' && character
<= '9')
1577 return character
- '0';
1578 if (character
>= 'a' && character
<= 'f')
1579 return 10 + character
- 'a';
1580 if (character
>= 'A' && character
<= 'F')
1581 return 10 + character
- 'A';
1585 uint8_t extract_checksum(const std::vector
<uint8_t> &packet
)
1587 return character_hex_value(*(packet
.end() - 1)) +
1588 16 * character_hex_value(*(packet
.end() - 2));
1591 void gdbserver_t::process_requests()
1593 // See https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html
1595 while (!recv_buf
.empty()) {
1596 std::vector
<uint8_t> packet
;
1597 for (unsigned int i
= 0; i
< recv_buf
.size(); i
++) {
1598 uint8_t b
= recv_buf
[i
];
1600 if (packet
.empty() && expect_ack
&& b
== '+') {
1601 recv_buf
.consume(1);
1605 if (packet
.empty() && b
== 3) {
1606 D(fprintf(stderr
, "Received interrupt\n"));
1607 recv_buf
.consume(1);
1613 // Start of new packet.
1614 if (!packet
.empty()) {
1615 fprintf(stderr
, "Received malformed %ld-byte packet from debug client: ",
1617 print_packet(packet
);
1618 recv_buf
.consume(i
);
1623 packet
.push_back(b
);
1625 // Packets consist of $<packet-data>#<checksum>
1626 // where <checksum> is
1627 if (packet
.size() >= 4 &&
1628 packet
[packet
.size()-3] == '#') {
1629 handle_packet(packet
);
1630 recv_buf
.consume(i
+1);
1634 // There's a partial packet in the buffer. Wait until we get more data to
1636 if (packet
.size()) {
1641 if (recv_buf
.full()) {
1643 "Receive buffer is full, but no complete packet was found!\n");
1644 for (unsigned line
= 0; line
< 8; line
++) {
1645 for (unsigned i
= 0; i
< 16; i
++) {
1646 fprintf(stderr
, "%02x ", recv_buf
.entry(line
* 16 + i
));
1648 for (unsigned i
= 0; i
< 16; i
++) {
1649 uint8_t e
= recv_buf
.entry(line
* 16 + i
);
1650 if (e
>= ' ' && e
<= '~')
1651 fprintf(stderr
, "%c", e
);
1653 fprintf(stderr
, ".");
1655 fprintf(stderr
, "\n");
1657 assert(!recv_buf
.full());
1661 void gdbserver_t::handle_halt_reason(const std::vector
<uint8_t> &packet
)
1666 void gdbserver_t::handle_general_registers_read(const std::vector
<uint8_t> &packet
)
1668 add_operation(new general_registers_read_op_t(*this));
1671 void gdbserver_t::set_interrupt(uint32_t hartid
) {
1672 sim
->debug_module
.set_interrupt(hartid
);
1675 // First byte is the most-significant one.
1676 // Eg. "08675309" becomes 0x08675309.
1677 uint64_t consume_hex_number(std::vector
<uint8_t>::const_iterator
&iter
,
1678 std::vector
<uint8_t>::const_iterator end
)
1682 while (iter
!= end
) {
1684 uint64_t c_value
= character_hex_value(c
);
1694 // First byte is the least-significant one.
1695 // Eg. "08675309" becomes 0x09536708
1696 uint64_t gdbserver_t::consume_hex_number_le(
1697 std::vector
<uint8_t>::const_iterator
&iter
,
1698 std::vector
<uint8_t>::const_iterator end
)
1701 unsigned int shift
= 4;
1703 while (iter
!= end
) {
1705 uint64_t c_value
= character_hex_value(c
);
1709 value
|= c_value
<< shift
;
1710 if ((shift
% 8) == 0)
1715 if (shift
>= xlen
) {
1717 "gdb sent too many data bytes. That means it thinks XLEN is greater than %d.\n"
1718 "To fix that, tell gdb: set arch riscv:rv%d\n",
1724 void consume_string(std::string
&str
, std::vector
<uint8_t>::const_iterator
&iter
,
1725 std::vector
<uint8_t>::const_iterator end
, uint8_t separator
)
1727 while (iter
!= end
&& *iter
!= separator
) {
1728 str
.append(1, (char) *iter
);
1733 void gdbserver_t::handle_register_read(const std::vector
<uint8_t> &packet
)
1737 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1738 unsigned int n
= consume_hex_number(iter
, packet
.end());
1740 return send_packet("E01");
1742 add_operation(new register_read_op_t(*this, n
));
1745 void gdbserver_t::handle_register_write(const std::vector
<uint8_t> &packet
)
1749 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1750 unsigned int n
= consume_hex_number(iter
, packet
.end());
1752 return send_packet("E05");
1755 reg_t value
= consume_hex_number_le(iter
, packet
.end());
1757 return send_packet("E06");
1759 processor_t
*p
= sim
->get_core(0);
1761 add_operation(new register_write_op_t(*this, n
, value
));
1763 return send_packet("OK");
1766 void gdbserver_t::handle_memory_read(const std::vector
<uint8_t> &packet
)
1769 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1770 reg_t address
= consume_hex_number(iter
, packet
.end());
1772 return send_packet("E10");
1774 reg_t length
= consume_hex_number(iter
, packet
.end());
1776 return send_packet("E11");
1778 add_operation(new collect_translation_info_op_t(*this, address
, length
));
1779 add_operation(new memory_read_op_t(*this, address
, length
));
1782 void gdbserver_t::handle_memory_binary_write(const std::vector
<uint8_t> &packet
)
1784 // X addr,length:XX...
1785 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1786 reg_t address
= consume_hex_number(iter
, packet
.end());
1788 return send_packet("E20");
1790 reg_t length
= consume_hex_number(iter
, packet
.end());
1792 return send_packet("E21");
1796 return send_packet("OK");
1799 unsigned char *data
= new unsigned char[length
];
1800 for (unsigned int i
= 0; i
< length
; i
++) {
1801 if (iter
== packet
.end()) {
1802 return send_packet("E22");
1807 // The binary data representation uses 7d (ascii ‘}’) as an escape
1808 // character. Any escaped byte is transmitted as the escape character
1809 // followed by the original character XORed with 0x20. For example, the
1810 // byte 0x7d would be transmitted as the two bytes 0x7d 0x5d. The bytes
1811 // 0x23 (ascii ‘#’), 0x24 (ascii ‘$’), and 0x7d (ascii ‘}’) must always
1813 if (iter
== packet
.end()) {
1814 return send_packet("E23");
1822 return send_packet("E4b"); // EOVERFLOW
1824 add_operation(new collect_translation_info_op_t(*this, address
, length
));
1825 add_operation(new memory_write_op_t(*this, address
, length
, data
));
1828 void gdbserver_t::handle_continue(const std::vector
<uint8_t> &packet
)
1831 processor_t
*p
= sim
->get_core(0);
1832 if (packet
[2] != '#') {
1833 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1834 dpc
= consume_hex_number(iter
, packet
.end());
1836 return send_packet("E30");
1839 add_operation(new maybe_restore_tselect_op_t(*this));
1840 add_operation(new continue_op_t(*this, false));
1843 void gdbserver_t::handle_step(const std::vector
<uint8_t> &packet
)
1846 if (packet
[2] != '#') {
1847 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1849 //p->state.pc = consume_hex_number(iter, packet.end());
1851 return send_packet("E40");
1854 add_operation(new maybe_restore_tselect_op_t(*this));
1855 add_operation(new continue_op_t(*this, true));
1858 void gdbserver_t::handle_kill(const std::vector
<uint8_t> &packet
)
1861 // The exact effect of this packet is not specified.
1862 // Looks like OpenOCD disconnects?
1866 void gdbserver_t::handle_extended(const std::vector
<uint8_t> &packet
)
1868 // Enable extended mode. In extended mode, the remote server is made
1869 // persistent. The ‘R’ packet is used to restart the program being debugged.
1871 extended_mode
= true;
1874 void gdbserver_t::software_breakpoint_insert(reg_t vaddr
, unsigned int size
)
1876 fence_i_required
= true;
1877 add_operation(new collect_translation_info_op_t(*this, vaddr
, size
));
1878 unsigned char* inst
= new unsigned char[4];
1880 inst
[0] = C_EBREAK
& 0xff;
1881 inst
[1] = (C_EBREAK
>> 8) & 0xff;
1883 inst
[0] = EBREAK
& 0xff;
1884 inst
[1] = (EBREAK
>> 8) & 0xff;
1885 inst
[2] = (EBREAK
>> 16) & 0xff;
1886 inst
[3] = (EBREAK
>> 24) & 0xff;
1889 software_breakpoint_t bp
= {
1893 software_breakpoints
[vaddr
] = bp
;
1894 add_operation(new memory_read_op_t(*this, bp
.vaddr
, bp
.size
,
1895 software_breakpoints
[bp
.vaddr
].instruction
));
1896 add_operation(new memory_write_op_t(*this, bp
.vaddr
, bp
.size
, inst
));
1899 void gdbserver_t::software_breakpoint_remove(reg_t vaddr
, unsigned int size
)
1901 fence_i_required
= true;
1902 add_operation(new collect_translation_info_op_t(*this, vaddr
, size
));
1904 software_breakpoint_t found_bp
= software_breakpoints
[vaddr
];
1905 unsigned char* instruction
= new unsigned char[4];
1906 memcpy(instruction
, found_bp
.instruction
, 4);
1907 add_operation(new memory_write_op_t(*this, found_bp
.vaddr
,
1908 found_bp
.size
, instruction
));
1909 software_breakpoints
.erase(vaddr
);
1912 void gdbserver_t::hardware_breakpoint_insert(const hardware_breakpoint_t
&bp
)
1914 add_operation(new maybe_save_tselect_op_t(*this));
1915 add_operation(new hardware_breakpoint_insert_op_t(*this, bp
));
1918 void gdbserver_t::hardware_breakpoint_remove(const hardware_breakpoint_t
&bp
)
1920 add_operation(new maybe_save_tselect_op_t(*this));
1921 hardware_breakpoint_t found
= *hardware_breakpoints
.find(bp
);
1922 add_operation(new hardware_breakpoint_remove_op_t(*this, found
));
1925 void gdbserver_t::handle_breakpoint(const std::vector
<uint8_t> &packet
)
1927 // insert: Z type,addr,length
1928 // remove: z type,addr,length
1930 // type: 0 - software breakpoint, 1 - hardware breakpoint, 2 - write
1931 // watchpoint, 3 - read watchpoint, 4 - access watchpoint; addr is address;
1932 // length is in bytes. For a software breakpoint, length specifies the size
1933 // of the instruction to be patched. For hardware breakpoints and watchpoints
1934 // length specifies the memory region to be monitored. To avoid potential
1935 // problems with duplicate packets, the operations should be implemented in
1936 // an idempotent way.
1938 bool insert
= (packet
[1] == 'Z');
1939 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1940 gdb_breakpoint_type_t type
= static_cast<gdb_breakpoint_type_t
>(
1941 consume_hex_number(iter
, packet
.end()));
1943 return send_packet("E50");
1945 reg_t address
= consume_hex_number(iter
, packet
.end());
1947 return send_packet("E51");
1949 unsigned int size
= consume_hex_number(iter
, packet
.end());
1950 // There may be more options after a ; here, but we don't support that.
1952 return send_packet("E52");
1956 if (size
!= 2 && size
!= 4) {
1957 return send_packet("E53");
1960 software_breakpoint_insert(address
, size
);
1962 software_breakpoint_remove(address
, size
);
1971 hardware_breakpoint_t bp
= {
1975 bp
.load
= (type
== GB_READ
|| type
== GB_ACCESS
);
1976 bp
.store
= (type
== GB_WRITE
|| type
== GB_ACCESS
);
1977 bp
.execute
= (type
== GB_HARDWARE
|| type
== GB_ACCESS
);
1979 hardware_breakpoint_insert(bp
);
1980 // Insert might fail if there's no space, so the insert operation will
1981 // send its own OK (or not).
1984 hardware_breakpoint_remove(bp
);
1990 return send_packet("E56");
1993 return send_packet("OK");
1996 void gdbserver_t::handle_query(const std::vector
<uint8_t> &packet
)
1999 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
2001 consume_string(name
, iter
, packet
.end(), ':');
2002 if (iter
!= packet
.end())
2004 if (name
== "Supported") {
2006 while (iter
!= packet
.end()) {
2007 std::string feature
;
2008 consume_string(feature
, iter
, packet
.end(), ';');
2009 if (iter
!= packet
.end())
2011 if (feature
== "swbreak+") {
2015 send("PacketSize=131072;");
2016 return end_packet();
2019 D(fprintf(stderr
, "Unsupported query %s\n", name
.c_str()));
2020 return send_packet("");
2023 void gdbserver_t::handle_packet(const std::vector
<uint8_t> &packet
)
2025 if (compute_checksum(packet
) != extract_checksum(packet
)) {
2026 fprintf(stderr
, "Received %ld-byte packet with invalid checksum\n", packet
.size());
2027 fprintf(stderr
, "Computed checksum: %x\n", compute_checksum(packet
));
2028 print_packet(packet
);
2033 D(fprintf(stderr
, "Received %ld-byte packet from debug client: ", packet
.size()));
2034 D(print_packet(packet
));
2037 switch (packet
[1]) {
2039 return handle_extended(packet
);
2041 return handle_halt_reason(packet
);
2043 return handle_general_registers_read(packet
);
2045 // return handle_kill(packet);
2047 return handle_memory_read(packet
);
2049 // return handle_memory_write(packet);
2051 return handle_memory_binary_write(packet
);
2053 return handle_register_read(packet
);
2055 return handle_register_write(packet
);
2057 return handle_continue(packet
);
2059 return handle_step(packet
);
2062 return handle_breakpoint(packet
);
2065 return handle_query(packet
);
2069 D(fprintf(stderr
, "** Unsupported packet: "));
2070 D(print_packet(packet
));
2074 void gdbserver_t::handle_interrupt()
2076 processor_t
*p
= sim
->get_core(0);
2077 add_operation(new halt_op_t(*this, true));
2080 void gdbserver_t::handle()
2082 if (client_fd
> 0) {
2083 processor_t
*p
= sim
->get_core(0);
2085 bool interrupt
= sim
->debug_module
.get_interrupt(0);
2087 if (!interrupt
&& !operation_queue
.empty()) {
2088 operation_t
*operation
= operation_queue
.front();
2089 if (operation
->step()) {
2090 operation_queue
.pop();
2095 bool halt_notification
= sim
->debug_module
.get_halt_notification(0);
2096 if (halt_notification
) {
2097 sim
->debug_module
.clear_halt_notification(0);
2098 add_operation(new halt_op_t(*this, true));
2108 if (operation_queue
.empty()) {
2109 this->process_requests();
2113 void gdbserver_t::send(const char* msg
)
2115 unsigned int length
= strlen(msg
);
2116 for (const char *c
= msg
; *c
; c
++)
2117 running_checksum
+= *c
;
2118 send_buf
.append((const uint8_t *) msg
, length
);
2121 void gdbserver_t::send(uint64_t value
)
2124 for (unsigned int i
= 0; i
< 8; i
++) {
2125 sprintf(buffer
, "%02x", (int) (value
& 0xff));
2131 void gdbserver_t::send(uint32_t value
)
2134 for (unsigned int i
= 0; i
< 4; i
++) {
2135 sprintf(buffer
, "%02x", (int) (value
& 0xff));
2141 void gdbserver_t::send(uint8_t value
)
2144 sprintf(buffer
, "%02x", (int) value
);
2148 void gdbserver_t::send_packet(const char* data
)
2156 void gdbserver_t::start_packet()
2159 running_checksum
= 0;
2162 void gdbserver_t::end_packet(const char* data
)
2168 char checksum_string
[4];
2169 sprintf(checksum_string
, "#%02x", running_checksum
);
2170 send(checksum_string
);