Changeset 690ad20 in mainline


Ignore:
Timestamp:
2025-04-17T15:29:16Z (5 days ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
master
Children:
39e1b9a
Parents:
d5b37b6
git-author:
Jiří Zárevúcky <zarevucky.jiri@…> (2025-04-11 20:06:16)
git-committer:
Jiří Zárevúcky <zarevucky.jiri@…> (2025-04-17 15:29:16)
Message:

Convert kio buffer to bytes (part 1)

Files:
1 deleted
9 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/include/console/console.h

    rd5b37b6 r690ad20  
    6464extern void kio_update(void *);
    6565extern void kio_flush(void);
    66 extern void kio_push_char(const char32_t);
     66extern void kio_push_bytes(const char *, size_t);
    6767extern irq_spinlock_t kio_lock;
    6868
     
    7878extern void console_unlock(void);
    7979
     80extern void putstr(const char *s, size_t n);
     81
    8082#endif /* KERN_CONSOLE_H_ */
    8183
  • kernel/generic/include/stdio.h

    rd5b37b6 r690ad20  
    3737
    3838#include <print.h>
    39 #include <putchar.h>
    4039
    4140#endif
  • kernel/generic/src/console/console.c

    rd5b37b6 r690ad20  
    4040#include <errno.h>
    4141#include <ipc/event.h>
     42#include <log.h>
    4243#include <panic.h>
    4344#include <preemption.h>
    4445#include <proc/task.h>
    45 #include <putchar.h>
    4646#include <stdatomic.h>
    4747#include <stdio.h>
    4848#include <stdlib.h>  /* malloc */
     49#include <str.h>
    4950#include <synch/mutex.h>
    5051#include <synch/spinlock.h>
     
    5354
    5455#define KIO_PAGES    8
    55 #define KIO_LENGTH   (KIO_PAGES * PAGE_SIZE / sizeof(char32_t))
     56#define KIO_LENGTH   (KIO_PAGES * PAGE_SIZE)
    5657
    5758/** Kernel log cyclic buffer */
    58 static char32_t kio[KIO_LENGTH];
     59static char kio[KIO_LENGTH];
    5960
    6061/** Kernel log initialized */
     
    7879/** Kernel log spinlock */
    7980IRQ_SPINLOCK_INITIALIZE(kio_lock);
     81
     82static IRQ_SPINLOCK_INITIALIZE(early_mbstate_lock);
     83static mbstate_t early_mbstate;
    8084
    8185static indev_t stdin_sink;
     
    245249        irq_spinlock_lock(&kio_lock, true);
    246250
     251        static mbstate_t mbstate;
     252
    247253        /* Print characters that weren't printed earlier */
    248254        while (kio_written != kio_processed) {
    249                 char32_t tmp = kio[kio_processed % KIO_LENGTH];
    250                 kio_processed++;
     255                size_t offset = kio_processed % KIO_LENGTH;
     256                size_t len = min(kio_written - kio_processed, KIO_LENGTH - offset);
     257                size_t bytes = 0;
     258
     259                char32_t ch = str_decode_r(&kio[offset], &bytes, len, U'�', &mbstate);
     260                assert(bytes <= 4);
     261                kio_processed += bytes;
    251262
    252263                /*
     
    256267                 */
    257268                irq_spinlock_unlock(&kio_lock, true);
    258                 stdout->op->write(stdout, tmp);
     269                stdout->op->write(stdout, ch);
    259270                irq_spinlock_lock(&kio_lock, true);
    260271        }
     
    263274}
    264275
    265 /** Put a character into the output buffer.
    266  *
    267  * The caller is required to hold kio_lock
    268  */
    269 void kio_push_char(const char32_t ch)
    270 {
    271         kio[kio_written % KIO_LENGTH] = ch;
    272         kio_written++;
    273 }
    274 
    275 void putuchar(const char32_t ch)
     276void kio_push_bytes(const char *s, size_t n)
     277{
     278        /* Skip the section we know we can't keep. */
     279        if (n > KIO_LENGTH) {
     280                size_t lost = n - KIO_LENGTH;
     281                kio_written += lost;
     282                s += lost;
     283                n -= lost;
     284        }
     285
     286        size_t offset = kio_written % KIO_LENGTH;
     287        if (offset + n > KIO_LENGTH) {
     288                size_t first = KIO_LENGTH - offset;
     289                size_t last = n - first;
     290                memcpy(kio + offset, s, first);
     291                memcpy(kio, s + first, last);
     292        } else {
     293                memcpy(kio + offset, s, n);
     294        }
     295
     296        kio_written += n;
     297}
     298
     299static void early_putstr(const char *s, size_t n)
     300{
     301        irq_spinlock_lock(&early_mbstate_lock, true);
     302
     303        size_t offset = 0;
     304        char32_t c;
     305
     306        while ((c = str_decode_r(s, &offset, n, U_SPECIAL, &early_mbstate)))
     307                early_putuchar(c);
     308
     309        irq_spinlock_unlock(&early_mbstate_lock, true);
     310}
     311
     312void putstr(const char *s, size_t n)
    276313{
    277314        bool ordy = ((stdout) && (stdout->op->write));
    278315
    279316        irq_spinlock_lock(&kio_lock, true);
    280         kio_push_char(ch);
     317        kio_push_bytes(s, n);
    281318        irq_spinlock_unlock(&kio_lock, true);
    282319
     
    295332                 * a no-op on certain hardware configurations.
    296333                 */
    297                 early_putuchar(ch);
    298         }
    299 
    300         /* Force notification on newline */
    301         if (ch == '\n')
     334                early_putstr(s, n);
     335        }
     336
     337        /* Force notification when containing a newline */
     338        if (memchr(s, '\n', n) != NULL)
    302339                kio_update(NULL);
    303340}
     
    336373                size_t first = KIO_LENGTH - offset;
    337374                size_t last = actual_read - first;
    338                 size_t first_bytes = first * sizeof(kio[0]);
    339                 size_t last_bytes = last * sizeof(kio[0]);
    340 
    341                 rc = copy_to_uspace(buf, &kio[offset], first_bytes);
     375
     376                rc = copy_to_uspace(buf, &kio[offset], first);
    342377                if (rc == EOK)
    343                         rc = copy_to_uspace(buf + first_bytes, &kio[0], last_bytes);
     378                        rc = copy_to_uspace(buf + first, &kio[0], last);
    344379        } else {
    345                 rc = copy_to_uspace(buf, &kio[offset], actual_read * sizeof(kio[0]));
     380                rc = copy_to_uspace(buf, &kio[offset], actual_read);
    346381        }
    347382
  • kernel/generic/src/console/kconsole.c

    rd5b37b6 r690ad20  
    3939 */
    4040
     41#include <adt/list.h>
     42#include <arch.h>
    4143#include <assert.h>
    42 #include <console/kconsole.h>
    43 #include <console/console.h>
    4444#include <console/chardev.h>
    4545#include <console/cmd.h>
     46#include <console/console.h>
     47#include <console/kconsole.h>
    4648#include <console/prompt.h>
     49#include <debug.h>
     50#include <errno.h>
     51#include <halt.h>
     52#include <macros.h>
     53#include <panic.h>
    4754#include <stdio.h>
    48 #include <panic.h>
     55#include <stdlib.h>
     56#include <str.h>
     57#include <symtab.h>
     58#include <sysinfo/sysinfo.h>
    4959#include <typedefs.h>
    50 #include <adt/list.h>
    51 #include <arch.h>
    52 #include <macros.h>
    53 #include <debug.h>
    54 #include <halt.h>
    55 #include <str.h>
    56 #include <sysinfo/sysinfo.h>
    57 #include <symtab.h>
    58 #include <errno.h>
    59 #include <putchar.h>
    60 #include <stdlib.h>
    6160
    6261/** Simple kernel console.
     
    158157
    159158/** Print count times a character */
    160 _NO_TRACE static void print_cc(char32_t ch, size_t count)
    161 {
    162         size_t i;
    163         for (i = 0; i < count; i++)
    164                 putuchar(ch);
     159_NO_TRACE static void print_cc(char ch, size_t count)
     160{
     161        // FIXME: only lock once
     162
     163        for (size_t i = 0; i < count; i++)
     164                putstr(&ch, 1);
    165165}
    166166
     
    347347                if (ch == '\n') {
    348348                        /* Enter */
    349                         putuchar(ch);
     349                        putstr("\n", 1);
    350350                        break;
    351351                }
     
    358358                        if (wstr_remove(current, position - 1)) {
    359359                                position--;
    360                                 putuchar('\b');
     360                                putstr("\b", 1);
    361361                                printf("%ls ", current + position);
    362362                                print_cc('\b', wstr_length(current) - position + 1);
     
    368368                        /* Tab completion */
    369369
    370                         /* Move to the end of the word */
    371                         for (; (current[position] != 0) && (!isspace(current[position]));
    372                             position++)
    373                                 putuchar(current[position]);
     370                        size_t i = position;
     371                        while (current[i] && !isspace(current[i]))
     372                                i++;
     373
     374                        char32_t stash = current[i];
     375                        current[i] = 0;
     376                        printf("%ls", &current[position]);
     377                        current[i] = stash;
     378                        position = i;
    374379
    375380                        /*
     
    430435                         */
    431436                        size_t off = 0;
    432                         size_t i = 0;
    433                         while ((ch = str_decode(tmp, &off, STR_NO_LIMIT)) != 0) {
     437                        for (size_t i = 0; (ch = str_decode(tmp, &off, STR_NO_LIMIT)); i++)
    434438                                if (!wstr_linsert(current, ch, position + i, MAX_CMDLINE))
    435439                                        break;
    436 
    437                                 i++;
    438                         }
    439440
    440441                        if (found > 1) {
     
    466467                        /* Left */
    467468                        if (position > 0) {
    468                                 putuchar('\b');
     469                                putstr("\b", 1);
    469470                                position--;
    470471                        }
     
    475476                        /* Right */
    476477                        if (position < wstr_length(current)) {
    477                                 putuchar(current[position]);
     478                                printf("%lc", current[position]);
    478479                                position++;
    479480                        }
  • kernel/generic/src/log/log.c

    rd5b37b6 r690ad20  
    4646#include <print.h>
    4747#include <printf_core.h>
    48 #include <putchar.h>
    4948#include <stdarg.h>
    5049#include <stdlib.h>
     
    179178        log_used += log_current_len;
    180179
    181         kio_push_char('\n');
     180        kio_push_bytes("\n", 1);
    182181        irq_spinlock_unlock(&kio_lock, true);
    183182        irq_spinlock_unlock(&log_lock, true);
     
    203202static int log_printf_str_write(const char *str, size_t size, void *data)
    204203{
    205         size_t offset = 0;
    206 
    207         while (offset < size)
    208                 kio_push_char(str_decode(str, &offset, size));
    209 
     204        kio_push_bytes(str, size);
    210205        log_append((const uint8_t *)str, size);
    211 
    212206        return EOK;
    213207}
  • kernel/generic/src/printf/vprintf.c

    rd5b37b6 r690ad20  
    3737#include <print.h>
    3838#include <printf_core.h>
    39 #include <putchar.h>
    4039#include <str.h>
    4140#include <synch/spinlock.h>
     
    4443static errno_t vprintf_str_write(const char *str, size_t size, void *data)
    4544{
    46         size_t offset = 0;
    47 
    48         while (offset < size)
    49                 putuchar(str_decode(str, &offset, size));
    50 
     45        putstr(str, size);
    5146        return EOK;
    5247}
     
    5449int puts(const char *str)
    5550{
    56         size_t offset = 0;
    57         size_t chars = 0;
    58         char32_t uc;
    59 
    60         console_lock();
    61 
    62         while ((uc = str_decode(str, &offset, STR_NO_LIMIT)) != 0) {
    63                 putuchar(uc);
    64                 chars++;
    65         }
    66 
    67         putuchar('\n');
    68 
    69         console_unlock();
    70         return chars;
     51        size_t n = str_size(str);
     52        putstr(str, n);
     53        return n;
    7154}
    7255
  • uspace/app/kio/kio.c

    rd5b37b6 r690ad20  
    5959typedef struct {
    6060        link_t link;
    61         size_t length;
    62         char32_t *data;
     61        size_t bytes;
     62        char *data;
    6363} item_t;
    6464
     
    6868static FIBRIL_MUTEX_INITIALIZE(mtx);
    6969
    70 #define READ_BUFFER_SIZE (PAGE_SIZE / sizeof(char32_t))
     70#define READ_BUFFER_SIZE PAGE_SIZE
    7171
    7272static size_t current_at;
    73 static char32_t read_buffer[READ_BUFFER_SIZE];
     73static char read_buffer[READ_BUFFER_SIZE];
    7474
    7575/** Klog producer
     
    8282 *
    8383 */
    84 static void producer(size_t length, char32_t *data)
    85 {
    86         item_t *item = (item_t *) malloc(sizeof(item_t));
     84static void producer(size_t bytes, char *data)
     85{
     86        item_t *item = malloc(sizeof(item_t));
    8787        if (item == NULL)
    8888                return;
    8989
    90         size_t sz = sizeof(char32_t) * length;
    91         char32_t *buf = (char32_t *) malloc(sz);
    92         if (buf == NULL) {
     90        item->bytes = bytes;
     91        item->data = malloc(bytes);
     92        if (!item->data) {
    9393                free(item);
    9494                return;
    9595        }
    9696
    97         memcpy(buf, data, sz);
     97        memcpy(item->data, data, bytes);
    9898
    9999        link_initialize(&item->link);
    100         item->length = length;
    101         item->data = buf;
    102100        prodcons_produce(&pc, &item->link);
    103101}
     
    125123                item_t *item = list_get_instance(link, item_t, link);
    126124
    127                 for (size_t i = 0; i < item->length; i++)
    128                         putuchar(item->data[i]);
    129 
    130                 if (log != NULL) {
    131                         for (size_t i = 0; i < item->length; i++)
    132                                 fputuc(item->data[i], log);
    133 
     125                fwrite(item->data, 1, item->bytes, stdout);
     126
     127                if (log) {
     128                        fwrite(item->data, 1, item->bytes, log);
    134129                        fflush(log);
    135130                        vfs_sync(fileno(log));
  • uspace/lib/c/generic/io/kio.c

    rd5b37b6 r690ad20  
    113113}
    114114
    115 size_t kio_read(char32_t *buf, size_t n, size_t at)
     115size_t kio_read(char *buf, size_t n, size_t at)
    116116{
    117117        return __SYSCALL3(SYS_KIO_READ, (sysarg_t) buf, n, at);
  • uspace/lib/c/include/io/kio.h

    rd5b37b6 r690ad20  
    5252extern int kio_vprintf(const char *, va_list);
    5353
    54 extern size_t kio_read(char32_t *buf, size_t n, size_t at);
     54extern size_t kio_read(char *buf, size_t n, size_t at);
    5555
    5656/*
Note: See TracChangeset for help on using the changeset viewer.