Skip to content

Commit 75133a5

Browse files
authored
Merge pull request #590 from tavip/lkl-rm-str-dup
lkl: remove string functions duplicate implementation
2 parents 0d9fd6f + 15a7be0 commit 75133a5

8 files changed

Lines changed: 60 additions & 49 deletions

File tree

arch/lkl/Kconfig

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,24 @@ config BUILTIN_CMDLINE
182182
image and used at boot time. The command line provided to
183183
lkl_start_kernel is appended to this string to form the full
184184
command line.
185+
186+
config LKL_HOST_MEMCPY
187+
bool "Host provides memcpy"
188+
default n
189+
help
190+
This options should be set (in tools/lkl/Makefile.autoconf)
191+
if the host provides a memcpy implementation.
192+
193+
config LKL_HOST_MEMSET
194+
bool "Host provides memset"
195+
default n
196+
help
197+
This options should be set (in tools/lkl/Makefile.autoconf)
198+
if the host provides a memset implementation.
199+
200+
config LKL_HOST_MEMMOVE
201+
bool "Host provides memmove"
202+
default n
203+
help
204+
This options should be set (in tools/lkl/Makefile.autoconf)
205+
if the host provides a memmove implementation.

arch/lkl/include/asm/string.h

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,70 +7,35 @@
77

88
/* use __mem* names to avoid conflict with KASAN's mem* functions. */
99

10+
#ifdef CONFIG_LKL_HOST_MEMCPY
1011
#define __HAVE_ARCH_MEMCPY
1112
extern void *memcpy(void *dest, const void *src, size_t count);
1213
static inline void *__memcpy(void *dest, const void *src, size_t count)
1314
{
14-
char *tmp = dest;
15-
const char *s = src;
16-
17-
if (lkl_ops->memcpy)
18-
return lkl_ops->memcpy(dest, src, count);
19-
20-
/* from lib/string.c */
21-
22-
while (count--)
23-
*tmp++ = *s++;
24-
return dest;
15+
return lkl_ops->memcpy(dest, src, count);
2516
}
17+
#define memcpy(dst, src, len) __memcpy(dst, src, len)
18+
#endif
2619

20+
#ifdef CONFIG_LKL_HOST_MEMSET
2721
#define __HAVE_ARCH_MEMSET
2822
extern void *memset(void *s, int c, size_t count);
2923
static inline void *__memset(void *s, int c, size_t count)
3024
{
31-
char *xs = s;
32-
33-
if (lkl_ops->memset)
34-
return lkl_ops->memset(s, c, count);
35-
36-
/* from lib/string.c */
37-
38-
while (count--)
39-
*xs++ = c;
40-
return s;
25+
return lkl_ops->memset(s, c, count);
4126
}
27+
#define memset(s, c, n) __memset(s, c, n)
28+
#endif
4229

30+
#ifdef CONFIG_LKL_HOST_MEMSET
4331
#define __HAVE_ARCH_MEMMOVE
4432
extern void *memmove(void *dest, const void *src, size_t count);
4533
static inline void *__memmove(void *dest, const void *src, size_t count)
4634
{
47-
char *tmp;
48-
const char *s;
49-
50-
if (lkl_ops->memmove)
51-
return lkl_ops->memmove(dest, src, count);
52-
53-
/* from lib/string.c */
54-
55-
if (dest <= src) {
56-
tmp = dest;
57-
s = src;
58-
while (count--)
59-
*tmp++ = *s++;
60-
} else {
61-
tmp = dest;
62-
tmp += count;
63-
s = src;
64-
s += count;
65-
while (count--)
66-
*--tmp = *--s;
67-
}
68-
return dest;
35+
return lkl_ops->memmove(dest, src, count);
6936
}
70-
71-
#define memcpy(dst, src, len) __memcpy(dst, src, len)
72-
#define memset(s, c, n) __memset(s, c, n)
7337
#define memmove(dst, src, len) __memmove(dst, src, len)
38+
#endif
7439

