Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions arch/lkl/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,24 @@ config BUILTIN_CMDLINE
image and used at boot time. The command line provided to
lkl_start_kernel is appended to this string to form the full
command line.

config LKL_HOST_MEMCPY
bool "Host provides memcpy"
default n
help
This options should be set (in tools/lkl/Makefile.autoconf)
if the host provides a memcpy implementation.

config LKL_HOST_MEMSET
bool "Host provides memset"
default n
help
This options should be set (in tools/lkl/Makefile.autoconf)
if the host provides a memset implementation.

config LKL_HOST_MEMMOVE
bool "Host provides memmove"
default n
help
This options should be set (in tools/lkl/Makefile.autoconf)
if the host provides a memmove implementation.
57 changes: 11 additions & 46 deletions arch/lkl/include/asm/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,70 +7,35 @@

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

#ifdef CONFIG_LKL_HOST_MEMCPY
#define __HAVE_ARCH_MEMCPY
extern void *memcpy(void *dest, const void *src, size_t count);
static inline void *__memcpy(void *dest, const void *src, size_t count)
{
char *tmp = dest;
const char *s = src;

if (lkl_ops->memcpy)
return lkl_ops->memcpy(dest, src, count);

/* from lib/string.c */

while (count--)
*tmp++ = *s++;
return dest;
return lkl_ops->memcpy(dest, src, count);
}
#define memcpy(dst, src, len) __memcpy(dst, src, len)
#endif

#ifdef CONFIG_LKL_HOST_MEMSET
#define __HAVE_ARCH_MEMSET
extern void *memset(void *s, int c, size_t count);
static inline void *__memset(void *s, int c, size_t count)
{
char *xs = s;

if (lkl_ops->memset)
return lkl_ops->memset(s, c, count);

/* from lib/string.c */

while (count--)
*xs++ = c;
return s;
return lkl_ops->memset(s, c, count);
}
#define memset(s, c, n) __memset(s, c, n)
#endif

#ifdef CONFIG_LKL_HOST_MEMSET
#define __HAVE_ARCH_MEMMOVE
extern void *memmove(void *dest, const void *src, size_t count);
static inline void *__memmove(void *dest, const void *src, size_t count)
{
char *tmp;
const char *s;

if (lkl_ops->memmove)
return lkl_ops->memmove(dest, src, count);

/* from lib/string.c */

if (dest <= src) {
tmp = dest;
s = src;
while (count--)
*tmp++ = *s++;
} else {
tmp = dest;
tmp += count;
s = src;
s += count;
while (count--)
*--tmp = *--s;
}
return dest;
return lkl_ops->memmove(dest, src, count);
}

#define memcpy(dst, src, len) __memcpy(dst, src, len)
#define memset(s, c, n) __memset(s, c, n)
#define memmove(dst, src, len) __memmove(dst, src, len)
#endif

#if defined(CONFIG_KASAN)

Expand Down
7 changes: 7 additions & 0 deletions arch/lkl/kernel/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

int lkl_init(struct lkl_host_operations *ops)
{
if ((IS_ENABLED(CONFIG_LKL_HOST_MEMCPY) && !ops->memcpy)
|| (IS_ENABLED(CONFIG_LKL_HOST_MEMSET) && !ops->memset)
|| (IS_ENABLED(CONFIG_LKL_HOST_MEMMOVE) && !ops->memmove)) {
lkl_printf("unexpected NULL lkl_host_ops member\n");
return -1;
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

coding-style.rst encourages avoiding #ifdef CONFIG_... outside of header files. How about something like:

        if ((IS_ENABLED(CONFIG_LKL_HOST_MEMCPY) && !ops->memcpy)
         || (IS_ENABLED(CONFIG_LKL_HOST_MEMSET) && !ops->memset)
         || (IS_ENABLED(CONFIG_LKL_HOST_MEMMOVE) && !ops->memmove)) {
                lkl_printf("unexpected NULL lkl_host_ops memory I/O call\n");
                return -1;
        }

The compiler should still be able to optimize it out for non-posix hosts.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, thanks!

lkl_ops = ops;

return kasan_init();
Expand Down
6 changes: 6 additions & 0 deletions arch/lkl/lib/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,28 @@
#undef memset
#undef memmove

#ifdef __HAVE_ARCH_MEMCPY
__visible void *memcpy(void *dest, const void *src, size_t count)
{
return __memcpy(dest, src, count);
}
EXPORT_SYMBOL(memcpy);
#endif

#ifdef __HAVE_ARCH_MEMSET
__visible void *memset(void *s, int c, size_t count)
{
return __memset(s, c, count);
}
EXPORT_SYMBOL(memset);
#endif

#ifdef __HAVE_ARCH_MEMMOVE
__visible void *memmove(void *dest, const void *src, size_t count)
{
return __memmove(dest, src, count);
}
EXPORT_SYMBOL(memmove);
#endif

#endif
3 changes: 3 additions & 0 deletions tools/lkl/Makefile.autoconf
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ define posix_host
$(if $(filter $(1),elf32-i386),$(call set_autoconf_var,I386,y))
$(if $(strip $(call find_include,jsmn.h)),$(call set_autoconf_var,JSMN,y))
$(if $(filter %,$(zpoline)),$(call zpoline_conf,$(zpoline)))
$(call set_kernel_config,LKL_HOST_MEMCPY,y)
$(call set_kernel_config,LKL_HOST_MEMSET,y)
$(call set_kernel_config,LKL_HOST_MEMMOVE,y)
endef

define nt64_host
Expand Down
5 changes: 4 additions & 1 deletion tools/lkl/tests/boot.c
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,10 @@ int main(int argc, const char **argv)

lkl_host_ops.print = lkl_test_log;

lkl_init(&lkl_host_ops);
if (lkl_init(&lkl_host_ops) < 0) {
printf("%s\n", lkl_test_get_log());
return 1;
}

ret = lkl_test_run(tests, sizeof(tests)/sizeof(struct lkl_test),
"boot");
Expand Down
5 changes: 4 additions & 1 deletion tools/lkl/tests/disk-vfio-pci.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ int main(int argc, const char **argv)

lkl_host_ops.print = lkl_test_log;

lkl_init(&lkl_host_ops);
if (lkl_init(&lkl_host_ops) < 0) {
printf("%s\n", lkl_test_get_log());
return 1;
}

ret = lkl_test_run(tests, sizeof(tests) / sizeof(struct lkl_test),
"disk-vfio-pci %s", cla.fstype);
Expand Down
5 changes: 4 additions & 1 deletion tools/lkl/tests/disk.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,10 @@ int main(int argc, const char **argv)

lkl_host_ops.print = lkl_test_log;

lkl_init(&lkl_host_ops);
if (lkl_init(&lkl_host_ops) < 0) {
printf("%s\n", lkl_test_get_log());
return 1;
}

ret = lkl_test_run(tests, sizeof(tests)/sizeof(struct lkl_test),
"disk %s", cla.fstype);
Expand Down
Loading