Walk page tables to translate addresses.
[riscv-isa-sim.git] / riscv / gdbserver.h
1 #ifndef _RISCV_GDBSERVER_H
2 #define _RISCV_GDBSERVER_H
3
4 #include <map>
5 #include <queue>
6
7 #include <stdint.h>
8
9 class sim_t;
10
11 template <typename T>
12 class circular_buffer_t
13 {
14 public:
15 // The buffer can store capacity-1 data elements.
16 circular_buffer_t(unsigned int capacity) : data(new T[capacity]),
17 start(0), end(0), capacity(capacity) {}
18 circular_buffer_t() : start(0), end(0), capacity(0) {}
19 ~circular_buffer_t() { delete[] data; }
20
21 T *data;
22 unsigned int start; // Data start, inclusive.
23 unsigned int end; // Data end, exclusive.
24 unsigned int capacity; // Size of the buffer.
25 unsigned int size() const;
26 bool empty() const { return start == end; }
27 bool full() const { return ((end+1) % capacity) == start; }
28
29 // Return size and address of the block of RAM where more data can be copied
30 // to be added to the buffer.
31 unsigned int contiguous_empty_size() const;
32 T *contiguous_empty() { return data + end; }
33 void data_added(unsigned int bytes);
34
35 unsigned int contiguous_data_size() const;
36 T *contiguous_data() { return data + start; }
37 // Tell the buffer that some bytes were consumed from the start of the
38 // buffer.
39 void consume(unsigned int bytes);
40
41 void reset();
42
43 T operator[](unsigned int i) const { return data[(start + i) % capacity]; }
44
45 void append(const T *src, unsigned int count);
46 };
47
48 // Class to track software breakpoints that we set.
49 class software_breakpoint_t
50 {
51 public:
52 reg_t address;
53 unsigned int size;
54 uint32_t instruction;
55
56 void insert(mmu_t* mmu);
57 void remove(mmu_t* mmu);
58 };
59
60 class gdbserver_t;
61
62 class operation_t
63 {
64 public:
65 operation_t(gdbserver_t& gdbserver) : gs(gdbserver), current_step(0) {}
66 virtual ~operation_t() {}
67
68 bool step() {
69 bool result = perform_step(current_step);
70 current_step++;
71 return result;
72 }
73
74 // Perform the next step of this operation (which is probably to write to
75 // Debug RAM and assert the debug interrupt).
76 // Return true if this operation is complete. In that case the object will
77 // be deleted.
78 // Return false if more steps are required the next time the debug
79 // interrupt is clear.
80 virtual bool perform_step(unsigned int step) = 0;
81
82 protected:
83 gdbserver_t& gs;
84 unsigned int current_step;
85 };
86
87 class gdbserver_t
88 {
89 public:
90 // Create a new server, listening for connections from localhost on the given
91 // port.
92 gdbserver_t(uint16_t port, sim_t *sim);
93
94 // Process all pending messages from a client.
95 void handle();
96
97 void handle_packet(const std::vector<uint8_t> &packet);
98 void handle_interrupt();
99
100 void handle_breakpoint(const std::vector<uint8_t> &packet);
101 void handle_continue(const std::vector<uint8_t> &packet);
102 void handle_extended(const std::vector<uint8_t> &packet);
103 void handle_general_registers_read(const std::vector<uint8_t> &packet);
104 void continue_general_registers_read();
105 void handle_halt_reason(const std::vector<uint8_t> &packet);
106 void handle_kill(const std::vector<uint8_t> &packet);
107 void handle_memory_binary_write(const std::vector<uint8_t> &packet);
108 void handle_memory_read(const std::vector<uint8_t> &packet);
109 void handle_query(const std::vector<uint8_t> &packet);
110 void handle_register_read(const std::vector<uint8_t> &packet);
111 void continue_register_read();
112 void handle_register_write(const std::vector<uint8_t> &packet);
113 void handle_step(const std::vector<uint8_t> &packet);
114
115 bool connected() const { return client_fd > 0; }
116
117 // TODO: Move this into its own packet sending class?
118 // Add the given message to send_buf.
119 void send(const char* msg);
120 // Hex-encode a 64-bit value, and send it to gcc in target byte order (little
121 // endian).
122 void send(uint64_t value);
123 // Hex-encode a 32-bit value, and send it to gcc in target byte order (little
124 // endian).
125 void send(uint32_t value);
126 void send_packet(const char* data);
127 uint8_t running_checksum;
128 // Send "$" and clear running checksum.
129 void start_packet();
130 // Send "#" and checksum.
131 void end_packet(const char* data=NULL);
132
133 // Write value to the index'th word in Debug RAM.
134 void write_debug_ram(unsigned int index, uint32_t value);
135 uint32_t read_debug_ram(unsigned int index);
136
137 void set_interrupt(uint32_t hartid);
138
139 // Members that ought to be privated, but that we'd really like to access
140 // from operation classes.
141 reg_t saved_dpc;
142 reg_t saved_mbadaddr;
143 reg_t saved_mcause;
144 reg_t saved_mstatus;
145 reg_t dcsr;
146 reg_t sptbr;
147 bool sptbr_valid;
148
149 std::map<reg_t, reg_t> pte_cache;
150
151 reg_t translate(reg_t vaddr);
152 // Return the PRV_x that is used when the code under debug performs a memory
153 // access.
154 unsigned int privilege_mode();
155 // Return the VM_x that is used when the code under debug performs a memory
156 // access.
157 unsigned int virtual_memory();
158
159 private:
160 sim_t *sim;
161 int socket_fd;
162 int client_fd;
163 circular_buffer_t<uint8_t> recv_buf;
164 circular_buffer_t<uint8_t> send_buf;
165
166 bool expect_ack;
167 bool extended_mode;
168 // Used to track whether we think the target is running. If we think it is
169 // but it isn't, we need to tell gdb about it.
170 bool running;
171
172 std::map <reg_t, software_breakpoint_t> breakpoints;
173
174 // Read pending data from the client.
175 void read();
176 void write();
177 // Accept a new client if there isn't one already connected.
178 void accept();
179 // Process all complete requests in recv_buf.
180 void process_requests();
181
182 std::queue<operation_t*> operation_queue;
183 void add_operation(operation_t* operation);
184 };
185
186 #endif