blob: 3eeab6d0065ffa52d0945d349bb755fd4ce5f8ab [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * Copyright (C) 1995 Linus Torvalds
4 *
5 * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999
6 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/module.h>
9#include <linux/signal.h>
10#include <linux/sched.h>
11#include <linux/kernel.h>
12#include <linux/errno.h>
13#include <linux/string.h>
14#include <linux/types.h>
15#include <linux/ptrace.h>
16#include <linux/mman.h>
17#include <linux/mm.h>
18#include <linux/hugetlb.h>
19#include <linux/swap.h>
20#include <linux/smp.h>
21#include <linux/init.h>
22#include <linux/highmem.h>
23#include <linux/pagemap.h>
Jan Beulich6fb14752007-05-02 19:27:10 +020024#include <linux/pfn.h>
Randy Dunlapc9cf5522006-06-27 02:53:52 -070025#include <linux/poison.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/bootmem.h>
27#include <linux/slab.h>
28#include <linux/proc_fs.h>
Dave Hansen05039b92005-10-29 18:16:57 -070029#include <linux/memory_hotplug.h>
Adrian Bunk27d99f72005-11-13 16:06:51 -080030#include <linux/initrd.h>
Shaohua Li55b23552006-06-23 02:04:49 -070031#include <linux/cpumask.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
H. Peter Anvinf832ff12008-02-04 16:47:58 +010033#include <asm/asm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/processor.h>
35#include <asm/system.h>
36#include <asm/uaccess.h>
37#include <asm/pgtable.h>
38#include <asm/dma.h>
39#include <asm/fixmap.h>
40#include <asm/e820.h>
41#include <asm/apic.h>
Ingo Molnar8550eb92008-01-30 13:34:10 +010042#include <asm/bugs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/tlb.h>
44#include <asm/tlbflush.h>
Jeremy Fitzhardingea5a19c62008-01-30 13:33:39 +010045#include <asm/pgalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/sections.h>
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +020047#include <asm/paravirt.h>
Ian Campbell551889a2008-02-09 23:24:09 +010048#include <asm/setup.h>
Harvey Harrison7bfeab92008-02-12 12:12:01 -080049#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51unsigned int __VMALLOC_RESERVE = 128 << 20;
52
Yinghai Luf361a452008-07-10 20:38:26 -070053unsigned long max_low_pfn_mapped;
Thomas Gleixner67794292008-03-21 21:27:10 +010054unsigned long max_pfn_mapped;
Andi Kleen7d1116a2008-03-12 03:53:27 +010055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
57unsigned long highstart_pfn, highend_pfn;
58
Ingo Molnar8550eb92008-01-30 13:34:10 +010059static noinline int do_test_wp_bit(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Yinghai Lu4e296842008-06-24 12:18:14 -070061
62static unsigned long __initdata table_start;
63static unsigned long __meminitdata table_end;
64static unsigned long __meminitdata table_top;
65
66static int __initdata after_init_bootmem;
67
68static __init void *alloc_low_page(unsigned long *phys)
69{
70 unsigned long pfn = table_end++;
71 void *adr;
72
73 if (pfn >= table_top)
74 panic("alloc_low_page: ran out of memory");
75
76 adr = __va(pfn * PAGE_SIZE);
77 memset(adr, 0, PAGE_SIZE);
78 *phys = pfn * PAGE_SIZE;
79 return adr;
80}
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/*
83 * Creates a middle page table and puts a pointer to it in the
84 * given global directory entry. This only returns the gd entry
85 * in non-PAE compilation mode, since the middle layer is folded.
86 */
87static pmd_t * __init one_md_table_init(pgd_t *pgd)
88{
89 pud_t *pud;
90 pmd_t *pmd_table;
Ingo Molnar8550eb92008-01-30 13:34:10 +010091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#ifdef CONFIG_X86_PAE
Yinghai Lu4e296842008-06-24 12:18:14 -070093 unsigned long phys;
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +020094 if (!(pgd_val(*pgd) & _PAGE_PRESENT)) {
Yinghai Lu4e296842008-06-24 12:18:14 -070095 if (after_init_bootmem)
96 pmd_table = (pmd_t *)alloc_bootmem_low_pages(PAGE_SIZE);
97 else
98 pmd_table = (pmd_t *)alloc_low_page(&phys);
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -070099 paravirt_alloc_pmd(&init_mm, __pa(pmd_table) >> PAGE_SHIFT);
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200100 set_pgd(pgd, __pgd(__pa(pmd_table) | _PAGE_PRESENT));
101 pud = pud_offset(pgd, 0);
Ingo Molnar8550eb92008-01-30 13:34:10 +0100102 BUG_ON(pmd_table != pmd_offset(pud, 0));
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200103 }
104#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 pud = pud_offset(pgd, 0);
106 pmd_table = pmd_offset(pud, 0);
Ingo Molnar8550eb92008-01-30 13:34:10 +0100107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return pmd_table;
109}
110
111/*
112 * Create a page table and place a pointer to it in a middle page
Ingo Molnar8550eb92008-01-30 13:34:10 +0100113 * directory entry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 */
115static pte_t * __init one_page_table_init(pmd_t *pmd)
116{
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200117 if (!(pmd_val(*pmd) & _PAGE_PRESENT)) {
Ingo Molnar509a80c2007-10-17 18:04:34 +0200118 pte_t *page_table = NULL;
119
Yinghai Lu4e296842008-06-24 12:18:14 -0700120 if (after_init_bootmem) {
Ingo Molnar509a80c2007-10-17 18:04:34 +0200121#ifdef CONFIG_DEBUG_PAGEALLOC
Yinghai Lu4e296842008-06-24 12:18:14 -0700122 page_table = (pte_t *) alloc_bootmem_pages(PAGE_SIZE);
Ingo Molnar509a80c2007-10-17 18:04:34 +0200123#endif
Yinghai Lu4e296842008-06-24 12:18:14 -0700124 if (!page_table)
125 page_table =
Ingo Molnar509a80c2007-10-17 18:04:34 +0200126 (pte_t *)alloc_bootmem_low_pages(PAGE_SIZE);
Yinghai Lu4e296842008-06-24 12:18:14 -0700127 } else {
128 unsigned long phys;
129 page_table = (pte_t *)alloc_low_page(&phys);
Ingo Molnar8550eb92008-01-30 13:34:10 +0100130 }
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200131
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -0700132 paravirt_alloc_pte(&init_mm, __pa(page_table) >> PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 set_pmd(pmd, __pmd(__pa(page_table) | _PAGE_TABLE));
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200134 BUG_ON(page_table != pte_offset_kernel(pmd, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
Ingo Molnar509a80c2007-10-17 18:04:34 +0200136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return pte_offset_kernel(pmd, 0);
138}
139
140/*
Ingo Molnar8550eb92008-01-30 13:34:10 +0100141 * This function initializes a certain range of kernel virtual memory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 * with new bootmem page tables, everywhere page tables are missing in
143 * the given range.
Ingo Molnar8550eb92008-01-30 13:34:10 +0100144 *
145 * NOTE: The pagetables are allocated contiguous on the physical space
146 * so we can cache the place of the first one and move around without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 * checking the pgd every time.
148 */
Ingo Molnar8550eb92008-01-30 13:34:10 +0100149static void __init
150page_table_range_init(unsigned long start, unsigned long end, pgd_t *pgd_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 int pgd_idx, pmd_idx;
153 unsigned long vaddr;
Ingo Molnar8550eb92008-01-30 13:34:10 +0100154 pgd_t *pgd;
155 pmd_t *pmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 vaddr = start;
158 pgd_idx = pgd_index(vaddr);
159 pmd_idx = pmd_index(vaddr);
160 pgd = pgd_base + pgd_idx;
161
162 for ( ; (pgd_idx < PTRS_PER_PGD) && (vaddr != end); pgd++, pgd_idx++) {
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200163 pmd = one_md_table_init(pgd);
164 pmd = pmd + pmd_index(vaddr);
Ingo Molnar8550eb92008-01-30 13:34:10 +0100165 for (; (pmd_idx < PTRS_PER_PMD) && (vaddr != end);
166 pmd++, pmd_idx++) {
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200167 one_page_table_init(pmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 vaddr += PMD_SIZE;
170 }
171 pmd_idx = 0;
172 }
173}
174
175static inline int is_kernel_text(unsigned long addr)
176{
177 if (addr >= PAGE_OFFSET && addr <= (unsigned long)__init_end)
178 return 1;
179 return 0;
180}
181
182/*
Ingo Molnar8550eb92008-01-30 13:34:10 +0100183 * This maps the physical memory to kernel virtual address space, a total
184 * of max_low_pfn pages, by creating page tables starting from address
185 * PAGE_OFFSET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 */
Yinghai Lu4e296842008-06-24 12:18:14 -0700187static void __init kernel_physical_mapping_init(pgd_t *pgd_base,
Yinghai Lua04ad822008-06-29 00:39:06 -0700188 unsigned long start_pfn,
189 unsigned long end_pfn,
190 int use_pse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Ingo Molnar8550eb92008-01-30 13:34:10 +0100192 int pgd_idx, pmd_idx, pte_ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 unsigned long pfn;
194 pgd_t *pgd;
195 pmd_t *pmd;
196 pte_t *pte;
Andi Kleence0c0e52008-05-02 11:46:49 +0200197 unsigned pages_2m = 0, pages_4k = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Yinghai Lua04ad822008-06-29 00:39:06 -0700199 if (!cpu_has_pse)
200 use_pse = 0;
201
202 pfn = start_pfn;
203 pgd_idx = pgd_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 pgd = pgd_base + pgd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 for (; pgd_idx < PTRS_PER_PGD; pgd++, pgd_idx++) {
206 pmd = one_md_table_init(pgd);
Ingo Molnar8550eb92008-01-30 13:34:10 +0100207
Yinghai Lua04ad822008-06-29 00:39:06 -0700208 if (pfn >= end_pfn)
209 continue;
210#ifdef CONFIG_X86_PAE
211 pmd_idx = pmd_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
212 pmd += pmd_idx;
213#else
214 pmd_idx = 0;
215#endif
216 for (; pmd_idx < PTRS_PER_PMD && pfn < end_pfn;
Jeremy Fitzhardingef3f20de2008-01-30 13:31:09 +0100217 pmd++, pmd_idx++) {
Ingo Molnar8550eb92008-01-30 13:34:10 +0100218 unsigned int addr = pfn * PAGE_SIZE + PAGE_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Ingo Molnar8550eb92008-01-30 13:34:10 +0100220 /*
221 * Map with big pages if possible, otherwise
222 * create normal page tables:
223 */
Yinghai Lua04ad822008-06-29 00:39:06 -0700224 if (use_pse) {
Ingo Molnar8550eb92008-01-30 13:34:10 +0100225 unsigned int addr2;
Jeremy Fitzhardingef3f20de2008-01-30 13:31:09 +0100226 pgprot_t prot = PAGE_KERNEL_LARGE;
227
Ingo Molnar8550eb92008-01-30 13:34:10 +0100228 addr2 = (pfn + PTRS_PER_PTE-1) * PAGE_SIZE +
Jeremy Fitzhardingef3f20de2008-01-30 13:31:09 +0100229 PAGE_OFFSET + PAGE_SIZE-1;
230
Ingo Molnar8550eb92008-01-30 13:34:10 +0100231 if (is_kernel_text(addr) ||
232 is_kernel_text(addr2))
Jeremy Fitzhardingef3f20de2008-01-30 13:31:09 +0100233 prot = PAGE_KERNEL_LARGE_EXEC;
234
Andi Kleence0c0e52008-05-02 11:46:49 +0200235 pages_2m++;
Jeremy Fitzhardingef3f20de2008-01-30 13:31:09 +0100236 set_pmd(pmd, pfn_pmd(pfn, prot));
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 pfn += PTRS_PER_PTE;
Ingo Molnar8550eb92008-01-30 13:34:10 +0100239 continue;
240 }
241 pte = one_page_table_init(pmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Yinghai Lua04ad822008-06-29 00:39:06 -0700243 pte_ofs = pte_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
244 pte += pte_ofs;
245 for (; pte_ofs < PTRS_PER_PTE && pfn < end_pfn;
Ingo Molnar8550eb92008-01-30 13:34:10 +0100246 pte++, pfn++, pte_ofs++, addr += PAGE_SIZE) {
247 pgprot_t prot = PAGE_KERNEL;
Jeremy Fitzhardingef3f20de2008-01-30 13:31:09 +0100248
Ingo Molnar8550eb92008-01-30 13:34:10 +0100249 if (is_kernel_text(addr))
250 prot = PAGE_KERNEL_EXEC;
Jeremy Fitzhardingef3f20de2008-01-30 13:31:09 +0100251
Andi Kleence0c0e52008-05-02 11:46:49 +0200252 pages_4k++;
Ingo Molnar8550eb92008-01-30 13:34:10 +0100253 set_pte(pte, pfn_pte(pfn, prot));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
255 }
256 }
Andi Kleence0c0e52008-05-02 11:46:49 +0200257 update_page_count(PG_LEVEL_2M, pages_2m);
258 update_page_count(PG_LEVEL_4K, pages_4k);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Arjan van de Venae531c22008-04-24 23:40:47 +0200261/*
262 * devmem_is_allowed() checks to see if /dev/mem access to a certain address
263 * is valid. The argument is a physical page number.
264 *
265 *
266 * On x86, access has to be given to the first megabyte of ram because that area
267 * contains bios code and data regions used by X and dosemu and similar apps.
268 * Access has to be given to non-kernel-ram areas as well, these contain the PCI
269 * mmio resources as well as potential bios/acpi data regions.
270 */
271int devmem_is_allowed(unsigned long pagenr)
272{
273 if (pagenr <= 256)
274 return 1;
275 if (!page_is_ram(pagenr))
276 return 1;
277 return 0;
278}
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280#ifdef CONFIG_HIGHMEM
281pte_t *kmap_pte;
282pgprot_t kmap_prot;
283
Ingo Molnar8550eb92008-01-30 13:34:10 +0100284static inline pte_t *kmap_get_fixmap_pte(unsigned long vaddr)
285{
286 return pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k(vaddr),
287 vaddr), vaddr), vaddr);
288}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290static void __init kmap_init(void)
291{
292 unsigned long kmap_vstart;
293
Ingo Molnar8550eb92008-01-30 13:34:10 +0100294 /*
295 * Cache the first kmap pte:
296 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN);
298 kmap_pte = kmap_get_fixmap_pte(kmap_vstart);
299
300 kmap_prot = PAGE_KERNEL;
301}
302
303static void __init permanent_kmaps_init(pgd_t *pgd_base)
304{
Ingo Molnar8550eb92008-01-30 13:34:10 +0100305 unsigned long vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 pgd_t *pgd;
307 pud_t *pud;
308 pmd_t *pmd;
309 pte_t *pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 vaddr = PKMAP_BASE;
312 page_table_range_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, pgd_base);
313
314 pgd = swapper_pg_dir + pgd_index(vaddr);
315 pud = pud_offset(pgd, vaddr);
316 pmd = pmd_offset(pud, vaddr);
317 pte = pte_offset_kernel(pmd, vaddr);
Ingo Molnar8550eb92008-01-30 13:34:10 +0100318 pkmap_page_table = pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
Yinghai Lucc9f7a02008-06-16 16:11:08 -0700321static void __init add_one_highpage_init(struct page *page, int pfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Yinghai Lucc9f7a02008-06-16 16:11:08 -0700323 ClearPageReserved(page);
324 init_page_count(page);
325 __free_page(page);
326 totalhigh_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
Yinghai Lub5bc6c02008-06-14 18:32:52 -0700329struct add_highpages_data {
330 unsigned long start_pfn;
331 unsigned long end_pfn;
Yinghai Lub5bc6c02008-06-14 18:32:52 -0700332};
333
Yinghai Lud52d53b2008-06-16 20:10:55 -0700334static int __init add_highpages_work_fn(unsigned long start_pfn,
Yinghai Lub5bc6c02008-06-14 18:32:52 -0700335 unsigned long end_pfn, void *datax)
336{
337 int node_pfn;
338 struct page *page;
339 unsigned long final_start_pfn, final_end_pfn;
340 struct add_highpages_data *data;
Yinghai Lub5bc6c02008-06-14 18:32:52 -0700341
342 data = (struct add_highpages_data *)datax;
Yinghai Lub5bc6c02008-06-14 18:32:52 -0700343
344 final_start_pfn = max(start_pfn, data->start_pfn);
345 final_end_pfn = min(end_pfn, data->end_pfn);
346 if (final_start_pfn >= final_end_pfn)
Yinghai Lud52d53b2008-06-16 20:10:55 -0700347 return 0;
Yinghai Lub5bc6c02008-06-14 18:32:52 -0700348
349 for (node_pfn = final_start_pfn; node_pfn < final_end_pfn;
350 node_pfn++) {
351 if (!pfn_valid(node_pfn))
352 continue;
353 page = pfn_to_page(node_pfn);
Yinghai Lucc9f7a02008-06-16 16:11:08 -0700354 add_one_highpage_init(page, node_pfn);
Yinghai Lub5bc6c02008-06-14 18:32:52 -0700355 }
356
Yinghai Lud52d53b2008-06-16 20:10:55 -0700357 return 0;
358
Yinghai Lub5bc6c02008-06-14 18:32:52 -0700359}
360
361void __init add_highpages_with_active_regions(int nid, unsigned long start_pfn,
Yinghai Lucc9f7a02008-06-16 16:11:08 -0700362 unsigned long end_pfn)
Yinghai Lub5bc6c02008-06-14 18:32:52 -0700363{
364 struct add_highpages_data data;
365
366 data.start_pfn = start_pfn;
367 data.end_pfn = end_pfn;
Yinghai Lub5bc6c02008-06-14 18:32:52 -0700368
369 work_with_active_regions(nid, add_highpages_work_fn, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
Ingo Molnar8550eb92008-01-30 13:34:10 +0100372#ifndef CONFIG_NUMA
Yinghai Lucc9f7a02008-06-16 16:11:08 -0700373static void __init set_highmem_pages_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Yinghai Lucc9f7a02008-06-16 16:11:08 -0700375 add_highpages_with_active_regions(0, highstart_pfn, highend_pfn);
Ingo Molnar8550eb92008-01-30 13:34:10 +0100376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 totalram_pages += totalhigh_pages;
378}
Ingo Molnar8550eb92008-01-30 13:34:10 +0100379#endif /* !CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381#else
Ingo Molnar8550eb92008-01-30 13:34:10 +0100382# define kmap_init() do { } while (0)
383# define permanent_kmaps_init(pgd_base) do { } while (0)
Yinghai Lucc9f7a02008-06-16 16:11:08 -0700384# define set_highmem_pages_init() do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385#endif /* CONFIG_HIGHMEM */
386
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200387void __init native_pagetable_setup_start(pgd_t *base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Ian Campbell551889a2008-02-09 23:24:09 +0100389 unsigned long pfn, va;
390 pgd_t *pgd;
391 pud_t *pud;
392 pmd_t *pmd;
393 pte_t *pte;
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200394
395 /*
Ian Campbell551889a2008-02-09 23:24:09 +0100396 * Remove any mappings which extend past the end of physical
397 * memory from the boot time page table:
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200398 */
Ian Campbell551889a2008-02-09 23:24:09 +0100399 for (pfn = max_low_pfn + 1; pfn < 1<<(32-PAGE_SHIFT); pfn++) {
400 va = PAGE_OFFSET + (pfn<<PAGE_SHIFT);
401 pgd = base + pgd_index(va);
402 if (!pgd_present(*pgd))
403 break;
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200404
Ian Campbell551889a2008-02-09 23:24:09 +0100405 pud = pud_offset(pgd, va);
406 pmd = pmd_offset(pud, va);
407 if (!pmd_present(*pmd))
408 break;
409
410 pte = pte_offset_kernel(pmd, va);
411 if (!pte_present(*pte))
412 break;
413
414 pte_clear(NULL, va, pte);
415 }
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -0700416 paravirt_alloc_pmd(&init_mm, __pa(base) >> PAGE_SHIFT);
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200417}
418
419void __init native_pagetable_setup_done(pgd_t *base)
420{
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200421}
422
423/*
424 * Build a proper pagetable for the kernel mappings. Up until this
425 * point, we've been running on some set of pagetables constructed by
426 * the boot process.
427 *
428 * If we're booting on native hardware, this will be a pagetable
Ian Campbell551889a2008-02-09 23:24:09 +0100429 * constructed in arch/x86/kernel/head_32.S. The root of the
430 * pagetable will be swapper_pg_dir.
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200431 *
432 * If we're booting paravirtualized under a hypervisor, then there are
433 * more options: we may already be running PAE, and the pagetable may
434 * or may not be based in swapper_pg_dir. In any case,
435 * paravirt_pagetable_setup_start() will set up swapper_pg_dir
436 * appropriately for the rest of the initialization to work.
437 *
438 * In general, pagetable_init() assumes that the pagetable may already
439 * be partially populated, and so it avoids stomping on any existing
440 * mappings.
441 */
Yinghai Lue7b37892008-06-25 21:51:28 -0700442static void __init early_ioremap_page_table_range_init(pgd_t *pgd_base)
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200443{
Ingo Molnar8550eb92008-01-30 13:34:10 +0100444 unsigned long vaddr, end;
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 /*
447 * Fixed mappings, only the page table structure has to be
448 * created - mappings will be set by set_fixmap():
449 */
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100450 early_ioremap_clear();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200452 end = (FIXADDR_TOP + PMD_SIZE - 1) & PMD_MASK;
453 page_table_range_init(vaddr, end, pgd_base);
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100454 early_ioremap_reset();
Yinghai Lue7b37892008-06-25 21:51:28 -0700455}
456
457static void __init pagetable_init(void)
458{
459 pgd_t *pgd_base = swapper_pg_dir;
460
461 paravirt_pagetable_setup_start(pgd_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 permanent_kmaps_init(pgd_base);
464
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200465 paravirt_pagetable_setup_done(pgd_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
Rafael J. Wysockia6eb84b2008-02-01 15:28:16 +0100468#ifdef CONFIG_ACPI_SLEEP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469/*
Rafael J. Wysockia6eb84b2008-02-01 15:28:16 +0100470 * ACPI suspend needs this for resume, because things like the intel-agp
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 * driver might have split up a kernel 4MB mapping.
472 */
Rafael J. Wysockia6eb84b2008-02-01 15:28:16 +0100473char swsusp_pg_dir[PAGE_SIZE]
Ingo Molnar8550eb92008-01-30 13:34:10 +0100474 __attribute__ ((aligned(PAGE_SIZE)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476static inline void save_pg_dir(void)
477{
478 memcpy(swsusp_pg_dir, swapper_pg_dir, PAGE_SIZE);
479}
Rafael J. Wysockia6eb84b2008-02-01 15:28:16 +0100480#else /* !CONFIG_ACPI_SLEEP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481static inline void save_pg_dir(void)
482{
483}
Rafael J. Wysockia6eb84b2008-02-01 15:28:16 +0100484#endif /* !CONFIG_ACPI_SLEEP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Ingo Molnar8550eb92008-01-30 13:34:10 +0100486void zap_low_mappings(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 int i;
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 /*
491 * Zap initial low-memory mappings.
492 *
493 * Note that "pgd_clear()" doesn't do it for
494 * us, because pgd_clear() is a no-op on i386.
495 */
Jeremy Fitzhardinge68db0652008-03-17 16:37:13 -0700496 for (i = 0; i < KERNEL_PGD_BOUNDARY; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497#ifdef CONFIG_X86_PAE
498 set_pgd(swapper_pg_dir+i, __pgd(1 + __pa(empty_zero_page)));
499#else
500 set_pgd(swapper_pg_dir+i, __pgd(0));
501#endif
Ingo Molnar8550eb92008-01-30 13:34:10 +0100502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 flush_tlb_all();
504}
505
Ingo Molnar8550eb92008-01-30 13:34:10 +0100506int nx_enabled;
Jan Beulichd5321ab2007-07-21 17:10:26 +0200507
Jeremy Fitzhardingeef5e94a2008-07-01 16:46:36 -0700508pteval_t __supported_pte_mask __read_mostly = ~(_PAGE_NX | _PAGE_GLOBAL);
Jeremy Fitzhardinge6fdc05d2008-01-30 13:32:57 +0100509EXPORT_SYMBOL_GPL(__supported_pte_mask);
510
Jan Beulichd5321ab2007-07-21 17:10:26 +0200511#ifdef CONFIG_X86_PAE
512
Ingo Molnar8550eb92008-01-30 13:34:10 +0100513static int disable_nx __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515/*
516 * noexec = on|off
517 *
518 * Control non executable mappings.
519 *
520 * on Enable
521 * off Disable
522 */
Rusty Russell1a3f2392006-09-26 10:52:32 +0200523static int __init noexec_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
Rusty Russell1a3f2392006-09-26 10:52:32 +0200525 if (!str || !strcmp(str, "on")) {
526 if (cpu_has_nx) {
527 __supported_pte_mask |= _PAGE_NX;
528 disable_nx = 0;
529 }
Ingo Molnar8550eb92008-01-30 13:34:10 +0100530 } else {
531 if (!strcmp(str, "off")) {
532 disable_nx = 1;
533 __supported_pte_mask &= ~_PAGE_NX;
534 } else {
535 return -EINVAL;
536 }
537 }
Rusty Russell1a3f2392006-09-26 10:52:32 +0200538
539 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
Rusty Russell1a3f2392006-09-26 10:52:32 +0200541early_param("noexec", noexec_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543static void __init set_nx(void)
544{
545 unsigned int v[4], l, h;
546
547 if (cpu_has_pae && (cpuid_eax(0x80000000) > 0x80000001)) {
548 cpuid(0x80000001, &v[0], &v[1], &v[2], &v[3]);
Ingo Molnar8550eb92008-01-30 13:34:10 +0100549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if ((v[3] & (1 << 20)) && !disable_nx) {
551 rdmsr(MSR_EFER, l, h);
552 l |= EFER_NX;
553 wrmsr(MSR_EFER, l, h);
554 nx_enabled = 1;
555 __supported_pte_mask |= _PAGE_NX;
556 }
557 }
558}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559#endif
560
Yinghai Lu90d967e2008-06-23 21:00:45 +0200561/* user-defined highmem size */
562static unsigned int highmem_pages = -1;
563
564/*
565 * highmem=size forces highmem to be exactly 'size' bytes.
566 * This works even on boxes that have no highmem otherwise.
567 * This also works to reduce highmem size on bigger boxes.
568 */
569static int __init parse_highmem(char *arg)
570{
571 if (!arg)
572 return -EINVAL;
573
574 highmem_pages = memparse(arg, &arg) >> PAGE_SHIFT;
575 return 0;
576}
577early_param("highmem", parse_highmem);
578
579/*
580 * Determine low and high memory ranges:
581 */
Yinghai Lu2ec65f82008-06-23 03:05:30 -0700582void __init find_low_pfn_range(void)
Yinghai Lu90d967e2008-06-23 21:00:45 +0200583{
Yinghai Lu2ec65f82008-06-23 03:05:30 -0700584 /* it could update max_pfn */
585
Yinghai Lu346cafe2008-06-23 03:06:14 -0700586 /* max_low_pfn is 0, we already have early_res support */
Yinghai Lu90d967e2008-06-23 21:00:45 +0200587
588 max_low_pfn = max_pfn;
589 if (max_low_pfn > MAXMEM_PFN) {
590 if (highmem_pages == -1)
591 highmem_pages = max_pfn - MAXMEM_PFN;
592 if (highmem_pages + MAXMEM_PFN < max_pfn)
593 max_pfn = MAXMEM_PFN + highmem_pages;
594 if (highmem_pages + MAXMEM_PFN > max_pfn) {
595 printk(KERN_WARNING "only %luMB highmem pages "
596 "available, ignoring highmem size of %uMB.\n",
597 pages_to_mb(max_pfn - MAXMEM_PFN),
598 pages_to_mb(highmem_pages));
599 highmem_pages = 0;
600 }
601 max_low_pfn = MAXMEM_PFN;
602#ifndef CONFIG_HIGHMEM
603 /* Maximum memory usable is what is directly addressable */
604 printk(KERN_WARNING "Warning only %ldMB will be used.\n",
605 MAXMEM>>20);
606 if (max_pfn > MAX_NONPAE_PFN)
607 printk(KERN_WARNING
608 "Use a HIGHMEM64G enabled kernel.\n");
609 else
610 printk(KERN_WARNING "Use a HIGHMEM enabled kernel.\n");
611 max_pfn = MAXMEM_PFN;
612#else /* !CONFIG_HIGHMEM */
613#ifndef CONFIG_HIGHMEM64G
614 if (max_pfn > MAX_NONPAE_PFN) {
615 max_pfn = MAX_NONPAE_PFN;
616 printk(KERN_WARNING "Warning only 4GB will be used."
617 "Use a HIGHMEM64G enabled kernel.\n");
618 }
619#endif /* !CONFIG_HIGHMEM64G */
620#endif /* !CONFIG_HIGHMEM */
621 } else {
622 if (highmem_pages == -1)
623 highmem_pages = 0;
624#ifdef CONFIG_HIGHMEM
625 if (highmem_pages >= max_pfn) {
626 printk(KERN_ERR "highmem size specified (%uMB) is "
627 "bigger than pages available (%luMB)!.\n",
628 pages_to_mb(highmem_pages),
629 pages_to_mb(max_pfn));
630 highmem_pages = 0;
631 }
632 if (highmem_pages) {
633 if (max_low_pfn - highmem_pages <
634 64*1024*1024/PAGE_SIZE){
635 printk(KERN_ERR "highmem size %uMB results in "
636 "smaller than 64MB lowmem, ignoring it.\n"
637 , pages_to_mb(highmem_pages));
638 highmem_pages = 0;
639 }
640 max_low_pfn -= highmem_pages;
641 }
642#else
643 if (highmem_pages)
644 printk(KERN_ERR "ignoring highmem size on non-highmem"
645 " kernel!\n");
646#endif
647 }
Yinghai Lu90d967e2008-06-23 21:00:45 +0200648}
649
Yinghai Lub2ac82a2008-06-22 02:45:39 -0700650#ifndef CONFIG_NEED_MULTIPLE_NODES
Yinghai Lu2ec65f82008-06-23 03:05:30 -0700651void __init initmem_init(unsigned long start_pfn,
Yinghai Lub2ac82a2008-06-22 02:45:39 -0700652 unsigned long end_pfn)
653{
Yinghai Lub2ac82a2008-06-22 02:45:39 -0700654#ifdef CONFIG_HIGHMEM
655 highstart_pfn = highend_pfn = max_pfn;
656 if (max_pfn > max_low_pfn)
657 highstart_pfn = max_low_pfn;
658 memory_present(0, 0, highend_pfn);
Yinghai Lucb95a132008-07-02 00:31:02 -0700659 e820_register_active_regions(0, 0, highend_pfn);
Yinghai Lub2ac82a2008-06-22 02:45:39 -0700660 printk(KERN_NOTICE "%ldMB HIGHMEM available.\n",
661 pages_to_mb(highend_pfn - highstart_pfn));
662 num_physpages = highend_pfn;
663 high_memory = (void *) __va(highstart_pfn * PAGE_SIZE - 1) + 1;
664#else
665 memory_present(0, 0, max_low_pfn);
Yinghai Lucb95a132008-07-02 00:31:02 -0700666 e820_register_active_regions(0, 0, max_low_pfn);
Yinghai Lub2ac82a2008-06-22 02:45:39 -0700667 num_physpages = max_low_pfn;
668 high_memory = (void *) __va(max_low_pfn * PAGE_SIZE - 1) + 1;
669#endif
670#ifdef CONFIG_FLATMEM
671 max_mapnr = num_physpages;
672#endif
673 printk(KERN_NOTICE "%ldMB LOWMEM available.\n",
674 pages_to_mb(max_low_pfn));
675
676 setup_bootmem_allocator();
Yinghai Lub2ac82a2008-06-22 02:45:39 -0700677}
Yinghai Lucb95a132008-07-02 00:31:02 -0700678#endif /* !CONFIG_NEED_MULTIPLE_NODES */
Yinghai Lub2ac82a2008-06-22 02:45:39 -0700679
Yinghai Lucb95a132008-07-02 00:31:02 -0700680static void __init zone_sizes_init(void)
Yinghai Lub2ac82a2008-06-22 02:45:39 -0700681{
682 unsigned long max_zone_pfns[MAX_NR_ZONES];
683 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
684 max_zone_pfns[ZONE_DMA] =
685 virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
686 max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
Yinghai Lub2ac82a2008-06-22 02:45:39 -0700687#ifdef CONFIG_HIGHMEM
688 max_zone_pfns[ZONE_HIGHMEM] = highend_pfn;
Yinghai Lub2ac82a2008-06-22 02:45:39 -0700689#endif
690
691 free_area_init_nodes(max_zone_pfns);
692}
Yinghai Lub2ac82a2008-06-22 02:45:39 -0700693
Yinghai Lub2ac82a2008-06-22 02:45:39 -0700694void __init setup_bootmem_allocator(void)
695{
696 int i;
697 unsigned long bootmap_size, bootmap;
698 /*
699 * Initialize the boot-time allocator (with low memory only):
700 */
701 bootmap_size = bootmem_bootmap_pages(max_low_pfn)<<PAGE_SHIFT;
702 bootmap = find_e820_area(min_low_pfn<<PAGE_SHIFT,
703 max_pfn_mapped<<PAGE_SHIFT, bootmap_size,
704 PAGE_SIZE);
705 if (bootmap == -1L)
706 panic("Cannot find bootmem map of size %ld\n", bootmap_size);
707 reserve_early(bootmap, bootmap + bootmap_size, "BOOTMAP");
Yinghai Lu225c37d2008-06-22 02:46:58 -0700708
Yinghai Lu346cafe2008-06-23 03:06:14 -0700709 /* don't touch min_low_pfn */
710 bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap >> PAGE_SHIFT,
711 min_low_pfn, max_low_pfn);
Yinghai Lub2ac82a2008-06-22 02:45:39 -0700712 printk(KERN_INFO " mapped low ram: 0 - %08lx\n",
713 max_pfn_mapped<<PAGE_SHIFT);
714 printk(KERN_INFO " low ram: %08lx - %08lx\n",
715 min_low_pfn<<PAGE_SHIFT, max_low_pfn<<PAGE_SHIFT);
716 printk(KERN_INFO " bootmap %08lx - %08lx\n",
717 bootmap, bootmap + bootmap_size);
718 for_each_online_node(i)
719 free_bootmem_with_active_regions(i, max_low_pfn);
720 early_res_to_bootmem(0, max_low_pfn<<PAGE_SHIFT);
721
Yinghai Lu4e296842008-06-24 12:18:14 -0700722 after_init_bootmem = 1;
Yinghai Lub2ac82a2008-06-22 02:45:39 -0700723}
724
Yinghai Lu4e296842008-06-24 12:18:14 -0700725static void __init find_early_table_space(unsigned long end)
726{
Yinghai Lu7482b0e2008-06-28 03:30:39 -0700727 unsigned long puds, pmds, ptes, tables, start;
Yinghai Lu4e296842008-06-24 12:18:14 -0700728
729 puds = (end + PUD_SIZE - 1) >> PUD_SHIFT;
730 tables = PAGE_ALIGN(puds * sizeof(pud_t));
731
732 pmds = (end + PMD_SIZE - 1) >> PMD_SHIFT;
733 tables += PAGE_ALIGN(pmds * sizeof(pmd_t));
734
Yinghai Lu7482b0e2008-06-28 03:30:39 -0700735 if (cpu_has_pse) {
736 unsigned long extra;
Yinghai Lua04ad822008-06-29 00:39:06 -0700737
738 extra = end - ((end>>PMD_SHIFT) << PMD_SHIFT);
739 extra += PMD_SIZE;
Yinghai Lu7482b0e2008-06-28 03:30:39 -0700740 ptes = (extra + PAGE_SIZE - 1) >> PAGE_SHIFT;
741 } else
742 ptes = (end + PAGE_SIZE - 1) >> PAGE_SHIFT;
743
744 tables += PAGE_ALIGN(ptes * sizeof(pte_t));
Jeremy Fitzhardinge8207c252008-06-24 17:32:48 -0400745
Yinghai Lua04ad822008-06-29 00:39:06 -0700746 /* for fixmap */
747 tables += PAGE_SIZE * 2;
748
Yinghai Lu4e296842008-06-24 12:18:14 -0700749 /*
750 * RED-PEN putting page tables only on node 0 could
751 * cause a hotspot and fill up ZONE_DMA. The page tables
752 * need roughly 0.5KB per GB.
753 */
754 start = 0x7000;
755 table_start = find_e820_area(start, max_pfn_mapped<<PAGE_SHIFT,
756 tables, PAGE_SIZE);
757 if (table_start == -1UL)
758 panic("Cannot find space for the kernel page tables");
759
760 table_start >>= PAGE_SHIFT;
761 table_end = table_start;
762 table_top = table_start + (tables>>PAGE_SHIFT);
763
764 printk(KERN_DEBUG "kernel direct mapping tables up to %lx @ %lx-%lx\n",
765 end, table_start << PAGE_SHIFT,
766 (table_start << PAGE_SHIFT) + tables);
767}
768
769unsigned long __init_refok init_memory_mapping(unsigned long start,
770 unsigned long end)
771{
772 pgd_t *pgd_base = swapper_pg_dir;
Yinghai Lua04ad822008-06-29 00:39:06 -0700773 unsigned long start_pfn, end_pfn;
774 unsigned long big_page_start;
Yinghai Lu4e296842008-06-24 12:18:14 -0700775
776 /*
777 * Find space for the kernel direct mapping tables.
778 */
779 if (!after_init_bootmem)
780 find_early_table_space(end);
781
782#ifdef CONFIG_X86_PAE
783 set_nx();
784 if (nx_enabled)
785 printk(KERN_INFO "NX (Execute Disable) protection: active\n");
786#endif
787
788 /* Enable PSE if available */
789 if (cpu_has_pse)
790 set_in_cr4(X86_CR4_PSE);
791
792 /* Enable PGE if available */
793 if (cpu_has_pge) {
794 set_in_cr4(X86_CR4_PGE);
Jeremy Fitzhardingeef5e94a2008-07-01 16:46:36 -0700795 __supported_pte_mask |= _PAGE_GLOBAL;
Yinghai Lu4e296842008-06-24 12:18:14 -0700796 }
797
Yinghai Lua04ad822008-06-29 00:39:06 -0700798 /*
799 * Don't use a large page for the first 2/4MB of memory
800 * because there are often fixed size MTRRs in there
801 * and overlapping MTRRs into large pages can cause
802 * slowdowns.
803 */
804 big_page_start = PMD_SIZE;
805
806 if (start < big_page_start) {
807 start_pfn = start >> PAGE_SHIFT;
808 end_pfn = min(big_page_start>>PAGE_SHIFT, end>>PAGE_SHIFT);
809 } else {
810 /* head is not big page alignment ? */
811 start_pfn = start >> PAGE_SHIFT;
812 end_pfn = ((start + (PMD_SIZE - 1))>>PMD_SHIFT)
813 << (PMD_SHIFT - PAGE_SHIFT);
814 }
815 if (start_pfn < end_pfn)
816 kernel_physical_mapping_init(pgd_base, start_pfn, end_pfn, 0);
817
818 /* big page range */
819 start_pfn = ((start + (PMD_SIZE - 1))>>PMD_SHIFT)
820 << (PMD_SHIFT - PAGE_SHIFT);
821 if (start_pfn < (big_page_start >> PAGE_SHIFT))
822 start_pfn = big_page_start >> PAGE_SHIFT;
823 end_pfn = (end>>PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
824 if (start_pfn < end_pfn)
825 kernel_physical_mapping_init(pgd_base, start_pfn, end_pfn,
826 cpu_has_pse);
827
828 /* tail is not big page alignment ? */
829 start_pfn = end_pfn;
830 if (start_pfn > (big_page_start>>PAGE_SHIFT)) {
831 end_pfn = end >> PAGE_SHIFT;
832 if (start_pfn < end_pfn)
833 kernel_physical_mapping_init(pgd_base, start_pfn,
834 end_pfn, 0);
835 }
Yinghai Lu4e296842008-06-24 12:18:14 -0700836
Yinghai Lue7b37892008-06-25 21:51:28 -0700837 early_ioremap_page_table_range_init(pgd_base);
838
Yinghai Lu4e296842008-06-24 12:18:14 -0700839 load_cr3(swapper_pg_dir);
840
841 __flush_tlb_all();
842
843 if (!after_init_bootmem)
844 reserve_early(table_start << PAGE_SHIFT,
845 table_end << PAGE_SHIFT, "PGTABLE");
846
Yinghai Lucaadbdc2008-07-15 00:03:44 -0700847 if (!after_init_bootmem)
848 early_memtest(start, end);
849
Yinghai Lu4e296842008-06-24 12:18:14 -0700850 return end >> PAGE_SHIFT;
851}
852
Yinghai Lue7b37892008-06-25 21:51:28 -0700853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854/*
855 * paging_init() sets up the page tables - note that the first 8MB are
856 * already mapped by head.S.
857 *
858 * This routines also unmaps the page at virtual kernel address 0, so
859 * that we can trap those pesky NULL-reference errors in the kernel.
860 */
861void __init paging_init(void)
862{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 pagetable_init();
864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 __flush_tlb_all();
866
867 kmap_init();
Yinghai Lu11cd0bc2008-06-23 19:51:10 -0700868
869 /*
870 * NOTE: at this point the bootmem allocator is fully available.
871 */
Yinghai Lu11cd0bc2008-06-23 19:51:10 -0700872 sparse_init();
873 zone_sizes_init();
874
875 paravirt_post_allocator_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
878/*
879 * Test if the WP bit works in supervisor mode. It isn't supported on 386's
Dmitri Vorobievf7f17a62008-04-21 00:47:55 +0400880 * and also on some strange 486's. All 586+'s are OK. This used to involve
881 * black magic jumps to work around some nasty CPU bugs, but fortunately the
882 * switch to using exceptions got rid of all that.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884static void __init test_wp_bit(void)
885{
Ingo Molnard7d119d2008-01-30 13:34:10 +0100886 printk(KERN_INFO
887 "Checking if this processor honours the WP bit even in supervisor mode...");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 /* Any page-aligned address will do, the test is non-destructive */
890 __set_fixmap(FIX_WP_TEST, __pa(&swapper_pg_dir), PAGE_READONLY);
891 boot_cpu_data.wp_works_ok = do_test_wp_bit();
892 clear_fixmap(FIX_WP_TEST);
893
894 if (!boot_cpu_data.wp_works_ok) {
Ingo Molnard7d119d2008-01-30 13:34:10 +0100895 printk(KERN_CONT "No.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896#ifdef CONFIG_X86_WP_WORKS_OK
Ingo Molnard7d119d2008-01-30 13:34:10 +0100897 panic(
898 "This kernel doesn't support CPU's with broken WP. Recompile it for a 386!");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899#endif
900 } else {
Ingo Molnard7d119d2008-01-30 13:34:10 +0100901 printk(KERN_CONT "Ok.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
903}
904
Ingo Molnar8550eb92008-01-30 13:34:10 +0100905static struct kcore_list kcore_mem, kcore_vmalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907void __init mem_init(void)
908{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 int codesize, reservedpages, datasize, initsize;
Yinghai Lucc9f7a02008-06-16 16:11:08 -0700910 int tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Andy Whitcroft05b79bd2005-06-23 00:07:57 -0700912#ifdef CONFIG_FLATMEM
Eric Sesterhenn8d8f3cb2006-10-03 23:34:58 +0200913 BUG_ON(!mem_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 /* this will put all low memory onto the freelists */
916 totalram_pages += free_all_bootmem();
917
918 reservedpages = 0;
919 for (tmp = 0; tmp < max_low_pfn; tmp++)
920 /*
Ingo Molnar8550eb92008-01-30 13:34:10 +0100921 * Only count reserved RAM pages:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 */
923 if (page_is_ram(tmp) && PageReserved(pfn_to_page(tmp)))
924 reservedpages++;
925
Yinghai Lucc9f7a02008-06-16 16:11:08 -0700926 set_highmem_pages_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 codesize = (unsigned long) &_etext - (unsigned long) &_text;
929 datasize = (unsigned long) &_edata - (unsigned long) &_etext;
930 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
931
Ingo Molnar8550eb92008-01-30 13:34:10 +0100932 kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
933 kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 VMALLOC_END-VMALLOC_START);
935
Ingo Molnar8550eb92008-01-30 13:34:10 +0100936 printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, "
937 "%dk reserved, %dk data, %dk init, %ldk highmem)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
939 num_physpages << (PAGE_SHIFT-10),
940 codesize >> 10,
941 reservedpages << (PAGE_SHIFT-10),
942 datasize >> 10,
943 initsize >> 10,
944 (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10))
945 );
946
Ingo Molnard7d119d2008-01-30 13:34:10 +0100947 printk(KERN_INFO "virtual kernel memory layout:\n"
Ingo Molnar8550eb92008-01-30 13:34:10 +0100948 " fixmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
Jeremy Fitzhardinge052e7992006-09-25 23:32:25 -0700949#ifdef CONFIG_HIGHMEM
Ingo Molnar8550eb92008-01-30 13:34:10 +0100950 " pkmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
Jeremy Fitzhardinge052e7992006-09-25 23:32:25 -0700951#endif
Ingo Molnar8550eb92008-01-30 13:34:10 +0100952 " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n"
953 " lowmem : 0x%08lx - 0x%08lx (%4ld MB)\n"
954 " .init : 0x%08lx - 0x%08lx (%4ld kB)\n"
955 " .data : 0x%08lx - 0x%08lx (%4ld kB)\n"
956 " .text : 0x%08lx - 0x%08lx (%4ld kB)\n",
957 FIXADDR_START, FIXADDR_TOP,
958 (FIXADDR_TOP - FIXADDR_START) >> 10,
Jeremy Fitzhardinge052e7992006-09-25 23:32:25 -0700959
960#ifdef CONFIG_HIGHMEM
Ingo Molnar8550eb92008-01-30 13:34:10 +0100961 PKMAP_BASE, PKMAP_BASE+LAST_PKMAP*PAGE_SIZE,
962 (LAST_PKMAP*PAGE_SIZE) >> 10,
Jeremy Fitzhardinge052e7992006-09-25 23:32:25 -0700963#endif
964
Ingo Molnar8550eb92008-01-30 13:34:10 +0100965 VMALLOC_START, VMALLOC_END,
966 (VMALLOC_END - VMALLOC_START) >> 20,
Jeremy Fitzhardinge052e7992006-09-25 23:32:25 -0700967
Ingo Molnar8550eb92008-01-30 13:34:10 +0100968 (unsigned long)__va(0), (unsigned long)high_memory,
969 ((unsigned long)high_memory - (unsigned long)__va(0)) >> 20,
Jeremy Fitzhardinge052e7992006-09-25 23:32:25 -0700970
Ingo Molnar8550eb92008-01-30 13:34:10 +0100971 (unsigned long)&__init_begin, (unsigned long)&__init_end,
972 ((unsigned long)&__init_end -
973 (unsigned long)&__init_begin) >> 10,
Jeremy Fitzhardinge052e7992006-09-25 23:32:25 -0700974
Ingo Molnar8550eb92008-01-30 13:34:10 +0100975 (unsigned long)&_etext, (unsigned long)&_edata,
976 ((unsigned long)&_edata - (unsigned long)&_etext) >> 10,
Jeremy Fitzhardinge052e7992006-09-25 23:32:25 -0700977
Ingo Molnar8550eb92008-01-30 13:34:10 +0100978 (unsigned long)&_text, (unsigned long)&_etext,
979 ((unsigned long)&_etext - (unsigned long)&_text) >> 10);
Jeremy Fitzhardinge052e7992006-09-25 23:32:25 -0700980
981#ifdef CONFIG_HIGHMEM
Ingo Molnar8550eb92008-01-30 13:34:10 +0100982 BUG_ON(PKMAP_BASE + LAST_PKMAP*PAGE_SIZE > FIXADDR_START);
983 BUG_ON(VMALLOC_END > PKMAP_BASE);
Jeremy Fitzhardinge052e7992006-09-25 23:32:25 -0700984#endif
Ingo Molnar8550eb92008-01-30 13:34:10 +0100985 BUG_ON(VMALLOC_START > VMALLOC_END);
986 BUG_ON((unsigned long)high_memory > VMALLOC_START);
Jeremy Fitzhardinge052e7992006-09-25 23:32:25 -0700987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (boot_cpu_data.wp_works_ok < 0)
989 test_wp_bit();
990
Thomas Gleixner76ebd052008-02-09 23:24:09 +0100991 cpa_init();
Hugh Dickins61165d72008-05-13 14:26:57 +0100992 save_pg_dir();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 zap_low_mappings();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994}
995
KAMEZAWA Hiroyukiad8f5792006-05-20 15:00:03 -0700996#ifdef CONFIG_MEMORY_HOTPLUG
Yasunori Gotobc02af92006-06-27 02:53:30 -0700997int arch_add_memory(int nid, u64 start, u64 size)
Dave Hansen05039b92005-10-29 18:16:57 -0700998{
Yasunori Goto7c7e9422006-12-22 01:11:13 -0800999 struct pglist_data *pgdata = NODE_DATA(nid);
Christoph Lameter776ed982006-09-25 23:31:09 -07001000 struct zone *zone = pgdata->node_zones + ZONE_HIGHMEM;
Dave Hansen05039b92005-10-29 18:16:57 -07001001 unsigned long start_pfn = start >> PAGE_SHIFT;
1002 unsigned long nr_pages = size >> PAGE_SHIFT;
1003
1004 return __add_pages(zone, start_pfn, nr_pages);
1005}
Andi Kleen9d99aaa2006-04-07 19:49:15 +02001006#endif
Dave Hansen05039b92005-10-29 18:16:57 -07001007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008/*
1009 * This function cannot be __init, since exceptions don't work in that
1010 * section. Put this after the callers, so that it cannot be inlined.
1011 */
Ingo Molnar8550eb92008-01-30 13:34:10 +01001012static noinline int do_test_wp_bit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
1014 char tmp_reg;
1015 int flag;
1016
1017 __asm__ __volatile__(
Ingo Molnar8550eb92008-01-30 13:34:10 +01001018 " movb %0, %1 \n"
1019 "1: movb %1, %0 \n"
1020 " xorl %2, %2 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 "2: \n"
H. Peter Anvinf832ff12008-02-04 16:47:58 +01001022 _ASM_EXTABLE(1b,2b)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 :"=m" (*(char *)fix_to_virt(FIX_WP_TEST)),
1024 "=q" (tmp_reg),
1025 "=r" (flag)
1026 :"2" (1)
1027 :"memory");
Ingo Molnar8550eb92008-01-30 13:34:10 +01001028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 return flag;
1030}
1031
Arjan van de Ven63aaf302006-01-06 00:12:02 -08001032#ifdef CONFIG_DEBUG_RODATA
Arjan van de Venedeed302008-01-30 13:34:08 +01001033const int rodata_test_data = 0xC3;
1034EXPORT_SYMBOL_GPL(rodata_test_data);
Arjan van de Ven63aaf302006-01-06 00:12:02 -08001035
Arjan van de Ven63aaf302006-01-06 00:12:02 -08001036void mark_rodata_ro(void)
1037{
Jan Beulich6fb14752007-05-02 19:27:10 +02001038 unsigned long start = PFN_ALIGN(_text);
1039 unsigned long size = PFN_ALIGN(_etext) - start;
Arjan van de Ven63aaf302006-01-06 00:12:02 -08001040
Steven Rostedt8f0f9962008-05-12 21:20:56 +02001041#ifndef CONFIG_DYNAMIC_FTRACE
1042 /* Dynamic tracing modifies the kernel text section */
Mathieu Desnoyers4e4eee02008-02-02 15:42:20 -05001043 set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
1044 printk(KERN_INFO "Write protecting the kernel text: %luk\n",
1045 size >> 10);
Andi Kleen0c42f392008-01-30 13:33:42 +01001046
1047#ifdef CONFIG_CPA_DEBUG
Mathieu Desnoyers4e4eee02008-02-02 15:42:20 -05001048 printk(KERN_INFO "Testing CPA: Reverting %lx-%lx\n",
1049 start, start+size);
1050 set_pages_rw(virt_to_page(start), size>>PAGE_SHIFT);
Andi Kleen0c42f392008-01-30 13:33:42 +01001051
Mathieu Desnoyers4e4eee02008-02-02 15:42:20 -05001052 printk(KERN_INFO "Testing CPA: write protecting again\n");
1053 set_pages_ro(virt_to_page(start), size>>PAGE_SHIFT);
Linus Torvalds602033e2007-07-26 12:07:21 -07001054#endif
Steven Rostedt8f0f9962008-05-12 21:20:56 +02001055#endif /* CONFIG_DYNAMIC_FTRACE */
1056
Jan Beulich6fb14752007-05-02 19:27:10 +02001057 start += size;
1058 size = (unsigned long)__end_rodata - start;
Arjan van de Ven6d238cc2008-01-30 13:34:06 +01001059 set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
Ingo Molnard7d119d2008-01-30 13:34:10 +01001060 printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
1061 size >> 10);
Arjan van de Venedeed302008-01-30 13:34:08 +01001062 rodata_test();
Arjan van de Ven63aaf302006-01-06 00:12:02 -08001063
Andi Kleen0c42f392008-01-30 13:33:42 +01001064#ifdef CONFIG_CPA_DEBUG
Ingo Molnard7d119d2008-01-30 13:34:10 +01001065 printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, start + size);
Arjan van de Ven6d238cc2008-01-30 13:34:06 +01001066 set_pages_rw(virt_to_page(start), size >> PAGE_SHIFT);
Andi Kleen0c42f392008-01-30 13:33:42 +01001067
Ingo Molnard7d119d2008-01-30 13:34:10 +01001068 printk(KERN_INFO "Testing CPA: write protecting again\n");
Arjan van de Ven6d238cc2008-01-30 13:34:06 +01001069 set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
Andi Kleen0c42f392008-01-30 13:33:42 +01001070#endif
Arjan van de Ven63aaf302006-01-06 00:12:02 -08001071}
1072#endif
1073
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -08001074void free_init_pages(char *what, unsigned long begin, unsigned long end)
1075{
Ingo Molnaree01f112008-01-30 13:34:09 +01001076#ifdef CONFIG_DEBUG_PAGEALLOC
1077 /*
1078 * If debugging page accesses then do not free this memory but
1079 * mark them not present - any buggy init-section access will
1080 * create a kernel page fault:
1081 */
1082 printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n",
1083 begin, PAGE_ALIGN(end));
1084 set_memory_np(begin, (end - begin) >> PAGE_SHIFT);
1085#else
Ingo Molnar86f03982008-01-30 13:34:09 +01001086 unsigned long addr;
1087
Arjan van de Ven3c1df682008-01-30 13:34:07 +01001088 /*
1089 * We just marked the kernel text read only above, now that
1090 * we are going to free part of that, we need to make that
1091 * writeable first.
1092 */
1093 set_memory_rw(begin, (end - begin) >> PAGE_SHIFT);
1094
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -08001095 for (addr = begin; addr < end; addr += PAGE_SIZE) {
Linus Torvaldse3ebadd2007-05-07 08:44:24 -07001096 ClearPageReserved(virt_to_page(addr));
1097 init_page_count(virt_to_page(addr));
1098 memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
1099 free_page(addr);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -08001100 totalram_pages++;
1101 }
Jan Beulich6fb14752007-05-02 19:27:10 +02001102 printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
Ingo Molnaree01f112008-01-30 13:34:09 +01001103#endif
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -08001104}
1105
1106void free_initmem(void)
1107{
1108 free_init_pages("unused kernel memory",
Linus Torvaldse3ebadd2007-05-07 08:44:24 -07001109 (unsigned long)(&__init_begin),
1110 (unsigned long)(&__init_end));
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -08001111}
Arjan van de Ven63aaf302006-01-06 00:12:02 -08001112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113#ifdef CONFIG_BLK_DEV_INITRD
1114void free_initrd_mem(unsigned long start, unsigned long end)
1115{
Linus Torvaldse3ebadd2007-05-07 08:44:24 -07001116 free_init_pages("initrd memory", start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117}
1118#endif
Yinghai Lud2dbf342008-06-13 02:00:56 -07001119
1120int __init reserve_bootmem_generic(unsigned long phys, unsigned long len,
1121 int flags)
1122{
1123 return reserve_bootmem(phys, len, flags);
1124}