Skip to contentSkip to conversation

x86 XIP

RFC In review Jim Kukunas 4 participants 21 messages Mar 23, 2015 – Mar 25, 2015

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

Hi Folks,

This patchset introduces eXecute-In-Place (XIP) support for x86. Right now only minimal configurations are supported (32-bit only, no SMP, no PAE, and so on). My goal is to increase the number of supported configurations in the future based on what functionality is requested. This patchset only supports storage configurations where the kernel text and read-only data will always be readable.

I didn't create a special Makefile target for building xip images, like how ARM has xipImage. Instead, I'm just using the basic vmlinux ELF executable. The kernel must be built with CONFIG_XIP_BASE set to the physical address of the vmlinux file. Additionally, since the .text section is read-only, all of the alternative instructions need to be resolved at build-time. To accomplish this, the cpu features to enable are selected through a series of Kconfig options. In order to boot, the bootloader just needs to fill out the zero page (whose address startup_32() expects in esi), switch to 32-bit protected mode and then jump into startup_32(), which will be at CONFIG_XIP_BASE plus one page.

Thanks.

#1 lore raw

PATCH 1/11 x86/xip: add XIP_KERNEL and XIP_BASE options

The CONFIG_XIP_KERNEL Kconfig option enables eXecute-In-Place (XIP) support. When XIP_KERNEL is set, XIP_BASE points to the physical address of the vmlinux ELF file.

---
 arch/x86/Kconfig | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
arch/x86/Kconfig +19 -0
index b7d31ca..f5fa02c 100644
@@ -294,6 +294,25 @@ config ZONE_DMA
 
 	  If unsure, say Y.
 
+config XIP_KERNEL
+	bool "eXecute-In-Place (XIP) support" if (X86_32 && EXPERT && EMBEDDED)
+	depends on !MODULES && !X86_PAE && !SMP
+	default n
+	help
+	  With this option enabled, the text and any read-only segments of
+	  the kernel are not copied from their initial location to their usual
+	  location in RAM. As a result, when the kernel is located in storage
+	  that is addressable by the CPU, the kernel text and read-only data
+	  segments are never loaded into memory, thereby using less RAM.
+
+	  Only enable this option if you know what you're doing.
+
+config XIP_BASE
+	hex "Physical address of XIP kernel" if XIP_KERNEL
+	default "0xFF800000"
+	help
+	  The physical address for the beginning of the vmlinux file.
+
 config SMP
 	bool "Symmetric multi-processing support"
 	---help---
Signature · 1 line
2.1.0
#2 lore raw

PATCH 2/11 x86/xip: Update address of sections in linker script

In reply to Jim Kukunas

In order to update the LMA for each section, according to CONFIG_XIP_BASE, this patch uses the preprocessor to change the arguments passed to the AT keyword. Each LMA is updated to that symbol's physical address.

The text section is aligned to a page so that the ELF header at the beginning of XIP_BASE isn't mapped into the linear address space. Also the initial location counter is incremented to account for the ELF header.

---
 arch/x86/include/asm/boot.h   |  4 ++++
 arch/x86/kernel/vmlinux.lds.S | 17 +++++++++++++++--
 2 files changed, 19 insertions(+), 2 deletions(-)
arch/x86/include/asm/boot.h +4 -0
index 4fa687a..a128c71 100644
@@ -10,6 +10,10 @@
 				+ (CONFIG_PHYSICAL_ALIGN - 1)) \
 				& ~(CONFIG_PHYSICAL_ALIGN - 1))
 
+#ifdef CONFIG_XIP_KERNEL
+#define PHYS_XIP_OFFSET (CONFIG_XIP_BASE - (LOAD_OFFSET + LOAD_PHYSICAL_ADDR))
+#endif
+
 /* Minimum kernel alignment, as a power of two */
 #ifdef CONFIG_X86_64
 #define MIN_KERNEL_ALIGN_LG2	PMD_SHIFT
arch/x86/kernel/vmlinux.lds.S +15 -2
index 00bf300..414a1ac 100644
@@ -20,6 +20,15 @@
 #define LOAD_OFFSET __START_KERNEL_map
 #endif
 
+#ifdef CONFIG_XIP_KERNEL
+#define AT(x)	AT(x + LOAD_OFFSET + PHYS_XIP_OFFSET)
+#define TEXT_ALIGN	ALIGN(0x1000)
+#define DATA_ALIGN	ALIGN(0x200000)
+#else
+#define TEXT_ALIGN
+#define DATA_ALIGN
+#endif
+
 #include <asm-generic/vmlinux.lds.h>
 #include <asm/asm-offsets.h>
 #include <asm/thread_info.h>
@@ -89,8 +98,12 @@ SECTIONS
         phys_startup_64 = startup_64 - LOAD_OFFSET;
 #endif
 
+#ifdef CONFIG_XIP_KERNEL
+	. += SIZEOF_HEADERS;
+#endif
+
 	/* Text and read-only data */
