hugetlbpage.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. /*
  2. * PPC64 (POWER4) Huge TLB Page Support for Kernel.
  3. *
  4. * Copyright (C) 2003 David Gibson, IBM Corporation.
  5. *
  6. * Based on the IA-32 version:
  7. * Copyright (C) 2002, Rohit Seth <rohit.seth@intel.com>
  8. */
  9. #include <linux/init.h>
  10. #include <linux/fs.h>
  11. #include <linux/mm.h>
  12. #include <linux/hugetlb.h>
  13. #include <linux/pagemap.h>
  14. #include <linux/smp_lock.h>
  15. #include <linux/slab.h>
  16. #include <linux/err.h>
  17. #include <linux/sysctl.h>
  18. #include <asm/mman.h>
  19. #include <asm/pgalloc.h>
  20. #include <asm/tlb.h>
  21. #include <asm/tlbflush.h>
  22. #include <asm/mmu_context.h>
  23. #include <asm/machdep.h>
  24. #include <asm/cputable.h>
  25. #include <asm/tlb.h>
  26. #include <linux/sysctl.h>
  27. #define NUM_LOW_AREAS (0x100000000UL >> SID_SHIFT)
  28. #define NUM_HIGH_AREAS (PGTABLE_RANGE >> HTLB_AREA_SHIFT)
  29. /* Modelled after find_linux_pte() */
  30. pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
  31. {
  32. pgd_t *pg;
  33. pud_t *pu;
  34. pmd_t *pm;
  35. pte_t *pt;
  36. BUG_ON(! in_hugepage_area(mm->context, addr));
  37. addr &= HPAGE_MASK;
  38. pg = pgd_offset(mm, addr);
  39. if (!pgd_none(*pg)) {
  40. pu = pud_offset(pg, addr);
  41. if (!pud_none(*pu)) {
  42. pm = pmd_offset(pu, addr);
  43. #ifdef CONFIG_PPC_64K_PAGES
  44. /* Currently, we use the normal PTE offset within full
  45. * size PTE pages, thus our huge PTEs are scattered in
  46. * the PTE page and we do waste some. We may change
  47. * that in the future, but the current mecanism keeps
  48. * things much simpler
  49. */
  50. if (!pmd_none(*pm)) {
  51. /* Note: pte_offset_* are all equivalent on
  52. * ppc64 as we don't have HIGHMEM
  53. */
  54. pt = pte_offset_kernel(pm, addr);
  55. return pt;
  56. }
  57. #else /* CONFIG_PPC_64K_PAGES */
  58. /* On 4k pages, we put huge PTEs in the PMD page */
  59. pt = (pte_t *)pm;
  60. return pt;
  61. #endif /* CONFIG_PPC_64K_PAGES */
  62. }
  63. }
  64. return NULL;
  65. }
  66. pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr)
  67. {
  68. pgd_t *pg;
  69. pud_t *pu;
  70. pmd_t *pm;
  71. pte_t *pt;
  72. BUG_ON(! in_hugepage_area(mm->context, addr));
  73. addr &= HPAGE_MASK;
  74. pg = pgd_offset(mm, addr);
  75. pu = pud_alloc(mm, pg, addr);
  76. if (pu) {
  77. pm = pmd_alloc(mm, pu, addr);
  78. if (pm) {
  79. #ifdef CONFIG_PPC_64K_PAGES
  80. /* See comment in huge_pte_offset. Note that if we ever
  81. * want to put the page size in the PMD, we would have
  82. * to open code our own pte_alloc* function in order
  83. * to populate and set the size atomically
  84. */
  85. pt = pte_alloc_map(mm, pm, addr);
  86. #else /* CONFIG_PPC_64K_PAGES */
  87. pt = (pte_t *)pm;
  88. #endif /* CONFIG_PPC_64K_PAGES */
  89. return pt;
  90. }
  91. }
  92. return NULL;
  93. }
  94. void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
  95. pte_t *ptep, pte_t pte)
  96. {
  97. if (pte_present(*ptep)) {
  98. /* We open-code pte_clear because we need to pass the right
  99. * argument to hpte_update (huge / !huge)
  100. */
  101. unsigned long old = pte_update(ptep, ~0UL);
  102. if (old & _PAGE_HASHPTE)
  103. hpte_update(mm, addr & HPAGE_MASK, ptep, old, 1);
  104. flush_tlb_pending();
  105. }
  106. *ptep = __pte(pte_val(pte) & ~_PAGE_HPTEFLAGS);
  107. }
  108. pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr,
  109. pte_t *ptep)
  110. {
  111. unsigned long old = pte_update(ptep, ~0UL);
  112. if (old & _PAGE_HASHPTE)
  113. hpte_update(mm, addr & HPAGE_MASK, ptep, old, 1);
  114. *ptep = __pte(0);
  115. return __pte(old);
  116. }
  117. /*
  118. * This function checks for proper alignment of input addr and len parameters.
  119. */
  120. int is_aligned_hugepage_range(unsigned long addr, unsigned long len)
  121. {
  122. if (len & ~HPAGE_MASK)
  123. return -EINVAL;
  124. if (addr & ~HPAGE_MASK)
  125. return -EINVAL;
  126. if (! (within_hugepage_low_range(addr, len)
  127. || within_hugepage_high_range(addr, len)) )
  128. return -EINVAL;
  129. return 0;
  130. }
  131. static void flush_low_segments(void *parm)
  132. {
  133. u16 areas = (unsigned long) parm;
  134. unsigned long i;
  135. asm volatile("isync" : : : "memory");
  136. BUILD_BUG_ON((sizeof(areas)*8) != NUM_LOW_AREAS);
  137. for (i = 0; i < NUM_LOW_AREAS; i++) {
  138. if (! (areas & (1U << i)))
  139. continue;
  140. asm volatile("slbie %0"
  141. : : "r" ((i << SID_SHIFT) | SLBIE_C));
  142. }
  143. asm volatile("isync" : : : "memory");
  144. }
  145. static void flush_high_segments(void *parm)
  146. {
  147. u16 areas = (unsigned long) parm;
  148. unsigned long i, j;
  149. asm volatile("isync" : : : "memory");
  150. BUILD_BUG_ON((sizeof(areas)*8) != NUM_HIGH_AREAS);
  151. for (i = 0; i < NUM_HIGH_AREAS; i++) {
  152. if (! (areas & (1U << i)))
  153. continue;
  154. for (j = 0; j < (1UL << (HTLB_AREA_SHIFT-SID_SHIFT)); j++)
  155. asm volatile("slbie %0"
  156. :: "r" (((i << HTLB_AREA_SHIFT)
  157. + (j << SID_SHIFT)) | SLBIE_C));
  158. }
  159. asm volatile("isync" : : : "memory");
  160. }
  161. static int prepare_low_area_for_htlb(struct mm_struct *mm, unsigned long area)
  162. {
  163. unsigned long start = area << SID_SHIFT;
  164. unsigned long end = (area+1) << SID_SHIFT;
  165. struct vm_area_struct *vma;
  166. BUG_ON(area >= NUM_LOW_AREAS);
  167. /* Check no VMAs are in the region */
  168. vma = find_vma(mm, start);
  169. if (vma && (vma->vm_start < end))
  170. return -EBUSY;
  171. return 0;
  172. }
  173. static int prepare_high_area_for_htlb(struct mm_struct *mm, unsigned long area)
  174. {
  175. unsigned long start = area << HTLB_AREA_SHIFT;
  176. unsigned long end = (area+1) << HTLB_AREA_SHIFT;
  177. struct vm_area_struct *vma;
  178. BUG_ON(area >= NUM_HIGH_AREAS);
  179. /* Check no VMAs are in the region */
  180. vma = find_vma(mm, start);
  181. if (vma && (vma->vm_start < end))
  182. return -EBUSY;
  183. return 0;
  184. }
  185. static int open_low_hpage_areas(struct mm_struct *mm, u16 newareas)
  186. {
  187. unsigned long i;
  188. BUILD_BUG_ON((sizeof(newareas)*8) != NUM_LOW_AREAS);
  189. BUILD_BUG_ON((sizeof(mm->context.low_htlb_areas)*8) != NUM_LOW_AREAS);
  190. newareas &= ~(mm->context.low_htlb_areas);
  191. if (! newareas)
  192. return 0; /* The segments we want are already open */
  193. for (i = 0; i < NUM_LOW_AREAS; i++)
  194. if ((1 << i) & newareas)
  195. if (prepare_low_area_for_htlb(mm, i) != 0)
  196. return -EBUSY;
  197. mm->context.low_htlb_areas |= newareas;
  198. /* update the paca copy of the context struct */
  199. get_paca()->context = mm->context;
  200. /* the context change must make it to memory before the flush,
  201. * so that further SLB misses do the right thing. */
  202. mb();
  203. on_each_cpu(flush_low_segments, (void *)(unsigned long)newareas, 0, 1);
  204. return 0;
  205. }
  206. static int open_high_hpage_areas(struct mm_struct *mm, u16 newareas)
  207. {
  208. unsigned long i;
  209. BUILD_BUG_ON((sizeof(newareas)*8) != NUM_HIGH_AREAS);
  210. BUILD_BUG_ON((sizeof(mm->context.high_htlb_areas)*8)
  211. != NUM_HIGH_AREAS);
  212. newareas &= ~(mm->context.high_htlb_areas);
  213. if (! newareas)
  214. return 0; /* The areas we want are already open */
  215. for (i = 0; i < NUM_HIGH_AREAS; i++)
  216. if ((1 << i) & newareas)
  217. if (prepare_high_area_for_htlb(mm, i) != 0)
  218. return -EBUSY;
  219. mm->context.high_htlb_areas |= newareas;
  220. /* update the paca copy of the context struct */
  221. get_paca()->context = mm->context;
  222. /* the context change must make it to memory before the flush,
  223. * so that further SLB misses do the right thing. */
  224. mb();
  225. on_each_cpu(flush_high_segments, (void *)(unsigned long)newareas, 0, 1);
  226. return 0;
  227. }
  228. int prepare_hugepage_range(unsigned long addr, unsigned long len)
  229. {
  230. int err;
  231. if ( (addr+len) < addr )
  232. return -EINVAL;
  233. if ((addr + len) < 0x100000000UL)
  234. err = open_low_hpage_areas(current->mm,
  235. LOW_ESID_MASK(addr, len));
  236. else
  237. err = open_high_hpage_areas(current->mm,
  238. HTLB_AREA_MASK(addr, len));
  239. if (err) {
  240. printk(KERN_DEBUG "prepare_hugepage_range(%lx, %lx)"
  241. " failed (lowmask: 0x%04hx, highmask: 0x%04hx)\n",
  242. addr, len,
  243. LOW_ESID_MASK(addr, len), HTLB_AREA_MASK(addr, len));
  244. return err;
  245. }
  246. return 0;
  247. }
  248. struct page *
  249. follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
  250. {
  251. pte_t *ptep;
  252. struct page *page;
  253. if (! in_hugepage_area(mm->context, address))
  254. return ERR_PTR(-EINVAL);
  255. ptep = huge_pte_offset(mm, address);
  256. page = pte_page(*ptep);
  257. if (page)
  258. page += (address % HPAGE_SIZE) / PAGE_SIZE;
  259. return page;
  260. }
  261. int pmd_huge(pmd_t pmd)
  262. {
  263. return 0;
  264. }
  265. struct page *
  266. follow_huge_pmd(struct mm_struct *mm, unsigned long address,
  267. pmd_t *pmd, int write)
  268. {
  269. BUG();
  270. return NULL;
  271. }
  272. /* Because we have an exclusive hugepage region which lies within the
  273. * normal user address space, we have to take special measures to make
  274. * non-huge mmap()s evade the hugepage reserved regions. */
  275. unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
  276. unsigned long len, unsigned long pgoff,
  277. unsigned long flags)
  278. {
  279. struct mm_struct *mm = current->mm;
  280. struct vm_area_struct *vma;
  281. unsigned long start_addr;
  282. if (len > TASK_SIZE)
  283. return -ENOMEM;
  284. if (addr) {
  285. addr = PAGE_ALIGN(addr);
  286. vma = find_vma(mm, addr);
  287. if (((TASK_SIZE - len) >= addr)
  288. && (!vma || (addr+len) <= vma->vm_start)
  289. && !is_hugepage_only_range(mm, addr,len))
  290. return addr;
  291. }
  292. if (len > mm->cached_hole_size) {
  293. start_addr = addr = mm->free_area_cache;
  294. } else {
  295. start_addr = addr = TASK_UNMAPPED_BASE;
  296. mm->cached_hole_size = 0;
  297. }
  298. full_search:
  299. vma = find_vma(mm, addr);
  300. while (TASK_SIZE - len >= addr) {
  301. BUG_ON(vma && (addr >= vma->vm_end));
  302. if (touches_hugepage_low_range(mm, addr, len)) {
  303. addr = ALIGN(addr+1, 1<<SID_SHIFT);
  304. vma = find_vma(mm, addr);
  305. continue;
  306. }
  307. if (touches_hugepage_high_range(mm, addr, len)) {
  308. addr = ALIGN(addr+1, 1UL<<HTLB_AREA_SHIFT);
  309. vma = find_vma(mm, addr);
  310. continue;
  311. }
  312. if (!vma || addr + len <= vma->vm_start) {
  313. /*
  314. * Remember the place where we stopped the search:
  315. */
  316. mm->free_area_cache = addr + len;
  317. return addr;
  318. }
  319. if (addr + mm->cached_hole_size < vma->vm_start)
  320. mm->cached_hole_size = vma->vm_start - addr;
  321. addr = vma->vm_end;
  322. vma = vma->vm_next;
  323. }
  324. /* Make sure we didn't miss any holes */
  325. if (start_addr != TASK_UNMAPPED_BASE) {
  326. start_addr = addr = TASK_UNMAPPED_BASE;
  327. mm->cached_hole_size = 0;
  328. goto full_search;
  329. }
  330. return -ENOMEM;
  331. }
  332. /*
  333. * This mmap-allocator allocates new areas top-down from below the
  334. * stack's low limit (the base):
  335. *
  336. * Because we have an exclusive hugepage region which lies within the
  337. * normal user address space, we have to take special measures to make
  338. * non-huge mmap()s evade the hugepage reserved regions.
  339. */
  340. unsigned long
  341. arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
  342. const unsigned long len, const unsigned long pgoff,
  343. const unsigned long flags)
  344. {
  345. struct vm_area_struct *vma, *prev_vma;
  346. struct mm_struct *mm = current->mm;
  347. unsigned long base = mm->mmap_base, addr = addr0;
  348. unsigned long largest_hole = mm->cached_hole_size;
  349. int first_time = 1;
  350. /* requested length too big for entire address space */
  351. if (len > TASK_SIZE)
  352. return -ENOMEM;
  353. /* dont allow allocations above current base */
  354. if (mm->free_area_cache > base)
  355. mm->free_area_cache = base;
  356. /* requesting a specific address */
  357. if (addr) {
  358. addr = PAGE_ALIGN(addr);
  359. vma = find_vma(mm, addr);
  360. if (TASK_SIZE - len >= addr &&
  361. (!vma || addr + len <= vma->vm_start)
  362. && !is_hugepage_only_range(mm, addr,len))
  363. return addr;
  364. }
  365. if (len <= largest_hole) {
  366. largest_hole = 0;
  367. mm->free_area_cache = base;
  368. }
  369. try_again:
  370. /* make sure it can fit in the remaining address space */
  371. if (mm->free_area_cache < len)
  372. goto fail;
  373. /* either no address requested or cant fit in requested address hole */
  374. addr = (mm->free_area_cache - len) & PAGE_MASK;
  375. do {
  376. hugepage_recheck:
  377. if (touches_hugepage_low_range(mm, addr, len)) {
  378. addr = (addr & ((~0) << SID_SHIFT)) - len;
  379. goto hugepage_recheck;
  380. } else if (touches_hugepage_high_range(mm, addr, len)) {
  381. addr = (addr & ((~0UL) << HTLB_AREA_SHIFT)) - len;
  382. goto hugepage_recheck;
  383. }
  384. /*
  385. * Lookup failure means no vma is above this address,
  386. * i.e. return with success:
  387. */
  388. if (!(vma = find_vma_prev(mm, addr, &prev_vma)))
  389. return addr;
  390. /*
  391. * new region fits between prev_vma->vm_end and
  392. * vma->vm_start, use it:
  393. */
  394. if (addr+len <= vma->vm_start &&
  395. (!prev_vma || (addr >= prev_vma->vm_end))) {
  396. /* remember the address as a hint for next time */
  397. mm->cached_hole_size = largest_hole;
  398. return (mm->free_area_cache = addr);
  399. } else {
  400. /* pull free_area_cache down to the first hole */
  401. if (mm->free_area_cache == vma->vm_end) {
  402. mm->free_area_cache = vma->vm_start;
  403. mm->cached_hole_size = largest_hole;
  404. }
  405. }
  406. /* remember the largest hole we saw so far */
  407. if (addr + largest_hole < vma->vm_start)
  408. largest_hole = vma->vm_start - addr;
  409. /* try just below the current vma->vm_start */
  410. addr = vma->vm_start-len;
  411. } while (len <= vma->vm_start);
  412. fail:
  413. /*
  414. * if hint left us with no space for the requested
  415. * mapping then try again:
  416. */
  417. if (first_time) {
  418. mm->free_area_cache = base;
  419. largest_hole = 0;
  420. first_time = 0;
  421. goto try_again;
  422. }
  423. /*
  424. * A failed mmap() very likely causes application failure,
  425. * so fall back to the bottom-up function here. This scenario
  426. * can happen with large stack limits and large mmap()
  427. * allocations.
  428. */
  429. mm->free_area_cache = TASK_UNMAPPED_BASE;
  430. mm->cached_hole_size = ~0UL;
  431. addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
  432. /*
  433. * Restore the topdown base:
  434. */
  435. mm->free_area_cache = base;
  436. mm->cached_hole_size = ~0UL;
  437. return addr;
  438. }
  439. static unsigned long htlb_get_low_area(unsigned long len, u16 segmask)
  440. {
  441. unsigned long addr = 0;
  442. struct vm_area_struct *vma;
  443. vma = find_vma(current->mm, addr);
  444. while (addr + len <= 0x100000000UL) {
  445. BUG_ON(vma && (addr >= vma->vm_end)); /* invariant */
  446. if (! __within_hugepage_low_range(addr, len, segmask)) {
  447. addr = ALIGN(addr+1, 1<<SID_SHIFT);
  448. vma = find_vma(current->mm, addr);
  449. continue;
  450. }
  451. if (!vma || (addr + len) <= vma->vm_start)
  452. return addr;
  453. addr = ALIGN(vma->vm_end, HPAGE_SIZE);
  454. /* Depending on segmask this might not be a confirmed
  455. * hugepage region, so the ALIGN could have skipped
  456. * some VMAs */
  457. vma = find_vma(current->mm, addr);
  458. }
  459. return -ENOMEM;
  460. }
  461. static unsigned long htlb_get_high_area(unsigned long len, u16 areamask)
  462. {
  463. unsigned long addr = 0x100000000UL;
  464. struct vm_area_struct *vma;
  465. vma = find_vma(current->mm, addr);
  466. while (addr + len <= TASK_SIZE_USER64) {
  467. BUG_ON(vma && (addr >= vma->vm_end)); /* invariant */
  468. if (! __within_hugepage_high_range(addr, len, areamask)) {
  469. addr = ALIGN(addr+1, 1UL<<HTLB_AREA_SHIFT);
  470. vma = find_vma(current->mm, addr);
  471. continue;
  472. }
  473. if (!vma || (addr + len) <= vma->vm_start)
  474. return addr;
  475. addr = ALIGN(vma->vm_end, HPAGE_SIZE);
  476. /* Depending on segmask this might not be a confirmed
  477. * hugepage region, so the ALIGN could have skipped
  478. * some VMAs */
  479. vma = find_vma(current->mm, addr);
  480. }
  481. return -ENOMEM;
  482. }
  483. unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  484. unsigned long len, unsigned long pgoff,
  485. unsigned long flags)
  486. {
  487. int lastshift;
  488. u16 areamask, curareas;
  489. if (HPAGE_SHIFT == 0)
  490. return -EINVAL;
  491. if (len & ~HPAGE_MASK)
  492. return -EINVAL;
  493. if (!cpu_has_feature(CPU_FTR_16M_PAGE))
  494. return -EINVAL;
  495. if (test_thread_flag(TIF_32BIT)) {
  496. curareas = current->mm->context.low_htlb_areas;
  497. /* First see if we can do the mapping in the existing
  498. * low areas */
  499. addr = htlb_get_low_area(len, curareas);
  500. if (addr != -ENOMEM)
  501. return addr;
  502. lastshift = 0;
  503. for (areamask = LOW_ESID_MASK(0x100000000UL-len, len);
  504. ! lastshift; areamask >>=1) {
  505. if (areamask & 1)
  506. lastshift = 1;
  507. addr = htlb_get_low_area(len, curareas | areamask);
  508. if ((addr != -ENOMEM)
  509. && open_low_hpage_areas(current->mm, areamask) == 0)
  510. return addr;
  511. }
  512. } else {
  513. curareas = current->mm->context.high_htlb_areas;
  514. /* First see if we can do the mapping in the existing
  515. * high areas */
  516. addr = htlb_get_high_area(len, curareas);
  517. if (addr != -ENOMEM)
  518. return addr;
  519. lastshift = 0;
  520. for (areamask = HTLB_AREA_MASK(TASK_SIZE_USER64-len, len);
  521. ! lastshift; areamask >>=1) {
  522. if (areamask & 1)
  523. lastshift = 1;
  524. addr = htlb_get_high_area(len, curareas | areamask);
  525. if ((addr != -ENOMEM)
  526. && open_high_hpage_areas(current->mm, areamask) == 0)
  527. return addr;
  528. }
  529. }
  530. printk(KERN_DEBUG "hugetlb_get_unmapped_area() unable to open"
  531. " enough areas\n");
  532. return -ENOMEM;
  533. }
  534. int hash_huge_page(struct mm_struct *mm, unsigned long access,
  535. unsigned long ea, unsigned long vsid, int local)
  536. {
  537. pte_t *ptep;
  538. unsigned long old_pte, new_pte;
  539. unsigned long va, rflags, pa;
  540. long slot;
  541. int err = 1;
  542. ptep = huge_pte_offset(mm, ea);
  543. /* Search the Linux page table for a match with va */
  544. va = (vsid << 28) | (ea & 0x0fffffff);
  545. /*
  546. * If no pte found or not present, send the problem up to
  547. * do_page_fault
  548. */
  549. if (unlikely(!ptep || pte_none(*ptep)))
  550. goto out;
  551. /*
  552. * Check the user's access rights to the page. If access should be
  553. * prevented then send the problem up to do_page_fault.
  554. */
  555. if (unlikely(access & ~pte_val(*ptep)))
  556. goto out;
  557. /*
  558. * At this point, we have a pte (old_pte) which can be used to build
  559. * or update an HPTE. There are 2 cases:
  560. *
  561. * 1. There is a valid (present) pte with no associated HPTE (this is
  562. * the most common case)
  563. * 2. There is a valid (present) pte with an associated HPTE. The
  564. * current values of the pp bits in the HPTE prevent access
  565. * because we are doing software DIRTY bit management and the
  566. * page is currently not DIRTY.
  567. */
  568. do {
  569. old_pte = pte_val(*ptep);
  570. if (old_pte & _PAGE_BUSY)
  571. goto out;
  572. new_pte = old_pte | _PAGE_BUSY |
  573. _PAGE_ACCESSED | _PAGE_HASHPTE;
  574. } while(old_pte != __cmpxchg_u64((unsigned long *)ptep,
  575. old_pte, new_pte));
  576. rflags = 0x2 | (!(new_pte & _PAGE_RW));
  577. /* _PAGE_EXEC -> HW_NO_EXEC since it's inverted */
  578. rflags |= ((new_pte & _PAGE_EXEC) ? 0 : HPTE_R_N);
  579. /* Check if pte already has an hpte (case 2) */
  580. if (unlikely(old_pte & _PAGE_HASHPTE)) {
  581. /* There MIGHT be an HPTE for this pte */
  582. unsigned long hash, slot;
  583. hash = hpt_hash(va, HPAGE_SHIFT);
  584. if (old_pte & _PAGE_F_SECOND)
  585. hash = ~hash;
  586. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  587. slot += (old_pte & _PAGE_F_GIX) >> 12;
  588. if (ppc_md.hpte_updatepp(slot, rflags, va, 1, local) == -1)
  589. old_pte &= ~_PAGE_HPTEFLAGS;
  590. }
  591. if (likely(!(old_pte & _PAGE_HASHPTE))) {
  592. unsigned long hash = hpt_hash(va, HPAGE_SHIFT);
  593. unsigned long hpte_group;
  594. pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;
  595. repeat:
  596. hpte_group = ((hash & htab_hash_mask) *
  597. HPTES_PER_GROUP) & ~0x7UL;
  598. /* clear HPTE slot informations in new PTE */
  599. new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | _PAGE_HASHPTE;
  600. /* Add in WIMG bits */
  601. /* XXX We should store these in the pte */
  602. /* --BenH: I think they are ... */
  603. rflags |= _PAGE_COHERENT;
  604. /* Insert into the hash table, primary slot */
  605. slot = ppc_md.hpte_insert(hpte_group, va, pa, rflags, 0,
  606. mmu_huge_psize);
  607. /* Primary is full, try the secondary */
  608. if (unlikely(slot == -1)) {
  609. new_pte |= _PAGE_F_SECOND;
  610. hpte_group = ((~hash & htab_hash_mask) *
  611. HPTES_PER_GROUP) & ~0x7UL;
  612. slot = ppc_md.hpte_insert(hpte_group, va, pa, rflags,
  613. HPTE_V_SECONDARY,
  614. mmu_huge_psize);
  615. if (slot == -1) {
  616. if (mftb() & 0x1)
  617. hpte_group = ((hash & htab_hash_mask) *
  618. HPTES_PER_GROUP)&~0x7UL;
  619. ppc_md.hpte_remove(hpte_group);
  620. goto repeat;
  621. }
  622. }
  623. if (unlikely(slot == -2))
  624. panic("hash_huge_page: pte_insert failed\n");
  625. new_pte |= (slot << 12) & _PAGE_F_GIX;
  626. }
  627. /*
  628. * No need to use ldarx/stdcx here because all who
  629. * might be updating the pte will hold the
  630. * page_table_lock
  631. */
  632. *ptep = __pte(new_pte & ~_PAGE_BUSY);
  633. err = 0;
  634. out:
  635. return err;
  636. }