hugetlbpage.c 18 KB

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