-	.text :  AT(ADDR(.text) - LOAD_OFFSET) {
+	.text :  AT(ADDR(.text) - LOAD_OFFSET) TEXT_ALIGN {
 		_text = .;
 		/* bootstrapping code */
 		HEAD_TEXT
@@ -121,7 +134,7 @@ SECTIONS
 	X64_ALIGN_DEBUG_RODATA_END
 
 	/* Data */
-	.data : AT(ADDR(.data) - LOAD_OFFSET) {
+	.data : AT(ADDR(.data) - LOAD_OFFSET) DATA_ALIGN {
 		/* Start of data section */
 		_sdata = .;
 
Signature · 1 line
2.1.0
#3 lore raw

PATCH 3/11 x86/xip: copy writable sections into RAM

In reply to Jim Kukunas

Loads all writable and non-zero sections into their VMA.

---
 arch/x86/include/asm/sections.h |  4 ++++
 arch/x86/kernel/head_32.S       | 22 ++++++++++++++++++++++
 arch/x86/kernel/vmlinux.lds.S   |  4 ++++
 3 files changed, 30 insertions(+)
arch/x86/include/asm/sections.h +4 -0
index 0a52424..9535e95 100644
@@ -11,4 +11,8 @@ extern struct exception_table_entry __stop___ex_table[];
 extern char __end_rodata_hpage_align[];
 #endif
 
+#ifdef CONFIG_XIP_KERNEL
+extern char phys_sdata[];
+#endif
+
 #endif	/* _ASM_X86_SECTIONS_H */
arch/x86/kernel/head_32.S +22 -0
index f36bd42..80f344a 100644
@@ -86,6 +86,28 @@ RESERVE_BRK(pagetables, INIT_MAP_SIZE)
  */
 __HEAD
 ENTRY(startup_32)
+
+#ifdef CONFIG_XIP_KERNEL
+	/*
+	 * Copy writable sections into RAM
+	 */
+
+	movl %esi, %ebp	# Preserve pointer to zero-page
+
+	leal pa(_sdata), %edi
+	leal phys_edata, %ecx
+	leal phys_sdata, %esi
+	subl %esi, %ecx
+
+	cld
+xip_data_cp:
+	lodsb
+	stosb
+	loop xip_data_cp
+
+	movl %ebp, %esi
+#endif
+
 	movl pa(stack_start),%ecx
 	
 	/* test KEEP_SEGMENTS flag to see if the bootloader is asking
arch/x86/kernel/vmlinux.lds.S +4 -0
index 414a1ac..59a9edb 100644
@@ -133,6 +133,9 @@ SECTIONS
 	RO_DATA(PAGE_SIZE)
 	X64_ALIGN_DEBUG_RODATA_END
 
+	phys_sdata = LOADADDR(.data);
+	phys_edata = phys_sdata + (_end_nonzero - _sdata);
+
 	/* Data */
 	.data : AT(ADDR(.data) - LOAD_OFFSET) DATA_ALIGN {
 		/* Start of data section */
@@ -319,6 +322,7 @@ SECTIONS
 		NOSAVE_DATA
 	}
 #endif
+	_end_nonzero = .;
 
 	/* BSS */
 	. = ALIGN(PAGE_SIZE);
Signature · 1 line
2.1.0
#4 lore raw

PATCH 4/11 x86/xip: XIP boot trampoline page tables

In reply to Jim Kukunas

Constructs the trampoline page tables for early XIP boot.

---
 arch/x86/kernel/head_32.S | 85 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 85 insertions(+)
arch/x86/kernel/head_32.S +85 -0
index 80f344a..642d73b 100644
@@ -227,6 +227,90 @@ xip_data_cp:
 	movl %eax,pa(initial_pg_pmd+0x1000*KPMDS-8)
 #else	/* Not PAE */
 
+#ifdef CONFIG_XIP_KERNEL
+	movl $pa(__brk_base), %edi
+	movl $pa(initial_page_table), %edx
+
+	movl $PTE_IDENT_ATTR, %eax	/* EAX holds identity mapping addr */
+	movl $__PAGE_OFFSET + PTE_IDENT_ATTR, %ebx /* EBX holds kernel addr */
+
+.Lxip_mapping:
+/* Allocate or Load Identity PDE */
+	leal -PTE_IDENT_ATTR(%eax), %ebp
+	andl $0xFFC00000, %ebp
+	shrl $20, %ebp
+	movl (%edx, %ebp), %ecx
+
+	test %ecx, %ecx
+	jnz .Lskip_ident_pde_alloc
+	leal PDE_IDENT_ATTR(%edi), %ecx
+	addl $4096, %edi
+	movl %ecx, (%edx, %ebp)
+
+.Lskip_ident_pde_alloc:
+	leal -PDE_IDENT_ATTR(%ecx), %ecx
+	leal -PTE_IDENT_ATTR(%eax), %ebp
+	andl $0x3FF000, %ebp
+	shrl $10, %ebp
+	movl %eax, (%ecx, %ebp)
+
+/* Allocate or Load PAGE_OFFSET PDE */
+	leal -PTE_IDENT_ATTR(%ebx), %ebp
+	andl $0xFFC00000, %ebp
+	shrl $20, %ebp
+	movl (%edx, %ebp), %ecx
+
+	test %ecx, %ecx
+	jnz .Lskip_offset_pde_alloc
+	leal PDE_IDENT_ATTR(%edi), %ecx
+	addl $4096, %edi
+	movl %ecx, (%edx, %ebp)
+
+.Lskip_offset_pde_alloc:
+	leal -PDE_IDENT_ATTR(%ecx), %ecx
+	leal -PTE_IDENT_ATTR(%ebx), %ebp
+	andl $0x3FF000, %ebp
+	shrl $10, %ebp
+	movl %eax, (%ecx, %ebp)
+
+	addl $4096, %eax
+	addl $4096, %ebx
+
+	cmpl $CONFIG_PHYSICAL_START + PTE_IDENT_ATTR, %eax
+	je   .Lsetup_text_addr
+
+	cmpl $phys_sdata + PTE_IDENT_ATTR, %eax
+	je   .Lsetup_data_addr
+
+	cmpl $pa(_end) + MAPPING_BEYOND_END + PTE_IDENT_ATTR, %eax
+	je   .Ldone
+
+	jmp  .Lxip_mapping
+
+.Lsetup_text_addr:
+	movl $CONFIG_XIP_BASE + 4096 + PTE_IDENT_ATTR, %eax
+	movl $_text, %ebx
+	addl $PTE_IDENT_ATTR, %ebx
+	jmp  .Lxip_mapping
+
+.Lsetup_data_addr:
+	movl $pa(_sdata), %eax
+	addl $PTE_IDENT_ATTR, %eax
+	movl $_sdata, %ebx
+	addl $PTE_IDENT_ATTR, %ebx
+	jmp  .Lxip_mapping
+.Ldone:
+	addl $__PAGE_OFFSET, %edi
+	movl %edi, pa(_brk_end)
+	movl $pa(_end) + MAPPING_BEYOND_END + PTE_IDENT_ATTR, %eax
+	shrl $12, %eax
+	movl %eax, pa(max_pfn_mapped)
+
+	movl $pa(initial_pg_fixmap) + PTE_IDENT_ATTR, %eax
+	movl %eax, pa(initial_page_table + 0xFFC)
+
+#else
+
 page_pde_offset = (__PAGE_OFFSET >> 20);
 
 	movl $pa(__brk_base), %edi
@@ -257,6 +341,7 @@ page_pde_offset = (__PAGE_OFFSET >> 20);
 	movl $pa(initial_pg_fixmap)+PDE_IDENT_ATTR,%eax
 	movl %eax,pa(initial_page_table+0xffc)
 #endif
+#endif
 
 #ifdef CONFIG_PARAVIRT
 	/* This is can only trip for a broken bootloader... */
Signature · 1 line
2.1.0
#5 lore raw

PATCH 5/11 x86/xip: reserve memblock for only data

In reply to Jim Kukunas

Nothing is loaded at the usual spot for .text, starting at CONFIG_PHYSICAL_START, so we don't reserve it. Additionally, the physical address of the _text isn't going to be physically contiguous with _data.

---
 arch/x86/kernel/setup.c | 5 +++++
 1 file changed, 5 insertions(+)
arch/x86/kernel/setup.c +5 -0
index 98dc931..e2d85c4 100644
@@ -869,8 +869,13 @@ dump_kernel_offset(struct notifier_block *self, unsigned long v, void *p)
 
 void __init setup_arch(char **cmdline_p)
 {
+#ifdef CONFIG_XIP_KERNEL
+	memblock_reserve(__pa_symbol(_sdata),
+			(unsigned long)__bss_stop - (unsigned long)_sdata);
+#else
 	memblock_reserve(__pa_symbol(_text),
 			 (unsigned long)__bss_stop - (unsigned long)_text);
+ #endif
 
 	early_reserve_initrd();
 
Signature · 1 line
2.1.0
#6 lore raw

PATCH 6/11 x86/xip: after paging trampoline, discard PMDs above _brk

In reply to Jim Kukunas

In the likely case that XIP_BASE is above PAGE_OFFSET, we want to discard any early identity mappings. So rather than keeping every PMD above PAGE_OFFSET, only copy the ones from PAGE_OFFSET to the last PMD of _end. At this point, the linear address space should look normal.

---
 arch/x86/include/asm/pgtable.h | 7 +++++++
 arch/x86/kernel/setup.c        | 2 +-
 2 files changed, 8 insertions(+), 1 deletion(-)
arch/x86/include/asm/pgtable.h +7 -0
index a0c35bf..5eaba7d 100644
@@ -660,6 +660,13 @@ static inline int pgd_none(pgd_t pgd)
 #define KERNEL_PGD_BOUNDARY	pgd_index(PAGE_OFFSET)
 #define KERNEL_PGD_PTRS		(PTRS_PER_PGD - KERNEL_PGD_BOUNDARY)
 
+#ifdef CONFIG_XIP_KERNEL
+#define BOOT_PGD_COPY_PTRS \
+	((pgd_index((unsigned long)_end)  - pgd_index(PAGE_OFFSET)) + 4)
+#else
+#define BOOT_PGD_COPY_PTRS	KERNEL_PGD_PTRS
+#endif
+
 #ifndef __ASSEMBLY__
 
 extern int direct_gbpages;
arch/x86/kernel/setup.c +1 -1
index e2d85c4..d276ebf 100644
@@ -894,7 +894,7 @@ void __init setup_arch(char **cmdline_p)
 	 */
 	clone_pgd_range(swapper_pg_dir     + KERNEL_PGD_BOUNDARY,
 			initial_page_table + KERNEL_PGD_BOUNDARY,
-			KERNEL_PGD_PTRS);
+			BOOT_PGD_COPY_PTRS);
 
 	load_cr3(swapper_pg_dir);
 	/*
Signature · 1 line
2.1.0
#7 lore raw

PATCH 7/11 x86/xip: make e820_add_kernel_range() a NOP

In reply to Jim Kukunas

e820_add_kernel_range() checks whether the kernel text is present in the e820 map, and marked as usable RAM. If not, it modifies the e820 map accordingly.

For XIP, that is unnecessary since the kernel text won't be loaded in RAM.

---
 arch/x86/kernel/setup.c | 6 ++++++
 1 file changed, 6 insertions(+)
arch/x86/kernel/setup.c +6 -0
index d276ebf..74fc6c8 100644
@@ -787,6 +787,7 @@ static void __init trim_bios_range(void)
 }
 
 /* called before trim_bios_range() to spare extra sanitize */
+#ifndef CONFIG_XIP_KERNEL
 static void __init e820_add_kernel_range(void)
 {
 	u64 start = __pa_symbol(_text);
@@ -806,6 +807,11 @@ static void __init e820_add_kernel_range(void)
 	e820_remove_range(start, size, E820_RAM, 0);
 	e820_add_region(start, size, E820_RAM);
 }
+#else
+static void __init e820_add_kernel_range(void)
+{
+}
+#endif
 
 static unsigned reserve_low = CONFIG_X86_RESERVE_LOW << 10;
 
Signature · 1 line
2.1.0
#8 lore raw

PATCH 8/11 x86/xip: in setup_arch(), handle resource physical addr

In reply to Jim Kukunas

set code_resources to proper physical addr in setup_arch()

---
 arch/x86/kernel/setup.c | 7 +++++++
 1 file changed, 7 insertions(+)
arch/x86/kernel/setup.c +7 -0
index 74fc6c8..f044453 100644
@@ -986,9 +986,16 @@ void __init setup_arch(char **cmdline_p)
 
 	mpx_mm_init(&init_mm);
 
+#ifndef CONFIG_XIP_KERNEL
 	code_resource.start = __pa_symbol(_text);
 	code_resource.end = __pa_symbol(_etext)-1;
+	data_resource.start = _pa(_sdata)-1;
+#else
+	code_resource.start = CONFIG_XIP_BASE;
+	code_resource.end = (phys_addr_t)phys_sdata-1;
 	data_resource.start = __pa_symbol(_etext);
+#endif
+
 	data_resource.end = __pa_symbol(_edata)-1;
 	bss_resource.start = __pa_symbol(__bss_start);
 	bss_resource.end = __pa_symbol(__bss_stop)-1;
Signature · 1 line
2.1.0
#9 lore raw

PATCH 9/11 x86/xip: snip the kernel text out of the memory mapping

In reply to Jim Kukunas

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.

---
 arch/x86/mm/init.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 78 insertions(+)
arch/x86/mm/init.c +78 -0
index a110efc..07b20c6 100644
@@ -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);
Signature · 1 line
2.1.0
#10 lore raw

PATCH 10/11 x86/xip: resolve alternative instructions at build

In reply to Jim Kukunas

Since the .text section can't be updated at run-time, remove the .alternatives sections and update the .text at build time. To pick the proper instructions, Kconfig options are exposed for each X86_FEATURE that needed to be resolved. Each X86_FEATURE gets a corresponding CONFIG_XIP_ENABLE_X86_FEATURE_ option. Based on whether this option is set, a resolver macro is setup to either generate that instruction, or the fallback. The resolver needs to be defined for each FEATURE, and the proper one is chosen via preprocessor string pasting.

This approach is horrific and ugly. A better approach might be to add an additional build step that, after generating the vmlinux file, goes through the alternatives section and performs the fixups on the file.

At the very least, a script like mkcapflags.sh could generate the resolver functions automatically. But since it's adding Kconfig options, it would need to run unconditionally before any of the config related Makefile targets.

---
 arch/x86/Kconfig                       |  45 +++++++++
 arch/x86/include/asm/alternative-xip.h | 161 +++++++++++++++++++++++++++++++++
 arch/x86/include/asm/alternative.h     |   5 +
 arch/x86/kernel/alternative.c          |   7 ++
 arch/x86/kernel/cpu/bugs.c             |   2 +
 arch/x86/kernel/setup.c                |   2 +
 arch/x86/kernel/smpboot.c              |   2 +
 arch/x86/kernel/vmlinux.lds.S          |   2 +
 arch/x86/vdso/vma.c                    |   2 +
 9 files changed, 228 insertions(+)
 create mode 100644 arch/x86/include/asm/alternative-xip.h
arch/x86/Kconfig +45 -0
index f5fa02c..dff781d 100644
@@ -313,6 +313,51 @@ config XIP_BASE
 	help
 	  The physical address for the beginning of the vmlinux file.
 
+menu "XIP Alternative Instructions"
+	depends on XIP_KERNEL
+
+	config XIP_ENABLE_X86_FEATURE_POPCNT
+	bool "Enable POPCNT alternative instructions"
+	default n
+
+	config XIP_ENABLE_X86_BUG_11AP
+	bool "Enable 11AP alternative instructions"
+	default n
+
+	config XIP_ENABLE_X86_FEATURE_XMM2
+	bool "Enable XMM2 alternative instructions"
+	default n
+
+	config XIP_ENABLE_X86_FEATURE_MFENCE_RDTSC
+	bool "Enable MFENCE_RDTSC alternative instructions"
+	default n
+
+	config XIP_ENABLE_X86_FEATURE_LFENCE_RDTSC
+	bool "Enable LFENCE_RDTSC alternative instructions"
+	default n
+
+	config XIP_ENABLE_X86_FEATURE_3DNOW
+	bool "Enable 3DNOW alternative instructions"
+	default n
+
+	config XIP_ENABLE_X86_FEATURE_XMM
+	bool "Enabled XMM alternative instructions"
+	default n
+
+	config XIP_ENABLE_X86_FEATURE_CLFLUSHOPT
+	bool "Enable CLFLUSHOPT alternative instructions"
+	default n
+
+	config XIP_ENABLE_X86_FEATURE_XSAVEOPT
+	bool "Enable XSAVEOPT alternative instructions"
+	default n
+
+	config XIP_ENABLE_X86_FEATURE_XSAVES
+	bool "Enable XSAVES alternative instructions"
+	default n
+
+endmenu
+
 config SMP
 	bool "Symmetric multi-processing support"
 	---help---
arch/x86/include/asm/alternative-xip.h new file +161 -0

161 lines not on this page · raw message

arch/x86/include/asm/alternative.h +5 -0
index 473bdbe..559a2cd 100644
@@ -7,6 +7,10 @@
 #include <asm/asm.h>
 #include <asm/ptrace.h>
 
+#ifdef CONFIG_XIP_KERNEL
+#include <asm/alternative-xip.h>
+#else
+
 /*
  * Alternative inline assembly for SMP.
  *
@@ -242,4 +246,5 @@ extern void *text_poke(void *addr, const void *opcode, size_t len);
 extern int poke_int3_handler(struct pt_regs *regs);
 extern void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler);
 
+#endif /* !CONFIG_XIP_KERNEL */
 #endif /* _ASM_X86_ALTERNATIVE_H */
arch/x86/kernel/alternative.c +7 -0
index 703130f..c337552 100644
@@ -21,6 +21,8 @@
 #include <asm/io.h>
 #include <asm/fixmap.h>
 
+#ifndef CONFIG_XIP_KERNEL
+
 #define MAX_PATCH_LEN (255-1)
 
 static int __initdata_or_module debug_alternative;
@@ -590,6 +592,8 @@ static void do_sync_core(void *info)
 	sync_core();
 }
 
+#endif /* !CONFIG_XIP_KERNEL */
+
 static bool bp_patching_in_progress;
 static void *bp_int3_handler, *bp_int3_addr;
 
@@ -611,6 +615,8 @@ int poke_int3_handler(struct pt_regs *regs)
 
 }
 
+#ifndef CONFIG_XIP_KERNEL
+
 /**
  * text_poke_bp() -- update instructions on live kernel on SMP
  * @addr:	address to patch
@@ -674,4 +680,5 @@ void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler)
 
 	return addr;
 }
+#endif /* !CONFIG_XIP_KERNEL */
 
arch/x86/kernel/cpu/bugs.c +2 -0
index 0344534..4c184a5 100644
@@ -83,7 +83,9 @@ void __init check_bugs(void)
 
 	init_utsname()->machine[1] =
 		'0' + (boot_cpu_data.x86 > 6 ? 6 : boot_cpu_data.x86);
+#ifndef CONFIG_XIP_KERNEL
 	alternative_instructions();
+#endif
 
 	/*
 	 * kernel_fpu_begin/end() in check_fpu() relies on the patched
arch/x86/kernel/setup.c +2 -0
index f044453..ed541b9 100644
@@ -1275,7 +1275,9 @@ void __init setup_arch(char **cmdline_p)
 
 	mcheck_init();
 
+#ifndef CONFIG_XIP_KERNEL
 	arch_init_ideal_nops();
+#endif
 
 	register_refined_jiffies(CLOCK_TICK_RATE);
 
arch/x86/kernel/smpboot.c +2 -0
index febc6aa..bebd323 100644
@@ -797,7 +797,9 @@ static int do_boot_cpu(int apicid, int cpu, struct task_struct *idle)
 	unsigned long timeout;
 
 	/* Just in case we booted with a single CPU. */
+#ifndef CONFIG_XIP_KERNEL
 	alternatives_enable_smp();
+#endif
 
 	idle->thread.sp = (unsigned long) (((struct pt_regs *)
 			  (THREAD_SIZE +  task_stack_page(idle))) - 1);
arch/x86/kernel/vmlinux.lds.S +2 -0
index 59a9edb..fc66ebf 100644
@@ -228,6 +228,7 @@ SECTIONS
 	}
 #endif
 
