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 /* 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. */
65 struct { unsigned flags
:12, pfn
:20; };
66 struct { unsigned long val
; } raw
;
69 struct { unsigned flags
:12, pfn
:20; };
70 struct { unsigned long val
; } raw
;
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
;
80 #define mkgpte(_val) ((gpte_t){.raw.val = _val})
81 #define mkgpgd(_val) ((gpgd_t){.raw.val = _val})
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. */
94 struct Xgt_desc_struct host_idt_desc
;
95 struct Xgt_desc_struct host_gdt_desc
;
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. */
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. */
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 */
141 /* Do we need to stop what we're doing and return to userspace? */
143 wait_queue_head_t break_wq
;
145 /* Bitmap of what has changed: see CHANGED_* above. */
147 struct lguest_pages
*last_pages
;
149 /* We keep a small number of these. */
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
;
158 unsigned long pending_dma
; /* struct lguest_dma */
159 unsigned long pending_key
; /* address they're sending to */
161 unsigned int stack_pages
;
164 struct lguest_dma_info dma
[LGUEST_MAX_DMA
];
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 */
179 /* Pending virtual interrupts */
180 DECLARE_BITMAP(irqs_pending
, LGUEST_IRQS
);
183 extern struct lguest lguests
[];
184 extern struct mutex lguest_lock
;
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
);
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
);
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
);
232 int lguest_device_init(void);
233 void lguest_device_remove(void);
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
);
245 void do_hypercalls(struct lguest
*lg
);
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
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
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 {
271 #define kill_guest(lg, fmt...) \
274 (lg)->dead = kasprintf(GFP_ATOMIC, fmt); \
276 (lg)->dead = ERR_PTR(-ENOMEM); \
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 */