mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jim Kukunas <james.t.kukunas@linux.intel.com>
To: Linux Kernel <linux-kernel@vger.kernel.org>, tom.zanussi@linux.intel.com
Cc: Arjan van de Ven <arjan@linux.intel.com>,
	"H. Peter Anvin" <hpa@zytor.com>,
	tglx@linutronix.de, mingo@redhat.com, x86@kernel.org
Subject: [PATCH 09/11] x86/xip: snip the kernel text out of the memory mapping
Date: Mon, 23 Mar 2015 00:46:38 -0700	[thread overview]
Message-ID: <1427096800-30452-10-git-send-email-james.t.kukunas@linux.intel.com> (raw)
In-Reply-To: <1427096800-30452-1-git-send-email-james.t.kukunas@linux.intel.com>

If the kernel tries to create an identity region for a memory range
that spans the kernel text, split it into two pieces, skipping the
text section. Otherwise, this will setup the standard text mapping,
which will point to the normal RAM location for text instead of the
XIP_BASE location.

Signed-off-by: Jim Kukunas <james.t.kukunas@linux.intel.com>
---
 arch/x86/mm/init.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 78 insertions(+)

diff --git a/arch/x86/mm/init.c b/arch/x86/mm/init.c
index a110efc..07b20c6 100644
--- a/arch/x86/mm/init.c
+++ b/arch/x86/mm/init.c
@@ -391,6 +391,82 @@ bool pfn_range_is_mapped(unsigned long start_pfn, unsigned long end_pfn)
 	return false;
 }
 
+#ifdef CONFIG_XIP_KERNEL
+/*
+ * Cut the .text virtual address out of mem range b/c the mapping
+ * is already correctly setup
+ */
+static inline void snip_xip_text(struct map_range *mr, int *nr_range)
+{
+	int i;
+
+	for (i = 0; i < *nr_range; i++) {
+		long diff;
+
+		if (mr[i].start <= CONFIG_PHYSICAL_START &&
+		mr[i].end <= CONFIG_PHYSICAL_START)
+			continue;
+		if (mr[i].start >= __pa_symbol(_sdata))
+			continue;
+
+		diff = mr[i].start - CONFIG_PHYSICAL_START;
+		if (diff < 0) { /* range starts below .text and includes it */
+			diff = mr[i].end - __pa_symbol(_sdata);
+
+			/* shorten segment so it ends just before .text */
+			mr[i].end = CONFIG_PHYSICAL_START;
+
+			/* if segment goes past .text, add 2nd segment*/
+			if (diff > 0) {
+				/* move next section down 1 */
+				if (i + 1 < *nr_range) {
+					memmove(&mr[i + 1], &mr[i + 2],
+						sizeof(struct map_range[
+						*nr_range - i - 2]));
+				}
+				mr[i + 1].start = __pa_symbol(_sdata);
+				mr[i + 1].end =  mr[i + 1].start + diff;
+				mr[i + 1].page_size_mask = 0;
+				*nr_range = *nr_range + 1;
+				i++;
+			}
+		} else if (diff == 0) {
+			diff = mr[i].end - __pa_symbol(_sdata);
+			if (diff > 0) {
+				mr[i].start = __pa_symbol(_sdata);
+				mr[i].end = mr[i].start + diff;
+				mr[i].page_size_mask = 0;
+			} else {
+				/* delete this range */
+				memmove(&mr[i + 1], &mr[i], sizeof(
+					struct map_range[*nr_range - i - 1]));
+				*nr_range = *nr_range - 1;
+				i--;
+			}
+		} else if (diff > 0) {
+			long ediff = mr[i].end - __pa_symbol(_sdata);
+
+			if (ediff > 0) {
+				mr[i].start = __pa_symbol(_sdata);
+				mr[i].end = mr[i].start + ediff;
+				mr[i].page_size_mask = 0;
+			} else {
+				/* delete this range */
+				memmove(&mr[i + 1], &mr[i], sizeof(
+					struct map_range[*nr_range - i - 1]));
+				*nr_range = *nr_range - 1;
+				i--;
+			}
+		}
+		break;
+	}
+}
+#else
+static inline void snip_xip_text(struct map_range *mr, int *mr_range)
+{
+}
+#endif
+
 /*
  * Setup the direct mapping of the physical memory at PAGE_OFFSET.
  * This runs before bootmem is initialized and gets pages directly from
@@ -409,6 +485,8 @@ unsigned long __init_refok init_memory_mapping(unsigned long start,
 	memset(mr, 0, sizeof(mr));
 	nr_range = split_mem_range(mr, 0, start, end);
 
+	snip_xip_text(mr, &nr_range);
+
 	for (i = 0; i < nr_range; i++)
 		ret = kernel_physical_mapping_init(mr[i].start, mr[i].end,
 						   mr[i].page_size_mask);
-- 
2.1.0


  parent reply	other threads:[~2015-03-23  7:56 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-23  7:46 [RFC] x86 XIP Jim Kukunas
2015-03-23  7:46 ` [PATCH 01/11] x86/xip: add XIP_KERNEL and XIP_BASE options Jim Kukunas
2015-03-23  7:46 ` [PATCH 02/11] x86/xip: Update address of sections in linker script Jim Kukunas
2015-03-23  7:46 ` [PATCH 03/11] x86/xip: copy writable sections into RAM Jim Kukunas
2015-03-23  7:46 ` [PATCH 04/11] x86/xip: XIP boot trampoline page tables Jim Kukunas
2015-03-23  7:46 ` [PATCH 05/11] x86/xip: reserve memblock for only data Jim Kukunas
2015-03-23  7:46 ` [PATCH 06/11] x86/xip: after paging trampoline, discard PMDs above _brk Jim Kukunas
2015-03-23  7:46 ` [PATCH 07/11] x86/xip: make e820_add_kernel_range() a NOP Jim Kukunas
2015-03-23  7:46 ` [PATCH 08/11] x86/xip: in setup_arch(), handle resource physical addr Jim Kukunas
2015-03-23  7:46 ` Jim Kukunas [this message]
2015-03-23  7:46 ` [PATCH 10/11] x86/xip: resolve alternative instructions at build Jim Kukunas
2015-03-23  8:33   ` Borislav Petkov
2015-03-23  8:38     ` Borislav Petkov
2015-03-25  0:50     ` Jim Kukunas
2015-03-25  8:44       ` Borislav Petkov
2015-03-23 22:40   ` Paul Bolle
2015-03-23  7:46 ` [PATCH 11/11] x86/xip: update _va() and _pa() macros Jim Kukunas
2015-03-23  8:09   ` Ingo Molnar
2015-03-23 15:09     ` Borislav Petkov
2015-03-23  8:07 ` [RFC] x86 XIP Ingo Molnar
2015-03-25  0:14   ` Jim Kukunas

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1427096800-30452-10-git-send-email-james.t.kukunas@linux.intel.com \
    --to=james.t.kukunas@linux.intel.com \
    --cc=arjan@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=tglx@linutronix.de \
    --cc=tom.zanussi@linux.intel.com \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®