debuggers.hg

view tools/firmware/hvmloader/util.h @ 19992:8ce42378828b

hvmloader: Add new test for MSR_SHADOW_GS_BASE validity after SWAPGS instruction.

Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Thu Jul 16 10:30:50 2009 +0100 (2009-07-16)
parents f3240cd3cd2b
children 809b20f066fb 5ea096ef7603
line source
1 #ifndef __HVMLOADER_UTIL_H__
2 #define __HVMLOADER_UTIL_H__
4 #include <stdarg.h>
5 #include <stdint.h>
6 #include <xen/hvm/hvm_info_table.h>
8 #define __STR(...) #__VA_ARGS__
9 #define STR(...) __STR(__VA_ARGS__)
11 /* GDT selector values. */
12 #define SEL_CODE16 0x0008
13 #define SEL_DATA16 0x0010
14 #define SEL_CODE32 0x0018
15 #define SEL_DATA32 0x0020
16 #define SEL_CODE64 0x0028
18 #undef offsetof
19 #define offsetof(t, m) ((unsigned long)&((t *)0)->m)
21 #undef NULL
22 #define NULL ((void*)0)
24 void __assert_failed(char *assertion, char *file, int line)
25 __attribute__((noreturn));
26 #define ASSERT(p) \
27 do { if (!(p)) __assert_failed(#p, __FILE__, __LINE__); } while (0)
28 void __bug(char *file, int line) __attribute__((noreturn));
29 #define BUG() __bug(__FILE__, __LINE__)
30 #define BUG_ON(p) do { if (p) BUG(); } while (0)
31 #define BUILD_BUG_ON(p) ((void)sizeof(char[1 - 2 * !!(p)]))
33 /* MSR access */
34 void wrmsr(uint32_t idx, uint64_t v);
35 uint64_t rdmsr(uint32_t idx);
37 /* I/O output */
38 void outb(uint16_t addr, uint8_t val);
39 void outw(uint16_t addr, uint16_t val);
40 void outl(uint16_t addr, uint32_t val);
42 /* I/O input */
43 uint8_t inb(uint16_t addr);
44 uint16_t inw(uint16_t addr);
45 uint32_t inl(uint16_t addr);
47 /* CMOS access */
48 uint8_t cmos_inb(uint8_t idx);
49 void cmos_outb(uint8_t idx, uint8_t val);
51 /* APIC access */
52 uint32_t ioapic_read(uint32_t reg);
53 void ioapic_write(uint32_t reg, uint32_t val);
54 uint32_t lapic_read(uint32_t reg);
55 void lapic_write(uint32_t reg, uint32_t val);
57 /* PCI access */
58 uint32_t pci_read(uint32_t devfn, uint32_t reg, uint32_t len);
59 #define pci_readb(devfn, reg) ((uint8_t) pci_read(devfn, reg, 1))
60 #define pci_readw(devfn, reg) ((uint16_t)pci_read(devfn, reg, 2))
61 #define pci_readl(devfn, reg) ((uint32_t)pci_read(devfn, reg, 4))
62 void pci_write(uint32_t devfn, uint32_t reg, uint32_t len, uint32_t val);
63 #define pci_writeb(devfn, reg, val) (pci_write(devfn, reg, 1, (uint8_t) val))
64 #define pci_writew(devfn, reg, val) (pci_write(devfn, reg, 2, (uint16_t)val))
65 #define pci_writel(devfn, reg, val) (pci_write(devfn, reg, 4, (uint32_t)val))
67 /* Get CPU speed in MHz. */
68 uint16_t get_cpu_mhz(void);
70 /* Hardware detection. */
71 int uart_exists(uint16_t uart_base);
72 int hpet_exists(unsigned long hpet_base);
74 /* Do cpuid instruction, with operation 'idx' */
75 void cpuid(uint32_t idx, uint32_t *eax, uint32_t *ebx,
76 uint32_t *ecx, uint32_t *edx);
78 /* Read the TSC register. */
79 static inline uint64_t rdtsc(void)
80 {
81 uint64_t tsc;
82 asm volatile ( "rdtsc" : "=A" (tsc) );
83 return tsc;
84 }
86 /* Relax the CPU and let the compiler know that time passes. */
87 static inline void cpu_relax(void)
88 {
89 asm volatile ( "rep ; nop" : : : "memory" );
90 }
92 /* Memory barriers. */
93 #define barrier() asm volatile ( "" : : : "memory" )
94 #define rmb() barrier()
95 #define wmb() barrier()
96 #define mb() asm volatile ( "lock; addl $0,0(%%esp)" : : : "memory" )
98 /*
99 * Divide a 64-bit dividend by a 32-bit divisor.
100 * (1) Overwrites the 64-bit dividend _in_place_ with the quotient
101 * (2) Returns the 32-bit remainder
102 */
103 #define do_div(n, base) ({ \
104 unsigned long __upper, __low, __high, __mod, __base; \
105 __base = (base); \
106 asm ( "" : "=a" (__low), "=d" (__high) : "A" (n) ); \
107 __upper = __high; \
108 if ( __high ) \
109 { \
110 __upper = __high % (__base); \
111 __high = __high / (__base); \
112 } \
113 asm ( "divl %2" \
114 : "=a" (__low), "=d" (__mod) \
115 : "rm" (__base), "0" (__low), "1" (__upper) ); \
116 asm ( "" : "=A" (n) : "a" (__low), "d" (__high) ); \
117 __mod; \
118 })
120 /* HVM-builder info. */
121 struct hvm_info_table *get_hvm_info_table(void);
122 #define hvm_info (get_hvm_info_table())
124 /* String and memory functions */
125 int strcmp(const char *cs, const char *ct);
126 int strncmp(const char *s1, const char *s2, uint32_t n);
127 char *strcpy(char *dest, const char *src);
128 char *strncpy(char *dest, const char *src, unsigned n);
129 unsigned strlen(const char *s);
130 int memcmp(const void *s1, const void *s2, unsigned n);
131 void *memcpy(void *dest, const void *src, unsigned n);
132 void *memmove(void *dest, const void *src, unsigned n);
133 void *memset(void *s, int c, unsigned n);
134 char *itoa(char *a, unsigned int i);
136 /* convert a byte to two lowercase hex digits, with no terminating NUL
137 character. digits[] must have at least two elements. */
138 void byte_to_hex(char *digits, uint8_t byte);
140 /* Convert an array of 16 unsigned bytes to a DCE/OSF formatted UUID
141 string. Pre-condition: sizeof(dest) >= 37 */
142 void uuid_to_string(char *dest, uint8_t *uuid);
144 /* Debug output */
145 int printf(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
146 int vprintf(const char *fmt, va_list ap);
148 /* Allocate memory in a reserved region below 4GB. */
149 void *mem_alloc(uint32_t size, uint32_t align);
150 #define virt_to_phys(v) ((unsigned long)(v))
152 /* Prepare the 32bit BIOS */
153 uint32_t highbios_setup(void);
155 /* Miscellaneous. */
156 void cacheattr_init(void);
157 void create_mp_tables(void);
158 int hvm_write_smbios_tables(void);
159 void smp_initialise(void);
161 #ifndef NDEBUG
162 void perform_tests(void);
163 #else
164 #define perform_tests() ((void)0)
165 #endif
167 #define isdigit(c) ((c) >= '0' && (c) <= '9')
169 extern char _start[], _end[];
171 #endif /* __HVMLOADER_UTIL_H__ */