xref: /illumos-gate/usr/src/cmd/mdb/common/kmdb/kmdb_promio.c (revision 7c478bd95313f5f23a4c958a745db2134aa0324)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * PROM I/O backend
31*7c478bd9Sstevel@tonic-gate  */
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/obpdefs.h>
36*7c478bd9Sstevel@tonic-gate #include <string.h>
37*7c478bd9Sstevel@tonic-gate #include <errno.h>
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #include <mdb/mdb_modapi.h>
40*7c478bd9Sstevel@tonic-gate #include <mdb/mdb_io_impl.h>
41*7c478bd9Sstevel@tonic-gate #include <kmdb/kmdb_promif.h>
42*7c478bd9Sstevel@tonic-gate #include <kmdb/kmdb_io.h>
43*7c478bd9Sstevel@tonic-gate #include <mdb/mdb_debug.h>
44*7c478bd9Sstevel@tonic-gate #include <mdb/mdb_err.h>
45*7c478bd9Sstevel@tonic-gate #include <mdb/mdb.h>
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate #define	PIO_FL_TIO_READ	0x001
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate typedef struct pio_data {
50*7c478bd9Sstevel@tonic-gate 	char		pio_name[MAXPATHLEN];
51*7c478bd9Sstevel@tonic-gate 	ihandle_t	pio_fd;
52*7c478bd9Sstevel@tonic-gate 	uint_t		pio_flags;
53*7c478bd9Sstevel@tonic-gate 	struct termios	pio_ti;
54*7c478bd9Sstevel@tonic-gate } pio_data_t;
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate static pid_t pio_pgrp;
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate static ssize_t
59*7c478bd9Sstevel@tonic-gate pio_read(mdb_io_t *io, void *buf, size_t nbytes)
60*7c478bd9Sstevel@tonic-gate {
61*7c478bd9Sstevel@tonic-gate 	pio_data_t *pdp = io->io_data;
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate 	if (io->io_next == NULL)
64*7c478bd9Sstevel@tonic-gate 		return (kmdb_prom_read(buf, nbytes, &pdp->pio_ti));
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate 	return (IOP_READ(io->io_next, buf, nbytes));
67*7c478bd9Sstevel@tonic-gate }
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate static ssize_t
70*7c478bd9Sstevel@tonic-gate pio_write(mdb_io_t *io, const void *buf, size_t nbytes)
71*7c478bd9Sstevel@tonic-gate {
72*7c478bd9Sstevel@tonic-gate 	pio_data_t *pdp = io->io_data;
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate 	if (io->io_next == NULL)
75*7c478bd9Sstevel@tonic-gate 		return (kmdb_prom_write(buf, nbytes, &pdp->pio_ti));
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	return (IOP_WRITE(io->io_next, buf, nbytes));
78*7c478bd9Sstevel@tonic-gate }
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate static off64_t
81*7c478bd9Sstevel@tonic-gate pio_seek(mdb_io_t *io, off64_t offset, int whence)
82*7c478bd9Sstevel@tonic-gate {
83*7c478bd9Sstevel@tonic-gate 	if (io->io_next == NULL)
84*7c478bd9Sstevel@tonic-gate 		return (set_errno(ENOTSUP));
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	return (IOP_SEEK(io->io_next, offset, whence));
87*7c478bd9Sstevel@tonic-gate }
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate static int
90*7c478bd9Sstevel@tonic-gate pio_ctl(mdb_io_t *io, int req, void *arg)
91*7c478bd9Sstevel@tonic-gate {
92*7c478bd9Sstevel@tonic-gate 	pio_data_t *pdp = io->io_data;
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate 	if (io->io_next != NULL)
95*7c478bd9Sstevel@tonic-gate 		return (IOP_CTL(io->io_next, req, arg));
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 	switch (req) {
98*7c478bd9Sstevel@tonic-gate 	case TIOCGWINSZ:
99*7c478bd9Sstevel@tonic-gate 		return (kmdb_prom_term_ctl(TIOCGWINSZ, arg));
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	case TCGETS: {
102*7c478bd9Sstevel@tonic-gate 		struct termios *ti = arg;
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 		if (!(pdp->pio_flags & PIO_FL_TIO_READ)) {
105*7c478bd9Sstevel@tonic-gate 			(void) kmdb_prom_term_ctl(TCGETS, &pdp->pio_ti);
106*7c478bd9Sstevel@tonic-gate 			pdp->pio_flags |= PIO_FL_TIO_READ;
107*7c478bd9Sstevel@tonic-gate 		}
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 		bcopy(&pdp->pio_ti, ti, sizeof (struct termios));
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 		mdb_dprintf(MDB_DBG_CMDBUF, "pio_ctl: gets: i: 0%o o: 0%o c: "
112*7c478bd9Sstevel@tonic-gate 		    "0%o l: 0%o\n", ti->c_iflag, ti->c_oflag, ti->c_cflag,
113*7c478bd9Sstevel@tonic-gate 		    ti->c_lflag);
114*7c478bd9Sstevel@tonic-gate 		return (0);
115*7c478bd9Sstevel@tonic-gate 	}
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 	case TCSETSW: {
118*7c478bd9Sstevel@tonic-gate 		struct termios *ti = arg;
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 		mdb_dprintf(MDB_DBG_CMDBUF, "pio_ctl: setsw: i: 0%o o: 0%o c: "
121*7c478bd9Sstevel@tonic-gate 		    "0%o l: 0%o\n", ti->c_iflag, ti->c_oflag, ti->c_cflag,
122*7c478bd9Sstevel@tonic-gate 		    ti->c_lflag);
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate 		bcopy(ti, &pdp->pio_ti, sizeof (struct termios));
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 		return (0);
127*7c478bd9Sstevel@tonic-gate 	}
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate 	case TIOCSPGRP:
130*7c478bd9Sstevel@tonic-gate 		pio_pgrp = *(pid_t *)arg;
131*7c478bd9Sstevel@tonic-gate 		mdb_dprintf(MDB_DBG_CMDBUF, "pio_ctl: spgrp: %ld\n",
132*7c478bd9Sstevel@tonic-gate 		    (long)pio_pgrp);
133*7c478bd9Sstevel@tonic-gate 		return (0);
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	case TIOCGPGRP:
136*7c478bd9Sstevel@tonic-gate 		mdb_dprintf(MDB_DBG_CMDBUF, "pio_ctl: gpgrp: %ld\n",
137*7c478bd9Sstevel@tonic-gate 		    (long)pio_pgrp);
138*7c478bd9Sstevel@tonic-gate 		*(pid_t *)arg = pio_pgrp;
139*7c478bd9Sstevel@tonic-gate 		return (0);
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 	case MDB_IOC_CTTY:
142*7c478bd9Sstevel@tonic-gate 		mdb_dprintf(MDB_DBG_CMDBUF, "pio_ctl: ignoring MDB_IOC_CTTY\n");
143*7c478bd9Sstevel@tonic-gate 		return (0);
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 	case MDB_IOC_GETFD:
146*7c478bd9Sstevel@tonic-gate 		return (set_errno(ENOTSUP));
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 	default:
149*7c478bd9Sstevel@tonic-gate 		warn("Unknown ioctl %d\n", req);
150*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
151*7c478bd9Sstevel@tonic-gate 	}
152*7c478bd9Sstevel@tonic-gate }
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate void
155*7c478bd9Sstevel@tonic-gate pio_close(mdb_io_t *io)
156*7c478bd9Sstevel@tonic-gate {
157*7c478bd9Sstevel@tonic-gate 	pio_data_t *pdp = io->io_data;
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate 	mdb_free(pdp, sizeof (pio_data_t));
160*7c478bd9Sstevel@tonic-gate }
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate static const char *
163*7c478bd9Sstevel@tonic-gate pio_name(mdb_io_t *io)
164*7c478bd9Sstevel@tonic-gate {
165*7c478bd9Sstevel@tonic-gate 	pio_data_t *pdp = io->io_data;
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate 	if (io->io_next == NULL)
168*7c478bd9Sstevel@tonic-gate 		return (pdp->pio_name);
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 	return (IOP_NAME(io->io_next));
171*7c478bd9Sstevel@tonic-gate }
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate static const mdb_io_ops_t promio_ops = {
174*7c478bd9Sstevel@tonic-gate 	pio_read,
175*7c478bd9Sstevel@tonic-gate 	pio_write,
176*7c478bd9Sstevel@tonic-gate 	pio_seek,
177*7c478bd9Sstevel@tonic-gate 	pio_ctl,
178*7c478bd9Sstevel@tonic-gate 	pio_close,
179*7c478bd9Sstevel@tonic-gate 	pio_name,
180*7c478bd9Sstevel@tonic-gate 	no_io_link,
181*7c478bd9Sstevel@tonic-gate 	no_io_unlink,
182*7c478bd9Sstevel@tonic-gate 	no_io_setattr,
183*7c478bd9Sstevel@tonic-gate 	no_io_suspend,
184*7c478bd9Sstevel@tonic-gate 	no_io_resume
185*7c478bd9Sstevel@tonic-gate };
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate mdb_io_t *
188*7c478bd9Sstevel@tonic-gate kmdb_promio_create(char *name)
189*7c478bd9Sstevel@tonic-gate {
190*7c478bd9Sstevel@tonic-gate 	mdb_io_t *io;
191*7c478bd9Sstevel@tonic-gate 	pio_data_t *pdp;
192*7c478bd9Sstevel@tonic-gate 	ihandle_t hdl = kmdb_prom_get_handle(name);
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 	if (hdl == -1)
195*7c478bd9Sstevel@tonic-gate 		return (NULL);
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 	io = mdb_zalloc(sizeof (mdb_io_t), UM_SLEEP);
198*7c478bd9Sstevel@tonic-gate 	pdp = mdb_zalloc(sizeof (pio_data_t), UM_SLEEP);
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate 	strlcpy(pdp->pio_name, name, MAXPATHLEN);
201*7c478bd9Sstevel@tonic-gate 	pdp->pio_fd = hdl;
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate #ifdef __sparc
204*7c478bd9Sstevel@tonic-gate 	pdp->pio_ti.c_oflag |= ONLCR;
205*7c478bd9Sstevel@tonic-gate 	pdp->pio_ti.c_iflag |= ICRNL;
206*7c478bd9Sstevel@tonic-gate #endif
207*7c478bd9Sstevel@tonic-gate 	pdp->pio_ti.c_lflag |= ECHO;
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate 	io->io_data = pdp;
210*7c478bd9Sstevel@tonic-gate 	io->io_ops = &promio_ops;
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate 	return (io);
213*7c478bd9Sstevel@tonic-gate }
214