Patchwork [v3,1/3] Loongson-2F: Flush the branch target history such as BTB and RAS

login
register
mail settings
Submitter Wu Zhangjin
Date 2010-03-13 04:03:15
Message ID <05e2ba2596f23fa4dda64d63ce2480504b1be4ac.1268453720.git.wuzhangjin@gmail.com>
Download mbox | patch
Permalink /patch/1066/
State Accepted
Delegated to: Ralf Baechle
Headers show

Comments

Wu Zhangjin - 2010-03-13 04:03:15
From: Wu Zhangjin <wuzhangjin@gmail.com>

As the Chapter 15: "Errata: Issue of Out-of-order in loongson"[1] shows, to
workaround the Issue of Loongson-2F?We need to do:

"When switching from user mode to kernel mode, you should flush the
branch target history such as BTB and RAS."

This patch did clear BTB(branch target buffer), forbid RAS(return
address stack) via Loongson-2F's 64bit diagnostic register.

[1] Chinese Version: http://www.loongson.cn/uploadfile/file/200808211
[2] English Version of Chapter 15:
http://groups.google.com.hk/group/loongson-dev/msg/e0d2e220958f10a6?dmode=source

Signed-off-by: Wu Zhangjin <wuzhangjin@gmail.com>
---
 arch/mips/include/asm/stackframe.h |   19 +++++++++++++++++++
 1 files changed, 19 insertions(+), 0 deletions(-)
root - 2010-03-17 03:03:24
On Sat, Mar 13, 2010 at 12:34:15PM +0800, Wu Zhangjin wrote:

> From: Wu Zhangjin <wuzhangjin@gmail.com>
> 
> As the Chapter 15: "Errata: Issue of Out-of-order in loongson"[1] shows, to
> workaround the Issue of Loongson-2F?We need to do:
> 
> "When switching from user mode to kernel mode, you should flush the
> branch target history such as BTB and RAS."
> 
> This patch did clear BTB(branch target buffer), forbid RAS(return
> address stack) via Loongson-2F's 64bit diagnostic register.
> 
> [1] Chinese Version: http://www.loongson.cn/uploadfile/file/200808211
> [2] English Version of Chapter 15:
> http://groups.google.com.hk/group/loongson-dev/msg/e0d2e220958f10a6?dmode=source

Thanks, applied.

  Ralf
Andreas Barth - 2010-04-02 02:04:01
* Wu Zhangjin (wuzhangjin@gmail.com) [100313 05:45]:
> This patch did clear BTB(branch target buffer), forbid RAS(return
> address stack) via Loongson-2F's 64bit diagnostic register.

Unfortunatly the Loongson 2F here still fails with this patch,
compiled with the new binutils and both options enabled.

Testcase: plain debian unstable chroot, build binutils in that chroot.

More ideas, codes, whatever welcome.



Andi
Wu Zhangjin - 2010-04-03 01:04:55
On Fri, 2010-04-02 at 16:54 +0200, Andreas Barth wrote:
> * Wu Zhangjin (wuzhangjin@gmail.com) [100313 05:45]:
> > This patch did clear BTB(branch target buffer), forbid RAS(return
> > address stack) via Loongson-2F's 64bit diagnostic register.
> 
> Unfortunatly the Loongson 2F here still fails with this patch,
> compiled with the new binutils and both options enabled.
> 
> Testcase: plain debian unstable chroot, build binutils in that chroot.
> 
> More ideas, codes, whatever welcome.

Hi,

This patch is not enough, please use the kernel(>=2.6.32) from
http://dev.lemote.com/code/rt4ls or
http://dev.lemote.com/code/linux-loongson-community

Regards,
	Wu Zhangjin
Zhang Le - 2010-04-03 01:04:52
On 16:54 Fri 02 Apr     , Andreas Barth wrote:
> * Wu Zhangjin (wuzhangjin@gmail.com) [100313 05:45]:
> > This patch did clear BTB(branch target buffer), forbid RAS(return
> > address stack) via Loongson-2F's 64bit diagnostic register.
> 
> Unfortunatly the Loongson 2F here still fails with this patch,
> compiled with the new binutils and both options enabled.

Which version of binutils exactly?
The patch is in cvs, but latest binutils-2.20.1 still didn't include it.
You need to patch it.

Zhang, Le
Andreas Barth - 2010-04-03 03:04:26
* Zhang Le (r0bertz@gentoo.org) [100403 04:20]:
> On 16:54 Fri 02 Apr     , Andreas Barth wrote:
> > * Wu Zhangjin (wuzhangjin@gmail.com) [100313 05:45]:
> > > This patch did clear BTB(branch target buffer), forbid RAS(return
> > > address stack) via Loongson-2F's 64bit diagnostic register.
> > 
> > Unfortunatly the Loongson 2F here still fails with this patch,
> > compiled with the new binutils and both options enabled.
> 
> Which version of binutils exactly?
> The patch is in cvs, but latest binutils-2.20.1 still didn't include it.
> You need to patch it.

Yes, binutils are patched with
http://sourceware.org/ml/binutils/2009-11/msg00387.html


