hugetlbpage.c 18 KB

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