init.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  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_online_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 __init 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 __init 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 __init
  141. __set_fixmap (enum fixed_addresses idx, unsigned long phys, pgprot_t prot)
  142. {
  143. unsigned long address = __fix_to_virt(idx);
  144. if (idx >= __end_of_fixed_addresses) {
  145. printk("Invalid __set_fixmap\n");
  146. return;
  147. }
  148. set_pte_phys(address, phys, prot);
  149. }
  150. unsigned long __initdata table_start, table_end;
  151. extern pmd_t temp_boot_pmds[];
  152. static struct temp_map {
  153. pmd_t *pmd;
  154. void *address;
  155. int allocated;
  156. } temp_mappings[] __initdata = {
  157. { &temp_boot_pmds[0], (void *)(40UL * 1024 * 1024) },
  158. { &temp_boot_pmds[1], (void *)(42UL * 1024 * 1024) },
  159. {}
  160. };
  161. static __meminit void *alloc_low_page(int *index, unsigned long *phys)
  162. {
  163. struct temp_map *ti;
  164. int i;
  165. unsigned long pfn = table_end++, paddr;
  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. for (i = 0; temp_mappings[i].allocated; i++) {
  175. if (!temp_mappings[i].pmd)
  176. panic("alloc_low_page: ran out of temp mappings");
  177. }
  178. ti = &temp_mappings[i];
  179. paddr = (pfn << PAGE_SHIFT) & PMD_MASK;
  180. set_pmd(ti->pmd, __pmd(paddr | _KERNPG_TABLE | _PAGE_PSE));
  181. ti->allocated = 1;
  182. __flush_tlb();
  183. adr = ti->address + ((pfn << PAGE_SHIFT) & ~PMD_MASK);
  184. memset(adr, 0, PAGE_SIZE);
  185. *index = i;
  186. *phys = pfn * PAGE_SIZE;
  187. return adr;
  188. }
  189. static __meminit void unmap_low_page(int i)
  190. {
  191. struct temp_map *ti;
  192. if (after_bootmem)
  193. return;
  194. ti = &temp_mappings[i];
  195. set_pmd(ti->pmd, __pmd(0));
  196. ti->allocated = 0;
  197. }
  198. /* Must run before zap_low_mappings */
  199. __init void *early_ioremap(unsigned long addr, unsigned long size)
  200. {
  201. unsigned long map = round_down(addr, LARGE_PAGE_SIZE);
  202. /* actually usually some more */
  203. if (size >= LARGE_PAGE_SIZE) {
  204. printk("SMBIOS area too long %lu\n", size);
  205. return NULL;
  206. }
  207. set_pmd(temp_mappings[0].pmd, __pmd(map | _KERNPG_TABLE | _PAGE_PSE));
  208. map += LARGE_PAGE_SIZE;
  209. set_pmd(temp_mappings[1].pmd, __pmd(map | _KERNPG_TABLE | _PAGE_PSE));
  210. __flush_tlb();
  211. return temp_mappings[0].address + (addr & (LARGE_PAGE_SIZE-1));
  212. }
  213. /* To avoid virtual aliases later */
  214. __init void early_iounmap(void *addr, unsigned long size)
  215. {
  216. if ((void *)round_down((unsigned long)addr, LARGE_PAGE_SIZE) != temp_mappings[0].address)
  217. printk("early_iounmap: bad address %p\n", addr);
  218. set_pmd(temp_mappings[0].pmd, __pmd(0));
  219. set_pmd(temp_mappings[1].pmd, __pmd(0));
  220. __flush_tlb();
  221. }
  222. static void __meminit
  223. phys_pmd_init(pmd_t *pmd, unsigned long address, unsigned long end)
  224. {
  225. int i;
  226. for (i = 0; i < PTRS_PER_PMD; pmd++, i++, address += PMD_SIZE) {
  227. unsigned long entry;
  228. if (address > end) {
  229. for (; i < PTRS_PER_PMD; i++, pmd++)
  230. set_pmd(pmd, __pmd(0));
  231. break;
  232. }
  233. entry = _PAGE_NX|_PAGE_PSE|_KERNPG_TABLE|_PAGE_GLOBAL|address;
  234. entry &= __supported_pte_mask;
  235. set_pmd(pmd, __pmd(entry));
  236. }
  237. }
  238. static void __meminit
  239. phys_pmd_update(pud_t *pud, unsigned long address, unsigned long end)
  240. {
  241. pmd_t *pmd = pmd_offset(pud, (unsigned long)__va(address));
  242. if (pmd_none(*pmd)) {
  243. spin_lock(&init_mm.page_table_lock);
  244. phys_pmd_init(pmd, address, end);
  245. spin_unlock(&init_mm.page_table_lock);
  246. __flush_tlb_all();
  247. }
  248. }
  249. static void __meminit phys_pud_init(pud_t *pud, unsigned long address, unsigned long end)
  250. {
  251. long i = pud_index(address);
  252. pud = pud + i;
  253. if (after_bootmem && pud_val(*pud)) {
  254. phys_pmd_update(pud, address, end);
  255. return;
  256. }
  257. for (; i < PTRS_PER_PUD; pud++, i++) {
  258. int map;
  259. unsigned long paddr, pmd_phys;
  260. pmd_t *pmd;
  261. paddr = (address & PGDIR_MASK) + i*PUD_SIZE;
  262. if (paddr >= end)
  263. break;
  264. if (!after_bootmem && !e820_any_mapped(paddr, paddr+PUD_SIZE, 0)) {
  265. set_pud(pud, __pud(0));
  266. continue;
  267. }
  268. pmd = alloc_low_page(&map, &pmd_phys);
  269. spin_lock(&init_mm.page_table_lock);
  270. set_pud(pud, __pud(pmd_phys | _KERNPG_TABLE));
  271. phys_pmd_init(pmd, paddr, end);
  272. spin_unlock(&init_mm.page_table_lock);
  273. unmap_low_page(map);
  274. }
  275. __flush_tlb();
  276. }
  277. static void __init find_early_table_space(unsigned long end)
  278. {
  279. unsigned long puds, pmds, tables, start;
  280. puds = (end + PUD_SIZE - 1) >> PUD_SHIFT;
  281. pmds = (end + PMD_SIZE - 1) >> PMD_SHIFT;
  282. tables = round_up(puds * sizeof(pud_t), PAGE_SIZE) +
  283. round_up(pmds * sizeof(pmd_t), PAGE_SIZE);
  284. /* RED-PEN putting page tables only on node 0 could
  285. cause a hotspot and fill up ZONE_DMA. The page tables
  286. need roughly 0.5KB per GB. */
  287. start = 0x8000;
  288. table_start = find_e820_area(start, end, tables);
  289. if (table_start == -1UL)
  290. panic("Cannot find space for the kernel page tables");
  291. table_start >>= PAGE_SHIFT;
  292. table_end = table_start;
  293. early_printk("kernel direct mapping tables up to %lx @ %lx-%lx\n",
  294. end, table_start << PAGE_SHIFT, table_end << PAGE_SHIFT);
  295. }
  296. /* Setup the direct mapping of the physical memory at PAGE_OFFSET.
  297. This runs before bootmem is initialized and gets pages directly from the
  298. physical memory. To access them they are temporarily mapped. */
  299. void __meminit init_memory_mapping(unsigned long start, unsigned long end)
  300. {
  301. unsigned long next;
  302. Dprintk("init_memory_mapping\n");
  303. /*
  304. * Find space for the kernel direct mapping tables.
  305. * Later we should allocate these tables in the local node of the memory
  306. * mapped. Unfortunately this is done currently before the nodes are
  307. * discovered.
  308. */
  309. if (!after_bootmem)
  310. find_early_table_space(end);
  311. start = (unsigned long)__va(start);
  312. end = (unsigned long)__va(end);
  313. for (; start < end; start = next) {
  314. int map;
  315. unsigned long pud_phys;
  316. pgd_t *pgd = pgd_offset_k(start);
  317. pud_t *pud;
  318. if (after_bootmem)
  319. pud = pud_offset_k(pgd, start & PGDIR_MASK);
  320. else
  321. pud = alloc_low_page(&map, &pud_phys);
  322. next = start + PGDIR_SIZE;
  323. if (next > end)
  324. next = end;
  325. phys_pud_init(pud, __pa(start), __pa(next));
  326. if (!after_bootmem)
  327. set_pgd(pgd_offset_k(start), mk_kernel_pgd(pud_phys));
  328. unmap_low_page(map);
  329. }
  330. if (!after_bootmem)
  331. asm volatile("movq %%cr4,%0" : "=r" (mmu_cr4_features));
  332. __flush_tlb_all();
  333. }
  334. void __cpuinit zap_low_mappings(int cpu)
  335. {
  336. if (cpu == 0) {
  337. pgd_t *pgd = pgd_offset_k(0UL);
  338. pgd_clear(pgd);
  339. } else {
  340. /*
  341. * For AP's, zap the low identity mappings by changing the cr3
  342. * to init_level4_pgt and doing local flush tlb all
  343. */
  344. asm volatile("movq %0,%%cr3" :: "r" (__pa_symbol(&init_level4_pgt)));
  345. }
  346. __flush_tlb_all();
  347. }
  348. /* Compute zone sizes for the DMA and DMA32 zones in a node. */
  349. __init void
  350. size_zones(unsigned long *z, unsigned long *h,
  351. unsigned long start_pfn, unsigned long end_pfn)
  352. {
  353. int i;
  354. unsigned long w;
  355. for (i = 0; i < MAX_NR_ZONES; i++)
  356. z[i] = 0;
  357. if (start_pfn < MAX_DMA_PFN)
  358. z[ZONE_DMA] = MAX_DMA_PFN - start_pfn;
  359. if (start_pfn < MAX_DMA32_PFN) {
  360. unsigned long dma32_pfn = MAX_DMA32_PFN;
  361. if (dma32_pfn > end_pfn)
  362. dma32_pfn = end_pfn;
  363. z[ZONE_DMA32] = dma32_pfn - start_pfn;
  364. }
  365. z[ZONE_NORMAL] = end_pfn - start_pfn;
  366. /* Remove lower zones from higher ones. */
  367. w = 0;
  368. for (i = 0; i < MAX_NR_ZONES; i++) {
  369. if (z[i])
  370. z[i] -= w;
  371. w += z[i];
  372. }
  373. /* Compute holes */
  374. w = start_pfn;
  375. for (i = 0; i < MAX_NR_ZONES; i++) {
  376. unsigned long s = w;
  377. w += z[i];
  378. h[i] = e820_hole_size(s, w);
  379. }
  380. /* Add the space pace needed for mem_map to the holes too. */
  381. for (i = 0; i < MAX_NR_ZONES; i++)
  382. h[i] += (z[i] * sizeof(struct page)) / PAGE_SIZE;
  383. /* The 16MB DMA zone has the kernel and other misc mappings.
  384. Account them too */
  385. if (h[ZONE_DMA]) {
  386. h[ZONE_DMA] += dma_reserve;
  387. if (h[ZONE_DMA] >= z[ZONE_DMA]) {
  388. printk(KERN_WARNING
  389. "Kernel too large and filling up ZONE_DMA?\n");
  390. h[ZONE_DMA] = z[ZONE_DMA];
  391. }
  392. }
  393. }
  394. #ifndef CONFIG_NUMA
  395. void __init paging_init(void)
  396. {
  397. unsigned long zones[MAX_NR_ZONES], holes[MAX_NR_ZONES];
  398. memory_present(0, 0, end_pfn);
  399. sparse_init();
  400. size_zones(zones, holes, 0, end_pfn);
  401. free_area_init_node(0, NODE_DATA(0), zones,
  402. __pa(PAGE_OFFSET) >> PAGE_SHIFT, holes);
  403. }
  404. #endif
  405. /* Unmap a kernel mapping if it exists. This is useful to avoid prefetches
  406. from the CPU leading to inconsistent cache lines. address and size
  407. must be aligned to 2MB boundaries.
  408. Does nothing when the mapping doesn't exist. */
  409. void __init clear_kernel_mapping(unsigned long address, unsigned long size)
  410. {
  411. unsigned long end = address + size;
  412. BUG_ON(address & ~LARGE_PAGE_MASK);
  413. BUG_ON(size & ~LARGE_PAGE_MASK);
  414. for (; address < end; address += LARGE_PAGE_SIZE) {
  415. pgd_t *pgd = pgd_offset_k(address);
  416. pud_t *pud;
  417. pmd_t *pmd;
  418. if (pgd_none(*pgd))
  419. continue;
  420. pud = pud_offset(pgd, address);
  421. if (pud_none(*pud))
  422. continue;
  423. pmd = pmd_offset(pud, address);
  424. if (!pmd || pmd_none(*pmd))
  425. continue;
  426. if (0 == (pmd_val(*pmd) & _PAGE_PSE)) {
  427. /* Could handle this, but it should not happen currently. */
  428. printk(KERN_ERR
  429. "clear_kernel_mapping: mapping has been split. will leak memory\n");
  430. pmd_ERROR(*pmd);
  431. }
  432. set_pmd(pmd, __pmd(0));
  433. }
  434. __flush_tlb_all();
  435. }
  436. /*
  437. * Memory hotplug specific functions
  438. */
  439. #if defined(CONFIG_ACPI_HOTPLUG_MEMORY) || defined(CONFIG_ACPI_HOTPLUG_MEMORY_MODULE)
  440. void online_page(struct page *page)
  441. {
  442. ClearPageReserved(page);
  443. init_page_count(page);
  444. __free_page(page);
  445. totalram_pages++;
  446. num_physpages++;
  447. }
  448. #ifndef CONFIG_MEMORY_HOTPLUG
  449. /*
  450. * Memory Hotadd without sparsemem. The mem_maps have been allocated in advance,
  451. * just online the pages.
  452. */
  453. int __add_pages(struct zone *z, unsigned long start_pfn, unsigned long nr_pages)
  454. {
  455. int err = -EIO;
  456. unsigned long pfn;
  457. unsigned long total = 0, mem = 0;
  458. for (pfn = start_pfn; pfn < start_pfn + nr_pages; pfn++) {
  459. if (pfn_valid(pfn)) {
  460. online_page(pfn_to_page(pfn));
  461. err = 0;
  462. mem++;
  463. }
  464. total++;
  465. }
  466. if (!err) {
  467. z->spanned_pages += total;
  468. z->present_pages += mem;
  469. z->zone_pgdat->node_spanned_pages += total;
  470. z->zone_pgdat->node_present_pages += mem;
  471. }
  472. return err;
  473. }
  474. #endif
  475. /*
  476. * Memory is added always to NORMAL zone. This means you will never get
  477. * additional DMA/DMA32 memory.
  478. */
  479. int add_memory(u64 start, u64 size)
  480. {
  481. struct pglist_data *pgdat = NODE_DATA(0);
  482. struct zone *zone = pgdat->node_zones + MAX_NR_ZONES-2;
  483. unsigned long start_pfn = start >> PAGE_SHIFT;
  484. unsigned long nr_pages = size >> PAGE_SHIFT;
  485. int ret;
  486. ret = __add_pages(zone, start_pfn, nr_pages);
  487. if (ret)
  488. goto error;
  489. init_memory_mapping(start, (start + size -1));
  490. return ret;
  491. error:
  492. printk("%s: Problem encountered in __add_pages!\n", __func__);
  493. return ret;
  494. }
  495. EXPORT_SYMBOL_GPL(add_memory);
  496. int remove_memory(u64 start, u64 size)
  497. {
  498. return -EINVAL;
  499. }
  500. EXPORT_SYMBOL_GPL(remove_memory);
  501. #endif
  502. static struct kcore_list kcore_mem, kcore_vmalloc, kcore_kernel, kcore_modules,
  503. kcore_vsyscall;
  504. void __init mem_init(void)
  505. {
  506. long codesize, reservedpages, datasize, initsize;
  507. #ifdef CONFIG_SWIOTLB
  508. pci_swiotlb_init();
  509. #endif
  510. no_iommu_init();
  511. /* How many end-of-memory variables you have, grandma! */
  512. max_low_pfn = end_pfn;
  513. max_pfn = end_pfn;
  514. num_physpages = end_pfn;
  515. high_memory = (void *) __va(end_pfn * PAGE_SIZE);
  516. /* clear the zero-page */
  517. memset(empty_zero_page, 0, PAGE_SIZE);
  518. reservedpages = 0;
  519. /* this will put all low memory onto the freelists */
  520. #ifdef CONFIG_NUMA
  521. totalram_pages = numa_free_all_bootmem();
  522. #else
  523. totalram_pages = free_all_bootmem();
  524. #endif
  525. reservedpages = end_pfn - totalram_pages - e820_hole_size(0, end_pfn);
  526. after_bootmem = 1;
  527. codesize = (unsigned long) &_etext - (unsigned long) &_text;
  528. datasize = (unsigned long) &_edata - (unsigned long) &_etext;
  529. initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
  530. /* Register memory areas for /proc/kcore */
  531. kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
  532. kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
  533. VMALLOC_END-VMALLOC_START);
  534. kclist_add(&kcore_kernel, &_stext, _end - _stext);
  535. kclist_add(&kcore_modules, (void *)MODULES_VADDR, MODULES_LEN);
  536. kclist_add(&kcore_vsyscall, (void *)VSYSCALL_START,
  537. VSYSCALL_END - VSYSCALL_START);
  538. printk("Memory: %luk/%luk available (%ldk kernel code, %ldk reserved, %ldk data, %ldk init)\n",
  539. (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
  540. end_pfn << (PAGE_SHIFT-10),
  541. codesize >> 10,
  542. reservedpages << (PAGE_SHIFT-10),
  543. datasize >> 10,
  544. initsize >> 10);
  545. #ifdef CONFIG_SMP
  546. /*
  547. * Sync boot_level4_pgt mappings with the init_level4_pgt
  548. * except for the low identity mappings which are already zapped
  549. * in init_level4_pgt. This sync-up is essential for AP's bringup
  550. */
  551. memcpy(boot_level4_pgt+1, init_level4_pgt+1, (PTRS_PER_PGD-1)*sizeof(pgd_t));
  552. #endif
  553. }
  554. void free_initmem(void)
  555. {
  556. unsigned long addr;
  557. addr = (unsigned long)(&__init_begin);
  558. for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) {
  559. ClearPageReserved(virt_to_page(addr));
  560. init_page_count(virt_to_page(addr));
  561. memset((void *)(addr & ~(PAGE_SIZE-1)), 0xcc, PAGE_SIZE);
  562. free_page(addr);
  563. totalram_pages++;
  564. }
  565. memset(__initdata_begin, 0xba, __initdata_end - __initdata_begin);
  566. printk ("Freeing unused kernel memory: %luk freed\n", (__init_end - __init_begin) >> 10);
  567. }
  568. #ifdef CONFIG_DEBUG_RODATA
  569. extern char __start_rodata, __end_rodata;
  570. void mark_rodata_ro(void)
  571. {
  572. unsigned long addr = (unsigned long)&__start_rodata;
  573. for (; addr < (unsigned long)&__end_rodata; addr += PAGE_SIZE)
  574. change_page_attr_addr(addr, 1, PAGE_KERNEL_RO);
  575. printk ("Write protecting the kernel read-only data: %luk\n",
  576. (&__end_rodata - &__start_rodata) >> 10);
  577. /*
  578. * change_page_attr_addr() requires a global_flush_tlb() call after it.
  579. * We do this after the printk so that if something went wrong in the
  580. * change, the printk gets out at least to give a better debug hint
  581. * of who is the culprit.
  582. */
  583. global_flush_tlb();
  584. }
  585. #endif
  586. #ifdef CONFIG_BLK_DEV_INITRD
  587. void free_initrd_mem(unsigned long start, unsigned long end)
  588. {
  589. if (start >= end)
  590. return;
  591. printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
  592. for (; start < end; start += PAGE_SIZE) {
  593. ClearPageReserved(virt_to_page(start));
  594. init_page_count(virt_to_page(start));
  595. free_page(start);
  596. totalram_pages++;
  597. }
  598. }
  599. #endif
  600. void __init reserve_bootmem_generic(unsigned long phys, unsigned len)
  601. {
  602. /* Should check here against the e820 map to avoid double free */
  603. #ifdef CONFIG_NUMA
  604. int nid = phys_to_nid(phys);
  605. reserve_bootmem_node(NODE_DATA(nid), phys, len);
  606. #else
  607. reserve_bootmem(phys, len);
  608. #endif
  609. if (phys+len <= MAX_DMA_PFN*PAGE_SIZE)
  610. dma_reserve += len / PAGE_SIZE;
  611. }
  612. int kern_addr_valid(unsigned long addr)
  613. {
  614. unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT;
  615. pgd_t *pgd;
  616. pud_t *pud;
  617. pmd_t *pmd;
  618. pte_t *pte;
  619. if (above != 0 && above != -1UL)
  620. return 0;
  621. pgd = pgd_offset_k(addr);
  622. if (pgd_none(*pgd))
  623. return 0;
  624. pud = pud_offset(pgd, addr);
  625. if (pud_none(*pud))
  626. return 0;
  627. pmd = pmd_offset(pud, addr);
  628. if (pmd_none(*pmd))
  629. return 0;
  630. if (pmd_large(*pmd))
  631. return pfn_valid(pmd_pfn(*pmd));
  632. pte = pte_offset_kernel(pmd, addr);
  633. if (pte_none(*pte))
  634. return 0;
  635. return pfn_valid(pte_pfn(*pte));
  636. }
  637. #ifdef CONFIG_SYSCTL
  638. #include <linux/sysctl.h>
  639. extern int exception_trace, page_fault_trace;
  640. static ctl_table debug_table2[] = {
  641. { 99, "exception-trace", &exception_trace, sizeof(int), 0644, NULL,
  642. proc_dointvec },
  643. { 0, }
  644. };
  645. static ctl_table debug_root_table2[] = {
  646. { .ctl_name = CTL_DEBUG, .procname = "debug", .mode = 0555,
  647. .child = debug_table2 },
  648. { 0 },
  649. };
  650. static __init int x8664_sysctl_init(void)
  651. {
  652. register_sysctl_table(debug_root_table2, 1);
  653. return 0;
  654. }
  655. __initcall(x8664_sysctl_init);
  656. #endif
  657. /* A pseudo VMAs to allow ptrace access for the vsyscall page. This only
  658. covers the 64bit vsyscall page now. 32bit has a real VMA now and does
  659. not need special handling anymore. */
  660. static struct vm_area_struct gate_vma = {
  661. .vm_start = VSYSCALL_START,
  662. .vm_end = VSYSCALL_END,
  663. .vm_page_prot = PAGE_READONLY
  664. };
  665. struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
  666. {
  667. #ifdef CONFIG_IA32_EMULATION
  668. if (test_tsk_thread_flag(tsk, TIF_IA32))
  669. return NULL;
  670. #endif
  671. return &gate_vma;
  672. }
  673. int in_gate_area(struct task_struct *task, unsigned long addr)
  674. {
  675. struct vm_area_struct *vma = get_gate_vma(task);
  676. if (!vma)
  677. return 0;
  678. return (addr >= vma->vm_start) && (addr < vma->vm_end);
  679. }
  680. /* Use this when you have no reliable task/vma, typically from interrupt
  681. * context. It is less reliable than using the task's vma and may give
  682. * false positives.
  683. */
  684. int in_gate_area_no_task(unsigned long addr)
  685. {
  686. return (addr >= VSYSCALL_START) && (addr < VSYSCALL_END);
  687. }