From patchwork Tue Jun 5 15:22:57 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Julien Grall X-Patchwork-Id: 137766 Delivered-To: patch@linaro.org Received: by 2002:a2e:970d:0:0:0:0:0 with SMTP id r13-v6csp1099086lji; Tue, 5 Jun 2018 08:26:13 -0700 (PDT) X-Google-Smtp-Source: ADUXVKKfImVUge/EZMhvb/I5huld3nKJsjR/80Zcp4rnIR23tnhADpdXJ/Wg5ysQ49PlySU/8qTt X-Received: by 2002:a6b:82a1:: with SMTP id m33-v6mr27131360ioi.56.1528212373251; Tue, 05 Jun 2018 08:26:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528212373; cv=none; d=google.com; s=arc-20160816; b=rM9QCNT+G0M3bvb7pSsE0HkGNJP7jqhseEx+ueByVAw47XB8F9MnljTNuMCAWH+HEj hhQ0A7yragQFLhWGYpI66oQnCIBk258bK+e5qZ7MP3pMdsR2/KOM9fRp+JaJJSlJWV8P 8tZxplntuUocv4mO1nu/7lk6eLT6/r6yVLA/KTCf5A5JX02pX/dVMDmYVvhqFJZqDDOY cX4KloYaFWnbTMrRcARj+9Rjzo4ufoCLiaPRu1yM199H0pTKRzHjou90Na6KvSZpoRZR 6sOx0+e6bOGsAhgamFnRIyiuyh/jvFJp4S1+n60KVLOpeWdkd5zxaZGsXE8tWGohFZzd wvtQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc :list-subscribe:list-help:list-post:list-unsubscribe:list-id :precedence:subject:references:in-reply-to:message-id:date:to:from :arc-authentication-results; bh=Lv8Ty3NRBlqB8Uja8s0vo4qQ94p/bwg+81G7jGLRD8E=; b=piKR8HRvR4X6zD29hyTPyUyvFTPr0R2g41K1P8CqtKBSpLTStYCb0rOdI3B8fYrgAJ VWoAHAoQln6wyjbLcXcwVQeKjl0Ivh8Ul49n6suZCPq76pRabIvBcVS50ZbWWma89nE/ kPGPyt6ALXA27RJVhug1mFikKD9kc30zg7487beG6Q+HBZAFDAbXwlbIvEgxXeEo04Bd //qrWpuV+zQ+FcHn1iX7S+uoaSUIhgMWvgrPLc6AGNFeih03qB2HVQ6pFJkAG3ty1PNF NT5sRwanPIrv1zQUwGG0fdAnopUE71kbdexhi8YcqE09AMHbrcyLYpA24eQ+dWmCTonQ yhvg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of xen-devel-bounces@lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Return-Path: Received: from lists.xenproject.org (lists.xenproject.org. [192.237.175.120]) by mx.google.com with ESMTPS id 125-v6si33733553iow.201.2018.06.05.08.26.13 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 05 Jun 2018 08:26:13 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of xen-devel-bounces@lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of xen-devel-bounces@lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1fQDo1-0005CA-LU; Tue, 05 Jun 2018 15:23:29 +0000 Received: from us1-rack-dfw2.inumbo.com ([104.130.134.6]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1fQDo0-0005Bx-80 for xen-devel@lists.xenproject.org; Tue, 05 Jun 2018 15:23:28 +0000 X-Inumbo-ID: 14a85d44-68d4-11e8-9728-bc764e045a96 Received: from foss.arm.com (unknown [217.140.101.70]) by us1-rack-dfw2.inumbo.com (Halon) with ESMTP id 14a85d44-68d4-11e8-9728-bc764e045a96; Tue, 05 Jun 2018 17:21:12 +0200 (CEST) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 885951596; Tue, 5 Jun 2018 08:23:26 -0700 (PDT) Received: from e108454-lin.cambridge.arm.com (e108454-lin.cambridge.arm.com [10.1.206.53]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 7AB613F557; Tue, 5 Jun 2018 08:23:25 -0700 (PDT) From: Julien Grall To: xen-devel@lists.xenproject.org Date: Tue, 5 Jun 2018 16:22:57 +0100 Message-Id: <20180605152303.14450-8-julien.grall@arm.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20180605152303.14450-1-julien.grall@arm.com> References: <20180605152303.14450-1-julien.grall@arm.com> Subject: [Xen-devel] [PATCH v1 07/13] xen/arm: Simplify alternative patching of non-writable region X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: andre.przywara@arm.com, Julien Grall , sstabellini@kernel.org MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" During the MMU setup process, Xen will set SCTLR_EL2.WNX (Write-Non-eXecutable) bit. Because of that, the alternative code need to re-mapped the region in a difference place in order to modify the text section. At the moment, the function patching the code is only aware of the re-mapped region. This requires the caller to mess with Xen internal in order to have function such as is_active_kernel_text() working. All the interaction with Xen internal can be removed by specify the offset between the region patch and the writable region for updating the instruction This simplification will also make easier to integrate dynamic patching a in a follow-up patch. Indeed, the callback address should be in a original region and not re-mapped only which is writeable non-executable. This is part of XSA-263. Signed-off-by: Julien Grall Reviewed-by: Stefano Stabellini --- Cc: Konrad Rzeszutek Wilk Changes in v2: - Add commit message - Remove comment in the code that does not make sense anymore --- xen/arch/arm/alternative.c | 42 +++++++++++++----------------------------- 1 file changed, 13 insertions(+), 29 deletions(-) diff --git a/xen/arch/arm/alternative.c b/xen/arch/arm/alternative.c index 9ffdc475d6..936cf04956 100644 --- a/xen/arch/arm/alternative.c +++ b/xen/arch/arm/alternative.c @@ -97,12 +97,16 @@ static u32 get_alt_insn(const struct alt_instr *alt, /* * The region patched should be read-write to allow __apply_alternatives * to replacing the instructions when necessary. + * + * @update_offset: Offset between the region patched and the writable + * region for the update. 0 if the patched region is writable. */ -static int __apply_alternatives(const struct alt_region *region) +static int __apply_alternatives(const struct alt_region *region, + paddr_t update_offset) { const struct alt_instr *alt; - const u32 *replptr; - u32 *origptr; + const u32 *replptr, *origptr; + u32 *updptr; printk(XENLOG_INFO "alternatives: Patching with alt table %p -> %p\n", region->begin, region->end); @@ -118,6 +122,7 @@ static int __apply_alternatives(const struct alt_region *region) BUG_ON(alt->alt_len != alt->orig_len); origptr = ALT_ORIG_PTR(alt); + updptr = (void *)origptr + update_offset; replptr = ALT_REPL_PTR(alt); nr_inst = alt->alt_len / sizeof(insn); @@ -125,7 +130,7 @@ static int __apply_alternatives(const struct alt_region *region) for ( i = 0; i < nr_inst; i++ ) { insn = get_alt_insn(alt, origptr + i, replptr + i); - *(origptr + i) = cpu_to_le32(insn); + *(updptr + i) = cpu_to_le32(insn); } /* Ensure the new instructions reached the memory and nuke */ @@ -162,9 +167,6 @@ static int __apply_alternatives_multi_stop(void *unused) paddr_t xen_size = _end - _start; unsigned int xen_order = get_order_from_bytes(xen_size); void *xenmap; - struct virtual_region patch_region = { - .list = LIST_HEAD_INIT(patch_region.list), - }; BUG_ON(patched); @@ -177,31 +179,13 @@ static int __apply_alternatives_multi_stop(void *unused) /* Re-mapping Xen is not expected to fail during boot. */ BUG_ON(!xenmap); - /* - * If we generate a new branch instruction, the target will be - * calculated in this re-mapped Xen region. So we have to register - * this re-mapped Xen region as a virtual region temporarily. - */ - patch_region.start = xenmap; - patch_region.end = xenmap + xen_size; - register_virtual_region(&patch_region); + region.begin = __alt_instructions; + region.end = __alt_instructions_end; - /* - * Find the virtual address of the alternative region in the new - * mapping. - * alt_instr contains relative offset, so the function - * __apply_alternatives will patch in the re-mapped version of - * Xen. - */ - region.begin = (void *)__alt_instructions - (void *)_start + xenmap; - region.end = (void *)__alt_instructions_end - (void *)_start + xenmap; - - ret = __apply_alternatives(®ion); + ret = __apply_alternatives(®ion, xenmap - (void *)_start); /* The patching is not expected to fail during boot. */ BUG_ON(ret != 0); - unregister_virtual_region(&patch_region); - vunmap(xenmap); /* Barriers provided by the cache flushing */ @@ -235,7 +219,7 @@ int apply_alternatives(const struct alt_instr *start, const struct alt_instr *en .end = end, }; - return __apply_alternatives(®ion); + return __apply_alternatives(®ion, 0); } /*