Subversion Repositories HelenOS

Rev

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