Subversion Repositories HelenOS

Rev

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

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