-
Notifications
You must be signed in to change notification settings - Fork 10
/
sse3.c
417 lines (350 loc) · 10.8 KB
/
sse3.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
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
/*
.d8888b. .d8888b. 8888888888 .d8888b.
d88P Y88b d88P Y88b 888 d88P Y88b
Y88b. Y88b. 888 .d88P
"Y888b. "Y888b. 8888888 8888"
"Y88b. "Y88b. 888 "Y8b.
"888 "888 888 888 888
Y88b d88P Y88b d88P 888 Y88b d88P
"Y8888P" "Y8888P" 8888888888 "Y8888P"
*/
#include <linux/uaccess.h>
#include "opemu.h"
#include "opemu_math.h"
#include "sse3_priv.h"
/**
* Load operands from memory/register, store in obj.
* @return: 0 if success
*/
int sse3_grab_operands(sse3_t *sse3_obj)
{
if (sse3_obj->ismmx) {
_store_mmx (sse3_obj->udo_dst->base - UD_R_MM0, &sse3_obj->dst.uint64[0]);
if (sse3_obj->udo_src->type == UD_OP_REG) {
_store_mmx (sse3_obj->udo_src->base - UD_R_MM0, &sse3_obj->src.uint64[0]);
} else {
// m64 load
int64_t disp = 0;
uint8_t disp_size = sse3_obj->udo_src->offset;
uint64_t address;
if (sse3_obj->udo_src->scale) goto bad; // TODO
if (retrieve_reg (sse3_obj->op_obj->state,
sse3_obj->udo_src->base, NULL, &address) != 0) goto bad;
switch (disp_size) {
case 8: disp = sse3_obj->udo_src->lval.sbyte; break;
case 16: disp = sse3_obj->udo_src->lval.sword; break;
case 32: disp = sse3_obj->udo_src->lval.sdword; break;
case 64: disp = sse3_obj->udo_src->lval.sqword; break;
}
address += disp;
if (sse3_obj->op_obj->ring0)
sse3_obj->src.uint64[0] = * ((uint64_t*) (address));
else copy_from_user((char*) &sse3_obj->src.uint64[0], address, 8);
}
} else {
_store_xmm (sse3_obj->udo_dst->base - UD_R_XMM0, &sse3_obj->dst.uint128);
if (sse3_obj->udo_src->type == UD_OP_REG) {
_store_xmm (sse3_obj->udo_src->base - UD_R_XMM0, &sse3_obj->src.uint128);
} else {
// m128 load
int64_t disp = 0;
uint8_t disp_size = sse3_obj->udo_src->offset;
uint64_t address;
if (sse3_obj->udo_src->scale) goto bad; // TODO
if (retrieve_reg (sse3_obj->op_obj->state,
sse3_obj->udo_src->base, NULL, &address) != 0) goto bad;
switch (disp_size) {
case 8: disp = sse3_obj->udo_src->lval.sbyte; break;
case 16: disp = sse3_obj->udo_src->lval.sword; break;
case 32: disp = sse3_obj->udo_src->lval.sdword; break;
case 64: disp = sse3_obj->udo_src->lval.sqword; break;
}
address += disp;
if (sse3_obj->op_obj->ring0)
sse3_obj->src.uint128 = * ((__uint128_t*) (address));
else copy_from_user((char*) &sse3_obj->src.uint128, address, 16);
}
}
return 0;
// Only reached if bad
bad: return -1;
}
/**
* Store operands from obj to memory/register.
* @return: 0 if success
*/
int sse3_commit_results(const sse3_t *sse3_obj)
{
if (sse3_obj->ismmx) {
_load_mmx (sse3_obj->udo_dst->base - UD_R_MM0, (void*) &sse3_obj->res.uint64[0]);
} else {
_load_xmm (sse3_obj->udo_dst->base - UD_R_XMM0, (void*) &sse3_obj->res.uint128);
}
return 0;
// Only reached if bad
//bad: return -1;
}
/**
* Main function for the sse3 portion. Check if the offending
* opcode is part of the sse3 instruction set, if not, quit early.
* if so, then we build the appropriate context, and jump to the right function.
* @param op_obj: opemu object
* @return: zero if an instruction was emulated properly
*/
int op_sse3_run(const op_t *op_obj)
{
sse3_t sse3_obj;
sse3_obj.op_obj = op_obj;
const uint32_t mnemonic = ud_insn_mnemonic(sse3_obj.op_obj->ud_obj);
sse3_func opf;
switch (mnemonic) {
case UD_Iaddsubpd: opf = addsubpd; goto sse3_common;
case UD_Iaddsubps: opf = addsubps; goto sse3_common;
case UD_Ihaddpd: opf = haddpd; goto sse3_common;
case UD_Ihaddps: opf = haddpd; goto sse3_common;
case UD_Ihsubpd: opf = hsubpd; goto sse3_common;
case UD_Ihsubps: opf = hsubps; goto sse3_common;
case UD_Ilddqu: opf = lddqu; goto sse3_common;
case UD_Imovddup: opf = movddup; goto sse3_common;
case UD_Imovshdup: opf = movshdup; goto sse3_common;
case UD_Imovsldup: opf = movsldup; goto sse3_common;
case UD_Ifistp: opf = fisttp; opf(&sse3_obj); goto good;
case UD_Imwait: goto good;
case UD_Imonitor: goto good;
sse3_common:
sse3_obj.udo_src = ud_insn_opr (op_obj->ud_obj, 1);
sse3_obj.udo_dst = ud_insn_opr (op_obj->ud_obj, 0);
sse3_obj.udo_imm = ud_insn_opr (op_obj->ud_obj, 2);
// run some sanity checks,
if (sse3_obj.udo_dst->type != UD_OP_REG) goto bad;
if ((sse3_obj.udo_src->type != UD_OP_REG)
&& (sse3_obj.udo_src->type != UD_OP_MEM))
goto bad;
// i'd like to know if this instruction is legacy mmx
if ((sse3_obj.udo_dst->base >= UD_R_MM0)
&& (sse3_obj.udo_dst->base <= UD_R_MM7)) {
sse3_obj.ismmx = 1;
} else sse3_obj.ismmx = 0;
if (sse3_grab_operands(&sse3_obj) != 0) goto bad;
opf(&sse3_obj);
if (sse3_commit_results(&sse3_obj)) goto bad;
goto good;
default: goto bad;
}
good:
return 0;
// Only reached if bad
bad:
return -1;
}
void fisttp(sse3_t *this)
{
const unsigned char *bytep = (const unsigned char *)ud_insn_hex((ud_t *)&this->op_obj->ud_obj);
uint8_t islongmode = is_saved_state64(this->op_obj->state);
int ins_size = 0;
uint8_t base = 0;
uint8_t mod = 0;
int8_t add = 0;
uint8_t modrm = 0;
uint64_t address = 0;
uint64_t reg_sel[8];
if (islongmode)
{
reg_sel[0] = this->op_obj->state64->ax;
reg_sel[1] = this->op_obj->state64->cx;
reg_sel[2] = this->op_obj->state64->dx;
reg_sel[3] = this->op_obj->state64->bx;
reg_sel[4] = this->op_obj->state64->sp;
reg_sel[5] = this->op_obj->state64->bp;
reg_sel[6] = this->op_obj->state64->si;
reg_sel[7] = this->op_obj->state64->di;
} else {
reg_sel[0] = this->op_obj->state32->ax;
reg_sel[1] = this->op_obj->state32->cx;
reg_sel[2] = this->op_obj->state32->dx;
reg_sel[3] = this->op_obj->state32->bx;
reg_sel[4] = this->op_obj->state32->sp;
reg_sel[5] = this->op_obj->state32->bp;
reg_sel[6] = this->op_obj->state32->si;
reg_sel[7] = this->op_obj->state32->di;
}
if (*bytep == 0x66)
{
bytep++;
ins_size++;
}
switch (*bytep)
{
case 0xDB:
bytep++;
ins_size++;
modrm = *bytep;
base = modrm & 0x7;
mod = (modrm & 0xC0) >> 6;
if (mod == 0)
{
address = reg_sel[base];
} else if (mod == 1) {
bytep++;
ins_size++;
add = *bytep;
address = reg_sel[base] + add;
} else {
return;
}
fisttpl((double *)address);
ins_size++;
return;
break;
case 0xDD:
bytep++;
ins_size++;
modrm = *bytep;
base = modrm & 0x7;
mod = (modrm & 0xC0) >> 6;
if (mod == 0)
{
address = reg_sel[base];
} else if (mod == 1) {
bytep++;
ins_size++;
add = *bytep;
address = reg_sel[base] + add;
} else {
return;
}
fisttpq((long double *)address);
ins_size++;
return;
break;
case 0xDF:
bytep++;
ins_size++;
modrm = *bytep;
base = modrm & 0x7;
mod = (modrm & 0xC0) >> 6;
if (mod == 0)
{
address = reg_sel[base];
} else if (mod == 1) {
bytep++;
ins_size++;
add = *bytep;
address = reg_sel[base] + add;
} else {
return;
}
fisttps((float *)address);
ins_size++;
return;
break;
}
}
/*********************************************/
/** AnV - SSE3 instructions implementation **/
/*********************************************/
void fisttps(float *res)
{
float value = opemu_truncf(*res);
__asm__ ("fistps %0" : : "m" (value));
*res = value;
}
void fisttpl(double *res)
{
double value = opemu_trunc(*res);
__asm__ ("fistpl %0" : : "m" (value));
*res = value;
}
void fisttpq(long double *res)
{
// AnV - Truncl doesn't work but fistpq has same result in this case... (tested)
long double value = *res; /*opemu_truncl(*res);*/
__asm__ ("fistpq %0" : : "m" (value));
*res = value;
}
void addsubpd(sse3_t *this)
{
const double *src = &this->src.fa64[0];
const double *dst = &this->dst.fa64[0];
double *res = &this->res.fa64[0];
res[0] = src[0] - dst[0];
res[1] = src[1] + dst[1];
}
void addsubps(sse3_t *this)
{
const float *src = &this->src.fa32[0];
const float *dst = &this->dst.fa32[0];
float *res = &this->res.fa32[0];
res[0] = src[0] - dst[0];
res[1] = src[1] + dst[1];
res[2] = src[2] - dst[2];
res[3] = src[3] + dst[3];
}
void haddpd(sse3_t *this)
{
const double *src = &this->src.fa64[0];
const double *dst = &this->dst.fa64[0];
double *res = &this->res.fa64[0];
res[0] = src[0] + src[1];
res[1] = dst[0] + dst[1];
}
void haddps(sse3_t *this)
{
const float *src = &this->src.fa32[0];
const float *dst = &this->dst.fa32[0];
float *res = &this->res.fa32[0];
res[0] = src[0] + src[1];
res[1] = src[2] + src[3];
res[2] = dst[0] + dst[1];
res[3] = dst[2] + dst[3];
}
void hsubpd(sse3_t *this)
{
const double *src = &this->src.fa64[0];
const double *dst = &this->dst.fa64[0];
double *res = &this->res.fa64[0];
res[0] = src[0] - src[1];
res[1] = dst[0] - dst[1];
}
void hsubps(sse3_t *this)
{
const float *src = &this->src.fa32[0];
const float *dst = &this->dst.fa32[0];
float *res = &this->res.fa32[0];
res[0] = src[0] - src[1];
res[1] = src[2] - src[3];
res[2] = dst[0] - dst[1];
res[3] = dst[2] - dst[3];
}
void lddqu(sse3_t *this)
{
const double *src = &this->src.fa64[0];
double *res = &this->res.fa64[0];
res[0] = src[0];
res[1] = src[1];
}
void movddup(sse3_t *this)
{
const double *src = &this->src.fa64[0];
double *res = &this->res.fa64[0];
res[0] = src[0];
res[1] = src[0];
}
void movshdup(sse3_t *this)
{
const float *src = &this->src.fa32[0];
float *res = &this->res.fa32[0];
res[0] = src[1];
res[1] = src[1];
res[2] = src[3];
res[3] = src[3];
}
void movsldup(sse3_t *this)
{
const float *src = &this->src.fa32[0];
float *res = &this->res.fa32[0];
res[0] = src[0];
res[1] = src[0];
res[2] = src[2];
res[3] = src[2];
}