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
, 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_read(SLOT_DATA_LAST
);
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_read(SLOT_DATA_LAST
);
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%lx -> 0x%lx (access=%d): ", vaddr
, paddr
,
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%lx] = 0x%lx\n", pte_addr
, gs
.pte_cache
[pte_addr
]));
1039 // Set up the next action.
1040 // We only get here for VM_SV32/39/38.
1042 if (!gs
.sptbr_valid
) {
1043 state
= STATE_READ_SPTBR
;
1044 gs
.dr_write32(0, csrr(S0
, CSR_SPTBR
));
1045 gs
.dr_write_store(1, S0
, SLOT_DATA0
);
1046 gs
.dr_write_jump(2);
1047 gs
.set_interrupt(0);
1051 reg_t base
= gs
.sptbr
<< PGSHIFT
;
1052 int ptshift
= (levels
- 1) * ptidxbits
;
1053 for (unsigned int i
= 0; i
< levels
; i
++, ptshift
-= ptidxbits
) {
1054 reg_t idx
= (vaddr
>> (PGSHIFT
+ ptshift
)) & ((1 << ptidxbits
) - 1);
1056 pte_addr
= base
+ idx
* ptesize
;
1057 auto it
= gs
.pte_cache
.find(pte_addr
);
1058 if (it
== gs
.pte_cache
.end()) {
1059 state
= STATE_READ_PTE
;
1061 gs
.dr_write32(0, lw(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
1062 gs
.dr_write32(1, lw(S1
, S0
, 0));
1063 gs
.dr_write32(2, sw(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
1065 assert(gs
.xlen
>= 64);
1066 gs
.dr_write32(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
1067 gs
.dr_write32(1, ld(S1
, S0
, 0));
1068 gs
.dr_write32(2, sd(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
1070 gs
.dr_write_jump(3);
1071 gs
.dr_write32(4, pte_addr
);
1072 gs
.dr_write32(5, pte_addr
>> 32);
1073 gs
.set_interrupt(0);
1077 reg_t pte
= gs
.pte_cache
[pte_addr
];
1078 reg_t ppn
= pte
>> PTE_PPN_SHIFT
;
1080 if (PTE_TABLE(pte
)) { // next level of page table
1081 base
= ppn
<< PGSHIFT
;
1083 // We've collected all the data required for the translation.
1088 "ERROR: gdbserver couldn't find appropriate PTEs to translate 0x%016" PRIx64
"\n",
1101 unsigned int levels
;
1102 unsigned int ptidxbits
;
1103 unsigned int ptesize
;
1107 class hardware_breakpoint_insert_op_t
: public operation_t
1110 hardware_breakpoint_insert_op_t(gdbserver_t
& gdbserver
,
1111 hardware_breakpoint_t bp
) :
1112 operation_t(gdbserver
), state(STATE_START
), bp(bp
) {};
1114 void write_new_index_program()
1116 gs
.dr_write_load(0, S0
, SLOT_DATA1
);
1117 gs
.dr_write32(1, csrw(S0
, CSR_TSELECT
));
1118 gs
.dr_write32(2, csrr(S0
, CSR_TSELECT
));
1119 gs
.dr_write_store(3, S0
, SLOT_DATA1
);
1120 gs
.dr_write_jump(4);
1121 gs
.dr_write(SLOT_DATA1
, bp
.index
);
1124 bool perform_step(unsigned int step
)
1129 write_new_index_program();
1130 state
= STATE_CHECK_INDEX
;
1133 case STATE_CHECK_INDEX
:
1134 if (gs
.dr_read(SLOT_DATA1
) != bp
.index
) {
1135 // We've exhausted breakpoints without finding an appropriate one.
1136 gs
.send_packet("E58");
1140 gs
.dr_write32(0, csrr(S0
, CSR_TDATA1
));
1141 gs
.dr_write_store(1, S0
, SLOT_DATA0
);
1142 gs
.dr_write_jump(2);
1143 state
= STATE_CHECK_MCONTROL
;
1146 case STATE_CHECK_MCONTROL
:
1148 reg_t mcontrol
= gs
.dr_read(SLOT_DATA0
);
1149 unsigned int type
= mcontrol
>> (gs
.xlen
- 4);
1151 // We've exhausted breakpoints without finding an appropriate one.
1152 gs
.send_packet("E58");
1157 !get_field(mcontrol
, MCONTROL_EXECUTE
) &&
1158 !get_field(mcontrol
, MCONTROL_LOAD
) &&
1159 !get_field(mcontrol
, MCONTROL_STORE
)) {
1160 // Found an unused trigger.
1161 gs
.dr_write_load(0, S0
, SLOT_DATA1
);
1162 gs
.dr_write32(1, csrw(S0
, CSR_TDATA1
));
1163 gs
.dr_write_jump(2);
1164 mcontrol
= set_field(0, MCONTROL_ACTION
, MCONTROL_ACTION_DEBUG_MODE
);
1165 mcontrol
= set_field(mcontrol
, MCONTROL_DMODE(gs
.xlen
), 1);
1166 mcontrol
= set_field(mcontrol
, MCONTROL_MATCH
, MCONTROL_MATCH_EQUAL
);
1167 mcontrol
= set_field(mcontrol
, MCONTROL_M
, 1);
1168 mcontrol
= set_field(mcontrol
, MCONTROL_H
, 1);
1169 mcontrol
= set_field(mcontrol
, MCONTROL_S
, 1);
1170 mcontrol
= set_field(mcontrol
, MCONTROL_U
, 1);
1171 mcontrol
= set_field(mcontrol
, MCONTROL_EXECUTE
, bp
.execute
);
1172 mcontrol
= set_field(mcontrol
, MCONTROL_LOAD
, bp
.load
);
1173 mcontrol
= set_field(mcontrol
, MCONTROL_STORE
, bp
.store
);
1174 // For store triggers it's nicer to fire just before the
1175 // instruction than just after. However, gdb doesn't clear the
1176 // breakpoints and step before resuming from a store trigger.
1177 // That means that without extra code, you'll keep hitting the
1178 // same watchpoint over and over again. That's not useful at all.
1179 // Instead of fixing this the right way, just set timing=1 for
1181 if (bp
.load
|| bp
.store
)
1182 mcontrol
= set_field(mcontrol
, MCONTROL_TIMING
, 1);
1184 gs
.dr_write(SLOT_DATA1
, mcontrol
);
1185 state
= STATE_WRITE_ADDRESS
;
1188 write_new_index_program();
1189 state
= STATE_CHECK_INDEX
;
1194 case STATE_WRITE_ADDRESS
:
1196 gs
.dr_write_load(0, S0
, SLOT_DATA1
);
1197 gs
.dr_write32(1, csrw(S0
, CSR_TDATA2
));
1198 gs
.dr_write_jump(2);
1199 gs
.dr_write(SLOT_DATA1
, bp
.vaddr
);
1200 gs
.set_interrupt(0);
1201 gs
.send_packet("OK");
1203 gs
.hardware_breakpoints
.insert(bp
);
1209 gs
.set_interrupt(0);
1217 STATE_CHECK_MCONTROL
,
1220 hardware_breakpoint_t bp
;
1223 class maybe_save_tselect_op_t
: public operation_t
1226 maybe_save_tselect_op_t(gdbserver_t
& gdbserver
) : operation_t(gdbserver
) {};
1227 bool perform_step(unsigned int step
) {
1228 if (gs
.tselect_valid
)
1233 gs
.dr_write32(0, csrr(S0
, CSR_TDATA1
));
1234 gs
.dr_write_store(1, S0
, SLOT_DATA0
);
1235 gs
.dr_write_jump(2);
1236 gs
.set_interrupt(0);
1239 gs
.tselect
= gs
.dr_read(SLOT_DATA0
);
1240 gs
.tselect_valid
= true;
1247 class maybe_restore_tselect_op_t
: public operation_t
1250 maybe_restore_tselect_op_t(gdbserver_t
& gdbserver
) : operation_t(gdbserver
) {};
1251 bool perform_step(unsigned int step
) {
1252 if (gs
.tselect_valid
) {
1253 gs
.dr_write_load(0, S0
, SLOT_DATA1
);
1254 gs
.dr_write32(1, csrw(S0
, CSR_TSELECT
));
1255 gs
.dr_write_jump(2);
1256 gs
.dr_write(SLOT_DATA1
, gs
.tselect
);
1262 class maybe_restore_mstatus_op_t
: public operation_t
1265 maybe_restore_mstatus_op_t(gdbserver_t
& gdbserver
) : operation_t(gdbserver
) {};
1266 bool perform_step(unsigned int step
) {
1267 if (gs
.mstatus_dirty
) {
1268 gs
.dr_write_load(0, S0
, SLOT_DATA1
);
1269 gs
.dr_write32(1, csrw(S0
, CSR_MSTATUS
));
1270 gs
.dr_write_jump(2);
1271 gs
.dr_write(SLOT_DATA1
, gs
.mstatus
);
1277 class hardware_breakpoint_remove_op_t
: public operation_t
1280 hardware_breakpoint_remove_op_t(gdbserver_t
& gdbserver
,
1281 hardware_breakpoint_t bp
) :
1282 operation_t(gdbserver
), bp(bp
) {};
1284 bool perform_step(unsigned int step
) {
1285 gs
.dr_write32(0, addi(S0
, ZERO
, bp
.index
));
1286 gs
.dr_write32(1, csrw(S0
, CSR_TSELECT
));
1287 gs
.dr_write32(2, csrw(ZERO
, CSR_TDATA1
));
1288 gs
.dr_write_jump(3);
1289 gs
.set_interrupt(0);
1294 hardware_breakpoint_t bp
;
1297 ////////////////////////////// gdbserver itself
1299 gdbserver_t::gdbserver_t(uint16_t port
, sim_t
*sim
) :
1303 // gdb likes to send 0x100000 bytes at once when downloading.
1304 recv_buf(0x180000), send_buf(64 * 1024)
1306 socket_fd
= socket(AF_INET
, SOCK_STREAM
, 0);
1307 if (socket_fd
== -1) {
1308 fprintf(stderr
, "failed to make socket: %s (%d)\n", strerror(errno
), errno
);
1312 fcntl(socket_fd
, F_SETFL
, O_NONBLOCK
);
1314 if (setsockopt(socket_fd
, SOL_SOCKET
, SO_REUSEADDR
, &reuseaddr
,
1315 sizeof(int)) == -1) {
1316 fprintf(stderr
, "failed setsockopt: %s (%d)\n", strerror(errno
), errno
);
1320 struct sockaddr_in addr
;
1321 memset(&addr
, 0, sizeof(addr
));
1322 addr
.sin_family
= AF_INET
;
1323 addr
.sin_addr
.s_addr
= INADDR_ANY
;
1324 addr
.sin_port
= htons(port
);
1326 if (bind(socket_fd
, (struct sockaddr
*) &addr
, sizeof(addr
)) == -1) {
1327 fprintf(stderr
, "failed to bind socket: %s (%d)\n", strerror(errno
), errno
);
1331 if (listen(socket_fd
, 1) == -1) {
1332 fprintf(stderr
, "failed to listen on socket: %s (%d)\n", strerror(errno
), errno
);
1337 unsigned int gdbserver_t::find_access_size(reg_t address
, int length
)
1339 reg_t composite
= address
| length
;
1340 if ((composite
& 0x7) == 0 && xlen
>= 64)
1342 if ((composite
& 0x3) == 0)
1347 reg_t
gdbserver_t::translate(reg_t vaddr
)
1349 unsigned int vm
= virtual_memory();
1350 unsigned int levels
, ptidxbits
, ptesize
;
1375 sprintf(buf
, "VM mode %d is not supported by gdbserver.cc.", vm
);
1377 return true; // die doesn't return, but gcc doesn't know that.
1381 // Handle page tables here. There's a bunch of duplicated code with
1382 // collect_translation_info_op_t. :-(
1383 reg_t base
= sptbr
<< PGSHIFT
;
1384 int ptshift
= (levels
- 1) * ptidxbits
;
1385 for (unsigned int i
= 0; i
< levels
; i
++, ptshift
-= ptidxbits
) {
1386 reg_t idx
= (vaddr
>> (PGSHIFT
+ ptshift
)) & ((1 << ptidxbits
) - 1);
1388 reg_t pte_addr
= base
+ idx
* ptesize
;
1389 auto it
= pte_cache
.find(pte_addr
);
1390 if (it
== pte_cache
.end()) {
1391 fprintf(stderr
, "ERROR: gdbserver tried to translate 0x%016" PRIx64
1392 " without first collecting the relevant PTEs.\n", vaddr
);
1393 die("gdbserver_t::translate()");
1396 reg_t pte
= pte_cache
[pte_addr
];
1397 reg_t ppn
= pte
>> PTE_PPN_SHIFT
;
1399 if (PTE_TABLE(pte
)) { // next level of page table
1400 base
= ppn
<< PGSHIFT
;
1402 // We've collected all the data required for the translation.
1403 reg_t vpn
= vaddr
>> PGSHIFT
;
1404 reg_t paddr
= (ppn
| (vpn
& ((reg_t(1) << ptshift
) - 1))) << PGSHIFT
;
1405 paddr
+= vaddr
& (PGSIZE
-1);
1406 D(fprintf(stderr
, "gdbserver translate 0x%lx -> 0x%lx\n", vaddr
, paddr
));
1411 fprintf(stderr
, "ERROR: gdbserver tried to translate 0x%016" PRIx64
1412 " but the relevant PTEs are invalid.\n", vaddr
);
1413 // TODO: Is it better to throw an exception here?
1417 unsigned int gdbserver_t::privilege_mode()
1419 unsigned int mode
= get_field(dcsr
, DCSR_PRV
);
1420 if (get_field(mstatus
, MSTATUS_MPRV
))
1421 mode
= get_field(mstatus
, MSTATUS_MPP
);
1425 unsigned int gdbserver_t::virtual_memory()
1427 unsigned int mode
= privilege_mode();
1430 return get_field(mstatus
, MSTATUS_VM
);
1433 void gdbserver_t::dr_write32(unsigned int index
, uint32_t value
)
1435 sim
->debug_module
.ram_write32(index
, value
);
1438 void gdbserver_t::dr_write64(unsigned int index
, uint64_t value
)
1440 dr_write32(index
, value
);
1441 dr_write32(index
+1, value
>> 32);
1444 void gdbserver_t::dr_write(enum slot slot
, uint64_t value
)
1448 dr_write32(slot_offset32
[slot
], value
);
1451 dr_write64(slot_offset64
[slot
], value
);
1459 void gdbserver_t::dr_write_jump(unsigned int index
)
1461 dr_write32(index
, jal(0,
1462 (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*index
))));
1465 void gdbserver_t::dr_write_store(unsigned int index
, unsigned int reg
, enum slot slot
)
1467 assert(slot
!= SLOT_INST0
|| index
> 2);
1468 assert(slot
!= SLOT_DATA0
|| index
< 4 || index
> 6);
1469 assert(slot
!= SLOT_DATA1
|| index
< 5 || index
> 10);
1470 assert(slot
!= SLOT_DATA_LAST
|| index
< 6 || index
> 14);
1473 return dr_write32(index
,
1474 sw(reg
, 0, (uint16_t) DEBUG_RAM_START
+ 4 * slot_offset32
[slot
]));
1476 return dr_write32(index
,
1477 sd(reg
, 0, (uint16_t) DEBUG_RAM_START
+ 4 * slot_offset64
[slot
]));
1479 return dr_write32(index
,
1480 sq(reg
, 0, (uint16_t) DEBUG_RAM_START
+ 4 * slot_offset128
[slot
]));
1482 fprintf(stderr
, "xlen is %d!\n", xlen
);
1487 void gdbserver_t::dr_write_load(unsigned int index
, unsigned int reg
, enum slot slot
)
1491 return dr_write32(index
,
1492 lw(reg
, 0, (uint16_t) DEBUG_RAM_START
+ 4 * slot_offset32
[slot
]));
1494 return dr_write32(index
,
1495 ld(reg
, 0, (uint16_t) DEBUG_RAM_START
+ 4 * slot_offset64
[slot
]));
1497 return dr_write32(index
,
1498 lq(reg
, 0, (uint16_t) DEBUG_RAM_START
+ 4 * slot_offset128
[slot
]));
1500 fprintf(stderr
, "xlen is %d!\n", xlen
);
1505 uint32_t gdbserver_t::dr_read32(unsigned int index
)
1507 uint32_t value
= sim
->debug_module
.ram_read32(index
);
1508 D(fprintf(stderr
, "read32(%d) -> 0x%x\n", index
, value
));
1512 uint64_t gdbserver_t::dr_read64(unsigned int index
)
1514 return ((uint64_t) dr_read32(index
+1) << 32) | dr_read32(index
);
1517 uint64_t gdbserver_t::dr_read(enum slot slot
)
1521 return dr_read32(slot_offset32
[slot
]);
1523 return dr_read64(slot_offset64
[slot
]);
1531 void gdbserver_t::add_operation(operation_t
* operation
)
1533 operation_queue
.push(operation
);
1536 void gdbserver_t::accept()
1538 client_fd
= ::accept(socket_fd
, NULL
, NULL
);
1539 if (client_fd
== -1) {
1540 if (errno
== EAGAIN
) {
1541 // No client waiting to connect right now.
1543 fprintf(stderr
, "failed to accept on socket: %s (%d)\n", strerror(errno
),
1548 fcntl(client_fd
, F_SETFL
, O_NONBLOCK
);
1551 extended_mode
= false;
1553 // gdb wants the core to be halted when it attaches.
1554 add_operation(new halt_op_t(*this));
1558 void gdbserver_t::read()
1560 // Reading from a non-blocking socket still blocks if there is no data
1563 size_t count
= recv_buf
.contiguous_empty_size();
1564 ssize_t bytes
= ::read(client_fd
, recv_buf
.contiguous_empty(), count
);
1566 if (errno
== EAGAIN
) {
1567 // We'll try again the next call.
1569 fprintf(stderr
, "failed to read on socket: %s (%d)\n", strerror(errno
), errno
);
1572 } else if (bytes
== 0) {
1573 // The remote disconnected.
1575 processor_t
*p
= sim
->get_core(0);
1576 // TODO p->set_halted(false, HR_NONE);
1580 recv_buf
.data_added(bytes
);
1584 void gdbserver_t::write()
1586 if (send_buf
.empty())
1589 while (!send_buf
.empty()) {
1590 unsigned int count
= send_buf
.contiguous_data_size();
1592 ssize_t bytes
= ::write(client_fd
, send_buf
.contiguous_data(), count
);
1594 fprintf(stderr
, "failed to write to socket: %s (%d)\n", strerror(errno
), errno
);
1596 } else if (bytes
== 0) {
1597 // Client can't take any more data right now.
1600 D(fprintf(stderr
, "wrote %ld bytes: ", bytes
));
1601 for (unsigned int i
= 0; i
< bytes
; i
++) {
1602 D(fprintf(stderr
, "%c", send_buf
[i
]));
1604 D(fprintf(stderr
, "\n"));
1605 send_buf
.consume(bytes
);
1610 void print_packet(const std::vector
<uint8_t> &packet
)
1612 for (uint8_t c
: packet
) {
1613 if (c
>= ' ' and c
<= '~')
1614 fprintf(stderr
, "%c", c
);
1616 fprintf(stderr
, "\\x%02x", c
);
1618 fprintf(stderr
, "\n");
1621 uint8_t compute_checksum(const std::vector
<uint8_t> &packet
)
1623 uint8_t checksum
= 0;
1624 for (auto i
= packet
.begin() + 1; i
!= packet
.end() - 3; i
++ ) {
1630 uint8_t character_hex_value(uint8_t character
)
1632 if (character
>= '0' && character
<= '9')
1633 return character
- '0';
1634 if (character
>= 'a' && character
<= 'f')
1635 return 10 + character
- 'a';
1636 if (character
>= 'A' && character
<= 'F')
1637 return 10 + character
- 'A';
1641 uint8_t extract_checksum(const std::vector
<uint8_t> &packet
)
1643 return character_hex_value(*(packet
.end() - 1)) +
1644 16 * character_hex_value(*(packet
.end() - 2));
1647 void gdbserver_t::process_requests()
1649 // See https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html
1651 while (!recv_buf
.empty()) {
1652 std::vector
<uint8_t> packet
;
1653 for (unsigned int i
= 0; i
< recv_buf
.size(); i
++) {
1654 uint8_t b
= recv_buf
[i
];
1656 if (packet
.empty() && expect_ack
&& b
== '+') {
1657 recv_buf
.consume(1);
1661 if (packet
.empty() && b
== 3) {
1662 D(fprintf(stderr
, "Received interrupt\n"));
1663 recv_buf
.consume(1);
1669 // Start of new packet.
1670 if (!packet
.empty()) {
1671 fprintf(stderr
, "Received malformed %ld-byte packet from debug client: ",
1673 print_packet(packet
);
1674 recv_buf
.consume(i
);
1679 packet
.push_back(b
);
1681 // Packets consist of $<packet-data>#<checksum>
1682 // where <checksum> is
1683 if (packet
.size() >= 4 &&
1684 packet
[packet
.size()-3] == '#') {
1685 handle_packet(packet
);
1686 recv_buf
.consume(i
+1);
1690 // There's a partial packet in the buffer. Wait until we get more data to
1692 if (packet
.size()) {
1697 if (recv_buf
.full()) {
1699 "Receive buffer is full, but no complete packet was found!\n");
1700 for (unsigned line
= 0; line
< 8; line
++) {
1701 for (unsigned i
= 0; i
< 16; i
++) {
1702 fprintf(stderr
, "%02x ", recv_buf
.entry(line
* 16 + i
));
1704 for (unsigned i
= 0; i
< 16; i
++) {
1705 uint8_t e
= recv_buf
.entry(line
* 16 + i
);
1706 if (e
>= ' ' && e
<= '~')
1707 fprintf(stderr
, "%c", e
);
1709 fprintf(stderr
, ".");
1711 fprintf(stderr
, "\n");
1713 assert(!recv_buf
.full());
1717 void gdbserver_t::handle_halt_reason(const std::vector
<uint8_t> &packet
)
1722 void gdbserver_t::handle_general_registers_read(const std::vector
<uint8_t> &packet
)
1724 add_operation(new general_registers_read_op_t(*this));
1727 void gdbserver_t::set_interrupt(uint32_t hartid
) {
1728 sim
->debug_module
.set_interrupt(hartid
);
1731 // First byte is the most-significant one.
1732 // Eg. "08675309" becomes 0x08675309.
1733 uint64_t consume_hex_number(std::vector
<uint8_t>::const_iterator
&iter
,
1734 std::vector
<uint8_t>::const_iterator end
)
1738 while (iter
!= end
) {
1740 uint64_t c_value
= character_hex_value(c
);
1750 // First byte is the least-significant one.
1751 // Eg. "08675309" becomes 0x09536708
1752 uint64_t gdbserver_t::consume_hex_number_le(
1753 std::vector
<uint8_t>::const_iterator
&iter
,
1754 std::vector
<uint8_t>::const_iterator end
)
1757 unsigned int shift
= 4;
1759 while (iter
!= end
) {
1761 uint64_t c_value
= character_hex_value(c
);
1765 value
|= c_value
<< shift
;
1766 if ((shift
% 8) == 0)
1771 if (shift
> (xlen
+4)) {
1773 "gdb sent too many data bytes. That means it thinks XLEN is greater "
1774 "than %d.\nTo fix that, tell gdb: set arch riscv:rv%d\n",
1780 void consume_string(std::string
&str
, std::vector
<uint8_t>::const_iterator
&iter
,
1781 std::vector
<uint8_t>::const_iterator end
, uint8_t separator
)
1783 while (iter
!= end
&& *iter
!= separator
) {
1784 str
.append(1, (char) *iter
);
1789 void gdbserver_t::handle_register_read(const std::vector
<uint8_t> &packet
)
1793 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1794 unsigned int n
= consume_hex_number(iter
, packet
.end());
1796 return send_packet("E01");
1798 add_operation(new register_read_op_t(*this, n
));
1801 void gdbserver_t::handle_register_write(const std::vector
<uint8_t> &packet
)
1805 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1806 unsigned int n
= consume_hex_number(iter
, packet
.end());
1808 return send_packet("E05");
1811 reg_t value
= consume_hex_number_le(iter
, packet
.end());
1813 return send_packet("E06");
1815 processor_t
*p
= sim
->get_core(0);
1817 add_operation(new register_write_op_t(*this, n
, value
));
1819 return send_packet("OK");
1822 void gdbserver_t::handle_memory_read(const std::vector
<uint8_t> &packet
)
1825 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1826 reg_t address
= consume_hex_number(iter
, packet
.end());
1828 return send_packet("E10");
1830 reg_t length
= consume_hex_number(iter
, packet
.end());
1832 return send_packet("E11");
1834 add_operation(new collect_translation_info_op_t(*this, address
, length
));
1835 add_operation(new memory_read_op_t(*this, address
, length
));
1838 void gdbserver_t::handle_memory_binary_write(const std::vector
<uint8_t> &packet
)
1840 // X addr,length:XX...
1841 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1842 reg_t address
= consume_hex_number(iter
, packet
.end());
1844 return send_packet("E20");
1846 reg_t length
= consume_hex_number(iter
, packet
.end());
1848 return send_packet("E21");
1852 return send_packet("OK");
1855 unsigned char *data
= new unsigned char[length
];
1856 for (unsigned int i
= 0; i
< length
; i
++) {
1857 if (iter
== packet
.end()) {
1858 return send_packet("E22");
1863 // The binary data representation uses 7d (ascii ‘}’) as an escape
1864 // character. Any escaped byte is transmitted as the escape character
1865 // followed by the original character XORed with 0x20. For example, the
1866 // byte 0x7d would be transmitted as the two bytes 0x7d 0x5d. The bytes
1867 // 0x23 (ascii ‘#’), 0x24 (ascii ‘$’), and 0x7d (ascii ‘}’) must always
1869 if (iter
== packet
.end()) {
1870 return send_packet("E23");
1878 return send_packet("E4b"); // EOVERFLOW
1880 add_operation(new collect_translation_info_op_t(*this, address
, length
));
1881 add_operation(new memory_write_op_t(*this, address
, length
, data
));
1884 void gdbserver_t::handle_continue(const std::vector
<uint8_t> &packet
)
1887 processor_t
*p
= sim
->get_core(0);
1888 if (packet
[2] != '#') {
1889 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1890 dpc
= consume_hex_number(iter
, packet
.end());
1892 return send_packet("E30");
1895 add_operation(new maybe_restore_tselect_op_t(*this));
1896 add_operation(new maybe_restore_mstatus_op_t(*this));
1897 add_operation(new continue_op_t(*this, false));
1900 void gdbserver_t::handle_step(const std::vector
<uint8_t> &packet
)
1903 if (packet
[2] != '#') {
1904 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1906 //p->state.pc = consume_hex_number(iter, packet.end());
1908 return send_packet("E40");
1911 add_operation(new maybe_restore_tselect_op_t(*this));
1912 add_operation(new continue_op_t(*this, true));
1915 void gdbserver_t::handle_kill(const std::vector
<uint8_t> &packet
)
1918 // The exact effect of this packet is not specified.
1919 // Looks like OpenOCD disconnects?
1923 void gdbserver_t::handle_extended(const std::vector
<uint8_t> &packet
)
1925 // Enable extended mode. In extended mode, the remote server is made
1926 // persistent. The ‘R’ packet is used to restart the program being debugged.
1928 extended_mode
= true;
1931 void gdbserver_t::software_breakpoint_insert(reg_t vaddr
, unsigned int size
)
1933 fence_i_required
= true;
1934 add_operation(new collect_translation_info_op_t(*this, vaddr
, size
));
1935 unsigned char* inst
= new unsigned char[4];
1937 inst
[0] = MATCH_C_EBREAK
& 0xff;
1938 inst
[1] = (MATCH_C_EBREAK
>> 8) & 0xff;
1940 inst
[0] = MATCH_EBREAK
& 0xff;
1941 inst
[1] = (MATCH_EBREAK
>> 8) & 0xff;
1942 inst
[2] = (MATCH_EBREAK
>> 16) & 0xff;
1943 inst
[3] = (MATCH_EBREAK
>> 24) & 0xff;
1946 software_breakpoint_t bp
= {
1950 software_breakpoints
[vaddr
] = bp
;
1951 add_operation(new memory_read_op_t(*this, bp
.vaddr
, bp
.size
,
1952 software_breakpoints
[bp
.vaddr
].instruction
));
1953 add_operation(new memory_write_op_t(*this, bp
.vaddr
, bp
.size
, inst
));
1956 void gdbserver_t::software_breakpoint_remove(reg_t vaddr
, unsigned int size
)
1958 fence_i_required
= true;
1959 add_operation(new collect_translation_info_op_t(*this, vaddr
, size
));
1961 software_breakpoint_t found_bp
= software_breakpoints
[vaddr
];
1962 unsigned char* instruction
= new unsigned char[4];
1963 memcpy(instruction
, found_bp
.instruction
, 4);
1964 add_operation(new memory_write_op_t(*this, found_bp
.vaddr
,
1965 found_bp
.size
, instruction
));
1966 software_breakpoints
.erase(vaddr
);
1969 void gdbserver_t::hardware_breakpoint_insert(const hardware_breakpoint_t
&bp
)
1971 add_operation(new maybe_save_tselect_op_t(*this));
1972 add_operation(new hardware_breakpoint_insert_op_t(*this, bp
));
1975 void gdbserver_t::hardware_breakpoint_remove(const hardware_breakpoint_t
&bp
)
1977 add_operation(new maybe_save_tselect_op_t(*this));
1978 hardware_breakpoint_t found
= *hardware_breakpoints
.find(bp
);
1979 add_operation(new hardware_breakpoint_remove_op_t(*this, found
));
1982 void gdbserver_t::handle_breakpoint(const std::vector
<uint8_t> &packet
)
1984 // insert: Z type,addr,length
1985 // remove: z type,addr,length
1987 // type: 0 - software breakpoint, 1 - hardware breakpoint, 2 - write
1988 // watchpoint, 3 - read watchpoint, 4 - access watchpoint; addr is address;
1989 // length is in bytes. For a software breakpoint, length specifies the size
1990 // of the instruction to be patched. For hardware breakpoints and watchpoints
1991 // length specifies the memory region to be monitored. To avoid potential
1992 // problems with duplicate packets, the operations should be implemented in
1993 // an idempotent way.
1995 bool insert
= (packet
[1] == 'Z');
1996 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1997 gdb_breakpoint_type_t type
= static_cast<gdb_breakpoint_type_t
>(
1998 consume_hex_number(iter
, packet
.end()));
2000 return send_packet("E50");
2002 reg_t address
= consume_hex_number(iter
, packet
.end());
2004 return send_packet("E51");
2006 unsigned int size
= consume_hex_number(iter
, packet
.end());
2007 // There may be more options after a ; here, but we don't support that.
2009 return send_packet("E52");
2013 if (size
!= 2 && size
!= 4) {
2014 return send_packet("E53");
2017 software_breakpoint_insert(address
, size
);
2019 software_breakpoint_remove(address
, size
);
2028 hardware_breakpoint_t bp
= {
2032 bp
.load
= (type
== GB_READ
|| type
== GB_ACCESS
);
2033 bp
.store
= (type
== GB_WRITE
|| type
== GB_ACCESS
);
2034 bp
.execute
= (type
== GB_HARDWARE
|| type
== GB_ACCESS
);
2036 hardware_breakpoint_insert(bp
);
2037 // Insert might fail if there's no space, so the insert operation will
2038 // send its own OK (or not).
2041 hardware_breakpoint_remove(bp
);
2047 return send_packet("E56");
2050 return send_packet("OK");
2053 void gdbserver_t::handle_query(const std::vector
<uint8_t> &packet
)
2056 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
2058 consume_string(name
, iter
, packet
.end(), ':');
2059 if (iter
!= packet
.end())
2061 if (name
== "Supported") {
2063 while (iter
!= packet
.end()) {
2064 std::string feature
;
2065 consume_string(feature
, iter
, packet
.end(), ';');
2066 if (iter
!= packet
.end())
2068 if (feature
== "swbreak+") {
2072 send("PacketSize=131072;");
2073 return end_packet();
2076 D(fprintf(stderr
, "Unsupported query %s\n", name
.c_str()));
2077 return send_packet("");
2080 void gdbserver_t::handle_packet(const std::vector
<uint8_t> &packet
)
2082 if (compute_checksum(packet
) != extract_checksum(packet
)) {
2083 fprintf(stderr
, "Received %ld-byte packet with invalid checksum\n", packet
.size());
2084 fprintf(stderr
, "Computed checksum: %x\n", compute_checksum(packet
));
2085 print_packet(packet
);
2090 D(fprintf(stderr
, "Received %ld-byte packet from debug client: ", packet
.size()));
2091 D(print_packet(packet
));
2094 switch (packet
[1]) {
2096 return handle_extended(packet
);
2098 return handle_halt_reason(packet
);
2100 return handle_general_registers_read(packet
);
2102 // return handle_kill(packet);
2104 return handle_memory_read(packet
);
2106 // return handle_memory_write(packet);
2108 return handle_memory_binary_write(packet
);
2110 return handle_register_read(packet
);
2112 return handle_register_write(packet
);
2114 return handle_continue(packet
);
2116 return handle_step(packet
);
2119 return handle_breakpoint(packet
);
2122 return handle_query(packet
);
2126 D(fprintf(stderr
, "** Unsupported packet: "));
2127 D(print_packet(packet
));
2131 void gdbserver_t::handle_interrupt()
2133 processor_t
*p
= sim
->get_core(0);
2134 add_operation(new halt_op_t(*this, true));
2137 void gdbserver_t::handle()
2139 if (client_fd
> 0) {
2140 processor_t
*p
= sim
->get_core(0);
2142 bool interrupt
= sim
->debug_module
.get_interrupt(0);
2144 if (!interrupt
&& !operation_queue
.empty()) {
2145 operation_t
*operation
= operation_queue
.front();
2146 if (operation
->step()) {
2147 operation_queue
.pop();
2152 bool halt_notification
= sim
->debug_module
.get_halt_notification(0);
2153 if (halt_notification
) {
2154 sim
->debug_module
.clear_halt_notification(0);
2155 add_operation(new halt_op_t(*this, true));
2165 if (operation_queue
.empty()) {
2166 this->process_requests();
2170 void gdbserver_t::send(const char* msg
)
2172 unsigned int length
= strlen(msg
);
2173 for (const char *c
= msg
; *c
; c
++)
2174 running_checksum
+= *c
;
2175 send_buf
.append((const uint8_t *) msg
, length
);
2178 void gdbserver_t::send(uint64_t value
)
2181 for (unsigned int i
= 0; i
< 8; i
++) {
2182 sprintf(buffer
, "%02x", (int) (value
& 0xff));
2188 void gdbserver_t::send(uint32_t value
)
2191 for (unsigned int i
= 0; i
< 4; i
++) {
2192 sprintf(buffer
, "%02x", (int) (value
& 0xff));
2198 void gdbserver_t::send(uint8_t value
)
2201 sprintf(buffer
, "%02x", (int) value
);
2205 void gdbserver_t::send_packet(const char* data
)
2213 void gdbserver_t::start_packet()
2216 running_checksum
= 0;
2219 void gdbserver_t::end_packet(const char* data
)
2225 char checksum_string
[4];
2226 sprintf(checksum_string
, "#%02x", running_checksum
);
2227 send(checksum_string
);