Linux Container Development
 help / color / mirror / Atom feed
* updated x86_64 eclone() stub
@ 2009-12-04 15:29 Dave Hansen
  2009-12-04 16:01 ` Louis Rilling
  0 siblings, 1 reply; 6+ messages in thread
From: Dave Hansen @ 2009-12-04 15:29 UTC (permalink / raw)
  To: containers

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: updated x86_64 eclone() stub
  2009-12-04 15:29 updated x86_64 eclone() stub Dave Hansen
@ 2009-12-04 16:01 ` Louis Rilling
       [not found]   ` <20091204160123.GJ2430-Hu8+6S1rdjywhHL9vcZdMVaTQe2KTcn/@public.gmane.org>
  0 siblings, 1 reply; 6+ messages in thread
From: Louis Rilling @ 2009-12-04 16:01 UTC (permalink / raw)
  To: Dave Hansen; +Cc: containers


[-- Attachment #1.1: Type: text/plain, Size: 2825 bytes --]

On 04/12/09  7:29 -0800, Dave Hansen wrote:
> 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"

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


>         );
> 
>         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

-- 
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

[-- Attachment #1.2: Digital signature --]
[-- Type: application/pgp-signature, Size: 197 bytes --]

[-- Attachment #2: Type: text/plain, Size: 206 bytes --]

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: updated x86_64 eclone() stub
       [not found]   ` <20091204160123.GJ2430-Hu8+6S1rdjywhHL9vcZdMVaTQe2KTcn/@public.gmane.org>
@ 2009-12-04 16:05     ` Dave Hansen
  2009-12-04 16:08       ` Louis Rilling
  0 siblings, 1 reply; 6+ messages in thread
From: Dave Hansen @ 2009-12-04 16:05 UTC (permalink / raw)
  To: Louis Rilling; +Cc: containers

On Fri, 2009-12-04 at 17:01 +0100, Louis Rilling wrote:
> On 04/12/09  7:29 -0800, Dave Hansen wrote:
> > 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"
> 
> 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. :)

> 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.

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

Even though it is a ptregscall?

> 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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: updated x86_64 eclone() stub
  2009-12-04 16:05     ` Dave Hansen
@ 2009-12-04 16:08       ` Louis Rilling
       [not found]         ` <20091204160825.GL2430-Hu8+6S1rdjywhHL9vcZdMVaTQe2KTcn/@public.gmane.org>
  0 siblings, 1 reply; 6+ messages in thread
From: Louis Rilling @ 2009-12-04 16:08 UTC (permalink / raw)
  To: Dave Hansen; +Cc: containers


[-- Attachment #1.1: Type: text/plain, Size: 424 bytes --]

On 04/12/09  8:05 -0800, Dave Hansen wrote:
> > 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

-- 
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

[-- Attachment #1.2: Digital signature --]
[-- Type: application/pgp-signature, Size: 197 bytes --]

[-- Attachment #2: Type: text/plain, Size: 206 bytes --]

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: updated x86_64 eclone() stub
       [not found]         ` <20091204160825.GL2430-Hu8+6S1rdjywhHL9vcZdMVaTQe2KTcn/@public.gmane.org>
@ 2009-12-04 16:21           ` Dave Hansen
  2009-12-04 17:24             ` Louis Rilling
  0 siblings, 1 reply; 6+ messages in thread
From: Dave Hansen @ 2009-12-04 16:21 UTC (permalink / raw)
  To: Louis Rilling; +Cc: containers

On Fri, 2009-12-04 at 17:08 +0100, Louis Rilling wrote:
> On 04/12/09  8:05 -0800, Dave Hansen wrote:
> > > 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;
}



> Thanks,
> 
> Louis
> 
-- Dave

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: updated x86_64 eclone() stub
  2009-12-04 16:21           ` Dave Hansen
@ 2009-12-04 17:24             ` Louis Rilling
  0 siblings, 0 replies; 6+ messages in thread
From: Louis Rilling @ 2009-12-04 17:24 UTC (permalink / raw)
  To: Dave Hansen; +Cc: containers


[-- Attachment #1.1: Type: text/plain, Size: 2352 bytes --]

On 04/12/09  8:21 -0800, Dave Hansen wrote:
> On Fri, 2009-12-04 at 17:08 +0100, Louis Rilling wrote:
> > On 04/12/09  8:05 -0800, Dave Hansen wrote:
> > > > 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

> 
> 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;
> }
> 
> 
> 
> > Thanks,
> > 
> > Louis
> > 
> -- Dave
> 

-- 
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

[-- Attachment #1.2: Digital signature --]
[-- Type: application/pgp-signature, Size: 197 bytes --]

[-- Attachment #2: Type: text/plain, Size: 206 bytes --]

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2009-12-04 17:24 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-12-04 15:29 updated x86_64 eclone() stub Dave Hansen
2009-12-04 16:01 ` Louis Rilling
     [not found]   ` <20091204160123.GJ2430-Hu8+6S1rdjywhHL9vcZdMVaTQe2KTcn/@public.gmane.org>
2009-12-04 16:05     ` Dave Hansen
2009-12-04 16:08       ` Louis Rilling
     [not found]         ` <20091204160825.GL2430-Hu8+6S1rdjywhHL9vcZdMVaTQe2KTcn/@public.gmane.org>
2009-12-04 16:21           ` Dave Hansen
2009-12-04 17:24             ` Louis Rilling

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox