pageattr.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright IBM Corp. 2011
  4. * Author(s): Jan Glauber <jang@linux.vnet.ibm.com>
  5. */
  6. #include <linux/hugetlb.h>
  7. #include <linux/mm.h>
  8. #include <asm/cacheflush.h>
  9. #include <asm/facility.h>
  10. #include <asm/pgtable.h>
  11. #include <asm/pgalloc.h>
  12. #include <asm/page.h>
  13. #include <asm/set_memory.h>
  14. static inline unsigned long sske_frame(unsigned long addr, unsigned char skey)
  15. {
  16. asm volatile(".insn rrf,0xb22b0000,%[skey],%[addr],1,0"
  17. : [addr] "+a" (addr) : [skey] "d" (skey));
  18. return addr;
  19. }
  20. void __storage_key_init_range(unsigned long start, unsigned long end)
  21. {
  22. unsigned long boundary, size;
  23. while (start < end) {
  24. if (MACHINE_HAS_EDAT1) {
  25. /* set storage keys for a 1MB frame */
  26. size = 1UL << 20;
  27. boundary = (start + size) & ~(size - 1);
  28. if (boundary <= end) {
  29. do {
  30. start = sske_frame(start, PAGE_DEFAULT_KEY);
  31. } while (start < boundary);
  32. continue;
  33. }
  34. }
  35. page_set_storage_key(start, PAGE_DEFAULT_KEY, 1);
  36. start += PAGE_SIZE;
  37. }
  38. }
  39. #ifdef CONFIG_PROC_FS
  40. atomic_long_t direct_pages_count[PG_DIRECT_MAP_MAX];
  41. void arch_report_meminfo(struct seq_file *m)
  42. {
  43. seq_printf(m, "DirectMap4k: %8lu kB\n",
  44. atomic_long_read(&direct_pages_count[PG_DIRECT_MAP_4K]) << 2);
  45. seq_printf(m, "DirectMap1M: %8lu kB\n",
  46. atomic_long_read(&direct_pages_count[PG_DIRECT_MAP_1M]) << 10);
  47. seq_printf(m, "DirectMap2G: %8lu kB\n",
  48. atomic_long_read(&direct_pages_count[PG_DIRECT_MAP_2G]) << 21);
  49. }
  50. #endif /* CONFIG_PROC_FS */
  51. static void pgt_set(unsigned long *old, unsigned long new, unsigned long addr,
  52. unsigned long dtt)
  53. {
  54. unsigned long table, mask;
  55. mask = 0;
  56. if (MACHINE_HAS_EDAT2) {
  57. switch (dtt) {
  58. case CRDTE_DTT_REGION3:
  59. mask = ~(PTRS_PER_PUD * sizeof(pud_t) - 1);
  60. break;
  61. case CRDTE_DTT_SEGMENT:
  62. mask = ~(PTRS_PER_PMD * sizeof(pmd_t) - 1);
  63. break;
  64. case CRDTE_DTT_PAGE:
  65. mask = ~(PTRS_PER_PTE * sizeof(pte_t) - 1);
  66. break;
  67. }
  68. table = (unsigned long)old & mask;
  69. crdte(*old, new, table, dtt, addr, S390_lowcore.kernel_asce);
  70. } else if (MACHINE_HAS_IDTE) {
  71. cspg(old, *old, new);
  72. } else {
  73. csp((unsigned int *)old + 1, *old, new);
  74. }
  75. }
  76. static int walk_pte_level(pmd_t *pmdp, unsigned long addr, unsigned long end,
  77. unsigned long flags)
  78. {
  79. pte_t *ptep, new;
  80. ptep = pte_offset(pmdp, addr);
  81. do {
  82. new = *ptep;
  83. if (pte_none(new))
  84. return -EINVAL;
  85. if (flags & SET_MEMORY_RO)
  86. new = pte_wrprotect(new);
  87. else if (flags & SET_MEMORY_RW)
  88. new = pte_mkwrite(pte_mkdirty(new));
  89. if (flags & SET_MEMORY_NX)
  90. pte_val(new) |= _PAGE_NOEXEC;
  91. else if (flags & SET_MEMORY_X)
  92. pte_val(new) &= ~_PAGE_NOEXEC;
  93. pgt_set((unsigned long *)ptep, pte_val(new), addr, CRDTE_DTT_PAGE);
  94. ptep++;
  95. addr += PAGE_SIZE;
  96. cond_resched();
  97. } while (addr < end);
  98. return 0;
  99. }
  100. static int split_pmd_page(pmd_t *pmdp, unsigned long addr)
  101. {
  102. unsigned long pte_addr, prot;
  103. pte_t *pt_dir, *ptep;
  104. pmd_t new;
  105. int i, ro, nx;
  106. pt_dir = vmem_pte_alloc();
  107. if (!pt_dir)
  108. return -ENOMEM;
  109. pte_addr = pmd_pfn(*pmdp) << PAGE_SHIFT;
  110. ro = !!(pmd_val(*pmdp) & _SEGMENT_ENTRY_PROTECT);
  111. nx = !!(pmd_val(*pmdp) & _SEGMENT_ENTRY_NOEXEC);
  112. prot = pgprot_val(ro ? PAGE_KERNEL_RO : PAGE_KERNEL);
  113. if (!nx)
  114. prot &= ~_PAGE_NOEXEC;
  115. ptep = pt_dir;
  116. for (i = 0; i < PTRS_PER_PTE; i++) {
  117. pte_val(*ptep) = pte_addr | prot;
  118. pte_addr += PAGE_SIZE;
  119. ptep++;
  120. }
  121. pmd_val(new) = __pa(pt_dir) | _SEGMENT_ENTRY;
  122. pgt_set((unsigned long *)pmdp, pmd_val(new), addr, CRDTE_DTT_SEGMENT);
  123. update_page_count(PG_DIRECT_MAP_4K, PTRS_PER_PTE);
  124. update_page_count(PG_DIRECT_MAP_1M, -1);
  125. return 0;
  126. }
  127. static void modify_pmd_page(pmd_t *pmdp, unsigned long addr,
  128. unsigned long flags)
  129. {
  130. pmd_t new = *pmdp;
  131. if (flags & SET_MEMORY_RO)
  132. new = pmd_wrprotect(new);
  133. else if (flags & SET_MEMORY_RW)
  134. new = pmd_mkwrite(pmd_mkdirty(new));
  135. if (flags & SET_MEMORY_NX)
  136. pmd_val(new) |= _SEGMENT_ENTRY_NOEXEC;
  137. else if (flags & SET_MEMORY_X)
  138. pmd_val(new) &= ~_SEGMENT_ENTRY_NOEXEC;
  139. pgt_set((unsigned long *)pmdp, pmd_val(new), addr, CRDTE_DTT_SEGMENT);
  140. }
  141. static int walk_pmd_level(pud_t *pudp, unsigned long addr, unsigned long end,
  142. unsigned long flags)
  143. {
  144. unsigned long next;
  145. pmd_t *pmdp;
  146. int rc = 0;
  147. pmdp = pmd_offset(pudp, addr);
  148. do {
  149. if (pmd_none(*pmdp))
  150. return -EINVAL;
  151. next = pmd_addr_end(addr, end);
  152. if (pmd_large(*pmdp)) {
  153. if (addr & ~PMD_MASK || addr + PMD_SIZE > next) {
  154. rc = split_pmd_page(pmdp, addr);
  155. if (rc)
  156. return rc;
  157. continue;
  158. }
  159. modify_pmd_page(pmdp, addr, flags);
  160. } else {
  161. rc = walk_pte_level(pmdp, addr, next, flags);
  162. if (rc)
  163. return rc;
  164. }
  165. pmdp++;
  166. addr = next;
  167. cond_resched();
  168. } while (addr < end);
  169. return rc;
  170. }
  171. static int split_pud_page(pud_t *pudp, unsigned long addr)
  172. {
  173. unsigned long pmd_addr, prot;
  174. pmd_t *pm_dir, *pmdp;
  175. pud_t new;
  176. int i, ro, nx;
  177. pm_dir = vmem_crst_alloc(_SEGMENT_ENTRY_EMPTY);
  178. if (!pm_dir)
  179. return -ENOMEM;
  180. pmd_addr = pud_pfn(*pudp) << PAGE_SHIFT;
  181. ro = !!(pud_val(*pudp) & _REGION_ENTRY_PROTECT);
  182. nx = !!(pud_val(*pudp) & _REGION_ENTRY_NOEXEC);
  183. prot = pgprot_val(ro ? SEGMENT_KERNEL_RO : SEGMENT_KERNEL);
  184. if (!nx)
  185. prot &= ~_SEGMENT_ENTRY_NOEXEC;
  186. pmdp = pm_dir;
  187. for (i = 0; i < PTRS_PER_PMD; i++) {
  188. pmd_val(*pmdp) = pmd_addr | prot;
  189. pmd_addr += PMD_SIZE;
  190. pmdp++;
  191. }
  192. pud_val(new) = __pa(pm_dir) | _REGION3_ENTRY;
  193. pgt_set((unsigned long *)pudp, pud_val(new), addr, CRDTE_DTT_REGION3);
  194. update_page_count(PG_DIRECT_MAP_1M, PTRS_PER_PMD);
  195. update_page_count(PG_DIRECT_MAP_2G, -1);
  196. return 0;
  197. }
  198. static void modify_pud_page(pud_t *pudp, unsigned long addr,
  199. unsigned long flags)
  200. {
  201. pud_t new = *pudp;
  202. if (flags & SET_MEMORY_RO)
  203. new = pud_wrprotect(new);
  204. else if (flags & SET_MEMORY_RW)
  205. new = pud_mkwrite(pud_mkdirty(new));
  206. if (flags & SET_MEMORY_NX)
  207. pud_val(new) |= _REGION_ENTRY_NOEXEC;
  208. else if (flags & SET_MEMORY_X)
  209. pud_val(new) &= ~_REGION_ENTRY_NOEXEC;
  210. pgt_set((unsigned long *)pudp, pud_val(new), addr, CRDTE_DTT_REGION3);
  211. }
  212. static int walk_pud_level(p4d_t *p4d, unsigned long addr, unsigned long end,
  213. unsigned long flags)
  214. {
  215. unsigned long next;
  216. pud_t *pudp;
  217. int rc = 0;
  218. pudp = pud_offset(p4d, addr);
  219. do {
  220. if (pud_none(*pudp))
  221. return -EINVAL;
  222. next = pud_addr_end(addr, end);
  223. if (pud_large(*pudp)) {
  224. if (addr & ~PUD_MASK || addr + PUD_SIZE > next) {
  225. rc = split_pud_page(pudp, addr);
  226. if (rc)
  227. break;
  228. continue;
  229. }
  230. modify_pud_page(pudp, addr, flags);
  231. } else {
  232. rc = walk_pmd_level(pudp, addr, next, flags);
  233. }
  234. pudp++;
  235. addr = next;
  236. cond_resched();
  237. } while (addr < end && !rc);
  238. return rc;
  239. }
  240. static int walk_p4d_level(pgd_t *pgd, unsigned long addr, unsigned long end,
  241. unsigned long flags)
  242. {
  243. unsigned long next;
  244. p4d_t *p4dp;
  245. int rc = 0;
  246. p4dp = p4d_offset(pgd, addr);
  247. do {
  248. if (p4d_none(*p4dp))
  249. return -EINVAL;
  250. next = p4d_addr_end(addr, end);
  251. rc = walk_pud_level(p4dp, addr, next, flags);
  252. p4dp++;
  253. addr = next;
  254. cond_resched();
  255. } while (addr < end && !rc);
  256. return rc;
  257. }
  258. static DEFINE_MUTEX(cpa_mutex);
  259. static int change_page_attr(unsigned long addr, unsigned long end,
  260. unsigned long flags)
  261. {
  262. unsigned long next;
  263. int rc = -EINVAL;
  264. pgd_t *pgdp;
  265. if (addr == end)
  266. return 0;
  267. if (end >= MODULES_END)
  268. return -EINVAL;
  269. mutex_lock(&cpa_mutex);
  270. pgdp = pgd_offset_k(addr);
  271. do {
  272. if (pgd_none(*pgdp))
  273. break;
  274. next = pgd_addr_end(addr, end);
  275. rc = walk_p4d_level(pgdp, addr, next, flags);
  276. if (rc)
  277. break;
  278. cond_resched();
  279. } while (pgdp++, addr = next, addr < end && !rc);
  280. mutex_unlock(&cpa_mutex);
  281. return rc;
  282. }
  283. int __set_memory(unsigned long addr, int numpages, unsigned long flags)
  284. {
  285. if (!MACHINE_HAS_NX)
  286. flags &= ~(SET_MEMORY_NX | SET_MEMORY_X);
  287. if (!flags)
  288. return 0;
  289. addr &= PAGE_MASK;
  290. return change_page_attr(addr, addr + numpages * PAGE_SIZE, flags);
  291. }
  292. #ifdef CONFIG_DEBUG_PAGEALLOC
  293. static void ipte_range(pte_t *pte, unsigned long address, int nr)
  294. {
  295. int i;
  296. if (test_facility(13)) {
  297. __ptep_ipte_range(address, nr - 1, pte, IPTE_GLOBAL);
  298. return;
  299. }
  300. for (i = 0; i < nr; i++) {
  301. __ptep_ipte(address, pte, 0, 0, IPTE_GLOBAL);
  302. address += PAGE_SIZE;
  303. pte++;
  304. }
  305. }
  306. void __kernel_map_pages(struct page *page, int numpages, int enable)
  307. {
  308. unsigned long address;
  309. int nr, i, j;
  310. pgd_t *pgd;
  311. p4d_t *p4d;
  312. pud_t *pud;
  313. pmd_t *pmd;
  314. pte_t *pte;
  315. for (i = 0; i < numpages;) {
  316. address = page_to_phys(page + i);
  317. pgd = pgd_offset_k(address);
  318. p4d = p4d_offset(pgd, address);
  319. pud = pud_offset(p4d, address);
  320. pmd = pmd_offset(pud, address);
  321. pte = pte_offset_kernel(pmd, address);
  322. nr = (unsigned long)pte >> ilog2(sizeof(long));
  323. nr = PTRS_PER_PTE - (nr & (PTRS_PER_PTE - 1));
  324. nr = min(numpages - i, nr);
  325. if (enable) {
  326. for (j = 0; j < nr; j++) {
  327. pte_val(*pte) &= ~_PAGE_INVALID;
  328. address += PAGE_SIZE;
  329. pte++;
  330. }
  331. } else {
  332. ipte_range(pte, address, nr);
  333. }
  334. i += nr;
  335. }
  336. }
  337. #ifdef CONFIG_HIBERNATION
  338. bool kernel_page_present(struct page *page)
  339. {
  340. unsigned long addr;
  341. int cc;
  342. addr = page_to_phys(page);
  343. asm volatile(
  344. " lra %1,0(%1)\n"
  345. " ipm %0\n"
  346. " srl %0,28"
  347. : "=d" (cc), "+a" (addr) : : "cc");
  348. return cc == 0;
  349. }
  350. #endif /* CONFIG_HIBERNATION */
  351. #endif /* CONFIG_DEBUG_PAGEALLOC */