Hi skeeto-
I think there's an elegant solution to the hardcoded flags due to
naked function issue. The idea is a second wrapper function to compute
the flags and along with a specific inner function signature so it can
drop two of the six lines of assembly by taking advantage of the fact
that "both function calls and system calls use rdi and rsi for their
first two parameters".
That said, I haven't written any assembly so I may be missing something.
__attribute((naked))
static long newthread_naked(long flags, struct stack_head *stack)
{
// rdi/rsi already correctly set by virtue of the
newthread_naked signature
__asm volatile (
"mov $56, %%eax\n" // SYS_clone
"syscall\n"
"mov %%rsp, %%rdi\n" // entry point argument
"ret\n"
: : : "rax", "rcx", "rsi", "rdi", "r11", "memory"
);
}
static long newthread(struct stack_head *stack)
{
// Single statement so gcc will precompute even at -O0
long flags = CLONE_FILES
| CLONE_FS
| CLONE_SIGHAND
| CLONE_SYSVSEM
| CLONE_THREAD
| CLONE_VM;
return newthread_naked(flags, stack);
}