1
2
3
4
5
6#include <linux/module.h>
7#include <linux/stringify.h>
8#include <linux/stddef.h>
9#include <linux/io.h>
10#include <linux/mm.h>
11#include <linux/vmalloc.h>
12#include <linux/cpu.h>
13#include <linux/freezer.h>
14#include <linux/highmem.h>
15#include <asm/paravirt.h>
16#include <asm/pgtable.h>
17#include <asm/uaccess.h>
18#include <asm/poll.h>
19#include <asm/asm-offsets.h>
20#include "lg.h"
21
22
23static struct vm_struct *switcher_vma;
24static struct page **switcher_page;
25
26
27DEFINE_MUTEX(lguest_lock);
28
29
30
31
32
33
34
35
36
37
38
39
40
41static __init int map_switcher(void)
42{
43 int i, err;
44 struct page **pagep;
45
46
47
48
49
50
51
52
53
54
55
56
57
58 switcher_page = kmalloc(sizeof(switcher_page[0])*TOTAL_SWITCHER_PAGES,
59 GFP_KERNEL);
60 if (!switcher_page) {
61 err = -ENOMEM;
62 goto out;
63 }
64
65
66
67
68
69 for (i = 0; i < TOTAL_SWITCHER_PAGES; i++) {
70 switcher_page[i] = alloc_page(GFP_KERNEL|__GFP_ZERO);
71 if (!switcher_page[i]) {
72 err = -ENOMEM;
73 goto free_some_pages;
74 }
75 }
76
77
78
79
80
81
82 if (SWITCHER_ADDR + (TOTAL_SWITCHER_PAGES+1)*PAGE_SIZE > FIXADDR_START){
83 err = -ENOMEM;
84 printk("lguest: mapping switcher would thwack fixmap\n");
85 goto free_pages;
86 }
87
88
89
90
91
92
93
94 switcher_vma = __get_vm_area(TOTAL_SWITCHER_PAGES * PAGE_SIZE,
95 VM_ALLOC, SWITCHER_ADDR, SWITCHER_ADDR
96 + (TOTAL_SWITCHER_PAGES+1) * PAGE_SIZE);
97 if (!switcher_vma) {
98 err = -ENOMEM;
99 printk("lguest: could not map switcher pages high\n");
100 goto free_pages;
101 }
102
103
104
105
106
107
108
109
110 pagep = switcher_page;
111 err = map_vm_area(switcher_vma, PAGE_KERNEL_EXEC, &pagep);
112 if (err) {
113 printk("lguest: map_vm_area failed: %i\n", err);
114 goto free_vma;
115 }
116
117
118
119
120
121 memcpy(switcher_vma->addr, start_switcher_text,
122 end_switcher_text - start_switcher_text);
123
124 printk(KERN_INFO "lguest: mapped switcher at %p\n",
125 switcher_vma->addr);
126
127 return 0;
128
129free_vma:
130 vunmap(switcher_vma->addr);
131free_pages:
132 i = TOTAL_SWITCHER_PAGES;
133free_some_pages:
134 for (--i; i >= 0; i--)
135 __free_pages(switcher_page[i], 0);
136 kfree(switcher_page);
137out:
138 return err;
139}
140
141
142
143static void unmap_switcher(void)
144{
145 unsigned int i;
146
147
148 vunmap(switcher_vma->addr);
149
150 for (i = 0; i < TOTAL_SWITCHER_PAGES; i++)
151 __free_pages(switcher_page[i], 0);
152 kfree(switcher_page);
153}
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170bool lguest_address_ok(const struct lguest *lg,
171 unsigned long addr, unsigned long len)
172{
173 return (addr+len) / PAGE_SIZE < lg->pfn_limit && (addr+len >= addr);
174}
175
176
177
178
179
180
181void __lgread(struct lg_cpu *cpu, void *b, unsigned long addr, unsigned bytes)
182{
183 if (!lguest_address_ok(cpu->lg, addr, bytes)
184 || copy_from_user(b, cpu->lg->mem_base + addr, bytes) != 0) {
185
186 memset(b, 0, bytes);
187 kill_guest(cpu, "bad read address %#lx len %u", addr, bytes);
188 }
189}
190
191
192void __lgwrite(struct lg_cpu *cpu, unsigned long addr, const void *b,
193 unsigned bytes)
194{
195 if (!lguest_address_ok(cpu->lg, addr, bytes)
196 || copy_to_user(cpu->lg->mem_base + addr, b, bytes) != 0)
197 kill_guest(cpu, "bad write address %#lx len %u", addr, bytes);
198}
199
200
201
202
203
204
205
206int run_guest(struct lg_cpu *cpu, unsigned long __user *user)
207{
208
209 while (!cpu->lg->dead) {
210 unsigned int irq;
211 bool more;
212
213
214 if (cpu->hcall)
215 do_hypercalls(cpu);
216
217
218
219
220
221 if (cpu->pending_notify) {
222
223
224
225
226 if (!send_notify_to_eventfd(cpu)) {
227
228 if (put_user(cpu->pending_notify, user))
229 return -EFAULT;
230 return sizeof(cpu->pending_notify);
231 }
232 }
233
234
235 if (signal_pending(current))
236 return -ERESTARTSYS;
237
238
239
240
241
242
243 irq = interrupt_pending(cpu, &more);
244 if (irq < LGUEST_IRQS)
245 try_deliver_interrupt(cpu, irq, more);
246
247
248
249
250
251
252 try_to_freeze();
253
254
255
256
257
258 if (cpu->lg->dead)
259 break;
260
261
262
263
264
265 if (cpu->halted) {
266 set_current_state(TASK_INTERRUPTIBLE);
267
268
269
270
271 if (interrupt_pending(cpu, &more) < LGUEST_IRQS)
272 set_current_state(TASK_RUNNING);
273 else
274 schedule();
275 continue;
276 }
277
278
279
280
281
282 local_irq_disable();
283
284
285 lguest_arch_run_guest(cpu);
286
287
288 local_irq_enable();
289
290
291 lguest_arch_handle_trap(cpu);
292 }
293
294
295 if (cpu->lg->dead == ERR_PTR(-ERESTART))
296 return -ERESTART;
297
298
299 return -ENOENT;
300}
301
302
303
304
305
306
307
308
309
310static int __init init(void)
311{
312 int err;
313
314
315 if (paravirt_enabled()) {
316 printk("lguest is afraid of being a guest\n");
317 return -EPERM;
318 }
319
320
321 err = map_switcher();
322 if (err)
323 goto out;
324
325
326 err = init_pagetables(switcher_page, SHARED_SWITCHER_PAGES);
327 if (err)
328 goto unmap;
329
330
331 err = init_interrupts();
332 if (err)
333 goto free_pgtables;
334
335
336 err = lguest_device_init();
337 if (err)
338 goto free_interrupts;
339
340
341 lguest_arch_host_init();
342
343
344 return 0;
345
346free_interrupts:
347 free_interrupts();
348free_pgtables:
349 free_pagetables();
350unmap:
351 unmap_switcher();
352out:
353 return err;
354}
355
356
357static void __exit fini(void)
358{
359 lguest_device_remove();
360 free_interrupts();
361 free_pagetables();
362 unmap_switcher();
363
364 lguest_arch_host_fini();
365}
366
367
368
369
370
371
372module_init(init);
373module_exit(fini);
374MODULE_LICENSE("GPL");
375MODULE_AUTHOR("Rusty Russell <rusty@rustcorp.com.au>");
376