Loading...
1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _ASM_X86_SET_MEMORY_H
3#define _ASM_X86_SET_MEMORY_H
4
5#include <asm/page.h>
6#include <asm-generic/set_memory.h>
7
8/*
9 * The set_memory_* API can be used to change various attributes of a virtual
10 * address range. The attributes include:
11 * Cachability : UnCached, WriteCombining, WriteThrough, WriteBack
12 * Executability : eXeutable, NoteXecutable
13 * Read/Write : ReadOnly, ReadWrite
14 * Presence : NotPresent
15 * Encryption : Encrypted, Decrypted
16 *
17 * Within a category, the attributes are mutually exclusive.
18 *
19 * The implementation of this API will take care of various aspects that
20 * are associated with changing such attributes, such as:
21 * - Flushing TLBs
22 * - Flushing CPU caches
23 * - Making sure aliases of the memory behind the mapping don't violate
24 * coherency rules as defined by the CPU in the system.
25 *
26 * What this API does not do:
27 * - Provide exclusion between various callers - including callers that
28 * operation on other mappings of the same physical page
29 * - Restore default attributes when a page is freed
30 * - Guarantee that mappings other than the requested one are
31 * in any state, other than that these do not violate rules for
32 * the CPU you have. Do not depend on any effects on other mappings,
33 * CPUs other than the one you have may have more relaxed rules.
34 * The caller is required to take care of these.
35 */
36
37int _set_memory_uc(unsigned long addr, int numpages);
38int _set_memory_wc(unsigned long addr, int numpages);
39int _set_memory_wt(unsigned long addr, int numpages);
40int _set_memory_wb(unsigned long addr, int numpages);
41int set_memory_uc(unsigned long addr, int numpages);
42int set_memory_wc(unsigned long addr, int numpages);
43int set_memory_wt(unsigned long addr, int numpages);
44int set_memory_wb(unsigned long addr, int numpages);
45int set_memory_np(unsigned long addr, int numpages);
46int set_memory_4k(unsigned long addr, int numpages);
47int set_memory_encrypted(unsigned long addr, int numpages);
48int set_memory_decrypted(unsigned long addr, int numpages);
49
50int set_memory_array_uc(unsigned long *addr, int addrinarray);
51int set_memory_array_wc(unsigned long *addr, int addrinarray);
52int set_memory_array_wt(unsigned long *addr, int addrinarray);
53int set_memory_array_wb(unsigned long *addr, int addrinarray);
54
55int set_pages_array_uc(struct page **pages, int addrinarray);
56int set_pages_array_wc(struct page **pages, int addrinarray);
57int set_pages_array_wt(struct page **pages, int addrinarray);
58int set_pages_array_wb(struct page **pages, int addrinarray);
59
60/*
61 * For legacy compatibility with the old APIs, a few functions
62 * are provided that work on a "struct page".
63 * These functions operate ONLY on the 1:1 kernel mapping of the
64 * memory that the struct page represents, and internally just
65 * call the set_memory_* function. See the description of the
66 * set_memory_* function for more details on conventions.
67 *
68 * These APIs should be considered *deprecated* and are likely going to
69 * be removed in the future.
70 * The reason for this is the implicit operation on the 1:1 mapping only,
71 * making this not a generally useful API.
72 *
73 * Specifically, many users of the old APIs had a virtual address,
74 * called virt_to_page() or vmalloc_to_page() on that address to
75 * get a struct page* that the old API required.
76 * To convert these cases, use set_memory_*() on the original
77 * virtual address, do not use these functions.
78 */
79
80int set_pages_uc(struct page *page, int numpages);
81int set_pages_wb(struct page *page, int numpages);
82int set_pages_x(struct page *page, int numpages);
83int set_pages_nx(struct page *page, int numpages);
84int set_pages_ro(struct page *page, int numpages);
85int set_pages_rw(struct page *page, int numpages);
86
87extern int kernel_set_to_readonly;
88void set_kernel_text_rw(void);
89void set_kernel_text_ro(void);
90
91#endif /* _ASM_X86_SET_MEMORY_H */
1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _ASM_X86_SET_MEMORY_H
3#define _ASM_X86_SET_MEMORY_H
4
5#include <asm/page.h>
6#include <asm-generic/set_memory.h>
7
8/*
9 * The set_memory_* API can be used to change various attributes of a virtual
10 * address range. The attributes include:
11 * Cachability : UnCached, WriteCombining, WriteThrough, WriteBack
12 * Executability : eXeutable, NoteXecutable
13 * Read/Write : ReadOnly, ReadWrite
14 * Presence : NotPresent
15 * Encryption : Encrypted, Decrypted
16 *
17 * Within a category, the attributes are mutually exclusive.
18 *
19 * The implementation of this API will take care of various aspects that
20 * are associated with changing such attributes, such as:
21 * - Flushing TLBs
22 * - Flushing CPU caches
23 * - Making sure aliases of the memory behind the mapping don't violate
24 * coherency rules as defined by the CPU in the system.
25 *
26 * What this API does not do:
27 * - Provide exclusion between various callers - including callers that
28 * operation on other mappings of the same physical page
29 * - Restore default attributes when a page is freed
30 * - Guarantee that mappings other than the requested one are
31 * in any state, other than that these do not violate rules for
32 * the CPU you have. Do not depend on any effects on other mappings,
33 * CPUs other than the one you have may have more relaxed rules.
34 * The caller is required to take care of these.
35 */
36
37int __set_memory_prot(unsigned long addr, int numpages, pgprot_t prot);
38int _set_memory_uc(unsigned long addr, int numpages);
39int _set_memory_wc(unsigned long addr, int numpages);
40int _set_memory_wt(unsigned long addr, int numpages);
41int _set_memory_wb(unsigned long addr, int numpages);
42int set_memory_uc(unsigned long addr, int numpages);
43int set_memory_wc(unsigned long addr, int numpages);
44int set_memory_wb(unsigned long addr, int numpages);
45int set_memory_np(unsigned long addr, int numpages);
46int set_memory_4k(unsigned long addr, int numpages);
47int set_memory_encrypted(unsigned long addr, int numpages);
48int set_memory_decrypted(unsigned long addr, int numpages);
49int set_memory_np_noalias(unsigned long addr, int numpages);
50int set_memory_nonglobal(unsigned long addr, int numpages);
51int set_memory_global(unsigned long addr, int numpages);
52
53int set_pages_array_uc(struct page **pages, int addrinarray);
54int set_pages_array_wc(struct page **pages, int addrinarray);
55int set_pages_array_wt(struct page **pages, int addrinarray);
56int set_pages_array_wb(struct page **pages, int addrinarray);
57
58/*
59 * For legacy compatibility with the old APIs, a few functions
60 * are provided that work on a "struct page".
61 * These functions operate ONLY on the 1:1 kernel mapping of the
62 * memory that the struct page represents, and internally just
63 * call the set_memory_* function. See the description of the
64 * set_memory_* function for more details on conventions.
65 *
66 * These APIs should be considered *deprecated* and are likely going to
67 * be removed in the future.
68 * The reason for this is the implicit operation on the 1:1 mapping only,
69 * making this not a generally useful API.
70 *
71 * Specifically, many users of the old APIs had a virtual address,
72 * called virt_to_page() or vmalloc_to_page() on that address to
73 * get a struct page* that the old API required.
74 * To convert these cases, use set_memory_*() on the original
75 * virtual address, do not use these functions.
76 */
77
78int set_pages_uc(struct page *page, int numpages);
79int set_pages_wb(struct page *page, int numpages);
80int set_pages_ro(struct page *page, int numpages);
81int set_pages_rw(struct page *page, int numpages);
82
83int set_direct_map_invalid_noflush(struct page *page);
84int set_direct_map_default_noflush(struct page *page);
85
86extern int kernel_set_to_readonly;
87
88#ifdef CONFIG_X86_64
89/*
90 * Prevent speculative access to the page by either unmapping
91 * it (if we do not require access to any part of the page) or
92 * marking it uncacheable (if we want to try to retrieve data
93 * from non-poisoned lines in the page).
94 */
95static inline int set_mce_nospec(unsigned long pfn, bool unmap)
96{
97 unsigned long decoy_addr;
98 int rc;
99
100 /*
101 * We would like to just call:
102 * set_memory_XX((unsigned long)pfn_to_kaddr(pfn), 1);
103 * but doing that would radically increase the odds of a
104 * speculative access to the poison page because we'd have
105 * the virtual address of the kernel 1:1 mapping sitting
106 * around in registers.
107 * Instead we get tricky. We create a non-canonical address
108 * that looks just like the one we want, but has bit 63 flipped.
109 * This relies on set_memory_XX() properly sanitizing any __pa()
110 * results with __PHYSICAL_MASK or PTE_PFN_MASK.
111 */
112 decoy_addr = (pfn << PAGE_SHIFT) + (PAGE_OFFSET ^ BIT(63));
113
114 if (unmap)
115 rc = set_memory_np(decoy_addr, 1);
116 else
117 rc = set_memory_uc(decoy_addr, 1);
118 if (rc)
119 pr_warn("Could not invalidate pfn=0x%lx from 1:1 map\n", pfn);
120 return rc;
121}
122#define set_mce_nospec set_mce_nospec
123
124/* Restore full speculative operation to the pfn. */
125static inline int clear_mce_nospec(unsigned long pfn)
126{
127 return set_memory_wb((unsigned long) pfn_to_kaddr(pfn), 1);
128}
129#define clear_mce_nospec clear_mce_nospec
130#else
131/*
132 * Few people would run a 32-bit kernel on a machine that supports
133 * recoverable errors because they have too much memory to boot 32-bit.
134 */
135#endif
136
137#endif /* _ASM_X86_SET_MEMORY_H */