blob: 54f2c2472bec2c764777514990297b22eba6e84e [file] [log] [blame]
Rusty Russelld7e28ff2007-07-19 01:49:23 -07001#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>
Rusty Russelld7e28ff2007-07-19 01:49:23 -070015#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
25struct 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
42void free_pagetables(void);
43int 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
49struct 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
Rusty Russellbff672e2007-07-26 10:41:04 -070060/*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. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070072typedef union {
73 struct { unsigned flags:12, pfn:20; };
74 struct { unsigned long val; } raw;
75} spgd_t;
76typedef union {
77 struct { unsigned flags:12, pfn:20; };
78 struct { unsigned long val; } raw;
79} spte_t;
80typedef union {
81 struct { unsigned flags:12, pfn:20; };
82 struct { unsigned long val; } raw;
83} gpgd_t;
84typedef union {
85 struct { unsigned flags:12, pfn:20; };
86 struct { unsigned long val; } raw;
87} gpte_t;
Rusty Russellbff672e2007-07-26 10:41:04 -070088
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. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070091#define mkgpte(_val) ((gpte_t){.raw.val = _val})
92#define mkgpgd(_val) ((gpgd_t){.raw.val = _val})
Rusty Russellbff672e2007-07-26 10:41:04 -070093/*:*/
Rusty Russelld7e28ff2007-07-19 01:49:23 -070094
95struct pgdir
96{
97 unsigned long cr3;
98 spgd_t *pgdir;
99};
100
101/* This is a guest-specific page (mapped ro) into the guest. */
102struct 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. */
119struct 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. */
135struct 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;
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000145 /* This provides the offset to the base of guest-physical
146 * memory in the Launcher. */
147 void __user *mem_base;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700148 u32 page_offset;
149 u32 cr2;
150 int halted;
151 int ts;
152 u32 next_hcall;
153 u32 esp1;
154 u8 ss1;
155
156 /* Do we need to stop what we're doing and return to userspace? */
157 int break_out;
158 wait_queue_head_t break_wq;
159
160 /* Bitmap of what has changed: see CHANGED_* above. */
161 int changed;
162 struct lguest_pages *last_pages;
163
164 /* We keep a small number of these. */
165 u32 pgdidx;
166 struct pgdir pgdirs[4];
167
168 /* Cached wakeup: we hold a reference to this task. */
169 struct task_struct *wake;
170
171 unsigned long noirq_start, noirq_end;
172 int dma_is_pending;
173 unsigned long pending_dma; /* struct lguest_dma */
174 unsigned long pending_key; /* address they're sending to */
175
176 unsigned int stack_pages;
177 u32 tsc_khz;
178
179 struct lguest_dma_info dma[LGUEST_MAX_DMA];
180
181 /* Dead? */
182 const char *dead;
183
184 /* The GDT entries copied into lguest_ro_state when running. */
185 struct desc_struct gdt[GDT_ENTRIES];
186
187 /* The IDT entries: some copied into lguest_ro_state when running. */
188 struct desc_struct idt[FIRST_EXTERNAL_VECTOR+LGUEST_IRQS];
189 struct desc_struct syscall_idt;
190
191 /* Virtual clock device */
192 struct hrtimer hrt;
193
194 /* Pending virtual interrupts */
195 DECLARE_BITMAP(irqs_pending, LGUEST_IRQS);
196};
197
198extern struct lguest lguests[];
199extern struct mutex lguest_lock;
200
201/* core.c: */
202u32 lgread_u32(struct lguest *lg, unsigned long addr);
203void lgwrite_u32(struct lguest *lg, unsigned long addr, u32 val);
204void lgread(struct lguest *lg, void *buf, unsigned long addr, unsigned len);
205void lgwrite(struct lguest *lg, unsigned long, const void *buf, unsigned len);
206int find_free_guest(void);
207int lguest_address_ok(const struct lguest *lg,
208 unsigned long addr, unsigned long len);
209int run_guest(struct lguest *lg, unsigned long __user *user);
210
211
212/* interrupts_and_traps.c: */
213void maybe_do_interrupt(struct lguest *lg);
214int deliver_trap(struct lguest *lg, unsigned int num);
215void load_guest_idt_entry(struct lguest *lg, unsigned int i, u32 low, u32 hi);
216void guest_set_stack(struct lguest *lg, u32 seg, u32 esp, unsigned int pages);
217void pin_stack_pages(struct lguest *lg);
218void setup_default_idt_entries(struct lguest_ro_state *state,
219 const unsigned long *def);
220void copy_traps(const struct lguest *lg, struct desc_struct *idt,
221 const unsigned long *def);
222void guest_set_clockevent(struct lguest *lg, unsigned long delta);
223void init_clockdev(struct lguest *lg);
224
225/* segments.c: */
226void setup_default_gdt_entries(struct lguest_ro_state *state);
227void setup_guest_gdt(struct lguest *lg);
228void load_guest_gdt(struct lguest *lg, unsigned long table, u32 num);
229void guest_load_tls(struct lguest *lg, unsigned long tls_array);
230void copy_gdt(const struct lguest *lg, struct desc_struct *gdt);
231void copy_gdt_tls(const struct lguest *lg, struct desc_struct *gdt);
232
233/* page_tables.c: */
234int init_guest_pagetable(struct lguest *lg, unsigned long pgtable);
235void free_guest_pagetable(struct lguest *lg);
236void guest_new_pagetable(struct lguest *lg, unsigned long pgtable);
237void guest_set_pmd(struct lguest *lg, unsigned long cr3, u32 i);
238void guest_pagetable_clear_all(struct lguest *lg);
239void guest_pagetable_flush_user(struct lguest *lg);
240void guest_set_pte(struct lguest *lg, unsigned long cr3,
241 unsigned long vaddr, gpte_t val);
242void map_switcher_in_guest(struct lguest *lg, struct lguest_pages *pages);
243int demand_page(struct lguest *info, unsigned long cr2, int errcode);
244void pin_page(struct lguest *lg, unsigned long vaddr);
245
246/* lguest_user.c: */
247int lguest_device_init(void);
248void lguest_device_remove(void);
249
250/* io.c: */
251void lguest_io_init(void);
252int bind_dma(struct lguest *lg,
253 unsigned long key, unsigned long udma, u16 numdmas, u8 interrupt);
254void send_dma(struct lguest *info, unsigned long key, unsigned long udma);
255void release_all_dma(struct lguest *lg);
256unsigned long get_dma_buffer(struct lguest *lg, unsigned long key,
257 unsigned long *interrupt);
258
259/* hypercalls.c: */
260void do_hypercalls(struct lguest *lg);
Rusty Russell6c8dca52007-07-27 13:42:52 +1000261void write_timestamp(struct lguest *lg);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700262
Rusty Russelldde79782007-07-26 10:41:03 -0700263/*L:035
264 * Let's step aside for the moment, to study one important routine that's used
265 * widely in the Host code.
266 *
267 * There are many cases where the Guest does something invalid, like pass crap
268 * to a hypercall. Since only the Guest kernel can make hypercalls, it's quite
269 * acceptable to simply terminate the Guest and give the Launcher a nicely
270 * formatted reason. It's also simpler for the Guest itself, which doesn't
271 * need to check most hypercalls for "success"; if you're still running, it
272 * succeeded.
273 *
274 * Once this is called, the Guest will never run again, so most Host code can
275 * call this then continue as if nothing had happened. This means many
276 * functions don't have to explicitly return an error code, which keeps the
277 * code simple.
278 *
279 * It also means that this can be called more than once: only the first one is
280 * remembered. The only trick is that we still need to kill the Guest even if
281 * we can't allocate memory to store the reason. Linux has a neat way of
282 * packing error codes into invalid pointers, so we use that here.
283 *
284 * Like any macro which uses an "if", it is safely wrapped in a run-once "do {
285 * } while(0)".
286 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700287#define kill_guest(lg, fmt...) \
288do { \
289 if (!(lg)->dead) { \
290 (lg)->dead = kasprintf(GFP_ATOMIC, fmt); \
291 if (!(lg)->dead) \
292 (lg)->dead = ERR_PTR(-ENOMEM); \
293 } \
294} while(0)
Rusty Russelldde79782007-07-26 10:41:03 -0700295/* (End of aside) :*/
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700296
297static inline unsigned long guest_pa(struct lguest *lg, unsigned long vaddr)
298{
299 return vaddr - lg->page_offset;
300}
301#endif /* __ASSEMBLY__ */
302#endif /* _LGUEST_H */