public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] x86, math-emu: fix init_fpu for task != current
@ 2009-03-02 15:19 Daniel Glöckner
  2009-03-02 19:52 ` Ingo Molnar
  0 siblings, 1 reply; 5+ messages in thread
From: Daniel Glöckner @ 2009-03-02 15:19 UTC (permalink / raw)
  To: x86; +Cc: Bill Metzenthen, linux-kernel

init_fpu calls finit to initialize a task's xstate, while finit always
works on the current task. If we use PTRACE_GETFPREGS on another
process and both processes did not already use floating point, we get
a null pointer exception in finit.

This patch creates a new function finit_task that takes a task_struct
parameter. finit becomes a wrapper that simply calls finit_task with
current. On the plus side this avoids many calls to get_current which
would each resolve to an inline assembler mov instruction.

Signed-off-by: Daniel Glöckner <dg@emlix.com>
---
 arch/x86/include/asm/i387.h |    1 +
 arch/x86/kernel/i387.c      |    2 +-
 arch/x86/math-emu/fpu_aux.c |   31 ++++++++++++++++++++-----------
 3 files changed, 22 insertions(+), 12 deletions(-)

diff --git a/arch/x86/include/asm/i387.h b/arch/x86/include/asm/i387.h
index 48f0004..10f349b 100644
--- a/arch/x86/include/asm/i387.h
+++ b/arch/x86/include/asm/i387.h
@@ -172,6 +172,7 @@ static inline void __save_init_fpu(struct task_struct *tsk)

 #else  /* CONFIG_X86_32 */

+extern void finit_task(struct task_struct *tsk);
 extern void finit(void);

 static inline void tolerant_fwait(void)
diff --git a/arch/x86/kernel/i387.c b/arch/x86/kernel/i387.c
index b0f61f0..f2f8540 100644
--- a/arch/x86/kernel/i387.c
+++ b/arch/x86/kernel/i387.c
@@ -136,7 +136,7 @@ int init_fpu(struct task_struct *tsk)
 #ifdef CONFIG_X86_32
 	if (!HAVE_HWFP) {
 		memset(tsk->thread.xstate, 0, xstate_size);
-		finit();
+		finit_task(tsk);
 		set_stopped_child_used_math(tsk);
 		return 0;
 	}
diff --git a/arch/x86/math-emu/fpu_aux.c b/arch/x86/math-emu/fpu_aux.c
index 491e737..aa09870 100644
--- a/arch/x86/math-emu/fpu_aux.c
+++ b/arch/x86/math-emu/fpu_aux.c
@@ -30,20 +30,29 @@ static void fclex(void)
 }

 /* Needs to be externally visible */
-void finit(void)
+void finit_task(struct task_struct *tsk)
 {
-	control_word = 0x037f;
-	partial_status = 0;
-	top = 0;		/* We don't keep top in the status word internally. */
-	fpu_tag_word = 0xffff;
+	struct i387_soft_struct *soft = &tsk->thread.xstate->soft;
+	struct address *oaddr, *iaddr;
+	soft->cwd = 0x037f;
+	soft->swd = 0;
+	soft->ftop = 0;	/* We don't keep top in the status word internally. */
+	soft->twd = 0xffff;
 	/* The behaviour is different from that detailed in
 	   Section 15.1.6 of the Intel manual */
-	operand_address.offset = 0;
-	operand_address.selector = 0;
-	instruction_address.offset = 0;
-	instruction_address.selector = 0;
-	instruction_address.opcode = 0;
-	no_ip_update = 1;
+	oaddr = (struct address *)&soft->foo;
+	oaddr->offset = 0;
+	oaddr->selector = 0;
+	iaddr = (struct address *)&soft->fip;
+	iaddr->offset = 0;
+	iaddr->selector = 0;
+	iaddr->opcode = 0;
+	soft->no_update = 1;
+}
+
+void finit(void)
+{
+	finit_task(current);
 }

 /*
-- 
1.5.6


-- 
Dipl.-Math. Daniel Glöckner, emlix GmbH, http://www.emlix.com
Fon +49 551 30664-0, Fax -11, Bahnhofsallee 1b, 37081 Göttingen, Germany
Geschäftsführung: Dr. Uwe Kracke, Dr. Cord Seele, Ust-IdNr.: DE 205 198 055
Sitz der Gesellschaft: Göttingen, Amtsgericht Göttingen HR B 3160

emlix - your embedded linux partner

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

* Re: [PATCH] x86, math-emu: fix init_fpu for task != current
  2009-03-02 15:19 [PATCH] x86, math-emu: fix init_fpu for task != current Daniel Glöckner
@ 2009-03-02 19:52 ` Ingo Molnar
  2009-03-04 16:01   ` Daniel Glöckner
  0 siblings, 1 reply; 5+ messages in thread
From: Ingo Molnar @ 2009-03-02 19:52 UTC (permalink / raw)
  To: Daniel Glöckner, H. Peter Anvin, Suresh Siddha,
	Pallipadi, Venkatesh, Arjan van de Ven
  Cc: x86, Bill Metzenthen, linux-kernel


* Daniel Glöckner <dg@emlix.com> wrote:

> init_fpu calls finit to initialize a task's xstate, while finit always
> works on the current task. If we use PTRACE_GETFPREGS on another
> process and both processes did not already use floating point, we get
> a null pointer exception in finit.
> 
> This patch creates a new function finit_task that takes a task_struct
> parameter. finit becomes a wrapper that simply calls finit_task with
> current. On the plus side this avoids many calls to get_current which
> would each resolve to an inline assembler mov instruction.
> 
> Signed-off-by: Daniel Glöckner <dg@emlix.com>
> ---
>  arch/x86/include/asm/i387.h |    1 +
>  arch/x86/kernel/i387.c      |    2 +-
>  arch/x86/math-emu/fpu_aux.c |   31 ++++++++++++++++++++-----------
>  3 files changed, 22 insertions(+), 12 deletions(-)
> 
> diff --git a/arch/x86/include/asm/i387.h b/arch/x86/include/asm/i387.h
> index 48f0004..10f349b 100644
> --- a/arch/x86/include/asm/i387.h
> +++ b/arch/x86/include/asm/i387.h
> @@ -172,6 +172,7 @@ static inline void __save_init_fpu(struct task_struct *tsk)
> 
>  #else  /* CONFIG_X86_32 */
> 
> +extern void finit_task(struct task_struct *tsk);
>  extern void finit(void);
> 
>  static inline void tolerant_fwait(void)
> diff --git a/arch/x86/kernel/i387.c b/arch/x86/kernel/i387.c
> index b0f61f0..f2f8540 100644
> --- a/arch/x86/kernel/i387.c
> +++ b/arch/x86/kernel/i387.c
> @@ -136,7 +136,7 @@ int init_fpu(struct task_struct *tsk)
>  #ifdef CONFIG_X86_32
>  	if (!HAVE_HWFP) {
>  		memset(tsk->thread.xstate, 0, xstate_size);
> -		finit();
> +		finit_task(tsk);
>  		set_stopped_child_used_math(tsk);
>  		return 0;
>  	}
> diff --git a/arch/x86/math-emu/fpu_aux.c b/arch/x86/math-emu/fpu_aux.c
> index 491e737..aa09870 100644
> --- a/arch/x86/math-emu/fpu_aux.c
> +++ b/arch/x86/math-emu/fpu_aux.c
> @@ -30,20 +30,29 @@ static void fclex(void)
>  }
> 
>  /* Needs to be externally visible */
> -void finit(void)
> +void finit_task(struct task_struct *tsk)

ok, the fix looks good - but we sure want to define this 
function in the "# CONFIG_MATH_EMU is not set" case too, agreed?

	Ingo

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

* Re: [PATCH] x86, math-emu: fix init_fpu for task != current
  2009-03-02 19:52 ` Ingo Molnar
