powerpc/mm: Validate address values against different region limits
[linux-2.6-block.git] / arch / powerpc / mm / pgtable_64.c
CommitLineData
14cf11af
PM
1/*
2 * This file contains ioremap and related functions for 64-bit machines.
3 *
4 * Derived from arch/ppc64/mm/init.c
5 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
6 *
7 * Modifications by Paul Mackerras (PowerMac) (paulus@samba.org)
8 * and Cort Dougan (PReP) (cort@cs.nmt.edu)
9 * Copyright (C) 1996 Paul Mackerras
14cf11af
PM
10 *
11 * Derived from "arch/i386/mm/init.c"
12 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
13 *
14 * Dave Engebretsen <engebret@us.ibm.com>
15 * Rework for PPC64 port.
16 *
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License
19 * as published by the Free Software Foundation; either version
20 * 2 of the License, or (at your option) any later version.
21 *
22 */
23
14cf11af
PM
24#include <linux/signal.h>
25#include <linux/sched.h>
26#include <linux/kernel.h>
27#include <linux/errno.h>
28#include <linux/string.h>
66b15db6 29#include <linux/export.h>
14cf11af
PM
30#include <linux/types.h>
31#include <linux/mman.h>
32#include <linux/mm.h>
33#include <linux/swap.h>
34#include <linux/stddef.h>
35#include <linux/vmalloc.h>
5a0e3ad6 36#include <linux/slab.h>
06743521 37#include <linux/hugetlb.h>
14cf11af
PM
38
39#include <asm/pgalloc.h>
40#include <asm/page.h>
41#include <asm/prom.h>
14cf11af
PM
42#include <asm/io.h>
43#include <asm/mmu_context.h>
44#include <asm/pgtable.h>
45#include <asm/mmu.h>
14cf11af
PM
46#include <asm/smp.h>
47#include <asm/machdep.h>
48#include <asm/tlb.h>
14cf11af 49#include <asm/processor.h>
14cf11af 50#include <asm/cputable.h>
14cf11af 51#include <asm/sections.h>
5e203d68 52#include <asm/firmware.h>
68cf0d64 53#include <asm/dma.h>
800fc3ee
DG
54
55#include "mmu_decl.h"
14cf11af 56
14cf11af 57
50de596d
AK
58#ifdef CONFIG_PPC_BOOK3S_64
59/*
60 * partition table and process table for ISA 3.0
61 */
62struct prtb_entry *process_tb;
63struct patb_entry *partition_tb;
dd1842a2
AK
64/*
65 * page table size
66 */
67unsigned long __pte_index_size;
68EXPORT_SYMBOL(__pte_index_size);
69unsigned long __pmd_index_size;
70EXPORT_SYMBOL(__pmd_index_size);
71unsigned long __pud_index_size;
72EXPORT_SYMBOL(__pud_index_size);
73unsigned long __pgd_index_size;
74EXPORT_SYMBOL(__pgd_index_size);
fae22116
AK
75unsigned long __pud_cache_index;
76EXPORT_SYMBOL(__pud_cache_index);
dd1842a2
AK
77unsigned long __pte_table_size;
78EXPORT_SYMBOL(__pte_table_size);
79unsigned long __pmd_table_size;
80EXPORT_SYMBOL(__pmd_table_size);
81unsigned long __pud_table_size;
82EXPORT_SYMBOL(__pud_table_size);
83unsigned long __pgd_table_size;
84EXPORT_SYMBOL(__pgd_table_size);
a2f41eb9
AK
85unsigned long __pmd_val_bits;
86EXPORT_SYMBOL(__pmd_val_bits);
87unsigned long __pud_val_bits;
88EXPORT_SYMBOL(__pud_val_bits);
89unsigned long __pgd_val_bits;
90EXPORT_SYMBOL(__pgd_val_bits);
d6a9996e
AK
91unsigned long __kernel_virt_start;
92EXPORT_SYMBOL(__kernel_virt_start);
d6a9996e
AK
93unsigned long __vmalloc_start;
94EXPORT_SYMBOL(__vmalloc_start);
95unsigned long __vmalloc_end;
96EXPORT_SYMBOL(__vmalloc_end);
63ee9b2f
ME
97unsigned long __kernel_io_start;
98EXPORT_SYMBOL(__kernel_io_start);
a35a3c6f
AK
99unsigned long __kernel_io_end;
100EXPORT_SYMBOL(__kernel_io_end);
d6a9996e
AK
101struct page *vmemmap;
102EXPORT_SYMBOL(vmemmap);
5ed7ecd0
AK
103unsigned long __pte_frag_nr;
104EXPORT_SYMBOL(__pte_frag_nr);
105unsigned long __pte_frag_size_shift;
106EXPORT_SYMBOL(__pte_frag_size_shift);
d6a9996e
AK
107unsigned long ioremap_bot;
108#else /* !CONFIG_PPC_BOOK3S_64 */
78f1dbde 109unsigned long ioremap_bot = IOREMAP_BASE;
d6a9996e 110#endif
a245067e 111
3d5134ee
BH
112/**
113 * __ioremap_at - Low level function to establish the page tables
114 * for an IO mapping
115 */
c766ee72 116void __iomem *__ioremap_at(phys_addr_t pa, void *ea, unsigned long size, pgprot_t prot)
14cf11af
PM
117{
118 unsigned long i;
119
a1f242ff 120 /* We don't support the 4K PFN hack with ioremap */
c766ee72 121 if (pgprot_val(prot) & H_PAGE_4K_PFN)
a1f242ff
BH
122 return NULL;
123
e0909392
AK
124 if ((ea + size) >= (void *)IOREMAP_END) {
125 pr_warn("Outisde the supported range\n");
126 return NULL;
127 }
128
3d5134ee
BH
129 WARN_ON(pa & ~PAGE_MASK);
130 WARN_ON(((unsigned long)ea) & ~PAGE_MASK);
131 WARN_ON(size & ~PAGE_MASK);
132
14cf11af 133 for (i = 0; i < size; i += PAGE_SIZE)
c766ee72 134 if (map_kernel_page((unsigned long)ea + i, pa + i, prot))
14cf11af
PM
135 return NULL;
136
3d5134ee
BH
137 return (void __iomem *)ea;
138}
139
140/**
141 * __iounmap_from - Low level function to tear down the page tables
142 * for an IO mapping. This is used for mappings that
143 * are manipulated manually, like partial unmapping of
144 * PCI IOs or ISA space.
145 */
146void __iounmap_at(void *ea, unsigned long size)
147{
148 WARN_ON(((unsigned long)ea) & ~PAGE_MASK);
149 WARN_ON(size & ~PAGE_MASK);
150
151 unmap_kernel_range((unsigned long)ea, size);
14cf11af
PM
152}
153
1cdab55d 154void __iomem * __ioremap_caller(phys_addr_t addr, unsigned long size,
c766ee72 155 pgprot_t prot, void *caller)
14cf11af 156{
3d5134ee 157 phys_addr_t paligned;
14cf11af
PM
158 void __iomem *ret;
159
160 /*
161 * Choose an address to map it to.
162 * Once the imalloc system is running, we use it.
163 * Before that, we map using addresses going
164 * up from ioremap_bot. imalloc will use
165 * the addresses from ioremap_bot through
166 * IMALLOC_END
167 *
168 */
3d5134ee
BH
169 paligned = addr & PAGE_MASK;
170 size = PAGE_ALIGN(addr + size) - paligned;
14cf11af 171
3d5134ee 172 if ((size == 0) || (paligned == 0))
14cf11af
PM
173 return NULL;
174
f691fa10 175 if (slab_is_available()) {
14cf11af 176 struct vm_struct *area;
3d5134ee 177
1cdab55d
BH
178 area = __get_vm_area_caller(size, VM_IOREMAP,
179 ioremap_bot, IOREMAP_END,
180 caller);
14cf11af
PM
181 if (area == NULL)
182 return NULL;
7a9d1256
ME
183
184 area->phys_addr = paligned;
c766ee72 185 ret = __ioremap_at(paligned, area->addr, size, prot);
14cf11af 186 if (!ret)
3d5134ee 187 vunmap(area->addr);
14cf11af 188 } else {
c766ee72 189 ret = __ioremap_at(paligned, (void *)ioremap_bot, size, prot);
14cf11af
PM
190 if (ret)
191 ioremap_bot += size;
192 }
3d5134ee
BH
193
194 if (ret)
195 ret += addr & ~PAGE_MASK;
14cf11af
PM
196 return ret;
197}
198
1cdab55d
BH
199void __iomem * __ioremap(phys_addr_t addr, unsigned long size,
200 unsigned long flags)
201{
c766ee72 202 return __ioremap_caller(addr, size, __pgprot(flags), __builtin_return_address(0));
1cdab55d 203}
4cb3cee0 204
68a64357 205void __iomem * ioremap(phys_addr_t addr, unsigned long size)
4cb3cee0 206{
c766ee72 207 pgprot_t prot = pgprot_noncached(PAGE_KERNEL);
1cdab55d 208 void *caller = __builtin_return_address(0);
4cb3cee0
BH
209
210 if (ppc_md.ioremap)
c766ee72
CL
211 return ppc_md.ioremap(addr, size, prot, caller);
212 return __ioremap_caller(addr, size, prot, caller);
4cb3cee0
BH
213}
214
be135f40
AB
215void __iomem * ioremap_wc(phys_addr_t addr, unsigned long size)
216{
c766ee72 217 pgprot_t prot = pgprot_noncached_wc(PAGE_KERNEL);
be135f40
AB
218 void *caller = __builtin_return_address(0);
219
220 if (ppc_md.ioremap)
c766ee72
CL
221 return ppc_md.ioremap(addr, size, prot, caller);
222 return __ioremap_caller(addr, size, prot, caller);
be135f40
AB
223}
224
86c391bd
CL
225void __iomem *ioremap_coherent(phys_addr_t addr, unsigned long size)
226{
c766ee72 227 pgprot_t prot = pgprot_cached(PAGE_KERNEL);
86c391bd
CL
228 void *caller = __builtin_return_address(0);
229
230 if (ppc_md.ioremap)
c766ee72
CL
231 return ppc_md.ioremap(addr, size, prot, caller);
232 return __ioremap_caller(addr, size, prot, caller);
86c391bd
CL
233}
234
40f1ce7f 235void __iomem * ioremap_prot(phys_addr_t addr, unsigned long size,
4cb3cee0
BH
236 unsigned long flags)
237{
26973fa5 238 pte_t pte = __pte(flags);
1cdab55d
BH
239 void *caller = __builtin_return_address(0);
240
a1f242ff 241 /* writeable implies dirty for kernel addresses */
26973fa5
CL
242 if (pte_write(pte))
243 pte = pte_mkdirty(pte);
a1f242ff 244
ac29c640 245 /* we don't want to let _PAGE_EXEC leak out */
26973fa5 246 pte = pte_exprotect(pte);
ac29c640
AK
247 /*
248 * Force kernel mapping.
249 */
26973fa5 250 pte = pte_mkprivileged(pte);
55052eec 251
4cb3cee0 252 if (ppc_md.ioremap)
26973fa5
CL
253 return ppc_md.ioremap(addr, size, pte_pgprot(pte), caller);
254 return __ioremap_caller(addr, size, pte_pgprot(pte), caller);
4cb3cee0
BH
255}
256
257
14cf11af
PM
258/*
259 * Unmap an IO region and remove it from imalloc'd list.
260 * Access to IO memory should be serialized by driver.
14cf11af 261 */
68a64357 262void __iounmap(volatile void __iomem *token)
14cf11af
PM
263{
264 void *addr;
265
f691fa10 266 if (!slab_is_available())
14cf11af
PM
267 return;
268
3d5134ee
BH
269 addr = (void *) ((unsigned long __force)
270 PCI_FIX_ADDR(token) & PAGE_MASK);
271 if ((unsigned long)addr < ioremap_bot) {
272 printk(KERN_WARNING "Attempt to iounmap early bolted mapping"
273 " at 0x%p\n", addr);
274 return;
275 }
276 vunmap(addr);
14cf11af
PM
277}
278
68a64357 279void iounmap(volatile void __iomem *token)
4cb3cee0
BH
280{
281 if (ppc_md.iounmap)
282 ppc_md.iounmap(token);
283 else
284 __iounmap(token);
285}
286
14cf11af 287EXPORT_SYMBOL(ioremap);
be135f40 288EXPORT_SYMBOL(ioremap_wc);
40f1ce7f 289EXPORT_SYMBOL(ioremap_prot);
14cf11af 290EXPORT_SYMBOL(__ioremap);
a302cb9d 291EXPORT_SYMBOL(__ioremap_at);
14cf11af 292EXPORT_SYMBOL(iounmap);
4cb3cee0 293EXPORT_SYMBOL(__iounmap);
a302cb9d 294EXPORT_SYMBOL(__iounmap_at);
5c1f6ee9 295
06743521
AK
296#ifndef __PAGETABLE_PUD_FOLDED
297/* 4 level page table */
298struct page *pgd_page(pgd_t pgd)
299{
300 if (pgd_huge(pgd))
301 return pte_page(pgd_pte(pgd));
302 return virt_to_page(pgd_page_vaddr(pgd));
303}
304#endif
305
306struct page *pud_page(pud_t pud)
307{
308 if (pud_huge(pud))
309 return pte_page(pud_pte(pud));
310 return virt_to_page(pud_page_vaddr(pud));
311}
312
074c2eae
AK
313/*
314 * For hugepage we have pfn in the pmd, we use PTE_RPN_SHIFT bits for flags
315 * For PTE page, we have a PTE_FRAG_SIZE (4K) aligned virtual address.
316 */
317struct page *pmd_page(pmd_t pmd)
318{
ae28f17b 319 if (pmd_large(pmd) || pmd_huge(pmd) || pmd_devmap(pmd))
e34aa03c 320 return pte_page(pmd_pte(pmd));
074c2eae
AK
321 return virt_to_page(pmd_page_vaddr(pmd));
322}
323
cd65d697
BS
324#ifdef CONFIG_STRICT_KERNEL_RWX
325void mark_rodata_ro(void)
326{
327 if (!mmu_has_feature(MMU_FTR_KERNEL_RO)) {
328 pr_warn("Warning: Unable to mark rodata read only on this CPU.\n");
329 return;
330 }
331
7614ff32
BS
332 if (radix_enabled())
333 radix__mark_rodata_ro();
334 else
cd65d697
BS
335 hash__mark_rodata_ro();
336}
029d9252
ME
337
338void mark_initmem_nx(void)
339{
340 if (radix_enabled())
341 radix__mark_initmem_nx();
342 else
343 hash__mark_initmem_nx();
344}
cd65d697 345#endif