]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/xen/console/xencons_ring.c
MFV r276761: tcpdump 4.6.2.
[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 #define DOM0_BUFFER_SIZE        16
39
40 static inline struct xencons_interface *
41 xencons_interface(void)
42 {
43         return (struct xencons_interface *)console_page;
44 }
45
46
47 int
48 xencons_has_input(void)
49 {
50         struct xencons_interface *intf; 
51
52         if (xen_initial_domain()) {
53                 /*
54                  * Since the Dom0 console works with hypercalls
55                  * there's no way to know if there's input unless
56                  * we actually try to retrieve it, so always return
57                  * like there's pending data. Then if the hypercall
58                  * returns no input, we can handle it without problems
59                  * in xencons_handle_input().
60                  */
61                 return 1;
62         }
63
64         intf = xencons_interface();             
65
66         return (intf->in_cons != intf->in_prod);
67 }
68
69
70 int 
71 xencons_ring_send(const char *data, unsigned len)
72 {
73         struct xencons_interface *intf; 
74         XENCONS_RING_IDX cons, prod;
75         int sent;
76         struct evtchn_send send = {
77                 .port = HYPERVISOR_start_info->console_evtchn
78         };
79
80         intf = xencons_interface();
81         cons = intf->out_cons;
82         prod = intf->out_prod;
83         sent = 0;
84
85         mb();
86         KASSERT((prod - cons) <= sizeof(intf->out),
87                 ("console send ring inconsistent"));
88         
89         while ((sent < len) && ((prod - cons) < sizeof(intf->out)))
90                 intf->out[MASK_XENCONS_IDX(prod++, intf->out)] = data[sent++];
91
92         wmb();
93         intf->out_prod = prod;
94
95         if (cnsl_evt_reg)
96                 xen_intr_signal(console_handle);
97         else
98                 HYPERVISOR_event_channel_op(EVTCHNOP_send, &send);
99
100         return sent;
101
102 }       
103
104
105 static xencons_receiver_func *xencons_receiver;
106
107 void 
108 xencons_handle_input(void *unused)
109 {
110         struct xencons_interface *intf;
111         XENCONS_RING_IDX cons, prod;
112
113         CN_LOCK(cn_mtx);
114
115         if (xen_initial_domain()) {
116                 static char rbuf[DOM0_BUFFER_SIZE];
117                 int         l;
118
119                 while ((l = HYPERVISOR_console_io(CONSOLEIO_read,
120                     DOM0_BUFFER_SIZE, rbuf)) > 0)
121                         xencons_rx(rbuf, l);
122
123                 CN_UNLOCK(cn_mtx);
124                 return;
125         }
126
127         intf = xencons_interface();
128
129         cons = intf->in_cons;
130         prod = intf->in_prod;
131         CN_UNLOCK(cn_mtx);
132         
133         /* XXX needs locking */
134         while (cons != prod) {
135                 xencons_rx(intf->in + MASK_XENCONS_IDX(cons, intf->in), 1);
136                 cons++;
137         }
138
139         mb();
140         intf->in_cons = cons;
141
142         CN_LOCK(cn_mtx);
143         xen_intr_signal(console_handle);
144
145         xencons_tx();
146         CN_UNLOCK(cn_mtx);
147 }
148
149 void 
150 xencons_ring_register_receiver(xencons_receiver_func *f)
151 {
152         xencons_receiver = f;
153 }
154
155 int
156 xencons_ring_init(void)
157 {
158         int err;
159
160         if (HYPERVISOR_start_info->console_evtchn == 0)
161                 return 0;
162
163         err = xen_intr_bind_local_port(xencons_dev,
164             HYPERVISOR_start_info->console_evtchn, NULL, xencons_handle_input, NULL,
165             INTR_TYPE_MISC | INTR_MPSAFE, &console_handle);
166         if (err) {
167                 return err;
168         }
169
170         return 0;
171 }
172
173 extern void xencons_suspend(void);
174 extern void xencons_resume(void);
175
176 void 
177 xencons_suspend(void)
178 {
179
180         if (HYPERVISOR_start_info->console_evtchn == 0)
181                 return;
182
183         xen_intr_unbind(&console_handle);
184 }
185
186 void 
187 xencons_resume(void)
188 {
189
190         (void)xencons_ring_init();
191 }
192
193 /*
194  * Local variables:
195  * mode: C
196  * c-set-style: "BSD"
197  * c-basic-offset: 8
198  * tab-width: 4
199  * indent-tabs-mode: t
200  * End:
201  */