Skip to contentSkip to conversation

updated x86_64 eclone() stub

DISCUSSION Dave Hansen 2 participants 6 messages Dec 4, 2009 2 messages not in the archive

lore
Share & tools
lore
#0 lore raw

This more closely resembles what glibc does.

The last version had a push/popq %ebp. But, after looking at the glibc code, I believe this to be unnecessary on 64-bit. Suka also pointed out that we were neglecting to pull the subthread's function argument off the stack.

I also changed the register being used for the subthread variable to be rax. This is just to more closely resemble the glibc code. It doesn't hurt to use it this way mostly because we overwrite it for the exit syscall anyway.

int eclone(long flags_low, struct clone_args *clone_args, long args_size,
int *pids)

{
        long retval;

        __asm__  __volatile__(
                 "movq %5, %%r10\n\t"   /* pids in r10*/
                 "syscall\n\t"          /* Linux/x86_64 system call */
                 "testq %0,%0\n\t"      /* check return value */
                 "jne 1f\n\t"           /* jump if parent */
                 "popq %%rax\n\t"       /* get subthread function */
                 "popq %%rdi\n\t"       /* get the subthread function arg */
                 "call *%%rax\n\t"      /* start subthread function */
                 "movq %6,%0\n\t"
                 "syscall\n"            /* exit system call: exit subthread */
                 "1:\n\t"
                :"=a" (retval)
                :"0" (__NR_clone3),/* eax */
                 "D" (flags_low),  /* rdi */
                 "S" (clone_args), /* rsi */
                 "d" (args_size),  /* rdx */
                 "m" (pids),       /* gets moved to r10 */
                 "i" (__NR_exit)
                :"rbx", "rcx", "r8", "r9", "r10"
        );

        if (retval < 0) {
                errno = -retval;
                retval = -1;
        }
        return retval;
}

-- Dave

#1 lore raw
Quoted 28 lines · Dave Hansen

This more closely resembles what glibc does.

The last version had a push/popq %ebp. But, after looking at the glibc code, I believe this to be unnecessary on 64-bit. Suka also pointed out that we were neglecting to pull the subthread's function argument off the stack.

I also changed the register being used for the subthread variable to be rax. This is just to more closely resemble the glibc code. It doesn't hurt to use it this way mostly because we overwrite it for the exit syscall anyway.

int eclone(long flags_low, struct clone_args *clone_args, long args_size,
int *pids)

{
        long retval;

        __asm__  __volatile__(
                 "movq %5, %%r10\n\t"   /* pids in r10*/
                 "syscall\n\t"          /* Linux/x86_64 system call */
                 "testq %0,%0\n\t"      /* check return value */
                 "jne 1f\n\t"           /* jump if parent */
                 "popq %%rax\n\t"       /* get subthread function */
                 "popq %%rdi\n\t"       /* get the subthread function arg */
                 "call *%%rax\n\t"      /* start subthread function */
                 "movq %6,%0\n\t"
                 "syscall\n"            /* exit system call: exit subthread */
                 "1:\n\t"
Quoted 8 lines
                :"=a" (retval)
                :"0" (__NR_clone3),/* eax */
                 "D" (flags_low),  /* rdi */
                 "S" (clone_args), /* rsi */
                 "d" (args_size),  /* rdx */
                 "m" (pids),       /* gets moved to r10 */
                 "i" (__NR_exit)
                :"rbx", "rcx", "r8", "r9", "r10"

Why is rbx in the clobber list? It's not used at all.

r8 and r9 can be removed from the clobber list, since they can only be clobbered when calling the subthread, and then exit() is called without returning to C.

syscall also destroys r11, so it should be added to the clobber list.

Libc also adds "cc" to the clobber list. I'm not sure that this matters though.

So the result could be:

                 :"rcx", "r10", "r11", "cc"

Thanks,

Louis

Quoted 15 lines
        );

        if (retval < 0) {
                errno = -retval;
                retval = -1;
        }
        return retval;
}

-- Dave

