Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2002 Andi Kleen, SuSE Labs. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * Thanks to Ben LaHaise for precious feedback. |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 5 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include <linux/highmem.h> |
Mike Rapoport | 57c8a66 | 2018-10-30 15:09:49 -0700 | [diff] [blame] | 7 | #include <linux/memblock.h> |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 8 | #include <linux/sched.h> |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 9 | #include <linux/mm.h> |
Thomas Gleixner | 76ebd05 | 2008-02-09 23:24:09 +0100 | [diff] [blame] | 10 | #include <linux/interrupt.h> |
Thomas Gleixner | ee7ae7a | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 11 | #include <linux/seq_file.h> |
| 12 | #include <linux/debugfs.h> |
Tejun Heo | e59a1bb | 2009-06-22 11:56:24 +0900 | [diff] [blame] | 13 | #include <linux/pfn.h> |
Tejun Heo | 8c4bfc6 | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 14 | #include <linux/percpu.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/gfp.h> |
Matthieu Castet | 5bd5a45 | 2010-11-16 22:31:26 +0100 | [diff] [blame] | 16 | #include <linux/pci.h> |
Stephen Rothwell | d647230 | 2015-06-02 19:01:38 +1000 | [diff] [blame] | 17 | #include <linux/vmalloc.h> |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 18 | |
Ingo Molnar | 66441bd | 2017-01-27 10:27:10 +0100 | [diff] [blame] | 19 | #include <asm/e820/api.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <asm/processor.h> |
| 21 | #include <asm/tlbflush.h> |
Dave Jones | f8af095 | 2006-01-06 00:12:10 -0800 | [diff] [blame] | 22 | #include <asm/sections.h> |
Jeremy Fitzhardinge | 93dbda7 | 2009-02-26 17:35:44 -0800 | [diff] [blame] | 23 | #include <asm/setup.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 24 | #include <linux/uaccess.h> |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 25 | #include <asm/pgalloc.h> |
Thomas Gleixner | c31c7d4 | 2008-02-18 20:54:14 +0100 | [diff] [blame] | 26 | #include <asm/proto.h> |
venkatesh.pallipadi@intel.com | 1219333 | 2008-03-18 17:00:18 -0700 | [diff] [blame] | 27 | #include <asm/pat.h> |
Laura Abbott | d116365 | 2017-05-08 15:58:11 -0700 | [diff] [blame] | 28 | #include <asm/set_memory.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Peter Zijlstra | 935f583 | 2018-12-03 18:03:49 +0100 | [diff] [blame] | 30 | #include "mm_internal.h" |
| 31 | |
Ingo Molnar | 9df8499 | 2008-02-04 16:48:09 +0100 | [diff] [blame] | 32 | /* |
| 33 | * The current flushing context - we pass it instead of 5 arguments: |
| 34 | */ |
Thomas Gleixner | 72e458d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 35 | struct cpa_data { |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 36 | unsigned long *vaddr; |
Borislav Petkov | 0fd64c2 | 2013-10-31 17:25:00 +0100 | [diff] [blame] | 37 | pgd_t *pgd; |
Thomas Gleixner | 72e458d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 38 | pgprot_t mask_set; |
| 39 | pgprot_t mask_clr; |
Matt Fleming | 7425637 | 2016-01-29 11:36:10 +0000 | [diff] [blame] | 40 | unsigned long numpages; |
Peter Zijlstra | 98bfc9b | 2018-12-03 18:03:47 +0100 | [diff] [blame] | 41 | unsigned long curpage; |
Thomas Gleixner | c31c7d4 | 2008-02-18 20:54:14 +0100 | [diff] [blame] | 42 | unsigned long pfn; |
Peter Zijlstra | 98bfc9b | 2018-12-03 18:03:47 +0100 | [diff] [blame] | 43 | unsigned int flags; |
| 44 | unsigned int force_split : 1, |
Thomas Gleixner | f61c5ba | 2018-09-17 16:29:14 +0200 | [diff] [blame] | 45 | force_static_prot : 1; |
venkatesh.pallipadi@intel.com | 9ae2847 | 2009-03-19 14:51:14 -0700 | [diff] [blame] | 46 | struct page **pages; |
Thomas Gleixner | 72e458d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 47 | }; |
| 48 | |
Thomas Gleixner | 4046460 | 2018-09-17 16:29:11 +0200 | [diff] [blame] | 49 | enum cpa_warn { |
Thomas Gleixner | f61c5ba | 2018-09-17 16:29:14 +0200 | [diff] [blame] | 50 | CPA_CONFLICT, |
Thomas Gleixner | 4046460 | 2018-09-17 16:29:11 +0200 | [diff] [blame] | 51 | CPA_PROTECT, |
| 52 | CPA_DETECT, |
| 53 | }; |
| 54 | |
| 55 | static const int cpa_warn_level = CPA_PROTECT; |
| 56 | |
Suresh Siddha | ad5ca55 | 2008-09-23 14:00:42 -0700 | [diff] [blame] | 57 | /* |
| 58 | * Serialize cpa() (for !DEBUG_PAGEALLOC which uses large identity mappings) |
| 59 | * using cpa_lock. So that we don't allow any other cpu, with stale large tlb |
| 60 | * entries change the page attribute in parallel to some other cpu |
| 61 | * splitting a large page entry along with changing the attribute. |
| 62 | */ |
| 63 | static DEFINE_SPINLOCK(cpa_lock); |
| 64 | |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 65 | #define CPA_FLUSHTLB 1 |
| 66 | #define CPA_ARRAY 2 |
venkatesh.pallipadi@intel.com | 9ae2847 | 2009-03-19 14:51:14 -0700 | [diff] [blame] | 67 | #define CPA_PAGES_ARRAY 4 |
Dave Hansen | c40a56a | 2018-08-02 15:58:31 -0700 | [diff] [blame] | 68 | #define CPA_NO_CHECK_ALIAS 8 /* Do not search for aliases */ |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 69 | |
Thomas Gleixner | 65280e6 | 2008-05-05 16:35:21 +0200 | [diff] [blame] | 70 | #ifdef CONFIG_PROC_FS |
Andi Kleen | ce0c0e5 | 2008-05-02 11:46:49 +0200 | [diff] [blame] | 71 | static unsigned long direct_pages_count[PG_LEVEL_NUM]; |
| 72 | |
Thomas Gleixner | 65280e6 | 2008-05-05 16:35:21 +0200 | [diff] [blame] | 73 | void update_page_count(int level, unsigned long pages) |
Andi Kleen | ce0c0e5 | 2008-05-02 11:46:49 +0200 | [diff] [blame] | 74 | { |
Andi Kleen | ce0c0e5 | 2008-05-02 11:46:49 +0200 | [diff] [blame] | 75 | /* Protect against CPA */ |
Andrea Arcangeli | a79e53d | 2011-02-16 15:45:22 -0800 | [diff] [blame] | 76 | spin_lock(&pgd_lock); |
Andi Kleen | ce0c0e5 | 2008-05-02 11:46:49 +0200 | [diff] [blame] | 77 | direct_pages_count[level] += pages; |
Andrea Arcangeli | a79e53d | 2011-02-16 15:45:22 -0800 | [diff] [blame] | 78 | spin_unlock(&pgd_lock); |
Andi Kleen | ce0c0e5 | 2008-05-02 11:46:49 +0200 | [diff] [blame] | 79 | } |
| 80 | |
Thomas Gleixner | 65280e6 | 2008-05-05 16:35:21 +0200 | [diff] [blame] | 81 | static void split_page_count(int level) |
| 82 | { |
Dave Jones | c9e0d39 | 2016-01-11 12:04:28 -0500 | [diff] [blame] | 83 | if (direct_pages_count[level] == 0) |
| 84 | return; |
| 85 | |
Thomas Gleixner | 65280e6 | 2008-05-05 16:35:21 +0200 | [diff] [blame] | 86 | direct_pages_count[level]--; |
| 87 | direct_pages_count[level - 1] += PTRS_PER_PTE; |
| 88 | } |
| 89 | |
Alexey Dobriyan | e1759c2 | 2008-10-15 23:50:22 +0400 | [diff] [blame] | 90 | void arch_report_meminfo(struct seq_file *m) |
Thomas Gleixner | 65280e6 | 2008-05-05 16:35:21 +0200 | [diff] [blame] | 91 | { |
Hugh Dickins | b9c3bfc | 2008-11-06 12:05:40 +0000 | [diff] [blame] | 92 | seq_printf(m, "DirectMap4k: %8lu kB\n", |
Hugh Dickins | a06de63 | 2008-08-15 13:58:32 +0100 | [diff] [blame] | 93 | direct_pages_count[PG_LEVEL_4K] << 2); |
| 94 | #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE) |
Hugh Dickins | b9c3bfc | 2008-11-06 12:05:40 +0000 | [diff] [blame] | 95 | seq_printf(m, "DirectMap2M: %8lu kB\n", |
Hugh Dickins | a06de63 | 2008-08-15 13:58:32 +0100 | [diff] [blame] | 96 | direct_pages_count[PG_LEVEL_2M] << 11); |
| 97 | #else |
Hugh Dickins | b9c3bfc | 2008-11-06 12:05:40 +0000 | [diff] [blame] | 98 | seq_printf(m, "DirectMap4M: %8lu kB\n", |
Hugh Dickins | a06de63 | 2008-08-15 13:58:32 +0100 | [diff] [blame] | 99 | direct_pages_count[PG_LEVEL_2M] << 12); |
| 100 | #endif |
Hugh Dickins | a06de63 | 2008-08-15 13:58:32 +0100 | [diff] [blame] | 101 | if (direct_gbpages) |
Hugh Dickins | b9c3bfc | 2008-11-06 12:05:40 +0000 | [diff] [blame] | 102 | seq_printf(m, "DirectMap1G: %8lu kB\n", |
Hugh Dickins | a06de63 | 2008-08-15 13:58:32 +0100 | [diff] [blame] | 103 | direct_pages_count[PG_LEVEL_1G] << 20); |
Thomas Gleixner | 65280e6 | 2008-05-05 16:35:21 +0200 | [diff] [blame] | 104 | } |
| 105 | #else |
| 106 | static inline void split_page_count(int level) { } |
| 107 | #endif |
| 108 | |
Thomas Gleixner | 5c280cf | 2018-09-17 16:29:12 +0200 | [diff] [blame] | 109 | #ifdef CONFIG_X86_CPA_STATISTICS |
| 110 | |
| 111 | static unsigned long cpa_1g_checked; |
| 112 | static unsigned long cpa_1g_sameprot; |
| 113 | static unsigned long cpa_1g_preserved; |
| 114 | static unsigned long cpa_2m_checked; |
| 115 | static unsigned long cpa_2m_sameprot; |
| 116 | static unsigned long cpa_2m_preserved; |
Thomas Gleixner | 5c280cf | 2018-09-17 16:29:12 +0200 | [diff] [blame] | 117 | static unsigned long cpa_4k_install; |
| 118 | |
| 119 | static inline void cpa_inc_1g_checked(void) |
| 120 | { |
| 121 | cpa_1g_checked++; |
| 122 | } |
| 123 | |
| 124 | static inline void cpa_inc_2m_checked(void) |
| 125 | { |
| 126 | cpa_2m_checked++; |
| 127 | } |
| 128 | |
Thomas Gleixner | 5c280cf | 2018-09-17 16:29:12 +0200 | [diff] [blame] | 129 | static inline void cpa_inc_4k_install(void) |
| 130 | { |
| 131 | cpa_4k_install++; |
| 132 | } |
| 133 | |
| 134 | static inline void cpa_inc_lp_sameprot(int level) |
| 135 | { |
| 136 | if (level == PG_LEVEL_1G) |
| 137 | cpa_1g_sameprot++; |
| 138 | else |
| 139 | cpa_2m_sameprot++; |
| 140 | } |
| 141 | |
| 142 | static inline void cpa_inc_lp_preserved(int level) |
| 143 | { |
| 144 | if (level == PG_LEVEL_1G) |
| 145 | cpa_1g_preserved++; |
| 146 | else |
| 147 | cpa_2m_preserved++; |
| 148 | } |
| 149 | |
| 150 | static int cpastats_show(struct seq_file *m, void *p) |
| 151 | { |
| 152 | seq_printf(m, "1G pages checked: %16lu\n", cpa_1g_checked); |
| 153 | seq_printf(m, "1G pages sameprot: %16lu\n", cpa_1g_sameprot); |
| 154 | seq_printf(m, "1G pages preserved: %16lu\n", cpa_1g_preserved); |
| 155 | seq_printf(m, "2M pages checked: %16lu\n", cpa_2m_checked); |
| 156 | seq_printf(m, "2M pages sameprot: %16lu\n", cpa_2m_sameprot); |
| 157 | seq_printf(m, "2M pages preserved: %16lu\n", cpa_2m_preserved); |
Thomas Gleixner | 5c280cf | 2018-09-17 16:29:12 +0200 | [diff] [blame] | 158 | seq_printf(m, "4K pages set-checked: %16lu\n", cpa_4k_install); |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | static int cpastats_open(struct inode *inode, struct file *file) |
| 163 | { |
| 164 | return single_open(file, cpastats_show, NULL); |
| 165 | } |
| 166 | |
| 167 | static const struct file_operations cpastats_fops = { |
| 168 | .open = cpastats_open, |
| 169 | .read = seq_read, |
| 170 | .llseek = seq_lseek, |
| 171 | .release = single_release, |
| 172 | }; |
| 173 | |
| 174 | static int __init cpa_stats_init(void) |
| 175 | { |
| 176 | debugfs_create_file("cpa_stats", S_IRUSR, arch_debugfs_dir, NULL, |
| 177 | &cpastats_fops); |
| 178 | return 0; |
| 179 | } |
| 180 | late_initcall(cpa_stats_init); |
| 181 | #else |
| 182 | static inline void cpa_inc_1g_checked(void) { } |
| 183 | static inline void cpa_inc_2m_checked(void) { } |
Thomas Gleixner | 5c280cf | 2018-09-17 16:29:12 +0200 | [diff] [blame] | 184 | static inline void cpa_inc_4k_install(void) { } |
| 185 | static inline void cpa_inc_lp_sameprot(int level) { } |
| 186 | static inline void cpa_inc_lp_preserved(int level) { } |
| 187 | #endif |
| 188 | |
| 189 | |
Dave Hansen | 58e65b5 | 2018-04-20 15:20:21 -0700 | [diff] [blame] | 190 | static inline int |
| 191 | within(unsigned long addr, unsigned long start, unsigned long end) |
| 192 | { |
| 193 | return addr >= start && addr < end; |
| 194 | } |
| 195 | |
| 196 | static inline int |
| 197 | within_inclusive(unsigned long addr, unsigned long start, unsigned long end) |
| 198 | { |
| 199 | return addr >= start && addr <= end; |
| 200 | } |
| 201 | |
Thomas Gleixner | c31c7d4 | 2008-02-18 20:54:14 +0100 | [diff] [blame] | 202 | #ifdef CONFIG_X86_64 |
| 203 | |
| 204 | static inline unsigned long highmap_start_pfn(void) |
| 205 | { |
Alexander Duyck | fc8d782 | 2012-11-16 13:57:13 -0800 | [diff] [blame] | 206 | return __pa_symbol(_text) >> PAGE_SHIFT; |
Thomas Gleixner | c31c7d4 | 2008-02-18 20:54:14 +0100 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | static inline unsigned long highmap_end_pfn(void) |
| 210 | { |
Thomas Garnier | 4ff5308 | 2016-06-15 12:05:45 -0700 | [diff] [blame] | 211 | /* Do not reference physical address outside the kernel. */ |
| 212 | return __pa_symbol(roundup(_brk_end, PMD_SIZE) - 1) >> PAGE_SHIFT; |
Thomas Gleixner | c31c7d4 | 2008-02-18 20:54:14 +0100 | [diff] [blame] | 213 | } |
| 214 | |
Dave Hansen | 58e65b5 | 2018-04-20 15:20:21 -0700 | [diff] [blame] | 215 | static bool __cpa_pfn_in_highmap(unsigned long pfn) |
| 216 | { |
| 217 | /* |
| 218 | * Kernel text has an alias mapping at a high address, known |
| 219 | * here as "highmap". |
| 220 | */ |
| 221 | return within_inclusive(pfn, highmap_start_pfn(), highmap_end_pfn()); |
| 222 | } |
| 223 | |
| 224 | #else |
| 225 | |
| 226 | static bool __cpa_pfn_in_highmap(unsigned long pfn) |
| 227 | { |
| 228 | /* There is no highmap on 32-bit */ |
| 229 | return false; |
| 230 | } |
| 231 | |
Thomas Gleixner | c31c7d4 | 2008-02-18 20:54:14 +0100 | [diff] [blame] | 232 | #endif |
| 233 | |
Peter Zijlstra | 0521e8b | 2019-02-08 13:08:59 +0100 | [diff] [blame] | 234 | /* |
| 235 | * See set_mce_nospec(). |
| 236 | * |
| 237 | * Machine check recovery code needs to change cache mode of poisoned pages to |
| 238 | * UC to avoid speculative access logging another error. But passing the |
| 239 | * address of the 1:1 mapping to set_memory_uc() is a fine way to encourage a |
| 240 | * speculative access. So we cheat and flip the top bit of the address. This |
| 241 | * works fine for the code that updates the page tables. But at the end of the |
| 242 | * process we need to flush the TLB and cache and the non-canonical address |
| 243 | * causes a #GP fault when used by the INVLPG and CLFLUSH instructions. |
| 244 | * |
| 245 | * But in the common case we already have a canonical address. This code |
| 246 | * will fix the top bit if needed and is a no-op otherwise. |
| 247 | */ |
| 248 | static inline unsigned long fix_addr(unsigned long addr) |
| 249 | { |
| 250 | #ifdef CONFIG_X86_64 |
| 251 | return (long)(addr << 1) >> 1; |
| 252 | #else |
| 253 | return addr; |
| 254 | #endif |
| 255 | } |
| 256 | |
Peter Zijlstra | 98bfc9b | 2018-12-03 18:03:47 +0100 | [diff] [blame] | 257 | static unsigned long __cpa_addr(struct cpa_data *cpa, unsigned long idx) |
Peter Zijlstra | 16ebf03 | 2018-12-03 18:03:46 +0100 | [diff] [blame] | 258 | { |
| 259 | if (cpa->flags & CPA_PAGES_ARRAY) { |
| 260 | struct page *page = cpa->pages[idx]; |
| 261 | |
| 262 | if (unlikely(PageHighMem(page))) |
| 263 | return 0; |
| 264 | |
| 265 | return (unsigned long)page_address(page); |
| 266 | } |
| 267 | |
| 268 | if (cpa->flags & CPA_ARRAY) |
| 269 | return cpa->vaddr[idx]; |
| 270 | |
Peter Zijlstra | 98bfc9b | 2018-12-03 18:03:47 +0100 | [diff] [blame] | 271 | return *cpa->vaddr + idx * PAGE_SIZE; |
Peter Zijlstra | 16ebf03 | 2018-12-03 18:03:46 +0100 | [diff] [blame] | 272 | } |
| 273 | |
Arjan van de Ven | ed724be | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 274 | /* |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 275 | * Flushing functions |
| 276 | */ |
Thomas Gleixner | cd8ddf1 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 277 | |
Peter Zijlstra | c38116b | 2018-12-03 18:03:52 +0100 | [diff] [blame] | 278 | static void clflush_cache_range_opt(void *vaddr, unsigned int size) |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 279 | { |
Chris Wilson | 1f1a89a | 2016-01-08 09:55:33 +0000 | [diff] [blame] | 280 | const unsigned long clflush_size = boot_cpu_data.x86_clflush_size; |
| 281 | void *p = (void *)((unsigned long)vaddr & ~(clflush_size - 1)); |
Ross Zwisler | 6c434d6 | 2015-05-11 10:15:49 +0200 | [diff] [blame] | 282 | void *vend = vaddr + size; |
Chris Wilson | 1f1a89a | 2016-01-08 09:55:33 +0000 | [diff] [blame] | 283 | |
| 284 | if (p >= vend) |
| 285 | return; |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 286 | |
Chris Wilson | 1f1a89a | 2016-01-08 09:55:33 +0000 | [diff] [blame] | 287 | for (; p < vend; p += clflush_size) |
Ross Zwisler | 6c434d6 | 2015-05-11 10:15:49 +0200 | [diff] [blame] | 288 | clflushopt(p); |
Peter Zijlstra | c38116b | 2018-12-03 18:03:52 +0100 | [diff] [blame] | 289 | } |
Ingo Molnar | 4c61afc | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 290 | |
Peter Zijlstra | c38116b | 2018-12-03 18:03:52 +0100 | [diff] [blame] | 291 | /** |
| 292 | * clflush_cache_range - flush a cache range with clflush |
| 293 | * @vaddr: virtual start address |
| 294 | * @size: number of bytes to flush |
| 295 | * |
| 296 | * CLFLUSHOPT is an unordered instruction which needs fencing with MFENCE or |
| 297 | * SFENCE to avoid ordering issues. |
| 298 | */ |
| 299 | void clflush_cache_range(void *vaddr, unsigned int size) |
| 300 | { |
| 301 | mb(); |
| 302 | clflush_cache_range_opt(vaddr, size); |
Thomas Gleixner | cd8ddf1 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 303 | mb(); |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 304 | } |
Eric Anholt | e517a5e | 2009-09-10 17:48:48 -0700 | [diff] [blame] | 305 | EXPORT_SYMBOL_GPL(clflush_cache_range); |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 306 | |
Dan Williams | f2b6125 | 2017-05-29 23:00:34 -0700 | [diff] [blame] | 307 | void arch_invalidate_pmem(void *addr, size_t size) |
| 308 | { |
| 309 | clflush_cache_range(addr, size); |
| 310 | } |
| 311 | EXPORT_SYMBOL_GPL(arch_invalidate_pmem); |
| 312 | |
Thomas Gleixner | af1e684 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 313 | static void __cpa_flush_all(void *arg) |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 314 | { |
Andi Kleen | 6bb8383 | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 315 | unsigned long cache = (unsigned long)arg; |
| 316 | |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 317 | /* |
| 318 | * Flush all to work around Errata in early athlons regarding |
| 319 | * large page flushing. |
| 320 | */ |
| 321 | __flush_tlb_all(); |
| 322 | |
venkatesh.pallipadi@intel.com | 0b82753 | 2009-05-22 13:23:37 -0700 | [diff] [blame] | 323 | if (cache && boot_cpu_data.x86 >= 4) |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 324 | wbinvd(); |
| 325 | } |
| 326 | |
Andi Kleen | 6bb8383 | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 327 | static void cpa_flush_all(unsigned long cache) |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 328 | { |
Dave Hansen | d2479a3 | 2018-04-20 15:20:19 -0700 | [diff] [blame] | 329 | BUG_ON(irqs_disabled() && !early_boot_irqs_disabled); |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 330 | |
Jens Axboe | 15c8b6c | 2008-05-09 09:39:44 +0200 | [diff] [blame] | 331 | on_each_cpu(__cpa_flush_all, (void *) cache, 1); |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 332 | } |
| 333 | |
Peter Zijlstra | fe0937b | 2018-12-03 18:03:51 +0100 | [diff] [blame] | 334 | void __cpa_flush_tlb(void *data) |
Peter Zijlstra | 47e262a | 2018-09-19 10:50:23 +0200 | [diff] [blame] | 335 | { |
Peter Zijlstra | 935f583 | 2018-12-03 18:03:49 +0100 | [diff] [blame] | 336 | struct cpa_data *cpa = data; |
| 337 | unsigned int i; |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 338 | |
Peter Zijlstra | 935f583 | 2018-12-03 18:03:49 +0100 | [diff] [blame] | 339 | for (i = 0; i < cpa->numpages; i++) |
Peter Zijlstra | 0521e8b | 2019-02-08 13:08:59 +0100 | [diff] [blame] | 340 | __flush_tlb_one_kernel(fix_addr(__cpa_addr(cpa, i))); |
Peter Zijlstra | 935f583 | 2018-12-03 18:03:49 +0100 | [diff] [blame] | 341 | } |
| 342 | |
Peter Zijlstra | fe0937b | 2018-12-03 18:03:51 +0100 | [diff] [blame] | 343 | static void cpa_flush(struct cpa_data *data, int cache) |
Peter Zijlstra | 935f583 | 2018-12-03 18:03:49 +0100 | [diff] [blame] | 344 | { |
Peter Zijlstra | fe0937b | 2018-12-03 18:03:51 +0100 | [diff] [blame] | 345 | struct cpa_data *cpa = data; |
Peter Zijlstra | 935f583 | 2018-12-03 18:03:49 +0100 | [diff] [blame] | 346 | unsigned int i; |
| 347 | |
Thomas Gleixner | 57a6a46 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 348 | BUG_ON(irqs_disabled() && !early_boot_irqs_disabled); |
| 349 | |
Thomas Gleixner | 57a6a46 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 350 | if (cache && !static_cpu_has(X86_FEATURE_CLFLUSH)) { |
| 351 | cpa_flush_all(cache); |
Peter Zijlstra | 721066d | 2018-12-03 18:03:44 +0100 | [diff] [blame] | 352 | return; |
Thomas Gleixner | 57a6a46 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 353 | } |
| 354 | |
Peter Zijlstra | 935f583 | 2018-12-03 18:03:49 +0100 | [diff] [blame] | 355 | if (cpa->numpages <= tlb_single_page_flush_ceiling) |
Peter Zijlstra | fe0937b | 2018-12-03 18:03:51 +0100 | [diff] [blame] | 356 | on_each_cpu(__cpa_flush_tlb, cpa, 1); |
Peter Zijlstra | 935f583 | 2018-12-03 18:03:49 +0100 | [diff] [blame] | 357 | else |
| 358 | flush_tlb_all(); |
Ingo Molnar | 4c61afc | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 359 | |
| 360 | if (!cache) |
| 361 | return; |
| 362 | |
Peter Zijlstra | c38116b | 2018-12-03 18:03:52 +0100 | [diff] [blame] | 363 | mb(); |
Peter Zijlstra | 935f583 | 2018-12-03 18:03:49 +0100 | [diff] [blame] | 364 | for (i = 0; i < cpa->numpages; i++) { |
| 365 | unsigned long addr = __cpa_addr(cpa, i); |
| 366 | unsigned int level; |
venkatesh.pallipadi@intel.com | 9ae2847 | 2009-03-19 14:51:14 -0700 | [diff] [blame] | 367 | |
Ingo Molnar | 4c61afc | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 368 | pte_t *pte = lookup_address(addr, &level); |
| 369 | |
Thomas Gleixner | 7bfb72e | 2008-02-04 16:48:08 +0100 | [diff] [blame] | 370 | /* |
Ingo Molnar | 4c61afc | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 371 | * Only flush present addresses: |
| 372 | */ |
Thomas Gleixner | 57a6a46 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 373 | if (pte && (pte_val(*pte) & _PAGE_PRESENT)) |
Peter Zijlstra | 0521e8b | 2019-02-08 13:08:59 +0100 | [diff] [blame] | 374 | clflush_cache_range_opt((void *)fix_addr(addr), PAGE_SIZE); |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 375 | } |
Peter Zijlstra | c38116b | 2018-12-03 18:03:52 +0100 | [diff] [blame] | 376 | mb(); |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 377 | } |
| 378 | |
Thomas Gleixner | 91ee8f5 | 2018-09-17 16:29:10 +0200 | [diff] [blame] | 379 | static bool overlaps(unsigned long r1_start, unsigned long r1_end, |
| 380 | unsigned long r2_start, unsigned long r2_end) |
| 381 | { |
| 382 | return (r1_start <= r2_end && r1_end >= r2_start) || |
| 383 | (r2_start <= r1_end && r2_end >= r1_start); |
| 384 | } |
| 385 | |
Thomas Gleixner | afd7969 | 2018-09-17 16:29:09 +0200 | [diff] [blame] | 386 | #ifdef CONFIG_PCI_BIOS |
| 387 | /* |
| 388 | * The BIOS area between 640k and 1Mb needs to be executable for PCI BIOS |
| 389 | * based config access (CONFIG_PCI_GOBIOS) support. |
| 390 | */ |
| 391 | #define BIOS_PFN PFN_DOWN(BIOS_BEGIN) |
Thomas Gleixner | 91ee8f5 | 2018-09-17 16:29:10 +0200 | [diff] [blame] | 392 | #define BIOS_PFN_END PFN_DOWN(BIOS_END - 1) |
Thomas Gleixner | afd7969 | 2018-09-17 16:29:09 +0200 | [diff] [blame] | 393 | |
Thomas Gleixner | 91ee8f5 | 2018-09-17 16:29:10 +0200 | [diff] [blame] | 394 | static pgprotval_t protect_pci_bios(unsigned long spfn, unsigned long epfn) |
Thomas Gleixner | afd7969 | 2018-09-17 16:29:09 +0200 | [diff] [blame] | 395 | { |
Thomas Gleixner | 91ee8f5 | 2018-09-17 16:29:10 +0200 | [diff] [blame] | 396 | if (pcibios_enabled && overlaps(spfn, epfn, BIOS_PFN, BIOS_PFN_END)) |
Thomas Gleixner | afd7969 | 2018-09-17 16:29:09 +0200 | [diff] [blame] | 397 | return _PAGE_NX; |
| 398 | return 0; |
| 399 | } |
| 400 | #else |
Thomas Gleixner | 91ee8f5 | 2018-09-17 16:29:10 +0200 | [diff] [blame] | 401 | static pgprotval_t protect_pci_bios(unsigned long spfn, unsigned long epfn) |
Thomas Gleixner | afd7969 | 2018-09-17 16:29:09 +0200 | [diff] [blame] | 402 | { |
| 403 | return 0; |
| 404 | } |
| 405 | #endif |
| 406 | |
| 407 | /* |
| 408 | * The .rodata section needs to be read-only. Using the pfn catches all |
| 409 | * aliases. This also includes __ro_after_init, so do not enforce until |
| 410 | * kernel_set_to_readonly is true. |
| 411 | */ |
Thomas Gleixner | 91ee8f5 | 2018-09-17 16:29:10 +0200 | [diff] [blame] | 412 | static pgprotval_t protect_rodata(unsigned long spfn, unsigned long epfn) |
Thomas Gleixner | afd7969 | 2018-09-17 16:29:09 +0200 | [diff] [blame] | 413 | { |
Thomas Gleixner | 91ee8f5 | 2018-09-17 16:29:10 +0200 | [diff] [blame] | 414 | unsigned long epfn_ro, spfn_ro = PFN_DOWN(__pa_symbol(__start_rodata)); |
Thomas Gleixner | afd7969 | 2018-09-17 16:29:09 +0200 | [diff] [blame] | 415 | |
Thomas Gleixner | 91ee8f5 | 2018-09-17 16:29:10 +0200 | [diff] [blame] | 416 | /* |
| 417 | * Note: __end_rodata is at page aligned and not inclusive, so |
| 418 | * subtract 1 to get the last enforced PFN in the rodata area. |
| 419 | */ |
| 420 | epfn_ro = PFN_DOWN(__pa_symbol(__end_rodata)) - 1; |
| 421 | |
| 422 | if (kernel_set_to_readonly && overlaps(spfn, epfn, spfn_ro, epfn_ro)) |
Thomas Gleixner | afd7969 | 2018-09-17 16:29:09 +0200 | [diff] [blame] | 423 | return _PAGE_RW; |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | * Protect kernel text against becoming non executable by forbidding |
| 429 | * _PAGE_NX. This protects only the high kernel mapping (_text -> _etext) |
| 430 | * out of which the kernel actually executes. Do not protect the low |
| 431 | * mapping. |
| 432 | * |
| 433 | * This does not cover __inittext since that is gone after boot. |
| 434 | */ |
Thomas Gleixner | 91ee8f5 | 2018-09-17 16:29:10 +0200 | [diff] [blame] | 435 | static pgprotval_t protect_kernel_text(unsigned long start, unsigned long end) |
Thomas Gleixner | afd7969 | 2018-09-17 16:29:09 +0200 | [diff] [blame] | 436 | { |
Thomas Gleixner | 91ee8f5 | 2018-09-17 16:29:10 +0200 | [diff] [blame] | 437 | unsigned long t_end = (unsigned long)_etext - 1; |
| 438 | unsigned long t_start = (unsigned long)_text; |
| 439 | |
| 440 | if (overlaps(start, end, t_start, t_end)) |
Thomas Gleixner | afd7969 | 2018-09-17 16:29:09 +0200 | [diff] [blame] | 441 | return _PAGE_NX; |
| 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | #if defined(CONFIG_X86_64) |
| 446 | /* |
| 447 | * Once the kernel maps the text as RO (kernel_set_to_readonly is set), |
| 448 | * kernel text mappings for the large page aligned text, rodata sections |
| 449 | * will be always read-only. For the kernel identity mappings covering the |
| 450 | * holes caused by this alignment can be anything that user asks. |
| 451 | * |
| 452 | * This will preserve the large page mappings for kernel text/data at no |
| 453 | * extra cost. |
| 454 | */ |
Thomas Gleixner | 91ee8f5 | 2018-09-17 16:29:10 +0200 | [diff] [blame] | 455 | static pgprotval_t protect_kernel_text_ro(unsigned long start, |
| 456 | unsigned long end) |
Thomas Gleixner | afd7969 | 2018-09-17 16:29:09 +0200 | [diff] [blame] | 457 | { |
Thomas Gleixner | 91ee8f5 | 2018-09-17 16:29:10 +0200 | [diff] [blame] | 458 | unsigned long t_end = (unsigned long)__end_rodata_hpage_align - 1; |
| 459 | unsigned long t_start = (unsigned long)_text; |
Thomas Gleixner | afd7969 | 2018-09-17 16:29:09 +0200 | [diff] [blame] | 460 | unsigned int level; |
| 461 | |
Thomas Gleixner | 91ee8f5 | 2018-09-17 16:29:10 +0200 | [diff] [blame] | 462 | if (!kernel_set_to_readonly || !overlaps(start, end, t_start, t_end)) |
Thomas Gleixner | afd7969 | 2018-09-17 16:29:09 +0200 | [diff] [blame] | 463 | return 0; |
| 464 | /* |
| 465 | * Don't enforce the !RW mapping for the kernel text mapping, if |
| 466 | * the current mapping is already using small page mapping. No |
| 467 | * need to work hard to preserve large page mappings in this case. |
| 468 | * |
| 469 | * This also fixes the Linux Xen paravirt guest boot failure caused |
| 470 | * by unexpected read-only mappings for kernel identity |
| 471 | * mappings. In this paravirt guest case, the kernel text mapping |
| 472 | * and the kernel identity mapping share the same page-table pages, |
| 473 | * so the protections for kernel text and identity mappings have to |
| 474 | * be the same. |
| 475 | */ |
Thomas Gleixner | 91ee8f5 | 2018-09-17 16:29:10 +0200 | [diff] [blame] | 476 | if (lookup_address(start, &level) && (level != PG_LEVEL_4K)) |
Thomas Gleixner | afd7969 | 2018-09-17 16:29:09 +0200 | [diff] [blame] | 477 | return _PAGE_RW; |
| 478 | return 0; |
| 479 | } |
| 480 | #else |
Thomas Gleixner | 91ee8f5 | 2018-09-17 16:29:10 +0200 | [diff] [blame] | 481 | static pgprotval_t protect_kernel_text_ro(unsigned long start, |
| 482 | unsigned long end) |
Thomas Gleixner | afd7969 | 2018-09-17 16:29:09 +0200 | [diff] [blame] | 483 | { |
| 484 | return 0; |
| 485 | } |
| 486 | #endif |
| 487 | |
Thomas Gleixner | 4046460 | 2018-09-17 16:29:11 +0200 | [diff] [blame] | 488 | static inline bool conflicts(pgprot_t prot, pgprotval_t val) |
| 489 | { |
| 490 | return (pgprot_val(prot) & ~val) != pgprot_val(prot); |
| 491 | } |
| 492 | |
| 493 | static inline void check_conflict(int warnlvl, pgprot_t prot, pgprotval_t val, |
| 494 | unsigned long start, unsigned long end, |
| 495 | unsigned long pfn, const char *txt) |
| 496 | { |
| 497 | static const char *lvltxt[] = { |
Thomas Gleixner | f61c5ba | 2018-09-17 16:29:14 +0200 | [diff] [blame] | 498 | [CPA_CONFLICT] = "conflict", |
Thomas Gleixner | 4046460 | 2018-09-17 16:29:11 +0200 | [diff] [blame] | 499 | [CPA_PROTECT] = "protect", |
| 500 | [CPA_DETECT] = "detect", |
| 501 | }; |
| 502 | |
| 503 | if (warnlvl > cpa_warn_level || !conflicts(prot, val)) |
| 504 | return; |
| 505 | |
| 506 | pr_warn("CPA %8s %10s: 0x%016lx - 0x%016lx PFN %lx req %016llx prevent %016llx\n", |
| 507 | lvltxt[warnlvl], txt, start, end, pfn, (unsigned long long)pgprot_val(prot), |
| 508 | (unsigned long long)val); |
| 509 | } |
| 510 | |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 511 | /* |
Arjan van de Ven | ed724be | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 512 | * Certain areas of memory on x86 require very specific protection flags, |
| 513 | * for example the BIOS area or kernel text. Callers don't always get this |
| 514 | * right (again, ioremap() on BIOS memory is not uncommon) so this function |
| 515 | * checks and fixes these known static required protection bits. |
| 516 | */ |
Thomas Gleixner | 91ee8f5 | 2018-09-17 16:29:10 +0200 | [diff] [blame] | 517 | static inline pgprot_t static_protections(pgprot_t prot, unsigned long start, |
Thomas Gleixner | 4046460 | 2018-09-17 16:29:11 +0200 | [diff] [blame] | 518 | unsigned long pfn, unsigned long npg, |
Thomas Gleixner | 7af0145 | 2019-08-29 00:31:34 +0200 | [diff] [blame] | 519 | unsigned long lpsize, int warnlvl) |
Arjan van de Ven | ed724be | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 520 | { |
Thomas Gleixner | 4046460 | 2018-09-17 16:29:11 +0200 | [diff] [blame] | 521 | pgprotval_t forbidden, res; |
Thomas Gleixner | 91ee8f5 | 2018-09-17 16:29:10 +0200 | [diff] [blame] | 522 | unsigned long end; |
Arjan van de Ven | ed724be | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 523 | |
Thomas Gleixner | 69c31e6 | 2018-09-17 16:29:13 +0200 | [diff] [blame] | 524 | /* |
| 525 | * There is no point in checking RW/NX conflicts when the requested |
| 526 | * mapping is setting the page !PRESENT. |
| 527 | */ |
| 528 | if (!(pgprot_val(prot) & _PAGE_PRESENT)) |
| 529 | return prot; |
| 530 | |
Thomas Gleixner | afd7969 | 2018-09-17 16:29:09 +0200 | [diff] [blame] | 531 | /* Operate on the virtual address */ |
Thomas Gleixner | 91ee8f5 | 2018-09-17 16:29:10 +0200 | [diff] [blame] | 532 | end = start + npg * PAGE_SIZE - 1; |
Thomas Gleixner | 4046460 | 2018-09-17 16:29:11 +0200 | [diff] [blame] | 533 | |
| 534 | res = protect_kernel_text(start, end); |
| 535 | check_conflict(warnlvl, prot, res, start, end, pfn, "Text NX"); |
| 536 | forbidden = res; |
| 537 | |
Thomas Gleixner | 7af0145 | 2019-08-29 00:31:34 +0200 | [diff] [blame] | 538 | /* |
| 539 | * Special case to preserve a large page. If the change spawns the |
| 540 | * full large page mapping then there is no point to split it |
| 541 | * up. Happens with ftrace and is going to be removed once ftrace |
| 542 | * switched to text_poke(). |
| 543 | */ |
| 544 | if (lpsize != (npg * PAGE_SIZE) || (start & (lpsize - 1))) { |
| 545 | res = protect_kernel_text_ro(start, end); |
| 546 | check_conflict(warnlvl, prot, res, start, end, pfn, "Text RO"); |
| 547 | forbidden |= res; |
| 548 | } |
Arjan van de Ven | ed724be | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 549 | |
Thomas Gleixner | afd7969 | 2018-09-17 16:29:09 +0200 | [diff] [blame] | 550 | /* Check the PFN directly */ |
Thomas Gleixner | 4046460 | 2018-09-17 16:29:11 +0200 | [diff] [blame] | 551 | res = protect_pci_bios(pfn, pfn + npg - 1); |
| 552 | check_conflict(warnlvl, prot, res, start, end, pfn, "PCIBIOS NX"); |
| 553 | forbidden |= res; |
| 554 | |
| 555 | res = protect_rodata(pfn, pfn + npg - 1); |
| 556 | check_conflict(warnlvl, prot, res, start, end, pfn, "Rodata RO"); |
| 557 | forbidden |= res; |
Arjan van de Ven | cc0f21b | 2008-02-04 16:48:05 +0100 | [diff] [blame] | 558 | |
Thomas Gleixner | afd7969 | 2018-09-17 16:29:09 +0200 | [diff] [blame] | 559 | return __pgprot(pgprot_val(prot) & ~forbidden); |
Ingo Molnar | 687c482 | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 560 | } |
| 561 | |
Matt Fleming | 426e34c | 2013-12-06 21:13:04 +0000 | [diff] [blame] | 562 | /* |
| 563 | * Lookup the page table entry for a virtual address in a specific pgd. |
| 564 | * Return a pointer to the entry and the level of the mapping. |
| 565 | */ |
| 566 | pte_t *lookup_address_in_pgd(pgd_t *pgd, unsigned long address, |
| 567 | unsigned int *level) |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 568 | { |
Kirill A. Shutemov | 4547833 | 2017-03-17 21:55:12 +0300 | [diff] [blame] | 569 | p4d_t *p4d; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | pud_t *pud; |
| 571 | pmd_t *pmd; |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 572 | |
Thomas Gleixner | 30551bb | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 573 | *level = PG_LEVEL_NONE; |
| 574 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | if (pgd_none(*pgd)) |
| 576 | return NULL; |
Ingo Molnar | 9df8499 | 2008-02-04 16:48:09 +0100 | [diff] [blame] | 577 | |
Kirill A. Shutemov | 4547833 | 2017-03-17 21:55:12 +0300 | [diff] [blame] | 578 | p4d = p4d_offset(pgd, address); |
| 579 | if (p4d_none(*p4d)) |
| 580 | return NULL; |
| 581 | |
| 582 | *level = PG_LEVEL_512G; |
| 583 | if (p4d_large(*p4d) || !p4d_present(*p4d)) |
| 584 | return (pte_t *)p4d; |
| 585 | |
| 586 | pud = pud_offset(p4d, address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | if (pud_none(*pud)) |
| 588 | return NULL; |
Andi Kleen | c2f71ee | 2008-02-04 16:48:09 +0100 | [diff] [blame] | 589 | |
| 590 | *level = PG_LEVEL_1G; |
| 591 | if (pud_large(*pud) || !pud_present(*pud)) |
| 592 | return (pte_t *)pud; |
| 593 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | pmd = pmd_offset(pud, address); |
| 595 | if (pmd_none(*pmd)) |
| 596 | return NULL; |
Thomas Gleixner | 30551bb | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 597 | |
| 598 | *level = PG_LEVEL_2M; |
Thomas Gleixner | 9a14aef | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 599 | if (pmd_large(*pmd) || !pmd_present(*pmd)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | return (pte_t *)pmd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | |
Thomas Gleixner | 30551bb | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 602 | *level = PG_LEVEL_4K; |
Ingo Molnar | 9df8499 | 2008-02-04 16:48:09 +0100 | [diff] [blame] | 603 | |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 604 | return pte_offset_kernel(pmd, address); |
| 605 | } |
Borislav Petkov | 0fd64c2 | 2013-10-31 17:25:00 +0100 | [diff] [blame] | 606 | |
| 607 | /* |
| 608 | * Lookup the page table entry for a virtual address. Return a pointer |
| 609 | * to the entry and the level of the mapping. |
| 610 | * |
| 611 | * Note: We return pud and pmd either when the entry is marked large |
| 612 | * or when the present bit is not set. Otherwise we would return a |
| 613 | * pointer to a nonexisting mapping. |
| 614 | */ |
| 615 | pte_t *lookup_address(unsigned long address, unsigned int *level) |
| 616 | { |
Thomas Gleixner | 8679de0 | 2018-09-17 16:29:08 +0200 | [diff] [blame] | 617 | return lookup_address_in_pgd(pgd_offset_k(address), address, level); |
Borislav Petkov | 0fd64c2 | 2013-10-31 17:25:00 +0100 | [diff] [blame] | 618 | } |
Pekka Paalanen | 75bb883 | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 619 | EXPORT_SYMBOL_GPL(lookup_address); |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 620 | |
Borislav Petkov | 0fd64c2 | 2013-10-31 17:25:00 +0100 | [diff] [blame] | 621 | static pte_t *_lookup_address_cpa(struct cpa_data *cpa, unsigned long address, |
| 622 | unsigned int *level) |
| 623 | { |
Thomas Gleixner | 8679de0 | 2018-09-17 16:29:08 +0200 | [diff] [blame] | 624 | if (cpa->pgd) |
Matt Fleming | 426e34c | 2013-12-06 21:13:04 +0000 | [diff] [blame] | 625 | return lookup_address_in_pgd(cpa->pgd + pgd_index(address), |
Borislav Petkov | 0fd64c2 | 2013-10-31 17:25:00 +0100 | [diff] [blame] | 626 | address, level); |
| 627 | |
Thomas Gleixner | 8679de0 | 2018-09-17 16:29:08 +0200 | [diff] [blame] | 628 | return lookup_address(address, level); |
Borislav Petkov | 0fd64c2 | 2013-10-31 17:25:00 +0100 | [diff] [blame] | 629 | } |
| 630 | |
Ingo Molnar | 9df8499 | 2008-02-04 16:48:09 +0100 | [diff] [blame] | 631 | /* |
Juergen Gross | 792230c | 2014-11-28 11:53:56 +0100 | [diff] [blame] | 632 | * Lookup the PMD entry for a virtual address. Return a pointer to the entry |
| 633 | * or NULL if not present. |
| 634 | */ |
| 635 | pmd_t *lookup_pmd_address(unsigned long address) |
| 636 | { |
| 637 | pgd_t *pgd; |
Kirill A. Shutemov | 4547833 | 2017-03-17 21:55:12 +0300 | [diff] [blame] | 638 | p4d_t *p4d; |
Juergen Gross | 792230c | 2014-11-28 11:53:56 +0100 | [diff] [blame] | 639 | pud_t *pud; |
| 640 | |
| 641 | pgd = pgd_offset_k(address); |
| 642 | if (pgd_none(*pgd)) |
| 643 | return NULL; |
| 644 | |
Kirill A. Shutemov | 4547833 | 2017-03-17 21:55:12 +0300 | [diff] [blame] | 645 | p4d = p4d_offset(pgd, address); |
| 646 | if (p4d_none(*p4d) || p4d_large(*p4d) || !p4d_present(*p4d)) |
| 647 | return NULL; |
| 648 | |
| 649 | pud = pud_offset(p4d, address); |
Juergen Gross | 792230c | 2014-11-28 11:53:56 +0100 | [diff] [blame] | 650 | if (pud_none(*pud) || pud_large(*pud) || !pud_present(*pud)) |
| 651 | return NULL; |
| 652 | |
| 653 | return pmd_offset(pud, address); |
| 654 | } |
| 655 | |
| 656 | /* |
Dave Hansen | d765653 | 2013-01-22 13:24:33 -0800 | [diff] [blame] | 657 | * This is necessary because __pa() does not work on some |
| 658 | * kinds of memory, like vmalloc() or the alloc_remap() |
| 659 | * areas on 32-bit NUMA systems. The percpu areas can |
| 660 | * end up in this kind of memory, for instance. |
| 661 | * |
| 662 | * This could be optimized, but it is only intended to be |
| 663 | * used at inititalization time, and keeping it |
| 664 | * unoptimized should increase the testing coverage for |
| 665 | * the more obscure platforms. |
| 666 | */ |
| 667 | phys_addr_t slow_virt_to_phys(void *__virt_addr) |
| 668 | { |
| 669 | unsigned long virt_addr = (unsigned long)__virt_addr; |
Dexuan Cui | bf70e55 | 2016-02-25 01:58:12 -0800 | [diff] [blame] | 670 | phys_addr_t phys_addr; |
| 671 | unsigned long offset; |
Dave Hansen | d765653 | 2013-01-22 13:24:33 -0800 | [diff] [blame] | 672 | enum pg_level level; |
Dave Hansen | d765653 | 2013-01-22 13:24:33 -0800 | [diff] [blame] | 673 | pte_t *pte; |
| 674 | |
| 675 | pte = lookup_address(virt_addr, &level); |
| 676 | BUG_ON(!pte); |
Toshi Kani | 34437e6 | 2015-09-17 12:24:20 -0600 | [diff] [blame] | 677 | |
Dexuan Cui | bf70e55 | 2016-02-25 01:58:12 -0800 | [diff] [blame] | 678 | /* |
| 679 | * pXX_pfn() returns unsigned long, which must be cast to phys_addr_t |
| 680 | * before being left-shifted PAGE_SHIFT bits -- this trick is to |
| 681 | * make 32-PAE kernel work correctly. |
| 682 | */ |
Toshi Kani | 34437e6 | 2015-09-17 12:24:20 -0600 | [diff] [blame] | 683 | switch (level) { |
| 684 | case PG_LEVEL_1G: |
Dexuan Cui | bf70e55 | 2016-02-25 01:58:12 -0800 | [diff] [blame] | 685 | phys_addr = (phys_addr_t)pud_pfn(*(pud_t *)pte) << PAGE_SHIFT; |
Toshi Kani | 34437e6 | 2015-09-17 12:24:20 -0600 | [diff] [blame] | 686 | offset = virt_addr & ~PUD_PAGE_MASK; |
| 687 | break; |
| 688 | case PG_LEVEL_2M: |
Dexuan Cui | bf70e55 | 2016-02-25 01:58:12 -0800 | [diff] [blame] | 689 | phys_addr = (phys_addr_t)pmd_pfn(*(pmd_t *)pte) << PAGE_SHIFT; |
Toshi Kani | 34437e6 | 2015-09-17 12:24:20 -0600 | [diff] [blame] | 690 | offset = virt_addr & ~PMD_PAGE_MASK; |
| 691 | break; |
| 692 | default: |
Dexuan Cui | bf70e55 | 2016-02-25 01:58:12 -0800 | [diff] [blame] | 693 | phys_addr = (phys_addr_t)pte_pfn(*pte) << PAGE_SHIFT; |
Toshi Kani | 34437e6 | 2015-09-17 12:24:20 -0600 | [diff] [blame] | 694 | offset = virt_addr & ~PAGE_MASK; |
| 695 | } |
| 696 | |
| 697 | return (phys_addr_t)(phys_addr | offset); |
Dave Hansen | d765653 | 2013-01-22 13:24:33 -0800 | [diff] [blame] | 698 | } |
| 699 | EXPORT_SYMBOL_GPL(slow_virt_to_phys); |
| 700 | |
| 701 | /* |
Ingo Molnar | 9df8499 | 2008-02-04 16:48:09 +0100 | [diff] [blame] | 702 | * Set the new pmd in all the pgds we know about: |
| 703 | */ |
Ingo Molnar | 9a3dc78 | 2008-01-30 13:33:57 +0100 | [diff] [blame] | 704 | static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte) |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 705 | { |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 706 | /* change init_mm */ |
| 707 | set_pte_atomic(kpte, pte); |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 708 | #ifdef CONFIG_X86_32 |
Ingo Molnar | e4b71dc | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 709 | if (!SHARED_KERNEL_PMD) { |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 710 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | |
Jeremy Fitzhardinge | e3ed910 | 2008-01-30 13:34:11 +0100 | [diff] [blame] | 712 | list_for_each_entry(page, &pgd_list, lru) { |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 713 | pgd_t *pgd; |
Kirill A. Shutemov | 4547833 | 2017-03-17 21:55:12 +0300 | [diff] [blame] | 714 | p4d_t *p4d; |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 715 | pud_t *pud; |
| 716 | pmd_t *pmd; |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 717 | |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 718 | pgd = (pgd_t *)page_address(page) + pgd_index(address); |
Kirill A. Shutemov | 4547833 | 2017-03-17 21:55:12 +0300 | [diff] [blame] | 719 | p4d = p4d_offset(pgd, address); |
| 720 | pud = pud_offset(p4d, address); |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 721 | pmd = pmd_offset(pud, address); |
| 722 | set_pte_atomic((pte_t *)pmd, pte); |
| 723 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | } |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 725 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | } |
| 727 | |
Dave Hansen | d1440b2 | 2018-04-06 13:55:02 -0700 | [diff] [blame] | 728 | static pgprot_t pgprot_clear_protnone_bits(pgprot_t prot) |
| 729 | { |
| 730 | /* |
| 731 | * _PAGE_GLOBAL means "global page" for present PTEs. |
| 732 | * But, it is also used to indicate _PAGE_PROTNONE |
| 733 | * for non-present PTEs. |
| 734 | * |
| 735 | * This ensures that a _PAGE_GLOBAL PTE going from |
| 736 | * present to non-present is not confused as |
| 737 | * _PAGE_PROTNONE. |
| 738 | */ |
| 739 | if (!(pgprot_val(prot) & _PAGE_PRESENT)) |
| 740 | pgprot_val(prot) &= ~_PAGE_GLOBAL; |
| 741 | |
| 742 | return prot; |
| 743 | } |
| 744 | |
Thomas Gleixner | 8679de0 | 2018-09-17 16:29:08 +0200 | [diff] [blame] | 745 | static int __should_split_large_page(pte_t *kpte, unsigned long address, |
| 746 | struct cpa_data *cpa) |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 747 | { |
Thomas Gleixner | 585948f4 | 2018-09-17 16:29:17 +0200 | [diff] [blame] | 748 | unsigned long numpages, pmask, psize, lpaddr, pfn, old_pfn; |
Thomas Gleixner | f61c5ba | 2018-09-17 16:29:14 +0200 | [diff] [blame] | 749 | pgprot_t old_prot, new_prot, req_prot, chk_prot; |
Qian Cai | 24c4122 | 2019-03-01 10:29:24 -0500 | [diff] [blame] | 750 | pte_t new_pte, *tmp; |
Dave Hansen | f3c4fbb | 2013-01-22 13:24:32 -0800 | [diff] [blame] | 751 | enum pg_level level; |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 752 | |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 753 | /* |
| 754 | * Check for races, another CPU might have split this page |
| 755 | * up already: |
| 756 | */ |
Borislav Petkov | 82f0712 | 2013-10-31 17:25:07 +0100 | [diff] [blame] | 757 | tmp = _lookup_address_cpa(cpa, address, &level); |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 758 | if (tmp != kpte) |
Thomas Gleixner | 8679de0 | 2018-09-17 16:29:08 +0200 | [diff] [blame] | 759 | return 1; |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 760 | |
| 761 | switch (level) { |
| 762 | case PG_LEVEL_2M: |
Toshi Kani | 3a19109 | 2015-09-17 12:24:22 -0600 | [diff] [blame] | 763 | old_prot = pmd_pgprot(*(pmd_t *)kpte); |
| 764 | old_pfn = pmd_pfn(*(pmd_t *)kpte); |
Thomas Gleixner | 5c280cf | 2018-09-17 16:29:12 +0200 | [diff] [blame] | 765 | cpa_inc_2m_checked(); |
Toshi Kani | 3a19109 | 2015-09-17 12:24:22 -0600 | [diff] [blame] | 766 | break; |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 767 | case PG_LEVEL_1G: |
Toshi Kani | 3a19109 | 2015-09-17 12:24:22 -0600 | [diff] [blame] | 768 | old_prot = pud_pgprot(*(pud_t *)kpte); |
| 769 | old_pfn = pud_pfn(*(pud_t *)kpte); |
Thomas Gleixner | 5c280cf | 2018-09-17 16:29:12 +0200 | [diff] [blame] | 770 | cpa_inc_1g_checked(); |
Dave Hansen | f3c4fbb | 2013-01-22 13:24:32 -0800 | [diff] [blame] | 771 | break; |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 772 | default: |
Thomas Gleixner | 8679de0 | 2018-09-17 16:29:08 +0200 | [diff] [blame] | 773 | return -EINVAL; |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 774 | } |
| 775 | |
Toshi Kani | 3a19109 | 2015-09-17 12:24:22 -0600 | [diff] [blame] | 776 | psize = page_level_size(level); |
| 777 | pmask = page_level_mask(level); |
| 778 | |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 779 | /* |
| 780 | * Calculate the number of pages, which fit into this large |
| 781 | * page starting at address: |
| 782 | */ |
Thomas Gleixner | 8679de0 | 2018-09-17 16:29:08 +0200 | [diff] [blame] | 783 | lpaddr = (address + psize) & pmask; |
| 784 | numpages = (lpaddr - address) >> PAGE_SHIFT; |
Rafael J. Wysocki | 9b5cf48 | 2008-03-03 01:17:37 +0100 | [diff] [blame] | 785 | if (numpages < cpa->numpages) |
| 786 | cpa->numpages = numpages; |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 787 | |
| 788 | /* |
| 789 | * We are safe now. Check whether the new pgprot is the same: |
Juergen Gross | f5b2831 | 2014-11-03 14:02:02 +0100 | [diff] [blame] | 790 | * Convert protection attributes to 4k-format, as cpa->mask* are set |
| 791 | * up accordingly. |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 792 | */ |
Qian Cai | 24c4122 | 2019-03-01 10:29:24 -0500 | [diff] [blame] | 793 | |
Dave Hansen | 606c719 | 2018-04-06 13:55:04 -0700 | [diff] [blame] | 794 | /* Clear PSE (aka _PAGE_PAT) and move PAT bit to correct position */ |
Toshi Kani | 55696b1 | 2015-09-17 12:24:24 -0600 | [diff] [blame] | 795 | req_prot = pgprot_large_2_4k(old_prot); |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 796 | |
matthieu castet | 64edc8e | 2010-11-16 22:30:27 +0100 | [diff] [blame] | 797 | pgprot_val(req_prot) &= ~pgprot_val(cpa->mask_clr); |
| 798 | pgprot_val(req_prot) |= pgprot_val(cpa->mask_set); |
Thomas Gleixner | c31c7d4 | 2008-02-18 20:54:14 +0100 | [diff] [blame] | 799 | |
| 800 | /* |
Juergen Gross | f5b2831 | 2014-11-03 14:02:02 +0100 | [diff] [blame] | 801 | * req_prot is in format of 4k pages. It must be converted to large |
| 802 | * page format: the caching mode includes the PAT bit located at |
| 803 | * different bit positions in the two formats. |
| 804 | */ |
| 805 | req_prot = pgprot_4k_2_large(req_prot); |
Dave Hansen | d1440b2 | 2018-04-06 13:55:02 -0700 | [diff] [blame] | 806 | req_prot = pgprot_clear_protnone_bits(req_prot); |
Andrea Arcangeli | f76cfa3 | 2013-04-10 15:28:25 +0200 | [diff] [blame] | 807 | if (pgprot_val(req_prot) & _PAGE_PRESENT) |
Dave Hansen | d1440b2 | 2018-04-06 13:55:02 -0700 | [diff] [blame] | 808 | pgprot_val(req_prot) |= _PAGE_PSE; |
Andrea Arcangeli | a8aed3e | 2013-02-22 15:11:51 -0800 | [diff] [blame] | 809 | |
| 810 | /* |
Thomas Gleixner | 8679de0 | 2018-09-17 16:29:08 +0200 | [diff] [blame] | 811 | * old_pfn points to the large page base pfn. So we need to add the |
| 812 | * offset of the virtual address: |
Thomas Gleixner | c31c7d4 | 2008-02-18 20:54:14 +0100 | [diff] [blame] | 813 | */ |
Toshi Kani | 3a19109 | 2015-09-17 12:24:22 -0600 | [diff] [blame] | 814 | pfn = old_pfn + ((address & (psize - 1)) >> PAGE_SHIFT); |
Thomas Gleixner | c31c7d4 | 2008-02-18 20:54:14 +0100 | [diff] [blame] | 815 | cpa->pfn = pfn; |
| 816 | |
Thomas Gleixner | 8679de0 | 2018-09-17 16:29:08 +0200 | [diff] [blame] | 817 | /* |
| 818 | * Calculate the large page base address and the number of 4K pages |
| 819 | * in the large page |
| 820 | */ |
| 821 | lpaddr = address & pmask; |
| 822 | numpages = psize >> PAGE_SHIFT; |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 823 | |
| 824 | /* |
Thomas Gleixner | f61c5ba | 2018-09-17 16:29:14 +0200 | [diff] [blame] | 825 | * Sanity check that the existing mapping is correct versus the static |
| 826 | * protections. static_protections() guards against !PRESENT, so no |
| 827 | * extra conditional required here. |
| 828 | */ |
| 829 | chk_prot = static_protections(old_prot, lpaddr, old_pfn, numpages, |
Thomas Gleixner | 7af0145 | 2019-08-29 00:31:34 +0200 | [diff] [blame] | 830 | psize, CPA_CONFLICT); |
Thomas Gleixner | f61c5ba | 2018-09-17 16:29:14 +0200 | [diff] [blame] | 831 | |
| 832 | if (WARN_ON_ONCE(pgprot_val(chk_prot) != pgprot_val(old_prot))) { |
| 833 | /* |
| 834 | * Split the large page and tell the split code to |
| 835 | * enforce static protections. |
| 836 | */ |
| 837 | cpa->force_static_prot = 1; |
| 838 | return 1; |
| 839 | } |
| 840 | |
| 841 | /* |
Thomas Gleixner | 1c4b406 | 2018-09-17 16:29:15 +0200 | [diff] [blame] | 842 | * Optimization: If the requested pgprot is the same as the current |
| 843 | * pgprot, then the large page can be preserved and no updates are |
| 844 | * required independent of alignment and length of the requested |
| 845 | * range. The above already established that the current pgprot is |
| 846 | * correct, which in consequence makes the requested pgprot correct |
| 847 | * as well if it is the same. The static protection scan below will |
| 848 | * not come to a different conclusion. |
| 849 | */ |
| 850 | if (pgprot_val(req_prot) == pgprot_val(old_prot)) { |
| 851 | cpa_inc_lp_sameprot(level); |
| 852 | return 0; |
| 853 | } |
| 854 | |
| 855 | /* |
Thomas Gleixner | 585948f4 | 2018-09-17 16:29:17 +0200 | [diff] [blame] | 856 | * If the requested range does not cover the full page, split it up |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 857 | */ |
Thomas Gleixner | 8679de0 | 2018-09-17 16:29:08 +0200 | [diff] [blame] | 858 | if (address != lpaddr || cpa->numpages != numpages) |
| 859 | return 1; |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 860 | |
Thomas Gleixner | 585948f4 | 2018-09-17 16:29:17 +0200 | [diff] [blame] | 861 | /* |
| 862 | * Check whether the requested pgprot is conflicting with a static |
| 863 | * protection requirement in the large page. |
| 864 | */ |
| 865 | new_prot = static_protections(req_prot, lpaddr, old_pfn, numpages, |
Thomas Gleixner | 7af0145 | 2019-08-29 00:31:34 +0200 | [diff] [blame] | 866 | psize, CPA_DETECT); |
Thomas Gleixner | 585948f4 | 2018-09-17 16:29:17 +0200 | [diff] [blame] | 867 | |
| 868 | /* |
| 869 | * If there is a conflict, split the large page. |
| 870 | * |
| 871 | * There used to be a 4k wise evaluation trying really hard to |
| 872 | * preserve the large pages, but experimentation has shown, that this |
| 873 | * does not help at all. There might be corner cases which would |
| 874 | * preserve one large page occasionally, but it's really not worth the |
| 875 | * extra code and cycles for the common case. |
| 876 | */ |
| 877 | if (pgprot_val(req_prot) != pgprot_val(new_prot)) |
| 878 | return 1; |
| 879 | |
Thomas Gleixner | 8679de0 | 2018-09-17 16:29:08 +0200 | [diff] [blame] | 880 | /* All checks passed. Update the large page mapping. */ |
| 881 | new_pte = pfn_pte(old_pfn, new_prot); |
| 882 | __set_pmd_pte(kpte, address, new_pte); |
| 883 | cpa->flags |= CPA_FLUSHTLB; |
Thomas Gleixner | 5c280cf | 2018-09-17 16:29:12 +0200 | [diff] [blame] | 884 | cpa_inc_lp_preserved(level); |
Thomas Gleixner | 8679de0 | 2018-09-17 16:29:08 +0200 | [diff] [blame] | 885 | return 0; |
| 886 | } |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 887 | |
Thomas Gleixner | 8679de0 | 2018-09-17 16:29:08 +0200 | [diff] [blame] | 888 | static int should_split_large_page(pte_t *kpte, unsigned long address, |
| 889 | struct cpa_data *cpa) |
| 890 | { |
| 891 | int do_split; |
| 892 | |
| 893 | if (cpa->force_split) |
| 894 | return 1; |
| 895 | |
| 896 | spin_lock(&pgd_lock); |
| 897 | do_split = __should_split_large_page(kpte, address, cpa); |
Andrea Arcangeli | a79e53d | 2011-02-16 15:45:22 -0800 | [diff] [blame] | 898 | spin_unlock(&pgd_lock); |
Ingo Molnar | 9df8499 | 2008-02-04 16:48:09 +0100 | [diff] [blame] | 899 | |
Ingo Molnar | beaff63 | 2008-02-04 16:48:09 +0100 | [diff] [blame] | 900 | return do_split; |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 901 | } |
| 902 | |
Thomas Gleixner | f61c5ba | 2018-09-17 16:29:14 +0200 | [diff] [blame] | 903 | static void split_set_pte(struct cpa_data *cpa, pte_t *pte, unsigned long pfn, |
| 904 | pgprot_t ref_prot, unsigned long address, |
| 905 | unsigned long size) |
| 906 | { |
| 907 | unsigned int npg = PFN_DOWN(size); |
| 908 | pgprot_t prot; |
| 909 | |
| 910 | /* |
| 911 | * If should_split_large_page() discovered an inconsistent mapping, |
| 912 | * remove the invalid protection in the split mapping. |
| 913 | */ |
| 914 | if (!cpa->force_static_prot) |
| 915 | goto set; |
| 916 | |
Thomas Gleixner | 7af0145 | 2019-08-29 00:31:34 +0200 | [diff] [blame] | 917 | /* Hand in lpsize = 0 to enforce the protection mechanism */ |
| 918 | prot = static_protections(ref_prot, address, pfn, npg, 0, CPA_PROTECT); |
Thomas Gleixner | f61c5ba | 2018-09-17 16:29:14 +0200 | [diff] [blame] | 919 | |
| 920 | if (pgprot_val(prot) == pgprot_val(ref_prot)) |
| 921 | goto set; |
| 922 | |
| 923 | /* |
| 924 | * If this is splitting a PMD, fix it up. PUD splits cannot be |
| 925 | * fixed trivially as that would require to rescan the newly |
| 926 | * installed PMD mappings after returning from split_large_page() |
| 927 | * so an eventual further split can allocate the necessary PTE |
| 928 | * pages. Warn for now and revisit it in case this actually |
| 929 | * happens. |
| 930 | */ |
| 931 | if (size == PAGE_SIZE) |
| 932 | ref_prot = prot; |
| 933 | else |
| 934 | pr_warn_once("CPA: Cannot fixup static protections for PUD split\n"); |
| 935 | set: |
| 936 | set_pte(pte, pfn_pte(pfn, ref_prot)); |
| 937 | } |
| 938 | |
Borislav Petkov | 5952886 | 2013-03-21 18:16:57 +0100 | [diff] [blame] | 939 | static int |
Borislav Petkov | 82f0712 | 2013-10-31 17:25:07 +0100 | [diff] [blame] | 940 | __split_large_page(struct cpa_data *cpa, pte_t *kpte, unsigned long address, |
| 941 | struct page *base) |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 942 | { |
Thomas Gleixner | f61c5ba | 2018-09-17 16:29:14 +0200 | [diff] [blame] | 943 | unsigned long lpaddr, lpinc, ref_pfn, pfn, pfninc = 1; |
Borislav Petkov | 5952886 | 2013-03-21 18:16:57 +0100 | [diff] [blame] | 944 | pte_t *pbase = (pte_t *)page_address(base); |
Ingo Molnar | 86f0398 | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 945 | unsigned int i, level; |
Ingo Molnar | 9df8499 | 2008-02-04 16:48:09 +0100 | [diff] [blame] | 946 | pgprot_t ref_prot; |
Thomas Gleixner | f61c5ba | 2018-09-17 16:29:14 +0200 | [diff] [blame] | 947 | pte_t *tmp; |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 948 | |
Andrea Arcangeli | a79e53d | 2011-02-16 15:45:22 -0800 | [diff] [blame] | 949 | spin_lock(&pgd_lock); |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 950 | /* |
| 951 | * Check for races, another CPU might have split this page |
| 952 | * up for us already: |
| 953 | */ |
Borislav Petkov | 82f0712 | 2013-10-31 17:25:07 +0100 | [diff] [blame] | 954 | tmp = _lookup_address_cpa(cpa, address, &level); |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 955 | if (tmp != kpte) { |
| 956 | spin_unlock(&pgd_lock); |
| 957 | return 1; |
| 958 | } |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 959 | |
Jeremy Fitzhardinge | 6944a9c | 2008-03-17 16:37:01 -0700 | [diff] [blame] | 960 | paravirt_alloc_pte(&init_mm, page_to_pfn(base)); |
Juergen Gross | f5b2831 | 2014-11-03 14:02:02 +0100 | [diff] [blame] | 961 | |
Toshi Kani | d551aaa | 2015-09-17 12:24:23 -0600 | [diff] [blame] | 962 | switch (level) { |
| 963 | case PG_LEVEL_2M: |
| 964 | ref_prot = pmd_pgprot(*(pmd_t *)kpte); |
Dave Hansen | 606c719 | 2018-04-06 13:55:04 -0700 | [diff] [blame] | 965 | /* |
| 966 | * Clear PSE (aka _PAGE_PAT) and move |
| 967 | * PAT bit to correct position. |
| 968 | */ |
Juergen Gross | f5b2831 | 2014-11-03 14:02:02 +0100 | [diff] [blame] | 969 | ref_prot = pgprot_large_2_4k(ref_prot); |
Toshi Kani | d551aaa | 2015-09-17 12:24:23 -0600 | [diff] [blame] | 970 | ref_pfn = pmd_pfn(*(pmd_t *)kpte); |
Thomas Gleixner | f61c5ba | 2018-09-17 16:29:14 +0200 | [diff] [blame] | 971 | lpaddr = address & PMD_MASK; |
| 972 | lpinc = PAGE_SIZE; |
Toshi Kani | d551aaa | 2015-09-17 12:24:23 -0600 | [diff] [blame] | 973 | break; |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 974 | |
Toshi Kani | d551aaa | 2015-09-17 12:24:23 -0600 | [diff] [blame] | 975 | case PG_LEVEL_1G: |
| 976 | ref_prot = pud_pgprot(*(pud_t *)kpte); |
| 977 | ref_pfn = pud_pfn(*(pud_t *)kpte); |
Andi Kleen | f07333f | 2008-02-04 16:48:09 +0100 | [diff] [blame] | 978 | pfninc = PMD_PAGE_SIZE >> PAGE_SHIFT; |
Thomas Gleixner | f61c5ba | 2018-09-17 16:29:14 +0200 | [diff] [blame] | 979 | lpaddr = address & PUD_MASK; |
| 980 | lpinc = PMD_SIZE; |
Andrea Arcangeli | a8aed3e | 2013-02-22 15:11:51 -0800 | [diff] [blame] | 981 | /* |
Toshi Kani | d551aaa | 2015-09-17 12:24:23 -0600 | [diff] [blame] | 982 | * Clear the PSE flags if the PRESENT flag is not set |
Andrea Arcangeli | a8aed3e | 2013-02-22 15:11:51 -0800 | [diff] [blame] | 983 | * otherwise pmd_present/pmd_huge will return true |
| 984 | * even on a non present pmd. |
| 985 | */ |
Toshi Kani | d551aaa | 2015-09-17 12:24:23 -0600 | [diff] [blame] | 986 | if (!(pgprot_val(ref_prot) & _PAGE_PRESENT)) |
Andrea Arcangeli | a8aed3e | 2013-02-22 15:11:51 -0800 | [diff] [blame] | 987 | pgprot_val(ref_prot) &= ~_PAGE_PSE; |
Toshi Kani | d551aaa | 2015-09-17 12:24:23 -0600 | [diff] [blame] | 988 | break; |
| 989 | |
| 990 | default: |
| 991 | spin_unlock(&pgd_lock); |
| 992 | return 1; |
Andi Kleen | f07333f | 2008-02-04 16:48:09 +0100 | [diff] [blame] | 993 | } |
Andi Kleen | f07333f | 2008-02-04 16:48:09 +0100 | [diff] [blame] | 994 | |
Dave Hansen | d1440b2 | 2018-04-06 13:55:02 -0700 | [diff] [blame] | 995 | ref_prot = pgprot_clear_protnone_bits(ref_prot); |
Andrea Arcangeli | a8aed3e | 2013-02-22 15:11:51 -0800 | [diff] [blame] | 996 | |
| 997 | /* |
Thomas Gleixner | 63c1dcf | 2008-02-04 16:48:05 +0100 | [diff] [blame] | 998 | * Get the target pfn from the original entry: |
| 999 | */ |
Toshi Kani | d551aaa | 2015-09-17 12:24:23 -0600 | [diff] [blame] | 1000 | pfn = ref_pfn; |
Thomas Gleixner | f61c5ba | 2018-09-17 16:29:14 +0200 | [diff] [blame] | 1001 | for (i = 0; i < PTRS_PER_PTE; i++, pfn += pfninc, lpaddr += lpinc) |
| 1002 | split_set_pte(cpa, pbase + i, pfn, ref_prot, lpaddr, lpinc); |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 1003 | |
Sai Praneeth | 2c66e24d | 2015-10-16 16:20:27 -0700 | [diff] [blame] | 1004 | if (virt_addr_valid(address)) { |
| 1005 | unsigned long pfn = PFN_DOWN(__pa(address)); |
| 1006 | |
| 1007 | if (pfn_range_is_mapped(pfn, pfn + 1)) |
| 1008 | split_page_count(level); |
| 1009 | } |
Yinghai Lu | f361a45 | 2008-07-10 20:38:26 -0700 | [diff] [blame] | 1010 | |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 1011 | /* |
Ingo Molnar | 07a66d7 | 2009-02-20 08:04:13 +0100 | [diff] [blame] | 1012 | * Install the new, split up pagetable. |
Huang, Ying | 4c881ca | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 1013 | * |
Ingo Molnar | 07a66d7 | 2009-02-20 08:04:13 +0100 | [diff] [blame] | 1014 | * We use the standard kernel pagetable protections for the new |
| 1015 | * pagetable protections, the actual ptes set above control the |
| 1016 | * primary protection behavior: |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 1017 | */ |
Ingo Molnar | 07a66d7 | 2009-02-20 08:04:13 +0100 | [diff] [blame] | 1018 | __set_pmd_pte(kpte, address, mk_pte(base, __pgprot(_KERNPG_TABLE))); |
Ingo Molnar | 211b3d0 | 2009-03-10 22:31:03 +0100 | [diff] [blame] | 1019 | |
| 1020 | /* |
Peter Zijlstra | c0a759a | 2018-09-19 10:50:18 +0200 | [diff] [blame] | 1021 | * Do a global flush tlb after splitting the large page |
| 1022 | * and before we do the actual change page attribute in the PTE. |
Ingo Molnar | 211b3d0 | 2009-03-10 22:31:03 +0100 | [diff] [blame] | 1023 | * |
Peter Zijlstra | c0a759a | 2018-09-19 10:50:18 +0200 | [diff] [blame] | 1024 | * Without this, we violate the TLB application note, that says: |
| 1025 | * "The TLBs may contain both ordinary and large-page |
| 1026 | * translations for a 4-KByte range of linear addresses. This |
| 1027 | * may occur if software modifies the paging structures so that |
| 1028 | * the page size used for the address range changes. If the two |
| 1029 | * translations differ with respect to page frame or attributes |
| 1030 | * (e.g., permissions), processor behavior is undefined and may |
| 1031 | * be implementation-specific." |
| 1032 | * |
| 1033 | * We do this global tlb flush inside the cpa_lock, so that we |
| 1034 | * don't allow any other cpu, with stale tlb entries change the |
| 1035 | * page attribute in parallel, that also falls into the |
| 1036 | * just split large page entry. |
Ingo Molnar | 211b3d0 | 2009-03-10 22:31:03 +0100 | [diff] [blame] | 1037 | */ |
Peter Zijlstra | c0a759a | 2018-09-19 10:50:18 +0200 | [diff] [blame] | 1038 | flush_tlb_all(); |
Andrea Arcangeli | a79e53d | 2011-02-16 15:45:22 -0800 | [diff] [blame] | 1039 | spin_unlock(&pgd_lock); |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 1040 | |
Ingo Molnar | bb5c2db | 2008-01-30 13:33:56 +0100 | [diff] [blame] | 1041 | return 0; |
| 1042 | } |
| 1043 | |
Borislav Petkov | 82f0712 | 2013-10-31 17:25:07 +0100 | [diff] [blame] | 1044 | static int split_large_page(struct cpa_data *cpa, pte_t *kpte, |
| 1045 | unsigned long address) |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 1046 | { |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 1047 | struct page *base; |
| 1048 | |
Christian Borntraeger | 288cf3c | 2016-03-15 14:57:33 -0700 | [diff] [blame] | 1049 | if (!debug_pagealloc_enabled()) |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 1050 | spin_unlock(&cpa_lock); |
Levin, Alexander (Sasha Levin) | 75f296d | 2017-11-15 17:35:54 -0800 | [diff] [blame] | 1051 | base = alloc_pages(GFP_KERNEL, 0); |
Christian Borntraeger | 288cf3c | 2016-03-15 14:57:33 -0700 | [diff] [blame] | 1052 | if (!debug_pagealloc_enabled()) |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 1053 | spin_lock(&cpa_lock); |
| 1054 | if (!base) |
| 1055 | return -ENOMEM; |
| 1056 | |
Borislav Petkov | 82f0712 | 2013-10-31 17:25:07 +0100 | [diff] [blame] | 1057 | if (__split_large_page(cpa, kpte, address, base)) |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 1058 | __free_page(base); |
| 1059 | |
| 1060 | return 0; |
| 1061 | } |
| 1062 | |
Borislav Petkov | 52a628f | 2013-10-31 17:25:06 +0100 | [diff] [blame] | 1063 | static bool try_to_free_pte_page(pte_t *pte) |
| 1064 | { |
| 1065 | int i; |
| 1066 | |
| 1067 | for (i = 0; i < PTRS_PER_PTE; i++) |
| 1068 | if (!pte_none(pte[i])) |
| 1069 | return false; |
| 1070 | |
| 1071 | free_page((unsigned long)pte); |
| 1072 | return true; |
| 1073 | } |
| 1074 | |
| 1075 | static bool try_to_free_pmd_page(pmd_t *pmd) |
| 1076 | { |
| 1077 | int i; |
| 1078 | |
| 1079 | for (i = 0; i < PTRS_PER_PMD; i++) |
| 1080 | if (!pmd_none(pmd[i])) |
| 1081 | return false; |
| 1082 | |
| 1083 | free_page((unsigned long)pmd); |
| 1084 | return true; |
| 1085 | } |
| 1086 | |
| 1087 | static bool unmap_pte_range(pmd_t *pmd, unsigned long start, unsigned long end) |
| 1088 | { |
| 1089 | pte_t *pte = pte_offset_kernel(pmd, start); |
| 1090 | |
| 1091 | while (start < end) { |
| 1092 | set_pte(pte, __pte(0)); |
| 1093 | |
| 1094 | start += PAGE_SIZE; |
| 1095 | pte++; |
| 1096 | } |
| 1097 | |
| 1098 | if (try_to_free_pte_page((pte_t *)pmd_page_vaddr(*pmd))) { |
| 1099 | pmd_clear(pmd); |
| 1100 | return true; |
| 1101 | } |
| 1102 | return false; |
| 1103 | } |
| 1104 | |
| 1105 | static void __unmap_pmd_range(pud_t *pud, pmd_t *pmd, |
| 1106 | unsigned long start, unsigned long end) |
| 1107 | { |
| 1108 | if (unmap_pte_range(pmd, start, end)) |
| 1109 | if (try_to_free_pmd_page((pmd_t *)pud_page_vaddr(*pud))) |
| 1110 | pud_clear(pud); |
| 1111 | } |
| 1112 | |
| 1113 | static void unmap_pmd_range(pud_t *pud, unsigned long start, unsigned long end) |
| 1114 | { |
| 1115 | pmd_t *pmd = pmd_offset(pud, start); |
| 1116 | |
| 1117 | /* |
| 1118 | * Not on a 2MB page boundary? |
| 1119 | */ |
| 1120 | if (start & (PMD_SIZE - 1)) { |
| 1121 | unsigned long next_page = (start + PMD_SIZE) & PMD_MASK; |
| 1122 | unsigned long pre_end = min_t(unsigned long, end, next_page); |
| 1123 | |
| 1124 | __unmap_pmd_range(pud, pmd, start, pre_end); |
| 1125 | |
| 1126 | start = pre_end; |
| 1127 | pmd++; |
| 1128 | } |
| 1129 | |
| 1130 | /* |
| 1131 | * Try to unmap in 2M chunks. |
| 1132 | */ |
| 1133 | while (end - start >= PMD_SIZE) { |
| 1134 | if (pmd_large(*pmd)) |
| 1135 | pmd_clear(pmd); |
| 1136 | else |
| 1137 | __unmap_pmd_range(pud, pmd, start, start + PMD_SIZE); |
| 1138 | |
| 1139 | start += PMD_SIZE; |
| 1140 | pmd++; |
| 1141 | } |
| 1142 | |
| 1143 | /* |
| 1144 | * 4K leftovers? |
| 1145 | */ |
| 1146 | if (start < end) |
| 1147 | return __unmap_pmd_range(pud, pmd, start, end); |
| 1148 | |
| 1149 | /* |
| 1150 | * Try again to free the PMD page if haven't succeeded above. |
| 1151 | */ |
| 1152 | if (!pud_none(*pud)) |
| 1153 | if (try_to_free_pmd_page((pmd_t *)pud_page_vaddr(*pud))) |
| 1154 | pud_clear(pud); |
| 1155 | } |
Borislav Petkov | 0bb8aee | 2013-10-31 17:25:05 +0100 | [diff] [blame] | 1156 | |
Kirill A. Shutemov | 4547833 | 2017-03-17 21:55:12 +0300 | [diff] [blame] | 1157 | static void unmap_pud_range(p4d_t *p4d, unsigned long start, unsigned long end) |
Borislav Petkov | 0bb8aee | 2013-10-31 17:25:05 +0100 | [diff] [blame] | 1158 | { |
Kirill A. Shutemov | 4547833 | 2017-03-17 21:55:12 +0300 | [diff] [blame] | 1159 | pud_t *pud = pud_offset(p4d, start); |
Borislav Petkov | 0bb8aee | 2013-10-31 17:25:05 +0100 | [diff] [blame] | 1160 | |
| 1161 | /* |
| 1162 | * Not on a GB page boundary? |
| 1163 | */ |
| 1164 | if (start & (PUD_SIZE - 1)) { |
| 1165 | unsigned long next_page = (start + PUD_SIZE) & PUD_MASK; |
| 1166 | unsigned long pre_end = min_t(unsigned long, end, next_page); |
| 1167 | |
| 1168 | unmap_pmd_range(pud, start, pre_end); |
| 1169 | |
| 1170 | start = pre_end; |
| 1171 | pud++; |
| 1172 | } |
| 1173 | |
| 1174 | /* |
| 1175 | * Try to unmap in 1G chunks? |
| 1176 | */ |
| 1177 | while (end - start >= PUD_SIZE) { |
| 1178 | |
| 1179 | if (pud_large(*pud)) |
| 1180 | pud_clear(pud); |
| 1181 | else |
| 1182 | unmap_pmd_range(pud, start, start + PUD_SIZE); |
| 1183 | |
| 1184 | start += PUD_SIZE; |
| 1185 | pud++; |
| 1186 | } |
| 1187 | |
| 1188 | /* |
| 1189 | * 2M leftovers? |
| 1190 | */ |
| 1191 | if (start < end) |
| 1192 | unmap_pmd_range(pud, start, end); |
| 1193 | |
| 1194 | /* |
| 1195 | * No need to try to free the PUD page because we'll free it in |
| 1196 | * populate_pgd's error path |
| 1197 | */ |
| 1198 | } |
| 1199 | |
Borislav Petkov | f900a4b | 2013-10-31 17:25:03 +0100 | [diff] [blame] | 1200 | static int alloc_pte_page(pmd_t *pmd) |
| 1201 | { |
Levin, Alexander (Sasha Levin) | 75f296d | 2017-11-15 17:35:54 -0800 | [diff] [blame] | 1202 | pte_t *pte = (pte_t *)get_zeroed_page(GFP_KERNEL); |
Borislav Petkov | f900a4b | 2013-10-31 17:25:03 +0100 | [diff] [blame] | 1203 | if (!pte) |
| 1204 | return -1; |
| 1205 | |
| 1206 | set_pmd(pmd, __pmd(__pa(pte) | _KERNPG_TABLE)); |
| 1207 | return 0; |
| 1208 | } |
| 1209 | |
Borislav Petkov | 4b23538 | 2013-10-31 17:25:02 +0100 | [diff] [blame] | 1210 | static int alloc_pmd_page(pud_t *pud) |
| 1211 | { |
Levin, Alexander (Sasha Levin) | 75f296d | 2017-11-15 17:35:54 -0800 | [diff] [blame] | 1212 | pmd_t *pmd = (pmd_t *)get_zeroed_page(GFP_KERNEL); |
Borislav Petkov | 4b23538 | 2013-10-31 17:25:02 +0100 | [diff] [blame] | 1213 | if (!pmd) |
| 1214 | return -1; |
| 1215 | |
| 1216 | set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE)); |
| 1217 | return 0; |
| 1218 | } |
| 1219 | |
Borislav Petkov | c6b6f36 | 2013-10-31 17:25:04 +0100 | [diff] [blame] | 1220 | static void populate_pte(struct cpa_data *cpa, |
| 1221 | unsigned long start, unsigned long end, |
| 1222 | unsigned num_pages, pmd_t *pmd, pgprot_t pgprot) |
| 1223 | { |
| 1224 | pte_t *pte; |
| 1225 | |
| 1226 | pte = pte_offset_kernel(pmd, start); |
| 1227 | |
Dave Hansen | d1440b2 | 2018-04-06 13:55:02 -0700 | [diff] [blame] | 1228 | pgprot = pgprot_clear_protnone_bits(pgprot); |
Sai Praneeth | 397630150 | 2016-02-17 12:35:56 +0000 | [diff] [blame] | 1229 | |
Borislav Petkov | c6b6f36 | 2013-10-31 17:25:04 +0100 | [diff] [blame] | 1230 | while (num_pages-- && start < end) { |
Matt Fleming | edc3b91 | 2015-11-27 21:09:31 +0000 | [diff] [blame] | 1231 | set_pte(pte, pfn_pte(cpa->pfn, pgprot)); |
Borislav Petkov | c6b6f36 | 2013-10-31 17:25:04 +0100 | [diff] [blame] | 1232 | |
| 1233 | start += PAGE_SIZE; |
Matt Fleming | edc3b91 | 2015-11-27 21:09:31 +0000 | [diff] [blame] | 1234 | cpa->pfn++; |
Borislav Petkov | c6b6f36 | 2013-10-31 17:25:04 +0100 | [diff] [blame] | 1235 | pte++; |
| 1236 | } |
| 1237 | } |
Borislav Petkov | f900a4b | 2013-10-31 17:25:03 +0100 | [diff] [blame] | 1238 | |
Matt Fleming | e535ec0 | 2016-09-20 14:26:21 +0100 | [diff] [blame] | 1239 | static long populate_pmd(struct cpa_data *cpa, |
| 1240 | unsigned long start, unsigned long end, |
| 1241 | unsigned num_pages, pud_t *pud, pgprot_t pgprot) |
Borislav Petkov | f900a4b | 2013-10-31 17:25:03 +0100 | [diff] [blame] | 1242 | { |
Matt Fleming | e535ec0 | 2016-09-20 14:26:21 +0100 | [diff] [blame] | 1243 | long cur_pages = 0; |
Borislav Petkov | f900a4b | 2013-10-31 17:25:03 +0100 | [diff] [blame] | 1244 | pmd_t *pmd; |
Juergen Gross | f5b2831 | 2014-11-03 14:02:02 +0100 | [diff] [blame] | 1245 | pgprot_t pmd_pgprot; |
Borislav Petkov | f900a4b | 2013-10-31 17:25:03 +0100 | [diff] [blame] | 1246 | |
| 1247 | /* |
| 1248 | * Not on a 2M boundary? |
| 1249 | */ |
| 1250 | if (start & (PMD_SIZE - 1)) { |
| 1251 | unsigned long pre_end = start + (num_pages << PAGE_SHIFT); |
| 1252 | unsigned long next_page = (start + PMD_SIZE) & PMD_MASK; |
| 1253 | |
| 1254 | pre_end = min_t(unsigned long, pre_end, next_page); |
| 1255 | cur_pages = (pre_end - start) >> PAGE_SHIFT; |
| 1256 | cur_pages = min_t(unsigned int, num_pages, cur_pages); |
| 1257 | |
| 1258 | /* |
| 1259 | * Need a PTE page? |
| 1260 | */ |
| 1261 | pmd = pmd_offset(pud, start); |
| 1262 | if (pmd_none(*pmd)) |
| 1263 | if (alloc_pte_page(pmd)) |
| 1264 | return -1; |
| 1265 | |
| 1266 | populate_pte(cpa, start, pre_end, cur_pages, pmd, pgprot); |
| 1267 | |
| 1268 | start = pre_end; |
| 1269 | } |
| 1270 | |
| 1271 | /* |
| 1272 | * We mapped them all? |
| 1273 | */ |
| 1274 | if (num_pages == cur_pages) |
| 1275 | return cur_pages; |
| 1276 | |
Juergen Gross | f5b2831 | 2014-11-03 14:02:02 +0100 | [diff] [blame] | 1277 | pmd_pgprot = pgprot_4k_2_large(pgprot); |
| 1278 | |
Borislav Petkov | f900a4b | 2013-10-31 17:25:03 +0100 | [diff] [blame] | 1279 | while (end - start >= PMD_SIZE) { |
| 1280 | |
| 1281 | /* |
| 1282 | * We cannot use a 1G page so allocate a PMD page if needed. |
| 1283 | */ |
| 1284 | if (pud_none(*pud)) |
| 1285 | if (alloc_pmd_page(pud)) |
| 1286 | return -1; |
| 1287 | |
| 1288 | pmd = pmd_offset(pud, start); |
| 1289 | |
Andi Kleen | 958f79b | 2018-08-07 15:09:39 -0700 | [diff] [blame] | 1290 | set_pmd(pmd, pmd_mkhuge(pfn_pmd(cpa->pfn, |
| 1291 | canon_pgprot(pmd_pgprot)))); |
Borislav Petkov | f900a4b | 2013-10-31 17:25:03 +0100 | [diff] [blame] | 1292 | |
| 1293 | start += PMD_SIZE; |
Matt Fleming | edc3b91 | 2015-11-27 21:09:31 +0000 | [diff] [blame] | 1294 | cpa->pfn += PMD_SIZE >> PAGE_SHIFT; |
Borislav Petkov | f900a4b | 2013-10-31 17:25:03 +0100 | [diff] [blame] | 1295 | cur_pages += PMD_SIZE >> PAGE_SHIFT; |
| 1296 | } |
| 1297 | |
| 1298 | /* |
| 1299 | * Map trailing 4K pages. |
| 1300 | */ |
| 1301 | if (start < end) { |
| 1302 | pmd = pmd_offset(pud, start); |
| 1303 | if (pmd_none(*pmd)) |
| 1304 | if (alloc_pte_page(pmd)) |
| 1305 | return -1; |
| 1306 | |
| 1307 | populate_pte(cpa, start, end, num_pages - cur_pages, |
| 1308 | pmd, pgprot); |
| 1309 | } |
| 1310 | return num_pages; |
| 1311 | } |
Borislav Petkov | 4b23538 | 2013-10-31 17:25:02 +0100 | [diff] [blame] | 1312 | |
Kirill A. Shutemov | 4547833 | 2017-03-17 21:55:12 +0300 | [diff] [blame] | 1313 | static int populate_pud(struct cpa_data *cpa, unsigned long start, p4d_t *p4d, |
| 1314 | pgprot_t pgprot) |
Borislav Petkov | 4b23538 | 2013-10-31 17:25:02 +0100 | [diff] [blame] | 1315 | { |
| 1316 | pud_t *pud; |
| 1317 | unsigned long end; |
Matt Fleming | e535ec0 | 2016-09-20 14:26:21 +0100 | [diff] [blame] | 1318 | long cur_pages = 0; |
Juergen Gross | f5b2831 | 2014-11-03 14:02:02 +0100 | [diff] [blame] | 1319 | pgprot_t pud_pgprot; |
Borislav Petkov | 4b23538 | 2013-10-31 17:25:02 +0100 | [diff] [blame] | 1320 | |
| 1321 | end = start + (cpa->numpages << PAGE_SHIFT); |
| 1322 | |
| 1323 | /* |
| 1324 | * Not on a Gb page boundary? => map everything up to it with |
| 1325 | * smaller pages. |
| 1326 | */ |
| 1327 | if (start & (PUD_SIZE - 1)) { |
| 1328 | unsigned long pre_end; |
| 1329 | unsigned long next_page = (start + PUD_SIZE) & PUD_MASK; |
| 1330 | |
| 1331 | pre_end = min_t(unsigned long, end, next_page); |
| 1332 | cur_pages = (pre_end - start) >> PAGE_SHIFT; |
| 1333 | cur_pages = min_t(int, (int)cpa->numpages, cur_pages); |
| 1334 | |
Kirill A. Shutemov | 4547833 | 2017-03-17 21:55:12 +0300 | [diff] [blame] | 1335 | pud = pud_offset(p4d, start); |
Borislav Petkov | 4b23538 | 2013-10-31 17:25:02 +0100 | [diff] [blame] | 1336 | |
| 1337 | /* |
| 1338 | * Need a PMD page? |
| 1339 | */ |
| 1340 | if (pud_none(*pud)) |
| 1341 | if (alloc_pmd_page(pud)) |
| 1342 | return -1; |
| 1343 | |
| 1344 | cur_pages = populate_pmd(cpa, start, pre_end, cur_pages, |
| 1345 | pud, pgprot); |
| 1346 | if (cur_pages < 0) |
| 1347 | return cur_pages; |
| 1348 | |
| 1349 | start = pre_end; |
| 1350 | } |
| 1351 | |
| 1352 | /* We mapped them all? */ |
| 1353 | if (cpa->numpages == cur_pages) |
| 1354 | return cur_pages; |
| 1355 | |
Kirill A. Shutemov | 4547833 | 2017-03-17 21:55:12 +0300 | [diff] [blame] | 1356 | pud = pud_offset(p4d, start); |
Juergen Gross | f5b2831 | 2014-11-03 14:02:02 +0100 | [diff] [blame] | 1357 | pud_pgprot = pgprot_4k_2_large(pgprot); |
Borislav Petkov | 4b23538 | 2013-10-31 17:25:02 +0100 | [diff] [blame] | 1358 | |
| 1359 | /* |
| 1360 | * Map everything starting from the Gb boundary, possibly with 1G pages |
| 1361 | */ |
Borislav Petkov | b8291adc | 2016-03-29 17:41:58 +0200 | [diff] [blame] | 1362 | while (boot_cpu_has(X86_FEATURE_GBPAGES) && end - start >= PUD_SIZE) { |
Andi Kleen | 958f79b | 2018-08-07 15:09:39 -0700 | [diff] [blame] | 1363 | set_pud(pud, pud_mkhuge(pfn_pud(cpa->pfn, |
| 1364 | canon_pgprot(pud_pgprot)))); |
Borislav Petkov | 4b23538 | 2013-10-31 17:25:02 +0100 | [diff] [blame] | 1365 | |
| 1366 | start += PUD_SIZE; |
Matt Fleming | edc3b91 | 2015-11-27 21:09:31 +0000 | [diff] [blame] | 1367 | cpa->pfn += PUD_SIZE >> PAGE_SHIFT; |
Borislav Petkov | 4b23538 | 2013-10-31 17:25:02 +0100 | [diff] [blame] | 1368 | cur_pages += PUD_SIZE >> PAGE_SHIFT; |
| 1369 | pud++; |
| 1370 | } |
| 1371 | |
| 1372 | /* Map trailing leftover */ |
| 1373 | if (start < end) { |
Matt Fleming | e535ec0 | 2016-09-20 14:26:21 +0100 | [diff] [blame] | 1374 | long tmp; |
Borislav Petkov | 4b23538 | 2013-10-31 17:25:02 +0100 | [diff] [blame] | 1375 | |
Kirill A. Shutemov | 4547833 | 2017-03-17 21:55:12 +0300 | [diff] [blame] | 1376 | pud = pud_offset(p4d, start); |
Borislav Petkov | 4b23538 | 2013-10-31 17:25:02 +0100 | [diff] [blame] | 1377 | if (pud_none(*pud)) |
| 1378 | if (alloc_pmd_page(pud)) |
| 1379 | return -1; |
| 1380 | |
| 1381 | tmp = populate_pmd(cpa, start, end, cpa->numpages - cur_pages, |
| 1382 | pud, pgprot); |
| 1383 | if (tmp < 0) |
| 1384 | return cur_pages; |
| 1385 | |
| 1386 | cur_pages += tmp; |
| 1387 | } |
| 1388 | return cur_pages; |
| 1389 | } |
Borislav Petkov | f3f7296 | 2013-10-31 17:25:01 +0100 | [diff] [blame] | 1390 | |
| 1391 | /* |
| 1392 | * Restrictions for kernel page table do not necessarily apply when mapping in |
| 1393 | * an alternate PGD. |
| 1394 | */ |
| 1395 | static int populate_pgd(struct cpa_data *cpa, unsigned long addr) |
| 1396 | { |
| 1397 | pgprot_t pgprot = __pgprot(_KERNPG_TABLE); |
Borislav Petkov | f3f7296 | 2013-10-31 17:25:01 +0100 | [diff] [blame] | 1398 | pud_t *pud = NULL; /* shut up gcc */ |
Kirill A. Shutemov | 4547833 | 2017-03-17 21:55:12 +0300 | [diff] [blame] | 1399 | p4d_t *p4d; |
Borislav Petkov | 42a5477 | 2014-01-18 12:48:16 +0100 | [diff] [blame] | 1400 | pgd_t *pgd_entry; |
Matt Fleming | e535ec0 | 2016-09-20 14:26:21 +0100 | [diff] [blame] | 1401 | long ret; |
Borislav Petkov | f3f7296 | 2013-10-31 17:25:01 +0100 | [diff] [blame] | 1402 | |
| 1403 | pgd_entry = cpa->pgd + pgd_index(addr); |
| 1404 | |
Kirill A. Shutemov | 4547833 | 2017-03-17 21:55:12 +0300 | [diff] [blame] | 1405 | if (pgd_none(*pgd_entry)) { |
Levin, Alexander (Sasha Levin) | 75f296d | 2017-11-15 17:35:54 -0800 | [diff] [blame] | 1406 | p4d = (p4d_t *)get_zeroed_page(GFP_KERNEL); |
Kirill A. Shutemov | 4547833 | 2017-03-17 21:55:12 +0300 | [diff] [blame] | 1407 | if (!p4d) |
| 1408 | return -1; |
| 1409 | |
| 1410 | set_pgd(pgd_entry, __pgd(__pa(p4d) | _KERNPG_TABLE)); |
| 1411 | } |
| 1412 | |
Borislav Petkov | f3f7296 | 2013-10-31 17:25:01 +0100 | [diff] [blame] | 1413 | /* |
| 1414 | * Allocate a PUD page and hand it down for mapping. |
| 1415 | */ |
Kirill A. Shutemov | 4547833 | 2017-03-17 21:55:12 +0300 | [diff] [blame] | 1416 | p4d = p4d_offset(pgd_entry, addr); |
| 1417 | if (p4d_none(*p4d)) { |
Levin, Alexander (Sasha Levin) | 75f296d | 2017-11-15 17:35:54 -0800 | [diff] [blame] | 1418 | pud = (pud_t *)get_zeroed_page(GFP_KERNEL); |
Borislav Petkov | f3f7296 | 2013-10-31 17:25:01 +0100 | [diff] [blame] | 1419 | if (!pud) |
| 1420 | return -1; |
Andy Lutomirski | 530dd8d | 2016-07-22 21:58:08 -0700 | [diff] [blame] | 1421 | |
Kirill A. Shutemov | 4547833 | 2017-03-17 21:55:12 +0300 | [diff] [blame] | 1422 | set_p4d(p4d, __p4d(__pa(pud) | _KERNPG_TABLE)); |
Borislav Petkov | f3f7296 | 2013-10-31 17:25:01 +0100 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | pgprot_val(pgprot) &= ~pgprot_val(cpa->mask_clr); |
| 1426 | pgprot_val(pgprot) |= pgprot_val(cpa->mask_set); |
| 1427 | |
Kirill A. Shutemov | 4547833 | 2017-03-17 21:55:12 +0300 | [diff] [blame] | 1428 | ret = populate_pud(cpa, addr, p4d, pgprot); |
Borislav Petkov | 0bb8aee | 2013-10-31 17:25:05 +0100 | [diff] [blame] | 1429 | if (ret < 0) { |
Andy Lutomirski | 55920d3 | 2016-07-23 09:59:28 -0700 | [diff] [blame] | 1430 | /* |
| 1431 | * Leave the PUD page in place in case some other CPU or thread |
| 1432 | * already found it, but remove any useless entries we just |
| 1433 | * added to it. |
| 1434 | */ |
Kirill A. Shutemov | 4547833 | 2017-03-17 21:55:12 +0300 | [diff] [blame] | 1435 | unmap_pud_range(p4d, addr, |
Borislav Petkov | 0bb8aee | 2013-10-31 17:25:05 +0100 | [diff] [blame] | 1436 | addr + (cpa->numpages << PAGE_SHIFT)); |
Borislav Petkov | 0bb8aee | 2013-10-31 17:25:05 +0100 | [diff] [blame] | 1437 | return ret; |
| 1438 | } |
Borislav Petkov | 42a5477 | 2014-01-18 12:48:16 +0100 | [diff] [blame] | 1439 | |
Borislav Petkov | f3f7296 | 2013-10-31 17:25:01 +0100 | [diff] [blame] | 1440 | cpa->numpages = ret; |
| 1441 | return 0; |
| 1442 | } |
| 1443 | |
Suresh Siddha | a1e4621 | 2009-01-20 14:20:21 -0800 | [diff] [blame] | 1444 | static int __cpa_process_fault(struct cpa_data *cpa, unsigned long vaddr, |
| 1445 | int primary) |
| 1446 | { |
Matt Fleming | 7fc8442 | 2016-04-25 21:06:35 +0100 | [diff] [blame] | 1447 | if (cpa->pgd) { |
| 1448 | /* |
| 1449 | * Right now, we only execute this code path when mapping |
| 1450 | * the EFI virtual memory map regions, no other users |
| 1451 | * provide a ->pgd value. This may change in the future. |
| 1452 | */ |
Borislav Petkov | 82f0712 | 2013-10-31 17:25:07 +0100 | [diff] [blame] | 1453 | return populate_pgd(cpa, vaddr); |
Matt Fleming | 7fc8442 | 2016-04-25 21:06:35 +0100 | [diff] [blame] | 1454 | } |
Borislav Petkov | 82f0712 | 2013-10-31 17:25:07 +0100 | [diff] [blame] | 1455 | |
Suresh Siddha | a1e4621 | 2009-01-20 14:20:21 -0800 | [diff] [blame] | 1456 | /* |
| 1457 | * Ignore all non primary paths. |
| 1458 | */ |
Jan Beulich | 405e1133 | 2016-02-10 02:03:00 -0700 | [diff] [blame] | 1459 | if (!primary) { |
| 1460 | cpa->numpages = 1; |
Suresh Siddha | a1e4621 | 2009-01-20 14:20:21 -0800 | [diff] [blame] | 1461 | return 0; |
Jan Beulich | 405e1133 | 2016-02-10 02:03:00 -0700 | [diff] [blame] | 1462 | } |
Suresh Siddha | a1e4621 | 2009-01-20 14:20:21 -0800 | [diff] [blame] | 1463 | |
| 1464 | /* |
| 1465 | * Ignore the NULL PTE for kernel identity mapping, as it is expected |
| 1466 | * to have holes. |
| 1467 | * Also set numpages to '1' indicating that we processed cpa req for |
| 1468 | * one virtual address page and its pfn. TBD: numpages can be set based |
| 1469 | * on the initial value and the level returned by lookup_address(). |
| 1470 | */ |
| 1471 | if (within(vaddr, PAGE_OFFSET, |
| 1472 | PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT))) { |
| 1473 | cpa->numpages = 1; |
| 1474 | cpa->pfn = __pa(vaddr) >> PAGE_SHIFT; |
| 1475 | return 0; |
Dave Hansen | 58e65b5 | 2018-04-20 15:20:21 -0700 | [diff] [blame] | 1476 | |
| 1477 | } else if (__cpa_pfn_in_highmap(cpa->pfn)) { |
| 1478 | /* Faults in the highmap are OK, so do not warn: */ |
| 1479 | return -EFAULT; |
Suresh Siddha | a1e4621 | 2009-01-20 14:20:21 -0800 | [diff] [blame] | 1480 | } else { |
| 1481 | WARN(1, KERN_WARNING "CPA: called for zero pte. " |
| 1482 | "vaddr = %lx cpa->vaddr = %lx\n", vaddr, |
| 1483 | *cpa->vaddr); |
| 1484 | |
| 1485 | return -EFAULT; |
| 1486 | } |
| 1487 | } |
| 1488 | |
Thomas Gleixner | c31c7d4 | 2008-02-18 20:54:14 +0100 | [diff] [blame] | 1489 | static int __change_page_attr(struct cpa_data *cpa, int primary) |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 1490 | { |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 1491 | unsigned long address; |
Harvey Harrison | da7bfc5 | 2008-02-09 23:24:08 +0100 | [diff] [blame] | 1492 | int do_split, err; |
| 1493 | unsigned int level; |
Thomas Gleixner | c31c7d4 | 2008-02-18 20:54:14 +0100 | [diff] [blame] | 1494 | pte_t *kpte, old_pte; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | |
Peter Zijlstra | 16ebf03 | 2018-12-03 18:03:46 +0100 | [diff] [blame] | 1496 | address = __cpa_addr(cpa, cpa->curpage); |
Ingo Molnar | 97f99fe | 2008-01-30 13:33:55 +0100 | [diff] [blame] | 1497 | repeat: |
Borislav Petkov | 82f0712 | 2013-10-31 17:25:07 +0100 | [diff] [blame] | 1498 | kpte = _lookup_address_cpa(cpa, address, &level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | if (!kpte) |
Suresh Siddha | a1e4621 | 2009-01-20 14:20:21 -0800 | [diff] [blame] | 1500 | return __cpa_process_fault(cpa, address, primary); |
Thomas Gleixner | c31c7d4 | 2008-02-18 20:54:14 +0100 | [diff] [blame] | 1501 | |
| 1502 | old_pte = *kpte; |
Dave Hansen | dcb32d9 | 2016-07-07 17:19:15 -0700 | [diff] [blame] | 1503 | if (pte_none(old_pte)) |
Suresh Siddha | a1e4621 | 2009-01-20 14:20:21 -0800 | [diff] [blame] | 1504 | return __cpa_process_fault(cpa, address, primary); |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 1505 | |
Thomas Gleixner | 30551bb | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 1506 | if (level == PG_LEVEL_4K) { |
Thomas Gleixner | c31c7d4 | 2008-02-18 20:54:14 +0100 | [diff] [blame] | 1507 | pte_t new_pte; |
Arjan van de Ven | 626c2c9 | 2008-02-04 16:48:05 +0100 | [diff] [blame] | 1508 | pgprot_t new_prot = pte_pgprot(old_pte); |
Thomas Gleixner | c31c7d4 | 2008-02-18 20:54:14 +0100 | [diff] [blame] | 1509 | unsigned long pfn = pte_pfn(old_pte); |
Thomas Gleixner | a72a08a | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 1510 | |
Thomas Gleixner | 72e458d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 1511 | pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr); |
| 1512 | pgprot_val(new_prot) |= pgprot_val(cpa->mask_set); |
Ingo Molnar | 86f0398 | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 1513 | |
Thomas Gleixner | 5c280cf | 2018-09-17 16:29:12 +0200 | [diff] [blame] | 1514 | cpa_inc_4k_install(); |
Thomas Gleixner | 7af0145 | 2019-08-29 00:31:34 +0200 | [diff] [blame] | 1515 | /* Hand in lpsize = 0 to enforce the protection mechanism */ |
| 1516 | new_prot = static_protections(new_prot, address, pfn, 1, 0, |
Thomas Gleixner | 4046460 | 2018-09-17 16:29:11 +0200 | [diff] [blame] | 1517 | CPA_PROTECT); |
Ingo Molnar | 86f0398 | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 1518 | |
Dave Hansen | d1440b2 | 2018-04-06 13:55:02 -0700 | [diff] [blame] | 1519 | new_prot = pgprot_clear_protnone_bits(new_prot); |
Andrea Arcangeli | a8aed3e | 2013-02-22 15:11:51 -0800 | [diff] [blame] | 1520 | |
| 1521 | /* |
Arjan van de Ven | 626c2c9 | 2008-02-04 16:48:05 +0100 | [diff] [blame] | 1522 | * We need to keep the pfn from the existing PTE, |
| 1523 | * after all we're only going to change it's attributes |
| 1524 | * not the memory it points to |
| 1525 | */ |
Dave Hansen | 1a54420 | 2018-04-06 13:55:11 -0700 | [diff] [blame] | 1526 | new_pte = pfn_pte(pfn, new_prot); |
Thomas Gleixner | c31c7d4 | 2008-02-18 20:54:14 +0100 | [diff] [blame] | 1527 | cpa->pfn = pfn; |
Thomas Gleixner | f4ae5da | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 1528 | /* |
| 1529 | * Do we really change anything ? |
| 1530 | */ |
| 1531 | if (pte_val(old_pte) != pte_val(new_pte)) { |
| 1532 | set_pte_atomic(kpte, new_pte); |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 1533 | cpa->flags |= CPA_FLUSHTLB; |
Thomas Gleixner | f4ae5da | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 1534 | } |
Rafael J. Wysocki | 9b5cf48 | 2008-03-03 01:17:37 +0100 | [diff] [blame] | 1535 | cpa->numpages = 1; |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 1536 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | } |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 1538 | |
| 1539 | /* |
| 1540 | * Check, whether we can keep the large page intact |
| 1541 | * and just change the pte: |
| 1542 | */ |
Thomas Gleixner | 8679de0 | 2018-09-17 16:29:08 +0200 | [diff] [blame] | 1543 | do_split = should_split_large_page(kpte, address, cpa); |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 1544 | /* |
| 1545 | * When the range fits into the existing large page, |
Rafael J. Wysocki | 9b5cf48 | 2008-03-03 01:17:37 +0100 | [diff] [blame] | 1546 | * return. cp->numpages and cpa->tlbflush have been updated in |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 1547 | * try_large_page: |
| 1548 | */ |
Ingo Molnar | 87f7f8f | 2008-02-04 16:48:10 +0100 | [diff] [blame] | 1549 | if (do_split <= 0) |
| 1550 | return do_split; |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 1551 | |
| 1552 | /* |
| 1553 | * We have to split the large page: |
| 1554 | */ |
Borislav Petkov | 82f0712 | 2013-10-31 17:25:07 +0100 | [diff] [blame] | 1555 | err = split_large_page(cpa, kpte, address); |
Peter Zijlstra | c0a759a | 2018-09-19 10:50:18 +0200 | [diff] [blame] | 1556 | if (!err) |
Ingo Molnar | 87f7f8f | 2008-02-04 16:48:10 +0100 | [diff] [blame] | 1557 | goto repeat; |
Ingo Molnar | beaff63 | 2008-02-04 16:48:09 +0100 | [diff] [blame] | 1558 | |
Ingo Molnar | 87f7f8f | 2008-02-04 16:48:10 +0100 | [diff] [blame] | 1559 | return err; |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 1560 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1561 | |
Thomas Gleixner | c31c7d4 | 2008-02-18 20:54:14 +0100 | [diff] [blame] | 1562 | static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias); |
| 1563 | |
| 1564 | static int cpa_process_alias(struct cpa_data *cpa) |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 1565 | { |
Thomas Gleixner | c31c7d4 | 2008-02-18 20:54:14 +0100 | [diff] [blame] | 1566 | struct cpa_data alias_cpa; |
Tejun Heo | 992f4c1 | 2009-06-22 11:56:24 +0900 | [diff] [blame] | 1567 | unsigned long laddr = (unsigned long)__va(cpa->pfn << PAGE_SHIFT); |
Tejun Heo | e933a73 | 2009-08-14 15:00:53 +0900 | [diff] [blame] | 1568 | unsigned long vaddr; |
Tejun Heo | 992f4c1 | 2009-06-22 11:56:24 +0900 | [diff] [blame] | 1569 | int ret; |
Thomas Gleixner | c31c7d4 | 2008-02-18 20:54:14 +0100 | [diff] [blame] | 1570 | |
Yinghai Lu | 8eb5779 | 2012-11-16 19:38:49 -0800 | [diff] [blame] | 1571 | if (!pfn_range_is_mapped(cpa->pfn, cpa->pfn + 1)) |
Thomas Gleixner | c31c7d4 | 2008-02-18 20:54:14 +0100 | [diff] [blame] | 1572 | return 0; |
| 1573 | |
Thomas Gleixner | f34b439 | 2008-02-15 22:17:57 +0100 | [diff] [blame] | 1574 | /* |
| 1575 | * No need to redo, when the primary call touched the direct |
| 1576 | * mapping already: |
| 1577 | */ |
Peter Zijlstra | 16ebf03 | 2018-12-03 18:03:46 +0100 | [diff] [blame] | 1578 | vaddr = __cpa_addr(cpa, cpa->curpage); |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 1579 | if (!(within(vaddr, PAGE_OFFSET, |
Suresh Siddha | a1e4621 | 2009-01-20 14:20:21 -0800 | [diff] [blame] | 1580 | PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT)))) { |
Thomas Gleixner | c31c7d4 | 2008-02-18 20:54:14 +0100 | [diff] [blame] | 1581 | |
Thomas Gleixner | f34b439 | 2008-02-15 22:17:57 +0100 | [diff] [blame] | 1582 | alias_cpa = *cpa; |
Tejun Heo | 992f4c1 | 2009-06-22 11:56:24 +0900 | [diff] [blame] | 1583 | alias_cpa.vaddr = &laddr; |
venkatesh.pallipadi@intel.com | 9ae2847 | 2009-03-19 14:51:14 -0700 | [diff] [blame] | 1584 | alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY); |
Peter Zijlstra | 98bfc9b | 2018-12-03 18:03:47 +0100 | [diff] [blame] | 1585 | alias_cpa.curpage = 0; |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 1586 | |
Thomas Gleixner | f34b439 | 2008-02-15 22:17:57 +0100 | [diff] [blame] | 1587 | ret = __change_page_attr_set_clr(&alias_cpa, 0); |
Tejun Heo | 992f4c1 | 2009-06-22 11:56:24 +0900 | [diff] [blame] | 1588 | if (ret) |
| 1589 | return ret; |
Thomas Gleixner | f34b439 | 2008-02-15 22:17:57 +0100 | [diff] [blame] | 1590 | } |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 1591 | |
Arjan van de Ven | 488fd99 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 1592 | #ifdef CONFIG_X86_64 |
Thomas Gleixner | 0879750 | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 1593 | /* |
Tejun Heo | 992f4c1 | 2009-06-22 11:56:24 +0900 | [diff] [blame] | 1594 | * If the primary call didn't touch the high mapping already |
| 1595 | * and the physical address is inside the kernel map, we need |
Thomas Gleixner | 0879750 | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 1596 | * to touch the high mapped kernel as well: |
| 1597 | */ |
Tejun Heo | 992f4c1 | 2009-06-22 11:56:24 +0900 | [diff] [blame] | 1598 | if (!within(vaddr, (unsigned long)_text, _brk_end) && |
Dave Hansen | 58e65b5 | 2018-04-20 15:20:21 -0700 | [diff] [blame] | 1599 | __cpa_pfn_in_highmap(cpa->pfn)) { |
Tejun Heo | 992f4c1 | 2009-06-22 11:56:24 +0900 | [diff] [blame] | 1600 | unsigned long temp_cpa_vaddr = (cpa->pfn << PAGE_SHIFT) + |
| 1601 | __START_KERNEL_map - phys_base; |
| 1602 | alias_cpa = *cpa; |
| 1603 | alias_cpa.vaddr = &temp_cpa_vaddr; |
| 1604 | alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY); |
Peter Zijlstra | 98bfc9b | 2018-12-03 18:03:47 +0100 | [diff] [blame] | 1605 | alias_cpa.curpage = 0; |
Thomas Gleixner | 0879750 | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 1606 | |
Tejun Heo | 992f4c1 | 2009-06-22 11:56:24 +0900 | [diff] [blame] | 1607 | /* |
| 1608 | * The high mapping range is imprecise, so ignore the |
| 1609 | * return value. |
| 1610 | */ |
| 1611 | __change_page_attr_set_clr(&alias_cpa, 0); |
| 1612 | } |
Thomas Gleixner | 0879750 | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 1613 | #endif |
Tejun Heo | 992f4c1 | 2009-06-22 11:56:24 +0900 | [diff] [blame] | 1614 | |
| 1615 | return 0; |
Ingo Molnar | 44af6c4 | 2008-01-30 13:34:03 +0100 | [diff] [blame] | 1616 | } |
| 1617 | |
Thomas Gleixner | c31c7d4 | 2008-02-18 20:54:14 +0100 | [diff] [blame] | 1618 | static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias) |
Thomas Gleixner | ff31452 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 1619 | { |
Matt Fleming | e535ec0 | 2016-09-20 14:26:21 +0100 | [diff] [blame] | 1620 | unsigned long numpages = cpa->numpages; |
Peter Zijlstra | 83b4e39 | 2018-12-03 18:03:50 +0100 | [diff] [blame] | 1621 | unsigned long rempages = numpages; |
| 1622 | int ret = 0; |
Thomas Gleixner | ff31452 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 1623 | |
Peter Zijlstra | 83b4e39 | 2018-12-03 18:03:50 +0100 | [diff] [blame] | 1624 | while (rempages) { |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 1625 | /* |
| 1626 | * Store the remaining nr of pages for the large page |
| 1627 | * preservation check. |
| 1628 | */ |
Peter Zijlstra | 83b4e39 | 2018-12-03 18:03:50 +0100 | [diff] [blame] | 1629 | cpa->numpages = rempages; |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 1630 | /* for array changes, we can't use large page */ |
venkatesh.pallipadi@intel.com | 9ae2847 | 2009-03-19 14:51:14 -0700 | [diff] [blame] | 1631 | if (cpa->flags & (CPA_ARRAY | CPA_PAGES_ARRAY)) |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 1632 | cpa->numpages = 1; |
Thomas Gleixner | c31c7d4 | 2008-02-18 20:54:14 +0100 | [diff] [blame] | 1633 | |
Christian Borntraeger | 288cf3c | 2016-03-15 14:57:33 -0700 | [diff] [blame] | 1634 | if (!debug_pagealloc_enabled()) |
Suresh Siddha | ad5ca55 | 2008-09-23 14:00:42 -0700 | [diff] [blame] | 1635 | spin_lock(&cpa_lock); |
Thomas Gleixner | c31c7d4 | 2008-02-18 20:54:14 +0100 | [diff] [blame] | 1636 | ret = __change_page_attr(cpa, checkalias); |
Christian Borntraeger | 288cf3c | 2016-03-15 14:57:33 -0700 | [diff] [blame] | 1637 | if (!debug_pagealloc_enabled()) |
Suresh Siddha | ad5ca55 | 2008-09-23 14:00:42 -0700 | [diff] [blame] | 1638 | spin_unlock(&cpa_lock); |
Thomas Gleixner | ff31452 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 1639 | if (ret) |
Peter Zijlstra | 83b4e39 | 2018-12-03 18:03:50 +0100 | [diff] [blame] | 1640 | goto out; |
Thomas Gleixner | ff31452 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 1641 | |
Thomas Gleixner | c31c7d4 | 2008-02-18 20:54:14 +0100 | [diff] [blame] | 1642 | if (checkalias) { |
| 1643 | ret = cpa_process_alias(cpa); |
| 1644 | if (ret) |
Peter Zijlstra | 83b4e39 | 2018-12-03 18:03:50 +0100 | [diff] [blame] | 1645 | goto out; |
Thomas Gleixner | c31c7d4 | 2008-02-18 20:54:14 +0100 | [diff] [blame] | 1646 | } |
| 1647 | |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 1648 | /* |
| 1649 | * Adjust the number of pages with the result of the |
| 1650 | * CPA operation. Either a large page has been |
| 1651 | * preserved or a single page update happened. |
| 1652 | */ |
Peter Zijlstra | 83b4e39 | 2018-12-03 18:03:50 +0100 | [diff] [blame] | 1653 | BUG_ON(cpa->numpages > rempages || !cpa->numpages); |
| 1654 | rempages -= cpa->numpages; |
Peter Zijlstra | 98bfc9b | 2018-12-03 18:03:47 +0100 | [diff] [blame] | 1655 | cpa->curpage += cpa->numpages; |
Thomas Gleixner | 65e074d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 1656 | } |
Peter Zijlstra | 83b4e39 | 2018-12-03 18:03:50 +0100 | [diff] [blame] | 1657 | |
| 1658 | out: |
| 1659 | /* Restore the original numpages */ |
| 1660 | cpa->numpages = numpages; |
| 1661 | return ret; |
Thomas Gleixner | ff31452 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 1662 | } |
| 1663 | |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 1664 | static int change_page_attr_set_clr(unsigned long *addr, int numpages, |
Andi Kleen | c9caa02 | 2008-03-12 03:53:29 +0100 | [diff] [blame] | 1665 | pgprot_t mask_set, pgprot_t mask_clr, |
venkatesh.pallipadi@intel.com | 9ae2847 | 2009-03-19 14:51:14 -0700 | [diff] [blame] | 1666 | int force_split, int in_flag, |
| 1667 | struct page **pages) |
Thomas Gleixner | ff31452 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 1668 | { |
Thomas Gleixner | 72e458d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 1669 | struct cpa_data cpa; |
Ingo Molnar | cacf890 | 2008-08-21 13:46:33 +0200 | [diff] [blame] | 1670 | int ret, cache, checkalias; |
Thomas Gleixner | 331e406 | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 1671 | |
Borislav Petkov | 82f0712 | 2013-10-31 17:25:07 +0100 | [diff] [blame] | 1672 | memset(&cpa, 0, sizeof(cpa)); |
| 1673 | |
Thomas Gleixner | 331e406 | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 1674 | /* |
Dave Hansen | 39114b7 | 2018-04-06 13:55:17 -0700 | [diff] [blame] | 1675 | * Check, if we are requested to set a not supported |
| 1676 | * feature. Clearing non-supported features is OK. |
Thomas Gleixner | 331e406 | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 1677 | */ |
| 1678 | mask_set = canon_pgprot(mask_set); |
Dave Hansen | 39114b7 | 2018-04-06 13:55:17 -0700 | [diff] [blame] | 1679 | |
Andi Kleen | c9caa02 | 2008-03-12 03:53:29 +0100 | [diff] [blame] | 1680 | if (!pgprot_val(mask_set) && !pgprot_val(mask_clr) && !force_split) |
Thomas Gleixner | 331e406 | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 1681 | return 0; |
| 1682 | |
Thomas Gleixner | 69b1415 | 2008-02-13 11:04:50 +0100 | [diff] [blame] | 1683 | /* Ensure we are PAGE_SIZE aligned */ |
venkatesh.pallipadi@intel.com | 9ae2847 | 2009-03-19 14:51:14 -0700 | [diff] [blame] | 1684 | if (in_flag & CPA_ARRAY) { |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 1685 | int i; |
| 1686 | for (i = 0; i < numpages; i++) { |
| 1687 | if (addr[i] & ~PAGE_MASK) { |
| 1688 | addr[i] &= PAGE_MASK; |
| 1689 | WARN_ON_ONCE(1); |
| 1690 | } |
| 1691 | } |
venkatesh.pallipadi@intel.com | 9ae2847 | 2009-03-19 14:51:14 -0700 | [diff] [blame] | 1692 | } else if (!(in_flag & CPA_PAGES_ARRAY)) { |
| 1693 | /* |
| 1694 | * in_flag of CPA_PAGES_ARRAY implies it is aligned. |
Ingo Molnar | a97673a | 2018-12-03 10:47:34 +0100 | [diff] [blame] | 1695 | * No need to check in that case |
venkatesh.pallipadi@intel.com | 9ae2847 | 2009-03-19 14:51:14 -0700 | [diff] [blame] | 1696 | */ |
| 1697 | if (*addr & ~PAGE_MASK) { |
| 1698 | *addr &= PAGE_MASK; |
| 1699 | /* |
| 1700 | * People should not be passing in unaligned addresses: |
| 1701 | */ |
| 1702 | WARN_ON_ONCE(1); |
| 1703 | } |
Thomas Gleixner | 69b1415 | 2008-02-13 11:04:50 +0100 | [diff] [blame] | 1704 | } |
| 1705 | |
Nick Piggin | 5843d9a | 2008-08-01 03:15:21 +0200 | [diff] [blame] | 1706 | /* Must avoid aliasing mappings in the highmem code */ |
| 1707 | kmap_flush_unused(); |
| 1708 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1709 | vm_unmap_aliases(); |
| 1710 | |
Thomas Gleixner | 72e458d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 1711 | cpa.vaddr = addr; |
venkatesh.pallipadi@intel.com | 9ae2847 | 2009-03-19 14:51:14 -0700 | [diff] [blame] | 1712 | cpa.pages = pages; |
Thomas Gleixner | 72e458d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 1713 | cpa.numpages = numpages; |
| 1714 | cpa.mask_set = mask_set; |
| 1715 | cpa.mask_clr = mask_clr; |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 1716 | cpa.flags = 0; |
| 1717 | cpa.curpage = 0; |
Andi Kleen | c9caa02 | 2008-03-12 03:53:29 +0100 | [diff] [blame] | 1718 | cpa.force_split = force_split; |
Thomas Gleixner | 72e458d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 1719 | |
venkatesh.pallipadi@intel.com | 9ae2847 | 2009-03-19 14:51:14 -0700 | [diff] [blame] | 1720 | if (in_flag & (CPA_ARRAY | CPA_PAGES_ARRAY)) |
| 1721 | cpa.flags |= in_flag; |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 1722 | |
Thomas Gleixner | af96e44 | 2008-02-15 21:49:46 +0100 | [diff] [blame] | 1723 | /* No alias checking for _NX bit modifications */ |
| 1724 | checkalias = (pgprot_val(mask_set) | pgprot_val(mask_clr)) != _PAGE_NX; |
Dave Hansen | c40a56a | 2018-08-02 15:58:31 -0700 | [diff] [blame] | 1725 | /* Has caller explicitly disabled alias checking? */ |
| 1726 | if (in_flag & CPA_NO_CHECK_ALIAS) |
| 1727 | checkalias = 0; |
Thomas Gleixner | af96e44 | 2008-02-15 21:49:46 +0100 | [diff] [blame] | 1728 | |
| 1729 | ret = __change_page_attr_set_clr(&cpa, checkalias); |
Thomas Gleixner | ff31452 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 1730 | |
Thomas Gleixner | 57a6a46 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 1731 | /* |
Thomas Gleixner | f4ae5da | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 1732 | * Check whether we really changed something: |
| 1733 | */ |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 1734 | if (!(cpa.flags & CPA_FLUSHTLB)) |
Shaohua Li | 1ac2f7d | 2008-08-04 14:51:24 +0800 | [diff] [blame] | 1735 | goto out; |
Ingo Molnar | cacf890 | 2008-08-21 13:46:33 +0200 | [diff] [blame] | 1736 | |
Thomas Gleixner | f4ae5da | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 1737 | /* |
Andi Kleen | 6bb8383 | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 1738 | * No need to flush, when we did not set any of the caching |
| 1739 | * attributes: |
| 1740 | */ |
Juergen Gross | c06814d | 2014-11-03 14:01:57 +0100 | [diff] [blame] | 1741 | cache = !!pgprot2cachemode(mask_set); |
Andi Kleen | 6bb8383 | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 1742 | |
| 1743 | /* |
Peter Zijlstra | fce2ce9 | 2018-09-19 10:50:22 +0200 | [diff] [blame] | 1744 | * On error; flush everything to be sure. |
Thomas Gleixner | 57a6a46 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 1745 | */ |
Peter Zijlstra | fce2ce9 | 2018-09-19 10:50:22 +0200 | [diff] [blame] | 1746 | if (ret) { |
Andi Kleen | 6bb8383 | 2008-02-04 16:48:06 +0100 | [diff] [blame] | 1747 | cpa_flush_all(cache); |
Peter Zijlstra | fce2ce9 | 2018-09-19 10:50:22 +0200 | [diff] [blame] | 1748 | goto out; |
| 1749 | } |
| 1750 | |
Peter Zijlstra | fe0937b | 2018-12-03 18:03:51 +0100 | [diff] [blame] | 1751 | cpa_flush(&cpa, cache); |
Thomas Gleixner | 76ebd05 | 2008-02-09 23:24:09 +0100 | [diff] [blame] | 1752 | out: |
Thomas Gleixner | ff31452 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 1753 | return ret; |
| 1754 | } |
| 1755 | |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 1756 | static inline int change_page_attr_set(unsigned long *addr, int numpages, |
| 1757 | pgprot_t mask, int array) |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 1758 | { |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 1759 | return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0), 0, |
venkatesh.pallipadi@intel.com | 9ae2847 | 2009-03-19 14:51:14 -0700 | [diff] [blame] | 1760 | (array ? CPA_ARRAY : 0), NULL); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 1761 | } |
| 1762 | |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 1763 | static inline int change_page_attr_clear(unsigned long *addr, int numpages, |
| 1764 | pgprot_t mask, int array) |
Thomas Gleixner | 72932c7 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 1765 | { |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 1766 | return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask, 0, |
venkatesh.pallipadi@intel.com | 9ae2847 | 2009-03-19 14:51:14 -0700 | [diff] [blame] | 1767 | (array ? CPA_ARRAY : 0), NULL); |
Thomas Gleixner | 72932c7 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 1768 | } |
| 1769 | |
venkatesh.pallipadi@intel.com | 0f35075 | 2009-03-19 14:51:15 -0700 | [diff] [blame] | 1770 | static inline int cpa_set_pages_array(struct page **pages, int numpages, |
| 1771 | pgprot_t mask) |
| 1772 | { |
| 1773 | return change_page_attr_set_clr(NULL, numpages, mask, __pgprot(0), 0, |
| 1774 | CPA_PAGES_ARRAY, pages); |
| 1775 | } |
| 1776 | |
| 1777 | static inline int cpa_clear_pages_array(struct page **pages, int numpages, |
| 1778 | pgprot_t mask) |
| 1779 | { |
| 1780 | return change_page_attr_set_clr(NULL, numpages, __pgprot(0), mask, 0, |
| 1781 | CPA_PAGES_ARRAY, pages); |
| 1782 | } |
| 1783 | |
venkatesh.pallipadi@intel.com | 1219333 | 2008-03-18 17:00:18 -0700 | [diff] [blame] | 1784 | int _set_memory_uc(unsigned long addr, int numpages) |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 1785 | { |
Suresh Siddha | de33c44 | 2008-04-25 17:07:22 -0700 | [diff] [blame] | 1786 | /* |
| 1787 | * for now UC MINUS. see comments in ioremap_nocache() |
Luis R. Rodriguez | e4b6be33 | 2015-05-11 10:15:53 +0200 | [diff] [blame] | 1788 | * If you really need strong UC use ioremap_uc(), but note |
| 1789 | * that you cannot override IO areas with set_memory_*() as |
| 1790 | * these helpers cannot work with IO memory. |
Suresh Siddha | de33c44 | 2008-04-25 17:07:22 -0700 | [diff] [blame] | 1791 | */ |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 1792 | return change_page_attr_set(&addr, numpages, |
Juergen Gross | c06814d | 2014-11-03 14:01:57 +0100 | [diff] [blame] | 1793 | cachemode2pgprot(_PAGE_CACHE_MODE_UC_MINUS), |
| 1794 | 0); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 1795 | } |
venkatesh.pallipadi@intel.com | 1219333 | 2008-03-18 17:00:18 -0700 | [diff] [blame] | 1796 | |
| 1797 | int set_memory_uc(unsigned long addr, int numpages) |
| 1798 | { |
venkatesh.pallipadi@intel.com | 9fa3ab3 | 2009-04-09 14:26:49 -0700 | [diff] [blame] | 1799 | int ret; |
| 1800 | |
Suresh Siddha | de33c44 | 2008-04-25 17:07:22 -0700 | [diff] [blame] | 1801 | /* |
| 1802 | * for now UC MINUS. see comments in ioremap_nocache() |
| 1803 | */ |
venkatesh.pallipadi@intel.com | 9fa3ab3 | 2009-04-09 14:26:49 -0700 | [diff] [blame] | 1804 | ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE, |
Juergen Gross | e00c8cc | 2014-11-03 14:01:59 +0100 | [diff] [blame] | 1805 | _PAGE_CACHE_MODE_UC_MINUS, NULL); |
venkatesh.pallipadi@intel.com | 9fa3ab3 | 2009-04-09 14:26:49 -0700 | [diff] [blame] | 1806 | if (ret) |
| 1807 | goto out_err; |
venkatesh.pallipadi@intel.com | 1219333 | 2008-03-18 17:00:18 -0700 | [diff] [blame] | 1808 | |
venkatesh.pallipadi@intel.com | 9fa3ab3 | 2009-04-09 14:26:49 -0700 | [diff] [blame] | 1809 | ret = _set_memory_uc(addr, numpages); |
| 1810 | if (ret) |
| 1811 | goto out_free; |
| 1812 | |
| 1813 | return 0; |
| 1814 | |
| 1815 | out_free: |
| 1816 | free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE); |
| 1817 | out_err: |
| 1818 | return ret; |
venkatesh.pallipadi@intel.com | 1219333 | 2008-03-18 17:00:18 -0700 | [diff] [blame] | 1819 | } |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 1820 | EXPORT_SYMBOL(set_memory_uc); |
| 1821 | |
Peter Zijlstra | 3c56735 | 2018-12-03 18:03:53 +0100 | [diff] [blame] | 1822 | static int _set_memory_array(unsigned long *addr, int numpages, |
Juergen Gross | c06814d | 2014-11-03 14:01:57 +0100 | [diff] [blame] | 1823 | enum page_cache_mode new_type) |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 1824 | { |
Toshi Kani | 623dffb | 2015-06-04 18:55:20 +0200 | [diff] [blame] | 1825 | enum page_cache_mode set_type; |
venkatesh.pallipadi@intel.com | 9fa3ab3 | 2009-04-09 14:26:49 -0700 | [diff] [blame] | 1826 | int i, j; |
| 1827 | int ret; |
| 1828 | |
Peter Zijlstra | 3c56735 | 2018-12-03 18:03:53 +0100 | [diff] [blame] | 1829 | for (i = 0; i < numpages; i++) { |
venkatesh.pallipadi@intel.com | 9fa3ab3 | 2009-04-09 14:26:49 -0700 | [diff] [blame] | 1830 | ret = reserve_memtype(__pa(addr[i]), __pa(addr[i]) + PAGE_SIZE, |
Pauli Nieminen | 4f64625 | 2010-04-01 12:45:01 +0000 | [diff] [blame] | 1831 | new_type, NULL); |
venkatesh.pallipadi@intel.com | 9fa3ab3 | 2009-04-09 14:26:49 -0700 | [diff] [blame] | 1832 | if (ret) |
| 1833 | goto out_free; |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 1834 | } |
| 1835 | |
Toshi Kani | 623dffb | 2015-06-04 18:55:20 +0200 | [diff] [blame] | 1836 | /* If WC, set to UC- first and then WC */ |
| 1837 | set_type = (new_type == _PAGE_CACHE_MODE_WC) ? |
| 1838 | _PAGE_CACHE_MODE_UC_MINUS : new_type; |
| 1839 | |
Peter Zijlstra | 3c56735 | 2018-12-03 18:03:53 +0100 | [diff] [blame] | 1840 | ret = change_page_attr_set(addr, numpages, |
Toshi Kani | 623dffb | 2015-06-04 18:55:20 +0200 | [diff] [blame] | 1841 | cachemode2pgprot(set_type), 1); |
Pauli Nieminen | 4f64625 | 2010-04-01 12:45:01 +0000 | [diff] [blame] | 1842 | |
Juergen Gross | c06814d | 2014-11-03 14:01:57 +0100 | [diff] [blame] | 1843 | if (!ret && new_type == _PAGE_CACHE_MODE_WC) |
Peter Zijlstra | 3c56735 | 2018-12-03 18:03:53 +0100 | [diff] [blame] | 1844 | ret = change_page_attr_set_clr(addr, numpages, |
Juergen Gross | c06814d | 2014-11-03 14:01:57 +0100 | [diff] [blame] | 1845 | cachemode2pgprot( |
| 1846 | _PAGE_CACHE_MODE_WC), |
Pauli Nieminen | 4f64625 | 2010-04-01 12:45:01 +0000 | [diff] [blame] | 1847 | __pgprot(_PAGE_CACHE_MASK), |
| 1848 | 0, CPA_ARRAY, NULL); |
venkatesh.pallipadi@intel.com | 9fa3ab3 | 2009-04-09 14:26:49 -0700 | [diff] [blame] | 1849 | if (ret) |
| 1850 | goto out_free; |
Rene Herman | c5e147c | 2008-08-22 01:02:20 +0200 | [diff] [blame] | 1851 | |
venkatesh.pallipadi@intel.com | 9fa3ab3 | 2009-04-09 14:26:49 -0700 | [diff] [blame] | 1852 | return 0; |
| 1853 | |
| 1854 | out_free: |
| 1855 | for (j = 0; j < i; j++) |
| 1856 | free_memtype(__pa(addr[j]), __pa(addr[j]) + PAGE_SIZE); |
| 1857 | |
| 1858 | return ret; |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 1859 | } |
Pauli Nieminen | 4f64625 | 2010-04-01 12:45:01 +0000 | [diff] [blame] | 1860 | |
Peter Zijlstra | 3c56735 | 2018-12-03 18:03:53 +0100 | [diff] [blame] | 1861 | int set_memory_array_uc(unsigned long *addr, int numpages) |
Pauli Nieminen | 4f64625 | 2010-04-01 12:45:01 +0000 | [diff] [blame] | 1862 | { |
Peter Zijlstra | 3c56735 | 2018-12-03 18:03:53 +0100 | [diff] [blame] | 1863 | return _set_memory_array(addr, numpages, _PAGE_CACHE_MODE_UC_MINUS); |
Pauli Nieminen | 4f64625 | 2010-04-01 12:45:01 +0000 | [diff] [blame] | 1864 | } |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 1865 | EXPORT_SYMBOL(set_memory_array_uc); |
| 1866 | |
Peter Zijlstra | 3c56735 | 2018-12-03 18:03:53 +0100 | [diff] [blame] | 1867 | int set_memory_array_wc(unsigned long *addr, int numpages) |
Pauli Nieminen | 4f64625 | 2010-04-01 12:45:01 +0000 | [diff] [blame] | 1868 | { |
Peter Zijlstra | 3c56735 | 2018-12-03 18:03:53 +0100 | [diff] [blame] | 1869 | return _set_memory_array(addr, numpages, _PAGE_CACHE_MODE_WC); |
Pauli Nieminen | 4f64625 | 2010-04-01 12:45:01 +0000 | [diff] [blame] | 1870 | } |
| 1871 | EXPORT_SYMBOL(set_memory_array_wc); |
| 1872 | |
Peter Zijlstra | 3c56735 | 2018-12-03 18:03:53 +0100 | [diff] [blame] | 1873 | int set_memory_array_wt(unsigned long *addr, int numpages) |
Toshi Kani | 623dffb | 2015-06-04 18:55:20 +0200 | [diff] [blame] | 1874 | { |
Peter Zijlstra | 3c56735 | 2018-12-03 18:03:53 +0100 | [diff] [blame] | 1875 | return _set_memory_array(addr, numpages, _PAGE_CACHE_MODE_WT); |
Toshi Kani | 623dffb | 2015-06-04 18:55:20 +0200 | [diff] [blame] | 1876 | } |
| 1877 | EXPORT_SYMBOL_GPL(set_memory_array_wt); |
| 1878 | |
venkatesh.pallipadi@intel.com | ef354af | 2008-03-18 17:00:23 -0700 | [diff] [blame] | 1879 | int _set_memory_wc(unsigned long addr, int numpages) |
| 1880 | { |
venkatesh.pallipadi@intel.com | 3869c4a | 2009-04-09 14:26:50 -0700 | [diff] [blame] | 1881 | int ret; |
Pallipadi, Venkatesh | bdc6340 | 2009-07-30 14:43:19 -0700 | [diff] [blame] | 1882 | |
venkatesh.pallipadi@intel.com | 3869c4a | 2009-04-09 14:26:50 -0700 | [diff] [blame] | 1883 | ret = change_page_attr_set(&addr, numpages, |
Juergen Gross | c06814d | 2014-11-03 14:01:57 +0100 | [diff] [blame] | 1884 | cachemode2pgprot(_PAGE_CACHE_MODE_UC_MINUS), |
| 1885 | 0); |
venkatesh.pallipadi@intel.com | 3869c4a | 2009-04-09 14:26:50 -0700 | [diff] [blame] | 1886 | if (!ret) { |
Peter Zijlstra | 5fe26b7 | 2018-12-03 18:03:48 +0100 | [diff] [blame] | 1887 | ret = change_page_attr_set_clr(&addr, numpages, |
| 1888 | cachemode2pgprot(_PAGE_CACHE_MODE_WC), |
Pallipadi, Venkatesh | bdc6340 | 2009-07-30 14:43:19 -0700 | [diff] [blame] | 1889 | __pgprot(_PAGE_CACHE_MASK), |
| 1890 | 0, 0, NULL); |
venkatesh.pallipadi@intel.com | 3869c4a | 2009-04-09 14:26:50 -0700 | [diff] [blame] | 1891 | } |
| 1892 | return ret; |
venkatesh.pallipadi@intel.com | ef354af | 2008-03-18 17:00:23 -0700 | [diff] [blame] | 1893 | } |
| 1894 | |
| 1895 | int set_memory_wc(unsigned long addr, int numpages) |
| 1896 | { |
venkatesh.pallipadi@intel.com | 9fa3ab3 | 2009-04-09 14:26:49 -0700 | [diff] [blame] | 1897 | int ret; |
| 1898 | |
venkatesh.pallipadi@intel.com | 9fa3ab3 | 2009-04-09 14:26:49 -0700 | [diff] [blame] | 1899 | ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE, |
Juergen Gross | e00c8cc | 2014-11-03 14:01:59 +0100 | [diff] [blame] | 1900 | _PAGE_CACHE_MODE_WC, NULL); |
venkatesh.pallipadi@intel.com | 9fa3ab3 | 2009-04-09 14:26:49 -0700 | [diff] [blame] | 1901 | if (ret) |
Toshi Kani | 623dffb | 2015-06-04 18:55:20 +0200 | [diff] [blame] | 1902 | return ret; |
venkatesh.pallipadi@intel.com | ef354af | 2008-03-18 17:00:23 -0700 | [diff] [blame] | 1903 | |
venkatesh.pallipadi@intel.com | 9fa3ab3 | 2009-04-09 14:26:49 -0700 | [diff] [blame] | 1904 | ret = _set_memory_wc(addr, numpages); |
| 1905 | if (ret) |
Toshi Kani | 623dffb | 2015-06-04 18:55:20 +0200 | [diff] [blame] | 1906 | free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE); |
venkatesh.pallipadi@intel.com | 9fa3ab3 | 2009-04-09 14:26:49 -0700 | [diff] [blame] | 1907 | |
venkatesh.pallipadi@intel.com | 9fa3ab3 | 2009-04-09 14:26:49 -0700 | [diff] [blame] | 1908 | return ret; |
venkatesh.pallipadi@intel.com | ef354af | 2008-03-18 17:00:23 -0700 | [diff] [blame] | 1909 | } |
| 1910 | EXPORT_SYMBOL(set_memory_wc); |
| 1911 | |
Toshi Kani | 623dffb | 2015-06-04 18:55:20 +0200 | [diff] [blame] | 1912 | int _set_memory_wt(unsigned long addr, int numpages) |
| 1913 | { |
| 1914 | return change_page_attr_set(&addr, numpages, |
| 1915 | cachemode2pgprot(_PAGE_CACHE_MODE_WT), 0); |
| 1916 | } |
| 1917 | |
| 1918 | int set_memory_wt(unsigned long addr, int numpages) |
| 1919 | { |
| 1920 | int ret; |
| 1921 | |
| 1922 | ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE, |
| 1923 | _PAGE_CACHE_MODE_WT, NULL); |
| 1924 | if (ret) |
| 1925 | return ret; |
| 1926 | |
| 1927 | ret = _set_memory_wt(addr, numpages); |
| 1928 | if (ret) |
| 1929 | free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE); |
| 1930 | |
| 1931 | return ret; |
| 1932 | } |
| 1933 | EXPORT_SYMBOL_GPL(set_memory_wt); |
| 1934 | |
venkatesh.pallipadi@intel.com | 1219333 | 2008-03-18 17:00:18 -0700 | [diff] [blame] | 1935 | int _set_memory_wb(unsigned long addr, int numpages) |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 1936 | { |
Juergen Gross | c06814d | 2014-11-03 14:01:57 +0100 | [diff] [blame] | 1937 | /* WB cache mode is hard wired to all cache attribute bits being 0 */ |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 1938 | return change_page_attr_clear(&addr, numpages, |
| 1939 | __pgprot(_PAGE_CACHE_MASK), 0); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 1940 | } |
venkatesh.pallipadi@intel.com | 1219333 | 2008-03-18 17:00:18 -0700 | [diff] [blame] | 1941 | |
| 1942 | int set_memory_wb(unsigned long addr, int numpages) |
| 1943 | { |
venkatesh.pallipadi@intel.com | 9fa3ab3 | 2009-04-09 14:26:49 -0700 | [diff] [blame] | 1944 | int ret; |
| 1945 | |
| 1946 | ret = _set_memory_wb(addr, numpages); |
| 1947 | if (ret) |
| 1948 | return ret; |
| 1949 | |
venkatesh.pallipadi@intel.com | c15238d | 2008-08-20 16:45:51 -0700 | [diff] [blame] | 1950 | free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE); |
venkatesh.pallipadi@intel.com | 9fa3ab3 | 2009-04-09 14:26:49 -0700 | [diff] [blame] | 1951 | return 0; |
venkatesh.pallipadi@intel.com | 1219333 | 2008-03-18 17:00:18 -0700 | [diff] [blame] | 1952 | } |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 1953 | EXPORT_SYMBOL(set_memory_wb); |
| 1954 | |
Peter Zijlstra | 3c56735 | 2018-12-03 18:03:53 +0100 | [diff] [blame] | 1955 | int set_memory_array_wb(unsigned long *addr, int numpages) |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 1956 | { |
| 1957 | int i; |
venkatesh.pallipadi@intel.com | a5593e0 | 2009-04-09 14:26:48 -0700 | [diff] [blame] | 1958 | int ret; |
| 1959 | |
Juergen Gross | c06814d | 2014-11-03 14:01:57 +0100 | [diff] [blame] | 1960 | /* WB cache mode is hard wired to all cache attribute bits being 0 */ |
Peter Zijlstra | 3c56735 | 2018-12-03 18:03:53 +0100 | [diff] [blame] | 1961 | ret = change_page_attr_clear(addr, numpages, |
venkatesh.pallipadi@intel.com | a5593e0 | 2009-04-09 14:26:48 -0700 | [diff] [blame] | 1962 | __pgprot(_PAGE_CACHE_MASK), 1); |
venkatesh.pallipadi@intel.com | 9fa3ab3 | 2009-04-09 14:26:49 -0700 | [diff] [blame] | 1963 | if (ret) |
| 1964 | return ret; |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 1965 | |
Peter Zijlstra | 3c56735 | 2018-12-03 18:03:53 +0100 | [diff] [blame] | 1966 | for (i = 0; i < numpages; i++) |
venkatesh.pallipadi@intel.com | 9fa3ab3 | 2009-04-09 14:26:49 -0700 | [diff] [blame] | 1967 | free_memtype(__pa(addr[i]), __pa(addr[i]) + PAGE_SIZE); |
Rene Herman | c5e147c | 2008-08-22 01:02:20 +0200 | [diff] [blame] | 1968 | |
venkatesh.pallipadi@intel.com | 9fa3ab3 | 2009-04-09 14:26:49 -0700 | [diff] [blame] | 1969 | return 0; |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 1970 | } |
| 1971 | EXPORT_SYMBOL(set_memory_array_wb); |
| 1972 | |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 1973 | int set_memory_x(unsigned long addr, int numpages) |
| 1974 | { |
H. Peter Anvin | 583140a | 2009-11-13 15:28:15 -0800 | [diff] [blame] | 1975 | if (!(__supported_pte_mask & _PAGE_NX)) |
| 1976 | return 0; |
| 1977 | |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 1978 | return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_NX), 0); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 1979 | } |
| 1980 | EXPORT_SYMBOL(set_memory_x); |
| 1981 | |
| 1982 | int set_memory_nx(unsigned long addr, int numpages) |
| 1983 | { |
H. Peter Anvin | 583140a | 2009-11-13 15:28:15 -0800 | [diff] [blame] | 1984 | if (!(__supported_pte_mask & _PAGE_NX)) |
| 1985 | return 0; |
| 1986 | |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 1987 | return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_NX), 0); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 1988 | } |
| 1989 | EXPORT_SYMBOL(set_memory_nx); |
| 1990 | |
| 1991 | int set_memory_ro(unsigned long addr, int numpages) |
| 1992 | { |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 1993 | return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_RW), 0); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 1994 | } |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 1995 | |
| 1996 | int set_memory_rw(unsigned long addr, int numpages) |
| 1997 | { |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 1998 | return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_RW), 0); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 1999 | } |
Ingo Molnar | f62d0f0 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 2000 | |
| 2001 | int set_memory_np(unsigned long addr, int numpages) |
| 2002 | { |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 2003 | return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_PRESENT), 0); |
Ingo Molnar | f62d0f0 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 2004 | } |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 2005 | |
Dave Hansen | c40a56a | 2018-08-02 15:58:31 -0700 | [diff] [blame] | 2006 | int set_memory_np_noalias(unsigned long addr, int numpages) |
| 2007 | { |
| 2008 | int cpa_flags = CPA_NO_CHECK_ALIAS; |
| 2009 | |
| 2010 | return change_page_attr_set_clr(&addr, numpages, __pgprot(0), |
| 2011 | __pgprot(_PAGE_PRESENT), 0, |
| 2012 | cpa_flags, NULL); |
| 2013 | } |
| 2014 | |
Andi Kleen | c9caa02 | 2008-03-12 03:53:29 +0100 | [diff] [blame] | 2015 | int set_memory_4k(unsigned long addr, int numpages) |
| 2016 | { |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 2017 | return change_page_attr_set_clr(&addr, numpages, __pgprot(0), |
venkatesh.pallipadi@intel.com | 9ae2847 | 2009-03-19 14:51:14 -0700 | [diff] [blame] | 2018 | __pgprot(0), 1, 0, NULL); |
Andi Kleen | c9caa02 | 2008-03-12 03:53:29 +0100 | [diff] [blame] | 2019 | } |
| 2020 | |
Dave Hansen | 39114b7 | 2018-04-06 13:55:17 -0700 | [diff] [blame] | 2021 | int set_memory_nonglobal(unsigned long addr, int numpages) |
| 2022 | { |
| 2023 | return change_page_attr_clear(&addr, numpages, |
| 2024 | __pgprot(_PAGE_GLOBAL), 0); |
| 2025 | } |
| 2026 | |
Dave Hansen | eac7073 | 2018-08-02 15:58:25 -0700 | [diff] [blame] | 2027 | int set_memory_global(unsigned long addr, int numpages) |
| 2028 | { |
| 2029 | return change_page_attr_set(&addr, numpages, |
| 2030 | __pgprot(_PAGE_GLOBAL), 0); |
| 2031 | } |
| 2032 | |
Tom Lendacky | 77bd234 | 2017-07-17 16:10:19 -0500 | [diff] [blame] | 2033 | static int __set_memory_enc_dec(unsigned long addr, int numpages, bool enc) |
| 2034 | { |
| 2035 | struct cpa_data cpa; |
Tom Lendacky | 77bd234 | 2017-07-17 16:10:19 -0500 | [diff] [blame] | 2036 | int ret; |
| 2037 | |
Tom Lendacky | a72ec5a | 2017-10-20 09:30:48 -0500 | [diff] [blame] | 2038 | /* Nothing to do if memory encryption is not active */ |
| 2039 | if (!mem_encrypt_active()) |
Tom Lendacky | 77bd234 | 2017-07-17 16:10:19 -0500 | [diff] [blame] | 2040 | return 0; |
| 2041 | |
| 2042 | /* Should not be working on unaligned addresses */ |
| 2043 | if (WARN_ONCE(addr & ~PAGE_MASK, "misaligned address: %#lx\n", addr)) |
| 2044 | addr &= PAGE_MASK; |
| 2045 | |
Tom Lendacky | 77bd234 | 2017-07-17 16:10:19 -0500 | [diff] [blame] | 2046 | memset(&cpa, 0, sizeof(cpa)); |
| 2047 | cpa.vaddr = &addr; |
| 2048 | cpa.numpages = numpages; |
| 2049 | cpa.mask_set = enc ? __pgprot(_PAGE_ENC) : __pgprot(0); |
| 2050 | cpa.mask_clr = enc ? __pgprot(0) : __pgprot(_PAGE_ENC); |
| 2051 | cpa.pgd = init_mm.pgd; |
| 2052 | |
| 2053 | /* Must avoid aliasing mappings in the highmem code */ |
| 2054 | kmap_flush_unused(); |
| 2055 | vm_unmap_aliases(); |
| 2056 | |
| 2057 | /* |
| 2058 | * Before changing the encryption attribute, we need to flush caches. |
| 2059 | */ |
Peter Zijlstra | fe0937b | 2018-12-03 18:03:51 +0100 | [diff] [blame] | 2060 | cpa_flush(&cpa, 1); |
Tom Lendacky | 77bd234 | 2017-07-17 16:10:19 -0500 | [diff] [blame] | 2061 | |
| 2062 | ret = __change_page_attr_set_clr(&cpa, 1); |
| 2063 | |
| 2064 | /* |
Peter Zijlstra | fe0937b | 2018-12-03 18:03:51 +0100 | [diff] [blame] | 2065 | * After changing the encryption attribute, we need to flush TLBs again |
| 2066 | * in case any speculative TLB caching occurred (but no need to flush |
| 2067 | * caches again). We could just use cpa_flush_all(), but in case TLB |
| 2068 | * flushing gets optimized in the cpa_flush() path use the same logic |
| 2069 | * as above. |
Tom Lendacky | 77bd234 | 2017-07-17 16:10:19 -0500 | [diff] [blame] | 2070 | */ |
Peter Zijlstra | fe0937b | 2018-12-03 18:03:51 +0100 | [diff] [blame] | 2071 | cpa_flush(&cpa, 0); |
Tom Lendacky | 77bd234 | 2017-07-17 16:10:19 -0500 | [diff] [blame] | 2072 | |
| 2073 | return ret; |
| 2074 | } |
| 2075 | |
| 2076 | int set_memory_encrypted(unsigned long addr, int numpages) |
| 2077 | { |
| 2078 | return __set_memory_enc_dec(addr, numpages, true); |
| 2079 | } |
Tom Lendacky | 95cf926 | 2017-07-17 16:10:26 -0500 | [diff] [blame] | 2080 | EXPORT_SYMBOL_GPL(set_memory_encrypted); |
Tom Lendacky | 77bd234 | 2017-07-17 16:10:19 -0500 | [diff] [blame] | 2081 | |
| 2082 | int set_memory_decrypted(unsigned long addr, int numpages) |
| 2083 | { |
| 2084 | return __set_memory_enc_dec(addr, numpages, false); |
| 2085 | } |
Tom Lendacky | 95cf926 | 2017-07-17 16:10:26 -0500 | [diff] [blame] | 2086 | EXPORT_SYMBOL_GPL(set_memory_decrypted); |
Tom Lendacky | 77bd234 | 2017-07-17 16:10:19 -0500 | [diff] [blame] | 2087 | |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 2088 | int set_pages_uc(struct page *page, int numpages) |
| 2089 | { |
| 2090 | unsigned long addr = (unsigned long)page_address(page); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 2091 | |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 2092 | return set_memory_uc(addr, numpages); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 2093 | } |
| 2094 | EXPORT_SYMBOL(set_pages_uc); |
| 2095 | |
Peter Zijlstra | 3c56735 | 2018-12-03 18:03:53 +0100 | [diff] [blame] | 2096 | static int _set_pages_array(struct page **pages, int numpages, |
Juergen Gross | c06814d | 2014-11-03 14:01:57 +0100 | [diff] [blame] | 2097 | enum page_cache_mode new_type) |
venkatesh.pallipadi@intel.com | 0f35075 | 2009-03-19 14:51:15 -0700 | [diff] [blame] | 2098 | { |
| 2099 | unsigned long start; |
| 2100 | unsigned long end; |
Toshi Kani | 623dffb | 2015-06-04 18:55:20 +0200 | [diff] [blame] | 2101 | enum page_cache_mode set_type; |
venkatesh.pallipadi@intel.com | 0f35075 | 2009-03-19 14:51:15 -0700 | [diff] [blame] | 2102 | int i; |
| 2103 | int free_idx; |
Pauli Nieminen | 4f64625 | 2010-04-01 12:45:01 +0000 | [diff] [blame] | 2104 | int ret; |
venkatesh.pallipadi@intel.com | 0f35075 | 2009-03-19 14:51:15 -0700 | [diff] [blame] | 2105 | |
Peter Zijlstra | 3c56735 | 2018-12-03 18:03:53 +0100 | [diff] [blame] | 2106 | for (i = 0; i < numpages; i++) { |
Thomas Hellstrom | 8523acf | 2009-08-03 09:25:45 +0200 | [diff] [blame] | 2107 | if (PageHighMem(pages[i])) |
| 2108 | continue; |
| 2109 | start = page_to_pfn(pages[i]) << PAGE_SHIFT; |
venkatesh.pallipadi@intel.com | 0f35075 | 2009-03-19 14:51:15 -0700 | [diff] [blame] | 2110 | end = start + PAGE_SIZE; |
Pauli Nieminen | 4f64625 | 2010-04-01 12:45:01 +0000 | [diff] [blame] | 2111 | if (reserve_memtype(start, end, new_type, NULL)) |
venkatesh.pallipadi@intel.com | 0f35075 | 2009-03-19 14:51:15 -0700 | [diff] [blame] | 2112 | goto err_out; |
| 2113 | } |
| 2114 | |
Toshi Kani | 623dffb | 2015-06-04 18:55:20 +0200 | [diff] [blame] | 2115 | /* If WC, set to UC- first and then WC */ |
| 2116 | set_type = (new_type == _PAGE_CACHE_MODE_WC) ? |
| 2117 | _PAGE_CACHE_MODE_UC_MINUS : new_type; |
| 2118 | |
Peter Zijlstra | 3c56735 | 2018-12-03 18:03:53 +0100 | [diff] [blame] | 2119 | ret = cpa_set_pages_array(pages, numpages, |
Toshi Kani | 623dffb | 2015-06-04 18:55:20 +0200 | [diff] [blame] | 2120 | cachemode2pgprot(set_type)); |
Juergen Gross | c06814d | 2014-11-03 14:01:57 +0100 | [diff] [blame] | 2121 | if (!ret && new_type == _PAGE_CACHE_MODE_WC) |
Peter Zijlstra | 3c56735 | 2018-12-03 18:03:53 +0100 | [diff] [blame] | 2122 | ret = change_page_attr_set_clr(NULL, numpages, |
Juergen Gross | c06814d | 2014-11-03 14:01:57 +0100 | [diff] [blame] | 2123 | cachemode2pgprot( |
| 2124 | _PAGE_CACHE_MODE_WC), |
Pauli Nieminen | 4f64625 | 2010-04-01 12:45:01 +0000 | [diff] [blame] | 2125 | __pgprot(_PAGE_CACHE_MASK), |
| 2126 | 0, CPA_PAGES_ARRAY, pages); |
| 2127 | if (ret) |
| 2128 | goto err_out; |
| 2129 | return 0; /* Success */ |
venkatesh.pallipadi@intel.com | 0f35075 | 2009-03-19 14:51:15 -0700 | [diff] [blame] | 2130 | err_out: |
| 2131 | free_idx = i; |
| 2132 | for (i = 0; i < free_idx; i++) { |
Thomas Hellstrom | 8523acf | 2009-08-03 09:25:45 +0200 | [diff] [blame] | 2133 | if (PageHighMem(pages[i])) |
| 2134 | continue; |
| 2135 | start = page_to_pfn(pages[i]) << PAGE_SHIFT; |
venkatesh.pallipadi@intel.com | 0f35075 | 2009-03-19 14:51:15 -0700 | [diff] [blame] | 2136 | end = start + PAGE_SIZE; |
| 2137 | free_memtype(start, end); |
| 2138 | } |
| 2139 | return -EINVAL; |
| 2140 | } |
Pauli Nieminen | 4f64625 | 2010-04-01 12:45:01 +0000 | [diff] [blame] | 2141 | |
Peter Zijlstra | 3c56735 | 2018-12-03 18:03:53 +0100 | [diff] [blame] | 2142 | int set_pages_array_uc(struct page **pages, int numpages) |
Pauli Nieminen | 4f64625 | 2010-04-01 12:45:01 +0000 | [diff] [blame] | 2143 | { |
Peter Zijlstra | 3c56735 | 2018-12-03 18:03:53 +0100 | [diff] [blame] | 2144 | return _set_pages_array(pages, numpages, _PAGE_CACHE_MODE_UC_MINUS); |
Pauli Nieminen | 4f64625 | 2010-04-01 12:45:01 +0000 | [diff] [blame] | 2145 | } |
venkatesh.pallipadi@intel.com | 0f35075 | 2009-03-19 14:51:15 -0700 | [diff] [blame] | 2146 | EXPORT_SYMBOL(set_pages_array_uc); |
| 2147 | |
Peter Zijlstra | 3c56735 | 2018-12-03 18:03:53 +0100 | [diff] [blame] | 2148 | int set_pages_array_wc(struct page **pages, int numpages) |
Pauli Nieminen | 4f64625 | 2010-04-01 12:45:01 +0000 | [diff] [blame] | 2149 | { |
Peter Zijlstra | 3c56735 | 2018-12-03 18:03:53 +0100 | [diff] [blame] | 2150 | return _set_pages_array(pages, numpages, _PAGE_CACHE_MODE_WC); |
Pauli Nieminen | 4f64625 | 2010-04-01 12:45:01 +0000 | [diff] [blame] | 2151 | } |
| 2152 | EXPORT_SYMBOL(set_pages_array_wc); |
| 2153 | |
Peter Zijlstra | 3c56735 | 2018-12-03 18:03:53 +0100 | [diff] [blame] | 2154 | int set_pages_array_wt(struct page **pages, int numpages) |
Toshi Kani | 623dffb | 2015-06-04 18:55:20 +0200 | [diff] [blame] | 2155 | { |
Peter Zijlstra | 3c56735 | 2018-12-03 18:03:53 +0100 | [diff] [blame] | 2156 | return _set_pages_array(pages, numpages, _PAGE_CACHE_MODE_WT); |
Toshi Kani | 623dffb | 2015-06-04 18:55:20 +0200 | [diff] [blame] | 2157 | } |
| 2158 | EXPORT_SYMBOL_GPL(set_pages_array_wt); |
| 2159 | |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 2160 | int set_pages_wb(struct page *page, int numpages) |
| 2161 | { |
| 2162 | unsigned long addr = (unsigned long)page_address(page); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 2163 | |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 2164 | return set_memory_wb(addr, numpages); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 2165 | } |
| 2166 | EXPORT_SYMBOL(set_pages_wb); |
| 2167 | |
Peter Zijlstra | 3c56735 | 2018-12-03 18:03:53 +0100 | [diff] [blame] | 2168 | int set_pages_array_wb(struct page **pages, int numpages) |
venkatesh.pallipadi@intel.com | 0f35075 | 2009-03-19 14:51:15 -0700 | [diff] [blame] | 2169 | { |
| 2170 | int retval; |
| 2171 | unsigned long start; |
| 2172 | unsigned long end; |
| 2173 | int i; |
| 2174 | |
Juergen Gross | c06814d | 2014-11-03 14:01:57 +0100 | [diff] [blame] | 2175 | /* WB cache mode is hard wired to all cache attribute bits being 0 */ |
Peter Zijlstra | 3c56735 | 2018-12-03 18:03:53 +0100 | [diff] [blame] | 2176 | retval = cpa_clear_pages_array(pages, numpages, |
venkatesh.pallipadi@intel.com | 0f35075 | 2009-03-19 14:51:15 -0700 | [diff] [blame] | 2177 | __pgprot(_PAGE_CACHE_MASK)); |
venkatesh.pallipadi@intel.com | 9fa3ab3 | 2009-04-09 14:26:49 -0700 | [diff] [blame] | 2178 | if (retval) |
| 2179 | return retval; |
venkatesh.pallipadi@intel.com | 0f35075 | 2009-03-19 14:51:15 -0700 | [diff] [blame] | 2180 | |
Peter Zijlstra | 3c56735 | 2018-12-03 18:03:53 +0100 | [diff] [blame] | 2181 | for (i = 0; i < numpages; i++) { |
Thomas Hellstrom | 8523acf | 2009-08-03 09:25:45 +0200 | [diff] [blame] | 2182 | if (PageHighMem(pages[i])) |
| 2183 | continue; |
| 2184 | start = page_to_pfn(pages[i]) << PAGE_SHIFT; |
venkatesh.pallipadi@intel.com | 0f35075 | 2009-03-19 14:51:15 -0700 | [diff] [blame] | 2185 | end = start + PAGE_SIZE; |
| 2186 | free_memtype(start, end); |
| 2187 | } |
| 2188 | |
venkatesh.pallipadi@intel.com | 9fa3ab3 | 2009-04-09 14:26:49 -0700 | [diff] [blame] | 2189 | return 0; |
venkatesh.pallipadi@intel.com | 0f35075 | 2009-03-19 14:51:15 -0700 | [diff] [blame] | 2190 | } |
| 2191 | EXPORT_SYMBOL(set_pages_array_wb); |
| 2192 | |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 2193 | int set_pages_x(struct page *page, int numpages) |
| 2194 | { |
| 2195 | unsigned long addr = (unsigned long)page_address(page); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 2196 | |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 2197 | return set_memory_x(addr, numpages); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 2198 | } |
| 2199 | EXPORT_SYMBOL(set_pages_x); |
| 2200 | |
| 2201 | int set_pages_nx(struct page *page, int numpages) |
| 2202 | { |
| 2203 | unsigned long addr = (unsigned long)page_address(page); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 2204 | |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 2205 | return set_memory_nx(addr, numpages); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 2206 | } |
| 2207 | EXPORT_SYMBOL(set_pages_nx); |
| 2208 | |
| 2209 | int set_pages_ro(struct page *page, int numpages) |
| 2210 | { |
| 2211 | unsigned long addr = (unsigned long)page_address(page); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 2212 | |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 2213 | return set_memory_ro(addr, numpages); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 2214 | } |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 2215 | |
| 2216 | int set_pages_rw(struct page *page, int numpages) |
| 2217 | { |
| 2218 | unsigned long addr = (unsigned long)page_address(page); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 2219 | |
Thomas Gleixner | d7c8f21 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 2220 | return set_memory_rw(addr, numpages); |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 2221 | } |
Arjan van de Ven | 75cbade | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 2222 | |
Ingo Molnar | f62d0f0 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 2223 | static int __set_pages_p(struct page *page, int numpages) |
| 2224 | { |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 2225 | unsigned long tempaddr = (unsigned long) page_address(page); |
| 2226 | struct cpa_data cpa = { .vaddr = &tempaddr, |
Borislav Petkov | 82f0712 | 2013-10-31 17:25:07 +0100 | [diff] [blame] | 2227 | .pgd = NULL, |
Thomas Gleixner | 72e458d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 2228 | .numpages = numpages, |
| 2229 | .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW), |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 2230 | .mask_clr = __pgprot(0), |
| 2231 | .flags = 0}; |
Thomas Gleixner | 72932c7 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 2232 | |
Suresh Siddha | 55121b4 | 2008-09-23 14:00:40 -0700 | [diff] [blame] | 2233 | /* |
| 2234 | * No alias checking needed for setting present flag. otherwise, |
| 2235 | * we may need to break large pages for 64-bit kernel text |
| 2236 | * mappings (this adds to complexity if we want to do this from |
| 2237 | * atomic context especially). Let's keep it simple! |
| 2238 | */ |
| 2239 | return __change_page_attr_set_clr(&cpa, 0); |
Ingo Molnar | f62d0f0 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 2240 | } |
| 2241 | |
| 2242 | static int __set_pages_np(struct page *page, int numpages) |
| 2243 | { |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 2244 | unsigned long tempaddr = (unsigned long) page_address(page); |
| 2245 | struct cpa_data cpa = { .vaddr = &tempaddr, |
Borislav Petkov | 82f0712 | 2013-10-31 17:25:07 +0100 | [diff] [blame] | 2246 | .pgd = NULL, |
Thomas Gleixner | 72e458d | 2008-02-04 16:48:07 +0100 | [diff] [blame] | 2247 | .numpages = numpages, |
| 2248 | .mask_set = __pgprot(0), |
Shaohua Li | d75586a | 2008-08-21 10:46:06 +0800 | [diff] [blame] | 2249 | .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW), |
| 2250 | .flags = 0}; |
Thomas Gleixner | 72932c7 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 2251 | |
Suresh Siddha | 55121b4 | 2008-09-23 14:00:40 -0700 | [diff] [blame] | 2252 | /* |
| 2253 | * No alias checking needed for setting not present flag. otherwise, |
| 2254 | * we may need to break large pages for 64-bit kernel text |
| 2255 | * mappings (this adds to complexity if we want to do this from |
| 2256 | * atomic context especially). Let's keep it simple! |
| 2257 | */ |
| 2258 | return __change_page_attr_set_clr(&cpa, 0); |
Ingo Molnar | f62d0f0 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 2259 | } |
| 2260 | |
Rick Edgecombe | d253ca0 | 2019-04-25 17:11:34 -0700 | [diff] [blame] | 2261 | int set_direct_map_invalid_noflush(struct page *page) |
| 2262 | { |
| 2263 | return __set_pages_np(page, 1); |
| 2264 | } |
| 2265 | |
| 2266 | int set_direct_map_default_noflush(struct page *page) |
| 2267 | { |
| 2268 | return __set_pages_p(page, 1); |
| 2269 | } |
| 2270 | |
Joonsoo Kim | 031bc57 | 2014-12-12 16:55:52 -0800 | [diff] [blame] | 2271 | void __kernel_map_pages(struct page *page, int numpages, int enable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2272 | { |
| 2273 | if (PageHighMem(page)) |
| 2274 | return; |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 2275 | if (!enable) { |
Ingo Molnar | f9b8404 | 2006-06-27 02:54:49 -0700 | [diff] [blame] | 2276 | debug_check_no_locks_freed(page_address(page), |
| 2277 | numpages * PAGE_SIZE); |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 2278 | } |
Ingo Molnar | de5097c | 2006-01-09 15:59:21 -0800 | [diff] [blame] | 2279 | |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 2280 | /* |
Ingo Molnar | f8d8406 | 2008-02-13 14:09:53 +0100 | [diff] [blame] | 2281 | * The return value is ignored as the calls cannot fail. |
Suresh Siddha | 55121b4 | 2008-09-23 14:00:40 -0700 | [diff] [blame] | 2282 | * Large pages for identity mappings are not used at boot time |
| 2283 | * and hence no memory allocations during large page split. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2284 | */ |
Ingo Molnar | f62d0f0 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 2285 | if (enable) |
| 2286 | __set_pages_p(page, numpages); |
| 2287 | else |
| 2288 | __set_pages_np(page, numpages); |
Ingo Molnar | 9f4c815 | 2008-01-30 13:33:41 +0100 | [diff] [blame] | 2289 | |
| 2290 | /* |
Ingo Molnar | e4b71dc | 2008-01-30 13:34:04 +0100 | [diff] [blame] | 2291 | * We should perform an IPI and flush all tlbs, |
Sebastian Andrzej Siewior | f77084d | 2018-10-17 12:34:32 +0200 | [diff] [blame] | 2292 | * but that can deadlock->flush only current cpu. |
| 2293 | * Preemption needs to be disabled around __flush_tlb_all() due to |
| 2294 | * CR3 reload in __native_flush_tlb(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2295 | */ |
Sebastian Andrzej Siewior | f77084d | 2018-10-17 12:34:32 +0200 | [diff] [blame] | 2296 | preempt_disable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2297 | __flush_tlb_all(); |
Sebastian Andrzej Siewior | f77084d | 2018-10-17 12:34:32 +0200 | [diff] [blame] | 2298 | preempt_enable(); |
Boris Ostrovsky | 2656460 | 2013-04-11 13:59:52 -0400 | [diff] [blame] | 2299 | |
| 2300 | arch_flush_lazy_mmu_mode(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2301 | } |
Rafael J. Wysocki | 8a235ef | 2008-02-20 01:47:44 +0100 | [diff] [blame] | 2302 | |
| 2303 | #ifdef CONFIG_HIBERNATION |
Rafael J. Wysocki | 8a235ef | 2008-02-20 01:47:44 +0100 | [diff] [blame] | 2304 | bool kernel_page_present(struct page *page) |
| 2305 | { |
| 2306 | unsigned int level; |
| 2307 | pte_t *pte; |
| 2308 | |
| 2309 | if (PageHighMem(page)) |
| 2310 | return false; |
| 2311 | |
| 2312 | pte = lookup_address((unsigned long)page_address(page), &level); |
| 2313 | return (pte_val(*pte) & _PAGE_PRESENT); |
| 2314 | } |
Rafael J. Wysocki | 8a235ef | 2008-02-20 01:47:44 +0100 | [diff] [blame] | 2315 | #endif /* CONFIG_HIBERNATION */ |
| 2316 | |
Sai Praneeth Prakhya | 7e0dabd | 2018-11-29 18:12:23 +0100 | [diff] [blame] | 2317 | int __init kernel_map_pages_in_pgd(pgd_t *pgd, u64 pfn, unsigned long address, |
| 2318 | unsigned numpages, unsigned long page_flags) |
Borislav Petkov | 82f0712 | 2013-10-31 17:25:07 +0100 | [diff] [blame] | 2319 | { |
| 2320 | int retval = -EINVAL; |
| 2321 | |
| 2322 | struct cpa_data cpa = { |
| 2323 | .vaddr = &address, |
| 2324 | .pfn = pfn, |
| 2325 | .pgd = pgd, |
| 2326 | .numpages = numpages, |
| 2327 | .mask_set = __pgprot(0), |
| 2328 | .mask_clr = __pgprot(0), |
| 2329 | .flags = 0, |
| 2330 | }; |
| 2331 | |
Sai Praneeth Prakhya | 7e0dabd | 2018-11-29 18:12:23 +0100 | [diff] [blame] | 2332 | WARN_ONCE(num_online_cpus() > 1, "Don't call after initializing SMP"); |
| 2333 | |
Borislav Petkov | 82f0712 | 2013-10-31 17:25:07 +0100 | [diff] [blame] | 2334 | if (!(__supported_pte_mask & _PAGE_NX)) |
| 2335 | goto out; |
| 2336 | |
| 2337 | if (!(page_flags & _PAGE_NX)) |
| 2338 | cpa.mask_clr = __pgprot(_PAGE_NX); |
| 2339 | |
Sai Praneeth | 15f003d | 2016-02-17 12:36:04 +0000 | [diff] [blame] | 2340 | if (!(page_flags & _PAGE_RW)) |
| 2341 | cpa.mask_clr = __pgprot(_PAGE_RW); |
| 2342 | |
Tom Lendacky | 21729f8 | 2017-07-17 16:10:07 -0500 | [diff] [blame] | 2343 | if (!(page_flags & _PAGE_ENC)) |
| 2344 | cpa.mask_clr = pgprot_encrypted(cpa.mask_clr); |
| 2345 | |
Borislav Petkov | 82f0712 | 2013-10-31 17:25:07 +0100 | [diff] [blame] | 2346 | cpa.mask_set = __pgprot(_PAGE_PRESENT | page_flags); |
| 2347 | |
| 2348 | retval = __change_page_attr_set_clr(&cpa, 0); |
| 2349 | __flush_tlb_all(); |
| 2350 | |
| 2351 | out: |
| 2352 | return retval; |
| 2353 | } |
| 2354 | |
Arjan van de Ven | d1028a1 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 2355 | /* |
Sai Praneeth Prakhya | 7e0dabd | 2018-11-29 18:12:23 +0100 | [diff] [blame] | 2356 | * __flush_tlb_all() flushes mappings only on current CPU and hence this |
| 2357 | * function shouldn't be used in an SMP environment. Presently, it's used only |
| 2358 | * during boot (way before smp_init()) by EFI subsystem and hence is ok. |
| 2359 | */ |
| 2360 | int __init kernel_unmap_pages_in_pgd(pgd_t *pgd, unsigned long address, |
| 2361 | unsigned long numpages) |
| 2362 | { |
| 2363 | int retval; |
| 2364 | |
| 2365 | /* |
| 2366 | * The typical sequence for unmapping is to find a pte through |
| 2367 | * lookup_address_in_pgd() (ideally, it should never return NULL because |
| 2368 | * the address is already mapped) and change it's protections. As pfn is |
| 2369 | * the *target* of a mapping, it's not useful while unmapping. |
| 2370 | */ |
| 2371 | struct cpa_data cpa = { |
| 2372 | .vaddr = &address, |
| 2373 | .pfn = 0, |
| 2374 | .pgd = pgd, |
| 2375 | .numpages = numpages, |
| 2376 | .mask_set = __pgprot(0), |
| 2377 | .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW), |
| 2378 | .flags = 0, |
| 2379 | }; |
| 2380 | |
| 2381 | WARN_ONCE(num_online_cpus() > 1, "Don't call after initializing SMP"); |
| 2382 | |
| 2383 | retval = __change_page_attr_set_clr(&cpa, 0); |
| 2384 | __flush_tlb_all(); |
| 2385 | |
| 2386 | return retval; |
| 2387 | } |
| 2388 | |
| 2389 | /* |
Arjan van de Ven | d1028a1 | 2008-01-30 13:34:07 +0100 | [diff] [blame] | 2390 | * The testcases use internal knowledge of the implementation that shouldn't |
| 2391 | * be exposed to the rest of the kernel. Include these directly here. |
| 2392 | */ |
| 2393 | #ifdef CONFIG_CPA_DEBUG |
| 2394 | #include "pageattr-test.c" |
| 2395 | #endif |