[PATCH v8 1/6] xen/riscv: introduce temporary printk stuff

Oleksii Kurochko posted 6 patches 1 year, 2 months ago
There is a newer version of this series
[PATCH v8 1/6] xen/riscv: introduce temporary printk stuff
Posted by Oleksii Kurochko 1 year, 2 months ago
The patch introdcuces printk related stuff which should be deleted
after Xen common code will be available.

Signed-off-by: Oleksii Kurochko <oleksii.kurochko@gmail.com>
---
Changes in V8:
 - only rebase was done.
---
Changes in V7:
 - only rebase was done.
---
Changes in V6:
 - the patch was introduced in the current patch series (V6)
---
 xen/arch/riscv/early_printk.c | 168 ++++++++++++++++++++++++++++++++++
 1 file changed, 168 insertions(+)

diff --git a/xen/arch/riscv/early_printk.c b/xen/arch/riscv/early_printk.c
index 610c814f54..60742a042d 100644
--- a/xen/arch/riscv/early_printk.c
+++ b/xen/arch/riscv/early_printk.c
@@ -40,3 +40,171 @@ void early_printk(const char *str)
         str++;
     }
 }
+
+/*
+ * The following #if 1 ... #endif should be removed after printk
+ * and related stuff are ready.
+ */
+#if 1
+
+#include <xen/stdarg.h>
+#include <xen/string.h>
+
+/**
+ * strlen - Find the length of a string
+ * @s: The string to be sized
+ */
+size_t (strlen)(const char * s)
+{
+    const char *sc;
+
+    for (sc = s; *sc != '\0'; ++sc)
+        /* nothing */;
+    return sc - s;
+}
+
+/**
+ * memcpy - Copy one area of memory to another
+ * @dest: Where to copy to
+ * @src: Where to copy from
+ * @count: The size of the area.
+ *
+ * You should not use this function to access IO space, use memcpy_toio()
+ * or memcpy_fromio() instead.
+ */
+void *(memcpy)(void *dest, const void *src, size_t count)
+{
+    char *tmp = (char *) dest, *s = (char *) src;
+
+    while (count--)
+        *tmp++ = *s++;
+
+    return dest;
+}
+
+int vsnprintf(char* str, size_t size, const char* format, va_list args)
+{
+    size_t i = 0; /* Current position in the output string */
+    size_t written = 0; /* Total number of characters written */
+    char* dest = str;
+
+    while ( format[i] != '\0' && written < size - 1 )
+    {
+        if ( format[i] == '%' )
+        {
+            i++;
+
+            if ( format[i] == '\0' )
+                break;
+
+            if ( format[i] == '%' )
+            {
+                if ( written < size - 1 )
+                {
+                    dest[written] = '%';
+                    written++;
+                }
+                i++;
+                continue;
+            }
+
+            /*
+             * Handle format specifiers.
+             * For simplicity, only %s and %d are implemented here.
+             */
+
+            if ( format[i] == 's' )
+            {
+                char* arg = va_arg(args, char*);
+                size_t arglen = strlen(arg);
+
+                size_t remaining = size - written - 1;
+
+                if ( arglen > remaining )
+                    arglen = remaining;
+
+                memcpy(dest + written, arg, arglen);
+
+                written += arglen;
+                i++;
+            }
+            else if ( format[i] == 'd' )
+            {
+                int arg = va_arg(args, int);
+
+                /* Convert the integer to string representation */
+                char numstr[32]; /* Assumes a maximum of 32 digits */
+                int numlen = 0;
+                int num = arg;
+                size_t remaining;
+
+                if ( arg < 0 )
+                {
+                    if ( written < size - 1 )
+                    {
+                        dest[written] = '-';
+                        written++;
+                    }
+
+                    num = -arg;
+                }
+
+                do
+                {
+                    numstr[numlen] = '0' + num % 10;
+                    num = num / 10;
+                    numlen++;
+                } while ( num > 0 );
+
+                /* Reverse the string */
+                for (int j = 0; j < numlen / 2; j++)
+                {
+                    char tmp = numstr[j];
+                    numstr[j] = numstr[numlen - 1 - j];
+                    numstr[numlen - 1 - j] = tmp;
+                }
+
+                remaining = size - written - 1;
+
+                if ( numlen > remaining )
+                    numlen = remaining;
+
+                memcpy(dest + written, numstr, numlen);
+
+                written += numlen;
+                i++;
+            }
+        }
+        else
+        {
+            if ( written < size - 1 )
+            {
+                dest[written] = format[i];
+                written++;
+            }
+            i++;
+        }
+    }
+
+    if ( size > 0 )
+        dest[written] = '\0';
+
+    return written;
+}
+
+void printk(const char *format, ...)
+{
+    static char buf[1024];
+
+    va_list args;
+    va_start(args, format);
+
+    (void)vsnprintf(buf, sizeof(buf), format, args);
+
+    early_printk(buf);
+
+    va_end(args);
+}
+
+#endif
+
-- 
2.41.0
Re: [PATCH v8 1/6] xen/riscv: introduce temporary printk stuff
Posted by Alistair Francis 1 year, 2 months ago
On Fri, Aug 11, 2023 at 10:33 AM Oleksii Kurochko
<oleksii.kurochko@gmail.com> wrote:
>
> The patch introdcuces printk related stuff which should be deleted
> after Xen common code will be available.
>
> Signed-off-by: Oleksii Kurochko <oleksii.kurochko@gmail.com>

