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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
|
/*
Copyright (c) 2015 DataLab, s.l. <http://www.datalab.es>
This file is part of GlusterFS.
This file is licensed to you under your choice of the GNU Lesser
General Public License, version 3 or any later version (LGPLv3 or
later), or the GNU General Public License, version 2 (GPLv2), in all
cases as published by the Free Software Foundation.
*/
#include <inttypes.h>
#include <string.h>
#include <errno.h>
#include "ec-code-intel.h"
static void
ec_code_intel_init(ec_code_intel_t *intel)
{
memset(intel, 0, sizeof(ec_code_intel_t));
}
static void
ec_code_intel_prefix(ec_code_intel_t *intel, uint8_t prefix)
{
intel->prefix.data[intel->prefix.bytes++] = prefix;
}
static void
ec_code_intel_rex(ec_code_intel_t *intel, gf_boolean_t w)
{
gf_boolean_t present = _gf_false;
if (w) {
intel->rex.w = 1;
present = _gf_true;
}
if (intel->modrm.present) {
if (intel->modrm.reg > 7) {
intel->modrm.reg &= 7;
intel->rex.r = 1;
present = _gf_true;
}
if (intel->sib.present) {
if (intel->sib.index > 7) {
intel->sib.index &= 7;
intel->rex.x = 1;
present = _gf_true;
}
if (intel->sib.base > 7) {
intel->sib.base &= 7;
intel->rex.b = 1;
present = _gf_true;
}
} else if (intel->modrm.rm > 7) {
intel->modrm.rm &= 7;
intel->rex.b = 1;
present = _gf_true;
}
} else if (intel->reg > 7) {
intel->reg &= 7;
intel->rex.b = 1;
present = _gf_true;
}
intel->rex.present = present;
}
static void
ec_code_intel_vex(ec_code_intel_t *intel, gf_boolean_t w, gf_boolean_t l,
ec_code_vex_opcode_t opcode, ec_code_vex_prefix_t prefix,
uint32_t reg)
{
ec_code_intel_rex(intel, w);
if (((intel->rex.w == 1) ||
(intel->rex.x == 0) ||
(intel->rex.b == 0)) ||
((opcode != VEX_OPCODE_NONE) && (opcode != VEX_OPCODE_0F))) {
intel->rex.present = _gf_false;
intel->vex.bytes = 3;
intel->vex.data[0] = 0xC4;
intel->vex.data[1] = ((intel->rex.r << 7) | (intel->rex.x << 6) |
(intel->rex.b << 5) | opcode) ^ 0xE0;
intel->vex.data[2] = (intel->rex.w << 7) | ((~reg & 0x0F) << 3) |
(l ? 0x04 : 0x00) | prefix;
} else {
intel->vex.bytes = 2;
intel->vex.data[0] = 0xC5;
intel->vex.data[1] = (intel->rex.r << 7) | ((~reg & 0x0F) << 3) |
(l ? 0x04 : 0x00) | prefix;
}
}
static void
ec_code_intel_modrm_reg(ec_code_intel_t *intel, uint32_t rm, uint32_t reg)
{
intel->modrm.present = _gf_true;
intel->modrm.mod = 3;
intel->modrm.rm = rm;
intel->modrm.reg = reg;
}
static void
ec_code_intel_modrm_mem(ec_code_intel_t *intel, uint32_t reg,
ec_code_intel_reg_t base, ec_code_intel_reg_t index,
uint32_t scale, int32_t offset)
{
if (index == REG_SP) {
intel->invalid = _gf_true;
return;
}
if ((index != REG_NULL) && (scale != 1) && (scale != 2) && (scale != 4) &&
(scale != 8)) {
intel->invalid = _gf_true;
return;
}
scale >>= 1;
if (scale == 4) {
scale = 3;
}
intel->modrm.present = _gf_true;
intel->modrm.reg = reg;
intel->offset.value = offset;
if ((offset == 0) && (base != REG_BP)) {
intel->modrm.mod = 0;
intel->offset.bytes = 0;
} else if ((offset >= -128) && (offset <= 127)) {
intel->modrm.mod = 1;
intel->offset.bytes = 1;
} else {
intel->modrm.mod = 2;
intel->offset.bytes = 4;
}
intel->modrm.rm = base;
if ((index != REG_NULL) || (base == REG_SP)) {
intel->modrm.rm = 4;
intel->sib.present = _gf_true;
intel->sib.index = index;
if (index == REG_NULL) {
intel->sib.index = 4;
}
intel->sib.scale = scale;
intel->sib.base = base;
if (base == REG_NULL) {
intel->sib.base = 5;
intel->modrm.mod = 0;
intel->offset.bytes = 4;
}
} else if (base == REG_NULL) {
intel->modrm.mod = 0;
intel->modrm.rm = 5;
intel->offset.bytes = 4;
}
}
static void
ec_code_intel_op_1(ec_code_intel_t *intel, uint8_t opcode, uint32_t reg)
{
intel->reg = reg;
intel->opcode.bytes = 1;
intel->opcode.data[0] = opcode;
}
static void
ec_code_intel_op_2(ec_code_intel_t *intel, uint8_t opcode1, uint8_t opcode2,
uint32_t reg)
{
intel->reg = reg;
intel->opcode.bytes = 2;
intel->opcode.data[0] = opcode1;
intel->opcode.data[1] = opcode2;
}
static void
ec_code_intel_immediate_1(ec_code_intel_t *intel, uint32_t value)
{
intel->immediate.bytes = 1;
intel->immediate.value = value;
}
static void
ec_code_intel_immediate_2(ec_code_intel_t *intel, uint32_t value)
{
intel->immediate.bytes = 2;
intel->immediate.value = value;
}
static void
ec_code_intel_immediate_4(ec_code_intel_t *intel, uint32_t value)
{
intel->immediate.bytes = 4;
intel->immediate.value = value;
}
static void
ec_code_intel_emit(ec_code_builder_t *builder, ec_code_intel_t *intel)
{
uint8_t insn[15];
uint32_t i, count;
if (intel->invalid) {
ec_code_error(builder, EINVAL);
return;
}
count = 0;
for (i = 0; i < intel->prefix.bytes; i++) {
insn[count++] = intel->prefix.data[i];
}
for (i = 0; i < intel->vex.bytes; i++) {
insn[count++] = intel->vex.data[i];
}
if (intel->rex.present) {
insn[count++] = 0x40 |
(intel->rex.w << 3) |
(intel->rex.r << 2) |
(intel->rex.x << 1) |
(intel->rex.b << 0);
}
for (i = 0; i < intel->opcode.bytes; i++) {
insn[count++] = intel->opcode.data[i];
}
if (intel->modrm.present) {
insn[count++] = (intel->modrm.mod << 6) |
(intel->modrm.reg << 3) |
(intel->modrm.rm << 0);
if (intel->sib.present) {
insn[count++] = (intel->sib.scale << 6) |
(intel->sib.index << 3) |
(intel->sib.base << 0);
}
}
for (i = 0; i < intel->offset.bytes; i++) {
insn[count++] = intel->offset.data[i];
}
for (i = 0; i < intel->immediate.bytes; i++) {
insn[count++] = intel->immediate.data[i];
}
ec_code_emit(builder, insn, count);
}
void
ec_code_intel_op_push_r(ec_code_builder_t *builder, ec_code_intel_reg_t reg)
{
ec_code_intel_t intel;
ec_code_intel_init(&intel);
ec_code_intel_op_1(&intel, 0x50 | (reg & 7), reg);
ec_code_intel_rex(&intel, _gf_false);
ec_code_intel_emit(builder, &intel);
}
void
ec_code_intel_op_pop_r(ec_code_builder_t *builder, ec_code_intel_reg_t reg)
{
ec_code_intel_t intel;
ec_code_intel_init(&intel);
ec_code_intel_op_1(&intel, 0x58 | (reg & 7), reg);
ec_code_intel_rex(&intel, _gf_false);
ec_code_intel_emit(builder, &intel);
}
void
ec_code_intel_op_ret(ec_code_builder_t *builder, uint32_t size)
{
ec_code_intel_t intel;
ec_code_intel_init(&intel);
if (size == 0) {
ec_code_intel_op_1(&intel, 0xC3, 0);
} else {
ec_code_intel_immediate_2(&intel, size);
ec_code_intel_op_1(&intel, 0xC2, 0);
}
ec_code_intel_rex(&intel, _gf_false);
ec_code_intel_emit(builder, &intel);
}
void
ec_code_intel_op_mov_r2r(ec_code_builder_t *builder, ec_code_intel_reg_t src,
ec_code_intel_reg_t dst)
{
ec_code_intel_t intel;
ec_code_intel_init(&intel);
ec_code_intel_modrm_reg(&intel, dst, src);
ec_code_intel_op_1(&intel, 0x89, 0);
ec_code_intel_rex(&intel, _gf_true);
ec_code_intel_emit(builder, &intel);
}
void
ec_code_intel_op_mov_r2m(ec_code_builder_t *builder, ec_code_intel_reg_t src,
ec_code_intel_reg_t base, ec_code_intel_reg_t index,
uint32_t scale, int32_t offset)
{
ec_code_intel_t intel;
ec_code_intel_init(&intel);
ec_code_intel_modrm_mem(&intel, src, base, index, scale, offset);
ec_code_intel_op_1(&intel, 0x89, 0);
ec_code_intel_rex(&intel, _gf_true);
ec_code_intel_emit(builder, &intel);
}
void
ec_code_intel_op_mov_m2r(ec_code_builder_t *builder, ec_code_intel_reg_t base,
ec_code_intel_reg_t index, uint32_t scale,
int32_t offset, ec_code_intel_reg_t dst)
{
ec_code_intel_t intel;
ec_code_intel_init(&intel);
ec_code_intel_modrm_mem(&intel, dst, base, index, scale, offset);
ec_code_intel_op_1(&intel, 0x8B, 0);
ec_code_intel_rex(&intel, _gf_true);
ec_code_intel_emit(builder, &intel);
}
void
ec_code_intel_op_xor_r2r(ec_code_builder_t *builder, ec_code_intel_reg_t src,
ec_code_intel_reg_t dst)
{
ec_code_intel_t intel;
ec_code_intel_init(&intel);
ec_code_intel_modrm_reg(&intel, dst, src);
ec_code_intel_op_1(&intel, 0x31, 0);
ec_code_intel_rex(&intel, _gf_true);
ec_code_intel_emit(builder, &intel);
}
void
ec_code_intel_op_xor_m2r(ec_code_builder_t *builder, ec_code_intel_reg_t base,
ec_code_intel_reg_t index, uint32_t scale,
int32_t offset, ec_code_intel_reg_t dst)
{
ec_code_intel_t intel;
ec_code_intel_init(&intel);
ec_code_intel_modrm_mem(&intel, dst, base, index, scale, offset);
ec_code_intel_op_1(&intel, 0x33, 0);
ec_code_intel_rex(&intel, _gf_true);
ec_code_intel_emit(builder, &intel);
}
void
ec_code_intel_op_add_i2r(ec_code_builder_t *builder, int32_t value,
ec_code_intel_reg_t reg)
{
ec_code_intel_t intel;
ec_code_intel_init(&intel);
if ((value >= -128) && (value < 128)) {
ec_code_intel_modrm_reg(&intel, reg, 0);
ec_code_intel_op_1(&intel, 0x83, 0);
ec_code_intel_immediate_1(&intel, value);
} else {
if (reg == REG_AX) {
ec_code_intel_op_1(&intel, 0x05, reg);
} else {
ec_code_intel_modrm_reg(&intel, reg, 0);
ec_code_intel_op_1(&intel, 0x81, 0);
}
ec_code_intel_immediate_4(&intel, value);
}
ec_code_intel_rex(&intel, _gf_true);
ec_code_intel_emit(builder, &intel);
}
void
ec_code_intel_op_test_i2r(ec_code_builder_t *builder, uint32_t value,
ec_code_intel_reg_t reg)
{
ec_code_intel_t intel;
ec_code_intel_init(&intel);
if (reg == REG_AX) {
ec_code_intel_op_1(&intel, 0xA9, reg);
} else {
ec_code_intel_modrm_reg(&intel, reg, 0);
ec_code_intel_op_1(&intel, 0xF7, 0);
}
ec_code_intel_immediate_4(&intel, value);
ec_code_intel_rex(&intel, _gf_true);
ec_code_intel_emit(builder, &intel);
}
void
ec_code_intel_op_jne(ec_code_builder_t *builder, uint32_t address)
{
ec_code_intel_t intel;
int32_t rel;
ec_code_intel_init(&intel);
rel = address - builder->address - 2;
if ((rel >= -128) && (rel < 128)) {
ec_code_intel_op_1(&intel, 0x75, 0);
ec_code_intel_immediate_1(&intel, rel);
} else {
rel -= 4;
ec_code_intel_op_2(&intel, 0x0F, 0x85, 0);
ec_code_intel_immediate_4(&intel, rel);
}
ec_code_intel_rex(&intel, _gf_false);
ec_code_intel_emit(builder, &intel);
}
void
ec_code_intel_op_mov_sse2sse(ec_code_builder_t *builder, uint32_t src,
uint32_t dst)
{
ec_code_intel_t intel;
ec_code_intel_init(&intel);
ec_code_intel_prefix(&intel, 0x66);
ec_code_intel_modrm_reg(&intel, src, dst);
ec_code_intel_op_2(&intel, 0x0F, 0x6F, 0);
ec_code_intel_rex(&intel, _gf_false);
ec_code_intel_emit(builder, &intel);
}
void
ec_code_intel_op_mov_sse2m(ec_code_builder_t *builder, uint32_t src,
ec_code_intel_reg_t base, ec_code_intel_reg_t index,
uint32_t scale, int32_t offset)
{
ec_code_intel_t intel;
ec_code_intel_init(&intel);
ec_code_intel_prefix(&intel, 0x66);
ec_code_intel_modrm_mem(&intel, src, base, index, scale, offset);
ec_code_intel_op_2(&intel, 0x0F, 0x7F, 0);
ec_code_intel_rex(&intel, _gf_false);
ec_code_intel_emit(builder, &intel);
}
void
ec_code_intel_op_mov_m2sse(ec_code_builder_t *builder,
ec_code_intel_reg_t base, ec_code_intel_reg_t index,
uint32_t scale, int32_t offset, uint32_t dst)
{
ec_code_intel_t intel;
ec_code_intel_init(&intel);
ec_code_intel_prefix(&intel, 0x66);
ec_code_intel_modrm_mem(&intel, dst, base, index, scale, offset);
ec_code_intel_op_2(&intel, 0x0F, 0x6F, 0);
ec_code_intel_rex(&intel, _gf_false);
ec_code_intel_emit(builder, &intel);
}
void
ec_code_intel_op_xor_sse2sse(ec_code_builder_t *builder, uint32_t src,
uint32_t dst)
{
ec_code_intel_t intel;
ec_code_intel_init(&intel);
ec_code_intel_prefix(&intel, 0x66);
ec_code_intel_modrm_reg(&intel, src, dst);
ec_code_intel_op_2(&intel, 0x0F, 0xEF, 0);
ec_code_intel_rex(&intel, _gf_false);
ec_code_intel_emit(builder, &intel);
}
void
ec_code_intel_op_xor_m2sse(ec_code_builder_t *builder,
ec_code_intel_reg_t base, ec_code_intel_reg_t index,
uint32_t scale, int32_t offset, uint32_t dst)
{
ec_code_intel_t intel;
ec_code_intel_init(&intel);
ec_code_intel_prefix(&intel, 0x66);
ec_code_intel_modrm_mem(&intel, dst, base, index, scale, offset);
ec_code_intel_op_2(&intel, 0x0F, 0xEF, 0);
ec_code_intel_rex(&intel, _gf_false);
ec_code_intel_emit(builder, &intel);
}
void
ec_code_intel_op_mov_avx2avx(ec_code_builder_t *builder, uint32_t src,
uint32_t dst)
{
ec_code_intel_t intel;
ec_code_intel_init(&intel);
ec_code_intel_modrm_reg(&intel, src, dst);
ec_code_intel_op_1(&intel, 0x6F, 0);
ec_code_intel_vex(&intel, _gf_false, _gf_true, VEX_OPCODE_0F,
VEX_PREFIX_66, VEX_REG_NONE);
ec_code_intel_emit(builder, &intel);
}
void
ec_code_intel_op_mov_avx2m(ec_code_builder_t *builder, uint32_t src,
ec_code_intel_reg_t base, ec_code_intel_reg_t index,
uint32_t scale, int32_t offset)
{
ec_code_intel_t intel;
ec_code_intel_init(&intel);
ec_code_intel_modrm_mem(&intel, src, base, index, scale, offset);
ec_code_intel_op_1(&intel, 0x7F, 0);
ec_code_intel_vex(&intel, _gf_false, _gf_true, VEX_OPCODE_0F,
VEX_PREFIX_66, VEX_REG_NONE);
ec_code_intel_emit(builder, &intel);
}
void
ec_code_intel_op_mov_m2avx(ec_code_builder_t *builder,
ec_code_intel_reg_t base, ec_code_intel_reg_t index,
uint32_t scale, int32_t offset, uint32_t dst)
{
ec_code_intel_t intel;
ec_code_intel_init(&intel);
ec_code_intel_modrm_mem(&intel, dst, base, index, scale, offset);
ec_code_intel_op_1(&intel, 0x6F, 0);
ec_code_intel_vex(&intel, _gf_false, _gf_true, VEX_OPCODE_0F,
VEX_PREFIX_66, VEX_REG_NONE);
ec_code_intel_emit(builder, &intel);
}
void
ec_code_intel_op_xor_avx2avx(ec_code_builder_t *builder, uint32_t src,
uint32_t dst)
{
ec_code_intel_t intel;
ec_code_intel_init(&intel);
ec_code_intel_modrm_reg(&intel, src, dst);
ec_code_intel_op_1(&intel, 0xEF, 0);
ec_code_intel_vex(&intel, _gf_false, _gf_true, VEX_OPCODE_0F,
VEX_PREFIX_66, dst);
ec_code_intel_emit(builder, &intel);
}
void
ec_code_intel_op_xor_m2avx(ec_code_builder_t *builder,
ec_code_intel_reg_t base, ec_code_intel_reg_t index,
uint32_t scale, int32_t offset, uint32_t dst)
{
ec_code_intel_t intel;
ec_code_intel_init(&intel);
ec_code_intel_modrm_mem(&intel, dst, base, index, scale, offset);
ec_code_intel_op_1(&intel, 0xEF, 0);
ec_code_intel_vex(&intel, _gf_false, _gf_true, VEX_OPCODE_0F,
VEX_PREFIX_66, dst);
ec_code_intel_emit(builder, &intel);
}
|