From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1161080AbcLPQb2 (ORCPT ); Fri, 16 Dec 2016 11:31:28 -0500 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:41426 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1761854AbcLPQbV (ORCPT ); Fri, 16 Dec 2016 11:31:21 -0500 Subject: Re: [PATCH V2 2/4] powerpc: add helper to check if offset is within rel branch range To: Masami Hiramatsu References: <1481732310-7779-1-git-send-email-anju@linux.vnet.ibm.com> <1481732310-7779-5-git-send-email-anju@linux.vnet.ibm.com> <20161216205221.b6368901a9eea6a8cb7a84ff@kernel.org> Cc: linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, ananth@in.ibm.com, naveen.n.rao@linux.vnet.ibm.com, paulus@samba.org, srikar@linux.vnet.ibm.com, benh@kernel.crashing.org, mpe@ellerman.id.au, mahesh@linux.vnet.ibm.com From: Anju T Sudhakar Date: Fri, 16 Dec 2016 22:01:03 +0530 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.5.1 MIME-Version: 1.0 In-Reply-To: <20161216205221.b6368901a9eea6a8cb7a84ff@kernel.org> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-TM-AS-GCONF: 00 X-Content-Scanned: Fidelis XPS MAILER x-cbid: 16121616-0004-0000-0000-0000111ACE41 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00006260; HX=3.00000240; KW=3.00000007; PH=3.00000004; SC=3.00000198; SDB=6.00794739; UDB=6.00385464; IPR=6.00572540; BA=6.00004975; NDR=6.00000001; ZLA=6.00000005; ZF=6.00000009; ZB=6.00000000; ZP=6.00000000; ZH=6.00000000; ZU=6.00000002; MB=3.00013646; XFM=3.00000011; UTC=2016-12-16 16:31:17 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 16121616-0005-0000-0000-00007B78459C Message-Id: X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2016-12-16_10:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=0 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1612050000 definitions=main-1612160260 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Masami, Thank you for reviewing the patch set. On Friday 16 December 2016 05:22 PM, Masami Hiramatsu wrote: > On Wed, 14 Dec 2016 21:48:30 +0530 > Anju T Sudhakar wrote: > >> From: "Naveen N. Rao" >> > The coding is OK to me. Please add a description for this patch > here, e.g. what is done by this patch, what kind of branch > instruction will be covered, and why thse checks are needed etc. Sure. I will give a description for this patch. Thanks and Regards, -Anju > > Thank you, > >> Signed-off-by: Naveen N. Rao >> Signed-off-by: Anju T Sudhakar >> --- >> arch/powerpc/include/asm/code-patching.h | 1 + >> arch/powerpc/lib/code-patching.c | 24 +++++++++++++++++++++++- >> 2 files changed, 24 insertions(+), 1 deletion(-) >> >> diff --git a/arch/powerpc/include/asm/code-patching.h b/arch/powerpc/include/asm/code-patching.h >> index 2015b07..75ee4f4 100644 >> --- a/arch/powerpc/include/asm/code-patching.h >> +++ b/arch/powerpc/include/asm/code-patching.h >> @@ -22,6 +22,7 @@ >> #define BRANCH_SET_LINK 0x1 >> #define BRANCH_ABSOLUTE 0x2 >> >> +bool is_offset_in_branch_range(long offset); >> unsigned int create_branch(const unsigned int *addr, >> unsigned long target, int flags); >> unsigned int create_cond_branch(const unsigned int *addr, >> diff --git a/arch/powerpc/lib/code-patching.c b/arch/powerpc/lib/code-patching.c >> index d5edbeb..f643451 100644 >> --- a/arch/powerpc/lib/code-patching.c >> +++ b/arch/powerpc/lib/code-patching.c >> @@ -32,6 +32,28 @@ int patch_branch(unsigned int *addr, unsigned long target, int flags) >> return patch_instruction(addr, create_branch(addr, target, flags)); >> } >> >> +bool is_offset_in_branch_range(long offset) >> +{ >> + /* >> + * Powerpc branch instruction is : >> + * >> + * 0 6 30 31 >> + * +---------+----------------+---+---+ >> + * | opcode | LI |AA |LK | >> + * +---------+----------------+---+---+ >> + * Where AA = 0 and LK = 0 >> + * >> + * LI is a signed 24 bits integer. The real branch offset is computed >> + * by: imm32 = SignExtend(LI:'0b00', 32); >> + * >> + * So the maximum forward branch should be: >> + * (0x007fffff << 2) = 0x01fffffc = 0x1fffffc >> + * The maximum backward branch should be: >> + * (0xff800000 << 2) = 0xfe000000 = -0x2000000 >> + */ >> + return (offset >= -0x2000000 && offset <= 0x1fffffc && !(offset & 0x3)); >> +} >> + >> unsigned int create_branch(const unsigned int *addr, >> unsigned long target, int flags) >> { >> @@ -43,7 +65,7 @@ unsigned int create_branch(const unsigned int *addr, >> offset = offset - (unsigned long)addr; >> >> /* Check we can represent the target in the instruction format */ >> - if (offset < -0x2000000 || offset > 0x1fffffc || offset & 0x3) >> + if (!is_offset_in_branch_range(offset)) >> return 0; >> >> /* Mask out the flags and target, so they don't step on each other. */ >> -- >> 2.7.4 >> >