init_64.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  1. /*
  2. * linux/arch/x86_64/mm/init.c
  3. *
  4. * Copyright (C) 1995 Linus Torvalds
  5. * Copyright (C) 2000 Pavel Machek <pavel@suse.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/proc_fs.h>
  24. #include <linux/pci.h>
  25. #include <linux/pfn.h>
  26. #include <linux/poison.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/module.h>
  29. #include <linux/memory_hotplug.h>
  30. #include <linux/nmi.h>
  31. #include <asm/processor.h>
  32. #include <asm/system.h>
  33. #include <asm/uaccess.h>
  34. #include <asm/pgtable.h>
  35. #include <asm/pgalloc.h>
  36. #include <asm/dma.h>
  37. #include <asm/fixmap.h>
  38. #include <asm/e820.h>
  39. #include <asm/apic.h>
  40. #include <asm/tlb.h>
  41. #include <asm/mmu_context.h>
  42. #include <asm/proto.h>
  43. #include <asm/smp.h>
  44. #include <asm/sections.h>
  45. #include <asm/kdebug.h>
  46. #include <asm/numa.h>
  47. #include <asm/cacheflush.h>
  48. /*
  49. * end_pfn only includes RAM, while max_pfn_mapped includes all e820 entries.
  50. * The direct mapping extends to max_pfn_mapped, so that we can directly access
  51. * apertures, ACPI and other tables without having to play with fixmaps.
  52. */
  53. unsigned long max_low_pfn_mapped;
  54. unsigned long max_pfn_mapped;
  55. static unsigned long dma_reserve __initdata;
  56. DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
  57. int direct_gbpages __meminitdata
  58. #ifdef CONFIG_DIRECT_GBPAGES
  59. = 1
  60. #endif
  61. ;
  62. static int __init parse_direct_gbpages_off(char *arg)
  63. {
  64. direct_gbpages = 0;
  65. return 0;
  66. }
  67. early_param("nogbpages", parse_direct_gbpages_off);
  68. static int __init parse_direct_gbpages_on(char *arg)
  69. {
  70. direct_gbpages = 1;
  71. return 0;
  72. }
  73. early_param("gbpages", parse_direct_gbpages_on);
  74. /*
  75. * NOTE: pagetable_init alloc all the fixmap pagetables contiguous on the
  76. * physical space so we can cache the place of the first one and move
  77. * around without checking the pgd every time.
  78. */
  79. void show_mem(void)
  80. {
  81. long i, total = 0, reserved = 0;
  82. long shared = 0, cached = 0;
  83. struct page *page;
  84. pg_data_t *pgdat;
  85. printk(KERN_INFO "Mem-info:\n");
  86. show_free_areas();
  87. for_each_online_pgdat(pgdat) {
  88. for (i = 0; i < pgdat->node_spanned_pages; ++i) {
  89. /*
  90. * This loop can take a while with 256 GB and
  91. * 4k pages so defer the NMI watchdog:
  92. */
  93. if (unlikely(i % MAX_ORDER_NR_PAGES == 0))
  94. touch_nmi_watchdog();
  95. if (!pfn_valid(pgdat->node_start_pfn + i))
  96. continue;
  97. page = pfn_to_page(pgdat->node_start_pfn + i);
  98. total++;
  99. if (PageReserved(page))
  100. reserved++;
  101. else if (PageSwapCache(page))
  102. cached++;
  103. else if (page_count(page))
  104. shared += page_count(page) - 1;
  105. }
  106. }
  107. printk(KERN_INFO "%lu pages of RAM\n", total);
  108. printk(KERN_INFO "%lu reserved pages\n", reserved);
  109. printk(KERN_INFO "%lu pages shared\n", shared);
  110. printk(KERN_INFO "%lu pages swap cached\n", cached);
  111. }
  112. int after_bootmem;
  113. static __init void *spp_getpage(void)
  114. {
  115. void *ptr;
  116. if (after_bootmem)
  117. ptr = (void *) get_zeroed_page(GFP_ATOMIC);
  118. else
  119. ptr = alloc_bootmem_pages(PAGE_SIZE);
  120. if (!ptr || ((unsigned long)ptr & ~PAGE_MASK)) {
  121. panic("set_pte_phys: cannot allocate page data %s\n",
  122. after_bootmem ? "after bootmem" : "");
  123. }
  124. pr_debug("spp_getpage %p\n", ptr);
  125. return ptr;
  126. }
  127. void
  128. set_pte_vaddr_pud(pud_t *pud_page, unsigned long vaddr, pte_t new_pte)
  129. {
  130. pud_t *pud;
  131. pmd_t *pmd;
  132. pte_t *pte;
  133. pud = pud_page + pud_index(vaddr);
  134. if (pud_none(*pud)) {
  135. pmd = (pmd_t *) spp_getpage();
  136. pud_populate(&init_mm, pud, pmd);
  137. if (pmd != pmd_offset(pud, 0)) {
  138. printk(KERN_ERR "PAGETABLE BUG #01! %p <-> %p\n",
  139. pmd, pmd_offset(pud, 0));
  140. return;
  141. }
  142. }
  143. pmd = pmd_offset(pud, vaddr);
  144. if (pmd_none(*pmd)) {
  145. pte = (pte_t *) spp_getpage();
  146. pmd_populate_kernel(&init_mm, pmd, pte);
  147. if (pte != pte_offset_kernel(pmd, 0)) {
  148. printk(KERN_ERR "PAGETABLE BUG #02!\n");
  149. return;
  150. }
  151. }
  152. pte = pte_offset_kernel(pmd, vaddr);
  153. if (!pte_none(*pte) && pte_val(new_pte) &&
  154. pte_val(*pte) != (pte_val(new_pte) & __supported_pte_mask))
  155. pte_ERROR(*pte);
  156. set_pte(pte, new_pte);
  157. /*
  158. * It's enough to flush this one mapping.
  159. * (PGE mappings get flushed as well)
  160. */
  161. __flush_tlb_one(vaddr);
  162. }
  163. void
  164. set_pte_vaddr(unsigned long vaddr, pte_t pteval)
  165. {
  166. pgd_t *pgd;
  167. pud_t *pud_page;
  168. pr_debug("set_pte_vaddr %lx to %lx\n", vaddr, native_pte_val(pteval));
  169. pgd = pgd_offset_k(vaddr);
  170. if (pgd_none(*pgd)) {
  171. printk(KERN_ERR
  172. "PGD FIXMAP MISSING, it should be setup in head.S!\n");
  173. return;
  174. }
  175. pud_page = (pud_t*)pgd_page_vaddr(*pgd);
  176. set_pte_vaddr_pud(pud_page, vaddr, pteval);
  177. }
  178. /*
  179. * Create large page table mappings for a range of physical addresses.
  180. */
  181. static void __init __init_extra_mapping(unsigned long phys, unsigned long size,
  182. pgprot_t prot)
  183. {
  184. pgd_t *pgd;
  185. pud_t *pud;
  186. pmd_t *pmd;
  187. BUG_ON((phys & ~PMD_MASK) || (size & ~PMD_MASK));
  188. for (; size; phys += PMD_SIZE, size -= PMD_SIZE) {
  189. pgd = pgd_offset_k((unsigned long)__va(phys));
  190. if (pgd_none(*pgd)) {
  191. pud = (pud_t *) spp_getpage();
  192. set_pgd(pgd, __pgd(__pa(pud) | _KERNPG_TABLE |
  193. _PAGE_USER));
  194. }
  195. pud = pud_offset(pgd, (unsigned long)__va(phys));
  196. if (pud_none(*pud)) {
  197. pmd = (pmd_t *) spp_getpage();
  198. set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE |
  199. _PAGE_USER));
  200. }
  201. pmd = pmd_offset(pud, phys);
  202. BUG_ON(!pmd_none(*pmd));
  203. set_pmd(pmd, __pmd(phys | pgprot_val(prot)));
  204. }
  205. }
  206. void __init init_extra_mapping_wb(unsigned long phys, unsigned long size)
  207. {
  208. __init_extra_mapping(phys, size, PAGE_KERNEL_LARGE);
  209. }
  210. void __init init_extra_mapping_uc(unsigned long phys, unsigned long size)
  211. {
  212. __init_extra_mapping(phys, size, PAGE_KERNEL_LARGE_NOCACHE);
  213. }
  214. /*
  215. * The head.S code sets up the kernel high mapping:
  216. *
  217. * from __START_KERNEL_map to __START_KERNEL_map + size (== _end-_text)
  218. *
  219. * phys_addr holds the negative offset to the kernel, which is added
  220. * to the compile time generated pmds. This results in invalid pmds up
  221. * to the point where we hit the physaddr 0 mapping.
  222. *
  223. * We limit the mappings to the region from _text to _end. _end is
  224. * rounded up to the 2MB boundary. This catches the invalid pmds as
  225. * well, as they are located before _text:
  226. */
  227. void __init cleanup_highmap(void)
  228. {
  229. unsigned long vaddr = __START_KERNEL_map;
  230. unsigned long end = round_up((unsigned long)_end, PMD_SIZE) - 1;
  231. pmd_t *pmd = level2_kernel_pgt;
  232. pmd_t *last_pmd = pmd + PTRS_PER_PMD;
  233. for (; pmd < last_pmd; pmd++, vaddr += PMD_SIZE) {
  234. if (pmd_none(*pmd))
  235. continue;
  236. if (vaddr < (unsigned long) _text || vaddr > end)
  237. set_pmd(pmd, __pmd(0));
  238. }
  239. }
  240. static unsigned long __initdata table_start;
  241. static unsigned long __meminitdata table_end;
  242. static unsigned long __meminitdata table_top;
  243. static __meminit void *alloc_low_page(unsigned long *phys)
  244. {
  245. unsigned long pfn = table_end++;
  246. void *adr;
  247. if (after_bootmem) {
  248. adr = (void *)get_zeroed_page(GFP_ATOMIC);
  249. *phys = __pa(adr);
  250. return adr;
  251. }
  252. if (pfn >= table_top)
  253. panic("alloc_low_page: ran out of memory");
  254. adr = early_ioremap(pfn * PAGE_SIZE, PAGE_SIZE);
  255. memset(adr, 0, PAGE_SIZE);
  256. *phys = pfn * PAGE_SIZE;
  257. return adr;
  258. }
  259. static __meminit void unmap_low_page(void *adr)
  260. {
  261. if (after_bootmem)
  262. return;
  263. early_iounmap(adr, PAGE_SIZE);
  264. }
  265. static unsigned long __meminit
  266. phys_pte_init(pte_t *pte_page, unsigned long addr, unsigned long end)
  267. {
  268. unsigned pages = 0;
  269. unsigned long last_map_addr = end;
  270. int i;
  271. pte_t *pte = pte_page + pte_index(addr);
  272. for(i = pte_index(addr); i < PTRS_PER_PTE; i++, addr += PAGE_SIZE, pte++) {
  273. if (addr >= end) {
  274. if (!after_bootmem) {
  275. for(; i < PTRS_PER_PTE; i++, pte++)
  276. set_pte(pte, __pte(0));
  277. }
  278. break;
  279. }
  280. if (pte_val(*pte))
  281. continue;
  282. if (0)
  283. printk(" pte=%p addr=%lx pte=%016lx\n",
  284. pte, addr, pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL).pte);
  285. set_pte(pte, pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL));
  286. last_map_addr = (addr & PAGE_MASK) + PAGE_SIZE;
  287. pages++;
  288. }
  289. update_page_count(PG_LEVEL_4K, pages);
  290. return last_map_addr;
  291. }
  292. static unsigned long __meminit
  293. phys_pte_update(pmd_t *pmd, unsigned long address, unsigned long end)
  294. {
  295. pte_t *pte = (pte_t *)pmd_page_vaddr(*pmd);
  296. return phys_pte_init(pte, address, end);
  297. }
  298. static unsigned long __meminit
  299. phys_pmd_init(pmd_t *pmd_page, unsigned long address, unsigned long end,
  300. unsigned long page_size_mask)
  301. {
  302. unsigned long pages = 0;
  303. unsigned long last_map_addr = end;
  304. int i = pmd_index(address);
  305. for (; i < PTRS_PER_PMD; i++, address += PMD_SIZE) {
  306. unsigned long pte_phys;
  307. pmd_t *pmd = pmd_page + pmd_index(address);
  308. pte_t *pte;
  309. if (address >= end) {
  310. if (!after_bootmem) {
  311. for (; i < PTRS_PER_PMD; i++, pmd++)
  312. set_pmd(pmd, __pmd(0));
  313. }
  314. break;
  315. }
  316. if (pmd_val(*pmd)) {
  317. if (!pmd_large(*pmd))
  318. last_map_addr = phys_pte_update(pmd, address,
  319. end);
  320. continue;
  321. }
  322. if (page_size_mask & (1<<PG_LEVEL_2M)) {
  323. pages++;
  324. set_pte((pte_t *)pmd,
  325. pfn_pte(address >> PAGE_SHIFT, PAGE_KERNEL_LARGE));
  326. last_map_addr = (address & PMD_MASK) + PMD_SIZE;
  327. continue;
  328. }
  329. pte = alloc_low_page(&pte_phys);
  330. last_map_addr = phys_pte_init(pte, address, end);
  331. unmap_low_page(pte);
  332. pmd_populate_kernel(&init_mm, pmd, __va(pte_phys));
  333. }
  334. update_page_count(PG_LEVEL_2M, pages);
  335. return last_map_addr;
  336. }
  337. static unsigned long __meminit
  338. phys_pmd_update(pud_t *pud, unsigned long address, unsigned long end,
  339. unsigned long page_size_mask)
  340. {
  341. pmd_t *pmd = pmd_offset(pud, 0);
  342. unsigned long last_map_addr;
  343. spin_lock(&init_mm.page_table_lock);
  344. last_map_addr = phys_pmd_init(pmd, address, end, page_size_mask);
  345. spin_unlock(&init_mm.page_table_lock);
  346. __flush_tlb_all();
  347. return last_map_addr;
  348. }
  349. static unsigned long __meminit
  350. phys_pud_init(pud_t *pud_page, unsigned long addr, unsigned long end,
  351. unsigned long page_size_mask)
  352. {
  353. unsigned long pages = 0;
  354. unsigned long last_map_addr = end;
  355. int i = pud_index(addr);
  356. for (; i < PTRS_PER_PUD; i++, addr = (addr & PUD_MASK) + PUD_SIZE) {
  357. unsigned long pmd_phys;
  358. pud_t *pud = pud_page + pud_index(addr);
  359. pmd_t *pmd;
  360. if (addr >= end)
  361. break;
  362. if (!after_bootmem &&
  363. !e820_any_mapped(addr, addr+PUD_SIZE, 0)) {
  364. set_pud(pud, __pud(0));
  365. continue;
  366. }
  367. if (pud_val(*pud)) {
  368. if (!pud_large(*pud))
  369. last_map_addr = phys_pmd_update(pud, addr, end,
  370. page_size_mask);
  371. continue;
  372. }
  373. if (page_size_mask & (1<<PG_LEVEL_1G)) {
  374. pages++;
  375. set_pte((pte_t *)pud,
  376. pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL_LARGE));
  377. last_map_addr = (addr & PUD_MASK) + PUD_SIZE;
  378. continue;
  379. }
  380. pmd = alloc_low_page(&pmd_phys);
  381. spin_lock(&init_mm.page_table_lock);
  382. last_map_addr = phys_pmd_init(pmd, addr, end, page_size_mask);
  383. unmap_low_page(pmd);
  384. pud_populate(&init_mm, pud, __va(pmd_phys));
  385. spin_unlock(&init_mm.page_table_lock);
  386. }
  387. __flush_tlb_all();
  388. update_page_count(PG_LEVEL_1G, pages);
  389. return last_map_addr;
  390. }
  391. static unsigned long __meminit
  392. phys_pud_update(pgd_t *pgd, unsigned long addr, unsigned long end,
  393. unsigned long page_size_mask)
  394. {
  395. pud_t *pud;
  396. pud = (pud_t *)pgd_page_vaddr(*pgd);
  397. return phys_pud_init(pud, addr, end, page_size_mask);
  398. }
  399. static void __init find_early_table_space(unsigned long end)
  400. {
  401. unsigned long puds, pmds, ptes, tables, start;
  402. puds = (end + PUD_SIZE - 1) >> PUD_SHIFT;
  403. tables = round_up(puds * sizeof(pud_t), PAGE_SIZE);
  404. if (direct_gbpages) {
  405. unsigned long extra;
  406. extra = end - ((end>>PUD_SHIFT) << PUD_SHIFT);
  407. pmds = (extra + PMD_SIZE - 1) >> PMD_SHIFT;
  408. } else
  409. pmds = (end + PMD_SIZE - 1) >> PMD_SHIFT;
  410. tables += round_up(pmds * sizeof(pmd_t), PAGE_SIZE);
  411. if (cpu_has_pse) {
  412. unsigned long extra;
  413. extra = end - ((end>>PMD_SHIFT) << PMD_SHIFT);
  414. ptes = (extra + PAGE_SIZE - 1) >> PAGE_SHIFT;
  415. } else
  416. ptes = (end + PAGE_SIZE - 1) >> PAGE_SHIFT;
  417. tables += round_up(ptes * sizeof(pte_t), PAGE_SIZE);
  418. /*
  419. * RED-PEN putting page tables only on node 0 could
  420. * cause a hotspot and fill up ZONE_DMA. The page tables
  421. * need roughly 0.5KB per GB.
  422. */
  423. start = 0x8000;
  424. table_start = find_e820_area(start, end, tables, PAGE_SIZE);
  425. if (table_start == -1UL)
  426. panic("Cannot find space for the kernel page tables");
  427. table_start >>= PAGE_SHIFT;
  428. table_end = table_start;
  429. table_top = table_start + (tables >> PAGE_SHIFT);
  430. printk(KERN_DEBUG "kernel direct mapping tables up to %lx @ %lx-%lx\n",
  431. end, table_start << PAGE_SHIFT, table_top << PAGE_SHIFT);
  432. }
  433. static void __init init_gbpages(void)
  434. {
  435. if (direct_gbpages && cpu_has_gbpages)
  436. printk(KERN_INFO "Using GB pages for direct mapping\n");
  437. else
  438. direct_gbpages = 0;
  439. }
  440. #ifdef CONFIG_MEMTEST
  441. static void __init memtest(unsigned long start_phys, unsigned long size,
  442. unsigned pattern)
  443. {
  444. unsigned long i;
  445. unsigned long *start;
  446. unsigned long start_bad;
  447. unsigned long last_bad;
  448. unsigned long val;
  449. unsigned long start_phys_aligned;
  450. unsigned long count;
  451. unsigned long incr;
  452. switch (pattern) {
  453. case 0:
  454. val = 0UL;
  455. break;
  456. case 1:
  457. val = -1UL;
  458. break;
  459. case 2:
  460. val = 0x5555555555555555UL;
  461. break;
  462. case 3:
  463. val = 0xaaaaaaaaaaaaaaaaUL;
  464. break;
  465. default:
  466. return;
  467. }
  468. incr = sizeof(unsigned long);
  469. start_phys_aligned = ALIGN(start_phys, incr);
  470. count = (size - (start_phys_aligned - start_phys))/incr;
  471. start = __va(start_phys_aligned);
  472. start_bad = 0;
  473. last_bad = 0;
  474. for (i = 0; i < count; i++)
  475. start[i] = val;
  476. for (i = 0; i < count; i++, start++, start_phys_aligned += incr) {
  477. if (*start != val) {
  478. if (start_phys_aligned == last_bad + incr) {
  479. last_bad += incr;
  480. } else {
  481. if (start_bad) {
  482. printk(KERN_CONT "\n %016lx bad mem addr %016lx - %016lx reserved",
  483. val, start_bad, last_bad + incr);
  484. reserve_early(start_bad, last_bad - start_bad, "BAD RAM");
  485. }
  486. start_bad = last_bad = start_phys_aligned;
  487. }
  488. }
  489. }
  490. if (start_bad) {
  491. printk(KERN_CONT "\n %016lx bad mem addr %016lx - %016lx reserved",
  492. val, start_bad, last_bad + incr);
  493. reserve_early(start_bad, last_bad - start_bad, "BAD RAM");
  494. }
  495. }
  496. /* default is disabled */
  497. static int memtest_pattern __initdata;
  498. static int __init parse_memtest(char *arg)
  499. {
  500. if (arg)
  501. memtest_pattern = simple_strtoul(arg, NULL, 0);
  502. return 0;
  503. }
  504. early_param("memtest", parse_memtest);
  505. static void __init early_memtest(unsigned long start, unsigned long end)
  506. {
  507. u64 t_start, t_size;
  508. unsigned pattern;
  509. if (!memtest_pattern)
  510. return;
  511. printk(KERN_INFO "early_memtest: pattern num %d", memtest_pattern);
  512. for (pattern = 0; pattern < memtest_pattern; pattern++) {
  513. t_start = start;
  514. t_size = 0;
  515. while (t_start < end) {
  516. t_start = find_e820_area_size(t_start, &t_size, 1);
  517. /* done ? */
  518. if (t_start >= end)
  519. break;
  520. if (t_start + t_size > end)
  521. t_size = end - t_start;
  522. printk(KERN_CONT "\n %016llx - %016llx pattern %d",
  523. (unsigned long long)t_start,
  524. (unsigned long long)t_start + t_size, pattern);
  525. memtest(t_start, t_size, pattern);
  526. t_start += t_size;
  527. }
  528. }
  529. printk(KERN_CONT "\n");
  530. }
  531. #else
  532. static void __init early_memtest(unsigned long start, unsigned long end)
  533. {
  534. }
  535. #endif
  536. static unsigned long __init kernel_physical_mapping_init(unsigned long start,
  537. unsigned long end,
  538. unsigned long page_size_mask)
  539. {
  540. unsigned long next, last_map_addr = end;
  541. start = (unsigned long)__va(start);
  542. end = (unsigned long)__va(end);
  543. for (; start < end; start = next) {
  544. pgd_t *pgd = pgd_offset_k(start);
  545. unsigned long pud_phys;
  546. pud_t *pud;
  547. next = start + PGDIR_SIZE;
  548. if (next > end)
  549. next = end;
  550. if (pgd_val(*pgd)) {
  551. last_map_addr = phys_pud_update(pgd, __pa(start),
  552. __pa(end), page_size_mask);
  553. continue;
  554. }
  555. if (after_bootmem)
  556. pud = pud_offset(pgd, start & PGDIR_MASK);
  557. else
  558. pud = alloc_low_page(&pud_phys);
  559. last_map_addr = phys_pud_init(pud, __pa(start), __pa(next),
  560. page_size_mask);
  561. unmap_low_page(pud);
  562. pgd_populate(&init_mm, pgd_offset_k(start),
  563. __va(pud_phys));
  564. }
  565. return last_map_addr;
  566. }
  567. struct map_range {
  568. unsigned long start;
  569. unsigned long end;
  570. unsigned page_size_mask;
  571. };
  572. #define NR_RANGE_MR 5
  573. static int save_mr(struct map_range *mr, int nr_range,
  574. unsigned long start_pfn, unsigned long end_pfn,
  575. unsigned long page_size_mask)
  576. {
  577. if (start_pfn < end_pfn) {
  578. if (nr_range >= NR_RANGE_MR)
  579. panic("run out of range for init_memory_mapping\n");
  580. mr[nr_range].start = start_pfn<<PAGE_SHIFT;
  581. mr[nr_range].end = end_pfn<<PAGE_SHIFT;
  582. mr[nr_range].page_size_mask = page_size_mask;
  583. nr_range++;
  584. }
  585. return nr_range;
  586. }
  587. /*
  588. * Setup the direct mapping of the physical memory at PAGE_OFFSET.
  589. * This runs before bootmem is initialized and gets pages directly from
  590. * the physical memory. To access them they are temporarily mapped.
  591. */
  592. unsigned long __init_refok init_memory_mapping(unsigned long start,
  593. unsigned long end)
  594. {
  595. unsigned long last_map_addr = 0;
  596. unsigned long page_size_mask = 0;
  597. unsigned long start_pfn, end_pfn;
  598. struct map_range mr[NR_RANGE_MR];
  599. int nr_range, i;
  600. printk(KERN_INFO "init_memory_mapping\n");
  601. /*
  602. * Find space for the kernel direct mapping tables.
  603. *
  604. * Later we should allocate these tables in the local node of the
  605. * memory mapped. Unfortunately this is done currently before the
  606. * nodes are discovered.
  607. */
  608. if (!after_bootmem)
  609. init_gbpages();
  610. if (direct_gbpages)
  611. page_size_mask |= 1 << PG_LEVEL_1G;
  612. if (cpu_has_pse)
  613. page_size_mask |= 1 << PG_LEVEL_2M;
  614. memset(mr, 0, sizeof(mr));
  615. nr_range = 0;
  616. /* head if not big page alignment ?*/
  617. start_pfn = start >> PAGE_SHIFT;
  618. end_pfn = ((start + (PMD_SIZE - 1)) >> PMD_SHIFT)
  619. << (PMD_SHIFT - PAGE_SHIFT);
  620. nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
  621. /* big page (2M) range*/
  622. start_pfn = ((start + (PMD_SIZE - 1))>>PMD_SHIFT)
  623. << (PMD_SHIFT - PAGE_SHIFT);
  624. end_pfn = ((start + (PUD_SIZE - 1))>>PUD_SHIFT)
  625. << (PUD_SHIFT - PAGE_SHIFT);
  626. if (end_pfn > ((end>>PUD_SHIFT)<<(PUD_SHIFT - PAGE_SHIFT)))
  627. end_pfn = ((end>>PUD_SHIFT)<<(PUD_SHIFT - PAGE_SHIFT));
  628. nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
  629. page_size_mask & (1<<PG_LEVEL_2M));
  630. /* big page (1G) range */
  631. start_pfn = end_pfn;
  632. end_pfn = (end>>PUD_SHIFT) << (PUD_SHIFT - PAGE_SHIFT);
  633. nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
  634. page_size_mask &
  635. ((1<<PG_LEVEL_2M)|(1<<PG_LEVEL_1G)));
  636. /* tail is not big page (1G) alignment */
  637. start_pfn = end_pfn;
  638. end_pfn = (end>>PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
  639. nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
  640. page_size_mask & (1<<PG_LEVEL_2M));
  641. /* tail is not big page (2M) alignment */
  642. start_pfn = end_pfn;
  643. end_pfn = end>>PAGE_SHIFT;
  644. nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
  645. /* try to merge same page size and continuous */
  646. for (i = 0; nr_range > 1 && i < nr_range - 1; i++) {
  647. unsigned long old_start;
  648. if (mr[i].end != mr[i+1].start ||
  649. mr[i].page_size_mask != mr[i+1].page_size_mask)
  650. continue;
  651. /* move it */
  652. old_start = mr[i].start;
  653. memmove(&mr[i], &mr[i+1],
  654. (nr_range - 1 - i) * sizeof (struct map_range));
  655. mr[i].start = old_start;
  656. nr_range--;
  657. }
  658. for (i = 0; i < nr_range; i++)
  659. printk(KERN_DEBUG " %010lx - %010lx page %s\n",
  660. mr[i].start, mr[i].end,
  661. (mr[i].page_size_mask & (1<<PG_LEVEL_1G))?"1G":(
  662. (mr[i].page_size_mask & (1<<PG_LEVEL_2M))?"2M":"4k"));
  663. if (!after_bootmem)
  664. find_early_table_space(end);
  665. for (i = 0; i < nr_range; i++)
  666. last_map_addr = kernel_physical_mapping_init(
  667. mr[i].start, mr[i].end,
  668. mr[i].page_size_mask);
  669. if (!after_bootmem)
  670. mmu_cr4_features = read_cr4();
  671. __flush_tlb_all();
  672. if (!after_bootmem && table_end > table_start)
  673. reserve_early(table_start << PAGE_SHIFT,
  674. table_end << PAGE_SHIFT, "PGTABLE");
  675. printk(KERN_INFO "last_map_addr: %lx end: %lx\n",
  676. last_map_addr, end);
  677. if (!after_bootmem)
  678. early_memtest(start, end);
  679. return last_map_addr >> PAGE_SHIFT;
  680. }
  681. #ifndef CONFIG_NUMA
  682. void __init initmem_init(unsigned long start_pfn, unsigned long end_pfn)
  683. {
  684. unsigned long bootmap_size, bootmap;
  685. bootmap_size = bootmem_bootmap_pages(end_pfn)<<PAGE_SHIFT;
  686. bootmap = find_e820_area(0, end_pfn<<PAGE_SHIFT, bootmap_size,
  687. PAGE_SIZE);
  688. if (bootmap == -1L)
  689. panic("Cannot find bootmem map of size %ld\n", bootmap_size);
  690. /* don't touch min_low_pfn */
  691. bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap >> PAGE_SHIFT,
  692. 0, end_pfn);
  693. e820_register_active_regions(0, start_pfn, end_pfn);
  694. free_bootmem_with_active_regions(0, end_pfn);
  695. early_res_to_bootmem(0, end_pfn<<PAGE_SHIFT);
  696. reserve_bootmem(bootmap, bootmap_size, BOOTMEM_DEFAULT);
  697. }
  698. void __init paging_init(void)
  699. {
  700. unsigned long max_zone_pfns[MAX_NR_ZONES];
  701. memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
  702. max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
  703. max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
  704. max_zone_pfns[ZONE_NORMAL] = max_pfn;
  705. memory_present(0, 0, max_pfn);
  706. sparse_init();
  707. free_area_init_nodes(max_zone_pfns);
  708. }
  709. #endif
  710. /*
  711. * Memory hotplug specific functions
  712. */
  713. #ifdef CONFIG_MEMORY_HOTPLUG
  714. /*
  715. * Memory is added always to NORMAL zone. This means you will never get
  716. * additional DMA/DMA32 memory.
  717. */
  718. int arch_add_memory(int nid, u64 start, u64 size)
  719. {
  720. struct pglist_data *pgdat = NODE_DATA(nid);
  721. struct zone *zone = pgdat->node_zones + ZONE_NORMAL;
  722. unsigned long last_mapped_pfn, start_pfn = start >> PAGE_SHIFT;
  723. unsigned long nr_pages = size >> PAGE_SHIFT;
  724. int ret;
  725. last_mapped_pfn = init_memory_mapping(start, start + size-1);
  726. if (last_mapped_pfn > max_pfn_mapped)
  727. max_pfn_mapped = last_mapped_pfn;
  728. ret = __add_pages(zone, start_pfn, nr_pages);
  729. WARN_ON(1);
  730. return ret;
  731. }
  732. EXPORT_SYMBOL_GPL(arch_add_memory);
  733. #if !defined(CONFIG_ACPI_NUMA) && defined(CONFIG_NUMA)
  734. int memory_add_physaddr_to_nid(u64 start)
  735. {
  736. return 0;
  737. }
  738. EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
  739. #endif
  740. #endif /* CONFIG_MEMORY_HOTPLUG */
  741. /*
  742. * devmem_is_allowed() checks to see if /dev/mem access to a certain address
  743. * is valid. The argument is a physical page number.
  744. *
  745. *
  746. * On x86, access has to be given to the first megabyte of ram because that area
  747. * contains bios code and data regions used by X and dosemu and similar apps.
  748. * Access has to be given to non-kernel-ram areas as well, these contain the PCI
  749. * mmio resources as well as potential bios/acpi data regions.
  750. */
  751. int devmem_is_allowed(unsigned long pagenr)
  752. {
  753. if (pagenr <= 256)
  754. return 1;
  755. if (!page_is_ram(pagenr))
  756. return 1;
  757. return 0;
  758. }
  759. static struct kcore_list kcore_mem, kcore_vmalloc, kcore_kernel,
  760. kcore_modules, kcore_vsyscall;
  761. void __init mem_init(void)
  762. {
  763. long codesize, reservedpages, datasize, initsize;
  764. pci_iommu_alloc();
  765. /* clear_bss() already clear the empty_zero_page */
  766. reservedpages = 0;
  767. /* this will put all low memory onto the freelists */
  768. #ifdef CONFIG_NUMA
  769. totalram_pages = numa_free_all_bootmem();
  770. #else
  771. totalram_pages = free_all_bootmem();
  772. #endif
  773. reservedpages = max_pfn - totalram_pages -
  774. absent_pages_in_range(0, max_pfn);
  775. after_bootmem = 1;
  776. codesize = (unsigned long) &_etext - (unsigned long) &_text;
  777. datasize = (unsigned long) &_edata - (unsigned long) &_etext;
  778. initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
  779. /* Register memory areas for /proc/kcore */
  780. kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
  781. kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
  782. VMALLOC_END-VMALLOC_START);
  783. kclist_add(&kcore_kernel, &_stext, _end - _stext);
  784. kclist_add(&kcore_modules, (void *)MODULES_VADDR, MODULES_LEN);
  785. kclist_add(&kcore_vsyscall, (void *)VSYSCALL_START,
  786. VSYSCALL_END - VSYSCALL_START);
  787. printk(KERN_INFO "Memory: %luk/%luk available (%ldk kernel code, "
  788. "%ldk reserved, %ldk data, %ldk init)\n",
  789. (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
  790. max_pfn << (PAGE_SHIFT-10),
  791. codesize >> 10,
  792. reservedpages << (PAGE_SHIFT-10),
  793. datasize >> 10,
  794. initsize >> 10);
  795. cpa_init();
  796. }
  797. void free_init_pages(char *what, unsigned long begin, unsigned long end)
  798. {
  799. unsigned long addr = begin;
  800. if (addr >= end)
  801. return;
  802. /*
  803. * If debugging page accesses then do not free this memory but
  804. * mark them not present - any buggy init-section access will
  805. * create a kernel page fault:
  806. */
  807. #ifdef CONFIG_DEBUG_PAGEALLOC
  808. printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n",
  809. begin, PAGE_ALIGN(end));
  810. set_memory_np(begin, (end - begin) >> PAGE_SHIFT);
  811. #else
  812. printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
  813. for (; addr < end; addr += PAGE_SIZE) {
  814. ClearPageReserved(virt_to_page(addr));
  815. init_page_count(virt_to_page(addr));
  816. memset((void *)(addr & ~(PAGE_SIZE-1)),
  817. POISON_FREE_INITMEM, PAGE_SIZE);
  818. free_page(addr);
  819. totalram_pages++;
  820. }
  821. #endif
  822. }
  823. void free_initmem(void)
  824. {
  825. free_init_pages("unused kernel memory",
  826. (unsigned long)(&__init_begin),
  827. (unsigned long)(&__init_end));
  828. }
  829. #ifdef CONFIG_DEBUG_RODATA
  830. const int rodata_test_data = 0xC3;
  831. EXPORT_SYMBOL_GPL(rodata_test_data);
  832. void mark_rodata_ro(void)
  833. {
  834. unsigned long start = PFN_ALIGN(_stext), end = PFN_ALIGN(__end_rodata);
  835. unsigned long rodata_start =
  836. ((unsigned long)__start_rodata + PAGE_SIZE - 1) & PAGE_MASK;
  837. #ifdef CONFIG_DYNAMIC_FTRACE
  838. /* Dynamic tracing modifies the kernel text section */
  839. start = rodata_start;
  840. #endif
  841. printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
  842. (end - start) >> 10);
  843. set_memory_ro(start, (end - start) >> PAGE_SHIFT);
  844. /*
  845. * The rodata section (but not the kernel text!) should also be
  846. * not-executable.
  847. */
  848. set_memory_nx(rodata_start, (end - rodata_start) >> PAGE_SHIFT);
  849. rodata_test();
  850. #ifdef CONFIG_CPA_DEBUG
  851. printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, end);
  852. set_memory_rw(start, (end-start) >> PAGE_SHIFT);
  853. printk(KERN_INFO "Testing CPA: again\n");
  854. set_memory_ro(start, (end-start) >> PAGE_SHIFT);
  855. #endif
  856. }
  857. #endif
  858. #ifdef CONFIG_BLK_DEV_INITRD
  859. void free_initrd_mem(unsigned long start, unsigned long end)
  860. {
  861. free_init_pages("initrd memory", start, end);
  862. }
  863. #endif
  864. int __init reserve_bootmem_generic(unsigned long phys, unsigned long len,
  865. int flags)
  866. {
  867. #ifdef CONFIG_NUMA
  868. int nid, next_nid;
  869. int ret;
  870. #endif
  871. unsigned long pfn = phys >> PAGE_SHIFT;
  872. if (pfn >= max_pfn) {
  873. /*
  874. * This can happen with kdump kernels when accessing
  875. * firmware tables:
  876. */
  877. if (pfn < max_pfn_mapped)
  878. return -EFAULT;
  879. printk(KERN_ERR "reserve_bootmem: illegal reserve %lx %lu\n",
  880. phys, len);
  881. return -EFAULT;
  882. }
  883. /* Should check here against the e820 map to avoid double free */
  884. #ifdef CONFIG_NUMA
  885. nid = phys_to_nid(phys);
  886. next_nid = phys_to_nid(phys + len - 1);
  887. if (nid == next_nid)
  888. ret = reserve_bootmem_node(NODE_DATA(nid), phys, len, flags);
  889. else
  890. ret = reserve_bootmem(phys, len, flags);
  891. if (ret != 0)
  892. return ret;
  893. #else
  894. reserve_bootmem(phys, len, BOOTMEM_DEFAULT);
  895. #endif
  896. if (phys+len <= MAX_DMA_PFN*PAGE_SIZE) {
  897. dma_reserve += len / PAGE_SIZE;
  898. set_dma_reserve(dma_reserve);
  899. }
  900. return 0;
  901. }
  902. int kern_addr_valid(unsigned long addr)
  903. {
  904. unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT;
  905. pgd_t *pgd;
  906. pud_t *pud;
  907. pmd_t *pmd;
  908. pte_t *pte;
  909. if (above != 0 && above != -1UL)
  910. return 0;
  911. pgd = pgd_offset_k(addr);
  912. if (pgd_none(*pgd))
  913. return 0;
  914. pud = pud_offset(pgd, addr);
  915. if (pud_none(*pud))
  916. return 0;
  917. pmd = pmd_offset(pud, addr);
  918. if (pmd_none(*pmd))
  919. return 0;
  920. if (pmd_large(*pmd))
  921. return pfn_valid(pmd_pfn(*pmd));
  922. pte = pte_offset_kernel(pmd, addr);
  923. if (pte_none(*pte))
  924. return 0;
  925. return pfn_valid(pte_pfn(*pte));
  926. }
  927. /*
  928. * A pseudo VMA to allow ptrace access for the vsyscall page. This only
  929. * covers the 64bit vsyscall page now. 32bit has a real VMA now and does
  930. * not need special handling anymore:
  931. */
  932. static struct vm_area_struct gate_vma = {
  933. .vm_start = VSYSCALL_START,
  934. .vm_end = VSYSCALL_START + (VSYSCALL_MAPPED_PAGES * PAGE_SIZE),
  935. .vm_page_prot = PAGE_READONLY_EXEC,
  936. .vm_flags = VM_READ | VM_EXEC
  937. };
  938. struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
  939. {
  940. #ifdef CONFIG_IA32_EMULATION
  941. if (test_tsk_thread_flag(tsk, TIF_IA32))
  942. return NULL;
  943. #endif
  944. return &gate_vma;
  945. }
  946. int in_gate_area(struct task_struct *task, unsigned long addr)
  947. {
  948. struct vm_area_struct *vma = get_gate_vma(task);
  949. if (!vma)
  950. return 0;
  951. return (addr >= vma->vm_start) && (addr < vma->vm_end);
  952. }
  953. /*
  954. * Use this when you have no reliable task/vma, typically from interrupt
  955. * context. It is less reliable than using the task's vma and may give
  956. * false positives:
  957. */
  958. int in_gate_area_no_task(unsigned long addr)
  959. {
  960. return (addr >= VSYSCALL_START) && (addr < VSYSCALL_END);
  961. }
  962. const char *arch_vma_name(struct vm_area_struct *vma)
  963. {
  964. if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
  965. return "[vdso]";
  966. if (vma == &gate_vma)
  967. return "[vsyscall]";
  968. return NULL;
  969. }
  970. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  971. /*
  972. * Initialise the sparsemem vmemmap using huge-pages at the PMD level.
  973. */
  974. static long __meminitdata addr_start, addr_end;
  975. static void __meminitdata *p_start, *p_end;
  976. static int __meminitdata node_start;
  977. int __meminit
  978. vmemmap_populate(struct page *start_page, unsigned long size, int node)
  979. {
  980. unsigned long addr = (unsigned long)start_page;
  981. unsigned long end = (unsigned long)(start_page + size);
  982. unsigned long next;
  983. pgd_t *pgd;
  984. pud_t *pud;
  985. pmd_t *pmd;
  986. for (; addr < end; addr = next) {
  987. void *p = NULL;
  988. pgd = vmemmap_pgd_populate(addr, node);
  989. if (!pgd)
  990. return -ENOMEM;
  991. pud = vmemmap_pud_populate(pgd, addr, node);
  992. if (!pud)
  993. return -ENOMEM;
  994. if (!cpu_has_pse) {
  995. next = (addr + PAGE_SIZE) & PAGE_MASK;
  996. pmd = vmemmap_pmd_populate(pud, addr, node);
  997. if (!pmd)
  998. return -ENOMEM;
  999. p = vmemmap_pte_populate(pmd, addr, node);
  1000. if (!p)
  1001. return -ENOMEM;
  1002. addr_end = addr + PAGE_SIZE;
  1003. p_end = p + PAGE_SIZE;
  1004. } else {
  1005. next = pmd_addr_end(addr, end);
  1006. pmd = pmd_offset(pud, addr);
  1007. if (pmd_none(*pmd)) {
  1008. pte_t entry;
  1009. p = vmemmap_alloc_block(PMD_SIZE, node);
  1010. if (!p)
  1011. return -ENOMEM;
  1012. entry = pfn_pte(__pa(p) >> PAGE_SHIFT,
  1013. PAGE_KERNEL_LARGE);
  1014. set_pmd(pmd, __pmd(pte_val(entry)));
  1015. /* check to see if we have contiguous blocks */
  1016. if (p_end != p || node_start != node) {
  1017. if (p_start)
  1018. printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n",
  1019. addr_start, addr_end-1, p_start, p_end-1, node_start);
  1020. addr_start = addr;
  1021. node_start = node;
  1022. p_start = p;
  1023. }
  1024. addr_end = addr + PMD_SIZE;
  1025. p_end = p + PMD_SIZE;
  1026. } else
  1027. vmemmap_verify((pte_t *)pmd, node, addr, next);
  1028. }
  1029. }
  1030. return 0;
  1031. }
  1032. void __meminit vmemmap_populate_print_last(void)
  1033. {
  1034. if (p_start) {
  1035. printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n",
  1036. addr_start, addr_end-1, p_start, p_end-1, node_start);
  1037. p_start = NULL;
  1038. p_end = NULL;
  1039. node_start = 0;
  1040. }
  1041. }
  1042. #endif