_______________________________________________
Containers mailing list Containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org
https://lists.linux-foundation.org/mailman/listinfo/containers

Signature · 4 lines
Dr Louis Rilling			Kerlabs
Skype: louis.rilling			Batiment Germanium
Phone: (+33|0) 6 80 89 08 23		80 avenue des Buttes de Coesmes
http://www.kerlabs.com/			35700 Rennes

_______________________________________________
Containers mailing list Containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org
https://lists.linux-foundation.org/mailman/listinfo/containers

A message that is not in the archive (sent off-list or not yet received): 20091204160123.GJ2430-Hu8+6S1rdjywhHL9vcZdMVaTQe2KTcn/@public.gmane.org

#3 lore raw
Quoted 38 lines · Louis Rilling
Quoted 28 lines · Dave Hansen

This more closely resembles what glibc does.

The last version had a push/popq %ebp. But, after looking at the glibc code, I believe this to be unnecessary on 64-bit. Suka also pointed out that we were neglecting to pull the subthread's function argument off the stack.

I also changed the register being used for the subthread variable to be rax. This is just to more closely resemble the glibc code. It doesn't hurt to use it this way mostly because we overwrite it for the exit syscall anyway.

int eclone(long flags_low, struct clone_args *clone_args, long args_size,
int *pids)

{
        long retval;

        __asm__  __volatile__(
                 "movq %5, %%r10\n\t"   /* pids in r10*/
                 "syscall\n\t"          /* Linux/x86_64 system call */
                 "testq %0,%0\n\t"      /* check return value */
                 "jne 1f\n\t"           /* jump if parent */
                 "popq %%rax\n\t"       /* get subthread function */
                 "popq %%rdi\n\t"       /* get the subthread function arg */
                 "call *%%rax\n\t"      /* start subthread function */
                 "movq %6,%0\n\t"
                 "syscall\n"            /* exit system call: exit subthread */
                 "1:\n\t"
Quoted 8 lines
                :"=a" (retval)
                :"0" (__NR_clone3),/* eax */
                 "D" (flags_low),  /* rdi */
                 "S" (clone_args), /* rsi */
                 "d" (args_size),  /* rdx */
                 "m" (pids),       /* gets moved to r10 */
                 "i" (__NR_exit)
                :"rbx", "rcx", "r8", "r9", "r10"
Quoted 1 line

Why is rbx in the clobber list? It's not used at all.

Because I forgot to take it out when I removed the popq into rbx. :)

Quoted 2 lines

r8 and r9 can be removed from the clobber list, since they can only be clobbered when calling the subthread, and then exit() is called without returning to C.

OK, that makes sense.

Quoted 1 line

syscall also destroys r11, so it should be added to the clobber list.

Even though it is a ptregscall?

Quoted 1 line

Libc also adds "cc" to the clobber list. I'm not sure that this matters though.

I guess it can't hurt. I'll add a comment about it.

-- Dave

#4 lore raw
Quoted 3 lines · Dave Hansen
Quoted 1 line

syscall also destroys r11, so it should be added to the clobber list.

Even though it is a ptregscall?

The assembly instruction itself destroys r11 (same for rcx).

Thanks,

Louis

Signature · 4 lines
Dr Louis Rilling			Kerlabs
Skype: louis.rilling			Batiment Germanium
Phone: (+33|0) 6 80 89 08 23		80 avenue des Buttes de Coesmes
http://www.kerlabs.com/			35700 Rennes

_______________________________________________
Containers mailing list Containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org
https://lists.linux-foundation.org/mailman/listinfo/containers

A message that is not in the archive (sent off-list or not yet received): 20091204160825.GL2430-Hu8+6S1rdjywhHL9vcZdMVaTQe2KTcn/@public.gmane.org

#6 lore raw
Quoted 6 lines · Louis Rilling
Quoted 3 lines · Dave Hansen
Quoted 1 line

syscall also destroys r11, so it should be added to the clobber list.

Even though it is a ptregscall?

The assembly instruction itself destroys r11 (same for rcx).

Thanks again for the help, Louis. How does this look?

int clone_with_pids(long flags_low, struct clone_args *clone_args, long args_size,
int *pids)

{
        long retval;

        __asm__  __volatile__(
                 "movq %5, %%r10\n\t"   /* pids in r10*/
                 "syscall\n\t"          /* Linux/x86_64 system call */
                 "testq %0,%0\n\t"      /* check return value */
                 "jne 1f\n\t"           /* jump if parent */
                 "popq %%rax\n\t"       /* get subthread function */
                 "popq %%rdi\n\t"       /* get the subthread function arg */
                 "call *%%rax\n\t"      /* start subthread function */
                 "movq %6,%0\n\t"
                 "syscall\n"            /* exit system call: exit subthread */
                 "1:\n\t"
                :"=a" (retval)
                :"0" (__NR_clone3),/* eax */
                 "D" (flags_low),  /* rdi */
                 "S" (clone_args), /* rsi */
                 "d" (args_size),  /* rdx */
                 "m" (pids),       /* gets moved to r10 */
                 "i" (__NR_exit)
                :"rcx", "r10", "r11", "cc" 
        );
        /*
         * glibc lists 'cc' as clobbered, so we might as
	 * well do it too.  'r11' and 'rcx' are clobbered
	 * by the 'syscall' instruction itself.  'r8' and
	 * 'r9' are clobbered by the clone, but that
	 * thread will exit before getting back out to C.
         */

        if (retval < 0) {
                errno = -retval;
                retval = -1;
        }
        return retval;
}
Quoted 4 lines

Thanks,

Louis

-- Dave

#7 lore raw
Quoted 9 lines · Dave Hansen
Quoted 6 lines · Louis Rilling
Quoted 3 lines · Dave Hansen
Quoted 1 line

syscall also destroys r11, so it should be added to the clobber list.

Even though it is a ptregscall?

The assembly instruction itself destroys r11 (same for rcx).

Thanks again for the help, Louis. How does this look?

Looks ok :)

