-
Notifications
You must be signed in to change notification settings - Fork 142
/
hvt_module_gdb.c
675 lines (573 loc) · 18.3 KB
/
hvt_module_gdb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
/*
* Copyright (c) 2015-2019 Contributors as noted in the AUTHORS file
*
* This file is part of Solo5, a sandboxed execution environment.
*
* Permission to use, copy, modify, and/or distribute this software
* for any purpose with or without fee is hereby granted, provided
* that the above copyright notice and this permission notice appear
* in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* Based on binutils-gdb/gdb/stubs/i386-stub.c, which is:
* Not copyrighted.
*/
/*
* hvt_module_gdb.c: Implements the GDB Remote Serial Protocol
* https://sourceware.org/gdb/onlinedocs/gdb/Overview.html
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <err.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <assert.h>
#include <stdbool.h>
#include <ctype.h>
#include "hvt.h"
#include "hvt_gdb.h"
#if defined(__linux__) && defined(__x86_64__)
#include "hvt_gdb_kvm_x86_64.c"
#elif defined(__FreeBSD__) && defined(__x86_64__)
#include "hvt_gdb_freebsd_x86_64.c"
#elif defined(__OpenBSD__) && defined(__x86_64__)
#include "hvt_gdb_openbsd_x86_64.c"
#elif defined(__linux__) && defined(__aarch64__)
#include "hvt_gdb_kvm_aarch64.c"
#else
#error Unsupported target
#endif
static bool use_gdb = false;
static int socket_fd = 0;
static int portno = 1234; /* Default port number */
static const char hexchars[] = "0123456789abcdef";
#define BUFMAX 4096
static char in_buffer[BUFMAX];
static unsigned char registers[BUFMAX];
/* The actual error code is ignored by GDB, so any number will do. */
#define GDB_ERROR_MSG "E01"
static int hex(unsigned char ch)
{
if ((ch >= 'a') && (ch <= 'f'))
return (ch - 'a' + 10);
if ((ch >= '0') && (ch <= '9'))
return (ch - '0');
if ((ch >= 'A') && (ch <= 'F'))
return (ch - 'A' + 10);
return -1;
}
/*
* Converts the (count) bytes of memory pointed to by mem into an hex string in
* buf. Returns a pointer to the last char put in buf (null).
*/
static char *mem2hex(const unsigned char *mem, char *buf, size_t count)
{
size_t i;
unsigned char ch;
for (i = 0; i < count; i++) {
ch = *mem++;
*buf++ = hexchars[ch >> 4];
*buf++ = hexchars[ch % 16];
}
*buf = 0;
return buf;
}
/*
* Converts the hex string in buf into binary in mem.
* Returns a pointer to the character AFTER the last byte written.
*/
static unsigned char *hex2mem(const char *buf,
unsigned char *mem, size_t count)
{
size_t i;
unsigned char ch;
assert(strlen(buf) >= (2 * count));
for (i = 0; i < count; i++) {
ch = hex(*buf++) << 4;
ch = ch + hex(*buf++);
*mem++ = ch;
}
return mem;
}
static int wait_for_connect()
{
int listen_socket_fd;
struct sockaddr_in server_addr, client_addr;
struct protoent *protoent;
struct in_addr ip_addr;
socklen_t len;
int opt;
listen_socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if (listen_socket_fd == -1) {
err(1, "Could not create socket");
return -1;
}
opt = 1;
if (setsockopt(listen_socket_fd, SOL_SOCKET, SO_REUSEADDR, &opt,
sizeof(opt)) == -1)
err(1, "setsockopt(SO_REUSEADDR) failed");
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(portno);
if (bind(listen_socket_fd, (struct sockaddr *)&server_addr,
sizeof(server_addr)) == -1) {
err(1, "bind failed");
return -1;
}
if (listen(listen_socket_fd , 0) == -1) {
err(1, "listen failed");
return -1;
}
warnx("Waiting for a debugger. Connect to it like this:");
warnx("\tgdb --ex=\"target remote localhost:%d\" KERNEL", portno);
len = sizeof(client_addr);
socket_fd = accept(listen_socket_fd, (struct sockaddr *)&client_addr, &len);
if (socket_fd == -1) {
err(1, "accept failed");
return -1;
}
close(listen_socket_fd);
protoent = getprotobyname("tcp");
if (!protoent) {
err(1, "getprotobyname (\"tcp\") failed");
return -1;
}
opt = 1;
if (setsockopt(socket_fd, protoent->p_proto, TCP_NODELAY, &opt,
sizeof(opt)) == -1)
err(1, "setsockopt(TCP_NODELAY) failed");
ip_addr.s_addr = client_addr.sin_addr.s_addr;
warnx("Connection from debugger at %s", inet_ntoa(ip_addr));
return 0;
}
static int send_char(char ch)
{
/* TCP is already buffering, so no need to buffer here as well. */
return send(socket_fd, &ch, 1, 0);
}
static char recv_char(void)
{
unsigned char ch;
int ret;
ret = recv(socket_fd, &ch, 1, 0);
if (ret < 0) {
return -1;
} else if (ret == 0) {
/* The peer has performed an orderly shutdown (from "man recv"). */
close(socket_fd);
socket_fd = -1;
return -1;
} else {
assert(ret == 1);
}
/* All GDB remote packets are encoded in ASCII. */
assert(isascii(ch));
return (char)ch;
}
/*
* Scan for the sequence $<data>#<checksum>
* Returns a null terminated string.
*/
static char *recv_packet(void)
{
char *buffer = &in_buffer[0];
unsigned char checksum;
unsigned char xmitcsum;
char ch;
int count;
while (1) {
/* wait around for the start character, ignore all other characters */
do {
ch = recv_char();
if (ch == -1)
return NULL;
} while (ch != '$');
retry:
checksum = 0;
xmitcsum = -1;
count = 0;
/* now, read until a # or end of buffer is found */
while (count < BUFMAX - 1) {
ch = recv_char();
if (ch == -1)
return NULL;
if (ch == '$')
goto retry;
if (ch == '#')
break;
checksum = checksum + ch;
buffer[count] = ch;
count = count + 1;
}
/* Let's make this a C string. */
buffer[count] = '\0';
if (ch == '#') {
ch = recv_char();
if (ch == -1)
return NULL;
xmitcsum = hex(ch) << 4;
ch = recv_char();
if (ch == -1)
return NULL;
xmitcsum += hex(ch);
if (checksum != xmitcsum) {
warnx("Failed checksum from GDB. "
"My count = 0x%x, sent=0x%x. buf=%s",
checksum, xmitcsum, buffer);
if (send_char('-') == -1)
/* Unsuccessful reply to a failed checksum */
err(1, "GDB: Could not send an ACK to the debugger.");
} else {
if (send_char('+') == -1)
/* Unsuccessful reply to a successful transfer */
err(1, "GDB: Could not send an ACK to the debugger.");
/* if a sequence char is present, reply the sequence ID */
if (buffer[2] == ':') {
send_char(buffer[0]);
send_char(buffer[1]);
return &buffer[3];
}
return &buffer[0];
}
}
}
}
/*
* Send packet of the form $<packet info>#<checksum> without waiting for an ACK
* from the debugger. Only send_response
*/
static void send_packet_no_ack(char *buffer)
{
unsigned char checksum;
int count;
char ch;
/*
* We ignore all send_char errors as we either: (1) care about sending our
* packet and we will keep sending it until we get a good ACK from the
* debugger, or (2) not care and just send it as a best-effort notification
* when dying (see handle_hvt_exit).
*/
send_char('$');
checksum = 0;
count = 0;
ch = buffer[count];
while (ch) {
send_char(ch);
checksum += ch;
count += 1;
ch = buffer[count];
}
send_char('#');
send_char(hexchars[checksum >> 4]);
send_char(hexchars[checksum % 16]);
}
/*
* Send a packet and wait for a successful ACK of '+' from the debugger.
* An ACK of '-' means that we have to resend.
*/
static void send_packet(char *buffer)
{
char ch;
for (;;) {
send_packet_no_ack(buffer);
ch = recv_char();
if (ch == -1)
return;
if (ch == '+')
break;
}
}
#define send_error_msg() do { send_packet(GDB_ERROR_MSG); } while (0)
#define send_not_supported_msg() do { send_packet(""); } while (0)
#define send_okay_msg() do { send_packet("OK"); } while (0)
/*
* This is a response to 'c' and 's'. In other words, the VM was
* running and it stopped for some reason. This message is to tell the
* debugger that whe stopped (and why). The argument code can take these
* and some other values:
* - 'S AA' received signal AA
* - 'W AA' exited with return code AA
* - 'X AA' exited with signal AA
* https://sourceware.org/gdb/onlinedocs/gdb/Stop-Reply-Packets.html
*/
static void send_response(char code, int sigval, bool wait_for_ack)
{
char obuf[BUFMAX];
snprintf(obuf, sizeof(obuf), "%c%02x", code, sigval);
if (wait_for_ack)
send_packet(obuf);
else
send_packet_no_ack(obuf);
}
static void gdb_handle_exception(struct hvt *hvt, int sigval)
{
char *packet;
char obuf[BUFMAX];
/* Notify the debugger of our last signal */
send_response('S', sigval, true);
for (;;) {
hvt_gpa_t addr = 0, result;
gdb_breakpoint_type type;
size_t len;
int command, ret;
packet = recv_packet();
if (packet == NULL)
/* Without a packet with instructions with what to do next there is
* really nothing we can do to recover. So, dying. */
errx(1, "GDB: Exiting as we could not receive the next command "
"from the debugger.");
/*
* From the GDB manual:
* "At a minimum, a stub is required to support the ‘g’ and ‘G’
* commands for register access, and the ‘m’ and ‘M’ commands
* for memory access. Stubs that only control single-threaded
* targets can implement run control with the ‘c’ (continue),
* and ‘s’ (step) commands."
*/
command = packet[0];
switch (command) {
case 's': {
/* Step */
if (sscanf(packet, "s%"PRIx64, &addr) == 1) {
/* not supported, but that's OK as GDB will retry with the
* slower version of this: update all registers. */
send_not_supported_msg();
break; /* Wait for another command. */
}
if (hvt_gdb_enable_ss(hvt) == -1) {
send_error_msg();
break; /* Wait for another command. */
}
return; /* Continue with program */
}
case 'c': {
/* Continue (and disable stepping for the next instruction) */
if (sscanf(packet, "c%"PRIx64, &addr) == 1) {
/* not supported, but that's OK as GDB will retry with the
* slower version of this: update all registers. */
send_not_supported_msg();
break; /* Wait for another command. */
}
if (hvt_gdb_disable_ss(hvt) == -1) {
send_error_msg();
break; /* Wait for another command. */
}
return; /* Continue with program */
}
case 'm': {
/* Read memory content */
if (sscanf(packet, "m%"PRIx64",%zx",
&addr, &len) != 2) {
send_error_msg();
break;
}
if ((addr > hvt->mem_size) ||
add_overflow(addr, len, result) ||
(result > hvt->mem_size)) {
/* Don't panic about this, just return error so the debugger
* tries again. */
send_error_msg();
} else {
mem2hex(hvt->mem + addr, obuf, len);
send_packet(obuf);
}
break; /* Wait for another command. */
}
case 'M': {
/* Write memory content */
assert(strlen(packet) <= sizeof(obuf));
if (sscanf(packet, "M%"PRIx64",%zx:%s", &addr, &len, obuf) != 3) {
send_error_msg();
break;
}
if ((addr > hvt->mem_size) ||
add_overflow(addr, len, result) ||
(result > hvt->mem_size)) {
/* Don't panic about this, just return error so the debugger
* tries again. */
send_error_msg();
} else {
hex2mem(obuf, hvt->mem + addr, len);
send_okay_msg();
}
break; /* Wait for another command. */
}
case 'g': {
/* Read general registers */
len = BUFMAX;
if (hvt_gdb_read_registers(hvt, registers, &len) == -1) {
send_error_msg();
} else {
mem2hex(registers, obuf, len);
send_packet(obuf);
}
break; /* Wait for another command. */
}
case 'G': {
/* Write general registers */
len = BUFMAX;
/* Call read_registers just to get len (not very efficient). */
if (hvt_gdb_read_registers(hvt, registers, &len) == -1) {
send_error_msg();
break;
}
/* Packet looks like 'Gxxxxx', so we have to skip the first char */
hex2mem(packet + 1, registers, len);
if (hvt_gdb_write_registers(hvt, registers, len) == -1) {
send_error_msg();
break;
}
send_okay_msg();
break; /* Wait for another command. */
}
case '?': {
/* Return last signal */
send_response('S', sigval, true);
break; /* Wait for another command. */
}
case 'Z':
/* Insert a breakpoint */
case 'z': {
/* Remove a breakpoint */
packet++;
if (sscanf(packet, "%"PRIx32",%"PRIx64",%zx",
&type, &addr, &len) != 3) {
send_error_msg();
break;
}
if ((addr > hvt->mem_size) ||
add_overflow(addr, len, result) ||
(result > hvt->mem_size)) {
/* Don't panic about this, just return error so the debugger
* tries again. */
send_error_msg();
break;
}
if (command == 'Z')
ret = hvt_gdb_add_breakpoint(hvt, type, addr, len);
else
ret = hvt_gdb_remove_breakpoint(hvt, type, addr, len);
if (ret == -1)
send_error_msg();
else
send_okay_msg();
break;
}
case 'k': {
warnx("Debugger asked us to quit");
send_okay_msg();
break;
}
case 'D': {
warnx("Debugger detached");
send_okay_msg();
return;
}
default:
send_not_supported_msg();
break;
}
}
return;
}
static void gdb_stub_start(struct hvt *hvt)
{
wait_for_connect();
gdb_handle_exception(hvt, GDB_SIGNAL_FIRST);
}
/*
* Maps a VM exit to a GDB signal, and if it is of type trap (breakpoint or
* step), we handle it here (and not in the vcpu loop). We force the handling
* here, by returning 0; and return -1 otherwise.
*/
static int handle_exit(struct hvt *hvt)
{
int sigval = 0;
if (hvt_gdb_read_last_signal(hvt, &sigval) == -1)
/* Handle this exit in the vcpu loop */
return -1;
switch(sigval) {
case GDB_SIGNAL_TRAP:
gdb_handle_exception(hvt, sigval);
return 0;
case GDB_SIGNAL_TERM:
/* We exited with return code 0 */
send_response('W', 0, true);
return -1;
case GDB_SIGNAL_QUIT:
case GDB_SIGNAL_KILL:
case GDB_SIGNAL_SEGV:
gdb_handle_exception(hvt, sigval);
return 0;
default:
/* Handle this exit in the vcpu loop */
return -1;
}
}
void handle_hvt_exit(void)
{
/* Tell the debugger that we exited with a "SIGKILL", and
* don't wait for an ACK. */
send_response('X', GDB_SIGNAL_KILL, false);
}
static int setup(struct hvt *hvt, struct mft *mft)
{
if (!use_gdb)
return 0;
if (hvt_core_register_vmexit(handle_exit) == -1)
return -1;
if (hvt_gdb_supported() == -1)
errx(1, "GDB support not implemented on this backend/architecture");
/*
* GDB clients can change memory, and software breakpoints work by
* replacing instructions with int3's.
*/
if (mprotect(hvt->mem, hvt->mem_size,
PROT_READ | PROT_WRITE | PROT_EXEC) == -1)
err(1, "GDB: Cannot remove guest memory protection");
/* Notify the debugger that we are dying. */
atexit(handle_hvt_exit);
gdb_stub_start(hvt);
return 0;
}
static int handle_cmdarg(char *cmdarg, struct mft *mft)
{
if (!strcmp("--gdb", cmdarg)) {
use_gdb = true;
return 0;
} else if (!strncmp("--gdb-port=", cmdarg, 11)) {
int rc = sscanf(cmdarg, "--gdb-port=%d", &portno);
if (rc != 1 || portno < 0 || portno > 65535) {
errx(1, "Malformed argument to --gdb-port");
}
return 0;
}
return -1;
}
static char *usage(void)
{
return "--gdb (optional flag for running in a gdb debug session)\n"
" [ --gdb-port=1234 ] (port to use) ";
}
DECLARE_MODULE(gdb,
.setup = setup,
.handle_cmdarg = handle_cmdarg,
.usage = usage
)