1ad757b6aSThomas Gleixner /* 2ad757b6aSThomas Gleixner * 3ad757b6aSThomas Gleixner * Copyright (C) 1995 Linus Torvalds 4ad757b6aSThomas Gleixner * 5ad757b6aSThomas Gleixner * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999 6ad757b6aSThomas Gleixner */ 7ad757b6aSThomas Gleixner 8ad757b6aSThomas Gleixner #include <linux/module.h> 9ad757b6aSThomas Gleixner #include <linux/signal.h> 10ad757b6aSThomas Gleixner #include <linux/sched.h> 11ad757b6aSThomas Gleixner #include <linux/kernel.h> 12ad757b6aSThomas Gleixner #include <linux/errno.h> 13ad757b6aSThomas Gleixner #include <linux/string.h> 14ad757b6aSThomas Gleixner #include <linux/types.h> 15ad757b6aSThomas Gleixner #include <linux/ptrace.h> 16ad757b6aSThomas Gleixner #include <linux/mman.h> 17ad757b6aSThomas Gleixner #include <linux/mm.h> 18ad757b6aSThomas Gleixner #include <linux/hugetlb.h> 19ad757b6aSThomas Gleixner #include <linux/swap.h> 20ad757b6aSThomas Gleixner #include <linux/smp.h> 21ad757b6aSThomas Gleixner #include <linux/init.h> 22ad757b6aSThomas Gleixner #include <linux/highmem.h> 23ad757b6aSThomas Gleixner #include <linux/pagemap.h> 24ad757b6aSThomas Gleixner #include <linux/pfn.h> 25ad757b6aSThomas Gleixner #include <linux/poison.h> 26ad757b6aSThomas Gleixner #include <linux/bootmem.h> 27ad757b6aSThomas Gleixner #include <linux/slab.h> 28ad757b6aSThomas Gleixner #include <linux/proc_fs.h> 29ad757b6aSThomas Gleixner #include <linux/memory_hotplug.h> 30ad757b6aSThomas Gleixner #include <linux/initrd.h> 31ad757b6aSThomas Gleixner #include <linux/cpumask.h> 32ad757b6aSThomas Gleixner 33f832ff18SH. Peter Anvin #include <asm/asm.h> 34ad757b6aSThomas Gleixner #include <asm/processor.h> 35ad757b6aSThomas Gleixner #include <asm/system.h> 36ad757b6aSThomas Gleixner #include <asm/uaccess.h> 37ad757b6aSThomas Gleixner #include <asm/pgtable.h> 38ad757b6aSThomas Gleixner #include <asm/dma.h> 39ad757b6aSThomas Gleixner #include <asm/fixmap.h> 40ad757b6aSThomas Gleixner #include <asm/e820.h> 41ad757b6aSThomas Gleixner #include <asm/apic.h> 428550eb99SIngo Molnar #include <asm/bugs.h> 43ad757b6aSThomas Gleixner #include <asm/tlb.h> 44ad757b6aSThomas Gleixner #include <asm/tlbflush.h> 45a5a19c63SJeremy Fitzhardinge #include <asm/pgalloc.h> 46ad757b6aSThomas Gleixner #include <asm/sections.h> 47ad757b6aSThomas Gleixner #include <asm/paravirt.h> 48551889a6SIan Campbell #include <asm/setup.h> 497bfeab9aSHarvey Harrison #include <asm/cacheflush.h> 50ad757b6aSThomas Gleixner 51ad757b6aSThomas Gleixner unsigned int __VMALLOC_RESERVE = 128 << 20; 52ad757b6aSThomas Gleixner 5367794292SThomas Gleixner unsigned long max_pfn_mapped; 547d1116a9SAndi Kleen 55ad757b6aSThomas Gleixner DEFINE_PER_CPU(struct mmu_gather, mmu_gathers); 56ad757b6aSThomas Gleixner unsigned long highstart_pfn, highend_pfn; 57ad757b6aSThomas Gleixner 588550eb99SIngo Molnar static noinline int do_test_wp_bit(void); 59ad757b6aSThomas Gleixner 60ad757b6aSThomas Gleixner /* 61ad757b6aSThomas Gleixner * Creates a middle page table and puts a pointer to it in the 62ad757b6aSThomas Gleixner * given global directory entry. This only returns the gd entry 63ad757b6aSThomas Gleixner * in non-PAE compilation mode, since the middle layer is folded. 64ad757b6aSThomas Gleixner */ 65ad757b6aSThomas Gleixner static pmd_t * __init one_md_table_init(pgd_t *pgd) 66ad757b6aSThomas Gleixner { 67ad757b6aSThomas Gleixner pud_t *pud; 68ad757b6aSThomas Gleixner pmd_t *pmd_table; 69ad757b6aSThomas Gleixner 70ad757b6aSThomas Gleixner #ifdef CONFIG_X86_PAE 71ad757b6aSThomas Gleixner if (!(pgd_val(*pgd) & _PAGE_PRESENT)) { 72ad757b6aSThomas Gleixner pmd_table = (pmd_t *) alloc_bootmem_low_pages(PAGE_SIZE); 73ad757b6aSThomas Gleixner 746944a9c8SJeremy Fitzhardinge paravirt_alloc_pmd(&init_mm, __pa(pmd_table) >> PAGE_SHIFT); 75ad757b6aSThomas Gleixner set_pgd(pgd, __pgd(__pa(pmd_table) | _PAGE_PRESENT)); 76ad757b6aSThomas Gleixner pud = pud_offset(pgd, 0); 778550eb99SIngo Molnar BUG_ON(pmd_table != pmd_offset(pud, 0)); 78ad757b6aSThomas Gleixner } 79ad757b6aSThomas Gleixner #endif 80ad757b6aSThomas Gleixner pud = pud_offset(pgd, 0); 81ad757b6aSThomas Gleixner pmd_table = pmd_offset(pud, 0); 828550eb99SIngo Molnar 83ad757b6aSThomas Gleixner return pmd_table; 84ad757b6aSThomas Gleixner } 85ad757b6aSThomas Gleixner 86ad757b6aSThomas Gleixner /* 87ad757b6aSThomas Gleixner * Create a page table and place a pointer to it in a middle page 888550eb99SIngo Molnar * directory entry: 89ad757b6aSThomas Gleixner */ 90ad757b6aSThomas Gleixner static pte_t * __init one_page_table_init(pmd_t *pmd) 91ad757b6aSThomas Gleixner { 92ad757b6aSThomas Gleixner if (!(pmd_val(*pmd) & _PAGE_PRESENT)) { 93509a80c4SIngo Molnar pte_t *page_table = NULL; 94509a80c4SIngo Molnar 95509a80c4SIngo Molnar #ifdef CONFIG_DEBUG_PAGEALLOC 96509a80c4SIngo Molnar page_table = (pte_t *) alloc_bootmem_pages(PAGE_SIZE); 97509a80c4SIngo Molnar #endif 988550eb99SIngo Molnar if (!page_table) { 99509a80c4SIngo Molnar page_table = 100509a80c4SIngo Molnar (pte_t *)alloc_bootmem_low_pages(PAGE_SIZE); 1018550eb99SIngo Molnar } 102ad757b6aSThomas Gleixner 1036944a9c8SJeremy Fitzhardinge paravirt_alloc_pte(&init_mm, __pa(page_table) >> PAGE_SHIFT); 104ad757b6aSThomas Gleixner set_pmd(pmd, __pmd(__pa(page_table) | _PAGE_TABLE)); 105ad757b6aSThomas Gleixner BUG_ON(page_table != pte_offset_kernel(pmd, 0)); 106ad757b6aSThomas Gleixner } 107ad757b6aSThomas Gleixner 108ad757b6aSThomas Gleixner return pte_offset_kernel(pmd, 0); 109ad757b6aSThomas Gleixner } 110ad757b6aSThomas Gleixner 111ad757b6aSThomas Gleixner /* 112ad757b6aSThomas Gleixner * This function initializes a certain range of kernel virtual memory 113ad757b6aSThomas Gleixner * with new bootmem page tables, everywhere page tables are missing in 114ad757b6aSThomas Gleixner * the given range. 1158550eb99SIngo Molnar * 116ad757b6aSThomas Gleixner * NOTE: The pagetables are allocated contiguous on the physical space 117ad757b6aSThomas Gleixner * so we can cache the place of the first one and move around without 118ad757b6aSThomas Gleixner * checking the pgd every time. 119ad757b6aSThomas Gleixner */ 1208550eb99SIngo Molnar static void __init 1218550eb99SIngo Molnar page_table_range_init(unsigned long start, unsigned long end, pgd_t *pgd_base) 122ad757b6aSThomas Gleixner { 123ad757b6aSThomas Gleixner int pgd_idx, pmd_idx; 124ad757b6aSThomas Gleixner unsigned long vaddr; 1258550eb99SIngo Molnar pgd_t *pgd; 1268550eb99SIngo Molnar pmd_t *pmd; 127ad757b6aSThomas Gleixner 128ad757b6aSThomas Gleixner vaddr = start; 129ad757b6aSThomas Gleixner pgd_idx = pgd_index(vaddr); 130ad757b6aSThomas Gleixner pmd_idx = pmd_index(vaddr); 131ad757b6aSThomas Gleixner pgd = pgd_base + pgd_idx; 132ad757b6aSThomas Gleixner 133ad757b6aSThomas Gleixner for ( ; (pgd_idx < PTRS_PER_PGD) && (vaddr != end); pgd++, pgd_idx++) { 134ad757b6aSThomas Gleixner pmd = one_md_table_init(pgd); 135ad757b6aSThomas Gleixner pmd = pmd + pmd_index(vaddr); 1368550eb99SIngo Molnar for (; (pmd_idx < PTRS_PER_PMD) && (vaddr != end); 1378550eb99SIngo Molnar pmd++, pmd_idx++) { 138ad757b6aSThomas Gleixner one_page_table_init(pmd); 139ad757b6aSThomas Gleixner 140ad757b6aSThomas Gleixner vaddr += PMD_SIZE; 141ad757b6aSThomas Gleixner } 142ad757b6aSThomas Gleixner pmd_idx = 0; 143ad757b6aSThomas Gleixner } 144ad757b6aSThomas Gleixner } 145ad757b6aSThomas Gleixner 146ad757b6aSThomas Gleixner static inline int is_kernel_text(unsigned long addr) 147ad757b6aSThomas Gleixner { 148ad757b6aSThomas Gleixner if (addr >= PAGE_OFFSET && addr <= (unsigned long)__init_end) 149ad757b6aSThomas Gleixner return 1; 150ad757b6aSThomas Gleixner return 0; 151ad757b6aSThomas Gleixner } 152ad757b6aSThomas Gleixner 153ad757b6aSThomas Gleixner /* 154ad757b6aSThomas Gleixner * This maps the physical memory to kernel virtual address space, a total 155ad757b6aSThomas Gleixner * of max_low_pfn pages, by creating page tables starting from address 1568550eb99SIngo Molnar * PAGE_OFFSET: 157ad757b6aSThomas Gleixner */ 158ad757b6aSThomas Gleixner static void __init kernel_physical_mapping_init(pgd_t *pgd_base) 159ad757b6aSThomas Gleixner { 1608550eb99SIngo Molnar int pgd_idx, pmd_idx, pte_ofs; 161ad757b6aSThomas Gleixner unsigned long pfn; 162ad757b6aSThomas Gleixner pgd_t *pgd; 163ad757b6aSThomas Gleixner pmd_t *pmd; 164ad757b6aSThomas Gleixner pte_t *pte; 165*ce0c0e50SAndi Kleen unsigned pages_2m = 0, pages_4k = 0; 166ad757b6aSThomas Gleixner 167ad757b6aSThomas Gleixner pgd_idx = pgd_index(PAGE_OFFSET); 168ad757b6aSThomas Gleixner pgd = pgd_base + pgd_idx; 169ad757b6aSThomas Gleixner pfn = 0; 170ad757b6aSThomas Gleixner 171ad757b6aSThomas Gleixner for (; pgd_idx < PTRS_PER_PGD; pgd++, pgd_idx++) { 172ad757b6aSThomas Gleixner pmd = one_md_table_init(pgd); 173ad757b6aSThomas Gleixner if (pfn >= max_low_pfn) 174ad757b6aSThomas Gleixner continue; 1758550eb99SIngo Molnar 176f3f20de8SJeremy Fitzhardinge for (pmd_idx = 0; 177f3f20de8SJeremy Fitzhardinge pmd_idx < PTRS_PER_PMD && pfn < max_low_pfn; 178f3f20de8SJeremy Fitzhardinge pmd++, pmd_idx++) { 1798550eb99SIngo Molnar unsigned int addr = pfn * PAGE_SIZE + PAGE_OFFSET; 180ad757b6aSThomas Gleixner 1818550eb99SIngo Molnar /* 1828550eb99SIngo Molnar * Map with big pages if possible, otherwise 1838550eb99SIngo Molnar * create normal page tables: 184f5c24a7fSAndi Kleen * 185f5c24a7fSAndi Kleen * Don't use a large page for the first 2/4MB of memory 186f5c24a7fSAndi Kleen * because there are often fixed size MTRRs in there 187f5c24a7fSAndi Kleen * and overlapping MTRRs into large pages can cause 188f5c24a7fSAndi Kleen * slowdowns. 1898550eb99SIngo Molnar */ 190f5c24a7fSAndi Kleen if (cpu_has_pse && !(pgd_idx == 0 && pmd_idx == 0)) { 1918550eb99SIngo Molnar unsigned int addr2; 192f3f20de8SJeremy Fitzhardinge pgprot_t prot = PAGE_KERNEL_LARGE; 193f3f20de8SJeremy Fitzhardinge 1948550eb99SIngo Molnar addr2 = (pfn + PTRS_PER_PTE-1) * PAGE_SIZE + 195f3f20de8SJeremy Fitzhardinge PAGE_OFFSET + PAGE_SIZE-1; 196f3f20de8SJeremy Fitzhardinge 1978550eb99SIngo Molnar if (is_kernel_text(addr) || 1988550eb99SIngo Molnar is_kernel_text(addr2)) 199f3f20de8SJeremy Fitzhardinge prot = PAGE_KERNEL_LARGE_EXEC; 200f3f20de8SJeremy Fitzhardinge 201*ce0c0e50SAndi Kleen pages_2m++; 202f3f20de8SJeremy Fitzhardinge set_pmd(pmd, pfn_pmd(pfn, prot)); 203ad757b6aSThomas Gleixner 204ad757b6aSThomas Gleixner pfn += PTRS_PER_PTE; 20567794292SThomas Gleixner max_pfn_mapped = pfn; 2068550eb99SIngo Molnar continue; 2078550eb99SIngo Molnar } 208ad757b6aSThomas Gleixner pte = one_page_table_init(pmd); 209ad757b6aSThomas Gleixner 210ad757b6aSThomas Gleixner for (pte_ofs = 0; 211ad757b6aSThomas Gleixner pte_ofs < PTRS_PER_PTE && pfn < max_low_pfn; 2128550eb99SIngo Molnar pte++, pfn++, pte_ofs++, addr += PAGE_SIZE) { 213f3f20de8SJeremy Fitzhardinge pgprot_t prot = PAGE_KERNEL; 214f3f20de8SJeremy Fitzhardinge 2158550eb99SIngo Molnar if (is_kernel_text(addr)) 216f3f20de8SJeremy Fitzhardinge prot = PAGE_KERNEL_EXEC; 217f3f20de8SJeremy Fitzhardinge 218*ce0c0e50SAndi Kleen pages_4k++; 219f3f20de8SJeremy Fitzhardinge set_pte(pte, pfn_pte(pfn, prot)); 220ad757b6aSThomas Gleixner } 22167794292SThomas Gleixner max_pfn_mapped = pfn; 222ad757b6aSThomas Gleixner } 223ad757b6aSThomas Gleixner } 224*ce0c0e50SAndi Kleen update_page_count(PG_LEVEL_2M, pages_2m); 225*ce0c0e50SAndi Kleen update_page_count(PG_LEVEL_4K, pages_4k); 226ad757b6aSThomas Gleixner } 227ad757b6aSThomas Gleixner 228ad757b6aSThomas Gleixner static inline int page_kills_ppro(unsigned long pagenr) 229ad757b6aSThomas Gleixner { 230ad757b6aSThomas Gleixner if (pagenr >= 0x70000 && pagenr <= 0x7003F) 231ad757b6aSThomas Gleixner return 1; 232ad757b6aSThomas Gleixner return 0; 233ad757b6aSThomas Gleixner } 234ad757b6aSThomas Gleixner 235ae531c26SArjan van de Ven /* 236ae531c26SArjan van de Ven * devmem_is_allowed() checks to see if /dev/mem access to a certain address 237ae531c26SArjan van de Ven * is valid. The argument is a physical page number. 238ae531c26SArjan van de Ven * 239ae531c26SArjan van de Ven * 240ae531c26SArjan van de Ven * On x86, access has to be given to the first megabyte of ram because that area 241ae531c26SArjan van de Ven * contains bios code and data regions used by X and dosemu and similar apps. 242ae531c26SArjan van de Ven * Access has to be given to non-kernel-ram areas as well, these contain the PCI 243ae531c26SArjan van de Ven * mmio resources as well as potential bios/acpi data regions. 244ae531c26SArjan van de Ven */ 245ae531c26SArjan van de Ven int devmem_is_allowed(unsigned long pagenr) 246ae531c26SArjan van de Ven { 247ae531c26SArjan van de Ven if (pagenr <= 256) 248ae531c26SArjan van de Ven return 1; 249ae531c26SArjan van de Ven if (!page_is_ram(pagenr)) 250ae531c26SArjan van de Ven return 1; 251ae531c26SArjan van de Ven return 0; 252ae531c26SArjan van de Ven } 253ae531c26SArjan van de Ven 254ad757b6aSThomas Gleixner #ifdef CONFIG_HIGHMEM 255ad757b6aSThomas Gleixner pte_t *kmap_pte; 256ad757b6aSThomas Gleixner pgprot_t kmap_prot; 257ad757b6aSThomas Gleixner 2588550eb99SIngo Molnar static inline pte_t *kmap_get_fixmap_pte(unsigned long vaddr) 2598550eb99SIngo Molnar { 2608550eb99SIngo Molnar return pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k(vaddr), 2618550eb99SIngo Molnar vaddr), vaddr), vaddr); 2628550eb99SIngo Molnar } 263ad757b6aSThomas Gleixner 264ad757b6aSThomas Gleixner static void __init kmap_init(void) 265ad757b6aSThomas Gleixner { 266ad757b6aSThomas Gleixner unsigned long kmap_vstart; 267ad757b6aSThomas Gleixner 2688550eb99SIngo Molnar /* 2698550eb99SIngo Molnar * Cache the first kmap pte: 2708550eb99SIngo Molnar */ 271ad757b6aSThomas Gleixner kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN); 272ad757b6aSThomas Gleixner kmap_pte = kmap_get_fixmap_pte(kmap_vstart); 273ad757b6aSThomas Gleixner 274ad757b6aSThomas Gleixner kmap_prot = PAGE_KERNEL; 275ad757b6aSThomas Gleixner } 276ad757b6aSThomas Gleixner 277ad757b6aSThomas Gleixner static void __init permanent_kmaps_init(pgd_t *pgd_base) 278ad757b6aSThomas Gleixner { 2798550eb99SIngo Molnar unsigned long vaddr; 280ad757b6aSThomas Gleixner pgd_t *pgd; 281ad757b6aSThomas Gleixner pud_t *pud; 282ad757b6aSThomas Gleixner pmd_t *pmd; 283ad757b6aSThomas Gleixner pte_t *pte; 284ad757b6aSThomas Gleixner 285ad757b6aSThomas Gleixner vaddr = PKMAP_BASE; 286ad757b6aSThomas Gleixner page_table_range_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, pgd_base); 287ad757b6aSThomas Gleixner 288ad757b6aSThomas Gleixner pgd = swapper_pg_dir + pgd_index(vaddr); 289ad757b6aSThomas Gleixner pud = pud_offset(pgd, vaddr); 290ad757b6aSThomas Gleixner pmd = pmd_offset(pud, vaddr); 291ad757b6aSThomas Gleixner pte = pte_offset_kernel(pmd, vaddr); 292ad757b6aSThomas Gleixner pkmap_page_table = pte; 293ad757b6aSThomas Gleixner } 294ad757b6aSThomas Gleixner 295ad757b6aSThomas Gleixner void __init add_one_highpage_init(struct page *page, int pfn, int bad_ppro) 296ad757b6aSThomas Gleixner { 297ad757b6aSThomas Gleixner if (page_is_ram(pfn) && !(bad_ppro && page_kills_ppro(pfn))) { 298ad757b6aSThomas Gleixner ClearPageReserved(page); 299180c06efSJeremy Fitzhardinge init_page_count(page); 300180c06efSJeremy Fitzhardinge __free_page(page); 301180c06efSJeremy Fitzhardinge totalhigh_pages++; 302ad757b6aSThomas Gleixner } else 303ad757b6aSThomas Gleixner SetPageReserved(page); 304ad757b6aSThomas Gleixner } 305ad757b6aSThomas Gleixner 3068550eb99SIngo Molnar #ifndef CONFIG_NUMA 307ad757b6aSThomas Gleixner static void __init set_highmem_pages_init(int bad_ppro) 308ad757b6aSThomas Gleixner { 309ad757b6aSThomas Gleixner int pfn; 3108550eb99SIngo Molnar 31123be8c7dSIngo Molnar for (pfn = highstart_pfn; pfn < highend_pfn; pfn++) { 31223be8c7dSIngo Molnar /* 31323be8c7dSIngo Molnar * Holes under sparsemem might not have no mem_map[]: 31423be8c7dSIngo Molnar */ 31523be8c7dSIngo Molnar if (pfn_valid(pfn)) 316ad757b6aSThomas Gleixner add_one_highpage_init(pfn_to_page(pfn), pfn, bad_ppro); 31723be8c7dSIngo Molnar } 318ad757b6aSThomas Gleixner totalram_pages += totalhigh_pages; 319ad757b6aSThomas Gleixner } 3208550eb99SIngo Molnar #endif /* !CONFIG_NUMA */ 321ad757b6aSThomas Gleixner 322ad757b6aSThomas Gleixner #else 323ad757b6aSThomas Gleixner # define kmap_init() do { } while (0) 324ad757b6aSThomas Gleixner # define permanent_kmaps_init(pgd_base) do { } while (0) 325ad757b6aSThomas Gleixner # define set_highmem_pages_init(bad_ppro) do { } while (0) 326ad757b6aSThomas Gleixner #endif /* CONFIG_HIGHMEM */ 327ad757b6aSThomas Gleixner 328c93c82bbSAndi Kleen pteval_t __PAGE_KERNEL = _PAGE_KERNEL; 329ad757b6aSThomas Gleixner EXPORT_SYMBOL(__PAGE_KERNEL); 330ad757b6aSThomas Gleixner 3318550eb99SIngo Molnar pteval_t __PAGE_KERNEL_EXEC = _PAGE_KERNEL_EXEC; 332ad757b6aSThomas Gleixner 333ad757b6aSThomas Gleixner void __init native_pagetable_setup_start(pgd_t *base) 334ad757b6aSThomas Gleixner { 335551889a6SIan Campbell unsigned long pfn, va; 336551889a6SIan Campbell pgd_t *pgd; 337551889a6SIan Campbell pud_t *pud; 338551889a6SIan Campbell pmd_t *pmd; 339551889a6SIan Campbell pte_t *pte; 340ad757b6aSThomas Gleixner 341ad757b6aSThomas Gleixner /* 342551889a6SIan Campbell * Remove any mappings which extend past the end of physical 343551889a6SIan Campbell * memory from the boot time page table: 344ad757b6aSThomas Gleixner */ 345551889a6SIan Campbell for (pfn = max_low_pfn + 1; pfn < 1<<(32-PAGE_SHIFT); pfn++) { 346551889a6SIan Campbell va = PAGE_OFFSET + (pfn<<PAGE_SHIFT); 347551889a6SIan Campbell pgd = base + pgd_index(va); 348551889a6SIan Campbell if (!pgd_present(*pgd)) 349551889a6SIan Campbell break; 350ad757b6aSThomas Gleixner 351551889a6SIan Campbell pud = pud_offset(pgd, va); 352551889a6SIan Campbell pmd = pmd_offset(pud, va); 353551889a6SIan Campbell if (!pmd_present(*pmd)) 354551889a6SIan Campbell break; 355551889a6SIan Campbell 356551889a6SIan Campbell pte = pte_offset_kernel(pmd, va); 357551889a6SIan Campbell if (!pte_present(*pte)) 358551889a6SIan Campbell break; 359551889a6SIan Campbell 360551889a6SIan Campbell pte_clear(NULL, va, pte); 361551889a6SIan Campbell } 3626944a9c8SJeremy Fitzhardinge paravirt_alloc_pmd(&init_mm, __pa(base) >> PAGE_SHIFT); 363ad757b6aSThomas Gleixner } 364ad757b6aSThomas Gleixner 365ad757b6aSThomas Gleixner void __init native_pagetable_setup_done(pgd_t *base) 366ad757b6aSThomas Gleixner { 367ad757b6aSThomas Gleixner } 368ad757b6aSThomas Gleixner 369ad757b6aSThomas Gleixner /* 370ad757b6aSThomas Gleixner * Build a proper pagetable for the kernel mappings. Up until this 371ad757b6aSThomas Gleixner * point, we've been running on some set of pagetables constructed by 372ad757b6aSThomas Gleixner * the boot process. 373ad757b6aSThomas Gleixner * 374ad757b6aSThomas Gleixner * If we're booting on native hardware, this will be a pagetable 375551889a6SIan Campbell * constructed in arch/x86/kernel/head_32.S. The root of the 376551889a6SIan Campbell * pagetable will be swapper_pg_dir. 377ad757b6aSThomas Gleixner * 378ad757b6aSThomas Gleixner * If we're booting paravirtualized under a hypervisor, then there are 379ad757b6aSThomas Gleixner * more options: we may already be running PAE, and the pagetable may 380ad757b6aSThomas Gleixner * or may not be based in swapper_pg_dir. In any case, 381ad757b6aSThomas Gleixner * paravirt_pagetable_setup_start() will set up swapper_pg_dir 382ad757b6aSThomas Gleixner * appropriately for the rest of the initialization to work. 383ad757b6aSThomas Gleixner * 384ad757b6aSThomas Gleixner * In general, pagetable_init() assumes that the pagetable may already 385ad757b6aSThomas Gleixner * be partially populated, and so it avoids stomping on any existing 386ad757b6aSThomas Gleixner * mappings. 387ad757b6aSThomas Gleixner */ 388ad757b6aSThomas Gleixner static void __init pagetable_init(void) 389ad757b6aSThomas Gleixner { 390ad757b6aSThomas Gleixner pgd_t *pgd_base = swapper_pg_dir; 3918550eb99SIngo Molnar unsigned long vaddr, end; 392ad757b6aSThomas Gleixner 393ad757b6aSThomas Gleixner paravirt_pagetable_setup_start(pgd_base); 394ad757b6aSThomas Gleixner 395ad757b6aSThomas Gleixner /* Enable PSE if available */ 396ad757b6aSThomas Gleixner if (cpu_has_pse) 397ad757b6aSThomas Gleixner set_in_cr4(X86_CR4_PSE); 398ad757b6aSThomas Gleixner 399ad757b6aSThomas Gleixner /* Enable PGE if available */ 400ad757b6aSThomas Gleixner if (cpu_has_pge) { 401ad757b6aSThomas Gleixner set_in_cr4(X86_CR4_PGE); 402ad757b6aSThomas Gleixner __PAGE_KERNEL |= _PAGE_GLOBAL; 403ad757b6aSThomas Gleixner __PAGE_KERNEL_EXEC |= _PAGE_GLOBAL; 404ad757b6aSThomas Gleixner } 405ad757b6aSThomas Gleixner 406ad757b6aSThomas Gleixner kernel_physical_mapping_init(pgd_base); 407ad757b6aSThomas Gleixner remap_numa_kva(); 408ad757b6aSThomas Gleixner 409ad757b6aSThomas Gleixner /* 410ad757b6aSThomas Gleixner * Fixed mappings, only the page table structure has to be 411ad757b6aSThomas Gleixner * created - mappings will be set by set_fixmap(): 412ad757b6aSThomas Gleixner */ 413beacfaacSHuang, Ying early_ioremap_clear(); 414ad757b6aSThomas Gleixner vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK; 415ad757b6aSThomas Gleixner end = (FIXADDR_TOP + PMD_SIZE - 1) & PMD_MASK; 416ad757b6aSThomas Gleixner page_table_range_init(vaddr, end, pgd_base); 417beacfaacSHuang, Ying early_ioremap_reset(); 418ad757b6aSThomas Gleixner 419ad757b6aSThomas Gleixner permanent_kmaps_init(pgd_base); 420ad757b6aSThomas Gleixner 421ad757b6aSThomas Gleixner paravirt_pagetable_setup_done(pgd_base); 422ad757b6aSThomas Gleixner } 423ad757b6aSThomas Gleixner 424a6eb84bcSRafael J. Wysocki #ifdef CONFIG_ACPI_SLEEP 425ad757b6aSThomas Gleixner /* 426a6eb84bcSRafael J. Wysocki * ACPI suspend needs this for resume, because things like the intel-agp 427ad757b6aSThomas Gleixner * driver might have split up a kernel 4MB mapping. 428ad757b6aSThomas Gleixner */ 429a6eb84bcSRafael J. Wysocki char swsusp_pg_dir[PAGE_SIZE] 430ad757b6aSThomas Gleixner __attribute__ ((aligned(PAGE_SIZE))); 431ad757b6aSThomas Gleixner 432ad757b6aSThomas Gleixner static inline void save_pg_dir(void) 433ad757b6aSThomas Gleixner { 434ad757b6aSThomas Gleixner memcpy(swsusp_pg_dir, swapper_pg_dir, PAGE_SIZE); 435ad757b6aSThomas Gleixner } 436a6eb84bcSRafael J. Wysocki #else /* !CONFIG_ACPI_SLEEP */ 437ad757b6aSThomas Gleixner static inline void save_pg_dir(void) 438ad757b6aSThomas Gleixner { 439ad757b6aSThomas Gleixner } 440a6eb84bcSRafael J. Wysocki #endif /* !CONFIG_ACPI_SLEEP */ 441ad757b6aSThomas Gleixner 442ad757b6aSThomas Gleixner void zap_low_mappings(void) 443ad757b6aSThomas Gleixner { 444ad757b6aSThomas Gleixner int i; 445ad757b6aSThomas Gleixner 446ad757b6aSThomas Gleixner /* 447ad757b6aSThomas Gleixner * Zap initial low-memory mappings. 448ad757b6aSThomas Gleixner * 449ad757b6aSThomas Gleixner * Note that "pgd_clear()" doesn't do it for 450ad757b6aSThomas Gleixner * us, because pgd_clear() is a no-op on i386. 451ad757b6aSThomas Gleixner */ 45268db065cSJeremy Fitzhardinge for (i = 0; i < KERNEL_PGD_BOUNDARY; i++) { 453ad757b6aSThomas Gleixner #ifdef CONFIG_X86_PAE 454ad757b6aSThomas Gleixner set_pgd(swapper_pg_dir+i, __pgd(1 + __pa(empty_zero_page))); 455ad757b6aSThomas Gleixner #else 456ad757b6aSThomas Gleixner set_pgd(swapper_pg_dir+i, __pgd(0)); 457ad757b6aSThomas Gleixner #endif 4588550eb99SIngo Molnar } 459ad757b6aSThomas Gleixner flush_tlb_all(); 460ad757b6aSThomas Gleixner } 461ad757b6aSThomas Gleixner 4628550eb99SIngo Molnar int nx_enabled; 463ad757b6aSThomas Gleixner 4646fdc05d4SJeremy Fitzhardinge pteval_t __supported_pte_mask __read_mostly = ~_PAGE_NX; 4656fdc05d4SJeremy Fitzhardinge EXPORT_SYMBOL_GPL(__supported_pte_mask); 4666fdc05d4SJeremy Fitzhardinge 467ad757b6aSThomas Gleixner #ifdef CONFIG_X86_PAE 468ad757b6aSThomas Gleixner 4698550eb99SIngo Molnar static int disable_nx __initdata; 470ad757b6aSThomas Gleixner 471ad757b6aSThomas Gleixner /* 472ad757b6aSThomas Gleixner * noexec = on|off 473ad757b6aSThomas Gleixner * 474ad757b6aSThomas Gleixner * Control non executable mappings. 475ad757b6aSThomas Gleixner * 476ad757b6aSThomas Gleixner * on Enable 477ad757b6aSThomas Gleixner * off Disable 478ad757b6aSThomas Gleixner */ 479ad757b6aSThomas Gleixner static int __init noexec_setup(char *str) 480ad757b6aSThomas Gleixner { 481ad757b6aSThomas Gleixner if (!str || !strcmp(str, "on")) { 482ad757b6aSThomas Gleixner if (cpu_has_nx) { 483ad757b6aSThomas Gleixner __supported_pte_mask |= _PAGE_NX; 484ad757b6aSThomas Gleixner disable_nx = 0; 485ad757b6aSThomas Gleixner } 4868550eb99SIngo Molnar } else { 4878550eb99SIngo Molnar if (!strcmp(str, "off")) { 488ad757b6aSThomas Gleixner disable_nx = 1; 489ad757b6aSThomas Gleixner __supported_pte_mask &= ~_PAGE_NX; 4908550eb99SIngo Molnar } else { 491ad757b6aSThomas Gleixner return -EINVAL; 4928550eb99SIngo Molnar } 4938550eb99SIngo Molnar } 494ad757b6aSThomas Gleixner 495ad757b6aSThomas Gleixner return 0; 496ad757b6aSThomas Gleixner } 497ad757b6aSThomas Gleixner early_param("noexec", noexec_setup); 498ad757b6aSThomas Gleixner 499ad757b6aSThomas Gleixner static void __init set_nx(void) 500ad757b6aSThomas Gleixner { 501ad757b6aSThomas Gleixner unsigned int v[4], l, h; 502ad757b6aSThomas Gleixner 503ad757b6aSThomas Gleixner if (cpu_has_pae && (cpuid_eax(0x80000000) > 0x80000001)) { 504ad757b6aSThomas Gleixner cpuid(0x80000001, &v[0], &v[1], &v[2], &v[3]); 5058550eb99SIngo Molnar 506ad757b6aSThomas Gleixner if ((v[3] & (1 << 20)) && !disable_nx) { 507ad757b6aSThomas Gleixner rdmsr(MSR_EFER, l, h); 508ad757b6aSThomas Gleixner l |= EFER_NX; 509ad757b6aSThomas Gleixner wrmsr(MSR_EFER, l, h); 510ad757b6aSThomas Gleixner nx_enabled = 1; 511ad757b6aSThomas Gleixner __supported_pte_mask |= _PAGE_NX; 512ad757b6aSThomas Gleixner } 513ad757b6aSThomas Gleixner } 514ad757b6aSThomas Gleixner } 515ad757b6aSThomas Gleixner #endif 516ad757b6aSThomas Gleixner 517ad757b6aSThomas Gleixner /* 518ad757b6aSThomas Gleixner * paging_init() sets up the page tables - note that the first 8MB are 519ad757b6aSThomas Gleixner * already mapped by head.S. 520ad757b6aSThomas Gleixner * 521ad757b6aSThomas Gleixner * This routines also unmaps the page at virtual kernel address 0, so 522ad757b6aSThomas Gleixner * that we can trap those pesky NULL-reference errors in the kernel. 523ad757b6aSThomas Gleixner */ 524ad757b6aSThomas Gleixner void __init paging_init(void) 525ad757b6aSThomas Gleixner { 526ad757b6aSThomas Gleixner #ifdef CONFIG_X86_PAE 527ad757b6aSThomas Gleixner set_nx(); 528ad757b6aSThomas Gleixner if (nx_enabled) 529d7d119d7SIngo Molnar printk(KERN_INFO "NX (Execute Disable) protection: active\n"); 530ad757b6aSThomas Gleixner #endif 531ad757b6aSThomas Gleixner pagetable_init(); 532ad757b6aSThomas Gleixner 533ad757b6aSThomas Gleixner load_cr3(swapper_pg_dir); 534ad757b6aSThomas Gleixner 535ad757b6aSThomas Gleixner __flush_tlb_all(); 536ad757b6aSThomas Gleixner 537ad757b6aSThomas Gleixner kmap_init(); 538ad757b6aSThomas Gleixner } 539ad757b6aSThomas Gleixner 540ad757b6aSThomas Gleixner /* 541ad757b6aSThomas Gleixner * Test if the WP bit works in supervisor mode. It isn't supported on 386's 542f7f17a67SDmitri Vorobiev * and also on some strange 486's. All 586+'s are OK. This used to involve 543f7f17a67SDmitri Vorobiev * black magic jumps to work around some nasty CPU bugs, but fortunately the 544f7f17a67SDmitri Vorobiev * switch to using exceptions got rid of all that. 545ad757b6aSThomas Gleixner */ 546ad757b6aSThomas Gleixner static void __init test_wp_bit(void) 547ad757b6aSThomas Gleixner { 548d7d119d7SIngo Molnar printk(KERN_INFO 549d7d119d7SIngo Molnar "Checking if this processor honours the WP bit even in supervisor mode..."); 550ad757b6aSThomas Gleixner 551ad757b6aSThomas Gleixner /* Any page-aligned address will do, the test is non-destructive */ 552ad757b6aSThomas Gleixner __set_fixmap(FIX_WP_TEST, __pa(&swapper_pg_dir), PAGE_READONLY); 553ad757b6aSThomas Gleixner boot_cpu_data.wp_works_ok = do_test_wp_bit(); 554ad757b6aSThomas Gleixner clear_fixmap(FIX_WP_TEST); 555ad757b6aSThomas Gleixner 556ad757b6aSThomas Gleixner if (!boot_cpu_data.wp_works_ok) { 557d7d119d7SIngo Molnar printk(KERN_CONT "No.\n"); 558ad757b6aSThomas Gleixner #ifdef CONFIG_X86_WP_WORKS_OK 559d7d119d7SIngo Molnar panic( 560d7d119d7SIngo Molnar "This kernel doesn't support CPU's with broken WP. Recompile it for a 386!"); 561ad757b6aSThomas Gleixner #endif 562ad757b6aSThomas Gleixner } else { 563d7d119d7SIngo Molnar printk(KERN_CONT "Ok.\n"); 564ad757b6aSThomas Gleixner } 565ad757b6aSThomas Gleixner } 566ad757b6aSThomas Gleixner 567ad757b6aSThomas Gleixner static struct kcore_list kcore_mem, kcore_vmalloc; 568ad757b6aSThomas Gleixner 569ad757b6aSThomas Gleixner void __init mem_init(void) 570ad757b6aSThomas Gleixner { 571ad757b6aSThomas Gleixner int codesize, reservedpages, datasize, initsize; 5728550eb99SIngo Molnar int tmp, bad_ppro; 573ad757b6aSThomas Gleixner 574ad757b6aSThomas Gleixner #ifdef CONFIG_FLATMEM 575ad757b6aSThomas Gleixner BUG_ON(!mem_map); 576ad757b6aSThomas Gleixner #endif 577ad757b6aSThomas Gleixner bad_ppro = ppro_with_ram_bug(); 578ad757b6aSThomas Gleixner 579ad757b6aSThomas Gleixner #ifdef CONFIG_HIGHMEM 580ad757b6aSThomas Gleixner /* check that fixmap and pkmap do not overlap */ 581ad757b6aSThomas Gleixner if (PKMAP_BASE + LAST_PKMAP*PAGE_SIZE >= FIXADDR_START) { 582d7d119d7SIngo Molnar printk(KERN_ERR 583d7d119d7SIngo Molnar "fixmap and kmap areas overlap - this will crash\n"); 584ad757b6aSThomas Gleixner printk(KERN_ERR "pkstart: %lxh pkend: %lxh fixstart %lxh\n", 585d7d119d7SIngo Molnar PKMAP_BASE, PKMAP_BASE + LAST_PKMAP*PAGE_SIZE, 586d7d119d7SIngo Molnar FIXADDR_START); 587ad757b6aSThomas Gleixner BUG(); 588ad757b6aSThomas Gleixner } 589ad757b6aSThomas Gleixner #endif 590ad757b6aSThomas Gleixner /* this will put all low memory onto the freelists */ 591ad757b6aSThomas Gleixner totalram_pages += free_all_bootmem(); 592ad757b6aSThomas Gleixner 593ad757b6aSThomas Gleixner reservedpages = 0; 594ad757b6aSThomas Gleixner for (tmp = 0; tmp < max_low_pfn; tmp++) 595ad757b6aSThomas Gleixner /* 5968550eb99SIngo Molnar * Only count reserved RAM pages: 597ad757b6aSThomas Gleixner */ 598ad757b6aSThomas Gleixner if (page_is_ram(tmp) && PageReserved(pfn_to_page(tmp))) 599ad757b6aSThomas Gleixner reservedpages++; 600ad757b6aSThomas Gleixner 601ad757b6aSThomas Gleixner set_highmem_pages_init(bad_ppro); 602ad757b6aSThomas Gleixner 603ad757b6aSThomas Gleixner codesize = (unsigned long) &_etext - (unsigned long) &_text; 604ad757b6aSThomas Gleixner datasize = (unsigned long) &_edata - (unsigned long) &_etext; 605ad757b6aSThomas Gleixner initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin; 606ad757b6aSThomas Gleixner 607ad757b6aSThomas Gleixner kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT); 608ad757b6aSThomas Gleixner kclist_add(&kcore_vmalloc, (void *)VMALLOC_START, 609ad757b6aSThomas Gleixner VMALLOC_END-VMALLOC_START); 610ad757b6aSThomas Gleixner 6118550eb99SIngo Molnar printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, " 6128550eb99SIngo Molnar "%dk reserved, %dk data, %dk init, %ldk highmem)\n", 613ad757b6aSThomas Gleixner (unsigned long) nr_free_pages() << (PAGE_SHIFT-10), 614ad757b6aSThomas Gleixner num_physpages << (PAGE_SHIFT-10), 615ad757b6aSThomas Gleixner codesize >> 10, 616ad757b6aSThomas Gleixner reservedpages << (PAGE_SHIFT-10), 617ad757b6aSThomas Gleixner datasize >> 10, 618ad757b6aSThomas Gleixner initsize >> 10, 619ad757b6aSThomas Gleixner (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10)) 620ad757b6aSThomas Gleixner ); 621ad757b6aSThomas Gleixner 622ad757b6aSThomas Gleixner #if 1 /* double-sanity-check paranoia */ 623d7d119d7SIngo Molnar printk(KERN_INFO "virtual kernel memory layout:\n" 624ad757b6aSThomas Gleixner " fixmap : 0x%08lx - 0x%08lx (%4ld kB)\n" 625ad757b6aSThomas Gleixner #ifdef CONFIG_HIGHMEM 626ad757b6aSThomas Gleixner " pkmap : 0x%08lx - 0x%08lx (%4ld kB)\n" 627ad757b6aSThomas Gleixner #endif 628ad757b6aSThomas Gleixner " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n" 629ad757b6aSThomas Gleixner " lowmem : 0x%08lx - 0x%08lx (%4ld MB)\n" 630ad757b6aSThomas Gleixner " .init : 0x%08lx - 0x%08lx (%4ld kB)\n" 631ad757b6aSThomas Gleixner " .data : 0x%08lx - 0x%08lx (%4ld kB)\n" 632ad757b6aSThomas Gleixner " .text : 0x%08lx - 0x%08lx (%4ld kB)\n", 633ad757b6aSThomas Gleixner FIXADDR_START, FIXADDR_TOP, 634ad757b6aSThomas Gleixner (FIXADDR_TOP - FIXADDR_START) >> 10, 635ad757b6aSThomas Gleixner 636ad757b6aSThomas Gleixner #ifdef CONFIG_HIGHMEM 637ad757b6aSThomas Gleixner PKMAP_BASE, PKMAP_BASE+LAST_PKMAP*PAGE_SIZE, 638ad757b6aSThomas Gleixner (LAST_PKMAP*PAGE_SIZE) >> 10, 639ad757b6aSThomas Gleixner #endif 640ad757b6aSThomas Gleixner 641ad757b6aSThomas Gleixner VMALLOC_START, VMALLOC_END, 642ad757b6aSThomas Gleixner (VMALLOC_END - VMALLOC_START) >> 20, 643ad757b6aSThomas Gleixner 644ad757b6aSThomas Gleixner (unsigned long)__va(0), (unsigned long)high_memory, 645ad757b6aSThomas Gleixner ((unsigned long)high_memory - (unsigned long)__va(0)) >> 20, 646ad757b6aSThomas Gleixner 647ad757b6aSThomas Gleixner (unsigned long)&__init_begin, (unsigned long)&__init_end, 6488550eb99SIngo Molnar ((unsigned long)&__init_end - 6498550eb99SIngo Molnar (unsigned long)&__init_begin) >> 10, 650ad757b6aSThomas Gleixner 651ad757b6aSThomas Gleixner (unsigned long)&_etext, (unsigned long)&_edata, 652ad757b6aSThomas Gleixner ((unsigned long)&_edata - (unsigned long)&_etext) >> 10, 653ad757b6aSThomas Gleixner 654ad757b6aSThomas Gleixner (unsigned long)&_text, (unsigned long)&_etext, 655ad757b6aSThomas Gleixner ((unsigned long)&_etext - (unsigned long)&_text) >> 10); 656ad757b6aSThomas Gleixner 657ad757b6aSThomas Gleixner #ifdef CONFIG_HIGHMEM 658ad757b6aSThomas Gleixner BUG_ON(PKMAP_BASE + LAST_PKMAP*PAGE_SIZE > FIXADDR_START); 659ad757b6aSThomas Gleixner BUG_ON(VMALLOC_END > PKMAP_BASE); 660ad757b6aSThomas Gleixner #endif 661ad757b6aSThomas Gleixner BUG_ON(VMALLOC_START > VMALLOC_END); 662ad757b6aSThomas Gleixner BUG_ON((unsigned long)high_memory > VMALLOC_START); 663ad757b6aSThomas Gleixner #endif /* double-sanity-check paranoia */ 664ad757b6aSThomas Gleixner 665ad757b6aSThomas Gleixner if (boot_cpu_data.wp_works_ok < 0) 666ad757b6aSThomas Gleixner test_wp_bit(); 667ad757b6aSThomas Gleixner 66876ebd054SThomas Gleixner cpa_init(); 66961165d7aSHugh Dickins save_pg_dir(); 670ad757b6aSThomas Gleixner zap_low_mappings(); 671ad757b6aSThomas Gleixner } 672ad757b6aSThomas Gleixner 673ad757b6aSThomas Gleixner #ifdef CONFIG_MEMORY_HOTPLUG 674ad757b6aSThomas Gleixner int arch_add_memory(int nid, u64 start, u64 size) 675ad757b6aSThomas Gleixner { 676ad757b6aSThomas Gleixner struct pglist_data *pgdata = NODE_DATA(nid); 677ad757b6aSThomas Gleixner struct zone *zone = pgdata->node_zones + ZONE_HIGHMEM; 678ad757b6aSThomas Gleixner unsigned long start_pfn = start >> PAGE_SHIFT; 679ad757b6aSThomas Gleixner unsigned long nr_pages = size >> PAGE_SHIFT; 680ad757b6aSThomas Gleixner 681ad757b6aSThomas Gleixner return __add_pages(zone, start_pfn, nr_pages); 682ad757b6aSThomas Gleixner } 683ad757b6aSThomas Gleixner #endif 684ad757b6aSThomas Gleixner 685ad757b6aSThomas Gleixner /* 686ad757b6aSThomas Gleixner * This function cannot be __init, since exceptions don't work in that 687ad757b6aSThomas Gleixner * section. Put this after the callers, so that it cannot be inlined. 688ad757b6aSThomas Gleixner */ 6898550eb99SIngo Molnar static noinline int do_test_wp_bit(void) 690ad757b6aSThomas Gleixner { 691ad757b6aSThomas Gleixner char tmp_reg; 692ad757b6aSThomas Gleixner int flag; 693ad757b6aSThomas Gleixner 694ad757b6aSThomas Gleixner __asm__ __volatile__( 695ad757b6aSThomas Gleixner " movb %0, %1 \n" 696ad757b6aSThomas Gleixner "1: movb %1, %0 \n" 697ad757b6aSThomas Gleixner " xorl %2, %2 \n" 698ad757b6aSThomas Gleixner "2: \n" 699f832ff18SH. Peter Anvin _ASM_EXTABLE(1b,2b) 700ad757b6aSThomas Gleixner :"=m" (*(char *)fix_to_virt(FIX_WP_TEST)), 701ad757b6aSThomas Gleixner "=q" (tmp_reg), 702ad757b6aSThomas Gleixner "=r" (flag) 703ad757b6aSThomas Gleixner :"2" (1) 704ad757b6aSThomas Gleixner :"memory"); 705ad757b6aSThomas Gleixner 706ad757b6aSThomas Gleixner return flag; 707ad757b6aSThomas Gleixner } 708ad757b6aSThomas Gleixner 709ad757b6aSThomas Gleixner #ifdef CONFIG_DEBUG_RODATA 710edeed305SArjan van de Ven const int rodata_test_data = 0xC3; 711edeed305SArjan van de Ven EXPORT_SYMBOL_GPL(rodata_test_data); 712ad757b6aSThomas Gleixner 713ad757b6aSThomas Gleixner void mark_rodata_ro(void) 714ad757b6aSThomas Gleixner { 715ad757b6aSThomas Gleixner unsigned long start = PFN_ALIGN(_text); 716ad757b6aSThomas Gleixner unsigned long size = PFN_ALIGN(_etext) - start; 717ad757b6aSThomas Gleixner 7186d238cc4SArjan van de Ven set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT); 719d7d119d7SIngo Molnar printk(KERN_INFO "Write protecting the kernel text: %luk\n", 720d7d119d7SIngo Molnar size >> 10); 7210c42f392SAndi Kleen 7220c42f392SAndi Kleen #ifdef CONFIG_CPA_DEBUG 723d7d119d7SIngo Molnar printk(KERN_INFO "Testing CPA: Reverting %lx-%lx\n", 724d7d119d7SIngo Molnar start, start+size); 7256d238cc4SArjan van de Ven set_pages_rw(virt_to_page(start), size>>PAGE_SHIFT); 7260c42f392SAndi Kleen 727d7d119d7SIngo Molnar printk(KERN_INFO "Testing CPA: write protecting again\n"); 7286d238cc4SArjan van de Ven set_pages_ro(virt_to_page(start), size>>PAGE_SHIFT); 7290c42f392SAndi Kleen #endif 730ad757b6aSThomas Gleixner start += size; 731ad757b6aSThomas Gleixner size = (unsigned long)__end_rodata - start; 7326d238cc4SArjan van de Ven set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT); 733d7d119d7SIngo Molnar printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n", 734ad757b6aSThomas Gleixner size >> 10); 735edeed305SArjan van de Ven rodata_test(); 736ad757b6aSThomas Gleixner 7370c42f392SAndi Kleen #ifdef CONFIG_CPA_DEBUG 738d7d119d7SIngo Molnar printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, start + size); 7396d238cc4SArjan van de Ven set_pages_rw(virt_to_page(start), size >> PAGE_SHIFT); 7400c42f392SAndi Kleen 741d7d119d7SIngo Molnar printk(KERN_INFO "Testing CPA: write protecting again\n"); 7426d238cc4SArjan van de Ven set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT); 7430c42f392SAndi Kleen #endif 744ad757b6aSThomas Gleixner } 745ad757b6aSThomas Gleixner #endif 746ad757b6aSThomas Gleixner 747ad757b6aSThomas Gleixner void free_init_pages(char *what, unsigned long begin, unsigned long end) 748ad757b6aSThomas Gleixner { 749ee01f112SIngo Molnar #ifdef CONFIG_DEBUG_PAGEALLOC 750ee01f112SIngo Molnar /* 751ee01f112SIngo Molnar * If debugging page accesses then do not free this memory but 752ee01f112SIngo Molnar * mark them not present - any buggy init-section access will 753ee01f112SIngo Molnar * create a kernel page fault: 754ee01f112SIngo Molnar */ 755ee01f112SIngo Molnar printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n", 756ee01f112SIngo Molnar begin, PAGE_ALIGN(end)); 757ee01f112SIngo Molnar set_memory_np(begin, (end - begin) >> PAGE_SHIFT); 758ee01f112SIngo Molnar #else 75986f03989SIngo Molnar unsigned long addr; 76086f03989SIngo Molnar 7613c1df68bSArjan van de Ven /* 7623c1df68bSArjan van de Ven * We just marked the kernel text read only above, now that 7633c1df68bSArjan van de Ven * we are going to free part of that, we need to make that 7643c1df68bSArjan van de Ven * writeable first. 7653c1df68bSArjan van de Ven */ 7663c1df68bSArjan van de Ven set_memory_rw(begin, (end - begin) >> PAGE_SHIFT); 7673c1df68bSArjan van de Ven 768ad757b6aSThomas Gleixner for (addr = begin; addr < end; addr += PAGE_SIZE) { 769ad757b6aSThomas Gleixner ClearPageReserved(virt_to_page(addr)); 770ad757b6aSThomas Gleixner init_page_count(virt_to_page(addr)); 771ad757b6aSThomas Gleixner memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE); 772ad757b6aSThomas Gleixner free_page(addr); 773ad757b6aSThomas Gleixner totalram_pages++; 774ad757b6aSThomas Gleixner } 775ad757b6aSThomas Gleixner printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10); 776ee01f112SIngo Molnar #endif 777ad757b6aSThomas Gleixner } 778ad757b6aSThomas Gleixner 779ad757b6aSThomas Gleixner void free_initmem(void) 780ad757b6aSThomas Gleixner { 781ad757b6aSThomas Gleixner free_init_pages("unused kernel memory", 782ad757b6aSThomas Gleixner (unsigned long)(&__init_begin), 783ad757b6aSThomas Gleixner (unsigned long)(&__init_end)); 784ad757b6aSThomas Gleixner } 785ad757b6aSThomas Gleixner 786ad757b6aSThomas Gleixner #ifdef CONFIG_BLK_DEV_INITRD 787ad757b6aSThomas Gleixner void free_initrd_mem(unsigned long start, unsigned long end) 788ad757b6aSThomas Gleixner { 789ad757b6aSThomas Gleixner free_init_pages("initrd memory", start, end); 790ad757b6aSThomas Gleixner } 791ad757b6aSThomas Gleixner #endif 792