init_64.c 19 KB

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