debuggers.hg

view netbsd-2.0-xen-sparse/sys/arch/xen/xen/xencons.c @ 2632:a4fbb98f00cb

bitkeeper revision 1.1159.1.202 (41616cc2-ciBh_VkJKwmQaCL6BEU6Q)

Merge labyrinth.cl.cam.ac.uk:/auto/groups/xeno/BK/xeno.bk
into labyrinth.cl.cam.ac.uk:/auto/anfs/scratch/labyrinth/iap10/xeno-clone/xeno.bk
author iap10@labyrinth.cl.cam.ac.uk
date Mon Oct 04 15:31:14 2004 +0000 (2004-10-04)
parents 309c1fb27f87 654b2df93458
children 0a4b76b6b5a0
line source
1 /* $NetBSD: xencons.c,v 1.1.2.1 2004/05/22 15:59:21 he Exp $ */
3 /*
4 *
5 * Copyright (c) 2004 Christian Limpach.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Christian Limpach.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: xencons.c,v 1.1.2.1 2004/05/22 15:59:21 he Exp $");
38 #include <sys/param.h>
39 #include <sys/ioctl.h>
40 #include <sys/proc.h>
41 #include <sys/tty.h>
42 #include <sys/systm.h>
43 #include <sys/device.h>
44 #include <sys/conf.h>
46 #include <machine/stdarg.h>
47 #include <machine/xen.h>
48 #include <machine/hypervisor.h>
49 #include <machine/evtchn.h>
50 #include <machine/ctrl_if.h>
52 #include <dev/cons.h>
54 #include <ddb/db_output.h> /* XXX for db_max_line */
56 static int xencons_isconsole = 0;
58 #define XENCONS_UNIT(x) (minor(x))
59 #define XENCONS_BURST 128
61 int xencons_match (struct device *, struct cfdata *, void *);
62 void xencons_attach (struct device *, struct device *, void *);
63 /* int xencons_intr (void *); */
64 void xencons_init (void);
66 struct xencons_softc {
67 struct device sc_dev;
68 struct tty *sc_tty;
69 };
71 CFATTACH_DECL(xencons, sizeof(struct xencons_softc),
72 xencons_match, xencons_attach, NULL, NULL);
74 extern struct cfdriver xencons_cd;
76 dev_type_open(xencons_open);
77 dev_type_close(xencons_close);
78 dev_type_read(xencons_read);
79 dev_type_write(xencons_write);
80 dev_type_ioctl(xencons_ioctl);
81 dev_type_stop(xencons_stop);
82 dev_type_tty(xencons_tty);
83 dev_type_poll(xencons_poll);
85 const struct cdevsw xencons_cdevsw = {
86 xencons_open, xencons_close, xencons_read, xencons_write,
87 xencons_ioctl, xencons_stop, xencons_tty, xencons_poll,
88 NULL, ttykqfilter, D_TTY
89 };
92 static void xencons_rx(ctrl_msg_t *, unsigned long);
93 void xenconscn_attach(void);
94 int xenconscn_getc(dev_t);
95 void xenconscn_putc(dev_t, int);
96 void xenconscn_pollc(dev_t, int);
98 static struct consdev xencons = {
99 NULL, NULL, xenconscn_getc, xenconscn_putc, xenconscn_pollc,
100 NULL, NULL, NULL, NODEV, CN_NORMAL
101 };
103 void xencons_start (struct tty *);
104 int xencons_param (struct tty *, struct termios *);
106 int
107 xencons_match(struct device *parent, struct cfdata *match, void *aux)
108 {
109 struct xencons_attach_args *xa = (struct xencons_attach_args *)aux;
111 if (strcmp(xa->xa_device, "xencons") == 0)
112 return 1;
113 return 0;
114 }
116 void
117 xencons_attach(struct device *parent, struct device *self, void *aux)
118 {
119 struct xencons_softc *sc = (void *)self;
121 aprint_normal(": Xen Virtual Console Driver\n");
123 if (xencons_isconsole) {
124 int maj;
126 /* Locate the major number. */
127 maj = cdevsw_lookup_major(&xencons_cdevsw);
129 /* There can be only one, but it can have any unit number. */
130 cn_tab->cn_dev = makedev(maj, sc->sc_dev.dv_unit);
132 aprint_verbose("%s: console major %d, unit %d\n",
133 sc->sc_dev.dv_xname, maj, sc->sc_dev.dv_unit);
135 /* Set db_max_line to avoid paging. */
136 db_max_line = 0x7fffffff;
138 (void)ctrl_if_register_receiver(CMSG_CONSOLE, xencons_rx, 0);
139 }
140 }
142 int
143 xencons_open(dev_t dev, int flag, int mode, struct proc *p)
144 {
145 struct xencons_softc *sc;
146 int unit = XENCONS_UNIT(dev);
147 struct tty *tp;
149 sc = device_lookup(&xencons_cd, unit);
150 if (sc == NULL)
151 return (ENXIO);
153 if (!sc->sc_tty) {
154 tp = sc->sc_tty = ttymalloc();
155 tty_attach(tp);
156 } else
157 tp = sc->sc_tty;
159 tp->t_oproc = xencons_start;
160 tp->t_param = xencons_param;
161 tp->t_dev = dev;
162 if ((tp->t_state & TS_ISOPEN) == 0) {
163 ttychars(tp);
164 tp->t_iflag = TTYDEF_IFLAG;
165 tp->t_oflag = TTYDEF_OFLAG;
166 tp->t_cflag = TTYDEF_CFLAG;
167 tp->t_lflag = TTYDEF_LFLAG;
168 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
169 xencons_param(tp, &tp->t_termios);
170 ttsetwater(tp);
171 } else if (tp->t_state&TS_XCLUDE && p->p_ucred->cr_uid != 0)
172 return (EBUSY);
173 tp->t_state |= TS_CARR_ON;
175 return ((*tp->t_linesw->l_open)(dev, tp));
176 }
178 int
179 xencons_close(dev_t dev, int flag, int mode, struct proc *p)
180 {
181 struct xencons_softc *sc = device_lookup(&xencons_cd,
182 XENCONS_UNIT(dev));
183 struct tty *tp = sc->sc_tty;
185 if (tp == NULL)
186 return (0);
187 (*tp->t_linesw->l_close)(tp, flag);
188 ttyclose(tp);
189 #ifdef notyet /* XXX */
190 ttyfree(tp);
191 #endif
192 return (0);
193 }
195 int
196 xencons_read(dev_t dev, struct uio *uio, int flag)
197 {
198 struct xencons_softc *sc = device_lookup(&xencons_cd,
199 XENCONS_UNIT(dev));
200 struct tty *tp = sc->sc_tty;
202 return ((*tp->t_linesw->l_read)(tp, uio, flag));
203 }
205 int
206 xencons_write(dev_t dev, struct uio *uio, int flag)
207 {
208 struct xencons_softc *sc = device_lookup(&xencons_cd,
209 XENCONS_UNIT(dev));
210 struct tty *tp = sc->sc_tty;
212 return ((*tp->t_linesw->l_write)(tp, uio, flag));
213 }
215 int
216 xencons_poll(dev_t dev, int events, struct proc *p)
217 {
218 struct xencons_softc *sc = device_lookup(&xencons_cd,
219 XENCONS_UNIT(dev));
220 struct tty *tp = sc->sc_tty;
222 return ((*tp->t_linesw->l_poll)(tp, events, p));
223 }
225 struct tty *
226 xencons_tty(dev_t dev)
227 {
228 struct xencons_softc *sc = device_lookup(&xencons_cd,
229 XENCONS_UNIT(dev));
230 struct tty *tp = sc->sc_tty;
232 return (tp);
233 }
235 int
236 xencons_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
237 {
238 struct xencons_softc *sc = device_lookup(&xencons_cd,
239 XENCONS_UNIT(dev));
240 struct tty *tp = sc->sc_tty;
241 int error;
243 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
244 if (error != EPASSTHROUGH)
245 return (error);
247 error = ttioctl(tp, cmd, data, flag, p);
248 if (error != EPASSTHROUGH)
249 return (error);
251 switch (cmd) {
252 default:
253 return (EPASSTHROUGH);
254 }
256 #ifdef DIAGNOSTIC
257 panic("xencons_ioctl: impossible");
258 #endif
259 }
261 void
262 xencons_start(struct tty *tp)
263 {
264 struct clist *cl;
265 int s, len;
267 s = spltty();
268 if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))
269 goto out;
270 tp->t_state |= TS_BUSY;
271 splx(s);
273 /*
274 * We need to do this outside spl since it could be fairly
275 * expensive and we don't want our serial ports to overflow.
276 */
277 cl = &tp->t_outq;
278 if (xen_start_info.flags & SIF_INITDOMAIN) {
279 u_char buf[XENCONS_BURST+1];
281 len = q_to_b(cl, buf, XENCONS_BURST);
282 (void)HYPERVISOR_console_io(CONSOLEIO_write, len, buf);
283 } else {
284 ctrl_msg_t msg;
286 len = q_to_b(cl, msg.msg, sizeof(msg.msg));
287 msg.type = CMSG_CONSOLE;
288 msg.subtype = CMSG_CONSOLE_DATA;
289 msg.length = len;
290 ctrl_if_send_message_noblock(&msg, NULL, 0);
291 /* XXX check return value and queue wait for space
292 * thread/softint */
293 }
295 s = spltty();
296 tp->t_state &= ~TS_BUSY;
297 if (cl->c_cc) {
298 tp->t_state |= TS_TIMEOUT;
299 callout_reset(&tp->t_rstrt_ch, 1, ttrstrt, tp);
300 }
301 if (cl->c_cc <= tp->t_lowat) {
302 if (tp->t_state & TS_ASLEEP) {
303 tp->t_state &= ~TS_ASLEEP;
304 wakeup(cl);
305 }
306 selwakeup(&tp->t_wsel);
307 }
308 out:
309 splx(s);
310 }
312 void
313 xencons_stop(struct tty *tp, int flag)
314 {
316 }
319 /* Non-privileged receive callback. */
320 static void
321 xencons_rx(ctrl_msg_t *msg, unsigned long id)
322 {
323 int i;
324 int s;
325 // unsigned long flags;
326 struct xencons_softc *sc;
327 struct tty *tp;
329 sc = device_lookup(&xencons_cd, XENCONS_UNIT(cn_tab->cn_dev));
330 if (sc == NULL)
331 goto out;
333 tp = sc->sc_tty;
334 if (tp == NULL)
335 goto out;
337 s = spltty();
338 // save_and_cli(flags);
339 // simple_lock(&xencons_lock);
340 for (i = 0; i < msg->length; i++)
341 (*tp->t_linesw->l_rint)(msg->msg[i], tp);
342 // simple_unlock(&xencons_lock);
343 // restore_flags(flags);
344 splx(s);
346 out:
347 msg->length = 0;
348 ctrl_if_send_response(msg);
349 }
351 void
352 xenconscn_attach()
353 {
355 cn_tab = &xencons;
357 ctrl_if_early_init();
359 xencons_isconsole = 1;
360 }
362 int
363 xenconscn_getc(dev_t dev)
364 {
366 printf("\n");
367 for (;;);
368 }
370 void
371 xenconscn_putc(dev_t dev, int c)
372 {
373 extern int ctrl_if_evtchn;
375 if (xen_start_info.flags & SIF_INITDOMAIN ||
376 ctrl_if_evtchn == -1) {
377 u_char buf[1];
379 buf[0] = c;
380 (void)HYPERVISOR_console_io(CONSOLEIO_write, 1, buf);
381 } else {
382 ctrl_msg_t msg;
384 msg.type = CMSG_CONSOLE;
385 msg.subtype = CMSG_CONSOLE_DATA;
386 msg.length = 1;
387 msg.msg[0] = c;
388 while (ctrl_if_send_message_noblock(&msg, NULL, 0) == EAGAIN) {
389 HYPERVISOR_yield();
390 /* XXX check return value and queue wait for space
391 * thread/softint */
392 }
393 }
394 }
396 void
397 xenconscn_pollc(dev_t dev, int on)
398 {
400 }
402 /*
403 * Set line parameters.
404 */
405 int
406 xencons_param(struct tty *tp, struct termios *t)
407 {
409 tp->t_ispeed = t->c_ispeed;
410 tp->t_ospeed = t->c_ospeed;
411 tp->t_cflag = t->c_cflag;
412 return (0);
413 }