7540
#if defined(CONFIG_KASAN)
7641

arch/lkl/kernel/init.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44

55
int lkl_init(struct lkl_host_operations *ops)
66
{
7+
if ((IS_ENABLED(CONFIG_LKL_HOST_MEMCPY) && !ops->memcpy)
8+
|| (IS_ENABLED(CONFIG_LKL_HOST_MEMSET) && !ops->memset)
9+
|| (IS_ENABLED(CONFIG_LKL_HOST_MEMMOVE) && !ops->memmove)) {
10+
lkl_printf("unexpected NULL lkl_host_ops member\n");
11+
return -1;
12+
}
13+
714
lkl_ops = ops;
815

916
return kasan_init();

arch/lkl/lib/string.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,28 @@
1414
#undef memset
1515
#undef memmove
1616

17+
#ifdef __HAVE_ARCH_MEMCPY
1718
__visible void *memcpy(void *dest, const void *src, size_t count)
1819
{
1920
return __memcpy(dest, src, count);
2021
}
2122
EXPORT_SYMBOL(memcpy);
23+
#endif
2224

25+
#ifdef __HAVE_ARCH_MEMSET
2326
__visible void *memset(void *s, int c, size_t count)
2427
{
2528
return __memset(s, c, count);
2629
}
2730
EXPORT_SYMBOL(memset);
31+
#endif
2832

33+
#ifdef __HAVE_ARCH_MEMMOVE
2934
__visible void *memmove(void *dest, const void *src, size_t count)
3035
{
3136
return __memmove(dest, src, count);
3237
}
3338
EXPORT_SYMBOL(memmove);
39+
#endif
3440

3541
#endif

tools/lkl/Makefile.autoconf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ define posix_host
8989
$(if $(filter $(1),elf32-i386),$(call set_autoconf_var,I386,y))
9090
$(if $(strip $(call find_include,jsmn.h)),$(call set_autoconf_var,JSMN,y))
9191
$(if $(filter %,$(zpoline)),$(call zpoline_conf,$(zpoline)))
92+
$(call set_kernel_config,LKL_HOST_MEMCPY,y)
93+
$(call set_kernel_config,LKL_HOST_MEMSET,y)
94+
$(call set_kernel_config,LKL_HOST_MEMMOVE,y)
9295
endef
9396

9497
define nt64_host

tools/lkl/tests/boot.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,10 @@ int main(int argc, const char **argv)
780780

781781
lkl_host_ops.print = lkl_test_log;
782782

783-
lkl_init(&lkl_host_ops);
783+
if (lkl_init(&lkl_host_ops) < 0) {
784+
printf("%s\n", lkl_test_get_log());
785+
return 1;
786+
}
784787

785788
ret = lkl_test_run(tests, sizeof(tests)/sizeof(struct lkl_test),
786789
"boot");

tools/lkl/tests/disk-vfio-pci.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,10 @@ int main(int argc, const char **argv)
109109

110110
lkl_host_ops.print = lkl_test_log;
111111

112-
lkl_init(&lkl_host_ops);
112+
if (lkl_init(&lkl_host_ops) < 0) {
113+
printf("%s\n", lkl_test_get_log());
114+
return 1;
115+
}
113116

114117
ret = lkl_test_run(tests, sizeof(tests) / sizeof(struct lkl_test),
115118
"disk-vfio-pci %s", cla.fstype);

tools/lkl/tests/disk.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,10 @@ int main(int argc, const char **argv)
189189

190190
lkl_host_ops.print = lkl_test_log;
191191

192-
lkl_init(&lkl_host_ops);
192+
if (lkl_init(&lkl_host_ops) < 0) {
193+
printf("%s\n", lkl_test_get_log());
194+
return 1;
195+
}
193196

194197
ret = lkl_test_run(tests, sizeof(tests)/sizeof(struct lkl_test),
195198
"disk %s", cla.fstype);

0 commit comments

Comments
 (0)