Acked-by: Alistair Francis <alistair.francis@wdc.com>

Alistair

> ---
> Changes in V8:
>  - only rebase was done.
> ---
> Changes in V7:
>  - only rebase was done.
> ---
> Changes in V6:
>  - the patch was introduced in the current patch series (V6)
> ---
>  xen/arch/riscv/early_printk.c | 168 ++++++++++++++++++++++++++++++++++
>  1 file changed, 168 insertions(+)
>
> diff --git a/xen/arch/riscv/early_printk.c b/xen/arch/riscv/early_printk.c
> index 610c814f54..60742a042d 100644
> --- a/xen/arch/riscv/early_printk.c
> +++ b/xen/arch/riscv/early_printk.c
> @@ -40,3 +40,171 @@ void early_printk(const char *str)
>          str++;
>      }
>  }
> +
> +/*
> + * The following #if 1 ... #endif should be removed after printk
> + * and related stuff are ready.
> + */
> +#if 1
> +
> +#include <xen/stdarg.h>
> +#include <xen/string.h>
> +
> +/**
> + * strlen - Find the length of a string
> + * @s: The string to be sized
> + */
> +size_t (strlen)(const char * s)
> +{
> +    const char *sc;
> +
> +    for (sc = s; *sc != '\0'; ++sc)
> +        /* nothing */;
> +    return sc - s;
> +}
> +
> +/**
> + * memcpy - Copy one area of memory to another
> + * @dest: Where to copy to
> + * @src: Where to copy from
> + * @count: The size of the area.
> + *
> + * You should not use this function to access IO space, use memcpy_toio()
> + * or memcpy_fromio() instead.
> + */
> +void *(memcpy)(void *dest, const void *src, size_t count)
> +{
> +    char *tmp = (char *) dest, *s = (char *) src;
> +
> +    while (count--)
> +        *tmp++ = *s++;
> +
> +    return dest;
> +}
> +
> +int vsnprintf(char* str, size_t size, const char* format, va_list args)
> +{
> +    size_t i = 0; /* Current position in the output string */
> +    size_t written = 0; /* Total number of characters written */
> +    char* dest = str;
> +
> +    while ( format[i] != '\0' && written < size - 1 )
> +    {
> +        if ( format[i] == '%' )
> +        {
> +            i++;
> +
> +            if ( format[i] == '\0' )
> +                break;
> +
> +            if ( format[i] == '%' )
> +            {
> +                if ( written < size - 1 )
> +                {
> +                    dest[written] = '%';
> +                    written++;
> +                }
> +                i++;
> +                continue;
> +            }
> +
> +            /*
> +             * Handle format specifiers.
> +             * For simplicity, only %s and %d are implemented here.
> +             */
> +
> +            if ( format[i] == 's' )
> +            {
> +                char* arg = va_arg(args, char*);
> +                size_t arglen = strlen(arg);
> +
> +                size_t remaining = size - written - 1;
> +
> +                if ( arglen > remaining )
> +                    arglen = remaining;
> +
> +                memcpy(dest + written, arg, arglen);
> +
> +                written += arglen;
> +                i++;
> +            }
> +            else if ( format[i] == 'd' )
> +            {
> +                int arg = va_arg(args, int);
> +
> +                /* Convert the integer to string representation */
> +                char numstr[32]; /* Assumes a maximum of 32 digits */
> +                int numlen = 0;
> +                int num = arg;
> +                size_t remaining;
> +
> +                if ( arg < 0 )
> +                {
> +                    if ( written < size - 1 )
> +                    {
> +                        dest[written] = '-';
> +                        written++;
> +                    }
> +
> +                    num = -arg;
> +                }
> +
> +                do
> +                {
> +                    numstr[numlen] = '0' + num % 10;
> +                    num = num / 10;
> +                    numlen++;
> +                } while ( num > 0 );
> +
> +                /* Reverse the string */
> +                for (int j = 0; j < numlen / 2; j++)
> +                {
> +                    char tmp = numstr[j];
> +                    numstr[j] = numstr[numlen - 1 - j];
> +                    numstr[numlen - 1 - j] = tmp;
> +                }
> +
> +                remaining = size - written - 1;
> +
> +                if ( numlen > remaining )
> +                    numlen = remaining;
> +
> +                memcpy(dest + written, numstr, numlen);
> +
> +                written += numlen;
> +                i++;
> +            }
> +        }
> +        else
> +        {
> +            if ( written < size - 1 )
> +            {
> +                dest[written] = format[i];
> +                written++;
> +            }
> +            i++;
> +        }
> +    }
> +
> +    if ( size > 0 )
> +        dest[written] = '\0';
> +
> +    return written;
> +}
> +
> +void printk(const char *format, ...)
> +{
> +    static char buf[1024];
> +
> +    va_list args;
> +    va_start(args, format);
> +
> +    (void)vsnprintf(buf, sizeof(buf), format, args);
> +
> +    early_printk(buf);
> +
> +    va_end(args);
> +}
> +
> +#endif
> +
> --
> 2.41.0
>
>