init_64.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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. #ifndef Dprintk
  47. #define Dprintk(x...)
  48. #endif
  49. const struct dma_mapping_ops* dma_ops;
  50. EXPORT_SYMBOL(dma_ops);
  51. static unsigned long dma_reserve __initdata;
  52. DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
  53. /*
  54. * NOTE: pagetable_init alloc all the fixmap pagetables contiguous on the
  55. * physical space so we can cache the place of the first one and move
  56. * around without checking the pgd every time.
  57. */
  58. void show_mem(void)
  59. {
  60. long i, total = 0, reserved = 0;
  61. long shared = 0, cached = 0;
  62. pg_data_t *pgdat;
  63. struct page *page;
  64. printk(KERN_INFO "Mem-info:\n");
  65. show_free_areas();
  66. printk(KERN_INFO "Free swap: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
  67. for_each_online_pgdat(pgdat) {
  68. for (i = 0; i < pgdat->node_spanned_pages; ++i) {
  69. /* this loop can take a while with 256 GB and 4k pages
  70. so update the NMI watchdog */
  71. if (unlikely(i % MAX_ORDER_NR_PAGES == 0)) {
  72. touch_nmi_watchdog();
  73. }
  74. if (!pfn_valid(pgdat->node_start_pfn + i))
  75. continue;
  76. page = pfn_to_page(pgdat->node_start_pfn + i);
  77. total++;
  78. if (PageReserved(page))
  79. reserved++;
  80. else if (PageSwapCache(page))
  81. cached++;
  82. else if (page_count(page))
  83. shared += page_count(page) - 1;
  84. }
  85. }
  86. printk(KERN_INFO "%lu pages of RAM\n", total);
  87. printk(KERN_INFO "%lu reserved pages\n",reserved);
  88. printk(KERN_INFO "%lu pages shared\n",shared);
  89. printk(KERN_INFO "%lu pages swap cached\n",cached);
  90. }
  91. int after_bootmem;
  92. static __init void *spp_getpage(void)
  93. {
  94. void *ptr;
  95. if (after_bootmem)
  96. ptr = (void *) get_zeroed_page(GFP_ATOMIC);
  97. else
  98. ptr = alloc_bootmem_pages(PAGE_SIZE);
  99. if (!ptr || ((unsigned long)ptr & ~PAGE_MASK))
  100. panic("set_pte_phys: cannot allocate page data %s\n", after_bootmem?"after bootmem":"");
  101. Dprintk("spp_getpage %p\n", ptr);
  102. return ptr;
  103. }
  104. static __init void set_pte_phys(unsigned long vaddr,
  105. unsigned long phys, pgprot_t prot)
  106. {
  107. pgd_t *pgd;
  108. pud_t *pud;
  109. pmd_t *pmd;
  110. pte_t *pte, new_pte;
  111. Dprintk("set_pte_phys %lx to %lx\n", vaddr, phys);
  112. pgd = pgd_offset_k(vaddr);
  113. if (pgd_none(*pgd)) {
  114. printk("PGD FIXMAP MISSING, it should be setup in head.S!\n");
  115. return;
  116. }
  117. pud = pud_offset(pgd, vaddr);
  118. if (pud_none(*pud)) {
  119. pmd = (pmd_t *) spp_getpage();
  120. set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE | _PAGE_USER));
  121. if (pmd != pmd_offset(pud, 0)) {
  122. printk("PAGETABLE BUG #01! %p <-> %p\n", pmd, pmd_offset(pud,0));
  123. return;
  124. }
  125. }
  126. pmd = pmd_offset(pud, vaddr);
  127. if (pmd_none(*pmd)) {
  128. pte = (pte_t *) spp_getpage();
  129. set_pmd(pmd, __pmd(__pa(pte) | _KERNPG_TABLE | _PAGE_USER));
  130. if (pte != pte_offset_kernel(pmd, 0)) {
  131. printk("PAGETABLE BUG #02!\n");
  132. return;
  133. }
  134. }
  135. new_pte = pfn_pte(phys >> PAGE_SHIFT, prot);
  136. pte = pte_offset_kernel(pmd, vaddr);
  137. if (!pte_none(*pte) &&
  138. pte_val(*pte) != (pte_val(new_pte) & __supported_pte_mask))
  139. pte_ERROR(*pte);
  140. set_pte(pte, new_pte);
  141. /*
  142. * It's enough to flush this one mapping.
  143. * (PGE mappings get flushed as well)
  144. */
  145. __flush_tlb_one(vaddr);
  146. }
  147. /* NOTE: this is meant to be run only at boot */
  148. void __init
  149. __set_fixmap (enum fixed_addresses idx, unsigned long phys, pgprot_t prot)
  150. {
  151. unsigned long address = __fix_to_virt(idx);
  152. if (idx >= __end_of_fixed_addresses) {
  153. printk("Invalid __set_fixmap\n");
  154. return;
  155. }
  156. set_pte_phys(address, phys, prot);
  157. }
  158. unsigned long __meminitdata table_start, table_end;
  159. static __meminit void *alloc_low_page(unsigned long *phys)
  160. {
  161. unsigned long pfn = table_end++;
  162. void *adr;
  163. if (after_bootmem) {
  164. adr = (void *)get_zeroed_page(GFP_ATOMIC);
  165. *phys = __pa(adr);
  166. return adr;
  167. }
  168. if (pfn >= end_pfn)
  169. panic("alloc_low_page: ran out of memory");
  170. adr = early_ioremap(pfn * PAGE_SIZE, PAGE_SIZE);
  171. memset(adr, 0, PAGE_SIZE);
  172. *phys = pfn * PAGE_SIZE;
  173. return adr;
  174. }
  175. static __meminit void unmap_low_page(void *adr)
  176. {
  177. if (after_bootmem)
  178. return;
  179. early_iounmap(adr, PAGE_SIZE);
  180. }
  181. /* Must run before zap_low_mappings */
  182. __meminit void *early_ioremap(unsigned long addr, unsigned long size)
  183. {
  184. unsigned long vaddr;
  185. pmd_t *pmd, *last_pmd;
  186. int i, pmds;
  187. pmds = ((addr & ~PMD_MASK) + size + ~PMD_MASK) / PMD_SIZE;
  188. vaddr = __START_KERNEL_map;
  189. pmd = level2_kernel_pgt;
  190. last_pmd = level2_kernel_pgt + PTRS_PER_PMD - 1;
  191. for (; pmd <= last_pmd; pmd++, vaddr += PMD_SIZE) {
  192. for (i = 0; i < pmds; i++) {
  193. if (pmd_present(pmd[i]))
  194. goto next;
  195. }
  196. vaddr += addr & ~PMD_MASK;
  197. addr &= PMD_MASK;
  198. for (i = 0; i < pmds; i++, addr += PMD_SIZE)
  199. set_pmd(pmd+i, __pmd(addr | __PAGE_KERNEL_LARGE_EXEC));
  200. __flush_tlb();
  201. return (void *)vaddr;
  202. next:
  203. ;
  204. }
  205. printk("early_ioremap(0x%lx, %lu) failed\n", addr, size);
  206. return NULL;
  207. }
  208. /* To avoid virtual aliases later */
  209. __meminit void early_iounmap(void *addr, unsigned long size)
  210. {
  211. unsigned long vaddr;
  212. pmd_t *pmd;
  213. int i, pmds;
  214. vaddr = (unsigned long)addr;
  215. pmds = ((vaddr & ~PMD_MASK) + size + ~PMD_MASK) / PMD_SIZE;
  216. pmd = level2_kernel_pgt + pmd_index(vaddr);
  217. for (i = 0; i < pmds; i++)
  218. pmd_clear(pmd + i);
  219. __flush_tlb();
  220. }
  221. static void __meminit
  222. phys_pmd_init(pmd_t *pmd_page, unsigned long address, unsigned long end)
  223. {
  224. int i = pmd_index(address);
  225. for (; i < PTRS_PER_PMD; i++, address += PMD_SIZE) {
  226. unsigned long entry;
  227. pmd_t *pmd = pmd_page + pmd_index(address);
  228. if (address >= end) {
  229. if (!after_bootmem)
  230. for (; i < PTRS_PER_PMD; i++, pmd++)
  231. set_pmd(pmd, __pmd(0));
  232. break;
  233. }
  234. if (pmd_val(*pmd))
  235. continue;
  236. entry = __PAGE_KERNEL_LARGE|_PAGE_GLOBAL|address;
  237. entry &= __supported_pte_mask;
  238. set_pmd(pmd, __pmd(entry));
  239. }
  240. }
  241. static void __meminit
  242. phys_pmd_update(pud_t *pud, unsigned long address, unsigned long end)
  243. {
  244. pmd_t *pmd = pmd_offset(pud,0);
  245. spin_lock(&init_mm.page_table_lock);
  246. phys_pmd_init(pmd, address, end);
  247. spin_unlock(&init_mm.page_table_lock);
  248. __flush_tlb_all();
  249. }
  250. static void __meminit phys_pud_init(pud_t *pud_page, unsigned long addr, unsigned long end)
  251. {
  252. int i = pud_index(addr);
  253. for (; i < PTRS_PER_PUD; i++, addr = (addr & PUD_MASK) + PUD_SIZE ) {
  254. unsigned long pmd_phys;
  255. pud_t *pud = pud_page + pud_index(addr);
  256. pmd_t *pmd;
  257. if (addr >= end)
  258. break;
  259. if (!after_bootmem && !e820_any_mapped(addr,addr+PUD_SIZE,0)) {
  260. set_pud(pud, __pud(0));
  261. continue;
  262. }
  263. if (pud_val(*pud)) {
  264. phys_pmd_update(pud, addr, end);
  265. continue;
  266. }
  267. pmd = alloc_low_page(&pmd_phys);
  268. spin_lock(&init_mm.page_table_lock);
  269. set_pud(pud, __pud(pmd_phys | _KERNPG_TABLE));
  270. phys_pmd_init(pmd, addr, end);
  271. spin_unlock(&init_mm.page_table_lock);
  272. unmap_low_page(pmd);
  273. }
  274. __flush_tlb();
  275. }
  276. static void __init find_early_table_space(unsigned long end)
  277. {
  278. unsigned long puds, pmds, tables, start;
  279. puds = (end + PUD_SIZE - 1) >> PUD_SHIFT;
  280. pmds = (end + PMD_SIZE - 1) >> PMD_SHIFT;
  281. tables = round_up(puds * sizeof(pud_t), PAGE_SIZE) +
  282. round_up(pmds * sizeof(pmd_t), PAGE_SIZE);
  283. /* RED-PEN putting page tables only on node 0 could
  284. cause a hotspot and fill up ZONE_DMA. The page tables
  285. need roughly 0.5KB per GB. */
  286. start = 0x8000;
  287. table_start = find_e820_area(start, end, tables);
  288. if (table_start == -1UL)
  289. panic("Cannot find space for the kernel page tables");
  290. table_start >>= PAGE_SHIFT;
  291. table_end = table_start;
  292. early_printk("kernel direct mapping tables up to %lx @ %lx-%lx\n",
  293. end, table_start << PAGE_SHIFT,
  294. (table_start << PAGE_SHIFT) + tables);
  295. }
  296. /* Setup the direct mapping of the physical memory at PAGE_OFFSET.
  297. This runs before bootmem is initialized and gets pages directly from the
  298. physical memory. To access them they are temporarily mapped. */
  299. void __init_refok init_memory_mapping(unsigned long start, unsigned long end)
  300. {
  301. unsigned long next;
  302. Dprintk("init_memory_mapping\n");
  303. /*
  304. * Find space for the kernel direct mapping tables.
  305. * Later we should allocate these tables in the local node of the memory
  306. * mapped. Unfortunately this is done currently before the nodes are
  307. * discovered.
  308. */
  309. if (!after_bootmem)
  310. find_early_table_space(end);
  311. start = (unsigned long)__va(start);
  312. end = (unsigned long)__va(end);
  313. for (; start < end; start = next) {
  314. unsigned long pud_phys;
  315. pgd_t *pgd = pgd_offset_k(start);
  316. pud_t *pud;
  317. if (after_bootmem)
  318. pud = pud_offset(pgd, start & PGDIR_MASK);
  319. else
  320. pud = alloc_low_page(&pud_phys);
  321. next = start + PGDIR_SIZE;
  322. if (next > end)
  323. next = end;
  324. phys_pud_init(pud, __pa(start), __pa(next));
  325. if (!after_bootmem)
  326. set_pgd(pgd_offset_k(start), mk_kernel_pgd(pud_phys));
  327. unmap_low_page(pud);
  328. }
  329. if (!after_bootmem)
  330. mmu_cr4_features = read_cr4();
  331. __flush_tlb_all();
  332. }
  333. #ifndef CONFIG_NUMA
  334. void __init paging_init(void)
  335. {
  336. unsigned long max_zone_pfns[MAX_NR_ZONES];
  337. memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
  338. max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
  339. max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
  340. max_zone_pfns[ZONE_NORMAL] = end_pfn;
  341. memory_present(0, 0, end_pfn);
  342. sparse_init();
  343. free_area_init_nodes(max_zone_pfns);
  344. }
  345. #endif
  346. /* Unmap a kernel mapping if it exists. This is useful to avoid prefetches
  347. from the CPU leading to inconsistent cache lines. address and size
  348. must be aligned to 2MB boundaries.
  349. Does nothing when the mapping doesn't exist. */
  350. void __init clear_kernel_mapping(unsigned long address, unsigned long size)
  351. {
  352. unsigned long end = address + size;
  353. BUG_ON(address & ~LARGE_PAGE_MASK);
  354. BUG_ON(size & ~LARGE_PAGE_MASK);
  355. for (; address < end; address += LARGE_PAGE_SIZE) {
  356. pgd_t *pgd = pgd_offset_k(address);
  357. pud_t *pud;
  358. pmd_t *pmd;
  359. if (pgd_none(*pgd))
  360. continue;
  361. pud = pud_offset(pgd, address);
  362. if (pud_none(*pud))
  363. continue;
  364. pmd = pmd_offset(pud, address);
  365. if (!pmd || pmd_none(*pmd))
  366. continue;
  367. if (0 == (pmd_val(*pmd) & _PAGE_PSE)) {
  368. /* Could handle this, but it should not happen currently. */
  369. printk(KERN_ERR
  370. "clear_kernel_mapping: mapping has been split. will leak memory\n");
  371. pmd_ERROR(*pmd);
  372. }
  373. set_pmd(pmd, __pmd(0));
  374. }
  375. __flush_tlb_all();
  376. }
  377. /*
  378. * Memory hotplug specific functions
  379. */
  380. void online_page(struct page *page)
  381. {
  382. ClearPageReserved(page);
  383. init_page_count(page);
  384. __free_page(page);
  385. totalram_pages++;
  386. num_physpages++;
  387. }
  388. #ifdef CONFIG_MEMORY_HOTPLUG
  389. /*
  390. * Memory is added always to NORMAL zone. This means you will never get
  391. * additional DMA/DMA32 memory.
  392. */
  393. int arch_add_memory(int nid, u64 start, u64 size)
  394. {
  395. struct pglist_data *pgdat = NODE_DATA(nid);
  396. struct zone *zone = pgdat->node_zones + ZONE_NORMAL;
  397. unsigned long start_pfn = start >> PAGE_SHIFT;
  398. unsigned long nr_pages = size >> PAGE_SHIFT;
  399. int ret;
  400. init_memory_mapping(start, (start + size -1));
  401. ret = __add_pages(zone, start_pfn, nr_pages);
  402. if (ret)
  403. goto error;
  404. return ret;
  405. error:
  406. printk("%s: Problem encountered in __add_pages!\n", __func__);
  407. return ret;
  408. }
  409. EXPORT_SYMBOL_GPL(arch_add_memory);
  410. #if !defined(CONFIG_ACPI_NUMA) && defined(CONFIG_NUMA)
  411. int memory_add_physaddr_to_nid(u64 start)
  412. {
  413. return 0;
  414. }
  415. EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
  416. #endif
  417. #endif /* CONFIG_MEMORY_HOTPLUG */
  418. static struct kcore_list kcore_mem, kcore_vmalloc, kcore_kernel, kcore_modules,
  419. kcore_vsyscall;
  420. void __init mem_init(void)
  421. {
  422. long codesize, reservedpages, datasize, initsize;
  423. pci_iommu_alloc();
  424. /* clear_bss() already clear the empty_zero_page */
  425. /* temporary debugging - double check it's true: */
  426. {
  427. int i;
  428. for (i = 0; i < 1024; i++)
  429. WARN_ON_ONCE(empty_zero_page[i]);
  430. }
  431. reservedpages = 0;
  432. /* this will put all low memory onto the freelists */
  433. #ifdef CONFIG_NUMA
  434. totalram_pages = numa_free_all_bootmem();
  435. #else
  436. totalram_pages = free_all_bootmem();
  437. #endif
  438. reservedpages = end_pfn - totalram_pages -
  439. absent_pages_in_range(0, end_pfn);
  440. after_bootmem = 1;
  441. codesize = (unsigned long) &_etext - (unsigned long) &_text;
  442. datasize = (unsigned long) &_edata - (unsigned long) &_etext;
  443. initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
  444. /* Register memory areas for /proc/kcore */
  445. kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
  446. kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
  447. VMALLOC_END-VMALLOC_START);
  448. kclist_add(&kcore_kernel, &_stext, _end - _stext);
  449. kclist_add(&kcore_modules, (void *)MODULES_VADDR, MODULES_LEN);
  450. kclist_add(&kcore_vsyscall, (void *)VSYSCALL_START,
  451. VSYSCALL_END - VSYSCALL_START);
  452. printk("Memory: %luk/%luk available (%ldk kernel code, %ldk reserved, %ldk data, %ldk init)\n",
  453. (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
  454. end_pfn << (PAGE_SHIFT-10),
  455. codesize >> 10,
  456. reservedpages << (PAGE_SHIFT-10),
  457. datasize >> 10,
  458. initsize >> 10);
  459. }
  460. void free_init_pages(char *what, unsigned long begin, unsigned long end)
  461. {
  462. unsigned long addr;
  463. if (begin >= end)
  464. return;
  465. printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
  466. for (addr = begin; addr < end; addr += PAGE_SIZE) {
  467. ClearPageReserved(virt_to_page(addr));
  468. init_page_count(virt_to_page(addr));
  469. memset((void *)(addr & ~(PAGE_SIZE-1)),
  470. POISON_FREE_INITMEM, PAGE_SIZE);
  471. if (addr >= __START_KERNEL_map)
  472. change_page_attr_addr(addr, 1, __pgprot(0));
  473. free_page(addr);
  474. totalram_pages++;
  475. }
  476. if (addr > __START_KERNEL_map)
  477. global_flush_tlb();
  478. }
  479. void free_initmem(void)
  480. {
  481. free_init_pages("unused kernel memory",
  482. (unsigned long)(&__init_begin),
  483. (unsigned long)(&__init_end));
  484. }
  485. #ifdef CONFIG_DEBUG_RODATA
  486. void mark_rodata_ro(void)
  487. {
  488. unsigned long start = (unsigned long)_stext, end;
  489. #ifdef CONFIG_HOTPLUG_CPU
  490. /* It must still be possible to apply SMP alternatives. */
  491. if (num_possible_cpus() > 1)
  492. start = (unsigned long)_etext;
  493. #endif
  494. #ifdef CONFIG_KPROBES
  495. start = (unsigned long)__start_rodata;
  496. #endif
  497. end = (unsigned long)__end_rodata;
  498. start = (start + PAGE_SIZE - 1) & PAGE_MASK;
  499. end &= PAGE_MASK;
  500. if (end <= start)
  501. return;
  502. change_page_attr_addr(start, (end - start) >> PAGE_SHIFT, PAGE_KERNEL_RO);
  503. printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
  504. (end - start) >> 10);
  505. /*
  506. * change_page_attr_addr() requires a global_flush_tlb() call after it.
  507. * We do this after the printk so that if something went wrong in the
  508. * change, the printk gets out at least to give a better debug hint
  509. * of who is the culprit.
  510. */
  511. global_flush_tlb();
  512. }
  513. #endif
  514. #ifdef CONFIG_BLK_DEV_INITRD
  515. void free_initrd_mem(unsigned long start, unsigned long end)
  516. {
  517. free_init_pages("initrd memory", start, end);
  518. }
  519. #endif
  520. void __init reserve_bootmem_generic(unsigned long phys, unsigned len)
  521. {
  522. #ifdef CONFIG_NUMA
  523. int nid = phys_to_nid(phys);
  524. #endif
  525. unsigned long pfn = phys >> PAGE_SHIFT;
  526. if (pfn >= end_pfn) {
  527. /* This can happen with kdump kernels when accessing firmware
  528. tables. */
  529. if (pfn < end_pfn_map)
  530. return;
  531. printk(KERN_ERR "reserve_bootmem: illegal reserve %lx %u\n",
  532. phys, len);
  533. return;
  534. }
  535. /* Should check here against the e820 map to avoid double free */
  536. #ifdef CONFIG_NUMA
  537. reserve_bootmem_node(NODE_DATA(nid), phys, len);
  538. #else
  539. reserve_bootmem(phys, len);
  540. #endif
  541. if (phys+len <= MAX_DMA_PFN*PAGE_SIZE) {
  542. dma_reserve += len / PAGE_SIZE;
  543. set_dma_reserve(dma_reserve);
  544. }
  545. }
  546. int kern_addr_valid(unsigned long addr)
  547. {
  548. unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT;
  549. pgd_t *pgd;
  550. pud_t *pud;
  551. pmd_t *pmd;
  552. pte_t *pte;
  553. if (above != 0 && above != -1UL)
  554. return 0;
  555. pgd = pgd_offset_k(addr);
  556. if (pgd_none(*pgd))
  557. return 0;
  558. pud = pud_offset(pgd, addr);
  559. if (pud_none(*pud))
  560. return 0;
  561. pmd = pmd_offset(pud, addr);
  562. if (pmd_none(*pmd))
  563. return 0;
  564. if (pmd_large(*pmd))
  565. return pfn_valid(pmd_pfn(*pmd));
  566. pte = pte_offset_kernel(pmd, addr);
  567. if (pte_none(*pte))
  568. return 0;
  569. return pfn_valid(pte_pfn(*pte));
  570. }
  571. /* A pseudo VMA to allow ptrace access for the vsyscall page. This only
  572. covers the 64bit vsyscall page now. 32bit has a real VMA now and does
  573. not need special handling anymore. */
  574. static struct vm_area_struct gate_vma = {
  575. .vm_start = VSYSCALL_START,
  576. .vm_end = VSYSCALL_START + (VSYSCALL_MAPPED_PAGES << PAGE_SHIFT),
  577. .vm_page_prot = PAGE_READONLY_EXEC,
  578. .vm_flags = VM_READ | VM_EXEC
  579. };
  580. struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
  581. {
  582. #ifdef CONFIG_IA32_EMULATION
  583. if (test_tsk_thread_flag(tsk, TIF_IA32))
  584. return NULL;
  585. #endif
  586. return &gate_vma;
  587. }
  588. int in_gate_area(struct task_struct *task, unsigned long addr)
  589. {
  590. struct vm_area_struct *vma = get_gate_vma(task);
  591. if (!vma)
  592. return 0;
  593. return (addr >= vma->vm_start) && (addr < vma->vm_end);
  594. }
  595. /* Use this when you have no reliable task/vma, typically from interrupt
  596. * context. It is less reliable than using the task's vma and may give
  597. * false positives.
  598. */
  599. int in_gate_area_no_task(unsigned long addr)
  600. {
  601. return (addr >= VSYSCALL_START) && (addr < VSYSCALL_END);
  602. }
  603. const char *arch_vma_name(struct vm_area_struct *vma)
  604. {
  605. if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
  606. return "[vdso]";
  607. if (vma == &gate_vma)
  608. return "[vsyscall]";
  609. return NULL;
  610. }
  611. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  612. /*
  613. * Initialise the sparsemem vmemmap using huge-pages at the PMD level.
  614. */
  615. int __meminit vmemmap_populate(struct page *start_page,
  616. unsigned long size, int node)
  617. {
  618. unsigned long addr = (unsigned long)start_page;
  619. unsigned long end = (unsigned long)(start_page + size);
  620. unsigned long next;
  621. pgd_t *pgd;
  622. pud_t *pud;
  623. pmd_t *pmd;
  624. for (; addr < end; addr = next) {
  625. next = pmd_addr_end(addr, end);
  626. pgd = vmemmap_pgd_populate(addr, node);
  627. if (!pgd)
  628. return -ENOMEM;
  629. pud = vmemmap_pud_populate(pgd, addr, node);
  630. if (!pud)
  631. return -ENOMEM;
  632. pmd = pmd_offset(pud, addr);
  633. if (pmd_none(*pmd)) {
  634. pte_t entry;
  635. void *p = vmemmap_alloc_block(PMD_SIZE, node);
  636. if (!p)
  637. return -ENOMEM;
  638. entry = pfn_pte(__pa(p) >> PAGE_SHIFT, PAGE_KERNEL_LARGE);
  639. set_pmd(pmd, __pmd(pte_val(entry)));
  640. printk(KERN_DEBUG " [%lx-%lx] PMD ->%p on node %d\n",
  641. addr, addr + PMD_SIZE - 1, p, node);
  642. } else
  643. vmemmap_verify((pte_t *)pmd, node, addr, next);
  644. }
  645. return 0;
  646. }
  647. #endif