+#ifndef CONFIG_XIP_KERNEL
 	/*
 	 * start address and size of operations which during runtime
 	 * can be patched with virtualization friendly instructions or
@@ -261,6 +262,7 @@ SECTIONS
 	.altinstr_replacement : AT(ADDR(.altinstr_replacement) - LOAD_OFFSET) {
 		*(.altinstr_replacement)
 	}
+#endif
 
 	/*
 	 * struct iommu_table_entry entries are injected in this section.
arch/x86/vdso/vma.c +2 -0
index 1c9f750..ba336ef 100644
@@ -34,9 +34,11 @@ void __init init_vdso_image(const struct vdso_image *image)
 		image->text_mapping.pages[i] =
 			virt_to_page(image->data + i*PAGE_SIZE);
 
+#ifndef CONFIG_XIP_KERNEL
 	apply_alternatives((struct alt_instr *)(image->data + image->alt),
 			   (struct alt_instr *)(image->data + image->alt +
 						image->alt_len));
+#endif
 }
 
 struct linux_binprm;
Signature · 1 line
2.1.0
maintainer
#11 lore raw

Re: [PATCH 10/11] x86/xip: resolve alternative instructions at build

Quoted 10 lines · Jim Kukunas

Since the .text section can't be updated at run-time, remove the .alternatives sections and update the .text at build time. To pick the proper instructions, Kconfig options are exposed for each X86_FEATURE that needed to be resolved. Each X86_FEATURE gets a corresponding CONFIG_XIP_ENABLE_X86_FEATURE_ option. Based on whether this option is set, a resolver macro is setup to either generate that instruction, or the fallback. The resolver needs to be defined for each FEATURE, and the proper one is chosen via preprocessor string pasting.

This approach is horrific and ugly.

You said it.

And with XIP enabled - whatever that means, your announce message could explain a lot more and more verbosely what this whole patchset is all about - this kernel is not going to be generic anymore but it will be destined only for the machine it is being built for, correct?

If that is so, how are distros supposed to ship one kernel with XIP or is this some obscure feature distros won't have to enable anyway?

Concerning this particular patch, I'd suggest a switch which simply disables alternatives patching at run time so that you don't add this ifdeffery to alternative.c

Btw, why do you even have to disable the alternatives? I see this in your patch 1/11:

+         location in RAM. As a result, when the kernel is located in storage
+         that is addressable by the CPU, the kernel text and read-only data
+         segments are never loaded into memory, thereby using less RAM.

is this it? To save some memory? Probably embedded, maybe some light bulb running linux... Yuck.

Thanks.

Signature · 5 lines
Regards/Gruss,
    Boris.

ECO tip #101: Trim your mails when you reply.
--
maintainer
#12 lore raw

Re: [PATCH 10/11] x86/xip: resolve alternative instructions at build

Quoted 2 lines · Borislav Petkov

is this it? To save some memory? Probably embedded, maybe some light bulb running linux... Yuck.

Oh, one more thing: AFAICT, you're adding the whole XIP functionality with a bunch of ifdeffery in the main code paths. It might be better to extract and put all that code in arch/x86/xip/, away from the generic paths. I'm thinking there will be more XIP code coming besides this patchset so designing it clean from the get-go should pay out handsomely later.

Hmm.

Signature · 5 lines
Regards/Gruss,
    Boris.

ECO tip #101: Trim your mails when you reply.
--
#13 lore raw

Re: [PATCH 10/11] x86/xip: resolve alternative instructions at build

In reply to Borislav Petkov
Quoted 10 lines · Borislav Petkov
Quoted 9 lines · Jim Kukunas

Since the .text section can't be updated at run-time, remove the .alternatives sections and update the .text at build time. To pick the proper instructions, Kconfig options are exposed for each X86_FEATURE that needed to be resolved. Each X86_FEATURE gets a corresponding CONFIG_XIP_ENABLE_X86_FEATURE_ option. Based on whether this option is set, a resolver macro is setup to either generate that instruction, or the fallback. The resolver needs to be defined for each FEATURE, and the proper one is chosen via preprocessor string pasting.

Quoted 8 lines
Quoted 1 line

This approach is horrific and ugly.

You said it.

And with XIP enabled - whatever that means, your announce message could explain a lot more and more verbosely what this whole patchset is all about - this kernel is not going to be generic anymore but it will be destined only for the machine it is being built for, correct?

Please see my response to Ingo for more information about the patchset.

Yes, regardless of how it's implemented, selecting alternatives at build time will produce a non-generic kernel (unless all alternative instructions are disabled and just the fallbacks are used).

Quoted 2 lines

If that is so, how are distros supposed to ship one kernel with XIP or is this some obscure feature distros won't have to enable anyway?

XIP isn't a general feature that distros are going to be enabling. It's designed for a very specific usage where people are building very custom kernels.

Quoted 3 lines

Concerning this particular patch, I'd suggest a switch which simply disables alternatives patching at run time so that you don't add this ifdeffery to alternative.c

I'll look into this.

Quoted 9 lines

Btw, why do you even have to disable the alternatives? I see this in your patch 1/11:

(quoted diff) +3 -0
+         location in RAM. As a result, when the kernel is located in storage
+         that is addressable by the CPU, the kernel text and read-only data
+         segments are never loaded into memory, thereby using less RAM.

is this it? To save some memory? Probably embedded, maybe some light bulb running linux... Yuck.

Alternatives are disabled because the kernel text will be read-only. For example, consider the kernel image being stored in and executing from ROM. Cutting the kernel text and read-only data out of RAM really helps Linux scale down to smaller systems.

So yes, it's for embedded. Linux has a lot to offer in that area.

Thanks.

Signature · 2 lines
Jim Kukunas
Intel Open Source Technology Center
maintainer
#14 lore raw

Re: [PATCH 10/11] x86/xip: resolve alternative instructions at build

Quoted 3 lines · Jim Kukunas

XIP isn't a general feature that distros are going to be enabling. It's designed for a very specific usage where people are building very custom kernels.

So something very very special which is not going to be enabled on distros...

But it is touching main x86 code paths a good deal. We should think about abstracting it out of the way. The current EFI ifdeffery is already nasty and sprinkling #ifdef CONFIG_XIP_KERNEL in addition is making it even worse. :/

Signature · 5 lines
Regards/Gruss,
    Boris.

ECO tip #101: Trim your mails when you reply.
--
#15 lore raw

Re: [PATCH 10/11] x86/xip: resolve alternative instructions at build

In reply to Jim Kukunas

Whitespace nits, mainly, I'm afraid.

arch/x86/Kconfig config XIP_BASE · line 319

Quoted 7 lines · Jim Kukunas
arch/x86/Kconfig +4 -0
 
+menu "XIP Alternative Instructions"
+	depends on XIP_KERNEL
+
+	config XIP_ENABLE_X86_FEATURE_POPCNT

No need to indent this line.

arch/x86/Kconfig config XIP_BASE · line 323

Quoted 4 lines
arch/x86/Kconfig +4 -0
+	bool "Enable POPCNT alternative instructions"
+	default n
+
+	config XIP_ENABLE_X86_BUG_11AP

Ditto.

arch/x86/Kconfig config XIP_BASE · line 327

Quoted 4 lines
arch/x86/Kconfig +4 -0
+	bool "Enable 11AP alternative instructions"
+	default n
+
+	config XIP_ENABLE_X86_FEATURE_XMM2

Etc.

Quoted 23 lines
arch/x86/Kconfig +23 -0
+	bool "Enable XMM2 alternative instructions"
+	default n
+
+	config XIP_ENABLE_X86_FEATURE_MFENCE_RDTSC
+	bool "Enable MFENCE_RDTSC alternative instructions"
+	default n
+
+	config XIP_ENABLE_X86_FEATURE_LFENCE_RDTSC
+	bool "Enable LFENCE_RDTSC alternative instructions"
+	default n
+
+	config XIP_ENABLE_X86_FEATURE_3DNOW
+	bool "Enable 3DNOW alternative instructions"
+	default n
+
+	config XIP_ENABLE_X86_FEATURE_XMM
+	bool "Enabled XMM alternative instructions"
+	default n
+
+	config XIP_ENABLE_X86_FEATURE_CLFLUSHOPT
+	bool "Enable CLFLUSHOPT alternative instructions"
+	default n
+

arch/x86/Kconfig config XIP_BASE · line 360

Quoted 10 lines
arch/x86/Kconfig +10 -0
+	config XIP_ENABLE_X86_FEATURE_XSAVEOPT
+	bool "Enable XSAVEOPT alternative instructions"
+	default n
+
+	config XIP_ENABLE_X86_FEATURE_XSAVES
+	bool "Enable XSAVES alternative instructions"
+	default n
+
+endmenu
+

The "n defaults are probably unneeded too.

Paul Bolle

#16 lore raw

PATCH 11/11 x86/xip: update _va() and _pa() macros

In reply to Jim Kukunas

For obtaining the physical address, we always take the slow path of slow_virt_to_phys(). In the future, we should probably special case data addresses to avoid walking the page table. For obtaining a virtual address, this patch introduces a slow path of slow_xip_phys_to_virt().

---
 arch/x86/include/asm/page.h | 15 +++++++++++++++
 arch/x86/mm/pageattr.c      | 11 +++++++++++
 2 files changed, 26 insertions(+)
arch/x86/include/asm/page.h +15 -0
index 802dde3..b54c7be 100644
@@ -6,6 +6,7 @@
 #ifdef __KERNEL__
 
 #include <asm/page_types.h>
+#include <asm/pgtable_types.h>
 
 #ifdef CONFIG_X86_64
 #include <asm/page_64.h>
@@ -37,8 +38,15 @@ static inline void copy_user_page(void *to, void *from, unsigned long vaddr,
 	alloc_page_vma(GFP_HIGHUSER | __GFP_ZERO | movableflags, vma, vaddr)
 #define __HAVE_ARCH_ALLOC_ZEROED_USER_HIGHPAGE
 
+
+#ifdef CONFIG_XIP_KERNEL	/* TODO special case text translations */
+#define __pa(x)		slow_virt_to_phys((void *)(x))
+#define __pa_nodebug	slow_virt_to_phys((void *)(x))
+#else
 #define __pa(x)		__phys_addr((unsigned long)(x))
 #define __pa_nodebug(x)	__phys_addr_nodebug((unsigned long)(x))
