hugetlbpage.c 22 KB

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