init_64.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  1. /*
  2. * linux/arch/x86_64/mm/init.c
  3. *
  4. * Copyright (C) 1995 Linus Torvalds
  5. * Copyright (C) 2000 Pavel Machek <pavel@ucw.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 <linux/gfp.h>
  32. #include <asm/processor.h>
  33. #include <asm/bios_ebda.h>
  34. #include <asm/system.h>
  35. #include <asm/uaccess.h>
  36. #include <asm/pgtable.h>
  37. #include <asm/pgalloc.h>
  38. #include <asm/dma.h>
  39. #include <asm/fixmap.h>
  40. #include <asm/e820.h>
  41. #include <asm/apic.h>
  42. #include <asm/tlb.h>
  43. #include <asm/mmu_context.h>
  44. #include <asm/proto.h>
  45. #include <asm/smp.h>
  46. #include <asm/sections.h>
  47. #include <asm/kdebug.h>
  48. #include <asm/numa.h>
  49. #include <asm/cacheflush.h>
  50. #include <asm/init.h>
  51. #include <linux/bootmem.h>
  52. static unsigned long dma_reserve __initdata;
  53. static int __init parse_direct_gbpages_off(char *arg)
  54. {
  55. direct_gbpages = 0;
  56. return 0;
  57. }
  58. early_param("nogbpages", parse_direct_gbpages_off);
  59. static int __init parse_direct_gbpages_on(char *arg)
  60. {
  61. direct_gbpages = 1;
  62. return 0;
  63. }
  64. early_param("gbpages", parse_direct_gbpages_on);
  65. /*
  66. * NOTE: pagetable_init alloc all the fixmap pagetables contiguous on the
  67. * physical space so we can cache the place of the first one and move
  68. * around without checking the pgd every time.
  69. */
  70. pteval_t __supported_pte_mask __read_mostly = ~_PAGE_IOMAP;
  71. EXPORT_SYMBOL_GPL(__supported_pte_mask);
  72. int force_personality32;
  73. /*
  74. * noexec32=on|off
  75. * Control non executable heap for 32bit processes.
  76. * To control the stack too use noexec=off
  77. *
  78. * on PROT_READ does not imply PROT_EXEC for 32-bit processes (default)
  79. * off PROT_READ implies PROT_EXEC
  80. */
  81. static int __init nonx32_setup(char *str)
  82. {
  83. if (!strcmp(str, "on"))
  84. force_personality32 &= ~READ_IMPLIES_EXEC;
  85. else if (!strcmp(str, "off"))
  86. force_personality32 |= READ_IMPLIES_EXEC;
  87. return 1;
  88. }
  89. __setup("noexec32=", nonx32_setup);
  90. /*
  91. * When memory was added/removed make sure all the processes MM have
  92. * suitable PGD entries in the local PGD level page.
  93. */
  94. void sync_global_pgds(unsigned long start, unsigned long end)
  95. {
  96. unsigned long address;
  97. for (address = start; address <= end; address += PGDIR_SIZE) {
  98. const pgd_t *pgd_ref = pgd_offset_k(address);
  99. unsigned long flags;
  100. struct page *page;
  101. if (pgd_none(*pgd_ref))
  102. continue;
  103. spin_lock_irqsave(&pgd_lock, flags);
  104. list_for_each_entry(page, &pgd_list, lru) {
  105. pgd_t *pgd;
  106. spinlock_t *pgt_lock;
  107. pgd = (pgd_t *)page_address(page) + pgd_index(address);
  108. pgt_lock = &pgd_page_get_mm(page)->page_table_lock;
  109. spin_lock(pgt_lock);
  110. if (pgd_none(*pgd))
  111. set_pgd(pgd, *pgd_ref);
  112. else
  113. BUG_ON(pgd_page_vaddr(*pgd)
  114. != pgd_page_vaddr(*pgd_ref));
  115. spin_unlock(pgt_lock);
  116. }
  117. spin_unlock_irqrestore(&pgd_lock, flags);
  118. }
  119. }
  120. /*
  121. * NOTE: This function is marked __ref because it calls __init function
  122. * (alloc_bootmem_pages). It's safe to do it ONLY when after_bootmem == 0.
  123. */
  124. static __ref void *spp_getpage(void)
  125. {
  126. void *ptr;
  127. if (after_bootmem)
  128. ptr = (void *) get_zeroed_page(GFP_ATOMIC | __GFP_NOTRACK);
  129. else
  130. ptr = alloc_bootmem_pages(PAGE_SIZE);
  131. if (!ptr || ((unsigned long)ptr & ~PAGE_MASK)) {
  132. panic("set_pte_phys: cannot allocate page data %s\n",
  133. after_bootmem ? "after bootmem" : "");
  134. }
  135. pr_debug("spp_getpage %p\n", ptr);
  136. return ptr;
  137. }
  138. static pud_t *fill_pud(pgd_t *pgd, unsigned long vaddr)
  139. {
  140. if (pgd_none(*pgd)) {
  141. pud_t *pud = (pud_t *)spp_getpage();
  142. pgd_populate(&init_mm, pgd, pud);
  143. if (pud != pud_offset(pgd, 0))
  144. printk(KERN_ERR "PAGETABLE BUG #00! %p <-> %p\n",
  145. pud, pud_offset(pgd, 0));
  146. }
  147. return pud_offset(pgd, vaddr);
  148. }
  149. static pmd_t *fill_pmd(pud_t *pud, unsigned long vaddr)
  150. {
  151. if (pud_none(*pud)) {
  152. pmd_t *pmd = (pmd_t *) spp_getpage();
  153. pud_populate(&init_mm, pud, pmd);
  154. if (pmd != pmd_offset(pud, 0))
  155. printk(KERN_ERR "PAGETABLE BUG #01! %p <-> %p\n",
  156. pmd, pmd_offset(pud, 0));
  157. }
  158. return pmd_offset(pud, vaddr);
  159. }
  160. static pte_t *fill_pte(pmd_t *pmd, unsigned long vaddr)
  161. {
  162. if (pmd_none(*pmd)) {
  163. pte_t *pte = (pte_t *) spp_getpage();
  164. pmd_populate_kernel(&init_mm, pmd, pte);
  165. if (pte != pte_offset_kernel(pmd, 0))
  166. printk(KERN_ERR "PAGETABLE BUG #02!\n");
  167. }
  168. return pte_offset_kernel(pmd, vaddr);
  169. }
  170. void set_pte_vaddr_pud(pud_t *pud_page, unsigned long vaddr, pte_t new_pte)
  171. {
  172. pud_t *pud;
  173. pmd_t *pmd;
  174. pte_t *pte;
  175. pud = pud_page + pud_index(vaddr);
  176. pmd = fill_pmd(pud, vaddr);
  177. pte = fill_pte(pmd, vaddr);
  178. set_pte(pte, new_pte);
  179. /*
  180. * It's enough to flush this one mapping.
  181. * (PGE mappings get flushed as well)
  182. */
  183. __flush_tlb_one(vaddr);
  184. }
  185. void 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. pmd_t * __init populate_extra_pmd(unsigned long vaddr)
  200. {
  201. pgd_t *pgd;
  202. pud_t *pud;
  203. pgd = pgd_offset_k(vaddr);
  204. pud = fill_pud(pgd, vaddr);
  205. return fill_pmd(pud, vaddr);
  206. }
  207. pte_t * __init populate_extra_pte(unsigned long vaddr)
  208. {
  209. pmd_t *pmd;
  210. pmd = populate_extra_pmd(vaddr);
  211. return fill_pte(pmd, vaddr);
  212. }
  213. /*
  214. * Create large page table mappings for a range of physical addresses.
  215. */
  216. static void __init __init_extra_mapping(unsigned long phys, unsigned long size,
  217. pgprot_t prot)
  218. {
  219. pgd_t *pgd;
  220. pud_t *pud;
  221. pmd_t *pmd;
  222. BUG_ON((phys & ~PMD_MASK) || (size & ~PMD_MASK));
  223. for (; size; phys += PMD_SIZE, size -= PMD_SIZE) {
  224. pgd = pgd_offset_k((unsigned long)__va(phys));
  225. if (pgd_none(*pgd)) {
  226. pud = (pud_t *) spp_getpage();
  227. set_pgd(pgd, __pgd(__pa(pud) | _KERNPG_TABLE |
  228. _PAGE_USER));
  229. }
  230. pud = pud_offset(pgd, (unsigned long)__va(phys));
  231. if (pud_none(*pud)) {
  232. pmd = (pmd_t *) spp_getpage();
  233. set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE |
  234. _PAGE_USER));
  235. }
  236. pmd = pmd_offset(pud, phys);
  237. BUG_ON(!pmd_none(*pmd));
  238. set_pmd(pmd, __pmd(phys | pgprot_val(prot)));
  239. }
  240. }
  241. void __init init_extra_mapping_wb(unsigned long phys, unsigned long size)
  242. {
  243. __init_extra_mapping(phys, size, PAGE_KERNEL_LARGE);
  244. }
  245. void __init init_extra_mapping_uc(unsigned long phys, unsigned long size)
  246. {
  247. __init_extra_mapping(phys, size, PAGE_KERNEL_LARGE_NOCACHE);
  248. }
  249. /*
  250. * The head.S code sets up the kernel high mapping:
  251. *
  252. * from __START_KERNEL_map to __START_KERNEL_map + size (== _end-_text)
  253. *
  254. * phys_addr holds the negative offset to the kernel, which is added
  255. * to the compile time generated pmds. This results in invalid pmds up
  256. * to the point where we hit the physaddr 0 mapping.
  257. *
  258. * We limit the mappings to the region from _text to _end. _end is
  259. * rounded up to the 2MB boundary. This catches the invalid pmds as
  260. * well, as they are located before _text:
  261. */
  262. void __init cleanup_highmap(void)
  263. {
  264. unsigned long vaddr = __START_KERNEL_map;
  265. unsigned long end = roundup((unsigned long)_end, PMD_SIZE) - 1;
  266. pmd_t *pmd = level2_kernel_pgt;
  267. pmd_t *last_pmd = pmd + PTRS_PER_PMD;
  268. for (; pmd < last_pmd; pmd++, vaddr += PMD_SIZE) {
  269. if (pmd_none(*pmd))
  270. continue;
  271. if (vaddr < (unsigned long) _text || vaddr > end)
  272. set_pmd(pmd, __pmd(0));
  273. }
  274. }
  275. static __ref void *alloc_low_page(unsigned long *phys)
  276. {
  277. unsigned long pfn = e820_table_end++;
  278. void *adr;
  279. if (after_bootmem) {
  280. adr = (void *)get_zeroed_page(GFP_ATOMIC | __GFP_NOTRACK);
  281. *phys = __pa(adr);
  282. return adr;
  283. }
  284. if (pfn >= e820_table_top)
  285. panic("alloc_low_page: ran out of memory");
  286. adr = early_memremap(pfn * PAGE_SIZE, PAGE_SIZE);
  287. clear_page(adr);
  288. *phys = pfn * PAGE_SIZE;
  289. return adr;
  290. }
  291. static __ref void unmap_low_page(void *adr)
  292. {
  293. if (after_bootmem)
  294. return;
  295. early_iounmap(adr, PAGE_SIZE);
  296. }
  297. static unsigned long __meminit
  298. phys_pte_init(pte_t *pte_page, unsigned long addr, unsigned long end,
  299. pgprot_t prot)
  300. {
  301. unsigned pages = 0;
  302. unsigned long last_map_addr = end;
  303. int i;
  304. pte_t *pte = pte_page + pte_index(addr);
  305. for(i = pte_index(addr); i < PTRS_PER_PTE; i++, addr += PAGE_SIZE, pte++) {
  306. if (addr >= end) {
  307. if (!after_bootmem) {
  308. for(; i < PTRS_PER_PTE; i++, pte++)
  309. set_pte(pte, __pte(0));
  310. }
  311. break;
  312. }
  313. /*
  314. * We will re-use the existing mapping.
  315. * Xen for example has some special requirements, like mapping
  316. * pagetable pages as RO. So assume someone who pre-setup
  317. * these mappings are more intelligent.
  318. */
  319. if (pte_val(*pte)) {
  320. pages++;
  321. continue;
  322. }
  323. if (0)
  324. printk(" pte=%p addr=%lx pte=%016lx\n",
  325. pte, addr, pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL).pte);
  326. pages++;
  327. set_pte(pte, pfn_pte(addr >> PAGE_SHIFT, prot));
  328. last_map_addr = (addr & PAGE_MASK) + PAGE_SIZE;
  329. }
  330. update_page_count(PG_LEVEL_4K, pages);
  331. return last_map_addr;
  332. }
  333. static unsigned long __meminit
  334. phys_pte_update(pmd_t *pmd, unsigned long address, unsigned long end,
  335. pgprot_t prot)
  336. {
  337. pte_t *pte = (pte_t *)pmd_page_vaddr(*pmd);
  338. return phys_pte_init(pte, address, end, prot);
  339. }
  340. static unsigned long __meminit
  341. phys_pmd_init(pmd_t *pmd_page, unsigned long address, unsigned long end,
  342. unsigned long page_size_mask, pgprot_t prot)
  343. {
  344. unsigned long pages = 0;
  345. unsigned long last_map_addr = end;
  346. int i = pmd_index(address);
  347. for (; i < PTRS_PER_PMD; i++, address += PMD_SIZE) {
  348. unsigned long pte_phys;
  349. pmd_t *pmd = pmd_page + pmd_index(address);
  350. pte_t *pte;
  351. pgprot_t new_prot = prot;
  352. if (address >= end) {
  353. if (!after_bootmem) {
  354. for (; i < PTRS_PER_PMD; i++, pmd++)
  355. set_pmd(pmd, __pmd(0));
  356. }
  357. break;
  358. }
  359. if (pmd_val(*pmd)) {
  360. if (!pmd_large(*pmd)) {
  361. spin_lock(&init_mm.page_table_lock);
  362. last_map_addr = phys_pte_update(pmd, address,
  363. end, prot);
  364. spin_unlock(&init_mm.page_table_lock);
  365. continue;
  366. }
  367. /*
  368. * If we are ok with PG_LEVEL_2M mapping, then we will
  369. * use the existing mapping,
  370. *
  371. * Otherwise, we will split the large page mapping but
  372. * use the same existing protection bits except for
  373. * large page, so that we don't violate Intel's TLB
  374. * Application note (317080) which says, while changing
  375. * the page sizes, new and old translations should
  376. * not differ with respect to page frame and
  377. * attributes.
  378. */
  379. if (page_size_mask & (1 << PG_LEVEL_2M)) {
  380. pages++;
  381. continue;
  382. }
  383. new_prot = pte_pgprot(pte_clrhuge(*(pte_t *)pmd));
  384. }
  385. if (page_size_mask & (1<<PG_LEVEL_2M)) {
  386. pages++;
  387. spin_lock(&init_mm.page_table_lock);
  388. set_pte((pte_t *)pmd,
  389. pfn_pte(address >> PAGE_SHIFT,
  390. __pgprot(pgprot_val(prot) | _PAGE_PSE)));
  391. spin_unlock(&init_mm.page_table_lock);
  392. last_map_addr = (address & PMD_MASK) + PMD_SIZE;
  393. continue;
  394. }
  395. pte = alloc_low_page(&pte_phys);
  396. last_map_addr = phys_pte_init(pte, address, end, new_prot);
  397. unmap_low_page(pte);
  398. spin_lock(&init_mm.page_table_lock);
  399. pmd_populate_kernel(&init_mm, pmd, __va(pte_phys));
  400. spin_unlock(&init_mm.page_table_lock);
  401. }
  402. update_page_count(PG_LEVEL_2M, pages);
  403. return last_map_addr;
  404. }
  405. static unsigned long __meminit
  406. phys_pmd_update(pud_t *pud, unsigned long address, unsigned long end,
  407. unsigned long page_size_mask, pgprot_t prot)
  408. {
  409. pmd_t *pmd = pmd_offset(pud, 0);
  410. unsigned long last_map_addr;
  411. last_map_addr = phys_pmd_init(pmd, address, end, page_size_mask, prot);
  412. __flush_tlb_all();
  413. return last_map_addr;
  414. }
  415. static unsigned long __meminit
  416. phys_pud_init(pud_t *pud_page, unsigned long addr, unsigned long end,
  417. unsigned long page_size_mask)
  418. {
  419. unsigned long pages = 0;
  420. unsigned long last_map_addr = end;
  421. int i = pud_index(addr);
  422. for (; i < PTRS_PER_PUD; i++, addr = (addr & PUD_MASK) + PUD_SIZE) {
  423. unsigned long pmd_phys;
  424. pud_t *pud = pud_page + pud_index(addr);
  425. pmd_t *pmd;
  426. pgprot_t prot = PAGE_KERNEL;
  427. if (addr >= end)
  428. break;
  429. if (!after_bootmem &&
  430. !e820_any_mapped(addr, addr+PUD_SIZE, 0)) {
  431. set_pud(pud, __pud(0));
  432. continue;
  433. }
  434. if (pud_val(*pud)) {
  435. if (!pud_large(*pud)) {
  436. last_map_addr = phys_pmd_update(pud, addr, end,
  437. page_size_mask, prot);
  438. continue;
  439. }
  440. /*
  441. * If we are ok with PG_LEVEL_1G mapping, then we will
  442. * use the existing mapping.
  443. *
  444. * Otherwise, we will split the gbpage mapping but use
  445. * the same existing protection bits except for large
  446. * page, so that we don't violate Intel's TLB
  447. * Application note (317080) which says, while changing
  448. * the page sizes, new and old translations should
  449. * not differ with respect to page frame and
  450. * attributes.
  451. */
  452. if (page_size_mask & (1 << PG_LEVEL_1G)) {
  453. pages++;
  454. continue;
  455. }
  456. prot = pte_pgprot(pte_clrhuge(*(pte_t *)pud));
  457. }
  458. if (page_size_mask & (1<<PG_LEVEL_1G)) {
  459. pages++;
  460. spin_lock(&init_mm.page_table_lock);
  461. set_pte((pte_t *)pud,
  462. pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL_LARGE));
  463. spin_unlock(&init_mm.page_table_lock);
  464. last_map_addr = (addr & PUD_MASK) + PUD_SIZE;
  465. continue;
  466. }
  467. pmd = alloc_low_page(&pmd_phys);
  468. last_map_addr = phys_pmd_init(pmd, addr, end, page_size_mask,
  469. prot);
  470. unmap_low_page(pmd);
  471. spin_lock(&init_mm.page_table_lock);
  472. pud_populate(&init_mm, pud, __va(pmd_phys));
  473. spin_unlock(&init_mm.page_table_lock);
  474. }
  475. __flush_tlb_all();
  476. update_page_count(PG_LEVEL_1G, pages);
  477. return last_map_addr;
  478. }
  479. static unsigned long __meminit
  480. phys_pud_update(pgd_t *pgd, unsigned long addr, unsigned long end,
  481. unsigned long page_size_mask)
  482. {
  483. pud_t *pud;
  484. pud = (pud_t *)pgd_page_vaddr(*pgd);
  485. return phys_pud_init(pud, addr, end, page_size_mask);
  486. }
  487. unsigned long __meminit
  488. kernel_physical_mapping_init(unsigned long start,
  489. unsigned long end,
  490. unsigned long page_size_mask)
  491. {
  492. bool pgd_changed = false;
  493. unsigned long next, last_map_addr = end;
  494. unsigned long addr;
  495. start = (unsigned long)__va(start);
  496. end = (unsigned long)__va(end);
  497. addr = start;
  498. for (; start < end; start = next) {
  499. pgd_t *pgd = pgd_offset_k(start);
  500. unsigned long pud_phys;
  501. pud_t *pud;
  502. next = (start + PGDIR_SIZE) & PGDIR_MASK;
  503. if (next > end)
  504. next = end;
  505. if (pgd_val(*pgd)) {
  506. last_map_addr = phys_pud_update(pgd, __pa(start),
  507. __pa(end), page_size_mask);
  508. continue;
  509. }
  510. pud = alloc_low_page(&pud_phys);
  511. last_map_addr = phys_pud_init(pud, __pa(start), __pa(next),
  512. page_size_mask);
  513. unmap_low_page(pud);
  514. spin_lock(&init_mm.page_table_lock);
  515. pgd_populate(&init_mm, pgd, __va(pud_phys));
  516. spin_unlock(&init_mm.page_table_lock);
  517. pgd_changed = true;
  518. }
  519. if (pgd_changed)
  520. sync_global_pgds(addr, end);
  521. __flush_tlb_all();
  522. return last_map_addr;
  523. }
  524. #ifndef CONFIG_NUMA
  525. void __init initmem_init(unsigned long start_pfn, unsigned long end_pfn,
  526. int acpi, int k8)
  527. {
  528. #ifndef CONFIG_NO_BOOTMEM
  529. unsigned long bootmap_size, bootmap;
  530. bootmap_size = bootmem_bootmap_pages(end_pfn)<<PAGE_SHIFT;
  531. bootmap = find_e820_area(0, end_pfn<<PAGE_SHIFT, bootmap_size,
  532. PAGE_SIZE);
  533. if (bootmap == -1L)
  534. panic("Cannot find bootmem map of size %ld\n", bootmap_size);
  535. reserve_early(bootmap, bootmap + bootmap_size, "BOOTMAP");
  536. /* don't touch min_low_pfn */
  537. bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap >> PAGE_SHIFT,
  538. 0, end_pfn);
  539. e820_register_active_regions(0, start_pfn, end_pfn);
  540. free_bootmem_with_active_regions(0, end_pfn);
  541. #else
  542. e820_register_active_regions(0, start_pfn, end_pfn);
  543. #endif
  544. }
  545. #endif
  546. void __init paging_init(void)
  547. {
  548. unsigned long max_zone_pfns[MAX_NR_ZONES];
  549. memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
  550. max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
  551. max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
  552. max_zone_pfns[ZONE_NORMAL] = max_pfn;
  553. sparse_memory_present_with_active_regions(MAX_NUMNODES);
  554. sparse_init();
  555. /*
  556. * clear the default setting with node 0
  557. * note: don't use nodes_clear here, that is really clearing when
  558. * numa support is not compiled in, and later node_set_state
  559. * will not set it back.
  560. */
  561. node_clear_state(0, N_NORMAL_MEMORY);
  562. free_area_init_nodes(max_zone_pfns);
  563. }
  564. /*
  565. * Memory hotplug specific functions
  566. */
  567. #ifdef CONFIG_MEMORY_HOTPLUG
  568. /*
  569. * After memory hotplug the variables max_pfn, max_low_pfn and high_memory need
  570. * updating.
  571. */
  572. static void update_end_of_memory_vars(u64 start, u64 size)
  573. {
  574. unsigned long end_pfn = PFN_UP(start + size);
  575. if (end_pfn > max_pfn) {
  576. max_pfn = end_pfn;
  577. max_low_pfn = end_pfn;
  578. high_memory = (void *)__va(max_pfn * PAGE_SIZE - 1) + 1;
  579. }
  580. }
  581. /*
  582. * Memory is added always to NORMAL zone. This means you will never get
  583. * additional DMA/DMA32 memory.
  584. */
  585. int arch_add_memory(int nid, u64 start, u64 size)
  586. {
  587. struct pglist_data *pgdat = NODE_DATA(nid);
  588. struct zone *zone = pgdat->node_zones + ZONE_NORMAL;
  589. unsigned long last_mapped_pfn, start_pfn = start >> PAGE_SHIFT;
  590. unsigned long nr_pages = size >> PAGE_SHIFT;
  591. int ret;
  592. last_mapped_pfn = init_memory_mapping(start, start + size);
  593. if (last_mapped_pfn > max_pfn_mapped)
  594. max_pfn_mapped = last_mapped_pfn;
  595. ret = __add_pages(nid, zone, start_pfn, nr_pages);
  596. WARN_ON_ONCE(ret);
  597. /* update max_pfn, max_low_pfn and high_memory */
  598. update_end_of_memory_vars(start, size);
  599. return ret;
  600. }
  601. EXPORT_SYMBOL_GPL(arch_add_memory);
  602. #if !defined(CONFIG_ACPI_NUMA) && defined(CONFIG_NUMA)
  603. int memory_add_physaddr_to_nid(u64 start)
  604. {
  605. return 0;
  606. }
  607. EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
  608. #endif
  609. #endif /* CONFIG_MEMORY_HOTPLUG */
  610. static struct kcore_list kcore_vsyscall;
  611. void __init mem_init(void)
  612. {
  613. long codesize, reservedpages, datasize, initsize;
  614. unsigned long absent_pages;
  615. pci_iommu_alloc();
  616. /* clear_bss() already clear the empty_zero_page */
  617. reservedpages = 0;
  618. /* this will put all low memory onto the freelists */
  619. #ifdef CONFIG_NUMA
  620. totalram_pages = numa_free_all_bootmem();
  621. #else
  622. totalram_pages = free_all_bootmem();
  623. #endif
  624. absent_pages = absent_pages_in_range(0, max_pfn);
  625. reservedpages = max_pfn - totalram_pages - absent_pages;
  626. after_bootmem = 1;
  627. codesize = (unsigned long) &_etext - (unsigned long) &_text;
  628. datasize = (unsigned long) &_edata - (unsigned long) &_etext;
  629. initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
  630. /* Register memory areas for /proc/kcore */
  631. kclist_add(&kcore_vsyscall, (void *)VSYSCALL_START,
  632. VSYSCALL_END - VSYSCALL_START, KCORE_OTHER);
  633. printk(KERN_INFO "Memory: %luk/%luk available (%ldk kernel code, "
  634. "%ldk absent, %ldk reserved, %ldk data, %ldk init)\n",
  635. nr_free_pages() << (PAGE_SHIFT-10),
  636. max_pfn << (PAGE_SHIFT-10),
  637. codesize >> 10,
  638. absent_pages << (PAGE_SHIFT-10),
  639. reservedpages << (PAGE_SHIFT-10),
  640. datasize >> 10,
  641. initsize >> 10);
  642. }
  643. #ifdef CONFIG_DEBUG_RODATA
  644. const int rodata_test_data = 0xC3;
  645. EXPORT_SYMBOL_GPL(rodata_test_data);
  646. int kernel_set_to_readonly;
  647. void set_kernel_text_rw(void)
  648. {
  649. unsigned long start = PFN_ALIGN(_text);
  650. unsigned long end = PFN_ALIGN(__stop___ex_table);
  651. if (!kernel_set_to_readonly)
  652. return;
  653. pr_debug("Set kernel text: %lx - %lx for read write\n",
  654. start, end);
  655. /*
  656. * Make the kernel identity mapping for text RW. Kernel text
  657. * mapping will always be RO. Refer to the comment in
  658. * static_protections() in pageattr.c
  659. */
  660. set_memory_rw(start, (end - start) >> PAGE_SHIFT);
  661. }
  662. void set_kernel_text_ro(void)
  663. {
  664. unsigned long start = PFN_ALIGN(_text);
  665. unsigned long end = PFN_ALIGN(__stop___ex_table);
  666. if (!kernel_set_to_readonly)
  667. return;
  668. pr_debug("Set kernel text: %lx - %lx for read only\n",
  669. start, end);
  670. /*
  671. * Set the kernel identity mapping for text RO.
  672. */
  673. set_memory_ro(start, (end - start) >> PAGE_SHIFT);
  674. }
  675. void mark_rodata_ro(void)
  676. {
  677. unsigned long start = PFN_ALIGN(_text);
  678. unsigned long rodata_start =
  679. ((unsigned long)__start_rodata + PAGE_SIZE - 1) & PAGE_MASK;
  680. unsigned long end = (unsigned long) &__end_rodata_hpage_align;
  681. unsigned long text_end = PAGE_ALIGN((unsigned long) &__stop___ex_table);
  682. unsigned long rodata_end = PAGE_ALIGN((unsigned long) &__end_rodata);
  683. unsigned long data_start = (unsigned long) &_sdata;
  684. printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
  685. (end - start) >> 10);
  686. set_memory_ro(start, (end - start) >> PAGE_SHIFT);
  687. kernel_set_to_readonly = 1;
  688. /*
  689. * The rodata section (but not the kernel text!) should also be
  690. * not-executable.
  691. */
  692. set_memory_nx(rodata_start, (end - rodata_start) >> PAGE_SHIFT);
  693. rodata_test();
  694. #ifdef CONFIG_CPA_DEBUG
  695. printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, end);
  696. set_memory_rw(start, (end-start) >> PAGE_SHIFT);
  697. printk(KERN_INFO "Testing CPA: again\n");
  698. set_memory_ro(start, (end-start) >> PAGE_SHIFT);
  699. #endif
  700. free_init_pages("unused kernel memory",
  701. (unsigned long) page_address(virt_to_page(text_end)),
  702. (unsigned long)
  703. page_address(virt_to_page(rodata_start)));
  704. free_init_pages("unused kernel memory",
  705. (unsigned long) page_address(virt_to_page(rodata_end)),
  706. (unsigned long) page_address(virt_to_page(data_start)));
  707. }
  708. #endif
  709. int __init reserve_bootmem_generic(unsigned long phys, unsigned long len,
  710. int flags)
  711. {
  712. #ifdef CONFIG_NUMA
  713. int nid, next_nid;
  714. int ret;
  715. #endif
  716. unsigned long pfn = phys >> PAGE_SHIFT;
  717. if (pfn >= max_pfn) {
  718. /*
  719. * This can happen with kdump kernels when accessing
  720. * firmware tables:
  721. */
  722. if (pfn < max_pfn_mapped)
  723. return -EFAULT;
  724. printk(KERN_ERR "reserve_bootmem: illegal reserve %lx %lu\n",
  725. phys, len);
  726. return -EFAULT;
  727. }
  728. /* Should check here against the e820 map to avoid double free */
  729. #ifdef CONFIG_NUMA
  730. nid = phys_to_nid(phys);
  731. next_nid = phys_to_nid(phys + len - 1);
  732. if (nid == next_nid)
  733. ret = reserve_bootmem_node(NODE_DATA(nid), phys, len, flags);
  734. else
  735. ret = reserve_bootmem(phys, len, flags);
  736. if (ret != 0)
  737. return ret;
  738. #else
  739. reserve_bootmem(phys, len, flags);
  740. #endif
  741. if (phys+len <= MAX_DMA_PFN*PAGE_SIZE) {
  742. dma_reserve += len / PAGE_SIZE;
  743. set_dma_reserve(dma_reserve);
  744. }
  745. return 0;
  746. }
  747. int kern_addr_valid(unsigned long addr)
  748. {
  749. unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT;
  750. pgd_t *pgd;
  751. pud_t *pud;
  752. pmd_t *pmd;
  753. pte_t *pte;
  754. if (above != 0 && above != -1UL)
  755. return 0;
  756. pgd = pgd_offset_k(addr);
  757. if (pgd_none(*pgd))
  758. return 0;
  759. pud = pud_offset(pgd, addr);
  760. if (pud_none(*pud))
  761. return 0;
  762. pmd = pmd_offset(pud, addr);
  763. if (pmd_none(*pmd))
  764. return 0;
  765. if (pmd_large(*pmd))
  766. return pfn_valid(pmd_pfn(*pmd));
  767. pte = pte_offset_kernel(pmd, addr);
  768. if (pte_none(*pte))
  769. return 0;
  770. return pfn_valid(pte_pfn(*pte));
  771. }
  772. /*
  773. * A pseudo VMA to allow ptrace access for the vsyscall page. This only
  774. * covers the 64bit vsyscall page now. 32bit has a real VMA now and does
  775. * not need special handling anymore:
  776. */
  777. static struct vm_area_struct gate_vma = {
  778. .vm_start = VSYSCALL_START,
  779. .vm_end = VSYSCALL_START + (VSYSCALL_MAPPED_PAGES * PAGE_SIZE),
  780. .vm_page_prot = PAGE_READONLY_EXEC,
  781. .vm_flags = VM_READ | VM_EXEC
  782. };
  783. struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
  784. {
  785. #ifdef CONFIG_IA32_EMULATION
  786. if (test_tsk_thread_flag(tsk, TIF_IA32))
  787. return NULL;
  788. #endif
  789. return &gate_vma;
  790. }
  791. int in_gate_area(struct task_struct *task, unsigned long addr)
  792. {
  793. struct vm_area_struct *vma = get_gate_vma(task);
  794. if (!vma)
  795. return 0;
  796. return (addr >= vma->vm_start) && (addr < vma->vm_end);
  797. }
  798. /*
  799. * Use this when you have no reliable task/vma, typically from interrupt
  800. * context. It is less reliable than using the task's vma and may give
  801. * false positives:
  802. */
  803. int in_gate_area_no_task(unsigned long addr)
  804. {
  805. return (addr >= VSYSCALL_START) && (addr < VSYSCALL_END);
  806. }
  807. const char *arch_vma_name(struct vm_area_struct *vma)
  808. {
  809. if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
  810. return "[vdso]";
  811. if (vma == &gate_vma)
  812. return "[vsyscall]";
  813. return NULL;
  814. }
  815. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  816. /*
  817. * Initialise the sparsemem vmemmap using huge-pages at the PMD level.
  818. */
  819. static long __meminitdata addr_start, addr_end;
  820. static void __meminitdata *p_start, *p_end;
  821. static int __meminitdata node_start;
  822. int __meminit
  823. vmemmap_populate(struct page *start_page, unsigned long size, int node)
  824. {
  825. unsigned long addr = (unsigned long)start_page;
  826. unsigned long end = (unsigned long)(start_page + size);
  827. unsigned long next;
  828. pgd_t *pgd;
  829. pud_t *pud;
  830. pmd_t *pmd;
  831. for (; addr < end; addr = next) {
  832. void *p = NULL;
  833. pgd = vmemmap_pgd_populate(addr, node);
  834. if (!pgd)
  835. return -ENOMEM;
  836. pud = vmemmap_pud_populate(pgd, addr, node);
  837. if (!pud)
  838. return -ENOMEM;
  839. if (!cpu_has_pse) {
  840. next = (addr + PAGE_SIZE) & PAGE_MASK;
  841. pmd = vmemmap_pmd_populate(pud, addr, node);
  842. if (!pmd)
  843. return -ENOMEM;
  844. p = vmemmap_pte_populate(pmd, addr, node);
  845. if (!p)
  846. return -ENOMEM;
  847. addr_end = addr + PAGE_SIZE;
  848. p_end = p + PAGE_SIZE;
  849. } else {
  850. next = pmd_addr_end(addr, end);
  851. pmd = pmd_offset(pud, addr);
  852. if (pmd_none(*pmd)) {
  853. pte_t entry;
  854. p = vmemmap_alloc_block_buf(PMD_SIZE, node);
  855. if (!p)
  856. return -ENOMEM;
  857. entry = pfn_pte(__pa(p) >> PAGE_SHIFT,
  858. PAGE_KERNEL_LARGE);
  859. set_pmd(pmd, __pmd(pte_val(entry)));
  860. /* check to see if we have contiguous blocks */
  861. if (p_end != p || node_start != node) {
  862. if (p_start)
  863. printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n",
  864. addr_start, addr_end-1, p_start, p_end-1, node_start);
  865. addr_start = addr;
  866. node_start = node;
  867. p_start = p;
  868. }
  869. addr_end = addr + PMD_SIZE;
  870. p_end = p + PMD_SIZE;
  871. } else
  872. vmemmap_verify((pte_t *)pmd, node, addr, next);
  873. }
  874. }
  875. sync_global_pgds((unsigned long)start_page, end);
  876. return 0;
  877. }
  878. void __meminit vmemmap_populate_print_last(void)
  879. {
  880. if (p_start) {
  881. printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n",
  882. addr_start, addr_end-1, p_start, p_end-1, node_start);
  883. p_start = NULL;
  884. p_end = NULL;
  885. node_start = 0;
  886. }
  887. }
  888. #endif