[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>
|
---|
| 38 | #include <synch/waitq.h>
|
---|
| 39 | #include <synch/spinlock.h>
|
---|
| 40 | #include <arch/types.h>
|
---|
| 41 | #include <arch.h>
|
---|
[93b84b3] | 42 | #include <func.h>
|
---|
| 43 | #include <print.h>
|
---|
[23684b7] | 44 | #include <atomic.h>
|
---|
[2677758] | 45 |
|
---|
[c859753] | 46 | #define KLOG_SIZE 4096
|
---|
| 47 |
|
---|
| 48 | /**< Kernel log cyclic buffer */
|
---|
| 49 | static char klog[KLOG_SIZE];
|
---|
| 50 |
|
---|
| 51 | /**< First kernel log characters */
|
---|
| 52 | static index_t klog_start = 0;
|
---|
| 53 | /**< Number of valid kernel log characters */
|
---|
| 54 | static size_t klog_len = 0;
|
---|
| 55 | /**< Number of stored (not printed) kernel log characters */
|
---|
| 56 | static size_t klog_stored = 0;
|
---|
[dabe6333] | 57 |
|
---|
| 58 | static chardev_operations_t null_stdout_ops = {
|
---|
[c859753] | 59 | .suspend = NULL,
|
---|
| 60 | .resume = NULL,
|
---|
| 61 | .write = NULL,
|
---|
| 62 | .read = NULL
|
---|
[dabe6333] | 63 | };
|
---|
[4cc2ddd] | 64 |
|
---|
[dabe6333] | 65 | chardev_t null_stdout = {
|
---|
| 66 | .name = "null",
|
---|
| 67 | .op = &null_stdout_ops
|
---|
| 68 | };
|
---|
| 69 |
|
---|
[2677758] | 70 | /** Standard input character device. */
|
---|
| 71 | chardev_t *stdin = NULL;
|
---|
[dabe6333] | 72 | chardev_t *stdout = &null_stdout;
|
---|
[2677758] | 73 |
|
---|
[0c8e692] | 74 | /** Get character from character device. Do not echo character.
|
---|
[e8a9dc3] | 75 | *
|
---|
| 76 | * @param chardev Character device.
|
---|
| 77 | *
|
---|
| 78 | * @return Character read.
|
---|
| 79 | */
|
---|
[7f1c620] | 80 | uint8_t _getc(chardev_t *chardev)
|
---|
[e8a9dc3] | 81 | {
|
---|
[7f1c620] | 82 | uint8_t ch;
|
---|
[e8a9dc3] | 83 | ipl_t ipl;
|
---|
| 84 |
|
---|
[36e7ee98] | 85 | if (atomic_get(&haltstate)) {
|
---|
[93b84b3] | 86 | /* If we are here, we are hopefully on the processor, that
|
---|
| 87 | * issued the 'halt' command, so proceed to read the character
|
---|
| 88 | * directly from input
|
---|
| 89 | */
|
---|
| 90 | if (chardev->op->read)
|
---|
| 91 | return chardev->op->read(chardev);
|
---|
| 92 | /* no other way of interacting with user, halt */
|
---|
[36e7ee98] | 93 | if (CPU)
|
---|
[c859753] | 94 | printf("cpu%u: ", CPU->id);
|
---|
[36e7ee98] | 95 | else
|
---|
| 96 | printf("cpu: ");
|
---|
| 97 | printf("halted - no kconsole\n");
|
---|
[93b84b3] | 98 | cpu_halt();
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[e8a9dc3] | 101 | waitq_sleep(&chardev->wq);
|
---|
| 102 | ipl = interrupts_disable();
|
---|
| 103 | spinlock_lock(&chardev->lock);
|
---|
| 104 | ch = chardev->buffer[(chardev->index - chardev->counter) % CHARDEV_BUFLEN];
|
---|
| 105 | chardev->counter--;
|
---|
| 106 | spinlock_unlock(&chardev->lock);
|
---|
| 107 | interrupts_restore(ipl);
|
---|
| 108 |
|
---|
| 109 | chardev->op->resume(chardev);
|
---|
| 110 |
|
---|
| 111 | return ch;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[2677758] | 114 | /** Get string from character device.
|
---|
| 115 | *
|
---|
| 116 | * Read characters from character device until first occurrence
|
---|
| 117 | * of newline character.
|
---|
| 118 | *
|
---|
| 119 | * @param chardev Character device.
|
---|
[ff3b3197] | 120 | * @param buf Buffer where to store string terminated by '\0'.
|
---|
[abbc16e] | 121 | * @param buflen Size of the buffer.
|
---|
[ff3b3197] | 122 | *
|
---|
| 123 | * @return Number of characters read.
|
---|
[2677758] | 124 | */
|
---|
[ff3b3197] | 125 | count_t gets(chardev_t *chardev, char *buf, size_t buflen)
|
---|
[2677758] | 126 | {
|
---|
| 127 | index_t index = 0;
|
---|
| 128 | char ch;
|
---|
| 129 |
|
---|
| 130 | while (index < buflen) {
|
---|
[e8a9dc3] | 131 | ch = _getc(chardev);
|
---|
| 132 | if (ch == '\b') {
|
---|
| 133 | if (index > 0) {
|
---|
| 134 | index--;
|
---|
| 135 | /* Space backspace, space */
|
---|
| 136 | putchar('\b');
|
---|
| 137 | putchar(' ');
|
---|
| 138 | putchar('\b');
|
---|
| 139 | }
|
---|
| 140 | continue;
|
---|
| 141 | }
|
---|
| 142 | putchar(ch);
|
---|
| 143 |
|
---|
[2677758] | 144 | if (ch == '\n') { /* end of string => write 0, return */
|
---|
[ff3b3197] | 145 | buf[index] = '\0';
|
---|
| 146 | return (count_t) index;
|
---|
[2677758] | 147 | }
|
---|
[ff3b3197] | 148 | buf[index++] = ch;
|
---|
[2677758] | 149 | }
|
---|
[ff3b3197] | 150 | return (count_t) index;
|
---|
[2677758] | 151 | }
|
---|
| 152 |
|
---|
[e8a9dc3] | 153 | /** Get character from device & echo it to screen */
|
---|
[7f1c620] | 154 | uint8_t getc(chardev_t *chardev)
|
---|
[2677758] | 155 | {
|
---|
[7f1c620] | 156 | uint8_t ch;
|
---|
[2677758] | 157 |
|
---|
[e8a9dc3] | 158 | ch = _getc(chardev);
|
---|
| 159 | putchar(ch);
|
---|
[2677758] | 160 | return ch;
|
---|
| 161 | }
|
---|
[973be64e] | 162 |
|
---|
| 163 | void putchar(char c)
|
---|
| 164 | {
|
---|
[c859753] | 165 | if ((klog_stored > 0) && (stdout->op->write)) {
|
---|
| 166 | /* Print charaters stored in kernel log */
|
---|
| 167 | index_t i;
|
---|
| 168 | for (i = klog_len - klog_stored; i < klog_len; i++)
|
---|
| 169 | stdout->op->write(stdout, klog[(klog_start + i) % KLOG_SIZE]);
|
---|
| 170 | klog_stored = 0;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | /* Store character in the cyclic kernel log */
|
---|
| 174 | klog[(klog_start + klog_len) % KLOG_SIZE] = c;
|
---|
| 175 | if (klog_len < KLOG_SIZE)
|
---|
| 176 | klog_len++;
|
---|
| 177 | else
|
---|
| 178 | klog_start = (klog_start + 1) % KLOG_SIZE;
|
---|
| 179 |
|
---|
[93b84b3] | 180 | if (stdout->op->write)
|
---|
| 181 | stdout->op->write(stdout, c);
|
---|
[c859753] | 182 | else {
|
---|
| 183 | /* The character is just in the kernel log */
|
---|
| 184 | if (klog_stored < klog_len)
|
---|
| 185 | klog_stored++;
|
---|
| 186 | }
|
---|
[973be64e] | 187 | }
|
---|
[b45c443] | 188 |
|
---|
[06e1e95] | 189 | /** @}
|
---|
[b45c443] | 190 | */
|
---|