Subversion Repositories HelenOS

Rev

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

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