forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-11 6778948f9de86c3cfaf36725a7c87dcff9ba247f
kernel/arch/parisc/mm/fault.c
....@@ -18,6 +18,7 @@
1818 #include <linux/extable.h>
1919 #include <linux/uaccess.h>
2020 #include <linux/hugetlb.h>
21
+#include <linux/perf_event.h>
2122
2223 #include <asm/traps.h>
2324
....@@ -66,6 +67,7 @@
6667 case 0x30000000: /* coproc2 */
6768 if (bit22set(inst))
6869 return VM_WRITE;
70
+ fallthrough;
6971
7072 case 0x0: /* indexed/memory management */
7173 if (bit22set(inst)) {
....@@ -273,15 +275,16 @@
273275 if (!mm)
274276 goto no_context;
275277
276
- flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
278
+ flags = FAULT_FLAG_DEFAULT;
277279 if (user_mode(regs))
278280 flags |= FAULT_FLAG_USER;
279281
280282 acc_type = parisc_acctyp(code, regs->iir);
281283 if (acc_type & VM_WRITE)
282284 flags |= FAULT_FLAG_WRITE;
285
+ perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
283286 retry:
284
- down_read(&mm->mmap_sem);
287
+ mmap_read_lock(mm);
285288 vma = find_vma_prev(mm, address, &prev_vma);
286289 if (!vma || address < vma->vm_start)
287290 goto check_expansion;
....@@ -301,9 +304,9 @@
301304 * fault.
302305 */
303306
304
- fault = handle_mm_fault(vma, address, flags);
307
+ fault = handle_mm_fault(vma, address, flags, regs);
305308
306
- if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
309
+ if (fault_signal_pending(fault, regs))
307310 return;
308311
309312 if (unlikely(fault & VM_FAULT_ERROR)) {
....@@ -322,23 +325,17 @@
322325 BUG();
323326 }
324327 if (flags & FAULT_FLAG_ALLOW_RETRY) {
325
- if (fault & VM_FAULT_MAJOR)
326
- current->maj_flt++;
327
- else
328
- current->min_flt++;
329328 if (fault & VM_FAULT_RETRY) {
330
- flags &= ~FAULT_FLAG_ALLOW_RETRY;
331
-
332329 /*
333
- * No need to up_read(&mm->mmap_sem) as we would
330
+ * No need to mmap_read_unlock(mm) as we would
334331 * have already released it in __lock_page_or_retry
335332 * in mm/filemap.c.
336333 */
337
-
334
+ flags |= FAULT_FLAG_TRIED;
338335 goto retry;
339336 }
340337 }
341
- up_read(&mm->mmap_sem);
338
+ mmap_read_unlock(mm);
342339 return;
343340
344341 check_expansion:
....@@ -350,7 +347,7 @@
350347 * Something tried to access memory that isn't in our memory map..
351348 */
352349 bad_area:
353
- up_read(&mm->mmap_sem);
350
+ mmap_read_unlock(mm);
354351
355352 if (user_mode(regs)) {
356353 int signo, si_code;
....@@ -373,7 +370,7 @@
373370 }
374371
375372 /* probably address is outside of mapped file */
376
- /* fall through */
373
+ fallthrough;
377374 case 17: /* NA data TLB miss / page fault */
378375 case 18: /* Unaligned access - PCXS only */
379376 signo = SIGBUS;
....@@ -403,13 +400,13 @@
403400 lsb = PAGE_SHIFT;
404401
405402 force_sig_mceerr(BUS_MCEERR_AR, (void __user *) address,
406
- lsb, current);
403
+ lsb);
407404 return;
408405 }
409406 #endif
410407 show_signal_msg(regs, code, address, tsk, vma);
411408
412
- force_sig_fault(signo, si_code, (void __user *) address, current);
409
+ force_sig_fault(signo, si_code, (void __user *) address);
413410 return;
414411 }
415412
....@@ -422,8 +419,97 @@
422419 parisc_terminate("Bad Address (null pointer deref?)", regs, code, address);
423420
424421 out_of_memory:
425
- up_read(&mm->mmap_sem);
422
+ mmap_read_unlock(mm);
426423 if (!user_mode(regs))
427424 goto no_context;
428425 pagefault_out_of_memory();
429426 }
427
+
428
+/* Handle non-access data TLB miss faults.
429
+ *
430
+ * For probe instructions, accesses to userspace are considered allowed
431
+ * if they lie in a valid VMA and the access type matches. We are not
432
+ * allowed to handle MM faults here so there may be situations where an
433
+ * actual access would fail even though a probe was successful.
434
+ */
435
+int
436
+handle_nadtlb_fault(struct pt_regs *regs)
437
+{
438
+ unsigned long insn = regs->iir;
439
+ int breg, treg, xreg, val = 0;
440
+ struct vm_area_struct *vma, *prev_vma;
441
+ struct task_struct *tsk;
442
+ struct mm_struct *mm;
443
+ unsigned long address;
444
+ unsigned long acc_type;
445
+
446
+ switch (insn & 0x380) {
447
+ case 0x280:
448
+ /* FDC instruction */
449
+ fallthrough;
450
+ case 0x380:
451
+ /* PDC and FIC instructions */
452
+ if (printk_ratelimit()) {
453
+ pr_warn("BUG: nullifying cache flush/purge instruction\n");
454
+ show_regs(regs);
455
+ }
456
+ if (insn & 0x20) {
457
+ /* Base modification */
458
+ breg = (insn >> 21) & 0x1f;
459
+ xreg = (insn >> 16) & 0x1f;
460
+ if (breg && xreg)
461
+ regs->gr[breg] += regs->gr[xreg];
462
+ }
463
+ regs->gr[0] |= PSW_N;
464
+ return 1;
465
+
466
+ case 0x180:
467
+ /* PROBE instruction */
468
+ treg = insn & 0x1f;
469
+ if (regs->isr) {
470
+ tsk = current;
471
+ mm = tsk->mm;
472
+ if (mm) {
473
+ /* Search for VMA */
474
+ address = regs->ior;
475
+ mmap_read_lock(mm);
476
+ vma = find_vma_prev(mm, address, &prev_vma);
477
+ mmap_read_unlock(mm);
478
+
479
+ /*
480
+ * Check if access to the VMA is okay.
481
+ * We don't allow for stack expansion.
482
+ */
483
+ acc_type = (insn & 0x40) ? VM_WRITE : VM_READ;
484
+ if (vma
485
+ && address >= vma->vm_start
486
+ && (vma->vm_flags & acc_type) == acc_type)
487
+ val = 1;
488
+ }
489
+ }
490
+ if (treg)
491
+ regs->gr[treg] = val;
492
+ regs->gr[0] |= PSW_N;
493
+ return 1;
494
+
495
+ case 0x300:
496
+ /* LPA instruction */
497
+ if (insn & 0x20) {
498
+ /* Base modification */
499
+ breg = (insn >> 21) & 0x1f;
500
+ xreg = (insn >> 16) & 0x1f;
501
+ if (breg && xreg)
502
+ regs->gr[breg] += regs->gr[xreg];
503
+ }
504
+ treg = insn & 0x1f;
505
+ if (treg)
506
+ regs->gr[treg] = 0;
507
+ regs->gr[0] |= PSW_N;
508
+ return 1;
509
+
510
+ default:
511
+ break;
512
+ }
513
+
514
+ return 0;
515
+}