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/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"
28 /* Manually saved part. */
29 unsigned long ebx, ecx, edx;
30 unsigned long esi, edi, ebp;
33 unsigned long fs, ds, es;
34 unsigned long trapnum, errcode;
35 /* Trap pushed part */
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;
58 u8 interrupt; /* 0 when not registered */
61 /*H:310 The page-table code owes a great debt of gratitude to Andi Kleen. He
62 * reviewed the original code which used "u32" for all page table entries, and
63 * insisted that it would be far clearer with explicit typing. I thought it
64 * was overkill, but he was right: it is much clearer than it was before.
66 * We have separate types for the Guest's ptes & pgds and the shadow ptes &
67 * pgds. There's already a Linux type for these (pte_t and pgd_t) but they
68 * change depending on kernel config options (PAE). */
70 /* Each entry is identical: lower 12 bits of flags and upper 20 bits for the
71 * "page frame number" (0 == first physical page, etc). They are different
72 * types so the compiler will warn us if we mix them improperly. */
74 struct { unsigned flags:12, pfn:20; };
75 struct { unsigned long val; } raw;
78 struct { unsigned flags:12, pfn:20; };
79 struct { unsigned long val; } raw;
82 struct { unsigned flags:12, pfn:20; };
83 struct { unsigned long val; } raw;
86 struct { unsigned flags:12, pfn:20; };
87 struct { unsigned long val; } raw;
90 /* We have two convenient macros to convert a "raw" value as handed to us by
91 * the Guest into the correct Guest PGD or PTE type. */
92 #define mkgpte(_val) ((gpte_t){.raw.val = _val})
93 #define mkgpgd(_val) ((gpgd_t){.raw.val = _val})
102 /* This is a guest-specific page (mapped ro) into the guest. */
103 struct lguest_ro_state
105 /* Host information we need to restore when we switch back. */
107 struct Xgt_desc_struct host_idt_desc;
108 struct Xgt_desc_struct host_gdt_desc;
111 /* Fields which are used when guest is running. */
112 struct Xgt_desc_struct guest_idt_desc;
113 struct Xgt_desc_struct guest_gdt_desc;
114 struct i386_hw_tss guest_tss;
115 struct desc_struct guest_idt[IDT_ENTRIES];
116 struct desc_struct guest_gdt[GDT_ENTRIES];
119 /* We have two pages shared with guests, per cpu. */
122 /* This is the stack page mapped rw in guest */
123 char spare[PAGE_SIZE - sizeof(struct lguest_regs)];
124 struct lguest_regs regs;
126 /* This is the host state & guest descriptor page, ro in guest */
127 struct lguest_ro_state state;
128 } __attribute__((aligned(PAGE_SIZE)));
130 #define CHANGED_IDT 1
131 #define CHANGED_GDT 2
132 #define CHANGED_GDT_TLS 4 /* Actually a subset of CHANGED_GDT */
133 #define CHANGED_ALL 3
135 /* The private info the thread maintains about the guest. */
138 /* At end of a page shared mapped over lguest_pages in guest. */
139 unsigned long regs_page;
140 struct lguest_regs *regs;
141 struct lguest_data __user *lguest_data;
142 struct task_struct *tsk;
143 struct mm_struct *mm; /* == tsk->mm, but that becomes NULL on exit */
154 /* Do we need to stop what we're doing and return to userspace? */
156 wait_queue_head_t break_wq;
158 /* Bitmap of what has changed: see CHANGED_* above. */
160 struct lguest_pages *last_pages;
162 /* We keep a small number of these. */
164 struct pgdir pgdirs[4];
166 /* Cached wakeup: we hold a reference to this task. */
167 struct task_struct *wake;
169 unsigned long noirq_start, noirq_end;
171 unsigned long pending_dma; /* struct lguest_dma */
172 unsigned long pending_key; /* address they're sending to */
174 unsigned int stack_pages;
177 struct lguest_dma_info dma[LGUEST_MAX_DMA];
182 /* The GDT entries copied into lguest_ro_state when running. */
183 struct desc_struct gdt[GDT_ENTRIES];
185 /* The IDT entries: some copied into lguest_ro_state when running. */
186 struct desc_struct idt[FIRST_EXTERNAL_VECTOR+LGUEST_IRQS];
187 struct desc_struct syscall_idt;
189 /* Virtual clock device */
192 /* Pending virtual interrupts */
193 DECLARE_BITMAP(irqs_pending, LGUEST_IRQS);
196 extern struct lguest lguests[];
197 extern struct mutex lguest_lock;
200 u32 lgread_u32(struct lguest *lg, unsigned long addr);
201 void lgwrite_u32(struct lguest *lg, unsigned long addr, u32 val);
202 void lgread(struct lguest *lg, void *buf, unsigned long addr, unsigned len);
203 void lgwrite(struct lguest *lg, unsigned long, const void *buf, unsigned len);
204 int find_free_guest(void);
205 int lguest_address_ok(const struct lguest *lg,
206 unsigned long addr, unsigned long len);
207 int run_guest(struct lguest *lg, unsigned long __user *user);
210 /* interrupts_and_traps.c: */
211 void maybe_do_interrupt(struct lguest *lg);
212 int deliver_trap(struct lguest *lg, unsigned int num);
213 void load_guest_idt_entry(struct lguest *lg, unsigned int i, u32 low, u32 hi);
214 void guest_set_stack(struct lguest *lg, u32 seg, u32 esp, unsigned int pages);
215 void pin_stack_pages(struct lguest *lg);
216 void setup_default_idt_entries(struct lguest_ro_state *state,
217 const unsigned long *def);
218 void copy_traps(const struct lguest *lg, struct desc_struct *idt,
219 const unsigned long *def);
220 void guest_set_clockevent(struct lguest *lg, unsigned long delta);
221 void init_clockdev(struct lguest *lg);
224 void setup_default_gdt_entries(struct lguest_ro_state *state);
225 void setup_guest_gdt(struct lguest *lg);
226 void load_guest_gdt(struct lguest *lg, unsigned long table, u32 num);
227 void guest_load_tls(struct lguest *lg, unsigned long tls_array);
228 void copy_gdt(const struct lguest *lg, struct desc_struct *gdt);
229 void copy_gdt_tls(const struct lguest *lg, struct desc_struct *gdt);
232 int init_guest_pagetable(struct lguest *lg, unsigned long pgtable);
233 void free_guest_pagetable(struct lguest *lg);
234 void guest_new_pagetable(struct lguest *lg, unsigned long pgtable);
235 void guest_set_pmd(struct lguest *lg, unsigned long cr3, u32 i);
236 void guest_pagetable_clear_all(struct lguest *lg);
237 void guest_pagetable_flush_user(struct lguest *lg);
238 void guest_set_pte(struct lguest *lg, unsigned long cr3,
239 unsigned long vaddr, gpte_t val);
240 void map_switcher_in_guest(struct lguest *lg, struct lguest_pages *pages);
241 int demand_page(struct lguest *info, unsigned long cr2, int errcode);
242 void pin_page(struct lguest *lg, unsigned long vaddr);
245 int lguest_device_init(void);
246 void lguest_device_remove(void);
249 void lguest_io_init(void);
250 int bind_dma(struct lguest *lg,
251 unsigned long key, unsigned long udma, u16 numdmas, u8 interrupt);
252 void send_dma(struct lguest *info, unsigned long key, unsigned long udma);
253 void release_all_dma(struct lguest *lg);
254 unsigned long get_dma_buffer(struct lguest *lg, unsigned long key,
255 unsigned long *interrupt);
258 void do_hypercalls(struct lguest *lg);
259 void write_timestamp(struct lguest *lg);
262 * Let's step aside for the moment, to study one important routine that's used
263 * widely in the Host code.
265 * There are many cases where the Guest does something invalid, like pass crap
266 * to a hypercall. Since only the Guest kernel can make hypercalls, it's quite
267 * acceptable to simply terminate the Guest and give the Launcher a nicely
268 * formatted reason. It's also simpler for the Guest itself, which doesn't
269 * need to check most hypercalls for "success"; if you're still running, it
272 * Once this is called, the Guest will never run again, so most Host code can
273 * call this then continue as if nothing had happened. This means many
274 * functions don't have to explicitly return an error code, which keeps the
277 * It also means that this can be called more than once: only the first one is
278 * remembered. The only trick is that we still need to kill the Guest even if
279 * we can't allocate memory to store the reason. Linux has a neat way of
280 * packing error codes into invalid pointers, so we use that here.
282 * Like any macro which uses an "if", it is safely wrapped in a run-once "do {
285 #define kill_guest(lg, fmt...) \
288 (lg)->dead = kasprintf(GFP_ATOMIC, fmt); \
290 (lg)->dead = ERR_PTR(-ENOMEM); \
293 /* (End of aside) :*/
295 static inline unsigned long guest_pa(struct lguest *lg, unsigned long vaddr)
297 return vaddr - lg->page_offset;
299 #endif /* __ASSEMBLY__ */
300 #endif /* _LGUEST_H */