init_64.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326
  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. #include <linux/signal.h>
  9. #include <linux/sched.h>
  10. #include <linux/kernel.h>
  11. #include <linux/errno.h>
  12. #include <linux/string.h>
  13. #include <linux/types.h>
  14. #include <linux/ptrace.h>
  15. #include <linux/mman.h>
  16. #include <linux/mm.h>
  17. #include <linux/swap.h>
  18. #include <linux/smp.h>
  19. #include <linux/init.h>
  20. #include <linux/initrd.h>
  21. #include <linux/pagemap.h>
  22. #include <linux/bootmem.h>
  23. #include <linux/memblock.h>
  24. #include <linux/proc_fs.h>
  25. #include <linux/pci.h>
  26. #include <linux/pfn.h>
  27. #include <linux/poison.h>
  28. #include <linux/dma-mapping.h>
  29. #include <linux/memory.h>
  30. #include <linux/memory_hotplug.h>
  31. #include <linux/memremap.h>
  32. #include <linux/nmi.h>
  33. #include <linux/gfp.h>
  34. #include <linux/kcore.h>
  35. #include <asm/processor.h>
  36. #include <asm/bios_ebda.h>
  37. #include <asm/uaccess.h>
  38. #include <asm/pgtable.h>
  39. #include <asm/pgalloc.h>
  40. #include <asm/dma.h>
  41. #include <asm/fixmap.h>
  42. #include <asm/e820.h>
  43. #include <asm/apic.h>
  44. #include <asm/tlb.h>
  45. #include <asm/mmu_context.h>
  46. #include <asm/proto.h>
  47. #include <asm/smp.h>
  48. #include <asm/sections.h>
  49. #include <asm/kdebug.h>
  50. #include <asm/numa.h>
  51. #include <asm/cacheflush.h>
  52. #include <asm/init.h>
  53. #include <asm/uv/uv.h>
  54. #include <asm/setup.h>
  55. #include "mm_internal.h"
  56. #include "ident_map.c"
  57. /*
  58. * NOTE: pagetable_init alloc all the fixmap pagetables contiguous on the
  59. * physical space so we can cache the place of the first one and move
  60. * around without checking the pgd every time.
  61. */
  62. pteval_t __supported_pte_mask __read_mostly = ~0;
  63. EXPORT_SYMBOL_GPL(__supported_pte_mask);
  64. int force_personality32;
  65. /*
  66. * noexec32=on|off
  67. * Control non executable heap for 32bit processes.
  68. * To control the stack too use noexec=off
  69. *
  70. * on PROT_READ does not imply PROT_EXEC for 32-bit processes (default)
  71. * off PROT_READ implies PROT_EXEC
  72. */
  73. static int __init nonx32_setup(char *str)
  74. {
  75. if (!strcmp(str, "on"))
  76. force_personality32 &= ~READ_IMPLIES_EXEC;
  77. else if (!strcmp(str, "off"))
  78. force_personality32 |= READ_IMPLIES_EXEC;
  79. return 1;
  80. }
  81. __setup("noexec32=", nonx32_setup);
  82. /*
  83. * When memory was added/removed make sure all the processes MM have
  84. * suitable PGD entries in the local PGD level page.
  85. */
  86. void sync_global_pgds(unsigned long start, unsigned long end, int removed)
  87. {
  88. unsigned long addr;
  89. for (addr = start; addr <= end; addr = ALIGN(addr + 1, PGDIR_SIZE)) {
  90. const pgd_t *pgd_ref = pgd_offset_k(addr);
  91. struct page *page;
  92. /*
  93. * When it is called after memory hot remove, pgd_none()
  94. * returns true. In this case (removed == 1), we must clear
  95. * the PGD entries in the local PGD level page.
  96. */
  97. if (pgd_none(*pgd_ref) && !removed)
  98. continue;
  99. spin_lock(&pgd_lock);
  100. list_for_each_entry(page, &pgd_list, lru) {
  101. pgd_t *pgd;
  102. spinlock_t *pgt_lock;
  103. pgd = (pgd_t *)page_address(page) + pgd_index(addr);
  104. /* the pgt_lock only for Xen */
  105. pgt_lock = &pgd_page_get_mm(page)->page_table_lock;
  106. spin_lock(pgt_lock);
  107. if (!pgd_none(*pgd_ref) && !pgd_none(*pgd))
  108. BUG_ON(pgd_page_vaddr(*pgd)
  109. != pgd_page_vaddr(*pgd_ref));
  110. if (removed) {
  111. if (pgd_none(*pgd_ref) && !pgd_none(*pgd))
  112. pgd_clear(pgd);
  113. } else {
  114. if (pgd_none(*pgd))
  115. set_pgd(pgd, *pgd_ref);
  116. }
  117. spin_unlock(pgt_lock);
  118. }
  119. spin_unlock(&pgd_lock);
  120. }
  121. }
  122. /*
  123. * NOTE: This function is marked __ref because it calls __init function
  124. * (alloc_bootmem_pages). It's safe to do it ONLY when after_bootmem == 0.
  125. */
  126. static __ref void *spp_getpage(void)
  127. {
  128. void *ptr;
  129. if (after_bootmem)
  130. ptr = (void *) get_zeroed_page(GFP_ATOMIC | __GFP_NOTRACK);
  131. else
  132. ptr = alloc_bootmem_pages(PAGE_SIZE);
  133. if (!ptr || ((unsigned long)ptr & ~PAGE_MASK)) {
  134. panic("set_pte_phys: cannot allocate page data %s\n",
  135. after_bootmem ? "after bootmem" : "");
  136. }
  137. pr_debug("spp_getpage %p\n", ptr);
  138. return ptr;
  139. }
  140. static pud_t *fill_pud(pgd_t *pgd, unsigned long vaddr)
  141. {
  142. if (pgd_none(*pgd)) {
  143. pud_t *pud = (pud_t *)spp_getpage();
  144. pgd_populate(&init_mm, pgd, pud);
  145. if (pud != pud_offset(pgd, 0))
  146. printk(KERN_ERR "PAGETABLE BUG #00! %p <-> %p\n",
  147. pud, pud_offset(pgd, 0));
  148. }
  149. return pud_offset(pgd, vaddr);
  150. }
  151. static pmd_t *fill_pmd(pud_t *pud, unsigned long vaddr)
  152. {
  153. if (pud_none(*pud)) {
  154. pmd_t *pmd = (pmd_t *) spp_getpage();
  155. pud_populate(&init_mm, pud, pmd);
  156. if (pmd != pmd_offset(pud, 0))
  157. printk(KERN_ERR "PAGETABLE BUG #01! %p <-> %p\n",
  158. pmd, pmd_offset(pud, 0));
  159. }
  160. return pmd_offset(pud, vaddr);
  161. }
  162. static pte_t *fill_pte(pmd_t *pmd, unsigned long vaddr)
  163. {
  164. if (pmd_none(*pmd)) {
  165. pte_t *pte = (pte_t *) spp_getpage();
  166. pmd_populate_kernel(&init_mm, pmd, pte);
  167. if (pte != pte_offset_kernel(pmd, 0))
  168. printk(KERN_ERR "PAGETABLE BUG #02!\n");
  169. }
  170. return pte_offset_kernel(pmd, vaddr);
  171. }
  172. void set_pte_vaddr_pud(pud_t *pud_page, unsigned long vaddr, pte_t new_pte)
  173. {
  174. pud_t *pud;
  175. pmd_t *pmd;
  176. pte_t *pte;
  177. pud = pud_page + pud_index(vaddr);
  178. pmd = fill_pmd(pud, vaddr);
  179. pte = fill_pte(pmd, vaddr);
  180. set_pte(pte, new_pte);
  181. /*
  182. * It's enough to flush this one mapping.
  183. * (PGE mappings get flushed as well)
  184. */
  185. __flush_tlb_one(vaddr);
  186. }
  187. void set_pte_vaddr(unsigned long vaddr, pte_t pteval)
  188. {
  189. pgd_t *pgd;
  190. pud_t *pud_page;
  191. pr_debug("set_pte_vaddr %lx to %lx\n", vaddr, native_pte_val(pteval));
  192. pgd = pgd_offset_k(vaddr);
  193. if (pgd_none(*pgd)) {
  194. printk(KERN_ERR
  195. "PGD FIXMAP MISSING, it should be setup in head.S!\n");
  196. return;
  197. }
  198. pud_page = (pud_t*)pgd_page_vaddr(*pgd);
  199. set_pte_vaddr_pud(pud_page, vaddr, pteval);
  200. }
  201. pmd_t * __init populate_extra_pmd(unsigned long vaddr)
  202. {
  203. pgd_t *pgd;
  204. pud_t *pud;
  205. pgd = pgd_offset_k(vaddr);
  206. pud = fill_pud(pgd, vaddr);
  207. return fill_pmd(pud, vaddr);
  208. }
  209. pte_t * __init populate_extra_pte(unsigned long vaddr)
  210. {
  211. pmd_t *pmd;
  212. pmd = populate_extra_pmd(vaddr);
  213. return fill_pte(pmd, vaddr);
  214. }
  215. /*
  216. * Create large page table mappings for a range of physical addresses.
  217. */
  218. static void __init __init_extra_mapping(unsigned long phys, unsigned long size,
  219. enum page_cache_mode cache)
  220. {
  221. pgd_t *pgd;
  222. pud_t *pud;
  223. pmd_t *pmd;
  224. pgprot_t prot;
  225. pgprot_val(prot) = pgprot_val(PAGE_KERNEL_LARGE) |
  226. pgprot_val(pgprot_4k_2_large(cachemode2pgprot(cache)));
  227. BUG_ON((phys & ~PMD_MASK) || (size & ~PMD_MASK));
  228. for (; size; phys += PMD_SIZE, size -= PMD_SIZE) {
  229. pgd = pgd_offset_k((unsigned long)__va(phys));
  230. if (pgd_none(*pgd)) {
  231. pud = (pud_t *) spp_getpage();
  232. set_pgd(pgd, __pgd(__pa(pud) | _KERNPG_TABLE |
  233. _PAGE_USER));
  234. }
  235. pud = pud_offset(pgd, (unsigned long)__va(phys));
  236. if (pud_none(*pud)) {
  237. pmd = (pmd_t *) spp_getpage();
  238. set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE |
  239. _PAGE_USER));
  240. }
  241. pmd = pmd_offset(pud, phys);
  242. BUG_ON(!pmd_none(*pmd));
  243. set_pmd(pmd, __pmd(phys | pgprot_val(prot)));
  244. }
  245. }
  246. void __init init_extra_mapping_wb(unsigned long phys, unsigned long size)
  247. {
  248. __init_extra_mapping(phys, size, _PAGE_CACHE_MODE_WB);
  249. }
  250. void __init init_extra_mapping_uc(unsigned long phys, unsigned long size)
  251. {
  252. __init_extra_mapping(phys, size, _PAGE_CACHE_MODE_UC);
  253. }
  254. /*
  255. * The head.S code sets up the kernel high mapping:
  256. *
  257. * from __START_KERNEL_map to __START_KERNEL_map + size (== _end-_text)
  258. *
  259. * phys_base holds the negative offset to the kernel, which is added
  260. * to the compile time generated pmds. This results in invalid pmds up
  261. * to the point where we hit the physaddr 0 mapping.
  262. *
  263. * We limit the mappings to the region from _text to _brk_end. _brk_end
  264. * is rounded up to the 2MB boundary. This catches the invalid pmds as
  265. * well, as they are located before _text:
  266. */
  267. void __init cleanup_highmap(void)
  268. {
  269. unsigned long vaddr = __START_KERNEL_map;
  270. unsigned long vaddr_end = __START_KERNEL_map + KERNEL_IMAGE_SIZE;
  271. unsigned long end = roundup((unsigned long)_brk_end, PMD_SIZE) - 1;
  272. pmd_t *pmd = level2_kernel_pgt;
  273. /*
  274. * Native path, max_pfn_mapped is not set yet.
  275. * Xen has valid max_pfn_mapped set in
  276. * arch/x86/xen/mmu.c:xen_setup_kernel_pagetable().
  277. */
  278. if (max_pfn_mapped)
  279. vaddr_end = __START_KERNEL_map + (max_pfn_mapped << PAGE_SHIFT);
  280. for (; vaddr + PMD_SIZE - 1 < vaddr_end; pmd++, vaddr += PMD_SIZE) {
  281. if (pmd_none(*pmd))
  282. continue;
  283. if (vaddr < (unsigned long) _text || vaddr > end)
  284. set_pmd(pmd, __pmd(0));
  285. else if (kaiser_enabled) {
  286. /*
  287. * level2_kernel_pgt is initialized with _PAGE_GLOBAL:
  288. * clear that now. This is not important, so long as
  289. * CR4.PGE remains clear, but it removes an anomaly.
  290. * Physical mapping setup below avoids _PAGE_GLOBAL
  291. * by use of massage_pgprot() inside pfn_pte() etc.
  292. */
  293. set_pmd(pmd, pmd_clear_flags(*pmd, _PAGE_GLOBAL));
  294. }
  295. }
  296. }
  297. /*
  298. * Create PTE level page table mapping for physical addresses.
  299. * It returns the last physical address mapped.
  300. */
  301. static unsigned long __meminit
  302. phys_pte_init(pte_t *pte_page, unsigned long paddr, unsigned long paddr_end,
  303. pgprot_t prot)
  304. {
  305. unsigned long pages = 0, paddr_next;
  306. unsigned long paddr_last = paddr_end;
  307. pte_t *pte;
  308. int i;
  309. pte = pte_page + pte_index(paddr);
  310. i = pte_index(paddr);
  311. for (; i < PTRS_PER_PTE; i++, paddr = paddr_next, pte++) {
  312. paddr_next = (paddr & PAGE_MASK) + PAGE_SIZE;
  313. if (paddr >= paddr_end) {
  314. if (!after_bootmem &&
  315. !e820_any_mapped(paddr & PAGE_MASK, paddr_next,
  316. E820_RAM) &&
  317. !e820_any_mapped(paddr & PAGE_MASK, paddr_next,
  318. E820_RESERVED_KERN))
  319. set_pte(pte, __pte(0));
  320. continue;
  321. }
  322. /*
  323. * We will re-use the existing mapping.
  324. * Xen for example has some special requirements, like mapping
  325. * pagetable pages as RO. So assume someone who pre-setup
  326. * these mappings are more intelligent.
  327. */
  328. if (!pte_none(*pte)) {
  329. if (!after_bootmem)
  330. pages++;
  331. continue;
  332. }
  333. if (0)
  334. pr_info(" pte=%p addr=%lx pte=%016lx\n", pte, paddr,
  335. pfn_pte(paddr >> PAGE_SHIFT, PAGE_KERNEL).pte);
  336. pages++;
  337. set_pte(pte, pfn_pte(paddr >> PAGE_SHIFT, prot));
  338. paddr_last = (paddr & PAGE_MASK) + PAGE_SIZE;
  339. }
  340. update_page_count(PG_LEVEL_4K, pages);
  341. return paddr_last;
  342. }
  343. /*
  344. * Create PMD level page table mapping for physical addresses. The virtual
  345. * and physical address have to be aligned at this level.
  346. * It returns the last physical address mapped.
  347. */
  348. static unsigned long __meminit
  349. phys_pmd_init(pmd_t *pmd_page, unsigned long paddr, unsigned long paddr_end,
  350. unsigned long page_size_mask, pgprot_t prot)
  351. {
  352. unsigned long pages = 0, paddr_next;
  353. unsigned long paddr_last = paddr_end;
  354. int i = pmd_index(paddr);
  355. for (; i < PTRS_PER_PMD; i++, paddr = paddr_next) {
  356. pmd_t *pmd = pmd_page + pmd_index(paddr);
  357. pte_t *pte;
  358. pgprot_t new_prot = prot;
  359. paddr_next = (paddr & PMD_MASK) + PMD_SIZE;
  360. if (paddr >= paddr_end) {
  361. if (!after_bootmem &&
  362. !e820_any_mapped(paddr & PMD_MASK, paddr_next,
  363. E820_RAM) &&
  364. !e820_any_mapped(paddr & PMD_MASK, paddr_next,
  365. E820_RESERVED_KERN))
  366. set_pmd(pmd, __pmd(0));
  367. continue;
  368. }
  369. if (!pmd_none(*pmd)) {
  370. if (!pmd_large(*pmd)) {
  371. spin_lock(&init_mm.page_table_lock);
  372. pte = (pte_t *)pmd_page_vaddr(*pmd);
  373. paddr_last = phys_pte_init(pte, paddr,
  374. paddr_end, prot);
  375. spin_unlock(&init_mm.page_table_lock);
  376. continue;
  377. }
  378. /*
  379. * If we are ok with PG_LEVEL_2M mapping, then we will
  380. * use the existing mapping,
  381. *
  382. * Otherwise, we will split the large page mapping but
  383. * use the same existing protection bits except for
  384. * large page, so that we don't violate Intel's TLB
  385. * Application note (317080) which says, while changing
  386. * the page sizes, new and old translations should
  387. * not differ with respect to page frame and
  388. * attributes.
  389. */
  390. if (page_size_mask & (1 << PG_LEVEL_2M)) {
  391. if (!after_bootmem)
  392. pages++;
  393. paddr_last = paddr_next;
  394. continue;
  395. }
  396. new_prot = pte_pgprot(pte_clrhuge(*(pte_t *)pmd));
  397. }
  398. if (page_size_mask & (1<<PG_LEVEL_2M)) {
  399. pages++;
  400. spin_lock(&init_mm.page_table_lock);
  401. set_pte((pte_t *)pmd,
  402. pfn_pte((paddr & PMD_MASK) >> PAGE_SHIFT,
  403. __pgprot(pgprot_val(prot) | _PAGE_PSE)));
  404. spin_unlock(&init_mm.page_table_lock);
  405. paddr_last = paddr_next;
  406. continue;
  407. }
  408. pte = alloc_low_page();
  409. paddr_last = phys_pte_init(pte, paddr, paddr_end, new_prot);
  410. spin_lock(&init_mm.page_table_lock);
  411. pmd_populate_kernel(&init_mm, pmd, pte);
  412. spin_unlock(&init_mm.page_table_lock);
  413. }
  414. update_page_count(PG_LEVEL_2M, pages);
  415. return paddr_last;
  416. }
  417. /*
  418. * Create PUD level page table mapping for physical addresses. The virtual
  419. * and physical address do not have to be aligned at this level. KASLR can
  420. * randomize virtual addresses up to this level.
  421. * It returns the last physical address mapped.
  422. */
  423. static unsigned long __meminit
  424. phys_pud_init(pud_t *pud_page, unsigned long paddr, unsigned long paddr_end,
  425. unsigned long page_size_mask)
  426. {
  427. unsigned long pages = 0, paddr_next;
  428. unsigned long paddr_last = paddr_end;
  429. unsigned long vaddr = (unsigned long)__va(paddr);
  430. int i = pud_index(vaddr);
  431. for (; i < PTRS_PER_PUD; i++, paddr = paddr_next) {
  432. pud_t *pud;
  433. pmd_t *pmd;
  434. pgprot_t prot = PAGE_KERNEL;
  435. vaddr = (unsigned long)__va(paddr);
  436. pud = pud_page + pud_index(vaddr);
  437. paddr_next = (paddr & PUD_MASK) + PUD_SIZE;
  438. if (paddr >= paddr_end) {
  439. if (!after_bootmem &&
  440. !e820_any_mapped(paddr & PUD_MASK, paddr_next,
  441. E820_RAM) &&
  442. !e820_any_mapped(paddr & PUD_MASK, paddr_next,
  443. E820_RESERVED_KERN))
  444. set_pud(pud, __pud(0));
  445. continue;
  446. }
  447. if (!pud_none(*pud)) {
  448. if (!pud_large(*pud)) {
  449. pmd = pmd_offset(pud, 0);
  450. paddr_last = phys_pmd_init(pmd, paddr,
  451. paddr_end,
  452. page_size_mask,
  453. prot);
  454. __flush_tlb_all();
  455. continue;
  456. }
  457. /*
  458. * If we are ok with PG_LEVEL_1G mapping, then we will
  459. * use the existing mapping.
  460. *
  461. * Otherwise, we will split the gbpage mapping but use
  462. * the same existing protection bits except for large
  463. * page, so that we don't violate Intel's TLB
  464. * Application note (317080) which says, while changing
  465. * the page sizes, new and old translations should
  466. * not differ with respect to page frame and
  467. * attributes.
  468. */
  469. if (page_size_mask & (1 << PG_LEVEL_1G)) {
  470. if (!after_bootmem)
  471. pages++;
  472. paddr_last = paddr_next;
  473. continue;
  474. }
  475. prot = pte_pgprot(pte_clrhuge(*(pte_t *)pud));
  476. }
  477. if (page_size_mask & (1<<PG_LEVEL_1G)) {
  478. pages++;
  479. spin_lock(&init_mm.page_table_lock);
  480. set_pte((pte_t *)pud,
  481. pfn_pte((paddr & PUD_MASK) >> PAGE_SHIFT,
  482. PAGE_KERNEL_LARGE));
  483. spin_unlock(&init_mm.page_table_lock);
  484. paddr_last = paddr_next;
  485. continue;
  486. }
  487. pmd = alloc_low_page();
  488. paddr_last = phys_pmd_init(pmd, paddr, paddr_end,
  489. page_size_mask, prot);
  490. spin_lock(&init_mm.page_table_lock);
  491. pud_populate(&init_mm, pud, pmd);
  492. spin_unlock(&init_mm.page_table_lock);
  493. }
  494. __flush_tlb_all();
  495. update_page_count(PG_LEVEL_1G, pages);
  496. return paddr_last;
  497. }
  498. /*
  499. * Create page table mapping for the physical memory for specific physical
  500. * addresses. The virtual and physical addresses have to be aligned on PMD level
  501. * down. It returns the last physical address mapped.
  502. */
  503. unsigned long __meminit
  504. kernel_physical_mapping_init(unsigned long paddr_start,
  505. unsigned long paddr_end,
  506. unsigned long page_size_mask)
  507. {
  508. bool pgd_changed = false;
  509. unsigned long vaddr, vaddr_start, vaddr_end, vaddr_next, paddr_last;
  510. paddr_last = paddr_end;
  511. vaddr = (unsigned long)__va(paddr_start);
  512. vaddr_end = (unsigned long)__va(paddr_end);
  513. vaddr_start = vaddr;
  514. for (; vaddr < vaddr_end; vaddr = vaddr_next) {
  515. pgd_t *pgd = pgd_offset_k(vaddr);
  516. pud_t *pud;
  517. vaddr_next = (vaddr & PGDIR_MASK) + PGDIR_SIZE;
  518. if (pgd_val(*pgd)) {
  519. pud = (pud_t *)pgd_page_vaddr(*pgd);
  520. paddr_last = phys_pud_init(pud, __pa(vaddr),
  521. __pa(vaddr_end),
  522. page_size_mask);
  523. continue;
  524. }
  525. pud = alloc_low_page();
  526. paddr_last = phys_pud_init(pud, __pa(vaddr), __pa(vaddr_end),
  527. page_size_mask);
  528. spin_lock(&init_mm.page_table_lock);
  529. pgd_populate(&init_mm, pgd, pud);
  530. spin_unlock(&init_mm.page_table_lock);
  531. pgd_changed = true;
  532. }
  533. if (pgd_changed)
  534. sync_global_pgds(vaddr_start, vaddr_end - 1, 0);
  535. __flush_tlb_all();
  536. return paddr_last;
  537. }
  538. #ifndef CONFIG_NUMA
  539. void __init initmem_init(void)
  540. {
  541. memblock_set_node(0, (phys_addr_t)ULLONG_MAX, &memblock.memory, 0);
  542. }
  543. #endif
  544. void __init paging_init(void)
  545. {
  546. sparse_memory_present_with_active_regions(MAX_NUMNODES);
  547. sparse_init();
  548. /*
  549. * clear the default setting with node 0
  550. * note: don't use nodes_clear here, that is really clearing when
  551. * numa support is not compiled in, and later node_set_state
  552. * will not set it back.
  553. */
  554. node_clear_state(0, N_MEMORY);
  555. if (N_MEMORY != N_NORMAL_MEMORY)
  556. node_clear_state(0, N_NORMAL_MEMORY);
  557. zone_sizes_init();
  558. }
  559. /*
  560. * Memory hotplug specific functions
  561. */
  562. #ifdef CONFIG_MEMORY_HOTPLUG
  563. /*
  564. * After memory hotplug the variables max_pfn, max_low_pfn and high_memory need
  565. * updating.
  566. */
  567. static void update_end_of_memory_vars(u64 start, u64 size)
  568. {
  569. unsigned long end_pfn = PFN_UP(start + size);
  570. if (end_pfn > max_pfn) {
  571. max_pfn = end_pfn;
  572. max_low_pfn = end_pfn;
  573. high_memory = (void *)__va(max_pfn * PAGE_SIZE - 1) + 1;
  574. }
  575. }
  576. /*
  577. * Memory is added always to NORMAL zone. This means you will never get
  578. * additional DMA/DMA32 memory.
  579. */
  580. int arch_add_memory(int nid, u64 start, u64 size, bool for_device)
  581. {
  582. struct pglist_data *pgdat = NODE_DATA(nid);
  583. struct zone *zone = pgdat->node_zones +
  584. zone_for_memory(nid, start, size, ZONE_NORMAL, for_device);
  585. unsigned long start_pfn = start >> PAGE_SHIFT;
  586. unsigned long nr_pages = size >> PAGE_SHIFT;
  587. int ret;
  588. init_memory_mapping(start, start + size);
  589. ret = __add_pages(nid, zone, start_pfn, nr_pages);
  590. WARN_ON_ONCE(ret);
  591. /* update max_pfn, max_low_pfn and high_memory */
  592. update_end_of_memory_vars(start, size);
  593. return ret;
  594. }
  595. EXPORT_SYMBOL_GPL(arch_add_memory);
  596. #define PAGE_INUSE 0xFD
  597. static void __meminit free_pagetable(struct page *page, int order)
  598. {
  599. unsigned long magic;
  600. unsigned int nr_pages = 1 << order;
  601. struct vmem_altmap *altmap = to_vmem_altmap((unsigned long) page);
  602. if (altmap) {
  603. vmem_altmap_free(altmap, nr_pages);
  604. return;
  605. }
  606. /* bootmem page has reserved flag */
  607. if (PageReserved(page)) {
  608. __ClearPageReserved(page);
  609. magic = (unsigned long)page->freelist;
  610. if (magic == SECTION_INFO || magic == MIX_SECTION_INFO) {
  611. while (nr_pages--)
  612. put_page_bootmem(page++);
  613. } else
  614. while (nr_pages--)
  615. free_reserved_page(page++);
  616. } else
  617. free_pages((unsigned long)page_address(page), order);
  618. }
  619. static void __meminit free_pte_table(pte_t *pte_start, pmd_t *pmd)
  620. {
  621. pte_t *pte;
  622. int i;
  623. for (i = 0; i < PTRS_PER_PTE; i++) {
  624. pte = pte_start + i;
  625. if (!pte_none(*pte))
  626. return;
  627. }
  628. /* free a pte talbe */
  629. free_pagetable(pmd_page(*pmd), 0);
  630. spin_lock(&init_mm.page_table_lock);
  631. pmd_clear(pmd);
  632. spin_unlock(&init_mm.page_table_lock);
  633. }
  634. static void __meminit free_pmd_table(pmd_t *pmd_start, pud_t *pud)
  635. {
  636. pmd_t *pmd;
  637. int i;
  638. for (i = 0; i < PTRS_PER_PMD; i++) {
  639. pmd = pmd_start + i;
  640. if (!pmd_none(*pmd))
  641. return;
  642. }
  643. /* free a pmd talbe */
  644. free_pagetable(pud_page(*pud), 0);
  645. spin_lock(&init_mm.page_table_lock);
  646. pud_clear(pud);
  647. spin_unlock(&init_mm.page_table_lock);
  648. }
  649. static void __meminit
  650. remove_pte_table(pte_t *pte_start, unsigned long addr, unsigned long end,
  651. bool direct)
  652. {
  653. unsigned long next, pages = 0;
  654. pte_t *pte;
  655. void *page_addr;
  656. phys_addr_t phys_addr;
  657. pte = pte_start + pte_index(addr);
  658. for (; addr < end; addr = next, pte++) {
  659. next = (addr + PAGE_SIZE) & PAGE_MASK;
  660. if (next > end)
  661. next = end;
  662. if (!pte_present(*pte))
  663. continue;
  664. /*
  665. * We mapped [0,1G) memory as identity mapping when
  666. * initializing, in arch/x86/kernel/head_64.S. These
  667. * pagetables cannot be removed.
  668. */
  669. phys_addr = pte_val(*pte) + (addr & PAGE_MASK);
  670. if (phys_addr < (phys_addr_t)0x40000000)
  671. return;
  672. if (PAGE_ALIGNED(addr) && PAGE_ALIGNED(next)) {
  673. /*
  674. * Do not free direct mapping pages since they were
  675. * freed when offlining, or simplely not in use.
  676. */
  677. if (!direct)
  678. free_pagetable(pte_page(*pte), 0);
  679. spin_lock(&init_mm.page_table_lock);
  680. pte_clear(&init_mm, addr, pte);
  681. spin_unlock(&init_mm.page_table_lock);
  682. /* For non-direct mapping, pages means nothing. */
  683. pages++;
  684. } else {
  685. /*
  686. * If we are here, we are freeing vmemmap pages since
  687. * direct mapped memory ranges to be freed are aligned.
  688. *
  689. * If we are not removing the whole page, it means
  690. * other page structs in this page are being used and
  691. * we canot remove them. So fill the unused page_structs
  692. * with 0xFD, and remove the page when it is wholly
  693. * filled with 0xFD.
  694. */
  695. memset((void *)addr, PAGE_INUSE, next - addr);
  696. page_addr = page_address(pte_page(*pte));
  697. if (!memchr_inv(page_addr, PAGE_INUSE, PAGE_SIZE)) {
  698. free_pagetable(pte_page(*pte), 0);
  699. spin_lock(&init_mm.page_table_lock);
  700. pte_clear(&init_mm, addr, pte);
  701. spin_unlock(&init_mm.page_table_lock);
  702. }
  703. }
  704. }
  705. /* Call free_pte_table() in remove_pmd_table(). */
  706. flush_tlb_all();
  707. if (direct)
  708. update_page_count(PG_LEVEL_4K, -pages);
  709. }
  710. static void __meminit
  711. remove_pmd_table(pmd_t *pmd_start, unsigned long addr, unsigned long end,
  712. bool direct)
  713. {
  714. unsigned long next, pages = 0;
  715. pte_t *pte_base;
  716. pmd_t *pmd;
  717. void *page_addr;
  718. pmd = pmd_start + pmd_index(addr);
  719. for (; addr < end; addr = next, pmd++) {
  720. next = pmd_addr_end(addr, end);
  721. if (!pmd_present(*pmd))
  722. continue;
  723. if (pmd_large(*pmd)) {
  724. if (IS_ALIGNED(addr, PMD_SIZE) &&
  725. IS_ALIGNED(next, PMD_SIZE)) {
  726. if (!direct)
  727. free_pagetable(pmd_page(*pmd),
  728. get_order(PMD_SIZE));
  729. spin_lock(&init_mm.page_table_lock);
  730. pmd_clear(pmd);
  731. spin_unlock(&init_mm.page_table_lock);
  732. pages++;
  733. } else {
  734. /* If here, we are freeing vmemmap pages. */
  735. memset((void *)addr, PAGE_INUSE, next - addr);
  736. page_addr = page_address(pmd_page(*pmd));
  737. if (!memchr_inv(page_addr, PAGE_INUSE,
  738. PMD_SIZE)) {
  739. free_pagetable(pmd_page(*pmd),
  740. get_order(PMD_SIZE));
  741. spin_lock(&init_mm.page_table_lock);
  742. pmd_clear(pmd);
  743. spin_unlock(&init_mm.page_table_lock);
  744. }
  745. }
  746. continue;
  747. }
  748. pte_base = (pte_t *)pmd_page_vaddr(*pmd);
  749. remove_pte_table(pte_base, addr, next, direct);
  750. free_pte_table(pte_base, pmd);
  751. }
  752. /* Call free_pmd_table() in remove_pud_table(). */
  753. if (direct)
  754. update_page_count(PG_LEVEL_2M, -pages);
  755. }
  756. static void __meminit
  757. remove_pud_table(pud_t *pud_start, unsigned long addr, unsigned long end,
  758. bool direct)
  759. {
  760. unsigned long next, pages = 0;
  761. pmd_t *pmd_base;
  762. pud_t *pud;
  763. void *page_addr;
  764. pud = pud_start + pud_index(addr);
  765. for (; addr < end; addr = next, pud++) {
  766. next = pud_addr_end(addr, end);
  767. if (!pud_present(*pud))
  768. continue;
  769. if (pud_large(*pud)) {
  770. if (IS_ALIGNED(addr, PUD_SIZE) &&
  771. IS_ALIGNED(next, PUD_SIZE)) {
  772. if (!direct)
  773. free_pagetable(pud_page(*pud),
  774. get_order(PUD_SIZE));
  775. spin_lock(&init_mm.page_table_lock);
  776. pud_clear(pud);
  777. spin_unlock(&init_mm.page_table_lock);
  778. pages++;
  779. } else {
  780. /* If here, we are freeing vmemmap pages. */
  781. memset((void *)addr, PAGE_INUSE, next - addr);
  782. page_addr = page_address(pud_page(*pud));
  783. if (!memchr_inv(page_addr, PAGE_INUSE,
  784. PUD_SIZE)) {
  785. free_pagetable(pud_page(*pud),
  786. get_order(PUD_SIZE));
  787. spin_lock(&init_mm.page_table_lock);
  788. pud_clear(pud);
  789. spin_unlock(&init_mm.page_table_lock);
  790. }
  791. }
  792. continue;
  793. }
  794. pmd_base = (pmd_t *)pud_page_vaddr(*pud);
  795. remove_pmd_table(pmd_base, addr, next, direct);
  796. free_pmd_table(pmd_base, pud);
  797. }
  798. if (direct)
  799. update_page_count(PG_LEVEL_1G, -pages);
  800. }
  801. /* start and end are both virtual address. */
  802. static void __meminit
  803. remove_pagetable(unsigned long start, unsigned long end, bool direct)
  804. {
  805. unsigned long next;
  806. unsigned long addr;
  807. pgd_t *pgd;
  808. pud_t *pud;
  809. for (addr = start; addr < end; addr = next) {
  810. next = pgd_addr_end(addr, end);
  811. pgd = pgd_offset_k(addr);
  812. if (!pgd_present(*pgd))
  813. continue;
  814. pud = (pud_t *)pgd_page_vaddr(*pgd);
  815. remove_pud_table(pud, addr, next, direct);
  816. }
  817. flush_tlb_all();
  818. }
  819. void __ref vmemmap_free(unsigned long start, unsigned long end)
  820. {
  821. remove_pagetable(start, end, false);
  822. }
  823. #ifdef CONFIG_MEMORY_HOTREMOVE
  824. static void __meminit
  825. kernel_physical_mapping_remove(unsigned long start, unsigned long end)
  826. {
  827. start = (unsigned long)__va(start);
  828. end = (unsigned long)__va(end);
  829. remove_pagetable(start, end, true);
  830. }
  831. int __ref arch_remove_memory(u64 start, u64 size)
  832. {
  833. unsigned long start_pfn = start >> PAGE_SHIFT;
  834. unsigned long nr_pages = size >> PAGE_SHIFT;
  835. struct page *page = pfn_to_page(start_pfn);
  836. struct vmem_altmap *altmap;
  837. struct zone *zone;
  838. int ret;
  839. /* With altmap the first mapped page is offset from @start */
  840. altmap = to_vmem_altmap((unsigned long) page);
  841. if (altmap)
  842. page += vmem_altmap_offset(altmap);
  843. zone = page_zone(page);
  844. ret = __remove_pages(zone, start_pfn, nr_pages);
  845. WARN_ON_ONCE(ret);
  846. kernel_physical_mapping_remove(start, start + size);
  847. return ret;
  848. }
  849. #endif
  850. #endif /* CONFIG_MEMORY_HOTPLUG */
  851. static struct kcore_list kcore_vsyscall;
  852. static void __init register_page_bootmem_info(void)
  853. {
  854. #ifdef CONFIG_NUMA
  855. int i;
  856. for_each_online_node(i)
  857. register_page_bootmem_info_node(NODE_DATA(i));
  858. #endif
  859. }
  860. void __init mem_init(void)
  861. {
  862. pci_iommu_alloc();
  863. /* clear_bss() already clear the empty_zero_page */
  864. register_page_bootmem_info();
  865. /* this will put all memory onto the freelists */
  866. free_all_bootmem();
  867. after_bootmem = 1;
  868. /* Register memory areas for /proc/kcore */
  869. kclist_add(&kcore_vsyscall, (void *)VSYSCALL_ADDR, PAGE_SIZE, KCORE_USER);
  870. mem_init_print_info(NULL);
  871. }
  872. const int rodata_test_data = 0xC3;
  873. EXPORT_SYMBOL_GPL(rodata_test_data);
  874. int kernel_set_to_readonly;
  875. void set_kernel_text_rw(void)
  876. {
  877. unsigned long start = PFN_ALIGN(_text);
  878. unsigned long end = PFN_ALIGN(__stop___ex_table);
  879. if (!kernel_set_to_readonly)
  880. return;
  881. pr_debug("Set kernel text: %lx - %lx for read write\n",
  882. start, end);
  883. /*
  884. * Make the kernel identity mapping for text RW. Kernel text
  885. * mapping will always be RO. Refer to the comment in
  886. * static_protections() in pageattr.c
  887. */
  888. set_memory_rw(start, (end - start) >> PAGE_SHIFT);
  889. }
  890. void set_kernel_text_ro(void)
  891. {
  892. unsigned long start = PFN_ALIGN(_text);
  893. unsigned long end = PFN_ALIGN(__stop___ex_table);
  894. if (!kernel_set_to_readonly)
  895. return;
  896. pr_debug("Set kernel text: %lx - %lx for read only\n",
  897. start, end);
  898. /*
  899. * Set the kernel identity mapping for text RO.
  900. */
  901. set_memory_ro(start, (end - start) >> PAGE_SHIFT);
  902. }
  903. void mark_rodata_ro(void)
  904. {
  905. unsigned long start = PFN_ALIGN(_text);
  906. unsigned long rodata_start = PFN_ALIGN(__start_rodata);
  907. unsigned long end = (unsigned long) &__end_rodata_hpage_align;
  908. unsigned long text_end = PFN_ALIGN(&__stop___ex_table);
  909. unsigned long rodata_end = PFN_ALIGN(&__end_rodata);
  910. unsigned long all_end;
  911. printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
  912. (end - start) >> 10);
  913. set_memory_ro(start, (end - start) >> PAGE_SHIFT);
  914. kernel_set_to_readonly = 1;
  915. /*
  916. * The rodata/data/bss/brk section (but not the kernel text!)
  917. * should also be not-executable.
  918. *
  919. * We align all_end to PMD_SIZE because the existing mapping
  920. * is a full PMD. If we would align _brk_end to PAGE_SIZE we
  921. * split the PMD and the reminder between _brk_end and the end
  922. * of the PMD will remain mapped executable.
  923. *
  924. * Any PMD which was setup after the one which covers _brk_end
  925. * has been zapped already via cleanup_highmem().
  926. */
  927. all_end = roundup((unsigned long)_brk_end, PMD_SIZE);
  928. set_memory_nx(text_end, (all_end - text_end) >> PAGE_SHIFT);
  929. rodata_test();
  930. #ifdef CONFIG_CPA_DEBUG
  931. printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, end);
  932. set_memory_rw(start, (end-start) >> PAGE_SHIFT);
  933. printk(KERN_INFO "Testing CPA: again\n");
  934. set_memory_ro(start, (end-start) >> PAGE_SHIFT);
  935. #endif
  936. free_init_pages("unused kernel",
  937. (unsigned long) __va(__pa_symbol(text_end)),
  938. (unsigned long) __va(__pa_symbol(rodata_start)));
  939. free_init_pages("unused kernel",
  940. (unsigned long) __va(__pa_symbol(rodata_end)),
  941. (unsigned long) __va(__pa_symbol(_sdata)));
  942. debug_checkwx();
  943. }
  944. int kern_addr_valid(unsigned long addr)
  945. {
  946. unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT;
  947. pgd_t *pgd;
  948. pud_t *pud;
  949. pmd_t *pmd;
  950. pte_t *pte;
  951. if (above != 0 && above != -1UL)
  952. return 0;
  953. pgd = pgd_offset_k(addr);
  954. if (pgd_none(*pgd))
  955. return 0;
  956. pud = pud_offset(pgd, addr);
  957. if (pud_none(*pud))
  958. return 0;
  959. if (pud_large(*pud))
  960. return pfn_valid(pud_pfn(*pud));
  961. pmd = pmd_offset(pud, addr);
  962. if (pmd_none(*pmd))
  963. return 0;
  964. if (pmd_large(*pmd))
  965. return pfn_valid(pmd_pfn(*pmd));
  966. pte = pte_offset_kernel(pmd, addr);
  967. if (pte_none(*pte))
  968. return 0;
  969. return pfn_valid(pte_pfn(*pte));
  970. }
  971. static unsigned long probe_memory_block_size(void)
  972. {
  973. unsigned long bz = MIN_MEMORY_BLOCK_SIZE;
  974. /* if system is UV or has 64GB of RAM or more, use large blocks */
  975. if (is_uv_system() || ((max_pfn << PAGE_SHIFT) >= (64UL << 30)))
  976. bz = 2UL << 30; /* 2GB */
  977. pr_info("x86/mm: Memory block size: %ldMB\n", bz >> 20);
  978. return bz;
  979. }
  980. static unsigned long memory_block_size_probed;
  981. unsigned long memory_block_size_bytes(void)
  982. {
  983. if (!memory_block_size_probed)
  984. memory_block_size_probed = probe_memory_block_size();
  985. return memory_block_size_probed;
  986. }
  987. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  988. /*
  989. * Initialise the sparsemem vmemmap using huge-pages at the PMD level.
  990. */
  991. static long __meminitdata addr_start, addr_end;
  992. static void __meminitdata *p_start, *p_end;
  993. static int __meminitdata node_start;
  994. static int __meminit vmemmap_populate_hugepages(unsigned long start,
  995. unsigned long end, int node, struct vmem_altmap *altmap)
  996. {
  997. unsigned long addr;
  998. unsigned long next;
  999. pgd_t *pgd;
  1000. pud_t *pud;
  1001. pmd_t *pmd;
  1002. for (addr = start; addr < end; addr = next) {
  1003. next = pmd_addr_end(addr, end);
  1004. pgd = vmemmap_pgd_populate(addr, node);
  1005. if (!pgd)
  1006. return -ENOMEM;
  1007. pud = vmemmap_pud_populate(pgd, addr, node);
  1008. if (!pud)
  1009. return -ENOMEM;
  1010. pmd = pmd_offset(pud, addr);
  1011. if (pmd_none(*pmd)) {
  1012. void *p;
  1013. p = __vmemmap_alloc_block_buf(PMD_SIZE, node, altmap);
  1014. if (p) {
  1015. pte_t entry;
  1016. entry = pfn_pte(__pa(p) >> PAGE_SHIFT,
  1017. PAGE_KERNEL_LARGE);
  1018. set_pmd(pmd, __pmd(pte_val(entry)));
  1019. /* check to see if we have contiguous blocks */
  1020. if (p_end != p || node_start != node) {
  1021. if (p_start)
  1022. pr_debug(" [%lx-%lx] PMD -> [%p-%p] on node %d\n",
  1023. addr_start, addr_end-1, p_start, p_end-1, node_start);
  1024. addr_start = addr;
  1025. node_start = node;
  1026. p_start = p;
  1027. }
  1028. addr_end = addr + PMD_SIZE;
  1029. p_end = p + PMD_SIZE;
  1030. continue;
  1031. } else if (altmap)
  1032. return -ENOMEM; /* no fallback */
  1033. } else if (pmd_large(*pmd)) {
  1034. vmemmap_verify((pte_t *)pmd, node, addr, next);
  1035. continue;
  1036. }
  1037. pr_warn_once("vmemmap: falling back to regular page backing\n");
  1038. if (vmemmap_populate_basepages(addr, next, node))
  1039. return -ENOMEM;
  1040. }
  1041. return 0;
  1042. }
  1043. int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node)
  1044. {
  1045. struct vmem_altmap *altmap = to_vmem_altmap(start);
  1046. int err;
  1047. if (boot_cpu_has(X86_FEATURE_PSE))
  1048. err = vmemmap_populate_hugepages(start, end, node, altmap);
  1049. else if (altmap) {
  1050. pr_err_once("%s: no cpu support for altmap allocations\n",
  1051. __func__);
  1052. err = -ENOMEM;
  1053. } else
  1054. err = vmemmap_populate_basepages(start, end, node);
  1055. if (!err)
  1056. sync_global_pgds(start, end - 1, 0);
  1057. return err;
  1058. }
  1059. #if defined(CONFIG_MEMORY_HOTPLUG_SPARSE) && defined(CONFIG_HAVE_BOOTMEM_INFO_NODE)
  1060. void register_page_bootmem_memmap(unsigned long section_nr,
  1061. struct page *start_page, unsigned long size)
  1062. {
  1063. unsigned long addr = (unsigned long)start_page;
  1064. unsigned long end = (unsigned long)(start_page + size);
  1065. unsigned long next;
  1066. pgd_t *pgd;
  1067. pud_t *pud;
  1068. pmd_t *pmd;
  1069. unsigned int nr_pages;
  1070. struct page *page;
  1071. for (; addr < end; addr = next) {
  1072. pte_t *pte = NULL;
  1073. pgd = pgd_offset_k(addr);
  1074. if (pgd_none(*pgd)) {
  1075. next = (addr + PAGE_SIZE) & PAGE_MASK;
  1076. continue;
  1077. }
  1078. get_page_bootmem(section_nr, pgd_page(*pgd), MIX_SECTION_INFO);
  1079. pud = pud_offset(pgd, addr);
  1080. if (pud_none(*pud)) {
  1081. next = (addr + PAGE_SIZE) & PAGE_MASK;
  1082. continue;
  1083. }
  1084. get_page_bootmem(section_nr, pud_page(*pud), MIX_SECTION_INFO);
  1085. if (!boot_cpu_has(X86_FEATURE_PSE)) {
  1086. next = (addr + PAGE_SIZE) & PAGE_MASK;
  1087. pmd = pmd_offset(pud, addr);
  1088. if (pmd_none(*pmd))
  1089. continue;
  1090. get_page_bootmem(section_nr, pmd_page(*pmd),
  1091. MIX_SECTION_INFO);
  1092. pte = pte_offset_kernel(pmd, addr);
  1093. if (pte_none(*pte))
  1094. continue;
  1095. get_page_bootmem(section_nr, pte_page(*pte),
  1096. SECTION_INFO);
  1097. } else {
  1098. next = pmd_addr_end(addr, end);
  1099. pmd = pmd_offset(pud, addr);
  1100. if (pmd_none(*pmd))
  1101. continue;
  1102. nr_pages = 1 << (get_order(PMD_SIZE));
  1103. page = pmd_page(*pmd);
  1104. while (nr_pages--)
  1105. get_page_bootmem(section_nr, page++,
  1106. SECTION_INFO);
  1107. }
  1108. }
  1109. }
  1110. #endif
  1111. void __meminit vmemmap_populate_print_last(void)
  1112. {
  1113. if (p_start) {
  1114. pr_debug(" [%lx-%lx] PMD -> [%p-%p] on node %d\n",
  1115. addr_start, addr_end-1, p_start, p_end-1, node_start);
  1116. p_start = NULL;
  1117. p_end = NULL;
  1118. node_start = 0;
  1119. }
  1120. }
  1121. #endif