summaryrefslogtreecommitdiff
path: root/src/debug/ee/arm64/arm64walker.cpp
blob: b38297b3ec9b9682b87d705d5b262cf0ac6e1955 (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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
// 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: Arm64walker.cpp
// 

//
// ARM64 instruction decoding/stepping logic
//
//*****************************************************************************

#include "stdafx.h"
#include "walker.h"
#include "frames.h"
#include "openum.h"

#ifdef _TARGET_ARM64_

PCODE Expand19bitoffset(PCODE opcode)
{
    opcode = opcode >> 5;
    PCODE  offset = (opcode & 0x7FFFF) << 2; //imm19:00 -> 21 bits

    //Sign Extension
    if ((offset & 0x100000)) //Check for 21'st bit
    {
        offset = offset | 0xFFFFFFFFFFE00000;
    }
    return offset;
}

void NativeWalker::Decode()
{
    
    PT_CONTEXT context = NULL;
    int RegNum = -1;
    PCODE offset = MAX_INSTRUCTION_LENGTH;

    //Reset so that we do not provide bogus info
    m_type = WALK_UNKNOWN;
    m_skipIP = NULL;
    m_nextIP = NULL;

    if (m_registers == NULL)
    {
       //walker does not use WALK_NEXT
       //Without registers decoding will work only for handful of instructions 
       return; 
    }

    m_skipIP = m_ip + MAX_INSTRUCTION_LENGTH;

    context = m_registers->pCurrentContext;
    // Fetch first word of the current instruction.If the current instruction is a break instruction, we'll
    // need to check the patch table to get the correct instruction.
    PRD_TYPE opcode = CORDbgGetInstruction(m_ip);
    PRD_TYPE unpatchedOpcode;
    if (DebuggerController::CheckGetPatchedOpcode(m_ip, &unpatchedOpcode))
    {
        opcode =  unpatchedOpcode;
    }

    LOG((LF_CORDB, LL_INFO100000, "Arm64Walker::Decode instruction at %p, opcode: %x\n", m_ip,opcode));
    
    
    
    if (NativeWalker::DecodeCallInst(opcode, RegNum, m_type)) //Unconditional Branch (register) instructions
    {
        if (m_type == WALK_RETURN)
        {
            m_skipIP = NULL;
        }
        m_nextIP = (BYTE*)GetReg(context, RegNum);
        return;
     }
        
   
    if (NativeWalker::DecodePCRelativeBranchInst(context, opcode, offset, m_type))
    {
        if (m_type == WALK_BRANCH)
        {
            m_skipIP = NULL;
        }
    }
  
    m_nextIP = m_ip + offset;
 

    return;
}


//When control reaches here m_pSharedPatchBypassBuffer has the original instructions in m_pSharedPatchBypassBuffer->PatchBypass
BYTE*  NativeWalker::SetupOrSimulateInstructionForPatchSkip(T_CONTEXT * context, SharedPatchBypassBuffer* m_pSharedPatchBypassBuffer,  const BYTE *address, PRD_TYPE opcode)
{
    
    BYTE* patchBypass = m_pSharedPatchBypassBuffer->PatchBypass;
    PCODE offset = 0;
    PCODE ip = 0;
    WALK_TYPE walk = WALK_UNKNOWN;
    int RegNum =-1;
    

    /*
    Modify the patchBypass if the opcode is IP-relative, otherwise return it
    The following are the instructions that are IP-relative  :
    . ADR and ADRP.
    . The Load register (literal) instruction class.
    . Direct branches that use an immediate offset.
    . The unconditional branch with link instructions, BL and BLR, that use the PC to create the return link
      address.
    */

    _ASSERTE((UINT_PTR)address == context->Pc);

    if ((opcode & 0x1F000000) == 0x10000000)  //ADR & ADRP
    {
        
        TADDR immhigh = ((opcode >> 5) & 0x007FFFF) << 2;
        TADDR immlow  = (opcode & 0x60000000) >> 29;
        offset = immhigh | immlow; //ADR 
        RegNum = (opcode & 0x1F);

        //Sign Extension
        if ((offset & 0x100000))  //Check for 21'st bit
        {
            offset = offset | 0xFFFFFFFFFFE00000;
        }

        if ((opcode & 0x80000000) != 0) //ADRP
        {
            offset = offset << 12; 
            LOG((LF_CORDB, LL_INFO100000, "Arm64Walker::Simulate opcode: %x to ADRP X%d %p\n", opcode, RegNum, offset));
        }
        else
        {
            LOG((LF_CORDB, LL_INFO100000, "Arm64Walker::Simulate opcode: %x to ADR X%d %p\n", opcode, RegNum, offset));
        }

       
    }
    
    else if ((opcode & 0x3B000000) == 0x18000000) //LDR Literal (General or SIMD)
     {
        
        offset = Expand19bitoffset(opcode);
        RegNum = (opcode & 0x1F);
        LOG((LF_CORDB, LL_INFO100000, "Arm64Walker::Simulate opcode: %x to LDR[SW] | PRFM X%d %p\n", opcode, RegNum, offset));
    }
    else if (NativeWalker::DecodePCRelativeBranchInst(context,opcode, offset, walk)) 
    {
        _ASSERTE(RegNum == -1);
    }
    else if (NativeWalker::DecodeCallInst(opcode, RegNum, walk)) 
    {
        _ASSERTE(offset == 0);
    }
    //else  Just execute the opcodes as is
    //{
    //}
    
    if (offset != 0) // calculate the next ip from current ip
    {
        ip = (PCODE)address + offset;
    }
    else if(RegNum >= 0)
    {
        ip = GetReg(context, RegNum);
    }

    //Do instruction emulation inplace here

    if (walk == WALK_BRANCH || walk == WALK_CALL || walk == WALK_RETURN)
    {
        CORDbgSetInstruction((CORDB_ADDRESS_TYPE *)patchBypass, 0xd503201f); //Add Nop in buffer
        
        m_pSharedPatchBypassBuffer->RipTargetFixup = ip; //Control Flow simulation alone is done DebuggerPatchSkip::TriggerExceptionHook
        LOG((LF_CORDB, LL_INFO100000, "Arm64Walker::Simulate opcode: %x  is a Control Flow instr \n", opcode));

        if (walk == WALK_CALL) //initialize Lr
        {
            SetLR(context, (PCODE)address + MAX_INSTRUCTION_LENGTH);
            LOG((LF_CORDB, LL_INFO100000, "Arm64Walker::Simulate opcode: %x  is a Call instr, setting LR to %p \n", opcode,GetLR(context)));
        }
    }
    else if(RegNum >= 0)
    {
        CORDbgSetInstruction((CORDB_ADDRESS_TYPE *)patchBypass, 0xd503201f); //Add Nop in buffer

        PCODE RegContents;
        if ((opcode & 0x3B000000) == 0x18000000) //LDR Literal
        {
            RegContents = (PCODE)GetMem(ip);
            if ((opcode & 0x4000000)) //LDR literal for SIMD
            {
                NEON128 SimdRegContents;
                LOG((LF_CORDB, LL_INFO100000, "Arm64Walker::Simulate opcode: %x to LDR V%d %p\n", opcode, RegNum, offset));
                short opc = (opcode >> 30);
                switch (opc)
                {
                case 0: //4byte data into St
                    RegContents = 0xFFFFFFFF & RegContents;  //zero the upper 32bit
                    SetReg(context, RegNum, RegContents);
                case 1: //8byte data into Dt
                    SetReg(context, RegNum, RegContents);
                    break;

                case 2: //SIMD 16 byte data
                    SimdRegContents = GetSimdMem(ip);
                    SetSimdReg(context, RegNum, SimdRegContents);
                    break;
                default:
                    LOG((LF_CORDB, LL_INFO100000, "Arm64Walker::Simulate Unknown opcode: %x [LDR(litera,SIMD &FP)]  \n", opcode));
                    _ASSERTE(!("Arm64Walker::Simulated Unknown opcode"));

                }
            }
            else 
            {
                short opc = (opcode >> 30);
                switch (opc)
                {
                case 0: //4byte data into Wt
                    LOG((LF_CORDB, LL_INFO100000, "Arm64Walker::Simulate opcode: %x to LDR W%d %p\n", opcode, RegNum, offset));
                    RegContents = 0xFFFFFFFF & RegContents;  //zero the upper 32bits
                    SetReg(context, RegNum, RegContents);
                    break;

                case 1: //8byte data into Xt
                    LOG((LF_CORDB, LL_INFO100000, "Arm64Walker::Simulate opcode: %x to LDR X%d %p\n", opcode, RegNum, offset));
                    SetReg(context, RegNum, RegContents);
                    break;

                case 2: //LDRSW 
                    LOG((LF_CORDB, LL_INFO100000, "Arm64Walker::Simulate opcode: %x to LDRSW X%d %p\n", opcode, RegNum, offset));
                    RegContents = 0xFFFFFFFF & RegContents;
                    
                    if (RegContents & 0x80000000) //Sign extend the Word
                    {
                        RegContents = 0xFFFFFFFF00000000 | RegContents;
                    }
                    SetReg(context, RegNum, RegContents);
                    break;
                case 3:
                    LOG((LF_CORDB, LL_INFO100000, "Arm64Walker::Simulate opcode: %x  as PRFM ,but do nothing \n", opcode));
                   
                    break;
                default:
                    LOG((LF_CORDB, LL_INFO100000, "Arm64Walker::Simulate Unknown opcode: %x [LDR(literal)]  \n", opcode));
                    _ASSERTE(!("Arm64Walker::Simulated Unknown opcode"));

                }
            }
        }
        else
        {
            RegContents = ip;
            SetReg(context, RegNum, RegContents);
        }
        
        LOG((LF_CORDB, LL_INFO100000, "Arm64Walker::Simulate opcode: %x  to update Reg X[V]%d, as %p \n", opcode, RegNum, GetReg(context, RegNum)));
    }
    //else  Just execute the opcodes as IS
    //{
    //}

    return patchBypass;
}

//Decodes PC Relative Branch Instructions
//This code  is shared between the NativeWalker and DebuggerPatchSkip.
//So ENSURE THIS FUNCTION DOES NOT CHANGE ANY STATE OF THE DEBUGEE
//This Function Decodes :
// BL     offset
// B      offset
// B.Cond offset
// CB[N]Z X<r> offset
// TB[N]Z X<r> offset

//Output of the Function are:
//offset - Offset from current PC to which control will go next
//WALK_TYPE

BOOL  NativeWalker::DecodePCRelativeBranchInst(PT_CONTEXT context, const PRD_TYPE& opcode, PCODE& offset, WALK_TYPE& walk)
{
#ifdef _DEBUG
    PCODE incomingoffset = offset;
    WALK_TYPE incomingwalk = walk;
#endif

    if ((opcode & 0x7C000000) == 0x14000000) // Decode B & BL
    {
        offset = (opcode & 0x03FFFFFF) << 2;
        // Sign extension
        if ((offset & 0x4000000)) //Check for 26'st bit
        {
            offset = offset | 0xFFFFFFFFF8000000;
        }

        if ((opcode & 0x80000000) != 0) //BL
        {
            walk = WALK_CALL;
            LOG((LF_CORDB, LL_INFO100000, "Arm64Walker::Decoded opcode: %x to BL %p \n", opcode, offset));
        }
        else
        {
            walk = WALK_BRANCH; //B
            LOG((LF_CORDB, LL_INFO100000, "Arm64Walker::Decoded opcode: %x to B %p \n", opcode, offset));
        }
        return TRUE;
    }

    //Conditional Branches
    _ASSERTE(context != NULL);
   

    if ((opcode & 0xFF000010) == 0x54000000) // B.cond
    {
        WORD cond = opcode & 0xF;
        bool result = false;
        switch (cond >> 1)
        {
        case 0x0:  result = (context->Cpsr & NZCV_Z) != 0; // EQ or NE
            break;
        case 0x1:  result = (context->Cpsr & NZCV_C) != 0; // CS or CC
            break;
        case 0x2:  result = (context->Cpsr & NZCV_N) != 0; // MI or PL
            break;
        case 0x3:  result = (context->Cpsr & NZCV_V) != 0; // VS or VC
            break;
        case 0x4:  result = ((context->Cpsr & NZCV_C) != 0) && ((context->Cpsr & NZCV_Z) == 0); // HI or LS
            break;
        case 0x5:  result = ((context->Cpsr & NZCV_N) >> NZCV_N_BIT) == ((context->Cpsr & NZCV_V) >> NZCV_V_BIT); // GE or LT
            break;
        case 0x6:  result = ((context->Cpsr & NZCV_N) >> NZCV_N_BIT) == ((context->Cpsr & NZCV_V) >> NZCV_V_BIT) && ((context->Cpsr & NZCV_Z) == 0); // GT or LE
            break;
        case 0x7:  result = true; // AL
            break;
        }

        if ((cond & 0x1) && (cond & 0xF) != 0) { result = !result; }

        if (result)
        {
            walk = WALK_BRANCH;
            offset = Expand19bitoffset(opcode); 
            LOG((LF_CORDB, LL_INFO100000, "Arm64Walker::Decoded opcode: %x to B.cond %p \n", opcode, offset));
        }
        else // NOP
        {
            walk = WALK_UNKNOWN;
            LOG((LF_CORDB, LL_INFO100000, "Arm64Walker::Decoded opcode: %x to B.cond but evaluated as NOP \n", opcode));
            offset = MAX_INSTRUCTION_LENGTH; 
        }
        
        return TRUE;

    }

    
    int RegNum       = opcode & 0x1F;
    PCODE RegContent = GetReg(context, RegNum);

    if ((opcode & 0xFE000000) == 0x34000000) // CBNZ || CBZ
    {
        bool result = false;

        if (!(opcode & 0x80000000)) //if sf == '1' the 64 else 32
        {
            RegContent = 0xFFFFFFFF & RegContent;  //zero the upper 32bit
        }

        if (opcode & 0x01000000) //CBNZ
        {
            result = RegContent != 0;
            LOG((LF_CORDB, LL_INFO100000, "Arm64Walker::Decoded opcode: %x to CBNZ X%d \n", opcode, RegNum));
        }
        else //CBZ
        {
            result = RegContent == 0;
            LOG((LF_CORDB, LL_INFO100000, "Arm64Walker::Decoded opcode: %x to CBZ X%d \n", opcode, RegNum));
        }
        
        if (result)
        {
            walk = WALK_BRANCH;
            offset = Expand19bitoffset(opcode);
            LOG((LF_CORDB, LL_INFO100000, "Arm64Walker::Decoded opcode: %x to CB[N]Z X%d %p \n", opcode, RegNum, offset));
        }
        else // NOP
        {
            walk = WALK_UNKNOWN;
            LOG((LF_CORDB, LL_INFO100000, "Arm64Walker::Decoded opcode: %x to B.cond but evaluated as NOP \n", opcode));
            offset = MAX_INSTRUCTION_LENGTH;
        }

        
        return TRUE;
    }
    if ((opcode & 0x7E000000) == 0x36000000)    // TBNZ || TBZ
    {
        bool result = false;
        int bit_pos = ((opcode >> 19) & 0x1F);

        if (opcode & 0x80000000)
        {
            bit_pos = bit_pos + 32;
        }

        PCODE bit_val = PCODE{ 1 } << bit_pos;
        if (opcode & 0x01000000) //TBNZ
        {
            result = (RegContent & bit_val) != 0;
            LOG((LF_CORDB, LL_INFO100000, "Arm64Walker::Decoded opcode: %x to TBNZ X%d \n", opcode, RegNum));
        }
        else //TBZ
        {
            result = (RegContent & bit_val) == 0;
            LOG((LF_CORDB, LL_INFO100000, "Arm64Walker::Decoded opcode: %x to CB[N]Z X%d \n", opcode, RegNum));
        }
        if (result)
        {
            walk = WALK_BRANCH;
            offset = ((opcode >> 5) & 0x3FFF) << 2; //imm14:00 -> 16 bits
            if (offset & 0x8000) //sign extension check for 16'th bit
            {
                offset = offset | 0xFFFFFFFFFFFF0000;
            }
            LOG((LF_CORDB, LL_INFO100000, "Arm64Walker::Decoded opcode: %x to TB[N]Z X%d %p \n", opcode, RegNum, offset));
        }
        else // NOP
        {
            walk = WALK_UNKNOWN;
            LOG((LF_CORDB, LL_INFO100000, "Arm64Walker::Decoded opcode: %x to B.cond but evaluated as NOP \n", opcode));
            offset = MAX_INSTRUCTION_LENGTH;
        }
       
        return TRUE;
    }
   
    _ASSERTE(offset == incomingoffset);
    _ASSERTE(walk == incomingwalk);
    return FALSE;
}

BOOL  NativeWalker::DecodeCallInst(const PRD_TYPE& opcode, int& RegNum, WALK_TYPE& walk)
{
    if ((opcode & 0xFF9FFC1F) == 0xD61F0000) // BR, BLR or RET -Unconditional Branch (register) instructions
    {

        RegNum = (opcode & 0x3E0) >> 5;


        short op = (opcode & 0x00600000) >> 21;  //Checking for 23 and 22 bits
        switch (op)
        {
        case 0: LOG((LF_CORDB, LL_INFO100000, "Arm64Walker::Decoded opcode: %x to BR X%d\n", opcode, RegNum));
            walk = WALK_BRANCH;
            break;
        case 1: LOG((LF_CORDB, LL_INFO100000, "Arm64Walker::Decoded opcode: %x to BLR X%d\n", opcode, RegNum));
            walk = WALK_CALL;
            break;
        case 2: LOG((LF_CORDB, LL_INFO100000, "Arm64Walker::Decoded opcode: %x to Ret X%d\n", opcode, RegNum));
            walk = WALK_RETURN;
            break;
        default:
            LOG((LF_CORDB, LL_INFO100000, "Arm64Walker::Simulate Unknown opcode: %x [Branch]  \n", opcode));
            _ASSERTE(!("Arm64Walker::Decoded Unknown opcode"));
        }

        return TRUE;
    }
        return FALSE;
}
#endif