1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2778cb929SIan Campbell /* 3778cb929SIan Campbell * misc.c 4778cb929SIan Campbell * 5c0402881SKees Cook * This is a collection of several routines used to extract the kernel 6c0402881SKees Cook * which includes KASLR relocation, decompression, ELF parsing, and 7c0402881SKees Cook * relocation processing. Additionally included are the screen and serial 8c0402881SKees Cook * output functions and related debugging support functions. 9778cb929SIan Campbell * 10778cb929SIan Campbell * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994 11778cb929SIan Campbell * puts by Nick Holloway 1993, better puts by Martin Mares 1995 12778cb929SIan Campbell * High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996 13778cb929SIan Campbell */ 14778cb929SIan Campbell 158fee13a4SYinghai Lu #include "misc.h" 16dc425a6eSKees Cook #include "error.h" 173548e131SKirill A. Shutemov #include "pgtable.h" 18820e8fecSVivek Goyal #include "../string.h" 1967b66625SYinghai Lu #include "../voffset.h" 208c5477e8SZhenzhong Duan #include <asm/bootparam_utils.h> 21778cb929SIan Campbell 224252db10SBaoquan He /* 234252db10SBaoquan He * WARNING!! 244252db10SBaoquan He * This code is compiled with -fPIC and it is relocated dynamically at 254252db10SBaoquan He * run time, but no relocation processing is performed. This means that 264252db10SBaoquan He * it is not safe to place pointers in static structures. 27778cb929SIan Campbell */ 28778cb929SIan Campbell 291f208de3SKees Cook /* Macros used by the included decompressor code below. */ 30778cb929SIan Campbell #define STATIC static 3133f98a97SKees Cook /* Define an externally visible malloc()/free(). */ 3233f98a97SKees Cook #define MALLOC_VISIBLE 3333f98a97SKees Cook #include <linux/decompress/mm.h> 34778cb929SIan Campbell 3504999550SVivek Goyal /* 36394b19d6SArvind Sankar * Provide definitions of memzero and memmove as some of the decompressors will 37394b19d6SArvind Sankar * try to define their own functions if these are not defined as macros. 3804999550SVivek Goyal */ 39778cb929SIan Campbell #define memzero(s, n) memset((s), 0, (n)) 40938a000eSKees Cook #ifndef memmove 4181b785f3SKees Cook #define memmove memmove 421f208de3SKees Cook /* Functions used by the included decompressor code below. */ 4381b785f3SKees Cook void *memmove(void *dest, const void *src, size_t n); 44938a000eSKees Cook #endif 45778cb929SIan Campbell 46778cb929SIan Campbell /* 47778cb929SIan Campbell * This is set up by the setup-routine at boot-time 48778cb929SIan Campbell */ 49d55d5bc5SArd Biesheuvel struct boot_params *boot_params_ptr; 50778cb929SIan Campbell 51eb4ea1aeSKirill A. Shutemov struct port_io_ops pio_ops; 52eb4ea1aeSKirill A. Shutemov 5382fa9637SKees Cook memptr free_mem_ptr; 5482fa9637SKees Cook memptr free_mem_end_ptr; 55ac456ca0SNOMURA JUNICHI(野村 淳一) int spurious_nmi_count; 56778cb929SIan Campbell 5703056c88SAlexander van Heukelum static char *vidmem; 58778cb929SIan Campbell static int vidport; 596044d159SMichael Roth 606044d159SMichael Roth /* These might be accessed before .bss is cleared, so use .data instead. */ 616044d159SMichael Roth static int lines __section(".data"); 626044d159SMichael Roth static int cols __section(".data"); 63778cb929SIan Campbell 64ae03c499SAlain Knaff #ifdef CONFIG_KERNEL_GZIP 65ae03c499SAlain Knaff #include "../../../../lib/decompress_inflate.c" 66ae03c499SAlain Knaff #endif 67ae03c499SAlain Knaff 68ae03c499SAlain Knaff #ifdef CONFIG_KERNEL_BZIP2 69ae03c499SAlain Knaff #include "../../../../lib/decompress_bunzip2.c" 70ae03c499SAlain Knaff #endif 71ae03c499SAlain Knaff 72ae03c499SAlain Knaff #ifdef CONFIG_KERNEL_LZMA 73ae03c499SAlain Knaff #include "../../../../lib/decompress_unlzma.c" 74ae03c499SAlain Knaff #endif 75778cb929SIan Campbell 7630314804SLasse Collin #ifdef CONFIG_KERNEL_XZ 7730314804SLasse Collin #include "../../../../lib/decompress_unxz.c" 7830314804SLasse Collin #endif 7930314804SLasse Collin 8013510997SAlbin Tonnerre #ifdef CONFIG_KERNEL_LZO 8113510997SAlbin Tonnerre #include "../../../../lib/decompress_unlzo.c" 8213510997SAlbin Tonnerre #endif 8313510997SAlbin Tonnerre 84f9b493acSKyungsik Lee #ifdef CONFIG_KERNEL_LZ4 85f9b493acSKyungsik Lee #include "../../../../lib/decompress_unlz4.c" 86f9b493acSKyungsik Lee #endif 87fb46d057SNick Terrell 88fb46d057SNick Terrell #ifdef CONFIG_KERNEL_ZSTD 89fb46d057SNick Terrell #include "../../../../lib/decompress_unzstd.c" 90fb46d057SNick Terrell #endif 914252db10SBaoquan He /* 924252db10SBaoquan He * NOTE: When adding a new decompressor, please update the analysis in 934252db10SBaoquan He * ../header.S. 944252db10SBaoquan He */ 95f9b493acSKyungsik Lee 96778cb929SIan Campbell static void scroll(void) 97778cb929SIan Campbell { 98778cb929SIan Campbell int i; 99778cb929SIan Campbell 10081b785f3SKees Cook memmove(vidmem, vidmem + cols * 2, (lines - 1) * cols * 2); 101778cb929SIan Campbell for (i = (lines - 1) * cols * 2; i < lines * cols * 2; i += 2) 102778cb929SIan Campbell vidmem[i] = ' '; 103778cb929SIan Campbell } 104778cb929SIan Campbell 1058fee13a4SYinghai Lu #define XMTRDY 0x20 1068fee13a4SYinghai Lu 1078fee13a4SYinghai Lu #define TXR 0 /* Transmit register (WRITE) */ 1088fee13a4SYinghai Lu #define LSR 5 /* Line Status */ 1098fee13a4SYinghai Lu static void serial_putchar(int ch) 1108fee13a4SYinghai Lu { 1118fee13a4SYinghai Lu unsigned timeout = 0xffff; 1128fee13a4SYinghai Lu 1138fee13a4SYinghai Lu while ((inb(early_serial_base + LSR) & XMTRDY) == 0 && --timeout) 1148fee13a4SYinghai Lu cpu_relax(); 1158fee13a4SYinghai Lu 1168fee13a4SYinghai Lu outb(ch, early_serial_base + TXR); 1178fee13a4SYinghai Lu } 1188fee13a4SYinghai Lu 1197aac3015SJoe Millenbach void __putstr(const char *s) 120778cb929SIan Campbell { 121778cb929SIan Campbell int x, y, pos; 122778cb929SIan Campbell char c; 123778cb929SIan Campbell 1248fee13a4SYinghai Lu if (early_serial_base) { 1258fee13a4SYinghai Lu const char *str = s; 1268fee13a4SYinghai Lu while (*str) { 1278fee13a4SYinghai Lu if (*str == '\n') 1288fee13a4SYinghai Lu serial_putchar('\r'); 1298fee13a4SYinghai Lu serial_putchar(*str++); 1308fee13a4SYinghai Lu } 1318fee13a4SYinghai Lu } 1326bcb13b3SBen Collins 133fb1cc2f9SJan H. Schönherr if (lines == 0 || cols == 0) 134778cb929SIan Campbell return; 135778cb929SIan Campbell 136d55d5bc5SArd Biesheuvel x = boot_params_ptr->screen_info.orig_x; 137d55d5bc5SArd Biesheuvel y = boot_params_ptr->screen_info.orig_y; 138778cb929SIan Campbell 139778cb929SIan Campbell while ((c = *s++) != '\0') { 140778cb929SIan Campbell if (c == '\n') { 141778cb929SIan Campbell x = 0; 142778cb929SIan Campbell if (++y >= lines) { 143778cb929SIan Campbell scroll(); 144778cb929SIan Campbell y--; 145778cb929SIan Campbell } 146778cb929SIan Campbell } else { 147778cb929SIan Campbell vidmem[(x + cols * y) * 2] = c; 148778cb929SIan Campbell if (++x >= cols) { 149778cb929SIan Campbell x = 0; 150778cb929SIan Campbell if (++y >= lines) { 151778cb929SIan Campbell scroll(); 152778cb929SIan Campbell y--; 153778cb929SIan Campbell } 154778cb929SIan Campbell } 155778cb929SIan Campbell } 156778cb929SIan Campbell } 157778cb929SIan Campbell 158d55d5bc5SArd Biesheuvel boot_params_ptr->screen_info.orig_x = x; 159d55d5bc5SArd Biesheuvel boot_params_ptr->screen_info.orig_y = y; 160778cb929SIan Campbell 161778cb929SIan Campbell pos = (x + cols * y) * 2; /* Update cursor position */ 162778cb929SIan Campbell outb(14, vidport); 163778cb929SIan Campbell outb(0xff & (pos >> 9), vidport+1); 164778cb929SIan Campbell outb(15, vidport); 165778cb929SIan Campbell outb(0xff & (pos >> 1), vidport+1); 166778cb929SIan Campbell } 167778cb929SIan Campbell 1689ba8ec8eSH. Peter Anvin static noinline void __putnum(unsigned long value, unsigned int base, 1699ba8ec8eSH. Peter Anvin int mindig) 1709ba8ec8eSH. Peter Anvin { 1719ba8ec8eSH. Peter Anvin char buf[8*sizeof(value)+1]; 1729ba8ec8eSH. Peter Anvin char *p; 1739ba8ec8eSH. Peter Anvin 1749ba8ec8eSH. Peter Anvin p = buf + sizeof(buf); 1759ba8ec8eSH. Peter Anvin *--p = '\0'; 1769ba8ec8eSH. Peter Anvin 1779ba8ec8eSH. Peter Anvin while (mindig-- > 0 || value) { 1789ba8ec8eSH. Peter Anvin unsigned char digit = value % base; 1799ba8ec8eSH. Peter Anvin digit += (digit >= 10) ? ('a'-10) : '0'; 1809ba8ec8eSH. Peter Anvin *--p = digit; 1819ba8ec8eSH. Peter Anvin 1829ba8ec8eSH. Peter Anvin value /= base; 1839ba8ec8eSH. Peter Anvin } 1849ba8ec8eSH. Peter Anvin 1859ba8ec8eSH. Peter Anvin __putstr(p); 1869ba8ec8eSH. Peter Anvin } 1879ba8ec8eSH. Peter Anvin 18879063a7cSKees Cook void __puthex(unsigned long value) 18979063a7cSKees Cook { 1909ba8ec8eSH. Peter Anvin __putnum(value, 16, sizeof(value)*2); 19179063a7cSKees Cook } 1929ba8ec8eSH. Peter Anvin 1939ba8ec8eSH. Peter Anvin void __putdec(unsigned long value) 1949ba8ec8eSH. Peter Anvin { 1959ba8ec8eSH. Peter Anvin __putnum(value, 10, 1); 19679063a7cSKees Cook } 19779063a7cSKees Cook 198a554e740SNick Desaulniers #ifdef CONFIG_X86_NEED_RELOCS 1998391c73cSBaoquan He static void handle_relocations(void *output, unsigned long output_len, 2008391c73cSBaoquan He unsigned long virt_addr) 201a0215061SKees Cook { 202a0215061SKees Cook int *reloc; 203a0215061SKees Cook unsigned long delta, map, ptr; 204a0215061SKees Cook unsigned long min_addr = (unsigned long)output; 2054abf061bSYinghai Lu unsigned long max_addr = min_addr + (VO___bss_start - VO__text); 206a0215061SKees Cook 207a0215061SKees Cook /* 208a0215061SKees Cook * Calculate the delta between where vmlinux was linked to load 209a0215061SKees Cook * and where it was actually loaded. 210a0215061SKees Cook */ 211a0215061SKees Cook delta = min_addr - LOAD_PHYSICAL_ADDR; 212a0215061SKees Cook 213a0215061SKees Cook /* 214a0215061SKees Cook * The kernel contains a table of relocation addresses. Those 215a0215061SKees Cook * addresses have the final load address of the kernel in virtual 216a0215061SKees Cook * memory. We are currently working in the self map. So we need to 217a0215061SKees Cook * create an adjustment for kernel memory addresses to the self map. 218a0215061SKees Cook * This will involve subtracting out the base address of the kernel. 219a0215061SKees Cook */ 220a0215061SKees Cook map = delta - __START_KERNEL_map; 221a0215061SKees Cook 222a0215061SKees Cook /* 2238391c73cSBaoquan He * 32-bit always performs relocations. 64-bit relocations are only 2248391c73cSBaoquan He * needed if KASLR has chosen a different starting address offset 2258391c73cSBaoquan He * from __START_KERNEL_map. 2268391c73cSBaoquan He */ 2278391c73cSBaoquan He if (IS_ENABLED(CONFIG_X86_64)) 2288391c73cSBaoquan He delta = virt_addr - LOAD_PHYSICAL_ADDR; 2298391c73cSBaoquan He 2308391c73cSBaoquan He if (!delta) { 2318391c73cSBaoquan He debug_putstr("No relocation needed... "); 2328391c73cSBaoquan He return; 2338391c73cSBaoquan He } 2348391c73cSBaoquan He debug_putstr("Performing relocations... "); 2358391c73cSBaoquan He 2368391c73cSBaoquan He /* 237a0215061SKees Cook * Process relocations: 32 bit relocations first then 64 bit after. 2386d24c5f7SJan Beulich * Three sets of binary relocations are added to the end of the kernel 239a0215061SKees Cook * before compression. Each relocation table entry is the kernel 240a0215061SKees Cook * address of the location which needs to be updated stored as a 241a0215061SKees Cook * 32-bit value which is sign extended to 64 bits. 242a0215061SKees Cook * 243a0215061SKees Cook * Format is: 244a0215061SKees Cook * 245a0215061SKees Cook * kernel bits... 246a0215061SKees Cook * 0 - zero terminator for 64 bit relocations 247a0215061SKees Cook * 64 bit relocation repeated 2486d24c5f7SJan Beulich * 0 - zero terminator for inverse 32 bit relocations 2496d24c5f7SJan Beulich * 32 bit inverse relocation repeated 250a0215061SKees Cook * 0 - zero terminator for 32 bit relocations 251a0215061SKees Cook * 32 bit relocation repeated 252a0215061SKees Cook * 253a0215061SKees Cook * So we work backwards from the end of the decompressed image. 254a0215061SKees Cook */ 255a0215061SKees Cook for (reloc = output + output_len - sizeof(*reloc); *reloc; reloc--) { 2566f9af75fSBaoquan He long extended = *reloc; 257a0215061SKees Cook extended += map; 258a0215061SKees Cook 259a0215061SKees Cook ptr = (unsigned long)extended; 260a0215061SKees Cook if (ptr < min_addr || ptr > max_addr) 261a0215061SKees Cook error("32-bit relocation outside of kernel!\n"); 262a0215061SKees Cook 263a0215061SKees Cook *(uint32_t *)ptr += delta; 264a0215061SKees Cook } 265a0215061SKees Cook #ifdef CONFIG_X86_64 2666d24c5f7SJan Beulich while (*--reloc) { 2676d24c5f7SJan Beulich long extended = *reloc; 2686d24c5f7SJan Beulich extended += map; 2696d24c5f7SJan Beulich 2706d24c5f7SJan Beulich ptr = (unsigned long)extended; 2716d24c5f7SJan Beulich if (ptr < min_addr || ptr > max_addr) 2726d24c5f7SJan Beulich error("inverse 32-bit relocation outside of kernel!\n"); 2736d24c5f7SJan Beulich 2746d24c5f7SJan Beulich *(int32_t *)ptr -= delta; 2756d24c5f7SJan Beulich } 276a0215061SKees Cook for (reloc--; *reloc; reloc--) { 277a0215061SKees Cook long extended = *reloc; 278a0215061SKees Cook extended += map; 279a0215061SKees Cook 280a0215061SKees Cook ptr = (unsigned long)extended; 281a0215061SKees Cook if (ptr < min_addr || ptr > max_addr) 282a0215061SKees Cook error("64-bit relocation outside of kernel!\n"); 283a0215061SKees Cook 284a0215061SKees Cook *(uint64_t *)ptr += delta; 285a0215061SKees Cook } 286a0215061SKees Cook #endif 287a0215061SKees Cook } 288a0215061SKees Cook #else 2898391c73cSBaoquan He static inline void handle_relocations(void *output, unsigned long output_len, 2908391c73cSBaoquan He unsigned long virt_addr) 291a0215061SKees Cook { } 292a0215061SKees Cook #endif 293a0215061SKees Cook 2947734a0f3SAlexander Lobakin static size_t parse_elf(void *output) 295099e1377SIan Campbell { 296099e1377SIan Campbell #ifdef CONFIG_X86_64 297099e1377SIan Campbell Elf64_Ehdr ehdr; 298099e1377SIan Campbell Elf64_Phdr *phdrs, *phdr; 299099e1377SIan Campbell #else 300099e1377SIan Campbell Elf32_Ehdr ehdr; 301099e1377SIan Campbell Elf32_Phdr *phdrs, *phdr; 302099e1377SIan Campbell #endif 303099e1377SIan Campbell void *dest; 304099e1377SIan Campbell int i; 305099e1377SIan Campbell 306099e1377SIan Campbell memcpy(&ehdr, output, sizeof(ehdr)); 307099e1377SIan Campbell if (ehdr.e_ident[EI_MAG0] != ELFMAG0 || 308099e1377SIan Campbell ehdr.e_ident[EI_MAG1] != ELFMAG1 || 309099e1377SIan Campbell ehdr.e_ident[EI_MAG2] != ELFMAG2 || 3107734a0f3SAlexander Lobakin ehdr.e_ident[EI_MAG3] != ELFMAG3) 311099e1377SIan Campbell error("Kernel is not a valid ELF file"); 312099e1377SIan Campbell 313e605a425SJoe Millenbach debug_putstr("Parsing ELF... "); 314099e1377SIan Campbell 315099e1377SIan Campbell phdrs = malloc(sizeof(*phdrs) * ehdr.e_phnum); 316099e1377SIan Campbell if (!phdrs) 317099e1377SIan Campbell error("Failed to allocate space for phdrs"); 318099e1377SIan Campbell 319099e1377SIan Campbell memcpy(phdrs, output + ehdr.e_phoff, sizeof(*phdrs) * ehdr.e_phnum); 320099e1377SIan Campbell 321099e1377SIan Campbell for (i = 0; i < ehdr.e_phnum; i++) { 322099e1377SIan Campbell phdr = &phdrs[i]; 323099e1377SIan Campbell 324099e1377SIan Campbell switch (phdr->p_type) { 325099e1377SIan Campbell case PT_LOAD: 326c55b8550SH.J. Lu #ifdef CONFIG_X86_64 327c55b8550SH.J. Lu if ((phdr->p_align % 0x200000) != 0) 328c55b8550SH.J. Lu error("Alignment of LOAD segment isn't multiple of 2MB"); 329c55b8550SH.J. Lu #endif 330099e1377SIan Campbell #ifdef CONFIG_RELOCATABLE 331099e1377SIan Campbell dest = output; 332099e1377SIan Campbell dest += (phdr->p_paddr - LOAD_PHYSICAL_ADDR); 333099e1377SIan Campbell #else 334099e1377SIan Campbell dest = (void *)(phdr->p_paddr); 335099e1377SIan Campbell #endif 33681b785f3SKees Cook memmove(dest, output + phdr->p_offset, phdr->p_filesz); 337099e1377SIan Campbell break; 338099e1377SIan Campbell default: /* Ignore other PT_* */ break; 339099e1377SIan Campbell } 340099e1377SIan Campbell } 3415067cf53SJesper Juhl 3425067cf53SJesper Juhl free(phdrs); 3437734a0f3SAlexander Lobakin 3447734a0f3SAlexander Lobakin return ehdr.e_entry - LOAD_PHYSICAL_ADDR; 345099e1377SIan Campbell } 346099e1377SIan Campbell 347*9c554610SArd Biesheuvel const unsigned long kernel_text_size = VO___start_rodata - VO__text; 34883381519SArd Biesheuvel const unsigned long kernel_total_size = VO__end - VO__text; 34983381519SArd Biesheuvel 35024388292SArd Biesheuvel static u8 boot_heap[BOOT_HEAP_SIZE] __aligned(4); 35124388292SArd Biesheuvel 35224388292SArd Biesheuvel extern unsigned char input_data[]; 35324388292SArd Biesheuvel extern unsigned int input_len, output_len; 35424388292SArd Biesheuvel 35583381519SArd Biesheuvel unsigned long decompress_kernel(unsigned char *outbuf, unsigned long virt_addr, 35683381519SArd Biesheuvel void (*error)(char *x)) 35783381519SArd Biesheuvel { 35883381519SArd Biesheuvel unsigned long entry; 35983381519SArd Biesheuvel 36083381519SArd Biesheuvel if (!free_mem_ptr) { 36183381519SArd Biesheuvel free_mem_ptr = (unsigned long)boot_heap; 36283381519SArd Biesheuvel free_mem_end_ptr = (unsigned long)boot_heap + sizeof(boot_heap); 36383381519SArd Biesheuvel } 36483381519SArd Biesheuvel 36583381519SArd Biesheuvel if (__decompress(input_data, input_len, NULL, NULL, outbuf, output_len, 36683381519SArd Biesheuvel NULL, error) < 0) 36783381519SArd Biesheuvel return ULONG_MAX; 36883381519SArd Biesheuvel 36983381519SArd Biesheuvel entry = parse_elf(outbuf); 37083381519SArd Biesheuvel handle_relocations(outbuf, output_len, virt_addr); 37183381519SArd Biesheuvel 37283381519SArd Biesheuvel return entry; 37383381519SArd Biesheuvel } 37483381519SArd Biesheuvel 375974f221cSYinghai Lu /* 376cd0d9d92SArd Biesheuvel * Set the memory encryption xloadflag based on the mem_encrypt= command line 377cd0d9d92SArd Biesheuvel * parameter, if provided. 378cd0d9d92SArd Biesheuvel */ 379cd0d9d92SArd Biesheuvel static void parse_mem_encrypt(struct setup_header *hdr) 380cd0d9d92SArd Biesheuvel { 381cd0d9d92SArd Biesheuvel int on = cmdline_find_option_bool("mem_encrypt=on"); 382cd0d9d92SArd Biesheuvel int off = cmdline_find_option_bool("mem_encrypt=off"); 383cd0d9d92SArd Biesheuvel 384cd0d9d92SArd Biesheuvel if (on > off) 385cd0d9d92SArd Biesheuvel hdr->xloadflags |= XLF_MEM_ENCRYPTION; 386cd0d9d92SArd Biesheuvel } 387cd0d9d92SArd Biesheuvel 388cd0d9d92SArd Biesheuvel /* 389974f221cSYinghai Lu * The compressed kernel image (ZO), has been moved so that its position 390974f221cSYinghai Lu * is against the end of the buffer used to hold the uncompressed kernel 391974f221cSYinghai Lu * image (VO) and the execution environment (.bss, .brk), which makes sure 392974f221cSYinghai Lu * there is room to do the in-place decompression. (See header.S for the 393974f221cSYinghai Lu * calculations.) 394974f221cSYinghai Lu * 395974f221cSYinghai Lu * |-----compressed kernel image------| 396974f221cSYinghai Lu * V V 397974f221cSYinghai Lu * 0 extract_offset +INIT_SIZE 398974f221cSYinghai Lu * |-----------|---------------|-------------------------|--------| 399974f221cSYinghai Lu * | | | | 400974f221cSYinghai Lu * VO__text startup_32 of ZO VO__end ZO__end 401974f221cSYinghai Lu * ^ ^ 402974f221cSYinghai Lu * |-------uncompressed kernel image---------| 403974f221cSYinghai Lu * 404974f221cSYinghai Lu */ 40524388292SArd Biesheuvel asmlinkage __visible void *extract_kernel(void *rmode, unsigned char *output) 406778cb929SIan Campbell { 4078eabf42aSBaoquan He unsigned long virt_addr = LOAD_PHYSICAL_ADDR; 40824388292SArd Biesheuvel memptr heap = (memptr)boot_heap; 4091869dbe8SSteve Wahl unsigned long needed_size; 4107734a0f3SAlexander Lobakin size_t entry_offset; 411f285f4a2SKees Cook 4126655e0aaSKees Cook /* Retain x86 boot parameters pointer passed from startup_32/64. */ 413d55d5bc5SArd Biesheuvel boot_params_ptr = rmode; 414778cb929SIan Campbell 4156655e0aaSKees Cook /* Clear flags intended for solely in-kernel use. */ 416d55d5bc5SArd Biesheuvel boot_params_ptr->hdr.loadflags &= ~KASLR_FLAG; 41778cac48cSBorislav Petkov 418cd0d9d92SArd Biesheuvel parse_mem_encrypt(&boot_params_ptr->hdr); 419cd0d9d92SArd Biesheuvel 420d55d5bc5SArd Biesheuvel sanitize_boot_params(boot_params_ptr); 4215dcd14ecSH. Peter Anvin 422d55d5bc5SArd Biesheuvel if (boot_params_ptr->screen_info.orig_video_mode == 7) { 423778cb929SIan Campbell vidmem = (char *) 0xb0000; 424778cb929SIan Campbell vidport = 0x3b4; 425778cb929SIan Campbell } else { 426778cb929SIan Campbell vidmem = (char *) 0xb8000; 427778cb929SIan Campbell vidport = 0x3d4; 428778cb929SIan Campbell } 429778cb929SIan Campbell 430d55d5bc5SArd Biesheuvel lines = boot_params_ptr->screen_info.orig_video_lines; 431d55d5bc5SArd Biesheuvel cols = boot_params_ptr->screen_info.orig_video_cols; 432778cb929SIan Campbell 433eb4ea1aeSKirill A. Shutemov init_default_io_ops(); 434eb4ea1aeSKirill A. Shutemov 4354b05f815SKuppuswamy Sathyanarayanan /* 4364b05f815SKuppuswamy Sathyanarayanan * Detect TDX guest environment. 4374b05f815SKuppuswamy Sathyanarayanan * 4384b05f815SKuppuswamy Sathyanarayanan * It has to be done before console_init() in order to use 4394b05f815SKuppuswamy Sathyanarayanan * paravirtualized port I/O operations if needed. 4404b05f815SKuppuswamy Sathyanarayanan */ 4414b05f815SKuppuswamy Sathyanarayanan early_tdx_detect(); 4424b05f815SKuppuswamy Sathyanarayanan 4438fee13a4SYinghai Lu console_init(); 4445b51ae96SBorislav Petkov 4455b51ae96SBorislav Petkov /* 4465b51ae96SBorislav Petkov * Save RSDP address for later use. Have this after console_init() 4475b51ae96SBorislav Petkov * so that early debugging output from the RSDP parsing code can be 4485b51ae96SBorislav Petkov * collected. 4495b51ae96SBorislav Petkov */ 450d55d5bc5SArd Biesheuvel boot_params_ptr->acpi_rsdp_addr = get_rsdp_addr(); 4515b51ae96SBorislav Petkov 452c0402881SKees Cook debug_putstr("early console in extract_kernel\n"); 4538fee13a4SYinghai Lu 454778cb929SIan Campbell free_mem_ptr = heap; /* Heap */ 4557c539764SAlexander van Heukelum free_mem_end_ptr = heap + BOOT_HEAP_SIZE; 456778cb929SIan Campbell 4571869dbe8SSteve Wahl /* 4581869dbe8SSteve Wahl * The memory hole needed for the kernel is the larger of either 4591869dbe8SSteve Wahl * the entire decompressed kernel plus relocation table, or the 4601869dbe8SSteve Wahl * entire decompressed kernel plus .bss and .brk sections. 4611869dbe8SSteve Wahl * 4621869dbe8SSteve Wahl * On X86_64, the memory is mapped with PMD pages. Round the 4631869dbe8SSteve Wahl * size up so that the full extent of PMD pages mapped is 4641869dbe8SSteve Wahl * included in the check against the valid memory table 4651869dbe8SSteve Wahl * entries. This ensures the full mapped area is usable RAM 4661869dbe8SSteve Wahl * and doesn't include any reserved areas. 4671869dbe8SSteve Wahl */ 46824388292SArd Biesheuvel needed_size = max_t(unsigned long, output_len, kernel_total_size); 4691869dbe8SSteve Wahl #ifdef CONFIG_X86_64 4701869dbe8SSteve Wahl needed_size = ALIGN(needed_size, MIN_KERNEL_ALIGN); 4711869dbe8SSteve Wahl #endif 4721869dbe8SSteve Wahl 47379063a7cSKees Cook /* Report initial kernel position details. */ 47479063a7cSKees Cook debug_putaddr(input_data); 47579063a7cSKees Cook debug_putaddr(input_len); 47679063a7cSKees Cook debug_putaddr(output); 47779063a7cSKees Cook debug_putaddr(output_len); 4784d2d5424SYinghai Lu debug_putaddr(kernel_total_size); 4791869dbe8SSteve Wahl debug_putaddr(needed_size); 48079063a7cSKees Cook 4813548e131SKirill A. Shutemov #ifdef CONFIG_X86_64 4823548e131SKirill A. Shutemov /* Report address of 32-bit trampoline */ 4833548e131SKirill A. Shutemov debug_putaddr(trampoline_32bit); 4843548e131SKirill A. Shutemov #endif 4853548e131SKirill A. Shutemov 4868391c73cSBaoquan He choose_random_location((unsigned long)input_data, input_len, 4878391c73cSBaoquan He (unsigned long *)&output, 4881869dbe8SSteve Wahl needed_size, 4898391c73cSBaoquan He &virt_addr); 4908ab3820fSKees Cook 4918ab3820fSKees Cook /* Validate memory location choices. */ 4927ed42a28SH. Peter Anvin if ((unsigned long)output & (MIN_KERNEL_ALIGN - 1)) 4938391c73cSBaoquan He error("Destination physical address inappropriately aligned"); 4948391c73cSBaoquan He if (virt_addr & (MIN_KERNEL_ALIGN - 1)) 4958391c73cSBaoquan He error("Destination virtual address inappropriately aligned"); 496778cb929SIan Campbell #ifdef CONFIG_X86_64 4977ed42a28SH. Peter Anvin if (heap > 0x3fffffffffffUL) 498778cb929SIan Campbell error("Destination address too large"); 49924388292SArd Biesheuvel if (virt_addr + needed_size > KERNEL_IMAGE_SIZE) 500b892cb87SBaoquan He error("Destination virtual address is beyond the kernel mapping area"); 501778cb929SIan Campbell #else 502147dd561SH. Peter Anvin if (heap > ((-__PAGE_OFFSET-(128<<20)-1) & 0x7fffffff)) 503778cb929SIan Campbell error("Destination address too large"); 504778cb929SIan Campbell #endif 5057ed42a28SH. Peter Anvin #ifndef CONFIG_RELOCATABLE 5068eabf42aSBaoquan He if (virt_addr != LOAD_PHYSICAL_ADDR) 5078391c73cSBaoquan He error("Destination virtual address changed when not relocatable"); 508778cb929SIan Campbell #endif 509778cb929SIan Campbell 510e605a425SJoe Millenbach debug_putstr("\nDecompressing Linux... "); 5113fd1239aSKirill A. Shutemov 5123fd1239aSKirill A. Shutemov if (init_unaccepted_memory()) { 5133fd1239aSKirill A. Shutemov debug_putstr("Accepting memory... "); 5143fd1239aSKirill A. Shutemov accept_memory(__pa(output), __pa(output) + needed_size); 5153fd1239aSKirill A. Shutemov } 5163fd1239aSKirill A. Shutemov 51783381519SArd Biesheuvel entry_offset = decompress_kernel(output, virt_addr, error); 5187734a0f3SAlexander Lobakin 5197734a0f3SAlexander Lobakin debug_putstr("done.\nBooting the kernel (entry_offset: 0x"); 5207734a0f3SAlexander Lobakin debug_puthex(entry_offset); 5217734a0f3SAlexander Lobakin debug_putstr(").\n"); 522597cfe48SJoerg Roedel 523b099155eSJoerg Roedel /* Disable exception handling before booting the kernel */ 524b099155eSJoerg Roedel cleanup_exception_handling(); 525597cfe48SJoerg Roedel 526ac456ca0SNOMURA JUNICHI(野村 淳一) if (spurious_nmi_count) { 527ac456ca0SNOMURA JUNICHI(野村 淳一) error_putstr("Spurious early NMIs ignored: "); 528ac456ca0SNOMURA JUNICHI(野村 淳一) error_putdec(spurious_nmi_count); 529ac456ca0SNOMURA JUNICHI(野村 淳一) error_putstr("\n"); 530ac456ca0SNOMURA JUNICHI(野村 淳一) } 531ac456ca0SNOMURA JUNICHI(野村 淳一) 5327734a0f3SAlexander Lobakin return output + entry_offset; 533778cb929SIan Campbell } 534