blob: 727158cb3b3c91111f7bfe63a3b2fd9744972c1a [file] [log] [blame]
Ingo Molnar9f4c8152008-01-30 13:33:41 +01001/*
2 * Copyright 2002 Andi Kleen, SuSE Labs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Thanks to Ben LaHaise for precious feedback.
Ingo Molnar9f4c8152008-01-30 13:33:41 +01004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/highmem.h>
Ingo Molnar81922062008-01-30 13:34:04 +01006#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/module.h>
Ingo Molnar9f4c8152008-01-30 13:33:41 +01008#include <linux/sched.h>
Ingo Molnar9f4c8152008-01-30 13:33:41 +01009#include <linux/mm.h>
Thomas Gleixner76ebd052008-02-09 23:24:09 +010010#include <linux/interrupt.h>
Thomas Gleixneree7ae7a2008-04-17 17:40:45 +020011#include <linux/seq_file.h>
12#include <linux/debugfs.h>
Tejun Heoe59a1bb2009-06-22 11:56:24 +090013#include <linux/pfn.h>
Tejun Heo8c4bfc62009-07-04 08:10:59 +090014#include <linux/percpu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/gfp.h>
Matthieu Castet5bd5a452010-11-16 22:31:26 +010016#include <linux/pci.h>
Stephen Rothwelld6472302015-06-02 19:01:38 +100017#include <linux/vmalloc.h>
Ingo Molnar9f4c8152008-01-30 13:33:41 +010018
Thomas Gleixner950f9d92008-01-30 13:34:06 +010019#include <asm/e820.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/processor.h>
21#include <asm/tlbflush.h>
Dave Jonesf8af0952006-01-06 00:12:10 -080022#include <asm/sections.h>
Jeremy Fitzhardinge93dbda72009-02-26 17:35:44 -080023#include <asm/setup.h>
Ingo Molnar9f4c8152008-01-30 13:33:41 +010024#include <asm/uaccess.h>
25#include <asm/pgalloc.h>
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +010026#include <asm/proto.h>
venkatesh.pallipadi@intel.com12193332008-03-18 17:00:18 -070027#include <asm/pat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Ingo Molnar9df84992008-02-04 16:48:09 +010029/*
30 * The current flushing context - we pass it instead of 5 arguments:
31 */
Thomas Gleixner72e458d2008-02-04 16:48:07 +010032struct cpa_data {
Shaohua Lid75586a2008-08-21 10:46:06 +080033 unsigned long *vaddr;
Borislav Petkov0fd64c22013-10-31 17:25:00 +010034 pgd_t *pgd;
Thomas Gleixner72e458d2008-02-04 16:48:07 +010035 pgprot_t mask_set;
36 pgprot_t mask_clr;
Thomas Gleixner65e074d2008-02-04 16:48:07 +010037 int numpages;
Shaohua Lid75586a2008-08-21 10:46:06 +080038 int flags;
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +010039 unsigned long pfn;
Andi Kleenc9caa022008-03-12 03:53:29 +010040 unsigned force_split : 1;
Shaohua Lid75586a2008-08-21 10:46:06 +080041 int curpage;
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -070042 struct page **pages;
Thomas Gleixner72e458d2008-02-04 16:48:07 +010043};
44
Suresh Siddhaad5ca552008-09-23 14:00:42 -070045/*
46 * Serialize cpa() (for !DEBUG_PAGEALLOC which uses large identity mappings)
47 * using cpa_lock. So that we don't allow any other cpu, with stale large tlb
48 * entries change the page attribute in parallel to some other cpu
49 * splitting a large page entry along with changing the attribute.
50 */
51static DEFINE_SPINLOCK(cpa_lock);
52
Shaohua Lid75586a2008-08-21 10:46:06 +080053#define CPA_FLUSHTLB 1
54#define CPA_ARRAY 2
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -070055#define CPA_PAGES_ARRAY 4
Shaohua Lid75586a2008-08-21 10:46:06 +080056
Thomas Gleixner65280e62008-05-05 16:35:21 +020057#ifdef CONFIG_PROC_FS
Andi Kleence0c0e52008-05-02 11:46:49 +020058static unsigned long direct_pages_count[PG_LEVEL_NUM];
59
Thomas Gleixner65280e62008-05-05 16:35:21 +020060void update_page_count(int level, unsigned long pages)
Andi Kleence0c0e52008-05-02 11:46:49 +020061{
Andi Kleence0c0e52008-05-02 11:46:49 +020062 /* Protect against CPA */
Andrea Arcangelia79e53d2011-02-16 15:45:22 -080063 spin_lock(&pgd_lock);
Andi Kleence0c0e52008-05-02 11:46:49 +020064 direct_pages_count[level] += pages;
Andrea Arcangelia79e53d2011-02-16 15:45:22 -080065 spin_unlock(&pgd_lock);
Andi Kleence0c0e52008-05-02 11:46:49 +020066}
67
Thomas Gleixner65280e62008-05-05 16:35:21 +020068static void split_page_count(int level)
69{
70 direct_pages_count[level]--;
71 direct_pages_count[level - 1] += PTRS_PER_PTE;
72}
73
Alexey Dobriyane1759c22008-10-15 23:50:22 +040074void arch_report_meminfo(struct seq_file *m)
Thomas Gleixner65280e62008-05-05 16:35:21 +020075{
Hugh Dickinsb9c3bfc2008-11-06 12:05:40 +000076 seq_printf(m, "DirectMap4k: %8lu kB\n",
Hugh Dickinsa06de632008-08-15 13:58:32 +010077 direct_pages_count[PG_LEVEL_4K] << 2);
78#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
Hugh Dickinsb9c3bfc2008-11-06 12:05:40 +000079 seq_printf(m, "DirectMap2M: %8lu kB\n",
Hugh Dickinsa06de632008-08-15 13:58:32 +010080 direct_pages_count[PG_LEVEL_2M] << 11);
81#else
Hugh Dickinsb9c3bfc2008-11-06 12:05:40 +000082 seq_printf(m, "DirectMap4M: %8lu kB\n",
Hugh Dickinsa06de632008-08-15 13:58:32 +010083 direct_pages_count[PG_LEVEL_2M] << 12);
84#endif
Hugh Dickinsa06de632008-08-15 13:58:32 +010085 if (direct_gbpages)
Hugh Dickinsb9c3bfc2008-11-06 12:05:40 +000086 seq_printf(m, "DirectMap1G: %8lu kB\n",
Hugh Dickinsa06de632008-08-15 13:58:32 +010087 direct_pages_count[PG_LEVEL_1G] << 20);
Thomas Gleixner65280e62008-05-05 16:35:21 +020088}
89#else
90static inline void split_page_count(int level) { }
91#endif
92
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +010093#ifdef CONFIG_X86_64
94
95static inline unsigned long highmap_start_pfn(void)
96{
Alexander Duyckfc8d7822012-11-16 13:57:13 -080097 return __pa_symbol(_text) >> PAGE_SHIFT;
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +010098}
99
100static inline unsigned long highmap_end_pfn(void)
101{
Alexander Duyckfc8d7822012-11-16 13:57:13 -0800102 return __pa_symbol(roundup(_brk_end, PMD_SIZE)) >> PAGE_SHIFT;
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100103}
104
105#endif
106
Ingo Molnar92cb54a2008-02-13 14:37:52 +0100107#ifdef CONFIG_DEBUG_PAGEALLOC
108# define debug_pagealloc 1
109#else
110# define debug_pagealloc 0
111#endif
112
Arjan van de Vened724be2008-01-30 13:34:04 +0100113static inline int
114within(unsigned long addr, unsigned long start, unsigned long end)
Ingo Molnar687c4822008-01-30 13:34:04 +0100115{
Arjan van de Vened724be2008-01-30 13:34:04 +0100116 return addr >= start && addr < end;
117}
118
119/*
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100120 * Flushing functions
121 */
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +0100122
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +0100123/**
124 * clflush_cache_range - flush a cache range with clflush
Wanpeng Li9efc31b2012-06-10 10:50:52 +0800125 * @vaddr: virtual start address
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +0100126 * @size: number of bytes to flush
127 *
Ross Zwisler8b80fd82014-02-26 12:06:50 -0700128 * clflushopt is an unordered instruction which needs fencing with mfence or
129 * sfence to avoid ordering issues.
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +0100130 */
Ingo Molnar4c61afc2008-01-30 13:34:09 +0100131void clflush_cache_range(void *vaddr, unsigned int size)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100132{
Ross Zwisler6c434d62015-05-11 10:15:49 +0200133 unsigned long clflush_mask = boot_cpu_data.x86_clflush_size - 1;
134 void *vend = vaddr + size;
135 void *p;
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100136
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +0100137 mb();
Ingo Molnar4c61afc2008-01-30 13:34:09 +0100138
Ross Zwisler6c434d62015-05-11 10:15:49 +0200139 for (p = (void *)((unsigned long)vaddr & ~clflush_mask);
140 p < vend; p += boot_cpu_data.x86_clflush_size)
141 clflushopt(p);
Ingo Molnar4c61afc2008-01-30 13:34:09 +0100142
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +0100143 mb();
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100144}
Eric Anholte517a5e2009-09-10 17:48:48 -0700145EXPORT_SYMBOL_GPL(clflush_cache_range);
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100146
Thomas Gleixneraf1e6842008-01-30 13:34:08 +0100147static void __cpa_flush_all(void *arg)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100148{
Andi Kleen6bb83832008-02-04 16:48:06 +0100149 unsigned long cache = (unsigned long)arg;
150
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100151 /*
152 * Flush all to work around Errata in early athlons regarding
153 * large page flushing.
154 */
155 __flush_tlb_all();
156
venkatesh.pallipadi@intel.com0b827532009-05-22 13:23:37 -0700157 if (cache && boot_cpu_data.x86 >= 4)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100158 wbinvd();
159}
160
Andi Kleen6bb83832008-02-04 16:48:06 +0100161static void cpa_flush_all(unsigned long cache)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100162{
163 BUG_ON(irqs_disabled());
164
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200165 on_each_cpu(__cpa_flush_all, (void *) cache, 1);
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100166}
167
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100168static void __cpa_flush_range(void *arg)
169{
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100170 /*
171 * We could optimize that further and do individual per page
172 * tlb invalidates for a low number of pages. Caveat: we must
173 * flush the high aliases on 64bit as well.
174 */
175 __flush_tlb_all();
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100176}
177
Andi Kleen6bb83832008-02-04 16:48:06 +0100178static void cpa_flush_range(unsigned long start, int numpages, int cache)
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100179{
Ingo Molnar4c61afc2008-01-30 13:34:09 +0100180 unsigned int i, level;
181 unsigned long addr;
182
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100183 BUG_ON(irqs_disabled());
Ingo Molnar4c61afc2008-01-30 13:34:09 +0100184 WARN_ON(PAGE_ALIGN(start) != start);
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100185
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200186 on_each_cpu(__cpa_flush_range, NULL, 1);
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100187
Andi Kleen6bb83832008-02-04 16:48:06 +0100188 if (!cache)
189 return;
190
Thomas Gleixner3b233e52008-01-30 13:34:08 +0100191 /*
192 * We only need to flush on one CPU,
193 * clflush is a MESI-coherent instruction that
194 * will cause all other CPUs to flush the same
195 * cachelines:
196 */
Ingo Molnar4c61afc2008-01-30 13:34:09 +0100197 for (i = 0, addr = start; i < numpages; i++, addr += PAGE_SIZE) {
198 pte_t *pte = lookup_address(addr, &level);
199
200 /*
201 * Only flush present addresses:
202 */
Thomas Gleixner7bfb72e2008-02-04 16:48:08 +0100203 if (pte && (pte_val(*pte) & _PAGE_PRESENT))
Ingo Molnar4c61afc2008-01-30 13:34:09 +0100204 clflush_cache_range((void *) addr, PAGE_SIZE);
205 }
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100206}
207
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700208static void cpa_flush_array(unsigned long *start, int numpages, int cache,
209 int in_flags, struct page **pages)
Shaohua Lid75586a2008-08-21 10:46:06 +0800210{
211 unsigned int i, level;
Pallipadi, Venkatesh21717872009-05-26 10:33:35 -0700212 unsigned long do_wbinvd = cache && numpages >= 1024; /* 4M threshold */
Shaohua Lid75586a2008-08-21 10:46:06 +0800213
214 BUG_ON(irqs_disabled());
215
Pallipadi, Venkatesh21717872009-05-26 10:33:35 -0700216 on_each_cpu(__cpa_flush_all, (void *) do_wbinvd, 1);
Shaohua Lid75586a2008-08-21 10:46:06 +0800217
Pallipadi, Venkatesh21717872009-05-26 10:33:35 -0700218 if (!cache || do_wbinvd)
Shaohua Lid75586a2008-08-21 10:46:06 +0800219 return;
220
Shaohua Lid75586a2008-08-21 10:46:06 +0800221 /*
222 * We only need to flush on one CPU,
223 * clflush is a MESI-coherent instruction that
224 * will cause all other CPUs to flush the same
225 * cachelines:
226 */
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700227 for (i = 0; i < numpages; i++) {
228 unsigned long addr;
229 pte_t *pte;
230
231 if (in_flags & CPA_PAGES_ARRAY)
232 addr = (unsigned long)page_address(pages[i]);
233 else
234 addr = start[i];
235
236 pte = lookup_address(addr, &level);
Shaohua Lid75586a2008-08-21 10:46:06 +0800237
238 /*
239 * Only flush present addresses:
240 */
241 if (pte && (pte_val(*pte) & _PAGE_PRESENT))
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700242 clflush_cache_range((void *)addr, PAGE_SIZE);
Shaohua Lid75586a2008-08-21 10:46:06 +0800243 }
244}
245
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100246/*
Arjan van de Vened724be2008-01-30 13:34:04 +0100247 * Certain areas of memory on x86 require very specific protection flags,
248 * for example the BIOS area or kernel text. Callers don't always get this
249 * right (again, ioremap() on BIOS memory is not uncommon) so this function
250 * checks and fixes these known static required protection bits.
251 */
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100252static inline pgprot_t static_protections(pgprot_t prot, unsigned long address,
253 unsigned long pfn)
Arjan van de Vened724be2008-01-30 13:34:04 +0100254{
255 pgprot_t forbidden = __pgprot(0);
256
Ingo Molnar687c4822008-01-30 13:34:04 +0100257 /*
Arjan van de Vened724be2008-01-30 13:34:04 +0100258 * The BIOS area between 640k and 1Mb needs to be executable for
259 * PCI BIOS based config access (CONFIG_PCI_GOBIOS) support.
Ingo Molnar687c4822008-01-30 13:34:04 +0100260 */
Matthieu Castet5bd5a452010-11-16 22:31:26 +0100261#ifdef CONFIG_PCI_BIOS
262 if (pcibios_enabled && within(pfn, BIOS_BEGIN >> PAGE_SHIFT, BIOS_END >> PAGE_SHIFT))
Arjan van de Vened724be2008-01-30 13:34:04 +0100263 pgprot_val(forbidden) |= _PAGE_NX;
Matthieu Castet5bd5a452010-11-16 22:31:26 +0100264#endif
Arjan van de Vened724be2008-01-30 13:34:04 +0100265
266 /*
267 * The kernel text needs to be executable for obvious reasons
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100268 * Does not cover __inittext since that is gone later on. On
269 * 64bit we do not enforce !NX on the low mapping
Arjan van de Vened724be2008-01-30 13:34:04 +0100270 */
271 if (within(address, (unsigned long)_text, (unsigned long)_etext))
272 pgprot_val(forbidden) |= _PAGE_NX;
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100273
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100274 /*
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100275 * The .rodata section needs to be read-only. Using the pfn
276 * catches all aliases.
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100277 */
Alexander Duyckfc8d7822012-11-16 13:57:13 -0800278 if (within(pfn, __pa_symbol(__start_rodata) >> PAGE_SHIFT,
279 __pa_symbol(__end_rodata) >> PAGE_SHIFT))
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100280 pgprot_val(forbidden) |= _PAGE_RW;
Arjan van de Vened724be2008-01-30 13:34:04 +0100281
Suresh Siddha55ca3cc2009-10-28 18:46:57 -0800282#if defined(CONFIG_X86_64) && defined(CONFIG_DEBUG_RODATA)
Suresh Siddha74e08172009-10-14 14:46:56 -0700283 /*
Suresh Siddha502f6602009-10-28 18:46:56 -0800284 * Once the kernel maps the text as RO (kernel_set_to_readonly is set),
285 * kernel text mappings for the large page aligned text, rodata sections
286 * will be always read-only. For the kernel identity mappings covering
287 * the holes caused by this alignment can be anything that user asks.
Suresh Siddha74e08172009-10-14 14:46:56 -0700288 *
289 * This will preserve the large page mappings for kernel text/data
290 * at no extra cost.
291 */
Suresh Siddha502f6602009-10-28 18:46:56 -0800292 if (kernel_set_to_readonly &&
293 within(address, (unsigned long)_text,
Suresh Siddha281ff332010-02-18 11:51:40 -0800294 (unsigned long)__end_rodata_hpage_align)) {
295 unsigned int level;
296
297 /*
298 * Don't enforce the !RW mapping for the kernel text mapping,
299 * if the current mapping is already using small page mapping.
300 * No need to work hard to preserve large page mappings in this
301 * case.
302 *
303 * This also fixes the Linux Xen paravirt guest boot failure
304 * (because of unexpected read-only mappings for kernel identity
305 * mappings). In this paravirt guest case, the kernel text
306 * mapping and the kernel identity mapping share the same
307 * page-table pages. Thus we can't really use different
308 * protections for the kernel text and identity mappings. Also,
309 * these shared mappings are made of small page mappings.
310 * Thus this don't enforce !RW mapping for small page kernel
311 * text mapping logic will help Linux Xen parvirt guest boot
Lucas De Marchi0d2eb442011-03-17 16:24:16 -0300312 * as well.
Suresh Siddha281ff332010-02-18 11:51:40 -0800313 */
314 if (lookup_address(address, &level) && (level != PG_LEVEL_4K))
315 pgprot_val(forbidden) |= _PAGE_RW;
316 }
Suresh Siddha74e08172009-10-14 14:46:56 -0700317#endif
318
Arjan van de Vened724be2008-01-30 13:34:04 +0100319 prot = __pgprot(pgprot_val(prot) & ~pgprot_val(forbidden));
Ingo Molnar687c4822008-01-30 13:34:04 +0100320
321 return prot;
322}
323
Matt Fleming426e34c2013-12-06 21:13:04 +0000324/*
325 * Lookup the page table entry for a virtual address in a specific pgd.
326 * Return a pointer to the entry and the level of the mapping.
327 */
328pte_t *lookup_address_in_pgd(pgd_t *pgd, unsigned long address,
329 unsigned int *level)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100330{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 pud_t *pud;
332 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100333
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100334 *level = PG_LEVEL_NONE;
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (pgd_none(*pgd))
337 return NULL;
Ingo Molnar9df84992008-02-04 16:48:09 +0100338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 pud = pud_offset(pgd, address);
340 if (pud_none(*pud))
341 return NULL;
Andi Kleenc2f71ee2008-02-04 16:48:09 +0100342
343 *level = PG_LEVEL_1G;
344 if (pud_large(*pud) || !pud_present(*pud))
345 return (pte_t *)pud;
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 pmd = pmd_offset(pud, address);
348 if (pmd_none(*pmd))
349 return NULL;
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100350
351 *level = PG_LEVEL_2M;
Thomas Gleixner9a14aef2008-02-04 16:48:07 +0100352 if (pmd_large(*pmd) || !pmd_present(*pmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return (pte_t *)pmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100355 *level = PG_LEVEL_4K;
Ingo Molnar9df84992008-02-04 16:48:09 +0100356
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100357 return pte_offset_kernel(pmd, address);
358}
Borislav Petkov0fd64c22013-10-31 17:25:00 +0100359
360/*
361 * Lookup the page table entry for a virtual address. Return a pointer
362 * to the entry and the level of the mapping.
363 *
364 * Note: We return pud and pmd either when the entry is marked large
365 * or when the present bit is not set. Otherwise we would return a
366 * pointer to a nonexisting mapping.
367 */
368pte_t *lookup_address(unsigned long address, unsigned int *level)
369{
Matt Fleming426e34c2013-12-06 21:13:04 +0000370 return lookup_address_in_pgd(pgd_offset_k(address), address, level);
Borislav Petkov0fd64c22013-10-31 17:25:00 +0100371}
Pekka Paalanen75bb8832008-05-12 21:20:56 +0200372EXPORT_SYMBOL_GPL(lookup_address);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100373
Borislav Petkov0fd64c22013-10-31 17:25:00 +0100374static pte_t *_lookup_address_cpa(struct cpa_data *cpa, unsigned long address,
375 unsigned int *level)
376{
377 if (cpa->pgd)
Matt Fleming426e34c2013-12-06 21:13:04 +0000378 return lookup_address_in_pgd(cpa->pgd + pgd_index(address),
Borislav Petkov0fd64c22013-10-31 17:25:00 +0100379 address, level);
380
381 return lookup_address(address, level);
382}
383
Ingo Molnar9df84992008-02-04 16:48:09 +0100384/*
Juergen Gross792230c2014-11-28 11:53:56 +0100385 * Lookup the PMD entry for a virtual address. Return a pointer to the entry
386 * or NULL if not present.
387 */
388pmd_t *lookup_pmd_address(unsigned long address)
389{
390 pgd_t *pgd;
391 pud_t *pud;
392
393 pgd = pgd_offset_k(address);
394 if (pgd_none(*pgd))
395 return NULL;
396
397 pud = pud_offset(pgd, address);
398 if (pud_none(*pud) || pud_large(*pud) || !pud_present(*pud))
399 return NULL;
400
401 return pmd_offset(pud, address);
402}
403
404/*
Dave Hansend7656532013-01-22 13:24:33 -0800405 * This is necessary because __pa() does not work on some
406 * kinds of memory, like vmalloc() or the alloc_remap()
407 * areas on 32-bit NUMA systems. The percpu areas can
408 * end up in this kind of memory, for instance.
409 *
410 * This could be optimized, but it is only intended to be
411 * used at inititalization time, and keeping it
412 * unoptimized should increase the testing coverage for
413 * the more obscure platforms.
414 */
415phys_addr_t slow_virt_to_phys(void *__virt_addr)
416{
417 unsigned long virt_addr = (unsigned long)__virt_addr;
418 phys_addr_t phys_addr;
419 unsigned long offset;
420 enum pg_level level;
Dave Hansend7656532013-01-22 13:24:33 -0800421 unsigned long pmask;
422 pte_t *pte;
423
424 pte = lookup_address(virt_addr, &level);
425 BUG_ON(!pte);
Dave Hansend7656532013-01-22 13:24:33 -0800426 pmask = page_level_mask(level);
427 offset = virt_addr & ~pmask;
Dexuan Cuid1cd1212014-10-29 03:53:37 -0700428 phys_addr = (phys_addr_t)pte_pfn(*pte) << PAGE_SHIFT;
Dave Hansend7656532013-01-22 13:24:33 -0800429 return (phys_addr | offset);
430}
431EXPORT_SYMBOL_GPL(slow_virt_to_phys);
432
433/*
Ingo Molnar9df84992008-02-04 16:48:09 +0100434 * Set the new pmd in all the pgds we know about:
435 */
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100436static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100437{
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100438 /* change init_mm */
439 set_pte_atomic(kpte, pte);
Ingo Molnar44af6c42008-01-30 13:34:03 +0100440#ifdef CONFIG_X86_32
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100441 if (!SHARED_KERNEL_PMD) {
Ingo Molnar44af6c42008-01-30 13:34:03 +0100442 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Jeremy Fitzhardingee3ed9102008-01-30 13:34:11 +0100444 list_for_each_entry(page, &pgd_list, lru) {
Ingo Molnar44af6c42008-01-30 13:34:03 +0100445 pgd_t *pgd;
446 pud_t *pud;
447 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100448
Ingo Molnar44af6c42008-01-30 13:34:03 +0100449 pgd = (pgd_t *)page_address(page) + pgd_index(address);
450 pud = pud_offset(pgd, address);
451 pmd = pmd_offset(pud, address);
452 set_pte_atomic((pte_t *)pmd, pte);
453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
Ingo Molnar44af6c42008-01-30 13:34:03 +0100455#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
Ingo Molnar9df84992008-02-04 16:48:09 +0100458static int
459try_preserve_large_page(pte_t *kpte, unsigned long address,
460 struct cpa_data *cpa)
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100461{
Andrea Arcangelia79e53d2011-02-16 15:45:22 -0800462 unsigned long nextpage_addr, numpages, pmask, psize, addr, pfn;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100463 pte_t new_pte, old_pte, *tmp;
matthieu castet64edc8e2010-11-16 22:30:27 +0100464 pgprot_t old_prot, new_prot, req_prot;
Thomas Gleixnerfac84932008-02-09 23:24:09 +0100465 int i, do_split = 1;
Dave Hansenf3c4fbb2013-01-22 13:24:32 -0800466 enum pg_level level;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100467
Andi Kleenc9caa022008-03-12 03:53:29 +0100468 if (cpa->force_split)
469 return 1;
470
Andrea Arcangelia79e53d2011-02-16 15:45:22 -0800471 spin_lock(&pgd_lock);
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100472 /*
473 * Check for races, another CPU might have split this page
474 * up already:
475 */
Borislav Petkov82f07122013-10-31 17:25:07 +0100476 tmp = _lookup_address_cpa(cpa, address, &level);
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100477 if (tmp != kpte)
478 goto out_unlock;
479
480 switch (level) {
481 case PG_LEVEL_2M:
Andi Kleenf07333f2008-02-04 16:48:09 +0100482#ifdef CONFIG_X86_64
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100483 case PG_LEVEL_1G:
Andi Kleenf07333f2008-02-04 16:48:09 +0100484#endif
Dave Hansenf3c4fbb2013-01-22 13:24:32 -0800485 psize = page_level_size(level);
486 pmask = page_level_mask(level);
487 break;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100488 default:
Ingo Molnarbeaff632008-02-04 16:48:09 +0100489 do_split = -EINVAL;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100490 goto out_unlock;
491 }
492
493 /*
494 * Calculate the number of pages, which fit into this large
495 * page starting at address:
496 */
497 nextpage_addr = (address + psize) & pmask;
498 numpages = (nextpage_addr - address) >> PAGE_SHIFT;
Rafael J. Wysocki9b5cf482008-03-03 01:17:37 +0100499 if (numpages < cpa->numpages)
500 cpa->numpages = numpages;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100501
502 /*
503 * We are safe now. Check whether the new pgprot is the same:
Juergen Grossf5b28312014-11-03 14:02:02 +0100504 * Convert protection attributes to 4k-format, as cpa->mask* are set
505 * up accordingly.
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100506 */
507 old_pte = *kpte;
Juergen Grossf5b28312014-11-03 14:02:02 +0100508 old_prot = req_prot = pgprot_large_2_4k(pte_pgprot(old_pte));
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100509
matthieu castet64edc8e2010-11-16 22:30:27 +0100510 pgprot_val(req_prot) &= ~pgprot_val(cpa->mask_clr);
511 pgprot_val(req_prot) |= pgprot_val(cpa->mask_set);
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100512
513 /*
Juergen Grossf5b28312014-11-03 14:02:02 +0100514 * req_prot is in format of 4k pages. It must be converted to large
515 * page format: the caching mode includes the PAT bit located at
516 * different bit positions in the two formats.
517 */
518 req_prot = pgprot_4k_2_large(req_prot);
519
520 /*
Andrea Arcangelia8aed3e2013-02-22 15:11:51 -0800521 * Set the PSE and GLOBAL flags only if the PRESENT flag is
522 * set otherwise pmd_present/pmd_huge will return true even on
523 * a non present pmd. The canon_pgprot will clear _PAGE_GLOBAL
524 * for the ancient hardware that doesn't support it.
525 */
Andrea Arcangelif76cfa32013-04-10 15:28:25 +0200526 if (pgprot_val(req_prot) & _PAGE_PRESENT)
527 pgprot_val(req_prot) |= _PAGE_PSE | _PAGE_GLOBAL;
Andrea Arcangelia8aed3e2013-02-22 15:11:51 -0800528 else
Andrea Arcangelif76cfa32013-04-10 15:28:25 +0200529 pgprot_val(req_prot) &= ~(_PAGE_PSE | _PAGE_GLOBAL);
Andrea Arcangelia8aed3e2013-02-22 15:11:51 -0800530
Andrea Arcangelif76cfa32013-04-10 15:28:25 +0200531 req_prot = canon_pgprot(req_prot);
Andrea Arcangelia8aed3e2013-02-22 15:11:51 -0800532
533 /*
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100534 * old_pte points to the large page base address. So we need
535 * to add the offset of the virtual address:
536 */
537 pfn = pte_pfn(old_pte) + ((address & (psize - 1)) >> PAGE_SHIFT);
538 cpa->pfn = pfn;
539
matthieu castet64edc8e2010-11-16 22:30:27 +0100540 new_prot = static_protections(req_prot, address, pfn);
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100541
542 /*
Thomas Gleixnerfac84932008-02-09 23:24:09 +0100543 * We need to check the full range, whether
544 * static_protection() requires a different pgprot for one of
545 * the pages in the range we try to preserve:
546 */
matthieu castet64edc8e2010-11-16 22:30:27 +0100547 addr = address & pmask;
548 pfn = pte_pfn(old_pte);
549 for (i = 0; i < (psize >> PAGE_SHIFT); i++, addr += PAGE_SIZE, pfn++) {
550 pgprot_t chk_prot = static_protections(req_prot, addr, pfn);
Thomas Gleixnerfac84932008-02-09 23:24:09 +0100551
552 if (pgprot_val(chk_prot) != pgprot_val(new_prot))
553 goto out_unlock;
554 }
555
556 /*
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100557 * If there are no changes, return. maxpages has been updated
558 * above:
559 */
560 if (pgprot_val(new_prot) == pgprot_val(old_prot)) {
Ingo Molnarbeaff632008-02-04 16:48:09 +0100561 do_split = 0;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100562 goto out_unlock;
563 }
564
565 /*
566 * We need to change the attributes. Check, whether we can
567 * change the large page in one go. We request a split, when
568 * the address is not aligned and the number of pages is
569 * smaller than the number of pages in the large page. Note
570 * that we limited the number of possible pages already to
571 * the number of pages in the large page.
572 */
matthieu castet64edc8e2010-11-16 22:30:27 +0100573 if (address == (address & pmask) && cpa->numpages == (psize >> PAGE_SHIFT)) {
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100574 /*
575 * The address is aligned and the number of pages
576 * covers the full page.
577 */
Andrea Arcangelia8aed3e2013-02-22 15:11:51 -0800578 new_pte = pfn_pte(pte_pfn(old_pte), new_prot);
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100579 __set_pmd_pte(kpte, address, new_pte);
Shaohua Lid75586a2008-08-21 10:46:06 +0800580 cpa->flags |= CPA_FLUSHTLB;
Ingo Molnarbeaff632008-02-04 16:48:09 +0100581 do_split = 0;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100582 }
583
584out_unlock:
Andrea Arcangelia79e53d2011-02-16 15:45:22 -0800585 spin_unlock(&pgd_lock);
Ingo Molnar9df84992008-02-04 16:48:09 +0100586
Ingo Molnarbeaff632008-02-04 16:48:09 +0100587 return do_split;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100588}
589
Borislav Petkov59528862013-03-21 18:16:57 +0100590static int
Borislav Petkov82f07122013-10-31 17:25:07 +0100591__split_large_page(struct cpa_data *cpa, pte_t *kpte, unsigned long address,
592 struct page *base)
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100593{
Borislav Petkov59528862013-03-21 18:16:57 +0100594 pte_t *pbase = (pte_t *)page_address(base);
Andrea Arcangelia79e53d2011-02-16 15:45:22 -0800595 unsigned long pfn, pfninc = 1;
Ingo Molnar86f03989d2008-01-30 13:34:09 +0100596 unsigned int i, level;
Wen Congyangae9aae92013-02-22 16:33:04 -0800597 pte_t *tmp;
Ingo Molnar9df84992008-02-04 16:48:09 +0100598 pgprot_t ref_prot;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100599
Andrea Arcangelia79e53d2011-02-16 15:45:22 -0800600 spin_lock(&pgd_lock);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100601 /*
602 * Check for races, another CPU might have split this page
603 * up for us already:
604 */
Borislav Petkov82f07122013-10-31 17:25:07 +0100605 tmp = _lookup_address_cpa(cpa, address, &level);
Wen Congyangae9aae92013-02-22 16:33:04 -0800606 if (tmp != kpte) {
607 spin_unlock(&pgd_lock);
608 return 1;
609 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100610
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -0700611 paravirt_alloc_pte(&init_mm, page_to_pfn(base));
Thomas Gleixner07cf89c2008-02-04 16:48:08 +0100612 ref_prot = pte_pgprot(pte_clrhuge(*kpte));
Juergen Grossf5b28312014-11-03 14:02:02 +0100613
614 /* promote PAT bit to correct position */
615 if (level == PG_LEVEL_2M)
616 ref_prot = pgprot_large_2_4k(ref_prot);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100617
Andi Kleenf07333f2008-02-04 16:48:09 +0100618#ifdef CONFIG_X86_64
619 if (level == PG_LEVEL_1G) {
620 pfninc = PMD_PAGE_SIZE >> PAGE_SHIFT;
Andrea Arcangelia8aed3e2013-02-22 15:11:51 -0800621 /*
622 * Set the PSE flags only if the PRESENT flag is set
623 * otherwise pmd_present/pmd_huge will return true
624 * even on a non present pmd.
625 */
626 if (pgprot_val(ref_prot) & _PAGE_PRESENT)
627 pgprot_val(ref_prot) |= _PAGE_PSE;
628 else
629 pgprot_val(ref_prot) &= ~_PAGE_PSE;
Andi Kleenf07333f2008-02-04 16:48:09 +0100630 }
631#endif
632
Thomas Gleixner63c1dcf2008-02-04 16:48:05 +0100633 /*
Andrea Arcangelia8aed3e2013-02-22 15:11:51 -0800634 * Set the GLOBAL flags only if the PRESENT flag is set
635 * otherwise pmd/pte_present will return true even on a non
636 * present pmd/pte. The canon_pgprot will clear _PAGE_GLOBAL
637 * for the ancient hardware that doesn't support it.
638 */
639 if (pgprot_val(ref_prot) & _PAGE_PRESENT)
640 pgprot_val(ref_prot) |= _PAGE_GLOBAL;
641 else
642 pgprot_val(ref_prot) &= ~_PAGE_GLOBAL;
643
644 /*
Thomas Gleixner63c1dcf2008-02-04 16:48:05 +0100645 * Get the target pfn from the original entry:
646 */
647 pfn = pte_pfn(*kpte);
Andi Kleenf07333f2008-02-04 16:48:09 +0100648 for (i = 0; i < PTRS_PER_PTE; i++, pfn += pfninc)
Andrea Arcangelia8aed3e2013-02-22 15:11:51 -0800649 set_pte(&pbase[i], pfn_pte(pfn, canon_pgprot(ref_prot)));
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100650
Yinghai Lu8eb57792012-11-16 19:38:49 -0800651 if (pfn_range_is_mapped(PFN_DOWN(__pa(address)),
652 PFN_DOWN(__pa(address)) + 1))
Yinghai Luf361a452008-07-10 20:38:26 -0700653 split_page_count(level);
654
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100655 /*
Ingo Molnar07a66d72009-02-20 08:04:13 +0100656 * Install the new, split up pagetable.
Huang, Ying4c881ca2008-01-30 13:34:04 +0100657 *
Ingo Molnar07a66d72009-02-20 08:04:13 +0100658 * We use the standard kernel pagetable protections for the new
659 * pagetable protections, the actual ptes set above control the
660 * primary protection behavior:
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100661 */
Ingo Molnar07a66d72009-02-20 08:04:13 +0100662 __set_pmd_pte(kpte, address, mk_pte(base, __pgprot(_KERNPG_TABLE)));
Ingo Molnar211b3d02009-03-10 22:31:03 +0100663
664 /*
665 * Intel Atom errata AAH41 workaround.
666 *
667 * The real fix should be in hw or in a microcode update, but
668 * we also probabilistically try to reduce the window of having
669 * a large TLB mixed with 4K TLBs while instruction fetches are
670 * going on.
671 */
672 __flush_tlb_all();
Andrea Arcangelia79e53d2011-02-16 15:45:22 -0800673 spin_unlock(&pgd_lock);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100674
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100675 return 0;
676}
677
Borislav Petkov82f07122013-10-31 17:25:07 +0100678static int split_large_page(struct cpa_data *cpa, pte_t *kpte,
679 unsigned long address)
Wen Congyangae9aae92013-02-22 16:33:04 -0800680{
Wen Congyangae9aae92013-02-22 16:33:04 -0800681 struct page *base;
682
683 if (!debug_pagealloc)
684 spin_unlock(&cpa_lock);
685 base = alloc_pages(GFP_KERNEL | __GFP_NOTRACK, 0);
686 if (!debug_pagealloc)
687 spin_lock(&cpa_lock);
688 if (!base)
689 return -ENOMEM;
690
Borislav Petkov82f07122013-10-31 17:25:07 +0100691 if (__split_large_page(cpa, kpte, address, base))
Wen Congyangae9aae92013-02-22 16:33:04 -0800692 __free_page(base);
693
694 return 0;
695}
696
Borislav Petkov52a628f2013-10-31 17:25:06 +0100697static bool try_to_free_pte_page(pte_t *pte)
698{
699 int i;
700
701 for (i = 0; i < PTRS_PER_PTE; i++)
702 if (!pte_none(pte[i]))
703 return false;
704
705 free_page((unsigned long)pte);
706 return true;
707}
708
709static bool try_to_free_pmd_page(pmd_t *pmd)
710{
711 int i;
712
713 for (i = 0; i < PTRS_PER_PMD; i++)
714 if (!pmd_none(pmd[i]))
715 return false;
716
717 free_page((unsigned long)pmd);
718 return true;
719}
720
Borislav Petkov42a54772014-01-18 12:48:16 +0100721static bool try_to_free_pud_page(pud_t *pud)
722{
723 int i;
724
725 for (i = 0; i < PTRS_PER_PUD; i++)
726 if (!pud_none(pud[i]))
727 return false;
728
729 free_page((unsigned long)pud);
730 return true;
731}
732
Borislav Petkov52a628f2013-10-31 17:25:06 +0100733static bool unmap_pte_range(pmd_t *pmd, unsigned long start, unsigned long end)
734{
735 pte_t *pte = pte_offset_kernel(pmd, start);
736
737 while (start < end) {
738 set_pte(pte, __pte(0));
739
740 start += PAGE_SIZE;
741 pte++;
742 }
743
744 if (try_to_free_pte_page((pte_t *)pmd_page_vaddr(*pmd))) {
745 pmd_clear(pmd);
746 return true;
747 }
748 return false;
749}
750
751static void __unmap_pmd_range(pud_t *pud, pmd_t *pmd,
752 unsigned long start, unsigned long end)
753{
754 if (unmap_pte_range(pmd, start, end))
755 if (try_to_free_pmd_page((pmd_t *)pud_page_vaddr(*pud)))
756 pud_clear(pud);
757}
758
759static void unmap_pmd_range(pud_t *pud, unsigned long start, unsigned long end)
760{
761 pmd_t *pmd = pmd_offset(pud, start);
762
763 /*
764 * Not on a 2MB page boundary?
765 */
766 if (start & (PMD_SIZE - 1)) {
767 unsigned long next_page = (start + PMD_SIZE) & PMD_MASK;
768 unsigned long pre_end = min_t(unsigned long, end, next_page);
769
770 __unmap_pmd_range(pud, pmd, start, pre_end);
771
772 start = pre_end;
773 pmd++;
774 }
775
776 /*
777 * Try to unmap in 2M chunks.
778 */
779 while (end - start >= PMD_SIZE) {
780 if (pmd_large(*pmd))
781 pmd_clear(pmd);
782 else
783 __unmap_pmd_range(pud, pmd, start, start + PMD_SIZE);
784
785 start += PMD_SIZE;
786 pmd++;
787 }
788
789 /*
790 * 4K leftovers?
791 */
792 if (start < end)
793 return __unmap_pmd_range(pud, pmd, start, end);
794
795 /*
796 * Try again to free the PMD page if haven't succeeded above.
797 */
798 if (!pud_none(*pud))
799 if (try_to_free_pmd_page((pmd_t *)pud_page_vaddr(*pud)))
800 pud_clear(pud);
801}
Borislav Petkov0bb8aee2013-10-31 17:25:05 +0100802
803static void unmap_pud_range(pgd_t *pgd, unsigned long start, unsigned long end)
804{
805 pud_t *pud = pud_offset(pgd, start);
806
807 /*
808 * Not on a GB page boundary?
809 */
810 if (start & (PUD_SIZE - 1)) {
811 unsigned long next_page = (start + PUD_SIZE) & PUD_MASK;
812 unsigned long pre_end = min_t(unsigned long, end, next_page);
813
814 unmap_pmd_range(pud, start, pre_end);
815
816 start = pre_end;
817 pud++;
818 }
819
820 /*
821 * Try to unmap in 1G chunks?
822 */
823 while (end - start >= PUD_SIZE) {
824
825 if (pud_large(*pud))
826 pud_clear(pud);
827 else
828 unmap_pmd_range(pud, start, start + PUD_SIZE);
829
830 start += PUD_SIZE;
831 pud++;
832 }
833
834 /*
835 * 2M leftovers?
836 */
837 if (start < end)
838 unmap_pmd_range(pud, start, end);
839
840 /*
841 * No need to try to free the PUD page because we'll free it in
842 * populate_pgd's error path
843 */
844}
845
Borislav Petkov42a54772014-01-18 12:48:16 +0100846static void unmap_pgd_range(pgd_t *root, unsigned long addr, unsigned long end)
847{
848 pgd_t *pgd_entry = root + pgd_index(addr);
849
850 unmap_pud_range(pgd_entry, addr, end);
851
852 if (try_to_free_pud_page((pud_t *)pgd_page_vaddr(*pgd_entry)))
853 pgd_clear(pgd_entry);
854}
855
Borislav Petkovf900a4b2013-10-31 17:25:03 +0100856static int alloc_pte_page(pmd_t *pmd)
857{
858 pte_t *pte = (pte_t *)get_zeroed_page(GFP_KERNEL | __GFP_NOTRACK);
859 if (!pte)
860 return -1;
861
862 set_pmd(pmd, __pmd(__pa(pte) | _KERNPG_TABLE));
863 return 0;
864}
865
Borislav Petkov4b235382013-10-31 17:25:02 +0100866static int alloc_pmd_page(pud_t *pud)
867{
868 pmd_t *pmd = (pmd_t *)get_zeroed_page(GFP_KERNEL | __GFP_NOTRACK);
869 if (!pmd)
870 return -1;
871
872 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE));
873 return 0;
874}
875
Borislav Petkovc6b6f362013-10-31 17:25:04 +0100876static void populate_pte(struct cpa_data *cpa,
877 unsigned long start, unsigned long end,
878 unsigned num_pages, pmd_t *pmd, pgprot_t pgprot)
879{
880 pte_t *pte;
881
882 pte = pte_offset_kernel(pmd, start);
883
884 while (num_pages-- && start < end) {
885
886 /* deal with the NX bit */
887 if (!(pgprot_val(pgprot) & _PAGE_NX))
888 cpa->pfn &= ~_PAGE_NX;
889
890 set_pte(pte, pfn_pte(cpa->pfn >> PAGE_SHIFT, pgprot));
891
892 start += PAGE_SIZE;
893 cpa->pfn += PAGE_SIZE;
894 pte++;
895 }
896}
Borislav Petkovf900a4b2013-10-31 17:25:03 +0100897
898static int populate_pmd(struct cpa_data *cpa,
899 unsigned long start, unsigned long end,
900 unsigned num_pages, pud_t *pud, pgprot_t pgprot)
901{
902 unsigned int cur_pages = 0;
903 pmd_t *pmd;
Juergen Grossf5b28312014-11-03 14:02:02 +0100904 pgprot_t pmd_pgprot;
Borislav Petkovf900a4b2013-10-31 17:25:03 +0100905
906 /*
907 * Not on a 2M boundary?
908 */
909 if (start & (PMD_SIZE - 1)) {
910 unsigned long pre_end = start + (num_pages << PAGE_SHIFT);
911 unsigned long next_page = (start + PMD_SIZE) & PMD_MASK;
912
913 pre_end = min_t(unsigned long, pre_end, next_page);
914 cur_pages = (pre_end - start) >> PAGE_SHIFT;
915 cur_pages = min_t(unsigned int, num_pages, cur_pages);
916
917 /*
918 * Need a PTE page?
919 */
920 pmd = pmd_offset(pud, start);
921 if (pmd_none(*pmd))
922 if (alloc_pte_page(pmd))
923 return -1;
924
925 populate_pte(cpa, start, pre_end, cur_pages, pmd, pgprot);
926
927 start = pre_end;
928 }
929
930 /*
931 * We mapped them all?
932 */
933 if (num_pages == cur_pages)
934 return cur_pages;
935
Juergen Grossf5b28312014-11-03 14:02:02 +0100936 pmd_pgprot = pgprot_4k_2_large(pgprot);
937
Borislav Petkovf900a4b2013-10-31 17:25:03 +0100938 while (end - start >= PMD_SIZE) {
939
940 /*
941 * We cannot use a 1G page so allocate a PMD page if needed.
942 */
943 if (pud_none(*pud))
944 if (alloc_pmd_page(pud))
945 return -1;
946
947 pmd = pmd_offset(pud, start);
948
Juergen Grossf5b28312014-11-03 14:02:02 +0100949 set_pmd(pmd, __pmd(cpa->pfn | _PAGE_PSE |
950 massage_pgprot(pmd_pgprot)));
Borislav Petkovf900a4b2013-10-31 17:25:03 +0100951
952 start += PMD_SIZE;
953 cpa->pfn += PMD_SIZE;
954 cur_pages += PMD_SIZE >> PAGE_SHIFT;
955 }
956
957 /*
958 * Map trailing 4K pages.
959 */
960 if (start < end) {
961 pmd = pmd_offset(pud, start);
962 if (pmd_none(*pmd))
963 if (alloc_pte_page(pmd))
964 return -1;
965
966 populate_pte(cpa, start, end, num_pages - cur_pages,
967 pmd, pgprot);
968 }
969 return num_pages;
970}
Borislav Petkov4b235382013-10-31 17:25:02 +0100971
972static int populate_pud(struct cpa_data *cpa, unsigned long start, pgd_t *pgd,
973 pgprot_t pgprot)
974{
975 pud_t *pud;
976 unsigned long end;
977 int cur_pages = 0;
Juergen Grossf5b28312014-11-03 14:02:02 +0100978 pgprot_t pud_pgprot;
Borislav Petkov4b235382013-10-31 17:25:02 +0100979
980 end = start + (cpa->numpages << PAGE_SHIFT);
981
982 /*
983 * Not on a Gb page boundary? => map everything up to it with
984 * smaller pages.
985 */
986 if (start & (PUD_SIZE - 1)) {
987 unsigned long pre_end;
988 unsigned long next_page = (start + PUD_SIZE) & PUD_MASK;
989
990 pre_end = min_t(unsigned long, end, next_page);
991 cur_pages = (pre_end - start) >> PAGE_SHIFT;
992 cur_pages = min_t(int, (int)cpa->numpages, cur_pages);
993
994 pud = pud_offset(pgd, start);
995
996 /*
997 * Need a PMD page?
998 */
999 if (pud_none(*pud))
1000 if (alloc_pmd_page(pud))
1001 return -1;
1002
1003 cur_pages = populate_pmd(cpa, start, pre_end, cur_pages,
1004 pud, pgprot);
1005 if (cur_pages < 0)
1006 return cur_pages;
1007
1008 start = pre_end;
1009 }
1010
1011 /* We mapped them all? */
1012 if (cpa->numpages == cur_pages)
1013 return cur_pages;
1014
1015 pud = pud_offset(pgd, start);
Juergen Grossf5b28312014-11-03 14:02:02 +01001016 pud_pgprot = pgprot_4k_2_large(pgprot);
Borislav Petkov4b235382013-10-31 17:25:02 +01001017
1018 /*
1019 * Map everything starting from the Gb boundary, possibly with 1G pages
1020 */
1021 while (end - start >= PUD_SIZE) {
Juergen Grossf5b28312014-11-03 14:02:02 +01001022 set_pud(pud, __pud(cpa->pfn | _PAGE_PSE |
1023 massage_pgprot(pud_pgprot)));
Borislav Petkov4b235382013-10-31 17:25:02 +01001024
1025 start += PUD_SIZE;
1026 cpa->pfn += PUD_SIZE;
1027 cur_pages += PUD_SIZE >> PAGE_SHIFT;
1028 pud++;
1029 }
1030
1031 /* Map trailing leftover */
1032 if (start < end) {
1033 int tmp;
1034
1035 pud = pud_offset(pgd, start);
1036 if (pud_none(*pud))
1037 if (alloc_pmd_page(pud))
1038 return -1;
1039
1040 tmp = populate_pmd(cpa, start, end, cpa->numpages - cur_pages,
1041 pud, pgprot);
1042 if (tmp < 0)
1043 return cur_pages;
1044
1045 cur_pages += tmp;
1046 }
1047 return cur_pages;
1048}
Borislav Petkovf3f72962013-10-31 17:25:01 +01001049
1050/*
1051 * Restrictions for kernel page table do not necessarily apply when mapping in
1052 * an alternate PGD.
1053 */
1054static int populate_pgd(struct cpa_data *cpa, unsigned long addr)
1055{
1056 pgprot_t pgprot = __pgprot(_KERNPG_TABLE);
Borislav Petkovf3f72962013-10-31 17:25:01 +01001057 pud_t *pud = NULL; /* shut up gcc */
Borislav Petkov42a54772014-01-18 12:48:16 +01001058 pgd_t *pgd_entry;
Borislav Petkovf3f72962013-10-31 17:25:01 +01001059 int ret;
1060
1061 pgd_entry = cpa->pgd + pgd_index(addr);
1062
1063 /*
1064 * Allocate a PUD page and hand it down for mapping.
1065 */
1066 if (pgd_none(*pgd_entry)) {
1067 pud = (pud_t *)get_zeroed_page(GFP_KERNEL | __GFP_NOTRACK);
1068 if (!pud)
1069 return -1;
1070
1071 set_pgd(pgd_entry, __pgd(__pa(pud) | _KERNPG_TABLE));
Borislav Petkovf3f72962013-10-31 17:25:01 +01001072 }
1073
1074 pgprot_val(pgprot) &= ~pgprot_val(cpa->mask_clr);
1075 pgprot_val(pgprot) |= pgprot_val(cpa->mask_set);
1076
1077 ret = populate_pud(cpa, addr, pgd_entry, pgprot);
Borislav Petkov0bb8aee2013-10-31 17:25:05 +01001078 if (ret < 0) {
Borislav Petkov42a54772014-01-18 12:48:16 +01001079 unmap_pgd_range(cpa->pgd, addr,
Borislav Petkov0bb8aee2013-10-31 17:25:05 +01001080 addr + (cpa->numpages << PAGE_SHIFT));
Borislav Petkov0bb8aee2013-10-31 17:25:05 +01001081 return ret;
1082 }
Borislav Petkov42a54772014-01-18 12:48:16 +01001083
Borislav Petkovf3f72962013-10-31 17:25:01 +01001084 cpa->numpages = ret;
1085 return 0;
1086}
1087
Suresh Siddhaa1e46212009-01-20 14:20:21 -08001088static int __cpa_process_fault(struct cpa_data *cpa, unsigned long vaddr,
1089 int primary)
1090{
Borislav Petkov82f07122013-10-31 17:25:07 +01001091 if (cpa->pgd)
1092 return populate_pgd(cpa, vaddr);
1093
Suresh Siddhaa1e46212009-01-20 14:20:21 -08001094 /*
1095 * Ignore all non primary paths.
1096 */
1097 if (!primary)
1098 return 0;
1099
1100 /*
1101 * Ignore the NULL PTE for kernel identity mapping, as it is expected
1102 * to have holes.
1103 * Also set numpages to '1' indicating that we processed cpa req for
1104 * one virtual address page and its pfn. TBD: numpages can be set based
1105 * on the initial value and the level returned by lookup_address().
1106 */
1107 if (within(vaddr, PAGE_OFFSET,
1108 PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT))) {
1109 cpa->numpages = 1;
1110 cpa->pfn = __pa(vaddr) >> PAGE_SHIFT;
1111 return 0;
1112 } else {
1113 WARN(1, KERN_WARNING "CPA: called for zero pte. "
1114 "vaddr = %lx cpa->vaddr = %lx\n", vaddr,
1115 *cpa->vaddr);
1116
1117 return -EFAULT;
1118 }
1119}
1120
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +01001121static int __change_page_attr(struct cpa_data *cpa, int primary)
Ingo Molnar9f4c8152008-01-30 13:33:41 +01001122{
Shaohua Lid75586a2008-08-21 10:46:06 +08001123 unsigned long address;
Harvey Harrisonda7bfc52008-02-09 23:24:08 +01001124 int do_split, err;
1125 unsigned int level;
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +01001126 pte_t *kpte, old_pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Thomas Hellstrom8523acf2009-08-03 09:25:45 +02001128 if (cpa->flags & CPA_PAGES_ARRAY) {
1129 struct page *page = cpa->pages[cpa->curpage];
1130 if (unlikely(PageHighMem(page)))
1131 return 0;
1132 address = (unsigned long)page_address(page);
1133 } else if (cpa->flags & CPA_ARRAY)
Shaohua Lid75586a2008-08-21 10:46:06 +08001134 address = cpa->vaddr[cpa->curpage];
1135 else
1136 address = *cpa->vaddr;
Ingo Molnar97f99fe2008-01-30 13:33:55 +01001137repeat:
Borislav Petkov82f07122013-10-31 17:25:07 +01001138 kpte = _lookup_address_cpa(cpa, address, &level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (!kpte)
Suresh Siddhaa1e46212009-01-20 14:20:21 -08001140 return __cpa_process_fault(cpa, address, primary);
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +01001141
1142 old_pte = *kpte;
Suresh Siddhaa1e46212009-01-20 14:20:21 -08001143 if (!pte_val(old_pte))
1144 return __cpa_process_fault(cpa, address, primary);
Ingo Molnar9f4c8152008-01-30 13:33:41 +01001145
Thomas Gleixner30551bb2008-01-30 13:34:04 +01001146 if (level == PG_LEVEL_4K) {
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +01001147 pte_t new_pte;
Arjan van de Ven626c2c92008-02-04 16:48:05 +01001148 pgprot_t new_prot = pte_pgprot(old_pte);
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +01001149 unsigned long pfn = pte_pfn(old_pte);
Thomas Gleixnera72a08a2008-01-30 13:34:07 +01001150
Thomas Gleixner72e458d2008-02-04 16:48:07 +01001151 pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
1152 pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
Ingo Molnar86f03989d2008-01-30 13:34:09 +01001153
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +01001154 new_prot = static_protections(new_prot, address, pfn);
Ingo Molnar86f03989d2008-01-30 13:34:09 +01001155
Arjan van de Ven626c2c92008-02-04 16:48:05 +01001156 /*
Andrea Arcangelia8aed3e2013-02-22 15:11:51 -08001157 * Set the GLOBAL flags only if the PRESENT flag is
1158 * set otherwise pte_present will return true even on
1159 * a non present pte. The canon_pgprot will clear
1160 * _PAGE_GLOBAL for the ancient hardware that doesn't
1161 * support it.
1162 */
1163 if (pgprot_val(new_prot) & _PAGE_PRESENT)
1164 pgprot_val(new_prot) |= _PAGE_GLOBAL;
1165 else
1166 pgprot_val(new_prot) &= ~_PAGE_GLOBAL;
1167
1168 /*
Arjan van de Ven626c2c92008-02-04 16:48:05 +01001169 * We need to keep the pfn from the existing PTE,
1170 * after all we're only going to change it's attributes
1171 * not the memory it points to
1172 */
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +01001173 new_pte = pfn_pte(pfn, canon_pgprot(new_prot));
1174 cpa->pfn = pfn;
Thomas Gleixnerf4ae5da2008-02-04 16:48:07 +01001175 /*
1176 * Do we really change anything ?
1177 */
1178 if (pte_val(old_pte) != pte_val(new_pte)) {
1179 set_pte_atomic(kpte, new_pte);
Shaohua Lid75586a2008-08-21 10:46:06 +08001180 cpa->flags |= CPA_FLUSHTLB;
Thomas Gleixnerf4ae5da2008-02-04 16:48:07 +01001181 }
Rafael J. Wysocki9b5cf482008-03-03 01:17:37 +01001182 cpa->numpages = 1;
Thomas Gleixner65e074d2008-02-04 16:48:07 +01001183 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 }
Thomas Gleixner65e074d2008-02-04 16:48:07 +01001185
1186 /*
1187 * Check, whether we can keep the large page intact
1188 * and just change the pte:
1189 */
Ingo Molnarbeaff632008-02-04 16:48:09 +01001190 do_split = try_preserve_large_page(kpte, address, cpa);
Thomas Gleixner65e074d2008-02-04 16:48:07 +01001191 /*
1192 * When the range fits into the existing large page,
Rafael J. Wysocki9b5cf482008-03-03 01:17:37 +01001193 * return. cp->numpages and cpa->tlbflush have been updated in
Thomas Gleixner65e074d2008-02-04 16:48:07 +01001194 * try_large_page:
1195 */
Ingo Molnar87f7f8f2008-02-04 16:48:10 +01001196 if (do_split <= 0)
1197 return do_split;
Thomas Gleixner65e074d2008-02-04 16:48:07 +01001198
1199 /*
1200 * We have to split the large page:
1201 */
Borislav Petkov82f07122013-10-31 17:25:07 +01001202 err = split_large_page(cpa, kpte, address);
Ingo Molnar87f7f8f2008-02-04 16:48:10 +01001203 if (!err) {
Suresh Siddhaad5ca552008-09-23 14:00:42 -07001204 /*
1205 * Do a global flush tlb after splitting the large page
1206 * and before we do the actual change page attribute in the PTE.
1207 *
1208 * With out this, we violate the TLB application note, that says
1209 * "The TLBs may contain both ordinary and large-page
1210 * translations for a 4-KByte range of linear addresses. This
1211 * may occur if software modifies the paging structures so that
1212 * the page size used for the address range changes. If the two
1213 * translations differ with respect to page frame or attributes
1214 * (e.g., permissions), processor behavior is undefined and may
1215 * be implementation-specific."
1216 *
1217 * We do this global tlb flush inside the cpa_lock, so that we
1218 * don't allow any other cpu, with stale tlb entries change the
1219 * page attribute in parallel, that also falls into the
1220 * just split large page entry.
1221 */
1222 flush_tlb_all();
Ingo Molnar87f7f8f2008-02-04 16:48:10 +01001223 goto repeat;
1224 }
Ingo Molnarbeaff632008-02-04 16:48:09 +01001225
Ingo Molnar87f7f8f2008-02-04 16:48:10 +01001226 return err;
Ingo Molnar9f4c8152008-01-30 13:33:41 +01001227}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +01001229static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias);
1230
1231static int cpa_process_alias(struct cpa_data *cpa)
Ingo Molnar44af6c42008-01-30 13:34:03 +01001232{
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +01001233 struct cpa_data alias_cpa;
Tejun Heo992f4c12009-06-22 11:56:24 +09001234 unsigned long laddr = (unsigned long)__va(cpa->pfn << PAGE_SHIFT);
Tejun Heoe933a732009-08-14 15:00:53 +09001235 unsigned long vaddr;
Tejun Heo992f4c12009-06-22 11:56:24 +09001236 int ret;
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +01001237
Yinghai Lu8eb57792012-11-16 19:38:49 -08001238 if (!pfn_range_is_mapped(cpa->pfn, cpa->pfn + 1))
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +01001239 return 0;
1240
Thomas Gleixnerf34b4392008-02-15 22:17:57 +01001241 /*
1242 * No need to redo, when the primary call touched the direct
1243 * mapping already:
1244 */
Thomas Hellstrom8523acf2009-08-03 09:25:45 +02001245 if (cpa->flags & CPA_PAGES_ARRAY) {
1246 struct page *page = cpa->pages[cpa->curpage];
1247 if (unlikely(PageHighMem(page)))
1248 return 0;
1249 vaddr = (unsigned long)page_address(page);
1250 } else if (cpa->flags & CPA_ARRAY)
Shaohua Lid75586a2008-08-21 10:46:06 +08001251 vaddr = cpa->vaddr[cpa->curpage];
1252 else
1253 vaddr = *cpa->vaddr;
1254
1255 if (!(within(vaddr, PAGE_OFFSET,
Suresh Siddhaa1e46212009-01-20 14:20:21 -08001256 PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT)))) {
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +01001257
Thomas Gleixnerf34b4392008-02-15 22:17:57 +01001258 alias_cpa = *cpa;
Tejun Heo992f4c12009-06-22 11:56:24 +09001259 alias_cpa.vaddr = &laddr;
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -07001260 alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
Shaohua Lid75586a2008-08-21 10:46:06 +08001261
Thomas Gleixnerf34b4392008-02-15 22:17:57 +01001262 ret = __change_page_attr_set_clr(&alias_cpa, 0);
Tejun Heo992f4c12009-06-22 11:56:24 +09001263 if (ret)
1264 return ret;
Thomas Gleixnerf34b4392008-02-15 22:17:57 +01001265 }
Ingo Molnar44af6c42008-01-30 13:34:03 +01001266
Arjan van de Ven488fd992008-01-30 13:34:07 +01001267#ifdef CONFIG_X86_64
Thomas Gleixner08797502008-01-30 13:34:09 +01001268 /*
Tejun Heo992f4c12009-06-22 11:56:24 +09001269 * If the primary call didn't touch the high mapping already
1270 * and the physical address is inside the kernel map, we need
Thomas Gleixner08797502008-01-30 13:34:09 +01001271 * to touch the high mapped kernel as well:
1272 */
Tejun Heo992f4c12009-06-22 11:56:24 +09001273 if (!within(vaddr, (unsigned long)_text, _brk_end) &&
1274 within(cpa->pfn, highmap_start_pfn(), highmap_end_pfn())) {
1275 unsigned long temp_cpa_vaddr = (cpa->pfn << PAGE_SHIFT) +
1276 __START_KERNEL_map - phys_base;
1277 alias_cpa = *cpa;
1278 alias_cpa.vaddr = &temp_cpa_vaddr;
1279 alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
Thomas Gleixner08797502008-01-30 13:34:09 +01001280
Tejun Heo992f4c12009-06-22 11:56:24 +09001281 /*
1282 * The high mapping range is imprecise, so ignore the
1283 * return value.
1284 */
1285 __change_page_attr_set_clr(&alias_cpa, 0);
1286 }
Thomas Gleixner08797502008-01-30 13:34:09 +01001287#endif
Tejun Heo992f4c12009-06-22 11:56:24 +09001288
1289 return 0;
Ingo Molnar44af6c42008-01-30 13:34:03 +01001290}
1291
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +01001292static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias)
Thomas Gleixnerff314522008-01-30 13:34:08 +01001293{
Thomas Gleixner65e074d2008-02-04 16:48:07 +01001294 int ret, numpages = cpa->numpages;
Thomas Gleixnerff314522008-01-30 13:34:08 +01001295
Thomas Gleixner65e074d2008-02-04 16:48:07 +01001296 while (numpages) {
1297 /*
1298 * Store the remaining nr of pages for the large page
1299 * preservation check.
1300 */
Rafael J. Wysocki9b5cf482008-03-03 01:17:37 +01001301 cpa->numpages = numpages;
Shaohua Lid75586a2008-08-21 10:46:06 +08001302 /* for array changes, we can't use large page */
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -07001303 if (cpa->flags & (CPA_ARRAY | CPA_PAGES_ARRAY))
Shaohua Lid75586a2008-08-21 10:46:06 +08001304 cpa->numpages = 1;
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +01001305
Suresh Siddhaad5ca552008-09-23 14:00:42 -07001306 if (!debug_pagealloc)
1307 spin_lock(&cpa_lock);
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +01001308 ret = __change_page_attr(cpa, checkalias);
Suresh Siddhaad5ca552008-09-23 14:00:42 -07001309 if (!debug_pagealloc)
1310 spin_unlock(&cpa_lock);
Thomas Gleixnerff314522008-01-30 13:34:08 +01001311 if (ret)
1312 return ret;
Thomas Gleixnerff314522008-01-30 13:34:08 +01001313
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +01001314 if (checkalias) {
1315 ret = cpa_process_alias(cpa);
1316 if (ret)
1317 return ret;
1318 }
1319
Thomas Gleixner65e074d2008-02-04 16:48:07 +01001320 /*
1321 * Adjust the number of pages with the result of the
1322 * CPA operation. Either a large page has been
1323 * preserved or a single page update happened.
1324 */
Rafael J. Wysocki9b5cf482008-03-03 01:17:37 +01001325 BUG_ON(cpa->numpages > numpages);
1326 numpages -= cpa->numpages;
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -07001327 if (cpa->flags & (CPA_PAGES_ARRAY | CPA_ARRAY))
Shaohua Lid75586a2008-08-21 10:46:06 +08001328 cpa->curpage++;
1329 else
1330 *cpa->vaddr += cpa->numpages * PAGE_SIZE;
1331
Thomas Gleixner65e074d2008-02-04 16:48:07 +01001332 }
Thomas Gleixnerff314522008-01-30 13:34:08 +01001333 return 0;
1334}
1335
Shaohua Lid75586a2008-08-21 10:46:06 +08001336static int change_page_attr_set_clr(unsigned long *addr, int numpages,
Andi Kleenc9caa022008-03-12 03:53:29 +01001337 pgprot_t mask_set, pgprot_t mask_clr,
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -07001338 int force_split, int in_flag,
1339 struct page **pages)
Thomas Gleixnerff314522008-01-30 13:34:08 +01001340{
Thomas Gleixner72e458d2008-02-04 16:48:07 +01001341 struct cpa_data cpa;
Ingo Molnarcacf8902008-08-21 13:46:33 +02001342 int ret, cache, checkalias;
Jack Steinerfa526d02009-09-03 12:56:02 -05001343 unsigned long baddr = 0;
Thomas Gleixner331e4062008-02-04 16:48:06 +01001344
Borislav Petkov82f07122013-10-31 17:25:07 +01001345 memset(&cpa, 0, sizeof(cpa));
1346
Thomas Gleixner331e4062008-02-04 16:48:06 +01001347 /*
1348 * Check, if we are requested to change a not supported
1349 * feature:
1350 */
1351 mask_set = canon_pgprot(mask_set);
1352 mask_clr = canon_pgprot(mask_clr);
Andi Kleenc9caa022008-03-12 03:53:29 +01001353 if (!pgprot_val(mask_set) && !pgprot_val(mask_clr) && !force_split)
Thomas Gleixner331e4062008-02-04 16:48:06 +01001354 return 0;
1355
Thomas Gleixner69b14152008-02-13 11:04:50 +01001356 /* Ensure we are PAGE_SIZE aligned */
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -07001357 if (in_flag & CPA_ARRAY) {
Shaohua Lid75586a2008-08-21 10:46:06 +08001358 int i;
1359 for (i = 0; i < numpages; i++) {
1360 if (addr[i] & ~PAGE_MASK) {
1361 addr[i] &= PAGE_MASK;
1362 WARN_ON_ONCE(1);
1363 }
1364 }
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -07001365 } else if (!(in_flag & CPA_PAGES_ARRAY)) {
1366 /*
1367 * in_flag of CPA_PAGES_ARRAY implies it is aligned.
1368 * No need to cehck in that case
1369 */
1370 if (*addr & ~PAGE_MASK) {
1371 *addr &= PAGE_MASK;
1372 /*
1373 * People should not be passing in unaligned addresses:
1374 */
1375 WARN_ON_ONCE(1);
1376 }
Jack Steinerfa526d02009-09-03 12:56:02 -05001377 /*
1378 * Save address for cache flush. *addr is modified in the call
1379 * to __change_page_attr_set_clr() below.
1380 */
1381 baddr = *addr;
Thomas Gleixner69b14152008-02-13 11:04:50 +01001382 }
1383
Nick Piggin5843d9a2008-08-01 03:15:21 +02001384 /* Must avoid aliasing mappings in the highmem code */
1385 kmap_flush_unused();
1386
Nick Piggindb64fe02008-10-18 20:27:03 -07001387 vm_unmap_aliases();
1388
Thomas Gleixner72e458d2008-02-04 16:48:07 +01001389 cpa.vaddr = addr;
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -07001390 cpa.pages = pages;
Thomas Gleixner72e458d2008-02-04 16:48:07 +01001391 cpa.numpages = numpages;
1392 cpa.mask_set = mask_set;
1393 cpa.mask_clr = mask_clr;
Shaohua Lid75586a2008-08-21 10:46:06 +08001394 cpa.flags = 0;
1395 cpa.curpage = 0;
Andi Kleenc9caa022008-03-12 03:53:29 +01001396 cpa.force_split = force_split;
Thomas Gleixner72e458d2008-02-04 16:48:07 +01001397
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -07001398 if (in_flag & (CPA_ARRAY | CPA_PAGES_ARRAY))
1399 cpa.flags |= in_flag;
Shaohua Lid75586a2008-08-21 10:46:06 +08001400
Thomas Gleixneraf96e442008-02-15 21:49:46 +01001401 /* No alias checking for _NX bit modifications */
1402 checkalias = (pgprot_val(mask_set) | pgprot_val(mask_clr)) != _PAGE_NX;
1403
1404 ret = __change_page_attr_set_clr(&cpa, checkalias);
Thomas Gleixnerff314522008-01-30 13:34:08 +01001405
Thomas Gleixner57a6a462008-01-30 13:34:08 +01001406 /*
Thomas Gleixnerf4ae5da2008-02-04 16:48:07 +01001407 * Check whether we really changed something:
1408 */
Shaohua Lid75586a2008-08-21 10:46:06 +08001409 if (!(cpa.flags & CPA_FLUSHTLB))
Shaohua Li1ac2f7d2008-08-04 14:51:24 +08001410 goto out;
Ingo Molnarcacf8902008-08-21 13:46:33 +02001411
Thomas Gleixnerf4ae5da2008-02-04 16:48:07 +01001412 /*
Andi Kleen6bb83832008-02-04 16:48:06 +01001413 * No need to flush, when we did not set any of the caching
1414 * attributes:
1415 */
Juergen Grossc06814d2014-11-03 14:01:57 +01001416 cache = !!pgprot2cachemode(mask_set);
Andi Kleen6bb83832008-02-04 16:48:06 +01001417
1418 /*
Borislav Petkovb82ad3d2014-03-12 15:13:04 +01001419 * On success we use CLFLUSH, when the CPU supports it to
1420 * avoid the WBINVD. If the CPU does not support it and in the
H. Peter Anvinf026cfa2012-08-14 09:53:38 -07001421 * error case we fall back to cpa_flush_all (which uses
Borislav Petkovb82ad3d2014-03-12 15:13:04 +01001422 * WBINVD):
Thomas Gleixner57a6a462008-01-30 13:34:08 +01001423 */
H. Peter Anvinf026cfa2012-08-14 09:53:38 -07001424 if (!ret && cpu_has_clflush) {
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -07001425 if (cpa.flags & (CPA_PAGES_ARRAY | CPA_ARRAY)) {
1426 cpa_flush_array(addr, numpages, cache,
1427 cpa.flags, pages);
1428 } else
Jack Steinerfa526d02009-09-03 12:56:02 -05001429 cpa_flush_range(baddr, numpages, cache);
Shaohua Lid75586a2008-08-21 10:46:06 +08001430 } else
Andi Kleen6bb83832008-02-04 16:48:06 +01001431 cpa_flush_all(cache);
Ingo Molnarcacf8902008-08-21 13:46:33 +02001432
Thomas Gleixner76ebd052008-02-09 23:24:09 +01001433out:
Thomas Gleixnerff314522008-01-30 13:34:08 +01001434 return ret;
1435}
1436
Shaohua Lid75586a2008-08-21 10:46:06 +08001437static inline int change_page_attr_set(unsigned long *addr, int numpages,
1438 pgprot_t mask, int array)
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001439{
Shaohua Lid75586a2008-08-21 10:46:06 +08001440 return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0), 0,
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -07001441 (array ? CPA_ARRAY : 0), NULL);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001442}
1443
Shaohua Lid75586a2008-08-21 10:46:06 +08001444static inline int change_page_attr_clear(unsigned long *addr, int numpages,
1445 pgprot_t mask, int array)
Thomas Gleixner72932c72008-01-30 13:34:08 +01001446{
Shaohua Lid75586a2008-08-21 10:46:06 +08001447 return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask, 0,
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -07001448 (array ? CPA_ARRAY : 0), NULL);
Thomas Gleixner72932c72008-01-30 13:34:08 +01001449}
1450
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001451static inline int cpa_set_pages_array(struct page **pages, int numpages,
1452 pgprot_t mask)
1453{
1454 return change_page_attr_set_clr(NULL, numpages, mask, __pgprot(0), 0,
1455 CPA_PAGES_ARRAY, pages);
1456}
1457
1458static inline int cpa_clear_pages_array(struct page **pages, int numpages,
1459 pgprot_t mask)
1460{
1461 return change_page_attr_set_clr(NULL, numpages, __pgprot(0), mask, 0,
1462 CPA_PAGES_ARRAY, pages);
1463}
1464
venkatesh.pallipadi@intel.com12193332008-03-18 17:00:18 -07001465int _set_memory_uc(unsigned long addr, int numpages)
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001466{
Suresh Siddhade33c442008-04-25 17:07:22 -07001467 /*
1468 * for now UC MINUS. see comments in ioremap_nocache()
Luis R. Rodrigueze4b6be332015-05-11 10:15:53 +02001469 * If you really need strong UC use ioremap_uc(), but note
1470 * that you cannot override IO areas with set_memory_*() as
1471 * these helpers cannot work with IO memory.
Suresh Siddhade33c442008-04-25 17:07:22 -07001472 */
Shaohua Lid75586a2008-08-21 10:46:06 +08001473 return change_page_attr_set(&addr, numpages,
Juergen Grossc06814d2014-11-03 14:01:57 +01001474 cachemode2pgprot(_PAGE_CACHE_MODE_UC_MINUS),
1475 0);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001476}
venkatesh.pallipadi@intel.com12193332008-03-18 17:00:18 -07001477
1478int set_memory_uc(unsigned long addr, int numpages)
1479{
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001480 int ret;
1481
Suresh Siddhade33c442008-04-25 17:07:22 -07001482 /*
1483 * for now UC MINUS. see comments in ioremap_nocache()
1484 */
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001485 ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
Juergen Grosse00c8cc2014-11-03 14:01:59 +01001486 _PAGE_CACHE_MODE_UC_MINUS, NULL);
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001487 if (ret)
1488 goto out_err;
venkatesh.pallipadi@intel.com12193332008-03-18 17:00:18 -07001489
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001490 ret = _set_memory_uc(addr, numpages);
1491 if (ret)
1492 goto out_free;
1493
1494 return 0;
1495
1496out_free:
1497 free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
1498out_err:
1499 return ret;
venkatesh.pallipadi@intel.com12193332008-03-18 17:00:18 -07001500}
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001501EXPORT_SYMBOL(set_memory_uc);
1502
H Hartley Sweeten2d070ef2011-11-15 14:49:00 -08001503static int _set_memory_array(unsigned long *addr, int addrinarray,
Juergen Grossc06814d2014-11-03 14:01:57 +01001504 enum page_cache_mode new_type)
Shaohua Lid75586a2008-08-21 10:46:06 +08001505{
Toshi Kani623dffb2015-06-04 18:55:20 +02001506 enum page_cache_mode set_type;
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001507 int i, j;
1508 int ret;
1509
Shaohua Lid75586a2008-08-21 10:46:06 +08001510 for (i = 0; i < addrinarray; i++) {
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001511 ret = reserve_memtype(__pa(addr[i]), __pa(addr[i]) + PAGE_SIZE,
Pauli Nieminen4f646252010-04-01 12:45:01 +00001512 new_type, NULL);
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001513 if (ret)
1514 goto out_free;
Shaohua Lid75586a2008-08-21 10:46:06 +08001515 }
1516
Toshi Kani623dffb2015-06-04 18:55:20 +02001517 /* If WC, set to UC- first and then WC */
1518 set_type = (new_type == _PAGE_CACHE_MODE_WC) ?
1519 _PAGE_CACHE_MODE_UC_MINUS : new_type;
1520
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001521 ret = change_page_attr_set(addr, addrinarray,
Toshi Kani623dffb2015-06-04 18:55:20 +02001522 cachemode2pgprot(set_type), 1);
Pauli Nieminen4f646252010-04-01 12:45:01 +00001523
Juergen Grossc06814d2014-11-03 14:01:57 +01001524 if (!ret && new_type == _PAGE_CACHE_MODE_WC)
Pauli Nieminen4f646252010-04-01 12:45:01 +00001525 ret = change_page_attr_set_clr(addr, addrinarray,
Juergen Grossc06814d2014-11-03 14:01:57 +01001526 cachemode2pgprot(
1527 _PAGE_CACHE_MODE_WC),
Pauli Nieminen4f646252010-04-01 12:45:01 +00001528 __pgprot(_PAGE_CACHE_MASK),
1529 0, CPA_ARRAY, NULL);
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001530 if (ret)
1531 goto out_free;
Rene Hermanc5e147c2008-08-22 01:02:20 +02001532
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001533 return 0;
1534
1535out_free:
1536 for (j = 0; j < i; j++)
1537 free_memtype(__pa(addr[j]), __pa(addr[j]) + PAGE_SIZE);
1538
1539 return ret;
Shaohua Lid75586a2008-08-21 10:46:06 +08001540}
Pauli Nieminen4f646252010-04-01 12:45:01 +00001541
1542int set_memory_array_uc(unsigned long *addr, int addrinarray)
1543{
Juergen Grossc06814d2014-11-03 14:01:57 +01001544 return _set_memory_array(addr, addrinarray, _PAGE_CACHE_MODE_UC_MINUS);
Pauli Nieminen4f646252010-04-01 12:45:01 +00001545}
Shaohua Lid75586a2008-08-21 10:46:06 +08001546EXPORT_SYMBOL(set_memory_array_uc);
1547
Pauli Nieminen4f646252010-04-01 12:45:01 +00001548int set_memory_array_wc(unsigned long *addr, int addrinarray)
1549{
Juergen Grossc06814d2014-11-03 14:01:57 +01001550 return _set_memory_array(addr, addrinarray, _PAGE_CACHE_MODE_WC);
Pauli Nieminen4f646252010-04-01 12:45:01 +00001551}
1552EXPORT_SYMBOL(set_memory_array_wc);
1553
Toshi Kani623dffb2015-06-04 18:55:20 +02001554int set_memory_array_wt(unsigned long *addr, int addrinarray)
1555{
1556 return _set_memory_array(addr, addrinarray, _PAGE_CACHE_MODE_WT);
1557}
1558EXPORT_SYMBOL_GPL(set_memory_array_wt);
1559
venkatesh.pallipadi@intel.comef354af2008-03-18 17:00:23 -07001560int _set_memory_wc(unsigned long addr, int numpages)
1561{
venkatesh.pallipadi@intel.com3869c4a2009-04-09 14:26:50 -07001562 int ret;
Pallipadi, Venkateshbdc63402009-07-30 14:43:19 -07001563 unsigned long addr_copy = addr;
1564
venkatesh.pallipadi@intel.com3869c4a2009-04-09 14:26:50 -07001565 ret = change_page_attr_set(&addr, numpages,
Juergen Grossc06814d2014-11-03 14:01:57 +01001566 cachemode2pgprot(_PAGE_CACHE_MODE_UC_MINUS),
1567 0);
venkatesh.pallipadi@intel.com3869c4a2009-04-09 14:26:50 -07001568 if (!ret) {
Pallipadi, Venkateshbdc63402009-07-30 14:43:19 -07001569 ret = change_page_attr_set_clr(&addr_copy, numpages,
Juergen Grossc06814d2014-11-03 14:01:57 +01001570 cachemode2pgprot(
1571 _PAGE_CACHE_MODE_WC),
Pallipadi, Venkateshbdc63402009-07-30 14:43:19 -07001572 __pgprot(_PAGE_CACHE_MASK),
1573 0, 0, NULL);
venkatesh.pallipadi@intel.com3869c4a2009-04-09 14:26:50 -07001574 }
1575 return ret;
venkatesh.pallipadi@intel.comef354af2008-03-18 17:00:23 -07001576}
1577
1578int set_memory_wc(unsigned long addr, int numpages)
1579{
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001580 int ret;
1581
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001582 ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
Juergen Grosse00c8cc2014-11-03 14:01:59 +01001583 _PAGE_CACHE_MODE_WC, NULL);
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001584 if (ret)
Toshi Kani623dffb2015-06-04 18:55:20 +02001585 return ret;
venkatesh.pallipadi@intel.comef354af2008-03-18 17:00:23 -07001586
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001587 ret = _set_memory_wc(addr, numpages);
1588 if (ret)
Toshi Kani623dffb2015-06-04 18:55:20 +02001589 free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001590
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001591 return ret;
venkatesh.pallipadi@intel.comef354af2008-03-18 17:00:23 -07001592}
1593EXPORT_SYMBOL(set_memory_wc);
1594
Toshi Kani623dffb2015-06-04 18:55:20 +02001595int _set_memory_wt(unsigned long addr, int numpages)
1596{
1597 return change_page_attr_set(&addr, numpages,
1598 cachemode2pgprot(_PAGE_CACHE_MODE_WT), 0);
1599}
1600
1601int set_memory_wt(unsigned long addr, int numpages)
1602{
1603 int ret;
1604
1605 ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
1606 _PAGE_CACHE_MODE_WT, NULL);
1607 if (ret)
1608 return ret;
1609
1610 ret = _set_memory_wt(addr, numpages);
1611 if (ret)
1612 free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
1613
1614 return ret;
1615}
1616EXPORT_SYMBOL_GPL(set_memory_wt);
1617
venkatesh.pallipadi@intel.com12193332008-03-18 17:00:18 -07001618int _set_memory_wb(unsigned long addr, int numpages)
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001619{
Juergen Grossc06814d2014-11-03 14:01:57 +01001620 /* WB cache mode is hard wired to all cache attribute bits being 0 */
Shaohua Lid75586a2008-08-21 10:46:06 +08001621 return change_page_attr_clear(&addr, numpages,
1622 __pgprot(_PAGE_CACHE_MASK), 0);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001623}
venkatesh.pallipadi@intel.com12193332008-03-18 17:00:18 -07001624
1625int set_memory_wb(unsigned long addr, int numpages)
1626{
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001627 int ret;
1628
1629 ret = _set_memory_wb(addr, numpages);
1630 if (ret)
1631 return ret;
1632
venkatesh.pallipadi@intel.comc15238d2008-08-20 16:45:51 -07001633 free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001634 return 0;
venkatesh.pallipadi@intel.com12193332008-03-18 17:00:18 -07001635}
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001636EXPORT_SYMBOL(set_memory_wb);
1637
Shaohua Lid75586a2008-08-21 10:46:06 +08001638int set_memory_array_wb(unsigned long *addr, int addrinarray)
1639{
1640 int i;
venkatesh.pallipadi@intel.coma5593e02009-04-09 14:26:48 -07001641 int ret;
1642
Juergen Grossc06814d2014-11-03 14:01:57 +01001643 /* WB cache mode is hard wired to all cache attribute bits being 0 */
venkatesh.pallipadi@intel.coma5593e02009-04-09 14:26:48 -07001644 ret = change_page_attr_clear(addr, addrinarray,
1645 __pgprot(_PAGE_CACHE_MASK), 1);
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001646 if (ret)
1647 return ret;
Shaohua Lid75586a2008-08-21 10:46:06 +08001648
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001649 for (i = 0; i < addrinarray; i++)
1650 free_memtype(__pa(addr[i]), __pa(addr[i]) + PAGE_SIZE);
Rene Hermanc5e147c2008-08-22 01:02:20 +02001651
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001652 return 0;
Shaohua Lid75586a2008-08-21 10:46:06 +08001653}
1654EXPORT_SYMBOL(set_memory_array_wb);
1655
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001656int set_memory_x(unsigned long addr, int numpages)
1657{
H. Peter Anvin583140a2009-11-13 15:28:15 -08001658 if (!(__supported_pte_mask & _PAGE_NX))
1659 return 0;
1660
Shaohua Lid75586a2008-08-21 10:46:06 +08001661 return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_NX), 0);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001662}
1663EXPORT_SYMBOL(set_memory_x);
1664
1665int set_memory_nx(unsigned long addr, int numpages)
1666{
H. Peter Anvin583140a2009-11-13 15:28:15 -08001667 if (!(__supported_pte_mask & _PAGE_NX))
1668 return 0;
1669
Shaohua Lid75586a2008-08-21 10:46:06 +08001670 return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_NX), 0);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001671}
1672EXPORT_SYMBOL(set_memory_nx);
1673
1674int set_memory_ro(unsigned long addr, int numpages)
1675{
Shaohua Lid75586a2008-08-21 10:46:06 +08001676 return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_RW), 0);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001677}
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001678
1679int set_memory_rw(unsigned long addr, int numpages)
1680{
Shaohua Lid75586a2008-08-21 10:46:06 +08001681 return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_RW), 0);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001682}
Ingo Molnarf62d0f02008-01-30 13:34:07 +01001683
1684int set_memory_np(unsigned long addr, int numpages)
1685{
Shaohua Lid75586a2008-08-21 10:46:06 +08001686 return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_PRESENT), 0);
Ingo Molnarf62d0f02008-01-30 13:34:07 +01001687}
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001688
Andi Kleenc9caa022008-03-12 03:53:29 +01001689int set_memory_4k(unsigned long addr, int numpages)
1690{
Shaohua Lid75586a2008-08-21 10:46:06 +08001691 return change_page_attr_set_clr(&addr, numpages, __pgprot(0),
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -07001692 __pgprot(0), 1, 0, NULL);
Andi Kleenc9caa022008-03-12 03:53:29 +01001693}
1694
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001695int set_pages_uc(struct page *page, int numpages)
1696{
1697 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001698
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +01001699 return set_memory_uc(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001700}
1701EXPORT_SYMBOL(set_pages_uc);
1702
Pauli Nieminen4f646252010-04-01 12:45:01 +00001703static int _set_pages_array(struct page **pages, int addrinarray,
Juergen Grossc06814d2014-11-03 14:01:57 +01001704 enum page_cache_mode new_type)
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001705{
1706 unsigned long start;
1707 unsigned long end;
Toshi Kani623dffb2015-06-04 18:55:20 +02001708 enum page_cache_mode set_type;
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001709 int i;
1710 int free_idx;
Pauli Nieminen4f646252010-04-01 12:45:01 +00001711 int ret;
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001712
1713 for (i = 0; i < addrinarray; i++) {
Thomas Hellstrom8523acf2009-08-03 09:25:45 +02001714 if (PageHighMem(pages[i]))
1715 continue;
1716 start = page_to_pfn(pages[i]) << PAGE_SHIFT;
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001717 end = start + PAGE_SIZE;
Pauli Nieminen4f646252010-04-01 12:45:01 +00001718 if (reserve_memtype(start, end, new_type, NULL))
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001719 goto err_out;
1720 }
1721
Toshi Kani623dffb2015-06-04 18:55:20 +02001722 /* If WC, set to UC- first and then WC */
1723 set_type = (new_type == _PAGE_CACHE_MODE_WC) ?
1724 _PAGE_CACHE_MODE_UC_MINUS : new_type;
1725
Pauli Nieminen4f646252010-04-01 12:45:01 +00001726 ret = cpa_set_pages_array(pages, addrinarray,
Toshi Kani623dffb2015-06-04 18:55:20 +02001727 cachemode2pgprot(set_type));
Juergen Grossc06814d2014-11-03 14:01:57 +01001728 if (!ret && new_type == _PAGE_CACHE_MODE_WC)
Pauli Nieminen4f646252010-04-01 12:45:01 +00001729 ret = change_page_attr_set_clr(NULL, addrinarray,
Juergen Grossc06814d2014-11-03 14:01:57 +01001730 cachemode2pgprot(
1731 _PAGE_CACHE_MODE_WC),
Pauli Nieminen4f646252010-04-01 12:45:01 +00001732 __pgprot(_PAGE_CACHE_MASK),
1733 0, CPA_PAGES_ARRAY, pages);
1734 if (ret)
1735 goto err_out;
1736 return 0; /* Success */
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001737err_out:
1738 free_idx = i;
1739 for (i = 0; i < free_idx; i++) {
Thomas Hellstrom8523acf2009-08-03 09:25:45 +02001740 if (PageHighMem(pages[i]))
1741 continue;
1742 start = page_to_pfn(pages[i]) << PAGE_SHIFT;
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001743 end = start + PAGE_SIZE;
1744 free_memtype(start, end);
1745 }
1746 return -EINVAL;
1747}
Pauli Nieminen4f646252010-04-01 12:45:01 +00001748
1749int set_pages_array_uc(struct page **pages, int addrinarray)
1750{
Juergen Grossc06814d2014-11-03 14:01:57 +01001751 return _set_pages_array(pages, addrinarray, _PAGE_CACHE_MODE_UC_MINUS);
Pauli Nieminen4f646252010-04-01 12:45:01 +00001752}
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001753EXPORT_SYMBOL(set_pages_array_uc);
1754
Pauli Nieminen4f646252010-04-01 12:45:01 +00001755int set_pages_array_wc(struct page **pages, int addrinarray)
1756{
Juergen Grossc06814d2014-11-03 14:01:57 +01001757 return _set_pages_array(pages, addrinarray, _PAGE_CACHE_MODE_WC);
Pauli Nieminen4f646252010-04-01 12:45:01 +00001758}
1759EXPORT_SYMBOL(set_pages_array_wc);
1760
Toshi Kani623dffb2015-06-04 18:55:20 +02001761int set_pages_array_wt(struct page **pages, int addrinarray)
1762{
1763 return _set_pages_array(pages, addrinarray, _PAGE_CACHE_MODE_WT);
1764}
1765EXPORT_SYMBOL_GPL(set_pages_array_wt);
1766
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001767int set_pages_wb(struct page *page, int numpages)
1768{
1769 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001770
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +01001771 return set_memory_wb(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001772}
1773EXPORT_SYMBOL(set_pages_wb);
1774
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001775int set_pages_array_wb(struct page **pages, int addrinarray)
1776{
1777 int retval;
1778 unsigned long start;
1779 unsigned long end;
1780 int i;
1781
Juergen Grossc06814d2014-11-03 14:01:57 +01001782 /* WB cache mode is hard wired to all cache attribute bits being 0 */
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001783 retval = cpa_clear_pages_array(pages, addrinarray,
1784 __pgprot(_PAGE_CACHE_MASK));
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001785 if (retval)
1786 return retval;
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001787
1788 for (i = 0; i < addrinarray; i++) {
Thomas Hellstrom8523acf2009-08-03 09:25:45 +02001789 if (PageHighMem(pages[i]))
1790 continue;
1791 start = page_to_pfn(pages[i]) << PAGE_SHIFT;
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001792 end = start + PAGE_SIZE;
1793 free_memtype(start, end);
1794 }
1795
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001796 return 0;
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001797}
1798EXPORT_SYMBOL(set_pages_array_wb);
1799
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001800int set_pages_x(struct page *page, int numpages)
1801{
1802 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001803
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +01001804 return set_memory_x(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001805}
1806EXPORT_SYMBOL(set_pages_x);
1807
1808int set_pages_nx(struct page *page, int numpages)
1809{
1810 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001811
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +01001812 return set_memory_nx(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001813}
1814EXPORT_SYMBOL(set_pages_nx);
1815
1816int set_pages_ro(struct page *page, int numpages)
1817{
1818 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001819
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +01001820 return set_memory_ro(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001821}
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001822
1823int set_pages_rw(struct page *page, int numpages)
1824{
1825 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001826
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +01001827 return set_memory_rw(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001828}
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830#ifdef CONFIG_DEBUG_PAGEALLOC
Ingo Molnarf62d0f02008-01-30 13:34:07 +01001831
1832static int __set_pages_p(struct page *page, int numpages)
1833{
Shaohua Lid75586a2008-08-21 10:46:06 +08001834 unsigned long tempaddr = (unsigned long) page_address(page);
1835 struct cpa_data cpa = { .vaddr = &tempaddr,
Borislav Petkov82f07122013-10-31 17:25:07 +01001836 .pgd = NULL,
Thomas Gleixner72e458d2008-02-04 16:48:07 +01001837 .numpages = numpages,
1838 .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW),
Shaohua Lid75586a2008-08-21 10:46:06 +08001839 .mask_clr = __pgprot(0),
1840 .flags = 0};
Thomas Gleixner72932c72008-01-30 13:34:08 +01001841
Suresh Siddha55121b42008-09-23 14:00:40 -07001842 /*
1843 * No alias checking needed for setting present flag. otherwise,
1844 * we may need to break large pages for 64-bit kernel text
1845 * mappings (this adds to complexity if we want to do this from
1846 * atomic context especially). Let's keep it simple!
1847 */
1848 return __change_page_attr_set_clr(&cpa, 0);
Ingo Molnarf62d0f02008-01-30 13:34:07 +01001849}
1850
1851static int __set_pages_np(struct page *page, int numpages)
1852{
Shaohua Lid75586a2008-08-21 10:46:06 +08001853 unsigned long tempaddr = (unsigned long) page_address(page);
1854 struct cpa_data cpa = { .vaddr = &tempaddr,
Borislav Petkov82f07122013-10-31 17:25:07 +01001855 .pgd = NULL,
Thomas Gleixner72e458d2008-02-04 16:48:07 +01001856 .numpages = numpages,
1857 .mask_set = __pgprot(0),
Shaohua Lid75586a2008-08-21 10:46:06 +08001858 .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW),
1859 .flags = 0};
Thomas Gleixner72932c72008-01-30 13:34:08 +01001860
Suresh Siddha55121b42008-09-23 14:00:40 -07001861 /*
1862 * No alias checking needed for setting not present flag. otherwise,
1863 * we may need to break large pages for 64-bit kernel text
1864 * mappings (this adds to complexity if we want to do this from
1865 * atomic context especially). Let's keep it simple!
1866 */
1867 return __change_page_attr_set_clr(&cpa, 0);
Ingo Molnarf62d0f02008-01-30 13:34:07 +01001868}
1869
Joonsoo Kim031bc572014-12-12 16:55:52 -08001870void __kernel_map_pages(struct page *page, int numpages, int enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
1872 if (PageHighMem(page))
1873 return;
Ingo Molnar9f4c8152008-01-30 13:33:41 +01001874 if (!enable) {
Ingo Molnarf9b84042006-06-27 02:54:49 -07001875 debug_check_no_locks_freed(page_address(page),
1876 numpages * PAGE_SIZE);
Ingo Molnar9f4c8152008-01-30 13:33:41 +01001877 }
Ingo Molnarde5097c2006-01-09 15:59:21 -08001878
Ingo Molnar9f4c8152008-01-30 13:33:41 +01001879 /*
Ingo Molnarf8d84062008-02-13 14:09:53 +01001880 * The return value is ignored as the calls cannot fail.
Suresh Siddha55121b42008-09-23 14:00:40 -07001881 * Large pages for identity mappings are not used at boot time
1882 * and hence no memory allocations during large page split.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 */
Ingo Molnarf62d0f02008-01-30 13:34:07 +01001884 if (enable)
1885 __set_pages_p(page, numpages);
1886 else
1887 __set_pages_np(page, numpages);
Ingo Molnar9f4c8152008-01-30 13:33:41 +01001888
1889 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +01001890 * We should perform an IPI and flush all tlbs,
1891 * but that can deadlock->flush only current cpu:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 */
1893 __flush_tlb_all();
Boris Ostrovsky26564602013-04-11 13:59:52 -04001894
1895 arch_flush_lazy_mmu_mode();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896}
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +01001897
1898#ifdef CONFIG_HIBERNATION
1899
1900bool kernel_page_present(struct page *page)
1901{
1902 unsigned int level;
1903 pte_t *pte;
1904
1905 if (PageHighMem(page))
1906 return false;
1907
1908 pte = lookup_address((unsigned long)page_address(page), &level);
1909 return (pte_val(*pte) & _PAGE_PRESENT);
1910}
1911
1912#endif /* CONFIG_HIBERNATION */
1913
1914#endif /* CONFIG_DEBUG_PAGEALLOC */
Arjan van de Vend1028a12008-01-30 13:34:07 +01001915
Borislav Petkov82f07122013-10-31 17:25:07 +01001916int kernel_map_pages_in_pgd(pgd_t *pgd, u64 pfn, unsigned long address,
1917 unsigned numpages, unsigned long page_flags)
1918{
1919 int retval = -EINVAL;
1920
1921 struct cpa_data cpa = {
1922 .vaddr = &address,
1923 .pfn = pfn,
1924 .pgd = pgd,
1925 .numpages = numpages,
1926 .mask_set = __pgprot(0),
1927 .mask_clr = __pgprot(0),
1928 .flags = 0,
1929 };
1930
1931 if (!(__supported_pte_mask & _PAGE_NX))
1932 goto out;
1933
1934 if (!(page_flags & _PAGE_NX))
1935 cpa.mask_clr = __pgprot(_PAGE_NX);
1936
1937 cpa.mask_set = __pgprot(_PAGE_PRESENT | page_flags);
1938
1939 retval = __change_page_attr_set_clr(&cpa, 0);
1940 __flush_tlb_all();
1941
1942out:
1943 return retval;
1944}
1945
Borislav Petkov42a54772014-01-18 12:48:16 +01001946void kernel_unmap_pages_in_pgd(pgd_t *root, unsigned long address,
1947 unsigned numpages)
1948{
1949 unmap_pgd_range(root, address, address + (numpages << PAGE_SHIFT));
1950}
1951
Arjan van de Vend1028a12008-01-30 13:34:07 +01001952/*
1953 * The testcases use internal knowledge of the implementation that shouldn't
1954 * be exposed to the rest of the kernel. Include these directly here.
1955 */
1956#ifdef CONFIG_CPA_DEBUG
1957#include "pageattr-test.c"
1958#endif