[PATCH] um: make stub_exe _start() pure inline asm

From: Johannes Berg
Date: Tue Oct 22 2024 - 05:48:21 EST


Since __attribute__((naked)) cannot be used with functions
containing C statements, just generate the few instructions
it needs in assembly directly.

Fixes: 8508a5e0e9db ("um: Fix misaligned stack in stub_exe")
Link: https://lore.kernel.org/linux-um/CABVgOSntH-uoOFMP5HwMXjx_f1osMnVdhgK=
RKm4uz6DFm2Lb8Q@xxxxxxxxxxxxxx/
Signed-off-by: Johannes Berg <johannes.berg@xxxxxxxxx>
---
arch/um/kernel/skas/stub_exe.c | 8 +-------
arch/x86/um/shared/sysdep/stub_32.h | 8 ++++++++
arch/x86/um/shared/sysdep/stub_64.h | 8 ++++++++
3 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/arch/um/kernel/skas/stub_exe.c b/arch/um/kernel/skas/stub_exe.=
c
index 722ce6267476..a61f9c008233 100644
--- a/arch/um/kernel/skas/stub_exe.c
+++ b/arch/um/kernel/skas/stub_exe.c
@@ -81,11 +81,5 @@ noinline static void real_init(void)
=20
__attribute__((naked)) void _start(void)
{
- char *alloc;
-
- /* Make enough space for the stub (including space for alignment) */
- alloc =3D __builtin_alloca((1 + 2 * STUB_DATA_PAGES - 1) * UM_KERN_PAGE_S=
IZE);
- asm volatile("" : "+r,m"(alloc) : : "memory");
-
- real_init();
+ stub_start(real_init);
}
diff --git a/arch/x86/um/shared/sysdep/stub_32.h b/arch/x86/um/shared/sysde=
p/stub_32.h
index 631a18d0ff44..760e8ce8093f 100644
--- a/arch/x86/um/shared/sysdep/stub_32.h
+++ b/arch/x86/um/shared/sysdep/stub_32.h
@@ -123,4 +123,12 @@ static __always_inline void *get_stub_data(void)
=20
return (void *)ret;
}
+
+#define stub_start(fn) \
+ asm volatile ( \
+ "subl %0,%%esp ;" \
+ "movl %1, %%eax ; " \
+ "call *%%eax ;" \
+ :: "i" ((STUB_DATA_PAGES + 1) * UM_KERN_PAGE_SIZE), \
+ "i" (&fn))
#endif
diff --git a/arch/x86/um/shared/sysdep/stub_64.h b/arch/x86/um/shared/sysde=
p/stub_64.h
index 17153dfd780a..148bf423289e 100644
--- a/arch/x86/um/shared/sysdep/stub_64.h
+++ b/arch/x86/um/shared/sysdep/stub_64.h
@@ -126,4 +126,12 @@ static __always_inline void *get_stub_data(void)
=20
return (void *)ret;
}
+
+#define stub_start(fn) \
+ asm volatile ( \
+ "subq %0,%%rsp ;" \
+ "movq %1,%%rax ;" \
+ "call *%%rax ;" \
+ :: "i" ((STUB_DATA_PAGES + 1) * UM_KERN_PAGE_SIZE), \
+ "i" (&fn))
#endif
--=20
2.47.0