Subversion Repositories HelenOS

Rev

Rev 3742 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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