Update release-README after completing the 2.43 release.
[binutils-gdb.git] / sim / aarch64 / cpustate.c
blob295fe0c31082c83c8f48bbca302bfe72438a8870
1 /* cpustate.h -- Prototypes for AArch64 simulator functions.
3 Copyright (C) 2015-2024 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/>. */
22 /* This must come before any other includes. */
23 #include "defs.h"
25 #include <stdio.h>
26 #include <math.h>
28 #include "sim-main.h"
29 #include "sim-signal.h"
30 #include "cpustate.h"
31 #include "simulator.h"
32 #include "libiberty.h"
34 #include "aarch64-sim.h"
36 /* Some operands are allowed to access the stack pointer (reg 31).
37 For others a read from r31 always returns 0, and a write to r31 is ignored. */
38 #define reg_num(reg) (((reg) == R31 && !r31_is_sp) ? 32 : (reg))
40 void
41 aarch64_set_reg_u64 (sim_cpu *cpu, GReg reg, int r31_is_sp, uint64_t val)
43 struct aarch64_sim_cpu *aarch64_cpu = AARCH64_SIM_CPU (cpu);
45 if (reg == R31 && ! r31_is_sp)
47 TRACE_REGISTER (cpu, "GR[31] NOT CHANGED!");
48 return;
51 if (val != aarch64_cpu->gr[reg].u64)
52 TRACE_REGISTER (cpu,
53 "GR[%2d] changes from %16" PRIx64 " to %16" PRIx64,
54 reg, aarch64_cpu->gr[reg].u64, val);
56 aarch64_cpu->gr[reg].u64 = val;
59 void
60 aarch64_set_reg_s64 (sim_cpu *cpu, GReg reg, int r31_is_sp, int64_t val)
62 struct aarch64_sim_cpu *aarch64_cpu = AARCH64_SIM_CPU (cpu);
64 if (reg == R31 && ! r31_is_sp)
66 TRACE_REGISTER (cpu, "GR[31] NOT CHANGED!");
67 return;
70 if (val != aarch64_cpu->gr[reg].s64)
71 TRACE_REGISTER (cpu,
72 "GR[%2d] changes from %16" PRIx64 " to %16" PRIx64,
73 reg, aarch64_cpu->gr[reg].s64, val);
75 aarch64_cpu->gr[reg].s64 = val;
78 uint64_t
79 aarch64_get_reg_u64 (sim_cpu *cpu, GReg reg, int r31_is_sp)
81 return AARCH64_SIM_CPU (cpu)->gr[reg_num(reg)].u64;
84 int64_t
85 aarch64_get_reg_s64 (sim_cpu *cpu, GReg reg, int r31_is_sp)
87 return AARCH64_SIM_CPU (cpu)->gr[reg_num(reg)].s64;
90 uint32_t
91 aarch64_get_reg_u32 (sim_cpu *cpu, GReg reg, int r31_is_sp)
93 return AARCH64_SIM_CPU (cpu)->gr[reg_num(reg)].u32;
96 int32_t
97 aarch64_get_reg_s32 (sim_cpu *cpu, GReg reg, int r31_is_sp)
99 return AARCH64_SIM_CPU (cpu)->gr[reg_num(reg)].s32;
102 void
103 aarch64_set_reg_s32 (sim_cpu *cpu, GReg reg, int r31_is_sp, int32_t val)
105 struct aarch64_sim_cpu *aarch64_cpu = AARCH64_SIM_CPU (cpu);
107 if (reg == R31 && ! r31_is_sp)
109 TRACE_REGISTER (cpu, "GR[31] NOT CHANGED!");
110 return;
113 if (val != aarch64_cpu->gr[reg].s32)
114 TRACE_REGISTER (cpu, "GR[%2d] changes from %8x to %8x",
115 reg, aarch64_cpu->gr[reg].s32, val);
117 /* The ARM ARM states that (C1.2.4):
118 When the data size is 32 bits, the lower 32 bits of the
119 register are used and the upper 32 bits are ignored on
120 a read and cleared to zero on a write.
121 We simulate this by first clearing the whole 64-bits and
122 then writing to the 32-bit value in the GRegister union. */
123 aarch64_cpu->gr[reg].s64 = 0;
124 aarch64_cpu->gr[reg].s32 = val;
127 void
128 aarch64_set_reg_u32 (sim_cpu *cpu, GReg reg, int r31_is_sp, uint32_t val)
130 struct aarch64_sim_cpu *aarch64_cpu = AARCH64_SIM_CPU (cpu);
132 if (reg == R31 && ! r31_is_sp)
134 TRACE_REGISTER (cpu, "GR[31] NOT CHANGED!");
135 return;
138 if (val != aarch64_cpu->gr[reg].u32)
139 TRACE_REGISTER (cpu, "GR[%2d] changes from %8x to %8x",
140 reg, aarch64_cpu->gr[reg].u32, val);
142 aarch64_cpu->gr[reg].u64 = 0;
143 aarch64_cpu->gr[reg].u32 = val;
146 uint32_t
147 aarch64_get_reg_u16 (sim_cpu *cpu, GReg reg, int r31_is_sp)
149 return AARCH64_SIM_CPU (cpu)->gr[reg_num(reg)].u16;
152 int32_t
153 aarch64_get_reg_s16 (sim_cpu *cpu, GReg reg, int r31_is_sp)
155 return AARCH64_SIM_CPU (cpu)->gr[reg_num(reg)].s16;
158 uint32_t
159 aarch64_get_reg_u8 (sim_cpu *cpu, GReg reg, int r31_is_sp)
161 return AARCH64_SIM_CPU (cpu)->gr[reg_num(reg)].u8;
164 int32_t
165 aarch64_get_reg_s8 (sim_cpu *cpu, GReg reg, int r31_is_sp)
167 return AARCH64_SIM_CPU (cpu)->gr[reg_num(reg)].s8;
170 uint64_t
171 aarch64_get_PC (sim_cpu *cpu)
173 return AARCH64_SIM_CPU (cpu)->pc;
176 uint64_t
177 aarch64_get_next_PC (sim_cpu *cpu)
179 return AARCH64_SIM_CPU (cpu)->nextpc;
182 void
183 aarch64_set_next_PC (sim_cpu *cpu, uint64_t next)
185 struct aarch64_sim_cpu *aarch64_cpu = AARCH64_SIM_CPU (cpu);
187 if (next != aarch64_cpu->nextpc + 4)
188 TRACE_REGISTER (cpu,
189 "NextPC changes from %16" PRIx64 " to %16" PRIx64,
190 aarch64_cpu->nextpc, next);
192 aarch64_cpu->nextpc = next;
195 void
196 aarch64_set_next_PC_by_offset (sim_cpu *cpu, int64_t offset)
198 struct aarch64_sim_cpu *aarch64_cpu = AARCH64_SIM_CPU (cpu);
200 if (aarch64_cpu->pc + offset != aarch64_cpu->nextpc + 4)
201 TRACE_REGISTER (cpu,
202 "NextPC changes from %16" PRIx64 " to %16" PRIx64,
203 aarch64_cpu->nextpc, aarch64_cpu->pc + offset);
205 aarch64_cpu->nextpc = aarch64_cpu->pc + offset;
208 /* Install nextpc as current pc. */
209 void
210 aarch64_update_PC (sim_cpu *cpu)
212 struct aarch64_sim_cpu *aarch64_cpu = AARCH64_SIM_CPU (cpu);
214 aarch64_cpu->pc = aarch64_cpu->nextpc;
215 /* Rezero the register we hand out when asked for ZR just in case it
216 was used as the destination for a write by the previous
217 instruction. */
218 aarch64_cpu->gr[32].u64 = 0UL;
221 /* This instruction can be used to save the next PC to LR
222 just before installing a branch PC. */
223 void
224 aarch64_save_LR (sim_cpu *cpu)
226 struct aarch64_sim_cpu *aarch64_cpu = AARCH64_SIM_CPU (cpu);
228 if (aarch64_cpu->gr[LR].u64 != aarch64_cpu->nextpc)
229 TRACE_REGISTER (cpu,
230 "LR changes from %16" PRIx64 " to %16" PRIx64,
231 aarch64_cpu->gr[LR].u64, aarch64_cpu->nextpc);
233 aarch64_cpu->gr[LR].u64 = aarch64_cpu->nextpc;
236 static const char *
237 decode_cpsr (FlagMask flags)
239 switch (flags & CPSR_ALL_FLAGS)
241 default:
242 case 0: return "----";
243 case 1: return "---V";
244 case 2: return "--C-";
245 case 3: return "--CV";
246 case 4: return "-Z--";
247 case 5: return "-Z-V";
248 case 6: return "-ZC-";
249 case 7: return "-ZCV";
250 case 8: return "N---";
251 case 9: return "N--V";
252 case 10: return "N-C-";
253 case 11: return "N-CV";
254 case 12: return "NZ--";
255 case 13: return "NZ-V";
256 case 14: return "NZC-";
257 case 15: return "NZCV";
261 /* Retrieve the CPSR register as an int. */
262 uint32_t
263 aarch64_get_CPSR (sim_cpu *cpu)
265 return AARCH64_SIM_CPU (cpu)->CPSR;
268 /* Set the CPSR register as an int. */
269 void
270 aarch64_set_CPSR (sim_cpu *cpu, uint32_t new_flags)
272 struct aarch64_sim_cpu *aarch64_cpu = AARCH64_SIM_CPU (cpu);
274 if (TRACE_REGISTER_P (cpu))
276 if (aarch64_cpu->CPSR != new_flags)
277 TRACE_REGISTER (cpu,
278 "CPSR changes from %s to %s",
279 decode_cpsr (aarch64_cpu->CPSR), decode_cpsr (new_flags));
280 else
281 TRACE_REGISTER (cpu,
282 "CPSR stays at %s", decode_cpsr (aarch64_cpu->CPSR));
285 aarch64_cpu->CPSR = new_flags & CPSR_ALL_FLAGS;
288 /* Read a specific subset of the CPSR as a bit pattern. */
289 uint32_t
290 aarch64_get_CPSR_bits (sim_cpu *cpu, FlagMask mask)
292 return AARCH64_SIM_CPU (cpu)->CPSR & mask;
295 /* Assign a specific subset of the CPSR as a bit pattern. */
296 void
297 aarch64_set_CPSR_bits (sim_cpu *cpu, uint32_t mask, uint32_t value)
299 struct aarch64_sim_cpu *aarch64_cpu = AARCH64_SIM_CPU (cpu);
300 uint32_t old_flags = aarch64_cpu->CPSR;
302 mask &= CPSR_ALL_FLAGS;
303 aarch64_cpu->CPSR &= ~ mask;
304 aarch64_cpu->CPSR |= (value & mask);
306 if (old_flags != aarch64_cpu->CPSR)
307 TRACE_REGISTER (cpu,
308 "CPSR changes from %s to %s",
309 decode_cpsr (old_flags), decode_cpsr (aarch64_cpu->CPSR));
312 /* Test the value of a single CPSR returned as non-zero or zero. */
313 uint32_t
314 aarch64_test_CPSR_bit (sim_cpu *cpu, FlagMask bit)
316 return AARCH64_SIM_CPU (cpu)->CPSR & bit;
319 /* Set a single flag in the CPSR. */
320 void
321 aarch64_set_CPSR_bit (sim_cpu *cpu, FlagMask bit)
323 struct aarch64_sim_cpu *aarch64_cpu = AARCH64_SIM_CPU (cpu);
324 uint32_t old_flags = aarch64_cpu->CPSR;
326 aarch64_cpu->CPSR |= (bit & CPSR_ALL_FLAGS);
328 if (old_flags != aarch64_cpu->CPSR)
329 TRACE_REGISTER (cpu,
330 "CPSR changes from %s to %s",
331 decode_cpsr (old_flags), decode_cpsr (aarch64_cpu->CPSR));
334 /* Clear a single flag in the CPSR. */
335 void
336 aarch64_clear_CPSR_bit (sim_cpu *cpu, FlagMask bit)
338 struct aarch64_sim_cpu *aarch64_cpu = AARCH64_SIM_CPU (cpu);
339 uint32_t old_flags = aarch64_cpu->CPSR;
341 aarch64_cpu->CPSR &= ~(bit & CPSR_ALL_FLAGS);
343 if (old_flags != aarch64_cpu->CPSR)
344 TRACE_REGISTER (cpu,
345 "CPSR changes from %s to %s",
346 decode_cpsr (old_flags), decode_cpsr (aarch64_cpu->CPSR));
349 float
350 aarch64_get_FP_half (sim_cpu *cpu, VReg reg)
352 struct aarch64_sim_cpu *aarch64_cpu = AARCH64_SIM_CPU (cpu);
353 union
355 uint16_t h[2];
356 float f;
357 } u;
359 u.h[0] = 0;
360 u.h[1] = aarch64_cpu->fr[reg].h[0];
361 return u.f;
365 float
366 aarch64_get_FP_float (sim_cpu *cpu, VReg reg)
368 return AARCH64_SIM_CPU (cpu)->fr[reg].s;
371 double
372 aarch64_get_FP_double (sim_cpu *cpu, VReg reg)
374 return AARCH64_SIM_CPU (cpu)->fr[reg].d;
377 void
378 aarch64_get_FP_long_double (sim_cpu *cpu, VReg reg, FRegister *a)
380 struct aarch64_sim_cpu *aarch64_cpu = AARCH64_SIM_CPU (cpu);
382 a->v[0] = aarch64_cpu->fr[reg].v[0];
383 a->v[1] = aarch64_cpu->fr[reg].v[1];
386 void
387 aarch64_set_FP_half (sim_cpu *cpu, VReg reg, float val)
389 struct aarch64_sim_cpu *aarch64_cpu = AARCH64_SIM_CPU (cpu);
390 union
392 uint16_t h[2];
393 float f;
394 } u;
396 u.f = val;
397 aarch64_cpu->fr[reg].h[0] = u.h[1];
398 aarch64_cpu->fr[reg].h[1] = 0;
402 void
403 aarch64_set_FP_float (sim_cpu *cpu, VReg reg, float val)
405 struct aarch64_sim_cpu *aarch64_cpu = AARCH64_SIM_CPU (cpu);
407 if (val != aarch64_cpu->fr[reg].s
408 /* Handle +/- zero. */
409 || signbit (val) != signbit (aarch64_cpu->fr[reg].s))
411 FRegister v;
413 v.s = val;
414 TRACE_REGISTER (cpu,
415 "FR[%d].s changes from %f to %f [hex: %0" PRIx64 "]",
416 reg, aarch64_cpu->fr[reg].s, val, v.v[0]);
419 aarch64_cpu->fr[reg].s = val;
422 void
423 aarch64_set_FP_double (sim_cpu *cpu, VReg reg, double val)
425 struct aarch64_sim_cpu *aarch64_cpu = AARCH64_SIM_CPU (cpu);
427 if (val != aarch64_cpu->fr[reg].d
428 /* Handle +/- zero. */
429 || signbit (val) != signbit (aarch64_cpu->fr[reg].d))
431 FRegister v;
433 v.d = val;
434 TRACE_REGISTER (cpu,
435 "FR[%d].d changes from %f to %f [hex: %0" PRIx64 "]",
436 reg, aarch64_cpu->fr[reg].d, val, v.v[0]);
438 aarch64_cpu->fr[reg].d = val;
441 void
442 aarch64_set_FP_long_double (sim_cpu *cpu, VReg reg, FRegister a)
444 struct aarch64_sim_cpu *aarch64_cpu = AARCH64_SIM_CPU (cpu);
446 if (aarch64_cpu->fr[reg].v[0] != a.v[0]
447 || aarch64_cpu->fr[reg].v[1] != a.v[1])
448 TRACE_REGISTER (cpu,
449 "FR[%d].q changes from [%0" PRIx64 " %0" PRIx64 "] to [%0"
450 PRIx64 " %0" PRIx64 "] ",
451 reg,
452 aarch64_cpu->fr[reg].v[0], aarch64_cpu->fr[reg].v[1],
453 a.v[0], a.v[1]);
455 aarch64_cpu->fr[reg].v[0] = a.v[0];
456 aarch64_cpu->fr[reg].v[1] = a.v[1];
459 #define GET_VEC_ELEMENT(REG, ELEMENT, FIELD) \
460 do \
462 struct aarch64_sim_cpu *aarch64_cpu = AARCH64_SIM_CPU (cpu); \
464 if (ELEMENT >= ARRAY_SIZE (aarch64_cpu->fr[0].FIELD)) \
466 TRACE_REGISTER (cpu, \
467 "Internal SIM error: invalid element number: %d ",\
468 ELEMENT); \
469 sim_engine_halt (CPU_STATE (cpu), cpu, NULL, aarch64_get_PC (cpu), \
470 sim_stopped, SIM_SIGBUS); \
472 return aarch64_cpu->fr[REG].FIELD [ELEMENT]; \
474 while (0)
476 uint64_t
477 aarch64_get_vec_u64 (sim_cpu *cpu, VReg reg, unsigned element)
479 GET_VEC_ELEMENT (reg, element, v);
482 uint32_t
483 aarch64_get_vec_u32 (sim_cpu *cpu, VReg reg, unsigned element)
485 GET_VEC_ELEMENT (reg, element, w);
488 uint16_t
489 aarch64_get_vec_u16 (sim_cpu *cpu, VReg reg, unsigned element)
491 GET_VEC_ELEMENT (reg, element, h);
494 uint8_t
495 aarch64_get_vec_u8 (sim_cpu *cpu, VReg reg, unsigned element)
497 GET_VEC_ELEMENT (reg, element, b);
500 int64_t
501 aarch64_get_vec_s64 (sim_cpu *cpu, VReg reg, unsigned element)
503 GET_VEC_ELEMENT (reg, element, V);
506 int32_t
507 aarch64_get_vec_s32 (sim_cpu *cpu, VReg reg, unsigned element)
509 GET_VEC_ELEMENT (reg, element, W);
512 int16_t
513 aarch64_get_vec_s16 (sim_cpu *cpu, VReg reg, unsigned element)
515 GET_VEC_ELEMENT (reg, element, H);
518 int8_t
519 aarch64_get_vec_s8 (sim_cpu *cpu, VReg reg, unsigned element)
521 GET_VEC_ELEMENT (reg, element, B);
524 float
525 aarch64_get_vec_float (sim_cpu *cpu, VReg reg, unsigned element)
527 GET_VEC_ELEMENT (reg, element, S);
530 double
531 aarch64_get_vec_double (sim_cpu *cpu, VReg reg, unsigned element)
533 GET_VEC_ELEMENT (reg, element, D);
537 #define SET_VEC_ELEMENT(REG, ELEMENT, VAL, FIELD, PRINTER) \
538 do \
540 struct aarch64_sim_cpu *aarch64_cpu = AARCH64_SIM_CPU (cpu); \
542 if (ELEMENT >= ARRAY_SIZE (aarch64_cpu->fr[0].FIELD)) \
544 TRACE_REGISTER (cpu, \
545 "Internal SIM error: invalid element number: %d ",\
546 ELEMENT); \
547 sim_engine_halt (CPU_STATE (cpu), cpu, NULL, aarch64_get_PC (cpu), \
548 sim_stopped, SIM_SIGBUS); \
550 if (VAL != aarch64_cpu->fr[REG].FIELD [ELEMENT]) \
551 TRACE_REGISTER (cpu, \
552 "VR[%2d]." #FIELD " [%d] changes from " PRINTER \
553 " to " PRINTER , REG, \
554 ELEMENT, aarch64_cpu->fr[REG].FIELD [ELEMENT], VAL); \
556 aarch64_cpu->fr[REG].FIELD [ELEMENT] = VAL; \
558 while (0)
560 void
561 aarch64_set_vec_u64 (sim_cpu *cpu, VReg reg, unsigned element, uint64_t val)
563 SET_VEC_ELEMENT (reg, element, val, v, "%16" PRIx64);
566 void
567 aarch64_set_vec_u32 (sim_cpu *cpu, VReg reg, unsigned element, uint32_t val)
569 SET_VEC_ELEMENT (reg, element, val, w, "%8x");
572 void
573 aarch64_set_vec_u16 (sim_cpu *cpu, VReg reg, unsigned element, uint16_t val)
575 SET_VEC_ELEMENT (reg, element, val, h, "%4x");
578 void
579 aarch64_set_vec_u8 (sim_cpu *cpu, VReg reg, unsigned element, uint8_t val)
581 SET_VEC_ELEMENT (reg, element, val, b, "%x");
584 void
585 aarch64_set_vec_s64 (sim_cpu *cpu, VReg reg, unsigned element, int64_t val)
587 SET_VEC_ELEMENT (reg, element, val, V, "%16" PRIx64);
590 void
591 aarch64_set_vec_s32 (sim_cpu *cpu, VReg reg, unsigned element, int32_t val)
593 SET_VEC_ELEMENT (reg, element, val, W, "%8x");
596 void
597 aarch64_set_vec_s16 (sim_cpu *cpu, VReg reg, unsigned element, int16_t val)
599 SET_VEC_ELEMENT (reg, element, val, H, "%4x");
602 void
603 aarch64_set_vec_s8 (sim_cpu *cpu, VReg reg, unsigned element, int8_t val)
605 SET_VEC_ELEMENT (reg, element, val, B, "%x");
608 void
609 aarch64_set_vec_float (sim_cpu *cpu, VReg reg, unsigned element, float val)
611 SET_VEC_ELEMENT (reg, element, val, S, "%f");
614 void
615 aarch64_set_vec_double (sim_cpu *cpu, VReg reg, unsigned element, double val)
617 SET_VEC_ELEMENT (reg, element, val, D, "%f");
620 void
621 aarch64_set_FPSR (sim_cpu *cpu, uint32_t value)
623 struct aarch64_sim_cpu *aarch64_cpu = AARCH64_SIM_CPU (cpu);
625 if (aarch64_cpu->FPSR != value)
626 TRACE_REGISTER (cpu,
627 "FPSR changes from %x to %x", aarch64_cpu->FPSR, value);
629 aarch64_cpu->FPSR = value & FPSR_ALL_FPSRS;
632 uint32_t
633 aarch64_get_FPSR (sim_cpu *cpu)
635 return AARCH64_SIM_CPU (cpu)->FPSR;
638 void
639 aarch64_set_FPSR_bits (sim_cpu *cpu, uint32_t mask, uint32_t value)
641 struct aarch64_sim_cpu *aarch64_cpu = AARCH64_SIM_CPU (cpu);
642 uint32_t old_FPSR = aarch64_cpu->FPSR;
644 mask &= FPSR_ALL_FPSRS;
645 aarch64_cpu->FPSR &= ~mask;
646 aarch64_cpu->FPSR |= (value & mask);
648 if (aarch64_cpu->FPSR != old_FPSR)
649 TRACE_REGISTER (cpu,
650 "FPSR changes from %x to %x", old_FPSR, aarch64_cpu->FPSR);
653 uint32_t
654 aarch64_get_FPSR_bits (sim_cpu *cpu, uint32_t mask)
656 mask &= FPSR_ALL_FPSRS;
657 return AARCH64_SIM_CPU (cpu)->FPSR & mask;
661 aarch64_test_FPSR_bit (sim_cpu *cpu, FPSRMask flag)
663 return AARCH64_SIM_CPU (cpu)->FPSR & flag;
666 uint64_t
667 aarch64_get_thread_id (sim_cpu *cpu)
669 return AARCH64_SIM_CPU (cpu)->tpidr;
672 uint32_t
673 aarch64_get_FPCR (sim_cpu *cpu)
675 return AARCH64_SIM_CPU (cpu)->FPCR;
678 void
679 aarch64_set_FPCR (sim_cpu *cpu, uint32_t val)
681 struct aarch64_sim_cpu *aarch64_cpu = AARCH64_SIM_CPU (cpu);
683 if (aarch64_cpu->FPCR != val)
684 TRACE_REGISTER (cpu,
685 "FPCR changes from %x to %x", aarch64_cpu->FPCR, val);
686 aarch64_cpu->FPCR = val;