Rizin
unix-like reverse engineering framework and cli tools
kd.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2014-2017 LemonBoy <thatlemon@gmail.com>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #ifndef KD_H
5 #define KD_H
6 #include <rz_types_base.h>
7 #include "transport.h"
8 
9 enum {
10  KD_E_OK = 0,
14  KD_E_IOERR = -4,
15  KD_E_BREAK = -5,
16 };
17 
31 };
32 
38  DbgKdMaximumStateChange = 0x00003033
39 };
40 
42  DbgKdMinimumManipulate = 0x00003130,
45  DbgKdGetContextApi = 0x00003132,
46  DbgKdSetContextApi = 0x00003133,
49  DbgKdContinueApi = 0x00003136,
52  DbgKdReadIoSpaceApi = 0x00003139,
53  DbgKdWriteIoSpaceApi = 0x0000313A,
54  DbgKdRebootApi = 0x0000313B,
55  DbgKdContinueApi2 = 0x0000313C,
59  DbgKdSetSpecialCallApi = 0x00003140,
65  DbgKdGetVersionApi = 0x00003146,
68  DbgKdCauseBugCheckApi = 0x00003149,
69  DbgKdSwitchProcessor = 0x00003150,
70  DbgKdPageInApi = 0x00003151,
73  OldVlm1 = 0x00003154,
74  OldVlm2 = 0x00003155,
75  DbgKdSearchMemoryApi = 0x00003156,
76  DbgKdGetBusDataApi = 0x00003157,
77  DbgKdSetBusDataApi = 0x00003158,
78  DbgKdCheckLowMemoryApi = 0x00003159,
80  DbgKdFillMemoryApi = 0x0000315B,
81  DbgKdQueryMemoryApi = 0x0000315C,
82  DbgKdSwitchPartition = 0x0000315D,
84  DbgKdGetContextEx = 0x0000315F,
85  DbgKdSetContextEx = 0x00003160,
86  DbgKdMaximumManipulate = 0x00003161
87 };
88 
90  DbgKdCreateFileApi = 0x00003430,
91  DbgKdReadFileApi = 0x00003431,
92  DbgKdWriteFileApi = 0x00003432,
93  DbgKdCloseFileApi = 0x00003433
94 };
95 
96 #define KD_PACKET_UNUSED 0x00000000
97 #define KD_PACKET_DATA 0x30303030
98 #define KD_PACKET_CTRL 0x69696969
99 
100 #define KD_INITIAL_PACKET_ID 0x80800000
101 
102 #define KD_MAX_PAYLOAD 0x480
103 #define KD_PACKET_MAX_SIZE 4000 // Not used ? What is max payload ?
104 
105 // http://msdn.microsoft.com/en-us/library/cc704588.aspx
106 #define KD_RET_OK 0x00000000
107 #define KD_RET_ERR 0xC0000001
108 #define KD_RET_ENOENT 0xC000000F
109 
110 #define KD_MACH_I386 0x014C
111 #define KD_MACH_IA64 0x0200
112 #define KD_MACH_AMD64 0x8664
113 #define KD_MACH_ARM 0x01c0
114 #define KD_MACH_EBC 0x0EBC
115 
116 #define DBGKD_VERS_FLAG_DATA 0x0002
117 #define DBGKD_VERS_FLAG_PTR64 0x0004
118 
120  typedef struct kd_req_t {
121  ut32 req;
122  ut16 cpu_level;
123  ut16 cpu;
124  ut32 ret;
125  // Pad to 16-byte boundary (?)
126  ut32 pad;
127  union {
128  RZ_PACKED(
129  struct {
130  ut64 addr;
131  ut32 length;
132  ut32 read;
133  })
134  rz_mem;
135  RZ_PACKED(
136  struct {
137  ut16 major;
138  ut16 minor;
139  ut8 proto_major;
140  ut8 proto_minor;
141  ut16 flags;
142  ut16 machine;
143  ut8 misc[6];
144  ut64 kernel_base;
145  ut64 mod_addr;
146  ut64 dbg_addr;
147  })
148  rz_ver;
149  struct {
150  ut32 reason;
151  ut32 tf;
152  ut32 dr7;
153  ut32 css;
154  ut32 cse;
155  } rz_cont;
156  struct {
157  ut64 addr;
158  ut32 handle;
159  } rz_set_bp;
160  struct {
161  ut32 handle;
162  } rz_del_bp;
163  struct {
164  ut64 addr;
165  ut32 flags;
166  } rz_set_ibp;
167  struct {
168  ut64 addr;
169  ut32 flags;
170  ut32 calls;
171  } rz_get_ibp;
172  struct {
173  ut32 flags;
174  } rz_ctx;
175  struct {
176  ut32 offset;
177  ut32 count;
178  ut32 copied;
179  } rz_ctx_ex;
180  struct {
181  ut64 addr;
182  ut64 reserved;
183  ut32 address_space;
184  ut32 flags;
185  } rz_query_mem;
186 
187  // Pad the struct to 56 bytes
188  ut8 raw[40];
189  };
190  ut8 data[];
191  })
192 kd_req_t;
193 
194 #define KD_EXC_BKPT 0x80000003
196  typedef struct kd_stc_64 {
197  ut32 state;
198  ut16 cpu_level;
199  ut16 cpu;
200  ut32 cpu_count;
201  ut32 pad1;
202  ut64 kthread;
203  ut64 pc;
204  union {
205  RZ_PACKED(
206  struct {
207  ut32 code;
208  ut32 flags;
209  ut64 ex_record;
210  ut64 ex_addr;
211  })
212  exception;
213  RZ_PACKED(
214  struct {
215  ut64 pathsize;
216  ut64 base;
217  ut64 pid;
218  ut32 checksum;
219  ut32 size;
220  ut8 unload;
221  })
222  load_symbols;
223  };
224  })
225 kd_stc_64;
226 
227 typedef struct kd_ioc_t {
230  ut64 pad[7];
232 
234  typedef struct kd_packet_t {
235  ut32 leader;
236  ut16 type;
237  ut16 length;
238  ut32 id;
239  ut32 checksum;
240  ut8 data[];
241  })
242 kd_packet_t;
243 
244 // KDNET
245 
246 #define KDNET_MAGIC 0x4d444247 // MDBG
247 #define KDNET_HMACKEY_SIZE 32
248 #define KDNET_HMAC_SIZE 16
249 
250 #define KDNET_PACKET_TYPE_DATA 0
251 #define KDNET_PACKET_TYPE_CONTROL 1
252 
254  typedef struct kdnet_packet_t {
255  ut32 magic; // KDNET_MAGIC
256  ut8 version; // Protocol Number
257  ut8 type; // Channel Type - 0 Data, 1 Control
258  })
259 kdnet_packet_t;
260 
261 // KDNet Data mask
262 #define KDNET_DATA_SIZE 8
263 #define KDNET_DATA_DIRECTION_MASK 0x80
264 #define KDNET_DATA_PADSIZE_MASK 0x7F
265 #define KDNET_DATA_SEQNO_MASK 0xFFFFFF00
266 
267 // Compile time assertions macros taken from :
268 // http://www.pixelbeat.org/programming/gcc/static_assert.html
269 #define ASSERT_CONCAT_(a, b) a##b
270 #define ASSERT_CONCAT(a, b) ASSERT_CONCAT_(a, b)
271 #define ct_assert(e) enum { ASSERT_CONCAT(assert_line_, __LINE__) = 1 / (!!(e)) }
272 
273 ct_assert(sizeof(kd_packet_t) == 16);
274 ct_assert(sizeof(kd_req_t) == 56);
275 ct_assert(sizeof(kd_ioc_t) == 64);
276 
277 int kd_send_ctrl_packet(io_desc_t *desc, const ut32 type, const ut32 id);
278 int kd_send_data_packet(io_desc_t *desc, const ut32 type, const ut32 id, const ut8 *req, const int req_len, const ut8 *buf, const ut32 buf_len);
279 
280 int kd_read_packet(io_desc_t *desc, kd_packet_t **p);
281 
282 bool kd_packet_is_valid(const kd_packet_t *p);
283 int kd_packet_is_ack(const kd_packet_t *p);
284 
285 ut32 kd_data_checksum(const ut8 *buf, const ut64 buf_len);
286 
287 #endif
@ exception
Definition: aarch64.h:495
static char * version
Definition: acr.h:4
static ut32 cpu[32]
Definition: analysis_or1k.c:21
static int buf_len
Definition: asm_arc.c:22
static mcore_handle handle
Definition: asm_mcore.c:8
const char * desc
Definition: bin_vsf.c:19
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec req
Definition: sflib.h:128
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
Definition: sflib.h:98
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void length
Definition: sflib.h:133
uint16_t ut16
uint32_t ut32
#define minor(dev)
Definition: fsmagic.c:57
#define major(dev)
Definition: fsmagic.c:56
voidpf void uLong size
Definition: ioapi.h:138
voidpf uLong offset
Definition: ioapi.h:144
voidpf void * buf
Definition: ioapi.h:138
ut32 kd_data_checksum(const ut8 *buf, const ut64 buf_len)
Definition: kd.c:11
struct kd_ioc_t kd_ioc_t
#define ct_assert(e)
Definition: kd.h:271
@ KD_E_BADCHKSUM
Definition: kd.h:11
@ KD_E_BREAK
Definition: kd.h:15
@ KD_E_IOERR
Definition: kd.h:14
@ KD_E_OK
Definition: kd.h:10
@ KD_E_MALFORMED
Definition: kd.h:13
@ KD_E_TIMEOUT
Definition: kd.h:12
int kd_send_ctrl_packet(io_desc_t *desc, const ut32 type, const ut32 id)
Definition: kd.c:25
KD_PACKET_MANIPULATE_TYPE
Definition: kd.h:41
@ DbgKdContinueApi
Definition: kd.h:49
@ DbgKdCauseBugCheckApi
Definition: kd.h:68
@ DbgKdWritePhysicalMemoryApi
Definition: kd.h:57
@ DbgKdMinimumManipulate
Definition: kd.h:42
@ DbgKdGetVersionApi
Definition: kd.h:65
@ DbgKdRestoreBreakPointExApi
Definition: kd.h:67
@ DbgKdWriteVirtualMemoryApi
Definition: kd.h:44
@ DbgKdSetContextEx
Definition: kd.h:85
@ DbgKdWriteMachineSpecificRegister
Definition: kd.h:72
@ DbgKdReadPhysicalMemoryApi
Definition: kd.h:56
@ DbgKdReadIoSpaceExtendedApi
Definition: kd.h:63
@ DbgKdFillMemoryApi
Definition: kd.h:80
@ DbgKdSwitchProcessor
Definition: kd.h:69
@ DbgKdGetContextApi
Definition: kd.h:45
@ DbgKdSetInternalBreakPointApi
Definition: kd.h:61
@ DbgKdGetBusDataApi
Definition: kd.h:76
@ DbgKdWriteControlSpaceApi
Definition: kd.h:51
@ DbgKdRebootApi
Definition: kd.h:54
@ DbgKdReadControlSpaceApi
Definition: kd.h:50
@ DbgKdReadVirtualMemoryApi
Definition: kd.h:43
@ DbgKdGetInternalBreakPointApi
Definition: kd.h:62
@ DbgKdMaximumManipulate
Definition: kd.h:86
@ DbgKdSearchMemoryApi
Definition: kd.h:75
@ DbgKdGetContextEx
Definition: kd.h:84
@ DbgKdWriteBreakPointApi
Definition: kd.h:47
@ DbgKdQueryMemoryApi
Definition: kd.h:81
@ DbgKdClearAllInternalBreakpointsApi
Definition: kd.h:79
@ DbgKdSetContextApi
Definition: kd.h:46
@ DbgKdWriteCustomBreakpointApi
Definition: kd.h:83
@ DbgKdSetBusDataApi
Definition: kd.h:77
@ OldVlm1
Definition: kd.h:73
@ DbgKdContinueApi2
Definition: kd.h:55
@ DbgKdQuerySpecialCallsApi
Definition: kd.h:58
@ DbgKdReadIoSpaceApi
Definition: kd.h:52
@ DbgKdSwitchPartition
Definition: kd.h:82
@ DbgKdCheckLowMemoryApi
Definition: kd.h:78
@ DbgKdClearSpecialCallsApi
Definition: kd.h:60
@ DbgKdWriteIoSpaceExtendedApi
Definition: kd.h:64
@ DbgKdRestoreBreakPointApi
Definition: kd.h:48
@ DbgKdSetSpecialCallApi
Definition: kd.h:59
@ OldVlm2
Definition: kd.h:74
@ DbgKdWriteBreakPointExApi
Definition: kd.h:66
@ DbgKdWriteIoSpaceApi
Definition: kd.h:53
@ DbgKdPageInApi
Definition: kd.h:70
@ DbgKdReadMachineSpecificRegister
Definition: kd.h:71
int kd_send_data_packet(io_desc_t *desc, const ut32 type, const ut32 id, const ut8 *req, const int req_len, const ut8 *buf, const ut32 buf_len)
Definition: kd.c:41
KD_PACKET_WAIT_STATE_CHANGE
Definition: kd.h:33
@ DbgKdMinimumStateChange
Definition: kd.h:34
@ DbgKdCommandStringStateChange
Definition: kd.h:37
@ DbgKdLoadSymbolsStateChange
Definition: kd.h:36
@ DbgKdExceptionStateChange
Definition: kd.h:35
@ DbgKdMaximumStateChange
Definition: kd.h:38
int kd_read_packet(io_desc_t *desc, kd_packet_t **p)
Definition: kd.c:80
bool kd_packet_is_valid(const kd_packet_t *p)
Definition: kd.c:154
KD_PACKET_TYPE
Definition: kd.h:18
@ KD_PACKET_TYPE_STATE_MANIPULATE
Definition: kd.h:21
@ KD_PACKET_TYPE_STATE_CHANGE64
Definition: kd.h:26
@ KD_PACKET_TYPE_RESEND
Definition: kd.h:24
@ KD_PACKET_TYPE_TRACE_IO
Definition: kd.h:28
@ KD_PACKET_TYPE_POLL_BREAKIN
Definition: kd.h:27
@ KD_PACKET_TYPE_DEBUG_IO
Definition: kd.h:22
@ KD_PACKET_TYPE_UNUSED
Definition: kd.h:19
@ KD_PACKET_TYPE_ACKNOWLEDGE
Definition: kd.h:23
@ KD_PACKET_TYPE_STATE_CHANGE32
Definition: kd.h:20
@ KD_PACKET_TYPE_FILE_IO
Definition: kd.h:30
@ KD_PACKET_TYPE_CONTROL_REQUEST
Definition: kd.h:29
@ KD_PACKET_TYPE_RESET
Definition: kd.h:25
KD_PACKET_FILE_IO_TYPE
Definition: kd.h:89
@ DbgKdReadFileApi
Definition: kd.h:91
@ DbgKdCreateFileApi
Definition: kd.h:90
@ DbgKdCloseFileApi
Definition: kd.h:93
@ DbgKdWriteFileApi
Definition: kd.h:92
RZ_PACKED(typedef struct kd_req_t { ut32 req;ut16 cpu_level;ut16 cpu;ut32 ret;ut32 pad;union { RZ_PACKED(struct { ut64 addr;ut32 length;ut32 read;}) rz_mem;RZ_PACKED(struct { ut16 major;ut16 minor;ut8 proto_major;ut8 proto_minor;ut16 flags;ut16 machine;ut8 misc[6];ut64 kernel_base;ut64 mod_addr;ut64 dbg_addr;}) rz_ver;struct { ut32 reason;ut32 tf;ut32 dr7;ut32 css;ut32 cse;} rz_cont;struct { ut64 addr;ut32 handle;} rz_set_bp;struct { ut32 handle;} rz_del_bp;struct { ut64 addr;ut32 flags;} rz_set_ibp;struct { ut64 addr;ut32 flags;ut32 calls;} rz_get_ibp;struct { ut32 flags;} rz_ctx;struct { ut32 offset;ut32 count;ut32 copied;} rz_ctx_ex;struct { ut64 addr;ut64 reserved;ut32 address_space;ut32 flags;} rz_query_mem;ut8 raw[40];};ut8 data[];}) kd_req_t
int kd_packet_is_ack(const kd_packet_t *p)
Definition: kd.c:158
uint8_t ut8
Definition: lh5801.h:11
void * p
Definition: libc.cpp:67
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc pid
Definition: sflib.h:64
@ reserved
Definition: lm32_isa.h:94
int type
Definition: mipsasm.c:17
int id
Definition: op.c:540
const char * code
Definition: pal.c:98
static void pad(RzStrBuf *sb, ut32 count)
Definition: protobuf.c:36
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123
Definition: kd.h:227
ut64 pad[7]
Definition: kd.h:230
ut32 ret
Definition: kd.h:229
ut32 req
Definition: kd.h:228
Definition: dis.h:43
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static int addr
Definition: z80asm.c:58
int read(izstream &zs, T *x, Items items)
Definition: zstream.h:115