6 #include <sys/socket.h>
17 #include "gdbserver.h"
20 #define C_EBREAK 0x9002
21 #define EBREAK 0x00100073
23 //////////////////////////////////////// Utility Functions
32 void die(const char* msg
)
34 fprintf(stderr
, "gdbserver code died: %s\n", msg
);
38 // gdb's register list is defined in riscv_gdb_reg_names gdb/riscv-tdep.c in
39 // its source tree. We must interpret the numbers the same here.
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 src
, unsigned int base
, uint16_t offset
)
215 return (bits(offset
, 11, 5) << 25) |
216 (bits(src
, 4, 0) << 20) |
218 (bits(offset
, 4, 0) << 7) |
222 static uint32_t fld(unsigned int src
, unsigned int base
, uint16_t offset
)
224 return (bits(offset
, 11, 5) << 25) |
225 (bits(src
, 4, 0) << 20) |
227 (bits(offset
, 4, 0) << 7) |
231 static uint32_t addi(unsigned int dest
, unsigned int src
, uint16_t imm
)
233 return (bits(imm
, 11, 0) << 20) |
239 static uint32_t ori(unsigned int dest
, unsigned int src
, uint16_t imm
)
241 return (bits(imm
, 11, 0) << 20) |
247 static uint32_t xori(unsigned int dest
, unsigned int src
, uint16_t imm
)
249 return (bits(imm
, 11, 0) << 20) |
255 static uint32_t srli(unsigned int dest
, unsigned int src
, uint8_t shamt
)
257 return (bits(shamt
, 4, 0) << 20) |
264 static uint32_t nop()
266 return addi(0, 0, 0);
269 template <typename T
>
270 unsigned int circular_buffer_t
<T
>::size() const
275 return end
+ capacity
- start
;
278 template <typename T
>
279 void circular_buffer_t
<T
>::consume(unsigned int bytes
)
281 start
= (start
+ bytes
) % capacity
;
284 template <typename T
>
285 unsigned int circular_buffer_t
<T
>::contiguous_empty_size() const
289 return capacity
- end
- 1;
291 return capacity
- end
;
293 return start
- end
- 1;
296 template <typename T
>
297 unsigned int circular_buffer_t
<T
>::contiguous_data_size() const
302 return capacity
- start
;
305 template <typename T
>
306 void circular_buffer_t
<T
>::data_added(unsigned int bytes
)
309 assert(end
<= capacity
);
314 template <typename T
>
315 void circular_buffer_t
<T
>::reset()
321 template <typename T
>
322 void circular_buffer_t
<T
>::append(const T
*src
, unsigned int count
)
324 unsigned int copy
= std::min(count
, contiguous_empty_size());
325 memcpy(contiguous_empty(), src
, copy
* sizeof(T
));
329 assert(count
< contiguous_empty_size());
330 memcpy(contiguous_empty(), src
, count
* sizeof(T
));
335 ////////////////////////////// Debug Operations
337 class halt_op_t
: public operation_t
340 halt_op_t(gdbserver_t
& gdbserver
, bool send_status
=false) :
341 operation_t(gdbserver
), send_status(send_status
),
344 void write_dpc_program() {
345 gs
.dr_write32(0, csrsi(CSR_DCSR
, DCSR_HALT
));
346 gs
.dr_write32(1, csrr(S0
, CSR_DPC
));
347 gs
.dr_write_store(2, S0
, SLOT_DATA0
);
352 bool perform_step(unsigned int step
) {
356 gs
.dr_write32(0, xori(S1
, ZERO
, -1));
357 gs
.dr_write32(1, srli(S1
, S1
, 31));
358 // 0x00000001 0x00000001:ffffffff 0x00000001:ffffffff:ffffffff:ffffffff
359 gs
.dr_write32(2, sw(S1
, ZERO
, DEBUG_RAM_START
));
360 gs
.dr_write32(3, srli(S1
, S1
, 31));
361 // 0x00000000 0x00000000:00000003 0x00000000:00000003:ffffffff:ffffffff
362 gs
.dr_write32(4, sw(S1
, ZERO
, DEBUG_RAM_START
+ 4));
375 uint32_t word0
= gs
.dr_read32(0);
376 uint32_t word1
= gs
.dr_read32(1);
378 if (word0
== 1 && word1
== 0) {
380 } else if (word0
== 0xffffffff && word1
== 3) {
382 } else if (word0
== 0xffffffff && word1
== 0xffffffff) {
392 gs
.dpc
= gs
.dr_read(SLOT_DATA0
);
393 gs
.dr_write32(0, csrr(S0
, CSR_MSTATUS
));
394 gs
.dr_write_store(1, S0
, SLOT_DATA0
);
401 gs
.mstatus
= gs
.dr_read(SLOT_DATA0
);
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:;");
454 class continue_op_t
: public operation_t
457 continue_op_t(gdbserver_t
& gdbserver
, bool single_step
) :
458 operation_t(gdbserver
), single_step(single_step
) {};
460 bool perform_step(unsigned int step
) {
463 gs
.dr_write_load(0, S0
, SLOT_DATA0
);
464 gs
.dr_write32(1, csrw(S0
, CSR_DPC
));
465 // TODO: Isn't there a fence.i in Debug ROM already?
466 if (gs
.fence_i_required
) {
467 gs
.dr_write32(2, fence_i());
469 gs
.fence_i_required
= false;
473 gs
.dr_write(SLOT_DATA0
, gs
.dpc
);
478 gs
.dr_write_load(0, S0
, SLOT_DATA0
);
479 gs
.dr_write32(1, csrw(S0
, CSR_MSTATUS
));
481 gs
.dr_write(SLOT_DATA0
, gs
.mstatus
);
486 gs
.dr_write32(0, lw(S0
, 0, (uint16_t) DEBUG_RAM_START
+16));
487 gs
.dr_write32(1, csrw(S0
, CSR_DCSR
));
490 reg_t dcsr
= set_field(gs
.dcsr
, DCSR_HALT
, 0);
491 dcsr
= set_field(dcsr
, DCSR_STEP
, single_step
);
492 // Software breakpoints should go here.
493 dcsr
= set_field(dcsr
, DCSR_EBREAKM
, 1);
494 dcsr
= set_field(dcsr
, DCSR_EBREAKH
, 1);
495 dcsr
= set_field(dcsr
, DCSR_EBREAKS
, 1);
496 dcsr
= set_field(dcsr
, DCSR_EBREAKU
, 1);
497 gs
.dr_write32(4, dcsr
);
509 class general_registers_read_op_t
: public operation_t
511 // Register order that gdb expects is:
512 // "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
513 // "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
514 // "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
515 // "x24", "x25", "x26", "x27", "x28", "x29", "x30", "x31",
517 // Each byte of register data is described by two hex digits. The bytes with
518 // the register are transmitted in target byte order. The size of each
519 // register and their position within the ‘g’ packet are determined by the
520 // gdb internal gdbarch functions DEPRECATED_REGISTER_RAW_SIZE and
521 // gdbarch_register_name.
524 general_registers_read_op_t(gdbserver_t
& gdbserver
) :
525 operation_t(gdbserver
) {};
527 bool perform_step(unsigned int step
)
529 D(fprintf(stderr
, "register_read step %d\n", step
));
533 // x0 is always zero.
535 gs
.send((uint32_t) 0);
537 gs
.send((uint64_t) 0);
540 gs
.dr_write_store(0, 1, SLOT_DATA0
);
541 gs
.dr_write_store(1, 2, SLOT_DATA1
);
548 gs
.send((uint32_t) gs
.dr_read(SLOT_DATA0
));
550 gs
.send((uint64_t) gs
.dr_read(SLOT_DATA0
));
558 gs
.send((uint32_t) gs
.dr_read(SLOT_DATA1
));
560 gs
.send((uint64_t) gs
.dr_read(SLOT_DATA1
));
563 unsigned int current_reg
= 2 * step
+ 1;
565 if (current_reg
== S1
) {
566 gs
.dr_write_load(i
++, S1
, SLOT_DATA_LAST
);
568 gs
.dr_write_store(i
++, current_reg
, SLOT_DATA0
);
569 if (current_reg
+ 1 == S0
) {
570 gs
.dr_write32(i
++, csrr(S0
, CSR_DSCRATCH
));
573 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);
619 } else if (reg
== REG_PRIV
) {
621 gs
.send((uint8_t) get_field(gs
.dcsr
, DCSR_PRV
));
625 gs
.send_packet("E02");
634 gs
.send(gs
.dr_read32(4));
636 gs
.send(gs
.dr_read(SLOT_DATA0
));
648 class register_write_op_t
: public operation_t
651 register_write_op_t(gdbserver_t
& gdbserver
, unsigned int reg
, reg_t value
) :
652 operation_t(gdbserver
), reg(reg
), value(value
) {};
654 bool perform_step(unsigned int step
)
656 gs
.dr_write_load(0, S0
, SLOT_DATA0
);
657 gs
.dr_write(SLOT_DATA0
, value
);
659 gs
.dr_write32(1, csrw(S0
, CSR_DSCRATCH
));
661 } else if (reg
== S1
) {
662 gs
.dr_write_store(1, S0
, SLOT_DATA_LAST
);
664 } else if (reg
>= REG_XPR0
&& reg
<= REG_XPR31
) {
665 gs
.dr_write32(1, addi(reg
, S0
, 0));
667 } else if (reg
== REG_PC
) {
670 } else if (reg
>= REG_FPR0
&& reg
<= REG_FPR31
) {
672 gs
.dr_write32(0, flw(reg
- REG_FPR0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
674 gs
.dr_write32(0, fld(reg
- REG_FPR0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
677 } else if (reg
>= REG_CSR0
&& reg
<= REG_CSR4095
) {
678 gs
.dr_write32(1, csrw(S0
, reg
- REG_CSR0
));
680 if (reg
== REG_CSR0
+ CSR_SPTBR
) {
682 gs
.sptbr_valid
= true;
684 } else if (reg
== REG_PRIV
) {
685 gs
.dcsr
= set_field(gs
.dcsr
, DCSR_PRV
, value
);
688 gs
.send_packet("E02");
692 gs
.send_packet("OK");
701 class memory_read_op_t
: public operation_t
704 // Read length bytes from vaddr, storing the result into data.
705 // If data is NULL, send the result straight to gdb.
706 memory_read_op_t(gdbserver_t
& gdbserver
, reg_t vaddr
, unsigned int length
,
707 unsigned char *data
=NULL
) :
708 operation_t(gdbserver
), vaddr(vaddr
), length(length
), data(data
) {};
710 bool perform_step(unsigned int step
)
713 // address goes in S0
714 paddr
= gs
.translate(vaddr
);
715 access_size
= gs
.find_access_size(paddr
, length
);
717 gs
.dr_write_load(0, S0
, SLOT_DATA0
);
718 switch (access_size
) {
720 gs
.dr_write32(1, lb(S1
, S0
, 0));
723 gs
.dr_write32(1, lh(S1
, S0
, 0));
726 gs
.dr_write32(1, lw(S1
, S0
, 0));
729 gs
.dr_write32(1, ld(S1
, S0
, 0));
732 gs
.dr_write_store(2, S1
, SLOT_DATA1
);
734 gs
.dr_write(SLOT_DATA0
, paddr
);
744 reg_t value
= gs
.dr_read(SLOT_DATA1
);
745 for (unsigned int i
= 0; i
< access_size
; i
++) {
747 *(data
++) = value
& 0xff;
748 D(fprintf(stderr
, "%02x", (unsigned int) (value
& 0xff)));
750 sprintf(buffer
, "%02x", (unsigned int) (value
& 0xff));
756 D(fprintf(stderr
, "\n"));
758 length
-= access_size
;
759 paddr
+= access_size
;
767 gs
.dr_write(SLOT_DATA0
, paddr
);
778 unsigned int access_size
;
781 class memory_write_op_t
: public operation_t
784 memory_write_op_t(gdbserver_t
& gdbserver
, reg_t vaddr
, unsigned int length
,
785 const unsigned char *data
) :
786 operation_t(gdbserver
), vaddr(vaddr
), offset(0), length(length
), data(data
) {};
788 ~memory_write_op_t() {
792 bool perform_step(unsigned int step
)
794 reg_t paddr
= gs
.translate(vaddr
);
796 unsigned int data_offset
;
799 data_offset
= slot_offset32
[SLOT_DATA1
];
802 data_offset
= slot_offset64
[SLOT_DATA1
];
805 data_offset
= slot_offset128
[SLOT_DATA1
];
812 access_size
= gs
.find_access_size(paddr
, length
);
814 D(fprintf(stderr
, "write to 0x%lx -> 0x%lx (access=%d): ", vaddr
, paddr
,
816 for (unsigned int i
= 0; i
< length
; i
++) {
817 D(fprintf(stderr
, "%02x", data
[i
]));
819 D(fprintf(stderr
, "\n"));
821 // address goes in S0
822 gs
.dr_write_load(0, S0
, SLOT_DATA0
);
823 switch (access_size
) {
825 gs
.dr_write32(1, lb(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 4*data_offset
));
826 gs
.dr_write32(2, sb(S1
, S0
, 0));
827 gs
.dr_write32(data_offset
, data
[0]);
830 gs
.dr_write32(1, lh(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 4*data_offset
));
831 gs
.dr_write32(2, sh(S1
, S0
, 0));
832 gs
.dr_write32(data_offset
, data
[0] | (data
[1] << 8));
835 gs
.dr_write32(1, lw(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 4*data_offset
));
836 gs
.dr_write32(2, sw(S1
, S0
, 0));
837 gs
.dr_write32(data_offset
, data
[0] | (data
[1] << 8) |
838 (data
[2] << 16) | (data
[3] << 24));
841 gs
.dr_write32(1, ld(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 4*data_offset
));
842 gs
.dr_write32(2, sd(S1
, S0
, 0));
843 gs
.dr_write32(data_offset
, data
[0] | (data
[1] << 8) |
844 (data
[2] << 16) | (data
[3] << 24));
845 gs
.dr_write32(data_offset
+1, data
[4] | (data
[5] << 8) |
846 (data
[6] << 16) | (data
[7] << 24));
849 fprintf(stderr
, "gdbserver error: write %d bytes to 0x%lx -> 0x%lx; "
850 "access_size=%d\n", length
, vaddr
, paddr
, access_size
);
851 gs
.send_packet("E12");
855 gs
.dr_write(SLOT_DATA0
, paddr
);
861 if (gs
.dr_read32(DEBUG_RAM_SIZE
/ 4 - 1)) {
862 fprintf(stderr
, "Exception happened while writing to 0x%lx -> 0x%lx\n",
866 offset
+= access_size
;
867 if (offset
>= length
) {
868 gs
.send_packet("OK");
871 const unsigned char *d
= data
+ offset
;
872 switch (access_size
) {
874 gs
.dr_write32(data_offset
, d
[0]);
877 gs
.dr_write32(data_offset
, d
[0] | (d
[1] << 8));
880 gs
.dr_write32(data_offset
, d
[0] | (d
[1] << 8) |
881 (d
[2] << 16) | (d
[3] << 24));
884 gs
.dr_write32(data_offset
, d
[0] | (d
[1] << 8) |
885 (d
[2] << 16) | (d
[3] << 24));
886 gs
.dr_write32(data_offset
+1, d
[4] | (d
[5] << 8) |
887 (d
[6] << 16) | (d
[7] << 24));
890 gs
.send_packet("E13");
893 gs
.dr_write(SLOT_DATA0
, paddr
+ offset
);
903 unsigned int access_size
;
904 const unsigned char *data
;
907 class collect_translation_info_op_t
: public operation_t
910 // Read sufficient information from the target into gdbserver structures so
911 // that it's possible to translate vaddr, vaddr+length, and all addresses
912 // in between to physical addresses.
913 collect_translation_info_op_t(gdbserver_t
& gdbserver
, reg_t vaddr
, size_t length
) :
914 operation_t(gdbserver
), state(STATE_START
), vaddr(vaddr
), length(length
) {};
916 bool perform_step(unsigned int step
)
918 unsigned int vm
= gs
.virtual_memory();
923 // Nothing to be done.
945 sprintf(buf
, "VM mode %d is not supported by gdbserver.cc.", vm
);
947 return true; // die doesn't return, but gcc doesn't know that.
952 // Perform any reads from the just-completed action.
956 case STATE_READ_SPTBR
:
957 gs
.sptbr
= gs
.dr_read(SLOT_DATA0
);
958 gs
.sptbr_valid
= true;
962 gs
.pte_cache
[pte_addr
] = gs
.dr_read32(4);
964 gs
.pte_cache
[pte_addr
] = ((uint64_t) gs
.dr_read32(5) << 32) |
967 D(fprintf(stderr
, "pte_cache[0x%lx] = 0x%lx\n", pte_addr
, gs
.pte_cache
[pte_addr
]));
971 // Set up the next action.
972 // We only get here for VM_SV32/39/38.
974 if (!gs
.sptbr_valid
) {
975 state
= STATE_READ_SPTBR
;
976 gs
.dr_write32(0, csrr(S0
, CSR_SPTBR
));
977 gs
.dr_write_store(1, S0
, SLOT_DATA0
);
983 reg_t base
= gs
.sptbr
<< PGSHIFT
;
984 int ptshift
= (levels
- 1) * ptidxbits
;
985 for (unsigned int i
= 0; i
< levels
; i
++, ptshift
-= ptidxbits
) {
986 reg_t idx
= (vaddr
>> (PGSHIFT
+ ptshift
)) & ((1 << ptidxbits
) - 1);
988 pte_addr
= base
+ idx
* ptesize
;
989 auto it
= gs
.pte_cache
.find(pte_addr
);
990 if (it
== gs
.pte_cache
.end()) {
991 state
= STATE_READ_PTE
;
993 gs
.dr_write32(0, lw(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
994 gs
.dr_write32(1, lw(S1
, S0
, 0));
995 gs
.dr_write32(2, sw(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
997 assert(gs
.xlen
>= 64);
998 gs
.dr_write32(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
999 gs
.dr_write32(1, ld(S1
, S0
, 0));
1000 gs
.dr_write32(2, sd(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
1002 gs
.dr_write32(3, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*3))));
1003 gs
.dr_write32(4, pte_addr
);
1004 gs
.dr_write32(5, pte_addr
>> 32);
1005 gs
.set_interrupt(0);
1009 reg_t pte
= gs
.pte_cache
[pte_addr
];
1010 reg_t ppn
= pte
>> PTE_PPN_SHIFT
;
1012 if (PTE_TABLE(pte
)) { // next level of page table
1013 base
= ppn
<< PGSHIFT
;
1015 // We've collected all the data required for the translation.
1020 "ERROR: gdbserver couldn't find appropriate PTEs to translate 0x%lx\n",
1033 unsigned int levels
;
1034 unsigned int ptidxbits
;
1035 unsigned int ptesize
;
1039 class hardware_breakpoint_insert_op_t
: public operation_t
1042 hardware_breakpoint_insert_op_t(gdbserver_t
& gdbserver
,
1043 hardware_breakpoint_t bp
) :
1044 operation_t(gdbserver
), state(STATE_START
), bp(bp
) {};
1046 void write_new_index_program()
1048 gs
.dr_write_load(0, S0
, SLOT_DATA1
);
1049 gs
.dr_write32(1, csrw(S0
, CSR_TSELECT
));
1050 gs
.dr_write32(2, csrr(S0
, CSR_TSELECT
));
1051 gs
.dr_write_store(3, S0
, SLOT_DATA1
);
1052 gs
.dr_write_jump(4);
1053 gs
.dr_write(SLOT_DATA1
, bp
.index
);
1056 bool perform_step(unsigned int step
)
1061 write_new_index_program();
1062 state
= STATE_CHECK_INDEX
;
1065 case STATE_CHECK_INDEX
:
1066 if (gs
.dr_read(SLOT_DATA1
) != bp
.index
) {
1067 // We've exhausted breakpoints without finding an appropriate one.
1068 gs
.send_packet("E58");
1072 gs
.dr_write32(0, csrr(S0
, CSR_TDATA0
));
1073 gs
.dr_write_store(1, S0
, SLOT_DATA0
);
1074 gs
.dr_write_jump(2);
1075 state
= STATE_CHECK_MCONTROL
;
1078 case STATE_CHECK_MCONTROL
:
1080 reg_t mcontrol
= gs
.dr_read(SLOT_DATA0
);
1081 unsigned int type
= mcontrol
>> (gs
.xlen
- 4);
1083 // We've exhausted breakpoints without finding an appropriate one.
1084 gs
.send_packet("E58");
1089 get_field(mcontrol
, MCONTROL_ACTION
) == MCONTROL_ACTION_NONE
) {
1090 // Found an unused trigger.
1091 gs
.dr_write_load(0, S0
, SLOT_DATA1
);
1092 gs
.dr_write32(1, csrw(S0
, CSR_TDATA0
));
1093 gs
.dr_write_jump(2);
1094 mcontrol
= set_field(0, MCONTROL_ACTION
, MCONTROL_ACTION_DEBUG_MODE
);
1095 mcontrol
= set_field(mcontrol
, MCONTROL_MATCH
, MCONTROL_MATCH_EQUAL
);
1096 mcontrol
= set_field(mcontrol
, MCONTROL_M
, 1);
1097 mcontrol
= set_field(mcontrol
, MCONTROL_H
, 1);
1098 mcontrol
= set_field(mcontrol
, MCONTROL_S
, 1);
1099 mcontrol
= set_field(mcontrol
, MCONTROL_U
, 1);
1100 mcontrol
= set_field(mcontrol
, MCONTROL_EXECUTE
, bp
.execute
);
1101 mcontrol
= set_field(mcontrol
, MCONTROL_LOAD
, bp
.load
);
1102 mcontrol
= set_field(mcontrol
, MCONTROL_STORE
, bp
.store
);
1103 gs
.dr_write(SLOT_DATA1
, mcontrol
);
1104 state
= STATE_WRITE_ADDRESS
;
1107 write_new_index_program();
1108 state
= STATE_CHECK_INDEX
;
1113 case STATE_WRITE_ADDRESS
:
1115 gs
.dr_write_load(0, S0
, SLOT_DATA1
);
1116 gs
.dr_write32(1, csrw(S0
, CSR_TDATA1
));
1117 gs
.dr_write_jump(2);
1118 gs
.dr_write(SLOT_DATA1
, bp
.vaddr
);
1119 gs
.set_interrupt(0);
1120 gs
.send_packet("OK");
1122 gs
.hardware_breakpoints
.insert(bp
);
1128 gs
.set_interrupt(0);
1136 STATE_CHECK_MCONTROL
,
1139 hardware_breakpoint_t bp
;
1142 class hardware_breakpoint_remove_op_t
: public operation_t
1145 hardware_breakpoint_remove_op_t(gdbserver_t
& gdbserver
,
1146 hardware_breakpoint_t bp
) :
1147 operation_t(gdbserver
), bp(bp
) {};
1149 bool perform_step(unsigned int step
) {
1150 gs
.dr_write32(0, addi(S0
, ZERO
, bp
.index
));
1151 gs
.dr_write32(1, csrw(S0
, CSR_TSELECT
));
1152 gs
.dr_write32(2, csrw(ZERO
, CSR_TDATA0
));
1153 gs
.dr_write_jump(3);
1154 gs
.set_interrupt(0);
1159 hardware_breakpoint_t bp
;
1162 ////////////////////////////// gdbserver itself
1164 gdbserver_t::gdbserver_t(uint16_t port
, sim_t
*sim
) :
1168 recv_buf(64 * 1024), send_buf(64 * 1024)
1170 socket_fd
= socket(AF_INET
, SOCK_STREAM
, 0);
1171 if (socket_fd
== -1) {
1172 fprintf(stderr
, "failed to make socket: %s (%d)\n", strerror(errno
), errno
);
1176 fcntl(socket_fd
, F_SETFL
, O_NONBLOCK
);
1178 if (setsockopt(socket_fd
, SOL_SOCKET
, SO_REUSEADDR
, &reuseaddr
,
1179 sizeof(int)) == -1) {
1180 fprintf(stderr
, "failed setsockopt: %s (%d)\n", strerror(errno
), errno
);
1184 struct sockaddr_in addr
;
1185 memset(&addr
, 0, sizeof(addr
));
1186 addr
.sin_family
= AF_INET
;
1187 addr
.sin_addr
.s_addr
= INADDR_ANY
;
1188 addr
.sin_port
= htons(port
);
1190 if (bind(socket_fd
, (struct sockaddr
*) &addr
, sizeof(addr
)) == -1) {
1191 fprintf(stderr
, "failed to bind socket: %s (%d)\n", strerror(errno
), errno
);
1195 if (listen(socket_fd
, 1) == -1) {
1196 fprintf(stderr
, "failed to listen on socket: %s (%d)\n", strerror(errno
), errno
);
1201 unsigned int gdbserver_t::find_access_size(reg_t address
, int length
)
1203 reg_t composite
= address
| length
;
1204 if ((composite
& 0x7) == 0 && xlen
>= 64)
1206 if ((composite
& 0x3) == 0)
1211 reg_t
gdbserver_t::translate(reg_t vaddr
)
1213 unsigned int vm
= virtual_memory();
1214 unsigned int levels
, ptidxbits
, ptesize
;
1239 sprintf(buf
, "VM mode %d is not supported by gdbserver.cc.", vm
);
1241 return true; // die doesn't return, but gcc doesn't know that.
1245 // Handle page tables here. There's a bunch of duplicated code with
1246 // collect_translation_info_op_t. :-(
1247 reg_t base
= sptbr
<< PGSHIFT
;
1248 int ptshift
= (levels
- 1) * ptidxbits
;
1249 for (unsigned int i
= 0; i
< levels
; i
++, ptshift
-= ptidxbits
) {
1250 reg_t idx
= (vaddr
>> (PGSHIFT
+ ptshift
)) & ((1 << ptidxbits
) - 1);
1252 reg_t pte_addr
= base
+ idx
* ptesize
;
1253 auto it
= pte_cache
.find(pte_addr
);
1254 if (it
== pte_cache
.end()) {
1255 fprintf(stderr
, "ERROR: gdbserver tried to translate 0x%lx without first "
1256 "collecting the relevant PTEs.\n", vaddr
);
1257 die("gdbserver_t::translate()");
1260 reg_t pte
= pte_cache
[pte_addr
];
1261 reg_t ppn
= pte
>> PTE_PPN_SHIFT
;
1263 if (PTE_TABLE(pte
)) { // next level of page table
1264 base
= ppn
<< PGSHIFT
;
1266 // We've collected all the data required for the translation.
1267 reg_t vpn
= vaddr
>> PGSHIFT
;
1268 reg_t paddr
= (ppn
| (vpn
& ((reg_t(1) << ptshift
) - 1))) << PGSHIFT
;
1269 paddr
+= vaddr
& (PGSIZE
-1);
1270 D(fprintf(stderr
, "gdbserver translate 0x%lx -> 0x%lx\n", vaddr
, paddr
));
1275 fprintf(stderr
, "ERROR: gdbserver tried to translate 0x%lx but the relevant "
1276 "PTEs are invalid.\n", vaddr
);
1277 // TODO: Is it better to throw an exception here?
1281 unsigned int gdbserver_t::privilege_mode()
1283 unsigned int mode
= get_field(dcsr
, DCSR_PRV
);
1284 if (get_field(mstatus
, MSTATUS_MPRV
))
1285 mode
= get_field(mstatus
, MSTATUS_MPP
);
1289 unsigned int gdbserver_t::virtual_memory()
1291 unsigned int mode
= privilege_mode();
1294 return get_field(mstatus
, MSTATUS_VM
);
1297 void gdbserver_t::dr_write32(unsigned int index
, uint32_t value
)
1299 sim
->debug_module
.ram_write32(index
, value
);
1302 void gdbserver_t::dr_write64(unsigned int index
, uint64_t value
)
1304 dr_write32(index
, value
);
1305 dr_write32(index
+1, value
>> 32);
1308 void gdbserver_t::dr_write(enum slot slot
, uint64_t value
)
1312 dr_write32(slot_offset32
[slot
], value
);
1315 dr_write64(slot_offset64
[slot
], value
);
1323 void gdbserver_t::dr_write_jump(unsigned int index
)
1325 dr_write32(index
, jal(0,
1326 (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*index
))));
1329 void gdbserver_t::dr_write_store(unsigned int index
, unsigned int reg
, enum slot slot
)
1331 assert(slot
!= SLOT_INST0
|| index
> 2);
1332 assert(slot
!= SLOT_DATA0
|| index
< 4 || index
> 6);
1333 assert(slot
!= SLOT_DATA1
|| index
< 5 || index
> 10);
1334 assert(slot
!= SLOT_DATA_LAST
|| index
< 6 || index
> 14);
1337 return dr_write32(index
,
1338 sw(reg
, 0, (uint16_t) DEBUG_RAM_START
+ 4 * slot_offset32
[slot
]));
1340 return dr_write32(index
,
1341 sd(reg
, 0, (uint16_t) DEBUG_RAM_START
+ 4 * slot_offset64
[slot
]));
1343 return dr_write32(index
,
1344 sq(reg
, 0, (uint16_t) DEBUG_RAM_START
+ 4 * slot_offset128
[slot
]));
1346 fprintf(stderr
, "xlen is %d!\n", xlen
);
1351 void gdbserver_t::dr_write_load(unsigned int index
, unsigned int reg
, enum slot slot
)
1355 return dr_write32(index
,
1356 lw(reg
, 0, (uint16_t) DEBUG_RAM_START
+ 4 * slot_offset32
[slot
]));
1358 return dr_write32(index
,
1359 ld(reg
, 0, (uint16_t) DEBUG_RAM_START
+ 4 * slot_offset64
[slot
]));
1361 return dr_write32(index
,
1362 lq(reg
, 0, (uint16_t) DEBUG_RAM_START
+ 4 * slot_offset128
[slot
]));
1364 fprintf(stderr
, "xlen is %d!\n", xlen
);
1369 uint32_t gdbserver_t::dr_read32(unsigned int index
)
1371 uint32_t value
= sim
->debug_module
.ram_read32(index
);
1372 D(fprintf(stderr
, "read32(%d) -> 0x%x\n", index
, value
));
1376 uint64_t gdbserver_t::dr_read64(unsigned int index
)
1378 return ((uint64_t) dr_read32(index
+1) << 32) | dr_read32(index
);
1381 uint64_t gdbserver_t::dr_read(enum slot slot
)
1385 return dr_read32(slot_offset32
[slot
]);
1387 return dr_read64(slot_offset64
[slot
]);
1395 void gdbserver_t::add_operation(operation_t
* operation
)
1397 operation_queue
.push(operation
);
1400 void gdbserver_t::accept()
1402 client_fd
= ::accept(socket_fd
, NULL
, NULL
);
1403 if (client_fd
== -1) {
1404 if (errno
== EAGAIN
) {
1405 // No client waiting to connect right now.
1407 fprintf(stderr
, "failed to accept on socket: %s (%d)\n", strerror(errno
),
1412 fcntl(client_fd
, F_SETFL
, O_NONBLOCK
);
1415 extended_mode
= false;
1417 // gdb wants the core to be halted when it attaches.
1418 add_operation(new halt_op_t(*this));
1422 void gdbserver_t::read()
1424 // Reading from a non-blocking socket still blocks if there is no data
1427 size_t count
= recv_buf
.contiguous_empty_size();
1429 ssize_t bytes
= ::read(client_fd
, recv_buf
.contiguous_empty(), count
);
1431 if (errno
== EAGAIN
) {
1432 // We'll try again the next call.
1434 fprintf(stderr
, "failed to read on socket: %s (%d)\n", strerror(errno
), errno
);
1437 } else if (bytes
== 0) {
1438 // The remote disconnected.
1440 processor_t
*p
= sim
->get_core(0);
1441 // TODO p->set_halted(false, HR_NONE);
1445 recv_buf
.data_added(bytes
);
1449 void gdbserver_t::write()
1451 if (send_buf
.empty())
1454 while (!send_buf
.empty()) {
1455 unsigned int count
= send_buf
.contiguous_data_size();
1457 ssize_t bytes
= ::write(client_fd
, send_buf
.contiguous_data(), count
);
1459 fprintf(stderr
, "failed to write to socket: %s (%d)\n", strerror(errno
), errno
);
1461 } else if (bytes
== 0) {
1462 // Client can't take any more data right now.
1465 D(fprintf(stderr
, "wrote %ld bytes: ", bytes
));
1466 for (unsigned int i
= 0; i
< bytes
; i
++) {
1467 D(fprintf(stderr
, "%c", send_buf
[i
]));
1469 D(fprintf(stderr
, "\n"));
1470 send_buf
.consume(bytes
);
1475 void print_packet(const std::vector
<uint8_t> &packet
)
1477 for (uint8_t c
: packet
) {
1478 if (c
>= ' ' and c
<= '~')
1479 fprintf(stderr
, "%c", c
);
1481 fprintf(stderr
, "\\x%02x", c
);
1483 fprintf(stderr
, "\n");
1486 uint8_t compute_checksum(const std::vector
<uint8_t> &packet
)
1488 uint8_t checksum
= 0;
1489 for (auto i
= packet
.begin() + 1; i
!= packet
.end() - 3; i
++ ) {
1495 uint8_t character_hex_value(uint8_t character
)
1497 if (character
>= '0' && character
<= '9')
1498 return character
- '0';
1499 if (character
>= 'a' && character
<= 'f')
1500 return 10 + character
- 'a';
1501 if (character
>= 'A' && character
<= 'F')
1502 return 10 + character
- 'A';
1506 uint8_t extract_checksum(const std::vector
<uint8_t> &packet
)
1508 return character_hex_value(*(packet
.end() - 1)) +
1509 16 * character_hex_value(*(packet
.end() - 2));
1512 void gdbserver_t::process_requests()
1514 // See https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html
1516 while (!recv_buf
.empty()) {
1517 std::vector
<uint8_t> packet
;
1518 for (unsigned int i
= 0; i
< recv_buf
.size(); i
++) {
1519 uint8_t b
= recv_buf
[i
];
1521 if (packet
.empty() && expect_ack
&& b
== '+') {
1522 recv_buf
.consume(1);
1526 if (packet
.empty() && b
== 3) {
1527 D(fprintf(stderr
, "Received interrupt\n"));
1528 recv_buf
.consume(1);
1534 // Start of new packet.
1535 if (!packet
.empty()) {
1536 fprintf(stderr
, "Received malformed %ld-byte packet from debug client: ",
1538 print_packet(packet
);
1539 recv_buf
.consume(i
);
1544 packet
.push_back(b
);
1546 // Packets consist of $<packet-data>#<checksum>
1547 // where <checksum> is
1548 if (packet
.size() >= 4 &&
1549 packet
[packet
.size()-3] == '#') {
1550 handle_packet(packet
);
1551 recv_buf
.consume(i
+1);
1555 // There's a partial packet in the buffer. Wait until we get more data to
1557 if (packet
.size()) {
1563 void gdbserver_t::handle_halt_reason(const std::vector
<uint8_t> &packet
)
1568 void gdbserver_t::handle_general_registers_read(const std::vector
<uint8_t> &packet
)
1570 add_operation(new general_registers_read_op_t(*this));
1573 void gdbserver_t::set_interrupt(uint32_t hartid
) {
1574 sim
->debug_module
.set_interrupt(hartid
);
1577 // First byte is the most-significant one.
1578 // Eg. "08675309" becomes 0x08675309.
1579 uint64_t consume_hex_number(std::vector
<uint8_t>::const_iterator
&iter
,
1580 std::vector
<uint8_t>::const_iterator end
)
1584 while (iter
!= end
) {
1586 uint64_t c_value
= character_hex_value(c
);
1596 // First byte is the least-significant one.
1597 // Eg. "08675309" becomes 0x09536708
1598 uint64_t consume_hex_number_le(std::vector
<uint8_t>::const_iterator
&iter
,
1599 std::vector
<uint8_t>::const_iterator end
)
1602 unsigned int shift
= 4;
1604 while (iter
!= end
) {
1606 uint64_t c_value
= character_hex_value(c
);
1610 value
|= c_value
<< shift
;
1611 if ((shift
% 8) == 0)
1619 void consume_string(std::string
&str
, std::vector
<uint8_t>::const_iterator
&iter
,
1620 std::vector
<uint8_t>::const_iterator end
, uint8_t separator
)
1622 while (iter
!= end
&& *iter
!= separator
) {
1623 str
.append(1, (char) *iter
);
1628 void gdbserver_t::handle_register_read(const std::vector
<uint8_t> &packet
)
1632 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1633 unsigned int n
= consume_hex_number(iter
, packet
.end());
1635 return send_packet("E01");
1637 add_operation(new register_read_op_t(*this, n
));
1640 void gdbserver_t::handle_register_write(const std::vector
<uint8_t> &packet
)
1644 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1645 unsigned int n
= consume_hex_number(iter
, packet
.end());
1647 return send_packet("E05");
1650 reg_t value
= consume_hex_number_le(iter
, packet
.end());
1652 return send_packet("E06");
1654 processor_t
*p
= sim
->get_core(0);
1656 add_operation(new register_write_op_t(*this, n
, value
));
1658 return send_packet("OK");
1661 void gdbserver_t::handle_memory_read(const std::vector
<uint8_t> &packet
)
1664 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1665 reg_t address
= consume_hex_number(iter
, packet
.end());
1667 return send_packet("E10");
1669 reg_t length
= consume_hex_number(iter
, packet
.end());
1671 return send_packet("E11");
1673 add_operation(new collect_translation_info_op_t(*this, address
, length
));
1674 add_operation(new memory_read_op_t(*this, address
, length
));
1677 void gdbserver_t::handle_memory_binary_write(const std::vector
<uint8_t> &packet
)
1679 // X addr,length:XX...
1680 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1681 reg_t address
= consume_hex_number(iter
, packet
.end());
1683 return send_packet("E20");
1685 reg_t length
= consume_hex_number(iter
, packet
.end());
1687 return send_packet("E21");
1691 return send_packet("OK");
1694 unsigned char *data
= new unsigned char[length
];
1695 for (unsigned int i
= 0; i
< length
; i
++) {
1696 if (iter
== packet
.end()) {
1697 return send_packet("E22");
1702 // The binary data representation uses 7d (ascii ‘}’) as an escape
1703 // character. Any escaped byte is transmitted as the escape character
1704 // followed by the original character XORed with 0x20. For example, the
1705 // byte 0x7d would be transmitted as the two bytes 0x7d 0x5d. The bytes
1706 // 0x23 (ascii ‘#’), 0x24 (ascii ‘$’), and 0x7d (ascii ‘}’) must always
1708 if (iter
== packet
.end()) {
1709 return send_packet("E23");
1717 return send_packet("E4b"); // EOVERFLOW
1719 add_operation(new collect_translation_info_op_t(*this, address
, length
));
1720 add_operation(new memory_write_op_t(*this, address
, length
, data
));
1723 void gdbserver_t::handle_continue(const std::vector
<uint8_t> &packet
)
1726 processor_t
*p
= sim
->get_core(0);
1727 if (packet
[2] != '#') {
1728 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1729 dpc
= consume_hex_number(iter
, packet
.end());
1731 return send_packet("E30");
1734 add_operation(new continue_op_t(*this, false));
1737 void gdbserver_t::handle_step(const std::vector
<uint8_t> &packet
)
1740 if (packet
[2] != '#') {
1741 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1743 //p->state.pc = consume_hex_number(iter, packet.end());
1745 return send_packet("E40");
1748 add_operation(new continue_op_t(*this, true));
1751 void gdbserver_t::handle_kill(const std::vector
<uint8_t> &packet
)
1754 // The exact effect of this packet is not specified.
1755 // Looks like OpenOCD disconnects?
1759 void gdbserver_t::handle_extended(const std::vector
<uint8_t> &packet
)
1761 // Enable extended mode. In extended mode, the remote server is made
1762 // persistent. The ‘R’ packet is used to restart the program being debugged.
1764 extended_mode
= true;
1767 void gdbserver_t::software_breakpoint_insert(reg_t vaddr
, unsigned int size
)
1769 fence_i_required
= true;
1770 add_operation(new collect_translation_info_op_t(*this, vaddr
, size
));
1771 unsigned char* inst
= new unsigned char[4];
1773 inst
[0] = C_EBREAK
& 0xff;
1774 inst
[1] = (C_EBREAK
>> 8) & 0xff;
1776 inst
[0] = EBREAK
& 0xff;
1777 inst
[1] = (EBREAK
>> 8) & 0xff;
1778 inst
[2] = (EBREAK
>> 16) & 0xff;
1779 inst
[3] = (EBREAK
>> 24) & 0xff;
1782 software_breakpoint_t bp
= {
1786 software_breakpoints
[vaddr
] = bp
;
1787 add_operation(new memory_read_op_t(*this, bp
.vaddr
, bp
.size
,
1788 software_breakpoints
[bp
.vaddr
].instruction
));
1789 add_operation(new memory_write_op_t(*this, bp
.vaddr
, bp
.size
, inst
));
1792 void gdbserver_t::software_breakpoint_remove(reg_t vaddr
, unsigned int size
)
1794 fence_i_required
= true;
1795 add_operation(new collect_translation_info_op_t(*this, vaddr
, size
));
1797 software_breakpoint_t found_bp
= software_breakpoints
[vaddr
];
1798 unsigned char* instruction
= new unsigned char[4];
1799 memcpy(instruction
, found_bp
.instruction
, 4);
1800 add_operation(new memory_write_op_t(*this, found_bp
.vaddr
,
1801 found_bp
.size
, instruction
));
1802 software_breakpoints
.erase(vaddr
);
1805 void gdbserver_t::hardware_breakpoint_insert(const hardware_breakpoint_t
&bp
)
1807 add_operation(new hardware_breakpoint_insert_op_t(*this, bp
));
1810 void gdbserver_t::hardware_breakpoint_remove(const hardware_breakpoint_t
&bp
)
1812 hardware_breakpoint_t found
= *hardware_breakpoints
.find(bp
);
1813 add_operation(new hardware_breakpoint_remove_op_t(*this, found
));
1816 void gdbserver_t::handle_breakpoint(const std::vector
<uint8_t> &packet
)
1818 // insert: Z type,addr,length
1819 // remove: z type,addr,length
1821 // type: 0 - software breakpoint, 1 - hardware breakpoint, 2 - write
1822 // watchpoint, 3 - read watchpoint, 4 - access watchpoint; addr is address;
1823 // length is in bytes. For a software breakpoint, length specifies the size
1824 // of the instruction to be patched. For hardware breakpoints and watchpoints
1825 // length specifies the memory region to be monitored. To avoid potential
1826 // problems with duplicate packets, the operations should be implemented in
1827 // an idempotent way.
1829 bool insert
= (packet
[1] == 'Z');
1830 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1831 gdb_breakpoint_type_t type
= static_cast<gdb_breakpoint_type_t
>(
1832 consume_hex_number(iter
, packet
.end()));
1834 return send_packet("E50");
1836 reg_t address
= consume_hex_number(iter
, packet
.end());
1838 return send_packet("E51");
1840 unsigned int size
= consume_hex_number(iter
, packet
.end());
1841 // There may be more options after a ; here, but we don't support that.
1843 return send_packet("E52");
1847 if (size
!= 2 && size
!= 4) {
1848 return send_packet("E53");
1851 software_breakpoint_insert(address
, size
);
1853 software_breakpoint_remove(address
, size
);
1862 hardware_breakpoint_t bp
= {
1866 bp
.load
= (type
== GB_READ
|| type
== GB_ACCESS
);
1867 bp
.store
= (type
== GB_WRITE
|| type
== GB_ACCESS
);
1868 bp
.execute
= (type
== GB_HARDWARE
|| type
== GB_ACCESS
);
1870 hardware_breakpoint_insert(bp
);
1871 // Insert might fail if there's no space, so the insert operation will
1872 // send its own OK (or not).
1875 hardware_breakpoint_remove(bp
);
1881 return send_packet("E56");
1884 return send_packet("OK");
1887 void gdbserver_t::handle_query(const std::vector
<uint8_t> &packet
)
1890 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1892 consume_string(name
, iter
, packet
.end(), ':');
1893 if (iter
!= packet
.end())
1895 if (name
== "Supported") {
1897 while (iter
!= packet
.end()) {
1898 std::string feature
;
1899 consume_string(feature
, iter
, packet
.end(), ';');
1900 if (iter
!= packet
.end())
1902 if (feature
== "swbreak+") {
1906 send("PacketSize=131072;");
1907 return end_packet();
1910 D(fprintf(stderr
, "Unsupported query %s\n", name
.c_str()));
1911 return send_packet("");
1914 void gdbserver_t::handle_packet(const std::vector
<uint8_t> &packet
)
1916 if (compute_checksum(packet
) != extract_checksum(packet
)) {
1917 fprintf(stderr
, "Received %ld-byte packet with invalid checksum\n", packet
.size());
1918 fprintf(stderr
, "Computed checksum: %x\n", compute_checksum(packet
));
1919 print_packet(packet
);
1924 D(fprintf(stderr
, "Received %ld-byte packet from debug client: ", packet
.size()));
1925 D(print_packet(packet
));
1928 switch (packet
[1]) {
1930 return handle_extended(packet
);
1932 return handle_halt_reason(packet
);
1934 return handle_general_registers_read(packet
);
1936 // return handle_kill(packet);
1938 return handle_memory_read(packet
);
1940 // return handle_memory_write(packet);
1942 return handle_memory_binary_write(packet
);
1944 return handle_register_read(packet
);
1946 return handle_register_write(packet
);
1948 return handle_continue(packet
);
1950 return handle_step(packet
);
1953 return handle_breakpoint(packet
);
1956 return handle_query(packet
);
1960 D(fprintf(stderr
, "** Unsupported packet: "));
1961 D(print_packet(packet
));
1965 void gdbserver_t::handle_interrupt()
1967 processor_t
*p
= sim
->get_core(0);
1968 add_operation(new halt_op_t(*this, true));
1971 void gdbserver_t::handle()
1973 if (client_fd
> 0) {
1974 processor_t
*p
= sim
->get_core(0);
1976 bool interrupt
= sim
->debug_module
.get_interrupt(0);
1978 if (!interrupt
&& !operation_queue
.empty()) {
1979 operation_t
*operation
= operation_queue
.front();
1980 if (operation
->step()) {
1981 operation_queue
.pop();
1986 bool halt_notification
= sim
->debug_module
.get_halt_notification(0);
1987 if (halt_notification
) {
1988 sim
->debug_module
.clear_halt_notification(0);
1989 add_operation(new halt_op_t(*this, true));
1999 if (operation_queue
.empty()) {
2000 this->process_requests();
2004 void gdbserver_t::send(const char* msg
)
2006 unsigned int length
= strlen(msg
);
2007 for (const char *c
= msg
; *c
; c
++)
2008 running_checksum
+= *c
;
2009 send_buf
.append((const uint8_t *) msg
, length
);
2012 void gdbserver_t::send(uint64_t value
)
2015 for (unsigned int i
= 0; i
< 8; i
++) {
2016 sprintf(buffer
, "%02x", (int) (value
& 0xff));
2022 void gdbserver_t::send(uint32_t value
)
2025 for (unsigned int i
= 0; i
< 4; i
++) {
2026 sprintf(buffer
, "%02x", (int) (value
& 0xff));
2032 void gdbserver_t::send(uint8_t value
)
2035 sprintf(buffer
, "%02x", (int) value
);
2039 void gdbserver_t::send_packet(const char* data
)
2047 void gdbserver_t::start_packet()
2050 running_checksum
= 0;
2053 void gdbserver_t::end_packet(const char* data
)
2059 char checksum_string
[4];
2060 sprintf(checksum_string
, "#%02x", running_checksum
);
2061 send(checksum_string
);