pgtable_32.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * linux/arch/i386/mm/pgtable.c
  3. */
  4. #include <linux/sched.h>
  5. #include <linux/kernel.h>
  6. #include <linux/errno.h>
  7. #include <linux/mm.h>
  8. #include <linux/nmi.h>
  9. #include <linux/swap.h>
  10. #include <linux/smp.h>
  11. #include <linux/highmem.h>
  12. #include <linux/slab.h>
  13. #include <linux/pagemap.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/module.h>
  16. #include <linux/quicklist.h>
  17. #include <asm/system.h>
  18. #include <asm/pgtable.h>
  19. #include <asm/pgalloc.h>
  20. #include <asm/fixmap.h>
  21. #include <asm/e820.h>
  22. #include <asm/tlb.h>
  23. #include <asm/tlbflush.h>
  24. void show_mem(void)
  25. {
  26. int total = 0, reserved = 0;
  27. int shared = 0, cached = 0;
  28. int highmem = 0;
  29. struct page *page;
  30. pg_data_t *pgdat;
  31. unsigned long i;
  32. unsigned long flags;
  33. printk(KERN_INFO "Mem-info:\n");
  34. show_free_areas();
  35. printk(KERN_INFO "Free swap: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
  36. for_each_online_pgdat(pgdat) {
  37. pgdat_resize_lock(pgdat, &flags);
  38. for (i = 0; i < pgdat->node_spanned_pages; ++i) {
  39. if (unlikely(i % MAX_ORDER_NR_PAGES == 0))
  40. touch_nmi_watchdog();
  41. page = pgdat_page_nr(pgdat, i);
  42. total++;
  43. if (PageHighMem(page))
  44. highmem++;
  45. if (PageReserved(page))
  46. reserved++;
  47. else if (PageSwapCache(page))
  48. cached++;
  49. else if (page_count(page))
  50. shared += page_count(page) - 1;
  51. }
  52. pgdat_resize_unlock(pgdat, &flags);
  53. }
  54. printk(KERN_INFO "%d pages of RAM\n", total);
  55. printk(KERN_INFO "%d pages of HIGHMEM\n", highmem);
  56. printk(KERN_INFO "%d reserved pages\n", reserved);
  57. printk(KERN_INFO "%d pages shared\n", shared);
  58. printk(KERN_INFO "%d pages swap cached\n", cached);
  59. printk(KERN_INFO "%lu pages dirty\n", global_page_state(NR_FILE_DIRTY));
  60. printk(KERN_INFO "%lu pages writeback\n",
  61. global_page_state(NR_WRITEBACK));
  62. printk(KERN_INFO "%lu pages mapped\n", global_page_state(NR_FILE_MAPPED));
  63. printk(KERN_INFO "%lu pages slab\n",
  64. global_page_state(NR_SLAB_RECLAIMABLE) +
  65. global_page_state(NR_SLAB_UNRECLAIMABLE));
  66. printk(KERN_INFO "%lu pages pagetables\n",
  67. global_page_state(NR_PAGETABLE));
  68. }
  69. /*
  70. * Associate a virtual page frame with a given physical page frame
  71. * and protection flags for that frame.
  72. */
  73. static void set_pte_pfn(unsigned long vaddr, unsigned long pfn, pgprot_t flags)
  74. {
  75. pgd_t *pgd;
  76. pud_t *pud;
  77. pmd_t *pmd;
  78. pte_t *pte;
  79. pgd = swapper_pg_dir + pgd_index(vaddr);
  80. if (pgd_none(*pgd)) {
  81. BUG();
  82. return;
  83. }
  84. pud = pud_offset(pgd, vaddr);
  85. if (pud_none(*pud)) {
  86. BUG();
  87. return;
  88. }
  89. pmd = pmd_offset(pud, vaddr);
  90. if (pmd_none(*pmd)) {
  91. BUG();
  92. return;
  93. }
  94. pte = pte_offset_kernel(pmd, vaddr);
  95. if (pgprot_val(flags))
  96. set_pte_present(&init_mm, vaddr, pte, pfn_pte(pfn, flags));
  97. else
  98. pte_clear(&init_mm, vaddr, pte);
  99. /*
  100. * It's enough to flush this one mapping.
  101. * (PGE mappings get flushed as well)
  102. */
  103. __flush_tlb_one(vaddr);
  104. }
  105. /*
  106. * Associate a large virtual page frame with a given physical page frame
  107. * and protection flags for that frame. pfn is for the base of the page,
  108. * vaddr is what the page gets mapped to - both must be properly aligned.
  109. * The pmd must already be instantiated. Assumes PAE mode.
  110. */
  111. void set_pmd_pfn(unsigned long vaddr, unsigned long pfn, pgprot_t flags)
  112. {
  113. pgd_t *pgd;
  114. pud_t *pud;
  115. pmd_t *pmd;
  116. if (vaddr & (PMD_SIZE-1)) { /* vaddr is misaligned */
  117. printk(KERN_WARNING "set_pmd_pfn: vaddr misaligned\n");
  118. return; /* BUG(); */
  119. }
  120. if (pfn & (PTRS_PER_PTE-1)) { /* pfn is misaligned */
  121. printk(KERN_WARNING "set_pmd_pfn: pfn misaligned\n");
  122. return; /* BUG(); */
  123. }
  124. pgd = swapper_pg_dir + pgd_index(vaddr);
  125. if (pgd_none(*pgd)) {
  126. printk(KERN_WARNING "set_pmd_pfn: pgd_none\n");
  127. return; /* BUG(); */
  128. }
  129. pud = pud_offset(pgd, vaddr);
  130. pmd = pmd_offset(pud, vaddr);
  131. set_pmd(pmd, pfn_pmd(pfn, flags));
  132. /*
  133. * It's enough to flush this one mapping.
  134. * (PGE mappings get flushed as well)
  135. */
  136. __flush_tlb_one(vaddr);
  137. }
  138. static int fixmaps;
  139. unsigned long __FIXADDR_TOP = 0xfffff000;
  140. EXPORT_SYMBOL(__FIXADDR_TOP);
  141. void __set_fixmap (enum fixed_addresses idx, unsigned long phys, pgprot_t flags)
  142. {
  143. unsigned long address = __fix_to_virt(idx);
  144. if (idx >= __end_of_fixed_addresses) {
  145. BUG();
  146. return;
  147. }
  148. set_pte_pfn(address, phys >> PAGE_SHIFT, flags);
  149. fixmaps++;
  150. }
  151. /**
  152. * reserve_top_address - reserves a hole in the top of kernel address space
  153. * @reserve - size of hole to reserve
  154. *
  155. * Can be used to relocate the fixmap area and poke a hole in the top
  156. * of kernel address space to make room for a hypervisor.
  157. */
  158. void reserve_top_address(unsigned long reserve)
  159. {
  160. BUG_ON(fixmaps > 0);
  161. printk(KERN_INFO "Reserving virtual address space above 0x%08x\n",
  162. (int)-reserve);
  163. __FIXADDR_TOP = -reserve - PAGE_SIZE;
  164. __VMALLOC_RESERVE += reserve;
  165. }
  166. pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
  167. {
  168. return (pte_t *)__get_free_page(GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO);
  169. }
  170. pgtable_t pte_alloc_one(struct mm_struct *mm, unsigned long address)
  171. {
  172. struct page *pte;
  173. #ifdef CONFIG_HIGHPTE
  174. pte = alloc_pages(GFP_KERNEL|__GFP_HIGHMEM|__GFP_REPEAT|__GFP_ZERO, 0);
  175. #else
  176. pte = alloc_pages(GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO, 0);
  177. #endif
  178. if (pte)
  179. pgtable_page_ctor(pte);
  180. return pte;
  181. }
  182. /*
  183. * List of all pgd's needed for non-PAE so it can invalidate entries
  184. * in both cached and uncached pgd's; not needed for PAE since the
  185. * kernel pmd is shared. If PAE were not to share the pmd a similar
  186. * tactic would be needed. This is essentially codepath-based locking
  187. * against pageattr.c; it is the unique case in which a valid change
  188. * of kernel pagetables can't be lazily synchronized by vmalloc faults.
  189. * vmalloc faults work because attached pagetables are never freed.
  190. * -- wli
  191. */
  192. static inline void pgd_list_add(pgd_t *pgd)
  193. {
  194. struct page *page = virt_to_page(pgd);
  195. list_add(&page->lru, &pgd_list);
  196. }
  197. static inline void pgd_list_del(pgd_t *pgd)
  198. {
  199. struct page *page = virt_to_page(pgd);
  200. list_del(&page->lru);
  201. }
  202. #define UNSHARED_PTRS_PER_PGD \
  203. (SHARED_KERNEL_PMD ? USER_PTRS_PER_PGD : PTRS_PER_PGD)
  204. static void pgd_ctor(void *p)
  205. {
  206. pgd_t *pgd = p;
  207. unsigned long flags;
  208. /* Clear usermode parts of PGD */
  209. memset(pgd, 0, USER_PTRS_PER_PGD*sizeof(pgd_t));
  210. spin_lock_irqsave(&pgd_lock, flags);
  211. /* If the pgd points to a shared pagetable level (either the
  212. ptes in non-PAE, or shared PMD in PAE), then just copy the
  213. references from swapper_pg_dir. */
  214. if (PAGETABLE_LEVELS == 2 ||
  215. (PAGETABLE_LEVELS == 3 && SHARED_KERNEL_PMD)) {
  216. clone_pgd_range(pgd + USER_PTRS_PER_PGD,
  217. swapper_pg_dir + USER_PTRS_PER_PGD,
  218. KERNEL_PGD_PTRS);
  219. paravirt_alloc_pd_clone(__pa(pgd) >> PAGE_SHIFT,
  220. __pa(swapper_pg_dir) >> PAGE_SHIFT,
  221. USER_PTRS_PER_PGD,
  222. KERNEL_PGD_PTRS);
  223. }
  224. /* list required to sync kernel mapping updates */
  225. if (!SHARED_KERNEL_PMD)
  226. pgd_list_add(pgd);
  227. spin_unlock_irqrestore(&pgd_lock, flags);
  228. }
  229. static void pgd_dtor(void *pgd)
  230. {
  231. unsigned long flags; /* can be called from interrupt context */
  232. if (SHARED_KERNEL_PMD)
  233. return;
  234. spin_lock_irqsave(&pgd_lock, flags);
  235. pgd_list_del(pgd);
  236. spin_unlock_irqrestore(&pgd_lock, flags);
  237. }
  238. #ifdef CONFIG_X86_PAE
  239. /*
  240. * Mop up any pmd pages which may still be attached to the pgd.
  241. * Normally they will be freed by munmap/exit_mmap, but any pmd we
  242. * preallocate which never got a corresponding vma will need to be
  243. * freed manually.
  244. */
  245. static void pgd_mop_up_pmds(struct mm_struct *mm, pgd_t *pgdp)
  246. {
  247. int i;
  248. for(i = 0; i < UNSHARED_PTRS_PER_PGD; i++) {
  249. pgd_t pgd = pgdp[i];
  250. if (pgd_val(pgd) != 0) {
  251. pmd_t *pmd = (pmd_t *)pgd_page_vaddr(pgd);
  252. pgdp[i] = native_make_pgd(0);
  253. paravirt_release_pd(pgd_val(pgd) >> PAGE_SHIFT);
  254. pmd_free(mm, pmd);
  255. }
  256. }
  257. }
  258. /*
  259. * In PAE mode, we need to do a cr3 reload (=tlb flush) when
  260. * updating the top-level pagetable entries to guarantee the
  261. * processor notices the update. Since this is expensive, and
  262. * all 4 top-level entries are used almost immediately in a
  263. * new process's life, we just pre-populate them here.
  264. *
  265. * Also, if we're in a paravirt environment where the kernel pmd is
  266. * not shared between pagetables (!SHARED_KERNEL_PMDS), we allocate
  267. * and initialize the kernel pmds here.
  268. */
  269. static int pgd_prepopulate_pmd(struct mm_struct *mm, pgd_t *pgd)
  270. {
  271. pud_t *pud;
  272. unsigned long addr;
  273. int i;
  274. pud = pud_offset(pgd, 0);
  275. for (addr = i = 0; i < UNSHARED_PTRS_PER_PGD;
  276. i++, pud++, addr += PUD_SIZE) {
  277. pmd_t *pmd = pmd_alloc_one(mm, addr);
  278. if (!pmd) {
  279. pgd_mop_up_pmds(mm, pgd);
  280. return 0;
  281. }
  282. if (i >= USER_PTRS_PER_PGD)
  283. memcpy(pmd, (pmd_t *)pgd_page_vaddr(swapper_pg_dir[i]),
  284. sizeof(pmd_t) * PTRS_PER_PMD);
  285. pud_populate(mm, pud, pmd);
  286. }
  287. return 1;
  288. }
  289. #else /* !CONFIG_X86_PAE */
  290. /* No need to prepopulate any pagetable entries in non-PAE modes. */
  291. static int pgd_prepopulate_pmd(struct mm_struct *mm, pgd_t *pgd)
  292. {
  293. return 1;
  294. }
  295. static void pgd_mop_up_pmds(struct mm_struct *mm, pgd_t *pgdp)
  296. {
  297. }
  298. #endif /* CONFIG_X86_PAE */
  299. pgd_t *pgd_alloc(struct mm_struct *mm)
  300. {
  301. pgd_t *pgd = quicklist_alloc(0, GFP_KERNEL, pgd_ctor);
  302. mm->pgd = pgd; /* so that alloc_pd can use it */
  303. if (pgd && !pgd_prepopulate_pmd(mm, pgd)) {
  304. quicklist_free(0, pgd_dtor, pgd);
  305. pgd = NULL;
  306. }
  307. return pgd;
  308. }
  309. void pgd_free(struct mm_struct *mm, pgd_t *pgd)
  310. {
  311. pgd_mop_up_pmds(mm, pgd);
  312. quicklist_free(0, pgd_dtor, pgd);
  313. }
  314. void check_pgt_cache(void)
  315. {
  316. quicklist_trim(0, pgd_dtor, 25, 16);
  317. }
  318. void __pte_free_tlb(struct mmu_gather *tlb, struct page *pte)
  319. {
  320. pgtable_page_dtor(pte);
  321. paravirt_release_pt(page_to_pfn(pte));
  322. tlb_remove_page(tlb, pte);
  323. }
  324. #ifdef CONFIG_X86_PAE
  325. void __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd)
  326. {
  327. paravirt_release_pd(__pa(pmd) >> PAGE_SHIFT);
  328. tlb_remove_page(tlb, virt_to_page(pmd));
  329. }
  330. #endif