Subversion Repositories HelenOS

Rev

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

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