]> rtime.felk.cvut.cz Git - jailhouse.git/blob - inmates/lib/x86/inmate.h
inmates: Add memory services to inmates framework
[jailhouse.git] / inmates / lib / x86 / inmate.h
1 /*
2  * Jailhouse, a Linux-based partitioning hypervisor
3  *
4  * Copyright (c) Siemens AG, 2013
5  *
6  * Authors:
7  *  Jan Kiszka <jan.kiszka@siemens.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.  See
10  * the COPYING file in the top-level directory.
11  */
12
13 #define HEAP_BASE               0x000000
14 #define FSEGMENT_BASE           0x0f0000
15 #define COMM_REGION_BASE        0x100000
16
17 #define INMATE_CS32             0x8
18 #define INMATE_CS64             0x10
19 #define INMATE_DS32             0x18
20
21 #define NS_PER_MSEC             1000000UL
22 #define NS_PER_SEC              1000000000UL
23
24 #define PAGE_SIZE               (4 * 1024ULL)
25 #ifdef __x86_64__
26 #define HUGE_PAGE_SIZE          (2 * 1024 * 1024ULL)
27 #else
28 #define HUGE_PAGE_SIZE          (4 * 1024 * 1024ULL)
29 #endif
30 #define PAGE_MASK               (~(PAGE_SIZE - 1))
31 #define HUGE_PAGE_MASK          (~(HUGE_PAGE_SIZE - 1))
32
33 #define X2APIC_ID               0x802
34
35 #ifndef __ASSEMBLY__
36 typedef signed char s8;
37 typedef unsigned char u8;
38
39 typedef signed short s16;
40 typedef unsigned short u16;
41
42 typedef signed int s32;
43 typedef unsigned int u32;
44
45 typedef signed long long s64;
46 typedef unsigned long long u64;
47
48 typedef s8 __s8;
49 typedef u8 __u8;
50
51 typedef s16 __s16;
52 typedef u16 __u16;
53
54 typedef s32 __s32;
55 typedef u32 __u32;
56
57 typedef s64 __s64;
58 typedef u64 __u64;
59
60 typedef enum { true=1, false=0 } bool;
61
62 static inline void cpu_relax(void)
63 {
64         asm volatile("rep; nop" : : : "memory");
65 }
66
67 static inline void outb(u8 v, u16 port)
68 {
69         asm volatile("outb %0,%1" : : "a" (v), "dN" (port));
70 }
71
72 static inline u8 inb(u16 port)
73 {
74         u8 v;
75         asm volatile("inb %1,%0" : "=a" (v) : "dN" (port));
76         return v;
77 }
78
79 static inline u32 inl(u16 port)
80 {
81         u32 v;
82         asm volatile("inl %1,%0" : "=a" (v) : "dN" (port));
83         return v;
84 }
85
86 static inline u8 mmio_read8(void *address)
87 {
88         return *(volatile u8 *)address;
89 }
90
91 static inline u16 mmio_read16(void *address)
92 {
93         return *(volatile u16 *)address;
94 }
95
96 static inline u32 mmio_read32(void *address)
97 {
98         u32 value;
99
100         /* assembly-encoded to match the hypervisor MMIO parser support */
101         asm volatile("movl (%1),%0" : "=r" (value) : "r" (address));
102         return value;
103 }
104
105 static inline u64 mmio_read64(void *address)
106 {
107         return *(volatile u64 *)address;
108 }
109
110 static inline void mmio_write8(void *address, u8 value)
111 {
112         *(volatile u8 *)address = value;
113 }
114
115 static inline void mmio_write16(void *address, u16 value)
116 {
117         *(volatile u16 *)address = value;
118 }
119
120 static inline void mmio_write32(void *address, u32 value)
121 {
122         /* assembly-encoded to match the hypervisor MMIO parser support */
123         asm volatile("movl %0,(%1)" : : "r" (value), "r" (address));
124 }
125
126 static inline void mmio_write64(void *address, u64 value)
127 {
128         *(volatile u64 *)address = value;
129 }
130
131 static inline u64 read_msr(unsigned int msr)
132 {
133         u32 low, high;
134
135         asm volatile("rdmsr" : "=a" (low), "=d" (high) : "c" (msr));
136         return low | ((u64)high << 32);
137 }
138
139 static inline void write_msr(unsigned int msr, u64 val)
140 {
141         asm volatile("wrmsr"
142                 : /* no output */
143                 : "c" (msr), "a" (val), "d" (val >> 32)
144                 : "memory");
145 }
146
147 static inline unsigned int cpu_id(void)
148 {
149         return read_msr(X2APIC_ID);
150 }
151
152 #include <jailhouse/hypercall.h>
153
154 #define comm_region     ((struct jailhouse_comm_region *)COMM_REGION_BASE)
155
156 extern unsigned int printk_uart_base;
157 void printk(const char *fmt, ...);
158
159 void *memset(void *s, int c, unsigned long n);
160
161 typedef void(*int_handler_t)(void);
162
163 void int_init(void);
164 void int_set_handler(unsigned int vector, int_handler_t handler);
165
166 void inmate_main(void);
167
168 unsigned long pm_timer_read(void);
169 unsigned long apic_timer_init(unsigned int vector);
170 void apic_timer_set(unsigned long timeout_ns);
171
172 enum map_type { MAP_CACHED, MAP_UNCACHED };
173
174 void *alloc(unsigned long size, unsigned long align);
175 void map_range(void *start, unsigned long size, enum map_type map_type);
176 #endif