blob: 8b830ca14ac46c08facc1a848ddcb3c42c0d56cf [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);
matthieu castet64edc8e2010-11-16 22:30:27 +0100259 pgprot_t required = __pgprot(0);
Arjan van de Vened724be2008-01-30 13:34:04 +0100260
Ingo Molnar687c4822008-01-30 13:34:04 +0100261 /*
Arjan van de Vened724be2008-01-30 13:34:04 +0100262 * The BIOS area between 640k and 1Mb needs to be executable for
263 * PCI BIOS based config access (CONFIG_PCI_GOBIOS) support.
Ingo Molnar687c4822008-01-30 13:34:04 +0100264 */
Matthieu Castet5bd5a452010-11-16 22:31:26 +0100265#ifdef CONFIG_PCI_BIOS
266 if (pcibios_enabled && within(pfn, BIOS_BEGIN >> PAGE_SHIFT, BIOS_END >> PAGE_SHIFT))
Arjan van de Vened724be2008-01-30 13:34:04 +0100267 pgprot_val(forbidden) |= _PAGE_NX;
Matthieu Castet5bd5a452010-11-16 22:31:26 +0100268#endif
Arjan van de Vened724be2008-01-30 13:34:04 +0100269
270 /*
271 * The kernel text needs to be executable for obvious reasons
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100272 * Does not cover __inittext since that is gone later on. On
273 * 64bit we do not enforce !NX on the low mapping
Arjan van de Vened724be2008-01-30 13:34:04 +0100274 */
275 if (within(address, (unsigned long)_text, (unsigned long)_etext))
276 pgprot_val(forbidden) |= _PAGE_NX;
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100277
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100278 /*
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100279 * The .rodata section needs to be read-only. Using the pfn
280 * catches all aliases.
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100281 */
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100282 if (within(pfn, __pa((unsigned long)__start_rodata) >> PAGE_SHIFT,
283 __pa((unsigned long)__end_rodata) >> PAGE_SHIFT))
Arjan van de Vencc0f21b2008-02-04 16:48:05 +0100284 pgprot_val(forbidden) |= _PAGE_RW;
matthieu castet64edc8e2010-11-16 22:30:27 +0100285 /*
286 * .data and .bss should always be writable.
287 */
288 if (within(address, (unsigned long)_sdata, (unsigned long)_edata) ||
289 within(address, (unsigned long)__bss_start, (unsigned long)__bss_stop))
290 pgprot_val(required) |= _PAGE_RW;
Arjan van de Vened724be2008-01-30 13:34:04 +0100291
Suresh Siddha55ca3cc2009-10-28 18:46:57 -0800292#if defined(CONFIG_X86_64) && defined(CONFIG_DEBUG_RODATA)
Suresh Siddha74e08172009-10-14 14:46:56 -0700293 /*
Suresh Siddha502f6602009-10-28 18:46:56 -0800294 * Once the kernel maps the text as RO (kernel_set_to_readonly is set),
295 * kernel text mappings for the large page aligned text, rodata sections
296 * will be always read-only. For the kernel identity mappings covering
297 * the holes caused by this alignment can be anything that user asks.
Suresh Siddha74e08172009-10-14 14:46:56 -0700298 *
299 * This will preserve the large page mappings for kernel text/data
300 * at no extra cost.
301 */
Suresh Siddha502f6602009-10-28 18:46:56 -0800302 if (kernel_set_to_readonly &&
303 within(address, (unsigned long)_text,
Suresh Siddha281ff332010-02-18 11:51:40 -0800304 (unsigned long)__end_rodata_hpage_align)) {
305 unsigned int level;
306
307 /*
308 * Don't enforce the !RW mapping for the kernel text mapping,
309 * if the current mapping is already using small page mapping.
310 * No need to work hard to preserve large page mappings in this
311 * case.
312 *
313 * This also fixes the Linux Xen paravirt guest boot failure
314 * (because of unexpected read-only mappings for kernel identity
315 * mappings). In this paravirt guest case, the kernel text
316 * mapping and the kernel identity mapping share the same
317 * page-table pages. Thus we can't really use different
318 * protections for the kernel text and identity mappings. Also,
319 * these shared mappings are made of small page mappings.
320 * Thus this don't enforce !RW mapping for small page kernel
321 * text mapping logic will help Linux Xen parvirt guest boot
322 * aswell.
323 */
324 if (lookup_address(address, &level) && (level != PG_LEVEL_4K))
325 pgprot_val(forbidden) |= _PAGE_RW;
326 }
Suresh Siddha74e08172009-10-14 14:46:56 -0700327#endif
328
Arjan van de Vened724be2008-01-30 13:34:04 +0100329 prot = __pgprot(pgprot_val(prot) & ~pgprot_val(forbidden));
matthieu castet64edc8e2010-11-16 22:30:27 +0100330 prot = __pgprot(pgprot_val(prot) | pgprot_val(required));
Ingo Molnar687c4822008-01-30 13:34:04 +0100331
332 return prot;
333}
334
Thomas Gleixner9a14aef2008-02-04 16:48:07 +0100335/*
336 * Lookup the page table entry for a virtual address. Return a pointer
337 * to the entry and the level of the mapping.
338 *
339 * Note: We return pud and pmd either when the entry is marked large
340 * or when the present bit is not set. Otherwise we would return a
341 * pointer to a nonexisting mapping.
342 */
Harvey Harrisonda7bfc52008-02-09 23:24:08 +0100343pte_t *lookup_address(unsigned long address, unsigned int *level)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100344{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 pgd_t *pgd = pgd_offset_k(address);
346 pud_t *pud;
347 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100348
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100349 *level = PG_LEVEL_NONE;
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (pgd_none(*pgd))
352 return NULL;
Ingo Molnar9df84992008-02-04 16:48:09 +0100353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 pud = pud_offset(pgd, address);
355 if (pud_none(*pud))
356 return NULL;
Andi Kleenc2f71ee2008-02-04 16:48:09 +0100357
358 *level = PG_LEVEL_1G;
359 if (pud_large(*pud) || !pud_present(*pud))
360 return (pte_t *)pud;
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 pmd = pmd_offset(pud, address);
363 if (pmd_none(*pmd))
364 return NULL;
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100365
366 *level = PG_LEVEL_2M;
Thomas Gleixner9a14aef2008-02-04 16:48:07 +0100367 if (pmd_large(*pmd) || !pmd_present(*pmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return (pte_t *)pmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100370 *level = PG_LEVEL_4K;
Ingo Molnar9df84992008-02-04 16:48:09 +0100371
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100372 return pte_offset_kernel(pmd, address);
373}
Pekka Paalanen75bb8832008-05-12 21:20:56 +0200374EXPORT_SYMBOL_GPL(lookup_address);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100375
Ingo Molnar9df84992008-02-04 16:48:09 +0100376/*
377 * Set the new pmd in all the pgds we know about:
378 */
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100379static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100380{
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100381 /* change init_mm */
382 set_pte_atomic(kpte, pte);
Ingo Molnar44af6c42008-01-30 13:34:03 +0100383#ifdef CONFIG_X86_32
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100384 if (!SHARED_KERNEL_PMD) {
Ingo Molnar44af6c42008-01-30 13:34:03 +0100385 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Jeremy Fitzhardingee3ed9102008-01-30 13:34:11 +0100387 list_for_each_entry(page, &pgd_list, lru) {
Ingo Molnar44af6c42008-01-30 13:34:03 +0100388 pgd_t *pgd;
389 pud_t *pud;
390 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100391
Ingo Molnar44af6c42008-01-30 13:34:03 +0100392 pgd = (pgd_t *)page_address(page) + pgd_index(address);
393 pud = pud_offset(pgd, address);
394 pmd = pmd_offset(pud, address);
395 set_pte_atomic((pte_t *)pmd, pte);
396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
Ingo Molnar44af6c42008-01-30 13:34:03 +0100398#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
Ingo Molnar9df84992008-02-04 16:48:09 +0100401static int
402try_preserve_large_page(pte_t *kpte, unsigned long address,
403 struct cpa_data *cpa)
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100404{
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100405 unsigned long nextpage_addr, numpages, pmask, psize, flags, addr, pfn;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100406 pte_t new_pte, old_pte, *tmp;
matthieu castet64edc8e2010-11-16 22:30:27 +0100407 pgprot_t old_prot, new_prot, req_prot;
Thomas Gleixnerfac84932008-02-09 23:24:09 +0100408 int i, do_split = 1;
Harvey Harrisonda7bfc52008-02-09 23:24:08 +0100409 unsigned int level;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100410
Andi Kleenc9caa022008-03-12 03:53:29 +0100411 if (cpa->force_split)
412 return 1;
413
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100414 spin_lock_irqsave(&pgd_lock, flags);
415 /*
416 * Check for races, another CPU might have split this page
417 * up already:
418 */
419 tmp = lookup_address(address, &level);
420 if (tmp != kpte)
421 goto out_unlock;
422
423 switch (level) {
424 case PG_LEVEL_2M:
Andi Kleen31422c52008-02-04 16:48:08 +0100425 psize = PMD_PAGE_SIZE;
426 pmask = PMD_PAGE_MASK;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100427 break;
Andi Kleenf07333f2008-02-04 16:48:09 +0100428#ifdef CONFIG_X86_64
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100429 case PG_LEVEL_1G:
Andi Kleen5d3c8b22008-02-13 16:20:35 +0100430 psize = PUD_PAGE_SIZE;
431 pmask = PUD_PAGE_MASK;
Andi Kleenf07333f2008-02-04 16:48:09 +0100432 break;
433#endif
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100434 default:
Ingo Molnarbeaff632008-02-04 16:48:09 +0100435 do_split = -EINVAL;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100436 goto out_unlock;
437 }
438
439 /*
440 * Calculate the number of pages, which fit into this large
441 * page starting at address:
442 */
443 nextpage_addr = (address + psize) & pmask;
444 numpages = (nextpage_addr - address) >> PAGE_SHIFT;
Rafael J. Wysocki9b5cf482008-03-03 01:17:37 +0100445 if (numpages < cpa->numpages)
446 cpa->numpages = numpages;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100447
448 /*
449 * We are safe now. Check whether the new pgprot is the same:
450 */
451 old_pte = *kpte;
matthieu castet64edc8e2010-11-16 22:30:27 +0100452 old_prot = new_prot = req_prot = pte_pgprot(old_pte);
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100453
matthieu castet64edc8e2010-11-16 22:30:27 +0100454 pgprot_val(req_prot) &= ~pgprot_val(cpa->mask_clr);
455 pgprot_val(req_prot) |= pgprot_val(cpa->mask_set);
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100456
457 /*
458 * old_pte points to the large page base address. So we need
459 * to add the offset of the virtual address:
460 */
461 pfn = pte_pfn(old_pte) + ((address & (psize - 1)) >> PAGE_SHIFT);
462 cpa->pfn = pfn;
463
matthieu castet64edc8e2010-11-16 22:30:27 +0100464 new_prot = static_protections(req_prot, address, pfn);
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100465
466 /*
Thomas Gleixnerfac84932008-02-09 23:24:09 +0100467 * We need to check the full range, whether
468 * static_protection() requires a different pgprot for one of
469 * the pages in the range we try to preserve:
470 */
matthieu castet64edc8e2010-11-16 22:30:27 +0100471 addr = address & pmask;
472 pfn = pte_pfn(old_pte);
473 for (i = 0; i < (psize >> PAGE_SHIFT); i++, addr += PAGE_SIZE, pfn++) {
474 pgprot_t chk_prot = static_protections(req_prot, addr, pfn);
Thomas Gleixnerfac84932008-02-09 23:24:09 +0100475
476 if (pgprot_val(chk_prot) != pgprot_val(new_prot))
477 goto out_unlock;
478 }
479
480 /*
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100481 * If there are no changes, return. maxpages has been updated
482 * above:
483 */
484 if (pgprot_val(new_prot) == pgprot_val(old_prot)) {
Ingo Molnarbeaff632008-02-04 16:48:09 +0100485 do_split = 0;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100486 goto out_unlock;
487 }
488
489 /*
490 * We need to change the attributes. Check, whether we can
491 * change the large page in one go. We request a split, when
492 * the address is not aligned and the number of pages is
493 * smaller than the number of pages in the large page. Note
494 * that we limited the number of possible pages already to
495 * the number of pages in the large page.
496 */
matthieu castet64edc8e2010-11-16 22:30:27 +0100497 if (address == (address & pmask) && cpa->numpages == (psize >> PAGE_SHIFT)) {
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100498 /*
499 * The address is aligned and the number of pages
500 * covers the full page.
501 */
502 new_pte = pfn_pte(pte_pfn(old_pte), canon_pgprot(new_prot));
503 __set_pmd_pte(kpte, address, new_pte);
Shaohua Lid75586a2008-08-21 10:46:06 +0800504 cpa->flags |= CPA_FLUSHTLB;
Ingo Molnarbeaff632008-02-04 16:48:09 +0100505 do_split = 0;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100506 }
507
508out_unlock:
509 spin_unlock_irqrestore(&pgd_lock, flags);
Ingo Molnar9df84992008-02-04 16:48:09 +0100510
Ingo Molnarbeaff632008-02-04 16:48:09 +0100511 return do_split;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100512}
513
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100514static int split_large_page(pte_t *kpte, unsigned long address)
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100515{
Thomas Gleixner7b610ee2008-02-04 16:48:10 +0100516 unsigned long flags, pfn, pfninc = 1;
Ingo Molnar86f03982008-01-30 13:34:09 +0100517 unsigned int i, level;
Ingo Molnar9df84992008-02-04 16:48:09 +0100518 pte_t *pbase, *tmp;
519 pgprot_t ref_prot;
Suresh Siddhaad5ca552008-09-23 14:00:42 -0700520 struct page *base;
521
522 if (!debug_pagealloc)
523 spin_unlock(&cpa_lock);
Vegard Nossum9e730232009-02-22 11:28:25 +0100524 base = alloc_pages(GFP_KERNEL | __GFP_NOTRACK, 0);
Suresh Siddhaad5ca552008-09-23 14:00:42 -0700525 if (!debug_pagealloc)
526 spin_lock(&cpa_lock);
Suresh Siddha8311eb82008-09-23 14:00:41 -0700527 if (!base)
528 return -ENOMEM;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100529
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100530 spin_lock_irqsave(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100531 /*
532 * Check for races, another CPU might have split this page
533 * up for us already:
534 */
535 tmp = lookup_address(address, &level);
Ingo Molnar6ce9fc12008-02-04 16:48:08 +0100536 if (tmp != kpte)
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100537 goto out_unlock;
538
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100539 pbase = (pte_t *)page_address(base);
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -0700540 paravirt_alloc_pte(&init_mm, page_to_pfn(base));
Thomas Gleixner07cf89c2008-02-04 16:48:08 +0100541 ref_prot = pte_pgprot(pte_clrhuge(*kpte));
Ingo Molnar7a5714e2009-02-20 17:44:21 +0100542 /*
543 * If we ever want to utilize the PAT bit, we need to
544 * update this function to make sure it's converted from
545 * bit 12 to bit 7 when we cross from the 2MB level to
546 * the 4K level:
547 */
548 WARN_ON_ONCE(pgprot_val(ref_prot) & _PAGE_PAT_LARGE);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100549
Andi Kleenf07333f2008-02-04 16:48:09 +0100550#ifdef CONFIG_X86_64
551 if (level == PG_LEVEL_1G) {
552 pfninc = PMD_PAGE_SIZE >> PAGE_SHIFT;
553 pgprot_val(ref_prot) |= _PAGE_PSE;
Andi Kleenf07333f2008-02-04 16:48:09 +0100554 }
555#endif
556
Thomas Gleixner63c1dcf2008-02-04 16:48:05 +0100557 /*
558 * Get the target pfn from the original entry:
559 */
560 pfn = pte_pfn(*kpte);
Andi Kleenf07333f2008-02-04 16:48:09 +0100561 for (i = 0; i < PTRS_PER_PTE; i++, pfn += pfninc)
Thomas Gleixner63c1dcf2008-02-04 16:48:05 +0100562 set_pte(&pbase[i], pfn_pte(pfn, ref_prot));
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100563
Andi Kleence0c0e52008-05-02 11:46:49 +0200564 if (address >= (unsigned long)__va(0) &&
Yinghai Luf361a452008-07-10 20:38:26 -0700565 address < (unsigned long)__va(max_low_pfn_mapped << PAGE_SHIFT))
566 split_page_count(level);
567
568#ifdef CONFIG_X86_64
569 if (address >= (unsigned long)__va(1UL<<32) &&
Thomas Gleixner65280e62008-05-05 16:35:21 +0200570 address < (unsigned long)__va(max_pfn_mapped << PAGE_SHIFT))
571 split_page_count(level);
Yinghai Luf361a452008-07-10 20:38:26 -0700572#endif
Andi Kleence0c0e52008-05-02 11:46:49 +0200573
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100574 /*
Ingo Molnar07a66d72009-02-20 08:04:13 +0100575 * Install the new, split up pagetable.
Huang, Ying4c881ca2008-01-30 13:34:04 +0100576 *
Ingo Molnar07a66d72009-02-20 08:04:13 +0100577 * We use the standard kernel pagetable protections for the new
578 * pagetable protections, the actual ptes set above control the
579 * primary protection behavior:
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100580 */
Ingo Molnar07a66d72009-02-20 08:04:13 +0100581 __set_pmd_pte(kpte, address, mk_pte(base, __pgprot(_KERNPG_TABLE)));
Ingo Molnar211b3d02009-03-10 22:31:03 +0100582
583 /*
584 * Intel Atom errata AAH41 workaround.
585 *
586 * The real fix should be in hw or in a microcode update, but
587 * we also probabilistically try to reduce the window of having
588 * a large TLB mixed with 4K TLBs while instruction fetches are
589 * going on.
590 */
591 __flush_tlb_all();
592
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100593 base = NULL;
594
595out_unlock:
Thomas Gleixnereb5b5f02008-02-09 23:24:09 +0100596 /*
597 * If we dropped out via the lookup_address check under
598 * pgd_lock then stick the page back into the pool:
599 */
Suresh Siddha8311eb82008-09-23 14:00:41 -0700600 if (base)
601 __free_page(base);
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100602 spin_unlock_irqrestore(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100603
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100604 return 0;
605}
606
Suresh Siddhaa1e46212009-01-20 14:20:21 -0800607static int __cpa_process_fault(struct cpa_data *cpa, unsigned long vaddr,
608 int primary)
609{
610 /*
611 * Ignore all non primary paths.
612 */
613 if (!primary)
614 return 0;
615
616 /*
617 * Ignore the NULL PTE for kernel identity mapping, as it is expected
618 * to have holes.
619 * Also set numpages to '1' indicating that we processed cpa req for
620 * one virtual address page and its pfn. TBD: numpages can be set based
621 * on the initial value and the level returned by lookup_address().
622 */
623 if (within(vaddr, PAGE_OFFSET,
624 PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT))) {
625 cpa->numpages = 1;
626 cpa->pfn = __pa(vaddr) >> PAGE_SHIFT;
627 return 0;
628 } else {
629 WARN(1, KERN_WARNING "CPA: called for zero pte. "
630 "vaddr = %lx cpa->vaddr = %lx\n", vaddr,
631 *cpa->vaddr);
632
633 return -EFAULT;
634 }
635}
636
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100637static int __change_page_attr(struct cpa_data *cpa, int primary)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100638{
Shaohua Lid75586a2008-08-21 10:46:06 +0800639 unsigned long address;
Harvey Harrisonda7bfc52008-02-09 23:24:08 +0100640 int do_split, err;
641 unsigned int level;
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100642 pte_t *kpte, old_pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Thomas Hellstrom8523acf2009-08-03 09:25:45 +0200644 if (cpa->flags & CPA_PAGES_ARRAY) {
645 struct page *page = cpa->pages[cpa->curpage];
646 if (unlikely(PageHighMem(page)))
647 return 0;
648 address = (unsigned long)page_address(page);
649 } else if (cpa->flags & CPA_ARRAY)
Shaohua Lid75586a2008-08-21 10:46:06 +0800650 address = cpa->vaddr[cpa->curpage];
651 else
652 address = *cpa->vaddr;
Ingo Molnar97f99fe2008-01-30 13:33:55 +0100653repeat:
Ingo Molnarf0646e42008-01-30 13:33:43 +0100654 kpte = lookup_address(address, &level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (!kpte)
Suresh Siddhaa1e46212009-01-20 14:20:21 -0800656 return __cpa_process_fault(cpa, address, primary);
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100657
658 old_pte = *kpte;
Suresh Siddhaa1e46212009-01-20 14:20:21 -0800659 if (!pte_val(old_pte))
660 return __cpa_process_fault(cpa, address, primary);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100661
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100662 if (level == PG_LEVEL_4K) {
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100663 pte_t new_pte;
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100664 pgprot_t new_prot = pte_pgprot(old_pte);
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100665 unsigned long pfn = pte_pfn(old_pte);
Thomas Gleixnera72a08a2008-01-30 13:34:07 +0100666
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100667 pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
668 pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
Ingo Molnar86f03982008-01-30 13:34:09 +0100669
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100670 new_prot = static_protections(new_prot, address, pfn);
Ingo Molnar86f03982008-01-30 13:34:09 +0100671
Arjan van de Ven626c2c92008-02-04 16:48:05 +0100672 /*
673 * We need to keep the pfn from the existing PTE,
674 * after all we're only going to change it's attributes
675 * not the memory it points to
676 */
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100677 new_pte = pfn_pte(pfn, canon_pgprot(new_prot));
678 cpa->pfn = pfn;
Thomas Gleixnerf4ae5da2008-02-04 16:48:07 +0100679 /*
680 * Do we really change anything ?
681 */
682 if (pte_val(old_pte) != pte_val(new_pte)) {
683 set_pte_atomic(kpte, new_pte);
Shaohua Lid75586a2008-08-21 10:46:06 +0800684 cpa->flags |= CPA_FLUSHTLB;
Thomas Gleixnerf4ae5da2008-02-04 16:48:07 +0100685 }
Rafael J. Wysocki9b5cf482008-03-03 01:17:37 +0100686 cpa->numpages = 1;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100687 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100689
690 /*
691 * Check, whether we can keep the large page intact
692 * and just change the pte:
693 */
Ingo Molnarbeaff632008-02-04 16:48:09 +0100694 do_split = try_preserve_large_page(kpte, address, cpa);
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100695 /*
696 * When the range fits into the existing large page,
Rafael J. Wysocki9b5cf482008-03-03 01:17:37 +0100697 * return. cp->numpages and cpa->tlbflush have been updated in
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100698 * try_large_page:
699 */
Ingo Molnar87f7f8f2008-02-04 16:48:10 +0100700 if (do_split <= 0)
701 return do_split;
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100702
703 /*
704 * We have to split the large page:
705 */
Ingo Molnar87f7f8f2008-02-04 16:48:10 +0100706 err = split_large_page(kpte, address);
707 if (!err) {
Suresh Siddhaad5ca552008-09-23 14:00:42 -0700708 /*
709 * Do a global flush tlb after splitting the large page
710 * and before we do the actual change page attribute in the PTE.
711 *
712 * With out this, we violate the TLB application note, that says
713 * "The TLBs may contain both ordinary and large-page
714 * translations for a 4-KByte range of linear addresses. This
715 * may occur if software modifies the paging structures so that
716 * the page size used for the address range changes. If the two
717 * translations differ with respect to page frame or attributes
718 * (e.g., permissions), processor behavior is undefined and may
719 * be implementation-specific."
720 *
721 * We do this global tlb flush inside the cpa_lock, so that we
722 * don't allow any other cpu, with stale tlb entries change the
723 * page attribute in parallel, that also falls into the
724 * just split large page entry.
725 */
726 flush_tlb_all();
Ingo Molnar87f7f8f2008-02-04 16:48:10 +0100727 goto repeat;
728 }
Ingo Molnarbeaff632008-02-04 16:48:09 +0100729
Ingo Molnar87f7f8f2008-02-04 16:48:10 +0100730 return err;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100731}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100733static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias);
734
735static int cpa_process_alias(struct cpa_data *cpa)
Ingo Molnar44af6c42008-01-30 13:34:03 +0100736{
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100737 struct cpa_data alias_cpa;
Tejun Heo992f4c12009-06-22 11:56:24 +0900738 unsigned long laddr = (unsigned long)__va(cpa->pfn << PAGE_SHIFT);
Tejun Heoe933a732009-08-14 15:00:53 +0900739 unsigned long vaddr;
Tejun Heo992f4c12009-06-22 11:56:24 +0900740 int ret;
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100741
Yinghai Lu965194c2008-07-12 14:31:28 -0700742 if (cpa->pfn >= max_pfn_mapped)
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100743 return 0;
744
Yinghai Luf361a452008-07-10 20:38:26 -0700745#ifdef CONFIG_X86_64
Yinghai Lu965194c2008-07-12 14:31:28 -0700746 if (cpa->pfn >= max_low_pfn_mapped && cpa->pfn < (1UL<<(32-PAGE_SHIFT)))
Yinghai Luf361a452008-07-10 20:38:26 -0700747 return 0;
748#endif
Thomas Gleixnerf34b4392008-02-15 22:17:57 +0100749 /*
750 * No need to redo, when the primary call touched the direct
751 * mapping already:
752 */
Thomas Hellstrom8523acf2009-08-03 09:25:45 +0200753 if (cpa->flags & CPA_PAGES_ARRAY) {
754 struct page *page = cpa->pages[cpa->curpage];
755 if (unlikely(PageHighMem(page)))
756 return 0;
757 vaddr = (unsigned long)page_address(page);
758 } else if (cpa->flags & CPA_ARRAY)
Shaohua Lid75586a2008-08-21 10:46:06 +0800759 vaddr = cpa->vaddr[cpa->curpage];
760 else
761 vaddr = *cpa->vaddr;
762
763 if (!(within(vaddr, PAGE_OFFSET,
Suresh Siddhaa1e46212009-01-20 14:20:21 -0800764 PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT)))) {
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100765
Thomas Gleixnerf34b4392008-02-15 22:17:57 +0100766 alias_cpa = *cpa;
Tejun Heo992f4c12009-06-22 11:56:24 +0900767 alias_cpa.vaddr = &laddr;
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700768 alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
Shaohua Lid75586a2008-08-21 10:46:06 +0800769
Thomas Gleixnerf34b4392008-02-15 22:17:57 +0100770 ret = __change_page_attr_set_clr(&alias_cpa, 0);
Tejun Heo992f4c12009-06-22 11:56:24 +0900771 if (ret)
772 return ret;
Thomas Gleixnerf34b4392008-02-15 22:17:57 +0100773 }
Ingo Molnar44af6c42008-01-30 13:34:03 +0100774
Arjan van de Ven488fd992008-01-30 13:34:07 +0100775#ifdef CONFIG_X86_64
Thomas Gleixner08797502008-01-30 13:34:09 +0100776 /*
Tejun Heo992f4c12009-06-22 11:56:24 +0900777 * If the primary call didn't touch the high mapping already
778 * and the physical address is inside the kernel map, we need
Thomas Gleixner08797502008-01-30 13:34:09 +0100779 * to touch the high mapped kernel as well:
780 */
Tejun Heo992f4c12009-06-22 11:56:24 +0900781 if (!within(vaddr, (unsigned long)_text, _brk_end) &&
782 within(cpa->pfn, highmap_start_pfn(), highmap_end_pfn())) {
783 unsigned long temp_cpa_vaddr = (cpa->pfn << PAGE_SHIFT) +
784 __START_KERNEL_map - phys_base;
785 alias_cpa = *cpa;
786 alias_cpa.vaddr = &temp_cpa_vaddr;
787 alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
Thomas Gleixner08797502008-01-30 13:34:09 +0100788
Tejun Heo992f4c12009-06-22 11:56:24 +0900789 /*
790 * The high mapping range is imprecise, so ignore the
791 * return value.
792 */
793 __change_page_attr_set_clr(&alias_cpa, 0);
794 }
Thomas Gleixner08797502008-01-30 13:34:09 +0100795#endif
Tejun Heo992f4c12009-06-22 11:56:24 +0900796
797 return 0;
Ingo Molnar44af6c42008-01-30 13:34:03 +0100798}
799
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100800static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias)
Thomas Gleixnerff314522008-01-30 13:34:08 +0100801{
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100802 int ret, numpages = cpa->numpages;
Thomas Gleixnerff314522008-01-30 13:34:08 +0100803
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100804 while (numpages) {
805 /*
806 * Store the remaining nr of pages for the large page
807 * preservation check.
808 */
Rafael J. Wysocki9b5cf482008-03-03 01:17:37 +0100809 cpa->numpages = numpages;
Shaohua Lid75586a2008-08-21 10:46:06 +0800810 /* for array changes, we can't use large page */
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700811 if (cpa->flags & (CPA_ARRAY | CPA_PAGES_ARRAY))
Shaohua Lid75586a2008-08-21 10:46:06 +0800812 cpa->numpages = 1;
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100813
Suresh Siddhaad5ca552008-09-23 14:00:42 -0700814 if (!debug_pagealloc)
815 spin_lock(&cpa_lock);
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100816 ret = __change_page_attr(cpa, checkalias);
Suresh Siddhaad5ca552008-09-23 14:00:42 -0700817 if (!debug_pagealloc)
818 spin_unlock(&cpa_lock);
Thomas Gleixnerff314522008-01-30 13:34:08 +0100819 if (ret)
820 return ret;
Thomas Gleixnerff314522008-01-30 13:34:08 +0100821
Thomas Gleixnerc31c7d42008-02-18 20:54:14 +0100822 if (checkalias) {
823 ret = cpa_process_alias(cpa);
824 if (ret)
825 return ret;
826 }
827
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100828 /*
829 * Adjust the number of pages with the result of the
830 * CPA operation. Either a large page has been
831 * preserved or a single page update happened.
832 */
Rafael J. Wysocki9b5cf482008-03-03 01:17:37 +0100833 BUG_ON(cpa->numpages > numpages);
834 numpages -= cpa->numpages;
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700835 if (cpa->flags & (CPA_PAGES_ARRAY | CPA_ARRAY))
Shaohua Lid75586a2008-08-21 10:46:06 +0800836 cpa->curpage++;
837 else
838 *cpa->vaddr += cpa->numpages * PAGE_SIZE;
839
Thomas Gleixner65e074d2008-02-04 16:48:07 +0100840 }
Thomas Gleixnerff314522008-01-30 13:34:08 +0100841 return 0;
842}
843
Andi Kleen6bb83832008-02-04 16:48:06 +0100844static inline int cache_attr(pgprot_t attr)
845{
846 return pgprot_val(attr) &
847 (_PAGE_PAT | _PAGE_PAT_LARGE | _PAGE_PWT | _PAGE_PCD);
848}
849
Shaohua Lid75586a2008-08-21 10:46:06 +0800850static int change_page_attr_set_clr(unsigned long *addr, int numpages,
Andi Kleenc9caa022008-03-12 03:53:29 +0100851 pgprot_t mask_set, pgprot_t mask_clr,
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700852 int force_split, int in_flag,
853 struct page **pages)
Thomas Gleixnerff314522008-01-30 13:34:08 +0100854{
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100855 struct cpa_data cpa;
Ingo Molnarcacf8902008-08-21 13:46:33 +0200856 int ret, cache, checkalias;
Jack Steinerfa526d02009-09-03 12:56:02 -0500857 unsigned long baddr = 0;
Thomas Gleixner331e4062008-02-04 16:48:06 +0100858
859 /*
860 * Check, if we are requested to change a not supported
861 * feature:
862 */
863 mask_set = canon_pgprot(mask_set);
864 mask_clr = canon_pgprot(mask_clr);
Andi Kleenc9caa022008-03-12 03:53:29 +0100865 if (!pgprot_val(mask_set) && !pgprot_val(mask_clr) && !force_split)
Thomas Gleixner331e4062008-02-04 16:48:06 +0100866 return 0;
867
Thomas Gleixner69b14152008-02-13 11:04:50 +0100868 /* Ensure we are PAGE_SIZE aligned */
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700869 if (in_flag & CPA_ARRAY) {
Shaohua Lid75586a2008-08-21 10:46:06 +0800870 int i;
871 for (i = 0; i < numpages; i++) {
872 if (addr[i] & ~PAGE_MASK) {
873 addr[i] &= PAGE_MASK;
874 WARN_ON_ONCE(1);
875 }
876 }
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700877 } else if (!(in_flag & CPA_PAGES_ARRAY)) {
878 /*
879 * in_flag of CPA_PAGES_ARRAY implies it is aligned.
880 * No need to cehck in that case
881 */
882 if (*addr & ~PAGE_MASK) {
883 *addr &= PAGE_MASK;
884 /*
885 * People should not be passing in unaligned addresses:
886 */
887 WARN_ON_ONCE(1);
888 }
Jack Steinerfa526d02009-09-03 12:56:02 -0500889 /*
890 * Save address for cache flush. *addr is modified in the call
891 * to __change_page_attr_set_clr() below.
892 */
893 baddr = *addr;
Thomas Gleixner69b14152008-02-13 11:04:50 +0100894 }
895
Nick Piggin5843d9a2008-08-01 03:15:21 +0200896 /* Must avoid aliasing mappings in the highmem code */
897 kmap_flush_unused();
898
Nick Piggindb64fe02008-10-18 20:27:03 -0700899 vm_unmap_aliases();
900
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100901 cpa.vaddr = addr;
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700902 cpa.pages = pages;
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100903 cpa.numpages = numpages;
904 cpa.mask_set = mask_set;
905 cpa.mask_clr = mask_clr;
Shaohua Lid75586a2008-08-21 10:46:06 +0800906 cpa.flags = 0;
907 cpa.curpage = 0;
Andi Kleenc9caa022008-03-12 03:53:29 +0100908 cpa.force_split = force_split;
Thomas Gleixner72e458d2008-02-04 16:48:07 +0100909
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700910 if (in_flag & (CPA_ARRAY | CPA_PAGES_ARRAY))
911 cpa.flags |= in_flag;
Shaohua Lid75586a2008-08-21 10:46:06 +0800912
Thomas Gleixneraf96e442008-02-15 21:49:46 +0100913 /* No alias checking for _NX bit modifications */
914 checkalias = (pgprot_val(mask_set) | pgprot_val(mask_clr)) != _PAGE_NX;
915
916 ret = __change_page_attr_set_clr(&cpa, checkalias);
Thomas Gleixnerff314522008-01-30 13:34:08 +0100917
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100918 /*
Thomas Gleixnerf4ae5da2008-02-04 16:48:07 +0100919 * Check whether we really changed something:
920 */
Shaohua Lid75586a2008-08-21 10:46:06 +0800921 if (!(cpa.flags & CPA_FLUSHTLB))
Shaohua Li1ac2f7d2008-08-04 14:51:24 +0800922 goto out;
Ingo Molnarcacf8902008-08-21 13:46:33 +0200923
Thomas Gleixnerf4ae5da2008-02-04 16:48:07 +0100924 /*
Andi Kleen6bb83832008-02-04 16:48:06 +0100925 * No need to flush, when we did not set any of the caching
926 * attributes:
927 */
928 cache = cache_attr(mask_set);
929
930 /*
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100931 * On success we use clflush, when the CPU supports it to
932 * avoid the wbindv. If the CPU does not support it and in the
Thomas Gleixneraf1e6842008-01-30 13:34:08 +0100933 * error case we fall back to cpa_flush_all (which uses
Thomas Gleixner57a6a462008-01-30 13:34:08 +0100934 * wbindv):
935 */
Shaohua Lid75586a2008-08-21 10:46:06 +0800936 if (!ret && cpu_has_clflush) {
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700937 if (cpa.flags & (CPA_PAGES_ARRAY | CPA_ARRAY)) {
938 cpa_flush_array(addr, numpages, cache,
939 cpa.flags, pages);
940 } else
Jack Steinerfa526d02009-09-03 12:56:02 -0500941 cpa_flush_range(baddr, numpages, cache);
Shaohua Lid75586a2008-08-21 10:46:06 +0800942 } else
Andi Kleen6bb83832008-02-04 16:48:06 +0100943 cpa_flush_all(cache);
Ingo Molnarcacf8902008-08-21 13:46:33 +0200944
Thomas Gleixner76ebd052008-02-09 23:24:09 +0100945out:
Thomas Gleixnerff314522008-01-30 13:34:08 +0100946 return ret;
947}
948
Shaohua Lid75586a2008-08-21 10:46:06 +0800949static inline int change_page_attr_set(unsigned long *addr, int numpages,
950 pgprot_t mask, int array)
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100951{
Shaohua Lid75586a2008-08-21 10:46:06 +0800952 return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0), 0,
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700953 (array ? CPA_ARRAY : 0), NULL);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100954}
955
Shaohua Lid75586a2008-08-21 10:46:06 +0800956static inline int change_page_attr_clear(unsigned long *addr, int numpages,
957 pgprot_t mask, int array)
Thomas Gleixner72932c72008-01-30 13:34:08 +0100958{
Shaohua Lid75586a2008-08-21 10:46:06 +0800959 return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask, 0,
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -0700960 (array ? CPA_ARRAY : 0), NULL);
Thomas Gleixner72932c72008-01-30 13:34:08 +0100961}
962
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -0700963static inline int cpa_set_pages_array(struct page **pages, int numpages,
964 pgprot_t mask)
965{
966 return change_page_attr_set_clr(NULL, numpages, mask, __pgprot(0), 0,
967 CPA_PAGES_ARRAY, pages);
968}
969
970static inline int cpa_clear_pages_array(struct page **pages, int numpages,
971 pgprot_t mask)
972{
973 return change_page_attr_set_clr(NULL, numpages, __pgprot(0), mask, 0,
974 CPA_PAGES_ARRAY, pages);
975}
976
venkatesh.pallipadi@intel.com12193332008-03-18 17:00:18 -0700977int _set_memory_uc(unsigned long addr, int numpages)
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100978{
Suresh Siddhade33c442008-04-25 17:07:22 -0700979 /*
980 * for now UC MINUS. see comments in ioremap_nocache()
981 */
Shaohua Lid75586a2008-08-21 10:46:06 +0800982 return change_page_attr_set(&addr, numpages,
983 __pgprot(_PAGE_CACHE_UC_MINUS), 0);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100984}
venkatesh.pallipadi@intel.com12193332008-03-18 17:00:18 -0700985
986int set_memory_uc(unsigned long addr, int numpages)
987{
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -0700988 int ret;
989
Suresh Siddhade33c442008-04-25 17:07:22 -0700990 /*
991 * for now UC MINUS. see comments in ioremap_nocache()
992 */
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -0700993 ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
994 _PAGE_CACHE_UC_MINUS, NULL);
995 if (ret)
996 goto out_err;
venkatesh.pallipadi@intel.com12193332008-03-18 17:00:18 -0700997
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -0700998 ret = _set_memory_uc(addr, numpages);
999 if (ret)
1000 goto out_free;
1001
1002 return 0;
1003
1004out_free:
1005 free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
1006out_err:
1007 return ret;
venkatesh.pallipadi@intel.com12193332008-03-18 17:00:18 -07001008}
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001009EXPORT_SYMBOL(set_memory_uc);
1010
Pauli Nieminen4f646252010-04-01 12:45:01 +00001011int _set_memory_array(unsigned long *addr, int addrinarray,
1012 unsigned long new_type)
Shaohua Lid75586a2008-08-21 10:46:06 +08001013{
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001014 int i, j;
1015 int ret;
1016
Shaohua Lid75586a2008-08-21 10:46:06 +08001017 /*
1018 * for now UC MINUS. see comments in ioremap_nocache()
1019 */
1020 for (i = 0; i < addrinarray; i++) {
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001021 ret = reserve_memtype(__pa(addr[i]), __pa(addr[i]) + PAGE_SIZE,
Pauli Nieminen4f646252010-04-01 12:45:01 +00001022 new_type, NULL);
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001023 if (ret)
1024 goto out_free;
Shaohua Lid75586a2008-08-21 10:46:06 +08001025 }
1026
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001027 ret = change_page_attr_set(addr, addrinarray,
Shaohua Lid75586a2008-08-21 10:46:06 +08001028 __pgprot(_PAGE_CACHE_UC_MINUS), 1);
Pauli Nieminen4f646252010-04-01 12:45:01 +00001029
1030 if (!ret && new_type == _PAGE_CACHE_WC)
1031 ret = change_page_attr_set_clr(addr, addrinarray,
1032 __pgprot(_PAGE_CACHE_WC),
1033 __pgprot(_PAGE_CACHE_MASK),
1034 0, CPA_ARRAY, NULL);
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001035 if (ret)
1036 goto out_free;
Rene Hermanc5e147c2008-08-22 01:02:20 +02001037
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001038 return 0;
1039
1040out_free:
1041 for (j = 0; j < i; j++)
1042 free_memtype(__pa(addr[j]), __pa(addr[j]) + PAGE_SIZE);
1043
1044 return ret;
Shaohua Lid75586a2008-08-21 10:46:06 +08001045}
Pauli Nieminen4f646252010-04-01 12:45:01 +00001046
1047int set_memory_array_uc(unsigned long *addr, int addrinarray)
1048{
1049 return _set_memory_array(addr, addrinarray, _PAGE_CACHE_UC_MINUS);
1050}
Shaohua Lid75586a2008-08-21 10:46:06 +08001051EXPORT_SYMBOL(set_memory_array_uc);
1052
Pauli Nieminen4f646252010-04-01 12:45:01 +00001053int set_memory_array_wc(unsigned long *addr, int addrinarray)
1054{
1055 return _set_memory_array(addr, addrinarray, _PAGE_CACHE_WC);
1056}
1057EXPORT_SYMBOL(set_memory_array_wc);
1058
venkatesh.pallipadi@intel.comef354af2008-03-18 17:00:23 -07001059int _set_memory_wc(unsigned long addr, int numpages)
1060{
venkatesh.pallipadi@intel.com3869c4a2009-04-09 14:26:50 -07001061 int ret;
Pallipadi, Venkateshbdc63402009-07-30 14:43:19 -07001062 unsigned long addr_copy = addr;
1063
venkatesh.pallipadi@intel.com3869c4a2009-04-09 14:26:50 -07001064 ret = change_page_attr_set(&addr, numpages,
1065 __pgprot(_PAGE_CACHE_UC_MINUS), 0);
venkatesh.pallipadi@intel.com3869c4a2009-04-09 14:26:50 -07001066 if (!ret) {
Pallipadi, Venkateshbdc63402009-07-30 14:43:19 -07001067 ret = change_page_attr_set_clr(&addr_copy, numpages,
1068 __pgprot(_PAGE_CACHE_WC),
1069 __pgprot(_PAGE_CACHE_MASK),
1070 0, 0, NULL);
venkatesh.pallipadi@intel.com3869c4a2009-04-09 14:26:50 -07001071 }
1072 return ret;
venkatesh.pallipadi@intel.comef354af2008-03-18 17:00:23 -07001073}
1074
1075int set_memory_wc(unsigned long addr, int numpages)
1076{
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001077 int ret;
1078
Andreas Herrmann499f8f82008-06-10 16:06:21 +02001079 if (!pat_enabled)
venkatesh.pallipadi@intel.comef354af2008-03-18 17:00:23 -07001080 return set_memory_uc(addr, numpages);
1081
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001082 ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
1083 _PAGE_CACHE_WC, NULL);
1084 if (ret)
1085 goto out_err;
venkatesh.pallipadi@intel.comef354af2008-03-18 17:00:23 -07001086
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001087 ret = _set_memory_wc(addr, numpages);
1088 if (ret)
1089 goto out_free;
1090
1091 return 0;
1092
1093out_free:
1094 free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
1095out_err:
1096 return ret;
venkatesh.pallipadi@intel.comef354af2008-03-18 17:00:23 -07001097}
1098EXPORT_SYMBOL(set_memory_wc);
1099
venkatesh.pallipadi@intel.com12193332008-03-18 17:00:18 -07001100int _set_memory_wb(unsigned long addr, int numpages)
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001101{
Shaohua Lid75586a2008-08-21 10:46:06 +08001102 return change_page_attr_clear(&addr, numpages,
1103 __pgprot(_PAGE_CACHE_MASK), 0);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001104}
venkatesh.pallipadi@intel.com12193332008-03-18 17:00:18 -07001105
1106int set_memory_wb(unsigned long addr, int numpages)
1107{
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001108 int ret;
1109
1110 ret = _set_memory_wb(addr, numpages);
1111 if (ret)
1112 return ret;
1113
venkatesh.pallipadi@intel.comc15238d2008-08-20 16:45:51 -07001114 free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001115 return 0;
venkatesh.pallipadi@intel.com12193332008-03-18 17:00:18 -07001116}
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001117EXPORT_SYMBOL(set_memory_wb);
1118
Shaohua Lid75586a2008-08-21 10:46:06 +08001119int set_memory_array_wb(unsigned long *addr, int addrinarray)
1120{
1121 int i;
venkatesh.pallipadi@intel.coma5593e02009-04-09 14:26:48 -07001122 int ret;
1123
1124 ret = change_page_attr_clear(addr, addrinarray,
1125 __pgprot(_PAGE_CACHE_MASK), 1);
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001126 if (ret)
1127 return ret;
Shaohua Lid75586a2008-08-21 10:46:06 +08001128
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001129 for (i = 0; i < addrinarray; i++)
1130 free_memtype(__pa(addr[i]), __pa(addr[i]) + PAGE_SIZE);
Rene Hermanc5e147c2008-08-22 01:02:20 +02001131
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001132 return 0;
Shaohua Lid75586a2008-08-21 10:46:06 +08001133}
1134EXPORT_SYMBOL(set_memory_array_wb);
1135
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001136int set_memory_x(unsigned long addr, int numpages)
1137{
H. Peter Anvin583140a2009-11-13 15:28:15 -08001138 if (!(__supported_pte_mask & _PAGE_NX))
1139 return 0;
1140
Shaohua Lid75586a2008-08-21 10:46:06 +08001141 return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_NX), 0);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001142}
1143EXPORT_SYMBOL(set_memory_x);
1144
1145int set_memory_nx(unsigned long addr, int numpages)
1146{
H. Peter Anvin583140a2009-11-13 15:28:15 -08001147 if (!(__supported_pte_mask & _PAGE_NX))
1148 return 0;
1149
Shaohua Lid75586a2008-08-21 10:46:06 +08001150 return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_NX), 0);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001151}
1152EXPORT_SYMBOL(set_memory_nx);
1153
1154int set_memory_ro(unsigned long addr, int numpages)
1155{
Shaohua Lid75586a2008-08-21 10:46:06 +08001156 return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_RW), 0);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001157}
Bruce Allana03352d2008-09-29 20:19:22 -07001158EXPORT_SYMBOL_GPL(set_memory_ro);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001159
1160int set_memory_rw(unsigned long addr, int numpages)
1161{
Shaohua Lid75586a2008-08-21 10:46:06 +08001162 return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_RW), 0);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001163}
Bruce Allana03352d2008-09-29 20:19:22 -07001164EXPORT_SYMBOL_GPL(set_memory_rw);
Ingo Molnarf62d0f02008-01-30 13:34:07 +01001165
1166int set_memory_np(unsigned long addr, int numpages)
1167{
Shaohua Lid75586a2008-08-21 10:46:06 +08001168 return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_PRESENT), 0);
Ingo Molnarf62d0f02008-01-30 13:34:07 +01001169}
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001170
Andi Kleenc9caa022008-03-12 03:53:29 +01001171int set_memory_4k(unsigned long addr, int numpages)
1172{
Shaohua Lid75586a2008-08-21 10:46:06 +08001173 return change_page_attr_set_clr(&addr, numpages, __pgprot(0),
venkatesh.pallipadi@intel.com9ae28472009-03-19 14:51:14 -07001174 __pgprot(0), 1, 0, NULL);
Andi Kleenc9caa022008-03-12 03:53:29 +01001175}
1176
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001177int set_pages_uc(struct page *page, int numpages)
1178{
1179 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001180
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +01001181 return set_memory_uc(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001182}
1183EXPORT_SYMBOL(set_pages_uc);
1184
Pauli Nieminen4f646252010-04-01 12:45:01 +00001185static int _set_pages_array(struct page **pages, int addrinarray,
1186 unsigned long new_type)
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001187{
1188 unsigned long start;
1189 unsigned long end;
1190 int i;
1191 int free_idx;
Pauli Nieminen4f646252010-04-01 12:45:01 +00001192 int ret;
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001193
1194 for (i = 0; i < addrinarray; i++) {
Thomas Hellstrom8523acf2009-08-03 09:25:45 +02001195 if (PageHighMem(pages[i]))
1196 continue;
1197 start = page_to_pfn(pages[i]) << PAGE_SHIFT;
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001198 end = start + PAGE_SIZE;
Pauli Nieminen4f646252010-04-01 12:45:01 +00001199 if (reserve_memtype(start, end, new_type, NULL))
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001200 goto err_out;
1201 }
1202
Pauli Nieminen4f646252010-04-01 12:45:01 +00001203 ret = cpa_set_pages_array(pages, addrinarray,
1204 __pgprot(_PAGE_CACHE_UC_MINUS));
1205 if (!ret && new_type == _PAGE_CACHE_WC)
1206 ret = change_page_attr_set_clr(NULL, addrinarray,
1207 __pgprot(_PAGE_CACHE_WC),
1208 __pgprot(_PAGE_CACHE_MASK),
1209 0, CPA_PAGES_ARRAY, pages);
1210 if (ret)
1211 goto err_out;
1212 return 0; /* Success */
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001213err_out:
1214 free_idx = i;
1215 for (i = 0; i < free_idx; i++) {
Thomas Hellstrom8523acf2009-08-03 09:25:45 +02001216 if (PageHighMem(pages[i]))
1217 continue;
1218 start = page_to_pfn(pages[i]) << PAGE_SHIFT;
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001219 end = start + PAGE_SIZE;
1220 free_memtype(start, end);
1221 }
1222 return -EINVAL;
1223}
Pauli Nieminen4f646252010-04-01 12:45:01 +00001224
1225int set_pages_array_uc(struct page **pages, int addrinarray)
1226{
1227 return _set_pages_array(pages, addrinarray, _PAGE_CACHE_UC_MINUS);
1228}
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001229EXPORT_SYMBOL(set_pages_array_uc);
1230
Pauli Nieminen4f646252010-04-01 12:45:01 +00001231int set_pages_array_wc(struct page **pages, int addrinarray)
1232{
1233 return _set_pages_array(pages, addrinarray, _PAGE_CACHE_WC);
1234}
1235EXPORT_SYMBOL(set_pages_array_wc);
1236
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001237int set_pages_wb(struct page *page, int numpages)
1238{
1239 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001240
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +01001241 return set_memory_wb(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001242}
1243EXPORT_SYMBOL(set_pages_wb);
1244
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001245int set_pages_array_wb(struct page **pages, int addrinarray)
1246{
1247 int retval;
1248 unsigned long start;
1249 unsigned long end;
1250 int i;
1251
1252 retval = cpa_clear_pages_array(pages, addrinarray,
1253 __pgprot(_PAGE_CACHE_MASK));
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001254 if (retval)
1255 return retval;
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001256
1257 for (i = 0; i < addrinarray; i++) {
Thomas Hellstrom8523acf2009-08-03 09:25:45 +02001258 if (PageHighMem(pages[i]))
1259 continue;
1260 start = page_to_pfn(pages[i]) << PAGE_SHIFT;
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001261 end = start + PAGE_SIZE;
1262 free_memtype(start, end);
1263 }
1264
venkatesh.pallipadi@intel.com9fa3ab32009-04-09 14:26:49 -07001265 return 0;
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -07001266}
1267EXPORT_SYMBOL(set_pages_array_wb);
1268
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001269int set_pages_x(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_x(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001274}
1275EXPORT_SYMBOL(set_pages_x);
1276
1277int set_pages_nx(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_nx(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001282}
1283EXPORT_SYMBOL(set_pages_nx);
1284
1285int set_pages_ro(struct page *page, int numpages)
1286{
1287 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001288
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +01001289 return set_memory_ro(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001290}
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001291
1292int set_pages_rw(struct page *page, int numpages)
1293{
1294 unsigned long addr = (unsigned long)page_address(page);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001295
Thomas Gleixnerd7c8f212008-01-30 13:34:07 +01001296 return set_memory_rw(addr, numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001297}
Arjan van de Ven75cbade2008-01-30 13:34:06 +01001298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299#ifdef CONFIG_DEBUG_PAGEALLOC
Ingo Molnarf62d0f02008-01-30 13:34:07 +01001300
1301static int __set_pages_p(struct page *page, int numpages)
1302{
Shaohua Lid75586a2008-08-21 10:46:06 +08001303 unsigned long tempaddr = (unsigned long) page_address(page);
1304 struct cpa_data cpa = { .vaddr = &tempaddr,
Thomas Gleixner72e458d2008-02-04 16:48:07 +01001305 .numpages = numpages,
1306 .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW),
Shaohua Lid75586a2008-08-21 10:46:06 +08001307 .mask_clr = __pgprot(0),
1308 .flags = 0};
Thomas Gleixner72932c72008-01-30 13:34:08 +01001309
Suresh Siddha55121b42008-09-23 14:00:40 -07001310 /*
1311 * No alias checking needed for setting present flag. otherwise,
1312 * we may need to break large pages for 64-bit kernel text
1313 * mappings (this adds to complexity if we want to do this from
1314 * atomic context especially). Let's keep it simple!
1315 */
1316 return __change_page_attr_set_clr(&cpa, 0);
Ingo Molnarf62d0f02008-01-30 13:34:07 +01001317}
1318
1319static int __set_pages_np(struct page *page, int numpages)
1320{
Shaohua Lid75586a2008-08-21 10:46:06 +08001321 unsigned long tempaddr = (unsigned long) page_address(page);
1322 struct cpa_data cpa = { .vaddr = &tempaddr,
Thomas Gleixner72e458d2008-02-04 16:48:07 +01001323 .numpages = numpages,
1324 .mask_set = __pgprot(0),
Shaohua Lid75586a2008-08-21 10:46:06 +08001325 .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW),
1326 .flags = 0};
Thomas Gleixner72932c72008-01-30 13:34:08 +01001327
Suresh Siddha55121b42008-09-23 14:00:40 -07001328 /*
1329 * No alias checking needed for setting not present flag. otherwise,
1330 * we may need to break large pages for 64-bit kernel text
1331 * mappings (this adds to complexity if we want to do this from
1332 * atomic context especially). Let's keep it simple!
1333 */
1334 return __change_page_attr_set_clr(&cpa, 0);
Ingo Molnarf62d0f02008-01-30 13:34:07 +01001335}
1336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337void kernel_map_pages(struct page *page, int numpages, int enable)
1338{
1339 if (PageHighMem(page))
1340 return;
Ingo Molnar9f4c8152008-01-30 13:33:41 +01001341 if (!enable) {
Ingo Molnarf9b84042006-06-27 02:54:49 -07001342 debug_check_no_locks_freed(page_address(page),
1343 numpages * PAGE_SIZE);
Ingo Molnar9f4c8152008-01-30 13:33:41 +01001344 }
Ingo Molnarde5097c2006-01-09 15:59:21 -08001345
Ingo Molnar9f4c8152008-01-30 13:33:41 +01001346 /*
Ingo Molnar12d6f212008-01-30 13:33:58 +01001347 * If page allocator is not up yet then do not call c_p_a():
1348 */
1349 if (!debug_pagealloc_enabled)
1350 return;
1351
1352 /*
Ingo Molnarf8d84062008-02-13 14:09:53 +01001353 * The return value is ignored as the calls cannot fail.
Suresh Siddha55121b42008-09-23 14:00:40 -07001354 * Large pages for identity mappings are not used at boot time
1355 * and hence no memory allocations during large page split.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 */
Ingo Molnarf62d0f02008-01-30 13:34:07 +01001357 if (enable)
1358 __set_pages_p(page, numpages);
1359 else
1360 __set_pages_np(page, numpages);
Ingo Molnar9f4c8152008-01-30 13:33:41 +01001361
1362 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +01001363 * We should perform an IPI and flush all tlbs,
1364 * but that can deadlock->flush only current cpu:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 */
1366 __flush_tlb_all();
1367}
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +01001368
1369#ifdef CONFIG_HIBERNATION
1370
1371bool kernel_page_present(struct page *page)
1372{
1373 unsigned int level;
1374 pte_t *pte;
1375
1376 if (PageHighMem(page))
1377 return false;
1378
1379 pte = lookup_address((unsigned long)page_address(page), &level);
1380 return (pte_val(*pte) & _PAGE_PRESENT);
1381}
1382
1383#endif /* CONFIG_HIBERNATION */
1384
1385#endif /* CONFIG_DEBUG_PAGEALLOC */
Arjan van de Vend1028a12008-01-30 13:34:07 +01001386
1387/*
1388 * The testcases use internal knowledge of the implementation that shouldn't
1389 * be exposed to the rest of the kernel. Include these directly here.
1390 */
1391#ifdef CONFIG_CPA_DEBUG
1392#include "pageattr-test.c"
1393#endif