|
| 1 | +/* |
| 2 | + * Copyright (c) 2011 Intel Corporation |
| 3 | + * Copyright (c) 2018 Kryptos Logic |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | + * this list of conditions and the following disclaimer. |
| 10 | + * |
| 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 | + * 3. Neither the name of the copyright holder nor the names of its |
| 16 | + * contributors may be used to endorse or promote products derived from |
| 17 | + * this software without specific prior written permission. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 23 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | + * POSSIBILITY OF SUCH DAMAGE. |
| 30 | + */ |
| 31 | + |
| 32 | +#ifndef HAX_LINUX_HAX_LINUX_H_ |
| 33 | +#define HAX_LINUX_HAX_LINUX_H_ |
| 34 | + |
| 35 | +#define HAX_RAM_ENTRY_SIZE 0x4000000 |
| 36 | + |
| 37 | +hax_spinlock *hax_spinlock_alloc_init(void); |
| 38 | +void hax_spinlock_free(hax_spinlock *lock); |
| 39 | +void hax_spin_lock(hax_spinlock *lock); |
| 40 | +void hax_spin_unlock(hax_spinlock *lock); |
| 41 | + |
| 42 | +hax_mutex hax_mutex_alloc_init(void); |
| 43 | +void hax_mutex_lock(hax_mutex lock); |
| 44 | +void hax_mutex_unlock(hax_mutex lock); |
| 45 | +void hax_mutex_free(hax_mutex lock); |
| 46 | + |
| 47 | +/* Return true if the bit is set already */ |
| 48 | +int hax_test_and_set_bit(int bit, uint64_t *memory); |
| 49 | + |
| 50 | +/* Return true if the bit is cleared already */ |
| 51 | +int hax_test_and_clear_bit(int bit, uint64_t *memory); |
| 52 | + |
| 53 | +/* Don't care for the big endian situation */ |
| 54 | +static inline bool hax_test_bit(int bit, uint64_t *memory) |
| 55 | +{ |
| 56 | + int byte = bit / 8; |
| 57 | + unsigned char *p; |
| 58 | + int offset = bit % 8; |
| 59 | + |
| 60 | + p = (unsigned char *)memory + byte; |
| 61 | + return !!(*p & (1 << offset)); |
| 62 | +} |
| 63 | + |
| 64 | +// memcpy_s() is part of the optional Bounds Checking Interfaces specified in |
| 65 | +// Annex K of the C11 standard: |
| 66 | +// http://en.cppreference.com/w/c/string/byte/memcpy |
| 67 | +// However, it is not implemented by Clang: |
| 68 | +// https://stackoverflow.com/questions/40829032/how-to-install-c11-compiler-on-mac-os-with-optional-string-functions-included |
| 69 | +// Provide a simplified implementation here so memcpy_s() can be used instead of |
| 70 | +// memcpy() everywhere else, which helps reduce the number of Klocwork warnings. |
| 71 | +static inline int memcpy_s(void *dest, size_t destsz, const void *src, |
| 72 | + size_t count) |
| 73 | +{ |
| 74 | + char *dest_start = (char *)dest; |
| 75 | + char *dest_end = (char *)dest + destsz; |
| 76 | + char *src_start = (char *)src; |
| 77 | + char *src_end = (char *)src + count; |
| 78 | + bool overlap; |
| 79 | + |
| 80 | + if (count == 0) |
| 81 | + return 0; |
| 82 | + |
| 83 | + if (!dest || destsz == 0) |
| 84 | + return -EINVAL; |
| 85 | + |
| 86 | + overlap = src_start < dest_start |
| 87 | + ? dest_start < src_end : src_start < dest_end; |
| 88 | + if (!src || count > destsz || overlap) { |
| 89 | + memset(dest, 0, destsz); |
| 90 | + return -EINVAL; |
| 91 | + } |
| 92 | + |
| 93 | + memcpy(dest, src, count); |
| 94 | + return 0; |
| 95 | +} |
| 96 | + |
| 97 | +/* Why it's a bool? Strange */ |
| 98 | +bool hax_cmpxchg32(uint32_t old_val, uint32_t new_val, volatile uint32_t *addr); |
| 99 | +bool hax_cmpxchg64(uint64_t old_val, uint64_t new_val, volatile uint64_t *addr); |
| 100 | + |
| 101 | +static inline bool cpu_is_online(int cpu) |
| 102 | +{ |
| 103 | + if (cpu < 0 || cpu >= max_cpus) |
| 104 | + return 0; |
| 105 | + return !!(((mword)1 << cpu) & cpu_online_map); |
| 106 | +} |
| 107 | + |
| 108 | +int hax_notify_host_event(enum hax_notify_event event, uint32_t *param, |
| 109 | + uint32_t size); |
| 110 | + |
| 111 | +extern int default_hax_log_level; |
| 112 | + |
| 113 | +void hax_error(char *fmt, ...); |
| 114 | +void hax_warning(char *fmt, ...); |
| 115 | +void hax_info(char *fmt, ...); |
| 116 | +void hax_debug(char *fmt, ...); |
| 117 | +void hax_log(char *fmt, ...); |
| 118 | + |
| 119 | +#define hax_log hax_info |
| 120 | + |
| 121 | +//#define hax_panic DbgPrint |
| 122 | +#define hax_panic hax_error |
| 123 | + |
| 124 | +//#define assert(condition) BUG_ON(!(condition)) |
| 125 | +void assert(bool condition); |
| 126 | + |
| 127 | +#endif // HAX_LINUX_HAX_LINUX_H_ |
0 commit comments