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