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 <arch/types.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 <print.h>
|
---|
48 | #include <putchar.h>
|
---|
49 | #include <atomic.h>
|
---|
50 | #include <syscall/copy.h>
|
---|
51 | #include <errno.h>
|
---|
52 | #include <string.h>
|
---|
53 |
|
---|
54 | #define KLOG_PAGES 4
|
---|
55 | #define KLOG_LENGTH (KLOG_PAGES * PAGE_SIZE / sizeof(wchar_t))
|
---|
56 | #define KLOG_LATENCY 8
|
---|
57 |
|
---|
58 | /** Kernel log cyclic buffer */
|
---|
59 | static wchar_t klog[KLOG_LENGTH] __attribute__ ((aligned (PAGE_SIZE)));
|
---|
60 |
|
---|
61 | /** Kernel log initialized */
|
---|
62 | static bool klog_inited = false;
|
---|
63 | /** First kernel log characters */
|
---|
64 | static size_t klog_start = 0;
|
---|
65 | /** Number of valid kernel log characters */
|
---|
66 | static size_t klog_len = 0;
|
---|
67 | /** Number of stored (not printed) kernel log characters */
|
---|
68 | static size_t klog_stored = 0;
|
---|
69 | /** Number of stored kernel log characters for uspace */
|
---|
70 | static size_t klog_uspace = 0;
|
---|
71 |
|
---|
72 | /** Kernel log spinlock */
|
---|
73 | SPINLOCK_INITIALIZE(klog_lock);
|
---|
74 |
|
---|
75 | /** Physical memory area used for klog buffer */
|
---|
76 | static parea_t klog_parea;
|
---|
77 |
|
---|
78 | static indev_t stdin_sink;
|
---|
79 | static outdev_t stdout_source;
|
---|
80 |
|
---|
81 | static indev_operations_t stdin_ops = {
|
---|
82 | .poll = NULL
|
---|
83 | };
|
---|
84 |
|
---|
85 | static void stdout_write(outdev_t *dev, wchar_t ch, bool silent);
|
---|
86 |
|
---|
87 | static outdev_operations_t stdout_ops = {
|
---|
88 | .write = stdout_write
|
---|
89 | };
|
---|
90 |
|
---|
91 | /** Silence output */
|
---|
92 | bool silent = false;
|
---|
93 |
|
---|
94 | /** Standard input and output character devices */
|
---|
95 | indev_t *stdin = NULL;
|
---|
96 | outdev_t *stdout = NULL;
|
---|
97 |
|
---|
98 | indev_t *stdin_wire(void)
|
---|
99 | {
|
---|
100 | if (stdin == NULL) {
|
---|
101 | indev_initialize("stdin", &stdin_sink, &stdin_ops);
|
---|
102 | stdin = &stdin_sink;
|
---|
103 | }
|
---|
104 |
|
---|
105 | return stdin;
|
---|
106 | }
|
---|
107 |
|
---|
108 | void stdout_wire(outdev_t *outdev)
|
---|
109 | {
|
---|
110 | if (stdout == NULL) {
|
---|
111 | outdev_initialize("stdout", &stdout_source, &stdout_ops);
|
---|
112 | stdout = &stdout_source;
|
---|
113 | }
|
---|
114 |
|
---|
115 | list_append(&outdev->link, &stdout->list);
|
---|
116 | }
|
---|
117 |
|
---|
118 | static void stdout_write(outdev_t *dev, wchar_t ch, bool silent)
|
---|
119 | {
|
---|
120 | link_t *cur;
|
---|
121 |
|
---|
122 | for (cur = dev->list.next; cur != &dev->list; cur = cur->next) {
|
---|
123 | outdev_t *sink = list_get_instance(cur, outdev_t, link);
|
---|
124 | sink->op->write(sink, ch, silent);
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | /** Initialize kernel logging facility
|
---|
129 | *
|
---|
130 | * The shared area contains kernel cyclic buffer. Userspace application may
|
---|
131 | * be notified on new data with indication of position and size
|
---|
132 | * of the data within the circular buffer.
|
---|
133 | *
|
---|
134 | */
|
---|
135 | void klog_init(void)
|
---|
136 | {
|
---|
137 | void *faddr = (void *) KA2PA(klog);
|
---|
138 |
|
---|
139 | ASSERT((uintptr_t) faddr % FRAME_SIZE == 0);
|
---|
140 |
|
---|
141 | klog_parea.pbase = (uintptr_t) faddr;
|
---|
142 | klog_parea.frames = SIZE2FRAMES(sizeof(klog));
|
---|
143 | ddi_parea_register(&klog_parea);
|
---|
144 |
|
---|
145 | sysinfo_set_item_val("klog.faddr", NULL, (unative_t) faddr);
|
---|
146 | sysinfo_set_item_val("klog.pages", NULL, KLOG_PAGES);
|
---|
147 |
|
---|
148 | spinlock_lock(&klog_lock);
|
---|
149 | klog_inited = true;
|
---|
150 | spinlock_unlock(&klog_lock);
|
---|
151 | }
|
---|
152 |
|
---|
153 | void grab_console(void)
|
---|
154 | {
|
---|
155 | bool prev = silent;
|
---|
156 |
|
---|
157 | silent = false;
|
---|
158 | arch_grab_console();
|
---|
159 |
|
---|
160 | /* Force the console to print the prompt */
|
---|
161 | if ((stdin) && (prev))
|
---|
162 | indev_push_character(stdin, '\n');
|
---|
163 | }
|
---|
164 |
|
---|
165 | void release_console(void)
|
---|
166 | {
|
---|
167 | silent = true;
|
---|
168 | arch_release_console();
|
---|
169 | }
|
---|
170 |
|
---|
171 | /** Tell kernel to get keyboard/console access again */
|
---|
172 | unative_t sys_debug_enable_console(void)
|
---|
173 | {
|
---|
174 | #ifdef CONFIG_KCONSOLE
|
---|
175 | grab_console();
|
---|
176 | return true;
|
---|
177 | #else
|
---|
178 | return false;
|
---|
179 | #endif
|
---|
180 | }
|
---|
181 |
|
---|
182 | /** Tell kernel to relinquish keyboard/console access */
|
---|
183 | unative_t sys_debug_disable_console(void)
|
---|
184 | {
|
---|
185 | release_console();
|
---|
186 | return true;
|
---|
187 | }
|
---|
188 |
|
---|
189 | /** Get string from input character device.
|
---|
190 | *
|
---|
191 | * Read characters from input character device until first occurrence
|
---|
192 | * of newline character.
|
---|
193 | *
|
---|
194 | * @param indev Input character device.
|
---|
195 | * @param buf Buffer where to store string terminated by NULL.
|
---|
196 | * @param buflen Size of the buffer.
|
---|
197 | *
|
---|
198 | * @return Number of characters read.
|
---|
199 | *
|
---|
200 | */
|
---|
201 | size_t gets(indev_t *indev, char *buf, size_t buflen)
|
---|
202 | {
|
---|
203 | size_t offset = 0;
|
---|
204 | size_t count = 0;
|
---|
205 | buf[offset] = 0;
|
---|
206 |
|
---|
207 | wchar_t ch;
|
---|
208 | while ((ch = indev_pop_character(indev)) != '\n') {
|
---|
209 | if (ch == '\b') {
|
---|
210 | if (count > 0) {
|
---|
211 | /* Space, backspace, space */
|
---|
212 | putchar('\b');
|
---|
213 | putchar(' ');
|
---|
214 | putchar('\b');
|
---|
215 |
|
---|
216 | count--;
|
---|
217 | offset = str_lsize(buf, count);
|
---|
218 | buf[offset] = 0;
|
---|
219 | }
|
---|
220 | }
|
---|
221 | if (chr_encode(ch, buf, &offset, buflen - 1) == EOK) {
|
---|
222 | putchar(ch);
|
---|
223 | count++;
|
---|
224 | buf[offset] = 0;
|
---|
225 | }
|
---|
226 | }
|
---|
227 |
|
---|
228 | return count;
|
---|
229 | }
|
---|
230 |
|
---|
231 | /** Get character from input device & echo it to screen */
|
---|
232 | wchar_t getc(indev_t *indev)
|
---|
233 | {
|
---|
234 | wchar_t ch = indev_pop_character(indev);
|
---|
235 | putchar(ch);
|
---|
236 | return ch;
|
---|
237 | }
|
---|
238 |
|
---|
239 | void klog_update(void)
|
---|
240 | {
|
---|
241 | spinlock_lock(&klog_lock);
|
---|
242 |
|
---|
243 | if ((klog_inited) && (event_is_subscribed(EVENT_KLOG)) && (klog_uspace > 0)) {
|
---|
244 | event_notify_3(EVENT_KLOG, klog_start, klog_len, klog_uspace);
|
---|
245 | klog_uspace = 0;
|
---|
246 | }
|
---|
247 |
|
---|
248 | spinlock_unlock(&klog_lock);
|
---|
249 | }
|
---|
250 |
|
---|
251 | void putchar(const wchar_t ch)
|
---|
252 | {
|
---|
253 | spinlock_lock(&klog_lock);
|
---|
254 |
|
---|
255 | if ((klog_stored > 0) && (stdout) && (stdout->op->write)) {
|
---|
256 | /* Print charaters stored in kernel log */
|
---|
257 | size_t i;
|
---|
258 | for (i = klog_len - klog_stored; i < klog_len; i++)
|
---|
259 | stdout->op->write(stdout, klog[(klog_start + i) % KLOG_LENGTH], silent);
|
---|
260 | klog_stored = 0;
|
---|
261 | }
|
---|
262 |
|
---|
263 | /* Store character in the cyclic kernel log */
|
---|
264 | klog[(klog_start + klog_len) % KLOG_LENGTH] = ch;
|
---|
265 | if (klog_len < KLOG_LENGTH)
|
---|
266 | klog_len++;
|
---|
267 | else
|
---|
268 | klog_start = (klog_start + 1) % KLOG_LENGTH;
|
---|
269 |
|
---|
270 | if ((stdout) && (stdout->op->write))
|
---|
271 | stdout->op->write(stdout, ch, silent);
|
---|
272 | else {
|
---|
273 | /* The character is just in the kernel log */
|
---|
274 | if (klog_stored < klog_len)
|
---|
275 | klog_stored++;
|
---|
276 | }
|
---|
277 |
|
---|
278 | /* The character is stored for uspace */
|
---|
279 | if (klog_uspace < klog_len)
|
---|
280 | klog_uspace++;
|
---|
281 |
|
---|
282 | /* Check notify uspace to update */
|
---|
283 | bool update;
|
---|
284 | if ((klog_uspace > KLOG_LATENCY) || (ch == '\n'))
|
---|
285 | update = true;
|
---|
286 | else
|
---|
287 | update = false;
|
---|
288 |
|
---|
289 | spinlock_unlock(&klog_lock);
|
---|
290 |
|
---|
291 | if (update)
|
---|
292 | klog_update();
|
---|
293 | }
|
---|
294 |
|
---|
295 | /** Print using kernel facility
|
---|
296 | *
|
---|
297 | * Print to kernel log.
|
---|
298 | *
|
---|
299 | */
|
---|
300 | unative_t sys_klog(int fd, const void *buf, size_t size)
|
---|
301 | {
|
---|
302 | char *data;
|
---|
303 | int rc;
|
---|
304 |
|
---|
305 | if (size > PAGE_SIZE)
|
---|
306 | return ELIMIT;
|
---|
307 |
|
---|
308 | if (size > 0) {
|
---|
309 | data = (char *) malloc(size + 1, 0);
|
---|
310 | if (!data)
|
---|
311 | return ENOMEM;
|
---|
312 |
|
---|
313 | rc = copy_from_uspace(data, buf, size);
|
---|
314 | if (rc) {
|
---|
315 | free(data);
|
---|
316 | return rc;
|
---|
317 | }
|
---|
318 | data[size] = 0;
|
---|
319 |
|
---|
320 | printf("%s", data);
|
---|
321 | free(data);
|
---|
322 | } else
|
---|
323 | klog_update();
|
---|
324 |
|
---|
325 | return size;
|
---|
326 | }
|
---|
327 |
|
---|
328 | /** @}
|
---|
329 | */
|
---|