From patchwork Thu Dec 2 11:53:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 12694499 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id BBEC3C433F5 for ; Thu, 2 Dec 2021 11:57:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=jzAKHGyzIzb2hY9znqa5F0Yx86eyE+ATa/ZwF4AsfqM=; b=IWdrRynt2+Pi9x as3KhNIjv/FWX4QaavibU9oFS4y8N4xbdtnEKky79tRWUrSC+lBieg3cE14MEoRiqgFjejMt3ZC/9 sEitfWGtZ9NmMAzqptdqVpWeacgxkrQGs0gC8b2UlQ3yXLM4warf/Sbo00hunk/G2LYhf9H98O/Mt fGhYXT4EH6YvarJLQsPZa4C946NxEdYgyUcKBruZn0xAKhJQqfqEANVsDDSdv1rJBodrsRvBHwYDH UsqEm8gSxgvqf083a7msHzmv2ZYjlP/3xJULndB1m5fpuyRERbLldJW4kHMNTMKVZeWuWZeHa/Cv2 3nqjfiun1mUJTqraYkWw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mskgG-00C5DV-HC; Thu, 02 Dec 2021 11:55:17 +0000 Received: from mail-wm1-x336.google.com ([2a00:1450:4864:20::336]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mskf3-00C4gS-1z for linux-arm-kernel@lists.infradead.org; Thu, 02 Dec 2021 11:54:03 +0000 Received: by mail-wm1-x336.google.com with SMTP id 137so22786911wma.1 for ; Thu, 02 Dec 2021 03:54:00 -0800 (PST) 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 :mime-version:content-transfer-encoding; bh=rKZGIAkHy6Wk8HmnCBA18OX+dHqy4xS8aZv10RY6IUY=; b=MblWz4yZRq+L8PlG0X2he7aOI50DRfzcKKh4ACKITTzWX30sgubF6ilsdFMxEd/cQS xFLZ1BTlHOfMHh7CQxspuYRuGrJUrEyCvOIdhDFnTchqezn9PzrH9ckJ+CWok8XovBBD K6+gyy2yx9LT7mKq+egq+hEJEA87NYXr0Xi68A+e9Jr/mC4QLFDTUP6kiccMr/kmIrQ5 ZwnzKuoQxybIktQMFBSxV4pnQTXf8+pGK4dQrO/AnVuKPsB9iYnb0zmN9Eg/ksvIscNO JrgkSwSh0NWBhv3zwQQKWrbfxNxvltgXoO4f6xtYCxyiOaMY6P8Ovm/lKTgo22JgZ7dg hG4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=rKZGIAkHy6Wk8HmnCBA18OX+dHqy4xS8aZv10RY6IUY=; b=WAzOPmQ1ml96hyJ9bVEA5cbOrYpkT/mXKjvIqsVLGG0g1P8NqI6A/ajDx2vDwzi7QZ akCYUr4xH4U2NeuittDqPorCGvI7nhWNv4n52lmsbmO9EZhFw1B1nmqF34tnuGKt1JsR GpQ9nDwqFeGhIWIQJbbfCc1zx7GpGrw4MNVJdJjENi8TLtUInMYL79bcgM/lV883Jx+I lUGkTQ83UZHY56fD/SV1R2wpOrOl1Y0+cv66mfd2v002LHCeU/vw/ik9tax41SadN36B w31jno3zlO898XB6tt/U+NT7XfTQphAAqiWwogY0l+N4Ja2KUrRemPxIOMmDjaPNI3MZ EDlg== X-Gm-Message-State: AOAM533wGLZmx2LspmrSn+dpJkmmCD5Vg+A84xogn+yM7MQQwH4JPbh1 dputihP3MTwtAD7rK8uwXnB4jA== X-Google-Smtp-Source: ABdhPJwN0YmW7tIvGU7I4fBuMFiBCDaW5OLUlQiHzPXrTIhlhJE6apr185FR6mYBG1nfWLphgaXOBg== X-Received: by 2002:a05:600c:1993:: with SMTP id t19mr5895466wmq.21.1638446039503; Thu, 02 Dec 2021 03:53:59 -0800 (PST) Received: from zen.linaroharston ([51.148.130.216]) by smtp.gmail.com with ESMTPSA id b188sm1880708wmd.45.2021.12.02.03.53.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Dec 2021 03:53:54 -0800 (PST) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id BC5791FF9B; Thu, 2 Dec 2021 11:53:52 +0000 (GMT) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: pbonzini@redhat.com, drjones@redhat.com, thuth@redhat.com Cc: kvm@vger.kernel.org, qemu-arm@nongnu.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, christoffer.dall@arm.com, maz@kernel.org, =?utf-8?q?Alex_Benn=C3=A9e?= , "Timothy B . Terriberry" Subject: [kvm-unit-tests PATCH v9 4/9] lib: add isaac prng library from CCAN Date: Thu, 2 Dec 2021 11:53:47 +0000 Message-Id: <20211202115352.951548-5-alex.bennee@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211202115352.951548-1-alex.bennee@linaro.org> References: <20211202115352.951548-1-alex.bennee@linaro.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211202_035401_189797_20E5EE93 X-CRM114-Status: GOOD ( 24.97 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org It's often useful to introduce some sort of random variation when testing several racing CPU conditions. Instead of each test implementing some half-arsed PRNG bring in a a decent one which has good statistical randomness. Obviously it is deterministic for a given seed value which is likely the behaviour you want. I've pulled in the ISAAC library from CCAN: http://ccodearchive.net/info/isaac.html I shaved off the float related stuff which is less useful for unit testing and re-indented to fit the style. The original license was CC0 (Public Domain) which is compatible with the LGPL v2 of kvm-unit-tests. Signed-off-by: Alex Bennée CC: Timothy B. Terriberry Acked-by: Andrew Jones Message-Id: <20211118184650.661575-6-alex.bennee@linaro.org> Acked-by: Thomas Huth --- arm/Makefile.common | 1 + lib/prng.h | 82 ++++++++++++++++++++++ lib/prng.c | 162 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 245 insertions(+) create mode 100644 lib/prng.h create mode 100644 lib/prng.c diff --git a/arm/Makefile.common b/arm/Makefile.common index 38385e0c..99bcf3fc 100644 --- a/arm/Makefile.common +++ b/arm/Makefile.common @@ -44,6 +44,7 @@ cflatobjs += lib/pci-testdev.o cflatobjs += lib/virtio.o cflatobjs += lib/virtio-mmio.o cflatobjs += lib/chr-testdev.o +cflatobjs += lib/prng.o cflatobjs += lib/arm/io.o cflatobjs += lib/arm/setup.o cflatobjs += lib/arm/mmu.o diff --git a/lib/prng.h b/lib/prng.h new file mode 100644 index 00000000..bf5776d1 --- /dev/null +++ b/lib/prng.h @@ -0,0 +1,82 @@ +/* + * PRNG Header + */ +#ifndef __PRNG_H__ +#define __PRNG_H__ + +# include + + + +typedef struct isaac_ctx isaac_ctx; + + + +/*This value may be lowered to reduce memory usage on embedded platforms, at + the cost of reducing security and increasing bias. + Quoting Bob Jenkins: "The current best guess is that bias is detectable after + 2**37 values for [ISAAC_SZ_LOG]=3, 2**45 for 4, 2**53 for 5, 2**61 for 6, + 2**69 for 7, and 2**77 values for [ISAAC_SZ_LOG]=8."*/ +#define ISAAC_SZ_LOG (8) +#define ISAAC_SZ (1< +#include "prng.h" + +#define ISAAC_MASK (0xFFFFFFFFU) + +/* Extract ISAAC_SZ_LOG bits (starting at bit 2). */ +static inline uint32_t lower_bits(uint32_t x) +{ + return (x & ((ISAAC_SZ-1) << 2)) >> 2; +} + +/* Extract next ISAAC_SZ_LOG bits (starting at bit ISAAC_SZ_LOG+2). */ +static inline uint32_t upper_bits(uint32_t y) +{ + return (y >> (ISAAC_SZ_LOG+2)) & (ISAAC_SZ-1); +} + +static void isaac_update(isaac_ctx *_ctx){ + uint32_t *m; + uint32_t *r; + uint32_t a; + uint32_t b; + uint32_t x; + uint32_t y; + int i; + m=_ctx->m; + r=_ctx->r; + a=_ctx->a; + b=_ctx->b+(++_ctx->c); + for(i=0;i>6)+m[i+ISAAC_SZ/2]; + m[i]=y=m[lower_bits(x)]+a+b; + r[i]=b=m[upper_bits(y)]+x; + x=m[++i]; + a=(a^a<<2)+m[i+ISAAC_SZ/2]; + m[i]=y=m[lower_bits(x)]+a+b; + r[i]=b=m[upper_bits(y)]+x; + x=m[++i]; + a=(a^a>>16)+m[i+ISAAC_SZ/2]; + m[i]=y=m[lower_bits(x)]+a+b; + r[i]=b=m[upper_bits(y)]+x; + } + for(i=ISAAC_SZ/2;i>6)+m[i-ISAAC_SZ/2]; + m[i]=y=m[lower_bits(x)]+a+b; + r[i]=b=m[upper_bits(y)]+x; + x=m[++i]; + a=(a^a<<2)+m[i-ISAAC_SZ/2]; + m[i]=y=m[lower_bits(x)]+a+b; + r[i]=b=m[upper_bits(y)]+x; + x=m[++i]; + a=(a^a>>16)+m[i-ISAAC_SZ/2]; + m[i]=y=m[lower_bits(x)]+a+b; + r[i]=b=m[upper_bits(y)]+x; + } + _ctx->b=b; + _ctx->a=a; + _ctx->n=ISAAC_SZ; +} + +static void isaac_mix(uint32_t _x[8]){ + static const unsigned char SHIFT[8]={11,2,8,16,10,4,8,9}; + int i; + for(i=0;i<8;i++){ + _x[i]^=_x[(i+1)&7]<>SHIFT[i]; + _x[(i+3)&7]+=_x[i]; + _x[(i+1)&7]+=_x[(i+2)&7]; + } +} + + +void isaac_init(isaac_ctx *_ctx,const unsigned char *_seed,int _nseed){ + _ctx->a=_ctx->b=_ctx->c=0; + memset(_ctx->r,0,sizeof(_ctx->r)); + isaac_reseed(_ctx,_seed,_nseed); +} + +void isaac_reseed(isaac_ctx *_ctx,const unsigned char *_seed,int _nseed){ + uint32_t *m; + uint32_t *r; + uint32_t x[8]; + int i; + int j; + m=_ctx->m; + r=_ctx->r; + if(_nseed>ISAAC_SEED_SZ_MAX)_nseed=ISAAC_SEED_SZ_MAX; + for(i=0;i<_nseed>>2;i++){ + r[i]^=(uint32_t)_seed[i<<2|3]<<24|(uint32_t)_seed[i<<2|2]<<16| + (uint32_t)_seed[i<<2|1]<<8|_seed[i<<2]; + } + _nseed-=i<<2; + if(_nseed>0){ + uint32_t ri; + ri=_seed[i<<2]; + for(j=1;j<_nseed;j++)ri|=(uint32_t)_seed[i<<2|j]<<(j<<3); + r[i++]^=ri; + } + x[0]=x[1]=x[2]=x[3]=x[4]=x[5]=x[6]=x[7]=0x9E3779B9U; + for(i=0;i<4;i++)isaac_mix(x); + for(i=0;in)isaac_update(_ctx); + return _ctx->r[--_ctx->n]; +} + +uint32_t isaac_next_uint(isaac_ctx *_ctx,uint32_t _n){ + uint32_t r; + uint32_t v; + uint32_t d; + do{ + r=isaac_next_uint32(_ctx); + v=r%_n; + d=r-v; + } + while(((d+_n-1)&ISAAC_MASK)