blob: c53de62a117066e6358e4640ba87d029cac0fd37 [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>
Ingo Molnar9f4c8152008-01-30 13:33:41 +010017
Thomas Gleixner950f9d92008-01-30 13:34:06 +010018#include <asm/e820.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/processor.h>
20#include <asm/tlbflush.h>
Dave Jonesf8af0952006-01-06 00:12:10 -080021#include <asm/sections.h>
Jeremy Fitzhardinge93dbda72009-02-26 17:35:44 -080022#include <asm/setup.h>
Ingo Molnar9f4c8152008-01-30 13:33:41 +010023#include <asm/uaccess.h>
24#include <asm/pgalloc.h>
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +010025#include <asm/proto.h>
venkatesh.pallipadi@intel.com12193332008-03-18 17:00:18 -070026#include <asm/pat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Ingo Molnar9df84992008-02-04 16:48:09 +010028/*
29 * The current flushing context - we pass it instead of 5 arguments:
30 */
Thomas Gleixner72e458d2008-02-04 16:48:07 +010031struct cpa_data {
Shaohua Lid75586a2008-08-21 10:46:06 +080032 unsigned long *vaddr;
Borislav Petkov0fd64c22013-10-31 17:25:00 +010033 pgd_t *pgd;
Thomas Gleixner72e458d2008-02-04 16:48:07 +010034 pgprot_t mask_set;
35 pgprot_t mask_clr;
Thomas Gleixner65e074d2008-02-04 16:48:07 +010036 int numpages;
Shaohua Lid75586a2008-08-21 10:46:06 +080037 int flags;
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +010038 unsigned long pfn;
Andi Kleenc9caa022008-03-12 03:53:29 +010039 unsigned force_split : 1;
Shaohua Lid75586a2008-08-21 10:46:06 +080040 int curpage;
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -070041 struct page **pages;
Thomas Gleixner72e458d2008-02-04 16:48:07 +010042};
43
Suresh Siddhaad5ca552008-09-23 14:00:42 -070044/*
45 * Serialize cpa() (for !DEBUG_PAGEALLOC which uses large identity mappings)
46 * using cpa_lock. So that we don't allow any other cpu, with stale large tlb
47 * entries change the page attribute in parallel to some other cpu
48 * splitting a large page entry along with changing the attribute.
49 */
50static DEFINE_SPINLOCK(cpa_lock);
51
Shaohua Lid75586a2008-08-21 10:46:06 +080052#define CPA_FLUSHTLB 1
53#define CPA_ARRAY 2
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -070054#define CPA_PAGES_ARRAY 4
Shaohua Lid75586a2008-08-21 10:46:06 +080055
Thomas Gleixner65280e62008-05-05 16:35:21 +020056#ifdef CONFIG_PROC_FS
Andi Kleence0c0e52008-05-02 11:46:49 +020057static unsigned long direct_pages_count[PG_LEVEL_NUM];
58
Thomas Gleixner65280e62008-05-05 16:35:21 +020059void update_page_count(int level, unsigned long pages)
Andi Kleence0c0e52008-05-02 11:46:49 +020060{
Andi Kleence0c0e52008-05-02 11:46:49 +020061 /* Protect against CPA */
Andrea Arcangelia79e53d2011-02-16 15:45:22 -080062 spin_lock(&pgd_lock);
Andi Kleence0c0e52008-05-02 11:46:49 +020063 direct_pages_count[level] += pages;
Andrea Arcangelia79e53d2011-02-16 15:45:22 -080064 spin_unlock(&pgd_lock);
Andi Kleence0c0e52008-05-02 11:46:49 +020065}
66
Thomas Gleixner65280e62008-05-05 16:35:21 +020067static void split_page_count(int level)
68{
69 direct_pages_count[level]--;
70 direct_pages_count[level - 1] += PTRS_PER_PTE;
71}
72
Alexey Dobriyane1759c22008-10-15 23:50:22 +040073void arch_report_meminfo(struct seq_file *m)
Thomas Gleixner65280e62008-05-05 16:35:21 +020074{
Hugh Dickinsb9c3bfc2008-11-06 12:05:40 +000075 seq_printf(m, "DirectMap4k: %8lu kB\n",
Hugh Dickinsa06de632008-08-15 13:58:32 +010076 direct_pages_count[PG_LEVEL_4K] << 2);
77#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
Hugh Dickinsb9c3bfc2008-11-06 12:05:40 +000078 seq_printf(m, "DirectMap2M: %8lu kB\n",
Hugh Dickinsa06de632008-08-15 13:58:32 +010079 direct_pages_count[PG_LEVEL_2M] << 11);
80#else
Hugh Dickinsb9c3bfc2008-11-06 12:05:40 +000081 seq_printf(m, "DirectMap4M: %8lu kB\n",
Hugh Dickinsa06de632008-08-15 13:58:32 +010082 direct_pages_count[PG_LEVEL_2M] << 12);
83#endif
Thomas Gleixner65280e62008-05-05 16:35:21 +020084#ifdef CONFIG_X86_64
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#endif
Thomas Gleixner65280e62008-05-05 16:35:21 +020089}
90#else
91static inline void split_page_count(int level) { }
92#endif
93
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +010094#ifdef CONFIG_X86_64
95
96static inline unsigned long highmap_start_pfn(void)
97{
Alexander Duyckfc8d7822012-11-16 13:57:13 -080098 return __pa_symbol(_text) >> PAGE_SHIFT;
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +010099}
100
101static inline unsigned long highmap_end_pfn(void)
102{
Alexander Duyckfc8d7822012-11-16 13:57:13 -0800103 return __pa_symbol(roundup(_brk_end, PMD_SIZE)) >> PAGE_SHIFT;
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100104}
105
106#endif
107
Ingo Molnar92cb54a2008-02-13 14:37:52 +0100108#ifdef CONFIG_DEBUG_PAGEALLOC
109# define debug_pagealloc 1
110#else
111# define debug_pagealloc 0
112#endif
113
Arjan van de Vened724be2008-01-30 13:34:04 +0100114static inline int
115within(unsigned long addr, unsigned long start, unsigned long end)
Ingo Molnar687c4822008-01-30 13:34:04 +0100116{
Arjan van de Vened724be2008-01-30 13:34:04 +0100117 return addr >= start && addr < end;
118}
119
120/*
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100121 * Flushing functions
122 */
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +0100123
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +0100124/**
125 * clflush_cache_range - flush a cache range with clflush
Wanpeng Li9efc31b2012-06-10 10:50:52 +0800126 * @vaddr: virtual start address
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +0100127 * @size: number of bytes to flush
128 *
129 * clflush is an unordered instruction which needs fencing with mfence
130 * to avoid ordering issues.
131 */
Ingo Molnar4c61afc2008-01-30 13:34:09 +0100132void clflush_cache_range(void *vaddr, unsigned int size)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100133{
Ingo Molnar4c61afc2008-01-30 13:34:09 +0100134 void *vend = vaddr + size - 1;
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100135
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +0100136 mb();
Ingo Molnar4c61afc2008-01-30 13:34:09 +0100137
138 for (; vaddr < vend; vaddr += boot_cpu_data.x86_clflush_size)
139 clflush(vaddr);
140 /*
141 * Flush any possible final partial cacheline:
142 */
143 clflush(vend);
144
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +0100145 mb();
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100146}
Eric Anholte517a5e2009-09-10 17:48:48 -0700147EXPORT_SYMBOL_GPL(clflush_cache_range);
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100148
Thomas Gleixneraf1e6842008-01-30 13:34:08 +0100149static void __cpa_flush_all(void *arg)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100150{
Andi Kleen6bb83832008-02-04 16:48:06 +0100151 unsigned long cache = (unsigned long)arg;
152
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100153 /*
154 * Flush all to work around Errata in early athlons regarding
155 * large page flushing.
156 */
157 __flush_tlb_all();
158
venkatesh.pallipadi@intel.com0b827532009-05-22 13:23:37 -0700159 if (cache && boot_cpu_data.x86 >= 4)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100160 wbinvd();
161}
162
Andi Kleen6bb83832008-02-04 16:48:06 +0100163static void cpa_flush_all(unsigned long cache)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100164{
165 BUG_ON(irqs_disabled());
166
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200167 on_each_cpu(__cpa_flush_all, (void *) cache, 1);
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100168}
169
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100170static void __cpa_flush_range(void *arg)
171{
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100172 /*
173 * We could optimize that further and do individual per page
174 * tlb invalidates for a low number of pages. Caveat: we must
175 * flush the high aliases on 64bit as well.
176 */
177 __flush_tlb_all();
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100178}
179
Andi Kleen6bb83832008-02-04 16:48:06 +0100180static void cpa_flush_range(unsigned long start, int numpages, int cache)
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100181{
Ingo Molnar4c61afc2008-01-30 13:34:09 +0100182 unsigned int i, level;
183 unsigned long addr;
184
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100185 BUG_ON(irqs_disabled());
Ingo Molnar4c61afc2008-01-30 13:34:09 +0100186 WARN_ON(PAGE_ALIGN(start) != start);
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100187
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200188 on_each_cpu(__cpa_flush_range, NULL, 1);
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100189
Andi Kleen6bb83832008-02-04 16:48:06 +0100190 if (!cache)
191 return;
192
Thomas Gleixner3b233e52008-01-30 13:34:08 +0100193 /*
194 * We only need to flush on one CPU,
195 * clflush is a MESI-coherent instruction that
196 * will cause all other CPUs to flush the same
197 * cachelines:
198 */
Ingo Molnar4c61afc2008-01-30 13:34:09 +0100199 for (i = 0, addr = start; i < numpages; i++, addr += PAGE_SIZE) {
200 pte_t *pte = lookup_address(addr, &level);
201
202 /*
203 * Only flush present addresses:
204 */
Thomas Gleixner7bfb72e2008-02-04 16:48:08 +0100205 if (pte && (pte_val(*pte) & _PAGE_PRESENT))
Ingo Molnar4c61afc2008-01-30 13:34:09 +0100206 clflush_cache_range((void *) addr, PAGE_SIZE);
207 }
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100208}
209
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700210static void cpa_flush_array(unsigned long *start, int numpages, int cache,
211 int in_flags, struct page **pages)
Shaohua Lid75586a2008-08-21 10:46:06 +0800212{
213 unsigned int i, level;
Pallipadi, Venkatesh21717872009-05-26 10:33:35 -0700214 unsigned long do_wbinvd = cache && numpages >= 1024; /* 4M threshold */
Shaohua Lid75586a2008-08-21 10:46:06 +0800215
216 BUG_ON(irqs_disabled());
217
Pallipadi, Venkatesh21717872009-05-26 10:33:35 -0700218 on_each_cpu(__cpa_flush_all, (void *) do_wbinvd, 1);
Shaohua Lid75586a2008-08-21 10:46:06 +0800219
Pallipadi, Venkatesh21717872009-05-26 10:33:35 -0700220 if (!cache || do_wbinvd)
Shaohua Lid75586a2008-08-21 10:46:06 +0800221 return;
222
Shaohua Lid75586a2008-08-21 10:46:06 +0800223 /*
224 * We only need to flush on one CPU,
225 * clflush is a MESI-coherent instruction that
226 * will cause all other CPUs to flush the same
227 * cachelines:
228 */
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700229 for (i = 0; i < numpages; i++) {
230 unsigned long addr;
231 pte_t *pte;
232
233 if (in_flags & CPA_PAGES_ARRAY)
234 addr = (unsigned long)page_address(pages[i]);
235 else
236 addr = start[i];
237
238 pte = lookup_address(addr, &level);
Shaohua Lid75586a2008-08-21 10:46:06 +0800239
240 /*
241 * Only flush present addresses:
242 */
243 if (pte && (pte_val(*pte) & _PAGE_PRESENT))
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700244 clflush_cache_range((void *)addr, PAGE_SIZE);
Shaohua Lid75586a2008-08-21 10:46:06 +0800245 }
246}
247
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100248/*
Arjan van de Vened724be2008-01-30 13:34:04 +0100249 * Certain areas of memory on x86 require very specific protection flags,
250 * for example the BIOS area or kernel text. Callers don't always get this
251 * right (again, ioremap() on BIOS memory is not uncommon) so this function
252 * checks and fixes these known static required protection bits.
253 */
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100254static inline pgprot_t static_protections(pgprot_t prot, unsigned long address,
255 unsigned long pfn)
Arjan van de Vened724be2008-01-30 13:34:04 +0100256{
257 pgprot_t forbidden = __pgprot(0);
258
Ingo Molnar687c4822008-01-30 13:34:04 +0100259 /*
Arjan van de Vened724be2008-01-30 13:34:04 +0100260 * The BIOS area between 640k and 1Mb needs to be executable for
261 * PCI BIOS based config access (CONFIG_PCI_GOBIOS) support.
Ingo Molnar687c4822008-01-30 13:34:04 +0100262 */
Matthieu Castet5bd5a452010-11-16 22:31:26 +0100263#ifdef CONFIG_PCI_BIOS
264 if (pcibios_enabled && within(pfn, BIOS_BEGIN >> PAGE_SHIFT, BIOS_END >> PAGE_SHIFT))
Arjan van de Vened724be2008-01-30 13:34:04 +0100265 pgprot_val(forbidden) |= _PAGE_NX;
Matthieu Castet5bd5a452010-11-16 22:31:26 +0100266#endif
Arjan van de Vened724be2008-01-30 13:34:04 +0100267
268 /*
269 * The kernel text needs to be executable for obvious reasons
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100270 * Does not cover __inittext since that is gone later on. On
271 * 64bit we do not enforce !NX on the low mapping
Arjan van de Vened724be2008-01-30 13:34:04 +0100272 */
273 if (within(address, (unsigned long)_text, (unsigned long)_etext))
274 pgprot_val(forbidden) |= _PAGE_NX;
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100275
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100276 /*
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100277 * The .rodata section needs to be read-only. Using the pfn
278 * catches all aliases.
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100279 */
Alexander Duyckfc8d7822012-11-16 13:57:13 -0800280 if (within(pfn, __pa_symbol(__start_rodata) >> PAGE_SHIFT,
281 __pa_symbol(__end_rodata) >> PAGE_SHIFT))
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100282 pgprot_val(forbidden) |= _PAGE_RW;
Arjan van de Vened724be2008-01-30 13:34:04 +0100283
Suresh Siddha55ca3cc2009-10-28 18:46:57 -0800284#if defined(CONFIG_X86_64) && defined(CONFIG_DEBUG_RODATA)
Suresh Siddha74e08172009-10-14 14:46:56 -0700285 /*
Suresh Siddha502f6602009-10-28 18:46:56 -0800286 * Once the kernel maps the text as RO (kernel_set_to_readonly is set),
287 * kernel text mappings for the large page aligned text, rodata sections
288 * will be always read-only. For the kernel identity mappings covering
289 * the holes caused by this alignment can be anything that user asks.
Suresh Siddha74e08172009-10-14 14:46:56 -0700290 *
291 * This will preserve the large page mappings for kernel text/data
292 * at no extra cost.
293 */
Suresh Siddha502f6602009-10-28 18:46:56 -0800294 if (kernel_set_to_readonly &&
295 within(address, (unsigned long)_text,
Suresh Siddha281ff332010-02-18 11:51:40 -0800296 (unsigned long)__end_rodata_hpage_align)) {
297 unsigned int level;
298
299 /*
300 * Don't enforce the !RW mapping for the kernel text mapping,
301 * if the current mapping is already using small page mapping.
302 * No need to work hard to preserve large page mappings in this
303 * case.
304 *
305 * This also fixes the Linux Xen paravirt guest boot failure
306 * (because of unexpected read-only mappings for kernel identity
307 * mappings). In this paravirt guest case, the kernel text
308 * mapping and the kernel identity mapping share the same
309 * page-table pages. Thus we can't really use different
310 * protections for the kernel text and identity mappings. Also,
311 * these shared mappings are made of small page mappings.
312 * Thus this don't enforce !RW mapping for small page kernel
313 * text mapping logic will help Linux Xen parvirt guest boot
Lucas De Marchi0d2eb442011-03-17 16:24:16 -0300314 * as well.
Suresh Siddha281ff332010-02-18 11:51:40 -0800315 */
316 if (lookup_address(address, &level) && (level != PG_LEVEL_4K))
317 pgprot_val(forbidden) |= _PAGE_RW;
318 }
Suresh Siddha74e08172009-10-14 14:46:56 -0700319#endif
320
Arjan van de Vened724be2008-01-30 13:34:04 +0100321 prot = __pgprot(pgprot_val(prot) & ~pgprot_val(forbidden));
Ingo Molnar687c4822008-01-30 13:34:04 +0100322
323 return prot;
324}
325
Borislav Petkov0fd64c22013-10-31 17:25:00 +0100326static pte_t *__lookup_address_in_pgd(pgd_t *pgd, unsigned long address,
327 unsigned int *level)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100328{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 pud_t *pud;
330 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100331
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100332 *level = PG_LEVEL_NONE;
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (pgd_none(*pgd))
335 return NULL;
Ingo Molnar9df84992008-02-04 16:48:09 +0100336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 pud = pud_offset(pgd, address);
338 if (pud_none(*pud))
339 return NULL;
Andi Kleenc2f71ee2008-02-04 16:48:09 +0100340
341 *level = PG_LEVEL_1G;
342 if (pud_large(*pud) || !pud_present(*pud))
343 return (pte_t *)pud;
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 pmd = pmd_offset(pud, address);
346 if (pmd_none(*pmd))
347 return NULL;
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100348
349 *level = PG_LEVEL_2M;
Thomas Gleixner9a14aef2008-02-04 16:48:07 +0100350 if (pmd_large(*pmd) || !pmd_present(*pmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return (pte_t *)pmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100353 *level = PG_LEVEL_4K;
Ingo Molnar9df84992008-02-04 16:48:09 +0100354
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100355 return pte_offset_kernel(pmd, address);
356}
Borislav Petkov0fd64c22013-10-31 17:25:00 +0100357
358/*
359 * Lookup the page table entry for a virtual address. Return a pointer
360 * to the entry and the level of the mapping.
361 *
362 * Note: We return pud and pmd either when the entry is marked large
363 * or when the present bit is not set. Otherwise we would return a
364 * pointer to a nonexisting mapping.
365 */
366pte_t *lookup_address(unsigned long address, unsigned int *level)
367{
368 return __lookup_address_in_pgd(pgd_offset_k(address), address, level);
369}
Pekka Paalanen75bb8832008-05-12 21:20:56 +0200370EXPORT_SYMBOL_GPL(lookup_address);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100371
Borislav Petkov0fd64c22013-10-31 17:25:00 +0100372static pte_t *_lookup_address_cpa(struct cpa_data *cpa, unsigned long address,
373 unsigned int *level)
374{
375 if (cpa->pgd)
376 return __lookup_address_in_pgd(cpa->pgd + pgd_index(address),
377 address, level);
378
379 return lookup_address(address, level);
380}
381
Ingo Molnar9df84992008-02-04 16:48:09 +0100382/*
Dave Hansend7656532013-01-22 13:24:33 -0800383 * This is necessary because __pa() does not work on some
384 * kinds of memory, like vmalloc() or the alloc_remap()
385 * areas on 32-bit NUMA systems. The percpu areas can
386 * end up in this kind of memory, for instance.
387 *
388 * This could be optimized, but it is only intended to be
389 * used at inititalization time, and keeping it
390 * unoptimized should increase the testing coverage for
391 * the more obscure platforms.
392 */
393phys_addr_t slow_virt_to_phys(void *__virt_addr)
394{
395 unsigned long virt_addr = (unsigned long)__virt_addr;
396 phys_addr_t phys_addr;
397 unsigned long offset;
398 enum pg_level level;
399 unsigned long psize;
400 unsigned long pmask;
401 pte_t *pte;
402
403 pte = lookup_address(virt_addr, &level);
404 BUG_ON(!pte);
405 psize = page_level_size(level);
406 pmask = page_level_mask(level);
407 offset = virt_addr & ~pmask;
408 phys_addr = pte_pfn(*pte) << PAGE_SHIFT;
409 return (phys_addr | offset);
410}
411EXPORT_SYMBOL_GPL(slow_virt_to_phys);
412
413/*
Ingo Molnar9df84992008-02-04 16:48:09 +0100414 * Set the new pmd in all the pgds we know about:
415 */
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100416static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100417{
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100418 /* change init_mm */
419 set_pte_atomic(kpte, pte);
Ingo Molnar44af6c42008-01-30 13:34:03 +0100420#ifdef CONFIG_X86_32
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100421 if (!SHARED_KERNEL_PMD) {
Ingo Molnar44af6c42008-01-30 13:34:03 +0100422 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Jeremy Fitzhardingee3ed9102008-01-30 13:34:11 +0100424 list_for_each_entry(page, &pgd_list, lru) {
Ingo Molnar44af6c42008-01-30 13:34:03 +0100425 pgd_t *pgd;
426 pud_t *pud;
427 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100428
Ingo Molnar44af6c42008-01-30 13:34:03 +0100429 pgd = (pgd_t *)page_address(page) + pgd_index(address);
430 pud = pud_offset(pgd, address);
431 pmd = pmd_offset(pud, address);
432 set_pte_atomic((pte_t *)pmd, pte);
433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
Ingo Molnar44af6c42008-01-30 13:34:03 +0100435#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436}
437
Ingo Molnar9df84992008-02-04 16:48:09 +0100438static int
439try_preserve_large_page(pte_t *kpte, unsigned long address,
440 struct cpa_data *cpa)
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100441{
Andrea Arcangelia79e53d2011-02-16 15:45:22 -0800442 unsigned long nextpage_addr, numpages, pmask, psize, addr, pfn;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100443 pte_t new_pte, old_pte, *tmp;
matthieu castet64edc8e2010-11-16 22:30:27 +0100444 pgprot_t old_prot, new_prot, req_prot;
Thomas Gleixnerfac84932008-02-09 23:24:09 +0100445 int i, do_split = 1;
Dave Hansenf3c4fbb2013-01-22 13:24:32 -0800446 enum pg_level level;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100447
Andi Kleenc9caa022008-03-12 03:53:29 +0100448 if (cpa->force_split)
449 return 1;
450
Andrea Arcangelia79e53d2011-02-16 15:45:22 -0800451 spin_lock(&pgd_lock);
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100452 /*
453 * Check for races, another CPU might have split this page
454 * up already:
455 */
456 tmp = lookup_address(address, &level);
457 if (tmp != kpte)
458 goto out_unlock;
459
460 switch (level) {
461 case PG_LEVEL_2M:
Andi Kleenf07333f2008-02-04 16:48:09 +0100462#ifdef CONFIG_X86_64
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100463 case PG_LEVEL_1G:
Andi Kleenf07333f2008-02-04 16:48:09 +0100464#endif
Dave Hansenf3c4fbb2013-01-22 13:24:32 -0800465 psize = page_level_size(level);
466 pmask = page_level_mask(level);
467 break;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100468 default:
Ingo Molnarbeaff632008-02-04 16:48:09 +0100469 do_split = -EINVAL;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100470 goto out_unlock;
471 }
472
473 /*
474 * Calculate the number of pages, which fit into this large
475 * page starting at address:
476 */
477 nextpage_addr = (address + psize) & pmask;
478 numpages = (nextpage_addr - address) >> PAGE_SHIFT;
Rafael J. Wysocki9b5cf482008-03-03 01:17:37 +0100479 if (numpages < cpa->numpages)
480 cpa->numpages = numpages;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100481
482 /*
483 * We are safe now. Check whether the new pgprot is the same:
484 */
485 old_pte = *kpte;
Andrea Arcangelif76cfa32013-04-10 15:28:25 +0200486 old_prot = req_prot = pte_pgprot(old_pte);
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100487
matthieu castet64edc8e2010-11-16 22:30:27 +0100488 pgprot_val(req_prot) &= ~pgprot_val(cpa->mask_clr);
489 pgprot_val(req_prot) |= pgprot_val(cpa->mask_set);
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100490
491 /*
Andrea Arcangelia8aed3e2013-02-22 15:11:51 -0800492 * Set the PSE and GLOBAL flags only if the PRESENT flag is
493 * set otherwise pmd_present/pmd_huge will return true even on
494 * a non present pmd. The canon_pgprot will clear _PAGE_GLOBAL
495 * for the ancient hardware that doesn't support it.
496 */
Andrea Arcangelif76cfa32013-04-10 15:28:25 +0200497 if (pgprot_val(req_prot) & _PAGE_PRESENT)
498 pgprot_val(req_prot) |= _PAGE_PSE | _PAGE_GLOBAL;
Andrea Arcangelia8aed3e2013-02-22 15:11:51 -0800499 else
Andrea Arcangelif76cfa32013-04-10 15:28:25 +0200500 pgprot_val(req_prot) &= ~(_PAGE_PSE | _PAGE_GLOBAL);
Andrea Arcangelia8aed3e2013-02-22 15:11:51 -0800501
Andrea Arcangelif76cfa32013-04-10 15:28:25 +0200502 req_prot = canon_pgprot(req_prot);
Andrea Arcangelia8aed3e2013-02-22 15:11:51 -0800503
504 /*
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100505 * old_pte points to the large page base address. So we need
506 * to add the offset of the virtual address:
507 */
508 pfn = pte_pfn(old_pte) + ((address & (psize - 1)) >> PAGE_SHIFT);
509 cpa->pfn = pfn;
510
matthieu castet64edc8e2010-11-16 22:30:27 +0100511 new_prot = static_protections(req_prot, address, pfn);
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100512
513 /*
Thomas Gleixnerfac84932008-02-09 23:24:09 +0100514 * We need to check the full range, whether
515 * static_protection() requires a different pgprot for one of
516 * the pages in the range we try to preserve:
517 */
matthieu castet64edc8e2010-11-16 22:30:27 +0100518 addr = address & pmask;
519 pfn = pte_pfn(old_pte);
520 for (i = 0; i < (psize >> PAGE_SHIFT); i++, addr += PAGE_SIZE, pfn++) {
521 pgprot_t chk_prot = static_protections(req_prot, addr, pfn);
Thomas Gleixnerfac84932008-02-09 23:24:09 +0100522
523 if (pgprot_val(chk_prot) != pgprot_val(new_prot))
524 goto out_unlock;
525 }
526
527 /*
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100528 * If there are no changes, return. maxpages has been updated
529 * above:
530 */
531 if (pgprot_val(new_prot) == pgprot_val(old_prot)) {
Ingo Molnarbeaff632008-02-04 16:48:09 +0100532 do_split = 0;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100533 goto out_unlock;
534 }
535
536 /*
537 * We need to change the attributes. Check, whether we can
538 * change the large page in one go. We request a split, when
539 * the address is not aligned and the number of pages is
540 * smaller than the number of pages in the large page. Note
541 * that we limited the number of possible pages already to
542 * the number of pages in the large page.
543 */
matthieu castet64edc8e2010-11-16 22:30:27 +0100544 if (address == (address & pmask) && cpa->numpages == (psize >> PAGE_SHIFT)) {
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100545 /*
546 * The address is aligned and the number of pages
547 * covers the full page.
548 */
Andrea Arcangelia8aed3e2013-02-22 15:11:51 -0800549 new_pte = pfn_pte(pte_pfn(old_pte), new_prot);
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100550 __set_pmd_pte(kpte, address, new_pte);
Shaohua Lid75586a2008-08-21 10:46:06 +0800551 cpa->flags |= CPA_FLUSHTLB;
Ingo Molnarbeaff632008-02-04 16:48:09 +0100552 do_split = 0;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100553 }
554
555out_unlock:
Andrea Arcangelia79e53d2011-02-16 15:45:22 -0800556 spin_unlock(&pgd_lock);
Ingo Molnar9df84992008-02-04 16:48:09 +0100557
Ingo Molnarbeaff632008-02-04 16:48:09 +0100558 return do_split;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100559}
560
Borislav Petkov59528862013-03-21 18:16:57 +0100561static int
562__split_large_page(pte_t *kpte, unsigned long address, struct page *base)
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100563{
Borislav Petkov59528862013-03-21 18:16:57 +0100564 pte_t *pbase = (pte_t *)page_address(base);
Andrea Arcangelia79e53d2011-02-16 15:45:22 -0800565 unsigned long pfn, pfninc = 1;
Ingo Molnar86f03982008-01-30 13:34:09 +0100566 unsigned int i, level;
Wen Congyangae9aae92013-02-22 16:33:04 -0800567 pte_t *tmp;
Ingo Molnar9df84992008-02-04 16:48:09 +0100568 pgprot_t ref_prot;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100569
Andrea Arcangelia79e53d2011-02-16 15:45:22 -0800570 spin_lock(&pgd_lock);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100571 /*
572 * Check for races, another CPU might have split this page
573 * up for us already:
574 */
575 tmp = lookup_address(address, &level);
Wen Congyangae9aae92013-02-22 16:33:04 -0800576 if (tmp != kpte) {
577 spin_unlock(&pgd_lock);
578 return 1;
579 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100580
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -0700581 paravirt_alloc_pte(&init_mm, page_to_pfn(base));
Thomas Gleixner07cf89c2008-02-04 16:48:08 +0100582 ref_prot = pte_pgprot(pte_clrhuge(*kpte));
Ingo Molnar7a5714e2009-02-20 17:44:21 +0100583 /*
584 * If we ever want to utilize the PAT bit, we need to
585 * update this function to make sure it's converted from
586 * bit 12 to bit 7 when we cross from the 2MB level to
587 * the 4K level:
588 */
589 WARN_ON_ONCE(pgprot_val(ref_prot) & _PAGE_PAT_LARGE);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100590
Andi Kleenf07333f2008-02-04 16:48:09 +0100591#ifdef CONFIG_X86_64
592 if (level == PG_LEVEL_1G) {
593 pfninc = PMD_PAGE_SIZE >> PAGE_SHIFT;
Andrea Arcangelia8aed3e2013-02-22 15:11:51 -0800594 /*
595 * Set the PSE flags only if the PRESENT flag is set
596 * otherwise pmd_present/pmd_huge will return true
597 * even on a non present pmd.
598 */
599 if (pgprot_val(ref_prot) & _PAGE_PRESENT)
600 pgprot_val(ref_prot) |= _PAGE_PSE;
601 else
602 pgprot_val(ref_prot) &= ~_PAGE_PSE;
Andi Kleenf07333f2008-02-04 16:48:09 +0100603 }
604#endif
605
Thomas Gleixner63c1dcf2008-02-04 16:48:05 +0100606 /*
Andrea Arcangelia8aed3e2013-02-22 15:11:51 -0800607 * Set the GLOBAL flags only if the PRESENT flag is set
608 * otherwise pmd/pte_present will return true even on a non
609 * present pmd/pte. The canon_pgprot will clear _PAGE_GLOBAL
610 * for the ancient hardware that doesn't support it.
611 */
612 if (pgprot_val(ref_prot) & _PAGE_PRESENT)
613 pgprot_val(ref_prot) |= _PAGE_GLOBAL;
614 else
615 pgprot_val(ref_prot) &= ~_PAGE_GLOBAL;
616
617 /*
Thomas Gleixner63c1dcf2008-02-04 16:48:05 +0100618 * Get the target pfn from the original entry:
619 */
620 pfn = pte_pfn(*kpte);
Andi Kleenf07333f2008-02-04 16:48:09 +0100621 for (i = 0; i < PTRS_PER_PTE; i++, pfn += pfninc)
Andrea Arcangelia8aed3e2013-02-22 15:11:51 -0800622 set_pte(&pbase[i], pfn_pte(pfn, canon_pgprot(ref_prot)));
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100623
Yinghai Lu8eb57792012-11-16 19:38:49 -0800624 if (pfn_range_is_mapped(PFN_DOWN(__pa(address)),
625 PFN_DOWN(__pa(address)) + 1))
Yinghai Luf361a452008-07-10 20:38:26 -0700626 split_page_count(level);
627
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100628 /*
Ingo Molnar07a66d72009-02-20 08:04:13 +0100629 * Install the new, split up pagetable.
Huang, Ying4c881ca2008-01-30 13:34:04 +0100630 *
Ingo Molnar07a66d72009-02-20 08:04:13 +0100631 * We use the standard kernel pagetable protections for the new
632 * pagetable protections, the actual ptes set above control the
633 * primary protection behavior:
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100634 */
Ingo Molnar07a66d72009-02-20 08:04:13 +0100635 __set_pmd_pte(kpte, address, mk_pte(base, __pgprot(_KERNPG_TABLE)));
Ingo Molnar211b3d02009-03-10 22:31:03 +0100636
637 /*
638 * Intel Atom errata AAH41 workaround.
639 *
640 * The real fix should be in hw or in a microcode update, but
641 * we also probabilistically try to reduce the window of having
642 * a large TLB mixed with 4K TLBs while instruction fetches are
643 * going on.
644 */
645 __flush_tlb_all();
Andrea Arcangelia79e53d2011-02-16 15:45:22 -0800646 spin_unlock(&pgd_lock);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100647
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100648 return 0;
649}
650
Wen Congyangae9aae92013-02-22 16:33:04 -0800651static int split_large_page(pte_t *kpte, unsigned long address)
652{
Wen Congyangae9aae92013-02-22 16:33:04 -0800653 struct page *base;
654
655 if (!debug_pagealloc)
656 spin_unlock(&cpa_lock);
657 base = alloc_pages(GFP_KERNEL | __GFP_NOTRACK, 0);
658 if (!debug_pagealloc)
659 spin_lock(&cpa_lock);
660 if (!base)
661 return -ENOMEM;
662
Borislav Petkov59528862013-03-21 18:16:57 +0100663 if (__split_large_page(kpte, address, base))
Wen Congyangae9aae92013-02-22 16:33:04 -0800664 __free_page(base);
665
666 return 0;
667}
668
Suresh Siddhaa1e46212009-01-20 14:20:21 -0800669static int __cpa_process_fault(struct cpa_data *cpa, unsigned long vaddr,
670 int primary)
671{
672 /*
673 * Ignore all non primary paths.
674 */
675 if (!primary)
676 return 0;
677
678 /*
679 * Ignore the NULL PTE for kernel identity mapping, as it is expected
680 * to have holes.
681 * Also set numpages to '1' indicating that we processed cpa req for
682 * one virtual address page and its pfn. TBD: numpages can be set based
683 * on the initial value and the level returned by lookup_address().
684 */
685 if (within(vaddr, PAGE_OFFSET,
686 PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT))) {
687 cpa->numpages = 1;
688 cpa->pfn = __pa(vaddr) >> PAGE_SHIFT;
689 return 0;
690 } else {
691 WARN(1, KERN_WARNING "CPA: called for zero pte. "
692 "vaddr = %lx cpa->vaddr = %lx\n", vaddr,
693 *cpa->vaddr);
694
695 return -EFAULT;
696 }
697}
698
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100699static int __change_page_attr(struct cpa_data *cpa, int primary)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100700{
Shaohua Lid75586a2008-08-21 10:46:06 +0800701 unsigned long address;
Harvey Harrisonda7bfc52008-02-09 23:24:08 +0100702 int do_split, err;
703 unsigned int level;
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100704 pte_t *kpte, old_pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Thomas Hellstrom8523acf2009-08-03 09:25:45 +0200706 if (cpa->flags & CPA_PAGES_ARRAY) {
707 struct page *page = cpa->pages[cpa->curpage];
708 if (unlikely(PageHighMem(page)))
709 return 0;
710 address = (unsigned long)page_address(page);
711 } else if (cpa->flags & CPA_ARRAY)
Shaohua Lid75586a2008-08-21 10:46:06 +0800712 address = cpa->vaddr[cpa->curpage];
713 else
714 address = *cpa->vaddr;
Ingo Molnar97f99fe2008-01-30 13:33:55 +0100715repeat:
Ingo Molnarf0646e42008-01-30 13:33:43 +0100716 kpte = lookup_address(address, &level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if (!kpte)
Suresh Siddhaa1e46212009-01-20 14:20:21 -0800718 return __cpa_process_fault(cpa, address, primary);
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100719
720 old_pte = *kpte;
Suresh Siddhaa1e46212009-01-20 14:20:21 -0800721 if (!pte_val(old_pte))
722 return __cpa_process_fault(cpa, address, primary);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100723
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100724 if (level == PG_LEVEL_4K) {
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100725 pte_t new_pte;
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100726 pgprot_t new_prot = pte_pgprot(old_pte);
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100727 unsigned long pfn = pte_pfn(old_pte);
Thomas Gleixnera72a08a2008-01-30 13:34:07 +0100728
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100729 pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
730 pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
Ingo Molnar86f03982008-01-30 13:34:09 +0100731
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100732 new_prot = static_protections(new_prot, address, pfn);
Ingo Molnar86f03982008-01-30 13:34:09 +0100733
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100734 /*
Andrea Arcangelia8aed3e2013-02-22 15:11:51 -0800735 * Set the GLOBAL flags only if the PRESENT flag is
736 * set otherwise pte_present will return true even on
737 * a non present pte. The canon_pgprot will clear
738 * _PAGE_GLOBAL for the ancient hardware that doesn't
739 * support it.
740 */
741 if (pgprot_val(new_prot) & _PAGE_PRESENT)
742 pgprot_val(new_prot) |= _PAGE_GLOBAL;
743 else
744 pgprot_val(new_prot) &= ~_PAGE_GLOBAL;
745
746 /*
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100747 * We need to keep the pfn from the existing PTE,
748 * after all we're only going to change it's attributes
749 * not the memory it points to
750 */
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100751 new_pte = pfn_pte(pfn, canon_pgprot(new_prot));
752 cpa->pfn = pfn;
Thomas Gleixnerf4ae5da2008-02-04 16:48:07 +0100753 /*
754 * Do we really change anything ?
755 */
756 if (pte_val(old_pte) != pte_val(new_pte)) {
757 set_pte_atomic(kpte, new_pte);
Shaohua Lid75586a2008-08-21 10:46:06 +0800758 cpa->flags |= CPA_FLUSHTLB;
Thomas Gleixnerf4ae5da2008-02-04 16:48:07 +0100759 }
Rafael J. Wysocki9b5cf482008-03-03 01:17:37 +0100760 cpa->numpages = 1;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100761 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100763
764 /*
765 * Check, whether we can keep the large page intact
766 * and just change the pte:
767 */
Ingo Molnarbeaff632008-02-04 16:48:09 +0100768 do_split = try_preserve_large_page(kpte, address, cpa);
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100769 /*
770 * When the range fits into the existing large page,
Rafael J. Wysocki9b5cf482008-03-03 01:17:37 +0100771 * return. cp->numpages and cpa->tlbflush have been updated in
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100772 * try_large_page:
773 */
Ingo Molnar87f7f8f2008-02-04 16:48:10 +0100774 if (do_split <= 0)
775 return do_split;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100776
777 /*
778 * We have to split the large page:
779 */
Ingo Molnar87f7f8f2008-02-04 16:48:10 +0100780 err = split_large_page(kpte, address);
781 if (!err) {
Suresh Siddhaad5ca552008-09-23 14:00:42 -0700782 /*
783 * Do a global flush tlb after splitting the large page
784 * and before we do the actual change page attribute in the PTE.
785 *
786 * With out this, we violate the TLB application note, that says
787 * "The TLBs may contain both ordinary and large-page
788 * translations for a 4-KByte range of linear addresses. This
789 * may occur if software modifies the paging structures so that
790 * the page size used for the address range changes. If the two
791 * translations differ with respect to page frame or attributes
792 * (e.g., permissions), processor behavior is undefined and may
793 * be implementation-specific."
794 *
795 * We do this global tlb flush inside the cpa_lock, so that we
796 * don't allow any other cpu, with stale tlb entries change the
797 * page attribute in parallel, that also falls into the
798 * just split large page entry.
799 */
800 flush_tlb_all();
Ingo Molnar87f7f8f2008-02-04 16:48:10 +0100801 goto repeat;
802 }
Ingo Molnarbeaff632008-02-04 16:48:09 +0100803
Ingo Molnar87f7f8f2008-02-04 16:48:10 +0100804 return err;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100805}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100807static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias);
808
809static int cpa_process_alias(struct cpa_data *cpa)
Ingo Molnar44af6c42008-01-30 13:34:03 +0100810{
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100811 struct cpa_data alias_cpa;
Tejun Heo992f4c12009-06-22 11:56:24 +0900812 unsigned long laddr = (unsigned long)__va(cpa->pfn << PAGE_SHIFT);
Tejun Heoe933a732009-08-14 15:00:53 +0900813 unsigned long vaddr;
Tejun Heo992f4c12009-06-22 11:56:24 +0900814 int ret;
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100815
Yinghai Lu8eb57792012-11-16 19:38:49 -0800816 if (!pfn_range_is_mapped(cpa->pfn, cpa->pfn + 1))
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100817 return 0;
818
Thomas Gleixnerf34b4392008-02-15 22:17:57 +0100819 /*
820 * No need to redo, when the primary call touched the direct
821 * mapping already:
822 */
Thomas Hellstrom8523acf2009-08-03 09:25:45 +0200823 if (cpa->flags & CPA_PAGES_ARRAY) {
824 struct page *page = cpa->pages[cpa->curpage];
825 if (unlikely(PageHighMem(page)))
826 return 0;
827 vaddr = (unsigned long)page_address(page);
828 } else if (cpa->flags & CPA_ARRAY)
Shaohua Lid75586a2008-08-21 10:46:06 +0800829 vaddr = cpa->vaddr[cpa->curpage];
830 else
831 vaddr = *cpa->vaddr;
832
833 if (!(within(vaddr, PAGE_OFFSET,
Suresh Siddhaa1e46212009-01-20 14:20:21 -0800834 PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT)))) {
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100835
Thomas Gleixnerf34b4392008-02-15 22:17:57 +0100836 alias_cpa = *cpa;
Tejun Heo992f4c12009-06-22 11:56:24 +0900837 alias_cpa.vaddr = &laddr;
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700838 alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
Shaohua Lid75586a2008-08-21 10:46:06 +0800839
Thomas Gleixnerf34b4392008-02-15 22:17:57 +0100840 ret = __change_page_attr_set_clr(&alias_cpa, 0);
Tejun Heo992f4c12009-06-22 11:56:24 +0900841 if (ret)
842 return ret;
Thomas Gleixnerf34b4392008-02-15 22:17:57 +0100843 }
Ingo Molnar44af6c42008-01-30 13:34:03 +0100844
Arjan van de Ven488fd992008-01-30 13:34:07 +0100845#ifdef CONFIG_X86_64
Thomas Gleixner08797502008-01-30 13:34:09 +0100846 /*
Tejun Heo992f4c12009-06-22 11:56:24 +0900847 * If the primary call didn't touch the high mapping already
848 * and the physical address is inside the kernel map, we need
Thomas Gleixner08797502008-01-30 13:34:09 +0100849 * to touch the high mapped kernel as well:
850 */
Tejun Heo992f4c12009-06-22 11:56:24 +0900851 if (!within(vaddr, (unsigned long)_text, _brk_end) &&
852 within(cpa->pfn, highmap_start_pfn(), highmap_end_pfn())) {
853 unsigned long temp_cpa_vaddr = (cpa->pfn << PAGE_SHIFT) +
854 __START_KERNEL_map - phys_base;
855 alias_cpa = *cpa;
856 alias_cpa.vaddr = &temp_cpa_vaddr;
857 alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
Thomas Gleixner08797502008-01-30 13:34:09 +0100858
Tejun Heo992f4c12009-06-22 11:56:24 +0900859 /*
860 * The high mapping range is imprecise, so ignore the
861 * return value.
862 */
863 __change_page_attr_set_clr(&alias_cpa, 0);
864 }
Thomas Gleixner08797502008-01-30 13:34:09 +0100865#endif
Tejun Heo992f4c12009-06-22 11:56:24 +0900866
867 return 0;
Ingo Molnar44af6c42008-01-30 13:34:03 +0100868}
869
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100870static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias)
Thomas Gleixnerff314522008-01-30 13:34:08 +0100871{
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100872 int ret, numpages = cpa->numpages;
Thomas Gleixnerff314522008-01-30 13:34:08 +0100873
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100874 while (numpages) {
875 /*
876 * Store the remaining nr of pages for the large page
877 * preservation check.
878 */
Rafael J. Wysocki9b5cf482008-03-03 01:17:37 +0100879 cpa->numpages = numpages;
Shaohua Lid75586a2008-08-21 10:46:06 +0800880 /* for array changes, we can't use large page */
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700881 if (cpa->flags & (CPA_ARRAY | CPA_PAGES_ARRAY))
Shaohua Lid75586a2008-08-21 10:46:06 +0800882 cpa->numpages = 1;
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100883
Suresh Siddhaad5ca552008-09-23 14:00:42 -0700884 if (!debug_pagealloc)
885 spin_lock(&cpa_lock);
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100886 ret = __change_page_attr(cpa, checkalias);
Suresh Siddhaad5ca552008-09-23 14:00:42 -0700887 if (!debug_pagealloc)
888 spin_unlock(&cpa_lock);
Thomas Gleixnerff314522008-01-30 13:34:08 +0100889 if (ret)
890 return ret;
Thomas Gleixnerff314522008-01-30 13:34:08 +0100891
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100892 if (checkalias) {
893 ret = cpa_process_alias(cpa);
894 if (ret)
895 return ret;
896 }
897
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100898 /*
899 * Adjust the number of pages with the result of the
900 * CPA operation. Either a large page has been
901 * preserved or a single page update happened.
902 */
Rafael J. Wysocki9b5cf482008-03-03 01:17:37 +0100903 BUG_ON(cpa->numpages > numpages);
904 numpages -= cpa->numpages;
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700905 if (cpa->flags & (CPA_PAGES_ARRAY | CPA_ARRAY))
Shaohua Lid75586a2008-08-21 10:46:06 +0800906 cpa->curpage++;
907 else
908 *cpa->vaddr += cpa->numpages * PAGE_SIZE;
909
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100910 }
Thomas Gleixnerff314522008-01-30 13:34:08 +0100911 return 0;
912}
913
Andi Kleen6bb83832008-02-04 16:48:06 +0100914static inline int cache_attr(pgprot_t attr)
915{
916 return pgprot_val(attr) &
917 (_PAGE_PAT | _PAGE_PAT_LARGE | _PAGE_PWT | _PAGE_PCD);
918}
919
Shaohua Lid75586a2008-08-21 10:46:06 +0800920static int change_page_attr_set_clr(unsigned long *addr, int numpages,
Andi Kleenc9caa022008-03-12 03:53:29 +0100921 pgprot_t mask_set, pgprot_t mask_clr,
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700922 int force_split, int in_flag,
923 struct page **pages)
Thomas Gleixnerff314522008-01-30 13:34:08 +0100924{
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100925 struct cpa_data cpa;
Ingo Molnarcacf8902008-08-21 13:46:33 +0200926 int ret, cache, checkalias;
Jack Steinerfa526d02009-09-03 12:56:02 -0500927 unsigned long baddr = 0;
Thomas Gleixner331e4062008-02-04 16:48:06 +0100928
929 /*
930 * Check, if we are requested to change a not supported
931 * feature:
932 */
933 mask_set = canon_pgprot(mask_set);
934 mask_clr = canon_pgprot(mask_clr);
Andi Kleenc9caa022008-03-12 03:53:29 +0100935 if (!pgprot_val(mask_set) && !pgprot_val(mask_clr) && !force_split)
Thomas Gleixner331e4062008-02-04 16:48:06 +0100936 return 0;
937
Thomas Gleixner69b14152008-02-13 11:04:50 +0100938 /* Ensure we are PAGE_SIZE aligned */
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700939 if (in_flag & CPA_ARRAY) {
Shaohua Lid75586a2008-08-21 10:46:06 +0800940 int i;
941 for (i = 0; i < numpages; i++) {
942 if (addr[i] & ~PAGE_MASK) {
943 addr[i] &= PAGE_MASK;
944 WARN_ON_ONCE(1);
945 }
946 }
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700947 } else if (!(in_flag & CPA_PAGES_ARRAY)) {
948 /*
949 * in_flag of CPA_PAGES_ARRAY implies it is aligned.
950 * No need to cehck in that case
951 */
952 if (*addr & ~PAGE_MASK) {
953 *addr &= PAGE_MASK;
954 /*
955 * People should not be passing in unaligned addresses:
956 */
957 WARN_ON_ONCE(1);
958 }
Jack Steinerfa526d02009-09-03 12:56:02 -0500959 /*
960 * Save address for cache flush. *addr is modified in the call
961 * to __change_page_attr_set_clr() below.
962 */
963 baddr = *addr;
Thomas Gleixner69b14152008-02-13 11:04:50 +0100964 }
965
Nick Piggin5843d9a2008-08-01 03:15:21 +0200966 /* Must avoid aliasing mappings in the highmem code */
967 kmap_flush_unused();
968
Nick Piggindb64fe02008-10-18 20:27:03 -0700969 vm_unmap_aliases();
970
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100971 cpa.vaddr = addr;
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700972 cpa.pages = pages;
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100973 cpa.numpages = numpages;
974 cpa.mask_set = mask_set;
975 cpa.mask_clr = mask_clr;
Shaohua Lid75586a2008-08-21 10:46:06 +0800976 cpa.flags = 0;
977 cpa.curpage = 0;
Andi Kleenc9caa022008-03-12 03:53:29 +0100978 cpa.force_split = force_split;
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100979
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700980 if (in_flag & (CPA_ARRAY | CPA_PAGES_ARRAY))
981 cpa.flags |= in_flag;
Shaohua Lid75586a2008-08-21 10:46:06 +0800982
Thomas Gleixneraf96e442008-02-15 21:49:46 +0100983 /* No alias checking for _NX bit modifications */
984 checkalias = (pgprot_val(mask_set) | pgprot_val(mask_clr)) != _PAGE_NX;
985
986 ret = __change_page_attr_set_clr(&cpa, checkalias);
Thomas Gleixnerff314522008-01-30 13:34:08 +0100987
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100988 /*
Thomas Gleixnerf4ae5da2008-02-04 16:48:07 +0100989 * Check whether we really changed something:
990 */
Shaohua Lid75586a2008-08-21 10:46:06 +0800991 if (!(cpa.flags & CPA_FLUSHTLB))
Shaohua Li1ac2f7d2008-08-04 14:51:24 +0800992 goto out;
Ingo Molnarcacf8902008-08-21 13:46:33 +0200993
Thomas Gleixnerf4ae5da2008-02-04 16:48:07 +0100994 /*
Andi Kleen6bb83832008-02-04 16:48:06 +0100995 * No need to flush, when we did not set any of the caching
996 * attributes:
997 */
998 cache = cache_attr(mask_set);
999
1000 /*
Thomas Gleixner57a6a462008-01-30 13:34:08 +01001001 * On success we use clflush, when the CPU supports it to
H. Peter Anvinf026cfa2012-08-14 09:53:38 -07001002 * avoid the wbindv. If the CPU does not support it and in the
1003 * error case we fall back to cpa_flush_all (which uses
1004 * wbindv):
Thomas Gleixner57a6a462008-01-30 13:34:08 +01001005 */
H. Peter Anvinf026cfa2012-08-14 09:53:38 -07001006 if (!ret && cpu_has_clflush) {
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -07001007 if (cpa.flags & (CPA_PAGES_ARRAY | CPA_ARRAY)) {
1008 cpa_flush_array(addr, numpages, cache,
1009 cpa.flags, pages);
1010 } else
Jack Steinerfa526d02009-09-03 12:56:02 -05001011 cpa_flush_range(baddr, numpages, cache);
Shaohua Lid75586a2008-08-21 10:46:06 +08001012 } else
Andi Kleen6bb83832008-02-04 16:48:06 +01001013 cpa_flush_all(cache);
Ingo Molnarcacf8902008-08-21 13:46:33 +02001014
Thomas Gleixner76ebd052008-02-09 23:24:09 +01001015out:
Thomas Gleixnerff314522008-01-30 13:34:08 +01001016 return ret;
1017}
1018
Shaohua Lid75586a2008-08-21 10:46:06 +08001019static inline int change_page_attr_set(unsigned long *addr, int numpages,
1020 pgprot_t mask, int array)
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001021{
Shaohua Lid75586a2008-08-21 10:46:06 +08001022 return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0), 0,
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -07001023 (array ? CPA_ARRAY : 0), NULL);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001024}
1025
Shaohua Lid75586a2008-08-21 10:46:06 +08001026static inline int change_page_attr_clear(unsigned long *addr, int numpages,
1027 pgprot_t mask, int array)
Thomas Gleixner72932c72008-01-30 13:34:08 +01001028{
Shaohua Lid75586a2008-08-21 10:46:06 +08001029 return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask, 0,
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -07001030 (array ? CPA_ARRAY : 0), NULL);
Thomas Gleixner72932c72008-01-30 13:34:08 +01001031}
1032
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001033static inline int cpa_set_pages_array(struct page **pages, int numpages,
1034 pgprot_t mask)
1035{
1036 return change_page_attr_set_clr(NULL, numpages, mask, __pgprot(0), 0,
1037 CPA_PAGES_ARRAY, pages);
1038}
1039
1040static inline int cpa_clear_pages_array(struct page **pages, int numpages,
1041 pgprot_t mask)
1042{
1043 return change_page_attr_set_clr(NULL, numpages, __pgprot(0), mask, 0,
1044 CPA_PAGES_ARRAY, pages);
1045}
1046
venkatesh.pallipadi@intel.com12193332008-03-18 17:00:18 -07001047int _set_memory_uc(unsigned long addr, int numpages)
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001048{
Suresh Siddhade33c442008-04-25 17:07:22 -07001049 /*
1050 * for now UC MINUS. see comments in ioremap_nocache()
1051 */
Shaohua Lid75586a2008-08-21 10:46:06 +08001052 return change_page_attr_set(&addr, numpages,
1053 __pgprot(_PAGE_CACHE_UC_MINUS), 0);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001054}
venkatesh.pallipadi@intel.com12193332008-03-18 17:00:18 -07001055
1056int set_memory_uc(unsigned long addr, int numpages)
1057{
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001058 int ret;
1059
Suresh Siddhade33c442008-04-25 17:07:22 -07001060 /*
1061 * for now UC MINUS. see comments in ioremap_nocache()
1062 */
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001063 ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
1064 _PAGE_CACHE_UC_MINUS, NULL);
1065 if (ret)
1066 goto out_err;
venkatesh.pallipadi@intel.com12193332008-03-18 17:00:18 -07001067
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001068 ret = _set_memory_uc(addr, numpages);
1069 if (ret)
1070 goto out_free;
1071
1072 return 0;
1073
1074out_free:
1075 free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
1076out_err:
1077 return ret;
venkatesh.pallipadi@intel.com12193332008-03-18 17:00:18 -07001078}
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001079EXPORT_SYMBOL(set_memory_uc);
1080
H Hartley Sweeten2d070ef2011-11-15 14:49:00 -08001081static int _set_memory_array(unsigned long *addr, int addrinarray,
Pauli Nieminen4f646252010-04-01 12:45:01 +00001082 unsigned long new_type)
Shaohua Lid75586a2008-08-21 10:46:06 +08001083{
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001084 int i, j;
1085 int ret;
1086
Shaohua Lid75586a2008-08-21 10:46:06 +08001087 /*
1088 * for now UC MINUS. see comments in ioremap_nocache()
1089 */
1090 for (i = 0; i < addrinarray; i++) {
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001091 ret = reserve_memtype(__pa(addr[i]), __pa(addr[i]) + PAGE_SIZE,
Pauli Nieminen4f646252010-04-01 12:45:01 +00001092 new_type, NULL);
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001093 if (ret)
1094 goto out_free;
Shaohua Lid75586a2008-08-21 10:46:06 +08001095 }
1096
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001097 ret = change_page_attr_set(addr, addrinarray,
Shaohua Lid75586a2008-08-21 10:46:06 +08001098 __pgprot(_PAGE_CACHE_UC_MINUS), 1);
Pauli Nieminen4f646252010-04-01 12:45:01 +00001099
1100 if (!ret && new_type == _PAGE_CACHE_WC)
1101 ret = change_page_attr_set_clr(addr, addrinarray,
1102 __pgprot(_PAGE_CACHE_WC),
1103 __pgprot(_PAGE_CACHE_MASK),
1104 0, CPA_ARRAY, NULL);
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001105 if (ret)
1106 goto out_free;
Rene Hermanc5e147c2008-08-22 01:02:20 +02001107
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001108 return 0;
1109
1110out_free:
1111 for (j = 0; j < i; j++)
1112 free_memtype(__pa(addr[j]), __pa(addr[j]) + PAGE_SIZE);
1113
1114 return ret;
Shaohua Lid75586a2008-08-21 10:46:06 +08001115}
Pauli Nieminen4f646252010-04-01 12:45:01 +00001116
1117int set_memory_array_uc(unsigned long *addr, int addrinarray)
1118{
1119 return _set_memory_array(addr, addrinarray, _PAGE_CACHE_UC_MINUS);
1120}
Shaohua Lid75586a2008-08-21 10:46:06 +08001121EXPORT_SYMBOL(set_memory_array_uc);
1122
Pauli Nieminen4f646252010-04-01 12:45:01 +00001123int set_memory_array_wc(unsigned long *addr, int addrinarray)
1124{
1125 return _set_memory_array(addr, addrinarray, _PAGE_CACHE_WC);
1126}
1127EXPORT_SYMBOL(set_memory_array_wc);
1128
venkatesh.pallipadi@intel.comef354af2008-03-18 17:00:23 -07001129int _set_memory_wc(unsigned long addr, int numpages)
1130{
venkatesh.pallipadi@intel.com3869c4a2009-04-09 14:26:50 -07001131 int ret;
Pallipadi, Venkateshbdc63402009-07-30 14:43:19 -07001132 unsigned long addr_copy = addr;
1133
venkatesh.pallipadi@intel.com3869c4a2009-04-09 14:26:50 -07001134 ret = change_page_attr_set(&addr, numpages,
1135 __pgprot(_PAGE_CACHE_UC_MINUS), 0);
venkatesh.pallipadi@intel.com3869c4a2009-04-09 14:26:50 -07001136 if (!ret) {
Pallipadi, Venkateshbdc63402009-07-30 14:43:19 -07001137 ret = change_page_attr_set_clr(&addr_copy, numpages,
1138 __pgprot(_PAGE_CACHE_WC),
1139 __pgprot(_PAGE_CACHE_MASK),
1140 0, 0, NULL);
venkatesh.pallipadi@intel.com3869c4a2009-04-09 14:26:50 -07001141 }
1142 return ret;
venkatesh.pallipadi@intel.comef354af2008-03-18 17:00:23 -07001143}
1144
1145int set_memory_wc(unsigned long addr, int numpages)
1146{
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001147 int ret;
1148
Andreas Herrmann499f8f82008-06-10 16:06:21 +02001149 if (!pat_enabled)
venkatesh.pallipadi@intel.comef354af2008-03-18 17:00:23 -07001150 return set_memory_uc(addr, numpages);
1151
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001152 ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
1153 _PAGE_CACHE_WC, NULL);
1154 if (ret)
1155 goto out_err;
venkatesh.pallipadi@intel.comef354af2008-03-18 17:00:23 -07001156
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001157 ret = _set_memory_wc(addr, numpages);
1158 if (ret)
1159 goto out_free;
1160
1161 return 0;
1162
1163out_free:
1164 free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
1165out_err:
1166 return ret;
venkatesh.pallipadi@intel.comef354af2008-03-18 17:00:23 -07001167}
1168EXPORT_SYMBOL(set_memory_wc);
1169
venkatesh.pallipadi@intel.com12193332008-03-18 17:00:18 -07001170int _set_memory_wb(unsigned long addr, int numpages)
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001171{
Shaohua Lid75586a2008-08-21 10:46:06 +08001172 return change_page_attr_clear(&addr, numpages,
1173 __pgprot(_PAGE_CACHE_MASK), 0);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001174}
venkatesh.pallipadi@intel.com12193332008-03-18 17:00:18 -07001175
1176int set_memory_wb(unsigned long addr, int numpages)
1177{
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001178 int ret;
1179
1180 ret = _set_memory_wb(addr, numpages);
1181 if (ret)
1182 return ret;
1183
venkatesh.pallipadi@intel.comc15238d2008-08-20 16:45:51 -07001184 free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001185 return 0;
venkatesh.pallipadi@intel.com12193332008-03-18 17:00:18 -07001186}
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001187EXPORT_SYMBOL(set_memory_wb);
1188
Shaohua Lid75586a2008-08-21 10:46:06 +08001189int set_memory_array_wb(unsigned long *addr, int addrinarray)
1190{
1191 int i;
venkatesh.pallipadi@intel.coma5593e02009-04-09 14:26:48 -07001192 int ret;
1193
1194 ret = change_page_attr_clear(addr, addrinarray,
1195 __pgprot(_PAGE_CACHE_MASK), 1);
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001196 if (ret)
1197 return ret;
Shaohua Lid75586a2008-08-21 10:46:06 +08001198
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001199 for (i = 0; i < addrinarray; i++)
1200 free_memtype(__pa(addr[i]), __pa(addr[i]) + PAGE_SIZE);
Rene Hermanc5e147c2008-08-22 01:02:20 +02001201
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001202 return 0;
Shaohua Lid75586a2008-08-21 10:46:06 +08001203}
1204EXPORT_SYMBOL(set_memory_array_wb);
1205
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001206int set_memory_x(unsigned long addr, int numpages)
1207{
H. Peter Anvin583140a2009-11-13 15:28:15 -08001208 if (!(__supported_pte_mask & _PAGE_NX))
1209 return 0;
1210
Shaohua Lid75586a2008-08-21 10:46:06 +08001211 return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_NX), 0);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001212}
1213EXPORT_SYMBOL(set_memory_x);
1214
1215int set_memory_nx(unsigned long addr, int numpages)
1216{
H. Peter Anvin583140a2009-11-13 15:28:15 -08001217 if (!(__supported_pte_mask & _PAGE_NX))
1218 return 0;
1219
Shaohua Lid75586a2008-08-21 10:46:06 +08001220 return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_NX), 0);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001221}
1222EXPORT_SYMBOL(set_memory_nx);
1223
1224int set_memory_ro(unsigned long addr, int numpages)
1225{
Shaohua Lid75586a2008-08-21 10:46:06 +08001226 return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_RW), 0);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001227}
Bruce Allana03352d2008-09-29 20:19:22 -07001228EXPORT_SYMBOL_GPL(set_memory_ro);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001229
1230int set_memory_rw(unsigned long addr, int numpages)
1231{
Shaohua Lid75586a2008-08-21 10:46:06 +08001232 return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_RW), 0);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001233}
Bruce Allana03352d2008-09-29 20:19:22 -07001234EXPORT_SYMBOL_GPL(set_memory_rw);
Ingo Molnarf62d0f02008-01-30 13:34:07 +01001235
1236int set_memory_np(unsigned long addr, int numpages)
1237{
Shaohua Lid75586a2008-08-21 10:46:06 +08001238 return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_PRESENT), 0);
Ingo Molnarf62d0f02008-01-30 13:34:07 +01001239}
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001240
Andi Kleenc9caa022008-03-12 03:53:29 +01001241int set_memory_4k(unsigned long addr, int numpages)
1242{
Shaohua Lid75586a2008-08-21 10:46:06 +08001243 return change_page_attr_set_clr(&addr, numpages, __pgprot(0),
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -07001244 __pgprot(0), 1, 0, NULL);
Andi Kleenc9caa022008-03-12 03:53:29 +01001245}
1246
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001247int set_pages_uc(struct page *page, int numpages)
1248{
1249 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001250
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +01001251 return set_memory_uc(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001252}
1253EXPORT_SYMBOL(set_pages_uc);
1254
Pauli Nieminen4f646252010-04-01 12:45:01 +00001255static int _set_pages_array(struct page **pages, int addrinarray,
1256 unsigned long new_type)
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001257{
1258 unsigned long start;
1259 unsigned long end;
1260 int i;
1261 int free_idx;
Pauli Nieminen4f646252010-04-01 12:45:01 +00001262 int ret;
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001263
1264 for (i = 0; i < addrinarray; i++) {
Thomas Hellstrom8523acf2009-08-03 09:25:45 +02001265 if (PageHighMem(pages[i]))
1266 continue;
1267 start = page_to_pfn(pages[i]) << PAGE_SHIFT;
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001268 end = start + PAGE_SIZE;
Pauli Nieminen4f646252010-04-01 12:45:01 +00001269 if (reserve_memtype(start, end, new_type, NULL))
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001270 goto err_out;
1271 }
1272
Pauli Nieminen4f646252010-04-01 12:45:01 +00001273 ret = cpa_set_pages_array(pages, addrinarray,
1274 __pgprot(_PAGE_CACHE_UC_MINUS));
1275 if (!ret && new_type == _PAGE_CACHE_WC)
1276 ret = change_page_attr_set_clr(NULL, addrinarray,
1277 __pgprot(_PAGE_CACHE_WC),
1278 __pgprot(_PAGE_CACHE_MASK),
1279 0, CPA_PAGES_ARRAY, pages);
1280 if (ret)
1281 goto err_out;
1282 return 0; /* Success */
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001283err_out:
1284 free_idx = i;
1285 for (i = 0; i < free_idx; i++) {
Thomas Hellstrom8523acf2009-08-03 09:25:45 +02001286 if (PageHighMem(pages[i]))
1287 continue;
1288 start = page_to_pfn(pages[i]) << PAGE_SHIFT;
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001289 end = start + PAGE_SIZE;
1290 free_memtype(start, end);
1291 }
1292 return -EINVAL;
1293}
Pauli Nieminen4f646252010-04-01 12:45:01 +00001294
1295int set_pages_array_uc(struct page **pages, int addrinarray)
1296{
1297 return _set_pages_array(pages, addrinarray, _PAGE_CACHE_UC_MINUS);
1298}
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001299EXPORT_SYMBOL(set_pages_array_uc);
1300
Pauli Nieminen4f646252010-04-01 12:45:01 +00001301int set_pages_array_wc(struct page **pages, int addrinarray)
1302{
1303 return _set_pages_array(pages, addrinarray, _PAGE_CACHE_WC);
1304}
1305EXPORT_SYMBOL(set_pages_array_wc);
1306
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001307int set_pages_wb(struct page *page, int numpages)
1308{
1309 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001310
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +01001311 return set_memory_wb(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001312}
1313EXPORT_SYMBOL(set_pages_wb);
1314
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001315int set_pages_array_wb(struct page **pages, int addrinarray)
1316{
1317 int retval;
1318 unsigned long start;
1319 unsigned long end;
1320 int i;
1321
1322 retval = cpa_clear_pages_array(pages, addrinarray,
1323 __pgprot(_PAGE_CACHE_MASK));
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001324 if (retval)
1325 return retval;
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001326
1327 for (i = 0; i < addrinarray; i++) {
Thomas Hellstrom8523acf2009-08-03 09:25:45 +02001328 if (PageHighMem(pages[i]))
1329 continue;
1330 start = page_to_pfn(pages[i]) << PAGE_SHIFT;
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001331 end = start + PAGE_SIZE;
1332 free_memtype(start, end);
1333 }
1334
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001335 return 0;
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001336}
1337EXPORT_SYMBOL(set_pages_array_wb);
1338
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001339int set_pages_x(struct page *page, int numpages)
1340{
1341 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001342
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +01001343 return set_memory_x(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001344}
1345EXPORT_SYMBOL(set_pages_x);
1346
1347int set_pages_nx(struct page *page, int numpages)
1348{
1349 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001350
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +01001351 return set_memory_nx(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001352}
1353EXPORT_SYMBOL(set_pages_nx);
1354
1355int set_pages_ro(struct page *page, int numpages)
1356{
1357 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001358
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +01001359 return set_memory_ro(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001360}
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001361
1362int set_pages_rw(struct page *page, int numpages)
1363{
1364 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001365
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +01001366 return set_memory_rw(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001367}
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369#ifdef CONFIG_DEBUG_PAGEALLOC
Ingo Molnarf62d0f02008-01-30 13:34:07 +01001370
1371static int __set_pages_p(struct page *page, int numpages)
1372{
Shaohua Lid75586a2008-08-21 10:46:06 +08001373 unsigned long tempaddr = (unsigned long) page_address(page);
1374 struct cpa_data cpa = { .vaddr = &tempaddr,
Thomas Gleixner72e458d2008-02-04 16:48:07 +01001375 .numpages = numpages,
1376 .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW),
Shaohua Lid75586a2008-08-21 10:46:06 +08001377 .mask_clr = __pgprot(0),
1378 .flags = 0};
Thomas Gleixner72932c72008-01-30 13:34:08 +01001379
Suresh Siddha55121b42008-09-23 14:00:40 -07001380 /*
1381 * No alias checking needed for setting present flag. otherwise,
1382 * we may need to break large pages for 64-bit kernel text
1383 * mappings (this adds to complexity if we want to do this from
1384 * atomic context especially). Let's keep it simple!
1385 */
1386 return __change_page_attr_set_clr(&cpa, 0);
Ingo Molnarf62d0f02008-01-30 13:34:07 +01001387}
1388
1389static int __set_pages_np(struct page *page, int numpages)
1390{
Shaohua Lid75586a2008-08-21 10:46:06 +08001391 unsigned long tempaddr = (unsigned long) page_address(page);
1392 struct cpa_data cpa = { .vaddr = &tempaddr,
Thomas Gleixner72e458d2008-02-04 16:48:07 +01001393 .numpages = numpages,
1394 .mask_set = __pgprot(0),
Shaohua Lid75586a2008-08-21 10:46:06 +08001395 .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW),
1396 .flags = 0};
Thomas Gleixner72932c72008-01-30 13:34:08 +01001397
Suresh Siddha55121b42008-09-23 14:00:40 -07001398 /*
1399 * No alias checking needed for setting not present flag. otherwise,
1400 * we may need to break large pages for 64-bit kernel text
1401 * mappings (this adds to complexity if we want to do this from
1402 * atomic context especially). Let's keep it simple!
1403 */
1404 return __change_page_attr_set_clr(&cpa, 0);
Ingo Molnarf62d0f02008-01-30 13:34:07 +01001405}
1406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407void kernel_map_pages(struct page *page, int numpages, int enable)
1408{
1409 if (PageHighMem(page))
1410 return;
Ingo Molnar9f4c8152008-01-30 13:33:41 +01001411 if (!enable) {
Ingo Molnarf9b84042006-06-27 02:54:49 -07001412 debug_check_no_locks_freed(page_address(page),
1413 numpages * PAGE_SIZE);
Ingo Molnar9f4c8152008-01-30 13:33:41 +01001414 }
Ingo Molnarde5097c2006-01-09 15:59:21 -08001415
Ingo Molnar9f4c8152008-01-30 13:33:41 +01001416 /*
Ingo Molnarf8d84062008-02-13 14:09:53 +01001417 * The return value is ignored as the calls cannot fail.
Suresh Siddha55121b42008-09-23 14:00:40 -07001418 * Large pages for identity mappings are not used at boot time
1419 * and hence no memory allocations during large page split.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 */
Ingo Molnarf62d0f02008-01-30 13:34:07 +01001421 if (enable)
1422 __set_pages_p(page, numpages);
1423 else
1424 __set_pages_np(page, numpages);
Ingo Molnar9f4c8152008-01-30 13:33:41 +01001425
1426 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +01001427 * We should perform an IPI and flush all tlbs,
1428 * but that can deadlock->flush only current cpu:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 */
1430 __flush_tlb_all();
Boris Ostrovsky26564602013-04-11 13:59:52 -04001431
1432 arch_flush_lazy_mmu_mode();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433}
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +01001434
1435#ifdef CONFIG_HIBERNATION
1436
1437bool kernel_page_present(struct page *page)
1438{
1439 unsigned int level;
1440 pte_t *pte;
1441
1442 if (PageHighMem(page))
1443 return false;
1444
1445 pte = lookup_address((unsigned long)page_address(page), &level);
1446 return (pte_val(*pte) & _PAGE_PRESENT);
1447}
1448
1449#endif /* CONFIG_HIBERNATION */
1450
1451#endif /* CONFIG_DEBUG_PAGEALLOC */
Arjan van de Vend1028a12008-01-30 13:34:07 +01001452
1453/*
1454 * The testcases use internal knowledge of the implementation that shouldn't
1455 * be exposed to the rest of the kernel. Include these directly here.
1456 */
1457#ifdef CONFIG_CPA_DEBUG
1458#include "pageattr-test.c"
1459#endif