]>
Commit | Line | Data |
---|---|---|
d7e28ffe RR |
1 | #ifndef _LGUEST_H |
2 | #define _LGUEST_H | |
3 | ||
d7e28ffe RR |
4 | #ifndef __ASSEMBLY__ |
5 | #include <linux/types.h> | |
6 | #include <linux/init.h> | |
7 | #include <linux/stringify.h> | |
d7e28ffe RR |
8 | #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> | |
d7e28ffe | 14 | |
625efab1 | 15 | #include <asm/lguest.h> |
d7e28ffe RR |
16 | |
17 | void free_pagetables(void); | |
18 | int init_pagetables(struct page **switcher_page, unsigned int pages); | |
19 | ||
d7e28ffe RR |
20 | struct lguest_dma_info |
21 | { | |
22 | struct list_head list; | |
23 | union futex_key key; | |
24 | unsigned long dmas; | |
48245cc0 | 25 | struct lguest *owner; |
d7e28ffe RR |
26 | u16 next_dma; |
27 | u16 num_dmas; | |
d7e28ffe RR |
28 | u8 interrupt; /* 0 when not registered */ |
29 | }; | |
30 | ||
bff672e6 RR |
31 | /*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. */ | |
d7e28ffe RR |
43 | typedef union { |
44 | struct { unsigned flags:12, pfn:20; }; | |
45 | struct { unsigned long val; } raw; | |
46 | } spgd_t; | |
47 | typedef union { | |
48 | struct { unsigned flags:12, pfn:20; }; | |
49 | struct { unsigned long val; } raw; | |
50 | } spte_t; | |
51 | typedef union { | |
52 | struct { unsigned flags:12, pfn:20; }; | |
53 | struct { unsigned long val; } raw; | |
54 | } gpgd_t; | |
55 | typedef union { | |
56 | struct { unsigned flags:12, pfn:20; }; | |
57 | struct { unsigned long val; } raw; | |
58 | } gpte_t; | |
bff672e6 RR |
59 | |
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. */ | |
d7e28ffe RR |
62 | #define mkgpte(_val) ((gpte_t){.raw.val = _val}) |
63 | #define mkgpgd(_val) ((gpgd_t){.raw.val = _val}) | |
bff672e6 | 64 | /*:*/ |
d7e28ffe RR |
65 | |
66 | struct pgdir | |
67 | { | |
68 | unsigned long cr3; | |
69 | spgd_t *pgdir; | |
70 | }; | |
71 | ||
d7e28ffe RR |
72 | /* We have two pages shared with guests, per cpu. */ |
73 | struct 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. */ | |
89 | struct 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 */ | |
d7e28ffe | 97 | u32 pfn_limit; |
3c6b5bfa RR |
98 | /* This provides the offset to the base of guest-physical |
99 | * memory in the Launcher. */ | |
100 | void __user *mem_base; | |
d7e28ffe RR |
101 | u32 page_offset; |
102 | u32 cr2; | |
103 | int halted; | |
104 | int ts; | |
105 | u32 next_hcall; | |
106 | u32 esp1; | |
107 | u8 ss1; | |
108 | ||
cc6d4fbc | 109 | /* If a hypercall was asked for, this points to the arguments. */ |
b410e7b1 | 110 | struct hcall_args *hcall; |
cc6d4fbc | 111 | |
d7e28ffe RR |
112 | /* Do we need to stop what we're doing and return to userspace? */ |
113 | int break_out; | |
114 | wait_queue_head_t break_wq; | |
115 | ||
116 | /* Bitmap of what has changed: see CHANGED_* above. */ | |
117 | int changed; | |
118 | struct lguest_pages *last_pages; | |
119 | ||
120 | /* We keep a small number of these. */ | |
121 | u32 pgdidx; | |
122 | struct pgdir pgdirs[4]; | |
123 | ||
124 | /* Cached wakeup: we hold a reference to this task. */ | |
125 | struct task_struct *wake; | |
126 | ||
127 | unsigned long noirq_start, noirq_end; | |
128 | int dma_is_pending; | |
129 | unsigned long pending_dma; /* struct lguest_dma */ | |
130 | unsigned long pending_key; /* address they're sending to */ | |
131 | ||
132 | unsigned int stack_pages; | |
133 | u32 tsc_khz; | |
134 | ||
135 | struct lguest_dma_info dma[LGUEST_MAX_DMA]; | |
136 | ||
137 | /* Dead? */ | |
138 | const char *dead; | |
139 | ||
625efab1 | 140 | struct lguest_arch arch; |
d7e28ffe RR |
141 | |
142 | /* Virtual clock device */ | |
143 | struct hrtimer hrt; | |
144 | ||
145 | /* Pending virtual interrupts */ | |
146 | DECLARE_BITMAP(irqs_pending, LGUEST_IRQS); | |
147 | }; | |
148 | ||
d7e28ffe RR |
149 | extern struct mutex lguest_lock; |
150 | ||
151 | /* core.c: */ | |
152 | u32 lgread_u32(struct lguest *lg, unsigned long addr); | |
153 | void lgwrite_u32(struct lguest *lg, unsigned long addr, u32 val); | |
154 | void lgread(struct lguest *lg, void *buf, unsigned long addr, unsigned len); | |
155 | void lgwrite(struct lguest *lg, unsigned long, const void *buf, unsigned len); | |
d7e28ffe RR |
156 | int lguest_address_ok(const struct lguest *lg, |
157 | unsigned long addr, unsigned long len); | |
158 | int run_guest(struct lguest *lg, unsigned long __user *user); | |
159 | ||
160 | ||
161 | /* interrupts_and_traps.c: */ | |
162 | void maybe_do_interrupt(struct lguest *lg); | |
163 | int deliver_trap(struct lguest *lg, unsigned int num); | |
164 | void load_guest_idt_entry(struct lguest *lg, unsigned int i, u32 low, u32 hi); | |
165 | void guest_set_stack(struct lguest *lg, u32 seg, u32 esp, unsigned int pages); | |
166 | void pin_stack_pages(struct lguest *lg); | |
167 | void setup_default_idt_entries(struct lguest_ro_state *state, | |
168 | const unsigned long *def); | |
169 | void copy_traps(const struct lguest *lg, struct desc_struct *idt, | |
170 | const unsigned long *def); | |
171 | void guest_set_clockevent(struct lguest *lg, unsigned long delta); | |
172 | void init_clockdev(struct lguest *lg); | |
173 | ||
174 | /* segments.c: */ | |
175 | void setup_default_gdt_entries(struct lguest_ro_state *state); | |
176 | void setup_guest_gdt(struct lguest *lg); | |
177 | void load_guest_gdt(struct lguest *lg, unsigned long table, u32 num); | |
178 | void guest_load_tls(struct lguest *lg, unsigned long tls_array); | |
179 | void copy_gdt(const struct lguest *lg, struct desc_struct *gdt); | |
180 | void copy_gdt_tls(const struct lguest *lg, struct desc_struct *gdt); | |
181 | ||
182 | /* page_tables.c: */ | |
183 | int init_guest_pagetable(struct lguest *lg, unsigned long pgtable); | |
184 | void free_guest_pagetable(struct lguest *lg); | |
185 | void guest_new_pagetable(struct lguest *lg, unsigned long pgtable); | |
186 | void guest_set_pmd(struct lguest *lg, unsigned long cr3, u32 i); | |
187 | void guest_pagetable_clear_all(struct lguest *lg); | |
188 | void guest_pagetable_flush_user(struct lguest *lg); | |
189 | void guest_set_pte(struct lguest *lg, unsigned long cr3, | |
190 | unsigned long vaddr, gpte_t val); | |
191 | void map_switcher_in_guest(struct lguest *lg, struct lguest_pages *pages); | |
192 | int demand_page(struct lguest *info, unsigned long cr2, int errcode); | |
193 | void pin_page(struct lguest *lg, unsigned long vaddr); | |
194 | ||
625efab1 JS |
195 | /* <arch>/core.c: */ |
196 | void lguest_arch_host_init(void); | |
197 | void lguest_arch_host_fini(void); | |
198 | void lguest_arch_run_guest(struct lguest *lg); | |
199 | void lguest_arch_handle_trap(struct lguest *lg); | |
b410e7b1 JS |
200 | int lguest_arch_init_hypercalls(struct lguest *lg); |
201 | int lguest_arch_do_hcall(struct lguest *lg, struct hcall_args *args); | |
d612cde0 | 202 | void lguest_arch_setup_regs(struct lguest *lg, unsigned long start); |
625efab1 JS |
203 | |
204 | /* <arch>/switcher.S: */ | |
205 | extern char start_switcher_text[], end_switcher_text[], switch_to_guest[]; | |
206 | ||
d7e28ffe RR |
207 | /* lguest_user.c: */ |
208 | int lguest_device_init(void); | |
209 | void lguest_device_remove(void); | |
210 | ||
211 | /* io.c: */ | |
212 | void lguest_io_init(void); | |
213 | int bind_dma(struct lguest *lg, | |
214 | unsigned long key, unsigned long udma, u16 numdmas, u8 interrupt); | |
215 | void send_dma(struct lguest *info, unsigned long key, unsigned long udma); | |
216 | void release_all_dma(struct lguest *lg); | |
217 | unsigned long get_dma_buffer(struct lguest *lg, unsigned long key, | |
218 | unsigned long *interrupt); | |
219 | ||
220 | /* hypercalls.c: */ | |
221 | void do_hypercalls(struct lguest *lg); | |
6c8dca5d | 222 | void write_timestamp(struct lguest *lg); |
d7e28ffe | 223 | |
dde79789 RR |
224 | /*L:035 |
225 | * Let's step aside for the moment, to study one important routine that's used | |
226 | * widely in the Host code. | |
227 | * | |
228 | * There are many cases where the Guest does something invalid, like pass crap | |
229 | * to a hypercall. Since only the Guest kernel can make hypercalls, it's quite | |
230 | * acceptable to simply terminate the Guest and give the Launcher a nicely | |
231 | * formatted reason. It's also simpler for the Guest itself, which doesn't | |
232 | * need to check most hypercalls for "success"; if you're still running, it | |
233 | * succeeded. | |
234 | * | |
235 | * Once this is called, the Guest will never run again, so most Host code can | |
236 | * call this then continue as if nothing had happened. This means many | |
237 | * functions don't have to explicitly return an error code, which keeps the | |
238 | * code simple. | |
239 | * | |
240 | * It also means that this can be called more than once: only the first one is | |
241 | * remembered. The only trick is that we still need to kill the Guest even if | |
242 | * we can't allocate memory to store the reason. Linux has a neat way of | |
243 | * packing error codes into invalid pointers, so we use that here. | |
244 | * | |
245 | * Like any macro which uses an "if", it is safely wrapped in a run-once "do { | |
246 | * } while(0)". | |
247 | */ | |
d7e28ffe RR |
248 | #define kill_guest(lg, fmt...) \ |
249 | do { \ | |
250 | if (!(lg)->dead) { \ | |
251 | (lg)->dead = kasprintf(GFP_ATOMIC, fmt); \ | |
252 | if (!(lg)->dead) \ | |
253 | (lg)->dead = ERR_PTR(-ENOMEM); \ | |
254 | } \ | |
255 | } while(0) | |
dde79789 | 256 | /* (End of aside) :*/ |
d7e28ffe RR |
257 | |
258 | static inline unsigned long guest_pa(struct lguest *lg, unsigned long vaddr) | |
259 | { | |
260 | return vaddr - lg->page_offset; | |
261 | } | |
262 | #endif /* __ASSEMBLY__ */ | |
263 | #endif /* _LGUEST_H */ |