[PATCH 09/11] x86/xip: snip the kernel text out of the memory mapping

From: Jim Kukunas
Date: Mon Mar 23 2015 - 03:56:42 EST


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@xxxxxxxxxxxxxxx>
---
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

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/