* nlm/alpha.c (strtol): Removed, it is provided by NetWare C library.
[binutils-gdb.git] / gdb / nlm / ppc.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <time.h>
5 #include <errno.h>
6
7 #include <nwtypes.h>
8 #include <nwdfs.h>
9 #include <nwconio.h>
10 #include <nwadv.h>
11 #include <nwdbgapi.h>
12 #include <nwthread.h>
13 #include "ppc.h"
14
15 extern char *mem2hex (void *mem, char *buf, int count, int may_fault);
16 extern char *hex2mem (char *buf, void *mem, int count, int may_fault);
17 extern int computeSignal (int exceptionVector);
18
19 void
20 flush_i_cache (void)
21 {
22 }
23
24 /* Get the registers out of the frame information. */
25
26 void
27 frame_to_registers (frame, regs)
28 struct StackFrame *frame;
29 char *regs;
30 {
31 mem2hex (&frame->ExceptionState.CsavedRegs, &regs[GP0_REGNUM * 4 * 2], 4 * 32, 0);
32
33 mem2hex (&frame->ExceptionState.CSavedFPRegs, &regs[FP0_REGNUM * 4 * 2], 4 * 32, 0);
34
35 mem2hex (&frame->ExceptionPC, &regs[PC_REGNUM * 4 * 2], 4 * 1, 0);
36
37 mem2hex (&frame->ExceptionState.u.SpecialRegistersEnumerated.CsavedSRR1, &regs[CR_REGNUM * 4 * 2], 4 * 1, 0);
38 mem2hex (&frame->ExceptionState.u.SpecialRegistersEnumerated.CsavedLR, &regs[LR_REGNUM * 4 * 2], 4 * 1, 0);
39 mem2hex (&frame->ExceptionState.u.SpecialRegistersEnumerated.CsavedCTR, &regs[CTR_REGNUM * 4 * 2], 4 * 1, 0);
40 mem2hex (&frame->ExceptionState.u.SpecialRegistersEnumerated.CsavedXER, &regs[XER_REGNUM * 4 * 2], 4 * 1, 0);
41 mem2hex (&frame->ExceptionState.u.SpecialRegistersEnumerated.CsavedMQ, &regs[MQ_REGNUM * 4 * 2], 4 * 1, 0);
42 }
43
44 /* Put the registers back into the frame information. */
45
46 void
47 registers_to_frame (regs, frame)
48 char *regs;
49 struct StackFrame *frame;
50 {
51 hex2mem (&regs[GP0_REGNUM * 4 * 2], &frame->ExceptionState.CsavedRegs, 4 * 32, 0);
52
53 hex2mem (&regs[FP0_REGNUM * 4 * 2], &frame->ExceptionState.CSavedFPRegs, 4 * 32, 0);
54
55 hex2mem (&regs[PC_REGNUM * 4 * 2], &frame->ExceptionPC, 4 * 1, 0);
56
57 hex2mem (&regs[CR_REGNUM * 4 * 2], &frame->ExceptionState.u.SpecialRegistersEnumerated.CsavedSRR1, 4 * 1, 0);
58 hex2mem (&regs[LR_REGNUM * 4 * 2], &frame->ExceptionState.u.SpecialRegistersEnumerated.CsavedLR, 4 * 1, 0);
59 hex2mem (&regs[CTR_REGNUM * 4 * 2], &frame->ExceptionState.u.SpecialRegistersEnumerated.CsavedCTR, 4 * 1, 0);
60 hex2mem (&regs[XER_REGNUM * 4 * 2], &frame->ExceptionState.u.SpecialRegistersEnumerated.CsavedXER, 4 * 1, 0);
61 hex2mem (&regs[MQ_REGNUM * 4 * 2], &frame->ExceptionState.u.SpecialRegistersEnumerated.CsavedMQ, 4 * 1, 0);
62 }
63
64 extern int ReadByteAltDebugger (char* addr, char *theByte);
65
66 extern int WriteByteAltDebugger (char* addr, char theByte);
67
68 extern volatile int mem_err;
69
70 int
71 get_char (addr)
72 char *addr;
73 {
74 char c;
75
76 if (!ReadByteAltDebugger (addr, &c))
77 mem_err = 1;
78
79 return c;
80 }
81
82 void
83 set_char (addr, val)
84 char *addr;
85 int val;
86 {
87 if (!WriteByteAltDebugger (addr, val))
88 mem_err = 1;
89 }
90
91 int
92 mem_write (dst, src, len)
93 char *dst, *src;
94 int len;
95 {
96 while (len-- && !mem_err)
97 set_char (dst++, *src++);
98
99 return mem_err;
100 }
101
102 union inst
103 {
104 LONG l;
105
106 struct
107 {
108 union
109 {
110 struct /* Unconditional branch */
111 {
112 unsigned opcode : 6; /* 18 */
113 signed li : 24;
114 unsigned aa : 1;
115 unsigned lk : 1;
116 } b;
117 struct /* Conditional branch */
118 {
119 unsigned opcode : 6; /* 16 */
120 unsigned bo : 5;
121 unsigned bi : 5;
122 signed bd : 14;
123 unsigned aa : 1;
124 unsigned lk : 1;
125 } bc;
126 struct /* Conditional branch to ctr or lr reg */
127 {
128 unsigned opcode : 6; /* 19 */
129 unsigned bo : 5;
130 unsigned bi : 5;
131 unsigned type : 15; /* 528 = ctr, 16 = lr */
132 unsigned lk : 1;
133 } bclr;
134 } variant;
135 } inst;
136 };
137
138 static LONG saved_inst;
139 static LONG *saved_inst_pc = 0;
140 static LONG saved_target_inst;
141 static LONG *saved_target_inst_pc = 0;
142
143 void
144 set_step_traps (frame)
145 struct StackFrame *frame;
146 {
147 union inst inst;
148 LONG *target;
149 int opcode;
150 int ra, rb;
151 LONG *pc = (LONG *)frame->ExceptionPC;
152
153 inst.l = *pc++;
154
155 opcode = inst.inst.variant.b.opcode;
156
157 target = pc;
158
159 switch (opcode)
160 {
161 case 18: /* Unconditional branch */
162
163 if (inst.inst.variant.b.aa) /* Absolute? */
164 target = 0;
165 target += inst.inst.variant.b.li;
166
167 break;
168 case 16: /* Conditional branch */
169
170 if (!inst.inst.variant.bc.aa) /* Absolute? */
171 target = 0;
172 target += inst.inst.variant.bc.bd;
173
174 break;
175 case 19: /* Cond. branch via ctr or lr reg */
176 switch (inst.inst.variant.bclr.type)
177 {
178 case 528: /* ctr */
179 target = (LONG *)frame->ExceptionState.u.SpecialRegistersEnumerated.CsavedCTR;
180 break;
181 case 16: /* lr */
182 target = (LONG *)frame->ExceptionState.u.SpecialRegistersEnumerated.CsavedLR;
183 break;
184 }
185 break;
186 }
187
188 saved_inst = *pc;
189 mem_write (pc, breakpoint_insn, BREAKPOINT_SIZE);
190 saved_inst_pc = pc;
191
192 if (target != pc)
193 {
194 saved_target_inst = *target;
195 mem_write (target, breakpoint_insn, BREAKPOINT_SIZE);
196 saved_target_inst_pc = target;
197 }
198 }
199
200 /* Remove step breakpoints. Returns non-zero if pc was at a step breakpoint,
201 zero otherwise. This routine works even if there were no step breakpoints
202 set. */
203
204 int
205 clear_step_traps (frame)
206 struct StackFrame *frame;
207 {
208 int retcode;
209 LONG *pc = (LONG *)frame->ExceptionPC;
210
211 if (saved_inst_pc == pc || saved_target_inst_pc == pc)
212 retcode = 1;
213 else
214 retcode = 0;
215
216 if (saved_inst_pc)
217 {
218 mem_write (saved_inst_pc, saved_inst, BREAKPOINT_SIZE);
219 saved_inst_pc = 0;
220 }
221
222 if (saved_target_inst_pc)
223 {
224 mem_write (saved_target_inst_pc, saved_target_inst, BREAKPOINT_SIZE);
225 saved_target_inst_pc = 0;
226 }
227
228 return retcode;
229 }
230
231 void
232 do_status (ptr, frame)
233 char *ptr;
234 struct StackFrame *frame;
235 {
236 int sigval;
237
238 sigval = computeSignal (frame->ExceptionNumber);
239
240 sprintf (ptr, "T%02x", sigval);
241 ptr += 3;
242
243 sprintf (ptr, "%02x:", PC_REGNUM);
244 ptr = mem2hex (&frame->ExceptionPC, ptr + 3, 4, 0);
245 *ptr++ = ';';
246
247 sprintf (ptr, "%02x:", SP_REGNUM);
248 ptr = mem2hex (&frame->ExceptionState.CsavedRegs[SP_REGNUM], ptr + 3, 4, 0);
249 *ptr++ = ';';
250
251 sprintf (ptr, "%02x:", LR_REGNUM);
252 ptr = mem2hex (&frame->ExceptionState.CsavedRegs[LR_REGNUM], ptr + 3, 4, 0);
253 *ptr++ = ';';
254
255 *ptr = '\000';
256 }
257
258 void
259 StopBell()
260 {
261 }