Subversion Repositories HelenOS

Rev

Rev 2927 | Rev 3153 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2927 Rev 3149
1
/*
1
/*
2
 * Copyright (c) 2003 Josef Cejka
2
 * Copyright (c) 2003 Josef Cejka
3
 * Copyright (c) 2005 Jakub Jermar
3
 * Copyright (c) 2005 Jakub Jermar
4
 * All rights reserved.
4
 * All rights reserved.
5
 *
5
 *
6
 * Redistribution and use in source and binary forms, with or without
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
7
 * modification, are permitted provided that the following conditions
8
 * are met:
8
 * are met:
9
 *
9
 *
10
 * - Redistributions of source code must retain the above copyright
10
 * - Redistributions of source code must retain the above copyright
11
 *   notice, this list of conditions and the following disclaimer.
11
 *   notice, this list of conditions and the following disclaimer.
12
 * - Redistributions in binary form must reproduce the above copyright
12
 * - Redistributions in binary form must reproduce the above copyright
13
 *   notice, this list of conditions and the following disclaimer in the
13
 *   notice, this list of conditions and the following disclaimer in the
14
 *   documentation and/or other materials provided with the distribution.
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
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.
16
 *   derived from this software without specific prior written permission.
17
 *
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
19
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
24
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
28
 */
29
 
29
 
30
/** @addtogroup genericconsole
30
/** @addtogroup genericconsole
31
 * @{
31
 * @{
32
 */
32
 */
33
/** @file
33
/** @file
34
 */
34
 */
35
 
35
 
36
#include <console/console.h>
36
#include <console/console.h>
37
#include <console/chardev.h>
37
#include <console/chardev.h>
38
#include <synch/waitq.h>
38
#include <synch/waitq.h>
39
#include <synch/spinlock.h>
39
#include <synch/spinlock.h>
40
#include <arch/types.h>
40
#include <arch/types.h>
41
#include <arch.h>
41
#include <arch.h>
42
#include <func.h>
42
#include <func.h>
43
#include <print.h>
43
#include <print.h>
44
#include <atomic.h>
44
#include <atomic.h>
45
 
45
 
46
#define BUFLEN 2048
46
#define KLOG_SIZE 4096
-
 
47
 
47
static char debug_buffer[BUFLEN];
48
/**< Kernel log cyclic buffer */
48
static size_t offset = 0;
49
static char klog[KLOG_SIZE];
49
/** Initialize stdout to something that does not print, but does not fail
-
 
50
 *
50
 
51
 * Save data in some buffer so that it could be retrieved in the debugger
51
/**< First kernel log characters */
52
 */
-
 
53
static void null_putchar(chardev_t *d, const char ch)
52
static index_t klog_start = 0;
54
{
-
 
55
    if (offset >= BUFLEN)
53
/**< Number of valid kernel log characters */
56
        offset = 0;
54
static size_t klog_len = 0;
-
 
55
/**< Number of stored (not printed) kernel log characters */
57
    debug_buffer[offset++] = ch;
56
static size_t klog_stored = 0;
58
}
-
 
59
 
57
 
60
static chardev_operations_t null_stdout_ops = {
58
static chardev_operations_t null_stdout_ops = {
-
 
59
    .suspend = NULL,
-
 
60
    .resume = NULL,
61
    .write = null_putchar
61
    .write = NULL,
-
 
62
    .read = NULL
62
};
63
};
63
 
64
 
64
chardev_t null_stdout = {
65
chardev_t null_stdout = {
65
    .name = "null",
66
    .name = "null",
66
    .op = &null_stdout_ops
67
    .op = &null_stdout_ops
67
};
68
};
68
 
69
 
69
/** Standard input character device. */
70
/** Standard input character device. */
70
chardev_t *stdin = NULL;
71
chardev_t *stdin = NULL;
71
chardev_t *stdout = &null_stdout;
72
chardev_t *stdout = &null_stdout;
72
 
73
 
73
/** Get character from character device. Do not echo character.
74
/** Get character from character device. Do not echo character.
74
 *
75
 *
75
 * @param chardev Character device.
76
 * @param chardev Character device.
76
 *
77
 *
77
 * @return Character read.
78
 * @return Character read.
78
 */
79
 */
