source: mainline/kernel/generic/src/console/console.c@ 207e8880

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 207e8880 was 1066041, checked in by Adam Hraska <adam.hraska+hos@…>, 13 years ago

preemption_disable: Turned functions into macros. Moved THREAD, AS, TASK, CPU into thread.h, as.h, task.h, cpu.h to fix the include hell that ensued.

  • Property mode set to 100644
File size: 8.4 KB
Line 
1/*
2 * Copyright (c) 2003 Josef Cejka
3 * Copyright (c) 2005 Jakub Jermar
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup genericconsole
31 * @{
32 */
33/** @file
34 */
35
36#include <console/console.h>
37#include <console/chardev.h>
38#include <sysinfo/sysinfo.h>
39#include <synch/waitq.h>
40#include <synch/spinlock.h>
41#include <typedefs.h>
42#include <ddi/irq.h>
43#include <ddi/ddi.h>
44#include <ipc/event.h>
45#include <ipc/irq.h>
46#include <arch.h>
47#include <panic.h>
48#include <print.h>
49#include <putchar.h>
50#include <atomic.h>
51#include <syscall/copy.h>
52#include <errno.h>
53#include <str.h>
54#include <mm/frame.h> /* SIZE2FRAMES */
55#include <mm/slab.h> /* malloc */
56
57#define KLOG_PAGES 8
58#define KLOG_LENGTH (KLOG_PAGES * PAGE_SIZE / sizeof(wchar_t))
59
60/** Kernel log cyclic buffer */
61wchar_t klog[KLOG_LENGTH] __attribute__((aligned(PAGE_SIZE)));
62
63/** Kernel log initialized */
64static atomic_t klog_inited = {false};
65
66/** First kernel log characters */
67static size_t klog_start = 0;
68
69/** Number of valid kernel log characters */
70static size_t klog_len = 0;
71
72/** Number of stored (not printed) kernel log characters */
73static size_t klog_stored = 0;
74
75/** Number of stored kernel log characters for uspace */
76static size_t klog_uspace = 0;
77
78/** Kernel log spinlock */
79SPINLOCK_STATIC_INITIALIZE_NAME(klog_lock, "klog_lock");
80
81/** Physical memory area used for klog buffer */
82static parea_t klog_parea;
83
84static indev_t stdin_sink;
85static outdev_t stdout_source;
86
87static indev_operations_t stdin_ops = {
88 .poll = NULL
89};
90
91static void stdout_write(outdev_t *, wchar_t);
92static void stdout_redraw(outdev_t *);
93
94static outdev_operations_t stdout_ops = {
95 .write = stdout_write,
96 .redraw = stdout_redraw
97};
98
99/** Override kernel console lockout */
100bool console_override = false;
101
102/** Standard input and output character devices */
103indev_t *stdin = NULL;
104outdev_t *stdout = NULL;
105
106indev_t *stdin_wire(void)
107{
108 if (stdin == NULL) {
109 indev_initialize("stdin", &stdin_sink, &stdin_ops);
110 stdin = &stdin_sink;
111 }
112
113 return stdin;
114}
115
116void stdout_wire(outdev_t *outdev)
117{
118 if (stdout == NULL) {
119 outdev_initialize("stdout", &stdout_source, &stdout_ops);
120 stdout = &stdout_source;
121 }
122
123 list_append(&outdev->link, &stdout->list);
124}
125
126static void stdout_write(outdev_t *dev, wchar_t ch)
127{
128 list_foreach(dev->list, cur) {
129 outdev_t *sink = list_get_instance(cur, outdev_t, link);
130 if ((sink) && (sink->op->write))
131 sink->op->write(sink, ch);
132 }
133}
134
135static void stdout_redraw(outdev_t *dev)
136{
137 list_foreach(dev->list, cur) {
138 outdev_t *sink = list_get_instance(cur, outdev_t, link);
139 if ((sink) && (sink->op->redraw))
140 sink->op->redraw(sink);
141 }
142}
143
144/** Initialize kernel logging facility
145 *
146 * The shared area contains kernel cyclic buffer. Userspace application may
147 * be notified on new data with indication of position and size
148 * of the data within the circular buffer.
149 *
150 */
151void klog_init(void)
152{
153 void *faddr = (void *) KA2PA(klog);
154
155 ASSERT((uintptr_t) faddr % FRAME_SIZE == 0);
156
157 klog_parea.pbase = (uintptr_t) faddr;
158 klog_parea.frames = SIZE2FRAMES(sizeof(klog));
159 klog_parea.unpriv = false;
160 klog_parea.mapped = false;
161 ddi_parea_register(&klog_parea);
162
163 sysinfo_set_item_val("klog.faddr", NULL, (sysarg_t) faddr);
164 sysinfo_set_item_val("klog.pages", NULL, KLOG_PAGES);
165
166 event_set_unmask_callback(EVENT_KLOG, klog_update);
167 atomic_set(&klog_inited, true);
168}
169
170void grab_console(void)
171{
172 bool prev = console_override;
173
174 console_override = true;
175 if ((stdout) && (stdout->op->redraw))
176 stdout->op->redraw(stdout);
177
178 if ((stdin) && (!prev)) {
179 /*
180 * Force the console to print the prompt.
181 */
182 indev_push_character(stdin, '\n');
183 }
184}
185
186void release_console(void)
187{
188 console_override = false;
189}
190
191/** Activate kernel console override */
192sysarg_t sys_debug_activate_console(void)
193{
194#ifdef CONFIG_KCONSOLE
195 grab_console();
196 return true;
197#else
198 return false;
199#endif
200}
201
202/** Get string from input character device.
203 *
204 * Read characters from input character device until first occurrence
205 * of newline character.
206 *
207 * @param indev Input character device.
208 * @param buf Buffer where to store string terminated by NULL.
209 * @param buflen Size of the buffer.
210 *
211 * @return Number of characters read.
212 *
213 */
214size_t gets(indev_t *indev, char *buf, size_t buflen)
215{
216 size_t offset = 0;
217 size_t count = 0;
218 buf[offset] = 0;
219
220 wchar_t ch;
221 while ((ch = indev_pop_character(indev)) != '\n') {
222 if (ch == '\b') {
223 if (count > 0) {
224 /* Space, backspace, space */
225 putchar('\b');
226 putchar(' ');
227 putchar('\b');
228
229 count--;
230 offset = str_lsize(buf, count);
231 buf[offset] = 0;
232 }
233 }
234 if (chr_encode(ch, buf, &offset, buflen - 1) == EOK) {
235 putchar(ch);
236 count++;
237 buf[offset] = 0;
238 }
239 }
240
241 return count;
242}
243
244/** Get character from input device & echo it to screen */
245wchar_t getc(indev_t *indev)
246{
247 wchar_t ch = indev_pop_character(indev);
248 putchar(ch);
249 return ch;
250}
251
252void klog_update(void *event)
253{
254 if (!atomic_get(&klog_inited))
255 return;
256
257 spinlock_lock(&klog_lock);
258
259 if (klog_uspace > 0) {
260 if (event_notify_3(EVENT_KLOG, true, klog_start, klog_len,
261 klog_uspace) == EOK)
262 klog_uspace = 0;
263 }
264
265 spinlock_unlock(&klog_lock);
266}
267
268void putchar(const wchar_t ch)
269{
270 bool ordy = ((stdout) && (stdout->op->write));
271
272 spinlock_lock(&klog_lock);
273
274 /* Print charaters stored in kernel log */
275 if (ordy) {
276 while (klog_stored > 0) {
277 wchar_t tmp = klog[(klog_start + klog_len - klog_stored) % KLOG_LENGTH];
278 klog_stored--;
279
280 /*
281 * We need to give up the spinlock for
282 * the physical operation of writting out
283 * the character.
284 */
285 spinlock_unlock(&klog_lock);
286 stdout->op->write(stdout, tmp);
287 spinlock_lock(&klog_lock);
288 }
289 }
290
291 /* Store character in the cyclic kernel log */
292 klog[(klog_start + klog_len) % KLOG_LENGTH] = ch;
293 if (klog_len < KLOG_LENGTH)
294 klog_len++;
295 else
296 klog_start = (klog_start + 1) % KLOG_LENGTH;
297
298 if (!ordy) {
299 if (klog_stored < klog_len)
300 klog_stored++;
301 }
302
303 /* The character is stored for uspace */
304 if (klog_uspace < klog_len)
305 klog_uspace++;
306
307 spinlock_unlock(&klog_lock);
308
309 if (ordy) {
310 /*
311 * Output the character. In this case
312 * it should be no longer buffered.
313 */
314 stdout->op->write(stdout, ch);
315 } else {
316 /*
317 * No standard output routine defined yet.
318 * The character is still stored in the kernel log
319 * for possible future output.
320 *
321 * The early_putchar() function is used to output
322 * the character for low-level debugging purposes.
323 * Note that the early_putc() function might be
324 * a no-op on certain hardware configurations.
325 */
326 early_putchar(ch);
327 }
328
329 /* Force notification on newline */
330 if (ch == '\n')
331 klog_update(NULL);
332}
333
334/** Print using kernel facility
335 *
336 * Print to kernel log.
337 *
338 */
339sysarg_t sys_klog(int fd, const void *buf, size_t size)
340{
341 char *data;
342 int rc;
343
344 if (size > PAGE_SIZE)
345 return (sysarg_t) ELIMIT;
346
347 if (size > 0) {
348 data = (char *) malloc(size + 1, 0);
349 if (!data)
350 return (sysarg_t) ENOMEM;
351
352 rc = copy_from_uspace(data, buf, size);
353 if (rc) {
354 free(data);
355 return (sysarg_t) rc;
356 }
357 data[size] = 0;
358
359 printf("%s", data);
360 free(data);
361 } else
362 klog_update(NULL);
363
364 return size;
365}
366
367/** @}
368 */
Note: See TracBrowser for help on using the repository browser.