utility.hh revision 13585:9c8328ccb03f
1/*
2 * Copyright (c) 2010, 2012-2013, 2016-2018 ARM Limited
3 * All rights reserved
4 *
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.
13 *
14 * Copyright (c) 2003-2005 The Regents of The University of Michigan
15 * Copyright (c) 2007-2008 The Florida State University
16 * All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Authors: Korey Sewell
42 *          Stephen Hines
43 */
44
45#ifndef __ARCH_ARM_UTILITY_HH__
46#define __ARCH_ARM_UTILITY_HH__
47
48#include "arch/arm/isa_traits.hh"
49#include "arch/arm/miscregs.hh"
50#include "arch/arm/types.hh"
51#include "base/logging.hh"
52#include "base/trace.hh"
53#include "base/types.hh"
54#include "cpu/static_inst.hh"
55#include "cpu/thread_context.hh"
56
57class ArmSystem;
58
59namespace ArmISA {
60
61inline PCState
62buildRetPC(const PCState &curPC, const PCState &callPC)
63{
64    PCState retPC = callPC;
65    retPC.uEnd();
66    return retPC;
67}
68
69inline bool
70testPredicate(uint32_t nz, uint32_t c, uint32_t v, ConditionCode code)
71{
72    bool n = (nz & 0x2);
73    bool z = (nz & 0x1);
74
75    switch (code)
76    {
77        case COND_EQ: return  z;
78        case COND_NE: return !z;
79        case COND_CS: return  c;
80        case COND_CC: return !c;
81        case COND_MI: return  n;
82        case COND_PL: return !n;
83        case COND_VS: return  v;
84        case COND_VC: return !v;
85        case COND_HI: return  (c && !z);
86        case COND_LS: return !(c && !z);
87        case COND_GE: return !(n ^ v);
88        case COND_LT: return  (n ^ v);
89        case COND_GT: return !(n ^ v || z);
90        case COND_LE: return  (n ^ v || z);
91        case COND_AL: return true;
92        case COND_UC: return true;
93        default:
94            panic("Unhandled predicate condition: %d\n", code);
95    }
96}
97
98/**
99 * Function to insure ISA semantics about 0 registers.
100 * @param tc The thread context.
101 */
102template <class TC>
103void zeroRegisters(TC *tc);
104
105inline void startupCPU(ThreadContext *tc, int cpuId)
106{
107    tc->activate();
108}
109
110void copyRegs(ThreadContext *src, ThreadContext *dest);
111
112static inline void
113copyMiscRegs(ThreadContext *src, ThreadContext *dest)
114{
115    panic("Copy Misc. Regs Not Implemented Yet\n");
116}
117
118void initCPU(ThreadContext *tc, int cpuId);
119
120static inline bool
121inUserMode(CPSR cpsr)
122{
123    return cpsr.mode == MODE_USER || cpsr.mode == MODE_EL0T;
124}
125
126static inline bool
127inUserMode(ThreadContext *tc)
128{
129    return inUserMode(tc->readMiscRegNoEffect(MISCREG_CPSR));
130}
131
132static inline bool
133inPrivilegedMode(CPSR cpsr)
134{
135    return !inUserMode(cpsr);
136}
137
138static inline bool
139inPrivilegedMode(ThreadContext *tc)
140{
141    return !inUserMode(tc);
142}
143
144bool inAArch64(ThreadContext *tc);
145
146static inline OperatingMode
147currOpMode(ThreadContext *tc)
148{
149    CPSR cpsr = tc->readMiscReg(MISCREG_CPSR);
150    return (OperatingMode) (uint8_t) cpsr.mode;
151}
152
153static inline ExceptionLevel
154currEL(ThreadContext *tc)
155{
156    CPSR cpsr = tc->readMiscReg(MISCREG_CPSR);
157    return (ExceptionLevel) (uint8_t) cpsr.el;
158}
159
160/**
161 * This function checks whether selected EL provided as an argument
162 * is using the AArch32 ISA. This information might be unavailable
163 * at the current EL status: it hence returns a pair of boolean values:
164 * a first boolean, true if information is available (known),
165 * and a second one, true if EL is using AArch32, false for AArch64.
166 *
167 * @param tc The thread context.
168 * @param el The target exception level.
169 * @retval known is FALSE for EL0 if the current Exception level
170 *               is not EL0 and EL1 is using AArch64, since it cannot
171 *               determine the state of EL0; TRUE otherwise.
172 * @retval aarch32 is TRUE if the specified Exception level is using AArch32;
173 *                 FALSE otherwise.
174 */
175std::pair<bool, bool>
176ELUsingAArch32K(ThreadContext *tc, ExceptionLevel el);
177
178bool ELIs32(ThreadContext *tc, ExceptionLevel el);
179
180bool ELIs64(ThreadContext *tc, ExceptionLevel el);
181
182bool isBigEndian64(ThreadContext *tc);
183
184/**
185 * badMode is checking if the execution mode provided as an argument is
186 * valid and implemented for AArch32
187 *
188 * @param tc ThreadContext
189 * @param mode OperatingMode to check
190 * @return false if mode is valid and implemented, true otherwise
191 */
192bool badMode32(ThreadContext *tc, OperatingMode mode);
193
194/**
195 * badMode is checking if the execution mode provided as an argument is
196 * valid and implemented.
197 *
198 * @param tc ThreadContext
199 * @param mode OperatingMode to check
200 * @return false if mode is valid and implemented, true otherwise
201 */
202bool badMode(ThreadContext *tc, OperatingMode mode);
203
204static inline uint8_t
205itState(CPSR psr)
206{
207    ITSTATE it = 0;
208    it.top6 = psr.it2;
209    it.bottom2 = psr.it1;
210
211    return (uint8_t)it;
212}
213
214/**
215 * Removes the tag from tagged addresses if that mode is enabled.
216 * @param addr The address to be purified.
217 * @param tc The thread context.
218 * @param el The controlled exception level.
219 * @return The purified address.
220 */
221Addr purifyTaggedAddr(Addr addr, ThreadContext *tc, ExceptionLevel el,
222                      TTBCR tcr);
223Addr purifyTaggedAddr(Addr addr, ThreadContext *tc, ExceptionLevel el);
224
225static inline bool
226inSecureState(SCR scr, CPSR cpsr)
227{
228    switch ((OperatingMode) (uint8_t) cpsr.mode) {
229      case MODE_MON:
230      case MODE_EL3T:
231      case MODE_EL3H:
232        return true;
233      case MODE_HYP:
234      case MODE_EL2T:
235      case MODE_EL2H:
236        return false;
237      default:
238        return !scr.ns;
239    }
240}
241
242bool inSecureState(ThreadContext *tc);
243
244/**
245 * Return TRUE if an Exception level below EL3 is in Secure state.
246 * Differs from inSecureState in that it ignores the current EL
247 * or Mode in considering security state.
248 */
249inline bool isSecureBelowEL3(ThreadContext *tc);
250
251bool longDescFormatInUse(ThreadContext *tc);
252
253/** This helper function is either returing the value of
254 * MPIDR_EL1 (by calling getMPIDR), or it is issuing a read
255 * to VMPIDR_EL2 (as it happens in virtualized systems) */
256RegVal readMPIDR(ArmSystem *arm_sys, ThreadContext *tc);
257
258/** This helper function is returing the value of MPIDR_EL1 */
259RegVal getMPIDR(ArmSystem *arm_sys, ThreadContext *tc);
260
261static inline uint32_t
262mcrMrcIssBuild(bool isRead, uint32_t crm, IntRegIndex rt, uint32_t crn,
263               uint32_t opc1, uint32_t opc2)
264{
265    return (isRead <<  0) |
266           (crm    <<  1) |
267           (rt     <<  5) |
268           (crn    << 10) |
269           (opc1   << 14) |
270           (opc2   << 17);
271}
272
273static inline void
274mcrMrcIssExtract(uint32_t iss, bool &isRead, uint32_t &crm, IntRegIndex &rt,
275                 uint32_t &crn, uint32_t &opc1, uint32_t &opc2)
276{
277    isRead = (iss >>  0) & 0x1;
278    crm    = (iss >>  1) & 0xF;
279    rt     = (IntRegIndex) ((iss >>  5) & 0xF);
280    crn    = (iss >> 10) & 0xF;
281    opc1   = (iss >> 14) & 0x7;
282    opc2   = (iss >> 17) & 0x7;
283}
284
285static inline uint32_t
286mcrrMrrcIssBuild(bool isRead, uint32_t crm, IntRegIndex rt, IntRegIndex rt2,
287                 uint32_t opc1)
288{
289    return (isRead <<  0) |
290           (crm    <<  1) |
291           (rt     <<  5) |
292           (rt2    << 10) |
293           (opc1   << 16);
294}
295
296static inline uint32_t
297msrMrs64IssBuild(bool isRead, uint32_t op0, uint32_t op1, uint32_t crn,
298                 uint32_t crm, uint32_t op2, IntRegIndex rt)
299{
300    return isRead |
301        (crm << 1) |
302        (rt << 5) |
303        (crn << 10) |
304        (op1 << 14) |
305        (op2 << 17) |
306        (op0 << 20);
307}
308
309bool
310mcrMrc15TrapToHyp(const MiscRegIndex miscReg, HCR hcr, CPSR cpsr, SCR scr,
311                  HDCR hdcr, HSTR hstr, HCPTR hcptr, uint32_t iss);
312bool
313mcrMrc14TrapToHyp(const MiscRegIndex miscReg, HCR hcr, CPSR cpsr, SCR scr,
314                  HDCR hdcr, HSTR hstr, HCPTR hcptr, uint32_t iss);
315bool
316mcrrMrrc15TrapToHyp(const MiscRegIndex miscReg, CPSR cpsr, SCR scr, HSTR hstr,
317                    HCR hcr, uint32_t iss);
318
319bool SPAlignmentCheckEnabled(ThreadContext* tc);
320
321uint64_t getArgument(ThreadContext *tc, int &number, uint16_t size, bool fp);
322
323void skipFunction(ThreadContext *tc);
324
325inline void
326advancePC(PCState &pc, const StaticInstPtr &inst)
327{
328    inst->advancePC(pc);
329}
330
331Addr truncPage(Addr addr);
332Addr roundPage(Addr addr);
333
334inline uint64_t
335getExecutingAsid(ThreadContext *tc)
336{
337    return tc->readMiscReg(MISCREG_CONTEXTIDR);
338}
339
340// Decodes the register index to access based on the fields used in a MSR
341// or MRS instruction
342bool
343decodeMrsMsrBankedReg(uint8_t sysM, bool r, bool &isIntReg, int &regIdx,
344                      CPSR cpsr, SCR scr, NSACR nsacr,
345                      bool checkSecurity = true);
346
347// This wrapper function is used to turn the register index into a source
348// parameter for the instruction. See Operands.isa
349static inline int
350decodeMrsMsrBankedIntRegIndex(uint8_t sysM, bool r)
351{
352    int  regIdx;
353    bool isIntReg;
354    bool validReg;
355
356    validReg = decodeMrsMsrBankedReg(sysM, r, isIntReg, regIdx, 0, 0, 0, false);
357    return (validReg && isIntReg) ? regIdx : INTREG_DUMMY;
358}
359
360/**
361 * Returns the n. of PA bits corresponding to the specified encoding.
362 */
363int decodePhysAddrRange64(uint8_t pa_enc);
364
365/**
366 * Returns the encoding corresponding to the specified n. of PA bits.
367 */
368uint8_t encodePhysAddrRange64(int pa_size);
369
370inline ByteOrder byteOrder(ThreadContext *tc)
371{
372    return isBigEndian64(tc) ? BigEndianByteOrder : LittleEndianByteOrder;
373};
374
375}
376
377#endif
378