1 /* $NetBSD: cons.c,v 1.9 2020/01/02 15:42:27 thorpej 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.9 2020/01/02 15:42:27 thorpej 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_name = "rumpcons",
64 .fo_read = (void *)nullop,
65 .fo_write = rumpcons_write,
66 .fo_ioctl = rumpcons_ioctl,
67 .fo_fcntl = fnullop_fcntl,
68 .fo_poll = rumpcons_poll,
69 .fo_stat = rumpcons_stat,
70 .fo_close = (void *)nullop,
71 .fo_kqfilter = fnullop_kqfilter,
72 .fo_restart = fnullop_restart,
73 };
74
75 void
rump_consdev_init(void)76 rump_consdev_init(void)
77 {
78 struct file *fp;
79 int fd, error;
80
81 KASSERT(fd_getfile(0) == NULL);
82 KASSERT(fd_getfile(1) == NULL);
83 KASSERT(fd_getfile(2) == NULL);
84
85 /* then, map a file descriptor to the device */
86 if ((error = fd_allocfile(&fp, &fd)) != 0)
87 panic("cons fd_allocfile failed: %d", error);
88
89 fp->f_flag = FWRITE;
90 fp->f_type = DTYPE_MISC;
91 fp->f_ops = &rumpcons_fileops;
92 fp->f_data = NULL;
93 fd_affix(curproc, fp, fd);
94
95 KASSERT(fd == 0);
96 error += fd_dup2(fp, 1, 0);
97 error += fd_dup2(fp, 2, 0);
98
99 if (error)
100 panic("failed to dup fd 0/1/2");
101 }
102
103 static int
rumpcons_write(struct file * fp,off_t * off,struct uio * uio,kauth_cred_t cred,int flags)104 rumpcons_write(struct file *fp, off_t *off, struct uio *uio,
105 kauth_cred_t cred, int flags)
106 {
107 char *buf;
108 size_t len, n;
109 int error = 0;
110
111 buf = kmem_alloc(PAGE_SIZE, KM_SLEEP);
112 while (uio->uio_resid > 0) {
113 len = uimin(PAGE_SIZE, uio->uio_resid);
114 error = uiomove(buf, len, uio);
115 if (error)
116 break;
117
118 for (n = 0; n < len; n++) {
119 rumpuser_putchar(*(buf+n));
120 }
121 }
122 kmem_free(buf, PAGE_SIZE);
123
124 return error;
125 }
126
127 static int
rumpcons_ioctl(struct file * fp,u_long cmd,void * data)128 rumpcons_ioctl(struct file *fp, u_long cmd, void *data)
129 {
130
131 if (cmd == TIOCGETA)
132 return 0;
133
134 return ENOTTY; /* considering how we are cheating, lol */
135 }
136
137 static int
rumpcons_stat(struct file * fp,struct stat * sb)138 rumpcons_stat(struct file *fp, struct stat *sb)
139 {
140 struct timespec ts;
141
142 getnanoboottime(&ts);
143
144 memset(sb, 0, sizeof(*sb));
145 sb->st_mode = 0600 | _S_IFCHR;
146 sb->st_atimespec = sb->st_mtimespec = sb->st_ctimespec = ts;
147 sb->st_birthtimespec = ts;
148
149 return 0;
150 }
151
152 static int
rumpcons_poll(struct file * fp,int events)153 rumpcons_poll(struct file *fp, int events)
154 {
155 int revents = 0;
156
157 if (events & (POLLOUT | POLLWRNORM))
158 revents |= events & (POLLOUT | POLLWRNORM);
159
160 return revents;
161 }
162