Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/arch/s390/mm/fault.c
50378 views
1
// SPDX-License-Identifier: GPL-2.0
2
/*
3
* S390 version
4
* Copyright IBM Corp. 1999
5
* Author(s): Hartmut Penner ([email protected])
6
* Ulrich Weigand ([email protected])
7
*
8
* Derived from "arch/i386/mm/fault.c"
9
* Copyright (C) 1995 Linus Torvalds
10
*/
11
12
#include <linux/kernel_stat.h>
13
#include <linux/mmu_context.h>
14
#include <linux/cpufeature.h>
15
#include <linux/perf_event.h>
16
#include <linux/signal.h>
17
#include <linux/sched.h>
18
#include <linux/sched/debug.h>
19
#include <linux/kernel.h>
20
#include <linux/errno.h>
21
#include <linux/string.h>
22
#include <linux/types.h>
23
#include <linux/ptrace.h>
24
#include <linux/mman.h>
25
#include <linux/mm.h>
26
#include <linux/smp.h>
27
#include <linux/kdebug.h>
28
#include <linux/init.h>
29
#include <linux/console.h>
30
#include <linux/extable.h>
31
#include <linux/hardirq.h>
32
#include <linux/kprobes.h>
33
#include <linux/uaccess.h>
34
#include <linux/hugetlb.h>
35
#include <linux/kfence.h>
36
#include <linux/pagewalk.h>
37
#include <asm/asm-extable.h>
38
#include <asm/asm-offsets.h>
39
#include <asm/ptrace.h>
40
#include <asm/fault.h>
41
#include <asm/diag.h>
42
#include <asm/irq.h>
43
#include <asm/facility.h>
44
#include <asm/uv.h>
45
#include "../kernel/entry.h"
46
47
/*
48
* Find out which address space caused the exception.
49
*/
50
static bool is_kernel_fault(struct pt_regs *regs)
51
{
52
union teid teid = { .val = regs->int_parm_long };
53
54
if (user_mode(regs))
55
return false;
56
if (teid.as == PSW_BITS_AS_SECONDARY)
57
return false;
58
return true;
59
}
60
61
static unsigned long get_fault_address(struct pt_regs *regs)
62
{
63
union teid teid = { .val = regs->int_parm_long };
64
65
return teid.addr * PAGE_SIZE;
66
}
67
68
static __always_inline bool fault_is_write(struct pt_regs *regs)
69
{
70
union teid teid = { .val = regs->int_parm_long };
71
72
if (test_facility(75))
73
return teid.fsi == TEID_FSI_STORE;
74
return false;
75
}
76
77
static void dump_pagetable(unsigned long asce, unsigned long address)
78
{
79
unsigned long entry, *table = __va(asce & _ASCE_ORIGIN);
80
81
pr_alert("AS:%016lx ", asce);
82
switch (asce & _ASCE_TYPE_MASK) {
83
case _ASCE_TYPE_REGION1:
84
table += (address & _REGION1_INDEX) >> _REGION1_SHIFT;
85
if (get_kernel_nofault(entry, table))
86
goto bad;
87
pr_cont("R1:%016lx ", entry);
88
if (entry & _REGION_ENTRY_INVALID)
89
goto out;
90
table = __va(entry & _REGION_ENTRY_ORIGIN);
91
fallthrough;
92
case _ASCE_TYPE_REGION2:
93
table += (address & _REGION2_INDEX) >> _REGION2_SHIFT;
94
if (get_kernel_nofault(entry, table))
95
goto bad;
96
pr_cont("R2:%016lx ", entry);
97
if (entry & _REGION_ENTRY_INVALID)
98
goto out;
99
table = __va(entry & _REGION_ENTRY_ORIGIN);
100
fallthrough;
101
case _ASCE_TYPE_REGION3:
102
table += (address & _REGION3_INDEX) >> _REGION3_SHIFT;
103
if (get_kernel_nofault(entry, table))
104
goto bad;
105
pr_cont("R3:%016lx ", entry);
106
if (entry & (_REGION_ENTRY_INVALID | _REGION3_ENTRY_LARGE))
107
goto out;
108
table = __va(entry & _REGION_ENTRY_ORIGIN);
109
fallthrough;
110
case _ASCE_TYPE_SEGMENT:
111
table += (address & _SEGMENT_INDEX) >> _SEGMENT_SHIFT;
112
if (get_kernel_nofault(entry, table))
113
goto bad;
114
pr_cont("S:%016lx ", entry);
115
if (entry & (_SEGMENT_ENTRY_INVALID | _SEGMENT_ENTRY_LARGE))
116
goto out;
117
table = __va(entry & _SEGMENT_ENTRY_ORIGIN);
118
}
119
table += (address & _PAGE_INDEX) >> PAGE_SHIFT;
120
if (get_kernel_nofault(entry, table))
121
goto bad;
122
pr_cont("P:%016lx ", entry);
123
out:
124
pr_cont("\n");
125
return;
126
bad:
127
pr_cont("BAD\n");
128
}
129
130
static void dump_fault_info(struct pt_regs *regs)
131
{
132
union teid teid = { .val = regs->int_parm_long };
133
unsigned long asce;
134
135
pr_alert("Failing address: %016lx TEID: %016lx",
136
get_fault_address(regs), teid.val);
137
if (test_facility(131))
138
pr_cont(" ESOP-2");
139
else if (machine_has_esop())
140
pr_cont(" ESOP-1");
141
else
142
pr_cont(" SOP");
143
if (test_facility(75))
144
pr_cont(" FSI");
145
pr_cont("\n");
146
pr_alert("Fault in ");
147
switch (teid.as) {
148
case PSW_BITS_AS_HOME:
149
pr_cont("home space ");
150
break;
151
case PSW_BITS_AS_SECONDARY:
152
pr_cont("secondary space ");
153
break;
154
case PSW_BITS_AS_ACCREG:
155
pr_cont("access register ");
156
break;
157
case PSW_BITS_AS_PRIMARY:
158
pr_cont("primary space ");
159
break;
160
}
161
pr_cont("mode while using ");
162
if (is_kernel_fault(regs)) {
163
asce = get_lowcore()->kernel_asce.val;
164
pr_cont("kernel ");
165
} else {
166
asce = get_lowcore()->user_asce.val;
167
pr_cont("user ");
168
}
169
pr_cont("ASCE.\n");
170
dump_pagetable(asce, get_fault_address(regs));
171
}
172
173
int show_unhandled_signals = 1;
174
175
static const struct ctl_table s390_fault_sysctl_table[] = {
176
{
177
.procname = "userprocess_debug",
178
.data = &show_unhandled_signals,
179
.maxlen = sizeof(int),
180
.mode = 0644,
181
.proc_handler = proc_dointvec,
182
},
183
};
184
185
static int __init init_s390_fault_sysctls(void)
186
{
187
register_sysctl_init("kernel", s390_fault_sysctl_table);
188
return 0;
189
}
190
arch_initcall(init_s390_fault_sysctls);
191
192
void report_user_fault(struct pt_regs *regs, long signr, int is_mm_fault)
193
{
194
static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL, DEFAULT_RATELIMIT_BURST);
195
196
if ((task_pid_nr(current) > 1) && !show_unhandled_signals)
197
return;
198
if (!unhandled_signal(current, signr))
199
return;
200
if (!__ratelimit(&rs))
201
return;
202
pr_alert("User process fault: interruption code %04x ilc:%d ",
203
regs->int_code & 0xffff, regs->int_code >> 17);
204
print_vma_addr(KERN_CONT "in ", regs->psw.addr);
205
pr_cont("\n");
206
if (is_mm_fault)
207
dump_fault_info(regs);
208
show_regs(regs);
209
}
210
211
static void do_sigsegv(struct pt_regs *regs, int si_code)
212
{
213
report_user_fault(regs, SIGSEGV, 1);
214
force_sig_fault(SIGSEGV, si_code, (void __user *)get_fault_address(regs));
215
}
216
217
static void handle_fault_error_nolock(struct pt_regs *regs, int si_code)
218
{
219
unsigned long address;
220
bool is_write;
221
222
if (user_mode(regs)) {
223
if (WARN_ON_ONCE(!si_code))
224
si_code = SEGV_MAPERR;
225
return do_sigsegv(regs, si_code);
226
}
227
if (fixup_exception(regs))
228
return;
229
if (is_kernel_fault(regs)) {
230
address = get_fault_address(regs);
231
is_write = fault_is_write(regs);
232
if (kfence_handle_page_fault(address, is_write, regs))
233
return;
234
pr_alert("Unable to handle kernel pointer dereference in virtual kernel address space\n");
235
} else {
236
pr_alert("Unable to handle kernel paging request in virtual user address space\n");
237
}
238
dump_fault_info(regs);
239
die(regs, "Oops");
240
}
241
242
static void handle_fault_error(struct pt_regs *regs, int si_code)
243
{
244
struct mm_struct *mm = current->mm;
245
246
mmap_read_unlock(mm);
247
handle_fault_error_nolock(regs, si_code);
248
}
249
250
static void do_sigbus(struct pt_regs *regs)
251
{
252
force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)get_fault_address(regs));
253
}
254
255
/*
256
* This routine handles page faults. It determines the address,
257
* and the problem, and then passes it off to one of the appropriate
258
* routines.
259
*
260
* interruption code (int_code):
261
* 04 Protection -> Write-Protection (suppression)
262
* 10 Segment translation -> Not present (nullification)
263
* 11 Page translation -> Not present (nullification)
264
* 3b Region third trans. -> Not present (nullification)
265
*/
266
static void do_exception(struct pt_regs *regs, int access)
267
{
268
struct vm_area_struct *vma;
269
unsigned long address;
270
struct mm_struct *mm;
271
unsigned int flags;
272
vm_fault_t fault;
273
bool is_write;
274
275
/*
276
* The instruction that caused the program check has
277
* been nullified. Don't signal single step via SIGTRAP.
278
*/
279
clear_thread_flag(TIF_PER_TRAP);
280
if (kprobe_page_fault(regs, 14))
281
return;
282
mm = current->mm;
283
address = get_fault_address(regs);
284
is_write = fault_is_write(regs);
285
if (is_kernel_fault(regs) || faulthandler_disabled() || !mm)
286
return handle_fault_error_nolock(regs, 0);
287
perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
288
flags = FAULT_FLAG_DEFAULT;
289
if (user_mode(regs))
290
flags |= FAULT_FLAG_USER;
291
if (is_write)
292
access = VM_WRITE;
293
if (access == VM_WRITE)
294
flags |= FAULT_FLAG_WRITE;
295
if (!(flags & FAULT_FLAG_USER))
296
goto lock_mmap;
297
vma = lock_vma_under_rcu(mm, address);
298
if (!vma)
299
goto lock_mmap;
300
if (!(vma->vm_flags & access)) {
301
vma_end_read(vma);
302
count_vm_vma_lock_event(VMA_LOCK_SUCCESS);
303
return handle_fault_error_nolock(regs, SEGV_ACCERR);
304
}
305
fault = handle_mm_fault(vma, address, flags | FAULT_FLAG_VMA_LOCK, regs);
306
if (!(fault & (VM_FAULT_RETRY | VM_FAULT_COMPLETED)))
307
vma_end_read(vma);
308
if (!(fault & VM_FAULT_RETRY)) {
309
count_vm_vma_lock_event(VMA_LOCK_SUCCESS);
310
goto done;
311
}
312
count_vm_vma_lock_event(VMA_LOCK_RETRY);
313
if (fault & VM_FAULT_MAJOR)
314
flags |= FAULT_FLAG_TRIED;
315
/* Quick path to respond to signals */
316
if (fault_signal_pending(fault, regs)) {
317
if (!user_mode(regs))
318
handle_fault_error_nolock(regs, 0);
319
return;
320
}
321
lock_mmap:
322
retry:
323
vma = lock_mm_and_find_vma(mm, address, regs);
324
if (!vma)
325
return handle_fault_error_nolock(regs, SEGV_MAPERR);
326
if (unlikely(!(vma->vm_flags & access)))
327
return handle_fault_error(regs, SEGV_ACCERR);
328
fault = handle_mm_fault(vma, address, flags, regs);
329
if (fault_signal_pending(fault, regs)) {
330
if (!user_mode(regs))
331
handle_fault_error_nolock(regs, 0);
332
return;
333
}
334
/* The fault is fully completed (including releasing mmap lock) */
335
if (fault & VM_FAULT_COMPLETED)
336
return;
337
if (fault & VM_FAULT_RETRY) {
338
flags |= FAULT_FLAG_TRIED;
339
goto retry;
340
}
341
mmap_read_unlock(mm);
342
done:
343
if (!(fault & VM_FAULT_ERROR))
344
return;
345
if (fault & VM_FAULT_OOM) {
346
if (!user_mode(regs))
347
handle_fault_error_nolock(regs, 0);
348
else
349
pagefault_out_of_memory();
350
} else if (fault & VM_FAULT_SIGSEGV) {
351
if (!user_mode(regs))
352
handle_fault_error_nolock(regs, 0);
353
else
354
do_sigsegv(regs, SEGV_MAPERR);
355
} else if (fault & (VM_FAULT_SIGBUS | VM_FAULT_HWPOISON |
356
VM_FAULT_HWPOISON_LARGE)) {
357
if (!user_mode(regs))
358
handle_fault_error_nolock(regs, 0);
359
else
360
do_sigbus(regs);
361
} else {
362
pr_emerg("Unexpected fault flags: %08x\n", fault);
363
BUG();
364
}
365
}
366
367
void do_protection_exception(struct pt_regs *regs)
368
{
369
union teid teid = { .val = regs->int_parm_long };
370
371
/*
372
* Protection exceptions are suppressing, decrement psw address.
373
* The exception to this rule are aborted transactions, for these
374
* the PSW already points to the correct location.
375
*/
376
if (!(regs->int_code & 0x200)) {
377
regs->psw.addr = __rewind_psw(regs->psw, regs->int_code >> 16);
378
set_pt_regs_flag(regs, PIF_PSW_ADDR_ADJUSTED);
379
}
380
/*
381
* If bit 61 if the TEID is not set, the remainder of the
382
* TEID is unpredictable. Special handling is required.
383
*/
384
if (unlikely(!teid.b61)) {
385
if (user_mode(regs)) {
386
dump_fault_info(regs);
387
die(regs, "Unexpected TEID");
388
}
389
/* Assume low-address protection in kernel mode. */
390
return handle_fault_error_nolock(regs, 0);
391
}
392
if (unlikely(cpu_has_nx() && teid.b56)) {
393
regs->int_parm_long = (teid.addr * PAGE_SIZE) | (regs->psw.addr & PAGE_MASK);
394
return handle_fault_error_nolock(regs, SEGV_ACCERR);
395
}
396
do_exception(regs, VM_WRITE);
397
}
398
NOKPROBE_SYMBOL(do_protection_exception);
399
400
void do_dat_exception(struct pt_regs *regs)
401
{
402
do_exception(regs, VM_ACCESS_FLAGS);
403
}
404
NOKPROBE_SYMBOL(do_dat_exception);
405
406
#if IS_ENABLED(CONFIG_PGSTE)
407
408
void do_secure_storage_access(struct pt_regs *regs)
409
{
410
union teid teid = { .val = regs->int_parm_long };
411
unsigned long addr = get_fault_address(regs);
412
struct vm_area_struct *vma;
413
struct folio_walk fw;
414
struct mm_struct *mm;
415
struct folio *folio;
416
int rc;
417
418
/*
419
* Bit 61 indicates if the address is valid, if it is not the
420
* kernel should be stopped or SIGSEGV should be sent to the
421
* process. Bit 61 is not reliable without the misc UV feature,
422
* therefore this needs to be checked too.
423
*/
424
if (uv_has_feature(BIT_UV_FEAT_MISC) && !teid.b61) {
425
/*
426
* When this happens, userspace did something that it
427
* was not supposed to do, e.g. branching into secure
428
* memory. Trigger a segmentation fault.
429
*/
430
if (user_mode(regs)) {
431
send_sig(SIGSEGV, current, 0);
432
return;
433
}
434
/*
435
* The kernel should never run into this case and
436
* there is no way out of this situation.
437
*/
438
panic("Unexpected PGM 0x3d with TEID bit 61=0");
439
}
440
if (is_kernel_fault(regs)) {
441
folio = phys_to_folio(addr);
442
if (unlikely(!folio_try_get(folio)))
443
return;
444
rc = arch_make_folio_accessible(folio);
445
folio_put(folio);
446
if (rc)
447
BUG();
448
} else {
449
if (faulthandler_disabled())
450
return handle_fault_error_nolock(regs, 0);
451
mm = current->mm;
452
mmap_read_lock(mm);
453
vma = find_vma(mm, addr);
454
if (!vma)
455
return handle_fault_error(regs, SEGV_MAPERR);
456
folio = folio_walk_start(&fw, vma, addr, 0);
457
if (!folio) {
458
mmap_read_unlock(mm);
459
return;
460
}
461
/* arch_make_folio_accessible() needs a raised refcount. */
462
folio_get(folio);
463
rc = arch_make_folio_accessible(folio);
464
folio_put(folio);
465
folio_walk_end(&fw, vma);
466
if (rc)
467
send_sig(SIGSEGV, current, 0);
468
mmap_read_unlock(mm);
469
}
470
}
471
NOKPROBE_SYMBOL(do_secure_storage_access);
472
473
#endif /* CONFIG_PGSTE */
474
475