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