2021-10-03 17:14:44 +03:00
|
|
|
#include "unicorn_test.h"
|
|
|
|
|
|
|
|
const uint64_t code_start = 0x1000;
|
|
|
|
const uint64_t code_len = 0x4000;
|
|
|
|
|
2021-10-29 13:44:49 +03:00
|
|
|
static void uc_common_setup(uc_engine **uc, uc_arch arch, uc_mode mode,
|
|
|
|
const char *code, uint64_t size)
|
|
|
|
{
|
2021-10-16 22:45:18 +03:00
|
|
|
OK(uc_open(arch, mode, uc));
|
|
|
|
OK(uc_mem_map(*uc, code_start, code_len, UC_PROT_ALL));
|
|
|
|
OK(uc_mem_write(*uc, code_start, code, size));
|
|
|
|
}
|
|
|
|
|
2022-04-16 14:40:04 +03:00
|
|
|
static void test_ppc32_add(void)
|
2021-10-29 13:44:49 +03:00
|
|
|
{
|
|
|
|
uc_engine *uc;
|
2021-10-16 22:45:18 +03:00
|
|
|
char code[] = "\x7f\x46\x1a\x14"; // ADD 26, 6, 3
|
|
|
|
int reg;
|
|
|
|
|
2021-10-29 13:44:49 +03:00
|
|
|
uc_common_setup(&uc, UC_ARCH_PPC, UC_MODE_32 | UC_MODE_BIG_ENDIAN, code,
|
|
|
|
sizeof(code) - 1);
|
2021-10-16 22:45:18 +03:00
|
|
|
|
|
|
|
reg = 42;
|
|
|
|
OK(uc_reg_write(uc, UC_PPC_REG_3, ®));
|
|
|
|
reg = 1337;
|
|
|
|
OK(uc_reg_write(uc, UC_PPC_REG_6, ®));
|
|
|
|
|
|
|
|
OK(uc_emu_start(uc, code_start, code_start + sizeof(code) - 1, 0, 0));
|
|
|
|
|
|
|
|
OK(uc_reg_read(uc, UC_PPC_REG_26, ®));
|
|
|
|
|
|
|
|
TEST_CHECK(reg == 1379);
|
|
|
|
|
|
|
|
OK(uc_close(uc));
|
|
|
|
}
|
|
|
|
|
2022-01-10 17:16:10 +03:00
|
|
|
// https://www.ibm.com/docs/en/aix/7.2?topic=set-fadd-fa-floating-add-instruction
|
2022-04-16 14:40:04 +03:00
|
|
|
static void test_ppc32_fadd(void)
|
2022-01-10 17:16:10 +03:00
|
|
|
{
|
|
|
|
uc_engine *uc;
|
|
|
|
char code[] = "\xfc\xc4\x28\x2a"; // fadd 6, 4, 5
|
|
|
|
uint32_t r_msr;
|
|
|
|
uint64_t r_fpr4, r_fpr5, r_fpr6;
|
|
|
|
|
|
|
|
uc_common_setup(&uc, UC_ARCH_PPC, UC_MODE_32 | UC_MODE_BIG_ENDIAN, code,
|
|
|
|
sizeof(code) - 1);
|
|
|
|
|
|
|
|
OK(uc_reg_read(uc, UC_PPC_REG_MSR, &r_msr));
|
|
|
|
r_msr |= (1 << 13); // Big endian
|
|
|
|
OK(uc_reg_write(uc, UC_PPC_REG_MSR, &r_msr)); // enable FP
|
|
|
|
|
|
|
|
r_fpr4 = 0xC053400000000000ul;
|
|
|
|
r_fpr5 = 0x400C000000000000ul;
|
|
|
|
OK(uc_reg_write(uc, UC_PPC_REG_FPR4, &r_fpr4));
|
|
|
|
OK(uc_reg_write(uc, UC_PPC_REG_FPR5, &r_fpr5));
|
|
|
|
|
|
|
|
OK(uc_emu_start(uc, code_start, code_start + sizeof(code) - 1, 0, 0));
|
|
|
|
|
|
|
|
OK(uc_reg_read(uc, UC_PPC_REG_FPR6, &r_fpr6));
|
|
|
|
|
|
|
|
TEST_CHECK(r_fpr6 == 0xC052600000000000ul);
|
|
|
|
|
|
|
|
OK(uc_close(uc));
|
|
|
|
}
|
|
|
|
|
2022-02-19 16:20:41 +03:00
|
|
|
static void test_ppc32_sc_cb(uc_engine *uc, uint32_t intno, void *data)
|
|
|
|
{
|
|
|
|
uc_emu_stop(uc);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-16 14:40:04 +03:00
|
|
|
static void test_ppc32_sc(void)
|
2022-02-19 16:20:41 +03:00
|
|
|
{
|
|
|
|
uc_engine *uc;
|
|
|
|
char code[] = "\x44\x00\x00\x02"; // sc
|
|
|
|
uint32_t r_pc;
|
|
|
|
uc_hook h;
|
|
|
|
|
|
|
|
uc_common_setup(&uc, UC_ARCH_PPC, UC_MODE_32 | UC_MODE_BIG_ENDIAN, code,
|
|
|
|
sizeof(code) - 1);
|
|
|
|
|
|
|
|
OK(uc_hook_add(uc, &h, UC_HOOK_INTR, test_ppc32_sc_cb, NULL, 1, 0));
|
|
|
|
OK(uc_emu_start(uc, code_start, code_start + sizeof(code) - 1, 0, 0));
|
|
|
|
|
|
|
|
OK(uc_reg_read(uc, UC_PPC_REG_PC, &r_pc));
|
|
|
|
|
|
|
|
TEST_CHECK(r_pc == code_start + 4);
|
|
|
|
|
|
|
|
OK(uc_close(uc));
|
|
|
|
}
|
|
|
|
|
2022-07-21 01:31:13 +03:00
|
|
|
static void test_ppc32_cr(void)
|
|
|
|
{
|
|
|
|
uc_engine *uc;
|
|
|
|
uint32_t r_cr = 0x12345678;
|
|
|
|
|
|
|
|
uc_common_setup(&uc, UC_ARCH_PPC, UC_MODE_32 | UC_MODE_BIG_ENDIAN, NULL, 0);
|
|
|
|
|
|
|
|
OK(uc_reg_write(uc, UC_PPC_REG_CR, &r_cr));
|
|
|
|
r_cr = 0;
|
|
|
|
OK(uc_reg_read(uc, UC_PPC_REG_CR, &r_cr));
|
|
|
|
|
|
|
|
TEST_CHECK(r_cr == 0x12345678);
|
|
|
|
|
|
|
|
OK(uc_close(uc));
|
|
|
|
}
|
|
|
|
|
2022-01-10 17:16:10 +03:00
|
|
|
TEST_LIST = {{"test_ppc32_add", test_ppc32_add},
|
|
|
|
{"test_ppc32_fadd", test_ppc32_fadd},
|
2022-02-19 16:20:41 +03:00
|
|
|
{"test_ppc32_sc", test_ppc32_sc},
|
2022-07-21 01:31:13 +03:00
|
|
|
{"test_ppc32_cr", test_ppc32_cr},
|
2022-01-10 17:16:10 +03:00
|
|
|
{NULL, NULL}};
|