about summary refs log tree commit diff stats
path: root/src/emu/x64run67.c
blob: f17e5e6dfddf316e569bc50cc51d6cb0b4f5ca37 (plain) (blame)
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
#define _GNU_SOURCE
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>

#include "debug.h"
#include "box64stack.h"
#include "x64emu.h"
#include "x64run.h"
#include "x64emu_private.h"
#include "x64run_private.h"
#include "x64primop.h"
#include "x64trace.h"
#include "x87emu_private.h"
#include "box64context.h"
#include "bridge.h"

#include "modrm.h"

int Run67(x64emu_t *emu, rex_t rex, int rep)
{
    uint8_t opcode;
    uint8_t nextop;
    int8_t tmp8s;
    uint8_t tmp8u;
    uint32_t tmp32u;
    int32_t tmp32s;
    uint64_t tmp64u;
    reg64_t *oped, *opgd;

    opcode = F8;

    while(opcode==0x67)
        opcode = F8;

    // REX prefix before the 67 are ignored
    rex.rex = 0;
    while(opcode>=0x40 && opcode<=0x4f) {
        rex.rex = opcode;
        opcode = F8;
    }
    while((opcode==0xF2) || (opcode==0xF3)) {
        rep = opcode-0xF1;
        opcode = F8;
    }

    switch(opcode) {
    #define GO(B, OP)                                   \
    case B+0:                                           \
        nextop = F8;                                    \
        GETEB32(0);                                     \
        GETGB;                                          \
        EB->byte[0] = OP##8(emu, EB->byte[0], GB);      \
        break;                                          \
    case B+1:                                           \
        nextop = F8;                                    \
        GETED32(0);                                     \
        GETGD;                                          \
        if(rex.w)                                       \
            ED->q[0] = OP##64(emu, ED->q[0], GD->q[0]); \
        else {                                          \
            if(MODREG)                                  \
                ED->q[0] = OP##32(emu, ED->dword[0], GD->dword[0]);     \
            else                                                        \
                ED->dword[0] = OP##32(emu, ED->dword[0], GD->dword[0]); \
        }                                               \
        break;                                          \
    case B+2:                                           \
        nextop = F8;                                    \
        GETEB32(0);                                     \
        GETGB;                                          \
        GB = OP##8(emu, GB, EB->byte[0]);               \
        break;                                          \
    case B+3:                                           \
        nextop = F8;                                    \
        GETED32(0);                                     \
        GETGD;                                          \
        if(rex.w)                                       \
            GD->q[0] = OP##64(emu, GD->q[0], ED->q[0]); \
        else                                            \
            GD->q[0] = OP##32(emu, GD->dword[0], ED->dword[0]); \
        break;                                          \
    case B+4:                                           \
        R_AL = OP##8(emu, R_AL, F8);                    \
        break;                                          \
    case B+5:                                           \
        if(rex.w)                                       \
            R_RAX = OP##64(emu, R_RAX, F32S64);         \
        else                                            \
            R_RAX = OP##32(emu, R_EAX, F32);            \
        break;

    GO(0x00, add)                   /* ADD 0x00 -> 0x05 */
    GO(0x08, or)                    /*  OR 0x08 -> 0x0D */
    case 0x0F:
        return Run670F(emu, rex, rep);
    GO(0x10, adc)                   /* ADC 0x10 -> 0x15 */
    GO(0x18, sbb)                   /* SBB 0x18 -> 0x1D */
    GO(0x20, and)                   /* AND 0x20 -> 0x25 */
    GO(0x28, sub)                   /* SUB 0x28 -> 0x2D */
    GO(0x30, xor)                   /* XOR 0x30 -> 0x35 */
    #undef GO

    case 0x66:
        return Run6766(emu, rex, rep);

    case 0x80:                      /* GRP Eb,Ib */
        nextop = F8;
        GETEB32(1);
        tmp8u = F8;
        switch((nextop>>3)&7) {
            case 0: EB->byte[0] = add8(emu, EB->byte[0], tmp8u); break;
            case 1: EB->byte[0] =  or8(emu, EB->byte[0], tmp8u); break;
            case 2: EB->byte[0] = adc8(emu, EB->byte[0], tmp8u); break;
            case 3: EB->byte[0] = sbb8(emu, EB->byte[0], tmp8u); break;
            case 4: EB->byte[0] = and8(emu, EB->byte[0], tmp8u); break;
            case 5: EB->byte[0] = sub8(emu, EB->byte[0], tmp8u); break;
            case 6: EB->byte[0] = xor8(emu, EB->byte[0], tmp8u); break;
            case 7:               cmp8(emu, EB->byte[0], tmp8u); break;
        }
        break;
    case 0x81:                      /* GRP Ed,Id */
    case 0x83:                      /* GRP Ed,Ib */
        nextop = F8;
        GETED32((opcode==0x81)?4:1);
        if(opcode==0x81) {
            tmp32s = F32S;
        } else {
            tmp32s = F8S;
        }
        if(rex.w) {
            tmp64u = (uint64_t)(int64_t)tmp32s;
            switch((nextop>>3)&7) {
                case 0: ED->q[0] = add64(emu, ED->q[0], tmp64u); break;
                case 1: ED->q[0] =  or64(emu, ED->q[0], tmp64u); break;
                case 2: ED->q[0] = adc64(emu, ED->q[0], tmp64u); break;
                case 3: ED->q[0] = sbb64(emu, ED->q[0], tmp64u); break;
                case 4: ED->q[0] = and64(emu, ED->q[0], tmp64u); break;
                case 5: ED->q[0] = sub64(emu, ED->q[0], tmp64u); break;
                case 6: ED->q[0] = xor64(emu, ED->q[0], tmp64u); break;
                case 7:            cmp64(emu, ED->q[0], tmp64u); break;
            }
        } else {
            tmp32u = (uint32_t)tmp32s;
            if(MODREG)
                switch((nextop>>3)&7) {
                    case 0: ED->q[0] = add32(emu, ED->dword[0], tmp32u); break;
                    case 1: ED->q[0] =  or32(emu, ED->dword[0], tmp32u); break;
                    case 2: ED->q[0] = adc32(emu, ED->dword[0], tmp32u); break;
                    case 3: ED->q[0] = sbb32(emu, ED->dword[0], tmp32u); break;
                    case 4: ED->q[0] = and32(emu, ED->dword[0], tmp32u); break;
                    case 5: ED->q[0] = sub32(emu, ED->dword[0], tmp32u); break;
                    case 6: ED->q[0] = xor32(emu, ED->dword[0], tmp32u); break;
                    case 7:            cmp32(emu, ED->dword[0], tmp32u); break;
                }
            else
                switch((nextop>>3)&7) {
                    case 0: ED->dword[0] = add32(emu, ED->dword[0], tmp32u); break;
                    case 1: ED->dword[0] =  or32(emu, ED->dword[0], tmp32u); break;
                    case 2: ED->dword[0] = adc32(emu, ED->dword[0], tmp32u); break;
                    case 3: ED->dword[0] = sbb32(emu, ED->dword[0], tmp32u); break;
                    case 4: ED->dword[0] = and32(emu, ED->dword[0], tmp32u); break;
                    case 5: ED->dword[0] = sub32(emu, ED->dword[0], tmp32u); break;
                    case 6: ED->dword[0] = xor32(emu, ED->dword[0], tmp32u); break;
                    case 7:                cmp32(emu, ED->dword[0], tmp32u); break;
                }
        }
        break;

    case 0x88:                      /* MOV Eb,Gb */
        nextop = F8;
        GETEB32(0);
        GETGB;
        EB->byte[0] = GB;
        break;
    case 0x89:                    /* MOV Ed,Gd */
        nextop = F8;
        GETED32(0);
        GETGD;
        if(rex.w) {
            ED->q[0] = GD->q[0];
        } else {
            //if ED is a reg, than the opcode works like movzx
            if(MODREG)
                ED->q[0] = GD->dword[0];
            else
                ED->dword[0] = GD->dword[0];
        }
        break;
    case 0x8A:                      /* MOV Gb,Eb */
        nextop = F8;
        GETEB32(0);
        GETGB;
        GB = EB->byte[0];
        break;
    case 0x8B:                      /* MOV Gd,Ed */
        nextop = F8;
        GETED32(0);
        GETGD;
        if(rex.w)
            GD->q[0] = ED->q[0];
        else
            GD->q[0] = ED->dword[0];
        break;

    case 0x8D:                      /* LEA Gd,M */
        nextop = F8;
        GETED32(0);
        GETGD;
        if(rex.w)
            GD->q[0] = (uint64_t)ED;
        else
            GD->q[0] = ((uintptr_t)ED)&0xffffffff;
        break;

    case 0xC1:                      /* GRP2 Ed,Ib */
        nextop = F8;
        GETED32(1);
        tmp8u = F8/* & 0x1f*/; // masking done in each functions
        if(rex.w) {
            switch((nextop>>3)&7) {
                case 0: ED->q[0] = rol64(emu, ED->q[0], tmp8u); break;
                case 1: ED->q[0] = ror64(emu, ED->q[0], tmp8u); break;
                case 2: ED->q[0] = rcl64(emu, ED->q[0], tmp8u); break;
                case 3: ED->q[0] = rcr64(emu, ED->q[0], tmp8u); break;
                case 4:
                case 6: ED->q[0] = shl64(emu, ED->q[0], tmp8u); break;
                case 5: ED->q[0] = shr64(emu, ED->q[0], tmp8u); break;
                case 7: ED->q[0] = sar64(emu, ED->q[0], tmp8u); break;
            }
        } else {
            if(MODREG)
                switch((nextop>>3)&7) {
                    case 0: ED->q[0] = rol32(emu, ED->dword[0], tmp8u); break;
                    case 1: ED->q[0] = ror32(emu, ED->dword[0], tmp8u); break;
                    case 2: ED->q[0] = rcl32(emu, ED->dword[0], tmp8u); break;
                    case 3: ED->q[0] = rcr32(emu, ED->dword[0], tmp8u); break;
                    case 4:
                    case 6: ED->q[0] = shl32(emu, ED->dword[0], tmp8u); break;
                    case 5: ED->q[0] = shr32(emu, ED->dword[0], tmp8u); break;
                    case 7: ED->q[0] = sar32(emu, ED->dword[0], tmp8u); break;
                }
            else
                switch((nextop>>3)&7) {
                    case 0: ED->dword[0] = rol32(emu, ED->dword[0], tmp8u); break;
                    case 1: ED->dword[0] = ror32(emu, ED->dword[0], tmp8u); break;
                    case 2: ED->dword[0] = rcl32(emu, ED->dword[0], tmp8u); break;
                    case 3: ED->dword[0] = rcr32(emu, ED->dword[0], tmp8u); break;
                    case 4:
                    case 6: ED->dword[0] = shl32(emu, ED->dword[0], tmp8u); break;
                    case 5: ED->dword[0] = shr32(emu, ED->dword[0], tmp8u); break;
                    case 7: ED->dword[0] = sar32(emu, ED->dword[0], tmp8u); break;
                }
        }
        break;

    case 0xC7:                      /* MOV Ed,Id */
        nextop = F8;
        GETED32(4);
        if(rex.w)
            ED->q[0] = F32S64;
        else
            if(MODREG)
                ED->q[0] = F32;
            else
                ED->dword[0] = F32;
        break;

    case 0xE0:                      /* LOOPNZ */
        CHECK_FLAGS(emu);
        tmp8s = F8S;
        --R_ECX; // don't update flags
        if(R_ECX && !ACCESS_FLAG(F_ZF))
            R_RIP += tmp8s;
        break;
    case 0xE1:                      /* LOOPZ */
        CHECK_FLAGS(emu);
        tmp8s = F8S;
        --R_ECX; // don't update flags
        if(R_ECX && ACCESS_FLAG(F_ZF))
            R_RIP += tmp8s;
        break;
    case 0xE2:                      /* LOOP */
        tmp8s = F8S;
        --R_ECX; // don't update flags
        if(R_ECX)
            R_RIP += tmp8s;
        break;
    case 0xE3:              /* JECXZ Ib */
        tmp8s = F8S;
        if(!R_ECX)
            R_RIP += tmp8s;
        break;

    case 0xE8:                      /* CALL Id */
        tmp32s = F32S; // call is relative
        Push(emu, R_RIP);
        R_RIP += tmp32s;
        break;

    case 0xF7:                      /* GRP3 Ed(,Id) */
        nextop = F8;
        tmp8u = (nextop>>3)&7;
        GETED32((tmp8u<2)?4:0);
        if(rex.w) {
            switch(tmp8u) {
                case 0: 
                case 1:                 /* TEST Ed,Id */
                    tmp64u = F32S64;
                    test64(emu, ED->q[0], tmp64u);
                    break;
                case 2:                 /* NOT Ed */
                    ED->q[0] = not64(emu, ED->q[0]);
                    break;
                case 3:                 /* NEG Ed */
                    ED->q[0] = neg64(emu, ED->q[0]);
                    break;
                case 4:                 /* MUL RAX,Ed */
                    mul64_rax(emu, ED->q[0]);
                    break;
                case 5:                 /* IMUL RAX,Ed */
                    imul64_rax(emu, ED->q[0]);
                    break;
                case 6:                 /* DIV Ed */
                    div64(emu, ED->q[0]);
                    break;
                case 7:                 /* IDIV Ed */
                    idiv64(emu, ED->q[0]);
                    break;
            }
        } else {
            switch(tmp8u) {
                case 0: 
                case 1:                 /* TEST Ed,Id */
                    tmp32u = F32;
                    test32(emu, ED->dword[0], tmp32u);
                    break;
                case 2:                 /* NOT Ed */
                    if(MODREG)
                        ED->q[0] = not32(emu, ED->dword[0]);
                    else
                        ED->dword[0] = not32(emu, ED->dword[0]);
                    break;
                case 3:                 /* NEG Ed */
                    if(MODREG)
                        ED->q[0] = neg32(emu, ED->dword[0]);
                    else
                        ED->dword[0] = neg32(emu, ED->dword[0]);
                    break;
                case 4:                 /* MUL EAX,Ed */
                    mul32_eax(emu, ED->dword[0]);
                    emu->regs[_AX].dword[1] = 0;
                    break;
                case 5:                 /* IMUL EAX,Ed */
                    imul32_eax(emu, ED->dword[0]);
                    emu->regs[_AX].dword[1] = 0;
                    break;
                case 6:                 /* DIV Ed */
                    div32(emu, ED->dword[0]);
                    emu->regs[_AX].dword[1] = 0;
                    emu->regs[_DX].dword[1] = 0;
                    break;
                case 7:                 /* IDIV Ed */
                    idiv32(emu, ED->dword[0]);
                    emu->regs[_AX].dword[1] = 0;
                    emu->regs[_DX].dword[1] = 0;
                    break;
            }
        }
        break;
            
    default:
        return 1;
    }
    return 0;
}