merge
[linux-based-stubdoms.git] / linux-src.patch
blob46d8b347f9e206774767c68f65c1a3c583e76164
1 diff --git a/arch/x86/xen/enlighten.c b/arch/x86/xen/enlighten.c
2 index 0493093..97f848d 100644
3 --- a/arch/x86/xen/enlighten.c
4 +++ b/arch/x86/xen/enlighten.c
5 @@ -779,7 +779,7 @@ static __init void set_xen_basic_apic_ops(void)
6 apic->icr_write = xen_apic_icr_write;
7 apic->wait_icr_idle = xen_apic_wait_icr_idle;
8 apic->safe_wait_icr_idle = xen_safe_apic_wait_icr_idle;
9 - apic->probe = xen_safe_probe;
10 +// apic->probe = xen_safe_probe;
11 apic->acpi_madt_oem_check = xen_safe_flat_acpi_madt_oem_check;
12 /* Copy over the full contents of the newly modified apic into
13 * our apic_xen, which is to be called first by apic_probe[]. */
14 diff --git a/arch/x86/xen/mmu.c b/arch/x86/xen/mmu.c
15 index 204e3ba..3132414 100644
16 --- a/arch/x86/xen/mmu.c
17 +++ b/arch/x86/xen/mmu.c
18 @@ -1666,6 +1666,7 @@ static void xen_flush_tlb_single(unsigned long addr)
19 static void xen_flush_tlb_others(const struct cpumask *cpus,
20 struct mm_struct *mm, unsigned long va)
22 + extern unsigned int num_processors;
23 struct {
24 struct mmuext_op op;
25 DECLARE_BITMAP(mask, num_processors);
26 diff --git a/drivers/char/hvc_xen.c b/drivers/char/hvc_xen.c
27 index 5620de5..3c4c6bc 100644
28 --- a/drivers/char/hvc_xen.c
29 +++ b/drivers/char/hvc_xen.c
30 @@ -23,44 +23,78 @@
31 #include <linux/err.h>
32 #include <linux/init.h>
33 #include <linux/types.h>
34 +#include <linux/list.h>
35 +#include <linux/irq.h>
37 +#include <asm/io.h>
38 #include <asm/xen/hypervisor.h>
40 #include <xen/xen.h>
41 +#include <xen/interface/xen.h>
42 +#include <xen/hvm.h>
43 +#include <xen/grant_table.h>
44 #include <xen/page.h>
45 #include <xen/events.h>
46 #include <xen/interface/io/console.h>
47 #include <xen/hvc-console.h>
48 +#include <xen/xenbus.h>
50 #include "hvc_console.h"
52 #define HVC_COOKIE 0x58656e /* "Xen" in hex */
54 -static struct hvc_struct *hvc;
55 -static int xencons_irq;
56 +struct xencons_info {
57 + struct list_head list;
58 + struct xenbus_device *xbdev;
59 + struct xencons_interface *intf;
60 + unsigned int evtchn;
61 + struct hvc_struct *hvc;
62 + int irq;
63 + int vtermno;
64 + grant_ref_t gntref;
65 +};
67 +static LIST_HEAD(xenconsoles);
68 +static DEFINE_SPINLOCK(xencons_lock);
69 +static struct xenbus_driver xencons_driver;
71 /* ------------------------------------------------------------------ */
73 -static unsigned long console_pfn = ~0ul;
74 +static struct xencons_info *vtermno_to_xencons(int vtermno)
76 + struct xencons_info *entry, *ret = NULL;
78 + if (list_empty(&xenconsoles))
79 + return NULL;
81 + spin_lock(&xencons_lock);
82 + list_for_each_entry(entry, &xenconsoles, list) {
83 + if (entry->vtermno == vtermno) {
84 + ret = entry;
85 + break;
86 + }
87 + }
88 + spin_unlock(&xencons_lock);
90 + return ret;
93 -static inline struct xencons_interface *xencons_interface(void)
94 +static inline int xenbus_devid_to_vtermno(int devid)
96 - if (console_pfn == ~0ul)
97 - return mfn_to_virt(xen_start_info->console.domU.mfn);
98 - else
99 - return __va(console_pfn << PAGE_SHIFT);
100 + return devid + HVC_COOKIE;
103 -static inline void notify_daemon(void)
104 +static inline void notify_daemon(struct xencons_info *cons)
106 /* Use evtchn: this is called early, before irq is set up. */
107 - notify_remote_via_evtchn(xen_start_info->console.domU.evtchn);
108 + notify_remote_via_evtchn(cons->evtchn);
111 -static int __write_console(const char *data, int len)
112 +static int __write_console(struct xencons_info *xencons,
113 +const char *data, int len)
115 - struct xencons_interface *intf = xencons_interface();
116 XENCONS_RING_IDX cons, prod;
117 + struct xencons_interface *intf = xencons->intf;
118 int sent = 0;
120 cons = intf->out_cons;
121 @@ -75,7 +109,7 @@ static int __write_console(const char *data, int len)
122 intf->out_prod = prod;
124 if (sent)
125 - notify_daemon();
126 + notify_daemon(xencons);
127 return sent;
130 @@ -83,6 +117,9 @@ static int domU_write_console(uint32_t vtermno, const char *data, int len)
132 int ret = len;
134 + struct xencons_info *cons = vtermno_to_xencons(vtermno);
135 + if (cons == NULL)
136 + return -EINVAL;
138 * Make sure the whole buffer is emitted, polling if
139 * necessary. We don't ever want to rely on the hvc daemon
140 @@ -90,8 +127,8 @@ static int domU_write_console(uint32_t vtermno, const char *data, int len)
141 * kernel is crippled.
143 while (len) {
144 - int sent = __write_console(data, len);
146 + int sent = __write_console(cons, data, len);
148 data += sent;
149 len -= sent;
151 @@ -104,9 +141,13 @@ static int domU_write_console(uint32_t vtermno, const char *data, int len)
153 static int domU_read_console(uint32_t vtermno, char *buf, int len)
155 - struct xencons_interface *intf = xencons_interface();
156 + struct xencons_interface *intf;
157 XENCONS_RING_IDX cons, prod;
158 int recv = 0;
159 + struct xencons_info *xencons = vtermno_to_xencons(vtermno);
160 + if (xencons == NULL)
161 + return -EINVAL;
162 + intf = xencons->intf;
164 cons = intf->in_cons;
165 prod = intf->in_prod;
166 @@ -119,7 +160,7 @@ static int domU_read_console(uint32_t vtermno, char *buf, int len)
167 mb(); /* read ring before consuming */
168 intf->in_cons = cons;
170 - notify_daemon();
171 + notify_daemon(xencons);
172 return recv;
175 @@ -157,69 +198,408 @@ static struct hv_ops dom0_hvc_ops = {
176 .notifier_hangup = notifier_hangup_irq,
179 +static int xen_hvm_console_init(void)
181 + int r;
182 + uint64_t v = 0;
183 + unsigned long mfn;
184 + struct xencons_info *info;
186 + if (!xen_hvm_domain())
187 + return -ENODEV;
189 + info = vtermno_to_xencons(HVC_COOKIE);
190 + if (!info) {
191 + info = kmalloc(sizeof(struct xencons_info), GFP_KERNEL | __GFP_ZERO);
192 + if (!info)
193 + return -ENOMEM;
196 + /* already configured */
197 + if (info->intf != NULL)
198 + return 0;
200 + r = hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN, &v);
201 + if (r < 0){
202 + kfree(info);
203 + return -ENODEV;
205 + info->evtchn = v;
206 + hvm_get_parameter(HVM_PARAM_CONSOLE_PFN, &v);
207 + if (r < 0) {
208 + kfree(info);
209 + return -ENODEV;
211 + mfn = v;
212 + info->intf = ioremap(mfn << PAGE_SHIFT, PAGE_SIZE);
213 + if (info->intf == NULL) {
214 + kfree(info);
215 + return -ENODEV;
217 + info->vtermno = HVC_COOKIE;
219 + spin_lock(&xencons_lock);
220 + list_add_tail(&info->list, &xenconsoles);
221 + spin_unlock(&xencons_lock);
223 + return 0;
226 +static int xen_pv_console_init(void)
228 + struct xencons_info *info;
230 + if (!xen_pv_domain())
231 + return -ENODEV;
233 + if (!xen_start_info->console.domU.evtchn)
234 + return -ENODEV;
236 + info = vtermno_to_xencons(HVC_COOKIE);
237 + if (!info) {
238 + info = kmalloc(sizeof(struct xencons_info), GFP_KERNEL | __GFP_ZERO);
239 + if (!info)
240 + return -ENOMEM;
243 + /* already configured */
244 + if (info->intf != NULL)
245 + return 0;
247 + info->evtchn = xen_start_info->console.domU.evtchn;
248 + info->intf = mfn_to_virt(xen_start_info->console.domU.mfn);
249 + info->vtermno = HVC_COOKIE;
251 + spin_lock(&xencons_lock);
252 + list_add_tail(&info->list, &xenconsoles);
253 + spin_unlock(&xencons_lock);
255 + return 0;
258 +static int xen_initial_domain_console_init(void)
260 + struct xencons_info *info;
262 + if (!xen_initial_domain())
263 + return -ENODEV;
265 + info = vtermno_to_xencons(HVC_COOKIE);
266 + if (!info) {
267 + info = kmalloc(sizeof(struct xencons_info), GFP_KERNEL | __GFP_ZERO);
268 + if (!info)
269 + return -ENOMEM;
272 + info->irq = bind_virq_to_irq(VIRQ_CONSOLE, 0);
273 + info->vtermno = HVC_COOKIE;
275 + spin_lock(&xencons_lock);
276 + list_add_tail(&info->list, &xenconsoles);
277 + spin_unlock(&xencons_lock);
279 + return 0;
282 static int __init xen_hvc_init(void)
284 - struct hvc_struct *hp;
285 - struct hv_ops *ops;
286 + int r;
287 + struct xencons_info *info;
288 + const struct hv_ops *ops;
290 - if (!xen_pv_domain())
291 + printk("temp: init xen hvc console\n");
292 + if (!xen_domain())
293 + return -ENODEV;
295 + if (xen_pv_domain() && !xen_initial_domain() &&
296 + !xen_start_info->console.domU.evtchn)
297 return -ENODEV;
299 if (xen_initial_domain()) {
300 ops = &dom0_hvc_ops;
301 - xencons_irq = bind_virq_to_irq(VIRQ_CONSOLE, 0);
302 + r = xen_initial_domain_console_init();
303 + if (r < 0)
304 + return r;
305 + info = vtermno_to_xencons(HVC_COOKIE);
306 } else {
307 - if (!xen_start_info->console.domU.evtchn)
308 - return -ENODEV;
310 ops = &domU_hvc_ops;
311 - xencons_irq = bind_evtchn_to_irq(xen_start_info->console.domU.evtchn);
312 + if (xen_hvm_domain())
313 + r = xen_hvm_console_init();
314 + else
315 + r = xen_pv_console_init();
316 + if (r < 0)
317 + return r;
319 + info = vtermno_to_xencons(HVC_COOKIE);
320 + info->irq = bind_evtchn_to_irq(info->evtchn);
323 - if (xencons_irq < 0)
324 - xencons_irq = 0; /* NO_IRQ */
325 + if (info->irq < 0)
326 + info->irq = 0; /* NO_IRQ */
327 else
328 - set_irq_noprobe(xencons_irq);
329 + set_irq_noprobe(info->irq);
331 + info->hvc = hvc_alloc(HVC_COOKIE, info->irq, ops, 256);
332 + if (IS_ERR(info->hvc)) {
333 + r = PTR_ERR(info->hvc);
334 + spin_lock(&xencons_lock);
335 + list_del(&info->list);
336 + spin_unlock(&xencons_lock);
337 + if (info->irq)
338 + unbind_from_irqhandler(info->irq, NULL);
339 + kfree(info);
340 + return r;
343 + return xenbus_register_frontend(&xencons_driver);
346 - hp = hvc_alloc(HVC_COOKIE, xencons_irq, ops, 256);
347 - if (IS_ERR(hp))
348 - return PTR_ERR(hp);
349 +void xen_console_resume(void)
351 + struct xencons_info *info = vtermno_to_xencons(HVC_COOKIE);
352 + if (info != NULL && info->irq)
353 + rebind_evtchn_irq(info->evtchn, info->irq);
356 +static void xencons_disconnect_backend(struct xencons_info *info)
358 + if (info->irq > 0)
359 + unbind_from_irqhandler(info->irq, NULL);
360 + info->irq = 0;
361 + if (info->evtchn > 0)
362 + xenbus_free_evtchn(info->xbdev, info->evtchn);
363 + info->evtchn = 0;
364 + if (info->gntref > 0)
365 + gnttab_free_grant_references(info->gntref);
366 + info->gntref = 0;
367 + if (info->hvc != NULL)
368 + hvc_remove(info->hvc);
369 + info->hvc = NULL;
372 - hvc = hp;
373 +static void xencons_free(struct xencons_info *info)
375 + xencons_disconnect_backend(info);
376 + free_page((unsigned long)info->intf);
377 + info->intf = NULL;
378 + info->vtermno = 0;
379 + kfree(info);
382 - console_pfn = mfn_to_pfn(xen_start_info->console.domU.mfn);
383 +static int xencons_remove(struct xenbus_device *dev)
385 + struct xencons_info *info = dev_get_drvdata(&dev->dev);
387 + spin_lock(&xencons_lock);
388 + list_del(&info->list);
389 + spin_unlock(&xencons_lock);
390 + xencons_free(info);
391 return 0;
394 -void xen_console_resume(void)
395 +static int xencons_connect_backend(struct xenbus_device *dev,
396 + struct xencons_info *info)
398 - if (xencons_irq)
399 - rebind_evtchn_irq(xen_start_info->console.domU.evtchn, xencons_irq);
400 + int ret, evtchn, devid, ref, irq;
401 + struct xenbus_transaction xbt;
402 + grant_ref_t gref_head;
403 + unsigned long mfn;
405 + ret = xenbus_alloc_evtchn(dev, &evtchn);
406 + if (ret)
407 + return ret;
408 + info->evtchn = evtchn;
409 + irq = bind_evtchn_to_irq(evtchn);
410 + if (irq < 0)
411 + return irq;
412 + info->irq = irq;
413 + devid = dev->nodename[strlen(dev->nodename) - 1] - '0';
414 + info->hvc = hvc_alloc(xenbus_devid_to_vtermno(devid),
415 + irq, &domU_hvc_ops, 256);
416 + if (IS_ERR(info->hvc))
417 + return PTR_ERR(info->hvc);
418 + if (xen_pv_domain())
419 + mfn = virt_to_mfn(info->intf);
420 + else
421 + mfn = __pa(info->intf) >> PAGE_SHIFT;
422 + ret = gnttab_alloc_grant_references(1, &gref_head);
423 + if (ret < 0)
424 + return ret;
425 + info->gntref = gref_head;
426 + ref = gnttab_claim_grant_reference(&gref_head);
427 + if (ref < 0)
428 + return ref;
429 + gnttab_grant_foreign_access_ref(ref, info->xbdev->otherend_id,mfn, 0);
431 +again:
432 + ret = xenbus_transaction_start(&xbt);
433 + if (ret) {
434 + xenbus_dev_fatal(dev, ret, "starting transaction");
435 + return ret;
437 + ret = xenbus_printf(xbt, dev->nodename, "ring-ref", "%d", ref);
438 + if (ret)
439 + goto error_xenbus;
440 + ret = xenbus_printf(xbt, dev->nodename, "port", "%u",evtchn);
441 + if (ret)
442 + goto error_xenbus;
443 + ret = xenbus_printf(xbt, dev->nodename, "type", "ioemu");
444 + if (ret)
445 + goto error_xenbus;
446 + ret = xenbus_transaction_end(xbt, 0);
447 + if (ret) {
448 + if (ret == -EAGAIN)
449 + goto again;
450 + xenbus_dev_fatal(dev, ret, "completing transaction");
451 + return ret;
454 + xenbus_switch_state(dev, XenbusStateInitialised);
455 + return 0;
457 +error_xenbus:
458 + xenbus_transaction_end(xbt, 1);
459 + xenbus_dev_fatal(dev, ret, "writing xenstore");
460 + return ret;
463 +static int __devinit xencons_probe(struct xenbus_device *dev,
464 + const struct xenbus_device_id *id)
466 + int ret, devid;
467 + struct xencons_info *info;
469 + devid = dev->nodename[strlen(dev->nodename) - 1] - '0';
470 + if (devid == 0){
471 + return -ENODEV;
474 + info = kmalloc(sizeof(struct xencons_info), GFP_KERNEL | __GFP_ZERO);
475 + if (!info)
476 + goto error_nomem;
477 + dev_set_drvdata(&dev->dev, info);
478 + info->xbdev = dev;
479 + info->vtermno = xenbus_devid_to_vtermno(devid);
480 + info->intf = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
481 + if (!info->intf)
482 + goto error_nomem;
484 + ret = xencons_connect_backend(dev, info);
485 + if (ret < 0)
486 + goto error;
487 + spin_lock(&xencons_lock);
488 + list_add_tail(&info->list, &xenconsoles);
489 + spin_unlock(&xencons_lock);
491 + return 0;
493 +error_nomem:
494 + ret = -ENOMEM;
495 + xenbus_dev_fatal(dev, ret, "allocating device memory");
496 +error:
497 + xencons_free(info);
498 + return ret;
501 +static int xencons_resume(struct xenbus_device *dev)
503 + struct xencons_info *info = dev_get_drvdata(&dev->dev);
505 + xencons_disconnect_backend(info);
506 + memset(info->intf, 0, PAGE_SIZE);
507 + return xencons_connect_backend(dev, info);
510 +static void xencons_backend_changed(struct xenbus_device *dev,
511 + enum xenbus_state backend_state)
513 + switch (backend_state) {
514 + case XenbusStateReconfiguring:
515 + case XenbusStateReconfigured:
516 + case XenbusStateInitialising:
517 + case XenbusStateInitialised:
518 + case XenbusStateUnknown:
519 + case XenbusStateClosed:
520 + break;
522 + case XenbusStateInitWait:
523 + break;
525 + case XenbusStateConnected:
526 + xenbus_switch_state(dev, XenbusStateConnected);
527 + break;
529 + case XenbusStateClosing:
530 + xenbus_frontend_closed(dev);
531 + break;
535 +static const struct xenbus_device_id xencons_ids[] = {
536 + { "console" },
537 + { "" }
540 static void __exit xen_hvc_fini(void)
542 - if (hvc)
543 - hvc_remove(hvc);
544 + struct xencons_info *entry, *next;
546 + if (list_empty(&xenconsoles))
547 + return;
549 + spin_lock(&xencons_lock);
550 + list_for_each_entry_safe(entry, next, &xenconsoles, list) {
551 + list_del(&entry->list);
552 + if (entry->xbdev)
553 + xencons_remove(entry->xbdev);
554 + else {
555 + if (entry->irq > 0)
556 + unbind_from_irqhandler(entry->irq, NULL);
557 + if (entry->hvc);
558 + hvc_remove(entry->hvc);
559 + kfree(entry);
562 + spin_unlock(&xencons_lock);
565 static int xen_cons_init(void)
567 - struct hv_ops *ops;
568 + const struct hv_ops *ops;
570 - if (!xen_pv_domain())
571 + if (!xen_domain())
572 return 0;
574 ops = &domU_hvc_ops;
575 if (xen_initial_domain())
576 ops = &dom0_hvc_ops;
577 + else {
578 + int r;
579 + ops = &domU_hvc_ops;
581 + if (xen_hvm_domain())
582 + r = xen_hvm_console_init();
583 + else
584 + r = xen_pv_console_init();
585 + if (r < 0)
586 + return r;
589 hvc_instantiate(HVC_COOKIE, 0, ops);
591 return 0;
594 +static struct xenbus_driver xencons_driver = {
595 + .name = "xenconsole",
596 + .owner = THIS_MODULE,
597 + .ids = xencons_ids,
598 + .probe = xencons_probe,
599 + .remove = xencons_remove,
600 + .resume = xencons_resume,
601 + .otherend_changed = xencons_backend_changed,
604 module_init(xen_hvc_init);
605 module_exit(xen_hvc_fini);
606 console_initcall(xen_cons_init);
607 diff --git a/drivers/xen/xenbus/xenbus_probe.c b/drivers/xen/xenbus/xenbus_probe.c
608 index be4c7c3..66daeb3 100644
609 --- a/drivers/xen/xenbus/xenbus_probe.c
610 +++ b/drivers/xen/xenbus/xenbus_probe.c
611 @@ -207,7 +207,7 @@ int xenbus_dev_probe(struct device *_dev)
612 const struct xenbus_device_id *id;
613 int err;
615 - DPRINTK("%s", dev->nodename);
616 + printk("%s\n", dev->nodename);
618 if (!drv->probe) {
619 err = -ENODEV;
620 diff --git a/drivers/xen/xenbus/xenbus_probe_frontend.c b/drivers/xen/xenbus/xenbus_probe_frontend.c
621 index f91b310..06af23d 100644
622 --- a/drivers/xen/xenbus/xenbus_probe_frontend.c
623 +++ b/drivers/xen/xenbus/xenbus_probe_frontend.c
624 @@ -268,7 +268,7 @@ static int __init xenbus_probe_frontend_init(void)
626 int err;
628 - DPRINTK("");
629 + printk("xenbus_probe_frontend_init\n");
631 /* Register ourselves with the kernel bus subsystem */
632 err = bus_register(&xenbus_frontend.bus);
633 diff --git a/drivers/xen/xenfs/privcmd.c b/drivers/xen/xenfs/privcmd.c
634 index f80be7f..7574487 100644
635 --- a/drivers/xen/xenfs/privcmd.c
636 +++ b/drivers/xen/xenfs/privcmd.c
637 @@ -191,8 +191,10 @@ static long privcmd_ioctl_mmap(void __user *udata)
638 LIST_HEAD(pagelist);
639 struct mmap_mfn_state state;
641 +#if 0
642 if (!xen_initial_domain())
643 return -EPERM;
644 +#endif
646 if (copy_from_user(&mmapcmd, udata, sizeof(mmapcmd)))
647 return -EFAULT;
648 @@ -283,8 +285,10 @@ static long privcmd_ioctl_mmap_batch(void __user *udata)
649 LIST_HEAD(pagelist);
650 struct mmap_batch_state state;
652 +#if 0
653 if (!xen_initial_domain())
654 return -EPERM;
655 +#endif
657 if (copy_from_user(&m, udata, sizeof(m)))
658 return -EFAULT;
659 diff --git a/include/xen/interface/hvm/params.h b/include/xen/interface/hvm/params.h
660 index 1888d8c..1b4f923 100644
661 --- a/include/xen/interface/hvm/params.h
662 +++ b/include/xen/interface/hvm/params.h
663 @@ -90,6 +90,10 @@
664 /* Boolean: Enable aligning all periodic vpts to reduce interrupts */
665 #define HVM_PARAM_VPT_ALIGN 16
667 -#define HVM_NR_PARAMS 17
668 +/* Console debug shared memory ring and event channel */
669 +#define HVM_PARAM_CONSOLE_PFN 17
670 +#define HVM_PARAM_CONSOLE_EVTCHN 18
672 +#define HVM_NR_PARAMS 19
674 #endif /* __XEN_PUBLIC_HVM_PARAMS_H__ */
675 diff --git a/kernel/printk.c b/kernel/printk.c
676 index 4cade47..fa4d843 100644
677 --- a/kernel/printk.c
678 +++ b/kernel/printk.c
679 @@ -36,6 +36,7 @@
681 #include <asm/uaccess.h>
683 +#include <xen/hvc-console.h>
685 * for_each_console() allows you to iterate on each console
687 @@ -589,12 +590,15 @@ static int have_callable_console(void)
688 asmlinkage int printk(const char *fmt, ...)
690 va_list args;
691 + static char buf[512];
692 int r;
694 va_start(args, fmt);
695 r = vprintk(fmt, args);
696 + vsnprintf(buf,sizeof(buf),fmt,args);
697 va_end(args);
699 + xen_raw_console_write(buf);
700 return r;