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_MBADADDR
));
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 mcause here as well, but only on 64-bit targets. I'm
287 // trying to keep The patterns here usable for 32-bit ISAs as well. (On a
288 // 32-bit ISA 8 words are required, while the minimum Debug RAM size is 7
293 gs
.saved_dpc
= ((uint64_t) gs
.read_debug_ram(1) << 32) | gs
.read_debug_ram(0);
294 gs
.saved_mbadaddr
= ((uint64_t) gs
.read_debug_ram(3) << 32) | gs
.read_debug_ram(2);
296 gs
.write_debug_ram(0, csrr(S0
, CSR_MCAUSE
));
297 gs
.write_debug_ram(1, sd(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 0));
298 gs
.write_debug_ram(2, csrr(S0
, CSR_MSTATUS
));
299 gs
.write_debug_ram(3, sd(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 8));
300 gs
.write_debug_ram(4, csrr(S0
, CSR_DCSR
));
301 gs
.write_debug_ram(5, sd(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
302 gs
.write_debug_ram(6, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*6))));
307 gs
.saved_mcause
= ((uint64_t) gs
.read_debug_ram(1) << 32) | gs
.read_debug_ram(0);
308 gs
.saved_mstatus
= ((uint64_t) gs
.read_debug_ram(3) << 32) | gs
.read_debug_ram(2);
309 gs
.dcsr
= ((uint64_t) gs
.read_debug_ram(5) << 32) | gs
.read_debug_ram(4);
311 gs
.sptbr_valid
= false;
312 gs
.pte_cache
.clear();
315 switch (get_field(gs
.dcsr
, DCSR_CAUSE
)) {
316 case DCSR_CAUSE_NONE
:
317 fprintf(stderr
, "Internal error. Processor halted without reason.\n");
320 case DCSR_CAUSE_DEBUGINT
:
321 gs
.send_packet("S02"); // Pretend program received SIGINT.
324 case DCSR_CAUSE_HWBP
:
325 case DCSR_CAUSE_STEP
:
326 case DCSR_CAUSE_HALT
:
327 // There's no gdb code for this.
328 gs
.send_packet("T05");
330 case DCSR_CAUSE_SWBP
:
331 gs
.send_packet("T05swbreak:;");
345 class continue_op_t
: public operation_t
348 continue_op_t(gdbserver_t
& gdbserver
, bool single_step
) :
349 operation_t(gdbserver
), single_step(single_step
) {};
351 bool perform_step(unsigned int step
) {
354 gs
.write_debug_ram(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+16));
355 gs
.write_debug_ram(1, csrw(S0
, CSR_DPC
));
356 if (gs
.fence_i_required
) {
357 gs
.write_debug_ram(2, fence_i());
358 gs
.write_debug_ram(3, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*3))));
359 gs
.fence_i_required
= false;
361 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
363 gs
.write_debug_ram(4, gs
.saved_dpc
);
364 gs
.write_debug_ram(5, gs
.saved_dpc
>> 32);
369 gs
.write_debug_ram(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+16));
370 gs
.write_debug_ram(1, csrw(S0
, CSR_MBADADDR
));
371 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
372 gs
.write_debug_ram(4, gs
.saved_mbadaddr
);
373 gs
.write_debug_ram(5, gs
.saved_mbadaddr
>> 32);
378 gs
.write_debug_ram(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+16));
379 gs
.write_debug_ram(1, csrw(S0
, CSR_MSTATUS
));
380 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
381 gs
.write_debug_ram(4, gs
.saved_mstatus
);
382 gs
.write_debug_ram(5, gs
.saved_mstatus
>> 32);
387 gs
.write_debug_ram(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+24));
388 gs
.write_debug_ram(1, csrw(S0
, CSR_MCAUSE
));
389 gs
.write_debug_ram(2, lw(S0
, 0, (uint16_t) DEBUG_RAM_START
+20));
390 gs
.write_debug_ram(3, csrw(S0
, CSR_DCSR
));
391 gs
.write_debug_ram(4, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*4))));
393 reg_t dcsr
= set_field(gs
.dcsr
, DCSR_HALT
, 0);
394 dcsr
= set_field(dcsr
, DCSR_STEP
, single_step
);
395 // Software breakpoints should go here.
396 dcsr
= set_field(dcsr
, DCSR_EBREAKM
, 1);
397 dcsr
= set_field(dcsr
, DCSR_EBREAKH
, 1);
398 dcsr
= set_field(dcsr
, DCSR_EBREAKS
, 1);
399 dcsr
= set_field(dcsr
, DCSR_EBREAKU
, 1);
400 gs
.write_debug_ram(5, dcsr
);
402 gs
.write_debug_ram(6, gs
.saved_mcause
);
403 gs
.write_debug_ram(7, gs
.saved_mcause
>> 32);
414 class general_registers_read_op_t
: public operation_t
416 // Register order that gdb expects is:
417 // "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
418 // "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
419 // "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
420 // "x24", "x25", "x26", "x27", "x28", "x29", "x30", "x31",
422 // Each byte of register data is described by two hex digits. The bytes with
423 // the register are transmitted in target byte order. The size of each
424 // register and their position within the ‘g’ packet are determined by the
425 // gdb internal gdbarch functions DEPRECATED_REGISTER_RAW_SIZE and
426 // gdbarch_register_name.
429 general_registers_read_op_t(gdbserver_t
& gdbserver
) :
430 operation_t(gdbserver
) {};
432 bool perform_step(unsigned int step
)
437 // x0 is always zero.
440 gs
.write_debug_ram(0, sd(1, 0, (uint16_t) DEBUG_RAM_START
+ 16));
441 gs
.write_debug_ram(1, sd(2, 0, (uint16_t) DEBUG_RAM_START
+ 0));
442 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
447 gs
.send(((uint64_t) gs
.read_debug_ram(5) << 32) | gs
.read_debug_ram(4));
453 gs
.send(((uint64_t) gs
.read_debug_ram(1) << 32) | gs
.read_debug_ram(0));
455 unsigned int current_reg
= 2 * step
+ 1;
457 if (current_reg
== S1
) {
458 gs
.write_debug_ram(i
++, ld(S1
, 0, (uint16_t) DEBUG_RAM_END
- 8));
460 gs
.write_debug_ram(i
++, sd(current_reg
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
461 if (current_reg
+ 1 == S0
) {
462 gs
.write_debug_ram(i
++, csrr(S0
, CSR_DSCRATCH
));
464 gs
.write_debug_ram(i
++, sd(current_reg
+1, 0, (uint16_t) DEBUG_RAM_START
+ 0));
465 gs
.write_debug_ram(i
, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*i
))));
472 class register_read_op_t
: public operation_t
475 register_read_op_t(gdbserver_t
& gdbserver
, unsigned int reg
) :
476 operation_t(gdbserver
), reg(reg
) {};
478 bool perform_step(unsigned int step
)
482 if (reg
>= REG_XPR0
&& reg
<= REG_XPR31
) {
483 die("handle_register_read");
484 // send(p->state.XPR[reg - REG_XPR0]);
485 } else if (reg
== REG_PC
) {
487 gs
.send(gs
.saved_dpc
);
490 } else if (reg
>= REG_FPR0
&& reg
<= REG_FPR31
) {
491 // send(p->state.FPR[reg - REG_FPR0]);
492 gs
.write_debug_ram(0, fsd(reg
- REG_FPR0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
493 gs
.write_debug_ram(1, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*1))));
494 } else if (reg
== REG_CSR0
+ CSR_MBADADDR
) {
496 gs
.send(gs
.saved_mbadaddr
);
499 } else if (reg
== REG_CSR0
+ CSR_MCAUSE
) {
501 gs
.send(gs
.saved_mcause
);
504 } else if (reg
>= REG_CSR0
&& reg
<= REG_CSR4095
) {
505 gs
.write_debug_ram(0, csrr(S0
, reg
- REG_CSR0
));
506 gs
.write_debug_ram(1, sd(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
507 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
508 // If we hit an exception reading the CSR, we'll end up returning ~0 as
509 // the register's value, which is what we want. (Right?)
510 gs
.write_debug_ram(4, 0xffffffff);
511 gs
.write_debug_ram(5, 0xffffffff);
513 gs
.send_packet("E02");
521 gs
.send(((uint64_t) gs
.read_debug_ram(5) << 32) | gs
.read_debug_ram(4));
532 class register_write_op_t
: public operation_t
535 register_write_op_t(gdbserver_t
& gdbserver
, unsigned int reg
, reg_t value
) :
536 operation_t(gdbserver
), reg(reg
), value(value
) {};
538 bool perform_step(unsigned int step
)
540 gs
.write_debug_ram(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
541 gs
.write_debug_ram(4, value
);
542 gs
.write_debug_ram(5, value
>> 32);
544 gs
.write_debug_ram(1, csrw(S0
, CSR_DSCRATCH
));
545 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
546 } else if (reg
== S1
) {
547 gs
.write_debug_ram(1, sd(S0
, 0, (uint16_t) DEBUG_RAM_END
- 8));
548 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
549 } else if (reg
>= REG_XPR0
&& reg
<= REG_XPR31
) {
550 gs
.write_debug_ram(1, addi(reg
, S0
, 0));
551 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
552 } else if (reg
== REG_PC
) {
553 gs
.saved_dpc
= value
;
555 } else if (reg
>= REG_FPR0
&& reg
<= REG_FPR31
) {
556 // send(p->state.FPR[reg - REG_FPR0]);
557 gs
.write_debug_ram(0, fld(reg
- REG_FPR0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
558 gs
.write_debug_ram(1, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*1))));
559 } else if (reg
== REG_CSR0
+ CSR_MBADADDR
) {
560 gs
.saved_mbadaddr
= value
;
562 } else if (reg
== REG_CSR0
+ CSR_MCAUSE
) {
563 gs
.saved_mcause
= value
;
565 } else if (reg
>= REG_CSR0
&& reg
<= REG_CSR4095
) {
566 gs
.write_debug_ram(1, csrw(S0
, reg
- REG_CSR0
));
567 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
569 gs
.send_packet("E02");
573 gs
.send_packet("OK");
582 class memory_read_op_t
: public operation_t
585 // Read length bytes from vaddr, storing the result into data.
586 // If data is NULL, send the result straight to gdb.
587 memory_read_op_t(gdbserver_t
& gdbserver
, reg_t vaddr
, unsigned int length
,
588 unsigned char *data
=NULL
) :
589 operation_t(gdbserver
), vaddr(vaddr
), length(length
), data(data
) {};
591 bool perform_step(unsigned int step
)
594 // address goes in S0
595 paddr
= gs
.translate(vaddr
);
596 access_size
= (paddr
% length
);
597 if (access_size
== 0)
598 access_size
= length
;
602 gs
.write_debug_ram(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
603 switch (access_size
) {
605 gs
.write_debug_ram(1, lb(S1
, S0
, 0));
608 gs
.write_debug_ram(1, lh(S1
, S0
, 0));
611 gs
.write_debug_ram(1, lw(S1
, S0
, 0));
614 gs
.write_debug_ram(1, ld(S1
, S0
, 0));
617 gs
.write_debug_ram(2, sd(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 24));
618 gs
.write_debug_ram(3, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*3))));
619 gs
.write_debug_ram(4, paddr
);
620 gs
.write_debug_ram(5, paddr
>> 32);
630 reg_t value
= ((uint64_t) gs
.read_debug_ram(7) << 32) | gs
.read_debug_ram(6);
631 for (unsigned int i
= 0; i
< access_size
; i
++) {
633 *(data
++) = value
& 0xff;
634 fprintf(stderr
, "%02x", (unsigned int) (value
& 0xff));
636 sprintf(buffer
, "%02x", (unsigned int) (value
& 0xff));
642 fprintf(stderr
, "\n");
643 length
-= access_size
;
644 paddr
+= access_size
;
652 gs
.write_debug_ram(4, paddr
);
653 gs
.write_debug_ram(5, paddr
>> 32);
664 unsigned int access_size
;
667 class memory_write_op_t
: public operation_t
670 memory_write_op_t(gdbserver_t
& gdbserver
, reg_t vaddr
, unsigned int length
,
671 const unsigned char *data
) :
672 operation_t(gdbserver
), vaddr(vaddr
), offset(0), length(length
), data(data
) {};
674 ~memory_write_op_t() {
678 bool perform_step(unsigned int step
)
680 reg_t paddr
= gs
.translate(vaddr
);
682 // address goes in S0
683 access_size
= (paddr
% length
);
684 if (access_size
== 0)
685 access_size
= length
;
689 fprintf(stderr
, "write to 0x%lx -> 0x%lx: ", vaddr
, paddr
);
690 for (unsigned int i
= 0; i
< length
; i
++)
691 fprintf(stderr
, "%02x", data
[i
]);
692 fprintf(stderr
, "\n");
694 gs
.write_debug_ram(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
695 switch (access_size
) {
697 gs
.write_debug_ram(1, lb(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 24));
698 gs
.write_debug_ram(2, sb(S1
, S0
, 0));
699 gs
.write_debug_ram(6, data
[0]);
702 gs
.write_debug_ram(1, lh(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 24));
703 gs
.write_debug_ram(2, sh(S1
, S0
, 0));
704 gs
.write_debug_ram(6, data
[0] | (data
[1] << 8));
707 gs
.write_debug_ram(1, lw(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 24));
708 gs
.write_debug_ram(2, sw(S1
, S0
, 0));
709 gs
.write_debug_ram(6, data
[0] | (data
[1] << 8) |
710 (data
[2] << 16) | (data
[3] << 24));
713 gs
.write_debug_ram(1, ld(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 24));
714 gs
.write_debug_ram(2, sd(S1
, S0
, 0));
715 gs
.write_debug_ram(6, data
[0] | (data
[1] << 8) |
716 (data
[2] << 16) | (data
[3] << 24));
717 gs
.write_debug_ram(7, data
[4] | (data
[5] << 8) |
718 (data
[6] << 16) | (data
[7] << 24));
721 gs
.send_packet("E12");
724 gs
.write_debug_ram(3, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*3))));
725 gs
.write_debug_ram(4, paddr
);
726 gs
.write_debug_ram(5, paddr
>> 32);
732 if (gs
.read_debug_ram(DEBUG_RAM_SIZE
/ 4 - 1)) {
733 fprintf(stderr
, "Exception happened while writing to 0x%lx -> 0x%lx\n",
737 offset
+= access_size
;
738 if (offset
>= length
) {
739 gs
.send_packet("OK");
742 const unsigned char *d
= data
+ offset
;
743 switch (access_size
) {
745 gs
.write_debug_ram(6, d
[0]);
748 gs
.write_debug_ram(6, d
[0] | (d
[1] << 8));
751 gs
.write_debug_ram(6, d
[0] | (d
[1] << 8) |
752 (d
[2] << 16) | (d
[3] << 24));
755 gs
.write_debug_ram(6, d
[0] | (d
[1] << 8) |
756 (d
[2] << 16) | (d
[3] << 24));
757 gs
.write_debug_ram(7, d
[4] | (d
[5] << 8) |
758 (d
[6] << 16) | (d
[7] << 24));
761 gs
.send_packet("E12");
764 gs
.write_debug_ram(4, paddr
+ offset
);
765 gs
.write_debug_ram(5, (paddr
+ offset
) >> 32);
775 unsigned int access_size
;
776 const unsigned char *data
;
779 class collect_translation_info_op_t
: public operation_t
782 // Read sufficient information from the target into gdbserver structures so
783 // that it's possible to translate vaddr, vaddr+length, and all addresses
784 // in between to physical addresses.
785 collect_translation_info_op_t(gdbserver_t
& gdbserver
, reg_t vaddr
, size_t length
) :
786 operation_t(gdbserver
), state(STATE_START
), vaddr(vaddr
), length(length
) {};
788 bool perform_step(unsigned int step
)
790 unsigned int vm
= gs
.virtual_memory();
795 // Nothing to be done.
817 sprintf(buf
, "VM mode %d is not supported by gdbserver.cc.", vm
);
819 return true; // die doesn't return, but gcc doesn't know that.
824 // Perform any reads from the just-completed action.
828 case STATE_READ_SPTBR
:
829 gs
.sptbr
= ((uint64_t) gs
.read_debug_ram(5) << 32) | gs
.read_debug_ram(4);
830 gs
.sptbr_valid
= true;
833 gs
.pte_cache
[pte_addr
] = ((uint64_t) gs
.read_debug_ram(5) << 32) |
834 gs
.read_debug_ram(4);
835 fprintf(stderr
, "pte_cache[0x%lx] = 0x%lx\n", pte_addr
, gs
.pte_cache
[pte_addr
]);
839 // Set up the next action.
840 // We only get here for VM_SV32/39/38.
842 if (!gs
.sptbr_valid
) {
843 state
= STATE_READ_SPTBR
;
844 gs
.write_debug_ram(0, csrr(S0
, CSR_SPTBR
));
845 gs
.write_debug_ram(1, sd(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
846 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
851 reg_t base
= gs
.sptbr
<< PGSHIFT
;
852 int ptshift
= (levels
- 1) * ptidxbits
;
853 for (unsigned int i
= 0; i
< levels
; i
++, ptshift
-= ptidxbits
) {
854 reg_t idx
= (vaddr
>> (PGSHIFT
+ ptshift
)) & ((1 << ptidxbits
) - 1);
856 pte_addr
= base
+ idx
* ptesize
;
857 auto it
= gs
.pte_cache
.find(pte_addr
);
858 if (it
== gs
.pte_cache
.end()) {
859 state
= STATE_READ_PTE
;
861 gs
.write_debug_ram(0, lw(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
862 gs
.write_debug_ram(1, lw(S1
, S0
, 0));
863 gs
.write_debug_ram(2, sd(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
865 gs
.write_debug_ram(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
866 gs
.write_debug_ram(1, ld(S1
, S0
, 0));
867 gs
.write_debug_ram(2, sd(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
869 gs
.write_debug_ram(3, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*3))));
870 gs
.write_debug_ram(4, pte_addr
);
871 gs
.write_debug_ram(5, pte_addr
>> 32);
876 reg_t pte
= gs
.pte_cache
[pte_addr
];
877 reg_t ppn
= pte
>> PTE_PPN_SHIFT
;
879 if (PTE_TABLE(pte
)) { // next level of page table
880 base
= ppn
<< PGSHIFT
;
882 // We've collected all the data required for the translation.
887 "ERROR: gdbserver couldn't find appropriate PTEs to translate 0x%lx\n",
901 unsigned int ptidxbits
;
902 unsigned int ptesize
;
906 ////////////////////////////// gdbserver itself
908 gdbserver_t::gdbserver_t(uint16_t port
, sim_t
*sim
) :
911 recv_buf(64 * 1024), send_buf(64 * 1024)
913 socket_fd
= socket(AF_INET
, SOCK_STREAM
, 0);
914 if (socket_fd
== -1) {
915 fprintf(stderr
, "failed to make socket: %s (%d)\n", strerror(errno
), errno
);
919 fcntl(socket_fd
, F_SETFL
, O_NONBLOCK
);
921 if (setsockopt(socket_fd
, SOL_SOCKET
, SO_REUSEADDR
, &reuseaddr
,
922 sizeof(int)) == -1) {
923 fprintf(stderr
, "failed setsockopt: %s (%d)\n", strerror(errno
), errno
);
927 struct sockaddr_in addr
;
928 memset(&addr
, 0, sizeof(addr
));
929 addr
.sin_family
= AF_INET
;
930 addr
.sin_addr
.s_addr
= INADDR_ANY
;
931 addr
.sin_port
= htons(port
);
933 if (bind(socket_fd
, (struct sockaddr
*) &addr
, sizeof(addr
)) == -1) {
934 fprintf(stderr
, "failed to bind socket: %s (%d)\n", strerror(errno
), errno
);
938 if (listen(socket_fd
, 1) == -1) {
939 fprintf(stderr
, "failed to listen on socket: %s (%d)\n", strerror(errno
), errno
);
944 reg_t
gdbserver_t::translate(reg_t vaddr
)
946 unsigned int vm
= virtual_memory();
947 unsigned int levels
, ptidxbits
, ptesize
;
972 sprintf(buf
, "VM mode %d is not supported by gdbserver.cc.", vm
);
974 return true; // die doesn't return, but gcc doesn't know that.
978 // Handle page tables here. There's a bunch of duplicated code with
979 // collect_translation_info_op_t. :-(
980 reg_t base
= sptbr
<< PGSHIFT
;
981 int ptshift
= (levels
- 1) * ptidxbits
;
982 for (unsigned int i
= 0; i
< levels
; i
++, ptshift
-= ptidxbits
) {
983 reg_t idx
= (vaddr
>> (PGSHIFT
+ ptshift
)) & ((1 << ptidxbits
) - 1);
985 reg_t pte_addr
= base
+ idx
* ptesize
;
986 auto it
= pte_cache
.find(pte_addr
);
987 if (it
== pte_cache
.end()) {
988 fprintf(stderr
, "ERROR: gdbserver tried to translate 0x%lx without first "
989 "collecting the relevant PTEs.\n", vaddr
);
990 die("gdbserver_t::translate()");
993 reg_t pte
= pte_cache
[pte_addr
];
994 reg_t ppn
= pte
>> PTE_PPN_SHIFT
;
996 if (PTE_TABLE(pte
)) { // next level of page table
997 base
= ppn
<< PGSHIFT
;
999 // We've collected all the data required for the translation.
1000 reg_t vpn
= vaddr
>> PGSHIFT
;
1001 reg_t paddr
= (ppn
| (vpn
& ((reg_t(1) << ptshift
) - 1))) << PGSHIFT
;
1002 paddr
+= vaddr
& (PGSIZE
-1);
1003 fprintf(stderr
, "gdbserver translate 0x%lx -> 0x%lx\n", vaddr
, paddr
);
1008 fprintf(stderr
, "ERROR: gdbserver tried to translate 0x%lx but the relevant "
1009 "PTEs are invalid.\n", vaddr
);
1010 // TODO: Is it better to throw an exception here?
1014 unsigned int gdbserver_t::privilege_mode()
1016 unsigned int mode
= get_field(dcsr
, DCSR_PRV
);
1017 if (get_field(saved_mstatus
, MSTATUS_MPRV
))
1018 mode
= get_field(saved_mstatus
, MSTATUS_MPP
);
1022 unsigned int gdbserver_t::virtual_memory()
1024 unsigned int mode
= privilege_mode();
1027 return get_field(saved_mstatus
, MSTATUS_VM
);
1030 void gdbserver_t::write_debug_ram(unsigned int index
, uint32_t value
)
1032 sim
->debug_module
.ram_write32(index
, value
);
1035 uint32_t gdbserver_t::read_debug_ram(unsigned int index
)
1037 return sim
->debug_module
.ram_read32(index
);
1040 void gdbserver_t::add_operation(operation_t
* operation
)
1042 operation_queue
.push(operation
);
1045 void gdbserver_t::accept()
1047 client_fd
= ::accept(socket_fd
, NULL
, NULL
);
1048 if (client_fd
== -1) {
1049 if (errno
== EAGAIN
) {
1050 // No client waiting to connect right now.
1052 fprintf(stderr
, "failed to accept on socket: %s (%d)\n", strerror(errno
),
1057 fcntl(client_fd
, F_SETFL
, O_NONBLOCK
);
1060 extended_mode
= false;
1062 // gdb wants the core to be halted when it attaches.
1063 add_operation(new halt_op_t(*this));
1067 void gdbserver_t::read()
1069 // Reading from a non-blocking socket still blocks if there is no data
1072 size_t count
= recv_buf
.contiguous_empty_size();
1074 ssize_t bytes
= ::read(client_fd
, recv_buf
.contiguous_empty(), count
);
1076 if (errno
== EAGAIN
) {
1077 // We'll try again the next call.
1079 fprintf(stderr
, "failed to read on socket: %s (%d)\n", strerror(errno
), errno
);
1082 } else if (bytes
== 0) {
1083 // The remote disconnected.
1085 processor_t
*p
= sim
->get_core(0);
1086 // TODO p->set_halted(false, HR_NONE);
1090 recv_buf
.data_added(bytes
);
1094 void gdbserver_t::write()
1096 if (send_buf
.empty())
1099 while (!send_buf
.empty()) {
1100 unsigned int count
= send_buf
.contiguous_data_size();
1102 ssize_t bytes
= ::write(client_fd
, send_buf
.contiguous_data(), count
);
1104 fprintf(stderr
, "failed to write to socket: %s (%d)\n", strerror(errno
), errno
);
1106 } else if (bytes
== 0) {
1107 // Client can't take any more data right now.
1110 fprintf(stderr
, "wrote %ld bytes: ", bytes
);
1111 for (unsigned int i
= 0; i
< bytes
; i
++) {
1112 fprintf(stderr
, "%c", send_buf
[i
]);
1114 fprintf(stderr
, "\n");
1115 send_buf
.consume(bytes
);
1120 void print_packet(const std::vector
<uint8_t> &packet
)
1122 for (uint8_t c
: packet
) {
1123 if (c
>= ' ' and c
<= '~')
1124 fprintf(stderr
, "%c", c
);
1126 fprintf(stderr
, "\\x%02x", c
);
1128 fprintf(stderr
, "\n");
1131 uint8_t compute_checksum(const std::vector
<uint8_t> &packet
)
1133 uint8_t checksum
= 0;
1134 for (auto i
= packet
.begin() + 1; i
!= packet
.end() - 3; i
++ ) {
1140 uint8_t character_hex_value(uint8_t character
)
1142 if (character
>= '0' && character
<= '9')
1143 return character
- '0';
1144 if (character
>= 'a' && character
<= 'f')
1145 return 10 + character
- 'a';
1146 if (character
>= 'A' && character
<= 'F')
1147 return 10 + character
- 'A';
1151 uint8_t extract_checksum(const std::vector
<uint8_t> &packet
)
1153 return character_hex_value(*(packet
.end() - 1)) +
1154 16 * character_hex_value(*(packet
.end() - 2));
1157 void gdbserver_t::process_requests()
1159 // See https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html
1161 while (!recv_buf
.empty()) {
1162 std::vector
<uint8_t> packet
;
1163 for (unsigned int i
= 0; i
< recv_buf
.size(); i
++) {
1164 uint8_t b
= recv_buf
[i
];
1166 if (packet
.empty() && expect_ack
&& b
== '+') {
1167 recv_buf
.consume(1);
1171 if (packet
.empty() && b
== 3) {
1172 fprintf(stderr
, "Received interrupt\n");
1173 recv_buf
.consume(1);
1179 // Start of new packet.
1180 if (!packet
.empty()) {
1181 fprintf(stderr
, "Received malformed %ld-byte packet from debug client: ",
1183 print_packet(packet
);
1184 recv_buf
.consume(i
);
1189 packet
.push_back(b
);
1191 // Packets consist of $<packet-data>#<checksum>
1192 // where <checksum> is
1193 if (packet
.size() >= 4 &&
1194 packet
[packet
.size()-3] == '#') {
1195 handle_packet(packet
);
1196 recv_buf
.consume(i
+1);
1200 // There's a partial packet in the buffer. Wait until we get more data to
1202 if (packet
.size()) {
1208 void gdbserver_t::handle_halt_reason(const std::vector
<uint8_t> &packet
)
1213 void gdbserver_t::handle_general_registers_read(const std::vector
<uint8_t> &packet
)
1215 add_operation(new general_registers_read_op_t(*this));
1218 void gdbserver_t::set_interrupt(uint32_t hartid
) {
1219 sim
->debug_module
.set_interrupt(hartid
);
1222 // First byte is the most-significant one.
1223 // Eg. "08675309" becomes 0x08675309.
1224 uint64_t consume_hex_number(std::vector
<uint8_t>::const_iterator
&iter
,
1225 std::vector
<uint8_t>::const_iterator end
)
1229 while (iter
!= end
) {
1231 uint64_t c_value
= character_hex_value(c
);
1241 // First byte is the least-significant one.
1242 // Eg. "08675309" becomes 0x09536708
1243 uint64_t consume_hex_number_le(std::vector
<uint8_t>::const_iterator
&iter
,
1244 std::vector
<uint8_t>::const_iterator end
)
1247 unsigned int shift
= 4;
1249 while (iter
!= end
) {
1251 uint64_t c_value
= character_hex_value(c
);
1255 value
|= c_value
<< shift
;
1256 if ((shift
% 8) == 0)
1264 void consume_string(std::string
&str
, std::vector
<uint8_t>::const_iterator
&iter
,
1265 std::vector
<uint8_t>::const_iterator end
, uint8_t separator
)
1267 while (iter
!= end
&& *iter
!= separator
) {
1268 str
.append(1, (char) *iter
);
1273 void gdbserver_t::handle_register_read(const std::vector
<uint8_t> &packet
)
1277 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1278 unsigned int n
= consume_hex_number(iter
, packet
.end());
1280 return send_packet("E01");
1282 add_operation(new register_read_op_t(*this, n
));
1285 void gdbserver_t::handle_register_write(const std::vector
<uint8_t> &packet
)
1289 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1290 unsigned int n
= consume_hex_number(iter
, packet
.end());
1292 return send_packet("E05");
1295 reg_t value
= consume_hex_number_le(iter
, packet
.end());
1297 return send_packet("E06");
1299 processor_t
*p
= sim
->get_core(0);
1301 add_operation(new register_write_op_t(*this, n
, value
));
1303 return send_packet("OK");
1306 void gdbserver_t::handle_memory_read(const std::vector
<uint8_t> &packet
)
1309 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1310 reg_t address
= consume_hex_number(iter
, packet
.end());
1312 return send_packet("E10");
1314 reg_t length
= consume_hex_number(iter
, packet
.end());
1316 return send_packet("E11");
1318 add_operation(new collect_translation_info_op_t(*this, address
, length
));
1319 add_operation(new memory_read_op_t(*this, address
, length
));
1322 void gdbserver_t::handle_memory_binary_write(const std::vector
<uint8_t> &packet
)
1324 // X addr,length:XX...
1325 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1326 reg_t address
= consume_hex_number(iter
, packet
.end());
1328 return send_packet("E20");
1330 reg_t length
= consume_hex_number(iter
, packet
.end());
1332 return send_packet("E21");
1336 return send_packet("OK");
1339 unsigned char *data
= new unsigned char[length
];
1340 for (unsigned int i
= 0; i
< length
; i
++) {
1341 if (iter
== packet
.end()) {
1342 return send_packet("E22");
1347 // The binary data representation uses 7d (ascii ‘}’) as an escape
1348 // character. Any escaped byte is transmitted as the escape character
1349 // followed by the original character XORed with 0x20. For example, the
1350 // byte 0x7d would be transmitted as the two bytes 0x7d 0x5d. The bytes
1351 // 0x23 (ascii ‘#’), 0x24 (ascii ‘$’), and 0x7d (ascii ‘}’) must always
1353 if (iter
== packet
.end()) {
1354 return send_packet("E23");
1362 return send_packet("E4b"); // EOVERFLOW
1364 add_operation(new collect_translation_info_op_t(*this, address
, length
));
1365 add_operation(new memory_write_op_t(*this, address
, length
, data
));
1368 void gdbserver_t::handle_continue(const std::vector
<uint8_t> &packet
)
1371 processor_t
*p
= sim
->get_core(0);
1372 if (packet
[2] != '#') {
1373 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1374 saved_dpc
= consume_hex_number(iter
, packet
.end());
1376 return send_packet("E30");
1379 add_operation(new continue_op_t(*this, false));
1382 void gdbserver_t::handle_step(const std::vector
<uint8_t> &packet
)
1385 if (packet
[2] != '#') {
1386 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1388 //p->state.pc = consume_hex_number(iter, packet.end());
1390 return send_packet("E40");
1393 add_operation(new continue_op_t(*this, true));
1396 void gdbserver_t::handle_kill(const std::vector
<uint8_t> &packet
)
1399 // The exact effect of this packet is not specified.
1400 // Looks like OpenOCD disconnects?
1404 void gdbserver_t::handle_extended(const std::vector
<uint8_t> &packet
)
1406 // Enable extended mode. In extended mode, the remote server is made
1407 // persistent. The ‘R’ packet is used to restart the program being debugged.
1409 extended_mode
= true;
1412 void gdbserver_t::handle_breakpoint(const std::vector
<uint8_t> &packet
)
1414 // insert: Z type,addr,kind
1415 // remove: z type,addr,kind
1417 software_breakpoint_t bp
;
1418 bool insert
= (packet
[1] == 'Z');
1419 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1420 int type
= consume_hex_number(iter
, packet
.end());
1422 return send_packet("E50");
1424 bp
.address
= consume_hex_number(iter
, packet
.end());
1426 return send_packet("E51");
1428 bp
.size
= consume_hex_number(iter
, packet
.end());
1429 // There may be more options after a ; here, but we don't support that.
1431 return send_packet("E52");
1433 if (bp
.size
!= 2 && bp
.size
!= 4) {
1434 return send_packet("E53");
1437 fence_i_required
= true;
1438 add_operation(new collect_translation_info_op_t(*this, bp
.address
, bp
.size
));
1440 unsigned char* swbp
= new unsigned char[4];
1442 swbp
[0] = C_EBREAK
& 0xff;
1443 swbp
[1] = (C_EBREAK
>> 8) & 0xff;
1445 swbp
[0] = EBREAK
& 0xff;
1446 swbp
[1] = (EBREAK
>> 8) & 0xff;
1447 swbp
[2] = (EBREAK
>> 16) & 0xff;
1448 swbp
[3] = (EBREAK
>> 24) & 0xff;
1451 breakpoints
[bp
.address
] = new software_breakpoint_t(bp
);
1452 add_operation(new memory_read_op_t(*this, bp
.address
, bp
.size
,
1453 breakpoints
[bp
.address
]->instruction
));
1454 add_operation(new memory_write_op_t(*this, bp
.address
, bp
.size
, swbp
));
1457 software_breakpoint_t
*found_bp
;
1458 found_bp
= breakpoints
[bp
.address
];
1459 unsigned char* instruction
= new unsigned char[4];
1460 memcpy(instruction
, found_bp
->instruction
, 4);
1461 add_operation(new memory_write_op_t(*this, found_bp
->address
,
1462 found_bp
->size
, instruction
));
1463 breakpoints
.erase(bp
.address
);
1467 return send_packet("OK");
1470 void gdbserver_t::handle_query(const std::vector
<uint8_t> &packet
)
1473 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1475 consume_string(name
, iter
, packet
.end(), ':');
1476 if (iter
!= packet
.end())
1478 if (name
== "Supported") {
1480 while (iter
!= packet
.end()) {
1481 std::string feature
;
1482 consume_string(feature
, iter
, packet
.end(), ';');
1483 if (iter
!= packet
.end())
1485 if (feature
== "swbreak+") {
1489 return end_packet();
1492 fprintf(stderr
, "Unsupported query %s\n", name
.c_str());
1493 return send_packet("");
1496 void gdbserver_t::handle_packet(const std::vector
<uint8_t> &packet
)
1498 if (compute_checksum(packet
) != extract_checksum(packet
)) {
1499 fprintf(stderr
, "Received %ld-byte packet with invalid checksum\n", packet
.size());
1500 fprintf(stderr
, "Computed checksum: %x\n", compute_checksum(packet
));
1501 print_packet(packet
);
1506 fprintf(stderr
, "Received %ld-byte packet from debug client: ", packet
.size());
1507 print_packet(packet
);
1510 switch (packet
[1]) {
1512 return handle_extended(packet
);
1514 return handle_halt_reason(packet
);
1516 return handle_general_registers_read(packet
);
1518 // return handle_kill(packet);
1520 return handle_memory_read(packet
);
1522 // return handle_memory_write(packet);
1524 return handle_memory_binary_write(packet
);
1526 return handle_register_read(packet
);
1528 return handle_register_write(packet
);
1530 return handle_continue(packet
);
1532 return handle_step(packet
);
1535 return handle_breakpoint(packet
);
1538 return handle_query(packet
);
1542 fprintf(stderr
, "** Unsupported packet: ");
1543 print_packet(packet
);
1547 void gdbserver_t::handle_interrupt()
1549 processor_t
*p
= sim
->get_core(0);
1550 add_operation(new halt_op_t(*this, true));
1553 void gdbserver_t::handle()
1555 if (client_fd
> 0) {
1556 processor_t
*p
= sim
->get_core(0);
1558 bool interrupt
= sim
->debug_module
.get_interrupt(0);
1560 if (!interrupt
&& !operation_queue
.empty()) {
1561 operation_t
*operation
= operation_queue
.front();
1562 if (operation
->step()) {
1563 operation_queue
.pop();
1568 bool halt_notification
= sim
->debug_module
.get_halt_notification(0);
1569 if (halt_notification
) {
1570 sim
->debug_module
.clear_halt_notification(0);
1571 add_operation(new halt_op_t(*this, true));
1581 if (operation_queue
.empty()) {
1582 this->process_requests();
1586 void gdbserver_t::send(const char* msg
)
1588 unsigned int length
= strlen(msg
);
1589 for (const char *c
= msg
; *c
; c
++)
1590 running_checksum
+= *c
;
1591 send_buf
.append((const uint8_t *) msg
, length
);
1594 void gdbserver_t::send(uint64_t value
)
1597 for (unsigned int i
= 0; i
< 8; i
++) {
1598 sprintf(buffer
, "%02x", (int) (value
& 0xff));
1604 void gdbserver_t::send(uint32_t value
)
1607 for (unsigned int i
= 0; i
< 4; i
++) {
1608 sprintf(buffer
, "%02x", (int) (value
& 0xff));
1614 void gdbserver_t::send_packet(const char* data
)
1622 void gdbserver_t::start_packet()
1625 running_checksum
= 0;
1628 void gdbserver_t::end_packet(const char* data
)
1634 char checksum_string
[4];
1635 sprintf(checksum_string
, "#%02x", running_checksum
);
1636 send(checksum_string
);