diff --git a/crates/stela-driver-gpu/build/native.rs b/crates/stela-driver-gpu/build/native.rs index 7d113fffe3..f1e7dd71b3 100644 --- a/crates/stela-driver-gpu/build/native.rs +++ b/crates/stela-driver-gpu/build/native.rs @@ -177,6 +177,7 @@ impl Linux { // extra conversion diagnostics remain confined to its own source. let upstream = [ PathBuf::from(format!("{DRM}/drm_fourcc.c")), + PathBuf::from(format!("{LINUX}/lib/string.c")), PathBuf::from(format!("{LINUX}/lib/scatterlist.c")), PathBuf::from(format!("{LINUX}/lib/find_bit.c")), PathBuf::from(format!("{LINUX}/lib/bitmap.c")), @@ -349,6 +350,7 @@ impl Linux { ), ("linux_printf", "printf.c", "STELA_LINUX_PRINTF_TEST"), ("linux_uuid", "uuid.c", "STELA_LINUX_UUID_TEST"), + ("linux_kstrtox", "kstrtox.c", "STELA_LINUX_KSTRTOX_TEST"), ("linux_refcount", "refcount.c", "STELA_LINUX_REFCOUNT_TEST"), ( "linux_const_string", diff --git a/crates/stela-driver-gpu/build/nouveau.rs b/crates/stela-driver-gpu/build/nouveau.rs index e7ecd8a309..8fb4ef494f 100644 --- a/crates/stela-driver-gpu/build/nouveau.rs +++ b/crates/stela-driver-gpu/build/nouveau.rs @@ -25,18 +25,6 @@ const RUNTIME: [&str; 4] = ["memcpy", "memmove", "memset", "memcmp"]; /// lacks, or defines only as a trap, by service. Building fails when the /// imports change, so each supplied service leaves this list with its change. const OPEN: &[(&str, &[&str])] = &[ - ( - "lib/string.c", - &[ - "memchr", - "memset32", - "sized_strscpy", - "strcasecmp", - "strcspn", - "strncasecmp", - ], - ), - ("lib/kstrtox.c", &["kstrtoll"]), ( "lib/hexdump.c, lib/ratelimit.c", &["print_hex_dump", "___ratelimit"], diff --git a/crates/stela-driver-gpu/src/compat/linux/asm/string.h b/crates/stela-driver-gpu/src/compat/linux/asm/string.h index b3e67997b3..b2c81684fc 100644 --- a/crates/stela-driver-gpu/src/compat/linux/asm/string.h +++ b/crates/stela-driver-gpu/src/compat/linux/asm/string.h @@ -1,3 +1,21 @@ /* SPDX-License-Identifier: GPL-2.0 */ -/* Stela's arch has no string routines of its own: linux/string.h declares - * lib/string.c's, which the kernel-API and the platform's C runtime supply. */ +/* Stela's arch takes the memory routines the compiler itself calls from the + * platform's C runtime; linux/string.h declares the rest, which the unchanged + * lib/string.c supplies. */ +#ifndef _ASM_STELA_STRING_H +#define _ASM_STELA_STRING_H + +#include + +#define __HAVE_ARCH_MEMCPY +#define __HAVE_ARCH_MEMMOVE +#define __HAVE_ARCH_MEMSET +#define __HAVE_ARCH_MEMCMP +#define __HAVE_ARCH_BCMP +void *memcpy(void *dest, const void *src, size_t count); +void *memmove(void *dest, const void *src, size_t count); +void *memset(void *s, int c, size_t count); +int memcmp(const void *a, const void *b, size_t count); +int bcmp(const void *a, const void *b, size_t count); + +#endif diff --git a/crates/stela-driver-gpu/src/compat/linux/kernel/kstrtox.c b/crates/stela-driver-gpu/src/compat/linux/kernel/kstrtox.c index c64b4d8350..7b3745b818 100644 --- a/crates/stela-driver-gpu/src/compat/linux/kernel/kstrtox.c +++ b/crates/stela-driver-gpu/src/compat/linux/kernel/kstrtox.c @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT /* Linux's string-to-integer conversions, with lib/kstrtox.c's results: an - * optional '+', base 0 choosing 16 for "0x", 8 for "0" and else 10, an + * optional '+' or, for a signed result, '-', base 0 choosing 16 for "0x", 8 for "0" and else 10, an * optional "0x" for base 16, at least one digit, and at most one trailing * newline. -EINVAL for anything else, -ERANGE for a value that overflows. */ #include @@ -22,10 +22,9 @@ static bool hex_prefix(const char *s) return s[0] == '0' && (s[1] | 0x20) == 'x' && digit(s[2]) < 16; } -int kstrtoull(const char *s, unsigned int base, unsigned long long *res) +/* kstrtoull without its optional '+', which kstrtoll's '-' excludes. */ +static int parse(const char *s, unsigned int base, unsigned long long *res) { - if (*s == '+') - s++; if (base == 0) base = hex_prefix(s) ? 16 : s[0] == '0' ? 8 : 10; if (base == 16 && hex_prefix(s)) @@ -46,6 +45,29 @@ int kstrtoull(const char *s, unsigned int base, unsigned long long *res) return 0; } +int kstrtoull(const char *s, unsigned int base, unsigned long long *res) +{ + return parse(*s == '+' ? s + 1 : s, base, res); +} + +int kstrtoll(const char *s, unsigned int base, long long *res) +{ + unsigned long long magnitude; + int ret = s[0] == '-' ? parse(s + 1, base, &magnitude) : kstrtoull(s, base, &magnitude); + if (ret) + return ret; + if (s[0] != '-') { + if (magnitude > (unsigned long long)LLONG_MAX) + return -ERANGE; + *res = (long long)magnitude; + } else { + if (magnitude > (unsigned long long)LLONG_MAX + 1) + return -ERANGE; + *res = magnitude ? -(long long)(magnitude - 1) - 1 : 0; + } + return 0; +} + int kstrtou16(const char *s, unsigned int base, u16 *res) { unsigned long long value; @@ -57,3 +79,28 @@ int kstrtou16(const char *s, unsigned int base, u16 *res) *res = (u16)value; return 0; } + +#ifdef STELA_LINUX_KSTRTOX_TEST +#include + +int main(void) +{ + long long value; + unsigned long long unsigned_value; + assert(!kstrtoll("-9223372036854775808", 10, &value) && value == LLONG_MIN); + assert(!kstrtoll("9223372036854775807\n", 0, &value) && value == LLONG_MAX); + assert(kstrtoll("9223372036854775808", 10, &value) == -ERANGE); + assert(kstrtoll("-9223372036854775809", 10, &value) == -ERANGE); + assert(!kstrtoll("-0x10", 0, &value) && value == -16); + assert(!kstrtoll("+010", 0, &value) && value == 8); + assert(!kstrtoll("-0", 10, &value) && value == 0); + /* A sign is taken once: lib/kstrtox.c refuses "-+1" and "--1". */ + assert(kstrtoll("-+1", 10, &value) == -EINVAL); + assert(kstrtoll("--1", 10, &value) == -EINVAL); + assert(kstrtoll("-", 10, &value) == -EINVAL); + assert(kstrtoull("-1", 10, &unsigned_value) == -EINVAL); + assert(!kstrtoull("+0xff", 16, &unsigned_value) && unsigned_value == 0xff); + printf("Linux integer parsing: signs, bases, bounds and trailing newline passed\n"); + return 0; +} +#endif diff --git a/crates/stela-driver-gpu/src/compat/linux/kernel/string.c b/crates/stela-driver-gpu/src/compat/linux/kernel/string.c deleted file mode 100644 index 0193f6db86..0000000000 --- a/crates/stela-driver-gpu/src/compat/linux/kernel/string.c +++ /dev/null @@ -1,61 +0,0 @@ -// SPDX-License-Identifier: MIT -/* Linux's string functions, with lib/string.c's results. memcpy, memmove, - * memset and memcmp come from the platform's compiler runtime, as the - * compiler itself emits calls to them. */ -#include - -size_t strlen(const char *s) -{ - const char *end = s; - while (*end) - end++; - return (size_t)(end - s); -} - -size_t strnlen(const char *s, size_t count) -{ - size_t length = 0; - while (length < count && s[length]) - length++; - return length; -} - -int strcmp(const char *cs, const char *ct) -{ - unsigned char a, b; - do { - a = (unsigned char)*cs++; - b = (unsigned char)*ct++; - } while (a == b && a); - return a == b ? 0 : a < b ? -1 : 1; -} - -char *strstr(const char *s1, const char *s2) -{ - size_t length = strlen(s2); - for (; length; s1++) { - if (!*s1) - return NULL; - if (!memcmp(s1, s2, length)) - break; - } - return (char *)s1; -} - -char *strsep(char **s, const char *ct) -{ - char *token = *s; - if (!token) - return NULL; - for (char *c = token; *c; c++) { - for (const char *delimiter = ct; *delimiter; delimiter++) { - if (*c == *delimiter) { - *c = '\0'; - *s = c + 1; - return token; - } - } - } - *s = NULL; - return token; -} diff --git a/crates/stela-driver-gpu/src/compat/linux/kernel/string_helpers.c b/crates/stela-driver-gpu/src/compat/linux/kernel/string_helpers.c index c238427736..b337f14f94 100644 --- a/crates/stela-driver-gpu/src/compat/linux/kernel/string_helpers.c +++ b/crates/stela-driver-gpu/src/compat/linux/kernel/string_helpers.c @@ -1,8 +1,8 @@ // SPDX-License-Identifier: GPL-2.0-only /* Owned common string helpers following Linux - * cc9de5d4b5ab6459f5774946aa3195ad51236fa8 lib/string.c and - * lib/string_helpers.c. Their original GPL notices remain with those sources. - * Existing MIT kernel/string.c is unchanged. */ + * cc9de5d4b5ab6459f5774946aa3195ad51236fa8 lib/string_helpers.c. Its + * original GPL notices remain with that source; lib/string.c itself is + * compiled unchanged. */ #include #include @@ -23,41 +23,6 @@ char *skip_spaces(const char *str) } -int strncmp(const char *left, const char *right, size_t count) -{ - for (size_t i = 0; i < count; i++) { - unsigned char a = (unsigned char)left[i]; - unsigned char b = (unsigned char)right[i]; - if (a != b) - return a < b ? -1 : 1; - if (!a) - break; - } - return 0; -} - -char *strchr(const char *string, int character) -{ - for (;;) { - if (*string == (char)character) - return (char *)string; - if (!*string) - return NULL; - string++; - } -} - -char *strnchr(const char *string, size_t count, int character) -{ - for (size_t i = 0; i < count; i++) { - if (string[i] == (char)character) - return (char *)(string + i); - if (!string[i]) - break; - } - return NULL; -} - char *strreplace(char *string, char old, char replacement) { char *cursor = string; @@ -107,7 +72,8 @@ static void leading_whitespace(void) int main(void) { leading_whitespace(); - /* Exercise the actual implementation, not compiler-folded libc calls. */ + /* Exercise lib/string.c's implementations, not compiler-folded libc + * calls. */ int (*volatile compare)(const char *, const char *, size_t) = strncmp; char *(*volatile find)(const char *, int) = strchr; char *(*volatile bounded_find)(const char *, size_t, int) = strnchr; diff --git a/third_party/linux.sparse b/third_party/linux.sparse index abdaf5812f..5fe0a58c05 100644 --- a/third_party/linux.sparse +++ b/third_party/linux.sparse @@ -18,6 +18,7 @@ /lib/kobject.c /lib/kobject_uevent.c /lib/ctype.c +/lib/string.c /lib/bitrev.c /lib/find_bit.c /lib/find_random_bit.c