cons.c revision 1.4 1 /* $NetBSD: cons.c,v 1.4 2014/08/25 14:58:48 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.4 2014/08/25 14:58:48 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/proc.h>
48 #include <sys/stat.h>
49 #include <sys/termios.h>
50
51 #include <rump/rumpuser.h>
52
53 #include "rump_private.h"
54
55 static int rumpcons_write(struct file *, off_t *, struct uio *,
56 kauth_cred_t, int);
57 static int rumpcons_ioctl(struct file *, u_long, void *);
58 static int rumpcons_stat(struct file *, struct stat *);
59
60 static const struct fileops rumpcons_fileops = {
61 .fo_read = (void *)nullop,
62 .fo_write = rumpcons_write,
63 .fo_ioctl = rumpcons_ioctl,
64 .fo_fcntl = fnullop_fcntl,
65 .fo_poll = fnullop_poll,
66 .fo_stat = rumpcons_stat,
67 .fo_close = (void *)nullop,
68 .fo_kqfilter = fnullop_kqfilter,
69 .fo_restart = fnullop_restart,
70 };
71
72 void
73 rump_consdev_init(void)
74 {
75 struct file *fp;
76 int fd, error;
77
78 KASSERT(fd_getfile(0) == NULL);
79 KASSERT(fd_getfile(1) == NULL);
80 KASSERT(fd_getfile(2) == NULL);
81
82 /* then, map a file descriptor to the device */
83 if ((error = fd_allocfile(&fp, &fd)) != 0)
84 panic("cons fd_allocfile failed: %d", error);
85
86 fp->f_flag = FWRITE;
87 fp->f_type = DTYPE_MISC;
88 fp->f_ops = &rumpcons_fileops;
89 fp->f_data = NULL;
90 fd_affix(curproc, fp, fd);
91
92 KASSERT(fd == 0);
93 error += fd_dup2(fp, 1, 0);
94 error += fd_dup2(fp, 2, 0);
95
96 if (error)
97 panic("failed to dup fd 0/1/2");
98 }
99
100 static int
101 rumpcons_write(struct file *fp, off_t *off, struct uio *uio,
102 kauth_cred_t cred, int flags)
103 {
104 char *buf;
105 size_t len, n;
106 int error = 0;
107
108 buf = kmem_alloc(PAGE_SIZE, KM_SLEEP);
109 while (uio->uio_resid > 0) {
110 len = min(PAGE_SIZE, uio->uio_resid);
111 error = uiomove(buf, len, uio);
112 if (error)
113 break;
114
115 for (n = 0; n < len; n++) {
116 rumpuser_putchar(*(buf+n));
117 }
118 }
119 kmem_free(buf, PAGE_SIZE);
120
121 return error;
122 }
123
124 static int
125 rumpcons_ioctl(struct file *fp, u_long cmd, void *data)
126 {
127
128 if (cmd == TIOCGETA)
129 return 0;
130
131 return ENOTTY; /* considering how we are cheating, lol */
132 }
133
134 static int
135 rumpcons_stat(struct file *fp, struct stat *sb)
136 {
137
138 memset(sb, 0, sizeof(*sb));
139 sb->st_mode = 0600 | _S_IFCHR;
140 sb->st_atimespec = sb->st_mtimespec = sb->st_ctimespec = boottime;
141 sb->st_birthtimespec = boottime;
142
143 return 0;
144 }
145