cons.c revision 1.86 1 1.86 riastrad /* $NetBSD: cons.c,v 1.86 2022/10/04 05:20:01 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.86 riastrad __KERNEL_RCSID(0, "$NetBSD: cons.c,v 1.86 2022/10/04 05:20:01 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.81 riastrad #include <sys/module.h>
57 1.1 cgd
58 1.11 cgd #include <dev/cons.h>
59 1.1 cgd
60 1.73 mlelstv #include "nullcons.h"
61 1.73 mlelstv
62 1.43 gehenna dev_type_open(cnopen);
63 1.43 gehenna dev_type_close(cnclose);
64 1.43 gehenna dev_type_read(cnread);
65 1.43 gehenna dev_type_write(cnwrite);
66 1.43 gehenna dev_type_ioctl(cnioctl);
67 1.43 gehenna dev_type_poll(cnpoll);
68 1.45 jdolecek dev_type_kqfilter(cnkqfilter);
69 1.43 gehenna
70 1.86 riastrad static bool cn_redirect(dev_t *, int, int *);
71 1.50 dsl
72 1.43 gehenna const struct cdevsw cons_cdevsw = {
73 1.71 dholland .d_open = cnopen,
74 1.71 dholland .d_close = cnclose,
75 1.71 dholland .d_read = cnread,
76 1.71 dholland .d_write = cnwrite,
77 1.71 dholland .d_ioctl = cnioctl,
78 1.71 dholland .d_stop = nostop,
79 1.71 dholland .d_tty = notty,
80 1.71 dholland .d_poll = cnpoll,
81 1.71 dholland .d_mmap = nommap,
82 1.71 dholland .d_kqfilter = cnkqfilter,
83 1.72 dholland .d_discard = nodiscard,
84 1.85 riastrad .d_flag = D_TTY
85 1.43 gehenna };
86 1.43 gehenna
87 1.81 riastrad static struct kmutex cn_lock;
88 1.81 riastrad
89 1.86 riastrad struct tty *constty = NULL; /* virtual console output device */
90 1.52 cdi struct consdev *cn_tab; /* physical console device info */
91 1.50 dsl struct vnode *cn_devvp[2]; /* vnode for underlying device. */
92 1.1 cgd
93 1.80 riastrad void
94 1.80 riastrad cn_set_tab(struct consdev *tab)
95 1.80 riastrad {
96 1.80 riastrad
97 1.80 riastrad /*
98 1.80 riastrad * This is a point that we should have KASSERT(cold) or add
99 1.80 riastrad * synchronization in case this can happen after cold boot.
100 1.80 riastrad * However, cn_tab initialization is so critical to any
101 1.80 riastrad * diagnostics or debugging that we need to tread carefully
102 1.80 riastrad * about introducing new ways to crash. So let's put the
103 1.80 riastrad * assertion in only after we've audited most or all of the
104 1.80 riastrad * cn_tab updates.
105 1.80 riastrad */
106 1.80 riastrad cn_tab = tab;
107 1.80 riastrad }
108 1.80 riastrad
109 1.7 andrew int
110 1.57 christos cnopen(dev_t dev, int flag, int mode, struct lwp *l)
111 1.1 cgd {
112 1.37 mrg dev_t cndev;
113 1.65 ad int unit, error;
114 1.50 dsl
115 1.50 dsl unit = minor(dev);
116 1.50 dsl if (unit > 1)
117 1.50 dsl return ENODEV;
118 1.21 mycroft
119 1.81 riastrad mutex_enter(&cn_lock);
120 1.81 riastrad
121 1.81 riastrad if (cn_tab == NULL) {
122 1.81 riastrad error = 0;
123 1.81 riastrad goto out;
124 1.81 riastrad }
125 1.10 cgd
126 1.10 cgd /*
127 1.10 cgd * always open the 'real' console device, so we don't get nailed
128 1.10 cgd * later. This follows normal device semantics; they always get
129 1.10 cgd * open() calls.
130 1.10 cgd */
131 1.37 mrg cndev = cn_tab->cn_dev;
132 1.73 mlelstv #if NNULLCONS > 0
133 1.73 mlelstv if (cndev == NODEV) {
134 1.73 mlelstv nullconsattach(0);
135 1.73 mlelstv }
136 1.73 mlelstv #else /* NNULLCONS > 0 */
137 1.37 mrg if (cndev == NODEV) {
138 1.33 leo /*
139 1.33 leo * This is most likely an error in the console attach
140 1.53 wiz * code. Panicking looks better than jumping into nowhere
141 1.33 leo * through cdevsw below....
142 1.33 leo */
143 1.44 provos panic("cnopen: no console device");
144 1.33 leo }
145 1.73 mlelstv #endif /* NNULLCONS > 0 */
146 1.37 mrg if (dev == cndev) {
147 1.37 mrg /*
148 1.37 mrg * This causes cnopen() to be called recursively, which
149 1.37 mrg * is generally a bad thing. It is often caused when
150 1.37 mrg * dev == 0 and cn_dev has not been set, but was probably
151 1.37 mrg * initialised to 0.
152 1.37 mrg */
153 1.44 provos panic("cnopen: cn_tab->cn_dev == dev");
154 1.37 mrg }
155 1.81 riastrad if (cn_devvp[unit] != NULLVP) {
156 1.81 riastrad error = 0;
157 1.81 riastrad goto out;
158 1.81 riastrad }
159 1.79 riastrad if ((error = cdevvp(cndev, &cn_devvp[unit])) != 0) {
160 1.65 ad printf("cnopen: unable to get vnode reference\n");
161 1.81 riastrad goto out;
162 1.79 riastrad }
163 1.78 riastrad vn_lock(cn_devvp[unit], LK_EXCLUSIVE | LK_RETRY);
164 1.78 riastrad error = VOP_OPEN(cn_devvp[unit], flag, kauth_cred_get());
165 1.78 riastrad VOP_UNLOCK(cn_devvp[unit]);
166 1.81 riastrad
167 1.81 riastrad out: mutex_exit(&cn_lock);
168 1.65 ad return error;
169 1.1 cgd }
170 1.54 perry
171 1.7 andrew int
172 1.57 christos cnclose(dev_t dev, int flag, int mode, struct lwp *l)
173 1.1 cgd {
174 1.10 cgd struct vnode *vp;
175 1.65 ad int unit, error;
176 1.50 dsl
177 1.50 dsl unit = minor(dev);
178 1.82 riastrad if (unit > 1)
179 1.82 riastrad return ENODEV;
180 1.10 cgd
181 1.81 riastrad mutex_enter(&cn_lock);
182 1.81 riastrad
183 1.81 riastrad if (cn_tab == NULL) {
184 1.81 riastrad error = 0;
185 1.81 riastrad goto out;
186 1.81 riastrad }
187 1.10 cgd
188 1.65 ad vp = cn_devvp[unit];
189 1.65 ad cn_devvp[unit] = NULL;
190 1.78 riastrad vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
191 1.78 riastrad error = VOP_CLOSE(vp, flag, kauth_cred_get());
192 1.78 riastrad VOP_UNLOCK(vp);
193 1.78 riastrad vrele(vp);
194 1.81 riastrad
195 1.81 riastrad out: mutex_exit(&cn_lock);
196 1.65 ad return error;
197 1.1 cgd }
198 1.54 perry
199 1.7 andrew int
200 1.46 matt cnread(dev_t dev, struct uio *uio, int flag)
201 1.1 cgd {
202 1.50 dsl int error;
203 1.21 mycroft
204 1.10 cgd /*
205 1.10 cgd * If we would redirect input, punt. This will keep strange
206 1.10 cgd * things from happening to people who are using the real
207 1.10 cgd * console. Nothing should be using /dev/console for
208 1.10 cgd * input (except a shell in single-user mode, but then,
209 1.10 cgd * one wouldn't TIOCCONS then).
210 1.10 cgd */
211 1.86 riastrad if (!cn_redirect(&dev, 1, &error))
212 1.50 dsl return error;
213 1.86 riastrad return cdev_read(dev, uio, flag);
214 1.1 cgd }
215 1.54 perry
216 1.7 andrew int
217 1.46 matt cnwrite(dev_t dev, struct uio *uio, int flag)
218 1.1 cgd {
219 1.50 dsl int error;
220 1.21 mycroft
221 1.50 dsl /* Redirect output, if that's appropriate. */
222 1.86 riastrad if (!cn_redirect(&dev, 0, &error))
223 1.50 dsl return error;
224 1.86 riastrad return cdev_write(dev, uio, flag);
225 1.25 mycroft }
226 1.25 mycroft
227 1.7 andrew int
228 1.63 christos cnioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
229 1.1 cgd {
230 1.1 cgd int error;
231 1.1 cgd
232 1.62 ad error = 0;
233 1.62 ad
234 1.1 cgd /*
235 1.1 cgd * Superuser can always use this to wrest control of console
236 1.1 cgd * output from the "virtual" console.
237 1.1 cgd */
238 1.86 riastrad if (cmd == TIOCCONS && constty != NULL) {
239 1.69 elad error = kauth_authorize_device_tty(l->l_cred,
240 1.86 riastrad KAUTH_DEVICE_TTY_VIRTUAL, constty);
241 1.62 ad if (!error)
242 1.86 riastrad constty = NULL;
243 1.86 riastrad return (error);
244 1.1 cgd }
245 1.86 riastrad
246 1.10 cgd /*
247 1.10 cgd * Redirect the ioctl, if that's appropriate.
248 1.10 cgd * Note that strange things can happen, if a program does
249 1.10 cgd * ioctls on /dev/console, then the console is redirected
250 1.10 cgd * out from under it.
251 1.10 cgd */
252 1.86 riastrad if (!cn_redirect(&dev, 0, &error))
253 1.64 ad return error;
254 1.86 riastrad return cdev_ioctl(dev, cmd, data, flag, l);
255 1.1 cgd }
256 1.1 cgd
257 1.1 cgd /*ARGSUSED*/
258 1.7 andrew int
259 1.57 christos cnpoll(dev_t dev, int events, struct lwp *l)
260 1.1 cgd {
261 1.50 dsl int error;
262 1.21 mycroft
263 1.10 cgd /*
264 1.38 lukem * Redirect the poll, if that's appropriate.
265 1.10 cgd * I don't want to think of the possible side effects
266 1.10 cgd * of console redirection here.
267 1.10 cgd */
268 1.86 riastrad if (!cn_redirect(&dev, 0, &error))
269 1.56 ws return POLLHUP;
270 1.86 riastrad return cdev_poll(dev, events, l);
271 1.45 jdolecek }
272 1.45 jdolecek
273 1.45 jdolecek /*ARGSUSED*/
274 1.45 jdolecek int
275 1.46 matt cnkqfilter(dev_t dev, struct knote *kn)
276 1.45 jdolecek {
277 1.50 dsl int error;
278 1.45 jdolecek
279 1.45 jdolecek /*
280 1.45 jdolecek * Redirect the kqfilter, if that's appropriate.
281 1.45 jdolecek * I don't want to think of the possible side effects
282 1.45 jdolecek * of console redirection here.
283 1.45 jdolecek */
284 1.86 riastrad if (!cn_redirect(&dev, 0, &error))
285 1.50 dsl return error;
286 1.86 riastrad return cdev_kqfilter(dev, kn);
287 1.1 cgd }
288 1.1 cgd
289 1.7 andrew int
290 1.46 matt cngetc(void)
291 1.1 cgd {
292 1.1 cgd if (cn_tab == NULL)
293 1.1 cgd return (0);
294 1.70 matt int s = splhigh();
295 1.70 matt for (;;) {
296 1.70 matt const int rv = (*cn_tab->cn_getc)(cn_tab->cn_dev);
297 1.70 matt if (rv >= 0) {
298 1.70 matt splx(s);
299 1.70 matt return rv;
300 1.70 matt }
301 1.70 matt docritpollhooks();
302 1.70 matt }
303 1.36 itojun }
304 1.36 itojun
305 1.36 itojun int
306 1.46 matt cngetsn(char *cp, int size)
307 1.36 itojun {
308 1.36 itojun char *lp;
309 1.36 itojun int c, len;
310 1.36 itojun
311 1.36 itojun cnpollc(1);
312 1.36 itojun
313 1.36 itojun lp = cp;
314 1.36 itojun len = 0;
315 1.36 itojun for (;;) {
316 1.36 itojun c = cngetc();
317 1.36 itojun switch (c) {
318 1.36 itojun case '\n':
319 1.36 itojun case '\r':
320 1.36 itojun printf("\n");
321 1.36 itojun *lp++ = '\0';
322 1.36 itojun cnpollc(0);
323 1.36 itojun return (len);
324 1.36 itojun case '\b':
325 1.36 itojun case '\177':
326 1.36 itojun case '#':
327 1.36 itojun if (len) {
328 1.36 itojun --len;
329 1.36 itojun --lp;
330 1.36 itojun printf("\b \b");
331 1.36 itojun }
332 1.36 itojun continue;
333 1.36 itojun case '@':
334 1.40 jdolecek case 'u'&037: /* CTRL-u */
335 1.36 itojun len = 0;
336 1.36 itojun lp = cp;
337 1.36 itojun printf("\n");
338 1.36 itojun continue;
339 1.36 itojun default:
340 1.36 itojun if (len + 1 >= size || c < ' ') {
341 1.40 jdolecek printf("\007");
342 1.36 itojun continue;
343 1.36 itojun }
344 1.36 itojun printf("%c", c);
345 1.36 itojun ++len;
346 1.36 itojun *lp++ = c;
347 1.36 itojun }
348 1.36 itojun }
349 1.1 cgd }
350 1.1 cgd
351 1.29 christos void
352 1.46 matt cnputc(int c)
353 1.1 cgd {
354 1.21 mycroft
355 1.1 cgd if (cn_tab == NULL)
356 1.54 perry return;
357 1.29 christos
358 1.75 macallan /*
359 1.75 macallan * XXX
360 1.75 macallan * for some reason this causes ARCS firmware to output an endless stream of
361 1.75 macallan * whitespaces with n32 kernels, so use the pre-1.74 code for now until I can
362 1.75 macallan * figure out why this happens
363 1.75 macallan */
364 1.75 macallan #ifndef sgimips
365 1.1 cgd if (c) {
366 1.70 matt if (c == '\n') {
367 1.74 nakayama (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
368 1.70 matt docritpollhooks();
369 1.70 matt }
370 1.74 nakayama (*cn_tab->cn_putc)(cn_tab->cn_dev, c);
371 1.1 cgd }
372 1.75 macallan #else
373 1.75 macallan if (c) {
374 1.75 macallan (*cn_tab->cn_putc)(cn_tab->cn_dev, c);
375 1.75 macallan if (c == '\n') {
376 1.75 macallan docritpollhooks();
377 1.75 macallan (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
378 1.75 macallan }
379 1.75 macallan }
380 1.75 macallan #endif
381 1.19 mycroft }
382 1.19 mycroft
383 1.19 mycroft void
384 1.46 matt cnpollc(int on)
385 1.19 mycroft {
386 1.19 mycroft static int refcount = 0;
387 1.19 mycroft
388 1.19 mycroft if (cn_tab == NULL)
389 1.19 mycroft return;
390 1.19 mycroft if (!on)
391 1.19 mycroft --refcount;
392 1.19 mycroft if (refcount == 0)
393 1.19 mycroft (*cn_tab->cn_pollc)(cn_tab->cn_dev, on);
394 1.19 mycroft if (on)
395 1.19 mycroft ++refcount;
396 1.21 mycroft }
397 1.21 mycroft
398 1.21 mycroft void
399 1.61 christos nullcnpollc(dev_t dev, int on)
400 1.21 mycroft {
401 1.21 mycroft
402 1.34 thorpej }
403 1.34 thorpej
404 1.34 thorpej void
405 1.46 matt cnbell(u_int pitch, u_int period, u_int volume)
406 1.34 thorpej {
407 1.34 thorpej
408 1.34 thorpej if (cn_tab == NULL || cn_tab->cn_bell == NULL)
409 1.34 thorpej return;
410 1.34 thorpej (*cn_tab->cn_bell)(cn_tab->cn_dev, pitch, period, volume);
411 1.46 matt }
412 1.46 matt
413 1.46 matt void
414 1.46 matt cnflush(void)
415 1.46 matt {
416 1.46 matt if (cn_tab == NULL || cn_tab->cn_flush == NULL)
417 1.46 matt return;
418 1.46 matt (*cn_tab->cn_flush)(cn_tab->cn_dev);
419 1.46 matt }
420 1.54 perry
421 1.46 matt void
422 1.46 matt cnhalt(void)
423 1.46 matt {
424 1.46 matt if (cn_tab == NULL || cn_tab->cn_halt == NULL)
425 1.46 matt return;
426 1.46 matt (*cn_tab->cn_halt)(cn_tab->cn_dev);
427 1.50 dsl }
428 1.50 dsl
429 1.62 ad /*
430 1.62 ad * Redirect output, if that's appropriate. If there's no real console,
431 1.62 ad * return ENXIO.
432 1.86 riastrad *
433 1.86 riastrad * Call with tty_mutex held.
434 1.62 ad */
435 1.64 ad static bool
436 1.86 riastrad cn_redirect(dev_t *devp, int is_read, int *error)
437 1.50 dsl {
438 1.50 dsl dev_t dev = *devp;
439 1.50 dsl
440 1.50 dsl *error = ENXIO;
441 1.86 riastrad if (constty != NULL && minor(dev) == 0 &&
442 1.55 martin (cn_tab == NULL || (cn_tab->cn_pri != CN_REMOTE))) {
443 1.50 dsl if (is_read) {
444 1.50 dsl *error = 0;
445 1.86 riastrad return false;
446 1.50 dsl }
447 1.86 riastrad dev = constty->t_dev;
448 1.86 riastrad } else if (cn_tab == NULL)
449 1.86 riastrad return false;
450 1.86 riastrad else
451 1.50 dsl dev = cn_tab->cn_dev;
452 1.50 dsl *devp = dev;
453 1.86 riastrad return true;
454 1.2 cgd }
455 1.81 riastrad
456 1.81 riastrad MODULE(MODULE_CLASS_DRIVER, cons, NULL);
457 1.81 riastrad
458 1.81 riastrad static int
459 1.81 riastrad cons_modcmd(modcmd_t cmd, void *arg)
460 1.81 riastrad {
461 1.81 riastrad
462 1.81 riastrad switch (cmd) {
463 1.81 riastrad case MODULE_CMD_INIT:
464 1.81 riastrad mutex_init(&cn_lock, MUTEX_DEFAULT, IPL_NONE);
465 1.81 riastrad return 0;
466 1.81 riastrad case MODULE_CMD_FINI:
467 1.81 riastrad mutex_destroy(&cn_lock);
468 1.81 riastrad return 0;
469 1.81 riastrad default:
470 1.81 riastrad return ENOTTY;
471 1.81 riastrad }
472 1.81 riastrad }
473