xref: /linux/arch/x86/mm/init_32.c (revision ac5c94264580f498e484c854031d0226b3c1038f)
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/signal.h>
9ad757b6aSThomas Gleixner #include <linux/sched.h>
10ad757b6aSThomas Gleixner #include <linux/kernel.h>
11ad757b6aSThomas Gleixner #include <linux/errno.h>
12ad757b6aSThomas Gleixner #include <linux/string.h>
13ad757b6aSThomas Gleixner #include <linux/types.h>
14ad757b6aSThomas Gleixner #include <linux/ptrace.h>
15ad757b6aSThomas Gleixner #include <linux/mman.h>
16ad757b6aSThomas Gleixner #include <linux/mm.h>
17ad757b6aSThomas Gleixner #include <linux/hugetlb.h>
18ad757b6aSThomas Gleixner #include <linux/swap.h>
19ad757b6aSThomas Gleixner #include <linux/smp.h>
20ad757b6aSThomas Gleixner #include <linux/init.h>
21ad757b6aSThomas Gleixner #include <linux/highmem.h>
22ad757b6aSThomas Gleixner #include <linux/pagemap.h>
23cfb80c9eSJeremy Fitzhardinge #include <linux/pci.h>
24ad757b6aSThomas Gleixner #include <linux/pfn.h>
25ad757b6aSThomas Gleixner #include <linux/poison.h>
26a9ce6bc1SYinghai Lu #include <linux/memblock.h>
27ad757b6aSThomas Gleixner #include <linux/proc_fs.h>
28ad757b6aSThomas Gleixner #include <linux/memory_hotplug.h>
29ad757b6aSThomas Gleixner #include <linux/initrd.h>
30ad757b6aSThomas Gleixner #include <linux/cpumask.h>
315a0e3ad6STejun Heo #include <linux/gfp.h>
32ad757b6aSThomas Gleixner 
33f832ff18SH. Peter Anvin #include <asm/asm.h>
3446eaa670SIngo Molnar #include <asm/bios_ebda.h>
35ad757b6aSThomas Gleixner #include <asm/processor.h>
367c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
37ad757b6aSThomas Gleixner #include <asm/pgtable.h>
38ad757b6aSThomas Gleixner #include <asm/dma.h>
39ad757b6aSThomas Gleixner #include <asm/fixmap.h>
4066441bd3SIngo Molnar #include <asm/e820/api.h>
41ad757b6aSThomas Gleixner #include <asm/apic.h>
428550eb99SIngo Molnar #include <asm/bugs.h>
43ad757b6aSThomas Gleixner #include <asm/tlb.h>
44ad757b6aSThomas Gleixner #include <asm/tlbflush.h>
45c10d1e26SAndres Salomon #include <asm/olpc_ofw.h>
46a5a19c63SJeremy Fitzhardinge #include <asm/pgalloc.h>
47ad757b6aSThomas Gleixner #include <asm/sections.h>
48ad757b6aSThomas Gleixner #include <asm/paravirt.h>
49551889a6SIan Campbell #include <asm/setup.h>
50d1163651SLaura Abbott #include <asm/set_memory.h>
512b72394eSPekka Enberg #include <asm/page_types.h>
5292a0f81dSThomas Gleixner #include <asm/cpu_entry_area.h>
534fcb2083SPekka Enberg #include <asm/init.h>
54ad757b6aSThomas Gleixner 
555c51bdbeSYinghai Lu #include "mm_internal.h"
565c51bdbeSYinghai Lu 
57ad757b6aSThomas Gleixner unsigned long highstart_pfn, highend_pfn;
58ad757b6aSThomas Gleixner 
59dc16ecf7SJeremy Fitzhardinge bool __read_mostly __vmalloc_start_set = false;
604e29684cSYinghai Lu 
61ad757b6aSThomas Gleixner /*
62ad757b6aSThomas Gleixner  * Creates a middle page table and puts a pointer to it in the
63ad757b6aSThomas Gleixner  * given global directory entry. This only returns the gd entry
64ad757b6aSThomas Gleixner  * in non-PAE compilation mode, since the middle layer is folded.
65ad757b6aSThomas Gleixner  */
66ad757b6aSThomas Gleixner static pmd_t * __init one_md_table_init(pgd_t *pgd)
67ad757b6aSThomas Gleixner {
68e0c4f675SKirill A. Shutemov 	p4d_t *p4d;
69ad757b6aSThomas Gleixner 	pud_t *pud;
70ad757b6aSThomas Gleixner 	pmd_t *pmd_table;
71ad757b6aSThomas Gleixner 
72ad757b6aSThomas Gleixner #ifdef CONFIG_X86_PAE
73ad757b6aSThomas Gleixner 	if (!(pgd_val(*pgd) & _PAGE_PRESENT)) {
74d6be89adSJan Beulich 		pmd_table = (pmd_t *)alloc_low_page();
756944a9c8SJeremy Fitzhardinge 		paravirt_alloc_pmd(&init_mm, __pa(pmd_table) >> PAGE_SHIFT);
76ad757b6aSThomas Gleixner 		set_pgd(pgd, __pgd(__pa(pmd_table) | _PAGE_PRESENT));
77e0c4f675SKirill A. Shutemov 		p4d = p4d_offset(pgd, 0);
78e0c4f675SKirill A. Shutemov 		pud = pud_offset(p4d, 0);
798550eb99SIngo Molnar 		BUG_ON(pmd_table != pmd_offset(pud, 0));
80a376f30aSZhaolei 
81a376f30aSZhaolei 		return pmd_table;
82ad757b6aSThomas Gleixner 	}
83ad757b6aSThomas Gleixner #endif
84e0c4f675SKirill A. Shutemov 	p4d = p4d_offset(pgd, 0);
85e0c4f675SKirill A. Shutemov 	pud = pud_offset(p4d, 0);
86ad757b6aSThomas Gleixner 	pmd_table = pmd_offset(pud, 0);
878550eb99SIngo Molnar 
88ad757b6aSThomas Gleixner 	return pmd_table;
89ad757b6aSThomas Gleixner }
90ad757b6aSThomas Gleixner 
91ad757b6aSThomas Gleixner /*
92ad757b6aSThomas Gleixner  * Create a page table and place a pointer to it in a middle page
938550eb99SIngo Molnar  * directory entry:
94ad757b6aSThomas Gleixner  */
95ad757b6aSThomas Gleixner static pte_t * __init one_page_table_init(pmd_t *pmd)
96ad757b6aSThomas Gleixner {
97ad757b6aSThomas Gleixner 	if (!(pmd_val(*pmd) & _PAGE_PRESENT)) {
984e37a890SYinghai Lu 		pte_t *page_table = (pte_t *)alloc_low_page();
99ad757b6aSThomas Gleixner 
1006944a9c8SJeremy Fitzhardinge 		paravirt_alloc_pte(&init_mm, __pa(page_table) >> PAGE_SHIFT);
101ad757b6aSThomas Gleixner 		set_pmd(pmd, __pmd(__pa(page_table) | _PAGE_TABLE));
102ad757b6aSThomas Gleixner 		BUG_ON(page_table != pte_offset_kernel(pmd, 0));
103ad757b6aSThomas Gleixner 	}
104ad757b6aSThomas Gleixner 
105ad757b6aSThomas Gleixner 	return pte_offset_kernel(pmd, 0);
106ad757b6aSThomas Gleixner }
107ad757b6aSThomas Gleixner 
108458a3e64STejun Heo pmd_t * __init populate_extra_pmd(unsigned long vaddr)
10911124411STejun Heo {
11011124411STejun Heo 	int pgd_idx = pgd_index(vaddr);
11111124411STejun Heo 	int pmd_idx = pmd_index(vaddr);
112458a3e64STejun Heo 
113458a3e64STejun Heo 	return one_md_table_init(swapper_pg_dir + pgd_idx) + pmd_idx;
114458a3e64STejun Heo }
115458a3e64STejun Heo 
116458a3e64STejun Heo pte_t * __init populate_extra_pte(unsigned long vaddr)
117458a3e64STejun Heo {
118458a3e64STejun Heo 	int pte_idx = pte_index(vaddr);
11911124411STejun Heo 	pmd_t *pmd;
12011124411STejun Heo 
121458a3e64STejun Heo 	pmd = populate_extra_pmd(vaddr);
122458a3e64STejun Heo 	return one_page_table_init(pmd) + pte_idx;
12311124411STejun Heo }
12411124411STejun Heo 
125719272c4SYinghai Lu static unsigned long __init
126719272c4SYinghai Lu page_table_range_init_count(unsigned long start, unsigned long end)
127719272c4SYinghai Lu {
128719272c4SYinghai Lu 	unsigned long count = 0;
129719272c4SYinghai Lu #ifdef CONFIG_HIGHMEM
130719272c4SYinghai Lu 	int pmd_idx_kmap_begin = fix_to_virt(FIX_KMAP_END) >> PMD_SHIFT;
131719272c4SYinghai Lu 	int pmd_idx_kmap_end = fix_to_virt(FIX_KMAP_BEGIN) >> PMD_SHIFT;
132719272c4SYinghai Lu 	int pgd_idx, pmd_idx;
133719272c4SYinghai Lu 	unsigned long vaddr;
134719272c4SYinghai Lu 
135719272c4SYinghai Lu 	if (pmd_idx_kmap_begin == pmd_idx_kmap_end)
136719272c4SYinghai Lu 		return 0;
137719272c4SYinghai Lu 
138719272c4SYinghai Lu 	vaddr = start;
139719272c4SYinghai Lu 	pgd_idx = pgd_index(vaddr);
1409962eea9SMinfei Huang 	pmd_idx = pmd_index(vaddr);
141719272c4SYinghai Lu 
142719272c4SYinghai Lu 	for ( ; (pgd_idx < PTRS_PER_PGD) && (vaddr != end); pgd_idx++) {
143719272c4SYinghai Lu 		for (; (pmd_idx < PTRS_PER_PMD) && (vaddr != end);
144719272c4SYinghai Lu 							pmd_idx++) {
145719272c4SYinghai Lu 			if ((vaddr >> PMD_SHIFT) >= pmd_idx_kmap_begin &&
146719272c4SYinghai Lu 			    (vaddr >> PMD_SHIFT) <= pmd_idx_kmap_end)
147719272c4SYinghai Lu 				count++;
148719272c4SYinghai Lu 			vaddr += PMD_SIZE;
149719272c4SYinghai Lu 		}
150719272c4SYinghai Lu 		pmd_idx = 0;
151719272c4SYinghai Lu 	}
152719272c4SYinghai Lu #endif
153719272c4SYinghai Lu 	return count;
154719272c4SYinghai Lu }
155719272c4SYinghai Lu 
156a3c6018eSJan Beulich static pte_t *__init page_table_kmap_check(pte_t *pte, pmd_t *pmd,
157719272c4SYinghai Lu 					   unsigned long vaddr, pte_t *lastpte,
158719272c4SYinghai Lu 					   void **adr)
159a3c6018eSJan Beulich {
160a3c6018eSJan Beulich #ifdef CONFIG_HIGHMEM
161a3c6018eSJan Beulich 	/*
162a3c6018eSJan Beulich 	 * Something (early fixmap) may already have put a pte
163a3c6018eSJan Beulich 	 * page here, which causes the page table allocation
164a3c6018eSJan Beulich 	 * to become nonlinear. Attempt to fix it, and if it
165a3c6018eSJan Beulich 	 * is still nonlinear then we have to bug.
166a3c6018eSJan Beulich 	 */
167a3c6018eSJan Beulich 	int pmd_idx_kmap_begin = fix_to_virt(FIX_KMAP_END) >> PMD_SHIFT;
168a3c6018eSJan Beulich 	int pmd_idx_kmap_end = fix_to_virt(FIX_KMAP_BEGIN) >> PMD_SHIFT;
169a3c6018eSJan Beulich 
170a3c6018eSJan Beulich 	if (pmd_idx_kmap_begin != pmd_idx_kmap_end
171a3c6018eSJan Beulich 	    && (vaddr >> PMD_SHIFT) >= pmd_idx_kmap_begin
172719272c4SYinghai Lu 	    && (vaddr >> PMD_SHIFT) <= pmd_idx_kmap_end) {
173a3c6018eSJan Beulich 		pte_t *newpte;
174a3c6018eSJan Beulich 		int i;
175a3c6018eSJan Beulich 
176c464573cSPekka Enberg 		BUG_ON(after_bootmem);
177719272c4SYinghai Lu 		newpte = *adr;
178a3c6018eSJan Beulich 		for (i = 0; i < PTRS_PER_PTE; i++)
179a3c6018eSJan Beulich 			set_pte(newpte + i, pte[i]);
180719272c4SYinghai Lu 		*adr = (void *)(((unsigned long)(*adr)) + PAGE_SIZE);
181a3c6018eSJan Beulich 
182a3c6018eSJan Beulich 		paravirt_alloc_pte(&init_mm, __pa(newpte) >> PAGE_SHIFT);
183a3c6018eSJan Beulich 		set_pmd(pmd, __pmd(__pa(newpte)|_PAGE_TABLE));
184a3c6018eSJan Beulich 		BUG_ON(newpte != pte_offset_kernel(pmd, 0));
185a3c6018eSJan Beulich 		__flush_tlb_all();
186a3c6018eSJan Beulich 
187a3c6018eSJan Beulich 		paravirt_release_pte(__pa(pte) >> PAGE_SHIFT);
188a3c6018eSJan Beulich 		pte = newpte;
189a3c6018eSJan Beulich 	}
190a3c6018eSJan Beulich 	BUG_ON(vaddr < fix_to_virt(FIX_KMAP_BEGIN - 1)
191a3c6018eSJan Beulich 	       && vaddr > fix_to_virt(FIX_KMAP_END)
192a3c6018eSJan Beulich 	       && lastpte && lastpte + PTRS_PER_PTE != pte);
193a3c6018eSJan Beulich #endif
194a3c6018eSJan Beulich 	return pte;
195a3c6018eSJan Beulich }
196a3c6018eSJan Beulich 
197ad757b6aSThomas Gleixner /*
198ad757b6aSThomas Gleixner  * This function initializes a certain range of kernel virtual memory
199ad757b6aSThomas Gleixner  * with new bootmem page tables, everywhere page tables are missing in
200ad757b6aSThomas Gleixner  * the given range.
2018550eb99SIngo Molnar  *
202ad757b6aSThomas Gleixner  * NOTE: The pagetables are allocated contiguous on the physical space
203ad757b6aSThomas Gleixner  * so we can cache the place of the first one and move around without
204ad757b6aSThomas Gleixner  * checking the pgd every time.
205ad757b6aSThomas Gleixner  */
2068550eb99SIngo Molnar static void __init
2078550eb99SIngo Molnar page_table_range_init(unsigned long start, unsigned long end, pgd_t *pgd_base)
208ad757b6aSThomas Gleixner {
209ad757b6aSThomas Gleixner 	int pgd_idx, pmd_idx;
210ad757b6aSThomas Gleixner 	unsigned long vaddr;
2118550eb99SIngo Molnar 	pgd_t *pgd;
2128550eb99SIngo Molnar 	pmd_t *pmd;
213a3c6018eSJan Beulich 	pte_t *pte = NULL;
214719272c4SYinghai Lu 	unsigned long count = page_table_range_init_count(start, end);
215719272c4SYinghai Lu 	void *adr = NULL;
216719272c4SYinghai Lu 
217719272c4SYinghai Lu 	if (count)
218719272c4SYinghai Lu 		adr = alloc_low_pages(count);
219ad757b6aSThomas Gleixner 
220ad757b6aSThomas Gleixner 	vaddr = start;
221ad757b6aSThomas Gleixner 	pgd_idx = pgd_index(vaddr);
222ad757b6aSThomas Gleixner 	pmd_idx = pmd_index(vaddr);
223ad757b6aSThomas Gleixner 	pgd = pgd_base + pgd_idx;
224ad757b6aSThomas Gleixner 
225ad757b6aSThomas Gleixner 	for ( ; (pgd_idx < PTRS_PER_PGD) && (vaddr != end); pgd++, pgd_idx++) {
226ad757b6aSThomas Gleixner 		pmd = one_md_table_init(pgd);
227ad757b6aSThomas Gleixner 		pmd = pmd + pmd_index(vaddr);
2288550eb99SIngo Molnar 		for (; (pmd_idx < PTRS_PER_PMD) && (vaddr != end);
2298550eb99SIngo Molnar 							pmd++, pmd_idx++) {
230a3c6018eSJan Beulich 			pte = page_table_kmap_check(one_page_table_init(pmd),
231719272c4SYinghai Lu 						    pmd, vaddr, pte, &adr);
232ad757b6aSThomas Gleixner 
233ad757b6aSThomas Gleixner 			vaddr += PMD_SIZE;
234ad757b6aSThomas Gleixner 		}
235ad757b6aSThomas Gleixner 		pmd_idx = 0;
236ad757b6aSThomas Gleixner 	}
237ad757b6aSThomas Gleixner }
238ad757b6aSThomas Gleixner 
239ad757b6aSThomas Gleixner static inline int is_kernel_text(unsigned long addr)
240ad757b6aSThomas Gleixner {
2415bd5a452SMatthieu Castet 	if (addr >= (unsigned long)_text && addr <= (unsigned long)__init_end)
242ad757b6aSThomas Gleixner 		return 1;
243ad757b6aSThomas Gleixner 	return 0;
244ad757b6aSThomas Gleixner }
245ad757b6aSThomas Gleixner 
246ad757b6aSThomas Gleixner /*
247ad757b6aSThomas Gleixner  * This maps the physical memory to kernel virtual address space, a total
248ad757b6aSThomas Gleixner  * of max_low_pfn pages, by creating page tables starting from address
2498550eb99SIngo Molnar  * PAGE_OFFSET:
250ad757b6aSThomas Gleixner  */
251e53fb04fSPekka Enberg unsigned long __init
252e53fb04fSPekka Enberg kernel_physical_mapping_init(unsigned long start,
253e53fb04fSPekka Enberg 			     unsigned long end,
254e53fb04fSPekka Enberg 			     unsigned long page_size_mask)
255ad757b6aSThomas Gleixner {
256e53fb04fSPekka Enberg 	int use_pse = page_size_mask == (1<<PG_LEVEL_2M);
257c1fd1b43SPekka Enberg 	unsigned long last_map_addr = end;
258e53fb04fSPekka Enberg 	unsigned long start_pfn, end_pfn;
259e7179853SPekka Enberg 	pgd_t *pgd_base = swapper_pg_dir;
2608550eb99SIngo Molnar 	int pgd_idx, pmd_idx, pte_ofs;
261ad757b6aSThomas Gleixner 	unsigned long pfn;
262ad757b6aSThomas Gleixner 	pgd_t *pgd;
263ad757b6aSThomas Gleixner 	pmd_t *pmd;
264ad757b6aSThomas Gleixner 	pte_t *pte;
265a2699e47SSuresh Siddha 	unsigned pages_2m, pages_4k;
266a2699e47SSuresh Siddha 	int mapping_iter;
267a2699e47SSuresh Siddha 
268e53fb04fSPekka Enberg 	start_pfn = start >> PAGE_SHIFT;
269e53fb04fSPekka Enberg 	end_pfn = end >> PAGE_SHIFT;
270e53fb04fSPekka Enberg 
271a2699e47SSuresh Siddha 	/*
272a2699e47SSuresh Siddha 	 * First iteration will setup identity mapping using large/small pages
273a2699e47SSuresh Siddha 	 * based on use_pse, with other attributes same as set by
274a2699e47SSuresh Siddha 	 * the early code in head_32.S
275a2699e47SSuresh Siddha 	 *
276a2699e47SSuresh Siddha 	 * Second iteration will setup the appropriate attributes (NX, GLOBAL..)
277a2699e47SSuresh Siddha 	 * as desired for the kernel identity mapping.
278a2699e47SSuresh Siddha 	 *
279a2699e47SSuresh Siddha 	 * This two pass mechanism conforms to the TLB app note which says:
280a2699e47SSuresh Siddha 	 *
281a2699e47SSuresh Siddha 	 *     "Software should not write to a paging-structure entry in a way
282a2699e47SSuresh Siddha 	 *      that would change, for any linear address, both the page size
283a2699e47SSuresh Siddha 	 *      and either the page frame or attributes."
284a2699e47SSuresh Siddha 	 */
285a2699e47SSuresh Siddha 	mapping_iter = 1;
286ad757b6aSThomas Gleixner 
28716bf9226SBorislav Petkov 	if (!boot_cpu_has(X86_FEATURE_PSE))
288a04ad82dSYinghai Lu 		use_pse = 0;
289a04ad82dSYinghai Lu 
290a2699e47SSuresh Siddha repeat:
291a2699e47SSuresh Siddha 	pages_2m = pages_4k = 0;
292a04ad82dSYinghai Lu 	pfn = start_pfn;
293a04ad82dSYinghai Lu 	pgd_idx = pgd_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
294ad757b6aSThomas Gleixner 	pgd = pgd_base + pgd_idx;
295ad757b6aSThomas Gleixner 	for (; pgd_idx < PTRS_PER_PGD; pgd++, pgd_idx++) {
296ad757b6aSThomas Gleixner 		pmd = one_md_table_init(pgd);
2978550eb99SIngo Molnar 
298a04ad82dSYinghai Lu 		if (pfn >= end_pfn)
299a04ad82dSYinghai Lu 			continue;
300a04ad82dSYinghai Lu #ifdef CONFIG_X86_PAE
301a04ad82dSYinghai Lu 		pmd_idx = pmd_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
302a04ad82dSYinghai Lu 		pmd += pmd_idx;
303a04ad82dSYinghai Lu #else
304a04ad82dSYinghai Lu 		pmd_idx = 0;
305a04ad82dSYinghai Lu #endif
306a04ad82dSYinghai Lu 		for (; pmd_idx < PTRS_PER_PMD && pfn < end_pfn;
307f3f20de8SJeremy Fitzhardinge 		     pmd++, pmd_idx++) {
3088550eb99SIngo Molnar 			unsigned int addr = pfn * PAGE_SIZE + PAGE_OFFSET;
309ad757b6aSThomas Gleixner 
3108550eb99SIngo Molnar 			/*
3118550eb99SIngo Molnar 			 * Map with big pages if possible, otherwise
3128550eb99SIngo Molnar 			 * create normal page tables:
3138550eb99SIngo Molnar 			 */
314a04ad82dSYinghai Lu 			if (use_pse) {
3158550eb99SIngo Molnar 				unsigned int addr2;
316f3f20de8SJeremy Fitzhardinge 				pgprot_t prot = PAGE_KERNEL_LARGE;
317a2699e47SSuresh Siddha 				/*
318a2699e47SSuresh Siddha 				 * first pass will use the same initial
319a2699e47SSuresh Siddha 				 * identity mapping attribute + _PAGE_PSE.
320a2699e47SSuresh Siddha 				 */
321a2699e47SSuresh Siddha 				pgprot_t init_prot =
322a2699e47SSuresh Siddha 					__pgprot(PTE_IDENT_ATTR |
323a2699e47SSuresh Siddha 						 _PAGE_PSE);
324f3f20de8SJeremy Fitzhardinge 
325960ddb4fSYinghai Lu 				pfn &= PMD_MASK >> PAGE_SHIFT;
3268550eb99SIngo Molnar 				addr2 = (pfn + PTRS_PER_PTE-1) * PAGE_SIZE +
327f3f20de8SJeremy Fitzhardinge 					PAGE_OFFSET + PAGE_SIZE-1;
328f3f20de8SJeremy Fitzhardinge 
3298550eb99SIngo Molnar 				if (is_kernel_text(addr) ||
3308550eb99SIngo Molnar 				    is_kernel_text(addr2))
331f3f20de8SJeremy Fitzhardinge 					prot = PAGE_KERNEL_LARGE_EXEC;
332f3f20de8SJeremy Fitzhardinge 
333ce0c0e50SAndi Kleen 				pages_2m++;
334a2699e47SSuresh Siddha 				if (mapping_iter == 1)
335a2699e47SSuresh Siddha 					set_pmd(pmd, pfn_pmd(pfn, init_prot));
336a2699e47SSuresh Siddha 				else
337f3f20de8SJeremy Fitzhardinge 					set_pmd(pmd, pfn_pmd(pfn, prot));
338ad757b6aSThomas Gleixner 
339ad757b6aSThomas Gleixner 				pfn += PTRS_PER_PTE;
3408550eb99SIngo Molnar 				continue;
3418550eb99SIngo Molnar 			}
342ad757b6aSThomas Gleixner 			pte = one_page_table_init(pmd);
343ad757b6aSThomas Gleixner 
344a04ad82dSYinghai Lu 			pte_ofs = pte_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
345a04ad82dSYinghai Lu 			pte += pte_ofs;
346a04ad82dSYinghai Lu 			for (; pte_ofs < PTRS_PER_PTE && pfn < end_pfn;
3478550eb99SIngo Molnar 			     pte++, pfn++, pte_ofs++, addr += PAGE_SIZE) {
348f3f20de8SJeremy Fitzhardinge 				pgprot_t prot = PAGE_KERNEL;
349a2699e47SSuresh Siddha 				/*
350a2699e47SSuresh Siddha 				 * first pass will use the same initial
351a2699e47SSuresh Siddha 				 * identity mapping attribute.
352a2699e47SSuresh Siddha 				 */
353a2699e47SSuresh Siddha 				pgprot_t init_prot = __pgprot(PTE_IDENT_ATTR);
354f3f20de8SJeremy Fitzhardinge 
3558550eb99SIngo Molnar 				if (is_kernel_text(addr))
356f3f20de8SJeremy Fitzhardinge 					prot = PAGE_KERNEL_EXEC;
357f3f20de8SJeremy Fitzhardinge 
358ce0c0e50SAndi Kleen 				pages_4k++;
359c1fd1b43SPekka Enberg 				if (mapping_iter == 1) {
360a2699e47SSuresh Siddha 					set_pte(pte, pfn_pte(pfn, init_prot));
361c1fd1b43SPekka Enberg 					last_map_addr = (pfn << PAGE_SHIFT) + PAGE_SIZE;
362c1fd1b43SPekka Enberg 				} else
363f3f20de8SJeremy Fitzhardinge 					set_pte(pte, pfn_pte(pfn, prot));
364ad757b6aSThomas Gleixner 			}
365ad757b6aSThomas Gleixner 		}
366ad757b6aSThomas Gleixner 	}
367a2699e47SSuresh Siddha 	if (mapping_iter == 1) {
368a2699e47SSuresh Siddha 		/*
369a2699e47SSuresh Siddha 		 * update direct mapping page count only in the first
370a2699e47SSuresh Siddha 		 * iteration.
371a2699e47SSuresh Siddha 		 */
372ce0c0e50SAndi Kleen 		update_page_count(PG_LEVEL_2M, pages_2m);
373ce0c0e50SAndi Kleen 		update_page_count(PG_LEVEL_4K, pages_4k);
374a2699e47SSuresh Siddha 
375a2699e47SSuresh Siddha 		/*
376a2699e47SSuresh Siddha 		 * local global flush tlb, which will flush the previous
377a2699e47SSuresh Siddha 		 * mappings present in both small and large page TLB's.
378a2699e47SSuresh Siddha 		 */
379a2699e47SSuresh Siddha 		__flush_tlb_all();
380a2699e47SSuresh Siddha 
381a2699e47SSuresh Siddha 		/*
382a2699e47SSuresh Siddha 		 * Second iteration will set the actual desired PTE attributes.
383a2699e47SSuresh Siddha 		 */
384a2699e47SSuresh Siddha 		mapping_iter = 2;
385a2699e47SSuresh Siddha 		goto repeat;
386a2699e47SSuresh Siddha 	}
387c1fd1b43SPekka Enberg 	return last_map_addr;
388ae531c26SArjan van de Ven }
389ae531c26SArjan van de Ven 
390ad757b6aSThomas Gleixner pte_t *kmap_pte;
391ad757b6aSThomas Gleixner 
3928550eb99SIngo Molnar static inline pte_t *kmap_get_fixmap_pte(unsigned long vaddr)
3938550eb99SIngo Molnar {
394e0c4f675SKirill A. Shutemov 	pgd_t *pgd = pgd_offset_k(vaddr);
395e0c4f675SKirill A. Shutemov 	p4d_t *p4d = p4d_offset(pgd, vaddr);
396e0c4f675SKirill A. Shutemov 	pud_t *pud = pud_offset(p4d, vaddr);
397e0c4f675SKirill A. Shutemov 	pmd_t *pmd = pmd_offset(pud, vaddr);
398e0c4f675SKirill A. Shutemov 	return pte_offset_kernel(pmd, vaddr);
3998550eb99SIngo Molnar }
400ad757b6aSThomas Gleixner 
401ad757b6aSThomas Gleixner static void __init kmap_init(void)
402ad757b6aSThomas Gleixner {
403ad757b6aSThomas Gleixner 	unsigned long kmap_vstart;
404ad757b6aSThomas Gleixner 
4058550eb99SIngo Molnar 	/*
4068550eb99SIngo Molnar 	 * Cache the first kmap pte:
4078550eb99SIngo Molnar 	 */
408ad757b6aSThomas Gleixner 	kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN);
409ad757b6aSThomas Gleixner 	kmap_pte = kmap_get_fixmap_pte(kmap_vstart);
410ad757b6aSThomas Gleixner }
411ad757b6aSThomas Gleixner 
412fd940934SKeith Packard #ifdef CONFIG_HIGHMEM
413ad757b6aSThomas Gleixner static void __init permanent_kmaps_init(pgd_t *pgd_base)
414ad757b6aSThomas Gleixner {
4158550eb99SIngo Molnar 	unsigned long vaddr;
416ad757b6aSThomas Gleixner 	pgd_t *pgd;
417e0c4f675SKirill A. Shutemov 	p4d_t *p4d;
418ad757b6aSThomas Gleixner 	pud_t *pud;
419ad757b6aSThomas Gleixner 	pmd_t *pmd;
420ad757b6aSThomas Gleixner 	pte_t *pte;
421ad757b6aSThomas Gleixner 
422ad757b6aSThomas Gleixner 	vaddr = PKMAP_BASE;
423ad757b6aSThomas Gleixner 	page_table_range_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, pgd_base);
424ad757b6aSThomas Gleixner 
425ad757b6aSThomas Gleixner 	pgd = swapper_pg_dir + pgd_index(vaddr);
426e0c4f675SKirill A. Shutemov 	p4d = p4d_offset(pgd, vaddr);
427e0c4f675SKirill A. Shutemov 	pud = pud_offset(p4d, vaddr);
428ad757b6aSThomas Gleixner 	pmd = pmd_offset(pud, vaddr);
429ad757b6aSThomas Gleixner 	pte = pte_offset_kernel(pmd, vaddr);
430ad757b6aSThomas Gleixner 	pkmap_page_table = pte;
431ad757b6aSThomas Gleixner }
432ad757b6aSThomas Gleixner 
4331d931264SYinghai Lu void __init add_highpages_with_active_regions(int nid,
4341d931264SYinghai Lu 			 unsigned long start_pfn, unsigned long end_pfn)
435b5bc6c0eSYinghai Lu {
4368a9ca34cSTejun Heo 	phys_addr_t start, end;
4378a9ca34cSTejun Heo 	u64 i;
4381d931264SYinghai Lu 
439fc6daaf9STony Luck 	for_each_free_mem_range(i, nid, MEMBLOCK_NONE, &start, &end, NULL) {
4408a9ca34cSTejun Heo 		unsigned long pfn = clamp_t(unsigned long, PFN_UP(start),
4418a9ca34cSTejun Heo 					    start_pfn, end_pfn);
4428a9ca34cSTejun Heo 		unsigned long e_pfn = clamp_t(unsigned long, PFN_DOWN(end),
4438a9ca34cSTejun Heo 					      start_pfn, end_pfn);
4448a9ca34cSTejun Heo 		for ( ; pfn < e_pfn; pfn++)
4458a9ca34cSTejun Heo 			if (pfn_valid(pfn))
4465e7ccf86SJiang Liu 				free_highmem_page(pfn_to_page(pfn));
447b5bc6c0eSYinghai Lu 	}
448ad757b6aSThomas Gleixner }
449ad757b6aSThomas Gleixner #else
450e8e32326SIngo Brueckl static inline void permanent_kmaps_init(pgd_t *pgd_base)
451e8e32326SIngo Brueckl {
452e8e32326SIngo Brueckl }
453ad757b6aSThomas Gleixner #endif /* CONFIG_HIGHMEM */
454ad757b6aSThomas Gleixner 
455945fd17aSThomas Gleixner void __init sync_initial_page_table(void)
456945fd17aSThomas Gleixner {
457945fd17aSThomas Gleixner 	clone_pgd_range(initial_page_table + KERNEL_PGD_BOUNDARY,
458945fd17aSThomas Gleixner 			swapper_pg_dir     + KERNEL_PGD_BOUNDARY,
459945fd17aSThomas Gleixner 			KERNEL_PGD_PTRS);
460945fd17aSThomas Gleixner 
461945fd17aSThomas Gleixner 	/*
462945fd17aSThomas Gleixner 	 * sync back low identity map too.  It is used for example
463945fd17aSThomas Gleixner 	 * in the 32-bit EFI stub.
464945fd17aSThomas Gleixner 	 */
465945fd17aSThomas Gleixner 	clone_pgd_range(initial_page_table,
466945fd17aSThomas Gleixner 			swapper_pg_dir     + KERNEL_PGD_BOUNDARY,
467945fd17aSThomas Gleixner 			min(KERNEL_PGD_PTRS, KERNEL_PGD_BOUNDARY));
468945fd17aSThomas Gleixner }
469945fd17aSThomas Gleixner 
4707737b215SAttilio Rao void __init native_pagetable_init(void)
471ad757b6aSThomas Gleixner {
472551889a6SIan Campbell 	unsigned long pfn, va;
47373090f89SAttilio Rao 	pgd_t *pgd, *base = swapper_pg_dir;
474e0c4f675SKirill A. Shutemov 	p4d_t *p4d;
475551889a6SIan Campbell 	pud_t *pud;
476551889a6SIan Campbell 	pmd_t *pmd;
477551889a6SIan Campbell 	pte_t *pte;
478ad757b6aSThomas Gleixner 
479ad757b6aSThomas Gleixner 	/*
480551889a6SIan Campbell 	 * Remove any mappings which extend past the end of physical
48111ed9e92SYinghai Lu 	 * memory from the boot time page table.
48211ed9e92SYinghai Lu 	 * In virtual address space, we should have at least two pages
48311ed9e92SYinghai Lu 	 * from VMALLOC_END to pkmap or fixmap according to VMALLOC_END
48411ed9e92SYinghai Lu 	 * definition. And max_low_pfn is set to VMALLOC_END physical
48511ed9e92SYinghai Lu 	 * address. If initial memory mapping is doing right job, we
48611ed9e92SYinghai Lu 	 * should have pte used near max_low_pfn or one pmd is not present.
487ad757b6aSThomas Gleixner 	 */
48811ed9e92SYinghai Lu 	for (pfn = max_low_pfn; pfn < 1<<(32-PAGE_SHIFT); pfn++) {
489551889a6SIan Campbell 		va = PAGE_OFFSET + (pfn<<PAGE_SHIFT);
490551889a6SIan Campbell 		pgd = base + pgd_index(va);
491551889a6SIan Campbell 		if (!pgd_present(*pgd))
492551889a6SIan Campbell 			break;
493ad757b6aSThomas Gleixner 
494e0c4f675SKirill A. Shutemov 		p4d = p4d_offset(pgd, va);
495e0c4f675SKirill A. Shutemov 		pud = pud_offset(p4d, va);
496551889a6SIan Campbell 		pmd = pmd_offset(pud, va);
497551889a6SIan Campbell 		if (!pmd_present(*pmd))
498551889a6SIan Campbell 			break;
499551889a6SIan Campbell 
50011ed9e92SYinghai Lu 		/* should not be large page here */
50111ed9e92SYinghai Lu 		if (pmd_large(*pmd)) {
50211ed9e92SYinghai Lu 			pr_warn("try to clear pte for ram above max_low_pfn: pfn: %lx pmd: %p pmd phys: %lx, but pmd is big page and is not using pte !\n",
50311ed9e92SYinghai Lu 				pfn, pmd, __pa(pmd));
50411ed9e92SYinghai Lu 			BUG_ON(1);
50511ed9e92SYinghai Lu 		}
50611ed9e92SYinghai Lu 
507551889a6SIan Campbell 		pte = pte_offset_kernel(pmd, va);
508551889a6SIan Campbell 		if (!pte_present(*pte))
509551889a6SIan Campbell 			break;
510551889a6SIan Campbell 
51111ed9e92SYinghai Lu 		printk(KERN_DEBUG "clearing pte for ram above max_low_pfn: pfn: %lx pmd: %p pmd phys: %lx pte: %p pte phys: %lx\n",
51211ed9e92SYinghai Lu 				pfn, pmd, __pa(pmd), pte, __pa(pte));
513551889a6SIan Campbell 		pte_clear(NULL, va, pte);
514551889a6SIan Campbell 	}
5156944a9c8SJeremy Fitzhardinge 	paravirt_alloc_pmd(&init_mm, __pa(base) >> PAGE_SHIFT);
516843b8ed2SAttilio Rao 	paging_init();
517ad757b6aSThomas Gleixner }
518ad757b6aSThomas Gleixner 
519ad757b6aSThomas Gleixner /*
520ad757b6aSThomas Gleixner  * Build a proper pagetable for the kernel mappings.  Up until this
521ad757b6aSThomas Gleixner  * point, we've been running on some set of pagetables constructed by
522ad757b6aSThomas Gleixner  * the boot process.
523ad757b6aSThomas Gleixner  *
524ad757b6aSThomas Gleixner  * If we're booting on native hardware, this will be a pagetable
525551889a6SIan Campbell  * constructed in arch/x86/kernel/head_32.S.  The root of the
526551889a6SIan Campbell  * pagetable will be swapper_pg_dir.
527ad757b6aSThomas Gleixner  *
528ad757b6aSThomas Gleixner  * If we're booting paravirtualized under a hypervisor, then there are
529ad757b6aSThomas Gleixner  * more options: we may already be running PAE, and the pagetable may
530ad757b6aSThomas Gleixner  * or may not be based in swapper_pg_dir.  In any case,
5317737b215SAttilio Rao  * paravirt_pagetable_init() will set up swapper_pg_dir
532ad757b6aSThomas Gleixner  * appropriately for the rest of the initialization to work.
533ad757b6aSThomas Gleixner  *
534ad757b6aSThomas Gleixner  * In general, pagetable_init() assumes that the pagetable may already
535ad757b6aSThomas Gleixner  * be partially populated, and so it avoids stomping on any existing
536ad757b6aSThomas Gleixner  * mappings.
537ad757b6aSThomas Gleixner  */
538f765090aSPekka Enberg void __init early_ioremap_page_table_range_init(void)
539ad757b6aSThomas Gleixner {
540e7179853SPekka Enberg 	pgd_t *pgd_base = swapper_pg_dir;
5418550eb99SIngo Molnar 	unsigned long vaddr, end;
542ad757b6aSThomas Gleixner 
543ad757b6aSThomas Gleixner 	/*
544ad757b6aSThomas Gleixner 	 * Fixed mappings, only the page table structure has to be
545ad757b6aSThomas Gleixner 	 * created - mappings will be set by set_fixmap():
546ad757b6aSThomas Gleixner 	 */
547ad757b6aSThomas Gleixner 	vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
548ad757b6aSThomas Gleixner 	end = (FIXADDR_TOP + PMD_SIZE - 1) & PMD_MASK;
549ad757b6aSThomas Gleixner 	page_table_range_init(vaddr, end, pgd_base);
550beacfaacSHuang, Ying 	early_ioremap_reset();
551e7b37895SYinghai Lu }
552e7b37895SYinghai Lu 
553e7b37895SYinghai Lu static void __init pagetable_init(void)
554e7b37895SYinghai Lu {
555e7b37895SYinghai Lu 	pgd_t *pgd_base = swapper_pg_dir;
556e7b37895SYinghai Lu 
557ad757b6aSThomas Gleixner 	permanent_kmaps_init(pgd_base);
558ad757b6aSThomas Gleixner }
559ad757b6aSThomas Gleixner 
5608a57f484SDave Hansen #define DEFAULT_PTE_MASK ~(_PAGE_NX | _PAGE_GLOBAL)
5618a57f484SDave Hansen /* Bits supported by the hardware: */
5628a57f484SDave Hansen pteval_t __supported_pte_mask __read_mostly = DEFAULT_PTE_MASK;
5638a57f484SDave Hansen /* Bits allowed in normal kernel mappings: */
5648a57f484SDave Hansen pteval_t __default_kernel_pte_mask __read_mostly = DEFAULT_PTE_MASK;
5656fdc05d4SJeremy Fitzhardinge EXPORT_SYMBOL_GPL(__supported_pte_mask);
5668a57f484SDave Hansen /* Used in PAGE_KERNEL_* macros which are reasonably used out-of-tree: */
5678a57f484SDave Hansen EXPORT_SYMBOL(__default_kernel_pte_mask);
5686fdc05d4SJeremy Fitzhardinge 
56990d967e0SYinghai Lu /* user-defined highmem size */
57090d967e0SYinghai Lu static unsigned int highmem_pages = -1;
57190d967e0SYinghai Lu 
57290d967e0SYinghai Lu /*
57390d967e0SYinghai Lu  * highmem=size forces highmem to be exactly 'size' bytes.
57490d967e0SYinghai Lu  * This works even on boxes that have no highmem otherwise.
57590d967e0SYinghai Lu  * This also works to reduce highmem size on bigger boxes.
57690d967e0SYinghai Lu  */
57790d967e0SYinghai Lu static int __init parse_highmem(char *arg)
57890d967e0SYinghai Lu {
57990d967e0SYinghai Lu 	if (!arg)
58090d967e0SYinghai Lu 		return -EINVAL;
58190d967e0SYinghai Lu 
58290d967e0SYinghai Lu 	highmem_pages = memparse(arg, &arg) >> PAGE_SHIFT;
58390d967e0SYinghai Lu 	return 0;
58490d967e0SYinghai Lu }
58590d967e0SYinghai Lu early_param("highmem", parse_highmem);
58690d967e0SYinghai Lu 
5874769843bSIngo Molnar #define MSG_HIGHMEM_TOO_BIG \
5884769843bSIngo Molnar 	"highmem size (%luMB) is bigger than pages available (%luMB)!\n"
5894769843bSIngo Molnar 
5904769843bSIngo Molnar #define MSG_LOWMEM_TOO_SMALL \
5914769843bSIngo Molnar 	"highmem size (%luMB) results in <64MB lowmem, ignoring it!\n"
59290d967e0SYinghai Lu /*
5934769843bSIngo Molnar  * All of RAM fits into lowmem - but if user wants highmem
5944769843bSIngo Molnar  * artificially via the highmem=x boot parameter then create
5954769843bSIngo Molnar  * it:
59690d967e0SYinghai Lu  */
597f836e35aSYinghai Lu static void __init lowmem_pfn_init(void)
59890d967e0SYinghai Lu {
599346cafecSYinghai Lu 	/* max_low_pfn is 0, we already have early_res support */
60090d967e0SYinghai Lu 	max_low_pfn = max_pfn;
601d88316c2SIngo Molnar 
6024769843bSIngo Molnar 	if (highmem_pages == -1)
6034769843bSIngo Molnar 		highmem_pages = 0;
6044769843bSIngo Molnar #ifdef CONFIG_HIGHMEM
6054769843bSIngo Molnar 	if (highmem_pages >= max_pfn) {
6064769843bSIngo Molnar 		printk(KERN_ERR MSG_HIGHMEM_TOO_BIG,
6074769843bSIngo Molnar 			pages_to_mb(highmem_pages), pages_to_mb(max_pfn));
6084769843bSIngo Molnar 		highmem_pages = 0;
6094769843bSIngo Molnar 	}
6104769843bSIngo Molnar 	if (highmem_pages) {
6114769843bSIngo Molnar 		if (max_low_pfn - highmem_pages < 64*1024*1024/PAGE_SIZE) {
6124769843bSIngo Molnar 			printk(KERN_ERR MSG_LOWMEM_TOO_SMALL,
6134769843bSIngo Molnar 				pages_to_mb(highmem_pages));
6144769843bSIngo Molnar 			highmem_pages = 0;
6154769843bSIngo Molnar 		}
6164769843bSIngo Molnar 		max_low_pfn -= highmem_pages;
6174769843bSIngo Molnar 	}
6184769843bSIngo Molnar #else
6194769843bSIngo Molnar 	if (highmem_pages)
6204769843bSIngo Molnar 		printk(KERN_ERR "ignoring highmem size on non-highmem kernel!\n");
6214769843bSIngo Molnar #endif
6224769843bSIngo Molnar }
6234769843bSIngo Molnar 
6244769843bSIngo Molnar #define MSG_HIGHMEM_TOO_SMALL \
6254769843bSIngo Molnar 	"only %luMB highmem pages available, ignoring highmem size of %luMB!\n"
6264769843bSIngo Molnar 
6274769843bSIngo Molnar #define MSG_HIGHMEM_TRIMMED \
6284769843bSIngo Molnar 	"Warning: only 4GB will be used. Use a HIGHMEM64G enabled kernel!\n"
6294769843bSIngo Molnar /*
6304769843bSIngo Molnar  * We have more RAM than fits into lowmem - we try to put it into
6314769843bSIngo Molnar  * highmem, also taking the highmem=x boot parameter into account:
6324769843bSIngo Molnar  */
633f836e35aSYinghai Lu static void __init highmem_pfn_init(void)
6344769843bSIngo Molnar {
635d88316c2SIngo Molnar 	max_low_pfn = MAXMEM_PFN;
636d88316c2SIngo Molnar 
63790d967e0SYinghai Lu 	if (highmem_pages == -1)
63890d967e0SYinghai Lu 		highmem_pages = max_pfn - MAXMEM_PFN;
6394769843bSIngo Molnar 
64090d967e0SYinghai Lu 	if (highmem_pages + MAXMEM_PFN < max_pfn)
64190d967e0SYinghai Lu 		max_pfn = MAXMEM_PFN + highmem_pages;
6424769843bSIngo Molnar 
64390d967e0SYinghai Lu 	if (highmem_pages + MAXMEM_PFN > max_pfn) {
6444769843bSIngo Molnar 		printk(KERN_WARNING MSG_HIGHMEM_TOO_SMALL,
64590d967e0SYinghai Lu 			pages_to_mb(max_pfn - MAXMEM_PFN),
64690d967e0SYinghai Lu 			pages_to_mb(highmem_pages));
64790d967e0SYinghai Lu 		highmem_pages = 0;
64890d967e0SYinghai Lu 	}
64990d967e0SYinghai Lu #ifndef CONFIG_HIGHMEM
65090d967e0SYinghai Lu 	/* Maximum memory usable is what is directly addressable */
6514769843bSIngo Molnar 	printk(KERN_WARNING "Warning only %ldMB will be used.\n", MAXMEM>>20);
65290d967e0SYinghai Lu 	if (max_pfn > MAX_NONPAE_PFN)
6534769843bSIngo Molnar 		printk(KERN_WARNING "Use a HIGHMEM64G enabled kernel.\n");
65490d967e0SYinghai Lu 	else
65590d967e0SYinghai Lu 		printk(KERN_WARNING "Use a HIGHMEM enabled kernel.\n");
65690d967e0SYinghai Lu 	max_pfn = MAXMEM_PFN;
65790d967e0SYinghai Lu #else /* !CONFIG_HIGHMEM */
65890d967e0SYinghai Lu #ifndef CONFIG_HIGHMEM64G
65990d967e0SYinghai Lu 	if (max_pfn > MAX_NONPAE_PFN) {
66090d967e0SYinghai Lu 		max_pfn = MAX_NONPAE_PFN;
6614769843bSIngo Molnar 		printk(KERN_WARNING MSG_HIGHMEM_TRIMMED);
66290d967e0SYinghai Lu 	}
66390d967e0SYinghai Lu #endif /* !CONFIG_HIGHMEM64G */
66490d967e0SYinghai Lu #endif /* !CONFIG_HIGHMEM */
66590d967e0SYinghai Lu }
6664769843bSIngo Molnar 
66790d967e0SYinghai Lu /*
66890d967e0SYinghai Lu  * Determine low and high memory ranges:
66990d967e0SYinghai Lu  */
67090d967e0SYinghai Lu void __init find_low_pfn_range(void)
67190d967e0SYinghai Lu {
67290d967e0SYinghai Lu 	/* it could update max_pfn */
67390d967e0SYinghai Lu 
674d88316c2SIngo Molnar 	if (max_pfn <= MAXMEM_PFN)
6754769843bSIngo Molnar 		lowmem_pfn_init();
676d88316c2SIngo Molnar 	else
677d88316c2SIngo Molnar 		highmem_pfn_init();
67890d967e0SYinghai Lu }
67990d967e0SYinghai Lu 
680b2ac82a0SYinghai Lu #ifndef CONFIG_NEED_MULTIPLE_NODES
681d8fc3afcSTejun Heo void __init initmem_init(void)
682b2ac82a0SYinghai Lu {
683b2ac82a0SYinghai Lu #ifdef CONFIG_HIGHMEM
684b2ac82a0SYinghai Lu 	highstart_pfn = highend_pfn = max_pfn;
685b2ac82a0SYinghai Lu 	if (max_pfn > max_low_pfn)
686b2ac82a0SYinghai Lu 		highstart_pfn = max_low_pfn;
687b2ac82a0SYinghai Lu 	printk(KERN_NOTICE "%ldMB HIGHMEM available.\n",
688b2ac82a0SYinghai Lu 		pages_to_mb(highend_pfn - highstart_pfn));
689b2ac82a0SYinghai Lu 	high_memory = (void *) __va(highstart_pfn * PAGE_SIZE - 1) + 1;
690b2ac82a0SYinghai Lu #else
691b2ac82a0SYinghai Lu 	high_memory = (void *) __va(max_low_pfn * PAGE_SIZE - 1) + 1;
692b2ac82a0SYinghai Lu #endif
6930608f70cSTejun Heo 
694d7dc899aSStefan Agner 	memblock_set_node(0, PHYS_ADDR_MAX, &memblock.memory, 0);
6950608f70cSTejun Heo 	sparse_memory_present_with_active_regions(0);
6960608f70cSTejun Heo 
697b2ac82a0SYinghai Lu #ifdef CONFIG_FLATMEM
69846a84132SJiang Liu 	max_mapnr = IS_ENABLED(CONFIG_HIGHMEM) ? highend_pfn : max_low_pfn;
699b2ac82a0SYinghai Lu #endif
700dc16ecf7SJeremy Fitzhardinge 	__vmalloc_start_set = true;
701dc16ecf7SJeremy Fitzhardinge 
702b2ac82a0SYinghai Lu 	printk(KERN_NOTICE "%ldMB LOWMEM available.\n",
703b2ac82a0SYinghai Lu 			pages_to_mb(max_low_pfn));
704b2ac82a0SYinghai Lu 
705b2ac82a0SYinghai Lu 	setup_bootmem_allocator();
706b2ac82a0SYinghai Lu }
707cb95a13aSYinghai Lu #endif /* !CONFIG_NEED_MULTIPLE_NODES */
708b2ac82a0SYinghai Lu 
709b2ac82a0SYinghai Lu void __init setup_bootmem_allocator(void)
710b2ac82a0SYinghai Lu {
711b2ac82a0SYinghai Lu 	printk(KERN_INFO "  mapped low ram: 0 - %08lx\n",
712b2ac82a0SYinghai Lu 		 max_pfn_mapped<<PAGE_SHIFT);
713fc5efe39SYinghai Lu 	printk(KERN_INFO "  low ram: 0 - %08lx\n", max_low_pfn<<PAGE_SHIFT);
7144e29684cSYinghai Lu }
7154e29684cSYinghai Lu 
716a04ad82dSYinghai Lu /*
717ad757b6aSThomas Gleixner  * paging_init() sets up the page tables - note that the first 8MB are
718ad757b6aSThomas Gleixner  * already mapped by head.S.
719ad757b6aSThomas Gleixner  *
720ad757b6aSThomas Gleixner  * This routines also unmaps the page at virtual kernel address 0, so
721ad757b6aSThomas Gleixner  * that we can trap those pesky NULL-reference errors in the kernel.
722ad757b6aSThomas Gleixner  */
723ad757b6aSThomas Gleixner void __init paging_init(void)
724ad757b6aSThomas Gleixner {
725ad757b6aSThomas Gleixner 	pagetable_init();
726ad757b6aSThomas Gleixner 
727ad757b6aSThomas Gleixner 	__flush_tlb_all();
728ad757b6aSThomas Gleixner 
729ad757b6aSThomas Gleixner 	kmap_init();
73011cd0bc1SYinghai Lu 
73111cd0bc1SYinghai Lu 	/*
73211cd0bc1SYinghai Lu 	 * NOTE: at this point the bootmem allocator is fully available.
73311cd0bc1SYinghai Lu 	 */
734c10d1e26SAndres Salomon 	olpc_dt_build_devicetree();
735797390d8STejun Heo 	sparse_memory_present_with_active_regions(MAX_NUMNODES);
73611cd0bc1SYinghai Lu 	sparse_init();
73711cd0bc1SYinghai Lu 	zone_sizes_init();
738ad757b6aSThomas Gleixner }
739ad757b6aSThomas Gleixner 
740ad757b6aSThomas Gleixner /*
741ad757b6aSThomas Gleixner  * Test if the WP bit works in supervisor mode. It isn't supported on 386's
742f7f17a67SDmitri Vorobiev  * and also on some strange 486's. All 586+'s are OK. This used to involve
743f7f17a67SDmitri Vorobiev  * black magic jumps to work around some nasty CPU bugs, but fortunately the
744f7f17a67SDmitri Vorobiev  * switch to using exceptions got rid of all that.
745ad757b6aSThomas Gleixner  */
746ad757b6aSThomas Gleixner static void __init test_wp_bit(void)
747ad757b6aSThomas Gleixner {
7484af17110SAndy Lutomirski 	char z = 0;
7496415813bSMathias Krause 
750952a6c2cSBorislav Petkov 	printk(KERN_INFO "Checking if this processor honours the WP bit even in supervisor mode...");
751ad757b6aSThomas Gleixner 
7524af17110SAndy Lutomirski 	__set_fixmap(FIX_WP_TEST, __pa_symbol(empty_zero_page), PAGE_KERNEL_RO);
753ad757b6aSThomas Gleixner 
754952a6c2cSBorislav Petkov 	if (probe_kernel_write((char *)fix_to_virt(FIX_WP_TEST), &z, 1)) {
755952a6c2cSBorislav Petkov 		clear_fixmap(FIX_WP_TEST);
756952a6c2cSBorislav Petkov 		printk(KERN_CONT "Ok.\n");
757952a6c2cSBorislav Petkov 		return;
758ad757b6aSThomas Gleixner 	}
7594af17110SAndy Lutomirski 
760952a6c2cSBorislav Petkov 	printk(KERN_CONT "No.\n");
761952a6c2cSBorislav Petkov 	panic("Linux doesn't support CPUs with broken WP.");
762ad757b6aSThomas Gleixner }
763ad757b6aSThomas Gleixner 
764ad757b6aSThomas Gleixner void __init mem_init(void)
765ad757b6aSThomas Gleixner {
766cfb80c9eSJeremy Fitzhardinge 	pci_iommu_alloc();
767cfb80c9eSJeremy Fitzhardinge 
768ad757b6aSThomas Gleixner #ifdef CONFIG_FLATMEM
769ad757b6aSThomas Gleixner 	BUG_ON(!mem_map);
770ad757b6aSThomas Gleixner #endif
771855c743aSStanislaw Gruszka 	/*
772855c743aSStanislaw Gruszka 	 * With CONFIG_DEBUG_PAGEALLOC initialization of highmem pages has to
773c6ffc5caSMike Rapoport 	 * be done before memblock_free_all(). Memblock use free low memory for
774855c743aSStanislaw Gruszka 	 * temporary data (see find_range_array()) and for this purpose can use
775855c743aSStanislaw Gruszka 	 * pages that was already passed to the buddy allocator, hence marked as
776855c743aSStanislaw Gruszka 	 * not accessible in the page tables when compiled with
777855c743aSStanislaw Gruszka 	 * CONFIG_DEBUG_PAGEALLOC. Otherwise order of initialization is not
778855c743aSStanislaw Gruszka 	 * important here.
779855c743aSStanislaw Gruszka 	 */
780855c743aSStanislaw Gruszka 	set_highmem_pages_init();
781855c743aSStanislaw Gruszka 
782ad757b6aSThomas Gleixner 	/* this will put all low memory onto the freelists */
783c6ffc5caSMike Rapoport 	memblock_free_all();
784ad757b6aSThomas Gleixner 
7854e37a890SYinghai Lu 	after_bootmem = 1;
7866f84f8d1SPavel Tatashin 	x86_init.hyper.init_after_bootmem();
7874e37a890SYinghai Lu 
78846a84132SJiang Liu 	mem_init_print_info(NULL);
789d7d119d7SIngo Molnar 	printk(KERN_INFO "virtual kernel memory layout:\n"
790ad757b6aSThomas Gleixner 		"    fixmap  : 0x%08lx - 0x%08lx   (%4ld kB)\n"
79192a0f81dSThomas Gleixner 		"  cpu_entry : 0x%08lx - 0x%08lx   (%4ld kB)\n"
792ad757b6aSThomas Gleixner #ifdef CONFIG_HIGHMEM
793ad757b6aSThomas Gleixner 		"    pkmap   : 0x%08lx - 0x%08lx   (%4ld kB)\n"
794ad757b6aSThomas Gleixner #endif
795ad757b6aSThomas Gleixner 		"    vmalloc : 0x%08lx - 0x%08lx   (%4ld MB)\n"
796ad757b6aSThomas Gleixner 		"    lowmem  : 0x%08lx - 0x%08lx   (%4ld MB)\n"
797ad757b6aSThomas Gleixner 		"      .init : 0x%08lx - 0x%08lx   (%4ld kB)\n"
798ad757b6aSThomas Gleixner 		"      .data : 0x%08lx - 0x%08lx   (%4ld kB)\n"
799ad757b6aSThomas Gleixner 		"      .text : 0x%08lx - 0x%08lx   (%4ld kB)\n",
800ad757b6aSThomas Gleixner 		FIXADDR_START, FIXADDR_TOP,
801ad757b6aSThomas Gleixner 		(FIXADDR_TOP - FIXADDR_START) >> 10,
802ad757b6aSThomas Gleixner 
80392a0f81dSThomas Gleixner 		CPU_ENTRY_AREA_BASE,
80492a0f81dSThomas Gleixner 		CPU_ENTRY_AREA_BASE + CPU_ENTRY_AREA_MAP_SIZE,
80592a0f81dSThomas Gleixner 		CPU_ENTRY_AREA_MAP_SIZE >> 10,
80692a0f81dSThomas Gleixner 
807ad757b6aSThomas Gleixner #ifdef CONFIG_HIGHMEM
808ad757b6aSThomas Gleixner 		PKMAP_BASE, PKMAP_BASE+LAST_PKMAP*PAGE_SIZE,
809ad757b6aSThomas Gleixner 		(LAST_PKMAP*PAGE_SIZE) >> 10,
810ad757b6aSThomas Gleixner #endif
811ad757b6aSThomas Gleixner 
812ad757b6aSThomas Gleixner 		VMALLOC_START, VMALLOC_END,
813ad757b6aSThomas Gleixner 		(VMALLOC_END - VMALLOC_START) >> 20,
814ad757b6aSThomas Gleixner 
815ad757b6aSThomas Gleixner 		(unsigned long)__va(0), (unsigned long)high_memory,
816ad757b6aSThomas Gleixner 		((unsigned long)high_memory - (unsigned long)__va(0)) >> 20,
817ad757b6aSThomas Gleixner 
818ad757b6aSThomas Gleixner 		(unsigned long)&__init_begin, (unsigned long)&__init_end,
8198550eb99SIngo Molnar 		((unsigned long)&__init_end -
8208550eb99SIngo Molnar 		 (unsigned long)&__init_begin) >> 10,
821ad757b6aSThomas Gleixner 
822ad757b6aSThomas Gleixner 		(unsigned long)&_etext, (unsigned long)&_edata,
823ad757b6aSThomas Gleixner 		((unsigned long)&_edata - (unsigned long)&_etext) >> 10,
824ad757b6aSThomas Gleixner 
825ad757b6aSThomas Gleixner 		(unsigned long)&_text, (unsigned long)&_etext,
826ad757b6aSThomas Gleixner 		((unsigned long)&_etext - (unsigned long)&_text) >> 10);
827ad757b6aSThomas Gleixner 
828beeb4195SJan Beulich 	/*
829beeb4195SJan Beulich 	 * Check boundaries twice: Some fundamental inconsistencies can
830beeb4195SJan Beulich 	 * be detected at build time already.
831beeb4195SJan Beulich 	 */
832beeb4195SJan Beulich #define __FIXADDR_TOP (-PAGE_SIZE)
833beeb4195SJan Beulich #ifdef CONFIG_HIGHMEM
834beeb4195SJan Beulich 	BUILD_BUG_ON(PKMAP_BASE + LAST_PKMAP*PAGE_SIZE	> FIXADDR_START);
835beeb4195SJan Beulich 	BUILD_BUG_ON(VMALLOC_END			> PKMAP_BASE);
836beeb4195SJan Beulich #endif
837beeb4195SJan Beulich #define high_memory (-128UL << 20)
838beeb4195SJan Beulich 	BUILD_BUG_ON(VMALLOC_START			>= VMALLOC_END);
839beeb4195SJan Beulich #undef high_memory
840beeb4195SJan Beulich #undef __FIXADDR_TOP
841beeb4195SJan Beulich 
842ad757b6aSThomas Gleixner #ifdef CONFIG_HIGHMEM
843ad757b6aSThomas Gleixner 	BUG_ON(PKMAP_BASE + LAST_PKMAP*PAGE_SIZE	> FIXADDR_START);
844ad757b6aSThomas Gleixner 	BUG_ON(VMALLOC_END				> PKMAP_BASE);
845ad757b6aSThomas Gleixner #endif
846beeb4195SJan Beulich 	BUG_ON(VMALLOC_START				>= VMALLOC_END);
847ad757b6aSThomas Gleixner 	BUG_ON((unsigned long)high_memory		> VMALLOC_START);
848ad757b6aSThomas Gleixner 
849ad757b6aSThomas Gleixner 	test_wp_bit();
850ad757b6aSThomas Gleixner }
851ad757b6aSThomas Gleixner 
852ad757b6aSThomas Gleixner #ifdef CONFIG_MEMORY_HOTPLUG
853940519f0SMichal Hocko int arch_add_memory(int nid, u64 start, u64 size,
854940519f0SMichal Hocko 			struct mhp_restrictions *restrictions)
855ad757b6aSThomas Gleixner {
856ad757b6aSThomas Gleixner 	unsigned long start_pfn = start >> PAGE_SHIFT;
857ad757b6aSThomas Gleixner 	unsigned long nr_pages = size >> PAGE_SHIFT;
858ad757b6aSThomas Gleixner 
859940519f0SMichal Hocko 	return __add_pages(nid, start_pfn, nr_pages, restrictions);
860ad757b6aSThomas Gleixner }
86124d335caSWen Congyang 
86224d335caSWen Congyang #ifdef CONFIG_MEMORY_HOTREMOVE
863*ac5c9426SDavid Hildenbrand void arch_remove_memory(int nid, u64 start, u64 size,
864*ac5c9426SDavid Hildenbrand 			struct vmem_altmap *altmap)
86524d335caSWen Congyang {
86624d335caSWen Congyang 	unsigned long start_pfn = start >> PAGE_SHIFT;
86724d335caSWen Congyang 	unsigned long nr_pages = size >> PAGE_SHIFT;
86824d335caSWen Congyang 	struct zone *zone;
86924d335caSWen Congyang 
87024d335caSWen Congyang 	zone = page_zone(pfn_to_page(start_pfn));
871*ac5c9426SDavid Hildenbrand 	__remove_pages(zone, start_pfn, nr_pages, altmap);
87224d335caSWen Congyang }
87324d335caSWen Congyang #endif
874ad757b6aSThomas Gleixner #endif
875ad757b6aSThomas Gleixner 
876502f6604SSuresh Siddha int kernel_set_to_readonly __read_mostly;
87716239630SSteven Rostedt 
87816239630SSteven Rostedt void set_kernel_text_rw(void)
87916239630SSteven Rostedt {
88016239630SSteven Rostedt 	unsigned long start = PFN_ALIGN(_text);
88116239630SSteven Rostedt 	unsigned long size = PFN_ALIGN(_etext) - start;
88216239630SSteven Rostedt 
88316239630SSteven Rostedt 	if (!kernel_set_to_readonly)
88416239630SSteven Rostedt 		return;
88516239630SSteven Rostedt 
88616239630SSteven Rostedt 	pr_debug("Set kernel text: %lx - %lx for read write\n",
88716239630SSteven Rostedt 		 start, start+size);
88816239630SSteven Rostedt 
88916239630SSteven Rostedt 	set_pages_rw(virt_to_page(start), size >> PAGE_SHIFT);
89016239630SSteven Rostedt }
89116239630SSteven Rostedt 
89216239630SSteven Rostedt void set_kernel_text_ro(void)
89316239630SSteven Rostedt {
89416239630SSteven Rostedt 	unsigned long start = PFN_ALIGN(_text);
89516239630SSteven Rostedt 	unsigned long size = PFN_ALIGN(_etext) - start;
89616239630SSteven Rostedt 
89716239630SSteven Rostedt 	if (!kernel_set_to_readonly)
89816239630SSteven Rostedt 		return;
89916239630SSteven Rostedt 
90016239630SSteven Rostedt 	pr_debug("Set kernel text: %lx - %lx for read only\n",
90116239630SSteven Rostedt 		 start, start+size);
90216239630SSteven Rostedt 
90316239630SSteven Rostedt 	set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
90416239630SSteven Rostedt }
90516239630SSteven Rostedt 
9065bd5a452SMatthieu Castet static void mark_nxdata_nx(void)
9075bd5a452SMatthieu Castet {
9085bd5a452SMatthieu Castet 	/*
9095bd5a452SMatthieu Castet 	 * When this called, init has already been executed and released,
9100d2eb44fSLucas De Marchi 	 * so everything past _etext should be NX.
9115bd5a452SMatthieu Castet 	 */
9125bd5a452SMatthieu Castet 	unsigned long start = PFN_ALIGN(_etext);
9135bd5a452SMatthieu Castet 	/*
9145bd5a452SMatthieu Castet 	 * This comes from is_kernel_text upper limit. Also HPAGE where used:
9155bd5a452SMatthieu Castet 	 */
9165bd5a452SMatthieu Castet 	unsigned long size = (((unsigned long)__init_end + HPAGE_SIZE) & HPAGE_MASK) - start;
9175bd5a452SMatthieu Castet 
9185bd5a452SMatthieu Castet 	if (__supported_pte_mask & _PAGE_NX)
9195bd5a452SMatthieu Castet 		printk(KERN_INFO "NX-protecting the kernel data: %luk\n", size >> 10);
9205bd5a452SMatthieu Castet 	set_pages_nx(virt_to_page(start), size >> PAGE_SHIFT);
9215bd5a452SMatthieu Castet }
9225bd5a452SMatthieu Castet 
923ad757b6aSThomas Gleixner void mark_rodata_ro(void)
924ad757b6aSThomas Gleixner {
925ad757b6aSThomas Gleixner 	unsigned long start = PFN_ALIGN(_text);
9262a25dc7cSThomas Gleixner 	unsigned long size = (unsigned long)__end_rodata - start;
927ad757b6aSThomas Gleixner 
9286d238cc4SArjan van de Ven 	set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
9292a25dc7cSThomas Gleixner 	pr_info("Write protecting kernel text and read-only data: %luk\n",
930d7d119d7SIngo Molnar 		size >> 10);
9310c42f392SAndi Kleen 
93216239630SSteven Rostedt 	kernel_set_to_readonly = 1;
93316239630SSteven Rostedt 
9340c42f392SAndi Kleen #ifdef CONFIG_CPA_DEBUG
9352a25dc7cSThomas Gleixner 	pr_info("Testing CPA: Reverting %lx-%lx\n", start, start + size);
9366d238cc4SArjan van de Ven 	set_pages_rw(virt_to_page(start), size >> PAGE_SHIFT);
9370c42f392SAndi Kleen 
9382a25dc7cSThomas Gleixner 	pr_info("Testing CPA: write protecting again\n");
9396d238cc4SArjan van de Ven 	set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
9400c42f392SAndi Kleen #endif
9415bd5a452SMatthieu Castet 	mark_nxdata_nx();
942e1a58320SStephen Smalley 	if (__supported_pte_mask & _PAGE_NX)
943e1a58320SStephen Smalley 		debug_checkwx();
944ad757b6aSThomas Gleixner }
945