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 ori(unsigned int dest
, unsigned int src
, uint16_t imm
)
190 return (bits(imm
, 11, 0) << 20) |
196 static uint32_t nop()
198 return addi(0, 0, 0);
201 template <typename T
>
202 unsigned int circular_buffer_t
<T
>::size() const
207 return end
+ capacity
- start
;
210 template <typename T
>
211 void circular_buffer_t
<T
>::consume(unsigned int bytes
)
213 start
= (start
+ bytes
) % capacity
;
216 template <typename T
>
217 unsigned int circular_buffer_t
<T
>::contiguous_empty_size() const
221 return capacity
- end
- 1;
223 return capacity
- end
;
225 return start
- end
- 1;
228 template <typename T
>
229 unsigned int circular_buffer_t
<T
>::contiguous_data_size() const
234 return capacity
- start
;
237 template <typename T
>
238 void circular_buffer_t
<T
>::data_added(unsigned int bytes
)
241 assert(end
<= capacity
);
246 template <typename T
>
247 void circular_buffer_t
<T
>::reset()
253 template <typename T
>
254 void circular_buffer_t
<T
>::append(const T
*src
, unsigned int count
)
256 unsigned int copy
= std::min(count
, contiguous_empty_size());
257 memcpy(contiguous_empty(), src
, copy
* sizeof(T
));
261 assert(count
< contiguous_empty_size());
262 memcpy(contiguous_empty(), src
, count
* sizeof(T
));
267 ////////////////////////////// Debug Operations
269 class halt_op_t
: public operation_t
272 halt_op_t(gdbserver_t
& gdbserver
, bool send_status
=false) :
273 operation_t(gdbserver
), send_status(send_status
) {};
275 bool perform_step(unsigned int step
) {
278 // TODO: For now we just assume the target is 64-bit.
279 gs
.write_debug_ram(0, csrsi(CSR_DCSR
, DCSR_HALT
));
280 gs
.write_debug_ram(1, csrr(S0
, CSR_DPC
));
281 gs
.write_debug_ram(2, sd(S0
, 0, (uint16_t) DEBUG_RAM_START
));
282 gs
.write_debug_ram(3, csrr(S0
, CSR_MSTATUS
));
283 gs
.write_debug_ram(4, sd(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 8));
284 gs
.write_debug_ram(5, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*5))));
286 // We could read more registers here, but only on 64-bit targets. I'm
287 // trying to keep The patterns here usable for 32-bit ISAs as well.
291 gs
.dpc
= ((uint64_t) gs
.read_debug_ram(1) << 32) | gs
.read_debug_ram(0);
292 gs
.mstatus
= ((uint64_t) gs
.read_debug_ram(3) << 32) | gs
.read_debug_ram(2);
293 gs
.write_debug_ram(0, csrr(S0
, CSR_DCSR
));
294 gs
.write_debug_ram(1, sd(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
295 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*6))));
300 gs
.dcsr
= ((uint64_t) gs
.read_debug_ram(5) << 32) | gs
.read_debug_ram(4);
302 gs
.sptbr_valid
= false;
303 gs
.pte_cache
.clear();
306 switch (get_field(gs
.dcsr
, DCSR_CAUSE
)) {
307 case DCSR_CAUSE_NONE
:
308 fprintf(stderr
, "Internal error. Processor halted without reason.\n");
311 case DCSR_CAUSE_DEBUGINT
:
312 gs
.send_packet("S02"); // Pretend program received SIGINT.
315 case DCSR_CAUSE_HWBP
:
316 case DCSR_CAUSE_STEP
:
317 case DCSR_CAUSE_HALT
:
318 // There's no gdb code for this.
319 gs
.send_packet("T05");
321 case DCSR_CAUSE_SWBP
:
322 gs
.send_packet("T05swbreak:;");
336 class continue_op_t
: public operation_t
339 continue_op_t(gdbserver_t
& gdbserver
, bool single_step
) :
340 operation_t(gdbserver
), single_step(single_step
) {};
342 bool perform_step(unsigned int step
) {
345 gs
.write_debug_ram(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+16));
346 gs
.write_debug_ram(1, csrw(S0
, CSR_DPC
));
347 if (gs
.fence_i_required
) {
348 gs
.write_debug_ram(2, fence_i());
349 gs
.write_debug_ram(3, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*3))));
350 gs
.fence_i_required
= false;
352 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
354 gs
.write_debug_ram(4, gs
.dpc
);
355 gs
.write_debug_ram(5, gs
.dpc
>> 32);
360 gs
.write_debug_ram(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+16));
361 gs
.write_debug_ram(1, csrw(S0
, CSR_MSTATUS
));
362 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
363 gs
.write_debug_ram(4, gs
.mstatus
);
364 gs
.write_debug_ram(5, gs
.mstatus
>> 32);
369 gs
.write_debug_ram(0, lw(S0
, 0, (uint16_t) DEBUG_RAM_START
+16));
370 gs
.write_debug_ram(1, csrw(S0
, CSR_DCSR
));
371 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
373 reg_t dcsr
= set_field(gs
.dcsr
, DCSR_HALT
, 0);
374 dcsr
= set_field(dcsr
, DCSR_STEP
, single_step
);
375 // Software breakpoints should go here.
376 dcsr
= set_field(dcsr
, DCSR_EBREAKM
, 1);
377 dcsr
= set_field(dcsr
, DCSR_EBREAKH
, 1);
378 dcsr
= set_field(dcsr
, DCSR_EBREAKS
, 1);
379 dcsr
= set_field(dcsr
, DCSR_EBREAKU
, 1);
380 gs
.write_debug_ram(4, dcsr
);
392 class general_registers_read_op_t
: public operation_t
394 // Register order that gdb expects is:
395 // "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
396 // "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
397 // "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
398 // "x24", "x25", "x26", "x27", "x28", "x29", "x30", "x31",
400 // Each byte of register data is described by two hex digits. The bytes with
401 // the register are transmitted in target byte order. The size of each
402 // register and their position within the ‘g’ packet are determined by the
403 // gdb internal gdbarch functions DEPRECATED_REGISTER_RAW_SIZE and
404 // gdbarch_register_name.
407 general_registers_read_op_t(gdbserver_t
& gdbserver
) :
408 operation_t(gdbserver
) {};
410 bool perform_step(unsigned int step
)
415 // x0 is always zero.
418 gs
.write_debug_ram(0, sd(1, 0, (uint16_t) DEBUG_RAM_START
+ 16));
419 gs
.write_debug_ram(1, sd(2, 0, (uint16_t) DEBUG_RAM_START
+ 0));
420 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
425 gs
.send(((uint64_t) gs
.read_debug_ram(5) << 32) | gs
.read_debug_ram(4));
431 gs
.send(((uint64_t) gs
.read_debug_ram(1) << 32) | gs
.read_debug_ram(0));
433 unsigned int current_reg
= 2 * step
+ 1;
435 if (current_reg
== S1
) {
436 gs
.write_debug_ram(i
++, ld(S1
, 0, (uint16_t) DEBUG_RAM_END
- 8));
438 gs
.write_debug_ram(i
++, sd(current_reg
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
439 if (current_reg
+ 1 == S0
) {
440 gs
.write_debug_ram(i
++, csrr(S0
, CSR_DSCRATCH
));
442 gs
.write_debug_ram(i
++, sd(current_reg
+1, 0, (uint16_t) DEBUG_RAM_START
+ 0));
443 gs
.write_debug_ram(i
, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*i
))));
450 class register_read_op_t
: public operation_t
453 register_read_op_t(gdbserver_t
& gdbserver
, unsigned int reg
) :
454 operation_t(gdbserver
), reg(reg
) {};
456 bool perform_step(unsigned int step
)
460 if (reg
>= REG_XPR0
&& reg
<= REG_XPR31
) {
461 die("handle_register_read");
462 // send(p->state.XPR[reg - REG_XPR0]);
463 } else if (reg
== REG_PC
) {
468 } else if (reg
>= REG_FPR0
&& reg
<= REG_FPR31
) {
469 // send(p->state.FPR[reg - REG_FPR0]);
470 gs
.write_debug_ram(0, fsd(reg
- REG_FPR0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
471 gs
.write_debug_ram(1, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*1))));
472 } else if (reg
>= REG_CSR0
&& reg
<= REG_CSR4095
) {
473 gs
.write_debug_ram(0, csrr(S0
, reg
- REG_CSR0
));
474 gs
.write_debug_ram(1, sd(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
475 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
476 // If we hit an exception reading the CSR, we'll end up returning ~0 as
477 // the register's value, which is what we want. (Right?)
478 gs
.write_debug_ram(4, 0xffffffff);
479 gs
.write_debug_ram(5, 0xffffffff);
481 gs
.send_packet("E02");
489 gs
.send(((uint64_t) gs
.read_debug_ram(5) << 32) | gs
.read_debug_ram(4));
500 class register_write_op_t
: public operation_t
503 register_write_op_t(gdbserver_t
& gdbserver
, unsigned int reg
, reg_t value
) :
504 operation_t(gdbserver
), reg(reg
), value(value
) {};
506 bool perform_step(unsigned int step
)
508 gs
.write_debug_ram(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
509 gs
.write_debug_ram(4, value
);
510 gs
.write_debug_ram(5, value
>> 32);
512 gs
.write_debug_ram(1, csrw(S0
, CSR_DSCRATCH
));
513 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
514 } else if (reg
== S1
) {
515 gs
.write_debug_ram(1, sd(S0
, 0, (uint16_t) DEBUG_RAM_END
- 8));
516 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
517 } else if (reg
>= REG_XPR0
&& reg
<= REG_XPR31
) {
518 gs
.write_debug_ram(1, addi(reg
, S0
, 0));
519 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
520 } else if (reg
== REG_PC
) {
523 } else if (reg
>= REG_FPR0
&& reg
<= REG_FPR31
) {
524 // send(p->state.FPR[reg - REG_FPR0]);
525 gs
.write_debug_ram(0, fld(reg
- REG_FPR0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
526 gs
.write_debug_ram(1, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*1))));
527 } else if (reg
>= REG_CSR0
&& reg
<= REG_CSR4095
) {
528 gs
.write_debug_ram(1, csrw(S0
, reg
- REG_CSR0
));
529 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
530 if (reg
== REG_CSR0
+ CSR_SPTBR
) {
532 gs
.sptbr_valid
= true;
535 gs
.send_packet("E02");
539 gs
.send_packet("OK");
548 class memory_read_op_t
: public operation_t
551 // Read length bytes from vaddr, storing the result into data.
552 // If data is NULL, send the result straight to gdb.
553 memory_read_op_t(gdbserver_t
& gdbserver
, reg_t vaddr
, unsigned int length
,
554 unsigned char *data
=NULL
) :
555 operation_t(gdbserver
), vaddr(vaddr
), length(length
), data(data
) {};
557 bool perform_step(unsigned int step
)
560 // address goes in S0
561 paddr
= gs
.translate(vaddr
);
562 access_size
= (paddr
% length
);
563 if (access_size
== 0)
564 access_size
= length
;
568 gs
.write_debug_ram(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
569 switch (access_size
) {
571 gs
.write_debug_ram(1, lb(S1
, S0
, 0));
574 gs
.write_debug_ram(1, lh(S1
, S0
, 0));
577 gs
.write_debug_ram(1, lw(S1
, S0
, 0));
580 gs
.write_debug_ram(1, ld(S1
, S0
, 0));
583 gs
.write_debug_ram(2, sd(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 24));
584 gs
.write_debug_ram(3, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*3))));
585 gs
.write_debug_ram(4, paddr
);
586 gs
.write_debug_ram(5, paddr
>> 32);
596 reg_t value
= ((uint64_t) gs
.read_debug_ram(7) << 32) | gs
.read_debug_ram(6);
597 for (unsigned int i
= 0; i
< access_size
; i
++) {
599 *(data
++) = value
& 0xff;
600 fprintf(stderr
, "%02x", (unsigned int) (value
& 0xff));
602 sprintf(buffer
, "%02x", (unsigned int) (value
& 0xff));
608 fprintf(stderr
, "\n");
609 length
-= access_size
;
610 paddr
+= access_size
;
618 gs
.write_debug_ram(4, paddr
);
619 gs
.write_debug_ram(5, paddr
>> 32);
630 unsigned int access_size
;
633 class memory_write_op_t
: public operation_t
636 memory_write_op_t(gdbserver_t
& gdbserver
, reg_t vaddr
, unsigned int length
,
637 const unsigned char *data
) :
638 operation_t(gdbserver
), vaddr(vaddr
), offset(0), length(length
), data(data
) {};
640 ~memory_write_op_t() {
644 bool perform_step(unsigned int step
)
646 reg_t paddr
= gs
.translate(vaddr
);
648 // address goes in S0
649 access_size
= (paddr
% length
);
650 if (access_size
== 0)
651 access_size
= length
;
655 fprintf(stderr
, "write to 0x%lx -> 0x%lx: ", vaddr
, paddr
);
656 for (unsigned int i
= 0; i
< length
; i
++)
657 fprintf(stderr
, "%02x", data
[i
]);
658 fprintf(stderr
, "\n");
660 gs
.write_debug_ram(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
661 switch (access_size
) {
663 gs
.write_debug_ram(1, lb(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 24));
664 gs
.write_debug_ram(2, sb(S1
, S0
, 0));
665 gs
.write_debug_ram(6, data
[0]);
668 gs
.write_debug_ram(1, lh(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 24));
669 gs
.write_debug_ram(2, sh(S1
, S0
, 0));
670 gs
.write_debug_ram(6, data
[0] | (data
[1] << 8));
673 gs
.write_debug_ram(1, lw(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 24));
674 gs
.write_debug_ram(2, sw(S1
, S0
, 0));
675 gs
.write_debug_ram(6, data
[0] | (data
[1] << 8) |
676 (data
[2] << 16) | (data
[3] << 24));
679 gs
.write_debug_ram(1, ld(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 24));
680 gs
.write_debug_ram(2, sd(S1
, S0
, 0));
681 gs
.write_debug_ram(6, data
[0] | (data
[1] << 8) |
682 (data
[2] << 16) | (data
[3] << 24));
683 gs
.write_debug_ram(7, data
[4] | (data
[5] << 8) |
684 (data
[6] << 16) | (data
[7] << 24));
687 gs
.send_packet("E12");
690 gs
.write_debug_ram(3, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*3))));
691 gs
.write_debug_ram(4, paddr
);
692 gs
.write_debug_ram(5, paddr
>> 32);
698 if (gs
.read_debug_ram(DEBUG_RAM_SIZE
/ 4 - 1)) {
699 fprintf(stderr
, "Exception happened while writing to 0x%lx -> 0x%lx\n",
703 offset
+= access_size
;
704 if (offset
>= length
) {
705 gs
.send_packet("OK");
708 const unsigned char *d
= data
+ offset
;
709 switch (access_size
) {
711 gs
.write_debug_ram(6, d
[0]);
714 gs
.write_debug_ram(6, d
[0] | (d
[1] << 8));
717 gs
.write_debug_ram(6, d
[0] | (d
[1] << 8) |
718 (d
[2] << 16) | (d
[3] << 24));
721 gs
.write_debug_ram(6, d
[0] | (d
[1] << 8) |
722 (d
[2] << 16) | (d
[3] << 24));
723 gs
.write_debug_ram(7, d
[4] | (d
[5] << 8) |
724 (d
[6] << 16) | (d
[7] << 24));
727 gs
.send_packet("E12");
730 gs
.write_debug_ram(4, paddr
+ offset
);
731 gs
.write_debug_ram(5, (paddr
+ offset
) >> 32);
741 unsigned int access_size
;
742 const unsigned char *data
;
745 class collect_translation_info_op_t
: public operation_t
748 // Read sufficient information from the target into gdbserver structures so
749 // that it's possible to translate vaddr, vaddr+length, and all addresses
750 // in between to physical addresses.
751 collect_translation_info_op_t(gdbserver_t
& gdbserver
, reg_t vaddr
, size_t length
) :
752 operation_t(gdbserver
), state(STATE_START
), vaddr(vaddr
), length(length
) {};
754 bool perform_step(unsigned int step
)
756 unsigned int vm
= gs
.virtual_memory();
761 // Nothing to be done.
783 sprintf(buf
, "VM mode %d is not supported by gdbserver.cc.", vm
);
785 return true; // die doesn't return, but gcc doesn't know that.
790 // Perform any reads from the just-completed action.
794 case STATE_READ_SPTBR
:
795 gs
.sptbr
= ((uint64_t) gs
.read_debug_ram(5) << 32) | gs
.read_debug_ram(4);
796 gs
.sptbr_valid
= true;
799 gs
.pte_cache
[pte_addr
] = ((uint64_t) gs
.read_debug_ram(5) << 32) |
800 gs
.read_debug_ram(4);
801 fprintf(stderr
, "pte_cache[0x%lx] = 0x%lx\n", pte_addr
, gs
.pte_cache
[pte_addr
]);
805 // Set up the next action.
806 // We only get here for VM_SV32/39/38.
808 if (!gs
.sptbr_valid
) {
809 state
= STATE_READ_SPTBR
;
810 gs
.write_debug_ram(0, csrr(S0
, CSR_SPTBR
));
811 gs
.write_debug_ram(1, sd(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
812 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
817 reg_t base
= gs
.sptbr
<< PGSHIFT
;
818 int ptshift
= (levels
- 1) * ptidxbits
;
819 for (unsigned int i
= 0; i
< levels
; i
++, ptshift
-= ptidxbits
) {
820 reg_t idx
= (vaddr
>> (PGSHIFT
+ ptshift
)) & ((1 << ptidxbits
) - 1);
822 pte_addr
= base
+ idx
* ptesize
;
823 auto it
= gs
.pte_cache
.find(pte_addr
);
824 if (it
== gs
.pte_cache
.end()) {
825 state
= STATE_READ_PTE
;
827 gs
.write_debug_ram(0, lw(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
828 gs
.write_debug_ram(1, lw(S1
, S0
, 0));
829 gs
.write_debug_ram(2, sd(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
831 gs
.write_debug_ram(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
832 gs
.write_debug_ram(1, ld(S1
, S0
, 0));
833 gs
.write_debug_ram(2, sd(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
835 gs
.write_debug_ram(3, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*3))));
836 gs
.write_debug_ram(4, pte_addr
);
837 gs
.write_debug_ram(5, pte_addr
>> 32);
842 reg_t pte
= gs
.pte_cache
[pte_addr
];
843 reg_t ppn
= pte
>> PTE_PPN_SHIFT
;
845 if (PTE_TABLE(pte
)) { // next level of page table
846 base
= ppn
<< PGSHIFT
;
848 // We've collected all the data required for the translation.
853 "ERROR: gdbserver couldn't find appropriate PTEs to translate 0x%lx\n",
867 unsigned int ptidxbits
;
868 unsigned int ptesize
;
872 ////////////////////////////// gdbserver itself
874 gdbserver_t::gdbserver_t(uint16_t port
, sim_t
*sim
) :
877 recv_buf(64 * 1024), send_buf(64 * 1024)
879 socket_fd
= socket(AF_INET
, SOCK_STREAM
, 0);
880 if (socket_fd
== -1) {
881 fprintf(stderr
, "failed to make socket: %s (%d)\n", strerror(errno
), errno
);
885 fcntl(socket_fd
, F_SETFL
, O_NONBLOCK
);
887 if (setsockopt(socket_fd
, SOL_SOCKET
, SO_REUSEADDR
, &reuseaddr
,
888 sizeof(int)) == -1) {
889 fprintf(stderr
, "failed setsockopt: %s (%d)\n", strerror(errno
), errno
);
893 struct sockaddr_in addr
;
894 memset(&addr
, 0, sizeof(addr
));
895 addr
.sin_family
= AF_INET
;
896 addr
.sin_addr
.s_addr
= INADDR_ANY
;
897 addr
.sin_port
= htons(port
);
899 if (bind(socket_fd
, (struct sockaddr
*) &addr
, sizeof(addr
)) == -1) {
900 fprintf(stderr
, "failed to bind socket: %s (%d)\n", strerror(errno
), errno
);
904 if (listen(socket_fd
, 1) == -1) {
905 fprintf(stderr
, "failed to listen on socket: %s (%d)\n", strerror(errno
), errno
);
910 reg_t
gdbserver_t::translate(reg_t vaddr
)
912 unsigned int vm
= virtual_memory();
913 unsigned int levels
, ptidxbits
, ptesize
;
938 sprintf(buf
, "VM mode %d is not supported by gdbserver.cc.", vm
);
940 return true; // die doesn't return, but gcc doesn't know that.
944 // Handle page tables here. There's a bunch of duplicated code with
945 // collect_translation_info_op_t. :-(
946 reg_t base
= sptbr
<< PGSHIFT
;
947 int ptshift
= (levels
- 1) * ptidxbits
;
948 for (unsigned int i
= 0; i
< levels
; i
++, ptshift
-= ptidxbits
) {
949 reg_t idx
= (vaddr
>> (PGSHIFT
+ ptshift
)) & ((1 << ptidxbits
) - 1);
951 reg_t pte_addr
= base
+ idx
* ptesize
;
952 auto it
= pte_cache
.find(pte_addr
);
953 if (it
== pte_cache
.end()) {
954 fprintf(stderr
, "ERROR: gdbserver tried to translate 0x%lx without first "
955 "collecting the relevant PTEs.\n", vaddr
);
956 die("gdbserver_t::translate()");
959 reg_t pte
= pte_cache
[pte_addr
];
960 reg_t ppn
= pte
>> PTE_PPN_SHIFT
;
962 if (PTE_TABLE(pte
)) { // next level of page table
963 base
= ppn
<< PGSHIFT
;
965 // We've collected all the data required for the translation.
966 reg_t vpn
= vaddr
>> PGSHIFT
;
967 reg_t paddr
= (ppn
| (vpn
& ((reg_t(1) << ptshift
) - 1))) << PGSHIFT
;
968 paddr
+= vaddr
& (PGSIZE
-1);
969 fprintf(stderr
, "gdbserver translate 0x%lx -> 0x%lx\n", vaddr
, paddr
);
974 fprintf(stderr
, "ERROR: gdbserver tried to translate 0x%lx but the relevant "
975 "PTEs are invalid.\n", vaddr
);
976 // TODO: Is it better to throw an exception here?
980 unsigned int gdbserver_t::privilege_mode()
982 unsigned int mode
= get_field(dcsr
, DCSR_PRV
);
983 if (get_field(mstatus
, MSTATUS_MPRV
))
984 mode
= get_field(mstatus
, MSTATUS_MPP
);
988 unsigned int gdbserver_t::virtual_memory()
990 unsigned int mode
= privilege_mode();
993 return get_field(mstatus
, MSTATUS_VM
);
996 void gdbserver_t::write_debug_ram(unsigned int index
, uint32_t value
)
998 sim
->debug_module
.ram_write32(index
, value
);
1001 uint32_t gdbserver_t::read_debug_ram(unsigned int index
)
1003 return sim
->debug_module
.ram_read32(index
);
1006 void gdbserver_t::add_operation(operation_t
* operation
)
1008 operation_queue
.push(operation
);
1011 void gdbserver_t::accept()
1013 client_fd
= ::accept(socket_fd
, NULL
, NULL
);
1014 if (client_fd
== -1) {
1015 if (errno
== EAGAIN
) {
1016 // No client waiting to connect right now.
1018 fprintf(stderr
, "failed to accept on socket: %s (%d)\n", strerror(errno
),
1023 fcntl(client_fd
, F_SETFL
, O_NONBLOCK
);
1026 extended_mode
= false;
1028 // gdb wants the core to be halted when it attaches.
1029 add_operation(new halt_op_t(*this));
1033 void gdbserver_t::read()
1035 // Reading from a non-blocking socket still blocks if there is no data
1038 size_t count
= recv_buf
.contiguous_empty_size();
1040 ssize_t bytes
= ::read(client_fd
, recv_buf
.contiguous_empty(), count
);
1042 if (errno
== EAGAIN
) {
1043 // We'll try again the next call.
1045 fprintf(stderr
, "failed to read on socket: %s (%d)\n", strerror(errno
), errno
);
1048 } else if (bytes
== 0) {
1049 // The remote disconnected.
1051 processor_t
*p
= sim
->get_core(0);
1052 // TODO p->set_halted(false, HR_NONE);
1056 recv_buf
.data_added(bytes
);
1060 void gdbserver_t::write()
1062 if (send_buf
.empty())
1065 while (!send_buf
.empty()) {
1066 unsigned int count
= send_buf
.contiguous_data_size();
1068 ssize_t bytes
= ::write(client_fd
, send_buf
.contiguous_data(), count
);
1070 fprintf(stderr
, "failed to write to socket: %s (%d)\n", strerror(errno
), errno
);
1072 } else if (bytes
== 0) {
1073 // Client can't take any more data right now.
1076 fprintf(stderr
, "wrote %ld bytes: ", bytes
);
1077 for (unsigned int i
= 0; i
< bytes
; i
++) {
1078 fprintf(stderr
, "%c", send_buf
[i
]);
1080 fprintf(stderr
, "\n");
1081 send_buf
.consume(bytes
);
1086 void print_packet(const std::vector
<uint8_t> &packet
)
1088 for (uint8_t c
: packet
) {
1089 if (c
>= ' ' and c
<= '~')
1090 fprintf(stderr
, "%c", c
);
1092 fprintf(stderr
, "\\x%02x", c
);
1094 fprintf(stderr
, "\n");
1097 uint8_t compute_checksum(const std::vector
<uint8_t> &packet
)
1099 uint8_t checksum
= 0;
1100 for (auto i
= packet
.begin() + 1; i
!= packet
.end() - 3; i
++ ) {
1106 uint8_t character_hex_value(uint8_t character
)
1108 if (character
>= '0' && character
<= '9')
1109 return character
- '0';
1110 if (character
>= 'a' && character
<= 'f')
1111 return 10 + character
- 'a';
1112 if (character
>= 'A' && character
<= 'F')
1113 return 10 + character
- 'A';
1117 uint8_t extract_checksum(const std::vector
<uint8_t> &packet
)
1119 return character_hex_value(*(packet
.end() - 1)) +
1120 16 * character_hex_value(*(packet
.end() - 2));
1123 void gdbserver_t::process_requests()
1125 // See https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html
1127 while (!recv_buf
.empty()) {
1128 std::vector
<uint8_t> packet
;
1129 for (unsigned int i
= 0; i
< recv_buf
.size(); i
++) {
1130 uint8_t b
= recv_buf
[i
];
1132 if (packet
.empty() && expect_ack
&& b
== '+') {
1133 recv_buf
.consume(1);
1137 if (packet
.empty() && b
== 3) {
1138 fprintf(stderr
, "Received interrupt\n");
1139 recv_buf
.consume(1);
1145 // Start of new packet.
1146 if (!packet
.empty()) {
1147 fprintf(stderr
, "Received malformed %ld-byte packet from debug client: ",
1149 print_packet(packet
);
1150 recv_buf
.consume(i
);
1155 packet
.push_back(b
);
1157 // Packets consist of $<packet-data>#<checksum>
1158 // where <checksum> is
1159 if (packet
.size() >= 4 &&
1160 packet
[packet
.size()-3] == '#') {
1161 handle_packet(packet
);
1162 recv_buf
.consume(i
+1);
1166 // There's a partial packet in the buffer. Wait until we get more data to
1168 if (packet
.size()) {
1174 void gdbserver_t::handle_halt_reason(const std::vector
<uint8_t> &packet
)
1179 void gdbserver_t::handle_general_registers_read(const std::vector
<uint8_t> &packet
)
1181 add_operation(new general_registers_read_op_t(*this));
1184 void gdbserver_t::set_interrupt(uint32_t hartid
) {
1185 sim
->debug_module
.set_interrupt(hartid
);
1188 // First byte is the most-significant one.
1189 // Eg. "08675309" becomes 0x08675309.
1190 uint64_t consume_hex_number(std::vector
<uint8_t>::const_iterator
&iter
,
1191 std::vector
<uint8_t>::const_iterator end
)
1195 while (iter
!= end
) {
1197 uint64_t c_value
= character_hex_value(c
);
1207 // First byte is the least-significant one.
1208 // Eg. "08675309" becomes 0x09536708
1209 uint64_t consume_hex_number_le(std::vector
<uint8_t>::const_iterator
&iter
,
1210 std::vector
<uint8_t>::const_iterator end
)
1213 unsigned int shift
= 4;
1215 while (iter
!= end
) {
1217 uint64_t c_value
= character_hex_value(c
);
1221 value
|= c_value
<< shift
;
1222 if ((shift
% 8) == 0)
1230 void consume_string(std::string
&str
, std::vector
<uint8_t>::const_iterator
&iter
,
1231 std::vector
<uint8_t>::const_iterator end
, uint8_t separator
)
1233 while (iter
!= end
&& *iter
!= separator
) {
1234 str
.append(1, (char) *iter
);
1239 void gdbserver_t::handle_register_read(const std::vector
<uint8_t> &packet
)
1243 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1244 unsigned int n
= consume_hex_number(iter
, packet
.end());
1246 return send_packet("E01");
1248 add_operation(new register_read_op_t(*this, n
));
1251 void gdbserver_t::handle_register_write(const std::vector
<uint8_t> &packet
)
1255 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1256 unsigned int n
= consume_hex_number(iter
, packet
.end());
1258 return send_packet("E05");
1261 reg_t value
= consume_hex_number_le(iter
, packet
.end());
1263 return send_packet("E06");
1265 processor_t
*p
= sim
->get_core(0);
1267 add_operation(new register_write_op_t(*this, n
, value
));
1269 return send_packet("OK");
1272 void gdbserver_t::handle_memory_read(const std::vector
<uint8_t> &packet
)
1275 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1276 reg_t address
= consume_hex_number(iter
, packet
.end());
1278 return send_packet("E10");
1280 reg_t length
= consume_hex_number(iter
, packet
.end());
1282 return send_packet("E11");
1284 add_operation(new collect_translation_info_op_t(*this, address
, length
));
1285 add_operation(new memory_read_op_t(*this, address
, length
));
1288 void gdbserver_t::handle_memory_binary_write(const std::vector
<uint8_t> &packet
)
1290 // X addr,length:XX...
1291 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1292 reg_t address
= consume_hex_number(iter
, packet
.end());
1294 return send_packet("E20");
1296 reg_t length
= consume_hex_number(iter
, packet
.end());
1298 return send_packet("E21");
1302 return send_packet("OK");
1305 unsigned char *data
= new unsigned char[length
];
1306 for (unsigned int i
= 0; i
< length
; i
++) {
1307 if (iter
== packet
.end()) {
1308 return send_packet("E22");
1313 // The binary data representation uses 7d (ascii ‘}’) as an escape
1314 // character. Any escaped byte is transmitted as the escape character
1315 // followed by the original character XORed with 0x20. For example, the
1316 // byte 0x7d would be transmitted as the two bytes 0x7d 0x5d. The bytes
1317 // 0x23 (ascii ‘#’), 0x24 (ascii ‘$’), and 0x7d (ascii ‘}’) must always
1319 if (iter
== packet
.end()) {
1320 return send_packet("E23");
1328 return send_packet("E4b"); // EOVERFLOW
1330 add_operation(new collect_translation_info_op_t(*this, address
, length
));
1331 add_operation(new memory_write_op_t(*this, address
, length
, data
));
1334 void gdbserver_t::handle_continue(const std::vector
<uint8_t> &packet
)
1337 processor_t
*p
= sim
->get_core(0);
1338 if (packet
[2] != '#') {
1339 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1340 dpc
= consume_hex_number(iter
, packet
.end());
1342 return send_packet("E30");
1345 add_operation(new continue_op_t(*this, false));
1348 void gdbserver_t::handle_step(const std::vector
<uint8_t> &packet
)
1351 if (packet
[2] != '#') {
1352 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1354 //p->state.pc = consume_hex_number(iter, packet.end());
1356 return send_packet("E40");
1359 add_operation(new continue_op_t(*this, true));
1362 void gdbserver_t::handle_kill(const std::vector
<uint8_t> &packet
)
1365 // The exact effect of this packet is not specified.
1366 // Looks like OpenOCD disconnects?
1370 void gdbserver_t::handle_extended(const std::vector
<uint8_t> &packet
)
1372 // Enable extended mode. In extended mode, the remote server is made
1373 // persistent. The ‘R’ packet is used to restart the program being debugged.
1375 extended_mode
= true;
1378 void gdbserver_t::handle_breakpoint(const std::vector
<uint8_t> &packet
)
1380 // insert: Z type,addr,kind
1381 // remove: z type,addr,kind
1383 software_breakpoint_t bp
;
1384 bool insert
= (packet
[1] == 'Z');
1385 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1386 int type
= consume_hex_number(iter
, packet
.end());
1388 return send_packet("E50");
1390 bp
.address
= consume_hex_number(iter
, packet
.end());
1392 return send_packet("E51");
1394 bp
.size
= consume_hex_number(iter
, packet
.end());
1395 // There may be more options after a ; here, but we don't support that.
1397 return send_packet("E52");
1399 if (bp
.size
!= 2 && bp
.size
!= 4) {
1400 return send_packet("E53");
1403 fence_i_required
= true;
1404 add_operation(new collect_translation_info_op_t(*this, bp
.address
, bp
.size
));
1406 unsigned char* swbp
= new unsigned char[4];
1408 swbp
[0] = C_EBREAK
& 0xff;
1409 swbp
[1] = (C_EBREAK
>> 8) & 0xff;
1411 swbp
[0] = EBREAK
& 0xff;
1412 swbp
[1] = (EBREAK
>> 8) & 0xff;
1413 swbp
[2] = (EBREAK
>> 16) & 0xff;
1414 swbp
[3] = (EBREAK
>> 24) & 0xff;
1417 breakpoints
[bp
.address
] = new software_breakpoint_t(bp
);
1418 add_operation(new memory_read_op_t(*this, bp
.address
, bp
.size
,
1419 breakpoints
[bp
.address
]->instruction
));
1420 add_operation(new memory_write_op_t(*this, bp
.address
, bp
.size
, swbp
));
1423 software_breakpoint_t
*found_bp
;
1424 found_bp
= breakpoints
[bp
.address
];
1425 unsigned char* instruction
= new unsigned char[4];
1426 memcpy(instruction
, found_bp
->instruction
, 4);
1427 add_operation(new memory_write_op_t(*this, found_bp
->address
,
1428 found_bp
->size
, instruction
));
1429 breakpoints
.erase(bp
.address
);
1433 return send_packet("OK");
1436 void gdbserver_t::handle_query(const std::vector
<uint8_t> &packet
)
1439 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1441 consume_string(name
, iter
, packet
.end(), ':');
1442 if (iter
!= packet
.end())
1444 if (name
== "Supported") {
1446 while (iter
!= packet
.end()) {
1447 std::string feature
;
1448 consume_string(feature
, iter
, packet
.end(), ';');
1449 if (iter
!= packet
.end())
1451 if (feature
== "swbreak+") {
1455 return end_packet();
1458 fprintf(stderr
, "Unsupported query %s\n", name
.c_str());
1459 return send_packet("");
1462 void gdbserver_t::handle_packet(const std::vector
<uint8_t> &packet
)
1464 if (compute_checksum(packet
) != extract_checksum(packet
)) {
1465 fprintf(stderr
, "Received %ld-byte packet with invalid checksum\n", packet
.size());
1466 fprintf(stderr
, "Computed checksum: %x\n", compute_checksum(packet
));
1467 print_packet(packet
);
1472 fprintf(stderr
, "Received %ld-byte packet from debug client: ", packet
.size());
1473 print_packet(packet
);
1476 switch (packet
[1]) {
1478 return handle_extended(packet
);
1480 return handle_halt_reason(packet
);
1482 return handle_general_registers_read(packet
);
1484 // return handle_kill(packet);
1486 return handle_memory_read(packet
);
1488 // return handle_memory_write(packet);
1490 return handle_memory_binary_write(packet
);
1492 return handle_register_read(packet
);
1494 return handle_register_write(packet
);
1496 return handle_continue(packet
);
1498 return handle_step(packet
);
1501 return handle_breakpoint(packet
);
1504 return handle_query(packet
);
1508 fprintf(stderr
, "** Unsupported packet: ");
1509 print_packet(packet
);
1513 void gdbserver_t::handle_interrupt()
1515 processor_t
*p
= sim
->get_core(0);
1516 add_operation(new halt_op_t(*this, true));
1519 void gdbserver_t::handle()
1521 if (client_fd
> 0) {
1522 processor_t
*p
= sim
->get_core(0);
1524 bool interrupt
= sim
->debug_module
.get_interrupt(0);
1526 if (!interrupt
&& !operation_queue
.empty()) {
1527 operation_t
*operation
= operation_queue
.front();
1528 if (operation
->step()) {
1529 operation_queue
.pop();
1534 bool halt_notification
= sim
->debug_module
.get_halt_notification(0);
1535 if (halt_notification
) {
1536 sim
->debug_module
.clear_halt_notification(0);
1537 add_operation(new halt_op_t(*this, true));
1547 if (operation_queue
.empty()) {
1548 this->process_requests();
1552 void gdbserver_t::send(const char* msg
)
1554 unsigned int length
= strlen(msg
);
1555 for (const char *c
= msg
; *c
; c
++)
1556 running_checksum
+= *c
;
1557 send_buf
.append((const uint8_t *) msg
, length
);
1560 void gdbserver_t::send(uint64_t value
)
1563 for (unsigned int i
= 0; i
< 8; i
++) {
1564 sprintf(buffer
, "%02x", (int) (value
& 0xff));
1570 void gdbserver_t::send(uint32_t value
)
1573 for (unsigned int i
= 0; i
< 4; i
++) {
1574 sprintf(buffer
, "%02x", (int) (value
& 0xff));
1580 void gdbserver_t::send_packet(const char* data
)
1588 void gdbserver_t::start_packet()
1591 running_checksum
= 0;
1594 void gdbserver_t::end_packet(const char* data
)
1600 char checksum_string
[4];
1601 sprintf(checksum_string
, "#%02x", running_checksum
);
1602 send(checksum_string
);