virtio: allow byte swapping for vring
[qemu.git] / target-ppc / gdbstub.c
blob694d303e199df43977107d87b5b060c1bd6f3e94
1 /*
2 * PowerPC gdb server stub
4 * Copyright (c) 2003-2005 Fabrice Bellard
5 * Copyright (c) 2013 SUSE LINUX Products GmbH
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "config.h"
21 #include "qemu-common.h"
22 #include "exec/gdbstub.h"
24 static int ppc_gdb_register_len_apple(int n)
26 switch (n) {
27 case 0 ... 31:
28 /* gprs */
29 return 8;
30 case 32 ... 63:
31 /* fprs */
32 return 8;
33 case 64 ... 95:
34 return 16;
35 case 64+32: /* nip */
36 case 65+32: /* msr */
37 case 67+32: /* lr */
38 case 68+32: /* ctr */
39 case 69+32: /* xer */
40 case 70+32: /* fpscr */
41 return 8;
42 case 66+32: /* cr */
43 return 4;
44 default:
45 return 0;
49 static int ppc_gdb_register_len(int n)
51 switch (n) {
52 case 0 ... 31:
53 /* gprs */
54 return sizeof(target_ulong);
55 case 32 ... 63:
56 /* fprs */
57 if (gdb_has_xml) {
58 return 0;
60 return 8;
61 case 66:
62 /* cr */
63 return 4;
64 case 64:
65 /* nip */
66 case 65:
67 /* msr */
68 case 67:
69 /* lr */
70 case 68:
71 /* ctr */
72 case 69:
73 /* xer */
74 return sizeof(target_ulong);
75 case 70:
76 /* fpscr */
77 if (gdb_has_xml) {
78 return 0;
80 return sizeof(target_ulong);
81 default:
82 return 0;
87 static void ppc_gdb_swap_register(uint8_t *mem_buf, int n, int len)
89 if (len == 4) {
90 bswap32s((uint32_t *)mem_buf);
91 } else if (len == 8) {
92 bswap64s((uint64_t *)mem_buf);
93 } else {
94 g_assert_not_reached();
98 /* Old gdb always expects FP registers. Newer (xml-aware) gdb only
99 * expects whatever the target description contains. Due to a
100 * historical mishap the FP registers appear in between core integer
101 * regs and PC, MSR, CR, and so forth. We hack round this by giving the
102 * FP regs zero size when talking to a newer gdb.
105 int ppc_cpu_gdb_read_register(CPUState *cs, uint8_t *mem_buf, int n)
107 PowerPCCPU *cpu = POWERPC_CPU(cs);
108 CPUPPCState *env = &cpu->env;
109 int r = ppc_gdb_register_len(n);
111 if (!r) {
112 return r;
115 if (n < 32) {
116 /* gprs */
117 gdb_get_regl(mem_buf, env->gpr[n]);
118 } else if (n < 64) {
119 /* fprs */
120 stfq_p(mem_buf, env->fpr[n-32]);
121 } else {
122 switch (n) {
123 case 64:
124 gdb_get_regl(mem_buf, env->nip);
125 break;
126 case 65:
127 gdb_get_regl(mem_buf, env->msr);
128 break;
129 case 66:
131 uint32_t cr = 0;
132 int i;
133 for (i = 0; i < 8; i++) {
134 cr |= env->crf[i] << (32 - ((i + 1) * 4));
136 gdb_get_reg32(mem_buf, cr);
137 break;
139 case 67:
140 gdb_get_regl(mem_buf, env->lr);
141 break;
142 case 68:
143 gdb_get_regl(mem_buf, env->ctr);
144 break;
145 case 69:
146 gdb_get_regl(mem_buf, env->xer);
147 break;
148 case 70:
149 gdb_get_reg32(mem_buf, env->fpscr);
150 break;
153 if (msr_le) {
154 /* If cpu is in LE mode, convert memory contents to LE. */
155 ppc_gdb_swap_register(mem_buf, n, r);
157 return r;
160 int ppc_cpu_gdb_read_register_apple(CPUState *cs, uint8_t *mem_buf, int n)
162 PowerPCCPU *cpu = POWERPC_CPU(cs);
163 CPUPPCState *env = &cpu->env;
164 int r = ppc_gdb_register_len_apple(n);
166 if (!r) {
167 return r;
170 if (n < 32) {
171 /* gprs */
172 gdb_get_reg64(mem_buf, env->gpr[n]);
173 } else if (n < 64) {
174 /* fprs */
175 stfq_p(mem_buf, env->fpr[n-32]);
176 } else if (n < 96) {
177 /* Altivec */
178 stq_p(mem_buf, n - 64);
179 stq_p(mem_buf + 8, 0);
180 } else {
181 switch (n) {
182 case 64 + 32:
183 gdb_get_reg64(mem_buf, env->nip);
184 break;
185 case 65 + 32:
186 gdb_get_reg64(mem_buf, env->msr);
187 break;
188 case 66 + 32:
190 uint32_t cr = 0;
191 int i;
192 for (i = 0; i < 8; i++) {
193 cr |= env->crf[i] << (32 - ((i + 1) * 4));
195 gdb_get_reg32(mem_buf, cr);
196 break;
198 case 67 + 32:
199 gdb_get_reg64(mem_buf, env->lr);
200 break;
201 case 68 + 32:
202 gdb_get_reg64(mem_buf, env->ctr);
203 break;
204 case 69 + 32:
205 gdb_get_reg64(mem_buf, env->xer);
206 break;
207 case 70 + 32:
208 gdb_get_reg64(mem_buf, env->fpscr);
209 break;
212 if (msr_le) {
213 /* If cpu is in LE mode, convert memory contents to LE. */
214 ppc_gdb_swap_register(mem_buf, n, r);
216 return r;
219 int ppc_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)
221 PowerPCCPU *cpu = POWERPC_CPU(cs);
222 CPUPPCState *env = &cpu->env;
223 int r = ppc_gdb_register_len(n);
225 if (!r) {
226 return r;
228 if (msr_le) {
229 /* If cpu is in LE mode, convert memory contents to LE. */
230 ppc_gdb_swap_register(mem_buf, n, r);
232 if (n < 32) {
233 /* gprs */
234 env->gpr[n] = ldtul_p(mem_buf);
235 } else if (n < 64) {
236 /* fprs */
237 env->fpr[n-32] = ldfq_p(mem_buf);
238 } else {
239 switch (n) {
240 case 64:
241 env->nip = ldtul_p(mem_buf);
242 break;
243 case 65:
244 ppc_store_msr(env, ldtul_p(mem_buf));
245 break;
246 case 66:
248 uint32_t cr = ldl_p(mem_buf);
249 int i;
250 for (i = 0; i < 8; i++) {
251 env->crf[i] = (cr >> (32 - ((i + 1) * 4))) & 0xF;
253 break;
255 case 67:
256 env->lr = ldtul_p(mem_buf);
257 break;
258 case 68:
259 env->ctr = ldtul_p(mem_buf);
260 break;
261 case 69:
262 env->xer = ldtul_p(mem_buf);
263 break;
264 case 70:
265 /* fpscr */
266 store_fpscr(env, ldtul_p(mem_buf), 0xffffffff);
267 break;
270 return r;
272 int ppc_cpu_gdb_write_register_apple(CPUState *cs, uint8_t *mem_buf, int n)
274 PowerPCCPU *cpu = POWERPC_CPU(cs);
275 CPUPPCState *env = &cpu->env;
276 int r = ppc_gdb_register_len_apple(n);
278 if (!r) {
279 return r;
281 if (msr_le) {
282 /* If cpu is in LE mode, convert memory contents to LE. */
283 ppc_gdb_swap_register(mem_buf, n, r);
285 if (n < 32) {
286 /* gprs */
287 env->gpr[n] = ldq_p(mem_buf);
288 } else if (n < 64) {
289 /* fprs */
290 env->fpr[n-32] = ldfq_p(mem_buf);
291 } else {
292 switch (n) {
293 case 64 + 32:
294 env->nip = ldq_p(mem_buf);
295 break;
296 case 65 + 32:
297 ppc_store_msr(env, ldq_p(mem_buf));
298 break;
299 case 66 + 32:
301 uint32_t cr = ldl_p(mem_buf);
302 int i;
303 for (i = 0; i < 8; i++) {
304 env->crf[i] = (cr >> (32 - ((i + 1) * 4))) & 0xF;
306 break;
308 case 67 + 32:
309 env->lr = ldq_p(mem_buf);
310 break;
311 case 68 + 32:
312 env->ctr = ldq_p(mem_buf);
313 break;
314 case 69 + 32:
315 env->xer = ldq_p(mem_buf);
316 break;
317 case 70 + 32:
318 /* fpscr */
319 store_fpscr(env, ldq_p(mem_buf), 0xffffffff);
320 break;
323 return r;