source: mainline/kernel/generic/src/console/console.c@ 907bb49

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 907bb49 was 312cc68, checked in by Martin Decky <martin@…>, 17 years ago

move syscall wrappers to more suitable location

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