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