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