1 /* cpustate.h -- Prototypes for AArch64 simulator functions.
3 Copyright (C) 2015-2019 Free Software Foundation, Inc.
5 Contributed by Red Hat.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #include "simulator.h"
28 #include "libiberty.h"
30 /* Some operands are allowed to access the stack pointer (reg 31).
31 For others a read from r31 always returns 0, and a write to r31 is ignored. */
32 #define reg_num(reg) (((reg) == R31 && !r31_is_sp) ? 32 : (reg))
35 aarch64_set_reg_u64 (sim_cpu
*cpu
, GReg reg
, int r31_is_sp
, uint64_t val
)
37 if (reg
== R31
&& ! r31_is_sp
)
39 TRACE_REGISTER (cpu
, "GR[31] NOT CHANGED!");
43 if (val
!= cpu
->gr
[reg
].u64
)
45 "GR[%2d] changes from %16" PRIx64
" to %16" PRIx64
,
46 reg
, cpu
->gr
[reg
].u64
, val
);
48 cpu
->gr
[reg
].u64
= val
;
52 aarch64_set_reg_s64 (sim_cpu
*cpu
, GReg reg
, int r31_is_sp
, int64_t val
)
54 if (reg
== R31
&& ! r31_is_sp
)
56 TRACE_REGISTER (cpu
, "GR[31] NOT CHANGED!");
60 if (val
!= cpu
->gr
[reg
].s64
)
62 "GR[%2d] changes from %16" PRIx64
" to %16" PRIx64
,
63 reg
, cpu
->gr
[reg
].s64
, val
);
65 cpu
->gr
[reg
].s64
= val
;
69 aarch64_get_reg_u64 (sim_cpu
*cpu
, GReg reg
, int r31_is_sp
)
71 return cpu
->gr
[reg_num(reg
)].u64
;
75 aarch64_get_reg_s64 (sim_cpu
*cpu
, GReg reg
, int r31_is_sp
)
77 return cpu
->gr
[reg_num(reg
)].s64
;
81 aarch64_get_reg_u32 (sim_cpu
*cpu
, GReg reg
, int r31_is_sp
)
83 return cpu
->gr
[reg_num(reg
)].u32
;
87 aarch64_get_reg_s32 (sim_cpu
*cpu
, GReg reg
, int r31_is_sp
)
89 return cpu
->gr
[reg_num(reg
)].s32
;
93 aarch64_set_reg_s32 (sim_cpu
*cpu
, GReg reg
, int r31_is_sp
, int32_t val
)
95 if (reg
== R31
&& ! r31_is_sp
)
97 TRACE_REGISTER (cpu
, "GR[31] NOT CHANGED!");
101 if (val
!= cpu
->gr
[reg
].s32
)
102 TRACE_REGISTER (cpu
, "GR[%2d] changes from %8x to %8x",
103 reg
, cpu
->gr
[reg
].s32
, val
);
105 /* The ARM ARM states that (C1.2.4):
106 When the data size is 32 bits, the lower 32 bits of the
107 register are used and the upper 32 bits are ignored on
108 a read and cleared to zero on a write.
109 We simulate this by first clearing the whole 64-bits and
110 then writing to the 32-bit value in the GRegister union. */
111 cpu
->gr
[reg
].s64
= 0;
112 cpu
->gr
[reg
].s32
= val
;
116 aarch64_set_reg_u32 (sim_cpu
*cpu
, GReg reg
, int r31_is_sp
, uint32_t val
)
118 if (reg
== R31
&& ! r31_is_sp
)
120 TRACE_REGISTER (cpu
, "GR[31] NOT CHANGED!");
124 if (val
!= cpu
->gr
[reg
].u32
)
125 TRACE_REGISTER (cpu
, "GR[%2d] changes from %8x to %8x",
126 reg
, cpu
->gr
[reg
].u32
, val
);
128 cpu
->gr
[reg
].u64
= 0;
129 cpu
->gr
[reg
].u32
= val
;
133 aarch64_get_reg_u16 (sim_cpu
*cpu
, GReg reg
, int r31_is_sp
)
135 return cpu
->gr
[reg_num(reg
)].u16
;
139 aarch64_get_reg_s16 (sim_cpu
*cpu
, GReg reg
, int r31_is_sp
)
141 return cpu
->gr
[reg_num(reg
)].s16
;
145 aarch64_get_reg_u8 (sim_cpu
*cpu
, GReg reg
, int r31_is_sp
)
147 return cpu
->gr
[reg_num(reg
)].u8
;
151 aarch64_get_reg_s8 (sim_cpu
*cpu
, GReg reg
, int r31_is_sp
)
153 return cpu
->gr
[reg_num(reg
)].s8
;
157 aarch64_get_PC (sim_cpu
*cpu
)
163 aarch64_get_next_PC (sim_cpu
*cpu
)
169 aarch64_set_next_PC (sim_cpu
*cpu
, uint64_t next
)
171 if (next
!= cpu
->nextpc
+ 4)
173 "NextPC changes from %16" PRIx64
" to %16" PRIx64
,
180 aarch64_set_next_PC_by_offset (sim_cpu
*cpu
, int64_t offset
)
182 if (cpu
->pc
+ offset
!= cpu
->nextpc
+ 4)
184 "NextPC changes from %16" PRIx64
" to %16" PRIx64
,
185 cpu
->nextpc
, cpu
->pc
+ offset
);
187 cpu
->nextpc
= cpu
->pc
+ offset
;
190 /* Install nextpc as current pc. */
192 aarch64_update_PC (sim_cpu
*cpu
)
194 cpu
->pc
= cpu
->nextpc
;
195 /* Rezero the register we hand out when asked for ZR just in case it
196 was used as the destination for a write by the previous
198 cpu
->gr
[32].u64
= 0UL;
201 /* This instruction can be used to save the next PC to LR
202 just before installing a branch PC. */
204 aarch64_save_LR (sim_cpu
*cpu
)
206 if (cpu
->gr
[LR
].u64
!= cpu
->nextpc
)
208 "LR changes from %16" PRIx64
" to %16" PRIx64
,
209 cpu
->gr
[LR
].u64
, cpu
->nextpc
);
211 cpu
->gr
[LR
].u64
= cpu
->nextpc
;
215 decode_cpsr (FlagMask flags
)
217 switch (flags
& CPSR_ALL_FLAGS
)
220 case 0: return "----";
221 case 1: return "---V";
222 case 2: return "--C-";
223 case 3: return "--CV";
224 case 4: return "-Z--";
225 case 5: return "-Z-V";
226 case 6: return "-ZC-";
227 case 7: return "-ZCV";
228 case 8: return "N---";
229 case 9: return "N--V";
230 case 10: return "N-C-";
231 case 11: return "N-CV";
232 case 12: return "NZ--";
233 case 13: return "NZ-V";
234 case 14: return "NZC-";
235 case 15: return "NZCV";
239 /* Retrieve the CPSR register as an int. */
241 aarch64_get_CPSR (sim_cpu
*cpu
)
246 /* Set the CPSR register as an int. */
248 aarch64_set_CPSR (sim_cpu
*cpu
, uint32_t new_flags
)
250 if (TRACE_REGISTER_P (cpu
))
252 if (cpu
->CPSR
!= new_flags
)
254 "CPSR changes from %s to %s",
255 decode_cpsr (cpu
->CPSR
), decode_cpsr (new_flags
));
258 "CPSR stays at %s", decode_cpsr (cpu
->CPSR
));
261 cpu
->CPSR
= new_flags
& CPSR_ALL_FLAGS
;
264 /* Read a specific subset of the CPSR as a bit pattern. */
266 aarch64_get_CPSR_bits (sim_cpu
*cpu
, FlagMask mask
)
268 return cpu
->CPSR
& mask
;
271 /* Assign a specific subset of the CPSR as a bit pattern. */
273 aarch64_set_CPSR_bits (sim_cpu
*cpu
, uint32_t mask
, uint32_t value
)
275 uint32_t old_flags
= cpu
->CPSR
;
277 mask
&= CPSR_ALL_FLAGS
;
279 cpu
->CPSR
|= (value
& mask
);
281 if (old_flags
!= cpu
->CPSR
)
283 "CPSR changes from %s to %s",
284 decode_cpsr (old_flags
), decode_cpsr (cpu
->CPSR
));
287 /* Test the value of a single CPSR returned as non-zero or zero. */
289 aarch64_test_CPSR_bit (sim_cpu
*cpu
, FlagMask bit
)
291 return cpu
->CPSR
& bit
;
294 /* Set a single flag in the CPSR. */
296 aarch64_set_CPSR_bit (sim_cpu
*cpu
, FlagMask bit
)
298 uint32_t old_flags
= cpu
->CPSR
;
300 cpu
->CPSR
|= (bit
& CPSR_ALL_FLAGS
);
302 if (old_flags
!= cpu
->CPSR
)
304 "CPSR changes from %s to %s",
305 decode_cpsr (old_flags
), decode_cpsr (cpu
->CPSR
));
308 /* Clear a single flag in the CPSR. */
310 aarch64_clear_CPSR_bit (sim_cpu
*cpu
, FlagMask bit
)
312 uint32_t old_flags
= cpu
->CPSR
;
314 cpu
->CPSR
&= ~(bit
& CPSR_ALL_FLAGS
);
316 if (old_flags
!= cpu
->CPSR
)
318 "CPSR changes from %s to %s",
319 decode_cpsr (old_flags
), decode_cpsr (cpu
->CPSR
));
323 aarch64_get_FP_half (sim_cpu
*cpu
, VReg reg
)
332 u
.h
[1] = cpu
->fr
[reg
].h
[0];
338 aarch64_get_FP_float (sim_cpu
*cpu
, VReg reg
)
340 return cpu
->fr
[reg
].s
;
344 aarch64_get_FP_double (sim_cpu
*cpu
, VReg reg
)
346 return cpu
->fr
[reg
].d
;
350 aarch64_get_FP_long_double (sim_cpu
*cpu
, VReg reg
, FRegister
*a
)
352 a
->v
[0] = cpu
->fr
[reg
].v
[0];
353 a
->v
[1] = cpu
->fr
[reg
].v
[1];
357 aarch64_set_FP_half (sim_cpu
*cpu
, VReg reg
, float val
)
366 cpu
->fr
[reg
].h
[0] = u
.h
[1];
367 cpu
->fr
[reg
].h
[1] = 0;
372 aarch64_set_FP_float (sim_cpu
*cpu
, VReg reg
, float val
)
374 if (val
!= cpu
->fr
[reg
].s
375 /* Handle +/- zero. */
376 || signbit (val
) != signbit (cpu
->fr
[reg
].s
))
382 "FR[%d].s changes from %f to %f [hex: %0lx]",
383 reg
, cpu
->fr
[reg
].s
, val
, v
.v
[0]);
386 cpu
->fr
[reg
].s
= val
;
390 aarch64_set_FP_double (sim_cpu
*cpu
, VReg reg
, double val
)
392 if (val
!= cpu
->fr
[reg
].d
393 /* Handle +/- zero. */
394 || signbit (val
) != signbit (cpu
->fr
[reg
].d
))
400 "FR[%d].d changes from %f to %f [hex: %0lx]",
401 reg
, cpu
->fr
[reg
].d
, val
, v
.v
[0]);
403 cpu
->fr
[reg
].d
= val
;
407 aarch64_set_FP_long_double (sim_cpu
*cpu
, VReg reg
, FRegister a
)
409 if (cpu
->fr
[reg
].v
[0] != a
.v
[0]
410 || cpu
->fr
[reg
].v
[1] != a
.v
[1])
412 "FR[%d].q changes from [%0lx %0lx] to [%0lx %0lx] ",
414 cpu
->fr
[reg
].v
[0], cpu
->fr
[reg
].v
[1],
417 cpu
->fr
[reg
].v
[0] = a
.v
[0];
418 cpu
->fr
[reg
].v
[1] = a
.v
[1];
421 #define GET_VEC_ELEMENT(REG, ELEMENT, FIELD) \
424 if (ELEMENT >= ARRAY_SIZE (cpu->fr[0].FIELD)) \
426 TRACE_REGISTER (cpu, \
427 "Internal SIM error: invalid element number: %d ",\
429 sim_engine_halt (CPU_STATE (cpu), cpu, NULL, aarch64_get_PC (cpu), \
430 sim_stopped, SIM_SIGBUS); \
432 return cpu->fr[REG].FIELD [ELEMENT]; \
437 aarch64_get_vec_u64 (sim_cpu
*cpu
, VReg reg
, unsigned element
)
439 GET_VEC_ELEMENT (reg
, element
, v
);
443 aarch64_get_vec_u32 (sim_cpu
*cpu
, VReg reg
, unsigned element
)
445 GET_VEC_ELEMENT (reg
, element
, w
);
449 aarch64_get_vec_u16 (sim_cpu
*cpu
, VReg reg
, unsigned element
)
451 GET_VEC_ELEMENT (reg
, element
, h
);
455 aarch64_get_vec_u8 (sim_cpu
*cpu
, VReg reg
, unsigned element
)
457 GET_VEC_ELEMENT (reg
, element
, b
);
461 aarch64_get_vec_s64 (sim_cpu
*cpu
, VReg reg
, unsigned element
)
463 GET_VEC_ELEMENT (reg
, element
, V
);
467 aarch64_get_vec_s32 (sim_cpu
*cpu
, VReg reg
, unsigned element
)
469 GET_VEC_ELEMENT (reg
, element
, W
);
473 aarch64_get_vec_s16 (sim_cpu
*cpu
, VReg reg
, unsigned element
)
475 GET_VEC_ELEMENT (reg
, element
, H
);
479 aarch64_get_vec_s8 (sim_cpu
*cpu
, VReg reg
, unsigned element
)
481 GET_VEC_ELEMENT (reg
, element
, B
);
485 aarch64_get_vec_float (sim_cpu
*cpu
, VReg reg
, unsigned element
)
487 GET_VEC_ELEMENT (reg
, element
, S
);
491 aarch64_get_vec_double (sim_cpu
*cpu
, VReg reg
, unsigned element
)
493 GET_VEC_ELEMENT (reg
, element
, D
);
497 #define SET_VEC_ELEMENT(REG, ELEMENT, VAL, FIELD, PRINTER) \
500 if (ELEMENT >= ARRAY_SIZE (cpu->fr[0].FIELD)) \
502 TRACE_REGISTER (cpu, \
503 "Internal SIM error: invalid element number: %d ",\
505 sim_engine_halt (CPU_STATE (cpu), cpu, NULL, aarch64_get_PC (cpu), \
506 sim_stopped, SIM_SIGBUS); \
508 if (VAL != cpu->fr[REG].FIELD [ELEMENT]) \
509 TRACE_REGISTER (cpu, \
510 "VR[%2d]." #FIELD " [%d] changes from " PRINTER \
511 " to " PRINTER , REG, \
512 ELEMENT, cpu->fr[REG].FIELD [ELEMENT], VAL); \
514 cpu->fr[REG].FIELD [ELEMENT] = VAL; \
519 aarch64_set_vec_u64 (sim_cpu
*cpu
, VReg reg
, unsigned element
, uint64_t val
)
521 SET_VEC_ELEMENT (reg
, element
, val
, v
, "%16lx");
525 aarch64_set_vec_u32 (sim_cpu
*cpu
, VReg reg
, unsigned element
, uint32_t val
)
527 SET_VEC_ELEMENT (reg
, element
, val
, w
, "%8x");
531 aarch64_set_vec_u16 (sim_cpu
*cpu
, VReg reg
, unsigned element
, uint16_t val
)
533 SET_VEC_ELEMENT (reg
, element
, val
, h
, "%4x");
537 aarch64_set_vec_u8 (sim_cpu
*cpu
, VReg reg
, unsigned element
, uint8_t val
)
539 SET_VEC_ELEMENT (reg
, element
, val
, b
, "%x");
543 aarch64_set_vec_s64 (sim_cpu
*cpu
, VReg reg
, unsigned element
, int64_t val
)
545 SET_VEC_ELEMENT (reg
, element
, val
, V
, "%16lx");
549 aarch64_set_vec_s32 (sim_cpu
*cpu
, VReg reg
, unsigned element
, int32_t val
)
551 SET_VEC_ELEMENT (reg
, element
, val
, W
, "%8x");
555 aarch64_set_vec_s16 (sim_cpu
*cpu
, VReg reg
, unsigned element
, int16_t val
)
557 SET_VEC_ELEMENT (reg
, element
, val
, H
, "%4x");
561 aarch64_set_vec_s8 (sim_cpu
*cpu
, VReg reg
, unsigned element
, int8_t val
)
563 SET_VEC_ELEMENT (reg
, element
, val
, B
, "%x");
567 aarch64_set_vec_float (sim_cpu
*cpu
, VReg reg
, unsigned element
, float val
)
569 SET_VEC_ELEMENT (reg
, element
, val
, S
, "%f");
573 aarch64_set_vec_double (sim_cpu
*cpu
, VReg reg
, unsigned element
, double val
)
575 SET_VEC_ELEMENT (reg
, element
, val
, D
, "%f");
579 aarch64_set_FPSR (sim_cpu
*cpu
, uint32_t value
)
581 if (cpu
->FPSR
!= value
)
583 "FPSR changes from %x to %x", cpu
->FPSR
, value
);
585 cpu
->FPSR
= value
& FPSR_ALL_FPSRS
;
589 aarch64_get_FPSR (sim_cpu
*cpu
)
595 aarch64_set_FPSR_bits (sim_cpu
*cpu
, uint32_t mask
, uint32_t value
)
597 uint32_t old_FPSR
= cpu
->FPSR
;
599 mask
&= FPSR_ALL_FPSRS
;
601 cpu
->FPSR
|= (value
& mask
);
603 if (cpu
->FPSR
!= old_FPSR
)
605 "FPSR changes from %x to %x", old_FPSR
, cpu
->FPSR
);
609 aarch64_get_FPSR_bits (sim_cpu
*cpu
, uint32_t mask
)
611 mask
&= FPSR_ALL_FPSRS
;
612 return cpu
->FPSR
& mask
;
616 aarch64_test_FPSR_bit (sim_cpu
*cpu
, FPSRMask flag
)
618 return cpu
->FPSR
& flag
;
622 aarch64_get_thread_id (sim_cpu
*cpu
)
628 aarch64_get_FPCR (sim_cpu
*cpu
)
634 aarch64_set_FPCR (sim_cpu
*cpu
, uint32_t val
)
636 if (cpu
->FPCR
!= val
)
638 "FPCR changes from %x to %x", cpu
->FPCR
, val
);