init_64.c 24 KB

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