6 #include <sys/socket.h>
17 #include "gdbserver.h"
20 #define C_EBREAK 0x9002
21 #define EBREAK 0x00100073
23 //////////////////////////////////////// Utility Functions
25 void die(const char* msg
)
27 fprintf(stderr
, "gdbserver code died: %s\n", msg
);
31 // gdb's register list is defined in riscv_gdb_reg_names gdb/riscv-tdep.c in
32 // its source tree. We must interpret the numbers the same here.
44 //////////////////////////////////////// Functions to generate RISC-V opcodes.
46 // TODO: Does this already exist somewhere?
48 // Using regnames.cc as source. The RVG Calling Convention of the 2.0 RISC-V
49 // spec says it should be 2 and 3.
52 static uint32_t bits(uint32_t value
, unsigned int hi
, unsigned int lo
) {
53 return (value
>> lo
) & ((1 << (hi
+1-lo
)) - 1);
56 static uint32_t bit(uint32_t value
, unsigned int b
) {
57 return (value
>> b
) & 1;
60 static uint32_t jal(unsigned int rd
, uint32_t imm
) {
61 return (bit(imm
, 20) << 31) |
62 (bits(imm
, 10, 1) << 21) |
63 (bit(imm
, 11) << 20) |
64 (bits(imm
, 19, 12) << 12) |
69 static uint32_t csrsi(unsigned int csr
, uint16_t imm
) {
71 (bits(imm
, 4, 0) << 15) |
75 static uint32_t csrci(unsigned int csr
, uint16_t imm
) {
77 (bits(imm
, 4, 0) << 15) |
81 static uint32_t csrr(unsigned int rd
, unsigned int csr
) {
82 return (csr
<< 20) | (rd
<< 7) | MATCH_CSRRS
;
85 static uint32_t csrw(unsigned int source
, unsigned int csr
) {
86 return (csr
<< 20) | (source
<< 15) | MATCH_CSRRW
;
89 static uint32_t fence_i()
94 static uint32_t sb(unsigned int src
, unsigned int base
, uint16_t offset
)
96 return (bits(offset
, 11, 5) << 25) |
99 (bits(offset
, 4, 0) << 7) |
103 static uint32_t sh(unsigned int src
, unsigned int base
, uint16_t offset
)
105 return (bits(offset
, 11, 5) << 25) |
108 (bits(offset
, 4, 0) << 7) |
112 static uint32_t sw(unsigned int src
, unsigned int base
, uint16_t offset
)
114 return (bits(offset
, 11, 5) << 25) |
117 (bits(offset
, 4, 0) << 7) |
121 static uint32_t sd(unsigned int src
, unsigned int base
, uint16_t offset
)
123 return (bits(offset
, 11, 5) << 25) |
124 (bits(src
, 4, 0) << 20) |
126 (bits(offset
, 4, 0) << 7) |
130 static uint32_t ld(unsigned int rd
, unsigned int base
, uint16_t offset
)
132 return (bits(offset
, 11, 0) << 20) |
134 (bits(rd
, 4, 0) << 7) |
138 static uint32_t lw(unsigned int rd
, unsigned int base
, uint16_t offset
)
140 return (bits(offset
, 11, 0) << 20) |
142 (bits(rd
, 4, 0) << 7) |
146 static uint32_t lh(unsigned int rd
, unsigned int base
, uint16_t offset
)
148 return (bits(offset
, 11, 0) << 20) |
150 (bits(rd
, 4, 0) << 7) |
154 static uint32_t lb(unsigned int rd
, unsigned int base
, uint16_t offset
)
156 return (bits(offset
, 11, 0) << 20) |
158 (bits(rd
, 4, 0) << 7) |
162 static uint32_t fsd(unsigned int src
, unsigned int base
, uint16_t offset
)
164 return (bits(offset
, 11, 5) << 25) |
165 (bits(src
, 4, 0) << 20) |
167 (bits(offset
, 4, 0) << 7) |
171 static uint32_t fld(unsigned int src
, unsigned int base
, uint16_t offset
)
173 return (bits(offset
, 11, 5) << 25) |
174 (bits(src
, 4, 0) << 20) |
176 (bits(offset
, 4, 0) << 7) |
180 static uint32_t addi(unsigned int dest
, unsigned int src
, uint16_t imm
)
182 return (bits(imm
, 11, 0) << 20) |
188 static uint32_t nop()
190 return addi(0, 0, 0);
193 template <typename T
>
194 unsigned int circular_buffer_t
<T
>::size() const
199 return end
+ capacity
- start
;
202 template <typename T
>
203 void circular_buffer_t
<T
>::consume(unsigned int bytes
)
205 start
= (start
+ bytes
) % capacity
;
208 template <typename T
>
209 unsigned int circular_buffer_t
<T
>::contiguous_empty_size() const
213 return capacity
- end
- 1;
215 return capacity
- end
;
217 return start
- end
- 1;
220 template <typename T
>
221 unsigned int circular_buffer_t
<T
>::contiguous_data_size() const
226 return capacity
- start
;
229 template <typename T
>
230 void circular_buffer_t
<T
>::data_added(unsigned int bytes
)
233 assert(end
<= capacity
);
238 template <typename T
>
239 void circular_buffer_t
<T
>::reset()
245 template <typename T
>
246 void circular_buffer_t
<T
>::append(const T
*src
, unsigned int count
)
248 unsigned int copy
= std::min(count
, contiguous_empty_size());
249 memcpy(contiguous_empty(), src
, copy
* sizeof(T
));
253 assert(count
< contiguous_empty_size());
254 memcpy(contiguous_empty(), src
, count
* sizeof(T
));
259 ////////////////////////////// Debug Operations
261 class halt_op_t
: public operation_t
264 halt_op_t(gdbserver_t
& gdbserver
, bool send_status
=false) :
265 operation_t(gdbserver
), send_status(send_status
) {};
267 bool perform_step(unsigned int step
) {
270 // TODO: For now we just assume the target is 64-bit.
271 gs
.write_debug_ram(0, csrsi(DCSR_ADDRESS
, DCSR_HALT_MASK
));
272 gs
.write_debug_ram(1, csrr(S0
, DPC_ADDRESS
));
273 gs
.write_debug_ram(2, sd(S0
, 0, (uint16_t) DEBUG_RAM_START
));
274 gs
.write_debug_ram(3, csrr(S0
, CSR_MBADADDR
));
275 gs
.write_debug_ram(4, sd(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 8));
276 gs
.write_debug_ram(5, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*5))));
278 // We could read mcause here as well, but only on 64-bit targets. I'm
279 // trying to keep The patterns here usable for 32-bit ISAs as well. (On a
280 // 32-bit ISA 8 words are required, while the minimum Debug RAM size is 7
285 gs
.saved_dpc
= ((uint64_t) gs
.read_debug_ram(1) << 32) | gs
.read_debug_ram(0);
286 gs
.saved_mbadaddr
= ((uint64_t) gs
.read_debug_ram(3) << 32) | gs
.read_debug_ram(2);
288 gs
.write_debug_ram(0, csrr(S0
, CSR_MCAUSE
));
289 gs
.write_debug_ram(1, sd(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 0));
290 gs
.write_debug_ram(2, csrr(S0
, CSR_MSTATUS
));
291 gs
.write_debug_ram(3, sd(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 8));
292 gs
.write_debug_ram(4, csrr(S0
, CSR_DCSR
));
293 gs
.write_debug_ram(5, sd(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
294 gs
.write_debug_ram(6, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*6))));
299 gs
.saved_mcause
= ((uint64_t) gs
.read_debug_ram(1) << 32) | gs
.read_debug_ram(0);
300 gs
.saved_mstatus
= ((uint64_t) gs
.read_debug_ram(3) << 32) | gs
.read_debug_ram(2);
301 gs
.dcsr
= ((uint64_t) gs
.read_debug_ram(5) << 32) | gs
.read_debug_ram(4);
303 gs
.sptbr_valid
= false;
304 gs
.pte_cache
.clear();
307 switch (get_field(gs
.dcsr
, DCSR_CAUSE
)) {
308 case DCSR_CAUSE_NONE
:
309 fprintf(stderr
, "Internal error. Processor halted without reason.\n");
312 case DCSR_CAUSE_DEBUGINT
:
313 gs
.send_packet("S02"); // Pretend program received SIGINT.
316 case DCSR_CAUSE_HWBP
:
317 case DCSR_CAUSE_STEP
:
318 case DCSR_CAUSE_HALT
:
319 // There's no gdb code for this.
320 gs
.send_packet("T05");
322 case DCSR_CAUSE_SWBP
:
323 gs
.send_packet("T05swbreak:;");
337 class continue_op_t
: public operation_t
340 continue_op_t(gdbserver_t
& gdbserver
, bool single_step
) :
341 operation_t(gdbserver
), single_step(single_step
) {};
343 bool perform_step(unsigned int step
) {
346 gs
.write_debug_ram(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+16));
347 gs
.write_debug_ram(1, csrw(S0
, DPC_ADDRESS
));
348 if (gs
.fence_i_required
) {
349 gs
.write_debug_ram(2, fence_i());
350 gs
.write_debug_ram(3, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*3))));
351 gs
.fence_i_required
= false;
353 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
355 gs
.write_debug_ram(4, gs
.saved_dpc
);
356 gs
.write_debug_ram(5, gs
.saved_dpc
>> 32);
361 gs
.write_debug_ram(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+16));
362 gs
.write_debug_ram(1, csrw(S0
, CSR_MBADADDR
));
363 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
364 gs
.write_debug_ram(4, gs
.saved_mbadaddr
);
365 gs
.write_debug_ram(5, gs
.saved_mbadaddr
>> 32);
370 gs
.write_debug_ram(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+16));
371 gs
.write_debug_ram(1, csrw(S0
, CSR_MSTATUS
));
372 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
373 gs
.write_debug_ram(4, gs
.saved_mstatus
);
374 gs
.write_debug_ram(5, gs
.saved_mstatus
>> 32);
379 gs
.write_debug_ram(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+24));
380 gs
.write_debug_ram(1, csrw(S0
, CSR_MCAUSE
));
381 gs
.write_debug_ram(2, lw(S0
, 0, (uint16_t) DEBUG_RAM_START
+20));
382 gs
.write_debug_ram(3, csrw(S0
, CSR_DCSR
));
383 gs
.write_debug_ram(4, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*4))));
385 reg_t dcsr
= gs
.dcsr
& ~DCSR_HALT_MASK
;
387 dcsr
|= DCSR_STEP_MASK
;
389 dcsr
&= ~DCSR_STEP_MASK
;
390 gs
.write_debug_ram(5, dcsr
);
392 gs
.write_debug_ram(6, gs
.saved_mcause
);
393 gs
.write_debug_ram(7, gs
.saved_mcause
>> 32);
404 class general_registers_read_op_t
: public operation_t
406 // Register order that gdb expects is:
407 // "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
408 // "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
409 // "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
410 // "x24", "x25", "x26", "x27", "x28", "x29", "x30", "x31",
412 // Each byte of register data is described by two hex digits. The bytes with
413 // the register are transmitted in target byte order. The size of each
414 // register and their position within the ‘g’ packet are determined by the
415 // gdb internal gdbarch functions DEPRECATED_REGISTER_RAW_SIZE and
416 // gdbarch_register_name.
419 general_registers_read_op_t(gdbserver_t
& gdbserver
) :
420 operation_t(gdbserver
) {};
422 bool perform_step(unsigned int step
)
427 // x0 is always zero.
430 gs
.write_debug_ram(0, sd(1, 0, (uint16_t) DEBUG_RAM_START
+ 16));
431 gs
.write_debug_ram(1, sd(2, 0, (uint16_t) DEBUG_RAM_START
+ 0));
432 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
437 gs
.send(((uint64_t) gs
.read_debug_ram(5) << 32) | gs
.read_debug_ram(4));
443 gs
.send(((uint64_t) gs
.read_debug_ram(1) << 32) | gs
.read_debug_ram(0));
445 unsigned int current_reg
= 2 * step
+ 1;
447 if (current_reg
== S1
) {
448 gs
.write_debug_ram(i
++, ld(S1
, 0, (uint16_t) DEBUG_RAM_END
- 8));
450 gs
.write_debug_ram(i
++, sd(current_reg
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
451 if (current_reg
+ 1 == S0
) {
452 gs
.write_debug_ram(i
++, csrr(S0
, CSR_DSCRATCH
));
454 gs
.write_debug_ram(i
++, sd(current_reg
+1, 0, (uint16_t) DEBUG_RAM_START
+ 0));
455 gs
.write_debug_ram(i
, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*i
))));
462 class register_read_op_t
: public operation_t
465 register_read_op_t(gdbserver_t
& gdbserver
, unsigned int reg
) :
466 operation_t(gdbserver
), reg(reg
) {};
468 bool perform_step(unsigned int step
)
472 if (reg
>= REG_XPR0
&& reg
<= REG_XPR31
) {
473 die("handle_register_read");
474 // send(p->state.XPR[reg - REG_XPR0]);
475 } else if (reg
== REG_PC
) {
477 gs
.send(gs
.saved_dpc
);
480 } else if (reg
>= REG_FPR0
&& reg
<= REG_FPR31
) {
481 // send(p->state.FPR[reg - REG_FPR0]);
482 gs
.write_debug_ram(0, fsd(reg
- REG_FPR0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
483 gs
.write_debug_ram(1, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*1))));
484 } else if (reg
== REG_CSR0
+ CSR_MBADADDR
) {
486 gs
.send(gs
.saved_mbadaddr
);
489 } else if (reg
== REG_CSR0
+ CSR_MCAUSE
) {
491 gs
.send(gs
.saved_mcause
);
494 } else if (reg
>= REG_CSR0
&& reg
<= REG_CSR4095
) {
495 gs
.write_debug_ram(0, csrr(S0
, reg
- REG_CSR0
));
496 gs
.write_debug_ram(1, sd(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
497 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
498 // If we hit an exception reading the CSR, we'll end up returning ~0 as
499 // the register's value, which is what we want. (Right?)
500 gs
.write_debug_ram(4, 0xffffffff);
501 gs
.write_debug_ram(5, 0xffffffff);
503 gs
.send_packet("E02");
511 gs
.send(((uint64_t) gs
.read_debug_ram(5) << 32) | gs
.read_debug_ram(4));
522 class register_write_op_t
: public operation_t
525 register_write_op_t(gdbserver_t
& gdbserver
, unsigned int reg
, reg_t value
) :
526 operation_t(gdbserver
), reg(reg
), value(value
) {};
528 bool perform_step(unsigned int step
)
530 gs
.write_debug_ram(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
531 gs
.write_debug_ram(4, value
);
532 gs
.write_debug_ram(5, value
>> 32);
534 gs
.write_debug_ram(1, csrw(S0
, CSR_DSCRATCH
));
535 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
536 } else if (reg
== S1
) {
537 gs
.write_debug_ram(1, sd(S0
, 0, (uint16_t) DEBUG_RAM_END
- 8));
538 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
539 } else if (reg
>= REG_XPR0
&& reg
<= REG_XPR31
) {
540 gs
.write_debug_ram(1, addi(reg
, S0
, 0));
541 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
542 } else if (reg
== REG_PC
) {
543 gs
.saved_dpc
= value
;
545 } else if (reg
>= REG_FPR0
&& reg
<= REG_FPR31
) {
546 // send(p->state.FPR[reg - REG_FPR0]);
547 gs
.write_debug_ram(0, fld(reg
- REG_FPR0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
548 gs
.write_debug_ram(1, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*1))));
549 } else if (reg
== REG_CSR0
+ CSR_MBADADDR
) {
550 gs
.saved_mbadaddr
= value
;
552 } else if (reg
== REG_CSR0
+ CSR_MCAUSE
) {
553 gs
.saved_mcause
= value
;
555 } else if (reg
>= REG_CSR0
&& reg
<= REG_CSR4095
) {
556 gs
.write_debug_ram(1, csrw(S0
, reg
- REG_CSR0
));
557 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
559 gs
.send_packet("E02");
563 gs
.send_packet("OK");
572 class memory_read_op_t
: public operation_t
575 // Read length bytes from vaddr, storing the result into data.
576 // If data is NULL, send the result straight to gdb.
577 memory_read_op_t(gdbserver_t
& gdbserver
, reg_t vaddr
, unsigned int length
,
578 unsigned char *data
=NULL
) :
579 operation_t(gdbserver
), vaddr(vaddr
), length(length
), data(data
) {};
581 bool perform_step(unsigned int step
)
584 // address goes in S0
585 paddr
= gs
.translate(vaddr
);
586 access_size
= (paddr
% length
);
587 if (access_size
== 0)
588 access_size
= length
;
592 gs
.write_debug_ram(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
593 switch (access_size
) {
595 gs
.write_debug_ram(1, lb(S1
, S0
, 0));
598 gs
.write_debug_ram(1, lh(S1
, S0
, 0));
601 gs
.write_debug_ram(1, lw(S1
, S0
, 0));
604 gs
.write_debug_ram(1, ld(S1
, S0
, 0));
607 gs
.write_debug_ram(2, sd(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 24));
608 gs
.write_debug_ram(3, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*3))));
609 gs
.write_debug_ram(4, paddr
);
610 gs
.write_debug_ram(5, paddr
>> 32);
620 reg_t value
= ((uint64_t) gs
.read_debug_ram(7) << 32) | gs
.read_debug_ram(6);
621 for (unsigned int i
= 0; i
< access_size
; i
++) {
623 *(data
++) = value
& 0xff;
624 fprintf(stderr
, "%02x", (unsigned int) (value
& 0xff));
626 sprintf(buffer
, "%02x", (unsigned int) (value
& 0xff));
632 fprintf(stderr
, "\n");
633 length
-= access_size
;
634 paddr
+= access_size
;
642 gs
.write_debug_ram(4, paddr
);
643 gs
.write_debug_ram(5, paddr
>> 32);
654 unsigned int access_size
;
657 class memory_write_op_t
: public operation_t
660 memory_write_op_t(gdbserver_t
& gdbserver
, reg_t vaddr
, unsigned int length
,
661 const unsigned char *data
) :
662 operation_t(gdbserver
), vaddr(vaddr
), offset(0), length(length
), data(data
) {};
664 ~memory_write_op_t() {
668 bool perform_step(unsigned int step
)
670 reg_t paddr
= gs
.translate(vaddr
);
672 // address goes in S0
673 access_size
= (paddr
% length
);
674 if (access_size
== 0)
675 access_size
= length
;
679 fprintf(stderr
, "write to 0x%lx -> 0x%lx: ", vaddr
, paddr
);
680 for (unsigned int i
= 0; i
< length
; i
++)
681 fprintf(stderr
, "%02x", data
[i
]);
682 fprintf(stderr
, "\n");
684 gs
.write_debug_ram(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
685 switch (access_size
) {
687 gs
.write_debug_ram(1, lb(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 24));
688 gs
.write_debug_ram(2, sb(S1
, S0
, 0));
689 gs
.write_debug_ram(6, data
[0]);
692 gs
.write_debug_ram(1, lh(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 24));
693 gs
.write_debug_ram(2, sh(S1
, S0
, 0));
694 gs
.write_debug_ram(6, data
[0] | (data
[1] << 8));
697 gs
.write_debug_ram(1, lw(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 24));
698 gs
.write_debug_ram(2, sw(S1
, S0
, 0));
699 gs
.write_debug_ram(6, data
[0] | (data
[1] << 8) |
700 (data
[2] << 16) | (data
[3] << 24));
703 gs
.write_debug_ram(1, ld(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 24));
704 gs
.write_debug_ram(2, sd(S1
, S0
, 0));
705 gs
.write_debug_ram(6, data
[0] | (data
[1] << 8) |
706 (data
[2] << 16) | (data
[3] << 24));
707 gs
.write_debug_ram(7, data
[4] | (data
[5] << 8) |
708 (data
[6] << 16) | (data
[7] << 24));
711 gs
.send_packet("E12");
714 gs
.write_debug_ram(3, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*3))));
715 gs
.write_debug_ram(4, paddr
);
716 gs
.write_debug_ram(5, paddr
>> 32);
722 offset
+= access_size
;
723 if (offset
>= length
) {
724 gs
.send_packet("OK");
727 const unsigned char *d
= data
+ offset
;
728 switch (access_size
) {
730 gs
.write_debug_ram(6, d
[0]);
733 gs
.write_debug_ram(6, d
[0] | (d
[1] << 8));
736 gs
.write_debug_ram(6, d
[0] | (d
[1] << 8) |
737 (d
[2] << 16) | (d
[3] << 24));
740 gs
.write_debug_ram(6, d
[0] | (d
[1] << 8) |
741 (d
[2] << 16) | (d
[3] << 24));
742 gs
.write_debug_ram(7, d
[4] | (d
[5] << 8) |
743 (d
[6] << 16) | (d
[7] << 24));
746 gs
.send_packet("E12");
749 gs
.write_debug_ram(4, paddr
+ offset
);
750 gs
.write_debug_ram(5, (paddr
+ offset
) >> 32);
760 unsigned int access_size
;
761 const unsigned char *data
;
764 class collect_translation_info_op_t
: public operation_t
767 // Read sufficient information from the target into gdbserver structures so
768 // that it's possible to translate vaddr, vaddr+length, and all addresses
769 // in between to physical addresses.
770 collect_translation_info_op_t(gdbserver_t
& gdbserver
, reg_t vaddr
, size_t length
) :
771 operation_t(gdbserver
), state(STATE_START
), vaddr(vaddr
), length(length
) {};
773 bool perform_step(unsigned int step
)
775 unsigned int vm
= gs
.virtual_memory();
780 // Nothing to be done.
802 sprintf(buf
, "VM mode %d is not supported by gdbserver.cc.", vm
);
804 return true; // die doesn't return, but gcc doesn't know that.
809 // Perform any reads from the just-completed action.
813 case STATE_READ_SPTBR
:
814 gs
.sptbr
= ((uint64_t) gs
.read_debug_ram(5) << 32) | gs
.read_debug_ram(4);
815 gs
.sptbr_valid
= true;
818 gs
.pte_cache
[pte_addr
] = ((uint64_t) gs
.read_debug_ram(5) << 32) |
819 gs
.read_debug_ram(4);
820 fprintf(stderr
, "pte_cache[0x%lx] = 0x%lx\n", pte_addr
, gs
.pte_cache
[pte_addr
]);
824 // Set up the next action.
825 // We only get here for VM_SV32/39/38.
827 if (!gs
.sptbr_valid
) {
828 state
= STATE_READ_SPTBR
;
829 gs
.write_debug_ram(0, csrr(S0
, CSR_SPTBR
));
830 gs
.write_debug_ram(1, sd(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
831 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
836 reg_t base
= gs
.sptbr
<< PGSHIFT
;
837 int ptshift
= (levels
- 1) * ptidxbits
;
838 for (unsigned int i
= 0; i
< levels
; i
++, ptshift
-= ptidxbits
) {
839 reg_t idx
= (vaddr
>> (PGSHIFT
+ ptshift
)) & ((1 << ptidxbits
) - 1);
841 pte_addr
= base
+ idx
* ptesize
;
842 auto it
= gs
.pte_cache
.find(pte_addr
);
843 if (it
== gs
.pte_cache
.end()) {
844 state
= STATE_READ_PTE
;
846 gs
.write_debug_ram(0, lw(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
847 gs
.write_debug_ram(1, lw(S1
, S0
, 0));
848 gs
.write_debug_ram(2, sd(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
850 gs
.write_debug_ram(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
851 gs
.write_debug_ram(1, ld(S1
, S0
, 0));
852 gs
.write_debug_ram(2, sd(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
854 gs
.write_debug_ram(3, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*3))));
855 gs
.write_debug_ram(4, pte_addr
);
856 gs
.write_debug_ram(5, pte_addr
>> 32);
861 reg_t pte
= gs
.pte_cache
[pte_addr
];
862 reg_t ppn
= pte
>> PTE_PPN_SHIFT
;
864 if (PTE_TABLE(pte
)) { // next level of page table
865 base
= ppn
<< PGSHIFT
;
867 // We've collected all the data required for the translation.
872 "ERROR: gdbserver couldn't find appropriate PTEs to translate 0x%lx\n",
886 unsigned int ptidxbits
;
887 unsigned int ptesize
;
891 ////////////////////////////// gdbserver itself
893 gdbserver_t::gdbserver_t(uint16_t port
, sim_t
*sim
) :
896 recv_buf(64 * 1024), send_buf(64 * 1024)
898 socket_fd
= socket(AF_INET
, SOCK_STREAM
, 0);
899 if (socket_fd
== -1) {
900 fprintf(stderr
, "failed to make socket: %s (%d)\n", strerror(errno
), errno
);
904 fcntl(socket_fd
, F_SETFL
, O_NONBLOCK
);
906 if (setsockopt(socket_fd
, SOL_SOCKET
, SO_REUSEADDR
, &reuseaddr
,
907 sizeof(int)) == -1) {
908 fprintf(stderr
, "failed setsockopt: %s (%d)\n", strerror(errno
), errno
);
912 struct sockaddr_in addr
;
913 memset(&addr
, 0, sizeof(addr
));
914 addr
.sin_family
= AF_INET
;
915 addr
.sin_addr
.s_addr
= INADDR_ANY
;
916 addr
.sin_port
= htons(port
);
918 if (bind(socket_fd
, (struct sockaddr
*) &addr
, sizeof(addr
)) == -1) {
919 fprintf(stderr
, "failed to bind socket: %s (%d)\n", strerror(errno
), errno
);
923 if (listen(socket_fd
, 1) == -1) {
924 fprintf(stderr
, "failed to listen on socket: %s (%d)\n", strerror(errno
), errno
);
929 reg_t
gdbserver_t::translate(reg_t vaddr
)
931 unsigned int vm
= virtual_memory();
932 unsigned int levels
, ptidxbits
, ptesize
;
957 sprintf(buf
, "VM mode %d is not supported by gdbserver.cc.", vm
);
959 return true; // die doesn't return, but gcc doesn't know that.
963 // Handle page tables here. There's a bunch of duplicated code with
964 // collect_translation_info_op_t. :-(
965 reg_t base
= sptbr
<< PGSHIFT
;
966 int ptshift
= (levels
- 1) * ptidxbits
;
967 for (unsigned int i
= 0; i
< levels
; i
++, ptshift
-= ptidxbits
) {
968 reg_t idx
= (vaddr
>> (PGSHIFT
+ ptshift
)) & ((1 << ptidxbits
) - 1);
970 reg_t pte_addr
= base
+ idx
* ptesize
;
971 auto it
= pte_cache
.find(pte_addr
);
972 if (it
== pte_cache
.end()) {
973 fprintf(stderr
, "ERROR: gdbserver tried to translate 0x%lx without first "
974 "collecting the relevant PTEs.\n", vaddr
);
975 die("gdbserver_t::translate()");
978 reg_t pte
= pte_cache
[pte_addr
];
979 reg_t ppn
= pte
>> PTE_PPN_SHIFT
;
981 if (PTE_TABLE(pte
)) { // next level of page table
982 base
= ppn
<< PGSHIFT
;
984 // We've collected all the data required for the translation.
985 reg_t vpn
= vaddr
>> PGSHIFT
;
986 reg_t paddr
= (ppn
| (vpn
& ((reg_t(1) << ptshift
) - 1))) << PGSHIFT
;
987 paddr
+= vaddr
& (PGSIZE
-1);
988 fprintf(stderr
, "gdbserver translate 0x%lx -> 0x%lx\n", vaddr
, paddr
);
993 fprintf(stderr
, "ERROR: gdbserver tried to translate 0x%lx but the relevant "
994 "PTEs are invalid.\n", vaddr
);
995 // TODO: Is it better to throw an exception here?
999 unsigned int gdbserver_t::privilege_mode()
1001 unsigned int mode
= get_field(dcsr
, DCSR_PRV
);
1002 if (get_field(saved_mstatus
, MSTATUS_MPRV
))
1003 mode
= get_field(saved_mstatus
, MSTATUS_MPP
);
1007 unsigned int gdbserver_t::virtual_memory()
1009 unsigned int mode
= privilege_mode();
1012 return get_field(saved_mstatus
, MSTATUS_VM
);
1015 void gdbserver_t::write_debug_ram(unsigned int index
, uint32_t value
)
1017 sim
->debug_module
.ram_write32(index
, value
);
1020 uint32_t gdbserver_t::read_debug_ram(unsigned int index
)
1022 return sim
->debug_module
.ram_read32(index
);
1025 void gdbserver_t::add_operation(operation_t
* operation
)
1027 operation_queue
.push(operation
);
1030 void gdbserver_t::accept()
1032 client_fd
= ::accept(socket_fd
, NULL
, NULL
);
1033 if (client_fd
== -1) {
1034 if (errno
== EAGAIN
) {
1035 // No client waiting to connect right now.
1037 fprintf(stderr
, "failed to accept on socket: %s (%d)\n", strerror(errno
),
1042 fcntl(client_fd
, F_SETFL
, O_NONBLOCK
);
1045 extended_mode
= false;
1047 // gdb wants the core to be halted when it attaches.
1048 add_operation(new halt_op_t(*this));
1052 void gdbserver_t::read()
1054 // Reading from a non-blocking socket still blocks if there is no data
1057 size_t count
= recv_buf
.contiguous_empty_size();
1059 ssize_t bytes
= ::read(client_fd
, recv_buf
.contiguous_empty(), count
);
1061 if (errno
== EAGAIN
) {
1062 // We'll try again the next call.
1064 fprintf(stderr
, "failed to read on socket: %s (%d)\n", strerror(errno
), errno
);
1067 } else if (bytes
== 0) {
1068 // The remote disconnected.
1070 processor_t
*p
= sim
->get_core(0);
1071 // TODO p->set_halted(false, HR_NONE);
1075 recv_buf
.data_added(bytes
);
1079 void gdbserver_t::write()
1081 if (send_buf
.empty())
1084 while (!send_buf
.empty()) {
1085 unsigned int count
= send_buf
.contiguous_data_size();
1087 ssize_t bytes
= ::write(client_fd
, send_buf
.contiguous_data(), count
);
1089 fprintf(stderr
, "failed to write to socket: %s (%d)\n", strerror(errno
), errno
);
1091 } else if (bytes
== 0) {
1092 // Client can't take any more data right now.
1095 fprintf(stderr
, "wrote %ld bytes: ", bytes
);
1096 for (unsigned int i
= 0; i
< bytes
; i
++) {
1097 fprintf(stderr
, "%c", send_buf
[i
]);
1099 fprintf(stderr
, "\n");
1100 send_buf
.consume(bytes
);
1105 void print_packet(const std::vector
<uint8_t> &packet
)
1107 for (uint8_t c
: packet
) {
1108 if (c
>= ' ' and c
<= '~')
1109 fprintf(stderr
, "%c", c
);
1111 fprintf(stderr
, "\\x%02x", c
);
1113 fprintf(stderr
, "\n");
1116 uint8_t compute_checksum(const std::vector
<uint8_t> &packet
)
1118 uint8_t checksum
= 0;
1119 for (auto i
= packet
.begin() + 1; i
!= packet
.end() - 3; i
++ ) {
1125 uint8_t character_hex_value(uint8_t character
)
1127 if (character
>= '0' && character
<= '9')
1128 return character
- '0';
1129 if (character
>= 'a' && character
<= 'f')
1130 return 10 + character
- 'a';
1131 if (character
>= 'A' && character
<= 'F')
1132 return 10 + character
- 'A';
1136 uint8_t extract_checksum(const std::vector
<uint8_t> &packet
)
1138 return character_hex_value(*(packet
.end() - 1)) +
1139 16 * character_hex_value(*(packet
.end() - 2));
1142 void gdbserver_t::process_requests()
1144 // See https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html
1146 while (!recv_buf
.empty()) {
1147 std::vector
<uint8_t> packet
;
1148 for (unsigned int i
= 0; i
< recv_buf
.size(); i
++) {
1149 uint8_t b
= recv_buf
[i
];
1151 if (packet
.empty() && expect_ack
&& b
== '+') {
1152 recv_buf
.consume(1);
1156 if (packet
.empty() && b
== 3) {
1157 fprintf(stderr
, "Received interrupt\n");
1158 recv_buf
.consume(1);
1164 // Start of new packet.
1165 if (!packet
.empty()) {
1166 fprintf(stderr
, "Received malformed %ld-byte packet from debug client: ",
1168 print_packet(packet
);
1169 recv_buf
.consume(i
);
1174 packet
.push_back(b
);
1176 // Packets consist of $<packet-data>#<checksum>
1177 // where <checksum> is
1178 if (packet
.size() >= 4 &&
1179 packet
[packet
.size()-3] == '#') {
1180 handle_packet(packet
);
1181 recv_buf
.consume(i
+1);
1185 // There's a partial packet in the buffer. Wait until we get more data to
1187 if (packet
.size()) {
1193 void gdbserver_t::handle_halt_reason(const std::vector
<uint8_t> &packet
)
1198 void gdbserver_t::handle_general_registers_read(const std::vector
<uint8_t> &packet
)
1200 add_operation(new general_registers_read_op_t(*this));
1203 void gdbserver_t::set_interrupt(uint32_t hartid
) {
1204 sim
->debug_module
.set_interrupt(hartid
);
1207 // First byte is the most-significant one.
1208 // Eg. "08675309" becomes 0x08675309.
1209 uint64_t consume_hex_number(std::vector
<uint8_t>::const_iterator
&iter
,
1210 std::vector
<uint8_t>::const_iterator end
)
1214 while (iter
!= end
) {
1216 uint64_t c_value
= character_hex_value(c
);
1226 // First byte is the least-significant one.
1227 // Eg. "08675309" becomes 0x09536708
1228 uint64_t consume_hex_number_le(std::vector
<uint8_t>::const_iterator
&iter
,
1229 std::vector
<uint8_t>::const_iterator end
)
1232 unsigned int shift
= 4;
1234 while (iter
!= end
) {
1236 uint64_t c_value
= character_hex_value(c
);
1240 value
|= c_value
<< shift
;
1241 if ((shift
% 8) == 0)
1249 void consume_string(std::string
&str
, std::vector
<uint8_t>::const_iterator
&iter
,
1250 std::vector
<uint8_t>::const_iterator end
, uint8_t separator
)
1252 while (iter
!= end
&& *iter
!= separator
) {
1253 str
.append(1, (char) *iter
);
1258 void gdbserver_t::handle_register_read(const std::vector
<uint8_t> &packet
)
1262 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1263 unsigned int n
= consume_hex_number(iter
, packet
.end());
1265 return send_packet("E01");
1267 add_operation(new register_read_op_t(*this, n
));
1270 void gdbserver_t::handle_register_write(const std::vector
<uint8_t> &packet
)
1274 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1275 unsigned int n
= consume_hex_number(iter
, packet
.end());
1277 return send_packet("E05");
1280 reg_t value
= consume_hex_number_le(iter
, packet
.end());
1282 return send_packet("E06");
1284 processor_t
*p
= sim
->get_core(0);
1286 add_operation(new register_write_op_t(*this, n
, value
));
1288 return send_packet("OK");
1291 void gdbserver_t::handle_memory_read(const std::vector
<uint8_t> &packet
)
1294 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1295 reg_t address
= consume_hex_number(iter
, packet
.end());
1297 return send_packet("E10");
1299 reg_t length
= consume_hex_number(iter
, packet
.end());
1301 return send_packet("E11");
1303 add_operation(new collect_translation_info_op_t(*this, address
, length
));
1304 add_operation(new memory_read_op_t(*this, address
, length
));
1307 void gdbserver_t::handle_memory_binary_write(const std::vector
<uint8_t> &packet
)
1309 // X addr,length:XX...
1310 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1311 reg_t address
= consume_hex_number(iter
, packet
.end());
1313 return send_packet("E20");
1315 reg_t length
= consume_hex_number(iter
, packet
.end());
1317 return send_packet("E21");
1321 return send_packet("OK");
1324 unsigned char *data
= new unsigned char[length
];
1325 for (unsigned int i
= 0; i
< length
; i
++) {
1326 if (iter
== packet
.end()) {
1327 return send_packet("E22");
1332 // The binary data representation uses 7d (ascii ‘}’) as an escape
1333 // character. Any escaped byte is transmitted as the escape character
1334 // followed by the original character XORed with 0x20. For example, the
1335 // byte 0x7d would be transmitted as the two bytes 0x7d 0x5d. The bytes
1336 // 0x23 (ascii ‘#’), 0x24 (ascii ‘$’), and 0x7d (ascii ‘}’) must always
1338 if (iter
== packet
.end()) {
1339 return send_packet("E23");
1347 return send_packet("E4b"); // EOVERFLOW
1349 add_operation(new collect_translation_info_op_t(*this, address
, length
));
1350 add_operation(new memory_write_op_t(*this, address
, length
, data
));
1353 void gdbserver_t::handle_continue(const std::vector
<uint8_t> &packet
)
1356 processor_t
*p
= sim
->get_core(0);
1357 if (packet
[2] != '#') {
1358 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1359 saved_dpc
= consume_hex_number(iter
, packet
.end());
1361 return send_packet("E30");
1364 add_operation(new continue_op_t(*this, false));
1367 void gdbserver_t::handle_step(const std::vector
<uint8_t> &packet
)
1370 if (packet
[2] != '#') {
1371 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1373 //p->state.pc = consume_hex_number(iter, packet.end());
1375 return send_packet("E40");
1378 add_operation(new continue_op_t(*this, true));
1381 void gdbserver_t::handle_kill(const std::vector
<uint8_t> &packet
)
1384 // The exact effect of this packet is not specified.
1385 // Looks like OpenOCD disconnects?
1389 void gdbserver_t::handle_extended(const std::vector
<uint8_t> &packet
)
1391 // Enable extended mode. In extended mode, the remote server is made
1392 // persistent. The ‘R’ packet is used to restart the program being debugged.
1394 extended_mode
= true;
1397 void gdbserver_t::handle_breakpoint(const std::vector
<uint8_t> &packet
)
1399 // insert: Z type,addr,kind
1400 // remove: z type,addr,kind
1402 software_breakpoint_t bp
;
1403 bool insert
= (packet
[1] == 'Z');
1404 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1405 int type
= consume_hex_number(iter
, packet
.end());
1407 return send_packet("E50");
1409 bp
.address
= consume_hex_number(iter
, packet
.end());
1411 return send_packet("E51");
1413 bp
.size
= consume_hex_number(iter
, packet
.end());
1414 // There may be more options after a ; here, but we don't support that.
1416 return send_packet("E52");
1418 if (bp
.size
!= 2 && bp
.size
!= 4) {
1419 return send_packet("E53");
1422 fence_i_required
= true;
1423 add_operation(new collect_translation_info_op_t(*this, bp
.address
, bp
.size
));
1425 unsigned char* swbp
= new unsigned char[4];
1427 swbp
[0] = C_EBREAK
& 0xff;
1428 swbp
[1] = (C_EBREAK
>> 8) & 0xff;
1430 swbp
[0] = EBREAK
& 0xff;
1431 swbp
[1] = (EBREAK
>> 8) & 0xff;
1432 swbp
[2] = (EBREAK
>> 16) & 0xff;
1433 swbp
[3] = (EBREAK
>> 24) & 0xff;
1436 breakpoints
[bp
.address
] = new software_breakpoint_t(bp
);
1437 add_operation(new memory_read_op_t(*this, bp
.address
, bp
.size
,
1438 breakpoints
[bp
.address
]->instruction
));
1439 add_operation(new memory_write_op_t(*this, bp
.address
, bp
.size
, swbp
));
1442 software_breakpoint_t
*found_bp
;
1443 found_bp
= breakpoints
[bp
.address
];
1444 unsigned char* instruction
= new unsigned char[4];
1445 memcpy(instruction
, found_bp
->instruction
, 4);
1446 add_operation(new memory_write_op_t(*this, found_bp
->address
,
1447 found_bp
->size
, instruction
));
1448 breakpoints
.erase(bp
.address
);
1452 return send_packet("OK");
1455 void gdbserver_t::handle_query(const std::vector
<uint8_t> &packet
)
1458 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1460 consume_string(name
, iter
, packet
.end(), ':');
1461 if (iter
!= packet
.end())
1463 if (name
== "Supported") {
1465 while (iter
!= packet
.end()) {
1466 std::string feature
;
1467 consume_string(feature
, iter
, packet
.end(), ';');
1468 if (iter
!= packet
.end())
1470 if (feature
== "swbreak+") {
1474 return end_packet();
1477 fprintf(stderr
, "Unsupported query %s\n", name
.c_str());
1478 return send_packet("");
1481 void gdbserver_t::handle_packet(const std::vector
<uint8_t> &packet
)
1483 if (compute_checksum(packet
) != extract_checksum(packet
)) {
1484 fprintf(stderr
, "Received %ld-byte packet with invalid checksum\n", packet
.size());
1485 fprintf(stderr
, "Computed checksum: %x\n", compute_checksum(packet
));
1486 print_packet(packet
);
1491 fprintf(stderr
, "Received %ld-byte packet from debug client: ", packet
.size());
1492 print_packet(packet
);
1495 switch (packet
[1]) {
1497 return handle_extended(packet
);
1499 return handle_halt_reason(packet
);
1501 return handle_general_registers_read(packet
);
1503 return handle_kill(packet
);
1505 return handle_memory_read(packet
);
1507 // return handle_memory_write(packet);
1509 return handle_memory_binary_write(packet
);
1511 return handle_register_read(packet
);
1513 return handle_register_write(packet
);
1515 return handle_continue(packet
);
1517 return handle_step(packet
);
1520 return handle_breakpoint(packet
);
1523 return handle_query(packet
);
1527 fprintf(stderr
, "** Unsupported packet: ");
1528 print_packet(packet
);
1532 void gdbserver_t::handle_interrupt()
1534 processor_t
*p
= sim
->get_core(0);
1535 add_operation(new halt_op_t(*this, true));
1538 void gdbserver_t::handle()
1540 if (client_fd
> 0) {
1541 processor_t
*p
= sim
->get_core(0);
1543 bool interrupt
= sim
->debug_module
.get_interrupt(0);
1545 if (!interrupt
&& !operation_queue
.empty()) {
1546 operation_t
*operation
= operation_queue
.front();
1547 if (operation
->step()) {
1548 operation_queue
.pop();
1553 bool halt_notification
= sim
->debug_module
.get_halt_notification(0);
1554 if (halt_notification
) {
1555 sim
->debug_module
.clear_halt_notification(0);
1556 add_operation(new halt_op_t(*this, true));
1566 if (operation_queue
.empty()) {
1567 this->process_requests();
1571 void gdbserver_t::send(const char* msg
)
1573 unsigned int length
= strlen(msg
);
1574 for (const char *c
= msg
; *c
; c
++)
1575 running_checksum
+= *c
;
1576 send_buf
.append((const uint8_t *) msg
, length
);
1579 void gdbserver_t::send(uint64_t value
)
1582 for (unsigned int i
= 0; i
< 8; i
++) {
1583 sprintf(buffer
, "%02x", (int) (value
& 0xff));
1589 void gdbserver_t::send(uint32_t value
)
1592 for (unsigned int i
= 0; i
< 4; i
++) {
1593 sprintf(buffer
, "%02x", (int) (value
& 0xff));
1599 void gdbserver_t::send_packet(const char* data
)
1607 void gdbserver_t::start_packet()
1610 running_checksum
= 0;
1613 void gdbserver_t::end_packet(const char* data
)
1619 char checksum_string
[4];
1620 sprintf(checksum_string
, "#%02x", running_checksum
);
1621 send(checksum_string
);