pageattr.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. set_pte_atomic(kpte, pfn_pte(pfn, canon_pgprot(prot)));
  183. } else {
  184. err = split_large_page(kpte, address);
  185. if (!err)
  186. goto repeat;
  187. }
  188. return err;
  189. }
  190. /**
  191. * change_page_attr_addr - Change page table attributes in linear mapping
  192. * @address: Virtual address in linear mapping.
  193. * @prot: New page table attribute (PAGE_*)
  194. *
  195. * Change page attributes of a page in the direct mapping. This is a variant
  196. * of change_page_attr() that also works on memory holes that do not have
  197. * mem_map entry (pfn_valid() is false).
  198. *
  199. * See change_page_attr() documentation for more details.
  200. *
  201. * Modules and drivers should use the set_memory_* APIs instead.
  202. */
  203. static int change_page_attr_addr(unsigned long address, pgprot_t prot)
  204. {
  205. int err = 0, kernel_map = 0;
  206. unsigned long pfn = __pa(address) >> PAGE_SHIFT;
  207. #ifdef CONFIG_X86_64
  208. if (address >= __START_KERNEL_map &&
  209. address < __START_KERNEL_map + KERNEL_TEXT_SIZE) {
  210. address = (unsigned long)__va(__pa(address));
  211. kernel_map = 1;
  212. }
  213. #endif
  214. if (!kernel_map || pte_present(pfn_pte(0, prot))) {
  215. err = __change_page_attr(address, pfn, prot);
  216. if (err)
  217. return err;
  218. }
  219. #ifdef CONFIG_X86_64
  220. /*
  221. * Handle kernel mapping too which aliases part of
  222. * lowmem:
  223. */
  224. if (__pa(address) < KERNEL_TEXT_SIZE) {
  225. unsigned long addr2;
  226. pgprot_t prot2;
  227. addr2 = __START_KERNEL_map + __pa(address);
  228. /* Make sure the kernel mappings stay executable */
  229. prot2 = pte_pgprot(pte_mkexec(pfn_pte(0, prot)));
  230. err = __change_page_attr(addr2, pfn, prot2);
  231. }
  232. #endif
  233. return err;
  234. }
  235. /**
  236. * change_page_attr_set - Change page table attributes in the linear mapping.
  237. * @addr: Virtual address in linear mapping.
  238. * @numpages: Number of pages to change
  239. * @prot: Protection/caching type bits to set (PAGE_*)
  240. *
  241. * Returns 0 on success, otherwise a negated errno.
  242. *
  243. * This should be used when a page is mapped with a different caching policy
  244. * than write-back somewhere - some CPUs do not like it when mappings with
  245. * different caching policies exist. This changes the page attributes of the
  246. * in kernel linear mapping too.
  247. *
  248. * The caller needs to ensure that there are no conflicting mappings elsewhere
  249. * (e.g. in user space) * This function only deals with the kernel linear map.
  250. *
  251. * This function is different from change_page_attr() in that only selected bits
  252. * are impacted, all other bits remain as is.
  253. */
  254. static int change_page_attr_set(unsigned long addr, int numpages,
  255. pgprot_t prot)
  256. {
  257. pgprot_t current_prot;
  258. int level;
  259. pte_t *pte;
  260. int i, ret;
  261. for (i = 0; i < numpages ; i++) {
  262. pte = lookup_address(addr, &level);
  263. if (pte)
  264. current_prot = pte_pgprot(*pte);
  265. else
  266. pgprot_val(current_prot) = 0;
  267. pgprot_val(prot) = pgprot_val(current_prot) | pgprot_val(prot);
  268. ret = change_page_attr_addr(addr, prot);
  269. if (ret)
  270. return ret;
  271. addr += PAGE_SIZE;
  272. }
  273. return 0;
  274. }
  275. /**
  276. * change_page_attr_clear - Change page table attributes in the linear mapping.
  277. * @addr: Virtual address in linear mapping.
  278. * @numpages: Number of pages to change
  279. * @prot: Protection/caching type bits to clear (PAGE_*)
  280. *
  281. * Returns 0 on success, otherwise a negated errno.
  282. *
  283. * This should be used when a page is mapped with a different caching policy
  284. * than write-back somewhere - some CPUs do not like it when mappings with
  285. * different caching policies exist. This changes the page attributes of the
  286. * in kernel linear mapping too.
  287. *
  288. * The caller needs to ensure that there are no conflicting mappings elsewhere
  289. * (e.g. in user space) * This function only deals with the kernel linear map.
  290. *
  291. * This function is different from change_page_attr() in that only selected bits
  292. * are impacted, all other bits remain as is.
  293. */
  294. static int change_page_attr_clear(unsigned long addr, int numpages,
  295. pgprot_t prot)
  296. {
  297. pgprot_t current_prot;
  298. int level;
  299. pte_t *pte;
  300. int i, ret;
  301. for (i = 0; i < numpages; i++) {
  302. pte = lookup_address(addr, &level);
  303. if (pte)
  304. current_prot = pte_pgprot(*pte);
  305. else
  306. pgprot_val(current_prot) = 0;
  307. pgprot_val(prot) =
  308. pgprot_val(current_prot) & ~pgprot_val(prot);
  309. ret = change_page_attr_addr(addr, prot);
  310. if (ret)
  311. return ret;
  312. addr += PAGE_SIZE;
  313. }
  314. return 0;
  315. }
  316. int set_memory_uc(unsigned long addr, int numpages)
  317. {
  318. int err;
  319. err = change_page_attr_set(addr, numpages,
  320. __pgprot(_PAGE_PCD | _PAGE_PWT));
  321. global_flush_tlb();
  322. return err;
  323. }
  324. EXPORT_SYMBOL(set_memory_uc);
  325. int set_memory_wb(unsigned long addr, int numpages)
  326. {
  327. int err;
  328. err = change_page_attr_clear(addr, numpages,
  329. __pgprot(_PAGE_PCD | _PAGE_PWT));
  330. global_flush_tlb();
  331. return err;
  332. }
  333. EXPORT_SYMBOL(set_memory_wb);
  334. int set_memory_x(unsigned long addr, int numpages)
  335. {
  336. int err;
  337. err = change_page_attr_clear(addr, numpages,
  338. __pgprot(_PAGE_NX));
  339. global_flush_tlb();
  340. return err;
  341. }
  342. EXPORT_SYMBOL(set_memory_x);
  343. int set_memory_nx(unsigned long addr, int numpages)
  344. {
  345. int err;
  346. err = change_page_attr_set(addr, numpages,
  347. __pgprot(_PAGE_NX));
  348. global_flush_tlb();
  349. return err;
  350. }
  351. EXPORT_SYMBOL(set_memory_nx);
  352. int set_memory_ro(unsigned long addr, int numpages)
  353. {
  354. int err;
  355. err = change_page_attr_clear(addr, numpages,
  356. __pgprot(_PAGE_RW));
  357. global_flush_tlb();
  358. return err;
  359. }
  360. int set_memory_rw(unsigned long addr, int numpages)
  361. {
  362. int err;
  363. err = change_page_attr_set(addr, numpages,
  364. __pgprot(_PAGE_RW));
  365. global_flush_tlb();
  366. return err;
  367. }
  368. int set_memory_np(unsigned long addr, int numpages)
  369. {
  370. int err;
  371. err = change_page_attr_clear(addr, numpages,
  372. __pgprot(_PAGE_PRESENT));
  373. global_flush_tlb();
  374. return err;
  375. }
  376. int set_pages_uc(struct page *page, int numpages)
  377. {
  378. unsigned long addr = (unsigned long)page_address(page);
  379. return set_memory_uc(addr, numpages);
  380. }
  381. EXPORT_SYMBOL(set_pages_uc);
  382. int set_pages_wb(struct page *page, int numpages)
  383. {
  384. unsigned long addr = (unsigned long)page_address(page);
  385. return set_memory_wb(addr, numpages);
  386. }
  387. EXPORT_SYMBOL(set_pages_wb);
  388. int set_pages_x(struct page *page, int numpages)
  389. {
  390. unsigned long addr = (unsigned long)page_address(page);
  391. return set_memory_x(addr, numpages);
  392. }
  393. EXPORT_SYMBOL(set_pages_x);
  394. int set_pages_nx(struct page *page, int numpages)
  395. {
  396. unsigned long addr = (unsigned long)page_address(page);
  397. return set_memory_nx(addr, numpages);
  398. }
  399. EXPORT_SYMBOL(set_pages_nx);
  400. int set_pages_ro(struct page *page, int numpages)
  401. {
  402. unsigned long addr = (unsigned long)page_address(page);
  403. return set_memory_ro(addr, numpages);
  404. }
  405. int set_pages_rw(struct page *page, int numpages)
  406. {
  407. unsigned long addr = (unsigned long)page_address(page);
  408. return set_memory_rw(addr, numpages);
  409. }
  410. #ifdef CONFIG_DEBUG_PAGEALLOC
  411. static int __set_pages_p(struct page *page, int numpages)
  412. {
  413. unsigned long addr = (unsigned long)page_address(page);
  414. return change_page_attr_set(addr, numpages,
  415. __pgprot(_PAGE_PRESENT | _PAGE_RW));
  416. }
  417. static int __set_pages_np(struct page *page, int numpages)
  418. {
  419. unsigned long addr = (unsigned long)page_address(page);
  420. return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_PRESENT));
  421. }
  422. void kernel_map_pages(struct page *page, int numpages, int enable)
  423. {
  424. if (PageHighMem(page))
  425. return;
  426. if (!enable) {
  427. debug_check_no_locks_freed(page_address(page),
  428. numpages * PAGE_SIZE);
  429. }
  430. /*
  431. * If page allocator is not up yet then do not call c_p_a():
  432. */
  433. if (!debug_pagealloc_enabled)
  434. return;
  435. /*
  436. * The return value is ignored - the calls cannot fail,
  437. * large pages are disabled at boot time:
  438. */
  439. if (enable)
  440. __set_pages_p(page, numpages);
  441. else
  442. __set_pages_np(page, numpages);
  443. /*
  444. * We should perform an IPI and flush all tlbs,
  445. * but that can deadlock->flush only current cpu:
  446. */
  447. __flush_tlb_all();
  448. }
  449. #endif
  450. /*
  451. * The testcases use internal knowledge of the implementation that shouldn't
  452. * be exposed to the rest of the kernel. Include these directly here.
  453. */
  454. #ifdef CONFIG_CPA_DEBUG
  455. #include "pageattr-test.c"
  456. #endif