From owner-svn-src-head@freebsd.org Thu Sep 10 18:41:16 2020 Return-Path: Delivered-To: svn-src-head@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 76BCD3E0FF2; Thu, 10 Sep 2020 18:41:16 +0000 (UTC) (envelope-from bdragon@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4BnSQN2X56z4TVx; Thu, 10 Sep 2020 18:41:16 +0000 (UTC) (envelope-from bdragon@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 3AB4F16E7E; Thu, 10 Sep 2020 18:41:16 +0000 (UTC) (envelope-from bdragon@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 08AIfGRc071818; Thu, 10 Sep 2020 18:41:16 GMT (envelope-from bdragon@FreeBSD.org) Received: (from bdragon@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 08AIfFEv071817; Thu, 10 Sep 2020 18:41:15 GMT (envelope-from bdragon@FreeBSD.org) Message-Id: <202009101841.08AIfFEv071817@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: bdragon set sender to bdragon@FreeBSD.org using -f From: Brandon Bergren Date: Thu, 10 Sep 2020 18:41:15 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r365607 - head/sys/powerpc/include X-SVN-Group: head X-SVN-Commit-Author: bdragon X-SVN-Commit-Paths: head/sys/powerpc/include X-SVN-Commit-Revision: 365607 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 10 Sep 2020 18:41:16 -0000 Author: bdragon Date: Thu Sep 10 18:41:15 2020 New Revision: 365607 URL: https://svnweb.freebsd.org/changeset/base/365607 Log: [PowerPC64LE] Add LOAD_LR_NIA and RETURN_TO_NATIVE_ENDIAN defines. * Add LOAD_LR_NIA define. This is preferred to "bl 1f; 1:" because it doesn't pollute the branch predictor. * Add magic sequence to return the CPU to the correct endianness after jumping to cross-endian code, similar to the sequence from Linux. Sponsored by: Tag1 Consulting, Inc. Modified: head/sys/powerpc/include/asm.h Modified: head/sys/powerpc/include/asm.h ============================================================================== --- head/sys/powerpc/include/asm.h Thu Sep 10 18:27:52 2020 (r365606) +++ head/sys/powerpc/include/asm.h Thu Sep 10 18:41:15 2020 (r365607) @@ -194,6 +194,43 @@ name: \ #define ASENTRY_NOPROF(y) _ENTRY(ASMNAME(y)) #define ENTRY_NOPROF(y) _ENTRY(CNAME(y)) +/* Load NIA without affecting branch prediction */ +#define LOAD_LR_NIA bcl 20, 31, .+4 + +/* + * Magic sequence to return to native endian. + * Overwrites r0 and r11. + * + * The encoding of the instruction "tdi 0, %r0, 0x48" in opposite endian + * happens to be "b . + 8". This is useful because we can write a sequence + * of instructions that can execute in either endian. + * + * Use a sequence of handcoded instructions that switches contexts to the + * instruction following the sequence, but with the correct PSL_LE bit. + * + * The same sequence works for both BE and LE because the xori will flip + * the bit to the other state, and the code only runs when running in the + * wrong endian. + * + * This sequence is NMI-reentrant. + */ +#define RETURN_TO_NATIVE_ENDIAN \ + tdi 0, %r0, 0x48; /* Endian swapped: b . + 8 */\ + b 1f; /* Will fall through to here if correct */\ + .long 0xa600607d; /* mfmsr %r11 */\ + .long 0x00000038; /* li %r0, 0 */\ + .long 0x6401617d; /* mtmsrd %r0, 1 (L=1 EE,RI bits only) */\ + .long 0x01006b69; /* xori %r11, %r11, 0x1 (PSL_LE) */\ + .long 0xa602087c; /* mflr %r0 */\ + .long 0x05009f42; /* LOAD_LR_NIA */\ + .long 0xa6037b7d; /* 0: mtsrr1 %r11 */\ + .long 0xa602687d; /* mflr %r11 */\ + .long 0x18006b39; /* addi %r11, %r11, (1f - 0b) */\ + .long 0xa6037a7d; /* mtsrr0 %r11 */\ + .long 0xa603087c; /* mtlr %r0 */\ + .long 0x2400004c; /* rfid */\ +1: /* RETURN_TO_NATIVE_ENDIAN */ + #define ASMSTR .asciz #define RCSID(x) .text; .asciz x