pageattr.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /*
  2. * Copyright 2002 Andi Kleen, SuSE Labs.
  3. * Thanks to Ben LaHaise for precious feedback.
  4. */
  5. #include <linux/highmem.h>
  6. #include <linux/bootmem.h>
  7. #include <linux/module.h>
  8. #include <linux/sched.h>
  9. #include <linux/slab.h>
  10. #include <linux/mm.h>
  11. #include <asm/e820.h>
  12. #include <asm/processor.h>
  13. #include <asm/tlbflush.h>
  14. #include <asm/sections.h>
  15. #include <asm/uaccess.h>
  16. #include <asm/pgalloc.h>
  17. static inline int
  18. within(unsigned long addr, unsigned long start, unsigned long end)
  19. {
  20. return addr >= start && addr < end;
  21. }
  22. /*
  23. * Flushing functions
  24. */
  25. /**
  26. * clflush_cache_range - flush a cache range with clflush
  27. * @addr: virtual start address
  28. * @size: number of bytes to flush
  29. *
  30. * clflush is an unordered instruction which needs fencing with mfence
  31. * to avoid ordering issues.
  32. */
  33. void clflush_cache_range(void *vaddr, unsigned int size)
  34. {
  35. void *vend = vaddr + size - 1;
  36. mb();
  37. for (; vaddr < vend; vaddr += boot_cpu_data.x86_clflush_size)
  38. clflush(vaddr);
  39. /*
  40. * Flush any possible final partial cacheline:
  41. */
  42. clflush(vend);
  43. mb();
  44. }
  45. static void __cpa_flush_all(void *arg)
  46. {
  47. /*
  48. * Flush all to work around Errata in early athlons regarding
  49. * large page flushing.
  50. */
  51. __flush_tlb_all();
  52. if (boot_cpu_data.x86_model >= 4)
  53. wbinvd();
  54. }
  55. static void cpa_flush_all(void)
  56. {
  57. BUG_ON(irqs_disabled());
  58. on_each_cpu(__cpa_flush_all, NULL, 1, 1);
  59. }
  60. static void __cpa_flush_range(void *arg)
  61. {
  62. /*
  63. * We could optimize that further and do individual per page
  64. * tlb invalidates for a low number of pages. Caveat: we must
  65. * flush the high aliases on 64bit as well.
  66. */
  67. __flush_tlb_all();
  68. }
  69. static void cpa_flush_range(unsigned long start, int numpages)
  70. {
  71. unsigned int i, level;
  72. unsigned long addr;
  73. BUG_ON(irqs_disabled());
  74. WARN_ON(PAGE_ALIGN(start) != start);
  75. on_each_cpu(__cpa_flush_range, NULL, 1, 1);
  76. /*
  77. * We only need to flush on one CPU,
  78. * clflush is a MESI-coherent instruction that
  79. * will cause all other CPUs to flush the same
  80. * cachelines:
  81. */
  82. for (i = 0, addr = start; i < numpages; i++, addr += PAGE_SIZE) {
  83. pte_t *pte = lookup_address(addr, &level);
  84. /*
  85. * Only flush present addresses:
  86. */
  87. if (pte && pte_present(*pte))
  88. clflush_cache_range((void *) addr, PAGE_SIZE);
  89. }
  90. }
  91. /*
  92. * Certain areas of memory on x86 require very specific protection flags,
  93. * for example the BIOS area or kernel text. Callers don't always get this
  94. * right (again, ioremap() on BIOS memory is not uncommon) so this function
  95. * checks and fixes these known static required protection bits.
  96. */
  97. static inline pgprot_t static_protections(pgprot_t prot, unsigned long address)
  98. {
  99. pgprot_t forbidden = __pgprot(0);
  100. /*
  101. * The BIOS area between 640k and 1Mb needs to be executable for
  102. * PCI BIOS based config access (CONFIG_PCI_GOBIOS) support.
  103. */
  104. if (within(__pa(address), BIOS_BEGIN, BIOS_END))
  105. pgprot_val(forbidden) |= _PAGE_NX;
  106. /*
  107. * The kernel text needs to be executable for obvious reasons
  108. * Does not cover __inittext since that is gone later on
  109. */
  110. if (within(address, (unsigned long)_text, (unsigned long)_etext))
  111. pgprot_val(forbidden) |= _PAGE_NX;
  112. #ifdef CONFIG_DEBUG_RODATA
  113. /* The .rodata section needs to be read-only */
  114. if (within(address, (unsigned long)__start_rodata,
  115. (unsigned long)__end_rodata))
  116. pgprot_val(forbidden) |= _PAGE_RW;
  117. #endif
  118. prot = __pgprot(pgprot_val(prot) & ~pgprot_val(forbidden));
  119. return prot;
  120. }
  121. pte_t *lookup_address(unsigned long address, int *level)
  122. {
  123. pgd_t *pgd = pgd_offset_k(address);
  124. pud_t *pud;
  125. pmd_t *pmd;
  126. *level = PG_LEVEL_NONE;
  127. if (pgd_none(*pgd))
  128. return NULL;
  129. pud = pud_offset(pgd, address);
  130. if (pud_none(*pud))
  131. return NULL;
  132. pmd = pmd_offset(pud, address);
  133. if (pmd_none(*pmd))
  134. return NULL;
  135. *level = PG_LEVEL_2M;
  136. if (pmd_large(*pmd))
  137. return (pte_t *)pmd;
  138. *level = PG_LEVEL_4K;
  139. return pte_offset_kernel(pmd, address);
  140. }
  141. static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
  142. {
  143. /* change init_mm */
  144. set_pte_atomic(kpte, pte);
  145. #ifdef CONFIG_X86_32
  146. if (!SHARED_KERNEL_PMD) {
  147. struct page *page;
  148. for (page = pgd_list; page; page = (struct page *)page->index) {
  149. pgd_t *pgd;
  150. pud_t *pud;
  151. pmd_t *pmd;
  152. pgd = (pgd_t *)page_address(page) + pgd_index(address);
  153. pud = pud_offset(pgd, address);
  154. pmd = pmd_offset(pud, address);
  155. set_pte_atomic((pte_t *)pmd, pte);
  156. }
  157. }
  158. #endif
  159. }
  160. static int split_large_page(pte_t *kpte, unsigned long address)
  161. {
  162. pgprot_t ref_prot = pte_pgprot(pte_clrhuge(*kpte));
  163. gfp_t gfp_flags = GFP_KERNEL;
  164. unsigned long flags;
  165. unsigned long addr;
  166. pte_t *pbase, *tmp;
  167. struct page *base;
  168. unsigned int i, level;
  169. #ifdef CONFIG_DEBUG_PAGEALLOC
  170. gfp_flags = __GFP_HIGH | __GFP_NOFAIL | __GFP_NOWARN;
  171. gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
  172. #endif
  173. base = alloc_pages(gfp_flags, 0);
  174. if (!base)
  175. return -ENOMEM;
  176. spin_lock_irqsave(&pgd_lock, flags);
  177. /*
  178. * Check for races, another CPU might have split this page
  179. * up for us already:
  180. */
  181. tmp = lookup_address(address, &level);
  182. if (tmp != kpte) {
  183. WARN_ON_ONCE(1);
  184. goto out_unlock;
  185. }
  186. address = __pa(address);
  187. addr = address & LARGE_PAGE_MASK;
  188. pbase = (pte_t *)page_address(base);
  189. #ifdef CONFIG_X86_32
  190. paravirt_alloc_pt(&init_mm, page_to_pfn(base));
  191. #endif
  192. pgprot_val(ref_prot) &= ~_PAGE_NX;
  193. for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE)
  194. set_pte(&pbase[i], pfn_pte(addr >> PAGE_SHIFT, ref_prot));
  195. /*
  196. * Install the new, split up pagetable. Important detail here:
  197. *
  198. * On Intel the NX bit of all levels must be cleared to make a
  199. * page executable. See section 4.13.2 of Intel 64 and IA-32
  200. * Architectures Software Developer's Manual).
  201. */
  202. ref_prot = pte_pgprot(pte_mkexec(pte_clrhuge(*kpte)));
  203. __set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
  204. base = NULL;
  205. out_unlock:
  206. spin_unlock_irqrestore(&pgd_lock, flags);
  207. if (base)
  208. __free_pages(base, 0);
  209. return 0;
  210. }
  211. static int
  212. __change_page_attr(unsigned long address, unsigned long pfn,
  213. pgprot_t mask_set, pgprot_t mask_clr)
  214. {
  215. struct page *kpte_page;
  216. int level, err = 0;
  217. pte_t *kpte;
  218. #ifdef CONFIG_X86_32
  219. BUG_ON(pfn > max_low_pfn);
  220. #endif
  221. repeat:
  222. kpte = lookup_address(address, &level);
  223. if (!kpte)
  224. return -EINVAL;
  225. kpte_page = virt_to_page(kpte);
  226. BUG_ON(PageLRU(kpte_page));
  227. BUG_ON(PageCompound(kpte_page));
  228. if (level == PG_LEVEL_4K) {
  229. pgprot_t new_prot = pte_pgprot(*kpte);
  230. pte_t new_pte, old_pte = *kpte;
  231. pgprot_val(new_prot) &= ~pgprot_val(mask_clr);
  232. pgprot_val(new_prot) |= pgprot_val(mask_set);
  233. new_prot = static_protections(new_prot, address);
  234. new_pte = pfn_pte(pfn, canon_pgprot(new_prot));
  235. BUG_ON(pte_pfn(new_pte) != pte_pfn(old_pte));
  236. set_pte_atomic(kpte, new_pte);
  237. } else {
  238. err = split_large_page(kpte, address);
  239. if (!err)
  240. goto repeat;
  241. }
  242. return err;
  243. }
  244. /**
  245. * change_page_attr_addr - Change page table attributes in linear mapping
  246. * @address: Virtual address in linear mapping.
  247. * @prot: New page table attribute (PAGE_*)
  248. *
  249. * Change page attributes of a page in the direct mapping. This is a variant
  250. * of change_page_attr() that also works on memory holes that do not have
  251. * mem_map entry (pfn_valid() is false).
  252. *
  253. * See change_page_attr() documentation for more details.
  254. *
  255. * Modules and drivers should use the set_memory_* APIs instead.
  256. */
  257. static int
  258. change_page_attr_addr(unsigned long address, pgprot_t mask_set,
  259. pgprot_t mask_clr)
  260. {
  261. int err = 0, kernel_map = 0;
  262. unsigned long pfn;
  263. #ifdef CONFIG_X86_64
  264. if (address >= __START_KERNEL_map &&
  265. address < __START_KERNEL_map + KERNEL_TEXT_SIZE) {
  266. address = (unsigned long)__va(__pa((void *)address));
  267. kernel_map = 1;
  268. }
  269. #endif
  270. pfn = __pa(address) >> PAGE_SHIFT;
  271. if (!kernel_map || 1) {
  272. err = __change_page_attr(address, pfn, mask_set, mask_clr);
  273. if (err)
  274. return err;
  275. }
  276. #ifdef CONFIG_X86_64
  277. /*
  278. * Handle kernel mapping too which aliases part of
  279. * lowmem:
  280. */
  281. if (__pa(address) < KERNEL_TEXT_SIZE) {
  282. unsigned long addr2;
  283. addr2 = __pa(address) + __START_KERNEL_map - phys_base;
  284. /* Make sure the kernel mappings stay executable */
  285. pgprot_val(mask_clr) |= _PAGE_NX;
  286. /*
  287. * Our high aliases are imprecise, so do not propagate
  288. * failures back to users:
  289. */
  290. __change_page_attr(addr2, pfn, mask_set, mask_clr);
  291. }
  292. #endif
  293. return err;
  294. }
  295. static int __change_page_attr_set_clr(unsigned long addr, int numpages,
  296. pgprot_t mask_set, pgprot_t mask_clr)
  297. {
  298. unsigned int i;
  299. int ret;
  300. for (i = 0; i < numpages ; i++, addr += PAGE_SIZE) {
  301. ret = change_page_attr_addr(addr, mask_set, mask_clr);
  302. if (ret)
  303. return ret;
  304. }
  305. return 0;
  306. }
  307. static int change_page_attr_set_clr(unsigned long addr, int numpages,
  308. pgprot_t mask_set, pgprot_t mask_clr)
  309. {
  310. int ret = __change_page_attr_set_clr(addr, numpages, mask_set,
  311. mask_clr);
  312. /*
  313. * On success we use clflush, when the CPU supports it to
  314. * avoid the wbindv. If the CPU does not support it and in the
  315. * error case we fall back to cpa_flush_all (which uses
  316. * wbindv):
  317. */
  318. if (!ret && cpu_has_clflush)
  319. cpa_flush_range(addr, numpages);
  320. else
  321. cpa_flush_all();
  322. return ret;
  323. }
  324. static inline int change_page_attr_set(unsigned long addr, int numpages,
  325. pgprot_t mask)
  326. {
  327. return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0));
  328. }
  329. static inline int change_page_attr_clear(unsigned long addr, int numpages,
  330. pgprot_t mask)
  331. {
  332. return __change_page_attr_set_clr(addr, numpages, __pgprot(0), mask);
  333. }
  334. int set_memory_uc(unsigned long addr, int numpages)
  335. {
  336. return change_page_attr_set(addr, numpages,
  337. __pgprot(_PAGE_PCD | _PAGE_PWT));
  338. }
  339. EXPORT_SYMBOL(set_memory_uc);
  340. int set_memory_wb(unsigned long addr, int numpages)
  341. {
  342. return change_page_attr_clear(addr, numpages,
  343. __pgprot(_PAGE_PCD | _PAGE_PWT));
  344. }
  345. EXPORT_SYMBOL(set_memory_wb);
  346. int set_memory_x(unsigned long addr, int numpages)
  347. {
  348. return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_NX));
  349. }
  350. EXPORT_SYMBOL(set_memory_x);
  351. int set_memory_nx(unsigned long addr, int numpages)
  352. {
  353. return change_page_attr_set(addr, numpages, __pgprot(_PAGE_NX));
  354. }
  355. EXPORT_SYMBOL(set_memory_nx);
  356. int set_memory_ro(unsigned long addr, int numpages)
  357. {
  358. return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_RW));
  359. }
  360. int set_memory_rw(unsigned long addr, int numpages)
  361. {
  362. return change_page_attr_set(addr, numpages, __pgprot(_PAGE_RW));
  363. }
  364. int set_memory_np(unsigned long addr, int numpages)
  365. {
  366. return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_PRESENT));
  367. }
  368. int set_pages_uc(struct page *page, int numpages)
  369. {
  370. unsigned long addr = (unsigned long)page_address(page);
  371. return set_memory_uc(addr, numpages);
  372. }
  373. EXPORT_SYMBOL(set_pages_uc);
  374. int set_pages_wb(struct page *page, int numpages)
  375. {
  376. unsigned long addr = (unsigned long)page_address(page);
  377. return set_memory_wb(addr, numpages);
  378. }
  379. EXPORT_SYMBOL(set_pages_wb);
  380. int set_pages_x(struct page *page, int numpages)
  381. {
  382. unsigned long addr = (unsigned long)page_address(page);
  383. return set_memory_x(addr, numpages);
  384. }
  385. EXPORT_SYMBOL(set_pages_x);
  386. int set_pages_nx(struct page *page, int numpages)
  387. {
  388. unsigned long addr = (unsigned long)page_address(page);
  389. return set_memory_nx(addr, numpages);
  390. }
  391. EXPORT_SYMBOL(set_pages_nx);
  392. int set_pages_ro(struct page *page, int numpages)
  393. {
  394. unsigned long addr = (unsigned long)page_address(page);
  395. return set_memory_ro(addr, numpages);
  396. }
  397. int set_pages_rw(struct page *page, int numpages)
  398. {
  399. unsigned long addr = (unsigned long)page_address(page);
  400. return set_memory_rw(addr, numpages);
  401. }
  402. #if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_CPA_DEBUG)
  403. static inline int __change_page_attr_set(unsigned long addr, int numpages,
  404. pgprot_t mask)
  405. {
  406. return __change_page_attr_set_clr(addr, numpages, mask, __pgprot(0));
  407. }
  408. static inline int __change_page_attr_clear(unsigned long addr, int numpages,
  409. pgprot_t mask)
  410. {
  411. return __change_page_attr_set_clr(addr, numpages, __pgprot(0), mask);
  412. }
  413. #endif
  414. #ifdef CONFIG_DEBUG_PAGEALLOC
  415. static int __set_pages_p(struct page *page, int numpages)
  416. {
  417. unsigned long addr = (unsigned long)page_address(page);
  418. return __change_page_attr_set(addr, numpages,
  419. __pgprot(_PAGE_PRESENT | _PAGE_RW));
  420. }
  421. static int __set_pages_np(struct page *page, int numpages)
  422. {
  423. unsigned long addr = (unsigned long)page_address(page);
  424. return __change_page_attr_clear(addr, numpages,
  425. __pgprot(_PAGE_PRESENT));
  426. }
  427. void kernel_map_pages(struct page *page, int numpages, int enable)
  428. {
  429. if (PageHighMem(page))
  430. return;
  431. if (!enable) {
  432. debug_check_no_locks_freed(page_address(page),
  433. numpages * PAGE_SIZE);
  434. }
  435. /*
  436. * If page allocator is not up yet then do not call c_p_a():
  437. */
  438. if (!debug_pagealloc_enabled)
  439. return;
  440. /*
  441. * The return value is ignored - the calls cannot fail,
  442. * large pages are disabled at boot time:
  443. */
  444. if (enable)
  445. __set_pages_p(page, numpages);
  446. else
  447. __set_pages_np(page, numpages);
  448. /*
  449. * We should perform an IPI and flush all tlbs,
  450. * but that can deadlock->flush only current cpu:
  451. */
  452. __flush_tlb_all();
  453. }
  454. #endif
  455. /*
  456. * The testcases use internal knowledge of the implementation that shouldn't
  457. * be exposed to the rest of the kernel. Include these directly here.
  458. */
  459. #ifdef CONFIG_CPA_DEBUG
  460. #include "pageattr-test.c"
  461. #endif