x86emu/emulator/
arith.rs

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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use super::Emulator;
use super::InternalError;
use super::rflags::update_flags_szp;
use crate::Cpu;
use iced_x86::Instruction;
use iced_x86::Register;
use x86defs::RFlags;

impl<T: Cpu> Emulator<'_, T> {
    // <op> rm instructions
    pub(super) async fn unary_arith<Op: UnaryArithOp>(
        &mut self,
        instr: &Instruction,
    ) -> Result<(), InternalError<T::Error>> {
        let left = self.op_value(instr, 0).await?;
        let result = Op::op(left);
        self.compare_if_locked_and_write_op_0(instr, left, result)
            .await?;
        let mut rflags = self.cpu.rflags();
        Op::update_flags(&mut rflags, instr.memory_size().size(), result, left);
        self.cpu.set_rflags(rflags);
        Ok(())
    }

    // <op> rm/r, rm/r/imm instructions
    pub(super) async fn arith<Op: ArithOp>(
        &mut self,
        instr: &Instruction,
    ) -> Result<(), InternalError<T::Error>> {
        let left = self.op_value(instr, 0).await?;
        let right = self.op_value(instr, 1).await?;
        let mut rflags = self.cpu.rflags();
        let result = Op::op(left, right, rflags);
        if Op::UPDATES_RESULT {
            self.compare_if_locked_and_write_op_0(instr, left, result)
                .await?;
        }
        Op::update_flags(&mut rflags, instr.memory_size().size(), result, left, right);
        self.cpu.set_rflags(rflags);
        Ok(())
    }

    // xadd rm/r, r
    pub(super) async fn xadd(
        &mut self,
        instr: &Instruction,
    ) -> Result<(), InternalError<T::Error>> {
        let left = self.op_value(instr, 0).await?;
        let right_reg = instr.op1_register();
        let right = self.cpu.gp(right_reg.into());
        let result = left.wrapping_add(right);

        self.compare_if_locked_and_write_op_0(instr, left, result)
            .await?;
        self.cpu.set_gp(right_reg.into(), left);
        let mut rflags = self.cpu.rflags();
        update_flags_arith(
            &mut rflags,
            true,
            true,
            instr.memory_size().size(),
            result,
            right,
            left,
        );
        self.cpu.set_rflags(rflags);
        Ok(())
    }

    // cmpxchg rm/r, r
    pub(super) async fn cmpxchg(
        &mut self,
        instr: &Instruction,
    ) -> Result<(), InternalError<T::Error>> {
        let left = self.op_value(instr, 0).await?;
        let right = self.cpu.gp(instr.op1_register().into());

        let op_size = instr.memory_size().size();
        let cmp_reg = match op_size {
            1 => Register::AL,
            2 => Register::AX,
            4 => Register::EAX,
            8 => Register::RAX,
            _ => unreachable!(),
        };
        let cmp_val = self.cpu.gp(cmp_reg.into());

        let result = CmpOp::op(cmp_val, left, self.cpu.rflags());

        if result == 0 {
            self.compare_if_locked_and_write_op_0(instr, left, right)
                .await?;
        } else {
            self.cpu.set_gp(cmp_reg.into(), left);
        }

        let mut rflags = self.cpu.rflags();
        CmpOp::update_flags(&mut rflags, op_size, result, cmp_val, left);
        self.cpu.set_rflags(rflags);

        Ok(())
    }

    // xchg rm/r, r
    pub(super) async fn xchg(
        &mut self,
        instr: &Instruction,
    ) -> Result<(), InternalError<T::Error>> {
        let left = self.op_value(instr, 0).await?;
        let right = self.cpu.gp(instr.op1_register().into());

        self.compare_if_locked_and_write_op_0(instr, left, right)
            .await?;
        self.cpu.set_gp(instr.op1_register().into(), left);
        Ok(())
    }
}

/// Updates rflags after an and, or, xor, etc. operation.
fn update_flags_logic(flags: &mut RFlags, operand_size: usize, result: u64) {
    update_flags_szp(flags, operand_size, result);
    flags.set_carry(false);
    flags.set_overflow(false);
}

/// Updates rflags after an add or subtract (or cmp) operation.
fn update_flags_arith(
    flags: &mut RFlags,
    is_add: bool,
    update_carry: bool,
    operand_size: usize,
    result: u64,
    val1: u64,
    val2: u64,
) {
    // Compute the carry bits of the computation.
    let carry_xor = val1 ^ val2 ^ result;
    // Compute the overflow bits of the computation.
    let overflow_xor = (val1 ^ result) & (val1 ^ val2 ^ if is_add { !0 } else { 0 });
    let op_shift = 64 - operand_size as u32 * 8;
    // Extract the high overflow bit.
    let overflow = ((overflow_xor << op_shift) as i64) < 0;
    // Extract the fifth carry bit.
    let aux_carry = carry_xor & 0x10 != 0;

    update_flags_szp(flags, operand_size, result);
    flags.set_overflow(overflow);
    flags.set_adjust(aux_carry);
    if update_carry {
        // Compute the nth carry bit. For 64-bit values, this is gone, but it
        // can be recomputed as the (n-1)th carry bit ^ (n-1)th overflow bit.
        let carry = (((carry_xor ^ overflow_xor) << op_shift) as i64) < 0;
        flags.set_carry(carry);
    }
}

/// Trait for binary arithmetic and comparison ops (add, test, cmp, etc.)
pub(super) trait ArithOp {
    const UPDATES_RESULT: bool;
    fn op(left: u64, right: u64, flags: RFlags) -> u64;
    fn update_flags(flags: &mut RFlags, operand_size: usize, result: u64, left: u64, right: u64);
}

