blob: 203d3100c3b451456ca762a4dc582d10adc0be6c [file] [log] [blame]
Rusty Russelld7e28ff2007-07-19 01:49:23 -07001#ifndef _LGUEST_H
2#define _LGUEST_H
3
Rusty Russelld7e28ff2007-07-19 01:49:23 -07004#ifndef __ASSEMBLY__
5#include <linux/types.h>
6#include <linux/init.h>
7#include <linux/stringify.h>
Rusty Russelld7e28ff2007-07-19 01:49:23 -07008#include <linux/futex.h>
9#include <linux/lguest.h>
10#include <linux/lguest_launcher.h>
11#include <linux/wait.h>
12#include <linux/err.h>
13#include <asm/semaphore.h>
Rusty Russelld7e28ff2007-07-19 01:49:23 -070014
Jes Sorensen625efab2007-10-22 11:03:28 +100015#include <asm/lguest.h>
Rusty Russelld7e28ff2007-07-19 01:49:23 -070016
17void free_pagetables(void);
18int init_pagetables(struct page **switcher_page, unsigned int pages);
19
Rusty Russelld7e28ff2007-07-19 01:49:23 -070020struct lguest_dma_info
21{
22 struct list_head list;
23 union futex_key key;
24 unsigned long dmas;
Rusty Russell48245cc2007-10-22 11:03:27 +100025 struct lguest *owner;
Rusty Russelld7e28ff2007-07-19 01:49:23 -070026 u16 next_dma;
27 u16 num_dmas;
Rusty Russelld7e28ff2007-07-19 01:49:23 -070028 u8 interrupt; /* 0 when not registered */
29};
30
Rusty Russellbff672e2007-07-26 10:41:04 -070031/*H:310 The page-table code owes a great debt of gratitude to Andi Kleen. He
32 * reviewed the original code which used "u32" for all page table entries, and
33 * insisted that it would be far clearer with explicit typing. I thought it
34 * was overkill, but he was right: it is much clearer than it was before.
35 *
36 * We have separate types for the Guest's ptes & pgds and the shadow ptes &
37 * pgds. There's already a Linux type for these (pte_t and pgd_t) but they
38 * change depending on kernel config options (PAE). */
39
40/* Each entry is identical: lower 12 bits of flags and upper 20 bits for the
41 * "page frame number" (0 == first physical page, etc). They are different
42 * types so the compiler will warn us if we mix them improperly. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070043typedef union {
44 struct { unsigned flags:12, pfn:20; };
45 struct { unsigned long val; } raw;
46} spgd_t;
47typedef union {
48 struct { unsigned flags:12, pfn:20; };
49 struct { unsigned long val; } raw;
50} spte_t;
51typedef union {
52 struct { unsigned flags:12, pfn:20; };
53 struct { unsigned long val; } raw;
54} gpgd_t;
55typedef union {
56 struct { unsigned flags:12, pfn:20; };
57 struct { unsigned long val; } raw;
58} gpte_t;
Rusty Russellbff672e2007-07-26 10:41:04 -070059
60/* We have two convenient macros to convert a "raw" value as handed to us by
61 * the Guest into the correct Guest PGD or PTE type. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070062#define mkgpte(_val) ((gpte_t){.raw.val = _val})
63#define mkgpgd(_val) ((gpgd_t){.raw.val = _val})
Rusty Russellbff672e2007-07-26 10:41:04 -070064/*:*/
Rusty Russelld7e28ff2007-07-19 01:49:23 -070065
66struct pgdir
67{
68 unsigned long cr3;
69 spgd_t *pgdir;
70};
71
Rusty Russelld7e28ff2007-07-19 01:49:23 -070072/* We have two pages shared with guests, per cpu. */
73struct lguest_pages
74{
75 /* This is the stack page mapped rw in guest */
76 char spare[PAGE_SIZE - sizeof(struct lguest_regs)];
77 struct lguest_regs regs;
78
79 /* This is the host state & guest descriptor page, ro in guest */
80 struct lguest_ro_state state;
81} __attribute__((aligned(PAGE_SIZE)));
82
83#define CHANGED_IDT 1
84#define CHANGED_GDT 2
85#define CHANGED_GDT_TLS 4 /* Actually a subset of CHANGED_GDT */
86#define CHANGED_ALL 3
87
88/* The private info the thread maintains about the guest. */
89struct lguest
90{
91 /* At end of a page shared mapped over lguest_pages in guest. */
92 unsigned long regs_page;
93 struct lguest_regs *regs;
94 struct lguest_data __user *lguest_data;
95 struct task_struct *tsk;
96 struct mm_struct *mm; /* == tsk->mm, but that becomes NULL on exit */
Rusty Russelld7e28ff2007-07-19 01:49:23 -070097 u32 pfn_limit;
Rusty Russell3c6b5bf2007-10-22 11:03:26 +100098 /* This provides the offset to the base of guest-physical
99 * memory in the Launcher. */
100 void __user *mem_base;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700101 u32 page_offset;
102 u32 cr2;
103 int halted;
104 int ts;
105 u32 next_hcall;
106 u32 esp1;
107 u8 ss1;
108
109 /* Do we need to stop what we're doing and return to userspace? */
110 int break_out;
111 wait_queue_head_t break_wq;
112
113 /* Bitmap of what has changed: see CHANGED_* above. */
114 int changed;
115 struct lguest_pages *last_pages;
116
117 /* We keep a small number of these. */
118 u32 pgdidx;
119 struct pgdir pgdirs[4];
120
121 /* Cached wakeup: we hold a reference to this task. */
122 struct task_struct *wake;
123
124 unsigned long noirq_start, noirq_end;
125 int dma_is_pending;
126 unsigned long pending_dma; /* struct lguest_dma */
127 unsigned long pending_key; /* address they're sending to */
128
129 unsigned int stack_pages;
130 u32 tsc_khz;
131
132 struct lguest_dma_info dma[LGUEST_MAX_DMA];
133
134 /* Dead? */
135 const char *dead;
136
Jes Sorensen625efab2007-10-22 11:03:28 +1000137 struct lguest_arch arch;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700138
139 /* Virtual clock device */
140 struct hrtimer hrt;
141
142 /* Pending virtual interrupts */
143 DECLARE_BITMAP(irqs_pending, LGUEST_IRQS);
144};
145
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700146extern struct mutex lguest_lock;
147
148/* core.c: */
149u32 lgread_u32(struct lguest *lg, unsigned long addr);
150void lgwrite_u32(struct lguest *lg, unsigned long addr, u32 val);
151void lgread(struct lguest *lg, void *buf, unsigned long addr, unsigned len);
152void lgwrite(struct lguest *lg, unsigned long, const void *buf, unsigned len);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700153int lguest_address_ok(const struct lguest *lg,
154 unsigned long addr, unsigned long len);
155int run_guest(struct lguest *lg, unsigned long __user *user);
156
157
158/* interrupts_and_traps.c: */
159void maybe_do_interrupt(struct lguest *lg);
160int deliver_trap(struct lguest *lg, unsigned int num);
161void load_guest_idt_entry(struct lguest *lg, unsigned int i, u32 low, u32 hi);
162void guest_set_stack(struct lguest *lg, u32 seg, u32 esp, unsigned int pages);
163void pin_stack_pages(struct lguest *lg);
164void setup_default_idt_entries(struct lguest_ro_state *state,
165 const unsigned long *def);
166void copy_traps(const struct lguest *lg, struct desc_struct *idt,
167 const unsigned long *def);
168void guest_set_clockevent(struct lguest *lg, unsigned long delta);
169void init_clockdev(struct lguest *lg);
170
171/* segments.c: */
172void setup_default_gdt_entries(struct lguest_ro_state *state);
173void setup_guest_gdt(struct lguest *lg);
174void load_guest_gdt(struct lguest *lg, unsigned long table, u32 num);
175void guest_load_tls(struct lguest *lg, unsigned long tls_array);
176void copy_gdt(const struct lguest *lg, struct desc_struct *gdt);
177void copy_gdt_tls(const struct lguest *lg, struct desc_struct *gdt);
178
179/* page_tables.c: */
180int init_guest_pagetable(struct lguest *lg, unsigned long pgtable);
181void free_guest_pagetable(struct lguest *lg);
182void guest_new_pagetable(struct lguest *lg, unsigned long pgtable);
183void guest_set_pmd(struct lguest *lg, unsigned long cr3, u32 i);
184void guest_pagetable_clear_all(struct lguest *lg);
185void guest_pagetable_flush_user(struct lguest *lg);
186void guest_set_pte(struct lguest *lg, unsigned long cr3,
187 unsigned long vaddr, gpte_t val);
188void map_switcher_in_guest(struct lguest *lg, struct lguest_pages *pages);
189int demand_page(struct lguest *info, unsigned long cr2, int errcode);
190void pin_page(struct lguest *lg, unsigned long vaddr);
191
Jes Sorensen625efab2007-10-22 11:03:28 +1000192/* <arch>/core.c: */
193void lguest_arch_host_init(void);
194void lguest_arch_host_fini(void);
195void lguest_arch_run_guest(struct lguest *lg);
196void lguest_arch_handle_trap(struct lguest *lg);
197
198/* <arch>/switcher.S: */
199extern char start_switcher_text[], end_switcher_text[], switch_to_guest[];
200
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700201/* lguest_user.c: */
202int lguest_device_init(void);
203void lguest_device_remove(void);
204
205/* io.c: */
206void lguest_io_init(void);
207int bind_dma(struct lguest *lg,
208 unsigned long key, unsigned long udma, u16 numdmas, u8 interrupt);
209void send_dma(struct lguest *info, unsigned long key, unsigned long udma);
210void release_all_dma(struct lguest *lg);
211unsigned long get_dma_buffer(struct lguest *lg, unsigned long key,
212 unsigned long *interrupt);
213
214/* hypercalls.c: */
215void do_hypercalls(struct lguest *lg);
Rusty Russell6c8dca52007-07-27 13:42:52 +1000216void write_timestamp(struct lguest *lg);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700217
Rusty Russelldde79782007-07-26 10:41:03 -0700218/*L:035
219 * Let's step aside for the moment, to study one important routine that's used
220 * widely in the Host code.
221 *
222 * There are many cases where the Guest does something invalid, like pass crap
223 * to a hypercall. Since only the Guest kernel can make hypercalls, it's quite
224 * acceptable to simply terminate the Guest and give the Launcher a nicely
225 * formatted reason. It's also simpler for the Guest itself, which doesn't
226 * need to check most hypercalls for "success"; if you're still running, it
227 * succeeded.
228 *
229 * Once this is called, the Guest will never run again, so most Host code can
230 * call this then continue as if nothing had happened. This means many
231 * functions don't have to explicitly return an error code, which keeps the
232 * code simple.
233 *
234 * It also means that this can be called more than once: only the first one is
235 * remembered. The only trick is that we still need to kill the Guest even if
236 * we can't allocate memory to store the reason. Linux has a neat way of
237 * packing error codes into invalid pointers, so we use that here.
238 *
239 * Like any macro which uses an "if", it is safely wrapped in a run-once "do {
240 * } while(0)".
241 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700242#define kill_guest(lg, fmt...) \
243do { \
244 if (!(lg)->dead) { \
245 (lg)->dead = kasprintf(GFP_ATOMIC, fmt); \
246 if (!(lg)->dead) \
247 (lg)->dead = ERR_PTR(-ENOMEM); \
248 } \
249} while(0)
Rusty Russelldde79782007-07-26 10:41:03 -0700250/* (End of aside) :*/
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700251
252static inline unsigned long guest_pa(struct lguest *lg, unsigned long vaddr)
253{
254 return vaddr - lg->page_offset;
255}
256#endif /* __ASSEMBLY__ */
257#endif /* _LGUEST_H */