init_64.c 24 KB

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