xref: /titanic_44/usr/src/cmd/mdb/common/kmdb/kmdb_promio.c (revision 80148899834a4078a2bd348504aa2d6de9752837)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5c7bf3205Sjohnlev  * Common Development and Distribution License (the "License").
6c7bf3205Sjohnlev  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*80148899SSurya Prakki  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * PROM I/O backend
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <sys/param.h>
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/obpdefs.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <errno.h>
35c7bf3205Sjohnlev #include <ctype.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <mdb/mdb_modapi.h>
387c478bd9Sstevel@tonic-gate #include <mdb/mdb_io_impl.h>
397c478bd9Sstevel@tonic-gate #include <kmdb/kmdb_promif.h>
407c478bd9Sstevel@tonic-gate #include <kmdb/kmdb_io.h>
417c478bd9Sstevel@tonic-gate #include <mdb/mdb_debug.h>
427c478bd9Sstevel@tonic-gate #include <mdb/mdb_err.h>
437c478bd9Sstevel@tonic-gate #include <mdb/mdb.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #define	PIO_FL_TIO_READ	0x001
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate typedef struct pio_data {
487c478bd9Sstevel@tonic-gate 	char		pio_name[MAXPATHLEN];
497c478bd9Sstevel@tonic-gate 	ihandle_t	pio_fd;
507c478bd9Sstevel@tonic-gate 	uint_t		pio_flags;
517c478bd9Sstevel@tonic-gate 	struct termios	pio_ti;
527c478bd9Sstevel@tonic-gate } pio_data_t;
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate static pid_t pio_pgrp;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate static ssize_t
pio_read(mdb_io_t * io,void * buf,size_t nbytes)577c478bd9Sstevel@tonic-gate pio_read(mdb_io_t *io, void *buf, size_t nbytes)
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate 	pio_data_t *pdp = io->io_data;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	if (io->io_next == NULL)
627c478bd9Sstevel@tonic-gate 		return (kmdb_prom_read(buf, nbytes, &pdp->pio_ti));
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	return (IOP_READ(io->io_next, buf, nbytes));
657c478bd9Sstevel@tonic-gate }
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate static ssize_t
pio_write(mdb_io_t * io,const void * buf,size_t nbytes)687c478bd9Sstevel@tonic-gate pio_write(mdb_io_t *io, const void *buf, size_t nbytes)
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate 	pio_data_t *pdp = io->io_data;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	if (io->io_next == NULL)
737c478bd9Sstevel@tonic-gate 		return (kmdb_prom_write(buf, nbytes, &pdp->pio_ti));
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	return (IOP_WRITE(io->io_next, buf, nbytes));
767c478bd9Sstevel@tonic-gate }
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate static off64_t
pio_seek(mdb_io_t * io,off64_t offset,int whence)797c478bd9Sstevel@tonic-gate pio_seek(mdb_io_t *io, off64_t offset, int whence)
807c478bd9Sstevel@tonic-gate {
817c478bd9Sstevel@tonic-gate 	if (io->io_next == NULL)
827c478bd9Sstevel@tonic-gate 		return (set_errno(ENOTSUP));
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	return (IOP_SEEK(io->io_next, offset, whence));
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate static int
pio_ctl(mdb_io_t * io,int req,void * arg)887c478bd9Sstevel@tonic-gate pio_ctl(mdb_io_t *io, int req, void *arg)
897c478bd9Sstevel@tonic-gate {
907c478bd9Sstevel@tonic-gate 	pio_data_t *pdp = io->io_data;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	if (io->io_next != NULL)
937c478bd9Sstevel@tonic-gate 		return (IOP_CTL(io->io_next, req, arg));
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	switch (req) {
967c478bd9Sstevel@tonic-gate 	case TIOCGWINSZ:
977c478bd9Sstevel@tonic-gate 		return (kmdb_prom_term_ctl(TIOCGWINSZ, arg));
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	case TCGETS: {
1007c478bd9Sstevel@tonic-gate 		struct termios *ti = arg;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 		if (!(pdp->pio_flags & PIO_FL_TIO_READ)) {
1037c478bd9Sstevel@tonic-gate 			(void) kmdb_prom_term_ctl(TCGETS, &pdp->pio_ti);
1047c478bd9Sstevel@tonic-gate 			pdp->pio_flags |= PIO_FL_TIO_READ;
1057c478bd9Sstevel@tonic-gate 		}
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 		bcopy(&pdp->pio_ti, ti, sizeof (struct termios));
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 		mdb_dprintf(MDB_DBG_CMDBUF, "pio_ctl: gets: i: 0%o o: 0%o c: "
1107c478bd9Sstevel@tonic-gate 		    "0%o l: 0%o\n", ti->c_iflag, ti->c_oflag, ti->c_cflag,
1117c478bd9Sstevel@tonic-gate 		    ti->c_lflag);
1127c478bd9Sstevel@tonic-gate 		return (0);
1137c478bd9Sstevel@tonic-gate 	}
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	case TCSETSW: {
1167c478bd9Sstevel@tonic-gate 		struct termios *ti = arg;
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 		mdb_dprintf(MDB_DBG_CMDBUF, "pio_ctl: setsw: i: 0%o o: 0%o c: "
1197c478bd9Sstevel@tonic-gate 		    "0%o l: 0%o\n", ti->c_iflag, ti->c_oflag, ti->c_cflag,
1207c478bd9Sstevel@tonic-gate 		    ti->c_lflag);
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 		bcopy(ti, &pdp->pio_ti, sizeof (struct termios));
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 		return (0);
1257c478bd9Sstevel@tonic-gate 	}
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	case TIOCSPGRP:
1287c478bd9Sstevel@tonic-gate 		pio_pgrp = *(pid_t *)arg;
1297c478bd9Sstevel@tonic-gate 		mdb_dprintf(MDB_DBG_CMDBUF, "pio_ctl: spgrp: %ld\n",
1307c478bd9Sstevel@tonic-gate 		    (long)pio_pgrp);
1317c478bd9Sstevel@tonic-gate 		return (0);
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	case TIOCGPGRP:
1347c478bd9Sstevel@tonic-gate 		mdb_dprintf(MDB_DBG_CMDBUF, "pio_ctl: gpgrp: %ld\n",
1357c478bd9Sstevel@tonic-gate 		    (long)pio_pgrp);
1367c478bd9Sstevel@tonic-gate 		*(pid_t *)arg = pio_pgrp;
1377c478bd9Sstevel@tonic-gate 		return (0);
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	case MDB_IOC_CTTY:
1407c478bd9Sstevel@tonic-gate 		mdb_dprintf(MDB_DBG_CMDBUF, "pio_ctl: ignoring MDB_IOC_CTTY\n");
1417c478bd9Sstevel@tonic-gate 		return (0);
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	case MDB_IOC_GETFD:
1447c478bd9Sstevel@tonic-gate 		return (set_errno(ENOTSUP));
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	default:
1477c478bd9Sstevel@tonic-gate 		warn("Unknown ioctl %d\n", req);
1487c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
1497c478bd9Sstevel@tonic-gate 	}
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate void
pio_close(mdb_io_t * io)1537c478bd9Sstevel@tonic-gate pio_close(mdb_io_t *io)
1547c478bd9Sstevel@tonic-gate {
1557c478bd9Sstevel@tonic-gate 	pio_data_t *pdp = io->io_data;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	mdb_free(pdp, sizeof (pio_data_t));
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate static const char *
pio_name(mdb_io_t * io)1617c478bd9Sstevel@tonic-gate pio_name(mdb_io_t *io)
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate 	pio_data_t *pdp = io->io_data;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	if (io->io_next == NULL)
1667c478bd9Sstevel@tonic-gate 		return (pdp->pio_name);
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	return (IOP_NAME(io->io_next));
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate static const mdb_io_ops_t promio_ops = {
1727c478bd9Sstevel@tonic-gate 	pio_read,
1737c478bd9Sstevel@tonic-gate 	pio_write,
1747c478bd9Sstevel@tonic-gate 	pio_seek,
1757c478bd9Sstevel@tonic-gate 	pio_ctl,
1767c478bd9Sstevel@tonic-gate 	pio_close,
1777c478bd9Sstevel@tonic-gate 	pio_name,
1787c478bd9Sstevel@tonic-gate 	no_io_link,
1797c478bd9Sstevel@tonic-gate 	no_io_unlink,
1807c478bd9Sstevel@tonic-gate 	no_io_setattr,
1817c478bd9Sstevel@tonic-gate 	no_io_suspend,
1827c478bd9Sstevel@tonic-gate 	no_io_resume
1837c478bd9Sstevel@tonic-gate };
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate mdb_io_t *
kmdb_promio_create(char * name)1867c478bd9Sstevel@tonic-gate kmdb_promio_create(char *name)
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate 	mdb_io_t *io;
1897c478bd9Sstevel@tonic-gate 	pio_data_t *pdp;
1907c478bd9Sstevel@tonic-gate 	ihandle_t hdl = kmdb_prom_get_handle(name);
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	if (hdl == -1)
1937c478bd9Sstevel@tonic-gate 		return (NULL);
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	io = mdb_zalloc(sizeof (mdb_io_t), UM_SLEEP);
1967c478bd9Sstevel@tonic-gate 	pdp = mdb_zalloc(sizeof (pio_data_t), UM_SLEEP);
1977c478bd9Sstevel@tonic-gate 
198*80148899SSurya Prakki 	(void) strlcpy(pdp->pio_name, name, MAXPATHLEN);
1997c478bd9Sstevel@tonic-gate 	pdp->pio_fd = hdl;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate #ifdef __sparc
2027c478bd9Sstevel@tonic-gate 	pdp->pio_ti.c_oflag |= ONLCR;
2037c478bd9Sstevel@tonic-gate 	pdp->pio_ti.c_iflag |= ICRNL;
2047c478bd9Sstevel@tonic-gate #endif
2057c478bd9Sstevel@tonic-gate 	pdp->pio_ti.c_lflag |= ECHO;
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	io->io_data = pdp;
2087c478bd9Sstevel@tonic-gate 	io->io_ops = &promio_ops;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	return (io);
2117c478bd9Sstevel@tonic-gate }
212c7bf3205Sjohnlev 
213c7bf3205Sjohnlev char
kmdb_getchar(void)214c7bf3205Sjohnlev kmdb_getchar(void)
215c7bf3205Sjohnlev {
216c7bf3205Sjohnlev 	char c;
217c7bf3205Sjohnlev 
218c7bf3205Sjohnlev 	while (IOP_READ(mdb.m_term, &c, 1) != 1)
219c7bf3205Sjohnlev 		continue;
220c7bf3205Sjohnlev 	if (isprint(c) && c != '\n')
221c7bf3205Sjohnlev 		mdb_iob_printf(mdb.m_out, "%c", c);
222c7bf3205Sjohnlev 	mdb_iob_printf(mdb.m_out, "\n");
223c7bf3205Sjohnlev 
224c7bf3205Sjohnlev 	return (c);
225c7bf3205Sjohnlev }
226