Pull vector-domain into release branch
[pandora-kernel.git] / drivers / lguest / lg.h
1 #ifndef _LGUEST_H
2 #define _LGUEST_H
3
4 #include <asm/desc.h>
5
6 #define GDT_ENTRY_LGUEST_CS     10
7 #define GDT_ENTRY_LGUEST_DS     11
8 #define LGUEST_CS               (GDT_ENTRY_LGUEST_CS * 8)
9 #define LGUEST_DS               (GDT_ENTRY_LGUEST_DS * 8)
10
11 #ifndef __ASSEMBLY__
12 #include <linux/types.h>
13 #include <linux/init.h>
14 #include <linux/stringify.h>
15 #include <linux/binfmts.h>
16 #include <linux/futex.h>
17 #include <linux/lguest.h>
18 #include <linux/lguest_launcher.h>
19 #include <linux/wait.h>
20 #include <linux/err.h>
21 #include <asm/semaphore.h>
22 #include "irq_vectors.h"
23
24 #define GUEST_PL 1
25
26 struct lguest_regs
27 {
28         /* Manually saved part. */
29         unsigned long ebx, ecx, edx;
30         unsigned long esi, edi, ebp;
31         unsigned long gs;
32         unsigned long eax;
33         unsigned long fs, ds, es;
34         unsigned long trapnum, errcode;
35         /* Trap pushed part */
36         unsigned long eip;
37         unsigned long cs;
38         unsigned long eflags;
39         unsigned long esp;
40         unsigned long ss;
41 };
42
43 void free_pagetables(void);
44 int init_pagetables(struct page **switcher_page, unsigned int pages);
45
46 /* Full 4G segment descriptors, suitable for CS and DS. */
47 #define FULL_EXEC_SEGMENT ((struct desc_struct){0x0000ffff, 0x00cf9b00})
48 #define FULL_SEGMENT ((struct desc_struct){0x0000ffff, 0x00cf9300})
49
50 struct lguest_dma_info
51 {
52         struct list_head list;
53         union futex_key key;
54         unsigned long dmas;
55         u16 next_dma;
56         u16 num_dmas;
57         u16 guestid;
58         u8 interrupt;   /* 0 when not registered */
59 };
60
61 /* We have separate types for the guest's ptes & pgds and the shadow ptes &
62  * pgds.  Since this host might use three-level pagetables and the guest and
63  * shadow pagetables don't, we can't use the normal pte_t/pgd_t. */
64 typedef union {
65         struct { unsigned flags:12, pfn:20; };
66         struct { unsigned long val; } raw;
67 } spgd_t;
68 typedef union {
69         struct { unsigned flags:12, pfn:20; };
70         struct { unsigned long val; } raw;
71 } spte_t;
72 typedef union {
73         struct { unsigned flags:12, pfn:20; };
74         struct { unsigned long val; } raw;
75 } gpgd_t;
76 typedef union {
77         struct { unsigned flags:12, pfn:20; };
78         struct { unsigned long val; } raw;
79 } gpte_t;
80 #define mkgpte(_val) ((gpte_t){.raw.val = _val})
81 #define mkgpgd(_val) ((gpgd_t){.raw.val = _val})
82
83 struct pgdir
84 {
85         unsigned long cr3;
86         spgd_t *pgdir;
87 };
88
89 /* This is a guest-specific page (mapped ro) into the guest. */
90 struct lguest_ro_state
91 {
92         /* Host information we need to restore when we switch back. */
93         u32 host_cr3;
94         struct Xgt_desc_struct host_idt_desc;
95         struct Xgt_desc_struct host_gdt_desc;
96         u32 host_sp;
97
98         /* Fields which are used when guest is running. */
99         struct Xgt_desc_struct guest_idt_desc;
100         struct Xgt_desc_struct guest_gdt_desc;
101         struct i386_hw_tss guest_tss;
102         struct desc_struct guest_idt[IDT_ENTRIES];
103         struct desc_struct guest_gdt[GDT_ENTRIES];
104 };
105
106 /* We have two pages shared with guests, per cpu.  */
107 struct lguest_pages
108 {
109         /* This is the stack page mapped rw in guest */
110         char spare[PAGE_SIZE - sizeof(struct lguest_regs)];
111         struct lguest_regs regs;
112
113         /* This is the host state & guest descriptor page, ro in guest */
114         struct lguest_ro_state state;
115 } __attribute__((aligned(PAGE_SIZE)));
116
117 #define CHANGED_IDT             1
118 #define CHANGED_GDT             2
119 #define CHANGED_GDT_TLS         4 /* Actually a subset of CHANGED_GDT */
120 #define CHANGED_ALL             3
121
122 /* The private info the thread maintains about the guest. */
123 struct lguest
124 {
125         /* At end of a page shared mapped over lguest_pages in guest.  */
126         unsigned long regs_page;
127         struct lguest_regs *regs;
128         struct lguest_data __user *lguest_data;
129         struct task_struct *tsk;
130         struct mm_struct *mm;   /* == tsk->mm, but that becomes NULL on exit */
131         u16 guestid;
132         u32 pfn_limit;
133         u32 page_offset;
134         u32 cr2;
135         int halted;
136         int ts;
137         u32 next_hcall;
138         u32 esp1;
139         u8 ss1;
140
141         /* Do we need to stop what we're doing and return to userspace? */
142         int break_out;
143         wait_queue_head_t break_wq;
144
145         /* Bitmap of what has changed: see CHANGED_* above. */
146         int changed;
147         struct lguest_pages *last_pages;
148
149         /* We keep a small number of these. */
150         u32 pgdidx;
151         struct pgdir pgdirs[4];
152
153         /* Cached wakeup: we hold a reference to this task. */
154         struct task_struct *wake;
155
156         unsigned long noirq_start, noirq_end;
157         int dma_is_pending;
158         unsigned long pending_dma; /* struct lguest_dma */
159         unsigned long pending_key; /* address they're sending to */
160
161         unsigned int stack_pages;
162         u32 tsc_khz;
163
164         struct lguest_dma_info dma[LGUEST_MAX_DMA];
165
166         /* Dead? */
167         const char *dead;
168
169         /* The GDT entries copied into lguest_ro_state when running. */
170         struct desc_struct gdt[GDT_ENTRIES];
171
172         /* The IDT entries: some copied into lguest_ro_state when running. */
173         struct desc_struct idt[FIRST_EXTERNAL_VECTOR+LGUEST_IRQS];
174         struct desc_struct syscall_idt;
175
176         /* Virtual clock device */
177         struct hrtimer hrt;
178
179         /* Pending virtual interrupts */
180         DECLARE_BITMAP(irqs_pending, LGUEST_IRQS);
181 };
182
183 extern struct lguest lguests[];
184 extern struct mutex lguest_lock;
185
186 /* core.c: */
187 u32 lgread_u32(struct lguest *lg, unsigned long addr);
188 void lgwrite_u32(struct lguest *lg, unsigned long addr, u32 val);
189 void lgread(struct lguest *lg, void *buf, unsigned long addr, unsigned len);
190 void lgwrite(struct lguest *lg, unsigned long, const void *buf, unsigned len);
191 int find_free_guest(void);
192 int lguest_address_ok(const struct lguest *lg,
193                       unsigned long addr, unsigned long len);
194 int run_guest(struct lguest *lg, unsigned long __user *user);
195
196
197 /* interrupts_and_traps.c: */
198 void maybe_do_interrupt(struct lguest *lg);
199 int deliver_trap(struct lguest *lg, unsigned int num);
200 void load_guest_idt_entry(struct lguest *lg, unsigned int i, u32 low, u32 hi);
201 void guest_set_stack(struct lguest *lg, u32 seg, u32 esp, unsigned int pages);
202 void pin_stack_pages(struct lguest *lg);
203 void setup_default_idt_entries(struct lguest_ro_state *state,
204                                const unsigned long *def);
205 void copy_traps(const struct lguest *lg, struct desc_struct *idt,
206                 const unsigned long *def);
207 void guest_set_clockevent(struct lguest *lg, unsigned long delta);
208 void init_clockdev(struct lguest *lg);
209
210 /* segments.c: */
211 void setup_default_gdt_entries(struct lguest_ro_state *state);
212 void setup_guest_gdt(struct lguest *lg);
213 void load_guest_gdt(struct lguest *lg, unsigned long table, u32 num);
214 void guest_load_tls(struct lguest *lg, unsigned long tls_array);
215 void copy_gdt(const struct lguest *lg, struct desc_struct *gdt);
216 void copy_gdt_tls(const struct lguest *lg, struct desc_struct *gdt);
217
218 /* page_tables.c: */
219 int init_guest_pagetable(struct lguest *lg, unsigned long pgtable);
220 void free_guest_pagetable(struct lguest *lg);
221 void guest_new_pagetable(struct lguest *lg, unsigned long pgtable);
222 void guest_set_pmd(struct lguest *lg, unsigned long cr3, u32 i);
223 void guest_pagetable_clear_all(struct lguest *lg);
224 void guest_pagetable_flush_user(struct lguest *lg);
225 void guest_set_pte(struct lguest *lg, unsigned long cr3,
226                    unsigned long vaddr, gpte_t val);
227 void map_switcher_in_guest(struct lguest *lg, struct lguest_pages *pages);
228 int demand_page(struct lguest *info, unsigned long cr2, int errcode);
229 void pin_page(struct lguest *lg, unsigned long vaddr);
230
231 /* lguest_user.c: */
232 int lguest_device_init(void);
233 void lguest_device_remove(void);
234
235 /* io.c: */
236 void lguest_io_init(void);
237 int bind_dma(struct lguest *lg,
238              unsigned long key, unsigned long udma, u16 numdmas, u8 interrupt);
239 void send_dma(struct lguest *info, unsigned long key, unsigned long udma);
240 void release_all_dma(struct lguest *lg);
241 unsigned long get_dma_buffer(struct lguest *lg, unsigned long key,
242                              unsigned long *interrupt);
243
244 /* hypercalls.c: */
245 void do_hypercalls(struct lguest *lg);
246
247 #define kill_guest(lg, fmt...)                                  \
248 do {                                                            \
249         if (!(lg)->dead) {                                      \
250                 (lg)->dead = kasprintf(GFP_ATOMIC, fmt);        \
251                 if (!(lg)->dead)                                \
252                         (lg)->dead = ERR_PTR(-ENOMEM);          \
253         }                                                       \
254 } while(0)
255
256 static inline unsigned long guest_pa(struct lguest *lg, unsigned long vaddr)
257 {
258         return vaddr - lg->page_offset;
259 }
260 #endif  /* __ASSEMBLY__ */
261 #endif  /* _LGUEST_H */