summaryrefslogtreecommitdiff
path: root/src/debug/ee/arm/armwalker.cpp
blob: 01e77b18906a16f86cdb3dce3799ea81c83db2e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//*****************************************************************************
// File: armwalker.cpp
// 

//
// ARM instruction decoding/stepping logic
//
//*****************************************************************************

#include "stdafx.h"

#include "walker.h"

#include "frames.h"
#include "openum.h"


#ifdef _TARGET_ARM_

void NativeWalker::Decode()
{
    // Set default next and skip instruction pointers.
    m_nextIP = NULL;
    m_skipIP = NULL;
    m_type = WALK_UNKNOWN;

    // We can't walk reliably without registers (because we need to know the IT state to determine whether or
    // not the current instruction will be executed).
    if (m_registers == NULL)
        return;

    // Determine whether we're executing in an IT block. If so, check the condition codes and IT state to see
    // whether we'll execute the current instruction.
    BYTE bITState = (BYTE)((BitExtract((WORD)m_registers->pCurrentContext->Cpsr, 15, 10) << 2) |
                           BitExtract((WORD)(m_registers->pCurrentContext->Cpsr >> 16), 10, 9));
    if ((bITState & 0x1f) && !ConditionHolds(BitExtract(bITState, 7, 4)))
    {
        // We're in an IT block and the state is such that the current instruction is not scheduled to
        // execute. Just return WALK_UNKNOWN so the caller will invoke single-step to update the register
        // context correctly for the next instruction.

        LOG((LF_CORDB, LL_INFO100000, "ArmWalker::Decode: IT block at %x\n", m_ip));
        return;
    }

    // Fetch first word of the current instruction. From this we can determine if we've gotten the whole thing
    // or we're dealing with a 32-bit instruction.  If the current instruction is a break instruction, we'll
    // need to check the patch table to get the correct instruction.
    WORD opcode1 = CORDbgGetInstruction(m_ip);
    PRD_TYPE unpatchedOpcode;
    if (DebuggerController::CheckGetPatchedOpcode(m_ip, &unpatchedOpcode))
    {
        opcode1 = (WORD) unpatchedOpcode;
    }


    if (Is32BitInstruction(opcode1))
    {
        // Fetch second word of 32-bit instruction.
        WORD opcode2 = CORDbgGetInstruction((BYTE*)((DWORD)m_ip) + 2);

        LOG((LF_CORDB, LL_INFO100000, "ArmWalker::Decode 32bit instruction at %x, opcode: %x%x\n", m_ip, (DWORD)opcode1, (DWORD)opcode2));

        // WALK_RETURN
        if (((opcode1 & 0xffd0) == 0xe890) &&
            ((opcode2 & 0x2000) == 0x0000))
        {
            // LDM.W : T2, POP.W : T2
            DWORD registerList = opcode2;

            if (registerList & 0x8000)
            {
                m_type = WALK_RETURN;
                return;
            }
        }

        // WALK_BRANCH
        else if (((opcode1 & 0xf800) == 0xf000) &&
                 ((opcode2 & 0xd000) == 0x8000) &&
                 ((opcode1 & 0x0380) != 0x0380))
        {
            // B.W : T3
            DWORD S = BitExtract(opcode1, 10, 10);
            DWORD cond = BitExtract(opcode1, 9, 6);
            DWORD imm6 = BitExtract(opcode1, 5, 0);
            DWORD J1 = BitExtract(opcode2, 13, 13);
            DWORD J2 = BitExtract(opcode2, 11, 11);
            DWORD imm11 = BitExtract(opcode2, 10, 0);

            if (ConditionHolds(cond))
            {
                DWORD disp = (S ? 0xfff00000 : 0) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
                m_nextIP = (BYTE*)((GetReg(15) + disp) | THUMB_CODE);
                m_skipIP = m_nextIP;
                m_type = WALK_BRANCH;
                return;
            }
        }
        else if (((opcode1 & 0xf800) == 0xf000) &&
                 ((opcode2 & 0xd000) == 0x9000))
        {
            // B.W : T4
            DWORD S = BitExtract(opcode1, 10, 10);
            DWORD imm10 = BitExtract(opcode1, 9, 0);
            DWORD J1 = BitExtract(opcode2, 13, 13);
            DWORD J2 = BitExtract(opcode2, 11, 11);
            DWORD imm11 = BitExtract(opcode2, 10, 0);

            DWORD I1 = (J1 ^ S) ^ 1;
            DWORD I2 = (J2 ^ S) ^ 1;

            DWORD disp = (S ? 0xff000000 : 0) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);

            m_nextIP = (BYTE*)((GetReg(15) + disp) | THUMB_CODE);
            m_skipIP = m_nextIP;
            m_type = WALK_BRANCH;
            return;
        }
        else if (((opcode1 & 0xfff0) == 0xf8d0) &&
                 ((opcode1 & 0x000f) != 0x000f))
        {
            // LDR.W (immediate): T3
            DWORD Rn = BitExtract(opcode1, 3, 0);
            DWORD Rt = BitExtract(opcode2, 15, 12);
            DWORD imm12 = BitExtract(opcode2, 11, 0);

            if (Rt == 15)
            {
                DWORD value = *(DWORD*)(GetReg(Rn) + imm12);

                m_nextIP = (BYTE*)(value | THUMB_CODE);
                m_skipIP = m_nextIP;
                m_type = WALK_BRANCH;
                return;
            }
        }
        else if (((opcode1 & 0xfff0) == 0xf850) &&
                 ((opcode2 & 0x0800) == 0x0800) &&
                 ((opcode1 & 0x000f) != 0x000f))
        {
            // LDR (immediate) : T4, POP : T3
            DWORD Rn = BitExtract(opcode1, 3, 0);
            DWORD Rt = BitExtract(opcode2, 15, 12);
            DWORD P = BitExtract(opcode2, 10, 10);
            DWORD U = BitExtract(opcode2, 9, 9);
            DWORD imm8 = BitExtract(opcode2, 7, 0);

            if (Rt == 15)
            {
                DWORD offset_addr = U ? GetReg(Rn) + imm8 : GetReg(Rn) - imm8;
                DWORD addr = P ? offset_addr : GetReg(Rn);

                DWORD value = *(DWORD*)addr;

                m_nextIP = (BYTE*)(value | THUMB_CODE);
                m_skipIP = m_nextIP;
                m_type = WALK_BRANCH;
                return;
            }
        }
        else if (((opcode1 & 0xff7f) == 0xf85f))
        {
            // LDR.W (literal) : T2
            DWORD U = BitExtract(opcode1, 7, 7);
            DWORD Rt = BitExtract(opcode2, 15, 12);
            DWORD imm12 = BitExtract(opcode2, 11, 0);

            if (Rt == 15)
            {
                DWORD addr = GetReg(15) & ~3;
                addr = U ? addr + imm12 : addr - imm12;

                DWORD value = *(DWORD*)addr;

                m_nextIP = (BYTE*)(value | THUMB_CODE);
                m_skipIP = m_nextIP;
                m_type = WALK_BRANCH;
                return;
            }
        }
        else if (((opcode1 & 0xfff0) == 0xf850) &&
                 ((opcode2 & 0x0fc0) == 0x0000) &&
                 ((opcode1 & 0x000f) != 0x000f))
        {
            // LDR.W : T2
            DWORD Rn = BitExtract(opcode1, 3, 0);
            DWORD Rt = BitExtract(opcode2, 15, 12);
            DWORD imm2 = BitExtract(opcode2, 5, 4);
            DWORD Rm = BitExtract(opcode2, 3, 0);

            if (Rt == 15)
            {
                DWORD addr = GetReg(Rn) + (GetReg(Rm) << imm2);

                DWORD value = *(DWORD*)addr;

                m_nextIP = (BYTE*)(value | THUMB_CODE);
                m_skipIP = m_nextIP;
                m_type = WALK_BRANCH;
                return;
            }
        }
        else if (((opcode1 & 0xfff0) == 0xe8d0) &&
                 ((opcode2 & 0xffe0) == 0xf000))
        {
            // TBB/TBH : T1
            DWORD Rn = BitExtract(opcode1, 3, 0);
            DWORD H = BitExtract(opcode2, 4, 4);
            DWORD Rm = BitExtract(opcode2, 3, 0);

            DWORD addr = GetReg(Rn);

            DWORD value;
            if (H)
                value = *(WORD*)(addr + (GetReg(Rm) << 1));
            else
                value = *(BYTE*)(addr + GetReg(Rm));

            m_nextIP = (BYTE*)((GetReg(15) + (value << 1)) | THUMB_CODE);
            m_skipIP = m_nextIP;
            m_type = WALK_BRANCH;
            return;
        }

        // WALK_CALL
        else if (((opcode1 & 0xf800) == 0xf000) &&
                 ((opcode2 & 0xd000) == 0xd000))
        {
            // BL (immediate) : T1
            DWORD S = BitExtract(opcode1, 10, 10);
            DWORD imm10 = BitExtract(opcode1, 9, 0);
            DWORD J1 = BitExtract(opcode2, 13, 13);
            DWORD J2 = BitExtract(opcode2, 11, 11);
            DWORD imm11 = BitExtract(opcode2, 10, 0);

            DWORD I1 = (J1 ^ S) ^ 1;
            DWORD I2 = (J2 ^ S) ^ 1;

            DWORD disp = (S ? 0xff000000 : 0) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);

            m_nextIP = (BYTE*)((GetReg(15) + disp) | THUMB_CODE);
            m_skipIP =(BYTE*)(((DWORD)m_ip) + 4);
            m_type = WALK_CALL;
            return;
        }
    }
    else
    {
        LOG((LF_CORDB, LL_INFO100000, "ArmWalker::Decode 16bit instruction at %x, opcode: %x\n", m_ip, (DWORD)opcode1));
        // WALK_RETURN
        if ((opcode1 & 0xfe00) == 0xbc00)
        {
            // POP : T1
            DWORD P = BitExtract(opcode1, 8, 8);
            DWORD registerList = (P << 15) | BitExtract(opcode1, 7, 0);

            if (registerList & 0x8000)
            {
                m_type = WALK_RETURN;
                return;
            }
        }

        // WALK_BRANCH
        else if (((opcode1 & 0xf000) == 0xd000) &&
            ((opcode1 & 0x0f00) != 0x0e00) )
        {
            // B : T1
            DWORD cond = BitExtract(opcode1, 11, 8);
            DWORD imm8 = BitExtract(opcode1, 7, 0);

            if (ConditionHolds(cond))
            {
                DWORD disp = (imm8 << 1) | ((imm8 & 0x80) ? 0xffffff00 : 0);

                m_nextIP = (BYTE*)((GetReg(15) + disp) | THUMB_CODE);
                m_skipIP = m_nextIP;
                m_type = WALK_BRANCH;
                return;
            }
        }
        else if ((opcode1 & 0xf800) == 0xe000)
        {
            // B : T2
            DWORD imm11 = BitExtract(opcode1, 10, 0);

            DWORD disp = (imm11 << 1) | ((imm11 & 0x400) ? 0xfffff000 : 0);

            m_nextIP = (BYTE*)((GetReg(15) + disp) | THUMB_CODE);
            m_skipIP = m_nextIP;
            m_type = WALK_BRANCH;
            return;
        }
        else if ((opcode1 & 0xff87) == 0x4700)
        {
            // BX : T1
            DWORD Rm = BitExtract(opcode1, 6, 3);

            m_nextIP = (BYTE*)(GetReg(Rm) | THUMB_CODE);
            m_skipIP = m_nextIP;
            m_type = (Rm != 14) ? WALK_BRANCH : WALK_RETURN;
            return;
        }
        else if ((opcode1 & 0xff00) == 0x4600)
        {
            // MOV (register) : T1
            DWORD D = BitExtract(opcode1, 7, 7);
            DWORD Rm = BitExtract(opcode1, 6, 3);
            DWORD Rd = (D << 3) | BitExtract(opcode1, 2, 0);

            if (Rd == 15)
            {
                m_nextIP = (BYTE*)(GetReg(Rm) | THUMB_CODE);
                m_skipIP = m_nextIP;
                m_type = WALK_BRANCH;
                return;
            }
        }

        // WALK_CALL
        else if ((opcode1 & 0xff87) == 0x4780)
        {
            // BLX (register) : T1
            DWORD Rm = BitExtract(opcode1, 6, 3);

            m_nextIP = (BYTE*)(GetReg(Rm) | THUMB_CODE);
            m_skipIP = (BYTE*)(((DWORD)m_ip) + 2);
            m_type = WALK_CALL;
            return;
        }
    }
}

