Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/x86_64/mm/init.c |
| 3 | * |
| 4 | * Copyright (C) 1995 Linus Torvalds |
| 5 | * Copyright (C) 2000 Pavel Machek <pavel@suse.cz> |
| 6 | * Copyright (C) 2002,2003 Andi Kleen <ak@suse.de> |
| 7 | */ |
| 8 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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/swap.h> |
| 19 | #include <linux/smp.h> |
| 20 | #include <linux/init.h> |
Thomas Gleixner | 11034d5 | 2008-05-12 15:43:36 +0200 | [diff] [blame] | 21 | #include <linux/initrd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/pagemap.h> |
| 23 | #include <linux/bootmem.h> |
| 24 | #include <linux/proc_fs.h> |
Andi Kleen | 5917089 | 2005-11-05 17:25:53 +0100 | [diff] [blame] | 25 | #include <linux/pci.h> |
Jan Beulich | 6fb1475 | 2007-05-02 19:27:10 +0200 | [diff] [blame] | 26 | #include <linux/pfn.h> |
Randy Dunlap | c9cf552 | 2006-06-27 02:53:52 -0700 | [diff] [blame] | 27 | #include <linux/poison.h> |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 28 | #include <linux/dma-mapping.h> |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 29 | #include <linux/module.h> |
| 30 | #include <linux/memory_hotplug.h> |
Konrad Rzeszutek | ae32b12 | 2007-05-02 19:27:11 +0200 | [diff] [blame] | 31 | #include <linux/nmi.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
| 33 | #include <asm/processor.h> |
| 34 | #include <asm/system.h> |
| 35 | #include <asm/uaccess.h> |
| 36 | #include <asm/pgtable.h> |
| 37 | #include <asm/pgalloc.h> |
| 38 | #include <asm/dma.h> |
| 39 | #include <asm/fixmap.h> |
| 40 | #include <asm/e820.h> |
| 41 | #include <asm/apic.h> |
| 42 | #include <asm/tlb.h> |
| 43 | #include <asm/mmu_context.h> |
| 44 | #include <asm/proto.h> |
| 45 | #include <asm/smp.h> |
Andi Kleen | 2bc0414 | 2005-11-05 17:25:53 +0100 | [diff] [blame] | 46 | #include <asm/sections.h> |
Thomas Gleixner | 718fc13 | 2008-01-30 13:30:17 +0100 | [diff] [blame] | 47 | #include <asm/kdebug.h> |
Thomas Gleixner | aaa64e0 | 2008-01-30 13:30:17 +0100 | [diff] [blame] | 48 | #include <asm/numa.h> |
Harvey Harrison | 7bfeab9 | 2008-02-12 12:12:01 -0800 | [diff] [blame] | 49 | #include <asm/cacheflush.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Yinghai Lu | 064d25f | 2008-06-16 19:58:28 -0700 | [diff] [blame] | 51 | /* |
Yinghai Lu | 064d25f | 2008-06-16 19:58:28 -0700 | [diff] [blame] | 52 | * end_pfn only includes RAM, while max_pfn_mapped includes all e820 entries. |
| 53 | * The direct mapping extends to max_pfn_mapped, so that we can directly access |
| 54 | * apertures, ACPI and other tables without having to play with fixmaps. |
| 55 | */ |
Yinghai Lu | f361a45 | 2008-07-10 20:38:26 -0700 | [diff] [blame] | 56 | unsigned long max_low_pfn_mapped; |
Yinghai Lu | 064d25f | 2008-06-16 19:58:28 -0700 | [diff] [blame] | 57 | unsigned long max_pfn_mapped; |
| 58 | |
Andi Kleen | e18c687 | 2005-11-05 17:25:53 +0100 | [diff] [blame] | 59 | static unsigned long dma_reserve __initdata; |
| 60 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | DEFINE_PER_CPU(struct mmu_gather, mmu_gathers); |
| 62 | |
Ingo Molnar | 00d1c5e | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 63 | int direct_gbpages __meminitdata |
| 64 | #ifdef CONFIG_DIRECT_GBPAGES |
| 65 | = 1 |
| 66 | #endif |
| 67 | ; |
| 68 | |
| 69 | static int __init parse_direct_gbpages_off(char *arg) |
| 70 | { |
| 71 | direct_gbpages = 0; |
| 72 | return 0; |
| 73 | } |
| 74 | early_param("nogbpages", parse_direct_gbpages_off); |
| 75 | |
| 76 | static int __init parse_direct_gbpages_on(char *arg) |
| 77 | { |
| 78 | direct_gbpages = 1; |
| 79 | return 0; |
| 80 | } |
| 81 | early_param("gbpages", parse_direct_gbpages_on); |
| 82 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | /* |
| 84 | * NOTE: pagetable_init alloc all the fixmap pagetables contiguous on the |
| 85 | * physical space so we can cache the place of the first one and move |
| 86 | * around without checking the pgd every time. |
| 87 | */ |
| 88 | |
| 89 | void show_mem(void) |
| 90 | { |
Andi Kleen | e92343c | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 91 | long i, total = 0, reserved = 0; |
| 92 | long shared = 0, cached = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | struct page *page; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 94 | pg_data_t *pgdat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
Andi Kleen | e92343c | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 96 | printk(KERN_INFO "Mem-info:\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | show_free_areas(); |
KAMEZAWA Hiroyuki | ec936fc | 2006-03-27 01:15:59 -0800 | [diff] [blame] | 98 | for_each_online_pgdat(pgdat) { |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 99 | for (i = 0; i < pgdat->node_spanned_pages; ++i) { |
| 100 | /* |
| 101 | * This loop can take a while with 256 GB and |
| 102 | * 4k pages so defer the NMI watchdog: |
| 103 | */ |
| 104 | if (unlikely(i % MAX_ORDER_NR_PAGES == 0)) |
Konrad Rzeszutek | ae32b12 | 2007-05-02 19:27:11 +0200 | [diff] [blame] | 105 | touch_nmi_watchdog(); |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 106 | |
Bob Picco | 12710a5 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 107 | if (!pfn_valid(pgdat->node_start_pfn + i)) |
| 108 | continue; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | page = pfn_to_page(pgdat->node_start_pfn + i); |
| 111 | total++; |
Andi Kleen | e92343c | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 112 | if (PageReserved(page)) |
| 113 | reserved++; |
| 114 | else if (PageSwapCache(page)) |
| 115 | cached++; |
| 116 | else if (page_count(page)) |
| 117 | shared += page_count(page) - 1; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 118 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | } |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 120 | printk(KERN_INFO "%lu pages of RAM\n", total); |
| 121 | printk(KERN_INFO "%lu reserved pages\n", reserved); |
| 122 | printk(KERN_INFO "%lu pages shared\n", shared); |
| 123 | printk(KERN_INFO "%lu pages swap cached\n", cached); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | int after_bootmem; |
| 127 | |
Andi Kleen | 5f44a66 | 2006-03-25 16:30:25 +0100 | [diff] [blame] | 128 | static __init void *spp_getpage(void) |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 129 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | void *ptr; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 131 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | if (after_bootmem) |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 133 | ptr = (void *) get_zeroed_page(GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | else |
| 135 | ptr = alloc_bootmem_pages(PAGE_SIZE); |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 136 | |
| 137 | if (!ptr || ((unsigned long)ptr & ~PAGE_MASK)) { |
| 138 | panic("set_pte_phys: cannot allocate page data %s\n", |
| 139 | after_bootmem ? "after bootmem" : ""); |
| 140 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
Ingo Molnar | 10f22dd | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 142 | pr_debug("spp_getpage %p\n", ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 144 | return ptr; |
| 145 | } |
| 146 | |
Jeremy Fitzhardinge | d494a96 | 2008-06-17 11:41:59 -0700 | [diff] [blame] | 147 | void |
Eduardo Habkost | 0814e0b | 2008-06-25 00:19:22 -0400 | [diff] [blame] | 148 | set_pte_vaddr_pud(pud_t *pud_page, unsigned long vaddr, pte_t new_pte) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | pud_t *pud; |
| 151 | pmd_t *pmd; |
Jeremy Fitzhardinge | d494a96 | 2008-06-17 11:41:59 -0700 | [diff] [blame] | 152 | pte_t *pte; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
Eduardo Habkost | 0814e0b | 2008-06-25 00:19:22 -0400 | [diff] [blame] | 154 | pud = pud_page + pud_index(vaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | if (pud_none(*pud)) { |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 156 | pmd = (pmd_t *) spp_getpage(); |
Jeremy Fitzhardinge | bb23e40 | 2008-06-25 00:19:02 -0400 | [diff] [blame] | 157 | pud_populate(&init_mm, pud, pmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | if (pmd != pmd_offset(pud, 0)) { |
Ingo Molnar | 10f22dd | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 159 | printk(KERN_ERR "PAGETABLE BUG #01! %p <-> %p\n", |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 160 | pmd, pmd_offset(pud, 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | return; |
| 162 | } |
| 163 | } |
| 164 | pmd = pmd_offset(pud, vaddr); |
| 165 | if (pmd_none(*pmd)) { |
| 166 | pte = (pte_t *) spp_getpage(); |
Jeremy Fitzhardinge | bb23e40 | 2008-06-25 00:19:02 -0400 | [diff] [blame] | 167 | pmd_populate_kernel(&init_mm, pmd, pte); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | if (pte != pte_offset_kernel(pmd, 0)) { |
Ingo Molnar | 10f22dd | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 169 | printk(KERN_ERR "PAGETABLE BUG #02!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | return; |
| 171 | } |
| 172 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
| 174 | pte = pte_offset_kernel(pmd, vaddr); |
Ingo Molnar | 70c9f59 | 2008-04-25 18:05:57 +0200 | [diff] [blame] | 175 | if (!pte_none(*pte) && pte_val(new_pte) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | pte_val(*pte) != (pte_val(new_pte) & __supported_pte_mask)) |
| 177 | pte_ERROR(*pte); |
| 178 | set_pte(pte, new_pte); |
| 179 | |
| 180 | /* |
| 181 | * It's enough to flush this one mapping. |
| 182 | * (PGE mappings get flushed as well) |
| 183 | */ |
| 184 | __flush_tlb_one(vaddr); |
| 185 | } |
| 186 | |
Eduardo Habkost | 0814e0b | 2008-06-25 00:19:22 -0400 | [diff] [blame] | 187 | void |
| 188 | set_pte_vaddr(unsigned long vaddr, pte_t pteval) |
| 189 | { |
| 190 | pgd_t *pgd; |
| 191 | pud_t *pud_page; |
| 192 | |
| 193 | pr_debug("set_pte_vaddr %lx to %lx\n", vaddr, native_pte_val(pteval)); |
| 194 | |
| 195 | pgd = pgd_offset_k(vaddr); |
| 196 | if (pgd_none(*pgd)) { |
| 197 | printk(KERN_ERR |
| 198 | "PGD FIXMAP MISSING, it should be setup in head.S!\n"); |
| 199 | return; |
| 200 | } |
| 201 | pud_page = (pud_t*)pgd_page_vaddr(*pgd); |
| 202 | set_pte_vaddr_pud(pud_page, vaddr, pteval); |
| 203 | } |
| 204 | |
Thomas Gleixner | 31eedd8 | 2008-02-15 17:29:12 +0100 | [diff] [blame] | 205 | /* |
Jack Steiner | 3a9e189 | 2008-07-01 14:45:32 -0500 | [diff] [blame] | 206 | * Create large page table mappings for a range of physical addresses. |
| 207 | */ |
| 208 | static void __init __init_extra_mapping(unsigned long phys, unsigned long size, |
| 209 | pgprot_t prot) |
| 210 | { |
| 211 | pgd_t *pgd; |
| 212 | pud_t *pud; |
| 213 | pmd_t *pmd; |
| 214 | |
| 215 | BUG_ON((phys & ~PMD_MASK) || (size & ~PMD_MASK)); |
| 216 | for (; size; phys += PMD_SIZE, size -= PMD_SIZE) { |
| 217 | pgd = pgd_offset_k((unsigned long)__va(phys)); |
| 218 | if (pgd_none(*pgd)) { |
| 219 | pud = (pud_t *) spp_getpage(); |
| 220 | set_pgd(pgd, __pgd(__pa(pud) | _KERNPG_TABLE | |
| 221 | _PAGE_USER)); |
| 222 | } |
| 223 | pud = pud_offset(pgd, (unsigned long)__va(phys)); |
| 224 | if (pud_none(*pud)) { |
| 225 | pmd = (pmd_t *) spp_getpage(); |
| 226 | set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE | |
| 227 | _PAGE_USER)); |
| 228 | } |
| 229 | pmd = pmd_offset(pud, phys); |
| 230 | BUG_ON(!pmd_none(*pmd)); |
| 231 | set_pmd(pmd, __pmd(phys | pgprot_val(prot))); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | void __init init_extra_mapping_wb(unsigned long phys, unsigned long size) |
| 236 | { |
| 237 | __init_extra_mapping(phys, size, PAGE_KERNEL_LARGE); |
| 238 | } |
| 239 | |
| 240 | void __init init_extra_mapping_uc(unsigned long phys, unsigned long size) |
| 241 | { |
| 242 | __init_extra_mapping(phys, size, PAGE_KERNEL_LARGE_NOCACHE); |
| 243 | } |
| 244 | |
| 245 | /* |
Ingo Molnar | 88f3aec | 2008-02-21 11:04:11 +0100 | [diff] [blame] | 246 | * The head.S code sets up the kernel high mapping: |
| 247 | * |
| 248 | * from __START_KERNEL_map to __START_KERNEL_map + size (== _end-_text) |
Thomas Gleixner | 31eedd8 | 2008-02-15 17:29:12 +0100 | [diff] [blame] | 249 | * |
| 250 | * phys_addr holds the negative offset to the kernel, which is added |
| 251 | * to the compile time generated pmds. This results in invalid pmds up |
| 252 | * to the point where we hit the physaddr 0 mapping. |
| 253 | * |
| 254 | * We limit the mappings to the region from _text to _end. _end is |
| 255 | * rounded up to the 2MB boundary. This catches the invalid pmds as |
| 256 | * well, as they are located before _text: |
| 257 | */ |
| 258 | void __init cleanup_highmap(void) |
| 259 | { |
| 260 | unsigned long vaddr = __START_KERNEL_map; |
| 261 | unsigned long end = round_up((unsigned long)_end, PMD_SIZE) - 1; |
| 262 | pmd_t *pmd = level2_kernel_pgt; |
| 263 | pmd_t *last_pmd = pmd + PTRS_PER_PMD; |
| 264 | |
| 265 | for (; pmd < last_pmd; pmd++, vaddr += PMD_SIZE) { |
Hugh Dickins | 2884f11 | 2008-05-28 19:36:07 +0100 | [diff] [blame] | 266 | if (pmd_none(*pmd)) |
Thomas Gleixner | 31eedd8 | 2008-02-15 17:29:12 +0100 | [diff] [blame] | 267 | continue; |
| 268 | if (vaddr < (unsigned long) _text || vaddr > end) |
| 269 | set_pmd(pmd, __pmd(0)); |
| 270 | } |
| 271 | } |
| 272 | |
Andi Kleen | 7517527 | 2008-01-30 13:33:17 +0100 | [diff] [blame] | 273 | static unsigned long __initdata table_start; |
| 274 | static unsigned long __meminitdata table_end; |
Yinghai Lu | d86623a | 2008-06-24 14:57:29 -0700 | [diff] [blame] | 275 | static unsigned long __meminitdata table_top; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
Vivek Goyal | dafe41e | 2007-05-02 19:27:06 +0200 | [diff] [blame] | 277 | static __meminit void *alloc_low_page(unsigned long *phys) |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 278 | { |
Vivek Goyal | dafe41e | 2007-05-02 19:27:06 +0200 | [diff] [blame] | 279 | unsigned long pfn = table_end++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | void *adr; |
| 281 | |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 282 | if (after_bootmem) { |
| 283 | adr = (void *)get_zeroed_page(GFP_ATOMIC); |
| 284 | *phys = __pa(adr); |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 285 | |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 286 | return adr; |
| 287 | } |
| 288 | |
Yinghai Lu | d86623a | 2008-06-24 14:57:29 -0700 | [diff] [blame] | 289 | if (pfn >= table_top) |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 290 | panic("alloc_low_page: ran out of memory"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
Vivek Goyal | dafe41e | 2007-05-02 19:27:06 +0200 | [diff] [blame] | 292 | adr = early_ioremap(pfn * PAGE_SIZE, PAGE_SIZE); |
| 293 | memset(adr, 0, PAGE_SIZE); |
| 294 | *phys = pfn * PAGE_SIZE; |
| 295 | return adr; |
| 296 | } |
| 297 | |
| 298 | static __meminit void unmap_low_page(void *adr) |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 299 | { |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 300 | if (after_bootmem) |
| 301 | return; |
| 302 | |
Vivek Goyal | dafe41e | 2007-05-02 19:27:06 +0200 | [diff] [blame] | 303 | early_iounmap(adr, PAGE_SIZE); |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 304 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 306 | static unsigned long __meminit |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 307 | phys_pte_init(pte_t *pte_page, unsigned long addr, unsigned long end) |
| 308 | { |
| 309 | unsigned pages = 0; |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 310 | unsigned long last_map_addr = end; |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 311 | int i; |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 312 | |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 313 | pte_t *pte = pte_page + pte_index(addr); |
| 314 | |
| 315 | for(i = pte_index(addr); i < PTRS_PER_PTE; i++, addr += PAGE_SIZE, pte++) { |
| 316 | |
| 317 | if (addr >= end) { |
| 318 | if (!after_bootmem) { |
| 319 | for(; i < PTRS_PER_PTE; i++, pte++) |
| 320 | set_pte(pte, __pte(0)); |
| 321 | } |
| 322 | break; |
| 323 | } |
| 324 | |
| 325 | if (pte_val(*pte)) |
| 326 | continue; |
| 327 | |
| 328 | if (0) |
| 329 | printk(" pte=%p addr=%lx pte=%016lx\n", |
| 330 | pte, addr, pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL).pte); |
| 331 | set_pte(pte, pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL)); |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 332 | last_map_addr = (addr & PAGE_MASK) + PAGE_SIZE; |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 333 | pages++; |
| 334 | } |
| 335 | update_page_count(PG_LEVEL_4K, pages); |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 336 | |
| 337 | return last_map_addr; |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 338 | } |
| 339 | |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 340 | static unsigned long __meminit |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 341 | phys_pte_update(pmd_t *pmd, unsigned long address, unsigned long end) |
| 342 | { |
| 343 | pte_t *pte = (pte_t *)pmd_page_vaddr(*pmd); |
| 344 | |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 345 | return phys_pte_init(pte, address, end); |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 346 | } |
| 347 | |
Andi Kleen | cc61503 | 2008-03-12 03:53:28 +0100 | [diff] [blame] | 348 | static unsigned long __meminit |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 349 | phys_pmd_init(pmd_t *pmd_page, unsigned long address, unsigned long end, |
| 350 | unsigned long page_size_mask) |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 351 | { |
Andi Kleen | ce0c0e5 | 2008-05-02 11:46:49 +0200 | [diff] [blame] | 352 | unsigned long pages = 0; |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 353 | unsigned long last_map_addr = end; |
Andi Kleen | ce0c0e5 | 2008-05-02 11:46:49 +0200 | [diff] [blame] | 354 | |
Keith Mannthey | 6ad9165 | 2006-09-26 10:52:36 +0200 | [diff] [blame] | 355 | int i = pmd_index(address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | |
Keith Mannthey | 6ad9165 | 2006-09-26 10:52:36 +0200 | [diff] [blame] | 357 | for (; i < PTRS_PER_PMD; i++, address += PMD_SIZE) { |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 358 | unsigned long pte_phys; |
Keith Mannthey | 6ad9165 | 2006-09-26 10:52:36 +0200 | [diff] [blame] | 359 | pmd_t *pmd = pmd_page + pmd_index(address); |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 360 | pte_t *pte; |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 361 | |
Jan Beulich | 5f51e13 | 2006-06-26 13:59:02 +0200 | [diff] [blame] | 362 | if (address >= end) { |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 363 | if (!after_bootmem) { |
Jan Beulich | 5f51e13 | 2006-06-26 13:59:02 +0200 | [diff] [blame] | 364 | for (; i < PTRS_PER_PMD; i++, pmd++) |
| 365 | set_pmd(pmd, __pmd(0)); |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 366 | } |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 367 | break; |
| 368 | } |
Keith Mannthey | 6ad9165 | 2006-09-26 10:52:36 +0200 | [diff] [blame] | 369 | |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 370 | if (pmd_val(*pmd)) { |
Jeremy Fitzhardinge | 22b4514 | 2008-06-26 12:02:49 -0700 | [diff] [blame] | 371 | if (!pmd_large(*pmd)) |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 372 | last_map_addr = phys_pte_update(pmd, address, |
| 373 | end); |
Keith Mannthey | 6ad9165 | 2006-09-26 10:52:36 +0200 | [diff] [blame] | 374 | continue; |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 375 | } |
Keith Mannthey | 6ad9165 | 2006-09-26 10:52:36 +0200 | [diff] [blame] | 376 | |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 377 | if (page_size_mask & (1<<PG_LEVEL_2M)) { |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 378 | pages++; |
| 379 | set_pte((pte_t *)pmd, |
| 380 | pfn_pte(address >> PAGE_SHIFT, PAGE_KERNEL_LARGE)); |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 381 | last_map_addr = (address & PMD_MASK) + PMD_SIZE; |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 382 | continue; |
| 383 | } |
| 384 | |
| 385 | pte = alloc_low_page(&pte_phys); |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 386 | last_map_addr = phys_pte_init(pte, address, end); |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 387 | unmap_low_page(pte); |
| 388 | |
| 389 | pmd_populate_kernel(&init_mm, pmd, __va(pte_phys)); |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 390 | } |
Andi Kleen | ce0c0e5 | 2008-05-02 11:46:49 +0200 | [diff] [blame] | 391 | update_page_count(PG_LEVEL_2M, pages); |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 392 | return last_map_addr; |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 393 | } |
| 394 | |
Andi Kleen | cc61503 | 2008-03-12 03:53:28 +0100 | [diff] [blame] | 395 | static unsigned long __meminit |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 396 | phys_pmd_update(pud_t *pud, unsigned long address, unsigned long end, |
| 397 | unsigned long page_size_mask) |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 398 | { |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 399 | pmd_t *pmd = pmd_offset(pud, 0); |
Andi Kleen | cc61503 | 2008-03-12 03:53:28 +0100 | [diff] [blame] | 400 | unsigned long last_map_addr; |
| 401 | |
Keith Mannthey | 6ad9165 | 2006-09-26 10:52:36 +0200 | [diff] [blame] | 402 | spin_lock(&init_mm.page_table_lock); |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 403 | last_map_addr = phys_pmd_init(pmd, address, end, page_size_mask); |
Keith Mannthey | 6ad9165 | 2006-09-26 10:52:36 +0200 | [diff] [blame] | 404 | spin_unlock(&init_mm.page_table_lock); |
| 405 | __flush_tlb_all(); |
Andi Kleen | cc61503 | 2008-03-12 03:53:28 +0100 | [diff] [blame] | 406 | return last_map_addr; |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 407 | } |
| 408 | |
Andi Kleen | cc61503 | 2008-03-12 03:53:28 +0100 | [diff] [blame] | 409 | static unsigned long __meminit |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 410 | phys_pud_init(pud_t *pud_page, unsigned long addr, unsigned long end, |
| 411 | unsigned long page_size_mask) |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 412 | { |
Andi Kleen | ce0c0e5 | 2008-05-02 11:46:49 +0200 | [diff] [blame] | 413 | unsigned long pages = 0; |
Andi Kleen | cc61503 | 2008-03-12 03:53:28 +0100 | [diff] [blame] | 414 | unsigned long last_map_addr = end; |
Keith Mannthey | 6ad9165 | 2006-09-26 10:52:36 +0200 | [diff] [blame] | 415 | int i = pud_index(addr); |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 416 | |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 417 | for (; i < PTRS_PER_PUD; i++, addr = (addr & PUD_MASK) + PUD_SIZE) { |
Keith Mannthey | 6ad9165 | 2006-09-26 10:52:36 +0200 | [diff] [blame] | 418 | unsigned long pmd_phys; |
| 419 | pud_t *pud = pud_page + pud_index(addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | pmd_t *pmd; |
| 421 | |
Keith Mannthey | 6ad9165 | 2006-09-26 10:52:36 +0200 | [diff] [blame] | 422 | if (addr >= end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 425 | if (!after_bootmem && |
| 426 | !e820_any_mapped(addr, addr+PUD_SIZE, 0)) { |
| 427 | set_pud(pud, __pud(0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | continue; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 429 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
Keith Mannthey | 6ad9165 | 2006-09-26 10:52:36 +0200 | [diff] [blame] | 431 | if (pud_val(*pud)) { |
Andi Kleen | ef92576 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 432 | if (!pud_large(*pud)) |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 433 | last_map_addr = phys_pmd_update(pud, addr, end, |
| 434 | page_size_mask); |
Andi Kleen | ef92576 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 435 | continue; |
| 436 | } |
| 437 | |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 438 | if (page_size_mask & (1<<PG_LEVEL_1G)) { |
Andi Kleen | ce0c0e5 | 2008-05-02 11:46:49 +0200 | [diff] [blame] | 439 | pages++; |
Andi Kleen | ef92576 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 440 | set_pte((pte_t *)pud, |
| 441 | pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL_LARGE)); |
Andi Kleen | cc61503 | 2008-03-12 03:53:28 +0100 | [diff] [blame] | 442 | last_map_addr = (addr & PUD_MASK) + PUD_SIZE; |
Keith Mannthey | 6ad9165 | 2006-09-26 10:52:36 +0200 | [diff] [blame] | 443 | continue; |
| 444 | } |
| 445 | |
Vivek Goyal | dafe41e | 2007-05-02 19:27:06 +0200 | [diff] [blame] | 446 | pmd = alloc_low_page(&pmd_phys); |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 447 | |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 448 | spin_lock(&init_mm.page_table_lock); |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 449 | last_map_addr = phys_pmd_init(pmd, addr, end, page_size_mask); |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 450 | unmap_low_page(pmd); |
| 451 | pud_populate(&init_mm, pud, __va(pmd_phys)); |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 452 | spin_unlock(&init_mm.page_table_lock); |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 453 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | } |
Andi Kleen | 1a2b441 | 2008-01-30 13:33:54 +0100 | [diff] [blame] | 455 | __flush_tlb_all(); |
Andi Kleen | ce0c0e5 | 2008-05-02 11:46:49 +0200 | [diff] [blame] | 456 | update_page_count(PG_LEVEL_1G, pages); |
Andi Kleen | cc61503 | 2008-03-12 03:53:28 +0100 | [diff] [blame] | 457 | |
Yinghai Lu | 1a0db38 | 2008-06-24 14:56:20 -0700 | [diff] [blame] | 458 | return last_map_addr; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 459 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 461 | static unsigned long __meminit |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 462 | phys_pud_update(pgd_t *pgd, unsigned long addr, unsigned long end, |
| 463 | unsigned long page_size_mask) |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 464 | { |
| 465 | pud_t *pud; |
| 466 | |
| 467 | pud = (pud_t *)pgd_page_vaddr(*pgd); |
| 468 | |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 469 | return phys_pud_init(pud, addr, end, page_size_mask); |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 470 | } |
| 471 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | static void __init find_early_table_space(unsigned long end) |
| 473 | { |
Yinghai Lu | c2e6d65 | 2008-07-08 01:43:27 -0700 | [diff] [blame] | 474 | unsigned long puds, pmds, ptes, tables, start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | |
| 476 | puds = (end + PUD_SIZE - 1) >> PUD_SHIFT; |
Andi Kleen | ef92576 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 477 | tables = round_up(puds * sizeof(pud_t), PAGE_SIZE); |
Yinghai Lu | c2e6d65 | 2008-07-08 01:43:27 -0700 | [diff] [blame] | 478 | if (direct_gbpages) { |
| 479 | unsigned long extra; |
| 480 | extra = end - ((end>>PUD_SHIFT) << PUD_SHIFT); |
| 481 | pmds = (extra + PMD_SIZE - 1) >> PMD_SHIFT; |
| 482 | } else |
| 483 | pmds = (end + PMD_SIZE - 1) >> PMD_SHIFT; |
| 484 | tables += round_up(pmds * sizeof(pmd_t), PAGE_SIZE); |
| 485 | |
| 486 | if (cpu_has_pse) { |
| 487 | unsigned long extra; |
| 488 | extra = end - ((end>>PMD_SHIFT) << PMD_SHIFT); |
| 489 | ptes = (extra + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 490 | } else |
| 491 | ptes = (end + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 492 | tables += round_up(ptes * sizeof(pte_t), PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 494 | /* |
| 495 | * RED-PEN putting page tables only on node 0 could |
| 496 | * cause a hotspot and fill up ZONE_DMA. The page tables |
| 497 | * need roughly 0.5KB per GB. |
| 498 | */ |
| 499 | start = 0x8000; |
Yinghai Lu | 24a5da7 | 2008-02-01 17:49:41 +0100 | [diff] [blame] | 500 | table_start = find_e820_area(start, end, tables, PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | if (table_start == -1UL) |
| 502 | panic("Cannot find space for the kernel page tables"); |
| 503 | |
| 504 | table_start >>= PAGE_SHIFT; |
| 505 | table_end = table_start; |
Yinghai Lu | d86623a | 2008-06-24 14:57:29 -0700 | [diff] [blame] | 506 | table_top = table_start + (tables >> PAGE_SHIFT); |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 507 | |
Yinghai Lu | d86623a | 2008-06-24 14:57:29 -0700 | [diff] [blame] | 508 | printk(KERN_DEBUG "kernel direct mapping tables up to %lx @ %lx-%lx\n", |
| 509 | end, table_start << PAGE_SHIFT, table_top << PAGE_SHIFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Andi Kleen | ef92576 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 512 | static void __init init_gbpages(void) |
| 513 | { |
| 514 | if (direct_gbpages && cpu_has_gbpages) |
| 515 | printk(KERN_INFO "Using GB pages for direct mapping\n"); |
| 516 | else |
| 517 | direct_gbpages = 0; |
| 518 | } |
| 519 | |
Yinghai Lu | 0327318 | 2008-04-18 17:49:15 -0700 | [diff] [blame] | 520 | #ifdef CONFIG_MEMTEST |
Yinghai Lu | c64df70 | 2008-03-21 18:56:19 -0700 | [diff] [blame] | 521 | |
| 522 | static void __init memtest(unsigned long start_phys, unsigned long size, |
| 523 | unsigned pattern) |
Yinghai Lu | 272b9cad | 2008-03-20 23:58:33 -0700 | [diff] [blame] | 524 | { |
| 525 | unsigned long i; |
| 526 | unsigned long *start; |
| 527 | unsigned long start_bad; |
| 528 | unsigned long last_bad; |
| 529 | unsigned long val; |
| 530 | unsigned long start_phys_aligned; |
| 531 | unsigned long count; |
| 532 | unsigned long incr; |
| 533 | |
| 534 | switch (pattern) { |
| 535 | case 0: |
| 536 | val = 0UL; |
| 537 | break; |
| 538 | case 1: |
| 539 | val = -1UL; |
| 540 | break; |
| 541 | case 2: |
| 542 | val = 0x5555555555555555UL; |
| 543 | break; |
| 544 | case 3: |
| 545 | val = 0xaaaaaaaaaaaaaaaaUL; |
| 546 | break; |
| 547 | default: |
| 548 | return; |
| 549 | } |
| 550 | |
| 551 | incr = sizeof(unsigned long); |
| 552 | start_phys_aligned = ALIGN(start_phys, incr); |
| 553 | count = (size - (start_phys_aligned - start_phys))/incr; |
| 554 | start = __va(start_phys_aligned); |
| 555 | start_bad = 0; |
| 556 | last_bad = 0; |
| 557 | |
| 558 | for (i = 0; i < count; i++) |
| 559 | start[i] = val; |
| 560 | for (i = 0; i < count; i++, start++, start_phys_aligned += incr) { |
| 561 | if (*start != val) { |
| 562 | if (start_phys_aligned == last_bad + incr) { |
| 563 | last_bad += incr; |
| 564 | } else { |
| 565 | if (start_bad) { |
Yinghai Lu | dcfe946 | 2008-04-15 23:17:42 -0700 | [diff] [blame] | 566 | printk(KERN_CONT "\n %016lx bad mem addr %016lx - %016lx reserved", |
Yinghai Lu | 272b9cad | 2008-03-20 23:58:33 -0700 | [diff] [blame] | 567 | val, start_bad, last_bad + incr); |
| 568 | reserve_early(start_bad, last_bad - start_bad, "BAD RAM"); |
| 569 | } |
| 570 | start_bad = last_bad = start_phys_aligned; |
| 571 | } |
| 572 | } |
| 573 | } |
| 574 | if (start_bad) { |
Yinghai Lu | dcfe946 | 2008-04-15 23:17:42 -0700 | [diff] [blame] | 575 | printk(KERN_CONT "\n %016lx bad mem addr %016lx - %016lx reserved", |
Yinghai Lu | 272b9cad | 2008-03-20 23:58:33 -0700 | [diff] [blame] | 576 | val, start_bad, last_bad + incr); |
| 577 | reserve_early(start_bad, last_bad - start_bad, "BAD RAM"); |
| 578 | } |
| 579 | |
| 580 | } |
| 581 | |
Yinghai Lu | 0327318 | 2008-04-18 17:49:15 -0700 | [diff] [blame] | 582 | /* default is disabled */ |
| 583 | static int memtest_pattern __initdata; |
Yinghai Lu | c64df70 | 2008-03-21 18:56:19 -0700 | [diff] [blame] | 584 | |
Yinghai Lu | 272b9cad | 2008-03-20 23:58:33 -0700 | [diff] [blame] | 585 | static int __init parse_memtest(char *arg) |
| 586 | { |
| 587 | if (arg) |
Yinghai Lu | c64df70 | 2008-03-21 18:56:19 -0700 | [diff] [blame] | 588 | memtest_pattern = simple_strtoul(arg, NULL, 0); |
Yinghai Lu | 272b9cad | 2008-03-20 23:58:33 -0700 | [diff] [blame] | 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | early_param("memtest", parse_memtest); |
| 593 | |
| 594 | static void __init early_memtest(unsigned long start, unsigned long end) |
| 595 | { |
Andrew Morton | 27df66a | 2008-07-03 10:14:10 +0200 | [diff] [blame] | 596 | u64 t_start, t_size; |
Yinghai Lu | 272b9cad | 2008-03-20 23:58:33 -0700 | [diff] [blame] | 597 | unsigned pattern; |
| 598 | |
Yinghai Lu | c64df70 | 2008-03-21 18:56:19 -0700 | [diff] [blame] | 599 | if (!memtest_pattern) |
| 600 | return; |
| 601 | |
| 602 | printk(KERN_INFO "early_memtest: pattern num %d", memtest_pattern); |
Yinghai Lu | 272b9cad | 2008-03-20 23:58:33 -0700 | [diff] [blame] | 603 | for (pattern = 0; pattern < memtest_pattern; pattern++) { |
| 604 | t_start = start; |
| 605 | t_size = 0; |
| 606 | while (t_start < end) { |
| 607 | t_start = find_e820_area_size(t_start, &t_size, 1); |
| 608 | |
| 609 | /* done ? */ |
| 610 | if (t_start >= end) |
| 611 | break; |
| 612 | if (t_start + t_size > end) |
| 613 | t_size = end - t_start; |
| 614 | |
Andrew Morton | 27df66a | 2008-07-03 10:14:10 +0200 | [diff] [blame] | 615 | printk(KERN_CONT "\n %016llx - %016llx pattern %d", |
| 616 | (unsigned long long)t_start, |
| 617 | (unsigned long long)t_start + t_size, pattern); |
Yinghai Lu | 272b9cad | 2008-03-20 23:58:33 -0700 | [diff] [blame] | 618 | |
| 619 | memtest(t_start, t_size, pattern); |
| 620 | |
| 621 | t_start += t_size; |
| 622 | } |
| 623 | } |
Yinghai Lu | c64df70 | 2008-03-21 18:56:19 -0700 | [diff] [blame] | 624 | printk(KERN_CONT "\n"); |
Yinghai Lu | 272b9cad | 2008-03-20 23:58:33 -0700 | [diff] [blame] | 625 | } |
Yinghai Lu | c64df70 | 2008-03-21 18:56:19 -0700 | [diff] [blame] | 626 | #else |
| 627 | static void __init early_memtest(unsigned long start, unsigned long end) |
| 628 | { |
| 629 | } |
| 630 | #endif |
Yinghai Lu | 272b9cad | 2008-03-20 23:58:33 -0700 | [diff] [blame] | 631 | |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 632 | static unsigned long __init kernel_physical_mapping_init(unsigned long start, |
| 633 | unsigned long end, |
| 634 | unsigned long page_size_mask) |
| 635 | { |
| 636 | |
| 637 | unsigned long next, last_map_addr = end; |
| 638 | |
| 639 | start = (unsigned long)__va(start); |
| 640 | end = (unsigned long)__va(end); |
| 641 | |
| 642 | for (; start < end; start = next) { |
| 643 | pgd_t *pgd = pgd_offset_k(start); |
| 644 | unsigned long pud_phys; |
| 645 | pud_t *pud; |
| 646 | |
| 647 | next = start + PGDIR_SIZE; |
| 648 | if (next > end) |
| 649 | next = end; |
| 650 | |
| 651 | if (pgd_val(*pgd)) { |
| 652 | last_map_addr = phys_pud_update(pgd, __pa(start), |
| 653 | __pa(end), page_size_mask); |
| 654 | continue; |
| 655 | } |
| 656 | |
| 657 | if (after_bootmem) |
| 658 | pud = pud_offset(pgd, start & PGDIR_MASK); |
| 659 | else |
| 660 | pud = alloc_low_page(&pud_phys); |
| 661 | |
| 662 | last_map_addr = phys_pud_init(pud, __pa(start), __pa(next), |
| 663 | page_size_mask); |
| 664 | unmap_low_page(pud); |
| 665 | pgd_populate(&init_mm, pgd_offset_k(start), |
| 666 | __va(pud_phys)); |
| 667 | } |
| 668 | |
| 669 | return last_map_addr; |
| 670 | } |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 671 | |
| 672 | struct map_range { |
| 673 | unsigned long start; |
| 674 | unsigned long end; |
| 675 | unsigned page_size_mask; |
| 676 | }; |
| 677 | |
| 678 | #define NR_RANGE_MR 5 |
| 679 | |
| 680 | static int save_mr(struct map_range *mr, int nr_range, |
| 681 | unsigned long start_pfn, unsigned long end_pfn, |
| 682 | unsigned long page_size_mask) |
| 683 | { |
| 684 | |
| 685 | if (start_pfn < end_pfn) { |
| 686 | if (nr_range >= NR_RANGE_MR) |
| 687 | panic("run out of range for init_memory_mapping\n"); |
| 688 | mr[nr_range].start = start_pfn<<PAGE_SHIFT; |
| 689 | mr[nr_range].end = end_pfn<<PAGE_SHIFT; |
| 690 | mr[nr_range].page_size_mask = page_size_mask; |
| 691 | nr_range++; |
| 692 | } |
| 693 | |
| 694 | return nr_range; |
| 695 | } |
| 696 | |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 697 | /* |
| 698 | * Setup the direct mapping of the physical memory at PAGE_OFFSET. |
| 699 | * This runs before bootmem is initialized and gets pages directly from |
| 700 | * the physical memory. To access them they are temporarily mapped. |
| 701 | */ |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 702 | unsigned long __init_refok init_memory_mapping(unsigned long start, |
| 703 | unsigned long end) |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 704 | { |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 705 | unsigned long last_map_addr = 0; |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 706 | unsigned long page_size_mask = 0; |
Yinghai Lu | c2e6d65 | 2008-07-08 01:43:27 -0700 | [diff] [blame] | 707 | unsigned long start_pfn, end_pfn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 709 | struct map_range mr[NR_RANGE_MR]; |
| 710 | int nr_range, i; |
| 711 | |
Yinghai Lu | 272b9cad | 2008-03-20 23:58:33 -0700 | [diff] [blame] | 712 | printk(KERN_INFO "init_memory_mapping\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 714 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | * Find space for the kernel direct mapping tables. |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 716 | * |
| 717 | * Later we should allocate these tables in the local node of the |
| 718 | * memory mapped. Unfortunately this is done currently before the |
| 719 | * nodes are discovered. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | */ |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 721 | if (!after_bootmem) |
Andi Kleen | ef92576 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 722 | init_gbpages(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 724 | if (direct_gbpages) |
| 725 | page_size_mask |= 1 << PG_LEVEL_1G; |
| 726 | if (cpu_has_pse) |
| 727 | page_size_mask |= 1 << PG_LEVEL_2M; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 729 | memset(mr, 0, sizeof(mr)); |
| 730 | nr_range = 0; |
| 731 | |
| 732 | /* head if not big page alignment ?*/ |
Yinghai Lu | c2e6d65 | 2008-07-08 01:43:27 -0700 | [diff] [blame] | 733 | start_pfn = start >> PAGE_SHIFT; |
| 734 | end_pfn = ((start + (PMD_SIZE - 1)) >> PMD_SHIFT) |
| 735 | << (PMD_SHIFT - PAGE_SHIFT); |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 736 | nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0); |
Yinghai Lu | c2e6d65 | 2008-07-08 01:43:27 -0700 | [diff] [blame] | 737 | |
| 738 | /* big page (2M) range*/ |
| 739 | start_pfn = ((start + (PMD_SIZE - 1))>>PMD_SHIFT) |
| 740 | << (PMD_SHIFT - PAGE_SHIFT); |
| 741 | end_pfn = ((start + (PUD_SIZE - 1))>>PUD_SHIFT) |
| 742 | << (PUD_SHIFT - PAGE_SHIFT); |
| 743 | if (end_pfn > ((end>>PUD_SHIFT)<<(PUD_SHIFT - PAGE_SHIFT))) |
| 744 | end_pfn = ((end>>PUD_SHIFT)<<(PUD_SHIFT - PAGE_SHIFT)); |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 745 | nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, |
| 746 | page_size_mask & (1<<PG_LEVEL_2M)); |
Yinghai Lu | c2e6d65 | 2008-07-08 01:43:27 -0700 | [diff] [blame] | 747 | |
| 748 | /* big page (1G) range */ |
| 749 | start_pfn = end_pfn; |
| 750 | end_pfn = (end>>PUD_SHIFT) << (PUD_SHIFT - PAGE_SHIFT); |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 751 | nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, |
| 752 | page_size_mask & |
| 753 | ((1<<PG_LEVEL_2M)|(1<<PG_LEVEL_1G))); |
Yinghai Lu | c2e6d65 | 2008-07-08 01:43:27 -0700 | [diff] [blame] | 754 | |
| 755 | /* tail is not big page (1G) alignment */ |
| 756 | start_pfn = end_pfn; |
| 757 | end_pfn = (end>>PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT); |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 758 | nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, |
| 759 | page_size_mask & (1<<PG_LEVEL_2M)); |
| 760 | |
Yinghai Lu | c2e6d65 | 2008-07-08 01:43:27 -0700 | [diff] [blame] | 761 | /* tail is not big page (2M) alignment */ |
| 762 | start_pfn = end_pfn; |
| 763 | end_pfn = end>>PAGE_SHIFT; |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 764 | nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0); |
| 765 | |
Yinghai Lu | 9958e81 | 2008-07-12 14:32:45 -0700 | [diff] [blame^] | 766 | /* try to merge same page size and continuous */ |
| 767 | for (i = 0; nr_range > 1 && i < nr_range - 1; i++) { |
| 768 | unsigned long old_start; |
| 769 | if (mr[i].end != mr[i+1].start || |
| 770 | mr[i].page_size_mask != mr[i+1].page_size_mask) |
| 771 | continue; |
| 772 | /* move it */ |
| 773 | old_start = mr[i].start; |
| 774 | memmove(&mr[i], &mr[i+1], |
| 775 | (nr_range - 1 - i) * sizeof (struct map_range)); |
| 776 | mr[i].start = old_start; |
| 777 | nr_range--; |
| 778 | } |
| 779 | |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 780 | for (i = 0; i < nr_range; i++) |
| 781 | printk(KERN_DEBUG " %010lx - %010lx page %s\n", |
| 782 | mr[i].start, mr[i].end, |
| 783 | (mr[i].page_size_mask & (1<<PG_LEVEL_1G))?"1G":( |
| 784 | (mr[i].page_size_mask & (1<<PG_LEVEL_2M))?"2M":"4k")); |
| 785 | |
| 786 | if (!after_bootmem) |
| 787 | find_early_table_space(end); |
| 788 | |
| 789 | for (i = 0; i < nr_range; i++) |
Yinghai Lu | c2e6d65 | 2008-07-08 01:43:27 -0700 | [diff] [blame] | 790 | last_map_addr = kernel_physical_mapping_init( |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 791 | mr[i].start, mr[i].end, |
| 792 | mr[i].page_size_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 794 | if (!after_bootmem) |
Glauber de Oliveira Costa | f51c945 | 2007-07-22 11:12:29 +0200 | [diff] [blame] | 795 | mmu_cr4_features = read_cr4(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | __flush_tlb_all(); |
Andi Kleen | 7517527 | 2008-01-30 13:33:17 +0100 | [diff] [blame] | 797 | |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 798 | if (!after_bootmem && table_end > table_start) |
Yinghai Lu | 24a5da7 | 2008-02-01 17:49:41 +0100 | [diff] [blame] | 799 | reserve_early(table_start << PAGE_SHIFT, |
| 800 | table_end << PAGE_SHIFT, "PGTABLE"); |
Yinghai Lu | 272b9cad | 2008-03-20 23:58:33 -0700 | [diff] [blame] | 801 | |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 802 | printk(KERN_INFO "last_map_addr: %lx end: %lx\n", |
| 803 | last_map_addr, end); |
| 804 | |
Yinghai Lu | 272b9cad | 2008-03-20 23:58:33 -0700 | [diff] [blame] | 805 | if (!after_bootmem) |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 806 | early_memtest(start, end); |
Andi Kleen | cc61503 | 2008-03-12 03:53:28 +0100 | [diff] [blame] | 807 | |
Yinghai Lu | 1a0db38 | 2008-06-24 14:56:20 -0700 | [diff] [blame] | 808 | return last_map_addr >> PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | } |
| 810 | |
Matt Tolentino | 2b97690 | 2005-06-23 00:08:06 -0700 | [diff] [blame] | 811 | #ifndef CONFIG_NUMA |
Yinghai Lu | 1f75d7e | 2008-06-22 02:44:49 -0700 | [diff] [blame] | 812 | void __init initmem_init(unsigned long start_pfn, unsigned long end_pfn) |
| 813 | { |
| 814 | unsigned long bootmap_size, bootmap; |
| 815 | |
| 816 | bootmap_size = bootmem_bootmap_pages(end_pfn)<<PAGE_SHIFT; |
| 817 | bootmap = find_e820_area(0, end_pfn<<PAGE_SHIFT, bootmap_size, |
| 818 | PAGE_SIZE); |
| 819 | if (bootmap == -1L) |
| 820 | panic("Cannot find bootmem map of size %ld\n", bootmap_size); |
Yinghai Lu | 346cafe | 2008-06-23 03:06:14 -0700 | [diff] [blame] | 821 | /* don't touch min_low_pfn */ |
| 822 | bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap >> PAGE_SHIFT, |
| 823 | 0, end_pfn); |
Yinghai Lu | 1f75d7e | 2008-06-22 02:44:49 -0700 | [diff] [blame] | 824 | e820_register_active_regions(0, start_pfn, end_pfn); |
| 825 | free_bootmem_with_active_regions(0, end_pfn); |
| 826 | early_res_to_bootmem(0, end_pfn<<PAGE_SHIFT); |
| 827 | reserve_bootmem(bootmap, bootmap_size, BOOTMEM_DEFAULT); |
| 828 | } |
| 829 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | void __init paging_init(void) |
| 831 | { |
Mel Gorman | 6391af1 | 2006-10-11 01:20:39 -0700 | [diff] [blame] | 832 | unsigned long max_zone_pfns[MAX_NR_ZONES]; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 833 | |
Mel Gorman | 6391af1 | 2006-10-11 01:20:39 -0700 | [diff] [blame] | 834 | memset(max_zone_pfns, 0, sizeof(max_zone_pfns)); |
| 835 | max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN; |
| 836 | max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN; |
Yinghai Lu | c987d12 | 2008-06-24 22:14:09 -0700 | [diff] [blame] | 837 | max_zone_pfns[ZONE_NORMAL] = max_pfn; |
Mel Gorman | 6391af1 | 2006-10-11 01:20:39 -0700 | [diff] [blame] | 838 | |
Yinghai Lu | c987d12 | 2008-06-24 22:14:09 -0700 | [diff] [blame] | 839 | memory_present(0, 0, max_pfn); |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 840 | sparse_init(); |
Mel Gorman | 5cb248a | 2006-09-27 01:49:52 -0700 | [diff] [blame] | 841 | free_area_init_nodes(max_zone_pfns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | } |
| 843 | #endif |
| 844 | |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 845 | /* |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 846 | * Memory hotplug specific functions |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 847 | */ |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 848 | #ifdef CONFIG_MEMORY_HOTPLUG |
| 849 | /* |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 850 | * Memory is added always to NORMAL zone. This means you will never get |
| 851 | * additional DMA/DMA32 memory. |
| 852 | */ |
| 853 | int arch_add_memory(int nid, u64 start, u64 size) |
| 854 | { |
| 855 | struct pglist_data *pgdat = NODE_DATA(nid); |
Christoph Lameter | 776ed98 | 2006-09-25 23:31:09 -0700 | [diff] [blame] | 856 | struct zone *zone = pgdat->node_zones + ZONE_NORMAL; |
Andi Kleen | cc61503 | 2008-03-12 03:53:28 +0100 | [diff] [blame] | 857 | unsigned long last_mapped_pfn, start_pfn = start >> PAGE_SHIFT; |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 858 | unsigned long nr_pages = size >> PAGE_SHIFT; |
| 859 | int ret; |
| 860 | |
Andi Kleen | cc61503 | 2008-03-12 03:53:28 +0100 | [diff] [blame] | 861 | last_mapped_pfn = init_memory_mapping(start, start + size-1); |
| 862 | if (last_mapped_pfn > max_pfn_mapped) |
| 863 | max_pfn_mapped = last_mapped_pfn; |
Keith Mannthey | 45e0b78 | 2006-09-30 23:27:09 -0700 | [diff] [blame] | 864 | |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 865 | ret = __add_pages(zone, start_pfn, nr_pages); |
Ingo Molnar | 10f22dd | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 866 | WARN_ON(1); |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 867 | |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 868 | return ret; |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 869 | } |
| 870 | EXPORT_SYMBOL_GPL(arch_add_memory); |
| 871 | |
Yasunori Goto | 8243229 | 2006-11-18 22:19:40 -0800 | [diff] [blame] | 872 | #if !defined(CONFIG_ACPI_NUMA) && defined(CONFIG_NUMA) |
Keith Mannthey | 4942e99 | 2006-09-30 23:27:06 -0700 | [diff] [blame] | 873 | int memory_add_physaddr_to_nid(u64 start) |
| 874 | { |
| 875 | return 0; |
| 876 | } |
Keith Mannthey | 8c2676a | 2006-09-30 23:27:07 -0700 | [diff] [blame] | 877 | EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid); |
Keith Mannthey | 4942e99 | 2006-09-30 23:27:06 -0700 | [diff] [blame] | 878 | #endif |
| 879 | |
Keith Mannthey | 45e0b78 | 2006-09-30 23:27:09 -0700 | [diff] [blame] | 880 | #endif /* CONFIG_MEMORY_HOTPLUG */ |
| 881 | |
Arjan van de Ven | ae531c2 | 2008-04-24 23:40:47 +0200 | [diff] [blame] | 882 | /* |
| 883 | * devmem_is_allowed() checks to see if /dev/mem access to a certain address |
| 884 | * is valid. The argument is a physical page number. |
| 885 | * |
| 886 | * |
| 887 | * On x86, access has to be given to the first megabyte of ram because that area |
| 888 | * contains bios code and data regions used by X and dosemu and similar apps. |
| 889 | * Access has to be given to non-kernel-ram areas as well, these contain the PCI |
| 890 | * mmio resources as well as potential bios/acpi data regions. |
| 891 | */ |
| 892 | int devmem_is_allowed(unsigned long pagenr) |
| 893 | { |
| 894 | if (pagenr <= 256) |
| 895 | return 1; |
| 896 | if (!page_is_ram(pagenr)) |
| 897 | return 1; |
| 898 | return 0; |
| 899 | } |
| 900 | |
| 901 | |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 902 | static struct kcore_list kcore_mem, kcore_vmalloc, kcore_kernel, |
| 903 | kcore_modules, kcore_vsyscall; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | |
| 905 | void __init mem_init(void) |
| 906 | { |
Andi Kleen | 0a43e4b | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 907 | long codesize, reservedpages, datasize, initsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | |
Jon Mason | 0dc243a | 2006-06-26 13:58:11 +0200 | [diff] [blame] | 909 | pci_iommu_alloc(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | |
Yinghai Lu | 48ddb15 | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 911 | /* clear_bss() already clear the empty_zero_page */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | |
| 913 | reservedpages = 0; |
| 914 | |
| 915 | /* this will put all low memory onto the freelists */ |
Matt Tolentino | 2b97690 | 2005-06-23 00:08:06 -0700 | [diff] [blame] | 916 | #ifdef CONFIG_NUMA |
Andi Kleen | 0a43e4b | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 917 | totalram_pages = numa_free_all_bootmem(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | #else |
Andi Kleen | 0a43e4b | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 919 | totalram_pages = free_all_bootmem(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | #endif |
Yinghai Lu | c987d12 | 2008-06-24 22:14:09 -0700 | [diff] [blame] | 921 | reservedpages = max_pfn - totalram_pages - |
| 922 | absent_pages_in_range(0, max_pfn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | after_bootmem = 1; |
| 924 | |
| 925 | codesize = (unsigned long) &_etext - (unsigned long) &_text; |
| 926 | datasize = (unsigned long) &_edata - (unsigned long) &_etext; |
| 927 | initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin; |
| 928 | |
| 929 | /* Register memory areas for /proc/kcore */ |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 930 | kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT); |
| 931 | kclist_add(&kcore_vmalloc, (void *)VMALLOC_START, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | VMALLOC_END-VMALLOC_START); |
| 933 | kclist_add(&kcore_kernel, &_stext, _end - _stext); |
| 934 | kclist_add(&kcore_modules, (void *)MODULES_VADDR, MODULES_LEN); |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 935 | kclist_add(&kcore_vsyscall, (void *)VSYSCALL_START, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | VSYSCALL_END - VSYSCALL_START); |
| 937 | |
Ingo Molnar | 10f22dd | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 938 | printk(KERN_INFO "Memory: %luk/%luk available (%ldk kernel code, " |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 939 | "%ldk reserved, %ldk data, %ldk init)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | (unsigned long) nr_free_pages() << (PAGE_SHIFT-10), |
Yinghai Lu | c987d12 | 2008-06-24 22:14:09 -0700 | [diff] [blame] | 941 | max_pfn << (PAGE_SHIFT-10), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | codesize >> 10, |
| 943 | reservedpages << (PAGE_SHIFT-10), |
| 944 | datasize >> 10, |
| 945 | initsize >> 10); |
Thomas Gleixner | 76ebd05 | 2008-02-09 23:24:09 +0100 | [diff] [blame] | 946 | |
| 947 | cpa_init(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | } |
| 949 | |
Gerd Hoffmann | d167a51 | 2006-06-26 13:56:16 +0200 | [diff] [blame] | 950 | void free_init_pages(char *what, unsigned long begin, unsigned long end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | { |
Thomas Gleixner | bfc734b | 2008-02-09 23:24:09 +0100 | [diff] [blame] | 952 | unsigned long addr = begin; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | |
Thomas Gleixner | bfc734b | 2008-02-09 23:24:09 +0100 | [diff] [blame] | 954 | if (addr >= end) |
Gerd Hoffmann | d167a51 | 2006-06-26 13:56:16 +0200 | [diff] [blame] | 955 | return; |
| 956 | |
Ingo Molnar | ee01f11 | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 957 | /* |
| 958 | * If debugging page accesses then do not free this memory but |
| 959 | * mark them not present - any buggy init-section access will |
| 960 | * create a kernel page fault: |
| 961 | */ |
| 962 | #ifdef CONFIG_DEBUG_PAGEALLOC |
| 963 | printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n", |
| 964 | begin, PAGE_ALIGN(end)); |
| 965 | set_memory_np(begin, (end - begin) >> PAGE_SHIFT); |
| 966 | #else |
Jan Beulich | 6fb1475 | 2007-05-02 19:27:10 +0200 | [diff] [blame] | 967 | printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10); |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 968 | |
Thomas Gleixner | bfc734b | 2008-02-09 23:24:09 +0100 | [diff] [blame] | 969 | for (; addr < end; addr += PAGE_SIZE) { |
Linus Torvalds | e3ebadd | 2007-05-07 08:44:24 -0700 | [diff] [blame] | 970 | ClearPageReserved(virt_to_page(addr)); |
| 971 | init_page_count(virt_to_page(addr)); |
| 972 | memset((void *)(addr & ~(PAGE_SIZE-1)), |
| 973 | POISON_FREE_INITMEM, PAGE_SIZE); |
Linus Torvalds | e3ebadd | 2007-05-07 08:44:24 -0700 | [diff] [blame] | 974 | free_page(addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | totalram_pages++; |
| 976 | } |
Ingo Molnar | ee01f11 | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 977 | #endif |
Gerd Hoffmann | d167a51 | 2006-06-26 13:56:16 +0200 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | void free_initmem(void) |
| 981 | { |
Gerd Hoffmann | d167a51 | 2006-06-26 13:56:16 +0200 | [diff] [blame] | 982 | free_init_pages("unused kernel memory", |
Linus Torvalds | e3ebadd | 2007-05-07 08:44:24 -0700 | [diff] [blame] | 983 | (unsigned long)(&__init_begin), |
| 984 | (unsigned long)(&__init_end)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | } |
| 986 | |
Arjan van de Ven | 67df197 | 2006-01-06 00:12:04 -0800 | [diff] [blame] | 987 | #ifdef CONFIG_DEBUG_RODATA |
Arjan van de Ven | edeed30 | 2008-01-30 13:34:08 +0100 | [diff] [blame] | 988 | const int rodata_test_data = 0xC3; |
| 989 | EXPORT_SYMBOL_GPL(rodata_test_data); |
Arjan van de Ven | 67df197 | 2006-01-06 00:12:04 -0800 | [diff] [blame] | 990 | |
Arjan van de Ven | 67df197 | 2006-01-06 00:12:04 -0800 | [diff] [blame] | 991 | void mark_rodata_ro(void) |
| 992 | { |
Mathieu Desnoyers | 4e4eee0 | 2008-02-02 15:42:20 -0500 | [diff] [blame] | 993 | unsigned long start = PFN_ALIGN(_stext), end = PFN_ALIGN(__end_rodata); |
Arjan van de Ven | 67df197 | 2006-01-06 00:12:04 -0800 | [diff] [blame] | 994 | |
Jan Beulich | 6fb1475 | 2007-05-02 19:27:10 +0200 | [diff] [blame] | 995 | printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n", |
Linus Torvalds | e3ebadd | 2007-05-07 08:44:24 -0700 | [diff] [blame] | 996 | (end - start) >> 10); |
Arjan van de Ven | 984bb80 | 2008-02-06 22:39:45 +0100 | [diff] [blame] | 997 | set_memory_ro(start, (end - start) >> PAGE_SHIFT); |
| 998 | |
| 999 | /* |
| 1000 | * The rodata section (but not the kernel text!) should also be |
| 1001 | * not-executable. |
| 1002 | */ |
| 1003 | start = ((unsigned long)__start_rodata + PAGE_SIZE - 1) & PAGE_MASK; |
| 1004 | set_memory_nx(start, (end - start) >> PAGE_SHIFT); |
Arjan van de Ven | 67df197 | 2006-01-06 00:12:04 -0800 | [diff] [blame] | 1005 | |
Arjan van de Ven | 1a48725 | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 1006 | rodata_test(); |
| 1007 | |
Andi Kleen | 0c42f39 | 2008-01-30 13:33:42 +0100 | [diff] [blame] | 1008 | #ifdef CONFIG_CPA_DEBUG |
Ingo Molnar | 10f22dd | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1009 | printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, end); |
Arjan van de Ven | 6d238cc | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 1010 | set_memory_rw(start, (end-start) >> PAGE_SHIFT); |
Andi Kleen | 0c42f39 | 2008-01-30 13:33:42 +0100 | [diff] [blame] | 1011 | |
Ingo Molnar | 10f22dd | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1012 | printk(KERN_INFO "Testing CPA: again\n"); |
Arjan van de Ven | 6d238cc | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 1013 | set_memory_ro(start, (end-start) >> PAGE_SHIFT); |
Andi Kleen | 0c42f39 | 2008-01-30 13:33:42 +0100 | [diff] [blame] | 1014 | #endif |
Arjan van de Ven | 67df197 | 2006-01-06 00:12:04 -0800 | [diff] [blame] | 1015 | } |
Mathieu Desnoyers | 4e4eee0 | 2008-02-02 15:42:20 -0500 | [diff] [blame] | 1016 | |
Arjan van de Ven | 67df197 | 2006-01-06 00:12:04 -0800 | [diff] [blame] | 1017 | #endif |
| 1018 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | #ifdef CONFIG_BLK_DEV_INITRD |
| 1020 | void free_initrd_mem(unsigned long start, unsigned long end) |
| 1021 | { |
Linus Torvalds | e3ebadd | 2007-05-07 08:44:24 -0700 | [diff] [blame] | 1022 | free_init_pages("initrd memory", start, end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | } |
| 1024 | #endif |
| 1025 | |
Yinghai Lu | d2dbf34 | 2008-06-13 02:00:56 -0700 | [diff] [blame] | 1026 | int __init reserve_bootmem_generic(unsigned long phys, unsigned long len, |
| 1027 | int flags) |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1028 | { |
Matt Tolentino | 2b97690 | 2005-06-23 00:08:06 -0700 | [diff] [blame] | 1029 | #ifdef CONFIG_NUMA |
Yinghai Lu | 8b3cd09e | 2008-03-18 12:50:21 -0700 | [diff] [blame] | 1030 | int nid, next_nid; |
Yinghai Lu | 6a07a0e | 2008-06-23 14:02:36 -0700 | [diff] [blame] | 1031 | int ret; |
Andi Kleen | 5e58a02 | 2006-11-14 16:57:46 +0100 | [diff] [blame] | 1032 | #endif |
| 1033 | unsigned long pfn = phys >> PAGE_SHIFT; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1034 | |
Yinghai Lu | c987d12 | 2008-06-24 22:14:09 -0700 | [diff] [blame] | 1035 | if (pfn >= max_pfn) { |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1036 | /* |
| 1037 | * This can happen with kdump kernels when accessing |
| 1038 | * firmware tables: |
| 1039 | */ |
Thomas Gleixner | 6779429 | 2008-03-21 21:27:10 +0100 | [diff] [blame] | 1040 | if (pfn < max_pfn_mapped) |
Bernhard Walle | 8b2ef1d | 2008-06-08 15:46:30 +0200 | [diff] [blame] | 1041 | return -EFAULT; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1042 | |
Yinghai Lu | 6a07a0e | 2008-06-23 14:02:36 -0700 | [diff] [blame] | 1043 | printk(KERN_ERR "reserve_bootmem: illegal reserve %lx %lu\n", |
Andi Kleen | 5e58a02 | 2006-11-14 16:57:46 +0100 | [diff] [blame] | 1044 | phys, len); |
Bernhard Walle | 8b2ef1d | 2008-06-08 15:46:30 +0200 | [diff] [blame] | 1045 | return -EFAULT; |
Andi Kleen | 5e58a02 | 2006-11-14 16:57:46 +0100 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | /* Should check here against the e820 map to avoid double free */ |
| 1049 | #ifdef CONFIG_NUMA |
Yinghai Lu | 8b3cd09e | 2008-03-18 12:50:21 -0700 | [diff] [blame] | 1050 | nid = phys_to_nid(phys); |
| 1051 | next_nid = phys_to_nid(phys + len - 1); |
| 1052 | if (nid == next_nid) |
Bernhard Walle | 8b2ef1d | 2008-06-08 15:46:30 +0200 | [diff] [blame] | 1053 | ret = reserve_bootmem_node(NODE_DATA(nid), phys, len, flags); |
Yinghai Lu | 8b3cd09e | 2008-03-18 12:50:21 -0700 | [diff] [blame] | 1054 | else |
Bernhard Walle | 8b2ef1d | 2008-06-08 15:46:30 +0200 | [diff] [blame] | 1055 | ret = reserve_bootmem(phys, len, flags); |
| 1056 | |
| 1057 | if (ret != 0) |
| 1058 | return ret; |
| 1059 | |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1060 | #else |
Bernhard Walle | 72a7fe3 | 2008-02-07 00:15:17 -0800 | [diff] [blame] | 1061 | reserve_bootmem(phys, len, BOOTMEM_DEFAULT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | #endif |
Yinghai Lu | 8b3cd09e | 2008-03-18 12:50:21 -0700 | [diff] [blame] | 1063 | |
Mel Gorman | 0e0b864 | 2006-09-27 01:49:56 -0700 | [diff] [blame] | 1064 | if (phys+len <= MAX_DMA_PFN*PAGE_SIZE) { |
Andi Kleen | e18c687 | 2005-11-05 17:25:53 +0100 | [diff] [blame] | 1065 | dma_reserve += len / PAGE_SIZE; |
Mel Gorman | 0e0b864 | 2006-09-27 01:49:56 -0700 | [diff] [blame] | 1066 | set_dma_reserve(dma_reserve); |
| 1067 | } |
Bernhard Walle | 8b2ef1d | 2008-06-08 15:46:30 +0200 | [diff] [blame] | 1068 | |
| 1069 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | } |
| 1071 | |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1072 | int kern_addr_valid(unsigned long addr) |
| 1073 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1075 | pgd_t *pgd; |
| 1076 | pud_t *pud; |
| 1077 | pmd_t *pmd; |
| 1078 | pte_t *pte; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | |
| 1080 | if (above != 0 && above != -1UL) |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1081 | return 0; |
| 1082 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | pgd = pgd_offset_k(addr); |
| 1084 | if (pgd_none(*pgd)) |
| 1085 | return 0; |
| 1086 | |
| 1087 | pud = pud_offset(pgd, addr); |
| 1088 | if (pud_none(*pud)) |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1089 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | |
| 1091 | pmd = pmd_offset(pud, addr); |
| 1092 | if (pmd_none(*pmd)) |
| 1093 | return 0; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1094 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | if (pmd_large(*pmd)) |
| 1096 | return pfn_valid(pmd_pfn(*pmd)); |
| 1097 | |
| 1098 | pte = pte_offset_kernel(pmd, addr); |
| 1099 | if (pte_none(*pte)) |
| 1100 | return 0; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | return pfn_valid(pte_pfn(*pte)); |
| 1103 | } |
| 1104 | |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1105 | /* |
| 1106 | * A pseudo VMA to allow ptrace access for the vsyscall page. This only |
| 1107 | * covers the 64bit vsyscall page now. 32bit has a real VMA now and does |
| 1108 | * not need special handling anymore: |
| 1109 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | static struct vm_area_struct gate_vma = { |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1111 | .vm_start = VSYSCALL_START, |
| 1112 | .vm_end = VSYSCALL_START + (VSYSCALL_MAPPED_PAGES * PAGE_SIZE), |
| 1113 | .vm_page_prot = PAGE_READONLY_EXEC, |
| 1114 | .vm_flags = VM_READ | VM_EXEC |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | }; |
| 1116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | struct vm_area_struct *get_gate_vma(struct task_struct *tsk) |
| 1118 | { |
| 1119 | #ifdef CONFIG_IA32_EMULATION |
Andi Kleen | 1e01441 | 2005-04-16 15:24:55 -0700 | [diff] [blame] | 1120 | if (test_tsk_thread_flag(tsk, TIF_IA32)) |
| 1121 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | #endif |
| 1123 | return &gate_vma; |
| 1124 | } |
| 1125 | |
| 1126 | int in_gate_area(struct task_struct *task, unsigned long addr) |
| 1127 | { |
| 1128 | struct vm_area_struct *vma = get_gate_vma(task); |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1129 | |
Andi Kleen | 1e01441 | 2005-04-16 15:24:55 -0700 | [diff] [blame] | 1130 | if (!vma) |
| 1131 | return 0; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | return (addr >= vma->vm_start) && (addr < vma->vm_end); |
| 1134 | } |
| 1135 | |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1136 | /* |
| 1137 | * Use this when you have no reliable task/vma, typically from interrupt |
| 1138 | * context. It is less reliable than using the task's vma and may give |
| 1139 | * false positives: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | */ |
| 1141 | int in_gate_area_no_task(unsigned long addr) |
| 1142 | { |
Andi Kleen | 1e01441 | 2005-04-16 15:24:55 -0700 | [diff] [blame] | 1143 | return (addr >= VSYSCALL_START) && (addr < VSYSCALL_END); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | } |
Zou Nan hai | 2e1c49d | 2007-06-01 00:46:28 -0700 | [diff] [blame] | 1145 | |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 1146 | const char *arch_vma_name(struct vm_area_struct *vma) |
| 1147 | { |
| 1148 | if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso) |
| 1149 | return "[vdso]"; |
| 1150 | if (vma == &gate_vma) |
| 1151 | return "[vsyscall]"; |
| 1152 | return NULL; |
| 1153 | } |
Christoph Lameter | 0889eba | 2007-10-16 01:24:15 -0700 | [diff] [blame] | 1154 | |
| 1155 | #ifdef CONFIG_SPARSEMEM_VMEMMAP |
| 1156 | /* |
| 1157 | * Initialise the sparsemem vmemmap using huge-pages at the PMD level. |
| 1158 | */ |
Yinghai Lu | c2b91e2 | 2008-04-12 01:19:24 -0700 | [diff] [blame] | 1159 | static long __meminitdata addr_start, addr_end; |
| 1160 | static void __meminitdata *p_start, *p_end; |
| 1161 | static int __meminitdata node_start; |
| 1162 | |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1163 | int __meminit |
| 1164 | vmemmap_populate(struct page *start_page, unsigned long size, int node) |
Christoph Lameter | 0889eba | 2007-10-16 01:24:15 -0700 | [diff] [blame] | 1165 | { |
| 1166 | unsigned long addr = (unsigned long)start_page; |
| 1167 | unsigned long end = (unsigned long)(start_page + size); |
| 1168 | unsigned long next; |
| 1169 | pgd_t *pgd; |
| 1170 | pud_t *pud; |
| 1171 | pmd_t *pmd; |
| 1172 | |
| 1173 | for (; addr < end; addr = next) { |
Jeremy Fitzhardinge | 7c934d3 | 2008-06-25 00:19:20 -0400 | [diff] [blame] | 1174 | void *p = NULL; |
Christoph Lameter | 0889eba | 2007-10-16 01:24:15 -0700 | [diff] [blame] | 1175 | |
| 1176 | pgd = vmemmap_pgd_populate(addr, node); |
| 1177 | if (!pgd) |
| 1178 | return -ENOMEM; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1179 | |
Christoph Lameter | 0889eba | 2007-10-16 01:24:15 -0700 | [diff] [blame] | 1180 | pud = vmemmap_pud_populate(pgd, addr, node); |
| 1181 | if (!pud) |
| 1182 | return -ENOMEM; |
| 1183 | |
Jeremy Fitzhardinge | 7c934d3 | 2008-06-25 00:19:20 -0400 | [diff] [blame] | 1184 | if (!cpu_has_pse) { |
| 1185 | next = (addr + PAGE_SIZE) & PAGE_MASK; |
| 1186 | pmd = vmemmap_pmd_populate(pud, addr, node); |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1187 | |
Jeremy Fitzhardinge | 7c934d3 | 2008-06-25 00:19:20 -0400 | [diff] [blame] | 1188 | if (!pmd) |
| 1189 | return -ENOMEM; |
| 1190 | |
| 1191 | p = vmemmap_pte_populate(pmd, addr, node); |
| 1192 | |
Christoph Lameter | 0889eba | 2007-10-16 01:24:15 -0700 | [diff] [blame] | 1193 | if (!p) |
| 1194 | return -ENOMEM; |
| 1195 | |
Jeremy Fitzhardinge | 7c934d3 | 2008-06-25 00:19:20 -0400 | [diff] [blame] | 1196 | addr_end = addr + PAGE_SIZE; |
| 1197 | p_end = p + PAGE_SIZE; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1198 | } else { |
Jeremy Fitzhardinge | 7c934d3 | 2008-06-25 00:19:20 -0400 | [diff] [blame] | 1199 | next = pmd_addr_end(addr, end); |
| 1200 | |
| 1201 | pmd = pmd_offset(pud, addr); |
| 1202 | if (pmd_none(*pmd)) { |
| 1203 | pte_t entry; |
| 1204 | |
| 1205 | p = vmemmap_alloc_block(PMD_SIZE, node); |
| 1206 | if (!p) |
| 1207 | return -ENOMEM; |
| 1208 | |
| 1209 | entry = pfn_pte(__pa(p) >> PAGE_SHIFT, |
| 1210 | PAGE_KERNEL_LARGE); |
| 1211 | set_pmd(pmd, __pmd(pte_val(entry))); |
| 1212 | |
Jeremy Fitzhardinge | 7c934d3 | 2008-06-25 00:19:20 -0400 | [diff] [blame] | 1213 | /* check to see if we have contiguous blocks */ |
| 1214 | if (p_end != p || node_start != node) { |
| 1215 | if (p_start) |
| 1216 | printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n", |
| 1217 | addr_start, addr_end-1, p_start, p_end-1, node_start); |
| 1218 | addr_start = addr; |
| 1219 | node_start = node; |
| 1220 | p_start = p; |
| 1221 | } |
Yinghai Lu | 49c980d | 2008-07-03 12:29:34 -0700 | [diff] [blame] | 1222 | |
| 1223 | addr_end = addr + PMD_SIZE; |
| 1224 | p_end = p + PMD_SIZE; |
Jeremy Fitzhardinge | 7c934d3 | 2008-06-25 00:19:20 -0400 | [diff] [blame] | 1225 | } else |
| 1226 | vmemmap_verify((pte_t *)pmd, node, addr, next); |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1227 | } |
Jeremy Fitzhardinge | 7c934d3 | 2008-06-25 00:19:20 -0400 | [diff] [blame] | 1228 | |
Christoph Lameter | 0889eba | 2007-10-16 01:24:15 -0700 | [diff] [blame] | 1229 | } |
Christoph Lameter | 0889eba | 2007-10-16 01:24:15 -0700 | [diff] [blame] | 1230 | return 0; |
| 1231 | } |
Yinghai Lu | c2b91e2 | 2008-04-12 01:19:24 -0700 | [diff] [blame] | 1232 | |
| 1233 | void __meminit vmemmap_populate_print_last(void) |
| 1234 | { |
| 1235 | if (p_start) { |
| 1236 | printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n", |
| 1237 | addr_start, addr_end-1, p_start, p_end-1, node_start); |
| 1238 | p_start = NULL; |
| 1239 | p_end = NULL; |
| 1240 | node_start = 0; |
| 1241 | } |
| 1242 | } |
Christoph Lameter | 0889eba | 2007-10-16 01:24:15 -0700 | [diff] [blame] | 1243 | #endif |