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");
640 unsigned result
= gs
.dr_read(SLOT_DATA_LAST
);
642 gs
.send_packet("E03");
647 gs
.send(gs
.dr_read32(4));
649 gs
.send(gs
.dr_read(SLOT_DATA0
));
662 class register_write_op_t
: public operation_t
665 register_write_op_t(gdbserver_t
& gdbserver
, unsigned int reg
, reg_t value
) :
666 operation_t(gdbserver
), reg(reg
), value(value
) {};
668 bool perform_step(unsigned int step
)
672 gs
.dr_write_load(0, S0
, SLOT_DATA0
);
673 gs
.dr_write(SLOT_DATA0
, value
);
675 gs
.dr_write32(1, csrw(S0
, CSR_DSCRATCH
));
677 } else if (reg
== S1
) {
678 gs
.dr_write_store(1, S0
, SLOT_DATA_LAST
);
680 } else if (reg
>= REG_XPR0
&& reg
<= REG_XPR31
) {
681 gs
.dr_write32(1, addi(reg
, S0
, 0));
683 } else if (reg
== REG_PC
) {
686 } else if (reg
>= REG_FPR0
&& reg
<= REG_FPR31
) {
688 gs
.dr_write32(0, flw(reg
- REG_FPR0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
690 gs
.dr_write32(0, fld(reg
- REG_FPR0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
693 } else if (reg
>= REG_CSR0
&& reg
<= REG_CSR4095
) {
694 gs
.dr_write32(1, csrw(S0
, reg
- REG_CSR0
));
696 if (reg
== REG_CSR0
+ CSR_SPTBR
) {
698 gs
.sptbr_valid
= true;
700 } else if (reg
== REG_PRIV
) {
701 gs
.dcsr
= set_field(gs
.dcsr
, DCSR_PRV
, value
);
704 gs
.send_packet("E02");
712 unsigned result
= gs
.dr_read(SLOT_DATA_LAST
);
714 gs
.send_packet("E03");
717 gs
.send_packet("OK");
730 class memory_read_op_t
: public operation_t
733 // Read length bytes from vaddr, storing the result into data.
734 // If data is NULL, send the result straight to gdb.
735 memory_read_op_t(gdbserver_t
& gdbserver
, reg_t vaddr
, unsigned int length
,
736 unsigned char *data
=NULL
) :
737 operation_t(gdbserver
), vaddr(vaddr
), length(length
), data(data
), index(0)
739 buf
= new uint8_t[length
];
747 bool perform_step(unsigned int step
)
750 // address goes in S0
751 paddr
= gs
.translate(vaddr
);
752 access_size
= gs
.find_access_size(paddr
, length
);
754 gs
.dr_write_load(0, S0
, SLOT_DATA0
);
755 switch (access_size
) {
757 gs
.dr_write32(1, lb(S1
, S0
, 0));
760 gs
.dr_write32(1, lh(S1
, S0
, 0));
763 gs
.dr_write32(1, lw(S1
, S0
, 0));
766 gs
.dr_write32(1, ld(S1
, S0
, 0));
769 gs
.dr_write_store(2, S1
, SLOT_DATA1
);
771 gs
.dr_write(SLOT_DATA0
, paddr
);
777 if (gs
.dr_read32(DEBUG_RAM_SIZE
/ 4 - 1)) {
778 // Note that OpenOCD doesn't report this error to gdb by default. They
779 // think it can mess up stack tracing. So far I haven't seen any
781 gs
.send_packet("E99");
785 reg_t value
= gs
.dr_read(SLOT_DATA1
);
786 for (unsigned int i
= 0; i
< access_size
; i
++) {
788 *(data
++) = value
& 0xff;
789 D(fprintf(stderr
, "%02x", (unsigned int) (value
& 0xff)));
791 buf
[index
++] = value
& 0xff;
796 D(fprintf(stderr
, "\n"));
798 length
-= access_size
;
799 paddr
+= access_size
;
805 for (unsigned int i
= 0; i
< index
; i
++) {
806 sprintf(buffer
, "%02x", (unsigned int) buf
[i
]);
813 gs
.dr_write(SLOT_DATA0
, paddr
);
824 unsigned int access_size
;
829 class memory_write_op_t
: public operation_t
832 memory_write_op_t(gdbserver_t
& gdbserver
, reg_t vaddr
, unsigned int length
,
833 const unsigned char *data
) :
834 operation_t(gdbserver
), vaddr(vaddr
), offset(0), length(length
), data(data
) {};
836 ~memory_write_op_t() {
840 bool perform_step(unsigned int step
)
842 reg_t paddr
= gs
.translate(vaddr
);
844 unsigned int data_offset
;
847 data_offset
= slot_offset32
[SLOT_DATA1
];
850 data_offset
= slot_offset64
[SLOT_DATA1
];
853 data_offset
= slot_offset128
[SLOT_DATA1
];
860 access_size
= gs
.find_access_size(paddr
, length
);
862 D(fprintf(stderr
, "write to 0x%lx -> 0x%lx (access=%d): ", vaddr
, paddr
,
864 for (unsigned int i
= 0; i
< length
; i
++) {
865 D(fprintf(stderr
, "%02x", data
[i
]));
867 D(fprintf(stderr
, "\n"));
869 // address goes in S0
870 gs
.dr_write_load(0, S0
, SLOT_DATA0
);
871 switch (access_size
) {
873 gs
.dr_write32(1, lb(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 4*data_offset
));
874 gs
.dr_write32(2, sb(S1
, S0
, 0));
875 gs
.dr_write32(data_offset
, data
[0]);
878 gs
.dr_write32(1, lh(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 4*data_offset
));
879 gs
.dr_write32(2, sh(S1
, S0
, 0));
880 gs
.dr_write32(data_offset
, data
[0] | (data
[1] << 8));
883 gs
.dr_write32(1, lw(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 4*data_offset
));
884 gs
.dr_write32(2, sw(S1
, S0
, 0));
885 gs
.dr_write32(data_offset
, data
[0] | (data
[1] << 8) |
886 (data
[2] << 16) | (data
[3] << 24));
889 gs
.dr_write32(1, ld(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 4*data_offset
));
890 gs
.dr_write32(2, sd(S1
, S0
, 0));
891 gs
.dr_write32(data_offset
, data
[0] | (data
[1] << 8) |
892 (data
[2] << 16) | (data
[3] << 24));
893 gs
.dr_write32(data_offset
+1, data
[4] | (data
[5] << 8) |
894 (data
[6] << 16) | (data
[7] << 24));
897 fprintf(stderr
, "gdbserver error: write %d bytes to 0x%016" PRIx64
898 " -> 0x%016" PRIx64
"; access_size=%d\n",
899 length
, vaddr
, paddr
, access_size
);
900 gs
.send_packet("E12");
904 gs
.dr_write(SLOT_DATA0
, paddr
);
910 if (gs
.dr_read32(DEBUG_RAM_SIZE
/ 4 - 1)) {
911 gs
.send_packet("E98");
915 offset
+= access_size
;
916 if (offset
>= length
) {
917 gs
.send_packet("OK");
920 const unsigned char *d
= data
+ offset
;
921 switch (access_size
) {
923 gs
.dr_write32(data_offset
, d
[0]);
926 gs
.dr_write32(data_offset
, d
[0] | (d
[1] << 8));
929 gs
.dr_write32(data_offset
, d
[0] | (d
[1] << 8) |
930 (d
[2] << 16) | (d
[3] << 24));
933 gs
.dr_write32(data_offset
, d
[0] | (d
[1] << 8) |
934 (d
[2] << 16) | (d
[3] << 24));
935 gs
.dr_write32(data_offset
+1, d
[4] | (d
[5] << 8) |
936 (d
[6] << 16) | (d
[7] << 24));
939 gs
.send_packet("E13");
942 gs
.dr_write(SLOT_DATA0
, paddr
+ offset
);
952 unsigned int access_size
;
953 const unsigned char *data
;
956 class collect_translation_info_op_t
: public operation_t
959 // Read sufficient information from the target into gdbserver structures so
960 // that it's possible to translate vaddr, vaddr+length, and all addresses
961 // in between to physical addresses.
962 collect_translation_info_op_t(gdbserver_t
& gdbserver
, reg_t vaddr
, size_t length
) :
963 operation_t(gdbserver
), state(STATE_START
), vaddr(vaddr
), length(length
) {};
965 bool perform_step(unsigned int step
)
967 unsigned int vm
= gs
.virtual_memory();
972 // Nothing to be done.
994 sprintf(buf
, "VM mode %d is not supported by gdbserver.cc.", vm
);
996 return true; // die doesn't return, but gcc doesn't know that.
1001 // Perform any reads from the just-completed action.
1005 case STATE_READ_SPTBR
:
1006 gs
.sptbr
= gs
.dr_read(SLOT_DATA0
);
1007 gs
.sptbr_valid
= true;
1009 case STATE_READ_PTE
:
1011 gs
.pte_cache
[pte_addr
] = gs
.dr_read32(4);
1013 gs
.pte_cache
[pte_addr
] = ((uint64_t) gs
.dr_read32(5) << 32) |
1016 D(fprintf(stderr
, "pte_cache[0x%lx] = 0x%lx\n", pte_addr
, gs
.pte_cache
[pte_addr
]));
1020 // Set up the next action.
1021 // We only get here for VM_SV32/39/38.
1023 if (!gs
.sptbr_valid
) {
1024 state
= STATE_READ_SPTBR
;
1025 gs
.dr_write32(0, csrr(S0
, CSR_SPTBR
));
1026 gs
.dr_write_store(1, S0
, SLOT_DATA0
);
1027 gs
.dr_write_jump(2);
1028 gs
.set_interrupt(0);
1032 reg_t base
= gs
.sptbr
<< PGSHIFT
;
1033 int ptshift
= (levels
- 1) * ptidxbits
;
1034 for (unsigned int i
= 0; i
< levels
; i
++, ptshift
-= ptidxbits
) {
1035 reg_t idx
= (vaddr
>> (PGSHIFT
+ ptshift
)) & ((1 << ptidxbits
) - 1);
1037 pte_addr
= base
+ idx
* ptesize
;
1038 auto it
= gs
.pte_cache
.find(pte_addr
);
1039 if (it
== gs
.pte_cache
.end()) {
1040 state
= STATE_READ_PTE
;
1042 gs
.dr_write32(0, lw(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
1043 gs
.dr_write32(1, lw(S1
, S0
, 0));
1044 gs
.dr_write32(2, sw(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
1046 assert(gs
.xlen
>= 64);
1047 gs
.dr_write32(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
1048 gs
.dr_write32(1, ld(S1
, S0
, 0));
1049 gs
.dr_write32(2, sd(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
1051 gs
.dr_write_jump(3);
1052 gs
.dr_write32(4, pte_addr
);
1053 gs
.dr_write32(5, pte_addr
>> 32);
1054 gs
.set_interrupt(0);
1058 reg_t pte
= gs
.pte_cache
[pte_addr
];
1059 reg_t ppn
= pte
>> PTE_PPN_SHIFT
;
1061 if (PTE_TABLE(pte
)) { // next level of page table
1062 base
= ppn
<< PGSHIFT
;
1064 // We've collected all the data required for the translation.
1069 "ERROR: gdbserver couldn't find appropriate PTEs to translate 0x%016" PRIx64
"\n",
1082 unsigned int levels
;
1083 unsigned int ptidxbits
;
1084 unsigned int ptesize
;
1088 class hardware_breakpoint_insert_op_t
: public operation_t
1091 hardware_breakpoint_insert_op_t(gdbserver_t
& gdbserver
,
1092 hardware_breakpoint_t bp
) :
1093 operation_t(gdbserver
), state(STATE_START
), bp(bp
) {};
1095 void write_new_index_program()
1097 gs
.dr_write_load(0, S0
, SLOT_DATA1
);
1098 gs
.dr_write32(1, csrw(S0
, CSR_TSELECT
));
1099 gs
.dr_write32(2, csrr(S0
, CSR_TSELECT
));
1100 gs
.dr_write_store(3, S0
, SLOT_DATA1
);
1101 gs
.dr_write_jump(4);
1102 gs
.dr_write(SLOT_DATA1
, bp
.index
);
1105 bool perform_step(unsigned int step
)
1110 write_new_index_program();
1111 state
= STATE_CHECK_INDEX
;
1114 case STATE_CHECK_INDEX
:
1115 if (gs
.dr_read(SLOT_DATA1
) != bp
.index
) {
1116 // We've exhausted breakpoints without finding an appropriate one.
1117 gs
.send_packet("E58");
1121 gs
.dr_write32(0, csrr(S0
, CSR_TDATA1
));
1122 gs
.dr_write_store(1, S0
, SLOT_DATA0
);
1123 gs
.dr_write_jump(2);
1124 state
= STATE_CHECK_MCONTROL
;
1127 case STATE_CHECK_MCONTROL
:
1129 reg_t mcontrol
= gs
.dr_read(SLOT_DATA0
);
1130 unsigned int type
= mcontrol
>> (gs
.xlen
- 4);
1132 // We've exhausted breakpoints without finding an appropriate one.
1133 gs
.send_packet("E58");
1138 !get_field(mcontrol
, MCONTROL_EXECUTE
) &&
1139 !get_field(mcontrol
, MCONTROL_LOAD
) &&
1140 !get_field(mcontrol
, MCONTROL_STORE
)) {
1141 // Found an unused trigger.
1142 gs
.dr_write_load(0, S0
, SLOT_DATA1
);
1143 gs
.dr_write32(1, csrw(S0
, CSR_TDATA1
));
1144 gs
.dr_write_jump(2);
1145 mcontrol
= set_field(0, MCONTROL_ACTION
, MCONTROL_ACTION_DEBUG_MODE
);
1146 mcontrol
= set_field(mcontrol
, MCONTROL_DMODE(gs
.xlen
), 1);
1147 mcontrol
= set_field(mcontrol
, MCONTROL_MATCH
, MCONTROL_MATCH_EQUAL
);
1148 mcontrol
= set_field(mcontrol
, MCONTROL_M
, 1);
1149 mcontrol
= set_field(mcontrol
, MCONTROL_H
, 1);
1150 mcontrol
= set_field(mcontrol
, MCONTROL_S
, 1);
1151 mcontrol
= set_field(mcontrol
, MCONTROL_U
, 1);
1152 mcontrol
= set_field(mcontrol
, MCONTROL_EXECUTE
, bp
.execute
);
1153 mcontrol
= set_field(mcontrol
, MCONTROL_LOAD
, bp
.load
);
1154 mcontrol
= set_field(mcontrol
, MCONTROL_STORE
, bp
.store
);
1155 // For store triggers it's nicer to fire just before the
1156 // instruction than just after. However, gdb doesn't clear the
1157 // breakpoints and step before resuming from a store trigger.
1158 // That means that without extra code, you'll keep hitting the
1159 // same watchpoint over and over again. That's not useful at all.
1160 // Instead of fixing this the right way, just set timing=1 for
1162 if (bp
.load
|| bp
.store
)
1163 mcontrol
= set_field(mcontrol
, MCONTROL_TIMING
, 1);
1165 gs
.dr_write(SLOT_DATA1
, mcontrol
);
1166 state
= STATE_WRITE_ADDRESS
;
1169 write_new_index_program();
1170 state
= STATE_CHECK_INDEX
;
1175 case STATE_WRITE_ADDRESS
:
1177 gs
.dr_write_load(0, S0
, SLOT_DATA1
);
1178 gs
.dr_write32(1, csrw(S0
, CSR_TDATA2
));
1179 gs
.dr_write_jump(2);
1180 gs
.dr_write(SLOT_DATA1
, bp
.vaddr
);
1181 gs
.set_interrupt(0);
1182 gs
.send_packet("OK");
1184 gs
.hardware_breakpoints
.insert(bp
);
1190 gs
.set_interrupt(0);
1198 STATE_CHECK_MCONTROL
,
1201 hardware_breakpoint_t bp
;
1204 class maybe_save_tselect_op_t
: public operation_t
1207 maybe_save_tselect_op_t(gdbserver_t
& gdbserver
) : operation_t(gdbserver
) {};
1208 bool perform_step(unsigned int step
) {
1209 if (gs
.tselect_valid
)
1214 gs
.dr_write32(0, csrr(S0
, CSR_TDATA1
));
1215 gs
.dr_write_store(1, S0
, SLOT_DATA0
);
1216 gs
.dr_write_jump(2);
1217 gs
.set_interrupt(0);
1220 gs
.tselect
= gs
.dr_read(SLOT_DATA0
);
1221 gs
.tselect_valid
= true;
1228 class maybe_restore_tselect_op_t
: public operation_t
1231 maybe_restore_tselect_op_t(gdbserver_t
& gdbserver
) : operation_t(gdbserver
) {};
1232 bool perform_step(unsigned int step
) {
1233 if (gs
.tselect_valid
) {
1234 gs
.dr_write_load(0, S0
, SLOT_DATA1
);
1235 gs
.dr_write32(1, csrw(S0
, CSR_TSELECT
));
1236 gs
.dr_write_jump(2);
1237 gs
.dr_write(SLOT_DATA1
, gs
.tselect
);
1243 class hardware_breakpoint_remove_op_t
: public operation_t
1246 hardware_breakpoint_remove_op_t(gdbserver_t
& gdbserver
,
1247 hardware_breakpoint_t bp
) :
1248 operation_t(gdbserver
), bp(bp
) {};
1250 bool perform_step(unsigned int step
) {
1251 gs
.dr_write32(0, addi(S0
, ZERO
, bp
.index
));
1252 gs
.dr_write32(1, csrw(S0
, CSR_TSELECT
));
1253 gs
.dr_write32(2, csrw(ZERO
, CSR_TDATA1
));
1254 gs
.dr_write_jump(3);
1255 gs
.set_interrupt(0);
1260 hardware_breakpoint_t bp
;
1263 ////////////////////////////// gdbserver itself
1265 gdbserver_t::gdbserver_t(uint16_t port
, sim_t
*sim
) :
1269 // gdb likes to send 0x100000 bytes at once when downloading.
1270 recv_buf(0x180000), send_buf(64 * 1024)
1272 socket_fd
= socket(AF_INET
, SOCK_STREAM
, 0);
1273 if (socket_fd
== -1) {
1274 fprintf(stderr
, "failed to make socket: %s (%d)\n", strerror(errno
), errno
);
1278 fcntl(socket_fd
, F_SETFL
, O_NONBLOCK
);
1280 if (setsockopt(socket_fd
, SOL_SOCKET
, SO_REUSEADDR
, &reuseaddr
,
1281 sizeof(int)) == -1) {
1282 fprintf(stderr
, "failed setsockopt: %s (%d)\n", strerror(errno
), errno
);
1286 struct sockaddr_in addr
;
1287 memset(&addr
, 0, sizeof(addr
));
1288 addr
.sin_family
= AF_INET
;
1289 addr
.sin_addr
.s_addr
= INADDR_ANY
;
1290 addr
.sin_port
= htons(port
);
1292 if (bind(socket_fd
, (struct sockaddr
*) &addr
, sizeof(addr
)) == -1) {
1293 fprintf(stderr
, "failed to bind socket: %s (%d)\n", strerror(errno
), errno
);
1297 if (listen(socket_fd
, 1) == -1) {
1298 fprintf(stderr
, "failed to listen on socket: %s (%d)\n", strerror(errno
), errno
);
1303 unsigned int gdbserver_t::find_access_size(reg_t address
, int length
)
1305 reg_t composite
= address
| length
;
1306 if ((composite
& 0x7) == 0 && xlen
>= 64)
1308 if ((composite
& 0x3) == 0)
1313 reg_t
gdbserver_t::translate(reg_t vaddr
)
1315 unsigned int vm
= virtual_memory();
1316 unsigned int levels
, ptidxbits
, ptesize
;
1341 sprintf(buf
, "VM mode %d is not supported by gdbserver.cc.", vm
);
1343 return true; // die doesn't return, but gcc doesn't know that.
1347 // Handle page tables here. There's a bunch of duplicated code with
1348 // collect_translation_info_op_t. :-(
1349 reg_t base
= sptbr
<< PGSHIFT
;
1350 int ptshift
= (levels
- 1) * ptidxbits
;
1351 for (unsigned int i
= 0; i
< levels
; i
++, ptshift
-= ptidxbits
) {
1352 reg_t idx
= (vaddr
>> (PGSHIFT
+ ptshift
)) & ((1 << ptidxbits
) - 1);
1354 reg_t pte_addr
= base
+ idx
* ptesize
;
1355 auto it
= pte_cache
.find(pte_addr
);
1356 if (it
== pte_cache
.end()) {
1357 fprintf(stderr
, "ERROR: gdbserver tried to translate 0x%016" PRIx64
1358 " without first collecting the relevant PTEs.\n", vaddr
);
1359 die("gdbserver_t::translate()");
1362 reg_t pte
= pte_cache
[pte_addr
];
1363 reg_t ppn
= pte
>> PTE_PPN_SHIFT
;
1365 if (PTE_TABLE(pte
)) { // next level of page table
1366 base
= ppn
<< PGSHIFT
;
1368 // We've collected all the data required for the translation.
1369 reg_t vpn
= vaddr
>> PGSHIFT
;
1370 reg_t paddr
= (ppn
| (vpn
& ((reg_t(1) << ptshift
) - 1))) << PGSHIFT
;
1371 paddr
+= vaddr
& (PGSIZE
-1);
1372 D(fprintf(stderr
, "gdbserver translate 0x%lx -> 0x%lx\n", vaddr
, paddr
));
1377 fprintf(stderr
, "ERROR: gdbserver tried to translate 0x%016" PRIx64
1378 " but the relevant PTEs are invalid.\n", vaddr
);
1379 // TODO: Is it better to throw an exception here?
1383 unsigned int gdbserver_t::privilege_mode()
1385 unsigned int mode
= get_field(dcsr
, DCSR_PRV
);
1386 if (get_field(mstatus
, MSTATUS_MPRV
))
1387 mode
= get_field(mstatus
, MSTATUS_MPP
);
1391 unsigned int gdbserver_t::virtual_memory()
1393 unsigned int mode
= privilege_mode();
1396 return get_field(mstatus
, MSTATUS_VM
);
1399 void gdbserver_t::dr_write32(unsigned int index
, uint32_t value
)
1401 sim
->debug_module
.ram_write32(index
, value
);
1404 void gdbserver_t::dr_write64(unsigned int index
, uint64_t value
)
1406 dr_write32(index
, value
);
1407 dr_write32(index
+1, value
>> 32);
1410 void gdbserver_t::dr_write(enum slot slot
, uint64_t value
)
1414 dr_write32(slot_offset32
[slot
], value
);
1417 dr_write64(slot_offset64
[slot
], value
);
1425 void gdbserver_t::dr_write_jump(unsigned int index
)
1427 dr_write32(index
, jal(0,
1428 (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*index
))));
1431 void gdbserver_t::dr_write_store(unsigned int index
, unsigned int reg
, enum slot slot
)
1433 assert(slot
!= SLOT_INST0
|| index
> 2);
1434 assert(slot
!= SLOT_DATA0
|| index
< 4 || index
> 6);
1435 assert(slot
!= SLOT_DATA1
|| index
< 5 || index
> 10);
1436 assert(slot
!= SLOT_DATA_LAST
|| index
< 6 || index
> 14);
1439 return dr_write32(index
,
1440 sw(reg
, 0, (uint16_t) DEBUG_RAM_START
+ 4 * slot_offset32
[slot
]));
1442 return dr_write32(index
,
1443 sd(reg
, 0, (uint16_t) DEBUG_RAM_START
+ 4 * slot_offset64
[slot
]));
1445 return dr_write32(index
,
1446 sq(reg
, 0, (uint16_t) DEBUG_RAM_START
+ 4 * slot_offset128
[slot
]));
1448 fprintf(stderr
, "xlen is %d!\n", xlen
);
1453 void gdbserver_t::dr_write_load(unsigned int index
, unsigned int reg
, enum slot slot
)
1457 return dr_write32(index
,
1458 lw(reg
, 0, (uint16_t) DEBUG_RAM_START
+ 4 * slot_offset32
[slot
]));
1460 return dr_write32(index
,
1461 ld(reg
, 0, (uint16_t) DEBUG_RAM_START
+ 4 * slot_offset64
[slot
]));
1463 return dr_write32(index
,
1464 lq(reg
, 0, (uint16_t) DEBUG_RAM_START
+ 4 * slot_offset128
[slot
]));
1466 fprintf(stderr
, "xlen is %d!\n", xlen
);
1471 uint32_t gdbserver_t::dr_read32(unsigned int index
)
1473 uint32_t value
= sim
->debug_module
.ram_read32(index
);
1474 D(fprintf(stderr
, "read32(%d) -> 0x%x\n", index
, value
));
1478 uint64_t gdbserver_t::dr_read64(unsigned int index
)
1480 return ((uint64_t) dr_read32(index
+1) << 32) | dr_read32(index
);
1483 uint64_t gdbserver_t::dr_read(enum slot slot
)
1487 return dr_read32(slot_offset32
[slot
]);
1489 return dr_read64(slot_offset64
[slot
]);
1497 void gdbserver_t::add_operation(operation_t
* operation
)
1499 operation_queue
.push(operation
);
1502 void gdbserver_t::accept()
1504 client_fd
= ::accept(socket_fd
, NULL
, NULL
);
1505 if (client_fd
== -1) {
1506 if (errno
== EAGAIN
) {
1507 // No client waiting to connect right now.
1509 fprintf(stderr
, "failed to accept on socket: %s (%d)\n", strerror(errno
),
1514 fcntl(client_fd
, F_SETFL
, O_NONBLOCK
);
1517 extended_mode
= false;
1519 // gdb wants the core to be halted when it attaches.
1520 add_operation(new halt_op_t(*this));
1524 void gdbserver_t::read()
1526 // Reading from a non-blocking socket still blocks if there is no data
1529 size_t count
= recv_buf
.contiguous_empty_size();
1530 ssize_t bytes
= ::read(client_fd
, recv_buf
.contiguous_empty(), count
);
1532 if (errno
== EAGAIN
) {
1533 // We'll try again the next call.
1535 fprintf(stderr
, "failed to read on socket: %s (%d)\n", strerror(errno
), errno
);
1538 } else if (bytes
== 0) {
1539 // The remote disconnected.
1541 processor_t
*p
= sim
->get_core(0);
1542 // TODO p->set_halted(false, HR_NONE);
1546 recv_buf
.data_added(bytes
);
1550 void gdbserver_t::write()
1552 if (send_buf
.empty())
1555 while (!send_buf
.empty()) {
1556 unsigned int count
= send_buf
.contiguous_data_size();
1558 ssize_t bytes
= ::write(client_fd
, send_buf
.contiguous_data(), count
);
1560 fprintf(stderr
, "failed to write to socket: %s (%d)\n", strerror(errno
), errno
);
1562 } else if (bytes
== 0) {
1563 // Client can't take any more data right now.
1566 D(fprintf(stderr
, "wrote %ld bytes: ", bytes
));
1567 for (unsigned int i
= 0; i
< bytes
; i
++) {
1568 D(fprintf(stderr
, "%c", send_buf
[i
]));
1570 D(fprintf(stderr
, "\n"));
1571 send_buf
.consume(bytes
);
1576 void print_packet(const std::vector
<uint8_t> &packet
)
1578 for (uint8_t c
: packet
) {
1579 if (c
>= ' ' and c
<= '~')
1580 fprintf(stderr
, "%c", c
);
1582 fprintf(stderr
, "\\x%02x", c
);
1584 fprintf(stderr
, "\n");
1587 uint8_t compute_checksum(const std::vector
<uint8_t> &packet
)
1589 uint8_t checksum
= 0;
1590 for (auto i
= packet
.begin() + 1; i
!= packet
.end() - 3; i
++ ) {
1596 uint8_t character_hex_value(uint8_t character
)
1598 if (character
>= '0' && character
<= '9')
1599 return character
- '0';
1600 if (character
>= 'a' && character
<= 'f')
1601 return 10 + character
- 'a';
1602 if (character
>= 'A' && character
<= 'F')
1603 return 10 + character
- 'A';
1607 uint8_t extract_checksum(const std::vector
<uint8_t> &packet
)
1609 return character_hex_value(*(packet
.end() - 1)) +
1610 16 * character_hex_value(*(packet
.end() - 2));
1613 void gdbserver_t::process_requests()
1615 // See https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html
1617 while (!recv_buf
.empty()) {
1618 std::vector
<uint8_t> packet
;
1619 for (unsigned int i
= 0; i
< recv_buf
.size(); i
++) {
1620 uint8_t b
= recv_buf
[i
];
1622 if (packet
.empty() && expect_ack
&& b
== '+') {
1623 recv_buf
.consume(1);
1627 if (packet
.empty() && b
== 3) {
1628 D(fprintf(stderr
, "Received interrupt\n"));
1629 recv_buf
.consume(1);
1635 // Start of new packet.
1636 if (!packet
.empty()) {
1637 fprintf(stderr
, "Received malformed %ld-byte packet from debug client: ",
1639 print_packet(packet
);
1640 recv_buf
.consume(i
);
1645 packet
.push_back(b
);
1647 // Packets consist of $<packet-data>#<checksum>
1648 // where <checksum> is
1649 if (packet
.size() >= 4 &&
1650 packet
[packet
.size()-3] == '#') {
1651 handle_packet(packet
);
1652 recv_buf
.consume(i
+1);
1656 // There's a partial packet in the buffer. Wait until we get more data to
1658 if (packet
.size()) {
1663 if (recv_buf
.full()) {
1665 "Receive buffer is full, but no complete packet was found!\n");
1666 for (unsigned line
= 0; line
< 8; line
++) {
1667 for (unsigned i
= 0; i
< 16; i
++) {
1668 fprintf(stderr
, "%02x ", recv_buf
.entry(line
* 16 + i
));
1670 for (unsigned i
= 0; i
< 16; i
++) {
1671 uint8_t e
= recv_buf
.entry(line
* 16 + i
);
1672 if (e
>= ' ' && e
<= '~')
1673 fprintf(stderr
, "%c", e
);
1675 fprintf(stderr
, ".");
1677 fprintf(stderr
, "\n");
1679 assert(!recv_buf
.full());
1683 void gdbserver_t::handle_halt_reason(const std::vector
<uint8_t> &packet
)
1688 void gdbserver_t::handle_general_registers_read(const std::vector
<uint8_t> &packet
)
1690 add_operation(new general_registers_read_op_t(*this));
1693 void gdbserver_t::set_interrupt(uint32_t hartid
) {
1694 sim
->debug_module
.set_interrupt(hartid
);
1697 // First byte is the most-significant one.
1698 // Eg. "08675309" becomes 0x08675309.
1699 uint64_t consume_hex_number(std::vector
<uint8_t>::const_iterator
&iter
,
1700 std::vector
<uint8_t>::const_iterator end
)
1704 while (iter
!= end
) {
1706 uint64_t c_value
= character_hex_value(c
);
1716 // First byte is the least-significant one.
1717 // Eg. "08675309" becomes 0x09536708
1718 uint64_t gdbserver_t::consume_hex_number_le(
1719 std::vector
<uint8_t>::const_iterator
&iter
,
1720 std::vector
<uint8_t>::const_iterator end
)
1723 unsigned int shift
= 4;
1725 while (iter
!= end
) {
1727 uint64_t c_value
= character_hex_value(c
);
1731 value
|= c_value
<< shift
;
1732 if ((shift
% 8) == 0)
1737 if (shift
> (xlen
+4)) {
1739 "gdb sent too many data bytes. That means it thinks XLEN is greater "
1740 "than %d.\nTo fix that, tell gdb: set arch riscv:rv%d\n",
1746 void consume_string(std::string
&str
, std::vector
<uint8_t>::const_iterator
&iter
,
1747 std::vector
<uint8_t>::const_iterator end
, uint8_t separator
)
1749 while (iter
!= end
&& *iter
!= separator
) {
1750 str
.append(1, (char) *iter
);
1755 void gdbserver_t::handle_register_read(const std::vector
<uint8_t> &packet
)
1759 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1760 unsigned int n
= consume_hex_number(iter
, packet
.end());
1762 return send_packet("E01");
1764 add_operation(new register_read_op_t(*this, n
));
1767 void gdbserver_t::handle_register_write(const std::vector
<uint8_t> &packet
)
1771 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1772 unsigned int n
= consume_hex_number(iter
, packet
.end());
1774 return send_packet("E05");
1777 reg_t value
= consume_hex_number_le(iter
, packet
.end());
1779 return send_packet("E06");
1781 processor_t
*p
= sim
->get_core(0);
1783 add_operation(new register_write_op_t(*this, n
, value
));
1785 return send_packet("OK");
1788 void gdbserver_t::handle_memory_read(const std::vector
<uint8_t> &packet
)
1791 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1792 reg_t address
= consume_hex_number(iter
, packet
.end());
1794 return send_packet("E10");
1796 reg_t length
= consume_hex_number(iter
, packet
.end());
1798 return send_packet("E11");
1800 add_operation(new collect_translation_info_op_t(*this, address
, length
));
1801 add_operation(new memory_read_op_t(*this, address
, length
));
1804 void gdbserver_t::handle_memory_binary_write(const std::vector
<uint8_t> &packet
)
1806 // X addr,length:XX...
1807 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1808 reg_t address
= consume_hex_number(iter
, packet
.end());
1810 return send_packet("E20");
1812 reg_t length
= consume_hex_number(iter
, packet
.end());
1814 return send_packet("E21");
1818 return send_packet("OK");
1821 unsigned char *data
= new unsigned char[length
];
1822 for (unsigned int i
= 0; i
< length
; i
++) {
1823 if (iter
== packet
.end()) {
1824 return send_packet("E22");
1829 // The binary data representation uses 7d (ascii ‘}’) as an escape
1830 // character. Any escaped byte is transmitted as the escape character
1831 // followed by the original character XORed with 0x20. For example, the
1832 // byte 0x7d would be transmitted as the two bytes 0x7d 0x5d. The bytes
1833 // 0x23 (ascii ‘#’), 0x24 (ascii ‘$’), and 0x7d (ascii ‘}’) must always
1835 if (iter
== packet
.end()) {
1836 return send_packet("E23");
1844 return send_packet("E4b"); // EOVERFLOW
1846 add_operation(new collect_translation_info_op_t(*this, address
, length
));
1847 add_operation(new memory_write_op_t(*this, address
, length
, data
));
1850 void gdbserver_t::handle_continue(const std::vector
<uint8_t> &packet
)
1853 processor_t
*p
= sim
->get_core(0);
1854 if (packet
[2] != '#') {
1855 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1856 dpc
= consume_hex_number(iter
, packet
.end());
1858 return send_packet("E30");
1861 add_operation(new maybe_restore_tselect_op_t(*this));
1862 add_operation(new continue_op_t(*this, false));
1865 void gdbserver_t::handle_step(const std::vector
<uint8_t> &packet
)
1868 if (packet
[2] != '#') {
1869 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1871 //p->state.pc = consume_hex_number(iter, packet.end());
1873 return send_packet("E40");
1876 add_operation(new maybe_restore_tselect_op_t(*this));
1877 add_operation(new continue_op_t(*this, true));
1880 void gdbserver_t::handle_kill(const std::vector
<uint8_t> &packet
)
1883 // The exact effect of this packet is not specified.
1884 // Looks like OpenOCD disconnects?
1888 void gdbserver_t::handle_extended(const std::vector
<uint8_t> &packet
)
1890 // Enable extended mode. In extended mode, the remote server is made
1891 // persistent. The ‘R’ packet is used to restart the program being debugged.
1893 extended_mode
= true;
1896 void gdbserver_t::software_breakpoint_insert(reg_t vaddr
, unsigned int size
)
1898 fence_i_required
= true;
1899 add_operation(new collect_translation_info_op_t(*this, vaddr
, size
));
1900 unsigned char* inst
= new unsigned char[4];
1902 inst
[0] = C_EBREAK
& 0xff;
1903 inst
[1] = (C_EBREAK
>> 8) & 0xff;
1905 inst
[0] = EBREAK
& 0xff;
1906 inst
[1] = (EBREAK
>> 8) & 0xff;
1907 inst
[2] = (EBREAK
>> 16) & 0xff;
1908 inst
[3] = (EBREAK
>> 24) & 0xff;
1911 software_breakpoint_t bp
= {
1915 software_breakpoints
[vaddr
] = bp
;
1916 add_operation(new memory_read_op_t(*this, bp
.vaddr
, bp
.size
,
1917 software_breakpoints
[bp
.vaddr
].instruction
));
1918 add_operation(new memory_write_op_t(*this, bp
.vaddr
, bp
.size
, inst
));
1921 void gdbserver_t::software_breakpoint_remove(reg_t vaddr
, unsigned int size
)
1923 fence_i_required
= true;
1924 add_operation(new collect_translation_info_op_t(*this, vaddr
, size
));
1926 software_breakpoint_t found_bp
= software_breakpoints
[vaddr
];
1927 unsigned char* instruction
= new unsigned char[4];
1928 memcpy(instruction
, found_bp
.instruction
, 4);
1929 add_operation(new memory_write_op_t(*this, found_bp
.vaddr
,
1930 found_bp
.size
, instruction
));
1931 software_breakpoints
.erase(vaddr
);
1934 void gdbserver_t::hardware_breakpoint_insert(const hardware_breakpoint_t
&bp
)
1936 add_operation(new maybe_save_tselect_op_t(*this));
1937 add_operation(new hardware_breakpoint_insert_op_t(*this, bp
));
1940 void gdbserver_t::hardware_breakpoint_remove(const hardware_breakpoint_t
&bp
)
1942 add_operation(new maybe_save_tselect_op_t(*this));
1943 hardware_breakpoint_t found
= *hardware_breakpoints
.find(bp
);
1944 add_operation(new hardware_breakpoint_remove_op_t(*this, found
));
1947 void gdbserver_t::handle_breakpoint(const std::vector
<uint8_t> &packet
)
1949 // insert: Z type,addr,length
1950 // remove: z type,addr,length
1952 // type: 0 - software breakpoint, 1 - hardware breakpoint, 2 - write
1953 // watchpoint, 3 - read watchpoint, 4 - access watchpoint; addr is address;
1954 // length is in bytes. For a software breakpoint, length specifies the size
1955 // of the instruction to be patched. For hardware breakpoints and watchpoints
1956 // length specifies the memory region to be monitored. To avoid potential
1957 // problems with duplicate packets, the operations should be implemented in
1958 // an idempotent way.
1960 bool insert
= (packet
[1] == 'Z');
1961 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1962 gdb_breakpoint_type_t type
= static_cast<gdb_breakpoint_type_t
>(
1963 consume_hex_number(iter
, packet
.end()));
1965 return send_packet("E50");
1967 reg_t address
= consume_hex_number(iter
, packet
.end());
1969 return send_packet("E51");
1971 unsigned int size
= consume_hex_number(iter
, packet
.end());
1972 // There may be more options after a ; here, but we don't support that.
1974 return send_packet("E52");
1978 if (size
!= 2 && size
!= 4) {
1979 return send_packet("E53");
1982 software_breakpoint_insert(address
, size
);
1984 software_breakpoint_remove(address
, size
);
1993 hardware_breakpoint_t bp
= {
1997 bp
.load
= (type
== GB_READ
|| type
== GB_ACCESS
);
1998 bp
.store
= (type
== GB_WRITE
|| type
== GB_ACCESS
);
1999 bp
.execute
= (type
== GB_HARDWARE
|| type
== GB_ACCESS
);
2001 hardware_breakpoint_insert(bp
);
2002 // Insert might fail if there's no space, so the insert operation will
2003 // send its own OK (or not).
2006 hardware_breakpoint_remove(bp
);
2012 return send_packet("E56");
2015 return send_packet("OK");
2018 void gdbserver_t::handle_query(const std::vector
<uint8_t> &packet
)
2021 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
2023 consume_string(name
, iter
, packet
.end(), ':');
2024 if (iter
!= packet
.end())
2026 if (name
== "Supported") {
2028 while (iter
!= packet
.end()) {
2029 std::string feature
;
2030 consume_string(feature
, iter
, packet
.end(), ';');
2031 if (iter
!= packet
.end())
2033 if (feature
== "swbreak+") {
2037 send("PacketSize=131072;");
2038 return end_packet();
2041 D(fprintf(stderr
, "Unsupported query %s\n", name
.c_str()));
2042 return send_packet("");
2045 void gdbserver_t::handle_packet(const std::vector
<uint8_t> &packet
)
2047 if (compute_checksum(packet
) != extract_checksum(packet
)) {
2048 fprintf(stderr
, "Received %ld-byte packet with invalid checksum\n", packet
.size());
2049 fprintf(stderr
, "Computed checksum: %x\n", compute_checksum(packet
));
2050 print_packet(packet
);
2055 D(fprintf(stderr
, "Received %ld-byte packet from debug client: ", packet
.size()));
2056 D(print_packet(packet
));
2059 switch (packet
[1]) {
2061 return handle_extended(packet
);
2063 return handle_halt_reason(packet
);
2065 return handle_general_registers_read(packet
);
2067 // return handle_kill(packet);
2069 return handle_memory_read(packet
);
2071 // return handle_memory_write(packet);
2073 return handle_memory_binary_write(packet
);
2075 return handle_register_read(packet
);
2077 return handle_register_write(packet
);
2079 return handle_continue(packet
);
2081 return handle_step(packet
);
2084 return handle_breakpoint(packet
);
2087 return handle_query(packet
);
2091 D(fprintf(stderr
, "** Unsupported packet: "));
2092 D(print_packet(packet
));
2096 void gdbserver_t::handle_interrupt()
2098 processor_t
*p
= sim
->get_core(0);
2099 add_operation(new halt_op_t(*this, true));
2102 void gdbserver_t::handle()
2104 if (client_fd
> 0) {
2105 processor_t
*p
= sim
->get_core(0);
2107 bool interrupt
= sim
->debug_module
.get_interrupt(0);
2109 if (!interrupt
&& !operation_queue
.empty()) {
2110 operation_t
*operation
= operation_queue
.front();
2111 if (operation
->step()) {
2112 operation_queue
.pop();
2117 bool halt_notification
= sim
->debug_module
.get_halt_notification(0);
2118 if (halt_notification
) {
2119 sim
->debug_module
.clear_halt_notification(0);
2120 add_operation(new halt_op_t(*this, true));
2130 if (operation_queue
.empty()) {
2131 this->process_requests();
2135 void gdbserver_t::send(const char* msg
)
2137 unsigned int length
= strlen(msg
);
2138 for (const char *c
= msg
; *c
; c
++)
2139 running_checksum
+= *c
;
2140 send_buf
.append((const uint8_t *) msg
, length
);
2143 void gdbserver_t::send(uint64_t value
)
2146 for (unsigned int i
= 0; i
< 8; i
++) {
2147 sprintf(buffer
, "%02x", (int) (value
& 0xff));
2153 void gdbserver_t::send(uint32_t value
)
2156 for (unsigned int i
= 0; i
< 4; i
++) {
2157 sprintf(buffer
, "%02x", (int) (value
& 0xff));
2163 void gdbserver_t::send(uint8_t value
)
2166 sprintf(buffer
, "%02x", (int) value
);
2170 void gdbserver_t::send_packet(const char* data
)
2178 void gdbserver_t::start_packet()
2181 running_checksum
= 0;
2184 void gdbserver_t::end_packet(const char* data
)
2190 char checksum_string
[4];
2191 sprintf(checksum_string
, "#%02x", running_checksum
);
2192 send(checksum_string
);