Thanks,

Louis

Quoted 49 lines

int clone_with_pids(long flags_low, struct clone_args *clone_args, long args_size,
int *pids)

{
        long retval;

        __asm__  __volatile__(
                 "movq %5, %%r10\n\t"   /* pids in r10*/
                 "syscall\n\t"          /* Linux/x86_64 system call */
                 "testq %0,%0\n\t"      /* check return value */
                 "jne 1f\n\t"           /* jump if parent */
                 "popq %%rax\n\t"       /* get subthread function */
                 "popq %%rdi\n\t"       /* get the subthread function arg */
                 "call *%%rax\n\t"      /* start subthread function */
                 "movq %6,%0\n\t"
                 "syscall\n"            /* exit system call: exit subthread */
                 "1:\n\t"
                :"=a" (retval)
                :"0" (__NR_clone3),/* eax */
                 "D" (flags_low),  /* rdi */
                 "S" (clone_args), /* rsi */
                 "d" (args_size),  /* rdx */
                 "m" (pids),       /* gets moved to r10 */
                 "i" (__NR_exit)
                :"rcx", "r10", "r11", "cc" 
        );
        /*
         * glibc lists 'cc' as clobbered, so we might as
	 * well do it too.  'r11' and 'rcx' are clobbered
	 * by the 'syscall' instruction itself.  'r8' and
	 * 'r9' are clobbered by the clone, but that
	 * thread will exit before getting back out to C.
         */

        if (retval < 0) {
                errno = -retval;
                retval = -1;
        }
        return retval;
}
Quoted 4 lines

Thanks,

Louis

-- Dave

Signature · 4 lines
Dr Louis Rilling			Kerlabs
Skype: louis.rilling			Batiment Germanium
Phone: (+33|0) 6 80 89 08 23		80 avenue des Buttes de Coesmes
http://www.kerlabs.com/			35700 Rennes

_______________________________________________
Containers mailing list Containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org
https://lists.linux-foundation.org/mailman/listinfo/containers