linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jordan Niethe <jniethe5@gmail.com>
To: Christophe Leroy <christophe.leroy@csgroup.eu>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>,
	Paul Mackerras <paulus@samba.org>,
	Michael Ellerman <mpe@ellerman.id.au>,
	"Naveen N. Rao" <naveen.n.rao@linux.vnet.ibm.com>,
	linux-kernel@vger.kernel.org,
	linuxppc-dev <linuxppc-dev@lists.ozlabs.org>
Subject: Re: [PATCH v2 07/12] powerpc/lib/code-patching: Don't use struct 'ppc_inst' for runnable code in tests.
Date: Tue, 15 Jun 2021 13:42:56 +1000	[thread overview]
Message-ID: <CACzsE9pBMPnZsDwUZ9MWWMSgn5q2m9x_W9D3+d+Q3-s=-6DJjw@mail.gmail.com> (raw)
In-Reply-To: <d8425fb42a4adebc35b7509f121817eeb02fac31.1621516826.git.christophe.leroy@csgroup.eu>

On Thu, May 20, 2021 at 11:50 PM Christophe Leroy
<christophe.leroy@csgroup.eu> wrote:
>
> 'struct ppc_inst' is meant to represent an instruction internally, it
> is not meant to dereference code in memory.
>
> For testing code patching, use patch_instruction() to properly
> write into memory the code to be tested.
>
> Signed-off-by: Christophe Leroy <christophe.leroy@csgroup.eu>
> ---
>  arch/powerpc/lib/code-patching.c | 95 ++++++++++++++++++--------------
>  1 file changed, 53 insertions(+), 42 deletions(-)
>
> diff --git a/arch/powerpc/lib/code-patching.c b/arch/powerpc/lib/code-patching.c
> index 82f2c1edb498..508e9511ca96 100644
> --- a/arch/powerpc/lib/code-patching.c
> +++ b/arch/powerpc/lib/code-patching.c
> @@ -422,9 +422,9 @@ static void __init test_branch_iform(void)
>  {
>         int err;
>         struct ppc_inst instr;
> -       unsigned long addr;
> -
> -       addr = (unsigned long)&instr;
> +       unsigned int tmp[2];
> +       struct ppc_inst *iptr = (struct ppc_inst *)tmp;
> +       unsigned long addr = (unsigned long)tmp;
>
>         /* The simplest case, branch to self, no flags */
>         check(instr_is_branch_iform(ppc_inst(0x48000000)));
> @@ -445,52 +445,57 @@ static void __init test_branch_iform(void)
>         check(!instr_is_branch_iform(ppc_inst(0x7bfffffd)));
>
>         /* Absolute branch to 0x100 */
> -       instr = ppc_inst(0x48000103);
> -       check(instr_is_branch_to_addr(&instr, 0x100));
> +       patch_instruction(iptr, ppc_inst(0x48000103));
> +       check(instr_is_branch_to_addr(iptr, 0x100));
>         /* Absolute branch to 0x420fc */
> -       instr = ppc_inst(0x480420ff);
> -       check(instr_is_branch_to_addr(&instr, 0x420fc));
> +       patch_instruction(iptr, ppc_inst(0x480420ff));
> +       check(instr_is_branch_to_addr(iptr, 0x420fc));
>         /* Maximum positive relative branch, + 20MB - 4B */
> -       instr = ppc_inst(0x49fffffc);
> -       check(instr_is_branch_to_addr(&instr, addr + 0x1FFFFFC));
> +       patch_instruction(iptr, ppc_inst(0x49fffffc));
> +       check(instr_is_branch_to_addr(iptr, addr + 0x1FFFFFC));
>         /* Smallest negative relative branch, - 4B */
> -       instr = ppc_inst(0x4bfffffc);
> -       check(instr_is_branch_to_addr(&instr, addr - 4));
> +       patch_instruction(iptr, ppc_inst(0x4bfffffc));
> +       check(instr_is_branch_to_addr(iptr, addr - 4));
>         /* Largest negative relative branch, - 32 MB */
> -       instr = ppc_inst(0x4a000000);
> -       check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
> +       patch_instruction(iptr, ppc_inst(0x4a000000));
> +       check(instr_is_branch_to_addr(iptr, addr - 0x2000000));
>
>         /* Branch to self, with link */
> -       err = create_branch(&instr, &instr, addr, BRANCH_SET_LINK);
> -       check(instr_is_branch_to_addr(&instr, addr));
> +       err = create_branch(&instr, iptr, addr, BRANCH_SET_LINK);
> +       patch_instruction(iptr, instr);
> +       check(instr_is_branch_to_addr(iptr, addr));
>
>         /* Branch to self - 0x100, with link */
> -       err = create_branch(&instr, &instr, addr - 0x100, BRANCH_SET_LINK);
> -       check(instr_is_branch_to_addr(&instr, addr - 0x100));
> +       err = create_branch(&instr, iptr, addr - 0x100, BRANCH_SET_LINK);
> +       patch_instruction(iptr, instr);
> +       check(instr_is_branch_to_addr(iptr, addr - 0x100));
>
>         /* Branch to self + 0x100, no link */
> -       err = create_branch(&instr, &instr, addr + 0x100, 0);
> -       check(instr_is_branch_to_addr(&instr, addr + 0x100));
> +       err = create_branch(&instr, iptr, addr + 0x100, 0);
> +       patch_instruction(iptr, instr);
> +       check(instr_is_branch_to_addr(iptr, addr + 0x100));
>
>         /* Maximum relative negative offset, - 32 MB */
> -       err = create_branch(&instr, &instr, addr - 0x2000000, BRANCH_SET_LINK);
> -       check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
> +       err = create_branch(&instr, iptr, addr - 0x2000000, BRANCH_SET_LINK);
> +       patch_instruction(iptr, instr);
> +       check(instr_is_branch_to_addr(iptr, addr - 0x2000000));
>
>         /* Out of range relative negative offset, - 32 MB + 4*/
> -       err = create_branch(&instr, &instr, addr - 0x2000004, BRANCH_SET_LINK);
> +       err = create_branch(&instr, iptr, addr - 0x2000004, BRANCH_SET_LINK);
>         check(err);
>
>         /* Out of range relative positive offset, + 32 MB */
> -       err = create_branch(&instr, &instr, addr + 0x2000000, BRANCH_SET_LINK);
> +       err = create_branch(&instr, iptr, addr + 0x2000000, BRANCH_SET_LINK);
>         check(err);
>
>         /* Unaligned target */
> -       err = create_branch(&instr, &instr, addr + 3, BRANCH_SET_LINK);
> +       err = create_branch(&instr, iptr, addr + 3, BRANCH_SET_LINK);
>         check(err);
>
>         /* Check flags are masked correctly */
> -       err = create_branch(&instr, &instr, addr, 0xFFFFFFFC);
> -       check(instr_is_branch_to_addr(&instr, addr));
> +       err = create_branch(&instr, iptr, addr, 0xFFFFFFFC);
> +       patch_instruction(iptr, instr);
> +       check(instr_is_branch_to_addr(iptr, addr));
>         check(ppc_inst_equal(instr, ppc_inst(0x48000000)));
>  }
>
> @@ -513,9 +518,10 @@ static void __init test_branch_bform(void)
>         int err;
>         unsigned long addr;
>         struct ppc_inst *iptr, instr;
> +       unsigned int tmp[2];
>         unsigned int flags;
>
> -       iptr = &instr;
> +       iptr = (struct ppc_inst *)tmp;
>         addr = (unsigned long)iptr;
>
>         /* The simplest case, branch to self, no flags */
> @@ -528,39 +534,43 @@ static void __init test_branch_bform(void)
>         check(!instr_is_branch_bform(ppc_inst(0x7bffffff)));
>
>         /* Absolute conditional branch to 0x100 */
> -       instr = ppc_inst(0x43ff0103);
> -       check(instr_is_branch_to_addr(&instr, 0x100));
> +       patch_instruction(iptr, ppc_inst(0x43ff0103));
> +       check(instr_is_branch_to_addr(iptr, 0x100));
>         /* Absolute conditional branch to 0x20fc */
> -       instr = ppc_inst(0x43ff20ff);
> -       check(instr_is_branch_to_addr(&instr, 0x20fc));
> +       patch_instruction(iptr, ppc_inst(0x43ff20ff));
> +       check(instr_is_branch_to_addr(iptr, 0x20fc));
>         /* Maximum positive relative conditional branch, + 32 KB - 4B */
> -       instr = ppc_inst(0x43ff7ffc);
> -       check(instr_is_branch_to_addr(&instr, addr + 0x7FFC));
> +       patch_instruction(iptr, ppc_inst(0x43ff7ffc));
> +       check(instr_is_branch_to_addr(iptr, addr + 0x7FFC));
>         /* Smallest negative relative conditional branch, - 4B */
> -       instr = ppc_inst(0x43fffffc);
> -       check(instr_is_branch_to_addr(&instr, addr - 4));
> +       patch_instruction(iptr, ppc_inst(0x43fffffc));
> +       check(instr_is_branch_to_addr(iptr, addr - 4));
>         /* Largest negative relative conditional branch, - 32 KB */
> -       instr = ppc_inst(0x43ff8000);
> -       check(instr_is_branch_to_addr(&instr, addr - 0x8000));
> +       patch_instruction(iptr, ppc_inst(0x43ff8000));
> +       check(instr_is_branch_to_addr(iptr, addr - 0x8000));
>
>         /* All condition code bits set & link */
>         flags = 0x3ff000 | BRANCH_SET_LINK;
>
>         /* Branch to self */
>         err = create_cond_branch(&instr, iptr, addr, flags);
> -       check(instr_is_branch_to_addr(&instr, addr));
> +       patch_instruction(iptr, instr);
> +       check(instr_is_branch_to_addr(iptr, addr));
>
>         /* Branch to self - 0x100 */
>         err = create_cond_branch(&instr, iptr, addr - 0x100, flags);
> -       check(instr_is_branch_to_addr(&instr, addr - 0x100));
> +       patch_instruction(iptr, instr);
> +       check(instr_is_branch_to_addr(iptr, addr - 0x100));
>
>         /* Branch to self + 0x100 */
>         err = create_cond_branch(&instr, iptr, addr + 0x100, flags);
> -       check(instr_is_branch_to_addr(&instr, addr + 0x100));
> +       patch_instruction(iptr, instr);
> +       check(instr_is_branch_to_addr(iptr, addr + 0x100));
>
>         /* Maximum relative negative offset, - 32 KB */
>         err = create_cond_branch(&instr, iptr, addr - 0x8000, flags);
> -       check(instr_is_branch_to_addr(&instr, addr - 0x8000));
> +       patch_instruction(iptr, instr);
> +       check(instr_is_branch_to_addr(iptr, addr - 0x8000));
>
>         /* Out of range relative negative offset, - 32 KB + 4*/
>         err = create_cond_branch(&instr, iptr, addr - 0x8004, flags);
> @@ -576,7 +586,8 @@ static void __init test_branch_bform(void)
>
>         /* Check flags are masked correctly */
>         err = create_cond_branch(&instr, iptr, addr, 0xFFFFFFFC);
> -       check(instr_is_branch_to_addr(&instr, addr));
> +       patch_instruction(iptr, instr);
> +       check(instr_is_branch_to_addr(iptr, addr));
>         check(ppc_inst_equal(instr, ppc_inst(0x43FF0000)));
>  }
>
> --
> 2.25.0
>
Reviewed by: Jordan Niethe <jniethe5@gmail.com>

  reply	other threads:[~2021-06-15  3:43 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-20 13:50 [PATCH v2 00/12] powerpc: Cleanup use of 'struct ppc_inst' Christophe Leroy
2021-05-20 13:50 ` [PATCH v2 01/12] powerpc/inst: Fix sparse detection on get_user_instr() Christophe Leroy
2021-05-20 13:50 ` [PATCH v2 02/12] powerpc/inst: Reduce casts in get_user_instr() Christophe Leroy
2021-05-20 13:50 ` [PATCH v2 03/12] powerpc/inst: Improve readability of get_user_instr() and friends Christophe Leroy
2021-05-20 13:50 ` [PATCH v2 04/12] powerpc/inst: Avoid pointer dereferencing in ppc_inst_equal() Christophe Leroy
2021-06-15  3:38   ` Jordan Niethe
2021-05-20 13:50 ` [PATCH v2 05/12] powerpc: Do not dereference code as 'struct ppc_inst' (uprobe, code-patching, feature-fixups) Christophe Leroy
2021-06-15  3:41   ` Jordan Niethe
2021-05-20 13:50 ` [PATCH v2 06/12] powerpc/lib/code-patching: Make instr_is_branch_to_addr() static Christophe Leroy
2021-06-15  3:42   ` Jordan Niethe
2021-05-20 13:50 ` [PATCH v2 07/12] powerpc/lib/code-patching: Don't use struct 'ppc_inst' for runnable code in tests Christophe Leroy
2021-06-15  3:42   ` Jordan Niethe [this message]
2021-05-20 13:50 ` [PATCH v2 08/12] powerpc: Don't use 'struct ppc_inst' to reference instruction location Christophe Leroy
2021-06-15  2:01   ` Michael Ellerman
2021-06-15  3:47     ` Jordan Niethe
2021-05-20 13:50 ` [PATCH v2 09/12] powerpc/inst: Refactor PPC32 and PPC64 versions Christophe Leroy
2021-06-15  3:48   ` Jordan Niethe
2021-05-20 13:50 ` [PATCH v2 10/12] powerpc/optprobes: Minimise casts Christophe Leroy
2021-05-20 13:50 ` [PATCH v2 11/12] powerpc/optprobes: Compact code source a bit Christophe Leroy
2021-05-20 13:50 ` [PATCH v2 12/12] powerpc/optprobes: use PPC_RAW_ macros Christophe Leroy
2021-06-08 12:46 ` [PATCH v2 00/12] powerpc: Cleanup use of 'struct ppc_inst' Christophe Leroy
2021-06-15  7:18 ` Michael Ellerman
2021-06-15  7:24   ` Christophe Leroy
2021-06-16 13:05     ` Michael Ellerman
2021-06-18  3:51 ` Michael Ellerman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CACzsE9pBMPnZsDwUZ9MWWMSgn5q2m9x_W9D3+d+Q3-s=-6DJjw@mail.gmail.com' \
    --to=jniethe5@gmail.com \
    --cc=benh@kernel.crashing.org \
    --cc=christophe.leroy@csgroup.eu \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=mpe@ellerman.id.au \
    --cc=naveen.n.rao@linux.vnet.ibm.com \
    --cc=paulus@samba.org \
    --subject='Re: [PATCH v2 07/12] powerpc/lib/code-patching: Don'\''t use struct '\''ppc_inst'\'' for runnable code in tests.' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

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