2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * @OSF_FREE_COPYRIGHT@
30 /* Intercept mach console output and supply it to a user application */
35 #include <device/buf.h>
36 #include <device/conf.h>
37 #include <device/errno.h>
38 #include <device/misc_protos.h>
39 #include <device/ds_routines.h>
40 #include <device/cirbuf.h>
41 #include <ppc/console_feed_entries.h>
42 #include <ppc/serial_io.h>
45 #include <ppc/db_machdep.h>
48 static struct cirbuf cons_feed_cb
;
49 static int cons_feed_count
= 0;
50 io_req_t cons_feed_queued
= 0;
52 /* console feed lock should be taken at splhigh */
53 decl_simple_lock_data(,cons_feed_lock
)
55 boolean_t
cons_feed_read_done(io_req_t ior
);
65 simple_lock_init(&cons_feed_lock
, ETAP_IO_TTY
);
67 if (console_is_serial()) {
71 cb_alloc(&cons_feed_cb
, CONSOLE_FEED_BUFSIZE
);
73 simple_lock(&cons_feed_lock
);
75 simple_unlock(&cons_feed_lock
);
87 simple_lock(&cons_feed_lock
);
89 simple_unlock(&cons_feed_lock
);
92 console_feed_cancel_and_flush();
93 cb_free(&cons_feed_cb
);
98 /* A routine that can be called from a panic or other problem
99 * situation. It switches off the console feed and dumps any
100 * remaining buffered information to the original console
101 * (usually the screen). It doesn't free up the buffer, since
102 * it tries to be as minimal as possible
105 void console_feed_cancel_and_flush(void)
112 if (console_is_serial()) {
115 #endif /* MACH_KDB */
118 simple_lock(&cons_feed_lock
);
119 if (cons_feed_count
== 0) {
120 simple_unlock(&cons_feed_lock
);
125 simple_unlock(&cons_feed_lock
);
129 c
= getc(&cons_feed_cb
);
134 #endif /* NCONSFEED > 0 */
146 rc
= device_read_alloc(ior
, (vm_size_t
) ior
->io_count
);
147 if (rc
!= KERN_SUCCESS
)
151 simple_lock(&cons_feed_lock
);
153 ior
->io_residual
= ior
->io_count
;
155 count
= q_to_b(&cons_feed_cb
, (char *) ior
->io_data
, ior
->io_count
);
157 if (ior
->io_mode
& D_NOWAIT
) {
160 if (cons_feed_queued
== NULL
) {
161 ior
->io_done
= cons_feed_read_done
;
162 cons_feed_queued
= ior
;
165 /* Can't queue multiple read requests yet */
166 rc
= D_INVALID_OPERATION
;
168 simple_unlock(&cons_feed_lock
);
173 simple_unlock(&cons_feed_lock
);
176 ior
->io_residual
-= count
;
180 if (ior
->io_op
& IO_SYNC
) {
187 /* Called when data is ready and there's a queued-up read waiting */
188 boolean_t
cons_feed_read_done(io_req_t ior
)
194 simple_lock(&cons_feed_lock
);
196 count
= q_to_b(&cons_feed_cb
, (char *) ior
->io_data
, ior
->io_count
);
198 if (cons_feed_queued
== NULL
) {
199 ior
->io_done
= cons_feed_read_done
;
200 cons_feed_queued
= ior
;
202 simple_unlock(&cons_feed_lock
);
207 simple_unlock(&cons_feed_lock
);
210 ior
->io_residual
-= count
;
216 /* This routine is called from putc() - it should return TRUE if
217 * the character should be passed on to a physical console, FALSE
218 * if the feed has intercepted the character. It may be called from
219 * under interrupt (even splhigh)
222 boolean_t
console_feed_putc(char c
)
232 #endif /* MACH_KDB */
234 retval
=TRUE
; /* TRUE : character should be displayed now */
235 if (!cons_feed_count
) {
239 simple_lock(&cons_feed_lock
);
240 if (!cons_feed_count
) {
241 simple_unlock(&cons_feed_lock
);
245 /* queue up the data if we can */
246 if (!putc(c
, &cons_feed_cb
)) {
247 /* able to stock the character */
250 if (cons_feed_queued
!= NULL
) {
251 /* Queued up request - service it */
252 ior
= cons_feed_queued
;
253 cons_feed_queued
= NULL
;
254 simple_unlock(&cons_feed_lock
);
259 simple_unlock(&cons_feed_lock
);