Subversion Repositories HelenOS

Rev

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

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