init_64.c 24 KB

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