From patchwork Tue Jun 12 11:36:37 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Julien Grall X-Patchwork-Id: 138331 Delivered-To: patch@linaro.org Received: by 2002:a2e:970d:0:0:0:0:0 with SMTP id r13-v6csp5235054lji; Tue, 12 Jun 2018 04:39:35 -0700 (PDT) X-Google-Smtp-Source: ADUXVKJNLqElQXNvvigIU8gZAUi88Frl2gvSLv2jfRKlzd8kZTByzLu7VgjjuBOCnL1Hd74CcxiJ X-Received: by 2002:a6b:fb04:: with SMTP id h4-v6mr3122736iog.193.1528803575349; Tue, 12 Jun 2018 04:39:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528803575; cv=none; d=google.com; s=arc-20160816; b=H3kXC2hKYhbbKE4ktcJzb4jiJprPRk0vTX4YvW0FHDcFaj2Y9x3SfxhrHJ7x67M3Dd iJKQ+6OwjCLL+FQlpHDv8AHPmtPA6Pa009WilnJCOpWdrvICPYwbdforlWe5q1KHXXFt uF4dVUMjya4W0rInpZxc0QbL//bb9gYuovykjkqUxHBHjpMA4JKNgKZX53p39ZoUqxpc 8aIkjLB8sNzJ8CI7jlskBetpvAB1+JE4FgA8QEtUoFu4uPvz1MP18OTkmFnXr0myCvmM fLIU61SQIyHd3p58AjJyVayGTvZ+Sw8pOxFmt3QmxmrllBn/TBoNIMwqUiBudjrPRW2E tt/A== 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=XFrKks2sB/xMNxPveEwDUoSnqw8v1S41g7E7lO4LhrM=; b=SUScKAJqfT33vijTmtN/iqhO/q6CP6VTqKF7GDahduI8Ifu/ZxZN/82M37Q2ruLS2v /iPsCA+rKWsjHaksOsaRSJrQCYLu7sB1UpN4mrjrU52/lj7TD2y+uNAKcYFELLa9gn2S GDiTyD6PgiDE6tEXLVoYNXC+PTohZS8EXj4ON2S6aYM6GgPm/373tlco+Y+Qxkw03zj2 9eQY/YV8gji4X+aDSVkYFD0TfsyVJ1G0B9hAljqbl+0D25H0OpnP4s9FUyWmkJgne29+ ibtdIMfKcchzvAss26D+gkhMcLSgHQ0dPuV1rbphNX58Dk1QXE0uPStgLLEvpRTye66C B0fw== 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 i16-v6si658475ioe.225.2018.06.12.04.39.35 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 12 Jun 2018 04:39:35 -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 1fShbi-0000r5-1j; Tue, 12 Jun 2018 11:37:02 +0000 Received: from us1-rack-dfw2.inumbo.com ([104.130.134.6]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1fShbg-0000qC-5O for xen-devel@lists.xenproject.org; Tue, 12 Jun 2018 11:37:00 +0000 X-Inumbo-ID: 93d9f179-6e34-11e8-9728-bc764e045a96 Received: from foss.arm.com (unknown [217.140.101.70]) by us1-rack-dfw2.inumbo.com (Halon) with ESMTP id 93d9f179-6e34-11e8-9728-bc764e045a96; Tue, 12 Jun 2018 13:34:33 +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 73C541596; Tue, 12 Jun 2018 04:36:58 -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 660FE3F318; Tue, 12 Jun 2018 04:36:57 -0700 (PDT) From: Julien Grall To: xen-devel@lists.xenproject.org Date: Tue, 12 Jun 2018 12:36:37 +0100 Message-Id: <20180612113643.32020-8-julien.grall@arm.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20180612113643.32020-1-julien.grall@arm.com> References: <20180612113643.32020-1-julien.grall@arm.com> Subject: [Xen-devel] [PATCH v3 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 interactions with Xen internal can be removed by specifying the offset between the region patch and the writable region for updating the instruction This simplification will also make it easier to integrate dynamic patching in a follow-up patch. Indeed, the callback address should be in an 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 v3: - Add stefano's reviewed-by 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); } /*