pageattr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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. #define HIGH_MAP_START __START_KERNEL_map
  92. #define HIGH_MAP_END (__START_KERNEL_map + KERNEL_TEXT_SIZE)
  93. /*
  94. * Converts a virtual address to a X86-64 highmap address
  95. */
  96. static unsigned long virt_to_highmap(void *address)
  97. {
  98. #ifdef CONFIG_X86_64
  99. return __pa((unsigned long)address) + HIGH_MAP_START - phys_base;
  100. #else
  101. return (unsigned long)address;
  102. #endif
  103. }
  104. /*
  105. * Certain areas of memory on x86 require very specific protection flags,
  106. * for example the BIOS area or kernel text. Callers don't always get this
  107. * right (again, ioremap() on BIOS memory is not uncommon) so this function
  108. * checks and fixes these known static required protection bits.
  109. */
  110. static inline pgprot_t static_protections(pgprot_t prot, unsigned long address)
  111. {
  112. pgprot_t forbidden = __pgprot(0);
  113. /*
  114. * The BIOS area between 640k and 1Mb needs to be executable for
  115. * PCI BIOS based config access (CONFIG_PCI_GOBIOS) support.
  116. */
  117. if (within(__pa(address), BIOS_BEGIN, BIOS_END))
  118. pgprot_val(forbidden) |= _PAGE_NX;
  119. /*
  120. * The kernel text needs to be executable for obvious reasons
  121. * Does not cover __inittext since that is gone later on
  122. */
  123. if (within(address, (unsigned long)_text, (unsigned long)_etext))
  124. pgprot_val(forbidden) |= _PAGE_NX;
  125. /*
  126. * Do the same for the x86-64 high kernel mapping
  127. */
  128. if (within(address, virt_to_highmap(_text), virt_to_highmap(_etext)))
  129. pgprot_val(forbidden) |= _PAGE_NX;
  130. #ifdef CONFIG_DEBUG_RODATA
  131. /* The .rodata section needs to be read-only */
  132. if (within(address, (unsigned long)__start_rodata,
  133. (unsigned long)__end_rodata))
  134. pgprot_val(forbidden) |= _PAGE_RW;
  135. /*
  136. * Do the same for the x86-64 high kernel mapping
  137. */
  138. if (within(address, virt_to_highmap(__start_rodata),
  139. virt_to_highmap(__end_rodata)))
  140. pgprot_val(forbidden) |= _PAGE_RW;
  141. #endif
  142. prot = __pgprot(pgprot_val(prot) & ~pgprot_val(forbidden));
  143. return prot;
  144. }
  145. pte_t *lookup_address(unsigned long address, int *level)
  146. {
  147. pgd_t *pgd = pgd_offset_k(address);
  148. pud_t *pud;
  149. pmd_t *pmd;
  150. *level = PG_LEVEL_NONE;
  151. if (pgd_none(*pgd))
  152. return NULL;
  153. pud = pud_offset(pgd, address);
  154. if (pud_none(*pud))
  155. return NULL;
  156. pmd = pmd_offset(pud, address);
  157. if (pmd_none(*pmd))
  158. return NULL;
  159. *level = PG_LEVEL_2M;
  160. if (pmd_large(*pmd))
  161. return (pte_t *)pmd;
  162. *level = PG_LEVEL_4K;
  163. return pte_offset_kernel(pmd, address);
  164. }
  165. static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
  166. {
  167. /* change init_mm */
  168. set_pte_atomic(kpte, pte);
  169. #ifdef CONFIG_X86_32
  170. if (!SHARED_KERNEL_PMD) {
  171. struct page *page;
  172. list_for_each_entry(page, &pgd_list, lru) {
  173. pgd_t *pgd;
  174. pud_t *pud;
  175. pmd_t *pmd;
  176. pgd = (pgd_t *)page_address(page) + pgd_index(address);
  177. pud = pud_offset(pgd, address);
  178. pmd = pmd_offset(pud, address);
  179. set_pte_atomic((pte_t *)pmd, pte);
  180. }
  181. }
  182. #endif
  183. }
  184. static int split_large_page(pte_t *kpte, unsigned long address)
  185. {
  186. pgprot_t ref_prot = pte_pgprot(pte_clrhuge(*kpte));
  187. gfp_t gfp_flags = GFP_KERNEL;
  188. unsigned long flags;
  189. unsigned long addr;
  190. pte_t *pbase, *tmp;
  191. struct page *base;
  192. unsigned int i, level;
  193. #ifdef CONFIG_DEBUG_PAGEALLOC
  194. gfp_flags = __GFP_HIGH | __GFP_NOFAIL | __GFP_NOWARN;
  195. gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
  196. #endif
  197. base = alloc_pages(gfp_flags, 0);
  198. if (!base)
  199. return -ENOMEM;
  200. spin_lock_irqsave(&pgd_lock, flags);
  201. /*
  202. * Check for races, another CPU might have split this page
  203. * up for us already:
  204. */
  205. tmp = lookup_address(address, &level);
  206. if (tmp != kpte) {
  207. WARN_ON_ONCE(1);
  208. goto out_unlock;
  209. }
  210. address = __pa(address);
  211. addr = address & LARGE_PAGE_MASK;
  212. pbase = (pte_t *)page_address(base);
  213. #ifdef CONFIG_X86_32
  214. paravirt_alloc_pt(&init_mm, page_to_pfn(base));
  215. #endif
  216. for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE)
  217. set_pte(&pbase[i], pfn_pte(addr >> PAGE_SHIFT, ref_prot));
  218. /*
  219. * Install the new, split up pagetable. Important detail here:
  220. *
  221. * On Intel the NX bit of all levels must be cleared to make a
  222. * page executable. See section 4.13.2 of Intel 64 and IA-32
  223. * Architectures Software Developer's Manual).
  224. */
  225. ref_prot = pte_pgprot(pte_mkexec(pte_clrhuge(*kpte)));
  226. __set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
  227. base = NULL;
  228. out_unlock:
  229. spin_unlock_irqrestore(&pgd_lock, flags);
  230. if (base)
  231. __free_pages(base, 0);
  232. return 0;
  233. }
  234. static int
  235. __change_page_attr(unsigned long address, pgprot_t mask_set, pgprot_t mask_clr)
  236. {
  237. struct page *kpte_page;
  238. int level, err = 0;
  239. pte_t *kpte;
  240. repeat:
  241. kpte = lookup_address(address, &level);
  242. if (!kpte)
  243. return -EINVAL;
  244. kpte_page = virt_to_page(kpte);
  245. BUG_ON(PageLRU(kpte_page));
  246. BUG_ON(PageCompound(kpte_page));
  247. if (level == PG_LEVEL_4K) {
  248. pte_t new_pte, old_pte = *kpte;
  249. pgprot_t new_prot = pte_pgprot(old_pte);
  250. if(!pte_val(old_pte)) {
  251. WARN_ON_ONCE(1);
  252. return -EINVAL;
  253. }
  254. pgprot_val(new_prot) &= ~pgprot_val(mask_clr);
  255. pgprot_val(new_prot) |= pgprot_val(mask_set);
  256. new_prot = static_protections(new_prot, address);
  257. /*
  258. * We need to keep the pfn from the existing PTE,
  259. * after all we're only going to change it's attributes
  260. * not the memory it points to
  261. */
  262. new_pte = pfn_pte(pte_pfn(old_pte), canon_pgprot(new_prot));
  263. set_pte_atomic(kpte, new_pte);
  264. } else {
  265. err = split_large_page(kpte, address);
  266. if (!err)
  267. goto repeat;
  268. }
  269. return err;
  270. }
  271. /**
  272. * change_page_attr_addr - Change page table attributes in linear mapping
  273. * @address: Virtual address in linear mapping.
  274. * @prot: New page table attribute (PAGE_*)
  275. *
  276. * Change page attributes of a page in the direct mapping. This is a variant
  277. * of change_page_attr() that also works on memory holes that do not have
  278. * mem_map entry (pfn_valid() is false).
  279. *
  280. * See change_page_attr() documentation for more details.
  281. *
  282. * Modules and drivers should use the set_memory_* APIs instead.
  283. */
  284. static int
  285. change_page_attr_addr(unsigned long address, pgprot_t mask_set,
  286. pgprot_t mask_clr)
  287. {
  288. int err;
  289. #ifdef CONFIG_X86_64
  290. unsigned long phys_addr = __pa(address);
  291. /*
  292. * If we are inside the high mapped kernel range, then we
  293. * fixup the low mapping first. __va() returns the virtual
  294. * address in the linear mapping:
  295. */
  296. if (within(address, HIGH_MAP_START, HIGH_MAP_END))
  297. address = (unsigned long) __va(phys_addr);
  298. #endif
  299. err = __change_page_attr(address, mask_set, mask_clr);
  300. if (err)
  301. return err;
  302. #ifdef CONFIG_X86_64
  303. /*
  304. * If the physical address is inside the kernel map, we need
  305. * to touch the high mapped kernel as well:
  306. */
  307. if (within(phys_addr, 0, KERNEL_TEXT_SIZE)) {
  308. /*
  309. * Calc the high mapping address. See __phys_addr()
  310. * for the non obvious details.
  311. *
  312. * Note that NX and other required permissions are
  313. * checked in static_protections().
  314. */
  315. address = phys_addr + HIGH_MAP_START - phys_base;
  316. /*
  317. * Our high aliases are imprecise, because we check
  318. * everything between 0 and KERNEL_TEXT_SIZE, so do
  319. * not propagate lookup failures back to users:
  320. */
  321. __change_page_attr(address, mask_set, mask_clr);
  322. }
  323. #endif
  324. return err;
  325. }
  326. static int __change_page_attr_set_clr(unsigned long addr, int numpages,
  327. pgprot_t mask_set, pgprot_t mask_clr)
  328. {
  329. unsigned int i;
  330. int ret;
  331. for (i = 0; i < numpages ; i++, addr += PAGE_SIZE) {
  332. ret = change_page_attr_addr(addr, mask_set, mask_clr);
  333. if (ret)
  334. return ret;
  335. }
  336. return 0;
  337. }
  338. static int change_page_attr_set_clr(unsigned long addr, int numpages,
  339. pgprot_t mask_set, pgprot_t mask_clr)
  340. {
  341. int ret = __change_page_attr_set_clr(addr, numpages, mask_set,
  342. mask_clr);
  343. /*
  344. * On success we use clflush, when the CPU supports it to
  345. * avoid the wbindv. If the CPU does not support it and in the
  346. * error case we fall back to cpa_flush_all (which uses
  347. * wbindv):
  348. */
  349. if (!ret && cpu_has_clflush)
  350. cpa_flush_range(addr, numpages);
  351. else
  352. cpa_flush_all();
  353. return ret;
  354. }
  355. static inline int change_page_attr_set(unsigned long addr, int numpages,
  356. pgprot_t mask)
  357. {
  358. return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0));
  359. }
  360. static inline int change_page_attr_clear(unsigned long addr, int numpages,
  361. pgprot_t mask)
  362. {
  363. return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask);
  364. }
  365. int set_memory_uc(unsigned long addr, int numpages)
  366. {
  367. return change_page_attr_set(addr, numpages,
  368. __pgprot(_PAGE_PCD | _PAGE_PWT));
  369. }
  370. EXPORT_SYMBOL(set_memory_uc);
  371. int set_memory_wb(unsigned long addr, int numpages)
  372. {
  373. return change_page_attr_clear(addr, numpages,
  374. __pgprot(_PAGE_PCD | _PAGE_PWT));
  375. }
  376. EXPORT_SYMBOL(set_memory_wb);
  377. int set_memory_x(unsigned long addr, int numpages)
  378. {
  379. return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_NX));
  380. }
  381. EXPORT_SYMBOL(set_memory_x);
  382. int set_memory_nx(unsigned long addr, int numpages)
  383. {
  384. return change_page_attr_set(addr, numpages, __pgprot(_PAGE_NX));
  385. }
  386. EXPORT_SYMBOL(set_memory_nx);
  387. int set_memory_ro(unsigned long addr, int numpages)
  388. {
  389. return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_RW));
  390. }
  391. int set_memory_rw(unsigned long addr, int numpages)
  392. {
  393. return change_page_attr_set(addr, numpages, __pgprot(_PAGE_RW));
  394. }
  395. int set_memory_np(unsigned long addr, int numpages)
  396. {
  397. return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_PRESENT));
  398. }
  399. int set_pages_uc(struct page *page, int numpages)
  400. {
  401. unsigned long addr = (unsigned long)page_address(page);
  402. return set_memory_uc(addr, numpages);
  403. }
  404. EXPORT_SYMBOL(set_pages_uc);
  405. int set_pages_wb(struct page *page, int numpages)
  406. {
  407. unsigned long addr = (unsigned long)page_address(page);
  408. return set_memory_wb(addr, numpages);
  409. }
  410. EXPORT_SYMBOL(set_pages_wb);
  411. int set_pages_x(struct page *page, int numpages)
  412. {
  413. unsigned long addr = (unsigned long)page_address(page);
  414. return set_memory_x(addr, numpages);
  415. }
  416. EXPORT_SYMBOL(set_pages_x);
  417. int set_pages_nx(struct page *page, int numpages)
  418. {
  419. unsigned long addr = (unsigned long)page_address(page);
  420. return set_memory_nx(addr, numpages);
  421. }
  422. EXPORT_SYMBOL(set_pages_nx);
  423. int set_pages_ro(struct page *page, int numpages)
  424. {
  425. unsigned long addr = (unsigned long)page_address(page);
  426. return set_memory_ro(addr, numpages);
  427. }
  428. int set_pages_rw(struct page *page, int numpages)
  429. {
  430. unsigned long addr = (unsigned long)page_address(page);
  431. return set_memory_rw(addr, numpages);
  432. }
  433. #if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_CPA_DEBUG)
  434. static inline int __change_page_attr_set(unsigned long addr, int numpages,
  435. pgprot_t mask)
  436. {
  437. return __change_page_attr_set_clr(addr, numpages, mask, __pgprot(0));
  438. }
  439. static inline int __change_page_attr_clear(unsigned long addr, int numpages,
  440. pgprot_t mask)
  441. {
  442. return __change_page_attr_set_clr(addr, numpages, __pgprot(0), mask);
  443. }
  444. #endif
  445. #ifdef CONFIG_DEBUG_PAGEALLOC
  446. static int __set_pages_p(struct page *page, int numpages)
  447. {
  448. unsigned long addr = (unsigned long)page_address(page);
  449. return __change_page_attr_set(addr, numpages,
  450. __pgprot(_PAGE_PRESENT | _PAGE_RW));
  451. }
  452. static int __set_pages_np(struct page *page, int numpages)
  453. {
  454. unsigned long addr = (unsigned long)page_address(page);
  455. return __change_page_attr_clear(addr, numpages,
  456. __pgprot(_PAGE_PRESENT));
  457. }
  458. void kernel_map_pages(struct page *page, int numpages, int enable)
  459. {
  460. if (PageHighMem(page))
  461. return;
  462. if (!enable) {
  463. debug_check_no_locks_freed(page_address(page),
  464. numpages * PAGE_SIZE);
  465. }
  466. /*
  467. * If page allocator is not up yet then do not call c_p_a():
  468. */
  469. if (!debug_pagealloc_enabled)
  470. return;
  471. /*
  472. * The return value is ignored - the calls cannot fail,
  473. * large pages are disabled at boot time:
  474. */
  475. if (enable)
  476. __set_pages_p(page, numpages);
  477. else
  478. __set_pages_np(page, numpages);
  479. /*
  480. * We should perform an IPI and flush all tlbs,
  481. * but that can deadlock->flush only current cpu:
  482. */
  483. __flush_tlb_all();
  484. }
  485. #endif
  486. /*
  487. * The testcases use internal knowledge of the implementation that shouldn't
  488. * be exposed to the rest of the kernel. Include these directly here.
  489. */
  490. #ifdef CONFIG_CPA_DEBUG
  491. #include "pageattr-test.c"
  492. #endif