From patchwork Fri May 10 17:30:36 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 163914 Delivered-To: patch@linaro.org Received: by 2002:a05:6e02:142:0:0:0:0 with SMTP id j2csp2673378ilr; Fri, 10 May 2019 10:37:58 -0700 (PDT) X-Google-Smtp-Source: APXvYqz9S8JwCpxqZpnuesrqTT7DyCn5IyTrFFH07U0V3MyJx+wvZKOkV+fuIRZlaVrQAx2ybPu0 X-Received: by 2002:a50:bb24:: with SMTP id y33mr12420454ede.116.1557509878712; Fri, 10 May 2019 10:37:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1557509878; cv=none; d=google.com; s=arc-20160816; b=NzgCxpAXaarpJPww4ZiL1pfpYWbgGveitdbdD22r59g4suWLCZUzKu9LGcM4if/ZFH m79NUi9fRM7+Up1QJ+fXR7sgo60ntdWwCnUYZ7KECe5eFsFSqIx6817uhSTOnroflwYr Q3ufvlJ6kZTCSkgTGn8g4P4A4YzlqrefVtQ8I76AMEbZ58jMk/6a0q5U2xFzKWMPQ0yt HiZf/DPxkwWHhXLbFdCOgQLZEzyci/hbCU2wMswvMfZ0QJt9XbnPiv7BcP1g7Kw2oQzU NgE+1jbFtoFcfhYNIfsUNHtw8oirOzM80OoZzNpCFOQDfO4QrG3ts+u7owBpDAFOOV9W s/8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature; bh=ceT2r+izts3PAZf3K2FbPyiFmzlKg5cF+haJm18PT78=; b=0NnBuQPNmrokkGPP44cXTbFsjv6tIrlQ3xcSOQTkIkrMjlxsXmzoU9h9OLNkpC7cSO RawiGzY3RDfin4o79KcWYZRlYY/2haGUWzyWl+gy/ZHztjg57wM34bsKmZOe+mKhvALz 1VVsTqTy62nUKZc8EbKEOHJqN79qctIBh5GLJlmeoimt9VbPS5HiWWUKB+nISzOEPNN4 xuYnTQHDNZf+slviDfkM29aq+VrhlzRR7jwVSJTnZwVLkVEjRaxju21LewQl4KZ7UMet sMRY4iKxzsCG39gGypPqBgH2+OnxgTsB9sj1nwUFkzuUXDVgZAZYaiIIwjz2VMa+zEgp hmSg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=GPF7u3U1; 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 x26si4603719edm.106.2019.05.10.10.37.58 for (version=TLS1 cipher=AES128-SHA bits=128/128); Fri, 10 May 2019 10:37:58 -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=GPF7u3U1; 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]:47570 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hP9T3-000543-Fu for patch@linaro.org; Fri, 10 May 2019 13:37:57 -0400 Received: from eggs.gnu.org ([209.51.188.92]:59110) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hP9MT-0007RV-5g for qemu-devel@nongnu.org; Fri, 10 May 2019 13:31:10 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hP9MR-00066X-Qy for qemu-devel@nongnu.org; Fri, 10 May 2019 13:31:08 -0400 Received: from mail-pf1-x442.google.com ([2607:f8b0:4864:20::442]:44171) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hP9MR-000663-Kp for qemu-devel@nongnu.org; Fri, 10 May 2019 13:31:07 -0400 Received: by mail-pf1-x442.google.com with SMTP id g9so3564953pfo.11 for ; Fri, 10 May 2019 10:31:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=ceT2r+izts3PAZf3K2FbPyiFmzlKg5cF+haJm18PT78=; b=GPF7u3U1LVBVDG6A5rXKtsYXh8+nXr/1DELjYbkrEtTUnrIco4/YzrXTE3q2l4rtgV +xlrrDeJPySMHxSICNsvSwqPR1kWsJq7M6jfQrCdGdODs0TWhpl90wj2OCqH42TG6Bes oIG67b/oeNe/LPY2cR8i2wZ/ATmie7/aYc8DbfjxbBMUjzZdsqblMYSiLIEuatqdR0y1 TIKz/YF7b5cfTmbKR8cphfOC7RqaQSKdNg3P5Px7sJRhrl8b+S/FzJV2tck9DZqvNuO6 foHmxL0w7w4PyNUQNCo1km6NeSOU7cdhX86YUKQpeWJKkJG9Ew5NJOUOa/4Viv3H4uP0 /a7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ceT2r+izts3PAZf3K2FbPyiFmzlKg5cF+haJm18PT78=; b=bg9x8tDkC6UOFZH8vjLEDzaEUGIhL1LaS6IoAcAVaXW1eHmL3iEdtSq4dYb+U5eTte yxUNN+sRg5i+jZmcpm4c5KAY4PqM5WbgVruw2N0SKMR85pJ2Vg0tNklGl08SMGrqZAwP f7YCV9eh9JP2ymGIcu0hQG8nGHDvbJjczBQwt86s3tUW/pxCJqNA9Dzxpo53wbS4wRjv HbnZ7p1olC8mHU6CI9CSw8kfzamewHKSnNiWriINcI21Pd+t9Nfd0CP4oNsUxeVRTkjb +nlDl5WGeF2Avt9PCfzjvgkpcTtciodeaagK/nM7ep1ZmZ6ov0HMvzj7IfwfLkM7Vjfs WsxA== X-Gm-Message-State: APjAAAWG0pR5OHN3AGC3Mn8zRZsb0V+onmZ/UfnoXa+CfMoOHOSHEDai GkfcDMkpPrgmbs/UU522MPTROM7JZgI= X-Received: by 2002:aa7:9148:: with SMTP id 8mr15706393pfi.176.1557509466384; Fri, 10 May 2019 10:31:06 -0700 (PDT) Received: from localhost.localdomain (97-113-13-231.tukw.qwest.net. [97.113.13.231]) by smtp.gmail.com with ESMTPSA id v64sm7936792pfv.106.2019.05.10.10.31.04 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 10 May 2019 10:31:05 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Fri, 10 May 2019 10:30:36 -0700 Message-Id: <20190510173049.28171-13-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190510173049.28171-1-richard.henderson@linaro.org> References: <20190510173049.28171-1-richard.henderson@linaro.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::442 Subject: [Qemu-devel] [PATCH v6 12/25] linux-user: Initialize pseudo-random seeds for all guest cpus 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: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" When the -seed option is given, call qemu_guest_random_seed_main, putting the subsystem into deterministic mode. Pass derived seeds to each cpu created during clone; which is a no-op unless the subsystem is in deterministic mode. Reviewed-by: Laurent Vivier Reviewed-by: Philippe Mathieu-Daudé Signed-off-by: Richard Henderson --- v5: Retain srand() until last user goes away. --- linux-user/main.c | 30 +++++++++++++++++++----------- linux-user/syscall.c | 3 +++ 2 files changed, 22 insertions(+), 11 deletions(-) -- 2.17.1 diff --git a/linux-user/main.c b/linux-user/main.c index 3d2230320b..7e704845c0 100644 --- a/linux-user/main.c +++ b/linux-user/main.c @@ -34,6 +34,7 @@ #include "tcg.h" #include "qemu/timer.h" #include "qemu/envlist.h" +#include "qemu/guest-random.h" #include "elf.h" #include "trace/control.h" #include "target_elf.h" @@ -48,6 +49,7 @@ static int gdbstub_port; static envlist_t *envlist; static const char *cpu_model; static const char *cpu_type; +static const char *seed_optarg; unsigned long mmap_min_addr; unsigned long guest_base; int have_guest_base; @@ -290,15 +292,9 @@ static void handle_arg_pagesize(const char *arg) } } -static void handle_arg_randseed(const char *arg) +static void handle_arg_seed(const char *arg) { - unsigned long long seed; - - if (parse_uint_full(arg, &seed, 0) != 0 || seed > UINT_MAX) { - fprintf(stderr, "Invalid seed number: %s\n", arg); - exit(EXIT_FAILURE); - } - srand(seed); + seed_optarg = arg; } static void handle_arg_gdb(const char *arg) @@ -433,7 +429,7 @@ static const struct qemu_argument arg_table[] = { "", "run in singlestep mode"}, {"strace", "QEMU_STRACE", false, handle_arg_strace, "", "log system calls"}, - {"seed", "QEMU_RAND_SEED", true, handle_arg_randseed, + {"seed", "QEMU_RAND_SEED", true, handle_arg_seed, "", "Seed for pseudo-random number generator"}, {"trace", "QEMU_TRACE", true, handle_arg_trace, "", "[[enable=]][,events=][,file=]"}, @@ -689,8 +685,20 @@ int main(int argc, char **argv, char **envp) do_strace = 1; } - if (getenv("QEMU_RAND_SEED")) { - handle_arg_randseed(getenv("QEMU_RAND_SEED")); + if (seed_optarg == NULL) { + seed_optarg = getenv("QEMU_RAND_SEED"); + } + if (seed_optarg != NULL) { + unsigned long long seed; + + /* This will go away with the last user of rand(). */ + if (parse_uint_full(seed_optarg, &seed, 0) != 0) { + fprintf(stderr, "Invalid seed number: %s\n", seed_optarg); + exit(EXIT_FAILURE); + } + srand(seed); + + qemu_guest_random_seed_main(seed_optarg, &error_fatal); } target_environ = envlist_to_environ(envlist, NULL); diff --git a/linux-user/syscall.c b/linux-user/syscall.c index f5ff6f5dc8..96f20886ce 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -107,6 +107,7 @@ #include "uname.h" #include "qemu.h" +#include "qemu/guest-random.h" #include "fd-trans.h" #ifndef CLONE_IO @@ -5482,6 +5483,7 @@ static void *clone_func(void *arg) put_user_u32(info->tid, info->child_tidptr); if (info->parent_tidptr) put_user_u32(info->tid, info->parent_tidptr); + qemu_guest_random_seed_thread_part2(cpu->random_seed); /* Enable signals. */ sigprocmask(SIG_SETMASK, &info->sigmask, NULL); /* Signal to the parent that we're ready. */ @@ -5568,6 +5570,7 @@ static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp, initializing, so temporarily block all signals. */ sigfillset(&sigmask); sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask); + cpu->random_seed = qemu_guest_random_seed_thread_part1(); /* If this is our first additional thread, we need to ensure we * generate code for parallel execution and flush old translations.