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