utility.cc revision 13500
12SN/A/*
22188SN/A * Copyright (c) 2009-2014, 2016-2018 ARM Limited
32SN/A * All rights reserved.
42SN/A *
52SN/A * The license below extends only to copyright in the software and shall
62SN/A * not be construed as granting a license to any other intellectual
72SN/A * property including but not limited to intellectual property relating
82SN/A * to a hardware implementation of the functionality of the software
92SN/A * licensed hereunder.  You may use the software subject to the license
102SN/A * terms below provided that you ensure that this notice is replicated
112SN/A * unmodified and in its entirety in all distributions of the software,
122SN/A * modified or unmodified, in source code or in binary form.
132SN/A *
142SN/A * Redistribution and use in source and binary forms, with or without
152SN/A * modification, are permitted provided that the following conditions are
162SN/A * met: redistributions of source code must retain the above copyright
172SN/A * notice, this list of conditions and the following disclaimer;
182SN/A * redistributions in binary form must reproduce the above copyright
192SN/A * notice, this list of conditions and the following disclaimer in the
202SN/A * documentation and/or other materials provided with the distribution;
212SN/A * neither the name of the copyright holders nor the names of its
222SN/A * contributors may be used to endorse or promote products derived from
232SN/A * this software without specific prior written permission.
242SN/A *
252SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
262SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
272665SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
282665SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
292665SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
302665SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
312665SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
322SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
332SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
342SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
352SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
362465SN/A *
373565Sgblack@eecs.umich.edu * Authors: Ali Saidi
385529Snate@binkert.org */
398777Sgblack@eecs.umich.edu
401917SN/A#include "arch/arm/utility.hh"
411070SN/A
421917SN/A#include <memory>
432188SN/A
448777Sgblack@eecs.umich.edu#include "arch/arm/faults.hh"
458777Sgblack@eecs.umich.edu#include "arch/arm/isa_traits.hh"
461917SN/A#include "arch/arm/system.hh"
472290SN/A#include "arch/arm/tlb.hh"
488777Sgblack@eecs.umich.edu#include "arch/arm/vtophys.hh"
498777Sgblack@eecs.umich.edu#include "cpu/base.hh"
508777Sgblack@eecs.umich.edu#include "cpu/checker/cpu.hh"
518777Sgblack@eecs.umich.edu#include "cpu/thread_context.hh"
528777Sgblack@eecs.umich.edu#include "mem/fs_translating_port_proxy.hh"
538793Sgblack@eecs.umich.edu#include "sim/full_system.hh"
548777Sgblack@eecs.umich.edu
551070SN/Anamespace ArmISA {
561917SN/A
572519SN/Avoid
582SN/AinitCPU(ThreadContext *tc, int cpuId)
592SN/A{
602SN/A    // Reset CP15?? What does that mean -- ali
612SN/A
628766Sgblack@eecs.umich.edu    // FPEXC.EN = 0
638766Sgblack@eecs.umich.edu
648766Sgblack@eecs.umich.edu    static Fault reset = std::make_shared<Reset>();
658766Sgblack@eecs.umich.edu    reset->invoke(tc);
668766Sgblack@eecs.umich.edu}
678766Sgblack@eecs.umich.edu
688766Sgblack@eecs.umich.eduuint64_t
698766Sgblack@eecs.umich.edugetArgument(ThreadContext *tc, int &number, uint16_t size, bool fp)
702683Sktlim@umich.edu{
716022Sgblack@eecs.umich.edu    if (!FullSystem) {
722683Sktlim@umich.edu        panic("getArgument() only implemented for full system mode.\n");
738766Sgblack@eecs.umich.edu        M5_DUMMY_RETURN
746324Sgblack@eecs.umich.edu    }
752521SN/A
762SN/A    if (fp)
772683Sktlim@umich.edu        panic("getArgument(): Floating point arguments not implemented\n");
782190SN/A
792680SN/A    if (inAArch64(tc)) {
802290SN/A        if (size == (uint16_t)(-1))
816316Sgblack@eecs.umich.edu            size = sizeof(uint64_t);
821917SN/A
835529Snate@binkert.org        if (number < 8 /*NumArgumentRegs64*/) {
841982SN/A               return tc->readIntReg(number);
851917SN/A        } else {
862683Sktlim@umich.edu            panic("getArgument(): No support reading stack args for AArch64\n");
872683Sktlim@umich.edu        }
881917SN/A    } else {
891917SN/A        if (size == (uint16_t)(-1))
901917SN/A            // todo: should this not be sizeof(uint32_t) rather?
911917SN/A            size = ArmISA::MachineBytes;
921917SN/A
931917SN/A        if (number < NumArgumentRegs) {
941917SN/A            // If the argument is 64 bits, it must be in an even regiser
951917SN/A            // number. Increment the number here if it isn't even.
962521SN/A            if (size == sizeof(uint64_t)) {
975482Snate@binkert.org                if ((number % 2) != 0)
983548Sgblack@eecs.umich.edu                    number++;
992SN/A                // Read the two halves of the data. Number is inc here to
1002862Sktlim@umich.edu                // get the second half of the 64 bit reg.
1012864Sktlim@umich.edu                uint64_t tmp;
1026331Sgblack@eecs.umich.edu                tmp = tc->readIntReg(number++);
1032190SN/A                tmp |= tc->readIntReg(number) << 32;
1042683Sktlim@umich.edu                return tmp;
1052190SN/A            } else {
1062190SN/A               return tc->readIntReg(number);
1072683Sktlim@umich.edu            }
1081070SN/A        } else {
1098754Sgblack@eecs.umich.edu            Addr sp = tc->readIntReg(StackPointerReg);
1103486Sktlim@umich.edu            FSTranslatingPortProxy &vp = tc->getVirtProxy();
1112680SN/A            uint64_t arg;
1121070SN/A            if (size == sizeof(uint64_t)) {
1131070SN/A                // If the argument is even it must be aligned
1141917SN/A                if ((number % 2) != 0)
1152683Sktlim@umich.edu                    number++;
116180SN/A                arg = vp.read<uint64_t>(sp +
117180SN/A                        (number-NumArgumentRegs) * sizeof(uint32_t));
1188793Sgblack@eecs.umich.edu                // since two 32 bit args == 1 64 bit arg, increment number
1198793Sgblack@eecs.umich.edu                number++;
1202235SN/A            } else {
121180SN/A                arg = vp.read<uint32_t>(sp +
1222862Sktlim@umich.edu                               (number-NumArgumentRegs) * sizeof(uint32_t));
1238793Sgblack@eecs.umich.edu            }
1248793Sgblack@eecs.umich.edu            return arg;
1258793Sgblack@eecs.umich.edu        }
1268793Sgblack@eecs.umich.edu    }
1278793Sgblack@eecs.umich.edu    panic("getArgument() should always return\n");
1288793Sgblack@eecs.umich.edu}
1298793Sgblack@eecs.umich.edu
1308793Sgblack@eecs.umich.eduvoid
1318793Sgblack@eecs.umich.eduskipFunction(ThreadContext *tc)
1328793Sgblack@eecs.umich.edu{
1338793Sgblack@eecs.umich.edu    PCState newPC = tc->pcState();
1348793Sgblack@eecs.umich.edu    if (inAArch64(tc)) {
1358793Sgblack@eecs.umich.edu        newPC.set(tc->readIntReg(INTREG_X30));
1368793Sgblack@eecs.umich.edu    } else {
1378793Sgblack@eecs.umich.edu        newPC.set(tc->readIntReg(ReturnAddressReg) & ~ULL(1));
1382313SN/A    }
139180SN/A
140180SN/A    CheckerCPU *checker = tc->getCheckerCpuPtr();
141180SN/A    if (checker) {
1426029Ssteve.reinhardt@amd.com        tc->pcStateNoRecord(newPC);
143180SN/A    } else {
144180SN/A        tc->pcState(newPC);
1452SN/A    }
1462864Sktlim@umich.edu}
1472864Sktlim@umich.edu
1482864Sktlim@umich.eduvoid
1492864Sktlim@umich.educopyRegs(ThreadContext *src, ThreadContext *dest)
1508793Sgblack@eecs.umich.edu{
1518793Sgblack@eecs.umich.edu    for (int i = 0; i < NumIntRegs; i++)
1528793Sgblack@eecs.umich.edu        dest->setIntRegFlat(i, src->readIntRegFlat(i));
1538793Sgblack@eecs.umich.edu
1548793Sgblack@eecs.umich.edu    for (int i = 0; i < NumFloatRegs; i++)
1558793Sgblack@eecs.umich.edu        dest->setFloatRegBitsFlat(i, src->readFloatRegBitsFlat(i));
1568793Sgblack@eecs.umich.edu
1578793Sgblack@eecs.umich.edu    for (int i = 0; i < NumVecRegs; i++)
1588793Sgblack@eecs.umich.edu        dest->setVecRegFlat(i, src->readVecRegFlat(i));
1592864Sktlim@umich.edu
1602864Sktlim@umich.edu    for (int i = 0; i < NumCCRegs; i++)
1612864Sktlim@umich.edu        dest->setCCReg(i, src->readCCReg(i));
1622864Sktlim@umich.edu
1632862Sktlim@umich.edu    for (int i = 0; i < NumMiscRegs; i++)
1642862Sktlim@umich.edu        dest->setMiscRegNoEffect(i, src->readMiscRegNoEffect(i));
1652862Sktlim@umich.edu
1662862Sktlim@umich.edu    // setMiscReg "with effect" will set the misc register mapping correctly.
1672862Sktlim@umich.edu    // e.g. updateRegMap(val)
1688793Sgblack@eecs.umich.edu    dest->setMiscReg(MISCREG_CPSR, src->readMiscRegNoEffect(MISCREG_CPSR));
1698793Sgblack@eecs.umich.edu
1705714Shsul@eecs.umich.edu    // Copy over the PC State
1715715Shsul@eecs.umich.edu    dest->pcState(src->pcState());
1725714Shsul@eecs.umich.edu
1732862Sktlim@umich.edu    // Invalidate the tlb misc register cache
1742862Sktlim@umich.edu    dynamic_cast<TLB *>(dest->getITBPtr())->invalidateMiscReg();
1752862Sktlim@umich.edu    dynamic_cast<TLB *>(dest->getDTBPtr())->invalidateMiscReg();
1762683Sktlim@umich.edu}
177217SN/A
1782862Sktlim@umich.edubool
1796315Sgblack@eecs.umich.eduinSecureState(ThreadContext *tc)
1806316Sgblack@eecs.umich.edu{
1817720Sgblack@eecs.umich.edu    SCR scr = inAArch64(tc) ? tc->readMiscReg(MISCREG_SCR_EL3) :
182223SN/A        tc->readMiscReg(MISCREG_SCR);
1836677SBrad.Beckmann@amd.com    return ArmSystem::haveSecurity(tc) && inSecureState(
1846677SBrad.Beckmann@amd.com        scr, tc->readMiscReg(MISCREG_CPSR));
1856677SBrad.Beckmann@amd.com}
1866677SBrad.Beckmann@amd.com
1876678Sgblack@eecs.umich.eduinline bool
188217SN/AisSecureBelowEL3(ThreadContext *tc)
189217SN/A{
190217SN/A    SCR scr = tc->readMiscReg(MISCREG_SCR_EL3);
191217SN/A    return ArmSystem::haveEL(tc, EL3) && scr.ns == 0;
1922683Sktlim@umich.edu}
193217SN/A
1942862Sktlim@umich.edubool
1956315Sgblack@eecs.umich.eduinAArch64(ThreadContext *tc)
1966316Sgblack@eecs.umich.edu{
1977720Sgblack@eecs.umich.edu    CPSR cpsr = tc->readMiscReg(MISCREG_CPSR);
198223SN/A    return opModeIs64((OperatingMode) (uint8_t) cpsr.mode);
1996677SBrad.Beckmann@amd.com}
2006677SBrad.Beckmann@amd.com
2016677SBrad.Beckmann@amd.combool
2026677SBrad.Beckmann@amd.comlongDescFormatInUse(ThreadContext *tc)
2036678Sgblack@eecs.umich.edu{
204217SN/A    TTBCR ttbcr = tc->readMiscReg(MISCREG_TTBCR);
205217SN/A    return ArmSystem::haveLPAE(tc) && ttbcr.eae;
2062683Sktlim@umich.edu}
2072683Sktlim@umich.edu
2082683Sktlim@umich.eduuint32_t
2092683Sktlim@umich.edugetMPIDR(ArmSystem *arm_sys, ThreadContext *tc)
2102683Sktlim@umich.edu{
2112683Sktlim@umich.edu    // Multiprocessor Affinity Register MPIDR from Cortex(tm)-A15 Technical
212217SN/A    // Reference Manual
213217SN/A    //
2142683Sktlim@umich.edu    // bit   31 - Multi-processor extensions available
2152SN/A    // bit   30 - Uni-processor system
2162680SN/A    // bit   24 - Multi-threaded cores
2172SN/A    // bit 11-8 - Cluster ID
2182SN/A    // bit  1-0 - CPU ID
2197823Ssteve.reinhardt@amd.com    //
2202188SN/A    // We deliberately extend both the Cluster ID and CPU ID fields to allow
2214400Srdreslin@umich.edu    // for simulation of larger systems
2225715Shsul@eecs.umich.edu    assert((0 <= tc->cpuId()) && (tc->cpuId() < 256));
2235543Ssaidi@eecs.umich.edu    assert(tc->socketId() < 65536);
2244400Srdreslin@umich.edu    if (arm_sys->multiThread) {
2252290SN/A       return 0x80000000 | // multiprocessor extensions available
2262680SN/A              0x01000000 | // multi-threaded cores
2272290SN/A              tc->contextId();
2282290SN/A    } else if (arm_sys->multiProc) {
2295715Shsul@eecs.umich.edu       return 0x80000000 | // multiprocessor extensions available
230393SN/A              tc->cpuId() | tc->socketId() << 8;
231393SN/A    } else {
232393SN/A       return 0x80000000 |  // multiprocessor extensions available
2332683Sktlim@umich.edu              0x40000000 |  // in up system
234393SN/A              tc->cpuId() | tc->socketId() << 8;
2352680SN/A    }
236393SN/A}
237393SN/A
2387823Ssteve.reinhardt@amd.combool
2397823Ssteve.reinhardt@amd.comELIs64(ThreadContext *tc, ExceptionLevel el)
2402680SN/A{
2415715Shsul@eecs.umich.edu    return !ELIs32(tc, el);
2422SN/A}
2432SN/A
244393SN/Abool
245393SN/AELIs32(ThreadContext *tc, ExceptionLevel el)
2462683Sktlim@umich.edu{
247393SN/A    bool known, aarch32;
2482680SN/A    std::tie(known, aarch32) = ELUsingAArch32K(tc, el);
249393SN/A    panic_if(!known, "EL state is UNKNOWN");
250393SN/A    return aarch32;
2512680SN/A}
2525715Shsul@eecs.umich.edu
253393SN/Astd::pair<bool, bool>
254393SN/AELUsingAArch32K(ThreadContext *tc, ExceptionLevel el)
255393SN/A{
256393SN/A    // Return true if the specified EL is in aarch32 state.
2572683Sktlim@umich.edu    const bool have_el3 = ArmSystem::haveSecurity(tc);
2582SN/A    const bool have_el2 = ArmSystem::haveVirtualization(tc);
2598793Sgblack@eecs.umich.edu
2602341SN/A    panic_if(el == EL2 && !have_el2, "Asking for EL2 when it doesn't exist");
2612SN/A    panic_if(el == EL3 && !have_el3, "Asking for EL3 when it doesn't exist");
262716SN/A
263716SN/A    bool known, aarch32;
2642683Sktlim@umich.edu    known = aarch32 = false;
2652190SN/A    if (ArmSystem::highestELIs64(tc) && ArmSystem::highestEL(tc) == el) {
2662680SN/A        // Target EL is the highest one in a system where
2672190SN/A        // the highest is using AArch64.
2682190SN/A        known = true; aarch32 = false;
269    } else if (!ArmSystem::highestELIs64(tc)) {
270        // All ELs are using AArch32:
271        known = true; aarch32 = true;
272    } else {
273        SCR scr = tc->readMiscReg(MISCREG_SCR_EL3);
274        bool aarch32_below_el3 = (have_el3 && scr.rw == 0);
275
276        HCR hcr = tc->readMiscReg(MISCREG_HCR_EL2);
277        bool aarch32_at_el1 = (aarch32_below_el3
278                               || (have_el2
279                               && !isSecureBelowEL3(tc) && hcr.rw == 0));
280
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);
285
286            known = (cpsr.el == EL0);
287            aarch32 = (cpsr.width == 1);
288        } else {
289            known = true;
290            aarch32 = (aarch32_below_el3 && el != EL3)
291                      || (aarch32_at_el1 && (el == EL0 || el == EL1) );
292        }
293    }
294
295    return std::make_pair(known, aarch32);
296}
297
298bool
299isBigEndian64(ThreadContext *tc)
300{
301    switch (opModeToEL(currOpMode(tc))) {
302      case EL3:
303        return ((SCTLR) tc->readMiscReg(MISCREG_SCTLR_EL3)).ee;
304      case EL2:
305        return ((SCTLR) tc->readMiscReg(MISCREG_SCTLR_EL2)).ee;
306      case EL1:
307        return ((SCTLR) tc->readMiscReg(MISCREG_SCTLR_EL1)).ee;
308      case EL0:
309        return ((SCTLR) tc->readMiscReg(MISCREG_SCTLR_EL1)).e0e;
310      default:
311        panic("Invalid exception level");
312        break;
313    }
314}
315
316bool
317badMode32(ThreadContext *tc, OperatingMode mode)
318{
319    return unknownMode32(mode) || !ArmSystem::haveEL(tc, opModeToEL(mode));
320}
321
322bool
323badMode(ThreadContext *tc, OperatingMode mode)
324{
325    return unknownMode(mode) || !ArmSystem::haveEL(tc, opModeToEL(mode));
326}
327
328Addr
329purifyTaggedAddr(Addr addr, ThreadContext *tc, ExceptionLevel el,
330                 TTBCR tcr)
331{
332    switch (el) {
333      case EL0:
334      case EL1:
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);
339        break;
340      case EL2:
341        assert(ArmSystem::haveVirtualization(tc));
342        tcr = tc->readMiscReg(MISCREG_TCR_EL2);
343        if (tcr.tbi)
344            return addr & mask(56);
345        break;
346      case EL3:
347        assert(ArmSystem::haveSecurity(tc));
348        if (tcr.tbi)
349            return addr & mask(56);
350        break;
351      default:
352        panic("Invalid exception level");
353        break;
354    }
355
356    return addr;  // Nothing to do if this is not a tagged address
357}
358
359Addr
360purifyTaggedAddr(Addr addr, ThreadContext *tc, ExceptionLevel el)
361{
362    TTBCR tcr;
363
364    switch (el) {
365      case EL0:
366      case EL1:
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);
372        break;
373      case EL2:
374        assert(ArmSystem::haveVirtualization(tc));
375        tcr = tc->readMiscReg(MISCREG_TCR_EL2);
376        if (tcr.tbi)
377            return addr & mask(56);
378        break;
379      case EL3:
380        assert(ArmSystem::haveSecurity(tc));
381        tcr = tc->readMiscReg(MISCREG_TCR_EL3);
382        if (tcr.tbi)
383            return addr & mask(56);
384        break;
385      default:
386        panic("Invalid exception level");
387        break;
388    }
389
390    return addr;  // Nothing to do if this is not a tagged address
391}
392
393Addr
394truncPage(Addr addr)
395{
396    return addr & ~(PageBytes - 1);
397}
398
399Addr
400roundPage(Addr addr)
401{
402    return (addr + PageBytes - 1) & ~(PageBytes - 1);
403}
404
405bool
406mcrMrc15TrapToHyp(const MiscRegIndex miscReg, HCR hcr, CPSR cpsr, SCR scr,
407                  HDCR hdcr, HSTR hstr, HCPTR hcptr, uint32_t iss)
408{
409    bool        isRead;
410    uint32_t    crm;
411    IntRegIndex rt;
412    uint32_t    crn;
413    uint32_t    opc1;
414    uint32_t    opc2;
415    bool        trapToHype = false;
416
417
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)))               );
426
427        if (!trapToHype) {
428            switch (unflattenMiscReg(miscReg)) {
429              case MISCREG_CPACR:
430                trapToHype = hcptr.tcpac;
431                break;
432              case MISCREG_REVIDR:
433              case MISCREG_TCMTR:
434              case MISCREG_TLBTR:
435              case MISCREG_AIDR:
436                trapToHype = hcr.tid1;
437                break;
438              case MISCREG_CTR:
439              case MISCREG_CCSIDR:
440              case MISCREG_CLIDR:
441              case MISCREG_CSSELR:
442                trapToHype = hcr.tid2;
443                break;
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;
459                break;
460              case MISCREG_DCISW:
461              case MISCREG_DCCSW:
462              case MISCREG_DCCISW:
463                trapToHype = hcr.tsw;
464                break;
465              case MISCREG_DCIMVAC:
466              case MISCREG_DCCIMVAC:
467              case MISCREG_DCCMVAC:
468                trapToHype = hcr.tpc;
469                break;
470              case MISCREG_ICIMVAU:
471              case MISCREG_ICIALLU:
472              case MISCREG_ICIALLUIS:
473              case MISCREG_DCCMVAU:
474                trapToHype = hcr.tpu;
475                break;
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;
495                break;
496              case MISCREG_ACTLR:
497                trapToHype = hcr.tac;
498                break;
499              case MISCREG_SCTLR:
500              case MISCREG_TTBR0:
501              case MISCREG_TTBR1:
502              case MISCREG_TTBCR:
503              case MISCREG_DACR:
504              case MISCREG_DFSR:
505              case MISCREG_IFSR:
506              case MISCREG_DFAR:
507              case MISCREG_IFAR:
508              case MISCREG_ADFSR:
509              case MISCREG_AIFSR:
510              case MISCREG_PRRR:
511              case MISCREG_NMRR:
512              case MISCREG_MAIR0:
513              case MISCREG_MAIR1:
514              case MISCREG_CONTEXTIDR:
515                trapToHype = hcr.tvm & !isRead;
516                break;
517              case MISCREG_PMCR:
518                trapToHype = hdcr.tpmcr;
519                break;
520              // No default action needed
521              default:
522                break;
523            }
524        }
525    }
526    return trapToHype;
527}
528
529
530bool
531mcrMrc14TrapToHyp(const MiscRegIndex miscReg, HCR hcr, CPSR cpsr, SCR scr,
532                  HDCR hdcr, HSTR hstr, HCPTR hcptr, uint32_t iss)
533{
534    bool        isRead;
535    uint32_t    crm;
536    IntRegIndex rt;
537    uint32_t    crn;
538    uint32_t    opc1;
539    uint32_t    opc2;
540    bool        trapToHype = false;
541
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);
548        if (!trapToHype) {
549            switch (unflattenMiscReg(miscReg)) {
550              case MISCREG_DBGOSLSR:
551              case MISCREG_DBGOSLAR:
552              case MISCREG_DBGOSDLR:
553              case MISCREG_DBGPRCR:
554                trapToHype = hdcr.tdosa;
555                break;
556              case MISCREG_DBGDRAR:
557              case MISCREG_DBGDSAR:
558                trapToHype = hdcr.tdra;
559                break;
560              case MISCREG_JIDR:
561                trapToHype = hcr.tid0;
562                break;
563              case MISCREG_JOSCR:
564              case MISCREG_JMCR:
565                trapToHype = hstr.tjdbx;
566                break;
567              case MISCREG_TEECR:
568              case MISCREG_TEEHBR:
569                trapToHype = hstr.ttee;
570                break;
571              // No default action needed
572              default:
573                break;
574            }
575        }
576    }
577    return trapToHype;
578}
579
580bool
581mcrrMrrc15TrapToHyp(const MiscRegIndex miscReg, CPSR cpsr, SCR scr, HSTR hstr,
582                    HCR hcr, uint32_t iss)
583{
584    uint32_t    crm;
585    IntRegIndex rt;
586    uint32_t    crn;
587    uint32_t    opc1;
588    uint32_t    opc2;
589    bool        isRead;
590    bool        trapToHype = false;
591
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
595        // mcrmrc layout
596        mcrMrcIssExtract(iss, isRead, crm, rt, crn, opc1, opc2);
597        trapToHype = ((uint32_t) hstr) & (1 << crm);
598
599        if (!trapToHype) {
600            switch (unflattenMiscReg(miscReg)) {
601              case MISCREG_SCTLR:
602              case MISCREG_TTBR0:
603              case MISCREG_TTBR1:
604              case MISCREG_TTBCR:
605              case MISCREG_DACR:
606              case MISCREG_DFSR:
607              case MISCREG_IFSR:
608              case MISCREG_DFAR:
609              case MISCREG_IFAR:
610              case MISCREG_ADFSR:
611              case MISCREG_AIFSR:
612              case MISCREG_PRRR:
613              case MISCREG_NMRR:
614              case MISCREG_MAIR0:
615              case MISCREG_MAIR1:
616              case MISCREG_CONTEXTIDR:
617                trapToHype = hcr.tvm & !isRead;
618                break;
619              // No default action needed
620              default:
621                break;
622            }
623        }
624    }
625    return trapToHype;
626}
627
628bool
629decodeMrsMsrBankedReg(uint8_t sysM, bool r, bool &isIntReg, int &regIdx,
630                      CPSR cpsr, SCR scr, NSACR nsacr, bool checkSecurity)
631{
632    OperatingMode mode = MODE_UNDEFINED;
633    bool          ok = true;
634
635    // R mostly indicates if its a int register or a misc reg, we override
636    // below if the few corner cases
637    isIntReg = !r;
638    // Loosely based on ARM ARM issue C section B9.3.10
639    if (r) {
640        switch (sysM)
641        {
642          case 0xE:
643            regIdx = MISCREG_SPSR_FIQ;
644            mode   = MODE_FIQ;
645            break;
646          case 0x10:
647            regIdx = MISCREG_SPSR_IRQ;
648            mode   = MODE_IRQ;
649            break;
650          case 0x12:
651            regIdx = MISCREG_SPSR_SVC;
652            mode   = MODE_SVC;
653            break;
654          case 0x14:
655            regIdx = MISCREG_SPSR_ABT;
656            mode   = MODE_ABORT;
657            break;
658          case 0x16:
659            regIdx = MISCREG_SPSR_UND;
660            mode   = MODE_UNDEFINED;
661            break;
662          case 0x1C:
663            regIdx = MISCREG_SPSR_MON;
664            mode   = MODE_MON;
665            break;
666          case 0x1E:
667            regIdx = MISCREG_SPSR_HYP;
668            mode   = MODE_HYP;
669            break;
670          default:
671            ok = false;
672            break;
673        }
674    } else {
675        int sysM4To3 = bits(sysM, 4, 3);
676
677        if (sysM4To3 == 0) {
678            mode = MODE_USER;
679            regIdx = intRegInMode(mode, bits(sysM, 2, 0) + 8);
680        } else if (sysM4To3 == 1) {
681            mode = MODE_FIQ;
682            regIdx = intRegInMode(mode, bits(sysM, 2, 0) + 8);
683        } else if (sysM4To3 == 3) {
684            if (bits(sysM, 1) == 0) {
685                mode = MODE_MON;
686                regIdx = intRegInMode(mode, 14 - bits(sysM, 0));
687            } else {
688                mode = MODE_HYP;
689                if (bits(sysM, 0) == 1) {
690                    regIdx = intRegInMode(mode, 13); // R13 in HYP
691                } else {
692                    isIntReg = false;
693                    regIdx   = MISCREG_ELR_HYP;
694                }
695            }
696        } else { // Other Banked registers
697            int sysM2 = bits(sysM, 2);
698            int sysM1 = bits(sysM, 1);
699
700            mode  = (OperatingMode) ( ((sysM2 ||  sysM1) << 0) |
701                                      (1                 << 1) |
702                                      ((sysM2 && !sysM1) << 2) |
703                                      ((sysM2 &&  sysM1) << 3) |
704                                      (1                 << 4) );
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;
709        }
710    }
711
712    // Check that the requested register is accessable from the current mode
713    if (ok && checkSecurity && mode != cpsr.mode) {
714        switch (cpsr.mode)
715        {
716          case MODE_USER:
717            ok = false;
718            break;
719          case MODE_FIQ:
720            ok &=  mode != MODE_HYP;
721            ok &= (mode != MODE_MON) || !scr.ns;
722            break;
723          case MODE_HYP:
724            ok &=  mode != MODE_MON;
725            ok &= (mode != MODE_FIQ) || !nsacr.rfr;
726            break;
727          case MODE_IRQ:
728          case MODE_SVC:
729          case MODE_ABORT:
730          case MODE_UNDEFINED:
731          case MODE_SYSTEM:
732            ok &=  mode != MODE_HYP;
733            ok &= (mode != MODE_MON) || !scr.ns;
734            ok &= (mode != MODE_FIQ) || !nsacr.rfr;
735            break;
736          // can access everything, no further checks required
737          case MODE_MON:
738            break;
739          default:
740            panic("unknown Mode 0x%x\n", cpsr.mode);
741            break;
742        }
743    }
744    return (ok);
745}
746
747bool
748SPAlignmentCheckEnabled(ThreadContext* tc)
749{
750    switch (opModeToEL(currOpMode(tc))) {
751      case EL3:
752        return ((SCTLR) tc->readMiscReg(MISCREG_SCTLR_EL3)).sa;
753      case EL2:
754        return ((SCTLR) tc->readMiscReg(MISCREG_SCTLR_EL2)).sa;
755      case EL1:
756        return ((SCTLR) tc->readMiscReg(MISCREG_SCTLR_EL1)).sa;
757      case EL0:
758        return ((SCTLR) tc->readMiscReg(MISCREG_SCTLR_EL1)).sa0;
759      default:
760        panic("Invalid exception level");
761        break;
762    }
763}
764
765int
766decodePhysAddrRange64(uint8_t pa_enc)
767{
768    switch (pa_enc) {
769      case 0x0:
770        return 32;
771      case 0x1:
772        return 36;
773      case 0x2:
774        return 40;
775      case 0x3:
776        return 42;
777      case 0x4:
778        return 44;
779      case 0x5:
780      case 0x6:
781      case 0x7:
782        return 48;
783      default:
784        panic("Invalid phys. address range encoding");
785    }
786}
787
788uint8_t
789encodePhysAddrRange64(int pa_size)
790{
791    switch (pa_size) {
792      case 32:
793        return 0x0;
794      case 36:
795        return 0x1;
796      case 40:
797        return 0x2;
798      case 42:
799        return 0x3;
800      case 44:
801        return 0x4;
802      case 48:
803        return 0x5;
804      default:
805        panic("Invalid phys. address range");
806    }
807}
808
809} // namespace ArmISA
810