init_64.c 24 KB

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