init.c 18 KB

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