]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/lguest/lguest.c
Fix lguest misannotation
[linux-2.6-omap-h63xx.git] / drivers / lguest / lguest.c
1 /*
2  * Lguest specific paravirt-ops implementation
3  *
4  * Copyright (C) 2006, Rusty Russell <rusty@rustcorp.com.au> IBM Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
14  * NON INFRINGEMENT.  See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21 #include <linux/kernel.h>
22 #include <linux/start_kernel.h>
23 #include <linux/string.h>
24 #include <linux/console.h>
25 #include <linux/screen_info.h>
26 #include <linux/irq.h>
27 #include <linux/interrupt.h>
28 #include <linux/clocksource.h>
29 #include <linux/clockchips.h>
30 #include <linux/lguest.h>
31 #include <linux/lguest_launcher.h>
32 #include <linux/lguest_bus.h>
33 #include <asm/paravirt.h>
34 #include <asm/param.h>
35 #include <asm/page.h>
36 #include <asm/pgtable.h>
37 #include <asm/desc.h>
38 #include <asm/setup.h>
39 #include <asm/e820.h>
40 #include <asm/mce.h>
41 #include <asm/io.h>
42 //#include <asm/sched-clock.h>
43
44 /* Declarations for definitions in lguest_guest.S */
45 extern char lguest_noirq_start[], lguest_noirq_end[];
46 extern const char lgstart_cli[], lgend_cli[];
47 extern const char lgstart_sti[], lgend_sti[];
48 extern const char lgstart_popf[], lgend_popf[];
49 extern const char lgstart_pushf[], lgend_pushf[];
50 extern const char lgstart_iret[], lgend_iret[];
51 extern void lguest_iret(void);
52
53 struct lguest_data lguest_data = {
54         .hcall_status = { [0 ... LHCALL_RING_SIZE-1] = 0xFF },
55         .noirq_start = (u32)lguest_noirq_start,
56         .noirq_end = (u32)lguest_noirq_end,
57         .blocked_interrupts = { 1 }, /* Block timer interrupts */
58 };
59 struct lguest_device_desc *lguest_devices;
60
61 static enum paravirt_lazy_mode lazy_mode;
62 static void lguest_lazy_mode(enum paravirt_lazy_mode mode)
63 {
64         if (mode == PARAVIRT_LAZY_FLUSH) {
65                 if (unlikely(lazy_mode != PARAVIRT_LAZY_NONE))
66                         hcall(LHCALL_FLUSH_ASYNC, 0, 0, 0);
67         } else {
68                 lazy_mode = mode;
69                 if (mode == PARAVIRT_LAZY_NONE)
70                         hcall(LHCALL_FLUSH_ASYNC, 0, 0, 0);
71         }
72 }
73
74 static void lazy_hcall(unsigned long call,
75                        unsigned long arg1,
76                        unsigned long arg2,
77                        unsigned long arg3)
78 {
79         if (lazy_mode == PARAVIRT_LAZY_NONE)
80                 hcall(call, arg1, arg2, arg3);
81         else
82                 async_hcall(call, arg1, arg2, arg3);
83 }
84
85 void async_hcall(unsigned long call,
86                  unsigned long arg1, unsigned long arg2, unsigned long arg3)
87 {
88         /* Note: This code assumes we're uniprocessor. */
89         static unsigned int next_call;
90         unsigned long flags;
91
92         local_irq_save(flags);
93         if (lguest_data.hcall_status[next_call] != 0xFF) {
94                 /* Table full, so do normal hcall which will flush table. */
95                 hcall(call, arg1, arg2, arg3);
96         } else {
97                 lguest_data.hcalls[next_call].eax = call;
98                 lguest_data.hcalls[next_call].edx = arg1;
99                 lguest_data.hcalls[next_call].ebx = arg2;
100                 lguest_data.hcalls[next_call].ecx = arg3;
101                 /* Make sure host sees arguments before "valid" flag. */
102                 wmb();
103                 lguest_data.hcall_status[next_call] = 0;
104                 if (++next_call == LHCALL_RING_SIZE)
105                         next_call = 0;
106         }
107         local_irq_restore(flags);
108 }
109
110 void lguest_send_dma(unsigned long key, struct lguest_dma *dma)
111 {
112         dma->used_len = 0;
113         hcall(LHCALL_SEND_DMA, key, __pa(dma), 0);
114 }
115
116 int lguest_bind_dma(unsigned long key, struct lguest_dma *dmas,
117                     unsigned int num, u8 irq)
118 {
119         if (!hcall(LHCALL_BIND_DMA, key, __pa(dmas), (num << 8) | irq))
120                 return -ENOMEM;
121         return 0;
122 }
123
124 void lguest_unbind_dma(unsigned long key, struct lguest_dma *dmas)
125 {
126         hcall(LHCALL_BIND_DMA, key, __pa(dmas), 0);
127 }
128
129 /* For guests, device memory can be used as normal memory, so we cast away the
130  * __iomem to quieten sparse. */
131 void *lguest_map(unsigned long phys_addr, unsigned long pages)
132 {
133         return (__force void *)ioremap(phys_addr, PAGE_SIZE*pages);
134 }
135
136 void lguest_unmap(void *addr)
137 {
138         iounmap((__force void __iomem *)addr);
139 }
140
141 static unsigned long save_fl(void)
142 {
143         return lguest_data.irq_enabled;
144 }
145
146 static void restore_fl(unsigned long flags)
147 {
148         /* FIXME: Check if interrupt pending... */
149         lguest_data.irq_enabled = flags;
150 }
151
152 static void irq_disable(void)
153 {
154         lguest_data.irq_enabled = 0;
155 }
156
157 static void irq_enable(void)
158 {
159         /* FIXME: Check if interrupt pending... */
160         lguest_data.irq_enabled = X86_EFLAGS_IF;
161 }
162
163 static void lguest_write_idt_entry(struct desc_struct *dt,
164                                    int entrynum, u32 low, u32 high)
165 {
166         write_dt_entry(dt, entrynum, low, high);
167         hcall(LHCALL_LOAD_IDT_ENTRY, entrynum, low, high);
168 }
169
170 static void lguest_load_idt(const struct Xgt_desc_struct *desc)
171 {
172         unsigned int i;
173         struct desc_struct *idt = (void *)desc->address;
174
175         for (i = 0; i < (desc->size+1)/8; i++)
176                 hcall(LHCALL_LOAD_IDT_ENTRY, i, idt[i].a, idt[i].b);
177 }
178
179 static void lguest_load_gdt(const struct Xgt_desc_struct *desc)
180 {
181         BUG_ON((desc->size+1)/8 != GDT_ENTRIES);
182         hcall(LHCALL_LOAD_GDT, __pa(desc->address), GDT_ENTRIES, 0);
183 }
184
185 static void lguest_write_gdt_entry(struct desc_struct *dt,
186                                    int entrynum, u32 low, u32 high)
187 {
188         write_dt_entry(dt, entrynum, low, high);
189         hcall(LHCALL_LOAD_GDT, __pa(dt), GDT_ENTRIES, 0);
190 }
191
192 static void lguest_load_tls(struct thread_struct *t, unsigned int cpu)
193 {
194         lazy_hcall(LHCALL_LOAD_TLS, __pa(&t->tls_array), cpu, 0);
195 }
196
197 static void lguest_set_ldt(const void *addr, unsigned entries)
198 {
199 }
200
201 static void lguest_load_tr_desc(void)
202 {
203 }
204
205 static void lguest_cpuid(unsigned int *eax, unsigned int *ebx,
206                          unsigned int *ecx, unsigned int *edx)
207 {
208         int function = *eax;
209
210         native_cpuid(eax, ebx, ecx, edx);
211         switch (function) {
212         case 1: /* Basic feature request. */
213                 /* We only allow kernel to see SSE3, CMPXCHG16B and SSSE3 */
214                 *ecx &= 0x00002201;
215                 /* SSE, SSE2, FXSR, MMX, CMOV, CMPXCHG8B, FPU. */
216                 *edx &= 0x07808101;
217                 /* Host wants to know when we flush kernel pages: set PGE. */
218                 *edx |= 0x00002000;
219                 break;
220         case 0x80000000:
221                 /* Futureproof this a little: if they ask how much extended
222                  * processor information, limit it to known fields. */
223                 if (*eax > 0x80000008)
224                         *eax = 0x80000008;
225                 break;
226         }
227 }
228
229 static unsigned long current_cr0, current_cr3;
230 static void lguest_write_cr0(unsigned long val)
231 {
232         lazy_hcall(LHCALL_TS, val & 8, 0, 0);
233         current_cr0 = val;
234 }
235
236 static unsigned long lguest_read_cr0(void)
237 {
238         return current_cr0;
239 }
240
241 static void lguest_clts(void)
242 {
243         lazy_hcall(LHCALL_TS, 0, 0, 0);
244         current_cr0 &= ~8U;
245 }
246
247 static unsigned long lguest_read_cr2(void)
248 {
249         return lguest_data.cr2;
250 }
251
252 static void lguest_write_cr3(unsigned long cr3)
253 {
254         lazy_hcall(LHCALL_NEW_PGTABLE, cr3, 0, 0);
255         current_cr3 = cr3;
256 }
257
258 static unsigned long lguest_read_cr3(void)
259 {
260         return current_cr3;
261 }
262
263 /* Used to enable/disable PGE, but we don't care. */
264 static unsigned long lguest_read_cr4(void)
265 {
266         return 0;
267 }
268
269 static void lguest_write_cr4(unsigned long val)
270 {
271 }
272
273 static void lguest_set_pte_at(struct mm_struct *mm, unsigned long addr,
274                               pte_t *ptep, pte_t pteval)
275 {
276         *ptep = pteval;
277         lazy_hcall(LHCALL_SET_PTE, __pa(mm->pgd), addr, pteval.pte_low);
278 }
279
280 /* We only support two-level pagetables at the moment. */
281 static void lguest_set_pmd(pmd_t *pmdp, pmd_t pmdval)
282 {
283         *pmdp = pmdval;
284         lazy_hcall(LHCALL_SET_PMD, __pa(pmdp)&PAGE_MASK,
285                    (__pa(pmdp)&(PAGE_SIZE-1))/4, 0);
286 }
287
288 /* FIXME: Eliminate all callers of this. */
289 static void lguest_set_pte(pte_t *ptep, pte_t pteval)
290 {
291         *ptep = pteval;
292         /* Don't bother with hypercall before initial setup. */
293         if (current_cr3)
294                 lazy_hcall(LHCALL_FLUSH_TLB, 1, 0, 0);
295 }
296
297 static void lguest_flush_tlb_single(unsigned long addr)
298 {
299         /* Simply set it to zero, and it will fault back in. */
300         lazy_hcall(LHCALL_SET_PTE, current_cr3, addr, 0);
301 }
302
303 static void lguest_flush_tlb_user(void)
304 {
305         lazy_hcall(LHCALL_FLUSH_TLB, 0, 0, 0);
306 }
307
308 static void lguest_flush_tlb_kernel(void)
309 {
310         lazy_hcall(LHCALL_FLUSH_TLB, 1, 0, 0);
311 }
312
313 static void disable_lguest_irq(unsigned int irq)
314 {
315         set_bit(irq, lguest_data.blocked_interrupts);
316 }
317
318 static void enable_lguest_irq(unsigned int irq)
319 {
320         clear_bit(irq, lguest_data.blocked_interrupts);
321         /* FIXME: If it's pending? */
322 }
323
324 static struct irq_chip lguest_irq_controller = {
325         .name           = "lguest",
326         .mask           = disable_lguest_irq,
327         .mask_ack       = disable_lguest_irq,
328         .unmask         = enable_lguest_irq,
329 };
330
331 static void __init lguest_init_IRQ(void)
332 {
333         unsigned int i;
334
335         for (i = 0; i < LGUEST_IRQS; i++) {
336                 int vector = FIRST_EXTERNAL_VECTOR + i;
337                 if (vector != SYSCALL_VECTOR) {
338                         set_intr_gate(vector, interrupt[i]);
339                         set_irq_chip_and_handler(i, &lguest_irq_controller,
340                                                  handle_level_irq);
341                 }
342         }
343         irq_ctx_init(smp_processor_id());
344 }
345
346 static unsigned long lguest_get_wallclock(void)
347 {
348         return hcall(LHCALL_GET_WALLCLOCK, 0, 0, 0);
349 }
350
351 static cycle_t lguest_clock_read(void)
352 {
353         if (lguest_data.tsc_khz)
354                 return native_read_tsc();
355         else
356                 return jiffies;
357 }
358
359 /* This is what we tell the kernel is our clocksource.  */
360 static struct clocksource lguest_clock = {
361         .name           = "lguest",
362         .rating         = 400,
363         .read           = lguest_clock_read,
364 };
365
366 /* We also need a "struct clock_event_device": Linux asks us to set it to go
367  * off some time in the future.  Actually, James Morris figured all this out, I
368  * just applied the patch. */
369 static int lguest_clockevent_set_next_event(unsigned long delta,
370                                            struct clock_event_device *evt)
371 {
372         if (delta < LG_CLOCK_MIN_DELTA) {
373                 if (printk_ratelimit())
374                         printk(KERN_DEBUG "%s: small delta %lu ns\n",
375                                __FUNCTION__, delta);
376                 return -ETIME;
377         }
378         hcall(LHCALL_SET_CLOCKEVENT, delta, 0, 0);
379         return 0;
380 }
381
382 static void lguest_clockevent_set_mode(enum clock_event_mode mode,
383                                       struct clock_event_device *evt)
384 {
385         switch (mode) {
386         case CLOCK_EVT_MODE_UNUSED:
387         case CLOCK_EVT_MODE_SHUTDOWN:
388                 /* A 0 argument shuts the clock down. */
389                 hcall(LHCALL_SET_CLOCKEVENT, 0, 0, 0);
390                 break;
391         case CLOCK_EVT_MODE_ONESHOT:
392                 /* This is what we expect. */
393                 break;
394         case CLOCK_EVT_MODE_PERIODIC:
395                 BUG();
396         }
397 }
398
399 /* This describes our primitive timer chip. */
400 static struct clock_event_device lguest_clockevent = {
401         .name                   = "lguest",
402         .features               = CLOCK_EVT_FEAT_ONESHOT,
403         .set_next_event         = lguest_clockevent_set_next_event,
404         .set_mode               = lguest_clockevent_set_mode,
405         .rating                 = INT_MAX,
406         .mult                   = 1,
407         .shift                  = 0,
408         .min_delta_ns           = LG_CLOCK_MIN_DELTA,
409         .max_delta_ns           = LG_CLOCK_MAX_DELTA,
410 };
411
412 /* This is the Guest timer interrupt handler (hardware interrupt 0).  We just
413  * call the clockevent infrastructure and it does whatever needs doing. */
414 static void lguest_time_irq(unsigned int irq, struct irq_desc *desc)
415 {
416         unsigned long flags;
417
418         /* Don't interrupt us while this is running. */
419         local_irq_save(flags);
420         lguest_clockevent.event_handler(&lguest_clockevent);
421         local_irq_restore(flags);
422 }
423
424 static void lguest_time_init(void)
425 {
426         set_irq_handler(0, lguest_time_irq);
427
428         /* We use the TSC if the Host tells us we can, otherwise a dumb
429          * jiffies-based clock. */
430         if (lguest_data.tsc_khz) {
431                 lguest_clock.shift = 22;
432                 lguest_clock.mult = clocksource_khz2mult(lguest_data.tsc_khz,
433                                                          lguest_clock.shift);
434                 lguest_clock.mask = CLOCKSOURCE_MASK(64);
435                 lguest_clock.flags = CLOCK_SOURCE_IS_CONTINUOUS;
436         } else {
437                 /* To understand this, start at kernel/time/jiffies.c... */
438                 lguest_clock.shift = 8;
439                 lguest_clock.mult = (((u64)NSEC_PER_SEC<<8)/ACTHZ) << 8;
440                 lguest_clock.mask = CLOCKSOURCE_MASK(32);
441         }
442         clocksource_register(&lguest_clock);
443
444         /* We can't set cpumask in the initializer: damn C limitations! */
445         lguest_clockevent.cpumask = cpumask_of_cpu(0);
446         clockevents_register_device(&lguest_clockevent);
447
448         enable_lguest_irq(0);
449 }
450
451 static void lguest_load_esp0(struct tss_struct *tss,
452                                      struct thread_struct *thread)
453 {
454         lazy_hcall(LHCALL_SET_STACK, __KERNEL_DS|0x1, thread->esp0,
455                    THREAD_SIZE/PAGE_SIZE);
456 }
457
458 static void lguest_set_debugreg(int regno, unsigned long value)
459 {
460         /* FIXME: Implement */
461 }
462
463 static void lguest_wbinvd(void)
464 {
465 }
466
467 #ifdef CONFIG_X86_LOCAL_APIC
468 static void lguest_apic_write(unsigned long reg, unsigned long v)
469 {
470 }
471
472 static unsigned long lguest_apic_read(unsigned long reg)
473 {
474         return 0;
475 }
476 #endif
477
478 static void lguest_safe_halt(void)
479 {
480         hcall(LHCALL_HALT, 0, 0, 0);
481 }
482
483 static void lguest_power_off(void)
484 {
485         hcall(LHCALL_CRASH, __pa("Power down"), 0, 0);
486 }
487
488 static int lguest_panic(struct notifier_block *nb, unsigned long l, void *p)
489 {
490         hcall(LHCALL_CRASH, __pa(p), 0, 0);
491         return NOTIFY_DONE;
492 }
493
494 static struct notifier_block paniced = {
495         .notifier_call = lguest_panic
496 };
497
498 static __init char *lguest_memory_setup(void)
499 {
500         /* We do this here because lockcheck barfs if before start_kernel */
501         atomic_notifier_chain_register(&panic_notifier_list, &paniced);
502
503         add_memory_region(E820_MAP->addr, E820_MAP->size, E820_MAP->type);
504         return "LGUEST";
505 }
506
507 static const struct lguest_insns
508 {
509         const char *start, *end;
510 } lguest_insns[] = {
511         [PARAVIRT_PATCH(irq_disable)] = { lgstart_cli, lgend_cli },
512         [PARAVIRT_PATCH(irq_enable)] = { lgstart_sti, lgend_sti },
513         [PARAVIRT_PATCH(restore_fl)] = { lgstart_popf, lgend_popf },
514         [PARAVIRT_PATCH(save_fl)] = { lgstart_pushf, lgend_pushf },
515 };
516 static unsigned lguest_patch(u8 type, u16 clobber, void *insns, unsigned len)
517 {
518         unsigned int insn_len;
519
520         /* Don't touch it if we don't have a replacement */
521         if (type >= ARRAY_SIZE(lguest_insns) || !lguest_insns[type].start)
522                 return paravirt_patch_default(type, clobber, insns, len);
523
524         insn_len = lguest_insns[type].end - lguest_insns[type].start;
525
526         /* Similarly if we can't fit replacement. */
527         if (len < insn_len)
528                 return paravirt_patch_default(type, clobber, insns, len);
529
530         memcpy(insns, lguest_insns[type].start, insn_len);
531         return insn_len;
532 }
533
534 __init void lguest_init(void *boot)
535 {
536         /* Copy boot parameters first. */
537         memcpy(&boot_params, boot, PARAM_SIZE);
538         memcpy(boot_command_line, __va(boot_params.hdr.cmd_line_ptr),
539                COMMAND_LINE_SIZE);
540
541         paravirt_ops.name = "lguest";
542         paravirt_ops.paravirt_enabled = 1;
543         paravirt_ops.kernel_rpl = 1;
544
545         paravirt_ops.save_fl = save_fl;
546         paravirt_ops.restore_fl = restore_fl;
547         paravirt_ops.irq_disable = irq_disable;
548         paravirt_ops.irq_enable = irq_enable;
549         paravirt_ops.load_gdt = lguest_load_gdt;
550         paravirt_ops.memory_setup = lguest_memory_setup;
551         paravirt_ops.cpuid = lguest_cpuid;
552         paravirt_ops.write_cr3 = lguest_write_cr3;
553         paravirt_ops.flush_tlb_user = lguest_flush_tlb_user;
554         paravirt_ops.flush_tlb_single = lguest_flush_tlb_single;
555         paravirt_ops.flush_tlb_kernel = lguest_flush_tlb_kernel;
556         paravirt_ops.set_pte = lguest_set_pte;
557         paravirt_ops.set_pte_at = lguest_set_pte_at;
558         paravirt_ops.set_pmd = lguest_set_pmd;
559 #ifdef CONFIG_X86_LOCAL_APIC
560         paravirt_ops.apic_write = lguest_apic_write;
561         paravirt_ops.apic_write_atomic = lguest_apic_write;
562         paravirt_ops.apic_read = lguest_apic_read;
563 #endif
564         paravirt_ops.load_idt = lguest_load_idt;
565         paravirt_ops.iret = lguest_iret;
566         paravirt_ops.load_esp0 = lguest_load_esp0;
567         paravirt_ops.load_tr_desc = lguest_load_tr_desc;
568         paravirt_ops.set_ldt = lguest_set_ldt;
569         paravirt_ops.load_tls = lguest_load_tls;
570         paravirt_ops.set_debugreg = lguest_set_debugreg;
571         paravirt_ops.clts = lguest_clts;
572         paravirt_ops.read_cr0 = lguest_read_cr0;
573         paravirt_ops.write_cr0 = lguest_write_cr0;
574         paravirt_ops.init_IRQ = lguest_init_IRQ;
575         paravirt_ops.read_cr2 = lguest_read_cr2;
576         paravirt_ops.read_cr3 = lguest_read_cr3;
577         paravirt_ops.read_cr4 = lguest_read_cr4;
578         paravirt_ops.write_cr4 = lguest_write_cr4;
579         paravirt_ops.write_gdt_entry = lguest_write_gdt_entry;
580         paravirt_ops.write_idt_entry = lguest_write_idt_entry;
581         paravirt_ops.patch = lguest_patch;
582         paravirt_ops.safe_halt = lguest_safe_halt;
583         paravirt_ops.get_wallclock = lguest_get_wallclock;
584         paravirt_ops.time_init = lguest_time_init;
585         paravirt_ops.set_lazy_mode = lguest_lazy_mode;
586         paravirt_ops.wbinvd = lguest_wbinvd;
587
588         hcall(LHCALL_LGUEST_INIT, __pa(&lguest_data), 0, 0);
589
590         /* We use top of mem for initial pagetables. */
591         init_pg_tables_end = __pa(pg0);
592
593         asm volatile ("mov %0, %%fs" : : "r" (__KERNEL_DS) : "memory");
594
595         reserve_top_address(lguest_data.reserve_mem);
596
597         lockdep_init();
598
599         paravirt_disable_iospace();
600
601         cpu_detect(&new_cpu_data);
602         /* head.S usually sets up the first capability word, so do it here. */
603         new_cpu_data.x86_capability[0] = cpuid_edx(1);
604
605         /* Math is always hard! */
606         new_cpu_data.hard_math = 1;
607
608 #ifdef CONFIG_X86_MCE
609         mce_disabled = 1;
610 #endif
611
612 #ifdef CONFIG_ACPI
613         acpi_disabled = 1;
614         acpi_ht = 0;
615 #endif
616
617         add_preferred_console("hvc", 0, NULL);
618
619         pm_power_off = lguest_power_off;
620         start_kernel();
621 }