init_64.c 25 KB

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