blob: fb5694d788bfda1274b6647b8824d76f3cce9cff [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 pgd_idx = pgd_index(PAGE_OFFSET);
167 pgd = pgd_base + pgd_idx;
168 pfn = 0;
169
170 for (; pgd_idx < PTRS_PER_PGD; pgd++, pgd_idx++) {
171 pmd = one_md_table_init(pgd);
172 if (pfn >= max_low_pfn)
173 continue;
Ingo Molnar8550eb92008-01-30 13:34:10 +0100174
Jeremy Fitzhardingef3f20de2008-01-30 13:31:09 +0100175 for (pmd_idx = 0;
176 pmd_idx < PTRS_PER_PMD && pfn < max_low_pfn;
177 pmd++, pmd_idx++) {
Ingo Molnar8550eb92008-01-30 13:34:10 +0100178 unsigned int addr = pfn * PAGE_SIZE + PAGE_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Ingo Molnar8550eb92008-01-30 13:34:10 +0100180 /*
181 * Map with big pages if possible, otherwise
182 * create normal page tables:
Andi Kleenf5c24a72008-03-12 03:53:30 +0100183 *
184 * Don't use a large page for the first 2/4MB of memory
185 * because there are often fixed size MTRRs in there
186 * and overlapping MTRRs into large pages can cause
187 * slowdowns.
Ingo Molnar8550eb92008-01-30 13:34:10 +0100188 */
Andi Kleenf5c24a72008-03-12 03:53:30 +0100189 if (cpu_has_pse && !(pgd_idx == 0 && pmd_idx == 0)) {
Ingo Molnar8550eb92008-01-30 13:34:10 +0100190 unsigned int addr2;
Jeremy Fitzhardingef3f20de2008-01-30 13:31:09 +0100191 pgprot_t prot = PAGE_KERNEL_LARGE;
192
Ingo Molnar8550eb92008-01-30 13:34:10 +0100193 addr2 = (pfn + PTRS_PER_PTE-1) * PAGE_SIZE +
Jeremy Fitzhardingef3f20de2008-01-30 13:31:09 +0100194 PAGE_OFFSET + PAGE_SIZE-1;
195
Ingo Molnar8550eb92008-01-30 13:34:10 +0100196 if (is_kernel_text(addr) ||
197 is_kernel_text(addr2))
Jeremy Fitzhardingef3f20de2008-01-30 13:31:09 +0100198 prot = PAGE_KERNEL_LARGE_EXEC;
199
200 set_pmd(pmd, pfn_pmd(pfn, prot));
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 pfn += PTRS_PER_PTE;
Thomas Gleixner67794292008-03-21 21:27:10 +0100203 max_pfn_mapped = pfn;
Ingo Molnar8550eb92008-01-30 13:34:10 +0100204 continue;
205 }
206 pte = one_page_table_init(pmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Ingo Molnar8550eb92008-01-30 13:34:10 +0100208 for (pte_ofs = 0;
209 pte_ofs < PTRS_PER_PTE && pfn < max_low_pfn;
210 pte++, pfn++, pte_ofs++, addr += PAGE_SIZE) {
211 pgprot_t prot = PAGE_KERNEL;
Jeremy Fitzhardingef3f20de2008-01-30 13:31:09 +0100212
Ingo Molnar8550eb92008-01-30 13:34:10 +0100213 if (is_kernel_text(addr))
214 prot = PAGE_KERNEL_EXEC;
Jeremy Fitzhardingef3f20de2008-01-30 13:31:09 +0100215
Ingo Molnar8550eb92008-01-30 13:34:10 +0100216 set_pte(pte, pfn_pte(pfn, prot));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
Thomas Gleixner67794292008-03-21 21:27:10 +0100218 max_pfn_mapped = pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
220 }
221}
222
Arjan van de Venae531c22008-04-24 23:40:47 +0200223/*
224 * devmem_is_allowed() checks to see if /dev/mem access to a certain address
225 * is valid. The argument is a physical page number.
226 *
227 *
228 * On x86, access has to be given to the first megabyte of ram because that area
229 * contains bios code and data regions used by X and dosemu and similar apps.
230 * Access has to be given to non-kernel-ram areas as well, these contain the PCI
231 * mmio resources as well as potential bios/acpi data regions.
232 */
233int devmem_is_allowed(unsigned long pagenr)
234{
235 if (pagenr <= 256)
236 return 1;
237 if (!page_is_ram(pagenr))
238 return 1;
239 return 0;
240}
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242#ifdef CONFIG_HIGHMEM
243pte_t *kmap_pte;
244pgprot_t kmap_prot;
245
Ingo Molnar8550eb92008-01-30 13:34:10 +0100246static inline pte_t *kmap_get_fixmap_pte(unsigned long vaddr)
247{
248 return pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k(vaddr),
249 vaddr), vaddr), vaddr);
250}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252static void __init kmap_init(void)
253{
254 unsigned long kmap_vstart;
255
Ingo Molnar8550eb92008-01-30 13:34:10 +0100256 /*
257 * Cache the first kmap pte:
258 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN);
260 kmap_pte = kmap_get_fixmap_pte(kmap_vstart);
261
262 kmap_prot = PAGE_KERNEL;
263}
264
265static void __init permanent_kmaps_init(pgd_t *pgd_base)
266{
Ingo Molnar8550eb92008-01-30 13:34:10 +0100267 unsigned long vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 pgd_t *pgd;
269 pud_t *pud;
270 pmd_t *pmd;
271 pte_t *pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 vaddr = PKMAP_BASE;
274 page_table_range_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, pgd_base);
275
276 pgd = swapper_pg_dir + pgd_index(vaddr);
277 pud = pud_offset(pgd, vaddr);
278 pmd = pmd_offset(pud, vaddr);
279 pte = pte_offset_kernel(pmd, vaddr);
Ingo Molnar8550eb92008-01-30 13:34:10 +0100280 pkmap_page_table = pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
Yinghai Lucc9f7a02008-06-16 16:11:08 -0700283static void __init add_one_highpage_init(struct page *page, int pfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Yinghai Lucc9f7a02008-06-16 16:11:08 -0700285 ClearPageReserved(page);
286 init_page_count(page);
287 __free_page(page);
288 totalhigh_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
Yinghai Lub5bc6c02008-06-14 18:32:52 -0700291struct add_highpages_data {
292 unsigned long start_pfn;
293 unsigned long end_pfn;
Yinghai Lub5bc6c02008-06-14 18:32:52 -0700294};
295
296static void __init add_highpages_work_fn(unsigned long start_pfn,
297 unsigned long end_pfn, void *datax)
298{
299 int node_pfn;
300 struct page *page;
301 unsigned long final_start_pfn, final_end_pfn;
302 struct add_highpages_data *data;
Yinghai Lub5bc6c02008-06-14 18:32:52 -0700303
304 data = (struct add_highpages_data *)datax;
Yinghai Lub5bc6c02008-06-14 18:32:52 -0700305
306 final_start_pfn = max(start_pfn, data->start_pfn);
307 final_end_pfn = min(end_pfn, data->end_pfn);
308 if (final_start_pfn >= final_end_pfn)
309 return;
310
311 for (node_pfn = final_start_pfn; node_pfn < final_end_pfn;
312 node_pfn++) {
313 if (!pfn_valid(node_pfn))
314 continue;
315 page = pfn_to_page(node_pfn);
Yinghai Lucc9f7a02008-06-16 16:11:08 -0700316 add_one_highpage_init(page, node_pfn);
Yinghai Lub5bc6c02008-06-14 18:32:52 -0700317 }
318
319}
320
321void __init add_highpages_with_active_regions(int nid, unsigned long start_pfn,
Yinghai Lucc9f7a02008-06-16 16:11:08 -0700322 unsigned long end_pfn)
Yinghai Lub5bc6c02008-06-14 18:32:52 -0700323{
324 struct add_highpages_data data;
325
326 data.start_pfn = start_pfn;
327 data.end_pfn = end_pfn;
Yinghai Lub5bc6c02008-06-14 18:32:52 -0700328
329 work_with_active_regions(nid, add_highpages_work_fn, &data);
330}
331
Ingo Molnar8550eb92008-01-30 13:34:10 +0100332#ifndef CONFIG_NUMA
Yinghai Lucc9f7a02008-06-16 16:11:08 -0700333static void __init set_highmem_pages_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Yinghai Lucc9f7a02008-06-16 16:11:08 -0700335 add_highpages_with_active_regions(0, highstart_pfn, highend_pfn);
Ingo Molnar8550eb92008-01-30 13:34:10 +0100336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 totalram_pages += totalhigh_pages;
338}
Ingo Molnar8550eb92008-01-30 13:34:10 +0100339#endif /* !CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341#else
Ingo Molnar8550eb92008-01-30 13:34:10 +0100342# define kmap_init() do { } while (0)
343# define permanent_kmaps_init(pgd_base) do { } while (0)
Yinghai Lucc9f7a02008-06-16 16:11:08 -0700344# define set_highmem_pages_init() do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345#endif /* CONFIG_HIGHMEM */
346
Andi Kleenc93c82b2008-01-30 13:33:50 +0100347pteval_t __PAGE_KERNEL = _PAGE_KERNEL;
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700348EXPORT_SYMBOL(__PAGE_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Ingo Molnar8550eb92008-01-30 13:34:10 +0100350pteval_t __PAGE_KERNEL_EXEC = _PAGE_KERNEL_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200352void __init native_pagetable_setup_start(pgd_t *base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Ian Campbell551889a2008-02-09 23:24:09 +0100354 unsigned long pfn, va;
355 pgd_t *pgd;
356 pud_t *pud;
357 pmd_t *pmd;
358 pte_t *pte;
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200359
360 /*
Ian Campbell551889a2008-02-09 23:24:09 +0100361 * Remove any mappings which extend past the end of physical
362 * memory from the boot time page table:
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200363 */
Ian Campbell551889a2008-02-09 23:24:09 +0100364 for (pfn = max_low_pfn + 1; pfn < 1<<(32-PAGE_SHIFT); pfn++) {
365 va = PAGE_OFFSET + (pfn<<PAGE_SHIFT);
366 pgd = base + pgd_index(va);
367 if (!pgd_present(*pgd))
368 break;
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200369
Ian Campbell551889a2008-02-09 23:24:09 +0100370 pud = pud_offset(pgd, va);
371 pmd = pmd_offset(pud, va);
372 if (!pmd_present(*pmd))
373 break;
374
375 pte = pte_offset_kernel(pmd, va);
376 if (!pte_present(*pte))
377 break;
378
379 pte_clear(NULL, va, pte);
380 }
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -0700381 paravirt_alloc_pmd(&init_mm, __pa(base) >> PAGE_SHIFT);
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200382}
383
384void __init native_pagetable_setup_done(pgd_t *base)
385{
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200386}
387
388/*
389 * Build a proper pagetable for the kernel mappings. Up until this
390 * point, we've been running on some set of pagetables constructed by
391 * the boot process.
392 *
393 * If we're booting on native hardware, this will be a pagetable
Ian Campbell551889a2008-02-09 23:24:09 +0100394 * constructed in arch/x86/kernel/head_32.S. The root of the
395 * pagetable will be swapper_pg_dir.
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200396 *
397 * If we're booting paravirtualized under a hypervisor, then there are
398 * more options: we may already be running PAE, and the pagetable may
399 * or may not be based in swapper_pg_dir. In any case,
400 * paravirt_pagetable_setup_start() will set up swapper_pg_dir
401 * appropriately for the rest of the initialization to work.
402 *
403 * In general, pagetable_init() assumes that the pagetable may already
404 * be partially populated, and so it avoids stomping on any existing
405 * mappings.
406 */
Ingo Molnar8550eb92008-01-30 13:34:10 +0100407static void __init pagetable_init(void)
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200408{
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200409 pgd_t *pgd_base = swapper_pg_dir;
Ingo Molnar8550eb92008-01-30 13:34:10 +0100410 unsigned long vaddr, end;
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200411
412 paravirt_pagetable_setup_start(pgd_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 /* Enable PSE if available */
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200415 if (cpu_has_pse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 set_in_cr4(X86_CR4_PSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 /* Enable PGE if available */
419 if (cpu_has_pge) {
420 set_in_cr4(X86_CR4_PGE);
421 __PAGE_KERNEL |= _PAGE_GLOBAL;
422 __PAGE_KERNEL_EXEC |= _PAGE_GLOBAL;
423 }
424
425 kernel_physical_mapping_init(pgd_base);
426 remap_numa_kva();
427
428 /*
429 * Fixed mappings, only the page table structure has to be
430 * created - mappings will be set by set_fixmap():
431 */
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100432 early_ioremap_clear();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200434 end = (FIXADDR_TOP + PMD_SIZE - 1) & PMD_MASK;
435 page_table_range_init(vaddr, end, pgd_base);
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100436 early_ioremap_reset();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 permanent_kmaps_init(pgd_base);
439
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200440 paravirt_pagetable_setup_done(pgd_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
Rafael J. Wysockia6eb84b2008-02-01 15:28:16 +0100443#ifdef CONFIG_ACPI_SLEEP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444/*
Rafael J. Wysockia6eb84b2008-02-01 15:28:16 +0100445 * ACPI suspend needs this for resume, because things like the intel-agp
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 * driver might have split up a kernel 4MB mapping.
447 */
Rafael J. Wysockia6eb84b2008-02-01 15:28:16 +0100448char swsusp_pg_dir[PAGE_SIZE]
Ingo Molnar8550eb92008-01-30 13:34:10 +0100449 __attribute__ ((aligned(PAGE_SIZE)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451static inline void save_pg_dir(void)
452{
453 memcpy(swsusp_pg_dir, swapper_pg_dir, PAGE_SIZE);
454}
Rafael J. Wysockia6eb84b2008-02-01 15:28:16 +0100455#else /* !CONFIG_ACPI_SLEEP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456static inline void save_pg_dir(void)
457{
458}
Rafael J. Wysockia6eb84b2008-02-01 15:28:16 +0100459#endif /* !CONFIG_ACPI_SLEEP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Ingo Molnar8550eb92008-01-30 13:34:10 +0100461void zap_low_mappings(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
463 int i;
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 /*
466 * Zap initial low-memory mappings.
467 *
468 * Note that "pgd_clear()" doesn't do it for
469 * us, because pgd_clear() is a no-op on i386.
470 */
Jeremy Fitzhardinge68db0652008-03-17 16:37:13 -0700471 for (i = 0; i < KERNEL_PGD_BOUNDARY; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472#ifdef CONFIG_X86_PAE
473 set_pgd(swapper_pg_dir+i, __pgd(1 + __pa(empty_zero_page)));
474#else
475 set_pgd(swapper_pg_dir+i, __pgd(0));
476#endif
Ingo Molnar8550eb92008-01-30 13:34:10 +0100477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 flush_tlb_all();
479}
480
Ingo Molnar8550eb92008-01-30 13:34:10 +0100481int nx_enabled;
Jan Beulichd5321ab2007-07-21 17:10:26 +0200482
Jeremy Fitzhardinge6fdc05d2008-01-30 13:32:57 +0100483pteval_t __supported_pte_mask __read_mostly = ~_PAGE_NX;
484EXPORT_SYMBOL_GPL(__supported_pte_mask);
485
Jan Beulichd5321ab2007-07-21 17:10:26 +0200486#ifdef CONFIG_X86_PAE
487
Ingo Molnar8550eb92008-01-30 13:34:10 +0100488static int disable_nx __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490/*
491 * noexec = on|off
492 *
493 * Control non executable mappings.
494 *
495 * on Enable
496 * off Disable
497 */
Rusty Russell1a3f2392006-09-26 10:52:32 +0200498static int __init noexec_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Rusty Russell1a3f2392006-09-26 10:52:32 +0200500 if (!str || !strcmp(str, "on")) {
501 if (cpu_has_nx) {
502 __supported_pte_mask |= _PAGE_NX;
503 disable_nx = 0;
504 }
Ingo Molnar8550eb92008-01-30 13:34:10 +0100505 } else {
506 if (!strcmp(str, "off")) {
507 disable_nx = 1;
508 __supported_pte_mask &= ~_PAGE_NX;
509 } else {
510 return -EINVAL;
511 }
512 }
Rusty Russell1a3f2392006-09-26 10:52:32 +0200513
514 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
Rusty Russell1a3f2392006-09-26 10:52:32 +0200516early_param("noexec", noexec_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518static void __init set_nx(void)
519{
520 unsigned int v[4], l, h;
521
522 if (cpu_has_pae && (cpuid_eax(0x80000000) > 0x80000001)) {
523 cpuid(0x80000001, &v[0], &v[1], &v[2], &v[3]);
Ingo Molnar8550eb92008-01-30 13:34:10 +0100524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 if ((v[3] & (1 << 20)) && !disable_nx) {
526 rdmsr(MSR_EFER, l, h);
527 l |= EFER_NX;
528 wrmsr(MSR_EFER, l, h);
529 nx_enabled = 1;
530 __supported_pte_mask |= _PAGE_NX;
531 }
532 }
533}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534#endif
535
536/*
537 * paging_init() sets up the page tables - note that the first 8MB are
538 * already mapped by head.S.
539 *
540 * This routines also unmaps the page at virtual kernel address 0, so
541 * that we can trap those pesky NULL-reference errors in the kernel.
542 */
543void __init paging_init(void)
544{
545#ifdef CONFIG_X86_PAE
546 set_nx();
547 if (nx_enabled)
Ingo Molnard7d119d2008-01-30 13:34:10 +0100548 printk(KERN_INFO "NX (Execute Disable) protection: active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 pagetable_init();
551
552 load_cr3(swapper_pg_dir);
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 __flush_tlb_all();
555
556 kmap_init();
557}
558
559/*
560 * Test if the WP bit works in supervisor mode. It isn't supported on 386's
Dmitri Vorobievf7f17a62008-04-21 00:47:55 +0400561 * and also on some strange 486's. All 586+'s are OK. This used to involve
562 * black magic jumps to work around some nasty CPU bugs, but fortunately the
563 * switch to using exceptions got rid of all that.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565static void __init test_wp_bit(void)
566{
Ingo Molnard7d119d2008-01-30 13:34:10 +0100567 printk(KERN_INFO
568 "Checking if this processor honours the WP bit even in supervisor mode...");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 /* Any page-aligned address will do, the test is non-destructive */
571 __set_fixmap(FIX_WP_TEST, __pa(&swapper_pg_dir), PAGE_READONLY);
572 boot_cpu_data.wp_works_ok = do_test_wp_bit();
573 clear_fixmap(FIX_WP_TEST);
574
575 if (!boot_cpu_data.wp_works_ok) {
Ingo Molnard7d119d2008-01-30 13:34:10 +0100576 printk(KERN_CONT "No.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577#ifdef CONFIG_X86_WP_WORKS_OK
Ingo Molnard7d119d2008-01-30 13:34:10 +0100578 panic(
579 "This kernel doesn't support CPU's with broken WP. Recompile it for a 386!");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580#endif
581 } else {
Ingo Molnard7d119d2008-01-30 13:34:10 +0100582 printk(KERN_CONT "Ok.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
584}
585
Ingo Molnar8550eb92008-01-30 13:34:10 +0100586static struct kcore_list kcore_mem, kcore_vmalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588void __init mem_init(void)
589{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 int codesize, reservedpages, datasize, initsize;
Yinghai Lucc9f7a02008-06-16 16:11:08 -0700591 int tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Andy Whitcroft05b79bd2005-06-23 00:07:57 -0700593#ifdef CONFIG_FLATMEM
Eric Sesterhenn8d8f3cb2006-10-03 23:34:58 +0200594 BUG_ON(!mem_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596#ifdef CONFIG_HIGHMEM
597 /* check that fixmap and pkmap do not overlap */
Ingo Molnard7d119d2008-01-30 13:34:10 +0100598 if (PKMAP_BASE + LAST_PKMAP*PAGE_SIZE >= FIXADDR_START) {
599 printk(KERN_ERR
600 "fixmap and kmap areas overlap - this will crash\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 printk(KERN_ERR "pkstart: %lxh pkend: %lxh fixstart %lxh\n",
Ingo Molnard7d119d2008-01-30 13:34:10 +0100602 PKMAP_BASE, PKMAP_BASE + LAST_PKMAP*PAGE_SIZE,
603 FIXADDR_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 BUG();
605 }
606#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 /* this will put all low memory onto the freelists */
608 totalram_pages += free_all_bootmem();
609
610 reservedpages = 0;
611 for (tmp = 0; tmp < max_low_pfn; tmp++)
612 /*
Ingo Molnar8550eb92008-01-30 13:34:10 +0100613 * Only count reserved RAM pages:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 */
615 if (page_is_ram(tmp) && PageReserved(pfn_to_page(tmp)))
616 reservedpages++;
617
Yinghai Lucc9f7a02008-06-16 16:11:08 -0700618 set_highmem_pages_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 codesize = (unsigned long) &_etext - (unsigned long) &_text;
621 datasize = (unsigned long) &_edata - (unsigned long) &_etext;
622 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
623
Ingo Molnar8550eb92008-01-30 13:34:10 +0100624 kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
625 kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 VMALLOC_END-VMALLOC_START);
627
Ingo Molnar8550eb92008-01-30 13:34:10 +0100628 printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, "
629 "%dk reserved, %dk data, %dk init, %ldk highmem)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
631 num_physpages << (PAGE_SHIFT-10),
632 codesize >> 10,
633 reservedpages << (PAGE_SHIFT-10),
634 datasize >> 10,
635 initsize >> 10,
636 (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10))
637 );
638
Jeremy Fitzhardinge052e7992006-09-25 23:32:25 -0700639#if 1 /* double-sanity-check paranoia */
Ingo Molnard7d119d2008-01-30 13:34:10 +0100640 printk(KERN_INFO "virtual kernel memory layout:\n"
Ingo Molnar8550eb92008-01-30 13:34:10 +0100641 " fixmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
Jeremy Fitzhardinge052e7992006-09-25 23:32:25 -0700642#ifdef CONFIG_HIGHMEM
Ingo Molnar8550eb92008-01-30 13:34:10 +0100643 " pkmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
Jeremy Fitzhardinge052e7992006-09-25 23:32:25 -0700644#endif
Ingo Molnar8550eb92008-01-30 13:34:10 +0100645 " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n"
646 " lowmem : 0x%08lx - 0x%08lx (%4ld MB)\n"
647 " .init : 0x%08lx - 0x%08lx (%4ld kB)\n"
648 " .data : 0x%08lx - 0x%08lx (%4ld kB)\n"
649 " .text : 0x%08lx - 0x%08lx (%4ld kB)\n",
650 FIXADDR_START, FIXADDR_TOP,
651 (FIXADDR_TOP - FIXADDR_START) >> 10,
Jeremy Fitzhardinge052e7992006-09-25 23:32:25 -0700652
653#ifdef CONFIG_HIGHMEM
Ingo Molnar8550eb92008-01-30 13:34:10 +0100654 PKMAP_BASE, PKMAP_BASE+LAST_PKMAP*PAGE_SIZE,
655 (LAST_PKMAP*PAGE_SIZE) >> 10,
Jeremy Fitzhardinge052e7992006-09-25 23:32:25 -0700656#endif
657
Ingo Molnar8550eb92008-01-30 13:34:10 +0100658 VMALLOC_START, VMALLOC_END,
659 (VMALLOC_END - VMALLOC_START) >> 20,
Jeremy Fitzhardinge052e7992006-09-25 23:32:25 -0700660
Ingo Molnar8550eb92008-01-30 13:34:10 +0100661 (unsigned long)__va(0), (unsigned long)high_memory,
662 ((unsigned long)high_memory - (unsigned long)__va(0)) >> 20,
Jeremy Fitzhardinge052e7992006-09-25 23:32:25 -0700663
Ingo Molnar8550eb92008-01-30 13:34:10 +0100664 (unsigned long)&__init_begin, (unsigned long)&__init_end,
665 ((unsigned long)&__init_end -
666 (unsigned long)&__init_begin) >> 10,
Jeremy Fitzhardinge052e7992006-09-25 23:32:25 -0700667
Ingo Molnar8550eb92008-01-30 13:34:10 +0100668 (unsigned long)&_etext, (unsigned long)&_edata,
669 ((unsigned long)&_edata - (unsigned long)&_etext) >> 10,
Jeremy Fitzhardinge052e7992006-09-25 23:32:25 -0700670
Ingo Molnar8550eb92008-01-30 13:34:10 +0100671 (unsigned long)&_text, (unsigned long)&_etext,
672 ((unsigned long)&_etext - (unsigned long)&_text) >> 10);
Jeremy Fitzhardinge052e7992006-09-25 23:32:25 -0700673
674#ifdef CONFIG_HIGHMEM
Ingo Molnar8550eb92008-01-30 13:34:10 +0100675 BUG_ON(PKMAP_BASE + LAST_PKMAP*PAGE_SIZE > FIXADDR_START);
676 BUG_ON(VMALLOC_END > PKMAP_BASE);
Jeremy Fitzhardinge052e7992006-09-25 23:32:25 -0700677#endif
Ingo Molnar8550eb92008-01-30 13:34:10 +0100678 BUG_ON(VMALLOC_START > VMALLOC_END);
679 BUG_ON((unsigned long)high_memory > VMALLOC_START);
Jeremy Fitzhardinge052e7992006-09-25 23:32:25 -0700680#endif /* double-sanity-check paranoia */
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (boot_cpu_data.wp_works_ok < 0)
683 test_wp_bit();
684
Thomas Gleixner76ebd052008-02-09 23:24:09 +0100685 cpa_init();
Hugh Dickins61165d72008-05-13 14:26:57 +0100686 save_pg_dir();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 zap_low_mappings();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
KAMEZAWA Hiroyukiad8f5792006-05-20 15:00:03 -0700690#ifdef CONFIG_MEMORY_HOTPLUG
Yasunori Gotobc02af92006-06-27 02:53:30 -0700691int arch_add_memory(int nid, u64 start, u64 size)
Dave Hansen05039b92005-10-29 18:16:57 -0700692{
Yasunori Goto7c7e9422006-12-22 01:11:13 -0800693 struct pglist_data *pgdata = NODE_DATA(nid);
Christoph Lameter776ed982006-09-25 23:31:09 -0700694 struct zone *zone = pgdata->node_zones + ZONE_HIGHMEM;
Dave Hansen05039b92005-10-29 18:16:57 -0700695 unsigned long start_pfn = start >> PAGE_SHIFT;
696 unsigned long nr_pages = size >> PAGE_SHIFT;
697
698 return __add_pages(zone, start_pfn, nr_pages);
699}
Andi Kleen9d99aaa2006-04-07 19:49:15 +0200700#endif
Dave Hansen05039b92005-10-29 18:16:57 -0700701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702/*
703 * This function cannot be __init, since exceptions don't work in that
704 * section. Put this after the callers, so that it cannot be inlined.
705 */
Ingo Molnar8550eb92008-01-30 13:34:10 +0100706static noinline int do_test_wp_bit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
708 char tmp_reg;
709 int flag;
710
711 __asm__ __volatile__(
Ingo Molnar8550eb92008-01-30 13:34:10 +0100712 " movb %0, %1 \n"
713 "1: movb %1, %0 \n"
714 " xorl %2, %2 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 "2: \n"
H. Peter Anvinf832ff12008-02-04 16:47:58 +0100716 _ASM_EXTABLE(1b,2b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 :"=m" (*(char *)fix_to_virt(FIX_WP_TEST)),
718 "=q" (tmp_reg),
719 "=r" (flag)
720 :"2" (1)
721 :"memory");
Ingo Molnar8550eb92008-01-30 13:34:10 +0100722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return flag;
724}
725
Arjan van de Ven63aaf302006-01-06 00:12:02 -0800726#ifdef CONFIG_DEBUG_RODATA
Arjan van de Venedeed302008-01-30 13:34:08 +0100727const int rodata_test_data = 0xC3;
728EXPORT_SYMBOL_GPL(rodata_test_data);
Arjan van de Ven63aaf302006-01-06 00:12:02 -0800729
Arjan van de Ven63aaf302006-01-06 00:12:02 -0800730void mark_rodata_ro(void)
731{
Jan Beulich6fb14752007-05-02 19:27:10 +0200732 unsigned long start = PFN_ALIGN(_text);
733 unsigned long size = PFN_ALIGN(_etext) - start;
Arjan van de Ven63aaf302006-01-06 00:12:02 -0800734
Mathieu Desnoyers4e4eee02008-02-02 15:42:20 -0500735 set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
736 printk(KERN_INFO "Write protecting the kernel text: %luk\n",
737 size >> 10);
Andi Kleen0c42f392008-01-30 13:33:42 +0100738
739#ifdef CONFIG_CPA_DEBUG
Mathieu Desnoyers4e4eee02008-02-02 15:42:20 -0500740 printk(KERN_INFO "Testing CPA: Reverting %lx-%lx\n",
741 start, start+size);
742 set_pages_rw(virt_to_page(start), size>>PAGE_SHIFT);
Andi Kleen0c42f392008-01-30 13:33:42 +0100743
Mathieu Desnoyers4e4eee02008-02-02 15:42:20 -0500744 printk(KERN_INFO "Testing CPA: write protecting again\n");
745 set_pages_ro(virt_to_page(start), size>>PAGE_SHIFT);
Linus Torvalds602033e2007-07-26 12:07:21 -0700746#endif
Jan Beulich6fb14752007-05-02 19:27:10 +0200747 start += size;
748 size = (unsigned long)__end_rodata - start;
Arjan van de Ven6d238cc2008-01-30 13:34:06 +0100749 set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
Ingo Molnard7d119d2008-01-30 13:34:10 +0100750 printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
751 size >> 10);
Arjan van de Venedeed302008-01-30 13:34:08 +0100752 rodata_test();
Arjan van de Ven63aaf302006-01-06 00:12:02 -0800753
Andi Kleen0c42f392008-01-30 13:33:42 +0100754#ifdef CONFIG_CPA_DEBUG
Ingo Molnard7d119d2008-01-30 13:34:10 +0100755 printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, start + size);
Arjan van de Ven6d238cc2008-01-30 13:34:06 +0100756 set_pages_rw(virt_to_page(start), size >> PAGE_SHIFT);
Andi Kleen0c42f392008-01-30 13:33:42 +0100757
Ingo Molnard7d119d2008-01-30 13:34:10 +0100758 printk(KERN_INFO "Testing CPA: write protecting again\n");
Arjan van de Ven6d238cc2008-01-30 13:34:06 +0100759 set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
Andi Kleen0c42f392008-01-30 13:33:42 +0100760#endif
Arjan van de Ven63aaf302006-01-06 00:12:02 -0800761}
762#endif
763
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800764void free_init_pages(char *what, unsigned long begin, unsigned long end)
765{
Ingo Molnaree01f112008-01-30 13:34:09 +0100766#ifdef CONFIG_DEBUG_PAGEALLOC
767 /*
768 * If debugging page accesses then do not free this memory but
769 * mark them not present - any buggy init-section access will
770 * create a kernel page fault:
771 */
772 printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n",
773 begin, PAGE_ALIGN(end));
774 set_memory_np(begin, (end - begin) >> PAGE_SHIFT);
775#else
Ingo Molnar86f03982008-01-30 13:34:09 +0100776 unsigned long addr;
777
Arjan van de Ven3c1df682008-01-30 13:34:07 +0100778 /*
779 * We just marked the kernel text read only above, now that
780 * we are going to free part of that, we need to make that
781 * writeable first.
782 */
783 set_memory_rw(begin, (end - begin) >> PAGE_SHIFT);
784
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800785 for (addr = begin; addr < end; addr += PAGE_SIZE) {
Linus Torvaldse3ebadd2007-05-07 08:44:24 -0700786 ClearPageReserved(virt_to_page(addr));
787 init_page_count(virt_to_page(addr));
788 memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
789 free_page(addr);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800790 totalram_pages++;
791 }
Jan Beulich6fb14752007-05-02 19:27:10 +0200792 printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
Ingo Molnaree01f112008-01-30 13:34:09 +0100793#endif
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800794}
795
796void free_initmem(void)
797{
798 free_init_pages("unused kernel memory",
Linus Torvaldse3ebadd2007-05-07 08:44:24 -0700799 (unsigned long)(&__init_begin),
800 (unsigned long)(&__init_end));
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800801}
Arjan van de Ven63aaf302006-01-06 00:12:02 -0800802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803#ifdef CONFIG_BLK_DEV_INITRD
804void free_initrd_mem(unsigned long start, unsigned long end)
805{
Linus Torvaldse3ebadd2007-05-07 08:44:24 -0700806 free_init_pages("initrd memory", start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808#endif
Yinghai Lud2dbf342008-06-13 02:00:56 -0700809
810int __init reserve_bootmem_generic(unsigned long phys, unsigned long len,
811 int flags)
812{
813 return reserve_bootmem(phys, len, flags);
814}