pageattr.c 19 KB

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