6 #include <sys/socket.h>
17 #include "gdbserver.h"
20 #define C_EBREAK 0x9002
21 #define EBREAK 0x00100073
23 //////////////////////////////////////// Utility Functions
32 const int debug_gdbserver
= 0;
34 void die(const char* msg
)
36 fprintf(stderr
, "gdbserver code died: %s\n", msg
);
40 // gdb's register list is defined in riscv_gdb_reg_names gdb/riscv-tdep.c in
41 // its source tree. We must interpret the numbers the same here.
53 //////////////////////////////////////// Functions to generate RISC-V opcodes.
55 // TODO: Does this already exist somewhere?
58 // Using regnames.cc as source. The RVG Calling Convention of the 2.0 RISC-V
59 // spec says it should be 2 and 3.
62 static uint32_t bits(uint32_t value
, unsigned int hi
, unsigned int lo
) {
63 return (value
>> lo
) & ((1 << (hi
+1-lo
)) - 1);
66 static uint32_t bit(uint32_t value
, unsigned int b
) {
67 return (value
>> b
) & 1;
70 static uint32_t jal(unsigned int rd
, uint32_t imm
) {
71 return (bit(imm
, 20) << 31) |
72 (bits(imm
, 10, 1) << 21) |
73 (bit(imm
, 11) << 20) |
74 (bits(imm
, 19, 12) << 12) |
79 static uint32_t csrsi(unsigned int csr
, uint16_t imm
) {
81 (bits(imm
, 4, 0) << 15) |
85 static uint32_t csrci(unsigned int csr
, uint16_t imm
) {
87 (bits(imm
, 4, 0) << 15) |
91 static uint32_t csrr(unsigned int rd
, unsigned int csr
) {
92 return (csr
<< 20) | (rd
<< 7) | MATCH_CSRRS
;
95 static uint32_t csrw(unsigned int source
, unsigned int csr
) {
96 return (csr
<< 20) | (source
<< 15) | MATCH_CSRRW
;
99 static uint32_t fence_i()
101 return MATCH_FENCE_I
;
104 static uint32_t sb(unsigned int src
, unsigned int base
, uint16_t offset
)
106 return (bits(offset
, 11, 5) << 25) |
109 (bits(offset
, 4, 0) << 7) |
113 static uint32_t sh(unsigned int src
, unsigned int base
, uint16_t offset
)
115 return (bits(offset
, 11, 5) << 25) |
118 (bits(offset
, 4, 0) << 7) |
122 static uint32_t sw(unsigned int src
, unsigned int base
, uint16_t offset
)
124 return (bits(offset
, 11, 5) << 25) |
127 (bits(offset
, 4, 0) << 7) |
131 static uint32_t sd(unsigned int src
, unsigned int base
, uint16_t offset
)
133 return (bits(offset
, 11, 5) << 25) |
134 (bits(src
, 4, 0) << 20) |
136 (bits(offset
, 4, 0) << 7) |
140 static uint32_t sq(unsigned int src
, unsigned int base
, uint16_t offset
)
143 return (bits(offset
, 11, 5) << 25) |
144 (bits(src
, 4, 0) << 20) |
146 (bits(offset
, 4, 0) << 7) |
153 static uint32_t lq(unsigned int rd
, unsigned int base
, uint16_t offset
)
156 return (bits(offset
, 11, 0) << 20) |
158 (bits(rd
, 4, 0) << 7) |
165 static uint32_t ld(unsigned int rd
, unsigned int base
, uint16_t offset
)
167 return (bits(offset
, 11, 0) << 20) |
169 (bits(rd
, 4, 0) << 7) |
173 static uint32_t lw(unsigned int rd
, unsigned int base
, uint16_t offset
)
175 return (bits(offset
, 11, 0) << 20) |
177 (bits(rd
, 4, 0) << 7) |
181 static uint32_t lh(unsigned int rd
, unsigned int base
, uint16_t offset
)
183 return (bits(offset
, 11, 0) << 20) |
185 (bits(rd
, 4, 0) << 7) |
189 static uint32_t lb(unsigned int rd
, unsigned int base
, uint16_t offset
)
191 return (bits(offset
, 11, 0) << 20) |
193 (bits(rd
, 4, 0) << 7) |
197 static uint32_t fsw(unsigned int src
, unsigned int base
, uint16_t offset
)
199 return (bits(offset
, 11, 5) << 25) |
200 (bits(src
, 4, 0) << 20) |
202 (bits(offset
, 4, 0) << 7) |
206 static uint32_t fsd(unsigned int src
, unsigned int base
, uint16_t offset
)
208 return (bits(offset
, 11, 5) << 25) |
209 (bits(src
, 4, 0) << 20) |
211 (bits(offset
, 4, 0) << 7) |
215 static uint32_t flw(unsigned int src
, unsigned int base
, uint16_t offset
)
217 return (bits(offset
, 11, 5) << 25) |
218 (bits(src
, 4, 0) << 20) |
220 (bits(offset
, 4, 0) << 7) |
224 static uint32_t fld(unsigned int src
, unsigned int base
, uint16_t offset
)
226 return (bits(offset
, 11, 5) << 25) |
227 (bits(src
, 4, 0) << 20) |
229 (bits(offset
, 4, 0) << 7) |
233 static uint32_t addi(unsigned int dest
, unsigned int src
, uint16_t imm
)
235 return (bits(imm
, 11, 0) << 20) |
241 static uint32_t ori(unsigned int dest
, unsigned int src
, uint16_t imm
)
243 return (bits(imm
, 11, 0) << 20) |
249 static uint32_t xori(unsigned int dest
, unsigned int src
, uint16_t imm
)
251 return (bits(imm
, 11, 0) << 20) |
257 static uint32_t srli(unsigned int dest
, unsigned int src
, uint8_t shamt
)
259 return (bits(shamt
, 4, 0) << 20) |
266 static uint32_t nop()
268 return addi(0, 0, 0);
271 template <typename T
>
272 unsigned int circular_buffer_t
<T
>::size() const
277 return end
+ capacity
- start
;
280 template <typename T
>
281 void circular_buffer_t
<T
>::consume(unsigned int bytes
)
283 start
= (start
+ bytes
) % capacity
;
286 template <typename T
>
287 unsigned int circular_buffer_t
<T
>::contiguous_empty_size() const
291 return capacity
- end
- 1;
293 return capacity
- end
;
295 return start
- end
- 1;
298 template <typename T
>
299 unsigned int circular_buffer_t
<T
>::contiguous_data_size() const
304 return capacity
- start
;
307 template <typename T
>
308 void circular_buffer_t
<T
>::data_added(unsigned int bytes
)
311 assert(end
<= capacity
);
316 template <typename T
>
317 void circular_buffer_t
<T
>::reset()
323 template <typename T
>
324 void circular_buffer_t
<T
>::append(const T
*src
, unsigned int count
)
326 unsigned int copy
= std::min(count
, contiguous_empty_size());
327 memcpy(contiguous_empty(), src
, copy
* sizeof(T
));
331 assert(count
< contiguous_empty_size());
332 memcpy(contiguous_empty(), src
, count
* sizeof(T
));
337 ////////////////////////////// Debug Operations
339 class halt_op_t
: public operation_t
342 halt_op_t(gdbserver_t
& gdbserver
, bool send_status
=false) :
343 operation_t(gdbserver
), send_status(send_status
),
346 void write_dpc_program() {
347 gs
.dr_write32(0, csrsi(CSR_DCSR
, DCSR_HALT
));
348 gs
.dr_write32(1, csrr(S0
, CSR_DPC
));
349 gs
.dr_write_store(2, S0
, SLOT_DATA0
);
354 bool perform_step(unsigned int step
) {
358 gs
.dr_write32(0, xori(S1
, ZERO
, -1));
359 gs
.dr_write32(1, srli(S1
, S1
, 31));
360 // 0x00000001 0x00000001:ffffffff 0x00000001:ffffffff:ffffffff:ffffffff
361 gs
.dr_write32(2, sw(S1
, ZERO
, DEBUG_RAM_START
));
362 gs
.dr_write32(3, srli(S1
, S1
, 31));
363 // 0x00000000 0x00000000:00000003 0x00000000:00000003:ffffffff:ffffffff
364 gs
.dr_write32(4, sw(S1
, ZERO
, DEBUG_RAM_START
+ 4));
377 uint32_t word0
= gs
.dr_read32(0);
378 uint32_t word1
= gs
.dr_read32(1);
380 if (word0
== 1 && word1
== 0) {
382 } else if (word0
== 0xffffffff && word1
== 3) {
384 } else if (word0
== 0xffffffff && word1
== 0xffffffff) {
394 gs
.dpc
= gs
.dr_read(SLOT_DATA0
);
395 fprintf(stderr
, "dpc=0x%lx\n", gs
.dpc
);
396 gs
.dr_write32(0, csrr(S0
, CSR_MSTATUS
));
397 gs
.dr_write_store(1, S0
, SLOT_DATA0
);
404 gs
.mstatus
= gs
.dr_read(SLOT_DATA0
);
405 gs
.dr_write32(0, csrr(S0
, CSR_DCSR
));
406 gs
.dr_write32(1, sw(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
413 gs
.dcsr
= gs
.dr_read32(4);
415 gs
.sptbr_valid
= false;
416 gs
.pte_cache
.clear();
419 switch (get_field(gs
.dcsr
, DCSR_CAUSE
)) {
420 case DCSR_CAUSE_NONE
:
421 fprintf(stderr
, "Internal error. Processor halted without reason.\n");
424 case DCSR_CAUSE_DEBUGINT
:
425 gs
.send_packet("S02"); // Pretend program received SIGINT.
428 case DCSR_CAUSE_HWBP
:
429 case DCSR_CAUSE_STEP
:
430 case DCSR_CAUSE_HALT
:
431 // There's no gdb code for this.
432 gs
.send_packet("T05");
434 case DCSR_CAUSE_SWBP
:
435 gs
.send_packet("T05swbreak:;");
457 class continue_op_t
: public operation_t
460 continue_op_t(gdbserver_t
& gdbserver
, bool single_step
) :
461 operation_t(gdbserver
), single_step(single_step
) {};
463 bool perform_step(unsigned int step
) {
466 gs
.dr_write_load(0, S0
, SLOT_DATA0
);
467 gs
.dr_write32(1, csrw(S0
, CSR_DPC
));
468 // TODO: Isn't there a fence.i in Debug ROM already?
469 if (gs
.fence_i_required
) {
470 gs
.dr_write32(2, fence_i());
472 gs
.fence_i_required
= false;
476 gs
.dr_write(SLOT_DATA0
, gs
.dpc
);
481 gs
.dr_write_load(0, S0
, SLOT_DATA0
);
482 gs
.dr_write32(1, csrw(S0
, CSR_MSTATUS
));
484 gs
.dr_write(SLOT_DATA0
, gs
.mstatus
);
489 gs
.dr_write32(0, lw(S0
, 0, (uint16_t) DEBUG_RAM_START
+16));
490 gs
.dr_write32(1, csrw(S0
, CSR_DCSR
));
493 reg_t dcsr
= set_field(gs
.dcsr
, DCSR_HALT
, 0);
494 dcsr
= set_field(dcsr
, DCSR_STEP
, single_step
);
495 // Software breakpoints should go here.
496 dcsr
= set_field(dcsr
, DCSR_EBREAKM
, 1);
497 dcsr
= set_field(dcsr
, DCSR_EBREAKH
, 1);
498 dcsr
= set_field(dcsr
, DCSR_EBREAKS
, 1);
499 dcsr
= set_field(dcsr
, DCSR_EBREAKU
, 1);
500 gs
.dr_write32(4, dcsr
);
512 class general_registers_read_op_t
: public operation_t
514 // Register order that gdb expects is:
515 // "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
516 // "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
517 // "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
518 // "x24", "x25", "x26", "x27", "x28", "x29", "x30", "x31",
520 // Each byte of register data is described by two hex digits. The bytes with
521 // the register are transmitted in target byte order. The size of each
522 // register and their position within the ‘g’ packet are determined by the
523 // gdb internal gdbarch functions DEPRECATED_REGISTER_RAW_SIZE and
524 // gdbarch_register_name.
527 general_registers_read_op_t(gdbserver_t
& gdbserver
) :
528 operation_t(gdbserver
) {};
530 bool perform_step(unsigned int step
)
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
));
574 gs
.dr_write_store(i
++, current_reg
+1, SLOT_DATA1
);
582 class register_read_op_t
: public operation_t
585 register_read_op_t(gdbserver_t
& gdbserver
, unsigned int reg
) :
586 operation_t(gdbserver
), reg(reg
) {};
588 bool perform_step(unsigned int step
)
592 if (reg
>= REG_XPR0
&& reg
<= REG_XPR31
) {
593 die("handle_register_read");
594 // send(p->state.XPR[reg - REG_XPR0]);
595 } else if (reg
== REG_PC
) {
598 gs
.send((uint32_t) gs
.dpc
);
604 } else if (reg
>= REG_FPR0
&& reg
<= REG_FPR31
) {
605 // send(p->state.FPR[reg - REG_FPR0]);
607 gs
.dr_write32(0, fsw(reg
- REG_FPR0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
609 gs
.dr_write32(0, fsd(reg
- REG_FPR0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
612 } else if (reg
>= REG_CSR0
&& reg
<= REG_CSR4095
) {
613 gs
.dr_write32(0, csrr(S0
, reg
- REG_CSR0
));
614 gs
.dr_write_store(1, S0
, SLOT_DATA0
);
616 // If we hit an exception reading the CSR, we'll end up returning ~0 as
617 // the register's value, which is what we want. (Right?)
618 gs
.dr_write(SLOT_DATA0
, ~(uint64_t) 0);
620 gs
.send_packet("E02");
629 gs
.send(gs
.dr_read32(4));
631 gs
.send(gs
.dr_read(SLOT_DATA0
));
643 class register_write_op_t
: public operation_t
646 register_write_op_t(gdbserver_t
& gdbserver
, unsigned int reg
, reg_t value
) :
647 operation_t(gdbserver
), reg(reg
), value(value
) {};
649 bool perform_step(unsigned int step
)
651 gs
.dr_write_load(0, S0
, SLOT_DATA0
);
652 gs
.dr_write(SLOT_DATA0
, value
);
654 gs
.dr_write32(1, csrw(S0
, CSR_DSCRATCH
));
656 } else if (reg
== S1
) {
657 gs
.dr_write_store(1, S0
, SLOT_DATA_LAST
);
659 } else if (reg
>= REG_XPR0
&& reg
<= REG_XPR31
) {
660 gs
.dr_write32(1, addi(reg
, S0
, 0));
662 } else if (reg
== REG_PC
) {
665 } else if (reg
>= REG_FPR0
&& reg
<= REG_FPR31
) {
667 gs
.dr_write32(0, flw(reg
- REG_FPR0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
669 gs
.dr_write32(0, fld(reg
- REG_FPR0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
672 } else if (reg
>= REG_CSR0
&& reg
<= REG_CSR4095
) {
673 gs
.dr_write32(1, csrw(S0
, reg
- REG_CSR0
));
675 if (reg
== REG_CSR0
+ CSR_SPTBR
) {
677 gs
.sptbr_valid
= true;
680 gs
.send_packet("E02");
684 gs
.send_packet("OK");
693 class memory_read_op_t
: public operation_t
696 // Read length bytes from vaddr, storing the result into data.
697 // If data is NULL, send the result straight to gdb.
698 memory_read_op_t(gdbserver_t
& gdbserver
, reg_t vaddr
, unsigned int length
,
699 unsigned char *data
=NULL
) :
700 operation_t(gdbserver
), vaddr(vaddr
), length(length
), data(data
) {};
702 bool perform_step(unsigned int step
)
705 // address goes in S0
706 paddr
= gs
.translate(vaddr
);
707 access_size
= gs
.find_access_size(paddr
, length
);
709 gs
.dr_write_load(0, S0
, SLOT_DATA0
);
710 switch (access_size
) {
712 gs
.dr_write32(1, lb(S1
, S0
, 0));
715 gs
.dr_write32(1, lh(S1
, S0
, 0));
718 gs
.dr_write32(1, lw(S1
, S0
, 0));
721 gs
.dr_write32(1, ld(S1
, S0
, 0));
724 gs
.dr_write_store(2, S1
, SLOT_DATA1
);
726 gs
.dr_write(SLOT_DATA0
, paddr
);
736 reg_t value
= gs
.dr_read(SLOT_DATA1
);
737 for (unsigned int i
= 0; i
< access_size
; i
++) {
739 *(data
++) = value
& 0xff;
740 D(fprintf(stderr
, "%02x", (unsigned int) (value
& 0xff)));
742 sprintf(buffer
, "%02x", (unsigned int) (value
& 0xff));
747 if (data
&& debug_gdbserver
) {
748 D(fprintf(stderr
, "\n"));
750 length
-= access_size
;
751 paddr
+= access_size
;
759 gs
.dr_write(SLOT_DATA0
, paddr
);
770 unsigned int access_size
;
773 class memory_write_op_t
: public operation_t
776 memory_write_op_t(gdbserver_t
& gdbserver
, reg_t vaddr
, unsigned int length
,
777 const unsigned char *data
) :
778 operation_t(gdbserver
), vaddr(vaddr
), offset(0), length(length
), data(data
) {};
780 ~memory_write_op_t() {
784 bool perform_step(unsigned int step
)
786 reg_t paddr
= gs
.translate(vaddr
);
788 access_size
= gs
.find_access_size(paddr
, length
);
790 D(fprintf(stderr
, "write to 0x%lx -> 0x%lx (access=%d): ", vaddr
, paddr
,
792 for (unsigned int i
= 0; i
< length
; i
++) {
793 D(fprintf(stderr
, "%02x", data
[i
]));
795 D(fprintf(stderr
, "\n"));
797 // address goes in S0
798 gs
.dr_write_load(0, S0
, SLOT_DATA0
);
799 switch (access_size
) {
801 gs
.dr_write32(1, lb(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 24));
802 gs
.dr_write32(2, sb(S1
, S0
, 0));
803 gs
.dr_write32(6, data
[0]);
806 gs
.dr_write32(1, lh(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 24));
807 gs
.dr_write32(2, sh(S1
, S0
, 0));
808 gs
.dr_write32(6, data
[0] | (data
[1] << 8));
811 gs
.dr_write32(1, lw(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 24));
812 gs
.dr_write32(2, sw(S1
, S0
, 0));
813 gs
.dr_write32(6, data
[0] | (data
[1] << 8) |
814 (data
[2] << 16) | (data
[3] << 24));
817 gs
.dr_write32(1, ld(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 24));
818 gs
.dr_write32(2, sd(S1
, S0
, 0));
819 gs
.dr_write32(6, data
[0] | (data
[1] << 8) |
820 (data
[2] << 16) | (data
[3] << 24));
821 gs
.dr_write32(7, data
[4] | (data
[5] << 8) |
822 (data
[6] << 16) | (data
[7] << 24));
825 fprintf(stderr
, "gdbserver error: write %d bytes to 0x%lx -> 0x%lx; "
826 "access_size=%d\n", length
, vaddr
, paddr
, access_size
);
827 gs
.send_packet("E12");
831 gs
.dr_write(SLOT_DATA0
, paddr
);
837 if (gs
.dr_read32(DEBUG_RAM_SIZE
/ 4 - 1)) {
838 fprintf(stderr
, "Exception happened while writing to 0x%lx -> 0x%lx\n",
842 offset
+= access_size
;
843 if (offset
>= length
) {
844 gs
.send_packet("OK");
847 const unsigned char *d
= data
+ offset
;
848 switch (access_size
) {
850 gs
.dr_write32(6, d
[0]);
853 gs
.dr_write32(6, d
[0] | (d
[1] << 8));
856 gs
.dr_write32(6, d
[0] | (d
[1] << 8) |
857 (d
[2] << 16) | (d
[3] << 24));
860 gs
.dr_write32(6, d
[0] | (d
[1] << 8) |
861 (d
[2] << 16) | (d
[3] << 24));
862 gs
.dr_write32(7, d
[4] | (d
[5] << 8) |
863 (d
[6] << 16) | (d
[7] << 24));
866 gs
.send_packet("E13");
869 gs
.dr_write(SLOT_DATA0
, paddr
+ offset
);
879 unsigned int access_size
;
880 const unsigned char *data
;
883 class collect_translation_info_op_t
: public operation_t
886 // Read sufficient information from the target into gdbserver structures so
887 // that it's possible to translate vaddr, vaddr+length, and all addresses
888 // in between to physical addresses.
889 collect_translation_info_op_t(gdbserver_t
& gdbserver
, reg_t vaddr
, size_t length
) :
890 operation_t(gdbserver
), state(STATE_START
), vaddr(vaddr
), length(length
) {};
892 bool perform_step(unsigned int step
)
894 unsigned int vm
= gs
.virtual_memory();
899 // Nothing to be done.
921 sprintf(buf
, "VM mode %d is not supported by gdbserver.cc.", vm
);
923 return true; // die doesn't return, but gcc doesn't know that.
928 // Perform any reads from the just-completed action.
932 case STATE_READ_SPTBR
:
933 gs
.sptbr
= ((uint64_t) gs
.dr_read32(5) << 32) | gs
.dr_read32(4);
934 gs
.sptbr_valid
= true;
937 gs
.pte_cache
[pte_addr
] = ((uint64_t) gs
.dr_read32(5) << 32) |
939 D(fprintf(stderr
, "pte_cache[0x%lx] = 0x%lx\n", pte_addr
, gs
.pte_cache
[pte_addr
]));
943 // Set up the next action.
944 // We only get here for VM_SV32/39/38.
946 if (!gs
.sptbr_valid
) {
947 state
= STATE_READ_SPTBR
;
948 gs
.dr_write32(0, csrr(S0
, CSR_SPTBR
));
949 gs
.dr_write32(1, sd(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
950 gs
.dr_write32(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
955 reg_t base
= gs
.sptbr
<< PGSHIFT
;
956 int ptshift
= (levels
- 1) * ptidxbits
;
957 for (unsigned int i
= 0; i
< levels
; i
++, ptshift
-= ptidxbits
) {
958 reg_t idx
= (vaddr
>> (PGSHIFT
+ ptshift
)) & ((1 << ptidxbits
) - 1);
960 pte_addr
= base
+ idx
* ptesize
;
961 auto it
= gs
.pte_cache
.find(pte_addr
);
962 if (it
== gs
.pte_cache
.end()) {
963 state
= STATE_READ_PTE
;
965 gs
.dr_write32(0, lw(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
966 gs
.dr_write32(1, lw(S1
, S0
, 0));
967 gs
.dr_write32(2, sw(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
969 assert(gs
.xlen
>= 64);
970 gs
.dr_write32(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
971 gs
.dr_write32(1, ld(S1
, S0
, 0));
972 gs
.dr_write32(2, sd(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
974 gs
.dr_write32(3, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*3))));
975 gs
.dr_write32(4, pte_addr
);
976 gs
.dr_write32(5, pte_addr
>> 32);
981 reg_t pte
= gs
.pte_cache
[pte_addr
];
982 reg_t ppn
= pte
>> PTE_PPN_SHIFT
;
984 if (PTE_TABLE(pte
)) { // next level of page table
985 base
= ppn
<< PGSHIFT
;
987 // We've collected all the data required for the translation.
992 "ERROR: gdbserver couldn't find appropriate PTEs to translate 0x%lx\n",
1005 unsigned int levels
;
1006 unsigned int ptidxbits
;
1007 unsigned int ptesize
;
1011 ////////////////////////////// gdbserver itself
1013 gdbserver_t::gdbserver_t(uint16_t port
, sim_t
*sim
) :
1017 recv_buf(64 * 1024), send_buf(64 * 1024)
1019 socket_fd
= socket(AF_INET
, SOCK_STREAM
, 0);
1020 if (socket_fd
== -1) {
1021 fprintf(stderr
, "failed to make socket: %s (%d)\n", strerror(errno
), errno
);
1025 fcntl(socket_fd
, F_SETFL
, O_NONBLOCK
);
1027 if (setsockopt(socket_fd
, SOL_SOCKET
, SO_REUSEADDR
, &reuseaddr
,
1028 sizeof(int)) == -1) {
1029 fprintf(stderr
, "failed setsockopt: %s (%d)\n", strerror(errno
), errno
);
1033 struct sockaddr_in addr
;
1034 memset(&addr
, 0, sizeof(addr
));
1035 addr
.sin_family
= AF_INET
;
1036 addr
.sin_addr
.s_addr
= INADDR_ANY
;
1037 addr
.sin_port
= htons(port
);
1039 if (bind(socket_fd
, (struct sockaddr
*) &addr
, sizeof(addr
)) == -1) {
1040 fprintf(stderr
, "failed to bind socket: %s (%d)\n", strerror(errno
), errno
);
1044 if (listen(socket_fd
, 1) == -1) {
1045 fprintf(stderr
, "failed to listen on socket: %s (%d)\n", strerror(errno
), errno
);
1050 unsigned int gdbserver_t::find_access_size(reg_t address
, int length
)
1052 reg_t composite
= address
| length
;
1053 if ((composite
& 0x7) == 0 && xlen
>= 64)
1055 if ((composite
& 0x3) == 0)
1060 reg_t
gdbserver_t::translate(reg_t vaddr
)
1062 unsigned int vm
= virtual_memory();
1063 unsigned int levels
, ptidxbits
, ptesize
;
1088 sprintf(buf
, "VM mode %d is not supported by gdbserver.cc.", vm
);
1090 return true; // die doesn't return, but gcc doesn't know that.
1094 // Handle page tables here. There's a bunch of duplicated code with
1095 // collect_translation_info_op_t. :-(
1096 reg_t base
= sptbr
<< PGSHIFT
;
1097 int ptshift
= (levels
- 1) * ptidxbits
;
1098 for (unsigned int i
= 0; i
< levels
; i
++, ptshift
-= ptidxbits
) {
1099 reg_t idx
= (vaddr
>> (PGSHIFT
+ ptshift
)) & ((1 << ptidxbits
) - 1);
1101 reg_t pte_addr
= base
+ idx
* ptesize
;
1102 auto it
= pte_cache
.find(pte_addr
);
1103 if (it
== pte_cache
.end()) {
1104 fprintf(stderr
, "ERROR: gdbserver tried to translate 0x%lx without first "
1105 "collecting the relevant PTEs.\n", vaddr
);
1106 die("gdbserver_t::translate()");
1109 reg_t pte
= pte_cache
[pte_addr
];
1110 reg_t ppn
= pte
>> PTE_PPN_SHIFT
;
1112 if (PTE_TABLE(pte
)) { // next level of page table
1113 base
= ppn
<< PGSHIFT
;
1115 // We've collected all the data required for the translation.
1116 reg_t vpn
= vaddr
>> PGSHIFT
;
1117 reg_t paddr
= (ppn
| (vpn
& ((reg_t(1) << ptshift
) - 1))) << PGSHIFT
;
1118 paddr
+= vaddr
& (PGSIZE
-1);
1119 D(fprintf(stderr
, "gdbserver translate 0x%lx -> 0x%lx\n", vaddr
, paddr
));
1124 fprintf(stderr
, "ERROR: gdbserver tried to translate 0x%lx but the relevant "
1125 "PTEs are invalid.\n", vaddr
);
1126 // TODO: Is it better to throw an exception here?
1130 unsigned int gdbserver_t::privilege_mode()
1132 unsigned int mode
= get_field(dcsr
, DCSR_PRV
);
1133 if (get_field(mstatus
, MSTATUS_MPRV
))
1134 mode
= get_field(mstatus
, MSTATUS_MPP
);
1138 unsigned int gdbserver_t::virtual_memory()
1140 unsigned int mode
= privilege_mode();
1143 return get_field(mstatus
, MSTATUS_VM
);
1146 void gdbserver_t::dr_write32(unsigned int index
, uint32_t value
)
1148 sim
->debug_module
.ram_write32(index
, value
);
1151 void gdbserver_t::dr_write64(unsigned int index
, uint64_t value
)
1153 dr_write32(index
, value
);
1154 dr_write32(index
+1, value
>> 32);
1157 void gdbserver_t::dr_write(enum slot slot
, uint64_t value
)
1161 dr_write32(slot_offset32
[slot
], value
);
1164 dr_write64(slot_offset64
[slot
], value
);
1172 void gdbserver_t::dr_write_jump(unsigned int index
)
1174 dr_write32(index
, jal(0,
1175 (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*index
))));
1178 void gdbserver_t::dr_write_store(unsigned int index
, unsigned int reg
, enum slot slot
)
1180 assert(slot
!= SLOT_INST0
|| index
> 2);
1181 assert(slot
!= SLOT_DATA0
|| index
< 4 || index
> 6);
1182 assert(slot
!= SLOT_DATA1
|| index
< 5 || index
> 10);
1183 assert(slot
!= SLOT_DATA_LAST
|| index
< 6 || index
> 14);
1186 return dr_write32(index
,
1187 sw(reg
, 0, (uint16_t) DEBUG_RAM_START
+ 4 * slot_offset32
[slot
]));
1189 return dr_write32(index
,
1190 sd(reg
, 0, (uint16_t) DEBUG_RAM_START
+ 4 * slot_offset64
[slot
]));
1192 return dr_write32(index
,
1193 sq(reg
, 0, (uint16_t) DEBUG_RAM_START
+ 4 * slot_offset128
[slot
]));
1195 fprintf(stderr
, "xlen is %d!\n", xlen
);
1200 void gdbserver_t::dr_write_load(unsigned int index
, unsigned int reg
, enum slot slot
)
1204 return dr_write32(index
,
1205 lw(reg
, 0, (uint16_t) DEBUG_RAM_START
+ 4 * slot_offset32
[slot
]));
1207 return dr_write32(index
,
1208 ld(reg
, 0, (uint16_t) DEBUG_RAM_START
+ 4 * slot_offset64
[slot
]));
1210 return dr_write32(index
,
1211 lq(reg
, 0, (uint16_t) DEBUG_RAM_START
+ 4 * slot_offset128
[slot
]));
1213 fprintf(stderr
, "xlen is %d!\n", xlen
);
1218 uint32_t gdbserver_t::dr_read32(unsigned int index
)
1220 uint32_t value
= sim
->debug_module
.ram_read32(index
);
1221 D(fprintf(stderr
, "read32(%d) -> 0x%x\n", index
, value
));
1225 uint64_t gdbserver_t::dr_read64(unsigned int index
)
1227 return ((uint64_t) dr_read32(index
+1) << 32) | dr_read32(index
);
1230 uint64_t gdbserver_t::dr_read(enum slot slot
)
1234 return dr_read32(slot_offset32
[slot
]);
1236 return dr_read64(slot_offset64
[slot
]);
1244 void gdbserver_t::add_operation(operation_t
* operation
)
1246 operation_queue
.push(operation
);
1249 void gdbserver_t::accept()
1251 client_fd
= ::accept(socket_fd
, NULL
, NULL
);
1252 if (client_fd
== -1) {
1253 if (errno
== EAGAIN
) {
1254 // No client waiting to connect right now.
1256 fprintf(stderr
, "failed to accept on socket: %s (%d)\n", strerror(errno
),
1261 fcntl(client_fd
, F_SETFL
, O_NONBLOCK
);
1264 extended_mode
= false;
1266 // gdb wants the core to be halted when it attaches.
1267 add_operation(new halt_op_t(*this));
1271 void gdbserver_t::read()
1273 // Reading from a non-blocking socket still blocks if there is no data
1276 size_t count
= recv_buf
.contiguous_empty_size();
1278 ssize_t bytes
= ::read(client_fd
, recv_buf
.contiguous_empty(), count
);
1280 if (errno
== EAGAIN
) {
1281 // We'll try again the next call.
1283 fprintf(stderr
, "failed to read on socket: %s (%d)\n", strerror(errno
), errno
);
1286 } else if (bytes
== 0) {
1287 // The remote disconnected.
1289 processor_t
*p
= sim
->get_core(0);
1290 // TODO p->set_halted(false, HR_NONE);
1294 recv_buf
.data_added(bytes
);
1298 void gdbserver_t::write()
1300 if (send_buf
.empty())
1303 while (!send_buf
.empty()) {
1304 unsigned int count
= send_buf
.contiguous_data_size();
1306 ssize_t bytes
= ::write(client_fd
, send_buf
.contiguous_data(), count
);
1308 fprintf(stderr
, "failed to write to socket: %s (%d)\n", strerror(errno
), errno
);
1310 } else if (bytes
== 0) {
1311 // Client can't take any more data right now.
1314 D(fprintf(stderr
, "wrote %ld bytes: ", bytes
));
1315 for (unsigned int i
= 0; i
< bytes
; i
++) {
1316 D(fprintf(stderr
, "%c", send_buf
[i
]));
1318 D(fprintf(stderr
, "\n"));
1319 send_buf
.consume(bytes
);
1324 void print_packet(const std::vector
<uint8_t> &packet
)
1326 for (uint8_t c
: packet
) {
1327 if (c
>= ' ' and c
<= '~')
1328 fprintf(stderr
, "%c", c
);
1330 fprintf(stderr
, "\\x%02x", c
);
1332 fprintf(stderr
, "\n");
1335 uint8_t compute_checksum(const std::vector
<uint8_t> &packet
)
1337 uint8_t checksum
= 0;
1338 for (auto i
= packet
.begin() + 1; i
!= packet
.end() - 3; i
++ ) {
1344 uint8_t character_hex_value(uint8_t character
)
1346 if (character
>= '0' && character
<= '9')
1347 return character
- '0';
1348 if (character
>= 'a' && character
<= 'f')
1349 return 10 + character
- 'a';
1350 if (character
>= 'A' && character
<= 'F')
1351 return 10 + character
- 'A';
1355 uint8_t extract_checksum(const std::vector
<uint8_t> &packet
)
1357 return character_hex_value(*(packet
.end() - 1)) +
1358 16 * character_hex_value(*(packet
.end() - 2));
1361 void gdbserver_t::process_requests()
1363 // See https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html
1365 while (!recv_buf
.empty()) {
1366 std::vector
<uint8_t> packet
;
1367 for (unsigned int i
= 0; i
< recv_buf
.size(); i
++) {
1368 uint8_t b
= recv_buf
[i
];
1370 if (packet
.empty() && expect_ack
&& b
== '+') {
1371 recv_buf
.consume(1);
1375 if (packet
.empty() && b
== 3) {
1376 D(fprintf(stderr
, "Received interrupt\n"));
1377 recv_buf
.consume(1);
1383 // Start of new packet.
1384 if (!packet
.empty()) {
1385 fprintf(stderr
, "Received malformed %ld-byte packet from debug client: ",
1387 print_packet(packet
);
1388 recv_buf
.consume(i
);
1393 packet
.push_back(b
);
1395 // Packets consist of $<packet-data>#<checksum>
1396 // where <checksum> is
1397 if (packet
.size() >= 4 &&
1398 packet
[packet
.size()-3] == '#') {
1399 handle_packet(packet
);
1400 recv_buf
.consume(i
+1);
1404 // There's a partial packet in the buffer. Wait until we get more data to
1406 if (packet
.size()) {
1412 void gdbserver_t::handle_halt_reason(const std::vector
<uint8_t> &packet
)
1417 void gdbserver_t::handle_general_registers_read(const std::vector
<uint8_t> &packet
)
1419 add_operation(new general_registers_read_op_t(*this));
1422 void gdbserver_t::set_interrupt(uint32_t hartid
) {
1423 sim
->debug_module
.set_interrupt(hartid
);
1426 // First byte is the most-significant one.
1427 // Eg. "08675309" becomes 0x08675309.
1428 uint64_t consume_hex_number(std::vector
<uint8_t>::const_iterator
&iter
,
1429 std::vector
<uint8_t>::const_iterator end
)
1433 while (iter
!= end
) {
1435 uint64_t c_value
= character_hex_value(c
);
1445 // First byte is the least-significant one.
1446 // Eg. "08675309" becomes 0x09536708
1447 uint64_t consume_hex_number_le(std::vector
<uint8_t>::const_iterator
&iter
,
1448 std::vector
<uint8_t>::const_iterator end
)
1451 unsigned int shift
= 4;
1453 while (iter
!= end
) {
1455 uint64_t c_value
= character_hex_value(c
);
1459 value
|= c_value
<< shift
;
1460 if ((shift
% 8) == 0)
1468 void consume_string(std::string
&str
, std::vector
<uint8_t>::const_iterator
&iter
,
1469 std::vector
<uint8_t>::const_iterator end
, uint8_t separator
)
1471 while (iter
!= end
&& *iter
!= separator
) {
1472 str
.append(1, (char) *iter
);
1477 void gdbserver_t::handle_register_read(const std::vector
<uint8_t> &packet
)
1481 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1482 unsigned int n
= consume_hex_number(iter
, packet
.end());
1484 return send_packet("E01");
1486 add_operation(new register_read_op_t(*this, n
));
1489 void gdbserver_t::handle_register_write(const std::vector
<uint8_t> &packet
)
1493 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1494 unsigned int n
= consume_hex_number(iter
, packet
.end());
1496 return send_packet("E05");
1499 reg_t value
= consume_hex_number_le(iter
, packet
.end());
1501 return send_packet("E06");
1503 processor_t
*p
= sim
->get_core(0);
1505 add_operation(new register_write_op_t(*this, n
, value
));
1507 return send_packet("OK");
1510 void gdbserver_t::handle_memory_read(const std::vector
<uint8_t> &packet
)
1513 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1514 reg_t address
= consume_hex_number(iter
, packet
.end());
1516 return send_packet("E10");
1518 reg_t length
= consume_hex_number(iter
, packet
.end());
1520 return send_packet("E11");
1522 add_operation(new collect_translation_info_op_t(*this, address
, length
));
1523 add_operation(new memory_read_op_t(*this, address
, length
));
1526 void gdbserver_t::handle_memory_binary_write(const std::vector
<uint8_t> &packet
)
1528 // X addr,length:XX...
1529 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1530 reg_t address
= consume_hex_number(iter
, packet
.end());
1532 return send_packet("E20");
1534 reg_t length
= consume_hex_number(iter
, packet
.end());
1536 return send_packet("E21");
1540 return send_packet("OK");
1543 unsigned char *data
= new unsigned char[length
];
1544 for (unsigned int i
= 0; i
< length
; i
++) {
1545 if (iter
== packet
.end()) {
1546 return send_packet("E22");
1551 // The binary data representation uses 7d (ascii ‘}’) as an escape
1552 // character. Any escaped byte is transmitted as the escape character
1553 // followed by the original character XORed with 0x20. For example, the
1554 // byte 0x7d would be transmitted as the two bytes 0x7d 0x5d. The bytes
1555 // 0x23 (ascii ‘#’), 0x24 (ascii ‘$’), and 0x7d (ascii ‘}’) must always
1557 if (iter
== packet
.end()) {
1558 return send_packet("E23");
1566 return send_packet("E4b"); // EOVERFLOW
1568 add_operation(new collect_translation_info_op_t(*this, address
, length
));
1569 add_operation(new memory_write_op_t(*this, address
, length
, data
));
1572 void gdbserver_t::handle_continue(const std::vector
<uint8_t> &packet
)
1575 processor_t
*p
= sim
->get_core(0);
1576 if (packet
[2] != '#') {
1577 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1578 dpc
= consume_hex_number(iter
, packet
.end());
1580 return send_packet("E30");
1583 add_operation(new continue_op_t(*this, false));
1586 void gdbserver_t::handle_step(const std::vector
<uint8_t> &packet
)
1589 if (packet
[2] != '#') {
1590 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1592 //p->state.pc = consume_hex_number(iter, packet.end());
1594 return send_packet("E40");
1597 add_operation(new continue_op_t(*this, true));
1600 void gdbserver_t::handle_kill(const std::vector
<uint8_t> &packet
)
1603 // The exact effect of this packet is not specified.
1604 // Looks like OpenOCD disconnects?
1608 void gdbserver_t::handle_extended(const std::vector
<uint8_t> &packet
)
1610 // Enable extended mode. In extended mode, the remote server is made
1611 // persistent. The ‘R’ packet is used to restart the program being debugged.
1613 extended_mode
= true;
1616 void gdbserver_t::handle_breakpoint(const std::vector
<uint8_t> &packet
)
1618 // insert: Z type,addr,kind
1619 // remove: z type,addr,kind
1621 software_breakpoint_t bp
;
1622 bool insert
= (packet
[1] == 'Z');
1623 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1624 int type
= consume_hex_number(iter
, packet
.end());
1626 return send_packet("E50");
1628 bp
.address
= consume_hex_number(iter
, packet
.end());
1630 return send_packet("E51");
1632 bp
.size
= consume_hex_number(iter
, packet
.end());
1633 // There may be more options after a ; here, but we don't support that.
1635 return send_packet("E52");
1638 // Only software breakpoints are supported.
1639 return send_packet("");
1642 if (bp
.size
!= 2 && bp
.size
!= 4) {
1643 return send_packet("E53");
1646 fence_i_required
= true;
1647 add_operation(new collect_translation_info_op_t(*this, bp
.address
, bp
.size
));
1649 unsigned char* swbp
= new unsigned char[4];
1651 swbp
[0] = C_EBREAK
& 0xff;
1652 swbp
[1] = (C_EBREAK
>> 8) & 0xff;
1654 swbp
[0] = EBREAK
& 0xff;
1655 swbp
[1] = (EBREAK
>> 8) & 0xff;
1656 swbp
[2] = (EBREAK
>> 16) & 0xff;
1657 swbp
[3] = (EBREAK
>> 24) & 0xff;
1660 breakpoints
[bp
.address
] = new software_breakpoint_t(bp
);
1661 add_operation(new memory_read_op_t(*this, bp
.address
, bp
.size
,
1662 breakpoints
[bp
.address
]->instruction
));
1663 add_operation(new memory_write_op_t(*this, bp
.address
, bp
.size
, swbp
));
1666 software_breakpoint_t
*found_bp
;
1667 found_bp
= breakpoints
[bp
.address
];
1668 unsigned char* instruction
= new unsigned char[4];
1669 memcpy(instruction
, found_bp
->instruction
, 4);
1670 add_operation(new memory_write_op_t(*this, found_bp
->address
,
1671 found_bp
->size
, instruction
));
1672 breakpoints
.erase(bp
.address
);
1676 return send_packet("OK");
1679 void gdbserver_t::handle_query(const std::vector
<uint8_t> &packet
)
1682 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1684 consume_string(name
, iter
, packet
.end(), ':');
1685 if (iter
!= packet
.end())
1687 if (name
== "Supported") {
1689 while (iter
!= packet
.end()) {
1690 std::string feature
;
1691 consume_string(feature
, iter
, packet
.end(), ';');
1692 if (iter
!= packet
.end())
1694 if (feature
== "swbreak+") {
1698 send("PacketSize=131072;");
1699 return end_packet();
1702 D(fprintf(stderr
, "Unsupported query %s\n", name
.c_str()));
1703 return send_packet("");
1706 void gdbserver_t::handle_packet(const std::vector
<uint8_t> &packet
)
1708 if (compute_checksum(packet
) != extract_checksum(packet
)) {
1709 fprintf(stderr
, "Received %ld-byte packet with invalid checksum\n", packet
.size());
1710 fprintf(stderr
, "Computed checksum: %x\n", compute_checksum(packet
));
1711 print_packet(packet
);
1716 D(fprintf(stderr
, "Received %ld-byte packet from debug client: ", packet
.size()));
1717 D(print_packet(packet
));
1720 switch (packet
[1]) {
1722 return handle_extended(packet
);
1724 return handle_halt_reason(packet
);
1726 return handle_general_registers_read(packet
);
1728 // return handle_kill(packet);
1730 return handle_memory_read(packet
);
1732 // return handle_memory_write(packet);
1734 return handle_memory_binary_write(packet
);
1736 return handle_register_read(packet
);
1738 return handle_register_write(packet
);
1740 return handle_continue(packet
);
1742 return handle_step(packet
);
1745 return handle_breakpoint(packet
);
1748 return handle_query(packet
);
1752 D(fprintf(stderr
, "** Unsupported packet: "));
1753 D(print_packet(packet
));
1757 void gdbserver_t::handle_interrupt()
1759 processor_t
*p
= sim
->get_core(0);
1760 add_operation(new halt_op_t(*this, true));
1763 void gdbserver_t::handle()
1765 if (client_fd
> 0) {
1766 processor_t
*p
= sim
->get_core(0);
1768 bool interrupt
= sim
->debug_module
.get_interrupt(0);
1770 if (!interrupt
&& !operation_queue
.empty()) {
1771 operation_t
*operation
= operation_queue
.front();
1772 if (operation
->step()) {
1773 operation_queue
.pop();
1778 bool halt_notification
= sim
->debug_module
.get_halt_notification(0);
1779 if (halt_notification
) {
1780 sim
->debug_module
.clear_halt_notification(0);
1781 add_operation(new halt_op_t(*this, true));
1791 if (operation_queue
.empty()) {
1792 this->process_requests();
1796 void gdbserver_t::send(const char* msg
)
1798 unsigned int length
= strlen(msg
);
1799 for (const char *c
= msg
; *c
; c
++)
1800 running_checksum
+= *c
;
1801 send_buf
.append((const uint8_t *) msg
, length
);
1804 void gdbserver_t::send(uint64_t value
)
1807 for (unsigned int i
= 0; i
< 8; i
++) {
1808 sprintf(buffer
, "%02x", (int) (value
& 0xff));
1814 void gdbserver_t::send(uint32_t value
)
1817 for (unsigned int i
= 0; i
< 4; i
++) {
1818 sprintf(buffer
, "%02x", (int) (value
& 0xff));
1824 void gdbserver_t::send_packet(const char* data
)
1832 void gdbserver_t::start_packet()
1835 running_checksum
= 0;
1838 void gdbserver_t::end_packet(const char* data
)
1844 char checksum_string
[4];
1845 sprintf(checksum_string
, "#%02x", running_checksum
);
1846 send(checksum_string
);