2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * @OSF_FREE_COPYRIGHT@
35 /* Intercept mach console output and supply it to a user application */
40 #include <device/buf.h>
41 #include <device/conf.h>
42 #include <device/errno.h>
43 #include <device/misc_protos.h>
44 #include <device/ds_routines.h>
45 #include <device/cirbuf.h>
46 #include <ppc/console_feed_entries.h>
47 #include <ppc/serial_io.h>
50 #include <ppc/db_machdep.h>
53 static struct cirbuf cons_feed_cb
;
54 static int cons_feed_count
= 0;
55 io_req_t cons_feed_queued
= 0;
57 /* console feed lock should be taken at splhigh */
58 decl_simple_lock_data(,cons_feed_lock
)
60 boolean_t
cons_feed_read_done(io_req_t ior
);
70 simple_lock_init(&cons_feed_lock
, 0);
72 if (console_is_serial()) {
76 cb_alloc(&cons_feed_cb
, CONSOLE_FEED_BUFSIZE
);
78 simple_lock(&cons_feed_lock
);
80 simple_unlock(&cons_feed_lock
);
92 simple_lock(&cons_feed_lock
);
94 simple_unlock(&cons_feed_lock
);
97 console_feed_cancel_and_flush();
98 cb_free(&cons_feed_cb
);
103 /* A routine that can be called from a panic or other problem
104 * situation. It switches off the console feed and dumps any
105 * remaining buffered information to the original console
106 * (usually the screen). It doesn't free up the buffer, since
107 * it tries to be as minimal as possible
110 void console_feed_cancel_and_flush(void)
117 if (console_is_serial()) {
120 #endif /* MACH_KDB */
123 simple_lock(&cons_feed_lock
);
124 if (cons_feed_count
== 0) {
125 simple_unlock(&cons_feed_lock
);
130 simple_unlock(&cons_feed_lock
);
134 c
= getc(&cons_feed_cb
);
139 #endif /* NCONSFEED > 0 */
151 rc
= device_read_alloc(ior
, (vm_size_t
) ior
->io_count
);
152 if (rc
!= KERN_SUCCESS
)
156 simple_lock(&cons_feed_lock
);
158 ior
->io_residual
= ior
->io_count
;
160 count
= q_to_b(&cons_feed_cb
, (char *) ior
->io_data
, ior
->io_count
);
162 if (ior
->io_mode
& D_NOWAIT
) {
165 if (cons_feed_queued
== NULL
) {
166 ior
->io_done
= cons_feed_read_done
;
167 cons_feed_queued
= ior
;
170 /* Can't queue multiple read requests yet */
171 rc
= D_INVALID_OPERATION
;
173 simple_unlock(&cons_feed_lock
);
178 simple_unlock(&cons_feed_lock
);
181 ior
->io_residual
-= count
;
185 if (ior
->io_op
& IO_SYNC
) {
192 /* Called when data is ready and there's a queued-up read waiting */
193 boolean_t
cons_feed_read_done(io_req_t ior
)
199 simple_lock(&cons_feed_lock
);
201 count
= q_to_b(&cons_feed_cb
, (char *) ior
->io_data
, ior
->io_count
);
203 if (cons_feed_queued
== NULL
) {
204 ior
->io_done
= cons_feed_read_done
;
205 cons_feed_queued
= ior
;
207 simple_unlock(&cons_feed_lock
);
212 simple_unlock(&cons_feed_lock
);
215 ior
->io_residual
-= count
;
221 /* This routine is called from putc() - it should return TRUE if
222 * the character should be passed on to a physical console, FALSE
223 * if the feed has intercepted the character. It may be called from
224 * under interrupt (even splhigh)
227 boolean_t
console_feed_putc(char c
)
237 #endif /* MACH_KDB */
239 retval
=TRUE
; /* TRUE : character should be displayed now */
240 if (!cons_feed_count
) {
244 simple_lock(&cons_feed_lock
);
245 if (!cons_feed_count
) {
246 simple_unlock(&cons_feed_lock
);
250 /* queue up the data if we can */
251 if (!putc(c
, &cons_feed_cb
)) {
252 /* able to stock the character */
255 if (cons_feed_queued
!= NULL
) {
256 /* Queued up request - service it */
257 ior
= cons_feed_queued
;
258 cons_feed_queued
= NULL
;
259 simple_unlock(&cons_feed_lock
);
264 simple_unlock(&cons_feed_lock
);