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 sb(unsigned int src
, unsigned int base
, uint16_t offset
)
91 return (bits(offset
, 11, 5) << 25) |
94 (bits(offset
, 4, 0) << 7) |
98 static uint32_t sh(unsigned int src
, unsigned int base
, uint16_t offset
)
100 return (bits(offset
, 11, 5) << 25) |
103 (bits(offset
, 4, 0) << 7) |
107 static uint32_t sw(unsigned int src
, unsigned int base
, uint16_t offset
)
109 return (bits(offset
, 11, 5) << 25) |
112 (bits(offset
, 4, 0) << 7) |
116 static uint32_t sd(unsigned int src
, unsigned int base
, uint16_t offset
)
118 return (bits(offset
, 11, 5) << 25) |
119 (bits(src
, 4, 0) << 20) |
121 (bits(offset
, 4, 0) << 7) |
125 static uint32_t ld(unsigned int rd
, unsigned int base
, uint16_t offset
)
127 return (bits(offset
, 11, 0) << 20) |
129 (bits(rd
, 4, 0) << 7) |
133 static uint32_t lw(unsigned int rd
, unsigned int base
, uint16_t offset
)
135 return (bits(offset
, 11, 0) << 20) |
137 (bits(rd
, 4, 0) << 7) |
141 static uint32_t lh(unsigned int rd
, unsigned int base
, uint16_t offset
)
143 return (bits(offset
, 11, 0) << 20) |
145 (bits(rd
, 4, 0) << 7) |
149 static uint32_t lb(unsigned int rd
, unsigned int base
, uint16_t offset
)
151 return (bits(offset
, 11, 0) << 20) |
153 (bits(rd
, 4, 0) << 7) |
157 static uint32_t fsd(unsigned int src
, unsigned int base
, uint16_t offset
)
159 return (bits(offset
, 11, 5) << 25) |
160 (bits(src
, 4, 0) << 20) |
162 (bits(offset
, 4, 0) << 7) |
166 static uint32_t addi(unsigned int dest
, unsigned int src
, uint16_t imm
)
168 return (bits(imm
, 11, 0) << 20) |
174 static uint32_t nop()
176 return addi(0, 0, 0);
179 template <typename T
>
180 unsigned int circular_buffer_t
<T
>::size() const
185 return end
+ capacity
- start
;
188 template <typename T
>
189 void circular_buffer_t
<T
>::consume(unsigned int bytes
)
191 start
= (start
+ bytes
) % capacity
;
194 template <typename T
>
195 unsigned int circular_buffer_t
<T
>::contiguous_empty_size() const
199 return capacity
- end
- 1;
201 return capacity
- end
;
203 return start
- end
- 1;
206 template <typename T
>
207 unsigned int circular_buffer_t
<T
>::contiguous_data_size() const
212 return capacity
- start
;
215 template <typename T
>
216 void circular_buffer_t
<T
>::data_added(unsigned int bytes
)
219 assert(end
<= capacity
);
224 template <typename T
>
225 void circular_buffer_t
<T
>::reset()
231 template <typename T
>
232 void circular_buffer_t
<T
>::append(const T
*src
, unsigned int count
)
234 unsigned int copy
= std::min(count
, contiguous_empty_size());
235 memcpy(contiguous_empty(), src
, copy
* sizeof(T
));
239 assert(count
< contiguous_empty_size());
240 memcpy(contiguous_empty(), src
, count
* sizeof(T
));
245 ////////////////////////////// Debug Operations
247 class halt_op_t
: public operation_t
250 halt_op_t(gdbserver_t
& gdbserver
, bool send_status
=false) :
251 operation_t(gdbserver
), send_status(send_status
) {};
253 bool perform_step(unsigned int step
) {
256 // TODO: For now we just assume the target is 64-bit.
257 gs
.write_debug_ram(0, csrsi(DCSR_ADDRESS
, DCSR_HALT_MASK
));
258 gs
.write_debug_ram(1, csrr(S0
, DPC_ADDRESS
));
259 gs
.write_debug_ram(2, sd(S0
, 0, (uint16_t) DEBUG_RAM_START
));
260 gs
.write_debug_ram(3, csrr(S0
, CSR_MBADADDR
));
261 gs
.write_debug_ram(4, sd(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 8));
262 gs
.write_debug_ram(5, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*5))));
264 // We could read mcause here as well, but only on 64-bit targets. I'm
265 // trying to keep The patterns here usable for 32-bit ISAs as well. (On a
266 // 32-bit ISA 8 words are required, while the minimum Debug RAM size is 7
271 gs
.saved_dpc
= ((uint64_t) gs
.read_debug_ram(1) << 32) | gs
.read_debug_ram(0);
272 gs
.saved_mbadaddr
= ((uint64_t) gs
.read_debug_ram(3) << 32) | gs
.read_debug_ram(2);
274 gs
.write_debug_ram(0, csrr(S0
, CSR_MCAUSE
));
275 gs
.write_debug_ram(1, sd(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 0));
276 gs
.write_debug_ram(2, csrr(S0
, CSR_MSTATUS
));
277 gs
.write_debug_ram(3, sd(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 8));
278 gs
.write_debug_ram(4, csrr(S0
, CSR_DCSR
));
279 gs
.write_debug_ram(5, sd(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
280 gs
.write_debug_ram(6, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*6))));
285 gs
.saved_mcause
= ((uint64_t) gs
.read_debug_ram(1) << 32) | gs
.read_debug_ram(0);
286 gs
.saved_mstatus
= ((uint64_t) gs
.read_debug_ram(3) << 32) | gs
.read_debug_ram(2);
287 gs
.dcsr
= ((uint64_t) gs
.read_debug_ram(5) << 32) | gs
.read_debug_ram(4);
289 gs
.sptbr_valid
= false;
290 gs
.pte_cache
.clear();
293 switch (get_field(gs
.dcsr
, DCSR_CAUSE
)) {
294 case DCSR_CAUSE_NONE
:
295 fprintf(stderr
, "Internal error. Processor halted without reason.\n");
298 case DCSR_CAUSE_HWBP
:
299 case DCSR_CAUSE_DEBUGINT
:
300 case DCSR_CAUSE_STEP
:
301 case DCSR_CAUSE_HALT
:
302 // There's no gdb code for this.
303 gs
.send_packet("T05");
305 case DCSR_CAUSE_SWBP
:
306 gs
.send_packet("T05swbreak:;");
320 class continue_op_t
: public operation_t
323 continue_op_t(gdbserver_t
& gdbserver
, bool single_step
) :
324 operation_t(gdbserver
), single_step(single_step
) {};
326 bool perform_step(unsigned int step
) {
329 gs
.write_debug_ram(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+16));
330 gs
.write_debug_ram(1, csrw(S0
, DPC_ADDRESS
));
331 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
332 gs
.write_debug_ram(4, gs
.saved_dpc
);
333 gs
.write_debug_ram(5, gs
.saved_dpc
>> 32);
338 gs
.write_debug_ram(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+16));
339 gs
.write_debug_ram(1, csrw(S0
, CSR_MBADADDR
));
340 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
341 gs
.write_debug_ram(4, gs
.saved_mbadaddr
);
342 gs
.write_debug_ram(5, gs
.saved_mbadaddr
>> 32);
347 gs
.write_debug_ram(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+16));
348 gs
.write_debug_ram(1, csrw(S0
, CSR_MSTATUS
));
349 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
350 gs
.write_debug_ram(4, gs
.saved_mstatus
);
351 gs
.write_debug_ram(5, gs
.saved_mstatus
>> 32);
356 gs
.write_debug_ram(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+24));
357 gs
.write_debug_ram(1, csrw(S0
, CSR_MCAUSE
));
358 gs
.write_debug_ram(2, lw(S0
, 0, (uint16_t) DEBUG_RAM_START
+20));
359 gs
.write_debug_ram(3, csrw(S0
, CSR_DCSR
));
360 gs
.write_debug_ram(4, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*4))));
362 reg_t dcsr
= gs
.dcsr
& ~DCSR_HALT_MASK
;
364 dcsr
|= DCSR_STEP_MASK
;
366 dcsr
&= ~DCSR_STEP_MASK
;
367 gs
.write_debug_ram(5, dcsr
);
369 gs
.write_debug_ram(6, gs
.saved_mcause
);
370 gs
.write_debug_ram(7, gs
.saved_mcause
>> 32);
381 class general_registers_read_op_t
: public operation_t
383 // Register order that gdb expects is:
384 // "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
385 // "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
386 // "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
387 // "x24", "x25", "x26", "x27", "x28", "x29", "x30", "x31",
389 // Each byte of register data is described by two hex digits. The bytes with
390 // the register are transmitted in target byte order. The size of each
391 // register and their position within the ‘g’ packet are determined by the
392 // gdb internal gdbarch functions DEPRECATED_REGISTER_RAW_SIZE and
393 // gdbarch_register_name.
396 general_registers_read_op_t(gdbserver_t
& gdbserver
) :
397 operation_t(gdbserver
) {};
399 bool perform_step(unsigned int step
)
404 // x0 is always zero.
407 gs
.write_debug_ram(0, sd(1, 0, (uint16_t) DEBUG_RAM_START
+ 16));
408 gs
.write_debug_ram(1, sd(2, 0, (uint16_t) DEBUG_RAM_START
+ 0));
409 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
414 gs
.send(((uint64_t) gs
.read_debug_ram(5) << 32) | gs
.read_debug_ram(4));
420 gs
.send(((uint64_t) gs
.read_debug_ram(1) << 32) | gs
.read_debug_ram(0));
422 unsigned int current_reg
= 2 * step
+ 1;
424 if (current_reg
== S1
) {
425 gs
.write_debug_ram(i
++, ld(S1
, 0, (uint16_t) DEBUG_RAM_END
- 8));
427 gs
.write_debug_ram(i
++, sd(current_reg
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
428 if (current_reg
+ 1 == S0
) {
429 gs
.write_debug_ram(i
++, csrr(S0
, CSR_DSCRATCH
));
431 gs
.write_debug_ram(i
++, sd(current_reg
+1, 0, (uint16_t) DEBUG_RAM_START
+ 0));
432 gs
.write_debug_ram(i
, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*i
))));
439 class register_read_op_t
: public operation_t
442 register_read_op_t(gdbserver_t
& gdbserver
, unsigned int reg
) :
443 operation_t(gdbserver
), reg(reg
) {};
445 bool perform_step(unsigned int step
)
449 if (reg
>= REG_XPR0
&& reg
<= REG_XPR31
) {
450 die("handle_register_read");
451 // send(p->state.XPR[reg - REG_XPR0]);
452 } else if (reg
== REG_PC
) {
454 gs
.send(gs
.saved_dpc
);
457 } else if (reg
>= REG_FPR0
&& reg
<= REG_FPR31
) {
458 // send(p->state.FPR[reg - REG_FPR0]);
459 gs
.write_debug_ram(0, fsd(reg
- REG_FPR0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
460 gs
.write_debug_ram(1, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*1))));
461 } else if (reg
== REG_CSR0
+ CSR_MBADADDR
) {
463 gs
.send(gs
.saved_mbadaddr
);
466 } else if (reg
== REG_CSR0
+ CSR_MCAUSE
) {
468 gs
.send(gs
.saved_mcause
);
471 } else if (reg
>= REG_CSR0
&& reg
<= REG_CSR4095
) {
472 gs
.write_debug_ram(0, csrr(S0
, reg
- REG_CSR0
));
473 gs
.write_debug_ram(1, sd(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
474 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
475 // If we hit an exception reading the CSR, we'll end up returning ~0 as
476 // the register's value, which is what we want. (Right?)
477 gs
.write_debug_ram(4, 0xffffffff);
478 gs
.write_debug_ram(5, 0xffffffff);
480 gs
.send_packet("E02");
487 gs
.send(((uint64_t) gs
.read_debug_ram(5) << 32) | gs
.read_debug_ram(4));
498 class memory_read_op_t
: public operation_t
501 // Read length bytes from vaddr, storing the result into data.
502 // If data is NULL, send the result straight to gdb.
503 memory_read_op_t(gdbserver_t
& gdbserver
, reg_t vaddr
, unsigned int length
,
504 unsigned char *data
=NULL
) :
505 operation_t(gdbserver
), vaddr(vaddr
), length(length
), data(data
) {};
507 bool perform_step(unsigned int step
)
510 // address goes in S0
511 paddr
= gs
.translate(vaddr
);
512 access_size
= (paddr
% length
);
513 if (access_size
== 0)
514 access_size
= length
;
518 gs
.write_debug_ram(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
519 switch (access_size
) {
521 gs
.write_debug_ram(1, lb(S1
, S0
, 0));
524 gs
.write_debug_ram(1, lh(S1
, S0
, 0));
527 gs
.write_debug_ram(1, lw(S1
, S0
, 0));
530 gs
.write_debug_ram(1, ld(S1
, S0
, 0));
533 gs
.write_debug_ram(2, sd(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 24));
534 gs
.write_debug_ram(3, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*3))));
535 gs
.write_debug_ram(4, paddr
);
536 gs
.write_debug_ram(5, paddr
>> 32);
546 reg_t value
= ((uint64_t) gs
.read_debug_ram(7) << 32) | gs
.read_debug_ram(6);
547 for (unsigned int i
= 0; i
< access_size
; i
++) {
549 *(data
++) = value
& 0xff;
550 fprintf(stderr
, "%02x", (unsigned int) (value
& 0xff));
552 sprintf(buffer
, "%02x", (unsigned int) (value
& 0xff));
558 fprintf(stderr
, "\n");
559 length
-= access_size
;
560 paddr
+= access_size
;
568 gs
.write_debug_ram(4, paddr
);
569 gs
.write_debug_ram(5, paddr
>> 32);
580 unsigned int access_size
;
583 class memory_write_op_t
: public operation_t
586 memory_write_op_t(gdbserver_t
& gdbserver
, reg_t vaddr
, unsigned int length
,
587 const unsigned char *data
) :
588 operation_t(gdbserver
), vaddr(vaddr
), offset(0), length(length
), data(data
) {};
590 ~memory_write_op_t() {
594 bool perform_step(unsigned int step
)
596 reg_t paddr
= gs
.translate(vaddr
);
598 // address goes in S0
599 access_size
= (paddr
% length
);
600 if (access_size
== 0)
601 access_size
= length
;
603 fprintf(stderr
, "write to 0x%lx -> 0x%lx: ", vaddr
, paddr
);
604 for (unsigned int i
= 0; i
< length
; i
++)
605 fprintf(stderr
, "%02x", data
[i
]);
606 fprintf(stderr
, "\n");
608 gs
.write_debug_ram(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
609 switch (access_size
) {
611 gs
.write_debug_ram(1, lb(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 24));
612 gs
.write_debug_ram(2, sb(S1
, S0
, 0));
613 gs
.write_debug_ram(6, data
[0]);
616 gs
.write_debug_ram(1, lh(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 24));
617 gs
.write_debug_ram(2, sh(S1
, S0
, 0));
618 gs
.write_debug_ram(6, data
[0] | (data
[1] << 8));
621 gs
.write_debug_ram(1, lw(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 24));
622 gs
.write_debug_ram(2, sw(S1
, S0
, 0));
623 gs
.write_debug_ram(6, data
[0] | (data
[1] << 8) |
624 (data
[2] << 16) | (data
[3] << 24));
627 gs
.write_debug_ram(1, ld(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 24));
628 gs
.write_debug_ram(2, sd(S1
, S0
, 0));
629 gs
.write_debug_ram(6, data
[0] | (data
[1] << 8) |
630 (data
[2] << 16) | (data
[3] << 24));
631 gs
.write_debug_ram(7, data
[4] | (data
[5] << 8) |
632 (data
[6] << 16) | (data
[7] << 24));
635 gs
.send_packet("E12");
638 gs
.write_debug_ram(3, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*3))));
639 gs
.write_debug_ram(4, paddr
);
640 gs
.write_debug_ram(5, paddr
>> 32);
646 offset
+= access_size
;
647 if (offset
>= length
) {
648 gs
.send_packet("OK");
651 const unsigned char *d
= data
+ offset
;
652 switch (access_size
) {
654 gs
.write_debug_ram(6, d
[0]);
657 gs
.write_debug_ram(6, d
[0] | (d
[1] << 8));
660 gs
.write_debug_ram(6, d
[0] | (d
[1] << 8) |
661 (d
[2] << 16) | (d
[3] << 24));
664 gs
.write_debug_ram(6, d
[0] | (d
[1] << 8) |
665 (d
[2] << 16) | (d
[3] << 24));
666 gs
.write_debug_ram(7, d
[4] | (d
[5] << 8) |
667 (d
[6] << 16) | (d
[7] << 24));
670 gs
.send_packet("E12");
673 gs
.write_debug_ram(4, paddr
+ offset
);
674 gs
.write_debug_ram(5, (paddr
+ offset
) >> 32);
684 unsigned int access_size
;
685 const unsigned char *data
;
688 class collect_translation_info_op_t
: public operation_t
691 // Read sufficient information from the target into gdbserver structures so
692 // that it's possible to translate vaddr, vaddr+length, and all addresses
693 // in between to physical addresses.
694 collect_translation_info_op_t(gdbserver_t
& gdbserver
, reg_t vaddr
, size_t length
) :
695 operation_t(gdbserver
), state(STATE_START
), vaddr(vaddr
), length(length
) {};
697 bool perform_step(unsigned int step
)
699 unsigned int vm
= gs
.virtual_memory();
704 // Nothing to be done.
726 sprintf(buf
, "VM mode %d is not supported by gdbserver.cc.", vm
);
728 return true; // die doesn't return, but gcc doesn't know that.
733 // Perform any reads from the just-completed action.
737 case STATE_READ_SPTBR
:
738 gs
.sptbr
= ((uint64_t) gs
.read_debug_ram(5) << 32) | gs
.read_debug_ram(4);
739 gs
.sptbr_valid
= true;
742 gs
.pte_cache
[pte_addr
] = ((uint64_t) gs
.read_debug_ram(5) << 32) |
743 gs
.read_debug_ram(4);
744 fprintf(stderr
, "pte_cache[0x%lx] = 0x%lx\n", pte_addr
, gs
.pte_cache
[pte_addr
]);
748 // Set up the next action.
749 // We only get here for VM_SV32/39/38.
751 if (!gs
.sptbr_valid
) {
752 state
= STATE_READ_SPTBR
;
753 gs
.write_debug_ram(0, csrr(S0
, CSR_SPTBR
));
754 gs
.write_debug_ram(1, sd(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
755 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
760 reg_t base
= gs
.sptbr
<< PGSHIFT
;
761 int ptshift
= (levels
- 1) * ptidxbits
;
762 for (unsigned int i
= 0; i
< levels
; i
++, ptshift
-= ptidxbits
) {
763 reg_t idx
= (vaddr
>> (PGSHIFT
+ ptshift
)) & ((1 << ptidxbits
) - 1);
765 pte_addr
= base
+ idx
* ptesize
;
766 auto it
= gs
.pte_cache
.find(pte_addr
);
767 if (it
== gs
.pte_cache
.end()) {
768 state
= STATE_READ_PTE
;
770 gs
.write_debug_ram(0, lw(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
771 gs
.write_debug_ram(1, lw(S1
, S0
, 0));
772 gs
.write_debug_ram(2, sd(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
774 gs
.write_debug_ram(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
775 gs
.write_debug_ram(1, ld(S1
, S0
, 0));
776 gs
.write_debug_ram(2, sd(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
778 gs
.write_debug_ram(3, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*3))));
779 gs
.write_debug_ram(4, pte_addr
);
780 gs
.write_debug_ram(5, pte_addr
>> 32);
785 reg_t pte
= gs
.pte_cache
[pte_addr
];
786 reg_t ppn
= pte
>> PTE_PPN_SHIFT
;
788 if (PTE_TABLE(pte
)) { // next level of page table
789 base
= ppn
<< PGSHIFT
;
791 // We've collected all the data required for the translation.
796 "ERROR: gdbserver couldn't find appropriate PTEs to translate 0x%lx\n",
810 unsigned int ptidxbits
;
811 unsigned int ptesize
;
815 ////////////////////////////// gdbserver itself
817 gdbserver_t::gdbserver_t(uint16_t port
, sim_t
*sim
) :
820 recv_buf(64 * 1024), send_buf(64 * 1024)
822 socket_fd
= socket(AF_INET
, SOCK_STREAM
, 0);
823 if (socket_fd
== -1) {
824 fprintf(stderr
, "failed to make socket: %s (%d)\n", strerror(errno
), errno
);
828 fcntl(socket_fd
, F_SETFL
, O_NONBLOCK
);
830 if (setsockopt(socket_fd
, SOL_SOCKET
, SO_REUSEADDR
, &reuseaddr
,
831 sizeof(int)) == -1) {
832 fprintf(stderr
, "failed setsockopt: %s (%d)\n", strerror(errno
), errno
);
836 struct sockaddr_in addr
;
837 memset(&addr
, 0, sizeof(addr
));
838 addr
.sin_family
= AF_INET
;
839 addr
.sin_addr
.s_addr
= INADDR_ANY
;
840 addr
.sin_port
= htons(port
);
842 if (bind(socket_fd
, (struct sockaddr
*) &addr
, sizeof(addr
)) == -1) {
843 fprintf(stderr
, "failed to bind socket: %s (%d)\n", strerror(errno
), errno
);
847 if (listen(socket_fd
, 1) == -1) {
848 fprintf(stderr
, "failed to listen on socket: %s (%d)\n", strerror(errno
), errno
);
853 reg_t
gdbserver_t::translate(reg_t vaddr
)
855 unsigned int vm
= virtual_memory();
856 unsigned int levels
, ptidxbits
, ptesize
;
881 sprintf(buf
, "VM mode %d is not supported by gdbserver.cc.", vm
);
883 return true; // die doesn't return, but gcc doesn't know that.
887 // Handle page tables here. There's a bunch of duplicated code with
888 // collect_translation_info_op_t. :-(
889 reg_t base
= sptbr
<< PGSHIFT
;
890 int ptshift
= (levels
- 1) * ptidxbits
;
891 for (unsigned int i
= 0; i
< levels
; i
++, ptshift
-= ptidxbits
) {
892 reg_t idx
= (vaddr
>> (PGSHIFT
+ ptshift
)) & ((1 << ptidxbits
) - 1);
894 reg_t pte_addr
= base
+ idx
* ptesize
;
895 auto it
= pte_cache
.find(pte_addr
);
896 if (it
== pte_cache
.end()) {
897 fprintf(stderr
, "ERROR: gdbserver tried to translate 0x%lx without first "
898 "collecting the relevant PTEs.\n", vaddr
);
899 die("gdbserver_t::translate()");
902 reg_t pte
= pte_cache
[pte_addr
];
903 reg_t ppn
= pte
>> PTE_PPN_SHIFT
;
905 if (PTE_TABLE(pte
)) { // next level of page table
906 base
= ppn
<< PGSHIFT
;
908 // We've collected all the data required for the translation.
909 reg_t vpn
= vaddr
>> PGSHIFT
;
910 reg_t paddr
= (ppn
| (vpn
& ((reg_t(1) << ptshift
) - 1))) << PGSHIFT
;
911 paddr
+= vaddr
& (PGSIZE
-1);
912 fprintf(stderr
, "gdbserver translate 0x%lx -> 0x%lx\n", vaddr
, paddr
);
917 fprintf(stderr
, "ERROR: gdbserver tried to translate 0x%lx but the relevant "
918 "PTEs are invalid.\n", vaddr
);
919 // TODO: Is it better to throw an exception here?
923 unsigned int gdbserver_t::privilege_mode()
925 unsigned int mode
= get_field(dcsr
, DCSR_PRV
);
926 if (get_field(saved_mstatus
, MSTATUS_MPRV
))
927 mode
= get_field(saved_mstatus
, MSTATUS_MPP
);
931 unsigned int gdbserver_t::virtual_memory()
933 unsigned int mode
= privilege_mode();
936 return get_field(saved_mstatus
, MSTATUS_VM
);
939 void gdbserver_t::write_debug_ram(unsigned int index
, uint32_t value
)
941 sim
->debug_module
.ram_write32(index
, value
);
944 uint32_t gdbserver_t::read_debug_ram(unsigned int index
)
946 return sim
->debug_module
.ram_read32(index
);
949 void gdbserver_t::add_operation(operation_t
* operation
)
951 operation_queue
.push(operation
);
954 void gdbserver_t::accept()
956 client_fd
= ::accept(socket_fd
, NULL
, NULL
);
957 if (client_fd
== -1) {
958 if (errno
== EAGAIN
) {
959 // No client waiting to connect right now.
961 fprintf(stderr
, "failed to accept on socket: %s (%d)\n", strerror(errno
),
966 fcntl(client_fd
, F_SETFL
, O_NONBLOCK
);
969 extended_mode
= false;
971 // gdb wants the core to be halted when it attaches.
972 add_operation(new halt_op_t(*this));
976 void gdbserver_t::read()
978 // Reading from a non-blocking socket still blocks if there is no data
981 size_t count
= recv_buf
.contiguous_empty_size();
983 ssize_t bytes
= ::read(client_fd
, recv_buf
.contiguous_empty(), count
);
985 if (errno
== EAGAIN
) {
986 // We'll try again the next call.
988 fprintf(stderr
, "failed to read on socket: %s (%d)\n", strerror(errno
), errno
);
991 } else if (bytes
== 0) {
992 // The remote disconnected.
994 processor_t
*p
= sim
->get_core(0);
995 // TODO p->set_halted(false, HR_NONE);
999 recv_buf
.data_added(bytes
);
1003 void gdbserver_t::write()
1005 if (send_buf
.empty())
1008 while (!send_buf
.empty()) {
1009 unsigned int count
= send_buf
.contiguous_data_size();
1011 ssize_t bytes
= ::write(client_fd
, send_buf
.contiguous_data(), count
);
1013 fprintf(stderr
, "failed to write to socket: %s (%d)\n", strerror(errno
), errno
);
1015 } else if (bytes
== 0) {
1016 // Client can't take any more data right now.
1019 fprintf(stderr
, "wrote %ld bytes: ", bytes
);
1020 for (unsigned int i
= 0; i
< bytes
; i
++) {
1021 fprintf(stderr
, "%c", send_buf
[i
]);
1023 fprintf(stderr
, "\n");
1024 send_buf
.consume(bytes
);
1029 void print_packet(const std::vector
<uint8_t> &packet
)
1031 for (uint8_t c
: packet
) {
1032 if (c
>= ' ' and c
<= '~')
1033 fprintf(stderr
, "%c", c
);
1035 fprintf(stderr
, "\\x%x", c
);
1037 fprintf(stderr
, "\n");
1040 uint8_t compute_checksum(const std::vector
<uint8_t> &packet
)
1042 uint8_t checksum
= 0;
1043 for (auto i
= packet
.begin() + 1; i
!= packet
.end() - 3; i
++ ) {
1049 uint8_t character_hex_value(uint8_t character
)
1051 if (character
>= '0' && character
<= '9')
1052 return character
- '0';
1053 if (character
>= 'a' && character
<= 'f')
1054 return 10 + character
- 'a';
1055 if (character
>= 'A' && character
<= 'F')
1056 return 10 + character
- 'A';
1060 uint8_t extract_checksum(const std::vector
<uint8_t> &packet
)
1062 return character_hex_value(*(packet
.end() - 1)) +
1063 16 * character_hex_value(*(packet
.end() - 2));
1066 void gdbserver_t::process_requests()
1068 // See https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html
1070 while (!recv_buf
.empty()) {
1071 std::vector
<uint8_t> packet
;
1072 for (unsigned int i
= 0; i
< recv_buf
.size(); i
++) {
1073 uint8_t b
= recv_buf
[i
];
1075 if (packet
.empty() && expect_ack
&& b
== '+') {
1076 recv_buf
.consume(1);
1080 if (packet
.empty() && b
== 3) {
1081 fprintf(stderr
, "Received interrupt\n");
1082 recv_buf
.consume(1);
1088 // Start of new packet.
1089 if (!packet
.empty()) {
1090 fprintf(stderr
, "Received malformed %ld-byte packet from debug client: ",
1092 print_packet(packet
);
1093 recv_buf
.consume(i
);
1098 packet
.push_back(b
);
1100 // Packets consist of $<packet-data>#<checksum>
1101 // where <checksum> is
1102 if (packet
.size() >= 4 &&
1103 packet
[packet
.size()-3] == '#') {
1104 handle_packet(packet
);
1105 recv_buf
.consume(i
+1);
1109 // There's a partial packet in the buffer. Wait until we get more data to
1111 if (packet
.size()) {
1117 void gdbserver_t::handle_halt_reason(const std::vector
<uint8_t> &packet
)
1122 void gdbserver_t::handle_general_registers_read(const std::vector
<uint8_t> &packet
)
1124 add_operation(new general_registers_read_op_t(*this));
1127 void gdbserver_t::set_interrupt(uint32_t hartid
) {
1128 sim
->debug_module
.set_interrupt(hartid
);
1131 // First byte is the most-significant one.
1132 // Eg. "08675309" becomes 0x08675309.
1133 uint64_t consume_hex_number(std::vector
<uint8_t>::const_iterator
&iter
,
1134 std::vector
<uint8_t>::const_iterator end
)
1138 while (iter
!= end
) {
1140 uint64_t c_value
= character_hex_value(c
);
1150 // First byte is the least-significant one.
1151 // Eg. "08675309" becomes 0x09536708
1152 uint64_t consume_hex_number_le(std::vector
<uint8_t>::const_iterator
&iter
,
1153 std::vector
<uint8_t>::const_iterator end
)
1156 unsigned int shift
= 4;
1158 while (iter
!= end
) {
1160 uint64_t c_value
= character_hex_value(c
);
1164 value
|= c_value
<< shift
;
1165 if ((shift
% 8) == 0)
1173 void consume_string(std::string
&str
, std::vector
<uint8_t>::const_iterator
&iter
,
1174 std::vector
<uint8_t>::const_iterator end
, uint8_t separator
)
1176 while (iter
!= end
&& *iter
!= separator
) {
1177 str
.append(1, (char) *iter
);
1182 void gdbserver_t::handle_register_read(const std::vector
<uint8_t> &packet
)
1186 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1187 unsigned int n
= consume_hex_number(iter
, packet
.end());
1189 return send_packet("E01");
1191 add_operation(new register_read_op_t(*this, n
));
1194 void gdbserver_t::handle_register_write(const std::vector
<uint8_t> &packet
)
1198 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1199 unsigned int n
= consume_hex_number(iter
, packet
.end());
1201 return send_packet("E05");
1204 reg_t value
= consume_hex_number_le(iter
, packet
.end());
1206 return send_packet("E06");
1208 processor_t
*p
= sim
->get_core(0);
1210 die("handle_register_write");
1212 if (n >= REG_XPR0 && n <= REG_XPR31) {
1213 p->state.XPR.write(n - REG_XPR0, value);
1214 } else if (n == REG_PC) {
1215 p->state.pc = value;
1216 } else if (n >= REG_FPR0 && n <= REG_FPR31) {
1217 p->state.FPR.write(n - REG_FPR0, value);
1218 } else if (n >= REG_CSR0 && n <= REG_CSR4095) {
1220 p->set_csr(n - REG_CSR0, value);
1221 } catch(trap_t& t) {
1222 return send_packet("EFF");
1225 return send_packet("E07");
1229 return send_packet("OK");
1232 void gdbserver_t::handle_memory_read(const std::vector
<uint8_t> &packet
)
1235 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1236 reg_t address
= consume_hex_number(iter
, packet
.end());
1238 return send_packet("E10");
1240 reg_t length
= consume_hex_number(iter
, packet
.end());
1242 return send_packet("E11");
1244 add_operation(new collect_translation_info_op_t(*this, address
, length
));
1245 add_operation(new memory_read_op_t(*this, address
, length
));
1248 void gdbserver_t::handle_memory_binary_write(const std::vector
<uint8_t> &packet
)
1250 // X addr,length:XX...
1251 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1252 reg_t address
= consume_hex_number(iter
, packet
.end());
1254 return send_packet("E20");
1256 reg_t length
= consume_hex_number(iter
, packet
.end());
1258 return send_packet("E21");
1262 return send_packet("OK");
1265 unsigned char *data
= new unsigned char[length
];
1266 for (unsigned int i
= 0; i
< length
; i
++) {
1267 if (iter
== packet
.end()) {
1268 return send_packet("E22");
1274 return send_packet("E4b"); // EOVERFLOW
1276 add_operation(new collect_translation_info_op_t(*this, address
, length
));
1277 add_operation(new memory_write_op_t(*this, address
, length
, data
));
1280 void gdbserver_t::handle_continue(const std::vector
<uint8_t> &packet
)
1283 processor_t
*p
= sim
->get_core(0);
1284 if (packet
[2] != '#') {
1285 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1286 saved_dpc
= consume_hex_number(iter
, packet
.end());
1288 return send_packet("E30");
1291 add_operation(new continue_op_t(*this, false));
1294 void gdbserver_t::handle_step(const std::vector
<uint8_t> &packet
)
1297 if (packet
[2] != '#') {
1298 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1300 //p->state.pc = consume_hex_number(iter, packet.end());
1302 return send_packet("E40");
1305 add_operation(new continue_op_t(*this, true));
1308 void gdbserver_t::handle_kill(const std::vector
<uint8_t> &packet
)
1311 // The exact effect of this packet is not specified.
1312 // Looks like OpenOCD disconnects?
1316 void gdbserver_t::handle_extended(const std::vector
<uint8_t> &packet
)
1318 // Enable extended mode. In extended mode, the remote server is made
1319 // persistent. The ‘R’ packet is used to restart the program being debugged.
1321 extended_mode
= true;
1324 void gdbserver_t::handle_breakpoint(const std::vector
<uint8_t> &packet
)
1326 // insert: Z type,addr,kind
1327 // remove: z type,addr,kind
1329 software_breakpoint_t bp
;
1330 bool insert
= (packet
[1] == 'Z');
1331 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1332 int type
= consume_hex_number(iter
, packet
.end());
1334 return send_packet("E50");
1336 bp
.address
= consume_hex_number(iter
, packet
.end());
1338 return send_packet("E51");
1340 bp
.size
= consume_hex_number(iter
, packet
.end());
1341 // There may be more options after a ; here, but we don't support that.
1343 return send_packet("E52");
1345 if (bp
.size
!= 2 && bp
.size
!= 4) {
1346 return send_packet("E53");
1349 add_operation(new collect_translation_info_op_t(*this, bp
.address
, bp
.size
));
1351 // TODO: this only works on little-endian hosts.
1352 unsigned char* swbp
= new unsigned char[4];
1354 swbp
[0] = C_EBREAK
& 0xff;
1355 swbp
[1] = (C_EBREAK
>> 8) & 0xff;
1357 swbp
[0] = EBREAK
& 0xff;
1358 swbp
[1] = (EBREAK
>> 8) & 0xff;
1359 swbp
[2] = (EBREAK
>> 16) & 0xff;
1360 swbp
[3] = (EBREAK
>> 24) & 0xff;
1363 breakpoints
[bp
.address
] = new software_breakpoint_t(bp
);
1364 add_operation(new memory_read_op_t(*this, bp
.address
, bp
.size
,
1365 breakpoints
[bp
.address
]->instruction
));
1366 add_operation(new memory_write_op_t(*this, bp
.address
, bp
.size
, swbp
));
1369 software_breakpoint_t
*found_bp
;
1370 found_bp
= breakpoints
[bp
.address
];
1371 unsigned char* instruction
= new unsigned char[4];
1372 memcpy(instruction
, found_bp
->instruction
, 4);
1373 add_operation(new memory_write_op_t(*this, found_bp
->address
,
1374 found_bp
->size
, instruction
));
1375 breakpoints
.erase(bp
.address
);
1379 // TODO mmu->flush_icache();
1380 // TODO sim->debug_mmu->flush_icache();
1382 return send_packet("OK");
1385 void gdbserver_t::handle_query(const std::vector
<uint8_t> &packet
)
1388 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1390 consume_string(name
, iter
, packet
.end(), ':');
1391 if (iter
!= packet
.end())
1393 if (name
== "Supported") {
1395 while (iter
!= packet
.end()) {
1396 std::string feature
;
1397 consume_string(feature
, iter
, packet
.end(), ';');
1398 if (iter
!= packet
.end())
1400 if (feature
== "swbreak+") {
1404 return end_packet();
1407 fprintf(stderr
, "Unsupported query %s\n", name
.c_str());
1408 return send_packet("");
1411 void gdbserver_t::handle_packet(const std::vector
<uint8_t> &packet
)
1413 if (compute_checksum(packet
) != extract_checksum(packet
)) {
1414 fprintf(stderr
, "Received %ld-byte packet with invalid checksum\n", packet
.size());
1415 fprintf(stderr
, "Computed checksum: %x\n", compute_checksum(packet
));
1416 print_packet(packet
);
1421 fprintf(stderr
, "Received %ld-byte packet from debug client: ", packet
.size());
1422 print_packet(packet
);
1425 switch (packet
[1]) {
1427 return handle_extended(packet
);
1429 return handle_halt_reason(packet
);
1431 return handle_general_registers_read(packet
);
1433 return handle_kill(packet
);
1435 return handle_memory_read(packet
);
1437 // return handle_memory_write(packet);
1439 return handle_memory_binary_write(packet
);
1441 return handle_register_read(packet
);
1443 return handle_register_write(packet
);
1445 return handle_continue(packet
);
1447 return handle_step(packet
);
1450 return handle_breakpoint(packet
);
1453 return handle_query(packet
);
1457 fprintf(stderr
, "** Unsupported packet: ");
1458 print_packet(packet
);
1462 void gdbserver_t::handle_interrupt()
1464 processor_t
*p
= sim
->get_core(0);
1465 // TODO p->set_halted(true, HR_INTERRUPT);
1466 send_packet("S02"); // Pretend program received SIGINT.
1467 // TODO running = false;
1470 void gdbserver_t::handle()
1472 if (client_fd
> 0) {
1473 processor_t
*p
= sim
->get_core(0);
1475 bool interrupt
= sim
->debug_module
.get_interrupt(0);
1477 if (!interrupt
&& !operation_queue
.empty()) {
1478 operation_t
*operation
= operation_queue
.front();
1479 if (operation
->step()) {
1480 operation_queue
.pop();
1485 bool halt_notification
= sim
->debug_module
.get_halt_notification(0);
1486 if (halt_notification
) {
1487 sim
->debug_module
.clear_halt_notification(0);
1488 add_operation(new halt_op_t(*this, true));
1498 if (operation_queue
.empty()) {
1499 this->process_requests();
1503 void gdbserver_t::send(const char* msg
)
1505 unsigned int length
= strlen(msg
);
1506 for (const char *c
= msg
; *c
; c
++)
1507 running_checksum
+= *c
;
1508 send_buf
.append((const uint8_t *) msg
, length
);
1511 void gdbserver_t::send(uint64_t value
)
1514 for (unsigned int i
= 0; i
< 8; i
++) {
1515 sprintf(buffer
, "%02x", (int) (value
& 0xff));
1521 void gdbserver_t::send(uint32_t value
)
1524 for (unsigned int i
= 0; i
< 4; i
++) {
1525 sprintf(buffer
, "%02x", (int) (value
& 0xff));
1531 void gdbserver_t::send_packet(const char* data
)
1539 void gdbserver_t::start_packet()
1542 running_checksum
= 0;
1545 void gdbserver_t::end_packet(const char* data
)
1551 char checksum_string
[4];
1552 sprintf(checksum_string
, "#%02x", running_checksum
);
1553 send(checksum_string
);