1 /*	$NetBSD: cons.c,v 1.6 2016/01/26 23:12:17 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).  The driver attempts to look like a tty just
35  * enough to fool isatty().  Let's see how far that gets us.
36  */
37 
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: cons.c,v 1.6 2016/01/26 23:12:17 pooka Exp $");
40 
41 #include <sys/param.h>
42 #include <sys/file.h>
43 #include <sys/filedesc.h>
44 #include <sys/ioctl.h>
45 #include <sys/kernel.h>
46 #include <sys/kmem.h>
47 #include <sys/poll.h>
48 #include <sys/proc.h>
49 #include <sys/stat.h>
50 #include <sys/termios.h>
51 
52 #include <rump-sys/kern.h>
53 
54 #include <rump/rumpuser.h>
55 
56 static int rumpcons_write(struct file *, off_t *, struct uio *,
57 			  kauth_cred_t, int);
58 static int rumpcons_ioctl(struct file *, u_long, void *);
59 static int rumpcons_stat(struct file *, struct stat *);
60 static int rumpcons_poll(struct file *, int events);
61 
62 static const struct fileops rumpcons_fileops = {
63 	.fo_read = (void *)nullop,
64 	.fo_write = rumpcons_write,
65 	.fo_ioctl = rumpcons_ioctl,
66 	.fo_fcntl = fnullop_fcntl,
67 	.fo_poll = rumpcons_poll,
68 	.fo_stat = rumpcons_stat,
69 	.fo_close = (void *)nullop,
70 	.fo_kqfilter = fnullop_kqfilter,
71 	.fo_restart = fnullop_restart,
72 };
73 
74 void
rump_consdev_init(void)75 rump_consdev_init(void)
76 {
77 	struct file *fp;
78 	int fd, error;
79 
80 	KASSERT(fd_getfile(0) == NULL);
81 	KASSERT(fd_getfile(1) == NULL);
82 	KASSERT(fd_getfile(2) == NULL);
83 
84 	/* then, map a file descriptor to the device */
85 	if ((error = fd_allocfile(&fp, &fd)) != 0)
86 		panic("cons fd_allocfile failed: %d", error);
87 
88 	fp->f_flag = FWRITE;
89 	fp->f_type = DTYPE_MISC;
90 	fp->f_ops = &rumpcons_fileops;
91 	fp->f_data = NULL;
92 	fd_affix(curproc, fp, fd);
93 
94 	KASSERT(fd == 0);
95 	error += fd_dup2(fp, 1, 0);
96 	error += fd_dup2(fp, 2, 0);
97 
98 	if (error)
99 		panic("failed to dup fd 0/1/2");
100 }
101 
102 static int
rumpcons_write(struct file * fp,off_t * off,struct uio * uio,kauth_cred_t cred,int flags)103 rumpcons_write(struct file *fp, off_t *off, struct uio *uio,
104 	kauth_cred_t cred, int flags)
105 {
106 	char *buf;
107 	size_t len, n;
108 	int error = 0;
109 
110 	buf = kmem_alloc(PAGE_SIZE, KM_SLEEP);
111 	while (uio->uio_resid > 0) {
112 		len = min(PAGE_SIZE, uio->uio_resid);
113 		error = uiomove(buf, len, uio);
114 		if (error)
115 			break;
116 
117 		for (n = 0; n < len; n++) {
118 			rumpuser_putchar(*(buf+n));
119 		}
120 	}
121 	kmem_free(buf, PAGE_SIZE);
122 
123 	return error;
124 }
125 
126 static int
rumpcons_ioctl(struct file * fp,u_long cmd,void * data)127 rumpcons_ioctl(struct file *fp, u_long cmd, void *data)
128 {
129 
130 	if (cmd == TIOCGETA)
131 		return 0;
132 
133 	return ENOTTY; /* considering how we are cheating, lol */
134 }
135 
136 static int
rumpcons_stat(struct file * fp,struct stat * sb)137 rumpcons_stat(struct file *fp, struct stat *sb)
138 {
139 
140 	memset(sb, 0, sizeof(*sb));
141 	sb->st_mode = 0600 | _S_IFCHR;
142 	sb->st_atimespec = sb->st_mtimespec = sb->st_ctimespec = boottime;
143 	sb->st_birthtimespec = boottime;
144 
145 	return 0;
146 }
147 
148 static int
rumpcons_poll(struct file * fp,int events)149 rumpcons_poll(struct file *fp, int events)
150 {
151 	int revents = 0;
152 
153 	if (events & (POLLOUT | POLLWRNORM))
154 		revents |= events & (POLLOUT | POLLWRNORM);
155 
156 	return revents;
157 }
158