pgtable_32.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. struct page *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. return pte;
  179. }
  180. /*
  181. * List of all pgd's needed for non-PAE so it can invalidate entries
  182. * in both cached and uncached pgd's; not needed for PAE since the
  183. * kernel pmd is shared. If PAE were not to share the pmd a similar
  184. * tactic would be needed. This is essentially codepath-based locking
  185. * against pageattr.c; it is the unique case in which a valid change
  186. * of kernel pagetables can't be lazily synchronized by vmalloc faults.
  187. * vmalloc faults work because attached pagetables are never freed.
  188. * -- wli
  189. */
  190. static inline void pgd_list_add(pgd_t *pgd)
  191. {
  192. struct page *page = virt_to_page(pgd);
  193. list_add(&page->lru, &pgd_list);
  194. }
  195. static inline void pgd_list_del(pgd_t *pgd)
  196. {
  197. struct page *page = virt_to_page(pgd);
  198. list_del(&page->lru);
  199. }
  200. #define UNSHARED_PTRS_PER_PGD \
  201. (SHARED_KERNEL_PMD ? USER_PTRS_PER_PGD : PTRS_PER_PGD)
  202. static void pgd_ctor(void *p)
  203. {
  204. pgd_t *pgd = p;
  205. unsigned long flags;
  206. /* Clear usermode parts of PGD */
  207. memset(pgd, 0, USER_PTRS_PER_PGD*sizeof(pgd_t));
  208. spin_lock_irqsave(&pgd_lock, flags);
  209. /* If the pgd points to a shared pagetable level (either the
  210. ptes in non-PAE, or shared PMD in PAE), then just copy the
  211. references from swapper_pg_dir. */
  212. if (PAGETABLE_LEVELS == 2 ||
  213. (PAGETABLE_LEVELS == 3 && SHARED_KERNEL_PMD)) {
  214. clone_pgd_range(pgd + USER_PTRS_PER_PGD,
  215. swapper_pg_dir + USER_PTRS_PER_PGD,
  216. KERNEL_PGD_PTRS);
  217. paravirt_alloc_pd_clone(__pa(pgd) >> PAGE_SHIFT,
  218. __pa(swapper_pg_dir) >> PAGE_SHIFT,
  219. USER_PTRS_PER_PGD,
  220. KERNEL_PGD_PTRS);
  221. }
  222. /* list required to sync kernel mapping updates */
  223. if (!SHARED_KERNEL_PMD)
  224. pgd_list_add(pgd);
  225. spin_unlock_irqrestore(&pgd_lock, flags);
  226. }
  227. static void pgd_dtor(void *pgd)
  228. {
  229. unsigned long flags; /* can be called from interrupt context */
  230. if (SHARED_KERNEL_PMD)
  231. return;
  232. spin_lock_irqsave(&pgd_lock, flags);
  233. pgd_list_del(pgd);
  234. spin_unlock_irqrestore(&pgd_lock, flags);
  235. }
  236. #ifdef CONFIG_X86_PAE
  237. /*
  238. * Mop up any pmd pages which may still be attached to the pgd.
  239. * Normally they will be freed by munmap/exit_mmap, but any pmd we
  240. * preallocate which never got a corresponding vma will need to be
  241. * freed manually.
  242. */
  243. static void pgd_mop_up_pmds(struct mm_struct *mm, pgd_t *pgdp)
  244. {
  245. int i;
  246. for(i = 0; i < UNSHARED_PTRS_PER_PGD; i++) {
  247. pgd_t pgd = pgdp[i];
  248. if (pgd_val(pgd) != 0) {
  249. pmd_t *pmd = (pmd_t *)pgd_page_vaddr(pgd);
  250. pgdp[i] = native_make_pgd(0);
  251. paravirt_release_pd(pgd_val(pgd) >> PAGE_SHIFT);
  252. pmd_free(mm, pmd);
  253. }
  254. }
  255. }
  256. /*
  257. * In PAE mode, we need to do a cr3 reload (=tlb flush) when
  258. * updating the top-level pagetable entries to guarantee the
  259. * processor notices the update. Since this is expensive, and
  260. * all 4 top-level entries are used almost immediately in a
  261. * new process's life, we just pre-populate them here.
  262. *
  263. * Also, if we're in a paravirt environment where the kernel pmd is
  264. * not shared between pagetables (!SHARED_KERNEL_PMDS), we allocate
  265. * and initialize the kernel pmds here.
  266. */
  267. static int pgd_prepopulate_pmd(struct mm_struct *mm, pgd_t *pgd)
  268. {
  269. pud_t *pud;
  270. unsigned long addr;
  271. int i;
  272. pud = pud_offset(pgd, 0);
  273. for (addr = i = 0; i < UNSHARED_PTRS_PER_PGD;
  274. i++, pud++, addr += PUD_SIZE) {
  275. pmd_t *pmd = pmd_alloc_one(mm, addr);
  276. if (!pmd) {
  277. pgd_mop_up_pmds(mm, pgd);
  278. return 0;
  279. }
  280. if (i >= USER_PTRS_PER_PGD)
  281. memcpy(pmd, (pmd_t *)pgd_page_vaddr(swapper_pg_dir[i]),
  282. sizeof(pmd_t) * PTRS_PER_PMD);
  283. pud_populate(mm, pud, pmd);
  284. }
  285. return 1;
  286. }
  287. #else /* !CONFIG_X86_PAE */
  288. /* No need to prepopulate any pagetable entries in non-PAE modes. */
  289. static int pgd_prepopulate_pmd(struct mm_struct *mm, pgd_t *pgd)
  290. {
  291. return 1;
  292. }
  293. static void pgd_mop_up_pmds(struct mm_struct *mm, pgd_t *pgdp)
  294. {
  295. }
  296. #endif /* CONFIG_X86_PAE */
  297. pgd_t *pgd_alloc(struct mm_struct *mm)
  298. {
  299. pgd_t *pgd = quicklist_alloc(0, GFP_KERNEL, pgd_ctor);
  300. mm->pgd = pgd; /* so that alloc_pd can use it */
  301. if (pgd && !pgd_prepopulate_pmd(mm, pgd)) {
  302. quicklist_free(0, pgd_dtor, pgd);
  303. pgd = NULL;
  304. }
  305. return pgd;
  306. }
  307. void pgd_free(struct mm_struct *mm, pgd_t *pgd)
  308. {
  309. pgd_mop_up_pmds(mm, pgd);
  310. quicklist_free(0, pgd_dtor, pgd);
  311. }
  312. void check_pgt_cache(void)
  313. {
  314. quicklist_trim(0, pgd_dtor, 25, 16);
  315. }
  316. void __pte_free_tlb(struct mmu_gather *tlb, struct page *pte)
  317. {
  318. paravirt_release_pt(page_to_pfn(pte));
  319. tlb_remove_page(tlb, pte);
  320. }
  321. #ifdef CONFIG_X86_PAE
  322. void __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd)
  323. {
  324. paravirt_release_pd(__pa(pmd) >> PAGE_SHIFT);
  325. tlb_remove_page(tlb, virt_to_page(pmd));
  326. }
  327. #endif