xref: /linux/arch/x86/mm/init_32.c (revision 1d931264af0f10649b35afa8fbd2e169da51ac08)
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>
24cfb80c9eSJeremy Fitzhardinge #include <linux/pci.h>
25ad757b6aSThomas Gleixner #include <linux/pfn.h>
26ad757b6aSThomas Gleixner #include <linux/poison.h>
27ad757b6aSThomas Gleixner #include <linux/bootmem.h>
28a9ce6bc1SYinghai Lu #include <linux/memblock.h>
29ad757b6aSThomas Gleixner #include <linux/proc_fs.h>
30ad757b6aSThomas Gleixner #include <linux/memory_hotplug.h>
31ad757b6aSThomas Gleixner #include <linux/initrd.h>
32ad757b6aSThomas Gleixner #include <linux/cpumask.h>
335a0e3ad6STejun Heo #include <linux/gfp.h>
34ad757b6aSThomas Gleixner 
35f832ff18SH. Peter Anvin #include <asm/asm.h>
3646eaa670SIngo Molnar #include <asm/bios_ebda.h>
37ad757b6aSThomas Gleixner #include <asm/processor.h>
38ad757b6aSThomas Gleixner #include <asm/system.h>
39ad757b6aSThomas Gleixner #include <asm/uaccess.h>
40ad757b6aSThomas Gleixner #include <asm/pgtable.h>
41ad757b6aSThomas Gleixner #include <asm/dma.h>
42ad757b6aSThomas Gleixner #include <asm/fixmap.h>
43ad757b6aSThomas Gleixner #include <asm/e820.h>
44ad757b6aSThomas Gleixner #include <asm/apic.h>
458550eb99SIngo Molnar #include <asm/bugs.h>
46ad757b6aSThomas Gleixner #include <asm/tlb.h>
47ad757b6aSThomas Gleixner #include <asm/tlbflush.h>
48a5a19c63SJeremy Fitzhardinge #include <asm/pgalloc.h>
49ad757b6aSThomas Gleixner #include <asm/sections.h>
50ad757b6aSThomas Gleixner #include <asm/paravirt.h>
51551889a6SIan Campbell #include <asm/setup.h>
527bfeab9aSHarvey Harrison #include <asm/cacheflush.h>
532b72394eSPekka Enberg #include <asm/page_types.h>
544fcb2083SPekka Enberg #include <asm/init.h>
55ad757b6aSThomas Gleixner 
56ad757b6aSThomas Gleixner unsigned long highstart_pfn, highend_pfn;
57ad757b6aSThomas Gleixner 
588550eb99SIngo Molnar static noinline int do_test_wp_bit(void);
59ad757b6aSThomas Gleixner 
60dc16ecf7SJeremy Fitzhardinge bool __read_mostly __vmalloc_start_set = false;
614e29684cSYinghai Lu 
62d6be89adSJan Beulich static __init void *alloc_low_page(void)
634e29684cSYinghai Lu {
64298af9d8SPekka Enberg 	unsigned long pfn = e820_table_end++;
654e29684cSYinghai Lu 	void *adr;
664e29684cSYinghai Lu 
67298af9d8SPekka Enberg 	if (pfn >= e820_table_top)
684e29684cSYinghai Lu 		panic("alloc_low_page: ran out of memory");
694e29684cSYinghai Lu 
704e29684cSYinghai Lu 	adr = __va(pfn * PAGE_SIZE);
714e29684cSYinghai Lu 	memset(adr, 0, PAGE_SIZE);
724e29684cSYinghai Lu 	return adr;
734e29684cSYinghai Lu }
744e29684cSYinghai Lu 
75ad757b6aSThomas Gleixner /*
76ad757b6aSThomas Gleixner  * Creates a middle page table and puts a pointer to it in the
77ad757b6aSThomas Gleixner  * given global directory entry. This only returns the gd entry
78ad757b6aSThomas Gleixner  * in non-PAE compilation mode, since the middle layer is folded.
79ad757b6aSThomas Gleixner  */
80ad757b6aSThomas Gleixner static pmd_t * __init one_md_table_init(pgd_t *pgd)
81ad757b6aSThomas Gleixner {
82ad757b6aSThomas Gleixner 	pud_t *pud;
83ad757b6aSThomas Gleixner 	pmd_t *pmd_table;
84ad757b6aSThomas Gleixner 
85ad757b6aSThomas Gleixner #ifdef CONFIG_X86_PAE
86ad757b6aSThomas Gleixner 	if (!(pgd_val(*pgd) & _PAGE_PRESENT)) {
87c464573cSPekka Enberg 		if (after_bootmem)
883c1596efSJan Beulich 			pmd_table = (pmd_t *)alloc_bootmem_pages(PAGE_SIZE);
894e29684cSYinghai Lu 		else
90d6be89adSJan Beulich 			pmd_table = (pmd_t *)alloc_low_page();
916944a9c8SJeremy Fitzhardinge 		paravirt_alloc_pmd(&init_mm, __pa(pmd_table) >> PAGE_SHIFT);
92ad757b6aSThomas Gleixner 		set_pgd(pgd, __pgd(__pa(pmd_table) | _PAGE_PRESENT));
93ad757b6aSThomas Gleixner 		pud = pud_offset(pgd, 0);
948550eb99SIngo Molnar 		BUG_ON(pmd_table != pmd_offset(pud, 0));
95a376f30aSZhaolei 
96a376f30aSZhaolei 		return pmd_table;
97ad757b6aSThomas Gleixner 	}
98ad757b6aSThomas Gleixner #endif
99ad757b6aSThomas Gleixner 	pud = pud_offset(pgd, 0);
100ad757b6aSThomas Gleixner 	pmd_table = pmd_offset(pud, 0);
1018550eb99SIngo Molnar 
102ad757b6aSThomas Gleixner 	return pmd_table;
103ad757b6aSThomas Gleixner }
104ad757b6aSThomas Gleixner 
105ad757b6aSThomas Gleixner /*
106ad757b6aSThomas Gleixner  * Create a page table and place a pointer to it in a middle page
1078550eb99SIngo Molnar  * directory entry:
108ad757b6aSThomas Gleixner  */
109ad757b6aSThomas Gleixner static pte_t * __init one_page_table_init(pmd_t *pmd)
110ad757b6aSThomas Gleixner {
111ad757b6aSThomas Gleixner 	if (!(pmd_val(*pmd) & _PAGE_PRESENT)) {
112509a80c4SIngo Molnar 		pte_t *page_table = NULL;
113509a80c4SIngo Molnar 
114c464573cSPekka Enberg 		if (after_bootmem) {
115f8561296SVegard Nossum #if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_KMEMCHECK)
116509a80c4SIngo Molnar 			page_table = (pte_t *) alloc_bootmem_pages(PAGE_SIZE);
117509a80c4SIngo Molnar #endif
1184e29684cSYinghai Lu 			if (!page_table)
119509a80c4SIngo Molnar 				page_table =
1203c1596efSJan Beulich 				(pte_t *)alloc_bootmem_pages(PAGE_SIZE);
121d6be89adSJan Beulich 		} else
122d6be89adSJan Beulich 			page_table = (pte_t *)alloc_low_page();
123ad757b6aSThomas Gleixner 
1246944a9c8SJeremy Fitzhardinge 		paravirt_alloc_pte(&init_mm, __pa(page_table) >> PAGE_SHIFT);
125ad757b6aSThomas Gleixner 		set_pmd(pmd, __pmd(__pa(page_table) | _PAGE_TABLE));
126ad757b6aSThomas Gleixner 		BUG_ON(page_table != pte_offset_kernel(pmd, 0));
127ad757b6aSThomas Gleixner 	}
128ad757b6aSThomas Gleixner 
129ad757b6aSThomas Gleixner 	return pte_offset_kernel(pmd, 0);
130ad757b6aSThomas Gleixner }
131ad757b6aSThomas Gleixner 
132458a3e64STejun Heo pmd_t * __init populate_extra_pmd(unsigned long vaddr)
13311124411STejun Heo {
13411124411STejun Heo 	int pgd_idx = pgd_index(vaddr);
13511124411STejun Heo 	int pmd_idx = pmd_index(vaddr);
136458a3e64STejun Heo 
137458a3e64STejun Heo 	return one_md_table_init(swapper_pg_dir + pgd_idx) + pmd_idx;
138458a3e64STejun Heo }
139458a3e64STejun Heo 
140458a3e64STejun Heo pte_t * __init populate_extra_pte(unsigned long vaddr)
141458a3e64STejun Heo {
142458a3e64STejun Heo 	int pte_idx = pte_index(vaddr);
14311124411STejun Heo 	pmd_t *pmd;
14411124411STejun Heo 
145458a3e64STejun Heo 	pmd = populate_extra_pmd(vaddr);
146458a3e64STejun Heo 	return one_page_table_init(pmd) + pte_idx;
14711124411STejun Heo }
14811124411STejun Heo 
149a3c6018eSJan Beulich static pte_t *__init page_table_kmap_check(pte_t *pte, pmd_t *pmd,
150a3c6018eSJan Beulich 					   unsigned long vaddr, pte_t *lastpte)
151a3c6018eSJan Beulich {
152a3c6018eSJan Beulich #ifdef CONFIG_HIGHMEM
153a3c6018eSJan Beulich 	/*
154a3c6018eSJan Beulich 	 * Something (early fixmap) may already have put a pte
155a3c6018eSJan Beulich 	 * page here, which causes the page table allocation
156a3c6018eSJan Beulich 	 * to become nonlinear. Attempt to fix it, and if it
157a3c6018eSJan Beulich 	 * is still nonlinear then we have to bug.
158a3c6018eSJan Beulich 	 */
159a3c6018eSJan Beulich 	int pmd_idx_kmap_begin = fix_to_virt(FIX_KMAP_END) >> PMD_SHIFT;
160a3c6018eSJan Beulich 	int pmd_idx_kmap_end = fix_to_virt(FIX_KMAP_BEGIN) >> PMD_SHIFT;
161a3c6018eSJan Beulich 
162a3c6018eSJan Beulich 	if (pmd_idx_kmap_begin != pmd_idx_kmap_end
163a3c6018eSJan Beulich 	    && (vaddr >> PMD_SHIFT) >= pmd_idx_kmap_begin
164a3c6018eSJan Beulich 	    && (vaddr >> PMD_SHIFT) <= pmd_idx_kmap_end
165298af9d8SPekka Enberg 	    && ((__pa(pte) >> PAGE_SHIFT) < e820_table_start
166298af9d8SPekka Enberg 		|| (__pa(pte) >> PAGE_SHIFT) >= e820_table_end)) {
167a3c6018eSJan Beulich 		pte_t *newpte;
168a3c6018eSJan Beulich 		int i;
169a3c6018eSJan Beulich 
170c464573cSPekka Enberg 		BUG_ON(after_bootmem);
171a3c6018eSJan Beulich 		newpte = alloc_low_page();
172a3c6018eSJan Beulich 		for (i = 0; i < PTRS_PER_PTE; i++)
173a3c6018eSJan Beulich 			set_pte(newpte + i, pte[i]);
174a3c6018eSJan Beulich 
175a3c6018eSJan Beulich 		paravirt_alloc_pte(&init_mm, __pa(newpte) >> PAGE_SHIFT);
176a3c6018eSJan Beulich 		set_pmd(pmd, __pmd(__pa(newpte)|_PAGE_TABLE));
177a3c6018eSJan Beulich 		BUG_ON(newpte != pte_offset_kernel(pmd, 0));
178a3c6018eSJan Beulich 		__flush_tlb_all();
179a3c6018eSJan Beulich 
180a3c6018eSJan Beulich 		paravirt_release_pte(__pa(pte) >> PAGE_SHIFT);
181a3c6018eSJan Beulich 		pte = newpte;
182a3c6018eSJan Beulich 	}
183a3c6018eSJan Beulich 	BUG_ON(vaddr < fix_to_virt(FIX_KMAP_BEGIN - 1)
184a3c6018eSJan Beulich 	       && vaddr > fix_to_virt(FIX_KMAP_END)
185a3c6018eSJan Beulich 	       && lastpte && lastpte + PTRS_PER_PTE != pte);
186a3c6018eSJan Beulich #endif
187a3c6018eSJan Beulich 	return pte;
188a3c6018eSJan Beulich }
189a3c6018eSJan Beulich 
190ad757b6aSThomas Gleixner /*
191ad757b6aSThomas Gleixner  * This function initializes a certain range of kernel virtual memory
192ad757b6aSThomas Gleixner  * with new bootmem page tables, everywhere page tables are missing in
193ad757b6aSThomas Gleixner  * the given range.
1948550eb99SIngo Molnar  *
195ad757b6aSThomas Gleixner  * NOTE: The pagetables are allocated contiguous on the physical space
196ad757b6aSThomas Gleixner  * so we can cache the place of the first one and move around without
197ad757b6aSThomas Gleixner  * checking the pgd every time.
198ad757b6aSThomas Gleixner  */
1998550eb99SIngo Molnar static void __init
2008550eb99SIngo Molnar page_table_range_init(unsigned long start, unsigned long end, pgd_t *pgd_base)
201ad757b6aSThomas Gleixner {
202ad757b6aSThomas Gleixner 	int pgd_idx, pmd_idx;
203ad757b6aSThomas Gleixner 	unsigned long vaddr;
2048550eb99SIngo Molnar 	pgd_t *pgd;
2058550eb99SIngo Molnar 	pmd_t *pmd;
206a3c6018eSJan Beulich 	pte_t *pte = NULL;
207ad757b6aSThomas Gleixner 
208ad757b6aSThomas Gleixner 	vaddr = start;
209ad757b6aSThomas Gleixner 	pgd_idx = pgd_index(vaddr);
210ad757b6aSThomas Gleixner 	pmd_idx = pmd_index(vaddr);
211ad757b6aSThomas Gleixner 	pgd = pgd_base + pgd_idx;
212ad757b6aSThomas Gleixner 
213ad757b6aSThomas Gleixner 	for ( ; (pgd_idx < PTRS_PER_PGD) && (vaddr != end); pgd++, pgd_idx++) {
214ad757b6aSThomas Gleixner 		pmd = one_md_table_init(pgd);
215ad757b6aSThomas Gleixner 		pmd = pmd + pmd_index(vaddr);
2168550eb99SIngo Molnar 		for (; (pmd_idx < PTRS_PER_PMD) && (vaddr != end);
2178550eb99SIngo Molnar 							pmd++, pmd_idx++) {
218a3c6018eSJan Beulich 			pte = page_table_kmap_check(one_page_table_init(pmd),
219a3c6018eSJan Beulich 			                            pmd, vaddr, pte);
220ad757b6aSThomas Gleixner 
221ad757b6aSThomas Gleixner 			vaddr += PMD_SIZE;
222ad757b6aSThomas Gleixner 		}
223ad757b6aSThomas Gleixner 		pmd_idx = 0;
224ad757b6aSThomas Gleixner 	}
225ad757b6aSThomas Gleixner }
226ad757b6aSThomas Gleixner 
227ad757b6aSThomas Gleixner static inline int is_kernel_text(unsigned long addr)
228ad757b6aSThomas Gleixner {
229ad757b6aSThomas Gleixner 	if (addr >= PAGE_OFFSET && addr <= (unsigned long)__init_end)
230ad757b6aSThomas Gleixner 		return 1;
231ad757b6aSThomas Gleixner 	return 0;
232ad757b6aSThomas Gleixner }
233ad757b6aSThomas Gleixner 
234ad757b6aSThomas Gleixner /*
235ad757b6aSThomas Gleixner  * This maps the physical memory to kernel virtual address space, a total
236ad757b6aSThomas Gleixner  * of max_low_pfn pages, by creating page tables starting from address
2378550eb99SIngo Molnar  * PAGE_OFFSET:
238ad757b6aSThomas Gleixner  */
239e53fb04fSPekka Enberg unsigned long __init
240e53fb04fSPekka Enberg kernel_physical_mapping_init(unsigned long start,
241e53fb04fSPekka Enberg 			     unsigned long end,
242e53fb04fSPekka Enberg 			     unsigned long page_size_mask)
243ad757b6aSThomas Gleixner {
244e53fb04fSPekka Enberg 	int use_pse = page_size_mask == (1<<PG_LEVEL_2M);
245c1fd1b43SPekka Enberg 	unsigned long last_map_addr = end;
246e53fb04fSPekka Enberg 	unsigned long start_pfn, end_pfn;
247e7179853SPekka Enberg 	pgd_t *pgd_base = swapper_pg_dir;
2488550eb99SIngo Molnar 	int pgd_idx, pmd_idx, pte_ofs;
249ad757b6aSThomas Gleixner 	unsigned long pfn;
250ad757b6aSThomas Gleixner 	pgd_t *pgd;
251ad757b6aSThomas Gleixner 	pmd_t *pmd;
252ad757b6aSThomas Gleixner 	pte_t *pte;
253a2699e47SSuresh Siddha 	unsigned pages_2m, pages_4k;
254a2699e47SSuresh Siddha 	int mapping_iter;
255a2699e47SSuresh Siddha 
256e53fb04fSPekka Enberg 	start_pfn = start >> PAGE_SHIFT;
257e53fb04fSPekka Enberg 	end_pfn = end >> PAGE_SHIFT;
258e53fb04fSPekka Enberg 
259a2699e47SSuresh Siddha 	/*
260a2699e47SSuresh Siddha 	 * First iteration will setup identity mapping using large/small pages
261a2699e47SSuresh Siddha 	 * based on use_pse, with other attributes same as set by
262a2699e47SSuresh Siddha 	 * the early code in head_32.S
263a2699e47SSuresh Siddha 	 *
264a2699e47SSuresh Siddha 	 * Second iteration will setup the appropriate attributes (NX, GLOBAL..)
265a2699e47SSuresh Siddha 	 * as desired for the kernel identity mapping.
266a2699e47SSuresh Siddha 	 *
267a2699e47SSuresh Siddha 	 * This two pass mechanism conforms to the TLB app note which says:
268a2699e47SSuresh Siddha 	 *
269a2699e47SSuresh Siddha 	 *     "Software should not write to a paging-structure entry in a way
270a2699e47SSuresh Siddha 	 *      that would change, for any linear address, both the page size
271a2699e47SSuresh Siddha 	 *      and either the page frame or attributes."
272a2699e47SSuresh Siddha 	 */
273a2699e47SSuresh Siddha 	mapping_iter = 1;
274ad757b6aSThomas Gleixner 
275a04ad82dSYinghai Lu 	if (!cpu_has_pse)
276a04ad82dSYinghai Lu 		use_pse = 0;
277a04ad82dSYinghai Lu 
278a2699e47SSuresh Siddha repeat:
279a2699e47SSuresh Siddha 	pages_2m = pages_4k = 0;
280a04ad82dSYinghai Lu 	pfn = start_pfn;
281a04ad82dSYinghai Lu 	pgd_idx = pgd_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
282ad757b6aSThomas Gleixner 	pgd = pgd_base + pgd_idx;
283ad757b6aSThomas Gleixner 	for (; pgd_idx < PTRS_PER_PGD; pgd++, pgd_idx++) {
284ad757b6aSThomas Gleixner 		pmd = one_md_table_init(pgd);
2858550eb99SIngo Molnar 
286a04ad82dSYinghai Lu 		if (pfn >= end_pfn)
287a04ad82dSYinghai Lu 			continue;
288a04ad82dSYinghai Lu #ifdef CONFIG_X86_PAE
289a04ad82dSYinghai Lu 		pmd_idx = pmd_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
290a04ad82dSYinghai Lu 		pmd += pmd_idx;
291a04ad82dSYinghai Lu #else
292a04ad82dSYinghai Lu 		pmd_idx = 0;
293a04ad82dSYinghai Lu #endif
294a04ad82dSYinghai Lu 		for (; pmd_idx < PTRS_PER_PMD && pfn < end_pfn;
295f3f20de8SJeremy Fitzhardinge 		     pmd++, pmd_idx++) {
2968550eb99SIngo Molnar 			unsigned int addr = pfn * PAGE_SIZE + PAGE_OFFSET;
297ad757b6aSThomas Gleixner 
2988550eb99SIngo Molnar 			/*
2998550eb99SIngo Molnar 			 * Map with big pages if possible, otherwise
3008550eb99SIngo Molnar 			 * create normal page tables:
3018550eb99SIngo Molnar 			 */
302a04ad82dSYinghai Lu 			if (use_pse) {
3038550eb99SIngo Molnar 				unsigned int addr2;
304f3f20de8SJeremy Fitzhardinge 				pgprot_t prot = PAGE_KERNEL_LARGE;
305a2699e47SSuresh Siddha 				/*
306a2699e47SSuresh Siddha 				 * first pass will use the same initial
307a2699e47SSuresh Siddha 				 * identity mapping attribute + _PAGE_PSE.
308a2699e47SSuresh Siddha 				 */
309a2699e47SSuresh Siddha 				pgprot_t init_prot =
310a2699e47SSuresh Siddha 					__pgprot(PTE_IDENT_ATTR |
311a2699e47SSuresh Siddha 						 _PAGE_PSE);
312f3f20de8SJeremy Fitzhardinge 
3138550eb99SIngo Molnar 				addr2 = (pfn + PTRS_PER_PTE-1) * PAGE_SIZE +
314f3f20de8SJeremy Fitzhardinge 					PAGE_OFFSET + PAGE_SIZE-1;
315f3f20de8SJeremy Fitzhardinge 
3168550eb99SIngo Molnar 				if (is_kernel_text(addr) ||
3178550eb99SIngo Molnar 				    is_kernel_text(addr2))
318f3f20de8SJeremy Fitzhardinge 					prot = PAGE_KERNEL_LARGE_EXEC;
319f3f20de8SJeremy Fitzhardinge 
320ce0c0e50SAndi Kleen 				pages_2m++;
321a2699e47SSuresh Siddha 				if (mapping_iter == 1)
322a2699e47SSuresh Siddha 					set_pmd(pmd, pfn_pmd(pfn, init_prot));
323a2699e47SSuresh Siddha 				else
324f3f20de8SJeremy Fitzhardinge 					set_pmd(pmd, pfn_pmd(pfn, prot));
325ad757b6aSThomas Gleixner 
326ad757b6aSThomas Gleixner 				pfn += PTRS_PER_PTE;
3278550eb99SIngo Molnar 				continue;
3288550eb99SIngo Molnar 			}
329ad757b6aSThomas Gleixner 			pte = one_page_table_init(pmd);
330ad757b6aSThomas Gleixner 
331a04ad82dSYinghai Lu 			pte_ofs = pte_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
332a04ad82dSYinghai Lu 			pte += pte_ofs;
333a04ad82dSYinghai Lu 			for (; pte_ofs < PTRS_PER_PTE && pfn < end_pfn;
3348550eb99SIngo Molnar 			     pte++, pfn++, pte_ofs++, addr += PAGE_SIZE) {
335f3f20de8SJeremy Fitzhardinge 				pgprot_t prot = PAGE_KERNEL;
336a2699e47SSuresh Siddha 				/*
337a2699e47SSuresh Siddha 				 * first pass will use the same initial
338a2699e47SSuresh Siddha 				 * identity mapping attribute.
339a2699e47SSuresh Siddha 				 */
340a2699e47SSuresh Siddha 				pgprot_t init_prot = __pgprot(PTE_IDENT_ATTR);
341f3f20de8SJeremy Fitzhardinge 
3428550eb99SIngo Molnar 				if (is_kernel_text(addr))
343f3f20de8SJeremy Fitzhardinge 					prot = PAGE_KERNEL_EXEC;
344f3f20de8SJeremy Fitzhardinge 
345ce0c0e50SAndi Kleen 				pages_4k++;
346c1fd1b43SPekka Enberg 				if (mapping_iter == 1) {
347a2699e47SSuresh Siddha 					set_pte(pte, pfn_pte(pfn, init_prot));
348c1fd1b43SPekka Enberg 					last_map_addr = (pfn << PAGE_SHIFT) + PAGE_SIZE;
349c1fd1b43SPekka Enberg 				} else
350f3f20de8SJeremy Fitzhardinge 					set_pte(pte, pfn_pte(pfn, prot));
351ad757b6aSThomas Gleixner 			}
352ad757b6aSThomas Gleixner 		}
353ad757b6aSThomas Gleixner 	}
354a2699e47SSuresh Siddha 	if (mapping_iter == 1) {
355a2699e47SSuresh Siddha 		/*
356a2699e47SSuresh Siddha 		 * update direct mapping page count only in the first
357a2699e47SSuresh Siddha 		 * iteration.
358a2699e47SSuresh Siddha 		 */
359ce0c0e50SAndi Kleen 		update_page_count(PG_LEVEL_2M, pages_2m);
360ce0c0e50SAndi Kleen 		update_page_count(PG_LEVEL_4K, pages_4k);
361a2699e47SSuresh Siddha 
362a2699e47SSuresh Siddha 		/*
363a2699e47SSuresh Siddha 		 * local global flush tlb, which will flush the previous
364a2699e47SSuresh Siddha 		 * mappings present in both small and large page TLB's.
365a2699e47SSuresh Siddha 		 */
366a2699e47SSuresh Siddha 		__flush_tlb_all();
367a2699e47SSuresh Siddha 
368a2699e47SSuresh Siddha 		/*
369a2699e47SSuresh Siddha 		 * Second iteration will set the actual desired PTE attributes.
370a2699e47SSuresh Siddha 		 */
371a2699e47SSuresh Siddha 		mapping_iter = 2;
372a2699e47SSuresh Siddha 		goto repeat;
373a2699e47SSuresh Siddha 	}
374c1fd1b43SPekka Enberg 	return last_map_addr;
375ae531c26SArjan van de Ven }
376ae531c26SArjan van de Ven 
377ad757b6aSThomas Gleixner pte_t *kmap_pte;
378ad757b6aSThomas Gleixner pgprot_t kmap_prot;
379ad757b6aSThomas Gleixner 
3808550eb99SIngo Molnar static inline pte_t *kmap_get_fixmap_pte(unsigned long vaddr)
3818550eb99SIngo Molnar {
3828550eb99SIngo Molnar 	return pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k(vaddr),
3838550eb99SIngo Molnar 			vaddr), vaddr), vaddr);
3848550eb99SIngo Molnar }
385ad757b6aSThomas Gleixner 
386ad757b6aSThomas Gleixner static void __init kmap_init(void)
387ad757b6aSThomas Gleixner {
388ad757b6aSThomas Gleixner 	unsigned long kmap_vstart;
389ad757b6aSThomas Gleixner 
3908550eb99SIngo Molnar 	/*
3918550eb99SIngo Molnar 	 * Cache the first kmap pte:
3928550eb99SIngo Molnar 	 */
393ad757b6aSThomas Gleixner 	kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN);
394ad757b6aSThomas Gleixner 	kmap_pte = kmap_get_fixmap_pte(kmap_vstart);
395ad757b6aSThomas Gleixner 
396ad757b6aSThomas Gleixner 	kmap_prot = PAGE_KERNEL;
397ad757b6aSThomas Gleixner }
398ad757b6aSThomas Gleixner 
399fd940934SKeith Packard #ifdef CONFIG_HIGHMEM
400ad757b6aSThomas Gleixner static void __init permanent_kmaps_init(pgd_t *pgd_base)
401ad757b6aSThomas Gleixner {
4028550eb99SIngo Molnar 	unsigned long vaddr;
403ad757b6aSThomas Gleixner 	pgd_t *pgd;
404ad757b6aSThomas Gleixner 	pud_t *pud;
405ad757b6aSThomas Gleixner 	pmd_t *pmd;
406ad757b6aSThomas Gleixner 	pte_t *pte;
407ad757b6aSThomas Gleixner 
408ad757b6aSThomas Gleixner 	vaddr = PKMAP_BASE;
409ad757b6aSThomas Gleixner 	page_table_range_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, pgd_base);
410ad757b6aSThomas Gleixner 
411ad757b6aSThomas Gleixner 	pgd = swapper_pg_dir + pgd_index(vaddr);
412ad757b6aSThomas Gleixner 	pud = pud_offset(pgd, vaddr);
413ad757b6aSThomas Gleixner 	pmd = pmd_offset(pud, vaddr);
414ad757b6aSThomas Gleixner 	pte = pte_offset_kernel(pmd, vaddr);
415ad757b6aSThomas Gleixner 	pkmap_page_table = pte;
416ad757b6aSThomas Gleixner }
417ad757b6aSThomas Gleixner 
418b1258ac2SMinchan Kim static void __init add_one_highpage_init(struct page *page)
419ad757b6aSThomas Gleixner {
420ad757b6aSThomas Gleixner 	ClearPageReserved(page);
421180c06efSJeremy Fitzhardinge 	init_page_count(page);
422180c06efSJeremy Fitzhardinge 	__free_page(page);
423180c06efSJeremy Fitzhardinge 	totalhigh_pages++;
424ad757b6aSThomas Gleixner }
425ad757b6aSThomas Gleixner 
426*1d931264SYinghai Lu void __init add_highpages_with_active_regions(int nid,
427*1d931264SYinghai Lu 			 unsigned long start_pfn, unsigned long end_pfn)
428b5bc6c0eSYinghai Lu {
429*1d931264SYinghai Lu 	struct range *range;
430*1d931264SYinghai Lu 	int nr_range;
431*1d931264SYinghai Lu 	int i;
432*1d931264SYinghai Lu 
433*1d931264SYinghai Lu 	nr_range = __get_free_all_memory_range(&range, nid, start_pfn, end_pfn);
434*1d931264SYinghai Lu 
435*1d931264SYinghai Lu 	for (i = 0; i < nr_range; i++) {
436b5bc6c0eSYinghai Lu 		struct page *page;
437*1d931264SYinghai Lu 		int node_pfn;
438b5bc6c0eSYinghai Lu 
439*1d931264SYinghai Lu 		for (node_pfn = range[i].start; node_pfn < range[i].end;
440b5bc6c0eSYinghai Lu 		     node_pfn++) {
441b5bc6c0eSYinghai Lu 			if (!pfn_valid(node_pfn))
442b5bc6c0eSYinghai Lu 				continue;
443b5bc6c0eSYinghai Lu 			page = pfn_to_page(node_pfn);
444b1258ac2SMinchan Kim 			add_one_highpage_init(page);
445b5bc6c0eSYinghai Lu 		}
446b5bc6c0eSYinghai Lu 	}
447ad757b6aSThomas Gleixner }
448ad757b6aSThomas Gleixner #else
449e8e32326SIngo Brueckl static inline void permanent_kmaps_init(pgd_t *pgd_base)
450e8e32326SIngo Brueckl {
451e8e32326SIngo Brueckl }
452ad757b6aSThomas Gleixner #endif /* CONFIG_HIGHMEM */
453ad757b6aSThomas Gleixner 
454ad757b6aSThomas Gleixner void __init native_pagetable_setup_start(pgd_t *base)
455ad757b6aSThomas Gleixner {
456551889a6SIan Campbell 	unsigned long pfn, va;
457551889a6SIan Campbell 	pgd_t *pgd;
458551889a6SIan Campbell 	pud_t *pud;
459551889a6SIan Campbell 	pmd_t *pmd;
460551889a6SIan Campbell 	pte_t *pte;
461ad757b6aSThomas Gleixner 
462ad757b6aSThomas Gleixner 	/*
463551889a6SIan Campbell 	 * Remove any mappings which extend past the end of physical
464551889a6SIan Campbell 	 * memory from the boot time page table:
465ad757b6aSThomas Gleixner 	 */
466551889a6SIan Campbell 	for (pfn = max_low_pfn + 1; pfn < 1<<(32-PAGE_SHIFT); pfn++) {
467551889a6SIan Campbell 		va = PAGE_OFFSET + (pfn<<PAGE_SHIFT);
468551889a6SIan Campbell 		pgd = base + pgd_index(va);
469551889a6SIan Campbell 		if (!pgd_present(*pgd))
470551889a6SIan Campbell 			break;
471ad757b6aSThomas Gleixner 
472551889a6SIan Campbell 		pud = pud_offset(pgd, va);
473551889a6SIan Campbell 		pmd = pmd_offset(pud, va);
474551889a6SIan Campbell 		if (!pmd_present(*pmd))
475551889a6SIan Campbell 			break;
476551889a6SIan Campbell 
477551889a6SIan Campbell 		pte = pte_offset_kernel(pmd, va);
478551889a6SIan Campbell 		if (!pte_present(*pte))
479551889a6SIan Campbell 			break;
480551889a6SIan Campbell 
481551889a6SIan Campbell 		pte_clear(NULL, va, pte);
482551889a6SIan Campbell 	}
4836944a9c8SJeremy Fitzhardinge 	paravirt_alloc_pmd(&init_mm, __pa(base) >> PAGE_SHIFT);
484ad757b6aSThomas Gleixner }
485ad757b6aSThomas Gleixner 
486ad757b6aSThomas Gleixner void __init native_pagetable_setup_done(pgd_t *base)
487ad757b6aSThomas Gleixner {
488ad757b6aSThomas Gleixner }
489ad757b6aSThomas Gleixner 
490ad757b6aSThomas Gleixner /*
491ad757b6aSThomas Gleixner  * Build a proper pagetable for the kernel mappings.  Up until this
492ad757b6aSThomas Gleixner  * point, we've been running on some set of pagetables constructed by
493ad757b6aSThomas Gleixner  * the boot process.
494ad757b6aSThomas Gleixner  *
495ad757b6aSThomas Gleixner  * If we're booting on native hardware, this will be a pagetable
496551889a6SIan Campbell  * constructed in arch/x86/kernel/head_32.S.  The root of the
497551889a6SIan Campbell  * pagetable will be swapper_pg_dir.
498ad757b6aSThomas Gleixner  *
499ad757b6aSThomas Gleixner  * If we're booting paravirtualized under a hypervisor, then there are
500ad757b6aSThomas Gleixner  * more options: we may already be running PAE, and the pagetable may
501ad757b6aSThomas Gleixner  * or may not be based in swapper_pg_dir.  In any case,
502ad757b6aSThomas Gleixner  * paravirt_pagetable_setup_start() will set up swapper_pg_dir
503ad757b6aSThomas Gleixner  * appropriately for the rest of the initialization to work.
504ad757b6aSThomas Gleixner  *
505ad757b6aSThomas Gleixner  * In general, pagetable_init() assumes that the pagetable may already
506ad757b6aSThomas Gleixner  * be partially populated, and so it avoids stomping on any existing
507ad757b6aSThomas Gleixner  * mappings.
508ad757b6aSThomas Gleixner  */
509f765090aSPekka Enberg void __init early_ioremap_page_table_range_init(void)
510ad757b6aSThomas Gleixner {
511e7179853SPekka Enberg 	pgd_t *pgd_base = swapper_pg_dir;
5128550eb99SIngo Molnar 	unsigned long vaddr, end;
513ad757b6aSThomas Gleixner 
514ad757b6aSThomas Gleixner 	/*
515ad757b6aSThomas Gleixner 	 * Fixed mappings, only the page table structure has to be
516ad757b6aSThomas Gleixner 	 * created - mappings will be set by set_fixmap():
517ad757b6aSThomas Gleixner 	 */
518ad757b6aSThomas Gleixner 	vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
519ad757b6aSThomas Gleixner 	end = (FIXADDR_TOP + PMD_SIZE - 1) & PMD_MASK;
520ad757b6aSThomas Gleixner 	page_table_range_init(vaddr, end, pgd_base);
521beacfaacSHuang, Ying 	early_ioremap_reset();
522e7b37895SYinghai Lu }
523e7b37895SYinghai Lu 
524e7b37895SYinghai Lu static void __init pagetable_init(void)
525e7b37895SYinghai Lu {
526e7b37895SYinghai Lu 	pgd_t *pgd_base = swapper_pg_dir;
527e7b37895SYinghai Lu 
528ad757b6aSThomas Gleixner 	permanent_kmaps_init(pgd_base);
529ad757b6aSThomas Gleixner }
530ad757b6aSThomas Gleixner 
531a6eb84bcSRafael J. Wysocki #ifdef CONFIG_ACPI_SLEEP
532ad757b6aSThomas Gleixner /*
533a6eb84bcSRafael J. Wysocki  * ACPI suspend needs this for resume, because things like the intel-agp
534ad757b6aSThomas Gleixner  * driver might have split up a kernel 4MB mapping.
535ad757b6aSThomas Gleixner  */
536a6eb84bcSRafael J. Wysocki char swsusp_pg_dir[PAGE_SIZE]
537ad757b6aSThomas Gleixner 	__attribute__ ((aligned(PAGE_SIZE)));
538ad757b6aSThomas Gleixner 
539ad757b6aSThomas Gleixner static inline void save_pg_dir(void)
540ad757b6aSThomas Gleixner {
541ad757b6aSThomas Gleixner 	memcpy(swsusp_pg_dir, swapper_pg_dir, PAGE_SIZE);
542ad757b6aSThomas Gleixner }
543a6eb84bcSRafael J. Wysocki #else /* !CONFIG_ACPI_SLEEP */
544ad757b6aSThomas Gleixner static inline void save_pg_dir(void)
545ad757b6aSThomas Gleixner {
546ad757b6aSThomas Gleixner }
547a6eb84bcSRafael J. Wysocki #endif /* !CONFIG_ACPI_SLEEP */
548ad757b6aSThomas Gleixner 
54955cd6367SYinghai Lu void zap_low_mappings(bool early)
550ad757b6aSThomas Gleixner {
551ad757b6aSThomas Gleixner 	int i;
552ad757b6aSThomas Gleixner 
553ad757b6aSThomas Gleixner 	/*
554ad757b6aSThomas Gleixner 	 * Zap initial low-memory mappings.
555ad757b6aSThomas Gleixner 	 *
556ad757b6aSThomas Gleixner 	 * Note that "pgd_clear()" doesn't do it for
557ad757b6aSThomas Gleixner 	 * us, because pgd_clear() is a no-op on i386.
558ad757b6aSThomas Gleixner 	 */
55968db065cSJeremy Fitzhardinge 	for (i = 0; i < KERNEL_PGD_BOUNDARY; i++) {
560ad757b6aSThomas Gleixner #ifdef CONFIG_X86_PAE
561ad757b6aSThomas Gleixner 		set_pgd(swapper_pg_dir+i, __pgd(1 + __pa(empty_zero_page)));
562ad757b6aSThomas Gleixner #else
563ad757b6aSThomas Gleixner 		set_pgd(swapper_pg_dir+i, __pgd(0));
564ad757b6aSThomas Gleixner #endif
5658550eb99SIngo Molnar 	}
56655cd6367SYinghai Lu 
56755cd6367SYinghai Lu 	if (early)
56855cd6367SYinghai Lu 		__flush_tlb();
56955cd6367SYinghai Lu 	else
570ad757b6aSThomas Gleixner 		flush_tlb_all();
571ad757b6aSThomas Gleixner }
572ad757b6aSThomas Gleixner 
573be43d728SJeremy Fitzhardinge pteval_t __supported_pte_mask __read_mostly = ~(_PAGE_NX | _PAGE_GLOBAL | _PAGE_IOMAP);
5746fdc05d4SJeremy Fitzhardinge EXPORT_SYMBOL_GPL(__supported_pte_mask);
5756fdc05d4SJeremy Fitzhardinge 
57690d967e0SYinghai Lu /* user-defined highmem size */
57790d967e0SYinghai Lu static unsigned int highmem_pages = -1;
57890d967e0SYinghai Lu 
57990d967e0SYinghai Lu /*
58090d967e0SYinghai Lu  * highmem=size forces highmem to be exactly 'size' bytes.
58190d967e0SYinghai Lu  * This works even on boxes that have no highmem otherwise.
58290d967e0SYinghai Lu  * This also works to reduce highmem size on bigger boxes.
58390d967e0SYinghai Lu  */
58490d967e0SYinghai Lu static int __init parse_highmem(char *arg)
58590d967e0SYinghai Lu {
58690d967e0SYinghai Lu 	if (!arg)
58790d967e0SYinghai Lu 		return -EINVAL;
58890d967e0SYinghai Lu 
58990d967e0SYinghai Lu 	highmem_pages = memparse(arg, &arg) >> PAGE_SHIFT;
59090d967e0SYinghai Lu 	return 0;
59190d967e0SYinghai Lu }
59290d967e0SYinghai Lu early_param("highmem", parse_highmem);
59390d967e0SYinghai Lu 
5944769843bSIngo Molnar #define MSG_HIGHMEM_TOO_BIG \
5954769843bSIngo Molnar 	"highmem size (%luMB) is bigger than pages available (%luMB)!\n"
5964769843bSIngo Molnar 
5974769843bSIngo Molnar #define MSG_LOWMEM_TOO_SMALL \
5984769843bSIngo Molnar 	"highmem size (%luMB) results in <64MB lowmem, ignoring it!\n"
59990d967e0SYinghai Lu /*
6004769843bSIngo Molnar  * All of RAM fits into lowmem - but if user wants highmem
6014769843bSIngo Molnar  * artificially via the highmem=x boot parameter then create
6024769843bSIngo Molnar  * it:
60390d967e0SYinghai Lu  */
6044769843bSIngo Molnar void __init lowmem_pfn_init(void)
60590d967e0SYinghai Lu {
606346cafecSYinghai Lu 	/* max_low_pfn is 0, we already have early_res support */
60790d967e0SYinghai Lu 	max_low_pfn = max_pfn;
608d88316c2SIngo Molnar 
6094769843bSIngo Molnar 	if (highmem_pages == -1)
6104769843bSIngo Molnar 		highmem_pages = 0;
6114769843bSIngo Molnar #ifdef CONFIG_HIGHMEM
6124769843bSIngo Molnar 	if (highmem_pages >= max_pfn) {
6134769843bSIngo Molnar 		printk(KERN_ERR MSG_HIGHMEM_TOO_BIG,
6144769843bSIngo Molnar 			pages_to_mb(highmem_pages), pages_to_mb(max_pfn));
6154769843bSIngo Molnar 		highmem_pages = 0;
6164769843bSIngo Molnar 	}
6174769843bSIngo Molnar 	if (highmem_pages) {
6184769843bSIngo Molnar 		if (max_low_pfn - highmem_pages < 64*1024*1024/PAGE_SIZE) {
6194769843bSIngo Molnar 			printk(KERN_ERR MSG_LOWMEM_TOO_SMALL,
6204769843bSIngo Molnar 				pages_to_mb(highmem_pages));
6214769843bSIngo Molnar 			highmem_pages = 0;
6224769843bSIngo Molnar 		}
6234769843bSIngo Molnar 		max_low_pfn -= highmem_pages;
6244769843bSIngo Molnar 	}
6254769843bSIngo Molnar #else
6264769843bSIngo Molnar 	if (highmem_pages)
6274769843bSIngo Molnar 		printk(KERN_ERR "ignoring highmem size on non-highmem kernel!\n");
6284769843bSIngo Molnar #endif
6294769843bSIngo Molnar }
6304769843bSIngo Molnar 
6314769843bSIngo Molnar #define MSG_HIGHMEM_TOO_SMALL \
6324769843bSIngo Molnar 	"only %luMB highmem pages available, ignoring highmem size of %luMB!\n"
6334769843bSIngo Molnar 
6344769843bSIngo Molnar #define MSG_HIGHMEM_TRIMMED \
6354769843bSIngo Molnar 	"Warning: only 4GB will be used. Use a HIGHMEM64G enabled kernel!\n"
6364769843bSIngo Molnar /*
6374769843bSIngo Molnar  * We have more RAM than fits into lowmem - we try to put it into
6384769843bSIngo Molnar  * highmem, also taking the highmem=x boot parameter into account:
6394769843bSIngo Molnar  */
6404769843bSIngo Molnar void __init highmem_pfn_init(void)
6414769843bSIngo Molnar {
642d88316c2SIngo Molnar 	max_low_pfn = MAXMEM_PFN;
643d88316c2SIngo Molnar 
64490d967e0SYinghai Lu 	if (highmem_pages == -1)
64590d967e0SYinghai Lu 		highmem_pages = max_pfn - MAXMEM_PFN;
6464769843bSIngo Molnar 
64790d967e0SYinghai Lu 	if (highmem_pages + MAXMEM_PFN < max_pfn)
64890d967e0SYinghai Lu 		max_pfn = MAXMEM_PFN + highmem_pages;
6494769843bSIngo Molnar 
65090d967e0SYinghai Lu 	if (highmem_pages + MAXMEM_PFN > max_pfn) {
6514769843bSIngo Molnar 		printk(KERN_WARNING MSG_HIGHMEM_TOO_SMALL,
65290d967e0SYinghai Lu 			pages_to_mb(max_pfn - MAXMEM_PFN),
65390d967e0SYinghai Lu 			pages_to_mb(highmem_pages));
65490d967e0SYinghai Lu 		highmem_pages = 0;
65590d967e0SYinghai Lu 	}
65690d967e0SYinghai Lu #ifndef CONFIG_HIGHMEM
65790d967e0SYinghai Lu 	/* Maximum memory usable is what is directly addressable */
6584769843bSIngo Molnar 	printk(KERN_WARNING "Warning only %ldMB will be used.\n", MAXMEM>>20);
65990d967e0SYinghai Lu 	if (max_pfn > MAX_NONPAE_PFN)
6604769843bSIngo Molnar 		printk(KERN_WARNING "Use a HIGHMEM64G enabled kernel.\n");
66190d967e0SYinghai Lu 	else
66290d967e0SYinghai Lu 		printk(KERN_WARNING "Use a HIGHMEM enabled kernel.\n");
66390d967e0SYinghai Lu 	max_pfn = MAXMEM_PFN;
66490d967e0SYinghai Lu #else /* !CONFIG_HIGHMEM */
66590d967e0SYinghai Lu #ifndef CONFIG_HIGHMEM64G
66690d967e0SYinghai Lu 	if (max_pfn > MAX_NONPAE_PFN) {
66790d967e0SYinghai Lu 		max_pfn = MAX_NONPAE_PFN;
6684769843bSIngo Molnar 		printk(KERN_WARNING MSG_HIGHMEM_TRIMMED);
66990d967e0SYinghai Lu 	}
67090d967e0SYinghai Lu #endif /* !CONFIG_HIGHMEM64G */
67190d967e0SYinghai Lu #endif /* !CONFIG_HIGHMEM */
67290d967e0SYinghai Lu }
6734769843bSIngo Molnar 
67490d967e0SYinghai Lu /*
67590d967e0SYinghai Lu  * Determine low and high memory ranges:
67690d967e0SYinghai Lu  */
67790d967e0SYinghai Lu void __init find_low_pfn_range(void)
67890d967e0SYinghai Lu {
67990d967e0SYinghai Lu 	/* it could update max_pfn */
68090d967e0SYinghai Lu 
681d88316c2SIngo Molnar 	if (max_pfn <= MAXMEM_PFN)
6824769843bSIngo Molnar 		lowmem_pfn_init();
683d88316c2SIngo Molnar 	else
684d88316c2SIngo Molnar 		highmem_pfn_init();
68590d967e0SYinghai Lu }
68690d967e0SYinghai Lu 
687b2ac82a0SYinghai Lu #ifndef CONFIG_NEED_MULTIPLE_NODES
6888ee2debcSDavid Rientjes void __init initmem_init(unsigned long start_pfn, unsigned long end_pfn,
6898ee2debcSDavid Rientjes 				int acpi, int k8)
690b2ac82a0SYinghai Lu {
691b2ac82a0SYinghai Lu #ifdef CONFIG_HIGHMEM
692b2ac82a0SYinghai Lu 	highstart_pfn = highend_pfn = max_pfn;
693b2ac82a0SYinghai Lu 	if (max_pfn > max_low_pfn)
694b2ac82a0SYinghai Lu 		highstart_pfn = max_low_pfn;
695a9ce6bc1SYinghai Lu 	memblock_x86_register_active_regions(0, 0, highend_pfn);
696ed077b58SShaohua Li 	sparse_memory_present_with_active_regions(0);
697b2ac82a0SYinghai Lu 	printk(KERN_NOTICE "%ldMB HIGHMEM available.\n",
698b2ac82a0SYinghai Lu 		pages_to_mb(highend_pfn - highstart_pfn));
699b2ac82a0SYinghai Lu 	num_physpages = highend_pfn;
700b2ac82a0SYinghai Lu 	high_memory = (void *) __va(highstart_pfn * PAGE_SIZE - 1) + 1;
701b2ac82a0SYinghai Lu #else
702a9ce6bc1SYinghai Lu 	memblock_x86_register_active_regions(0, 0, max_low_pfn);
703ed077b58SShaohua Li 	sparse_memory_present_with_active_regions(0);
704b2ac82a0SYinghai Lu 	num_physpages = max_low_pfn;
705b2ac82a0SYinghai Lu 	high_memory = (void *) __va(max_low_pfn * PAGE_SIZE - 1) + 1;
706b2ac82a0SYinghai Lu #endif
707b2ac82a0SYinghai Lu #ifdef CONFIG_FLATMEM
708b2ac82a0SYinghai Lu 	max_mapnr = num_physpages;
709b2ac82a0SYinghai Lu #endif
710dc16ecf7SJeremy Fitzhardinge 	__vmalloc_start_set = true;
711dc16ecf7SJeremy Fitzhardinge 
712b2ac82a0SYinghai Lu 	printk(KERN_NOTICE "%ldMB LOWMEM available.\n",
713b2ac82a0SYinghai Lu 			pages_to_mb(max_low_pfn));
714b2ac82a0SYinghai Lu 
715b2ac82a0SYinghai Lu 	setup_bootmem_allocator();
716b2ac82a0SYinghai Lu }
717cb95a13aSYinghai Lu #endif /* !CONFIG_NEED_MULTIPLE_NODES */
718b2ac82a0SYinghai Lu 
719cb95a13aSYinghai Lu static void __init zone_sizes_init(void)
720b2ac82a0SYinghai Lu {
721b2ac82a0SYinghai Lu 	unsigned long max_zone_pfns[MAX_NR_ZONES];
722b2ac82a0SYinghai Lu 	memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
723b2ac82a0SYinghai Lu 	max_zone_pfns[ZONE_DMA] =
724b2ac82a0SYinghai Lu 		virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
725b2ac82a0SYinghai Lu 	max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
726b2ac82a0SYinghai Lu #ifdef CONFIG_HIGHMEM
727b2ac82a0SYinghai Lu 	max_zone_pfns[ZONE_HIGHMEM] = highend_pfn;
728b2ac82a0SYinghai Lu #endif
729b2ac82a0SYinghai Lu 
730b2ac82a0SYinghai Lu 	free_area_init_nodes(max_zone_pfns);
731b2ac82a0SYinghai Lu }
732b2ac82a0SYinghai Lu 
733b2ac82a0SYinghai Lu void __init setup_bootmem_allocator(void)
734b2ac82a0SYinghai Lu {
735b2ac82a0SYinghai Lu 	printk(KERN_INFO "  mapped low ram: 0 - %08lx\n",
736b2ac82a0SYinghai Lu 		 max_pfn_mapped<<PAGE_SHIFT);
737fc5efe39SYinghai Lu 	printk(KERN_INFO "  low ram: 0 - %08lx\n", max_low_pfn<<PAGE_SHIFT);
738b2ac82a0SYinghai Lu 
739c464573cSPekka Enberg 	after_bootmem = 1;
7404e29684cSYinghai Lu }
7414e29684cSYinghai Lu 
742a04ad82dSYinghai Lu /*
743ad757b6aSThomas Gleixner  * paging_init() sets up the page tables - note that the first 8MB are
744ad757b6aSThomas Gleixner  * already mapped by head.S.
745ad757b6aSThomas Gleixner  *
746ad757b6aSThomas Gleixner  * This routines also unmaps the page at virtual kernel address 0, so
747ad757b6aSThomas Gleixner  * that we can trap those pesky NULL-reference errors in the kernel.
748ad757b6aSThomas Gleixner  */
749ad757b6aSThomas Gleixner void __init paging_init(void)
750ad757b6aSThomas Gleixner {
751ad757b6aSThomas Gleixner 	pagetable_init();
752ad757b6aSThomas Gleixner 
753ad757b6aSThomas Gleixner 	__flush_tlb_all();
754ad757b6aSThomas Gleixner 
755ad757b6aSThomas Gleixner 	kmap_init();
75611cd0bc1SYinghai Lu 
75711cd0bc1SYinghai Lu 	/*
75811cd0bc1SYinghai Lu 	 * NOTE: at this point the bootmem allocator is fully available.
75911cd0bc1SYinghai Lu 	 */
76011cd0bc1SYinghai Lu 	sparse_init();
76111cd0bc1SYinghai Lu 	zone_sizes_init();
762ad757b6aSThomas Gleixner }
763ad757b6aSThomas Gleixner 
764ad757b6aSThomas Gleixner /*
765ad757b6aSThomas Gleixner  * Test if the WP bit works in supervisor mode. It isn't supported on 386's
766f7f17a67SDmitri Vorobiev  * and also on some strange 486's. All 586+'s are OK. This used to involve
767f7f17a67SDmitri Vorobiev  * black magic jumps to work around some nasty CPU bugs, but fortunately the
768f7f17a67SDmitri Vorobiev  * switch to using exceptions got rid of all that.
769ad757b6aSThomas Gleixner  */
770ad757b6aSThomas Gleixner static void __init test_wp_bit(void)
771ad757b6aSThomas Gleixner {
772d7d119d7SIngo Molnar 	printk(KERN_INFO
773d7d119d7SIngo Molnar   "Checking if this processor honours the WP bit even in supervisor mode...");
774ad757b6aSThomas Gleixner 
775ad757b6aSThomas Gleixner 	/* Any page-aligned address will do, the test is non-destructive */
776ad757b6aSThomas Gleixner 	__set_fixmap(FIX_WP_TEST, __pa(&swapper_pg_dir), PAGE_READONLY);
777ad757b6aSThomas Gleixner 	boot_cpu_data.wp_works_ok = do_test_wp_bit();
778ad757b6aSThomas Gleixner 	clear_fixmap(FIX_WP_TEST);
779ad757b6aSThomas Gleixner 
780ad757b6aSThomas Gleixner 	if (!boot_cpu_data.wp_works_ok) {
781d7d119d7SIngo Molnar 		printk(KERN_CONT "No.\n");
782ad757b6aSThomas Gleixner #ifdef CONFIG_X86_WP_WORKS_OK
783d7d119d7SIngo Molnar 		panic(
784d7d119d7SIngo Molnar   "This kernel doesn't support CPU's with broken WP. Recompile it for a 386!");
785ad757b6aSThomas Gleixner #endif
786ad757b6aSThomas Gleixner 	} else {
787d7d119d7SIngo Molnar 		printk(KERN_CONT "Ok.\n");
788ad757b6aSThomas Gleixner 	}
789ad757b6aSThomas Gleixner }
790ad757b6aSThomas Gleixner 
791ad757b6aSThomas Gleixner void __init mem_init(void)
792ad757b6aSThomas Gleixner {
793ad757b6aSThomas Gleixner 	int codesize, reservedpages, datasize, initsize;
794cc9f7a0cSYinghai Lu 	int tmp;
795ad757b6aSThomas Gleixner 
796cfb80c9eSJeremy Fitzhardinge 	pci_iommu_alloc();
797cfb80c9eSJeremy Fitzhardinge 
798ad757b6aSThomas Gleixner #ifdef CONFIG_FLATMEM
799ad757b6aSThomas Gleixner 	BUG_ON(!mem_map);
800ad757b6aSThomas Gleixner #endif
801ad757b6aSThomas Gleixner 	/* this will put all low memory onto the freelists */
802ad757b6aSThomas Gleixner 	totalram_pages += free_all_bootmem();
803ad757b6aSThomas Gleixner 
804ad757b6aSThomas Gleixner 	reservedpages = 0;
805ad757b6aSThomas Gleixner 	for (tmp = 0; tmp < max_low_pfn; tmp++)
806ad757b6aSThomas Gleixner 		/*
8078550eb99SIngo Molnar 		 * Only count reserved RAM pages:
808ad757b6aSThomas Gleixner 		 */
809ad757b6aSThomas Gleixner 		if (page_is_ram(tmp) && PageReserved(pfn_to_page(tmp)))
810ad757b6aSThomas Gleixner 			reservedpages++;
811ad757b6aSThomas Gleixner 
812cc9f7a0cSYinghai Lu 	set_highmem_pages_init();
813ad757b6aSThomas Gleixner 
814ad757b6aSThomas Gleixner 	codesize =  (unsigned long) &_etext - (unsigned long) &_text;
815ad757b6aSThomas Gleixner 	datasize =  (unsigned long) &_edata - (unsigned long) &_etext;
816ad757b6aSThomas Gleixner 	initsize =  (unsigned long) &__init_end - (unsigned long) &__init_begin;
817ad757b6aSThomas Gleixner 
8188550eb99SIngo Molnar 	printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, "
8198550eb99SIngo Molnar 			"%dk reserved, %dk data, %dk init, %ldk highmem)\n",
820cc013a88SGeert Uytterhoeven 		nr_free_pages() << (PAGE_SHIFT-10),
821ad757b6aSThomas Gleixner 		num_physpages << (PAGE_SHIFT-10),
822ad757b6aSThomas Gleixner 		codesize >> 10,
823ad757b6aSThomas Gleixner 		reservedpages << (PAGE_SHIFT-10),
824ad757b6aSThomas Gleixner 		datasize >> 10,
825ad757b6aSThomas Gleixner 		initsize >> 10,
8264b529401SAndreas Fenkart 		totalhigh_pages << (PAGE_SHIFT-10));
827ad757b6aSThomas Gleixner 
828d7d119d7SIngo Molnar 	printk(KERN_INFO "virtual kernel memory layout:\n"
829ad757b6aSThomas Gleixner 		"    fixmap  : 0x%08lx - 0x%08lx   (%4ld kB)\n"
830ad757b6aSThomas Gleixner #ifdef CONFIG_HIGHMEM
831ad757b6aSThomas Gleixner 		"    pkmap   : 0x%08lx - 0x%08lx   (%4ld kB)\n"
832ad757b6aSThomas Gleixner #endif
833ad757b6aSThomas Gleixner 		"    vmalloc : 0x%08lx - 0x%08lx   (%4ld MB)\n"
834ad757b6aSThomas Gleixner 		"    lowmem  : 0x%08lx - 0x%08lx   (%4ld MB)\n"
835ad757b6aSThomas Gleixner 		"      .init : 0x%08lx - 0x%08lx   (%4ld kB)\n"
836ad757b6aSThomas Gleixner 		"      .data : 0x%08lx - 0x%08lx   (%4ld kB)\n"
837ad757b6aSThomas Gleixner 		"      .text : 0x%08lx - 0x%08lx   (%4ld kB)\n",
838ad757b6aSThomas Gleixner 		FIXADDR_START, FIXADDR_TOP,
839ad757b6aSThomas Gleixner 		(FIXADDR_TOP - FIXADDR_START) >> 10,
840ad757b6aSThomas Gleixner 
841ad757b6aSThomas Gleixner #ifdef CONFIG_HIGHMEM
842ad757b6aSThomas Gleixner 		PKMAP_BASE, PKMAP_BASE+LAST_PKMAP*PAGE_SIZE,
843ad757b6aSThomas Gleixner 		(LAST_PKMAP*PAGE_SIZE) >> 10,
844ad757b6aSThomas Gleixner #endif
845ad757b6aSThomas Gleixner 
846ad757b6aSThomas Gleixner 		VMALLOC_START, VMALLOC_END,
847ad757b6aSThomas Gleixner 		(VMALLOC_END - VMALLOC_START) >> 20,
848ad757b6aSThomas Gleixner 
849ad757b6aSThomas Gleixner 		(unsigned long)__va(0), (unsigned long)high_memory,
850ad757b6aSThomas Gleixner 		((unsigned long)high_memory - (unsigned long)__va(0)) >> 20,
851ad757b6aSThomas Gleixner 
852ad757b6aSThomas Gleixner 		(unsigned long)&__init_begin, (unsigned long)&__init_end,
8538550eb99SIngo Molnar 		((unsigned long)&__init_end -
8548550eb99SIngo Molnar 		 (unsigned long)&__init_begin) >> 10,
855ad757b6aSThomas Gleixner 
856ad757b6aSThomas Gleixner 		(unsigned long)&_etext, (unsigned long)&_edata,
857ad757b6aSThomas Gleixner 		((unsigned long)&_edata - (unsigned long)&_etext) >> 10,
858ad757b6aSThomas Gleixner 
859ad757b6aSThomas Gleixner 		(unsigned long)&_text, (unsigned long)&_etext,
860ad757b6aSThomas Gleixner 		((unsigned long)&_etext - (unsigned long)&_text) >> 10);
861ad757b6aSThomas Gleixner 
862beeb4195SJan Beulich 	/*
863beeb4195SJan Beulich 	 * Check boundaries twice: Some fundamental inconsistencies can
864beeb4195SJan Beulich 	 * be detected at build time already.
865beeb4195SJan Beulich 	 */
866beeb4195SJan Beulich #define __FIXADDR_TOP (-PAGE_SIZE)
867beeb4195SJan Beulich #ifdef CONFIG_HIGHMEM
868beeb4195SJan Beulich 	BUILD_BUG_ON(PKMAP_BASE + LAST_PKMAP*PAGE_SIZE	> FIXADDR_START);
869beeb4195SJan Beulich 	BUILD_BUG_ON(VMALLOC_END			> PKMAP_BASE);
870beeb4195SJan Beulich #endif
871beeb4195SJan Beulich #define high_memory (-128UL << 20)
872beeb4195SJan Beulich 	BUILD_BUG_ON(VMALLOC_START			>= VMALLOC_END);
873beeb4195SJan Beulich #undef high_memory
874beeb4195SJan Beulich #undef __FIXADDR_TOP
875beeb4195SJan Beulich 
876ad757b6aSThomas Gleixner #ifdef CONFIG_HIGHMEM
877ad757b6aSThomas Gleixner 	BUG_ON(PKMAP_BASE + LAST_PKMAP*PAGE_SIZE	> FIXADDR_START);
878ad757b6aSThomas Gleixner 	BUG_ON(VMALLOC_END				> PKMAP_BASE);
879ad757b6aSThomas Gleixner #endif
880beeb4195SJan Beulich 	BUG_ON(VMALLOC_START				>= VMALLOC_END);
881ad757b6aSThomas Gleixner 	BUG_ON((unsigned long)high_memory		> VMALLOC_START);
882ad757b6aSThomas Gleixner 
883ad757b6aSThomas Gleixner 	if (boot_cpu_data.wp_works_ok < 0)
884ad757b6aSThomas Gleixner 		test_wp_bit();
885ad757b6aSThomas Gleixner 
88661165d7aSHugh Dickins 	save_pg_dir();
88755cd6367SYinghai Lu 	zap_low_mappings(true);
888ad757b6aSThomas Gleixner }
889ad757b6aSThomas Gleixner 
890ad757b6aSThomas Gleixner #ifdef CONFIG_MEMORY_HOTPLUG
891ad757b6aSThomas Gleixner int arch_add_memory(int nid, u64 start, u64 size)
892ad757b6aSThomas Gleixner {
893ad757b6aSThomas Gleixner 	struct pglist_data *pgdata = NODE_DATA(nid);
894ad757b6aSThomas Gleixner 	struct zone *zone = pgdata->node_zones + ZONE_HIGHMEM;
895ad757b6aSThomas Gleixner 	unsigned long start_pfn = start >> PAGE_SHIFT;
896ad757b6aSThomas Gleixner 	unsigned long nr_pages = size >> PAGE_SHIFT;
897ad757b6aSThomas Gleixner 
898c04fc586SGary Hade 	return __add_pages(nid, zone, start_pfn, nr_pages);
899ad757b6aSThomas Gleixner }
900ad757b6aSThomas Gleixner #endif
901ad757b6aSThomas Gleixner 
902ad757b6aSThomas Gleixner /*
903ad757b6aSThomas Gleixner  * This function cannot be __init, since exceptions don't work in that
904ad757b6aSThomas Gleixner  * section.  Put this after the callers, so that it cannot be inlined.
905ad757b6aSThomas Gleixner  */
9068550eb99SIngo Molnar static noinline int do_test_wp_bit(void)
907ad757b6aSThomas Gleixner {
908ad757b6aSThomas Gleixner 	char tmp_reg;
909ad757b6aSThomas Gleixner 	int flag;
910ad757b6aSThomas Gleixner 
911ad757b6aSThomas Gleixner 	__asm__ __volatile__(
912ad757b6aSThomas Gleixner 		"	movb %0, %1	\n"
913ad757b6aSThomas Gleixner 		"1:	movb %1, %0	\n"
914ad757b6aSThomas Gleixner 		"	xorl %2, %2	\n"
915ad757b6aSThomas Gleixner 		"2:			\n"
916f832ff18SH. Peter Anvin 		_ASM_EXTABLE(1b,2b)
917ad757b6aSThomas Gleixner 		:"=m" (*(char *)fix_to_virt(FIX_WP_TEST)),
918ad757b6aSThomas Gleixner 		 "=q" (tmp_reg),
919ad757b6aSThomas Gleixner 		 "=r" (flag)
920ad757b6aSThomas Gleixner 		:"2" (1)
921ad757b6aSThomas Gleixner 		:"memory");
922ad757b6aSThomas Gleixner 
923ad757b6aSThomas Gleixner 	return flag;
924ad757b6aSThomas Gleixner }
925ad757b6aSThomas Gleixner 
926ad757b6aSThomas Gleixner #ifdef CONFIG_DEBUG_RODATA
927edeed305SArjan van de Ven const int rodata_test_data = 0xC3;
928edeed305SArjan van de Ven EXPORT_SYMBOL_GPL(rodata_test_data);
929ad757b6aSThomas Gleixner 
930502f6604SSuresh Siddha int kernel_set_to_readonly __read_mostly;
93116239630SSteven Rostedt 
93216239630SSteven Rostedt void set_kernel_text_rw(void)
93316239630SSteven Rostedt {
93416239630SSteven Rostedt 	unsigned long start = PFN_ALIGN(_text);
93516239630SSteven Rostedt 	unsigned long size = PFN_ALIGN(_etext) - start;
93616239630SSteven Rostedt 
93716239630SSteven Rostedt 	if (!kernel_set_to_readonly)
93816239630SSteven Rostedt 		return;
93916239630SSteven Rostedt 
94016239630SSteven Rostedt 	pr_debug("Set kernel text: %lx - %lx for read write\n",
94116239630SSteven Rostedt 		 start, start+size);
94216239630SSteven Rostedt 
94316239630SSteven Rostedt 	set_pages_rw(virt_to_page(start), size >> PAGE_SHIFT);
94416239630SSteven Rostedt }
94516239630SSteven Rostedt 
94616239630SSteven Rostedt void set_kernel_text_ro(void)
94716239630SSteven Rostedt {
94816239630SSteven Rostedt 	unsigned long start = PFN_ALIGN(_text);
94916239630SSteven Rostedt 	unsigned long size = PFN_ALIGN(_etext) - start;
95016239630SSteven Rostedt 
95116239630SSteven Rostedt 	if (!kernel_set_to_readonly)
95216239630SSteven Rostedt 		return;
95316239630SSteven Rostedt 
95416239630SSteven Rostedt 	pr_debug("Set kernel text: %lx - %lx for read only\n",
95516239630SSteven Rostedt 		 start, start+size);
95616239630SSteven Rostedt 
95716239630SSteven Rostedt 	set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
95816239630SSteven Rostedt }
95916239630SSteven Rostedt 
960ad757b6aSThomas Gleixner void mark_rodata_ro(void)
961ad757b6aSThomas Gleixner {
962ad757b6aSThomas Gleixner 	unsigned long start = PFN_ALIGN(_text);
963ad757b6aSThomas Gleixner 	unsigned long size = PFN_ALIGN(_etext) - start;
964ad757b6aSThomas Gleixner 
9656d238cc4SArjan van de Ven 	set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
966d7d119d7SIngo Molnar 	printk(KERN_INFO "Write protecting the kernel text: %luk\n",
967d7d119d7SIngo Molnar 		size >> 10);
9680c42f392SAndi Kleen 
96916239630SSteven Rostedt 	kernel_set_to_readonly = 1;
97016239630SSteven Rostedt 
9710c42f392SAndi Kleen #ifdef CONFIG_CPA_DEBUG
972d7d119d7SIngo Molnar 	printk(KERN_INFO "Testing CPA: Reverting %lx-%lx\n",
973d7d119d7SIngo Molnar 		start, start+size);
9746d238cc4SArjan van de Ven 	set_pages_rw(virt_to_page(start), size>>PAGE_SHIFT);
9750c42f392SAndi Kleen 
976d7d119d7SIngo Molnar 	printk(KERN_INFO "Testing CPA: write protecting again\n");
9776d238cc4SArjan van de Ven 	set_pages_ro(virt_to_page(start), size>>PAGE_SHIFT);
9780c42f392SAndi Kleen #endif
9798f0f996eSSteven Rostedt 
980ad757b6aSThomas Gleixner 	start += size;
981ad757b6aSThomas Gleixner 	size = (unsigned long)__end_rodata - start;
9826d238cc4SArjan van de Ven 	set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
983d7d119d7SIngo Molnar 	printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
984ad757b6aSThomas Gleixner 		size >> 10);
985edeed305SArjan van de Ven 	rodata_test();
986ad757b6aSThomas Gleixner 
9870c42f392SAndi Kleen #ifdef CONFIG_CPA_DEBUG
988d7d119d7SIngo Molnar 	printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, start + size);
9896d238cc4SArjan van de Ven 	set_pages_rw(virt_to_page(start), size >> PAGE_SHIFT);
9900c42f392SAndi Kleen 
991d7d119d7SIngo Molnar 	printk(KERN_INFO "Testing CPA: write protecting again\n");
9926d238cc4SArjan van de Ven 	set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
9930c42f392SAndi Kleen #endif
994ad757b6aSThomas Gleixner }
995ad757b6aSThomas Gleixner #endif
996a9ce6bc1SYinghai Lu 
997