Subversion Repositories HelenOS

Rev

Rev 4344 | Rev 4347 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
510 jermar 1
/*
2071 jermar 2
 * Copyright (c) 2003 Josef Cejka
3
 * Copyright (c) 2005 Jakub Jermar
510 jermar 4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 *
10
 * - Redistributions of source code must retain the above copyright
11
 *   notice, this list of conditions and the following disclaimer.
12
 * - Redistributions in binary form must reproduce the above copyright
13
 *   notice, this list of conditions and the following disclaimer in the
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
16
 *   derived from this software without specific prior written permission.
17
 *
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
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
29
 
1888 jermar 30
/** @addtogroup genericconsole
1702 cejka 31
 * @{
32
 */
33
/** @file
34
 */
35
 
510 jermar 36
#include <console/console.h>
37
#include <console/chardev.h>
3153 svoboda 38
#include <sysinfo/sysinfo.h>
510 jermar 39
#include <synch/waitq.h>
40
#include <synch/spinlock.h>
41
#include <arch/types.h>
3153 svoboda 42
#include <ddi/device.h>
43
#include <ddi/irq.h>
44
#include <ddi/ddi.h>
45
#include <ipc/irq.h>
510 jermar 46
#include <arch.h>
607 palkovsky 47
#include <func.h>
48
#include <print.h>
1104 jermar 49
#include <atomic.h>
510 jermar 50
 
3153 svoboda 51
#define KLOG_SIZE PAGE_SIZE
52
#define KLOG_LATENCY 8
1050 palkovsky 53
 
4344 svoboda 54
/** Kernel log cyclic buffer */
3153 svoboda 55
static char klog[KLOG_SIZE] __attribute__ ((aligned (PAGE_SIZE)));
3149 svoboda 56
 
4344 svoboda 57
/** Kernel log initialized */
3153 svoboda 58
static bool klog_inited = false;
4344 svoboda 59
/** First kernel log characters */
3149 svoboda 60
static index_t klog_start = 0;
4344 svoboda 61
/** Number of valid kernel log characters */
3149 svoboda 62
static size_t klog_len = 0;
4344 svoboda 63
/** Number of stored (not printed) kernel log characters */
3149 svoboda 64
static size_t klog_stored = 0;
4344 svoboda 65
/** Number of stored kernel log characters for uspace */
3153 svoboda 66
static size_t klog_uspace = 0;
3149 svoboda 67
 
4344 svoboda 68
/** Silence output */
69
bool silent = false;
4341 svoboda 70
 
4344 svoboda 71
/** Kernel log spinlock */
3153 svoboda 72
SPINLOCK_INITIALIZE(klog_lock);
73
 
74
/** Physical memory area used for klog buffer */
75
static parea_t klog_parea;
4341 svoboda 76
 
4346 svoboda 77
/** Standard input and output character devices */
78
indev_t *stdin = NULL;
79
outdev_t *stdout = NULL;
3153 svoboda 80
 
81
/** Initialize kernel logging facility
82
 *
83
 * The shared area contains kernel cyclic buffer. Userspace application may
84
 * be notified on new data with indication of position and size
85
 * of the data within the circular buffer.
4346 svoboda 86
 *
3153 svoboda 87
 */
88
void klog_init(void)
89
{
90
    void *faddr = (void *) KA2PA(klog);
91
 
92
    ASSERT((uintptr_t) faddr % FRAME_SIZE == 0);
93
    ASSERT(KLOG_SIZE % FRAME_SIZE == 0);
94
 
95
    klog_parea.pbase = (uintptr_t) faddr;
96
    klog_parea.frames = SIZE2FRAMES(KLOG_SIZE);
97
    ddi_parea_register(&klog_parea);
4346 svoboda 98
 
3153 svoboda 99
    sysinfo_set_item_val("klog.faddr", NULL, (unative_t) faddr);
100
    sysinfo_set_item_val("klog.pages", NULL, SIZE2FRAMES(KLOG_SIZE));
101
 
4346 svoboda 102
    //irq_initialize(&klog_irq);
103
    //klog_irq.devno = devno;
104
    //klog_irq.inr = KLOG_VIRT_INR;
105
    //klog_irq.claim = klog_claim;
106
    //irq_register(&klog_irq);
107
 
3153 svoboda 108
    spinlock_lock(&klog_lock);
109
    klog_inited = true;
110
    spinlock_unlock(&klog_lock);
111
}
112
 
4341 svoboda 113
void grab_console(void)
114
{
115
    silent = false;
116
    arch_grab_console();
117
}
118
 
119
void release_console(void)
120
{
121
    silent = true;
122
    arch_release_console();
123
}
124
 
4346 svoboda 125
bool check_poll(indev_t *indev)
126
{
127
    if (indev == NULL)
128
        return false;
129
 
130
    if (indev->op == NULL)
131
        return false;
132
 
133
    return (indev->op->poll != NULL);
134
}
135
 
136
/** Get character from input character device. Do not echo character.
588 palkovsky 137
 *
4346 svoboda 138
 * @param indev Input character device.
139
 * @return Character read.
588 palkovsky 140
 *
141
 */
