From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id 4D5FDA05A0; Tue, 21 Apr 2020 21:55:56 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 274A51D41D; Tue, 21 Apr 2020 21:55:56 +0200 (CEST) Received: from mail-qk1-f194.google.com (mail-qk1-f194.google.com [209.85.222.194]) by dpdk.org (Postfix) with ESMTP id 7E72B1D17A for ; Tue, 21 Apr 2020 21:55:54 +0200 (CEST) Received: by mail-qk1-f194.google.com with SMTP id c63so26551qke.2 for ; Tue, 21 Apr 2020 12:55:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=JUGqS7GdHHkTq5Y3fPueA6oX1uhWWDiCvuypNYvDXD4=; b=RhL143e9aBl00Z0M5vjo06GX5JQdFNb0ibiB5RBktDu3vFMdjvTV9OV/nkiNSkdhAD xgrAHtPDSf322F12m3+F4H7Ay/RiL4uBAHmOPVBIj8d7mB4qy3Wbb0/xBlmd/1R20Z1n jAY4KeR7d8qXjFoI79t5svsushO4MTJtOveQCRl0c0hOoNE2uHvOMaX6unf4k7OBBXSj jDbTnTgD+ykguUNXaJ7vwmbLaM/hx2ba7M34BCov4RN3G8K52x1TcT1rvAY3WKgNiQ1h moP2Z2UDmLOMvhcQw8i3UBk9/OEfbwFzOtbeKYWbgGTMMYLmx5d8s2w871I9+CaGCQQx DVIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=JUGqS7GdHHkTq5Y3fPueA6oX1uhWWDiCvuypNYvDXD4=; b=m7b6iyhmcEEgmkI5omF4JsBqhgUrmkHJPo5BvFvHdQzCDqh7G0GoENJAtNLKOGq1pA Z7q/D+6XsGIAGQWlVtVhkwSq3gUyZfV2Qk/n/JPwN9Smxq6ZAH0HluRNK+tnYBHmXnCf NSB0f5bSabfTL7oMTEhRvJV0hQx0xh5lm0hV0KFNMYrDUVP7ACAf1hNCtTUhe18ZlcKL 65ETtfzljQ6l3SDXfTSBdpuBYsKU7F4QPqyY2Mn5utA5ayliH65/R70rCSQ0Pis734MB m36n8HWHHJXl0PcIoHVGdi2PYLXZ+emfGBDnT2H+edjhJQF/WdWUPiKHEVJ37j1A8u+J R5Gg== X-Gm-Message-State: AGi0PuYQ50uICh7RJvtkmXE0PVvblhk54XO4PTlPh1RgXyhYlmHA41AM W2C5xgLshkBoIEsywKNcDS0Ns0zZhEM= X-Google-Smtp-Source: APiQypK7lzreNwOyeMpRqukCjgujjSgi660ZcCom+4a+9NbjxCv/zLO2g6zrCd1N+uKTMgBtz3IbsQ== X-Received: by 2002:a37:5209:: with SMTP id g9mr23506204qkb.237.1587498953643; Tue, 21 Apr 2020 12:55:53 -0700 (PDT) Received: from localhost.localdomain ([168.121.99.161]) by smtp.gmail.com with ESMTPSA id o94sm2423378qtd.34.2020.04.21.12.55.51 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 21 Apr 2020 12:55:53 -0700 (PDT) From: Dan Gora To: dev@dpdk.org, Thomas Monjalon , =?UTF-8?q?Mattias=20R=C3=B6nnblom?= Cc: David Marchand , Jerin Jacob , Dan Gora Date: Tue, 21 Apr 2020 16:54:44 -0300 Message-Id: <20200421195446.1730-2-dg@adax.com> X-Mailer: git-send-email 2.24.1.425.g7034cd094b In-Reply-To: <20200421195446.1730-1-dg@adax.com> References: <20200421195446.1730-1-dg@adax.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [dpdk-dev] [PATCH 1/2] eal: check for rdseed at run time for random seed X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Instead of enabling the rdseed instruction for the random number generator entropy source at compilation time, determine if the instruction can be used at run time. The DPDK build is updated to check that the compiler can generate the rdseed instruction even if the compilation platform does not natively support it. If so, the -mrdseed flag is explicitly added. This allows binaries compiled on systems which do not support the rdseed instruction to use the instruction if the execution platform supports it. At run-time on x86, __rte_random_initial_seed() will check for the availability of the rdseed instruction on the execution platform using rte_cpu_get_flag_enabled(). This allows binaries which were compiled on systems which support the rdseed instruction to run on x86 CPUs which do not support the rdseed instruction. RTE_CPUFLAG_RDSEED is removed from the list of RTE_COMPILE_TIME_CPUFLAGS which are checked in rte_eal_init() at run time because it is no longer required to match the compilation system. Signed-off-by: Dan Gora --- config/x86/meson.build | 11 ++++++++--- lib/librte_eal/common/rte_random.c | 19 +++++++++++-------- mk/rte.cpuflags.mk | 9 +++++++-- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/config/x86/meson.build b/config/x86/meson.build index adc857ba2..9491fad3a 100644 --- a/config/x86/meson.build +++ b/config/x86/meson.build @@ -20,15 +20,20 @@ if cc.get_define('__SSE4_2__', args: machine_args) == '' machine_args += '-msse4' endif +# set -mrdseed if necessary so _rdseed32_step compiles if the +# compilation host does not support the RDSEED instruction. +if cc.get_define('__RDSEED__', args: machine_args) == '' and cc.has_argument('-mrdseed') + machine_args += '-mrdseed' + message('RDSEED not enabled by default, explicitly setting -mrdseed') +endif + base_flags = ['SSE', 'SSE2', 'SSE3','SSSE3', 'SSE4_1', 'SSE4_2'] foreach f:base_flags dpdk_conf.set('RTE_MACHINE_CPUFLAG_' + f, 1) compile_time_cpuflags += ['RTE_CPUFLAG_' + f] endforeach -optional_flags = ['AES', 'PCLMUL', - 'AVX', 'AVX2', 'AVX512F', - 'RDRND', 'RDSEED'] +optional_flags = ['AES', 'PCLMUL', 'AVX', 'AVX2', 'AVX512F', 'RDRND'] foreach f:optional_flags if cc.get_define('__@0@__'.format(f), args: machine_args) == '1' if f == 'PCLMUL' # special case flags with different defines diff --git a/lib/librte_eal/common/rte_random.c b/lib/librte_eal/common/rte_random.c index 57ec8fb2b..df02f1307 100644 --- a/lib/librte_eal/common/rte_random.c +++ b/lib/librte_eal/common/rte_random.c @@ -2,7 +2,7 @@ * Copyright(c) 2019 Ericsson AB */ -#ifdef RTE_MACHINE_CPUFLAG_RDSEED +#if defined(RTE_ARCH_X86) #include #endif #include @@ -188,14 +188,17 @@ __rte_random_initial_seed(void) if (ge_rc == 0) return ge_seed; #endif -#ifdef RTE_MACHINE_CPUFLAG_RDSEED - unsigned int rdseed_low; - unsigned int rdseed_high; - +#if defined(RTE_ARCH_X86) /* first fallback: rdseed instruction, if available */ - if (_rdseed32_step(&rdseed_low) == 1 && - _rdseed32_step(&rdseed_high) == 1) - return (uint64_t)rdseed_low | ((uint64_t)rdseed_high << 32); + if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_RDSEED)) { + unsigned int rdseed_low; + unsigned int rdseed_high; + + if (_rdseed32_step(&rdseed_low) == 1 && + _rdseed32_step(&rdseed_high) == 1) + return (uint64_t)rdseed_low | + ((uint64_t)rdseed_high << 32); + } #endif /* second fallback: seed using rdtsc */ return rte_get_timer_cycles(); diff --git a/mk/rte.cpuflags.mk b/mk/rte.cpuflags.mk index fa8753531..a114e9c02 100644 --- a/mk/rte.cpuflags.mk +++ b/mk/rte.cpuflags.mk @@ -51,8 +51,13 @@ ifneq ($(filter $(AUTO_CPUFLAGS),__RDRND__),) CPUFLAGS += RDRAND endif -ifneq ($(filter $(AUTO_CPUFLAGS),__RDSEED__),) -CPUFLAGS += RDSEED +ifeq ($(filter $(AUTO_CPUFLAGS),__RDSEED__),) +# If the native environment doesn't define __RDSEED__, see +# if the compiler supports -mrdseed. +RDSEED_CPUFLAGS := $(shell $(CC) $(MACHINE_CFLAGS) $(WERROR_FLAGS) $(EXTRA_CFLAGS) -mrdseed -dM -E - < /dev/null) +ifneq ($(filter $(RDSEED_CPUFLAGS),__RDSEED__),) +MACHINE_CFLAGS += -mrdseed +endif endif ifneq ($(filter $(AUTO_CPUFLAGS),__FSGSBASE__),) -- 2.24.1.425.g7034cd094b