xref: /linux/arch/x86/mm/init_32.c (revision 8bd753be7a96443dd5111cb07ed5907a3787c978)
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/uaccess.h>
39ad757b6aSThomas Gleixner #include <asm/pgtable.h>
40ad757b6aSThomas Gleixner #include <asm/dma.h>
41ad757b6aSThomas Gleixner #include <asm/fixmap.h>
42ad757b6aSThomas Gleixner #include <asm/e820.h>
43ad757b6aSThomas Gleixner #include <asm/apic.h>
448550eb99SIngo Molnar #include <asm/bugs.h>
45ad757b6aSThomas Gleixner #include <asm/tlb.h>
46ad757b6aSThomas Gleixner #include <asm/tlbflush.h>
47c10d1e26SAndres Salomon #include <asm/olpc_ofw.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 {
64d1b19426SYinghai Lu 	unsigned long pfn = pgt_buf_end++;
654e29684cSYinghai Lu 	void *adr;
664e29684cSYinghai Lu 
67d1b19426SYinghai Lu 	if (pfn >= pgt_buf_top)
684e29684cSYinghai Lu 		panic("alloc_low_page: ran out of memory");
694e29684cSYinghai Lu 
704e29684cSYinghai Lu 	adr = __va(pfn * PAGE_SIZE);
71234bb549SJan Beulich 	clear_page(adr);
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
165d1b19426SYinghai Lu 	    && ((__pa(pte) >> PAGE_SHIFT) < pgt_buf_start
166d1b19426SYinghai Lu 		|| (__pa(pte) >> PAGE_SHIFT) >= pgt_buf_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 {
2295bd5a452SMatthieu Castet 	if (addr >= (unsigned long)_text && 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 
4261d931264SYinghai Lu void __init add_highpages_with_active_regions(int nid,
4271d931264SYinghai Lu 			 unsigned long start_pfn, unsigned long end_pfn)
428b5bc6c0eSYinghai Lu {
4298a9ca34cSTejun Heo 	phys_addr_t start, end;
4308a9ca34cSTejun Heo 	u64 i;
4311d931264SYinghai Lu 
4328a9ca34cSTejun Heo 	for_each_free_mem_range(i, nid, &start, &end, NULL) {
4338a9ca34cSTejun Heo 		unsigned long pfn = clamp_t(unsigned long, PFN_UP(start),
4348a9ca34cSTejun Heo 					    start_pfn, end_pfn);
4358a9ca34cSTejun Heo 		unsigned long e_pfn = clamp_t(unsigned long, PFN_DOWN(end),
4368a9ca34cSTejun Heo 					      start_pfn, end_pfn);
4378a9ca34cSTejun Heo 		for ( ; pfn < e_pfn; pfn++)
4388a9ca34cSTejun Heo 			if (pfn_valid(pfn))
4398a9ca34cSTejun Heo 				add_one_highpage_init(pfn_to_page(pfn));
440b5bc6c0eSYinghai Lu 	}
441ad757b6aSThomas Gleixner }
442ad757b6aSThomas Gleixner #else
443e8e32326SIngo Brueckl static inline void permanent_kmaps_init(pgd_t *pgd_base)
444e8e32326SIngo Brueckl {
445e8e32326SIngo Brueckl }
446ad757b6aSThomas Gleixner #endif /* CONFIG_HIGHMEM */
447ad757b6aSThomas Gleixner 
448ad757b6aSThomas Gleixner void __init native_pagetable_setup_start(pgd_t *base)
449ad757b6aSThomas Gleixner {
450551889a6SIan Campbell 	unsigned long pfn, va;
451551889a6SIan Campbell 	pgd_t *pgd;
452551889a6SIan Campbell 	pud_t *pud;
453551889a6SIan Campbell 	pmd_t *pmd;
454551889a6SIan Campbell 	pte_t *pte;
455ad757b6aSThomas Gleixner 
456ad757b6aSThomas Gleixner 	/*
457551889a6SIan Campbell 	 * Remove any mappings which extend past the end of physical
458551889a6SIan Campbell 	 * memory from the boot time page table:
459ad757b6aSThomas Gleixner 	 */
460551889a6SIan Campbell 	for (pfn = max_low_pfn + 1; pfn < 1<<(32-PAGE_SHIFT); pfn++) {
461551889a6SIan Campbell 		va = PAGE_OFFSET + (pfn<<PAGE_SHIFT);
462551889a6SIan Campbell 		pgd = base + pgd_index(va);
463551889a6SIan Campbell 		if (!pgd_present(*pgd))
464551889a6SIan Campbell 			break;
465ad757b6aSThomas Gleixner 
466551889a6SIan Campbell 		pud = pud_offset(pgd, va);
467551889a6SIan Campbell 		pmd = pmd_offset(pud, va);
468551889a6SIan Campbell 		if (!pmd_present(*pmd))
469551889a6SIan Campbell 			break;
470551889a6SIan Campbell 
471551889a6SIan Campbell 		pte = pte_offset_kernel(pmd, va);
472551889a6SIan Campbell 		if (!pte_present(*pte))
473551889a6SIan Campbell 			break;
474551889a6SIan Campbell 
475551889a6SIan Campbell 		pte_clear(NULL, va, pte);
476551889a6SIan Campbell 	}
4776944a9c8SJeremy Fitzhardinge 	paravirt_alloc_pmd(&init_mm, __pa(base) >> PAGE_SHIFT);
478ad757b6aSThomas Gleixner }
479ad757b6aSThomas Gleixner 
480ad757b6aSThomas Gleixner void __init native_pagetable_setup_done(pgd_t *base)
481ad757b6aSThomas Gleixner {
482ad757b6aSThomas Gleixner }
483ad757b6aSThomas Gleixner 
484ad757b6aSThomas Gleixner /*
485ad757b6aSThomas Gleixner  * Build a proper pagetable for the kernel mappings.  Up until this
486ad757b6aSThomas Gleixner  * point, we've been running on some set of pagetables constructed by
487ad757b6aSThomas Gleixner  * the boot process.
488ad757b6aSThomas Gleixner  *
489ad757b6aSThomas Gleixner  * If we're booting on native hardware, this will be a pagetable
490551889a6SIan Campbell  * constructed in arch/x86/kernel/head_32.S.  The root of the
491551889a6SIan Campbell  * pagetable will be swapper_pg_dir.
492ad757b6aSThomas Gleixner  *
493ad757b6aSThomas Gleixner  * If we're booting paravirtualized under a hypervisor, then there are
494ad757b6aSThomas Gleixner  * more options: we may already be running PAE, and the pagetable may
495ad757b6aSThomas Gleixner  * or may not be based in swapper_pg_dir.  In any case,
496ad757b6aSThomas Gleixner  * paravirt_pagetable_setup_start() will set up swapper_pg_dir
497ad757b6aSThomas Gleixner  * appropriately for the rest of the initialization to work.
498ad757b6aSThomas Gleixner  *
499ad757b6aSThomas Gleixner  * In general, pagetable_init() assumes that the pagetable may already
500ad757b6aSThomas Gleixner  * be partially populated, and so it avoids stomping on any existing
501ad757b6aSThomas Gleixner  * mappings.
502ad757b6aSThomas Gleixner  */
503f765090aSPekka Enberg void __init early_ioremap_page_table_range_init(void)
504ad757b6aSThomas Gleixner {
505e7179853SPekka Enberg 	pgd_t *pgd_base = swapper_pg_dir;
5068550eb99SIngo Molnar 	unsigned long vaddr, end;
507ad757b6aSThomas Gleixner 
508ad757b6aSThomas Gleixner 	/*
509ad757b6aSThomas Gleixner 	 * Fixed mappings, only the page table structure has to be
510ad757b6aSThomas Gleixner 	 * created - mappings will be set by set_fixmap():
511ad757b6aSThomas Gleixner 	 */
512ad757b6aSThomas Gleixner 	vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
513ad757b6aSThomas Gleixner 	end = (FIXADDR_TOP + PMD_SIZE - 1) & PMD_MASK;
514ad757b6aSThomas Gleixner 	page_table_range_init(vaddr, end, pgd_base);
515beacfaacSHuang, Ying 	early_ioremap_reset();
516e7b37895SYinghai Lu }
517e7b37895SYinghai Lu 
518e7b37895SYinghai Lu static void __init pagetable_init(void)
519e7b37895SYinghai Lu {
520e7b37895SYinghai Lu 	pgd_t *pgd_base = swapper_pg_dir;
521e7b37895SYinghai Lu 
522ad757b6aSThomas Gleixner 	permanent_kmaps_init(pgd_base);
523ad757b6aSThomas Gleixner }
524ad757b6aSThomas Gleixner 
525be43d728SJeremy Fitzhardinge pteval_t __supported_pte_mask __read_mostly = ~(_PAGE_NX | _PAGE_GLOBAL | _PAGE_IOMAP);
5266fdc05d4SJeremy Fitzhardinge EXPORT_SYMBOL_GPL(__supported_pte_mask);
5276fdc05d4SJeremy Fitzhardinge 
52890d967e0SYinghai Lu /* user-defined highmem size */
52990d967e0SYinghai Lu static unsigned int highmem_pages = -1;
53090d967e0SYinghai Lu 
53190d967e0SYinghai Lu /*
53290d967e0SYinghai Lu  * highmem=size forces highmem to be exactly 'size' bytes.
53390d967e0SYinghai Lu  * This works even on boxes that have no highmem otherwise.
53490d967e0SYinghai Lu  * This also works to reduce highmem size on bigger boxes.
53590d967e0SYinghai Lu  */
53690d967e0SYinghai Lu static int __init parse_highmem(char *arg)
53790d967e0SYinghai Lu {
53890d967e0SYinghai Lu 	if (!arg)
53990d967e0SYinghai Lu 		return -EINVAL;
54090d967e0SYinghai Lu 
54190d967e0SYinghai Lu 	highmem_pages = memparse(arg, &arg) >> PAGE_SHIFT;
54290d967e0SYinghai Lu 	return 0;
54390d967e0SYinghai Lu }
54490d967e0SYinghai Lu early_param("highmem", parse_highmem);
54590d967e0SYinghai Lu 
5464769843bSIngo Molnar #define MSG_HIGHMEM_TOO_BIG \
5474769843bSIngo Molnar 	"highmem size (%luMB) is bigger than pages available (%luMB)!\n"
5484769843bSIngo Molnar 
5494769843bSIngo Molnar #define MSG_LOWMEM_TOO_SMALL \
5504769843bSIngo Molnar 	"highmem size (%luMB) results in <64MB lowmem, ignoring it!\n"
55190d967e0SYinghai Lu /*
5524769843bSIngo Molnar  * All of RAM fits into lowmem - but if user wants highmem
5534769843bSIngo Molnar  * artificially via the highmem=x boot parameter then create
5544769843bSIngo Molnar  * it:
55590d967e0SYinghai Lu  */
5564769843bSIngo Molnar void __init lowmem_pfn_init(void)
55790d967e0SYinghai Lu {
558346cafecSYinghai Lu 	/* max_low_pfn is 0, we already have early_res support */
55990d967e0SYinghai Lu 	max_low_pfn = max_pfn;
560d88316c2SIngo Molnar 
5614769843bSIngo Molnar 	if (highmem_pages == -1)
5624769843bSIngo Molnar 		highmem_pages = 0;
5634769843bSIngo Molnar #ifdef CONFIG_HIGHMEM
5644769843bSIngo Molnar 	if (highmem_pages >= max_pfn) {
5654769843bSIngo Molnar 		printk(KERN_ERR MSG_HIGHMEM_TOO_BIG,
5664769843bSIngo Molnar 			pages_to_mb(highmem_pages), pages_to_mb(max_pfn));
5674769843bSIngo Molnar 		highmem_pages = 0;
5684769843bSIngo Molnar 	}
5694769843bSIngo Molnar 	if (highmem_pages) {
5704769843bSIngo Molnar 		if (max_low_pfn - highmem_pages < 64*1024*1024/PAGE_SIZE) {
5714769843bSIngo Molnar 			printk(KERN_ERR MSG_LOWMEM_TOO_SMALL,
5724769843bSIngo Molnar 				pages_to_mb(highmem_pages));
5734769843bSIngo Molnar 			highmem_pages = 0;
5744769843bSIngo Molnar 		}
5754769843bSIngo Molnar 		max_low_pfn -= highmem_pages;
5764769843bSIngo Molnar 	}
5774769843bSIngo Molnar #else
5784769843bSIngo Molnar 	if (highmem_pages)
5794769843bSIngo Molnar 		printk(KERN_ERR "ignoring highmem size on non-highmem kernel!\n");
5804769843bSIngo Molnar #endif
5814769843bSIngo Molnar }
5824769843bSIngo Molnar 
5834769843bSIngo Molnar #define MSG_HIGHMEM_TOO_SMALL \
5844769843bSIngo Molnar 	"only %luMB highmem pages available, ignoring highmem size of %luMB!\n"
5854769843bSIngo Molnar 
5864769843bSIngo Molnar #define MSG_HIGHMEM_TRIMMED \
5874769843bSIngo Molnar 	"Warning: only 4GB will be used. Use a HIGHMEM64G enabled kernel!\n"
5884769843bSIngo Molnar /*
5894769843bSIngo Molnar  * We have more RAM than fits into lowmem - we try to put it into
5904769843bSIngo Molnar  * highmem, also taking the highmem=x boot parameter into account:
5914769843bSIngo Molnar  */
5924769843bSIngo Molnar void __init highmem_pfn_init(void)
5934769843bSIngo Molnar {
594d88316c2SIngo Molnar 	max_low_pfn = MAXMEM_PFN;
595d88316c2SIngo Molnar 
59690d967e0SYinghai Lu 	if (highmem_pages == -1)
59790d967e0SYinghai Lu 		highmem_pages = max_pfn - MAXMEM_PFN;
5984769843bSIngo Molnar 
59990d967e0SYinghai Lu 	if (highmem_pages + MAXMEM_PFN < max_pfn)
60090d967e0SYinghai Lu 		max_pfn = MAXMEM_PFN + highmem_pages;
6014769843bSIngo Molnar 
60290d967e0SYinghai Lu 	if (highmem_pages + MAXMEM_PFN > max_pfn) {
6034769843bSIngo Molnar 		printk(KERN_WARNING MSG_HIGHMEM_TOO_SMALL,
60490d967e0SYinghai Lu 			pages_to_mb(max_pfn - MAXMEM_PFN),
60590d967e0SYinghai Lu 			pages_to_mb(highmem_pages));
60690d967e0SYinghai Lu 		highmem_pages = 0;
60790d967e0SYinghai Lu 	}
60890d967e0SYinghai Lu #ifndef CONFIG_HIGHMEM
60990d967e0SYinghai Lu 	/* Maximum memory usable is what is directly addressable */
6104769843bSIngo Molnar 	printk(KERN_WARNING "Warning only %ldMB will be used.\n", MAXMEM>>20);
61190d967e0SYinghai Lu 	if (max_pfn > MAX_NONPAE_PFN)
6124769843bSIngo Molnar 		printk(KERN_WARNING "Use a HIGHMEM64G enabled kernel.\n");
61390d967e0SYinghai Lu 	else
61490d967e0SYinghai Lu 		printk(KERN_WARNING "Use a HIGHMEM enabled kernel.\n");
61590d967e0SYinghai Lu 	max_pfn = MAXMEM_PFN;
61690d967e0SYinghai Lu #else /* !CONFIG_HIGHMEM */
61790d967e0SYinghai Lu #ifndef CONFIG_HIGHMEM64G
61890d967e0SYinghai Lu 	if (max_pfn > MAX_NONPAE_PFN) {
61990d967e0SYinghai Lu 		max_pfn = MAX_NONPAE_PFN;
6204769843bSIngo Molnar 		printk(KERN_WARNING MSG_HIGHMEM_TRIMMED);
62190d967e0SYinghai Lu 	}
62290d967e0SYinghai Lu #endif /* !CONFIG_HIGHMEM64G */
62390d967e0SYinghai Lu #endif /* !CONFIG_HIGHMEM */
62490d967e0SYinghai Lu }
6254769843bSIngo Molnar 
62690d967e0SYinghai Lu /*
62790d967e0SYinghai Lu  * Determine low and high memory ranges:
62890d967e0SYinghai Lu  */
62990d967e0SYinghai Lu void __init find_low_pfn_range(void)
63090d967e0SYinghai Lu {
63190d967e0SYinghai Lu 	/* it could update max_pfn */
63290d967e0SYinghai Lu 
633d88316c2SIngo Molnar 	if (max_pfn <= MAXMEM_PFN)
6344769843bSIngo Molnar 		lowmem_pfn_init();
635d88316c2SIngo Molnar 	else
636d88316c2SIngo Molnar 		highmem_pfn_init();
63790d967e0SYinghai Lu }
63890d967e0SYinghai Lu 
639b2ac82a0SYinghai Lu #ifndef CONFIG_NEED_MULTIPLE_NODES
640d8fc3afcSTejun Heo void __init initmem_init(void)
641b2ac82a0SYinghai Lu {
642b2ac82a0SYinghai Lu #ifdef CONFIG_HIGHMEM
643b2ac82a0SYinghai Lu 	highstart_pfn = highend_pfn = max_pfn;
644b2ac82a0SYinghai Lu 	if (max_pfn > max_low_pfn)
645b2ac82a0SYinghai Lu 		highstart_pfn = max_low_pfn;
646b2ac82a0SYinghai Lu 	printk(KERN_NOTICE "%ldMB HIGHMEM available.\n",
647b2ac82a0SYinghai Lu 		pages_to_mb(highend_pfn - highstart_pfn));
648b2ac82a0SYinghai Lu 	num_physpages = highend_pfn;
649b2ac82a0SYinghai Lu 	high_memory = (void *) __va(highstart_pfn * PAGE_SIZE - 1) + 1;
650b2ac82a0SYinghai Lu #else
651b2ac82a0SYinghai Lu 	num_physpages = max_low_pfn;
652b2ac82a0SYinghai Lu 	high_memory = (void *) __va(max_low_pfn * PAGE_SIZE - 1) + 1;
653b2ac82a0SYinghai Lu #endif
6540608f70cSTejun Heo 
6550608f70cSTejun Heo 	memblock_set_node(0, (phys_addr_t)ULLONG_MAX, 0);
6560608f70cSTejun Heo 	sparse_memory_present_with_active_regions(0);
6570608f70cSTejun Heo 
658b2ac82a0SYinghai Lu #ifdef CONFIG_FLATMEM
659b2ac82a0SYinghai Lu 	max_mapnr = num_physpages;
660b2ac82a0SYinghai Lu #endif
661dc16ecf7SJeremy Fitzhardinge 	__vmalloc_start_set = true;
662dc16ecf7SJeremy Fitzhardinge 
663b2ac82a0SYinghai Lu 	printk(KERN_NOTICE "%ldMB LOWMEM available.\n",
664b2ac82a0SYinghai Lu 			pages_to_mb(max_low_pfn));
665b2ac82a0SYinghai Lu 
666b2ac82a0SYinghai Lu 	setup_bootmem_allocator();
667b2ac82a0SYinghai Lu }
668cb95a13aSYinghai Lu #endif /* !CONFIG_NEED_MULTIPLE_NODES */
669b2ac82a0SYinghai Lu 
670b2ac82a0SYinghai Lu void __init setup_bootmem_allocator(void)
671b2ac82a0SYinghai Lu {
672b2ac82a0SYinghai Lu 	printk(KERN_INFO "  mapped low ram: 0 - %08lx\n",
673b2ac82a0SYinghai Lu 		 max_pfn_mapped<<PAGE_SHIFT);
674fc5efe39SYinghai Lu 	printk(KERN_INFO "  low ram: 0 - %08lx\n", max_low_pfn<<PAGE_SHIFT);
675b2ac82a0SYinghai Lu 
676c464573cSPekka Enberg 	after_bootmem = 1;
6774e29684cSYinghai Lu }
6784e29684cSYinghai Lu 
679a04ad82dSYinghai Lu /*
680ad757b6aSThomas Gleixner  * paging_init() sets up the page tables - note that the first 8MB are
681ad757b6aSThomas Gleixner  * already mapped by head.S.
682ad757b6aSThomas Gleixner  *
683ad757b6aSThomas Gleixner  * This routines also unmaps the page at virtual kernel address 0, so
684ad757b6aSThomas Gleixner  * that we can trap those pesky NULL-reference errors in the kernel.
685ad757b6aSThomas Gleixner  */
686ad757b6aSThomas Gleixner void __init paging_init(void)
687ad757b6aSThomas Gleixner {
688ad757b6aSThomas Gleixner 	pagetable_init();
689ad757b6aSThomas Gleixner 
690ad757b6aSThomas Gleixner 	__flush_tlb_all();
691ad757b6aSThomas Gleixner 
692ad757b6aSThomas Gleixner 	kmap_init();
69311cd0bc1SYinghai Lu 
69411cd0bc1SYinghai Lu 	/*
69511cd0bc1SYinghai Lu 	 * NOTE: at this point the bootmem allocator is fully available.
69611cd0bc1SYinghai Lu 	 */
697c10d1e26SAndres Salomon 	olpc_dt_build_devicetree();
698797390d8STejun Heo 	sparse_memory_present_with_active_regions(MAX_NUMNODES);
69911cd0bc1SYinghai Lu 	sparse_init();
70011cd0bc1SYinghai Lu 	zone_sizes_init();
701ad757b6aSThomas Gleixner }
702ad757b6aSThomas Gleixner 
703ad757b6aSThomas Gleixner /*
704ad757b6aSThomas Gleixner  * Test if the WP bit works in supervisor mode. It isn't supported on 386's
705f7f17a67SDmitri Vorobiev  * and also on some strange 486's. All 586+'s are OK. This used to involve
706f7f17a67SDmitri Vorobiev  * black magic jumps to work around some nasty CPU bugs, but fortunately the
707f7f17a67SDmitri Vorobiev  * switch to using exceptions got rid of all that.
708ad757b6aSThomas Gleixner  */
709ad757b6aSThomas Gleixner static void __init test_wp_bit(void)
710ad757b6aSThomas Gleixner {
711d7d119d7SIngo Molnar 	printk(KERN_INFO
712d7d119d7SIngo Molnar   "Checking if this processor honours the WP bit even in supervisor mode...");
713ad757b6aSThomas Gleixner 
714ad757b6aSThomas Gleixner 	/* Any page-aligned address will do, the test is non-destructive */
715*8bd753beSH. Peter Anvin 	__set_fixmap(FIX_WP_TEST, __pa(&swapper_pg_dir), PAGE_KERNEL_RO);
716ad757b6aSThomas Gleixner 	boot_cpu_data.wp_works_ok = do_test_wp_bit();
717ad757b6aSThomas Gleixner 	clear_fixmap(FIX_WP_TEST);
718ad757b6aSThomas Gleixner 
719ad757b6aSThomas Gleixner 	if (!boot_cpu_data.wp_works_ok) {
720d7d119d7SIngo Molnar 		printk(KERN_CONT "No.\n");
721ad757b6aSThomas Gleixner #ifdef CONFIG_X86_WP_WORKS_OK
722d7d119d7SIngo Molnar 		panic(
723d7d119d7SIngo Molnar   "This kernel doesn't support CPU's with broken WP. Recompile it for a 386!");
724ad757b6aSThomas Gleixner #endif
725ad757b6aSThomas Gleixner 	} else {
726d7d119d7SIngo Molnar 		printk(KERN_CONT "Ok.\n");
727ad757b6aSThomas Gleixner 	}
728ad757b6aSThomas Gleixner }
729ad757b6aSThomas Gleixner 
730ad757b6aSThomas Gleixner void __init mem_init(void)
731ad757b6aSThomas Gleixner {
732ad757b6aSThomas Gleixner 	int codesize, reservedpages, datasize, initsize;
733cc9f7a0cSYinghai Lu 	int tmp;
734ad757b6aSThomas Gleixner 
735cfb80c9eSJeremy Fitzhardinge 	pci_iommu_alloc();
736cfb80c9eSJeremy Fitzhardinge 
737ad757b6aSThomas Gleixner #ifdef CONFIG_FLATMEM
738ad757b6aSThomas Gleixner 	BUG_ON(!mem_map);
739ad757b6aSThomas Gleixner #endif
740855c743aSStanislaw Gruszka 	/*
741855c743aSStanislaw Gruszka 	 * With CONFIG_DEBUG_PAGEALLOC initialization of highmem pages has to
742855c743aSStanislaw Gruszka 	 * be done before free_all_bootmem(). Memblock use free low memory for
743855c743aSStanislaw Gruszka 	 * temporary data (see find_range_array()) and for this purpose can use
744855c743aSStanislaw Gruszka 	 * pages that was already passed to the buddy allocator, hence marked as
745855c743aSStanislaw Gruszka 	 * not accessible in the page tables when compiled with
746855c743aSStanislaw Gruszka 	 * CONFIG_DEBUG_PAGEALLOC. Otherwise order of initialization is not
747855c743aSStanislaw Gruszka 	 * important here.
748855c743aSStanislaw Gruszka 	 */
749855c743aSStanislaw Gruszka 	set_highmem_pages_init();
750855c743aSStanislaw Gruszka 
751ad757b6aSThomas Gleixner 	/* this will put all low memory onto the freelists */
752ad757b6aSThomas Gleixner 	totalram_pages += free_all_bootmem();
753ad757b6aSThomas Gleixner 
754ad757b6aSThomas Gleixner 	reservedpages = 0;
755ad757b6aSThomas Gleixner 	for (tmp = 0; tmp < max_low_pfn; tmp++)
756ad757b6aSThomas Gleixner 		/*
7578550eb99SIngo Molnar 		 * Only count reserved RAM pages:
758ad757b6aSThomas Gleixner 		 */
759ad757b6aSThomas Gleixner 		if (page_is_ram(tmp) && PageReserved(pfn_to_page(tmp)))
760ad757b6aSThomas Gleixner 			reservedpages++;
761ad757b6aSThomas Gleixner 
762ad757b6aSThomas Gleixner 	codesize =  (unsigned long) &_etext - (unsigned long) &_text;
763ad757b6aSThomas Gleixner 	datasize =  (unsigned long) &_edata - (unsigned long) &_etext;
764ad757b6aSThomas Gleixner 	initsize =  (unsigned long) &__init_end - (unsigned long) &__init_begin;
765ad757b6aSThomas Gleixner 
7668550eb99SIngo Molnar 	printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, "
7678550eb99SIngo Molnar 			"%dk reserved, %dk data, %dk init, %ldk highmem)\n",
768cc013a88SGeert Uytterhoeven 		nr_free_pages() << (PAGE_SHIFT-10),
769ad757b6aSThomas Gleixner 		num_physpages << (PAGE_SHIFT-10),
770ad757b6aSThomas Gleixner 		codesize >> 10,
771ad757b6aSThomas Gleixner 		reservedpages << (PAGE_SHIFT-10),
772ad757b6aSThomas Gleixner 		datasize >> 10,
773ad757b6aSThomas Gleixner 		initsize >> 10,
7744b529401SAndreas Fenkart 		totalhigh_pages << (PAGE_SHIFT-10));
775ad757b6aSThomas Gleixner 
776d7d119d7SIngo Molnar 	printk(KERN_INFO "virtual kernel memory layout:\n"
777ad757b6aSThomas Gleixner 		"    fixmap  : 0x%08lx - 0x%08lx   (%4ld kB)\n"
778ad757b6aSThomas Gleixner #ifdef CONFIG_HIGHMEM
779ad757b6aSThomas Gleixner 		"    pkmap   : 0x%08lx - 0x%08lx   (%4ld kB)\n"
780ad757b6aSThomas Gleixner #endif
781ad757b6aSThomas Gleixner 		"    vmalloc : 0x%08lx - 0x%08lx   (%4ld MB)\n"
782ad757b6aSThomas Gleixner 		"    lowmem  : 0x%08lx - 0x%08lx   (%4ld MB)\n"
783ad757b6aSThomas Gleixner 		"      .init : 0x%08lx - 0x%08lx   (%4ld kB)\n"
784ad757b6aSThomas Gleixner 		"      .data : 0x%08lx - 0x%08lx   (%4ld kB)\n"
785ad757b6aSThomas Gleixner 		"      .text : 0x%08lx - 0x%08lx   (%4ld kB)\n",
786ad757b6aSThomas Gleixner 		FIXADDR_START, FIXADDR_TOP,
787ad757b6aSThomas Gleixner 		(FIXADDR_TOP - FIXADDR_START) >> 10,
788ad757b6aSThomas Gleixner 
789ad757b6aSThomas Gleixner #ifdef CONFIG_HIGHMEM
790ad757b6aSThomas Gleixner 		PKMAP_BASE, PKMAP_BASE+LAST_PKMAP*PAGE_SIZE,
791ad757b6aSThomas Gleixner 		(LAST_PKMAP*PAGE_SIZE) >> 10,
792ad757b6aSThomas Gleixner #endif
793ad757b6aSThomas Gleixner 
794ad757b6aSThomas Gleixner 		VMALLOC_START, VMALLOC_END,
795ad757b6aSThomas Gleixner 		(VMALLOC_END - VMALLOC_START) >> 20,
796ad757b6aSThomas Gleixner 
797ad757b6aSThomas Gleixner 		(unsigned long)__va(0), (unsigned long)high_memory,
798ad757b6aSThomas Gleixner 		((unsigned long)high_memory - (unsigned long)__va(0)) >> 20,
799ad757b6aSThomas Gleixner 
800ad757b6aSThomas Gleixner 		(unsigned long)&__init_begin, (unsigned long)&__init_end,
8018550eb99SIngo Molnar 		((unsigned long)&__init_end -
8028550eb99SIngo Molnar 		 (unsigned long)&__init_begin) >> 10,
803ad757b6aSThomas Gleixner 
804ad757b6aSThomas Gleixner 		(unsigned long)&_etext, (unsigned long)&_edata,
805ad757b6aSThomas Gleixner 		((unsigned long)&_edata - (unsigned long)&_etext) >> 10,
806ad757b6aSThomas Gleixner 
807ad757b6aSThomas Gleixner 		(unsigned long)&_text, (unsigned long)&_etext,
808ad757b6aSThomas Gleixner 		((unsigned long)&_etext - (unsigned long)&_text) >> 10);
809ad757b6aSThomas Gleixner 
810beeb4195SJan Beulich 	/*
811beeb4195SJan Beulich 	 * Check boundaries twice: Some fundamental inconsistencies can
812beeb4195SJan Beulich 	 * be detected at build time already.
813beeb4195SJan Beulich 	 */
814beeb4195SJan Beulich #define __FIXADDR_TOP (-PAGE_SIZE)
815beeb4195SJan Beulich #ifdef CONFIG_HIGHMEM
816beeb4195SJan Beulich 	BUILD_BUG_ON(PKMAP_BASE + LAST_PKMAP*PAGE_SIZE	> FIXADDR_START);
817beeb4195SJan Beulich 	BUILD_BUG_ON(VMALLOC_END			> PKMAP_BASE);
818beeb4195SJan Beulich #endif
819beeb4195SJan Beulich #define high_memory (-128UL << 20)
820beeb4195SJan Beulich 	BUILD_BUG_ON(VMALLOC_START			>= VMALLOC_END);
821beeb4195SJan Beulich #undef high_memory
822beeb4195SJan Beulich #undef __FIXADDR_TOP
823beeb4195SJan Beulich 
824ad757b6aSThomas Gleixner #ifdef CONFIG_HIGHMEM
825ad757b6aSThomas Gleixner 	BUG_ON(PKMAP_BASE + LAST_PKMAP*PAGE_SIZE	> FIXADDR_START);
826ad757b6aSThomas Gleixner 	BUG_ON(VMALLOC_END				> PKMAP_BASE);
827ad757b6aSThomas Gleixner #endif
828beeb4195SJan Beulich 	BUG_ON(VMALLOC_START				>= VMALLOC_END);
829ad757b6aSThomas Gleixner 	BUG_ON((unsigned long)high_memory		> VMALLOC_START);
830ad757b6aSThomas Gleixner 
831ad757b6aSThomas Gleixner 	if (boot_cpu_data.wp_works_ok < 0)
832ad757b6aSThomas Gleixner 		test_wp_bit();
833ad757b6aSThomas Gleixner }
834ad757b6aSThomas Gleixner 
835ad757b6aSThomas Gleixner #ifdef CONFIG_MEMORY_HOTPLUG
836ad757b6aSThomas Gleixner int arch_add_memory(int nid, u64 start, u64 size)
837ad757b6aSThomas Gleixner {
838ad757b6aSThomas Gleixner 	struct pglist_data *pgdata = NODE_DATA(nid);
839ad757b6aSThomas Gleixner 	struct zone *zone = pgdata->node_zones + ZONE_HIGHMEM;
840ad757b6aSThomas Gleixner 	unsigned long start_pfn = start >> PAGE_SHIFT;
841ad757b6aSThomas Gleixner 	unsigned long nr_pages = size >> PAGE_SHIFT;
842ad757b6aSThomas Gleixner 
843c04fc586SGary Hade 	return __add_pages(nid, zone, start_pfn, nr_pages);
844ad757b6aSThomas Gleixner }
845ad757b6aSThomas Gleixner #endif
846ad757b6aSThomas Gleixner 
847ad757b6aSThomas Gleixner /*
848ad757b6aSThomas Gleixner  * This function cannot be __init, since exceptions don't work in that
849ad757b6aSThomas Gleixner  * section.  Put this after the callers, so that it cannot be inlined.
850ad757b6aSThomas Gleixner  */
8518550eb99SIngo Molnar static noinline int do_test_wp_bit(void)
852ad757b6aSThomas Gleixner {
853ad757b6aSThomas Gleixner 	char tmp_reg;
854ad757b6aSThomas Gleixner 	int flag;
855ad757b6aSThomas Gleixner 
856ad757b6aSThomas Gleixner 	__asm__ __volatile__(
857ad757b6aSThomas Gleixner 		"	movb %0, %1	\n"
858ad757b6aSThomas Gleixner 		"1:	movb %1, %0	\n"
859ad757b6aSThomas Gleixner 		"	xorl %2, %2	\n"
860ad757b6aSThomas Gleixner 		"2:			\n"
861f832ff18SH. Peter Anvin 		_ASM_EXTABLE(1b,2b)
862ad757b6aSThomas Gleixner 		:"=m" (*(char *)fix_to_virt(FIX_WP_TEST)),
863ad757b6aSThomas Gleixner 		 "=q" (tmp_reg),
864ad757b6aSThomas Gleixner 		 "=r" (flag)
865ad757b6aSThomas Gleixner 		:"2" (1)
866ad757b6aSThomas Gleixner 		:"memory");
867ad757b6aSThomas Gleixner 
868ad757b6aSThomas Gleixner 	return flag;
869ad757b6aSThomas Gleixner }
870ad757b6aSThomas Gleixner 
871ad757b6aSThomas Gleixner #ifdef CONFIG_DEBUG_RODATA
872edeed305SArjan van de Ven const int rodata_test_data = 0xC3;
873edeed305SArjan van de Ven EXPORT_SYMBOL_GPL(rodata_test_data);
874ad757b6aSThomas Gleixner 
875502f6604SSuresh Siddha int kernel_set_to_readonly __read_mostly;
87616239630SSteven Rostedt 
87716239630SSteven Rostedt void set_kernel_text_rw(void)
87816239630SSteven Rostedt {
87916239630SSteven Rostedt 	unsigned long start = PFN_ALIGN(_text);
88016239630SSteven Rostedt 	unsigned long size = PFN_ALIGN(_etext) - start;
88116239630SSteven Rostedt 
88216239630SSteven Rostedt 	if (!kernel_set_to_readonly)
88316239630SSteven Rostedt 		return;
88416239630SSteven Rostedt 
88516239630SSteven Rostedt 	pr_debug("Set kernel text: %lx - %lx for read write\n",
88616239630SSteven Rostedt 		 start, start+size);
88716239630SSteven Rostedt 
88816239630SSteven Rostedt 	set_pages_rw(virt_to_page(start), size >> PAGE_SHIFT);
88916239630SSteven Rostedt }
89016239630SSteven Rostedt 
89116239630SSteven Rostedt void set_kernel_text_ro(void)
89216239630SSteven Rostedt {
89316239630SSteven Rostedt 	unsigned long start = PFN_ALIGN(_text);
89416239630SSteven Rostedt 	unsigned long size = PFN_ALIGN(_etext) - start;
89516239630SSteven Rostedt 
89616239630SSteven Rostedt 	if (!kernel_set_to_readonly)
89716239630SSteven Rostedt 		return;
89816239630SSteven Rostedt 
89916239630SSteven Rostedt 	pr_debug("Set kernel text: %lx - %lx for read only\n",
90016239630SSteven Rostedt 		 start, start+size);
90116239630SSteven Rostedt 
90216239630SSteven Rostedt 	set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
90316239630SSteven Rostedt }
90416239630SSteven Rostedt 
9055bd5a452SMatthieu Castet static void mark_nxdata_nx(void)
9065bd5a452SMatthieu Castet {
9075bd5a452SMatthieu Castet 	/*
9085bd5a452SMatthieu Castet 	 * When this called, init has already been executed and released,
9090d2eb44fSLucas De Marchi 	 * so everything past _etext should be NX.
9105bd5a452SMatthieu Castet 	 */
9115bd5a452SMatthieu Castet 	unsigned long start = PFN_ALIGN(_etext);
9125bd5a452SMatthieu Castet 	/*
9135bd5a452SMatthieu Castet 	 * This comes from is_kernel_text upper limit. Also HPAGE where used:
9145bd5a452SMatthieu Castet 	 */
9155bd5a452SMatthieu Castet 	unsigned long size = (((unsigned long)__init_end + HPAGE_SIZE) & HPAGE_MASK) - start;
9165bd5a452SMatthieu Castet 
9175bd5a452SMatthieu Castet 	if (__supported_pte_mask & _PAGE_NX)
9185bd5a452SMatthieu Castet 		printk(KERN_INFO "NX-protecting the kernel data: %luk\n", size >> 10);
9195bd5a452SMatthieu Castet 	set_pages_nx(virt_to_page(start), size >> PAGE_SHIFT);
9205bd5a452SMatthieu Castet }
9215bd5a452SMatthieu Castet 
922ad757b6aSThomas Gleixner void mark_rodata_ro(void)
923ad757b6aSThomas Gleixner {
924ad757b6aSThomas Gleixner 	unsigned long start = PFN_ALIGN(_text);
925ad757b6aSThomas Gleixner 	unsigned long size = PFN_ALIGN(_etext) - start;
926ad757b6aSThomas Gleixner 
9276d238cc4SArjan van de Ven 	set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
928d7d119d7SIngo Molnar 	printk(KERN_INFO "Write protecting the kernel text: %luk\n",
929d7d119d7SIngo Molnar 		size >> 10);
9300c42f392SAndi Kleen 
93116239630SSteven Rostedt 	kernel_set_to_readonly = 1;
93216239630SSteven Rostedt 
9330c42f392SAndi Kleen #ifdef CONFIG_CPA_DEBUG
934d7d119d7SIngo Molnar 	printk(KERN_INFO "Testing CPA: Reverting %lx-%lx\n",
935d7d119d7SIngo Molnar 		start, start+size);
9366d238cc4SArjan van de Ven 	set_pages_rw(virt_to_page(start), size>>PAGE_SHIFT);
9370c42f392SAndi Kleen 
938d7d119d7SIngo Molnar 	printk(KERN_INFO "Testing CPA: write protecting again\n");
9396d238cc4SArjan van de Ven 	set_pages_ro(virt_to_page(start), size>>PAGE_SHIFT);
9400c42f392SAndi Kleen #endif
9418f0f996eSSteven Rostedt 
942ad757b6aSThomas Gleixner 	start += size;
943ad757b6aSThomas Gleixner 	size = (unsigned long)__end_rodata - start;
9446d238cc4SArjan van de Ven 	set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
945d7d119d7SIngo Molnar 	printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
946ad757b6aSThomas Gleixner 		size >> 10);
947edeed305SArjan van de Ven 	rodata_test();
948ad757b6aSThomas Gleixner 
9490c42f392SAndi Kleen #ifdef CONFIG_CPA_DEBUG
950d7d119d7SIngo Molnar 	printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, start + size);
9516d238cc4SArjan van de Ven 	set_pages_rw(virt_to_page(start), size >> PAGE_SHIFT);
9520c42f392SAndi Kleen 
953d7d119d7SIngo Molnar 	printk(KERN_INFO "Testing CPA: write protecting again\n");
9546d238cc4SArjan van de Ven 	set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
9550c42f392SAndi Kleen #endif
9565bd5a452SMatthieu Castet 	mark_nxdata_nx();
957ad757b6aSThomas Gleixner }
958ad757b6aSThomas Gleixner #endif
959ad757b6aSThomas Gleixner 
960