Arthur Heymans has uploaded this change for review. ( https://review.coreboot.org/c/coreboot/+/63716 )
Change subject: [WIP]util/cbfstool: Rewrite trampoline in C ......................................................................
[WIP]util/cbfstool: Rewrite trampoline in C
Rewrite the trampoline in C code. Now it does use stack, but it's easier to read.
Works on QEMU.
Change-Id: Ibbd7a5ecd225edf87f451a82ff4cbe9fea522a89 Signed-off-by: Arthur Heymans arthur@aheymans.xyz --- M util/cbfstool/Makefile D util/cbfstool/linux_trampoline.S M util/cbfstool/linux_trampoline.c A util/cbfstool/x86_linux_trampoline.c 4 files changed, 154 insertions(+), 165 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/16/63716/1
diff --git a/util/cbfstool/Makefile b/util/cbfstool/Makefile index 034aca2..f18f818 100644 --- a/util/cbfstool/Makefile +++ b/util/cbfstool/Makefile @@ -49,10 +49,10 @@ $(RM) -f $(objutil)/cbfstool/cse_serger $(cse_serger_obj) $(RM) -rf $(VBOOT_HOST_BUILD)
-linux_trampoline.c: linux_trampoline.S - rm -f linux_trampoline.c - $(CC) -m32 -o linux_trampoline linux_trampoline.S -ffreestanding -nostdlib -nostdinc -Wl,--defsym=_start=0 - $(OBJCOPY) -Obinary -j .data linux_trampoline trampoline +linux_trampoline.c: x86_linux_trampoline.c + rm -f $@ + $(CC) -I../../src/include -I../../src/commonlib/include -I../../src/commonlib/bsd/include -m32 -o linux_trampoline $< -Os -ffreestanding -nostdlib -nostdinc -fPIC -Wl,--defsym=_start=0 + $(OBJCOPY) -Obinary -j .text linux_trampoline trampoline echo "/* This file is automatically generated. Do not manually change */" > trampoline.c echo "/* SPDX-License-Identifier: GPL-2.0-only */" >> trampoline.c xxd -c 16 -i trampoline >> trampoline.c diff --git a/util/cbfstool/linux_trampoline.S b/util/cbfstool/linux_trampoline.S deleted file mode 100644 index 767c8be..0000000 --- a/util/cbfstool/linux_trampoline.S +++ /dev/null @@ -1,145 +0,0 @@ -/* linux_trampoline */ -/* SPDX-License-Identifier: GPL-2.0-only */ - -/* NOTE: THIS CODE MUST REMAIN POSITION INDEPENDENT - * IT SHOULDN'T USE THE STACK - * AND IN GENERAL EXPECT NOTHING BUT RAM TO WORK - */ -.code32 -.data - -#include "linux_trampoline.h" -#define HEADER_SIG 0x4f49424c // LBIO little endian -#define CB_TAG_FORWARD 0x11 -#define CB_TAG_MEMORY 0x1 -#define CB_TAG_FRAMEBUFFER 0x12 -#define CB_TAG_ACPI_RSDP 0x43 - -#define ACPI_RSDP_ADDR 0x70 -#define E820_NR_OFFSET 0x1e8 -#define LINUX_ENTRY_OFFSET 0x214 -#define E820_OFFSET 0x2d0 - -.trampoline_start: -cld -xor %edx, %edx -mov $0, %ecx - -.headerSearch: -mov $0x10000, %ebx -add %ecx, %ebx -mov (%ecx), %eax -cmp $HEADER_SIG, %eax -je .headerSearchDone // found the header -add $16, %ecx -cmp %ecx, %ebx -jne .headerSearch - -.headerSearchDone: -cmp %ecx, %ebx // reached the end == not found anything? -je 2f // give up - -// we assume the checksum is okay, no test -mov 4(%ecx), %ebx -add %ecx, %ebx // ebx = cb_header + header_bytes -mov 20(%ecx), %ecx // ecx = table_entries - -.tableScan: -cmp $CB_TAG_FORWARD, (%ebx) -jne .testMemory - -/* forward tag: assume 32bit pointer */ -mov 8(%ebx), %ecx -jmp .headerSearch - -.testMemory: -cmp $CB_TAG_MEMORY, (%ebx) -jne .testAcpiRsdp - -/* memory tag: copy e820 map and entry count. also determine alt_mem_k */ -mov 4(%ebx), %eax -sub $8, %eax -shr $2, %eax /* eax = number of dwords of e820 data */ -cmp $(32 * 5), %eax /* linux wants at most 32 entries of 5 dwords */ -jng 1f -mov $(32 * 5), %eax /* only copy 32 entries */ -1: -mov %eax, %esi -mov $5, %edi -div %edi -mov %eax, (LINUX_PARAM_LOC + E820_NR_OFFSET) -mov %esi, %eax -xchg %eax, %ecx -lea 8(%ebx), %esi /* e820 data source */ -mov $(LINUX_PARAM_LOC + E820_OFFSET), %edi -rep movsl -xchg %eax, %ecx -/* e820 and LB_TAG_MEMORY type don't fully match: remap unknown type to 2, reserved memory */ -mov (LINUX_PARAM_LOC + E820_NR_OFFSET), %eax -mov $(LINUX_PARAM_LOC + E820_OFFSET), %edi -.test_e820_entry: -cmp $0, %eax -je .endScan -cmp $12, 16(%edi) /* type */ -jng .next_e820_entry -/* Fixup the type to 2, reserved memory */ -mov $2, 16(%edi) -.next_e820_entry: -dec %eax -add $20, %edi -jmp .test_e820_entry - -.testAcpiRsdp: -cmp $CB_TAG_ACPI_RSDP, (%ebx) -jne .testFramebuffer - -mov 8(%ebx), %eax -mov %eax, (LINUX_PARAM_LOC + ACPI_RSDP_ADDR) -mov 12(%ebx), %eax -mov %eax, (LINUX_PARAM_LOC + ACPI_RSDP_ADDR + 4) -jmp .endScan - -.testFramebuffer: -cmp $CB_TAG_FRAMEBUFFER, (%ebx) -jne .endScan -/* TODO: handle framebuffer tag */ - -.endScan: -add 4(%ebx), %ebx -dec %ecx -jnz .tableScan - -/* Setup basic code and data segment selectors for Linux -** -** Flat code segment descriptor: -** selector: 0x10 -** base : 0x00000000 -** limit : 0xFFFFFFFF -** type : code, execute, read -** -** Flat data segment descriptor: -** selector: 0x18 -** base : 0x00000000 -** limit : 0xFFFFFFFF -** type : data, read/write -** -** Use TRAMPOLINE_ENTRY_LOC as a scratchpad. -*/ -mov $TRAMPOLINE_ENTRY_LOC, %eax -movl $0x0000ffff, 16(%eax) // Set up the 2 new descriptors -movl $0x00cf9b00, 20(%eax) -movl $0x0000ffff, 24(%eax) -movl $0x00cf9300, 28(%eax) -movb $0x2b, 0(%eax) // Set the size -movl %eax, 2(%eax) // Set pointer to new GDT -lgdt (%eax) // Load it - -/* finally: jump to kernel */ -mov $LINUX_PARAM_LOC, %esi -jmp *(LINUX_PARAM_LOC + LINUX_ENTRY_OFFSET) - - -2: -hlt -jmp 2b -.trampoline_end: diff --git a/util/cbfstool/linux_trampoline.c b/util/cbfstool/linux_trampoline.c index 46bb800..c5c2955 100644 --- a/util/cbfstool/linux_trampoline.c +++ b/util/cbfstool/linux_trampoline.c @@ -1,20 +1,26 @@ /* This file is automatically generated. Do not manually change */ /* SPDX-License-Identifier: GPL-2.0-only */ unsigned char trampoline[] = { - 0xfc, 0x31, 0xd2, 0xb9, 0x00, 0x00, 0x00, 0x00, 0xbb, 0x00, 0x00, 0x01, 0x00, 0x01, 0xcb, 0x8b, - 0x01, 0x3d, 0x4c, 0x42, 0x49, 0x4f, 0x74, 0x07, 0x83, 0xc1, 0x10, 0x39, 0xcb, 0x75, 0xe9, 0x39, - 0xcb, 0x0f, 0x84, 0xc5, 0x00, 0x00, 0x00, 0x8b, 0x59, 0x04, 0x01, 0xcb, 0x8b, 0x49, 0x14, 0x83, - 0x3b, 0x11, 0x75, 0x05, 0x8b, 0x4b, 0x08, 0xeb, 0xcf, 0x83, 0x3b, 0x01, 0x75, 0x53, 0x8b, 0x43, - 0x04, 0x83, 0xe8, 0x08, 0xc1, 0xe8, 0x02, 0x3d, 0xa0, 0x00, 0x00, 0x00, 0x7e, 0x05, 0xb8, 0xa0, - 0x00, 0x00, 0x00, 0x89, 0xc6, 0xbf, 0x05, 0x00, 0x00, 0x00, 0xf7, 0xf7, 0xa3, 0xe8, 0x01, 0x09, - 0x00, 0x89, 0xf0, 0x91, 0x8d, 0x73, 0x08, 0xbf, 0xd0, 0x02, 0x09, 0x00, 0xf3, 0xa5, 0x91, 0xa1, - 0xe8, 0x01, 0x09, 0x00, 0xbf, 0xd0, 0x02, 0x09, 0x00, 0x83, 0xf8, 0x00, 0x74, 0x2f, 0x83, 0x7f, - 0x10, 0x0c, 0x7e, 0x07, 0xc7, 0x47, 0x10, 0x02, 0x00, 0x00, 0x00, 0x48, 0x83, 0xc7, 0x14, 0xeb, - 0xe8, 0x83, 0x3b, 0x43, 0x75, 0x12, 0x8b, 0x43, 0x08, 0xa3, 0x70, 0x00, 0x09, 0x00, 0x8b, 0x43, - 0x0c, 0xa3, 0x74, 0x00, 0x09, 0x00, 0xeb, 0x05, 0x83, 0x3b, 0x12, 0x75, 0x00, 0x03, 0x5b, 0x04, - 0x49, 0x0f, 0x85, 0x78, 0xff, 0xff, 0xff, 0xb8, 0x00, 0x00, 0x04, 0x00, 0xc7, 0x40, 0x10, 0xff, - 0xff, 0x00, 0x00, 0xc7, 0x40, 0x14, 0x00, 0x9b, 0xcf, 0x00, 0xc7, 0x40, 0x18, 0xff, 0xff, 0x00, - 0x00, 0xc7, 0x40, 0x1c, 0x00, 0x93, 0xcf, 0x00, 0xc6, 0x00, 0x2b, 0x89, 0x40, 0x02, 0x0f, 0x01, - 0x10, 0xbe, 0x00, 0x00, 0x09, 0x00, 0xff, 0x25, 0x14, 0x02, 0x09, 0x00, 0xf4, 0xeb, 0xfd + 0x55, 0x89, 0xe5, 0x57, 0x56, 0x53, 0x83, 0xe4, 0xf8, 0x83, 0xec, 0x28, 0x8b, 0x5d, 0x08, 0xfc, + 0x80, 0x3b, 0x4c, 0x74, 0x03, 0xf4, 0xeb, 0xfd, 0x80, 0x7b, 0x01, 0x42, 0x74, 0x03, 0xf4, 0xeb, + 0xfd, 0x80, 0x7b, 0x02, 0x49, 0x74, 0x03, 0xf4, 0xeb, 0xfd, 0x80, 0x7b, 0x03, 0x4f, 0x74, 0x03, + 0xf4, 0xeb, 0xfd, 0x8b, 0x4b, 0x04, 0xc7, 0x44, 0x24, 0x04, 0x00, 0x00, 0x00, 0x00, 0x01, 0xd9, + 0x8b, 0x44, 0x24, 0x04, 0x39, 0x43, 0x14, 0x0f, 0x86, 0x9a, 0x00, 0x00, 0x00, 0x8b, 0x11, 0x8b, + 0x41, 0x04, 0x83, 0xfa, 0x01, 0x74, 0x18, 0x83, 0xfa, 0x43, 0x75, 0x7f, 0x8b, 0x41, 0x08, 0x8b, + 0x51, 0x0c, 0xa3, 0x70, 0x00, 0x09, 0x00, 0x89, 0x15, 0x74, 0x00, 0x09, 0x00, 0xeb, 0x6c, 0x83, + 0xe8, 0x08, 0x3d, 0x93, 0x02, 0x00, 0x00, 0x76, 0x03, 0xf4, 0xeb, 0xfd, 0xbf, 0x14, 0x00, 0x00, + 0x00, 0x31, 0xd2, 0xf7, 0xf7, 0xba, 0xd0, 0x02, 0x09, 0x00, 0xa3, 0xe8, 0x01, 0x09, 0x00, 0x6b, + 0xc0, 0x14, 0x05, 0xd0, 0x02, 0x09, 0x00, 0x39, 0xd0, 0x74, 0x40, 0x8b, 0xb4, 0x11, 0x38, 0xfd, + 0xf6, 0xff, 0x8b, 0xbc, 0x11, 0x3c, 0xfd, 0xf6, 0xff, 0x89, 0x32, 0x89, 0x7a, 0x04, 0x8b, 0xbc, + 0x11, 0x44, 0xfd, 0xf6, 0xff, 0x8b, 0xb4, 0x11, 0x40, 0xfd, 0xf6, 0xff, 0x89, 0x7a, 0x0c, 0x8b, + 0xbc, 0x11, 0x48, 0xfd, 0xf6, 0xff, 0x89, 0x72, 0x08, 0x83, 0xff, 0x0b, 0x76, 0x05, 0xbf, 0x02, + 0x00, 0x00, 0x00, 0x89, 0x7a, 0x10, 0x83, 0xc2, 0x14, 0xeb, 0xbc, 0xff, 0x44, 0x24, 0x04, 0x03, + 0x49, 0x04, 0xe9, 0x59, 0xff, 0xff, 0xff, 0x8d, 0x44, 0x24, 0x08, 0xc7, 0x44, 0x24, 0x10, 0x00, + 0x00, 0x00, 0x00, 0x89, 0xc2, 0xc1, 0xe8, 0x10, 0xc7, 0x44, 0x24, 0x14, 0x00, 0x00, 0x00, 0x00, + 0xc1, 0xe2, 0x10, 0xc7, 0x44, 0x24, 0x18, 0xff, 0xff, 0x00, 0x00, 0x83, 0xca, 0x20, 0xc7, 0x44, + 0x24, 0x1c, 0x00, 0x9b, 0xcf, 0x00, 0xc7, 0x44, 0x24, 0x20, 0xff, 0xff, 0x00, 0x00, 0xc7, 0x44, + 0x24, 0x24, 0x00, 0x93, 0xcf, 0x00, 0x89, 0x54, 0x24, 0x08, 0x89, 0x44, 0x24, 0x0c, 0x0f, 0x01, + 0x54, 0x24, 0x08, 0x31, 0xdb, 0xa1, 0x14, 0x02, 0x09, 0x00, 0xbe, 0x00, 0x00, 0x09, 0x00, 0x89, + 0xdf, 0xbd, 0x00, 0x00, 0x00, 0x00, 0xfa, 0xff, 0xe0, 0xf4, 0xeb, 0xfd }; -unsigned int trampoline_len = 239; +unsigned int trampoline_len = 332; diff --git a/util/cbfstool/x86_linux_trampoline.c b/util/cbfstool/x86_linux_trampoline.c new file mode 100644 index 0000000..c4eb563 --- /dev/null +++ b/util/cbfstool/x86_linux_trampoline.c @@ -0,0 +1,128 @@ +/* linux_trampoline */ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include <types.h> +#include <stdint.h> +#include <commonlib/bsd/compiler.h> +#include <commonlib/coreboot_tables.h> + +#include "linux_trampoline.h" + +#define ACPI_RSDP_ADDR 0x70 +#define E820_NR_OFFSET 0x1e8 +#define LINUX_ENTRY_OFFSET 0x214 +#define E820_OFFSET 0x2d0 + +static __noreturn __always_inline void hlt(void) +{ + while (1) + asm("hlt"); +} + +static inline struct lb_record *get_next_entry(const struct lb_record *prev) +{ + return (void *)((uintptr_t)prev + prev->size); +} + +#define MEM_RANGE_COUNT(_rec) (((_rec)->size - sizeof(*(_rec))) / sizeof((_rec)->map[0])) + +struct e820entry { + uint64_t start; + uint64_t size; + uint32_t type; +} __packed; + +static inline void fill_linux_e820(const struct lb_memory *lb_mem) +{ + const size_t count = MEM_RANGE_COUNT(lb_mem); + /* linux wants at most 32 entries of 5 dwords */ + if (count > 32) + hlt(); + + volatile uint32_t *nr_e820 = (void *)(LINUX_PARAM_LOC + E820_NR_OFFSET); + *nr_e820 = count; + + struct e820entry *e820_entries = (void *)(LINUX_PARAM_LOC + E820_OFFSET); + + for (int i = 0; i < count; i++) { + e820_entries[i].start = lb_mem->map[i].start; + e820_entries[i].size = lb_mem->map[i].size; + /* e820 and LB_TAG_MEMORY type don't fully match: remap unknown type to 2, + * reserved memory */ + e820_entries[i].type = lb_mem->map[i].type < 12 ? lb_mem->map[i].type : 2; + } +} + +static inline void fill_linux_rsdp(const struct lb_acpi_rsdp *rsdp) +{ + volatile uint64_t *linux_rsdp = (void *)(LINUX_PARAM_LOC + ACPI_RSDP_ADDR); + *linux_rsdp = rsdp->rsdp_pointer; +} + +/* Setup basic code and data segment selectors for Linux +** +** Flat code segment descriptor: +** selector: 0x10 +** base : 0x00000000 +** limit : 0xFFFFFFFF +** type : code, execute, read +** +** Flat data segment descriptor: +** selector: 0x18 +** base : 0x00000000 +** limit : 0xFFFFFFFF +** type : data, read/write +*/ + +static __noreturn void jump_to_kernel(void) +{ + __aligned(8) uint32_t gdt[8]; + gdt[1] = 0; + gdt[2] = 0; + gdt[3] = 0; + gdt[4] = 0x0000ffff; + gdt[5] = 0x00cf9b00; + gdt[6] = 0x0000ffff; + gdt[7] = 0x00cf9300; + gdt[0] = 0x20 | (((uintptr_t)gdt) << 16); + gdt[1] = (uintptr_t)gdt >> 16; + __asm__ __volatile__("lgdt %0" ::"m"(gdt)); + __asm__ __volatile__( + "movl $0, %%ebp \n" + "cli \n" + "jmp *%[kernel_entry] \n" + : + : [kernel_entry] "a"(*(uintptr_t *)(LINUX_PARAM_LOC + LINUX_ENTRY_OFFSET)), + [boot_params] "S"(LINUX_PARAM_LOC), "b"(0), "D"(0)); + + hlt(); +} + +void __noreturn main(uint32_t coreboot_table) +{ + __asm__ ("cld"); + const struct lb_header *header = (void *)coreboot_table; + if (header->signature[0] != 'L') + hlt(); + if (header->signature[1] != 'B') + hlt(); + if (header->signature[2] != 'I') + hlt(); + if (header->signature[3] != 'O') + hlt(); + + const struct lb_record *entry = (void *)(coreboot_table + header->header_bytes); + for (size_t i = 0; i < header->table_entries; i++, entry = get_next_entry(entry)) { + switch (entry->tag) { + case LB_TAG_MEMORY: + fill_linux_e820((void *)entry); + break; + case LB_TAG_ACPI_RSDP: + fill_linux_rsdp((void *)entry); + break; + } + } + + jump_to_kernel(); + hlt(); +}