Subversion Repositories HelenOS

Rev

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

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