Commit e90a21ea authored by Christophe Leroy's avatar Christophe Leroy Committed by Michael Ellerman

powerpc/lib/code-patching: Don't use struct 'ppc_inst' for runnable code in tests.

'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: default avatarChristophe Leroy <christophe.leroy@csgroup.eu>
Signed-off-by: default avatarMichael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/d8425fb42a4adebc35b7509f121817eeb02fac31.1621516826.git.christophe.leroy@csgroup.eu
parent 6c0d181d
...@@ -422,9 +422,9 @@ static void __init test_branch_iform(void) ...@@ -422,9 +422,9 @@ static void __init test_branch_iform(void)
{ {
int err; int err;
struct ppc_inst instr; struct ppc_inst instr;
unsigned long addr; u32 tmp[2];
struct ppc_inst *iptr = (struct ppc_inst *)tmp;
addr = (unsigned long)&instr; unsigned long addr = (unsigned long)tmp;
/* The simplest case, branch to self, no flags */ /* The simplest case, branch to self, no flags */
check(instr_is_branch_iform(ppc_inst(0x48000000))); check(instr_is_branch_iform(ppc_inst(0x48000000)));
...@@ -445,52 +445,57 @@ static void __init test_branch_iform(void) ...@@ -445,52 +445,57 @@ static void __init test_branch_iform(void)
check(!instr_is_branch_iform(ppc_inst(0x7bfffffd))); check(!instr_is_branch_iform(ppc_inst(0x7bfffffd)));
/* Absolute branch to 0x100 */ /* Absolute branch to 0x100 */
instr = ppc_inst(0x48000103); patch_instruction(iptr, ppc_inst(0x48000103));
check(instr_is_branch_to_addr(&instr, 0x100)); check(instr_is_branch_to_addr(iptr, 0x100));
/* Absolute branch to 0x420fc */ /* Absolute branch to 0x420fc */
instr = ppc_inst(0x480420ff); patch_instruction(iptr, ppc_inst(0x480420ff));
check(instr_is_branch_to_addr(&instr, 0x420fc)); check(instr_is_branch_to_addr(iptr, 0x420fc));
/* Maximum positive relative branch, + 20MB - 4B */ /* Maximum positive relative branch, + 20MB - 4B */
instr = ppc_inst(0x49fffffc); patch_instruction(iptr, ppc_inst(0x49fffffc));
check(instr_is_branch_to_addr(&instr, addr + 0x1FFFFFC)); check(instr_is_branch_to_addr(iptr, addr + 0x1FFFFFC));
/* Smallest negative relative branch, - 4B */ /* Smallest negative relative branch, - 4B */
instr = ppc_inst(0x4bfffffc); patch_instruction(iptr, ppc_inst(0x4bfffffc));
check(instr_is_branch_to_addr(&instr, addr - 4)); check(instr_is_branch_to_addr(iptr, addr - 4));
/* Largest negative relative branch, - 32 MB */ /* Largest negative relative branch, - 32 MB */
instr = ppc_inst(0x4a000000); patch_instruction(iptr, ppc_inst(0x4a000000));
check(instr_is_branch_to_addr(&instr, addr - 0x2000000)); check(instr_is_branch_to_addr(iptr, addr - 0x2000000));
/* Branch to self, with link */ /* Branch to self, with link */
err = create_branch(&instr, &instr, addr, BRANCH_SET_LINK); err = create_branch(&instr, iptr, addr, BRANCH_SET_LINK);
check(instr_is_branch_to_addr(&instr, addr)); patch_instruction(iptr, instr);
check(instr_is_branch_to_addr(iptr, addr));
/* Branch to self - 0x100, with link */ /* Branch to self - 0x100, with link */
err = create_branch(&instr, &instr, addr - 0x100, BRANCH_SET_LINK); err = create_branch(&instr, iptr, addr - 0x100, BRANCH_SET_LINK);
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, no link */ /* Branch to self + 0x100, no link */
err = create_branch(&instr, &instr, addr + 0x100, 0); err = create_branch(&instr, iptr, addr + 0x100, 0);
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 MB */ /* Maximum relative negative offset, - 32 MB */
err = create_branch(&instr, &instr, addr - 0x2000000, BRANCH_SET_LINK); err = create_branch(&instr, iptr, addr - 0x2000000, BRANCH_SET_LINK);
check(instr_is_branch_to_addr(&instr, addr - 0x2000000)); patch_instruction(iptr, instr);
check(instr_is_branch_to_addr(iptr, addr - 0x2000000));
/* Out of range relative negative offset, - 32 MB + 4*/ /* 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); check(err);
/* Out of range relative positive offset, + 32 MB */ /* 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); check(err);
/* Unaligned target */ /* 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(err);
/* Check flags are masked correctly */ /* Check flags are masked correctly */
err = create_branch(&instr, &instr, addr, 0xFFFFFFFC); err = create_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(0x48000000))); check(ppc_inst_equal(instr, ppc_inst(0x48000000)));
} }
...@@ -513,9 +518,10 @@ static void __init test_branch_bform(void) ...@@ -513,9 +518,10 @@ static void __init test_branch_bform(void)
int err; int err;
unsigned long addr; unsigned long addr;
struct ppc_inst *iptr, instr; struct ppc_inst *iptr, instr;
u32 tmp[2];
unsigned int flags; unsigned int flags;
iptr = &instr; iptr = (struct ppc_inst *)tmp;
addr = (unsigned long)iptr; addr = (unsigned long)iptr;
/* The simplest case, branch to self, no flags */ /* The simplest case, branch to self, no flags */
...@@ -528,39 +534,43 @@ static void __init test_branch_bform(void) ...@@ -528,39 +534,43 @@ static void __init test_branch_bform(void)
check(!instr_is_branch_bform(ppc_inst(0x7bffffff))); check(!instr_is_branch_bform(ppc_inst(0x7bffffff)));
/* Absolute conditional branch to 0x100 */ /* Absolute conditional branch to 0x100 */
instr = ppc_inst(0x43ff0103); patch_instruction(iptr, ppc_inst(0x43ff0103));
check(instr_is_branch_to_addr(&instr, 0x100)); check(instr_is_branch_to_addr(iptr, 0x100));
/* Absolute conditional branch to 0x20fc */ /* Absolute conditional branch to 0x20fc */
instr = ppc_inst(0x43ff20ff); patch_instruction(iptr, ppc_inst(0x43ff20ff));
check(instr_is_branch_to_addr(&instr, 0x20fc)); check(instr_is_branch_to_addr(iptr, 0x20fc));
/* Maximum positive relative conditional branch, + 32 KB - 4B */ /* Maximum positive relative conditional branch, + 32 KB - 4B */
instr = ppc_inst(0x43ff7ffc); patch_instruction(iptr, ppc_inst(0x43ff7ffc));
check(instr_is_branch_to_addr(&instr, addr + 0x7FFC)); check(instr_is_branch_to_addr(iptr, addr + 0x7FFC));
/* Smallest negative relative conditional branch, - 4B */ /* Smallest negative relative conditional branch, - 4B */
instr = ppc_inst(0x43fffffc); patch_instruction(iptr, ppc_inst(0x43fffffc));
check(instr_is_branch_to_addr(&instr, addr - 4)); check(instr_is_branch_to_addr(iptr, addr - 4));
/* Largest negative relative conditional branch, - 32 KB */ /* Largest negative relative conditional branch, - 32 KB */
instr = ppc_inst(0x43ff8000); patch_instruction(iptr, ppc_inst(0x43ff8000));
check(instr_is_branch_to_addr(&instr, addr - 0x8000)); check(instr_is_branch_to_addr(iptr, addr - 0x8000));
/* All condition code bits set & link */ /* All condition code bits set & link */
flags = 0x3ff000 | BRANCH_SET_LINK; flags = 0x3ff000 | BRANCH_SET_LINK;
/* Branch to self */ /* Branch to self */
err = create_cond_branch(&instr, iptr, addr, flags); 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 */ /* Branch to self - 0x100 */
err = create_cond_branch(&instr, iptr, addr - 0x100, flags); 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 */ /* Branch to self + 0x100 */
err = create_cond_branch(&instr, iptr, addr + 0x100, flags); 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 */ /* Maximum relative negative offset, - 32 KB */
err = create_cond_branch(&instr, iptr, addr - 0x8000, flags); 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*/ /* Out of range relative negative offset, - 32 KB + 4*/
err = create_cond_branch(&instr, iptr, addr - 0x8004, flags); err = create_cond_branch(&instr, iptr, addr - 0x8004, flags);
...@@ -576,7 +586,8 @@ static void __init test_branch_bform(void) ...@@ -576,7 +586,8 @@ static void __init test_branch_bform(void)
/* Check flags are masked correctly */ /* Check flags are masked correctly */
err = create_cond_branch(&instr, iptr, addr, 0xFFFFFFFC); 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))); check(ppc_inst_equal(instr, ppc_inst(0x43FF0000)));
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment