pageattr.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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. struct cpa_data {
  18. unsigned long vaddr;
  19. pgprot_t mask_set;
  20. pgprot_t mask_clr;
  21. int numpages;
  22. int flushtlb;
  23. };
  24. enum {
  25. CPA_NO_SPLIT = 0,
  26. CPA_SPLIT,
  27. };
  28. static inline int
  29. within(unsigned long addr, unsigned long start, unsigned long end)
  30. {
  31. return addr >= start && addr < end;
  32. }
  33. /*
  34. * Flushing functions
  35. */
  36. /**
  37. * clflush_cache_range - flush a cache range with clflush
  38. * @addr: virtual start address
  39. * @size: number of bytes to flush
  40. *
  41. * clflush is an unordered instruction which needs fencing with mfence
  42. * to avoid ordering issues.
  43. */
  44. void clflush_cache_range(void *vaddr, unsigned int size)
  45. {
  46. void *vend = vaddr + size - 1;
  47. mb();
  48. for (; vaddr < vend; vaddr += boot_cpu_data.x86_clflush_size)
  49. clflush(vaddr);
  50. /*
  51. * Flush any possible final partial cacheline:
  52. */
  53. clflush(vend);
  54. mb();
  55. }
  56. static void __cpa_flush_all(void *arg)
  57. {
  58. unsigned long cache = (unsigned long)arg;
  59. /*
  60. * Flush all to work around Errata in early athlons regarding
  61. * large page flushing.
  62. */
  63. __flush_tlb_all();
  64. if (cache && boot_cpu_data.x86_model >= 4)
  65. wbinvd();
  66. }
  67. static void cpa_flush_all(unsigned long cache)
  68. {
  69. BUG_ON(irqs_disabled());
  70. on_each_cpu(__cpa_flush_all, (void *) cache, 1, 1);
  71. }
  72. static void __cpa_flush_range(void *arg)
  73. {
  74. /*
  75. * We could optimize that further and do individual per page
  76. * tlb invalidates for a low number of pages. Caveat: we must
  77. * flush the high aliases on 64bit as well.
  78. */
  79. __flush_tlb_all();
  80. }
  81. static void cpa_flush_range(unsigned long start, int numpages, int cache)
  82. {
  83. unsigned int i, level;
  84. unsigned long addr;
  85. BUG_ON(irqs_disabled());
  86. WARN_ON(PAGE_ALIGN(start) != start);
  87. on_each_cpu(__cpa_flush_range, NULL, 1, 1);
  88. if (!cache)
  89. return;
  90. /*
  91. * We only need to flush on one CPU,
  92. * clflush is a MESI-coherent instruction that
  93. * will cause all other CPUs to flush the same
  94. * cachelines:
  95. */
  96. for (i = 0, addr = start; i < numpages; i++, addr += PAGE_SIZE) {
  97. pte_t *pte = lookup_address(addr, &level);
  98. /*
  99. * Only flush present addresses:
  100. */
  101. if (pte && pte_present(*pte))
  102. clflush_cache_range((void *) addr, PAGE_SIZE);
  103. }
  104. }
  105. #define HIGH_MAP_START __START_KERNEL_map
  106. #define HIGH_MAP_END (__START_KERNEL_map + KERNEL_TEXT_SIZE)
  107. /*
  108. * Converts a virtual address to a X86-64 highmap address
  109. */
  110. static unsigned long virt_to_highmap(void *address)
  111. {
  112. #ifdef CONFIG_X86_64
  113. return __pa((unsigned long)address) + HIGH_MAP_START - phys_base;
  114. #else
  115. return (unsigned long)address;
  116. #endif
  117. }
  118. /*
  119. * Certain areas of memory on x86 require very specific protection flags,
  120. * for example the BIOS area or kernel text. Callers don't always get this
  121. * right (again, ioremap() on BIOS memory is not uncommon) so this function
  122. * checks and fixes these known static required protection bits.
  123. */
  124. static inline pgprot_t static_protections(pgprot_t prot, unsigned long address)
  125. {
  126. pgprot_t forbidden = __pgprot(0);
  127. /*
  128. * The BIOS area between 640k and 1Mb needs to be executable for
  129. * PCI BIOS based config access (CONFIG_PCI_GOBIOS) support.
  130. */
  131. if (within(__pa(address), BIOS_BEGIN, BIOS_END))
  132. pgprot_val(forbidden) |= _PAGE_NX;
  133. /*
  134. * The kernel text needs to be executable for obvious reasons
  135. * Does not cover __inittext since that is gone later on
  136. */
  137. if (within(address, (unsigned long)_text, (unsigned long)_etext))
  138. pgprot_val(forbidden) |= _PAGE_NX;
  139. /*
  140. * Do the same for the x86-64 high kernel mapping
  141. */
  142. if (within(address, virt_to_highmap(_text), virt_to_highmap(_etext)))
  143. pgprot_val(forbidden) |= _PAGE_NX;
  144. #ifdef CONFIG_DEBUG_RODATA
  145. /* The .rodata section needs to be read-only */
  146. if (within(address, (unsigned long)__start_rodata,
  147. (unsigned long)__end_rodata))
  148. pgprot_val(forbidden) |= _PAGE_RW;
  149. /*
  150. * Do the same for the x86-64 high kernel mapping
  151. */
  152. if (within(address, virt_to_highmap(__start_rodata),
  153. virt_to_highmap(__end_rodata)))
  154. pgprot_val(forbidden) |= _PAGE_RW;
  155. #endif
  156. prot = __pgprot(pgprot_val(prot) & ~pgprot_val(forbidden));
  157. return prot;
  158. }
  159. /*
  160. * Lookup the page table entry for a virtual address. Return a pointer
  161. * to the entry and the level of the mapping.
  162. *
  163. * Note: We return pud and pmd either when the entry is marked large
  164. * or when the present bit is not set. Otherwise we would return a
  165. * pointer to a nonexisting mapping.
  166. */
  167. pte_t *lookup_address(unsigned long address, int *level)
  168. {
  169. pgd_t *pgd = pgd_offset_k(address);
  170. pud_t *pud;
  171. pmd_t *pmd;
  172. *level = PG_LEVEL_NONE;
  173. if (pgd_none(*pgd))
  174. return NULL;
  175. pud = pud_offset(pgd, address);
  176. if (pud_none(*pud))
  177. return NULL;
  178. pmd = pmd_offset(pud, address);
  179. if (pmd_none(*pmd))
  180. return NULL;
  181. *level = PG_LEVEL_2M;
  182. if (pmd_large(*pmd) || !pmd_present(*pmd))
  183. return (pte_t *)pmd;
  184. *level = PG_LEVEL_4K;
  185. return pte_offset_kernel(pmd, address);
  186. }
  187. static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
  188. {
  189. /* change init_mm */
  190. set_pte_atomic(kpte, pte);
  191. #ifdef CONFIG_X86_32
  192. if (!SHARED_KERNEL_PMD) {
  193. struct page *page;
  194. list_for_each_entry(page, &pgd_list, lru) {
  195. pgd_t *pgd;
  196. pud_t *pud;
  197. pmd_t *pmd;
  198. pgd = (pgd_t *)page_address(page) + pgd_index(address);
  199. pud = pud_offset(pgd, address);
  200. pmd = pmd_offset(pud, address);
  201. set_pte_atomic((pte_t *)pmd, pte);
  202. }
  203. }
  204. #endif
  205. }
  206. static int try_preserve_large_page(pte_t *kpte, unsigned long address,
  207. struct cpa_data *cpa)
  208. {
  209. unsigned long nextpage_addr, numpages, pmask, psize, flags;
  210. pte_t new_pte, old_pte, *tmp;
  211. pgprot_t old_prot, new_prot;
  212. int level, res = CPA_SPLIT;
  213. /*
  214. * An Athlon 64 X2 showed hard hangs if we tried to preserve
  215. * largepages and changed the PSE entry from RW to RO.
  216. *
  217. * As AMD CPUs have a long series of erratas in this area,
  218. * (and none of the known ones seem to explain this hang),
  219. * disable this code until the hang can be debugged:
  220. */
  221. if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
  222. return res;
  223. spin_lock_irqsave(&pgd_lock, flags);
  224. /*
  225. * Check for races, another CPU might have split this page
  226. * up already:
  227. */
  228. tmp = lookup_address(address, &level);
  229. if (tmp != kpte)
  230. goto out_unlock;
  231. switch (level) {
  232. case PG_LEVEL_2M:
  233. psize = LARGE_PAGE_SIZE;
  234. pmask = LARGE_PAGE_MASK;
  235. break;
  236. case PG_LEVEL_1G:
  237. default:
  238. res = -EINVAL;
  239. goto out_unlock;
  240. }
  241. /*
  242. * Calculate the number of pages, which fit into this large
  243. * page starting at address:
  244. */
  245. nextpage_addr = (address + psize) & pmask;
  246. numpages = (nextpage_addr - address) >> PAGE_SHIFT;
  247. if (numpages < cpa->numpages)
  248. cpa->numpages = numpages;
  249. /*
  250. * We are safe now. Check whether the new pgprot is the same:
  251. */
  252. old_pte = *kpte;
  253. old_prot = new_prot = pte_pgprot(old_pte);
  254. pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
  255. pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
  256. new_prot = static_protections(new_prot, address);
  257. /*
  258. * If there are no changes, return. maxpages has been updated
  259. * above:
  260. */
  261. if (pgprot_val(new_prot) == pgprot_val(old_prot)) {
  262. res = CPA_NO_SPLIT;
  263. goto out_unlock;
  264. }
  265. /*
  266. * We need to change the attributes. Check, whether we can
  267. * change the large page in one go. We request a split, when
  268. * the address is not aligned and the number of pages is
  269. * smaller than the number of pages in the large page. Note
  270. * that we limited the number of possible pages already to
  271. * the number of pages in the large page.
  272. */
  273. if (address == (nextpage_addr - psize) && cpa->numpages == numpages) {
  274. /*
  275. * The address is aligned and the number of pages
  276. * covers the full page.
  277. */
  278. new_pte = pfn_pte(pte_pfn(old_pte), canon_pgprot(new_prot));
  279. __set_pmd_pte(kpte, address, new_pte);
  280. cpa->flushtlb = 1;
  281. res = CPA_NO_SPLIT;
  282. }
  283. out_unlock:
  284. spin_unlock_irqrestore(&pgd_lock, flags);
  285. return res;
  286. }
  287. static int split_large_page(pte_t *kpte, unsigned long address)
  288. {
  289. pgprot_t ref_prot = pte_pgprot(pte_clrhuge(*kpte));
  290. gfp_t gfp_flags = GFP_KERNEL;
  291. unsigned long flags, addr, pfn;
  292. pte_t *pbase, *tmp;
  293. struct page *base;
  294. unsigned int i, level;
  295. #ifdef CONFIG_DEBUG_PAGEALLOC
  296. gfp_flags = __GFP_HIGH | __GFP_NOFAIL | __GFP_NOWARN;
  297. gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
  298. #endif
  299. base = alloc_pages(gfp_flags, 0);
  300. if (!base)
  301. return -ENOMEM;
  302. spin_lock_irqsave(&pgd_lock, flags);
  303. /*
  304. * Check for races, another CPU might have split this page
  305. * up for us already:
  306. */
  307. tmp = lookup_address(address, &level);
  308. if (tmp != kpte) {
  309. WARN_ON_ONCE(1);
  310. goto out_unlock;
  311. }
  312. address = __pa(address);
  313. addr = address & LARGE_PAGE_MASK;
  314. pbase = (pte_t *)page_address(base);
  315. #ifdef CONFIG_X86_32
  316. paravirt_alloc_pt(&init_mm, page_to_pfn(base));
  317. #endif
  318. /*
  319. * Get the target pfn from the original entry:
  320. */
  321. pfn = pte_pfn(*kpte);
  322. for (i = 0; i < PTRS_PER_PTE; i++, pfn++)
  323. set_pte(&pbase[i], pfn_pte(pfn, ref_prot));
  324. /*
  325. * Install the new, split up pagetable. Important detail here:
  326. *
  327. * On Intel the NX bit of all levels must be cleared to make a
  328. * page executable. See section 4.13.2 of Intel 64 and IA-32
  329. * Architectures Software Developer's Manual).
  330. */
  331. ref_prot = pte_pgprot(pte_mkexec(pte_clrhuge(*kpte)));
  332. __set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
  333. base = NULL;
  334. out_unlock:
  335. spin_unlock_irqrestore(&pgd_lock, flags);
  336. if (base)
  337. __free_pages(base, 0);
  338. return 0;
  339. }
  340. static int __change_page_attr(unsigned long address, struct cpa_data *cpa)
  341. {
  342. struct page *kpte_page;
  343. int level, res;
  344. pte_t *kpte;
  345. repeat:
  346. kpte = lookup_address(address, &level);
  347. if (!kpte)
  348. return -EINVAL;
  349. kpte_page = virt_to_page(kpte);
  350. BUG_ON(PageLRU(kpte_page));
  351. BUG_ON(PageCompound(kpte_page));
  352. if (level == PG_LEVEL_4K) {
  353. pte_t new_pte, old_pte = *kpte;
  354. pgprot_t new_prot = pte_pgprot(old_pte);
  355. if(!pte_val(old_pte)) {
  356. printk(KERN_WARNING "CPA: called for zero pte. "
  357. "vaddr = %lx cpa->vaddr = %lx\n", address,
  358. cpa->vaddr);
  359. WARN_ON(1);
  360. return -EINVAL;
  361. }
  362. pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
  363. pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
  364. new_prot = static_protections(new_prot, address);
  365. /*
  366. * We need to keep the pfn from the existing PTE,
  367. * after all we're only going to change it's attributes
  368. * not the memory it points to
  369. */
  370. new_pte = pfn_pte(pte_pfn(old_pte), canon_pgprot(new_prot));
  371. /*
  372. * Do we really change anything ?
  373. */
  374. if (pte_val(old_pte) != pte_val(new_pte)) {
  375. set_pte_atomic(kpte, new_pte);
  376. cpa->flushtlb = 1;
  377. }
  378. cpa->numpages = 1;
  379. return 0;
  380. }
  381. /*
  382. * Check, whether we can keep the large page intact
  383. * and just change the pte:
  384. */
  385. res = try_preserve_large_page(kpte, address, cpa);
  386. if (res < 0)
  387. return res;
  388. /*
  389. * When the range fits into the existing large page,
  390. * return. cp->numpages and cpa->tlbflush have been updated in
  391. * try_large_page:
  392. */
  393. if (res == CPA_NO_SPLIT)
  394. return 0;
  395. /*
  396. * We have to split the large page:
  397. */
  398. res = split_large_page(kpte, address);
  399. if (res)
  400. return res;
  401. cpa->flushtlb = 1;
  402. goto repeat;
  403. }
  404. /**
  405. * change_page_attr_addr - Change page table attributes in linear mapping
  406. * @address: Virtual address in linear mapping.
  407. * @prot: New page table attribute (PAGE_*)
  408. *
  409. * Change page attributes of a page in the direct mapping. This is a variant
  410. * of change_page_attr() that also works on memory holes that do not have
  411. * mem_map entry (pfn_valid() is false).
  412. *
  413. * See change_page_attr() documentation for more details.
  414. *
  415. * Modules and drivers should use the set_memory_* APIs instead.
  416. */
  417. static int change_page_attr_addr(struct cpa_data *cpa)
  418. {
  419. int err;
  420. unsigned long address = cpa->vaddr;
  421. #ifdef CONFIG_X86_64
  422. unsigned long phys_addr = __pa(address);
  423. /*
  424. * If we are inside the high mapped kernel range, then we
  425. * fixup the low mapping first. __va() returns the virtual
  426. * address in the linear mapping:
  427. */
  428. if (within(address, HIGH_MAP_START, HIGH_MAP_END))
  429. address = (unsigned long) __va(phys_addr);
  430. #endif
  431. err = __change_page_attr(address, cpa);
  432. if (err)
  433. return err;
  434. #ifdef CONFIG_X86_64
  435. /*
  436. * If the physical address is inside the kernel map, we need
  437. * to touch the high mapped kernel as well:
  438. */
  439. if (within(phys_addr, 0, KERNEL_TEXT_SIZE)) {
  440. /*
  441. * Calc the high mapping address. See __phys_addr()
  442. * for the non obvious details.
  443. *
  444. * Note that NX and other required permissions are
  445. * checked in static_protections().
  446. */
  447. address = phys_addr + HIGH_MAP_START - phys_base;
  448. /*
  449. * Our high aliases are imprecise, because we check
  450. * everything between 0 and KERNEL_TEXT_SIZE, so do
  451. * not propagate lookup failures back to users:
  452. */
  453. __change_page_attr(address, cpa);
  454. }
  455. #endif
  456. return err;
  457. }
  458. static int __change_page_attr_set_clr(struct cpa_data *cpa)
  459. {
  460. int ret, numpages = cpa->numpages;
  461. while (numpages) {
  462. /*
  463. * Store the remaining nr of pages for the large page
  464. * preservation check.
  465. */
  466. cpa->numpages = numpages;
  467. ret = change_page_attr_addr(cpa);
  468. if (ret)
  469. return ret;
  470. /*
  471. * Adjust the number of pages with the result of the
  472. * CPA operation. Either a large page has been
  473. * preserved or a single page update happened.
  474. */
  475. BUG_ON(cpa->numpages > numpages);
  476. numpages -= cpa->numpages;
  477. cpa->vaddr += cpa->numpages * PAGE_SIZE;
  478. }
  479. return 0;
  480. }
  481. static inline int cache_attr(pgprot_t attr)
  482. {
  483. return pgprot_val(attr) &
  484. (_PAGE_PAT | _PAGE_PAT_LARGE | _PAGE_PWT | _PAGE_PCD);
  485. }
  486. static int change_page_attr_set_clr(unsigned long addr, int numpages,
  487. pgprot_t mask_set, pgprot_t mask_clr)
  488. {
  489. struct cpa_data cpa;
  490. int ret, cache;
  491. /*
  492. * Check, if we are requested to change a not supported
  493. * feature:
  494. */
  495. mask_set = canon_pgprot(mask_set);
  496. mask_clr = canon_pgprot(mask_clr);
  497. if (!pgprot_val(mask_set) && !pgprot_val(mask_clr))
  498. return 0;
  499. cpa.vaddr = addr;
  500. cpa.numpages = numpages;
  501. cpa.mask_set = mask_set;
  502. cpa.mask_clr = mask_clr;
  503. cpa.flushtlb = 0;
  504. ret = __change_page_attr_set_clr(&cpa);
  505. /*
  506. * Check whether we really changed something:
  507. */
  508. if (!cpa.flushtlb)
  509. return ret;
  510. /*
  511. * No need to flush, when we did not set any of the caching
  512. * attributes:
  513. */
  514. cache = cache_attr(mask_set);
  515. /*
  516. * On success we use clflush, when the CPU supports it to
  517. * avoid the wbindv. If the CPU does not support it and in the
  518. * error case we fall back to cpa_flush_all (which uses
  519. * wbindv):
  520. */
  521. if (!ret && cpu_has_clflush)
  522. cpa_flush_range(addr, numpages, cache);
  523. else
  524. cpa_flush_all(cache);
  525. return ret;
  526. }
  527. static inline int change_page_attr_set(unsigned long addr, int numpages,
  528. pgprot_t mask)
  529. {
  530. return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0));
  531. }
  532. static inline int change_page_attr_clear(unsigned long addr, int numpages,
  533. pgprot_t mask)
  534. {
  535. return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask);
  536. }
  537. int set_memory_uc(unsigned long addr, int numpages)
  538. {
  539. return change_page_attr_set(addr, numpages,
  540. __pgprot(_PAGE_PCD | _PAGE_PWT));
  541. }
  542. EXPORT_SYMBOL(set_memory_uc);
  543. int set_memory_wb(unsigned long addr, int numpages)
  544. {
  545. return change_page_attr_clear(addr, numpages,
  546. __pgprot(_PAGE_PCD | _PAGE_PWT));
  547. }
  548. EXPORT_SYMBOL(set_memory_wb);
  549. int set_memory_x(unsigned long addr, int numpages)
  550. {
  551. return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_NX));
  552. }
  553. EXPORT_SYMBOL(set_memory_x);
  554. int set_memory_nx(unsigned long addr, int numpages)
  555. {
  556. return change_page_attr_set(addr, numpages, __pgprot(_PAGE_NX));
  557. }
  558. EXPORT_SYMBOL(set_memory_nx);
  559. int set_memory_ro(unsigned long addr, int numpages)
  560. {
  561. return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_RW));
  562. }
  563. int set_memory_rw(unsigned long addr, int numpages)
  564. {
  565. return change_page_attr_set(addr, numpages, __pgprot(_PAGE_RW));
  566. }
  567. int set_memory_np(unsigned long addr, int numpages)
  568. {
  569. return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_PRESENT));
  570. }
  571. int set_pages_uc(struct page *page, int numpages)
  572. {
  573. unsigned long addr = (unsigned long)page_address(page);
  574. return set_memory_uc(addr, numpages);
  575. }
  576. EXPORT_SYMBOL(set_pages_uc);
  577. int set_pages_wb(struct page *page, int numpages)
  578. {
  579. unsigned long addr = (unsigned long)page_address(page);
  580. return set_memory_wb(addr, numpages);
  581. }
  582. EXPORT_SYMBOL(set_pages_wb);
  583. int set_pages_x(struct page *page, int numpages)
  584. {
  585. unsigned long addr = (unsigned long)page_address(page);
  586. return set_memory_x(addr, numpages);
  587. }
  588. EXPORT_SYMBOL(set_pages_x);
  589. int set_pages_nx(struct page *page, int numpages)
  590. {
  591. unsigned long addr = (unsigned long)page_address(page);
  592. return set_memory_nx(addr, numpages);
  593. }
  594. EXPORT_SYMBOL(set_pages_nx);
  595. int set_pages_ro(struct page *page, int numpages)
  596. {
  597. unsigned long addr = (unsigned long)page_address(page);
  598. return set_memory_ro(addr, numpages);
  599. }
  600. int set_pages_rw(struct page *page, int numpages)
  601. {
  602. unsigned long addr = (unsigned long)page_address(page);
  603. return set_memory_rw(addr, numpages);
  604. }
  605. #ifdef CONFIG_DEBUG_PAGEALLOC
  606. static int __set_pages_p(struct page *page, int numpages)
  607. {
  608. struct cpa_data cpa = { .vaddr = (unsigned long) page_address(page),
  609. .numpages = numpages,
  610. .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW),
  611. .mask_clr = __pgprot(0)};
  612. return __change_page_attr_set_clr(&cpa);
  613. }
  614. static int __set_pages_np(struct page *page, int numpages)
  615. {
  616. struct cpa_data cpa = { .vaddr = (unsigned long) page_address(page),
  617. .numpages = numpages,
  618. .mask_set = __pgprot(0),
  619. .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW)};
  620. return __change_page_attr_set_clr(&cpa);
  621. }
  622. void kernel_map_pages(struct page *page, int numpages, int enable)
  623. {
  624. if (PageHighMem(page))
  625. return;
  626. if (!enable) {
  627. debug_check_no_locks_freed(page_address(page),
  628. numpages * PAGE_SIZE);
  629. }
  630. /*
  631. * If page allocator is not up yet then do not call c_p_a():
  632. */
  633. if (!debug_pagealloc_enabled)
  634. return;
  635. /*
  636. * The return value is ignored - the calls cannot fail,
  637. * large pages are disabled at boot time:
  638. */
  639. if (enable)
  640. __set_pages_p(page, numpages);
  641. else
  642. __set_pages_np(page, numpages);
  643. /*
  644. * We should perform an IPI and flush all tlbs,
  645. * but that can deadlock->flush only current cpu:
  646. */
  647. __flush_tlb_all();
  648. }
  649. #endif
  650. /*
  651. * The testcases use internal knowledge of the implementation that shouldn't
  652. * be exposed to the rest of the kernel. Include these directly here.
  653. */
  654. #ifdef CONFIG_CPA_DEBUG
  655. #include "pageattr-test.c"
  656. #endif