init_64.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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. if (!pud_large(*pud))
  316. phys_pmd_update(pud, addr, end);
  317. continue;
  318. }
  319. if (direct_gbpages) {
  320. set_pte((pte_t *)pud,
  321. pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL_LARGE));
  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. 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. }
  333. static void __init find_early_table_space(unsigned long end)
  334. {
  335. unsigned long puds, pmds, tables, start;
  336. puds = (end + PUD_SIZE - 1) >> PUD_SHIFT;
  337. tables = round_up(puds * sizeof(pud_t), PAGE_SIZE);
  338. if (!direct_gbpages) {
  339. pmds = (end + PMD_SIZE - 1) >> PMD_SHIFT;
  340. tables += round_up(pmds * sizeof(pmd_t), PAGE_SIZE);
  341. }
  342. /*
  343. * RED-PEN putting page tables only on node 0 could
  344. * cause a hotspot and fill up ZONE_DMA. The page tables
  345. * need roughly 0.5KB per GB.
  346. */
  347. start = 0x8000;
  348. table_start = find_e820_area(start, end, tables, PAGE_SIZE);
  349. if (table_start == -1UL)
  350. panic("Cannot find space for the kernel page tables");
  351. table_start >>= PAGE_SHIFT;
  352. table_end = table_start;
  353. early_printk("kernel direct mapping tables up to %lx @ %lx-%lx\n",
  354. end, table_start << PAGE_SHIFT,
  355. (table_start << PAGE_SHIFT) + tables);
  356. }
  357. static void __init init_gbpages(void)
  358. {
  359. if (direct_gbpages && cpu_has_gbpages)
  360. printk(KERN_INFO "Using GB pages for direct mapping\n");
  361. else
  362. direct_gbpages = 0;
  363. }
  364. /*
  365. * Setup the direct mapping of the physical memory at PAGE_OFFSET.
  366. * This runs before bootmem is initialized and gets pages directly from
  367. * the physical memory. To access them they are temporarily mapped.
  368. */
  369. void __init_refok init_memory_mapping(unsigned long start, unsigned long end)
  370. {
  371. unsigned long next;
  372. pr_debug("init_memory_mapping\n");
  373. /*
  374. * Find space for the kernel direct mapping tables.
  375. *
  376. * Later we should allocate these tables in the local node of the
  377. * memory mapped. Unfortunately this is done currently before the
  378. * nodes are discovered.
  379. */
  380. if (!after_bootmem) {
  381. init_gbpages();
  382. find_early_table_space(end);
  383. }
  384. start = (unsigned long)__va(start);
  385. end = (unsigned long)__va(end);
  386. for (; start < end; start = next) {
  387. pgd_t *pgd = pgd_offset_k(start);
  388. unsigned long pud_phys;
  389. pud_t *pud;
  390. if (after_bootmem)
  391. pud = pud_offset(pgd, start & PGDIR_MASK);
  392. else
  393. pud = alloc_low_page(&pud_phys);
  394. next = start + PGDIR_SIZE;
  395. if (next > end)
  396. next = end;
  397. phys_pud_init(pud, __pa(start), __pa(next));
  398. if (!after_bootmem)
  399. set_pgd(pgd_offset_k(start), mk_kernel_pgd(pud_phys));
  400. unmap_low_page(pud);
  401. }
  402. if (!after_bootmem)
  403. mmu_cr4_features = read_cr4();
  404. __flush_tlb_all();
  405. if (!after_bootmem)
  406. reserve_early(table_start << PAGE_SHIFT,
  407. table_end << PAGE_SHIFT, "PGTABLE");
  408. }
  409. #ifndef CONFIG_NUMA
  410. void __init paging_init(void)
  411. {
  412. unsigned long max_zone_pfns[MAX_NR_ZONES];
  413. memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
  414. max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
  415. max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
  416. max_zone_pfns[ZONE_NORMAL] = end_pfn;
  417. memory_present(0, 0, end_pfn);
  418. sparse_init();
  419. free_area_init_nodes(max_zone_pfns);
  420. }
  421. #endif
  422. /*
  423. * Memory hotplug specific functions
  424. */
  425. void online_page(struct page *page)
  426. {
  427. ClearPageReserved(page);
  428. init_page_count(page);
  429. __free_page(page);
  430. totalram_pages++;
  431. num_physpages++;
  432. }
  433. #ifdef CONFIG_MEMORY_HOTPLUG
  434. /*
  435. * Memory is added always to NORMAL zone. This means you will never get
  436. * additional DMA/DMA32 memory.
  437. */
  438. int arch_add_memory(int nid, u64 start, u64 size)
  439. {
  440. struct pglist_data *pgdat = NODE_DATA(nid);
  441. struct zone *zone = pgdat->node_zones + ZONE_NORMAL;
  442. unsigned long start_pfn = start >> PAGE_SHIFT;
  443. unsigned long nr_pages = size >> PAGE_SHIFT;
  444. int ret;
  445. init_memory_mapping(start, start + size-1);
  446. ret = __add_pages(zone, start_pfn, nr_pages);
  447. WARN_ON(1);
  448. return ret;
  449. }
  450. EXPORT_SYMBOL_GPL(arch_add_memory);
  451. #if !defined(CONFIG_ACPI_NUMA) && defined(CONFIG_NUMA)
  452. int memory_add_physaddr_to_nid(u64 start)
  453. {
  454. return 0;
  455. }
  456. EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
  457. #endif
  458. #endif /* CONFIG_MEMORY_HOTPLUG */
  459. static struct kcore_list kcore_mem, kcore_vmalloc, kcore_kernel,
  460. kcore_modules, kcore_vsyscall;
  461. void __init mem_init(void)
  462. {
  463. long codesize, reservedpages, datasize, initsize;
  464. pci_iommu_alloc();
  465. /* clear_bss() already clear the empty_zero_page */
  466. reservedpages = 0;
  467. /* this will put all low memory onto the freelists */
  468. #ifdef CONFIG_NUMA
  469. totalram_pages = numa_free_all_bootmem();
  470. #else
  471. totalram_pages = free_all_bootmem();
  472. #endif
  473. reservedpages = end_pfn - totalram_pages -
  474. absent_pages_in_range(0, end_pfn);
  475. after_bootmem = 1;
  476. codesize = (unsigned long) &_etext - (unsigned long) &_text;
  477. datasize = (unsigned long) &_edata - (unsigned long) &_etext;
  478. initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
  479. /* Register memory areas for /proc/kcore */
  480. kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
  481. kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
  482. VMALLOC_END-VMALLOC_START);
  483. kclist_add(&kcore_kernel, &_stext, _end - _stext);
  484. kclist_add(&kcore_modules, (void *)MODULES_VADDR, MODULES_LEN);
  485. kclist_add(&kcore_vsyscall, (void *)VSYSCALL_START,
  486. VSYSCALL_END - VSYSCALL_START);
  487. printk(KERN_INFO "Memory: %luk/%luk available (%ldk kernel code, "
  488. "%ldk reserved, %ldk data, %ldk init)\n",
  489. (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
  490. end_pfn << (PAGE_SHIFT-10),
  491. codesize >> 10,
  492. reservedpages << (PAGE_SHIFT-10),
  493. datasize >> 10,
  494. initsize >> 10);
  495. cpa_init();
  496. }
  497. void free_init_pages(char *what, unsigned long begin, unsigned long end)
  498. {
  499. unsigned long addr = begin;
  500. if (addr >= end)
  501. return;
  502. /*
  503. * If debugging page accesses then do not free this memory but
  504. * mark them not present - any buggy init-section access will
  505. * create a kernel page fault:
  506. */
  507. #ifdef CONFIG_DEBUG_PAGEALLOC
  508. printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n",
  509. begin, PAGE_ALIGN(end));
  510. set_memory_np(begin, (end - begin) >> PAGE_SHIFT);
  511. #else
  512. printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
  513. for (; addr < end; addr += PAGE_SIZE) {
  514. ClearPageReserved(virt_to_page(addr));
  515. init_page_count(virt_to_page(addr));
  516. memset((void *)(addr & ~(PAGE_SIZE-1)),
  517. POISON_FREE_INITMEM, PAGE_SIZE);
  518. free_page(addr);
  519. totalram_pages++;
  520. }
  521. #endif
  522. }
  523. void free_initmem(void)
  524. {
  525. free_init_pages("unused kernel memory",
  526. (unsigned long)(&__init_begin),
  527. (unsigned long)(&__init_end));
  528. }
  529. #ifdef CONFIG_DEBUG_RODATA
  530. const int rodata_test_data = 0xC3;
  531. EXPORT_SYMBOL_GPL(rodata_test_data);
  532. void mark_rodata_ro(void)
  533. {
  534. unsigned long start = (unsigned long)_stext, end;
  535. #ifdef CONFIG_HOTPLUG_CPU
  536. /* It must still be possible to apply SMP alternatives. */
  537. if (num_possible_cpus() > 1)
  538. start = (unsigned long)_etext;
  539. #endif
  540. #ifdef CONFIG_KPROBES
  541. start = (unsigned long)__start_rodata;
  542. #endif
  543. end = (unsigned long)__end_rodata;
  544. start = (start + PAGE_SIZE - 1) & PAGE_MASK;
  545. end &= PAGE_MASK;
  546. if (end <= start)
  547. return;
  548. printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
  549. (end - start) >> 10);
  550. set_memory_ro(start, (end - start) >> PAGE_SHIFT);
  551. /*
  552. * The rodata section (but not the kernel text!) should also be
  553. * not-executable.
  554. */
  555. start = ((unsigned long)__start_rodata + PAGE_SIZE - 1) & PAGE_MASK;
  556. set_memory_nx(start, (end - start) >> PAGE_SHIFT);
  557. rodata_test();
  558. #ifdef CONFIG_CPA_DEBUG
  559. printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, end);
  560. set_memory_rw(start, (end-start) >> PAGE_SHIFT);
  561. printk(KERN_INFO "Testing CPA: again\n");
  562. set_memory_ro(start, (end-start) >> PAGE_SHIFT);
  563. #endif
  564. }
  565. #endif
  566. #ifdef CONFIG_BLK_DEV_INITRD
  567. void free_initrd_mem(unsigned long start, unsigned long end)
  568. {
  569. free_init_pages("initrd memory", start, end);
  570. }
  571. #endif
  572. void __init reserve_bootmem_generic(unsigned long phys, unsigned len)
  573. {
  574. #ifdef CONFIG_NUMA
  575. int nid = phys_to_nid(phys);
  576. #endif
  577. unsigned long pfn = phys >> PAGE_SHIFT;
  578. if (pfn >= end_pfn) {
  579. /*
  580. * This can happen with kdump kernels when accessing
  581. * firmware tables:
  582. */
  583. if (pfn < end_pfn_map)
  584. return;
  585. printk(KERN_ERR "reserve_bootmem: illegal reserve %lx %u\n",
  586. phys, len);
  587. return;
  588. }
  589. /* Should check here against the e820 map to avoid double free */
  590. #ifdef CONFIG_NUMA
  591. reserve_bootmem_node(NODE_DATA(nid), phys, len, BOOTMEM_DEFAULT);
  592. #else
  593. reserve_bootmem(phys, len, BOOTMEM_DEFAULT);
  594. #endif
  595. if (phys+len <= MAX_DMA_PFN*PAGE_SIZE) {
  596. dma_reserve += len / PAGE_SIZE;
  597. set_dma_reserve(dma_reserve);
  598. }
  599. }
  600. int kern_addr_valid(unsigned long addr)
  601. {
  602. unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT;
  603. pgd_t *pgd;
  604. pud_t *pud;
  605. pmd_t *pmd;
  606. pte_t *pte;
  607. if (above != 0 && above != -1UL)
  608. return 0;
  609. pgd = pgd_offset_k(addr);
  610. if (pgd_none(*pgd))
  611. return 0;
  612. pud = pud_offset(pgd, addr);
  613. if (pud_none(*pud))
  614. return 0;
  615. pmd = pmd_offset(pud, addr);
  616. if (pmd_none(*pmd))
  617. return 0;
  618. if (pmd_large(*pmd))
  619. return pfn_valid(pmd_pfn(*pmd));
  620. pte = pte_offset_kernel(pmd, addr);
  621. if (pte_none(*pte))
  622. return 0;
  623. return pfn_valid(pte_pfn(*pte));
  624. }
  625. /*
  626. * A pseudo VMA to allow ptrace access for the vsyscall page. This only
  627. * covers the 64bit vsyscall page now. 32bit has a real VMA now and does
  628. * not need special handling anymore:
  629. */
  630. static struct vm_area_struct gate_vma = {
  631. .vm_start = VSYSCALL_START,
  632. .vm_end = VSYSCALL_START + (VSYSCALL_MAPPED_PAGES * PAGE_SIZE),
  633. .vm_page_prot = PAGE_READONLY_EXEC,
  634. .vm_flags = VM_READ | VM_EXEC
  635. };
  636. struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
  637. {
  638. #ifdef CONFIG_IA32_EMULATION
  639. if (test_tsk_thread_flag(tsk, TIF_IA32))
  640. return NULL;
  641. #endif
  642. return &gate_vma;
  643. }
  644. int in_gate_area(struct task_struct *task, unsigned long addr)
  645. {
  646. struct vm_area_struct *vma = get_gate_vma(task);
  647. if (!vma)
  648. return 0;
  649. return (addr >= vma->vm_start) && (addr < vma->vm_end);
  650. }
  651. /*
  652. * Use this when you have no reliable task/vma, typically from interrupt
  653. * context. It is less reliable than using the task's vma and may give
  654. * false positives:
  655. */
  656. int in_gate_area_no_task(unsigned long addr)
  657. {
  658. return (addr >= VSYSCALL_START) && (addr < VSYSCALL_END);
  659. }
  660. const char *arch_vma_name(struct vm_area_struct *vma)
  661. {
  662. if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
  663. return "[vdso]";
  664. if (vma == &gate_vma)
  665. return "[vsyscall]";
  666. return NULL;
  667. }
  668. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  669. /*
  670. * Initialise the sparsemem vmemmap using huge-pages at the PMD level.
  671. */
  672. int __meminit
  673. vmemmap_populate(struct page *start_page, unsigned long size, int node)
  674. {
  675. unsigned long addr = (unsigned long)start_page;
  676. unsigned long end = (unsigned long)(start_page + size);
  677. unsigned long next;
  678. pgd_t *pgd;
  679. pud_t *pud;
  680. pmd_t *pmd;
  681. for (; addr < end; addr = next) {
  682. next = pmd_addr_end(addr, end);
  683. pgd = vmemmap_pgd_populate(addr, node);
  684. if (!pgd)
  685. return -ENOMEM;
  686. pud = vmemmap_pud_populate(pgd, addr, node);
  687. if (!pud)
  688. return -ENOMEM;
  689. pmd = pmd_offset(pud, addr);
  690. if (pmd_none(*pmd)) {
  691. pte_t entry;
  692. void *p;
  693. p = vmemmap_alloc_block(PMD_SIZE, node);
  694. if (!p)
  695. return -ENOMEM;
  696. entry = pfn_pte(__pa(p) >> PAGE_SHIFT,
  697. PAGE_KERNEL_LARGE);
  698. set_pmd(pmd, __pmd(pte_val(entry)));
  699. printk(KERN_DEBUG " [%lx-%lx] PMD ->%p on node %d\n",
  700. addr, addr + PMD_SIZE - 1, p, node);
  701. } else {
  702. vmemmap_verify((pte_t *)pmd, node, addr, next);
  703. }
  704. }
  705. return 0;
  706. }
  707. #endif