source: mainline/kernel/generic/src/console/console.c@ 3014e2b2

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3014e2b2 was 411b6a6, checked in by Jakub Jermar <jakub@…>, 17 years ago

Complete emancipation of kernel serial controller drivers (i8042, ns16550 and
z8530). Provide a common keyboard module for PC and Sun keyboards. The serial
line module is still to follow.

  • Property mode set to 100644
File size: 7.7 KB
RevLine 
[2677758]1/*
[df4ed85]2 * Copyright (c) 2003 Josef Cejka
3 * Copyright (c) 2005 Jakub Jermar
[2677758]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
[06e1e95]30/** @addtogroup genericconsole
[b45c443]31 * @{
32 */
33/** @file
34 */
35
[2677758]36#include <console/console.h>
37#include <console/chardev.h>
[82b71ef1]38#include <sysinfo/sysinfo.h>
[2677758]39#include <synch/waitq.h>
40#include <synch/spinlock.h>
41#include <arch/types.h>
[82b71ef1]42#include <ddi/device.h>
43#include <ddi/irq.h>
44#include <ddi/ddi.h>
45#include <ipc/irq.h>
[2677758]46#include <arch.h>
[93b84b3]47#include <func.h>
48#include <print.h>
[23684b7]49#include <atomic.h>
[2677758]50
[82b71ef1]51#define KLOG_SIZE PAGE_SIZE
[c05a50f]52#define KLOG_LATENCY 8
[c859753]53
[c0855a0]54/** Kernel log cyclic buffer */
[82b71ef1]55static char klog[KLOG_SIZE] __attribute__ ((aligned (PAGE_SIZE)));
[c859753]56
[c0855a0]57/** Kernel log initialized */
[82b71ef1]58static bool klog_inited = false;
[c0855a0]59/** First kernel log characters */
[c859753]60static index_t klog_start = 0;
[c0855a0]61/** Number of valid kernel log characters */
[c859753]62static size_t klog_len = 0;
[c0855a0]63/** Number of stored (not printed) kernel log characters */
[c859753]64static size_t klog_stored = 0;
[c0855a0]65/** Number of stored kernel log characters for uspace */
[82b71ef1]66static size_t klog_uspace = 0;
67
[c0855a0]68/** Silence output */
[691eb52]69bool silent = false;
[516ff92]70
[c0855a0]71/** Kernel log spinlock */
[82b71ef1]72SPINLOCK_INITIALIZE(klog_lock);
73
74/** Physical memory area used for klog buffer */
75static parea_t klog_parea;
[516ff92]76
[82b71ef1]77/*
78 * For now, we use 0 as INR.
79 * However, it is therefore desirable to have architecture specific
80 * definition of KLOG_VIRT_INR in the future.
81 */
82#define KLOG_VIRT_INR 0
83
84static irq_t klog_irq;
[dabe6333]85
86static chardev_operations_t null_stdout_ops = {
[c859753]87 .suspend = NULL,
88 .resume = NULL,
89 .write = NULL,
90 .read = NULL
[dabe6333]91};
[4cc2ddd]92
[dabe6333]93chardev_t null_stdout = {
94 .name = "null",
95 .op = &null_stdout_ops
96};
97
[82b71ef1]98/** Allways refuse IRQ ownership.
99 *
100 * This is not a real IRQ, so we always decline.
101 *
102 * @return Always returns IRQ_DECLINE.
103 */
[c9b550b]104static irq_ownership_t klog_claim(irq_t *irq)
[82b71ef1]105{
106 return IRQ_DECLINE;
107}
108
[411b6a6]109static void stdin_suspend(chardev_t *d)
110{
111}
112
113static void stdin_resume(chardev_t *d)
114{
115}
116
117static chardev_operations_t stdin_ops = {
118 .suspend = stdin_suspend,
119 .resume = stdin_resume,
120};
121
[82b71ef1]122/** Standard input character device */
[411b6a6]123static chardev_t _stdin;
[2677758]124chardev_t *stdin = NULL;
[dabe6333]125chardev_t *stdout = &null_stdout;
[2677758]126
[411b6a6]127void console_init(void)
128{
129 chardev_initialize("stdin", &_stdin, &stdin_ops);
130 stdin = &_stdin;
131}
132
[82b71ef1]133/** Initialize kernel logging facility
134 *
135 * The shared area contains kernel cyclic buffer. Userspace application may
136 * be notified on new data with indication of position and size
137 * of the data within the circular buffer.
138 */
139void klog_init(void)
140{
141 void *faddr = (void *) KA2PA(klog);
142
143 ASSERT((uintptr_t) faddr % FRAME_SIZE == 0);
144 ASSERT(KLOG_SIZE % FRAME_SIZE == 0);
145
146 devno_t devno = device_assign_devno();
147
148 klog_parea.pbase = (uintptr_t) faddr;
149 klog_parea.frames = SIZE2FRAMES(KLOG_SIZE);
150 ddi_parea_register(&klog_parea);
151
152 sysinfo_set_item_val("klog.faddr", NULL, (unative_t) faddr);
153 sysinfo_set_item_val("klog.pages", NULL, SIZE2FRAMES(KLOG_SIZE));
154 sysinfo_set_item_val("klog.devno", NULL, devno);
155 sysinfo_set_item_val("klog.inr", NULL, KLOG_VIRT_INR);
156
157 irq_initialize(&klog_irq);
158 klog_irq.devno = devno;
159 klog_irq.inr = KLOG_VIRT_INR;
160 klog_irq.claim = klog_claim;
161 irq_register(&klog_irq);
162
163 spinlock_lock(&klog_lock);
164 klog_inited = true;
165 spinlock_unlock(&klog_lock);
166}
167
[516ff92]168void grab_console(void)
169{
170 silent = false;
171 arch_grab_console();
172}
173
174void release_console(void)
175{
176 silent = true;
177 arch_release_console();
178}
179
[0c8e692]180/** Get character from character device. Do not echo character.
[e8a9dc3]181 *
182 * @param chardev Character device.
183 *
184 * @return Character read.
185 */
[7f1c620]186uint8_t _getc(chardev_t *chardev)
[e8a9dc3]187{
[7f1c620]188 uint8_t ch;
[e8a9dc3]189 ipl_t ipl;
190
[36e7ee98]191 if (atomic_get(&haltstate)) {
[93b84b3]192 /* If we are here, we are hopefully on the processor, that
193 * issued the 'halt' command, so proceed to read the character
194 * directly from input
195 */
196 if (chardev->op->read)
197 return chardev->op->read(chardev);
198 /* no other way of interacting with user, halt */
[36e7ee98]199 if (CPU)
[c859753]200 printf("cpu%u: ", CPU->id);
[36e7ee98]201 else
202 printf("cpu: ");
[76fca31]203 printf("halted (no kconsole)\n");
[93b84b3]204 cpu_halt();
205 }
206
[e8a9dc3]207 waitq_sleep(&chardev->wq);
208 ipl = interrupts_disable();
209 spinlock_lock(&chardev->lock);
210 ch = chardev->buffer[(chardev->index - chardev->counter) % CHARDEV_BUFLEN];
211 chardev->counter--;
212 spinlock_unlock(&chardev->lock);
213 interrupts_restore(ipl);
214
215 chardev->op->resume(chardev);
216
217 return ch;
218}
219
[2677758]220/** Get string from character device.
221 *
222 * Read characters from character device until first occurrence
223 * of newline character.
224 *
225 * @param chardev Character device.
[ff3b3197]226 * @param buf Buffer where to store string terminated by '\0'.
[abbc16e]227 * @param buflen Size of the buffer.
[ff3b3197]228 *
229 * @return Number of characters read.
[2677758]230 */
[ff3b3197]231count_t gets(chardev_t *chardev, char *buf, size_t buflen)
[2677758]232{
233 index_t index = 0;
234 char ch;
[516ff92]235
[2677758]236 while (index < buflen) {
[e8a9dc3]237 ch = _getc(chardev);
238 if (ch == '\b') {
239 if (index > 0) {
240 index--;
241 /* Space backspace, space */
242 putchar('\b');
243 putchar(' ');
244 putchar('\b');
245 }
246 continue;
247 }
248 putchar(ch);
[516ff92]249
[2677758]250 if (ch == '\n') { /* end of string => write 0, return */
[ff3b3197]251 buf[index] = '\0';
252 return (count_t) index;
[2677758]253 }
[ff3b3197]254 buf[index++] = ch;
[2677758]255 }
[ff3b3197]256 return (count_t) index;
[2677758]257}
258
[e8a9dc3]259/** Get character from device & echo it to screen */
[7f1c620]260uint8_t getc(chardev_t *chardev)
[2677758]261{
[7f1c620]262 uint8_t ch;
[2677758]263
[e8a9dc3]264 ch = _getc(chardev);
265 putchar(ch);
[2677758]266 return ch;
267}
[973be64e]268
[82b71ef1]269void klog_update(void)
270{
271 spinlock_lock(&klog_lock);
272
273 if ((klog_inited) && (klog_irq.notif_cfg.notify) && (klog_uspace > 0)) {
274 ipc_irq_send_msg_3(&klog_irq, klog_start, klog_len, klog_uspace);
275 klog_uspace = 0;
276 }
277
278 spinlock_unlock(&klog_lock);
279}
280
[973be64e]281void putchar(char c)
282{
[82b71ef1]283 spinlock_lock(&klog_lock);
284
[c859753]285 if ((klog_stored > 0) && (stdout->op->write)) {
286 /* Print charaters stored in kernel log */
287 index_t i;
288 for (i = klog_len - klog_stored; i < klog_len; i++)
[516ff92]289 stdout->op->write(stdout, klog[(klog_start + i) % KLOG_SIZE], silent);
[c859753]290 klog_stored = 0;
291 }
292
293 /* Store character in the cyclic kernel log */
294 klog[(klog_start + klog_len) % KLOG_SIZE] = c;
295 if (klog_len < KLOG_SIZE)
296 klog_len++;
297 else
298 klog_start = (klog_start + 1) % KLOG_SIZE;
299
[93b84b3]300 if (stdout->op->write)
[516ff92]301 stdout->op->write(stdout, c, silent);
[c859753]302 else {
303 /* The character is just in the kernel log */
304 if (klog_stored < klog_len)
305 klog_stored++;
306 }
[82b71ef1]307
308 /* The character is stored for uspace */
309 if (klog_uspace < klog_len)
310 klog_uspace++;
311
[c05a50f]312 /* Check notify uspace to update */
313 bool update;
314 if ((klog_uspace > KLOG_LATENCY) || (c == '\n'))
315 update = true;
316 else
317 update = false;
318
[82b71ef1]319 spinlock_unlock(&klog_lock);
320
[c05a50f]321 if (update)
322 klog_update();
[973be64e]323}
[b45c443]324
[06e1e95]325/** @}
[b45c443]326 */
Note: See TracBrowser for help on using the repository browser.