From patchwork Fri Mar 15 03:26:29 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 160369 Delivered-To: patch@linaro.org Received: by 2002:a02:5cc1:0:0:0:0:0 with SMTP id w62csp16265860jad; Thu, 14 Mar 2019 20:37:52 -0700 (PDT) X-Google-Smtp-Source: APXvYqwRe6WS9IdBDfu18wKxh1sCjth/ur4Y3sthVU8pXh7TdiDRh3of6FqFiWAVlCQvLX8eCvYv X-Received: by 2002:a7b:cf32:: with SMTP id m18mr523572wmg.98.1552621072055; Thu, 14 Mar 2019 20:37:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552621072; cv=none; d=google.com; s=arc-20160816; b=fN7Bz3HPdiP7e1i7/utji26UYDzJPh30PRHI/Z8eA8UNLEBa1Y2JhlpSZ1uQyt35We 1WEY4SLVycS+kcRzKJ3v3O+cwVqsyy3QbaYBwwY0ipzHwpbhE/aOe8bvB7bUumXyr64x Y5lrb3zt3VITpEonO1UsoDs3uxWsucnWlj2KQKAMDsfzNEx96YdOfl7BrLaIHishmge5 JlamU4TxhJhGZi51E5I5VTqL1KtLLAWyprgQ9Wz6cOUAfP5Qgezajoe+AEELx/LLqzmQ kV9nE6ltosUjjYcjM2YcQqSgbqRmTeKVtxiCjW6vdZinIuhh8geUoeiP1hg4BX2a79f0 Ra4A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject:references:in-reply-to :message-id:date:to:from:dkim-signature; bh=ZgJdz5TgrAEa9MUsE7W5Z5vwo/bknf4rpmDLWjvMoPs=; b=M4oaKLMslaJOCZts0n0kQXJbuwOICr1zzgEStD2BLeuNNLY43DDsCInQuVZs2QxbYf flXcel786E+GSItSGIuxOBgJdKbejzLShZhtoLRFYsZLW7CRUQgQJfqIYcumsNCy8y6t 0HYsxwjDPajiLniBj4zo81hr/ziSVQsM5fWlp02pL08yrjTaf5yDSlB+JmRsz9uOm3Uy Mfx96u602mWoJ6bIOja/8GvHouLeG7v3gkhnq558NTTsbqMs4gCxr8zBlPZDTnlPghos GYYhyO5PnjOsIDN+/TrMmoxnTU47RSoC5jMTDVNRvY89Fej2XQWEexsIAxz8eimL6pWe BMyw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=aScehl97; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id y1si455275wru.3.2019.03.14.20.37.51 for (version=TLS1 cipher=AES128-SHA bits=128/128); Thu, 14 Mar 2019 20:37:52 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=aScehl97; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([127.0.0.1]:48718 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h4dfK-0007kO-RC for patch@linaro.org; Thu, 14 Mar 2019 23:37:50 -0400 Received: from eggs.gnu.org ([209.51.188.92]:43798) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h4dcH-00059t-Eh for qemu-devel@nongnu.org; Thu, 14 Mar 2019 23:34:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h4dUt-0003Dl-UW for qemu-devel@nongnu.org; Thu, 14 Mar 2019 23:27:04 -0400 Received: from mail-pg1-x52d.google.com ([2607:f8b0:4864:20::52d]:39948) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h4dUt-0003Cx-Km for qemu-devel@nongnu.org; Thu, 14 Mar 2019 23:27:03 -0400 Received: by mail-pg1-x52d.google.com with SMTP id u9so5403647pgo.7 for ; Thu, 14 Mar 2019 20:27:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=ZgJdz5TgrAEa9MUsE7W5Z5vwo/bknf4rpmDLWjvMoPs=; b=aScehl972pqRk2Deqa8g+oQc4p28xs9GsNzsvBtt/+6EXY5qKm+qm5wM+bWbAJOnkB hMIK3QATYS+BEz2oqAw2p0RZyknriNo1kHpo/Js9A3zaepiJr1E1tQYgO0uPrnPG57M5 USgLvrZa6R9OsDJwNMCM6E/0iLb+CpdulAsT0oWAlHrBmzyIBUSS5mUgxqY3sppY75/h hOB8fY2Kk9LPEFPYgnQ+eJWRXgkUQu+zVHo/0Aq6wBCykchUnAKWx6aWIPtg+O7s2m68 aTEr6NDEHJNQRcN8Zi7rk+4uDbUIOpxMj72ghYAL5gclUtNCTW98j78rWyIyUNXuhUZL 7mmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=ZgJdz5TgrAEa9MUsE7W5Z5vwo/bknf4rpmDLWjvMoPs=; b=lmu0YiBIsrWi8N70ACximMngtiLLknt4BLmVHurVQVLzdvL44Rze2QoKlV9f5DHPWp 5JMqFfHk51Ct9IjJE07g+SKunKERFEDiB+XVUjuVvVBIhv0qnk8f5BBy39txeiORfO/D PPp9vSsHnS7K32vxW1WLHHuOz6GotsmhypNnKp0eWs1y7LliGTxUAA1lBpwNwcH2B46R 0agN/jK9jnMf+0O6fl9FpaxObKyafR8vdGo9/GbIbFaEM/kDUbvWteiAKHPS3IZoVRuY DaWCjI3lAI300RUcjuh83LILFIL5evFL/SBJSapRJaAu91s/PjKWlpflLPGwCxmOLifv vlug== X-Gm-Message-State: APjAAAXWKXoPxC1oaahiWehmkwA1RqO44P/EFMIubd+xS4Hab9tB/7oS HmI546xOTVUKMlzUO8yo8AT7hJ9J77k= X-Received: by 2002:aa7:8516:: with SMTP id v22mr1757692pfn.23.1552620422251; Thu, 14 Mar 2019 20:27:02 -0700 (PDT) Received: from cloudburst.twiddle.net (97-113-188-82.tukw.qwest.net. [97.113.188.82]) by smtp.gmail.com with ESMTPSA id d26sm690816pfn.86.2019.03.14.20.27.00 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 14 Mar 2019 20:27:01 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Thu, 14 Mar 2019 20:26:29 -0700 Message-Id: <20190315032629.21234-24-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.2 In-Reply-To: <20190315032629.21234-1-richard.henderson@linaro.org> References: <20190315032629.21234-1-richard.henderson@linaro.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::52d Subject: [Qemu-devel] [PATCH v3 23/23] target/i386: Implement CPUID_EXT_RDRAND X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paolo Bonzini , Eduardo Habkost Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" We now have an interface for guest visible random numbers. Cc: Paolo Bonzini Cc: Eduardo Habkost Signed-off-by: Richard Henderson --- target/i386/helper.h | 2 ++ target/i386/cpu.c | 5 ++-- target/i386/int_helper.c | 21 +++++++++++++++ target/i386/translate.c | 55 +++++++++++++++++++++++++++++----------- 4 files changed, 66 insertions(+), 17 deletions(-) -- 2.17.2 diff --git a/target/i386/helper.h b/target/i386/helper.h index 6fb8fb9b74..8f9e1905c3 100644 --- a/target/i386/helper.h +++ b/target/i386/helper.h @@ -226,3 +226,5 @@ DEF_HELPER_3(rcrl, tl, env, tl, tl) DEF_HELPER_3(rclq, tl, env, tl, tl) DEF_HELPER_3(rcrq, tl, env, tl, tl) #endif + +DEF_HELPER_1(rdrand, tl, env) diff --git a/target/i386/cpu.c b/target/i386/cpu.c index d90c01a059..e9fa43346d 100644 --- a/target/i386/cpu.c +++ b/target/i386/cpu.c @@ -729,13 +729,14 @@ static void x86_cpu_vendor_words2str(char *dst, uint32_t vendor1, CPUID_EXT_MONITOR | CPUID_EXT_SSSE3 | CPUID_EXT_CX16 | \ CPUID_EXT_SSE41 | CPUID_EXT_SSE42 | CPUID_EXT_POPCNT | \ CPUID_EXT_XSAVE | /* CPUID_EXT_OSXSAVE is dynamic */ \ - CPUID_EXT_MOVBE | CPUID_EXT_AES | CPUID_EXT_HYPERVISOR) + CPUID_EXT_MOVBE | CPUID_EXT_AES | CPUID_EXT_HYPERVISOR | \ + CPUID_EXT_RDRAND) /* missing: CPUID_EXT_DTES64, CPUID_EXT_DSCPL, CPUID_EXT_VMX, CPUID_EXT_SMX, CPUID_EXT_EST, CPUID_EXT_TM2, CPUID_EXT_CID, CPUID_EXT_FMA, CPUID_EXT_XTPR, CPUID_EXT_PDCM, CPUID_EXT_PCID, CPUID_EXT_DCA, CPUID_EXT_X2APIC, CPUID_EXT_TSC_DEADLINE_TIMER, CPUID_EXT_AVX, - CPUID_EXT_F16C, CPUID_EXT_RDRAND */ + CPUID_EXT_F16C */ #ifdef TARGET_X86_64 #define TCG_EXT2_X86_64_FEATURES (CPUID_EXT2_SYSCALL | CPUID_EXT2_LM) diff --git a/target/i386/int_helper.c b/target/i386/int_helper.c index 4dc5c65991..334469ca8c 100644 --- a/target/i386/int_helper.c +++ b/target/i386/int_helper.c @@ -22,6 +22,8 @@ #include "exec/exec-all.h" #include "qemu/host-utils.h" #include "exec/helper-proto.h" +#include "qapi/error.h" +#include "qemu/guest-random.h" //#define DEBUG_MULDIV @@ -470,3 +472,22 @@ void helper_cr4_testbit(CPUX86State *env, uint32_t bit) raise_exception_ra(env, EXCP06_ILLOP, GETPC()); } } + +target_ulong HELPER(rdrand)(CPUX86State *env) +{ + Error *err = NULL; + target_ulong ret; + + if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) { + qemu_log_mask(LOG_UNIMP, "rdrand: Crypto failure: %s", + error_get_pretty(err)); + error_free(err); + /* Failure clears CF and all other flags, and returns 0. */ + env->cc_src = 0; + return 0; + } + + /* Success sets CF and clears all others. */ + env->cc_src = CC_C; + return ret; +} diff --git a/target/i386/translate.c b/target/i386/translate.c index 49cd298374..b4922dd730 100644 --- a/target/i386/translate.c +++ b/target/i386/translate.c @@ -5327,31 +5327,56 @@ static target_ulong disas_insn(DisasContext *s, CPUState *cpu) case 0x1c7: /* cmpxchg8b */ modrm = x86_ldub_code(env, s); mod = (modrm >> 6) & 3; - if ((mod == 3) || ((modrm & 0x38) != 0x8)) - goto illegal_op; -#ifdef TARGET_X86_64 - if (dflag == MO_64) { - if (!(s->cpuid_ext_features & CPUID_EXT_CX16)) + switch ((modrm >> 3) & 7) { + case 1: /* CMPXCHG8, CMPXCHG16 */ + if (mod == 3) { goto illegal_op; - gen_lea_modrm(env, s, modrm); - if ((s->prefix & PREFIX_LOCK) && (tb_cflags(s->base.tb) & CF_PARALLEL)) { - gen_helper_cmpxchg16b(cpu_env, s->A0); - } else { - gen_helper_cmpxchg16b_unlocked(cpu_env, s->A0); } - } else +#ifdef TARGET_X86_64 + if (dflag == MO_64) { + if (!(s->cpuid_ext_features & CPUID_EXT_CX16)) { + goto illegal_op; + } + gen_lea_modrm(env, s, modrm); + if ((s->prefix & PREFIX_LOCK) && + (tb_cflags(s->base.tb) & CF_PARALLEL)) { + gen_helper_cmpxchg16b(cpu_env, s->A0); + } else { + gen_helper_cmpxchg16b_unlocked(cpu_env, s->A0); + } + set_cc_op(s, CC_OP_EFLAGS); + break; + } #endif - { - if (!(s->cpuid_features & CPUID_CX8)) + if (!(s->cpuid_features & CPUID_CX8)) { goto illegal_op; + } gen_lea_modrm(env, s, modrm); - if ((s->prefix & PREFIX_LOCK) && (tb_cflags(s->base.tb) & CF_PARALLEL)) { + if ((s->prefix & PREFIX_LOCK) && + (tb_cflags(s->base.tb) & CF_PARALLEL)) { gen_helper_cmpxchg8b(cpu_env, s->A0); } else { gen_helper_cmpxchg8b_unlocked(cpu_env, s->A0); } + set_cc_op(s, CC_OP_EFLAGS); + break; + + case 7: /* RDSEED */ + case 6: /* RDRAND */ + if (mod != 3 || + (s->prefix & (PREFIX_LOCK | PREFIX_REPZ | PREFIX_REPNZ)) || + !(s->cpuid_ext_features & CPUID_EXT_RDRAND)) { + goto illegal_op; + } + gen_helper_rdrand(s->T0, cpu_env); + rm = (modrm & 7) | REX_B(s); + gen_op_mov_reg_v(s, dflag, rm, s->T0); + set_cc_op(s, CC_OP_EFLAGS); + break; + + default: + goto illegal_op; } - set_cc_op(s, CC_OP_EFLAGS); break; /**************************/