Jan Glauber | 305b152 | 2011-03-15 17:08:22 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright IBM Corp. 2011 |
| 3 | * Author(s): Jan Glauber <jang@linux.vnet.ibm.com> |
| 4 | */ |
Heiko Carstens | 6b70a92 | 2012-11-02 12:56:43 +0100 | [diff] [blame] | 5 | #include <linux/hugetlb.h> |
Jan Glauber | 305b152 | 2011-03-15 17:08:22 +0100 | [diff] [blame] | 6 | #include <linux/module.h> |
| 7 | #include <linux/mm.h> |
Martin Schwidefsky | 638ad34 | 2011-10-30 15:17:13 +0100 | [diff] [blame] | 8 | #include <asm/cacheflush.h> |
Jan Glauber | 305b152 | 2011-03-15 17:08:22 +0100 | [diff] [blame] | 9 | #include <asm/pgtable.h> |
Heiko Carstens | 6b70a92 | 2012-11-02 12:56:43 +0100 | [diff] [blame] | 10 | #include <asm/page.h> |
| 11 | |
Heiko Carstens | f7f8d7e | 2013-03-14 16:46:05 +0100 | [diff] [blame] | 12 | static inline unsigned long sske_frame(unsigned long addr, unsigned char skey) |
| 13 | { |
| 14 | asm volatile(".insn rrf,0xb22b0000,%[skey],%[addr],9,0" |
| 15 | : [addr] "+a" (addr) : [skey] "d" (skey)); |
| 16 | return addr; |
| 17 | } |
| 18 | |
Heiko Carstens | 6b70a92 | 2012-11-02 12:56:43 +0100 | [diff] [blame] | 19 | void storage_key_init_range(unsigned long start, unsigned long end) |
| 20 | { |
Heiko Carstens | f7f8d7e | 2013-03-14 16:46:05 +0100 | [diff] [blame] | 21 | unsigned long boundary, size; |
Heiko Carstens | 6b70a92 | 2012-11-02 12:56:43 +0100 | [diff] [blame] | 22 | |
| 23 | while (start < end) { |
Heiko Carstens | 6b70a92 | 2012-11-02 12:56:43 +0100 | [diff] [blame] | 24 | if (MACHINE_HAS_EDAT1) { |
| 25 | /* set storage keys for a 1MB frame */ |
Heiko Carstens | 6b70a92 | 2012-11-02 12:56:43 +0100 | [diff] [blame] | 26 | size = 1UL << 20; |
| 27 | boundary = (start + size) & ~(size - 1); |
| 28 | if (boundary <= end) { |
| 29 | do { |
Heiko Carstens | f7f8d7e | 2013-03-14 16:46:05 +0100 | [diff] [blame] | 30 | start = sske_frame(start, PAGE_DEFAULT_KEY); |
Heiko Carstens | 6b70a92 | 2012-11-02 12:56:43 +0100 | [diff] [blame] | 31 | } while (start < boundary); |
| 32 | continue; |
| 33 | } |
| 34 | } |
| 35 | page_set_storage_key(start, PAGE_DEFAULT_KEY, 0); |
| 36 | start += PAGE_SIZE; |
| 37 | } |
| 38 | } |
Jan Glauber | 305b152 | 2011-03-15 17:08:22 +0100 | [diff] [blame] | 39 | |
Heiko Carstens | 5b1ba9e | 2012-10-01 16:18:46 +0200 | [diff] [blame] | 40 | static pte_t *walk_page_table(unsigned long addr) |
| 41 | { |
| 42 | pgd_t *pgdp; |
| 43 | pud_t *pudp; |
| 44 | pmd_t *pmdp; |
| 45 | pte_t *ptep; |
| 46 | |
| 47 | pgdp = pgd_offset_k(addr); |
| 48 | if (pgd_none(*pgdp)) |
| 49 | return NULL; |
| 50 | pudp = pud_offset(pgdp, addr); |
Heiko Carstens | 18da236 | 2012-10-08 09:18:26 +0200 | [diff] [blame] | 51 | if (pud_none(*pudp) || pud_large(*pudp)) |
Heiko Carstens | 5b1ba9e | 2012-10-01 16:18:46 +0200 | [diff] [blame] | 52 | return NULL; |
| 53 | pmdp = pmd_offset(pudp, addr); |
| 54 | if (pmd_none(*pmdp) || pmd_large(*pmdp)) |
| 55 | return NULL; |
| 56 | ptep = pte_offset_kernel(pmdp, addr); |
| 57 | if (pte_none(*ptep)) |
| 58 | return NULL; |
| 59 | return ptep; |
| 60 | } |
| 61 | |
Jan Glauber | 305b152 | 2011-03-15 17:08:22 +0100 | [diff] [blame] | 62 | static void change_page_attr(unsigned long addr, int numpages, |
| 63 | pte_t (*set) (pte_t)) |
| 64 | { |
| 65 | pte_t *ptep, pte; |
Jan Glauber | 305b152 | 2011-03-15 17:08:22 +0100 | [diff] [blame] | 66 | int i; |
| 67 | |
| 68 | for (i = 0; i < numpages; i++) { |
Heiko Carstens | 5b1ba9e | 2012-10-01 16:18:46 +0200 | [diff] [blame] | 69 | ptep = walk_page_table(addr); |
| 70 | if (WARN_ON_ONCE(!ptep)) |
| 71 | break; |
Jan Glauber | 305b152 | 2011-03-15 17:08:22 +0100 | [diff] [blame] | 72 | pte = *ptep; |
| 73 | pte = set(pte); |
Martin Schwidefsky | b2fa47e | 2011-05-23 10:24:40 +0200 | [diff] [blame] | 74 | __ptep_ipte(addr, ptep); |
Jan Glauber | 305b152 | 2011-03-15 17:08:22 +0100 | [diff] [blame] | 75 | *ptep = pte; |
Jan Glauber | e4c031b | 2011-04-20 10:15:32 +0200 | [diff] [blame] | 76 | addr += PAGE_SIZE; |
Jan Glauber | 305b152 | 2011-03-15 17:08:22 +0100 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
| 80 | int set_memory_ro(unsigned long addr, int numpages) |
| 81 | { |
| 82 | change_page_attr(addr, numpages, pte_wrprotect); |
| 83 | return 0; |
| 84 | } |
Jan Glauber | 305b152 | 2011-03-15 17:08:22 +0100 | [diff] [blame] | 85 | |
| 86 | int set_memory_rw(unsigned long addr, int numpages) |
| 87 | { |
| 88 | change_page_attr(addr, numpages, pte_mkwrite); |
| 89 | return 0; |
| 90 | } |
Jan Glauber | 305b152 | 2011-03-15 17:08:22 +0100 | [diff] [blame] | 91 | |
| 92 | /* not possible */ |
| 93 | int set_memory_nx(unsigned long addr, int numpages) |
| 94 | { |
| 95 | return 0; |
| 96 | } |
Jan Glauber | 448694a | 2011-05-19 16:55:26 -0600 | [diff] [blame] | 97 | |
| 98 | int set_memory_x(unsigned long addr, int numpages) |
| 99 | { |
| 100 | return 0; |
| 101 | } |
Heiko Carstens | 0a4ccc9 | 2012-11-02 13:28:48 +0100 | [diff] [blame] | 102 | |
| 103 | #ifdef CONFIG_DEBUG_PAGEALLOC |
| 104 | void kernel_map_pages(struct page *page, int numpages, int enable) |
| 105 | { |
| 106 | unsigned long address; |
| 107 | pgd_t *pgd; |
| 108 | pud_t *pud; |
| 109 | pmd_t *pmd; |
| 110 | pte_t *pte; |
| 111 | int i; |
| 112 | |
| 113 | for (i = 0; i < numpages; i++) { |
| 114 | address = page_to_phys(page + i); |
| 115 | pgd = pgd_offset_k(address); |
| 116 | pud = pud_offset(pgd, address); |
| 117 | pmd = pmd_offset(pud, address); |
| 118 | pte = pte_offset_kernel(pmd, address); |
| 119 | if (!enable) { |
| 120 | __ptep_ipte(address, pte); |
| 121 | pte_val(*pte) = _PAGE_TYPE_EMPTY; |
| 122 | continue; |
| 123 | } |
Martin Schwidefsky | abf09be | 2012-11-07 13:17:37 +0100 | [diff] [blame] | 124 | pte_val(*pte) = __pa(address); |
Heiko Carstens | 0a4ccc9 | 2012-11-02 13:28:48 +0100 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
| 128 | #ifdef CONFIG_HIBERNATION |
| 129 | bool kernel_page_present(struct page *page) |
| 130 | { |
| 131 | unsigned long addr; |
| 132 | int cc; |
| 133 | |
| 134 | addr = page_to_phys(page); |
| 135 | asm volatile( |
| 136 | " lra %1,0(%1)\n" |
| 137 | " ipm %0\n" |
| 138 | " srl %0,28" |
| 139 | : "=d" (cc), "+a" (addr) : : "cc"); |
| 140 | return cc == 0; |
| 141 | } |
| 142 | #endif /* CONFIG_HIBERNATION */ |
| 143 | |
| 144 | #endif /* CONFIG_DEBUG_PAGEALLOC */ |