18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 28c2ecf20Sopenharmony_ci#ifndef _ASM_X86_SET_MEMORY_H 38c2ecf20Sopenharmony_ci#define _ASM_X86_SET_MEMORY_H 48c2ecf20Sopenharmony_ci 58c2ecf20Sopenharmony_ci#include <asm/page.h> 68c2ecf20Sopenharmony_ci#include <asm-generic/set_memory.h> 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci/* 98c2ecf20Sopenharmony_ci * The set_memory_* API can be used to change various attributes of a virtual 108c2ecf20Sopenharmony_ci * address range. The attributes include: 118c2ecf20Sopenharmony_ci * Cachability : UnCached, WriteCombining, WriteThrough, WriteBack 128c2ecf20Sopenharmony_ci * Executability : eXeutable, NoteXecutable 138c2ecf20Sopenharmony_ci * Read/Write : ReadOnly, ReadWrite 148c2ecf20Sopenharmony_ci * Presence : NotPresent 158c2ecf20Sopenharmony_ci * Encryption : Encrypted, Decrypted 168c2ecf20Sopenharmony_ci * 178c2ecf20Sopenharmony_ci * Within a category, the attributes are mutually exclusive. 188c2ecf20Sopenharmony_ci * 198c2ecf20Sopenharmony_ci * The implementation of this API will take care of various aspects that 208c2ecf20Sopenharmony_ci * are associated with changing such attributes, such as: 218c2ecf20Sopenharmony_ci * - Flushing TLBs 228c2ecf20Sopenharmony_ci * - Flushing CPU caches 238c2ecf20Sopenharmony_ci * - Making sure aliases of the memory behind the mapping don't violate 248c2ecf20Sopenharmony_ci * coherency rules as defined by the CPU in the system. 258c2ecf20Sopenharmony_ci * 268c2ecf20Sopenharmony_ci * What this API does not do: 278c2ecf20Sopenharmony_ci * - Provide exclusion between various callers - including callers that 288c2ecf20Sopenharmony_ci * operation on other mappings of the same physical page 298c2ecf20Sopenharmony_ci * - Restore default attributes when a page is freed 308c2ecf20Sopenharmony_ci * - Guarantee that mappings other than the requested one are 318c2ecf20Sopenharmony_ci * in any state, other than that these do not violate rules for 328c2ecf20Sopenharmony_ci * the CPU you have. Do not depend on any effects on other mappings, 338c2ecf20Sopenharmony_ci * CPUs other than the one you have may have more relaxed rules. 348c2ecf20Sopenharmony_ci * The caller is required to take care of these. 358c2ecf20Sopenharmony_ci */ 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ciint __set_memory_prot(unsigned long addr, int numpages, pgprot_t prot); 388c2ecf20Sopenharmony_ciint _set_memory_uc(unsigned long addr, int numpages); 398c2ecf20Sopenharmony_ciint _set_memory_wc(unsigned long addr, int numpages); 408c2ecf20Sopenharmony_ciint _set_memory_wt(unsigned long addr, int numpages); 418c2ecf20Sopenharmony_ciint _set_memory_wb(unsigned long addr, int numpages); 428c2ecf20Sopenharmony_ciint set_memory_uc(unsigned long addr, int numpages); 438c2ecf20Sopenharmony_ciint set_memory_wc(unsigned long addr, int numpages); 448c2ecf20Sopenharmony_ciint set_memory_wb(unsigned long addr, int numpages); 458c2ecf20Sopenharmony_ciint set_memory_np(unsigned long addr, int numpages); 468c2ecf20Sopenharmony_ciint set_memory_4k(unsigned long addr, int numpages); 478c2ecf20Sopenharmony_ciint set_memory_encrypted(unsigned long addr, int numpages); 488c2ecf20Sopenharmony_ciint set_memory_decrypted(unsigned long addr, int numpages); 498c2ecf20Sopenharmony_ciint set_memory_np_noalias(unsigned long addr, int numpages); 508c2ecf20Sopenharmony_ciint set_memory_nonglobal(unsigned long addr, int numpages); 518c2ecf20Sopenharmony_ciint set_memory_global(unsigned long addr, int numpages); 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ciint set_pages_array_uc(struct page **pages, int addrinarray); 548c2ecf20Sopenharmony_ciint set_pages_array_wc(struct page **pages, int addrinarray); 558c2ecf20Sopenharmony_ciint set_pages_array_wt(struct page **pages, int addrinarray); 568c2ecf20Sopenharmony_ciint set_pages_array_wb(struct page **pages, int addrinarray); 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci/* 598c2ecf20Sopenharmony_ci * For legacy compatibility with the old APIs, a few functions 608c2ecf20Sopenharmony_ci * are provided that work on a "struct page". 618c2ecf20Sopenharmony_ci * These functions operate ONLY on the 1:1 kernel mapping of the 628c2ecf20Sopenharmony_ci * memory that the struct page represents, and internally just 638c2ecf20Sopenharmony_ci * call the set_memory_* function. See the description of the 648c2ecf20Sopenharmony_ci * set_memory_* function for more details on conventions. 658c2ecf20Sopenharmony_ci * 668c2ecf20Sopenharmony_ci * These APIs should be considered *deprecated* and are likely going to 678c2ecf20Sopenharmony_ci * be removed in the future. 688c2ecf20Sopenharmony_ci * The reason for this is the implicit operation on the 1:1 mapping only, 698c2ecf20Sopenharmony_ci * making this not a generally useful API. 708c2ecf20Sopenharmony_ci * 718c2ecf20Sopenharmony_ci * Specifically, many users of the old APIs had a virtual address, 728c2ecf20Sopenharmony_ci * called virt_to_page() or vmalloc_to_page() on that address to 738c2ecf20Sopenharmony_ci * get a struct page* that the old API required. 748c2ecf20Sopenharmony_ci * To convert these cases, use set_memory_*() on the original 758c2ecf20Sopenharmony_ci * virtual address, do not use these functions. 768c2ecf20Sopenharmony_ci */ 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ciint set_pages_uc(struct page *page, int numpages); 798c2ecf20Sopenharmony_ciint set_pages_wb(struct page *page, int numpages); 808c2ecf20Sopenharmony_ciint set_pages_ro(struct page *page, int numpages); 818c2ecf20Sopenharmony_ciint set_pages_rw(struct page *page, int numpages); 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ciint set_direct_map_invalid_noflush(struct page *page); 848c2ecf20Sopenharmony_ciint set_direct_map_default_noflush(struct page *page); 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ciextern int kernel_set_to_readonly; 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_64 898c2ecf20Sopenharmony_ci/* 908c2ecf20Sopenharmony_ci * Prevent speculative access to the page by either unmapping 918c2ecf20Sopenharmony_ci * it (if we do not require access to any part of the page) or 928c2ecf20Sopenharmony_ci * marking it uncacheable (if we want to try to retrieve data 938c2ecf20Sopenharmony_ci * from non-poisoned lines in the page). 948c2ecf20Sopenharmony_ci */ 958c2ecf20Sopenharmony_cistatic inline int set_mce_nospec(unsigned long pfn, bool unmap) 968c2ecf20Sopenharmony_ci{ 978c2ecf20Sopenharmony_ci unsigned long decoy_addr; 988c2ecf20Sopenharmony_ci int rc; 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci /* 1018c2ecf20Sopenharmony_ci * We would like to just call: 1028c2ecf20Sopenharmony_ci * set_memory_XX((unsigned long)pfn_to_kaddr(pfn), 1); 1038c2ecf20Sopenharmony_ci * but doing that would radically increase the odds of a 1048c2ecf20Sopenharmony_ci * speculative access to the poison page because we'd have 1058c2ecf20Sopenharmony_ci * the virtual address of the kernel 1:1 mapping sitting 1068c2ecf20Sopenharmony_ci * around in registers. 1078c2ecf20Sopenharmony_ci * Instead we get tricky. We create a non-canonical address 1088c2ecf20Sopenharmony_ci * that looks just like the one we want, but has bit 63 flipped. 1098c2ecf20Sopenharmony_ci * This relies on set_memory_XX() properly sanitizing any __pa() 1108c2ecf20Sopenharmony_ci * results with __PHYSICAL_MASK or PTE_PFN_MASK. 1118c2ecf20Sopenharmony_ci */ 1128c2ecf20Sopenharmony_ci decoy_addr = (pfn << PAGE_SHIFT) + (PAGE_OFFSET ^ BIT(63)); 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci if (unmap) 1158c2ecf20Sopenharmony_ci rc = set_memory_np(decoy_addr, 1); 1168c2ecf20Sopenharmony_ci else 1178c2ecf20Sopenharmony_ci rc = set_memory_uc(decoy_addr, 1); 1188c2ecf20Sopenharmony_ci if (rc) 1198c2ecf20Sopenharmony_ci pr_warn("Could not invalidate pfn=0x%lx from 1:1 map\n", pfn); 1208c2ecf20Sopenharmony_ci return rc; 1218c2ecf20Sopenharmony_ci} 1228c2ecf20Sopenharmony_ci#define set_mce_nospec set_mce_nospec 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci/* Restore full speculative operation to the pfn. */ 1258c2ecf20Sopenharmony_cistatic inline int clear_mce_nospec(unsigned long pfn) 1268c2ecf20Sopenharmony_ci{ 1278c2ecf20Sopenharmony_ci return set_memory_wb((unsigned long) pfn_to_kaddr(pfn), 1); 1288c2ecf20Sopenharmony_ci} 1298c2ecf20Sopenharmony_ci#define clear_mce_nospec clear_mce_nospec 1308c2ecf20Sopenharmony_ci#else 1318c2ecf20Sopenharmony_ci/* 1328c2ecf20Sopenharmony_ci * Few people would run a 32-bit kernel on a machine that supports 1338c2ecf20Sopenharmony_ci * recoverable errors because they have too much memory to boot 32-bit. 1348c2ecf20Sopenharmony_ci */ 1358c2ecf20Sopenharmony_ci#endif 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci#endif /* _ASM_X86_SET_MEMORY_H */ 138