[PATCH v2 07/12] powerpc/lib/code-patching: Don't use struct 'ppc_inst' for runnable code in tests.

Jordan Niethe jniethe5 at gmail.com
Tue Jun 15 13:42:56 AEST 2021


On Thu, May 20, 2021 at 11:50 PM Christophe Leroy
<christophe.leroy at 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 at 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 at gmail.com>


More information about the Linuxppc-dev mailing list