Changeset cfdeedc in mainline


Ignore:
Timestamp:
2018-10-21T23:12:23Z (6 years ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
bf05c74
Parents:
d59718e
git-author:
Jiří Zárevúcky <zarevucky.jiri@…> (2018-10-21 22:53:48)
git-committer:
Jiří Zárevúcky <zarevucky.jiri@…> (2018-10-21 23:12:23)
Message:

Keep kernel in ELF format

By keeping kernel in an ELF file (instead of converting it to
a flat binary), we can use the information it contains, like
symbol table and debug info.

We can also later implement more advanced functionality, like
loading kernel at multiple discontiguous blocks, or loading
a position-independent kernel at a randomized address.

Currently the functionality is quite restricted, to keep changes
to a minimum. Code in boot/generic/src/kernel.c validates that
the kernel image was built with the same addresses as the boot
loader uses, giving an extra level of sanity checking compared
to a flat binary.

Files:
2 added
38 edited

Legend:

Unmodified
Added
Removed
  • boot/Makefile.common

    rd59718e rcfdeedc  
    250250
    251251COMPONENTS = \
    252         $(KERNEL_PATH)/kernel.bin \
     252        $(KERNEL_PATH)/kernel.elf \
    253253        $(INIT_TASKS) \
    254254        $(INITRD).img
  • boot/Makefile.grub

    rd59718e rcfdeedc  
    8585        for module in $(MODULES) ; do \
    8686                echo "  echo 'Loading $$module'" >> $(BOOT_CONFIG) ; \
    87                 if [ "$$module" = "kernel.bin" ] ; then \
     87                if [ "$$module" = "kernel.elf" ] ; then \
    8888                        echo "  $(MULTIBOOT_CMD) /boot/$$module" >> $(BOOT_CONFIG) ; \
    8989                else \
  • boot/arch/arm32/Makefile.inc

    rd59718e rcfdeedc  
    107107        generic/src/gzip.c \
    108108        generic/src/tar.c \
     109        generic/src/kernel.c \
    109110        generic/src/payload.c
  • boot/arch/arm32/src/main.c

    rd59718e rcfdeedc  
    5050#include <arch/cp15.h>
    5151#include <payload.h>
     52#include <kernel.h>
    5253
    5354static void clean_dcache_poc(void *address, size_t size)
     
    104105        clean_dcache_poc(boot_pt, PTL0_ENTRIES * PTL0_ENTRY_SIZE);
    105106
     107        uintptr_t entry = check_kernel((void *) PA2KA(BOOT_OFFSET));
     108
    106109        printf("Booting the kernel...\n");
    107         jump_to_kernel((void *) PA2KA(BOOT_OFFSET), &bootinfo);
     110        jump_to_kernel((void *) entry, &bootinfo);
    108111}
    109112
  • boot/arch/ia64/Makefile.inc

    rd59718e rcfdeedc  
    6161        generic/src/tar.c \
    6262        generic/src/gzip.c \
     63        generic/src/kernel.c \
    6364        generic/src/payload.c
    6465
  • boot/arch/ia64/include/arch/arch.h

    rd59718e rcfdeedc  
    3636#define LOADER_ADDRESS  0x4400000
    3737#define KERNEL_ADDRESS  0x4800000
     38#define KERNEL_VADDRESS 0xe000000004800000
    3839
    3940#define STACK_SIZE                   8192
  • boot/arch/ia64/src/main.c

    rd59718e rcfdeedc  
    4444#include <errno.h>
    4545#include <payload.h>
     46#include <kernel.h>
    4647
    4748#define DEFAULT_MEMORY_BASE             0x4000000ULL
     
    182183            (uintptr_t) kernel_start, NULL);
    183184
    184         printf("Booting the kernel ...\n");
    185         jump_to_kernel(&bootinfo, kernel_start);
     185        uintptr_t entry = check_kernel(kernel_start);
     186
     187        // FIXME: kernel's entry point is linked at a different address than
     188        //        where it is run from.
     189        entry = entry - KERNEL_VADDRESS + KERNEL_ADDRESS;
     190
     191        printf("Booting the kernel at %p...\n", (void *) entry);
     192        jump_to_kernel(&bootinfo, (void *) entry);
    186193}
  • boot/arch/mips32/Makefile.inc

    rd59718e rcfdeedc  
    8888        generic/src/gzip.c \
    8989        generic/src/tar.c \
     90        generic/src/kernel.c \
    9091        generic/src/payload.c
  • boot/arch/mips32/src/main.c

    rd59718e rcfdeedc  
    4040#include <errno.h>
    4141#include <payload.h>
     42#include <kernel.h>
    4243
    4344static bootinfo_t *bootinfo = (bootinfo_t *) PA2KA(BOOTINFO_OFFSET);
     
    7879        }
    7980
    80         printf("Booting the kernel ... \n");
    81         jump_to_kernel((void *) PA2KA(BOOT_OFFSET), bootinfo);
     81        uintptr_t entry = check_kernel(kernel_start);
     82
     83        printf("Booting the kernel...\n");
     84        jump_to_kernel((void *) entry, bootinfo);
    8285}
  • boot/arch/ppc32/Makefile.inc

    rd59718e rcfdeedc  
    7676        generic/src/gzip.c \
    7777        generic/src/tar.c \
     78        generic/src/kernel.c \
    7879        generic/src/payload.c
  • boot/arch/ppc32/_link.ld.in

    rd59718e rcfdeedc  
    66                loader_start = .;
    77                *(BOOTSTRAP);
    8                 *(REALMODE);
    98                *(.text);
    109        }
  • boot/arch/ppc32/src/asm.S

    rd59718e rcfdeedc  
    152152FUNCTION_END(jump_to_kernel)
    153153
    154 .section REALMODE, "ax"
    155 
    156 .align PAGE_WIDTH
    157154SYMBOL(real_mode)
    158155
  • boot/arch/ppc32/src/main.c

    rd59718e rcfdeedc  
    4242#include <errno.h>
    4343#include <payload.h>
     44#include <kernel.h>
    4445
    4546#define BALLOC_MAX_SIZE  131072
     
    6869        printf(" %p|%p: real mode trampoline\n", &real_mode, real_mode_pa);
    6970        printf(" %p|%p: boot info structure\n", &bootinfo, bootinfo_pa);
    70         printf(" %p|%p: kernel entry point\n",
    71             (void *) PA2KA(BOOT_OFFSET), (void *) BOOT_OFFSET);
    7271        printf(" %p|%p: loader entry point\n",
    7372            (void *) LOADER_ADDRESS, loader_address_pa);
     
    144143        }
    145144
     145        uintptr_t entry = check_kernel_translated(inflate_base, 0);
     146
    146147        printf("Booting the kernel...\n");
    147         jump_to_kernel(bootinfo_pa, transtable_pa, pages, real_mode_pa,
    148             PA2KA(BOOT_OFFSET));
     148        jump_to_kernel(bootinfo_pa, transtable_pa, pages, real_mode_pa, entry);
    149149}
  • boot/arch/riscv64/Makefile.inc

    rd59718e rcfdeedc  
    3030BFD_OUTPUT = $(BFD_NAME)
    3131BFD_ARCH = riscv
    32 BFD = binary
    3332
    3433BITS = 64
     
    5150        generic/src/gzip.c \
    5251        generic/src/tar.c \
     52        generic/src/kernel.c \
    5353        generic/src/payload.c
  • boot/arch/riscv64/include/arch/asm.h

    rd59718e rcfdeedc  
    3636extern char pt_page[];
    3737
    38 extern _Noreturn void jump_to_kernel(uintptr_t);
     38extern _Noreturn void jump_to_kernel(uintptr_t, uintptr_t);
    3939
    4040#endif
  • boot/arch/riscv64/src/asm.S

    rd59718e rcfdeedc  
    125125        csrw mstatus, t0
    126126
    127         li ra, PA2KA(BOOT_OFFSET)
     127        /* Entry point address is in a1. */
     128        mv ra, a1
    128129        csrw mepc, ra
    129130
  • boot/arch/riscv64/src/main.c

    rd59718e rcfdeedc  
    4242#include <halt.h>
    4343#include <payload.h>
     44#include <kernel.h>
    4445
    4546static bootinfo_t bootinfo;
     
    8889        extract_payload(&bootinfo.taskmap, load_addr, end, kernel_addr, NULL);
    8990
     91        uintptr_t entry = check_kernel(load_addr);
     92
    9093        printf("Booting the kernel...\n");
    91         jump_to_kernel(PA2KA(&bootinfo));
     94        jump_to_kernel(PA2KA(&bootinfo), entry);
    9295}
  • boot/arch/sparc64/Makefile.inc

    rd59718e rcfdeedc  
    7272        generic/src/gzip.c \
    7373        generic/src/tar.c \
     74        generic/src/kernel.c \
    7475        generic/src/payload.c
  • boot/arch/sparc64/src/main.c

    rd59718e rcfdeedc  
    4343#include <errno.h>
    4444#include <payload.h>
     45#include <kernel.h>
    4546
    4647/* The lowest ID (read from the VER register) of some US3 CPU model */
     
    257258                sun4u_smp();
    258259
     260        uintptr_t entry = check_kernel((void *) KERNEL_ADDRESS);
     261
    259262        printf("Booting the kernel ...\n");
    260         jump_to_kernel(bootinfo.physmem_start | BSP_PROCESSOR, &bootinfo, subarch,
    261             (void *) KERNEL_ADDRESS);
    262 }
     263        jump_to_kernel(bootinfo.physmem_start | BSP_PROCESSOR, &bootinfo,
     264            subarch, (void *) entry);
     265}
  • kernel/Makefile

    rd59718e rcfdeedc  
    4949
    5050AUTOCHECK = $(realpath $(ROOT_PATH)/tools/autocheck.awk)
    51 RAW = kernel.raw
    52 BIN = kernel.bin
     51KERNEL = kernel.elf
    5352MAP = kernel.map
    5453JOB = kernel.job
     
    7574.DELETE_ON_ERROR:
    7675
    77 all: $(VERSION_DEF) $(COMMON_MAKEFILE) $(COMMON_HEADER) $(CONFIG_MAKEFILE) $(CONFIG_HEADER) $(BIN) $(DISASM)
     76all: $(VERSION_DEF) $(COMMON_MAKEFILE) $(COMMON_HEADER) $(CONFIG_MAKEFILE) $(CONFIG_HEADER) $(KERNEL) $(DISASM)
    7877
    7978clean: autogen_clean
    80         rm -f $(RAW) $(BIN) $(MAP) $(JOB) $(MAP_PREV) $(DISASM) $(DUMP) $(REAL_MAP).* arch/*/_link.ld arch/*/include/arch/common.h
     79        rm -f $(KERNEL) $(MAP) $(JOB) $(MAP_PREV) $(DISASM) $(DUMP) $(REAL_MAP).* arch/*/_link.ld arch/*/include/arch/common.h
    8180        find generic/src/ arch/*/src/ genarch/src/ test/ -name '*.o' -follow -exec rm '{}' \;
    8281        find generic/src/ arch/*/src/ genarch/src/ test/ -name '*.d' -follow -exec rm '{}' \;
     
    370369endif
    371370
    372 $(BIN): $(RAW)
    373         $(OBJCOPY) -O $(BFD) $< $@
    374 
    375 $(DISASM): $(RAW)
     371$(DISASM): $(KERNEL)
    376372ifeq ($(CONFIG_LINE_DEBUG),y)
    377373        $(OBJDUMP) -d -S $< > $@
     
    380376endif
    381377
    382 $(RAW): $(LINK) $(ARCH_OBJECTS) $(GENARCH_OBJECTS) $(GENERIC_OBJECTS) $(SYMTAB_OBJECTS)
     378$(KERNEL): $(LINK) $(ARCH_OBJECTS) $(GENARCH_OBJECTS) $(GENERIC_OBJECTS) $(SYMTAB_OBJECTS)
    383379        $(CC) $(DEFS) $(CFLAGS) $(LDFLAGS) -Wl,-Map,$(MAP) -o $@ $(ARCH_OBJECTS) $(GENARCH_OBJECTS) $(GENERIC_OBJECTS) $(EXTRA_OBJECTS) $(SYMTAB_OBJECTS)
    384380
  • kernel/arch/abs32le/Makefile.inc

    rd59718e rcfdeedc  
    2727#
    2828
    29 BFD = binary
    3029
    3130ifeq ($(COMPILER),gcc_cross)
  • kernel/arch/amd64/Makefile.inc

    rd59718e rcfdeedc  
    2727#
    2828
    29 BFD_NAME = elf64-x86-64
    30 BFD_ARCH = i386:x86-64
    31 BFD = binary
    3229
    3330FPU_NO_CFLAGS = -mno-sse -mno-sse2
  • kernel/arch/amd64/_link.ld.in

    rd59718e rcfdeedc  
    3333                *(COMMON);      /* global variables */
    3434
     35                /* XXX: bss can't be omitted from the ELF image. */
     36                *(.bss);        /* uninitialized static variables */
     37
    3538                . = ALIGN(8);
    3639                symbol_table = .;
    3740                *(symtab.*);    /* Symbol table, must be LAST symbol!*/
    38 
    39                 *(.bss);        /* uninitialized static variables */
    4041
    4142                kdata_end = .;
  • kernel/arch/arm32/Makefile.inc

    rd59718e rcfdeedc  
    2727#
    2828
    29 BFD_NAME = elf32-littlearm
    30 BFD_ARCH = arm
    31 BFD = binary
    3229
    3330ATSIGN = %
  • kernel/arch/arm32/_link.ld.in

    rd59718e rcfdeedc  
    2525        . = KERNEL_LOAD_ADDRESS;
    2626        kernel_load_address = .;
     27        . = . + SIZEOF_HEADERS;
    2728        .text : {
    2829                ktext_start = .;
  • kernel/arch/ia32/Makefile.inc

    rd59718e rcfdeedc  
    2727#
    2828
    29 BFD_NAME = elf32-i386
    30 BFD_ARCH = i386
    31 BFD = binary
    3229
    3330BITS = 32
  • kernel/arch/ia32/_link.ld.in

    rd59718e rcfdeedc  
    3131                *(.rodata .rodata.*);   /* string literals */
    3232                *(COMMON);              /* global variables */
     33
     34                /* XXX: bss can't be omitted from the ELF image. */
     35                *(.bss);                /* uninitialized static variables */
     36
    3337                . = ALIGN(8);
    3438                symbol_table = .;
    3539                *(symtab.*);            /* Symbol table, must be LAST symbol! */
    36                 *(.bss);                /* uninitialized static variables */
    3740                kdata_end = .;
    3841        }
  • kernel/arch/ia64/Makefile.inc

    rd59718e rcfdeedc  
    2727#
    2828
    29 BFD_NAME = elf64-little
    30 BFD_ARCH = ia64-elf64
    3129
    3230COMMON_CFLAGS += -mconstant-gp -fno-unwind-tables -mfixed-range=f32-f127
     
    6563ifeq ($(MACHINE),ski)
    6664        ARCH_SOURCES += arch/$(KARCH)/src/drivers/ski.c
    67         BFD = binary
    6865endif
    6966
    7067ifeq ($(MACHINE),i460GX)
    7168        DEFS += -DI460GX
    72         BFD = binary
    7369endif
    7470
  • kernel/arch/ia64/_link.ld.in

    rd59718e rcfdeedc  
    1515        kernel_load_address = LOAD_ADDRESS_V;
    1616
    17         .image LOAD_ADDRESS_V: AT (LOAD_ADDRESS_P) {
     17        .image (LOAD_ADDRESS_V + SIZEOF_HEADERS): AT (LOAD_ADDRESS_P + SIZEOF_HEADERS) {
     18                . = ALIGN(16);
    1819                ktext_start = .;
    1920                *(K_TEXT_START);
  • kernel/arch/mips32/Makefile.inc

    rd59718e rcfdeedc  
    2727#
    2828
    29 BFD_ARCH = mips
    30 BFD = binary
    3129COMMON_CFLAGS += -msoft-float -mno-abicalls -G 0 -fno-zero-initialized-in-bss -mabi=32
    3230AS_PROLOG = .module softfloat;
     
    3836
    3937ifeq ($(MACHINE),msim)
    40         BFD_NAME = elf32-tradlittlemips
    4138        ENDIANESS = LE
    4239        COMMON_CFLAGS += -march=r4000
    4340endif
    4441ifeq ($(MACHINE),bmalta)
    45         BFD_NAME = elf32-tradbigmips
    4642        ENDIANESS = BE
    4743        COMMON_CFLAGS += -D__BE__ -march=4kc
    4844endif
    4945ifeq ($(MACHINE),lmalta)
    50         BFD_NAME = elf32-tradlittlemips
    5146        ENDIANESS = LE
    5247        COMMON_CFLAGS += -march=4kc
  • kernel/arch/mips32/_link.ld.in

    rd59718e rcfdeedc  
    2424        . = KERNEL_LOAD_ADDRESS;
    2525        kernel_load_address = .;
     26        . = . + SIZEOF_HEADERS;
    2627
    2728        .text : {
  • kernel/arch/ppc32/Makefile.inc

    rd59718e rcfdeedc  
    2727#
    2828
    29 BFD_NAME = elf32-powerpc
    30 BFD_ARCH = powerpc:common
    31 BFD = binary
    3229
    3330COMMON_CFLAGS += -mcpu=powerpc -msoft-float -m32
  • kernel/arch/ppc32/_link.ld.in

    rd59718e rcfdeedc  
    2020        kernel_load_address = PA2KA(0);
    2121
    22         .unmapped 0: AT (0) {
     22        .unmapped (SIZEOF_HEADERS): AT (SIZEOF_HEADERS) {
     23                . = ALIGN(0x100);
    2324                *(K_UNMAPPED_TEXT_START);
    2425        }
     
    4647                kdata_end = .;
    4748        }
     49
     50        /DISCARD/ : {
     51                *(*);
     52        }
    4853}
  • kernel/arch/ppc32/src/exception.S

    rd59718e rcfdeedc  
    126126.endm
    127127
    128 .org 0x100
     128// XXX: K_UNMAPPED_TEXT_START section starts at 0x100,
     129// so all the following .org directives are relative to that.
     130#define ABSOLUTE(x) ((x) - 0x100)
     131
     132.org ABSOLUTE(0x100)
    129133SYMBOL(exc_system_reset)
    130134        CONTEXT_STORE
     
    133137        b jump_to_kernel
    134138
    135 .org 0x200
     139.org ABSOLUTE(0x200)
    136140SYMBOL(exc_machine_check)
    137141        CONTEXT_STORE
     
    140144        b jump_to_kernel
    141145
    142 .org 0x300
     146.org ABSOLUTE(0x300)
    143147SYMBOL(exc_data_storage)
    144148        CONTEXT_STORE
     
    147151        b jump_to_kernel
    148152
    149 .org 0x400
     153.org ABSOLUTE(0x400)
    150154SYMBOL(exc_instruction_storage)
    151155        CONTEXT_STORE
     
    154158        b jump_to_kernel
    155159
    156 .org 0x500
     160.org ABSOLUTE(0x500)
    157161SYMBOL(exc_external)
    158162        CONTEXT_STORE
     
    161165        b jump_to_kernel
    162166
    163 .org 0x600
     167.org ABSOLUTE(0x600)
    164168SYMBOL(exc_alignment)
    165169        CONTEXT_STORE
     
    168172        b jump_to_kernel
    169173
    170 .org 0x700
     174.org ABSOLUTE(0x700)
    171175SYMBOL(exc_program)
    172176        CONTEXT_STORE
     
    175179        b jump_to_kernel
    176180
    177 .org 0x800
     181.org ABSOLUTE(0x800)
    178182SYMBOL(exc_fp_unavailable)
    179183        CONTEXT_STORE
     
    182186        b jump_to_kernel
    183187
    184 .org 0x900
     188.org ABSOLUTE(0x900)
    185189SYMBOL(exc_decrementer)
    186190        CONTEXT_STORE
     
    189193        b jump_to_kernel
    190194
    191 .org 0xa00
     195.org ABSOLUTE(0xa00)
    192196SYMBOL(exc_reserved0)
    193197        CONTEXT_STORE
     
    196200        b jump_to_kernel
    197201
    198 .org 0xb00
     202.org ABSOLUTE(0xb00)
    199203SYMBOL(exc_reserved1)
    200204        CONTEXT_STORE
     
    203207        b jump_to_kernel
    204208
    205 .org 0xc00
     209.org ABSOLUTE(0xc00)
    206210SYMBOL(exc_syscall)
    207211        CONTEXT_STORE
     
    209213        b jump_to_kernel_syscall
    210214
    211 .org 0xd00
     215.org ABSOLUTE(0xd00)
    212216SYMBOL(exc_trace)
    213217        CONTEXT_STORE
     
    216220        b jump_to_kernel
    217221
    218 .org 0x1000
     222.org ABSOLUTE(0x1000)
    219223SYMBOL(exc_itlb_miss)
    220224        CONTEXT_STORE
     
    223227        b jump_to_kernel
    224228
    225 .org 0x1100
     229.org ABSOLUTE(0x1100)
    226230SYMBOL(exc_dtlb_miss_load)
    227231        CONTEXT_STORE
     
    230234        b jump_to_kernel
    231235
    232 .org 0x1200
     236.org ABSOLUTE(0x1200)
    233237SYMBOL(exc_dtlb_miss_store)
    234238        CONTEXT_STORE
     
    237241        b jump_to_kernel
    238242
    239 .org 0x4000
     243.org ABSOLUTE(0x4000)
    240244jump_to_kernel:
    241245        mfsrr1 r5
     
    289293        addis sp, sp, 0x8000
    290294        rfi
     295
     296/* Rest is for stack. */
     297
     298.org ABSOLUTE(0x8000)
  • kernel/arch/riscv64/Makefile.inc

    rd59718e rcfdeedc  
    2727#
    2828
    29 BFD_NAME = elf64-littleriscv
    30 BFD_ARCH = riscv
    31 BFD = binary
    3229
    3330COMMON_CFLAGS += -mcmodel=medany
  • kernel/arch/riscv64/_link.ld.in

    rd59718e rcfdeedc  
    1616        kernel_load_address = PA2KA(BOOT_OFFSET);
    1717
    18         .image (PA2KA(BOOT_OFFSET)) : AT (0) {
     18        .image (PA2KA(BOOT_OFFSET) + SIZEOF_HEADERS) : AT (SIZEOF_HEADERS) {
    1919                ktext_start = .;
    2020                *(K_TEXT_START);
  • kernel/arch/sparc64/Makefile.inc

    rd59718e rcfdeedc  
    2727#
    2828
    29 BFD_NAME = elf64-sparc
    30 BFD_ARCH = sparc
    31 BFD = binary
    3229
    3330COMMON_CFLAGS += -m64 -mcpu=ultrasparc -mcmodel=medlow -mno-fpu
  • kernel/arch/sparc64/_link.ld.in

    rd59718e rcfdeedc  
    1212
    1313SECTIONS {
    14         .image VMA: AT (LMA) {
    15                 kernel_load_address = .;
     14        kernel_load_address = VMA;
    1615
     16        .image (VMA + SIZEOF_HEADERS): AT (LMA + SIZEOF_HEADERS) {
    1717                ktext_start = .;
    1818                *(K_TEXT_START)
Note: See TracChangeset for help on using the changeset viewer.