From patchwork Thu Mar 14 04:55:20 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 160293 Delivered-To: patch@linaro.org Received: by 2002:a02:5cc1:0:0:0:0:0 with SMTP id w62csp15077703jad; Wed, 13 Mar 2019 22:04:42 -0700 (PDT) X-Google-Smtp-Source: APXvYqyjRjOvdRGIJi6K6Ein+RiOUE62QsdkmjomSM4/b3q9Phz7zT1SyTHRGxeEe/HWahniKeLg X-Received: by 2002:a25:a285:: with SMTP id c5mr24645857ybi.140.1552539882669; Wed, 13 Mar 2019 22:04:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552539882; cv=none; d=google.com; s=arc-20160816; b=Q18ZVvhTXe9nd8FrhO8c4BE9a69wXDgNqzqHC2WNMXBdkgurGBzc6jb9LLvbeZCe/q euxDZFoceBVNWNxMmAfHSubr9xEc2AvHRRgxDbrG8qpsxTzT34rN6dnfZRp/104Skid6 M7f9eMrgGSoszzPwsdit12JV55sQxK6unD6sfbhIK7KBG7ZaA7PVu7ZaTNhi6rxBIxyC 5MkPEmP6fQEyITM0dCNoea5XbWwwkST+CpcYPSMguVdw6LPcDLkAsVQs6Ml2MCp2fmdm SY31ha+PfxxzrBz10BK5mVSvBL/lq3+GYMfRWfcx52Qvwda+28Q1QVtuFhE//pjFagZy rPOQ== 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:references:in-reply-to :message-id:date:to:from:dkim-signature; bh=r/9fu8oUAgIeYStikSu5RD4Xo7mI5WjXjD4LSh4V41I=; b=IiHukV/rbNR8urVZaFrvXZkp31YhK1sDB7XGvRUhOMsMnoKM7k7gA5/A8BbIyVGqXg uRN1TtisfbXHbbLvNwbttbrQwve3S3RrLOs0kYIMOc57ad6Z2OZtrxVGvo/wVFZ62c+A SZzh/kp5bJD/4NECZtqualUdBJ2dJEoI5rgfH26eYMe/sswbP/s8IgAmr7UlZdXk0iKA L9O09PfduxcybmL/ZiBMmuyYCqwieaKCTUkxalET3xsKqLo7gNEft7e2rNLCef8BdlvU kjAyOiil9bswMZW1II3Y8Ett6vbYuz19Xfp+ccae9dptkuyU7YpSxYoqRt1WDqiBYT0W FpNQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=tiddGgJB; 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 7si7981192ybc.200.2019.03.13.22.04.42 for (version=TLS1 cipher=AES128-SHA bits=128/128); Wed, 13 Mar 2019 22:04:42 -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=tiddGgJB; 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]:56953 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h4IXq-0002rn-6B for patch@linaro.org; Thu, 14 Mar 2019 01:04:42 -0400 Received: from eggs.gnu.org ([209.51.188.92]:56192) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h4IP8-0004Xe-E5 for qemu-devel@nongnu.org; Thu, 14 Mar 2019 00:55:44 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h4IP6-0007PF-9D for qemu-devel@nongnu.org; Thu, 14 Mar 2019 00:55:42 -0400 Received: from mail-pf1-x441.google.com ([2607:f8b0:4864:20::441]:36605) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h4IP5-0007Ou-Ta for qemu-devel@nongnu.org; Thu, 14 Mar 2019 00:55:40 -0400 Received: by mail-pf1-x441.google.com with SMTP id n22so3014169pfa.3 for ; Wed, 13 Mar 2019 21:55:39 -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; bh=r/9fu8oUAgIeYStikSu5RD4Xo7mI5WjXjD4LSh4V41I=; b=tiddGgJBgeKZrs3fcr94TxtqQkLDjLqGH4nZMKQ5yEuNlrNVt8BeK6Cwkp/grX4FSn pSq+cvp8/6lanki/w+tinpJMF8t0/W6Gy94y2gtqynw8oHBU+v2KcQOyALZohG/A5gFj HFvS85elwcIZ0gH2g+teATPn+VX47qT1kEcdMR7TedsLFv//VVE91iw/F7chxUiN+Hle xg3vm0OHRQFLLkH0aIcDkaINA9mLQfzrMsMneXKlo3CJKEzKD813AIYClMfG4pCov+YM LyvF4qobPDGBSOSvF2ajThrZrvvJqlQbBBT1UEYP9p6YyrNoaeN1QIPRBBuRvoTEUNp9 VKCA== 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; bh=r/9fu8oUAgIeYStikSu5RD4Xo7mI5WjXjD4LSh4V41I=; b=stYP670uQIpeyMZpa0ZuA/ZUdye0qg8fC8+wXMSix4QOVofNKo7DrWbhiDWOkQkon6 xwWTHXiIeo5YgVolpUO/B7gTxktm8aZ0qaM/MYTDto6l+/UGvXBMKAcq4aZ4+c2Q0p6h VTotl75D4N5YJkisX7/LwFt35NOI9rjac4CaJY8uCmhB0Y4+Tc2lYkzC5yyZ3Kyc3+2d baPhagDy5r5+TTSdqAWnU7NRtNNBtdbeHeiMeFUNf1Ng22dDUv2W2uXcUOuuo3Oi1qrk oICkycZL7TbafxREgoDbMPXSI5EQkmzX+I+0lGhtQO7bqN7Ja3wG+Gc8q4+MJ9ve5nZh fVvg== X-Gm-Message-State: APjAAAUEc3P0k6hGJqeIU/mV4vAhOx0QOjhuLpUbw2HbBoXA2HwTlDcI 5lQgxMlG+UGKBa8zG9DP6OSrtpba2IA= X-Received: by 2002:a63:4287:: with SMTP id p129mr25071919pga.84.1552539338273; Wed, 13 Mar 2019 21:55:38 -0700 (PDT) Received: from localhost.localdomain (97-113-188-82.tukw.qwest.net. [97.113.188.82]) by smtp.gmail.com with ESMTPSA id d9sm16185027pgq.6.2019.03.13.21.55.37 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 13 Mar 2019 21:55:37 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Wed, 13 Mar 2019 21:55:20 -0700 Message-Id: <20190314045526.13342-9-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190314045526.13342-1-richard.henderson@linaro.org> References: <20190314045526.13342-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::441 Subject: [Qemu-devel] [PATCH v2 08/14] util: Add qemu_getrandom and support functions 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" Initialize the system from system and linux-user arguments. Propagate deterministic seeds when creating new cpu threads. Signed-off-by: Richard Henderson --- include/qemu/random.h | 58 +++++++++++++++++++++++++++++++++++ include/qom/cpu.h | 1 + cpus.c | 9 ++++++ linux-user/main.c | 21 ++++++------- linux-user/syscall.c | 3 ++ util/random.c | 71 +++++++++++++++++++++++++++++++++++++++++++ vl.c | 4 +++ qemu-options.hx | 10 ++++++ util/Makefile.objs | 1 + 9 files changed, 167 insertions(+), 11 deletions(-) create mode 100644 include/qemu/random.h create mode 100644 util/random.c -- 2.17.1 diff --git a/include/qemu/random.h b/include/qemu/random.h new file mode 100644 index 0000000000..9d88008288 --- /dev/null +++ b/include/qemu/random.h @@ -0,0 +1,58 @@ +/* + * QEMU random functions + * + * Copyright 2019 Linaro, Ltd. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + */ + +#ifndef QEMU_RANDOM_H +#define QEMU_RANDOM_H + +/** + * qemu_seedrandom_main(const char *optarg, Error **errp) + * @optarg: a non-NULL pointer to a C string + * @errp: an Error handler + * + * The @optarg value is that which accompanies the -seed argument. + * This forces qemu_getrandom into deterministic mode. + */ +void qemu_seedrandom_main(const char *optarg, Error **errp); + +/** + * qemu_seedrandom_thread_part1(void) + * + * If qemu_getrandom is in deterministic mode, returns an + * independant seed for the new thread. Otherwise returns 0. + */ +uint64_t qemu_seedrandom_thread_part1(void); + +/** + * qemu_seedrandom_thread_part2(uint64_t seed) + * @seed: a value for the new thread. + * + * If qemu_getrandom is in deterministic mode, this stores an + * independant seed for the new thread. Otherwise a no-op. + */ +void qemu_seedrandom_thread_part2(uint64_t seed); + +/** + * qemu_getrandom(void *buf, size_t len, bool nonblock) + * @buf: a buffer of bytes to be written + * @len: the number of bytes in @buf + * @nonblock: do not delay if the entropy pool is low + * + * Fills len bytes in buf with random data. If nonblock is false, + * this may require a delay while the entropy pool fills. Returns + * true if the call is successful, but the only non-successful case + * is when nonblock is true. + * + * The value of len must be <= 256, so that the BSD getentropy(3) + * function can be used to implement this. + */ +bool qemu_getrandom(void *buf, size_t len, bool nonblock); + +#endif /* QEMU_RANDOM_H */ diff --git a/include/qom/cpu.h b/include/qom/cpu.h index 1d6099e5d4..343cc6d51e 100644 --- a/include/qom/cpu.h +++ b/include/qom/cpu.h @@ -372,6 +372,7 @@ struct CPUState { int singlestep_enabled; int64_t icount_budget; int64_t icount_extra; + uint64_t random_seed; sigjmp_buf jmp_env; QemuMutex work_mutex; diff --git a/cpus.c b/cpus.c index e83f72b48b..b5d3f46220 100644 --- a/cpus.c +++ b/cpus.c @@ -49,6 +49,7 @@ #include "qemu/option.h" #include "qemu/bitmap.h" #include "qemu/seqlock.h" +#include "qemu/random.h" #include "tcg.h" #include "hw/nmi.h" #include "sysemu/replay.h" @@ -1275,6 +1276,7 @@ static void *qemu_kvm_cpu_thread_fn(void *arg) /* signal CPU creation */ cpu->created = true; qemu_cond_signal(&qemu_cpu_cond); + qemu_seedrandom_thread_part2(cpu->random_seed); do { if (cpu_can_run(cpu)) { @@ -1318,6 +1320,7 @@ static void *qemu_dummy_cpu_thread_fn(void *arg) /* signal CPU creation */ cpu->created = true; qemu_cond_signal(&qemu_cpu_cond); + qemu_seedrandom_thread_part2(cpu->random_seed); do { qemu_mutex_unlock_iothread(); @@ -1477,6 +1480,7 @@ static void *qemu_tcg_rr_cpu_thread_fn(void *arg) cpu->created = true; cpu->can_do_io = 1; qemu_cond_signal(&qemu_cpu_cond); + qemu_seedrandom_thread_part2(cpu->random_seed); /* wait for initial kick-off after machine start */ while (first_cpu->stopped) { @@ -1591,6 +1595,7 @@ static void *qemu_hax_cpu_thread_fn(void *arg) hax_init_vcpu(cpu); qemu_cond_signal(&qemu_cpu_cond); + qemu_seedrandom_thread_part2(cpu->random_seed); do { if (cpu_can_run(cpu)) { @@ -1630,6 +1635,7 @@ static void *qemu_hvf_cpu_thread_fn(void *arg) /* signal CPU creation */ cpu->created = true; qemu_cond_signal(&qemu_cpu_cond); + qemu_seedrandom_thread_part2(cpu->random_seed); do { if (cpu_can_run(cpu)) { @@ -1670,6 +1676,7 @@ static void *qemu_whpx_cpu_thread_fn(void *arg) /* signal CPU creation */ cpu->created = true; qemu_cond_signal(&qemu_cpu_cond); + qemu_seedrandom_thread_part2(cpu->random_seed); do { if (cpu_can_run(cpu)) { @@ -1723,6 +1730,7 @@ static void *qemu_tcg_cpu_thread_fn(void *arg) cpu->can_do_io = 1; current_cpu = cpu; qemu_cond_signal(&qemu_cpu_cond); + qemu_seedrandom_thread_part2(cpu->random_seed); /* process any pending work */ cpu->exit_request = 1; @@ -2070,6 +2078,7 @@ void qemu_init_vcpu(CPUState *cpu) cpu->nr_cores = smp_cores; cpu->nr_threads = smp_threads; cpu->stopped = true; + cpu->random_seed = qemu_seedrandom_thread_part1(); if (!cpu->as) { /* If the target cpu hasn't set up any address spaces itself, diff --git a/linux-user/main.c b/linux-user/main.c index a0aba9cb1e..9682e81610 100644 --- a/linux-user/main.c +++ b/linux-user/main.c @@ -33,6 +33,7 @@ #include "tcg.h" #include "qemu/timer.h" #include "qemu/envlist.h" +#include "qemu/random.h" #include "elf.h" #include "trace/control.h" #include "target_elf.h" @@ -47,6 +48,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; @@ -289,15 +291,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) @@ -432,7 +428,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=]"}, @@ -687,8 +683,11 @@ 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) { + qemu_seedrandom_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 208fd1813d..18d98f5a08 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -110,6 +110,7 @@ #include "uname.h" #include "qemu.h" +#include "qemu/random.h" #include "fd-trans.h" #ifndef CLONE_IO @@ -5448,6 +5449,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_seedrandom_thread_part2(cpu->random_seed); /* Enable signals. */ sigprocmask(SIG_SETMASK, &info->sigmask, NULL); /* Signal to the parent that we're ready. */ @@ -5534,6 +5536,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_seedrandom_thread_part1(); /* If this is our first additional thread, we need to ensure we * generate code for parallel execution and flush old translations. diff --git a/util/random.c b/util/random.c new file mode 100644 index 0000000000..467c987a66 --- /dev/null +++ b/util/random.c @@ -0,0 +1,71 @@ +/* + * QEMU random functions + * + * Copyright 2019 Linaro, Ltd. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + */ + +#include "qemu/osdep.h" +#include "qemu-common.h" +#include "qemu/cutils.h" +#include "qapi/error.h" +#include "qemu/random.h" + +static __thread GRand *thread_rand; + +/* Deterministic implementation using Glib's Mersenne Twister. */ +bool qemu_getrandom(void *buf, size_t len, bool nonblock) +{ + GRand *rand; + size_t i; + uint32_t x; + + g_assert(len <= 256); + + rand = thread_rand; + if (unlikely(rand == NULL)) { + /* Thread not initialized for a cpu, or main w/o -seed. */ + thread_rand = rand = g_rand_new(); + } + + for (i = 0; i + 4 <= len; i += 4) { + x = g_rand_int(rand); + __builtin_memcpy(buf + i, &x, 4); + } + if (i < len) { + x = g_rand_int(rand); + __builtin_memcpy(buf + i, &x, i - len); + } + + return true; +} + +uint64_t qemu_seedrandom_thread_part1(void) +{ + uint64_t ret; + qemu_getrandom(&ret, sizeof(ret), false); + return ret; +} + +void qemu_seedrandom_thread_part2(uint64_t seed) +{ + g_assert(thread_rand == NULL); + thread_rand = g_rand_new_with_seed_array((const guint32 *)&seed, + sizeof(seed) / sizeof(guint32)); +} + +void qemu_seedrandom_main(const char *optarg, Error **errp) +{ + unsigned long long seed; + if (parse_uint_full(optarg, &seed, 0)) { + error_setg(errp, "Invalid seed number: %s", optarg); + } else { + g_assert(thread_rand != NULL); + g_rand_set_seed_array(thread_rand, (const guint32 *)&seed, + sizeof(seed) / sizeof(guint32)); + } +} diff --git a/vl.c b/vl.c index c1d5484e12..0438b72f95 100644 --- a/vl.c +++ b/vl.c @@ -128,6 +128,7 @@ int main(int argc, char **argv) #include "qapi/qapi-commands-ui.h" #include "qapi/qmp/qerror.h" #include "sysemu/iothread.h" +#include "qemu/random.h" #define MAX_VIRTIO_CONSOLES 1 @@ -3330,6 +3331,9 @@ int main(int argc, char **argv, char **envp) case QEMU_OPTION_DFILTER: qemu_set_dfilter_ranges(optarg, &error_fatal); break; + case QEMU_OPTION_seed: + qemu_seedrandom_main(optarg, &error_fatal); + break; case QEMU_OPTION_s: add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT); break; diff --git a/qemu-options.hx b/qemu-options.hx index 08749a3391..ed70de67d8 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -3601,6 +3601,16 @@ the 0x200 sized block starting at 0xffffffc000080000 and another 0x1000 sized block starting at 0xffffffc00005f000. ETEXI +DEF("seed", HAS_ARG, QEMU_OPTION_seed, \ + "-seed number seed the pseudo-random number generator\n", + QEMU_ARCH_ALL) +STEXI +@item -seed @var{number} +@findex -seed +Force qemu to use a deterministic pseudo-random number generator, +seeded with @var{number}. +ETEXI + DEF("L", HAS_ARG, QEMU_OPTION_L, \ "-L path set the directory for the BIOS, VGA BIOS and keymaps\n", QEMU_ARCH_ALL) diff --git a/util/Makefile.objs b/util/Makefile.objs index 835fcd69e2..bc7405c535 100644 --- a/util/Makefile.objs +++ b/util/Makefile.objs @@ -53,5 +53,6 @@ util-obj-y += iova-tree.o util-obj-$(CONFIG_INOTIFY1) += filemonitor-inotify.o util-obj-$(CONFIG_LINUX) += vfio-helpers.o util-obj-$(CONFIG_OPENGL) += drm.o +util-obj-y += random.o stub-obj-y += filemonitor-stub.o