~skeeto/public-inbox

1

flags idea for newthread

R Samuel Klatchko <rsk@moocat.org>
Details
Message ID
<CAM6f_1=caPvXg10q9ViwNU0SCGwsJADim47k80qeRzeAjoiQMw@mail.gmail.com>
DKIM signature
missing
Download raw message
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);
    }
Details
Message ID
<20230411000716.jl3zcj6nzy4zqhft@nullprogram.com>
In-Reply-To
<CAM6f_1=caPvXg10q9ViwNU0SCGwsJADim47k80qeRzeAjoiQMw@mail.gmail.com> (view parent)
DKIM signature
missing
Download raw message
Clever idea and good thinking! I like it. Next time I need it I might take 
this route.
Reply to thread Export thread (mbox)