2 * Copyright (c) 2009-2014, 2016-2018 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include "arch/arm/utility.hh"
44 #include "arch/arm/faults.hh"
45 #include "arch/arm/isa_traits.hh"
46 #include "arch/arm/system.hh"
47 #include "arch/arm/tlb.hh"
48 #include "arch/arm/vtophys.hh"
49 #include "cpu/base.hh"
50 #include "cpu/checker/cpu.hh"
51 #include "cpu/thread_context.hh"
52 #include "mem/fs_translating_port_proxy.hh"
53 #include "sim/full_system.hh"
58 initCPU(ThreadContext
*tc
, int cpuId
)
60 // Reset CP15?? What does that mean -- ali
64 static Fault reset
= std::make_shared
<Reset
>();
69 getArgument(ThreadContext
*tc
, int &number
, uint16_t size
, bool fp
)
72 panic("getArgument() only implemented for full system mode.\n");
77 panic("getArgument(): Floating point arguments not implemented\n");
80 if (size
== (uint16_t)(-1))
81 size
= sizeof(uint64_t);
83 if (number
< 8 /*NumArgumentRegs64*/) {
84 return tc
->readIntReg(number
);
86 panic("getArgument(): No support reading stack args for AArch64\n");
89 if (size
== (uint16_t)(-1))
90 // todo: should this not be sizeof(uint32_t) rather?
91 size
= ArmISA::MachineBytes
;
93 if (number
< NumArgumentRegs
) {
94 // If the argument is 64 bits, it must be in an even regiser
95 // number. Increment the number here if it isn't even.
96 if (size
== sizeof(uint64_t)) {
97 if ((number
% 2) != 0)
99 // Read the two halves of the data. Number is inc here to
100 // get the second half of the 64 bit reg.
102 tmp
= tc
->readIntReg(number
++);
103 tmp
|= tc
->readIntReg(number
) << 32;
106 return tc
->readIntReg(number
);
109 Addr sp
= tc
->readIntReg(StackPointerReg
);
110 FSTranslatingPortProxy
&vp
= tc
->getVirtProxy();
112 if (size
== sizeof(uint64_t)) {
113 // If the argument is even it must be aligned
114 if ((number
% 2) != 0)
116 arg
= vp
.read
<uint64_t>(sp
+
117 (number
-NumArgumentRegs
) * sizeof(uint32_t));
118 // since two 32 bit args == 1 64 bit arg, increment number
121 arg
= vp
.read
<uint32_t>(sp
+
122 (number
-NumArgumentRegs
) * sizeof(uint32_t));
127 panic("getArgument() should always return\n");
131 skipFunction(ThreadContext
*tc
)
133 PCState newPC
= tc
->pcState();
135 newPC
.set(tc
->readIntReg(INTREG_X30
));
137 newPC
.set(tc
->readIntReg(ReturnAddressReg
) & ~ULL(1));
140 CheckerCPU
*checker
= tc
->getCheckerCpuPtr();
142 tc
->pcStateNoRecord(newPC
);
149 copyRegs(ThreadContext
*src
, ThreadContext
*dest
)
151 for (int i
= 0; i
< NumIntRegs
; i
++)
152 dest
->setIntRegFlat(i
, src
->readIntRegFlat(i
));
154 for (int i
= 0; i
< NumFloatRegs
; i
++)
155 dest
->setFloatRegBitsFlat(i
, src
->readFloatRegBitsFlat(i
));
157 for (int i
= 0; i
< NumVecRegs
; i
++)
158 dest
->setVecRegFlat(i
, src
->readVecRegFlat(i
));
160 for (int i
= 0; i
< NumCCRegs
; i
++)
161 dest
->setCCReg(i
, src
->readCCReg(i
));
163 for (int i
= 0; i
< NumMiscRegs
; i
++)
164 dest
->setMiscRegNoEffect(i
, src
->readMiscRegNoEffect(i
));
166 // setMiscReg "with effect" will set the misc register mapping correctly.
167 // e.g. updateRegMap(val)
168 dest
->setMiscReg(MISCREG_CPSR
, src
->readMiscRegNoEffect(MISCREG_CPSR
));
170 // Copy over the PC State
171 dest
->pcState(src
->pcState());
173 // Invalidate the tlb misc register cache
174 dynamic_cast<TLB
*>(dest
->getITBPtr())->invalidateMiscReg();
175 dynamic_cast<TLB
*>(dest
->getDTBPtr())->invalidateMiscReg();
179 inSecureState(ThreadContext
*tc
)
181 SCR scr
= inAArch64(tc
) ? tc
->readMiscReg(MISCREG_SCR_EL3
) :
182 tc
->readMiscReg(MISCREG_SCR
);
183 return ArmSystem::haveSecurity(tc
) && inSecureState(
184 scr
, tc
->readMiscReg(MISCREG_CPSR
));
188 isSecureBelowEL3(ThreadContext
*tc
)
190 SCR scr
= tc
->readMiscReg(MISCREG_SCR_EL3
);
191 return ArmSystem::haveEL(tc
, EL3
) && scr
.ns
== 0;
195 inAArch64(ThreadContext
*tc
)
197 CPSR cpsr
= tc
->readMiscReg(MISCREG_CPSR
);
198 return opModeIs64((OperatingMode
) (uint8_t) cpsr
.mode
);
202 longDescFormatInUse(ThreadContext
*tc
)
204 TTBCR ttbcr
= tc
->readMiscReg(MISCREG_TTBCR
);
205 return ArmSystem::haveLPAE(tc
) && ttbcr
.eae
;
209 getMPIDR(ArmSystem
*arm_sys
, ThreadContext
*tc
)
211 // Multiprocessor Affinity Register MPIDR from Cortex(tm)-A15 Technical
214 // bit 31 - Multi-processor extensions available
215 // bit 30 - Uni-processor system
216 // bit 24 - Multi-threaded cores
217 // bit 11-8 - Cluster ID
220 // We deliberately extend both the Cluster ID and CPU ID fields to allow
221 // for simulation of larger systems
222 assert((0 <= tc
->cpuId()) && (tc
->cpuId() < 256));
223 assert(tc
->socketId() < 65536);
224 if (arm_sys
->multiThread
) {
225 return 0x80000000 | // multiprocessor extensions available
226 0x01000000 | // multi-threaded cores
228 } else if (arm_sys
->multiProc
) {
229 return 0x80000000 | // multiprocessor extensions available
230 tc
->cpuId() | tc
->socketId() << 8;
232 return 0x80000000 | // multiprocessor extensions available
233 0x40000000 | // in up system
234 tc
->cpuId() | tc
->socketId() << 8;
239 ELIs64(ThreadContext
*tc
, ExceptionLevel el
)
241 return !ELIs32(tc
, el
);
245 ELIs32(ThreadContext
*tc
, ExceptionLevel el
)
248 std::tie(known
, aarch32
) = ELUsingAArch32K(tc
, el
);
249 panic_if(!known
, "EL state is UNKNOWN");
253 std::pair
<bool, bool>
254 ELUsingAArch32K(ThreadContext
*tc
, ExceptionLevel el
)
256 // Return true if the specified EL is in aarch32 state.
257 const bool have_el3
= ArmSystem::haveSecurity(tc
);
258 const bool have_el2
= ArmSystem::haveVirtualization(tc
);
260 panic_if(el
== EL2
&& !have_el2
, "Asking for EL2 when it doesn't exist");
261 panic_if(el
== EL3
&& !have_el3
, "Asking for EL3 when it doesn't exist");
264 known
= aarch32
= false;
265 if (ArmSystem::highestELIs64(tc
) && ArmSystem::highestEL(tc
) == el
) {
266 // Target EL is the highest one in a system where
267 // the highest is using AArch64.
268 known
= true; aarch32
= false;
269 } else if (!ArmSystem::highestELIs64(tc
)) {
270 // All ELs are using AArch32:
271 known
= true; aarch32
= true;
273 SCR scr
= tc
->readMiscReg(MISCREG_SCR_EL3
);
274 bool aarch32_below_el3
= (have_el3
&& scr
.rw
== 0);
276 HCR hcr
= tc
->readMiscReg(MISCREG_HCR_EL2
);
277 bool aarch32_at_el1
= (aarch32_below_el3
279 && !isSecureBelowEL3(tc
) && hcr
.rw
== 0));
281 // Only know if EL0 using AArch32 from PSTATE
282 if (el
== EL0
&& !aarch32_at_el1
) {
283 // EL0 controlled by PSTATE
284 CPSR cpsr
= tc
->readMiscReg(MISCREG_CPSR
);
286 known
= (cpsr
.el
== EL0
);
287 aarch32
= (cpsr
.width
== 1);
290 aarch32
= (aarch32_below_el3
&& el
!= EL3
)
291 || (aarch32_at_el1
&& (el
== EL0
|| el
== EL1
) );
295 return std::make_pair(known
, aarch32
);
299 isBigEndian64(ThreadContext
*tc
)
301 switch (opModeToEL(currOpMode(tc
))) {
303 return ((SCTLR
) tc
->readMiscReg(MISCREG_SCTLR_EL3
)).ee
;
305 return ((SCTLR
) tc
->readMiscReg(MISCREG_SCTLR_EL2
)).ee
;
307 return ((SCTLR
) tc
->readMiscReg(MISCREG_SCTLR_EL1
)).ee
;
309 return ((SCTLR
) tc
->readMiscReg(MISCREG_SCTLR_EL1
)).e0e
;
311 panic("Invalid exception level");
317 badMode32(ThreadContext
*tc
, OperatingMode mode
)
319 return unknownMode32(mode
) || !ArmSystem::haveEL(tc
, opModeToEL(mode
));
323 badMode(ThreadContext
*tc
, OperatingMode mode
)
325 return unknownMode(mode
) || !ArmSystem::haveEL(tc
, opModeToEL(mode
));
329 purifyTaggedAddr(Addr addr
, ThreadContext
*tc
, ExceptionLevel el
,
335 if (bits(addr
, 55, 48) == 0xFF && tcr
.tbi1
)
336 return addr
| mask(63, 55);
337 else if (!bits(addr
, 55, 48) && tcr
.tbi0
)
338 return bits(addr
,55, 0);
341 assert(ArmSystem::haveVirtualization(tc
));
342 tcr
= tc
->readMiscReg(MISCREG_TCR_EL2
);
344 return addr
& mask(56);
347 assert(ArmSystem::haveSecurity(tc
));
349 return addr
& mask(56);
352 panic("Invalid exception level");
356 return addr
; // Nothing to do if this is not a tagged address
360 purifyTaggedAddr(Addr addr
, ThreadContext
*tc
, ExceptionLevel el
)
367 tcr
= tc
->readMiscReg(MISCREG_TCR_EL1
);
368 if (bits(addr
, 55, 48) == 0xFF && tcr
.tbi1
)
369 return addr
| mask(63, 55);
370 else if (!bits(addr
, 55, 48) && tcr
.tbi0
)
371 return bits(addr
,55, 0);
374 assert(ArmSystem::haveVirtualization(tc
));
375 tcr
= tc
->readMiscReg(MISCREG_TCR_EL2
);
377 return addr
& mask(56);
380 assert(ArmSystem::haveSecurity(tc
));
381 tcr
= tc
->readMiscReg(MISCREG_TCR_EL3
);
383 return addr
& mask(56);
386 panic("Invalid exception level");
390 return addr
; // Nothing to do if this is not a tagged address
396 return addr
& ~(PageBytes
- 1);
402 return (addr
+ PageBytes
- 1) & ~(PageBytes
- 1);
406 mcrMrc15TrapToHyp(const MiscRegIndex miscReg
, HCR hcr
, CPSR cpsr
, SCR scr
,
407 HDCR hdcr
, HSTR hstr
, HCPTR hcptr
, uint32_t iss
)
415 bool trapToHype
= false;
418 if (!inSecureState(scr
, cpsr
) && (cpsr
.mode
!= MODE_HYP
)) {
419 mcrMrcIssExtract(iss
, isRead
, crm
, rt
, crn
, opc1
, opc2
);
420 trapToHype
= ((uint32_t) hstr
) & (1 << crn
);
421 trapToHype
|= hdcr
.tpm
&& (crn
== 9) && (crm
>= 12);
422 trapToHype
|= hcr
.tidcp
&& (
423 ((crn
== 9) && ((crm
<= 2) || ((crm
>= 5) && (crm
<= 8)))) ||
424 ((crn
== 10) && ((crm
<= 1) || (crm
== 4) || (crm
== 8))) ||
425 ((crn
== 11) && ((crm
<= 8) || (crm
== 15))) );
428 switch (unflattenMiscReg(miscReg
)) {
430 trapToHype
= hcptr
.tcpac
;
436 trapToHype
= hcr
.tid1
;
442 trapToHype
= hcr
.tid2
;
444 case MISCREG_ID_PFR0
:
445 case MISCREG_ID_PFR1
:
446 case MISCREG_ID_DFR0
:
447 case MISCREG_ID_AFR0
:
448 case MISCREG_ID_MMFR0
:
449 case MISCREG_ID_MMFR1
:
450 case MISCREG_ID_MMFR2
:
451 case MISCREG_ID_MMFR3
:
452 case MISCREG_ID_ISAR0
:
453 case MISCREG_ID_ISAR1
:
454 case MISCREG_ID_ISAR2
:
455 case MISCREG_ID_ISAR3
:
456 case MISCREG_ID_ISAR4
:
457 case MISCREG_ID_ISAR5
:
458 trapToHype
= hcr
.tid3
;
463 trapToHype
= hcr
.tsw
;
465 case MISCREG_DCIMVAC
:
466 case MISCREG_DCCIMVAC
:
467 case MISCREG_DCCMVAC
:
468 trapToHype
= hcr
.tpc
;
470 case MISCREG_ICIMVAU
:
471 case MISCREG_ICIALLU
:
472 case MISCREG_ICIALLUIS
:
473 case MISCREG_DCCMVAU
:
474 trapToHype
= hcr
.tpu
;
476 case MISCREG_TLBIALLIS
:
477 case MISCREG_TLBIMVAIS
:
478 case MISCREG_TLBIASIDIS
:
479 case MISCREG_TLBIMVAAIS
:
480 case MISCREG_TLBIMVALIS
:
481 case MISCREG_TLBIMVAALIS
:
482 case MISCREG_DTLBIALL
:
483 case MISCREG_ITLBIALL
:
484 case MISCREG_DTLBIMVA
:
485 case MISCREG_ITLBIMVA
:
486 case MISCREG_DTLBIASID
:
487 case MISCREG_ITLBIASID
:
488 case MISCREG_TLBIMVAA
:
489 case MISCREG_TLBIALL
:
490 case MISCREG_TLBIMVA
:
491 case MISCREG_TLBIMVAL
:
492 case MISCREG_TLBIMVAAL
:
493 case MISCREG_TLBIASID
:
494 trapToHype
= hcr
.ttlb
;
497 trapToHype
= hcr
.tac
;
514 case MISCREG_CONTEXTIDR
:
515 trapToHype
= hcr
.tvm
& !isRead
;
518 trapToHype
= hdcr
.tpmcr
;
520 // No default action needed
531 mcrMrc14TrapToHyp(const MiscRegIndex miscReg
, HCR hcr
, CPSR cpsr
, SCR scr
,
532 HDCR hdcr
, HSTR hstr
, HCPTR hcptr
, uint32_t iss
)
540 bool trapToHype
= false;
542 if (!inSecureState(scr
, cpsr
) && (cpsr
.mode
!= MODE_HYP
)) {
543 mcrMrcIssExtract(iss
, isRead
, crm
, rt
, crn
, opc1
, opc2
);
544 inform("trap check M:%x N:%x 1:%x 2:%x hdcr %x, hcptr %x, hstr %x\n",
545 crm
, crn
, opc1
, opc2
, hdcr
, hcptr
, hstr
);
546 trapToHype
= hdcr
.tda
&& (opc1
== 0);
547 trapToHype
|= hcptr
.tta
&& (opc1
== 1);
549 switch (unflattenMiscReg(miscReg
)) {
550 case MISCREG_DBGOSLSR
:
551 case MISCREG_DBGOSLAR
:
552 case MISCREG_DBGOSDLR
:
553 case MISCREG_DBGPRCR
:
554 trapToHype
= hdcr
.tdosa
;
556 case MISCREG_DBGDRAR
:
557 case MISCREG_DBGDSAR
:
558 trapToHype
= hdcr
.tdra
;
561 trapToHype
= hcr
.tid0
;
565 trapToHype
= hstr
.tjdbx
;
569 trapToHype
= hstr
.ttee
;
571 // No default action needed
581 mcrrMrrc15TrapToHyp(const MiscRegIndex miscReg
, CPSR cpsr
, SCR scr
, HSTR hstr
,
582 HCR hcr
, uint32_t iss
)
590 bool trapToHype
= false;
592 if (!inSecureState(scr
, cpsr
) && (cpsr
.mode
!= MODE_HYP
)) {
593 // This is technically the wrong function, but we can re-use it for
594 // the moment because we only need one field, which overlaps with the
596 mcrMrcIssExtract(iss
, isRead
, crm
, rt
, crn
, opc1
, opc2
);
597 trapToHype
= ((uint32_t) hstr
) & (1 << crm
);
600 switch (unflattenMiscReg(miscReg
)) {
616 case MISCREG_CONTEXTIDR
:
617 trapToHype
= hcr
.tvm
& !isRead
;
619 // No default action needed
629 decodeMrsMsrBankedReg(uint8_t sysM
, bool r
, bool &isIntReg
, int ®Idx
,
630 CPSR cpsr
, SCR scr
, NSACR nsacr
, bool checkSecurity
)
632 OperatingMode mode
= MODE_UNDEFINED
;
635 // R mostly indicates if its a int register or a misc reg, we override
636 // below if the few corner cases
638 // Loosely based on ARM ARM issue C section B9.3.10
643 regIdx
= MISCREG_SPSR_FIQ
;
647 regIdx
= MISCREG_SPSR_IRQ
;
651 regIdx
= MISCREG_SPSR_SVC
;
655 regIdx
= MISCREG_SPSR_ABT
;
659 regIdx
= MISCREG_SPSR_UND
;
660 mode
= MODE_UNDEFINED
;
663 regIdx
= MISCREG_SPSR_MON
;
667 regIdx
= MISCREG_SPSR_HYP
;
675 int sysM4To3
= bits(sysM
, 4, 3);
679 regIdx
= intRegInMode(mode
, bits(sysM
, 2, 0) + 8);
680 } else if (sysM4To3
== 1) {
682 regIdx
= intRegInMode(mode
, bits(sysM
, 2, 0) + 8);
683 } else if (sysM4To3
== 3) {
684 if (bits(sysM
, 1) == 0) {
686 regIdx
= intRegInMode(mode
, 14 - bits(sysM
, 0));
689 if (bits(sysM
, 0) == 1) {
690 regIdx
= intRegInMode(mode
, 13); // R13 in HYP
693 regIdx
= MISCREG_ELR_HYP
;
696 } else { // Other Banked registers
697 int sysM2
= bits(sysM
, 2);
698 int sysM1
= bits(sysM
, 1);
700 mode
= (OperatingMode
) ( ((sysM2
|| sysM1
) << 0) |
702 ((sysM2
&& !sysM1
) << 2) |
703 ((sysM2
&& sysM1
) << 3) |
705 regIdx
= intRegInMode(mode
, 14 - bits(sysM
, 0));
706 // Don't flatten the register here. This is going to go through
707 // setIntReg() which will do the flattening
708 ok
&= mode
!= cpsr
.mode
;
712 // Check that the requested register is accessable from the current mode
713 if (ok
&& checkSecurity
&& mode
!= cpsr
.mode
) {
720 ok
&= mode
!= MODE_HYP
;
721 ok
&= (mode
!= MODE_MON
) || !scr
.ns
;
724 ok
&= mode
!= MODE_MON
;
725 ok
&= (mode
!= MODE_FIQ
) || !nsacr
.rfr
;
732 ok
&= mode
!= MODE_HYP
;
733 ok
&= (mode
!= MODE_MON
) || !scr
.ns
;
734 ok
&= (mode
!= MODE_FIQ
) || !nsacr
.rfr
;
736 // can access everything, no further checks required
740 panic("unknown Mode 0x%x\n", cpsr
.mode
);
748 SPAlignmentCheckEnabled(ThreadContext
* tc
)
750 switch (opModeToEL(currOpMode(tc
))) {
752 return ((SCTLR
) tc
->readMiscReg(MISCREG_SCTLR_EL3
)).sa
;
754 return ((SCTLR
) tc
->readMiscReg(MISCREG_SCTLR_EL2
)).sa
;
756 return ((SCTLR
) tc
->readMiscReg(MISCREG_SCTLR_EL1
)).sa
;
758 return ((SCTLR
) tc
->readMiscReg(MISCREG_SCTLR_EL1
)).sa0
;
760 panic("Invalid exception level");
766 decodePhysAddrRange64(uint8_t pa_enc
)
784 panic("Invalid phys. address range encoding");
789 encodePhysAddrRange64(int pa_size
)
805 panic("Invalid phys. address range");
809 } // namespace ArmISA