blob: 65154eaa3714a4e9182cb87654e7b896e7be3e2f [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * S390 version
4 *
5 * Derived from "include/asm-i386/mmu_context.h"
6 */
7
8#ifndef __S390_MMU_CONTEXT_H
9#define __S390_MMU_CONTEXT_H
10
Gerald Schaeferc1821c22007-02-05 21:18:17 +010011#include <asm/pgalloc.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080012#include <linux/uaccess.h>
Ingo Molnar589ee622017-02-04 00:16:44 +010013#include <linux/mm_types.h>
Martin Schwidefsky050eef32010-08-24 09:26:21 +020014#include <asm/tlbflush.h>
David Howellsa0616cd2012-03-28 18:30:02 +010015#include <asm/ctl_reg.h>
Martin Schwidefsky8e58ab52017-08-23 12:13:51 +020016#include <asm-generic/mm_hooks.h>
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +020017
Martin Schwidefsky6f457e12008-01-26 14:10:58 +010018static inline int init_new_context(struct task_struct *tsk,
19 struct mm_struct *mm)
20{
Martin Schwidefsky60f07c82017-08-17 08:15:16 +020021 spin_lock_init(&mm->context.lock);
Martin Schwidefsky3446c132016-02-15 14:46:49 +010022 INIT_LIST_HEAD(&mm->context.pgtable_list);
23 INIT_LIST_HEAD(&mm->context.gmap_list);
Martin Schwidefsky1b948d62014-04-03 13:55:01 +020024 cpumask_clear(&mm->context.cpu_attach_mask);
Martin Schwidefsky64f31d52016-05-25 09:45:26 +020025 atomic_set(&mm->context.flush_count, 0);
Martin Schwidefsky44b6cc82016-06-13 10:36:00 +020026 mm->context.gmap_asce = 0;
Martin Schwidefsky050eef32010-08-24 09:26:21 +020027 mm->context.flush_mm = 0;
Martin Schwidefsky0b46e0a2015-04-15 13:23:26 +020028#ifdef CONFIG_PGSTE
Martin Schwidefsky23fefe12017-06-07 14:10:24 +020029 mm->context.alloc_pgste = page_table_allocate_pgste ||
30 test_thread_flag(TIF_PGSTE) ||
Martin Schwidefsky53c4ab72017-11-22 17:19:32 +010031 (current->mm && current->mm->context.alloc_pgste);
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +020032 mm->context.has_pgste = 0;
Dominik Dingel693ffc02014-01-14 18:11:14 +010033 mm->context.use_skey = 0;
Claudio Imbrendaaa824e12017-04-20 10:03:46 +020034 mm->context.use_cmma = 0;
Martin Schwidefsky0b46e0a2015-04-15 13:23:26 +020035#endif
Gerald Schaefer723cacb2016-04-15 16:38:40 +020036 switch (mm->context.asce_limit) {
Heiko Carstensf1c11742017-07-05 07:37:27 +020037 case _REGION2_SIZE:
Gerald Schaefer723cacb2016-04-15 16:38:40 +020038 /*
39 * forked 3-level task, fall through to set new asce with new
40 * mm->pgd
41 */
42 case 0:
Martin Schwidefsky3446c132016-02-15 14:46:49 +010043 /* context created by exec, set asce limit to 4TB */
Martin Schwidefsky3446c132016-02-15 14:46:49 +010044 mm->context.asce_limit = STACK_TOP_MAX;
Gerald Schaefer723cacb2016-04-15 16:38:40 +020045 mm->context.asce = __pa(mm->pgd) | _ASCE_TABLE_LENGTH |
46 _ASCE_USER_BITS | _ASCE_TYPE_REGION3;
Kirill A. Shutemovb4e98d92017-11-15 17:35:33 -080047 /* pgd_alloc() did not account this pud */
48 mm_inc_nr_puds(mm);
Gerald Schaefer723cacb2016-04-15 16:38:40 +020049 break;
Martin Schwidefsky0b89ede2017-08-31 12:30:54 +020050 case -PAGE_SIZE:
51 /* forked 5-level task, set new asce with new_mm->pgd */
52 mm->context.asce = __pa(mm->pgd) | _ASCE_TABLE_LENGTH |
53 _ASCE_USER_BITS | _ASCE_TYPE_REGION1;
54 break;
Heiko Carstensf1c11742017-07-05 07:37:27 +020055 case _REGION1_SIZE:
Gerald Schaefer723cacb2016-04-15 16:38:40 +020056 /* forked 4-level task, set new asce with new mm->pgd */
57 mm->context.asce = __pa(mm->pgd) | _ASCE_TABLE_LENGTH |
58 _ASCE_USER_BITS | _ASCE_TYPE_REGION2;
59 break;
Heiko Carstensf1c11742017-07-05 07:37:27 +020060 case _REGION3_SIZE:
Gerald Schaefer723cacb2016-04-15 16:38:40 +020061 /* forked 2-level compat task, set new asce with new mm->pgd */
62 mm->context.asce = __pa(mm->pgd) | _ASCE_TABLE_LENGTH |
63 _ASCE_USER_BITS | _ASCE_TYPE_SEGMENT;
Kirill A. Shutemovb4e98d92017-11-15 17:35:33 -080064 /* pgd_alloc() did not account this pmd */
Martin Schwidefsky3446c132016-02-15 14:46:49 +010065 mm_inc_nr_pmds(mm);
66 }
Martin Schwidefsky6252d702008-02-09 18:24:37 +010067 crst_table_init((unsigned long *) mm->pgd, pgd_entry_type(mm));
Martin Schwidefsky6f457e12008-01-26 14:10:58 +010068 return 0;
69}
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71#define destroy_context(mm) do { } while (0)
72
Martin Schwidefskybeef5602014-04-14 15:11:26 +020073static inline void set_user_asce(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Gerald Schaefer723cacb2016-04-15 16:38:40 +020075 S390_lowcore.user_asce = mm->context.asce;
Martin Schwidefsky0aaba412017-08-22 12:08:22 +020076 __ctl_load(S390_lowcore.user_asce, 1, 1);
77 clear_cpu_flag(CIF_ASCE_PRIMARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
Martin Schwidefskybeef5602014-04-14 15:11:26 +020080static inline void clear_user_asce(void)
Martin Schwidefsky02a8f3a2014-04-03 13:54:59 +020081{
82 S390_lowcore.user_asce = S390_lowcore.kernel_asce;
Martin Schwidefsky0aaba412017-08-22 12:08:22 +020083 __ctl_load(S390_lowcore.kernel_asce, 1, 1);
Heiko Carstens606aa4a2017-02-17 08:12:30 +010084 set_cpu_flag(CIF_ASCE_PRIMARY);
Martin Schwidefsky02a8f3a2014-04-03 13:54:59 +020085}
86
Martin Schwidefsky0aaba412017-08-22 12:08:22 +020087mm_segment_t enable_sacf_uaccess(void);
88void disable_sacf_uaccess(mm_segment_t old_fs);
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
Gerald Schaeferc1821c22007-02-05 21:18:17 +010091 struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Martin Schwidefsky53e857f2012-09-10 13:00:09 +020093 int cpu = smp_processor_id();
94
95 if (prev == next)
96 return;
Martin Schwidefsky0aaba412017-08-22 12:08:22 +020097 S390_lowcore.user_asce = next->context.asce;
Martin Schwidefsky64f31d52016-05-25 09:45:26 +020098 cpumask_set_cpu(cpu, &next->context.cpu_attach_mask);
Martin Schwidefsky0aaba412017-08-22 12:08:22 +020099 /* Clear previous user-ASCE from CR1 and CR7 */
100 if (!test_cpu_flag(CIF_ASCE_PRIMARY)) {
101 __ctl_load(S390_lowcore.kernel_asce, 1, 1);
102 set_cpu_flag(CIF_ASCE_PRIMARY);
103 }
104 if (test_cpu_flag(CIF_ASCE_SECONDARY)) {
105 __ctl_load(S390_lowcore.vdso_asce, 7, 7);
106 clear_cpu_flag(CIF_ASCE_SECONDARY);
107 }
Martin Schwidefsky64f31d52016-05-25 09:45:26 +0200108 cpumask_clear_cpu(cpu, &prev->context.cpu_attach_mask);
Martin Schwidefsky53e857f2012-09-10 13:00:09 +0200109}
110
111#define finish_arch_post_lock_switch finish_arch_post_lock_switch
112static inline void finish_arch_post_lock_switch(void)
113{
114 struct task_struct *tsk = current;
115 struct mm_struct *mm = tsk->mm;
116
Martin Schwidefskyf8b13502014-06-02 14:53:57 +0200117 if (mm) {
118 preempt_disable();
Martin Schwidefsky64f31d52016-05-25 09:45:26 +0200119 while (atomic_read(&mm->context.flush_count))
Martin Schwidefskyf8b13502014-06-02 14:53:57 +0200120 cpu_relax();
Martin Schwidefskyb3e5dc42017-08-16 14:10:01 +0200121 cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
Martin Schwidefsky60f07c82017-08-17 08:15:16 +0200122 __tlb_flush_mm_lazy(mm);
Martin Schwidefskyf8b13502014-06-02 14:53:57 +0200123 preempt_enable();
124 }
125 set_fs(current->thread.mm_segment);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200128#define enter_lazy_tlb(mm,tsk) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129#define deactivate_mm(tsk,mm) do { } while (0)
130
Adrian Bunk4448aaf2005-11-08 21:34:42 -0800131static inline void activate_mm(struct mm_struct *prev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 struct mm_struct *next)
133{
Martin Schwidefskybeef5602014-04-14 15:11:26 +0200134 switch_mm(prev, next, current);
Martin Schwidefskyb3e5dc42017-08-16 14:10:01 +0200135 cpumask_set_cpu(smp_processor_id(), mm_cpumask(next));
Martin Schwidefskybeef5602014-04-14 15:11:26 +0200136 set_user_asce(next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100139#endif /* __S390_MMU_CONTEXT_H */