Subversion Repositories HelenOS

Rev

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