// Get the current value of a register. PC (register 15) is always reported as the current instruction PC + 4
// as per the ARM architecture.
DWORD NativeWalker::GetReg(DWORD reg)
{
    _ASSERTE(reg <= 15);

    if (reg == 15)
        return (m_registers->pCurrentContext->Pc + 4) & ~THUMB_CODE;

    return (&m_registers->pCurrentContext->R0)[reg];
}

// Returns true if the current context indicates the ARM condition specified holds.
bool NativeWalker::ConditionHolds(DWORD cond)
{
    // Bit numbers of the condition flags in the CPSR.
    enum APSRBits
    {
        APSR_N = 31,
        APSR_Z = 30,
        APSR_C = 29,
        APSR_V = 28,
    };

// Return true if the given condition (C, N, Z or V) holds in the current context.
#define GET_FLAG(_flag)                         \
    ((m_registers->pCurrentContext->Cpsr & (1 << APSR_##_flag)) != 0)

    switch (cond)
    {
    case 0:                 // EQ (Z==1)
        return GET_FLAG(Z);
    case 1:                 // NE (Z==0)
        return !GET_FLAG(Z);
    case 2:                 // CS (C==1)
        return GET_FLAG(C);
    case 3:                 // CC (C==0)
        return !GET_FLAG(C);
    case 4:                 // MI (N==1)
        return GET_FLAG(N);
    case 5:                 // PL (N==0)
        return !GET_FLAG(N);
    case 6:                 // VS (V==1)
        return GET_FLAG(V);
    case 7:                 // VC (V==0)
        return !GET_FLAG(V);
    case 8:                 // HI (C==1 && Z==0)
        return GET_FLAG(C) && !GET_FLAG(Z);
    case 9:                 // LS (C==0 || Z==1)
        return !GET_FLAG(C) || GET_FLAG(Z);
    case 10:                // GE (N==V)
        return GET_FLAG(N) == GET_FLAG(V);
    case 11:                // LT (N!=V)
        return GET_FLAG(N) != GET_FLAG(V);
    case 12:                // GT (Z==0 && N==V)
        return !GET_FLAG(Z) && (GET_FLAG(N) == GET_FLAG(V));
    case 13:                // LE (Z==1 || N!=V)
        return GET_FLAG(Z) || (GET_FLAG(N) != GET_FLAG(V));
    case 14:                // AL
        return true;
    case 15:
        _ASSERTE(!"Unsupported condition code: 15");
        return false;
    default:
        UNREACHABLE();
        return false;
    }
}

#endif