init_32.c 28 KB

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