Andi
Andreas Barth - 2010-04-06 07:04:26
* Wu Zhangjin (wuzhangjin@gmail.com) [100403 03:50]:
> On Fri, 2010-04-02 at 16:54 +0200, Andreas Barth wrote:
> > * Wu Zhangjin (wuzhangjin@gmail.com) [100313 05:45]:
> > > This patch did clear BTB(branch target buffer), forbid RAS(return
> > > address stack) via Loongson-2F's 64bit diagnostic register.
> > 
> > Unfortunatly the Loongson 2F here still fails with this patch,
> > compiled with the new binutils and both options enabled.
> > 
> > Testcase: plain debian unstable chroot, build binutils in that chroot.
> > 
> > More ideas, codes, whatever welcome.


> This patch is not enough, please use the kernel(>=2.6.32) from
> http://dev.lemote.com/code/rt4ls or
> http://dev.lemote.com/code/linux-loongson-community

The kernel vmlinuz-2.6.33-lemote2f-bfs inside of
http://www.anheng.com.cn/loongson/install/loongson2_debian6_20100328.tar.lzma
(linked via linux-loongson-community) fails at the same place:

touch stamp-picdir
if [ x"-fPIC" != x ]; then \
          gcc -c -DHAVE_CONFIG_H -g -O2  -I. -I../../libiberty/../include  -W -Wall -Wwrite-strings -Wc++-compat -Wstrict-prototypes -pedantic  -fPIC ../../libiberty/regex.c -o pic/regex.o; \
        else true; fi
gcc -c -DHAVE_CONFIG_H -g -O2  -I. -I../../libiberty/../include  -W -Wall -Wwrite-strings -Wc++-compat -Wstrict-prototypes -pedantic  ../../libiberty/regex.c -o regex.o
if [ x"-fPIC" != x ]; then \
          gcc -c -DHAVE_CONFIG_H -g -O2  -I. -I../../libiberty/../include  -W -Wall -Wwrite-strings -Wc++-compat -Wstrict-prototypes -pedantic  -fPIC ../../libiberty/cplus-dem.c -o pic/cplus-dem.o; \
        else true; fi


Any other kernel I should try?



Andi
Wu Zhangjin - 2010-04-07 07:04:55
On Tue, 2010-04-06 at 21:10 +0200, Andreas Barth wrote:
[...]
> 
> The kernel vmlinuz-2.6.33-lemote2f-bfs inside of
> http://www.anheng.com.cn/loongson/install/loongson2_debian6_20100328.tar.lzma
> (linked via linux-loongson-community) fails at the same place:
> 
> touch stamp-picdir
> if [ x"-fPIC" != x ]; then \
>           gcc -c -DHAVE_CONFIG_H -g -O2  -I. -I../../libiberty/../include  -W -Wall -Wwrite-strings -Wc++-compat -Wstrict-prototypes -pedantic  -fPIC ../../libiberty/regex.c -o pic/regex.o; \
>         else true; fi
> gcc -c -DHAVE_CONFIG_H -g -O2  -I. -I../../libiberty/../include  -W -Wall -Wwrite-strings -Wc++-compat -Wstrict-prototypes -pedantic  ../../libiberty/regex.c -o regex.o
> if [ x"-fPIC" != x ]; then \
>           gcc -c -DHAVE_CONFIG_H -g -O2  -I. -I../../libiberty/../include  -W -Wall -Wwrite-strings -Wc++-compat -Wstrict-prototypes -pedantic  -fPIC ../../libiberty/cplus-dem.c -o pic/cplus-dem.o; \
>         else true; fi
> 

When & where did you get the above information?

do you mean the kernel can not boot or there are some other problems
after the kernel booting?

I guess: the whole system crashed when you was compiling something? then
please ensure the as & ld is ok via fixing the NOPS with the tool
(fix-nop.c) from  http://dev.lemote.com/code/linux-loongson-community :

$ ./fix-nop `which as`
$ ./fix-nop `which ld`

> 
> Any other kernel I should try?
> 

The kernel in the above link should boot, or you can compile one
yourself.

Regards,
	Wu Zhangjin
