printk: introduce temporary buffer

This is just a proof of concept for now, i am perfectly aware of the
fact that this will cause buffer overruns if the line gets too long.
This commit is contained in:
anna 2021-03-01 16:09:30 +01:00
parent dca3e716ca
commit 6f6be12e83
Signed by: fef
GPG key ID: EC22E476DC2D3D84
2 changed files with 59 additions and 29 deletions

View file

@ -4,11 +4,13 @@
#include <arch/hardware.h>
#include <ardix/io.h>
#include <ardix/kent.h>
#include <ardix/printk.h>
#include <ardix/sched.h>
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <unistd.h>
#define REG_PIOB_PER (*(uint32_t *)0x400E1000U)
@ -33,18 +35,20 @@ void do_bootstrap(void)
REG_PIOB_PER = 1 << 27;
REG_PIOB_CODR = 1 << 27;
kent_root_init();
sched_init();
devices_init();
io_init();
write(1, "hello, world\n", 13);
while (true) {
if (count++ != 1000000)
continue;
printk("hello, world (%u)!\n", print_count);
print_count++;
if (print_count % 2)
REG_PIOB_CODR = 1 << 27;

View file

@ -1,7 +1,11 @@
/* SPDX-License-Identifier: GPL-3.0-or-later */
/* See the end of this file for copyright, license, and warranty information. */
#include <arch/serial.h>
#include <ardix/dma.h>
#include <ardix/printk.h>
#include <ardix/ringbuf.h>
#include <ardix/serial.h>
#include <errno.h>
@ -12,6 +16,10 @@
#include <string.h>
#include <toolchain.h>
#ifndef CONFIG_PRINTK_BUFSZ
#define CONFIG_PRINTK_BUFSZ 64
#endif
/*
* TODO: THIS CAUSES A STACK BUFFER OVERFLOW ON SYSTEMS WHERE INT IS 64 BITS
*/
@ -19,6 +27,19 @@
/* 10 decimal digits of 4294967295 (2 ** 32 - 1) */
#define PRINTK_UINT_BUFSZ 10
static ssize_t printk_flush(struct ringbuf *buf)
{
ssize_t ret;
struct dmabuf *dma = dmabuf_create(&serial_default_device->device, buf->len);
if (dma == NULL)
return -ENOMEM;
ringbuf_read(dma->data, buf, buf->len);
ret = serial_write_dma(serial_default_device, dma);
dmabuf_put(dma);
return ret;
}
__rodata static const char fmt_hex_table[] = {
'0', '1', '2', '3',
'4', '5', '6', '7',
@ -26,30 +47,30 @@ __rodata static const char fmt_hex_table[] = {
'c', 'd', 'e', 'f',
};
static int fmt_handle_ptr(uintptr_t ptr)
static int fmt_handle_ptr(struct ringbuf *buf, uintptr_t ptr)
{
int ret;
/* 2 chars per byte, plus 2 for the "0x" hex prefix */
char buf[2 * sizeof(uintptr_t) + 2];
char *pos = &buf[2 * sizeof(uintptr_t) + 1];
char str[2 * sizeof(uintptr_t) + 2];
char *pos = &str[2 * sizeof(uintptr_t) + 1];
buf[0] = '0';
buf[1] = 'x';
str[0] = '0';
str[1] = 'x';
do {
*pos-- = fmt_hex_table[ptr & 0xf];
ptr >>= 4;
} while (pos != &buf[2]);
} while (pos != &str[2]);
ret = serial_write(serial_default_device, &buf[0], 2 * sizeof(uintptr_t) + 2);
ret = ringbuf_write(buf, &str[0], 2 * sizeof(uintptr_t) + 2);
return ret;
}
static int fmt_handle_uint(unsigned int u)
static int fmt_handle_uint(struct ringbuf *buf, unsigned int u)
{
int ret;
char buf[PRINTK_UINT_BUFSZ];
char *pos = &buf[PRINTK_UINT_BUFSZ - 1];
char str[PRINTK_UINT_BUFSZ];
char *pos = &str[PRINTK_UINT_BUFSZ - 1];
do {
/* stupid big endian humans, forcing us to do the whole thing in reverse */
@ -58,22 +79,21 @@ static int fmt_handle_uint(unsigned int u)
} while (u != 0);
pos++;
ret = serial_write(serial_default_device, pos,
PRINTK_UINT_BUFSZ - ( (size_t)pos - (size_t)&buf[0] ));
ret = ringbuf_write(buf, pos, PRINTK_UINT_BUFSZ - (pos - &str[0]));
return ret;
}
static inline int fmt_handle_int(int i)
static inline int fmt_handle_int(struct ringbuf *buf, int i)
{
int ret = 0;
char minus = '-';
if (i < 0) {
ret = serial_write(serial_default_device, &minus, sizeof(minus));
ret = ringbuf_write(buf, &minus, sizeof(minus));
i = -i;
}
ret += fmt_handle_uint((unsigned int)i);
ret += fmt_handle_uint(buf, i);
return ret;
}
@ -88,7 +108,7 @@ static inline int fmt_handle_int(int i)
* @param args: A pointer to the varargs list. Will be manipulated.
* @returns The amount of bytes written, or a negative POSIX error code.
*/
static inline int fmt_handle(const char **pos, va_list args)
static inline int fmt_handle(struct ringbuf *buf, const char **pos, va_list args)
{
int ret = 0;
union {
@ -101,33 +121,33 @@ static inline int fmt_handle(const char **pos, va_list args)
switch (**pos) {
case '%': /* literal percent sign */
ret = serial_write(serial_default_device, *pos, sizeof(**pos));
ret = ringbuf_write(buf, *pos, sizeof(**pos));
break;
case 'c': /* char */
val.c = va_arg(args, typeof(val.c));
ret = serial_write(serial_default_device, &val.c, sizeof(val.c));
ret = ringbuf_write(buf, &val.c, sizeof(val.c));
break;
case 'd': /* int */
val.d = va_arg(args, typeof(val.d));
ret = fmt_handle_int(val.d);
ret = fmt_handle_int(buf, val.d);
break;
case 'p': /* ptr */
val.p = va_arg(args, typeof(val.p));
ret = fmt_handle_ptr(val.p);
ret = fmt_handle_ptr(buf, val.p);
break;
case 's': /* string */
val.s = va_arg(args, typeof(val.s));
ret = (int)strlen(val.s);
ret = serial_write(serial_default_device, val.s, (size_t)ret);
ret = ringbuf_write(buf, val.s, (size_t)ret);
break;
case 'u': /* unsigned int */
val.u = va_arg(args, typeof(val.u));
ret = fmt_handle_uint(val.u);
ret = fmt_handle_uint(buf, val.u);
break;
}
@ -142,16 +162,19 @@ int printk(const char *fmt, ...)
int ret = 0;
int tmpret;
const char *tmp = fmt;
struct ringbuf *buf = ringbuf_create(CONFIG_PRINTK_BUFSZ);
if (buf == NULL)
return -ENOMEM;
va_start(args, fmt);
while (*tmp != '\0') {
if (*tmp++ == '%') {
/* flush out everything we have so far (minus one char for %) */
ret += (int)serial_write(serial_default_device, fmt,
(size_t)tmp - (size_t)fmt - 1);
ret += (int)ringbuf_write(buf, fmt, (size_t)tmp - (size_t)fmt - 1);
tmpret = fmt_handle(&tmp, args);
tmpret = fmt_handle(buf, &tmp, args);
/*
* act as if the current position were the beginning in
* order to make the first step of this if block easier
@ -165,10 +188,13 @@ int printk(const char *fmt, ...)
}
if (tmp != fmt && ret >= 0)
ret += serial_write(serial_default_device, fmt, (size_t)tmp - (size_t)fmt);
ret += ringbuf_write(buf, fmt, (size_t)tmp - (size_t)fmt);
va_end(args);
printk_flush(buf);
ringbuf_destroy(buf);
return ret;
}