]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/xen/console/xencons_ring.c
Upgrade to OpenSSH 6.6p1.
[FreeBSD/FreeBSD.git] / sys / dev / xen / console / xencons_ring.c
1 #include <sys/cdefs.h>
2 __FBSDID("$FreeBSD$");
3
4 #include <sys/param.h>
5 #include <sys/module.h>
6 #include <sys/systm.h>
7 #include <sys/consio.h>
8 #include <sys/proc.h>
9 #include <sys/uio.h>
10 #include <sys/tty.h>
11 #include <sys/systm.h>
12 #include <sys/taskqueue.h>
13 #include <sys/conf.h>
14 #include <sys/kernel.h>
15 #include <sys/bus.h>
16 #include <sys/cons.h>
17
18 #include <machine/stdarg.h>
19
20 #include <xen/xen-os.h>
21 #include <xen/hypervisor.h>
22 #include <xen/xen_intr.h>
23 #include <sys/cons.h>
24
25 #include <xen/xen_intr.h>
26 #include <xen/evtchn.h>
27 #include <xen/interface/io/console.h>
28
29 #include <dev/xen/console/xencons_ring.h>
30 #include <xen/evtchn.h>
31 #include <xen/interface/io/console.h>
32
33 #define console_evtchn  console.domU.evtchn
34 xen_intr_handle_t console_handle;
35 extern struct mtx              cn_mtx;
36 extern device_t xencons_dev;
37 extern bool cnsl_evt_reg;
38
39 static inline struct xencons_interface *
40 xencons_interface(void)
41 {
42         return (struct xencons_interface *)console_page;
43 }
44
45
46 int
47 xencons_has_input(void)
48 {
49         struct xencons_interface *intf; 
50
51         intf = xencons_interface();             
52
53         return (intf->in_cons != intf->in_prod);
54 }
55
56
57 int 
58 xencons_ring_send(const char *data, unsigned len)
59 {
60         struct xencons_interface *intf; 
61         XENCONS_RING_IDX cons, prod;
62         int sent;
63         struct evtchn_send send = {
64                 .port = HYPERVISOR_start_info->console_evtchn
65         };
66
67         intf = xencons_interface();
68         cons = intf->out_cons;
69         prod = intf->out_prod;
70         sent = 0;
71
72         mb();
73         KASSERT((prod - cons) <= sizeof(intf->out),
74                 ("console send ring inconsistent"));
75         
76         while ((sent < len) && ((prod - cons) < sizeof(intf->out)))
77                 intf->out[MASK_XENCONS_IDX(prod++, intf->out)] = data[sent++];
78
79         wmb();
80         intf->out_prod = prod;
81
82         if (cnsl_evt_reg)
83                 xen_intr_signal(console_handle);
84         else
85                 HYPERVISOR_event_channel_op(EVTCHNOP_send, &send);
86
87         return sent;
88
89 }       
90
91
92 static xencons_receiver_func *xencons_receiver;
93
94 void 
95 xencons_handle_input(void *unused)
96 {
97         struct xencons_interface *intf;
98         XENCONS_RING_IDX cons, prod;
99
100         CN_LOCK(cn_mtx);
101         intf = xencons_interface();
102
103         cons = intf->in_cons;
104         prod = intf->in_prod;
105         CN_UNLOCK(cn_mtx);
106         
107         /* XXX needs locking */
108         while (cons != prod) {
109                 xencons_rx(intf->in + MASK_XENCONS_IDX(cons, intf->in), 1);
110                 cons++;
111         }
112
113         mb();
114         intf->in_cons = cons;
115
116         CN_LOCK(cn_mtx);
117         xen_intr_signal(console_handle);
118
119         xencons_tx();
120         CN_UNLOCK(cn_mtx);
121 }
122
123 void 
124 xencons_ring_register_receiver(xencons_receiver_func *f)
125 {
126         xencons_receiver = f;
127 }
128
129 int
130 xencons_ring_init(void)
131 {
132         int err;
133
134         if (HYPERVISOR_start_info->console_evtchn == 0)
135                 return 0;
136
137         err = xen_intr_bind_local_port(xencons_dev,
138             HYPERVISOR_start_info->console_evtchn, NULL, xencons_handle_input, NULL,
139             INTR_TYPE_MISC | INTR_MPSAFE, &console_handle);
140         if (err) {
141                 return err;
142         }
143
144         return 0;
145 }
146
147 extern void xencons_suspend(void);
148 extern void xencons_resume(void);
149
150 void 
151 xencons_suspend(void)
152 {
153
154         if (HYPERVISOR_start_info->console_evtchn == 0)
155                 return;
156
157         xen_intr_unbind(&console_handle);
158 }
159
160 void 
161 xencons_resume(void)
162 {
163
164         (void)xencons_ring_init();
165 }
166
167 /*
168  * Local variables:
169  * mode: C
170  * c-set-style: "BSD"
171  * c-basic-offset: 8
172  * tab-width: 4
173  * indent-tabs-mode: t
174  * End:
175  */