4346 svoboda 142
uint8_t _getc(indev_t *indev)
588 palkovsky 143
{
631 palkovsky 144
    if (atomic_get(&haltstate)) {
4346 svoboda 145
        /* If we are here, we are hopefully on the processor that
607 palkovsky 146
         * issued the 'halt' command, so proceed to read the character
147
         * directly from input
148
         */
4346 svoboda 149
        if (check_poll(indev))
150
            return indev->op->poll(indev);
151
 
152
        /* No other way of interacting with user */
153
        interrupts_disable();
154
 
631 palkovsky 155
        if (CPU)
3149 svoboda 156
            printf("cpu%u: ", CPU->id);
631 palkovsky 157
        else
158
            printf("cpu: ");
4346 svoboda 159
        printf("halted (no polling input)\n");
607 palkovsky 160
        cpu_halt();
161
    }
4346 svoboda 162
 
163
    waitq_sleep(&indev->wq);
164
    ipl_t ipl = interrupts_disable();
165
    spinlock_lock(&indev->lock);
166
    uint8_t ch = indev->buffer[(indev->index - indev->counter) % INDEV_BUFLEN];
167
    indev->counter--;
168
    spinlock_unlock(&indev->lock);
588 palkovsky 169
    interrupts_restore(ipl);
4346 svoboda 170
 
588 palkovsky 171
    return ch;
172
}
173
 
4346 svoboda 174
/** Get string from input character device.
510 jermar 175
 *
4346 svoboda 176
 * Read characters from input character device until first occurrence
510 jermar 177
 * of newline character.
178
 *
4346 svoboda 179
 * @param indev  Input character device.
180
 * @param buf    Buffer where to store string terminated by '\0'.
1708 jermar 181
 * @param buflen Size of the buffer.
517 jermar 182
 *
183
 * @return Number of characters read.
4346 svoboda 184
 *
510 jermar 185
 */
4346 svoboda 186
count_t gets(indev_t *indev, char *buf, size_t buflen)
510 jermar 187
{
188
    index_t index = 0;
4341 svoboda 189
 
510 jermar 190
    while (index < buflen) {
4346 svoboda 191
        char ch = _getc(indev);
588 palkovsky 192
        if (ch == '\b') {
193
            if (index > 0) {
194
                index--;
195
                /* Space backspace, space */
196
                putchar('\b');
197
                putchar(' ');
198
                putchar('\b');
199
            }
200
            continue;
201
        }
202
        putchar(ch);
4341 svoboda 203
 
510 jermar 204
        if (ch == '\n') { /* end of string => write 0, return */
517 jermar 205
            buf[index] = '\0';
206
            return (count_t) index;
510 jermar 207
        }
517 jermar 208
        buf[index++] = ch;
510 jermar 209
    }
4346 svoboda 210
 
517 jermar 211
    return (count_t) index;
510 jermar 212
}
213
 
4346 svoboda 214
/** Get character from input device & echo it to screen */
215
uint8_t getc(indev_t *indev)
510 jermar 216
{
4346 svoboda 217
    uint8_t ch = _getc(indev);
588 palkovsky 218
    putchar(ch);
510 jermar 219
    return ch;
220
}
575 palkovsky 221
 
3153 svoboda 222
void klog_update(void)
223
{
224
    spinlock_lock(&klog_lock);
225
 
4346 svoboda 226
//  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);
228
//      klog_uspace = 0;
229
//  }
3153 svoboda 230
 
231
    spinlock_unlock(&klog_lock);
232
}
233
 
575 palkovsky 234
void putchar(char c)
235
{
3153 svoboda 236
    spinlock_lock(&klog_lock);
237
 
4346 svoboda 238
    if ((klog_stored > 0) && (stdout) && (stdout->op->write)) {
3149 svoboda 239
        /* Print charaters stored in kernel log */
240
        index_t i;
241
        for (i = klog_len - klog_stored; i < klog_len; i++)
4341 svoboda 242
            stdout->op->write(stdout, klog[(klog_start + i) % KLOG_SIZE], silent);
3149 svoboda 243
        klog_stored = 0;
244
    }
245
 
246
    /* Store character in the cyclic kernel log */
247
    klog[(klog_start + klog_len) % KLOG_SIZE] = c;
248
    if (klog_len < KLOG_SIZE)
249
        klog_len++;
250
    else
251
        klog_start = (klog_start + 1) % KLOG_SIZE;
252
 
4346 svoboda 253
    if ((stdout) && (stdout->op->write))
4341 svoboda 254
        stdout->op->write(stdout, c, silent);
3149 svoboda 255
    else {
256
        /* The character is just in the kernel log */
257
        if (klog_stored < klog_len)
258
            klog_stored++;
259
    }
3153 svoboda 260
 
261
    /* The character is stored for uspace */
262
    if (klog_uspace < klog_len)
263
        klog_uspace++;
264
 
265
    /* Check notify uspace to update */
266
    bool update;
267
    if ((klog_uspace > KLOG_LATENCY) || (c == '\n'))
268
        update = true;
269
    else
270
        update = false;
271
 
272
    spinlock_unlock(&klog_lock);
273
 
274
    if (update)
275
        klog_update();
575 palkovsky 276
}
1702 cejka 277
 
1888 jermar 278
/** @}
1702 cejka 279
 */