Home | History | Annotate | Line # | Download | only in rumpkern
cons.c revision 1.3.10.2
      1  1.3.10.2  tls /*	$NetBSD: cons.c,v 1.3.10.2 2014/08/20 00:04:40 tls Exp $	*/
      2  1.3.10.2  tls 
      3  1.3.10.2  tls /*
      4  1.3.10.2  tls  * Copyright (c) 2013 Antti Kantee.  All Rights Reserved.
      5  1.3.10.2  tls  *
      6  1.3.10.2  tls  * Redistribution and use in source and binary forms, with or without
      7  1.3.10.2  tls  * modification, are permitted provided that the following conditions
      8  1.3.10.2  tls  * are met:
      9  1.3.10.2  tls  * 1. Redistributions of source code must retain the above copyright
     10  1.3.10.2  tls  *    notice, this list of conditions and the following disclaimer.
     11  1.3.10.2  tls  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.3.10.2  tls  *    notice, this list of conditions and the following disclaimer in the
     13  1.3.10.2  tls  *    documentation and/or other materials provided with the distribution.
     14  1.3.10.2  tls  *
     15  1.3.10.2  tls  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16  1.3.10.2  tls  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  1.3.10.2  tls  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  1.3.10.2  tls  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  1.3.10.2  tls  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  1.3.10.2  tls  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  1.3.10.2  tls  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  1.3.10.2  tls  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  1.3.10.2  tls  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  1.3.10.2  tls  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  1.3.10.2  tls  * SUCH DAMAGE.
     26  1.3.10.2  tls  */
     27  1.3.10.2  tls 
     28  1.3.10.2  tls /*
     29  1.3.10.2  tls  * rumpcons, a (very) simple console-type device which relays output
     30  1.3.10.2  tls  * to the rumpuser_putchar() hypercall.  This is most useful in
     31  1.3.10.2  tls  * environments where there is no Unix-like host (e.g. Xen DomU).
     32  1.3.10.2  tls  * It's currently a truly half duplex console since there is support
     33  1.3.10.2  tls  * only for writing to the console (there is no hypercall for reading
     34  1.3.10.2  tls  * the host console).  The driver attempts to look like a tty just
     35  1.3.10.2  tls  * enough to fool isatty().  Let's see how far that gets us.
     36  1.3.10.2  tls  */
     37  1.3.10.2  tls 
     38  1.3.10.2  tls #include <sys/cdefs.h>
     39  1.3.10.2  tls __KERNEL_RCSID(0, "$NetBSD: cons.c,v 1.3.10.2 2014/08/20 00:04:40 tls Exp $");
     40  1.3.10.2  tls 
     41  1.3.10.2  tls #include <sys/param.h>
     42  1.3.10.2  tls #include <sys/file.h>
     43  1.3.10.2  tls #include <sys/filedesc.h>
     44  1.3.10.2  tls #include <sys/ioctl.h>
     45  1.3.10.2  tls #include <sys/kernel.h>
     46  1.3.10.2  tls #include <sys/kmem.h>
     47  1.3.10.2  tls #include <sys/proc.h>
     48  1.3.10.2  tls #include <sys/stat.h>
     49  1.3.10.2  tls #include <sys/termios.h>
     50  1.3.10.2  tls 
     51  1.3.10.2  tls #include <rump/rumpuser.h>
     52  1.3.10.2  tls 
     53  1.3.10.2  tls #include "rump_private.h"
     54  1.3.10.2  tls 
     55  1.3.10.2  tls static int rumpcons_write(struct file *, off_t *, struct uio *,
     56  1.3.10.2  tls 			  kauth_cred_t, int);
     57  1.3.10.2  tls static int rumpcons_ioctl(struct file *, u_long, void *);
     58  1.3.10.2  tls static int rumpcons_stat(struct file *, struct stat *);
     59  1.3.10.2  tls 
     60  1.3.10.2  tls static const struct fileops rumpcons_fileops = {
     61  1.3.10.2  tls 	.fo_read = (void *)nullop,
     62  1.3.10.2  tls 	.fo_write = rumpcons_write,
     63  1.3.10.2  tls 	.fo_ioctl = rumpcons_ioctl,
     64  1.3.10.2  tls 	.fo_fcntl = fnullop_fcntl,
     65  1.3.10.2  tls 	.fo_poll = fnullop_poll,
     66  1.3.10.2  tls 	.fo_stat = rumpcons_stat,
     67  1.3.10.2  tls 	.fo_close = (void *)nullop,
     68  1.3.10.2  tls 	.fo_kqfilter = fnullop_kqfilter,
     69  1.3.10.2  tls 	.fo_restart = fnullop_restart,
     70  1.3.10.2  tls };
     71  1.3.10.2  tls 
     72  1.3.10.2  tls void
     73  1.3.10.2  tls rump_consdev_init(void)
     74  1.3.10.2  tls {
     75  1.3.10.2  tls 	struct file *fp;
     76  1.3.10.2  tls 	int fd, error;
     77  1.3.10.2  tls 
     78  1.3.10.2  tls 	/*
     79  1.3.10.2  tls 	 * We want to open the descriptors for the implicit proc
     80  1.3.10.2  tls 	 * so that they get inherited by default to all processes.
     81  1.3.10.2  tls 	 */
     82  1.3.10.2  tls 	KASSERT(curproc->p_pid == 1);
     83  1.3.10.2  tls 	KASSERT(fd_getfile(0) == NULL);
     84  1.3.10.2  tls 
     85  1.3.10.2  tls 	/* then, map a file descriptor to the device */
     86  1.3.10.2  tls 	if ((error = fd_allocfile(&fp, &fd)) != 0)
     87  1.3.10.2  tls 		panic("cons fd_allocfile failed: %d", error);
     88  1.3.10.2  tls 
     89  1.3.10.2  tls 	fp->f_flag = FWRITE;
     90  1.3.10.2  tls 	fp->f_type = DTYPE_MISC;
     91  1.3.10.2  tls 	fp->f_ops = &rumpcons_fileops;
     92  1.3.10.2  tls 	fp->f_data = NULL;
     93  1.3.10.2  tls 	fd_affix(curproc, fp, fd);
     94  1.3.10.2  tls 
     95  1.3.10.2  tls 	KASSERT(fd == 0);
     96  1.3.10.2  tls 	error += fd_dup2(fp, 1, 0);
     97  1.3.10.2  tls 	error += fd_dup2(fp, 2, 0);
     98  1.3.10.2  tls 
     99  1.3.10.2  tls 	if (error)
    100  1.3.10.2  tls 		panic("failed to dup fd 0/1/2");
    101  1.3.10.2  tls }
    102  1.3.10.2  tls 
    103  1.3.10.2  tls static int
    104  1.3.10.2  tls rumpcons_write(struct file *fp, off_t *off, struct uio *uio,
    105  1.3.10.2  tls 	kauth_cred_t cred, int flags)
    106  1.3.10.2  tls {
    107  1.3.10.2  tls 	char *buf;
    108  1.3.10.2  tls 	size_t len, n;
    109  1.3.10.2  tls 	int error = 0;
    110  1.3.10.2  tls 
    111  1.3.10.2  tls 	buf = kmem_alloc(PAGE_SIZE, KM_SLEEP);
    112  1.3.10.2  tls 	while (uio->uio_resid > 0) {
    113  1.3.10.2  tls 		len = min(PAGE_SIZE, uio->uio_resid);
    114  1.3.10.2  tls 		error = uiomove(buf, len, uio);
    115  1.3.10.2  tls 		if (error)
    116  1.3.10.2  tls 			break;
    117  1.3.10.2  tls 
    118  1.3.10.2  tls 		for (n = 0; n < len; n++) {
    119  1.3.10.2  tls 			rumpuser_putchar(*(buf+n));
    120  1.3.10.2  tls 		}
    121  1.3.10.2  tls 	}
    122  1.3.10.2  tls 	kmem_free(buf, PAGE_SIZE);
    123  1.3.10.2  tls 
    124  1.3.10.2  tls 	return error;
    125  1.3.10.2  tls }
    126  1.3.10.2  tls 
    127  1.3.10.2  tls static int
    128  1.3.10.2  tls rumpcons_ioctl(struct file *fp, u_long cmd, void *data)
    129  1.3.10.2  tls {
    130  1.3.10.2  tls 
    131  1.3.10.2  tls 	if (cmd == TIOCGETA)
    132  1.3.10.2  tls 		return 0;
    133  1.3.10.2  tls 
    134  1.3.10.2  tls 	return ENOTTY; /* considering how we are cheating, lol */
    135  1.3.10.2  tls }
    136  1.3.10.2  tls 
    137  1.3.10.2  tls static int
    138  1.3.10.2  tls rumpcons_stat(struct file *fp, struct stat *sb)
    139  1.3.10.2  tls {
    140  1.3.10.2  tls 
    141  1.3.10.2  tls 	memset(sb, 0, sizeof(*sb));
    142  1.3.10.2  tls 	sb->st_mode = 0600 | _S_IFCHR;
    143  1.3.10.2  tls 	sb->st_atimespec = sb->st_mtimespec = sb->st_ctimespec = boottime;
    144  1.3.10.2  tls 	sb->st_birthtimespec = boottime;
    145  1.3.10.2  tls 
    146  1.3.10.2  tls 	return 0;
    147  1.3.10.2  tls }
    148