1 /*
2  * Copyright (C) 2009, Mukesh Rathor, Oracle Corp.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #define XGERR(...)   \
18            do {(xgprt(__FUNCTION__,__VA_ARGS__));} while (0)
19 #define XGTRC(...)   \
20            do {(xgtrc_on) ? (xgtrc(__FUNCTION__,__VA_ARGS__)):0;} while (0)
21 #define XGTRC1(...)  \
22            do {(xgtrc_on==2) ? (xgtrc(__FUNCTION__,__VA_ARGS__)):0;} while (0)
23 
24 
25 typedef enum {
26     XG_GPRS=1,          /* general purpose user regs */
27     XG_FPRS=2,          /* floating point user regs */
28 } regstype_t;
29 
30 
31 typedef uint32_t vcpuid_t;
32 
33 extern int xgtrc_on;
34 
35 /* what gdb wants to receive during register read, or sends during write.
36  * this from : regformats/reg-i386-linux.dat in gdbserver */
37 struct xg_gdb_regs32 {
38     uint32_t  eax;
39     uint32_t  ecx;
40     uint32_t  edx;
41     uint32_t  ebx;
42     uint32_t  esp;
43     uint32_t  ebp;
44     uint32_t  esi;
45     uint32_t  edi;
46     uint32_t  eip;
47     uint32_t  eflags;
48     uint32_t  cs;
49     uint32_t  ss;
50     uint32_t  ds;
51     uint32_t  es;
52     uint32_t  fs;
53     uint32_t  gs;
54 };
55 
56 /* based on gdb/features/i386/64bit-core.xml in gdb */
57 struct xg_gdb_regs64 {
58     uint64_t  rax;
59     uint64_t  rbx;
60     uint64_t  rcx;
61     uint64_t  rdx;
62     uint64_t  rsi;
63     uint64_t  rdi;
64     uint64_t  rbp;
65     uint64_t  rsp;
66     uint64_t  r8;
67     uint64_t  r9;
68     uint64_t  r10;
69     uint64_t  r11;
70     uint64_t  r12;
71     uint64_t  r13;
72     uint64_t  r14;
73     uint64_t  r15;
74     uint64_t  rip;
75     uint32_t  eflags;
76     uint32_t  cs;
77     uint32_t  ss;
78     uint32_t  ds;
79     uint32_t  es;
80     uint32_t  fs;
81     uint32_t  gs;
82 } __attribute__((__packed__));
83 
84 union xg_gdb_regs {
85     struct xg_gdb_regs32 gregs_32;
86     struct xg_gdb_regs64 gregs_64;
87 };
88 
89 
90 int xg_init(void);
91 int xg_attach(int, int);
92 void xg_detach_deinit(void);
93 int xg_step(vcpuid_t, int);
94 vcpuid_t xg_resume_n_wait(int);
95 int xg_regs_read(regstype_t, vcpuid_t, union xg_gdb_regs *, int);
96 int xg_regs_write(regstype_t, vcpuid_t, union xg_gdb_regs *, int);
97 int xg_read_mem(uint64_t, char *, int, uint64_t);
98 int xg_write_mem(uint64_t, char *, int, uint64_t);
99 void xgprt(const char *fn, const char *fmt, ...);
100 void xgtrc(const char *fn, const char *fmt, ...);
101