Subversion Repositories HelenOS

Rev

Rev 3424 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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