hugetlbpage.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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 HUGEPGDIR_SHIFT (HPAGE_SHIFT + PAGE_SHIFT - 3)
  28. #define HUGEPGDIR_SIZE (1UL << HUGEPGDIR_SHIFT)
  29. #define HUGEPGDIR_MASK (~(HUGEPGDIR_SIZE-1))
  30. #define HUGEPTE_INDEX_SIZE 9
  31. #define HUGEPGD_INDEX_SIZE 10
  32. #define PTRS_PER_HUGEPTE (1 << HUGEPTE_INDEX_SIZE)
  33. #define PTRS_PER_HUGEPGD (1 << HUGEPGD_INDEX_SIZE)
  34. static inline int hugepgd_index(unsigned long addr)
  35. {
  36. return (addr & ~REGION_MASK) >> HUGEPGDIR_SHIFT;
  37. }
  38. static pud_t *hugepgd_offset(struct mm_struct *mm, unsigned long addr)
  39. {
  40. int index;
  41. if (! mm->context.huge_pgdir)
  42. return NULL;
  43. index = hugepgd_index(addr);
  44. BUG_ON(index >= PTRS_PER_HUGEPGD);
  45. return (pud_t *)(mm->context.huge_pgdir + index);
  46. }
  47. static inline pte_t *hugepte_offset(pud_t *dir, unsigned long addr)
  48. {
  49. int index;
  50. if (pud_none(*dir))
  51. return NULL;
  52. index = (addr >> HPAGE_SHIFT) % PTRS_PER_HUGEPTE;
  53. return (pte_t *)pud_page(*dir) + index;
  54. }
  55. static pud_t *hugepgd_alloc(struct mm_struct *mm, unsigned long addr)
  56. {
  57. BUG_ON(! in_hugepage_area(mm->context, addr));
  58. if (! mm->context.huge_pgdir) {
  59. pgd_t *new;
  60. spin_unlock(&mm->page_table_lock);
  61. /* Don't use pgd_alloc(), because we want __GFP_REPEAT */
  62. new = kmem_cache_alloc(zero_cache, GFP_KERNEL | __GFP_REPEAT);
  63. BUG_ON(memcmp(new, empty_zero_page, PAGE_SIZE));
  64. spin_lock(&mm->page_table_lock);
  65. /*
  66. * Because we dropped the lock, we should re-check the
  67. * entry, as somebody else could have populated it..
  68. */
  69. if (mm->context.huge_pgdir)
  70. pgd_free(new);
  71. else
  72. mm->context.huge_pgdir = new;
  73. }
  74. return hugepgd_offset(mm, addr);
  75. }
  76. static pte_t *hugepte_alloc(struct mm_struct *mm, pud_t *dir, unsigned long addr)
  77. {
  78. if (! pud_present(*dir)) {
  79. pte_t *new;
  80. spin_unlock(&mm->page_table_lock);
  81. new = kmem_cache_alloc(zero_cache, GFP_KERNEL | __GFP_REPEAT);
  82. BUG_ON(memcmp(new, empty_zero_page, PAGE_SIZE));
  83. spin_lock(&mm->page_table_lock);
  84. /*
  85. * Because we dropped the lock, we should re-check the
  86. * entry, as somebody else could have populated it..
  87. */
  88. if (pud_present(*dir)) {
  89. if (new)
  90. kmem_cache_free(zero_cache, new);
  91. } else {
  92. struct page *ptepage;
  93. if (! new)
  94. return NULL;
  95. ptepage = virt_to_page(new);
  96. ptepage->mapping = (void *) mm;
  97. ptepage->index = addr & HUGEPGDIR_MASK;
  98. pud_populate(mm, dir, new);
  99. }
  100. }
  101. return hugepte_offset(dir, addr);
  102. }
  103. pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
  104. {
  105. pud_t *pud;
  106. BUG_ON(! in_hugepage_area(mm->context, addr));
  107. pud = hugepgd_offset(mm, addr);
  108. if (! pud)
  109. return NULL;
  110. return hugepte_offset(pud, addr);
  111. }
  112. pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr)
  113. {
  114. pud_t *pud;
  115. BUG_ON(! in_hugepage_area(mm->context, addr));
  116. pud = hugepgd_alloc(mm, addr);
  117. if (! pud)
  118. return NULL;
  119. return hugepte_alloc(mm, pud, addr);
  120. }
  121. /*
  122. * This function checks for proper alignment of input addr and len parameters.
  123. */
  124. int is_aligned_hugepage_range(unsigned long addr, unsigned long len)
  125. {
  126. if (len & ~HPAGE_MASK)
  127. return -EINVAL;
  128. if (addr & ~HPAGE_MASK)
  129. return -EINVAL;
  130. if (! (within_hugepage_low_range(addr, len)
  131. || within_hugepage_high_range(addr, len)) )
  132. return -EINVAL;
  133. return 0;
  134. }
  135. static void flush_segments(void *parm)
  136. {
  137. u16 segs = (unsigned long) parm;
  138. unsigned long i;
  139. asm volatile("isync" : : : "memory");
  140. for (i = 0; i < 16; i++) {
  141. if (! (segs & (1U << i)))
  142. continue;
  143. asm volatile("slbie %0" : : "r" (i << SID_SHIFT));
  144. }
  145. asm volatile("isync" : : : "memory");
  146. }
  147. static int prepare_low_seg_for_htlb(struct mm_struct *mm, unsigned long seg)
  148. {
  149. unsigned long start = seg << SID_SHIFT;
  150. unsigned long end = (seg+1) << SID_SHIFT;
  151. struct vm_area_struct *vma;
  152. BUG_ON(seg >= 16);
  153. /* Check no VMAs are in the region */
  154. vma = find_vma(mm, start);
  155. if (vma && (vma->vm_start < end))
  156. return -EBUSY;
  157. return 0;
  158. }
  159. static int open_low_hpage_segs(struct mm_struct *mm, u16 newsegs)
  160. {
  161. unsigned long i;
  162. newsegs &= ~(mm->context.htlb_segs);
  163. if (! newsegs)
  164. return 0; /* The segments we want are already open */
  165. for (i = 0; i < 16; i++)
  166. if ((1 << i) & newsegs)
  167. if (prepare_low_seg_for_htlb(mm, i) != 0)
  168. return -EBUSY;
  169. mm->context.htlb_segs |= newsegs;
  170. /* update the paca copy of the context struct */
  171. get_paca()->context = mm->context;
  172. /* the context change must make it to memory before the flush,
  173. * so that further SLB misses do the right thing. */
  174. mb();
  175. on_each_cpu(flush_segments, (void *)(unsigned long)newsegs, 0, 1);
  176. return 0;
  177. }
  178. int prepare_hugepage_range(unsigned long addr, unsigned long len)
  179. {
  180. if (within_hugepage_high_range(addr, len))
  181. return 0;
  182. else if ((addr < 0x100000000UL) && ((addr+len) < 0x100000000UL)) {
  183. int err;
  184. /* Yes, we need both tests, in case addr+len overflows
  185. * 64-bit arithmetic */
  186. err = open_low_hpage_segs(current->mm,
  187. LOW_ESID_MASK(addr, len));
  188. if (err)
  189. printk(KERN_DEBUG "prepare_hugepage_range(%lx, %lx)"
  190. " failed (segs: 0x%04hx)\n", addr, len,
  191. LOW_ESID_MASK(addr, len));
  192. return err;
  193. }
  194. return -EINVAL;
  195. }
  196. struct page *
  197. follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
  198. {
  199. pte_t *ptep;
  200. struct page *page;
  201. if (! in_hugepage_area(mm->context, address))
  202. return ERR_PTR(-EINVAL);
  203. ptep = huge_pte_offset(mm, address);
  204. page = pte_page(*ptep);
  205. if (page)
  206. page += (address % HPAGE_SIZE) / PAGE_SIZE;
  207. return page;
  208. }
  209. int pmd_huge(pmd_t pmd)
  210. {
  211. return 0;
  212. }
  213. struct page *
  214. follow_huge_pmd(struct mm_struct *mm, unsigned long address,
  215. pmd_t *pmd, int write)
  216. {
  217. BUG();
  218. return NULL;
  219. }
  220. /* Because we have an exclusive hugepage region which lies within the
  221. * normal user address space, we have to take special measures to make
  222. * non-huge mmap()s evade the hugepage reserved regions. */
  223. unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
  224. unsigned long len, unsigned long pgoff,
  225. unsigned long flags)
  226. {
  227. struct mm_struct *mm = current->mm;
  228. struct vm_area_struct *vma;
  229. unsigned long start_addr;
  230. if (len > TASK_SIZE)
  231. return -ENOMEM;
  232. if (addr) {
  233. addr = PAGE_ALIGN(addr);
  234. vma = find_vma(mm, addr);
  235. if (((TASK_SIZE - len) >= addr)
  236. && (!vma || (addr+len) <= vma->vm_start)
  237. && !is_hugepage_only_range(mm, addr,len))
  238. return addr;
  239. }
  240. if (len > mm->cached_hole_size) {
  241. start_addr = addr = mm->free_area_cache;
  242. } else {
  243. start_addr = addr = TASK_UNMAPPED_BASE;
  244. mm->cached_hole_size = 0;
  245. }
  246. full_search:
  247. vma = find_vma(mm, addr);
  248. while (TASK_SIZE - len >= addr) {
  249. BUG_ON(vma && (addr >= vma->vm_end));
  250. if (touches_hugepage_low_range(mm, addr, len)) {
  251. addr = ALIGN(addr+1, 1<<SID_SHIFT);
  252. vma = find_vma(mm, addr);
  253. continue;
  254. }
  255. if (touches_hugepage_high_range(addr, len)) {
  256. addr = TASK_HPAGE_END;
  257. vma = find_vma(mm, addr);
  258. continue;
  259. }
  260. if (!vma || addr + len <= vma->vm_start) {
  261. /*
  262. * Remember the place where we stopped the search:
  263. */
  264. mm->free_area_cache = addr + len;
  265. return addr;
  266. }
  267. if (addr + mm->cached_hole_size < vma->vm_start)
  268. mm->cached_hole_size = vma->vm_start - addr;
  269. addr = vma->vm_end;
  270. vma = vma->vm_next;
  271. }
  272. /* Make sure we didn't miss any holes */
  273. if (start_addr != TASK_UNMAPPED_BASE) {
  274. start_addr = addr = TASK_UNMAPPED_BASE;
  275. mm->cached_hole_size = 0;
  276. goto full_search;
  277. }
  278. return -ENOMEM;
  279. }
  280. /*
  281. * This mmap-allocator allocates new areas top-down from below the
  282. * stack's low limit (the base):
  283. *
  284. * Because we have an exclusive hugepage region which lies within the
  285. * normal user address space, we have to take special measures to make
  286. * non-huge mmap()s evade the hugepage reserved regions.
  287. */
  288. unsigned long
  289. arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
  290. const unsigned long len, const unsigned long pgoff,
  291. const unsigned long flags)
  292. {
  293. struct vm_area_struct *vma, *prev_vma;
  294. struct mm_struct *mm = current->mm;
  295. unsigned long base = mm->mmap_base, addr = addr0;
  296. unsigned long largest_hole = mm->cached_hole_size;
  297. int first_time = 1;
  298. /* requested length too big for entire address space */
  299. if (len > TASK_SIZE)
  300. return -ENOMEM;
  301. /* dont allow allocations above current base */
  302. if (mm->free_area_cache > base)
  303. mm->free_area_cache = base;
  304. /* requesting a specific address */
  305. if (addr) {
  306. addr = PAGE_ALIGN(addr);
  307. vma = find_vma(mm, addr);
  308. if (TASK_SIZE - len >= addr &&
  309. (!vma || addr + len <= vma->vm_start)
  310. && !is_hugepage_only_range(mm, addr,len))
  311. return addr;
  312. }
  313. if (len <= largest_hole) {
  314. largest_hole = 0;
  315. mm->free_area_cache = base;
  316. }
  317. try_again:
  318. /* make sure it can fit in the remaining address space */
  319. if (mm->free_area_cache < len)
  320. goto fail;
  321. /* either no address requested or cant fit in requested address hole */
  322. addr = (mm->free_area_cache - len) & PAGE_MASK;
  323. do {
  324. hugepage_recheck:
  325. if (touches_hugepage_low_range(mm, addr, len)) {
  326. addr = (addr & ((~0) << SID_SHIFT)) - len;
  327. goto hugepage_recheck;
  328. } else if (touches_hugepage_high_range(addr, len)) {
  329. addr = TASK_HPAGE_BASE - len;
  330. }
  331. /*
  332. * Lookup failure means no vma is above this address,
  333. * i.e. return with success:
  334. */
  335. if (!(vma = find_vma_prev(mm, addr, &prev_vma)))
  336. return addr;
  337. /*
  338. * new region fits between prev_vma->vm_end and
  339. * vma->vm_start, use it:
  340. */
  341. if (addr+len <= vma->vm_start &&
  342. (!prev_vma || (addr >= prev_vma->vm_end))) {
  343. /* remember the address as a hint for next time */
  344. mm->cached_hole_size = largest_hole;
  345. return (mm->free_area_cache = addr);
  346. } else {
  347. /* pull free_area_cache down to the first hole */
  348. if (mm->free_area_cache == vma->vm_end) {
  349. mm->free_area_cache = vma->vm_start;
  350. mm->cached_hole_size = largest_hole;
  351. }
  352. }
  353. /* remember the largest hole we saw so far */
  354. if (addr + largest_hole < vma->vm_start)
  355. largest_hole = vma->vm_start - addr;
  356. /* try just below the current vma->vm_start */
  357. addr = vma->vm_start-len;
  358. } while (len <= vma->vm_start);
  359. fail:
  360. /*
  361. * if hint left us with no space for the requested
  362. * mapping then try again:
  363. */
  364. if (first_time) {
  365. mm->free_area_cache = base;
  366. largest_hole = 0;
  367. first_time = 0;
  368. goto try_again;
  369. }
  370. /*
  371. * A failed mmap() very likely causes application failure,
  372. * so fall back to the bottom-up function here. This scenario
  373. * can happen with large stack limits and large mmap()
  374. * allocations.
  375. */
  376. mm->free_area_cache = TASK_UNMAPPED_BASE;
  377. mm->cached_hole_size = ~0UL;
  378. addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
  379. /*
  380. * Restore the topdown base:
  381. */
  382. mm->free_area_cache = base;
  383. mm->cached_hole_size = ~0UL;
  384. return addr;
  385. }
  386. static unsigned long htlb_get_low_area(unsigned long len, u16 segmask)
  387. {
  388. unsigned long addr = 0;
  389. struct vm_area_struct *vma;
  390. vma = find_vma(current->mm, addr);
  391. while (addr + len <= 0x100000000UL) {
  392. BUG_ON(vma && (addr >= vma->vm_end)); /* invariant */
  393. if (! __within_hugepage_low_range(addr, len, segmask)) {
  394. addr = ALIGN(addr+1, 1<<SID_SHIFT);
  395. vma = find_vma(current->mm, addr);
  396. continue;
  397. }
  398. if (!vma || (addr + len) <= vma->vm_start)
  399. return addr;
  400. addr = ALIGN(vma->vm_end, HPAGE_SIZE);
  401. /* Depending on segmask this might not be a confirmed
  402. * hugepage region, so the ALIGN could have skipped
  403. * some VMAs */
  404. vma = find_vma(current->mm, addr);
  405. }
  406. return -ENOMEM;
  407. }
  408. static unsigned long htlb_get_high_area(unsigned long len)
  409. {
  410. unsigned long addr = TASK_HPAGE_BASE;
  411. struct vm_area_struct *vma;
  412. vma = find_vma(current->mm, addr);
  413. for (vma = find_vma(current->mm, addr);
  414. addr + len <= TASK_HPAGE_END;
  415. vma = vma->vm_next) {
  416. BUG_ON(vma && (addr >= vma->vm_end)); /* invariant */
  417. BUG_ON(! within_hugepage_high_range(addr, len));
  418. if (!vma || (addr + len) <= vma->vm_start)
  419. return addr;
  420. addr = ALIGN(vma->vm_end, HPAGE_SIZE);
  421. /* Because we're in a hugepage region, this alignment
  422. * should not skip us over any VMAs */
  423. }
  424. return -ENOMEM;
  425. }
  426. unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  427. unsigned long len, unsigned long pgoff,
  428. unsigned long flags)
  429. {
  430. if (len & ~HPAGE_MASK)
  431. return -EINVAL;
  432. if (!cpu_has_feature(CPU_FTR_16M_PAGE))
  433. return -EINVAL;
  434. if (test_thread_flag(TIF_32BIT)) {
  435. int lastshift = 0;
  436. u16 segmask, cursegs = current->mm->context.htlb_segs;
  437. /* First see if we can do the mapping in the existing
  438. * low hpage segments */
  439. addr = htlb_get_low_area(len, cursegs);
  440. if (addr != -ENOMEM)
  441. return addr;
  442. for (segmask = LOW_ESID_MASK(0x100000000UL-len, len);
  443. ! lastshift; segmask >>=1) {
  444. if (segmask & 1)
  445. lastshift = 1;
  446. addr = htlb_get_low_area(len, cursegs | segmask);
  447. if ((addr != -ENOMEM)
  448. && open_low_hpage_segs(current->mm, segmask) == 0)
  449. return addr;
  450. }
  451. printk(KERN_DEBUG "hugetlb_get_unmapped_area() unable to open"
  452. " enough segments\n");
  453. return -ENOMEM;
  454. } else {
  455. return htlb_get_high_area(len);
  456. }
  457. }
  458. void hugetlb_mm_free_pgd(struct mm_struct *mm)
  459. {
  460. int i;
  461. pgd_t *pgdir;
  462. spin_lock(&mm->page_table_lock);
  463. pgdir = mm->context.huge_pgdir;
  464. if (! pgdir)
  465. goto out;
  466. mm->context.huge_pgdir = NULL;
  467. /* cleanup any hugepte pages leftover */
  468. for (i = 0; i < PTRS_PER_HUGEPGD; i++) {
  469. pud_t *pud = (pud_t *)(pgdir + i);
  470. if (! pud_none(*pud)) {
  471. pte_t *pte = (pte_t *)pud_page(*pud);
  472. struct page *ptepage = virt_to_page(pte);
  473. ptepage->mapping = NULL;
  474. BUG_ON(memcmp(pte, empty_zero_page, PAGE_SIZE));
  475. kmem_cache_free(zero_cache, pte);
  476. }
  477. pud_clear(pud);
  478. }
  479. BUG_ON(memcmp(pgdir, empty_zero_page, PAGE_SIZE));
  480. kmem_cache_free(zero_cache, pgdir);
  481. out:
  482. spin_unlock(&mm->page_table_lock);
  483. }
  484. int hash_huge_page(struct mm_struct *mm, unsigned long access,
  485. unsigned long ea, unsigned long vsid, int local)
  486. {
  487. pte_t *ptep;
  488. unsigned long va, vpn;
  489. pte_t old_pte, new_pte;
  490. unsigned long hpteflags, prpn;
  491. long slot;
  492. int err = 1;
  493. spin_lock(&mm->page_table_lock);
  494. ptep = huge_pte_offset(mm, ea);
  495. /* Search the Linux page table for a match with va */
  496. va = (vsid << 28) | (ea & 0x0fffffff);
  497. vpn = va >> HPAGE_SHIFT;
  498. /*
  499. * If no pte found or not present, send the problem up to
  500. * do_page_fault
  501. */
  502. if (unlikely(!ptep || pte_none(*ptep)))
  503. goto out;
  504. /* BUG_ON(pte_bad(*ptep)); */
  505. /*
  506. * Check the user's access rights to the page. If access should be
  507. * prevented then send the problem up to do_page_fault.
  508. */
  509. if (unlikely(access & ~pte_val(*ptep)))
  510. goto out;
  511. /*
  512. * At this point, we have a pte (old_pte) which can be used to build
  513. * or update an HPTE. There are 2 cases:
  514. *
  515. * 1. There is a valid (present) pte with no associated HPTE (this is
  516. * the most common case)
  517. * 2. There is a valid (present) pte with an associated HPTE. The
  518. * current values of the pp bits in the HPTE prevent access
  519. * because we are doing software DIRTY bit management and the
  520. * page is currently not DIRTY.
  521. */
  522. old_pte = *ptep;
  523. new_pte = old_pte;
  524. hpteflags = 0x2 | (! (pte_val(new_pte) & _PAGE_RW));
  525. /* _PAGE_EXEC -> HW_NO_EXEC since it's inverted */
  526. hpteflags |= ((pte_val(new_pte) & _PAGE_EXEC) ? 0 : HW_NO_EXEC);
  527. /* Check if pte already has an hpte (case 2) */
  528. if (unlikely(pte_val(old_pte) & _PAGE_HASHPTE)) {
  529. /* There MIGHT be an HPTE for this pte */
  530. unsigned long hash, slot;
  531. hash = hpt_hash(vpn, 1);
  532. if (pte_val(old_pte) & _PAGE_SECONDARY)
  533. hash = ~hash;
  534. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  535. slot += (pte_val(old_pte) & _PAGE_GROUP_IX) >> 12;
  536. if (ppc_md.hpte_updatepp(slot, hpteflags, va, 1, local) == -1)
  537. pte_val(old_pte) &= ~_PAGE_HPTEFLAGS;
  538. }
  539. if (likely(!(pte_val(old_pte) & _PAGE_HASHPTE))) {
  540. unsigned long hash = hpt_hash(vpn, 1);
  541. unsigned long hpte_group;
  542. prpn = pte_pfn(old_pte);
  543. repeat:
  544. hpte_group = ((hash & htab_hash_mask) *
  545. HPTES_PER_GROUP) & ~0x7UL;
  546. /* Update the linux pte with the HPTE slot */
  547. pte_val(new_pte) &= ~_PAGE_HPTEFLAGS;
  548. pte_val(new_pte) |= _PAGE_HASHPTE;
  549. /* Add in WIMG bits */
  550. /* XXX We should store these in the pte */
  551. hpteflags |= _PAGE_COHERENT;
  552. slot = ppc_md.hpte_insert(hpte_group, va, prpn, 0,
  553. hpteflags, 0, 1);
  554. /* Primary is full, try the secondary */
  555. if (unlikely(slot == -1)) {
  556. pte_val(new_pte) |= _PAGE_SECONDARY;
  557. hpte_group = ((~hash & htab_hash_mask) *
  558. HPTES_PER_GROUP) & ~0x7UL;
  559. slot = ppc_md.hpte_insert(hpte_group, va, prpn,
  560. 1, hpteflags, 0, 1);
  561. if (slot == -1) {
  562. if (mftb() & 0x1)
  563. hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
  564. ppc_md.hpte_remove(hpte_group);
  565. goto repeat;
  566. }
  567. }
  568. if (unlikely(slot == -2))
  569. panic("hash_huge_page: pte_insert failed\n");
  570. pte_val(new_pte) |= (slot<<12) & _PAGE_GROUP_IX;
  571. /*
  572. * No need to use ldarx/stdcx here because all who
  573. * might be updating the pte will hold the
  574. * page_table_lock
  575. */
  576. *ptep = new_pte;
  577. }
  578. err = 0;
  579. out:
  580. spin_unlock(&mm->page_table_lock);
  581. return err;
  582. }