From d946114dfe6b02c5d8b1c1617704478f112ce23f Mon Sep 17 00:00:00 2001 From: lazymio Date: Fri, 25 Feb 2022 22:44:42 +0100 Subject: [PATCH] Set EFLAGS correctly on startup --- qemu/target/i386/unicorn.c | 2 +- tests/unit/test_x86.c | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/qemu/target/i386/unicorn.c b/qemu/target/i386/unicorn.c index ded937fe..b846cd69 100644 --- a/qemu/target/i386/unicorn.c +++ b/qemu/target/i386/unicorn.c @@ -80,7 +80,7 @@ void x86_reg_reset(struct uc_struct *uc) memset(&env->idt, 0, sizeof(env->idt)); env->eip = 0; - env->eflags = 0; + cpu_load_eflags(env, 0, -1); env->cc_op = CC_OP_EFLAGS; env->fpstt = 0; /* top of stack index */ diff --git a/tests/unit/test_x86.c b/tests/unit/test_x86.c index 6bec3554..4c786d5a 100644 --- a/tests/unit/test_x86.c +++ b/tests/unit/test_x86.c @@ -932,6 +932,26 @@ static void test_x86_64_nested_emu_start_error() OK(uc_close(uc)); } +static void test_x86_eflags_reserved_bit() +{ + uc_engine *uc; + uint32_t r_eflags; + + OK(uc_open(UC_ARCH_X86, UC_MODE_32, &uc)); + + OK(uc_reg_read(uc, UC_X86_REG_EFLAGS, &r_eflags)); + + TEST_CHECK((r_eflags & 2) != 0); + + OK(uc_reg_write(uc, UC_X86_REG_EFLAGS, &r_eflags)); + + OK(uc_reg_read(uc, UC_X86_REG_EFLAGS, &r_eflags)); + + TEST_CHECK((r_eflags & 2) != 0); + + OK(uc_close(uc)); +} + TEST_LIST = { {"test_x86_in", test_x86_in}, {"test_x86_out", test_x86_out}, @@ -962,4 +982,5 @@ TEST_LIST = { {"test_x86_nested_emu_start", test_x86_nested_emu_start}, {"test_x86_nested_emu_stop", test_x86_nested_emu_stop}, {"test_x86_64_nested_emu_start_error", test_x86_64_nested_emu_start_error}, + {"test_x86_eflags_reserved_bit", test_x86_eflags_reserved_bit}, {NULL, NULL}};