pageattr.c 12 KB

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