blob: e5910ac37e599a628bcbc11695401d8fd69ba1d9 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/slab.h>
Ingo Molnar9f4c8152008-01-30 13:33:41 +010010#include <linux/mm.h>
11
Thomas Gleixner950f9d92008-01-30 13:34:06 +010012#include <asm/e820.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <asm/processor.h>
14#include <asm/tlbflush.h>
Dave Jonesf8af0952006-01-06 00:12:10 -080015#include <asm/sections.h>
Ingo Molnar9f4c8152008-01-30 13:33:41 +010016#include <asm/uaccess.h>
17#include <asm/pgalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Arjan van de Vened724be2008-01-30 13:34:04 +010019static inline int
20within(unsigned long addr, unsigned long start, unsigned long end)
Ingo Molnar687c4822008-01-30 13:34:04 +010021{
Arjan van de Vened724be2008-01-30 13:34:04 +010022 return addr >= start && addr < end;
23}
24
25/*
26 * Certain areas of memory on x86 require very specific protection flags,
27 * for example the BIOS area or kernel text. Callers don't always get this
28 * right (again, ioremap() on BIOS memory is not uncommon) so this function
29 * checks and fixes these known static required protection bits.
30 */
31static inline pgprot_t static_protections(pgprot_t prot, unsigned long address)
32{
33 pgprot_t forbidden = __pgprot(0);
34
Ingo Molnar687c4822008-01-30 13:34:04 +010035 /*
Arjan van de Vened724be2008-01-30 13:34:04 +010036 * The BIOS area between 640k and 1Mb needs to be executable for
37 * PCI BIOS based config access (CONFIG_PCI_GOBIOS) support.
Ingo Molnar687c4822008-01-30 13:34:04 +010038 */
Arjan van de Vened724be2008-01-30 13:34:04 +010039 if (within(__pa(address), BIOS_BEGIN, BIOS_END))
40 pgprot_val(forbidden) |= _PAGE_NX;
41
42 /*
43 * The kernel text needs to be executable for obvious reasons
44 * Does not cover __inittext since that is gone later on
45 */
46 if (within(address, (unsigned long)_text, (unsigned long)_etext))
47 pgprot_val(forbidden) |= _PAGE_NX;
48
49#ifdef CONFIG_DEBUG_RODATA
50 /* The .rodata section needs to be read-only */
51 if (within(address, (unsigned long)__start_rodata,
52 (unsigned long)__end_rodata))
53 pgprot_val(forbidden) |= _PAGE_RW;
54#endif
55
56 prot = __pgprot(pgprot_val(prot) & ~pgprot_val(forbidden));
Ingo Molnar687c4822008-01-30 13:34:04 +010057
58 return prot;
59}
60
Ingo Molnarf0646e42008-01-30 13:33:43 +010061pte_t *lookup_address(unsigned long address, int *level)
Ingo Molnar9f4c8152008-01-30 13:33:41 +010062{
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 pgd_t *pgd = pgd_offset_k(address);
64 pud_t *pud;
65 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +010066
Thomas Gleixner30551bb2008-01-30 13:34:04 +010067 *level = PG_LEVEL_NONE;
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 if (pgd_none(*pgd))
70 return NULL;
71 pud = pud_offset(pgd, address);
72 if (pud_none(*pud))
73 return NULL;
74 pmd = pmd_offset(pud, address);
75 if (pmd_none(*pmd))
76 return NULL;
Thomas Gleixner30551bb2008-01-30 13:34:04 +010077
78 *level = PG_LEVEL_2M;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 if (pmd_large(*pmd))
80 return (pte_t *)pmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Thomas Gleixner30551bb2008-01-30 13:34:04 +010082 *level = PG_LEVEL_4K;
Ingo Molnar9f4c8152008-01-30 13:33:41 +010083 return pte_offset_kernel(pmd, address);
84}
85
Ingo Molnar9a3dc782008-01-30 13:33:57 +010086static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
Ingo Molnar9f4c8152008-01-30 13:33:41 +010087{
Ingo Molnar9f4c8152008-01-30 13:33:41 +010088 /* change init_mm */
89 set_pte_atomic(kpte, pte);
Ingo Molnar44af6c42008-01-30 13:34:03 +010090#ifdef CONFIG_X86_32
Ingo Molnare4b71dc2008-01-30 13:34:04 +010091 if (!SHARED_KERNEL_PMD) {
Ingo Molnar44af6c42008-01-30 13:34:03 +010092 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Ingo Molnar44af6c42008-01-30 13:34:03 +010094 for (page = pgd_list; page; page = (struct page *)page->index) {
95 pgd_t *pgd;
96 pud_t *pud;
97 pmd_t *pmd;
Ingo Molnar9f4c8152008-01-30 13:33:41 +010098
Ingo Molnar44af6c42008-01-30 13:34:03 +010099 pgd = (pgd_t *)page_address(page) + pgd_index(address);
100 pud = pud_offset(pgd, address);
101 pmd = pmd_offset(pud, address);
102 set_pte_atomic((pte_t *)pmd, pte);
103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
Ingo Molnar44af6c42008-01-30 13:34:03 +0100105#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100108static int split_large_page(pte_t *kpte, unsigned long address)
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100109{
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100110 pgprot_t ref_prot = pte_pgprot(pte_clrhuge(*kpte));
Ingo Molnar12d6f212008-01-30 13:33:58 +0100111 gfp_t gfp_flags = GFP_KERNEL;
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100112 unsigned long flags;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100113 unsigned long addr;
114 pte_t *pbase, *tmp;
115 struct page *base;
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100116 int i, level;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100117
Ingo Molnar12d6f212008-01-30 13:33:58 +0100118#ifdef CONFIG_DEBUG_PAGEALLOC
119 gfp_flags = GFP_ATOMIC;
120#endif
121 base = alloc_pages(gfp_flags, 0);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100122 if (!base)
123 return -ENOMEM;
124
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100125 spin_lock_irqsave(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100126 /*
127 * Check for races, another CPU might have split this page
128 * up for us already:
129 */
130 tmp = lookup_address(address, &level);
Ingo Molnar5508a7482008-01-30 13:33:56 +0100131 if (tmp != kpte) {
132 WARN_ON_ONCE(1);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100133 goto out_unlock;
Ingo Molnar5508a7482008-01-30 13:33:56 +0100134 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100135
136 address = __pa(address);
137 addr = address & LARGE_PAGE_MASK;
138 pbase = (pte_t *)page_address(base);
Ingo Molnar44af6c42008-01-30 13:34:03 +0100139#ifdef CONFIG_X86_32
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100140 paravirt_alloc_pt(&init_mm, page_to_pfn(base));
Ingo Molnar44af6c42008-01-30 13:34:03 +0100141#endif
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100142
143 for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE)
144 set_pte(&pbase[i], pfn_pte(addr >> PAGE_SHIFT, ref_prot));
145
146 /*
Huang, Ying4c881ca2008-01-30 13:34:04 +0100147 * Install the new, split up pagetable. Important detail here:
148 *
149 * On Intel the NX bit of all levels must be cleared to make a
150 * page executable. See section 4.13.2 of Intel 64 and IA-32
151 * Architectures Software Developer's Manual).
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100152 */
Huang, Ying4c881ca2008-01-30 13:34:04 +0100153 ref_prot = pte_pgprot(pte_mkexec(pte_clrhuge(*kpte)));
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100154 __set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100155 base = NULL;
156
157out_unlock:
Ingo Molnar9a3dc782008-01-30 13:33:57 +0100158 spin_unlock_irqrestore(&pgd_lock, flags);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100159
160 if (base)
161 __free_pages(base, 0);
162
163 return 0;
164}
165
Ingo Molnar44af6c42008-01-30 13:34:03 +0100166static int
Ingo Molnar81922062008-01-30 13:34:04 +0100167__change_page_attr(unsigned long address, unsigned long pfn, pgprot_t prot)
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100168{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 struct page *kpte_page;
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100170 int level, err = 0;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100171 pte_t *kpte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Ingo Molnar81922062008-01-30 13:34:04 +0100173#ifdef CONFIG_X86_32
174 BUG_ON(pfn > max_low_pfn);
175#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Ingo Molnar97f99fe2008-01-30 13:33:55 +0100177repeat:
Ingo Molnarf0646e42008-01-30 13:33:43 +0100178 kpte = lookup_address(address, &level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 if (!kpte)
180 return -EINVAL;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 kpte_page = virt_to_page(kpte);
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200183 BUG_ON(PageLRU(kpte_page));
184 BUG_ON(PageCompound(kpte_page));
185
Arjan van de Vened724be2008-01-30 13:34:04 +0100186 prot = static_protections(prot, address);
Andi Kleen65d2f0b2007-07-21 17:09:51 +0200187
Thomas Gleixner30551bb2008-01-30 13:34:04 +0100188 if (level == PG_LEVEL_4K) {
Ingo Molnar81922062008-01-30 13:34:04 +0100189 set_pte_atomic(kpte, pfn_pte(pfn, canon_pgprot(prot)));
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100190 } else {
Ingo Molnar7afe15b2008-01-30 13:33:57 +0100191 err = split_large_page(kpte, address);
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100192 if (!err)
193 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
Ingo Molnarbb5c2db2008-01-30 13:33:56 +0100195 return err;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100196}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Ingo Molnar44af6c42008-01-30 13:34:03 +0100198/**
199 * change_page_attr_addr - Change page table attributes in linear mapping
200 * @address: Virtual address in linear mapping.
201 * @numpages: Number of pages to change
202 * @prot: New page table attribute (PAGE_*)
203 *
204 * Change page attributes of a page in the direct mapping. This is a variant
205 * of change_page_attr() that also works on memory holes that do not have
206 * mem_map entry (pfn_valid() is false).
207 *
208 * See change_page_attr() documentation for more details.
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100209 *
210 * Modules and drivers should use the set_memory_* APIs instead.
Ingo Molnar44af6c42008-01-30 13:34:03 +0100211 */
212
213int change_page_attr_addr(unsigned long address, int numpages, pgprot_t prot)
214{
215 int err = 0, kernel_map = 0, i;
216
217#ifdef CONFIG_X86_64
218 if (address >= __START_KERNEL_map &&
219 address < __START_KERNEL_map + KERNEL_TEXT_SIZE) {
220
221 address = (unsigned long)__va(__pa(address));
222 kernel_map = 1;
223 }
224#endif
225
226 for (i = 0; i < numpages; i++, address += PAGE_SIZE) {
227 unsigned long pfn = __pa(address) >> PAGE_SHIFT;
228
229 if (!kernel_map || pte_present(pfn_pte(0, prot))) {
Ingo Molnar81922062008-01-30 13:34:04 +0100230 err = __change_page_attr(address, pfn, prot);
Ingo Molnar44af6c42008-01-30 13:34:03 +0100231 if (err)
232 break;
233 }
234#ifdef CONFIG_X86_64
235 /*
236 * Handle kernel mapping too which aliases part of
237 * lowmem:
238 */
239 if (__pa(address) < KERNEL_TEXT_SIZE) {
240 unsigned long addr2;
241 pgprot_t prot2;
242
243 addr2 = __START_KERNEL_map + __pa(address);
244 /* Make sure the kernel mappings stay executable */
245 prot2 = pte_pgprot(pte_mkexec(pfn_pte(0, prot)));
Ingo Molnar81922062008-01-30 13:34:04 +0100246 err = __change_page_attr(addr2, pfn, prot2);
Ingo Molnar44af6c42008-01-30 13:34:03 +0100247 }
248#endif
249 }
250
251 return err;
252}
253
254/**
255 * change_page_attr - Change page table attributes in the linear mapping.
256 * @page: First page to change
257 * @numpages: Number of pages to change
258 * @prot: New protection/caching type (PAGE_*)
259 *
260 * Returns 0 on success, otherwise a negated errno.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 *
262 * This should be used when a page is mapped with a different caching policy
263 * than write-back somewhere - some CPUs do not like it when mappings with
264 * different caching policies exist. This changes the page attributes of the
265 * in kernel linear mapping too.
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100266 *
Ingo Molnar44af6c42008-01-30 13:34:03 +0100267 * Caller must call global_flush_tlb() later to make the changes active.
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100268 *
Ingo Molnar44af6c42008-01-30 13:34:03 +0100269 * The caller needs to ensure that there are no conflicting mappings elsewhere
270 * (e.g. in user space) * This function only deals with the kernel linear map.
271 *
272 * For MMIO areas without mem_map use change_page_attr_addr() instead.
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100273 *
274 * Modules and drivers should use the set_pages_* APIs instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 */
276int change_page_attr(struct page *page, int numpages, pgprot_t prot)
277{
Ingo Molnar44af6c42008-01-30 13:34:03 +0100278 unsigned long addr = (unsigned long)page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Ingo Molnar44af6c42008-01-30 13:34:03 +0100280 return change_page_attr_addr(addr, numpages, prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
Arjan van de Vene1271f62008-01-30 13:34:06 +0100282EXPORT_UNUSED_SYMBOL(change_page_attr); /* to be removed in 2.6.27 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100284/**
285 * change_page_attr_set - Change page table attributes in the linear mapping.
286 * @addr: Virtual address in linear mapping.
287 * @numpages: Number of pages to change
288 * @prot: Protection/caching type bits to set (PAGE_*)
289 *
290 * Returns 0 on success, otherwise a negated errno.
291 *
292 * This should be used when a page is mapped with a different caching policy
293 * than write-back somewhere - some CPUs do not like it when mappings with
294 * different caching policies exist. This changes the page attributes of the
295 * in kernel linear mapping too.
296 *
297 * Caller must call global_flush_tlb() later to make the changes active.
298 *
299 * The caller needs to ensure that there are no conflicting mappings elsewhere
300 * (e.g. in user space) * This function only deals with the kernel linear map.
301 *
302 * This function is different from change_page_attr() in that only selected bits
303 * are impacted, all other bits remain as is.
304 */
305int change_page_attr_set(unsigned long addr, int numpages, pgprot_t prot)
306{
307 pgprot_t current_prot;
308 int level;
309 pte_t *pte;
310
311 pte = lookup_address(addr, &level);
312 if (pte)
313 current_prot = pte_pgprot(*pte);
314 else
315 pgprot_val(current_prot) = 0;
316
317 pgprot_val(prot) = pgprot_val(current_prot) | pgprot_val(prot);
318
319 return change_page_attr_addr(addr, numpages, prot);
320}
321
322/**
323 * change_page_attr_clear - Change page table attributes in the linear mapping.
324 * @addr: Virtual address in linear mapping.
325 * @numpages: Number of pages to change
326 * @prot: Protection/caching type bits to clear (PAGE_*)
327 *
328 * Returns 0 on success, otherwise a negated errno.
329 *
330 * This should be used when a page is mapped with a different caching policy
331 * than write-back somewhere - some CPUs do not like it when mappings with
332 * different caching policies exist. This changes the page attributes of the
333 * in kernel linear mapping too.
334 *
335 * Caller must call global_flush_tlb() later to make the changes active.
336 *
337 * The caller needs to ensure that there are no conflicting mappings elsewhere
338 * (e.g. in user space) * This function only deals with the kernel linear map.
339 *
340 * This function is different from change_page_attr() in that only selected bits
341 * are impacted, all other bits remain as is.
342 */
343int change_page_attr_clear(unsigned long addr, int numpages, pgprot_t prot)
344{
345 pgprot_t current_prot;
346 int level;
347 pte_t *pte;
348
349 pte = lookup_address(addr, &level);
350 if (pte)
351 current_prot = pte_pgprot(*pte);
352 else
353 pgprot_val(current_prot) = 0;
354
355 pgprot_val(prot) = pgprot_val(current_prot) & ~pgprot_val(prot);
356
357 return change_page_attr_addr(addr, numpages, prot);
358}
359
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100360int set_memory_uc(unsigned long addr, int numpages)
361{
362 pgprot_t uncached;
363
364 pgprot_val(uncached) = _PAGE_PCD | _PAGE_PWT;
365 return change_page_attr_set(addr, numpages, uncached);
366}
367EXPORT_SYMBOL(set_memory_uc);
368
369int set_memory_wb(unsigned long addr, int numpages)
370{
371 pgprot_t uncached;
372
373 pgprot_val(uncached) = _PAGE_PCD | _PAGE_PWT;
374 return change_page_attr_clear(addr, numpages, uncached);
375}
376EXPORT_SYMBOL(set_memory_wb);
377
378int set_memory_x(unsigned long addr, int numpages)
379{
380 pgprot_t nx;
381
382 pgprot_val(nx) = _PAGE_NX;
383 return change_page_attr_clear(addr, numpages, nx);
384}
385EXPORT_SYMBOL(set_memory_x);
386
387int set_memory_nx(unsigned long addr, int numpages)
388{
389 pgprot_t nx;
390
391 pgprot_val(nx) = _PAGE_NX;
392 return change_page_attr_set(addr, numpages, nx);
393}
394EXPORT_SYMBOL(set_memory_nx);
395
396int set_memory_ro(unsigned long addr, int numpages)
397{
398 pgprot_t rw;
399
400 pgprot_val(rw) = _PAGE_RW;
401 return change_page_attr_clear(addr, numpages, rw);
402}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100403
404int set_memory_rw(unsigned long addr, int numpages)
405{
406 pgprot_t rw;
407
408 pgprot_val(rw) = _PAGE_RW;
409 return change_page_attr_set(addr, numpages, rw);
410}
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100411
412int set_memory_np(unsigned long addr, int numpages)
413{
414 pgprot_t present;
415
416 pgprot_val(present) = _PAGE_PRESENT;
417 return change_page_attr_clear(addr, numpages, present);
418}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100419
420int set_pages_uc(struct page *page, int numpages)
421{
422 unsigned long addr = (unsigned long)page_address(page);
423 pgprot_t uncached;
424
425 pgprot_val(uncached) = _PAGE_PCD | _PAGE_PWT;
426 return change_page_attr_set(addr, numpages, uncached);
427}
428EXPORT_SYMBOL(set_pages_uc);
429
430int set_pages_wb(struct page *page, int numpages)
431{
432 unsigned long addr = (unsigned long)page_address(page);
433 pgprot_t uncached;
434
435 pgprot_val(uncached) = _PAGE_PCD | _PAGE_PWT;
436 return change_page_attr_clear(addr, numpages, uncached);
437}
438EXPORT_SYMBOL(set_pages_wb);
439
440int set_pages_x(struct page *page, int numpages)
441{
442 unsigned long addr = (unsigned long)page_address(page);
443 pgprot_t nx;
444
445 pgprot_val(nx) = _PAGE_NX;
446 return change_page_attr_clear(addr, numpages, nx);
447}
448EXPORT_SYMBOL(set_pages_x);
449
450int set_pages_nx(struct page *page, int numpages)
451{
452 unsigned long addr = (unsigned long)page_address(page);
453 pgprot_t nx;
454
455 pgprot_val(nx) = _PAGE_NX;
456 return change_page_attr_set(addr, numpages, nx);
457}
458EXPORT_SYMBOL(set_pages_nx);
459
460int set_pages_ro(struct page *page, int numpages)
461{
462 unsigned long addr = (unsigned long)page_address(page);
463 pgprot_t rw;
464
465 pgprot_val(rw) = _PAGE_RW;
466 return change_page_attr_clear(addr, numpages, rw);
467}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100468
469int set_pages_rw(struct page *page, int numpages)
470{
471 unsigned long addr = (unsigned long)page_address(page);
472 pgprot_t rw;
473
474 pgprot_val(rw) = _PAGE_RW;
475 return change_page_attr_set(addr, numpages, rw);
476}
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100477
Ingo Molnare81d5dc2008-01-30 13:34:06 +0100478void clflush_cache_range(void *addr, int size)
479{
480 int i;
481
482 for (i = 0; i < size; i += boot_cpu_data.x86_clflush_size)
483 clflush(addr+i);
484}
485
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100486static void flush_kernel_map(void *arg)
487{
488 /*
489 * Flush all to work around Errata in early athlons regarding
490 * large page flushing.
491 */
492 __flush_tlb_all();
493
494 if (boot_cpu_data.x86_model >= 4)
495 wbinvd();
496}
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498void global_flush_tlb(void)
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700499{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 BUG_ON(irqs_disabled());
501
Ingo Molnar78c94ab2008-01-30 13:33:55 +0100502 on_each_cpu(flush_kernel_map, NULL, 1, 1);
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700503}
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100504EXPORT_SYMBOL(global_flush_tlb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506#ifdef CONFIG_DEBUG_PAGEALLOC
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100507
508static int __set_pages_p(struct page *page, int numpages)
509{
510 unsigned long addr = (unsigned long)page_address(page);
511 return change_page_attr_set(addr, numpages,
512 __pgprot(_PAGE_PRESENT | _PAGE_RW));
513}
514
515static int __set_pages_np(struct page *page, int numpages)
516{
517 unsigned long addr = (unsigned long)page_address(page);
518 return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_PRESENT));
519}
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521void kernel_map_pages(struct page *page, int numpages, int enable)
522{
523 if (PageHighMem(page))
524 return;
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100525 if (!enable) {
Ingo Molnarf9b84042006-06-27 02:54:49 -0700526 debug_check_no_locks_freed(page_address(page),
527 numpages * PAGE_SIZE);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100528 }
Ingo Molnarde5097c2006-01-09 15:59:21 -0800529
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100530 /*
Ingo Molnar12d6f212008-01-30 13:33:58 +0100531 * If page allocator is not up yet then do not call c_p_a():
532 */
533 if (!debug_pagealloc_enabled)
534 return;
535
536 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100537 * The return value is ignored - the calls cannot fail,
538 * large pages are disabled at boot time:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 */
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100540 if (enable)
541 __set_pages_p(page, numpages);
542 else
543 __set_pages_np(page, numpages);
Ingo Molnar9f4c8152008-01-30 13:33:41 +0100544
545 /*
Ingo Molnare4b71dc2008-01-30 13:34:04 +0100546 * We should perform an IPI and flush all tlbs,
547 * but that can deadlock->flush only current cpu:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 */
549 __flush_tlb_all();
550}
551#endif