@ 2009-03-04 16:01   ` Daniel Glöckner
  2009-03-04 17:51     ` Ingo Molnar
  0 siblings, 1 reply; 5+ messages in thread
From: Daniel Glöckner @ 2009-03-04 16:01 UTC (permalink / raw)
  To: Ingo Molnar
  Cc: H. Peter Anvin, Suresh Siddha, Pallipadi, Venkatesh,
	Arjan van de Ven, x86, Bill Metzenthen, linux-kernel

On 03/02/2009 08:52 PM, Ingo Molnar wrote:
> * Daniel Glöckner <dg@emlix.com> wrote:
[..]
>> This patch creates a new function finit_task that takes a task_struct
>> parameter. finit becomes a wrapper that simply calls finit_task with
>> current. On the plus side this avoids many calls to get_current which
>> would each resolve to an inline assembler mov instruction.
[..]
>> --- a/arch/x86/kernel/i387.c
>> +++ b/arch/x86/kernel/i387.c
>> @@ -136,7 +136,7 @@ int init_fpu(struct task_struct *tsk)
>>  #ifdef CONFIG_X86_32
>>  	if (!HAVE_HWFP) {
>>  		memset(tsk->thread.xstate, 0, xstate_size);
>> -		finit();
>> +		finit_task(tsk);
>>  		set_stopped_child_used_math(tsk);
>>  		return 0;
>>  	}
[..]
> 
> ok, the fix looks good - but we sure want to define this 
> function in the "# CONFIG_MATH_EMU is not set" case too, agreed?

