i386: Relocatable BzImage (V3)
Subsystems (4)
cover i386: Relocatable BzImage (V3)
Hi,
Here is the third attempt on implementing relocatable bzImage for i386. Eric has done all the ground work and I am just giving it final finish. Generated patches against (2.6.19-rc2-git7).
V2 ran into issues with lilo on Andrew's machine. I can't reproduce the problem. For the sake of simpilicity, I have dropped the bit which added and ELF header to bzImage. Instead, I have extended the bzImage protocol to enable boot loaders to load protected mode kernel at a non 1MB address. Hopefully this should not break any existing behaviour.
This functionality is especially useful for kdump where a single kernel can be used both as production kernel and dump capture kernel and distors don't have to maintain an additional kernel just for capturing the dump.
I have tested it with grub (.97) and lilo (lilo 22.7.3).
Note: One has to upgrade the binutils if you want to use
CONFIG_RELOCATABLE=y. Previous ld version will make section relative symbols absolute, if section containing the symbols has zero size. These absolute symbols are not relocated. This issue has been fixed in latest binutils. I am using the one built on 09th octoer 2006 and it works fine for me.
Chages since version V2
----------------------
- Dropped adding and ELF header to bzImage.
- Extended bzImage protocol for relocatable bzImage (version 2.05)
- Added support to display warning message at compilation time if relocations relative to absolute symbols are present.
- Fixed a bug where some intermediate files were not being cleaned up by "make clean"
- Avoid building vmlinux.bin.all if CONFIG_RELOCATABLE is not set.
Looking forward for your suggestions and any test results.
Thanks
Vivek
PATCH 1/11 i386: Distinguish absolute symbols
Ld knows about 2 kinds of symbols, absolute and section relative. Section relative symbols symbols change value when a section is moved and absolute symbols do not.
Currently in the linker script we have several labels marking the beginning and ending of sections that are outside of sections, making them absolute symbols. Having a mixture of absolute and section relative symbols refereing to the same data is currently harmless but it is confusing.
This must be done carefully as newer revs of ld do not place symbols that appear in sections without data and instead ld makes those symbols global :(
My ultimate goal is to build a relocatable kernel. The safest and least intrusive technique is to generate relocation entries so the kernel can be relocated at load time. The only penalty would be an increase in the size of the kernel binary. The problem is that if absolute and relocatable symbols are not properly specified absolute symbols will be relocated or section relative symbols won't be, which is fatal.
The practical motivation is that when generating kernels that will run from a reserved area for analyzing what caused a kernel panic, it is simpler if you don't need to hard code the physical memory location they will run at, especially for the distributions.
---
arch/i386/kernel/vmlinux.lds.S | 109 ++++++++++++++++++++------------------ include/asm-generic/vmlinux.lds.h | 10 +-- 2 files changed, 63 insertions(+), 56 deletions(-)
arch/i386/kernel/vmlinux.lds.S renamed from arch/i386/kernel/vmlinux.lds.S~i386-vmlinux.lds.S-Distinguish-absolute-symbols +58 -51
@@ -24,31 +24,32 @@ SECTIONS . = __KERNEL_START; phys_startup_32 = startup_32 - LOAD_OFFSET; /* read-only */ - _text = .; /* Text and read-only data */ .text : AT(ADDR(.text) - LOAD_OFFSET) { + _text = .; /* Text and read-only data */ *(.text) SCHED_TEXT LOCK_TEXT KPROBES_TEXT *(.fixup) *(.gnu.warning) - } :text = 0x9090 - - _etext = .; /* End of text section */ + _etext = .; /* End of text section */ + } :text = 0x9090 . = ALIGN(16); /* Exception table */ - __start___ex_table = .; - __ex_table : AT(ADDR(__ex_table) - LOAD_OFFSET) { *(__ex_table) } - __stop___ex_table = .; + __ex_table : AT(ADDR(__ex_table) - LOAD_OFFSET) { + __start___ex_table = .; + *(__ex_table) + __stop___ex_table = .; + } RODATA . = ALIGN(4); - __tracedata_start = .; .tracedata : AT(ADDR(.tracedata) - LOAD_OFFSET) { + __tracedata_start = .; *(.tracedata) + __tracedata_end = .; } - __tracedata_end = .; /* writeable */ .data : AT(ADDR(.data) - LOAD_OFFSET) { /* Data */
@@ -57,10 +58,12 @@ SECTIONS } :data . = ALIGN(4096); - __nosave_begin = .; - .data_nosave : AT(ADDR(.data_nosave) - LOAD_OFFSET) { *(.data.nosave) } - . = ALIGN(4096); - __nosave_end = .; + .data_nosave : AT(ADDR(.data_nosave) - LOAD_OFFSET) { + __nosave_begin = .; + *(.data.nosave) + . = ALIGN(4096); + __nosave_end = .; + } . = ALIGN(4096); .data.page_aligned : AT(ADDR(.data.page_aligned) - LOAD_OFFSET) {
@@ -74,8 +77,10 @@ SECTIONS /* rarely changed data like cpu maps */ . = ALIGN(32); - .data.read_mostly : AT(ADDR(.data.read_mostly) - LOAD_OFFSET) { *(.data.read_mostly) } - _edata = .; /* End of data section */ + .data.read_mostly : AT(ADDR(.data.read_mostly) - LOAD_OFFSET) { + *(.data.read_mostly) + _edata = .; /* End of data section */ + } #ifdef CONFIG_STACK_UNWIND . = ALIGN(4);
@@ -93,39 +98,41 @@ SECTIONS /* might get freed after init */ . = ALIGN(4096); - __smp_alt_begin = .; - __smp_alt_instructions = .; .smp_altinstructions : AT(ADDR(.smp_altinstructions) - LOAD_OFFSET) { + __smp_alt_begin = .; + __smp_alt_instructions = .; *(.smp_altinstructions) + __smp_alt_instructions_end = .; } - __smp_alt_instructions_end = .; . = ALIGN(4); - __smp_locks = .; .smp_locks : AT(ADDR(.smp_locks) - LOAD_OFFSET) { + __smp_locks = .; *(.smp_locks) + __smp_locks_end = .; } - __smp_locks_end = .; .smp_altinstr_replacement : AT(ADDR(.smp_altinstr_replacement) - LOAD_OFFSET) { *(.smp_altinstr_replacement) + . = ALIGN(4096); + __smp_alt_end = .; } - . = ALIGN(4096); - __smp_alt_end = .; /* will be freed after init */ . = ALIGN(4096); /* Init code and data */ - __init_begin = .; .init.text : AT(ADDR(.init.text) - LOAD_OFFSET) { + __init_begin = .; _sinittext = .; *(.init.text) _einittext = .; } .init.data : AT(ADDR(.init.data) - LOAD_OFFSET) { *(.init.data) } . = ALIGN(16); - __setup_start = .; - .init.setup : AT(ADDR(.init.setup) - LOAD_OFFSET) { *(.init.setup) } - __setup_end = .; - __initcall_start = .; + .init.setup : AT(ADDR(.init.setup) - LOAD_OFFSET) { + __setup_start = .; + *(.init.setup) + __setup_end = .; + } .initcall.init : AT(ADDR(.initcall.init) - LOAD_OFFSET) { + __initcall_start = .; *(.initcall1.init) *(.initcall2.init) *(.initcall3.init)
@@ -133,20 +140,20 @@ SECTIONS *(.initcall5.init) *(.initcall6.init) *(.initcall7.init) + __initcall_end = .; } - __initcall_end = .; - __con_initcall_start = .; .con_initcall.init : AT(ADDR(.con_initcall.init) - LOAD_OFFSET) { + __con_initcall_start = .; *(.con_initcall.init) + __con_initcall_end = .; } - __con_initcall_end = .; SECURITY_INIT . = ALIGN(4); - __alt_instructions = .; .altinstructions : AT(ADDR(.altinstructions) - LOAD_OFFSET) { + __alt_instructions = .; *(.altinstructions) + __alt_instructions_end = .; } - __alt_instructions_end = .; .altinstr_replacement : AT(ADDR(.altinstr_replacement) - LOAD_OFFSET) { *(.altinstr_replacement) }
@@ -155,32 +162,32 @@ SECTIONS .exit.text : AT(ADDR(.exit.text) - LOAD_OFFSET) { *(.exit.text) } .exit.data : AT(ADDR(.exit.data) - LOAD_OFFSET) { *(.exit.data) } . = ALIGN(4096); - __initramfs_start = .; - .init.ramfs : AT(ADDR(.init.ramfs) - LOAD_OFFSET) { *(.init.ramfs) } - __initramfs_end = .; + .init.ramfs : AT(ADDR(.init.ramfs) - LOAD_OFFSET) { + __initramfs_start = .; + *(.init.ramfs) + __initramfs_end = .; + } . = ALIGN(L1_CACHE_BYTES); - __per_cpu_start = .; - .data.percpu : AT(ADDR(.data.percpu) - LOAD_OFFSET) { *(.data.percpu) } - __per_cpu_end = .; + .data.percpu : AT(ADDR(.data.percpu) - LOAD_OFFSET) { + __per_cpu_start = .; + *(.data.percpu) + __per_cpu_end = .; + } . = ALIGN(4096); - __init_end = .; /* freed after init ends here */ - __bss_start = .; /* BSS */ - .bss.page_aligned : AT(ADDR(.bss.page_aligned) - LOAD_OFFSET) { - *(.bss.page_aligned) - } .bss : AT(ADDR(.bss) - LOAD_OFFSET) { + __init_end = .; + __bss_start = .; /* BSS */ + *(.bss.page_aligned) *(.bss) + . = ALIGN(4); + __bss_stop = .; + _end = . ; + /* This is where the kernel creates the early boot page tables */ + . = ALIGN(4096); + pg0 = . ; } - . = ALIGN(4); - __bss_stop = .; - - _end = . ; - - /* This is where the kernel creates the early boot page tables */ - . = ALIGN(4096); - pg0 = .; /* Sections to be discarded */ /DISCARD/ : {
include/asm-generic/vmlinux.lds.h renamed from include/asm-generic/vmlinux.lds.h~i386-vmlinux.lds.S-Distinguish-absolute-symbols +5 -5
@@ -11,8 +11,8 @@#define RODATA \ . = ALIGN(4096); \ - __start_rodata = .; \ .rodata : AT(ADDR(.rodata) - LOAD_OFFSET) { \ + VMLINUX_SYMBOL(__start_rodata) = .; \ *(.rodata) *(.rodata.*) \ *(__vermagic)/* Kernel version magic */ \ } \
@@ -119,17 +119,17 @@ *(__ksymtab_strings) \ } \ \ +/* Unwind data binary search table */ \ +EH_FRAME_HDR \ + \/* Built-in module parameters. */ \ __param : AT(ADDR(__param) - LOAD_OFFSET) { \ VMLINUX_SYMBOL(__start___param) = .; \ *(__param ) \ VMLINUX_SYMBOL(__stop___param) = .; \ + VMLINUX_SYMBOL(__end_rodata) = .; \ } \ \ -/* Unwind data binary search table */ \ -EH_FRAME_HDR \ - \ - __end_rodata = .; \ . = ALIGN(4096);#define SECURITY_INIT \
_
PATCH 2/11 i386: Remove unnecessary ALIGN() in vmlinux.lds.S
In reply to Vivek Goyalo There seems to be one extra ALIGN(4096) before symbol __smp_alt_end. The only usage of __smp_alt_end is to mark the end of smp alternative sections so that this memory can be freed. As a physical page is freed one has to just make sure that there is no other data on the same page where __smp_alt_end is pointing. There is already a ALIGN(4096) after this section which should take care of the above issue. Hence it looks like the ALIGN(4096) before __smp_alt_end is redundant and not required.
---
arch/i386/kernel/vmlinux.lds.S | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
arch/i386/kernel/vmlinux.lds.S renamed from arch/i386/kernel/vmlinux.lds.S~i386-remove-unnecessary-align-option +6 -2
@@ -112,11 +112,15 @@ SECTIONS } .smp_altinstr_replacement : AT(ADDR(.smp_altinstr_replacement) - LOAD_OFFSET) { *(.smp_altinstr_replacement) - . = ALIGN(4096); __smp_alt_end = .; } - /* will be freed after init */ + /* will be freed after init + * Following ALIGN() is required to make sure no other data falls on the + * same page where __smp_alt_end is pointing as that page might be freed + * after boot. Always make sure that ALIGN() directive is present after + * the section which contains __smp_alt_end. + */ . = ALIGN(4096); /* Init code and data */ .init.text : AT(ADDR(.init.text) - LOAD_OFFSET) { __init_begin = .;
_
PATCH 3/11 i386: Force data segment to be 4K aligned
In reply to Vivek Goyalo Currently there is no specific alignment restriction in linker script and in some cases it can be placed non 4K aligned addresses. This fails kexec which checks that segment to be loaded is page aligned.
o I guess, it does not harm data segment to be 4K aligned.
---
arch/i386/kernel/vmlinux.lds.S | 1 + 1 file changed, 1 insertion(+)
arch/i386/kernel/vmlinux.lds.S renamed from arch/i386/kernel/vmlinux.lds.S~i386-force-data-section-to-4K-aligned +1 -0
@@ -52,6 +52,7 @@ SECTIONS } /* writeable */ + . = ALIGN(4096); .data : AT(ADDR(.data) - LOAD_OFFSET) { /* Data */ *(.data) CONSTRUCTORS
_
PATCH 4/11 i386: define __pa_symbol()
In reply to Vivek GoyalOn x86_64 we have to be careful with calculating the physical address of kernel symbols. Both because of compiler odditities and because the symbols live in a different range of the virtual address space.
Having a defintition of __pa_symbol that works on both x86_64 and i386 simplifies writing code that works for both x86_64 and i386 that has these kinds of dependencies.
So this patch adds the trivial i386 __pa_symbol definition.
Added assembly magic similar to RELOC_HIDE as suggested by Andi Kleen. Just picked it up from x86_64.
---
include/asm-i386/page.h | 3 +++ 1 file changed, 3 insertions(+)
include/asm-i386/page.h renamed from include/asm-i386/page.h~i386-define-__pa_symbol +3 -0
@@ -124,6 +124,9 @@ extern int page_is_ram(unsigned long pag#define VMALLOC_RESERVE ((unsigned long)__VMALLOC_RESERVE)#define MAXMEM (-__PAGE_OFFSET-__VMALLOC_RESERVE)#define __pa (x) ((unsigned long)(x)-PAGE_OFFSET) +/* __pa_symbol should be used for C visible symbols. +This seems to be the official gcc blessed way to do such arithmetic. */ +#define __pa_symbol (x) __pa(RELOC_HIDE((unsigned long)(x),0))#define __va (x) ((void *)((unsigned long)(x)+PAGE_OFFSET))#define pfn_to_kaddr (pfn) __va((pfn) << PAGE_SHIFT)#ifdef CONFIG_FLATMEM
_
PATCH 5/11 i386: Reserve kernel memory starting from _text
In reply to Vivek GoyalCurrently when we are reserving the memory the kernel text resides in we start at __PHYSICAL_START which happens to be correct but not very obvious. In addition when we start relocating the kernel __PHYSICAL_START is the wrong value, as it is an absolute symbol that does not get relocated.
By starting the reservation at __pa_symbol(_text) the code is clearer and will be correct when relocated.
---
arch/i386/kernel/setup.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/i386/kernel/setup.c renamed from arch/i386/kernel/setup.c~i386-setup.c-Reserve-kernel-memory-starting-from-_text +2 -2
@@ -1118,8 +1118,8 @@ void __init setup_bootmem_allocator(void* the (very unlikely) case of us accidentally initializing the * bootmem allocator with an invalid RAM area. */ -reserve_bootmem (__PHYSICAL_START, (PFN_PHYS (min_low_pfn) + - bootmap_size +PAGE_SIZE -1 ) - (__PHYSICAL_START)); +reserve_bootmem (__pa_symbol (_text), (PFN_PHYS (min_low_pfn) + + bootmap_size +PAGE_SIZE -1 ) -__pa_symbol (_text));/* * reserve physical page 0 - it's a special BIOS page on many boxes,
_
PATCH 6/11 i386: CONFIG_PHYSICAL_START cleanup
In reply to Vivek GoyalDefining __PHYSICAL_START and __KERNEL_START in asm-i386/page.h works but it triggers a full kernel rebuild for the silliest of reasons. This modifies the users to directly use CONFIG_PHYSICAL_START and linux/config.h which prevents the full rebuild problem, which makes the code much more maintainer and hopefully user friendly.
---
arch/i386/boot/compressed/head.S | 7 +++---- arch/i386/boot/compressed/misc.c | 7 +++---- arch/i386/kernel/vmlinux.lds.S | 2 +- include/asm-i386/page.h | 3 --- 4 files changed, 7 insertions(+), 12 deletions(-)
arch/i386/boot/compressed/head.S renamed from arch/i386/boot/compressed/head.S~i386-CONFIG_PHYSICAL_START-cleanup +3 -4
@@ -25,7 +25,6 @@ #include <linux/linkage.h> #include <asm/segment.h> -#include <asm/page.h> .globl startup_32
@@ -75,7 +74,7 @@ startup_32: popl %esi # discard address popl %esi # real mode pointer xorl %ebx,%ebx - ljmp $(__BOOT_CS), $__PHYSICAL_START + ljmp $(__BOOT_CS), $CONFIG_PHYSICAL_START /* * We come here, if we were loaded high.
@@ -100,7 +99,7 @@ startup_32: popl %ecx # lcount popl %edx # high_buffer_start popl %eax # hcount - movl $__PHYSICAL_START,%edi + movl $CONFIG_PHYSICAL_START,%edi cli # make sure we don't get interrupted ljmp $(__BOOT_CS), $0x1000 # and jump to the move routine
@@ -125,5 +124,5 @@ move_routine_start: movsl movl %ebx,%esi # Restore setup pointer xorl %ebx,%ebx - ljmp $(__BOOT_CS), $__PHYSICAL_START + ljmp $(__BOOT_CS), $CONFIG_PHYSICAL_START move_routine_end:
arch/i386/boot/compressed/misc.c renamed from arch/i386/boot/compressed/misc.c~i386-CONFIG_PHYSICAL_START-cleanup +3 -4
@@ -13,7 +13,6 @@#include <linux/vmalloc.h> #include <linux/screen_info.h> #include <asm/io.h> -#include <asm/page.h> /* * gzip declarations
@@ -303,7 +302,7 @@ static void setup_normal_output_buffer(v#else if ((RM_ALT_MEM_K >RM_EXT_MEM_K ?RM_ALT_MEM_K :RM_EXT_MEM_K ) <1024 )error ("Less than 2MB of memory" );#endif - output_data = (unsigned char *)__PHYSICAL_START;/* Normally Points to 1M */ + output_data = (unsigned char *)CONFIG_PHYSICAL_START ;/* Normally Points to 1M */ free_mem_end_ptr = (long )real_mode; }
@@ -326,8 +325,8 @@ static void setup_output_buffer_if_we_ru low_buffer_size = low_buffer_end -LOW_BUFFER_START ; high_loaded =1 ; free_mem_end_ptr = (long )high_buffer_start; -if ( (__PHYSICAL_START + low_buffer_size) > ((ulg )high_buffer_start)) { - high_buffer_start = (uch *)(__PHYSICAL_START + low_buffer_size); +if ( (CONFIG_PHYSICAL_START + low_buffer_size) > ((ulg )high_buffer_start)) { + high_buffer_start = (uch *)(CONFIG_PHYSICAL_START + low_buffer_size); mv->hcount =0 ;/* say: we need not to move high_buffer */ }else mv->hcount =-1 ;
arch/i386/kernel/vmlinux.lds.S renamed from arch/i386/kernel/vmlinux.lds.S~i386-CONFIG_PHYSICAL_START-cleanup +1 -1
@@ -21,7 +21,7 @@ PHDRS { } SECTIONS { - . = __KERNEL_START; + . = LOAD_OFFSET + CONFIG_PHYSICAL_START; phys_startup_32 = startup_32 - LOAD_OFFSET; /* read-only */ .text : AT(ADDR(.text) - LOAD_OFFSET) {
include/asm-i386/page.h renamed from include/asm-i386/page.h~i386-CONFIG_PHYSICAL_START-cleanup +0 -3
@@ -112,12 +112,9 @@ extern int page_is_ram(unsigned long pag#ifdef __ASSEMBLY__#define __PAGE_OFFSET CONFIG_PAGE_OFFSET -#define __PHYSICAL_START CONFIG_PHYSICAL_START#else #define __PAGE_OFFSET ((unsigned long)CONFIG_PAGE_OFFSET) -#define __PHYSICAL_START ((unsigned long)CONFIG_PHYSICAL_START)#endif -#define __KERNEL_START (__PAGE_OFFSET + __PHYSICAL_START)#define PAGE_OFFSET ((unsigned long)__PAGE_OFFSET)
_
PATCH 7/11 i386: Kallsyms generate relocatable symbols
In reply to Vivek GoyalPrint the addresses of non-absolute symbols relative to _text so that ld will generate relocations. Allowing a relocatable kernel to relocate them. We can't actually use the symbol names because kallsyms includes static symbols that are not exported from their object files.
Add the _text symbol definitions to the architectures which don't define it otherwise linker will fail.
---
arch/h8300/kernel/vmlinux.lds.S | 1 + arch/m68knommu/kernel/vmlinux.lds.S | 1 + arch/powerpc/kernel/vmlinux.lds.S | 1 + arch/ppc/kernel/vmlinux.lds.S | 1 + arch/sparc/kernel/vmlinux.lds.S | 1 + arch/sparc64/kernel/vmlinux.lds.S | 1 + arch/v850/kernel/vmlinux.lds.S | 1 + scripts/kallsyms.c | 20 +++++++++++++++++--- 8 files changed, 24 insertions(+), 3 deletions(-)
arch/h8300/kernel/vmlinux.lds.S renamed from arch/h8300/kernel/vmlinux.lds.S~kallsyms.c-Generate-relocatable-symbols +1 -0
@@ -70,6 +70,7 @@ SECTIONS #endif .text : { + _text = .; #if defined(CONFIG_ROMKERNEL) *(.int_redirect) #endif
arch/m68knommu/kernel/vmlinux.lds.S renamed from arch/m68knommu/kernel/vmlinux.lds.S~kallsyms.c-Generate-relocatable-symbols +1 -0
@@ -60,6 +60,7 @@ SECTIONS { #endif .text : { + _text = .; _stext = . ; *(.text) SCHED_TEXT
arch/powerpc/kernel/vmlinux.lds.S renamed from arch/powerpc/kernel/vmlinux.lds.S~kallsyms.c-Generate-relocatable-symbols +1 -0
@@ -33,6 +33,7 @@ SECTIONS /* Text and gots */ .text : { + _text = .; *(.text .text.*) SCHED_TEXT LOCK_TEXT
arch/ppc/kernel/vmlinux.lds.S renamed from arch/ppc/kernel/vmlinux.lds.S~kallsyms.c-Generate-relocatable-symbols +1 -0
@@ -31,6 +31,7 @@ SECTIONS .plt : { *(.plt) } .text : { + _text = .; *(.text) SCHED_TEXT LOCK_TEXT
arch/sparc64/kernel/vmlinux.lds.S renamed from arch/sparc64/kernel/vmlinux.lds.S~kallsyms.c-Generate-relocatable-symbols +1 -0
@@ -13,6 +13,7 @@ SECTIONS . = 0x4000; .text 0x0000000000404000 : { + _text = .; *(.text) SCHED_TEXT LOCK_TEXT
arch/sparc/kernel/vmlinux.lds.S renamed from arch/sparc/kernel/vmlinux.lds.S~kallsyms.c-Generate-relocatable-symbols +1 -0
@@ -11,6 +11,7 @@ SECTIONS . = 0x10000 + SIZEOF_HEADERS; .text 0xf0004000 : { + _text = .; *(.text) SCHED_TEXT LOCK_TEXT
arch/v850/kernel/vmlinux.lds.S renamed from arch/v850/kernel/vmlinux.lds.S~kallsyms.c-Generate-relocatable-symbols +1 -0
@@ -90,6 +90,7 @@ /* Kernel text segment, and some constant data areas. */ #define TEXT_CONTENTS \ + _text = .; \ __stext = . ; \ *(.text) \ SCHED_TEXT \
scripts/kallsyms.c renamed from scripts/kallsyms.c~kallsyms.c-Generate-relocatable-symbols +17 -3
@@ -43,7 +43,7 @@ struct sym_entry {static struct sym_entry *table;static unsigned int table_size, table_cnt; -static unsigned long long _stext, _etext, _sinittext, _einittext, _sextratext, _eextratext; +static unsigned long long _text, _stext, _etext, _sinittext, _einittext, _sextratext, _eextratext;static int all_symbols =0 ;static char symbol_prefix_char ='\0' ;
@@ -91,7 +91,9 @@ static int read_symbol(FILE *in, struct sym++;/* Ignore most absolute/undefined (?) symbols. */ -if (strcmp (sym,"_stext" ) ==0 ) +if (strcmp (sym,"_text" ) ==0 ) + _text = s->addr; +else if (strcmp (sym,"_stext" ) ==0 ) _stext = s->addr;else if (strcmp (sym,"_etext" ) ==0 ) _etext = s->addr;
@@ -265,9 +267,21 @@ static void write_src(void)printf (".data\n" ); +/* Provide proper symbols relocatability by their '_text' +* relativeness. The symbol names cannot be used to construct +* normal symbol references as the list of symbols contains +* symbols that are declared static and are private to their +* .o files. This prevents .tmp_kallsyms.o or any other +* object from referencing them. +*/ output_label ("kallsyms_addresses" );for (i =0 ; i < table_cnt; i++) { -printf ("\tPTR\t%#llx\n" , table[i].addr); +if (toupper (table[i].sym[0 ]) !='A' ) { +printf ("\tPTR\t_text + %#llx\n" , + table[i].addr - _text); + }else { +printf ("\tPTR\t%#llx\n" , table[i].addr); + } }printf ("\n" );
_
Re: [PATCH 7/11] i386: Kallsyms generate relocatable symbols
Quoted 2 lines · On Mon, 23 Oct 2006 15:37:48 -0400 Vivek Goyal <[email protected]> wrote:
Add the
_textsymbol definitions to the architectures which don't define it otherwise linker will fail.
I get lots of this when building latest -mm for avr32:
.tmp_kallsyms2.S:643: Warning: right operand is a bignum; integer 0 assumed
Reverting this patch makes the warnings go away. I think it's because on avr32, .init is the first section, not .text, so many of the offsets become negative and kallsyms doesn't seem to handle this very well. Here's a few lines from .tmp_kallsyms2.S:
kallsyms_addresses:
PTR _text + 0xffffffffffff4000
PTR _text + 0xffffffffffff4000Any idea how to fix this? Could we introduce a new symbol that always marks the start of the image perhaps?
Haavard
Re: [PATCH 7/11] i386: Kallsyms generate relocatable symbols
Quoted 16 lines · Haavard Skinnemoen
Quoted 2 lines · On Mon, 23 Oct 2006 15:37:48 -0400 Vivek Goyal <[email protected]> wrote:
Add the
_textsymbol definitions to the architectures which don't define it otherwise linker will fail.I get lots of this when building latest -mm for avr32:
.tmp_kallsyms2.S:643: Warning: right operand is a bignum; integer 0 assumed
Reverting this patch makes the warnings go away. I think it's because on avr32, .init is the first section, not .text, so many of the offsets become negative and kallsyms doesn't seem to handle this very well. Here's a few lines from
.tmp_kallsyms2.S:
Quoted 7 lines
kallsyms_addresses:PTR _text + 0xffffffffffff4000 PTR _text + 0xffffffffffff4000Any idea how to fix this? Could we introduce a new symbol that always marks the start of the image perhaps?
Hi Haavard,
Does the attached patch solve the issue for you?
Thanks
Vivek
o On some platforms like avr32, section init comes before .text and not necessarily a symbol's relative position w.r.t _text is positive. In such cases assembler detects the overflow and emits warning. This patch fixes it.
---
scripts/kallsyms.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
scripts/kallsyms.c renamed from scripts/kallsyms.c~i386-reloc-kallsyms-_text-symbol-relative-address-fix +6 -2
@@ -277,8 +277,12 @@ static void write_src(void)output_label ("kallsyms_addresses" );for (i =0 ; i < table_cnt; i++) {if (toupper (table[i].sym[0 ]) !='A' ) { -printf ("\tPTR\t_text + %#llx\n" , - table[i].addr - _text); +if (_text <= table[i].addr) +printf ("\tPTR\t_text + %#llx\n" , + table[i].addr - _text); +else +printf ("\tPTR\t_text - %#llx\n" , + _text - table[i].addr); }else {printf ("\tPTR\t%#llx\n" , table[i].addr); }
_
Re: [PATCH 7/11] i386: Kallsyms generate relocatable symbols
Quoted 6 lines · On Mon, 6 Nov 2006 11:03:53 -0500 Vivek Goyal <[email protected]> wrote:
Quoted 4 lines · Haavard Skinnemoen
kallsyms_addresses:PTR _text + 0xffffffffffff4000 PTR _text + 0xffffffffffff4000
Quoted 7 lines
Quoted 3 lines
Any idea how to fix this? Could we introduce a new symbol that always marks the start of the image perhaps?
Hi Haavard,
Does the attached patch solve the issue for you?
Yes it does, thanks. I tested it by forcing a crash in a __init function, and the resulting Oops looks correct.
Haavard
Re: [PATCH 7/11] i386: Kallsyms generate relocatable symbols
In reply to Haavard SkinnemoenQuoted 16 lines · Haavard Skinnemoen
Quoted 2 lines · On Mon, 23 Oct 2006 15:37:48 -0400 Vivek Goyal <[email protected]> wrote:
Add the
_textsymbol definitions to the architectures which don't define it otherwise linker will fail.I get lots of this when building latest -mm for avr32:
.tmp_kallsyms2.S:643: Warning: right operand is a bignum; integer 0 assumed
Reverting this patch makes the warnings go away. I think it's because on avr32, .init is the first section, not .text, so many of the offsets become negative and kallsyms doesn't seem to handle this very well. Here's a few lines from
.tmp_kallsyms2.S:
Quoted 7 lines
kallsyms_addresses:PTR _text + 0xffffffffffff4000 PTR _text + 0xffffffffffff4000Any idea how to fix this? Could we introduce a new symbol that always marks the start of the image perhaps?
Maybe we should generate it as a signed value (-0x8000) instead of an unsigned value.
-hpa
PATCH 8/11 i386: Relocatable kernel support
In reply to Vivek GoyalThis patch modifies the x86 kernel so that if CONFIG_RELOCATABLE is selected it will be able to be loaded at any 4K aligned address below 1G. The technique used is to compile the decompressor with -fPIC and modify it so the decompressor is fully relocatable. For the main kernel relocations are generated. Resulting in a kernel that is relocatable with no runtime overhead and no need to modify the source code.
A reserved 32bit word in the parameters has been assigned to serve as a stack so we figure out where are running.
---
arch/i386/Kconfig | 12 arch/i386/Makefile | 4 arch/i386/boot/compressed/Makefile | 28 + arch/i386/boot/compressed/head.S | 190 +++++++---- arch/i386/boot/compressed/misc.c | 267 ++++++++-------- arch/i386/boot/compressed/relocs.c | 563 ++++++++++++++++++++++++++++++++++ arch/i386/boot/compressed/vmlinux.lds | 43 ++ arch/i386/boot/compressed/vmlinux.scr | 3 arch/i386/boot/setup.S | 29 + include/linux/screen_info.h | 3 10 files changed, 926 insertions(+), 216 deletions(-)
arch/i386/boot/compressed/head.S renamed from arch/i386/boot/compressed/head.S~i386-Relocatable-kernel-support +120 -70
@@ -25,9 +25,11 @@ #include <linux/linkage.h> #include <asm/segment.h> +#include <asm/page.h> +.section ".text.head" .globl startup_32 - + startup_32: cld cli
@@ -36,93 +38,141 @@ startup_32: movl %eax,%es movl %eax,%fs movl %eax,%gs + movl %eax,%ss - lss stack_start,%esp - xorl %eax,%eax -1: incl %eax # check that A20 really IS enabled - movl %eax,0x000000 # loop forever if it isn't - cmpl %eax,0x100000 - je 1b +/* Calculate the delta between where we were compiled to run + * at and where we were actually loaded at. This can only be done + * with a short local call on x86. Nothing else will tell us what + * address we are running at. The reserved chunk of the real-mode + * data at 0x34-0x3f are used as the stack for this calculation. + * Only 4 bytes are needed. + */ + leal 0x40(%esi), %esp + call 1f +1: popl %ebp + subl $1b, %ebp + +/* Compute the delta between where we were compiled to run at + * and where the code will actually run at. + */ + /* Start with the delta to where the kernel will run at. If we are + * a relocatable kernel this is the delta to our load address otherwise + * this is the delta to CONFIG_PHYSICAL start. + */ +#ifdef CONFIG_RELOCATABLE + movl %ebp, %ebx +#else + movl $(CONFIG_PHYSICAL_START - startup_32), %ebx +#endif + + /* Replace the compressed data size with the uncompressed size */ + subl input_len(%ebp), %ebx + movl output_len(%ebp), %eax + addl %eax, %ebx + /* Add 8 bytes for every 32K input block */ + shrl $12, %eax + addl %eax, %ebx + /* Add 32K + 18 bytes of extra slack */ + addl $(32768 + 18), %ebx + /* Align on a 4K boundary */ + addl $4095, %ebx + andl $~4095, %ebx + +/* Copy the compressed kernel to the end of our buffer + * where decompression in place becomes safe. + */ + pushl %esi + leal _end(%ebp), %esi + leal _end(%ebx), %edi + movl $(_end - startup_32), %ecx + std + rep + movsb + cld + popl %esi + +/* Compute the kernel start address. + */ +#ifdef CONFIG_RELOCATABLE + leal startup_32(%ebp), %ebp +#else + movl $CONFIG_PHYSICAL_START, %ebp +#endif /* - * Initialize eflags. Some BIOS's leave bits like NT set. This would - * confuse the debugger if this code is traced. - * XXX - best to initialize before switching to protected mode. + * Jump to the relocated address. */ - pushl $0 - popfl + leal relocated(%ebx), %eax + jmp *%eax +.section ".text" +relocated: + /* * Clear BSS */ xorl %eax,%eax - movl $_edata,%edi - movl $_end,%ecx + leal _edata(%ebx),%edi + leal _end(%ebx), %ecx subl %edi,%ecx cld rep stosb + +/* + * Setup the stack for the decompressor + */ + leal stack_end(%ebx), %esp + /* * Do the decompression, and jump to the new kernel.. */ - subl $16,%esp # place for structure on the stack - movl %esp,%eax + movl output_len(%ebx), %eax + pushl %eax + pushl %ebp # output address + movl input_len(%ebx), %eax + pushl %eax # input_len + leal input_data(%ebx), %eax + pushl %eax # input_data + leal _end(%ebx), %eax + pushl %eax # end of the image as third argument pushl %esi # real mode pointer as second arg - pushl %eax # address of structure as first arg call decompress_kernel - orl %eax,%eax - jnz 3f - popl %esi # discard address - popl %esi # real mode pointer - xorl %ebx,%ebx - ljmp $(__BOOT_CS), $CONFIG_PHYSICAL_START + addl $20, %esp + popl %ecx + +#if CONFIG_RELOCATABLE +/* Find the address of the relocations. + */ + movl %ebp, %edi + addl %ecx, %edi + +/* Calculate the delta between where vmlinux was compiled to run + * and where it was actually loaded. + */ + movl %ebp, %ebx + subl $CONFIG_PHYSICAL_START, %ebx /* - * We come here, if we were loaded high. - * We need to move the move-in-place routine down to 0x1000 - * and then start it with the buffer addresses in registers, - * which we got from the stack. - */ -3: - movl $move_routine_start,%esi - movl $0x1000,%edi - movl $move_routine_end,%ecx - subl %esi,%ecx - addl $3,%ecx - shrl $2,%ecx - cld - rep - movsl + * Process relocations. + */ - popl %esi # discard the address - popl %ebx # real mode pointer - popl %esi # low_buffer_start - popl %ecx # lcount - popl %edx # high_buffer_start - popl %eax # hcount - movl $CONFIG_PHYSICAL_START,%edi - cli # make sure we don't get interrupted - ljmp $(__BOOT_CS), $0x1000 # and jump to the move routine - -/* - * Routine (template) for moving the decompressed kernel in place, - * if we were high loaded. This _must_ PIC-code ! - */ -move_routine_start: - movl %ecx,%ebp - shrl $2,%ecx - rep - movsl - movl %ebp,%ecx - andl $3,%ecx - rep - movsb - movl %edx,%esi - movl %eax,%ecx # NOTE: rep movsb won't move if %ecx == 0 - addl $3,%ecx - shrl $2,%ecx - rep - movsl - movl %ebx,%esi # Restore setup pointer +1: subl $4, %edi + movl 0(%edi), %ecx + testl %ecx, %ecx + jz 2f + addl %ebx, -__PAGE_OFFSET(%ebx, %ecx) + jmp 1b +2: +#endif + +/* + * Jump to the decompressed kernel. + */ xorl %ebx,%ebx - ljmp $(__BOOT_CS), $CONFIG_PHYSICAL_START -move_routine_end: + jmp *%ebp + +.bss +.balign 4 +stack: + .fill 4096, 1, 0 +stack_end:
arch/i386/boot/compressed/Makefile renamed from arch/i386/boot/compressed/Makefile~i386-Relocatable-kernel-support +24 -4
@@ -4,22 +4,42 @@ # create a compressed vmlinux image from the original vmlinux # -targets := vmlinux vmlinux.bin vmlinux.bin.gz head.o misc.o piggy.o +targets := vmlinux vmlinux.bin vmlinux.bin.gz head.o misc.o piggy.o \ + vmlinux.bin.all vmlinux.relocs EXTRA_AFLAGS := -traditional -LDFLAGS_vmlinux := -Ttext $(IMAGE_OFFSET) -e startup_32 +LDFLAGS_vmlinux := -T +CFLAGS_misc.o += -fPIC +hostprogs-y := relocs -$(obj)/vmlinux: $(obj)/head.o $(obj)/misc.o $(obj)/piggy.o FORCE +$(obj)/vmlinux: $(src)/vmlinux.lds $(obj)/head.o $(obj)/misc.o $(obj)/piggy.o FORCE $(call if_changed,ld) @: $(obj)/vmlinux.bin: vmlinux FORCE $(call if_changed,objcopy) +quiet_cmd_relocs = RELOCS $@ + cmd_relocs = $(obj)/relocs $< > $@ +$(obj)/vmlinux.relocs: vmlinux $(obj)/relocs FORCE + $(call if_changed,relocs) + +vmlinux.bin.all-y := $(obj)/vmlinux.bin +vmlinux.bin.all-$(CONFIG_RELOCATABLE) += $(obj)/vmlinux.relocs +quiet_cmd_relocbin = BUILD $@ + cmd_relocbin = cat $(filter-out FORCE,$^) > $@ +$(obj)/vmlinux.bin.all: $(vmlinux.bin.all-y) FORCE + $(call if_changed,relocbin) + +ifdef CONFIG_RELOCATABLE +$(obj)/vmlinux.bin.gz: $(obj)/vmlinux.bin.all FORCE + $(call if_changed,gzip) +else $(obj)/vmlinux.bin.gz: $(obj)/vmlinux.bin FORCE $(call if_changed,gzip) +endif LDFLAGS_piggy.o := -r --format binary --oformat elf32-i386 -T -$(obj)/piggy.o: $(obj)/vmlinux.scr $(obj)/vmlinux.bin.gz FORCE +$(obj)/piggy.o: $(src)/vmlinux.scr $(obj)/vmlinux.bin.gz FORCE $(call if_changed,ld)
arch/i386/boot/compressed/misc.c renamed from arch/i386/boot/compressed/misc.c~i386-Relocatable-kernel-support +136 -131
333 lines not on this page · raw message
arch/i386/boot/compressed/relocs.c new file +563 -0
563 lines not on this page · raw message
arch/i386/boot/compressed/vmlinux.lds new file +43 -0
43 lines not on this page · raw message
arch/i386/boot/compressed/vmlinux.scr renamed from arch/i386/boot/compressed/vmlinux.scr~i386-Relocatable-kernel-support +2 -1
@@ -1,9 +1,10 @@ SECTIONS { - .data : { + .data.compressed : { input_len = .; LONG(input_data_end - input_data) input_data = .; *(.data) + output_len = . - 4; input_data_end = .; } }
arch/i386/boot/setup.S renamed from arch/i386/boot/setup.S~i386-Relocatable-kernel-support +21 -8
@@ -588,11 +588,6 @@ rmodeswtch_normal: call default_switch rmodeswtch_end: -# we get the code32 start address and modify the below 'jmpi' -# (loader may have changed it) - movl %cs:code32_start, %eax - movl %eax, %cs:code32 - # Now we move the system to its rightful place ... but we check if we have a # big-kernel. In that case we *must* not move it ... testb $LOADED_HIGH, %cs:loadflags
@@ -788,11 +783,12 @@ a20_err_msg: a20_done: #endif /* CONFIG_X86_VOYAGER */ -# set up gdt and idt +# set up gdt and idt and 32bit start address lidt idt_48 # load idt with 0,0 xorl %eax, %eax # Compute gdt_base movw %ds, %ax # (Convert %ds:gdt to a linear ptr) shll $4, %eax + addl %eax, code32 addl $gdt, %eax movl %eax, (gdt_48+2) lgdt gdt_48 # load gdt with whatever is
@@ -851,9 +847,26 @@ flush_instr: # Manual, Mixing 16-bit and 32-bit code, page 16-6) .byte 0x66, 0xea # prefix + jmpi-opcode -code32: .long 0x1000 # will be set to 0x100000 - # for big kernels +code32: .long startup_32 # will be set to %cs+startup_32 .word __BOOT_CS +.code32 +startup_32: + movl $(__BOOT_DS), %eax + movl %eax, %ds + movl %eax, %es + movl %eax, %fs + movl %eax, %gs + movl %eax, %ss + + xorl %eax, %eax +1: incl %eax # check that A20 really IS enabled + movl %eax, 0x00000000 # loop forever if it isn't + cmpl %eax, 0x00100000 + je 1b + + # Jump to the 32bit entry point + jmpl *(code32_start - start + (DELTA_INITSEG << 4))(%esi) +.code16 # Here's a bunch of information about your current kernel.. kernel_version: .ascii UTS_RELEASE
arch/i386/Kconfig renamed from arch/i386/Kconfig~i386-Relocatable-kernel-support +12 -0
@@ -773,6 +773,18 @@ config CRASH_DUMP PHYSICAL_START. For more details see Documentation/kdump/kdump.txt +config RELOCATABLE + bool "Build a relocatable kernel" + help + This build a kernel image that retains relocation information + so it can be loaded someplace besides the default 1MB. + The relocations tend to the kernel binary about 10% larger, + but are discarded at runtime. + + One use is for the kexec on panic case where the recovery kernel + must live at a different physical address than the primary + kernel. + config PHYSICAL_START hex "Physical address where the kernel is loaded" if (EMBEDDED || CRASH_DUMP)
arch/i386/Makefile renamed from arch/i386/Makefile~i386-Relocatable-kernel-support +3 -1
@@ -26,7 +26,9 @@ endif LDFLAGS := -m elf_i386 OBJCOPYFLAGS := -O binary -R .note -R .comment -S -LDFLAGS_vmlinux := +ifdef CONFIG_RELOCATABLE +LDFLAGS_vmlinux := --emit-relocs +endif CHECKFLAGS += -D__i386__ CFLAGS += -pipe -msoft-float
include/linux/screen_info.h renamed from include/linux/screen_info.h~i386-Relocatable-kernel-support +2 -1
@@ -42,7 +42,8 @@ struct screen_info {u16 pages;/* 0x32 */ u16 vesa_attributes;/* 0x34 */ u32 capabilities;/* 0x36 */ -/* 0x3a -- 0x3f reserved for future expansion */ +/* 0x3a -- 0x3b reserved for future expansion */ +/* 0x3c -- 0x3f micro stack for relocatable kernels */ };extern struct screen_info screen_info;
_
PATCH 9/11 i386: Warn upon absolute relocations being present
In reply to Vivek Goyalo Relocations generated w.r.t absolute symbols are not processed as by definition, absolute symbols are not to be relocated. Explicitly warn user about absolutions relocations present at compile time.
o These relocations get introduced either due to linker optimizations or some programming oversights.
o Also create a list of symbols which have been audited to be safe and don't emit warnings for these.
---
arch/i386/boot/compressed/Makefile | 2 arch/i386/boot/compressed/relocs.c | 82 ++++++++++++++++++++++++++++++++----- 2 files changed, 73 insertions(+), 11 deletions(-)
arch/i386/boot/compressed/Makefile renamed from arch/i386/boot/compressed/Makefile~i386-explicitly-warn-about-absolute-symbols-during-compile +1 -1
@@ -20,7 +20,7 @@ $(obj)/vmlinux.bin: vmlinux FORCE $(call if_changed,objcopy) quiet_cmd_relocs = RELOCS $@ - cmd_relocs = $(obj)/relocs $< > $@ + cmd_relocs = $(obj)/relocs $< > $@;$(obj)/relocs --abs-relocs $< $(obj)/vmlinux.relocs: vmlinux $(obj)/relocs FORCE $(call if_changed,relocs)
arch/i386/boot/compressed/relocs.c renamed from arch/i386/boot/compressed/relocs.c~i386-explicitly-warn-about-absolute-symbols-during-compile +72 -10
133 lines not on this page · raw message
_
PATCH 10/11 i386: Implement CONFIG_PHYSICAL_ALIGN
In reply to Vivek Goyalo Now CONFIG_PHYSICAL_START is being replaced with CONFIG_PHYSICAL_ALIGN. Hardcoding the kernel physical start value creates a problem in relocatable kernel context due to boot loader limitations. For ex, if somebody compiles a relocatable kernel to be run from address 4MB, but this kernel will run from location 1MB as grub loads the kernel at physical address 1MB. Kernel thinks that I am a relocatable kernel and I should run from the address I have been loaded at. So somebody wanting to run kernel from 4MB alignment location (for improved performance regions) can't do that.
o Hence, Eric proposed that probably CONFIG_PHYSICAL_ALIGN will make more sense in relocatable kernel context. At run time kernel will move itself to a physical addr location which meets user specified alignment restrictions.
---
arch/i386/Kconfig | 33 ++++++++++++++++++--------------- arch/i386/boot/compressed/head.S | 26 ++++++++++++++------------ arch/i386/boot/compressed/misc.c | 7 ++++--- arch/i386/kernel/vmlinux.lds.S | 3 ++- include/asm-i386/boot.h | 6 +++++- 5 files changed, 43 insertions(+), 32 deletions(-)
arch/i386/boot/compressed/head.S renamed from arch/i386/boot/compressed/head.S~i386-implement-config-physical-align-option +14 -12
@@ -26,6 +26,7 @@ #include <linux/linkage.h> #include <asm/segment.h> #include <asm/page.h> +#include <asm/boot.h> .section ".text.head" .globl startup_32
@@ -52,17 +53,17 @@ startup_32: 1: popl %ebp subl $1b, %ebp -/* Compute the delta between where we were compiled to run at - * and where the code will actually run at. +/* %ebp contains the address we are loaded at by the boot loader and %ebx + * contains the address where we should move the kernel image temporarily + * for safe in-place decompression. */ - /* Start with the delta to where the kernel will run at. If we are - * a relocatable kernel this is the delta to our load address otherwise - * this is the delta to CONFIG_PHYSICAL start. - */ + #ifdef CONFIG_RELOCATABLE - movl %ebp, %ebx + movl %ebp, %ebx + addl $(CONFIG_PHYSICAL_ALIGN - 1), %ebx + andl $(~(CONFIG_PHYSICAL_ALIGN - 1)), %ebx #else - movl $(CONFIG_PHYSICAL_START - startup_32), %ebx + movl $LOAD_PHYSICAL_ADDR, %ebx #endif /* Replace the compressed data size with the uncompressed size */
@@ -94,9 +95,10 @@ startup_32: /* Compute the kernel start address. */ #ifdef CONFIG_RELOCATABLE - leal startup_32(%ebp), %ebp + addl $(CONFIG_PHYSICAL_ALIGN - 1), %ebp + andl $(~(CONFIG_PHYSICAL_ALIGN - 1)), %ebp #else - movl $CONFIG_PHYSICAL_START, %ebp + movl $LOAD_PHYSICAL_ADDR, %ebp #endif /*
@@ -150,8 +152,8 @@ relocated: * and where it was actually loaded. */ movl %ebp, %ebx - subl $CONFIG_PHYSICAL_START, %ebx - + subl $LOAD_PHYSICAL_ADDR, %ebx + jz 2f /* Nothing to be done if loaded at compiled addr. */ /* * Process relocations. */
arch/i386/boot/compressed/misc.c renamed from arch/i386/boot/compressed/misc.c~i386-implement-config-physical-align-option +4 -3
@@ -14,6 +14,7 @@#include <linux/screen_info.h> #include <asm/io.h> #include <asm/page.h> +#include <asm/boot.h> /* WARNING!! * This code is compiled with -fPIC and it is relocated dynamically
@@ -360,12 +361,12 @@ asmlinkage void decompress_kernel(void * insize = input_len; inptr =0 ; -if (((u32 )output -CONFIG_PHYSICAL_START ) &0x3fffff ) -error ("Destination address not 4M aligned" ); +if ((u32 )output & (CONFIG_PHYSICAL_ALIGN -1 )) +error ("Destination address not CONFIG_PHYSICAL_ALIGN aligned" );if (end > ((-__PAGE_OFFSET-(512 <<20 )-1 ) &0x7fffffff ))error ("Destination address too large" );#ifndef CONFIG_RELOCATABLE -if ((u32 )output !=CONFIG_PHYSICAL_START ) +if ((u32 )output !=LOAD_PHYSICAL_ADDR )error ("Wrong destination address" );#endif
arch/i386/Kconfig renamed from arch/i386/Kconfig~i386-implement-config-physical-align-option +18 -15
@@ -785,23 +785,26 @@ config RELOCATABLE must live at a different physical address than the primary kernel. -config PHYSICAL_START - hex "Physical address where the kernel is loaded" if (EMBEDDED || CRASH_DUMP) - - default "0x1000000" if CRASH_DUMP +config PHYSICAL_ALIGN + hex "Alignment value to which kernel should be aligned" default "0x100000" + range 0x2000 0x400000 help - This gives the physical address where the kernel is loaded. Normally - for regular kernels this value is 0x100000 (1MB). But in the case - of kexec on panic the fail safe kernel needs to run at a different - address than the panic-ed kernel. This option is used to set the load - address for kernels used to capture crash dump on being kexec'ed - after panic. The default value for crash dump kernels is - 0x1000000 (16MB). This can also be set based on the "X" value as - specified in the "crashkernel=YM@XM" command line boot parameter - passed to the panic-ed kernel. Typically this parameter is set as - crashkernel=64M@16M. Please take a look at - Documentation/kdump/kdump.txt for more details about crash dumps. + This value puts the alignment restrictions on physical address + where kernel is loaded and run from. Kernel is compiled for an + address which meets above alignment restriction. + + If bootloader loads the kernel at a non-aligned address and + CONFIG_RELOCATABLE is set, kernel will move itself to nearest + address aligned to above value and run from there. + + If bootloader loads the kernel at a non-aligned address and + CONFIG_RELOCATABLE is not set, kernel will ignore the run time + load address and decompress itself to the address it has been + compiled for and run from there. The address for which kernel is + compiled already meets above alignment restrictions. Hence the + end result is that kernel runs from a physical address meeting + above alignment restrictions. Don't change this unless you know what you are doing.
arch/i386/kernel/vmlinux.lds.S renamed from arch/i386/kernel/vmlinux.lds.S~i386-implement-config-physical-align-option +2 -1
@@ -8,6 +8,7 @@ #include <asm/thread_info.h> #include <asm/page.h> #include <asm/cache.h> +#include <asm/boot.h> OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386") OUTPUT_ARCH(i386)
@@ -21,7 +22,7 @@ PHDRS { } SECTIONS { - . = LOAD_OFFSET + CONFIG_PHYSICAL_START; + . = LOAD_OFFSET + LOAD_PHYSICAL_ADDR; phys_startup_32 = startup_32 - LOAD_OFFSET; /* read-only */ .text : AT(ADDR(.text) - LOAD_OFFSET) {
include/asm-i386/boot.h renamed from include/asm-i386/boot.h~i386-implement-config-physical-align-option +5 -1
@@ -12,4 +12,8 @@#define EXTENDED_VGA 0xfffe/* 80x50 mode */ #define ASK_VGA 0xfffd/* ask for it at bootup */ -#endif +/* Physical address where kenrel should be loaded. */ +#define LOAD_PHYSICAL_ADDR ((0x100000 + CONFIG_PHYSICAL_ALIGN - 1) \ + & ~(CONFIG_PHYSICAL_ALIGN - 1)) + +#endif /* _LINUX_BOOT_H */
_
PATCH 11/11 i386: Extend bzImage protocol for relocatable protected mode kernel
In reply to Vivek Goyalo Extend bzImage protocol to enable bootloaders to load a completely relocatable bzImage. Now protected mode component of kernel is also relocatable and a boot-loader can load the protected mode component at a differnt physical address than 1MB. (If kernel was built with CONFIG_RELOCATABLE)
o Kexec can make use of it to load this kernel at a different physical address to capture kernel crash dumps.
---
Documentation/i386/boot.txt | 4 ++++ arch/i386/boot/setup.S | 13 ++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-)
arch/i386/boot/setup.S renamed from arch/i386/boot/setup.S~extend-bzImage-protocol-for-relocatable-protected-mode-kernel +12 -1
@@ -81,7 +81,7 @@ start: # This is the setup header, and it must start at %cs:2 (old 0x9020:2) .ascii "HdrS" # header signature - .word 0x0204 # header version number (>= 0x0105) + .word 0x0205 # header version number (>= 0x0105) # or else old loadlin-1.5 will fail) realmode_swtch: .word 0, 0 # default_switch, SETUPSEG start_sys_seg: .word SYSSEG
@@ -160,6 +160,17 @@ ramdisk_max: .long (-__PAGE_OFFSET-(512 # The highest safe address for # the contents of an initrd +kernel_alignment: .long CONFIG_PHYSICAL_ALIGN #physical addr alignment + #required for protected mode + #kernel +#ifdef CONFIG_RELOCATABLE +relocatable_kernel: .byte 1 +#else +relocatable_kernel: .byte 0 +#endif +pad2: .byte 0 +pad3: .word 0 + trampoline: call start_of_setup .align 16 # The offset at this point is 0x240
Documentation/i386/boot.txt renamed from Documentation/i386/boot.txt~extend-bzImage-protocol-for-relocatable-protected-mode-kernel +4 -0
@@ -35,6 +35,8 @@ Protocol 2.03: (Kernel 2.4.18-pre1) Expl initrd address available to the bootloader. Protocol 2.04: (Kernel 2.6.14) Extend the syssize field to four bytes. +Protocol 2.05: (Kernel 2.6.20) Make protected mode kernel relocatable. + Introduce relocatable_kernel and kernel_alignment fields. **** MEMORY LAYOUT
@@ -129,6 +131,8 @@ Offset Proto Name Meaning 0226/2 N/A pad1 Unused 0228/4 2.02+ cmd_line_ptr 32-bit pointer to the kernel command line 022C/4 2.03+ initrd_addr_max Highest legal initrd address +0230/4 2.04+ kernel_alignment Physical addr alignment required for kernel +0234/1 2.04+ relocatable_kernel Whether kernel is relocatable or not (1) For backwards compatibility, if the setup_sects field contains 0, the real value is 4.
_
Quoted 5 lines · Vivek Goyal
Hi,
Here is the third attempt on implementing relocatable bzImage for i386. Eric has done all the ground work and I am just giving it final finish. Generated patches against (2.6.19-rc2-git7).
I merged them now. Didn't see anything that i didn't like, but I admit i haven't read everything closely
-Andi