init.c 18 KB

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