linux/arch/powerpc/kernel/exceptions-64s.S
<<
>>
Prefs
   1/* SPDX-License-Identifier: GPL-2.0 */
   2/*
   3 * This file contains the 64-bit "server" PowerPC variant
   4 * of the low level exception handling including exception
   5 * vectors, exception return, part of the slb and stab
   6 * handling and other fixed offset specific things.
   7 *
   8 * This file is meant to be #included from head_64.S due to
   9 * position dependent assembly.
  10 *
  11 * Most of this originates from head_64.S and thus has the same
  12 * copyright history.
  13 *
  14 */
  15
  16#include <asm/hw_irq.h>
  17#include <asm/exception-64s.h>
  18#include <asm/ptrace.h>
  19#include <asm/cpuidle.h>
  20#include <asm/head-64.h>
  21
  22/*
  23 * There are a few constraints to be concerned with.
  24 * - Real mode exceptions code/data must be located at their physical location.
  25 * - Virtual mode exceptions must be mapped at their 0xc000... location.
  26 * - Fixed location code must not call directly beyond the __end_interrupts
  27 *   area when built with CONFIG_RELOCATABLE. LOAD_HANDLER / bctr sequence
  28 *   must be used.
  29 * - LOAD_HANDLER targets must be within first 64K of physical 0 /
  30 *   virtual 0xc00...
  31 * - Conditional branch targets must be within +/-32K of caller.
  32 *
  33 * "Virtual exceptions" run with relocation on (MSR_IR=1, MSR_DR=1), and
  34 * therefore don't have to run in physically located code or rfid to
  35 * virtual mode kernel code. However on relocatable kernels they do have
  36 * to branch to KERNELBASE offset because the rest of the kernel (outside
  37 * the exception vectors) may be located elsewhere.
  38 *
  39 * Virtual exceptions correspond with physical, except their entry points
  40 * are offset by 0xc000000000000000 and also tend to get an added 0x4000
  41 * offset applied. Virtual exceptions are enabled with the Alternate
  42 * Interrupt Location (AIL) bit set in the LPCR. However this does not
  43 * guarantee they will be delivered virtually. Some conditions (see the ISA)
  44 * cause exceptions to be delivered in real mode.
  45 *
  46 * It's impossible to receive interrupts below 0x300 via AIL.
  47 *
  48 * KVM: None of the virtual exceptions are from the guest. Anything that
  49 * escalated to HV=1 from HV=0 is delivered via real mode handlers.
  50 *
  51 *
  52 * We layout physical memory as follows:
  53 * 0x0000 - 0x00ff : Secondary processor spin code
  54 * 0x0100 - 0x18ff : Real mode pSeries interrupt vectors
  55 * 0x1900 - 0x3fff : Real mode trampolines
  56 * 0x4000 - 0x58ff : Relon (IR=1,DR=1) mode pSeries interrupt vectors
  57 * 0x5900 - 0x6fff : Relon mode trampolines
  58 * 0x7000 - 0x7fff : FWNMI data area
  59 * 0x8000 -   .... : Common interrupt handlers, remaining early
  60 *                   setup code, rest of kernel.
  61 *
  62 * We could reclaim 0x4000-0x42ff for real mode trampolines if the space
  63 * is necessary. Until then it's more consistent to explicitly put VIRT_NONE
  64 * vectors there.
  65 */
  66OPEN_FIXED_SECTION(real_vectors,        0x0100, 0x1900)
  67OPEN_FIXED_SECTION(real_trampolines,    0x1900, 0x4000)
  68OPEN_FIXED_SECTION(virt_vectors,        0x4000, 0x5900)
  69OPEN_FIXED_SECTION(virt_trampolines,    0x5900, 0x7000)
  70#if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_POWERNV)
  71/*
  72 * Data area reserved for FWNMI option.
  73 * This address (0x7000) is fixed by the RPA.
  74 * pseries and powernv need to keep the whole page from
  75 * 0x7000 to 0x8000 free for use by the firmware
  76 */
  77ZERO_FIXED_SECTION(fwnmi_page,          0x7000, 0x8000)
  78OPEN_TEXT_SECTION(0x8000)
  79#else
  80OPEN_TEXT_SECTION(0x7000)
  81#endif
  82
  83USE_FIXED_SECTION(real_vectors)
  84
  85/*
  86 * This is the start of the interrupt handlers for pSeries
  87 * This code runs with relocation off.
  88 * Code from here to __end_interrupts gets copied down to real
  89 * address 0x100 when we are running a relocatable kernel.
  90 * Therefore any relative branches in this section must only
  91 * branch to labels in this section.
  92 */
  93        .globl __start_interrupts
  94__start_interrupts:
  95
  96/* No virt vectors corresponding with 0x0..0x100 */
  97EXC_VIRT_NONE(0x4000, 0x100)
  98
  99
 100#ifdef CONFIG_PPC_P7_NAP
 101        /*
 102         * If running native on arch 2.06 or later, check if we are waking up
 103         * from nap/sleep/winkle, and branch to idle handler. This tests SRR1
 104         * bits 46:47. A non-0 value indicates that we are coming from a power
 105         * saving state. The idle wakeup handler initially runs in real mode,
 106         * but we branch to the 0xc000... address so we can turn on relocation
 107         * with mtmsr.
 108         */
 109#define IDLETEST(n)                                                     \
 110        BEGIN_FTR_SECTION ;                                             \
 111        mfspr   r10,SPRN_SRR1 ;                                         \
 112        rlwinm. r10,r10,47-31,30,31 ;                                   \
 113        beq-    1f ;                                                    \
 114        cmpwi   cr1,r10,2 ;                                             \
 115        mfspr   r3,SPRN_SRR1 ;                                          \
 116        bltlr   cr1 ;   /* no state loss, return to idle caller */      \
 117        BRANCH_TO_C000(r10, system_reset_idle_common) ;                 \
 1181:                                                                      \
 119        KVMTEST_PR(n) ;                                                 \
 120        END_FTR_SECTION_IFSET(CPU_FTR_HVMODE | CPU_FTR_ARCH_206)
 121#else
 122#define IDLETEST NOTEST
 123#endif
 124
 125EXC_REAL_BEGIN(system_reset, 0x100, 0x100)
 126        SET_SCRATCH0(r13)
 127        /*
 128         * MSR_RI is not enabled, because PACA_EXNMI and nmi stack is
 129         * being used, so a nested NMI exception would corrupt it.
 130         */
 131        EXCEPTION_PROLOG_PSERIES_NORI(PACA_EXNMI, system_reset_common, EXC_STD,
 132                                 IDLETEST, 0x100)
 133
 134EXC_REAL_END(system_reset, 0x100, 0x100)
 135EXC_VIRT_NONE(0x4100, 0x100)
 136TRAMP_KVM(PACA_EXNMI, 0x100)
 137
 138#ifdef CONFIG_PPC_P7_NAP
 139EXC_COMMON_BEGIN(system_reset_idle_common)
 140        /*
 141         * This must be a direct branch (without linker branch stub) because
 142         * we can not use TOC at this point as r2 may not be restored yet.
 143         */
 144        b       idle_return_gpr_loss
 145#endif
 146
 147/*
 148 * Set IRQS_ALL_DISABLED unconditionally so arch_irqs_disabled does
 149 * the right thing. We do not want to reconcile because that goes
 150 * through irq tracing which we don't want in NMI.
 151 *
 152 * Save PACAIRQHAPPENED because some code will do a hard disable
 153 * (e.g., xmon). So we want to restore this back to where it was
 154 * when we return. DAR is unused in the stack, so save it there.
 155 */
 156#define ADD_RECONCILE_NMI                                               \
 157        li      r10,IRQS_ALL_DISABLED;                                  \
 158        stb     r10,PACAIRQSOFTMASK(r13);                               \
 159        lbz     r10,PACAIRQHAPPENED(r13);                               \
 160        std     r10,_DAR(r1)
 161
 162EXC_COMMON_BEGIN(system_reset_common)
 163        /*
 164         * Increment paca->in_nmi then enable MSR_RI. SLB or MCE will be able
 165         * to recover, but nested NMI will notice in_nmi and not recover
 166         * because of the use of the NMI stack. in_nmi reentrancy is tested in
 167         * system_reset_exception.
 168         */
 169        lhz     r10,PACA_IN_NMI(r13)
 170        addi    r10,r10,1
 171        sth     r10,PACA_IN_NMI(r13)
 172        li      r10,MSR_RI
 173        mtmsrd  r10,1
 174
 175        mr      r10,r1
 176        ld      r1,PACA_NMI_EMERG_SP(r13)
 177        subi    r1,r1,INT_FRAME_SIZE
 178        EXCEPTION_COMMON_NORET_STACK(PACA_EXNMI, 0x100,
 179                        system_reset, system_reset_exception,
 180                        ADD_NVGPRS;ADD_RECONCILE_NMI)
 181
 182        /* This (and MCE) can be simplified with mtmsrd L=1 */
 183        /* Clear MSR_RI before setting SRR0 and SRR1. */
 184        li      r0,MSR_RI
 185        mfmsr   r9
 186        andc    r9,r9,r0
 187        mtmsrd  r9,1
 188
 189        /*
 190         * MSR_RI is clear, now we can decrement paca->in_nmi.
 191         */
 192        lhz     r10,PACA_IN_NMI(r13)
 193        subi    r10,r10,1
 194        sth     r10,PACA_IN_NMI(r13)
 195
 196        /*
 197         * Restore soft mask settings.
 198         */
 199        ld      r10,_DAR(r1)
 200        stb     r10,PACAIRQHAPPENED(r13)
 201        ld      r10,SOFTE(r1)
 202        stb     r10,PACAIRQSOFTMASK(r13)
 203
 204        /*
 205         * Keep below code in synch with MACHINE_CHECK_HANDLER_WINDUP.
 206         * Should share common bits...
 207         */
 208
 209        /* Move original SRR0 and SRR1 into the respective regs */
 210        ld      r9,_MSR(r1)
 211        mtspr   SPRN_SRR1,r9
 212        ld      r3,_NIP(r1)
 213        mtspr   SPRN_SRR0,r3
 214        ld      r9,_CTR(r1)
 215        mtctr   r9
 216        ld      r9,_XER(r1)
 217        mtxer   r9
 218        ld      r9,_LINK(r1)
 219        mtlr    r9
 220        REST_GPR(0, r1)
 221        REST_8GPRS(2, r1)
 222        REST_GPR(10, r1)
 223        ld      r11,_CCR(r1)
 224        mtcr    r11
 225        REST_GPR(11, r1)
 226        REST_2GPRS(12, r1)
 227        /* restore original r1. */
 228        ld      r1,GPR1(r1)
 229        RFI_TO_USER_OR_KERNEL
 230
 231#ifdef CONFIG_PPC_PSERIES
 232/*
 233 * Vectors for the FWNMI option.  Share common code.
 234 */
 235TRAMP_REAL_BEGIN(system_reset_fwnmi)
 236        SET_SCRATCH0(r13)               /* save r13 */
 237        /* See comment at system_reset exception */
 238        EXCEPTION_PROLOG_PSERIES_NORI(PACA_EXNMI, system_reset_common,
 239                                                EXC_STD, NOTEST, 0x100)
 240#endif /* CONFIG_PPC_PSERIES */
 241
 242
 243EXC_REAL_BEGIN(machine_check, 0x200, 0x100)
 244        /* This is moved out of line as it can be patched by FW, but
 245         * some code path might still want to branch into the original
 246         * vector
 247         */
 248        SET_SCRATCH0(r13)               /* save r13 */
 249        EXCEPTION_PROLOG_0(PACA_EXMC)
 250BEGIN_FTR_SECTION
 251        b       machine_check_common_early
 252FTR_SECTION_ELSE
 253        b       machine_check_pSeries_0
 254ALT_FTR_SECTION_END_IFSET(CPU_FTR_HVMODE)
 255EXC_REAL_END(machine_check, 0x200, 0x100)
 256EXC_VIRT_NONE(0x4200, 0x100)
 257TRAMP_REAL_BEGIN(machine_check_common_early)
 258        EXCEPTION_PROLOG_1(PACA_EXMC, NOTEST, 0x200)
 259        /*
 260         * Register contents:
 261         * R13          = PACA
 262         * R9           = CR
 263         * Original R9 to R13 is saved on PACA_EXMC
 264         *
 265         * Switch to mc_emergency stack and handle re-entrancy (we limit
 266         * the nested MCE upto level 4 to avoid stack overflow).
 267         * Save MCE registers srr1, srr0, dar and dsisr and then set ME=1
 268         *
 269         * We use paca->in_mce to check whether this is the first entry or
 270         * nested machine check. We increment paca->in_mce to track nested
 271         * machine checks.
 272         *
 273         * If this is the first entry then set stack pointer to
 274         * paca->mc_emergency_sp, otherwise r1 is already pointing to
 275         * stack frame on mc_emergency stack.
 276         *
 277         * NOTE: We are here with MSR_ME=0 (off), which means we risk a
 278         * checkstop if we get another machine check exception before we do
 279         * rfid with MSR_ME=1.
 280         *
 281         * This interrupt can wake directly from idle. If that is the case,
 282         * the machine check is handled then the idle wakeup code is called
 283         * to restore state.
 284         */
 285        mr      r11,r1                  /* Save r1 */
 286        lhz     r10,PACA_IN_MCE(r13)
 287        cmpwi   r10,0                   /* Are we in nested machine check */
 288        bne     0f                      /* Yes, we are. */
 289        /* First machine check entry */
 290        ld      r1,PACAMCEMERGSP(r13)   /* Use MC emergency stack */
 2910:      subi    r1,r1,INT_FRAME_SIZE    /* alloc stack frame */
 292        addi    r10,r10,1               /* increment paca->in_mce */
 293        sth     r10,PACA_IN_MCE(r13)
 294        /* Limit nested MCE to level 4 to avoid stack overflow */
 295        cmpwi   r10,MAX_MCE_DEPTH
 296        bgt     2f                      /* Check if we hit limit of 4 */
 297        std     r11,GPR1(r1)            /* Save r1 on the stack. */
 298        std     r11,0(r1)               /* make stack chain pointer */
 299        mfspr   r11,SPRN_SRR0           /* Save SRR0 */
 300        std     r11,_NIP(r1)
 301        mfspr   r11,SPRN_SRR1           /* Save SRR1 */
 302        std     r11,_MSR(r1)
 303        mfspr   r11,SPRN_DAR            /* Save DAR */
 304        std     r11,_DAR(r1)
 305        mfspr   r11,SPRN_DSISR          /* Save DSISR */
 306        std     r11,_DSISR(r1)
 307        std     r9,_CCR(r1)             /* Save CR in stackframe */
 308        /* Save r9 through r13 from EXMC save area to stack frame. */
 309        EXCEPTION_PROLOG_COMMON_2(PACA_EXMC)
 310        mfmsr   r11                     /* get MSR value */
 311BEGIN_FTR_SECTION
 312        ori     r11,r11,MSR_ME          /* turn on ME bit */
 313END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
 314        ori     r11,r11,MSR_RI          /* turn on RI bit */
 315        LOAD_HANDLER(r12, machine_check_handle_early)
 3161:      mtspr   SPRN_SRR0,r12
 317        mtspr   SPRN_SRR1,r11
 318        RFI_TO_KERNEL
 319        b       .       /* prevent speculative execution */
 3202:
 321        /* Stack overflow. Stay on emergency stack and panic.
 322         * Keep the ME bit off while panic-ing, so that if we hit
 323         * another machine check we checkstop.
 324         */
 325        addi    r1,r1,INT_FRAME_SIZE    /* go back to previous stack frame */
 326        ld      r11,PACAKMSR(r13)
 327        LOAD_HANDLER(r12, unrecover_mce)
 328        li      r10,MSR_ME
 329        andc    r11,r11,r10             /* Turn off MSR_ME */
 330        b       1b
 331        b       .       /* prevent speculative execution */
 332
 333TRAMP_REAL_BEGIN(machine_check_pSeries)
 334        .globl machine_check_fwnmi
 335machine_check_fwnmi:
 336        SET_SCRATCH0(r13)               /* save r13 */
 337        EXCEPTION_PROLOG_0(PACA_EXMC)
 338BEGIN_FTR_SECTION
 339        b       machine_check_common_early
 340END_FTR_SECTION_IFCLR(CPU_FTR_HVMODE)
 341machine_check_pSeries_0:
 342        EXCEPTION_PROLOG_1(PACA_EXMC, KVMTEST_PR, 0x200)
 343        /*
 344         * MSR_RI is not enabled, because PACA_EXMC is being used, so a
 345         * nested machine check corrupts it. machine_check_common enables
 346         * MSR_RI.
 347         */
 348        EXCEPTION_PROLOG_PSERIES_1_NORI(machine_check_common, EXC_STD)
 349
 350TRAMP_KVM_SKIP(PACA_EXMC, 0x200)
 351
 352EXC_COMMON_BEGIN(machine_check_common)
 353        /*
 354         * Machine check is different because we use a different
 355         * save area: PACA_EXMC instead of PACA_EXGEN.
 356         */
 357        mfspr   r10,SPRN_DAR
 358        std     r10,PACA_EXMC+EX_DAR(r13)
 359        mfspr   r10,SPRN_DSISR
 360        stw     r10,PACA_EXMC+EX_DSISR(r13)
 361        EXCEPTION_PROLOG_COMMON(0x200, PACA_EXMC)
 362        FINISH_NAP
 363        RECONCILE_IRQ_STATE(r10, r11)
 364        ld      r3,PACA_EXMC+EX_DAR(r13)
 365        lwz     r4,PACA_EXMC+EX_DSISR(r13)
 366        /* Enable MSR_RI when finished with PACA_EXMC */
 367        li      r10,MSR_RI
 368        mtmsrd  r10,1
 369        std     r3,_DAR(r1)
 370        std     r4,_DSISR(r1)
 371        bl      save_nvgprs
 372        addi    r3,r1,STACK_FRAME_OVERHEAD
 373        bl      machine_check_exception
 374        b       ret_from_except
 375
 376#define MACHINE_CHECK_HANDLER_WINDUP                    \
 377        /* Clear MSR_RI before setting SRR0 and SRR1. */\
 378        li      r0,MSR_RI;                              \
 379        mfmsr   r9;             /* get MSR value */     \
 380        andc    r9,r9,r0;                               \
 381        mtmsrd  r9,1;           /* Clear MSR_RI */      \
 382        /* Move original SRR0 and SRR1 into the respective regs */      \
 383        ld      r9,_MSR(r1);                            \
 384        mtspr   SPRN_SRR1,r9;                           \
 385        ld      r3,_NIP(r1);                            \
 386        mtspr   SPRN_SRR0,r3;                           \
 387        ld      r9,_CTR(r1);                            \
 388        mtctr   r9;                                     \
 389        ld      r9,_XER(r1);                            \
 390        mtxer   r9;                                     \
 391        ld      r9,_LINK(r1);                           \
 392        mtlr    r9;                                     \
 393        REST_GPR(0, r1);                                \
 394        REST_8GPRS(2, r1);                              \
 395        REST_GPR(10, r1);                               \
 396        ld      r11,_CCR(r1);                           \
 397        mtcr    r11;                                    \
 398        /* Decrement paca->in_mce. */                   \
 399        lhz     r12,PACA_IN_MCE(r13);                   \
 400        subi    r12,r12,1;                              \
 401        sth     r12,PACA_IN_MCE(r13);                   \
 402        REST_GPR(11, r1);                               \
 403        REST_2GPRS(12, r1);                             \
 404        /* restore original r1. */                      \
 405        ld      r1,GPR1(r1)
 406
 407#ifdef CONFIG_PPC_P7_NAP
 408/*
 409 * This is an idle wakeup. Low level machine check has already been
 410 * done. Queue the event then call the idle code to do the wake up.
 411 */
 412EXC_COMMON_BEGIN(machine_check_idle_common)
 413        bl      machine_check_queue_event
 414
 415        /*
 416         * We have not used any non-volatile GPRs here, and as a rule
 417         * most exception code including machine check does not.
 418         * Therefore PACA_NAPSTATELOST does not need to be set. Idle
 419         * wakeup will restore volatile registers.
 420         *
 421         * Load the original SRR1 into r3 for pnv_powersave_wakeup_mce.
 422         *
 423         * Then decrement MCE nesting after finishing with the stack.
 424         */
 425        ld      r3,_MSR(r1)
 426        ld      r4,_LINK(r1)
 427
 428        lhz     r11,PACA_IN_MCE(r13)
 429        subi    r11,r11,1
 430        sth     r11,PACA_IN_MCE(r13)
 431
 432        mtlr    r4
 433        rlwinm  r10,r3,47-31,30,31
 434        cmpwi   cr1,r10,2
 435        bltlr   cr1     /* no state loss, return to idle caller */
 436        b       idle_return_gpr_loss
 437#endif
 438        /*
 439         * Handle machine check early in real mode. We come here with
 440         * ME=1, MMU (IR=0 and DR=0) off and using MC emergency stack.
 441         */
 442EXC_COMMON_BEGIN(machine_check_handle_early)
 443        std     r0,GPR0(r1)     /* Save r0 */
 444        EXCEPTION_PROLOG_COMMON_3(0x200)
 445        bl      save_nvgprs
 446        addi    r3,r1,STACK_FRAME_OVERHEAD
 447        bl      machine_check_early
 448        std     r3,RESULT(r1)   /* Save result */
 449        ld      r12,_MSR(r1)
 450BEGIN_FTR_SECTION
 451        b       4f
 452END_FTR_SECTION_IFCLR(CPU_FTR_HVMODE)
 453
 454#ifdef  CONFIG_PPC_P7_NAP
 455        /*
 456         * Check if thread was in power saving mode. We come here when any
 457         * of the following is true:
 458         * a. thread wasn't in power saving mode
 459         * b. thread was in power saving mode with no state loss,
 460         *    supervisor state loss or hypervisor state loss.
 461         *
 462         * Go back to nap/sleep/winkle mode again if (b) is true.
 463         */
 464        BEGIN_FTR_SECTION
 465        rlwinm. r11,r12,47-31,30,31
 466        bne     machine_check_idle_common
 467        END_FTR_SECTION_IFSET(CPU_FTR_HVMODE | CPU_FTR_ARCH_206)
 468#endif
 469
 470        /*
 471         * Check if we are coming from hypervisor userspace. If yes then we
 472         * continue in host kernel in V mode to deliver the MC event.
 473         */
 474        rldicl. r11,r12,4,63            /* See if MC hit while in HV mode. */
 475        beq     5f
 4764:      andi.   r11,r12,MSR_PR          /* See if coming from user. */
 477        bne     9f                      /* continue in V mode if we are. */
 478
 4795:
 480#ifdef CONFIG_KVM_BOOK3S_64_HANDLER
 481BEGIN_FTR_SECTION
 482        /*
 483         * We are coming from kernel context. Check if we are coming from
 484         * guest. if yes, then we can continue. We will fall through
 485         * do_kvm_200->kvmppc_interrupt to deliver the MC event to guest.
 486         */
 487        lbz     r11,HSTATE_IN_GUEST(r13)
 488        cmpwi   r11,0                   /* Check if coming from guest */
 489        bne     9f                      /* continue if we are. */
 490END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
 491#endif
 492        /*
 493         * At this point we are not sure about what context we come from.
 494         * Queue up the MCE event and return from the interrupt.
 495         * But before that, check if this is an un-recoverable exception.
 496         * If yes, then stay on emergency stack and panic.
 497         */
 498        andi.   r11,r12,MSR_RI
 499        bne     2f
 5001:      mfspr   r11,SPRN_SRR0
 501        LOAD_HANDLER(r10,unrecover_mce)
 502        mtspr   SPRN_SRR0,r10
 503        ld      r10,PACAKMSR(r13)
 504        /*
 505         * We are going down. But there are chances that we might get hit by
 506         * another MCE during panic path and we may run into unstable state
 507         * with no way out. Hence, turn ME bit off while going down, so that
 508         * when another MCE is hit during panic path, system will checkstop
 509         * and hypervisor will get restarted cleanly by SP.
 510         */
 511        li      r3,MSR_ME
 512        andc    r10,r10,r3              /* Turn off MSR_ME */
 513        mtspr   SPRN_SRR1,r10
 514        RFI_TO_KERNEL
 515        b       .
 5162:
 517        /*
 518         * Check if we have successfully handled/recovered from error, if not
 519         * then stay on emergency stack and panic.
 520         */
 521        ld      r3,RESULT(r1)   /* Load result */
 522        cmpdi   r3,0            /* see if we handled MCE successfully */
 523
 524        beq     1b              /* if !handled then panic */
 525BEGIN_FTR_SECTION
 526        /*
 527         * Return from MC interrupt.
 528         * Queue up the MCE event so that we can log it later, while
 529         * returning from kernel or opal call.
 530         */
 531        bl      machine_check_queue_event
 532        MACHINE_CHECK_HANDLER_WINDUP
 533        RFI_TO_USER_OR_KERNEL
 534FTR_SECTION_ELSE
 535        /*
 536         * pSeries: Return from MC interrupt. Before that stay on emergency
 537         * stack and call machine_check_exception to log the MCE event.
 538         */
 539        LOAD_HANDLER(r10,mce_return)
 540        mtspr   SPRN_SRR0,r10
 541        ld      r10,PACAKMSR(r13)
 542        mtspr   SPRN_SRR1,r10
 543        RFI_TO_KERNEL
 544        b       .
 545ALT_FTR_SECTION_END_IFSET(CPU_FTR_HVMODE)
 5469:
 547        /* Deliver the machine check to host kernel in V mode. */
 548        MACHINE_CHECK_HANDLER_WINDUP
 549        SET_SCRATCH0(r13)               /* save r13 */
 550        EXCEPTION_PROLOG_0(PACA_EXMC)
 551        b       machine_check_pSeries_0
 552
 553EXC_COMMON_BEGIN(unrecover_mce)
 554        /* Invoke machine_check_exception to print MCE event and panic. */
 555        addi    r3,r1,STACK_FRAME_OVERHEAD
 556        bl      machine_check_exception
 557        /*
 558         * We will not reach here. Even if we did, there is no way out. Call
 559         * unrecoverable_exception and die.
 560         */
 5611:      addi    r3,r1,STACK_FRAME_OVERHEAD
 562        bl      unrecoverable_exception
 563        b       1b
 564
 565EXC_COMMON_BEGIN(mce_return)
 566        /* Invoke machine_check_exception to print MCE event and return. */
 567        addi    r3,r1,STACK_FRAME_OVERHEAD
 568        bl      machine_check_exception
 569        MACHINE_CHECK_HANDLER_WINDUP
 570        RFI_TO_KERNEL
 571        b       .
 572
 573EXC_REAL(data_access, 0x300, 0x80)
 574EXC_VIRT(data_access, 0x4300, 0x80, 0x300)
 575TRAMP_KVM_SKIP(PACA_EXGEN, 0x300)
 576
 577EXC_COMMON_BEGIN(data_access_common)
 578        /*
 579         * Here r13 points to the paca, r9 contains the saved CR,
 580         * SRR0 and SRR1 are saved in r11 and r12,
 581         * r9 - r13 are saved in paca->exgen.
 582         */
 583        mfspr   r10,SPRN_DAR
 584        std     r10,PACA_EXGEN+EX_DAR(r13)
 585        mfspr   r10,SPRN_DSISR
 586        stw     r10,PACA_EXGEN+EX_DSISR(r13)
 587        EXCEPTION_PROLOG_COMMON(0x300, PACA_EXGEN)
 588        RECONCILE_IRQ_STATE(r10, r11)
 589        ld      r12,_MSR(r1)
 590        ld      r3,PACA_EXGEN+EX_DAR(r13)
 591        lwz     r4,PACA_EXGEN+EX_DSISR(r13)
 592        li      r5,0x300
 593        std     r3,_DAR(r1)
 594        std     r4,_DSISR(r1)
 595BEGIN_MMU_FTR_SECTION
 596        b       do_hash_page            /* Try to handle as hpte fault */
 597MMU_FTR_SECTION_ELSE
 598        b       handle_page_fault
 599ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_TYPE_RADIX)
 600
 601
 602EXC_REAL_BEGIN(data_access_slb, 0x380, 0x80)
 603        SET_SCRATCH0(r13)
 604        EXCEPTION_PROLOG_0(PACA_EXSLB)
 605        EXCEPTION_PROLOG_1(PACA_EXSLB, KVMTEST_PR, 0x380)
 606        mr      r12,r3  /* save r3 */
 607        mfspr   r3,SPRN_DAR
 608        mfspr   r11,SPRN_SRR1
 609        crset   4*cr6+eq
 610        BRANCH_TO_COMMON(r10, slb_miss_common)
 611EXC_REAL_END(data_access_slb, 0x380, 0x80)
 612
 613EXC_VIRT_BEGIN(data_access_slb, 0x4380, 0x80)
 614        SET_SCRATCH0(r13)
 615        EXCEPTION_PROLOG_0(PACA_EXSLB)
 616        EXCEPTION_PROLOG_1(PACA_EXSLB, NOTEST, 0x380)
 617        mr      r12,r3  /* save r3 */
 618        mfspr   r3,SPRN_DAR
 619        mfspr   r11,SPRN_SRR1
 620        crset   4*cr6+eq
 621        BRANCH_TO_COMMON(r10, slb_miss_common)
 622EXC_VIRT_END(data_access_slb, 0x4380, 0x80)
 623TRAMP_KVM_SKIP(PACA_EXSLB, 0x380)
 624
 625
 626EXC_REAL(instruction_access, 0x400, 0x80)
 627EXC_VIRT(instruction_access, 0x4400, 0x80, 0x400)
 628TRAMP_KVM(PACA_EXGEN, 0x400)
 629
 630EXC_COMMON_BEGIN(instruction_access_common)
 631        EXCEPTION_PROLOG_COMMON(0x400, PACA_EXGEN)
 632        RECONCILE_IRQ_STATE(r10, r11)
 633        ld      r12,_MSR(r1)
 634        ld      r3,_NIP(r1)
 635        andis.  r4,r12,DSISR_SRR1_MATCH_64S@h
 636        li      r5,0x400
 637        std     r3,_DAR(r1)
 638        std     r4,_DSISR(r1)
 639BEGIN_MMU_FTR_SECTION
 640        b       do_hash_page            /* Try to handle as hpte fault */
 641MMU_FTR_SECTION_ELSE
 642        b       handle_page_fault
 643ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_TYPE_RADIX)
 644
 645
 646EXC_REAL_BEGIN(instruction_access_slb, 0x480, 0x80)
 647        SET_SCRATCH0(r13)
 648        EXCEPTION_PROLOG_0(PACA_EXSLB)
 649        EXCEPTION_PROLOG_1(PACA_EXSLB, KVMTEST_PR, 0x480)
 650        mr      r12,r3  /* save r3 */
 651        mfspr   r3,SPRN_SRR0            /* SRR0 is faulting address */
 652        mfspr   r11,SPRN_SRR1
 653        crclr   4*cr6+eq
 654        BRANCH_TO_COMMON(r10, slb_miss_common)
 655EXC_REAL_END(instruction_access_slb, 0x480, 0x80)
 656
 657EXC_VIRT_BEGIN(instruction_access_slb, 0x4480, 0x80)
 658        SET_SCRATCH0(r13)
 659        EXCEPTION_PROLOG_0(PACA_EXSLB)
 660        EXCEPTION_PROLOG_1(PACA_EXSLB, NOTEST, 0x480)
 661        mr      r12,r3  /* save r3 */
 662        mfspr   r3,SPRN_SRR0            /* SRR0 is faulting address */
 663        mfspr   r11,SPRN_SRR1
 664        crclr   4*cr6+eq
 665        BRANCH_TO_COMMON(r10, slb_miss_common)
 666EXC_VIRT_END(instruction_access_slb, 0x4480, 0x80)
 667TRAMP_KVM(PACA_EXSLB, 0x480)
 668
 669
 670/*
 671 * This handler is used by the 0x380 and 0x480 SLB miss interrupts, as well as
 672 * the virtual mode 0x4380 and 0x4480 interrupts if AIL is enabled.
 673 */
 674EXC_COMMON_BEGIN(slb_miss_common)
 675        /*
 676         * r13 points to the PACA, r9 contains the saved CR,
 677         * r12 contains the saved r3,
 678         * r11 contain the saved SRR1, SRR0 is still ready for return
 679         * r3 has the faulting address
 680         * r9 - r13 are saved in paca->exslb.
 681         * cr6.eq is set for a D-SLB miss, clear for a I-SLB miss
 682         * We assume we aren't going to take any exceptions during this
 683         * procedure.
 684         */
 685        mflr    r10
 686        stw     r9,PACA_EXSLB+EX_CCR(r13)       /* save CR in exc. frame */
 687        std     r10,PACA_EXSLB+EX_LR(r13)       /* save LR */
 688
 689        andi.   r9,r11,MSR_PR   // Check for exception from userspace
 690        cmpdi   cr4,r9,MSR_PR   // And save the result in CR4 for later
 691
 692        /*
 693         * Test MSR_RI before calling slb_allocate_realmode, because the
 694         * MSR in r11 gets clobbered. However we still want to allocate
 695         * SLB in case MSR_RI=0, to minimise the risk of getting stuck in
 696         * recursive SLB faults. So use cr5 for this, which is preserved.
 697         */
 698        andi.   r11,r11,MSR_RI  /* check for unrecoverable exception */
 699        cmpdi   cr5,r11,MSR_RI
 700
 701        crset   4*cr0+eq
 702#ifdef CONFIG_PPC_BOOK3S_64
 703BEGIN_MMU_FTR_SECTION
 704        bl      slb_allocate
 705END_MMU_FTR_SECTION_IFCLR(MMU_FTR_TYPE_RADIX)
 706#endif
 707
 708        ld      r10,PACA_EXSLB+EX_LR(r13)
 709        lwz     r9,PACA_EXSLB+EX_CCR(r13)       /* get saved CR */
 710        mtlr    r10
 711
 712        /*
 713         * Large address, check whether we have to allocate new contexts.
 714         */
 715        beq-    8f
 716
 717        bne-    cr5,2f          /* if unrecoverable exception, oops */
 718
 719        /* All done -- return from exception. */
 720
 721        bne     cr4,1f          /* returning to kernel */
 722
 723        mtcrf   0x80,r9
 724        mtcrf   0x08,r9         /* MSR[PR] indication is in cr4 */
 725        mtcrf   0x04,r9         /* MSR[RI] indication is in cr5 */
 726        mtcrf   0x02,r9         /* I/D indication is in cr6 */
 727        mtcrf   0x01,r9         /* slb_allocate uses cr0 and cr7 */
 728
 729        RESTORE_CTR(r9, PACA_EXSLB)
 730        RESTORE_PPR_PACA(PACA_EXSLB, r9)
 731        mr      r3,r12
 732        ld      r9,PACA_EXSLB+EX_R9(r13)
 733        ld      r10,PACA_EXSLB+EX_R10(r13)
 734        ld      r11,PACA_EXSLB+EX_R11(r13)
 735        ld      r12,PACA_EXSLB+EX_R12(r13)
 736        ld      r13,PACA_EXSLB+EX_R13(r13)
 737        RFI_TO_USER
 738        b       .       /* prevent speculative execution */
 7391:
 740        mtcrf   0x80,r9
 741        mtcrf   0x08,r9         /* MSR[PR] indication is in cr4 */
 742        mtcrf   0x04,r9         /* MSR[RI] indication is in cr5 */
 743        mtcrf   0x02,r9         /* I/D indication is in cr6 */
 744        mtcrf   0x01,r9         /* slb_allocate uses cr0 and cr7 */
 745
 746        RESTORE_CTR(r9, PACA_EXSLB)
 747        RESTORE_PPR_PACA(PACA_EXSLB, r9)
 748        mr      r3,r12
 749        ld      r9,PACA_EXSLB+EX_R9(r13)
 750        ld      r10,PACA_EXSLB+EX_R10(r13)
 751        ld      r11,PACA_EXSLB+EX_R11(r13)
 752        ld      r12,PACA_EXSLB+EX_R12(r13)
 753        ld      r13,PACA_EXSLB+EX_R13(r13)
 754        RFI_TO_KERNEL
 755        b       .       /* prevent speculative execution */
 756
 757
 7582:      std     r3,PACA_EXSLB+EX_DAR(r13)
 759        mr      r3,r12
 760        mfspr   r11,SPRN_SRR0
 761        mfspr   r12,SPRN_SRR1
 762        LOAD_HANDLER(r10,unrecov_slb)
 763        mtspr   SPRN_SRR0,r10
 764        ld      r10,PACAKMSR(r13)
 765        mtspr   SPRN_SRR1,r10
 766        RFI_TO_KERNEL
 767        b       .
 768
 7698:      std     r3,PACA_EXSLB+EX_DAR(r13)
 770        mr      r3,r12
 771        mfspr   r11,SPRN_SRR0
 772        mfspr   r12,SPRN_SRR1
 773        LOAD_HANDLER(r10, large_addr_slb)
 774        mtspr   SPRN_SRR0,r10
 775        ld      r10,PACAKMSR(r13)
 776        mtspr   SPRN_SRR1,r10
 777        RFI_TO_KERNEL
 778        b       .
 779
 780EXC_COMMON_BEGIN(unrecov_slb)
 781        EXCEPTION_PROLOG_COMMON(0x4100, PACA_EXSLB)
 782        RECONCILE_IRQ_STATE(r10, r11)
 783        bl      save_nvgprs
 7841:      addi    r3,r1,STACK_FRAME_OVERHEAD
 785        bl      unrecoverable_exception
 786        b       1b
 787
 788EXC_COMMON_BEGIN(large_addr_slb)
 789        EXCEPTION_PROLOG_COMMON(0x380, PACA_EXSLB)
 790        RECONCILE_IRQ_STATE(r10, r11)
 791        ld      r3, PACA_EXSLB+EX_DAR(r13)
 792        std     r3, _DAR(r1)
 793        beq     cr6, 2f
 794        li      r10, 0x481              /* fix trap number for I-SLB miss */
 795        std     r10, _TRAP(r1)
 7962:      bl      save_nvgprs
 797        addi    r3, r1, STACK_FRAME_OVERHEAD
 798        bl      slb_miss_large_addr
 799        b       ret_from_except
 800
 801EXC_REAL_BEGIN(hardware_interrupt, 0x500, 0x100)
 802        .globl hardware_interrupt_hv;
 803hardware_interrupt_hv:
 804        BEGIN_FTR_SECTION
 805                _MASKABLE_EXCEPTION_PSERIES(0x500, hardware_interrupt_common,
 806                                            EXC_HV, SOFTEN_TEST_HV,
 807                                            IRQS_DISABLED)
 808        FTR_SECTION_ELSE
 809                _MASKABLE_EXCEPTION_PSERIES(0x500, hardware_interrupt_common,
 810                                            EXC_STD, SOFTEN_TEST_PR,
 811                                            IRQS_DISABLED)
 812        ALT_FTR_SECTION_END_IFSET(CPU_FTR_HVMODE | CPU_FTR_ARCH_206)
 813EXC_REAL_END(hardware_interrupt, 0x500, 0x100)
 814
 815EXC_VIRT_BEGIN(hardware_interrupt, 0x4500, 0x100)
 816        .globl hardware_interrupt_relon_hv;
 817hardware_interrupt_relon_hv:
 818        BEGIN_FTR_SECTION
 819                _MASKABLE_RELON_EXCEPTION_PSERIES(0x500, hardware_interrupt_common,
 820                                                  EXC_HV, SOFTEN_TEST_HV,
 821                                                  IRQS_DISABLED)
 822        FTR_SECTION_ELSE
 823                _MASKABLE_RELON_EXCEPTION_PSERIES(0x500, hardware_interrupt_common,
 824                                                  EXC_STD, SOFTEN_TEST_PR,
 825                                                  IRQS_DISABLED)
 826        ALT_FTR_SECTION_END_IFSET(CPU_FTR_HVMODE)
 827EXC_VIRT_END(hardware_interrupt, 0x4500, 0x100)
 828
 829TRAMP_KVM(PACA_EXGEN, 0x500)
 830TRAMP_KVM_HV(PACA_EXGEN, 0x500)
 831EXC_COMMON_ASYNC(hardware_interrupt_common, 0x500, do_IRQ)
 832
 833
 834EXC_REAL(alignment, 0x600, 0x100)
 835EXC_VIRT(alignment, 0x4600, 0x100, 0x600)
 836TRAMP_KVM(PACA_EXGEN, 0x600)
 837EXC_COMMON_BEGIN(alignment_common)
 838        mfspr   r10,SPRN_DAR
 839        std     r10,PACA_EXGEN+EX_DAR(r13)
 840        mfspr   r10,SPRN_DSISR
 841        stw     r10,PACA_EXGEN+EX_DSISR(r13)
 842        EXCEPTION_PROLOG_COMMON(0x600, PACA_EXGEN)
 843        ld      r3,PACA_EXGEN+EX_DAR(r13)
 844        lwz     r4,PACA_EXGEN+EX_DSISR(r13)
 845        std     r3,_DAR(r1)
 846        std     r4,_DSISR(r1)
 847        bl      save_nvgprs
 848        RECONCILE_IRQ_STATE(r10, r11)
 849        addi    r3,r1,STACK_FRAME_OVERHEAD
 850        bl      alignment_exception
 851        b       ret_from_except
 852
 853
 854EXC_REAL(program_check, 0x700, 0x100)
 855EXC_VIRT(program_check, 0x4700, 0x100, 0x700)
 856TRAMP_KVM(PACA_EXGEN, 0x700)
 857EXC_COMMON_BEGIN(program_check_common)
 858        /*
 859         * It's possible to receive a TM Bad Thing type program check with
 860         * userspace register values (in particular r1), but with SRR1 reporting
 861         * that we came from the kernel. Normally that would confuse the bad
 862         * stack logic, and we would report a bad kernel stack pointer. Instead
 863         * we switch to the emergency stack if we're taking a TM Bad Thing from
 864         * the kernel.
 865         */
 866        li      r10,MSR_PR              /* Build a mask of MSR_PR ..    */
 867        oris    r10,r10,0x200000@h      /* .. and SRR1_PROGTM           */
 868        and     r10,r10,r12             /* Mask SRR1 with that.         */
 869        srdi    r10,r10,8               /* Shift it so we can compare   */
 870        cmpldi  r10,(0x200000 >> 8)     /* .. with an immediate.        */
 871        bne 1f                          /* If != go to normal path.     */
 872
 873        /* SRR1 had PR=0 and SRR1_PROGTM=1, so use the emergency stack  */
 874        andi.   r10,r12,MSR_PR;         /* Set CR0 correctly for label  */
 875                                        /* 3 in EXCEPTION_PROLOG_COMMON */
 876        mr      r10,r1                  /* Save r1                      */
 877        ld      r1,PACAEMERGSP(r13)     /* Use emergency stack          */
 878        subi    r1,r1,INT_FRAME_SIZE    /* alloc stack frame            */
 879        b 3f                            /* Jump into the macro !!       */
 8801:      EXCEPTION_PROLOG_COMMON(0x700, PACA_EXGEN)
 881        bl      save_nvgprs
 882        RECONCILE_IRQ_STATE(r10, r11)
 883        addi    r3,r1,STACK_FRAME_OVERHEAD
 884        bl      program_check_exception
 885        b       ret_from_except
 886
 887
 888EXC_REAL(fp_unavailable, 0x800, 0x100)
 889EXC_VIRT(fp_unavailable, 0x4800, 0x100, 0x800)
 890TRAMP_KVM(PACA_EXGEN, 0x800)
 891EXC_COMMON_BEGIN(fp_unavailable_common)
 892        EXCEPTION_PROLOG_COMMON(0x800, PACA_EXGEN)
 893        bne     1f                      /* if from user, just load it up */
 894        bl      save_nvgprs
 895        RECONCILE_IRQ_STATE(r10, r11)
 896        addi    r3,r1,STACK_FRAME_OVERHEAD
 897        bl      kernel_fp_unavailable_exception
 8980:      trap
 899        EMIT_BUG_ENTRY 0b, __FILE__, __LINE__, 0
 9001:
 901#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
 902BEGIN_FTR_SECTION
 903        /* Test if 2 TM state bits are zero.  If non-zero (ie. userspace was in
 904         * transaction), go do TM stuff
 905         */
 906        rldicl. r0, r12, (64-MSR_TS_LG), (64-2)
 907        bne-    2f
 908END_FTR_SECTION_IFSET(CPU_FTR_TM)
 909#endif
 910        bl      load_up_fpu
 911        b       fast_exception_return
 912#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
 9132:      /* User process was in a transaction */
 914        bl      save_nvgprs
 915        RECONCILE_IRQ_STATE(r10, r11)
 916        addi    r3,r1,STACK_FRAME_OVERHEAD
 917        bl      fp_unavailable_tm
 918        b       ret_from_except
 919#endif
 920
 921
 922EXC_REAL_OOL_MASKABLE(decrementer, 0x900, 0x80, IRQS_DISABLED)
 923EXC_VIRT_MASKABLE(decrementer, 0x4900, 0x80, 0x900, IRQS_DISABLED)
 924TRAMP_KVM(PACA_EXGEN, 0x900)
 925EXC_COMMON_ASYNC(decrementer_common, 0x900, timer_interrupt)
 926
 927
 928EXC_REAL_HV(hdecrementer, 0x980, 0x80)
 929EXC_VIRT_HV(hdecrementer, 0x4980, 0x80, 0x980)
 930TRAMP_KVM_HV(PACA_EXGEN, 0x980)
 931EXC_COMMON(hdecrementer_common, 0x980, hdec_interrupt)
 932
 933
 934EXC_REAL_MASKABLE(doorbell_super, 0xa00, 0x100, IRQS_DISABLED)
 935EXC_VIRT_MASKABLE(doorbell_super, 0x4a00, 0x100, 0xa00, IRQS_DISABLED)
 936TRAMP_KVM(PACA_EXGEN, 0xa00)
 937#ifdef CONFIG_PPC_DOORBELL
 938EXC_COMMON_ASYNC(doorbell_super_common, 0xa00, doorbell_exception)
 939#else
 940EXC_COMMON_ASYNC(doorbell_super_common, 0xa00, unknown_exception)
 941#endif
 942
 943
 944EXC_REAL(trap_0b, 0xb00, 0x100)
 945EXC_VIRT(trap_0b, 0x4b00, 0x100, 0xb00)
 946TRAMP_KVM(PACA_EXGEN, 0xb00)
 947EXC_COMMON(trap_0b_common, 0xb00, unknown_exception)
 948
 949/*
 950 * system call / hypercall (0xc00, 0x4c00)
 951 *
 952 * The system call exception is invoked with "sc 0" and does not alter HV bit.
 953 * There is support for kernel code to invoke system calls but there are no
 954 * in-tree users.
 955 *
 956 * The hypercall is invoked with "sc 1" and sets HV=1.
 957 *
 958 * In HPT, sc 1 always goes to 0xc00 real mode. In RADIX, sc 1 can go to
 959 * 0x4c00 virtual mode.
 960 *
 961 * Call convention:
 962 *
 963 * syscall register convention is in Documentation/powerpc/syscall64-abi.txt
 964 *
 965 * For hypercalls, the register convention is as follows:
 966 * r0 volatile
 967 * r1-2 nonvolatile
 968 * r3 volatile parameter and return value for status
 969 * r4-r10 volatile input and output value
 970 * r11 volatile hypercall number and output value
 971 * r12 volatile input and output value
 972 * r13-r31 nonvolatile
 973 * LR nonvolatile
 974 * CTR volatile
 975 * XER volatile
 976 * CR0-1 CR5-7 volatile
 977 * CR2-4 nonvolatile
 978 * Other registers nonvolatile
 979 *
 980 * The intersection of volatile registers that don't contain possible
 981 * inputs is: cr0, xer, ctr. We may use these as scratch regs upon entry
 982 * without saving, though xer is not a good idea to use, as hardware may
 983 * interpret some bits so it may be costly to change them.
 984 */
 985#ifdef CONFIG_KVM_BOOK3S_64_HANDLER
 986        /*
 987         * There is a little bit of juggling to get syscall and hcall
 988         * working well. Save r13 in ctr to avoid using SPRG scratch
 989         * register.
 990         *
 991         * Userspace syscalls have already saved the PPR, hcalls must save
 992         * it before setting HMT_MEDIUM.
 993         */
 994#define SYSCALL_KVMTEST                                                 \
 995        mtctr   r13;                                                    \
 996        GET_PACA(r13);                                                  \
 997        std     r10,PACA_EXGEN+EX_R10(r13);                             \
 998        INTERRUPT_TO_KERNEL;                                            \
 999        KVMTEST_PR(0xc00); /* uses r10, branch to do_kvm_0xc00_system_call */ \
