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 #if defined(__x86_64__)
25     #define  XGFM64  "%lx"
26     #define  XGF64   "%016lx"
27 #else
28     #define  XGFM64  "%llx"
29     #define  XGF64   "%016llx"
30 #endif
31 
32 
33 typedef enum {
34     XG_GPRS=1,          /* general purpose user regs */
35     XG_FPRS=2,          /* floating point user regs */
36 } regstype_t;
37 
38 
39 typedef uint32_t vcpuid_t;
40 
41 extern int xgtrc_on;
42 
43 /* what gdb wants to receive during register read, or sends during write.
44  * this from : regformats/reg-i386-linux.dat in gdbserver */
45 struct xg_gdb_regs32 {
46     uint32_t  eax;
47     uint32_t  ecx;
48     uint32_t  edx;
49     uint32_t  ebx;
50     uint32_t  esp;
51     uint32_t  ebp;
52     uint32_t  esi;
53     uint32_t  edi;
54     uint32_t  eip;
55     uint32_t  eflags;
56     uint32_t  cs;
57     uint32_t  ss;
58     uint32_t  ds;
59     uint32_t  es;
60     uint32_t  fs;
61     uint32_t  gs;
62 };
63 
64 /* this from: regformats/reg-x86-64.dat in gdbserver */
65 struct xg_gdb_regs64 {
66     uint64_t  rax;
67     uint64_t  rbx;
68     uint64_t  rcx;
69     uint64_t  rdx;
70     uint64_t  rsi;
71     uint64_t  rdi;
72     uint64_t  rbp;
73     uint64_t  rsp;
74     uint64_t  r8;
75     uint64_t  r9;
76     uint64_t  r10;
77     uint64_t  r11;
78     uint64_t  r12;
79     uint64_t  r13;
80     uint64_t  r14;
81     uint64_t  r15;
82     uint64_t  rip;
83     uint64_t  rflags;
84     uint64_t  cs;
85     uint64_t  ss;
86     uint64_t  ds;
87     uint64_t  es;
88     uint64_t  fs;
89     uint64_t  gs;
90 };
91 
92 union xg_gdb_regs {
93     struct xg_gdb_regs32 gregs_32;
94     struct xg_gdb_regs64 gregs_64;
95 };
96 
97 
98 int xg_init(void);
99 int xg_attach(int, int);
100 void xg_detach_deinit(void);
101 int xg_step(vcpuid_t, int);
102 vcpuid_t xg_resume_n_wait(int);
103 int xg_regs_read(regstype_t, vcpuid_t, union xg_gdb_regs *, int);
104 int xg_regs_write(regstype_t, vcpuid_t, union xg_gdb_regs *, int);
105 int xg_read_mem(uint64_t, char *, int, uint64_t);
106 int xg_write_mem(uint64_t, char *, int, uint64_t);
107 void xgprt(const char *fn, const char *fmt, ...);
108 void xgtrc(const char *fn, const char *fmt, ...);
109