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