From owner-freebsd-arm@FreeBSD.ORG Sat Aug 3 09:24:51 2013 Return-Path: Delivered-To: arm@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTP id 6E648470 for ; Sat, 3 Aug 2013 09:24:51 +0000 (UTC) (envelope-from mark@grondar.org) Received: from gromit.grondar.org (grandfather.grondar.org [IPv6:2a01:348:0:15:5d59:5c20:0:2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 36A9F2CE6 for ; Sat, 3 Aug 2013 09:24:51 +0000 (UTC) Received: from graveyard.grondar.org ([88.96.155.33] helo=gronkulator.grondar.org) by gromit.grondar.org with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.80.1 (FreeBSD)) (envelope-from ) id 1V5Y4t-000Bdt-Cl; Sat, 03 Aug 2013 10:24:48 +0100 From: Mark R V Murray Content-Type: multipart/signed; boundary="Apple-Mail=_D171F5B6-526F-4EE1-99BD-6BDA85945958"; protocol="application/pgp-signature"; micalg=pgp-sha512 Date: Sat, 3 Aug 2013 10:24:40 +0100 Subject: PATCH: get_cyclecount() on ARMv6 and better To: "freebsd-arm@freebsd.org" Message-Id: <78D22A66-86E5-43B1-ABCA-7BF14F8061AB@grondar.org> Mime-Version: 1.0 (Mac OS X Mail 6.5 \(1508\)) X-Mailer: Apple Mail (2.1508) X-SA-Score: -2.2 X-BeenThere: freebsd-arm@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Porting FreeBSD to the StrongARM Processor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 03 Aug 2013 09:24:51 -0000 --Apple-Mail=_D171F5B6-526F-4EE1-99BD-6BDA85945958 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Hi folks The CSPRNG used to drive /dev/random is Yarrow, and it needs good timing = jitter to produce decent numbers. The i86_32 and i86_64 platforms both have the TSC register, wrapped in = the get_cyclecount() inline function, but the ARM platform doesn't use = its equivalent, the CCNT register. The alternative, using system time, = loses LOTS of low-bit jitter, and is certainly worth improving upon. I'm at the early stages of testing the patch below (I only have RPi), = and would like to get some comments and reviews, please. I am very = doubtful indeed that I got the #ifdefs right - they are a bit of a = minefield! ;-) The patch returns the 32-bit CCNT register as the 64-bit quantity that = get_cyclecount() provides on all platforms; this is a very minor = problem, but I suppose I could figure out some kind of crude carry = mechanism and force the number to increment beyond 32 bits; I doubt its = worth it though, as its the low bits that provide the jitter. Thanks in advance! M --=20 Mark R V Murray Index: cpu.h =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- cpu.h (revision 253832) +++ cpu.h (working copy) @@ -5,6 +5,9 @@ #define MACHINE_CPU_H =20 #include +#ifndef _KERNEL +#include +#endif =20 void cpu_halt(void); void swi_vm(void *); @@ -13,11 +16,26 @@ static __inline uint64_t get_cyclecount(void) { +#if defined (__ARM_ARCH_7__) || \ + defined (__ARM_ARCH_7A__) || \ + defined (__ARM_ARCH_6__) || \ + defined (__ARM_ARCH_6J__) || \ + defined (__ARM_ARCH_6K__) || \ + defined (__ARM_ARCH_6T2__) || \ + defined (__ARM_ARCH_6Z__) || \ + defined (__ARM_ARCH_6ZK__) + + uint32_t ccnt; + + /* Read CCNT. Darn; its only 32 bits. */ + __asm __volatile("mrc p15, 0, %0, c9, c13, 0": "=3Dr" (ccnt)); + return ((uint64_t)ccnt); +#else struct bintime bt; =20 binuptime(&bt); return ((uint64_t)bt.sec << 56 | bt.frac >> 8); - =09 +#endif } #endif --Apple-Mail=_D171F5B6-526F-4EE1-99BD-6BDA85945958 Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename=signature.asc Content-Type: application/pgp-signature; name=signature.asc Content-Description: Message signed with OpenPGP using GPGMail -----BEGIN PGP SIGNATURE----- Version: GnuPG/MacGPG2 v2.0.20 (Darwin) Comment: GPGTools - http://gpgtools.org iQCVAwUBUfzMXt58vKOKE6LNAQoPHQQAqTiNIzalyHsXM2OnsOS+L84Ye8zeULy1 8UGin+/GBov8o50bfsxKKtNRsc32vmDdHR8kUZ6WIq+QzduGXGusHDGJZqE3dAs/ n7e2KXWzb4Oq6Rtr8LcQmLYrUsUypMrtPvn5OPaQc+IkYjoSTDgd0Hkt6xsYnl7n JCajRgHnSlg= =xwZi -----END PGP SIGNATURE----- --Apple-Mail=_D171F5B6-526F-4EE1-99BD-6BDA85945958--