]> git.saurik.com Git - apple/xnu.git/blame - osfmk/console/i386/serial_console.c
xnu-792.24.17.tar.gz
[apple/xnu.git] / osfmk / console / i386 / serial_console.c
CommitLineData
1c79356b 1/*
55e303ae 2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
1c79356b 3 *
6601e61a 4 * @APPLE_LICENSE_HEADER_START@
1c79356b 5 *
6601e61a
A
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
8f6c56a5 11 *
6601e61a
A
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
6601e61a
A
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
8f6c56a5 19 *
6601e61a 20 * @APPLE_LICENSE_HEADER_END@
1c79356b 21 */
1c79356b 22
55e303ae 23#include <i386/mp.h>
91447636
A
24#include <i386/cpu_data.h>
25#include <i386/machine_cpu.h>
55e303ae 26#include <i386/machine_routines.h>
91447636
A
27#include <i386/misc_protos.h>
28#include <vm/vm_kern.h>
29#include <console/video_console.h>
30#include <kern/kalloc.h>
31
32static struct {
33 char *buffer;
34 int len;
35 int used;
36 char *write_ptr;
37 char *read_ptr;
38 decl_simple_lock_data(,read_lock);
39 decl_simple_lock_data(,write_lock);
40} console_ring;
41
42typedef struct console_buf {
43 char *buf_base;
44 char *buf_end;
45 char *buf_ptr;
46#define CPU_BUFFER_LEN (256 - 3*(sizeof(char*)))
47 char buf[CPU_BUFFER_LEN];
48} console_buf_t;
1c79356b 49
55e303ae 50void
91447636
A
51console_init(void)
52{
53 int ret;
54
55 console_ring.len = PAGE_SIZE;
56 ret = kmem_alloc(kernel_map, (vm_offset_t *) &console_ring.buffer,
57 console_ring.len);
58 if (ret != KERN_SUCCESS)
59 panic("console_ring_init() "
60 "failed to allocate ring buffer, error %d\n", ret);
61 console_ring.used = 0;
62 console_ring.read_ptr = console_ring.buffer;
63 console_ring.write_ptr = console_ring.buffer;
64 simple_lock_init(&console_ring.read_lock, 0);
65 simple_lock_init(&console_ring.write_lock, 0);
6601e61a 66
91447636
A
67}
68
69void *
70console_cpu_alloc(__unused boolean_t boot_processor)
71{
72 int ret;
73 console_buf_t *cbp;
74
75 ret = kmem_alloc(kernel_map, (vm_offset_t *) &cbp,
76 sizeof(console_buf_t));
77 if (ret != KERN_SUCCESS) {
78 printf("console_cpu_alloc() "
79 "failed to allocate cpu buffer, error=%d\n", ret);
80 return NULL;
81 }
82
83 cbp->buf_base = (char *) &cbp->buf;
84 cbp->buf_ptr = cbp->buf_base;
85 cbp->buf_end = cbp->buf_base + CPU_BUFFER_LEN;
86
87 return (void *) cbp;
88}
89
90void
91console_cpu_free(void *buf)
92{
93 if (buf != NULL)
94 kfree((void *) buf, sizeof(console_buf_t));
95}
96
97static boolean_t
98console_ring_put(char ch)
99{
100 if (console_ring.used < console_ring.len) {
101 console_ring.used++;;
102 *console_ring.write_ptr++ = ch;
103 if (console_ring.write_ptr - console_ring.buffer
104 == console_ring.len)
105 console_ring.write_ptr = console_ring.buffer;
106 return TRUE;
107 } else {
108 return FALSE;
109 }
110}
111
112static int
113console_ring_get(void)
114{
115 char ch = 0;
116
117 if (console_ring.used > 0) {
118 console_ring.used--;
119 ch = *console_ring.read_ptr++;
120 if (console_ring.read_ptr - console_ring.buffer
121 == console_ring.len)
122 console_ring.read_ptr = console_ring.buffer;
123 }
124 return (int) ch;
125}
126
127static inline void
128cpu_buffer_put(console_buf_t *cbp, char ch)
129{
6601e61a 130 if (cbp->buf_ptr < cbp->buf_end)
91447636
A
131 *(cbp->buf_ptr++) = ch;
132}
133
134static inline void
135_cnputc(char c)
1c79356b 136{
6601e61a 137 vcputc(0, 0, c);
55e303ae 138 if (c == '\n')
6601e61a 139 vcputc(0, 0,'\r');
91447636
A
140}
141
142void
143cnputcusr(char c)
6601e61a
A
144{
145 simple_lock(&console_ring.read_lock);
91447636
A
146 _cnputc(c);
147 simple_unlock(&console_ring.read_lock);
148}
149
150void
151cnputc(char c)
152{
153 console_buf_t *cbp;
6601e61a
A
154
155 if (!(real_ncpus > 1)) {
4452a7af
A
156 _cnputc(c);
157 return;
158 }
159
6601e61a 160 mp_disable_preemption();
4452a7af 161 /* add to stack buf */
6601e61a 162 cbp = (console_buf_t *) current_cpu_datap()->cpu_console_buf;
91447636
A
163 if (c != '\n') {
164 cpu_buffer_put(cbp, c);
165 } else {
166 boolean_t state;
167 char *cp;
168
169 /* Here at end of printf -- time to try to output */
170
171 /* copy this buffer into the shared ring buffer */
172 state = ml_set_interrupts_enabled(FALSE);
6601e61a
A
173 simple_lock(&console_ring.write_lock);
174 for (cp = cbp->buf_base; cp < cbp->buf_ptr; cp++) {
175 while (!console_ring_put(*cp))
176 /* spin if share buffer full */
177 cpu_pause();
178 }
179 (void) console_ring_put('\n');
180 simple_unlock(&console_ring.write_lock);
181 ml_set_interrupts_enabled(state);
182 cbp->buf_ptr = cbp->buf_base;
91447636
A
183
184 /*
6601e61a
A
185 * Try to get the read lock on the ring buffer to empty it.
186 * If this fails someone else is already emptying...
91447636 187 */
6601e61a
A
188 if (simple_lock_try(&console_ring.read_lock)) {
189 for (;;) {
190 char ch;
191
192 simple_lock(&console_ring.write_lock);
193 ch = console_ring_get();
194 simple_unlock(&console_ring.write_lock);
195 if (ch == 0)
196 break;
197 _cnputc(ch);
198 }
199 simple_unlock(&console_ring.read_lock);
91447636
A
200 }
201 }
202 mp_enable_preemption();
55e303ae 203}