linux/arch/unicore32/mm/fault.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * linux/arch/unicore32/mm/fault.c
   4 *
   5 * Code specific to PKUnity SoC and UniCore ISA
   6 *
   7 * Copyright (C) 2001-2010 GUAN Xue-tao
   8 */
   9#include <linux/extable.h>
  10#include <linux/signal.h>
  11#include <linux/mm.h>
  12#include <linux/hardirq.h>
  13#include <linux/init.h>
  14#include <linux/kprobes.h>
  15#include <linux/uaccess.h>
  16#include <linux/page-flags.h>
  17#include <linux/sched/signal.h>
  18#include <linux/io.h>
  19
  20#include <asm/pgtable.h>
  21#include <asm/tlbflush.h>
  22
  23/*
  24 * Fault status register encodings.  We steal bit 31 for our own purposes.
  25 */
  26#define FSR_LNX_PF              (1 << 31)
  27
  28static inline int fsr_fs(unsigned int fsr)
  29{
  30        /* xyabcde will be abcde+xy */
  31        return (fsr & 31) + ((fsr & (3 << 5)) >> 5);
  32}
  33
  34/*
  35 * This is useful to dump out the page tables associated with
  36 * 'addr' in mm 'mm'.
  37 */
  38void show_pte(struct mm_struct *mm, unsigned long addr)
  39{
  40        pgd_t *pgd;
  41
  42        if (!mm)
  43                mm = &init_mm;
  44
  45        printk(KERN_ALERT "pgd = %p\n", mm->pgd);
  46        pgd = pgd_offset(mm, addr);
  47        printk(KERN_ALERT "[%08lx] *pgd=%08lx", addr, pgd_val(*pgd));
  48
  49        do {
  50                pmd_t *pmd;
  51                pte_t *pte;
  52
  53                if (pgd_none(*pgd))
  54                        break;
  55
  56                if (pgd_bad(*pgd)) {
  57                        printk("(bad)");
  58                        break;
  59                }
  60
  61                pmd = pmd_offset((pud_t *) pgd, addr);
  62                if (PTRS_PER_PMD != 1)
  63                        printk(", *pmd=%08lx", pmd_val(*pmd));
  64
  65                if (pmd_none(*pmd))
  66                        break;
  67
  68                if (pmd_bad(*pmd)) {
  69                        printk("(bad)");
  70                        break;
  71                }
  72
  73                /* We must not map this if we have highmem enabled */
  74                if (PageHighMem(pfn_to_page(pmd_val(*pmd) >> PAGE_SHIFT)))
  75                        break;
  76
  77                pte = pte_offset_map(pmd, addr);
  78                printk(", *pte=%08lx", pte_val(*pte));
  79                pte_unmap(pte);
  80        } while (0);
  81
  82        printk("\n");
  83}
  84
  85/*
  86 * Oops.  The kernel tried to access some page that wasn't present.
  87 */
  88static void __do_kernel_fault(struct mm_struct *mm, unsigned long addr,
  89                unsigned int fsr, struct pt_regs *regs)
  90{
  91        /*
  92         * Are we prepared to handle this kernel fault?
  93         */
  94        if (fixup_exception(regs))
  95                return;
  96
  97        /*
  98         * No handler, we'll have to terminate things with extreme prejudice.
  99         */
 100        bust_spinlocks(1);
 101        printk(KERN_ALERT
 102               "Unable to handle kernel %s at virtual address %08lx\n",
 103               (addr < PAGE_SIZE) ? "NULL pointer dereference" :
 104               "paging request", addr);
 105
 106        show_pte(mm, addr);
 107        die("Oops", regs, fsr);
 108        bust_spinlocks(0);
 109        do_exit(SIGKILL);
 110}
 111
 112/*
 113 * Something tried to access memory that isn't in our memory map..
 114 * User mode accesses just cause a SIGSEGV
 115 */
 116static void __do_user_fault(unsigned long addr, unsigned int fsr,
 117                            unsigned int sig, int code, struct pt_regs *regs)
 118{
 119        struct task_struct *tsk = current;
 120
 121        tsk->thread.address = addr;
 122        tsk->thread.error_code = fsr;
 123        tsk->thread.trap_no = 14;
 124        force_sig_fault(sig, code, (void __user *)addr);
 125}
 126
 127void do_bad_area(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
 128{
 129        struct task_struct *tsk = current;
 130        struct mm_struct *mm = tsk->active_mm;
 131
 132        /*
 133         * If we are in kernel mode at this point, we
 134         * have no context to handle this fault with.
 135         */
 136        if (user_mode(regs))
 137                __do_user_fault(addr, fsr, SIGSEGV, SEGV_MAPERR, regs);
 138        else
 139                __do_kernel_fault(mm, addr, fsr, regs);
 140}
 141
 142#define VM_FAULT_BADMAP         0x010000
 143#define VM_FAULT_BADACCESS      0x020000
 144
 145/*
 146 * Check that the permissions on the VMA allow for the fault which occurred.
 147 * If we encountered a write fault, we must have write permission, otherwise
 148 * we allow any permission.
 149 */
 150static inline bool access_error(unsigned int fsr, struct vm_area_struct *vma)
 151{
 152        unsigned int mask = VM_ACCESS_FLAGS;
 153
 154        if (!(fsr ^ 0x12))      /* write? */
 155                mask = VM_WRITE;
 156        if (fsr & FSR_LNX_PF)
 157                mask = VM_EXEC;
 158
 159        return vma->vm_flags & mask ? false : true;
 160}
 161
 162static vm_fault_t __do_pf(struct mm_struct *mm, unsigned long addr,
 163                unsigned int fsr, unsigned int flags, struct task_struct *tsk)
 164{
 165        struct vm_area_struct *vma;
 166        vm_fault_t fault;
 167
 168        vma = find_vma(mm, addr);
 169        fault = VM_FAULT_BADMAP;
 170        if (unlikely(!vma))
 171                goto out;
 172        if (unlikely(vma->vm_start > addr))
 173                goto check_stack;
 174
 175        /*
 176         * Ok, we have a good vm_area for this
 177         * memory access, so we can handle it.
 178         */
 179good_area:
 180        if (access_error(fsr, vma)) {
 181                fault = VM_FAULT_BADACCESS;
 182                goto out;
 183        }
 184
 185        /*
 186         * If for any reason at all we couldn't handle the fault, make
 187         * sure we exit gracefully rather than endlessly redo the fault.
 188         */
 189        fault = handle_mm_fault(vma, addr & PAGE_MASK, flags);
 190        return fault;
 191
 192check_stack:
 193        if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr))
 194                goto good_area;
 195out:
 196        return fault;
 197}
 198
 199static int do_pf(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
 200{
 201        struct task_struct *tsk;
 202        struct mm_struct *mm;
 203        int sig, code;
 204        vm_fault_t fault;
 205        unsigned int flags = FAULT_FLAG_DEFAULT;
 206
 207        tsk = current;
 208        mm = tsk->mm;
 209
 210        /*
 211         * If we're in an interrupt or have no user
 212         * context, we must not take the fault..
 213         */
 214        if (faulthandler_disabled() || !mm)
 215                goto no_context;
 216
 217        if (user_mode(regs))
 218                flags |= FAULT_FLAG_USER;
 219        if (!(fsr ^ 0x12))
 220                flags |= FAULT_FLAG_WRITE;
 221
 222        /*
 223         * As per x86, we may deadlock here.  However, since the kernel only
 224         * validly references user space from well defined areas of the code,
 225         * we can bug out early if this is from code which shouldn't.
 226         */
 227        if (!down_read_trylock(&mm->mmap_sem)) {
 228                if (!user_mode(regs)
 229                    && !search_exception_tables(regs->UCreg_pc))
 230                        goto no_context;
 231retry:
 232                down_read(&mm->mmap_sem);
 233        } else {
 234                /*
 235                 * The above down_read_trylock() might have succeeded in
 236                 * which case, we'll have missed the might_sleep() from
 237                 * down_read()
 238                 */
 239                might_sleep();
 240#ifdef CONFIG_DEBUG_VM
 241                if (!user_mode(regs) &&
 242                    !search_exception_tables(regs->UCreg_pc))
 243                        goto no_context;
 244#endif
 245        }
 246
 247        fault = __do_pf(mm, addr, fsr, flags, tsk);
 248
 249        /* If we need to retry but a fatal signal is pending, handle the
 250         * signal first. We do not need to release the mmap_sem because
 251         * it would already be released in __lock_page_or_retry in
 252         * mm/filemap.c. */
 253        if (fault_signal_pending(fault, regs))
 254                return 0;
 255
 256        if (!(fault & VM_FAULT_ERROR) && (flags & FAULT_FLAG_ALLOW_RETRY)) {
 257                if (fault & VM_FAULT_MAJOR)
 258                        tsk->maj_flt++;
 259                else
 260                        tsk->min_flt++;
 261                if (fault & VM_FAULT_RETRY) {
 262                        flags |= FAULT_FLAG_TRIED;
 263                        goto retry;
 264                }
 265        }
 266
 267        up_read(&mm->mmap_sem);
 268
 269        /*
 270         * Handle the "normal" case first - VM_FAULT_MAJOR
 271         */
 272        if (likely(!(fault &
 273               (VM_FAULT_ERROR | VM_FAULT_BADMAP | VM_FAULT_BADACCESS))))
 274                return 0;
 275
 276        /*
 277         * If we are in kernel mode at this point, we
 278         * have no context to handle this fault with.
 279         */
 280        if (!user_mode(regs))
 281                goto no_context;
 282
 283        if (fault & VM_FAULT_OOM) {
 284                /*
 285                 * We ran out of memory, call the OOM killer, and return to
 286                 * userspace (which will retry the fault, or kill us if we
 287                 * got oom-killed)
 288                 */
 289                pagefault_out_of_memory();
 290                return 0;
 291        }
 292
 293        if (fault & VM_FAULT_SIGBUS) {
 294                /*
 295                 * We had some memory, but were unable to
 296                 * successfully fix up this page fault.
 297                 */
 298                sig = SIGBUS;
 299                code = BUS_ADRERR;
 300        } else {
 301                /*
 302                 * Something tried to access memory that
 303                 * isn't in our memory map..
 304                 */
 305                sig = SIGSEGV;
 306                code = fault == VM_FAULT_BADACCESS ? SEGV_ACCERR : SEGV_MAPERR;
 307        }
 308
 309        __do_user_fault(addr, fsr, sig, code, regs);
 310        return 0;
 311
 312no_context:
 313        __do_kernel_fault(mm, addr, fsr, regs);
 314        return 0;
 315}
 316
 317/*
 318 * First Level Translation Fault Handler
 319 *
 320 * We enter here because the first level page table doesn't contain
 321 * a valid entry for the address.
 322 *
 323 * If the address is in kernel space (>= TASK_SIZE), then we are
 324 * probably faulting in the vmalloc() area.
 325 *
 326 * If the init_task's first level page tables contains the relevant
 327 * entry, we copy the it to this task.  If not, we send the process
 328 * a signal, fixup the exception, or oops the kernel.
 329 *
 330 * NOTE! We MUST NOT take any locks for this case. We may be in an
 331 * interrupt or a critical region, and should only copy the information
 332 * from the master page table, nothing more.
 333 */
 334static int do_ifault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
 335{
 336        unsigned int index;
 337        pgd_t *pgd, *pgd_k;
 338        pmd_t *pmd, *pmd_k;
 339
 340        if (addr < TASK_SIZE)
 341                return do_pf(addr, fsr, regs);
 342
 343        if (user_mode(regs))
 344                goto bad_area;
 345
 346        index = pgd_index(addr);
 347
 348        pgd = cpu_get_pgd() + index;
 349        pgd_k = init_mm.pgd + index;
 350
 351        if (pgd_none(*pgd_k))
 352                goto bad_area;
 353
 354        pmd_k = pmd_offset((pud_t *) pgd_k, addr);
 355        pmd = pmd_offset((pud_t *) pgd, addr);
 356
 357        if (pmd_none(*pmd_k))
 358                goto bad_area;
 359
 360        set_pmd(pmd, *pmd_k);
 361        flush_pmd_entry(pmd);
 362        return 0;
 363
 364bad_area:
 365        do_bad_area(addr, fsr, regs);
 366        return 0;
 367}
 368
 369/*
 370 * This abort handler always returns "fault".
 371 */
 372static int do_bad(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
 373{
 374        return 1;
 375}
 376
 377static int do_good(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
 378{
 379        unsigned int res1, res2;
 380
 381        printk("dabt exception but no error!\n");
 382
 383        __asm__ __volatile__(
 384                        "mff %0,f0\n"
 385                        "mff %1,f1\n"
 386                        : "=r"(res1), "=r"(res2)
 387                        :
 388                        : "memory");
 389
 390        printk(KERN_EMERG "r0 :%08x  r1 :%08x\n", res1, res2);
 391        panic("shut up\n");
 392        return 0;
 393}
 394
 395static struct fsr_info {
 396        int (*fn) (unsigned long addr, unsigned int fsr, struct pt_regs *regs);
 397        int sig;
 398        int code;
 399        const char *name;
 400} fsr_info[] = {
 401        /*
 402         * The following are the standard Unicore-I and UniCore-II aborts.
 403         */
 404        { do_good,      SIGBUS,  0,             "no error"              },
 405        { do_bad,       SIGBUS,  BUS_ADRALN,    "alignment exception"   },
 406        { do_bad,       SIGBUS,  BUS_OBJERR,    "external exception"    },
 407        { do_bad,       SIGBUS,  0,             "burst operation"       },
 408        { do_bad,       SIGBUS,  0,             "unknown 00100"         },
 409        { do_ifault,    SIGSEGV, SEGV_MAPERR,   "2nd level pt non-exist"},
 410        { do_bad,       SIGBUS,  0,             "2nd lvl large pt non-exist" },
 411        { do_bad,       SIGBUS,  0,             "invalid pte"           },
 412        { do_pf,        SIGSEGV, SEGV_MAPERR,   "page miss"             },
 413        { do_bad,       SIGBUS,  0,             "middle page miss"      },
 414        { do_bad,       SIGBUS,  0,             "large page miss"       },
 415        { do_pf,        SIGSEGV, SEGV_MAPERR,   "super page (section) miss" },
 416        { do_bad,       SIGBUS,  0,             "unknown 01100"         },
 417        { do_bad,       SIGBUS,  0,             "unknown 01101"         },
 418        { do_bad,       SIGBUS,  0,             "unknown 01110"         },
 419        { do_bad,       SIGBUS,  0,             "unknown 01111"         },
 420        { do_bad,       SIGBUS,  0,             "addr: up 3G or IO"     },
 421        { do_pf,        SIGSEGV, SEGV_ACCERR,   "read unreadable addr"  },
 422        { do_pf,        SIGSEGV, SEGV_ACCERR,   "write unwriteable addr"},
 423        { do_pf,        SIGSEGV, SEGV_ACCERR,   "exec unexecutable addr"},
 424        { do_bad,       SIGBUS,  0,             "unknown 10100"         },
 425        { do_bad,       SIGBUS,  0,             "unknown 10101"         },
 426        { do_bad,       SIGBUS,  0,             "unknown 10110"         },
 427        { do_bad,       SIGBUS,  0,             "unknown 10111"         },
 428        { do_bad,       SIGBUS,  0,             "unknown 11000"         },
 429        { do_bad,       SIGBUS,  0,             "unknown 11001"         },
 430        { do_bad,       SIGBUS,  0,             "unknown 11010"         },
 431        { do_bad,       SIGBUS,  0,             "unknown 11011"         },
 432        { do_bad,       SIGBUS,  0,             "unknown 11100"         },
 433        { do_bad,       SIGBUS,  0,             "unknown 11101"         },
 434        { do_bad,       SIGBUS,  0,             "unknown 11110"         },
 435        { do_bad,       SIGBUS,  0,             "unknown 11111"         }
 436};
 437
 438void __init hook_fault_code(int nr,
 439                int (*fn) (unsigned long, unsigned int, struct pt_regs *),
 440                int sig, int code, const char *name)
 441{
 442        if (nr < 0 || nr >= ARRAY_SIZE(fsr_info))
 443                BUG();
 444
 445        fsr_info[nr].fn   = fn;
 446        fsr_info[nr].sig  = sig;
 447        fsr_info[nr].code = code;
 448        fsr_info[nr].name = name;
 449}
 450
 451/*
 452 * Dispatch a data abort to the relevant handler.
 453 */
 454asmlinkage void do_DataAbort(unsigned long addr, unsigned int fsr,
 455                        struct pt_regs *regs)
 456{
 457        const struct fsr_info *inf = fsr_info + fsr_fs(fsr);
 458
 459        if (!inf->fn(addr, fsr & ~FSR_LNX_PF, regs))
 460                return;
 461
 462        printk(KERN_ALERT "Unhandled fault: %s (0x%03x) at 0x%08lx\n",
 463               inf->name, fsr, addr);
 464
 465        uc32_notify_die("", regs, inf->sig, inf->code, (void __user *)addr,
 466                        fsr, 0);
 467}
 468
 469asmlinkage void do_PrefetchAbort(unsigned long addr,
 470                        unsigned int ifsr, struct pt_regs *regs)
 471{
 472        const struct fsr_info *inf = fsr_info + fsr_fs(ifsr);
 473
 474        if (!inf->fn(addr, ifsr | FSR_LNX_PF, regs))
 475                return;
 476
 477        printk(KERN_ALERT "Unhandled prefetch abort: %s (0x%03x) at 0x%08lx\n",
 478               inf->name, ifsr, addr);
 479
 480        uc32_notify_die("", regs, inf->sig, inf->code, (void __user *)addr,
 481                        ifsr, 0);
 482}
 483