init_32.c 30 KB

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