pgtable_32.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * This file contains the routines setting up the linux page tables.
  3. * -- paulus
  4. *
  5. * Derived from arch/ppc/mm/init.c:
  6. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  7. *
  8. * Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
  9. * and Cort Dougan (PReP) (cort@cs.nmt.edu)
  10. * Copyright (C) 1996 Paul Mackerras
  11. *
  12. * Derived from "arch/i386/mm/init.c"
  13. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * as published by the Free Software Foundation; either version
  18. * 2 of the License, or (at your option) any later version.
  19. *
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/types.h>
  24. #include <linux/mm.h>
  25. #include <linux/vmalloc.h>
  26. #include <linux/init.h>
  27. #include <linux/highmem.h>
  28. #include <linux/memblock.h>
  29. #include <linux/slab.h>
  30. #include <asm/pgtable.h>
  31. #include <asm/pgalloc.h>
  32. #include <asm/fixmap.h>
  33. #include <asm/io.h>
  34. #include <asm/setup.h>
  35. #include <asm/sections.h>
  36. #include "mmu_decl.h"
  37. unsigned long ioremap_bot;
  38. EXPORT_SYMBOL(ioremap_bot); /* aka VMALLOC_END */
  39. extern char etext[], _stext[], _sinittext[], _einittext[];
  40. __ref pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
  41. {
  42. pte_t *pte;
  43. if (slab_is_available()) {
  44. pte = (pte_t *)__get_free_page(GFP_KERNEL|__GFP_ZERO);
  45. } else {
  46. pte = __va(memblock_alloc(PAGE_SIZE, PAGE_SIZE));
  47. if (pte)
  48. clear_page(pte);
  49. }
  50. return pte;
  51. }
  52. pgtable_t pte_alloc_one(struct mm_struct *mm, unsigned long address)
  53. {
  54. struct page *ptepage;
  55. gfp_t flags = GFP_KERNEL | __GFP_ZERO | __GFP_ACCOUNT;
  56. ptepage = alloc_pages(flags, 0);
  57. if (!ptepage)
  58. return NULL;
  59. if (!pgtable_page_ctor(ptepage)) {
  60. __free_page(ptepage);
  61. return NULL;
  62. }
  63. return ptepage;
  64. }
  65. void __iomem *
  66. ioremap(phys_addr_t addr, unsigned long size)
  67. {
  68. return __ioremap_caller(addr, size, _PAGE_NO_CACHE | _PAGE_GUARDED,
  69. __builtin_return_address(0));
  70. }
  71. EXPORT_SYMBOL(ioremap);
  72. void __iomem *
  73. ioremap_wc(phys_addr_t addr, unsigned long size)
  74. {
  75. return __ioremap_caller(addr, size, _PAGE_NO_CACHE,
  76. __builtin_return_address(0));
  77. }
  78. EXPORT_SYMBOL(ioremap_wc);
  79. void __iomem *
  80. ioremap_prot(phys_addr_t addr, unsigned long size, unsigned long flags)
  81. {
  82. /* writeable implies dirty for kernel addresses */
  83. if ((flags & (_PAGE_RW | _PAGE_RO)) != _PAGE_RO)
  84. flags |= _PAGE_DIRTY | _PAGE_HWWRITE;
  85. /* we don't want to let _PAGE_USER and _PAGE_EXEC leak out */
  86. flags &= ~(_PAGE_USER | _PAGE_EXEC);
  87. flags |= _PAGE_PRIVILEGED;
  88. return __ioremap_caller(addr, size, flags, __builtin_return_address(0));
  89. }
  90. EXPORT_SYMBOL(ioremap_prot);
  91. void __iomem *
  92. __ioremap(phys_addr_t addr, unsigned long size, unsigned long flags)
  93. {
  94. return __ioremap_caller(addr, size, flags, __builtin_return_address(0));
  95. }
  96. void __iomem *
  97. __ioremap_caller(phys_addr_t addr, unsigned long size, unsigned long flags,
  98. void *caller)
  99. {
  100. unsigned long v, i;
  101. phys_addr_t p;
  102. int err;
  103. /* Make sure we have the base flags */
  104. if ((flags & _PAGE_PRESENT) == 0)
  105. flags |= pgprot_val(PAGE_KERNEL);
  106. /* Non-cacheable page cannot be coherent */
  107. if (flags & _PAGE_NO_CACHE)
  108. flags &= ~_PAGE_COHERENT;
  109. /*
  110. * Choose an address to map it to.
  111. * Once the vmalloc system is running, we use it.
  112. * Before then, we use space going down from IOREMAP_TOP
  113. * (ioremap_bot records where we're up to).
  114. */
  115. p = addr & PAGE_MASK;
  116. size = PAGE_ALIGN(addr + size) - p;
  117. /*
  118. * If the address lies within the first 16 MB, assume it's in ISA
  119. * memory space
  120. */
  121. if (p < 16*1024*1024)
  122. p += _ISA_MEM_BASE;
  123. #ifndef CONFIG_CRASH_DUMP
  124. /*
  125. * Don't allow anybody to remap normal RAM that we're using.
  126. * mem_init() sets high_memory so only do the check after that.
  127. */
  128. if (slab_is_available() && (p < virt_to_phys(high_memory)) &&
  129. page_is_ram(__phys_to_pfn(p))) {
  130. printk("__ioremap(): phys addr 0x%llx is RAM lr %ps\n",
  131. (unsigned long long)p, __builtin_return_address(0));
  132. return NULL;
  133. }
  134. #endif
  135. if (size == 0)
  136. return NULL;
  137. /*
  138. * Is it already mapped? Perhaps overlapped by a previous
  139. * mapping.
  140. */
  141. v = p_block_mapped(p);
  142. if (v)
  143. goto out;
  144. if (slab_is_available()) {
  145. struct vm_struct *area;
  146. area = get_vm_area_caller(size, VM_IOREMAP, caller);
  147. if (area == 0)
  148. return NULL;
  149. area->phys_addr = p;
  150. v = (unsigned long) area->addr;
  151. } else {
  152. v = (ioremap_bot -= size);
  153. }
  154. /*
  155. * Should check if it is a candidate for a BAT mapping
  156. */
  157. err = 0;
  158. for (i = 0; i < size && err == 0; i += PAGE_SIZE)
  159. err = map_kernel_page(v+i, p+i, flags);
  160. if (err) {
  161. if (slab_is_available())
  162. vunmap((void *)v);
  163. return NULL;
  164. }
  165. out:
  166. return (void __iomem *) (v + ((unsigned long)addr & ~PAGE_MASK));
  167. }
  168. EXPORT_SYMBOL(__ioremap);
  169. void iounmap(volatile void __iomem *addr)
  170. {
  171. /*
  172. * If mapped by BATs then there is nothing to do.
  173. * Calling vfree() generates a benign warning.
  174. */
  175. if (v_block_mapped((unsigned long)addr))
  176. return;
  177. if (addr > high_memory && (unsigned long) addr < ioremap_bot)
  178. vunmap((void *) (PAGE_MASK & (unsigned long)addr));
  179. }
  180. EXPORT_SYMBOL(iounmap);
  181. int map_kernel_page(unsigned long va, phys_addr_t pa, int flags)
  182. {
  183. pmd_t *pd;
  184. pte_t *pg;
  185. int err = -ENOMEM;
  186. /* Use upper 10 bits of VA to index the first level map */
  187. pd = pmd_offset(pud_offset(pgd_offset_k(va), va), va);
  188. /* Use middle 10 bits of VA to index the second-level map */
  189. pg = pte_alloc_kernel(pd, va);
  190. if (pg != 0) {
  191. err = 0;
  192. /* The PTE should never be already set nor present in the
  193. * hash table
  194. */
  195. BUG_ON((pte_val(*pg) & (_PAGE_PRESENT | _PAGE_HASHPTE)) &&
  196. flags);
  197. set_pte_at(&init_mm, va, pg, pfn_pte(pa >> PAGE_SHIFT,
  198. __pgprot(flags)));
  199. }
  200. smp_wmb();
  201. return err;
  202. }
  203. /*
  204. * Map in a chunk of physical memory starting at start.
  205. */
  206. static void __init __mapin_ram_chunk(unsigned long offset, unsigned long top)
  207. {
  208. unsigned long v, s, f;
  209. phys_addr_t p;
  210. int ktext;
  211. s = offset;
  212. v = PAGE_OFFSET + s;
  213. p = memstart_addr + s;
  214. for (; s < top; s += PAGE_SIZE) {
  215. ktext = ((char *)v >= _stext && (char *)v < etext) ||
  216. ((char *)v >= _sinittext && (char *)v < _einittext);
  217. f = ktext ? pgprot_val(PAGE_KERNEL_TEXT) : pgprot_val(PAGE_KERNEL);
  218. map_kernel_page(v, p, f);
  219. #ifdef CONFIG_PPC_STD_MMU_32
  220. if (ktext)
  221. hash_preload(&init_mm, v, 0, 0x300);
  222. #endif
  223. v += PAGE_SIZE;
  224. p += PAGE_SIZE;
  225. }
  226. }
  227. void __init mapin_ram(void)
  228. {
  229. unsigned long s, top;
  230. #ifndef CONFIG_WII
  231. top = total_lowmem;
  232. s = mmu_mapin_ram(top);
  233. __mapin_ram_chunk(s, top);
  234. #else
  235. if (!wii_hole_size) {
  236. s = mmu_mapin_ram(total_lowmem);
  237. __mapin_ram_chunk(s, total_lowmem);
  238. } else {
  239. top = wii_hole_start;
  240. s = mmu_mapin_ram(top);
  241. __mapin_ram_chunk(s, top);
  242. top = memblock_end_of_DRAM();
  243. s = wii_mmu_mapin_mem2(top);
  244. __mapin_ram_chunk(s, top);
  245. }
  246. #endif
  247. }
  248. /* Scan the real Linux page tables and return a PTE pointer for
  249. * a virtual address in a context.
  250. * Returns true (1) if PTE was found, zero otherwise. The pointer to
  251. * the PTE pointer is unmodified if PTE is not found.
  252. */
  253. static int
  254. get_pteptr(struct mm_struct *mm, unsigned long addr, pte_t **ptep, pmd_t **pmdp)
  255. {
  256. pgd_t *pgd;
  257. pud_t *pud;
  258. pmd_t *pmd;
  259. pte_t *pte;
  260. int retval = 0;
  261. pgd = pgd_offset(mm, addr & PAGE_MASK);
  262. if (pgd) {
  263. pud = pud_offset(pgd, addr & PAGE_MASK);
  264. if (pud && pud_present(*pud)) {
  265. pmd = pmd_offset(pud, addr & PAGE_MASK);
  266. if (pmd_present(*pmd)) {
  267. pte = pte_offset_map(pmd, addr & PAGE_MASK);
  268. if (pte) {
  269. retval = 1;
  270. *ptep = pte;
  271. if (pmdp)
  272. *pmdp = pmd;
  273. /* XXX caller needs to do pte_unmap, yuck */
  274. }
  275. }
  276. }
  277. }
  278. return(retval);
  279. }
  280. static int __change_page_attr_noflush(struct page *page, pgprot_t prot)
  281. {
  282. pte_t *kpte;
  283. pmd_t *kpmd;
  284. unsigned long address;
  285. BUG_ON(PageHighMem(page));
  286. address = (unsigned long)page_address(page);
  287. if (v_block_mapped(address))
  288. return 0;
  289. if (!get_pteptr(&init_mm, address, &kpte, &kpmd))
  290. return -EINVAL;
  291. __set_pte_at(&init_mm, address, kpte, mk_pte(page, prot), 0);
  292. pte_unmap(kpte);
  293. return 0;
  294. }
  295. /*
  296. * Change the page attributes of an page in the linear mapping.
  297. *
  298. * THIS DOES NOTHING WITH BAT MAPPINGS, DEBUG USE ONLY
  299. */
  300. static int change_page_attr(struct page *page, int numpages, pgprot_t prot)
  301. {
  302. int i, err = 0;
  303. unsigned long flags;
  304. struct page *start = page;
  305. local_irq_save(flags);
  306. for (i = 0; i < numpages; i++, page++) {
  307. err = __change_page_attr_noflush(page, prot);
  308. if (err)
  309. break;
  310. }
  311. wmb();
  312. local_irq_restore(flags);
  313. flush_tlb_kernel_range((unsigned long)page_address(start),
  314. (unsigned long)page_address(page));
  315. return err;
  316. }
  317. void mark_initmem_nx(void)
  318. {
  319. struct page *page = virt_to_page(_sinittext);
  320. unsigned long numpages = PFN_UP((unsigned long)_einittext) -
  321. PFN_DOWN((unsigned long)_sinittext);
  322. change_page_attr(page, numpages, PAGE_KERNEL);
  323. }
  324. #ifdef CONFIG_STRICT_KERNEL_RWX
  325. void mark_rodata_ro(void)
  326. {
  327. struct page *page;
  328. unsigned long numpages;
  329. page = virt_to_page(_stext);
  330. numpages = PFN_UP((unsigned long)_etext) -
  331. PFN_DOWN((unsigned long)_stext);
  332. change_page_attr(page, numpages, PAGE_KERNEL_ROX);
  333. /*
  334. * mark .rodata as read only. Use __init_begin rather than __end_rodata
  335. * to cover NOTES and EXCEPTION_TABLE.
  336. */
  337. page = virt_to_page(__start_rodata);
  338. numpages = PFN_UP((unsigned long)__init_begin) -
  339. PFN_DOWN((unsigned long)__start_rodata);
  340. change_page_attr(page, numpages, PAGE_KERNEL_RO);
  341. }
  342. #endif
  343. #ifdef CONFIG_DEBUG_PAGEALLOC
  344. void __kernel_map_pages(struct page *page, int numpages, int enable)
  345. {
  346. if (PageHighMem(page))
  347. return;
  348. change_page_attr(page, numpages, enable ? PAGE_KERNEL : __pgprot(0));
  349. }
  350. #endif /* CONFIG_DEBUG_PAGEALLOC */