init_64.c 22 KB

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