+#endif
+
 /* __pa_symbol should be used for C visible symbols.
    This seems to be the official gcc blessed way to do such arithmetic. */
 /*
@@ -51,7 +59,14 @@ static inline void copy_user_page(void *to, void *from, unsigned long vaddr,
 #define __pa_symbol(x) \
 	__phys_addr_symbol(__phys_reloc_hide((unsigned long)(x)))
 
+
+#ifdef CONFIG_XIP_KERNEL
+extern unsigned long slow_xip_phys_to_virt(phys_addr_t);
+
+#define __va(x)			((void *)slow_xip_phys_to_virt((phys_addr_t)x))
+#else
 #define __va(x)			((void *)((unsigned long)(x)+PAGE_OFFSET))
+#endif
 
 #define __boot_va(x)		__va(x)
 #define __boot_pa(x)		__pa(x)
arch/x86/mm/pageattr.c +11 -0
index 536ea2f..ca9e2ca 100644
@@ -383,6 +383,17 @@ static pte_t *_lookup_address_cpa(struct cpa_data *cpa, unsigned long address,
         return lookup_address(address, level);
 }
 
+#ifdef CONFIG_XIP_KERNEL
+unsigned long slow_xip_phys_to_virt(phys_addr_t x)
+{
+	if (x >= CONFIG_XIP_BASE && x <= (phys_addr_t)phys_sdata) {
+		unsigned long off = x - CONFIG_XIP_BASE;
+		return PAGE_OFFSET + off;
+	}
+	return x + PAGE_OFFSET;
+}
+#endif
+
 /*
  * Lookup the PMD entry for a virtual address. Return a pointer to the entry
  * or NULL if not present.
Signature · 1 line
2.1.0
#17 lore raw

Re: [PATCH 11/11] x86/xip: update _va() and _pa() macros

Quoted 26 lines · Jim Kukunas

For obtaining the physical address, we always take the slow path of slow_virt_to_phys(). In the future, we should probably special case data addresses to avoid walking the page table. For obtaining a virtual address, this patch introduces a slow path of slow_xip_phys_to_virt().

---
 arch/x86/include/asm/page.h | 15 +++++++++++++++
 arch/x86/mm/pageattr.c      | 11 +++++++++++
 2 files changed, 26 insertions(+)
arch/x86/include/asm/page.h +1 -0
index 802dde3..b54c7be 100644
@@ -6,6 +6,7 @@
 #ifdef __KERNEL__
 
 #include <asm/page_types.h>
+#include <asm/pgtable_types.h>
 
 #ifdef CONFIG_X86_64
 #include <asm/page_64.h>
@@ -37,8 +38,15 @@ static inline void copy_user_page(void *to, void *from, unsigned long vaddr,
 	alloc_page_vma(GFP_HIGHUSER | __GFP_ZERO | movableflags, vma, vaddr)

arch/x86/include/asm/page.h static inline void copy_user_page(void *to, void *from, unsigned long vaddr, · line 48

Quoted 10 lines
arch/x86/include/asm/page.h +6 -0
 #define __HAVE_ARCH_ALLOC_ZEROED_USER_HIGHPAGE
 
+
+#ifdef CONFIG_XIP_KERNEL	/* TODO special case text translations */
+#define __pa(x)		slow_virt_to_phys((void *)(x))
+#define __pa_nodebug	slow_virt_to_phys((void *)(x))
+#else
 #define __pa(x)		__phys_addr((unsigned long)(x))
 #define __pa_nodebug(x)	__phys_addr_nodebug((unsigned long)(x))
