init_64.c 23 KB

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