79
uint8_t _getc(chardev_t *chardev)
80
uint8_t _getc(chardev_t *chardev)
80
{
81
{
81
    uint8_t ch;
82
    uint8_t ch;
82
    ipl_t ipl;
83
    ipl_t ipl;
83
 
84
 
84
    if (atomic_get(&haltstate)) {
85
    if (atomic_get(&haltstate)) {
85
        /* If we are here, we are hopefully on the processor, that
86
        /* If we are here, we are hopefully on the processor, that
86
         * issued the 'halt' command, so proceed to read the character
87
         * issued the 'halt' command, so proceed to read the character
87
         * directly from input
88
         * directly from input
88
         */
89
         */
89
        if (chardev->op->read)
90
        if (chardev->op->read)
90
            return chardev->op->read(chardev);
91
            return chardev->op->read(chardev);
91
        /* no other way of interacting with user, halt */
92
        /* no other way of interacting with user, halt */
92
        if (CPU)
93
        if (CPU)
93
            printf("cpu%d: ", CPU->id);
94
            printf("cpu%u: ", CPU->id);
94
        else
95
        else
95
            printf("cpu: ");
96
            printf("cpu: ");
96
        printf("halted - no kconsole\n");
97
        printf("halted - no kconsole\n");
97
        cpu_halt();
98
        cpu_halt();
98
    }
99
    }
99
 
100
 
100
    waitq_sleep(&chardev->wq);
101
    waitq_sleep(&chardev->wq);
101
    ipl = interrupts_disable();
102
    ipl = interrupts_disable();
102
    spinlock_lock(&chardev->lock);
103
    spinlock_lock(&chardev->lock);
103
    ch = chardev->buffer[(chardev->index - chardev->counter) % CHARDEV_BUFLEN];
104
    ch = chardev->buffer[(chardev->index - chardev->counter) % CHARDEV_BUFLEN];
104
    chardev->counter--;
105
    chardev->counter--;
105
    spinlock_unlock(&chardev->lock);
106
    spinlock_unlock(&chardev->lock);
106
    interrupts_restore(ipl);
107
    interrupts_restore(ipl);
107
 
108
 
108
    chardev->op->resume(chardev);
109
    chardev->op->resume(chardev);
109
 
110
 
110
    return ch;
111
    return ch;
111
}
112
}
112
 
113
 
113
/** Get string from character device.
114
/** Get string from character device.
114
 *
115
 *
115
 * Read characters from character device until first occurrence
116
 * Read characters from character device until first occurrence
116
 * of newline character.
117
 * of newline character.
117
 *
118
 *
118
 * @param chardev Character device.
119
 * @param chardev Character device.
119
 * @param buf Buffer where to store string terminated by '\0'.
120
 * @param buf Buffer where to store string terminated by '\0'.
120
 * @param buflen Size of the buffer.
121
 * @param buflen Size of the buffer.
121
 *
122
 *
122
 * @return Number of characters read.
123
 * @return Number of characters read.
123
 */
124
 */
124
count_t gets(chardev_t *chardev, char *buf, size_t buflen)
125
count_t gets(chardev_t *chardev, char *buf, size_t buflen)
125
{
126
{
126
    index_t index = 0;
127
    index_t index = 0;
127
    char ch;
128
    char ch;
128
 
129
 
129
    while (index < buflen) {
130
    while (index < buflen) {
130
        ch = _getc(chardev);
131
        ch = _getc(chardev);
131
        if (ch == '\b') {
132
        if (ch == '\b') {
132
            if (index > 0) {
133
            if (index > 0) {
133
                index--;
134
                index--;
134
                /* Space backspace, space */
135
                /* Space backspace, space */
135
                putchar('\b');
136
                putchar('\b');
136
                putchar(' ');
137
                putchar(' ');
137
                putchar('\b');
138
                putchar('\b');
138
            }
139
            }
139
            continue;
140
            continue;
140
        }
141
        }
141
        putchar(ch);
142
        putchar(ch);
142
 
143
 
143
        if (ch == '\n') { /* end of string => write 0, return */
144
        if (ch == '\n') { /* end of string => write 0, return */
144
            buf[index] = '\0';
145
            buf[index] = '\0';
145
            return (count_t) index;
146
            return (count_t) index;
146
        }
147
        }
147
        buf[index++] = ch;
148
        buf[index++] = ch;
148
    }
149
    }
149
    return (count_t) index;
150
    return (count_t) index;
150
}
151
}
151
 
152
 
152
/** Get character from device & echo it to screen */
153
/** Get character from device & echo it to screen */
153
uint8_t getc(chardev_t *chardev)
154
uint8_t getc(chardev_t *chardev)
154
{
155
{
155
    uint8_t ch;
156
    uint8_t ch;
156
 
157
 
157
    ch = _getc(chardev);
158
    ch = _getc(chardev);
158
    putchar(ch);
159
    putchar(ch);
159
    return ch;
160
    return ch;
160
}
161
}
161
 
162
 
162
void putchar(char c)
163
void putchar(char c)
163
{
164
{
-
 
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
   
164
    if (stdout->op->write)
180
    if (stdout->op->write)
165
        stdout->op->write(stdout, c);
181
        stdout->op->write(stdout, c);
-
 
182
    else {
-
 
183
        /* The character is just in the kernel log */
-
 
184
        if (klog_stored < klog_len)
-
 
185
            klog_stored++;
-
 
186
    }
166
}
187
}
167
 
188
 
168
/** @}
189
/** @}
169
 */
190
 */
170
 
191