Andreas Barth - 2010-04-10 08:04:39
* Wu Zhangjin (wuzhangjin@gmail.com) [100407 09:38]:
> On Tue, 2010-04-06 at 21:10 +0200, Andreas Barth wrote:
> [...]
> > 
> > The kernel vmlinuz-2.6.33-lemote2f-bfs inside of
> > http://www.anheng.com.cn/loongson/install/loongson2_debian6_20100328.tar.lzma
> > (linked via linux-loongson-community) fails at the same place:
> > 
> > touch stamp-picdir
> > if [ x"-fPIC" != x ]; then \
> >           gcc -c -DHAVE_CONFIG_H -g -O2  -I. -I../../libiberty/../include  -W -Wall -Wwrite-strings -Wc++-compat -Wstrict-prototypes -pedantic  -fPIC ../../libiberty/regex.c -o pic/regex.o; \
> >         else true; fi
> > gcc -c -DHAVE_CONFIG_H -g -O2  -I. -I../../libiberty/../include  -W -Wall -Wwrite-strings -Wc++-compat -Wstrict-prototypes -pedantic  ../../libiberty/regex.c -o regex.o
> > if [ x"-fPIC" != x ]; then \
> >           gcc -c -DHAVE_CONFIG_H -g -O2  -I. -I../../libiberty/../include  -W -Wall -Wwrite-strings -Wc++-compat -Wstrict-prototypes -pedantic  -fPIC ../../libiberty/cplus-dem.c -o pic/cplus-dem.o; \
> >         else true; fi
> > 
> 
> When & where did you get the above information?
> 
> do you mean the kernel can not boot or there are some other problems
> after the kernel booting?
> 
> I guess: the whole system crashed when you was compiling something? then
> please ensure the as & ld is ok via fixing the NOPS with the tool
> (fix-nop.c) from  http://dev.lemote.com/code/linux-loongson-community :

The kernel does boot, but the whole machines crashes.

I know the fixups (I have adjusted binutils), but I need an kernel
that userland cannot crash (otherwise it gets a bit hard to run that
as debian buildd).


Andi
Wu Zhangjin - 2010-04-12 03:04:59
On Sat, 2010-04-10 at 10:10 +0200, Andreas Barth wrote:
> * Wu Zhangjin (wuzhangjin@gmail.com) [100407 09:38]:
> > On Tue, 2010-04-06 at 21:10 +0200, Andreas Barth wrote:
> > [...]
> > > 
> > > The kernel vmlinuz-2.6.33-lemote2f-bfs inside of
> > > http://www.anheng.com.cn/loongson/install/loongson2_debian6_20100328.tar.lzma
> > > (linked via linux-loongson-community) fails at the same place:
> > > 
> > > touch stamp-picdir
> > > if [ x"-fPIC" != x ]; then \
> > >           gcc -c -DHAVE_CONFIG_H -g -O2  -I. -I../../libiberty/../include  -W -Wall -Wwrite-strings -Wc++-compat -Wstrict-prototypes -pedantic  -fPIC ../../libiberty/regex.c -o pic/regex.o; \
> > >         else true; fi
> > > gcc -c -DHAVE_CONFIG_H -g -O2  -I. -I../../libiberty/../include  -W -Wall -Wwrite-strings -Wc++-compat -Wstrict-prototypes -pedantic  ../../libiberty/regex.c -o regex.o
> > > if [ x"-fPIC" != x ]; then \
> > >           gcc -c -DHAVE_CONFIG_H -g -O2  -I. -I../../libiberty/../include  -W -Wall -Wwrite-strings -Wc++-compat -Wstrict-prototypes -pedantic  -fPIC ../../libiberty/cplus-dem.c -o pic/cplus-dem.o; \
> > >         else true; fi
> > > 
> > 
> > When & where did you get the above information?
> > 
> > do you mean the kernel can not boot or there are some other problems
> > after the kernel booting?
> > 
> > I guess: the whole system crashed when you was compiling something? then
> > please ensure the as & ld is ok via fixing the NOPS with the tool
> > (fix-nop.c) from  http://dev.lemote.com/code/linux-loongson-community :
> 
> The kernel does boot, but the whole machines crashes.
> 
> I know the fixups (I have adjusted binutils), but I need an kernel
> that userland cannot crash (otherwise it gets a bit hard to run that
> as debian buildd).
> 
> 

The userland canot be prevented from crash without user-space fixups,
that's why we need the fix-nop.c to fix the NOPs in the binaries of
user-land applications or using the -mfix-loongson-nop to compile the
user-land applications.

If you just need to rebuild debian, you just need to fix the NOPs in the
as and ld with fix-nop.c as I have mentioned before:

$ ./fix-nop $(which as)
$ ./fix-nop $(which ld)

Regards,
	Wu Zhangjin

Patch

diff --git a/arch/mips/include/asm/stackframe.h b/arch/mips/include/asm/stackframe.h
index 3b6da33..c841912 100644
--- a/arch/mips/include/asm/stackframe.h
+++ b/arch/mips/include/asm/stackframe.h
@@ -121,6 +121,25 @@ 
 		.endm
 #else
 		.macro	get_saved_sp	/* Uniprocessor variation */
+#ifdef CONFIG_CPU_LOONGSON2F
+		/*
+		 * Clear BTB (branch target buffer), forbid RAS (return address
+		 * stack) to workaround the Out-of-order Issue in Loongson2F
+		 * via its diagnostic register.
+		 */
+		move	k0, ra
+		jal	1f
+		 nop
+1:		jal	1f
+		 nop
+1:		jal	1f
+		 nop
+1:		jal	1f
+		 nop
+1:		move	ra, k0
+		li	k0, 3
+		mtc0	k0, $22
+#endif /* CONFIG_CPU_LOONGSON2F */
 #if defined(CONFIG_32BIT) || defined(KBUILD_64BIT_SYM32)
 		lui	k1, %hi(kernelsp)
 #else