Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * include/asm-s390/pgalloc.h |
| 3 | * |
| 4 | * S390 version |
| 5 | * Copyright (C) 1999,2000 IBM Deutschland Entwicklung GmbH, IBM Corporation |
| 6 | * Author(s): Hartmut Penner (hp@de.ibm.com) |
| 7 | * Martin Schwidefsky (schwidefsky@de.ibm.com) |
| 8 | * |
| 9 | * Derived from "include/asm-i386/pgalloc.h" |
| 10 | * Copyright (C) 1994 Linus Torvalds |
| 11 | */ |
| 12 | |
| 13 | #ifndef _S390_PGALLOC_H |
| 14 | #define _S390_PGALLOC_H |
| 15 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/threads.h> |
| 17 | #include <linux/gfp.h> |
| 18 | #include <linux/mm.h> |
| 19 | |
| 20 | #define check_pgt_cache() do {} while (0) |
| 21 | |
| 22 | extern void diag10(unsigned long addr); |
| 23 | |
| 24 | /* |
Gerald Schaefer | 9282ed9 | 2006-09-20 15:59:37 +0200 | [diff] [blame] | 25 | * Page allocation orders. |
| 26 | */ |
| 27 | #ifndef __s390x__ |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 28 | # define PTE_ALLOC_ORDER 0 |
| 29 | # define PMD_ALLOC_ORDER 0 |
Gerald Schaefer | 9282ed9 | 2006-09-20 15:59:37 +0200 | [diff] [blame] | 30 | # define PGD_ALLOC_ORDER 1 |
| 31 | #else /* __s390x__ */ |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 32 | # define PTE_ALLOC_ORDER 0 |
Gerald Schaefer | 9282ed9 | 2006-09-20 15:59:37 +0200 | [diff] [blame] | 33 | # define PMD_ALLOC_ORDER 2 |
| 34 | # define PGD_ALLOC_ORDER 2 |
| 35 | #endif /* __s390x__ */ |
| 36 | |
| 37 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | * Allocate and free page tables. The xxx_kernel() versions are |
| 39 | * used to allocate a kernel page table - this turns on ASN bits |
| 40 | * if any. |
| 41 | */ |
| 42 | |
| 43 | static inline pgd_t *pgd_alloc(struct mm_struct *mm) |
| 44 | { |
Gerald Schaefer | 9282ed9 | 2006-09-20 15:59:37 +0200 | [diff] [blame] | 45 | pgd_t *pgd = (pgd_t *) __get_free_pages(GFP_KERNEL, PGD_ALLOC_ORDER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | int i; |
| 47 | |
Gerald Schaefer | 9282ed9 | 2006-09-20 15:59:37 +0200 | [diff] [blame] | 48 | if (!pgd) |
| 49 | return NULL; |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame^] | 50 | if (s390_noexec) { |
| 51 | pgd_t *shadow_pgd = (pgd_t *) |
| 52 | __get_free_pages(GFP_KERNEL, PGD_ALLOC_ORDER); |
| 53 | struct page *page = virt_to_page(pgd); |
| 54 | |
| 55 | if (!shadow_pgd) { |
| 56 | free_pages((unsigned long) pgd, PGD_ALLOC_ORDER); |
| 57 | return NULL; |
| 58 | } |
| 59 | page->lru.next = (void *) shadow_pgd; |
| 60 | } |
Gerald Schaefer | 9282ed9 | 2006-09-20 15:59:37 +0200 | [diff] [blame] | 61 | for (i = 0; i < PTRS_PER_PGD; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | #ifndef __s390x__ |
Gerald Schaefer | 9282ed9 | 2006-09-20 15:59:37 +0200 | [diff] [blame] | 63 | pmd_clear(pmd_offset(pgd + i, i*PGDIR_SIZE)); |
| 64 | #else |
| 65 | pgd_clear(pgd + i); |
| 66 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | return pgd; |
| 68 | } |
| 69 | |
| 70 | static inline void pgd_free(pgd_t *pgd) |
| 71 | { |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame^] | 72 | pgd_t *shadow_pgd = get_shadow_pgd(pgd); |
| 73 | |
| 74 | if (shadow_pgd) |
| 75 | free_pages((unsigned long) shadow_pgd, PGD_ALLOC_ORDER); |
Gerald Schaefer | 9282ed9 | 2006-09-20 15:59:37 +0200 | [diff] [blame] | 76 | free_pages((unsigned long) pgd, PGD_ALLOC_ORDER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | #ifndef __s390x__ |
| 80 | /* |
| 81 | * page middle directory allocation/free routines. |
| 82 | * We use pmd cache only on s390x, so these are dummy routines. This |
| 83 | * code never triggers because the pgd will always be present. |
| 84 | */ |
| 85 | #define pmd_alloc_one(mm,address) ({ BUG(); ((pmd_t *)2); }) |
| 86 | #define pmd_free(x) do { } while (0) |
| 87 | #define __pmd_free_tlb(tlb,x) do { } while (0) |
| 88 | #define pgd_populate(mm, pmd, pte) BUG() |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame^] | 89 | #define pgd_populate_kernel(mm, pmd, pte) BUG() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | #else /* __s390x__ */ |
| 91 | static inline pmd_t * pmd_alloc_one(struct mm_struct *mm, unsigned long vmaddr) |
| 92 | { |
Gerald Schaefer | 9282ed9 | 2006-09-20 15:59:37 +0200 | [diff] [blame] | 93 | pmd_t *pmd = (pmd_t *) __get_free_pages(GFP_KERNEL, PMD_ALLOC_ORDER); |
| 94 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
Gerald Schaefer | 9282ed9 | 2006-09-20 15:59:37 +0200 | [diff] [blame] | 96 | if (!pmd) |
| 97 | return NULL; |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame^] | 98 | if (s390_noexec) { |
| 99 | pmd_t *shadow_pmd = (pmd_t *) |
| 100 | __get_free_pages(GFP_KERNEL, PMD_ALLOC_ORDER); |
| 101 | struct page *page = virt_to_page(pmd); |
| 102 | |
| 103 | if (!shadow_pmd) { |
| 104 | free_pages((unsigned long) pmd, PMD_ALLOC_ORDER); |
| 105 | return NULL; |
| 106 | } |
| 107 | page->lru.next = (void *) shadow_pmd; |
| 108 | } |
Gerald Schaefer | 9282ed9 | 2006-09-20 15:59:37 +0200 | [diff] [blame] | 109 | for (i=0; i < PTRS_PER_PMD; i++) |
| 110 | pmd_clear(pmd + i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | return pmd; |
| 112 | } |
| 113 | |
| 114 | static inline void pmd_free (pmd_t *pmd) |
| 115 | { |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame^] | 116 | pmd_t *shadow_pmd = get_shadow_pmd(pmd); |
| 117 | |
| 118 | if (shadow_pmd) |
| 119 | free_pages((unsigned long) shadow_pmd, PMD_ALLOC_ORDER); |
Gerald Schaefer | 9282ed9 | 2006-09-20 15:59:37 +0200 | [diff] [blame] | 120 | free_pages((unsigned long) pmd, PMD_ALLOC_ORDER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | #define __pmd_free_tlb(tlb,pmd) \ |
| 124 | do { \ |
| 125 | tlb_flush_mmu(tlb, 0, 0); \ |
| 126 | pmd_free(pmd); \ |
| 127 | } while (0) |
| 128 | |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame^] | 129 | static inline void |
| 130 | pgd_populate_kernel(struct mm_struct *mm, pgd_t *pgd, pmd_t *pmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | { |
| 132 | pgd_val(*pgd) = _PGD_ENTRY | __pa(pmd); |
| 133 | } |
| 134 | |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame^] | 135 | static inline void pgd_populate(struct mm_struct *mm, pgd_t *pgd, pmd_t *pmd) |
| 136 | { |
| 137 | pgd_t *shadow_pgd = get_shadow_pgd(pgd); |
| 138 | pmd_t *shadow_pmd = get_shadow_pmd(pmd); |
| 139 | |
| 140 | if (shadow_pgd && shadow_pmd) |
| 141 | pgd_populate_kernel(mm, shadow_pgd, shadow_pmd); |
| 142 | pgd_populate_kernel(mm, pgd, pmd); |
| 143 | } |
| 144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | #endif /* __s390x__ */ |
| 146 | |
| 147 | static inline void |
| 148 | pmd_populate_kernel(struct mm_struct *mm, pmd_t *pmd, pte_t *pte) |
| 149 | { |
| 150 | #ifndef __s390x__ |
| 151 | pmd_val(pmd[0]) = _PAGE_TABLE + __pa(pte); |
| 152 | pmd_val(pmd[1]) = _PAGE_TABLE + __pa(pte+256); |
| 153 | pmd_val(pmd[2]) = _PAGE_TABLE + __pa(pte+512); |
| 154 | pmd_val(pmd[3]) = _PAGE_TABLE + __pa(pte+768); |
| 155 | #else /* __s390x__ */ |
| 156 | pmd_val(*pmd) = _PMD_ENTRY + __pa(pte); |
| 157 | pmd_val1(*pmd) = _PMD_ENTRY + __pa(pte+256); |
| 158 | #endif /* __s390x__ */ |
| 159 | } |
| 160 | |
| 161 | static inline void |
| 162 | pmd_populate(struct mm_struct *mm, pmd_t *pmd, struct page *page) |
| 163 | { |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame^] | 164 | pte_t *pte = (pte_t *)page_to_phys(page); |
| 165 | pmd_t *shadow_pmd = get_shadow_pmd(pmd); |
| 166 | pte_t *shadow_pte = get_shadow_pte(pte); |
| 167 | |
| 168 | pmd_populate_kernel(mm, pmd, pte); |
| 169 | if (shadow_pmd && shadow_pte) |
| 170 | pmd_populate_kernel(mm, shadow_pmd, shadow_pte); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /* |
| 174 | * page table entry allocation/free routines. |
| 175 | */ |
| 176 | static inline pte_t * |
| 177 | pte_alloc_one_kernel(struct mm_struct *mm, unsigned long vmaddr) |
| 178 | { |
Gerald Schaefer | 9282ed9 | 2006-09-20 15:59:37 +0200 | [diff] [blame] | 179 | pte_t *pte = (pte_t *) __get_free_page(GFP_KERNEL|__GFP_REPEAT); |
| 180 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
Gerald Schaefer | 9282ed9 | 2006-09-20 15:59:37 +0200 | [diff] [blame] | 182 | if (!pte) |
| 183 | return NULL; |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame^] | 184 | if (s390_noexec) { |
| 185 | pte_t *shadow_pte = (pte_t *) |
| 186 | __get_free_page(GFP_KERNEL|__GFP_REPEAT); |
| 187 | struct page *page = virt_to_page(pte); |
| 188 | |
| 189 | if (!shadow_pte) { |
| 190 | free_page((unsigned long) pte); |
| 191 | return NULL; |
| 192 | } |
| 193 | page->lru.next = (void *) shadow_pte; |
| 194 | } |
Gerald Schaefer | 9282ed9 | 2006-09-20 15:59:37 +0200 | [diff] [blame] | 195 | for (i=0; i < PTRS_PER_PTE; i++) { |
| 196 | pte_clear(mm, vmaddr, pte + i); |
| 197 | vmaddr += PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } |
| 199 | return pte; |
| 200 | } |
| 201 | |
| 202 | static inline struct page * |
| 203 | pte_alloc_one(struct mm_struct *mm, unsigned long vmaddr) |
| 204 | { |
| 205 | pte_t *pte = pte_alloc_one_kernel(mm, vmaddr); |
| 206 | if (pte) |
| 207 | return virt_to_page(pte); |
Heiko Carstens | d2c993d | 2006-07-12 16:41:55 +0200 | [diff] [blame] | 208 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | static inline void pte_free_kernel(pte_t *pte) |
| 212 | { |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame^] | 213 | pte_t *shadow_pte = get_shadow_pte(pte); |
| 214 | |
| 215 | if (shadow_pte) |
| 216 | free_page((unsigned long) shadow_pte); |
| 217 | free_page((unsigned long) pte); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | static inline void pte_free(struct page *pte) |
| 221 | { |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame^] | 222 | struct page *shadow_page = get_shadow_page(pte); |
| 223 | |
| 224 | if (shadow_page) |
| 225 | __free_page(shadow_page); |
| 226 | __free_page(pte); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame^] | 229 | #define __pte_free_tlb(tlb, pte) \ |
| 230 | ({ \ |
| 231 | struct mmu_gather *__tlb = (tlb); \ |
| 232 | struct page *__pte = (pte); \ |
| 233 | struct page *shadow_page = get_shadow_page(__pte); \ |
| 234 | if (shadow_page) \ |
| 235 | tlb_remove_page(__tlb, shadow_page); \ |
| 236 | tlb_remove_page(__tlb, __pte); \ |
| 237 | }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | #endif /* _S390_PGALLOC_H */ |