Subversion Repositories HelenOS

Rev

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

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