init_64.c 30 KB

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