pageattr.c 13 KB

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