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");
488 gs
.send(((uint64_t) gs
.read_debug_ram(5) << 32) | gs
.read_debug_ram(4));
499 class memory_read_op_t
: public operation_t
502 // Read length bytes from vaddr, storing the result into data.
503 // If data is NULL, send the result straight to gdb.
504 memory_read_op_t(gdbserver_t
& gdbserver
, reg_t vaddr
, unsigned int length
,
505 unsigned char *data
=NULL
) :
506 operation_t(gdbserver
), vaddr(vaddr
), length(length
), data(data
) {};
508 bool perform_step(unsigned int step
)
511 // address goes in S0
512 paddr
= gs
.translate(vaddr
);
513 access_size
= (paddr
% length
);
514 if (access_size
== 0)
515 access_size
= length
;
519 gs
.write_debug_ram(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
520 switch (access_size
) {
522 gs
.write_debug_ram(1, lb(S1
, S0
, 0));
525 gs
.write_debug_ram(1, lh(S1
, S0
, 0));
528 gs
.write_debug_ram(1, lw(S1
, S0
, 0));
531 gs
.write_debug_ram(1, ld(S1
, S0
, 0));
534 gs
.write_debug_ram(2, sd(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 24));
535 gs
.write_debug_ram(3, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*3))));
536 gs
.write_debug_ram(4, paddr
);
537 gs
.write_debug_ram(5, paddr
>> 32);
547 reg_t value
= ((uint64_t) gs
.read_debug_ram(7) << 32) | gs
.read_debug_ram(6);
548 for (unsigned int i
= 0; i
< access_size
; i
++) {
550 *(data
++) = value
& 0xff;
551 fprintf(stderr
, "%02x", (unsigned int) (value
& 0xff));
553 sprintf(buffer
, "%02x", (unsigned int) (value
& 0xff));
559 fprintf(stderr
, "\n");
560 length
-= access_size
;
561 paddr
+= access_size
;
569 gs
.write_debug_ram(4, paddr
);
570 gs
.write_debug_ram(5, paddr
>> 32);
581 unsigned int access_size
;
584 class memory_write_op_t
: public operation_t
587 memory_write_op_t(gdbserver_t
& gdbserver
, reg_t vaddr
, unsigned int length
,
588 const unsigned char *data
) :
589 operation_t(gdbserver
), vaddr(vaddr
), offset(0), length(length
), data(data
) {};
591 ~memory_write_op_t() {
595 bool perform_step(unsigned int step
)
597 reg_t paddr
= gs
.translate(vaddr
);
599 // address goes in S0
600 access_size
= (paddr
% length
);
601 if (access_size
== 0)
602 access_size
= length
;
604 fprintf(stderr
, "write to 0x%lx -> 0x%lx: ", vaddr
, paddr
);
605 for (unsigned int i
= 0; i
< length
; i
++)
606 fprintf(stderr
, "%02x", data
[i
]);
607 fprintf(stderr
, "\n");
609 gs
.write_debug_ram(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
610 switch (access_size
) {
612 gs
.write_debug_ram(1, lb(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 24));
613 gs
.write_debug_ram(2, sb(S1
, S0
, 0));
614 gs
.write_debug_ram(6, data
[0]);
617 gs
.write_debug_ram(1, lh(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 24));
618 gs
.write_debug_ram(2, sh(S1
, S0
, 0));
619 gs
.write_debug_ram(6, data
[0] | (data
[1] << 8));
622 gs
.write_debug_ram(1, lw(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 24));
623 gs
.write_debug_ram(2, sw(S1
, S0
, 0));
624 gs
.write_debug_ram(6, data
[0] | (data
[1] << 8) |
625 (data
[2] << 16) | (data
[3] << 24));
628 gs
.write_debug_ram(1, ld(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 24));
629 gs
.write_debug_ram(2, sd(S1
, S0
, 0));
630 gs
.write_debug_ram(6, data
[0] | (data
[1] << 8) |
631 (data
[2] << 16) | (data
[3] << 24));
632 gs
.write_debug_ram(7, data
[4] | (data
[5] << 8) |
633 (data
[6] << 16) | (data
[7] << 24));
636 gs
.send_packet("E12");
639 gs
.write_debug_ram(3, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*3))));
640 gs
.write_debug_ram(4, paddr
);
641 gs
.write_debug_ram(5, paddr
>> 32);
647 offset
+= access_size
;
648 if (offset
>= length
) {
649 gs
.send_packet("OK");
652 const unsigned char *d
= data
+ offset
;
653 switch (access_size
) {
655 gs
.write_debug_ram(6, d
[0]);
658 gs
.write_debug_ram(6, d
[0] | (d
[1] << 8));
661 gs
.write_debug_ram(6, d
[0] | (d
[1] << 8) |
662 (d
[2] << 16) | (d
[3] << 24));
665 gs
.write_debug_ram(6, d
[0] | (d
[1] << 8) |
666 (d
[2] << 16) | (d
[3] << 24));
667 gs
.write_debug_ram(7, d
[4] | (d
[5] << 8) |
668 (d
[6] << 16) | (d
[7] << 24));
671 gs
.send_packet("E12");
674 gs
.write_debug_ram(4, paddr
+ offset
);
675 gs
.write_debug_ram(5, (paddr
+ offset
) >> 32);
685 unsigned int access_size
;
686 const unsigned char *data
;
689 class collect_translation_info_op_t
: public operation_t
692 // Read sufficient information from the target into gdbserver structures so
693 // that it's possible to translate vaddr, vaddr+length, and all addresses
694 // in between to physical addresses.
695 collect_translation_info_op_t(gdbserver_t
& gdbserver
, reg_t vaddr
, size_t length
) :
696 operation_t(gdbserver
), state(STATE_START
), vaddr(vaddr
), length(length
) {};
698 bool perform_step(unsigned int step
)
700 unsigned int vm
= gs
.virtual_memory();
705 // Nothing to be done.
727 sprintf(buf
, "VM mode %d is not supported by gdbserver.cc.", vm
);
729 return true; // die doesn't return, but gcc doesn't know that.
734 // Perform any reads from the just-completed action.
738 case STATE_READ_SPTBR
:
739 gs
.sptbr
= ((uint64_t) gs
.read_debug_ram(5) << 32) | gs
.read_debug_ram(4);
740 gs
.sptbr_valid
= true;
743 gs
.pte_cache
[pte_addr
] = ((uint64_t) gs
.read_debug_ram(5) << 32) |
744 gs
.read_debug_ram(4);
745 fprintf(stderr
, "pte_cache[0x%lx] = 0x%lx\n", pte_addr
, gs
.pte_cache
[pte_addr
]);
749 // Set up the next action.
750 // We only get here for VM_SV32/39/38.
752 if (!gs
.sptbr_valid
) {
753 state
= STATE_READ_SPTBR
;
754 gs
.write_debug_ram(0, csrr(S0
, CSR_SPTBR
));
755 gs
.write_debug_ram(1, sd(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
756 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
761 reg_t base
= gs
.sptbr
<< PGSHIFT
;
762 int ptshift
= (levels
- 1) * ptidxbits
;
763 for (unsigned int i
= 0; i
< levels
; i
++, ptshift
-= ptidxbits
) {
764 reg_t idx
= (vaddr
>> (PGSHIFT
+ ptshift
)) & ((1 << ptidxbits
) - 1);
766 pte_addr
= base
+ idx
* ptesize
;
767 auto it
= gs
.pte_cache
.find(pte_addr
);
768 if (it
== gs
.pte_cache
.end()) {
769 state
= STATE_READ_PTE
;
771 gs
.write_debug_ram(0, lw(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
772 gs
.write_debug_ram(1, lw(S1
, S0
, 0));
773 gs
.write_debug_ram(2, sd(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
775 gs
.write_debug_ram(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
776 gs
.write_debug_ram(1, ld(S1
, S0
, 0));
777 gs
.write_debug_ram(2, sd(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
779 gs
.write_debug_ram(3, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*3))));
780 gs
.write_debug_ram(4, pte_addr
);
781 gs
.write_debug_ram(5, pte_addr
>> 32);
786 reg_t pte
= gs
.pte_cache
[pte_addr
];
787 reg_t ppn
= pte
>> PTE_PPN_SHIFT
;
789 if (PTE_TABLE(pte
)) { // next level of page table
790 base
= ppn
<< PGSHIFT
;
792 // We've collected all the data required for the translation.
797 "ERROR: gdbserver couldn't find appropriate PTEs to translate 0x%lx\n",
811 unsigned int ptidxbits
;
812 unsigned int ptesize
;
816 ////////////////////////////// gdbserver itself
818 gdbserver_t::gdbserver_t(uint16_t port
, sim_t
*sim
) :
821 recv_buf(64 * 1024), send_buf(64 * 1024)
823 socket_fd
= socket(AF_INET
, SOCK_STREAM
, 0);
824 if (socket_fd
== -1) {
825 fprintf(stderr
, "failed to make socket: %s (%d)\n", strerror(errno
), errno
);
829 fcntl(socket_fd
, F_SETFL
, O_NONBLOCK
);
831 if (setsockopt(socket_fd
, SOL_SOCKET
, SO_REUSEADDR
, &reuseaddr
,
832 sizeof(int)) == -1) {
833 fprintf(stderr
, "failed setsockopt: %s (%d)\n", strerror(errno
), errno
);
837 struct sockaddr_in addr
;
838 memset(&addr
, 0, sizeof(addr
));
839 addr
.sin_family
= AF_INET
;
840 addr
.sin_addr
.s_addr
= INADDR_ANY
;
841 addr
.sin_port
= htons(port
);
843 if (bind(socket_fd
, (struct sockaddr
*) &addr
, sizeof(addr
)) == -1) {
844 fprintf(stderr
, "failed to bind socket: %s (%d)\n", strerror(errno
), errno
);
848 if (listen(socket_fd
, 1) == -1) {
849 fprintf(stderr
, "failed to listen on socket: %s (%d)\n", strerror(errno
), errno
);
854 reg_t
gdbserver_t::translate(reg_t vaddr
)
856 unsigned int vm
= virtual_memory();
857 unsigned int levels
, ptidxbits
, ptesize
;
882 sprintf(buf
, "VM mode %d is not supported by gdbserver.cc.", vm
);
884 return true; // die doesn't return, but gcc doesn't know that.
888 // Handle page tables here. There's a bunch of duplicated code with
889 // collect_translation_info_op_t. :-(
890 reg_t base
= sptbr
<< PGSHIFT
;
891 int ptshift
= (levels
- 1) * ptidxbits
;
892 for (unsigned int i
= 0; i
< levels
; i
++, ptshift
-= ptidxbits
) {
893 reg_t idx
= (vaddr
>> (PGSHIFT
+ ptshift
)) & ((1 << ptidxbits
) - 1);
895 reg_t pte_addr
= base
+ idx
* ptesize
;
896 auto it
= pte_cache
.find(pte_addr
);
897 if (it
== pte_cache
.end()) {
898 fprintf(stderr
, "ERROR: gdbserver tried to translate 0x%lx without first "
899 "collecting the relevant PTEs.\n", vaddr
);
900 die("gdbserver_t::translate()");
903 reg_t pte
= pte_cache
[pte_addr
];
904 reg_t ppn
= pte
>> PTE_PPN_SHIFT
;
906 if (PTE_TABLE(pte
)) { // next level of page table
907 base
= ppn
<< PGSHIFT
;
909 // We've collected all the data required for the translation.
910 reg_t vpn
= vaddr
>> PGSHIFT
;
911 reg_t paddr
= (ppn
| (vpn
& ((reg_t(1) << ptshift
) - 1))) << PGSHIFT
;
912 paddr
+= vaddr
& (PGSIZE
-1);
913 fprintf(stderr
, "gdbserver translate 0x%lx -> 0x%lx\n", vaddr
, paddr
);
918 fprintf(stderr
, "ERROR: gdbserver tried to translate 0x%lx but the relevant "
919 "PTEs are invalid.\n", vaddr
);
920 // TODO: Is it better to throw an exception here?
924 unsigned int gdbserver_t::privilege_mode()
926 unsigned int mode
= get_field(dcsr
, DCSR_PRV
);
927 if (get_field(saved_mstatus
, MSTATUS_MPRV
))
928 mode
= get_field(saved_mstatus
, MSTATUS_MPP
);
932 unsigned int gdbserver_t::virtual_memory()
934 unsigned int mode
= privilege_mode();
937 return get_field(saved_mstatus
, MSTATUS_VM
);
940 void gdbserver_t::write_debug_ram(unsigned int index
, uint32_t value
)
942 sim
->debug_module
.ram_write32(index
, value
);
945 uint32_t gdbserver_t::read_debug_ram(unsigned int index
)
947 return sim
->debug_module
.ram_read32(index
);
950 void gdbserver_t::add_operation(operation_t
* operation
)
952 operation_queue
.push(operation
);
955 void gdbserver_t::accept()
957 client_fd
= ::accept(socket_fd
, NULL
, NULL
);
958 if (client_fd
== -1) {
959 if (errno
== EAGAIN
) {
960 // No client waiting to connect right now.
962 fprintf(stderr
, "failed to accept on socket: %s (%d)\n", strerror(errno
),
967 fcntl(client_fd
, F_SETFL
, O_NONBLOCK
);
970 extended_mode
= false;
972 // gdb wants the core to be halted when it attaches.
973 add_operation(new halt_op_t(*this));
977 void gdbserver_t::read()
979 // Reading from a non-blocking socket still blocks if there is no data
982 size_t count
= recv_buf
.contiguous_empty_size();
984 ssize_t bytes
= ::read(client_fd
, recv_buf
.contiguous_empty(), count
);
986 if (errno
== EAGAIN
) {
987 // We'll try again the next call.
989 fprintf(stderr
, "failed to read on socket: %s (%d)\n", strerror(errno
), errno
);
992 } else if (bytes
== 0) {
993 // The remote disconnected.
995 processor_t
*p
= sim
->get_core(0);
996 // TODO p->set_halted(false, HR_NONE);
1000 recv_buf
.data_added(bytes
);
1004 void gdbserver_t::write()
1006 if (send_buf
.empty())
1009 while (!send_buf
.empty()) {
1010 unsigned int count
= send_buf
.contiguous_data_size();
1012 ssize_t bytes
= ::write(client_fd
, send_buf
.contiguous_data(), count
);
1014 fprintf(stderr
, "failed to write to socket: %s (%d)\n", strerror(errno
), errno
);
1016 } else if (bytes
== 0) {
1017 // Client can't take any more data right now.
1020 fprintf(stderr
, "wrote %ld bytes: ", bytes
);
1021 for (unsigned int i
= 0; i
< bytes
; i
++) {
1022 fprintf(stderr
, "%c", send_buf
[i
]);
1024 fprintf(stderr
, "\n");
1025 send_buf
.consume(bytes
);
1030 void print_packet(const std::vector
<uint8_t> &packet
)
1032 for (uint8_t c
: packet
) {
1033 if (c
>= ' ' and c
<= '~')
1034 fprintf(stderr
, "%c", c
);
1036 fprintf(stderr
, "\\x%x", c
);
1038 fprintf(stderr
, "\n");
1041 uint8_t compute_checksum(const std::vector
<uint8_t> &packet
)
1043 uint8_t checksum
= 0;
1044 for (auto i
= packet
.begin() + 1; i
!= packet
.end() - 3; i
++ ) {
1050 uint8_t character_hex_value(uint8_t character
)
1052 if (character
>= '0' && character
<= '9')
1053 return character
- '0';
1054 if (character
>= 'a' && character
<= 'f')
1055 return 10 + character
- 'a';
1056 if (character
>= 'A' && character
<= 'F')
1057 return 10 + character
- 'A';
1061 uint8_t extract_checksum(const std::vector
<uint8_t> &packet
)
1063 return character_hex_value(*(packet
.end() - 1)) +
1064 16 * character_hex_value(*(packet
.end() - 2));
1067 void gdbserver_t::process_requests()
1069 // See https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html
1071 while (!recv_buf
.empty()) {
1072 std::vector
<uint8_t> packet
;
1073 for (unsigned int i
= 0; i
< recv_buf
.size(); i
++) {
1074 uint8_t b
= recv_buf
[i
];
1076 if (packet
.empty() && expect_ack
&& b
== '+') {
1077 recv_buf
.consume(1);
1081 if (packet
.empty() && b
== 3) {
1082 fprintf(stderr
, "Received interrupt\n");
1083 recv_buf
.consume(1);
1089 // Start of new packet.
1090 if (!packet
.empty()) {
1091 fprintf(stderr
, "Received malformed %ld-byte packet from debug client: ",
1093 print_packet(packet
);
1094 recv_buf
.consume(i
);
1099 packet
.push_back(b
);
1101 // Packets consist of $<packet-data>#<checksum>
1102 // where <checksum> is
1103 if (packet
.size() >= 4 &&
1104 packet
[packet
.size()-3] == '#') {
1105 handle_packet(packet
);
1106 recv_buf
.consume(i
+1);
1110 // There's a partial packet in the buffer. Wait until we get more data to
1112 if (packet
.size()) {
1118 void gdbserver_t::handle_halt_reason(const std::vector
<uint8_t> &packet
)
1123 void gdbserver_t::handle_general_registers_read(const std::vector
<uint8_t> &packet
)
1125 add_operation(new general_registers_read_op_t(*this));
1128 void gdbserver_t::set_interrupt(uint32_t hartid
) {
1129 sim
->debug_module
.set_interrupt(hartid
);
1132 // First byte is the most-significant one.
1133 // Eg. "08675309" becomes 0x08675309.
1134 uint64_t consume_hex_number(std::vector
<uint8_t>::const_iterator
&iter
,
1135 std::vector
<uint8_t>::const_iterator end
)
1139 while (iter
!= end
) {
1141 uint64_t c_value
= character_hex_value(c
);
1151 // First byte is the least-significant one.
1152 // Eg. "08675309" becomes 0x09536708
1153 uint64_t consume_hex_number_le(std::vector
<uint8_t>::const_iterator
&iter
,
1154 std::vector
<uint8_t>::const_iterator end
)
1157 unsigned int shift
= 4;
1159 while (iter
!= end
) {
1161 uint64_t c_value
= character_hex_value(c
);
1165 value
|= c_value
<< shift
;
1166 if ((shift
% 8) == 0)
1174 void consume_string(std::string
&str
, std::vector
<uint8_t>::const_iterator
&iter
,
1175 std::vector
<uint8_t>::const_iterator end
, uint8_t separator
)
1177 while (iter
!= end
&& *iter
!= separator
) {
1178 str
.append(1, (char) *iter
);
1183 void gdbserver_t::handle_register_read(const std::vector
<uint8_t> &packet
)
1187 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1188 unsigned int n
= consume_hex_number(iter
, packet
.end());
1190 return send_packet("E01");
1192 add_operation(new register_read_op_t(*this, n
));
1195 void gdbserver_t::handle_register_write(const std::vector
<uint8_t> &packet
)
1199 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1200 unsigned int n
= consume_hex_number(iter
, packet
.end());
1202 return send_packet("E05");
1205 reg_t value
= consume_hex_number_le(iter
, packet
.end());
1207 return send_packet("E06");
1209 processor_t
*p
= sim
->get_core(0);
1211 die("handle_register_write");
1213 if (n >= REG_XPR0 && n <= REG_XPR31) {
1214 p->state.XPR.write(n - REG_XPR0, value);
1215 } else if (n == REG_PC) {
1216 p->state.pc = value;
1217 } else if (n >= REG_FPR0 && n <= REG_FPR31) {
1218 p->state.FPR.write(n - REG_FPR0, value);
1219 } else if (n >= REG_CSR0 && n <= REG_CSR4095) {
1221 p->set_csr(n - REG_CSR0, value);
1222 } catch(trap_t& t) {
1223 return send_packet("EFF");
1226 return send_packet("E07");
1230 return send_packet("OK");
1233 void gdbserver_t::handle_memory_read(const std::vector
<uint8_t> &packet
)
1236 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1237 reg_t address
= consume_hex_number(iter
, packet
.end());
1239 return send_packet("E10");
1241 reg_t length
= consume_hex_number(iter
, packet
.end());
1243 return send_packet("E11");
1245 add_operation(new collect_translation_info_op_t(*this, address
, length
));
1246 add_operation(new memory_read_op_t(*this, address
, length
));
1249 void gdbserver_t::handle_memory_binary_write(const std::vector
<uint8_t> &packet
)
1251 // X addr,length:XX...
1252 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1253 reg_t address
= consume_hex_number(iter
, packet
.end());
1255 return send_packet("E20");
1257 reg_t length
= consume_hex_number(iter
, packet
.end());
1259 return send_packet("E21");
1263 return send_packet("OK");
1266 unsigned char *data
= new unsigned char[length
];
1267 for (unsigned int i
= 0; i
< length
; i
++) {
1268 if (iter
== packet
.end()) {
1269 return send_packet("E22");
1275 return send_packet("E4b"); // EOVERFLOW
1277 add_operation(new collect_translation_info_op_t(*this, address
, length
));
1278 add_operation(new memory_write_op_t(*this, address
, length
, data
));
1281 void gdbserver_t::handle_continue(const std::vector
<uint8_t> &packet
)
1284 processor_t
*p
= sim
->get_core(0);
1285 if (packet
[2] != '#') {
1286 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1287 saved_dpc
= consume_hex_number(iter
, packet
.end());
1289 return send_packet("E30");
1292 add_operation(new continue_op_t(*this, false));
1295 void gdbserver_t::handle_step(const std::vector
<uint8_t> &packet
)
1298 if (packet
[2] != '#') {
1299 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1301 //p->state.pc = consume_hex_number(iter, packet.end());
1303 return send_packet("E40");
1306 add_operation(new continue_op_t(*this, true));
1309 void gdbserver_t::handle_kill(const std::vector
<uint8_t> &packet
)
1312 // The exact effect of this packet is not specified.
1313 // Looks like OpenOCD disconnects?
1317 void gdbserver_t::handle_extended(const std::vector
<uint8_t> &packet
)
1319 // Enable extended mode. In extended mode, the remote server is made
1320 // persistent. The ‘R’ packet is used to restart the program being debugged.
1322 extended_mode
= true;
1325 void gdbserver_t::handle_breakpoint(const std::vector
<uint8_t> &packet
)
1327 // insert: Z type,addr,kind
1328 // remove: z type,addr,kind
1330 software_breakpoint_t bp
;
1331 bool insert
= (packet
[1] == 'Z');
1332 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1333 int type
= consume_hex_number(iter
, packet
.end());
1335 return send_packet("E50");
1337 bp
.address
= consume_hex_number(iter
, packet
.end());
1339 return send_packet("E51");
1341 bp
.size
= consume_hex_number(iter
, packet
.end());
1342 // There may be more options after a ; here, but we don't support that.
1344 return send_packet("E52");
1346 if (bp
.size
!= 2 && bp
.size
!= 4) {
1347 return send_packet("E53");
1350 add_operation(new collect_translation_info_op_t(*this, bp
.address
, bp
.size
));
1352 // TODO: this only works on little-endian hosts.
1353 unsigned char* swbp
= new unsigned char[4];
1355 swbp
[0] = C_EBREAK
& 0xff;
1356 swbp
[1] = (C_EBREAK
>> 8) & 0xff;
1358 swbp
[0] = EBREAK
& 0xff;
1359 swbp
[1] = (EBREAK
>> 8) & 0xff;
1360 swbp
[2] = (EBREAK
>> 16) & 0xff;
1361 swbp
[3] = (EBREAK
>> 24) & 0xff;
1364 breakpoints
[bp
.address
] = new software_breakpoint_t(bp
);
1365 add_operation(new memory_read_op_t(*this, bp
.address
, bp
.size
,
1366 breakpoints
[bp
.address
]->instruction
));
1367 add_operation(new memory_write_op_t(*this, bp
.address
, bp
.size
, swbp
));
1370 software_breakpoint_t
*found_bp
;
1371 found_bp
= breakpoints
[bp
.address
];
1372 unsigned char* instruction
= new unsigned char[4];
1373 memcpy(instruction
, found_bp
->instruction
, 4);
1374 add_operation(new memory_write_op_t(*this, found_bp
->address
,
1375 found_bp
->size
, instruction
));
1376 breakpoints
.erase(bp
.address
);
1380 // TODO mmu->flush_icache();
1381 // TODO sim->debug_mmu->flush_icache();
1383 return send_packet("OK");
1386 void gdbserver_t::handle_query(const std::vector
<uint8_t> &packet
)
1389 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1391 consume_string(name
, iter
, packet
.end(), ':');
1392 if (iter
!= packet
.end())
1394 if (name
== "Supported") {
1396 while (iter
!= packet
.end()) {
1397 std::string feature
;
1398 consume_string(feature
, iter
, packet
.end(), ';');
1399 if (iter
!= packet
.end())
1401 if (feature
== "swbreak+") {
1405 return end_packet();
1408 fprintf(stderr
, "Unsupported query %s\n", name
.c_str());
1409 return send_packet("");
1412 void gdbserver_t::handle_packet(const std::vector
<uint8_t> &packet
)
1414 if (compute_checksum(packet
) != extract_checksum(packet
)) {
1415 fprintf(stderr
, "Received %ld-byte packet with invalid checksum\n", packet
.size());
1416 fprintf(stderr
, "Computed checksum: %x\n", compute_checksum(packet
));
1417 print_packet(packet
);
1422 fprintf(stderr
, "Received %ld-byte packet from debug client: ", packet
.size());
1423 print_packet(packet
);
1426 switch (packet
[1]) {
1428 return handle_extended(packet
);
1430 return handle_halt_reason(packet
);
1432 return handle_general_registers_read(packet
);
1434 return handle_kill(packet
);
1436 return handle_memory_read(packet
);
1438 // return handle_memory_write(packet);
1440 return handle_memory_binary_write(packet
);
1442 return handle_register_read(packet
);
1444 return handle_register_write(packet
);
1446 return handle_continue(packet
);
1448 return handle_step(packet
);
1451 return handle_breakpoint(packet
);
1454 return handle_query(packet
);
1458 fprintf(stderr
, "** Unsupported packet: ");
1459 print_packet(packet
);
1463 void gdbserver_t::handle_interrupt()
1465 processor_t
*p
= sim
->get_core(0);
1466 // TODO p->set_halted(true, HR_INTERRUPT);
1467 send_packet("S02"); // Pretend program received SIGINT.
1468 // TODO running = false;
1471 void gdbserver_t::handle()
1473 if (client_fd
> 0) {
1474 processor_t
*p
= sim
->get_core(0);
1476 bool interrupt
= sim
->debug_module
.get_interrupt(0);
1478 if (!interrupt
&& !operation_queue
.empty()) {
1479 operation_t
*operation
= operation_queue
.front();
1480 if (operation
->step()) {
1481 operation_queue
.pop();
1486 bool halt_notification
= sim
->debug_module
.get_halt_notification(0);
1487 if (halt_notification
) {
1488 sim
->debug_module
.clear_halt_notification(0);
1489 add_operation(new halt_op_t(*this, true));
1499 if (operation_queue
.empty()) {
1500 this->process_requests();
1504 void gdbserver_t::send(const char* msg
)
1506 unsigned int length
= strlen(msg
);
1507 for (const char *c
= msg
; *c
; c
++)
1508 running_checksum
+= *c
;
1509 send_buf
.append((const uint8_t *) msg
, length
);
1512 void gdbserver_t::send(uint64_t value
)
1515 for (unsigned int i
= 0; i
< 8; i
++) {
1516 sprintf(buffer
, "%02x", (int) (value
& 0xff));
1522 void gdbserver_t::send(uint32_t value
)
1525 for (unsigned int i
= 0; i
< 4; i
++) {
1526 sprintf(buffer
, "%02x", (int) (value
& 0xff));
1532 void gdbserver_t::send_packet(const char* data
)
1540 void gdbserver_t::start_packet()
1543 running_checksum
= 0;
1546 void gdbserver_t::end_packet(const char* data
)
1552 char checksum_string
[4];
1553 sprintf(checksum_string
, "#%02x", running_checksum
);
1554 send(checksum_string
);