init_64.c 23 KB

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