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
27
28 #ifndef UNIT_TEST_H_INCLUDED
29 #define UNIT_TEST_H_INCLUDED
30
31 #ifdef _KERNEL
32 #error "Random unit tests cannot be compiled into the kernel."
33 #endif
34
35 #include <sys/types.h>
36 #include <inttypes.h>
37 #include <stdint.h>
38
39 #ifndef __LITEOS__
40 #if defined(clang) && __has_builtin(__builtin_readcyclecounter)
41 #define rdtsc __builtin_readcyclecounter
42 #else /* !clang */
43 #if defined(__amd64__) || defined(__i386__)
44 static __inline uint64_t
rdtsc(void)45 rdtsc(void)
46 {
47 uint32_t low, high;
48
49 __asm __volatile("rdtsc" : "=a" (low), "=d" (high));
50 return (low | ((uint64_t)high << 32));
51 }
52 #else /* __amd64__ || __i386__ */
53 #error "No rdtsc() implementation available."
54 #endif /* __amd64__ || __i386__ */
55 #endif /* !clang */
56 #endif
57
58 static __inline uint64_t
get_cyclecount(void)59 get_cyclecount(void)
60 {
61 #ifndef __LITEOS__
62 return (rdtsc());
63 #else
64 return 0;
65 #endif
66 }
67
68 #define HARVESTSIZE 2
69 #define RANDOM_BLOCKSIZE 16
70
71 enum random_entropy_source {
72 RANDOM_START = 0,
73 RANDOM_CACHED = 0,
74 ENTROPYSOURCE = 32
75 };
76
77 struct harvest_event {
78 uintmax_t he_somecounter; /* fast counter for clock jitter */
79 uint32_t he_entropy[HARVESTSIZE];/* some harvested entropy */
80 u_int he_size; /* harvested entropy byte count */
81 u_int he_bits; /* stats about the entropy */
82 u_int he_destination; /* destination pool of this entropy */
83 enum random_entropy_source he_source; /* origin of the entropy */
84 void * he_next; /* next item on the list */
85 };
86
87 struct sysctl_ctx_list;
88
89 #define CTASSERT(x) _Static_assert(x, "compile-time assertion failed")
90 #define KASSERT(exp,msg) do { \
91 if (!(exp)) { \
92 printf msg; \
93 exit(0); \
94 } \
95 } while (0)
96
97 #endif /* UNIT_TEST_H_INCLUDED */
98