blob: 05dda641af80838f7eafa972f0b71064c9e21f48 [file] [log] [blame]
David Howellsb920de12008-02-08 04:19:31 -08001/* MN10300 Page table manipulators and constants
2 *
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
10 *
11 *
12 * The Linux memory management assumes a three-level page table setup. On
13 * the i386, we use that, but "fold" the mid level into the top-level page
14 * table, so that we physically have the same two-level page table as the
15 * i386 mmu expects.
16 *
17 * This file contains the functions and defines necessary to modify and use
18 * the i386 page table tree for the purposes of the MN10300 TLB handler
19 * functions.
20 */
21#ifndef _ASM_PGTABLE_H
22#define _ASM_PGTABLE_H
23
24#include <asm/cpu-regs.h>
25
26#ifndef __ASSEMBLY__
27#include <asm/processor.h>
28#include <asm/cache.h>
29#include <linux/threads.h>
30
31#include <asm/bitops.h>
32
33#include <linux/slab.h>
34#include <linux/list.h>
35#include <linux/spinlock.h>
36
37/*
38 * ZERO_PAGE is a global shared page that is always zero: used
39 * for zero-mapped memory areas etc..
40 */
41#define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))
42extern unsigned long empty_zero_page[1024];
43extern spinlock_t pgd_lock;
44extern struct page *pgd_list;
45
46extern void pmd_ctor(void *, struct kmem_cache *, unsigned long);
47extern void pgtable_cache_init(void);
48extern void paging_init(void);
49
50#endif /* !__ASSEMBLY__ */
51
52/*
53 * The Linux mn10300 paging architecture only implements both the traditional
54 * 2-level page tables
55 */
56#define PGDIR_SHIFT 22
57#define PTRS_PER_PGD 1024
58#define PTRS_PER_PUD 1 /* we don't really have any PUD physically */
59#define PTRS_PER_PMD 1 /* we don't really have any PMD physically */
60#define PTRS_PER_PTE 1024
61
62#define PGD_SIZE PAGE_SIZE
63#define PMD_SIZE (1UL << PMD_SHIFT)
64#define PGDIR_SIZE (1UL << PGDIR_SHIFT)
65#define PGDIR_MASK (~(PGDIR_SIZE - 1))
66
67#define USER_PTRS_PER_PGD (TASK_SIZE / PGDIR_SIZE)
68#define FIRST_USER_ADDRESS 0
69
70#define USER_PGD_PTRS (PAGE_OFFSET >> PGDIR_SHIFT)
71#define KERNEL_PGD_PTRS (PTRS_PER_PGD - USER_PGD_PTRS)
72
73#define TWOLEVEL_PGDIR_SHIFT 22
74#define BOOT_USER_PGD_PTRS (__PAGE_OFFSET >> TWOLEVEL_PGDIR_SHIFT)
75#define BOOT_KERNEL_PGD_PTRS (1024 - BOOT_USER_PGD_PTRS)
76
77#ifndef __ASSEMBLY__
78extern pgd_t swapper_pg_dir[PTRS_PER_PGD];
79#endif
80
81/*
82 * Unfortunately, due to the way the MMU works on the MN10300, the vmalloc VM
83 * area has to be in the lower half of the virtual address range (the upper
84 * half is not translated through the TLB).
85 *
86 * So in this case, the vmalloc area goes at the bottom of the address map
87 * (leaving a hole at the very bottom to catch addressing errors), and
88 * userspace starts immediately above.
89 *
90 * The vmalloc() routines also leaves a hole of 4kB between each vmalloced
91 * area to catch addressing errors.
92 */
93#define VMALLOC_OFFSET (8 * 1024 * 1024)
94#define VMALLOC_START (0x70000000)
95#define VMALLOC_END (0x7C000000)
96
97#ifndef __ASSEMBLY__
98extern pte_t kernel_vmalloc_ptes[(VMALLOC_END - VMALLOC_START) / PAGE_SIZE];
99#endif
100
Akira Takeuchidccbf482010-10-27 17:28:50 +0100101/* IPTEL2/DPTEL2 bit assignments */
102#define _PAGE_BIT_VALID xPTEL2_V_BIT
103#define _PAGE_BIT_CACHE xPTEL2_C_BIT
104#define _PAGE_BIT_PRESENT xPTEL2_PV_BIT
105#define _PAGE_BIT_DIRTY xPTEL2_D_BIT
106#define _PAGE_BIT_GLOBAL xPTEL2_G_BIT
107#define _PAGE_BIT_ACCESSED xPTEL2_UNUSED1_BIT /* mustn't be loaded into IPTEL2/DPTEL2 */
David Howellsb920de12008-02-08 04:19:31 -0800108
Akira Takeuchidccbf482010-10-27 17:28:50 +0100109#define _PAGE_VALID xPTEL2_V
110#define _PAGE_CACHE xPTEL2_C
111#define _PAGE_PRESENT xPTEL2_PV
112#define _PAGE_DIRTY xPTEL2_D
113#define _PAGE_PROT xPTEL2_PR
114#define _PAGE_PROT_RKNU xPTEL2_PR_ROK
115#define _PAGE_PROT_WKNU xPTEL2_PR_RWK
116#define _PAGE_PROT_RKRU xPTEL2_PR_ROK_ROU
117#define _PAGE_PROT_WKRU xPTEL2_PR_RWK_ROU
118#define _PAGE_PROT_WKWU xPTEL2_PR_RWK_RWU
119#define _PAGE_GLOBAL xPTEL2_G
120#define _PAGE_PS_MASK xPTEL2_PS
121#define _PAGE_PS_4Kb xPTEL2_PS_4Kb
122#define _PAGE_PS_128Kb xPTEL2_PS_128Kb
123#define _PAGE_PS_1Kb xPTEL2_PS_1Kb
124#define _PAGE_PS_4Mb xPTEL2_PS_4Mb
125#define _PAGE_PSE xPTEL2_PS_4Mb /* 4MB page */
126#define _PAGE_CACHE_WT xPTEL2_CWT
127#define _PAGE_ACCESSED xPTEL2_UNUSED1
128#define _PAGE_NX 0 /* no-execute bit */
David Howellsb920de12008-02-08 04:19:31 -0800129
Akira Takeuchidccbf482010-10-27 17:28:50 +0100130/* If _PAGE_VALID is clear, we use these: */
131#define _PAGE_FILE xPTEL2_C /* set:pagecache unset:swap */
132#define _PAGE_PROTNONE 0x000 /* If not present */
David Howellsb920de12008-02-08 04:19:31 -0800133
Akira Takeuchidccbf482010-10-27 17:28:50 +0100134#define __PAGE_PROT_UWAUX 0x010
135#define __PAGE_PROT_USER 0x020
136#define __PAGE_PROT_WRITE 0x040
David Howellsb920de12008-02-08 04:19:31 -0800137
138#define _PAGE_PRESENTV (_PAGE_PRESENT|_PAGE_VALID)
David Howellsb920de12008-02-08 04:19:31 -0800139
140#ifndef __ASSEMBLY__
141
142#define VMALLOC_VMADDR(x) ((unsigned long)(x))
143
144#define _PAGE_TABLE (_PAGE_PRESENTV | _PAGE_PROT_WKNU | _PAGE_ACCESSED | _PAGE_DIRTY)
145#define _PAGE_CHG_MASK (PTE_MASK | _PAGE_ACCESSED | _PAGE_DIRTY)
146
147#define __PAGE_NONE (_PAGE_PRESENTV | _PAGE_PROT_RKNU | _PAGE_ACCESSED | _PAGE_CACHE)
148#define __PAGE_SHARED (_PAGE_PRESENTV | _PAGE_PROT_WKWU | _PAGE_ACCESSED | _PAGE_CACHE)
149#define __PAGE_COPY (_PAGE_PRESENTV | _PAGE_PROT_RKRU | _PAGE_ACCESSED | _PAGE_CACHE)
150#define __PAGE_READONLY (_PAGE_PRESENTV | _PAGE_PROT_RKRU | _PAGE_ACCESSED | _PAGE_CACHE)
151
152#define PAGE_NONE __pgprot(__PAGE_NONE | _PAGE_NX)
153#define PAGE_SHARED_NOEXEC __pgprot(__PAGE_SHARED | _PAGE_NX)
154#define PAGE_COPY_NOEXEC __pgprot(__PAGE_COPY | _PAGE_NX)
155#define PAGE_READONLY_NOEXEC __pgprot(__PAGE_READONLY | _PAGE_NX)
156#define PAGE_SHARED_EXEC __pgprot(__PAGE_SHARED)
157#define PAGE_COPY_EXEC __pgprot(__PAGE_COPY)
158#define PAGE_READONLY_EXEC __pgprot(__PAGE_READONLY)
159#define PAGE_COPY PAGE_COPY_NOEXEC
160#define PAGE_READONLY PAGE_READONLY_NOEXEC
161#define PAGE_SHARED PAGE_SHARED_EXEC
162
163#define __PAGE_KERNEL_BASE (_PAGE_PRESENTV | _PAGE_DIRTY | _PAGE_ACCESSED | _PAGE_GLOBAL)
164
165#define __PAGE_KERNEL (__PAGE_KERNEL_BASE | _PAGE_PROT_WKNU | _PAGE_CACHE | _PAGE_NX)
166#define __PAGE_KERNEL_NOCACHE (__PAGE_KERNEL_BASE | _PAGE_PROT_WKNU | _PAGE_NX)
167#define __PAGE_KERNEL_EXEC (__PAGE_KERNEL & ~_PAGE_NX)
168#define __PAGE_KERNEL_RO (__PAGE_KERNEL_BASE | _PAGE_PROT_RKNU | _PAGE_CACHE | _PAGE_NX)
169#define __PAGE_KERNEL_LARGE (__PAGE_KERNEL | _PAGE_PSE)
170#define __PAGE_KERNEL_LARGE_EXEC (__PAGE_KERNEL_EXEC | _PAGE_PSE)
171
172#define PAGE_KERNEL __pgprot(__PAGE_KERNEL)
173#define PAGE_KERNEL_RO __pgprot(__PAGE_KERNEL_RO)
174#define PAGE_KERNEL_EXEC __pgprot(__PAGE_KERNEL_EXEC)
175#define PAGE_KERNEL_NOCACHE __pgprot(__PAGE_KERNEL_NOCACHE)
176#define PAGE_KERNEL_LARGE __pgprot(__PAGE_KERNEL_LARGE)
177#define PAGE_KERNEL_LARGE_EXEC __pgprot(__PAGE_KERNEL_LARGE_EXEC)
178
179/*
180 * Whilst the MN10300 can do page protection for execute (given separate data
181 * and insn TLBs), we are not supporting it at the moment. Write permission,
182 * however, always implies read permission (but not execute permission).
183 */
184#define __P000 PAGE_NONE
185#define __P001 PAGE_READONLY_NOEXEC
186#define __P010 PAGE_COPY_NOEXEC
187#define __P011 PAGE_COPY_NOEXEC
188#define __P100 PAGE_READONLY_EXEC
189#define __P101 PAGE_READONLY_EXEC
190#define __P110 PAGE_COPY_EXEC
191#define __P111 PAGE_COPY_EXEC
192
193#define __S000 PAGE_NONE
194#define __S001 PAGE_READONLY_NOEXEC
195#define __S010 PAGE_SHARED_NOEXEC
196#define __S011 PAGE_SHARED_NOEXEC
197#define __S100 PAGE_READONLY_EXEC
198#define __S101 PAGE_READONLY_EXEC
199#define __S110 PAGE_SHARED_EXEC
200#define __S111 PAGE_SHARED_EXEC
201
202/*
203 * Define this to warn about kernel memory accesses that are
204 * done without a 'verify_area(VERIFY_WRITE,..)'
205 */
206#undef TEST_VERIFY_AREA
207
208#define pte_present(x) (pte_val(x) & _PAGE_VALID)
209#define pte_clear(mm, addr, xp) \
210do { \
211 set_pte_at((mm), (addr), (xp), __pte(0)); \
212} while (0)
213
214#define pmd_none(x) (!pmd_val(x))
215#define pmd_present(x) (!pmd_none(x))
216#define pmd_clear(xp) do { set_pmd(xp, __pmd(0)); } while (0)
217#define pmd_bad(x) 0
218
219
220#define pages_to_mb(x) ((x) >> (20 - PAGE_SHIFT))
221
222#ifndef __ASSEMBLY__
223
224/*
225 * The following only work if pte_present() is true.
226 * Undefined behaviour if not..
227 */
228static inline int pte_user(pte_t pte) { return pte_val(pte) & __PAGE_PROT_USER; }
229static inline int pte_read(pte_t pte) { return pte_val(pte) & __PAGE_PROT_USER; }
230static inline int pte_dirty(pte_t pte) { return pte_val(pte) & _PAGE_DIRTY; }
231static inline int pte_young(pte_t pte) { return pte_val(pte) & _PAGE_ACCESSED; }
232static inline int pte_write(pte_t pte) { return pte_val(pte) & __PAGE_PROT_WRITE; }
Nick Piggin7e675132008-04-28 02:13:00 -0700233static inline int pte_special(pte_t pte){ return 0; }
David Howellsb920de12008-02-08 04:19:31 -0800234
235/*
236 * The following only works if pte_present() is not true.
237 */
238static inline int pte_file(pte_t pte) { return pte_val(pte) & _PAGE_FILE; }
239
240static inline pte_t pte_rdprotect(pte_t pte)
241{
242 pte_val(pte) &= ~(__PAGE_PROT_USER|__PAGE_PROT_UWAUX); return pte;
243}
244static inline pte_t pte_exprotect(pte_t pte)
245{
246 pte_val(pte) |= _PAGE_NX; return pte;
247}
248
249static inline pte_t pte_wrprotect(pte_t pte)
250{
251 pte_val(pte) &= ~(__PAGE_PROT_WRITE|__PAGE_PROT_UWAUX); return pte;
252}
253
254static inline pte_t pte_mkclean(pte_t pte) { pte_val(pte) &= ~_PAGE_DIRTY; return pte; }
255static inline pte_t pte_mkold(pte_t pte) { pte_val(pte) &= ~_PAGE_ACCESSED; return pte; }
256static inline pte_t pte_mkdirty(pte_t pte) { pte_val(pte) |= _PAGE_DIRTY; return pte; }
257static inline pte_t pte_mkyoung(pte_t pte) { pte_val(pte) |= _PAGE_ACCESSED; return pte; }
258static inline pte_t pte_mkexec(pte_t pte) { pte_val(pte) &= ~_PAGE_NX; return pte; }
259
260static inline pte_t pte_mkread(pte_t pte)
261{
262 pte_val(pte) |= __PAGE_PROT_USER;
263 if (pte_write(pte))
264 pte_val(pte) |= __PAGE_PROT_UWAUX;
265 return pte;
266}
267static inline pte_t pte_mkwrite(pte_t pte)
268{
269 pte_val(pte) |= __PAGE_PROT_WRITE;
270 if (pte_val(pte) & __PAGE_PROT_USER)
271 pte_val(pte) |= __PAGE_PROT_UWAUX;
272 return pte;
273}
274
Nick Piggin7e675132008-04-28 02:13:00 -0700275static inline pte_t pte_mkspecial(pte_t pte) { return pte; }
276
David Howellsb920de12008-02-08 04:19:31 -0800277#define pte_ERROR(e) \
278 printk(KERN_ERR "%s:%d: bad pte %08lx.\n", \
279 __FILE__, __LINE__, pte_val(e))
280#define pgd_ERROR(e) \
281 printk(KERN_ERR "%s:%d: bad pgd %08lx.\n", \
282 __FILE__, __LINE__, pgd_val(e))
283
284/*
285 * The "pgd_xxx()" functions here are trivial for a folded two-level
286 * setup: the pgd is never bad, and a pmd always exists (as it's folded
287 * into the pgd entry)
288 */
289#define pgd_clear(xp) do { } while (0)
290
291/*
292 * Certain architectures need to do special things when PTEs
293 * within a page table are directly modified. Thus, the following
294 * hook is made available.
295 */
296#define set_pte(pteptr, pteval) (*(pteptr) = pteval)
297#define set_pte_at(mm, addr, ptep, pteval) set_pte((ptep), (pteval))
298#define set_pte_atomic(pteptr, pteval) set_pte((pteptr), (pteval))
299
300/*
301 * (pmds are folded into pgds so this doesn't get actually called,
302 * but the define is needed for a generic inline function.)
303 */
304#define set_pmd(pmdptr, pmdval) (*(pmdptr) = pmdval)
305
306#define ptep_get_and_clear(mm, addr, ptep) \
307 __pte(xchg(&(ptep)->pte, 0))
308#define pte_same(a, b) (pte_val(a) == pte_val(b))
309#define pte_page(x) pfn_to_page(pte_pfn(x))
310#define pte_none(x) (!pte_val(x))
311#define pte_pfn(x) ((unsigned long) (pte_val(x) >> PAGE_SHIFT))
312#define __pfn_addr(pfn) ((pfn) << PAGE_SHIFT)
313#define pfn_pte(pfn, prot) __pte(__pfn_addr(pfn) | pgprot_val(prot))
314#define pfn_pmd(pfn, prot) __pmd(__pfn_addr(pfn) | pgprot_val(prot))
315
316/*
317 * All present user pages are user-executable:
318 */
319static inline int pte_exec(pte_t pte)
320{
321 return pte_user(pte);
322}
323
324/*
325 * All present pages are kernel-executable:
326 */
327static inline int pte_exec_kernel(pte_t pte)
328{
329 return 1;
330}
331
332/*
333 * Bits 0 and 1 are taken, split up the 29 bits of offset
334 * into this range:
335 */
336#define PTE_FILE_MAX_BITS 29
337
338#define pte_to_pgoff(pte) (pte_val(pte) >> 2)
339#define pgoff_to_pte(off) __pte((off) << 2 | _PAGE_FILE)
340
341/* Encode and de-code a swap entry */
342#define __swp_type(x) (((x).val >> 2) & 0x3f)
343#define __swp_offset(x) ((x).val >> 8)
344#define __swp_entry(type, offset) \
345 ((swp_entry_t) { ((type) << 2) | ((offset) << 8) })
346#define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) })
347#define __swp_entry_to_pte(x) __pte((x).val)
348
349static inline
350int ptep_test_and_clear_dirty(struct vm_area_struct *vma, unsigned long addr,
351 pte_t *ptep)
352{
353 if (!pte_dirty(*ptep))
354 return 0;
355 return test_and_clear_bit(_PAGE_BIT_DIRTY, &ptep->pte);
356}
357
358static inline
359int ptep_test_and_clear_young(struct vm_area_struct *vma, unsigned long addr,
360 pte_t *ptep)
361{
362 if (!pte_young(*ptep))
363 return 0;
364 return test_and_clear_bit(_PAGE_BIT_ACCESSED, &ptep->pte);
365}
366
367static inline
368void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
369{
370 pte_val(*ptep) &= ~(__PAGE_PROT_WRITE|__PAGE_PROT_UWAUX);
371}
372
373static inline void ptep_mkdirty(pte_t *ptep)
374{
375 set_bit(_PAGE_BIT_DIRTY, &ptep->pte);
376}
377
378/*
379 * Macro to mark a page protection value as "uncacheable". On processors which
380 * do not support it, this is a no-op.
381 */
382#define pgprot_noncached(prot) __pgprot(pgprot_val(prot) | _PAGE_CACHE)
383
384
385/*
386 * Conversion functions: convert a page and protection to a page entry,
387 * and a page entry and page directory to the page they refer to.
388 */
389
390#define mk_pte(page, pgprot) pfn_pte(page_to_pfn(page), (pgprot))
391#define mk_pte_huge(entry) \
392 ((entry).pte |= _PAGE_PRESENT | _PAGE_PSE | _PAGE_VALID)
393
394static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
395{
396 pte_val(pte) &= _PAGE_CHG_MASK;
397 pte_val(pte) |= pgprot_val(newprot);
398 return pte;
399}
400
401#define page_pte(page) page_pte_prot((page), __pgprot(0))
402
403#define pmd_page_kernel(pmd) \
404 ((unsigned long) __va(pmd_val(pmd) & PAGE_MASK))
405
406#define pmd_page(pmd) pfn_to_page(pmd_val(pmd) >> PAGE_SHIFT)
407
408#define pmd_large(pmd) \
409 ((pmd_val(pmd) & (_PAGE_PSE | _PAGE_PRESENT)) == \
410 (_PAGE_PSE | _PAGE_PRESENT))
411
412/*
413 * the pgd page can be thought of an array like this: pgd_t[PTRS_PER_PGD]
414 *
415 * this macro returns the index of the entry in the pgd page which would
416 * control the given virtual address
417 */
418#define pgd_index(address) (((address) >> PGDIR_SHIFT) & (PTRS_PER_PGD - 1))
419
420/*
421 * pgd_offset() returns a (pgd_t *)
422 * pgd_index() is used get the offset into the pgd page's array of pgd_t's;
423 */
424#define pgd_offset(mm, address) ((mm)->pgd + pgd_index(address))
425
426/*
427 * a shortcut which implies the use of the kernel's pgd, instead
428 * of a process's
429 */
430#define pgd_offset_k(address) pgd_offset(&init_mm, address)
431
432/*
433 * the pmd page can be thought of an array like this: pmd_t[PTRS_PER_PMD]
434 *
435 * this macro returns the index of the entry in the pmd page which would
436 * control the given virtual address
437 */
438#define pmd_index(address) \
439 (((address) >> PMD_SHIFT) & (PTRS_PER_PMD - 1))
440
441/*
442 * the pte page can be thought of an array like this: pte_t[PTRS_PER_PTE]
443 *
444 * this macro returns the index of the entry in the pte page which would
445 * control the given virtual address
446 */
447#define pte_index(address) \
448 (((address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1))
449
450#define pte_offset_kernel(dir, address) \
451 ((pte_t *) pmd_page_kernel(*(dir)) + pte_index(address))
452
453/*
454 * Make a given kernel text page executable/non-executable.
455 * Returns the previous executability setting of that page (which
456 * is used to restore the previous state). Used by the SMP bootup code.
457 * NOTE: this is an __init function for security reasons.
458 */
459static inline int set_kernel_exec(unsigned long vaddr, int enable)
460{
461 return 0;
462}
463
464#define pte_offset_map(dir, address) \
465 ((pte_t *) page_address(pmd_page(*(dir))) + pte_index(address))
David Howellsb920de12008-02-08 04:19:31 -0800466#define pte_unmap(pte) do {} while (0)
David Howellsb920de12008-02-08 04:19:31 -0800467
468/*
469 * The MN10300 has external MMU info in the form of a TLB: this is adapted from
470 * the kernel page tables containing the necessary information by tlb-mn10300.S
471 */
472extern void update_mmu_cache(struct vm_area_struct *vma,
Russell King4b3073e2009-12-18 16:40:18 +0000473 unsigned long address, pte_t *ptep);
David Howellsb920de12008-02-08 04:19:31 -0800474
475#endif /* !__ASSEMBLY__ */
476
477#define kern_addr_valid(addr) (1)
478
479#define io_remap_pfn_range(vma, vaddr, pfn, size, prot) \
480 remap_pfn_range((vma), (vaddr), (pfn), (size), (prot))
481
482#define MK_IOSPACE_PFN(space, pfn) (pfn)
483#define GET_IOSPACE(pfn) 0
484#define GET_PFN(pfn) (pfn)
485
486#define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
487#define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_DIRTY
488#define __HAVE_ARCH_PTEP_GET_AND_CLEAR
489#define __HAVE_ARCH_PTEP_SET_WRPROTECT
490#define __HAVE_ARCH_PTEP_MKDIRTY
491#define __HAVE_ARCH_PTE_SAME
492#include <asm-generic/pgtable.h>
493
494#endif /* !__ASSEMBLY__ */
495
496#endif /* _ASM_PGTABLE_H */