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)
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"
27 /* Manually saved part. */
28 unsigned long ebx, ecx, edx;
29 unsigned long esi, edi, ebp;
32 unsigned long fs, ds, es;
33 unsigned long trapnum, errcode;
34 /* Trap pushed part */
42 void free_pagetables(void);
43 int init_pagetables(struct page **switcher_page, unsigned int pages);
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})
49 struct lguest_dma_info
51 struct list_head list;
57 u8 interrupt; /* 0 when not registered */
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.
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). */
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. */
73 struct { unsigned flags:12, pfn:20; };
74 struct { unsigned long val; } raw;
77 struct { unsigned flags:12, pfn:20; };
78 struct { unsigned long val; } raw;
81 struct { unsigned flags:12, pfn:20; };
82 struct { unsigned long val; } raw;
85 struct { unsigned flags:12, pfn:20; };
86 struct { unsigned long val; } raw;
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})
101 /* This is a guest-specific page (mapped ro) into the guest. */
102 struct lguest_ro_state
104 /* Host information we need to restore when we switch back. */
106 struct Xgt_desc_struct host_idt_desc;
107 struct Xgt_desc_struct host_gdt_desc;
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];
118 /* We have two pages shared with guests, per cpu. */
121 /* This is the stack page mapped rw in guest */
122 char spare[PAGE_SIZE - sizeof(struct lguest_regs)];
123 struct lguest_regs regs;
125 /* This is the host state & guest descriptor page, ro in guest */
126 struct lguest_ro_state state;
127 } __attribute__((aligned(PAGE_SIZE)));
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
134 /* The private info the thread maintains about the guest. */
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 */
145 /* This provides the offset to the base of guest-physical
146 * memory in the Launcher. */
147 void __user *mem_base;
156 /* Do we need to stop what we're doing and return to userspace? */
158 wait_queue_head_t break_wq;
160 /* Bitmap of what has changed: see CHANGED_* above. */
162 struct lguest_pages *last_pages;
164 /* We keep a small number of these. */
166 struct pgdir pgdirs[4];
168 /* Cached wakeup: we hold a reference to this task. */
169 struct task_struct *wake;
171 unsigned long noirq_start, noirq_end;
173 unsigned long pending_dma; /* struct lguest_dma */
174 unsigned long pending_key; /* address they're sending to */
176 unsigned int stack_pages;
179 struct lguest_dma_info dma[LGUEST_MAX_DMA];
184 /* The GDT entries copied into lguest_ro_state when running. */
185 struct desc_struct gdt[GDT_ENTRIES];
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;
191 /* Virtual clock device */
194 /* Pending virtual interrupts */
195 DECLARE_BITMAP(irqs_pending, LGUEST_IRQS);
198 extern struct lguest lguests[];
199 extern struct mutex lguest_lock;
202 u32 lgread_u32(struct lguest *lg, unsigned long addr);
203 void lgwrite_u32(struct lguest *lg, unsigned long addr, u32 val);
204 void lgread(struct lguest *lg, void *buf, unsigned long addr, unsigned len);
205 void lgwrite(struct lguest *lg, unsigned long, const void *buf, unsigned len);
206 int find_free_guest(void);
207 int lguest_address_ok(const struct lguest *lg,
208 unsigned long addr, unsigned long len);
209 int run_guest(struct lguest *lg, unsigned long __user *user);
212 /* interrupts_and_traps.c: */
213 void maybe_do_interrupt(struct lguest *lg);
214 int deliver_trap(struct lguest *lg, unsigned int num);
215 void load_guest_idt_entry(struct lguest *lg, unsigned int i, u32 low, u32 hi);
216 void guest_set_stack(struct lguest *lg, u32 seg, u32 esp, unsigned int pages);
217 void pin_stack_pages(struct lguest *lg);
218 void setup_default_idt_entries(struct lguest_ro_state *state,
219 const unsigned long *def);
220 void copy_traps(const struct lguest *lg, struct desc_struct *idt,
221 const unsigned long *def);
222 void guest_set_clockevent(struct lguest *lg, unsigned long delta);
223 void init_clockdev(struct lguest *lg);
226 void setup_default_gdt_entries(struct lguest_ro_state *state);
227 void setup_guest_gdt(struct lguest *lg);
228 void load_guest_gdt(struct lguest *lg, unsigned long table, u32 num);
229 void guest_load_tls(struct lguest *lg, unsigned long tls_array);
230 void copy_gdt(const struct lguest *lg, struct desc_struct *gdt);
231 void copy_gdt_tls(const struct lguest *lg, struct desc_struct *gdt);
234 int init_guest_pagetable(struct lguest *lg, unsigned long pgtable);
235 void free_guest_pagetable(struct lguest *lg);
236 void guest_new_pagetable(struct lguest *lg, unsigned long pgtable);
237 void guest_set_pmd(struct lguest *lg, unsigned long cr3, u32 i);
238 void guest_pagetable_clear_all(struct lguest *lg);
239 void guest_pagetable_flush_user(struct lguest *lg);
240 void guest_set_pte(struct lguest *lg, unsigned long cr3,
241 unsigned long vaddr, gpte_t val);
242 void map_switcher_in_guest(struct lguest *lg, struct lguest_pages *pages);
243 int demand_page(struct lguest *info, unsigned long cr2, int errcode);
244 void pin_page(struct lguest *lg, unsigned long vaddr);
247 int lguest_device_init(void);
248 void lguest_device_remove(void);
251 void lguest_io_init(void);
252 int bind_dma(struct lguest *lg,
253 unsigned long key, unsigned long udma, u16 numdmas, u8 interrupt);
254 void send_dma(struct lguest *info, unsigned long key, unsigned long udma);
255 void release_all_dma(struct lguest *lg);
256 unsigned long get_dma_buffer(struct lguest *lg, unsigned long key,
257 unsigned long *interrupt);
260 void do_hypercalls(struct lguest *lg);
261 void write_timestamp(struct lguest *lg);
264 * Let's step aside for the moment, to study one important routine that's used
265 * widely in the Host code.
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
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
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.
284 * Like any macro which uses an "if", it is safely wrapped in a run-once "do {
287 #define kill_guest(lg, fmt...) \
290 (lg)->dead = kasprintf(GFP_ATOMIC, fmt); \
292 (lg)->dead = ERR_PTR(-ENOMEM); \
295 /* (End of aside) :*/
297 static inline unsigned long guest_pa(struct lguest *lg, unsigned long vaddr)
299 return vaddr - lg->page_offset;
301 #endif /* __ASSEMBLY__ */
302 #endif /* _LGUEST_H */