pageattr.c 3.4 KB

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