~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/arch/x86/mm/init_64.c

Version: ~ [ linux-5.1-rc5 ] ~ [ linux-5.0.7 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.34 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.111 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.168 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.178 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.138 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.65 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  *  linux/arch/x86_64/mm/init.c
  3  *
  4  *  Copyright (C) 1995  Linus Torvalds
  5  *  Copyright (C) 2000  Pavel Machek <pavel@ucw.cz>
  6  *  Copyright (C) 2002,2003 Andi Kleen <ak@suse.de>
  7  */
  8 
  9 #include <linux/signal.h>
 10 #include <linux/sched.h>
 11 #include <linux/kernel.h>
 12 #include <linux/errno.h>
 13 #include <linux/string.h>
 14 #include <linux/types.h>
 15 #include <linux/ptrace.h>
 16 #include <linux/mman.h>
 17 #include <linux/mm.h>
 18 #include <linux/swap.h>
 19 #include <linux/smp.h>
 20 #include <linux/init.h>
 21 #include <linux/initrd.h>
 22 #include <linux/pagemap.h>
 23 #include <linux/bootmem.h>
 24 #include <linux/memblock.h>
 25 #include <linux/proc_fs.h>
 26 #include <linux/pci.h>
 27 #include <linux/pfn.h>
 28 #include <linux/poison.h>
 29 #include <linux/dma-mapping.h>
 30 #include <linux/memory.h>
 31 #include <linux/memory_hotplug.h>
 32 #include <linux/memremap.h>
 33 #include <linux/nmi.h>
 34 #include <linux/gfp.h>
 35 #include <linux/kcore.h>
 36 
 37 #include <asm/processor.h>
 38 #include <asm/bios_ebda.h>
 39 #include <linux/uaccess.h>
 40 #include <asm/pgtable.h>
 41 #include <asm/pgalloc.h>
 42 #include <asm/dma.h>
 43 #include <asm/fixmap.h>
 44 #include <asm/e820/api.h>
 45 #include <asm/apic.h>
 46 #include <asm/tlb.h>
 47 #include <asm/mmu_context.h>
 48 #include <asm/proto.h>
 49 #include <asm/smp.h>
 50 #include <asm/sections.h>
 51 #include <asm/kdebug.h>
 52 #include <asm/numa.h>
 53 #include <asm/set_memory.h>
 54 #include <asm/init.h>
 55 #include <asm/uv/uv.h>
 56 #include <asm/setup.h>
 57 
 58 #include "mm_internal.h"
 59 
 60 #include "ident_map.c"
 61 
 62 /*
 63  * NOTE: pagetable_init alloc all the fixmap pagetables contiguous on the
 64  * physical space so we can cache the place of the first one and move
 65  * around without checking the pgd every time.
 66  */
 67 
 68 pteval_t __supported_pte_mask __read_mostly = ~0;
 69 EXPORT_SYMBOL_GPL(__supported_pte_mask);
 70 
 71 int force_personality32;
 72 
 73 /*
 74  * noexec32=on|off
 75  * Control non executable heap for 32bit processes.
 76  * To control the stack too use noexec=off
 77  *
 78  * on   PROT_READ does not imply PROT_EXEC for 32-bit processes (default)
 79  * off  PROT_READ implies PROT_EXEC
 80  */
 81 static int __init nonx32_setup(char *str)
 82 {
 83         if (!strcmp(str, "on"))
 84                 force_personality32 &= ~READ_IMPLIES_EXEC;
 85         else if (!strcmp(str, "off"))
 86                 force_personality32 |= READ_IMPLIES_EXEC;
 87         return 1;
 88 }
 89 __setup("noexec32=", nonx32_setup);
 90 
 91 /*
 92  * When memory was added make sure all the processes MM have
 93  * suitable PGD entries in the local PGD level page.
 94  */
 95 #ifdef CONFIG_X86_5LEVEL
 96 void sync_global_pgds(unsigned long start, unsigned long end)
 97 {
 98         unsigned long addr;
 99 
100         for (addr = start; addr <= end; addr = ALIGN(addr + 1, PGDIR_SIZE)) {
101                 const pgd_t *pgd_ref = pgd_offset_k(addr);
102                 struct page *page;
103 
104                 /* Check for overflow */
105                 if (addr < start)
106                         break;
107 
108                 if (pgd_none(*pgd_ref))
109                         continue;
110 
111                 spin_lock(&pgd_lock);
112                 list_for_each_entry(page, &pgd_list, lru) {
113                         pgd_t *pgd;
114                         spinlock_t *pgt_lock;
115 
116                         pgd = (pgd_t *)page_address(page) + pgd_index(addr);
117                         /* the pgt_lock only for Xen */
118                         pgt_lock = &pgd_page_get_mm(page)->page_table_lock;
119                         spin_lock(pgt_lock);
120 
121                         if (!pgd_none(*pgd_ref) && !pgd_none(*pgd))
122                                 BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
123 
124                         if (pgd_none(*pgd))
125                                 set_pgd(pgd, *pgd_ref);
126 
127                         spin_unlock(pgt_lock);
128                 }
129                 spin_unlock(&pgd_lock);
130         }
131 }
132 #else
133 void sync_global_pgds(unsigned long start, unsigned long end)
134 {
135         unsigned long addr;
136 
137         for (addr = start; addr <= end; addr = ALIGN(addr + 1, PGDIR_SIZE)) {
138                 pgd_t *pgd_ref = pgd_offset_k(addr);
139                 const p4d_t *p4d_ref;
140                 struct page *page;
141 
142                 /*
143                  * With folded p4d, pgd_none() is always false, we need to
144                  * handle synchonization on p4d level.
145                  */
146                 BUILD_BUG_ON(pgd_none(*pgd_ref));
147                 p4d_ref = p4d_offset(pgd_ref, addr);
148 
149                 if (p4d_none(*p4d_ref))
150                         continue;
151 
152                 spin_lock(&pgd_lock);
153                 list_for_each_entry(page, &pgd_list, lru) {
154                         pgd_t *pgd;
155                         p4d_t *p4d;
156                         spinlock_t *pgt_lock;
157 
158                         pgd = (pgd_t *)page_address(page) + pgd_index(addr);
159                         p4d = p4d_offset(pgd, addr);
160                         /* the pgt_lock only for Xen */
161                         pgt_lock = &pgd_page_get_mm(page)->page_table_lock;
162                         spin_lock(pgt_lock);
163 
164                         if (!p4d_none(*p4d_ref) && !p4d_none(*p4d))
165                                 BUG_ON(p4d_page_vaddr(*p4d)
166                                        != p4d_page_vaddr(*p4d_ref));
167 
168                         if (p4d_none(*p4d))
169                                 set_p4d(p4d, *p4d_ref);
170 
171                         spin_unlock(pgt_lock);
172                 }
173                 spin_unlock(&pgd_lock);
174         }
175 }
176 #endif
177 
178 /*
179  * NOTE: This function is marked __ref because it calls __init function
180  * (alloc_bootmem_pages). It's safe to do it ONLY when after_bootmem == 0.
181  */
182 static __ref void *spp_getpage(void)
183 {
184         void *ptr;
185 
186         if (after_bootmem)
187                 ptr = (void *) get_zeroed_page(GFP_ATOMIC);
188         else
189                 ptr = alloc_bootmem_pages(PAGE_SIZE);
190 
191         if (!ptr || ((unsigned long)ptr & ~PAGE_MASK)) {
192                 panic("set_pte_phys: cannot allocate page data %s\n",
193                         after_bootmem ? "after bootmem" : "");
194         }
195 
196         pr_debug("spp_getpage %p\n", ptr);
197 
198         return ptr;
199 }
200 
201 static p4d_t *fill_p4d(pgd_t *pgd, unsigned long vaddr)
202 {
203         if (pgd_none(*pgd)) {
204                 p4d_t *p4d = (p4d_t *)spp_getpage();
205                 pgd_populate(&init_mm, pgd, p4d);
206                 if (p4d != p4d_offset(pgd, 0))
207                         printk(KERN_ERR "PAGETABLE BUG #00! %p <-> %p\n",
208                                p4d, p4d_offset(pgd, 0));
209         }
210         return p4d_offset(pgd, vaddr);
211 }
212 
213 static pud_t *fill_pud(p4d_t *p4d, unsigned long vaddr)
214 {
215         if (p4d_none(*p4d)) {
216                 pud_t *pud = (pud_t *)spp_getpage();
217                 p4d_populate(&init_mm, p4d, pud);
218                 if (pud != pud_offset(p4d, 0))
219                         printk(KERN_ERR "PAGETABLE BUG #01! %p <-> %p\n",
220                                pud, pud_offset(p4d, 0));
221         }
222         return pud_offset(p4d, vaddr);
223 }
224 
225 static pmd_t *fill_pmd(pud_t *pud, unsigned long vaddr)
226 {
227         if (pud_none(*pud)) {
228                 pmd_t *pmd = (pmd_t *) spp_getpage();
229                 pud_populate(&init_mm, pud, pmd);
230                 if (pmd != pmd_offset(pud, 0))
231                         printk(KERN_ERR "PAGETABLE BUG #02! %p <-> %p\n",
232                                pmd, pmd_offset(pud, 0));
233         }
234         return pmd_offset(pud, vaddr);
235 }
236 
237 static pte_t *fill_pte(pmd_t *pmd, unsigned long vaddr)
238 {
239         if (pmd_none(*pmd)) {
240                 pte_t *pte = (pte_t *) spp_getpage();
241                 pmd_populate_kernel(&init_mm, pmd, pte);
242                 if (pte != pte_offset_kernel(pmd, 0))
243                         printk(KERN_ERR "PAGETABLE BUG #03!\n");
244         }
245         return pte_offset_kernel(pmd, vaddr);
246 }
247 
248 static void __set_pte_vaddr(pud_t *pud, unsigned long vaddr, pte_t new_pte)
249 {
250         pmd_t *pmd = fill_pmd(pud, vaddr);
251         pte_t *pte = fill_pte(pmd, vaddr);
252 
253         set_pte(pte, new_pte);
254 
255         /*
256          * It's enough to flush this one mapping.
257          * (PGE mappings get flushed as well)
258          */
259         __flush_tlb_one_kernel(vaddr);
260 }
261 
262 void set_pte_vaddr_p4d(p4d_t *p4d_page, unsigned long vaddr, pte_t new_pte)
263 {
264         p4d_t *p4d = p4d_page + p4d_index(vaddr);
265         pud_t *pud = fill_pud(p4d, vaddr);
266 
267         __set_pte_vaddr(pud, vaddr, new_pte);
268 }
269 
270 void set_pte_vaddr_pud(pud_t *pud_page, unsigned long vaddr, pte_t new_pte)
271 {
272         pud_t *pud = pud_page + pud_index(vaddr);
273 
274         __set_pte_vaddr(pud, vaddr, new_pte);
275 }
276 
277 void set_pte_vaddr(unsigned long vaddr, pte_t pteval)
278 {
279         pgd_t *pgd;
280         p4d_t *p4d_page;
281 
282         pr_debug("set_pte_vaddr %lx to %lx\n", vaddr, native_pte_val(pteval));
283 
284         pgd = pgd_offset_k(vaddr);
285         if (pgd_none(*pgd)) {
286                 printk(KERN_ERR
287                         "PGD FIXMAP MISSING, it should be setup in head.S!\n");
288                 return;
289         }
290 
291         p4d_page = p4d_offset(pgd, 0);
292         set_pte_vaddr_p4d(p4d_page, vaddr, pteval);
293 }
294 
295 pmd_t * __init populate_extra_pmd(unsigned long vaddr)
296 {
297         pgd_t *pgd;
298         p4d_t *p4d;
299         pud_t *pud;
300 
301         pgd = pgd_offset_k(vaddr);
302         p4d = fill_p4d(pgd, vaddr);
303         pud = fill_pud(p4d, vaddr);
304         return fill_pmd(pud, vaddr);
305 }
306 
307 pte_t * __init populate_extra_pte(unsigned long vaddr)
308 {
309         pmd_t *pmd;
310 
311         pmd = populate_extra_pmd(vaddr);
312         return fill_pte(pmd, vaddr);
313 }
314 
315 /*
316  * Create large page table mappings for a range of physical addresses.
317  */
318 static void __init __init_extra_mapping(unsigned long phys, unsigned long size,
319                                         enum page_cache_mode cache)
320 {
321         pgd_t *pgd;
322         p4d_t *p4d;
323         pud_t *pud;
324         pmd_t *pmd;
325         pgprot_t prot;
326 
327         pgprot_val(prot) = pgprot_val(PAGE_KERNEL_LARGE) |
328                 pgprot_val(pgprot_4k_2_large(cachemode2pgprot(cache)));
329         BUG_ON((phys & ~PMD_MASK) || (size & ~PMD_MASK));
330         for (; size; phys += PMD_SIZE, size -= PMD_SIZE) {
331                 pgd = pgd_offset_k((unsigned long)__va(phys));
332                 if (pgd_none(*pgd)) {
333                         p4d = (p4d_t *) spp_getpage();
334                         set_pgd(pgd, __pgd(__pa(p4d) | _KERNPG_TABLE |
335                                                 _PAGE_USER));
336                 }
337                 p4d = p4d_offset(pgd, (unsigned long)__va(phys));
338                 if (p4d_none(*p4d)) {
339                         pud = (pud_t *) spp_getpage();
340                         set_p4d(p4d, __p4d(__pa(pud) | _KERNPG_TABLE |
341                                                 _PAGE_USER));
342                 }
343                 pud = pud_offset(p4d, (unsigned long)__va(phys));
344                 if (pud_none(*pud)) {
345                         pmd = (pmd_t *) spp_getpage();
346                         set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE |
347                                                 _PAGE_USER));
348                 }
349                 pmd = pmd_offset(pud, phys);
350                 BUG_ON(!pmd_none(*pmd));
351                 set_pmd(pmd, __pmd(phys | pgprot_val(prot)));
352         }
353 }
354 
355 void __init init_extra_mapping_wb(unsigned long phys, unsigned long size)
356 {
357         __init_extra_mapping(phys, size, _PAGE_CACHE_MODE_WB);
358 }
359 
360 void __init init_extra_mapping_uc(unsigned long phys, unsigned long size)
361 {
362         __init_extra_mapping(phys, size, _PAGE_CACHE_MODE_UC);
363 }
364 
365 /*
366  * The head.S code sets up the kernel high mapping:
367  *
368  *   from __START_KERNEL_map to __START_KERNEL_map + size (== _end-_text)
369  *
370  * phys_base holds the negative offset to the kernel, which is added
371  * to the compile time generated pmds. This results in invalid pmds up
372  * to the point where we hit the physaddr 0 mapping.
373  *
374  * We limit the mappings to the region from _text to _brk_end.  _brk_end
375  * is rounded up to the 2MB boundary. This catches the invalid pmds as
376  * well, as they are located before _text:
377  */
378 void __init cleanup_highmap(void)
379 {
380         unsigned long vaddr = __START_KERNEL_map;
381         unsigned long vaddr_end = __START_KERNEL_map + KERNEL_IMAGE_SIZE;
382         unsigned long end = roundup((unsigned long)_brk_end, PMD_SIZE) - 1;
383         pmd_t *pmd = level2_kernel_pgt;
384 
385         /*
386          * Native path, max_pfn_mapped is not set yet.
387          * Xen has valid max_pfn_mapped set in
388          *      arch/x86/xen/mmu.c:xen_setup_kernel_pagetable().
389          */
390         if (max_pfn_mapped)
391                 vaddr_end = __START_KERNEL_map + (max_pfn_mapped << PAGE_SHIFT);
392 
393         for (; vaddr + PMD_SIZE - 1 < vaddr_end; pmd++, vaddr += PMD_SIZE) {
394                 if (pmd_none(*pmd))
395                         continue;
396                 if (vaddr < (unsigned long) _text || vaddr > end)
397                         set_pmd(pmd, __pmd(0));
398         }
399 }
400 
401 /*
402  * Create PTE level page table mapping for physical addresses.
403  * It returns the last physical address mapped.
404  */
405 static unsigned long __meminit
406 phys_pte_init(pte_t *pte_page, unsigned long paddr, unsigned long paddr_end,
407               pgprot_t prot)
408 {
409         unsigned long pages = 0, paddr_next;
410         unsigned long paddr_last = paddr_end;
411         pte_t *pte;
412         int i;
413 
414         pte = pte_page + pte_index(paddr);
415         i = pte_index(paddr);
416 
417         for (; i < PTRS_PER_PTE; i++, paddr = paddr_next, pte++) {
418                 paddr_next = (paddr & PAGE_MASK) + PAGE_SIZE;
419                 if (paddr >= paddr_end) {
420                         if (!after_bootmem &&
421                             !e820__mapped_any(paddr & PAGE_MASK, paddr_next,
422                                              E820_TYPE_RAM) &&
423                             !e820__mapped_any(paddr & PAGE_MASK, paddr_next,
424                                              E820_TYPE_RESERVED_KERN))
425                                 set_pte(pte, __pte(0));
426                         continue;
427                 }
428 
429                 /*
430                  * We will re-use the existing mapping.
431                  * Xen for example has some special requirements, like mapping
432                  * pagetable pages as RO. So assume someone who pre-setup
433                  * these mappings are more intelligent.
434                  */
435                 if (!pte_none(*pte)) {
436                         if (!after_bootmem)
437                                 pages++;
438                         continue;
439                 }
440 
441                 if (0)
442                         pr_info("   pte=%p addr=%lx pte=%016lx\n", pte, paddr,
443                                 pfn_pte(paddr >> PAGE_SHIFT, PAGE_KERNEL).pte);
444                 pages++;
445                 set_pte(pte, pfn_pte(paddr >> PAGE_SHIFT, prot));
446                 paddr_last = (paddr & PAGE_MASK) + PAGE_SIZE;
447         }
448 
449         update_page_count(PG_LEVEL_4K, pages);
450 
451         return paddr_last;
452 }
453 
454 /*
455  * Create PMD level page table mapping for physical addresses. The virtual
456  * and physical address have to be aligned at this level.
457  * It returns the last physical address mapped.
458  */
459 static unsigned long __meminit
460 phys_pmd_init(pmd_t *pmd_page, unsigned long paddr, unsigned long paddr_end,
461               unsigned long page_size_mask, pgprot_t prot)
462 {
463         unsigned long pages = 0, paddr_next;
464         unsigned long paddr_last = paddr_end;
465 
466         int i = pmd_index(paddr);
467 
468         for (; i < PTRS_PER_PMD; i++, paddr = paddr_next) {
469                 pmd_t *pmd = pmd_page + pmd_index(paddr);
470                 pte_t *pte;
471                 pgprot_t new_prot = prot;
472 
473                 paddr_next = (paddr & PMD_MASK) + PMD_SIZE;
474                 if (paddr >= paddr_end) {
475                         if (!after_bootmem &&
476                             !e820__mapped_any(paddr & PMD_MASK, paddr_next,
477                                              E820_TYPE_RAM) &&
478                             !e820__mapped_any(paddr & PMD_MASK, paddr_next,
479                                              E820_TYPE_RESERVED_KERN))
480                                 set_pmd(pmd, __pmd(0));
481                         continue;
482                 }
483 
484                 if (!pmd_none(*pmd)) {
485                         if (!pmd_large(*pmd)) {
486                                 spin_lock(&init_mm.page_table_lock);
487                                 pte = (pte_t *)pmd_page_vaddr(*pmd);
488                                 paddr_last = phys_pte_init(pte, paddr,
489                                                            paddr_end, prot);
490                                 spin_unlock(&init_mm.page_table_lock);
491                                 continue;
492                         }
493                         /*
494                          * If we are ok with PG_LEVEL_2M mapping, then we will
495                          * use the existing mapping,
496                          *
497                          * Otherwise, we will split the large page mapping but
498                          * use the same existing protection bits except for
499                          * large page, so that we don't violate Intel's TLB
500                          * Application note (317080) which says, while changing
501                          * the page sizes, new and old translations should
502                          * not differ with respect to page frame and
503                          * attributes.
504                          */
505                         if (page_size_mask & (1 << PG_LEVEL_2M)) {
506                                 if (!after_bootmem)
507                                         pages++;
508                                 paddr_last = paddr_next;
509                                 continue;
510                         }
511                         new_prot = pte_pgprot(pte_clrhuge(*(pte_t *)pmd));
512                 }
513 
514                 if (page_size_mask & (1<<PG_LEVEL_2M)) {
515                         pages++;
516                         spin_lock(&init_mm.page_table_lock);
517                         set_pte((pte_t *)pmd,
518                                 pfn_pte((paddr & PMD_MASK) >> PAGE_SHIFT,
519                                         __pgprot(pgprot_val(prot) | _PAGE_PSE)));
520                         spin_unlock(&init_mm.page_table_lock);
521                         paddr_last = paddr_next;
522                         continue;
523                 }
524 
525                 pte = alloc_low_page();
526                 paddr_last = phys_pte_init(pte, paddr, paddr_end, new_prot);
527 
528                 spin_lock(&init_mm.page_table_lock);
529                 pmd_populate_kernel(&init_mm, pmd, pte);
530                 spin_unlock(&init_mm.page_table_lock);
531         }
532         update_page_count(PG_LEVEL_2M, pages);
533         return paddr_last;
534 }
535 
536 /*
537  * Create PUD level page table mapping for physical addresses. The virtual
538  * and physical address do not have to be aligned at this level. KASLR can
539  * randomize virtual addresses up to this level.
540  * It returns the last physical address mapped.
541  */
542 static unsigned long __meminit
543 phys_pud_init(pud_t *pud_page, unsigned long paddr, unsigned long paddr_end,
544               unsigned long page_size_mask)
545 {
546         unsigned long pages = 0, paddr_next;
547         unsigned long paddr_last = paddr_end;
548         unsigned long vaddr = (unsigned long)__va(paddr);
549         int i = pud_index(vaddr);
550 
551         for (; i < PTRS_PER_PUD; i++, paddr = paddr_next) {
552                 pud_t *pud;
553                 pmd_t *pmd;
554                 pgprot_t prot = PAGE_KERNEL;
555 
556                 vaddr = (unsigned long)__va(paddr);
557                 pud = pud_page + pud_index(vaddr);
558                 paddr_next = (paddr & PUD_MASK) + PUD_SIZE;
559 
560                 if (paddr >= paddr_end) {
561                         if (!after_bootmem &&
562                             !e820__mapped_any(paddr & PUD_MASK, paddr_next,
563                                              E820_TYPE_RAM) &&
564                             !e820__mapped_any(paddr & PUD_MASK, paddr_next,
565                                              E820_TYPE_RESERVED_KERN))
566                                 set_pud(pud, __pud(0));
567                         continue;
568                 }
569 
570                 if (!pud_none(*pud)) {
571                         if (!pud_large(*pud)) {
572                                 pmd = pmd_offset(pud, 0);
573                                 paddr_last = phys_pmd_init(pmd, paddr,
574                                                            paddr_end,
575                                                            page_size_mask,
576                                                            prot);
577                                 __flush_tlb_all();
578                                 continue;
579                         }
580                         /*
581                          * If we are ok with PG_LEVEL_1G mapping, then we will
582                          * use the existing mapping.
583                          *
584                          * Otherwise, we will split the gbpage mapping but use
585                          * the same existing protection  bits except for large
586                          * page, so that we don't violate Intel's TLB
587                          * Application note (317080) which says, while changing
588                          * the page sizes, new and old translations should
589                          * not differ with respect to page frame and
590                          * attributes.
591                          */
592                         if (page_size_mask & (1 << PG_LEVEL_1G)) {
593                                 if (!after_bootmem)
594                                         pages++;
595                                 paddr_last = paddr_next;
596                                 continue;
597                         }
598                         prot = pte_pgprot(pte_clrhuge(*(pte_t *)pud));
599                 }
600 
601                 if (page_size_mask & (1<<PG_LEVEL_1G)) {
602                         pages++;
603                         spin_lock(&init_mm.page_table_lock);
604                         set_pte((pte_t *)pud,
605                                 pfn_pte((paddr & PUD_MASK) >> PAGE_SHIFT,
606                                         PAGE_KERNEL_LARGE));
607                         spin_unlock(&init_mm.page_table_lock);
608                         paddr_last = paddr_next;
609                         continue;
610                 }
611 
612                 pmd = alloc_low_page();
613                 paddr_last = phys_pmd_init(pmd, paddr, paddr_end,
614                                            page_size_mask, prot);
615 
616                 spin_lock(&init_mm.page_table_lock);
617                 pud_populate(&init_mm, pud, pmd);
618                 spin_unlock(&init_mm.page_table_lock);
619         }
620         __flush_tlb_all();
621 
622         update_page_count(PG_LEVEL_1G, pages);
623 
624         return paddr_last;
625 }
626 
627 static unsigned long __meminit
628 phys_p4d_init(p4d_t *p4d_page, unsigned long paddr, unsigned long paddr_end,
629               unsigned long page_size_mask)
630 {
631         unsigned long paddr_next, paddr_last = paddr_end;
632         unsigned long vaddr = (unsigned long)__va(paddr);
633         int i = p4d_index(vaddr);
634 
635         if (!IS_ENABLED(CONFIG_X86_5LEVEL))
636                 return phys_pud_init((pud_t *) p4d_page, paddr, paddr_end, page_size_mask);
637 
638         for (; i < PTRS_PER_P4D; i++, paddr = paddr_next) {
639                 p4d_t *p4d;
640                 pud_t *pud;
641 
642                 vaddr = (unsigned long)__va(paddr);
643                 p4d = p4d_page + p4d_index(vaddr);
644                 paddr_next = (paddr & P4D_MASK) + P4D_SIZE;
645 
646                 if (paddr >= paddr_end) {
647                         if (!after_bootmem &&
648                             !e820__mapped_any(paddr & P4D_MASK, paddr_next,
649                                              E820_TYPE_RAM) &&
650                             !e820__mapped_any(paddr & P4D_MASK, paddr_next,
651                                              E820_TYPE_RESERVED_KERN))
652                                 set_p4d(p4d, __p4d(0));
653                         continue;
654                 }
655 
656                 if (!p4d_none(*p4d)) {
657                         pud = pud_offset(p4d, 0);
658                         paddr_last = phys_pud_init(pud, paddr,
659                                         paddr_end,
660                                         page_size_mask);
661                         __flush_tlb_all();
662                         continue;
663                 }
664 
665                 pud = alloc_low_page();
666                 paddr_last = phys_pud_init(pud, paddr, paddr_end,
667                                            page_size_mask);
668 
669                 spin_lock(&init_mm.page_table_lock);
670                 p4d_populate(&init_mm, p4d, pud);
671                 spin_unlock(&init_mm.page_table_lock);
672         }
673         __flush_tlb_all();
674 
675         return paddr_last;
676 }
677 
678 /*
679  * Create page table mapping for the physical memory for specific physical
680  * addresses. The virtual and physical addresses have to be aligned on PMD level
681  * down. It returns the last physical address mapped.
682  */
683 unsigned long __meminit
684 kernel_physical_mapping_init(unsigned long paddr_start,
685                              unsigned long paddr_end,
686                              unsigned long page_size_mask)
687 {
688         bool pgd_changed = false;
689         unsigned long vaddr, vaddr_start, vaddr_end, vaddr_next, paddr_last;
690 
691         paddr_last = paddr_end;
692         vaddr = (unsigned long)__va(paddr_start);
693         vaddr_end = (unsigned long)__va(paddr_end);
694         vaddr_start = vaddr;
695 
696         for (; vaddr < vaddr_end; vaddr = vaddr_next) {
697                 pgd_t *pgd = pgd_offset_k(vaddr);
698                 p4d_t *p4d;
699 
700                 vaddr_next = (vaddr & PGDIR_MASK) + PGDIR_SIZE;
701 
702                 if (pgd_val(*pgd)) {
703                         p4d = (p4d_t *)pgd_page_vaddr(*pgd);
704                         paddr_last = phys_p4d_init(p4d, __pa(vaddr),
705                                                    __pa(vaddr_end),
706                                                    page_size_mask);
707                         continue;
708                 }
709 
710                 p4d = alloc_low_page();
711                 paddr_last = phys_p4d_init(p4d, __pa(vaddr), __pa(vaddr_end),
712                                            page_size_mask);
713 
714                 spin_lock(&init_mm.page_table_lock);
715                 if (IS_ENABLED(CONFIG_X86_5LEVEL))
716                         pgd_populate(&init_mm, pgd, p4d);
717                 else
718                         p4d_populate(&init_mm, p4d_offset(pgd, vaddr), (pud_t *) p4d);
719                 spin_unlock(&init_mm.page_table_lock);
720                 pgd_changed = true;
721         }
722 
723         if (pgd_changed)
724                 sync_global_pgds(vaddr_start, vaddr_end - 1);
725 
726         __flush_tlb_all();
727 
728         return paddr_last;
729 }
730 
731 #ifndef CONFIG_NUMA
732 void __init initmem_init(void)
733 {
734         memblock_set_node(0, (phys_addr_t)ULLONG_MAX, &memblock.memory, 0);
735 }
736 #endif
737 
738 void __init paging_init(void)
739 {
740         sparse_memory_present_with_active_regions(MAX_NUMNODES);
741         sparse_init();
742 
743         /*
744          * clear the default setting with node 0
745          * note: don't use nodes_clear here, that is really clearing when
746          *       numa support is not compiled in, and later node_set_state
747          *       will not set it back.
748          */
749         node_clear_state(0, N_MEMORY);
750         if (N_MEMORY != N_NORMAL_MEMORY)
751                 node_clear_state(0, N_NORMAL_MEMORY);
752 
753         zone_sizes_init();
754 }
755 
756 /*
757  * Memory hotplug specific functions
758  */
759 #ifdef CONFIG_MEMORY_HOTPLUG
760 /*
761  * After memory hotplug the variables max_pfn, max_low_pfn and high_memory need
762  * updating.
763  */
764 static void update_end_of_memory_vars(u64 start, u64 size)
765 {
766         unsigned long end_pfn = PFN_UP(start + size);
767 
768         if (end_pfn > max_pfn) {
769                 max_pfn = end_pfn;
770                 max_low_pfn = end_pfn;
771                 high_memory = (void *)__va(max_pfn * PAGE_SIZE - 1) + 1;
772         }
773 }
774 
775 int add_pages(int nid, unsigned long start_pfn, unsigned long nr_pages,
776                 struct vmem_altmap *altmap, bool want_memblock)
777 {
778         int ret;
779 
780         ret = __add_pages(nid, start_pfn, nr_pages, altmap, want_memblock);
781         WARN_ON_ONCE(ret);
782 
783         /* update max_pfn, max_low_pfn and high_memory */
784         update_end_of_memory_vars(start_pfn << PAGE_SHIFT,
785                                   nr_pages << PAGE_SHIFT);
786 
787         return ret;
788 }
789 
790 int arch_add_memory(int nid, u64 start, u64 size, struct vmem_altmap *altmap,
791                 bool want_memblock)
792 {
793         unsigned long start_pfn = start >> PAGE_SHIFT;
794         unsigned long nr_pages = size >> PAGE_SHIFT;
795 
796         init_memory_mapping(start, start + size);
797 
798         return add_pages(nid, start_pfn, nr_pages, altmap, want_memblock);
799 }
800 
801 #define PAGE_INUSE 0xFD
802 
803 static void __meminit free_pagetable(struct page *page, int order)
804 {
805         unsigned long magic;
806         unsigned int nr_pages = 1 << order;
807 
808         /* bootmem page has reserved flag */
809         if (PageReserved(page)) {
810                 __ClearPageReserved(page);
811 
812                 magic = (unsigned long)page->freelist;
813                 if (magic == SECTION_INFO || magic == MIX_SECTION_INFO) {
814                         while (nr_pages--)
815                                 put_page_bootmem(page++);
816                 } else
817                         while (nr_pages--)
818                                 free_reserved_page(page++);
819         } else
820                 free_pages((unsigned long)page_address(page), order);
821 }
822 
823 static void __meminit free_hugepage_table(struct page *page,
824                 struct vmem_altmap *altmap)
825 {
826         if (altmap)
827                 vmem_altmap_free(altmap, PMD_SIZE / PAGE_SIZE);
828         else
829                 free_pagetable(page, get_order(PMD_SIZE));
830 }
831 
832 static void __meminit free_pte_table(pte_t *pte_start, pmd_t *pmd)
833 {
834         pte_t *pte;
835         int i;
836 
837         for (i = 0; i < PTRS_PER_PTE; i++) {
838                 pte = pte_start + i;
839                 if (!pte_none(*pte))
840                         return;
841         }
842 
843         /* free a pte talbe */
844         free_pagetable(pmd_page(*pmd), 0);
845         spin_lock(&init_mm.page_table_lock);
846         pmd_clear(pmd);
847         spin_unlock(&init_mm.page_table_lock);
848 }
849 
850 static void __meminit free_pmd_table(pmd_t *pmd_start, pud_t *pud)
851 {
852         pmd_t *pmd;
853         int i;
854 
855         for (i = 0; i < PTRS_PER_PMD; i++) {
856                 pmd = pmd_start + i;
857                 if (!pmd_none(*pmd))
858                         return;
859         }
860 
861         /* free a pmd talbe */
862         free_pagetable(pud_page(*pud), 0);
863         spin_lock(&init_mm.page_table_lock);
864         pud_clear(pud);
865         spin_unlock(&init_mm.page_table_lock);
866 }
867 
868 static void __meminit free_pud_table(pud_t *pud_start, p4d_t *p4d)
869 {
870         pud_t *pud;
871         int i;
872 
873         for (i = 0; i < PTRS_PER_PUD; i++) {
874                 pud = pud_start + i;
875                 if (!pud_none(*pud))
876                         return;
877         }
878 
879         /* free a pud talbe */
880         free_pagetable(p4d_page(*p4d), 0);
881         spin_lock(&init_mm.page_table_lock);
882         p4d_clear(p4d);
883         spin_unlock(&init_mm.page_table_lock);
884 }
885 
886 static void __meminit
887 remove_pte_table(pte_t *pte_start, unsigned long addr, unsigned long end,
888                  bool direct)
889 {
890         unsigned long next, pages = 0;
891         pte_t *pte;
892         void *page_addr;
893         phys_addr_t phys_addr;
894 
895         pte = pte_start + pte_index(addr);
896         for (; addr < end; addr = next, pte++) {
897                 next = (addr + PAGE_SIZE) & PAGE_MASK;
898                 if (next > end)
899                         next = end;
900 
901                 if (!pte_present(*pte))
902                         continue;
903 
904                 /*
905                  * We mapped [0,1G) memory as identity mapping when
906                  * initializing, in arch/x86/kernel/head_64.S. These
907                  * pagetables cannot be removed.
908                  */
909                 phys_addr = pte_val(*pte) + (addr & PAGE_MASK);
910                 if (phys_addr < (phys_addr_t)0x40000000)
911                         return;
912 
913                 if (PAGE_ALIGNED(addr) && PAGE_ALIGNED(next)) {
914                         /*
915                          * Do not free direct mapping pages since they were
916                          * freed when offlining, or simplely not in use.
917                          */
918                         if (!direct)
919                                 free_pagetable(pte_page(*pte), 0);
920 
921                         spin_lock(&init_mm.page_table_lock);
922                         pte_clear(&init_mm, addr, pte);
923                         spin_unlock(&init_mm.page_table_lock);
924 
925                         /* For non-direct mapping, pages means nothing. */
926                         pages++;
927                 } else {
928                         /*
929                          * If we are here, we are freeing vmemmap pages since
930                          * direct mapped memory ranges to be freed are aligned.
931                          *
932                          * If we are not removing the whole page, it means
933                          * other page structs in this page are being used and
934                          * we canot remove them. So fill the unused page_structs
935                          * with 0xFD, and remove the page when it is wholly
936                          * filled with 0xFD.
937                          */
938                         memset((void *)addr, PAGE_INUSE, next - addr);
939 
940                         page_addr = page_address(pte_page(*pte));
941                         if (!memchr_inv(page_addr, PAGE_INUSE, PAGE_SIZE)) {
942                                 free_pagetable(pte_page(*pte), 0);
943 
944                                 spin_lock(&init_mm.page_table_lock);
945                                 pte_clear(&init_mm, addr, pte);
946                                 spin_unlock(&init_mm.page_table_lock);
947                         }
948                 }
949         }
950 
951         /* Call free_pte_table() in remove_pmd_table(). */
952         flush_tlb_all();
953         if (direct)
954                 update_page_count(PG_LEVEL_4K, -pages);
955 }
956 
957 static void __meminit
958 remove_pmd_table(pmd_t *pmd_start, unsigned long addr, unsigned long end,
959                  bool direct, struct vmem_altmap *altmap)
960 {
961         unsigned long next, pages = 0;
962         pte_t *pte_base;
963         pmd_t *pmd;
964         void *page_addr;
965 
966         pmd = pmd_start + pmd_index(addr);
967         for (; addr < end; addr = next, pmd++) {
968                 next = pmd_addr_end(addr, end);
969 
970                 if (!pmd_present(*pmd))
971                         continue;
972 
973                 if (pmd_large(*pmd)) {
974                         if (IS_ALIGNED(addr, PMD_SIZE) &&
975                             IS_ALIGNED(next, PMD_SIZE)) {
976                                 if (!direct)
977                                         free_hugepage_table(pmd_page(*pmd),
978                                                             altmap);
979 
980                                 spin_lock(&init_mm.page_table_lock);
981                                 pmd_clear(pmd);
982                                 spin_unlock(&init_mm.page_table_lock);
983                                 pages++;
984                         } else {
985                                 /* If here, we are freeing vmemmap pages. */
986                                 memset((void *)addr, PAGE_INUSE, next - addr);
987 
988                                 page_addr = page_address(pmd_page(*pmd));
989                                 if (!memchr_inv(page_addr, PAGE_INUSE,
990                                                 PMD_SIZE)) {
991                                         free_hugepage_table(pmd_page(*pmd),
992                                                             altmap);
993 
994                                         spin_lock(&init_mm.page_table_lock);
995                                         pmd_clear(pmd);
996                                         spin_unlock(&init_mm.page_table_lock);
997                                 }
998                         }
999 
1000                         continue;
1001                 }
1002 
1003                 pte_base = (pte_t *)pmd_page_vaddr(*pmd);
1004                 remove_pte_table(pte_base, addr, next, direct);
1005                 free_pte_table(pte_base, pmd);
1006         }
1007 
1008         /* Call free_pmd_table() in remove_pud_table(). */
1009         if (direct)
1010                 update_page_count(PG_LEVEL_2M, -pages);
1011 }
1012 
1013 static void __meminit
1014 remove_pud_table(pud_t *pud_start, unsigned long addr, unsigned long end,
1015                  struct vmem_altmap *altmap, bool direct)
1016 {
1017         unsigned long next, pages = 0;
1018         pmd_t *pmd_base;
1019         pud_t *pud;
1020         void *page_addr;
1021 
1022         pud = pud_start + pud_index(addr);
1023         for (; addr < end; addr = next, pud++) {
1024                 next = pud_addr_end(addr, end);
1025 
1026                 if (!pud_present(*pud))
1027                         continue;
1028 
1029                 if (pud_large(*pud)) {
1030                         if (IS_ALIGNED(addr, PUD_SIZE) &&
1031                             IS_ALIGNED(next, PUD_SIZE)) {
1032                                 if (!direct)
1033                                         free_pagetable(pud_page(*pud),
1034                                                        get_order(PUD_SIZE));
1035 
1036                                 spin_lock(&init_mm.page_table_lock);
1037                                 pud_clear(pud);
1038                                 spin_unlock(&init_mm.page_table_lock);
1039                                 pages++;
1040                         } else {
1041                                 /* If here, we are freeing vmemmap pages. */
1042                                 memset((void *)addr, PAGE_INUSE, next - addr);
1043 
1044                                 page_addr = page_address(pud_page(*pud));
1045                                 if (!memchr_inv(page_addr, PAGE_INUSE,
1046                                                 PUD_SIZE)) {
1047                                         free_pagetable(pud_page(*pud),
1048                                                        get_order(PUD_SIZE));
1049 
1050                                         spin_lock(&init_mm.page_table_lock);
1051                                         pud_clear(pud);
1052                                         spin_unlock(&init_mm.page_table_lock);
1053                                 }
1054                         }
1055 
1056                         continue;
1057                 }
1058 
1059                 pmd_base = pmd_offset(pud, 0);
1060                 remove_pmd_table(pmd_base, addr, next, direct, altmap);
1061                 free_pmd_table(pmd_base, pud);
1062         }
1063 
1064         if (direct)
1065                 update_page_count(PG_LEVEL_1G, -pages);
1066 }
1067 
1068 static void __meminit
1069 remove_p4d_table(p4d_t *p4d_start, unsigned long addr, unsigned long end,
1070                  struct vmem_altmap *altmap, bool direct)
1071 {
1072         unsigned long next, pages = 0;
1073         pud_t *pud_base;
1074         p4d_t *p4d;
1075 
1076         p4d = p4d_start + p4d_index(addr);
1077         for (; addr < end; addr = next, p4d++) {
1078                 next = p4d_addr_end(addr, end);
1079 
1080                 if (!p4d_present(*p4d))
1081                         continue;
1082 
1083                 BUILD_BUG_ON(p4d_large(*p4d));
1084 
1085                 pud_base = pud_offset(p4d, 0);
1086                 remove_pud_table(pud_base, addr, next, altmap, direct);
1087                 /*
1088                  * For 4-level page tables we do not want to free PUDs, but in the
1089                  * 5-level case we should free them. This code will have to change
1090                  * to adapt for boot-time switching between 4 and 5 level page tables.
1091                  */
1092                 if (CONFIG_PGTABLE_LEVELS == 5)
1093                         free_pud_table(pud_base, p4d);
1094         }
1095 
1096         if (direct)
1097                 update_page_count(PG_LEVEL_512G, -pages);
1098 }
1099 
1100 /* start and end are both virtual address. */
1101 static void __meminit
1102 remove_pagetable(unsigned long start, unsigned long end, bool direct,
1103                 struct vmem_altmap *altmap)
1104 {
1105         unsigned long next;
1106         unsigned long addr;
1107         pgd_t *pgd;
1108         p4d_t *p4d;
1109 
1110         for (addr = start; addr < end; addr = next) {
1111                 next = pgd_addr_end(addr, end);
1112 
1113                 pgd = pgd_offset_k(addr);
1114                 if (!pgd_present(*pgd))
1115                         continue;
1116 
1117                 p4d = p4d_offset(pgd, 0);
1118                 remove_p4d_table(p4d, addr, next, altmap, direct);
1119         }
1120 
1121         flush_tlb_all();
1122 }
1123 
1124 void __ref vmemmap_free(unsigned long start, unsigned long end,
1125                 struct vmem_altmap *altmap)
1126 {
1127         remove_pagetable(start, end, false, altmap);
1128 }
1129 
1130 #ifdef CONFIG_MEMORY_HOTREMOVE
1131 static void __meminit
1132 kernel_physical_mapping_remove(unsigned long start, unsigned long end)
1133 {
1134         start = (unsigned long)__va(start);
1135         end = (unsigned long)__va(end);
1136 
1137         remove_pagetable(start, end, true, NULL);
1138 }
1139 
1140 int __ref arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap)
1141 {
1142         unsigned long start_pfn = start >> PAGE_SHIFT;
1143         unsigned long nr_pages = size >> PAGE_SHIFT;
1144         struct page *page = pfn_to_page(start_pfn);
1145         struct zone *zone;
1146         int ret;
1147 
1148         /* With altmap the first mapped page is offset from @start */
1149         if (altmap)
1150                 page += vmem_altmap_offset(altmap);
1151         zone = page_zone(page);
1152         ret = __remove_pages(zone, start_pfn, nr_pages, altmap);
1153         WARN_ON_ONCE(ret);
1154         kernel_physical_mapping_remove(start, start + size);
1155 
1156         return ret;
1157 }
1158 #endif
1159 #endif /* CONFIG_MEMORY_HOTPLUG */
1160 
1161 static struct kcore_list kcore_vsyscall;
1162 
1163 static void __init register_page_bootmem_info(void)
1164 {
1165 #ifdef CONFIG_NUMA
1166         int i;
1167 
1168         for_each_online_node(i)
1169                 register_page_bootmem_info_node(NODE_DATA(i));
1170 #endif
1171 }
1172 
1173 void __init mem_init(void)
1174 {
1175         pci_iommu_alloc();
1176 
1177         /* clear_bss() already clear the empty_zero_page */
1178 
1179         /* this will put all memory onto the freelists */
1180         free_all_bootmem();
1181         after_bootmem = 1;
1182 
1183         /*
1184          * Must be done after boot memory is put on freelist, because here we
1185          * might set fields in deferred struct pages that have not yet been
1186          * initialized, and free_all_bootmem() initializes all the reserved
1187          * deferred pages for us.
1188          */
1189         register_page_bootmem_info();
1190 
1191         /* Register memory areas for /proc/kcore */
1192         if (get_gate_vma(&init_mm))
1193                 kclist_add(&kcore_vsyscall, (void *)VSYSCALL_ADDR, PAGE_SIZE, KCORE_USER);
1194 
1195         mem_init_print_info(NULL);
1196 }
1197 
1198 int kernel_set_to_readonly;
1199 
1200 void set_kernel_text_rw(void)
1201 {
1202         unsigned long start = PFN_ALIGN(_text);
1203         unsigned long end = PFN_ALIGN(__stop___ex_table);
1204 
1205         if (!kernel_set_to_readonly)
1206                 return;
1207 
1208         pr_debug("Set kernel text: %lx - %lx for read write\n",
1209                  start, end);
1210 
1211         /*
1212          * Make the kernel identity mapping for text RW. Kernel text
1213          * mapping will always be RO. Refer to the comment in
1214          * static_protections() in pageattr.c
1215          */
1216         set_memory_rw(start, (end - start) >> PAGE_SHIFT);
1217 }
1218 
1219 void set_kernel_text_ro(void)
1220 {
1221         unsigned long start = PFN_ALIGN(_text);
1222         unsigned long end = PFN_ALIGN(__stop___ex_table);
1223 
1224         if (!kernel_set_to_readonly)
1225                 return;
1226 
1227         pr_debug("Set kernel text: %lx - %lx for read only\n",
1228                  start, end);
1229 
1230         /*
1231          * Set the kernel identity mapping for text RO.
1232          */
1233         set_memory_ro(start, (end - start) >> PAGE_SHIFT);
1234 }
1235 
1236 void mark_rodata_ro(void)
1237 {
1238         unsigned long start = PFN_ALIGN(_text);
1239         unsigned long rodata_start = PFN_ALIGN(__start_rodata);
1240         unsigned long end = (unsigned long) &__end_rodata_hpage_align;
1241         unsigned long text_end = PFN_ALIGN(&__stop___ex_table);
1242         unsigned long rodata_end = PFN_ALIGN(&__end_rodata);
1243         unsigned long all_end;
1244 
1245         printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
1246                (end - start) >> 10);
1247         set_memory_ro(start, (end - start) >> PAGE_SHIFT);
1248 
1249         kernel_set_to_readonly = 1;
1250 
1251         /*
1252          * The rodata/data/bss/brk section (but not the kernel text!)
1253          * should also be not-executable.
1254          *
1255          * We align all_end to PMD_SIZE because the existing mapping
1256          * is a full PMD. If we would align _brk_end to PAGE_SIZE we
1257          * split the PMD and the reminder between _brk_end and the end
1258          * of the PMD will remain mapped executable.
1259          *
1260          * Any PMD which was setup after the one which covers _brk_end
1261          * has been zapped already via cleanup_highmem().
1262          */
1263         all_end = roundup((unsigned long)_brk_end, PMD_SIZE);
1264         set_memory_nx(text_end, (all_end - text_end) >> PAGE_SHIFT);
1265 
1266 #ifdef CONFIG_CPA_DEBUG
1267         printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, end);
1268         set_memory_rw(start, (end-start) >> PAGE_SHIFT);
1269 
1270         printk(KERN_INFO "Testing CPA: again\n");
1271         set_memory_ro(start, (end-start) >> PAGE_SHIFT);
1272 #endif
1273 
1274         free_init_pages("unused kernel",
1275                         (unsigned long) __va(__pa_symbol(text_end)),
1276                         (unsigned long) __va(__pa_symbol(rodata_start)));
1277         free_init_pages("unused kernel",
1278                         (unsigned long) __va(__pa_symbol(rodata_end)),
1279                         (unsigned long) __va(__pa_symbol(_sdata)));
1280 
1281         debug_checkwx();
1282 }
1283 
1284 int kern_addr_valid(unsigned long addr)
1285 {
1286         unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT;
1287         pgd_t *pgd;
1288         p4d_t *p4d;
1289         pud_t *pud;
1290         pmd_t *pmd;
1291         pte_t *pte;
1292 
1293         if (above != 0 && above != -1UL)
1294                 return 0;
1295 
1296         pgd = pgd_offset_k(addr);
1297         if (pgd_none(*pgd))
1298                 return 0;
1299 
1300         p4d = p4d_offset(pgd, addr);
1301         if (p4d_none(*p4d))
1302                 return 0;
1303 
1304         pud = pud_offset(p4d, addr);
1305         if (pud_none(*pud))
1306                 return 0;
1307 
1308         if (pud_large(*pud))
1309                 return pfn_valid(pud_pfn(*pud));
1310 
1311         pmd = pmd_offset(pud, addr);
1312         if (pmd_none(*pmd))
1313                 return 0;
1314 
1315         if (pmd_large(*pmd))
1316                 return pfn_valid(pmd_pfn(*pmd));
1317 
1318         pte = pte_offset_kernel(pmd, addr);
1319         if (pte_none(*pte))
1320                 return 0;
1321 
1322         return pfn_valid(pte_pfn(*pte));
1323 }
1324 
1325 static unsigned long probe_memory_block_size(void)
1326 {
1327         unsigned long bz = MIN_MEMORY_BLOCK_SIZE;
1328 
1329         /* if system is UV or has 64GB of RAM or more, use large blocks */
1330         if (is_uv_system() || ((max_pfn << PAGE_SHIFT) >= (64UL << 30)))
1331                 bz = 2UL << 30; /* 2GB */
1332 
1333         pr_info("x86/mm: Memory block size: %ldMB\n", bz >> 20);
1334 
1335         return bz;
1336 }
1337 
1338 static unsigned long memory_block_size_probed;
1339 unsigned long memory_block_size_bytes(void)
1340 {
1341         if (!memory_block_size_probed)
1342                 memory_block_size_probed = probe_memory_block_size();
1343 
1344         return memory_block_size_probed;
1345 }
1346 
1347 #ifdef CONFIG_SPARSEMEM_VMEMMAP
1348 /*
1349  * Initialise the sparsemem vmemmap using huge-pages at the PMD level.
1350  */
1351 static long __meminitdata addr_start, addr_end;
1352 static void __meminitdata *p_start, *p_end;
1353 static int __meminitdata node_start;
1354 
1355 static int __meminit vmemmap_populate_hugepages(unsigned long start,
1356                 unsigned long end, int node, struct vmem_altmap *altmap)
1357 {
1358         unsigned long addr;
1359         unsigned long next;
1360         pgd_t *pgd;
1361         p4d_t *p4d;
1362         pud_t *pud;
1363         pmd_t *pmd;
1364 
1365         for (addr = start; addr < end; addr = next) {
1366                 next = pmd_addr_end(addr, end);
1367 
1368                 pgd = vmemmap_pgd_populate(addr, node);
1369                 if (!pgd)
1370                         return -ENOMEM;
1371 
1372                 p4d = vmemmap_p4d_populate(pgd, addr, node);
1373                 if (!p4d)
1374                         return -ENOMEM;
1375 
1376                 pud = vmemmap_pud_populate(p4d, addr, node);
1377                 if (!pud)
1378                         return -ENOMEM;
1379 
1380                 pmd = pmd_offset(pud, addr);
1381                 if (pmd_none(*pmd)) {
1382                         void *p;
1383 
1384                         if (altmap)
1385                                 p = altmap_alloc_block_buf(PMD_SIZE, altmap);
1386                         else
1387                                 p = vmemmap_alloc_block_buf(PMD_SIZE, node);
1388                         if (p) {
1389                                 pte_t entry;
1390 
1391                                 entry = pfn_pte(__pa(p) >> PAGE_SHIFT,
1392                                                 PAGE_KERNEL_LARGE);
1393                                 set_pmd(pmd, __pmd(pte_val(entry)));
1394 
1395                                 /* check to see if we have contiguous blocks */
1396                                 if (p_end != p || node_start != node) {
1397                                         if (p_start)
1398                                                 pr_debug(" [%lx-%lx] PMD -> [%p-%p] on node %d\n",
1399                                                        addr_start, addr_end-1, p_start, p_end-1, node_start);
1400                                         addr_start = addr;
1401                                         node_start = node;
1402                                         p_start = p;
1403                                 }
1404 
1405                                 addr_end = addr + PMD_SIZE;
1406                                 p_end = p + PMD_SIZE;
1407                                 continue;
1408                         } else if (altmap)
1409                                 return -ENOMEM; /* no fallback */
1410                 } else if (pmd_large(*pmd)) {
1411                         vmemmap_verify((pte_t *)pmd, node, addr, next);
1412                         continue;
1413                 }
1414                 if (vmemmap_populate_basepages(addr, next, node))
1415                         return -ENOMEM;
1416         }
1417         return 0;
1418 }
1419 
1420 int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
1421                 struct vmem_altmap *altmap)
1422 {
1423         int err;
1424 
1425         if (boot_cpu_has(X86_FEATURE_PSE))
1426                 err = vmemmap_populate_hugepages(start, end, node, altmap);
1427         else if (altmap) {
1428                 pr_err_once("%s: no cpu support for altmap allocations\n",
1429                                 __func__);
1430                 err = -ENOMEM;
1431         } else
1432                 err = vmemmap_populate_basepages(start, end, node);
1433         if (!err)
1434                 sync_global_pgds(start, end - 1);
1435         return err;
1436 }
1437 
1438 #if defined(CONFIG_MEMORY_HOTPLUG_SPARSE) && defined(CONFIG_HAVE_BOOTMEM_INFO_NODE)
1439 void register_page_bootmem_memmap(unsigned long section_nr,
1440                                   struct page *start_page, unsigned long nr_pages)
1441 {
1442         unsigned long addr = (unsigned long)start_page;
1443         unsigned long end = (unsigned long)(start_page + nr_pages);
1444         unsigned long next;
1445         pgd_t *pgd;
1446         p4d_t *p4d;
1447         pud_t *pud;
1448         pmd_t *pmd;
1449         unsigned int nr_pmd_pages;
1450         struct page *page;
1451 
1452         for (; addr < end; addr = next) {
1453                 pte_t *pte = NULL;
1454 
1455                 pgd = pgd_offset_k(addr);
1456                 if (pgd_none(*pgd)) {
1457                         next = (addr + PAGE_SIZE) & PAGE_MASK;
1458                         continue;
1459                 }
1460                 get_page_bootmem(section_nr, pgd_page(*pgd), MIX_SECTION_INFO);
1461 
1462                 p4d = p4d_offset(pgd, addr);
1463                 if (p4d_none(*p4d)) {
1464                         next = (addr + PAGE_SIZE) & PAGE_MASK;
1465                         continue;
1466                 }
1467                 get_page_bootmem(section_nr, p4d_page(*p4d), MIX_SECTION_INFO);
1468 
1469                 pud = pud_offset(p4d, addr);
1470                 if (pud_none(*pud)) {
1471                         next = (addr + PAGE_SIZE) & PAGE_MASK;
1472                         continue;
1473                 }
1474                 get_page_bootmem(section_nr, pud_page(*pud), MIX_SECTION_INFO);
1475 
1476                 if (!boot_cpu_has(X86_FEATURE_PSE)) {
1477                         next = (addr + PAGE_SIZE) & PAGE_MASK;
1478                         pmd = pmd_offset(pud, addr);
1479                         if (pmd_none(*pmd))
1480                                 continue;
1481                         get_page_bootmem(section_nr, pmd_page(*pmd),
1482                                          MIX_SECTION_INFO);
1483 
1484                         pte = pte_offset_kernel(pmd, addr);
1485                         if (pte_none(*pte))
1486                                 continue;
1487                         get_page_bootmem(section_nr, pte_page(*pte),
1488                                          SECTION_INFO);
1489                 } else {
1490                         next = pmd_addr_end(addr, end);
1491 
1492                         pmd = pmd_offset(pud, addr);
1493                         if (pmd_none(*pmd))
1494                                 continue;
1495 
1496                         nr_pmd_pages = 1 << get_order(PMD_SIZE);
1497                         page = pmd_page(*pmd);
1498                         while (nr_pmd_pages--)
1499                                 get_page_bootmem(section_nr, page++,
1500                                                  SECTION_INFO);
1501                 }
1502         }
1503 }
1504 #endif
1505 
1506 void __meminit vmemmap_populate_print_last(void)
1507 {
1508         if (p_start) {
1509                 pr_debug(" [%lx-%lx] PMD -> [%p-%p] on node %d\n",
1510                         addr_start, addr_end-1, p_start, p_end-1, node_start);
1511                 p_start = NULL;
1512                 p_end = NULL;
1513                 node_start = 0;
1514         }
1515 }
1516 #endif
1517 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp