From patchwork Wed Mar 13 06:26:24 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 160204 Delivered-To: patch@linaro.org Received: by 2002:a02:5cc1:0:0:0:0:0 with SMTP id w62csp13955900jad; Tue, 12 Mar 2019 23:33:49 -0700 (PDT) X-Google-Smtp-Source: APXvYqxuMVnI06tiiJJWQwxORkd9aCp354pwlNH39HFCcW2KHY30iBOCbBaXVHUV9SdwlM9vMhNQ X-Received: by 2002:a81:7092:: with SMTP id l140mr14742714ywc.85.1552458829828; Tue, 12 Mar 2019 23:33:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552458829; cv=none; d=google.com; s=arc-20160816; b=Uf20N/pka5DnzG81IbU/tW07o3PMdvM4s4MBw3nuz0WUaDut9n26JxVB17WChiGTV5 /z6XyJM+AQ2OXuysWo7FLTTV8T6cDFj+Fj7ip1VgZb1Z05vy1MJJiM6x0HXRbiAU53wP fUxFAIwx0CcklC/spuoElatucjGBAFnYfdzlhtiqoSwkO1AiI+WBfMutYROCRa1fxJUO NMYprXIcXLp+jcjyK/AwU6HYgy5SemFXDGlyNcMq2xO8uqk7q+SIu8+ke1F+Hm+JRhyX erY2CgOq0mhpaMIzQbT+kajrgpIcFfYMUbFDQ9nXdKiZwHISh84cG+TsSsR2QVpBlzWI tM7w== 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=H1kR7KejSOEAo4SO3V5tbV5T3KpzO83g+NoqvRJ2QWw=; b=Zk0/aLBclav/K7K15fdX/8We865HTOvDWCReq7lip89GfEb3MjtoU/IBZP5iU/1VtK aTp8EsLfYzsopemZQCaezcaHDQyEsbF9gubxpyVV9Zf/GK7HsdDv9+UBi7b0yUKriqir OchiODGN3OeUjn51s2iLWr14/i70QUXtX+4nixtb3to4ekoW3L1Y0caS6N17jDIDvCnA sETVSEXbpfpOHXb5LEohpMWy3mx841n9IWbxGWgIJBRTcZRTIJbVG/L8cfjOgqkgn1ap MmzG0abBJi3f76u5aW5XZ34b05hz2eqgKRuV4fxz1IbLZu2cnme6SG/sS0YoHTYynD/2 4B2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=S0zDuzdK; 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 p184si3533346ybb.328.2019.03.12.23.33.49 for (version=TLS1 cipher=AES128-SHA bits=128/128); Tue, 12 Mar 2019 23:33:49 -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=S0zDuzdK; 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]:39238 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3xSX-0004vr-Dg for patch@linaro.org; Wed, 13 Mar 2019 02:33:49 -0400 Received: from eggs.gnu.org ([209.51.188.92]:53889) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3xLg-0000f2-Ur for qemu-devel@nongnu.org; Wed, 13 Mar 2019 02:26:46 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h3xLd-00020W-QL for qemu-devel@nongnu.org; Wed, 13 Mar 2019 02:26:44 -0400 Received: from mail-pf1-x441.google.com ([2607:f8b0:4864:20::441]:44559) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h3xLb-0001z8-JO for qemu-devel@nongnu.org; Wed, 13 Mar 2019 02:26:40 -0400 Received: by mail-pf1-x441.google.com with SMTP id a3so646105pff.11 for ; Tue, 12 Mar 2019 23:26:35 -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=H1kR7KejSOEAo4SO3V5tbV5T3KpzO83g+NoqvRJ2QWw=; b=S0zDuzdK22q4q3mWgO+5DpUFb8pbei00ekirao9yNGHgtTi/50t373vCGn7hvtUuTd tqf1qD2xPcy503eH0svjOd/oO8SeIFS9zqsz7cdmuO7uTliLPvGXZGpHntxwh+9615NS RMrItAUW4abDGgVs1Oi9fdnlcBsolNYBsNxy8IG4YKOSdEnQVQuxnfoOxmamy9e/Mxdq kGGPEAVjxTyYce/lxx0XgUqRl5PQGhMBUHeQLbnaIJW+sposFufHdAODdRmSG7D81dfi JuwTDPWxNtWIodhsveVdQ2FywUQnyx73xUBzM4EjDYnAoNyXuWw3PyAHIis5Y+qbpSaT okBQ== 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=H1kR7KejSOEAo4SO3V5tbV5T3KpzO83g+NoqvRJ2QWw=; b=M4CGVyPsf66dE3ATNWxnQHB06h0M8EyMVq1oZ6G/DdxuyL0/Kiu8g9c+laM/RC6dYg LzOpLQ5LbLxIOWL8WFPFFHe0MWobbYOB8Xfmmat4NGgyEiJ2CnyWa7OvWzyVeTHqD0fh yiRUN1RSbntHo2MwOcYQin7Ztt4+v03MB6PY9HewyvNxufx/c3FoaJeICpWNnk+8i6yf uojfogL1sIdnGmsIW8LtSI2q6SUKuK1fMYedn+I/YZvZ+clDWqcZb8EXhhAhb/AVV2mK ealQrPsk4T9z/mTTmr8cUva/6swC5noAR1/U3bW/emkwNEFXk/EWgR5BYIFtx64fFwrP LBnQ== X-Gm-Message-State: APjAAAXzYyIBpIaQBLjVQ2jynIhsQi0WJ5Gz9ZZRZRz4/o/wtzq4ZH0P VYMiPRi3f3Te/fZePloRwHn1nvMU09M= X-Received: by 2002:a17:902:b416:: with SMTP id x22mr44336735plr.285.1552458393888; Tue, 12 Mar 2019 23:26:33 -0700 (PDT) Received: from localhost.localdomain (97-113-188-82.tukw.qwest.net. [97.113.188.82]) by smtp.gmail.com with ESMTPSA id m64sm25593889pfi.149.2019.03.12.23.26.32 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 12 Mar 2019 23:26:33 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Tue, 12 Mar 2019 23:26:24 -0700 Message-Id: <20190313062630.30568-2-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190313062630.30568-1-richard.henderson@linaro.org> References: <20190313062630.30568-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 for-4.1 1/7] 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: , Cc: peter.maydell@linaro.org, david@redhat.com, laurent@vivier.eu, armbru@redhat.com, kraxel@redhat.com, pbonzini@redhat.com, david@gibson.dropbear.id.au 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 | 9 ++--- linux-user/syscall.c | 3 ++ util/random.c | 76 +++++++++++++++++++++++++++++++++++++++++++ vl.c | 4 +++ qemu-options.hx | 10 ++++++ util/Makefile.objs | 1 + 9 files changed, 164 insertions(+), 7 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..37300230f5 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" @@ -291,13 +292,7 @@ static void handle_arg_pagesize(const char *arg) static void handle_arg_randseed(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); + qemu_seedrandom_main(arg, &error_fatal); } static void handle_arg_gdb(const char *arg) 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..ded8725a3b --- /dev/null +++ b/util/random.c @@ -0,0 +1,76 @@ +/* + * 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" + + +/* + * While jrand48 is not technically thread safe, jrand48_r is glibc specific. + * However, the only other global state are the A and C values, which are + * otherwise constant. The only way to muck with those is with lcong48(3). + * So if we don't do that, jrand48 *is* thread-safe. + */ +static __thread uint16_t xsubi[3]; + +/* Deterministic implementation using libc functions. */ +bool qemu_getrandom(void *buf, size_t len, bool nonblock) +{ + size_t i; + uint32_t val; + + g_assert_cmpuint(len, <=, 256); + + for (i = 0; i + 4 <= len; i += 4) { + val = jrand48(xsubi); + __builtin_memcpy(buf + i, &val, 4); + } + if (i < len) { + val = jrand48(xsubi); + __builtin_memcpy(buf + i, &val, len - i); + } + + 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) +{ + xsubi[0] = seed; + xsubi[1] = seed >> 16; + xsubi[2] = seed >> 32; +} + +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 { + qemu_seedrandom_thread_part2(seed); + } +} + +static void __attribute__((constructor)) initialize(void) +{ + /* Make sure A and C parameters are initialized. */ + srand48(0); + qemu_seedrandom_thread_part2(time(NULL) + getpid() * 1500450271ull); +} diff --git a/vl.c b/vl.c index 027b853d92..5daf12f74a 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 8693f5fa3c..a45ae70d33 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -3598,6 +3598,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 psudorandom 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