mem64.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * PowerPC version
  3. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  4. *
  5. * Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
  6. * and Cort Dougan (PReP) (cort@cs.nmt.edu)
  7. * Copyright (C) 1996 Paul Mackerras
  8. * Amiga/APUS changes by Jesper Skov (jskov@cygnus.co.uk).
  9. *
  10. * Derived from "arch/i386/mm/init.c"
  11. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  12. *
  13. * Dave Engebretsen <engebret@us.ibm.com>
  14. * Rework for PPC64 port.
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License
  18. * as published by the Free Software Foundation; either version
  19. * 2 of the License, or (at your option) any later version.
  20. *
  21. */
  22. #include <linux/config.h>
  23. #include <linux/signal.h>
  24. #include <linux/sched.h>
  25. #include <linux/kernel.h>
  26. #include <linux/errno.h>
  27. #include <linux/string.h>
  28. #include <linux/types.h>
  29. #include <linux/mman.h>
  30. #include <linux/mm.h>
  31. #include <linux/swap.h>
  32. #include <linux/stddef.h>
  33. #include <linux/vmalloc.h>
  34. #include <linux/init.h>
  35. #include <linux/delay.h>
  36. #include <linux/bootmem.h>
  37. #include <linux/highmem.h>
  38. #include <linux/idr.h>
  39. #include <linux/nodemask.h>
  40. #include <linux/module.h>
  41. #include <asm/pgalloc.h>
  42. #include <asm/page.h>
  43. #include <asm/prom.h>
  44. #include <asm/lmb.h>
  45. #include <asm/rtas.h>
  46. #include <asm/io.h>
  47. #include <asm/mmu_context.h>
  48. #include <asm/pgtable.h>
  49. #include <asm/mmu.h>
  50. #include <asm/uaccess.h>
  51. #include <asm/smp.h>
  52. #include <asm/machdep.h>
  53. #include <asm/tlb.h>
  54. #include <asm/eeh.h>
  55. #include <asm/processor.h>
  56. #include <asm/mmzone.h>
  57. #include <asm/cputable.h>
  58. #include <asm/ppcdebug.h>
  59. #include <asm/sections.h>
  60. #include <asm/system.h>
  61. #include <asm/iommu.h>
  62. #include <asm/abs_addr.h>
  63. #include <asm/vdso.h>
  64. #include <asm/imalloc.h>
  65. /*
  66. * This is called by /dev/mem to know if a given address has to
  67. * be mapped non-cacheable or not
  68. */
  69. int page_is_ram(unsigned long pfn)
  70. {
  71. int i;
  72. unsigned long paddr = (pfn << PAGE_SHIFT);
  73. for (i=0; i < lmb.memory.cnt; i++) {
  74. unsigned long base;
  75. base = lmb.memory.region[i].base;
  76. if ((paddr >= base) &&
  77. (paddr < (base + lmb.memory.region[i].size))) {
  78. return 1;
  79. }
  80. }
  81. return 0;
  82. }
  83. EXPORT_SYMBOL(page_is_ram);
  84. pgprot_t phys_mem_access_prot(struct file *file, unsigned long addr,
  85. unsigned long size, pgprot_t vma_prot)
  86. {
  87. if (ppc_md.phys_mem_access_prot)
  88. return ppc_md.phys_mem_access_prot(file, addr, size, vma_prot);
  89. if (!page_is_ram(addr >> PAGE_SHIFT))
  90. vma_prot = __pgprot(pgprot_val(vma_prot)
  91. | _PAGE_GUARDED | _PAGE_NO_CACHE);
  92. return vma_prot;
  93. }
  94. EXPORT_SYMBOL(phys_mem_access_prot);
  95. void show_mem(void)
  96. {
  97. unsigned long total = 0, reserved = 0;
  98. unsigned long shared = 0, cached = 0;
  99. struct page *page;
  100. pg_data_t *pgdat;
  101. unsigned long i;
  102. printk("Mem-info:\n");
  103. show_free_areas();
  104. printk("Free swap: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
  105. for_each_pgdat(pgdat) {
  106. for (i = 0; i < pgdat->node_spanned_pages; i++) {
  107. page = pgdat_page_nr(pgdat, i);
  108. total++;
  109. if (PageReserved(page))
  110. reserved++;
  111. else if (PageSwapCache(page))
  112. cached++;
  113. else if (page_count(page))
  114. shared += page_count(page) - 1;
  115. }
  116. }
  117. printk("%ld pages of RAM\n", total);
  118. printk("%ld reserved pages\n", reserved);
  119. printk("%ld pages shared\n", shared);
  120. printk("%ld pages swap cached\n", cached);
  121. }
  122. /*
  123. * This is called when a page has been modified by the kernel.
  124. * It just marks the page as not i-cache clean. We do the i-cache
  125. * flush later when the page is given to a user process, if necessary.
  126. */
  127. void flush_dcache_page(struct page *page)
  128. {
  129. if (cpu_has_feature(CPU_FTR_COHERENT_ICACHE))
  130. return;
  131. /* avoid an atomic op if possible */
  132. if (test_bit(PG_arch_1, &page->flags))
  133. clear_bit(PG_arch_1, &page->flags);
  134. }
  135. EXPORT_SYMBOL(flush_dcache_page);
  136. void clear_user_page(void *page, unsigned long vaddr, struct page *pg)
  137. {
  138. clear_page(page);
  139. if (cpu_has_feature(CPU_FTR_COHERENT_ICACHE))
  140. return;
  141. /*
  142. * We shouldnt have to do this, but some versions of glibc
  143. * require it (ld.so assumes zero filled pages are icache clean)
  144. * - Anton
  145. */
  146. /* avoid an atomic op if possible */
  147. if (test_bit(PG_arch_1, &pg->flags))
  148. clear_bit(PG_arch_1, &pg->flags);
  149. }
  150. EXPORT_SYMBOL(clear_user_page);
  151. void copy_user_page(void *vto, void *vfrom, unsigned long vaddr,
  152. struct page *pg)
  153. {
  154. copy_page(vto, vfrom);
  155. /*
  156. * We should be able to use the following optimisation, however
  157. * there are two problems.
  158. * Firstly a bug in some versions of binutils meant PLT sections
  159. * were not marked executable.
  160. * Secondly the first word in the GOT section is blrl, used
  161. * to establish the GOT address. Until recently the GOT was
  162. * not marked executable.
  163. * - Anton
  164. */
  165. #if 0
  166. if (!vma->vm_file && ((vma->vm_flags & VM_EXEC) == 0))
  167. return;
  168. #endif
  169. if (cpu_has_feature(CPU_FTR_COHERENT_ICACHE))
  170. return;
  171. /* avoid an atomic op if possible */
  172. if (test_bit(PG_arch_1, &pg->flags))
  173. clear_bit(PG_arch_1, &pg->flags);
  174. }
  175. void flush_icache_user_range(struct vm_area_struct *vma, struct page *page,
  176. unsigned long addr, int len)
  177. {
  178. unsigned long maddr;
  179. maddr = (unsigned long)page_address(page) + (addr & ~PAGE_MASK);
  180. flush_icache_range(maddr, maddr + len);
  181. }
  182. EXPORT_SYMBOL(flush_icache_user_range);
  183. /*
  184. * This is called at the end of handling a user page fault, when the
  185. * fault has been handled by updating a PTE in the linux page tables.
  186. * We use it to preload an HPTE into the hash table corresponding to
  187. * the updated linux PTE.
  188. *
  189. * This must always be called with the mm->page_table_lock held
  190. */
  191. void update_mmu_cache(struct vm_area_struct *vma, unsigned long ea,
  192. pte_t pte)
  193. {
  194. unsigned long vsid;
  195. void *pgdir;
  196. pte_t *ptep;
  197. int local = 0;
  198. cpumask_t tmp;
  199. unsigned long flags;
  200. /* handle i-cache coherency */
  201. if (!cpu_has_feature(CPU_FTR_COHERENT_ICACHE) &&
  202. !cpu_has_feature(CPU_FTR_NOEXECUTE)) {
  203. unsigned long pfn = pte_pfn(pte);
  204. if (pfn_valid(pfn)) {
  205. struct page *page = pfn_to_page(pfn);
  206. if (!PageReserved(page)
  207. && !test_bit(PG_arch_1, &page->flags)) {
  208. __flush_dcache_icache(page_address(page));
  209. set_bit(PG_arch_1, &page->flags);
  210. }
  211. }
  212. }
  213. /* We only want HPTEs for linux PTEs that have _PAGE_ACCESSED set */
  214. if (!pte_young(pte))
  215. return;
  216. pgdir = vma->vm_mm->pgd;
  217. if (pgdir == NULL)
  218. return;
  219. ptep = find_linux_pte(pgdir, ea);
  220. if (!ptep)
  221. return;
  222. vsid = get_vsid(vma->vm_mm->context.id, ea);
  223. local_irq_save(flags);
  224. tmp = cpumask_of_cpu(smp_processor_id());
  225. if (cpus_equal(vma->vm_mm->cpu_vm_mask, tmp))
  226. local = 1;
  227. __hash_page(ea, pte_val(pte) & (_PAGE_USER|_PAGE_RW), vsid, ptep,
  228. 0x300, local);
  229. local_irq_restore(flags);
  230. }