forked from mirh/opemu-linux
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sse41.c
386 lines (312 loc) · 10.5 KB
/
sse41.c
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
#include "ssse3_priv.h"
#include <asm/fpu/types.h>
void blendpd(ssse3_t *this)
{
uint8_t imm = this->udo_imm->lval.ubyte;
uint64_t* temp1 = this->src.uint128;
uint64_t* temp2 = this->dst.uint128;
if (imm & 1) {
temp2 = temp1;
}
if ((imm & 2) > 1) {
temp2[1] = temp1[1];
}
this->res.uint128 = ((__uint128_t*) temp2);
}
void blendps(ssse3_t *this)
{
uint8_t imm = this->udo_imm->lval.ubyte;
uint32_t* temp1 = this->src.uint128;
uint32_t* temp2 = this->dst.uint128;
if (imm & 1) { //1st bit imm != 0
temp2 = temp1;
}
if ((imm & 2) > 1) { //2nd bit imm != 0
temp2[1] = temp1[1];
}
if ((imm & 4) > 3) { //3rd bit imm != 0
temp2[2] = temp1[2];
}
if ((imm & 8) > 7) { //4th bit imm != 0
temp2[3] = temp1[3];
}
this->res.uint128 = ((__uint128_t*) temp2);
}
void pblendw(ssse3_t *this)
{
uint8_t imm = this->udo_imm->lval.ubyte;
uint16_t* temp1 = this->src.uint128;
uint16_t* temp2 = this->dst.uint128;
if (imm & 1) { //1st bit imm != 0
temp2 = temp1;
}
if ((imm & 2) > 1) { //2nd bit imm != 0
temp2[1] = temp1[1];
}
if ((imm & 4) > 3) { //3rd bit imm != 0
temp2[2] = temp1[2];
}
if ((imm & 8) > 7) { //4th bit imm != 0
temp2[3] = temp1[3];
}
if ((imm & 16) > 15) {
temp2[4] = temp1[4];
}
if ((imm & 32) > 31) {
temp2[5] = temp1[5];
}
if ((imm & 64) > 63) {
temp2[6] = temp1[6];
}
if ((imm & 128) > 127) {
temp2[7] = temp1[7];
}
this->res.uint128 = ((__uint128_t*) temp2);
}
void pmovsxbw(ssse3_t *this)
{
int8_t* temp1 = this->src.uint128;
int16_t* temp2 = this->dst.uint128;
temp2[0] = temp1[0] > 127 ? 0xFF00 | temp1[0] : temp1[0];
temp2[1] = temp1[1] > 127 ? 0xFF00 | temp1[1] : temp1[1];
temp2[2] = temp1[2] > 127 ? 0xFF00 | temp1[2] : temp1[2];
temp2[3] = temp1[3] > 127 ? 0xFF00 | temp1[3] : temp1[3];
temp2[4] = temp1[4] > 127 ? 0xFF00 | temp1[4] : temp1[4];
temp2[5] = temp1[5] > 127 ? 0xFF00 | temp1[5] : temp1[5];
temp2[6] = temp1[6] > 127 ? 0xFF00 | temp1[6] : temp1[6];
temp2[7] = temp1[7] > 127 ? 0xFF00 | temp1[7] : temp1[7];
this->res.uint128 = ((__uint128_t*) temp2);
}
void pmovsxbd(ssse3_t *this)
{
uint8_t* temp1 = this->src.uint128;
uint32_t* temp2 = this->dst.uint128;
temp2[0] = temp1[0] > 127 ? 0xFFFF00 | temp1[0] : (uint32_t) temp1[0];
temp2[1] = temp1[1] > 127 ? 0xFFFF00 | temp1[1] : (uint32_t) temp1[1];
temp2[2] = temp1[2] > 127 ? 0xFFFF00 | temp1[2] : (uint32_t) temp1[2];
temp2[3] = temp1[3] > 127 ? 0xFFFF00 | temp1[3] : (uint32_t) temp1[3];
this->res.uint128 = ((__uint128_t*) temp2);
}
void pmovsxbq(ssse3_t *this)
{
uint8_t* temp1 = this->src.uint128;
uint64_t* temp2 = this->dst.uint128;
temp2[0] = temp1[0] > 127 ? 0xFFFFFFFFFFFFFF00 | temp1[0] : (uint64_t) temp1[0];
temp2[1] = temp1[1] > 127 ? 0xFFFFFFFFFFFFFF00 | temp1[1] : (uint64_t) temp1[1];
this->res.uint128 = ((__uint128_t*) temp2);
}
void pmovsxwd(ssse3_t *this)
{
uint16_t* temp1 = this->src.uint128;
uint32_t* temp2 = this->dst.uint128;
temp2[0] = temp1[0] > 32767 ? 0xFFFF0000 | temp1[0] : (uint32_t) temp1[0];
temp2[1] = temp1[1] > 32767 ? 0xFFFF0000 | temp1[1] : (uint32_t) temp1[1];
temp2[2] = temp1[2] > 32767 ? 0xFFFF0000 | temp1[2] : (uint32_t) temp1[2];
temp2[3] = temp1[3] > 32767 ? 0xFFFF0000 | temp1[3] : (uint32_t) temp1[3];
this->res.uint128 = ((__uint128_t*) temp2);
}
void pmovsxwq(ssse3_t *this)
{
uint16_t* temp1 = this->src.uint128;
uint64_t* temp2 = this->dst.uint128;
temp2[0] = temp1[0] > 32767 ? 0xFFFFFFFFFFFF0000 | temp1[0] : (uint64_t) temp1[0];
temp2[1] = temp1[1] > 32767 ? 0xFFFFFFFFFFFF0000 | temp1[1] : (uint64_t) temp1[1];
this->res.uint128 = ((__uint128_t*) temp2);
}
void pmovsxdq(ssse3_t *this)
{
uint32_t* temp1 = this->src.uint128;
uint64_t* temp2 = this->dst.uint128;
temp2[0] = temp1[0] > 0x7FFFFFFF ? 0xFFFFFFFF00000000 | temp1[0] : (uint64_t) temp1[0];
temp2[1] = temp1[1] > 0x7FFFFFFF ? 0xFFFFFFFF00000000 | temp1[1] : (uint64_t) temp1[1];
this->res.uint128 = ((__uint128_t*) temp2);
}
void pmovzxbw(ssse3_t *this)
{
int8_t* temp1 = this->src.uint128;
int16_t* temp2 = this->dst.uint128;
temp2[0] = temp1[0] > 127 ? 0xFF00 | temp1[0] : temp1[0];
temp2[1] = temp1[1] > 127 ? 0xFF00 | temp1[1] : temp1[1];
temp2[2] = temp1[2] > 127 ? 0xFF00 | temp1[2] : temp1[2];
temp2[3] = temp1[3] > 127 ? 0xFF00 | temp1[3] : temp1[3];
temp2[4] = temp1[4] > 127 ? 0xFF00 | temp1[4] : temp1[4];
temp2[5] = temp1[5] > 127 ? 0xFF00 | temp1[5] : temp1[5];
temp2[6] = temp1[6] > 127 ? 0xFF00 | temp1[6] : temp1[6];
temp2[7] = temp1[7] > 127 ? 0xFF00 | temp1[7] : temp1[7];
this->res.uint128 = ((__uint128_t*) temp2);
}
void pmovzxbd(ssse3_t *this)
{
uint8_t* temp1 = this->src.uint128;
uint32_t* temp2 = this->dst.uint128;
temp2[0] = temp1[0] > 127 ? 0xFFFF00 | temp1[0] : (uint32_t) temp1[0];
temp2[1] = temp1[1] > 127 ? 0xFFFF00 | temp1[1] : (uint32_t) temp1[1];
temp2[2] = temp1[2] > 127 ? 0xFFFF00 | temp1[2] : (uint32_t) temp1[2];
temp2[3] = temp1[3] > 127 ? 0xFFFF00 | temp1[3] : (uint32_t) temp1[3];
this->res.uint128 = ((__uint128_t*) temp2);
}
void pmovzxbq(ssse3_t *this)
{
uint8_t* temp1 = this->src.uint128;
uint64_t* temp2 = this->dst.uint128;
temp2[0] = 0x00000000000000FF & temp1[0];
temp2[1] = 0x00000000000000FF & temp1[1];
this->res.uint128 = ((__uint128_t*) temp2);
}
void pmovzxwd(ssse3_t *this)
{
uint16_t* temp1 = this->src.uint128;
uint32_t* temp2 = this->dst.uint128;
temp2[0] = 0x0000FFFF & temp1[0];
temp2[1] = 0x0000FFFF & temp1[1];
temp2[2] = 0x0000FFFF & temp1[2];
temp2[3] = 0x0000FFFF & temp1[3];
this->res.uint128 = ((__uint128_t*) temp2);
}
void pmovzxwq(ssse3_t *this)
{
uint16_t* temp1 = this->src.uint128;
uint64_t* temp2 = this->dst.uint128;
temp2[0] = 0x000000000000FFFF & temp1[0];
temp2[1] = 0x000000000000FFFF & temp1[1];
this->res.uint128 = ((__uint128_t*) temp2);
}
void pmovzxdq(ssse3_t *this)
{
uint32_t* temp1 = this->src.uint128;
uint64_t* temp2 = this->dst.uint128;
temp2[0] = 0x00000000FFFFFFFF & temp1[0];
temp2[1] = 0x00000000FFFFFFFF & temp1[1];
this->res.uint128 = ((__uint128_t*) temp2);
}
void pextrb(ssse3_t *this)
{
uint8_t sel = this->udo_imm->lval.ubyte & 0xF;
uint8_t temp1 = this->src.uint8[sel];
uint8_t islongmode = is_saved_state64(this->op_obj->state);
if (this->udo_dst->type == UD_OP_MEM) {
this->res.uint8[0] = temp1;
printk("pextrb this->udo_dst->type == UD_OP_MEM this->res.uint8[0]: %hhu", temp1);
}
else if (islongmode && this->udo_dst->size == 64) {
this->res.uint64[0] = 0;
this->res.uint8[0] = temp1;
printk("pextrb islongmode && this->udo_dst->size == 64 this->res.uint64[0]: %hhu", temp1);
}
else {
this->res.uint32[0] = 0;
this->res.uint8[0] = temp1;
printk("pextrb this->res.uint32[0]: %hhu", temp1);
}
printk("OPEMU: %s\n", ud_insn_asm(this->op_obj->ud_obj));
}
void pextrd(ssse3_t *this)
{
uint8_t sel = this->udo_imm->lval.ubyte & 3;
this->res.uint32[0] = 0;
this->res.uint32[0] = this->src.uint32[sel];
//printk("temp1: %hhu", this->res.uint64[0]);
//printk("2temp1: %hhu", this->res.uint128);
printk("OPEMU: pextrd %s\n", ud_insn_asm(this->op_obj->ud_obj));
//printk("e %u", this->res.uint64[0]);
}
void pextrq(ssse3_t *this)
{
uint8_t sel = this->udo_imm->lval.ubyte & 1;
uint64_t temp1 = this->src.uint64[sel];
uint8_t islongmode = is_saved_state64(this->op_obj->state);
if (this->udo_dst->type == UD_OP_MEM) {
this->res.uint128 = temp1;
printk("pextrq this->udo_dst->type == UD_OP_MEM this->res.uint128: %hhu", temp1);
}
else if (islongmode && this->udo_dst->size == 128) {
this->res.uint128 = 0;
this->res.uint128 = temp1;
printk("pextrq islongmode && this->udo_dst->size == 128 this->res.uint128: %hhu", temp1);
}
else if (islongmode && this->udo_dst->size == 64) {
this->res.uint64[0] = 0;
this->res.uint64[0] = temp1;
//printk("pextrq islongmode && this->udo_dst->size == 64 this->res.uint64[0]: %hhu", temp1);
}
else {
this->res.uint32[0] = 0;
this->res.uint32[0] = temp1;
printk("pextrq this->res.uint32[0]: %hhu", temp1);
}
//printk("OPEMU: %s\n", ud_insn_asm(this->op_obj->ud_obj));
}
void roundss(ssse3_t *this)
{
kernel_fpu_begin();
uint8_t imm = this->udo_imm->lval.ubyte;
int rc;
int msi = (imm >> 2) & 1;
if (msi == 0) {
rc = imm & 3;
} else {
//get mxcsr round control
rc = getmxcsr();
}
this->res.fa32[0] = round_fp32(this->src.fa32[0], rc);
//printk("roundss this->res.uint32[0]: %hhu", this->res.uint32[0]);
//printk("roundss this->res.uint32[1]: %hhu", this->res.uint32[1]);
//printk("roundss this->res.uint32[2]: %hhu", this->res.uint32[2]);
//printk("roundss this->res.uint32[3]: %hhu", this->res.uint32[3]);
//printk("roundss temp1: %hhu", temp1);
//printk("roundss this->src.uint32[0]: %hhu", this->src.uint32[0]);
kernel_fpu_end();
}
void ptest(ssse3_t *this)
{
struct pt_regs *regs;
uint64_t FLAGS;
FLAGS = regs->flags;
sse_reg_t AND1, AND2;
AND1.uint128 = this->src.uint128 & this->dst.uint128;
AND2.uint128 = this->src.uint128 & ~(this->dst.uint128);
if (AND1.uint128 == 0) {
FLAGS |= 0x00000040; //set ZF = 1
} else {
FLAGS = (FLAGS | 0x00000040) ^ 0x00000040; //set ZF = 0
}
if (AND2.uint128 == 0) {
FLAGS |= 0x00000001; //set CF = 1
} else {
FLAGS = (FLAGS | 0x00000001) ^ 0x00000001; //set CF = 0
}
FLAGS = (FLAGS | 0x00000800) ^ 0x00000800; //set OF = 0
FLAGS = (FLAGS | 0x00000010) ^ 0x00000010; //set AF = 0
FLAGS = (FLAGS | 0x00000080) ^ 0x00000080; //set SF = 0
FLAGS = (FLAGS | 0x00000004) ^ 0x00000004; //set PF = 0
regs->flags = FLAGS;
}
void pinsrb(ssse3_t *this)
{
uint8_t sel = this->udo_imm->lval.ubyte & 0xF;
//uint32_t mask = 0xFFFFFFFF << (sel * 32);
//printk("1temp1: %hhu", this->dst.uint128);
//uint32_t res = &this->res.uint128;
this->dst.uint8[sel] = this->src.uint8[0];
this->res.uint128 = this->dst.uint128;
printk("OPEMU: pinsrb %s\n", ud_insn_asm(this->op_obj->ud_obj));
}
void pinsrd(ssse3_t *this)
{
uint8_t sel = this->udo_imm->lval.ubyte & 3;
//uint32_t mask = 0xFFFFFFFF << (sel * 32);
//printk("1temp1: %hhu", this->dst.uint128);
//uint32_t res = &this->res.uint128;
__uint128_t mask = 0xFFFFFFFF << (sel * 32);
this->res.uint128 = (this->dst.uint128 & ~mask) | ((this->src.uint128 << (sel * 32)) & mask);
}
void pinsrq(ssse3_t *this)
{
uint8_t sel = this->udo_imm->lval.ubyte & 1;
//uint32_t mask = 0xFFFFFFFF << (sel * 32);
//printk("1temp1: %hhu", this->dst.uint128);
//uint32_t res = &this->res.uint128;
this->dst.uint64[sel] = this->src.uint64[0];
this->res.uint128 = this->dst.uint128;
printk("OPEMU: pinsrq %s\n", ud_insn_asm(this->op_obj->ud_obj));
}