lguest: documentation IV: Launcher
[linux-2.6/zen-sources.git] / drivers / lguest / lg.h
blob3b9dc123a7df97955837d82123e99f225299d3b5
1 #ifndef _LGUEST_H
2 #define _LGUEST_H
4 #include <asm/desc.h>
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)
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"
24 #define GUEST_PL 1
26 struct lguest_regs
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;
43 void free_pagetables(void);
44 int init_pagetables(struct page **switcher_page, unsigned int pages);
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})
50 struct lguest_dma_info
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 */
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})
83 struct pgdir
85 unsigned long cr3;
86 spgd_t *pgdir;
89 /* This is a guest-specific page (mapped ro) into the guest. */
90 struct lguest_ro_state
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;
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];
106 /* We have two pages shared with guests, per cpu. */
107 struct lguest_pages
109 /* This is the stack page mapped rw in guest */
110 char spare[PAGE_SIZE - sizeof(struct lguest_regs)];
111 struct lguest_regs regs;
113 /* This is the host state & guest descriptor page, ro in guest */
114 struct lguest_ro_state state;
115 } __attribute__((aligned(PAGE_SIZE)));
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
122 /* The private info the thread maintains about the guest. */
123 struct lguest
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;
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;
145 /* Bitmap of what has changed: see CHANGED_* above. */
146 int changed;
147 struct lguest_pages *last_pages;
149 /* We keep a small number of these. */
150 u32 pgdidx;
151 struct pgdir pgdirs[4];
153 /* Cached wakeup: we hold a reference to this task. */
154 struct task_struct *wake;
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 */
161 unsigned int stack_pages;
162 u32 tsc_khz;
164 struct lguest_dma_info dma[LGUEST_MAX_DMA];
166 /* Dead? */
167 const char *dead;
169 /* The GDT entries copied into lguest_ro_state when running. */
170 struct desc_struct gdt[GDT_ENTRIES];
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;
176 /* Virtual clock device */
177 struct hrtimer hrt;
179 /* Pending virtual interrupts */
180 DECLARE_BITMAP(irqs_pending, LGUEST_IRQS);
183 extern struct lguest lguests[];
184 extern struct mutex lguest_lock;
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);
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);
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);
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);
231 /* lguest_user.c: */
232 int lguest_device_init(void);
233 void lguest_device_remove(void);
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);
244 /* hypercalls.c: */
245 void do_hypercalls(struct lguest *lg);
247 /*L:035
248 * Let's step aside for the moment, to study one important routine that's used
249 * widely in the Host code.
251 * There are many cases where the Guest does something invalid, like pass crap
252 * to a hypercall. Since only the Guest kernel can make hypercalls, it's quite
253 * acceptable to simply terminate the Guest and give the Launcher a nicely
254 * formatted reason. It's also simpler for the Guest itself, which doesn't
255 * need to check most hypercalls for "success"; if you're still running, it
256 * succeeded.
258 * Once this is called, the Guest will never run again, so most Host code can
259 * call this then continue as if nothing had happened. This means many
260 * functions don't have to explicitly return an error code, which keeps the
261 * code simple.
263 * It also means that this can be called more than once: only the first one is
264 * remembered. The only trick is that we still need to kill the Guest even if
265 * we can't allocate memory to store the reason. Linux has a neat way of
266 * packing error codes into invalid pointers, so we use that here.
268 * Like any macro which uses an "if", it is safely wrapped in a run-once "do {
269 * } while(0)".
271 #define kill_guest(lg, fmt...) \
272 do { \
273 if (!(lg)->dead) { \
274 (lg)->dead = kasprintf(GFP_ATOMIC, fmt); \
275 if (!(lg)->dead) \
276 (lg)->dead = ERR_PTR(-ENOMEM); \
278 } while(0)
279 /* (End of aside) :*/
281 static inline unsigned long guest_pa(struct lguest *lg, unsigned long vaddr)
283 return vaddr - lg->page_offset;
285 #endif /* __ASSEMBLY__ */
286 #endif /* _LGUEST_H */