dcons_os.c revision 136680
1/*
2 * Copyright (C) 2003,2004
3 * 	Hidetoshi Shimokawa. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *
16 *	This product includes software developed by Hidetoshi Shimokawa.
17 *
18 * 4. Neither the name of the author nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * $FreeBSD: head/sys/dev/dcons/dcons_os.c 136680 2004-10-18 21:51:27Z phk $
35 */
36
37#include <sys/param.h>
38#if __FreeBSD_version >= 502122
39#include <sys/kdb.h>
40#include <gdb/gdb.h>
41#endif
42#include <sys/kernel.h>
43#include <sys/module.h>
44#include <sys/systm.h>
45#include <sys/types.h>
46#include <sys/conf.h>
47#include <sys/cons.h>
48#include <sys/consio.h>
49#include <sys/tty.h>
50#include <sys/malloc.h>
51#include <sys/proc.h>
52#include <sys/ucred.h>
53
54#include <machine/bus.h>
55
56#ifdef __DragonFly__
57#include "dcons.h"
58#include "dcons_os.h"
59#else
60#include <dev/dcons/dcons.h>
61#include <dev/dcons/dcons_os.h>
62#endif
63
64#include <ddb/ddb.h>
65#include <sys/reboot.h>
66
67#include <sys/sysctl.h>
68
69#include <vm/vm.h>
70#include <vm/vm_param.h>
71#include <vm/pmap.h>
72
73#include "opt_ddb.h"
74#include "opt_comconsole.h"
75#include "opt_dcons.h"
76
77#ifndef DCONS_POLL_HZ
78#define DCONS_POLL_HZ	100
79#endif
80
81#ifndef DCONS_BUF_SIZE
82#define DCONS_BUF_SIZE (16*1024)
83#endif
84
85#ifndef DCONS_FORCE_CONSOLE
86#define DCONS_FORCE_CONSOLE	0	/* Mostly for FreeBSD-4/DragonFly */
87#endif
88
89#ifndef DCONS_FORCE_GDB
90#define DCONS_FORCE_GDB	1
91#endif
92
93#if __FreeBSD_version >= 500101
94#define CONS_NODEV	1
95#if __FreeBSD_version < 502122
96static struct consdev gdbconsdev;
97#endif
98#endif
99
100static d_open_t		dcons_open;
101static d_close_t	dcons_close;
102#if defined(__DragonFly__) || __FreeBSD_version < 500104
103static d_ioctl_t	dcons_ioctl;
104#endif
105
106static struct cdevsw dcons_cdevsw = {
107#ifdef __DragonFly__
108#define CDEV_MAJOR      184
109	"dcons", CDEV_MAJOR, D_TTY, NULL, 0,
110	dcons_open, dcons_close, ttyread, ttywrite, dcons_ioctl,
111	ttypoll, nommap, nostrategy, nodump, nopsize,
112#elif __FreeBSD_version >= 500104
113	.d_version =	D_VERSION,
114	.d_open =	dcons_open,
115	.d_close =	dcons_close,
116	.d_name =	"dcons",
117	.d_flags =	D_TTY | D_NEEDGIANT,
118#else
119#define CDEV_MAJOR      184
120	/* open */	dcons_open,
121	/* close */	dcons_close,
122	/* read */	ttyread,
123	/* write */	ttywrite,
124	/* ioctl */	dcons_ioctl,
125	/* poll */	ttypoll,
126	/* mmap */	nommap,
127	/* strategy */	nostrategy,
128	/* name */	"dcons",
129	/* major */	CDEV_MAJOR,
130	/* dump */	nodump,
131	/* psize */	nopsize,
132	/* flags */	D_TTY,
133#endif
134};
135
136#ifndef KLD_MODULE
137static char bssbuf[DCONS_BUF_SIZE];	/* buf in bss */
138#endif
139
140/* global data */
141static struct dcons_global dg;
142struct dcons_global *dcons_conf;
143static int poll_hz = DCONS_POLL_HZ;
144
145static struct dcons_softc sc[DCONS_NPORT];
146
147SYSCTL_NODE(_kern, OID_AUTO, dcons, CTLFLAG_RD, 0, "Dumb Console");
148SYSCTL_INT(_kern_dcons, OID_AUTO, poll_hz, CTLFLAG_RW, &poll_hz, 0,
149				"dcons polling rate");
150
151static int drv_init = 0;
152static struct callout dcons_callout;
153struct dcons_buf *dcons_buf;		/* for local dconschat */
154
155#ifdef __DragonFly__
156#define DEV	dev_t
157#define THREAD	d_thread_t
158#elif __FreeBSD_version < 500000
159#define DEV	dev_t
160#define THREAD	struct proc
161#else
162#define DEV	struct cdev *
163#define THREAD	struct thread
164#endif
165
166
167static void	dcons_tty_start(struct tty *);
168static int	dcons_tty_param(struct tty *, struct termios *);
169static void	dcons_timeout(void *);
170static int	dcons_drv_init(int);
171
172static cn_probe_t	dcons_cnprobe;
173static cn_init_t	dcons_cninit;
174static cn_getc_t	dcons_cngetc;
175static cn_checkc_t 	dcons_cncheckc;
176static cn_putc_t	dcons_cnputc;
177
178CONS_DRIVER(dcons, dcons_cnprobe, dcons_cninit, NULL, dcons_cngetc,
179    dcons_cncheckc, dcons_cnputc, NULL);
180
181#if __FreeBSD_version >= 502122
182static gdb_probe_f dcons_dbg_probe;
183static gdb_init_f dcons_dbg_init;
184static gdb_term_f dcons_dbg_term;
185static gdb_getc_f dcons_dbg_getc;
186static gdb_checkc_f dcons_dbg_checkc;
187static gdb_putc_f dcons_dbg_putc;
188
189GDB_DBGPORT(dcons, dcons_dbg_probe, dcons_dbg_init, dcons_dbg_term,
190    dcons_dbg_checkc, dcons_dbg_getc, dcons_dbg_putc);
191
192extern struct gdb_dbgport *gdb_cur;
193#endif
194
195#if (KDB || DDB) && ALT_BREAK_TO_DEBUGGER
196static int
197dcons_check_break(struct dcons_softc *dc, int c)
198{
199	if (c < 0)
200		return (c);
201
202#if __FreeBSD_version >= 502122
203	if (kdb_alt_break(c, &dc->brk_state)) {
204		if ((dc->flags & DC_GDB) != 0) {
205			if (gdb_cur == &dcons_gdb_dbgport) {
206				kdb_dbbe_select("gdb");
207				breakpoint();
208			}
209		} else
210			breakpoint();
211	}
212#else
213	switch (dc->brk_state) {
214	case STATE1:
215		if (c == KEY_TILDE)
216			dc->brk_state = STATE2;
217		else
218			dc->brk_state = STATE0;
219		break;
220	case STATE2:
221		dc->brk_state = STATE0;
222		if (c == KEY_CTRLB) {
223#if DCONS_FORCE_GDB
224			if (dc->flags & DC_GDB)
225				boothowto |= RB_GDB;
226#endif
227			breakpoint();
228		}
229	}
230	if (c == KEY_CR)
231		dc->brk_state = STATE1;
232#endif
233	return (c);
234}
235#else
236#define	dcons_check_break(dc, c)	(c)
237#endif
238
239static int
240dcons_os_checkc(struct dcons_softc *dc)
241{
242	int c;
243
244	if (dg.dma_tag != NULL)
245		bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_POSTREAD);
246
247	c = dcons_check_break(dc, dcons_checkc(dc));
248
249	if (dg.dma_tag != NULL)
250		bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_PREREAD);
251
252	return (c);
253}
254
255static int
256dcons_os_getc(struct dcons_softc *dc)
257{
258	int c;
259
260	while ((c = dcons_os_checkc(dc)) == -1);
261
262	return (c & 0xff);
263}
264
265static void
266dcons_os_putc(struct dcons_softc *dc, int c)
267{
268	if (dg.dma_tag != NULL)
269		bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_POSTWRITE);
270
271	dcons_putc(dc, c);
272
273	if (dg.dma_tag != NULL)
274		bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_PREWRITE);
275}
276static int
277dcons_open(DEV dev, int flag, int mode, THREAD *td)
278{
279	struct tty *tp;
280	int unit, error, s;
281
282	unit = minor(dev);
283	if (unit != 0)
284		return (ENXIO);
285
286	tp = dev->si_tty = ttymalloc(dev->si_tty);
287	tp->t_oproc = dcons_tty_start;
288	tp->t_param = dcons_tty_param;
289	tp->t_stop = nottystop;
290	tp->t_dev = dev;
291
292	error = 0;
293
294	s = spltty();
295	if ((tp->t_state & TS_ISOPEN) == 0) {
296		tp->t_state |= TS_CARR_ON;
297		ttyconsolemode(tp, 0);
298		ttsetwater(tp);
299	} else if ((tp->t_state & TS_XCLUDE) && suser(td)) {
300		splx(s);
301		return (EBUSY);
302	}
303	splx(s);
304
305#if __FreeBSD_version < 502113
306	error = (*linesw[tp->t_line].l_open)(dev, tp);
307#else
308	error = ttyld_open(tp, dev);
309#endif
310
311	return (error);
312}
313
314static int
315dcons_close(DEV dev, int flag, int mode, THREAD *td)
316{
317	int	unit;
318	struct	tty *tp;
319
320	unit = minor(dev);
321	if (unit != 0)
322		return (ENXIO);
323
324	tp = dev->si_tty;
325	if (tp->t_state & TS_ISOPEN) {
326#if __FreeBSD_version < 502113
327		(*linesw[tp->t_line].l_close)(tp, flag);
328		ttyclose(tp);
329#else
330		ttyld_close(tp, flag);
331		tty_close(tp);
332#endif
333	}
334
335	return (0);
336}
337
338#if defined(__DragonFly__) || __FreeBSD_version < 500104
339static int
340dcons_ioctl(DEV dev, u_long cmd, caddr_t data, int flag, THREAD *td)
341{
342	int	unit;
343	struct	tty *tp;
344	int	error;
345
346	unit = minor(dev);
347	if (unit != 0)
348		return (ENXIO);
349
350	tp = dev->si_tty;
351	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, td);
352	if (error != ENOIOCTL)
353		return (error);
354
355	error = ttioctl(tp, cmd, data, flag);
356	if (error != ENOIOCTL)
357		return (error);
358
359	return (ENOTTY);
360}
361#endif
362
363static int
364dcons_tty_param(struct tty *tp, struct termios *t)
365{
366	tp->t_ispeed = t->c_ispeed;
367	tp->t_ospeed = t->c_ospeed;
368	tp->t_cflag = t->c_cflag;
369	return 0;
370}
371
372static void
373dcons_tty_start(struct tty *tp)
374{
375	struct dcons_softc *dc;
376	int s;
377
378	dc = (struct dcons_softc *)tp->t_dev->si_drv1;
379	s = spltty();
380	if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) {
381		ttwwakeup(tp);
382		return;
383	}
384
385	tp->t_state |= TS_BUSY;
386	while (tp->t_outq.c_cc != 0)
387		dcons_os_putc(dc, getc(&tp->t_outq));
388	tp->t_state &= ~TS_BUSY;
389
390	ttwwakeup(tp);
391	splx(s);
392}
393
394static void
395dcons_timeout(void *v)
396{
397	struct	tty *tp;
398	struct dcons_softc *dc;
399	int i, c, polltime;
400
401	for (i = 0; i < DCONS_NPORT; i ++) {
402		dc = &sc[i];
403		tp = ((DEV)dc->dev)->si_tty;
404		while ((c = dcons_os_checkc(dc)) != -1)
405			if (tp->t_state & TS_ISOPEN)
406#if __FreeBSD_version < 502113
407				(*linesw[tp->t_line].l_rint)(c, tp);
408#else
409				ttyld_rint(tp, c);
410#endif
411	}
412	polltime = hz / poll_hz;
413	if (polltime < 1)
414		polltime = 1;
415	callout_reset(&dcons_callout, polltime, dcons_timeout, tp);
416}
417
418static void
419dcons_cnprobe(struct consdev *cp)
420{
421#ifdef __DragonFly__
422	cp->cn_dev = make_dev(&dcons_cdevsw, DCONS_CON,
423	    UID_ROOT, GID_WHEEL, 0600, "dcons");
424#elif __FreeBSD_version >= 501109
425	sprintf(cp->cn_name, "dcons");
426#else
427	cp->cn_dev = makedev(CDEV_MAJOR, DCONS_CON);
428#endif
429#if DCONS_FORCE_CONSOLE
430	cp->cn_pri = CN_REMOTE;
431#else
432	cp->cn_pri = CN_NORMAL;
433#endif
434}
435
436static void
437dcons_cninit(struct consdev *cp)
438{
439	dcons_drv_init(0);
440#if CONS_NODEV
441	cp->cn_arg
442#else
443	cp->cn_dev->si_drv1
444#endif
445		= (void *)&sc[DCONS_CON]; /* share port0 with unit0 */
446}
447
448#if CONS_NODEV
449static int
450dcons_cngetc(struct consdev *cp)
451{
452	struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg;
453	return (dcons_os_getc(dc));
454}
455static int
456dcons_cncheckc(struct consdev *cp)
457{
458	struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg;
459	return (dcons_os_checkc(dc));
460}
461static void
462dcons_cnputc(struct consdev *cp, int c)
463{
464	struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg;
465	dcons_os_putc(dc, c);
466}
467#else
468static int
469dcons_cngetc(DEV dev)
470{
471	struct dcons_softc *dc = (struct dcons_softc *)dev->si_drv1;
472	return (dcons_os_getc(dc));
473}
474static int
475dcons_cncheckc(DEV dev)
476{
477	struct dcons_softc *dc = (struct dcons_softc *)dev->si_drv1;
478	return (dcons_os_checkc(dc));
479}
480static void
481dcons_cnputc(DEV dev, int c)
482{
483	struct dcons_softc *dc = (struct dcons_softc *)dev->si_drv1;
484	dcons_os_putc(dc, c);
485}
486#endif
487
488static int
489dcons_drv_init(int stage)
490{
491#ifdef __i386__
492	int addr, size;
493#endif
494
495	if (drv_init)
496		return(drv_init);
497
498	drv_init = -1;
499
500	bzero(&dg, sizeof(dg));
501	dcons_conf = &dg;
502	dg.cdev = &dcons_consdev;
503	dg.buf = NULL;
504	dg.size = DCONS_BUF_SIZE;
505
506#ifdef __i386__
507	if (getenv_int("dcons.addr", &addr) > 0 &&
508	    getenv_int("dcons.size", &size) > 0) {
509		/* XXX P to V */
510		dg.buf = (struct dcons_buf *)(KERNBASE + addr);
511		dg.size = size;
512		if (dcons_load_buffer(dg.buf, dg.size, sc) < 0)
513			dg.buf = NULL;
514	}
515#endif
516	if (dg.buf != NULL)
517		goto ok;
518
519#ifndef KLD_MODULE
520	if (stage == 0) { /* XXX or cold */
521		/*
522		 * DCONS_FORCE_CONSOLE == 1 and statically linked.
523		 * called from cninit(). can't use contigmalloc yet .
524		 */
525		dg.buf = (struct dcons_buf *) bssbuf;
526		dcons_init(dg.buf, dg.size, sc);
527	} else
528#endif
529	{
530		/*
531		 * DCONS_FORCE_CONSOLE == 0 or kernel module case.
532		 * if the module is loaded after boot,
533		 * bssbuf could be non-continuous.
534		 */
535		dg.buf = (struct dcons_buf *) contigmalloc(dg.size,
536			M_DEVBUF, 0, 0x10000, 0xffffffff, PAGE_SIZE, 0ul);
537		dcons_init(dg.buf, dg.size, sc);
538	}
539
540ok:
541	dcons_buf = dg.buf;
542
543#if __FreeBSD_version < 502122
544#if DDB && DCONS_FORCE_GDB
545#if CONS_NODEV
546	gdbconsdev.cn_arg = (void *)&sc[DCONS_GDB];
547#if __FreeBSD_version >= 501109
548	sprintf(gdbconsdev.cn_name, "dgdb");
549#endif
550	gdb_arg = &gdbconsdev;
551#elif defined(__DragonFly__)
552	gdbdev = make_dev(&dcons_cdevsw, DCONS_GDB,
553	    UID_ROOT, GID_WHEEL, 0600, "dgdb");
554#else
555	gdbdev = makedev(CDEV_MAJOR, DCONS_GDB);
556#endif
557	gdb_getc = dcons_cngetc;
558	gdb_putc = dcons_cnputc;
559#endif
560#endif
561	drv_init = 1;
562
563	return 0;
564}
565
566
567static int
568dcons_attach_port(int port, char *name, int flags)
569{
570	struct dcons_softc *dc;
571	struct tty *tp;
572	DEV dev;
573
574	dc = &sc[port];
575	dc->flags = flags;
576	dev = make_dev(&dcons_cdevsw, port,
577			UID_ROOT, GID_WHEEL, 0600, name);
578	dc->dev = (void *)dev;
579	tp = ttymalloc(NULL);
580
581	dev->si_drv1 = (void *)dc;
582	dev->si_tty = tp;
583
584	tp->t_oproc = dcons_tty_start;
585	tp->t_param = dcons_tty_param;
586	tp->t_stop = nottystop;
587	tp->t_dev = dc->dev;
588
589	return(0);
590}
591
592static int
593dcons_attach(void)
594{
595	int polltime;
596
597#ifdef __DragonFly__
598	cdevsw_add(&dcons_cdevsw, -1, 0);
599#endif
600	dcons_attach_port(DCONS_CON, "dcons", 0);
601	dcons_attach_port(DCONS_GDB, "dgdb", DC_GDB);
602#if __FreeBSD_version < 500000
603	callout_init(&dcons_callout);
604#else
605	callout_init(&dcons_callout, 0);
606#endif
607	polltime = hz / poll_hz;
608	if (polltime < 1)
609		polltime = 1;
610	callout_reset(&dcons_callout, polltime, dcons_timeout, NULL);
611	return(0);
612}
613
614static int
615dcons_detach(int port)
616{
617	struct	tty *tp;
618	struct dcons_softc *dc;
619
620	dc = &sc[port];
621
622	tp = ((DEV)dc->dev)->si_tty;
623
624	if (tp->t_state & TS_ISOPEN) {
625		printf("dcons: still opened\n");
626#if __FreeBSD_version < 502113
627		(*linesw[tp->t_line].l_close)(tp, 0);
628		tp->t_gen++;
629		ttyclose(tp);
630		ttwakeup(tp);
631		ttwwakeup(tp);
632#else
633		ttyld_close(tp, 0);
634		tty_close(tp);
635#endif
636	}
637	/* XXX
638	 * must wait until all device are closed.
639	 */
640#ifdef __DragonFly__
641	tsleep((void *)dc, 0, "dcodtc", hz/4);
642#else
643	tsleep((void *)dc, PWAIT, "dcodtc", hz/4);
644#endif
645	destroy_dev(dc->dev);
646
647	return(0);
648}
649
650
651/* cnXXX works only for FreeBSD-5 */
652static int
653dcons_modevent(module_t mode, int type, void *data)
654{
655	int err = 0, ret;
656
657	switch (type) {
658	case MOD_LOAD:
659		ret = dcons_drv_init(1);
660		dcons_attach();
661#if __FreeBSD_version >= 500000
662		if (ret == 0) {
663			dcons_cnprobe(&dcons_consdev);
664			dcons_cninit(&dcons_consdev);
665			cnadd(&dcons_consdev);
666		}
667#endif
668		break;
669	case MOD_UNLOAD:
670		printf("dcons: unload\n");
671		callout_stop(&dcons_callout);
672#if __FreeBSD_version < 502122
673#if DDB && DCONS_FORCE_GDB
674#if CONS_NODEV
675		gdb_arg = NULL;
676#else
677		gdbdev = NULL;
678#endif
679#endif
680#endif
681#if __FreeBSD_version >= 500000
682		cnremove(&dcons_consdev);
683#endif
684		dcons_detach(DCONS_CON);
685		dcons_detach(DCONS_GDB);
686		dg.buf->magic = 0;
687
688		contigfree(dg.buf, DCONS_BUF_SIZE, M_DEVBUF);
689
690		break;
691	case MOD_SHUTDOWN:
692		dg.buf->magic = 0;
693		break;
694	default:
695		err = EOPNOTSUPP;
696		break;
697	}
698	return(err);
699}
700
701#if __FreeBSD_version >= 502122
702/* Debugger interface */
703
704static int
705dcons_dbg_probe(void)
706{
707	return(DCONS_FORCE_GDB);
708}
709
710static void
711dcons_dbg_init(void)
712{
713}
714
715static void
716dcons_dbg_term(void)
717{
718}
719
720static void
721dcons_dbg_putc(int c)
722{
723	struct dcons_softc *dc = &sc[DCONS_GDB];
724	dcons_os_putc(dc, c);
725}
726
727static int
728dcons_dbg_checkc(void)
729{
730	struct dcons_softc *dc = &sc[DCONS_GDB];
731	return (dcons_os_checkc(dc));
732}
733
734static int
735dcons_dbg_getc(void)
736{
737	struct dcons_softc *dc = &sc[DCONS_GDB];
738	return (dcons_os_getc(dc));
739}
740#endif
741
742DEV_MODULE(dcons, dcons_modevent, NULL);
743MODULE_VERSION(dcons, DCONS_VERSION);
744