+#endif

Ugh. This needs to be fixed properly. slow_virt_to_phys() is very painful.

arch/x86/include/asm/page.h static inline void copy_user_page(void *to, void *from, unsigned long vaddr,

Quoted 10 lines
arch/x86/include/asm/page.h +10 -0
+#ifdef CONFIG_XIP_KERNEL
+unsigned long slow_xip_phys_to_virt(phys_addr_t x)
+{
+	if (x >= CONFIG_XIP_BASE && x <= (phys_addr_t)phys_sdata) {
+		unsigned long off = x - CONFIG_XIP_BASE;
+		return PAGE_OFFSET + off;
+	}
+	return x + PAGE_OFFSET;
+}
+#endif

So why not relocate the kernel properly before loading it into flash, instead of hacking these runtime translations into va/pa?

Thanks,

Ingo

maintainer
#18 lore raw

Re: [PATCH 11/11] x86/xip: update _va() and _pa() macros

Quoted 8 lines · Ingo Molnar

arch/x86/include/asm/page.h static inline void copy_user_page(void *to, void *from, unsigned long vaddr, · line 44

Quoted 8 lines
arch/x86/include/asm/page.h +4 -0
@@ -37,8 +38,15 @@ static inline void copy_user_page(void *to, void *from, unsigned long vaddr,
 	alloc_page_vma(GFP_HIGHUSER | __GFP_ZERO | movableflags, vma, vaddr)
 #define __HAVE_ARCH_ALLOC_ZEROED_USER_HIGHPAGE
 
+
+#ifdef CONFIG_XIP_KERNEL	/* TODO special case text translations */
+#define __pa(x)		slow_virt_to_phys((void *)(x))
+#define __pa_nodebug	slow_virt_to_phys((void *)(x))

This macro looks like it is missing an arg too.

Signature · 5 lines
Regards/Gruss,
    Boris.

ECO tip #101: Trim your mails when you reply.
--
#19 lore raw
In reply to Jim Kukunas
Quoted 5 lines · Jim Kukunas

Hi Folks,

This patchset introduces eXecute-In-Place (XIP) support for x86. [...]

So we'd need a lot better high level description than this:

- a bit of background description: what are the advantages of having the kernel image in non-RAM (flash), etc.

- on what hardware/bootloaders is or will be XIP supported?

Also, there should probably be some fail-safe mechanism included: such as to check whether caching attributes (MTRRs, PAT) are properly set for the XIP area (at minimum to not be uncacheable).

Thanks,

Ingo

#20 lore raw
Quoted 9 lines · Ingo Molnar
Quoted 5 lines · Jim Kukunas

Hi Folks,

This patchset introduces eXecute-In-Place (XIP) support for x86. [...]

So we'd need a lot better high level description than this:

In future patch revisions, I'll update my coverletter to include the information below.

Quoted 2 lines

- a bit of background description: what are the advantages of having the kernel image in non-RAM (flash), etc.

Currently for tiny memory-constrained embedded systems, the kernel configuration is usually stripped down in order to reduce the kernel's RAM footprint, freeing up more precious memory for user space and allowing the kernel to fit into smaller systems. With XIP, the kernel's text and read-only data sections are never loaded into RAM, thereby reducing the kernel's memory usage. Also, since a significant portion of the kernel is never loaded into RAM, a larger kernel configuration can be used without bloating memory usage. I haven't done any performance analysis yet, but it's probably safe to say that executing from storage will negatively affect performance.

Quoted 1 line

- on what hardware/bootloaders is or will be XIP supported?

With regards to supported hardware, these patches aren't targeting any specific platform. As mentioned in the coverletter, there are current limits on the supported configurations (32-bit only, no SMP, no PAE), but these are not technical limits ... I just need to implement support for them.

With regards to supported bootloaders, I've been testing with a small bootloader that I wrote specifically for XIP. Which other bootloaders I add support to will depend on the feedback/requests that I get.

Quoted 3 lines

Also, there should probably be some fail-safe mechanism included: such as to check whether caching attributes (MTRRs, PAT) are properly set for the XIP area (at minimum to not be uncacheable).

Good idea. I'll add that into the next revision.

Thanks.

Signature · 2 lines
Jim Kukunas
Intel Open Source Technology Center