• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*-
2  * Copyright (c) 2013-2015 Mark R V Murray
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD: releng/12.2/sys/dev/random/unit_test.h 338324 2018-08-26 12:51:46Z markm $
27  */
28 
29 
30 #ifndef UNIT_TEST_H_INCLUDED
31 #define	UNIT_TEST_H_INCLUDED
32 
33 #ifdef _KERNEL
34 #error "Random unit tests cannot be compiled into the kernel."
35 #endif
36 
37 #include <sys/types.h>
38 #include <inttypes.h>
39 #include <stdint.h>
40 
41 #ifndef __LITEOS__
42 #if defined(clang) && __has_builtin(__builtin_readcyclecounter)
43 #define	rdtsc __builtin_readcyclecounter
44 #else /* !clang */
45 #if defined(__amd64__) || defined(__i386__)
46 static __inline uint64_t
rdtsc(void)47 rdtsc(void)
48 {
49 	uint32_t low, high;
50 
51 	__asm __volatile("rdtsc" : "=a" (low), "=d" (high));
52 	return (low | ((uint64_t)high << 32));
53 }
54 #else /* __amd64__ || __i386__ */
55 #error "No rdtsc() implementation available."
56 #endif /* __amd64__ || __i386__ */
57 #endif /* !clang */
58 #endif
59 
60 static __inline uint64_t
get_cyclecount(void)61 get_cyclecount(void)
62 {
63 #ifndef __LITEOS__
64 	return (rdtsc());
65 #else
66    return 0;
67 #endif
68 }
69 
70 #define	HARVESTSIZE	2
71 #define RANDOM_BLOCKSIZE	16
72 
73 enum random_entropy_source {
74 	RANDOM_START = 0,
75 	RANDOM_CACHED = 0,
76 	ENTROPYSOURCE = 32
77 };
78 
79 struct harvest_event {
80 	uintmax_t			he_somecounter;		/* fast counter for clock jitter */
81 	uint32_t			he_entropy[HARVESTSIZE];/* some harvested entropy */
82 	u_int				he_size;		/* harvested entropy byte count */
83 	u_int				he_bits;		/* stats about the entropy */
84 	u_int				he_destination;		/* destination pool of this entropy */
85 	enum random_entropy_source	he_source;		/* origin of the entropy */
86 	void *				he_next;		/* next item on the list */
87 };
88 
89 struct sysctl_ctx_list;
90 
91 #define	CTASSERT(x)	_Static_assert(x, "compile-time assertion failed")
92 #define	KASSERT(exp,msg) do {	\
93 	if (!(exp)) {		\
94 		printf msg;	\
95 		exit(0);	\
96 	}			\
97 } while (0)
98 
99 #endif /* UNIT_TEST_H_INCLUDED */
100