To avoid linker errors if the compiler emits the call although !HAVE_HWFP
is always false? Sounds plausible. I also removed finit from i387.h as the
only other external call site, reg_ld_str.c, gets its prototype from
fpu_proto.h.

Signed-off-by: Daniel Glöckner <dg@emlix.com>
---
 arch/x86/include/asm/i387.h |    7 ++++++-
 1 files changed, 6 insertions(+), 1 deletions(-)

diff --git a/arch/x86/include/asm/i387.h b/arch/x86/include/asm/i387.h
index 10f349b..71c9e51 100644
--- a/arch/x86/include/asm/i387.h
+++ b/arch/x86/include/asm/i387.h
@@ -172,8 +172,13 @@ static inline void __save_init_fpu(struct task_struct *tsk)

 #else  /* CONFIG_X86_32 */

+#ifdef CONFIG_MATH_EMULATION
 extern void finit_task(struct task_struct *tsk);
-extern void finit(void);
+#else
+static inline void finit_task(struct task_struct *tsk)
+{
+}
+#endif

 static inline void tolerant_fwait(void)
 {
--
1.5.6

-- 
Dipl.-Math. Daniel Glöckner, emlix GmbH, http://www.emlix.com
Fon +49 551 30664-0, Fax -11, Bahnhofsallee 1b, 37081 Göttingen, Germany
Geschäftsführung: Dr. Uwe Kracke, Dr. Cord Seele, Ust-IdNr.: DE 205 198 055
Sitz der Gesellschaft: Göttingen, Amtsgericht Göttingen HR B 3160

emlix - your embedded linux partner

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

* Re: [PATCH] x86, math-emu: fix init_fpu for task != current
  2009-03-04 16:01   ` Daniel Glöckner
@ 2009-03-04 17:51     ` Ingo Molnar
  2009-03-04 18:42       ` [PATCH -v2] " Daniel Glöckner
  0 siblings, 1 reply; 5+ messages in thread
From: Ingo Molnar @ 2009-03-04 17:51 UTC (permalink / raw)
  To: Daniel Glöckner
  Cc: H. Peter Anvin, Suresh Siddha, Pallipadi, Venkatesh,
	Arjan van de Ven, x86, Bill Metzenthen, linux-kernel


* Daniel Glöckner <dg@emlix.com> wrote:

> On 03/02/2009 08:52 PM, Ingo Molnar wrote:
> > * Daniel Glöckner <dg@emlix.com> wrote:
> [..]
> >> This patch creates a new function finit_task that takes a task_struct
> >> parameter. finit becomes a wrapper that simply calls finit_task with
> >> current. On the plus side this avoids many calls to get_current which
> >> would each resolve to an inline assembler mov instruction.
> [..]
> >> --- a/arch/x86/kernel/i387.c
> >> +++ b/arch/x86/kernel/i387.c
> >> @@ -136,7 +136,7 @@ int init_fpu(struct task_struct *tsk)
> >>  #ifdef CONFIG_X86_32
> >>  	if (!HAVE_HWFP) {
> >>  		memset(tsk->thread.xstate, 0, xstate_size);
> >> -		finit();
> >> +		finit_task(tsk);
> >>  		set_stopped_child_used_math(tsk);
> >>  		return 0;
> >>  	}
> [..]
> > 
> > ok, the fix looks good - but we sure want to define this 
> > function in the "# CONFIG_MATH_EMU is not set" case too, agreed?
> 
> To avoid linker errors if the compiler emits the call although 
> !HAVE_HWFP is always false? Sounds plausible. I also removed 
> finit from i387.h as the only other external call site, 
> reg_ld_str.c, gets its prototype from fpu_proto.h.

Could you please send a single combo patch with a full 
changelog, etc.

Thanks,

	Ingo

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

* [PATCH -v2] x86, math-emu: fix init_fpu for task != current
  2009-03-04 17:51     ` Ingo Molnar
@ 2009-03-04 18:42       ` Daniel Glöckner
  0 siblings, 0 replies; 5+ messages in thread
From: Daniel Glöckner @ 2009-03-04 18:42 UTC (permalink / raw)
  To: Ingo Molnar
  Cc: H. Peter Anvin, Suresh Siddha, Pallipadi, Venkatesh,
	Arjan van de Ven, x86, Bill Metzenthen, linux-kernel

init_fpu calls finit to initialize a task's xstate, while finit always
works on the current task. If we use PTRACE_GETFPREGS on another
process and both processes did not already use floating point, we get
a null pointer exception in finit.

This patch creates a new function finit_task that takes a task_struct
parameter. finit becomes a wrapper that simply calls finit_task with
current. On the plus side this avoids many calls to get_current which
would each resolve to an inline assembler mov instruction.

An empty finit_task has been added to i387.h to avoid linker errors in
case the compiler still emits the call in init_fpu when
CONFIG_MATH_EMULATION is not defined.

The declaration of finit in i387.h has been removed as the remaining
code using this function gets its prototype from fpu_proto.h.

Signed-off-by: Daniel Glöckner <dg@emlix.com>
---
 arch/x86/include/asm/i387.h |    8 +++++++-
 arch/x86/kernel/i387.c      |    2 +-
 arch/x86/math-emu/fpu_aux.c |   31 ++++++++++++++++++++-----------
 3 files changed, 28 insertions(+), 13 deletions(-)

diff --git a/arch/x86/include/asm/i387.h b/arch/x86/include/asm/i387.h
index 48f0004..71c9e51 100644
--- a/arch/x86/include/asm/i387.h
+++ b/arch/x86/include/asm/i387.h
@@ -172,7 +172,13 @@ static inline void __save_init_fpu(struct task_struct *tsk)

 #else  /* CONFIG_X86_32 */

-extern void finit(void);
+#ifdef CONFIG_MATH_EMULATION
+extern void finit_task(struct task_struct *tsk);
+#else
+static inline void finit_task(struct task_struct *tsk)
+{
+}
+#endif

 static inline void tolerant_fwait(void)
 {
diff --git a/arch/x86/kernel/i387.c b/arch/x86/kernel/i387.c
index b0f61f0..f2f8540 100644
--- a/arch/x86/kernel/i387.c
+++ b/arch/x86/kernel/i387.c
@@ -136,7 +136,7 @@ int init_fpu(struct task_struct *tsk)
 #ifdef CONFIG_X86_32
 	if (!HAVE_HWFP) {
 		memset(tsk->thread.xstate, 0, xstate_size);
-		finit();
+		finit_task(tsk);
 		set_stopped_child_used_math(tsk);
 		return 0;
 	}
diff --git a/arch/x86/math-emu/fpu_aux.c b/arch/x86/math-emu/fpu_aux.c
index 491e737..aa09870 100644
--- a/arch/x86/math-emu/fpu_aux.c
+++ b/arch/x86/math-emu/fpu_aux.c
@@ -30,20 +30,29 @@ static void fclex(void)
 }

 /* Needs to be externally visible */
-void finit(void)
+void finit_task(struct task_struct *tsk)
 {
-	control_word = 0x037f;
-	partial_status = 0;
-	top = 0;		/* We don't keep top in the status word internally. */
-	fpu_tag_word = 0xffff;
+	struct i387_soft_struct *soft = &tsk->thread.xstate->soft;
+	struct address *oaddr, *iaddr;
+	soft->cwd = 0x037f;
+	soft->swd = 0;
+	soft->ftop = 0;	/* We don't keep top in the status word internally. */
+	soft->twd = 0xffff;
 	/* The behaviour is different from that detailed in
 	   Section 15.1.6 of the Intel manual */
-	operand_address.offset = 0;
-	operand_address.selector = 0;
-	instruction_address.offset = 0;
-	instruction_address.selector = 0;
-	instruction_address.opcode = 0;
-	no_ip_update = 1;
+	oaddr = (struct address *)&soft->foo;
+	oaddr->offset = 0;
+	oaddr->selector = 0;
+	iaddr = (struct address *)&soft->fip;
+	iaddr->offset = 0;
+	iaddr->selector = 0;
+	iaddr->opcode = 0;
+	soft->no_update = 1;
+}
+
+void finit(void)
+{
+	finit_task(current);
 }

 /*
-- 
1.5.6


-- 
Dipl.-Math. Daniel Glöckner, emlix GmbH, http://www.emlix.com
Fon +49 551 30664-0, Fax -11, Bahnhofsallee 1b, 37081 Göttingen, Germany
Geschäftsführung: Dr. Uwe Kracke, Dr. Cord Seele, Ust-IdNr.: DE 205 198 055
Sitz der Gesellschaft: Göttingen, Amtsgericht Göttingen HR B 3160

emlix - your embedded linux partner

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

end of thread, other threads:[~2009-03-04 18:43 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-03-02 15:19 [PATCH] x86, math-emu: fix init_fpu for task != current Daniel Glöckner
2009-03-02 19:52 ` Ingo Molnar
2009-03-04 16:01   ` Daniel Glöckner
2009-03-04 17:51     ` Ingo Molnar
2009-03-04 18:42       ` [PATCH -v2] " Daniel Glöckner

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