From: Ravi Bangoria <ravi.bango...@linux.vnet.ibm.com> darn: Deliver A Random Number
For both CRN and RRN, returning 64-bit random number. Signed-off-by: Ravi Bangoria <ravi.bango...@linux.vnet.ibm.com> [ use /dev/random to get the number ] Signed-off-by: Nikunj A Dadhania <nik...@linux.vnet.ibm.com> --- target-ppc/helper.h | 2 ++ target-ppc/int_helper.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ target-ppc/translate.c | 18 +++++++++++++++++ 3 files changed, 71 insertions(+) diff --git a/target-ppc/helper.h b/target-ppc/helper.h index 8eada2f..0e55d48 100644 --- a/target-ppc/helper.h +++ b/target-ppc/helper.h @@ -50,6 +50,8 @@ DEF_HELPER_FLAGS_1(cnttzd, TCG_CALL_NO_RWG_SE, tl, tl) DEF_HELPER_FLAGS_1(popcntd, TCG_CALL_NO_RWG_SE, tl, tl) DEF_HELPER_FLAGS_2(bpermd, TCG_CALL_NO_RWG_SE, i64, i64, i64) DEF_HELPER_3(srad, tl, env, tl, tl) +DEF_HELPER_0(darn32, tl) +DEF_HELPER_0(darn64, tl) #endif DEF_HELPER_FLAGS_1(cntlsw32, TCG_CALL_NO_RWG_SE, i32, i32) diff --git a/target-ppc/int_helper.c b/target-ppc/int_helper.c index 552b2e0..5b70054 100644 --- a/target-ppc/int_helper.c +++ b/target-ppc/int_helper.c @@ -182,6 +182,57 @@ target_ulong helper_cnttzd(target_ulong t) { return ctz64(t); } + +#define FILE_DEV_RANDOM "/dev/random" + +static bool get_random(char *s, size_t size) +{ + int fd; + size_t bytes_read; + + fd = open(FILE_DEV_RANDOM, O_RDONLY); + if (fd < 0) { + return false; + } + + while (size > 0) { + bytes_read = read(fd, s, size); + if (bytes_read <= 0) { + close(fd); + return false; + } + size -= bytes_read; + s += bytes_read; + } + close(fd); + return true; +} + +target_ulong helper_darn32(void) +{ + char s[4]; + if (!get_random(s, 4)) { + return 0; + } + + return *((uint32_t *)s); +} + +target_ulong helper_darn64(void) +{ + char s[8]; + uint64_t val; + + do { + if (!get_random(s, 8)) { + return -1; + } + val = *((uint64_t *)s); + } while (val == UINT64_MAX); + + return val; +} + #endif #if defined(TARGET_PPC64) diff --git a/target-ppc/translate.c b/target-ppc/translate.c index c7bbe28..67eac9f 100644 --- a/target-ppc/translate.c +++ b/target-ppc/translate.c @@ -526,6 +526,8 @@ EXTRACT_HELPER(FPW, 16, 1); /* addpcis */ EXTRACT_HELPER_DXFORM(DX, 10, 6, 6, 5, 16, 1, 1, 0, 0) +/* darn */ +EXTRACT_HELPER(L, 16, 2); /*** Jump target decoding ***/ /* Immediate address */ @@ -1893,6 +1895,21 @@ static void gen_cnttzd(DisasContext *ctx) gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]); } } + +/* darn */ +static void gen_darn(DisasContext *ctx) +{ + int l = L(ctx->opcode); + + if (l == 0) { + gen_helper_darn32(cpu_gpr[rD(ctx->opcode)]); + } else if (l <= 2) { + /* Return 64-bit random for both CRN and RRN */ + gen_helper_darn64(cpu_gpr[rD(ctx->opcode)]); + } else { + tcg_gen_movi_i64(cpu_gpr[rD(ctx->opcode)], -1); + } +} #endif /*** Integer rotate ***/ @@ -6219,6 +6236,7 @@ GEN_HANDLER_E(prtyw, 0x1F, 0x1A, 0x04, 0x0000F801, PPC_NONE, PPC2_ISA205), GEN_HANDLER(popcntd, 0x1F, 0x1A, 0x0F, 0x0000F801, PPC_POPCNTWD), GEN_HANDLER(cntlzd, 0x1F, 0x1A, 0x01, 0x00000000, PPC_64B), GEN_HANDLER_E(cnttzd, 0x1F, 0x1A, 0x11, 0x00000000, PPC_NONE, PPC2_ISA300), +GEN_HANDLER_E(darn, 0x1F, 0x13, 0x17, 0x001CF801, PPC_NONE, PPC2_ISA300), GEN_HANDLER_E(prtyd, 0x1F, 0x1A, 0x05, 0x0000F801, PPC_NONE, PPC2_ISA205), GEN_HANDLER_E(bpermd, 0x1F, 0x1C, 0x07, 0x00000001, PPC_NONE, PPC2_PERM_ISA206), #endif -- 2.7.4