Update contrib.
1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // This file contains stepping code refactored from rm_debug_kerneldriver.cpp/rm_debug_kerneldriver.h
18 #include <e32def_private.h>
20 #include <e32cmn_private.h>
21 #include <kernel/kernel.h>
22 #include <kernel/kern_priv.h>
25 #include <rm_debug_api.h>
27 #include "d_rmd_stepping.h"
28 #include "d_rmd_breakpoints.h"
29 #include "rm_debug_kerneldriver.h" // needed to access DRM_DebugChannel
30 #include "rm_debug_driver.h"
31 #include "debug_logging.h"
33 using namespace Debug;
36 // DRMDStepping::DRMDStepping
38 DRMDStepping::DRMDStepping(DRM_DebugChannel* aChannel)
46 // DRMDStepping::~DRM_DebugChannel
48 DRMDStepping::~DRMDStepping()
54 // DRMDStepping::IsExecuted
56 TBool DRMDStepping::IsExecuted(TUint8 aCondition ,TUint32 aStatusRegister)
58 LOG_MSG("DRMDStepping::IsExecuted()");
60 TBool N = ((aStatusRegister >> 28) & 0x0000000F) & 0x00000008;
61 TBool Z = ((aStatusRegister >> 28) & 0x0000000F) & 0x00000004;
62 TBool C = ((aStatusRegister >> 28) & 0x0000000F) & 0x00000002;
63 TBool V = ((aStatusRegister >> 28) & 0x0000000F) & 0x00000001;
92 return ((N == V) && !Z);
94 return (Z || (N != V));
104 // DRMDStepping::IsPreviousInstructionMovePCToLR
106 TBool DRMDStepping::IsPreviousInstructionMovePCToLR(DThread *aThread)
108 LOG_MSG("DRMDStepping::IsPreviousInstructionMovePCToLR()");
112 // there are several types of instructions that modify the PC that aren't
113 // designated as linked or non linked branches. the way gcc generates the
114 // code can tell us whether or not these instructions are to be treated as
115 // linked branches. the main cases are bx and any type of mov or load or
116 // arithmatic operation that changes the PC. if these are really just
117 // function calls that will return, gcc will generate a mov lr, pc
118 // instruction as the previous instruction. note that this is just for arm
121 // get the address of the previous instruction
123 err = iChannel->ReadKernelRegisterValue(aThread, PC_REGISTER, address);
126 LOG_MSG2("Non-zero error code discarded: %d", err);
130 TBuf8<4> previousInstruction;
131 err = iChannel->DoReadMemory(aThread, address, 4, previousInstruction);
134 LOG_MSG2("Error %d reading memory at address %x", address);
138 const TUint32 movePCToLRIgnoringCondition = 0x01A0E00F;
140 TUint32 inst = *(TUint32 *)previousInstruction.Ptr();
142 if ((inst & 0x0FFFFFFF) == movePCToLRIgnoringCondition)
151 // DRMDStepping::DecodeDataProcessingInstruction
153 void DRMDStepping::DecodeDataProcessingInstruction(TUint8 aOpcode, TUint32 aOp1, TUint32 aOp2, TUint32 aStatusRegister, TUint32 &aBreakAddress)
155 LOG_MSG("DRMDStepping::DecodeDataProcessingInstruction()");
162 aBreakAddress = aOp1 & aOp2;
168 aBreakAddress = aOp1 ^ aOp2;
174 aBreakAddress = aOp1 - aOp2;
180 aBreakAddress = aOp2 - aOp1;
186 aBreakAddress = aOp1 + aOp2;
192 aBreakAddress = aOp1 + aOp2 + (aStatusRegister & arm_carry_bit()) ? 1 : 0;
198 aBreakAddress = aOp1 - aOp2 - (aStatusRegister & arm_carry_bit()) ? 0 : 1;
204 aBreakAddress = aOp2 - aOp1 - (aStatusRegister & arm_carry_bit()) ? 0 : 1;
210 aBreakAddress = aOp1 | aOp2;
216 aBreakAddress = aOp2;
222 aBreakAddress = aOp1 & ~aOp2;
228 aBreakAddress = ~aOp2;
235 // DRMDStepping::CurrentInstruction
237 // Returns the current instruction bitpattern (either 32-bits or 16-bits) if possible
238 TInt DRMDStepping::CurrentInstruction(DThread* aThread, TUint32& aInstruction)
240 LOG_MSG("DRMDStepping::CurrentInstruction");
242 // What is the current PC?
244 ReturnIfError(CurrentPC(aThread,pc));
246 // Read it one byte at a time to ensure alignment doesn't matter
248 for(TInt i=3;i>=0;i--)
251 TBuf8<1> instruction;
252 TInt err = iChannel->DoReadMemory(aThread, (pc+i), 1, instruction);
255 LOG_MSG2("DRMDStepping::CurrentInstruction : Failed to read memory at current PC: return 0x%08x",pc);
259 inst = (inst << 8) | (*(TUint8 *)instruction.Ptr());
264 LOG_MSG2("DRMDStepping::CurrentInstruction 0x%08x", aInstruction);
270 // DRMDStepping::CurrentArchMode
272 // Determines architecture mode from the supplied cpsr
273 TInt DRMDStepping::CurrentArchMode(const TUint32 aCpsr, Debug::TArchitectureMode& aMode)
275 // Thumb2 work will depend on having a suitable cpu architecture to compile for...
277 // State table as per ARM ARM DDI0406A, section A.2.5.1
280 if (aCpsr & ECpuThumb)
283 aMode = Debug::EThumb2EEMode;
287 // Jazelle mode - not supported
288 return KErrNotSupported;
294 if (aCpsr & ECpuThumb)
297 aMode = Debug::EThumbMode;
302 aMode = Debug::EArmMode;
310 // DRMDStepping::PCAfterInstructionExecutes
312 // Note, this function pretty much ignores all the arguments except for aThread.
313 // The arguments continue to exist so that the function has the same prototype as
314 // the original from Nokia. In the long term this function will be re-factored
315 // to remove obsolete parameters.
317 TUint32 DRMDStepping::PCAfterInstructionExecutes(DThread *aThread, TUint32 aCurrentPC, TUint32 aStatusRegister, TInt aInstSize, /*TBool aStepInto,*/ TUint32 &aNewRangeEnd, TBool &aChangingModes)
319 LOG_MSG("DRMDStepping::PCAfterInstructionExecutes()");
321 // by default we will set the breakpoint at the next instruction
322 TUint32 breakAddress = aCurrentPC + aInstSize;
326 // determine the architecture
328 asm("mrc p15, 0, cpuid, c0, c0, 0 ");
329 LOG_MSG2("DRMDStepping::PCAfterInstructionExecutes() - cpuid = 0x%08x\n",cpuid);
334 // determine the architecture mode for the current instruction
335 TArchitectureMode mode = EArmMode; // Default assumption is ARM
337 // Now we must examine the CPSR to read the T and J bits. See ARM ARM DDI0406A, section B1.3.3
340 ReturnIfError(CurrentCPSR(aThread,cpsr));
341 LOG_MSG2("DRMDStepping::PCAfterInstructionExecutes() - cpsr = 0x%08x\n",cpsr);
343 // Determine the mode
344 ReturnIfError(CurrentArchMode(cpsr,mode));
346 // Decode instruction based on current CPU mode
349 case Debug::EArmMode:
351 // Obtain the current instruction bit pattern
353 ReturnIfError(CurrentInstruction(aThread,inst));
355 LOG_MSG2("Current instruction: %x", inst);
357 // check the conditions to see if this will actually get executed
358 if (IsExecuted(((inst>>28) & 0x0000000F), aStatusRegister))
360 switch(arm_opcode(inst)) // bits 27-25
364 switch((inst & 0x00000010) >> 4) // bit 4
368 switch((inst & 0x01800000) >> 23) // bits 24-23
372 // move to/from status register. pc updates not allowed
373 // or TST, TEQ, CMP, CMN which don't modify the PC
378 // Data processing immediate shift
379 if (arm_rd(inst) == PC_REGISTER)
381 TUint32 rn = aCurrentPC + 8;
382 if (arm_rn(inst) != PC_REGISTER) // bits 19-16
384 err = iChannel->ReadKernelRegisterValue(aThread, arm_rn(inst), rn);
387 LOG_MSG2("Non-zero error code discarded: %d", err);
391 TUint32 shifter = ShiftedRegValue(aThread, inst, aCurrentPC, aStatusRegister);
393 DecodeDataProcessingInstruction(((inst & 0x01E00000) >> 21), rn, shifter, aStatusRegister, breakAddress);
402 switch((inst & 0x00000080) >> 7) // bit 7
406 switch((inst & 0x01900000) >> 20) // bits 24-23 and bit 20
411 switch((inst & 0x000000F0) >> 4) // bits 7-4
415 if (((inst & 0x00400000) >> 22) == 0) // bit 22
418 // this is a strange case. normally this is used in the epilogue to branch the the link
419 // register. sometimes it is used to call a function, and the LR is stored in the previous
420 // instruction. since what we want to do is different for the two cases when stepping over,
421 // we need to read the previous instruction to see what we should do
422 err = iChannel->ReadKernelRegisterValue(aThread, (inst & 0x0000000F), breakAddress);
425 LOG_MSG2("Non-zero error code discarded: %d", err);
428 if ((breakAddress & 0x00000001) == 1)
430 aChangingModes = ETrue;
433 breakAddress &= 0xFFFFFFFE;
441 err = iChannel->ReadKernelRegisterValue(aThread, (inst & 0x0000000F), breakAddress);
444 LOG_MSG2("Non-zero error code discarded: %d", err);
447 if ((breakAddress & 0x00000001) == 1)
449 aChangingModes = ETrue;
452 breakAddress &= 0xFFFFFFFE;
458 // either doesn't modify the PC or it is illegal to
466 // Data processing register shift
467 if (((inst & 0x01800000) >> 23) == 2) // bits 24-23
469 // TST, TEQ, CMP, CMN don't modify the PC
471 else if (arm_rd(inst) == PC_REGISTER)
473 // destination register is the PC
474 TUint32 rn = aCurrentPC + 8;
475 if (arm_rn(inst) != PC_REGISTER) // bits 19-16
477 err = iChannel->ReadKernelRegisterValue(aThread, arm_rn(inst), rn);
480 LOG_MSG2("Non-zero error code discarded: %d", err);
484 TUint32 shifter = ShiftedRegValue(aThread, inst, aCurrentPC, aStatusRegister);
486 DecodeDataProcessingInstruction(((inst & 0x01E00000) >> 21), rn, shifter, aStatusRegister, breakAddress);
495 // from figure 3-2, updates to the PC illegal
506 if (((inst & 0x01800000) >> 23) == 2) // bits 24-23
508 // cannot modify the PC
511 else if (arm_rd(inst) == PC_REGISTER)
513 // destination register is the PC
515 err = iChannel->ReadKernelRegisterValue(aThread, arm_rn(inst), rn); // bits 19-16
518 LOG_MSG2("Non-zero error code discarded: %d", err);
520 TUint32 shifter = ((arm_data_imm(inst) >> arm_data_rot(inst)) | (arm_data_imm(inst) << (32 - arm_data_rot(inst)))) & 0xffffffff;
522 DecodeDataProcessingInstruction(((inst & 0x01E00000) >> 21), rn, shifter, aStatusRegister, breakAddress);
528 // load/store immediate offset
529 if (arm_load(inst)) // bit 20
531 // loading a register from memory
532 if (arm_rd(inst) == PC_REGISTER)
534 // loading the PC register
536 err = iChannel->ReadKernelRegisterValue(aThread, arm_rn(inst), base);
539 LOG_MSG2("Non-zero error code discarded: %d", err);
542 /* Note: At runtime the PC would be 8 further on
544 if (arm_rn(inst) == PC_REGISTER)
546 base = aCurrentPC + 8;
551 if (arm_single_pre(inst))
554 offset = arm_single_imm(inst);
556 if (arm_single_u(inst))
566 TBuf8<4> destination;
567 err = iChannel->DoReadMemory(aThread, base, 4, destination);
571 breakAddress = *(TUint32 *)destination.Ptr();
573 if ((breakAddress & 0x00000001) == 1)
575 aChangingModes = ETrue;
577 breakAddress &= 0xFFFFFFFE;
581 LOG_MSG("Error reading memory in decoding step instruction");
589 if (((inst & 0xF0000000) != 0xF0000000) && ((inst & 0x00000010) == 0))
591 // load/store register offset
592 if (arm_load(inst)) // bit 20
594 // loading a register from memory
595 if (arm_rd(inst) == PC_REGISTER)
597 // loading the PC register
599 if(arm_rn(inst) == PC_REGISTER)
601 base = aCurrentPC + 8;
605 err = iChannel->ReadKernelRegisterValue(aThread, arm_rn(inst), base);
608 LOG_MSG2("Non-zero error code discarded: %d", err);
614 if (arm_single_pre(inst))
616 offset = ShiftedRegValue(aThread, inst, aCurrentPC, aStatusRegister);
618 if (arm_single_u(inst))
628 TBuf8<4> destination;
629 err = iChannel->DoReadMemory(aThread, base, 4, destination);
633 breakAddress = *(TUint32 *)destination.Ptr();
635 if ((breakAddress & 0x00000001) == 1)
637 aChangingModes = ETrue;
639 breakAddress &= 0xFFFFFFFE;
643 LOG_MSG("Error reading memory in decoding step instruction");
652 if ((inst & 0xF0000000) != 0xF0000000)
654 // load/store multiple
655 if (arm_load(inst)) // bit 20
657 // loading a register from memory
658 if (((inst & 0x00008000) >> 15))
660 // loading the PC register
662 if (arm_block_u(inst))
664 TUint32 reglist = arm_block_reglist(inst);
665 offset = iChannel->Bitcount(reglist) * 4 - 4;
666 if (arm_block_pre(inst))
669 else if (arm_block_pre(inst))
675 err = iChannel->ReadKernelRegisterValue(aThread, arm_rn(inst), temp);
678 LOG_MSG2("Non-zero error code discarded: %d", err);
683 TBuf8<4> destination;
684 err = iChannel->DoReadMemory(aThread, temp, 4, destination);
688 breakAddress = *(TUint32 *)destination.Ptr();
689 if ((breakAddress & 0x00000001) == 1)
691 aChangingModes = ETrue;
693 breakAddress &= 0xFFFFFFFE;
697 LOG_MSG("Error reading memory in decoding step instruction");
706 if ((inst & 0xF0000000) == 0xF0000000)
710 breakAddress = (TUint32)arm_instr_b_dest(inst, aCurrentPC);
712 // Unconditionally change into Thumb mode
713 aChangingModes = ETrue;
715 breakAddress &= 0xFFFFFFFE;
720 if ((inst & 0x01000000)) // bit 24
724 breakAddress = (TUint32)arm_instr_b_dest(inst, aCurrentPC);
730 breakAddress = (TUint32)arm_instr_b_dest(inst, aCurrentPC);
740 case Debug::EThumbMode:
744 // Notes: This now includes the extra code
745 // required to decode V6T2 instructions
747 LOG_MSG("DRMDStepping::PCAfterInstructionExecutes: Thumb Instruction");
751 // Obtain the current instruction bit pattern
753 ReturnIfError(CurrentInstruction(aThread,inst32));
755 inst = static_cast<TUint16>(inst32 & 0xFFFF);
757 LOG_MSG2("Current Thumb instruction: 0x%x", inst);
761 // Note: v6T2 decoding is only enabled for DEBUG builds or if using an
762 // an ARM_V6T2 supporting build system. At the time of writing, no
763 // ARM_V6T2 supporting build system exists, so the stepping code cannot
764 // be said to be known to work. Hence it is not run for release builds
766 TBool use_v6t2_decodings = EFalse;
768 #if defined(DEBUG) || defined(__ARMV6T2__)
769 use_v6t2_decodings = ETrue;
772 // coverity[dead_error_line]
773 if (use_v6t2_decodings)
777 // A6.2.5 Misc 16-bit instructions
778 // DONE Compare and branch on zero (page A8-66)
781 // ARM ARM DDI0406A - section A8.6.27 CBNZ, CBZ
783 // Compare and branch on Nonzero and Compare and Branch on Zero.
784 if ((inst & 0xF500) == 0xB100)
786 LOG_MSG("ARM ARM DDI0406A - section A8.6.27 CBNZ, CBZ");
788 // Decoding as per ARM ARM description
789 TUint32 op = (inst & 0x0800) >> 11;
790 TUint32 i = (inst & 0x0200) >> 9;
791 TUint32 imm5 = (inst & 0x00F8) >> 3;
792 TUint32 Rn = inst & 0x0007;
794 TUint32 imm32 = (i << 6) | (imm5 << 1);
796 // Obtain value for register Rn
798 ReturnIfError(RegisterValue(aThread,Rn,RnVal));
806 breakAddress = aCurrentPC + imm32;
815 breakAddress = aCurrentPC + imm32;
820 // ARM ARM DDI0406A - section A8.6.50 IT
822 // If Then instruction
823 if ((inst & 0xFF00) == 0xBF00)
825 LOG_MSG("ARM ARM DDI0406A - section A8.6.50 IT");
827 // Decoding as per ARM ARM description
828 TUint32 firstcond = inst & 0x00F0 >> 4;
829 TUint32 mask = inst & 0x000F;
831 if (firstcond == 0xF)
834 LOG_MSG("ARM ARM DDI0406A - section A8.6.50 IT - Unpredictable");
838 if ((firstcond == 0xE) && (BitCount(mask) != 1))
841 LOG_MSG("ARM ARM DDI0406A - section A8.6.50 IT - Unpredictable");
845 // should check if 'in-it-block'
846 LOG_MSG("Cannot step IT instructions.");
848 // all the conds are as per Table A8-1 (i.e. the usual 16 cases)
849 // no idea how to decode the it block 'after-the-fact'
850 // so probably need to treat instructions in the it block
851 // as 'may' be executed. So breakpoints at both possible locations
852 // depending on whether the instruction is executed or not.
854 // also, how do we know if we have hit a breakpoint whilst 'in' an it block?
855 // can we check the status registers to find out?
857 // see arm arm page 390.
859 // seems to depend on the itstate field. this also says what the condition code
860 // actually is, and how many instructions are left in the itblock.
861 // perhaps we can just totally ignore this state, and always do the two-instruction
862 // breakpoint thing? Not if there is any possibility that the address target
863 // would be invalid for the non-taken branch address...
871 // Data processing instructions a6-28
874 // ARM ARM DDI0406A - section A8.6.26
875 if (inst32 & 0xFFF0FFFF == 0xE3C08F00)
877 LOG_MSG("ARM ARM DDI0406A - section A8.6.26 - BXJ is not supported");
879 // Decoding as per ARM ARM description
880 // TUint32 Rm = inst32 & 0x000F0000; // not needed yet
883 // return from exception... SUBS PC,LR. page b6-25
885 // ARM ARM DDi046A - section B6.1.13 - SUBS PC,LR
888 if (inst32 & 0xFFFFFF00 == 0xF3DE8F00)
890 LOG_MSG("ARM ARM DDI0406A - section B6.1.13 - SUBS PC,LR Encoding T1");
892 // Decoding as per ARM ARM description
893 TUint32 imm8 = inst32 & 0x000000FF;
894 TUint32 imm32 = imm8;
896 // TUint32 register_form = EFalse; // not needed for this decoding
897 // TUint32 opcode = 0x2; // SUB // not needed for this decoding
902 ReturnIfError(RegisterValue(aThread,n,lrVal));
904 TUint32 operand2 = imm32; // always for Encoding T1
906 TUint32 result = lrVal - operand2;
908 breakAddress = result;
911 // ARM ARM DDI0406A - section A8.6.16 - B
913 // Branch Encoding T3
914 if (inst32 & 0xF800D000 == 0xF0008000)
916 LOG_MSG("ARM ARM DDI0406A - section A8.6.16 - B Encoding T3");
918 // Decoding as per ARM ARM description
919 TUint32 S = inst32 & 0x04000000 >> 26;
920 // TUint32 cond = inst32 & 0x03C00000 >> 22; // not needed for this decoding
921 TUint32 imm6 = inst32 & 0x003F0000 >> 16;
922 TUint32 J1 = inst32 & 0x00002000 >> 13;
923 TUint32 J2 = inst32 & 0x00000800 >> 11;
924 TUint32 imm11 = inst32 & 0x000007FF;
926 TUint32 imm32 = S ? 0xFFFFFFFF : 0 ;
927 imm32 = (imm32 << 1) | J2;
928 imm32 = (imm32 << 1) | J1;
929 imm32 = (imm32 << 6) | imm6;
930 imm32 = (imm32 << 11) | imm11;
931 imm32 = (imm32 << 1) | 0;
933 breakAddress = aCurrentPC + imm32;
936 // ARM ARM DDI0406A - section A8.6.16 - B
938 // Branch Encoding T4
939 if (inst32 & 0xF800D000 == 0xF0009000)
941 LOG_MSG("ARM ARM DDI0406A - section A8.6.16 - B");
943 // Decoding as per ARM ARM description
944 TUint32 S = inst32 & 0x04000000 >> 26;
945 TUint32 imm10 = inst32 & 0x03FF0000 >> 16;
946 TUint32 J1 = inst32 & 0x00002000 >> 12;
947 TUint32 J2 = inst32 & 0x00000800 >> 11;
948 TUint32 imm11 = inst32 & 0x000003FF;
950 TUint32 I1 = !(J1 ^ S);
951 TUint32 I2 = !(J2 ^ S);
953 TUint32 imm32 = S ? 0xFFFFFFFF : 0;
954 imm32 = (imm32 << 1) | S;
955 imm32 = (imm32 << 1) | I1;
956 imm32 = (imm32 << 1) | I2;
957 imm32 = (imm32 << 10) | imm10;
958 imm32 = (imm32 << 11) | imm11;
959 imm32 = (imm32 << 1) | 0;
961 breakAddress = aCurrentPC + imm32;
965 // ARM ARM DDI0406A - section A8.6.225 - TBB, TBH
967 // Table Branch Byte, Table Branch Halfword
968 if (inst32 & 0xFFF0FFE0 == 0xE8D0F000)
970 LOG_MSG("ARM ARM DDI0406A - section A8.6.225 TBB,TBH Encoding T1");
972 // Decoding as per ARM ARM description
973 TUint32 Rn = inst32 & 0x000F0000 >> 16;
974 TUint32 H = inst32 & 0x00000010 >> 4;
975 TUint32 Rm = inst32 & 0x0000000F;
978 if (Rm == 13 || Rm == 15)
980 LOG_MSG("ARM ARM DDI0406A - section A8.6.225 TBB,TBH Encoding T1 - Unpredictable");
986 ReturnIfError(RegisterValue(aThread,Rn,address));
989 ReturnIfError(RegisterValue(aThread,Rm,offset));
994 address += offset << 1;
1001 ReturnIfError(ReadMem32(aThread,address,halfwords));
1003 breakAddress = aCurrentPC + 2*halfwords;
1007 // ARM ARM DDI0406A - section A8.6.55 - LDMDB, LDMEA
1009 // LDMDB Encoding T1
1010 if (inst32 & 0xFFD02000 == 0xE9100000)
1012 LOG_MSG("ARM ARM DDI0406 - section A8.6.55 LDMDB Encoding T1");
1014 // Decoding as per ARM ARM description
1015 // TUint32 W = inst32 & 0x00200000 >> 21; // Not needed for this encoding
1016 TUint32 Rn = inst32 & 0x000F0000 >> 16;
1017 TUint32 P = inst32 & 0x00008000 >> 15;
1018 TUint32 M = inst32 & 0x00004000 >> 14;
1019 TUint32 registers = inst32 & 0x00001FFF;
1021 //TBool wback = (W == 1); // not needed for this encoding
1024 if (Rn == 15 || BitCount(registers) < 2 || ((P == 1) && (M==1)))
1026 LOG_MSG("ARM ARM DDI0406 - section A8.6.55 LDMDB Encoding T1 - Unpredictable");
1031 ReturnIfError(RegisterValue(aThread,Rn,address));
1033 address -= 4*BitCount(registers);
1035 for(TInt i=0; i<15; i++)
1037 if (IsBitSet(registers,i))
1043 if (IsBitSet(registers,15))
1046 ReturnIfError(ReadMem32(aThread,address,RnVal));
1048 breakAddress = RnVal;
1053 // ARM ARM DDI0406A - section A8.6.121 POP
1055 // POP.W Encoding T2
1056 if (inst32 & 0xFFFF2000 == 0xE8BD0000)
1058 LOG_MSG("ARM ARM DDI0406A - section A8.6.121 POP Encoding T2");
1060 // Decoding as per ARM ARM description
1061 TUint32 registers = inst32 & 0x00001FFF;
1062 TUint32 P = inst32 & 0x00008000;
1063 TUint32 M = inst32 & 0x00004000;
1066 if ( (BitCount(registers)<2) || ((P == 1)&&(M == 1)) )
1068 LOG_MSG("ARM ARM DDI0406A - section A8.6.121 POP Encoding T2 - Unpredictable");
1073 ReturnIfError(RegisterValue(aThread,13,address));
1075 for(TInt i=0; i< 15; i++)
1077 if (IsBitSet(registers,i))
1083 // Is the PC written?
1084 if (IsBitSet(registers,15))
1087 ReturnIfError(ReadMem32(aThread,address,breakAddress));
1092 if (inst32 & 0xFFFF0FFFF == 0xF85D0B04)
1094 LOG_MSG("ARM ARM DDI0406A - section A8.6.121 POP Encoding T3");
1096 // Decoding as per ARM ARM description
1097 TUint32 Rt = inst32 & 0x0000F000 >> 12;
1098 TUint32 registers = 1 << Rt;
1101 if (Rt == 13 || Rt == 15)
1103 LOG_MSG("ARM ARM DDI0406A - section A8.6.121 POP Encoding T3 - Unpredictable");
1108 ReturnIfError(RegisterValue(aThread,13,address));
1110 for(TInt i=0; i< 15; i++)
1112 if (IsBitSet(registers,i))
1118 // Is the PC written?
1119 if (IsBitSet(registers,15))
1122 ReturnIfError(ReadMem32(aThread,address,breakAddress));
1128 // ARM ARM DDI0406A - section A8.6.53 LDM
1130 // Load Multiple Encoding T2
1131 if ((inst32 & 0xFFD02000) == 0xE8900000)
1133 LOG_MSG("ARM ARM DDI0406A - section A8.6.53 LDM Encoding T2");
1135 // Decoding as per ARM ARM description
1136 TUint32 W = inst32 & 0x0020000 >> 21;
1137 TUint32 Rn = inst32 & 0x000F0000 >> 16;
1138 TUint32 P = inst32 & 0x00008000 >> 15;
1139 TUint32 M = inst32 & 0x00004000 >> 14;
1140 TUint32 registers = inst32 & 0x0000FFFF;
1141 TUint32 register_list = inst32 & 0x00001FFF;
1144 if ( (W == 1) && (Rn == 13) )
1147 LOG_MSG("ARM ARM DDI0406A - section A8.6.53 LDM Encoding T2 - POP");
1151 if (Rn == 15 || BitCount(register_list) < 2 || ((P == 1) && (M == 1)) )
1153 LOG_MSG("ARM ARM DDI0406A - section A8.6.53 LDM Encoding T2 - Unpredictable");
1158 ReturnIfError(RegisterValue(aThread,Rn,RnVal));
1160 TUint32 address = RnVal;
1162 // Calculate offset of address
1163 for(TInt i = 0; i < 15; i++)
1165 if (IsBitSet(registers,i))
1171 // Does it load the PC?
1172 if (IsBitSet(registers,15))
1174 // Obtain the value loaded into the PC
1175 ReturnIfError(ReadMem32(aThread,address,breakAddress));
1181 // ARM ARM DDI0406A - section B6.1.8 RFE
1183 // Return From Exception Encoding T1 RFEDB
1184 if ((inst32 & 0xFFD0FFFF) == 0xE810C000)
1186 LOG_MSG("ARM ARM DDI0406A - section B6.1.8 RFE Encoding T1");
1188 // Decoding as per ARM ARM description
1189 // TUint32 W = (inst32 & 0x00200000) >> 21; // not needed for this encoding
1190 TUint32 Rn = (inst32 & 0x000F0000) >> 16;
1192 // TBool wback = (W == 1); // not needed for this encoding
1193 TBool increment = EFalse;
1194 TBool wordhigher = EFalse;
1200 LOG_MSG("ARM ARM DDI0406A - section B6.1.8 RFE Encoding T1 - Unpredictable");
1205 ReturnIfError(RegisterValue(aThread,Rn,RnVal));
1207 TUint32 address = 0;
1208 ReturnIfError(ReadMem32(aThread,RnVal,address));
1220 breakAddress = address;
1224 // Return From Exception Encoding T2 RFEIA
1225 if ((inst32 & 0xFFD0FFFF) == 0xE990C000)
1227 LOG_MSG("ARM ARM DDI0406A - section B6.1.8 RFE Encoding T2");
1229 // Decoding as per ARM ARM description
1230 // TUint32 W = (inst32 & 0x00200000) >> 21; // not needed for this encoding
1231 TUint32 Rn = (inst32 & 0x000F0000) >> 16;
1233 // TBool wback = (W == 1); // not needed for this encoding
1234 TBool increment = ETrue;
1235 TBool wordhigher = EFalse;
1241 LOG_MSG("ARM ARM DDI0406A - section B6.1.8 RFE Encoding T2 - Unpredictable");
1246 ReturnIfError(RegisterValue(aThread,Rn,RnVal));
1248 TUint32 address = 0;
1249 ReturnIfError(ReadMem32(aThread,RnVal,address));
1261 breakAddress = RnVal;
1265 // Return From Exception Encoding A1 RFE<amode>
1266 if ((inst32 & 0xFE50FFFF) == 0xF8100A00)
1268 LOG_MSG("ARM ARM DDI0406A - section B6.1.8 RFE Encoding A1");
1270 // Decoding as per ARM ARM description
1271 TUint32 P = (inst32 & 0x01000000) >> 24;
1272 TUint32 U = (inst32 & 0x00800000) >> 23;
1273 // TUint32 W = (inst32 & 0x00200000) >> 21; // not needed for this encoding
1274 TUint32 Rn = (inst32 & 0x000F0000) >> 16;
1276 // TBool wback = (W == 1); // not needed for this encoding
1277 TBool increment = (U == 1);
1278 TBool wordhigher = (P == U);
1284 LOG_MSG("ARM ARM DDI0406A - section B6.1.8 RFE Encoding A1 - Unpredictable");
1289 ReturnIfError(RegisterValue(aThread,Rn,RnVal));
1291 TUint32 address = 0;
1292 ReturnIfError(ReadMem32(aThread,RnVal,address));
1304 breakAddress = address;
1309 // v4T/v5T/v6T instructions
1310 switch(thumb_opcode(inst))
1314 // Data-processing. See ARM ARM DDI0406A, section A6-8, A6.2.2.
1316 if ((thumb_inst_7_15(inst) == 0x08F))
1319 err = iChannel->ReadKernelRegisterValue(aThread, ((inst & 0x0078) >> 3), breakAddress);
1322 LOG_MSG2("Non-zero error code discarded: %d", err);
1325 if ((breakAddress & 0x00000001) == 0)
1327 aChangingModes = ETrue;
1330 breakAddress &= 0xFFFFFFFE;
1332 // Report how we decoded this instruction
1333 LOG_MSG("DRMDStepping::PCAfterInstructionExecutes: Decoded as BLX (2)");
1335 else if (thumb_inst_7_15(inst) == 0x08E)
1338 err = iChannel->ReadKernelRegisterValue(aThread, ((inst & 0x0078) >> 3), breakAddress);
1341 LOG_MSG2("Non-zero error code discarded: %d", err);
1344 if ((breakAddress & 0x00000001) == 0)
1346 aChangingModes = ETrue;
1349 breakAddress &= 0xFFFFFFFE;
1351 // Report how we decoded this instruction
1352 LOG_MSG("DRMDStepping::PCAfterInstructionExecutes: Decoded as BX");
1354 else if ((thumb_inst_8_15(inst) == 0x46) && ((inst & 0x87) == 0x87))
1356 // MOV with PC as the destination
1357 err = iChannel->ReadKernelRegisterValue(aThread, ((inst & 0x0078) >> 3), breakAddress);
1360 LOG_MSG2("Non-zero error code discarded: %d", err);
1363 // Report how we decoded this instruction
1364 LOG_MSG("DRMDStepping::PCAfterInstructionExecutes: Decoded as MOV with PC as the destination");
1366 else if ((thumb_inst_8_15(inst) == 0x44) && ((inst & 0x87) == 0x87))
1368 // ADD with PC as the destination
1369 err = iChannel->ReadKernelRegisterValue(aThread, ((inst & 0x0078) >> 3), breakAddress);
1372 LOG_MSG2("Non-zero error code discarded: %d", err);
1374 breakAddress += aCurrentPC + 4; // +4 because we need to use the PC+4 according to ARM ARM DDI0406A, section A6.1.2.
1376 // Report how we decoded this instruction
1377 LOG_MSG("DRMDStepping::PCAfterInstructionExecutes: Decoded as ADD with PC as the destination");
1383 // Load/Store single data item. See ARM ARM DDI0406A, section A6-10
1385 //This instruction doesn't modify the PC.
1387 //if (thumb_inst_8_15(inst) == 0x9F)
1389 // LDR(4) with the PC as the destination
1390 // breakAddress = ReadRegister(aThread, SP_REGISTER) + (4 * (inst & 0x00FF));
1393 // Report how we decoded this instruction
1394 LOG_MSG("DRMDStepping::PCAfterInstructionExecutes: Decoded as This instruction doesn't modify the PC.");
1399 // Misc 16-bit instruction. See ARM ARM DDI0406A, section A6-11
1401 if (thumb_inst_8_15(inst) == 0xBD)
1403 // POP with the PC in the list
1404 TUint32 regList = (inst & 0x00FF);
1406 err = iChannel->ReadKernelRegisterValue(aThread, SP_REGISTER, (T4ByteRegisterValue&)offset);
1409 LOG_MSG2("Non-zero error code discarded: %d", err);
1411 offset += (iChannel->Bitcount(regList) * 4);
1413 TBuf8<4> destination;
1414 err = iChannel->DoReadMemory(aThread, offset, 4, destination);
1416 if (KErrNone == err)
1418 breakAddress = *(TUint32 *)destination.Ptr();
1420 if ((breakAddress & 0x00000001) == 0)
1422 aChangingModes = ETrue;
1425 breakAddress &= 0xFFFFFFFE;
1429 LOG_MSG("Error reading memory in decoding step instruction");
1432 // Report how we decoded this instruction
1433 LOG_MSG("DRMDStepping::PCAfterInstructionExecutes: Decoded as POP with the PC in the list");
1440 // Conditional branch, and supervisor call. See ARM ARM DDI0406A, section A6-13
1442 if (thumb_inst_8_15(inst) < 0xDE)
1444 // B(1) conditional branch
1445 if (IsExecuted(((inst & 0x0F00) >> 8), aStatusRegister))
1447 TUint32 offset = ((inst & 0x000000FF) << 1);
1448 if (offset & 0x00000100)
1450 offset |= 0xFFFFFF00;
1453 breakAddress = aCurrentPC + 4 + offset;
1455 // Report how we decoded this instruction
1456 LOG_MSG("DRMDStepping::PCAfterInstructionExecutes: Decoded as B(1) conditional branch");
1463 // Unconditional branch, See ARM ARM DDI0406A, section A8-44.
1465 // B(2) unconditional branch
1466 TUint32 offset = (inst & 0x000007FF) << 1;
1467 if (offset & 0x00000800)
1469 offset |= 0xFFFFF800;
1472 breakAddress = aCurrentPC + 4 + offset;
1474 // Report how we decoded this instruction
1475 LOG_MSG("DRMDStepping::PCAfterInstructionExecutes: Decoded as B(2) unconditional branch");
1481 if (!(inst & 0x0001))
1484 err = iChannel->ReadKernelRegisterValue(aThread, LINK_REGISTER, breakAddress);
1487 LOG_MSG2("Non-zero error code discarded: %d", err);
1489 breakAddress += ((inst & 0x07FF) << 1);
1490 if ((breakAddress & 0x00000001) == 0)
1492 aChangingModes = ETrue;
1495 breakAddress &= 0xFFFFFFFC;
1497 // Report how we decoded this instruction
1498 LOG_MSG("DRMDStepping::PCAfterInstructionExecutes: Decoded as BLX(1)");
1505 // Check for ARMv7 CPU
1508 // BL/BLX 32-bit instruction
1511 breakAddress = (TUint32)thumb_instr_b_dest(inst32, aCurrentPC);
1513 if((inst32 >> 27) == 0x1D)
1516 if ((breakAddress & 0x00000001) == 0)
1518 aChangingModes = ETrue;
1521 breakAddress &= 0xFFFFFFFC;
1523 // Report how we decoded this instruction
1524 LOG_MSG("DRMDStepping::PCAfterInstructionExecutes: Decoded as 32-bit BLX(1)");
1528 // Report how we decoded this instruction
1529 LOG_MSG("DRMDStepping::PCAfterInstructionExecutes: 32-bit BL instruction");
1531 LOG_MSG2(" 32-bit BL/BLX instruction: breakAddress = 0x%X", breakAddress);
1535 // BL/BLX prefix - destination is encoded in this and the next instruction
1538 // Report how we decoded this instruction
1539 LOG_MSG("DRMDStepping::PCAfterInstructionExecutes: BL/BLX prefix - destination is encoded in this and the next instruction");
1549 err = iChannel->ReadKernelRegisterValue(aThread, LINK_REGISTER, breakAddress);
1552 LOG_MSG2("Non-zero error code discarded: %d", err);
1554 breakAddress += ((inst & 0x07FF) << 1);
1556 // Report how we decoded this instruction
1557 LOG_MSG("DRMDStepping::PCAfterInstructionExecutes: Decoded as BL");
1563 // Don't know any better at this point!
1564 LOG_MSG("DRMDStepping::PCAfterInstructionExecutes:- default to next instruction");
1571 case Debug::EThumb2EEMode:
1573 // Not yet supported
1574 LOG_MSG("DRMDStepping::PCAfterInstructionExecutes - Debug::EThumb2Mode is not supported");
1580 LOG_MSG("DRMDStepping::PCAfterInstructionExecutes - Cannot determine CPU mode architecture");
1583 LOG_MSG2("DRMDStepping::PCAfterInstructionExecutes : return 0x%08x",breakAddress);
1584 return breakAddress;
1587 // Obtain a 32-bit memory value with minimum fuss
1588 TInt DRMDStepping::ReadMem32(DThread* aThread, const TUint32 aAddress, TUint32& aValue)
1591 TInt err = iChannel->DoReadMemory(aThread, aAddress, 4, valBuf);
1592 if (err != KErrNone)
1594 LOG_MSG2("DRMDStepping::ReadMem32 failed to read memory at 0x%08x", aAddress);
1598 aValue = *(TUint32 *)valBuf.Ptr();
1603 // Obtain a 16-bit memory value with minimum fuss
1604 TInt DRMDStepping::ReadMem16(DThread* aThread, const TUint32 aAddress, TUint16& aValue)
1607 TInt err = iChannel->DoReadMemory(aThread, aAddress, 2, valBuf);
1608 if (err != KErrNone)
1610 LOG_MSG2("DRMDStepping::ReadMem16 failed to read memory at 0x%08x", aAddress);
1614 aValue = *(TUint16 *)valBuf.Ptr();
1619 // Obtain a 16-bit memory value with minimum fuss
1620 TInt DRMDStepping::ReadMem8(DThread* aThread, const TUint32 aAddress, TUint8& aValue)
1623 TInt err = iChannel->DoReadMemory(aThread, aAddress, 1, valBuf);
1624 if (err != KErrNone)
1626 LOG_MSG2("DRMDStepping::ReadMem8 failed to read memory at 0x%08x", aAddress);
1630 aValue = *(TUint8 *)valBuf.Ptr();
1635 // Obtain a core register value with minimum fuss
1636 TInt DRMDStepping::RegisterValue(DThread *aThread, const TUint32 aKernelRegisterId, TUint32 &aValue)
1638 TInt err = iChannel->ReadKernelRegisterValue(aThread, aKernelRegisterId, aValue);
1641 LOG_MSG3("DRMDStepping::RegisterValue failed to read register %d err = %d", aKernelRegisterId, err);
1647 // Encodings from ARM ARM DDI0406A, section 9.2.1
1648 enum TThumb2EEOpcode
1650 EThumb2HDP, // Handler Branch with Parameter
1651 EThumb2UNDEF, // UNDEFINED
1652 EThumb2HB, // Handler Branch, Handler Branch with Link
1653 EThumb2HBLP, // Handle Branch with Link and Parameter
1654 EThumb2LDRF, // Load Register from a frame
1655 EThumb2CHKA, // Check Array
1656 EThumb2LDRL, // Load Register from a literal pool
1657 EThumb2LDRA, // Load Register (array operations)
1658 EThumb2STR // Store Register to a frame
1662 // DRMDStepping::ShiftedRegValue
1664 TUint32 DRMDStepping::ShiftedRegValue(DThread *aThread, TUint32 aInstruction, TUint32 aCurrentPC, TUint32 aStatusRegister)
1666 LOG_MSG("DRM_DebugChannel::ShiftedRegValue()");
1669 if (aInstruction & 0x10) // bit 4
1671 shift = (arm_rs(aInstruction) == PC_REGISTER ? aCurrentPC + 8 : aStatusRegister) & 0xFF;
1675 shift = arm_data_c(aInstruction);
1678 TInt rm = arm_rm(aInstruction);
1681 if(rm == PC_REGISTER)
1683 res = aCurrentPC + ((aInstruction & 0x10) ? 12 : 8);
1687 TInt err = iChannel->ReadKernelRegisterValue(aThread, rm, res);
1690 LOG_MSG2("DRMDStepping::ShiftedRegValue - Non-zero error code discarded: %d", err);
1694 switch(arm_data_shift(aInstruction))
1698 res = shift >= 32 ? 0 : res << shift;
1703 res = shift >= 32 ? 0 : res >> shift;
1710 res = ((res & 0x80000000L) ? ~((~res) >> shift) : res >> shift);
1718 res = (res >> 1) | ((aStatusRegister & arm_carry_bit()) ? 0x80000000L : 0);
1722 res = (res >> shift) | (res << (32 - shift));
1728 return res & 0xFFFFFFFF;
1732 // DRMDStepping::CurrentPC
1736 TInt DRMDStepping::CurrentPC(DThread* aThread, TUint32& aPC)
1738 LOG_MSG("DRMDStepping::CurrentPC");
1740 TInt err = iChannel->ReadKernelRegisterValue(aThread, PC_REGISTER, aPC);
1743 // We don't know the current PC for this thread!
1744 LOG_MSG("DRMDStepping::CurrentPC - Failed to read the current PC");
1749 LOG_MSG2("DRMDStepping::CurrentPC 0x%08x", aPC);
1755 // DRMDStepping::CurrentCPSR
1759 TInt DRMDStepping::CurrentCPSR(DThread* aThread, TUint32& aCPSR)
1761 LOG_MSG("DRMDStepping::CurrentCPSR");
1763 TInt err = iChannel->ReadKernelRegisterValue(aThread, STATUS_REGISTER, aCPSR);
1766 // We don't know the current PC for this thread!
1767 LOG_MSG("DRMDStepping::CurrentPC - Failed to read the current CPSR");
1772 LOG_MSG2("DRMDStepping::CurrentCPSR 0x%08x", aCPSR);
1778 // DRMDStepping::ModifyBreaksForStep
1780 // Set a temporary breakpoint at the next instruction to be executed after the one at the current PC
1781 // Disable the breakpoint at the current PC if one exists
1783 TInt DRMDStepping::ModifyBreaksForStep(DThread *aThread, TUint32 aRangeStart, TUint32 aRangeEnd, /*TBool aStepInto,*/ TBool aResumeOnceOutOfRange, TBool aCheckForStubs, const TUint32 aNumSteps)
1785 LOG_MSG2("DRMDStepping::ModifyBreaksForStep() Numsteps 0x%d",aNumSteps);
1787 // Validate arguments
1790 LOG_MSG("DRMDStepping::ModifyBreaksForStep() - No aThread specified to step");
1791 return KErrArgument;
1797 ReturnIfError(CurrentPC(aThread,currentPC));
1798 LOG_MSG2("Current PC: 0x%x", currentPC);
1800 // disable breakpoint at the current PC if necessary
1801 ReturnIfError(iChannel->iBreakManager->DisableBreakAtAddress(currentPC));
1804 TUint32 statusRegister;
1806 ReturnIfError(CurrentCPSR(aThread,statusRegister));
1807 LOG_MSG2("Current CPSR: %x", statusRegister);
1809 TBool thumbMode = (statusRegister & ECpuThumb);
1811 LOG_MSG("Thumb Mode");
1813 TInt instSize = thumbMode ? 2 : 4;
1815 TBool changingModes = EFalse;
1817 TUint32 breakAddress = 0;
1819 TUint32 newRangeEnd = aRangeEnd;
1821 breakAddress = PCAfterInstructionExecutes(aThread, currentPC, statusRegister, instSize, /* aStepInto, */ newRangeEnd, changingModes);
1824 If there is already a user breakpoint at this address, we do not need to set a temp breakpoint. The program
1825 should simply stop at that address.
1827 TBreakEntry* breakEntry = NULL;
1830 breakEntry = iChannel->iBreakManager->GetNextBreak(breakEntry);
1831 if(breakEntry && !iChannel->iBreakManager->IsTemporaryBreak(*breakEntry))
1833 if ((breakEntry->iAddress == breakAddress) && ((breakEntry->iThreadSpecific && breakEntry->iId == aThread->iId) || (!breakEntry->iThreadSpecific && breakEntry->iId == aThread->iOwningProcess->iId)))
1835 LOG_MSG("DRMDStepping::ModifyBreaksForStep - Breakpoint already exists at the step target address\n");
1837 // note also that if this is the case, we will not keep stepping if we hit a real breakpoint, so may as well set
1838 // the step count = 0.
1839 breakEntry->iNumSteps = 0;
1844 } while(breakEntry);
1849 breakEntry = iChannel->iBreakManager->GetNextBreak(breakEntry);
1850 if(breakEntry && iChannel->iBreakManager->IsTemporaryBreak(*breakEntry))
1852 if (breakEntry->iAddress == 0)
1854 breakEntry->iId = aThread->iId;
1855 breakEntry->iAddress = breakAddress;
1856 breakEntry->iThreadSpecific = ETrue;
1858 TBool realThumbMode = (thumbMode && !changingModes) || (!thumbMode && changingModes);
1860 // Need to set the correct type of breakpoint for the mode we are in
1861 // and the the one we are changing into
1864 // We are remaining in Thumb mode
1865 breakEntry->iMode = EThumbMode;
1869 // We are switching to ARM mode
1870 breakEntry->iMode = EArmMode;
1873 breakEntry->iResumeOnceOutOfRange = aResumeOnceOutOfRange;
1874 breakEntry->iSteppingInto = ETrue /* aStepInto */;
1875 breakEntry->iRangeStart = 0; // no longer used
1876 breakEntry->iRangeEnd = 0; // no longer used
1878 LOG_MSG2("Adding temp breakpoint with id: %d", breakEntry->iBreakId);
1879 LOG_MSG2("Adding temp breakpoint with thread id: %d", aThread->iId);
1881 // Record how many more steps to go after we hit this one
1882 breakEntry->iNumSteps = aNumSteps;
1884 LOG_MSG3("Setting temp breakpoint id %d with %d steps to go\n", breakEntry->iBreakId, aNumSteps);
1886 return iChannel->iBreakManager->DoEnableBreak(*breakEntry, ETrue);
1889 } while(breakEntry);
1890 LOG_MSG("ModifyBreaksForStep : Failed to set suitable breakpoint for stepping");
1891 return KErrNoMemory; // should never get here
1894 // End of file - d-rmd-stepping.cpp