From ec5ebcf5ca11c1f2abd911955afb3aeabbe5008b Mon Sep 17 00:00:00 2001 From: Mathias Krause Date: Mon, 13 Nov 2023 16:09:39 +0100 Subject: [PATCH 1/5] string: make source argument of memcpy() 'const' The memory behind the source pointer passed to memcpy() is only read, make the function prototype match that fact. This allows us to get rid of some casts. Signed-off-by: Mathias Krause --- arch/x86/ioapic.c | 2 +- include/string.h | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/arch/x86/ioapic.c b/arch/x86/ioapic.c index 776d1142..85d3cb18 100644 --- a/arch/x86/ioapic.c +++ b/arch/x86/ioapic.c @@ -47,7 +47,7 @@ static int __get_system_bus_name(uint8_t bus_name[IOAPIC_SYSTEM_BUS_NAME_SIZE], return -EINVAL; memset(bus_name, ' ', IOAPIC_SYSTEM_BUS_NAME_SIZE); - memcpy(bus_name, (char *) name, namelen); + memcpy(bus_name, name, namelen); return 0; } diff --git a/include/string.h b/include/string.h index 9eb5fd68..7a244ef9 100644 --- a/include/string.h +++ b/include/string.h @@ -113,7 +113,7 @@ static inline void *memset(void *s, int c, size_t n) { return s; } -static inline void *memcpy(void *d, void *s, size_t n) { +static inline void *memcpy(void *d, const void *s, size_t n) { unsigned long d0; /* clang-format off */ @@ -138,7 +138,7 @@ static inline void *memmove(void *d, const void *s, size_t n) { /* if we don't have a range overlap, just use memcpy */ if ((d > s && d > s + n) || (d < s && d + n < s)) { - return memcpy(d, (void *) s, n); + return memcpy(d, s, n); } /* @@ -156,7 +156,7 @@ static inline void *memmove(void *d, const void *s, size_t n) { * d ----- * normal copy */ - return memcpy(d, (void *) s, n); + return memcpy(d, s, n); return d; } From 453a53b03fb4d46ea276f8572b9d84545665ac18 Mon Sep 17 00:00:00 2001 From: Mathias Krause Date: Mon, 13 Nov 2023 16:15:41 +0100 Subject: [PATCH 2/5] string: fix strdup("") Make strdup("") do the right thing -- return a duplicate of an empty string, not NULL. Only return NULL if we got passed a NULL pointer or the memory allocation failed. Signed-off-by: Mathias Krause --- include/string.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/string.h b/include/string.h index 7a244ef9..2a080bc0 100644 --- a/include/string.h +++ b/include/string.h @@ -265,7 +265,7 @@ static inline int string_equal(const char *s1, const char *s2) { static inline char *strdup(const char *s1) { char *s2; - if (string_empty(s1)) + if (!s1) return NULL; s2 = (char *) kmalloc(strlen(s1) + 1); From 68358544cf237d4303d0b66f652c484187d10011 Mon Sep 17 00:00:00 2001 From: Mathias Krause Date: Mon, 13 Nov 2023 16:16:44 +0100 Subject: [PATCH 3/5] string: micro-optimize strdup() Tweak the implementation of strdup() by making ues of memcpy() for the actual string copy, as we already determined its length before. Signed-off-by: Mathias Krause --- include/string.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/include/string.h b/include/string.h index 2a080bc0..7093793e 100644 --- a/include/string.h +++ b/include/string.h @@ -263,16 +263,18 @@ static inline int string_equal(const char *s1, const char *s2) { } static inline char *strdup(const char *s1) { + size_t len; char *s2; if (!s1) return NULL; - s2 = (char *) kmalloc(strlen(s1) + 1); - if (!s2) - return NULL; + len = strlen(s1) + 1; + s2 = kmalloc(len); + if (s2) + memcpy(s2, s1, len); - return strcpy(s2, s1); + return s2; } static inline size_t strspn(const char *s1, const char *s2) { From de9df880325aa671105e58c144d00abc076cd38a Mon Sep 17 00:00:00 2001 From: Mathias Krause Date: Mon, 13 Nov 2023 16:19:52 +0100 Subject: [PATCH 4/5] console: make panic()'s declaration noreturn Instead of attaching the __noreturn annotation to the definition, attach it to the declaration of panic() to make callers aware that this function won't return. Signed-off-by: Mathias Krause --- common/console.c | 2 +- include/console.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/common/console.c b/common/console.c index 48da57db..aa6ffbf1 100644 --- a/common/console.c +++ b/common/console.c @@ -122,7 +122,7 @@ void register_console_callback(console_callback_t cb, void *arg) { console_callbacks[num_console_callbacks++].arg = arg; } -void __noreturn panic(const char *fmt, ...) { +void panic(const char *fmt, ...) { va_list args; printk("************** PANIC **************\n"); diff --git a/include/console.h b/include/console.h index 04e87a18..231ec38e 100644 --- a/include/console.h +++ b/include/console.h @@ -51,7 +51,7 @@ extern void fb_console_write(void *arg, const char *buf, size_t len); extern void register_console_callback(console_callback_t func, void *arg); -extern void panic(const char *fmt, ...); +extern void panic(const char *fmt, ...) __noreturn; extern void warning(const char *fmt, ...); #endif /* KTF_CONSOLE_H */ From 4835c1dd6a96e2fe9cec2a87e9d1f1794f5f2fe7 Mon Sep 17 00:00:00 2001 From: Mathias Krause Date: Mon, 13 Nov 2023 16:32:34 +0100 Subject: [PATCH 5/5] console: unify panic() and warning() Move the duplicated code of panic() and warning() to a common helper that'll take care about the printk()s. The message type ("PANIC" / "WARNING") will still be center aligned. Signed-off-by: Mathias Krause --- common/console.c | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/common/console.c b/common/console.c index aa6ffbf1..fa082576 100644 --- a/common/console.c +++ b/common/console.c @@ -122,18 +122,31 @@ void register_console_callback(console_callback_t cb, void *arg) { console_callbacks[num_console_callbacks++].arg = arg; } -void panic(const char *fmt, ...) { - va_list args; +static void oops_print(const char *fmt, va_list args, const char *type) { + static const char stars[] = "***********************************"; + size_t slen = sizeof(stars) - 1; + size_t tlen = strlen(type); + int s1_len, s2_len; + + if (tlen > slen - 4) + tlen = slen - 4; + + s1_len = (slen - tlen - 2) / 2; + s2_len = slen - tlen - 2 - s1_len; - printk("************** PANIC **************\n"); + printk("%.*s %s %.*s\n", s1_len, stars, type, s2_len, stars); printk("CPU[%u]: ", smp_processor_id()); + vprintk(fmt, args); + printk("\n%s\n", stars); +} + +void panic(const char *fmt, ...) { + va_list args; va_start(args, fmt); - vprintk(fmt, args); + oops_print(fmt, args, "PANIC"); va_end(args); - printk("\n***********************************\n"); - while (1) halt(); } @@ -141,12 +154,7 @@ void panic(const char *fmt, ...) { void warning(const char *fmt, ...) { va_list args; - printk("************* WARNING *************\n"); - printk("CPU[%u]: ", smp_processor_id()); - va_start(args, fmt); - vprintk(fmt, args); + oops_print(fmt, args, "WARNING"); va_end(args); - - printk("\n***********************************\n"); }