/*1* Copyright 2010 Tilera Corporation. All Rights Reserved.2*3* This program is free software; you can redistribute it and/or4* modify it under the terms of the GNU General Public License5* as published by the Free Software Foundation, version 2.6*7* This program is distributed in the hope that it will be useful, but8* WITHOUT ANY WARRANTY; without even the implied warranty of9* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or10* NON INFRINGEMENT. See the GNU General Public License for11* more details.12*13* Linux interrupt vectors.14*/1516#include <linux/linkage.h>17#include <linux/errno.h>18#include <linux/init.h>19#include <linux/unistd.h>20#include <asm/ptrace.h>21#include <asm/thread_info.h>22#include <asm/irqflags.h>23#include <asm/atomic.h>24#include <asm/asm-offsets.h>25#include <hv/hypervisor.h>26#include <arch/abi.h>27#include <arch/interrupts.h>28#include <arch/spr_def.h>2930#ifdef CONFIG_PREEMPT31# error "No support for kernel preemption currently"32#endif3334#define PTREGS_PTR(reg, ptreg) addli reg, sp, C_ABI_SAVE_AREA_SIZE + (ptreg)3536#define PTREGS_OFFSET_SYSCALL PTREGS_OFFSET_REG(TREG_SYSCALL_NR)3738#if !CHIP_HAS_WH64()39/* By making this an empty macro, we can use wh64 in the code. */40.macro wh64 reg41.endm42#endif4344.macro push_reg reg, ptr=sp, delta=-445{46sw \ptr, \reg47addli \ptr, \ptr, \delta48}49.endm5051.macro pop_reg reg, ptr=sp, delta=452{53lw \reg, \ptr54addli \ptr, \ptr, \delta55}56.endm5758.macro pop_reg_zero reg, zreg, ptr=sp, delta=459{60move \zreg, zero61lw \reg, \ptr62addi \ptr, \ptr, \delta63}64.endm6566.macro push_extra_callee_saves reg67PTREGS_PTR(\reg, PTREGS_OFFSET_REG(51))68push_reg r51, \reg69push_reg r50, \reg70push_reg r49, \reg71push_reg r48, \reg72push_reg r47, \reg73push_reg r46, \reg74push_reg r45, \reg75push_reg r44, \reg76push_reg r43, \reg77push_reg r42, \reg78push_reg r41, \reg79push_reg r40, \reg80push_reg r39, \reg81push_reg r38, \reg82push_reg r37, \reg83push_reg r36, \reg84push_reg r35, \reg85push_reg r34, \reg, PTREGS_OFFSET_BASE - PTREGS_OFFSET_REG(34)86.endm8788.macro panic str89.pushsection .rodata, "a"901:91.asciz "\str"92.popsection93{94moveli r0, lo16(1b)95}96{97auli r0, r0, ha16(1b)98jal panic99}100.endm101102#ifdef __COLLECT_LINKER_FEEDBACK__103.pushsection .text.intvec_feedback,"ax"104intvec_feedback:105.popsection106#endif107108/*109* Default interrupt handler.110*111* vecnum is where we'll put this code.112* c_routine is the C routine we'll call.113*114* The C routine is passed two arguments:115* - A pointer to the pt_regs state.116* - The interrupt vector number.117*118* The "processing" argument specifies the code for processing119* the interrupt. Defaults to "handle_interrupt".120*/121.macro int_hand vecnum, vecname, c_routine, processing=handle_interrupt122.org (\vecnum << 8)123intvec_\vecname:124.ifc \vecnum, INT_SWINT_1125blz TREG_SYSCALL_NR_NAME, sys_cmpxchg126.endif127128/* Temporarily save a register so we have somewhere to work. */129130mtspr SPR_SYSTEM_SAVE_K_1, r0131mfspr r0, SPR_EX_CONTEXT_K_1132133/* The cmpxchg code clears sp to force us to reset it here on fault. */134{135bz sp, 2f136andi r0, r0, SPR_EX_CONTEXT_1_1__PL_MASK /* mask off ICS */137}138139.ifc \vecnum, INT_DOUBLE_FAULT140/*141* For double-faults from user-space, fall through to the normal142* register save and stack setup path. Otherwise, it's the143* hypervisor giving us one last chance to dump diagnostics, and we144* branch to the kernel_double_fault routine to do so.145*/146bz r0, 1f147j _kernel_double_fault1481:149.else150/*151* If we're coming from user-space, then set sp to the top of152* the kernel stack. Otherwise, assume sp is already valid.153*/154{155bnz r0, 0f156move r0, sp157}158.endif159160.ifc \c_routine, do_page_fault161/*162* The page_fault handler may be downcalled directly by the163* hypervisor even when Linux is running and has ICS set.164*165* In this case the contents of EX_CONTEXT_K_1 reflect the166* previous fault and can't be relied on to choose whether or167* not to reinitialize the stack pointer. So we add a test168* to see whether SYSTEM_SAVE_K_2 has the high bit set,169* and if so we don't reinitialize sp, since we must be coming170* from Linux. (In fact the precise case is !(val & ~1),171* but any Linux PC has to have the high bit set.)172*173* Note that the hypervisor *always* sets SYSTEM_SAVE_K_2 for174* any path that turns into a downcall to one of our TLB handlers.175*/176mfspr r0, SPR_SYSTEM_SAVE_K_2177{178blz r0, 0f /* high bit in S_S_1_2 is for a PC to use */179move r0, sp180}181.endif1821832:184/*185* SYSTEM_SAVE_K_0 holds the cpu number in the low bits, and186* the current stack top in the higher bits. So we recover187* our stack top by just masking off the low bits, then188* point sp at the top aligned address on the actual stack page.189*/190mfspr r0, SPR_SYSTEM_SAVE_K_0191mm r0, r0, zero, LOG2_THREAD_SIZE, 311921930:194/*195* Align the stack mod 64 so we can properly predict what196* cache lines we need to write-hint to reduce memory fetch197* latency as we enter the kernel. The layout of memory is198* as follows, with cache line 0 at the lowest VA, and cache199* line 4 just below the r0 value this "andi" computes.200* Note that we never write to cache line 4, and we skip201* cache line 1 for syscalls.202*203* cache line 4: ptregs padding (two words)204* cache line 3: r46...lr, pc, ex1, faultnum, orig_r0, flags, pad205* cache line 2: r30...r45206* cache line 1: r14...r29207* cache line 0: 2 x frame, r0..r13208*/209andi r0, r0, -64210211/*212* Push the first four registers on the stack, so that we can set213* them to vector-unique values before we jump to the common code.214*215* Registers are pushed on the stack as a struct pt_regs,216* with the sp initially just above the struct, and when we're217* done, sp points to the base of the struct, minus218* C_ABI_SAVE_AREA_SIZE, so we can directly jal to C code.219*220* This routine saves just the first four registers, plus the221* stack context so we can do proper backtracing right away,222* and defers to handle_interrupt to save the rest.223* The backtracer needs pc, ex1, lr, sp, r52, and faultnum.224*/225addli r0, r0, PTREGS_OFFSET_LR - (PTREGS_SIZE + KSTK_PTREGS_GAP)226wh64 r0 /* cache line 3 */227{228sw r0, lr229addli r0, r0, PTREGS_OFFSET_SP - PTREGS_OFFSET_LR230}231{232sw r0, sp233addli sp, r0, PTREGS_OFFSET_REG(52) - PTREGS_OFFSET_SP234}235{236sw sp, r52237addli sp, sp, PTREGS_OFFSET_REG(1) - PTREGS_OFFSET_REG(52)238}239wh64 sp /* cache line 0 */240{241sw sp, r1242addli sp, sp, PTREGS_OFFSET_REG(2) - PTREGS_OFFSET_REG(1)243}244{245sw sp, r2246addli sp, sp, PTREGS_OFFSET_REG(3) - PTREGS_OFFSET_REG(2)247}248{249sw sp, r3250addli sp, sp, PTREGS_OFFSET_PC - PTREGS_OFFSET_REG(3)251}252mfspr r0, SPR_EX_CONTEXT_K_0253.ifc \processing,handle_syscall254/*255* Bump the saved PC by one bundle so that when we return, we won't256* execute the same swint instruction again. We need to do this while257* we're in the critical section.258*/259addi r0, r0, 8260.endif261{262sw sp, r0263addli sp, sp, PTREGS_OFFSET_EX1 - PTREGS_OFFSET_PC264}265mfspr r0, SPR_EX_CONTEXT_K_1266{267sw sp, r0268addi sp, sp, PTREGS_OFFSET_FAULTNUM - PTREGS_OFFSET_EX1269/*270* Use r0 for syscalls so it's a temporary; use r1 for interrupts271* so that it gets passed through unchanged to the handler routine.272* Note that the .if conditional confusingly spans bundles.273*/274.ifc \processing,handle_syscall275movei r0, \vecnum276}277{278sw sp, r0279.else280movei r1, \vecnum281}282{283sw sp, r1284.endif285addli sp, sp, PTREGS_OFFSET_REG(0) - PTREGS_OFFSET_FAULTNUM286}287mfspr r0, SPR_SYSTEM_SAVE_K_1 /* Original r0 */288{289sw sp, r0290addi sp, sp, -PTREGS_OFFSET_REG(0) - 4291}292{293sw sp, zero /* write zero into "Next SP" frame pointer */294addi sp, sp, -4 /* leave SP pointing at bottom of frame */295}296.ifc \processing,handle_syscall297j handle_syscall298.else299/*300* Capture per-interrupt SPR context to registers.301* We overload the meaning of r3 on this path such that if its bit 31302* is set, we have to mask all interrupts including NMIs before303* clearing the interrupt critical section bit.304* See discussion below at "finish_interrupt_save".305*/306.ifc \c_routine, do_page_fault307mfspr r2, SPR_SYSTEM_SAVE_K_3 /* address of page fault */308mfspr r3, SPR_SYSTEM_SAVE_K_2 /* info about page fault */309.else310.ifc \vecnum, INT_DOUBLE_FAULT311{312mfspr r2, SPR_SYSTEM_SAVE_K_2 /* double fault info from HV */313movei r3, 0314}315.else316.ifc \c_routine, do_trap317{318mfspr r2, GPV_REASON319movei r3, 0320}321.else322.ifc \c_routine, op_handle_perf_interrupt323{324mfspr r2, PERF_COUNT_STS325movei r3, -1 /* not used, but set for consistency */326}327.else328#if CHIP_HAS_AUX_PERF_COUNTERS()329.ifc \c_routine, op_handle_aux_perf_interrupt330{331mfspr r2, AUX_PERF_COUNT_STS332movei r3, -1 /* not used, but set for consistency */333}334.else335#endif336movei r3, 0337#if CHIP_HAS_AUX_PERF_COUNTERS()338.endif339#endif340.endif341.endif342.endif343.endif344/* Put function pointer in r0 */345moveli r0, lo16(\c_routine)346{347auli r0, r0, ha16(\c_routine)348j \processing349}350.endif351ENDPROC(intvec_\vecname)352353#ifdef __COLLECT_LINKER_FEEDBACK__354.pushsection .text.intvec_feedback,"ax"355.org (\vecnum << 5)356FEEDBACK_ENTER_EXPLICIT(intvec_\vecname, .intrpt1, 1 << 8)357jrp lr358.popsection359#endif360361.endm362363364/*365* Save the rest of the registers that we didn't save in the actual366* vector itself. We can't use r0-r10 inclusive here.367*/368.macro finish_interrupt_save, function369370/* If it's a syscall, save a proper orig_r0, otherwise just zero. */371PTREGS_PTR(r52, PTREGS_OFFSET_ORIG_R0)372{373.ifc \function,handle_syscall374sw r52, r0375.else376sw r52, zero377.endif378PTREGS_PTR(r52, PTREGS_OFFSET_TP)379}380381/*382* For ordinary syscalls, we save neither caller- nor callee-383* save registers, since the syscall invoker doesn't expect the384* caller-saves to be saved, and the called kernel functions will385* take care of saving the callee-saves for us.386*387* For interrupts we save just the caller-save registers. Saving388* them is required (since the "caller" can't save them). Again,389* the called kernel functions will restore the callee-save390* registers for us appropriately.391*392* On return, we normally restore nothing special for syscalls,393* and just the caller-save registers for interrupts.394*395* However, there are some important caveats to all this:396*397* - We always save a few callee-save registers to give us398* some scratchpad registers to carry across function calls.399*400* - fork/vfork/etc require us to save all the callee-save401* registers, which we do in PTREGS_SYSCALL_ALL_REGS, below.402*403* - We always save r0..r5 and r10 for syscalls, since we need404* to reload them a bit later for the actual kernel call, and405* since we might need them for -ERESTARTNOINTR, etc.406*407* - Before invoking a signal handler, we save the unsaved408* callee-save registers so they are visible to the409* signal handler or any ptracer.410*411* - If the unsaved callee-save registers are modified, we set412* a bit in pt_regs so we know to reload them from pt_regs413* and not just rely on the kernel function unwinding.414* (Done for ptrace register writes and SA_SIGINFO handler.)415*/416{417sw r52, tp418PTREGS_PTR(r52, PTREGS_OFFSET_REG(33))419}420wh64 r52 /* cache line 2 */421push_reg r33, r52422push_reg r32, r52423push_reg r31, r52424.ifc \function,handle_syscall425push_reg r30, r52, PTREGS_OFFSET_SYSCALL - PTREGS_OFFSET_REG(30)426push_reg TREG_SYSCALL_NR_NAME, r52, \427PTREGS_OFFSET_REG(5) - PTREGS_OFFSET_SYSCALL428.else429430push_reg r30, r52, PTREGS_OFFSET_REG(29) - PTREGS_OFFSET_REG(30)431wh64 r52 /* cache line 1 */432push_reg r29, r52433push_reg r28, r52434push_reg r27, r52435push_reg r26, r52436push_reg r25, r52437push_reg r24, r52438push_reg r23, r52439push_reg r22, r52440push_reg r21, r52441push_reg r20, r52442push_reg r19, r52443push_reg r18, r52444push_reg r17, r52445push_reg r16, r52446push_reg r15, r52447push_reg r14, r52448push_reg r13, r52449push_reg r12, r52450push_reg r11, r52451push_reg r10, r52452push_reg r9, r52453push_reg r8, r52454push_reg r7, r52455push_reg r6, r52456457.endif458459push_reg r5, r52460sw r52, r4461462/* Load tp with our per-cpu offset. */463#ifdef CONFIG_SMP464{465mfspr r20, SPR_SYSTEM_SAVE_K_0466moveli r21, lo16(__per_cpu_offset)467}468{469auli r21, r21, ha16(__per_cpu_offset)470mm r20, r20, zero, 0, LOG2_THREAD_SIZE-1471}472s2a r20, r20, r21473lw tp, r20474#else475move tp, zero476#endif477478/*479* If we will be returning to the kernel, we will need to480* reset the interrupt masks to the state they had before.481* Set DISABLE_IRQ in flags iff we came from PL1 with irqs disabled.482* We load flags in r32 here so we can jump to .Lrestore_regs483* directly after do_page_fault_ics() if necessary.484*/485mfspr r32, SPR_EX_CONTEXT_K_1486{487andi r32, r32, SPR_EX_CONTEXT_1_1__PL_MASK /* mask off ICS */488PTREGS_PTR(r21, PTREGS_OFFSET_FLAGS)489}490bzt r32, 1f /* zero if from user space */491IRQS_DISABLED(r32) /* zero if irqs enabled */492#if PT_FLAGS_DISABLE_IRQ != 1493# error Value of IRQS_DISABLED used to set PT_FLAGS_DISABLE_IRQ; fix494#endif4951:496.ifnc \function,handle_syscall497/* Record the fact that we saved the caller-save registers above. */498ori r32, r32, PT_FLAGS_CALLER_SAVES499.endif500sw r21, r32501502#ifdef __COLLECT_LINKER_FEEDBACK__503/*504* Notify the feedback routines that we were in the505* appropriate fixed interrupt vector area. Note that we506* still have ICS set at this point, so we can't invoke any507* atomic operations or we will panic. The feedback508* routines internally preserve r0..r10 and r30 up.509*/510.ifnc \function,handle_syscall511shli r20, r1, 5512.else513moveli r20, INT_SWINT_1 << 5514.endif515addli r20, r20, lo16(intvec_feedback)516auli r20, r20, ha16(intvec_feedback)517jalr r20518519/* And now notify the feedback routines that we are here. */520FEEDBACK_ENTER(\function)521#endif522523/*524* we've captured enough state to the stack (including in525* particular our EX_CONTEXT state) that we can now release526* the interrupt critical section and replace it with our527* standard "interrupts disabled" mask value. This allows528* synchronous interrupts (and profile interrupts) to punch529* through from this point onwards.530*531* If bit 31 of r3 is set during a non-NMI interrupt, we know we532* are on the path where the hypervisor has punched through our533* ICS with a page fault, so we call out to do_page_fault_ics()534* to figure out what to do with it. If the fault was in535* an atomic op, we unlock the atomic lock, adjust the536* saved register state a little, and return "zero" in r4,537* falling through into the normal page-fault interrupt code.538* If the fault was in a kernel-space atomic operation, then539* do_page_fault_ics() resolves it itself, returns "one" in r4,540* and as a result goes directly to restoring registers and iret,541* without trying to adjust the interrupt masks at all.542* The do_page_fault_ics() API involves passing and returning543* a five-word struct (in registers) to avoid writing the544* save and restore code here.545*/546.ifc \function,handle_nmi547IRQ_DISABLE_ALL(r20)548.else549.ifnc \function,handle_syscall550bgezt r3, 1f551{552PTREGS_PTR(r0, PTREGS_OFFSET_BASE)553jal do_page_fault_ics554}555FEEDBACK_REENTER(\function)556bzt r4, 1f557j .Lrestore_regs5581:559.endif560IRQ_DISABLE(r20, r21)561.endif562mtspr INTERRUPT_CRITICAL_SECTION, zero563564#if CHIP_HAS_WH64()565/*566* Prepare the first 256 stack bytes to be rapidly accessible567* without having to fetch the background data. We don't really568* know how far to write-hint, but kernel stacks generally569* aren't that big, and write-hinting here does take some time.570*/571addi r52, sp, -64572{573wh64 r52574addi r52, r52, -64575}576{577wh64 r52578addi r52, r52, -64579}580{581wh64 r52582addi r52, r52, -64583}584wh64 r52585#endif586587#ifdef CONFIG_TRACE_IRQFLAGS588.ifnc \function,handle_nmi589/*590* We finally have enough state set up to notify the irq591* tracing code that irqs were disabled on entry to the handler.592* The TRACE_IRQS_OFF call clobbers registers r0-r29.593* For syscalls, we already have the register state saved away594* on the stack, so we don't bother to do any register saves here,595* and later we pop the registers back off the kernel stack.596* For interrupt handlers, save r0-r3 in callee-saved registers.597*/598.ifnc \function,handle_syscall599{ move r30, r0; move r31, r1 }600{ move r32, r2; move r33, r3 }601.endif602TRACE_IRQS_OFF603.ifnc \function,handle_syscall604{ move r0, r30; move r1, r31 }605{ move r2, r32; move r3, r33 }606.endif607.endif608#endif609610.endm611612.macro check_single_stepping, kind, not_single_stepping613/*614* Check for single stepping in user-level priv615* kind can be "normal", "ill", or "syscall"616* At end, if fall-thru617* r29: thread_info->step_state618* r28: &pt_regs->pc619* r27: pt_regs->pc620* r26: thread_info->step_state->buffer621*/622623/* Check for single stepping */624GET_THREAD_INFO(r29)625{626/* Get pointer to field holding step state */627addi r29, r29, THREAD_INFO_STEP_STATE_OFFSET628629/* Get pointer to EX1 in register state */630PTREGS_PTR(r27, PTREGS_OFFSET_EX1)631}632{633/* Get pointer to field holding PC */634PTREGS_PTR(r28, PTREGS_OFFSET_PC)635636/* Load the pointer to the step state */637lw r29, r29638}639/* Load EX1 */640lw r27, r27641{642/* Points to flags */643addi r23, r29, SINGLESTEP_STATE_FLAGS_OFFSET644645/* No single stepping if there is no step state structure */646bzt r29, \not_single_stepping647}648{649/* mask off ICS and any other high bits */650andi r27, r27, SPR_EX_CONTEXT_1_1__PL_MASK651652/* Load pointer to single step instruction buffer */653lw r26, r29654}655/* Check priv state */656bnz r27, \not_single_stepping657658/* Get flags */659lw r22, r23660{661/* Branch if single-step mode not enabled */662bbnst r22, \not_single_stepping663664/* Clear enabled flag */665andi r22, r22, ~SINGLESTEP_STATE_MASK_IS_ENABLED666}667.ifc \kind,normal668{669/* Load PC */670lw r27, r28671672/* Point to the entry containing the original PC */673addi r24, r29, SINGLESTEP_STATE_ORIG_PC_OFFSET674}675{676/* Disable single stepping flag */677sw r23, r22678}679{680/* Get the original pc */681lw r24, r24682683/* See if the PC is at the start of the single step buffer */684seq r25, r26, r27685}686/*687* NOTE: it is really expected that the PC be in the single step buffer688* at this point689*/690bzt r25, \not_single_stepping691692/* Restore the original PC */693sw r28, r24694.else695.ifc \kind,syscall696{697/* Load PC */698lw r27, r28699700/* Point to the entry containing the next PC */701addi r24, r29, SINGLESTEP_STATE_NEXT_PC_OFFSET702}703{704/* Increment the stopped PC by the bundle size */705addi r26, r26, 8706707/* Disable single stepping flag */708sw r23, r22709}710{711/* Get the next pc */712lw r24, r24713714/*715* See if the PC is one bundle past the start of the716* single step buffer717*/718seq r25, r26, r27719}720{721/*722* NOTE: it is really expected that the PC be in the723* single step buffer at this point724*/725bzt r25, \not_single_stepping726}727/* Set to the next PC */728sw r28, r24729.else730{731/* Point to 3rd bundle in buffer */732addi r25, r26, 16733734/* Load PC */735lw r27, r28736}737{738/* Disable single stepping flag */739sw r23, r22740741/* See if the PC is in the single step buffer */742slte_u r24, r26, r27743}744{745slte_u r25, r27, r25746747/*748* NOTE: it is really expected that the PC be in the749* single step buffer at this point750*/751bzt r24, \not_single_stepping752}753bzt r25, \not_single_stepping754.endif755.endif756.endm757758/*759* Redispatch a downcall.760*/761.macro dc_dispatch vecnum, vecname762.org (\vecnum << 8)763intvec_\vecname:764j hv_downcall_dispatch765ENDPROC(intvec_\vecname)766.endm767768/*769* Common code for most interrupts. The C function we're eventually770* going to is in r0, and the faultnum is in r1; the original771* values for those registers are on the stack.772*/773.pushsection .text.handle_interrupt,"ax"774handle_interrupt:775finish_interrupt_save handle_interrupt776777/*778* Check for if we are single stepping in user level. If so, then779* we need to restore the PC.780*/781782check_single_stepping normal, .Ldispatch_interrupt783.Ldispatch_interrupt:784785/* Jump to the C routine; it should enable irqs as soon as possible. */786{787jalr r0788PTREGS_PTR(r0, PTREGS_OFFSET_BASE)789}790FEEDBACK_REENTER(handle_interrupt)791{792movei r30, 0 /* not an NMI */793j interrupt_return794}795STD_ENDPROC(handle_interrupt)796797/*798* This routine takes a boolean in r30 indicating if this is an NMI.799* If so, we also expect a boolean in r31 indicating whether to800* re-enable the oprofile interrupts.801*/802STD_ENTRY(interrupt_return)803/* If we're resuming to kernel space, don't check thread flags. */804{805bnz r30, .Lrestore_all /* NMIs don't special-case user-space */806PTREGS_PTR(r29, PTREGS_OFFSET_EX1)807}808lw r29, r29809andi r29, r29, SPR_EX_CONTEXT_1_1__PL_MASK /* mask off ICS */810{811bzt r29, .Lresume_userspace812PTREGS_PTR(r29, PTREGS_OFFSET_PC)813}814815/* If we're resuming to _cpu_idle_nap, bump PC forward by 8. */816{817lw r28, r29818moveli r27, lo16(_cpu_idle_nap)819}820{821auli r27, r27, ha16(_cpu_idle_nap)822}823{824seq r27, r27, r28825}826{827bbns r27, .Lrestore_all828addi r28, r28, 8829}830sw r29, r28831j .Lrestore_all832833.Lresume_userspace:834FEEDBACK_REENTER(interrupt_return)835836/*837* Disable interrupts so as to make sure we don't838* miss an interrupt that sets any of the thread flags (like839* need_resched or sigpending) between sampling and the iret.840* Routines like schedule() or do_signal() may re-enable841* interrupts before returning.842*/843IRQ_DISABLE(r20, r21)844TRACE_IRQS_OFF /* Note: clobbers registers r0-r29 */845846/* Get base of stack in r32; note r30/31 are used as arguments here. */847GET_THREAD_INFO(r32)848849850/* Check to see if there is any work to do before returning to user. */851{852addi r29, r32, THREAD_INFO_FLAGS_OFFSET853moveli r1, lo16(_TIF_ALLWORK_MASK)854}855{856lw r29, r29857auli r1, r1, ha16(_TIF_ALLWORK_MASK)858}859and r1, r29, r1860bzt r1, .Lrestore_all861862/*863* Make sure we have all the registers saved for signal864* handling or single-step. Call out to C code to figure out865* exactly what we need to do for each flag bit, then if866* necessary, reload the flags and recheck.867*/868push_extra_callee_saves r0869{870PTREGS_PTR(r0, PTREGS_OFFSET_BASE)871jal do_work_pending872}873bnz r0, .Lresume_userspace874875/*876* In the NMI case we877* omit the call to single_process_check_nohz, which normally checks878* to see if we should start or stop the scheduler tick, because879* we can't call arbitrary Linux code from an NMI context.880* We always call the homecache TLB deferral code to re-trigger881* the deferral mechanism.882*883* The other chunk of responsibility this code has is to reset the884* interrupt masks appropriately to reset irqs and NMIs. We have885* to call TRACE_IRQS_OFF and TRACE_IRQS_ON to support all the886* lockdep-type stuff, but we can't set ICS until afterwards, since887* ICS can only be used in very tight chunks of code to avoid888* tripping over various assertions that it is off.889*890* (There is what looks like a window of vulnerability here since891* we might take a profile interrupt between the two SPR writes892* that set the mask, but since we write the low SPR word first,893* and our interrupt entry code checks the low SPR word, any894* profile interrupt will actually disable interrupts in both SPRs895* before returning, which is OK.)896*/897.Lrestore_all:898PTREGS_PTR(r0, PTREGS_OFFSET_EX1)899{900lw r0, r0901PTREGS_PTR(r32, PTREGS_OFFSET_FLAGS)902}903{904andi r0, r0, SPR_EX_CONTEXT_1_1__PL_MASK905lw r32, r32906}907bnz r0, 1f908j 2f909#if PT_FLAGS_DISABLE_IRQ != 1910# error Assuming PT_FLAGS_DISABLE_IRQ == 1 so we can use bbnst below911#endif9121: bbnst r32, 2f913IRQ_DISABLE(r20,r21)914TRACE_IRQS_OFF915movei r0, 1916mtspr INTERRUPT_CRITICAL_SECTION, r0917bzt r30, .Lrestore_regs918j 3f9192: TRACE_IRQS_ON920movei r0, 1921mtspr INTERRUPT_CRITICAL_SECTION, r0922IRQ_ENABLE(r20, r21)923bzt r30, .Lrestore_regs9243:925926927/*928* We now commit to returning from this interrupt, since we will be929* doing things like setting EX_CONTEXT SPRs and unwinding the stack930* frame. No calls should be made to any other code after this point.931* This code should only be entered with ICS set.932* r32 must still be set to ptregs.flags.933* We launch loads to each cache line separately first, so we can934* get some parallelism out of the memory subsystem.935* We start zeroing caller-saved registers throughout, since936* that will save some cycles if this turns out to be a syscall.937*/938.Lrestore_regs:939FEEDBACK_REENTER(interrupt_return) /* called from elsewhere */940941/*942* Rotate so we have one high bit and one low bit to test.943* - low bit says whether to restore all the callee-saved registers,944* or just r30-r33, and r52 up.945* - high bit (i.e. sign bit) says whether to restore all the946* caller-saved registers, or just r0.947*/948#if PT_FLAGS_CALLER_SAVES != 2 || PT_FLAGS_RESTORE_REGS != 4949# error Rotate trick does not work :-)950#endif951{952rli r20, r32, 30953PTREGS_PTR(sp, PTREGS_OFFSET_REG(0))954}955956/*957* Load cache lines 0, 2, and 3 in that order, then use958* the last loaded value, which makes it likely that the other959* cache lines have also loaded, at which point we should be960* able to safely read all the remaining words on those cache961* lines without waiting for the memory subsystem.962*/963pop_reg_zero r0, r28, sp, PTREGS_OFFSET_REG(30) - PTREGS_OFFSET_REG(0)964pop_reg_zero r30, r2, sp, PTREGS_OFFSET_PC - PTREGS_OFFSET_REG(30)965pop_reg_zero r21, r3, sp, PTREGS_OFFSET_EX1 - PTREGS_OFFSET_PC966pop_reg_zero lr, r4, sp, PTREGS_OFFSET_REG(52) - PTREGS_OFFSET_EX1967{968mtspr SPR_EX_CONTEXT_K_0, r21969move r5, zero970}971{972mtspr SPR_EX_CONTEXT_K_1, lr973andi lr, lr, SPR_EX_CONTEXT_1_1__PL_MASK /* mask off ICS */974}975976/* Restore callee-saveds that we actually use. */977pop_reg_zero r52, r6, sp, PTREGS_OFFSET_REG(31) - PTREGS_OFFSET_REG(52)978pop_reg_zero r31, r7979pop_reg_zero r32, r8980pop_reg_zero r33, r9, sp, PTREGS_OFFSET_REG(29) - PTREGS_OFFSET_REG(33)981982/*983* If we modified other callee-saveds, restore them now.984* This is rare, but could be via ptrace or signal handler.985*/986{987move r10, zero988bbs r20, .Lrestore_callees989}990.Lcontinue_restore_regs:991992/* Check if we're returning from a syscall. */993{994move r11, zero995blzt r20, 1f /* no, so go restore callee-save registers */996}997998/*999* Check if we're returning to userspace.1000* Note that if we're not, we don't worry about zeroing everything.1001*/1002{1003addli sp, sp, PTREGS_OFFSET_LR - PTREGS_OFFSET_REG(29)1004bnz lr, .Lkernel_return1005}10061007/*1008* On return from syscall, we've restored r0 from pt_regs, but we1009* clear the remainder of the caller-saved registers. We could1010* restore the syscall arguments, but there's not much point,1011* and it ensures user programs aren't trying to use the1012* caller-saves if we clear them, as well as avoiding leaking1013* kernel pointers into userspace.1014*/1015pop_reg_zero lr, r12, sp, PTREGS_OFFSET_TP - PTREGS_OFFSET_LR1016pop_reg_zero tp, r13, sp, PTREGS_OFFSET_SP - PTREGS_OFFSET_TP1017{1018lw sp, sp1019move r14, zero1020move r15, zero1021}1022{ move r16, zero; move r17, zero }1023{ move r18, zero; move r19, zero }1024{ move r20, zero; move r21, zero }1025{ move r22, zero; move r23, zero }1026{ move r24, zero; move r25, zero }1027{ move r26, zero; move r27, zero }10281029/* Set r1 to errno if we are returning an error, otherwise zero. */1030{1031moveli r29, 40961032sub r1, zero, r01033}1034slt_u r29, r1, r291035{1036mnz r1, r29, r11037move r29, zero1038}1039iret10401041/*1042* Not a syscall, so restore caller-saved registers.1043* First kick off a load for cache line 1, which we're touching1044* for the first time here.1045*/1046.align 6410471: pop_reg r29, sp, PTREGS_OFFSET_REG(1) - PTREGS_OFFSET_REG(29)1048pop_reg r11049pop_reg r21050pop_reg r31051pop_reg r41052pop_reg r51053pop_reg r61054pop_reg r71055pop_reg r81056pop_reg r91057pop_reg r101058pop_reg r111059pop_reg r121060pop_reg r131061pop_reg r141062pop_reg r151063pop_reg r161064pop_reg r171065pop_reg r181066pop_reg r191067pop_reg r201068pop_reg r211069pop_reg r221070pop_reg r231071pop_reg r241072pop_reg r251073pop_reg r261074pop_reg r271075pop_reg r28, sp, PTREGS_OFFSET_LR - PTREGS_OFFSET_REG(28)1076/* r29 already restored above */1077bnz lr, .Lkernel_return1078pop_reg lr, sp, PTREGS_OFFSET_TP - PTREGS_OFFSET_LR1079pop_reg tp, sp, PTREGS_OFFSET_SP - PTREGS_OFFSET_TP1080lw sp, sp1081iret10821083/*1084* We can't restore tp when in kernel mode, since a thread might1085* have migrated from another cpu and brought a stale tp value.1086*/1087.Lkernel_return:1088pop_reg lr, sp, PTREGS_OFFSET_SP - PTREGS_OFFSET_LR1089lw sp, sp1090iret10911092/* Restore callee-saved registers from r34 to r51. */1093.Lrestore_callees:1094addli sp, sp, PTREGS_OFFSET_REG(34) - PTREGS_OFFSET_REG(29)1095pop_reg r341096pop_reg r351097pop_reg r361098pop_reg r371099pop_reg r381100pop_reg r391101pop_reg r401102pop_reg r411103pop_reg r421104pop_reg r431105pop_reg r441106pop_reg r451107pop_reg r461108pop_reg r471109pop_reg r481110pop_reg r491111pop_reg r501112pop_reg r51, sp, PTREGS_OFFSET_REG(29) - PTREGS_OFFSET_REG(51)1113j .Lcontinue_restore_regs1114STD_ENDPROC(interrupt_return)11151116/*1117* Some interrupts don't check for single stepping1118*/1119.pushsection .text.handle_interrupt_no_single_step,"ax"1120handle_interrupt_no_single_step:1121finish_interrupt_save handle_interrupt_no_single_step1122{1123jalr r01124PTREGS_PTR(r0, PTREGS_OFFSET_BASE)1125}1126FEEDBACK_REENTER(handle_interrupt_no_single_step)1127{1128movei r30, 0 /* not an NMI */1129j interrupt_return1130}1131STD_ENDPROC(handle_interrupt_no_single_step)11321133/*1134* "NMI" interrupts mask ALL interrupts before calling the1135* handler, and don't check thread flags, etc., on the way1136* back out. In general, the only things we do here for NMIs1137* are the register save/restore, fixing the PC if we were1138* doing single step, and the dataplane kernel-TLB management.1139* We don't (for example) deal with start/stop of the sched tick.1140*/1141.pushsection .text.handle_nmi,"ax"1142handle_nmi:1143finish_interrupt_save handle_nmi1144check_single_stepping normal, .Ldispatch_nmi1145.Ldispatch_nmi:1146{1147jalr r01148PTREGS_PTR(r0, PTREGS_OFFSET_BASE)1149}1150FEEDBACK_REENTER(handle_nmi)1151j interrupt_return1152STD_ENDPROC(handle_nmi)11531154/*1155* Parallel code for syscalls to handle_interrupt.1156*/1157.pushsection .text.handle_syscall,"ax"1158handle_syscall:1159finish_interrupt_save handle_syscall11601161/*1162* Check for if we are single stepping in user level. If so, then1163* we need to restore the PC.1164*/1165check_single_stepping syscall, .Ldispatch_syscall1166.Ldispatch_syscall:11671168/* Enable irqs. */1169TRACE_IRQS_ON1170IRQ_ENABLE(r20, r21)11711172/* Bump the counter for syscalls made on this tile. */1173moveli r20, lo16(irq_stat + IRQ_CPUSTAT_SYSCALL_COUNT_OFFSET)1174auli r20, r20, ha16(irq_stat + IRQ_CPUSTAT_SYSCALL_COUNT_OFFSET)1175add r20, r20, tp1176lw r21, r201177addi r21, r21, 11178sw r20, r2111791180/* Trace syscalls, if requested. */1181GET_THREAD_INFO(r31)1182addi r31, r31, THREAD_INFO_FLAGS_OFFSET1183lw r30, r311184andi r30, r30, _TIF_SYSCALL_TRACE1185bzt r30, .Lrestore_syscall_regs1186jal do_syscall_trace1187FEEDBACK_REENTER(handle_syscall)11881189/*1190* We always reload our registers from the stack at this1191* point. They might be valid, if we didn't build with1192* TRACE_IRQFLAGS, and this isn't a dataplane tile, and we're not1193* doing syscall tracing, but there are enough cases now that it1194* seems simplest just to do the reload unconditionally.1195*/1196.Lrestore_syscall_regs:1197PTREGS_PTR(r11, PTREGS_OFFSET_REG(0))1198pop_reg r0, r111199pop_reg r1, r111200pop_reg r2, r111201pop_reg r3, r111202pop_reg r4, r111203pop_reg r5, r11, PTREGS_OFFSET_SYSCALL - PTREGS_OFFSET_REG(5)1204pop_reg TREG_SYSCALL_NR_NAME, r1112051206/* Ensure that the syscall number is within the legal range. */1207moveli r21, __NR_syscalls1208{1209slt_u r21, TREG_SYSCALL_NR_NAME, r211210moveli r20, lo16(sys_call_table)1211}1212{1213bbns r21, .Linvalid_syscall1214auli r20, r20, ha16(sys_call_table)1215}1216s2a r20, TREG_SYSCALL_NR_NAME, r201217lw r20, r2012181219/* Jump to syscall handler. */1220jalr r201221.Lhandle_syscall_link: /* value of "lr" after "jalr r20" above */12221223/*1224* Write our r0 onto the stack so it gets restored instead1225* of whatever the user had there before.1226*/1227PTREGS_PTR(r29, PTREGS_OFFSET_REG(0))1228sw r29, r012291230.Lsyscall_sigreturn_skip:1231FEEDBACK_REENTER(handle_syscall)12321233/* Do syscall trace again, if requested. */1234lw r30, r311235andi r30, r30, _TIF_SYSCALL_TRACE1236bzt r30, 1f1237jal do_syscall_trace1238FEEDBACK_REENTER(handle_syscall)12391: j .Lresume_userspace /* jump into middle of interrupt_return */12401241.Linvalid_syscall:1242/* Report an invalid syscall back to the user program */1243{1244PTREGS_PTR(r29, PTREGS_OFFSET_REG(0))1245movei r28, -ENOSYS1246}1247sw r29, r281248j .Lresume_userspace /* jump into middle of interrupt_return */1249STD_ENDPROC(handle_syscall)12501251/* Return the address for oprofile to suppress in backtraces. */1252STD_ENTRY_SECTION(handle_syscall_link_address, .text.handle_syscall)1253lnk r01254{1255addli r0, r0, .Lhandle_syscall_link - .1256jrp lr1257}1258STD_ENDPROC(handle_syscall_link_address)12591260STD_ENTRY(ret_from_fork)1261jal sim_notify_fork1262jal schedule_tail1263FEEDBACK_REENTER(ret_from_fork)1264j .Lresume_userspace /* jump into middle of interrupt_return */1265STD_ENDPROC(ret_from_fork)12661267/*1268* Code for ill interrupt.1269*/1270.pushsection .text.handle_ill,"ax"1271handle_ill:1272finish_interrupt_save handle_ill12731274/*1275* Check for if we are single stepping in user level. If so, then1276* we need to restore the PC.1277*/1278check_single_stepping ill, .Ldispatch_normal_ill12791280{1281/* See if the PC is the 1st bundle in the buffer */1282seq r25, r27, r2612831284/* Point to the 2nd bundle in the buffer */1285addi r26, r26, 81286}1287{1288/* Point to the original pc */1289addi r24, r29, SINGLESTEP_STATE_ORIG_PC_OFFSET12901291/* Branch if the PC is the 1st bundle in the buffer */1292bnz r25, 3f1293}1294{1295/* See if the PC is the 2nd bundle of the buffer */1296seq r25, r27, r2612971298/* Set PC to next instruction */1299addi r24, r29, SINGLESTEP_STATE_NEXT_PC_OFFSET1300}1301{1302/* Point to flags */1303addi r25, r29, SINGLESTEP_STATE_FLAGS_OFFSET13041305/* Branch if PC is in the second bundle */1306bz r25, 2f1307}1308/* Load flags */1309lw r25, r251310{1311/*1312* Get the offset for the register to restore1313* Note: the lower bound is 2, so we have implicit scaling by 4.1314* No multiplication of the register number by the size of a register1315* is needed.1316*/1317mm r27, r25, zero, SINGLESTEP_STATE_TARGET_LB, \1318SINGLESTEP_STATE_TARGET_UB13191320/* Mask Rewrite_LR */1321andi r25, r25, SINGLESTEP_STATE_MASK_UPDATE1322}1323{1324addi r29, r29, SINGLESTEP_STATE_UPDATE_VALUE_OFFSET13251326/* Don't rewrite temp register */1327bz r25, 3f1328}1329{1330/* Get the temp value */1331lw r29, r2913321333/* Point to where the register is stored */1334add r27, r27, sp1335}13361337/* Add in the C ABI save area size to the register offset */1338addi r27, r27, C_ABI_SAVE_AREA_SIZE13391340/* Restore the user's register with the temp value */1341sw r27, r291342j 3f134313442:1345/* Must be in the third bundle */1346addi r24, r29, SINGLESTEP_STATE_BRANCH_NEXT_PC_OFFSET134713483:1349/* set PC and continue */1350lw r26, r241351sw r28, r2613521353/*1354* Clear TIF_SINGLESTEP to prevent recursion if we execute an ill.1355* The normal non-arch flow redundantly clears TIF_SINGLESTEP, but we1356* need to clear it here and can't really impose on all other arches.1357* So what's another write between friends?1358*/1359GET_THREAD_INFO(r0)13601361addi r1, r0, THREAD_INFO_FLAGS_OFFSET1362{1363lw r2, r11364addi r0, r0, THREAD_INFO_TASK_OFFSET /* currently a no-op */1365}1366andi r2, r2, ~_TIF_SINGLESTEP1367sw r1, r213681369/* Issue a sigtrap */1370{1371lw r0, r0 /* indirect thru thread_info to get task_info*/1372addi r1, sp, C_ABI_SAVE_AREA_SIZE /* put ptregs pointer into r1 */1373move r2, zero /* load error code into r2 */1374}13751376jal send_sigtrap /* issue a SIGTRAP */1377FEEDBACK_REENTER(handle_ill)1378j .Lresume_userspace /* jump into middle of interrupt_return */13791380.Ldispatch_normal_ill:1381{1382jalr r01383PTREGS_PTR(r0, PTREGS_OFFSET_BASE)1384}1385FEEDBACK_REENTER(handle_ill)1386{1387movei r30, 0 /* not an NMI */1388j interrupt_return1389}1390STD_ENDPROC(handle_ill)13911392/* Various stub interrupt handlers and syscall handlers */13931394STD_ENTRY_LOCAL(_kernel_double_fault)1395mfspr r1, SPR_EX_CONTEXT_K_01396move r2, lr1397move r3, sp1398move r4, r521399addi sp, sp, -C_ABI_SAVE_AREA_SIZE1400j kernel_double_fault1401STD_ENDPROC(_kernel_double_fault)14021403STD_ENTRY_LOCAL(bad_intr)1404mfspr r2, SPR_EX_CONTEXT_K_01405panic "Unhandled interrupt %#x: PC %#lx"1406STD_ENDPROC(bad_intr)14071408/* Put address of pt_regs in reg and jump. */1409#define PTREGS_SYSCALL(x, reg) \1410STD_ENTRY(_##x); \1411{ \1412PTREGS_PTR(reg, PTREGS_OFFSET_BASE); \1413j x \1414}; \1415STD_ENDPROC(_##x)14161417/*1418* Special-case sigreturn to not write r0 to the stack on return.1419* This is technically more efficient, but it also avoids difficulties1420* in the 64-bit OS when handling 32-bit compat code, since we must not1421* sign-extend r0 for the sigreturn return-value case.1422*/1423#define PTREGS_SYSCALL_SIGRETURN(x, reg) \1424STD_ENTRY(_##x); \1425addli lr, lr, .Lsyscall_sigreturn_skip - .Lhandle_syscall_link; \1426{ \1427PTREGS_PTR(reg, PTREGS_OFFSET_BASE); \1428j x \1429}; \1430STD_ENDPROC(_##x)14311432PTREGS_SYSCALL(sys_execve, r3)1433PTREGS_SYSCALL(sys_sigaltstack, r2)1434PTREGS_SYSCALL_SIGRETURN(sys_rt_sigreturn, r0)1435PTREGS_SYSCALL(sys_cmpxchg_badaddr, r1)14361437/* Save additional callee-saves to pt_regs, put address in r4 and jump. */1438STD_ENTRY(_sys_clone)1439push_extra_callee_saves r41440j sys_clone1441STD_ENDPROC(_sys_clone)14421443/*1444* This entrypoint is taken for the cmpxchg and atomic_update fast1445* swints. We may wish to generalize it to other fast swints at some1446* point, but for now there are just two very similar ones, which1447* makes it faster.1448*1449* The fast swint code is designed to have a small footprint. It does1450* not save or restore any GPRs, counting on the caller-save registers1451* to be available to it on entry. It does not modify any callee-save1452* registers (including "lr"). It does not check what PL it is being1453* called at, so you'd better not call it other than at PL0.1454* The <atomic.h> wrapper assumes it only clobbers r20-r29, so if1455* it ever is necessary to use more registers, be aware.1456*1457* It does not use the stack, but since it might be re-interrupted by1458* a page fault which would assume the stack was valid, it does1459* save/restore the stack pointer and zero it out to make sure it gets reset.1460* Since we always keep interrupts disabled, the hypervisor won't1461* clobber our EX_CONTEXT_K_x registers, so we don't save/restore them1462* (other than to advance the PC on return).1463*1464* We have to manually validate the user vs kernel address range1465* (since at PL1 we can read/write both), and for performance reasons1466* we don't allow cmpxchg on the fc000000 memory region, since we only1467* validate that the user address is below PAGE_OFFSET.1468*1469* We place it in the __HEAD section to ensure it is relatively1470* near to the intvec_SWINT_1 code (reachable by a conditional branch).1471*1472* Our use of ATOMIC_LOCK_REG here must match do_page_fault_ics().1473*1474* As we do in lib/atomic_asm_32.S, we bypass a store if the value we1475* would store is the same as the value we just loaded.1476*/1477__HEAD1478.align 641479/* Align much later jump on the start of a cache line. */1480#if !ATOMIC_LOCKS_FOUND_VIA_TABLE()1481nop1482#if PAGE_SIZE >= 0x100001483nop1484#endif1485#endif1486ENTRY(sys_cmpxchg)14871488/*1489* Save "sp" and set it zero for any possible page fault.1490*1491* HACK: We want to both zero sp and check r0's alignment,1492* so we do both at once. If "sp" becomes nonzero we1493* know r0 is unaligned and branch to the error handler that1494* restores sp, so this is OK.1495*1496* ICS is disabled right now so having a garbage but nonzero1497* sp is OK, since we won't execute any faulting instructions1498* when it is nonzero.1499*/1500{1501move r27, sp1502andi sp, r0, 31503}15041505/*1506* Get the lock address in ATOMIC_LOCK_REG, and also validate that the1507* address is less than PAGE_OFFSET, since that won't trap at PL1.1508* We only use bits less than PAGE_SHIFT to avoid having to worry1509* about aliasing among multiple mappings of the same physical page,1510* and we ignore the low 3 bits so we have one lock that covers1511* both a cmpxchg64() and a cmpxchg() on either its low or high word.1512* NOTE: this must match __atomic_hashed_lock() in lib/atomic_32.c.1513*/15141515#if (PAGE_OFFSET & 0xffff) != 01516# error Code here assumes PAGE_OFFSET can be loaded with just hi16()1517#endif15181519#if ATOMIC_LOCKS_FOUND_VIA_TABLE()1520{1521/* Check for unaligned input. */1522bnz sp, .Lcmpxchg_badaddr1523mm r25, r0, zero, 3, PAGE_SHIFT-11524}1525{1526crc32_32 r25, zero, r251527moveli r21, lo16(atomic_lock_ptr)1528}1529{1530auli r21, r21, ha16(atomic_lock_ptr)1531auli r23, zero, hi16(PAGE_OFFSET) /* hugepage-aligned */1532}1533{1534shri r20, r25, 32 - ATOMIC_HASH_L1_SHIFT1535slt_u r23, r0, r231536lw r26, r0 /* see comment in the "#else" for the "lw r26". */1537}1538{1539s2a r21, r20, r211540bbns r23, .Lcmpxchg_badaddr1541}1542{1543lw r21, r211544seqi r23, TREG_SYSCALL_NR_NAME, __NR_FAST_cmpxchg641545andi r25, r25, ATOMIC_HASH_L2_SIZE - 11546}1547{1548/* Branch away at this point if we're doing a 64-bit cmpxchg. */1549bbs r23, .Lcmpxchg641550andi r23, r0, 7 /* Precompute alignment for cmpxchg64. */1551}1552{1553s2a ATOMIC_LOCK_REG_NAME, r25, r211554j .Lcmpxchg32_tns /* see comment in the #else for the jump. */1555}15561557#else /* ATOMIC_LOCKS_FOUND_VIA_TABLE() */1558{1559/* Check for unaligned input. */1560bnz sp, .Lcmpxchg_badaddr1561auli r23, zero, hi16(PAGE_OFFSET) /* hugepage-aligned */1562}1563{1564/*1565* Slide bits into position for 'mm'. We want to ignore1566* the low 3 bits of r0, and consider only the next1567* ATOMIC_HASH_SHIFT bits.1568* Because of C pointer arithmetic, we want to compute this:1569*1570* ((char*)atomic_locks +1571* (((r0 >> 3) & (1 << (ATOMIC_HASH_SIZE - 1))) << 2))1572*1573* Instead of two shifts we just ">> 1", and use 'mm'1574* to ignore the low and high bits we don't want.1575*/1576shri r25, r0, 115771578slt_u r23, r0, r2315791580/*1581* Ensure that the TLB is loaded before we take out the lock.1582* On tilepro, this will start fetching the value all the way1583* into our L1 as well (and if it gets modified before we1584* grab the lock, it will be invalidated from our cache1585* before we reload it). On tile64, we'll start fetching it1586* into our L1 if we're the home, and if we're not, we'll1587* still at least start fetching it into the home's L2.1588*/1589lw r26, r01590}1591{1592auli r21, zero, ha16(atomic_locks)15931594bbns r23, .Lcmpxchg_badaddr1595}1596#if PAGE_SIZE < 0x100001597/* atomic_locks is page-aligned so for big pages we don't need this. */1598addli r21, r21, lo16(atomic_locks)1599#endif1600{1601/*1602* Insert the hash bits into the page-aligned pointer.1603* ATOMIC_HASH_SHIFT is so big that we don't actually hash1604* the unmasked address bits, as that may cause unnecessary1605* collisions.1606*/1607mm ATOMIC_LOCK_REG_NAME, r25, r21, 2, (ATOMIC_HASH_SHIFT + 2) - 116081609seqi r23, TREG_SYSCALL_NR_NAME, __NR_FAST_cmpxchg641610}1611{1612/* Branch away at this point if we're doing a 64-bit cmpxchg. */1613bbs r23, .Lcmpxchg641614andi r23, r0, 7 /* Precompute alignment for cmpxchg64. */1615}1616{1617/*1618* We very carefully align the code that actually runs with1619* the lock held (twelve bundles) so that we know it is all in1620* the icache when we start. This instruction (the jump) is1621* at the start of the first cache line, address zero mod 64;1622* we jump to the very end of the second cache line to get that1623* line loaded in the icache, then fall through to issue the tns1624* in the third cache line, at which point it's all cached.1625* Note that is for performance, not correctness.1626*/1627j .Lcmpxchg32_tns1628}16291630#endif /* ATOMIC_LOCKS_FOUND_VIA_TABLE() */16311632/* Symbol for do_page_fault_ics() to use to compare against the PC. */1633.global __sys_cmpxchg_grab_lock1634__sys_cmpxchg_grab_lock:16351636/*1637* Perform the actual cmpxchg or atomic_update.1638*/1639.Ldo_cmpxchg32:1640{1641lw r21, r01642seqi r23, TREG_SYSCALL_NR_NAME, __NR_FAST_atomic_update1643move r24, r21644}1645{1646seq r22, r21, r1 /* See if cmpxchg matches. */1647and r25, r21, r1 /* If atomic_update, compute (*mem & mask) */1648}1649{1650or r22, r22, r23 /* Skip compare branch for atomic_update. */1651add r25, r25, r2 /* Compute (*mem & mask) + addend. */1652}1653{1654mvnz r24, r23, r25 /* Use atomic_update value if appropriate. */1655bbns r22, .Lcmpxchg32_nostore1656}1657seq r22, r24, r21 /* Are we storing the value we loaded? */1658bbs r22, .Lcmpxchg32_nostore1659sw r0, r2416601661/* The following instruction is the start of the second cache line. */1662/* Do slow mtspr here so the following "mf" waits less. */1663{1664move sp, r271665mtspr SPR_EX_CONTEXT_K_0, r281666}1667mf16681669{1670move r0, r211671sw ATOMIC_LOCK_REG_NAME, zero1672}1673iret16741675/* Duplicated code here in the case where we don't overlap "mf" */1676.Lcmpxchg32_nostore:1677{1678move r0, r211679sw ATOMIC_LOCK_REG_NAME, zero1680}1681{1682move sp, r271683mtspr SPR_EX_CONTEXT_K_0, r281684}1685iret16861687/*1688* The locking code is the same for 32-bit cmpxchg/atomic_update,1689* and for 64-bit cmpxchg. We provide it as a macro and put1690* it into both versions. We can't share the code literally1691* since it depends on having the right branch-back address.1692*/1693.macro cmpxchg_lock, bitwidth16941695/* Lock; if we succeed, jump back up to the read-modify-write. */1696#ifdef CONFIG_SMP1697tns r21, ATOMIC_LOCK_REG_NAME1698#else1699/*1700* Non-SMP preserves all the lock infrastructure, to keep the1701* code simpler for the interesting (SMP) case. However, we do1702* one small optimization here and in atomic_asm.S, which is1703* to fake out acquiring the actual lock in the atomic_lock table.1704*/1705movei r21, 01706#endif17071708/* Issue the slow SPR here while the tns result is in flight. */1709mfspr r28, SPR_EX_CONTEXT_K_017101711{1712addi r28, r28, 8 /* return to the instruction after the swint1 */1713bzt r21, .Ldo_cmpxchg\bitwidth1714}1715/*1716* The preceding instruction is the last thing that must be1717* hot in the icache before we do the "tns" above.1718*/17191720#ifdef CONFIG_SMP1721/*1722* We failed to acquire the tns lock on our first try. Now use1723* bounded exponential backoff to retry, like __atomic_spinlock().1724*/1725{1726moveli r23, 2048 /* maximum backoff time in cycles */1727moveli r25, 32 /* starting backoff time in cycles */1728}17291: mfspr r26, CYCLE_LOW /* get start point for this backoff */17302: mfspr r22, CYCLE_LOW /* test to see if we've backed off enough */1731sub r22, r22, r261732slt r22, r22, r251733bbst r22, 2b1734{1735shli r25, r25, 1 /* double the backoff; retry the tns */1736tns r21, ATOMIC_LOCK_REG_NAME1737}1738slt r26, r23, r25 /* is the proposed backoff too big? */1739{1740mvnz r25, r26, r231741bzt r21, .Ldo_cmpxchg\bitwidth1742}1743j 1b1744#endif /* CONFIG_SMP */1745.endm17461747.Lcmpxchg32_tns:1748/*1749* This is the last instruction on the second cache line.1750* The nop here loads the second line, then we fall through1751* to the tns to load the third line before we take the lock.1752*/1753nop1754cmpxchg_lock 3217551756/*1757* This code is invoked from sys_cmpxchg after most of the1758* preconditions have been checked. We still need to check1759* that r0 is 8-byte aligned, since if it's not we won't1760* actually be atomic. However, ATOMIC_LOCK_REG has the atomic1761* lock pointer and r27/r28 have the saved SP/PC.1762* r23 is holding "r0 & 7" so we can test for alignment.1763* The compare value is in r2/r3; the new value is in r4/r5.1764* On return, we must put the old value in r0/r1.1765*/1766.align 641767.Lcmpxchg64:1768{1769#if ATOMIC_LOCKS_FOUND_VIA_TABLE()1770s2a ATOMIC_LOCK_REG_NAME, r25, r211771#endif1772bzt r23, .Lcmpxchg64_tns1773}1774j .Lcmpxchg_badaddr17751776.Ldo_cmpxchg64:1777{1778lw r21, r01779addi r25, r0, 41780}1781{1782lw r1, r251783}1784seq r26, r21, r21785{1786bz r26, .Lcmpxchg64_mismatch1787seq r26, r1, r31788}1789{1790bz r26, .Lcmpxchg64_mismatch1791}1792sw r0, r41793sw r25, r517941795/*1796* The 32-bit path provides optimized "match" and "mismatch"1797* iret paths, but we don't have enough bundles in this cache line1798* to do that, so we just make even the "mismatch" path do an "mf".1799*/1800.Lcmpxchg64_mismatch:1801{1802move sp, r271803mtspr SPR_EX_CONTEXT_K_0, r281804}1805mf1806{1807move r0, r211808sw ATOMIC_LOCK_REG_NAME, zero1809}1810iret18111812.Lcmpxchg64_tns:1813cmpxchg_lock 64181418151816/*1817* Reset sp and revector to sys_cmpxchg_badaddr(), which will1818* just raise the appropriate signal and exit. Doing it this1819* way means we don't have to duplicate the code in intvec.S's1820* int_hand macro that locates the top of the stack.1821*/1822.Lcmpxchg_badaddr:1823{1824moveli TREG_SYSCALL_NR_NAME, __NR_cmpxchg_badaddr1825move sp, r271826}1827j intvec_SWINT_11828ENDPROC(sys_cmpxchg)1829ENTRY(__sys_cmpxchg_end)183018311832/* The single-step support may need to read all the registers. */1833int_unalign:1834push_extra_callee_saves r01835j do_trap18361837/* Include .intrpt1 array of interrupt vectors */1838.section ".intrpt1", "ax"18391840#define op_handle_perf_interrupt bad_intr1841#define op_handle_aux_perf_interrupt bad_intr18421843#ifndef CONFIG_HARDWALL1844#define do_hardwall_trap bad_intr1845#endif18461847int_hand INT_ITLB_MISS, ITLB_MISS, \1848do_page_fault, handle_interrupt_no_single_step1849int_hand INT_MEM_ERROR, MEM_ERROR, bad_intr1850int_hand INT_ILL, ILL, do_trap, handle_ill1851int_hand INT_GPV, GPV, do_trap1852int_hand INT_SN_ACCESS, SN_ACCESS, do_trap1853int_hand INT_IDN_ACCESS, IDN_ACCESS, do_trap1854int_hand INT_UDN_ACCESS, UDN_ACCESS, do_trap1855int_hand INT_IDN_REFILL, IDN_REFILL, bad_intr1856int_hand INT_UDN_REFILL, UDN_REFILL, bad_intr1857int_hand INT_IDN_COMPLETE, IDN_COMPLETE, bad_intr1858int_hand INT_UDN_COMPLETE, UDN_COMPLETE, bad_intr1859int_hand INT_SWINT_3, SWINT_3, do_trap1860int_hand INT_SWINT_2, SWINT_2, do_trap1861int_hand INT_SWINT_1, SWINT_1, SYSCALL, handle_syscall1862int_hand INT_SWINT_0, SWINT_0, do_trap1863int_hand INT_UNALIGN_DATA, UNALIGN_DATA, int_unalign1864int_hand INT_DTLB_MISS, DTLB_MISS, do_page_fault1865int_hand INT_DTLB_ACCESS, DTLB_ACCESS, do_page_fault1866int_hand INT_DMATLB_MISS, DMATLB_MISS, do_page_fault1867int_hand INT_DMATLB_ACCESS, DMATLB_ACCESS, do_page_fault1868int_hand INT_SNITLB_MISS, SNITLB_MISS, do_page_fault1869int_hand INT_SN_NOTIFY, SN_NOTIFY, bad_intr1870int_hand INT_SN_FIREWALL, SN_FIREWALL, do_hardwall_trap1871int_hand INT_IDN_FIREWALL, IDN_FIREWALL, bad_intr1872int_hand INT_UDN_FIREWALL, UDN_FIREWALL, do_hardwall_trap1873int_hand INT_TILE_TIMER, TILE_TIMER, do_timer_interrupt1874int_hand INT_IDN_TIMER, IDN_TIMER, bad_intr1875int_hand INT_UDN_TIMER, UDN_TIMER, bad_intr1876int_hand INT_DMA_NOTIFY, DMA_NOTIFY, bad_intr1877int_hand INT_IDN_CA, IDN_CA, bad_intr1878int_hand INT_UDN_CA, UDN_CA, bad_intr1879int_hand INT_IDN_AVAIL, IDN_AVAIL, bad_intr1880int_hand INT_UDN_AVAIL, UDN_AVAIL, bad_intr1881int_hand INT_PERF_COUNT, PERF_COUNT, \1882op_handle_perf_interrupt, handle_nmi1883int_hand INT_INTCTRL_3, INTCTRL_3, bad_intr1884#if CONFIG_KERNEL_PL == 21885dc_dispatch INT_INTCTRL_2, INTCTRL_21886int_hand INT_INTCTRL_1, INTCTRL_1, bad_intr1887#else1888int_hand INT_INTCTRL_2, INTCTRL_2, bad_intr1889dc_dispatch INT_INTCTRL_1, INTCTRL_11890#endif1891int_hand INT_INTCTRL_0, INTCTRL_0, bad_intr1892int_hand INT_MESSAGE_RCV_DWNCL, MESSAGE_RCV_DWNCL, \1893hv_message_intr1894int_hand INT_DEV_INTR_DWNCL, DEV_INTR_DWNCL, \1895tile_dev_intr1896int_hand INT_I_ASID, I_ASID, bad_intr1897int_hand INT_D_ASID, D_ASID, bad_intr1898int_hand INT_DMATLB_MISS_DWNCL, DMATLB_MISS_DWNCL, \1899do_page_fault1900int_hand INT_SNITLB_MISS_DWNCL, SNITLB_MISS_DWNCL, \1901do_page_fault1902int_hand INT_DMATLB_ACCESS_DWNCL, DMATLB_ACCESS_DWNCL, \1903do_page_fault1904int_hand INT_SN_CPL, SN_CPL, bad_intr1905int_hand INT_DOUBLE_FAULT, DOUBLE_FAULT, do_trap1906#if CHIP_HAS_AUX_PERF_COUNTERS()1907int_hand INT_AUX_PERF_COUNT, AUX_PERF_COUNT, \1908op_handle_aux_perf_interrupt, handle_nmi1909#endif19101911/* Synthetic interrupt delivered only by the simulator */1912int_hand INT_BREAKPOINT, BREAKPOINT, do_breakpoint191319141915