pageattr.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991
  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/sched.h>
  8. #include <linux/mm.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/seq_file.h>
  11. #include <linux/debugfs.h>
  12. #include <linux/pfn.h>
  13. #include <linux/percpu.h>
  14. #include <linux/gfp.h>
  15. #include <linux/pci.h>
  16. #include <linux/vmalloc.h>
  17. #include <asm/e820.h>
  18. #include <asm/processor.h>
  19. #include <asm/tlbflush.h>
  20. #include <asm/sections.h>
  21. #include <asm/setup.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/pgalloc.h>
  24. #include <asm/proto.h>
  25. #include <asm/pat.h>
  26. /*
  27. * The current flushing context - we pass it instead of 5 arguments:
  28. */
  29. struct cpa_data {
  30. unsigned long *vaddr;
  31. pgd_t *pgd;
  32. pgprot_t mask_set;
  33. pgprot_t mask_clr;
  34. unsigned long numpages;
  35. int flags;
  36. unsigned long pfn;
  37. unsigned force_split : 1;
  38. int curpage;
  39. struct page **pages;
  40. };
  41. /*
  42. * Serialize cpa() (for !DEBUG_PAGEALLOC which uses large identity mappings)
  43. * using cpa_lock. So that we don't allow any other cpu, with stale large tlb
  44. * entries change the page attribute in parallel to some other cpu
  45. * splitting a large page entry along with changing the attribute.
  46. */
  47. static DEFINE_SPINLOCK(cpa_lock);
  48. #define CPA_FLUSHTLB 1
  49. #define CPA_ARRAY 2
  50. #define CPA_PAGES_ARRAY 4
  51. #ifdef CONFIG_PROC_FS
  52. static unsigned long direct_pages_count[PG_LEVEL_NUM];
  53. void update_page_count(int level, unsigned long pages)
  54. {
  55. /* Protect against CPA */
  56. spin_lock(&pgd_lock);
  57. direct_pages_count[level] += pages;
  58. spin_unlock(&pgd_lock);
  59. }
  60. static void split_page_count(int level)
  61. {
  62. if (direct_pages_count[level] == 0)
  63. return;
  64. direct_pages_count[level]--;
  65. direct_pages_count[level - 1] += PTRS_PER_PTE;
  66. }
  67. void arch_report_meminfo(struct seq_file *m)
  68. {
  69. seq_printf(m, "DirectMap4k: %8lu kB\n",
  70. direct_pages_count[PG_LEVEL_4K] << 2);
  71. #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
  72. seq_printf(m, "DirectMap2M: %8lu kB\n",
  73. direct_pages_count[PG_LEVEL_2M] << 11);
  74. #else
  75. seq_printf(m, "DirectMap4M: %8lu kB\n",
  76. direct_pages_count[PG_LEVEL_2M] << 12);
  77. #endif
  78. if (direct_gbpages)
  79. seq_printf(m, "DirectMap1G: %8lu kB\n",
  80. direct_pages_count[PG_LEVEL_1G] << 20);
  81. }
  82. #else
  83. static inline void split_page_count(int level) { }
  84. #endif
  85. #ifdef CONFIG_X86_64
  86. static inline unsigned long highmap_start_pfn(void)
  87. {
  88. return __pa_symbol(_text) >> PAGE_SHIFT;
  89. }
  90. static inline unsigned long highmap_end_pfn(void)
  91. {
  92. /* Do not reference physical address outside the kernel. */
  93. return __pa_symbol(roundup(_brk_end, PMD_SIZE) - 1) >> PAGE_SHIFT;
  94. }
  95. #endif
  96. static inline int
  97. within(unsigned long addr, unsigned long start, unsigned long end)
  98. {
  99. return addr >= start && addr < end;
  100. }
  101. static inline int
  102. within_inclusive(unsigned long addr, unsigned long start, unsigned long end)
  103. {
  104. return addr >= start && addr <= end;
  105. }
  106. /*
  107. * Flushing functions
  108. */
  109. /**
  110. * clflush_cache_range - flush a cache range with clflush
  111. * @vaddr: virtual start address
  112. * @size: number of bytes to flush
  113. *
  114. * clflushopt is an unordered instruction which needs fencing with mfence or
  115. * sfence to avoid ordering issues.
  116. */
  117. void clflush_cache_range(void *vaddr, unsigned int size)
  118. {
  119. const unsigned long clflush_size = boot_cpu_data.x86_clflush_size;
  120. void *p = (void *)((unsigned long)vaddr & ~(clflush_size - 1));
  121. void *vend = vaddr + size;
  122. if (p >= vend)
  123. return;
  124. mb();
  125. for (; p < vend; p += clflush_size)
  126. clflushopt(p);
  127. mb();
  128. }
  129. EXPORT_SYMBOL_GPL(clflush_cache_range);
  130. static void __cpa_flush_all(void *arg)
  131. {
  132. unsigned long cache = (unsigned long)arg;
  133. /*
  134. * Flush all to work around Errata in early athlons regarding
  135. * large page flushing.
  136. */
  137. __flush_tlb_all();
  138. if (cache && boot_cpu_data.x86 >= 4)
  139. wbinvd();
  140. }
  141. static void cpa_flush_all(unsigned long cache)
  142. {
  143. BUG_ON(irqs_disabled());
  144. on_each_cpu(__cpa_flush_all, (void *) cache, 1);
  145. }
  146. static void __cpa_flush_range(void *arg)
  147. {
  148. /*
  149. * We could optimize that further and do individual per page
  150. * tlb invalidates for a low number of pages. Caveat: we must
  151. * flush the high aliases on 64bit as well.
  152. */
  153. __flush_tlb_all();
  154. }
  155. static void cpa_flush_range(unsigned long start, int numpages, int cache)
  156. {
  157. unsigned int i, level;
  158. unsigned long addr;
  159. BUG_ON(irqs_disabled());
  160. WARN_ON(PAGE_ALIGN(start) != start);
  161. on_each_cpu(__cpa_flush_range, NULL, 1);
  162. if (!cache)
  163. return;
  164. /*
  165. * We only need to flush on one CPU,
  166. * clflush is a MESI-coherent instruction that
  167. * will cause all other CPUs to flush the same
  168. * cachelines:
  169. */
  170. for (i = 0, addr = start; i < numpages; i++, addr += PAGE_SIZE) {
  171. pte_t *pte = lookup_address(addr, &level);
  172. /*
  173. * Only flush present addresses:
  174. */
  175. if (pte && (pte_val(*pte) & _PAGE_PRESENT))
  176. clflush_cache_range((void *) addr, PAGE_SIZE);
  177. }
  178. }
  179. static void cpa_flush_array(unsigned long *start, int numpages, int cache,
  180. int in_flags, struct page **pages)
  181. {
  182. unsigned int i, level;
  183. unsigned long do_wbinvd = cache && numpages >= 1024; /* 4M threshold */
  184. BUG_ON(irqs_disabled());
  185. on_each_cpu(__cpa_flush_all, (void *) do_wbinvd, 1);
  186. if (!cache || do_wbinvd)
  187. return;
  188. /*
  189. * We only need to flush on one CPU,
  190. * clflush is a MESI-coherent instruction that
  191. * will cause all other CPUs to flush the same
  192. * cachelines:
  193. */
  194. for (i = 0; i < numpages; i++) {
  195. unsigned long addr;
  196. pte_t *pte;
  197. if (in_flags & CPA_PAGES_ARRAY)
  198. addr = (unsigned long)page_address(pages[i]);
  199. else
  200. addr = start[i];
  201. pte = lookup_address(addr, &level);
  202. /*
  203. * Only flush present addresses:
  204. */
  205. if (pte && (pte_val(*pte) & _PAGE_PRESENT))
  206. clflush_cache_range((void *)addr, PAGE_SIZE);
  207. }
  208. }
  209. /*
  210. * Certain areas of memory on x86 require very specific protection flags,
  211. * for example the BIOS area or kernel text. Callers don't always get this
  212. * right (again, ioremap() on BIOS memory is not uncommon) so this function
  213. * checks and fixes these known static required protection bits.
  214. */
  215. static inline pgprot_t static_protections(pgprot_t prot, unsigned long address,
  216. unsigned long pfn)
  217. {
  218. pgprot_t forbidden = __pgprot(0);
  219. /*
  220. * The BIOS area between 640k and 1Mb needs to be executable for
  221. * PCI BIOS based config access (CONFIG_PCI_GOBIOS) support.
  222. */
  223. #ifdef CONFIG_PCI_BIOS
  224. if (pcibios_enabled && within(pfn, BIOS_BEGIN >> PAGE_SHIFT, BIOS_END >> PAGE_SHIFT))
  225. pgprot_val(forbidden) |= _PAGE_NX;
  226. #endif
  227. /*
  228. * The kernel text needs to be executable for obvious reasons
  229. * Does not cover __inittext since that is gone later on. On
  230. * 64bit we do not enforce !NX on the low mapping
  231. */
  232. if (within(address, (unsigned long)_text, (unsigned long)_etext))
  233. pgprot_val(forbidden) |= _PAGE_NX;
  234. /*
  235. * The .rodata section needs to be read-only. Using the pfn
  236. * catches all aliases.
  237. */
  238. if (within(pfn, __pa_symbol(__start_rodata) >> PAGE_SHIFT,
  239. __pa_symbol(__end_rodata) >> PAGE_SHIFT))
  240. pgprot_val(forbidden) |= _PAGE_RW;
  241. #if defined(CONFIG_X86_64)
  242. /*
  243. * Once the kernel maps the text as RO (kernel_set_to_readonly is set),
  244. * kernel text mappings for the large page aligned text, rodata sections
  245. * will be always read-only. For the kernel identity mappings covering
  246. * the holes caused by this alignment can be anything that user asks.
  247. *
  248. * This will preserve the large page mappings for kernel text/data
  249. * at no extra cost.
  250. */
  251. if (kernel_set_to_readonly &&
  252. within(address, (unsigned long)_text,
  253. (unsigned long)__end_rodata_hpage_align)) {
  254. unsigned int level;
  255. /*
  256. * Don't enforce the !RW mapping for the kernel text mapping,
  257. * if the current mapping is already using small page mapping.
  258. * No need to work hard to preserve large page mappings in this
  259. * case.
  260. *
  261. * This also fixes the Linux Xen paravirt guest boot failure
  262. * (because of unexpected read-only mappings for kernel identity
  263. * mappings). In this paravirt guest case, the kernel text
  264. * mapping and the kernel identity mapping share the same
  265. * page-table pages. Thus we can't really use different
  266. * protections for the kernel text and identity mappings. Also,
  267. * these shared mappings are made of small page mappings.
  268. * Thus this don't enforce !RW mapping for small page kernel
  269. * text mapping logic will help Linux Xen parvirt guest boot
  270. * as well.
  271. */
  272. if (lookup_address(address, &level) && (level != PG_LEVEL_4K))
  273. pgprot_val(forbidden) |= _PAGE_RW;
  274. }
  275. #endif
  276. prot = __pgprot(pgprot_val(prot) & ~pgprot_val(forbidden));
  277. return prot;
  278. }
  279. /*
  280. * Lookup the page table entry for a virtual address in a specific pgd.
  281. * Return a pointer to the entry and the level of the mapping.
  282. */
  283. pte_t *lookup_address_in_pgd(pgd_t *pgd, unsigned long address,
  284. unsigned int *level)
  285. {
  286. pud_t *pud;
  287. pmd_t *pmd;
  288. *level = PG_LEVEL_NONE;
  289. if (pgd_none(*pgd))
  290. return NULL;
  291. pud = pud_offset(pgd, address);
  292. if (pud_none(*pud))
  293. return NULL;
  294. *level = PG_LEVEL_1G;
  295. if (pud_large(*pud) || !pud_present(*pud))
  296. return (pte_t *)pud;
  297. pmd = pmd_offset(pud, address);
  298. if (pmd_none(*pmd))
  299. return NULL;
  300. *level = PG_LEVEL_2M;
  301. if (pmd_large(*pmd) || !pmd_present(*pmd))
  302. return (pte_t *)pmd;
  303. *level = PG_LEVEL_4K;
  304. return pte_offset_kernel(pmd, address);
  305. }
  306. /*
  307. * Lookup the page table entry for a virtual address. Return a pointer
  308. * to the entry and the level of the mapping.
  309. *
  310. * Note: We return pud and pmd either when the entry is marked large
  311. * or when the present bit is not set. Otherwise we would return a
  312. * pointer to a nonexisting mapping.
  313. */
  314. pte_t *lookup_address(unsigned long address, unsigned int *level)
  315. {
  316. return lookup_address_in_pgd(pgd_offset_k(address), address, level);
  317. }
  318. EXPORT_SYMBOL_GPL(lookup_address);
  319. static pte_t *_lookup_address_cpa(struct cpa_data *cpa, unsigned long address,
  320. unsigned int *level)
  321. {
  322. if (cpa->pgd)
  323. return lookup_address_in_pgd(cpa->pgd + pgd_index(address),
  324. address, level);
  325. return lookup_address(address, level);
  326. }
  327. /*
  328. * Lookup the PMD entry for a virtual address. Return a pointer to the entry
  329. * or NULL if not present.
  330. */
  331. pmd_t *lookup_pmd_address(unsigned long address)
  332. {
  333. pgd_t *pgd;
  334. pud_t *pud;
  335. pgd = pgd_offset_k(address);
  336. if (pgd_none(*pgd))
  337. return NULL;
  338. pud = pud_offset(pgd, address);
  339. if (pud_none(*pud) || pud_large(*pud) || !pud_present(*pud))
  340. return NULL;
  341. return pmd_offset(pud, address);
  342. }
  343. /*
  344. * This is necessary because __pa() does not work on some
  345. * kinds of memory, like vmalloc() or the alloc_remap()
  346. * areas on 32-bit NUMA systems. The percpu areas can
  347. * end up in this kind of memory, for instance.
  348. *
  349. * This could be optimized, but it is only intended to be
  350. * used at inititalization time, and keeping it
  351. * unoptimized should increase the testing coverage for
  352. * the more obscure platforms.
  353. */
  354. phys_addr_t slow_virt_to_phys(void *__virt_addr)
  355. {
  356. unsigned long virt_addr = (unsigned long)__virt_addr;
  357. phys_addr_t phys_addr;
  358. unsigned long offset;
  359. enum pg_level level;
  360. pte_t *pte;
  361. pte = lookup_address(virt_addr, &level);
  362. BUG_ON(!pte);
  363. /*
  364. * pXX_pfn() returns unsigned long, which must be cast to phys_addr_t
  365. * before being left-shifted PAGE_SHIFT bits -- this trick is to
  366. * make 32-PAE kernel work correctly.
  367. */
  368. switch (level) {
  369. case PG_LEVEL_1G:
  370. phys_addr = (phys_addr_t)pud_pfn(*(pud_t *)pte) << PAGE_SHIFT;
  371. offset = virt_addr & ~PUD_PAGE_MASK;
  372. break;
  373. case PG_LEVEL_2M:
  374. phys_addr = (phys_addr_t)pmd_pfn(*(pmd_t *)pte) << PAGE_SHIFT;
  375. offset = virt_addr & ~PMD_PAGE_MASK;
  376. break;
  377. default:
  378. phys_addr = (phys_addr_t)pte_pfn(*pte) << PAGE_SHIFT;
  379. offset = virt_addr & ~PAGE_MASK;
  380. }
  381. return (phys_addr_t)(phys_addr | offset);
  382. }
  383. EXPORT_SYMBOL_GPL(slow_virt_to_phys);
  384. /*
  385. * Set the new pmd in all the pgds we know about:
  386. */
  387. static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
  388. {
  389. /* change init_mm */
  390. set_pte_atomic(kpte, pte);
  391. #ifdef CONFIG_X86_32
  392. if (!SHARED_KERNEL_PMD) {
  393. struct page *page;
  394. list_for_each_entry(page, &pgd_list, lru) {
  395. pgd_t *pgd;
  396. pud_t *pud;
  397. pmd_t *pmd;
  398. pgd = (pgd_t *)page_address(page) + pgd_index(address);
  399. pud = pud_offset(pgd, address);
  400. pmd = pmd_offset(pud, address);
  401. set_pte_atomic((pte_t *)pmd, pte);
  402. }
  403. }
  404. #endif
  405. }
  406. static int
  407. try_preserve_large_page(pte_t *kpte, unsigned long address,
  408. struct cpa_data *cpa)
  409. {
  410. unsigned long nextpage_addr, numpages, pmask, psize, addr, pfn, old_pfn;
  411. pte_t new_pte, old_pte, *tmp;
  412. pgprot_t old_prot, new_prot, req_prot;
  413. int i, do_split = 1;
  414. enum pg_level level;
  415. if (cpa->force_split)
  416. return 1;
  417. spin_lock(&pgd_lock);
  418. /*
  419. * Check for races, another CPU might have split this page
  420. * up already:
  421. */
  422. tmp = _lookup_address_cpa(cpa, address, &level);
  423. if (tmp != kpte)
  424. goto out_unlock;
  425. switch (level) {
  426. case PG_LEVEL_2M:
  427. old_prot = pmd_pgprot(*(pmd_t *)kpte);
  428. old_pfn = pmd_pfn(*(pmd_t *)kpte);
  429. break;
  430. case PG_LEVEL_1G:
  431. old_prot = pud_pgprot(*(pud_t *)kpte);
  432. old_pfn = pud_pfn(*(pud_t *)kpte);
  433. break;
  434. default:
  435. do_split = -EINVAL;
  436. goto out_unlock;
  437. }
  438. psize = page_level_size(level);
  439. pmask = page_level_mask(level);
  440. /*
  441. * Calculate the number of pages, which fit into this large
  442. * page starting at address:
  443. */
  444. nextpage_addr = (address + psize) & pmask;
  445. numpages = (nextpage_addr - address) >> PAGE_SHIFT;
  446. if (numpages < cpa->numpages)
  447. cpa->numpages = numpages;
  448. /*
  449. * We are safe now. Check whether the new pgprot is the same:
  450. * Convert protection attributes to 4k-format, as cpa->mask* are set
  451. * up accordingly.
  452. */
  453. old_pte = *kpte;
  454. req_prot = pgprot_large_2_4k(old_prot);
  455. pgprot_val(req_prot) &= ~pgprot_val(cpa->mask_clr);
  456. pgprot_val(req_prot) |= pgprot_val(cpa->mask_set);
  457. /*
  458. * req_prot is in format of 4k pages. It must be converted to large
  459. * page format: the caching mode includes the PAT bit located at
  460. * different bit positions in the two formats.
  461. */
  462. req_prot = pgprot_4k_2_large(req_prot);
  463. /*
  464. * Set the PSE and GLOBAL flags only if the PRESENT flag is
  465. * set otherwise pmd_present/pmd_huge will return true even on
  466. * a non present pmd. The canon_pgprot will clear _PAGE_GLOBAL
  467. * for the ancient hardware that doesn't support it.
  468. */
  469. if (pgprot_val(req_prot) & _PAGE_PRESENT)
  470. pgprot_val(req_prot) |= _PAGE_PSE | _PAGE_GLOBAL;
  471. else
  472. pgprot_val(req_prot) &= ~(_PAGE_PSE | _PAGE_GLOBAL);
  473. req_prot = canon_pgprot(req_prot);
  474. /*
  475. * old_pfn points to the large page base pfn. So we need
  476. * to add the offset of the virtual address:
  477. */
  478. pfn = old_pfn + ((address & (psize - 1)) >> PAGE_SHIFT);
  479. cpa->pfn = pfn;
  480. new_prot = static_protections(req_prot, address, pfn);
  481. /*
  482. * We need to check the full range, whether
  483. * static_protection() requires a different pgprot for one of
  484. * the pages in the range we try to preserve:
  485. */
  486. addr = address & pmask;
  487. pfn = old_pfn;
  488. for (i = 0; i < (psize >> PAGE_SHIFT); i++, addr += PAGE_SIZE, pfn++) {
  489. pgprot_t chk_prot = static_protections(req_prot, addr, pfn);
  490. if (pgprot_val(chk_prot) != pgprot_val(new_prot))
  491. goto out_unlock;
  492. }
  493. /*
  494. * If there are no changes, return. maxpages has been updated
  495. * above:
  496. */
  497. if (pgprot_val(new_prot) == pgprot_val(old_prot)) {
  498. do_split = 0;
  499. goto out_unlock;
  500. }
  501. /*
  502. * We need to change the attributes. Check, whether we can
  503. * change the large page in one go. We request a split, when
  504. * the address is not aligned and the number of pages is
  505. * smaller than the number of pages in the large page. Note
  506. * that we limited the number of possible pages already to
  507. * the number of pages in the large page.
  508. */
  509. if (address == (address & pmask) && cpa->numpages == (psize >> PAGE_SHIFT)) {
  510. /*
  511. * The address is aligned and the number of pages
  512. * covers the full page.
  513. */
  514. new_pte = pfn_pte(old_pfn, new_prot);
  515. __set_pmd_pte(kpte, address, new_pte);
  516. cpa->flags |= CPA_FLUSHTLB;
  517. do_split = 0;
  518. }
  519. out_unlock:
  520. spin_unlock(&pgd_lock);
  521. return do_split;
  522. }
  523. static int
  524. __split_large_page(struct cpa_data *cpa, pte_t *kpte, unsigned long address,
  525. struct page *base)
  526. {
  527. pte_t *pbase = (pte_t *)page_address(base);
  528. unsigned long ref_pfn, pfn, pfninc = 1;
  529. unsigned int i, level;
  530. pte_t *tmp;
  531. pgprot_t ref_prot;
  532. spin_lock(&pgd_lock);
  533. /*
  534. * Check for races, another CPU might have split this page
  535. * up for us already:
  536. */
  537. tmp = _lookup_address_cpa(cpa, address, &level);
  538. if (tmp != kpte) {
  539. spin_unlock(&pgd_lock);
  540. return 1;
  541. }
  542. paravirt_alloc_pte(&init_mm, page_to_pfn(base));
  543. switch (level) {
  544. case PG_LEVEL_2M:
  545. ref_prot = pmd_pgprot(*(pmd_t *)kpte);
  546. /* clear PSE and promote PAT bit to correct position */
  547. ref_prot = pgprot_large_2_4k(ref_prot);
  548. ref_pfn = pmd_pfn(*(pmd_t *)kpte);
  549. break;
  550. case PG_LEVEL_1G:
  551. ref_prot = pud_pgprot(*(pud_t *)kpte);
  552. ref_pfn = pud_pfn(*(pud_t *)kpte);
  553. pfninc = PMD_PAGE_SIZE >> PAGE_SHIFT;
  554. /*
  555. * Clear the PSE flags if the PRESENT flag is not set
  556. * otherwise pmd_present/pmd_huge will return true
  557. * even on a non present pmd.
  558. */
  559. if (!(pgprot_val(ref_prot) & _PAGE_PRESENT))
  560. pgprot_val(ref_prot) &= ~_PAGE_PSE;
  561. break;
  562. default:
  563. spin_unlock(&pgd_lock);
  564. return 1;
  565. }
  566. /*
  567. * Set the GLOBAL flags only if the PRESENT flag is set
  568. * otherwise pmd/pte_present will return true even on a non
  569. * present pmd/pte. The canon_pgprot will clear _PAGE_GLOBAL
  570. * for the ancient hardware that doesn't support it.
  571. */
  572. if (pgprot_val(ref_prot) & _PAGE_PRESENT)
  573. pgprot_val(ref_prot) |= _PAGE_GLOBAL;
  574. else
  575. pgprot_val(ref_prot) &= ~_PAGE_GLOBAL;
  576. /*
  577. * Get the target pfn from the original entry:
  578. */
  579. pfn = ref_pfn;
  580. for (i = 0; i < PTRS_PER_PTE; i++, pfn += pfninc)
  581. set_pte(&pbase[i], pfn_pte(pfn, canon_pgprot(ref_prot)));
  582. if (virt_addr_valid(address)) {
  583. unsigned long pfn = PFN_DOWN(__pa(address));
  584. if (pfn_range_is_mapped(pfn, pfn + 1))
  585. split_page_count(level);
  586. }
  587. /*
  588. * Install the new, split up pagetable.
  589. *
  590. * We use the standard kernel pagetable protections for the new
  591. * pagetable protections, the actual ptes set above control the
  592. * primary protection behavior:
  593. */
  594. __set_pmd_pte(kpte, address, mk_pte(base, __pgprot(_KERNPG_TABLE)));
  595. /*
  596. * Intel Atom errata AAH41 workaround.
  597. *
  598. * The real fix should be in hw or in a microcode update, but
  599. * we also probabilistically try to reduce the window of having
  600. * a large TLB mixed with 4K TLBs while instruction fetches are
  601. * going on.
  602. */
  603. __flush_tlb_all();
  604. spin_unlock(&pgd_lock);
  605. return 0;
  606. }
  607. static int split_large_page(struct cpa_data *cpa, pte_t *kpte,
  608. unsigned long address)
  609. {
  610. struct page *base;
  611. if (!debug_pagealloc_enabled())
  612. spin_unlock(&cpa_lock);
  613. base = alloc_pages(GFP_KERNEL | __GFP_NOTRACK, 0);
  614. if (!debug_pagealloc_enabled())
  615. spin_lock(&cpa_lock);
  616. if (!base)
  617. return -ENOMEM;
  618. if (__split_large_page(cpa, kpte, address, base))
  619. __free_page(base);
  620. return 0;
  621. }
  622. static bool try_to_free_pte_page(pte_t *pte)
  623. {
  624. int i;
  625. for (i = 0; i < PTRS_PER_PTE; i++)
  626. if (!pte_none(pte[i]))
  627. return false;
  628. free_page((unsigned long)pte);
  629. return true;
  630. }
  631. static bool try_to_free_pmd_page(pmd_t *pmd)
  632. {
  633. int i;
  634. for (i = 0; i < PTRS_PER_PMD; i++)
  635. if (!pmd_none(pmd[i]))
  636. return false;
  637. free_page((unsigned long)pmd);
  638. return true;
  639. }
  640. static bool unmap_pte_range(pmd_t *pmd, unsigned long start, unsigned long end)
  641. {
  642. pte_t *pte = pte_offset_kernel(pmd, start);
  643. while (start < end) {
  644. set_pte(pte, __pte(0));
  645. start += PAGE_SIZE;
  646. pte++;
  647. }
  648. if (try_to_free_pte_page((pte_t *)pmd_page_vaddr(*pmd))) {
  649. pmd_clear(pmd);
  650. return true;
  651. }
  652. return false;
  653. }
  654. static void __unmap_pmd_range(pud_t *pud, pmd_t *pmd,
  655. unsigned long start, unsigned long end)
  656. {
  657. if (unmap_pte_range(pmd, start, end))
  658. if (try_to_free_pmd_page((pmd_t *)pud_page_vaddr(*pud)))
  659. pud_clear(pud);
  660. }
  661. static void unmap_pmd_range(pud_t *pud, unsigned long start, unsigned long end)
  662. {
  663. pmd_t *pmd = pmd_offset(pud, start);
  664. /*
  665. * Not on a 2MB page boundary?
  666. */
  667. if (start & (PMD_SIZE - 1)) {
  668. unsigned long next_page = (start + PMD_SIZE) & PMD_MASK;
  669. unsigned long pre_end = min_t(unsigned long, end, next_page);
  670. __unmap_pmd_range(pud, pmd, start, pre_end);
  671. start = pre_end;
  672. pmd++;
  673. }
  674. /*
  675. * Try to unmap in 2M chunks.
  676. */
  677. while (end - start >= PMD_SIZE) {
  678. if (pmd_large(*pmd))
  679. pmd_clear(pmd);
  680. else
  681. __unmap_pmd_range(pud, pmd, start, start + PMD_SIZE);
  682. start += PMD_SIZE;
  683. pmd++;
  684. }
  685. /*
  686. * 4K leftovers?
  687. */
  688. if (start < end)
  689. return __unmap_pmd_range(pud, pmd, start, end);
  690. /*
  691. * Try again to free the PMD page if haven't succeeded above.
  692. */
  693. if (!pud_none(*pud))
  694. if (try_to_free_pmd_page((pmd_t *)pud_page_vaddr(*pud)))
  695. pud_clear(pud);
  696. }
  697. static void unmap_pud_range(pgd_t *pgd, unsigned long start, unsigned long end)
  698. {
  699. pud_t *pud = pud_offset(pgd, start);
  700. /*
  701. * Not on a GB page boundary?
  702. */
  703. if (start & (PUD_SIZE - 1)) {
  704. unsigned long next_page = (start + PUD_SIZE) & PUD_MASK;
  705. unsigned long pre_end = min_t(unsigned long, end, next_page);
  706. unmap_pmd_range(pud, start, pre_end);
  707. start = pre_end;
  708. pud++;
  709. }
  710. /*
  711. * Try to unmap in 1G chunks?
  712. */
  713. while (end - start >= PUD_SIZE) {
  714. if (pud_large(*pud))
  715. pud_clear(pud);
  716. else
  717. unmap_pmd_range(pud, start, start + PUD_SIZE);
  718. start += PUD_SIZE;
  719. pud++;
  720. }
  721. /*
  722. * 2M leftovers?
  723. */
  724. if (start < end)
  725. unmap_pmd_range(pud, start, end);
  726. /*
  727. * No need to try to free the PUD page because we'll free it in
  728. * populate_pgd's error path
  729. */
  730. }
  731. static int alloc_pte_page(pmd_t *pmd)
  732. {
  733. pte_t *pte = (pte_t *)get_zeroed_page(GFP_KERNEL | __GFP_NOTRACK);
  734. if (!pte)
  735. return -1;
  736. set_pmd(pmd, __pmd(__pa(pte) | _KERNPG_TABLE));
  737. return 0;
  738. }
  739. static int alloc_pmd_page(pud_t *pud)
  740. {
  741. pmd_t *pmd = (pmd_t *)get_zeroed_page(GFP_KERNEL | __GFP_NOTRACK);
  742. if (!pmd)
  743. return -1;
  744. set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE));
  745. return 0;
  746. }
  747. static void populate_pte(struct cpa_data *cpa,
  748. unsigned long start, unsigned long end,
  749. unsigned num_pages, pmd_t *pmd, pgprot_t pgprot)
  750. {
  751. pte_t *pte;
  752. pte = pte_offset_kernel(pmd, start);
  753. /*
  754. * Set the GLOBAL flags only if the PRESENT flag is
  755. * set otherwise pte_present will return true even on
  756. * a non present pte. The canon_pgprot will clear
  757. * _PAGE_GLOBAL for the ancient hardware that doesn't
  758. * support it.
  759. */
  760. if (pgprot_val(pgprot) & _PAGE_PRESENT)
  761. pgprot_val(pgprot) |= _PAGE_GLOBAL;
  762. else
  763. pgprot_val(pgprot) &= ~_PAGE_GLOBAL;
  764. pgprot = canon_pgprot(pgprot);
  765. while (num_pages-- && start < end) {
  766. set_pte(pte, pfn_pte(cpa->pfn, pgprot));
  767. start += PAGE_SIZE;
  768. cpa->pfn++;
  769. pte++;
  770. }
  771. }
  772. static int populate_pmd(struct cpa_data *cpa,
  773. unsigned long start, unsigned long end,
  774. unsigned num_pages, pud_t *pud, pgprot_t pgprot)
  775. {
  776. unsigned int cur_pages = 0;
  777. pmd_t *pmd;
  778. pgprot_t pmd_pgprot;
  779. /*
  780. * Not on a 2M boundary?
  781. */
  782. if (start & (PMD_SIZE - 1)) {
  783. unsigned long pre_end = start + (num_pages << PAGE_SHIFT);
  784. unsigned long next_page = (start + PMD_SIZE) & PMD_MASK;
  785. pre_end = min_t(unsigned long, pre_end, next_page);
  786. cur_pages = (pre_end - start) >> PAGE_SHIFT;
  787. cur_pages = min_t(unsigned int, num_pages, cur_pages);
  788. /*
  789. * Need a PTE page?
  790. */
  791. pmd = pmd_offset(pud, start);
  792. if (pmd_none(*pmd))
  793. if (alloc_pte_page(pmd))
  794. return -1;
  795. populate_pte(cpa, start, pre_end, cur_pages, pmd, pgprot);
  796. start = pre_end;
  797. }
  798. /*
  799. * We mapped them all?
  800. */
  801. if (num_pages == cur_pages)
  802. return cur_pages;
  803. pmd_pgprot = pgprot_4k_2_large(pgprot);
  804. while (end - start >= PMD_SIZE) {
  805. /*
  806. * We cannot use a 1G page so allocate a PMD page if needed.
  807. */
  808. if (pud_none(*pud))
  809. if (alloc_pmd_page(pud))
  810. return -1;
  811. pmd = pmd_offset(pud, start);
  812. set_pmd(pmd, __pmd(cpa->pfn << PAGE_SHIFT | _PAGE_PSE |
  813. massage_pgprot(pmd_pgprot)));
  814. start += PMD_SIZE;
  815. cpa->pfn += PMD_SIZE >> PAGE_SHIFT;
  816. cur_pages += PMD_SIZE >> PAGE_SHIFT;
  817. }
  818. /*
  819. * Map trailing 4K pages.
  820. */
  821. if (start < end) {
  822. pmd = pmd_offset(pud, start);
  823. if (pmd_none(*pmd))
  824. if (alloc_pte_page(pmd))
  825. return -1;
  826. populate_pte(cpa, start, end, num_pages - cur_pages,
  827. pmd, pgprot);
  828. }
  829. return num_pages;
  830. }
  831. static int populate_pud(struct cpa_data *cpa, unsigned long start, pgd_t *pgd,
  832. pgprot_t pgprot)
  833. {
  834. pud_t *pud;
  835. unsigned long end;
  836. int cur_pages = 0;
  837. pgprot_t pud_pgprot;
  838. end = start + (cpa->numpages << PAGE_SHIFT);
  839. /*
  840. * Not on a Gb page boundary? => map everything up to it with
  841. * smaller pages.
  842. */
  843. if (start & (PUD_SIZE - 1)) {
  844. unsigned long pre_end;
  845. unsigned long next_page = (start + PUD_SIZE) & PUD_MASK;
  846. pre_end = min_t(unsigned long, end, next_page);
  847. cur_pages = (pre_end - start) >> PAGE_SHIFT;
  848. cur_pages = min_t(int, (int)cpa->numpages, cur_pages);
  849. pud = pud_offset(pgd, start);
  850. /*
  851. * Need a PMD page?
  852. */
  853. if (pud_none(*pud))
  854. if (alloc_pmd_page(pud))
  855. return -1;
  856. cur_pages = populate_pmd(cpa, start, pre_end, cur_pages,
  857. pud, pgprot);
  858. if (cur_pages < 0)
  859. return cur_pages;
  860. start = pre_end;
  861. }
  862. /* We mapped them all? */
  863. if (cpa->numpages == cur_pages)
  864. return cur_pages;
  865. pud = pud_offset(pgd, start);
  866. pud_pgprot = pgprot_4k_2_large(pgprot);
  867. /*
  868. * Map everything starting from the Gb boundary, possibly with 1G pages
  869. */
  870. while (boot_cpu_has(X86_FEATURE_GBPAGES) && end - start >= PUD_SIZE) {
  871. set_pud(pud, __pud(cpa->pfn << PAGE_SHIFT | _PAGE_PSE |
  872. massage_pgprot(pud_pgprot)));
  873. start += PUD_SIZE;
  874. cpa->pfn += PUD_SIZE >> PAGE_SHIFT;
  875. cur_pages += PUD_SIZE >> PAGE_SHIFT;
  876. pud++;
  877. }
  878. /* Map trailing leftover */
  879. if (start < end) {
  880. int tmp;
  881. pud = pud_offset(pgd, start);
  882. if (pud_none(*pud))
  883. if (alloc_pmd_page(pud))
  884. return -1;
  885. tmp = populate_pmd(cpa, start, end, cpa->numpages - cur_pages,
  886. pud, pgprot);
  887. if (tmp < 0)
  888. return cur_pages;
  889. cur_pages += tmp;
  890. }
  891. return cur_pages;
  892. }
  893. /*
  894. * Restrictions for kernel page table do not necessarily apply when mapping in
  895. * an alternate PGD.
  896. */
  897. static int populate_pgd(struct cpa_data *cpa, unsigned long addr)
  898. {
  899. pgprot_t pgprot = __pgprot(_KERNPG_TABLE);
  900. pud_t *pud = NULL; /* shut up gcc */
  901. pgd_t *pgd_entry;
  902. int ret;
  903. pgd_entry = cpa->pgd + pgd_index(addr);
  904. /*
  905. * Allocate a PUD page and hand it down for mapping.
  906. */
  907. if (pgd_none(*pgd_entry)) {
  908. pud = (pud_t *)get_zeroed_page(GFP_KERNEL | __GFP_NOTRACK);
  909. if (!pud)
  910. return -1;
  911. set_pgd(pgd_entry, __pgd(__pa(pud) | _KERNPG_TABLE));
  912. }
  913. pgprot_val(pgprot) &= ~pgprot_val(cpa->mask_clr);
  914. pgprot_val(pgprot) |= pgprot_val(cpa->mask_set);
  915. ret = populate_pud(cpa, addr, pgd_entry, pgprot);
  916. if (ret < 0) {
  917. /*
  918. * Leave the PUD page in place in case some other CPU or thread
  919. * already found it, but remove any useless entries we just
  920. * added to it.
  921. */
  922. unmap_pud_range(pgd_entry, addr,
  923. addr + (cpa->numpages << PAGE_SHIFT));
  924. return ret;
  925. }
  926. cpa->numpages = ret;
  927. return 0;
  928. }
  929. static int __cpa_process_fault(struct cpa_data *cpa, unsigned long vaddr,
  930. int primary)
  931. {
  932. if (cpa->pgd) {
  933. /*
  934. * Right now, we only execute this code path when mapping
  935. * the EFI virtual memory map regions, no other users
  936. * provide a ->pgd value. This may change in the future.
  937. */
  938. return populate_pgd(cpa, vaddr);
  939. }
  940. /*
  941. * Ignore all non primary paths.
  942. */
  943. if (!primary) {
  944. cpa->numpages = 1;
  945. return 0;
  946. }
  947. /*
  948. * Ignore the NULL PTE for kernel identity mapping, as it is expected
  949. * to have holes.
  950. * Also set numpages to '1' indicating that we processed cpa req for
  951. * one virtual address page and its pfn. TBD: numpages can be set based
  952. * on the initial value and the level returned by lookup_address().
  953. */
  954. if (within(vaddr, PAGE_OFFSET,
  955. PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT))) {
  956. cpa->numpages = 1;
  957. cpa->pfn = __pa(vaddr) >> PAGE_SHIFT;
  958. return 0;
  959. } else {
  960. WARN(1, KERN_WARNING "CPA: called for zero pte. "
  961. "vaddr = %lx cpa->vaddr = %lx\n", vaddr,
  962. *cpa->vaddr);
  963. return -EFAULT;
  964. }
  965. }
  966. static int __change_page_attr(struct cpa_data *cpa, int primary)
  967. {
  968. unsigned long address;
  969. int do_split, err;
  970. unsigned int level;
  971. pte_t *kpte, old_pte;
  972. if (cpa->flags & CPA_PAGES_ARRAY) {
  973. struct page *page = cpa->pages[cpa->curpage];
  974. if (unlikely(PageHighMem(page)))
  975. return 0;
  976. address = (unsigned long)page_address(page);
  977. } else if (cpa->flags & CPA_ARRAY)
  978. address = cpa->vaddr[cpa->curpage];
  979. else
  980. address = *cpa->vaddr;
  981. repeat:
  982. kpte = _lookup_address_cpa(cpa, address, &level);
  983. if (!kpte)
  984. return __cpa_process_fault(cpa, address, primary);
  985. old_pte = *kpte;
  986. if (pte_none(old_pte))
  987. return __cpa_process_fault(cpa, address, primary);
  988. if (level == PG_LEVEL_4K) {
  989. pte_t new_pte;
  990. pgprot_t new_prot = pte_pgprot(old_pte);
  991. unsigned long pfn = pte_pfn(old_pte);
  992. pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
  993. pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
  994. new_prot = static_protections(new_prot, address, pfn);
  995. /*
  996. * Set the GLOBAL flags only if the PRESENT flag is
  997. * set otherwise pte_present will return true even on
  998. * a non present pte. The canon_pgprot will clear
  999. * _PAGE_GLOBAL for the ancient hardware that doesn't
  1000. * support it.
  1001. */
  1002. if (pgprot_val(new_prot) & _PAGE_PRESENT)
  1003. pgprot_val(new_prot) |= _PAGE_GLOBAL;
  1004. else
  1005. pgprot_val(new_prot) &= ~_PAGE_GLOBAL;
  1006. /*
  1007. * We need to keep the pfn from the existing PTE,
  1008. * after all we're only going to change it's attributes
  1009. * not the memory it points to
  1010. */
  1011. new_pte = pfn_pte(pfn, canon_pgprot(new_prot));
  1012. cpa->pfn = pfn;
  1013. /*
  1014. * Do we really change anything ?
  1015. */
  1016. if (pte_val(old_pte) != pte_val(new_pte)) {
  1017. set_pte_atomic(kpte, new_pte);
  1018. cpa->flags |= CPA_FLUSHTLB;
  1019. }
  1020. cpa->numpages = 1;
  1021. return 0;
  1022. }
  1023. /*
  1024. * Check, whether we can keep the large page intact
  1025. * and just change the pte:
  1026. */
  1027. do_split = try_preserve_large_page(kpte, address, cpa);
  1028. /*
  1029. * When the range fits into the existing large page,
  1030. * return. cp->numpages and cpa->tlbflush have been updated in
  1031. * try_large_page:
  1032. */
  1033. if (do_split <= 0)
  1034. return do_split;
  1035. /*
  1036. * We have to split the large page:
  1037. */
  1038. err = split_large_page(cpa, kpte, address);
  1039. if (!err) {
  1040. /*
  1041. * Do a global flush tlb after splitting the large page
  1042. * and before we do the actual change page attribute in the PTE.
  1043. *
  1044. * With out this, we violate the TLB application note, that says
  1045. * "The TLBs may contain both ordinary and large-page
  1046. * translations for a 4-KByte range of linear addresses. This
  1047. * may occur if software modifies the paging structures so that
  1048. * the page size used for the address range changes. If the two
  1049. * translations differ with respect to page frame or attributes
  1050. * (e.g., permissions), processor behavior is undefined and may
  1051. * be implementation-specific."
  1052. *
  1053. * We do this global tlb flush inside the cpa_lock, so that we
  1054. * don't allow any other cpu, with stale tlb entries change the
  1055. * page attribute in parallel, that also falls into the
  1056. * just split large page entry.
  1057. */
  1058. flush_tlb_all();
  1059. goto repeat;
  1060. }
  1061. return err;
  1062. }
  1063. static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias);
  1064. static int cpa_process_alias(struct cpa_data *cpa)
  1065. {
  1066. struct cpa_data alias_cpa;
  1067. unsigned long laddr = (unsigned long)__va(cpa->pfn << PAGE_SHIFT);
  1068. unsigned long vaddr;
  1069. int ret;
  1070. if (!pfn_range_is_mapped(cpa->pfn, cpa->pfn + 1))
  1071. return 0;
  1072. /*
  1073. * No need to redo, when the primary call touched the direct
  1074. * mapping already:
  1075. */
  1076. if (cpa->flags & CPA_PAGES_ARRAY) {
  1077. struct page *page = cpa->pages[cpa->curpage];
  1078. if (unlikely(PageHighMem(page)))
  1079. return 0;
  1080. vaddr = (unsigned long)page_address(page);
  1081. } else if (cpa->flags & CPA_ARRAY)
  1082. vaddr = cpa->vaddr[cpa->curpage];
  1083. else
  1084. vaddr = *cpa->vaddr;
  1085. if (!(within(vaddr, PAGE_OFFSET,
  1086. PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT)))) {
  1087. alias_cpa = *cpa;
  1088. alias_cpa.vaddr = &laddr;
  1089. alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
  1090. ret = __change_page_attr_set_clr(&alias_cpa, 0);
  1091. if (ret)
  1092. return ret;
  1093. }
  1094. #ifdef CONFIG_X86_64
  1095. /*
  1096. * If the primary call didn't touch the high mapping already
  1097. * and the physical address is inside the kernel map, we need
  1098. * to touch the high mapped kernel as well:
  1099. */
  1100. if (!within(vaddr, (unsigned long)_text, _brk_end) &&
  1101. within_inclusive(cpa->pfn, highmap_start_pfn(),
  1102. highmap_end_pfn())) {
  1103. unsigned long temp_cpa_vaddr = (cpa->pfn << PAGE_SHIFT) +
  1104. __START_KERNEL_map - phys_base;
  1105. alias_cpa = *cpa;
  1106. alias_cpa.vaddr = &temp_cpa_vaddr;
  1107. alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
  1108. /*
  1109. * The high mapping range is imprecise, so ignore the
  1110. * return value.
  1111. */
  1112. __change_page_attr_set_clr(&alias_cpa, 0);
  1113. }
  1114. #endif
  1115. return 0;
  1116. }
  1117. static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias)
  1118. {
  1119. int ret, numpages = cpa->numpages;
  1120. while (numpages) {
  1121. /*
  1122. * Store the remaining nr of pages for the large page
  1123. * preservation check.
  1124. */
  1125. cpa->numpages = numpages;
  1126. /* for array changes, we can't use large page */
  1127. if (cpa->flags & (CPA_ARRAY | CPA_PAGES_ARRAY))
  1128. cpa->numpages = 1;
  1129. if (!debug_pagealloc_enabled())
  1130. spin_lock(&cpa_lock);
  1131. ret = __change_page_attr(cpa, checkalias);
  1132. if (!debug_pagealloc_enabled())
  1133. spin_unlock(&cpa_lock);
  1134. if (ret)
  1135. return ret;
  1136. if (checkalias) {
  1137. ret = cpa_process_alias(cpa);
  1138. if (ret)
  1139. return ret;
  1140. }
  1141. /*
  1142. * Adjust the number of pages with the result of the
  1143. * CPA operation. Either a large page has been
  1144. * preserved or a single page update happened.
  1145. */
  1146. BUG_ON(cpa->numpages > numpages || !cpa->numpages);
  1147. numpages -= cpa->numpages;
  1148. if (cpa->flags & (CPA_PAGES_ARRAY | CPA_ARRAY))
  1149. cpa->curpage++;
  1150. else
  1151. *cpa->vaddr += cpa->numpages * PAGE_SIZE;
  1152. }
  1153. return 0;
  1154. }
  1155. static int change_page_attr_set_clr(unsigned long *addr, int numpages,
  1156. pgprot_t mask_set, pgprot_t mask_clr,
  1157. int force_split, int in_flag,
  1158. struct page **pages)
  1159. {
  1160. struct cpa_data cpa;
  1161. int ret, cache, checkalias;
  1162. unsigned long baddr = 0;
  1163. memset(&cpa, 0, sizeof(cpa));
  1164. /*
  1165. * Check, if we are requested to change a not supported
  1166. * feature:
  1167. */
  1168. mask_set = canon_pgprot(mask_set);
  1169. mask_clr = canon_pgprot(mask_clr);
  1170. if (!pgprot_val(mask_set) && !pgprot_val(mask_clr) && !force_split)
  1171. return 0;
  1172. /* Ensure we are PAGE_SIZE aligned */
  1173. if (in_flag & CPA_ARRAY) {
  1174. int i;
  1175. for (i = 0; i < numpages; i++) {
  1176. if (addr[i] & ~PAGE_MASK) {
  1177. addr[i] &= PAGE_MASK;
  1178. WARN_ON_ONCE(1);
  1179. }
  1180. }
  1181. } else if (!(in_flag & CPA_PAGES_ARRAY)) {
  1182. /*
  1183. * in_flag of CPA_PAGES_ARRAY implies it is aligned.
  1184. * No need to cehck in that case
  1185. */
  1186. if (*addr & ~PAGE_MASK) {
  1187. *addr &= PAGE_MASK;
  1188. /*
  1189. * People should not be passing in unaligned addresses:
  1190. */
  1191. WARN_ON_ONCE(1);
  1192. }
  1193. /*
  1194. * Save address for cache flush. *addr is modified in the call
  1195. * to __change_page_attr_set_clr() below.
  1196. */
  1197. baddr = *addr;
  1198. }
  1199. /* Must avoid aliasing mappings in the highmem code */
  1200. kmap_flush_unused();
  1201. vm_unmap_aliases();
  1202. cpa.vaddr = addr;
  1203. cpa.pages = pages;
  1204. cpa.numpages = numpages;
  1205. cpa.mask_set = mask_set;
  1206. cpa.mask_clr = mask_clr;
  1207. cpa.flags = 0;
  1208. cpa.curpage = 0;
  1209. cpa.force_split = force_split;
  1210. if (in_flag & (CPA_ARRAY | CPA_PAGES_ARRAY))
  1211. cpa.flags |= in_flag;
  1212. /* No alias checking for _NX bit modifications */
  1213. checkalias = (pgprot_val(mask_set) | pgprot_val(mask_clr)) != _PAGE_NX;
  1214. ret = __change_page_attr_set_clr(&cpa, checkalias);
  1215. /*
  1216. * Check whether we really changed something:
  1217. */
  1218. if (!(cpa.flags & CPA_FLUSHTLB))
  1219. goto out;
  1220. /*
  1221. * No need to flush, when we did not set any of the caching
  1222. * attributes:
  1223. */
  1224. cache = !!pgprot2cachemode(mask_set);
  1225. /*
  1226. * On success we use CLFLUSH, when the CPU supports it to
  1227. * avoid the WBINVD. If the CPU does not support it and in the
  1228. * error case we fall back to cpa_flush_all (which uses
  1229. * WBINVD):
  1230. */
  1231. if (!ret && boot_cpu_has(X86_FEATURE_CLFLUSH)) {
  1232. if (cpa.flags & (CPA_PAGES_ARRAY | CPA_ARRAY)) {
  1233. cpa_flush_array(addr, numpages, cache,
  1234. cpa.flags, pages);
  1235. } else
  1236. cpa_flush_range(baddr, numpages, cache);
  1237. } else
  1238. cpa_flush_all(cache);
  1239. out:
  1240. return ret;
  1241. }
  1242. static inline int change_page_attr_set(unsigned long *addr, int numpages,
  1243. pgprot_t mask, int array)
  1244. {
  1245. return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0), 0,
  1246. (array ? CPA_ARRAY : 0), NULL);
  1247. }
  1248. static inline int change_page_attr_clear(unsigned long *addr, int numpages,
  1249. pgprot_t mask, int array)
  1250. {
  1251. return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask, 0,
  1252. (array ? CPA_ARRAY : 0), NULL);
  1253. }
  1254. static inline int cpa_set_pages_array(struct page **pages, int numpages,
  1255. pgprot_t mask)
  1256. {
  1257. return change_page_attr_set_clr(NULL, numpages, mask, __pgprot(0), 0,
  1258. CPA_PAGES_ARRAY, pages);
  1259. }
  1260. static inline int cpa_clear_pages_array(struct page **pages, int numpages,
  1261. pgprot_t mask)
  1262. {
  1263. return change_page_attr_set_clr(NULL, numpages, __pgprot(0), mask, 0,
  1264. CPA_PAGES_ARRAY, pages);
  1265. }
  1266. int _set_memory_uc(unsigned long addr, int numpages)
  1267. {
  1268. /*
  1269. * for now UC MINUS. see comments in ioremap_nocache()
  1270. * If you really need strong UC use ioremap_uc(), but note
  1271. * that you cannot override IO areas with set_memory_*() as
  1272. * these helpers cannot work with IO memory.
  1273. */
  1274. return change_page_attr_set(&addr, numpages,
  1275. cachemode2pgprot(_PAGE_CACHE_MODE_UC_MINUS),
  1276. 0);
  1277. }
  1278. int set_memory_uc(unsigned long addr, int numpages)
  1279. {
  1280. int ret;
  1281. /*
  1282. * for now UC MINUS. see comments in ioremap_nocache()
  1283. */
  1284. ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
  1285. _PAGE_CACHE_MODE_UC_MINUS, NULL);
  1286. if (ret)
  1287. goto out_err;
  1288. ret = _set_memory_uc(addr, numpages);
  1289. if (ret)
  1290. goto out_free;
  1291. return 0;
  1292. out_free:
  1293. free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
  1294. out_err:
  1295. return ret;
  1296. }
  1297. EXPORT_SYMBOL(set_memory_uc);
  1298. static int _set_memory_array(unsigned long *addr, int addrinarray,
  1299. enum page_cache_mode new_type)
  1300. {
  1301. enum page_cache_mode set_type;
  1302. int i, j;
  1303. int ret;
  1304. for (i = 0; i < addrinarray; i++) {
  1305. ret = reserve_memtype(__pa(addr[i]), __pa(addr[i]) + PAGE_SIZE,
  1306. new_type, NULL);
  1307. if (ret)
  1308. goto out_free;
  1309. }
  1310. /* If WC, set to UC- first and then WC */
  1311. set_type = (new_type == _PAGE_CACHE_MODE_WC) ?
  1312. _PAGE_CACHE_MODE_UC_MINUS : new_type;
  1313. ret = change_page_attr_set(addr, addrinarray,
  1314. cachemode2pgprot(set_type), 1);
  1315. if (!ret && new_type == _PAGE_CACHE_MODE_WC)
  1316. ret = change_page_attr_set_clr(addr, addrinarray,
  1317. cachemode2pgprot(
  1318. _PAGE_CACHE_MODE_WC),
  1319. __pgprot(_PAGE_CACHE_MASK),
  1320. 0, CPA_ARRAY, NULL);
  1321. if (ret)
  1322. goto out_free;
  1323. return 0;
  1324. out_free:
  1325. for (j = 0; j < i; j++)
  1326. free_memtype(__pa(addr[j]), __pa(addr[j]) + PAGE_SIZE);
  1327. return ret;
  1328. }
  1329. int set_memory_array_uc(unsigned long *addr, int addrinarray)
  1330. {
  1331. return _set_memory_array(addr, addrinarray, _PAGE_CACHE_MODE_UC_MINUS);
  1332. }
  1333. EXPORT_SYMBOL(set_memory_array_uc);
  1334. int set_memory_array_wc(unsigned long *addr, int addrinarray)
  1335. {
  1336. return _set_memory_array(addr, addrinarray, _PAGE_CACHE_MODE_WC);
  1337. }
  1338. EXPORT_SYMBOL(set_memory_array_wc);
  1339. int set_memory_array_wt(unsigned long *addr, int addrinarray)
  1340. {
  1341. return _set_memory_array(addr, addrinarray, _PAGE_CACHE_MODE_WT);
  1342. }
  1343. EXPORT_SYMBOL_GPL(set_memory_array_wt);
  1344. int _set_memory_wc(unsigned long addr, int numpages)
  1345. {
  1346. int ret;
  1347. unsigned long addr_copy = addr;
  1348. ret = change_page_attr_set(&addr, numpages,
  1349. cachemode2pgprot(_PAGE_CACHE_MODE_UC_MINUS),
  1350. 0);
  1351. if (!ret) {
  1352. ret = change_page_attr_set_clr(&addr_copy, numpages,
  1353. cachemode2pgprot(
  1354. _PAGE_CACHE_MODE_WC),
  1355. __pgprot(_PAGE_CACHE_MASK),
  1356. 0, 0, NULL);
  1357. }
  1358. return ret;
  1359. }
  1360. int set_memory_wc(unsigned long addr, int numpages)
  1361. {
  1362. int ret;
  1363. ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
  1364. _PAGE_CACHE_MODE_WC, NULL);
  1365. if (ret)
  1366. return ret;
  1367. ret = _set_memory_wc(addr, numpages);
  1368. if (ret)
  1369. free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
  1370. return ret;
  1371. }
  1372. EXPORT_SYMBOL(set_memory_wc);
  1373. int _set_memory_wt(unsigned long addr, int numpages)
  1374. {
  1375. return change_page_attr_set(&addr, numpages,
  1376. cachemode2pgprot(_PAGE_CACHE_MODE_WT), 0);
  1377. }
  1378. int set_memory_wt(unsigned long addr, int numpages)
  1379. {
  1380. int ret;
  1381. ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
  1382. _PAGE_CACHE_MODE_WT, NULL);
  1383. if (ret)
  1384. return ret;
  1385. ret = _set_memory_wt(addr, numpages);
  1386. if (ret)
  1387. free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
  1388. return ret;
  1389. }
  1390. EXPORT_SYMBOL_GPL(set_memory_wt);
  1391. int _set_memory_wb(unsigned long addr, int numpages)
  1392. {
  1393. /* WB cache mode is hard wired to all cache attribute bits being 0 */
  1394. return change_page_attr_clear(&addr, numpages,
  1395. __pgprot(_PAGE_CACHE_MASK), 0);
  1396. }
  1397. int set_memory_wb(unsigned long addr, int numpages)
  1398. {
  1399. int ret;
  1400. ret = _set_memory_wb(addr, numpages);
  1401. if (ret)
  1402. return ret;
  1403. free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
  1404. return 0;
  1405. }
  1406. EXPORT_SYMBOL(set_memory_wb);
  1407. int set_memory_array_wb(unsigned long *addr, int addrinarray)
  1408. {
  1409. int i;
  1410. int ret;
  1411. /* WB cache mode is hard wired to all cache attribute bits being 0 */
  1412. ret = change_page_attr_clear(addr, addrinarray,
  1413. __pgprot(_PAGE_CACHE_MASK), 1);
  1414. if (ret)
  1415. return ret;
  1416. for (i = 0; i < addrinarray; i++)
  1417. free_memtype(__pa(addr[i]), __pa(addr[i]) + PAGE_SIZE);
  1418. return 0;
  1419. }
  1420. EXPORT_SYMBOL(set_memory_array_wb);
  1421. int set_memory_x(unsigned long addr, int numpages)
  1422. {
  1423. if (!(__supported_pte_mask & _PAGE_NX))
  1424. return 0;
  1425. return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_NX), 0);
  1426. }
  1427. EXPORT_SYMBOL(set_memory_x);
  1428. int set_memory_nx(unsigned long addr, int numpages)
  1429. {
  1430. if (!(__supported_pte_mask & _PAGE_NX))
  1431. return 0;
  1432. return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_NX), 0);
  1433. }
  1434. EXPORT_SYMBOL(set_memory_nx);
  1435. int set_memory_ro(unsigned long addr, int numpages)
  1436. {
  1437. return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_RW), 0);
  1438. }
  1439. int set_memory_rw(unsigned long addr, int numpages)
  1440. {
  1441. return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_RW), 0);
  1442. }
  1443. int set_memory_np(unsigned long addr, int numpages)
  1444. {
  1445. return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_PRESENT), 0);
  1446. }
  1447. int set_memory_4k(unsigned long addr, int numpages)
  1448. {
  1449. return change_page_attr_set_clr(&addr, numpages, __pgprot(0),
  1450. __pgprot(0), 1, 0, NULL);
  1451. }
  1452. int set_pages_uc(struct page *page, int numpages)
  1453. {
  1454. unsigned long addr = (unsigned long)page_address(page);
  1455. return set_memory_uc(addr, numpages);
  1456. }
  1457. EXPORT_SYMBOL(set_pages_uc);
  1458. static int _set_pages_array(struct page **pages, int addrinarray,
  1459. enum page_cache_mode new_type)
  1460. {
  1461. unsigned long start;
  1462. unsigned long end;
  1463. enum page_cache_mode set_type;
  1464. int i;
  1465. int free_idx;
  1466. int ret;
  1467. for (i = 0; i < addrinarray; i++) {
  1468. if (PageHighMem(pages[i]))
  1469. continue;
  1470. start = page_to_pfn(pages[i]) << PAGE_SHIFT;
  1471. end = start + PAGE_SIZE;
  1472. if (reserve_memtype(start, end, new_type, NULL))
  1473. goto err_out;
  1474. }
  1475. /* If WC, set to UC- first and then WC */
  1476. set_type = (new_type == _PAGE_CACHE_MODE_WC) ?
  1477. _PAGE_CACHE_MODE_UC_MINUS : new_type;
  1478. ret = cpa_set_pages_array(pages, addrinarray,
  1479. cachemode2pgprot(set_type));
  1480. if (!ret && new_type == _PAGE_CACHE_MODE_WC)
  1481. ret = change_page_attr_set_clr(NULL, addrinarray,
  1482. cachemode2pgprot(
  1483. _PAGE_CACHE_MODE_WC),
  1484. __pgprot(_PAGE_CACHE_MASK),
  1485. 0, CPA_PAGES_ARRAY, pages);
  1486. if (ret)
  1487. goto err_out;
  1488. return 0; /* Success */
  1489. err_out:
  1490. free_idx = i;
  1491. for (i = 0; i < free_idx; i++) {
  1492. if (PageHighMem(pages[i]))
  1493. continue;
  1494. start = page_to_pfn(pages[i]) << PAGE_SHIFT;
  1495. end = start + PAGE_SIZE;
  1496. free_memtype(start, end);
  1497. }
  1498. return -EINVAL;
  1499. }
  1500. int set_pages_array_uc(struct page **pages, int addrinarray)
  1501. {
  1502. return _set_pages_array(pages, addrinarray, _PAGE_CACHE_MODE_UC_MINUS);
  1503. }
  1504. EXPORT_SYMBOL(set_pages_array_uc);
  1505. int set_pages_array_wc(struct page **pages, int addrinarray)
  1506. {
  1507. return _set_pages_array(pages, addrinarray, _PAGE_CACHE_MODE_WC);
  1508. }
  1509. EXPORT_SYMBOL(set_pages_array_wc);
  1510. int set_pages_array_wt(struct page **pages, int addrinarray)
  1511. {
  1512. return _set_pages_array(pages, addrinarray, _PAGE_CACHE_MODE_WT);
  1513. }
  1514. EXPORT_SYMBOL_GPL(set_pages_array_wt);
  1515. int set_pages_wb(struct page *page, int numpages)
  1516. {
  1517. unsigned long addr = (unsigned long)page_address(page);
  1518. return set_memory_wb(addr, numpages);
  1519. }
  1520. EXPORT_SYMBOL(set_pages_wb);
  1521. int set_pages_array_wb(struct page **pages, int addrinarray)
  1522. {
  1523. int retval;
  1524. unsigned long start;
  1525. unsigned long end;
  1526. int i;
  1527. /* WB cache mode is hard wired to all cache attribute bits being 0 */
  1528. retval = cpa_clear_pages_array(pages, addrinarray,
  1529. __pgprot(_PAGE_CACHE_MASK));
  1530. if (retval)
  1531. return retval;
  1532. for (i = 0; i < addrinarray; i++) {
  1533. if (PageHighMem(pages[i]))
  1534. continue;
  1535. start = page_to_pfn(pages[i]) << PAGE_SHIFT;
  1536. end = start + PAGE_SIZE;
  1537. free_memtype(start, end);
  1538. }
  1539. return 0;
  1540. }
  1541. EXPORT_SYMBOL(set_pages_array_wb);
  1542. int set_pages_x(struct page *page, int numpages)
  1543. {
  1544. unsigned long addr = (unsigned long)page_address(page);
  1545. return set_memory_x(addr, numpages);
  1546. }
  1547. EXPORT_SYMBOL(set_pages_x);
  1548. int set_pages_nx(struct page *page, int numpages)
  1549. {
  1550. unsigned long addr = (unsigned long)page_address(page);
  1551. return set_memory_nx(addr, numpages);
  1552. }
  1553. EXPORT_SYMBOL(set_pages_nx);
  1554. int set_pages_ro(struct page *page, int numpages)
  1555. {
  1556. unsigned long addr = (unsigned long)page_address(page);
  1557. return set_memory_ro(addr, numpages);
  1558. }
  1559. int set_pages_rw(struct page *page, int numpages)
  1560. {
  1561. unsigned long addr = (unsigned long)page_address(page);
  1562. return set_memory_rw(addr, numpages);
  1563. }
  1564. #ifdef CONFIG_DEBUG_PAGEALLOC
  1565. static int __set_pages_p(struct page *page, int numpages)
  1566. {
  1567. unsigned long tempaddr = (unsigned long) page_address(page);
  1568. struct cpa_data cpa = { .vaddr = &tempaddr,
  1569. .pgd = NULL,
  1570. .numpages = numpages,
  1571. .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW),
  1572. .mask_clr = __pgprot(0),
  1573. .flags = 0};
  1574. /*
  1575. * No alias checking needed for setting present flag. otherwise,
  1576. * we may need to break large pages for 64-bit kernel text
  1577. * mappings (this adds to complexity if we want to do this from
  1578. * atomic context especially). Let's keep it simple!
  1579. */
  1580. return __change_page_attr_set_clr(&cpa, 0);
  1581. }
  1582. static int __set_pages_np(struct page *page, int numpages)
  1583. {
  1584. unsigned long tempaddr = (unsigned long) page_address(page);
  1585. struct cpa_data cpa = { .vaddr = &tempaddr,
  1586. .pgd = NULL,
  1587. .numpages = numpages,
  1588. .mask_set = __pgprot(0),
  1589. .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW),
  1590. .flags = 0};
  1591. /*
  1592. * No alias checking needed for setting not present flag. otherwise,
  1593. * we may need to break large pages for 64-bit kernel text
  1594. * mappings (this adds to complexity if we want to do this from
  1595. * atomic context especially). Let's keep it simple!
  1596. */
  1597. return __change_page_attr_set_clr(&cpa, 0);
  1598. }
  1599. void __kernel_map_pages(struct page *page, int numpages, int enable)
  1600. {
  1601. if (PageHighMem(page))
  1602. return;
  1603. if (!enable) {
  1604. debug_check_no_locks_freed(page_address(page),
  1605. numpages * PAGE_SIZE);
  1606. }
  1607. /*
  1608. * The return value is ignored as the calls cannot fail.
  1609. * Large pages for identity mappings are not used at boot time
  1610. * and hence no memory allocations during large page split.
  1611. */
  1612. if (enable)
  1613. __set_pages_p(page, numpages);
  1614. else
  1615. __set_pages_np(page, numpages);
  1616. /*
  1617. * We should perform an IPI and flush all tlbs,
  1618. * but that can deadlock->flush only current cpu:
  1619. */
  1620. __flush_tlb_all();
  1621. arch_flush_lazy_mmu_mode();
  1622. }
  1623. #ifdef CONFIG_HIBERNATION
  1624. bool kernel_page_present(struct page *page)
  1625. {
  1626. unsigned int level;
  1627. pte_t *pte;
  1628. if (PageHighMem(page))
  1629. return false;
  1630. pte = lookup_address((unsigned long)page_address(page), &level);
  1631. return (pte_val(*pte) & _PAGE_PRESENT);
  1632. }
  1633. #endif /* CONFIG_HIBERNATION */
  1634. #endif /* CONFIG_DEBUG_PAGEALLOC */
  1635. int kernel_map_pages_in_pgd(pgd_t *pgd, u64 pfn, unsigned long address,
  1636. unsigned numpages, unsigned long page_flags)
  1637. {
  1638. int retval = -EINVAL;
  1639. struct cpa_data cpa = {
  1640. .vaddr = &address,
  1641. .pfn = pfn,
  1642. .pgd = pgd,
  1643. .numpages = numpages,
  1644. .mask_set = __pgprot(0),
  1645. .mask_clr = __pgprot(0),
  1646. .flags = 0,
  1647. };
  1648. if (!(__supported_pte_mask & _PAGE_NX))
  1649. goto out;
  1650. if (!(page_flags & _PAGE_NX))
  1651. cpa.mask_clr = __pgprot(_PAGE_NX);
  1652. if (!(page_flags & _PAGE_RW))
  1653. cpa.mask_clr = __pgprot(_PAGE_RW);
  1654. cpa.mask_set = __pgprot(_PAGE_PRESENT | page_flags);
  1655. retval = __change_page_attr_set_clr(&cpa, 0);
  1656. __flush_tlb_all();
  1657. out:
  1658. return retval;
  1659. }
  1660. /*
  1661. * The testcases use internal knowledge of the implementation that shouldn't
  1662. * be exposed to the rest of the kernel. Include these directly here.
  1663. */
  1664. #ifdef CONFIG_CPA_DEBUG
  1665. #include "pageattr-test.c"
  1666. #endif