/* SPDX-License-Identifier: GPL-2.0-or-later */1/*2* OpenRISC entry.S3*4* Linux architectural port borrowing liberally from similar works of5* others. All original copyrights apply as per the original source6* declaration.7*8* Modifications for the OpenRISC architecture:9* Copyright (C) 2003 Matjaz Breskvar <[email protected]>10* Copyright (C) 2005 Gyorgy Jeney <[email protected]>11* Copyright (C) 2010-2011 Jonas Bonn <[email protected]>12*/1314#include <linux/linkage.h>15#include <linux/pgtable.h>1617#include <asm/processor.h>18#include <asm/unistd.h>19#include <asm/thread_info.h>20#include <asm/errno.h>21#include <asm/spr_defs.h>22#include <asm/page.h>23#include <asm/mmu.h>24#include <asm/asm-offsets.h>2526#define DISABLE_INTERRUPTS(t1,t2) \27l.mfspr t2,r0,SPR_SR ;\28l.movhi t1,hi(~(SPR_SR_IEE|SPR_SR_TEE)) ;\29l.ori t1,t1,lo(~(SPR_SR_IEE|SPR_SR_TEE)) ;\30l.and t2,t2,t1 ;\31l.mtspr r0,t2,SPR_SR3233#define ENABLE_INTERRUPTS(t1) \34l.mfspr t1,r0,SPR_SR ;\35l.ori t1,t1,lo(SPR_SR_IEE|SPR_SR_TEE) ;\36l.mtspr r0,t1,SPR_SR3738/* =========================================================[ macros ]=== */3940#ifdef CONFIG_TRACE_IRQFLAGS41/*42* Trace irq on/off creating a stack frame.43*/44#define TRACE_IRQS_OP(trace_op) \45l.sw -8(r1),r2 /* store frame pointer */ ;\46l.sw -4(r1),r9 /* store return address */ ;\47l.addi r2,r1,0 /* move sp to fp */ ;\48l.jal trace_op ;\49l.addi r1,r1,-8 ;\50l.ori r1,r2,0 /* restore sp */ ;\51l.lwz r9,-4(r1) /* restore return address */ ;\52l.lwz r2,-8(r1) /* restore fp */ ;\53/*54* Trace irq on/off and save registers we need that would otherwise be55* clobbered.56*/57#define TRACE_IRQS_SAVE(t1,trace_op) \58l.sw -12(r1),t1 /* save extra reg */ ;\59l.sw -8(r1),r2 /* store frame pointer */ ;\60l.sw -4(r1),r9 /* store return address */ ;\61l.addi r2,r1,0 /* move sp to fp */ ;\62l.jal trace_op ;\63l.addi r1,r1,-12 ;\64l.ori r1,r2,0 /* restore sp */ ;\65l.lwz r9,-4(r1) /* restore return address */ ;\66l.lwz r2,-8(r1) /* restore fp */ ;\67l.lwz t1,-12(r1) /* restore extra reg */6869#define TRACE_IRQS_OFF TRACE_IRQS_OP(trace_hardirqs_off)70#define TRACE_IRQS_ON TRACE_IRQS_OP(trace_hardirqs_on)71#define TRACE_IRQS_ON_SYSCALL \72TRACE_IRQS_SAVE(r10,trace_hardirqs_on) ;\73l.lwz r3,PT_GPR3(r1) ;\74l.lwz r4,PT_GPR4(r1) ;\75l.lwz r5,PT_GPR5(r1) ;\76l.lwz r6,PT_GPR6(r1) ;\77l.lwz r7,PT_GPR7(r1) ;\78l.lwz r8,PT_GPR8(r1) ;\79l.lwz r11,PT_GPR11(r1)80#define TRACE_IRQS_OFF_ENTRY \81l.lwz r5,PT_SR(r1) ;\82l.andi r3,r5,(SPR_SR_IEE|SPR_SR_TEE) ;\83l.sfeq r5,r0 /* skip trace if irqs were already off */;\84l.bf 1f ;\85l.nop ;\86TRACE_IRQS_SAVE(r4,trace_hardirqs_off) ;\871:88#else89#define TRACE_IRQS_OFF90#define TRACE_IRQS_ON91#define TRACE_IRQS_OFF_ENTRY92#define TRACE_IRQS_ON_SYSCALL93#endif9495/*96* We need to disable interrupts at beginning of RESTORE_ALL97* since interrupt might come in after we've loaded EPC return address98* and overwrite EPC with address somewhere in RESTORE_ALL99* which is of course wrong!100*/101102#define RESTORE_ALL \103DISABLE_INTERRUPTS(r3,r4) ;\104l.lwz r3,PT_PC(r1) ;\105l.mtspr r0,r3,SPR_EPCR_BASE ;\106l.lwz r3,PT_SR(r1) ;\107l.mtspr r0,r3,SPR_ESR_BASE ;\108l.lwz r2,PT_GPR2(r1) ;\109l.lwz r3,PT_GPR3(r1) ;\110l.lwz r4,PT_GPR4(r1) ;\111l.lwz r5,PT_GPR5(r1) ;\112l.lwz r6,PT_GPR6(r1) ;\113l.lwz r7,PT_GPR7(r1) ;\114l.lwz r8,PT_GPR8(r1) ;\115l.lwz r9,PT_GPR9(r1) ;\116l.lwz r10,PT_GPR10(r1) ;\117l.lwz r11,PT_GPR11(r1) ;\118l.lwz r12,PT_GPR12(r1) ;\119l.lwz r13,PT_GPR13(r1) ;\120l.lwz r14,PT_GPR14(r1) ;\121l.lwz r15,PT_GPR15(r1) ;\122l.lwz r16,PT_GPR16(r1) ;\123l.lwz r17,PT_GPR17(r1) ;\124l.lwz r18,PT_GPR18(r1) ;\125l.lwz r19,PT_GPR19(r1) ;\126l.lwz r20,PT_GPR20(r1) ;\127l.lwz r21,PT_GPR21(r1) ;\128l.lwz r22,PT_GPR22(r1) ;\129l.lwz r23,PT_GPR23(r1) ;\130l.lwz r24,PT_GPR24(r1) ;\131l.lwz r25,PT_GPR25(r1) ;\132l.lwz r26,PT_GPR26(r1) ;\133l.lwz r27,PT_GPR27(r1) ;\134l.lwz r28,PT_GPR28(r1) ;\135l.lwz r29,PT_GPR29(r1) ;\136l.lwz r30,PT_GPR30(r1) ;\137l.lwz r31,PT_GPR31(r1) ;\138l.lwz r1,PT_SP(r1) ;\139l.rfe140141142#define EXCEPTION_ENTRY(handler) \143.global handler ;\144handler: ;\145/* r1, EPCR, ESR a already saved */ ;\146l.sw PT_GPR2(r1),r2 ;\147l.sw PT_GPR3(r1),r3 ;\148/* r4 already save */ ;\149l.sw PT_GPR5(r1),r5 ;\150l.sw PT_GPR6(r1),r6 ;\151l.sw PT_GPR7(r1),r7 ;\152l.sw PT_GPR8(r1),r8 ;\153l.sw PT_GPR9(r1),r9 ;\154/* r10 already saved */ ;\155l.sw PT_GPR11(r1),r11 ;\156/* r12 already saved */ ;\157l.sw PT_GPR13(r1),r13 ;\158l.sw PT_GPR14(r1),r14 ;\159l.sw PT_GPR15(r1),r15 ;\160l.sw PT_GPR16(r1),r16 ;\161l.sw PT_GPR17(r1),r17 ;\162l.sw PT_GPR18(r1),r18 ;\163l.sw PT_GPR19(r1),r19 ;\164l.sw PT_GPR20(r1),r20 ;\165l.sw PT_GPR21(r1),r21 ;\166l.sw PT_GPR22(r1),r22 ;\167l.sw PT_GPR23(r1),r23 ;\168l.sw PT_GPR24(r1),r24 ;\169l.sw PT_GPR25(r1),r25 ;\170l.sw PT_GPR26(r1),r26 ;\171l.sw PT_GPR27(r1),r27 ;\172l.sw PT_GPR28(r1),r28 ;\173l.sw PT_GPR29(r1),r29 ;\174/* r30 already save */ ;\175l.sw PT_GPR31(r1),r31 ;\176TRACE_IRQS_OFF_ENTRY ;\177/* Store -1 in orig_gpr11 for non-syscall exceptions */ ;\178l.addi r30,r0,-1 ;\179l.sw PT_ORIG_GPR11(r1),r30180181#define UNHANDLED_EXCEPTION(handler,vector) \182.global handler ;\183handler: ;\184/* r1, EPCR, ESR already saved */ ;\185l.sw PT_GPR2(r1),r2 ;\186l.sw PT_GPR3(r1),r3 ;\187l.sw PT_GPR5(r1),r5 ;\188l.sw PT_GPR6(r1),r6 ;\189l.sw PT_GPR7(r1),r7 ;\190l.sw PT_GPR8(r1),r8 ;\191l.sw PT_GPR9(r1),r9 ;\192/* r10 already saved */ ;\193l.sw PT_GPR11(r1),r11 ;\194/* r12 already saved */ ;\195l.sw PT_GPR13(r1),r13 ;\196l.sw PT_GPR14(r1),r14 ;\197l.sw PT_GPR15(r1),r15 ;\198l.sw PT_GPR16(r1),r16 ;\199l.sw PT_GPR17(r1),r17 ;\200l.sw PT_GPR18(r1),r18 ;\201l.sw PT_GPR19(r1),r19 ;\202l.sw PT_GPR20(r1),r20 ;\203l.sw PT_GPR21(r1),r21 ;\204l.sw PT_GPR22(r1),r22 ;\205l.sw PT_GPR23(r1),r23 ;\206l.sw PT_GPR24(r1),r24 ;\207l.sw PT_GPR25(r1),r25 ;\208l.sw PT_GPR26(r1),r26 ;\209l.sw PT_GPR27(r1),r27 ;\210l.sw PT_GPR28(r1),r28 ;\211l.sw PT_GPR29(r1),r29 ;\212/* r30 already saved */ ;\213l.sw PT_GPR31(r1),r31 ;\214/* Store -1 in orig_gpr11 for non-syscall exceptions */ ;\215l.addi r30,r0,-1 ;\216l.sw PT_ORIG_GPR11(r1),r30 ;\217l.addi r3,r1,0 ;\218/* r4 is exception EA */ ;\219l.addi r5,r0,vector ;\220l.jal unhandled_exception ;\221l.nop ;\222l.j _ret_from_exception ;\223l.nop224225/* clobbers 'reg' */226#define CLEAR_LWA_FLAG(reg) \227l.movhi reg,hi(lwa_flag) ;\228l.ori reg,reg,lo(lwa_flag) ;\229l.sw 0(reg),r0230/*231* NOTE: one should never assume that SPR_EPC, SPR_ESR, SPR_EEAR232* contain the same values as when exception we're handling233* occured. in fact they never do. if you need them use234* values saved on stack (for SPR_EPC, SPR_ESR) or content235* of r4 (for SPR_EEAR). for details look at EXCEPTION_HANDLE()236* in 'arch/openrisc/kernel/head.S'237*/238239/* =====================================================[ exceptions] === */240241__REF242243/* ---[ 0x100: RESET exception ]----------------------------------------- */244245EXCEPTION_ENTRY(_tng_kernel_start)246l.jal _start247l.andi r0,r0,0248249/* ---[ 0x200: BUS exception ]------------------------------------------- */250251EXCEPTION_ENTRY(_bus_fault_handler)252CLEAR_LWA_FLAG(r3)253/* r4: EA of fault (set by EXCEPTION_HANDLE) */254l.jal do_bus_fault255l.addi r3,r1,0 /* pt_regs */256257l.j _ret_from_exception258l.nop259260/* ---[ 0x300: Data Page Fault exception ]------------------------------- */261EXCEPTION_ENTRY(_dtlb_miss_page_fault_handler)262CLEAR_LWA_FLAG(r3)263l.and r5,r5,r0264l.j 1f265l.nop266267EXCEPTION_ENTRY(_data_page_fault_handler)268CLEAR_LWA_FLAG(r3)269/* set up parameters for do_page_fault */270l.ori r5,r0,0x300 // exception vector2711:272l.addi r3,r1,0 // pt_regs273/* r4 set be EXCEPTION_HANDLE */ // effective address of fault274275#ifdef CONFIG_OPENRISC_NO_SPR_SR_DSX276l.lwz r6,PT_PC(r3) // address of an offending insn277l.lwz r6,0(r6) // instruction that caused pf278279l.srli r6,r6,26 // check opcode for jump insn280l.sfeqi r6,0 // l.j281l.bf 8f282l.sfeqi r6,1 // l.jal283l.bf 8f284l.sfeqi r6,3 // l.bnf285l.bf 8f286l.sfeqi r6,4 // l.bf287l.bf 8f288l.sfeqi r6,0x11 // l.jr289l.bf 8f290l.sfeqi r6,0x12 // l.jalr291l.bf 8f292l.nop293294l.j 9f295l.nop2962978: // offending insn is in delay slot298l.lwz r6,PT_PC(r3) // address of an offending insn299l.addi r6,r6,4300l.lwz r6,0(r6) // instruction that caused pf301l.srli r6,r6,26 // get opcode3029: // offending instruction opcode loaded in r6303304#else305306l.mfspr r6,r0,SPR_SR // SR307l.andi r6,r6,SPR_SR_DSX // check for delay slot exception308l.sfne r6,r0 // exception happened in delay slot309l.bnf 7f310l.lwz r6,PT_PC(r3) // address of an offending insn311312l.addi r6,r6,4 // offending insn is in delay slot3137:314l.lwz r6,0(r6) // instruction that caused pf315l.srli r6,r6,26 // check opcode for write access316#endif317318l.sfgeui r6,0x33 // check opcode for write access319l.bnf 1f320l.sfleui r6,0x37321l.bnf 1f322l.ori r6,r0,0x1 // write access323l.j 2f324l.nop3251: l.ori r6,r0,0x0 // !write access3262:327328/* call fault.c handler in openrisc/mm/fault.c */329l.jal do_page_fault330l.nop331l.j _ret_from_exception332l.nop333334/* ---[ 0x400: Insn Page Fault exception ]------------------------------- */335EXCEPTION_ENTRY(_itlb_miss_page_fault_handler)336CLEAR_LWA_FLAG(r3)337l.and r5,r5,r0338l.j 1f339l.nop340341EXCEPTION_ENTRY(_insn_page_fault_handler)342CLEAR_LWA_FLAG(r3)343/* set up parameters for do_page_fault */344l.ori r5,r0,0x400 // exception vector3451:346l.addi r3,r1,0 // pt_regs347/* r4 set be EXCEPTION_HANDLE */ // effective address of fault348l.ori r6,r0,0x0 // !write access349350/* call fault.c handler in openrisc/mm/fault.c */351l.jal do_page_fault352l.nop353l.j _ret_from_exception354l.nop355356357/* ---[ 0x500: Timer exception ]----------------------------------------- */358359EXCEPTION_ENTRY(_timer_handler)360CLEAR_LWA_FLAG(r3)361l.jal timer_interrupt362l.addi r3,r1,0 /* pt_regs */363364l.j _ret_from_intr365l.nop366367/* ---[ 0x600: Alignment exception ]-------------------------------------- */368369EXCEPTION_ENTRY(_alignment_handler)370CLEAR_LWA_FLAG(r3)371/* r4: EA of fault (set by EXCEPTION_HANDLE) */372l.jal do_unaligned_access373l.addi r3,r1,0 /* pt_regs */374375l.j _ret_from_exception376l.nop377378#if 0379EXCEPTION_ENTRY(_alignment_handler)380// l.mfspr r2,r0,SPR_EEAR_BASE /* Load the effective address */381l.addi r2,r4,0382// l.mfspr r5,r0,SPR_EPCR_BASE /* Load the insn address */383l.lwz r5,PT_PC(r1)384385l.lwz r3,0(r5) /* Load insn */386l.srli r4,r3,26 /* Shift left to get the insn opcode */387388l.sfeqi r4,0x00 /* Check if the load/store insn is in delay slot */389l.bf jmp390l.sfeqi r4,0x01391l.bf jmp392l.sfeqi r4,0x03393l.bf jmp394l.sfeqi r4,0x04395l.bf jmp396l.sfeqi r4,0x11397l.bf jr398l.sfeqi r4,0x12399l.bf jr400l.nop401l.j 1f402l.addi r5,r5,4 /* Increment PC to get return insn address */403404jmp:405l.slli r4,r3,6 /* Get the signed extended jump length */406l.srai r4,r4,4407408l.lwz r3,4(r5) /* Load the real load/store insn */409410l.add r5,r5,r4 /* Calculate jump target address */411412l.j 1f413l.srli r4,r3,26 /* Shift left to get the insn opcode */414415jr:416l.slli r4,r3,9 /* Shift to get the reg nb */417l.andi r4,r4,0x7c418419l.lwz r3,4(r5) /* Load the real load/store insn */420421l.add r4,r4,r1 /* Load the jump register value from the stack */422l.lwz r5,0(r4)423424l.srli r4,r3,26 /* Shift left to get the insn opcode */4254264271:428// l.mtspr r0,r5,SPR_EPCR_BASE429l.sw PT_PC(r1),r5430431l.sfeqi r4,0x26432l.bf lhs433l.sfeqi r4,0x25434l.bf lhz435l.sfeqi r4,0x22436l.bf lws437l.sfeqi r4,0x21438l.bf lwz439l.sfeqi r4,0x37440l.bf sh441l.sfeqi r4,0x35442l.bf sw443l.nop4444451: l.j 1b /* I don't know what to do */446l.nop447448lhs: l.lbs r5,0(r2)449l.slli r5,r5,8450l.lbz r6,1(r2)451l.or r5,r5,r6452l.srli r4,r3,19453l.andi r4,r4,0x7c454l.add r4,r4,r1455l.j align_end456l.sw 0(r4),r5457458lhz: l.lbz r5,0(r2)459l.slli r5,r5,8460l.lbz r6,1(r2)461l.or r5,r5,r6462l.srli r4,r3,19463l.andi r4,r4,0x7c464l.add r4,r4,r1465l.j align_end466l.sw 0(r4),r5467468lws: l.lbs r5,0(r2)469l.slli r5,r5,24470l.lbz r6,1(r2)471l.slli r6,r6,16472l.or r5,r5,r6473l.lbz r6,2(r2)474l.slli r6,r6,8475l.or r5,r5,r6476l.lbz r6,3(r2)477l.or r5,r5,r6478l.srli r4,r3,19479l.andi r4,r4,0x7c480l.add r4,r4,r1481l.j align_end482l.sw 0(r4),r5483484lwz: l.lbz r5,0(r2)485l.slli r5,r5,24486l.lbz r6,1(r2)487l.slli r6,r6,16488l.or r5,r5,r6489l.lbz r6,2(r2)490l.slli r6,r6,8491l.or r5,r5,r6492l.lbz r6,3(r2)493l.or r5,r5,r6494l.srli r4,r3,19495l.andi r4,r4,0x7c496l.add r4,r4,r1497l.j align_end498l.sw 0(r4),r5499500sh:501l.srli r4,r3,9502l.andi r4,r4,0x7c503l.add r4,r4,r1504l.lwz r5,0(r4)505l.sb 1(r2),r5506l.srli r5,r5,8507l.j align_end508l.sb 0(r2),r5509510sw:511l.srli r4,r3,9512l.andi r4,r4,0x7c513l.add r4,r4,r1514l.lwz r5,0(r4)515l.sb 3(r2),r5516l.srli r5,r5,8517l.sb 2(r2),r5518l.srli r5,r5,8519l.sb 1(r2),r5520l.srli r5,r5,8521l.j align_end522l.sb 0(r2),r5523524align_end:525l.j _ret_from_intr526l.nop527#endif528529/* ---[ 0x700: Illegal insn exception ]---------------------------------- */530531EXCEPTION_ENTRY(_illegal_instruction_handler)532/* r4: EA of fault (set by EXCEPTION_HANDLE) */533l.jal do_illegal_instruction534l.addi r3,r1,0 /* pt_regs */535536l.j _ret_from_exception537l.nop538539/* ---[ 0x800: External interrupt exception ]---------------------------- */540541EXCEPTION_ENTRY(_external_irq_handler)542#ifdef CONFIG_OPENRISC_ESR_EXCEPTION_BUG_CHECK543l.lwz r4,PT_SR(r1) // were interrupts enabled ?544l.andi r4,r4,SPR_SR_IEE545l.sfeqi r4,0546l.bnf 1f // ext irq enabled, all ok.547l.nop548549#ifdef CONFIG_PRINTK550l.addi r1,r1,-0x8551l.movhi r3,hi(42f)552l.ori r3,r3,lo(42f)553l.sw 0x0(r1),r3554l.jal _printk555l.sw 0x4(r1),r4556l.addi r1,r1,0x8557558.section .rodata, "a"55942:560.string "\n\rESR interrupt bug: in _external_irq_handler (ESR %x)\n\r"561.align 4562.previous563#endif564565l.ori r4,r4,SPR_SR_IEE // fix the bug566// l.sw PT_SR(r1),r45671:568#endif569CLEAR_LWA_FLAG(r3)570l.addi r3,r1,0571l.movhi r8,hi(generic_handle_arch_irq)572l.ori r8,r8,lo(generic_handle_arch_irq)573l.jalr r8574l.nop575l.j _ret_from_intr576l.nop577578/* ---[ 0x900: DTLB miss exception ]------------------------------------- */579580581/* ---[ 0xa00: ITLB miss exception ]------------------------------------- */582583584/* ---[ 0xb00: Range exception ]----------------------------------------- */585586UNHANDLED_EXCEPTION(_vector_0xb00,0xb00)587588/* ---[ 0xc00: Syscall exception ]--------------------------------------- */589590/*591* Syscalls are a special type of exception in that they are592* _explicitly_ invoked by userspace and can therefore be593* held to conform to the same ABI as normal functions with594* respect to whether registers are preserved across the call595* or not.596*/597598/* Upon syscall entry we just save the callee-saved registers599* and not the call-clobbered ones.600*/601602_string_syscall_return:603.string "syscall r9:0x%08x -> syscall(%ld) return %ld\0"604.align 4605606ENTRY(_sys_call_handler)607/* r1, EPCR, ESR a already saved */608l.sw PT_GPR2(r1),r2609/* r3-r8 must be saved because syscall restart relies610* on us being able to restart the syscall args... technically611* they should be clobbered, otherwise612*/613l.sw PT_GPR3(r1),r3614/*615* r4 already saved616* r4 holds the EEAR address of the fault, use it as screatch reg and617* then load the original r4618*/619CLEAR_LWA_FLAG(r4)620l.lwz r4,PT_GPR4(r1)621l.sw PT_GPR5(r1),r5622l.sw PT_GPR6(r1),r6623l.sw PT_GPR7(r1),r7624l.sw PT_GPR8(r1),r8625l.sw PT_GPR9(r1),r9626/* r10 already saved */627l.sw PT_GPR11(r1),r11628/* orig_gpr11 must be set for syscalls */629l.sw PT_ORIG_GPR11(r1),r11630/* r12,r13 already saved */631632/* r14-r28 (even) aren't touched by the syscall fast path below633* so we don't need to save them. However, the functions that return634* to userspace via a call to switch() DO need to save these because635* switch() effectively clobbers them... saving these registers for636* such functions is handled in their syscall wrappers (see fork, vfork,637* and clone, below).638639/* r30 is the only register we clobber in the fast path */640/* r30 already saved */641/* l.sw PT_GPR30(r1),r30 */642643_syscall_check_trace_enter:644/* syscalls run with interrupts enabled */645TRACE_IRQS_ON_SYSCALL646ENABLE_INTERRUPTS(r29) // enable interrupts, r29 is temp647648/* If TIF_SYSCALL_TRACE is set, then we want to do syscall tracing */649l.lwz r30,TI_FLAGS(r10)650l.andi r30,r30,_TIF_SYSCALL_TRACE651l.sfne r30,r0652l.bf _syscall_trace_enter653l.nop654655_syscall_check:656/* Ensure that the syscall number is reasonable */657l.sfgeui r11,__NR_syscalls658l.bf _syscall_badsys659l.nop660661_syscall_call:662l.movhi r29,hi(sys_call_table)663l.ori r29,r29,lo(sys_call_table)664l.slli r11,r11,2665l.add r29,r29,r11666l.lwz r29,0(r29)667668l.jalr r29669l.nop670671_syscall_return:672/* All syscalls return here... just pay attention to ret_from_fork673* which does it in a round-about way.674*/675l.sw PT_GPR11(r1),r11 // save return value676677#if 0678_syscall_debug:679l.movhi r3,hi(_string_syscall_return)680l.ori r3,r3,lo(_string_syscall_return)681l.ori r27,r0,2682l.sw -4(r1),r27683l.sw -8(r1),r11684l.lwz r29,PT_ORIG_GPR11(r1)685l.sw -12(r1),r29686l.lwz r29,PT_GPR9(r1)687l.sw -16(r1),r29688l.movhi r27,hi(_printk)689l.ori r27,r27,lo(_printk)690l.jalr r27691l.addi r1,r1,-16692l.addi r1,r1,16693#endif694#if 0695_syscall_show_regs:696l.movhi r27,hi(show_registers)697l.ori r27,r27,lo(show_registers)698l.jalr r27699l.or r3,r1,r1700#endif701702_syscall_check_trace_leave:703/* r30 is a callee-saved register so this should still hold the704* _TIF_SYSCALL_TRACE flag from _syscall_check_trace_enter above...705* _syscall_trace_leave expects syscall result to be in pt_regs->r11.706*/707l.sfne r30,r0708l.bf _syscall_trace_leave709l.nop710711/* This is where the exception-return code begins... interrupts need to be712* disabled the rest of the way here because we can't afford to miss any713* interrupts that set NEED_RESCHED or SIGNALPENDING... really true? */714715_syscall_check_work:716#ifdef CONFIG_DEBUG_RSEQ717l.jal rseq_syscall718l.ori r3,r1,0719#endif720/* Here we need to disable interrupts */721DISABLE_INTERRUPTS(r27,r29)722TRACE_IRQS_OFF723l.lwz r30,TI_FLAGS(r10)724l.andi r30,r30,_TIF_WORK_MASK725l.sfne r30,r0726727l.bnf _syscall_resume_userspace728l.nop729730/* Work pending follows a different return path, so we need to731* make sure that all the call-saved registers get into pt_regs732* before branching...733*/734l.sw PT_GPR14(r1),r14735l.sw PT_GPR16(r1),r16736l.sw PT_GPR18(r1),r18737l.sw PT_GPR20(r1),r20738l.sw PT_GPR22(r1),r22739l.sw PT_GPR24(r1),r24740l.sw PT_GPR26(r1),r26741l.sw PT_GPR28(r1),r28742743/* _work_pending needs to be called with interrupts disabled */744l.j _work_pending745l.nop746747_syscall_resume_userspace:748// ENABLE_INTERRUPTS(r29)749750751/* This is the hot path for returning to userspace from a syscall. If there's752* work to be done and the branch to _work_pending was taken above, then the753* return to userspace will be done via the normal exception return path...754* that path restores _all_ registers and will overwrite the "clobbered"755* registers with whatever garbage is in pt_regs -- that's OK because those756* registers are clobbered anyway and because the extra work is insignificant757* in the context of the extra work that _work_pending is doing.758759/* Once again, syscalls are special and only guarantee to preserve the760* same registers as a normal function call */761762/* The assumption here is that the registers r14-r28 (even) are untouched and763* don't need to be restored... be sure that that's really the case!764*/765766/* This is still too much... we should only be restoring what we actually767* clobbered... we should even be using 'scratch' (odd) regs above so that768* we don't need to restore anything, hardly...769*/770771l.lwz r2,PT_GPR2(r1)772773/* Restore args */774/* r3-r8 are technically clobbered, but syscall restart needs these775* to be restored...776*/777l.lwz r3,PT_GPR3(r1)778l.lwz r4,PT_GPR4(r1)779l.lwz r5,PT_GPR5(r1)780l.lwz r6,PT_GPR6(r1)781l.lwz r7,PT_GPR7(r1)782l.lwz r8,PT_GPR8(r1)783784l.lwz r9,PT_GPR9(r1)785l.lwz r10,PT_GPR10(r1)786l.lwz r11,PT_GPR11(r1)787788/* r30 is the only register we clobber in the fast path */789l.lwz r30,PT_GPR30(r1)790791/* Here we use r13-r19 (odd) as scratch regs */792l.lwz r13,PT_PC(r1)793l.lwz r15,PT_SR(r1)794l.lwz r1,PT_SP(r1)795/* Interrupts need to be disabled for setting EPCR and ESR796* so that another interrupt doesn't come in here and clobber797* them before we can use them for our l.rfe */798DISABLE_INTERRUPTS(r17,r19)799l.mtspr r0,r13,SPR_EPCR_BASE800l.mtspr r0,r15,SPR_ESR_BASE801l.rfe802803/* End of hot path!804* Keep the below tracing and error handling out of the hot path...805*/806807_syscall_trace_enter:808/* Here we pass pt_regs to do_syscall_trace_enter. Make sure809* that function is really getting all the info it needs as810* pt_regs isn't a complete set of userspace regs, just the811* ones relevant to the syscall...812*813* Note use of delay slot for setting argument.814*/815l.jal do_syscall_trace_enter816l.addi r3,r1,0817818/* Restore arguments (not preserved across do_syscall_trace_enter)819* so that we can do the syscall for real and return to the syscall820* hot path.821*/822l.lwz r11,PT_GPR11(r1)823l.lwz r3,PT_GPR3(r1)824l.lwz r4,PT_GPR4(r1)825l.lwz r5,PT_GPR5(r1)826l.lwz r6,PT_GPR6(r1)827l.lwz r7,PT_GPR7(r1)828829l.j _syscall_check830l.lwz r8,PT_GPR8(r1)831832_syscall_trace_leave:833l.jal do_syscall_trace_leave834l.addi r3,r1,0835836l.j _syscall_check_work837l.nop838839_syscall_badsys:840/* Here we effectively pretend to have executed an imaginary841* syscall that returns -ENOSYS and then return to the regular842* syscall hot path.843* Note that "return value" is set in the delay slot...844*/845l.j _syscall_return846l.addi r11,r0,-ENOSYS847848/******* END SYSCALL HANDLING *******/849850/* ---[ 0xd00: Floating Point exception ]-------------------------------- */851852EXCEPTION_ENTRY(_fpe_trap_handler)853CLEAR_LWA_FLAG(r3)854855/* r4: EA of fault (set by EXCEPTION_HANDLE) */856l.jal do_fpe_trap857l.addi r3,r1,0 /* pt_regs */858859l.j _ret_from_exception860l.nop861862/* ---[ 0xe00: Trap exception ]------------------------------------------ */863864EXCEPTION_ENTRY(_trap_handler)865CLEAR_LWA_FLAG(r3)866/* r4: EA of fault (set by EXCEPTION_HANDLE) */867l.jal do_trap868l.addi r3,r1,0 /* pt_regs */869870l.j _ret_from_exception871l.nop872873/* ---[ 0xf00: Reserved exception ]-------------------------------------- */874875UNHANDLED_EXCEPTION(_vector_0xf00,0xf00)876877/* ---[ 0x1000: Reserved exception ]------------------------------------- */878879UNHANDLED_EXCEPTION(_vector_0x1000,0x1000)880881/* ---[ 0x1100: Reserved exception ]------------------------------------- */882883UNHANDLED_EXCEPTION(_vector_0x1100,0x1100)884885/* ---[ 0x1200: Reserved exception ]------------------------------------- */886887UNHANDLED_EXCEPTION(_vector_0x1200,0x1200)888889/* ---[ 0x1300: Reserved exception ]------------------------------------- */890891UNHANDLED_EXCEPTION(_vector_0x1300,0x1300)892893/* ---[ 0x1400: Reserved exception ]------------------------------------- */894895UNHANDLED_EXCEPTION(_vector_0x1400,0x1400)896897/* ---[ 0x1500: Reserved exception ]------------------------------------- */898899UNHANDLED_EXCEPTION(_vector_0x1500,0x1500)900901/* ---[ 0x1600: Reserved exception ]------------------------------------- */902903UNHANDLED_EXCEPTION(_vector_0x1600,0x1600)904905/* ---[ 0x1700: Reserved exception ]------------------------------------- */906907UNHANDLED_EXCEPTION(_vector_0x1700,0x1700)908909/* ---[ 0x1800: Reserved exception ]------------------------------------- */910911UNHANDLED_EXCEPTION(_vector_0x1800,0x1800)912913/* ---[ 0x1900: Reserved exception ]------------------------------------- */914915UNHANDLED_EXCEPTION(_vector_0x1900,0x1900)916917/* ---[ 0x1a00: Reserved exception ]------------------------------------- */918919UNHANDLED_EXCEPTION(_vector_0x1a00,0x1a00)920921/* ---[ 0x1b00: Reserved exception ]------------------------------------- */922923UNHANDLED_EXCEPTION(_vector_0x1b00,0x1b00)924925/* ---[ 0x1c00: Reserved exception ]------------------------------------- */926927UNHANDLED_EXCEPTION(_vector_0x1c00,0x1c00)928929/* ---[ 0x1d00: Reserved exception ]------------------------------------- */930931UNHANDLED_EXCEPTION(_vector_0x1d00,0x1d00)932933/* ---[ 0x1e00: Reserved exception ]------------------------------------- */934935UNHANDLED_EXCEPTION(_vector_0x1e00,0x1e00)936937/* ---[ 0x1f00: Reserved exception ]------------------------------------- */938939UNHANDLED_EXCEPTION(_vector_0x1f00,0x1f00)940941/* ========================================================[ return ] === */942943_resume_userspace:944DISABLE_INTERRUPTS(r3,r4)945TRACE_IRQS_OFF946l.lwz r4,TI_FLAGS(r10)947l.andi r13,r4,_TIF_WORK_MASK948l.sfeqi r13,0949l.bf _restore_all950l.nop951952_work_pending:953l.lwz r5,PT_ORIG_GPR11(r1)954l.sfltsi r5,0955l.bnf 1f956l.nop957l.andi r5,r5,09581:959l.jal do_work_pending960l.ori r3,r1,0 /* pt_regs */961962l.sfeqi r11,0963l.bf _restore_all964l.nop965l.sfltsi r11,0966l.bnf 1f967l.nop968l.and r11,r11,r0969l.ori r11,r11,__NR_restart_syscall970l.j _syscall_check_trace_enter971l.nop9721:973l.lwz r11,PT_ORIG_GPR11(r1)974/* Restore arg registers */975l.lwz r3,PT_GPR3(r1)976l.lwz r4,PT_GPR4(r1)977l.lwz r5,PT_GPR5(r1)978l.lwz r6,PT_GPR6(r1)979l.lwz r7,PT_GPR7(r1)980l.j _syscall_check_trace_enter981l.lwz r8,PT_GPR8(r1)982983_restore_all:984#ifdef CONFIG_TRACE_IRQFLAGS985l.lwz r4,PT_SR(r1)986l.andi r3,r4,(SPR_SR_IEE|SPR_SR_TEE)987l.sfeq r3,r0 /* skip trace if irqs were off */988l.bf skip_hardirqs_on989l.nop990TRACE_IRQS_ON991skip_hardirqs_on:992#endif993RESTORE_ALL994/* This returns to userspace code */995996997ENTRY(_ret_from_intr)998ENTRY(_ret_from_exception)999l.lwz r4,PT_SR(r1)1000l.andi r3,r4,SPR_SR_SM1001l.sfeqi r3,01002l.bnf _restore_all1003l.nop1004l.j _resume_userspace1005l.nop10061007ENTRY(ret_from_fork)1008l.jal schedule_tail1009l.nop10101011/* Check if we are a kernel thread */1012l.sfeqi r20,01013l.bf 1f1014l.nop10151016/* ...we are a kernel thread so invoke the requested callback */1017l.jalr r201018l.or r3,r22,r0101910201:1021/* _syscall_returns expect r11 to contain return value */1022l.lwz r11,PT_GPR11(r1)10231024/* The syscall fast path return expects call-saved registers1025* r14-r28 to be untouched, so we restore them here as they1026* will have been effectively clobbered when arriving here1027* via the call to switch()1028*/1029l.lwz r14,PT_GPR14(r1)1030l.lwz r16,PT_GPR16(r1)1031l.lwz r18,PT_GPR18(r1)1032l.lwz r20,PT_GPR20(r1)1033l.lwz r22,PT_GPR22(r1)1034l.lwz r24,PT_GPR24(r1)1035l.lwz r26,PT_GPR26(r1)1036l.lwz r28,PT_GPR28(r1)10371038l.j _syscall_return1039l.nop10401041/* ========================================================[ switch ] === */10421043/*1044* This routine switches between two different tasks. The process1045* state of one is saved on its kernel stack. Then the state1046* of the other is restored from its kernel stack. The memory1047* management hardware is updated to the second process's state.1048* Finally, we can return to the second process, via the 'return'.1049*1050* Note: there are two ways to get to the "going out" portion1051* of this code; either by coming in via the entry (_switch)1052* or via "fork" which must set up an environment equivalent1053* to the "_switch" path. If you change this (or in particular, the1054* SAVE_REGS macro), you'll have to change the fork code also.1055*/105610571058/* _switch MUST never lay on page boundry, cause it runs from1059* effective addresses and beeing interrupted by iTLB miss would kill it.1060* dTLB miss seems to never accour in the bad place since data accesses1061* are from task structures which are always page aligned.1062*1063* The problem happens in RESTORE_ALL where we first set the EPCR1064* register, then load the previous register values and only at the end call1065* the l.rfe instruction. If get TLB miss in beetwen the EPCR register gets1066* garbled and we end up calling l.rfe with the wrong EPCR. (same probably1067* holds for ESR)1068*1069* To avoid this problems it is sufficient to align _switch to1070* some nice round number smaller than it's size...1071*/10721073/* ABI rules apply here... we either enter _switch via schedule() or via1074* an imaginary call to which we shall return at return_from_fork. Either1075* way, we are a function call and only need to preserve the callee-saved1076* registers when we return. As such, we don't need to save the registers1077* on the stack that we won't be returning as they were...1078*/10791080.align 0x4001081ENTRY(_switch)1082/* We don't store SR as _switch only gets called in a context where1083* the SR will be the same going in and coming out... */10841085/* Set up new pt_regs struct for saving task state */1086l.addi r1,r1,-(INT_FRAME_SIZE)10871088/* No need to store r1/PT_SP as it goes into KSP below */1089l.sw PT_GPR2(r1),r21090l.sw PT_GPR9(r1),r910911092/* Save callee-saved registers to the new pt_regs */1093l.sw PT_GPR14(r1),r141094l.sw PT_GPR16(r1),r161095l.sw PT_GPR18(r1),r181096l.sw PT_GPR20(r1),r201097l.sw PT_GPR22(r1),r221098l.sw PT_GPR24(r1),r241099l.sw PT_GPR26(r1),r261100l.sw PT_GPR28(r1),r281101l.sw PT_GPR30(r1),r3011021103l.addi r11,r10,0 /* Save old 'current' to 'last' return value*/11041105/* We use thread_info->ksp for storing the address of the above1106* structure so that we can get back to it later... we don't want1107* to lose the value of thread_info->ksp, though, so store it as1108* pt_regs->sp so that we can easily restore it when we are made1109* live again...1110*/11111112/* Save the old value of thread_info->ksp as pt_regs->sp */1113l.lwz r29,TI_KSP(r10)1114l.sw PT_SP(r1),r2911151116/* Swap kernel stack pointers */1117l.sw TI_KSP(r10),r1 /* Save old stack pointer */1118l.or r10,r4,r0 /* Set up new current_thread_info */1119l.lwz r1,TI_KSP(r10) /* Load new stack pointer */11201121/* Restore the old value of thread_info->ksp */1122l.lwz r29,PT_SP(r1)1123l.sw TI_KSP(r10),r2911241125/* ...and restore the registers, except r11 because the return value1126* has already been set above.1127*/1128l.lwz r2,PT_GPR2(r1)1129l.lwz r9,PT_GPR9(r1)1130/* No need to restore r10 */1131/* ...and do not restore r11 */11321133/* Restore callee-saved registers */1134l.lwz r14,PT_GPR14(r1)1135l.lwz r16,PT_GPR16(r1)1136l.lwz r18,PT_GPR18(r1)1137l.lwz r20,PT_GPR20(r1)1138l.lwz r22,PT_GPR22(r1)1139l.lwz r24,PT_GPR24(r1)1140l.lwz r26,PT_GPR26(r1)1141l.lwz r28,PT_GPR28(r1)1142l.lwz r30,PT_GPR30(r1)11431144/* Unwind stack to pre-switch state */1145l.addi r1,r1,(INT_FRAME_SIZE)11461147/* Return via the link-register back to where we 'came from', where1148* that may be either schedule(), ret_from_fork(), or1149* ret_from_kernel_thread(). If we are returning to a new thread,1150* we are expected to have set up the arg to schedule_tail already,1151* hence we do so here unconditionally:1152*/1153l.lwz r3,TI_TASK(r3) /* Load 'prev' as schedule_tail arg */1154l.jr r91155l.nop11561157/* ==================================================================== */11581159/* These all use the delay slot for setting the argument register, so the1160* jump is always happening after the l.addi instruction.1161*1162* These are all just wrappers that don't touch the link-register r9, so the1163* return from the "real" syscall function will return back to the syscall1164* code that did the l.jal that brought us here.1165*/11661167/* fork requires that we save all the callee-saved registers because they1168* are all effectively clobbered by the call to _switch. Here we store1169* all the registers that aren't touched by the syscall fast path and thus1170* weren't saved there.1171*/11721173_fork_save_extra_regs_and_call:1174l.sw PT_GPR14(r1),r141175l.sw PT_GPR16(r1),r161176l.sw PT_GPR18(r1),r181177l.sw PT_GPR20(r1),r201178l.sw PT_GPR22(r1),r221179l.sw PT_GPR24(r1),r241180l.sw PT_GPR26(r1),r261181l.jr r291182l.sw PT_GPR28(r1),r2811831184ENTRY(__sys_clone)1185l.movhi r29,hi(sys_clone)1186l.j _fork_save_extra_regs_and_call1187l.ori r29,r29,lo(sys_clone)11881189ENTRY(__sys_clone3)1190l.movhi r29,hi(sys_clone3)1191l.j _fork_save_extra_regs_and_call1192l.ori r29,r29,lo(sys_clone3)11931194ENTRY(__sys_fork)1195l.movhi r29,hi(sys_fork)1196l.j _fork_save_extra_regs_and_call1197l.ori r29,r29,lo(sys_fork)11981199ENTRY(sys_rt_sigreturn)1200l.jal _sys_rt_sigreturn1201l.addi r3,r1,01202l.sfne r30,r01203l.bnf _no_syscall_trace1204l.nop1205l.jal do_syscall_trace_leave1206l.addi r3,r1,01207_no_syscall_trace:1208l.j _resume_userspace1209l.nop12101211/* This is a catch-all syscall for atomic instructions for the OpenRISC 1000.1212* The functions takes a variable number of parameters depending on which1213* particular flavour of atomic you want... parameter 1 is a flag identifying1214* the atomic in question. Currently, this function implements the1215* following variants:1216*1217* XCHG:1218* @flag: 11219* @ptr1:1220* @ptr2:1221* Atomically exchange the values in pointers 1 and 2.1222*1223*/12241225ENTRY(sys_or1k_atomic)1226/* FIXME: This ignores r3 and always does an XCHG */1227DISABLE_INTERRUPTS(r17,r19)1228l.lwz r29,0(r4)1229l.lwz r27,0(r5)1230l.sw 0(r4),r271231l.sw 0(r5),r291232ENABLE_INTERRUPTS(r17)1233l.jr r91234l.or r11,r0,r012351236/* ============================================================[ EOF ]=== */123712381239