hugetlbpage.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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. /* Hack, so that each addresses is controlled by exactly one
  180. * of the high or low area bitmaps, the first high area starts
  181. * at 4GB, not 0 */
  182. if (start == 0)
  183. start = 0x100000000UL;
  184. /* Check no VMAs are in the region */
  185. vma = find_vma(mm, start);
  186. if (vma && (vma->vm_start < end))
  187. return -EBUSY;
  188. return 0;
  189. }
  190. static int open_low_hpage_areas(struct mm_struct *mm, u16 newareas)
  191. {
  192. unsigned long i;
  193. BUILD_BUG_ON((sizeof(newareas)*8) != NUM_LOW_AREAS);
  194. BUILD_BUG_ON((sizeof(mm->context.low_htlb_areas)*8) != NUM_LOW_AREAS);
  195. newareas &= ~(mm->context.low_htlb_areas);
  196. if (! newareas)
  197. return 0; /* The segments we want are already open */
  198. for (i = 0; i < NUM_LOW_AREAS; i++)
  199. if ((1 << i) & newareas)
  200. if (prepare_low_area_for_htlb(mm, i) != 0)
  201. return -EBUSY;
  202. mm->context.low_htlb_areas |= newareas;
  203. /* update the paca copy of the context struct */
  204. get_paca()->context = mm->context;
  205. /* the context change must make it to memory before the flush,
  206. * so that further SLB misses do the right thing. */
  207. mb();
  208. on_each_cpu(flush_low_segments, (void *)(unsigned long)newareas, 0, 1);
  209. return 0;
  210. }
  211. static int open_high_hpage_areas(struct mm_struct *mm, u16 newareas)
  212. {
  213. unsigned long i;
  214. BUILD_BUG_ON((sizeof(newareas)*8) != NUM_HIGH_AREAS);
  215. BUILD_BUG_ON((sizeof(mm->context.high_htlb_areas)*8)
  216. != NUM_HIGH_AREAS);
  217. newareas &= ~(mm->context.high_htlb_areas);
  218. if (! newareas)
  219. return 0; /* The areas we want are already open */
  220. for (i = 0; i < NUM_HIGH_AREAS; i++)
  221. if ((1 << i) & newareas)
  222. if (prepare_high_area_for_htlb(mm, i) != 0)
  223. return -EBUSY;
  224. mm->context.high_htlb_areas |= newareas;
  225. /* update the paca copy of the context struct */
  226. get_paca()->context = mm->context;
  227. /* the context change must make it to memory before the flush,
  228. * so that further SLB misses do the right thing. */
  229. mb();
  230. on_each_cpu(flush_high_segments, (void *)(unsigned long)newareas, 0, 1);
  231. return 0;
  232. }
  233. int prepare_hugepage_range(unsigned long addr, unsigned long len)
  234. {
  235. int err = 0;
  236. if ( (addr+len) < addr )
  237. return -EINVAL;
  238. if (addr < 0x100000000UL)
  239. err = open_low_hpage_areas(current->mm,
  240. LOW_ESID_MASK(addr, len));
  241. if ((addr + len) > 0x100000000UL)
  242. err = open_high_hpage_areas(current->mm,
  243. HTLB_AREA_MASK(addr, len));
  244. if (err) {
  245. printk(KERN_DEBUG "prepare_hugepage_range(%lx, %lx)"
  246. " failed (lowmask: 0x%04hx, highmask: 0x%04hx)\n",
  247. addr, len,
  248. LOW_ESID_MASK(addr, len), HTLB_AREA_MASK(addr, len));
  249. return err;
  250. }
  251. return 0;
  252. }
  253. struct page *
  254. follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
  255. {
  256. pte_t *ptep;
  257. struct page *page;
  258. if (! in_hugepage_area(mm->context, address))
  259. return ERR_PTR(-EINVAL);
  260. ptep = huge_pte_offset(mm, address);
  261. page = pte_page(*ptep);
  262. if (page)
  263. page += (address % HPAGE_SIZE) / PAGE_SIZE;
  264. return page;
  265. }
  266. int pmd_huge(pmd_t pmd)
  267. {
  268. return 0;
  269. }
  270. struct page *
  271. follow_huge_pmd(struct mm_struct *mm, unsigned long address,
  272. pmd_t *pmd, int write)
  273. {
  274. BUG();
  275. return NULL;
  276. }
  277. /* Because we have an exclusive hugepage region which lies within the
  278. * normal user address space, we have to take special measures to make
  279. * non-huge mmap()s evade the hugepage reserved regions. */
  280. unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
  281. unsigned long len, unsigned long pgoff,
  282. unsigned long flags)
  283. {
  284. struct mm_struct *mm = current->mm;
  285. struct vm_area_struct *vma;
  286. unsigned long start_addr;
  287. if (len > TASK_SIZE)
  288. return -ENOMEM;
  289. if (addr) {
  290. addr = PAGE_ALIGN(addr);
  291. vma = find_vma(mm, addr);
  292. if (((TASK_SIZE - len) >= addr)
  293. && (!vma || (addr+len) <= vma->vm_start)
  294. && !is_hugepage_only_range(mm, addr,len))
  295. return addr;
  296. }
  297. if (len > mm->cached_hole_size) {
  298. start_addr = addr = mm->free_area_cache;
  299. } else {
  300. start_addr = addr = TASK_UNMAPPED_BASE;
  301. mm->cached_hole_size = 0;
  302. }
  303. full_search:
  304. vma = find_vma(mm, addr);
  305. while (TASK_SIZE - len >= addr) {
  306. BUG_ON(vma && (addr >= vma->vm_end));
  307. if (touches_hugepage_low_range(mm, addr, len)) {
  308. addr = ALIGN(addr+1, 1<<SID_SHIFT);
  309. vma = find_vma(mm, addr);
  310. continue;
  311. }
  312. if (touches_hugepage_high_range(mm, addr, len)) {
  313. addr = ALIGN(addr+1, 1UL<<HTLB_AREA_SHIFT);
  314. vma = find_vma(mm, addr);
  315. continue;
  316. }
  317. if (!vma || addr + len <= vma->vm_start) {
  318. /*
  319. * Remember the place where we stopped the search:
  320. */
  321. mm->free_area_cache = addr + len;
  322. return addr;
  323. }
  324. if (addr + mm->cached_hole_size < vma->vm_start)
  325. mm->cached_hole_size = vma->vm_start - addr;
  326. addr = vma->vm_end;
  327. vma = vma->vm_next;
  328. }
  329. /* Make sure we didn't miss any holes */
  330. if (start_addr != TASK_UNMAPPED_BASE) {
  331. start_addr = addr = TASK_UNMAPPED_BASE;
  332. mm->cached_hole_size = 0;
  333. goto full_search;
  334. }
  335. return -ENOMEM;
  336. }
  337. /*
  338. * This mmap-allocator allocates new areas top-down from below the
  339. * stack's low limit (the base):
  340. *
  341. * Because we have an exclusive hugepage region which lies within the
  342. * normal user address space, we have to take special measures to make
  343. * non-huge mmap()s evade the hugepage reserved regions.
  344. */
  345. unsigned long
  346. arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
  347. const unsigned long len, const unsigned long pgoff,
  348. const unsigned long flags)
  349. {
  350. struct vm_area_struct *vma, *prev_vma;
  351. struct mm_struct *mm = current->mm;
  352. unsigned long base = mm->mmap_base, addr = addr0;
  353. unsigned long largest_hole = mm->cached_hole_size;
  354. int first_time = 1;
  355. /* requested length too big for entire address space */
  356. if (len > TASK_SIZE)
  357. return -ENOMEM;
  358. /* dont allow allocations above current base */
  359. if (mm->free_area_cache > base)
  360. mm->free_area_cache = base;
  361. /* requesting a specific address */
  362. if (addr) {
  363. addr = PAGE_ALIGN(addr);
  364. vma = find_vma(mm, addr);
  365. if (TASK_SIZE - len >= addr &&
  366. (!vma || addr + len <= vma->vm_start)
  367. && !is_hugepage_only_range(mm, addr,len))
  368. return addr;
  369. }
  370. if (len <= largest_hole) {
  371. largest_hole = 0;
  372. mm->free_area_cache = base;
  373. }
  374. try_again:
  375. /* make sure it can fit in the remaining address space */
  376. if (mm->free_area_cache < len)
  377. goto fail;
  378. /* either no address requested or cant fit in requested address hole */
  379. addr = (mm->free_area_cache - len) & PAGE_MASK;
  380. do {
  381. hugepage_recheck:
  382. if (touches_hugepage_low_range(mm, addr, len)) {
  383. addr = (addr & ((~0) << SID_SHIFT)) - len;
  384. goto hugepage_recheck;
  385. } else if (touches_hugepage_high_range(mm, addr, len)) {
  386. addr = (addr & ((~0UL) << HTLB_AREA_SHIFT)) - len;
  387. goto hugepage_recheck;
  388. }
  389. /*
  390. * Lookup failure means no vma is above this address,
  391. * i.e. return with success:
  392. */
  393. if (!(vma = find_vma_prev(mm, addr, &prev_vma)))
  394. return addr;
  395. /*
  396. * new region fits between prev_vma->vm_end and
  397. * vma->vm_start, use it:
  398. */
  399. if (addr+len <= vma->vm_start &&
  400. (!prev_vma || (addr >= prev_vma->vm_end))) {
  401. /* remember the address as a hint for next time */
  402. mm->cached_hole_size = largest_hole;
  403. return (mm->free_area_cache = addr);
  404. } else {
  405. /* pull free_area_cache down to the first hole */
  406. if (mm->free_area_cache == vma->vm_end) {
  407. mm->free_area_cache = vma->vm_start;
  408. mm->cached_hole_size = largest_hole;
  409. }
  410. }
  411. /* remember the largest hole we saw so far */
  412. if (addr + largest_hole < vma->vm_start)
  413. largest_hole = vma->vm_start - addr;
  414. /* try just below the current vma->vm_start */
  415. addr = vma->vm_start-len;
  416. } while (len <= vma->vm_start);
  417. fail:
  418. /*
  419. * if hint left us with no space for the requested
  420. * mapping then try again:
  421. */
  422. if (first_time) {
  423. mm->free_area_cache = base;
  424. largest_hole = 0;
  425. first_time = 0;
  426. goto try_again;
  427. }
  428. /*
  429. * A failed mmap() very likely causes application failure,
  430. * so fall back to the bottom-up function here. This scenario
  431. * can happen with large stack limits and large mmap()
  432. * allocations.
  433. */
  434. mm->free_area_cache = TASK_UNMAPPED_BASE;
  435. mm->cached_hole_size = ~0UL;
  436. addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
  437. /*
  438. * Restore the topdown base:
  439. */
  440. mm->free_area_cache = base;
  441. mm->cached_hole_size = ~0UL;
  442. return addr;
  443. }
  444. static unsigned long htlb_get_low_area(unsigned long len, u16 segmask)
  445. {
  446. unsigned long addr = 0;
  447. struct vm_area_struct *vma;
  448. vma = find_vma(current->mm, addr);
  449. while (addr + len <= 0x100000000UL) {
  450. BUG_ON(vma && (addr >= vma->vm_end)); /* invariant */
  451. if (! __within_hugepage_low_range(addr, len, segmask)) {
  452. addr = ALIGN(addr+1, 1<<SID_SHIFT);
  453. vma = find_vma(current->mm, addr);
  454. continue;
  455. }
  456. if (!vma || (addr + len) <= vma->vm_start)
  457. return addr;
  458. addr = ALIGN(vma->vm_end, HPAGE_SIZE);
  459. /* Depending on segmask this might not be a confirmed
  460. * hugepage region, so the ALIGN could have skipped
  461. * some VMAs */
  462. vma = find_vma(current->mm, addr);
  463. }
  464. return -ENOMEM;
  465. }
  466. static unsigned long htlb_get_high_area(unsigned long len, u16 areamask)
  467. {
  468. unsigned long addr = 0x100000000UL;
  469. struct vm_area_struct *vma;
  470. vma = find_vma(current->mm, addr);
  471. while (addr + len <= TASK_SIZE_USER64) {
  472. BUG_ON(vma && (addr >= vma->vm_end)); /* invariant */
  473. if (! __within_hugepage_high_range(addr, len, areamask)) {
  474. addr = ALIGN(addr+1, 1UL<<HTLB_AREA_SHIFT);
  475. vma = find_vma(current->mm, addr);
  476. continue;
  477. }
  478. if (!vma || (addr + len) <= vma->vm_start)
  479. return addr;
  480. addr = ALIGN(vma->vm_end, HPAGE_SIZE);
  481. /* Depending on segmask this might not be a confirmed
  482. * hugepage region, so the ALIGN could have skipped
  483. * some VMAs */
  484. vma = find_vma(current->mm, addr);
  485. }
  486. return -ENOMEM;
  487. }
  488. unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  489. unsigned long len, unsigned long pgoff,
  490. unsigned long flags)
  491. {
  492. int lastshift;
  493. u16 areamask, curareas;
  494. if (HPAGE_SHIFT == 0)
  495. return -EINVAL;
  496. if (len & ~HPAGE_MASK)
  497. return -EINVAL;
  498. if (!cpu_has_feature(CPU_FTR_16M_PAGE))
  499. return -EINVAL;
  500. if (test_thread_flag(TIF_32BIT)) {
  501. curareas = current->mm->context.low_htlb_areas;
  502. /* First see if we can do the mapping in the existing
  503. * low areas */
  504. addr = htlb_get_low_area(len, curareas);
  505. if (addr != -ENOMEM)
  506. return addr;
  507. lastshift = 0;
  508. for (areamask = LOW_ESID_MASK(0x100000000UL-len, len);
  509. ! lastshift; areamask >>=1) {
  510. if (areamask & 1)
  511. lastshift = 1;
  512. addr = htlb_get_low_area(len, curareas | areamask);
  513. if ((addr != -ENOMEM)
  514. && open_low_hpage_areas(current->mm, areamask) == 0)
  515. return addr;
  516. }
  517. } else {
  518. curareas = current->mm->context.high_htlb_areas;
  519. /* First see if we can do the mapping in the existing
  520. * high areas */
  521. addr = htlb_get_high_area(len, curareas);
  522. if (addr != -ENOMEM)
  523. return addr;
  524. lastshift = 0;
  525. for (areamask = HTLB_AREA_MASK(TASK_SIZE_USER64-len, len);
  526. ! lastshift; areamask >>=1) {
  527. if (areamask & 1)
  528. lastshift = 1;
  529. addr = htlb_get_high_area(len, curareas | areamask);
  530. if ((addr != -ENOMEM)
  531. && open_high_hpage_areas(current->mm, areamask) == 0)
  532. return addr;
  533. }
  534. }
  535. printk(KERN_DEBUG "hugetlb_get_unmapped_area() unable to open"
  536. " enough areas\n");
  537. return -ENOMEM;
  538. }
  539. int hash_huge_page(struct mm_struct *mm, unsigned long access,
  540. unsigned long ea, unsigned long vsid, int local)
  541. {
  542. pte_t *ptep;
  543. unsigned long old_pte, new_pte;
  544. unsigned long va, rflags, pa;
  545. long slot;
  546. int err = 1;
  547. ptep = huge_pte_offset(mm, ea);
  548. /* Search the Linux page table for a match with va */
  549. va = (vsid << 28) | (ea & 0x0fffffff);
  550. /*
  551. * If no pte found or not present, send the problem up to
  552. * do_page_fault
  553. */
  554. if (unlikely(!ptep || pte_none(*ptep)))
  555. goto out;
  556. /*
  557. * Check the user's access rights to the page. If access should be
  558. * prevented then send the problem up to do_page_fault.
  559. */
  560. if (unlikely(access & ~pte_val(*ptep)))
  561. goto out;
  562. /*
  563. * At this point, we have a pte (old_pte) which can be used to build
  564. * or update an HPTE. There are 2 cases:
  565. *
  566. * 1. There is a valid (present) pte with no associated HPTE (this is
  567. * the most common case)
  568. * 2. There is a valid (present) pte with an associated HPTE. The
  569. * current values of the pp bits in the HPTE prevent access
  570. * because we are doing software DIRTY bit management and the
  571. * page is currently not DIRTY.
  572. */
  573. do {
  574. old_pte = pte_val(*ptep);
  575. if (old_pte & _PAGE_BUSY)
  576. goto out;
  577. new_pte = old_pte | _PAGE_BUSY |
  578. _PAGE_ACCESSED | _PAGE_HASHPTE;
  579. } while(old_pte != __cmpxchg_u64((unsigned long *)ptep,
  580. old_pte, new_pte));
  581. rflags = 0x2 | (!(new_pte & _PAGE_RW));
  582. /* _PAGE_EXEC -> HW_NO_EXEC since it's inverted */
  583. rflags |= ((new_pte & _PAGE_EXEC) ? 0 : HPTE_R_N);
  584. /* Check if pte already has an hpte (case 2) */
  585. if (unlikely(old_pte & _PAGE_HASHPTE)) {
  586. /* There MIGHT be an HPTE for this pte */
  587. unsigned long hash, slot;
  588. hash = hpt_hash(va, HPAGE_SHIFT);
  589. if (old_pte & _PAGE_F_SECOND)
  590. hash = ~hash;
  591. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  592. slot += (old_pte & _PAGE_F_GIX) >> 12;
  593. if (ppc_md.hpte_updatepp(slot, rflags, va, 1, local) == -1)
  594. old_pte &= ~_PAGE_HPTEFLAGS;
  595. }
  596. if (likely(!(old_pte & _PAGE_HASHPTE))) {
  597. unsigned long hash = hpt_hash(va, HPAGE_SHIFT);
  598. unsigned long hpte_group;
  599. pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;
  600. repeat:
  601. hpte_group = ((hash & htab_hash_mask) *
  602. HPTES_PER_GROUP) & ~0x7UL;
  603. /* clear HPTE slot informations in new PTE */
  604. new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | _PAGE_HASHPTE;
  605. /* Add in WIMG bits */
  606. /* XXX We should store these in the pte */
  607. /* --BenH: I think they are ... */
  608. rflags |= _PAGE_COHERENT;
  609. /* Insert into the hash table, primary slot */
  610. slot = ppc_md.hpte_insert(hpte_group, va, pa, rflags, 0,
  611. mmu_huge_psize);
  612. /* Primary is full, try the secondary */
  613. if (unlikely(slot == -1)) {
  614. new_pte |= _PAGE_F_SECOND;
  615. hpte_group = ((~hash & htab_hash_mask) *
  616. HPTES_PER_GROUP) & ~0x7UL;
  617. slot = ppc_md.hpte_insert(hpte_group, va, pa, rflags,
  618. HPTE_V_SECONDARY,
  619. mmu_huge_psize);
  620. if (slot == -1) {
  621. if (mftb() & 0x1)
  622. hpte_group = ((hash & htab_hash_mask) *
  623. HPTES_PER_GROUP)&~0x7UL;
  624. ppc_md.hpte_remove(hpte_group);
  625. goto repeat;
  626. }
  627. }
  628. if (unlikely(slot == -2))
  629. panic("hash_huge_page: pte_insert failed\n");
  630. new_pte |= (slot << 12) & _PAGE_F_GIX;
  631. }
  632. /*
  633. * No need to use ldarx/stdcx here
  634. */
  635. *ptep = __pte(new_pte & ~_PAGE_BUSY);
  636. err = 0;
  637. out:
  638. return err;
  639. }