Subversion Repositories HelenOS

Rev

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

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