blob: d343b3c81f3c1e79a7a4f21eb51dc4672f7a1c9b [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;
Thomas Gleixner72e458d2008-02-04 16:48:07 +010033 pgprot_t mask_set;
34 pgprot_t mask_clr;
Thomas Gleixner65e074d2008-02-04 16:48:07 +010035 int numpages;
Shaohua Lid75586a2008-08-21 10:46:06 +080036 int flags;
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +010037 unsigned long pfn;
Andi Kleenc9caa022008-03-12 03:53:29 +010038 unsigned force_split : 1;
Shaohua Lid75586a2008-08-21 10:46:06 +080039 int curpage;
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -070040 struct page **pages;
Thomas Gleixner72e458d2008-02-04 16:48:07 +010041};
42
Suresh Siddhaad5ca552008-09-23 14:00:42 -070043/*
44 * Serialize cpa() (for !DEBUG_PAGEALLOC which uses large identity mappings)
45 * using cpa_lock. So that we don't allow any other cpu, with stale large tlb
46 * entries change the page attribute in parallel to some other cpu
47 * splitting a large page entry along with changing the attribute.
48 */
49static DEFINE_SPINLOCK(cpa_lock);
50
Shaohua Lid75586a2008-08-21 10:46:06 +080051#define CPA_FLUSHTLB 1
52#define CPA_ARRAY 2
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -070053#define CPA_PAGES_ARRAY 4
Shaohua Lid75586a2008-08-21 10:46:06 +080054
Thomas Gleixner65280e62008-05-05 16:35:21 +020055#ifdef CONFIG_PROC_FS
Andi Kleence0c0e52008-05-02 11:46:49 +020056static unsigned long direct_pages_count[PG_LEVEL_NUM];
57
Thomas Gleixner65280e62008-05-05 16:35:21 +020058void update_page_count(int level, unsigned long pages)
Andi Kleence0c0e52008-05-02 11:46:49 +020059{
Andi Kleence0c0e52008-05-02 11:46:49 +020060 unsigned long flags;
Thomas Gleixner65280e62008-05-05 16:35:21 +020061
Andi Kleence0c0e52008-05-02 11:46:49 +020062 /* Protect against CPA */
63 spin_lock_irqsave(&pgd_lock, flags);
64 direct_pages_count[level] += pages;
65 spin_unlock_irqrestore(&pgd_lock, flags);
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
Thomas Gleixner65280e62008-05-05 16:35:21 +020085#ifdef CONFIG_X86_64
Hugh Dickinsa06de632008-08-15 13:58:32 +010086 if (direct_gbpages)
Hugh Dickinsb9c3bfc2008-11-06 12:05:40 +000087 seq_printf(m, "DirectMap1G: %8lu kB\n",
Hugh Dickinsa06de632008-08-15 13:58:32 +010088 direct_pages_count[PG_LEVEL_1G] << 20);
Thomas Gleixner65280e62008-05-05 16:35:21 +020089#endif
Thomas Gleixner65280e62008-05-05 16:35:21 +020090}
91#else
92static inline void split_page_count(int level) { }
93#endif
94
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +010095#ifdef CONFIG_X86_64
96
97static inline unsigned long highmap_start_pfn(void)
98{
99 return __pa(_text) >> PAGE_SHIFT;
100}
101
102static inline unsigned long highmap_end_pfn(void)
103{
Jeremy Fitzhardinge93dbda72009-02-26 17:35:44 -0800104 return __pa(roundup(_brk_end, PMD_SIZE)) >> PAGE_SHIFT;
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100105}
106
107#endif
108
Ingo Molnar92cb54a2008-02-13 14:37:52 +0100109#ifdef CONFIG_DEBUG_PAGEALLOC
110# define debug_pagealloc 1
111#else
112# define debug_pagealloc 0
113#endif
114
Arjan van de Vened724be2008-01-30 13:34:04 +0100115static inline int
116within(unsigned long addr, unsigned long start, unsigned long end)
Ingo Molnar687c4822008-01-30 13:34:04 +0100117{
Arjan van de Vened724be2008-01-30 13:34:04 +0100118 return addr >= start && addr < end;
119}
120
121/*
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100122 * Flushing functions
123 */
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +0100124
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +0100125/**
126 * clflush_cache_range - flush a cache range with clflush
127 * @addr: virtual start address
128 * @size: number of bytes to flush
129 *
130 * clflush is an unordered instruction which needs fencing with mfence
131 * to avoid ordering issues.
132 */
Ingo Molnar4c61afc2008-01-30 13:34:09 +0100133void clflush_cache_range(void *vaddr, unsigned int size)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100134{
Ingo Molnar4c61afc2008-01-30 13:34:09 +0100135 void *vend = vaddr + size - 1;
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
139 for (; vaddr < vend; vaddr += boot_cpu_data.x86_clflush_size)
140 clflush(vaddr);
141 /*
142 * Flush any possible final partial cacheline:
143 */
144 clflush(vend);
145
Thomas Gleixnercd8ddf12008-01-30 13:34:08 +0100146 mb();
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100147}
Eric Anholte517a5e2009-09-10 17:48:48 -0700148EXPORT_SYMBOL_GPL(clflush_cache_range);
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100149
Thomas Gleixneraf1e6842008-01-30 13:34:08 +0100150static void __cpa_flush_all(void *arg)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100151{
Andi Kleen6bb83832008-02-04 16:48:06 +0100152 unsigned long cache = (unsigned long)arg;
153
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100154 /*
155 * Flush all to work around Errata in early athlons regarding
156 * large page flushing.
157 */
158 __flush_tlb_all();
159
venkatesh.pallipadi@intel.com0b827532009-05-22 13:23:37 -0700160 if (cache && boot_cpu_data.x86 >= 4)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100161 wbinvd();
162}
163
Andi Kleen6bb83832008-02-04 16:48:06 +0100164static void cpa_flush_all(unsigned long cache)
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100165{
166 BUG_ON(irqs_disabled());
167
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200168 on_each_cpu(__cpa_flush_all, (void *) cache, 1);
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100169}
170
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100171static void __cpa_flush_range(void *arg)
172{
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100173 /*
174 * We could optimize that further and do individual per page
175 * tlb invalidates for a low number of pages. Caveat: we must
176 * flush the high aliases on 64bit as well.
177 */
178 __flush_tlb_all();
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100179}
180
Andi Kleen6bb83832008-02-04 16:48:06 +0100181static void cpa_flush_range(unsigned long start, int numpages, int cache)
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100182{
Ingo Molnar4c61afc2008-01-30 13:34:09 +0100183 unsigned int i, level;
184 unsigned long addr;
185
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100186 BUG_ON(irqs_disabled());
Ingo Molnar4c61afc2008-01-30 13:34:09 +0100187 WARN_ON(PAGE_ALIGN(start) != start);
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100188
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200189 on_each_cpu(__cpa_flush_range, NULL, 1);
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100190
Andi Kleen6bb83832008-02-04 16:48:06 +0100191 if (!cache)
192 return;
193
Thomas Gleixner3b233e52008-01-30 13:34:08 +0100194 /*
195 * We only need to flush on one CPU,
196 * clflush is a MESI-coherent instruction that
197 * will cause all other CPUs to flush the same
198 * cachelines:
199 */
Ingo Molnar4c61afc2008-01-30 13:34:09 +0100200 for (i = 0, addr = start; i < numpages; i++, addr += PAGE_SIZE) {
201 pte_t *pte = lookup_address(addr, &level);
202
203 /*
204 * Only flush present addresses:
205 */
Thomas Gleixner7bfb72e2008-02-04 16:48:08 +0100206 if (pte && (pte_val(*pte) & _PAGE_PRESENT))
Ingo Molnar4c61afc2008-01-30 13:34:09 +0100207 clflush_cache_range((void *) addr, PAGE_SIZE);
208 }
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100209}
210
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700211static void cpa_flush_array(unsigned long *start, int numpages, int cache,
212 int in_flags, struct page **pages)
Shaohua Lid75586a2008-08-21 10:46:06 +0800213{
214 unsigned int i, level;
Pallipadi, Venkatesh21717872009-05-26 10:33:35 -0700215 unsigned long do_wbinvd = cache && numpages >= 1024; /* 4M threshold */
Shaohua Lid75586a2008-08-21 10:46:06 +0800216
217 BUG_ON(irqs_disabled());
218
Pallipadi, Venkatesh21717872009-05-26 10:33:35 -0700219 on_each_cpu(__cpa_flush_all, (void *) do_wbinvd, 1);
Shaohua Lid75586a2008-08-21 10:46:06 +0800220
Pallipadi, Venkatesh21717872009-05-26 10:33:35 -0700221 if (!cache || do_wbinvd)
Shaohua Lid75586a2008-08-21 10:46:06 +0800222 return;
223
Shaohua Lid75586a2008-08-21 10:46:06 +0800224 /*
225 * We only need to flush on one CPU,
226 * clflush is a MESI-coherent instruction that
227 * will cause all other CPUs to flush the same
228 * cachelines:
229 */
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700230 for (i = 0; i < numpages; i++) {
231 unsigned long addr;
232 pte_t *pte;
233
234 if (in_flags & CPA_PAGES_ARRAY)
235 addr = (unsigned long)page_address(pages[i]);
236 else
237 addr = start[i];
238
239 pte = lookup_address(addr, &level);
Shaohua Lid75586a2008-08-21 10:46:06 +0800240
241 /*
242 * Only flush present addresses:
243 */
244 if (pte && (pte_val(*pte) & _PAGE_PRESENT))
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700245 clflush_cache_range((void *)addr, PAGE_SIZE);
Shaohua Lid75586a2008-08-21 10:46:06 +0800246 }
247}
248
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +0100249/*
Arjan van de Vened724be2008-01-30 13:34:04 +0100250 * Certain areas of memory on x86 require very specific protection flags,
251 * for example the BIOS area or kernel text. Callers don't always get this
252 * right (again, ioremap() on BIOS memory is not uncommon) so this function
253 * checks and fixes these known static required protection bits.
254 */
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100255static inline pgprot_t static_protections(pgprot_t prot, unsigned long address,
256 unsigned long pfn)
Arjan van de Vened724be2008-01-30 13:34:04 +0100257{
258 pgprot_t forbidden = __pgprot(0);
259
Ingo Molnar687c4822008-01-30 13:34:04 +0100260 /*
Arjan van de Vened724be2008-01-30 13:34:04 +0100261 * The BIOS area between 640k and 1Mb needs to be executable for
262 * PCI BIOS based config access (CONFIG_PCI_GOBIOS) support.
Ingo Molnar687c4822008-01-30 13:34:04 +0100263 */
Matthieu Castet5bd5a452010-11-16 22:31:26 +0100264#ifdef CONFIG_PCI_BIOS
265 if (pcibios_enabled && within(pfn, BIOS_BEGIN >> PAGE_SHIFT, BIOS_END >> PAGE_SHIFT))
Arjan van de Vened724be2008-01-30 13:34:04 +0100266 pgprot_val(forbidden) |= _PAGE_NX;
Matthieu Castet5bd5a452010-11-16 22:31:26 +0100267#endif
Arjan van de Vened724be2008-01-30 13:34:04 +0100268
269 /*
270 * The kernel text needs to be executable for obvious reasons
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100271 * Does not cover __inittext since that is gone later on. On
272 * 64bit we do not enforce !NX on the low mapping
Arjan van de Vened724be2008-01-30 13:34:04 +0100273 */
274 if (within(address, (unsigned long)_text, (unsigned long)_etext))
275 pgprot_val(forbidden) |= _PAGE_NX;
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100276
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100277 /*
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100278 * The .rodata section needs to be read-only. Using the pfn
279 * catches all aliases.
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100280 */
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100281 if (within(pfn, __pa((unsigned long)__start_rodata) >> PAGE_SHIFT,
282 __pa((unsigned long)__end_rodata) >> PAGE_SHIFT))
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100283 pgprot_val(forbidden) |= _PAGE_RW;
Arjan van de Vened724be2008-01-30 13:34:04 +0100284
Suresh Siddha55ca3cc2009-10-28 18:46:57 -0800285#if defined(CONFIG_X86_64) && defined(CONFIG_DEBUG_RODATA)
Suresh Siddha74e08172009-10-14 14:46:56 -0700286 /*
Suresh Siddha502f6602009-10-28 18:46:56 -0800287 * Once the kernel maps the text as RO (kernel_set_to_readonly is set),
288 * kernel text mappings for the large page aligned text, rodata sections
289 * will be always read-only. For the kernel identity mappings covering
290 * the holes caused by this alignment can be anything that user asks.
Suresh Siddha74e08172009-10-14 14:46:56 -0700291 *
292 * This will preserve the large page mappings for kernel text/data
293 * at no extra cost.
294 */
Suresh Siddha502f6602009-10-28 18:46:56 -0800295 if (kernel_set_to_readonly &&
296 within(address, (unsigned long)_text,
Suresh Siddha281ff332010-02-18 11:51:40 -0800297 (unsigned long)__end_rodata_hpage_align)) {
298 unsigned int level;
299
300 /*
301 * Don't enforce the !RW mapping for the kernel text mapping,
302 * if the current mapping is already using small page mapping.
303 * No need to work hard to preserve large page mappings in this
304 * case.
305 *
306 * This also fixes the Linux Xen paravirt guest boot failure
307 * (because of unexpected read-only mappings for kernel identity
308 * mappings). In this paravirt guest case, the kernel text
309 * mapping and the kernel identity mapping share the same
310 * page-table pages. Thus we can't really use different
311 * protections for the kernel text and identity mappings. Also,
312 * these shared mappings are made of small page mappings.
313 * Thus this don't enforce !RW mapping for small page kernel
314 * text mapping logic will help Linux Xen parvirt guest boot
315 * aswell.
316 */
317 if (lookup_address(address, &level) && (level != PG_LEVEL_4K))
318 pgprot_val(forbidden) |= _PAGE_RW;
319 }
Suresh Siddha74e08172009-10-14 14:46:56 -0700320#endif
321
Arjan van de Vened724be2008-01-30 13:34:04 +0100322 prot = __pgprot(pgprot_val(prot) & ~pgprot_val(forbidden));
Ingo Molnar687c4822008-01-30 13:34:04 +0100323
324 return prot;
325}
326
Thomas Gleixner9a14aef2008-02-04 16:48:07 +0100327/*
328 * Lookup the page table entry for a virtual address. Return a pointer
329 * to the entry and the level of the mapping.
330 *
331 * Note: We return pud and pmd either when the entry is marked large
332 * or when the present bit is not set. Otherwise we would return a
333 * pointer to a nonexisting mapping.
334 */
Harvey Harrisonda7bfc52008-02-09 23:24:08 +0100335pte_t *lookup_address(unsigned long address, unsigned int *level)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100336{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 pgd_t *pgd = pgd_offset_k(address);
338 pud_t *pud;
339 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100340
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100341 *level = PG_LEVEL_NONE;
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (pgd_none(*pgd))
344 return NULL;
Ingo Molnar9df84992008-02-04 16:48:09 +0100345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 pud = pud_offset(pgd, address);
347 if (pud_none(*pud))
348 return NULL;
Andi Kleenc2f71ee2008-02-04 16:48:09 +0100349
350 *level = PG_LEVEL_1G;
351 if (pud_large(*pud) || !pud_present(*pud))
352 return (pte_t *)pud;
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 pmd = pmd_offset(pud, address);
355 if (pmd_none(*pmd))
356 return NULL;
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100357
358 *level = PG_LEVEL_2M;
Thomas Gleixner9a14aef2008-02-04 16:48:07 +0100359 if (pmd_large(*pmd) || !pmd_present(*pmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return (pte_t *)pmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100362 *level = PG_LEVEL_4K;
Ingo Molnar9df84992008-02-04 16:48:09 +0100363
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100364 return pte_offset_kernel(pmd, address);
365}
Pekka Paalanen75bb8832008-05-12 21:20:56 +0200366EXPORT_SYMBOL_GPL(lookup_address);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100367
Ingo Molnar9df84992008-02-04 16:48:09 +0100368/*
369 * Set the new pmd in all the pgds we know about:
370 */
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100371static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100372{
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100373 /* change init_mm */
374 set_pte_atomic(kpte, pte);
Ingo Molnar44af6c42008-01-30 13:34:03 +0100375#ifdef CONFIG_X86_32
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100376 if (!SHARED_KERNEL_PMD) {
Ingo Molnar44af6c42008-01-30 13:34:03 +0100377 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Jeremy Fitzhardingee3ed9102008-01-30 13:34:11 +0100379 list_for_each_entry(page, &pgd_list, lru) {
Ingo Molnar44af6c42008-01-30 13:34:03 +0100380 pgd_t *pgd;
381 pud_t *pud;
382 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100383
Ingo Molnar44af6c42008-01-30 13:34:03 +0100384 pgd = (pgd_t *)page_address(page) + pgd_index(address);
385 pud = pud_offset(pgd, address);
386 pmd = pmd_offset(pud, address);
387 set_pte_atomic((pte_t *)pmd, pte);
388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
Ingo Molnar44af6c42008-01-30 13:34:03 +0100390#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
Ingo Molnar9df84992008-02-04 16:48:09 +0100393static int
394try_preserve_large_page(pte_t *kpte, unsigned long address,
395 struct cpa_data *cpa)
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100396{
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100397 unsigned long nextpage_addr, numpages, pmask, psize, flags, addr, pfn;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100398 pte_t new_pte, old_pte, *tmp;
matthieu castet64edc8e2010-11-16 22:30:27 +0100399 pgprot_t old_prot, new_prot, req_prot;
Thomas Gleixnerfac84932008-02-09 23:24:09 +0100400 int i, do_split = 1;
Harvey Harrisonda7bfc52008-02-09 23:24:08 +0100401 unsigned int level;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100402
Andi Kleenc9caa022008-03-12 03:53:29 +0100403 if (cpa->force_split)
404 return 1;
405
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100406 spin_lock_irqsave(&pgd_lock, flags);
407 /*
408 * Check for races, another CPU might have split this page
409 * up already:
410 */
411 tmp = lookup_address(address, &level);
412 if (tmp != kpte)
413 goto out_unlock;
414
415 switch (level) {
416 case PG_LEVEL_2M:
Andi Kleen31422c52008-02-04 16:48:08 +0100417 psize = PMD_PAGE_SIZE;
418 pmask = PMD_PAGE_MASK;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100419 break;
Andi Kleenf07333f2008-02-04 16:48:09 +0100420#ifdef CONFIG_X86_64
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100421 case PG_LEVEL_1G:
Andi Kleen5d3c8b22008-02-13 16:20:35 +0100422 psize = PUD_PAGE_SIZE;
423 pmask = PUD_PAGE_MASK;
Andi Kleenf07333f2008-02-04 16:48:09 +0100424 break;
425#endif
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100426 default:
Ingo Molnarbeaff632008-02-04 16:48:09 +0100427 do_split = -EINVAL;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100428 goto out_unlock;
429 }
430
431 /*
432 * Calculate the number of pages, which fit into this large
433 * page starting at address:
434 */
435 nextpage_addr = (address + psize) & pmask;
436 numpages = (nextpage_addr - address) >> PAGE_SHIFT;
Rafael J. Wysocki9b5cf482008-03-03 01:17:37 +0100437 if (numpages < cpa->numpages)
438 cpa->numpages = numpages;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100439
440 /*
441 * We are safe now. Check whether the new pgprot is the same:
442 */
443 old_pte = *kpte;
matthieu castet64edc8e2010-11-16 22:30:27 +0100444 old_prot = new_prot = req_prot = pte_pgprot(old_pte);
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100445
matthieu castet64edc8e2010-11-16 22:30:27 +0100446 pgprot_val(req_prot) &= ~pgprot_val(cpa->mask_clr);
447 pgprot_val(req_prot) |= pgprot_val(cpa->mask_set);
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100448
449 /*
450 * old_pte points to the large page base address. So we need
451 * to add the offset of the virtual address:
452 */
453 pfn = pte_pfn(old_pte) + ((address & (psize - 1)) >> PAGE_SHIFT);
454 cpa->pfn = pfn;
455
matthieu castet64edc8e2010-11-16 22:30:27 +0100456 new_prot = static_protections(req_prot, address, pfn);
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100457
458 /*
Thomas Gleixnerfac84932008-02-09 23:24:09 +0100459 * We need to check the full range, whether
460 * static_protection() requires a different pgprot for one of
461 * the pages in the range we try to preserve:
462 */
matthieu castet64edc8e2010-11-16 22:30:27 +0100463 addr = address & pmask;
464 pfn = pte_pfn(old_pte);
465 for (i = 0; i < (psize >> PAGE_SHIFT); i++, addr += PAGE_SIZE, pfn++) {
466 pgprot_t chk_prot = static_protections(req_prot, addr, pfn);
Thomas Gleixnerfac84932008-02-09 23:24:09 +0100467
468 if (pgprot_val(chk_prot) != pgprot_val(new_prot))
469 goto out_unlock;
470 }
471
472 /*
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100473 * If there are no changes, return. maxpages has been updated
474 * above:
475 */
476 if (pgprot_val(new_prot) == pgprot_val(old_prot)) {
Ingo Molnarbeaff632008-02-04 16:48:09 +0100477 do_split = 0;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100478 goto out_unlock;
479 }
480
481 /*
482 * We need to change the attributes. Check, whether we can
483 * change the large page in one go. We request a split, when
484 * the address is not aligned and the number of pages is
485 * smaller than the number of pages in the large page. Note
486 * that we limited the number of possible pages already to
487 * the number of pages in the large page.
488 */
matthieu castet64edc8e2010-11-16 22:30:27 +0100489 if (address == (address & pmask) && cpa->numpages == (psize >> PAGE_SHIFT)) {
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100490 /*
491 * The address is aligned and the number of pages
492 * covers the full page.
493 */
494 new_pte = pfn_pte(pte_pfn(old_pte), canon_pgprot(new_prot));
495 __set_pmd_pte(kpte, address, new_pte);
Shaohua Lid75586a2008-08-21 10:46:06 +0800496 cpa->flags |= CPA_FLUSHTLB;
Ingo Molnarbeaff632008-02-04 16:48:09 +0100497 do_split = 0;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100498 }
499
500out_unlock:
501 spin_unlock_irqrestore(&pgd_lock, flags);
Ingo Molnar9df84992008-02-04 16:48:09 +0100502
Ingo Molnarbeaff632008-02-04 16:48:09 +0100503 return do_split;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100504}
505
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100506static int split_large_page(pte_t *kpte, unsigned long address)
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100507{
Thomas Gleixner7b610ee2008-02-04 16:48:10 +0100508 unsigned long flags, pfn, pfninc = 1;
Ingo Molnar86f03982008-01-30 13:34:09 +0100509 unsigned int i, level;
Ingo Molnar9df84992008-02-04 16:48:09 +0100510 pte_t *pbase, *tmp;
511 pgprot_t ref_prot;
Suresh Siddhaad5ca552008-09-23 14:00:42 -0700512 struct page *base;
513
514 if (!debug_pagealloc)
515 spin_unlock(&cpa_lock);
Vegard Nossum9e730232009-02-22 11:28:25 +0100516 base = alloc_pages(GFP_KERNEL | __GFP_NOTRACK, 0);
Suresh Siddhaad5ca552008-09-23 14:00:42 -0700517 if (!debug_pagealloc)
518 spin_lock(&cpa_lock);
Suresh Siddha8311eb82008-09-23 14:00:41 -0700519 if (!base)
520 return -ENOMEM;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100521
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100522 spin_lock_irqsave(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100523 /*
524 * Check for races, another CPU might have split this page
525 * up for us already:
526 */
527 tmp = lookup_address(address, &level);
Ingo Molnar6ce9fc12008-02-04 16:48:08 +0100528 if (tmp != kpte)
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100529 goto out_unlock;
530
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100531 pbase = (pte_t *)page_address(base);
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -0700532 paravirt_alloc_pte(&init_mm, page_to_pfn(base));
Thomas Gleixner07cf89c2008-02-04 16:48:08 +0100533 ref_prot = pte_pgprot(pte_clrhuge(*kpte));
Ingo Molnar7a5714e2009-02-20 17:44:21 +0100534 /*
535 * If we ever want to utilize the PAT bit, we need to
536 * update this function to make sure it's converted from
537 * bit 12 to bit 7 when we cross from the 2MB level to
538 * the 4K level:
539 */
540 WARN_ON_ONCE(pgprot_val(ref_prot) & _PAGE_PAT_LARGE);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100541
Andi Kleenf07333f2008-02-04 16:48:09 +0100542#ifdef CONFIG_X86_64
543 if (level == PG_LEVEL_1G) {
544 pfninc = PMD_PAGE_SIZE >> PAGE_SHIFT;
545 pgprot_val(ref_prot) |= _PAGE_PSE;
Andi Kleenf07333f2008-02-04 16:48:09 +0100546 }
547#endif
548
Thomas Gleixner63c1dcf2008-02-04 16:48:05 +0100549 /*
550 * Get the target pfn from the original entry:
551 */
552 pfn = pte_pfn(*kpte);
Andi Kleenf07333f2008-02-04 16:48:09 +0100553 for (i = 0; i < PTRS_PER_PTE; i++, pfn += pfninc)
Thomas Gleixner63c1dcf2008-02-04 16:48:05 +0100554 set_pte(&pbase[i], pfn_pte(pfn, ref_prot));
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100555
Andi Kleence0c0e52008-05-02 11:46:49 +0200556 if (address >= (unsigned long)__va(0) &&
Yinghai Luf361a452008-07-10 20:38:26 -0700557 address < (unsigned long)__va(max_low_pfn_mapped << PAGE_SHIFT))
558 split_page_count(level);
559
560#ifdef CONFIG_X86_64
561 if (address >= (unsigned long)__va(1UL<<32) &&
Thomas Gleixner65280e62008-05-05 16:35:21 +0200562 address < (unsigned long)__va(max_pfn_mapped << PAGE_SHIFT))
563 split_page_count(level);
Yinghai Luf361a452008-07-10 20:38:26 -0700564#endif
Andi Kleence0c0e52008-05-02 11:46:49 +0200565
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100566 /*
Ingo Molnar07a66d72009-02-20 08:04:13 +0100567 * Install the new, split up pagetable.
Huang, Ying4c881ca2008-01-30 13:34:04 +0100568 *
Ingo Molnar07a66d72009-02-20 08:04:13 +0100569 * We use the standard kernel pagetable protections for the new
570 * pagetable protections, the actual ptes set above control the
571 * primary protection behavior:
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100572 */
Ingo Molnar07a66d72009-02-20 08:04:13 +0100573 __set_pmd_pte(kpte, address, mk_pte(base, __pgprot(_KERNPG_TABLE)));
Ingo Molnar211b3d02009-03-10 22:31:03 +0100574
575 /*
576 * Intel Atom errata AAH41 workaround.
577 *
578 * The real fix should be in hw or in a microcode update, but
579 * we also probabilistically try to reduce the window of having
580 * a large TLB mixed with 4K TLBs while instruction fetches are
581 * going on.
582 */
583 __flush_tlb_all();
584
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100585 base = NULL;
586
587out_unlock:
Thomas Gleixnereb5b5f02008-02-09 23:24:09 +0100588 /*
589 * If we dropped out via the lookup_address check under
590 * pgd_lock then stick the page back into the pool:
591 */
Suresh Siddha8311eb82008-09-23 14:00:41 -0700592 if (base)
593 __free_page(base);
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100594 spin_unlock_irqrestore(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100595
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100596 return 0;
597}
598
Suresh Siddhaa1e46212009-01-20 14:20:21 -0800599static int __cpa_process_fault(struct cpa_data *cpa, unsigned long vaddr,
600 int primary)
601{
602 /*
603 * Ignore all non primary paths.
604 */
605 if (!primary)
606 return 0;
607
608 /*
609 * Ignore the NULL PTE for kernel identity mapping, as it is expected
610 * to have holes.
611 * Also set numpages to '1' indicating that we processed cpa req for
612 * one virtual address page and its pfn. TBD: numpages can be set based
613 * on the initial value and the level returned by lookup_address().
614 */
615 if (within(vaddr, PAGE_OFFSET,
616 PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT))) {
617 cpa->numpages = 1;
618 cpa->pfn = __pa(vaddr) >> PAGE_SHIFT;
619 return 0;
620 } else {
621 WARN(1, KERN_WARNING "CPA: called for zero pte. "
622 "vaddr = %lx cpa->vaddr = %lx\n", vaddr,
623 *cpa->vaddr);
624
625 return -EFAULT;
626 }
627}
628
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100629static int __change_page_attr(struct cpa_data *cpa, int primary)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100630{
Shaohua Lid75586a2008-08-21 10:46:06 +0800631 unsigned long address;
Harvey Harrisonda7bfc52008-02-09 23:24:08 +0100632 int do_split, err;
633 unsigned int level;
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100634 pte_t *kpte, old_pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Thomas Hellstrom8523acf2009-08-03 09:25:45 +0200636 if (cpa->flags & CPA_PAGES_ARRAY) {
637 struct page *page = cpa->pages[cpa->curpage];
638 if (unlikely(PageHighMem(page)))
639 return 0;
640 address = (unsigned long)page_address(page);
641 } else if (cpa->flags & CPA_ARRAY)
Shaohua Lid75586a2008-08-21 10:46:06 +0800642 address = cpa->vaddr[cpa->curpage];
643 else
644 address = *cpa->vaddr;
Ingo Molnar97f99fe2008-01-30 13:33:55 +0100645repeat:
Ingo Molnarf0646e42008-01-30 13:33:43 +0100646 kpte = lookup_address(address, &level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (!kpte)
Suresh Siddhaa1e46212009-01-20 14:20:21 -0800648 return __cpa_process_fault(cpa, address, primary);
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100649
650 old_pte = *kpte;
Suresh Siddhaa1e46212009-01-20 14:20:21 -0800651 if (!pte_val(old_pte))
652 return __cpa_process_fault(cpa, address, primary);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100653
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100654 if (level == PG_LEVEL_4K) {
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100655 pte_t new_pte;
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100656 pgprot_t new_prot = pte_pgprot(old_pte);
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100657 unsigned long pfn = pte_pfn(old_pte);
Thomas Gleixnera72a08a2008-01-30 13:34:07 +0100658
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100659 pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
660 pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
Ingo Molnar86f03982008-01-30 13:34:09 +0100661
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100662 new_prot = static_protections(new_prot, address, pfn);
Ingo Molnar86f03982008-01-30 13:34:09 +0100663
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100664 /*
665 * We need to keep the pfn from the existing PTE,
666 * after all we're only going to change it's attributes
667 * not the memory it points to
668 */
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100669 new_pte = pfn_pte(pfn, canon_pgprot(new_prot));
670 cpa->pfn = pfn;
Thomas Gleixnerf4ae5da2008-02-04 16:48:07 +0100671 /*
672 * Do we really change anything ?
673 */
674 if (pte_val(old_pte) != pte_val(new_pte)) {
675 set_pte_atomic(kpte, new_pte);
Shaohua Lid75586a2008-08-21 10:46:06 +0800676 cpa->flags |= CPA_FLUSHTLB;
Thomas Gleixnerf4ae5da2008-02-04 16:48:07 +0100677 }
Rafael J. Wysocki9b5cf482008-03-03 01:17:37 +0100678 cpa->numpages = 1;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100679 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100681
682 /*
683 * Check, whether we can keep the large page intact
684 * and just change the pte:
685 */
Ingo Molnarbeaff632008-02-04 16:48:09 +0100686 do_split = try_preserve_large_page(kpte, address, cpa);
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100687 /*
688 * When the range fits into the existing large page,
Rafael J. Wysocki9b5cf482008-03-03 01:17:37 +0100689 * return. cp->numpages and cpa->tlbflush have been updated in
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100690 * try_large_page:
691 */
Ingo Molnar87f7f8f2008-02-04 16:48:10 +0100692 if (do_split <= 0)
693 return do_split;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100694
695 /*
696 * We have to split the large page:
697 */
Ingo Molnar87f7f8f2008-02-04 16:48:10 +0100698 err = split_large_page(kpte, address);
699 if (!err) {
Suresh Siddhaad5ca552008-09-23 14:00:42 -0700700 /*
701 * Do a global flush tlb after splitting the large page
702 * and before we do the actual change page attribute in the PTE.
703 *
704 * With out this, we violate the TLB application note, that says
705 * "The TLBs may contain both ordinary and large-page
706 * translations for a 4-KByte range of linear addresses. This
707 * may occur if software modifies the paging structures so that
708 * the page size used for the address range changes. If the two
709 * translations differ with respect to page frame or attributes
710 * (e.g., permissions), processor behavior is undefined and may
711 * be implementation-specific."
712 *
713 * We do this global tlb flush inside the cpa_lock, so that we
714 * don't allow any other cpu, with stale tlb entries change the
715 * page attribute in parallel, that also falls into the
716 * just split large page entry.
717 */
718 flush_tlb_all();
Ingo Molnar87f7f8f2008-02-04 16:48:10 +0100719 goto repeat;
720 }
Ingo Molnarbeaff632008-02-04 16:48:09 +0100721
Ingo Molnar87f7f8f2008-02-04 16:48:10 +0100722 return err;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100723}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100725static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias);
726
727static int cpa_process_alias(struct cpa_data *cpa)
Ingo Molnar44af6c42008-01-30 13:34:03 +0100728{
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100729 struct cpa_data alias_cpa;
Tejun Heo992f4c12009-06-22 11:56:24 +0900730 unsigned long laddr = (unsigned long)__va(cpa->pfn << PAGE_SHIFT);
Tejun Heoe933a732009-08-14 15:00:53 +0900731 unsigned long vaddr;
Tejun Heo992f4c12009-06-22 11:56:24 +0900732 int ret;
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100733
Yinghai Lu965194c2008-07-12 14:31:28 -0700734 if (cpa->pfn >= max_pfn_mapped)
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100735 return 0;
736
Yinghai Luf361a452008-07-10 20:38:26 -0700737#ifdef CONFIG_X86_64
Yinghai Lu965194c2008-07-12 14:31:28 -0700738 if (cpa->pfn >= max_low_pfn_mapped && cpa->pfn < (1UL<<(32-PAGE_SHIFT)))
Yinghai Luf361a452008-07-10 20:38:26 -0700739 return 0;
740#endif
Thomas Gleixnerf34b4392008-02-15 22:17:57 +0100741 /*
742 * No need to redo, when the primary call touched the direct
743 * mapping already:
744 */
Thomas Hellstrom8523acf2009-08-03 09:25:45 +0200745 if (cpa->flags & CPA_PAGES_ARRAY) {
746 struct page *page = cpa->pages[cpa->curpage];
747 if (unlikely(PageHighMem(page)))
748 return 0;
749 vaddr = (unsigned long)page_address(page);
750 } else if (cpa->flags & CPA_ARRAY)
Shaohua Lid75586a2008-08-21 10:46:06 +0800751 vaddr = cpa->vaddr[cpa->curpage];
752 else
753 vaddr = *cpa->vaddr;
754
755 if (!(within(vaddr, PAGE_OFFSET,
Suresh Siddhaa1e46212009-01-20 14:20:21 -0800756 PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT)))) {
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100757
Thomas Gleixnerf34b4392008-02-15 22:17:57 +0100758 alias_cpa = *cpa;
Tejun Heo992f4c12009-06-22 11:56:24 +0900759 alias_cpa.vaddr = &laddr;
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700760 alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
Shaohua Lid75586a2008-08-21 10:46:06 +0800761
Thomas Gleixnerf34b4392008-02-15 22:17:57 +0100762 ret = __change_page_attr_set_clr(&alias_cpa, 0);
Tejun Heo992f4c12009-06-22 11:56:24 +0900763 if (ret)
764 return ret;
Thomas Gleixnerf34b4392008-02-15 22:17:57 +0100765 }
Ingo Molnar44af6c42008-01-30 13:34:03 +0100766
Arjan van de Ven488fd992008-01-30 13:34:07 +0100767#ifdef CONFIG_X86_64
Thomas Gleixner08797502008-01-30 13:34:09 +0100768 /*
Tejun Heo992f4c12009-06-22 11:56:24 +0900769 * If the primary call didn't touch the high mapping already
770 * and the physical address is inside the kernel map, we need
Thomas Gleixner08797502008-01-30 13:34:09 +0100771 * to touch the high mapped kernel as well:
772 */
Tejun Heo992f4c12009-06-22 11:56:24 +0900773 if (!within(vaddr, (unsigned long)_text, _brk_end) &&
774 within(cpa->pfn, highmap_start_pfn(), highmap_end_pfn())) {
775 unsigned long temp_cpa_vaddr = (cpa->pfn << PAGE_SHIFT) +
776 __START_KERNEL_map - phys_base;
777 alias_cpa = *cpa;
778 alias_cpa.vaddr = &temp_cpa_vaddr;
779 alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
Thomas Gleixner08797502008-01-30 13:34:09 +0100780
Tejun Heo992f4c12009-06-22 11:56:24 +0900781 /*
782 * The high mapping range is imprecise, so ignore the
783 * return value.
784 */
785 __change_page_attr_set_clr(&alias_cpa, 0);
786 }
Thomas Gleixner08797502008-01-30 13:34:09 +0100787#endif
Tejun Heo992f4c12009-06-22 11:56:24 +0900788
789 return 0;
Ingo Molnar44af6c42008-01-30 13:34:03 +0100790}
791
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100792static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias)
Thomas Gleixnerff314522008-01-30 13:34:08 +0100793{
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100794 int ret, numpages = cpa->numpages;
Thomas Gleixnerff314522008-01-30 13:34:08 +0100795
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100796 while (numpages) {
797 /*
798 * Store the remaining nr of pages for the large page
799 * preservation check.
800 */
Rafael J. Wysocki9b5cf482008-03-03 01:17:37 +0100801 cpa->numpages = numpages;
Shaohua Lid75586a2008-08-21 10:46:06 +0800802 /* for array changes, we can't use large page */
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700803 if (cpa->flags & (CPA_ARRAY | CPA_PAGES_ARRAY))
Shaohua Lid75586a2008-08-21 10:46:06 +0800804 cpa->numpages = 1;
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100805
Suresh Siddhaad5ca552008-09-23 14:00:42 -0700806 if (!debug_pagealloc)
807 spin_lock(&cpa_lock);
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100808 ret = __change_page_attr(cpa, checkalias);
Suresh Siddhaad5ca552008-09-23 14:00:42 -0700809 if (!debug_pagealloc)
810 spin_unlock(&cpa_lock);
Thomas Gleixnerff314522008-01-30 13:34:08 +0100811 if (ret)
812 return ret;
Thomas Gleixnerff314522008-01-30 13:34:08 +0100813
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100814 if (checkalias) {
815 ret = cpa_process_alias(cpa);
816 if (ret)
817 return ret;
818 }
819
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100820 /*
821 * Adjust the number of pages with the result of the
822 * CPA operation. Either a large page has been
823 * preserved or a single page update happened.
824 */
Rafael J. Wysocki9b5cf482008-03-03 01:17:37 +0100825 BUG_ON(cpa->numpages > numpages);
826 numpages -= cpa->numpages;
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700827 if (cpa->flags & (CPA_PAGES_ARRAY | CPA_ARRAY))
Shaohua Lid75586a2008-08-21 10:46:06 +0800828 cpa->curpage++;
829 else
830 *cpa->vaddr += cpa->numpages * PAGE_SIZE;
831
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100832 }
Thomas Gleixnerff314522008-01-30 13:34:08 +0100833 return 0;
834}
835
Andi Kleen6bb83832008-02-04 16:48:06 +0100836static inline int cache_attr(pgprot_t attr)
837{
838 return pgprot_val(attr) &
839 (_PAGE_PAT | _PAGE_PAT_LARGE | _PAGE_PWT | _PAGE_PCD);
840}
841
Shaohua Lid75586a2008-08-21 10:46:06 +0800842static int change_page_attr_set_clr(unsigned long *addr, int numpages,
Andi Kleenc9caa022008-03-12 03:53:29 +0100843 pgprot_t mask_set, pgprot_t mask_clr,
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700844 int force_split, int in_flag,
845 struct page **pages)
Thomas Gleixnerff314522008-01-30 13:34:08 +0100846{
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100847 struct cpa_data cpa;
Ingo Molnarcacf8902008-08-21 13:46:33 +0200848 int ret, cache, checkalias;
Jack Steinerfa526d02009-09-03 12:56:02 -0500849 unsigned long baddr = 0;
Thomas Gleixner331e4062008-02-04 16:48:06 +0100850
851 /*
852 * Check, if we are requested to change a not supported
853 * feature:
854 */
855 mask_set = canon_pgprot(mask_set);
856 mask_clr = canon_pgprot(mask_clr);
Andi Kleenc9caa022008-03-12 03:53:29 +0100857 if (!pgprot_val(mask_set) && !pgprot_val(mask_clr) && !force_split)
Thomas Gleixner331e4062008-02-04 16:48:06 +0100858 return 0;
859
Thomas Gleixner69b14152008-02-13 11:04:50 +0100860 /* Ensure we are PAGE_SIZE aligned */
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700861 if (in_flag & CPA_ARRAY) {
Shaohua Lid75586a2008-08-21 10:46:06 +0800862 int i;
863 for (i = 0; i < numpages; i++) {
864 if (addr[i] & ~PAGE_MASK) {
865 addr[i] &= PAGE_MASK;
866 WARN_ON_ONCE(1);
867 }
868 }
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700869 } else if (!(in_flag & CPA_PAGES_ARRAY)) {
870 /*
871 * in_flag of CPA_PAGES_ARRAY implies it is aligned.
872 * No need to cehck in that case
873 */
874 if (*addr & ~PAGE_MASK) {
875 *addr &= PAGE_MASK;
876 /*
877 * People should not be passing in unaligned addresses:
878 */
879 WARN_ON_ONCE(1);
880 }
Jack Steinerfa526d02009-09-03 12:56:02 -0500881 /*
882 * Save address for cache flush. *addr is modified in the call
883 * to __change_page_attr_set_clr() below.
884 */
885 baddr = *addr;
Thomas Gleixner69b14152008-02-13 11:04:50 +0100886 }
887
Nick Piggin5843d9a2008-08-01 03:15:21 +0200888 /* Must avoid aliasing mappings in the highmem code */
889 kmap_flush_unused();
890
Nick Piggindb64fe02008-10-18 20:27:03 -0700891 vm_unmap_aliases();
892
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100893 cpa.vaddr = addr;
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700894 cpa.pages = pages;
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100895 cpa.numpages = numpages;
896 cpa.mask_set = mask_set;
897 cpa.mask_clr = mask_clr;
Shaohua Lid75586a2008-08-21 10:46:06 +0800898 cpa.flags = 0;
899 cpa.curpage = 0;
Andi Kleenc9caa022008-03-12 03:53:29 +0100900 cpa.force_split = force_split;
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100901
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700902 if (in_flag & (CPA_ARRAY | CPA_PAGES_ARRAY))
903 cpa.flags |= in_flag;
Shaohua Lid75586a2008-08-21 10:46:06 +0800904
Thomas Gleixneraf96e442008-02-15 21:49:46 +0100905 /* No alias checking for _NX bit modifications */
906 checkalias = (pgprot_val(mask_set) | pgprot_val(mask_clr)) != _PAGE_NX;
907
908 ret = __change_page_attr_set_clr(&cpa, checkalias);
Thomas Gleixnerff314522008-01-30 13:34:08 +0100909
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100910 /*
Thomas Gleixnerf4ae5da2008-02-04 16:48:07 +0100911 * Check whether we really changed something:
912 */
Shaohua Lid75586a2008-08-21 10:46:06 +0800913 if (!(cpa.flags & CPA_FLUSHTLB))
Shaohua Li1ac2f7d2008-08-04 14:51:24 +0800914 goto out;
Ingo Molnarcacf8902008-08-21 13:46:33 +0200915
Thomas Gleixnerf4ae5da2008-02-04 16:48:07 +0100916 /*
Andi Kleen6bb83832008-02-04 16:48:06 +0100917 * No need to flush, when we did not set any of the caching
918 * attributes:
919 */
920 cache = cache_attr(mask_set);
921
922 /*
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100923 * On success we use clflush, when the CPU supports it to
924 * avoid the wbindv. If the CPU does not support it and in the
Thomas Gleixneraf1e6842008-01-30 13:34:08 +0100925 * error case we fall back to cpa_flush_all (which uses
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100926 * wbindv):
927 */
Shaohua Lid75586a2008-08-21 10:46:06 +0800928 if (!ret && cpu_has_clflush) {
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700929 if (cpa.flags & (CPA_PAGES_ARRAY | CPA_ARRAY)) {
930 cpa_flush_array(addr, numpages, cache,
931 cpa.flags, pages);
932 } else
Jack Steinerfa526d02009-09-03 12:56:02 -0500933 cpa_flush_range(baddr, numpages, cache);
Shaohua Lid75586a2008-08-21 10:46:06 +0800934 } else
Andi Kleen6bb83832008-02-04 16:48:06 +0100935 cpa_flush_all(cache);
Ingo Molnarcacf8902008-08-21 13:46:33 +0200936
Thomas Gleixner76ebd052008-02-09 23:24:09 +0100937out:
Thomas Gleixnerff314522008-01-30 13:34:08 +0100938 return ret;
939}
940
Shaohua Lid75586a2008-08-21 10:46:06 +0800941static inline int change_page_attr_set(unsigned long *addr, int numpages,
942 pgprot_t mask, int array)
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100943{
Shaohua Lid75586a2008-08-21 10:46:06 +0800944 return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0), 0,
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700945 (array ? CPA_ARRAY : 0), NULL);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100946}
947
Shaohua Lid75586a2008-08-21 10:46:06 +0800948static inline int change_page_attr_clear(unsigned long *addr, int numpages,
949 pgprot_t mask, int array)
Thomas Gleixner72932c72008-01-30 13:34:08 +0100950{
Shaohua Lid75586a2008-08-21 10:46:06 +0800951 return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask, 0,
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700952 (array ? CPA_ARRAY : 0), NULL);
Thomas Gleixner72932c72008-01-30 13:34:08 +0100953}
954
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -0700955static inline int cpa_set_pages_array(struct page **pages, int numpages,
956 pgprot_t mask)
957{
958 return change_page_attr_set_clr(NULL, numpages, mask, __pgprot(0), 0,
959 CPA_PAGES_ARRAY, pages);
960}
961
962static inline int cpa_clear_pages_array(struct page **pages, int numpages,
963 pgprot_t mask)
964{
965 return change_page_attr_set_clr(NULL, numpages, __pgprot(0), mask, 0,
966 CPA_PAGES_ARRAY, pages);
967}
968
venkatesh.pallipadi@intel.com12193332008-03-18 17:00:18 -0700969int _set_memory_uc(unsigned long addr, int numpages)
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100970{
Suresh Siddhade33c442008-04-25 17:07:22 -0700971 /*
972 * for now UC MINUS. see comments in ioremap_nocache()
973 */
Shaohua Lid75586a2008-08-21 10:46:06 +0800974 return change_page_attr_set(&addr, numpages,
975 __pgprot(_PAGE_CACHE_UC_MINUS), 0);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100976}
venkatesh.pallipadi@intel.com12193332008-03-18 17:00:18 -0700977
978int set_memory_uc(unsigned long addr, int numpages)
979{
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -0700980 int ret;
981
Suresh Siddhade33c442008-04-25 17:07:22 -0700982 /*
983 * for now UC MINUS. see comments in ioremap_nocache()
984 */
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -0700985 ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
986 _PAGE_CACHE_UC_MINUS, NULL);
987 if (ret)
988 goto out_err;
venkatesh.pallipadi@intel.com12193332008-03-18 17:00:18 -0700989
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -0700990 ret = _set_memory_uc(addr, numpages);
991 if (ret)
992 goto out_free;
993
994 return 0;
995
996out_free:
997 free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
998out_err:
999 return ret;
venkatesh.pallipadi@intel.com12193332008-03-18 17:00:18 -07001000}
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001001EXPORT_SYMBOL(set_memory_uc);
1002
Pauli Nieminen4f646252010-04-01 12:45:01 +00001003int _set_memory_array(unsigned long *addr, int addrinarray,
1004 unsigned long new_type)
Shaohua Lid75586a2008-08-21 10:46:06 +08001005{
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001006 int i, j;
1007 int ret;
1008
Shaohua Lid75586a2008-08-21 10:46:06 +08001009 /*
1010 * for now UC MINUS. see comments in ioremap_nocache()
1011 */
1012 for (i = 0; i < addrinarray; i++) {
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001013 ret = reserve_memtype(__pa(addr[i]), __pa(addr[i]) + PAGE_SIZE,
Pauli Nieminen4f646252010-04-01 12:45:01 +00001014 new_type, NULL);
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001015 if (ret)
1016 goto out_free;
Shaohua Lid75586a2008-08-21 10:46:06 +08001017 }
1018
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001019 ret = change_page_attr_set(addr, addrinarray,
Shaohua Lid75586a2008-08-21 10:46:06 +08001020 __pgprot(_PAGE_CACHE_UC_MINUS), 1);
Pauli Nieminen4f646252010-04-01 12:45:01 +00001021
1022 if (!ret && new_type == _PAGE_CACHE_WC)
1023 ret = change_page_attr_set_clr(addr, addrinarray,
1024 __pgprot(_PAGE_CACHE_WC),
1025 __pgprot(_PAGE_CACHE_MASK),
1026 0, CPA_ARRAY, NULL);
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001027 if (ret)
1028 goto out_free;
Rene Hermanc5e147c2008-08-22 01:02:20 +02001029
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001030 return 0;
1031
1032out_free:
1033 for (j = 0; j < i; j++)
1034 free_memtype(__pa(addr[j]), __pa(addr[j]) + PAGE_SIZE);
1035
1036 return ret;
Shaohua Lid75586a2008-08-21 10:46:06 +08001037}
Pauli Nieminen4f646252010-04-01 12:45:01 +00001038
1039int set_memory_array_uc(unsigned long *addr, int addrinarray)
1040{
1041 return _set_memory_array(addr, addrinarray, _PAGE_CACHE_UC_MINUS);
1042}
Shaohua Lid75586a2008-08-21 10:46:06 +08001043EXPORT_SYMBOL(set_memory_array_uc);
1044
Pauli Nieminen4f646252010-04-01 12:45:01 +00001045int set_memory_array_wc(unsigned long *addr, int addrinarray)
1046{
1047 return _set_memory_array(addr, addrinarray, _PAGE_CACHE_WC);
1048}
1049EXPORT_SYMBOL(set_memory_array_wc);
1050
venkatesh.pallipadi@intel.comef354af2008-03-18 17:00:23 -07001051int _set_memory_wc(unsigned long addr, int numpages)
1052{
venkatesh.pallipadi@intel.com3869c4a2009-04-09 14:26:50 -07001053 int ret;
Pallipadi, Venkateshbdc63402009-07-30 14:43:19 -07001054 unsigned long addr_copy = addr;
1055
venkatesh.pallipadi@intel.com3869c4a2009-04-09 14:26:50 -07001056 ret = change_page_attr_set(&addr, numpages,
1057 __pgprot(_PAGE_CACHE_UC_MINUS), 0);
venkatesh.pallipadi@intel.com3869c4a2009-04-09 14:26:50 -07001058 if (!ret) {
Pallipadi, Venkateshbdc63402009-07-30 14:43:19 -07001059 ret = change_page_attr_set_clr(&addr_copy, numpages,
1060 __pgprot(_PAGE_CACHE_WC),
1061 __pgprot(_PAGE_CACHE_MASK),
1062 0, 0, NULL);
venkatesh.pallipadi@intel.com3869c4a2009-04-09 14:26:50 -07001063 }
1064 return ret;
venkatesh.pallipadi@intel.comef354af2008-03-18 17:00:23 -07001065}
1066
1067int set_memory_wc(unsigned long addr, int numpages)
1068{
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001069 int ret;
1070
Andreas Herrmann499f8f82008-06-10 16:06:21 +02001071 if (!pat_enabled)
venkatesh.pallipadi@intel.comef354af2008-03-18 17:00:23 -07001072 return set_memory_uc(addr, numpages);
1073
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001074 ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
1075 _PAGE_CACHE_WC, NULL);
1076 if (ret)
1077 goto out_err;
venkatesh.pallipadi@intel.comef354af2008-03-18 17:00:23 -07001078
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001079 ret = _set_memory_wc(addr, numpages);
1080 if (ret)
1081 goto out_free;
1082
1083 return 0;
1084
1085out_free:
1086 free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
1087out_err:
1088 return ret;
venkatesh.pallipadi@intel.comef354af2008-03-18 17:00:23 -07001089}
1090EXPORT_SYMBOL(set_memory_wc);
1091
venkatesh.pallipadi@intel.com12193332008-03-18 17:00:18 -07001092int _set_memory_wb(unsigned long addr, int numpages)
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001093{
Shaohua Lid75586a2008-08-21 10:46:06 +08001094 return change_page_attr_clear(&addr, numpages,
1095 __pgprot(_PAGE_CACHE_MASK), 0);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001096}
venkatesh.pallipadi@intel.com12193332008-03-18 17:00:18 -07001097
1098int set_memory_wb(unsigned long addr, int numpages)
1099{
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001100 int ret;
1101
1102 ret = _set_memory_wb(addr, numpages);
1103 if (ret)
1104 return ret;
1105
venkatesh.pallipadi@intel.comc15238d2008-08-20 16:45:51 -07001106 free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001107 return 0;
venkatesh.pallipadi@intel.com12193332008-03-18 17:00:18 -07001108}
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001109EXPORT_SYMBOL(set_memory_wb);
1110
Shaohua Lid75586a2008-08-21 10:46:06 +08001111int set_memory_array_wb(unsigned long *addr, int addrinarray)
1112{
1113 int i;
venkatesh.pallipadi@intel.coma5593e02009-04-09 14:26:48 -07001114 int ret;
1115
1116 ret = change_page_attr_clear(addr, addrinarray,
1117 __pgprot(_PAGE_CACHE_MASK), 1);
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001118 if (ret)
1119 return ret;
Shaohua Lid75586a2008-08-21 10:46:06 +08001120
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001121 for (i = 0; i < addrinarray; i++)
1122 free_memtype(__pa(addr[i]), __pa(addr[i]) + PAGE_SIZE);
Rene Hermanc5e147c2008-08-22 01:02:20 +02001123
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001124 return 0;
Shaohua Lid75586a2008-08-21 10:46:06 +08001125}
1126EXPORT_SYMBOL(set_memory_array_wb);
1127
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001128int set_memory_x(unsigned long addr, int numpages)
1129{
H. Peter Anvin583140a2009-11-13 15:28:15 -08001130 if (!(__supported_pte_mask & _PAGE_NX))
1131 return 0;
1132
Shaohua Lid75586a2008-08-21 10:46:06 +08001133 return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_NX), 0);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001134}
1135EXPORT_SYMBOL(set_memory_x);
1136
1137int set_memory_nx(unsigned long addr, int numpages)
1138{
H. Peter Anvin583140a2009-11-13 15:28:15 -08001139 if (!(__supported_pte_mask & _PAGE_NX))
1140 return 0;
1141
Shaohua Lid75586a2008-08-21 10:46:06 +08001142 return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_NX), 0);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001143}
1144EXPORT_SYMBOL(set_memory_nx);
1145
1146int set_memory_ro(unsigned long addr, int numpages)
1147{
Shaohua Lid75586a2008-08-21 10:46:06 +08001148 return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_RW), 0);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001149}
Bruce Allana03352d2008-09-29 20:19:22 -07001150EXPORT_SYMBOL_GPL(set_memory_ro);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001151
1152int set_memory_rw(unsigned long addr, int numpages)
1153{
Shaohua Lid75586a2008-08-21 10:46:06 +08001154 return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_RW), 0);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001155}
Bruce Allana03352d2008-09-29 20:19:22 -07001156EXPORT_SYMBOL_GPL(set_memory_rw);
Ingo Molnarf62d0f02008-01-30 13:34:07 +01001157
1158int set_memory_np(unsigned long addr, int numpages)
1159{
Shaohua Lid75586a2008-08-21 10:46:06 +08001160 return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_PRESENT), 0);
Ingo Molnarf62d0f02008-01-30 13:34:07 +01001161}
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001162
Andi Kleenc9caa022008-03-12 03:53:29 +01001163int set_memory_4k(unsigned long addr, int numpages)
1164{
Shaohua Lid75586a2008-08-21 10:46:06 +08001165 return change_page_attr_set_clr(&addr, numpages, __pgprot(0),
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -07001166 __pgprot(0), 1, 0, NULL);
Andi Kleenc9caa022008-03-12 03:53:29 +01001167}
1168
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001169int set_pages_uc(struct page *page, int numpages)
1170{
1171 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001172
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +01001173 return set_memory_uc(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001174}
1175EXPORT_SYMBOL(set_pages_uc);
1176
Pauli Nieminen4f646252010-04-01 12:45:01 +00001177static int _set_pages_array(struct page **pages, int addrinarray,
1178 unsigned long new_type)
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001179{
1180 unsigned long start;
1181 unsigned long end;
1182 int i;
1183 int free_idx;
Pauli Nieminen4f646252010-04-01 12:45:01 +00001184 int ret;
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001185
1186 for (i = 0; i < addrinarray; i++) {
Thomas Hellstrom8523acf2009-08-03 09:25:45 +02001187 if (PageHighMem(pages[i]))
1188 continue;
1189 start = page_to_pfn(pages[i]) << PAGE_SHIFT;
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001190 end = start + PAGE_SIZE;
Pauli Nieminen4f646252010-04-01 12:45:01 +00001191 if (reserve_memtype(start, end, new_type, NULL))
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001192 goto err_out;
1193 }
1194
Pauli Nieminen4f646252010-04-01 12:45:01 +00001195 ret = cpa_set_pages_array(pages, addrinarray,
1196 __pgprot(_PAGE_CACHE_UC_MINUS));
1197 if (!ret && new_type == _PAGE_CACHE_WC)
1198 ret = change_page_attr_set_clr(NULL, addrinarray,
1199 __pgprot(_PAGE_CACHE_WC),
1200 __pgprot(_PAGE_CACHE_MASK),
1201 0, CPA_PAGES_ARRAY, pages);
1202 if (ret)
1203 goto err_out;
1204 return 0; /* Success */
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001205err_out:
1206 free_idx = i;
1207 for (i = 0; i < free_idx; i++) {
Thomas Hellstrom8523acf2009-08-03 09:25:45 +02001208 if (PageHighMem(pages[i]))
1209 continue;
1210 start = page_to_pfn(pages[i]) << PAGE_SHIFT;
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001211 end = start + PAGE_SIZE;
1212 free_memtype(start, end);
1213 }
1214 return -EINVAL;
1215}
Pauli Nieminen4f646252010-04-01 12:45:01 +00001216
1217int set_pages_array_uc(struct page **pages, int addrinarray)
1218{
1219 return _set_pages_array(pages, addrinarray, _PAGE_CACHE_UC_MINUS);
1220}
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001221EXPORT_SYMBOL(set_pages_array_uc);
1222
Pauli Nieminen4f646252010-04-01 12:45:01 +00001223int set_pages_array_wc(struct page **pages, int addrinarray)
1224{
1225 return _set_pages_array(pages, addrinarray, _PAGE_CACHE_WC);
1226}
1227EXPORT_SYMBOL(set_pages_array_wc);
1228
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001229int set_pages_wb(struct page *page, int numpages)
1230{
1231 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001232
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +01001233 return set_memory_wb(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001234}
1235EXPORT_SYMBOL(set_pages_wb);
1236
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001237int set_pages_array_wb(struct page **pages, int addrinarray)
1238{
1239 int retval;
1240 unsigned long start;
1241 unsigned long end;
1242 int i;
1243
1244 retval = cpa_clear_pages_array(pages, addrinarray,
1245 __pgprot(_PAGE_CACHE_MASK));
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001246 if (retval)
1247 return retval;
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001248
1249 for (i = 0; i < addrinarray; i++) {
Thomas Hellstrom8523acf2009-08-03 09:25:45 +02001250 if (PageHighMem(pages[i]))
1251 continue;
1252 start = page_to_pfn(pages[i]) << PAGE_SHIFT;
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001253 end = start + PAGE_SIZE;
1254 free_memtype(start, end);
1255 }
1256
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001257 return 0;
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001258}
1259EXPORT_SYMBOL(set_pages_array_wb);
1260
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001261int set_pages_x(struct page *page, int numpages)
1262{
1263 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001264
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +01001265 return set_memory_x(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001266}
1267EXPORT_SYMBOL(set_pages_x);
1268
1269int set_pages_nx(struct page *page, int numpages)
1270{
1271 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001272
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +01001273 return set_memory_nx(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001274}
1275EXPORT_SYMBOL(set_pages_nx);
1276
1277int set_pages_ro(struct page *page, int numpages)
1278{
1279 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001280
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +01001281 return set_memory_ro(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001282}
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001283
1284int set_pages_rw(struct page *page, int numpages)
1285{
1286 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001287
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +01001288 return set_memory_rw(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001289}
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291#ifdef CONFIG_DEBUG_PAGEALLOC
Ingo Molnarf62d0f02008-01-30 13:34:07 +01001292
1293static int __set_pages_p(struct page *page, int numpages)
1294{
Shaohua Lid75586a2008-08-21 10:46:06 +08001295 unsigned long tempaddr = (unsigned long) page_address(page);
1296 struct cpa_data cpa = { .vaddr = &tempaddr,
Thomas Gleixner72e458d2008-02-04 16:48:07 +01001297 .numpages = numpages,
1298 .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW),
Shaohua Lid75586a2008-08-21 10:46:06 +08001299 .mask_clr = __pgprot(0),
1300 .flags = 0};
Thomas Gleixner72932c72008-01-30 13:34:08 +01001301
Suresh Siddha55121b42008-09-23 14:00:40 -07001302 /*
1303 * No alias checking needed for setting present flag. otherwise,
1304 * we may need to break large pages for 64-bit kernel text
1305 * mappings (this adds to complexity if we want to do this from
1306 * atomic context especially). Let's keep it simple!
1307 */
1308 return __change_page_attr_set_clr(&cpa, 0);
Ingo Molnarf62d0f02008-01-30 13:34:07 +01001309}
1310
1311static int __set_pages_np(struct page *page, int numpages)
1312{
Shaohua Lid75586a2008-08-21 10:46:06 +08001313 unsigned long tempaddr = (unsigned long) page_address(page);
1314 struct cpa_data cpa = { .vaddr = &tempaddr,
Thomas Gleixner72e458d2008-02-04 16:48:07 +01001315 .numpages = numpages,
1316 .mask_set = __pgprot(0),
Shaohua Lid75586a2008-08-21 10:46:06 +08001317 .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW),
1318 .flags = 0};
Thomas Gleixner72932c72008-01-30 13:34:08 +01001319
Suresh Siddha55121b42008-09-23 14:00:40 -07001320 /*
1321 * No alias checking needed for setting not present flag. otherwise,
1322 * we may need to break large pages for 64-bit kernel text
1323 * mappings (this adds to complexity if we want to do this from
1324 * atomic context especially). Let's keep it simple!
1325 */
1326 return __change_page_attr_set_clr(&cpa, 0);
Ingo Molnarf62d0f02008-01-30 13:34:07 +01001327}
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329void kernel_map_pages(struct page *page, int numpages, int enable)
1330{
1331 if (PageHighMem(page))
1332 return;
Ingo Molnar9f4c8152008-01-30 13:33:41 +01001333 if (!enable) {
Ingo Molnarf9b84042006-06-27 02:54:49 -07001334 debug_check_no_locks_freed(page_address(page),
1335 numpages * PAGE_SIZE);
Ingo Molnar9f4c8152008-01-30 13:33:41 +01001336 }
Ingo Molnarde5097c2006-01-09 15:59:21 -08001337
Ingo Molnar9f4c8152008-01-30 13:33:41 +01001338 /*
Ingo Molnar12d6f212008-01-30 13:33:58 +01001339 * If page allocator is not up yet then do not call c_p_a():
1340 */
1341 if (!debug_pagealloc_enabled)
1342 return;
1343
1344 /*
Ingo Molnarf8d84062008-02-13 14:09:53 +01001345 * The return value is ignored as the calls cannot fail.
Suresh Siddha55121b42008-09-23 14:00:40 -07001346 * Large pages for identity mappings are not used at boot time
1347 * and hence no memory allocations during large page split.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 */
Ingo Molnarf62d0f02008-01-30 13:34:07 +01001349 if (enable)
1350 __set_pages_p(page, numpages);
1351 else
1352 __set_pages_np(page, numpages);
Ingo Molnar9f4c8152008-01-30 13:33:41 +01001353
1354 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +01001355 * We should perform an IPI and flush all tlbs,
1356 * but that can deadlock->flush only current cpu:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 */
1358 __flush_tlb_all();
1359}
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +01001360
1361#ifdef CONFIG_HIBERNATION
1362
1363bool kernel_page_present(struct page *page)
1364{
1365 unsigned int level;
1366 pte_t *pte;
1367
1368 if (PageHighMem(page))
1369 return false;
1370
1371 pte = lookup_address((unsigned long)page_address(page), &level);
1372 return (pte_val(*pte) & _PAGE_PRESENT);
1373}
1374
1375#endif /* CONFIG_HIBERNATION */
1376
1377#endif /* CONFIG_DEBUG_PAGEALLOC */
Arjan van de Vend1028a12008-01-30 13:34:07 +01001378
1379/*
1380 * The testcases use internal knowledge of the implementation that shouldn't
1381 * be exposed to the rest of the kernel. Include these directly here.
1382 */
1383#ifdef CONFIG_CPA_DEBUG
1384#include "pageattr-test.c"
1385#endif