Commit 968de4f0 authored by Eric W. Biederman's avatar Eric W. Biederman Committed by Andi Kleen

[PATCH] i386: Relocatable kernel support

This patch modifies the i386 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.
Signed-off-by: default avatarEric W. Biederman <ebiederm@xmission.com>
Signed-off-by: default avatarVivek Goyal <vgoyal@in.ibm.com>
Signed-off-by: default avatarAndi Kleen <ak@suse.de>
parent fd593d12
...@@ -773,6 +773,18 @@ config CRASH_DUMP ...@@ -773,6 +773,18 @@ config CRASH_DUMP
PHYSICAL_START. PHYSICAL_START.
For more details see Documentation/kdump/kdump.txt 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 config PHYSICAL_START
hex "Physical address where the kernel is loaded" if (EMBEDDED || CRASH_DUMP) hex "Physical address where the kernel is loaded" if (EMBEDDED || CRASH_DUMP)
......
...@@ -26,7 +26,9 @@ endif ...@@ -26,7 +26,9 @@ endif
LDFLAGS := -m elf_i386 LDFLAGS := -m elf_i386
OBJCOPYFLAGS := -O binary -R .note -R .comment -S OBJCOPYFLAGS := -O binary -R .note -R .comment -S
LDFLAGS_vmlinux := ifdef CONFIG_RELOCATABLE
LDFLAGS_vmlinux := --emit-relocs
endif
CHECKFLAGS += -D__i386__ CHECKFLAGS += -D__i386__
CFLAGS += -pipe -msoft-float CFLAGS += -pipe -msoft-float
......
...@@ -4,22 +4,42 @@ ...@@ -4,22 +4,42 @@
# create a compressed vmlinux image from the original vmlinux # 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 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) $(call if_changed,ld)
@: @:
$(obj)/vmlinux.bin: vmlinux FORCE $(obj)/vmlinux.bin: vmlinux FORCE
$(call if_changed,objcopy) $(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 $(obj)/vmlinux.bin.gz: $(obj)/vmlinux.bin FORCE
$(call if_changed,gzip) $(call if_changed,gzip)
endif
LDFLAGS_piggy.o := -r --format binary --oformat elf32-i386 -T 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) $(call if_changed,ld)
...@@ -25,9 +25,11 @@ ...@@ -25,9 +25,11 @@
#include <linux/linkage.h> #include <linux/linkage.h>
#include <asm/segment.h> #include <asm/segment.h>
#include <asm/page.h>
.section ".text.head"
.globl startup_32 .globl startup_32
startup_32: startup_32:
cld cld
cli cli
...@@ -36,93 +38,141 @@ startup_32: ...@@ -36,93 +38,141 @@ startup_32:
movl %eax,%es movl %eax,%es
movl %eax,%fs movl %eax,%fs
movl %eax,%gs movl %eax,%gs
movl %eax,%ss
lss stack_start,%esp /* Calculate the delta between where we were compiled to run
xorl %eax,%eax * at and where we were actually loaded at. This can only be done
1: incl %eax # check that A20 really IS enabled * with a short local call on x86. Nothing else will tell us what
movl %eax,0x000000 # loop forever if it isn't * address we are running at. The reserved chunk of the real-mode
cmpl %eax,0x100000 * data at 0x34-0x3f are used as the stack for this calculation.
je 1b * 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 * Jump to the relocated address.
* confuse the debugger if this code is traced.
* XXX - best to initialize before switching to protected mode.
*/ */
pushl $0 leal relocated(%ebx), %eax
popfl jmp *%eax
.section ".text"
relocated:
/* /*
* Clear BSS * Clear BSS
*/ */
xorl %eax,%eax xorl %eax,%eax
movl $_edata,%edi leal _edata(%ebx),%edi
movl $_end,%ecx leal _end(%ebx), %ecx
subl %edi,%ecx subl %edi,%ecx
cld cld
rep rep
stosb stosb
/*
* Setup the stack for the decompressor
*/
leal stack_end(%ebx), %esp
/* /*
* Do the decompression, and jump to the new kernel.. * Do the decompression, and jump to the new kernel..
*/ */
subl $16,%esp # place for structure on the stack movl output_len(%ebx), %eax
movl %esp,%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 %esi # real mode pointer as second arg
pushl %eax # address of structure as first arg
call decompress_kernel call decompress_kernel
orl %eax,%eax addl $20, %esp
jnz 3f popl %ecx
popl %esi # discard address
popl %esi # real mode pointer #if CONFIG_RELOCATABLE
xorl %ebx,%ebx /* Find the address of the relocations.
ljmp $(__BOOT_CS), $CONFIG_PHYSICAL_START */
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. * Process relocations.
* 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 1: subl $4, %edi
movl $0x1000,%edi movl 0(%edi), %ecx
movl $move_routine_end,%ecx testl %ecx, %ecx
subl %esi,%ecx jz 2f
addl $3,%ecx addl %ebx, -__PAGE_OFFSET(%ebx, %ecx)
shrl $2,%ecx jmp 1b
cld 2:
rep #endif
movsl
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, * Jump to the decompressed kernel.
* 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
xorl %ebx,%ebx xorl %ebx,%ebx
ljmp $(__BOOT_CS), $CONFIG_PHYSICAL_START jmp *%ebp
move_routine_end:
.bss
.balign 4
stack:
.fill 4096, 1, 0
stack_end:
This diff is collapsed.
This diff is collapsed.
OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386")
OUTPUT_ARCH(i386)
ENTRY(startup_32)
SECTIONS
{
/* Be careful parts of head.S assume startup_32 is at
* address 0.
*/
. = 0 ;
.text.head : {
_head = . ;
*(.text.head)
_ehead = . ;
}
.data.compressed : {
*(.data.compressed)
}
.text : {
_text = .; /* Text */
*(.text)
*(.text.*)
_etext = . ;
}
.rodata : {
_rodata = . ;
*(.rodata) /* read-only data */
*(.rodata.*)
_erodata = . ;
}
.data : {
_data = . ;
*(.data)
*(.data.*)
_edata = . ;
}
.bss : {
_bss = . ;
*(.bss)
*(.bss.*)
*(COMMON)
_end = . ;
}
}
SECTIONS SECTIONS
{ {
.data : { .data.compressed : {
input_len = .; input_len = .;
LONG(input_data_end - input_data) input_data = .; LONG(input_data_end - input_data) input_data = .;
*(.data) *(.data)
output_len = . - 4;
input_data_end = .; input_data_end = .;
} }
} }
...@@ -588,11 +588,6 @@ rmodeswtch_normal: ...@@ -588,11 +588,6 @@ rmodeswtch_normal:
call default_switch call default_switch
rmodeswtch_end: 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 # 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 ... # big-kernel. In that case we *must* not move it ...
testb $LOADED_HIGH, %cs:loadflags testb $LOADED_HIGH, %cs:loadflags
...@@ -788,11 +783,12 @@ a20_err_msg: ...@@ -788,11 +783,12 @@ a20_err_msg:
a20_done: a20_done:
#endif /* CONFIG_X86_VOYAGER */ #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 lidt idt_48 # load idt with 0,0
xorl %eax, %eax # Compute gdt_base xorl %eax, %eax # Compute gdt_base
movw %ds, %ax # (Convert %ds:gdt to a linear ptr) movw %ds, %ax # (Convert %ds:gdt to a linear ptr)
shll $4, %eax shll $4, %eax
addl %eax, code32
addl $gdt, %eax addl $gdt, %eax
movl %eax, (gdt_48+2) movl %eax, (gdt_48+2)
lgdt gdt_48 # load gdt with whatever is lgdt gdt_48 # load gdt with whatever is
...@@ -851,9 +847,26 @@ flush_instr: ...@@ -851,9 +847,26 @@ flush_instr:
# Manual, Mixing 16-bit and 32-bit code, page 16-6) # Manual, Mixing 16-bit and 32-bit code, page 16-6)
.byte 0x66, 0xea # prefix + jmpi-opcode .byte 0x66, 0xea # prefix + jmpi-opcode
code32: .long 0x1000 # will be set to 0x100000 code32: .long startup_32 # will be set to %cs+startup_32
# for big kernels
.word __BOOT_CS .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.. # Here's a bunch of information about your current kernel..
kernel_version: .ascii UTS_RELEASE kernel_version: .ascii UTS_RELEASE
......
...@@ -42,7 +42,8 @@ struct screen_info { ...@@ -42,7 +42,8 @@ struct screen_info {
u16 pages; /* 0x32 */ u16 pages; /* 0x32 */
u16 vesa_attributes; /* 0x34 */ u16 vesa_attributes; /* 0x34 */
u32 capabilities; /* 0x36 */ 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; extern struct screen_info screen_info;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment