18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Hibernation support for x86 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (c) 2007 Rafael J. Wysocki <rjw@sisk.pl> 68c2ecf20Sopenharmony_ci * Copyright (c) 2002 Pavel Machek <pavel@ucw.cz> 78c2ecf20Sopenharmony_ci * Copyright (c) 2001 Patrick Mochel <mochel@osdl.org> 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci#include <linux/gfp.h> 108c2ecf20Sopenharmony_ci#include <linux/smp.h> 118c2ecf20Sopenharmony_ci#include <linux/suspend.h> 128c2ecf20Sopenharmony_ci#include <linux/scatterlist.h> 138c2ecf20Sopenharmony_ci#include <linux/kdebug.h> 148c2ecf20Sopenharmony_ci#include <linux/cpu.h> 158c2ecf20Sopenharmony_ci#include <linux/pgtable.h> 168c2ecf20Sopenharmony_ci#include <linux/types.h> 178c2ecf20Sopenharmony_ci#include <linux/crc32.h> 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci#include <asm/e820/api.h> 208c2ecf20Sopenharmony_ci#include <asm/init.h> 218c2ecf20Sopenharmony_ci#include <asm/proto.h> 228c2ecf20Sopenharmony_ci#include <asm/page.h> 238c2ecf20Sopenharmony_ci#include <asm/mtrr.h> 248c2ecf20Sopenharmony_ci#include <asm/sections.h> 258c2ecf20Sopenharmony_ci#include <asm/suspend.h> 268c2ecf20Sopenharmony_ci#include <asm/tlbflush.h> 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci/* 298c2ecf20Sopenharmony_ci * Address to jump to in the last phase of restore in order to get to the image 308c2ecf20Sopenharmony_ci * kernel's text (this value is passed in the image header). 318c2ecf20Sopenharmony_ci */ 328c2ecf20Sopenharmony_ciunsigned long restore_jump_address __visible; 338c2ecf20Sopenharmony_ciunsigned long jump_address_phys; 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci/* 368c2ecf20Sopenharmony_ci * Value of the cr3 register from before the hibernation (this value is passed 378c2ecf20Sopenharmony_ci * in the image header). 388c2ecf20Sopenharmony_ci */ 398c2ecf20Sopenharmony_ciunsigned long restore_cr3 __visible; 408c2ecf20Sopenharmony_ciunsigned long temp_pgt __visible; 418c2ecf20Sopenharmony_ciunsigned long relocated_restore_code __visible; 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci/** 448c2ecf20Sopenharmony_ci * pfn_is_nosave - check if given pfn is in the 'nosave' section 458c2ecf20Sopenharmony_ci */ 468c2ecf20Sopenharmony_ciint pfn_is_nosave(unsigned long pfn) 478c2ecf20Sopenharmony_ci{ 488c2ecf20Sopenharmony_ci unsigned long nosave_begin_pfn; 498c2ecf20Sopenharmony_ci unsigned long nosave_end_pfn; 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci nosave_begin_pfn = __pa_symbol(&__nosave_begin) >> PAGE_SHIFT; 528c2ecf20Sopenharmony_ci nosave_end_pfn = PAGE_ALIGN(__pa_symbol(&__nosave_end)) >> PAGE_SHIFT; 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci return pfn >= nosave_begin_pfn && pfn < nosave_end_pfn; 558c2ecf20Sopenharmony_ci} 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_cistruct restore_data_record { 588c2ecf20Sopenharmony_ci unsigned long jump_address; 598c2ecf20Sopenharmony_ci unsigned long jump_address_phys; 608c2ecf20Sopenharmony_ci unsigned long cr3; 618c2ecf20Sopenharmony_ci unsigned long magic; 628c2ecf20Sopenharmony_ci unsigned long e820_checksum; 638c2ecf20Sopenharmony_ci}; 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci/** 668c2ecf20Sopenharmony_ci * compute_e820_crc32 - calculate crc32 of a given e820 table 678c2ecf20Sopenharmony_ci * 688c2ecf20Sopenharmony_ci * @table: the e820 table to be calculated 698c2ecf20Sopenharmony_ci * 708c2ecf20Sopenharmony_ci * Return: the resulting checksum 718c2ecf20Sopenharmony_ci */ 728c2ecf20Sopenharmony_cistatic inline u32 compute_e820_crc32(struct e820_table *table) 738c2ecf20Sopenharmony_ci{ 748c2ecf20Sopenharmony_ci int size = offsetof(struct e820_table, entries) + 758c2ecf20Sopenharmony_ci sizeof(struct e820_entry) * table->nr_entries; 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci return ~crc32_le(~0, (unsigned char const *)table, size); 788c2ecf20Sopenharmony_ci} 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_64 818c2ecf20Sopenharmony_ci#define RESTORE_MAGIC 0x23456789ABCDEF02UL 828c2ecf20Sopenharmony_ci#else 838c2ecf20Sopenharmony_ci#define RESTORE_MAGIC 0x12345679UL 848c2ecf20Sopenharmony_ci#endif 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci/** 878c2ecf20Sopenharmony_ci * arch_hibernation_header_save - populate the architecture specific part 888c2ecf20Sopenharmony_ci * of a hibernation image header 898c2ecf20Sopenharmony_ci * @addr: address to save the data at 908c2ecf20Sopenharmony_ci */ 918c2ecf20Sopenharmony_ciint arch_hibernation_header_save(void *addr, unsigned int max_size) 928c2ecf20Sopenharmony_ci{ 938c2ecf20Sopenharmony_ci struct restore_data_record *rdr = addr; 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci if (max_size < sizeof(struct restore_data_record)) 968c2ecf20Sopenharmony_ci return -EOVERFLOW; 978c2ecf20Sopenharmony_ci rdr->magic = RESTORE_MAGIC; 988c2ecf20Sopenharmony_ci rdr->jump_address = (unsigned long)restore_registers; 998c2ecf20Sopenharmony_ci rdr->jump_address_phys = __pa_symbol(restore_registers); 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci /* 1028c2ecf20Sopenharmony_ci * The restore code fixes up CR3 and CR4 in the following sequence: 1038c2ecf20Sopenharmony_ci * 1048c2ecf20Sopenharmony_ci * [in hibernation asm] 1058c2ecf20Sopenharmony_ci * 1. CR3 <= temporary page tables 1068c2ecf20Sopenharmony_ci * 2. CR4 <= mmu_cr4_features (from the kernel that restores us) 1078c2ecf20Sopenharmony_ci * 3. CR3 <= rdr->cr3 1088c2ecf20Sopenharmony_ci * 4. CR4 <= mmu_cr4_features (from us, i.e. the image kernel) 1098c2ecf20Sopenharmony_ci * [in restore_processor_state()] 1108c2ecf20Sopenharmony_ci * 5. CR4 <= saved CR4 1118c2ecf20Sopenharmony_ci * 6. CR3 <= saved CR3 1128c2ecf20Sopenharmony_ci * 1138c2ecf20Sopenharmony_ci * Our mmu_cr4_features has CR4.PCIDE=0, and toggling 1148c2ecf20Sopenharmony_ci * CR4.PCIDE while CR3's PCID bits are nonzero is illegal, so 1158c2ecf20Sopenharmony_ci * rdr->cr3 needs to point to valid page tables but must not 1168c2ecf20Sopenharmony_ci * have any of the PCID bits set. 1178c2ecf20Sopenharmony_ci */ 1188c2ecf20Sopenharmony_ci rdr->cr3 = restore_cr3 & ~CR3_PCID_MASK; 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci rdr->e820_checksum = compute_e820_crc32(e820_table_firmware); 1218c2ecf20Sopenharmony_ci return 0; 1228c2ecf20Sopenharmony_ci} 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci/** 1258c2ecf20Sopenharmony_ci * arch_hibernation_header_restore - read the architecture specific data 1268c2ecf20Sopenharmony_ci * from the hibernation image header 1278c2ecf20Sopenharmony_ci * @addr: address to read the data from 1288c2ecf20Sopenharmony_ci */ 1298c2ecf20Sopenharmony_ciint arch_hibernation_header_restore(void *addr) 1308c2ecf20Sopenharmony_ci{ 1318c2ecf20Sopenharmony_ci struct restore_data_record *rdr = addr; 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci if (rdr->magic != RESTORE_MAGIC) { 1348c2ecf20Sopenharmony_ci pr_crit("Unrecognized hibernate image header format!\n"); 1358c2ecf20Sopenharmony_ci return -EINVAL; 1368c2ecf20Sopenharmony_ci } 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci restore_jump_address = rdr->jump_address; 1398c2ecf20Sopenharmony_ci jump_address_phys = rdr->jump_address_phys; 1408c2ecf20Sopenharmony_ci restore_cr3 = rdr->cr3; 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci if (rdr->e820_checksum != compute_e820_crc32(e820_table_firmware)) { 1438c2ecf20Sopenharmony_ci pr_crit("Hibernate inconsistent memory map detected!\n"); 1448c2ecf20Sopenharmony_ci return -ENODEV; 1458c2ecf20Sopenharmony_ci } 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci return 0; 1488c2ecf20Sopenharmony_ci} 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ciint relocate_restore_code(void) 1518c2ecf20Sopenharmony_ci{ 1528c2ecf20Sopenharmony_ci pgd_t *pgd; 1538c2ecf20Sopenharmony_ci p4d_t *p4d; 1548c2ecf20Sopenharmony_ci pud_t *pud; 1558c2ecf20Sopenharmony_ci pmd_t *pmd; 1568c2ecf20Sopenharmony_ci pte_t *pte; 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci relocated_restore_code = get_safe_page(GFP_ATOMIC); 1598c2ecf20Sopenharmony_ci if (!relocated_restore_code) 1608c2ecf20Sopenharmony_ci return -ENOMEM; 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci memcpy((void *)relocated_restore_code, core_restore_code, PAGE_SIZE); 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci /* Make the page containing the relocated code executable */ 1658c2ecf20Sopenharmony_ci pgd = (pgd_t *)__va(read_cr3_pa()) + 1668c2ecf20Sopenharmony_ci pgd_index(relocated_restore_code); 1678c2ecf20Sopenharmony_ci p4d = p4d_offset(pgd, relocated_restore_code); 1688c2ecf20Sopenharmony_ci if (p4d_large(*p4d)) { 1698c2ecf20Sopenharmony_ci set_p4d(p4d, __p4d(p4d_val(*p4d) & ~_PAGE_NX)); 1708c2ecf20Sopenharmony_ci goto out; 1718c2ecf20Sopenharmony_ci } 1728c2ecf20Sopenharmony_ci pud = pud_offset(p4d, relocated_restore_code); 1738c2ecf20Sopenharmony_ci if (pud_large(*pud)) { 1748c2ecf20Sopenharmony_ci set_pud(pud, __pud(pud_val(*pud) & ~_PAGE_NX)); 1758c2ecf20Sopenharmony_ci goto out; 1768c2ecf20Sopenharmony_ci } 1778c2ecf20Sopenharmony_ci pmd = pmd_offset(pud, relocated_restore_code); 1788c2ecf20Sopenharmony_ci if (pmd_large(*pmd)) { 1798c2ecf20Sopenharmony_ci set_pmd(pmd, __pmd(pmd_val(*pmd) & ~_PAGE_NX)); 1808c2ecf20Sopenharmony_ci goto out; 1818c2ecf20Sopenharmony_ci } 1828c2ecf20Sopenharmony_ci pte = pte_offset_kernel(pmd, relocated_restore_code); 1838c2ecf20Sopenharmony_ci set_pte(pte, __pte(pte_val(*pte) & ~_PAGE_NX)); 1848c2ecf20Sopenharmony_ciout: 1858c2ecf20Sopenharmony_ci __flush_tlb_all(); 1868c2ecf20Sopenharmony_ci return 0; 1878c2ecf20Sopenharmony_ci} 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ciint arch_resume_nosmt(void) 1908c2ecf20Sopenharmony_ci{ 1918c2ecf20Sopenharmony_ci int ret = 0; 1928c2ecf20Sopenharmony_ci /* 1938c2ecf20Sopenharmony_ci * We reached this while coming out of hibernation. This means 1948c2ecf20Sopenharmony_ci * that SMT siblings are sleeping in hlt, as mwait is not safe 1958c2ecf20Sopenharmony_ci * against control transition during resume (see comment in 1968c2ecf20Sopenharmony_ci * hibernate_resume_nonboot_cpu_disable()). 1978c2ecf20Sopenharmony_ci * 1988c2ecf20Sopenharmony_ci * If the resumed kernel has SMT disabled, we have to take all the 1998c2ecf20Sopenharmony_ci * SMT siblings out of hlt, and offline them again so that they 2008c2ecf20Sopenharmony_ci * end up in mwait proper. 2018c2ecf20Sopenharmony_ci * 2028c2ecf20Sopenharmony_ci * Called with hotplug disabled. 2038c2ecf20Sopenharmony_ci */ 2048c2ecf20Sopenharmony_ci cpu_hotplug_enable(); 2058c2ecf20Sopenharmony_ci if (cpu_smt_control == CPU_SMT_DISABLED || 2068c2ecf20Sopenharmony_ci cpu_smt_control == CPU_SMT_FORCE_DISABLED) { 2078c2ecf20Sopenharmony_ci enum cpuhp_smt_control old = cpu_smt_control; 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_ci ret = cpuhp_smt_enable(); 2108c2ecf20Sopenharmony_ci if (ret) 2118c2ecf20Sopenharmony_ci goto out; 2128c2ecf20Sopenharmony_ci ret = cpuhp_smt_disable(old); 2138c2ecf20Sopenharmony_ci if (ret) 2148c2ecf20Sopenharmony_ci goto out; 2158c2ecf20Sopenharmony_ci } 2168c2ecf20Sopenharmony_ciout: 2178c2ecf20Sopenharmony_ci cpu_hotplug_disable(); 2188c2ecf20Sopenharmony_ci return ret; 2198c2ecf20Sopenharmony_ci} 220