init_64.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  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/initrd.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/pfn.h>
  26. #include <linux/poison.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/module.h>
  29. #include <linux/memory_hotplug.h>
  30. #include <linux/nmi.h>
  31. #include <asm/processor.h>
  32. #include <asm/bios_ebda.h>
  33. #include <asm/system.h>
  34. #include <asm/uaccess.h>
  35. #include <asm/pgtable.h>
  36. #include <asm/pgalloc.h>
  37. #include <asm/dma.h>
  38. #include <asm/fixmap.h>
  39. #include <asm/e820.h>
  40. #include <asm/apic.h>
  41. #include <asm/tlb.h>
  42. #include <asm/mmu_context.h>
  43. #include <asm/proto.h>
  44. #include <asm/smp.h>
  45. #include <asm/sections.h>
  46. #include <asm/kdebug.h>
  47. #include <asm/numa.h>
  48. #include <asm/cacheflush.h>
  49. /*
  50. * end_pfn only includes RAM, while max_pfn_mapped includes all e820 entries.
  51. * The direct mapping extends to max_pfn_mapped, so that we can directly access
  52. * apertures, ACPI and other tables without having to play with fixmaps.
  53. */
  54. unsigned long max_low_pfn_mapped;
  55. unsigned long max_pfn_mapped;
  56. static unsigned long dma_reserve __initdata;
  57. DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
  58. int direct_gbpages
  59. #ifdef CONFIG_DIRECT_GBPAGES
  60. = 1
  61. #endif
  62. ;
  63. static int __init parse_direct_gbpages_off(char *arg)
  64. {
  65. direct_gbpages = 0;
  66. return 0;
  67. }
  68. early_param("nogbpages", parse_direct_gbpages_off);
  69. static int __init parse_direct_gbpages_on(char *arg)
  70. {
  71. direct_gbpages = 1;
  72. return 0;
  73. }
  74. early_param("gbpages", parse_direct_gbpages_on);
  75. /*
  76. * NOTE: pagetable_init alloc all the fixmap pagetables contiguous on the
  77. * physical space so we can cache the place of the first one and move
  78. * around without checking the pgd every time.
  79. */
  80. int after_bootmem;
  81. pteval_t __supported_pte_mask __read_mostly = ~_PAGE_IOMAP;
  82. EXPORT_SYMBOL_GPL(__supported_pte_mask);
  83. static int do_not_nx __cpuinitdata;
  84. /*
  85. * noexec=on|off
  86. * Control non-executable mappings for 64-bit processes.
  87. *
  88. * on Enable (default)
  89. * off Disable
  90. */
  91. static int __init nonx_setup(char *str)
  92. {
  93. if (!str)
  94. return -EINVAL;
  95. if (!strncmp(str, "on", 2)) {
  96. __supported_pte_mask |= _PAGE_NX;
  97. do_not_nx = 0;
  98. } else if (!strncmp(str, "off", 3)) {
  99. do_not_nx = 1;
  100. __supported_pte_mask &= ~_PAGE_NX;
  101. }
  102. return 0;
  103. }
  104. early_param("noexec", nonx_setup);
  105. void __cpuinit check_efer(void)
  106. {
  107. unsigned long efer;
  108. rdmsrl(MSR_EFER, efer);
  109. if (!(efer & EFER_NX) || do_not_nx)
  110. __supported_pte_mask &= ~_PAGE_NX;
  111. }
  112. int force_personality32;
  113. /*
  114. * noexec32=on|off
  115. * Control non executable heap for 32bit processes.
  116. * To control the stack too use noexec=off
  117. *
  118. * on PROT_READ does not imply PROT_EXEC for 32-bit processes (default)
  119. * off PROT_READ implies PROT_EXEC
  120. */
  121. static int __init nonx32_setup(char *str)
  122. {
  123. if (!strcmp(str, "on"))
  124. force_personality32 &= ~READ_IMPLIES_EXEC;
  125. else if (!strcmp(str, "off"))
  126. force_personality32 |= READ_IMPLIES_EXEC;
  127. return 1;
  128. }
  129. __setup("noexec32=", nonx32_setup);
  130. /*
  131. * NOTE: This function is marked __ref because it calls __init function
  132. * (alloc_bootmem_pages). It's safe to do it ONLY when after_bootmem == 0.
  133. */
  134. static __ref void *spp_getpage(void)
  135. {
  136. void *ptr;
  137. if (after_bootmem)
  138. ptr = (void *) get_zeroed_page(GFP_ATOMIC);
  139. else
  140. ptr = alloc_bootmem_pages(PAGE_SIZE);
  141. if (!ptr || ((unsigned long)ptr & ~PAGE_MASK)) {
  142. panic("set_pte_phys: cannot allocate page data %s\n",
  143. after_bootmem ? "after bootmem" : "");
  144. }
  145. pr_debug("spp_getpage %p\n", ptr);
  146. return ptr;
  147. }
  148. void
  149. set_pte_vaddr_pud(pud_t *pud_page, unsigned long vaddr, pte_t new_pte)
  150. {
  151. pud_t *pud;
  152. pmd_t *pmd;
  153. pte_t *pte;
  154. pud = pud_page + pud_index(vaddr);
  155. if (pud_none(*pud)) {
  156. pmd = (pmd_t *) spp_getpage();
  157. pud_populate(&init_mm, pud, pmd);
  158. if (pmd != pmd_offset(pud, 0)) {
  159. printk(KERN_ERR "PAGETABLE BUG #01! %p <-> %p\n",
  160. pmd, pmd_offset(pud, 0));
  161. return;
  162. }
  163. }
  164. pmd = pmd_offset(pud, vaddr);
  165. if (pmd_none(*pmd)) {
  166. pte = (pte_t *) spp_getpage();
  167. pmd_populate_kernel(&init_mm, pmd, pte);
  168. if (pte != pte_offset_kernel(pmd, 0)) {
  169. printk(KERN_ERR "PAGETABLE BUG #02!\n");
  170. return;
  171. }
  172. }
  173. pte = pte_offset_kernel(pmd, vaddr);
  174. if (!pte_none(*pte) && pte_val(new_pte) &&
  175. pte_val(*pte) != (pte_val(new_pte) & __supported_pte_mask))
  176. pte_ERROR(*pte);
  177. set_pte(pte, new_pte);
  178. /*
  179. * It's enough to flush this one mapping.
  180. * (PGE mappings get flushed as well)
  181. */
  182. __flush_tlb_one(vaddr);
  183. }
  184. void
  185. set_pte_vaddr(unsigned long vaddr, pte_t pteval)
  186. {
  187. pgd_t *pgd;
  188. pud_t *pud_page;
  189. pr_debug("set_pte_vaddr %lx to %lx\n", vaddr, native_pte_val(pteval));
  190. pgd = pgd_offset_k(vaddr);
  191. if (pgd_none(*pgd)) {
  192. printk(KERN_ERR
  193. "PGD FIXMAP MISSING, it should be setup in head.S!\n");
  194. return;
  195. }
  196. pud_page = (pud_t*)pgd_page_vaddr(*pgd);
  197. set_pte_vaddr_pud(pud_page, vaddr, pteval);
  198. }
  199. /*
  200. * Create large page table mappings for a range of physical addresses.
  201. */
  202. static void __init __init_extra_mapping(unsigned long phys, unsigned long size,
  203. pgprot_t prot)
  204. {
  205. pgd_t *pgd;
  206. pud_t *pud;
  207. pmd_t *pmd;
  208. BUG_ON((phys & ~PMD_MASK) || (size & ~PMD_MASK));
  209. for (; size; phys += PMD_SIZE, size -= PMD_SIZE) {
  210. pgd = pgd_offset_k((unsigned long)__va(phys));
  211. if (pgd_none(*pgd)) {
  212. pud = (pud_t *) spp_getpage();
  213. set_pgd(pgd, __pgd(__pa(pud) | _KERNPG_TABLE |
  214. _PAGE_USER));
  215. }
  216. pud = pud_offset(pgd, (unsigned long)__va(phys));
  217. if (pud_none(*pud)) {
  218. pmd = (pmd_t *) spp_getpage();
  219. set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE |
  220. _PAGE_USER));
  221. }
  222. pmd = pmd_offset(pud, phys);
  223. BUG_ON(!pmd_none(*pmd));
  224. set_pmd(pmd, __pmd(phys | pgprot_val(prot)));
  225. }
  226. }
  227. void __init init_extra_mapping_wb(unsigned long phys, unsigned long size)
  228. {
  229. __init_extra_mapping(phys, size, PAGE_KERNEL_LARGE);
  230. }
  231. void __init init_extra_mapping_uc(unsigned long phys, unsigned long size)
  232. {
  233. __init_extra_mapping(phys, size, PAGE_KERNEL_LARGE_NOCACHE);
  234. }
  235. /*
  236. * The head.S code sets up the kernel high mapping:
  237. *
  238. * from __START_KERNEL_map to __START_KERNEL_map + size (== _end-_text)
  239. *
  240. * phys_addr holds the negative offset to the kernel, which is added
  241. * to the compile time generated pmds. This results in invalid pmds up
  242. * to the point where we hit the physaddr 0 mapping.
  243. *
  244. * We limit the mappings to the region from _text to _end. _end is
  245. * rounded up to the 2MB boundary. This catches the invalid pmds as
  246. * well, as they are located before _text:
  247. */
  248. void __init cleanup_highmap(void)
  249. {
  250. unsigned long vaddr = __START_KERNEL_map;
  251. unsigned long end = roundup((unsigned long)_end, PMD_SIZE) - 1;
  252. pmd_t *pmd = level2_kernel_pgt;
  253. pmd_t *last_pmd = pmd + PTRS_PER_PMD;
  254. for (; pmd < last_pmd; pmd++, vaddr += PMD_SIZE) {
  255. if (pmd_none(*pmd))
  256. continue;
  257. if (vaddr < (unsigned long) _text || vaddr > end)
  258. set_pmd(pmd, __pmd(0));
  259. }
  260. }
  261. static unsigned long __initdata table_start;
  262. static unsigned long __meminitdata table_end;
  263. static unsigned long __meminitdata table_top;
  264. static __ref void *alloc_low_page(unsigned long *phys)
  265. {
  266. unsigned long pfn = table_end++;
  267. void *adr;
  268. if (after_bootmem) {
  269. adr = (void *)get_zeroed_page(GFP_ATOMIC);
  270. *phys = __pa(adr);
  271. return adr;
  272. }
  273. if (pfn >= table_top)
  274. panic("alloc_low_page: ran out of memory");
  275. adr = early_memremap(pfn * PAGE_SIZE, PAGE_SIZE);
  276. memset(adr, 0, PAGE_SIZE);
  277. *phys = pfn * PAGE_SIZE;
  278. return adr;
  279. }
  280. static __ref void unmap_low_page(void *adr)
  281. {
  282. if (after_bootmem)
  283. return;
  284. early_iounmap(adr, PAGE_SIZE);
  285. }
  286. static unsigned long __meminit
  287. phys_pte_init(pte_t *pte_page, unsigned long addr, unsigned long end,
  288. pgprot_t prot)
  289. {
  290. unsigned pages = 0;
  291. unsigned long last_map_addr = end;
  292. int i;
  293. pte_t *pte = pte_page + pte_index(addr);
  294. for(i = pte_index(addr); i < PTRS_PER_PTE; i++, addr += PAGE_SIZE, pte++) {
  295. if (addr >= end) {
  296. if (!after_bootmem) {
  297. for(; i < PTRS_PER_PTE; i++, pte++)
  298. set_pte(pte, __pte(0));
  299. }
  300. break;
  301. }
  302. /*
  303. * We will re-use the existing mapping.
  304. * Xen for example has some special requirements, like mapping
  305. * pagetable pages as RO. So assume someone who pre-setup
  306. * these mappings are more intelligent.
  307. */
  308. if (pte_val(*pte))
  309. continue;
  310. if (0)
  311. printk(" pte=%p addr=%lx pte=%016lx\n",
  312. pte, addr, pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL).pte);
  313. pages++;
  314. set_pte(pte, pfn_pte(addr >> PAGE_SHIFT, prot));
  315. last_map_addr = (addr & PAGE_MASK) + PAGE_SIZE;
  316. }
  317. update_page_count(PG_LEVEL_4K, pages);
  318. return last_map_addr;
  319. }
  320. static unsigned long __meminit
  321. phys_pte_update(pmd_t *pmd, unsigned long address, unsigned long end,
  322. pgprot_t prot)
  323. {
  324. pte_t *pte = (pte_t *)pmd_page_vaddr(*pmd);
  325. return phys_pte_init(pte, address, end, prot);
  326. }
  327. static unsigned long __meminit
  328. phys_pmd_init(pmd_t *pmd_page, unsigned long address, unsigned long end,
  329. unsigned long page_size_mask, pgprot_t prot)
  330. {
  331. unsigned long pages = 0;
  332. unsigned long last_map_addr = end;
  333. int i = pmd_index(address);
  334. for (; i < PTRS_PER_PMD; i++, address += PMD_SIZE) {
  335. unsigned long pte_phys;
  336. pmd_t *pmd = pmd_page + pmd_index(address);
  337. pte_t *pte;
  338. pgprot_t new_prot = prot;
  339. if (address >= end) {
  340. if (!after_bootmem) {
  341. for (; i < PTRS_PER_PMD; i++, pmd++)
  342. set_pmd(pmd, __pmd(0));
  343. }
  344. break;
  345. }
  346. if (pmd_val(*pmd)) {
  347. if (!pmd_large(*pmd)) {
  348. spin_lock(&init_mm.page_table_lock);
  349. last_map_addr = phys_pte_update(pmd, address,
  350. end, prot);
  351. spin_unlock(&init_mm.page_table_lock);
  352. continue;
  353. }
  354. /*
  355. * If we are ok with PG_LEVEL_2M mapping, then we will
  356. * use the existing mapping,
  357. *
  358. * Otherwise, we will split the large page mapping but
  359. * use the same existing protection bits except for
  360. * large page, so that we don't violate Intel's TLB
  361. * Application note (317080) which says, while changing
  362. * the page sizes, new and old translations should
  363. * not differ with respect to page frame and
  364. * attributes.
  365. */
  366. if (page_size_mask & (1 << PG_LEVEL_2M))
  367. continue;
  368. new_prot = pte_pgprot(pte_clrhuge(*(pte_t *)pmd));
  369. }
  370. if (page_size_mask & (1<<PG_LEVEL_2M)) {
  371. pages++;
  372. spin_lock(&init_mm.page_table_lock);
  373. set_pte((pte_t *)pmd,
  374. pfn_pte(address >> PAGE_SHIFT,
  375. __pgprot(pgprot_val(prot) | _PAGE_PSE)));
  376. spin_unlock(&init_mm.page_table_lock);
  377. last_map_addr = (address & PMD_MASK) + PMD_SIZE;
  378. continue;
  379. }
  380. pte = alloc_low_page(&pte_phys);
  381. last_map_addr = phys_pte_init(pte, address, end, new_prot);
  382. unmap_low_page(pte);
  383. spin_lock(&init_mm.page_table_lock);
  384. pmd_populate_kernel(&init_mm, pmd, __va(pte_phys));
  385. spin_unlock(&init_mm.page_table_lock);
  386. }
  387. update_page_count(PG_LEVEL_2M, pages);
  388. return last_map_addr;
  389. }
  390. static unsigned long __meminit
  391. phys_pmd_update(pud_t *pud, unsigned long address, unsigned long end,
  392. unsigned long page_size_mask, pgprot_t prot)
  393. {
  394. pmd_t *pmd = pmd_offset(pud, 0);
  395. unsigned long last_map_addr;
  396. last_map_addr = phys_pmd_init(pmd, address, end, page_size_mask, prot);
  397. __flush_tlb_all();
  398. return last_map_addr;
  399. }
  400. static unsigned long __meminit
  401. phys_pud_init(pud_t *pud_page, unsigned long addr, unsigned long end,
  402. unsigned long page_size_mask)
  403. {
  404. unsigned long pages = 0;
  405. unsigned long last_map_addr = end;
  406. int i = pud_index(addr);
  407. for (; i < PTRS_PER_PUD; i++, addr = (addr & PUD_MASK) + PUD_SIZE) {
  408. unsigned long pmd_phys;
  409. pud_t *pud = pud_page + pud_index(addr);
  410. pmd_t *pmd;
  411. pgprot_t prot = PAGE_KERNEL;
  412. if (addr >= end)
  413. break;
  414. if (!after_bootmem &&
  415. !e820_any_mapped(addr, addr+PUD_SIZE, 0)) {
  416. set_pud(pud, __pud(0));
  417. continue;
  418. }
  419. if (pud_val(*pud)) {
  420. if (!pud_large(*pud)) {
  421. last_map_addr = phys_pmd_update(pud, addr, end,
  422. page_size_mask, prot);
  423. continue;
  424. }
  425. /*
  426. * If we are ok with PG_LEVEL_1G mapping, then we will
  427. * use the existing mapping.
  428. *
  429. * Otherwise, we will split the gbpage mapping but use
  430. * the same existing protection bits except for large
  431. * page, so that we don't violate Intel's TLB
  432. * Application note (317080) which says, while changing
  433. * the page sizes, new and old translations should
  434. * not differ with respect to page frame and
  435. * attributes.
  436. */
  437. if (page_size_mask & (1 << PG_LEVEL_1G))
  438. continue;
  439. prot = pte_pgprot(pte_clrhuge(*(pte_t *)pud));
  440. }
  441. if (page_size_mask & (1<<PG_LEVEL_1G)) {
  442. pages++;
  443. spin_lock(&init_mm.page_table_lock);
  444. set_pte((pte_t *)pud,
  445. pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL_LARGE));
  446. spin_unlock(&init_mm.page_table_lock);
  447. last_map_addr = (addr & PUD_MASK) + PUD_SIZE;
  448. continue;
  449. }
  450. pmd = alloc_low_page(&pmd_phys);
  451. last_map_addr = phys_pmd_init(pmd, addr, end, page_size_mask,
  452. prot);
  453. unmap_low_page(pmd);
  454. spin_lock(&init_mm.page_table_lock);
  455. pud_populate(&init_mm, pud, __va(pmd_phys));
  456. spin_unlock(&init_mm.page_table_lock);
  457. }
  458. __flush_tlb_all();
  459. update_page_count(PG_LEVEL_1G, pages);
  460. return last_map_addr;
  461. }
  462. static unsigned long __meminit
  463. phys_pud_update(pgd_t *pgd, unsigned long addr, unsigned long end,
  464. unsigned long page_size_mask)
  465. {
  466. pud_t *pud;
  467. pud = (pud_t *)pgd_page_vaddr(*pgd);
  468. return phys_pud_init(pud, addr, end, page_size_mask);
  469. }
  470. static void __init find_early_table_space(unsigned long end, int use_pse,
  471. int use_gbpages)
  472. {
  473. unsigned long puds, pmds, ptes, tables, start;
  474. puds = (end + PUD_SIZE - 1) >> PUD_SHIFT;
  475. tables = roundup(puds * sizeof(pud_t), PAGE_SIZE);
  476. if (use_gbpages) {
  477. unsigned long extra;
  478. extra = end - ((end>>PUD_SHIFT) << PUD_SHIFT);
  479. pmds = (extra + PMD_SIZE - 1) >> PMD_SHIFT;
  480. } else
  481. pmds = (end + PMD_SIZE - 1) >> PMD_SHIFT;
  482. tables += roundup(pmds * sizeof(pmd_t), PAGE_SIZE);
  483. if (use_pse) {
  484. unsigned long extra;
  485. extra = end - ((end>>PMD_SHIFT) << PMD_SHIFT);
  486. ptes = (extra + PAGE_SIZE - 1) >> PAGE_SHIFT;
  487. } else
  488. ptes = (end + PAGE_SIZE - 1) >> PAGE_SHIFT;
  489. tables += roundup(ptes * sizeof(pte_t), PAGE_SIZE);
  490. /*
  491. * RED-PEN putting page tables only on node 0 could
  492. * cause a hotspot and fill up ZONE_DMA. The page tables
  493. * need roughly 0.5KB per GB.
  494. */
  495. start = 0x8000;
  496. table_start = find_e820_area(start, end, tables, PAGE_SIZE);
  497. if (table_start == -1UL)
  498. panic("Cannot find space for the kernel page tables");
  499. table_start >>= PAGE_SHIFT;
  500. table_end = table_start;
  501. table_top = table_start + (tables >> PAGE_SHIFT);
  502. printk(KERN_DEBUG "kernel direct mapping tables up to %lx @ %lx-%lx\n",
  503. end, table_start << PAGE_SHIFT, table_top << PAGE_SHIFT);
  504. }
  505. static void __init init_gbpages(void)
  506. {
  507. if (direct_gbpages && cpu_has_gbpages)
  508. printk(KERN_INFO "Using GB pages for direct mapping\n");
  509. else
  510. direct_gbpages = 0;
  511. }
  512. static unsigned long __init kernel_physical_mapping_init(unsigned long start,
  513. unsigned long end,
  514. unsigned long page_size_mask)
  515. {
  516. unsigned long next, last_map_addr = end;
  517. start = (unsigned long)__va(start);
  518. end = (unsigned long)__va(end);
  519. for (; start < end; start = next) {
  520. pgd_t *pgd = pgd_offset_k(start);
  521. unsigned long pud_phys;
  522. pud_t *pud;
  523. next = (start + PGDIR_SIZE) & PGDIR_MASK;
  524. if (next > end)
  525. next = end;
  526. if (pgd_val(*pgd)) {
  527. last_map_addr = phys_pud_update(pgd, __pa(start),
  528. __pa(end), page_size_mask);
  529. continue;
  530. }
  531. pud = alloc_low_page(&pud_phys);
  532. last_map_addr = phys_pud_init(pud, __pa(start), __pa(next),
  533. page_size_mask);
  534. unmap_low_page(pud);
  535. spin_lock(&init_mm.page_table_lock);
  536. pgd_populate(&init_mm, pgd, __va(pud_phys));
  537. spin_unlock(&init_mm.page_table_lock);
  538. }
  539. __flush_tlb_all();
  540. return last_map_addr;
  541. }
  542. struct map_range {
  543. unsigned long start;
  544. unsigned long end;
  545. unsigned page_size_mask;
  546. };
  547. #define NR_RANGE_MR 5
  548. static int save_mr(struct map_range *mr, int nr_range,
  549. unsigned long start_pfn, unsigned long end_pfn,
  550. unsigned long page_size_mask)
  551. {
  552. if (start_pfn < end_pfn) {
  553. if (nr_range >= NR_RANGE_MR)
  554. panic("run out of range for init_memory_mapping\n");
  555. mr[nr_range].start = start_pfn<<PAGE_SHIFT;
  556. mr[nr_range].end = end_pfn<<PAGE_SHIFT;
  557. mr[nr_range].page_size_mask = page_size_mask;
  558. nr_range++;
  559. }
  560. return nr_range;
  561. }
  562. /*
  563. * Setup the direct mapping of the physical memory at PAGE_OFFSET.
  564. * This runs before bootmem is initialized and gets pages directly from
  565. * the physical memory. To access them they are temporarily mapped.
  566. */
  567. unsigned long __init_refok init_memory_mapping(unsigned long start,
  568. unsigned long end)
  569. {
  570. unsigned long last_map_addr = 0;
  571. unsigned long page_size_mask = 0;
  572. unsigned long start_pfn, end_pfn;
  573. struct map_range mr[NR_RANGE_MR];
  574. int nr_range, i;
  575. int use_pse, use_gbpages;
  576. printk(KERN_INFO "init_memory_mapping\n");
  577. /*
  578. * Find space for the kernel direct mapping tables.
  579. *
  580. * Later we should allocate these tables in the local node of the
  581. * memory mapped. Unfortunately this is done currently before the
  582. * nodes are discovered.
  583. */
  584. if (!after_bootmem)
  585. init_gbpages();
  586. #ifdef CONFIG_DEBUG_PAGEALLOC
  587. /*
  588. * For CONFIG_DEBUG_PAGEALLOC, identity mapping will use small pages.
  589. * This will simplify cpa(), which otherwise needs to support splitting
  590. * large pages into small in interrupt context, etc.
  591. */
  592. use_pse = use_gbpages = 0;
  593. #else
  594. use_pse = cpu_has_pse;
  595. use_gbpages = direct_gbpages;
  596. #endif
  597. if (use_gbpages)
  598. page_size_mask |= 1 << PG_LEVEL_1G;
  599. if (use_pse)
  600. page_size_mask |= 1 << PG_LEVEL_2M;
  601. memset(mr, 0, sizeof(mr));
  602. nr_range = 0;
  603. /* head if not big page alignment ?*/
  604. start_pfn = start >> PAGE_SHIFT;
  605. end_pfn = ((start + (PMD_SIZE - 1)) >> PMD_SHIFT)
  606. << (PMD_SHIFT - PAGE_SHIFT);
  607. nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
  608. /* big page (2M) range*/
  609. start_pfn = ((start + (PMD_SIZE - 1))>>PMD_SHIFT)
  610. << (PMD_SHIFT - PAGE_SHIFT);
  611. end_pfn = ((start + (PUD_SIZE - 1))>>PUD_SHIFT)
  612. << (PUD_SHIFT - PAGE_SHIFT);
  613. if (end_pfn > ((end>>PUD_SHIFT)<<(PUD_SHIFT - PAGE_SHIFT)))
  614. end_pfn = ((end>>PUD_SHIFT)<<(PUD_SHIFT - PAGE_SHIFT));
  615. nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
  616. page_size_mask & (1<<PG_LEVEL_2M));
  617. /* big page (1G) range */
  618. start_pfn = end_pfn;
  619. end_pfn = (end>>PUD_SHIFT) << (PUD_SHIFT - PAGE_SHIFT);
  620. nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
  621. page_size_mask &
  622. ((1<<PG_LEVEL_2M)|(1<<PG_LEVEL_1G)));
  623. /* tail is not big page (1G) alignment */
  624. start_pfn = end_pfn;
  625. end_pfn = (end>>PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
  626. nr_range = save_mr(mr, nr_range, start_pfn, end_pfn,
  627. page_size_mask & (1<<PG_LEVEL_2M));
  628. /* tail is not big page (2M) alignment */
  629. start_pfn = end_pfn;
  630. end_pfn = end>>PAGE_SHIFT;
  631. nr_range = save_mr(mr, nr_range, start_pfn, end_pfn, 0);
  632. /* try to merge same page size and continuous */
  633. for (i = 0; nr_range > 1 && i < nr_range - 1; i++) {
  634. unsigned long old_start;
  635. if (mr[i].end != mr[i+1].start ||
  636. mr[i].page_size_mask != mr[i+1].page_size_mask)
  637. continue;
  638. /* move it */
  639. old_start = mr[i].start;
  640. memmove(&mr[i], &mr[i+1],
  641. (nr_range - 1 - i) * sizeof (struct map_range));
  642. mr[i].start = old_start;
  643. nr_range--;
  644. }
  645. for (i = 0; i < nr_range; i++)
  646. printk(KERN_DEBUG " %010lx - %010lx page %s\n",
  647. mr[i].start, mr[i].end,
  648. (mr[i].page_size_mask & (1<<PG_LEVEL_1G))?"1G":(
  649. (mr[i].page_size_mask & (1<<PG_LEVEL_2M))?"2M":"4k"));
  650. if (!after_bootmem)
  651. find_early_table_space(end, use_pse, use_gbpages);
  652. for (i = 0; i < nr_range; i++)
  653. last_map_addr = kernel_physical_mapping_init(
  654. mr[i].start, mr[i].end,
  655. mr[i].page_size_mask);
  656. if (!after_bootmem)
  657. mmu_cr4_features = read_cr4();
  658. __flush_tlb_all();
  659. if (!after_bootmem && table_end > table_start)
  660. reserve_early(table_start << PAGE_SHIFT,
  661. table_end << PAGE_SHIFT, "PGTABLE");
  662. printk(KERN_INFO "last_map_addr: %lx end: %lx\n",
  663. last_map_addr, end);
  664. if (!after_bootmem)
  665. early_memtest(start, end);
  666. return last_map_addr >> PAGE_SHIFT;
  667. }
  668. #ifndef CONFIG_NUMA
  669. void __init initmem_init(unsigned long start_pfn, unsigned long end_pfn)
  670. {
  671. unsigned long bootmap_size, bootmap;
  672. bootmap_size = bootmem_bootmap_pages(end_pfn)<<PAGE_SHIFT;
  673. bootmap = find_e820_area(0, end_pfn<<PAGE_SHIFT, bootmap_size,
  674. PAGE_SIZE);
  675. if (bootmap == -1L)
  676. panic("Cannot find bootmem map of size %ld\n", bootmap_size);
  677. /* don't touch min_low_pfn */
  678. bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap >> PAGE_SHIFT,
  679. 0, end_pfn);
  680. e820_register_active_regions(0, start_pfn, end_pfn);
  681. free_bootmem_with_active_regions(0, end_pfn);
  682. early_res_to_bootmem(0, end_pfn<<PAGE_SHIFT);
  683. reserve_bootmem(bootmap, bootmap_size, BOOTMEM_DEFAULT);
  684. }
  685. void __init paging_init(void)
  686. {
  687. unsigned long max_zone_pfns[MAX_NR_ZONES];
  688. memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
  689. max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
  690. max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
  691. max_zone_pfns[ZONE_NORMAL] = max_pfn;
  692. memory_present(0, 0, max_pfn);
  693. sparse_init();
  694. free_area_init_nodes(max_zone_pfns);
  695. }
  696. #endif
  697. /*
  698. * Memory hotplug specific functions
  699. */
  700. #ifdef CONFIG_MEMORY_HOTPLUG
  701. /*
  702. * Memory is added always to NORMAL zone. This means you will never get
  703. * additional DMA/DMA32 memory.
  704. */
  705. int arch_add_memory(int nid, u64 start, u64 size)
  706. {
  707. struct pglist_data *pgdat = NODE_DATA(nid);
  708. struct zone *zone = pgdat->node_zones + ZONE_NORMAL;
  709. unsigned long last_mapped_pfn, start_pfn = start >> PAGE_SHIFT;
  710. unsigned long nr_pages = size >> PAGE_SHIFT;
  711. int ret;
  712. last_mapped_pfn = init_memory_mapping(start, start + size-1);
  713. if (last_mapped_pfn > max_pfn_mapped)
  714. max_pfn_mapped = last_mapped_pfn;
  715. ret = __add_pages(zone, start_pfn, nr_pages);
  716. WARN_ON(1);
  717. return ret;
  718. }
  719. EXPORT_SYMBOL_GPL(arch_add_memory);
  720. #if !defined(CONFIG_ACPI_NUMA) && defined(CONFIG_NUMA)
  721. int memory_add_physaddr_to_nid(u64 start)
  722. {
  723. return 0;
  724. }
  725. EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
  726. #endif
  727. #endif /* CONFIG_MEMORY_HOTPLUG */
  728. /*
  729. * devmem_is_allowed() checks to see if /dev/mem access to a certain address
  730. * is valid. The argument is a physical page number.
  731. *
  732. *
  733. * On x86, access has to be given to the first megabyte of ram because that area
  734. * contains bios code and data regions used by X and dosemu and similar apps.
  735. * Access has to be given to non-kernel-ram areas as well, these contain the PCI
  736. * mmio resources as well as potential bios/acpi data regions.
  737. */
  738. int devmem_is_allowed(unsigned long pagenr)
  739. {
  740. if (pagenr <= 256)
  741. return 1;
  742. if (!page_is_ram(pagenr))
  743. return 1;
  744. return 0;
  745. }
  746. static struct kcore_list kcore_mem, kcore_vmalloc, kcore_kernel,
  747. kcore_modules, kcore_vsyscall;
  748. void __init mem_init(void)
  749. {
  750. long codesize, reservedpages, datasize, initsize;
  751. start_periodic_check_for_corruption();
  752. pci_iommu_alloc();
  753. /* clear_bss() already clear the empty_zero_page */
  754. reservedpages = 0;
  755. /* this will put all low memory onto the freelists */
  756. #ifdef CONFIG_NUMA
  757. totalram_pages = numa_free_all_bootmem();
  758. #else
  759. totalram_pages = free_all_bootmem();
  760. #endif
  761. reservedpages = max_pfn - totalram_pages -
  762. absent_pages_in_range(0, max_pfn);
  763. after_bootmem = 1;
  764. codesize = (unsigned long) &_etext - (unsigned long) &_text;
  765. datasize = (unsigned long) &_edata - (unsigned long) &_etext;
  766. initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
  767. /* Register memory areas for /proc/kcore */
  768. kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
  769. kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
  770. VMALLOC_END-VMALLOC_START);
  771. kclist_add(&kcore_kernel, &_stext, _end - _stext);
  772. kclist_add(&kcore_modules, (void *)MODULES_VADDR, MODULES_LEN);
  773. kclist_add(&kcore_vsyscall, (void *)VSYSCALL_START,
  774. VSYSCALL_END - VSYSCALL_START);
  775. printk(KERN_INFO "Memory: %luk/%luk available (%ldk kernel code, "
  776. "%ldk reserved, %ldk data, %ldk init)\n",
  777. (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
  778. max_pfn << (PAGE_SHIFT-10),
  779. codesize >> 10,
  780. reservedpages << (PAGE_SHIFT-10),
  781. datasize >> 10,
  782. initsize >> 10);
  783. }
  784. void free_init_pages(char *what, unsigned long begin, unsigned long end)
  785. {
  786. unsigned long addr = begin;
  787. if (addr >= end)
  788. return;
  789. /*
  790. * If debugging page accesses then do not free this memory but
  791. * mark them not present - any buggy init-section access will
  792. * create a kernel page fault:
  793. */
  794. #ifdef CONFIG_DEBUG_PAGEALLOC
  795. printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n",
  796. begin, PAGE_ALIGN(end));
  797. set_memory_np(begin, (end - begin) >> PAGE_SHIFT);
  798. #else
  799. printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
  800. for (; addr < end; addr += PAGE_SIZE) {
  801. ClearPageReserved(virt_to_page(addr));
  802. init_page_count(virt_to_page(addr));
  803. memset((void *)(addr & ~(PAGE_SIZE-1)),
  804. POISON_FREE_INITMEM, PAGE_SIZE);
  805. free_page(addr);
  806. totalram_pages++;
  807. }
  808. #endif
  809. }
  810. void free_initmem(void)
  811. {
  812. free_init_pages("unused kernel memory",
  813. (unsigned long)(&__init_begin),
  814. (unsigned long)(&__init_end));
  815. }
  816. #ifdef CONFIG_DEBUG_RODATA
  817. const int rodata_test_data = 0xC3;
  818. EXPORT_SYMBOL_GPL(rodata_test_data);
  819. void mark_rodata_ro(void)
  820. {
  821. unsigned long start = PFN_ALIGN(_stext), end = PFN_ALIGN(__end_rodata);
  822. unsigned long rodata_start =
  823. ((unsigned long)__start_rodata + PAGE_SIZE - 1) & PAGE_MASK;
  824. #ifdef CONFIG_DYNAMIC_FTRACE
  825. /* Dynamic tracing modifies the kernel text section */
  826. start = rodata_start;
  827. #endif
  828. printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
  829. (end - start) >> 10);
  830. set_memory_ro(start, (end - start) >> PAGE_SHIFT);
  831. /*
  832. * The rodata section (but not the kernel text!) should also be
  833. * not-executable.
  834. */
  835. set_memory_nx(rodata_start, (end - rodata_start) >> PAGE_SHIFT);
  836. rodata_test();
  837. #ifdef CONFIG_CPA_DEBUG
  838. printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, end);
  839. set_memory_rw(start, (end-start) >> PAGE_SHIFT);
  840. printk(KERN_INFO "Testing CPA: again\n");
  841. set_memory_ro(start, (end-start) >> PAGE_SHIFT);
  842. #endif
  843. }
  844. #endif
  845. #ifdef CONFIG_BLK_DEV_INITRD
  846. void free_initrd_mem(unsigned long start, unsigned long end)
  847. {
  848. free_init_pages("initrd memory", start, end);
  849. }
  850. #endif
  851. int __init reserve_bootmem_generic(unsigned long phys, unsigned long len,
  852. int flags)
  853. {
  854. #ifdef CONFIG_NUMA
  855. int nid, next_nid;
  856. int ret;
  857. #endif
  858. unsigned long pfn = phys >> PAGE_SHIFT;
  859. if (pfn >= max_pfn) {
  860. /*
  861. * This can happen with kdump kernels when accessing
  862. * firmware tables:
  863. */
  864. if (pfn < max_pfn_mapped)
  865. return -EFAULT;
  866. printk(KERN_ERR "reserve_bootmem: illegal reserve %lx %lu\n",
  867. phys, len);
  868. return -EFAULT;
  869. }
  870. /* Should check here against the e820 map to avoid double free */
  871. #ifdef CONFIG_NUMA
  872. nid = phys_to_nid(phys);
  873. next_nid = phys_to_nid(phys + len - 1);
  874. if (nid == next_nid)
  875. ret = reserve_bootmem_node(NODE_DATA(nid), phys, len, flags);
  876. else
  877. ret = reserve_bootmem(phys, len, flags);
  878. if (ret != 0)
  879. return ret;
  880. #else
  881. reserve_bootmem(phys, len, BOOTMEM_DEFAULT);
  882. #endif
  883. if (phys+len <= MAX_DMA_PFN*PAGE_SIZE) {
  884. dma_reserve += len / PAGE_SIZE;
  885. set_dma_reserve(dma_reserve);
  886. }
  887. return 0;
  888. }
  889. int kern_addr_valid(unsigned long addr)
  890. {
  891. unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT;
  892. pgd_t *pgd;
  893. pud_t *pud;
  894. pmd_t *pmd;
  895. pte_t *pte;
  896. if (above != 0 && above != -1UL)
  897. return 0;
  898. pgd = pgd_offset_k(addr);
  899. if (pgd_none(*pgd))
  900. return 0;
  901. pud = pud_offset(pgd, addr);
  902. if (pud_none(*pud))
  903. return 0;
  904. pmd = pmd_offset(pud, addr);
  905. if (pmd_none(*pmd))
  906. return 0;
  907. if (pmd_large(*pmd))
  908. return pfn_valid(pmd_pfn(*pmd));
  909. pte = pte_offset_kernel(pmd, addr);
  910. if (pte_none(*pte))
  911. return 0;
  912. return pfn_valid(pte_pfn(*pte));
  913. }
  914. /*
  915. * A pseudo VMA to allow ptrace access for the vsyscall page. This only
  916. * covers the 64bit vsyscall page now. 32bit has a real VMA now and does
  917. * not need special handling anymore:
  918. */
  919. static struct vm_area_struct gate_vma = {
  920. .vm_start = VSYSCALL_START,
  921. .vm_end = VSYSCALL_START + (VSYSCALL_MAPPED_PAGES * PAGE_SIZE),
  922. .vm_page_prot = PAGE_READONLY_EXEC,
  923. .vm_flags = VM_READ | VM_EXEC
  924. };
  925. struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
  926. {
  927. #ifdef CONFIG_IA32_EMULATION
  928. if (test_tsk_thread_flag(tsk, TIF_IA32))
  929. return NULL;
  930. #endif
  931. return &gate_vma;
  932. }
  933. int in_gate_area(struct task_struct *task, unsigned long addr)
  934. {
  935. struct vm_area_struct *vma = get_gate_vma(task);
  936. if (!vma)
  937. return 0;
  938. return (addr >= vma->vm_start) && (addr < vma->vm_end);
  939. }
  940. /*
  941. * Use this when you have no reliable task/vma, typically from interrupt
  942. * context. It is less reliable than using the task's vma and may give
  943. * false positives:
  944. */
  945. int in_gate_area_no_task(unsigned long addr)
  946. {
  947. return (addr >= VSYSCALL_START) && (addr < VSYSCALL_END);
  948. }
  949. const char *arch_vma_name(struct vm_area_struct *vma)
  950. {
  951. if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
  952. return "[vdso]";
  953. if (vma == &gate_vma)
  954. return "[vsyscall]";
  955. return NULL;
  956. }
  957. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  958. /*
  959. * Initialise the sparsemem vmemmap using huge-pages at the PMD level.
  960. */
  961. static long __meminitdata addr_start, addr_end;
  962. static void __meminitdata *p_start, *p_end;
  963. static int __meminitdata node_start;
  964. int __meminit
  965. vmemmap_populate(struct page *start_page, unsigned long size, int node)
  966. {
  967. unsigned long addr = (unsigned long)start_page;
  968. unsigned long end = (unsigned long)(start_page + size);
  969. unsigned long next;
  970. pgd_t *pgd;
  971. pud_t *pud;
  972. pmd_t *pmd;
  973. for (; addr < end; addr = next) {
  974. void *p = NULL;
  975. pgd = vmemmap_pgd_populate(addr, node);
  976. if (!pgd)
  977. return -ENOMEM;
  978. pud = vmemmap_pud_populate(pgd, addr, node);
  979. if (!pud)
  980. return -ENOMEM;
  981. if (!cpu_has_pse) {
  982. next = (addr + PAGE_SIZE) & PAGE_MASK;
  983. pmd = vmemmap_pmd_populate(pud, addr, node);
  984. if (!pmd)
  985. return -ENOMEM;
  986. p = vmemmap_pte_populate(pmd, addr, node);
  987. if (!p)
  988. return -ENOMEM;
  989. addr_end = addr + PAGE_SIZE;
  990. p_end = p + PAGE_SIZE;
  991. } else {
  992. next = pmd_addr_end(addr, end);
  993. pmd = pmd_offset(pud, addr);
  994. if (pmd_none(*pmd)) {
  995. pte_t entry;
  996. p = vmemmap_alloc_block(PMD_SIZE, node);
  997. if (!p)
  998. return -ENOMEM;
  999. entry = pfn_pte(__pa(p) >> PAGE_SHIFT,
  1000. PAGE_KERNEL_LARGE);
  1001. set_pmd(pmd, __pmd(pte_val(entry)));
  1002. /* check to see if we have contiguous blocks */
  1003. if (p_end != p || node_start != node) {
  1004. if (p_start)
  1005. printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n",
  1006. addr_start, addr_end-1, p_start, p_end-1, node_start);
  1007. addr_start = addr;
  1008. node_start = node;
  1009. p_start = p;
  1010. }
  1011. addr_end = addr + PMD_SIZE;
  1012. p_end = p + PMD_SIZE;
  1013. } else
  1014. vmemmap_verify((pte_t *)pmd, node, addr, next);
  1015. }
  1016. }
  1017. return 0;
  1018. }
  1019. void __meminit vmemmap_populate_print_last(void)
  1020. {
  1021. if (p_start) {
  1022. printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n",
  1023. addr_start, addr_end-1, p_start, p_end-1, node_start);
  1024. p_start = NULL;
  1025. p_end = NULL;
  1026. node_start = 0;
  1027. }
  1028. }
  1029. #endif