2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * @OSF_FREE_COPYRIGHT@
28 /* Intercept mach console output and supply it to a user application */
33 #include <device/buf.h>
34 #include <device/conf.h>
35 #include <device/errno.h>
36 #include <device/misc_protos.h>
37 #include <device/ds_routines.h>
38 #include <device/cirbuf.h>
39 #include <ppc/console_feed_entries.h>
40 #include <ppc/serial_io.h>
43 #include <ppc/db_machdep.h>
46 static struct cirbuf cons_feed_cb
;
47 static int cons_feed_count
= 0;
48 io_req_t cons_feed_queued
= 0;
50 /* console feed lock should be taken at splhigh */
51 decl_simple_lock_data(,cons_feed_lock
)
53 boolean_t
cons_feed_read_done(io_req_t ior
);
63 simple_lock_init(&cons_feed_lock
, 0);
65 if (console_is_serial()) {
69 cb_alloc(&cons_feed_cb
, CONSOLE_FEED_BUFSIZE
);
71 simple_lock(&cons_feed_lock
);
73 simple_unlock(&cons_feed_lock
);
85 simple_lock(&cons_feed_lock
);
87 simple_unlock(&cons_feed_lock
);
90 console_feed_cancel_and_flush();
91 cb_free(&cons_feed_cb
);
96 /* A routine that can be called from a panic or other problem
97 * situation. It switches off the console feed and dumps any
98 * remaining buffered information to the original console
99 * (usually the screen). It doesn't free up the buffer, since
100 * it tries to be as minimal as possible
103 void console_feed_cancel_and_flush(void)
110 if (console_is_serial()) {
113 #endif /* MACH_KDB */
116 simple_lock(&cons_feed_lock
);
117 if (cons_feed_count
== 0) {
118 simple_unlock(&cons_feed_lock
);
123 simple_unlock(&cons_feed_lock
);
127 c
= getc(&cons_feed_cb
);
132 #endif /* NCONSFEED > 0 */
144 rc
= device_read_alloc(ior
, (vm_size_t
) ior
->io_count
);
145 if (rc
!= KERN_SUCCESS
)
149 simple_lock(&cons_feed_lock
);
151 ior
->io_residual
= ior
->io_count
;
153 count
= q_to_b(&cons_feed_cb
, (char *) ior
->io_data
, ior
->io_count
);
155 if (ior
->io_mode
& D_NOWAIT
) {
158 if (cons_feed_queued
== NULL
) {
159 ior
->io_done
= cons_feed_read_done
;
160 cons_feed_queued
= ior
;
163 /* Can't queue multiple read requests yet */
164 rc
= D_INVALID_OPERATION
;
166 simple_unlock(&cons_feed_lock
);
171 simple_unlock(&cons_feed_lock
);
174 ior
->io_residual
-= count
;
178 if (ior
->io_op
& IO_SYNC
) {
185 /* Called when data is ready and there's a queued-up read waiting */
186 boolean_t
cons_feed_read_done(io_req_t ior
)
192 simple_lock(&cons_feed_lock
);
194 count
= q_to_b(&cons_feed_cb
, (char *) ior
->io_data
, ior
->io_count
);
196 if (cons_feed_queued
== NULL
) {
197 ior
->io_done
= cons_feed_read_done
;
198 cons_feed_queued
= ior
;
200 simple_unlock(&cons_feed_lock
);
205 simple_unlock(&cons_feed_lock
);
208 ior
->io_residual
-= count
;
214 /* This routine is called from putc() - it should return TRUE if
215 * the character should be passed on to a physical console, FALSE
216 * if the feed has intercepted the character. It may be called from
217 * under interrupt (even splhigh)
220 boolean_t
console_feed_putc(char c
)
230 #endif /* MACH_KDB */
232 retval
=TRUE
; /* TRUE : character should be displayed now */
233 if (!cons_feed_count
) {
237 simple_lock(&cons_feed_lock
);
238 if (!cons_feed_count
) {
239 simple_unlock(&cons_feed_lock
);
243 /* queue up the data if we can */
244 if (!putc(c
, &cons_feed_cb
)) {
245 /* able to stock the character */
248 if (cons_feed_queued
!= NULL
) {
249 /* Queued up request - service it */
250 ior
= cons_feed_queued
;
251 cons_feed_queued
= NULL
;
252 simple_unlock(&cons_feed_lock
);
257 simple_unlock(&cons_feed_lock
);