init_64.c 19 KB

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