2 * Copyright (c) 2019 Metempsy Technology LSC
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.
38 #ifndef __ARCH_ARM_SELF_DEBUG_HH__
39 #define __ARCH_ARM_SELF_DEBUG_HH__
42 #include "arch/arm/faults.hh"
43 #include "arch/arm/miscregs.hh"
44 #include "arch/arm/system.hh"
45 #include "arch/arm/types.hh"
46 #include "arch/arm/utility.hh"
47 #include "arch/generic/tlb.hh"
48 #include "cpu/thread_context.hh"
60 MiscRegIndex ctrlRegIndex;
61 MiscRegIndex valRegIndex;
71 friend class SelfDebug;
73 BrkPoint(MiscRegIndex ctrl_index, MiscRegIndex val_index,
74 SelfDebug* _conf, bool ctx_aw, bool lva,
75 bool vmid16, bool aarch32):
76 ctrlRegIndex(ctrl_index), valRegIndex(val_index),
77 conf(_conf), isCntxtAware(ctx_aw),
78 VMID16enabled(vmid16), activePc(0x0), enable(false)
80 maxAddrSize = lva ? 52: 48 ;
81 maxAddrSize = aarch32 ? 31 : maxAddrSize;
85 bool testLinkedBk(ThreadContext *tc, Addr vaddr, ExceptionLevel el);
86 bool test(ThreadContext *tc, Addr pc, ExceptionLevel el, DBGBCR ctr,
91 getAddrfromReg(ThreadContext *tc) const
93 return bits(tc->readMiscReg(valRegIndex), maxAddrSize, 2);
97 getContextfromReg(ThreadContext *tc, bool ctxid1) const
100 return bits(tc->readMiscReg(valRegIndex), 31, 0);
102 return bits(tc->readMiscReg(valRegIndex), 63, 32);
106 inline uint32_t getVMIDfromReg(ThreadContext *tc);
109 bool testAddrMatch(ThreadContext *tc, Addr pc, uint8_t bas);
110 bool testAddrMissMatch(ThreadContext *tc, Addr pc, uint8_t bas);
111 bool testContextMatch(ThreadContext *tc, bool ctx1, bool low_ctx);
112 bool testContextMatch(ThreadContext *tc, bool ctx1);
113 bool testVMIDMatch(ThreadContext *tc);
116 getControlReg(ThreadContext *tc)
118 return tc->readMiscReg(ctrlRegIndex);
121 bool isEnabled(ThreadContext* tc, ExceptionLevel el,
122 uint8_t hmc, uint8_t ssc, uint8_t pmc);
127 if (vaddr == activePc) {
137 updateControl(DBGBCR val)
139 enable = val.e == 0x1;
146 MiscRegIndex ctrlRegIndex;
147 MiscRegIndex valRegIndex;
153 friend class SelfDebug;
155 WatchPoint(MiscRegIndex ctrl_index, MiscRegIndex val_index,
156 SelfDebug* _conf, bool lva, bool aarch32) :
157 ctrlRegIndex(ctrl_index),
158 valRegIndex(val_index), conf(_conf), enable(false)
160 maxAddrSize = lva ? 52: 48 ;
161 maxAddrSize = aarch32 ? 31 : maxAddrSize;
164 bool compareAddress(ThreadContext *tc, Addr in_addr,
165 uint8_t bas, uint8_t mask, unsigned size);
168 getAddrfromReg(ThreadContext *tc)
170 return bits(tc->readMiscReg(valRegIndex), maxAddrSize, 0);
175 isDoubleAligned(Addr addr)
181 updateControl(DBGWCR val)
183 enable = val.e == 0x1;
186 bool isEnabled(ThreadContext* tc, ExceptionLevel el, bool hmc,
187 uint8_t ssc, uint8_t pac);
188 bool test(ThreadContext *tc, Addr addr, ExceptionLevel el, bool& wrt,
189 bool atomic, unsigned size);
195 static const uint8_t INACTIVE_STATE = 0;
196 static const uint8_t ACTIVE_PENDING_STATE = 1;
197 static const uint8_t ACTIVE_NOT_PENDING_STATE = 2;
207 SoftwareStep(SelfDebug *s)
208 : bSS(false), stateSS(INACTIVE_STATE),
209 conf(s), steppedLdx(false)
212 bool debugExceptionReturnSS(ThreadContext *tc, CPSR spsr,
213 ExceptionLevel dest, bool aarch32);
214 bool advanceSS(ThreadContext *tc);
223 setEnableSS(bool val)
231 prevSteppedLdx = steppedLdx;
238 prevSteppedLdx = steppedLdx;
245 return prevSteppedLdx;
254 std::vector<Fault *> vectorTypes();
257 VectorCatch(bool _vcmatch, SelfDebug* s) : vcmatch(_vcmatch), conf(s)
260 bool addressMatching(ThreadContext *tc, Addr addr, ExceptionLevel el);
261 bool exceptionTrapping(ThreadContext *tc, ExceptionLevel el,
264 bool isVCMatch() const { return vcmatch; }
268 getVectorBase(ThreadContext *tc, bool monitor)
271 return tc->readMiscReg(MISCREG_MVBAR) & ~0x1F;
273 SCTLR sctlr = tc->readMiscReg(MISCREG_SCTLR_EL1);
275 return (Addr) 0xFFFF0000;
277 Addr vbar = tc->readMiscReg(MISCREG_VBAR) & ~0x1F;
287 std::vector<BrkPoint> arBrkPoints;
288 std::vector<WatchPoint> arWatchPoints;
289 SoftwareStep * softStep;
290 VectorCatch * vcExcpt;
293 bool enableTdeTge; // MDCR_EL2.TDE || HCR_EL2.TGE
295 // THIS is MDSCR_EL1.MDE in aarch64 and DBGDSCRext.MDBGen in aarch32
298 bool bSDD; // MDCR_EL3.SDD
299 bool bKDE; // MDSCR_EL1.KDE
300 bool oslk; // OS lock flag
302 bool aarch32; // updates with stage1 aarch64/32
307 : initialized(false), enableTdeTge(false),
308 enableFlag(false), bSDD(false), bKDE(false), oslk(false)
310 softStep = new SoftwareStep(this);
319 Fault testDebug(ThreadContext *tc, const RequestPtr &req,
323 Fault testBreakPoints(ThreadContext *tc, Addr vaddr);
324 Fault testWatchPoints(ThreadContext *tc, Addr vaddr, bool write,
325 bool atomic, unsigned size, bool cm);
327 Fault triggerException(ThreadContext * tc, Addr vaddr);
328 Fault triggerWatchpointException(ThreadContext *tc, Addr vaddr,
329 bool write, bool cm);
331 Fault testVectorCatch(ThreadContext *tc, Addr addr, ArmFault* flt);
334 getBrkPoint(uint8_t index)
336 return &arBrkPoints[index];
340 securityStateMatch(ThreadContext *tc, uint8_t ssc, bool hmc)
343 case 0x0: return true;
344 case 0x1: return !isSecure(tc);
345 case 0x2: return isSecure(tc);
348 bool b = hmc? true: isSecure(tc);
351 default: panic("Unreachable value");
356 bool isDebugEnabledForEL64(ThreadContext *tc, ExceptionLevel el,
357 bool secure, bool mask);
358 bool isDebugEnabledForEL32(ThreadContext *tc, ExceptionLevel el,
359 bool secure, bool mask);
364 for (auto &p: arBrkPoints){
370 isDebugEnabled(ThreadContext *tc)
372 CPSR cpsr = tc->readMiscReg(MISCREG_CPSR);
373 ExceptionLevel el = (ExceptionLevel) currEL(tc);
375 return isDebugEnabledForEL32(tc, el, isSecure(tc),
378 return isDebugEnabledForEL64(tc, el, isSecure(tc),
386 bSDD = bits(val, 16);
390 setMDSCRvals(RegVal val)
392 enableFlag = bits(val, 15);
393 bKDE = bits(val, 13);
394 softStep->setEnableSS((bool)bits(val, 0));
398 setMDBGen(RegVal val)
400 enableFlag = bits(val, 15);
404 setenableTDETGE(HCR hcr, HDCR mdcr)
406 enableTdeTge = (mdcr.tde == 0x1 || hcr.tge == 0x1);
410 updateOSLock(RegVal val)
412 oslk = bool(bits(val, 0));
416 updateDBGBCR(int index, DBGBCR val)
418 arBrkPoints[index].updateControl(val);
422 updateDBGWCR(int index, DBGWCR val)
424 arWatchPoints[index].updateControl(val);
428 setDebugMask(bool mask)
430 softStep->setCPSRD(mask);
440 setAArch32(ThreadContext *tc)
442 ExceptionLevel from_el = (ExceptionLevel) currEL(tc);
444 aarch32 = ELIs32(tc, EL0) && ELIs32(tc, EL1);
446 aarch32 = ELIs32(tc, from_el);
457 getVectorCatch(ThreadContext *tc)
465 targetAArch32(ThreadContext *tc)
467 ExceptionLevel ELd = debugTargetFrom(tc, isSecure(tc));
468 return ELIs32(tc, ELd) && aarch32;
471 void init(ThreadContext *tc);