init_32.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  1. /*
  2. *
  3. * Copyright (C) 1995 Linus Torvalds
  4. *
  5. * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999
  6. */
  7. #include <linux/module.h>
  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/hugetlb.h>
  18. #include <linux/swap.h>
  19. #include <linux/smp.h>
  20. #include <linux/init.h>
  21. #include <linux/highmem.h>
  22. #include <linux/pagemap.h>
  23. #include <linux/pfn.h>
  24. #include <linux/poison.h>
  25. #include <linux/bootmem.h>
  26. #include <linux/slab.h>
  27. #include <linux/proc_fs.h>
  28. #include <linux/memory_hotplug.h>
  29. #include <linux/initrd.h>
  30. #include <linux/cpumask.h>
  31. #include <asm/asm.h>
  32. #include <asm/bios_ebda.h>
  33. #include <asm/processor.h>
  34. #include <asm/system.h>
  35. #include <asm/uaccess.h>
  36. #include <asm/pgtable.h>
  37. #include <asm/dma.h>
  38. #include <asm/fixmap.h>
  39. #include <asm/e820.h>
  40. #include <asm/apic.h>
  41. #include <asm/bugs.h>
  42. #include <asm/tlb.h>
  43. #include <asm/tlbflush.h>
  44. #include <asm/pgalloc.h>
  45. #include <asm/sections.h>
  46. #include <asm/paravirt.h>
  47. #include <asm/setup.h>
  48. #include <asm/cacheflush.h>
  49. #include <asm/smp.h>
  50. unsigned int __VMALLOC_RESERVE = 128 << 20;
  51. unsigned long max_low_pfn_mapped;
  52. unsigned long max_pfn_mapped;
  53. DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
  54. unsigned long highstart_pfn, highend_pfn;
  55. static noinline int do_test_wp_bit(void);
  56. static unsigned long __initdata table_start;
  57. static unsigned long __meminitdata table_end;
  58. static unsigned long __meminitdata table_top;
  59. static int __initdata after_init_bootmem;
  60. static __init void *alloc_low_page(unsigned long *phys)
  61. {
  62. unsigned long pfn = table_end++;
  63. void *adr;
  64. if (pfn >= table_top)
  65. panic("alloc_low_page: ran out of memory");
  66. adr = __va(pfn * PAGE_SIZE);
  67. memset(adr, 0, PAGE_SIZE);
  68. *phys = pfn * PAGE_SIZE;
  69. return adr;
  70. }
  71. /*
  72. * Creates a middle page table and puts a pointer to it in the
  73. * given global directory entry. This only returns the gd entry
  74. * in non-PAE compilation mode, since the middle layer is folded.
  75. */
  76. static pmd_t * __init one_md_table_init(pgd_t *pgd)
  77. {
  78. pud_t *pud;
  79. pmd_t *pmd_table;
  80. #ifdef CONFIG_X86_PAE
  81. unsigned long phys;
  82. if (!(pgd_val(*pgd) & _PAGE_PRESENT)) {
  83. if (after_init_bootmem)
  84. pmd_table = (pmd_t *)alloc_bootmem_low_pages(PAGE_SIZE);
  85. else
  86. pmd_table = (pmd_t *)alloc_low_page(&phys);
  87. paravirt_alloc_pmd(&init_mm, __pa(pmd_table) >> PAGE_SHIFT);
  88. set_pgd(pgd, __pgd(__pa(pmd_table) | _PAGE_PRESENT));
  89. pud = pud_offset(pgd, 0);
  90. BUG_ON(pmd_table != pmd_offset(pud, 0));
  91. return pmd_table;
  92. }
  93. #endif
  94. pud = pud_offset(pgd, 0);
  95. pmd_table = pmd_offset(pud, 0);
  96. return pmd_table;
  97. }
  98. /*
  99. * Create a page table and place a pointer to it in a middle page
  100. * directory entry:
  101. */
  102. static pte_t * __init one_page_table_init(pmd_t *pmd)
  103. {
  104. if (!(pmd_val(*pmd) & _PAGE_PRESENT)) {
  105. pte_t *page_table = NULL;
  106. if (after_init_bootmem) {
  107. #ifdef CONFIG_DEBUG_PAGEALLOC
  108. page_table = (pte_t *) alloc_bootmem_pages(PAGE_SIZE);
  109. #endif
  110. if (!page_table)
  111. page_table =
  112. (pte_t *)alloc_bootmem_low_pages(PAGE_SIZE);
  113. } else {
  114. unsigned long phys;
  115. page_table = (pte_t *)alloc_low_page(&phys);
  116. }
  117. paravirt_alloc_pte(&init_mm, __pa(page_table) >> PAGE_SHIFT);
  118. set_pmd(pmd, __pmd(__pa(page_table) | _PAGE_TABLE));
  119. BUG_ON(page_table != pte_offset_kernel(pmd, 0));
  120. }
  121. return pte_offset_kernel(pmd, 0);
  122. }
  123. /*
  124. * This function initializes a certain range of kernel virtual memory
  125. * with new bootmem page tables, everywhere page tables are missing in
  126. * the given range.
  127. *
  128. * NOTE: The pagetables are allocated contiguous on the physical space
  129. * so we can cache the place of the first one and move around without
  130. * checking the pgd every time.
  131. */
  132. static void __init
  133. page_table_range_init(unsigned long start, unsigned long end, pgd_t *pgd_base)
  134. {
  135. int pgd_idx, pmd_idx;
  136. unsigned long vaddr;
  137. pgd_t *pgd;
  138. pmd_t *pmd;
  139. vaddr = start;
  140. pgd_idx = pgd_index(vaddr);
  141. pmd_idx = pmd_index(vaddr);
  142. pgd = pgd_base + pgd_idx;
  143. for ( ; (pgd_idx < PTRS_PER_PGD) && (vaddr != end); pgd++, pgd_idx++) {
  144. pmd = one_md_table_init(pgd);
  145. pmd = pmd + pmd_index(vaddr);
  146. for (; (pmd_idx < PTRS_PER_PMD) && (vaddr != end);
  147. pmd++, pmd_idx++) {
  148. one_page_table_init(pmd);
  149. vaddr += PMD_SIZE;
  150. }
  151. pmd_idx = 0;
  152. }
  153. }
  154. static inline int is_kernel_text(unsigned long addr)
  155. {
  156. if (addr >= PAGE_OFFSET && addr <= (unsigned long)__init_end)
  157. return 1;
  158. return 0;
  159. }
  160. /*
  161. * This maps the physical memory to kernel virtual address space, a total
  162. * of max_low_pfn pages, by creating page tables starting from address
  163. * PAGE_OFFSET:
  164. */
  165. static void __init kernel_physical_mapping_init(pgd_t *pgd_base,
  166. unsigned long start_pfn,
  167. unsigned long end_pfn,
  168. int use_pse)
  169. {
  170. int pgd_idx, pmd_idx, pte_ofs;
  171. unsigned long pfn;
  172. pgd_t *pgd;
  173. pmd_t *pmd;
  174. pte_t *pte;
  175. unsigned pages_2m, pages_4k;
  176. int mapping_iter;
  177. /*
  178. * First iteration will setup identity mapping using large/small pages
  179. * based on use_pse, with other attributes same as set by
  180. * the early code in head_32.S
  181. *
  182. * Second iteration will setup the appropriate attributes (NX, GLOBAL..)
  183. * as desired for the kernel identity mapping.
  184. *
  185. * This two pass mechanism conforms to the TLB app note which says:
  186. *
  187. * "Software should not write to a paging-structure entry in a way
  188. * that would change, for any linear address, both the page size
  189. * and either the page frame or attributes."
  190. */
  191. mapping_iter = 1;
  192. if (!cpu_has_pse)
  193. use_pse = 0;
  194. repeat:
  195. pages_2m = pages_4k = 0;
  196. pfn = start_pfn;
  197. pgd_idx = pgd_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
  198. pgd = pgd_base + pgd_idx;
  199. for (; pgd_idx < PTRS_PER_PGD; pgd++, pgd_idx++) {
  200. pmd = one_md_table_init(pgd);
  201. if (pfn >= end_pfn)
  202. continue;
  203. #ifdef CONFIG_X86_PAE
  204. pmd_idx = pmd_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
  205. pmd += pmd_idx;
  206. #else
  207. pmd_idx = 0;
  208. #endif
  209. for (; pmd_idx < PTRS_PER_PMD && pfn < end_pfn;
  210. pmd++, pmd_idx++) {
  211. unsigned int addr = pfn * PAGE_SIZE + PAGE_OFFSET;
  212. /*
  213. * Map with big pages if possible, otherwise
  214. * create normal page tables:
  215. */
  216. if (use_pse) {
  217. unsigned int addr2;
  218. pgprot_t prot = PAGE_KERNEL_LARGE;
  219. /*
  220. * first pass will use the same initial
  221. * identity mapping attribute + _PAGE_PSE.
  222. */
  223. pgprot_t init_prot =
  224. __pgprot(PTE_IDENT_ATTR |
  225. _PAGE_PSE);
  226. addr2 = (pfn + PTRS_PER_PTE-1) * PAGE_SIZE +
  227. PAGE_OFFSET + PAGE_SIZE-1;
  228. if (is_kernel_text(addr) ||
  229. is_kernel_text(addr2))
  230. prot = PAGE_KERNEL_LARGE_EXEC;
  231. pages_2m++;
  232. if (mapping_iter == 1)
  233. set_pmd(pmd, pfn_pmd(pfn, init_prot));
  234. else
  235. set_pmd(pmd, pfn_pmd(pfn, prot));
  236. pfn += PTRS_PER_PTE;
  237. continue;
  238. }
  239. pte = one_page_table_init(pmd);
  240. pte_ofs = pte_index((pfn<<PAGE_SHIFT) + PAGE_OFFSET);
  241. pte += pte_ofs;
  242. for (; pte_ofs < PTRS_PER_PTE && pfn < end_pfn;
  243. pte++, pfn++, pte_ofs++, addr += PAGE_SIZE) {
  244. pgprot_t prot = PAGE_KERNEL;
  245. /*
  246. * first pass will use the same initial
  247. * identity mapping attribute.
  248. */
  249. pgprot_t init_prot = __pgprot(PTE_IDENT_ATTR);
  250. if (is_kernel_text(addr))
  251. prot = PAGE_KERNEL_EXEC;
  252. pages_4k++;
  253. if (mapping_iter == 1)
  254. set_pte(pte, pfn_pte(pfn, init_prot));
  255. else
  256. set_pte(pte, pfn_pte(pfn, prot));
  257. }
  258. }
  259. }
  260. if (mapping_iter == 1) {
  261. /*
  262. * update direct mapping page count only in the first
  263. * iteration.
  264. */
  265. update_page_count(PG_LEVEL_2M, pages_2m);
  266. update_page_count(PG_LEVEL_4K, pages_4k);
  267. /*
  268. * local global flush tlb, which will flush the previous
  269. * mappings present in both small and large page TLB's.
  270. */
  271. __flush_tlb_all();
  272. /*
  273. * Second iteration will set the actual desired PTE attributes.
  274. */
  275. mapping_iter = 2;
  276. goto repeat;
  277. }
  278. }
  279. /*
  280. * devmem_is_allowed() checks to see if /dev/mem access to a certain address
  281. * is valid. The argument is a physical page number.
  282. *
  283. *
  284. * On x86, access has to be given to the first megabyte of ram because that area
  285. * contains bios code and data regions used by X and dosemu and similar apps.
  286. * Access has to be given to non-kernel-ram areas as well, these contain the PCI
  287. * mmio resources as well as potential bios/acpi data regions.
  288. */
  289. int devmem_is_allowed(unsigned long pagenr)
  290. {
  291. if (pagenr <= 256)
  292. return 1;
  293. if (!page_is_ram(pagenr))
  294. return 1;
  295. return 0;
  296. }
  297. pte_t *kmap_pte;
  298. pgprot_t kmap_prot;
  299. static inline pte_t *kmap_get_fixmap_pte(unsigned long vaddr)
  300. {
  301. return pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k(vaddr),
  302. vaddr), vaddr), vaddr);
  303. }
  304. static void __init kmap_init(void)
  305. {
  306. unsigned long kmap_vstart;
  307. /*
  308. * Cache the first kmap pte:
  309. */
  310. kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN);
  311. kmap_pte = kmap_get_fixmap_pte(kmap_vstart);
  312. kmap_prot = PAGE_KERNEL;
  313. }
  314. #ifdef CONFIG_HIGHMEM
  315. static void __init permanent_kmaps_init(pgd_t *pgd_base)
  316. {
  317. unsigned long vaddr;
  318. pgd_t *pgd;
  319. pud_t *pud;
  320. pmd_t *pmd;
  321. pte_t *pte;
  322. vaddr = PKMAP_BASE;
  323. page_table_range_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, pgd_base);
  324. pgd = swapper_pg_dir + pgd_index(vaddr);
  325. pud = pud_offset(pgd, vaddr);
  326. pmd = pmd_offset(pud, vaddr);
  327. pte = pte_offset_kernel(pmd, vaddr);
  328. pkmap_page_table = pte;
  329. }
  330. static void __init add_one_highpage_init(struct page *page, int pfn)
  331. {
  332. ClearPageReserved(page);
  333. init_page_count(page);
  334. __free_page(page);
  335. totalhigh_pages++;
  336. }
  337. struct add_highpages_data {
  338. unsigned long start_pfn;
  339. unsigned long end_pfn;
  340. };
  341. static int __init add_highpages_work_fn(unsigned long start_pfn,
  342. unsigned long end_pfn, void *datax)
  343. {
  344. int node_pfn;
  345. struct page *page;
  346. unsigned long final_start_pfn, final_end_pfn;
  347. struct add_highpages_data *data;
  348. data = (struct add_highpages_data *)datax;
  349. final_start_pfn = max(start_pfn, data->start_pfn);
  350. final_end_pfn = min(end_pfn, data->end_pfn);
  351. if (final_start_pfn >= final_end_pfn)
  352. return 0;
  353. for (node_pfn = final_start_pfn; node_pfn < final_end_pfn;
  354. node_pfn++) {
  355. if (!pfn_valid(node_pfn))
  356. continue;
  357. page = pfn_to_page(node_pfn);
  358. add_one_highpage_init(page, node_pfn);
  359. }
  360. return 0;
  361. }
  362. void __init add_highpages_with_active_regions(int nid, unsigned long start_pfn,
  363. unsigned long end_pfn)
  364. {
  365. struct add_highpages_data data;
  366. data.start_pfn = start_pfn;
  367. data.end_pfn = end_pfn;
  368. work_with_active_regions(nid, add_highpages_work_fn, &data);
  369. }
  370. #ifndef CONFIG_NUMA
  371. static void __init set_highmem_pages_init(void)
  372. {
  373. add_highpages_with_active_regions(0, highstart_pfn, highend_pfn);
  374. totalram_pages += totalhigh_pages;
  375. }
  376. #endif /* !CONFIG_NUMA */
  377. #else
  378. # define permanent_kmaps_init(pgd_base) do { } while (0)
  379. # define set_highmem_pages_init() do { } while (0)
  380. #endif /* CONFIG_HIGHMEM */
  381. void __init native_pagetable_setup_start(pgd_t *base)
  382. {
  383. unsigned long pfn, va;
  384. pgd_t *pgd;
  385. pud_t *pud;
  386. pmd_t *pmd;
  387. pte_t *pte;
  388. /*
  389. * Remove any mappings which extend past the end of physical
  390. * memory from the boot time page table:
  391. */
  392. for (pfn = max_low_pfn + 1; pfn < 1<<(32-PAGE_SHIFT); pfn++) {
  393. va = PAGE_OFFSET + (pfn<<PAGE_SHIFT);
  394. pgd = base + pgd_index(va);
  395. if (!pgd_present(*pgd))
  396. break;
  397. pud = pud_offset(pgd, va);
  398. pmd = pmd_offset(pud, va);
  399. if (!pmd_present(*pmd))
  400. break;
  401. pte = pte_offset_kernel(pmd, va);
  402. if (!pte_present(*pte))
  403. break;
  404. pte_clear(NULL, va, pte);
  405. }
  406. paravirt_alloc_pmd(&init_mm, __pa(base) >> PAGE_SHIFT);
  407. }
  408. void __init native_pagetable_setup_done(pgd_t *base)
  409. {
  410. }
  411. /*
  412. * Build a proper pagetable for the kernel mappings. Up until this
  413. * point, we've been running on some set of pagetables constructed by
  414. * the boot process.
  415. *
  416. * If we're booting on native hardware, this will be a pagetable
  417. * constructed in arch/x86/kernel/head_32.S. The root of the
  418. * pagetable will be swapper_pg_dir.
  419. *
  420. * If we're booting paravirtualized under a hypervisor, then there are
  421. * more options: we may already be running PAE, and the pagetable may
  422. * or may not be based in swapper_pg_dir. In any case,
  423. * paravirt_pagetable_setup_start() will set up swapper_pg_dir
  424. * appropriately for the rest of the initialization to work.
  425. *
  426. * In general, pagetable_init() assumes that the pagetable may already
  427. * be partially populated, and so it avoids stomping on any existing
  428. * mappings.
  429. */
  430. static void __init early_ioremap_page_table_range_init(pgd_t *pgd_base)
  431. {
  432. unsigned long vaddr, end;
  433. /*
  434. * Fixed mappings, only the page table structure has to be
  435. * created - mappings will be set by set_fixmap():
  436. */
  437. early_ioremap_clear();
  438. vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
  439. end = (FIXADDR_TOP + PMD_SIZE - 1) & PMD_MASK;
  440. page_table_range_init(vaddr, end, pgd_base);
  441. early_ioremap_reset();
  442. }
  443. static void __init pagetable_init(void)
  444. {
  445. pgd_t *pgd_base = swapper_pg_dir;
  446. permanent_kmaps_init(pgd_base);
  447. }
  448. #ifdef CONFIG_ACPI_SLEEP
  449. /*
  450. * ACPI suspend needs this for resume, because things like the intel-agp
  451. * driver might have split up a kernel 4MB mapping.
  452. */
  453. char swsusp_pg_dir[PAGE_SIZE]
  454. __attribute__ ((aligned(PAGE_SIZE)));
  455. static inline void save_pg_dir(void)
  456. {
  457. memcpy(swsusp_pg_dir, swapper_pg_dir, PAGE_SIZE);
  458. }
  459. #else /* !CONFIG_ACPI_SLEEP */
  460. static inline void save_pg_dir(void)
  461. {
  462. }
  463. #endif /* !CONFIG_ACPI_SLEEP */
  464. void zap_low_mappings(void)
  465. {
  466. int i;
  467. /*
  468. * Zap initial low-memory mappings.
  469. *
  470. * Note that "pgd_clear()" doesn't do it for
  471. * us, because pgd_clear() is a no-op on i386.
  472. */
  473. for (i = 0; i < KERNEL_PGD_BOUNDARY; i++) {
  474. #ifdef CONFIG_X86_PAE
  475. set_pgd(swapper_pg_dir+i, __pgd(1 + __pa(empty_zero_page)));
  476. #else
  477. set_pgd(swapper_pg_dir+i, __pgd(0));
  478. #endif
  479. }
  480. flush_tlb_all();
  481. }
  482. int nx_enabled;
  483. pteval_t __supported_pte_mask __read_mostly = ~(_PAGE_NX | _PAGE_GLOBAL | _PAGE_IOMAP);
  484. EXPORT_SYMBOL_GPL(__supported_pte_mask);
  485. #ifdef CONFIG_X86_PAE
  486. static int disable_nx __initdata;
  487. /*
  488. * noexec = on|off
  489. *
  490. * Control non executable mappings.
  491. *
  492. * on Enable
  493. * off Disable
  494. */
  495. static int __init noexec_setup(char *str)
  496. {
  497. if (!str || !strcmp(str, "on")) {
  498. if (cpu_has_nx) {
  499. __supported_pte_mask |= _PAGE_NX;
  500. disable_nx = 0;
  501. }
  502. } else {
  503. if (!strcmp(str, "off")) {
  504. disable_nx = 1;
  505. __supported_pte_mask &= ~_PAGE_NX;
  506. } else {
  507. return -EINVAL;
  508. }
  509. }
  510. return 0;
  511. }
  512. early_param("noexec", noexec_setup);
  513. static void __init set_nx(void)
  514. {
  515. unsigned int v[4], l, h;
  516. if (cpu_has_pae && (cpuid_eax(0x80000000) > 0x80000001)) {
  517. cpuid(0x80000001, &v[0], &v[1], &v[2], &v[3]);
  518. if ((v[3] & (1 << 20)) && !disable_nx) {
  519. rdmsr(MSR_EFER, l, h);
  520. l |= EFER_NX;
  521. wrmsr(MSR_EFER, l, h);
  522. nx_enabled = 1;
  523. __supported_pte_mask |= _PAGE_NX;
  524. }
  525. }
  526. }
  527. #endif
  528. /* user-defined highmem size */
  529. static unsigned int highmem_pages = -1;
  530. /*
  531. * highmem=size forces highmem to be exactly 'size' bytes.
  532. * This works even on boxes that have no highmem otherwise.
  533. * This also works to reduce highmem size on bigger boxes.
  534. */
  535. static int __init parse_highmem(char *arg)
  536. {
  537. if (!arg)
  538. return -EINVAL;
  539. highmem_pages = memparse(arg, &arg) >> PAGE_SHIFT;
  540. return 0;
  541. }
  542. early_param("highmem", parse_highmem);
  543. /*
  544. * Determine low and high memory ranges:
  545. */
  546. void __init find_low_pfn_range(void)
  547. {
  548. /* it could update max_pfn */
  549. /* max_low_pfn is 0, we already have early_res support */
  550. max_low_pfn = max_pfn;
  551. if (max_low_pfn > MAXMEM_PFN) {
  552. if (highmem_pages == -1)
  553. highmem_pages = max_pfn - MAXMEM_PFN;
  554. if (highmem_pages + MAXMEM_PFN < max_pfn)
  555. max_pfn = MAXMEM_PFN + highmem_pages;
  556. if (highmem_pages + MAXMEM_PFN > max_pfn) {
  557. printk(KERN_WARNING "only %luMB highmem pages "
  558. "available, ignoring highmem size of %uMB.\n",
  559. pages_to_mb(max_pfn - MAXMEM_PFN),
  560. pages_to_mb(highmem_pages));
  561. highmem_pages = 0;
  562. }
  563. max_low_pfn = MAXMEM_PFN;
  564. #ifndef CONFIG_HIGHMEM
  565. /* Maximum memory usable is what is directly addressable */
  566. printk(KERN_WARNING "Warning only %ldMB will be used.\n",
  567. MAXMEM>>20);
  568. if (max_pfn > MAX_NONPAE_PFN)
  569. printk(KERN_WARNING
  570. "Use a HIGHMEM64G enabled kernel.\n");
  571. else
  572. printk(KERN_WARNING "Use a HIGHMEM enabled kernel.\n");
  573. max_pfn = MAXMEM_PFN;
  574. #else /* !CONFIG_HIGHMEM */
  575. #ifndef CONFIG_HIGHMEM64G
  576. if (max_pfn > MAX_NONPAE_PFN) {
  577. max_pfn = MAX_NONPAE_PFN;
  578. printk(KERN_WARNING "Warning only 4GB will be used."
  579. "Use a HIGHMEM64G enabled kernel.\n");
  580. }
  581. #endif /* !CONFIG_HIGHMEM64G */
  582. #endif /* !CONFIG_HIGHMEM */
  583. } else {
  584. if (highmem_pages == -1)
  585. highmem_pages = 0;
  586. #ifdef CONFIG_HIGHMEM
  587. if (highmem_pages >= max_pfn) {
  588. printk(KERN_ERR "highmem size specified (%uMB) is "
  589. "bigger than pages available (%luMB)!.\n",
  590. pages_to_mb(highmem_pages),
  591. pages_to_mb(max_pfn));
  592. highmem_pages = 0;
  593. }
  594. if (highmem_pages) {
  595. if (max_low_pfn - highmem_pages <
  596. 64*1024*1024/PAGE_SIZE){
  597. printk(KERN_ERR "highmem size %uMB results in "
  598. "smaller than 64MB lowmem, ignoring it.\n"
  599. , pages_to_mb(highmem_pages));
  600. highmem_pages = 0;
  601. }
  602. max_low_pfn -= highmem_pages;
  603. }
  604. #else
  605. if (highmem_pages)
  606. printk(KERN_ERR "ignoring highmem size on non-highmem"
  607. " kernel!\n");
  608. #endif
  609. }
  610. }
  611. #ifndef CONFIG_NEED_MULTIPLE_NODES
  612. void __init initmem_init(unsigned long start_pfn,
  613. unsigned long end_pfn)
  614. {
  615. #ifdef CONFIG_HIGHMEM
  616. highstart_pfn = highend_pfn = max_pfn;
  617. if (max_pfn > max_low_pfn)
  618. highstart_pfn = max_low_pfn;
  619. memory_present(0, 0, highend_pfn);
  620. e820_register_active_regions(0, 0, highend_pfn);
  621. printk(KERN_NOTICE "%ldMB HIGHMEM available.\n",
  622. pages_to_mb(highend_pfn - highstart_pfn));
  623. num_physpages = highend_pfn;
  624. high_memory = (void *) __va(highstart_pfn * PAGE_SIZE - 1) + 1;
  625. #else
  626. memory_present(0, 0, max_low_pfn);
  627. e820_register_active_regions(0, 0, max_low_pfn);
  628. num_physpages = max_low_pfn;
  629. high_memory = (void *) __va(max_low_pfn * PAGE_SIZE - 1) + 1;
  630. #endif
  631. #ifdef CONFIG_FLATMEM
  632. max_mapnr = num_physpages;
  633. #endif
  634. printk(KERN_NOTICE "%ldMB LOWMEM available.\n",
  635. pages_to_mb(max_low_pfn));
  636. setup_bootmem_allocator();
  637. }
  638. #endif /* !CONFIG_NEED_MULTIPLE_NODES */
  639. static void __init zone_sizes_init(void)
  640. {
  641. unsigned long max_zone_pfns[MAX_NR_ZONES];
  642. memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
  643. max_zone_pfns[ZONE_DMA] =
  644. virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
  645. max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
  646. #ifdef CONFIG_HIGHMEM
  647. max_zone_pfns[ZONE_HIGHMEM] = highend_pfn;
  648. #endif
  649. free_area_init_nodes(max_zone_pfns);
  650. }
  651. void __init setup_bootmem_allocator(void)
  652. {
  653. int i;
  654. unsigned long bootmap_size, bootmap;
  655. /*
  656. * Initialize the boot-time allocator (with low memory only):
  657. */
  658. bootmap_size = bootmem_bootmap_pages(max_low_pfn)<<PAGE_SHIFT;
  659. bootmap = find_e820_area(min_low_pfn<<PAGE_SHIFT,
  660. max_pfn_mapped<<PAGE_SHIFT, bootmap_size,
  661. PAGE_SIZE);
  662. if (bootmap == -1L)
  663. panic("Cannot find bootmem map of size %ld\n", bootmap_size);
  664. reserve_early(bootmap, bootmap + bootmap_size, "BOOTMAP");
  665. /* don't touch min_low_pfn */
  666. bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap >> PAGE_SHIFT,
  667. min_low_pfn, max_low_pfn);
  668. printk(KERN_INFO " mapped low ram: 0 - %08lx\n",
  669. max_pfn_mapped<<PAGE_SHIFT);
  670. printk(KERN_INFO " low ram: %08lx - %08lx\n",
  671. min_low_pfn<<PAGE_SHIFT, max_low_pfn<<PAGE_SHIFT);
  672. printk(KERN_INFO " bootmap %08lx - %08lx\n",
  673. bootmap, bootmap + bootmap_size);
  674. for_each_online_node(i)
  675. free_bootmem_with_active_regions(i, max_low_pfn);
  676. early_res_to_bootmem(0, max_low_pfn<<PAGE_SHIFT);
  677. after_init_bootmem = 1;
  678. }
  679. static void __init find_early_table_space(unsigned long end, int use_pse)
  680. {
  681. unsigned long puds, pmds, ptes, tables, start;
  682. puds = (end + PUD_SIZE - 1) >> PUD_SHIFT;
  683. tables = PAGE_ALIGN(puds * sizeof(pud_t));
  684. pmds = (end + PMD_SIZE - 1) >> PMD_SHIFT;
  685. tables += PAGE_ALIGN(pmds * sizeof(pmd_t));
  686. if (use_pse) {
  687. unsigned long extra;
  688. extra = end - ((end>>PMD_SHIFT) << PMD_SHIFT);
  689. extra += PMD_SIZE;
  690. ptes = (extra + PAGE_SIZE - 1) >> PAGE_SHIFT;
  691. } else
  692. ptes = (end + PAGE_SIZE - 1) >> PAGE_SHIFT;
  693. tables += PAGE_ALIGN(ptes * sizeof(pte_t));
  694. /* for fixmap */
  695. tables += PAGE_SIZE * 2;
  696. /*
  697. * RED-PEN putting page tables only on node 0 could
  698. * cause a hotspot and fill up ZONE_DMA. The page tables
  699. * need roughly 0.5KB per GB.
  700. */
  701. start = 0x7000;
  702. table_start = find_e820_area(start, max_pfn_mapped<<PAGE_SHIFT,
  703. tables, PAGE_SIZE);
  704. if (table_start == -1UL)
  705. panic("Cannot find space for the kernel page tables");
  706. table_start >>= PAGE_SHIFT;
  707. table_end = table_start;
  708. table_top = table_start + (tables>>PAGE_SHIFT);
  709. printk(KERN_DEBUG "kernel direct mapping tables up to %lx @ %lx-%lx\n",
  710. end, table_start << PAGE_SHIFT,
  711. (table_start << PAGE_SHIFT) + tables);
  712. }
  713. unsigned long __init_refok init_memory_mapping(unsigned long start,
  714. unsigned long end)
  715. {
  716. pgd_t *pgd_base = swapper_pg_dir;
  717. unsigned long start_pfn, end_pfn;
  718. unsigned long big_page_start;
  719. #ifdef CONFIG_DEBUG_PAGEALLOC
  720. /*
  721. * For CONFIG_DEBUG_PAGEALLOC, identity mapping will use small pages.
  722. * This will simplify cpa(), which otherwise needs to support splitting
  723. * large pages into small in interrupt context, etc.
  724. */
  725. int use_pse = 0;
  726. #else
  727. int use_pse = cpu_has_pse;
  728. #endif
  729. /*
  730. * Find space for the kernel direct mapping tables.
  731. */
  732. if (!after_init_bootmem)
  733. find_early_table_space(end, use_pse);
  734. #ifdef CONFIG_X86_PAE
  735. set_nx();
  736. if (nx_enabled)
  737. printk(KERN_INFO "NX (Execute Disable) protection: active\n");
  738. #endif
  739. /* Enable PSE if available */
  740. if (cpu_has_pse)
  741. set_in_cr4(X86_CR4_PSE);
  742. /* Enable PGE if available */
  743. if (cpu_has_pge) {
  744. set_in_cr4(X86_CR4_PGE);
  745. __supported_pte_mask |= _PAGE_GLOBAL;
  746. }
  747. /*
  748. * Don't use a large page for the first 2/4MB of memory
  749. * because there are often fixed size MTRRs in there
  750. * and overlapping MTRRs into large pages can cause
  751. * slowdowns.
  752. */
  753. big_page_start = PMD_SIZE;
  754. if (start < big_page_start) {
  755. start_pfn = start >> PAGE_SHIFT;
  756. end_pfn = min(big_page_start>>PAGE_SHIFT, end>>PAGE_SHIFT);
  757. } else {
  758. /* head is not big page alignment ? */
  759. start_pfn = start >> PAGE_SHIFT;
  760. end_pfn = ((start + (PMD_SIZE - 1))>>PMD_SHIFT)
  761. << (PMD_SHIFT - PAGE_SHIFT);
  762. }
  763. if (start_pfn < end_pfn)
  764. kernel_physical_mapping_init(pgd_base, start_pfn, end_pfn, 0);
  765. /* big page range */
  766. start_pfn = ((start + (PMD_SIZE - 1))>>PMD_SHIFT)
  767. << (PMD_SHIFT - PAGE_SHIFT);
  768. if (start_pfn < (big_page_start >> PAGE_SHIFT))
  769. start_pfn = big_page_start >> PAGE_SHIFT;
  770. end_pfn = (end>>PMD_SHIFT) << (PMD_SHIFT - PAGE_SHIFT);
  771. if (start_pfn < end_pfn)
  772. kernel_physical_mapping_init(pgd_base, start_pfn, end_pfn,
  773. use_pse);
  774. /* tail is not big page alignment ? */
  775. start_pfn = end_pfn;
  776. if (start_pfn > (big_page_start>>PAGE_SHIFT)) {
  777. end_pfn = end >> PAGE_SHIFT;
  778. if (start_pfn < end_pfn)
  779. kernel_physical_mapping_init(pgd_base, start_pfn,
  780. end_pfn, 0);
  781. }
  782. early_ioremap_page_table_range_init(pgd_base);
  783. load_cr3(swapper_pg_dir);
  784. __flush_tlb_all();
  785. if (!after_init_bootmem)
  786. reserve_early(table_start << PAGE_SHIFT,
  787. table_end << PAGE_SHIFT, "PGTABLE");
  788. if (!after_init_bootmem)
  789. early_memtest(start, end);
  790. return end >> PAGE_SHIFT;
  791. }
  792. /*
  793. * paging_init() sets up the page tables - note that the first 8MB are
  794. * already mapped by head.S.
  795. *
  796. * This routines also unmaps the page at virtual kernel address 0, so
  797. * that we can trap those pesky NULL-reference errors in the kernel.
  798. */
  799. void __init paging_init(void)
  800. {
  801. pagetable_init();
  802. __flush_tlb_all();
  803. kmap_init();
  804. /*
  805. * NOTE: at this point the bootmem allocator is fully available.
  806. */
  807. sparse_init();
  808. zone_sizes_init();
  809. }
  810. /*
  811. * Test if the WP bit works in supervisor mode. It isn't supported on 386's
  812. * and also on some strange 486's. All 586+'s are OK. This used to involve
  813. * black magic jumps to work around some nasty CPU bugs, but fortunately the
  814. * switch to using exceptions got rid of all that.
  815. */
  816. static void __init test_wp_bit(void)
  817. {
  818. printk(KERN_INFO
  819. "Checking if this processor honours the WP bit even in supervisor mode...");
  820. /* Any page-aligned address will do, the test is non-destructive */
  821. __set_fixmap(FIX_WP_TEST, __pa(&swapper_pg_dir), PAGE_READONLY);
  822. boot_cpu_data.wp_works_ok = do_test_wp_bit();
  823. clear_fixmap(FIX_WP_TEST);
  824. if (!boot_cpu_data.wp_works_ok) {
  825. printk(KERN_CONT "No.\n");
  826. #ifdef CONFIG_X86_WP_WORKS_OK
  827. panic(
  828. "This kernel doesn't support CPU's with broken WP. Recompile it for a 386!");
  829. #endif
  830. } else {
  831. printk(KERN_CONT "Ok.\n");
  832. }
  833. }
  834. static struct kcore_list kcore_mem, kcore_vmalloc;
  835. void __init mem_init(void)
  836. {
  837. int codesize, reservedpages, datasize, initsize;
  838. int tmp;
  839. start_periodic_check_for_corruption();
  840. #ifdef CONFIG_FLATMEM
  841. BUG_ON(!mem_map);
  842. #endif
  843. /* this will put all low memory onto the freelists */
  844. totalram_pages += free_all_bootmem();
  845. reservedpages = 0;
  846. for (tmp = 0; tmp < max_low_pfn; tmp++)
  847. /*
  848. * Only count reserved RAM pages:
  849. */
  850. if (page_is_ram(tmp) && PageReserved(pfn_to_page(tmp)))
  851. reservedpages++;
  852. set_highmem_pages_init();
  853. codesize = (unsigned long) &_etext - (unsigned long) &_text;
  854. datasize = (unsigned long) &_edata - (unsigned long) &_etext;
  855. initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
  856. kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
  857. kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
  858. VMALLOC_END-VMALLOC_START);
  859. printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, "
  860. "%dk reserved, %dk data, %dk init, %ldk highmem)\n",
  861. (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
  862. num_physpages << (PAGE_SHIFT-10),
  863. codesize >> 10,
  864. reservedpages << (PAGE_SHIFT-10),
  865. datasize >> 10,
  866. initsize >> 10,
  867. (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10))
  868. );
  869. printk(KERN_INFO "virtual kernel memory layout:\n"
  870. " fixmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
  871. #ifdef CONFIG_HIGHMEM
  872. " pkmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
  873. #endif
  874. " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n"
  875. " lowmem : 0x%08lx - 0x%08lx (%4ld MB)\n"
  876. " .init : 0x%08lx - 0x%08lx (%4ld kB)\n"
  877. " .data : 0x%08lx - 0x%08lx (%4ld kB)\n"
  878. " .text : 0x%08lx - 0x%08lx (%4ld kB)\n",
  879. FIXADDR_START, FIXADDR_TOP,
  880. (FIXADDR_TOP - FIXADDR_START) >> 10,
  881. #ifdef CONFIG_HIGHMEM
  882. PKMAP_BASE, PKMAP_BASE+LAST_PKMAP*PAGE_SIZE,
  883. (LAST_PKMAP*PAGE_SIZE) >> 10,
  884. #endif
  885. VMALLOC_START, VMALLOC_END,
  886. (VMALLOC_END - VMALLOC_START) >> 20,
  887. (unsigned long)__va(0), (unsigned long)high_memory,
  888. ((unsigned long)high_memory - (unsigned long)__va(0)) >> 20,
  889. (unsigned long)&__init_begin, (unsigned long)&__init_end,
  890. ((unsigned long)&__init_end -
  891. (unsigned long)&__init_begin) >> 10,
  892. (unsigned long)&_etext, (unsigned long)&_edata,
  893. ((unsigned long)&_edata - (unsigned long)&_etext) >> 10,
  894. (unsigned long)&_text, (unsigned long)&_etext,
  895. ((unsigned long)&_etext - (unsigned long)&_text) >> 10);
  896. #ifdef CONFIG_HIGHMEM
  897. BUG_ON(PKMAP_BASE + LAST_PKMAP*PAGE_SIZE > FIXADDR_START);
  898. BUG_ON(VMALLOC_END > PKMAP_BASE);
  899. #endif
  900. BUG_ON(VMALLOC_START > VMALLOC_END);
  901. BUG_ON((unsigned long)high_memory > VMALLOC_START);
  902. if (boot_cpu_data.wp_works_ok < 0)
  903. test_wp_bit();
  904. save_pg_dir();
  905. zap_low_mappings();
  906. }
  907. #ifdef CONFIG_MEMORY_HOTPLUG
  908. int arch_add_memory(int nid, u64 start, u64 size)
  909. {
  910. struct pglist_data *pgdata = NODE_DATA(nid);
  911. struct zone *zone = pgdata->node_zones + ZONE_HIGHMEM;
  912. unsigned long start_pfn = start >> PAGE_SHIFT;
  913. unsigned long nr_pages = size >> PAGE_SHIFT;
  914. return __add_pages(zone, start_pfn, nr_pages);
  915. }
  916. #endif
  917. /*
  918. * This function cannot be __init, since exceptions don't work in that
  919. * section. Put this after the callers, so that it cannot be inlined.
  920. */
  921. static noinline int do_test_wp_bit(void)
  922. {
  923. char tmp_reg;
  924. int flag;
  925. __asm__ __volatile__(
  926. " movb %0, %1 \n"
  927. "1: movb %1, %0 \n"
  928. " xorl %2, %2 \n"
  929. "2: \n"
  930. _ASM_EXTABLE(1b,2b)
  931. :"=m" (*(char *)fix_to_virt(FIX_WP_TEST)),
  932. "=q" (tmp_reg),
  933. "=r" (flag)
  934. :"2" (1)
  935. :"memory");
  936. return flag;
  937. }
  938. #ifdef CONFIG_DEBUG_RODATA
  939. const int rodata_test_data = 0xC3;
  940. EXPORT_SYMBOL_GPL(rodata_test_data);
  941. void mark_rodata_ro(void)
  942. {
  943. unsigned long start = PFN_ALIGN(_text);
  944. unsigned long size = PFN_ALIGN(_etext) - start;
  945. #ifndef CONFIG_DYNAMIC_FTRACE
  946. /* Dynamic tracing modifies the kernel text section */
  947. set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
  948. printk(KERN_INFO "Write protecting the kernel text: %luk\n",
  949. size >> 10);
  950. #ifdef CONFIG_CPA_DEBUG
  951. printk(KERN_INFO "Testing CPA: Reverting %lx-%lx\n",
  952. start, start+size);
  953. set_pages_rw(virt_to_page(start), size>>PAGE_SHIFT);
  954. printk(KERN_INFO "Testing CPA: write protecting again\n");
  955. set_pages_ro(virt_to_page(start), size>>PAGE_SHIFT);
  956. #endif
  957. #endif /* CONFIG_DYNAMIC_FTRACE */
  958. start += size;
  959. size = (unsigned long)__end_rodata - start;
  960. set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
  961. printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
  962. size >> 10);
  963. rodata_test();
  964. #ifdef CONFIG_CPA_DEBUG
  965. printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, start + size);
  966. set_pages_rw(virt_to_page(start), size >> PAGE_SHIFT);
  967. printk(KERN_INFO "Testing CPA: write protecting again\n");
  968. set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
  969. #endif
  970. }
  971. #endif
  972. void free_init_pages(char *what, unsigned long begin, unsigned long end)
  973. {
  974. #ifdef CONFIG_DEBUG_PAGEALLOC
  975. /*
  976. * If debugging page accesses then do not free this memory but
  977. * mark them not present - any buggy init-section access will
  978. * create a kernel page fault:
  979. */
  980. printk(KERN_INFO "debug: unmapping init memory %08lx..%08lx\n",
  981. begin, PAGE_ALIGN(end));
  982. set_memory_np(begin, (end - begin) >> PAGE_SHIFT);
  983. #else
  984. unsigned long addr;
  985. /*
  986. * We just marked the kernel text read only above, now that
  987. * we are going to free part of that, we need to make that
  988. * writeable first.
  989. */
  990. set_memory_rw(begin, (end - begin) >> PAGE_SHIFT);
  991. for (addr = begin; addr < end; addr += PAGE_SIZE) {
  992. ClearPageReserved(virt_to_page(addr));
  993. init_page_count(virt_to_page(addr));
  994. memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
  995. free_page(addr);
  996. totalram_pages++;
  997. }
  998. printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
  999. #endif
  1000. }
  1001. void free_initmem(void)
  1002. {
  1003. free_init_pages("unused kernel memory",
  1004. (unsigned long)(&__init_begin),
  1005. (unsigned long)(&__init_end));
  1006. }
  1007. #ifdef CONFIG_BLK_DEV_INITRD
  1008. void free_initrd_mem(unsigned long start, unsigned long end)
  1009. {
  1010. free_init_pages("initrd memory", start, end);
  1011. }
  1012. #endif
  1013. int __init reserve_bootmem_generic(unsigned long phys, unsigned long len,
  1014. int flags)
  1015. {
  1016. return reserve_bootmem(phys, len, flags);
  1017. }