pgalloc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Page table allocation functions
  4. *
  5. * Copyright IBM Corp. 2016
  6. * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
  7. */
  8. #include <linux/sysctl.h>
  9. #include <linux/slab.h>
  10. #include <linux/mm.h>
  11. #include <asm/mmu_context.h>
  12. #include <asm/pgalloc.h>
  13. #include <asm/gmap.h>
  14. #include <asm/tlb.h>
  15. #include <asm/tlbflush.h>
  16. #ifdef CONFIG_PGSTE
  17. static int page_table_allocate_pgste_min = 0;
  18. static int page_table_allocate_pgste_max = 1;
  19. int page_table_allocate_pgste = 0;
  20. EXPORT_SYMBOL(page_table_allocate_pgste);
  21. static struct ctl_table page_table_sysctl[] = {
  22. {
  23. .procname = "allocate_pgste",
  24. .data = &page_table_allocate_pgste,
  25. .maxlen = sizeof(int),
  26. .mode = S_IRUGO | S_IWUSR,
  27. .proc_handler = proc_dointvec,
  28. .extra1 = &page_table_allocate_pgste_min,
  29. .extra2 = &page_table_allocate_pgste_max,
  30. },
  31. { }
  32. };
  33. static struct ctl_table page_table_sysctl_dir[] = {
  34. {
  35. .procname = "vm",
  36. .maxlen = 0,
  37. .mode = 0555,
  38. .child = page_table_sysctl,
  39. },
  40. { }
  41. };
  42. static int __init page_table_register_sysctl(void)
  43. {
  44. return register_sysctl_table(page_table_sysctl_dir) ? 0 : -ENOMEM;
  45. }
  46. __initcall(page_table_register_sysctl);
  47. #endif /* CONFIG_PGSTE */
  48. unsigned long *crst_table_alloc(struct mm_struct *mm)
  49. {
  50. struct page *page = alloc_pages(GFP_KERNEL, 2);
  51. if (!page)
  52. return NULL;
  53. arch_set_page_dat(page, 2);
  54. return (unsigned long *) page_to_phys(page);
  55. }
  56. void crst_table_free(struct mm_struct *mm, unsigned long *table)
  57. {
  58. free_pages((unsigned long) table, 2);
  59. }
  60. static void __crst_table_upgrade(void *arg)
  61. {
  62. struct mm_struct *mm = arg;
  63. if (current->active_mm == mm)
  64. set_user_asce(mm);
  65. __tlb_flush_local();
  66. }
  67. int crst_table_upgrade(struct mm_struct *mm, unsigned long end)
  68. {
  69. unsigned long *table, *pgd;
  70. int rc, notify;
  71. /* upgrade should only happen from 3 to 4, 3 to 5, or 4 to 5 levels */
  72. VM_BUG_ON(mm->context.asce_limit < _REGION2_SIZE);
  73. rc = 0;
  74. notify = 0;
  75. while (mm->context.asce_limit < end) {
  76. table = crst_table_alloc(mm);
  77. if (!table) {
  78. rc = -ENOMEM;
  79. break;
  80. }
  81. spin_lock_bh(&mm->page_table_lock);
  82. pgd = (unsigned long *) mm->pgd;
  83. if (mm->context.asce_limit == _REGION2_SIZE) {
  84. crst_table_init(table, _REGION2_ENTRY_EMPTY);
  85. p4d_populate(mm, (p4d_t *) table, (pud_t *) pgd);
  86. mm->pgd = (pgd_t *) table;
  87. mm->context.asce_limit = _REGION1_SIZE;
  88. mm->context.asce = __pa(mm->pgd) | _ASCE_TABLE_LENGTH |
  89. _ASCE_USER_BITS | _ASCE_TYPE_REGION2;
  90. } else {
  91. crst_table_init(table, _REGION1_ENTRY_EMPTY);
  92. pgd_populate(mm, (pgd_t *) table, (p4d_t *) pgd);
  93. mm->pgd = (pgd_t *) table;
  94. mm->context.asce_limit = -PAGE_SIZE;
  95. mm->context.asce = __pa(mm->pgd) | _ASCE_TABLE_LENGTH |
  96. _ASCE_USER_BITS | _ASCE_TYPE_REGION1;
  97. }
  98. notify = 1;
  99. spin_unlock_bh(&mm->page_table_lock);
  100. }
  101. if (notify)
  102. on_each_cpu(__crst_table_upgrade, mm, 0);
  103. return rc;
  104. }
  105. void crst_table_downgrade(struct mm_struct *mm)
  106. {
  107. pgd_t *pgd;
  108. /* downgrade should only happen from 3 to 2 levels (compat only) */
  109. VM_BUG_ON(mm->context.asce_limit != _REGION2_SIZE);
  110. if (current->active_mm == mm) {
  111. clear_user_asce();
  112. __tlb_flush_mm(mm);
  113. }
  114. pgd = mm->pgd;
  115. mm->pgd = (pgd_t *) (pgd_val(*pgd) & _REGION_ENTRY_ORIGIN);
  116. mm->context.asce_limit = _REGION3_SIZE;
  117. mm->context.asce = __pa(mm->pgd) | _ASCE_TABLE_LENGTH |
  118. _ASCE_USER_BITS | _ASCE_TYPE_SEGMENT;
  119. crst_table_free(mm, (unsigned long *) pgd);
  120. if (current->active_mm == mm)
  121. set_user_asce(mm);
  122. }
  123. static inline unsigned int atomic_xor_bits(atomic_t *v, unsigned int bits)
  124. {
  125. unsigned int old, new;
  126. do {
  127. old = atomic_read(v);
  128. new = old ^ bits;
  129. } while (atomic_cmpxchg(v, old, new) != old);
  130. return new;
  131. }
  132. #ifdef CONFIG_PGSTE
  133. struct page *page_table_alloc_pgste(struct mm_struct *mm)
  134. {
  135. struct page *page;
  136. u64 *table;
  137. page = alloc_page(GFP_KERNEL);
  138. if (page) {
  139. table = (u64 *)page_to_phys(page);
  140. memset64(table, _PAGE_INVALID, PTRS_PER_PTE);
  141. memset64(table + PTRS_PER_PTE, 0, PTRS_PER_PTE);
  142. }
  143. return page;
  144. }
  145. void page_table_free_pgste(struct page *page)
  146. {
  147. __free_page(page);
  148. }
  149. #endif /* CONFIG_PGSTE */
  150. /*
  151. * page table entry allocation/free routines.
  152. */
  153. unsigned long *page_table_alloc(struct mm_struct *mm)
  154. {
  155. unsigned long *table;
  156. struct page *page;
  157. unsigned int mask, bit;
  158. /* Try to get a fragment of a 4K page as a 2K page table */
  159. if (!mm_alloc_pgste(mm)) {
  160. table = NULL;
  161. spin_lock_bh(&mm->context.lock);
  162. if (!list_empty(&mm->context.pgtable_list)) {
  163. page = list_first_entry(&mm->context.pgtable_list,
  164. struct page, lru);
  165. mask = atomic_read(&page->_mapcount);
  166. mask = (mask | (mask >> 4)) & 3;
  167. if (mask != 3) {
  168. table = (unsigned long *) page_to_phys(page);
  169. bit = mask & 1; /* =1 -> second 2K */
  170. if (bit)
  171. table += PTRS_PER_PTE;
  172. atomic_xor_bits(&page->_mapcount, 1U << bit);
  173. list_del(&page->lru);
  174. }
  175. }
  176. spin_unlock_bh(&mm->context.lock);
  177. if (table)
  178. return table;
  179. }
  180. /* Allocate a fresh page */
  181. page = alloc_page(GFP_KERNEL);
  182. if (!page)
  183. return NULL;
  184. if (!pgtable_page_ctor(page)) {
  185. __free_page(page);
  186. return NULL;
  187. }
  188. arch_set_page_dat(page, 0);
  189. /* Initialize page table */
  190. table = (unsigned long *) page_to_phys(page);
  191. if (mm_alloc_pgste(mm)) {
  192. /* Return 4K page table with PGSTEs */
  193. atomic_set(&page->_mapcount, 3);
  194. memset64((u64 *)table, _PAGE_INVALID, PTRS_PER_PTE);
  195. memset64((u64 *)table + PTRS_PER_PTE, 0, PTRS_PER_PTE);
  196. } else {
  197. /* Return the first 2K fragment of the page */
  198. atomic_set(&page->_mapcount, 1);
  199. memset64((u64 *)table, _PAGE_INVALID, 2 * PTRS_PER_PTE);
  200. spin_lock_bh(&mm->context.lock);
  201. list_add(&page->lru, &mm->context.pgtable_list);
  202. spin_unlock_bh(&mm->context.lock);
  203. }
  204. return table;
  205. }
  206. void page_table_free(struct mm_struct *mm, unsigned long *table)
  207. {
  208. struct page *page;
  209. unsigned int bit, mask;
  210. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  211. if (!mm_alloc_pgste(mm)) {
  212. /* Free 2K page table fragment of a 4K page */
  213. bit = (__pa(table) & ~PAGE_MASK)/(PTRS_PER_PTE*sizeof(pte_t));
  214. spin_lock_bh(&mm->context.lock);
  215. mask = atomic_xor_bits(&page->_mapcount, 1U << bit);
  216. if (mask & 3)
  217. list_add(&page->lru, &mm->context.pgtable_list);
  218. else
  219. list_del(&page->lru);
  220. spin_unlock_bh(&mm->context.lock);
  221. if (mask != 0)
  222. return;
  223. }
  224. pgtable_page_dtor(page);
  225. atomic_set(&page->_mapcount, -1);
  226. __free_page(page);
  227. }
  228. void page_table_free_rcu(struct mmu_gather *tlb, unsigned long *table,
  229. unsigned long vmaddr)
  230. {
  231. struct mm_struct *mm;
  232. struct page *page;
  233. unsigned int bit, mask;
  234. mm = tlb->mm;
  235. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  236. if (mm_alloc_pgste(mm)) {
  237. gmap_unlink(mm, table, vmaddr);
  238. table = (unsigned long *) (__pa(table) | 3);
  239. tlb_remove_table(tlb, table);
  240. return;
  241. }
  242. bit = (__pa(table) & ~PAGE_MASK) / (PTRS_PER_PTE*sizeof(pte_t));
  243. spin_lock_bh(&mm->context.lock);
  244. mask = atomic_xor_bits(&page->_mapcount, 0x11U << bit);
  245. if (mask & 3)
  246. list_add_tail(&page->lru, &mm->context.pgtable_list);
  247. else
  248. list_del(&page->lru);
  249. spin_unlock_bh(&mm->context.lock);
  250. table = (unsigned long *) (__pa(table) | (1U << bit));
  251. tlb_remove_table(tlb, table);
  252. }
  253. static void __tlb_remove_table(void *_table)
  254. {
  255. unsigned int mask = (unsigned long) _table & 3;
  256. void *table = (void *)((unsigned long) _table ^ mask);
  257. struct page *page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  258. switch (mask) {
  259. case 0: /* pmd, pud, or p4d */
  260. free_pages((unsigned long) table, 2);
  261. break;
  262. case 1: /* lower 2K of a 4K page table */
  263. case 2: /* higher 2K of a 4K page table */
  264. if (atomic_xor_bits(&page->_mapcount, mask << 4) != 0)
  265. break;
  266. /* fallthrough */
  267. case 3: /* 4K page table with pgstes */
  268. pgtable_page_dtor(page);
  269. atomic_set(&page->_mapcount, -1);
  270. __free_page(page);
  271. break;
  272. }
  273. }
  274. static void tlb_remove_table_smp_sync(void *arg)
  275. {
  276. /* Simply deliver the interrupt */
  277. }
  278. static void tlb_remove_table_one(void *table)
  279. {
  280. /*
  281. * This isn't an RCU grace period and hence the page-tables cannot be
  282. * assumed to be actually RCU-freed.
  283. *
  284. * It is however sufficient for software page-table walkers that rely
  285. * on IRQ disabling. See the comment near struct mmu_table_batch.
  286. */
  287. smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
  288. __tlb_remove_table(table);
  289. }
  290. static void tlb_remove_table_rcu(struct rcu_head *head)
  291. {
  292. struct mmu_table_batch *batch;
  293. int i;
  294. batch = container_of(head, struct mmu_table_batch, rcu);
  295. for (i = 0; i < batch->nr; i++)
  296. __tlb_remove_table(batch->tables[i]);
  297. free_page((unsigned long)batch);
  298. }
  299. void tlb_table_flush(struct mmu_gather *tlb)
  300. {
  301. struct mmu_table_batch **batch = &tlb->batch;
  302. if (*batch) {
  303. call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu);
  304. *batch = NULL;
  305. }
  306. }
  307. void tlb_remove_table(struct mmu_gather *tlb, void *table)
  308. {
  309. struct mmu_table_batch **batch = &tlb->batch;
  310. tlb->mm->context.flush_mm = 1;
  311. if (*batch == NULL) {
  312. *batch = (struct mmu_table_batch *)
  313. __get_free_page(GFP_NOWAIT | __GFP_NOWARN);
  314. if (*batch == NULL) {
  315. __tlb_flush_mm_lazy(tlb->mm);
  316. tlb_remove_table_one(table);
  317. return;
  318. }
  319. (*batch)->nr = 0;
  320. }
  321. (*batch)->tables[(*batch)->nr++] = table;
  322. if ((*batch)->nr == MAX_TABLE_BATCH)
  323. tlb_flush_mmu(tlb);
  324. }
  325. /*
  326. * Base infrastructure required to generate basic asces, region, segment,
  327. * and page tables that do not make use of enhanced features like EDAT1.
  328. */
  329. static struct kmem_cache *base_pgt_cache;
  330. static unsigned long base_pgt_alloc(void)
  331. {
  332. u64 *table;
  333. table = kmem_cache_alloc(base_pgt_cache, GFP_KERNEL);
  334. if (table)
  335. memset64(table, _PAGE_INVALID, PTRS_PER_PTE);
  336. return (unsigned long) table;
  337. }
  338. static void base_pgt_free(unsigned long table)
  339. {
  340. kmem_cache_free(base_pgt_cache, (void *) table);
  341. }
  342. static unsigned long base_crst_alloc(unsigned long val)
  343. {
  344. unsigned long table;
  345. table = __get_free_pages(GFP_KERNEL, CRST_ALLOC_ORDER);
  346. if (table)
  347. crst_table_init((unsigned long *)table, val);
  348. return table;
  349. }
  350. static void base_crst_free(unsigned long table)
  351. {
  352. free_pages(table, CRST_ALLOC_ORDER);
  353. }
  354. #define BASE_ADDR_END_FUNC(NAME, SIZE) \
  355. static inline unsigned long base_##NAME##_addr_end(unsigned long addr, \
  356. unsigned long end) \
  357. { \
  358. unsigned long next = (addr + (SIZE)) & ~((SIZE) - 1); \
  359. \
  360. return (next - 1) < (end - 1) ? next : end; \
  361. }
  362. BASE_ADDR_END_FUNC(page, _PAGE_SIZE)
  363. BASE_ADDR_END_FUNC(segment, _SEGMENT_SIZE)
  364. BASE_ADDR_END_FUNC(region3, _REGION3_SIZE)
  365. BASE_ADDR_END_FUNC(region2, _REGION2_SIZE)
  366. BASE_ADDR_END_FUNC(region1, _REGION1_SIZE)
  367. static inline unsigned long base_lra(unsigned long address)
  368. {
  369. unsigned long real;
  370. asm volatile(
  371. " lra %0,0(%1)\n"
  372. : "=d" (real) : "a" (address) : "cc");
  373. return real;
  374. }
  375. static int base_page_walk(unsigned long origin, unsigned long addr,
  376. unsigned long end, int alloc)
  377. {
  378. unsigned long *pte, next;
  379. if (!alloc)
  380. return 0;
  381. pte = (unsigned long *) origin;
  382. pte += (addr & _PAGE_INDEX) >> _PAGE_SHIFT;
  383. do {
  384. next = base_page_addr_end(addr, end);
  385. *pte = base_lra(addr);
  386. } while (pte++, addr = next, addr < end);
  387. return 0;
  388. }
  389. static int base_segment_walk(unsigned long origin, unsigned long addr,
  390. unsigned long end, int alloc)
  391. {
  392. unsigned long *ste, next, table;
  393. int rc;
  394. ste = (unsigned long *) origin;
  395. ste += (addr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT;
  396. do {
  397. next = base_segment_addr_end(addr, end);
  398. if (*ste & _SEGMENT_ENTRY_INVALID) {
  399. if (!alloc)
  400. continue;
  401. table = base_pgt_alloc();
  402. if (!table)
  403. return -ENOMEM;
  404. *ste = table | _SEGMENT_ENTRY;
  405. }
  406. table = *ste & _SEGMENT_ENTRY_ORIGIN;
  407. rc = base_page_walk(table, addr, next, alloc);
  408. if (rc)
  409. return rc;
  410. if (!alloc)
  411. base_pgt_free(table);
  412. cond_resched();
  413. } while (ste++, addr = next, addr < end);
  414. return 0;
  415. }
  416. static int base_region3_walk(unsigned long origin, unsigned long addr,
  417. unsigned long end, int alloc)
  418. {
  419. unsigned long *rtte, next, table;
  420. int rc;
  421. rtte = (unsigned long *) origin;
  422. rtte += (addr & _REGION3_INDEX) >> _REGION3_SHIFT;
  423. do {
  424. next = base_region3_addr_end(addr, end);
  425. if (*rtte & _REGION_ENTRY_INVALID) {
  426. if (!alloc)
  427. continue;
  428. table = base_crst_alloc(_SEGMENT_ENTRY_EMPTY);
  429. if (!table)
  430. return -ENOMEM;
  431. *rtte = table | _REGION3_ENTRY;
  432. }
  433. table = *rtte & _REGION_ENTRY_ORIGIN;
  434. rc = base_segment_walk(table, addr, next, alloc);
  435. if (rc)
  436. return rc;
  437. if (!alloc)
  438. base_crst_free(table);
  439. } while (rtte++, addr = next, addr < end);
  440. return 0;
  441. }
  442. static int base_region2_walk(unsigned long origin, unsigned long addr,
  443. unsigned long end, int alloc)
  444. {
  445. unsigned long *rste, next, table;
  446. int rc;
  447. rste = (unsigned long *) origin;
  448. rste += (addr & _REGION2_INDEX) >> _REGION2_SHIFT;
  449. do {
  450. next = base_region2_addr_end(addr, end);
  451. if (*rste & _REGION_ENTRY_INVALID) {
  452. if (!alloc)
  453. continue;
  454. table = base_crst_alloc(_REGION3_ENTRY_EMPTY);
  455. if (!table)
  456. return -ENOMEM;
  457. *rste = table | _REGION2_ENTRY;
  458. }
  459. table = *rste & _REGION_ENTRY_ORIGIN;
  460. rc = base_region3_walk(table, addr, next, alloc);
  461. if (rc)
  462. return rc;
  463. if (!alloc)
  464. base_crst_free(table);
  465. } while (rste++, addr = next, addr < end);
  466. return 0;
  467. }
  468. static int base_region1_walk(unsigned long origin, unsigned long addr,
  469. unsigned long end, int alloc)
  470. {
  471. unsigned long *rfte, next, table;
  472. int rc;
  473. rfte = (unsigned long *) origin;
  474. rfte += (addr & _REGION1_INDEX) >> _REGION1_SHIFT;
  475. do {
  476. next = base_region1_addr_end(addr, end);
  477. if (*rfte & _REGION_ENTRY_INVALID) {
  478. if (!alloc)
  479. continue;
  480. table = base_crst_alloc(_REGION2_ENTRY_EMPTY);
  481. if (!table)
  482. return -ENOMEM;
  483. *rfte = table | _REGION1_ENTRY;
  484. }
  485. table = *rfte & _REGION_ENTRY_ORIGIN;
  486. rc = base_region2_walk(table, addr, next, alloc);
  487. if (rc)
  488. return rc;
  489. if (!alloc)
  490. base_crst_free(table);
  491. } while (rfte++, addr = next, addr < end);
  492. return 0;
  493. }
  494. /**
  495. * base_asce_free - free asce and tables returned from base_asce_alloc()
  496. * @asce: asce to be freed
  497. *
  498. * Frees all region, segment, and page tables that were allocated with a
  499. * corresponding base_asce_alloc() call.
  500. */
  501. void base_asce_free(unsigned long asce)
  502. {
  503. unsigned long table = asce & _ASCE_ORIGIN;
  504. if (!asce)
  505. return;
  506. switch (asce & _ASCE_TYPE_MASK) {
  507. case _ASCE_TYPE_SEGMENT:
  508. base_segment_walk(table, 0, _REGION3_SIZE, 0);
  509. break;
  510. case _ASCE_TYPE_REGION3:
  511. base_region3_walk(table, 0, _REGION2_SIZE, 0);
  512. break;
  513. case _ASCE_TYPE_REGION2:
  514. base_region2_walk(table, 0, _REGION1_SIZE, 0);
  515. break;
  516. case _ASCE_TYPE_REGION1:
  517. base_region1_walk(table, 0, -_PAGE_SIZE, 0);
  518. break;
  519. }
  520. base_crst_free(table);
  521. }
  522. static int base_pgt_cache_init(void)
  523. {
  524. static DEFINE_MUTEX(base_pgt_cache_mutex);
  525. unsigned long sz = _PAGE_TABLE_SIZE;
  526. if (base_pgt_cache)
  527. return 0;
  528. mutex_lock(&base_pgt_cache_mutex);
  529. if (!base_pgt_cache)
  530. base_pgt_cache = kmem_cache_create("base_pgt", sz, sz, 0, NULL);
  531. mutex_unlock(&base_pgt_cache_mutex);
  532. return base_pgt_cache ? 0 : -ENOMEM;
  533. }
  534. /**
  535. * base_asce_alloc - create kernel mapping without enhanced DAT features
  536. * @addr: virtual start address of kernel mapping
  537. * @num_pages: number of consecutive pages
  538. *
  539. * Generate an asce, including all required region, segment and page tables,
  540. * that can be used to access the virtual kernel mapping. The difference is
  541. * that the returned asce does not make use of any enhanced DAT features like
  542. * e.g. large pages. This is required for some I/O functions that pass an
  543. * asce, like e.g. some service call requests.
  544. *
  545. * Note: the returned asce may NEVER be attached to any cpu. It may only be
  546. * used for I/O requests. tlb entries that might result because the
  547. * asce was attached to a cpu won't be cleared.
  548. */
  549. unsigned long base_asce_alloc(unsigned long addr, unsigned long num_pages)
  550. {
  551. unsigned long asce, table, end;
  552. int rc;
  553. if (base_pgt_cache_init())
  554. return 0;
  555. end = addr + num_pages * PAGE_SIZE;
  556. if (end <= _REGION3_SIZE) {
  557. table = base_crst_alloc(_SEGMENT_ENTRY_EMPTY);
  558. if (!table)
  559. return 0;
  560. rc = base_segment_walk(table, addr, end, 1);
  561. asce = table | _ASCE_TYPE_SEGMENT | _ASCE_TABLE_LENGTH;
  562. } else if (end <= _REGION2_SIZE) {
  563. table = base_crst_alloc(_REGION3_ENTRY_EMPTY);
  564. if (!table)
  565. return 0;
  566. rc = base_region3_walk(table, addr, end, 1);
  567. asce = table | _ASCE_TYPE_REGION3 | _ASCE_TABLE_LENGTH;
  568. } else if (end <= _REGION1_SIZE) {
  569. table = base_crst_alloc(_REGION2_ENTRY_EMPTY);
  570. if (!table)
  571. return 0;
  572. rc = base_region2_walk(table, addr, end, 1);
  573. asce = table | _ASCE_TYPE_REGION2 | _ASCE_TABLE_LENGTH;
  574. } else {
  575. table = base_crst_alloc(_REGION1_ENTRY_EMPTY);
  576. if (!table)
  577. return 0;
  578. rc = base_region1_walk(table, addr, end, 1);
  579. asce = table | _ASCE_TYPE_REGION1 | _ASCE_TABLE_LENGTH;
  580. }
  581. if (rc) {
  582. base_asce_free(asce);
  583. asce = 0;
  584. }
  585. return asce;
  586. }