cons.c revision 1.80 1 1.80 riastrad /* $NetBSD: cons.c,v 1.80 2022/10/03 19:12:29 riastradh Exp $ */
2 1.18 cgd
3 1.1 cgd /*
4 1.68 rmind * Copyright (c) 1988 University of Utah.
5 1.17 cgd * Copyright (c) 1990, 1993
6 1.17 cgd * The Regents of the University of California. All rights reserved.
7 1.1 cgd *
8 1.1 cgd * This code is derived from software contributed to Berkeley by
9 1.1 cgd * the Systems Programming Group of the University of Utah Computer
10 1.1 cgd * Science Department.
11 1.1 cgd *
12 1.1 cgd * Redistribution and use in source and binary forms, with or without
13 1.1 cgd * modification, are permitted provided that the following conditions
14 1.1 cgd * are met:
15 1.1 cgd * 1. Redistributions of source code must retain the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer.
17 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 cgd * notice, this list of conditions and the following disclaimer in the
19 1.1 cgd * documentation and/or other materials provided with the distribution.
20 1.49 agc * 3. Neither the name of the University nor the names of its contributors
21 1.49 agc * may be used to endorse or promote products derived from this software
22 1.49 agc * without specific prior written permission.
23 1.49 agc *
24 1.49 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.49 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.49 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.49 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.49 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.49 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.49 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.49 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.49 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.49 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.49 agc * SUCH DAMAGE.
35 1.49 agc *
36 1.77 riastrad * from: Utah $Hdr: cons.c 1.7 92/01/21$
37 1.49 agc *
38 1.49 agc * @(#)cons.c 8.2 (Berkeley) 1/12/94
39 1.49 agc */
40 1.49 agc
41 1.41 lukem #include <sys/cdefs.h>
42 1.80 riastrad __KERNEL_RCSID(0, "$NetBSD: cons.c,v 1.80 2022/10/03 19:12:29 riastradh Exp $");
43 1.1 cgd
44 1.10 cgd #include <sys/param.h>
45 1.10 cgd #include <sys/proc.h>
46 1.10 cgd #include <sys/systm.h>
47 1.10 cgd #include <sys/buf.h>
48 1.10 cgd #include <sys/ioctl.h>
49 1.56 ws #include <sys/poll.h>
50 1.10 cgd #include <sys/tty.h>
51 1.10 cgd #include <sys/file.h>
52 1.10 cgd #include <sys/conf.h>
53 1.10 cgd #include <sys/vnode.h>
54 1.58 elad #include <sys/kauth.h>
55 1.62 ad #include <sys/mutex.h>
56 1.1 cgd
57 1.11 cgd #include <dev/cons.h>
58 1.1 cgd
59 1.73 mlelstv #include "nullcons.h"
60 1.73 mlelstv
61 1.43 gehenna dev_type_open(cnopen);
62 1.43 gehenna dev_type_close(cnclose);
63 1.43 gehenna dev_type_read(cnread);
64 1.43 gehenna dev_type_write(cnwrite);
65 1.43 gehenna dev_type_ioctl(cnioctl);
66 1.43 gehenna dev_type_poll(cnpoll);
67 1.45 jdolecek dev_type_kqfilter(cnkqfilter);
68 1.43 gehenna
69 1.64 ad static bool cn_redirect(dev_t *, int, int *);
70 1.50 dsl
71 1.43 gehenna const struct cdevsw cons_cdevsw = {
72 1.71 dholland .d_open = cnopen,
73 1.71 dholland .d_close = cnclose,
74 1.71 dholland .d_read = cnread,
75 1.71 dholland .d_write = cnwrite,
76 1.71 dholland .d_ioctl = cnioctl,
77 1.71 dholland .d_stop = nostop,
78 1.71 dholland .d_tty = notty,
79 1.71 dholland .d_poll = cnpoll,
80 1.71 dholland .d_mmap = nommap,
81 1.71 dholland .d_kqfilter = cnkqfilter,
82 1.72 dholland .d_discard = nodiscard,
83 1.71 dholland .d_flag = D_TTY
84 1.43 gehenna };
85 1.43 gehenna
86 1.10 cgd struct tty *constty = NULL; /* virtual console output device */
87 1.52 cdi struct consdev *cn_tab; /* physical console device info */
88 1.50 dsl struct vnode *cn_devvp[2]; /* vnode for underlying device. */
89 1.1 cgd
90 1.80 riastrad void
91 1.80 riastrad cn_set_tab(struct consdev *tab)
92 1.80 riastrad {
93 1.80 riastrad
94 1.80 riastrad /*
95 1.80 riastrad * This is a point that we should have KASSERT(cold) or add
96 1.80 riastrad * synchronization in case this can happen after cold boot.
97 1.80 riastrad * However, cn_tab initialization is so critical to any
98 1.80 riastrad * diagnostics or debugging that we need to tread carefully
99 1.80 riastrad * about introducing new ways to crash. So let's put the
100 1.80 riastrad * assertion in only after we've audited most or all of the
101 1.80 riastrad * cn_tab updates.
102 1.80 riastrad */
103 1.80 riastrad cn_tab = tab;
104 1.80 riastrad }
105 1.80 riastrad
106 1.7 andrew int
107 1.57 christos cnopen(dev_t dev, int flag, int mode, struct lwp *l)
108 1.1 cgd {
109 1.37 mrg dev_t cndev;
110 1.65 ad int unit, error;
111 1.50 dsl
112 1.50 dsl unit = minor(dev);
113 1.50 dsl if (unit > 1)
114 1.50 dsl return ENODEV;
115 1.21 mycroft
116 1.1 cgd if (cn_tab == NULL)
117 1.1 cgd return (0);
118 1.10 cgd
119 1.10 cgd /*
120 1.10 cgd * always open the 'real' console device, so we don't get nailed
121 1.10 cgd * later. This follows normal device semantics; they always get
122 1.10 cgd * open() calls.
123 1.10 cgd */
124 1.37 mrg cndev = cn_tab->cn_dev;
125 1.73 mlelstv #if NNULLCONS > 0
126 1.73 mlelstv if (cndev == NODEV) {
127 1.73 mlelstv nullconsattach(0);
128 1.73 mlelstv }
129 1.73 mlelstv #else /* NNULLCONS > 0 */
130 1.37 mrg if (cndev == NODEV) {
131 1.33 leo /*
132 1.33 leo * This is most likely an error in the console attach
133 1.53 wiz * code. Panicking looks better than jumping into nowhere
134 1.33 leo * through cdevsw below....
135 1.33 leo */
136 1.44 provos panic("cnopen: no console device");
137 1.33 leo }
138 1.73 mlelstv #endif /* NNULLCONS > 0 */
139 1.37 mrg if (dev == cndev) {
140 1.37 mrg /*
141 1.37 mrg * This causes cnopen() to be called recursively, which
142 1.37 mrg * is generally a bad thing. It is often caused when
143 1.37 mrg * dev == 0 and cn_dev has not been set, but was probably
144 1.37 mrg * initialised to 0.
145 1.37 mrg */
146 1.44 provos panic("cnopen: cn_tab->cn_dev == dev");
147 1.37 mrg }
148 1.65 ad if (cn_devvp[unit] != NULLVP)
149 1.65 ad return 0;
150 1.79 riastrad if ((error = cdevvp(cndev, &cn_devvp[unit])) != 0) {
151 1.65 ad printf("cnopen: unable to get vnode reference\n");
152 1.79 riastrad return error;
153 1.79 riastrad }
154 1.78 riastrad vn_lock(cn_devvp[unit], LK_EXCLUSIVE | LK_RETRY);
155 1.78 riastrad error = VOP_OPEN(cn_devvp[unit], flag, kauth_cred_get());
156 1.78 riastrad VOP_UNLOCK(cn_devvp[unit]);
157 1.65 ad return error;
158 1.1 cgd }
159 1.54 perry
160 1.7 andrew int
161 1.57 christos cnclose(dev_t dev, int flag, int mode, struct lwp *l)
162 1.1 cgd {
163 1.10 cgd struct vnode *vp;
164 1.65 ad int unit, error;
165 1.50 dsl
166 1.50 dsl unit = minor(dev);
167 1.10 cgd
168 1.1 cgd if (cn_tab == NULL)
169 1.1 cgd return (0);
170 1.10 cgd
171 1.65 ad vp = cn_devvp[unit];
172 1.65 ad cn_devvp[unit] = NULL;
173 1.78 riastrad vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
174 1.78 riastrad error = VOP_CLOSE(vp, flag, kauth_cred_get());
175 1.78 riastrad VOP_UNLOCK(vp);
176 1.78 riastrad vrele(vp);
177 1.65 ad return error;
178 1.1 cgd }
179 1.54 perry
180 1.7 andrew int
181 1.46 matt cnread(dev_t dev, struct uio *uio, int flag)
182 1.1 cgd {
183 1.50 dsl int error;
184 1.21 mycroft
185 1.10 cgd /*
186 1.10 cgd * If we would redirect input, punt. This will keep strange
187 1.10 cgd * things from happening to people who are using the real
188 1.10 cgd * console. Nothing should be using /dev/console for
189 1.10 cgd * input (except a shell in single-user mode, but then,
190 1.10 cgd * one wouldn't TIOCCONS then).
191 1.10 cgd */
192 1.64 ad if (!cn_redirect(&dev, 1, &error))
193 1.50 dsl return error;
194 1.64 ad return cdev_read(dev, uio, flag);
195 1.1 cgd }
196 1.54 perry
197 1.7 andrew int
198 1.46 matt cnwrite(dev_t dev, struct uio *uio, int flag)
199 1.1 cgd {
200 1.50 dsl int error;
201 1.21 mycroft
202 1.50 dsl /* Redirect output, if that's appropriate. */
203 1.64 ad if (!cn_redirect(&dev, 0, &error))
204 1.50 dsl return error;
205 1.64 ad return cdev_write(dev, uio, flag);
206 1.25 mycroft }
207 1.25 mycroft
208 1.7 andrew int
209 1.63 christos cnioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
210 1.1 cgd {
211 1.1 cgd int error;
212 1.1 cgd
213 1.62 ad error = 0;
214 1.62 ad
215 1.1 cgd /*
216 1.1 cgd * Superuser can always use this to wrest control of console
217 1.1 cgd * output from the "virtual" console.
218 1.1 cgd */
219 1.10 cgd if (cmd == TIOCCONS && constty != NULL) {
220 1.69 elad error = kauth_authorize_device_tty(l->l_cred,
221 1.69 elad KAUTH_DEVICE_TTY_VIRTUAL, constty);
222 1.62 ad if (!error)
223 1.62 ad constty = NULL;
224 1.62 ad return (error);
225 1.1 cgd }
226 1.10 cgd
227 1.10 cgd /*
228 1.10 cgd * Redirect the ioctl, if that's appropriate.
229 1.10 cgd * Note that strange things can happen, if a program does
230 1.10 cgd * ioctls on /dev/console, then the console is redirected
231 1.10 cgd * out from under it.
232 1.10 cgd */
233 1.64 ad if (!cn_redirect(&dev, 0, &error))
234 1.64 ad return error;
235 1.64 ad return cdev_ioctl(dev, cmd, data, flag, l);
236 1.1 cgd }
237 1.1 cgd
238 1.1 cgd /*ARGSUSED*/
239 1.7 andrew int
240 1.57 christos cnpoll(dev_t dev, int events, struct lwp *l)
241 1.1 cgd {
242 1.50 dsl int error;
243 1.21 mycroft
244 1.10 cgd /*
245 1.38 lukem * Redirect the poll, if that's appropriate.
246 1.10 cgd * I don't want to think of the possible side effects
247 1.10 cgd * of console redirection here.
248 1.10 cgd */
249 1.64 ad if (!cn_redirect(&dev, 0, &error))
250 1.56 ws return POLLHUP;
251 1.64 ad return cdev_poll(dev, events, l);
252 1.45 jdolecek }
253 1.45 jdolecek
254 1.45 jdolecek /*ARGSUSED*/
255 1.45 jdolecek int
256 1.46 matt cnkqfilter(dev_t dev, struct knote *kn)
257 1.45 jdolecek {
258 1.50 dsl int error;
259 1.45 jdolecek
260 1.45 jdolecek /*
261 1.45 jdolecek * Redirect the kqfilter, if that's appropriate.
262 1.45 jdolecek * I don't want to think of the possible side effects
263 1.45 jdolecek * of console redirection here.
264 1.45 jdolecek */
265 1.64 ad if (!cn_redirect(&dev, 0, &error))
266 1.50 dsl return error;
267 1.64 ad return cdev_kqfilter(dev, kn);
268 1.1 cgd }
269 1.1 cgd
270 1.7 andrew int
271 1.46 matt cngetc(void)
272 1.1 cgd {
273 1.1 cgd if (cn_tab == NULL)
274 1.1 cgd return (0);
275 1.70 matt int s = splhigh();
276 1.70 matt for (;;) {
277 1.70 matt const int rv = (*cn_tab->cn_getc)(cn_tab->cn_dev);
278 1.70 matt if (rv >= 0) {
279 1.70 matt splx(s);
280 1.70 matt return rv;
281 1.70 matt }
282 1.70 matt docritpollhooks();
283 1.70 matt }
284 1.36 itojun }
285 1.36 itojun
286 1.36 itojun int
287 1.46 matt cngetsn(char *cp, int size)
288 1.36 itojun {
289 1.36 itojun char *lp;
290 1.36 itojun int c, len;
291 1.36 itojun
292 1.36 itojun cnpollc(1);
293 1.36 itojun
294 1.36 itojun lp = cp;
295 1.36 itojun len = 0;
296 1.36 itojun for (;;) {
297 1.36 itojun c = cngetc();
298 1.36 itojun switch (c) {
299 1.36 itojun case '\n':
300 1.36 itojun case '\r':
301 1.36 itojun printf("\n");
302 1.36 itojun *lp++ = '\0';
303 1.36 itojun cnpollc(0);
304 1.36 itojun return (len);
305 1.36 itojun case '\b':
306 1.36 itojun case '\177':
307 1.36 itojun case '#':
308 1.36 itojun if (len) {
309 1.36 itojun --len;
310 1.36 itojun --lp;
311 1.36 itojun printf("\b \b");
312 1.36 itojun }
313 1.36 itojun continue;
314 1.36 itojun case '@':
315 1.40 jdolecek case 'u'&037: /* CTRL-u */
316 1.36 itojun len = 0;
317 1.36 itojun lp = cp;
318 1.36 itojun printf("\n");
319 1.36 itojun continue;
320 1.36 itojun default:
321 1.36 itojun if (len + 1 >= size || c < ' ') {
322 1.40 jdolecek printf("\007");
323 1.36 itojun continue;
324 1.36 itojun }
325 1.36 itojun printf("%c", c);
326 1.36 itojun ++len;
327 1.36 itojun *lp++ = c;
328 1.36 itojun }
329 1.36 itojun }
330 1.1 cgd }
331 1.1 cgd
332 1.29 christos void
333 1.46 matt cnputc(int c)
334 1.1 cgd {
335 1.21 mycroft
336 1.1 cgd if (cn_tab == NULL)
337 1.54 perry return;
338 1.29 christos
339 1.75 macallan /*
340 1.75 macallan * XXX
341 1.75 macallan * for some reason this causes ARCS firmware to output an endless stream of
342 1.75 macallan * whitespaces with n32 kernels, so use the pre-1.74 code for now until I can
343 1.75 macallan * figure out why this happens
344 1.75 macallan */
345 1.75 macallan #ifndef sgimips
346 1.1 cgd if (c) {
347 1.70 matt if (c == '\n') {
348 1.74 nakayama (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
349 1.70 matt docritpollhooks();
350 1.70 matt }
351 1.74 nakayama (*cn_tab->cn_putc)(cn_tab->cn_dev, c);
352 1.1 cgd }
353 1.75 macallan #else
354 1.75 macallan if (c) {
355 1.75 macallan (*cn_tab->cn_putc)(cn_tab->cn_dev, c);
356 1.75 macallan if (c == '\n') {
357 1.75 macallan docritpollhooks();
358 1.75 macallan (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
359 1.75 macallan }
360 1.75 macallan }
361 1.75 macallan #endif
362 1.19 mycroft }
363 1.19 mycroft
364 1.19 mycroft void
365 1.46 matt cnpollc(int on)
366 1.19 mycroft {
367 1.19 mycroft static int refcount = 0;
368 1.19 mycroft
369 1.19 mycroft if (cn_tab == NULL)
370 1.19 mycroft return;
371 1.19 mycroft if (!on)
372 1.19 mycroft --refcount;
373 1.19 mycroft if (refcount == 0)
374 1.19 mycroft (*cn_tab->cn_pollc)(cn_tab->cn_dev, on);
375 1.19 mycroft if (on)
376 1.19 mycroft ++refcount;
377 1.21 mycroft }
378 1.21 mycroft
379 1.21 mycroft void
380 1.61 christos nullcnpollc(dev_t dev, int on)
381 1.21 mycroft {
382 1.21 mycroft
383 1.34 thorpej }
384 1.34 thorpej
385 1.34 thorpej void
386 1.46 matt cnbell(u_int pitch, u_int period, u_int volume)
387 1.34 thorpej {
388 1.34 thorpej
389 1.34 thorpej if (cn_tab == NULL || cn_tab->cn_bell == NULL)
390 1.34 thorpej return;
391 1.34 thorpej (*cn_tab->cn_bell)(cn_tab->cn_dev, pitch, period, volume);
392 1.46 matt }
393 1.46 matt
394 1.46 matt void
395 1.46 matt cnflush(void)
396 1.46 matt {
397 1.46 matt if (cn_tab == NULL || cn_tab->cn_flush == NULL)
398 1.46 matt return;
399 1.46 matt (*cn_tab->cn_flush)(cn_tab->cn_dev);
400 1.46 matt }
401 1.54 perry
402 1.46 matt void
403 1.46 matt cnhalt(void)
404 1.46 matt {
405 1.46 matt if (cn_tab == NULL || cn_tab->cn_halt == NULL)
406 1.46 matt return;
407 1.46 matt (*cn_tab->cn_halt)(cn_tab->cn_dev);
408 1.50 dsl }
409 1.50 dsl
410 1.62 ad /*
411 1.62 ad * Redirect output, if that's appropriate. If there's no real console,
412 1.62 ad * return ENXIO.
413 1.62 ad *
414 1.62 ad * Call with tty_mutex held.
415 1.62 ad */
416 1.64 ad static bool
417 1.50 dsl cn_redirect(dev_t *devp, int is_read, int *error)
418 1.50 dsl {
419 1.50 dsl dev_t dev = *devp;
420 1.50 dsl
421 1.50 dsl *error = ENXIO;
422 1.50 dsl if (constty != NULL && minor(dev) == 0 &&
423 1.55 martin (cn_tab == NULL || (cn_tab->cn_pri != CN_REMOTE))) {
424 1.50 dsl if (is_read) {
425 1.50 dsl *error = 0;
426 1.64 ad return false;
427 1.50 dsl }
428 1.50 dsl dev = constty->t_dev;
429 1.50 dsl } else if (cn_tab == NULL)
430 1.64 ad return false;
431 1.50 dsl else
432 1.50 dsl dev = cn_tab->cn_dev;
433 1.50 dsl *devp = dev;
434 1.64 ad return true;
435 1.2 cgd }
436