From patchwork Mon Nov 16 22:35:35 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Shi X-Patchwork-Id: 56697 Delivered-To: patch@linaro.org Received: by 10.112.155.196 with SMTP id vy4csp1596472lbb; Mon, 16 Nov 2015 14:54:47 -0800 (PST) X-Received: by 10.66.219.39 with SMTP id pl7mr58751803pac.88.1447714487678; Mon, 16 Nov 2015 14:54:47 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id gn10si52958960pac.10.2015.11.16.14.54.47; Mon, 16 Nov 2015 14:54:47 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dkim=neutral (body hash did not verify) header.i=@linaro_org.20150623.gappssmtp.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752553AbbKPWyq (ORCPT + 28 others); Mon, 16 Nov 2015 17:54:46 -0500 Received: from mail-pa0-f48.google.com ([209.85.220.48]:33707 "EHLO mail-pa0-f48.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751465AbbKPWyo (ORCPT ); Mon, 16 Nov 2015 17:54:44 -0500 Received: by pabfh17 with SMTP id fh17so191998756pab.0 for ; Mon, 16 Nov 2015 14:54:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro_org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=rg6i8QXknUQkT9acjd7ZbGoF7B4BiTDvrSOfEXfC5Zw=; b=FepZz8RxXUBESV7NCKEf5TP4HK66G8IQxCho+dDVJx+g0Mp6sQ3Ki/GWWjisUMQxkl xlH4A9FYMjQbsV98CdGDwultEf1pIx0nFRsGFRgWFhNZ5NJUEhAU8onc1OdgSWF0V/qL 4kIc76Fnl8fff31J89/vhXbpBfO8XLGvdnhJBGH7qKyvzljI2USOX9Hqdk8YPQ482Rp1 HURfJLnAzQXJ5tLeumB7pCy1aq4RJlUft3I/XSowBka3c+AtgH2N4n0B/tQv2T2bFM4a fwMrX5+yp9UX8FaiZLHGalsDhnIL75YZJHKTCXKb9iS1ehlZuiW11QCGIXSll0LbuqA0 O63Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=rg6i8QXknUQkT9acjd7ZbGoF7B4BiTDvrSOfEXfC5Zw=; b=kr8S6ZZrR+8LeW8XSstPCzplCYhXAJ7FMgSO4ZI/mNfLbOY42xSW6v2rjfTp88fSzQ 2x+ErYwo4OQDjflv02aEJywQtt/DlXHM+yxd3NGA3IdH/7QzbszTzMmvzs7EpZsxdRAE eVM6OUhUDvqtJXmaYV5dUOuiqM0If3ZKIWwUAkeLYNmMVCKWFAYtil1KYomE5vtxwlFO 1TbjI83R9Pj1wmUtxNYuA6X3fgnYEj+tRubmEa2FTh9ZPE+hNMGcRI95XetIDwzOa/OF XErswO8zGmDDiv3EgMb4ySZZrliKUERBFhrbONFoL7GioOYoVOaTtuAFNDcrKULLpYWP gi9g== X-Gm-Message-State: ALoCoQkOQ+k5Pxmx5skCTMFv1r1VlAHsOBJ5DJS54fZwaFSXEOPOrVoo28rulJManqyeMj4+vLgr X-Received: by 10.66.235.231 with SMTP id up7mr35365744pac.87.1447714483637; Mon, 16 Nov 2015 14:54:43 -0800 (PST) Received: from yshi-Precision-T5600.corp.ad.wrs.com (unknown-216-82.windriver.com. [147.11.216.82]) by smtp.gmail.com with ESMTPSA id c1sm27567351pap.36.2015.11.16.14.54.42 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 16 Nov 2015 14:54:42 -0800 (PST) From: Yang Shi To: ast@kernel.org, daniel@iogearbox.net, catalin.marinas@arm.com, will.deacon@arm.com, davem@davemloft.net Cc: zlim.lnx@gmail.com, xi.wang@gmail.com, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linaro-kernel@lists.linaro.org, yang.shi@linaro.org Subject: [PATCH V4 2/2] arm64: bpf: make BPF prologue and epilogue align with ARM64 AAPCS Date: Mon, 16 Nov 2015 14:35:35 -0800 Message-Id: <1447713335-29707-1-git-send-email-yang.shi@linaro.org> X-Mailer: git-send-email 2.0.2 In-Reply-To: References: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Save and restore FP/LR in BPF prog prologue and epilogue, save SP to FP in prologue in order to get the correct stack backtrace. However, ARM64 JIT used FP (x29) as eBPF fp register, FP is subjected to change during function call so it may cause the BPF prog stack base address change too. Use x25 to replace FP as BPF stack base register (fp). Since x25 is callee saved register, so it will keep intact during function call. It is initialized in BPF prog prologue when BPF prog is started to run everytime. Save and restore x25/x26 in BPF prologue and epilogue to keep them intact for the outside of BPF. Actually, x26 is unnecessary, but SP requires 16 bytes alignment. So, the BPF stack layout looks like: high original A64_SP => 0:+-----+ BPF prologue |FP/LR| current A64_FP => -16:+-----+ | ... | callee saved registers +-----+ | | x25/x26 BPF fp register => -80:+-----+ | | | ... | BPF prog stack | | | | current A64_SP => +-----+ | | | ... | Function call stack | | +-----+ low CC: Zi Shen Lim CC: Xi Wang Signed-off-by: Yang Shi --- V4 --> V3: * Save/restore x25 and x26 V3 --> V2: * Make FP point to FP' * Fix a compile warning arch/arm64/net/bpf_jit_comp.c | 44 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) -- 2.0.2 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/ Acked-by: Zi Shen Lim diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c index ac8b548..86a3253 100644 --- a/arch/arm64/net/bpf_jit_comp.c +++ b/arch/arm64/net/bpf_jit_comp.c @@ -50,7 +50,7 @@ static const int bpf2a64[] = { [BPF_REG_8] = A64_R(21), [BPF_REG_9] = A64_R(22), /* read-only frame pointer to access stack */ - [BPF_REG_FP] = A64_FP, + [BPF_REG_FP] = A64_R(25), /* temporary register for internal BPF JIT */ [TMP_REG_1] = A64_R(23), [TMP_REG_2] = A64_R(24), @@ -155,16 +155,47 @@ static void build_prologue(struct jit_ctx *ctx) stack_size += 4; /* extra for skb_copy_bits buffer */ stack_size = STACK_ALIGN(stack_size); + /* + * BPF prog stack layout + * + * high + * original A64_SP => 0:+-----+ BPF prologue + * |FP/LR| + * current A64_FP => -16:+-----+ + * | ... | callee saved registers + * +-----+ + * | | x25/x26 + * BPF fp register => -80:+-----+ + * | | + * | ... | BPF prog stack + * | | + * | | + * current A64_SP => +-----+ + * | | + * | ... | Function call stack + * | | + * +-----+ + * low + * + */ + + /* Save FP and LR registers to stay align with ARM64 AAPCS */ + emit(A64_PUSH(A64_FP, A64_LR, A64_SP), ctx); + emit(A64_MOV(1, A64_FP, A64_SP), ctx); + /* Save callee-saved register */ emit(A64_PUSH(r6, r7, A64_SP), ctx); emit(A64_PUSH(r8, r9, A64_SP), ctx); if (ctx->tmp_used) emit(A64_PUSH(tmp1, tmp2, A64_SP), ctx); - /* Set up frame pointer */ + /* Save fp (x25) and x26. SP requires 16 bytes alignment */ + emit(A64_PUSH(fp, A64_R(26), A64_SP), ctx); + + /* Set up BPF prog stack base register (x25) */ emit(A64_MOV(1, fp, A64_SP), ctx); - /* Set up BPF stack */ + /* Set up function call stack */ emit(A64_SUB_I(1, A64_SP, A64_SP, stack_size), ctx); /* Clear registers A and X */ @@ -190,14 +221,17 @@ static void build_epilogue(struct jit_ctx *ctx) /* We're done with BPF stack */ emit(A64_ADD_I(1, A64_SP, A64_SP, stack_size), ctx); + /* Restore fs (x25) and x26 */ + emit(A64_POP(fp, A64_R(26), A64_SP), ctx); + /* Restore callee-saved register */ if (ctx->tmp_used) emit(A64_POP(tmp1, tmp2, A64_SP), ctx); emit(A64_POP(r8, r9, A64_SP), ctx); emit(A64_POP(r6, r7, A64_SP), ctx); - /* Restore frame pointer */ - emit(A64_MOV(1, fp, A64_SP), ctx); + /* Restore FP/LR registers */ + emit(A64_POP(A64_FP, A64_LR, A64_SP), ctx); /* Set return value */ emit(A64_MOV(1, A64_R(0), r0), ctx);