blob: 9bb35cf8bc4cb21f6e5207c1c80cf087fc5a635e [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
Thomas Gleixner67794292008-03-21 21:27:10 +010053unsigned long max_pfn_mapped;
Andi Kleen7d1116a2008-03-12 03:53:27 +010054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
56unsigned long highstart_pfn, highend_pfn;
57
Ingo Molnar8550eb92008-01-30 13:34:10 +010058static noinline int do_test_wp_bit(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60/*
61 * Creates a middle page table and puts a pointer to it in the
62 * given global directory entry. This only returns the gd entry
63 * in non-PAE compilation mode, since the middle layer is folded.
64 */
65static pmd_t * __init one_md_table_init(pgd_t *pgd)
66{
67 pud_t *pud;
68 pmd_t *pmd_table;
Ingo Molnar8550eb92008-01-30 13:34:10 +010069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#ifdef CONFIG_X86_PAE
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +020071 if (!(pgd_val(*pgd) & _PAGE_PRESENT)) {
72 pmd_table = (pmd_t *) alloc_bootmem_low_pages(PAGE_SIZE);
73
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -070074 paravirt_alloc_pmd(&init_mm, __pa(pmd_table) >> PAGE_SHIFT);
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +020075 set_pgd(pgd, __pgd(__pa(pmd_table) | _PAGE_PRESENT));
76 pud = pud_offset(pgd, 0);
Ingo Molnar8550eb92008-01-30 13:34:10 +010077 BUG_ON(pmd_table != pmd_offset(pud, 0));
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +020078 }
79#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 pud = pud_offset(pgd, 0);
81 pmd_table = pmd_offset(pud, 0);
Ingo Molnar8550eb92008-01-30 13:34:10 +010082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 return pmd_table;
84}
85
86/*
87 * Create a page table and place a pointer to it in a middle page
Ingo Molnar8550eb92008-01-30 13:34:10 +010088 * directory entry:
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 */
90static pte_t * __init one_page_table_init(pmd_t *pmd)
91{
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +020092 if (!(pmd_val(*pmd) & _PAGE_PRESENT)) {
Ingo Molnar509a80c2007-10-17 18:04:34 +020093 pte_t *page_table = NULL;
94
95#ifdef CONFIG_DEBUG_PAGEALLOC
96 page_table = (pte_t *) alloc_bootmem_pages(PAGE_SIZE);
97#endif
Ingo Molnar8550eb92008-01-30 13:34:10 +010098 if (!page_table) {
Ingo Molnar509a80c2007-10-17 18:04:34 +020099 page_table =
100 (pte_t *)alloc_bootmem_low_pages(PAGE_SIZE);
Ingo Molnar8550eb92008-01-30 13:34:10 +0100101 }
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200102
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -0700103 paravirt_alloc_pte(&init_mm, __pa(page_table) >> PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 set_pmd(pmd, __pmd(__pa(page_table) | _PAGE_TABLE));
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200105 BUG_ON(page_table != pte_offset_kernel(pmd, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
Ingo Molnar509a80c2007-10-17 18:04:34 +0200107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return pte_offset_kernel(pmd, 0);
109}
110
111/*
Ingo Molnar8550eb92008-01-30 13:34:10 +0100112 * This function initializes a certain range of kernel virtual memory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * with new bootmem page tables, everywhere page tables are missing in
114 * the given range.
Ingo Molnar8550eb92008-01-30 13:34:10 +0100115 *
116 * NOTE: The pagetables are allocated contiguous on the physical space
117 * so we can cache the place of the first one and move around without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 * checking the pgd every time.
119 */
Ingo Molnar8550eb92008-01-30 13:34:10 +0100120static void __init
121page_table_range_init(unsigned long start, unsigned long end, pgd_t *pgd_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 int pgd_idx, pmd_idx;
124 unsigned long vaddr;
Ingo Molnar8550eb92008-01-30 13:34:10 +0100125 pgd_t *pgd;
126 pmd_t *pmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 vaddr = start;
129 pgd_idx = pgd_index(vaddr);
130 pmd_idx = pmd_index(vaddr);
131 pgd = pgd_base + pgd_idx;
132
133 for ( ; (pgd_idx < PTRS_PER_PGD) && (vaddr != end); pgd++, pgd_idx++) {
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200134 pmd = one_md_table_init(pgd);
135 pmd = pmd + pmd_index(vaddr);
Ingo Molnar8550eb92008-01-30 13:34:10 +0100136 for (; (pmd_idx < PTRS_PER_PMD) && (vaddr != end);
137 pmd++, pmd_idx++) {
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200138 one_page_table_init(pmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 vaddr += PMD_SIZE;
141 }
142 pmd_idx = 0;
143 }
144}
145
146static inline int is_kernel_text(unsigned long addr)
147{
148 if (addr >= PAGE_OFFSET && addr <= (unsigned long)__init_end)
149 return 1;
150 return 0;
151}
152
153/*
Ingo Molnar8550eb92008-01-30 13:34:10 +0100154 * This maps the physical memory to kernel virtual address space, a total
155 * of max_low_pfn pages, by creating page tables starting from address
156 * PAGE_OFFSET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 */
158static void __init kernel_physical_mapping_init(pgd_t *pgd_base)
159{
Ingo Molnar8550eb92008-01-30 13:34:10 +0100160 int pgd_idx, pmd_idx, pte_ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 unsigned long pfn;
162 pgd_t *pgd;
163 pmd_t *pmd;
164 pte_t *pte;
Andi Kleence0c0e52008-05-02 11:46:49 +0200165 unsigned pages_2m = 0, pages_4k = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 pgd_idx = pgd_index(PAGE_OFFSET);
168 pgd = pgd_base + pgd_idx;
169 pfn = 0;
170
171 for (; pgd_idx < PTRS_PER_PGD; pgd++, pgd_idx++) {
172 pmd = one_md_table_init(pgd);
173 if (pfn >= max_low_pfn)
174 continue;
Ingo Molnar8550eb92008-01-30 13:34:10 +0100175
Jeremy Fitzhardingef3f20de2008-01-30 13:31:09 +0100176 for (pmd_idx = 0;
177 pmd_idx < PTRS_PER_PMD && pfn < max_low_pfn;
178 pmd++, pmd_idx++) {
Ingo Molnar8550eb92008-01-30 13:34:10 +0100179 unsigned int addr = pfn * PAGE_SIZE + PAGE_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Ingo Molnar8550eb92008-01-30 13:34:10 +0100181 /*
182 * Map with big pages if possible, otherwise
183 * create normal page tables:
Andi Kleenf5c24a72008-03-12 03:53:30 +0100184 *
185 * Don't use a large page for the first 2/4MB of memory
186 * because there are often fixed size MTRRs in there
187 * and overlapping MTRRs into large pages can cause
188 * slowdowns.
Ingo Molnar8550eb92008-01-30 13:34:10 +0100189 */
Andi Kleenf5c24a72008-03-12 03:53:30 +0100190 if (cpu_has_pse && !(pgd_idx == 0 && pmd_idx == 0)) {
Ingo Molnar8550eb92008-01-30 13:34:10 +0100191 unsigned int addr2;
Jeremy Fitzhardingef3f20de2008-01-30 13:31:09 +0100192 pgprot_t prot = PAGE_KERNEL_LARGE;
193
Ingo Molnar8550eb92008-01-30 13:34:10 +0100194 addr2 = (pfn + PTRS_PER_PTE-1) * PAGE_SIZE +
Jeremy Fitzhardingef3f20de2008-01-30 13:31:09 +0100195 PAGE_OFFSET + PAGE_SIZE-1;
196
Ingo Molnar8550eb92008-01-30 13:34:10 +0100197 if (is_kernel_text(addr) ||
198 is_kernel_text(addr2))
Jeremy Fitzhardingef3f20de2008-01-30 13:31:09 +0100199 prot = PAGE_KERNEL_LARGE_EXEC;
200
Andi Kleence0c0e52008-05-02 11:46:49 +0200201 pages_2m++;
Jeremy Fitzhardingef3f20de2008-01-30 13:31:09 +0100202 set_pmd(pmd, pfn_pmd(pfn, prot));
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 pfn += PTRS_PER_PTE;
Thomas Gleixner67794292008-03-21 21:27:10 +0100205 max_pfn_mapped = pfn;
Ingo Molnar8550eb92008-01-30 13:34:10 +0100206 continue;
207 }
208 pte = one_page_table_init(pmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Ingo Molnar8550eb92008-01-30 13:34:10 +0100210 for (pte_ofs = 0;
211 pte_ofs < PTRS_PER_PTE && pfn < max_low_pfn;
212 pte++, pfn++, pte_ofs++, addr += PAGE_SIZE) {
213 pgprot_t prot = PAGE_KERNEL;
Jeremy Fitzhardingef3f20de2008-01-30 13:31:09 +0100214
Ingo Molnar8550eb92008-01-30 13:34:10 +0100215 if (is_kernel_text(addr))
216 prot = PAGE_KERNEL_EXEC;
Jeremy Fitzhardingef3f20de2008-01-30 13:31:09 +0100217
Andi Kleence0c0e52008-05-02 11:46:49 +0200218 pages_4k++;
Ingo Molnar8550eb92008-01-30 13:34:10 +0100219 set_pte(pte, pfn_pte(pfn, prot));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
Thomas Gleixner67794292008-03-21 21:27:10 +0100221 max_pfn_mapped = pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
223 }
Andi Kleence0c0e52008-05-02 11:46:49 +0200224 update_page_count(PG_LEVEL_2M, pages_2m);
225 update_page_count(PG_LEVEL_4K, pages_4k);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
Arjan van de Venae531c22008-04-24 23:40:47 +0200228/*
229 * devmem_is_allowed() checks to see if /dev/mem access to a certain address
230 * is valid. The argument is a physical page number.
231 *
232 *
233 * On x86, access has to be given to the first megabyte of ram because that area
234 * contains bios code and data regions used by X and dosemu and similar apps.
235 * Access has to be given to non-kernel-ram areas as well, these contain the PCI
236 * mmio resources as well as potential bios/acpi data regions.
237 */
238int devmem_is_allowed(unsigned long pagenr)
239{
240 if (pagenr <= 256)
241 return 1;
242 if (!page_is_ram(pagenr))
243 return 1;
244 return 0;
245}
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247#ifdef CONFIG_HIGHMEM
248pte_t *kmap_pte;
249pgprot_t kmap_prot;
250
Ingo Molnar8550eb92008-01-30 13:34:10 +0100251static inline pte_t *kmap_get_fixmap_pte(unsigned long vaddr)
252{
253 return pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k(vaddr),
254 vaddr), vaddr), vaddr);
255}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257static void __init kmap_init(void)
258{
259 unsigned long kmap_vstart;
260
Ingo Molnar8550eb92008-01-30 13:34:10 +0100261 /*
262 * Cache the first kmap pte:
263 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN);
265 kmap_pte = kmap_get_fixmap_pte(kmap_vstart);
266
267 kmap_prot = PAGE_KERNEL;
268}
269
270static void __init permanent_kmaps_init(pgd_t *pgd_base)
271{
Ingo Molnar8550eb92008-01-30 13:34:10 +0100272 unsigned long vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 pgd_t *pgd;
274 pud_t *pud;
275 pmd_t *pmd;
276 pte_t *pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 vaddr = PKMAP_BASE;
279 page_table_range_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, pgd_base);
280
281 pgd = swapper_pg_dir + pgd_index(vaddr);
282 pud = pud_offset(pgd, vaddr);
283 pmd = pmd_offset(pud, vaddr);
284 pte = pte_offset_kernel(pmd, vaddr);
Ingo Molnar8550eb92008-01-30 13:34:10 +0100285 pkmap_page_table = pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
Yinghai Lucc9f7a02008-06-16 16:11:08 -0700288static void __init add_one_highpage_init(struct page *page, int pfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Yinghai Lucc9f7a02008-06-16 16:11:08 -0700290 ClearPageReserved(page);
291 init_page_count(page);
292 __free_page(page);
293 totalhigh_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
Yinghai Lub5bc6c02008-06-14 18:32:52 -0700296struct add_highpages_data {
297 unsigned long start_pfn;
298 unsigned long end_pfn;
Yinghai Lub5bc6c02008-06-14 18:32:52 -0700299};
300
Yinghai Lud52d53b2008-06-16 20:10:55 -0700301static int __init add_highpages_work_fn(unsigned long start_pfn,
Yinghai Lub5bc6c02008-06-14 18:32:52 -0700302 unsigned long end_pfn, void *datax)
303{
304 int node_pfn;
305 struct page *page;
306 unsigned long final_start_pfn, final_end_pfn;
307 struct add_highpages_data *data;
Yinghai Lub5bc6c02008-06-14 18:32:52 -0700308
309 data = (struct add_highpages_data *)datax;
Yinghai Lub5bc6c02008-06-14 18:32:52 -0700310
311 final_start_pfn = max(start_pfn, data->start_pfn);
312 final_end_pfn = min(end_pfn, data->end_pfn);
313 if (final_start_pfn >= final_end_pfn)
Yinghai Lud52d53b2008-06-16 20:10:55 -0700314 return 0;
Yinghai Lub5bc6c02008-06-14 18:32:52 -0700315
316 for (node_pfn = final_start_pfn; node_pfn < final_end_pfn;
317 node_pfn++) {
318 if (!pfn_valid(node_pfn))
319 continue;
320 page = pfn_to_page(node_pfn);
Yinghai Lucc9f7a02008-06-16 16:11:08 -0700321 add_one_highpage_init(page, node_pfn);
Yinghai Lub5bc6c02008-06-14 18:32:52 -0700322 }
323
Yinghai Lud52d53b2008-06-16 20:10:55 -0700324 return 0;
325
Yinghai Lub5bc6c02008-06-14 18:32:52 -0700326}
327
328void __init add_highpages_with_active_regions(int nid, unsigned long start_pfn,
Yinghai Lucc9f7a02008-06-16 16:11:08 -0700329 unsigned long end_pfn)
Yinghai Lub5bc6c02008-06-14 18:32:52 -0700330{
331 struct add_highpages_data data;
332
333 data.start_pfn = start_pfn;
334 data.end_pfn = end_pfn;
Yinghai Lub5bc6c02008-06-14 18:32:52 -0700335
336 work_with_active_regions(nid, add_highpages_work_fn, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
Ingo Molnar8550eb92008-01-30 13:34:10 +0100339#ifndef CONFIG_NUMA
Yinghai Lucc9f7a02008-06-16 16:11:08 -0700340static void __init set_highmem_pages_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Yinghai Lucc9f7a02008-06-16 16:11:08 -0700342 add_highpages_with_active_regions(0, highstart_pfn, highend_pfn);
Ingo Molnar8550eb92008-01-30 13:34:10 +0100343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 totalram_pages += totalhigh_pages;
345}
Ingo Molnar8550eb92008-01-30 13:34:10 +0100346#endif /* !CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348#else
Ingo Molnar8550eb92008-01-30 13:34:10 +0100349# define kmap_init() do { } while (0)
350# define permanent_kmaps_init(pgd_base) do { } while (0)
Yinghai Lucc9f7a02008-06-16 16:11:08 -0700351# define set_highmem_pages_init() do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352#endif /* CONFIG_HIGHMEM */
353
Andi Kleenc93c82b2008-01-30 13:33:50 +0100354pteval_t __PAGE_KERNEL = _PAGE_KERNEL;
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700355EXPORT_SYMBOL(__PAGE_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Ingo Molnar8550eb92008-01-30 13:34:10 +0100357pteval_t __PAGE_KERNEL_EXEC = _PAGE_KERNEL_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200359void __init native_pagetable_setup_start(pgd_t *base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Ian Campbell551889a2008-02-09 23:24:09 +0100361 unsigned long pfn, va;
362 pgd_t *pgd;
363 pud_t *pud;
364 pmd_t *pmd;
365 pte_t *pte;
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200366
367 /*
Ian Campbell551889a2008-02-09 23:24:09 +0100368 * Remove any mappings which extend past the end of physical
369 * memory from the boot time page table:
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200370 */
Ian Campbell551889a2008-02-09 23:24:09 +0100371 for (pfn = max_low_pfn + 1; pfn < 1<<(32-PAGE_SHIFT); pfn++) {
372 va = PAGE_OFFSET + (pfn<<PAGE_SHIFT);
373 pgd = base + pgd_index(va);
374 if (!pgd_present(*pgd))
375 break;
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200376
Ian Campbell551889a2008-02-09 23:24:09 +0100377 pud = pud_offset(pgd, va);
378 pmd = pmd_offset(pud, va);
379 if (!pmd_present(*pmd))
380 break;
381
382 pte = pte_offset_kernel(pmd, va);
383 if (!pte_present(*pte))
384 break;
385
386 pte_clear(NULL, va, pte);
387 }
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -0700388 paravirt_alloc_pmd(&init_mm, __pa(base) >> PAGE_SHIFT);
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200389}
390
391void __init native_pagetable_setup_done(pgd_t *base)
392{
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200393}
394
395/*
396 * Build a proper pagetable for the kernel mappings. Up until this
397 * point, we've been running on some set of pagetables constructed by
398 * the boot process.
399 *
400 * If we're booting on native hardware, this will be a pagetable
Ian Campbell551889a2008-02-09 23:24:09 +0100401 * constructed in arch/x86/kernel/head_32.S. The root of the
402 * pagetable will be swapper_pg_dir.
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200403 *
404 * If we're booting paravirtualized under a hypervisor, then there are
405 * more options: we may already be running PAE, and the pagetable may
406 * or may not be based in swapper_pg_dir. In any case,
407 * paravirt_pagetable_setup_start() will set up swapper_pg_dir
408 * appropriately for the rest of the initialization to work.
409 *
410 * In general, pagetable_init() assumes that the pagetable may already
411 * be partially populated, and so it avoids stomping on any existing
412 * mappings.
413 */
Ingo Molnar8550eb92008-01-30 13:34:10 +0100414static void __init pagetable_init(void)
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200415{
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200416 pgd_t *pgd_base = swapper_pg_dir;
Ingo Molnar8550eb92008-01-30 13:34:10 +0100417 unsigned long vaddr, end;
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200418
419 paravirt_pagetable_setup_start(pgd_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 /* Enable PSE if available */
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200422 if (cpu_has_pse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 set_in_cr4(X86_CR4_PSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 /* Enable PGE if available */
426 if (cpu_has_pge) {
427 set_in_cr4(X86_CR4_PGE);
428 __PAGE_KERNEL |= _PAGE_GLOBAL;
429 __PAGE_KERNEL_EXEC |= _PAGE_GLOBAL;
430 }
431
432 kernel_physical_mapping_init(pgd_base);
433 remap_numa_kva();
434
435 /*
436 * Fixed mappings, only the page table structure has to be
437 * created - mappings will be set by set_fixmap():
438 */
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100439 early_ioremap_clear();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200441 end = (FIXADDR_TOP + PMD_SIZE - 1) & PMD_MASK;
442 page_table_range_init(vaddr, end, pgd_base);
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100443 early_ioremap_reset();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 permanent_kmaps_init(pgd_base);
446
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200447 paravirt_pagetable_setup_done(pgd_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
Rafael J. Wysockia6eb84b2008-02-01 15:28:16 +0100450#ifdef CONFIG_ACPI_SLEEP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451/*
Rafael J. Wysockia6eb84b2008-02-01 15:28:16 +0100452 * ACPI suspend needs this for resume, because things like the intel-agp
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 * driver might have split up a kernel 4MB mapping.
454 */
Rafael J. Wysockia6eb84b2008-02-01 15:28:16 +0100455char swsusp_pg_dir[PAGE_SIZE]
Ingo Molnar8550eb92008-01-30 13:34:10 +0100456 __attribute__ ((aligned(PAGE_SIZE)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458static inline void save_pg_dir(void)
459{
460 memcpy(swsusp_pg_dir, swapper_pg_dir, PAGE_SIZE);
461}
Rafael J. Wysockia6eb84b2008-02-01 15:28:16 +0100462#else /* !CONFIG_ACPI_SLEEP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463static inline void save_pg_dir(void)
464{
465}
Rafael J. Wysockia6eb84b2008-02-01 15:28:16 +0100466#endif /* !CONFIG_ACPI_SLEEP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Ingo Molnar8550eb92008-01-30 13:34:10 +0100468void zap_low_mappings(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 int i;
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 /*
473 * Zap initial low-memory mappings.
474 *
475 * Note that "pgd_clear()" doesn't do it for
476 * us, because pgd_clear() is a no-op on i386.
477 */
Jeremy Fitzhardinge68db0652008-03-17 16:37:13 -0700478 for (i = 0; i < KERNEL_PGD_BOUNDARY; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479#ifdef CONFIG_X86_PAE
480 set_pgd(swapper_pg_dir+i, __pgd(1 + __pa(empty_zero_page)));
481#else
482 set_pgd(swapper_pg_dir+i, __pgd(0));
483#endif
Ingo Molnar8550eb92008-01-30 13:34:10 +0100484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 flush_tlb_all();
486}
487
Ingo Molnar8550eb92008-01-30 13:34:10 +0100488int nx_enabled;
Jan Beulichd5321ab2007-07-21 17:10:26 +0200489
Jeremy Fitzhardinge6fdc05d2008-01-30 13:32:57 +0100490pteval_t __supported_pte_mask __read_mostly = ~_PAGE_NX;
491EXPORT_SYMBOL_GPL(__supported_pte_mask);
492
Jan Beulichd5321ab2007-07-21 17:10:26 +0200493#ifdef CONFIG_X86_PAE
494
Ingo Molnar8550eb92008-01-30 13:34:10 +0100495static int disable_nx __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497/*
498 * noexec = on|off
499 *
500 * Control non executable mappings.
501 *
502 * on Enable
503 * off Disable
504 */
Rusty Russell1a3f2392006-09-26 10:52:32 +0200505static int __init noexec_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Rusty Russell1a3f2392006-09-26 10:52:32 +0200507 if (!str || !strcmp(str, "on")) {
508 if (cpu_has_nx) {
509 __supported_pte_mask |= _PAGE_NX;
510 disable_nx = 0;
511 }
Ingo Molnar8550eb92008-01-30 13:34:10 +0100512 } else {
513 if (!strcmp(str, "off")) {
514 disable_nx = 1;
515 __supported_pte_mask &= ~_PAGE_NX;
516 } else {
517 return -EINVAL;
518 }
519 }
Rusty Russell1a3f2392006-09-26 10:52:32 +0200520
521 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522}
Rusty Russell1a3f2392006-09-26 10:52:32 +0200523early_param("noexec", noexec_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525static void __init set_nx(void)
526{
527 unsigned int v[4], l, h;
528
529 if (cpu_has_pae && (cpuid_eax(0x80000000) > 0x80000001)) {
530 cpuid(0x80000001, &v[0], &v[1], &v[2], &v[3]);
Ingo Molnar8550eb92008-01-30 13:34:10 +0100531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if ((v[3] & (1 << 20)) && !disable_nx) {
533 rdmsr(MSR_EFER, l, h);
534 l |= EFER_NX;
535 wrmsr(MSR_EFER, l, h);
536 nx_enabled = 1;
537 __supported_pte_mask |= _PAGE_NX;
538 }
539 }
540}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541#endif
542
Yinghai Lu90d967e2008-06-23 21:00:45 +0200543/* user-defined highmem size */
544static unsigned int highmem_pages = -1;
545
546/*
547 * highmem=size forces highmem to be exactly 'size' bytes.
548 * This works even on boxes that have no highmem otherwise.
549 * This also works to reduce highmem size on bigger boxes.
550 */
551static int __init parse_highmem(char *arg)
552{
553 if (!arg)
554 return -EINVAL;
555
556 highmem_pages = memparse(arg, &arg) >> PAGE_SHIFT;
557 return 0;
558}
559early_param("highmem", parse_highmem);
560
561/*
562 * Determine low and high memory ranges:
563 */
Yinghai Lu2ec65f82008-06-23 03:05:30 -0700564void __init find_low_pfn_range(void)
Yinghai Lu90d967e2008-06-23 21:00:45 +0200565{
Yinghai Lu2ec65f82008-06-23 03:05:30 -0700566 /* it could update max_pfn */
567
Yinghai Lu346cafe2008-06-23 03:06:14 -0700568 /* max_low_pfn is 0, we already have early_res support */
Yinghai Lu90d967e2008-06-23 21:00:45 +0200569
570 max_low_pfn = max_pfn;
571 if (max_low_pfn > MAXMEM_PFN) {
572 if (highmem_pages == -1)
573 highmem_pages = max_pfn - MAXMEM_PFN;
574 if (highmem_pages + MAXMEM_PFN < max_pfn)
575 max_pfn = MAXMEM_PFN + highmem_pages;
576 if (highmem_pages + MAXMEM_PFN > max_pfn) {
577 printk(KERN_WARNING "only %luMB highmem pages "
578 "available, ignoring highmem size of %uMB.\n",
579 pages_to_mb(max_pfn - MAXMEM_PFN),
580 pages_to_mb(highmem_pages));
581 highmem_pages = 0;
582 }
583 max_low_pfn = MAXMEM_PFN;
584#ifndef CONFIG_HIGHMEM
585 /* Maximum memory usable is what is directly addressable */
586 printk(KERN_WARNING "Warning only %ldMB will be used.\n",
587 MAXMEM>>20);
588 if (max_pfn > MAX_NONPAE_PFN)
589 printk(KERN_WARNING
590 "Use a HIGHMEM64G enabled kernel.\n");
591 else
592 printk(KERN_WARNING "Use a HIGHMEM enabled kernel.\n");
593 max_pfn = MAXMEM_PFN;
594#else /* !CONFIG_HIGHMEM */
595#ifndef CONFIG_HIGHMEM64G
596 if (max_pfn > MAX_NONPAE_PFN) {
597 max_pfn = MAX_NONPAE_PFN;
598 printk(KERN_WARNING "Warning only 4GB will be used."
599 "Use a HIGHMEM64G enabled kernel.\n");
600 }
601#endif /* !CONFIG_HIGHMEM64G */
602#endif /* !CONFIG_HIGHMEM */
603 } else {
604 if (highmem_pages == -1)
605 highmem_pages = 0;
606#ifdef CONFIG_HIGHMEM
607 if (highmem_pages >= max_pfn) {
608 printk(KERN_ERR "highmem size specified (%uMB) is "
609 "bigger than pages available (%luMB)!.\n",
610 pages_to_mb(highmem_pages),
611 pages_to_mb(max_pfn));
612 highmem_pages = 0;
613 }
614 if (highmem_pages) {
615 if (max_low_pfn - highmem_pages <
616 64*1024*1024/PAGE_SIZE){
617 printk(KERN_ERR "highmem size %uMB results in "
618 "smaller than 64MB lowmem, ignoring it.\n"
619 , pages_to_mb(highmem_pages));
620 highmem_pages = 0;
621 }
622 max_low_pfn -= highmem_pages;
623 }
624#else
625 if (highmem_pages)
626 printk(KERN_ERR "ignoring highmem size on non-highmem"
627 " kernel!\n");
628#endif
629 }
Yinghai Lu90d967e2008-06-23 21:00:45 +0200630}
631
Yinghai Lub2ac82a2008-06-22 02:45:39 -0700632#ifndef CONFIG_NEED_MULTIPLE_NODES
Yinghai Lu2ec65f82008-06-23 03:05:30 -0700633void __init initmem_init(unsigned long start_pfn,
Yinghai Lub2ac82a2008-06-22 02:45:39 -0700634 unsigned long end_pfn)
635{
Yinghai Lub2ac82a2008-06-22 02:45:39 -0700636#ifdef CONFIG_HIGHMEM
637 highstart_pfn = highend_pfn = max_pfn;
638 if (max_pfn > max_low_pfn)
639 highstart_pfn = max_low_pfn;
640 memory_present(0, 0, highend_pfn);
641 printk(KERN_NOTICE "%ldMB HIGHMEM available.\n",
642 pages_to_mb(highend_pfn - highstart_pfn));
643 num_physpages = highend_pfn;
644 high_memory = (void *) __va(highstart_pfn * PAGE_SIZE - 1) + 1;
645#else
646 memory_present(0, 0, max_low_pfn);
647 num_physpages = max_low_pfn;
648 high_memory = (void *) __va(max_low_pfn * PAGE_SIZE - 1) + 1;
649#endif
650#ifdef CONFIG_FLATMEM
651 max_mapnr = num_physpages;
652#endif
653 printk(KERN_NOTICE "%ldMB LOWMEM available.\n",
654 pages_to_mb(max_low_pfn));
655
656 setup_bootmem_allocator();
Yinghai Lub2ac82a2008-06-22 02:45:39 -0700657}
658
659void __init zone_sizes_init(void)
660{
661 unsigned long max_zone_pfns[MAX_NR_ZONES];
662 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
663 max_zone_pfns[ZONE_DMA] =
664 virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
665 max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
666 remove_all_active_ranges();
667#ifdef CONFIG_HIGHMEM
668 max_zone_pfns[ZONE_HIGHMEM] = highend_pfn;
669 e820_register_active_regions(0, 0, highend_pfn);
670#else
671 e820_register_active_regions(0, 0, max_low_pfn);
672#endif
673
674 free_area_init_nodes(max_zone_pfns);
675}
676#endif /* !CONFIG_NEED_MULTIPLE_NODES */
677
Yinghai Lub2ac82a2008-06-22 02:45:39 -0700678void __init setup_bootmem_allocator(void)
679{
680 int i;
681 unsigned long bootmap_size, bootmap;
682 /*
683 * Initialize the boot-time allocator (with low memory only):
684 */
685 bootmap_size = bootmem_bootmap_pages(max_low_pfn)<<PAGE_SHIFT;
686 bootmap = find_e820_area(min_low_pfn<<PAGE_SHIFT,
687 max_pfn_mapped<<PAGE_SHIFT, bootmap_size,
688 PAGE_SIZE);
689 if (bootmap == -1L)
690 panic("Cannot find bootmem map of size %ld\n", bootmap_size);
691 reserve_early(bootmap, bootmap + bootmap_size, "BOOTMAP");
Yinghai Lu225c37d2008-06-22 02:46:58 -0700692
Yinghai Lu346cafe2008-06-23 03:06:14 -0700693 /* don't touch min_low_pfn */
694 bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap >> PAGE_SHIFT,
695 min_low_pfn, max_low_pfn);
Yinghai Lub2ac82a2008-06-22 02:45:39 -0700696 printk(KERN_INFO " mapped low ram: 0 - %08lx\n",
697 max_pfn_mapped<<PAGE_SHIFT);
698 printk(KERN_INFO " low ram: %08lx - %08lx\n",
699 min_low_pfn<<PAGE_SHIFT, max_low_pfn<<PAGE_SHIFT);
700 printk(KERN_INFO " bootmap %08lx - %08lx\n",
701 bootmap, bootmap + bootmap_size);
702 for_each_online_node(i)
703 free_bootmem_with_active_regions(i, max_low_pfn);
704 early_res_to_bootmem(0, max_low_pfn<<PAGE_SHIFT);
705
706}
707
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709/*
710 * paging_init() sets up the page tables - note that the first 8MB are
711 * already mapped by head.S.
712 *
713 * This routines also unmaps the page at virtual kernel address 0, so
714 * that we can trap those pesky NULL-reference errors in the kernel.
715 */
716void __init paging_init(void)
717{
718#ifdef CONFIG_X86_PAE
719 set_nx();
720 if (nx_enabled)
Ingo Molnard7d119d2008-01-30 13:34:10 +0100721 printk(KERN_INFO "NX (Execute Disable) protection: active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 pagetable_init();
724
725 load_cr3(swapper_pg_dir);
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 __flush_tlb_all();
728
729 kmap_init();
730}
731
732/*
733 * Test if the WP bit works in supervisor mode. It isn't supported on 386's
Dmitri Vorobievf7f17a62008-04-21 00:47:55 +0400734 * and also on some strange 486's. All 586+'s are OK. This used to involve
735 * black magic jumps to work around some nasty CPU bugs, but fortunately the
736 * switch to using exceptions got rid of all that.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738static void __init test_wp_bit(void)
739{
Ingo Molnard7d119d2008-01-30 13:34:10 +0100740 printk(KERN_INFO
741 "Checking if this processor honours the WP bit even in supervisor mode...");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 /* Any page-aligned address will do, the test is non-destructive */
744 __set_fixmap(FIX_WP_TEST, __pa(&swapper_pg_dir), PAGE_READONLY);
745 boot_cpu_data.wp_works_ok = do_test_wp_bit();
746 clear_fixmap(FIX_WP_TEST);
747
748 if (!boot_cpu_data.wp_works_ok) {
Ingo Molnard7d119d2008-01-30 13:34:10 +0100749 printk(KERN_CONT "No.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750#ifdef CONFIG_X86_WP_WORKS_OK
Ingo Molnard7d119d2008-01-30 13:34:10 +0100751 panic(
752 "This kernel doesn't support CPU's with broken WP. Recompile it for a 386!");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753#endif
754 } else {
Ingo Molnard7d119d2008-01-30 13:34:10 +0100755 printk(KERN_CONT "Ok.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
757}
758
Ingo Molnar8550eb92008-01-30 13:34:10 +0100759static struct kcore_list kcore_mem, kcore_vmalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761void __init mem_init(void)
762{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 int codesize, reservedpages, datasize, initsize;
Yinghai Lucc9f7a02008-06-16 16:11:08 -0700764 int tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Andy Whitcroft05b79bd2005-06-23 00:07:57 -0700766#ifdef CONFIG_FLATMEM
Eric Sesterhenn8d8f3cb2006-10-03 23:34:58 +0200767 BUG_ON(!mem_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 /* this will put all low memory onto the freelists */
770 totalram_pages += free_all_bootmem();
771
772 reservedpages = 0;
773 for (tmp = 0; tmp < max_low_pfn; tmp++)
774 /*
Ingo Molnar8550eb92008-01-30 13:34:10 +0100775 * Only count reserved RAM pages:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 */
777 if (page_is_ram(tmp) && PageReserved(pfn_to_page(tmp)))
778 reservedpages++;
779
Yinghai Lucc9f7a02008-06-16 16:11:08 -0700780 set_highmem_pages_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 codesize = (unsigned long) &_etext - (unsigned long) &_text;
783 datasize = (unsigned long) &_edata - (unsigned long) &_etext;
784 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
785
Ingo Molnar8550eb92008-01-30 13:34:10 +0100786 kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
787 kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 VMALLOC_END-VMALLOC_START);
789
Ingo Molnar8550eb92008-01-30 13:34:10 +0100790 printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, "
791 "%dk reserved, %dk data, %dk init, %ldk highmem)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
793 num_physpages << (PAGE_SHIFT-10),
794 codesize >> 10,
795 reservedpages << (PAGE_SHIFT-10),
796 datasize >> 10,
797 initsize >> 10,
798 (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10))
799 );
800
Ingo Molnard7d119d2008-01-30 13:34:10 +0100801 printk(KERN_INFO "virtual kernel memory layout:\n"
Ingo Molnar8550eb92008-01-30 13:34:10 +0100802 " fixmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
Jeremy Fitzhardinge052e7992006-09-25 23:32:25 -0700803#ifdef CONFIG_HIGHMEM
Ingo Molnar8550eb92008-01-30 13:34:10 +0100804 " pkmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
Jeremy Fitzhardinge052e7992006-09-25 23:32:25 -0700805#endif
Ingo Molnar8550eb92008-01-30 13:34:10 +0100806 " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n"
807 " lowmem : 0x%08lx - 0x%08lx (%4ld MB)\n"
808 " .init : 0x%08lx - 0x%08lx (%4ld kB)\n"
809 " .data : 0x%08lx - 0x%08lx (%4ld kB)\n"
810 " .text : 0x%08lx - 0x%08lx (%4ld kB)\n",
811 FIXADDR_START, FIXADDR_TOP,
812 (FIXADDR_TOP - FIXADDR_START) >> 10,
Jeremy Fitzhardinge052e7992006-09-25 23:32:25 -0700813
814#ifdef CONFIG_HIGHMEM
Ingo Molnar8550eb92008-01-30 13:34:10 +0100815 PKMAP_BASE, PKMAP_BASE+LAST_PKMAP*PAGE_SIZE,
816 (LAST_PKMAP*PAGE_SIZE) >> 10,
Jeremy Fitzhardinge052e7992006-09-25 23:32:25 -0700817#endif
818
Ingo Molnar8550eb92008-01-30 13:34:10 +0100819 VMALLOC_START, VMALLOC_END,
820 (VMALLOC_END - VMALLOC_START) >> 20,
Jeremy Fitzhardinge052e7992006-09-25 23:32:25 -0700821
Ingo Molnar8550eb92008-01-30 13:34:10 +0100822 (unsigned long)__va(0), (unsigned long)high_memory,
823 ((unsigned long)high_memory - (unsigned long)__va(0)) >> 20,
Jeremy Fitzhardinge052e7992006-09-25 23:32:25 -0700824
Ingo Molnar8550eb92008-01-30 13:34:10 +0100825 (unsigned long)&__init_begin, (unsigned long)&__init_end,
826 ((unsigned long)&__init_end -
827 (unsigned long)&__init_begin) >> 10,
Jeremy Fitzhardinge052e7992006-09-25 23:32:25 -0700828
Ingo Molnar8550eb92008-01-30 13:34:10 +0100829 (unsigned long)&_etext, (unsigned long)&_edata,
830 ((unsigned long)&_edata - (unsigned long)&_etext) >> 10,
Jeremy Fitzhardinge052e7992006-09-25 23:32:25 -0700831
Ingo Molnar8550eb92008-01-30 13:34:10 +0100832 (unsigned long)&_text, (unsigned long)&_etext,
833 ((unsigned long)&_etext - (unsigned long)&_text) >> 10);
Jeremy Fitzhardinge052e7992006-09-25 23:32:25 -0700834
835#ifdef CONFIG_HIGHMEM
Ingo Molnar8550eb92008-01-30 13:34:10 +0100836 BUG_ON(PKMAP_BASE + LAST_PKMAP*PAGE_SIZE > FIXADDR_START);
837 BUG_ON(VMALLOC_END > PKMAP_BASE);
Jeremy Fitzhardinge052e7992006-09-25 23:32:25 -0700838#endif
Ingo Molnar8550eb92008-01-30 13:34:10 +0100839 BUG_ON(VMALLOC_START > VMALLOC_END);
840 BUG_ON((unsigned long)high_memory > VMALLOC_START);
Jeremy Fitzhardinge052e7992006-09-25 23:32:25 -0700841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (boot_cpu_data.wp_works_ok < 0)
843 test_wp_bit();
844
Thomas Gleixner76ebd052008-02-09 23:24:09 +0100845 cpa_init();
Hugh Dickins61165d72008-05-13 14:26:57 +0100846 save_pg_dir();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 zap_low_mappings();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
KAMEZAWA Hiroyukiad8f5792006-05-20 15:00:03 -0700850#ifdef CONFIG_MEMORY_HOTPLUG
Yasunori Gotobc02af92006-06-27 02:53:30 -0700851int arch_add_memory(int nid, u64 start, u64 size)
Dave Hansen05039b92005-10-29 18:16:57 -0700852{
Yasunori Goto7c7e9422006-12-22 01:11:13 -0800853 struct pglist_data *pgdata = NODE_DATA(nid);
Christoph Lameter776ed982006-09-25 23:31:09 -0700854 struct zone *zone = pgdata->node_zones + ZONE_HIGHMEM;
Dave Hansen05039b92005-10-29 18:16:57 -0700855 unsigned long start_pfn = start >> PAGE_SHIFT;
856 unsigned long nr_pages = size >> PAGE_SHIFT;
857
858 return __add_pages(zone, start_pfn, nr_pages);
859}
Andi Kleen9d99aaa2006-04-07 19:49:15 +0200860#endif
Dave Hansen05039b92005-10-29 18:16:57 -0700861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862/*
863 * This function cannot be __init, since exceptions don't work in that
864 * section. Put this after the callers, so that it cannot be inlined.
865 */
Ingo Molnar8550eb92008-01-30 13:34:10 +0100866static noinline int do_test_wp_bit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
868 char tmp_reg;
869 int flag;
870
871 __asm__ __volatile__(
Ingo Molnar8550eb92008-01-30 13:34:10 +0100872 " movb %0, %1 \n"
873 "1: movb %1, %0 \n"
874 " xorl %2, %2 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 "2: \n"
H. Peter Anvinf832ff12008-02-04 16:47:58 +0100876 _ASM_EXTABLE(1b,2b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 :"=m" (*(char *)fix_to_virt(FIX_WP_TEST)),
878 "=q" (tmp_reg),
879 "=r" (flag)
880 :"2" (1)
881 :"memory");
Ingo Molnar8550eb92008-01-30 13:34:10 +0100882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return flag;
884}
885
Arjan van de Ven63aaf302006-01-06 00:12:02 -0800886#ifdef CONFIG_DEBUG_RODATA
Arjan van de Venedeed302008-01-30 13:34:08 +0100887const int rodata_test_data = 0xC3;
888EXPORT_SYMBOL_GPL(rodata_test_data);
Arjan van de Ven63aaf302006-01-06 00:12:02 -0800889
Arjan van de Ven63aaf302006-01-06 00:12:02 -0800890void mark_rodata_ro(void)
891{
Jan Beulich6fb14752007-05-02 19:27:10 +0200892 unsigned long start = PFN_ALIGN(_text);
893 unsigned long size = PFN_ALIGN(_etext) - start;
Arjan van de Ven63aaf302006-01-06 00:12:02 -0800894
Mathieu Desnoyers4e4eee02008-02-02 15:42:20 -0500895 set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
896 printk(KERN_INFO "Write protecting the kernel text: %luk\n",
897 size >> 10);
Andi Kleen0c42f392008-01-30 13:33:42 +0100898
899#ifdef CONFIG_CPA_DEBUG
Mathieu Desnoyers4e4eee02008-02-02 15:42:20 -0500900 printk(KERN_INFO "Testing CPA: Reverting %lx-%lx\n",
901 start, start+size);
902 set_pages_rw(virt_to_page(start), size>>PAGE_SHIFT);
Andi Kleen0c42f392008-01-30 13:33:42 +0100903
Mathieu Desnoyers4e4eee02008-02-02 15:42:20 -0500904 printk(KERN_INFO "Testing CPA: write protecting again\n");
905 set_pages_ro(virt_to_page(start), size>>PAGE_SHIFT);
Linus Torvalds602033e2007-07-26 12:07:21 -0700906#endif
Jan Beulich6fb14752007-05-02 19:27:10 +0200907 start += size;
908 size = (unsigned long)__end_rodata - start;
Arjan van de Ven6d238cc2008-01-30 13:34:06 +0100909 set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
Ingo Molnard7d119d2008-01-30 13:34:10 +0100910 printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
911 size >> 10);
Arjan van de Venedeed302008-01-30 13:34:08 +0100912 rodata_test();
Arjan van de Ven63aaf302006-01-06 00:12:02 -0800913
Andi Kleen0c42f392008-01-30 13:33:42 +0100914#ifdef CONFIG_CPA_DEBUG
Ingo Molnard7d119d2008-01-30 13:34:10 +0100915 printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, start + size);
Arjan van de Ven6d238cc2008-01-30 13:34:06 +0100916 set_pages_rw(virt_to_page(start), size >> PAGE_SHIFT);
Andi Kleen0c42f392008-01-30 13:33:42 +0100917
Ingo Molnard7d119d2008-01-30 13:34:10 +0100918 printk(KERN_INFO "Testing CPA: write protecting again\n");
Arjan van de Ven6d238cc2008-01-30 13:34:06 +0100919 set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
Andi Kleen0c42f392008-01-30 13:33:42 +0100920#endif
Arjan van de Ven63aaf302006-01-06 00:12:02 -0800921}
922#endif
923
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800924void free_init_pages(char *what, unsigned long begin, unsigned long end)
925{
Ingo Molnaree01f112008-01-30 13:34:09 +0100926#ifdef CONFIG_DEBUG_PAGEALLOC
927 /*
928 * If debugging page accesses then do not free this memory but
929 * mark them not present - any buggy init-section access will
930 * create a kernel page fault:
931 */
932 printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n",
933 begin, PAGE_ALIGN(end));
934 set_memory_np(begin, (end - begin) >> PAGE_SHIFT);
935#else
Ingo Molnar86f03982008-01-30 13:34:09 +0100936 unsigned long addr;
937
Arjan van de Ven3c1df682008-01-30 13:34:07 +0100938 /*
939 * We just marked the kernel text read only above, now that
940 * we are going to free part of that, we need to make that
941 * writeable first.
942 */
943 set_memory_rw(begin, (end - begin) >> PAGE_SHIFT);
944
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800945 for (addr = begin; addr < end; addr += PAGE_SIZE) {
Linus Torvaldse3ebadd2007-05-07 08:44:24 -0700946 ClearPageReserved(virt_to_page(addr));
947 init_page_count(virt_to_page(addr));
948 memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
949 free_page(addr);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800950 totalram_pages++;
951 }
Jan Beulich6fb14752007-05-02 19:27:10 +0200952 printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
Ingo Molnaree01f112008-01-30 13:34:09 +0100953#endif
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800954}
955
956void free_initmem(void)
957{
958 free_init_pages("unused kernel memory",
Linus Torvaldse3ebadd2007-05-07 08:44:24 -0700959 (unsigned long)(&__init_begin),
960 (unsigned long)(&__init_end));
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800961}
Arjan van de Ven63aaf302006-01-06 00:12:02 -0800962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963#ifdef CONFIG_BLK_DEV_INITRD
964void free_initrd_mem(unsigned long start, unsigned long end)
965{
Linus Torvaldse3ebadd2007-05-07 08:44:24 -0700966 free_init_pages("initrd memory", start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967}
968#endif
Yinghai Lud2dbf342008-06-13 02:00:56 -0700969
970int __init reserve_bootmem_generic(unsigned long phys, unsigned long len,
971 int flags)
972{
973 return reserve_bootmem(phys, len, flags);
974}