4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #include <sys/types.h>
28 #include <sys/termios.h>
29 #include <sys/promif.h>
31 #include <sys/promif_impl.h>
37 #include <kmdb/kmdb_promif_impl.h>
38 #include <kmdb/kmdb_kdi.h>
39 #include <kmdb/kmdb_dpi.h>
40 #include <mdb/mdb_debug.h>
41 #include <mdb/mdb_err.h>
42 #include <mdb/mdb_frame.h>
43 #include <mdb/mdb_string.h>
46 #define KMDB_PROM_DEF_CONS_MODE "9600,n,1,-,-"
48 #define KMDB_PROM_READBUF_SIZE 1024
50 static char kmdb_prom_readbuf
[KMDB_PROM_READBUF_SIZE
];
51 static int kmdb_prom_readbuf_head
;
52 static int kmdb_prom_readbuf_tail
;
55 kmdb_prom_getchar(int wait
)
57 struct cons_polledio
*pio
= mdb
.m_pio
;
62 if (pio
== NULL
|| pio
->cons_polledio_getchar
== NULL
) {
64 while ((c
= prom_mayget()) == -1) {
71 ischar
= (uintptr_t)pio
->cons_polledio_ischar
;
72 getchar
= (uintptr_t)pio
->cons_polledio_getchar
;
73 arg
= (uintptr_t)pio
->cons_polledio_argument
;
75 if (!wait
&& ischar
!= 0 && !kmdb_dpi_call(ischar
, 1, &arg
))
78 return ((int)kmdb_dpi_call(getchar
, 1, &arg
));
82 kmdb_prom_polled_write(caddr_t buf
, size_t len
)
87 args
[0] = (uintptr_t)mdb
.m_pio
->cons_polledio_argument
;
89 for (i
= 0; i
< len
; i
++) {
92 (uintptr_t)mdb
.m_pio
->cons_polledio_putchar
, 2, args
);
99 kmdb_prom_reader(caddr_t buf
, size_t len
, int wait
)
104 while (nread
< len
) {
105 if ((c
= kmdb_prom_getchar(wait
)) == -1)
117 kmdb_prom_writer(caddr_t buf
, size_t len
)
119 if (mdb
.m_pio
!= NULL
&& mdb
.m_pio
->cons_polledio_putchar
!= NULL
)
120 return (kmdb_prom_polled_write(buf
, len
));
122 return (kmdb_prom_obp_writer(buf
, len
));
126 * Due to the nature of kmdb, we don't have signals. This prevents us from
127 * receiving asynchronous notification when the user would like to abort active
128 * dcmds. Whereas mdb can simply declare a SIGINT handler, we must
129 * occasionally poll the input stream, looking for pending ^C characters. To
130 * give the illusion of asynchronous interrupt delivery, this polling is
131 * triggered from several commonly-used functions, such as kmdb_prom_write and
132 * the *read and *write target ops. When an interrupt check is triggered, we
133 * read through pending input, looking for interrupt characters. If we find
134 * one, we deliver an interrupt immediately.
136 * In a read context, we can deliver the interrupt character directly back to
137 * the termio handler rather than raising an interrupt.
139 * OBP doesn't have an "unget" facility. Any character read for interrupt
140 * checking is gone forever, unless we save it. Loss of these characters
141 * would prevent us from supporting typeahead. We like typeahead, so we're
142 * going to save characters gathered during interrupt checking. As with
143 * ungetc(3c), however, we can only store a finite number of characters in
144 * our typeahead buffer. Characters read beyond that will be silently dropped
145 * after they undergo interrupt processing.
147 * The typeahead facility is implemented as a ring buffer, stored in
151 kmdb_prom_drain_readbuf(void *buf
, size_t len
)
156 * If head > tail, life is easy - we can simply read as much as we need
159 if (kmdb_prom_readbuf_head
> kmdb_prom_readbuf_tail
) {
160 n
= MIN(kmdb_prom_readbuf_head
- kmdb_prom_readbuf_tail
, len
);
161 bcopy(kmdb_prom_readbuf
+ kmdb_prom_readbuf_tail
, buf
, n
);
162 kmdb_prom_readbuf_tail
+= n
;
165 } else if (kmdb_prom_readbuf_head
== kmdb_prom_readbuf_tail
) {
170 * The consumable slots wrap around zero (there are slots from tail to
171 * zero, and from zero to head). We have to read them in two parts.
173 n
= MIN(KMDB_PROM_READBUF_SIZE
- kmdb_prom_readbuf_tail
, len
);
174 bcopy(kmdb_prom_readbuf
+ kmdb_prom_readbuf_tail
, buf
, n
);
175 kmdb_prom_readbuf_tail
= (kmdb_prom_readbuf_tail
+ n
) %
176 KMDB_PROM_READBUF_SIZE
;
180 * We filled the passed buffer from the first part, so there's
181 * no need to read the second.
188 n
= MIN(kmdb_prom_readbuf_head
, len
- tailread
);
189 buf
= (void *)((uintptr_t)buf
+ tailread
);
190 bcopy(kmdb_prom_readbuf
, buf
, n
);
192 kmdb_prom_readbuf_tail
= (kmdb_prom_readbuf_tail
+ n
) %
193 KMDB_PROM_READBUF_SIZE
;
195 return (tailread
+ n
);
199 check_int(char *buf
, size_t len
)
203 for (i
= 0; i
< len
; i
++) {
204 if (buf
[i
] == CTRL('c')) {
205 kmdb_prom_readbuf_tail
= kmdb_prom_readbuf_head
;
207 longjmp(mdb
.m_frame
->f_pcb
, MDB_ERR_SIGINT
);
215 * Attempt to refill the ring buffer from the input stream. This called from
218 * Direct read: read the input into our buffer until input is exhausted, or the
221 * Interrupt check: called 'asynchronously' from the normal read routines; read
222 * the input into our buffer until it is exhausted, discarding input if the
223 * buffer is full. In this case we look ahead for any interrupt characters,
224 * delivering an interrupt directly if we find one.
227 kmdb_prom_fill_readbuf(int check_for_int
, int wait
)
229 int oldhead
, left
, n
;
232 * Calculate the number of slots left before we wrap around to the
235 left
= KMDB_PROM_READBUF_SIZE
- kmdb_prom_readbuf_head
;
236 if (kmdb_prom_readbuf_tail
== 0)
239 if (kmdb_prom_readbuf_head
== kmdb_prom_readbuf_tail
||
240 (kmdb_prom_readbuf_head
> kmdb_prom_readbuf_tail
&& left
> 0)) {
242 * head > tail, so we have to read in two parts - the slots
243 * from head until we wrap back around to zero, and the ones
244 * from zero to tail. We handle the first part here, and let
245 * the common code handle the second.
247 if ((n
= kmdb_prom_reader(kmdb_prom_readbuf
+
248 kmdb_prom_readbuf_head
, left
, wait
)) <= 0)
251 oldhead
= kmdb_prom_readbuf_head
;
252 kmdb_prom_readbuf_head
= (kmdb_prom_readbuf_head
+ n
) %
253 KMDB_PROM_READBUF_SIZE
;
256 check_int(kmdb_prom_readbuf
+ oldhead
, n
);
262 left
= kmdb_prom_readbuf_tail
- kmdb_prom_readbuf_head
- 1;
264 if ((n
= kmdb_prom_reader(kmdb_prom_readbuf
+
265 kmdb_prom_readbuf_head
, left
, wait
)) <= 0)
268 oldhead
= kmdb_prom_readbuf_head
;
269 kmdb_prom_readbuf_head
= (kmdb_prom_readbuf_head
+ n
) %
270 KMDB_PROM_READBUF_SIZE
;
273 check_int(kmdb_prom_readbuf
+ oldhead
, n
);
282 while (kmdb_prom_reader(&c
, 1, 0) == 1)
288 kmdb_prom_check_interrupt(void)
290 kmdb_prom_fill_readbuf(1, 0);
294 * OBP reads are always non-blocking. If there are characters available,
295 * we'll return as many as we can. If nothing is available, we'll spin
296 * until one shows up.
299 kmdb_prom_read(void *buf
, size_t len
, struct termios
*tio
)
303 char *c
= (char *)buf
;
307 kmdb_prom_fill_readbuf(0, wait
);
308 thisread
= kmdb_prom_drain_readbuf(c
, len
);
313 /* wait until something shows up */
320 * We're done if we've exhausted available input or if we've
321 * filled the provided buffer.
323 if (len
== 0 || thisread
== 0)
327 if (tio
->c_iflag
& ICRNL
) {
331 for (i
= 0; i
< totread
; i
++) {
337 if (tio
->c_lflag
& ECHO
)
338 (void) kmdb_prom_write(buf
, totread
, tio
);
345 kmdb_prom_write(const void *bufp
, size_t len
, struct termios
*tio
)
347 caddr_t buf
= (caddr_t
)bufp
;
352 kmdb_prom_check_interrupt();
354 if (!(tio
->c_oflag
& ONLCR
))
355 return (kmdb_prom_writer(buf
, left
));
357 /* translate every \n into \r\n */
358 while ((c
= strnchr(buf
, '\n', left
)) != NULL
) {
360 size_t sz
= (size_t)(c
- buf
);
361 (void) kmdb_prom_writer(buf
, sz
);
368 (void) kmdb_prom_writer(nl
, 2);
372 (void) kmdb_prom_writer(buf
, left
);
378 kmdb_get_ttyio_mode(kmdb_auxv_t
*kav
, char *devname
)
380 char *modepname
, *modepval
;
382 modepname
= mdb_alloc(strlen(devname
) + 5 + 1, UM_SLEEP
);
383 (void) strcpy(modepname
, devname
);
384 (void) strcat(modepname
, "-mode");
386 modepval
= kmdb_prom_get_ddi_prop(kav
, modepname
);
394 termios_setispeed(struct termios
*tip
, speed_t s
)
396 if (s
> (2 * CBAUD
+ 1))
399 if ((s
<< IBSHIFT
) > CIBAUD
) {
400 tip
->c_cflag
|= CIBAUDEXT
;
401 s
-= ((CIBAUD
>> IBSHIFT
) + 1);
403 tip
->c_cflag
&= ~CIBAUDEXT
;
405 tip
->c_cflag
= (tip
->c_cflag
& ~CIBAUD
) | ((s
<< IBSHIFT
) & CIBAUD
);
411 termios_setospeed(struct termios
*tip
, speed_t s
)
413 if (s
> (2 * CBAUD
+ 1))
417 tip
->c_cflag
|= CBAUDEXT
;
420 tip
->c_cflag
&= ~CBAUDEXT
;
422 tip
->c_cflag
= (tip
->c_cflag
& ~CBAUD
) | (s
& CBAUD
);
428 kmdb_parse_mode(const char *mode
, struct termios
*tip
, int in
)
430 static const uint_t baudmap
[] = {
431 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200,
432 1800, 2400, 4800, 9600, 19200, 38400, 57600,
433 76800, 115200, 153600, 230400, 307200, 460800, 921600
435 static const uint_t bitsmap
[] = { CS6
, CS6
, CS7
, CS8
};
436 char *m
= strdup(mode
);
443 * termios supports different baud rates and flow control types for
444 * input and output, but it requires character width, parity, and stop
445 * bits to be equal in input and output. obp allows them to be
446 * different, but we're going to (silently) assume that nobody will use
450 /* baud rate - see baudmap above */
451 if ((w
= strtok(m
, ",")) == NULL
)
452 goto parse_mode_bail
;
454 baud
= strtol(w
, NULL
, 10);
456 for (i
= 0; i
< sizeof (baudmap
) / sizeof (baudmap
[0]); i
++) {
457 if (baudmap
[i
] == baud
) {
463 goto parse_mode_bail
;
466 (void) termios_setispeed(tip
, speed
);
468 (void) termios_setospeed(tip
, speed
);
470 /* character width (bits) - 5, 6, 7, or 8 */
471 if ((w
= strtok(NULL
, ",")) == NULL
|| strlen(w
) != 1 || *w
< '5' ||
473 goto parse_mode_bail
;
474 tip
->c_cflag
= (tip
->c_cflag
& ~CSIZE
) | bitsmap
[*w
- '5'];
476 /* parity - `n' (none), `e' (even), or `o' (odd) */
477 if ((w
= strtok(NULL
, ",")) == NULL
|| strlen(w
) != 1 ||
478 strchr("neo", *w
) == NULL
)
479 goto parse_mode_bail
;
481 tip
->c_cflag
= (tip
->c_cflag
& ~(PARENB
|PARODD
));
487 tip
->c_cflag
|= PARENB
;
490 tip
->c_cflag
|= PARENB
|PARODD
;
495 * stop bits - 1, or 2. obp can, in theory, support 1.5 bits,
496 * but we can't. how many angels can dance on half of a bit?
498 if ((w
= strtok(NULL
, ",")) == NULL
|| strlen(w
) != 1 || *w
< '1' ||
500 goto parse_mode_bail
;
503 tip
->c_cflag
&= ~CSTOPB
;
505 tip
->c_cflag
|= CSTOPB
;
507 /* flow control - `-' (none), `h' (h/w), or `s' (s/w - XON/XOFF) */
508 if ((w
= strtok(NULL
, ",")) == NULL
|| strlen(w
) != 1 ||
509 strchr("-hs", *w
) == NULL
)
510 goto parse_mode_bail
;
512 tip
->c_cflag
&= ~(CRTSXOFF
|CRTSCTS
);
513 tip
->c_iflag
&= ~(IXON
|IXANY
|IXOFF
);
517 tip
->c_cflag
|= (in
== 1 ? CRTSXOFF
: CRTSCTS
);
521 tip
->c_iflag
|= (in
== 1 ? IXOFF
: IXON
);
533 #define ATTACHED_TERM_TYPE "sun-color"
536 kmdb_prom_term_init(kmdb_auxv_t
*kav
, kmdb_promif_t
*pif
)
538 const char ccs
[NCCS
] = { 0x03, 0x1c, 0x08, 0x15, 0x04, 0x00, 0x00,
539 0x00, 0x11, 0x13, 0x1a, 0x19, 0x12, 0x0f, 0x17, 0x16 };
540 char *conin
= NULL
, *conout
= NULL
;
542 if (kmdb_prom_stdout_is_framebuffer(kav
))
543 pif
->pif_oterm
= ATTACHED_TERM_TYPE
;
545 bzero(&pif
->pif_tios
, sizeof (struct termios
));
547 /* output device characteristics */
548 if ((conout
= kmdb_prom_get_ddi_prop(kav
, "output-device")) ==
549 NULL
|| strcmp(conout
, "screen") == 0) {
550 (void) kmdb_parse_mode(KMDB_PROM_DEF_CONS_MODE
,
552 } else if (*conout
== '/') {
554 * We're not going to be able to get characteristics for a
555 * device that's specified as a path, so don't even try.
556 * Conveniently, this allows us to avoid chattering on
559 (void) kmdb_parse_mode(KMDB_PROM_DEF_CONS_MODE
,
562 char *mode
= kmdb_get_ttyio_mode(kav
, conout
);
565 if (mode
== NULL
|| kmdb_parse_mode(mode
,
566 &pif
->pif_tios
, 0) < 0) {
568 * Either we couldn't retrieve the
569 * characteristics for this console, or they
570 * weren't parseable. The console hasn't been
571 * set up yet, so we can't warn. We'll have to
572 * silently fall back to the default
575 (void) kmdb_parse_mode(KMDB_PROM_DEF_CONS_MODE
,
581 kmdb_prom_free_ddi_prop(mode
);
584 /* input device characteristics */
585 if ((conin
= kmdb_prom_get_ddi_prop(kav
, "input-device")) == NULL
||
586 strcmp(conin
, "keyboard") == 0) {
587 (void) kmdb_parse_mode(KMDB_PROM_DEF_CONS_MODE
,
589 } else if (*conin
== '/') {
590 /* See similar case in output-device above */
591 (void) kmdb_parse_mode(KMDB_PROM_DEF_CONS_MODE
,
594 char *mode
= kmdb_get_ttyio_mode(kav
, conin
);
597 if (mode
== NULL
|| kmdb_parse_mode(mode
,
598 &pif
->pif_tios
, 1) < 0) {
600 * Either we couldn't retrieve the
601 * characteristics for this console, or they
602 * weren't parseable. The console hasn't been
603 * set up yet, so we can't warn. We'll have to
604 * silently fall back to the default
607 (void) kmdb_parse_mode(KMDB_PROM_DEF_CONS_MODE
,
613 kmdb_prom_free_ddi_prop(mode
);
616 /* various characteristics of the prom read/write interface */
617 pif
->pif_tios
.c_iflag
|= ICRNL
;
618 pif
->pif_tios
.c_lflag
|= ECHO
;
619 bcopy(ccs
, &pif
->pif_tios
.c_cc
, sizeof (ccs
));
622 kmdb_prom_free_ddi_prop(conin
);
624 kmdb_prom_free_ddi_prop(conout
);
628 kmdb_prom_term_type(void)
630 return (mdb
.m_promif
->pif_oterm
);
634 kmdb_prom_term_ctl(int req
, void *arg
)
638 struct termios
*ti
= arg
;
639 bcopy(&mdb
.m_promif
->pif_tios
, ti
, sizeof (struct termios
));
644 * When kmdb is used over a serial console, we have no idea how
645 * large the terminal window is. When we're invoked on a local
646 * console, however, we do, and need to share that information
647 * with the debugger in order to contradict potentially
648 * incorrect sizing information retrieved from the terminfo
649 * database. One specific case where this happens is with the
650 * Intel console, which is 80x25. The terminfo entry for
651 * sun-color -- the default terminal type for local Intel
652 * consoles -- was cloned from sun, which has a height of 34
655 if (mdb
.m_promif
->pif_oterm
!= NULL
) {
656 struct winsize
*wsz
= arg
;
657 wsz
->ws_row
= KMDB_PIF_WINSIZE_ROWS
;
658 wsz
->ws_col
= KMDB_PIF_WINSIZE_COLS
;
659 wsz
->ws_xpixel
= wsz
->ws_ypixel
= 0;
663 return (set_errno(ENOTSUP
));
665 return (set_errno(EINVAL
));
670 kmdb_prom_vtop(uintptr_t virt
, physaddr_t
*pap
)
673 int rc
= kmdb_kdi_vtop(virt
, &pa
);
676 if (rc
== 0 && pap
!= NULL
)
683 kmdb_prom_debugger_entry(void)
686 * While kmdb_prom_debugger_entry and kmdb_prom_debugger_exit are not
687 * guaranteed to be called an identical number of times (an intentional
688 * debugger fault will cause an additional entry call without a matching
689 * exit call), we must ensure that the polled I/O entry and exit calls
692 if (mdb
.m_pio
== NULL
) {
693 mdb
.m_pio
= kmdb_kdi_get_polled_io();
695 if (mdb
.m_pio
!= NULL
&&
696 mdb
.m_pio
->cons_polledio_enter
!= NULL
) {
697 (void) kmdb_dpi_call(
698 (uintptr_t)mdb
.m_pio
->cons_polledio_enter
, 1,
699 (uintptr_t *)&mdb
.m_pio
->cons_polledio_argument
);
705 kmdb_prom_debugger_exit(void)
707 if (mdb
.m_pio
!= NULL
&& mdb
.m_pio
->cons_polledio_exit
!= NULL
) {
708 (void) kmdb_dpi_call((uintptr_t)mdb
.m_pio
->cons_polledio_exit
,
709 1, (uintptr_t *)&mdb
.m_pio
->cons_polledio_argument
);
716 * The prom_* files use ASSERT, which is #defined as assfail(). We need to
717 * redirect that to our assert function. This is also used by the various STAND
721 kmdb_prom_assfail(const char *assertion
, const char *file
, int line
)
723 (void) mdb_dassert(assertion
, file
, line
);
729 * Begin the initialization of the debugger/PROM interface. Initialization is
730 * performed in two steps due to interlocking dependencies between promif and
731 * both the memory allocator and mdb_create. The first phase is performed
732 * before either of the others have been initialized, and thus must neither
733 * attempt to allocate memory nor access/write to `mdb'.
736 kmdb_prom_init_begin(char *pgmname
, kmdb_auxv_t
*kav
)
739 if (kav
->kav_domaining
)
740 kmdb_prom_init_promif(pgmname
, kav
);
742 prom_init(pgmname
, kav
->kav_romp
);
744 prom_init(pgmname
, kav
->kav_romp
);
747 /* Initialize the interrupt ring buffer */
748 kmdb_prom_readbuf_head
= kmdb_prom_readbuf_tail
;
750 #if defined(__i386) || defined(__amd64)
751 kmdb_sysp
= kav
->kav_romp
;
757 kmdb_prom_init_promif(char *pgmname
, kmdb_auxv_t
*kav
)
759 ASSERT(kav
->kav_domaining
);
760 cif_init(pgmname
, kav
->kav_promif_root
,
761 kav
->kav_promif_in
, kav
->kav_promif_out
,
762 kav
->kav_promif_pin
, kav
->kav_promif_pout
,
763 kav
->kav_promif_chosennode
, kav
->kav_promif_optionsnode
);
768 * Conclude the initialization of the debugger/PROM interface. Memory
769 * allocation and the global `mdb' object are now available.
772 kmdb_prom_init_finish(kmdb_auxv_t
*kav
)
774 mdb
.m_promif
= mdb_zalloc(sizeof (kmdb_promif_t
), UM_SLEEP
);
775 kmdb_prom_term_init(kav
, mdb
.m_promif
);