2 * Jailhouse, a Linux-based partitioning hypervisor
4 * Copyright (c) Siemens AG, 2013
7 * Jan Kiszka <jan.kiszka@siemens.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
13 #define HEAP_BASE 0x000000
14 #define FSEGMENT_BASE 0x0f0000
15 #define COMM_REGION_BASE 0x100000
17 #define INMATE_CS32 0x8
18 #define INMATE_CS64 0x10
19 #define INMATE_DS32 0x18
21 #define NS_PER_USEC 1000UL
22 #define NS_PER_MSEC 1000000UL
23 #define NS_PER_SEC 1000000000UL
25 #define PAGE_SIZE (4 * 1024ULL)
27 #define HUGE_PAGE_SIZE (2 * 1024 * 1024ULL)
29 #define HUGE_PAGE_SIZE (4 * 1024 * 1024ULL)
31 #define PAGE_MASK (~(PAGE_SIZE - 1))
32 #define HUGE_PAGE_MASK (~(HUGE_PAGE_SIZE - 1))
34 #define X2APIC_ID 0x802
36 #define PCI_CFG_VENDOR_ID 0x000
37 #define PCI_CFG_DEVICE_ID 0x002
38 #define PCI_CFG_COMMAND 0x004
39 # define PCI_CMD_IO (1 << 0)
40 # define PCI_CMD_MEM (1 << 1)
41 # define PCI_CMD_MASTER (1 << 2)
42 # define PCI_CMD_INTX_OFF (1 << 10)
43 #define PCI_CFG_STATUS 0x006
44 # define PCI_STS_INT (1 << 3)
45 #define PCI_CFG_BAR 0x010
46 #define PCI_CFG_CAP_PTR 0x034
48 #define PCI_ID_ANY 0xffff
50 #define PCI_CAP_MSI 0x05
53 typedef signed char s8;
54 typedef unsigned char u8;
56 typedef signed short s16;
57 typedef unsigned short u16;
59 typedef signed int s32;
60 typedef unsigned int u32;
62 typedef signed long long s64;
63 typedef unsigned long long u64;
77 typedef enum { true=1, false=0 } bool;
79 static inline void cpu_relax(void)
81 asm volatile("rep; nop" : : : "memory");
84 static inline void outb(u8 v, u16 port)
86 asm volatile("outb %0,%1" : : "a" (v), "dN" (port));
89 static inline void outw(u16 v, u16 port)
91 asm volatile("outw %0,%1" : : "a" (v), "dN" (port));
94 static inline void outl(u32 v, u16 port)
96 asm volatile("outl %0,%1" : : "a" (v), "dN" (port));
99 static inline u8 inb(u16 port)
102 asm volatile("inb %1,%0" : "=a" (v) : "dN" (port));
106 static inline u16 inw(u16 port)
109 asm volatile("inw %1,%0" : "=a" (v) : "dN" (port));
113 static inline u32 inl(u16 port)
116 asm volatile("inl %1,%0" : "=a" (v) : "dN" (port));
120 static inline u8 mmio_read8(void *address)
122 return *(volatile u8 *)address;
125 static inline u16 mmio_read16(void *address)
127 return *(volatile u16 *)address;
130 static inline u32 mmio_read32(void *address)
134 /* assembly-encoded to match the hypervisor MMIO parser support */
135 asm volatile("movl (%1),%0" : "=r" (value) : "r" (address));
139 static inline u64 mmio_read64(void *address)
141 return *(volatile u64 *)address;
144 static inline void mmio_write8(void *address, u8 value)
146 *(volatile u8 *)address = value;
149 static inline void mmio_write16(void *address, u16 value)
151 *(volatile u16 *)address = value;
154 static inline void mmio_write32(void *address, u32 value)
156 /* assembly-encoded to match the hypervisor MMIO parser support */
157 asm volatile("movl %0,(%1)" : : "r" (value), "r" (address));
160 static inline void mmio_write64(void *address, u64 value)
162 *(volatile u64 *)address = value;
165 static inline u64 read_msr(unsigned int msr)
169 asm volatile("rdmsr" : "=a" (low), "=d" (high) : "c" (msr));
170 return low | ((u64)high << 32);
173 static inline void write_msr(unsigned int msr, u64 val)
177 : "c" (msr), "a" (val), "d" (val >> 32)
181 static inline unsigned int cpu_id(void)
183 return read_msr(X2APIC_ID);
186 #include <jailhouse/hypercall.h>
188 #define comm_region ((struct jailhouse_comm_region *)COMM_REGION_BASE)
190 extern unsigned int printk_uart_base;
191 void printk(const char *fmt, ...);
193 void *memset(void *s, int c, unsigned long n);
194 void *memcpy(void *d, const void *s, unsigned long n);
196 typedef void(*int_handler_t)(void);
199 void int_set_handler(unsigned int vector, int_handler_t handler);
201 enum ioapic_trigger_mode {
203 TRIGGER_LEVEL_ACTIVE_HIGH = 1 << 15,
204 TRIGGER_LEVEL_ACTIVE_LOW = (1 << 15) | (1 << 13),
207 void ioapic_init(void);
208 void ioapic_pin_set_vector(unsigned int pin,
209 enum ioapic_trigger_mode trigger_mode,
210 unsigned int vector);
212 void inmate_main(void);
214 unsigned long pm_timer_read(void);
215 void delay_us(unsigned long microsecs);
216 unsigned long apic_timer_init(unsigned int vector);
217 void apic_timer_set(unsigned long timeout_ns);
219 enum map_type { MAP_CACHED, MAP_UNCACHED };
221 void *alloc(unsigned long size, unsigned long align);
222 void map_range(void *start, unsigned long size, enum map_type map_type);
224 u32 pci_read_config(u16 bdf, unsigned int addr, unsigned int size);
225 void pci_write_config(u16 bdf, unsigned int addr, u32 value,
227 int pci_find_device(u16 vendor, u16 device);
228 int pci_find_cap(u16 bdf, u16 cap);
229 void pci_msi_set_vector(u16 bdf, unsigned int vector);