init_64.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  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/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. #include <asm/uv/uv.h>
  53. #include <asm/setup.h>
  54. #include "mm_internal.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 unsigned long __meminit
  278. phys_pte_init(pte_t *pte_page, unsigned long addr, unsigned long end,
  279. pgprot_t prot)
  280. {
  281. unsigned long pages = 0, next;
  282. unsigned long last_map_addr = end;
  283. int i;
  284. pte_t *pte = pte_page + pte_index(addr);
  285. for (i = pte_index(addr); i < PTRS_PER_PTE; i++, addr = next, pte++) {
  286. next = (addr & PAGE_MASK) + PAGE_SIZE;
  287. if (addr >= end) {
  288. if (!after_bootmem &&
  289. !e820_any_mapped(addr & PAGE_MASK, next, E820_RAM) &&
  290. !e820_any_mapped(addr & PAGE_MASK, next, E820_RESERVED_KERN))
  291. set_pte(pte, __pte(0));
  292. continue;
  293. }
  294. /*
  295. * We will re-use the existing mapping.
  296. * Xen for example has some special requirements, like mapping
  297. * pagetable pages as RO. So assume someone who pre-setup
  298. * these mappings are more intelligent.
  299. */
  300. if (pte_val(*pte)) {
  301. if (!after_bootmem)
  302. pages++;
  303. continue;
  304. }
  305. if (0)
  306. printk(" pte=%p addr=%lx pte=%016lx\n",
  307. pte, addr, pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL).pte);
  308. pages++;
  309. set_pte(pte, pfn_pte(addr >> PAGE_SHIFT, prot));
  310. last_map_addr = (addr & PAGE_MASK) + PAGE_SIZE;
  311. }
  312. update_page_count(PG_LEVEL_4K, pages);
  313. return last_map_addr;
  314. }
  315. static unsigned long __meminit
  316. phys_pmd_init(pmd_t *pmd_page, unsigned long address, unsigned long end,
  317. unsigned long page_size_mask, pgprot_t prot)
  318. {
  319. unsigned long pages = 0, next;
  320. unsigned long last_map_addr = end;
  321. int i = pmd_index(address);
  322. for (; i < PTRS_PER_PMD; i++, address = next) {
  323. pmd_t *pmd = pmd_page + pmd_index(address);
  324. pte_t *pte;
  325. pgprot_t new_prot = prot;
  326. next = (address & PMD_MASK) + PMD_SIZE;
  327. if (address >= end) {
  328. if (!after_bootmem &&
  329. !e820_any_mapped(address & PMD_MASK, next, E820_RAM) &&
  330. !e820_any_mapped(address & PMD_MASK, next, E820_RESERVED_KERN))
  331. set_pmd(pmd, __pmd(0));
  332. continue;
  333. }
  334. if (pmd_val(*pmd)) {
  335. if (!pmd_large(*pmd)) {
  336. spin_lock(&init_mm.page_table_lock);
  337. pte = (pte_t *)pmd_page_vaddr(*pmd);
  338. last_map_addr = phys_pte_init(pte, address,
  339. end, prot);
  340. spin_unlock(&init_mm.page_table_lock);
  341. continue;
  342. }
  343. /*
  344. * If we are ok with PG_LEVEL_2M mapping, then we will
  345. * use the existing mapping,
  346. *
  347. * Otherwise, we will split the large page mapping but
  348. * use the same existing protection bits except for
  349. * large page, so that we don't violate Intel's TLB
  350. * Application note (317080) which says, while changing
  351. * the page sizes, new and old translations should
  352. * not differ with respect to page frame and
  353. * attributes.
  354. */
  355. if (page_size_mask & (1 << PG_LEVEL_2M)) {
  356. if (!after_bootmem)
  357. pages++;
  358. last_map_addr = next;
  359. continue;
  360. }
  361. new_prot = pte_pgprot(pte_clrhuge(*(pte_t *)pmd));
  362. }
  363. if (page_size_mask & (1<<PG_LEVEL_2M)) {
  364. pages++;
  365. spin_lock(&init_mm.page_table_lock);
  366. set_pte((pte_t *)pmd,
  367. pfn_pte((address & PMD_MASK) >> PAGE_SHIFT,
  368. __pgprot(pgprot_val(prot) | _PAGE_PSE)));
  369. spin_unlock(&init_mm.page_table_lock);
  370. last_map_addr = next;
  371. continue;
  372. }
  373. pte = alloc_low_page();
  374. last_map_addr = phys_pte_init(pte, address, end, new_prot);
  375. spin_lock(&init_mm.page_table_lock);
  376. pmd_populate_kernel(&init_mm, pmd, pte);
  377. spin_unlock(&init_mm.page_table_lock);
  378. }
  379. update_page_count(PG_LEVEL_2M, pages);
  380. return last_map_addr;
  381. }
  382. static unsigned long __meminit
  383. phys_pud_init(pud_t *pud_page, unsigned long addr, unsigned long end,
  384. unsigned long page_size_mask)
  385. {
  386. unsigned long pages = 0, next;
  387. unsigned long last_map_addr = end;
  388. int i = pud_index(addr);
  389. for (; i < PTRS_PER_PUD; i++, addr = next) {
  390. pud_t *pud = pud_page + pud_index(addr);
  391. pmd_t *pmd;
  392. pgprot_t prot = PAGE_KERNEL;
  393. next = (addr & PUD_MASK) + PUD_SIZE;
  394. if (addr >= end) {
  395. if (!after_bootmem &&
  396. !e820_any_mapped(addr & PUD_MASK, next, E820_RAM) &&
  397. !e820_any_mapped(addr & PUD_MASK, next, E820_RESERVED_KERN))
  398. set_pud(pud, __pud(0));
  399. continue;
  400. }
  401. if (pud_val(*pud)) {
  402. if (!pud_large(*pud)) {
  403. pmd = pmd_offset(pud, 0);
  404. last_map_addr = phys_pmd_init(pmd, addr, end,
  405. page_size_mask, prot);
  406. __flush_tlb_all();
  407. continue;
  408. }
  409. /*
  410. * If we are ok with PG_LEVEL_1G mapping, then we will
  411. * use the existing mapping.
  412. *
  413. * Otherwise, we will split the gbpage mapping but use
  414. * the same existing protection bits except for large
  415. * page, so that we don't violate Intel's TLB
  416. * Application note (317080) which says, while changing
  417. * the page sizes, new and old translations should
  418. * not differ with respect to page frame and
  419. * attributes.
  420. */
  421. if (page_size_mask & (1 << PG_LEVEL_1G)) {
  422. if (!after_bootmem)
  423. pages++;
  424. last_map_addr = next;
  425. continue;
  426. }
  427. prot = pte_pgprot(pte_clrhuge(*(pte_t *)pud));
  428. }
  429. if (page_size_mask & (1<<PG_LEVEL_1G)) {
  430. pages++;
  431. spin_lock(&init_mm.page_table_lock);
  432. set_pte((pte_t *)pud,
  433. pfn_pte((addr & PUD_MASK) >> PAGE_SHIFT,
  434. PAGE_KERNEL_LARGE));
  435. spin_unlock(&init_mm.page_table_lock);
  436. last_map_addr = next;
  437. continue;
  438. }
  439. pmd = alloc_low_page();
  440. last_map_addr = phys_pmd_init(pmd, addr, end, page_size_mask,
  441. prot);
  442. spin_lock(&init_mm.page_table_lock);
  443. pud_populate(&init_mm, pud, pmd);
  444. spin_unlock(&init_mm.page_table_lock);
  445. }
  446. __flush_tlb_all();
  447. update_page_count(PG_LEVEL_1G, pages);
  448. return last_map_addr;
  449. }
  450. unsigned long __meminit
  451. kernel_physical_mapping_init(unsigned long start,
  452. unsigned long end,
  453. unsigned long page_size_mask)
  454. {
  455. bool pgd_changed = false;
  456. unsigned long next, last_map_addr = end;
  457. unsigned long addr;
  458. start = (unsigned long)__va(start);
  459. end = (unsigned long)__va(end);
  460. addr = start;
  461. for (; start < end; start = next) {
  462. pgd_t *pgd = pgd_offset_k(start);
  463. pud_t *pud;
  464. next = (start + PGDIR_SIZE) & PGDIR_MASK;
  465. if (next > end)
  466. next = end;
  467. if (pgd_val(*pgd)) {
  468. pud = (pud_t *)pgd_page_vaddr(*pgd);
  469. last_map_addr = phys_pud_init(pud, __pa(start),
  470. __pa(end), page_size_mask);
  471. continue;
  472. }
  473. pud = alloc_low_page();
  474. last_map_addr = phys_pud_init(pud, __pa(start), __pa(next),
  475. page_size_mask);
  476. spin_lock(&init_mm.page_table_lock);
  477. pgd_populate(&init_mm, pgd, pud);
  478. spin_unlock(&init_mm.page_table_lock);
  479. pgd_changed = true;
  480. }
  481. if (pgd_changed)
  482. sync_global_pgds(addr, end);
  483. __flush_tlb_all();
  484. return last_map_addr;
  485. }
  486. #ifndef CONFIG_NUMA
  487. void __init initmem_init(void)
  488. {
  489. memblock_set_node(0, (phys_addr_t)ULLONG_MAX, 0);
  490. }
  491. #endif
  492. void __init paging_init(void)
  493. {
  494. sparse_memory_present_with_active_regions(MAX_NUMNODES);
  495. sparse_init();
  496. /*
  497. * clear the default setting with node 0
  498. * note: don't use nodes_clear here, that is really clearing when
  499. * numa support is not compiled in, and later node_set_state
  500. * will not set it back.
  501. */
  502. node_clear_state(0, N_NORMAL_MEMORY);
  503. zone_sizes_init();
  504. }
  505. /*
  506. * Memory hotplug specific functions
  507. */
  508. #ifdef CONFIG_MEMORY_HOTPLUG
  509. /*
  510. * After memory hotplug the variables max_pfn, max_low_pfn and high_memory need
  511. * updating.
  512. */
  513. static void update_end_of_memory_vars(u64 start, u64 size)
  514. {
  515. unsigned long end_pfn = PFN_UP(start + size);
  516. if (end_pfn > max_pfn) {
  517. max_pfn = end_pfn;
  518. max_low_pfn = end_pfn;
  519. high_memory = (void *)__va(max_pfn * PAGE_SIZE - 1) + 1;
  520. }
  521. }
  522. /*
  523. * Memory is added always to NORMAL zone. This means you will never get
  524. * additional DMA/DMA32 memory.
  525. */
  526. int arch_add_memory(int nid, u64 start, u64 size)
  527. {
  528. struct pglist_data *pgdat = NODE_DATA(nid);
  529. struct zone *zone = pgdat->node_zones + ZONE_NORMAL;
  530. unsigned long start_pfn = start >> PAGE_SHIFT;
  531. unsigned long nr_pages = size >> PAGE_SHIFT;
  532. int ret;
  533. init_memory_mapping(start, start + size);
  534. ret = __add_pages(nid, zone, start_pfn, nr_pages);
  535. WARN_ON_ONCE(ret);
  536. /* update max_pfn, max_low_pfn and high_memory */
  537. update_end_of_memory_vars(start, size);
  538. return ret;
  539. }
  540. EXPORT_SYMBOL_GPL(arch_add_memory);
  541. #endif /* CONFIG_MEMORY_HOTPLUG */
  542. static struct kcore_list kcore_vsyscall;
  543. static void __init register_page_bootmem_info(void)
  544. {
  545. #ifdef CONFIG_NUMA
  546. int i;
  547. for_each_online_node(i)
  548. register_page_bootmem_info_node(NODE_DATA(i));
  549. #endif
  550. }
  551. void __init mem_init(void)
  552. {
  553. long codesize, reservedpages, datasize, initsize;
  554. unsigned long absent_pages;
  555. pci_iommu_alloc();
  556. /* clear_bss() already clear the empty_zero_page */
  557. reservedpages = 0;
  558. /* this will put all low memory onto the freelists */
  559. register_page_bootmem_info();
  560. totalram_pages = free_all_bootmem();
  561. absent_pages = absent_pages_in_range(0, max_pfn);
  562. reservedpages = max_pfn - totalram_pages - absent_pages;
  563. after_bootmem = 1;
  564. codesize = (unsigned long) &_etext - (unsigned long) &_text;
  565. datasize = (unsigned long) &_edata - (unsigned long) &_etext;
  566. initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
  567. /* Register memory areas for /proc/kcore */
  568. kclist_add(&kcore_vsyscall, (void *)VSYSCALL_START,
  569. VSYSCALL_END - VSYSCALL_START, KCORE_OTHER);
  570. printk(KERN_INFO "Memory: %luk/%luk available (%ldk kernel code, "
  571. "%ldk absent, %ldk reserved, %ldk data, %ldk init)\n",
  572. nr_free_pages() << (PAGE_SHIFT-10),
  573. max_pfn << (PAGE_SHIFT-10),
  574. codesize >> 10,
  575. absent_pages << (PAGE_SHIFT-10),
  576. reservedpages << (PAGE_SHIFT-10),
  577. datasize >> 10,
  578. initsize >> 10);
  579. }
  580. #ifdef CONFIG_DEBUG_RODATA
  581. const int rodata_test_data = 0xC3;
  582. EXPORT_SYMBOL_GPL(rodata_test_data);
  583. int kernel_set_to_readonly;
  584. void set_kernel_text_rw(void)
  585. {
  586. unsigned long start = PFN_ALIGN(_text);
  587. unsigned long end = PFN_ALIGN(__stop___ex_table);
  588. if (!kernel_set_to_readonly)
  589. return;
  590. pr_debug("Set kernel text: %lx - %lx for read write\n",
  591. start, end);
  592. /*
  593. * Make the kernel identity mapping for text RW. Kernel text
  594. * mapping will always be RO. Refer to the comment in
  595. * static_protections() in pageattr.c
  596. */
  597. set_memory_rw(start, (end - start) >> PAGE_SHIFT);
  598. }
  599. void set_kernel_text_ro(void)
  600. {
  601. unsigned long start = PFN_ALIGN(_text);
  602. unsigned long end = PFN_ALIGN(__stop___ex_table);
  603. if (!kernel_set_to_readonly)
  604. return;
  605. pr_debug("Set kernel text: %lx - %lx for read only\n",
  606. start, end);
  607. /*
  608. * Set the kernel identity mapping for text RO.
  609. */
  610. set_memory_ro(start, (end - start) >> PAGE_SHIFT);
  611. }
  612. void mark_rodata_ro(void)
  613. {
  614. unsigned long start = PFN_ALIGN(_text);
  615. unsigned long rodata_start =
  616. ((unsigned long)__start_rodata + PAGE_SIZE - 1) & PAGE_MASK;
  617. unsigned long end = (unsigned long) &__end_rodata_hpage_align;
  618. unsigned long text_end = PAGE_ALIGN((unsigned long) &__stop___ex_table);
  619. unsigned long rodata_end = PAGE_ALIGN((unsigned long) &__end_rodata);
  620. unsigned long data_start = (unsigned long) &_sdata;
  621. printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
  622. (end - start) >> 10);
  623. set_memory_ro(start, (end - start) >> PAGE_SHIFT);
  624. kernel_set_to_readonly = 1;
  625. /*
  626. * The rodata section (but not the kernel text!) should also be
  627. * not-executable.
  628. */
  629. set_memory_nx(rodata_start, (end - rodata_start) >> PAGE_SHIFT);
  630. rodata_test();
  631. #ifdef CONFIG_CPA_DEBUG
  632. printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, end);
  633. set_memory_rw(start, (end-start) >> PAGE_SHIFT);
  634. printk(KERN_INFO "Testing CPA: again\n");
  635. set_memory_ro(start, (end-start) >> PAGE_SHIFT);
  636. #endif
  637. free_init_pages("unused kernel memory",
  638. (unsigned long) page_address(virt_to_page(text_end)),
  639. (unsigned long)
  640. page_address(virt_to_page(rodata_start)));
  641. free_init_pages("unused kernel memory",
  642. (unsigned long) page_address(virt_to_page(rodata_end)),
  643. (unsigned long) page_address(virt_to_page(data_start)));
  644. }
  645. #endif
  646. int kern_addr_valid(unsigned long addr)
  647. {
  648. unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT;
  649. pgd_t *pgd;
  650. pud_t *pud;
  651. pmd_t *pmd;
  652. pte_t *pte;
  653. if (above != 0 && above != -1UL)
  654. return 0;
  655. pgd = pgd_offset_k(addr);
  656. if (pgd_none(*pgd))
  657. return 0;
  658. pud = pud_offset(pgd, addr);
  659. if (pud_none(*pud))
  660. return 0;
  661. pmd = pmd_offset(pud, addr);
  662. if (pmd_none(*pmd))
  663. return 0;
  664. if (pmd_large(*pmd))
  665. return pfn_valid(pmd_pfn(*pmd));
  666. pte = pte_offset_kernel(pmd, addr);
  667. if (pte_none(*pte))
  668. return 0;
  669. return pfn_valid(pte_pfn(*pte));
  670. }
  671. /*
  672. * A pseudo VMA to allow ptrace access for the vsyscall page. This only
  673. * covers the 64bit vsyscall page now. 32bit has a real VMA now and does
  674. * not need special handling anymore:
  675. */
  676. static struct vm_area_struct gate_vma = {
  677. .vm_start = VSYSCALL_START,
  678. .vm_end = VSYSCALL_START + (VSYSCALL_MAPPED_PAGES * PAGE_SIZE),
  679. .vm_page_prot = PAGE_READONLY_EXEC,
  680. .vm_flags = VM_READ | VM_EXEC
  681. };
  682. struct vm_area_struct *get_gate_vma(struct mm_struct *mm)
  683. {
  684. #ifdef CONFIG_IA32_EMULATION
  685. if (!mm || mm->context.ia32_compat)
  686. return NULL;
  687. #endif
  688. return &gate_vma;
  689. }
  690. int in_gate_area(struct mm_struct *mm, unsigned long addr)
  691. {
  692. struct vm_area_struct *vma = get_gate_vma(mm);
  693. if (!vma)
  694. return 0;
  695. return (addr >= vma->vm_start) && (addr < vma->vm_end);
  696. }
  697. /*
  698. * Use this when you have no reliable mm, typically from interrupt
  699. * context. It is less reliable than using a task's mm and may give
  700. * false positives.
  701. */
  702. int in_gate_area_no_mm(unsigned long addr)
  703. {
  704. return (addr >= VSYSCALL_START) && (addr < VSYSCALL_END);
  705. }
  706. const char *arch_vma_name(struct vm_area_struct *vma)
  707. {
  708. if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
  709. return "[vdso]";
  710. if (vma == &gate_vma)
  711. return "[vsyscall]";
  712. return NULL;
  713. }
  714. #ifdef CONFIG_X86_UV
  715. unsigned long memory_block_size_bytes(void)
  716. {
  717. if (is_uv_system()) {
  718. printk(KERN_INFO "UV: memory block size 2GB\n");
  719. return 2UL * 1024 * 1024 * 1024;
  720. }
  721. return MIN_MEMORY_BLOCK_SIZE;
  722. }
  723. #endif
  724. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  725. /*
  726. * Initialise the sparsemem vmemmap using huge-pages at the PMD level.
  727. */
  728. static long __meminitdata addr_start, addr_end;
  729. static void __meminitdata *p_start, *p_end;
  730. static int __meminitdata node_start;
  731. int __meminit
  732. vmemmap_populate(struct page *start_page, unsigned long size, int node)
  733. {
  734. unsigned long addr = (unsigned long)start_page;
  735. unsigned long end = (unsigned long)(start_page + size);
  736. unsigned long next;
  737. pgd_t *pgd;
  738. pud_t *pud;
  739. pmd_t *pmd;
  740. for (; addr < end; addr = next) {
  741. void *p = NULL;
  742. pgd = vmemmap_pgd_populate(addr, node);
  743. if (!pgd)
  744. return -ENOMEM;
  745. pud = vmemmap_pud_populate(pgd, addr, node);
  746. if (!pud)
  747. return -ENOMEM;
  748. if (!cpu_has_pse) {
  749. next = (addr + PAGE_SIZE) & PAGE_MASK;
  750. pmd = vmemmap_pmd_populate(pud, addr, node);
  751. if (!pmd)
  752. return -ENOMEM;
  753. p = vmemmap_pte_populate(pmd, addr, node);
  754. if (!p)
  755. return -ENOMEM;
  756. addr_end = addr + PAGE_SIZE;
  757. p_end = p + PAGE_SIZE;
  758. } else {
  759. next = pmd_addr_end(addr, end);
  760. pmd = pmd_offset(pud, addr);
  761. if (pmd_none(*pmd)) {
  762. pte_t entry;
  763. p = vmemmap_alloc_block_buf(PMD_SIZE, node);
  764. if (!p)
  765. return -ENOMEM;
  766. entry = pfn_pte(__pa(p) >> PAGE_SHIFT,
  767. PAGE_KERNEL_LARGE);
  768. set_pmd(pmd, __pmd(pte_val(entry)));
  769. /* check to see if we have contiguous blocks */
  770. if (p_end != p || node_start != node) {
  771. if (p_start)
  772. printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n",
  773. addr_start, addr_end-1, p_start, p_end-1, node_start);
  774. addr_start = addr;
  775. node_start = node;
  776. p_start = p;
  777. }
  778. addr_end = addr + PMD_SIZE;
  779. p_end = p + PMD_SIZE;
  780. } else
  781. vmemmap_verify((pte_t *)pmd, node, addr, next);
  782. }
  783. }
  784. sync_global_pgds((unsigned long)start_page, end);
  785. return 0;
  786. }
  787. void __meminit vmemmap_populate_print_last(void)
  788. {
  789. if (p_start) {
  790. printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n",
  791. addr_start, addr_end-1, p_start, p_end-1, node_start);
  792. p_start = NULL;
  793. p_end = NULL;
  794. node_start = 0;
  795. }
  796. }
  797. #endif