forked from PerditionC/biefircate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.h
More file actions
122 lines (109 loc) · 4.13 KB
/
common.h
File metadata and controls
122 lines (109 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* Copyright (c) 2021 TK Chia
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the developer(s) nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef H_COMMON
#define H_COMMON
#include <inttypes.h>
#define PARA_SIZE 0x10UL /* no. of bytes in a paragraph */
#define KIBYTE 1024UL /* no. of bytes in a KiB */
#define HKIBYTE (KIBYTE / 2) /* no. of bytes in half a KiB */
#define BMEM_MAX_ADDR 0x100000ULL /* end of base memory, i.e. the
1 MiB mark */
/* Fabricate a 32-bit magic number from 4 characters. */
#define MAGIC32(a, b, c, d) \
((uint32_t)(unsigned char)(a) | \
(uint32_t)(unsigned char)(b) << 8 | \
(uint32_t)(unsigned char)(c) << 16 | \
(uint32_t)(unsigned char)(d) << 24)
/* Fabricate a 64-bit magic number from 8 characters. */
#define MAGIC64(a, b, c, d, e, f, g, h) \
((uint64_t)(unsigned char)(a) | \
(uint64_t)(unsigned char)(b) << 8 | \
(uint64_t)(unsigned char)(c) << 16 | \
(uint64_t)(unsigned char)(d) << 24 | \
(uint64_t)(unsigned char)(e) << 32 | \
(uint64_t)(unsigned char)(f) << 40 | \
(uint64_t)(unsigned char)(g) << 48 | \
(uint64_t)(unsigned char)(h) << 56)
/*
* Address range types, in the manner of BIOS int 0x15, ax = 0xe820. These
* go into bdat_mem_range_t::e820_type. Other than E820_DISABLED, the names
* are taken from the Linux 5.9.14 kernel code.
*/
#define E820_RAM 1U /* available memory */
#define E820_RESERVED 2U /* reserved memory */
#define E820_ACPI 3U /* ACPI reclaimable */
#define E820_NVS 4U /* ACPI NVS */
#define E820_UNUSABLE 5U /* bad memory */
#define E820_DISABLED 6U /* disabled memory (ACPI 6.3) */
#define E820_PMEM 7U /* persistent memory (ACPI 6.3) */
/* Wait for an asynchronous interrupt. */
static inline void hlt(void)
{
__asm volatile("hlt" : : : "memory");
}
/* Read an x86 model-specific register. */
static inline uint64_t rdmsr(uint32_t idx)
{
uint32_t hi, lo;
__asm volatile("rdmsr" : "=d" (hi), "=a" (lo)
: "c" (idx));
return (uint64_t)hi << 32 | lo;
}
/* Model-specific register numbers. */
#define MSR_APIC_BASE 0x0000001bU
#define MSR_MISC_ENABLE 0x000001a0U
#define MCEN_LCMV 0x00400000U
/* Obtain processor information. */
static inline void cpuid(uint32_t leaf, uint32_t *pa, uint32_t *pb,
uint32_t *pc, uint32_t *pd)
{
uint32_t a, b, c, d;
__asm volatile("cpuid" : "=a" (a), "=b" (b), "=c" (c), "=d" (d)
: "0" (leaf));
if (pa)
*pa = a;
if (pb)
*pb = b;
if (pc)
*pc = c;
if (pd)
*pd = d;
}
/* Bit fields in various CPUID leaves. */
#define ID1C_MON 0x00000008U /* monitor, MISC_ENABLE.LCMV, etc.
(leaf 1, ecx) */
#define ID6A_ARAT 0x00000004U /* always-on APIC timer
(leaf 6, eax) */
/* Type of a 64-bit pointer. */
#ifndef __x86_64__
typedef uint64_t ptr64_t;
#else
typedef void *ptr64_t;
#endif
#endif