Remove binfmts.h include from lg.h
[safe/jmp/linux-2.6] / 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/futex.h>
16 #include <linux/lguest.h>
17 #include <linux/lguest_launcher.h>
18 #include <linux/wait.h>
19 #include <linux/err.h>
20 #include <asm/semaphore.h>
21 #include "irq_vectors.h"
22
23 #define GUEST_PL 1
24
25 struct lguest_regs
26 {
27         /* Manually saved part. */
28         unsigned long ebx, ecx, edx;
29         unsigned long esi, edi, ebp;
30         unsigned long gs;
31         unsigned long eax;
32         unsigned long fs, ds, es;
33         unsigned long trapnum, errcode;
34         /* Trap pushed part */
35         unsigned long eip;
36         unsigned long cs;
37         unsigned long eflags;
38         unsigned long esp;
39         unsigned long ss;
40 };
41
42 void free_pagetables(void);
43 int init_pagetables(struct page **switcher_page, unsigned int pages);
44
45 /* Full 4G segment descriptors, suitable for CS and DS. */
46 #define FULL_EXEC_SEGMENT ((struct desc_struct){0x0000ffff, 0x00cf9b00})
47 #define FULL_SEGMENT ((struct desc_struct){0x0000ffff, 0x00cf9300})
48
49 struct lguest_dma_info
50 {
51         struct list_head list;
52         union futex_key key;
53         unsigned long dmas;
54         u16 next_dma;
55         u16 num_dmas;
56         u16 guestid;
57         u8 interrupt;   /* 0 when not registered */
58 };
59
60 /*H:310 The page-table code owes a great debt of gratitude to Andi Kleen.  He
61  * reviewed the original code which used "u32" for all page table entries, and
62  * insisted that it would be far clearer with explicit typing.  I thought it
63  * was overkill, but he was right: it is much clearer than it was before.
64  *
65  * We have separate types for the Guest's ptes & pgds and the shadow ptes &
66  * pgds.  There's already a Linux type for these (pte_t and pgd_t) but they
67  * change depending on kernel config options (PAE). */
68
69 /* Each entry is identical: lower 12 bits of flags and upper 20 bits for the
70  * "page frame number" (0 == first physical page, etc).  They are different
71  * types so the compiler will warn us if we mix them improperly. */
72 typedef union {
73         struct { unsigned flags:12, pfn:20; };
74         struct { unsigned long val; } raw;
75 } spgd_t;
76 typedef union {
77         struct { unsigned flags:12, pfn:20; };
78         struct { unsigned long val; } raw;
79 } spte_t;
80 typedef union {
81         struct { unsigned flags:12, pfn:20; };
82         struct { unsigned long val; } raw;
83 } gpgd_t;
84 typedef union {
85         struct { unsigned flags:12, pfn:20; };
86         struct { unsigned long val; } raw;
87 } gpte_t;
88
89 /* We have two convenient macros to convert a "raw" value as handed to us by
90  * the Guest into the correct Guest PGD or PTE type. */
91 #define mkgpte(_val) ((gpte_t){.raw.val = _val})
92 #define mkgpgd(_val) ((gpgd_t){.raw.val = _val})
93 /*:*/
94
95 struct pgdir
96 {
97         unsigned long cr3;
98         spgd_t *pgdir;
99 };
100
101 /* This is a guest-specific page (mapped ro) into the guest. */
102 struct lguest_ro_state
103 {
104         /* Host information we need to restore when we switch back. */
105         u32 host_cr3;
106         struct Xgt_desc_struct host_idt_desc;
107         struct Xgt_desc_struct host_gdt_desc;
108         u32 host_sp;
109
110         /* Fields which are used when guest is running. */
111         struct Xgt_desc_struct guest_idt_desc;
112         struct Xgt_desc_struct guest_gdt_desc;
113         struct i386_hw_tss guest_tss;
114         struct desc_struct guest_idt[IDT_ENTRIES];
115         struct desc_struct guest_gdt[GDT_ENTRIES];
116 };
117
118 /* We have two pages shared with guests, per cpu.  */
119 struct lguest_pages
120 {
121         /* This is the stack page mapped rw in guest */
122         char spare[PAGE_SIZE - sizeof(struct lguest_regs)];
123         struct lguest_regs regs;
124
125         /* This is the host state & guest descriptor page, ro in guest */
126         struct lguest_ro_state state;
127 } __attribute__((aligned(PAGE_SIZE)));
128
129 #define CHANGED_IDT             1
130 #define CHANGED_GDT             2
131 #define CHANGED_GDT_TLS         4 /* Actually a subset of CHANGED_GDT */
132 #define CHANGED_ALL             3
133
134 /* The private info the thread maintains about the guest. */
135 struct lguest
136 {
137         /* At end of a page shared mapped over lguest_pages in guest.  */
138         unsigned long regs_page;
139         struct lguest_regs *regs;
140         struct lguest_data __user *lguest_data;
141         struct task_struct *tsk;
142         struct mm_struct *mm;   /* == tsk->mm, but that becomes NULL on exit */
143         u16 guestid;
144         u32 pfn_limit;
145         u32 page_offset;
146         u32 cr2;
147         int halted;
148         int ts;
149         u32 next_hcall;
150         u32 esp1;
151         u8 ss1;
152
153         /* Do we need to stop what we're doing and return to userspace? */
154         int break_out;
155         wait_queue_head_t break_wq;
156
157         /* Bitmap of what has changed: see CHANGED_* above. */
158         int changed;
159         struct lguest_pages *last_pages;
160
161         /* We keep a small number of these. */
162         u32 pgdidx;
163         struct pgdir pgdirs[4];
164
165         /* Cached wakeup: we hold a reference to this task. */
166         struct task_struct *wake;
167
168         unsigned long noirq_start, noirq_end;
169         int dma_is_pending;
170         unsigned long pending_dma; /* struct lguest_dma */
171         unsigned long pending_key; /* address they're sending to */
172
173         unsigned int stack_pages;
174         u32 tsc_khz;
175
176         struct lguest_dma_info dma[LGUEST_MAX_DMA];
177
178         /* Dead? */
179         const char *dead;
180
181         /* The GDT entries copied into lguest_ro_state when running. */
182         struct desc_struct gdt[GDT_ENTRIES];
183
184         /* The IDT entries: some copied into lguest_ro_state when running. */
185         struct desc_struct idt[FIRST_EXTERNAL_VECTOR+LGUEST_IRQS];
186         struct desc_struct syscall_idt;
187
188         /* Virtual clock device */
189         struct hrtimer hrt;
190
191         /* Pending virtual interrupts */
192         DECLARE_BITMAP(irqs_pending, LGUEST_IRQS);
193 };
194
195 extern struct lguest lguests[];
196 extern struct mutex lguest_lock;
197
198 /* core.c: */
199 u32 lgread_u32(struct lguest *lg, unsigned long addr);
200 void lgwrite_u32(struct lguest *lg, unsigned long addr, u32 val);
201 void lgread(struct lguest *lg, void *buf, unsigned long addr, unsigned len);
202 void lgwrite(struct lguest *lg, unsigned long, const void *buf, unsigned len);
203 int find_free_guest(void);
204 int lguest_address_ok(const struct lguest *lg,
205                       unsigned long addr, unsigned long len);
206 int run_guest(struct lguest *lg, unsigned long __user *user);
207
208
209 /* interrupts_and_traps.c: */
210 void maybe_do_interrupt(struct lguest *lg);
211 int deliver_trap(struct lguest *lg, unsigned int num);
212 void load_guest_idt_entry(struct lguest *lg, unsigned int i, u32 low, u32 hi);
213 void guest_set_stack(struct lguest *lg, u32 seg, u32 esp, unsigned int pages);
214 void pin_stack_pages(struct lguest *lg);
215 void setup_default_idt_entries(struct lguest_ro_state *state,
216                                const unsigned long *def);
217 void copy_traps(const struct lguest *lg, struct desc_struct *idt,
218                 const unsigned long *def);
219 void guest_set_clockevent(struct lguest *lg, unsigned long delta);
220 void init_clockdev(struct lguest *lg);
221
222 /* segments.c: */
223 void setup_default_gdt_entries(struct lguest_ro_state *state);
224 void setup_guest_gdt(struct lguest *lg);
225 void load_guest_gdt(struct lguest *lg, unsigned long table, u32 num);
226 void guest_load_tls(struct lguest *lg, unsigned long tls_array);
227 void copy_gdt(const struct lguest *lg, struct desc_struct *gdt);
228 void copy_gdt_tls(const struct lguest *lg, struct desc_struct *gdt);
229
230 /* page_tables.c: */
231 int init_guest_pagetable(struct lguest *lg, unsigned long pgtable);
232 void free_guest_pagetable(struct lguest *lg);
233 void guest_new_pagetable(struct lguest *lg, unsigned long pgtable);
234 void guest_set_pmd(struct lguest *lg, unsigned long cr3, u32 i);
235 void guest_pagetable_clear_all(struct lguest *lg);
236 void guest_pagetable_flush_user(struct lguest *lg);
237 void guest_set_pte(struct lguest *lg, unsigned long cr3,
238                    unsigned long vaddr, gpte_t val);
239 void map_switcher_in_guest(struct lguest *lg, struct lguest_pages *pages);
240 int demand_page(struct lguest *info, unsigned long cr2, int errcode);
241 void pin_page(struct lguest *lg, unsigned long vaddr);
242
243 /* lguest_user.c: */
244 int lguest_device_init(void);
245 void lguest_device_remove(void);
246
247 /* io.c: */
248 void lguest_io_init(void);
249 int bind_dma(struct lguest *lg,
250              unsigned long key, unsigned long udma, u16 numdmas, u8 interrupt);
251 void send_dma(struct lguest *info, unsigned long key, unsigned long udma);
252 void release_all_dma(struct lguest *lg);
253 unsigned long get_dma_buffer(struct lguest *lg, unsigned long key,
254                              unsigned long *interrupt);
255
256 /* hypercalls.c: */
257 void do_hypercalls(struct lguest *lg);
258 void write_timestamp(struct lguest *lg);
259
260 /*L:035
261  * Let's step aside for the moment, to study one important routine that's used
262  * widely in the Host code.
263  *
264  * There are many cases where the Guest does something invalid, like pass crap
265  * to a hypercall.  Since only the Guest kernel can make hypercalls, it's quite
266  * acceptable to simply terminate the Guest and give the Launcher a nicely
267  * formatted reason.  It's also simpler for the Guest itself, which doesn't
268  * need to check most hypercalls for "success"; if you're still running, it
269  * succeeded.
270  *
271  * Once this is called, the Guest will never run again, so most Host code can
272  * call this then continue as if nothing had happened.  This means many
273  * functions don't have to explicitly return an error code, which keeps the
274  * code simple.
275  *
276  * It also means that this can be called more than once: only the first one is
277  * remembered.  The only trick is that we still need to kill the Guest even if
278  * we can't allocate memory to store the reason.  Linux has a neat way of
279  * packing error codes into invalid pointers, so we use that here.
280  *
281  * Like any macro which uses an "if", it is safely wrapped in a run-once "do {
282  * } while(0)".
283  */
284 #define kill_guest(lg, fmt...)                                  \
285 do {                                                            \
286         if (!(lg)->dead) {                                      \
287                 (lg)->dead = kasprintf(GFP_ATOMIC, fmt);        \
288                 if (!(lg)->dead)                                \
289                         (lg)->dead = ERR_PTR(-ENOMEM);          \
290         }                                                       \
291 } while(0)
292 /* (End of aside) :*/
293
294 static inline unsigned long guest_pa(struct lguest *lg, unsigned long vaddr)
295 {
296         return vaddr - lg->page_offset;
297 }
298 #endif  /* __ASSEMBLY__ */
299 #endif  /* _LGUEST_H */