ReactOS  0.4.13-dev-551-gf37fb1f
gdblib.c
Go to the documentation of this file.
1 /****************************************************************************
2 
3  THIS SOFTWARE IS NOT COPYRIGHTED
4 
5  HP offers the following for use in the public domain. HP makes no
6  warranty with regard to the software or it's performance and the
7  user accepts the software "AS IS" with all faults.
8 
9  HP DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD
10  TO THIS SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES
11  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
12 
13 ****************************************************************************/
14 
15 /****************************************************************************
16  * Header: remcom.c,v 1.34 91/03/09 12:29:49 glenne Exp $
17  *
18  * Module name: remcom.c $
19  * Revision: 1.34 $
20  * Date: 91/03/09 12:29:49 $
21  * Contributor: Lake Stevens Instrument Division$
22  *
23  * Description: low level support for gdb debugger. $
24  *
25  * Considerations: only works on target hardware $
26  *
27  * Written by: Glenn Engel $
28  * ModuleState: Experimental $
29  *
30  * NOTES: See Below $
31  *
32  * Modified for 386 by Jim Kingdon, Cygnus Support.
33  * Modified for ReactOS by Casper S. Hornstrup <chorns@users.sourceforge.net>
34  * Modified heavily for PowerPC ReactOS by arty
35  *
36  * To enable debugger support, two things need to happen. One, setting
37  * up a routine so that it is in the exception path, is necessary in order
38  * to allow any breakpoints or error conditions to be properly intercepted
39  * and reported to gdb.
40  * Two, a breakpoint needs to be generated to begin communication.
41  ER*
42  * Because gdb will sometimes write to the stack area to execute function
43  * calls, this program cannot rely on using the supervisor stack so it
44  * uses it's own stack area.
45  *
46  *************
47  *
48  * The following gdb commands are supported:
49  *
50  * command function Return value
51  *
52  * g return the value of the CPU Registers hex data or ENN
53  * G set the value of the CPU Registers OK or ENN
54  *
55  * mAA..AA,LLLL Read LLLL bytes at address AA..AA hex data or ENN
56  * MAA..AA,LLLL: Write LLLL bytes at address AA.AA OK or ENN
57  *
58  * c Resume at current address SNN ( signal NN)
59  * cAA..AA Continue at address AA..AA SNN
60  *
61  * s Step one instruction SNN
62  * sAA..AA Step one instruction from AA..AA SNN
63  *
64  * k kill
65  *
66  * ? What was the last sigval ? SNN (signal NN)
67  *
68  * All commands and responses are sent with a packet which includes a
69  * Checksum. A packet consists of
70  *
71  * $<packet info>#<Checksum>.
72  *
73  * where
74  * <packet info> :: <characters representing the command or response>
75  * <Checksum> :: < two hex digits computed as modulo 256 sum of <packetinfo>>
76  *
77  * When a packet is received, it is first acknowledged with either '+' or '-'.
78  * '+' indicates a successful transfer. '-' indicates a failed transfer.
79  *
80  * Example:
81  *
82  * Host: Reply:
83  * $m0,10#2a +$00010203040506070809101112131415#42
84  *
85  ****************************************************************************/
86 
87 #include "ppcmmu/mmu.h"
88 
89 #define GDB_SAVE_SIZE 0x66
90 
91 typedef struct _BREAKPOINT {
92  int OldCode;
93  int *Address;
95 
97 char DataOutBuffer[1024];
98 volatile int DataOutAddr, DataOutCsum;
99 char DataInBuffer[128];
101 volatile int PacketSent = 0, SendSignal = 0;
102 volatile int Continue = 0, Signal = 0;
104 char *hex = "0123456789abcdef";
105 
106 #define RCV 0
107 #define THR 0
108 #define BAUDLOW 0
109 #define BAUDHIGH 1
110 #define IER 1
111 #define FCR 2
112 #define ISR 2
113 #define LCR 3
114 #define MCR 4
115 #define LSR 5
116 #define MSR 6
117 #define SPR 7
118 
119 extern void send(char *serport, char c);
120 extern char recv(char *serport);
121 extern void setup(char *serport, int baud);
122 
123 char *serport = (char *)0x800003f8;
124 
125 int isxdigit(int ch)
126 {
127  return
128  (ch >= 'A' && ch <= 'F') ||
129  (ch >= 'a' && ch <= 'f') ||
130  (ch >= '0' && ch <= '9');
131 }
132 
133 inline void sync() {
134  __asm__("eieio\n\t"
135  "sync");
136 }
137 
138 inline void send(char *serport, char c) {
139  /* Wait for Clear to Send */
140  while( !(GetPhysByte((paddr_t)serport+LSR) & 0x20) ) sync();
141 
143  sync();
144 }
145 
146 inline int rdy(char *serport)
147 {
148  sync();
149  return (GetPhysByte((paddr_t)serport+LSR) & 0x20);
150 }
151 
152 inline int chr(char *serport)
153 {
154  sync();
155  return GetPhysByte((paddr_t)serport+LSR) & 1;
156 }
157 
158 inline char recv(char *serport) {
159  char c;
160 
161  while( !chr(serport) ) sync();
162 
164  sync();
165 
166  return c;
167 }
168 
169 void setup(char *serport, int baud) {
170  int x = 115200 / baud;
172  sync();
174  sync();
176  sync();
178  sync();
179 }
180 
181 void SerialSetUp(int deviceType, void *deviceAddr, int baud)
182 {
183  int i;
184  serport = deviceAddr;
185  setup(serport, baud);
186 }
187 
188 extern int SerialInterrupt(int signal, ppc_trap_frame_t *tf);
189 
190 void IntEnable()
191 {
193 }
194 
195 void SerialWrite(int ch)
196 {
197  send(serport, ch);
198 }
199 
201 {
202  return recv(serport);
203 }
204 
205 int hex2int(int ch)
206 {
207  if (ch >= 'a' && ch <= 'f') return ch + 10 - 'a';
208  else if (ch >= 'A' && ch <= 'F') return ch + 10 - 'A';
209  else return ch - '0';
210 }
211 
213 {
214  int i;
215  int result = 0;
216  for (i = 0; i < dig && isxdigit(DataInBuffer[DataInAddr]); i++)
217  {
218  result <<= 4;
220  }
221  return result;
222 }
223 
224 void PacketWriteChar(int ch)
225 {
226  DataOutCsum += ch;
227  DataOutBuffer[DataOutAddr++] = ch;
228 }
229 
230 int PacketWriteHexNumber(int hnum, int dig)
231 {
232  int i;
233  hnum <<= (8 - dig) * 4;
234  for (i = 0; i < dig; i++)
235  {
236  PacketWriteChar(hex[(hnum >> 28) & 15]);
237  hnum <<= 4;
238  }
239  return i;
240 }
241 
243 {
244  DataOutCsum = 0;
245  DataOutAddr = 0;
246 }
247 
249 {
250  int i, ch, count = 0;
251 
252  PacketSent = 0;
253 
254  SerialWrite('$');
255  for (i = 0; i < DataOutAddr; i++)
256  {
258  }
259  SerialWrite('#');
260  SerialWrite(hex[(DataOutCsum >> 4) & 15]);
261  SerialWrite(hex[DataOutCsum & 15]);
262 
263  while(!chr(serport) && ((ch = SerialRead()) != '+') && (ch != '$'));
264  if (ch == '$')
265  {
266  ParseState = 0;
267  DataInAddr = 0;
268  ComputedCsum = 0;
269  }
270 }
271 
272 
274 {
275  while(*str) PacketWriteChar(*str++);
276 }
277 
278 void PacketOk()
279 {
280  PacketStart();
281  PacketWriteString("OK");
282  PacketFinish();
283 }
284 
286 {
287  PacketStart();
288  PacketFinish();
289 }
290 
292 {
293  PacketStart();
294  PacketWriteChar('S');
296  PacketFinish();
297 }
298 
300 {
301  PacketStart();
302  PacketWriteChar('E');
304  PacketFinish();
305 }
306 
307 void marker() { }
308 
309 void GotPacket()
310 {
311  int i, memaddr, memsize;
312 
313  Continue = 0;
314  switch (DataInBuffer[DataInAddr++])
315  {
316  case 'g':
317  PacketStart();
318  for (i = 0; i < GDB_SAVE_SIZE; i++)
319  {
321  }
322  PacketFinish();
323  break;
324 
325  case 'G':
326  for (i = 0; i < sizeof(*RegisterSaveArea) / sizeof(int); i++)
327  {
328  ((int *)RegisterSaveArea)[i] = PacketReadHexNumber(8);
329  }
330  PacketOk();
331  break;
332 
333  case 'm':
334  memaddr = PacketReadHexNumber(8);
335  DataInAddr++;
336  memsize = PacketReadHexNumber(8);
337  PacketStart();
338  while(memsize-- > 0)
339  {
340  PacketWriteHexNumber(*((char *)memaddr++), 2);
341  }
342  PacketFinish();
343  break;
344 
345  case 'M':
346  memaddr = PacketReadHexNumber(8);
347  DataInAddr++;
348  memsize = PacketReadHexNumber(8);
349  DataInAddr++;
350  while(memsize-- > 0)
351  {
352  *((char *)memaddr++) = PacketReadHexNumber(2);
353  }
354  PacketOk();
355  break;
356 
357  case '?':
359  break;
360 
361  case 'c':
362  PacketOk();
363  Continue = 1;
364  break;
365 
366  case 'S':
367  PacketOk();
368  Continue = 0;
369  break;
370 
371  case 's':
372  RegisterSaveArea->srr1 |= 0x400;
373  PacketOk();
374  Continue = 1;
375  marker();
376  break;
377 
378  case 'q':
379  switch (DataInBuffer[1])
380  {
381  case 'S': /*upported => nothing*/
382  PacketEmpty();
383  break;
384 
385  case 'O': /*ffsets*/
386  PacketEmpty();
387  break;
388  }
389  break;
390 
391  default:
392  PacketEmpty();
393  break;
394  }
395 }
396 
398 {
399  int ch;
400 
401  if (!chr(serport)) return 0;
402 
403  Signal = signal;
405 
406  do
407  {
408  ch = SerialRead();
409 
410  if (ch == 3) /* Break in - tehe */
411  {
412  Continue = 0;
414  }
415  else if (ch == '+')
416  {
417  /* Nothing */
418  }
419  else if (ch == '$')
420  {
421  DataInAddr = 0;
422  ParseState = 0;
423  ComputedCsum = 0;
424  ActualCsum = 0;
425  }
426  else if (ch == '#' && ParseState == 0)
427  {
428  ParseState = 2;
429  }
430  else if (ParseState == 0)
431  {
432  ComputedCsum += ch;
433  DataInBuffer[DataInAddr++] = ch;
434  }
435  else if (ParseState == 2)
436  {
437  ActualCsum = ch;
438  ParseState++;
439  }
440  else if (ParseState == 3)
441  {
442  ActualCsum = hex2int(ch) | (hex2int(ActualCsum) << 4);
443  ComputedCsum &= 255;
444  ParseState = -1;
445  if (ComputedCsum == ActualCsum)
446  {
447  ComputedCsum = 0;
449  DataInAddr = 0;
450  Continue = 0;
451  SerialWrite('+');
452  GotPacket();
453  }
454  else
455  SerialWrite('-');
456  }
457  else if (ParseState == -1)
458  SerialWrite('-');
459  }
460  while (!Continue);
461  return 1;
462 }
463 
465 {
466  Signal = n;
469  SendSignal = 0;
470  Continue = 0;
471  while(!Continue) SerialInterrupt(n, tf);
472  return 1;
473 }
474 
475 /* EOF */
int isxdigit(int ch)
Definition: gdblib.c:125
int * Address
Definition: gdblib.c:93
void PacketFinish()
Definition: gdblib.c:248
int GetPhysByte(int Addr)
int rdy(char *serport)
Definition: gdblib.c:146
volatile ppc_trap_frame_t * RegisterSaveArea
Definition: gdblib.c:103
char * serport
Definition: gdblib.c:123
void PacketWriteChar(int ch)
Definition: gdblib.c:224
GLuint GLuint GLsizei count
Definition: gl.h:1545
volatile int DataOutAddr
Definition: gdblib.c:98
int OldCode
Definition: gdblib.c:92
void PacketWriteString(char *str)
Definition: gdblib.c:273
void PacketEmpty()
Definition: gdblib.c:285
void IntEnable()
Definition: gdblib.c:190
unsigned long srr1
Definition: mmu.h:115
GLdouble n
Definition: glext.h:7729
HFONT tf
Definition: icontest.c:17
char * hex
Definition: gdblib.c:104
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
void PacketOk()
Definition: gdblib.c:278
void GotPacket()
Definition: gdblib.c:309
void PacketStart()
Definition: gdblib.c:242
unsigned long paddr_t
Definition: mmu.h:89
int hex2int(int ch)
Definition: gdblib.c:205
void SerialWrite(int ch)
Definition: gdblib.c:195
#define IER
Definition: gdblib.c:110
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
volatile int DataOutCsum
Definition: gdblib.c:98
char DataOutBuffer[1024]
Definition: gdblib.c:97
volatile int DataInAddr
Definition: gdblib.c:100
__asm__("\t.globl GetPhys\n" "GetPhys:\t\n" "mflr 0\n\t" "stwu 0,-16(1)\n\t" "mfmsr 5\n\t" "andi. 6,5,0xffef\n\t" "mtmsr 6\n\t" "isync\n\t" "sync\n\t" "lwz 3,0(3)\n\t" "mtmsr 5\n\t" "isync\n\t" "sync\n\t" "lwz 0,0(1)\n\t" "addi 1,1,16\n\t" "mtlr 0\n\t" "blr")
volatile int ParseState
Definition: gdblib.c:100
BREAKPOINT BreakPoints[64]
Definition: gdblib.c:96
const WCHAR * str
#define LSR
Definition: gdblib.c:115
void PacketWriteError(int code)
Definition: gdblib.c:299
void sync()
Definition: gdblib.c:133
int PacketReadHexNumber(int dig)
Definition: gdblib.c:212
struct _BREAKPOINT * PBREAKPOINT
#define RCV
Definition: gdblib.c:106
volatile ppc_trap_frame_t RegisterSaves
Definition: gdblib.c:103
struct _BREAKPOINT BREAKPOINT
void PacketWriteSignal(int code)
Definition: gdblib.c:291
#define GDB_SAVE_SIZE
Definition: gdblib.c:89
volatile int ActualCsum
Definition: gdblib.c:100
void send(char *serport, char c)
Definition: gdblib.c:138
const GLubyte * c
Definition: glext.h:8905
volatile int ComputedCsum
Definition: gdblib.c:100
volatile int SendSignal
Definition: gdblib.c:101
char DataInBuffer[128]
Definition: gdblib.c:99
volatile int Signal
Definition: gdblib.c:102
volatile int Continue
Definition: gdblib.c:102
int signal
Definition: xcptfil.c:12
char recv(char *serport)
Definition: gdblib.c:158
int chr(char *serport)
Definition: gdblib.c:152
void SerialSetUp(int deviceType, void *deviceAddr, int baud)
Definition: gdblib.c:181
#define THR
Definition: gdblib.c:107
volatile int PacketSent
Definition: gdblib.c:101
int PacketWriteHexNumber(int hnum, int dig)
Definition: gdblib.c:230
int SerialRead()
Definition: gdblib.c:200
void marker()
Definition: gdblib.c:307
void setup(char *serport, int baud)
Definition: gdblib.c:169
int SerialInterrupt(int signal, ppc_trap_frame_t *tf)
Definition: gdblib.c:397
#define c
Definition: ke_i.h:80
#define BAUDHIGH
Definition: gdblib.c:109
#define BAUDLOW
Definition: gdblib.c:108
GLuint64EXT * result
Definition: glext.h:11304
void SetPhysByte(ULONG Addr, ULONG Data)
#define LCR
Definition: gdblib.c:113
int TakeException(int n, ppc_trap_frame_t *tf)
Definition: gdblib.c:464
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31