6 #include <sys/socket.h>
18 #include "gdbserver.h"
22 //////////////////////////////////////// Utility Functions
31 void die(const char* msg
)
33 fprintf(stderr
, "gdbserver code died: %s\n", msg
);
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.
46 REG_MSTATUS
= CSR_MSTATUS
+ REG_CSR0
,
51 //////////////////////////////////////// Functions to generate RISC-V opcodes.
53 // TODO: Does this already exist somewhere?
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.
60 static uint32_t bits(uint32_t value
, unsigned int hi
, unsigned int lo
) {
61 return (value
>> lo
) & ((1 << (hi
+1-lo
)) - 1);
64 static uint32_t bit(uint32_t value
, unsigned int b
) {
65 return (value
>> b
) & 1;
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) |
77 static uint32_t csrsi(unsigned int csr
, uint16_t imm
) {
79 (bits(imm
, 4, 0) << 15) |
83 static uint32_t csrci(unsigned int csr
, uint16_t imm
) {
85 (bits(imm
, 4, 0) << 15) |
89 static uint32_t csrr(unsigned int rd
, unsigned int csr
) {
90 return (csr
<< 20) | (rd
<< 7) | MATCH_CSRRS
;
93 static uint32_t csrw(unsigned int source
, unsigned int csr
) {
94 return (csr
<< 20) | (source
<< 15) | MATCH_CSRRW
;
97 static uint32_t fence_i()
102 static uint32_t sb(unsigned int src
, unsigned int base
, uint16_t offset
)
104 return (bits(offset
, 11, 5) << 25) |
107 (bits(offset
, 4, 0) << 7) |
111 static uint32_t sh(unsigned int src
, unsigned int base
, uint16_t offset
)
113 return (bits(offset
, 11, 5) << 25) |
116 (bits(offset
, 4, 0) << 7) |
120 static uint32_t sw(unsigned int src
, unsigned int base
, uint16_t offset
)
122 return (bits(offset
, 11, 5) << 25) |
125 (bits(offset
, 4, 0) << 7) |
129 static uint32_t sd(unsigned int src
, unsigned int base
, uint16_t offset
)
131 return (bits(offset
, 11, 5) << 25) |
132 (bits(src
, 4, 0) << 20) |
134 (bits(offset
, 4, 0) << 7) |
138 static uint32_t sq(unsigned int src
, unsigned int base
, uint16_t offset
)
141 return (bits(offset
, 11, 5) << 25) |
142 (bits(src
, 4, 0) << 20) |
144 (bits(offset
, 4, 0) << 7) |
151 static uint32_t lq(unsigned int rd
, unsigned int base
, uint16_t offset
)
154 return (bits(offset
, 11, 0) << 20) |
156 (bits(rd
, 4, 0) << 7) |
163 static uint32_t ld(unsigned int rd
, unsigned int base
, uint16_t offset
)
165 return (bits(offset
, 11, 0) << 20) |
167 (bits(rd
, 4, 0) << 7) |
171 static uint32_t lw(unsigned int rd
, unsigned int base
, uint16_t offset
)
173 return (bits(offset
, 11, 0) << 20) |
175 (bits(rd
, 4, 0) << 7) |
179 static uint32_t lh(unsigned int rd
, unsigned int base
, uint16_t offset
)
181 return (bits(offset
, 11, 0) << 20) |
183 (bits(rd
, 4, 0) << 7) |
187 static uint32_t lb(unsigned int rd
, unsigned int base
, uint16_t offset
)
189 return (bits(offset
, 11, 0) << 20) |
191 (bits(rd
, 4, 0) << 7) |
195 static uint32_t fsw(unsigned int src
, unsigned int base
, uint16_t offset
)
197 return (bits(offset
, 11, 5) << 25) |
198 (bits(src
, 4, 0) << 20) |
200 (bits(offset
, 4, 0) << 7) |
204 static uint32_t fsd(unsigned int src
, unsigned int base
, uint16_t offset
)
206 return (bits(offset
, 11, 5) << 25) |
207 (bits(src
, 4, 0) << 20) |
209 (bits(offset
, 4, 0) << 7) |
213 static uint32_t flw(unsigned int dest
, unsigned int base
, uint16_t offset
)
215 return (bits(offset
, 11, 0) << 20) |
217 (bits(dest
, 4, 0) << 7) |
221 static uint32_t fld(unsigned int dest
, unsigned int base
, uint16_t offset
)
223 return (bits(offset
, 11, 0) << 20) |
225 (bits(dest
, 4, 0) << 7) |
229 static uint32_t addi(unsigned int dest
, unsigned int src
, uint16_t imm
)
231 return (bits(imm
, 11, 0) << 20) |
237 static uint32_t ori(unsigned int dest
, unsigned int src
, uint16_t imm
)
239 return (bits(imm
, 11, 0) << 20) |
245 static uint32_t xori(unsigned int dest
, unsigned int src
, uint16_t imm
)
247 return (bits(imm
, 11, 0) << 20) |
253 static uint32_t srli(unsigned int dest
, unsigned int src
, uint8_t shamt
)
255 return (bits(shamt
, 4, 0) << 20) |
262 static uint32_t nop()
264 return addi(0, 0, 0);
267 template <typename T
>
268 unsigned int circular_buffer_t
<T
>::size() const
273 return end
+ capacity
- start
;
276 template <typename T
>
277 void circular_buffer_t
<T
>::consume(unsigned int bytes
)
279 start
= (start
+ bytes
) % capacity
;
282 template <typename T
>
283 unsigned int circular_buffer_t
<T
>::contiguous_empty_size() const
287 return capacity
- end
- 1;
289 return capacity
- end
;
291 return start
- end
- 1;
294 template <typename T
>
295 unsigned int circular_buffer_t
<T
>::contiguous_data_size() const
300 return capacity
- start
;
303 template <typename T
>
304 void circular_buffer_t
<T
>::data_added(unsigned int bytes
)
307 assert(end
<= capacity
);
312 template <typename T
>
313 void circular_buffer_t
<T
>::reset()
319 template <typename T
>
320 void circular_buffer_t
<T
>::append(const T
*src
, unsigned int count
)
322 unsigned int copy
= std::min(count
, contiguous_empty_size());
323 memcpy(contiguous_empty(), src
, copy
* sizeof(T
));
327 assert(count
< contiguous_empty_size());
328 memcpy(contiguous_empty(), src
+copy
, count
* sizeof(T
));
333 ////////////////////////////// Debug Operations
335 class halt_op_t
: public operation_t
338 halt_op_t(gdbserver_t
& gdbserver
, bool send_status
=false) :
339 operation_t(gdbserver
), send_status(send_status
),
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
);
350 bool perform_step(unsigned int step
) {
352 gs
.tselect_valid
= false;
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));
374 uint32_t word0
= gs
.dr_read32(0);
375 uint32_t word1
= gs
.dr_read32(1);
377 if (word0
== 1 && word1
== 0) {
379 } else if (word0
== 0xffffffff && word1
== 3) {
381 } else if (word0
== 0xffffffff && word1
== 0xffffffff) {
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
);
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));
410 gs
.dcsr
= gs
.dr_read32(4);
412 gs
.sptbr_valid
= false;
413 gs
.pte_cache
.clear();
416 switch (get_field(gs
.dcsr
, DCSR_CAUSE
)) {
417 case DCSR_CAUSE_NONE
:
418 fprintf(stderr
, "Internal error. Processor halted without reason.\n");
421 case DCSR_CAUSE_DEBUGINT
:
422 gs
.send_packet("S02"); // Pretend program received SIGINT.
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");
431 case DCSR_CAUSE_SWBP
:
432 gs
.send_packet("T05swbreak:;");
455 class continue_op_t
: public operation_t
458 continue_op_t(gdbserver_t
& gdbserver
, bool single_step
) :
459 operation_t(gdbserver
), single_step(single_step
) {};
461 bool perform_step(unsigned int step
) {
462 D(fprintf(stderr
, "continue step %d\n", step
));
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());
471 gs
.fence_i_required
= false;
475 gs
.dr_write(SLOT_DATA0
, gs
.dpc
);
480 gs
.dr_write_load(0, S0
, SLOT_DATA0
);
481 gs
.dr_write32(1, csrw(S0
, CSR_MSTATUS
));
483 gs
.dr_write(SLOT_DATA0
, gs
.mstatus
);
488 gs
.dr_write32(0, lw(S0
, 0, (uint16_t) DEBUG_RAM_START
+16));
489 gs
.dr_write32(1, csrw(S0
, CSR_DCSR
));
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
);
511 class general_registers_read_op_t
: public operation_t
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",
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.
526 general_registers_read_op_t(gdbserver_t
& gdbserver
) :
527 operation_t(gdbserver
) {};
529 bool perform_step(unsigned int step
)
531 D(fprintf(stderr
, "register_read step %d\n", step
));
535 // x0 is always zero.
537 gs
.send((uint32_t) 0);
539 gs
.send((uint64_t) 0);
542 gs
.dr_write_store(0, 1, SLOT_DATA0
);
543 gs
.dr_write_store(1, 2, SLOT_DATA1
);
550 gs
.send((uint32_t) gs
.dr_read(SLOT_DATA0
));
552 gs
.send((uint64_t) gs
.dr_read(SLOT_DATA0
));
560 gs
.send((uint32_t) gs
.dr_read(SLOT_DATA1
));
562 gs
.send((uint64_t) gs
.dr_read(SLOT_DATA1
));
565 unsigned int current_reg
= 2 * step
+ 1;
567 if (current_reg
== S1
) {
568 gs
.dr_write_load(i
++, S1
, SLOT_DATA_LAST
);
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
));
575 gs
.dr_write_store(i
++, current_reg
+1, SLOT_DATA1
);
584 class register_read_op_t
: public operation_t
587 register_read_op_t(gdbserver_t
& gdbserver
, unsigned int reg
) :
588 operation_t(gdbserver
), reg(reg
) {};
590 bool perform_step(unsigned int step
)
594 if (reg
>= REG_XPR0
&& reg
<= REG_XPR31
) {
596 gs
.dr_write32(0, sw(reg
- REG_XPR0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
598 gs
.dr_write32(0, sd(reg
- REG_XPR0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
601 } else if (reg
== REG_PC
) {
604 gs
.send((uint32_t) gs
.dpc
);
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;
616 gs
.dr_write32(2, fsw(reg
- REG_FPR0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
618 gs
.dr_write32(2, fsd(reg
- REG_FPR0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
621 } else if (reg
== REG_MSTATUS
) {
624 gs
.send((uint32_t) gs
.mstatus
);
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
);
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
) {
639 gs
.send((uint8_t) get_field(gs
.dcsr
, DCSR_PRV
));
643 gs
.send_packet("E02");
651 unsigned result
= gs
.dr_read32(DEBUG_RAM_SIZE
/ 4 - 1);
653 gs
.send_packet("E03");
658 gs
.send(gs
.dr_read32(4));
660 gs
.send(gs
.dr_read(SLOT_DATA0
));
673 class register_write_op_t
: public operation_t
676 register_write_op_t(gdbserver_t
& gdbserver
, unsigned int reg
, reg_t value
) :
677 operation_t(gdbserver
), reg(reg
), value(value
) {};
679 bool perform_step(unsigned int step
)
683 gs
.dr_write_load(0, S0
, SLOT_DATA0
);
684 gs
.dr_write(SLOT_DATA0
, value
);
686 gs
.dr_write32(1, csrw(S0
, CSR_DSCRATCH
));
688 } else if (reg
== S1
) {
689 gs
.dr_write_store(1, S0
, SLOT_DATA_LAST
);
691 } else if (reg
>= REG_XPR0
&& reg
<= REG_XPR31
) {
692 gs
.dr_write32(1, addi(reg
, S0
, 0));
694 } else if (reg
== REG_PC
) {
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;
703 gs
.dr_write32(2, flw(reg
- REG_FPR0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
705 gs
.dr_write32(2, fld(reg
- REG_FPR0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
708 } else if (reg
== REG_MSTATUS
) {
710 gs
.mstatus_dirty
= true;
712 } else if (reg
>= REG_CSR0
&& reg
<= REG_CSR4095
) {
713 gs
.dr_write32(1, csrw(S0
, reg
- REG_CSR0
));
715 if (reg
== REG_CSR0
+ CSR_SPTBR
) {
717 gs
.sptbr_valid
= true;
719 } else if (reg
== REG_PRIV
) {
720 gs
.dcsr
= set_field(gs
.dcsr
, DCSR_PRV
, value
);
723 gs
.send_packet("E02");
731 unsigned result
= gs
.dr_read32(DEBUG_RAM_SIZE
/ 4 - 1);
733 gs
.send_packet("E03");
736 gs
.send_packet("OK");
749 class memory_read_op_t
: public operation_t
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)
758 buf
= new uint8_t[length
];
766 bool perform_step(unsigned int step
)
769 // address goes in S0
770 paddr
= gs
.translate(vaddr
);
771 access_size
= gs
.find_access_size(paddr
, length
);
773 gs
.dr_write_load(0, S0
, SLOT_DATA0
);
774 switch (access_size
) {
776 gs
.dr_write32(1, lb(S1
, S0
, 0));
779 gs
.dr_write32(1, lh(S1
, S0
, 0));
782 gs
.dr_write32(1, lw(S1
, S0
, 0));
785 gs
.dr_write32(1, ld(S1
, S0
, 0));
788 gs
.dr_write_store(2, S1
, SLOT_DATA1
);
790 gs
.dr_write(SLOT_DATA0
, paddr
);
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
800 gs
.send_packet("E99");
804 reg_t value
= gs
.dr_read(SLOT_DATA1
);
805 for (unsigned int i
= 0; i
< access_size
; i
++) {
807 *(data
++) = value
& 0xff;
808 D(fprintf(stderr
, "%02x", (unsigned int) (value
& 0xff)));
810 buf
[index
++] = value
& 0xff;
815 D(fprintf(stderr
, "\n"));
817 length
-= access_size
;
818 paddr
+= access_size
;
824 for (unsigned int i
= 0; i
< index
; i
++) {
825 sprintf(buffer
, "%02x", (unsigned int) buf
[i
]);
832 gs
.dr_write(SLOT_DATA0
, paddr
);
843 unsigned int access_size
;
848 class memory_write_op_t
: public operation_t
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
) {};
855 ~memory_write_op_t() {
859 bool perform_step(unsigned int step
)
861 reg_t paddr
= gs
.translate(vaddr
);
863 unsigned int data_offset
;
866 data_offset
= slot_offset32
[SLOT_DATA1
];
869 data_offset
= slot_offset64
[SLOT_DATA1
];
872 data_offset
= slot_offset128
[SLOT_DATA1
];
879 access_size
= gs
.find_access_size(paddr
, length
);
881 D(fprintf(stderr
, "write to 0x%" PRIx64
" -> 0x%" PRIx64
" (access=%d): ",
882 vaddr
, paddr
, access_size
));
883 for (unsigned int i
= 0; i
< length
; i
++) {
884 D(fprintf(stderr
, "%02x", data
[i
]));
886 D(fprintf(stderr
, "\n"));
888 // address goes in S0
889 gs
.dr_write_load(0, S0
, SLOT_DATA0
);
890 switch (access_size
) {
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]);
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));
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));
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));
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");
923 gs
.dr_write(SLOT_DATA0
, paddr
);
929 if (gs
.dr_read32(DEBUG_RAM_SIZE
/ 4 - 1)) {
930 gs
.send_packet("E98");
934 offset
+= access_size
;
935 if (offset
>= length
) {
936 gs
.send_packet("OK");
939 const unsigned char *d
= data
+ offset
;
940 switch (access_size
) {
942 gs
.dr_write32(data_offset
, d
[0]);
945 gs
.dr_write32(data_offset
, d
[0] | (d
[1] << 8));
948 gs
.dr_write32(data_offset
, d
[0] | (d
[1] << 8) |
949 (d
[2] << 16) | (d
[3] << 24));
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));
958 gs
.send_packet("E13");
961 gs
.dr_write(SLOT_DATA0
, paddr
+ offset
);
971 unsigned int access_size
;
972 const unsigned char *data
;
975 class collect_translation_info_op_t
: public operation_t
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
) {};
984 bool perform_step(unsigned int step
)
986 unsigned int vm
= gs
.virtual_memory();
991 // Nothing to be done.
1013 sprintf(buf
, "VM mode %d is not supported by gdbserver.cc.", vm
);
1015 return true; // die doesn't return, but gcc doesn't know that.
1020 // Perform any reads from the just-completed action.
1024 case STATE_READ_SPTBR
:
1025 gs
.sptbr
= gs
.dr_read(SLOT_DATA0
);
1026 gs
.sptbr_valid
= true;
1028 case STATE_READ_PTE
:
1030 gs
.pte_cache
[pte_addr
] = gs
.dr_read32(4);
1032 gs
.pte_cache
[pte_addr
] = ((uint64_t) gs
.dr_read32(5) << 32) |
1035 D(fprintf(stderr
, "pte_cache[0x%" PRIx64
"] = 0x%" PRIx64
"\n", pte_addr
,
1036 gs
.pte_cache
[pte_addr
]));
1040 // Set up the next action.
1041 // We only get here for VM_SV32/39/38.
1043 if (!gs
.sptbr_valid
) {
1044 state
= STATE_READ_SPTBR
;
1045 gs
.dr_write32(0, csrr(S0
, CSR_SPTBR
));
1046 gs
.dr_write_store(1, S0
, SLOT_DATA0
);
1047 gs
.dr_write_jump(2);
1048 gs
.set_interrupt(0);
1052 reg_t base
= gs
.sptbr
<< PGSHIFT
;
1053 int ptshift
= (levels
- 1) * ptidxbits
;
1054 for (unsigned int i
= 0; i
< levels
; i
++, ptshift
-= ptidxbits
) {
1055 reg_t idx
= (vaddr
>> (PGSHIFT
+ ptshift
)) & ((1 << ptidxbits
) - 1);
1057 pte_addr
= base
+ idx
* ptesize
;
1058 auto it
= gs
.pte_cache
.find(pte_addr
);
1059 if (it
== gs
.pte_cache
.end()) {
1060 state
= STATE_READ_PTE
;
1062 gs
.dr_write32(0, lw(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
1063 gs
.dr_write32(1, lw(S1
, S0
, 0));
1064 gs
.dr_write32(2, sw(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
1066 assert(gs
.xlen
>= 64);
1067 gs
.dr_write32(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
1068 gs
.dr_write32(1, ld(S1
, S0
, 0));
1069 gs
.dr_write32(2, sd(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
1071 gs
.dr_write_jump(3);
1072 gs
.dr_write32(4, pte_addr
);
1073 gs
.dr_write32(5, pte_addr
>> 32);
1074 gs
.set_interrupt(0);
1078 reg_t pte
= gs
.pte_cache
[pte_addr
];
1079 reg_t ppn
= pte
>> PTE_PPN_SHIFT
;
1081 if (PTE_TABLE(pte
)) { // next level of page table
1082 base
= ppn
<< PGSHIFT
;
1084 // We've collected all the data required for the translation.
1089 "ERROR: gdbserver couldn't find appropriate PTEs to translate 0x%016" PRIx64
"\n",
1102 unsigned int levels
;
1103 unsigned int ptidxbits
;
1104 unsigned int ptesize
;
1108 class hardware_breakpoint_insert_op_t
: public operation_t
1111 hardware_breakpoint_insert_op_t(gdbserver_t
& gdbserver
,
1112 hardware_breakpoint_t bp
) :
1113 operation_t(gdbserver
), state(STATE_START
), bp(bp
) {};
1115 void write_new_index_program()
1117 gs
.dr_write_load(0, S0
, SLOT_DATA1
);
1118 gs
.dr_write32(1, csrw(S0
, CSR_TSELECT
));
1119 gs
.dr_write32(2, csrr(S0
, CSR_TSELECT
));
1120 gs
.dr_write_store(3, S0
, SLOT_DATA1
);
1121 gs
.dr_write_jump(4);
1122 gs
.dr_write(SLOT_DATA1
, bp
.index
);
1125 bool perform_step(unsigned int step
)
1130 write_new_index_program();
1131 state
= STATE_CHECK_INDEX
;
1134 case STATE_CHECK_INDEX
:
1135 if (gs
.dr_read(SLOT_DATA1
) != bp
.index
) {
1136 // We've exhausted breakpoints without finding an appropriate one.
1137 gs
.send_packet("E58");
1141 gs
.dr_write32(0, csrr(S0
, CSR_TDATA1
));
1142 gs
.dr_write_store(1, S0
, SLOT_DATA0
);
1143 gs
.dr_write_jump(2);
1144 state
= STATE_CHECK_MCONTROL
;
1147 case STATE_CHECK_MCONTROL
:
1149 reg_t mcontrol
= gs
.dr_read(SLOT_DATA0
);
1150 unsigned int type
= mcontrol
>> (gs
.xlen
- 4);
1152 // We've exhausted breakpoints without finding an appropriate one.
1153 gs
.send_packet("E58");
1158 !get_field(mcontrol
, MCONTROL_EXECUTE
) &&
1159 !get_field(mcontrol
, MCONTROL_LOAD
) &&
1160 !get_field(mcontrol
, MCONTROL_STORE
)) {
1161 // Found an unused trigger.
1162 gs
.dr_write_load(0, S0
, SLOT_DATA1
);
1163 gs
.dr_write32(1, csrw(S0
, CSR_TDATA1
));
1164 gs
.dr_write_jump(2);
1165 mcontrol
= set_field(0, MCONTROL_ACTION
, MCONTROL_ACTION_DEBUG_MODE
);
1166 mcontrol
= set_field(mcontrol
, MCONTROL_DMODE(gs
.xlen
), 1);
1167 mcontrol
= set_field(mcontrol
, MCONTROL_MATCH
, MCONTROL_MATCH_EQUAL
);
1168 mcontrol
= set_field(mcontrol
, MCONTROL_M
, 1);
1169 mcontrol
= set_field(mcontrol
, MCONTROL_H
, 1);
1170 mcontrol
= set_field(mcontrol
, MCONTROL_S
, 1);
1171 mcontrol
= set_field(mcontrol
, MCONTROL_U
, 1);
1172 mcontrol
= set_field(mcontrol
, MCONTROL_EXECUTE
, bp
.execute
);
1173 mcontrol
= set_field(mcontrol
, MCONTROL_LOAD
, bp
.load
);
1174 mcontrol
= set_field(mcontrol
, MCONTROL_STORE
, bp
.store
);
1175 // For store triggers it's nicer to fire just before the
1176 // instruction than just after. However, gdb doesn't clear the
1177 // breakpoints and step before resuming from a store trigger.
1178 // That means that without extra code, you'll keep hitting the
1179 // same watchpoint over and over again. That's not useful at all.
1180 // Instead of fixing this the right way, just set timing=1 for
1182 if (bp
.load
|| bp
.store
)
1183 mcontrol
= set_field(mcontrol
, MCONTROL_TIMING
, 1);
1185 gs
.dr_write(SLOT_DATA1
, mcontrol
);
1186 state
= STATE_WRITE_ADDRESS
;
1189 write_new_index_program();
1190 state
= STATE_CHECK_INDEX
;
1195 case STATE_WRITE_ADDRESS
:
1197 gs
.dr_write_load(0, S0
, SLOT_DATA1
);
1198 gs
.dr_write32(1, csrw(S0
, CSR_TDATA2
));
1199 gs
.dr_write_jump(2);
1200 gs
.dr_write(SLOT_DATA1
, bp
.vaddr
);
1201 gs
.set_interrupt(0);
1202 gs
.send_packet("OK");
1204 gs
.hardware_breakpoints
.insert(bp
);
1210 gs
.set_interrupt(0);
1218 STATE_CHECK_MCONTROL
,
1221 hardware_breakpoint_t bp
;
1224 class maybe_save_tselect_op_t
: public operation_t
1227 maybe_save_tselect_op_t(gdbserver_t
& gdbserver
) : operation_t(gdbserver
) {};
1228 bool perform_step(unsigned int step
) {
1229 if (gs
.tselect_valid
)
1234 gs
.dr_write32(0, csrr(S0
, CSR_TDATA1
));
1235 gs
.dr_write_store(1, S0
, SLOT_DATA0
);
1236 gs
.dr_write_jump(2);
1237 gs
.set_interrupt(0);
1240 gs
.tselect
= gs
.dr_read(SLOT_DATA0
);
1241 gs
.tselect_valid
= true;
1248 class maybe_restore_tselect_op_t
: public operation_t
1251 maybe_restore_tselect_op_t(gdbserver_t
& gdbserver
) : operation_t(gdbserver
) {};
1252 bool perform_step(unsigned int step
) {
1253 if (gs
.tselect_valid
) {
1254 gs
.dr_write_load(0, S0
, SLOT_DATA1
);
1255 gs
.dr_write32(1, csrw(S0
, CSR_TSELECT
));
1256 gs
.dr_write_jump(2);
1257 gs
.dr_write(SLOT_DATA1
, gs
.tselect
);
1263 class maybe_restore_mstatus_op_t
: public operation_t
1266 maybe_restore_mstatus_op_t(gdbserver_t
& gdbserver
) : operation_t(gdbserver
) {};
1267 bool perform_step(unsigned int step
) {
1268 if (gs
.mstatus_dirty
) {
1269 gs
.dr_write_load(0, S0
, SLOT_DATA1
);
1270 gs
.dr_write32(1, csrw(S0
, CSR_MSTATUS
));
1271 gs
.dr_write_jump(2);
1272 gs
.dr_write(SLOT_DATA1
, gs
.mstatus
);
1278 class hardware_breakpoint_remove_op_t
: public operation_t
1281 hardware_breakpoint_remove_op_t(gdbserver_t
& gdbserver
,
1282 hardware_breakpoint_t bp
) :
1283 operation_t(gdbserver
), bp(bp
) {};
1285 bool perform_step(unsigned int step
) {
1286 gs
.dr_write32(0, addi(S0
, ZERO
, bp
.index
));
1287 gs
.dr_write32(1, csrw(S0
, CSR_TSELECT
));
1288 gs
.dr_write32(2, csrw(ZERO
, CSR_TDATA1
));
1289 gs
.dr_write_jump(3);
1290 gs
.set_interrupt(0);
1295 hardware_breakpoint_t bp
;
1298 ////////////////////////////// gdbserver itself
1300 gdbserver_t::gdbserver_t(uint16_t port
, sim_t
*sim
) :
1304 // gdb likes to send 0x100000 bytes at once when downloading.
1305 recv_buf(0x180000), send_buf(64 * 1024)
1307 socket_fd
= socket(AF_INET
, SOCK_STREAM
, 0);
1308 if (socket_fd
== -1) {
1309 fprintf(stderr
, "failed to make socket: %s (%d)\n", strerror(errno
), errno
);
1313 fcntl(socket_fd
, F_SETFL
, O_NONBLOCK
);
1315 if (setsockopt(socket_fd
, SOL_SOCKET
, SO_REUSEADDR
, &reuseaddr
,
1316 sizeof(int)) == -1) {
1317 fprintf(stderr
, "failed setsockopt: %s (%d)\n", strerror(errno
), errno
);
1321 struct sockaddr_in addr
;
1322 memset(&addr
, 0, sizeof(addr
));
1323 addr
.sin_family
= AF_INET
;
1324 addr
.sin_addr
.s_addr
= INADDR_ANY
;
1325 addr
.sin_port
= htons(port
);
1327 if (bind(socket_fd
, (struct sockaddr
*) &addr
, sizeof(addr
)) == -1) {
1328 fprintf(stderr
, "failed to bind socket: %s (%d)\n", strerror(errno
), errno
);
1332 if (listen(socket_fd
, 1) == -1) {
1333 fprintf(stderr
, "failed to listen on socket: %s (%d)\n", strerror(errno
), errno
);
1338 unsigned int gdbserver_t::find_access_size(reg_t address
, int length
)
1340 reg_t composite
= address
| length
;
1341 if ((composite
& 0x7) == 0 && xlen
>= 64)
1343 if ((composite
& 0x3) == 0)
1348 reg_t
gdbserver_t::translate(reg_t vaddr
)
1350 unsigned int vm
= virtual_memory();
1351 unsigned int levels
, ptidxbits
, ptesize
;
1376 sprintf(buf
, "VM mode %d is not supported by gdbserver.cc.", vm
);
1378 return true; // die doesn't return, but gcc doesn't know that.
1382 // Handle page tables here. There's a bunch of duplicated code with
1383 // collect_translation_info_op_t. :-(
1384 reg_t base
= sptbr
<< PGSHIFT
;
1385 int ptshift
= (levels
- 1) * ptidxbits
;
1386 for (unsigned int i
= 0; i
< levels
; i
++, ptshift
-= ptidxbits
) {
1387 reg_t idx
= (vaddr
>> (PGSHIFT
+ ptshift
)) & ((1 << ptidxbits
) - 1);
1389 reg_t pte_addr
= base
+ idx
* ptesize
;
1390 auto it
= pte_cache
.find(pte_addr
);
1391 if (it
== pte_cache
.end()) {
1392 fprintf(stderr
, "ERROR: gdbserver tried to translate 0x%016" PRIx64
1393 " without first collecting the relevant PTEs.\n", vaddr
);
1394 die("gdbserver_t::translate()");
1397 reg_t pte
= pte_cache
[pte_addr
];
1398 reg_t ppn
= pte
>> PTE_PPN_SHIFT
;
1400 if (PTE_TABLE(pte
)) { // next level of page table
1401 base
= ppn
<< PGSHIFT
;
1403 // We've collected all the data required for the translation.
1404 reg_t vpn
= vaddr
>> PGSHIFT
;
1405 reg_t paddr
= (ppn
| (vpn
& ((reg_t(1) << ptshift
) - 1))) << PGSHIFT
;
1406 paddr
+= vaddr
& (PGSIZE
-1);
1407 D(fprintf(stderr
, "gdbserver translate 0x%" PRIx64
" -> 0x%" PRIx64
"\n", vaddr
, paddr
));
1412 fprintf(stderr
, "ERROR: gdbserver tried to translate 0x%016" PRIx64
1413 " but the relevant PTEs are invalid.\n", vaddr
);
1414 // TODO: Is it better to throw an exception here?
1418 unsigned int gdbserver_t::privilege_mode()
1420 unsigned int mode
= get_field(dcsr
, DCSR_PRV
);
1421 if (get_field(mstatus
, MSTATUS_MPRV
))
1422 mode
= get_field(mstatus
, MSTATUS_MPP
);
1426 unsigned int gdbserver_t::virtual_memory()
1428 unsigned int mode
= privilege_mode();
1431 return get_field(mstatus
, MSTATUS_VM
);
1434 void gdbserver_t::dr_write32(unsigned int index
, uint32_t value
)
1436 sim
->debug_module
.ram_write32(index
, value
);
1439 void gdbserver_t::dr_write64(unsigned int index
, uint64_t value
)
1441 dr_write32(index
, value
);
1442 dr_write32(index
+1, value
>> 32);
1445 void gdbserver_t::dr_write(enum slot slot
, uint64_t value
)
1449 dr_write32(slot_offset32
[slot
], value
);
1452 dr_write64(slot_offset64
[slot
], value
);
1460 void gdbserver_t::dr_write_jump(unsigned int index
)
1462 dr_write32(index
, jal(0,
1463 (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*index
))));
1466 void gdbserver_t::dr_write_store(unsigned int index
, unsigned int reg
, enum slot slot
)
1468 assert(slot
!= SLOT_INST0
|| index
> 2);
1469 assert(slot
!= SLOT_DATA0
|| index
< 4 || index
> 6);
1470 assert(slot
!= SLOT_DATA1
|| index
< 5 || index
> 10);
1471 assert(slot
!= SLOT_DATA_LAST
|| index
< 6 || index
> 14);
1474 return dr_write32(index
,
1475 sw(reg
, 0, (uint16_t) DEBUG_RAM_START
+ 4 * slot_offset32
[slot
]));
1477 return dr_write32(index
,
1478 sd(reg
, 0, (uint16_t) DEBUG_RAM_START
+ 4 * slot_offset64
[slot
]));
1480 return dr_write32(index
,
1481 sq(reg
, 0, (uint16_t) DEBUG_RAM_START
+ 4 * slot_offset128
[slot
]));
1483 fprintf(stderr
, "xlen is %d!\n", xlen
);
1488 void gdbserver_t::dr_write_load(unsigned int index
, unsigned int reg
, enum slot slot
)
1492 return dr_write32(index
,
1493 lw(reg
, 0, (uint16_t) DEBUG_RAM_START
+ 4 * slot_offset32
[slot
]));
1495 return dr_write32(index
,
1496 ld(reg
, 0, (uint16_t) DEBUG_RAM_START
+ 4 * slot_offset64
[slot
]));
1498 return dr_write32(index
,
1499 lq(reg
, 0, (uint16_t) DEBUG_RAM_START
+ 4 * slot_offset128
[slot
]));
1501 fprintf(stderr
, "xlen is %d!\n", xlen
);
1506 uint32_t gdbserver_t::dr_read32(unsigned int index
)
1508 uint32_t value
= sim
->debug_module
.ram_read32(index
);
1509 D(fprintf(stderr
, "read32(%d) -> 0x%x\n", index
, value
));
1513 uint64_t gdbserver_t::dr_read64(unsigned int index
)
1515 return ((uint64_t) dr_read32(index
+1) << 32) | dr_read32(index
);
1518 uint64_t gdbserver_t::dr_read(enum slot slot
)
1522 return dr_read32(slot_offset32
[slot
]);
1524 return dr_read64(slot_offset64
[slot
]);
1532 void gdbserver_t::add_operation(operation_t
* operation
)
1534 operation_queue
.push(operation
);
1537 void gdbserver_t::accept()
1539 client_fd
= ::accept(socket_fd
, NULL
, NULL
);
1540 if (client_fd
== -1) {
1541 if (errno
== EAGAIN
) {
1542 // No client waiting to connect right now.
1544 fprintf(stderr
, "failed to accept on socket: %s (%d)\n", strerror(errno
),
1549 fcntl(client_fd
, F_SETFL
, O_NONBLOCK
);
1552 extended_mode
= false;
1554 // gdb wants the core to be halted when it attaches.
1555 add_operation(new halt_op_t(*this));
1559 void gdbserver_t::read()
1561 // Reading from a non-blocking socket still blocks if there is no data
1564 size_t count
= recv_buf
.contiguous_empty_size();
1565 ssize_t bytes
= ::read(client_fd
, recv_buf
.contiguous_empty(), count
);
1567 if (errno
== EAGAIN
) {
1568 // We'll try again the next call.
1570 fprintf(stderr
, "failed to read on socket: %s (%d)\n", strerror(errno
), errno
);
1573 } else if (bytes
== 0) {
1574 // The remote disconnected.
1576 processor_t
*p
= sim
->get_core(0);
1577 // TODO p->set_halted(false, HR_NONE);
1581 recv_buf
.data_added(bytes
);
1585 void gdbserver_t::write()
1587 if (send_buf
.empty())
1590 while (!send_buf
.empty()) {
1591 unsigned int count
= send_buf
.contiguous_data_size();
1593 ssize_t bytes
= ::write(client_fd
, send_buf
.contiguous_data(), count
);
1595 fprintf(stderr
, "failed to write to socket: %s (%d)\n", strerror(errno
), errno
);
1597 } else if (bytes
== 0) {
1598 // Client can't take any more data right now.
1601 D(fprintf(stderr
, "wrote %zd bytes: ", bytes
));
1602 for (int i
= 0; i
< bytes
; i
++) {
1603 D(fprintf(stderr
, "%c", send_buf
[i
]));
1605 D(fprintf(stderr
, "\n"));
1606 send_buf
.consume(bytes
);
1611 void print_packet(const std::vector
<uint8_t> &packet
)
1613 for (uint8_t c
: packet
) {
1614 if (c
>= ' ' and c
<= '~')
1615 fprintf(stderr
, "%c", c
);
1617 fprintf(stderr
, "\\x%02x", c
);
1619 fprintf(stderr
, "\n");
1622 uint8_t compute_checksum(const std::vector
<uint8_t> &packet
)
1624 uint8_t checksum
= 0;
1625 for (auto i
= packet
.begin() + 1; i
!= packet
.end() - 3; i
++ ) {
1631 uint8_t character_hex_value(uint8_t character
)
1633 if (character
>= '0' && character
<= '9')
1634 return character
- '0';
1635 if (character
>= 'a' && character
<= 'f')
1636 return 10 + character
- 'a';
1637 if (character
>= 'A' && character
<= 'F')
1638 return 10 + character
- 'A';
1642 uint8_t extract_checksum(const std::vector
<uint8_t> &packet
)
1644 return character_hex_value(*(packet
.end() - 1)) +
1645 16 * character_hex_value(*(packet
.end() - 2));
1648 void gdbserver_t::process_requests()
1650 // See https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html
1652 while (!recv_buf
.empty()) {
1653 std::vector
<uint8_t> packet
;
1654 for (unsigned int i
= 0; i
< recv_buf
.size(); i
++) {
1655 uint8_t b
= recv_buf
[i
];
1657 if (packet
.empty() && expect_ack
&& b
== '+') {
1658 recv_buf
.consume(1);
1662 if (packet
.empty() && b
== 3) {
1663 D(fprintf(stderr
, "Received interrupt\n"));
1664 recv_buf
.consume(1);
1670 // Start of new packet.
1671 if (!packet
.empty()) {
1672 fprintf(stderr
, "Received malformed %zd-byte packet from debug client: ",
1674 print_packet(packet
);
1675 recv_buf
.consume(i
);
1680 packet
.push_back(b
);
1682 // Packets consist of $<packet-data>#<checksum>
1683 // where <checksum> is
1684 if (packet
.size() >= 4 &&
1685 packet
[packet
.size()-3] == '#') {
1686 handle_packet(packet
);
1687 recv_buf
.consume(i
+1);
1691 // There's a partial packet in the buffer. Wait until we get more data to
1693 if (packet
.size()) {
1698 if (recv_buf
.full()) {
1700 "Receive buffer is full, but no complete packet was found!\n");
1701 for (unsigned line
= 0; line
< 8; line
++) {
1702 for (unsigned i
= 0; i
< 16; i
++) {
1703 fprintf(stderr
, "%02x ", recv_buf
.entry(line
* 16 + i
));
1705 for (unsigned i
= 0; i
< 16; i
++) {
1706 uint8_t e
= recv_buf
.entry(line
* 16 + i
);
1707 if (e
>= ' ' && e
<= '~')
1708 fprintf(stderr
, "%c", e
);
1710 fprintf(stderr
, ".");
1712 fprintf(stderr
, "\n");
1714 assert(!recv_buf
.full());
1718 void gdbserver_t::handle_halt_reason(const std::vector
<uint8_t> &packet
)
1723 void gdbserver_t::handle_general_registers_read(const std::vector
<uint8_t> &packet
)
1725 add_operation(new general_registers_read_op_t(*this));
1728 void gdbserver_t::set_interrupt(uint32_t hartid
) {
1729 sim
->debug_module
.set_interrupt(hartid
);
1732 // First byte is the most-significant one.
1733 // Eg. "08675309" becomes 0x08675309.
1734 uint64_t consume_hex_number(std::vector
<uint8_t>::const_iterator
&iter
,
1735 std::vector
<uint8_t>::const_iterator end
)
1739 while (iter
!= end
) {
1741 uint64_t c_value
= character_hex_value(c
);
1751 // First byte is the least-significant one.
1752 // Eg. "08675309" becomes 0x09536708
1753 uint64_t gdbserver_t::consume_hex_number_le(
1754 std::vector
<uint8_t>::const_iterator
&iter
,
1755 std::vector
<uint8_t>::const_iterator end
)
1758 unsigned int shift
= 4;
1760 while (iter
!= end
) {
1762 uint64_t c_value
= character_hex_value(c
);
1766 value
|= c_value
<< shift
;
1767 if ((shift
% 8) == 0)
1772 if (shift
> (xlen
+4)) {
1774 "gdb sent too many data bytes. That means it thinks XLEN is greater "
1775 "than %d.\nTo fix that, tell gdb: set arch riscv:rv%d\n",
1781 void consume_string(std::string
&str
, std::vector
<uint8_t>::const_iterator
&iter
,
1782 std::vector
<uint8_t>::const_iterator end
, uint8_t separator
)
1784 while (iter
!= end
&& *iter
!= separator
) {
1785 str
.append(1, (char) *iter
);
1790 void gdbserver_t::handle_register_read(const std::vector
<uint8_t> &packet
)
1794 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1795 unsigned int n
= consume_hex_number(iter
, packet
.end());
1797 return send_packet("E01");
1799 add_operation(new register_read_op_t(*this, n
));
1802 void gdbserver_t::handle_register_write(const std::vector
<uint8_t> &packet
)
1806 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1807 unsigned int n
= consume_hex_number(iter
, packet
.end());
1809 return send_packet("E05");
1812 reg_t value
= consume_hex_number_le(iter
, packet
.end());
1814 return send_packet("E06");
1816 processor_t
*p
= sim
->get_core(0);
1818 add_operation(new register_write_op_t(*this, n
, value
));
1820 return send_packet("OK");
1823 void gdbserver_t::handle_memory_read(const std::vector
<uint8_t> &packet
)
1826 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1827 reg_t address
= consume_hex_number(iter
, packet
.end());
1829 return send_packet("E10");
1831 reg_t length
= consume_hex_number(iter
, packet
.end());
1833 return send_packet("E11");
1835 add_operation(new collect_translation_info_op_t(*this, address
, length
));
1836 add_operation(new memory_read_op_t(*this, address
, length
));
1839 void gdbserver_t::handle_memory_binary_write(const std::vector
<uint8_t> &packet
)
1841 // X addr,length:XX...
1842 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1843 reg_t address
= consume_hex_number(iter
, packet
.end());
1845 return send_packet("E20");
1847 reg_t length
= consume_hex_number(iter
, packet
.end());
1849 return send_packet("E21");
1853 return send_packet("OK");
1856 unsigned char *data
= new unsigned char[length
];
1857 for (unsigned int i
= 0; i
< length
; i
++) {
1858 if (iter
== packet
.end()) {
1859 return send_packet("E22");
1864 // The binary data representation uses 7d (ascii ‘}’) as an escape
1865 // character. Any escaped byte is transmitted as the escape character
1866 // followed by the original character XORed with 0x20. For example, the
1867 // byte 0x7d would be transmitted as the two bytes 0x7d 0x5d. The bytes
1868 // 0x23 (ascii ‘#’), 0x24 (ascii ‘$’), and 0x7d (ascii ‘}’) must always
1870 if (iter
== packet
.end()) {
1871 return send_packet("E23");
1879 return send_packet("E4b"); // EOVERFLOW
1881 add_operation(new collect_translation_info_op_t(*this, address
, length
));
1882 add_operation(new memory_write_op_t(*this, address
, length
, data
));
1885 void gdbserver_t::handle_continue(const std::vector
<uint8_t> &packet
)
1888 processor_t
*p
= sim
->get_core(0);
1889 if (packet
[2] != '#') {
1890 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1891 dpc
= consume_hex_number(iter
, packet
.end());
1893 return send_packet("E30");
1896 add_operation(new maybe_restore_tselect_op_t(*this));
1897 add_operation(new maybe_restore_mstatus_op_t(*this));
1898 add_operation(new continue_op_t(*this, false));
1901 void gdbserver_t::handle_step(const std::vector
<uint8_t> &packet
)
1904 if (packet
[2] != '#') {
1905 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1907 //p->state.pc = consume_hex_number(iter, packet.end());
1909 return send_packet("E40");
1912 add_operation(new maybe_restore_tselect_op_t(*this));
1913 add_operation(new continue_op_t(*this, true));
1916 void gdbserver_t::handle_kill(const std::vector
<uint8_t> &packet
)
1919 // The exact effect of this packet is not specified.
1920 // Looks like OpenOCD disconnects?
1924 void gdbserver_t::handle_extended(const std::vector
<uint8_t> &packet
)
1926 // Enable extended mode. In extended mode, the remote server is made
1927 // persistent. The ‘R’ packet is used to restart the program being debugged.
1929 extended_mode
= true;
1932 void gdbserver_t::software_breakpoint_insert(reg_t vaddr
, unsigned int size
)
1934 fence_i_required
= true;
1935 add_operation(new collect_translation_info_op_t(*this, vaddr
, size
));
1936 unsigned char* inst
= new unsigned char[4];
1938 inst
[0] = MATCH_C_EBREAK
& 0xff;
1939 inst
[1] = (MATCH_C_EBREAK
>> 8) & 0xff;
1941 inst
[0] = MATCH_EBREAK
& 0xff;
1942 inst
[1] = (MATCH_EBREAK
>> 8) & 0xff;
1943 inst
[2] = (MATCH_EBREAK
>> 16) & 0xff;
1944 inst
[3] = (MATCH_EBREAK
>> 24) & 0xff;
1947 software_breakpoint_t bp
= {
1951 software_breakpoints
[vaddr
] = bp
;
1952 add_operation(new memory_read_op_t(*this, bp
.vaddr
, bp
.size
,
1953 software_breakpoints
[bp
.vaddr
].instruction
));
1954 add_operation(new memory_write_op_t(*this, bp
.vaddr
, bp
.size
, inst
));
1957 void gdbserver_t::software_breakpoint_remove(reg_t vaddr
, unsigned int size
)
1959 fence_i_required
= true;
1960 add_operation(new collect_translation_info_op_t(*this, vaddr
, size
));
1962 software_breakpoint_t found_bp
= software_breakpoints
[vaddr
];
1963 unsigned char* instruction
= new unsigned char[4];
1964 memcpy(instruction
, found_bp
.instruction
, 4);
1965 add_operation(new memory_write_op_t(*this, found_bp
.vaddr
,
1966 found_bp
.size
, instruction
));
1967 software_breakpoints
.erase(vaddr
);
1970 void gdbserver_t::hardware_breakpoint_insert(const hardware_breakpoint_t
&bp
)
1972 add_operation(new maybe_save_tselect_op_t(*this));
1973 add_operation(new hardware_breakpoint_insert_op_t(*this, bp
));
1976 void gdbserver_t::hardware_breakpoint_remove(const hardware_breakpoint_t
&bp
)
1978 add_operation(new maybe_save_tselect_op_t(*this));
1979 hardware_breakpoint_t found
= *hardware_breakpoints
.find(bp
);
1980 add_operation(new hardware_breakpoint_remove_op_t(*this, found
));
1983 void gdbserver_t::handle_breakpoint(const std::vector
<uint8_t> &packet
)
1985 // insert: Z type,addr,length
1986 // remove: z type,addr,length
1988 // type: 0 - software breakpoint, 1 - hardware breakpoint, 2 - write
1989 // watchpoint, 3 - read watchpoint, 4 - access watchpoint; addr is address;
1990 // length is in bytes. For a software breakpoint, length specifies the size
1991 // of the instruction to be patched. For hardware breakpoints and watchpoints
1992 // length specifies the memory region to be monitored. To avoid potential
1993 // problems with duplicate packets, the operations should be implemented in
1994 // an idempotent way.
1996 bool insert
= (packet
[1] == 'Z');
1997 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1998 gdb_breakpoint_type_t type
= static_cast<gdb_breakpoint_type_t
>(
1999 consume_hex_number(iter
, packet
.end()));
2001 return send_packet("E50");
2003 reg_t address
= consume_hex_number(iter
, packet
.end());
2005 return send_packet("E51");
2007 unsigned int size
= consume_hex_number(iter
, packet
.end());
2008 // There may be more options after a ; here, but we don't support that.
2010 return send_packet("E52");
2014 if (size
!= 2 && size
!= 4) {
2015 return send_packet("E53");
2018 software_breakpoint_insert(address
, size
);
2020 software_breakpoint_remove(address
, size
);
2029 hardware_breakpoint_t bp
= {
2033 bp
.load
= (type
== GB_READ
|| type
== GB_ACCESS
);
2034 bp
.store
= (type
== GB_WRITE
|| type
== GB_ACCESS
);
2035 bp
.execute
= (type
== GB_HARDWARE
|| type
== GB_ACCESS
);
2037 hardware_breakpoint_insert(bp
);
2038 // Insert might fail if there's no space, so the insert operation will
2039 // send its own OK (or not).
2042 hardware_breakpoint_remove(bp
);
2048 return send_packet("E56");
2051 return send_packet("OK");
2054 void gdbserver_t::handle_query(const std::vector
<uint8_t> &packet
)
2057 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
2059 consume_string(name
, iter
, packet
.end(), ':');
2060 if (iter
!= packet
.end())
2062 if (name
== "Supported") {
2064 while (iter
!= packet
.end()) {
2065 std::string feature
;
2066 consume_string(feature
, iter
, packet
.end(), ';');
2067 if (iter
!= packet
.end())
2069 if (feature
== "swbreak+") {
2073 send("PacketSize=131072;");
2074 return end_packet();
2077 D(fprintf(stderr
, "Unsupported query %s\n", name
.c_str()));
2078 return send_packet("");
2081 void gdbserver_t::handle_packet(const std::vector
<uint8_t> &packet
)
2083 if (compute_checksum(packet
) != extract_checksum(packet
)) {
2084 fprintf(stderr
, "Received %zd-byte packet with invalid checksum\n", packet
.size());
2085 fprintf(stderr
, "Computed checksum: %x\n", compute_checksum(packet
));
2086 print_packet(packet
);
2091 D(fprintf(stderr
, "Received %zd-byte packet from debug client: ", packet
.size()));
2092 D(print_packet(packet
));
2095 switch (packet
[1]) {
2097 return handle_extended(packet
);
2099 return handle_halt_reason(packet
);
2101 return handle_general_registers_read(packet
);
2103 // return handle_kill(packet);
2105 return handle_memory_read(packet
);
2107 // return handle_memory_write(packet);
2109 return handle_memory_binary_write(packet
);
2111 return handle_register_read(packet
);
2113 return handle_register_write(packet
);
2115 return handle_continue(packet
);
2117 return handle_step(packet
);
2120 return handle_breakpoint(packet
);
2123 return handle_query(packet
);
2127 D(fprintf(stderr
, "** Unsupported packet: "));
2128 D(print_packet(packet
));
2132 void gdbserver_t::handle_interrupt()
2134 processor_t
*p
= sim
->get_core(0);
2135 add_operation(new halt_op_t(*this, true));
2138 void gdbserver_t::handle()
2140 if (client_fd
> 0) {
2141 processor_t
*p
= sim
->get_core(0);
2143 bool interrupt
= sim
->debug_module
.get_interrupt(0);
2145 if (!interrupt
&& !operation_queue
.empty()) {
2146 operation_t
*operation
= operation_queue
.front();
2147 if (operation
->step()) {
2148 operation_queue
.pop();
2153 bool halt_notification
= sim
->debug_module
.get_halt_notification(0);
2154 if (halt_notification
) {
2155 sim
->debug_module
.clear_halt_notification(0);
2156 add_operation(new halt_op_t(*this, true));
2166 if (operation_queue
.empty()) {
2167 this->process_requests();
2171 void gdbserver_t::send(const char* msg
)
2173 unsigned int length
= strlen(msg
);
2174 for (const char *c
= msg
; *c
; c
++)
2175 running_checksum
+= *c
;
2176 send_buf
.append((const uint8_t *) msg
, length
);
2179 void gdbserver_t::send(uint64_t value
)
2182 for (unsigned int i
= 0; i
< 8; i
++) {
2183 sprintf(buffer
, "%02x", (int) (value
& 0xff));
2189 void gdbserver_t::send(uint32_t value
)
2192 for (unsigned int i
= 0; i
< 4; i
++) {
2193 sprintf(buffer
, "%02x", (int) (value
& 0xff));
2199 void gdbserver_t::send(uint8_t value
)
2202 sprintf(buffer
, "%02x", (int) value
);
2206 void gdbserver_t::send_packet(const char* data
)
2214 void gdbserver_t::start_packet()
2217 running_checksum
= 0;
2220 void gdbserver_t::end_packet(const char* data
)
2226 char checksum_string
[4];
2227 sprintf(checksum_string
, "#%02x", running_checksum
);
2228 send(checksum_string
);