xref: /netbsd-src/sys/rump/librump/rumpkern/cons.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: cons.c,v 1.3 2013/09/08 04:37: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.3 2013/09/08 04:37: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/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 	/*
79 	 * We want to open the descriptors for the implicit proc
80 	 * so that they get inherited by default to all processes.
81 	 */
82 	KASSERT(curproc->p_pid == 1);
83 	KASSERT(fd_getfile(0) == 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
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 = min(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
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
138 rumpcons_stat(struct file *fp, struct stat *sb)
139 {
140 
141 	memset(sb, 0, sizeof(*sb));
142 	sb->st_mode = 0600 | _S_IFCHR;
143 	sb->st_atimespec = sb->st_mtimespec = sb->st_ctimespec = boottime;
144 	sb->st_birthtimespec = boottime;
145 
146 	return 0;
147 }
148