pageattr.c 13 KB

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