/// Trait for unary arithmetic ops (not, neg, etc.)
pub(super) trait UnaryArithOp {
    fn op(left: u64) -> u64;
    fn update_flags(flags: &mut RFlags, operand_size: usize, result: u64, left: u64);
}

pub(super) struct CmpOp;
impl ArithOp for CmpOp {
    const UPDATES_RESULT: bool = false;
    fn op(left: u64, right: u64, _flags: RFlags) -> u64 {
        left.wrapping_sub(right)
    }
    fn update_flags(flags: &mut RFlags, operand_size: usize, result: u64, left: u64, right: u64) {
        update_flags_arith(flags, false, true, operand_size, result, left, right)
    }
}

pub(super) struct TestOp;
impl ArithOp for TestOp {
    const UPDATES_RESULT: bool = false;
    fn op(left: u64, right: u64, _flags: RFlags) -> u64 {
        left & right
    }
    fn update_flags(flags: &mut RFlags, operand_size: usize, result: u64, _left: u64, _right: u64) {
        update_flags_logic(flags, operand_size, result)
    }
}

pub(super) struct AndOp;
impl ArithOp for AndOp {
    const UPDATES_RESULT: bool = true;
    fn op(left: u64, right: u64, _flags: RFlags) -> u64 {
        left & right
    }
    fn update_flags(flags: &mut RFlags, operand_size: usize, result: u64, _left: u64, _right: u64) {
        update_flags_logic(flags, operand_size, result)
    }
}

pub(super) struct OrOp;
impl ArithOp for OrOp {
    const UPDATES_RESULT: bool = true;
    fn op(left: u64, right: u64, _flags: RFlags) -> u64 {
        left | right
    }

    fn update_flags(flags: &mut RFlags, operand_size: usize, result: u64, _left: u64, _right: u64) {
        update_flags_logic(flags, operand_size, result)
    }
}

pub(super) struct XorOp;
impl ArithOp for XorOp {
    const UPDATES_RESULT: bool = true;
    fn op(left: u64, right: u64, _flags: RFlags) -> u64 {
        left ^ right
    }

    fn update_flags(flags: &mut RFlags, operand_size: usize, result: u64, _left: u64, _right: u64) {
        update_flags_logic(flags, operand_size, result)
    }
}

pub(super) struct AddOp;
impl ArithOp for AddOp {
    const UPDATES_RESULT: bool = true;
    fn op(left: u64, right: u64, _flags: RFlags) -> u64 {
        left.wrapping_add(right)
    }
    fn update_flags(flags: &mut RFlags, operand_size: usize, result: u64, left: u64, right: u64) {
        update_flags_arith(flags, true, true, operand_size, result, left, right)
    }
}

pub(super) struct AdcOp;
impl ArithOp for AdcOp {
    const UPDATES_RESULT: bool = true;
    fn op(left: u64, right: u64, flags: RFlags) -> u64 {
        left.wrapping_add(right).wrapping_add(flags.carry() as u64)
    }
    fn update_flags(flags: &mut RFlags, operand_size: usize, result: u64, left: u64, right: u64) {
        update_flags_arith(flags, true, true, operand_size, result, left, right)
    }
}

pub(super) struct SubOp;
impl ArithOp for SubOp {
    const UPDATES_RESULT: bool = true;
    fn op(left: u64, right: u64, _flags: RFlags) -> u64 {
        left.wrapping_sub(right)
    }
    fn update_flags(flags: &mut RFlags, operand_size: usize, result: u64, left: u64, right: u64) {
        update_flags_arith(flags, false, true, operand_size, result, left, right)
    }
}

pub(super) struct SbbOp;
impl ArithOp for SbbOp {
    const UPDATES_RESULT: bool = true;
    fn op(left: u64, right: u64, flags: RFlags) -> u64 {
        left.wrapping_sub(right.wrapping_add(flags.carry() as u64))
    }
    fn update_flags(flags: &mut RFlags, operand_size: usize, result: u64, left: u64, right: u64) {
        update_flags_arith(flags, false, true, operand_size, result, left, right)
    }
}

pub(super) struct IncOp;
impl UnaryArithOp for IncOp {
    fn op(left: u64) -> u64 {
        left.wrapping_add(1)
    }
    fn update_flags(flags: &mut RFlags, operand_size: usize, result: u64, left: u64) {
        update_flags_arith(flags, true, false, operand_size, result, left, 1)
    }
}

pub(super) struct DecOp;
impl UnaryArithOp for DecOp {
    fn op(left: u64) -> u64 {
        left.wrapping_sub(1)
    }
    fn update_flags(flags: &mut RFlags, operand_size: usize, result: u64, left: u64) {
        update_flags_arith(flags, false, false, operand_size, result, left, 1)
    }
}

pub(super) struct NegOp;
impl UnaryArithOp for NegOp {
    fn op(left: u64) -> u64 {
        0u64.wrapping_sub(left)
    }
    fn update_flags(flags: &mut RFlags, operand_size: usize, result: u64, left: u64) {
        update_flags_arith(flags, false, true, operand_size, result, 0, left)
    }
}

pub(super) struct NotOp;
impl UnaryArithOp for NotOp {
    fn op(left: u64) -> u64 {
        !left
    }
    fn update_flags(_flags: &mut RFlags, _operand_size: usize, _result: u64, _left: u64) {
        // Flags not affected
    }
}