1000        HMT_MEDIUM;                                                     \
1001        mfctr   r9;
1002
1003#else
1004#define SYSCALL_KVMTEST                                                 \
1005        HMT_MEDIUM;                                                     \
1006        mr      r9,r13;                                                 \
1007        GET_PACA(r13);                                                  \
1008        INTERRUPT_TO_KERNEL;
1009#endif
1010        
1011#define LOAD_SYSCALL_HANDLER(reg)                                       \
1012        __LOAD_HANDLER(reg, system_call_common)
1013
1014/*
1015 * After SYSCALL_KVMTEST, we reach here with PACA in r13, r13 in r9,
1016 * and HMT_MEDIUM.
1017 */
1018#define SYSCALL_REAL                                            \
1019        mfspr   r11,SPRN_SRR0 ;                                 \
1020        mfspr   r12,SPRN_SRR1 ;                                 \
1021        LOAD_SYSCALL_HANDLER(r10) ;                             \
1022        mtspr   SPRN_SRR0,r10 ;                                 \
1023        ld      r10,PACAKMSR(r13) ;                             \
1024        mtspr   SPRN_SRR1,r10 ;                                 \
1025        RFI_TO_KERNEL ;                                         \
1026        b       . ;     /* prevent speculative execution */
1027
1028#ifdef CONFIG_PPC_FAST_ENDIAN_SWITCH
1029#define SYSCALL_FASTENDIAN_TEST                                 \
1030BEGIN_FTR_SECTION                                               \
1031        cmpdi   r0,0x1ebe ;                                     \
1032        beq-    1f ;                                            \
1033END_FTR_SECTION_IFSET(CPU_FTR_REAL_LE)                          \
1034
1035#define SYSCALL_FASTENDIAN                                      \
1036        /* Fast LE/BE switch system call */                     \
10371:      mfspr   r12,SPRN_SRR1 ;                                 \
1038        xori    r12,r12,MSR_LE ;                                \
1039        mtspr   SPRN_SRR1,r12 ;                                 \
1040        mr      r13,r9 ;                                        \
1041        RFI_TO_USER ;   /* return to userspace */               \
1042        b       . ;     /* prevent speculative execution */
1043#else
1044#define SYSCALL_FASTENDIAN_TEST
1045#define SYSCALL_FASTENDIAN
1046#endif /* CONFIG_PPC_FAST_ENDIAN_SWITCH */
1047
1048#if defined(CONFIG_RELOCATABLE)
1049        /*
1050         * We can't branch directly so we do it via the CTR which
1051         * is volatile across system calls.
1052         */
1053#define SYSCALL_VIRT                                            \
1054        LOAD_SYSCALL_HANDLER(r10) ;                             \
1055        mtctr   r10 ;                                           \
1056        mfspr   r11,SPRN_SRR0 ;                                 \
1057        mfspr   r12,SPRN_SRR1 ;                                 \
1058        li      r10,MSR_RI ;                                    \
1059        mtmsrd  r10,1 ;                                         \
1060        bctr ;
1061#else
1062        /* We can branch directly */
1063#define SYSCALL_VIRT                                            \
1064        mfspr   r11,SPRN_SRR0 ;                                 \
1065        mfspr   r12,SPRN_SRR1 ;                                 \
1066        li      r10,MSR_RI ;                                    \
1067        mtmsrd  r10,1 ;                 /* Set RI (EE=0) */     \
1068        b       system_call_common ;
1069#endif
1070
1071EXC_REAL_BEGIN(system_call, 0xc00, 0x100)
1072        SYSCALL_KVMTEST /* loads PACA into r13, and saves r13 to r9 */
1073        SYSCALL_FASTENDIAN_TEST
1074        SYSCALL_REAL
1075        SYSCALL_FASTENDIAN
1076EXC_REAL_END(system_call, 0xc00, 0x100)
1077
1078EXC_VIRT_BEGIN(system_call, 0x4c00, 0x100)
1079        SYSCALL_KVMTEST /* loads PACA into r13, and saves r13 to r9 */
1080        SYSCALL_FASTENDIAN_TEST
1081        SYSCALL_VIRT
1082        SYSCALL_FASTENDIAN
1083EXC_VIRT_END(system_call, 0x4c00, 0x100)
1084
1085#ifdef CONFIG_KVM_BOOK3S_64_HANDLER
1086        /*
1087         * This is a hcall, so register convention is as above, with these
1088         * differences:
1089         * r13 = PACA
1090         * ctr = orig r13
1091         * orig r10 saved in PACA
1092         */
1093TRAMP_KVM_BEGIN(do_kvm_0xc00)
1094         /*
1095          * Save the PPR (on systems that support it) before changing to
1096          * HMT_MEDIUM. That allows the KVM code to save that value into the
1097          * guest state (it is the guest's PPR value).
1098          */
1099        OPT_GET_SPR(r10, SPRN_PPR, CPU_FTR_HAS_PPR)
1100        HMT_MEDIUM
1101        OPT_SAVE_REG_TO_PACA(PACA_EXGEN+EX_PPR, r10, CPU_FTR_HAS_PPR)
1102        mfctr   r10
1103        SET_SCRATCH0(r10)
1104        std     r9,PACA_EXGEN+EX_R9(r13)
1105        mfcr    r9
1106        KVM_HANDLER(PACA_EXGEN, EXC_STD, 0xc00)
1107#endif
1108
1109
1110EXC_REAL(single_step, 0xd00, 0x100)
1111EXC_VIRT(single_step, 0x4d00, 0x100, 0xd00)
1112TRAMP_KVM(PACA_EXGEN, 0xd00)
1113EXC_COMMON(single_step_common, 0xd00, single_step_exception)
1114
1115EXC_REAL_OOL_HV(h_data_storage, 0xe00, 0x20)
1116EXC_VIRT_OOL_HV(h_data_storage, 0x4e00, 0x20, 0xe00)
1117TRAMP_KVM_HV_SKIP(PACA_EXGEN, 0xe00)
1118EXC_COMMON_BEGIN(h_data_storage_common)
1119        mfspr   r10,SPRN_HDAR
1120        std     r10,PACA_EXGEN+EX_DAR(r13)
1121        mfspr   r10,SPRN_HDSISR
1122        stw     r10,PACA_EXGEN+EX_DSISR(r13)
1123        EXCEPTION_PROLOG_COMMON(0xe00, PACA_EXGEN)
1124        bl      save_nvgprs
1125        RECONCILE_IRQ_STATE(r10, r11)
1126        addi    r3,r1,STACK_FRAME_OVERHEAD
1127BEGIN_MMU_FTR_SECTION
1128        ld      r4,PACA_EXGEN+EX_DAR(r13)
1129        lwz     r5,PACA_EXGEN+EX_DSISR(r13)
1130        std     r4,_DAR(r1)
1131        std     r5,_DSISR(r1)
1132        li      r5,SIGSEGV
1133        bl      bad_page_fault
1134MMU_FTR_SECTION_ELSE
1135        bl      unknown_exception
1136ALT_MMU_FTR_SECTION_END_IFSET(MMU_FTR_TYPE_RADIX)
1137        b       ret_from_except
1138
1139
1140EXC_REAL_OOL_HV(h_instr_storage, 0xe20, 0x20)
1141EXC_VIRT_OOL_HV(h_instr_storage, 0x4e20, 0x20, 0xe20)
1142TRAMP_KVM_HV(PACA_EXGEN, 0xe20)
1143EXC_COMMON(h_instr_storage_common, 0xe20, unknown_exception)
1144
1145
1146EXC_REAL_OOL_HV(emulation_assist, 0xe40, 0x20)
1147EXC_VIRT_OOL_HV(emulation_assist, 0x4e40, 0x20, 0xe40)
1148TRAMP_KVM_HV(PACA_EXGEN, 0xe40)
1149EXC_COMMON(emulation_assist_common, 0xe40, emulation_assist_interrupt)
1150
1151
1152/*
1153 * hmi_exception trampoline is a special case. It jumps to hmi_exception_early
1154 * first, and then eventaully from there to the trampoline to get into virtual
1155 * mode.
1156 */
1157__EXC_REAL_OOL_HV_DIRECT(hmi_exception, 0xe60, 0x20, hmi_exception_early)
1158__TRAMP_REAL_OOL_MASKABLE_HV(hmi_exception, 0xe60, IRQS_DISABLED)
1159EXC_VIRT_NONE(0x4e60, 0x20)
1160TRAMP_KVM_HV(PACA_EXGEN, 0xe60)
1161TRAMP_REAL_BEGIN(hmi_exception_early)
1162        EXCEPTION_PROLOG_1(PACA_EXGEN, KVMTEST_HV, 0xe60)
1163        mr      r10,r1                  /* Save r1 */
1164        ld      r1,PACAEMERGSP(r13)     /* Use emergency stack for realmode */
1165        subi    r1,r1,INT_FRAME_SIZE    /* alloc stack frame            */
1166        mfspr   r11,SPRN_HSRR0          /* Save HSRR0 */
1167        mfspr   r12,SPRN_HSRR1          /* Save HSRR1 */
1168        EXCEPTION_PROLOG_COMMON_1()
1169        EXCEPTION_PROLOG_COMMON_2(PACA_EXGEN)
1170        EXCEPTION_PROLOG_COMMON_3(0xe60)
1171        addi    r3,r1,STACK_FRAME_OVERHEAD
1172        BRANCH_LINK_TO_FAR(hmi_exception_realmode) /* Function call ABI */
1173        cmpdi   cr0,r3,0
1174
1175        /* Windup the stack. */
1176        /* Move original HSRR0 and HSRR1 into the respective regs */
1177        ld      r9,_MSR(r1)
1178        mtspr   SPRN_HSRR1,r9
1179        ld      r3,_NIP(r1)
1180        mtspr   SPRN_HSRR0,r3
1181        ld      r9,_CTR(r1)
1182        mtctr   r9
1183        ld      r9,_XER(r1)
1184        mtxer   r9
1185        ld      r9,_LINK(r1)
1186        mtlr    r9
1187        REST_GPR(0, r1)
1188        REST_8GPRS(2, r1)
1189        REST_GPR(10, r1)
1190        ld      r11,_CCR(r1)
1191        REST_2GPRS(12, r1)
1192        bne     1f
1193        mtcr    r11
1194        REST_GPR(11, r1)
1195        ld      r1,GPR1(r1)
1196        HRFI_TO_USER_OR_KERNEL
1197
11981:      mtcr    r11
1199        REST_GPR(11, r1)
1200        ld      r1,GPR1(r1)
1201
1202        /*
1203         * Go to virtual mode and pull the HMI event information from
1204         * firmware.
1205         */
1206        .globl hmi_exception_after_realmode
1207hmi_exception_after_realmode:
1208        SET_SCRATCH0(r13)
1209        EXCEPTION_PROLOG_0(PACA_EXGEN)
1210        b       tramp_real_hmi_exception
1211
1212EXC_COMMON_BEGIN(hmi_exception_common)
1213EXCEPTION_COMMON(PACA_EXGEN, 0xe60, hmi_exception_common, handle_hmi_exception,
1214        ret_from_except, FINISH_NAP;ADD_NVGPRS;ADD_RECONCILE;RUNLATCH_ON)
1215
1216EXC_REAL_OOL_MASKABLE_HV(h_doorbell, 0xe80, 0x20, IRQS_DISABLED)
1217EXC_VIRT_OOL_MASKABLE_HV(h_doorbell, 0x4e80, 0x20, 0xe80, IRQS_DISABLED)
1218TRAMP_KVM_HV(PACA_EXGEN, 0xe80)
1219#ifdef CONFIG_PPC_DOORBELL
1220EXC_COMMON_ASYNC(h_doorbell_common, 0xe80, doorbell_exception)
1221#else
1222EXC_COMMON_ASYNC(h_doorbell_common, 0xe80, unknown_exception)
1223#endif
1224
1225
1226EXC_REAL_OOL_MASKABLE_HV(h_virt_irq, 0xea0, 0x20, IRQS_DISABLED)
1227EXC_VIRT_OOL_MASKABLE_HV(h_virt_irq, 0x4ea0, 0x20, 0xea0, IRQS_DISABLED)
1228TRAMP_KVM_HV(PACA_EXGEN, 0xea0)
1229EXC_COMMON_ASYNC(h_virt_irq_common, 0xea0, do_IRQ)
1230
1231
1232EXC_REAL_NONE(0xec0, 0x20)
1233EXC_VIRT_NONE(0x4ec0, 0x20)
1234EXC_REAL_NONE(0xee0, 0x20)
1235EXC_VIRT_NONE(0x4ee0, 0x20)
1236
1237
1238EXC_REAL_OOL_MASKABLE(performance_monitor, 0xf00, 0x20, IRQS_PMI_DISABLED)
1239EXC_VIRT_OOL_MASKABLE(performance_monitor, 0x4f00, 0x20, 0xf00, IRQS_PMI_DISABLED)
1240TRAMP_KVM(PACA_EXGEN, 0xf00)
1241EXC_COMMON_ASYNC(performance_monitor_common, 0xf00, performance_monitor_exception)
1242
1243
1244EXC_REAL_OOL(altivec_unavailable, 0xf20, 0x20)
1245EXC_VIRT_OOL(altivec_unavailable, 0x4f20, 0x20, 0xf20)
1246TRAMP_KVM(PACA_EXGEN, 0xf20)
1247EXC_COMMON_BEGIN(altivec_unavailable_common)
1248        EXCEPTION_PROLOG_COMMON(0xf20, PACA_EXGEN)
1249#ifdef CONFIG_ALTIVEC
1250BEGIN_FTR_SECTION
1251        beq     1f
1252#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1253  BEGIN_FTR_SECTION_NESTED(69)
1254        /* Test if 2 TM state bits are zero.  If non-zero (ie. userspace was in
1255         * transaction), go do TM stuff
1256         */
1257        rldicl. r0, r12, (64-MSR_TS_LG), (64-2)
1258        bne-    2f
1259  END_FTR_SECTION_NESTED(CPU_FTR_TM, CPU_FTR_TM, 69)
1260#endif
1261        bl      load_up_altivec
1262        b       fast_exception_return
1263#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
12642:      /* User process was in a transaction */
1265        bl      save_nvgprs
1266        RECONCILE_IRQ_STATE(r10, r11)
1267        addi    r3,r1,STACK_FRAME_OVERHEAD
1268        bl      altivec_unavailable_tm
1269        b       ret_from_except
1270#endif
12711:
1272END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
1273#endif
1274        bl      save_nvgprs
1275        RECONCILE_IRQ_STATE(r10, r11)
1276        addi    r3,r1,STACK_FRAME_OVERHEAD
1277        bl      altivec_unavailable_exception
1278        b       ret_from_except
1279
1280
1281EXC_REAL_OOL(vsx_unavailable, 0xf40, 0x20)
1282EXC_VIRT_OOL(vsx_unavailable, 0x4f40, 0x20, 0xf40)
1283TRAMP_KVM(PACA_EXGEN, 0xf40)
1284EXC_COMMON_BEGIN(vsx_unavailable_common)
1285        EXCEPTION_PROLOG_COMMON(0xf40, PACA_EXGEN)
1286#ifdef CONFIG_VSX
1287BEGIN_FTR_SECTION
1288        beq     1f
1289#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1290  BEGIN_FTR_SECTION_NESTED(69)
1291        /* Test if 2 TM state bits are zero.  If non-zero (ie. userspace was in
1292         * transaction), go do TM stuff
1293         */
1294        rldicl. r0, r12, (64-MSR_TS_LG), (64-2)
1295        bne-    2f
1296  END_FTR_SECTION_NESTED(CPU_FTR_TM, CPU_FTR_TM, 69)
1297#endif
1298        b       load_up_vsx
1299#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
13002:      /* User process was in a transaction */
1301        bl      save_nvgprs
1302        RECONCILE_IRQ_STATE(r10, r11)
1303        addi    r3,r1,STACK_FRAME_OVERHEAD
1304        bl      vsx_unavailable_tm
1305        b       ret_from_except
1306#endif
13071:
1308END_FTR_SECTION_IFSET(CPU_FTR_VSX)
1309#endif
1310        bl      save_nvgprs
1311        RECONCILE_IRQ_STATE(r10, r11)
1312        addi    r3,r1,STACK_FRAME_OVERHEAD
1313        bl      vsx_unavailable_exception
1314        b       ret_from_except
1315
1316
1317EXC_REAL_OOL(facility_unavailable, 0xf60, 0x20)
1318EXC_VIRT_OOL(facility_unavailable, 0x4f60, 0x20, 0xf60)
1319TRAMP_KVM(PACA_EXGEN, 0xf60)
1320EXC_COMMON(facility_unavailable_common, 0xf60, facility_unavailable_exception)
1321
1322
1323EXC_REAL_OOL_HV(h_facility_unavailable, 0xf80, 0x20)
1324EXC_VIRT_OOL_HV(h_facility_unavailable, 0x4f80, 0x20, 0xf80)
1325TRAMP_KVM_HV(PACA_EXGEN, 0xf80)
1326EXC_COMMON(h_facility_unavailable_common, 0xf80, facility_unavailable_exception)
1327
1328
1329EXC_REAL_NONE(0xfa0, 0x20)
1330EXC_VIRT_NONE(0x4fa0, 0x20)
1331EXC_REAL_NONE(0xfc0, 0x20)
1332EXC_VIRT_NONE(0x4fc0, 0x20)
1333EXC_REAL_NONE(0xfe0, 0x20)
1334EXC_VIRT_NONE(0x4fe0, 0x20)
1335
1336EXC_REAL_NONE(0x1000, 0x100)
1337EXC_VIRT_NONE(0x5000, 0x100)
1338EXC_REAL_NONE(0x1100, 0x100)
1339EXC_VIRT_NONE(0x5100, 0x100)
1340
1341#ifdef CONFIG_CBE_RAS
1342EXC_REAL_HV(cbe_system_error, 0x1200, 0x100)
1343EXC_VIRT_NONE(0x5200, 0x100)
1344TRAMP_KVM_HV_SKIP(PACA_EXGEN, 0x1200)
1345EXC_COMMON(cbe_system_error_common, 0x1200, cbe_system_error_exception)
1346#else /* CONFIG_CBE_RAS */
1347EXC_REAL_NONE(0x1200, 0x100)
1348EXC_VIRT_NONE(0x5200, 0x100)
1349#endif
1350
1351
1352EXC_REAL(instruction_breakpoint, 0x1300, 0x100)
1353EXC_VIRT(instruction_breakpoint, 0x5300, 0x100, 0x1300)
1354TRAMP_KVM_SKIP(PACA_EXGEN, 0x1300)
1355EXC_COMMON(instruction_breakpoint_common, 0x1300, instruction_breakpoint_exception)
1356
1357EXC_REAL_NONE(0x1400, 0x100)
1358EXC_VIRT_NONE(0x5400, 0x100)
1359
1360EXC_REAL_BEGIN(denorm_exception_hv, 0x1500, 0x100)
1361        mtspr   SPRN_SPRG_HSCRATCH0,r13
1362        EXCEPTION_PROLOG_0(PACA_EXGEN)
1363        EXCEPTION_PROLOG_1(PACA_EXGEN, NOTEST, 0x1500)
1364
1365#ifdef CONFIG_PPC_DENORMALISATION
1366        mfspr   r10,SPRN_HSRR1
1367        andis.  r10,r10,(HSRR1_DENORM)@h /* denorm? */
1368        bne+    denorm_assist
1369#endif
1370
1371        KVMTEST_HV(0x1500)
1372        EXCEPTION_PROLOG_PSERIES_1(denorm_common, EXC_HV)
1373EXC_REAL_END(denorm_exception_hv, 0x1500, 0x100)
1374
1375#ifdef CONFIG_PPC_DENORMALISATION
1376EXC_VIRT_BEGIN(denorm_exception, 0x5500, 0x100)
1377        b       exc_real_0x1500_denorm_exception_hv
1378EXC_VIRT_END(denorm_exception, 0x5500, 0x100)
1379#else
1380EXC_VIRT_NONE(0x5500, 0x100)
1381#endif
1382
1383TRAMP_KVM_HV(PACA_EXGEN, 0x1500)
1384
1385#ifdef CONFIG_PPC_DENORMALISATION
1386TRAMP_REAL_BEGIN(denorm_assist)
1387BEGIN_FTR_SECTION
1388/*
1389 * To denormalise we need to move a copy of the register to itself.
1390 * For POWER6 do that here for all FP regs.
1391 */
1392        mfmsr   r10
1393        ori     r10,r10,(MSR_FP|MSR_FE0|MSR_FE1)
1394        xori    r10,r10,(MSR_FE0|MSR_FE1)
1395        mtmsrd  r10
1396        sync
1397
1398#define FMR2(n)  fmr (n), (n) ; fmr n+1, n+1
1399#define FMR4(n)  FMR2(n) ; FMR2(n+2)
1400#define FMR8(n)  FMR4(n) ; FMR4(n+4)
1401#define FMR16(n) FMR8(n) ; FMR8(n+8)
1402#define FMR32(n) FMR16(n) ; FMR16(n+16)
1403        FMR32(0)
1404
1405FTR_SECTION_ELSE
1406/*
1407 * To denormalise we need to move a copy of the register to itself.
1408 * For POWER7 do that here for the first 32 VSX registers only.
1409 */
1410        mfmsr   r10
1411        oris    r10,r10,MSR_VSX@h
1412        mtmsrd  r10
1413        sync
1414
1415#define XVCPSGNDP2(n) XVCPSGNDP(n,n,n) ; XVCPSGNDP(n+1,n+1,n+1)
1416#define XVCPSGNDP4(n) XVCPSGNDP2(n) ; XVCPSGNDP2(n+2)
1417#define XVCPSGNDP8(n) XVCPSGNDP4(n) ; XVCPSGNDP4(n+4)
1418#define XVCPSGNDP16(n) XVCPSGNDP8(n) ; XVCPSGNDP8(n+8)
1419#define XVCPSGNDP32(n) XVCPSGNDP16(n) ; XVCPSGNDP16(n+16)
1420        XVCPSGNDP32(0)
1421
1422ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_206)
1423
1424BEGIN_FTR_SECTION
1425        b       denorm_done
1426END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
1427/*
1428 * To denormalise we need to move a copy of the register to itself.
1429 * For POWER8 we need to do that for all 64 VSX registers
1430 */
1431        XVCPSGNDP32(32)
1432denorm_done:
1433        mfspr   r11,SPRN_HSRR0
1434        subi    r11,r11,4
1435        mtspr   SPRN_HSRR0,r11
1436        mtcrf   0x80,r9
1437        ld      r9,PACA_EXGEN+EX_R9(r13)
1438        RESTORE_PPR_PACA(PACA_EXGEN, r10)
1439BEGIN_FTR_SECTION
1440        ld      r10,PACA_EXGEN+EX_CFAR(r13)
1441        mtspr   SPRN_CFAR,r10
1442END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
1443        ld      r10,PACA_EXGEN+EX_R10(r13)
1444        ld      r11,PACA_EXGEN+EX_R11(r13)
1445        ld      r12,PACA_EXGEN+EX_R12(r13)
1446        ld      r13,PACA_EXGEN+EX_R13(r13)
1447        HRFI_TO_UNKNOWN
1448        b       .
1449#endif
1450
1451EXC_COMMON(denorm_common, 0x1500, unknown_exception)
1452
1453
1454#ifdef CONFIG_CBE_RAS
1455EXC_REAL_HV(cbe_maintenance, 0x1600, 0x100)
1456EXC_VIRT_NONE(0x5600, 0x100)
1457TRAMP_KVM_HV_SKIP(PACA_EXGEN, 0x1600)
1458EXC_COMMON(cbe_maintenance_common, 0x1600, cbe_maintenance_exception)
1459#else /* CONFIG_CBE_RAS */
1460EXC_REAL_NONE(0x1600, 0x100)
1461EXC_VIRT_NONE(0x5600, 0x100)
1462#endif
1463
1464
1465EXC_REAL(altivec_assist, 0x1700, 0x100)
1466EXC_VIRT(altivec_assist, 0x5700, 0x100, 0x1700)
1467TRAMP_KVM(PACA_EXGEN, 0x1700)
1468#ifdef CONFIG_ALTIVEC
1469EXC_COMMON(altivec_assist_common, 0x1700, altivec_assist_exception)
1470#else
1471EXC_COMMON(altivec_assist_common, 0x1700, unknown_exception)
1472#endif
1473
1474
1475#ifdef CONFIG_CBE_RAS
1476EXC_REAL_HV(cbe_thermal, 0x1800, 0x100)
1477EXC_VIRT_NONE(0x5800, 0x100)
1478TRAMP_KVM_HV_SKIP(PACA_EXGEN, 0x1800)
1479EXC_COMMON(cbe_thermal_common, 0x1800, cbe_thermal_exception)
1480#else /* CONFIG_CBE_RAS */
1481EXC_REAL_NONE(0x1800, 0x100)
1482EXC_VIRT_NONE(0x5800, 0x100)
1483#endif
1484
1485#ifdef CONFIG_PPC_WATCHDOG
1486
1487#define MASKED_DEC_HANDLER_LABEL 3f
1488
1489#define MASKED_DEC_HANDLER(_H)                          \
14903: /* soft-nmi */                                       \
1491        std     r12,PACA_EXGEN+EX_R12(r13);             \
1492        GET_SCRATCH0(r10);                              \
1493        std     r10,PACA_EXGEN+EX_R13(r13);             \
1494        EXCEPTION_PROLOG_PSERIES_1(soft_nmi_common, _H)
1495
1496/*
1497 * Branch to soft_nmi_interrupt using the emergency stack. The emergency
1498 * stack is one that is usable by maskable interrupts so long as MSR_EE
1499 * remains off. It is used for recovery when something has corrupted the
1500 * normal kernel stack, for example. The "soft NMI" must not use the process
1501 * stack because we want irq disabled sections to avoid touching the stack
1502 * at all (other than PMU interrupts), so use the emergency stack for this,
1503 * and run it entirely with interrupts hard disabled.
1504 */
1505EXC_COMMON_BEGIN(soft_nmi_common)
1506        mr      r10,r1
1507        ld      r1,PACAEMERGSP(r13)
1508        subi    r1,r1,INT_FRAME_SIZE
1509        EXCEPTION_COMMON_NORET_STACK(PACA_EXGEN, 0x900,
1510                        system_reset, soft_nmi_interrupt,
1511                        ADD_NVGPRS;ADD_RECONCILE)
1512        b       ret_from_except
1513
1514#else /* CONFIG_PPC_WATCHDOG */
1515#define MASKED_DEC_HANDLER_LABEL 2f /* normal return */
1516#define MASKED_DEC_HANDLER(_H)
1517#endif /* CONFIG_PPC_WATCHDOG */
1518
1519/*
1520 * An interrupt came in while soft-disabled. We set paca->irq_happened, then:
1521 * - If it was a decrementer interrupt, we bump the dec to max and and return.
1522 * - If it was a doorbell we return immediately since doorbells are edge
1523 *   triggered and won't automatically refire.
1524 * - If it was a HMI we return immediately since we handled it in realmode
1525 *   and it won't refire.
1526 * - Else it is one of PACA_IRQ_MUST_HARD_MASK, so hard disable and return.
1527 * This is called with r10 containing the value to OR to the paca field.
1528 */
1529#define MASKED_INTERRUPT(_H)                            \
1530masked_##_H##interrupt:                                 \
1531        std     r11,PACA_EXGEN+EX_R11(r13);             \
1532        lbz     r11,PACAIRQHAPPENED(r13);               \
1533        or      r11,r11,r10;                            \
1534        stb     r11,PACAIRQHAPPENED(r13);               \
1535        cmpwi   r10,PACA_IRQ_DEC;                       \
1536        bne     1f;                                     \
1537        lis     r10,0x7fff;                             \
1538        ori     r10,r10,0xffff;                         \
1539        mtspr   SPRN_DEC,r10;                           \
1540        b       MASKED_DEC_HANDLER_LABEL;               \
15411:      andi.   r10,r10,PACA_IRQ_MUST_HARD_MASK;        \
1542        beq     2f;                                     \
1543        mfspr   r10,SPRN_##_H##SRR1;                    \
1544        xori    r10,r10,MSR_EE; /* clear MSR_EE */      \
1545        mtspr   SPRN_##_H##SRR1,r10;                    \
15462:      mtcrf   0x80,r9;                                \
1547        std     r1,PACAR1(r13);                         \
1548        ld      r9,PACA_EXGEN+EX_R9(r13);               \
1549        ld      r10,PACA_EXGEN+EX_R10(r13);             \
1550        ld      r11,PACA_EXGEN+EX_R11(r13);             \
1551        /* returns to kernel where r13 must be set up, so don't restore it */ \
1552        ##_H##RFI_TO_KERNEL;                            \
1553        b       .;                                      \
1554        MASKED_DEC_HANDLER(_H)
1555
1556TRAMP_REAL_BEGIN(stf_barrier_fallback)
1557        std     r9,PACA_EXRFI+EX_R9(r13)
1558        std     r10,PACA_EXRFI+EX_R10(r13)
1559        sync
1560        ld      r9,PACA_EXRFI+EX_R9(r13)
1561        ld      r10,PACA_EXRFI+EX_R10(r13)
1562        ori     31,31,0
1563        .rept 14
1564        b       1f
15651:
1566        .endr
1567        blr
1568
1569TRAMP_REAL_BEGIN(rfi_flush_fallback)
1570        SET_SCRATCH0(r13);
1571        GET_PACA(r13);
1572        std     r9,PACA_EXRFI+EX_R9(r13)
1573        std     r10,PACA_EXRFI+EX_R10(r13)
1574        std     r11,PACA_EXRFI+EX_R11(r13)
1575        mfctr   r9
1576        ld      r10,PACA_RFI_FLUSH_FALLBACK_AREA(r13)
1577        ld      r11,PACA_L1D_FLUSH_SIZE(r13)
1578        srdi    r11,r11,(7 + 3) /* 128 byte lines, unrolled 8x */
1579        mtctr   r11
1580        DCBT_BOOK3S_STOP_ALL_STREAM_IDS(r11) /* Stop prefetch streams */
1581
1582        /* order ld/st prior to dcbt stop all streams with flushing */
1583        sync
1584
1585        /*
1586         * The load adresses are at staggered offsets within cachelines,
1587         * which suits some pipelines better (on others it should not
1588         * hurt).
1589         */
15901:
1591        ld      r11,(0x80 + 8)*0(r10)
1592        ld      r11,(0x80 + 8)*1(r10)
1593        ld      r11,(0x80 + 8)*2(r10)
1594        ld      r11,(0x80 + 8)*3(r10)
1595        ld      r11,(0x80 + 8)*4(r10)
1596        ld      r11,(0x80 + 8)*5(r10)
1597        ld      r11,(0x80 + 8)*6(r10)
1598        ld      r11,(0x80 + 8)*7(r10)
1599        addi    r10,r10,0x80*8
1600        bdnz    1b
1601
1602        mtctr   r9
1603        ld      r9,PACA_EXRFI+EX_R9(r13)
1604        ld      r10,PACA_EXRFI+EX_R10(r13)
1605        ld      r11,PACA_EXRFI+EX_R11(r13)
1606        GET_SCRATCH0(r13);
1607        rfid
1608
1609TRAMP_REAL_BEGIN(hrfi_flush_fallback)
1610        SET_SCRATCH0(r13);
1611        GET_PACA(r13);
1612        std     r9,PACA_EXRFI+EX_R9(r13)
1613        std     r10,PACA_EXRFI+EX_R10(r13)
1614        std     r11,PACA_EXRFI+EX_R11(r13)
1615        mfctr   r9
1616        ld      r10,PACA_RFI_FLUSH_FALLBACK_AREA(r13)
1617        ld      r11,PACA_L1D_FLUSH_SIZE(r13)
1618        srdi    r11,r11,(7 + 3) /* 128 byte lines, unrolled 8x */
1619        mtctr   r11
1620        DCBT_BOOK3S_STOP_ALL_STREAM_IDS(r11) /* Stop prefetch streams */
1621
1622        /* order ld/st prior to dcbt stop all streams with flushing */
1623        sync
1624
1625        /*
1626         * The load adresses are at staggered offsets within cachelines,
1627         * which suits some pipelines better (on others it should not
1628         * hurt).
1629         */
16301:
1631        ld      r11,(0x80 + 8)*0(r10)
1632        ld      r11,(0x80 + 8)*1(r10)
1633        ld      r11,(0x80 + 8)*2(r10)
1634        ld      r11,(0x80 + 8)*3(r10)
1635        ld      r11,(0x80 + 8)*4(r10)
1636        ld      r11,(0x80 + 8)*5(r10)
1637        ld      r11,(0x80 + 8)*6(r10)
1638        ld      r11,(0x80 + 8)*7(r10)
1639        addi    r10,r10,0x80*8
1640        bdnz    1b
1641
1642        mtctr   r9
1643        ld      r9,PACA_EXRFI+EX_R9(r13)
1644        ld      r10,PACA_EXRFI+EX_R10(r13)
1645        ld      r11,PACA_EXRFI+EX_R11(r13)
1646        GET_SCRATCH0(r13);
1647        hrfid
1648
1649/*
1650 * Real mode exceptions actually use this too, but alternate
1651 * instruction code patches (which end up in the common .text area)
1652 * cannot reach these if they are put there.
1653 */
1654USE_FIXED_SECTION(virt_trampolines)
1655        MASKED_INTERRUPT()
1656        MASKED_INTERRUPT(H)
1657
1658#ifdef CONFIG_KVM_BOOK3S_64_HANDLER
1659TRAMP_REAL_BEGIN(kvmppc_skip_interrupt)
1660        /*
1661         * Here all GPRs are unchanged from when the interrupt happened
1662         * except for r13, which is saved in SPRG_SCRATCH0.
1663         */
1664        mfspr   r13, SPRN_SRR0
1665        addi    r13, r13, 4
1666        mtspr   SPRN_SRR0, r13
1667        GET_SCRATCH0(r13)
1668        RFI_TO_KERNEL
1669        b       .
1670
1671TRAMP_REAL_BEGIN(kvmppc_skip_Hinterrupt)
1672        /*
1673         * Here all GPRs are unchanged from when the interrupt happened
1674         * except for r13, which is saved in SPRG_SCRATCH0.
1675         */
1676        mfspr   r13, SPRN_HSRR0
1677        addi    r13, r13, 4
1678        mtspr   SPRN_HSRR0, r13
1679        GET_SCRATCH0(r13)
1680        HRFI_TO_KERNEL
1681        b       .
1682#endif
1683
1684/*
1685 * Ensure that any handlers that get invoked from the exception prologs
1686 * above are below the first 64KB (0x10000) of the kernel image because
1687 * the prologs assemble the addresses of these handlers using the
1688 * LOAD_HANDLER macro, which uses an ori instruction.
1689 */
1690
1691/*** Common interrupt handlers ***/
1692
1693
1694        /*
1695         * Relocation-on interrupts: A subset of the interrupts can be delivered
1696         * with IR=1/DR=1, if AIL==2 and MSR.HV won't be changed by delivering
1697         * it.  Addresses are the same as the original interrupt addresses, but
1698         * offset by 0xc000000000004000.
1699         * It's impossible to receive interrupts below 0x300 via this mechanism.
1700         * KVM: None of these traps are from the guest ; anything that escalated
1701         * to HV=1 from HV=0 is delivered via real mode handlers.
1702         */
1703
1704        /*
1705         * This uses the standard macro, since the original 0x300 vector
1706         * only has extra guff for STAB-based processors -- which never
1707         * come here.
1708         */
1709
1710EXC_COMMON_BEGIN(ppc64_runlatch_on_trampoline)
1711        b       __ppc64_runlatch_on
1712
1713USE_FIXED_SECTION(virt_trampolines)
1714        /*
1715         * The __end_interrupts marker must be past the out-of-line (OOL)
1716         * handlers, so that they are copied to real address 0x100 when running
1717         * a relocatable kernel. This ensures they can be reached from the short
1718         * trampoline handlers (like 0x4f00, 0x4f20, etc.) which branch
1719         * directly, without using LOAD_HANDLER().
1720         */
1721        .align  7
1722        .globl  __end_interrupts
1723__end_interrupts:
1724DEFINE_FIXED_SYMBOL(__end_interrupts)
1725
1726#ifdef CONFIG_PPC_970_NAP
1727EXC_COMMON_BEGIN(power4_fixup_nap)
1728        andc    r9,r9,r10
1729        std     r9,TI_LOCAL_FLAGS(r11)
1730        ld      r10,_LINK(r1)           /* make idle task do the */
1731        std     r10,_NIP(r1)            /* equivalent of a blr */
1732        blr
1733#endif
1734
1735CLOSE_FIXED_SECTION(real_vectors);
1736CLOSE_FIXED_SECTION(real_trampolines);
1737CLOSE_FIXED_SECTION(virt_vectors);
1738CLOSE_FIXED_SECTION(virt_trampolines);
1739
1740USE_TEXT_SECTION()
1741
1742/*
1743 * Hash table stuff
1744 */
1745        .balign IFETCH_ALIGN_BYTES
1746do_hash_page:
1747#ifdef CONFIG_PPC_BOOK3S_64
1748        lis     r0,(DSISR_BAD_FAULT_64S | DSISR_DABRMATCH | DSISR_KEYFAULT)@h
1749        ori     r0,r0,DSISR_BAD_FAULT_64S@l
1750        and.    r0,r4,r0                /* weird error? */
1751        bne-    handle_page_fault       /* if not, try to insert a HPTE */
1752        CURRENT_THREAD_INFO(r11, r1)
1753        lwz     r0,TI_PREEMPT(r11)      /* If we're in an "NMI" */
1754        andis.  r0,r0,NMI_MASK@h        /* (i.e. an irq when soft-disabled) */
1755        bne     77f                     /* then don't call hash_page now */
1756
1757        /*
1758         * r3 contains the faulting address
1759         * r4 msr
1760         * r5 contains the trap number
1761         * r6 contains dsisr
1762         *
1763         * at return r3 = 0 for success, 1 for page fault, negative for error
1764         */
1765        mr      r4,r12
1766        ld      r6,_DSISR(r1)
1767        bl      __hash_page             /* build HPTE if possible */
1768        cmpdi   r3,0                    /* see if __hash_page succeeded */
1769
1770        /* Success */
1771        beq     fast_exc_return_irq     /* Return from exception on success */
1772
1773        /* Error */
1774        blt-    13f
1775
1776        /* Reload DSISR into r4 for the DABR check below */
1777        ld      r4,_DSISR(r1)
1778#endif /* CONFIG_PPC_BOOK3S_64 */
1779
1780/* Here we have a page fault that hash_page can't handle. */
1781handle_page_fault:
178211:     andis.  r0,r4,DSISR_DABRMATCH@h
1783        bne-    handle_dabr_fault
1784        ld      r4,_DAR(r1)
1785        ld      r5,_DSISR(r1)
1786        addi    r3,r1,STACK_FRAME_OVERHEAD
1787        bl      do_page_fault
1788        cmpdi   r3,0
1789        beq+    ret_from_except_lite
1790        bl      save_nvgprs
1791        mr      r5,r3
1792        addi    r3,r1,STACK_FRAME_OVERHEAD
1793        lwz     r4,_DAR(r1)
1794        bl      bad_page_fault
1795        b       ret_from_except
1796
1797/* We have a data breakpoint exception - handle it */
1798handle_dabr_fault:
1799        bl      save_nvgprs
1800        ld      r4,_DAR(r1)
1801        ld      r5,_DSISR(r1)
1802        addi    r3,r1,STACK_FRAME_OVERHEAD
1803        bl      do_break
1804        /*
1805         * do_break() may have changed the NV GPRS while handling a breakpoint.
1806         * If so, we need to restore them with their updated values. Don't use
1807         * ret_from_except_lite here.
1808         */
1809        b       ret_from_except
1810
1811
1812#ifdef CONFIG_PPC_BOOK3S_64
1813/* We have a page fault that hash_page could handle but HV refused
1814 * the PTE insertion
1815 */
181613:     bl      save_nvgprs
1817        mr      r5,r3
1818        addi    r3,r1,STACK_FRAME_OVERHEAD
1819        ld      r4,_DAR(r1)
1820        bl      low_hash_fault
1821        b       ret_from_except
1822#endif
1823
1824/*
1825 * We come here as a result of a DSI at a point where we don't want
1826 * to call hash_page, such as when we are accessing memory (possibly
1827 * user memory) inside a PMU interrupt that occurred while interrupts
1828 * were soft-disabled.  We want to invoke the exception handler for
1829 * the access, or panic if there isn't a handler.
1830 */
183177:     bl      save_nvgprs
1832        mr      r4,r3
1833        addi    r3,r1,STACK_FRAME_OVERHEAD
1834        li      r5,SIGSEGV
1835        bl      bad_page_fault
1836        b       ret_from_except
1837
1838/*
1839 * Here we have detected that the kernel stack pointer is bad.
1840 * R9 contains the saved CR, r13 points to the paca,
1841 * r10 contains the (bad) kernel stack pointer,
1842 * r11 and r12 contain the saved SRR0 and SRR1.
1843 * We switch to using an emergency stack, save the registers there,
1844 * and call kernel_bad_stack(), which panics.
1845 */
1846bad_stack:
1847        ld      r1,PACAEMERGSP(r13)
1848        subi    r1,r1,64+INT_FRAME_SIZE
1849        std     r9,_CCR(r1)
1850        std     r10,GPR1(r1)
1851        std     r11,_NIP(r1)
1852        std     r12,_MSR(r1)
1853        mfspr   r11,SPRN_DAR
1854        mfspr   r12,SPRN_DSISR
1855        std     r11,_DAR(r1)
1856        std     r12,_DSISR(r1)
1857        mflr    r10
1858        mfctr   r11
1859        mfxer   r12
1860        std     r10,_LINK(r1)
1861        std     r11,_CTR(r1)
1862        std     r12,_XER(r1)
1863        SAVE_GPR(0,r1)
1864        SAVE_GPR(2,r1)
1865        ld      r10,EX_R3(r3)
1866        std     r10,GPR3(r1)
1867        SAVE_GPR(4,r1)
1868        SAVE_4GPRS(5,r1)
1869        ld      r9,EX_R9(r3)
1870        ld      r10,EX_R10(r3)
1871        SAVE_2GPRS(9,r1)
1872        ld      r9,EX_R11(r3)
1873        ld      r10,EX_R12(r3)
1874        ld      r11,EX_R13(r3)
1875        std     r9,GPR11(r1)
1876        std     r10,GPR12(r1)
1877        std     r11,GPR13(r1)
1878BEGIN_FTR_SECTION
1879        ld      r10,EX_CFAR(r3)
1880        std     r10,ORIG_GPR3(r1)
1881END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
1882        SAVE_8GPRS(14,r1)
1883        SAVE_10GPRS(22,r1)
1884        lhz     r12,PACA_TRAP_SAVE(r13)
1885        std     r12,_TRAP(r1)
1886        addi    r11,r1,INT_FRAME_SIZE
1887        std     r11,0(r1)
1888        li      r12,0
1889        std     r12,0(r11)
1890        ld      r2,PACATOC(r13)
1891        ld      r11,exception_marker@toc(r2)
1892        std     r12,RESULT(r1)
1893        std     r11,STACK_FRAME_OVERHEAD-16(r1)
18941:      addi    r3,r1,STACK_FRAME_OVERHEAD
1895        bl      kernel_bad_stack
1896        b       1b
1897_ASM_NOKPROBE_SYMBOL(bad_stack);
1898
1899/*
1900 * When doorbell is triggered from system reset wakeup, the message is
1901 * not cleared, so it would fire again when EE is enabled.
1902 *
1903 * When coming from local_irq_enable, there may be the same problem if
1904 * we were hard disabled.
1905 *
1906 * Execute msgclr to clear pending exceptions before handling it.
1907 */
1908h_doorbell_common_msgclr:
1909        LOAD_REG_IMMEDIATE(r3, PPC_DBELL_MSGTYPE << (63-36))
1910        PPC_MSGCLR(3)
1911        b       h_doorbell_common
1912
1913doorbell_super_common_msgclr:
1914        LOAD_REG_IMMEDIATE(r3, PPC_DBELL_MSGTYPE << (63-36))
1915        PPC_MSGCLRP(3)
1916        b       doorbell_super_common
1917
1918/*
1919 * Called from arch_local_irq_enable when an interrupt needs
1920 * to be resent. r3 contains 0x500, 0x900, 0xa00 or 0xe80 to indicate
1921 * which kind of interrupt. MSR:EE is already off. We generate a
1922 * stackframe like if a real interrupt had happened.
1923 *
1924 * Note: While MSR:EE is off, we need to make sure that _MSR
1925 * in the generated frame has EE set to 1 or the exception
1926 * handler will not properly re-enable them.
1927 *
1928 * Note that we don't specify LR as the NIP (return address) for
1929 * the interrupt because that would unbalance the return branch
1930 * predictor.
1931 */
1932_GLOBAL(__replay_interrupt)
1933        /* We are going to jump to the exception common code which
1934         * will retrieve various register values from the PACA which
1935         * we don't give a damn about, so we don't bother storing them.
1936         */
1937        mfmsr   r12
1938        LOAD_REG_ADDR(r11, replay_interrupt_return)
1939        mfcr    r9
1940        ori     r12,r12,MSR_EE
1941        cmpwi   r3,0x900
1942        beq     decrementer_common
1943        cmpwi   r3,0x500
1944BEGIN_FTR_SECTION
1945        beq     h_virt_irq_common
1946FTR_SECTION_ELSE
1947        beq     hardware_interrupt_common
1948ALT_FTR_SECTION_END_IFSET(CPU_FTR_HVMODE | CPU_FTR_ARCH_300)
1949        cmpwi   r3,0xf00
1950        beq     performance_monitor_common
1951BEGIN_FTR_SECTION
1952        cmpwi   r3,0xa00
1953        beq     h_doorbell_common_msgclr
1954        cmpwi   r3,0xe60
1955        beq     hmi_exception_common
1956FTR_SECTION_ELSE
1957        cmpwi   r3,0xa00
1958        beq     doorbell_super_common_msgclr
1959ALT_FTR_SECTION_END_IFSET(CPU_FTR_HVMODE)
1960replay_interrupt_return:
1961        blr
1962
1963_ASM_NOKPROBE_SYMBOL(__replay_interrupt)
1964