hugetlbpage.c 21 KB

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