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
, uint8_t imm
) {
71 (bits(imm
, 4, 0) << 15) |
75 static uint32_t csrci(unsigned int csr
, uint8_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
) : operation_t(gdbserver
) {};
254 // TODO: For now we just assume the target is 64-bit.
255 gs
.write_debug_ram(0, csrsi(DCSR_ADDRESS
, DCSR_HALT_MASK
));
256 gs
.write_debug_ram(1, csrr(S0
, DPC_ADDRESS
));
257 gs
.write_debug_ram(2, sd(S0
, 0, (uint16_t) DEBUG_RAM_START
));
258 gs
.write_debug_ram(3, csrr(S0
, CSR_MBADADDR
));
259 gs
.write_debug_ram(4, sd(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 8));
260 gs
.write_debug_ram(5, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*5))));
262 // We could read mcause here as well, but only on 64-bit targets. I'm
263 // trying to keep The patterns here usable for 32-bit ISAs as well. (On a
264 // 32-bit ISA 8 words are required, while the minimum Debug RAM size is 7
272 if (state
== READ_DPC
) {
273 gs
.saved_dpc
= ((uint64_t) gs
.read_debug_ram(1) << 32) | gs
.read_debug_ram(0);
274 gs
.saved_mbadaddr
= ((uint64_t) gs
.read_debug_ram(3) << 32) | gs
.read_debug_ram(2);
276 gs
.write_debug_ram(0, csrr(S0
, CSR_MCAUSE
));
277 gs
.write_debug_ram(1, sd(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 0));
278 gs
.write_debug_ram(2, csrr(S0
, CSR_MSTATUS
));
279 gs
.write_debug_ram(3, sd(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 8));
280 gs
.write_debug_ram(4, csrr(S0
, CSR_DCSR
));
281 gs
.write_debug_ram(5, sd(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
282 gs
.write_debug_ram(6, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*6))));
288 gs
.saved_mcause
= ((uint64_t) gs
.read_debug_ram(1) << 32) | gs
.read_debug_ram(0);
289 gs
.saved_mstatus
= ((uint64_t) gs
.read_debug_ram(3) << 32) | gs
.read_debug_ram(2);
290 gs
.dcsr
= ((uint64_t) gs
.read_debug_ram(5) << 32) | gs
.read_debug_ram(4);
303 class continue_op_t
: public operation_t
306 continue_op_t(gdbserver_t
& gdbserver
) : operation_t(gdbserver
) {};
310 gs
.write_debug_ram(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+16));
311 gs
.write_debug_ram(1, csrw(S0
, DPC_ADDRESS
));
312 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
313 gs
.write_debug_ram(4, gs
.saved_dpc
);
314 gs
.write_debug_ram(5, gs
.saved_dpc
>> 32);
322 if (state
== WRITE_DPC
) {
323 gs
.write_debug_ram(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+16));
324 gs
.write_debug_ram(1, csrw(S0
, CSR_MBADADDR
));
325 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
326 gs
.write_debug_ram(4, gs
.saved_mbadaddr
);
327 gs
.write_debug_ram(5, gs
.saved_mbadaddr
>> 32);
329 state
= WRITE_MBADADDR
;
332 } else if (state
== WRITE_MBADADDR
) {
333 gs
.write_debug_ram(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+16));
334 gs
.write_debug_ram(1, csrw(S0
, CSR_MSTATUS
));
335 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
336 gs
.write_debug_ram(4, gs
.saved_mstatus
);
337 gs
.write_debug_ram(5, gs
.saved_mstatus
>> 32);
339 state
= WRITE_MSTATUS
;
343 gs
.write_debug_ram(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+16));
344 gs
.write_debug_ram(1, csrw(S0
, CSR_MCAUSE
));
345 gs
.write_debug_ram(2, csrci(DCSR_ADDRESS
, DCSR_HALT_MASK
));
346 gs
.write_debug_ram(3, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*3))));
347 gs
.write_debug_ram(4, gs
.saved_mcause
);
348 gs
.write_debug_ram(5, gs
.saved_mcause
>> 32);
362 class general_registers_read_op_t
: public operation_t
364 // Register order that gdb expects is:
365 // "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
366 // "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
367 // "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
368 // "x24", "x25", "x26", "x27", "x28", "x29", "x30", "x31",
370 // Each byte of register data is described by two hex digits. The bytes with
371 // the register are transmitted in target byte order. The size of each
372 // register and their position within the ‘g’ packet are determined by the
373 // gdb internal gdbarch functions DEPRECATED_REGISTER_RAW_SIZE and
374 // gdbarch_register_name.
377 general_registers_read_op_t(gdbserver_t
& gdbserver
) :
378 operation_t(gdbserver
), current_reg(0) {};
384 // x0 is always zero.
387 gs
.write_debug_ram(0, sd(1, 0, (uint16_t) DEBUG_RAM_START
+ 16));
388 gs
.write_debug_ram(1, sd(2, 0, (uint16_t) DEBUG_RAM_START
+ 0));
389 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
397 fprintf(stderr
, "step %d\n", current_reg
);
398 gs
.send(((uint64_t) gs
.read_debug_ram(5) << 32) | gs
.read_debug_ram(4));
399 if (current_reg
>= 31) {
404 gs
.send(((uint64_t) gs
.read_debug_ram(1) << 32) | gs
.read_debug_ram(0));
408 if (current_reg
== S1
) {
409 gs
.write_debug_ram(i
++, ld(S1
, 0, (uint16_t) DEBUG_RAM_END
- 8));
411 gs
.write_debug_ram(i
++, sd(current_reg
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
412 if (current_reg
+ 1 == S0
) {
413 gs
.write_debug_ram(i
++, csrr(S0
, CSR_DSCRATCH
));
415 gs
.write_debug_ram(i
++, sd(current_reg
+1, 0, (uint16_t) DEBUG_RAM_START
+ 0));
416 gs
.write_debug_ram(i
, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*i
))));
423 unsigned int current_reg
;
426 class register_read_op_t
: public operation_t
429 register_read_op_t(gdbserver_t
& gdbserver
, unsigned int reg
) :
430 operation_t(gdbserver
), reg(reg
) {};
434 if (reg
>= REG_XPR0
&& reg
<= REG_XPR31
) {
435 die("handle_register_read");
436 // send(p->state.XPR[reg - REG_XPR0]);
437 } else if (reg
== REG_PC
) {
439 gs
.send(gs
.saved_dpc
);
442 } else if (reg
>= REG_FPR0
&& reg
<= REG_FPR31
) {
443 // send(p->state.FPR[reg - REG_FPR0]);
444 gs
.write_debug_ram(0, fsd(reg
- REG_FPR0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
445 gs
.write_debug_ram(1, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*1))));
446 } else if (reg
== REG_CSR0
+ CSR_MBADADDR
) {
448 gs
.send(gs
.saved_mbadaddr
);
451 } else if (reg
== REG_CSR0
+ CSR_MCAUSE
) {
453 gs
.send(gs
.saved_mcause
);
456 } else if (reg
>= REG_CSR0
&& reg
<= REG_CSR4095
) {
457 gs
.write_debug_ram(0, csrr(S0
, reg
- REG_CSR0
));
458 gs
.write_debug_ram(1, sd(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
459 gs
.write_debug_ram(2, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*2))));
460 // If we hit an exception reading the CSR, we'll end up returning ~0 as
461 // the register's value, which is what we want. (Right?)
462 gs
.write_debug_ram(4, 0xffffffff);
463 gs
.write_debug_ram(5, 0xffffffff);
465 gs
.send_packet("E02");
477 gs
.send(((uint64_t) gs
.read_debug_ram(5) << 32) | gs
.read_debug_ram(4));
486 class memory_read_op_t
: public operation_t
489 memory_read_op_t(gdbserver_t
& gdbserver
, reg_t addr
, unsigned int length
) :
490 operation_t(gdbserver
), addr(addr
), length(length
) {};
494 // address goes in S0
495 access_size
= (addr
% length
);
496 if (access_size
== 0)
497 access_size
= length
;
499 gs
.write_debug_ram(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
500 switch (access_size
) {
502 gs
.write_debug_ram(1, lb(S1
, S0
, 0));
505 gs
.write_debug_ram(1, lh(S1
, S0
, 0));
508 gs
.write_debug_ram(1, lw(S1
, S0
, 0));
511 gs
.write_debug_ram(1, ld(S1
, S0
, 0));
514 gs
.send_packet("E12");
517 gs
.write_debug_ram(2, sd(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 24));
518 gs
.write_debug_ram(3, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*3))));
519 gs
.write_debug_ram(4, addr
);
520 gs
.write_debug_ram(5, addr
>> 32);
531 reg_t value
= ((uint64_t) gs
.read_debug_ram(7) << 32) | gs
.read_debug_ram(6);
532 for (unsigned int i
= 0; i
< access_size
; i
++) {
533 sprintf(buffer
, "%02x", (unsigned int) (value
& 0xff));
537 length
-= access_size
;
544 gs
.write_debug_ram(4, addr
);
545 gs
.write_debug_ram(5, addr
>> 32);
554 unsigned int access_size
;
557 class memory_write_op_t
: public operation_t
560 memory_write_op_t(gdbserver_t
& gdbserver
, reg_t addr
, unsigned int length
,
561 unsigned char *data
) :
562 operation_t(gdbserver
), addr(addr
), offset(0), length(length
), data(data
) {};
564 ~memory_write_op_t() {
570 // address goes in S0
571 access_size
= (addr
% length
);
572 if (access_size
== 0)
573 access_size
= length
;
575 gs
.write_debug_ram(0, ld(S0
, 0, (uint16_t) DEBUG_RAM_START
+ 16));
576 switch (access_size
) {
578 gs
.write_debug_ram(1, lb(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 24));
579 gs
.write_debug_ram(2, sb(S1
, S0
, 0));
580 gs
.write_debug_ram(6, data
[0]);
583 gs
.write_debug_ram(1, lh(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 24));
584 gs
.write_debug_ram(2, sh(S1
, S0
, 0));
585 gs
.write_debug_ram(6, data
[0] | (data
[1] << 8));
588 gs
.write_debug_ram(1, lw(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 24));
589 gs
.write_debug_ram(2, sw(S1
, S0
, 0));
590 gs
.write_debug_ram(6, data
[0] | (data
[1] << 8) |
591 (data
[2] << 16) | (data
[3] << 24));
594 gs
.write_debug_ram(1, ld(S1
, 0, (uint16_t) DEBUG_RAM_START
+ 24));
595 gs
.write_debug_ram(2, sd(S1
, S0
, 0));
596 gs
.write_debug_ram(6, data
[0] | (data
[1] << 8) |
597 (data
[2] << 16) | (data
[3] << 24));
598 gs
.write_debug_ram(7, data
[4] | (data
[5] << 8) |
599 (data
[6] << 16) | (data
[7] << 24));
602 gs
.send_packet("E12");
605 gs
.write_debug_ram(3, jal(0, (uint32_t) (DEBUG_ROM_RESUME
- (DEBUG_RAM_START
+ 4*3))));
606 gs
.write_debug_ram(4, addr
);
607 gs
.write_debug_ram(5, addr
>> 32);
615 offset
+= access_size
;
616 if (offset
>= length
) {
617 gs
.send_packet("OK");
620 const unsigned char *d
= data
+ offset
;
621 switch (access_size
) {
623 gs
.write_debug_ram(6, d
[0]);
626 gs
.write_debug_ram(6, d
[0] | (d
[1] << 8));
629 gs
.write_debug_ram(6, d
[0] | (d
[1] << 8) |
630 (d
[2] << 16) | (d
[3] << 24));
633 gs
.write_debug_ram(6, d
[0] | (d
[1] << 8) |
634 (d
[2] << 16) | (d
[3] << 24));
635 gs
.write_debug_ram(7, d
[4] | (d
[5] << 8) |
636 (d
[6] << 16) | (d
[7] << 24));
639 gs
.send_packet("E12");
642 gs
.write_debug_ram(4, addr
+ offset
);
643 gs
.write_debug_ram(5, (addr
+ offset
) >> 32);
653 unsigned int access_size
;
657 ////////////////////////////// gdbserver itself
659 gdbserver_t::gdbserver_t(uint16_t port
, sim_t
*sim
) :
662 recv_buf(64 * 1024), send_buf(64 * 1024),
665 socket_fd
= socket(AF_INET
, SOCK_STREAM
, 0);
666 if (socket_fd
== -1) {
667 fprintf(stderr
, "failed to make socket: %s (%d)\n", strerror(errno
), errno
);
671 fcntl(socket_fd
, F_SETFL
, O_NONBLOCK
);
673 if (setsockopt(socket_fd
, SOL_SOCKET
, SO_REUSEADDR
, &reuseaddr
,
674 sizeof(int)) == -1) {
675 fprintf(stderr
, "failed setsockopt: %s (%d)\n", strerror(errno
), errno
);
679 struct sockaddr_in addr
;
680 memset(&addr
, 0, sizeof(addr
));
681 addr
.sin_family
= AF_INET
;
682 addr
.sin_addr
.s_addr
= INADDR_ANY
;
683 addr
.sin_port
= htons(port
);
685 if (bind(socket_fd
, (struct sockaddr
*) &addr
, sizeof(addr
)) == -1) {
686 fprintf(stderr
, "failed to bind socket: %s (%d)\n", strerror(errno
), errno
);
690 if (listen(socket_fd
, 1) == -1) {
691 fprintf(stderr
, "failed to listen on socket: %s (%d)\n", strerror(errno
), errno
);
696 void gdbserver_t::write_debug_ram(unsigned int index
, uint32_t value
)
698 sim
->debug_module
.ram_write32(index
, value
);
701 uint32_t gdbserver_t::read_debug_ram(unsigned int index
)
703 return sim
->debug_module
.ram_read32(index
);
706 void gdbserver_t::set_operation(operation_t
* operation
)
708 assert(this->operation
== NULL
|| operation
== NULL
);
709 if (operation
&& operation
->start()) {
712 this->operation
= operation
;
716 void gdbserver_t::accept()
718 client_fd
= ::accept(socket_fd
, NULL
, NULL
);
719 if (client_fd
== -1) {
720 if (errno
== EAGAIN
) {
721 // No client waiting to connect right now.
723 fprintf(stderr
, "failed to accept on socket: %s (%d)\n", strerror(errno
),
728 fcntl(client_fd
, F_SETFL
, O_NONBLOCK
);
731 extended_mode
= false;
733 // gdb wants the core to be halted when it attaches.
734 set_operation(new halt_op_t(*this));
738 void gdbserver_t::read()
740 // Reading from a non-blocking socket still blocks if there is no data
743 size_t count
= recv_buf
.contiguous_empty_size();
745 ssize_t bytes
= ::read(client_fd
, recv_buf
.contiguous_empty(), count
);
747 if (errno
== EAGAIN
) {
748 // We'll try again the next call.
750 fprintf(stderr
, "failed to read on socket: %s (%d)\n", strerror(errno
), errno
);
753 } else if (bytes
== 0) {
754 // The remote disconnected.
756 processor_t
*p
= sim
->get_core(0);
757 // TODO p->set_halted(false, HR_NONE);
761 recv_buf
.data_added(bytes
);
765 void gdbserver_t::write()
767 if (send_buf
.empty())
770 while (!send_buf
.empty()) {
771 unsigned int count
= send_buf
.contiguous_data_size();
773 ssize_t bytes
= ::write(client_fd
, send_buf
.contiguous_data(), count
);
775 fprintf(stderr
, "failed to write to socket: %s (%d)\n", strerror(errno
), errno
);
777 } else if (bytes
== 0) {
778 // Client can't take any more data right now.
781 fprintf(stderr
, "wrote %ld bytes: ", bytes
);
782 for (unsigned int i
= 0; i
< bytes
; i
++) {
783 fprintf(stderr
, "%c", send_buf
[i
]);
785 fprintf(stderr
, "\n");
786 send_buf
.consume(bytes
);
791 void print_packet(const std::vector
<uint8_t> &packet
)
793 for (uint8_t c
: packet
) {
794 if (c
>= ' ' and c
<= '~')
795 fprintf(stderr
, "%c", c
);
797 fprintf(stderr
, "\\x%x", c
);
799 fprintf(stderr
, "\n");
802 uint8_t compute_checksum(const std::vector
<uint8_t> &packet
)
804 uint8_t checksum
= 0;
805 for (auto i
= packet
.begin() + 1; i
!= packet
.end() - 3; i
++ ) {
811 uint8_t character_hex_value(uint8_t character
)
813 if (character
>= '0' && character
<= '9')
814 return character
- '0';
815 if (character
>= 'a' && character
<= 'f')
816 return 10 + character
- 'a';
817 if (character
>= 'A' && character
<= 'F')
818 return 10 + character
- 'A';
822 uint8_t extract_checksum(const std::vector
<uint8_t> &packet
)
824 return character_hex_value(*(packet
.end() - 1)) +
825 16 * character_hex_value(*(packet
.end() - 2));
828 void gdbserver_t::process_requests()
830 // See https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html
832 while (!recv_buf
.empty()) {
833 std::vector
<uint8_t> packet
;
834 for (unsigned int i
= 0; i
< recv_buf
.size(); i
++) {
835 uint8_t b
= recv_buf
[i
];
837 if (packet
.empty() && expect_ack
&& b
== '+') {
842 if (packet
.empty() && b
== 3) {
843 fprintf(stderr
, "Received interrupt\n");
850 // Start of new packet.
851 if (!packet
.empty()) {
852 fprintf(stderr
, "Received malformed %ld-byte packet from debug client: ",
854 print_packet(packet
);
862 // Packets consist of $<packet-data>#<checksum>
863 // where <checksum> is
864 if (packet
.size() >= 4 &&
865 packet
[packet
.size()-3] == '#') {
866 handle_packet(packet
);
867 recv_buf
.consume(i
+1);
871 // There's a partial packet in the buffer. Wait until we get more data to
879 void gdbserver_t::handle_halt_reason(const std::vector
<uint8_t> &packet
)
884 void gdbserver_t::handle_general_registers_read(const std::vector
<uint8_t> &packet
)
886 set_operation(new general_registers_read_op_t(*this));
889 void gdbserver_t::set_interrupt(uint32_t hartid
) {
890 sim
->debug_module
.set_interrupt(hartid
);
893 // First byte is the most-significant one.
894 // Eg. "08675309" becomes 0x08675309.
895 uint64_t consume_hex_number(std::vector
<uint8_t>::const_iterator
&iter
,
896 std::vector
<uint8_t>::const_iterator end
)
900 while (iter
!= end
) {
902 uint64_t c_value
= character_hex_value(c
);
912 // First byte is the least-significant one.
913 // Eg. "08675309" becomes 0x09536708
914 uint64_t consume_hex_number_le(std::vector
<uint8_t>::const_iterator
&iter
,
915 std::vector
<uint8_t>::const_iterator end
)
918 unsigned int shift
= 4;
920 while (iter
!= end
) {
922 uint64_t c_value
= character_hex_value(c
);
926 value
|= c_value
<< shift
;
927 if ((shift
% 8) == 0)
935 void consume_string(std::string
&str
, std::vector
<uint8_t>::const_iterator
&iter
,
936 std::vector
<uint8_t>::const_iterator end
, uint8_t separator
)
938 while (iter
!= end
&& *iter
!= separator
) {
939 str
.append(1, (char) *iter
);
944 void gdbserver_t::handle_register_read(const std::vector
<uint8_t> &packet
)
948 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
949 unsigned int n
= consume_hex_number(iter
, packet
.end());
951 return send_packet("E01");
953 set_operation(new register_read_op_t(*this, n
));
956 void gdbserver_t::handle_register_write(const std::vector
<uint8_t> &packet
)
960 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
961 unsigned int n
= consume_hex_number(iter
, packet
.end());
963 return send_packet("E05");
966 reg_t value
= consume_hex_number_le(iter
, packet
.end());
968 return send_packet("E06");
970 processor_t
*p
= sim
->get_core(0);
972 die("handle_register_write");
974 if (n >= REG_XPR0 && n <= REG_XPR31) {
975 p->state.XPR.write(n - REG_XPR0, value);
976 } else if (n == REG_PC) {
978 } else if (n >= REG_FPR0 && n <= REG_FPR31) {
979 p->state.FPR.write(n - REG_FPR0, value);
980 } else if (n >= REG_CSR0 && n <= REG_CSR4095) {
982 p->set_csr(n - REG_CSR0, value);
984 return send_packet("EFF");
987 return send_packet("E07");
991 return send_packet("OK");
994 void gdbserver_t::handle_memory_read(const std::vector
<uint8_t> &packet
)
997 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
998 reg_t address
= consume_hex_number(iter
, packet
.end());
1000 return send_packet("E10");
1002 reg_t length
= consume_hex_number(iter
, packet
.end());
1004 return send_packet("E11");
1006 set_operation(new memory_read_op_t(*this, address
, length
));
1009 void gdbserver_t::handle_memory_binary_write(const std::vector
<uint8_t> &packet
)
1011 // X addr,length:XX...
1012 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1013 reg_t address
= consume_hex_number(iter
, packet
.end());
1015 return send_packet("E20");
1017 reg_t length
= consume_hex_number(iter
, packet
.end());
1019 return send_packet("E21");
1023 return send_packet("OK");
1026 unsigned char *data
= new unsigned char[length
];
1027 for (unsigned int i
= 0; i
< length
; i
++) {
1028 if (iter
== packet
.end()) {
1029 return send_packet("E22");
1035 return send_packet("E4b"); // EOVERFLOW
1037 set_operation(new memory_write_op_t(*this, address
, length
, data
));
1040 void gdbserver_t::handle_continue(const std::vector
<uint8_t> &packet
)
1043 processor_t
*p
= sim
->get_core(0);
1044 if (packet
[2] != '#') {
1045 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1046 saved_dpc
= consume_hex_number(iter
, packet
.end());
1048 return send_packet("E30");
1051 set_operation(new continue_op_t(*this));
1054 void gdbserver_t::handle_step(const std::vector
<uint8_t> &packet
)
1057 if (packet
[2] != '#') {
1058 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1060 //p->state.pc = consume_hex_number(iter, packet.end());
1062 return send_packet("E40");
1065 // TODO: p->set_single_step(true);
1066 // TODO running = true;
1069 void gdbserver_t::handle_kill(const std::vector
<uint8_t> &packet
)
1072 // The exact effect of this packet is not specified.
1073 // Looks like OpenOCD disconnects?
1077 void gdbserver_t::handle_extended(const std::vector
<uint8_t> &packet
)
1079 // Enable extended mode. In extended mode, the remote server is made
1080 // persistent. The ‘R’ packet is used to restart the program being debugged.
1082 extended_mode
= true;
1085 void software_breakpoint_t::insert(mmu_t
* mmu
)
1088 instruction
= mmu
->load_uint16(address
);
1089 mmu
->store_uint16(address
, C_EBREAK
);
1091 instruction
= mmu
->load_uint32(address
);
1092 mmu
->store_uint32(address
, EBREAK
);
1094 fprintf(stderr
, ">>> Read %x from %lx\n", instruction
, address
);
1097 void software_breakpoint_t::remove(mmu_t
* mmu
)
1099 fprintf(stderr
, ">>> write %x to %lx\n", instruction
, address
);
1101 mmu
->store_uint16(address
, instruction
);
1103 mmu
->store_uint32(address
, instruction
);
1107 void gdbserver_t::handle_breakpoint(const std::vector
<uint8_t> &packet
)
1109 // insert: Z type,addr,kind
1110 // remove: z type,addr,kind
1112 software_breakpoint_t bp
;
1113 bool insert
= (packet
[1] == 'Z');
1114 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1115 int type
= consume_hex_number(iter
, packet
.end());
1117 return send_packet("E50");
1119 bp
.address
= consume_hex_number(iter
, packet
.end());
1121 return send_packet("E51");
1123 bp
.size
= consume_hex_number(iter
, packet
.end());
1124 // There may be more options after a ; here, but we don't support that.
1126 return send_packet("E52");
1128 if (bp
.size
!= 2 && bp
.size
!= 4) {
1129 return send_packet("E53");
1132 processor_t
*p
= sim
->get_core(0);
1133 die("handle_breakpoint");
1135 mmu_t* mmu = p->mmu;
1138 breakpoints[bp.address] = bp;
1141 bp = breakpoints[bp.address];
1143 breakpoints.erase(bp.address);
1145 mmu->flush_icache();
1146 sim->debug_mmu->flush_icache();
1148 return send_packet("OK");
1151 void gdbserver_t::handle_query(const std::vector
<uint8_t> &packet
)
1154 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
1156 consume_string(name
, iter
, packet
.end(), ':');
1157 if (iter
!= packet
.end())
1159 if (name
== "Supported") {
1161 while (iter
!= packet
.end()) {
1162 std::string feature
;
1163 consume_string(feature
, iter
, packet
.end(), ';');
1164 if (iter
!= packet
.end())
1166 if (feature
== "swbreak+") {
1170 return end_packet();
1173 fprintf(stderr
, "Unsupported query %s\n", name
.c_str());
1174 return send_packet("");
1177 void gdbserver_t::handle_packet(const std::vector
<uint8_t> &packet
)
1179 if (compute_checksum(packet
) != extract_checksum(packet
)) {
1180 fprintf(stderr
, "Received %ld-byte packet with invalid checksum\n", packet
.size());
1181 fprintf(stderr
, "Computed checksum: %x\n", compute_checksum(packet
));
1182 print_packet(packet
);
1187 fprintf(stderr
, "Received %ld-byte packet from debug client: ", packet
.size());
1188 print_packet(packet
);
1191 switch (packet
[1]) {
1193 return handle_extended(packet
);
1195 return handle_halt_reason(packet
);
1197 return handle_general_registers_read(packet
);
1199 return handle_kill(packet
);
1201 return handle_memory_read(packet
);
1203 // return handle_memory_write(packet);
1205 return handle_memory_binary_write(packet
);
1207 return handle_register_read(packet
);
1209 return handle_register_write(packet
);
1211 return handle_continue(packet
);
1213 return handle_step(packet
);
1216 return handle_breakpoint(packet
);
1219 return handle_query(packet
);
1223 fprintf(stderr
, "** Unsupported packet: ");
1224 print_packet(packet
);
1228 void gdbserver_t::handle_interrupt()
1230 processor_t
*p
= sim
->get_core(0);
1231 // TODO p->set_halted(true, HR_INTERRUPT);
1232 send_packet("S02"); // Pretend program received SIGINT.
1233 // TODO running = false;
1236 void gdbserver_t::handle()
1238 if (client_fd
> 0) {
1239 processor_t
*p
= sim
->get_core(0);
1241 bool interrupt
= sim
->debug_module
.get_interrupt(0);
1244 if (operation
&& operation
->step()) {
1246 set_operation(NULL
);
1252 // gdb requested a halt and now it's done.
1254 fprintf(stderr, "DPC: 0x%x\n", read_debug_ram(0));
1255 fprintf(stderr, "DCSR: 0x%x\n", read_debug_ram(2));
1256 state = STATE_HALTED;
1263 if (running && p->halted) {
1264 // The core was running, but now it's halted. Better tell gdb.
1265 switch (p->halt_reason) {
1267 fprintf(stderr, "Internal error. Processor halted without reason.\n");
1273 // There's no gdb code for this.
1277 send_packet("T05swbreak:;");
1281 // TODO: Actually include register values here
1294 this->process_requests();
1298 void gdbserver_t::send(const char* msg
)
1300 unsigned int length
= strlen(msg
);
1301 for (const char *c
= msg
; *c
; c
++)
1302 running_checksum
+= *c
;
1303 send_buf
.append((const uint8_t *) msg
, length
);
1306 void gdbserver_t::send(uint64_t value
)
1309 for (unsigned int i
= 0; i
< 8; i
++) {
1310 sprintf(buffer
, "%02x", (int) (value
& 0xff));
1316 void gdbserver_t::send(uint32_t value
)
1319 for (unsigned int i
= 0; i
< 4; i
++) {
1320 sprintf(buffer
, "%02x", (int) (value
& 0xff));
1326 void gdbserver_t::send_packet(const char* data
)
1334 void gdbserver_t::start_packet()
1337 running_checksum
= 0;
1340 void gdbserver_t::end_packet(const char* data
)
1346 char checksum_string
[4];
1347 sprintf(checksum_string
, "#%02x", running_checksum
);
1348 send(checksum_string
);