1ca54502bSMichal Simek/* 2ca54502bSMichal Simek * Low-level system-call handling, trap handlers and context-switching 3ca54502bSMichal Simek * 4ca54502bSMichal Simek * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu> 5ca54502bSMichal Simek * Copyright (C) 2008-2009 PetaLogix 6ca54502bSMichal Simek * Copyright (C) 2003 John Williams <jwilliams@itee.uq.edu.au> 7ca54502bSMichal Simek * Copyright (C) 2001,2002 NEC Corporation 8ca54502bSMichal Simek * Copyright (C) 2001,2002 Miles Bader <miles@gnu.org> 9ca54502bSMichal Simek * 10ca54502bSMichal Simek * This file is subject to the terms and conditions of the GNU General 11ca54502bSMichal Simek * Public License. See the file COPYING in the main directory of this 12ca54502bSMichal Simek * archive for more details. 13ca54502bSMichal Simek * 14ca54502bSMichal Simek * Written by Miles Bader <miles@gnu.org> 15ca54502bSMichal Simek * Heavily modified by John Williams for Microblaze 16ca54502bSMichal Simek */ 17ca54502bSMichal Simek 18ca54502bSMichal Simek#include <linux/sys.h> 19ca54502bSMichal Simek#include <linux/linkage.h> 20ca54502bSMichal Simek 21ca54502bSMichal Simek#include <asm/entry.h> 22ca54502bSMichal Simek#include <asm/current.h> 23ca54502bSMichal Simek#include <asm/processor.h> 24ca54502bSMichal Simek#include <asm/exceptions.h> 25ca54502bSMichal Simek#include <asm/asm-offsets.h> 26ca54502bSMichal Simek#include <asm/thread_info.h> 27ca54502bSMichal Simek 28ca54502bSMichal Simek#include <asm/page.h> 29ca54502bSMichal Simek#include <asm/unistd.h> 30ca54502bSMichal Simek 31ca54502bSMichal Simek#include <linux/errno.h> 32ca54502bSMichal Simek#include <asm/signal.h> 33ca54502bSMichal Simek 3411d51360SMichal Simek#undef DEBUG 3511d51360SMichal Simek 36d8748e73SMichal Simek#ifdef DEBUG 37d8748e73SMichal Simek/* Create space for syscalls counting. */ 38d8748e73SMichal Simek.section .data 39d8748e73SMichal Simek.global syscall_debug_table 40d8748e73SMichal Simek.align 4 41d8748e73SMichal Simeksyscall_debug_table: 42d8748e73SMichal Simek .space (__NR_syscalls * 4) 43d8748e73SMichal Simek#endif /* DEBUG */ 44d8748e73SMichal Simek 45ca54502bSMichal Simek#define C_ENTRY(name) .globl name; .align 4; name 46ca54502bSMichal Simek 47ca54502bSMichal Simek/* 48ca54502bSMichal Simek * Various ways of setting and clearing BIP in flags reg. 49ca54502bSMichal Simek * This is mucky, but necessary using microblaze version that 50ca54502bSMichal Simek * allows msr ops to write to BIP 51ca54502bSMichal Simek */ 52ca54502bSMichal Simek#if CONFIG_XILINX_MICROBLAZE0_USE_MSR_INSTR 53ca54502bSMichal Simek .macro clear_bip 5466f7de86SMichal Simek msrclr r0, MSR_BIP 55ca54502bSMichal Simek .endm 56ca54502bSMichal Simek 57ca54502bSMichal Simek .macro set_bip 5866f7de86SMichal Simek msrset r0, MSR_BIP 59ca54502bSMichal Simek .endm 60ca54502bSMichal Simek 61ca54502bSMichal Simek .macro clear_eip 6266f7de86SMichal Simek msrclr r0, MSR_EIP 63ca54502bSMichal Simek .endm 64ca54502bSMichal Simek 65ca54502bSMichal Simek .macro set_ee 6666f7de86SMichal Simek msrset r0, MSR_EE 67ca54502bSMichal Simek .endm 68ca54502bSMichal Simek 69ca54502bSMichal Simek .macro disable_irq 7066f7de86SMichal Simek msrclr r0, MSR_IE 71ca54502bSMichal Simek .endm 72ca54502bSMichal Simek 73ca54502bSMichal Simek .macro enable_irq 7466f7de86SMichal Simek msrset r0, MSR_IE 75ca54502bSMichal Simek .endm 76ca54502bSMichal Simek 77ca54502bSMichal Simek .macro set_ums 7866f7de86SMichal Simek msrset r0, MSR_UMS 7966f7de86SMichal Simek msrclr r0, MSR_VMS 80ca54502bSMichal Simek .endm 81ca54502bSMichal Simek 82ca54502bSMichal Simek .macro set_vms 8366f7de86SMichal Simek msrclr r0, MSR_UMS 8466f7de86SMichal Simek msrset r0, MSR_VMS 85ca54502bSMichal Simek .endm 86ca54502bSMichal Simek 87b318067eSMichal Simek .macro clear_ums 8866f7de86SMichal Simek msrclr r0, MSR_UMS 89b318067eSMichal Simek .endm 90b318067eSMichal Simek 91ca54502bSMichal Simek .macro clear_vms_ums 9266f7de86SMichal Simek msrclr r0, MSR_VMS | MSR_UMS 93ca54502bSMichal Simek .endm 94ca54502bSMichal Simek#else 95ca54502bSMichal Simek .macro clear_bip 96ca54502bSMichal Simek mfs r11, rmsr 97ca54502bSMichal Simek andi r11, r11, ~MSR_BIP 98ca54502bSMichal Simek mts rmsr, r11 99ca54502bSMichal Simek .endm 100ca54502bSMichal Simek 101ca54502bSMichal Simek .macro set_bip 102ca54502bSMichal Simek mfs r11, rmsr 103ca54502bSMichal Simek ori r11, r11, MSR_BIP 104ca54502bSMichal Simek mts rmsr, r11 105ca54502bSMichal Simek .endm 106ca54502bSMichal Simek 107ca54502bSMichal Simek .macro clear_eip 108ca54502bSMichal Simek mfs r11, rmsr 109ca54502bSMichal Simek andi r11, r11, ~MSR_EIP 110ca54502bSMichal Simek mts rmsr, r11 111ca54502bSMichal Simek .endm 112ca54502bSMichal Simek 113ca54502bSMichal Simek .macro set_ee 114ca54502bSMichal Simek mfs r11, rmsr 115ca54502bSMichal Simek ori r11, r11, MSR_EE 116ca54502bSMichal Simek mts rmsr, r11 117ca54502bSMichal Simek .endm 118ca54502bSMichal Simek 119ca54502bSMichal Simek .macro disable_irq 120ca54502bSMichal Simek mfs r11, rmsr 121ca54502bSMichal Simek andi r11, r11, ~MSR_IE 122ca54502bSMichal Simek mts rmsr, r11 123ca54502bSMichal Simek .endm 124ca54502bSMichal Simek 125ca54502bSMichal Simek .macro enable_irq 126ca54502bSMichal Simek mfs r11, rmsr 127ca54502bSMichal Simek ori r11, r11, MSR_IE 128ca54502bSMichal Simek mts rmsr, r11 129ca54502bSMichal Simek .endm 130ca54502bSMichal Simek 131ca54502bSMichal Simek .macro set_ums 132ca54502bSMichal Simek mfs r11, rmsr 133ca54502bSMichal Simek ori r11, r11, MSR_VMS 134ca54502bSMichal Simek andni r11, r11, MSR_UMS 135ca54502bSMichal Simek mts rmsr, r11 136ca54502bSMichal Simek .endm 137ca54502bSMichal Simek 138ca54502bSMichal Simek .macro set_vms 139ca54502bSMichal Simek mfs r11, rmsr 140ca54502bSMichal Simek ori r11, r11, MSR_VMS 141ca54502bSMichal Simek andni r11, r11, MSR_UMS 142ca54502bSMichal Simek mts rmsr, r11 143ca54502bSMichal Simek .endm 144ca54502bSMichal Simek 145b318067eSMichal Simek .macro clear_ums 146b318067eSMichal Simek mfs r11, rmsr 147b318067eSMichal Simek andni r11, r11, MSR_UMS 148b318067eSMichal Simek mts rmsr,r11 149b318067eSMichal Simek .endm 150b318067eSMichal Simek 151ca54502bSMichal Simek .macro clear_vms_ums 152ca54502bSMichal Simek mfs r11, rmsr 153ca54502bSMichal Simek andni r11, r11, (MSR_VMS|MSR_UMS) 154ca54502bSMichal Simek mts rmsr,r11 155ca54502bSMichal Simek .endm 156ca54502bSMichal Simek#endif 157ca54502bSMichal Simek 158ca54502bSMichal Simek/* Define how to call high-level functions. With MMU, virtual mode must be 159ca54502bSMichal Simek * enabled when calling the high-level function. Clobbers R11. 160ca54502bSMichal Simek * VM_ON, VM_OFF, DO_JUMP_BIPCLR, DO_CALL 161ca54502bSMichal Simek */ 162ca54502bSMichal Simek 163ca54502bSMichal Simek/* turn on virtual protected mode save */ 164ca54502bSMichal Simek#define VM_ON \ 165ca54502bSMichal Simek set_ums; \ 166ca54502bSMichal Simek rted r0, 2f; \ 167a4a94dbfSMichal Simek nop; \ 168a4a94dbfSMichal Simek2: 169ca54502bSMichal Simek 170ca54502bSMichal Simek/* turn off virtual protected mode save and user mode save*/ 171ca54502bSMichal Simek#define VM_OFF \ 172ca54502bSMichal Simek clear_vms_ums; \ 173ca54502bSMichal Simek rted r0, TOPHYS(1f); \ 174a4a94dbfSMichal Simek nop; \ 175a4a94dbfSMichal Simek1: 176ca54502bSMichal Simek 177ca54502bSMichal Simek#define SAVE_REGS \ 178*6e83557cSMichal Simek swi r2, r1, PT_R2; /* Save SDA */ \ 179*6e83557cSMichal Simek swi r3, r1, PT_R3; \ 180*6e83557cSMichal Simek swi r4, r1, PT_R4; \ 181*6e83557cSMichal Simek swi r5, r1, PT_R5; \ 182*6e83557cSMichal Simek swi r6, r1, PT_R6; \ 183*6e83557cSMichal Simek swi r7, r1, PT_R7; \ 184*6e83557cSMichal Simek swi r8, r1, PT_R8; \ 185*6e83557cSMichal Simek swi r9, r1, PT_R9; \ 186*6e83557cSMichal Simek swi r10, r1, PT_R10; \ 187*6e83557cSMichal Simek swi r11, r1, PT_R11; /* save clobbered regs after rval */\ 188*6e83557cSMichal Simek swi r12, r1, PT_R12; \ 189*6e83557cSMichal Simek swi r13, r1, PT_R13; /* Save SDA2 */ \ 190*6e83557cSMichal Simek swi r14, r1, PT_PC; /* PC, before IRQ/trap */ \ 191*6e83557cSMichal Simek swi r15, r1, PT_R15; /* Save LP */ \ 192*6e83557cSMichal Simek swi r16, r1, PT_R16; \ 193*6e83557cSMichal Simek swi r17, r1, PT_R17; \ 194*6e83557cSMichal Simek swi r18, r1, PT_R18; /* Save asm scratch reg */ \ 195*6e83557cSMichal Simek swi r19, r1, PT_R19; \ 196*6e83557cSMichal Simek swi r20, r1, PT_R20; \ 197*6e83557cSMichal Simek swi r21, r1, PT_R21; \ 198*6e83557cSMichal Simek swi r22, r1, PT_R22; \ 199*6e83557cSMichal Simek swi r23, r1, PT_R23; \ 200*6e83557cSMichal Simek swi r24, r1, PT_R24; \ 201*6e83557cSMichal Simek swi r25, r1, PT_R25; \ 202*6e83557cSMichal Simek swi r26, r1, PT_R26; \ 203*6e83557cSMichal Simek swi r27, r1, PT_R27; \ 204*6e83557cSMichal Simek swi r28, r1, PT_R28; \ 205*6e83557cSMichal Simek swi r29, r1, PT_R29; \ 206*6e83557cSMichal Simek swi r30, r1, PT_R30; \ 207*6e83557cSMichal Simek swi r31, r1, PT_R31; /* Save current task reg */ \ 208ca54502bSMichal Simek mfs r11, rmsr; /* save MSR */ \ 209*6e83557cSMichal Simek swi r11, r1, PT_MSR; 210ca54502bSMichal Simek 211ca54502bSMichal Simek#define RESTORE_REGS \ 212*6e83557cSMichal Simek lwi r11, r1, PT_MSR; \ 213ca54502bSMichal Simek mts rmsr , r11; \ 214*6e83557cSMichal Simek lwi r2, r1, PT_R2; /* restore SDA */ \ 215*6e83557cSMichal Simek lwi r3, r1, PT_R3; \ 216*6e83557cSMichal Simek lwi r4, r1, PT_R4; \ 217*6e83557cSMichal Simek lwi r5, r1, PT_R5; \ 218*6e83557cSMichal Simek lwi r6, r1, PT_R6; \ 219*6e83557cSMichal Simek lwi r7, r1, PT_R7; \ 220*6e83557cSMichal Simek lwi r8, r1, PT_R8; \ 221*6e83557cSMichal Simek lwi r9, r1, PT_R9; \ 222*6e83557cSMichal Simek lwi r10, r1, PT_R10; \ 223*6e83557cSMichal Simek lwi r11, r1, PT_R11; /* restore clobbered regs after rval */\ 224*6e83557cSMichal Simek lwi r12, r1, PT_R12; \ 225*6e83557cSMichal Simek lwi r13, r1, PT_R13; /* restore SDA2 */ \ 226*6e83557cSMichal Simek lwi r14, r1, PT_PC; /* RESTORE_LINK PC, before IRQ/trap */\ 227*6e83557cSMichal Simek lwi r15, r1, PT_R15; /* restore LP */ \ 228*6e83557cSMichal Simek lwi r16, r1, PT_R16; \ 229*6e83557cSMichal Simek lwi r17, r1, PT_R17; \ 230*6e83557cSMichal Simek lwi r18, r1, PT_R18; /* restore asm scratch reg */ \ 231*6e83557cSMichal Simek lwi r19, r1, PT_R19; \ 232*6e83557cSMichal Simek lwi r20, r1, PT_R20; \ 233*6e83557cSMichal Simek lwi r21, r1, PT_R21; \ 234*6e83557cSMichal Simek lwi r22, r1, PT_R22; \ 235*6e83557cSMichal Simek lwi r23, r1, PT_R23; \ 236*6e83557cSMichal Simek lwi r24, r1, PT_R24; \ 237*6e83557cSMichal Simek lwi r25, r1, PT_R25; \ 238*6e83557cSMichal Simek lwi r26, r1, PT_R26; \ 239*6e83557cSMichal Simek lwi r27, r1, PT_R27; \ 240*6e83557cSMichal Simek lwi r28, r1, PT_R28; \ 241*6e83557cSMichal Simek lwi r29, r1, PT_R29; \ 242*6e83557cSMichal Simek lwi r30, r1, PT_R30; \ 243*6e83557cSMichal Simek lwi r31, r1, PT_R31; /* Restore cur task reg */ 244ca54502bSMichal Simek 245e5d2af2bSMichal Simek#define SAVE_STATE \ 246e5d2af2bSMichal Simek swi r1, r0, TOPHYS(PER_CPU(ENTRY_SP)); /* save stack */ \ 247e5d2af2bSMichal Simek /* See if already in kernel mode.*/ \ 248e5d2af2bSMichal Simek mfs r1, rmsr; \ 249e5d2af2bSMichal Simek andi r1, r1, MSR_UMS; \ 250e5d2af2bSMichal Simek bnei r1, 1f; \ 251e5d2af2bSMichal Simek /* Kernel-mode state save. */ \ 252e5d2af2bSMichal Simek /* Reload kernel stack-ptr. */ \ 253e5d2af2bSMichal Simek lwi r1, r0, TOPHYS(PER_CPU(ENTRY_SP)); \ 254287503faSMichal Simek /* FIXME: I can add these two lines to one */ \ 255287503faSMichal Simek /* tophys(r1,r1); */ \ 256*6e83557cSMichal Simek /* addik r1, r1, -PT_SIZE; */ \ 257*6e83557cSMichal Simek addik r1, r1, CONFIG_KERNEL_BASE_ADDR - CONFIG_KERNEL_START - PT_SIZE; \ 258e5d2af2bSMichal Simek SAVE_REGS \ 259e5d2af2bSMichal Simek brid 2f; \ 260*6e83557cSMichal Simek swi r1, r1, PT_MODE; \ 261e5d2af2bSMichal Simek1: /* User-mode state save. */ \ 262e5d2af2bSMichal Simek lwi r1, r0, TOPHYS(PER_CPU(CURRENT_SAVE)); /* get saved current */\ 263e5d2af2bSMichal Simek tophys(r1,r1); \ 264e5d2af2bSMichal Simek lwi r1, r1, TS_THREAD_INFO; /* get the thread info */ \ 265287503faSMichal Simek /* MS these three instructions can be added to one */ \ 266287503faSMichal Simek /* addik r1, r1, THREAD_SIZE; */ \ 267287503faSMichal Simek /* tophys(r1,r1); */ \ 268*6e83557cSMichal Simek /* addik r1, r1, -PT_SIZE; */ \ 269*6e83557cSMichal Simek addik r1, r1, THREAD_SIZE + CONFIG_KERNEL_BASE_ADDR - CONFIG_KERNEL_START - PT_SIZE; \ 270e5d2af2bSMichal Simek SAVE_REGS \ 271e5d2af2bSMichal Simek lwi r11, r0, TOPHYS(PER_CPU(ENTRY_SP)); \ 272*6e83557cSMichal Simek swi r11, r1, PT_R1; /* Store user SP. */ \ 273*6e83557cSMichal Simek swi r0, r1, PT_MODE; /* Was in user-mode. */ \ 274e5d2af2bSMichal Simek /* MS: I am clearing UMS even in case when I come from kernel space */ \ 275e5d2af2bSMichal Simek clear_ums; \ 276e5d2af2bSMichal Simek2: lwi CURRENT_TASK, r0, TOPHYS(PER_CPU(CURRENT_SAVE)); 277e5d2af2bSMichal Simek 278ca54502bSMichal Simek.text 279ca54502bSMichal Simek 280ca54502bSMichal Simek/* 281ca54502bSMichal Simek * User trap. 282ca54502bSMichal Simek * 283ca54502bSMichal Simek * System calls are handled here. 284ca54502bSMichal Simek * 285ca54502bSMichal Simek * Syscall protocol: 286ca54502bSMichal Simek * Syscall number in r12, args in r5-r10 287ca54502bSMichal Simek * Return value in r3 288ca54502bSMichal Simek * 289ca54502bSMichal Simek * Trap entered via brki instruction, so BIP bit is set, and interrupts 290ca54502bSMichal Simek * are masked. This is nice, means we don't have to CLI before state save 291ca54502bSMichal Simek */ 292ca54502bSMichal SimekC_ENTRY(_user_exception): 2930e41c909SMichal Simek swi r1, r0, TOPHYS(PER_CPU(ENTRY_SP)) /* save stack */ 2949da63458SMichal Simek addi r14, r14, 4 /* return address is 4 byte after call */ 295ca54502bSMichal Simek 2969da63458SMichal Simek mfs r1, rmsr 2979da63458SMichal Simek nop 2989da63458SMichal Simek andi r1, r1, MSR_UMS 2999da63458SMichal Simek bnei r1, 1f 3009da63458SMichal Simek 3019da63458SMichal Simek/* Kernel-mode state save - kernel execve */ 3029da63458SMichal Simek lwi r1, r0, TOPHYS(PER_CPU(ENTRY_SP)); /* Reload kernel stack-ptr*/ 3039da63458SMichal Simek tophys(r1,r1); 3049da63458SMichal Simek 305*6e83557cSMichal Simek addik r1, r1, -PT_SIZE; /* Make room on the stack. */ 3069da63458SMichal Simek SAVE_REGS 3079da63458SMichal Simek 308*6e83557cSMichal Simek swi r1, r1, PT_MODE; /* pt_regs -> kernel mode */ 3099da63458SMichal Simek brid 2f; 3109da63458SMichal Simek nop; /* Fill delay slot */ 3119da63458SMichal Simek 3129da63458SMichal Simek/* User-mode state save. */ 3139da63458SMichal Simek1: 314ca54502bSMichal Simek lwi r1, r0, TOPHYS(PER_CPU(CURRENT_SAVE)); /* get saved current */ 315ca54502bSMichal Simek tophys(r1,r1); 316ca54502bSMichal Simek lwi r1, r1, TS_THREAD_INFO; /* get stack from task_struct */ 3179da63458SMichal Simek/* calculate kernel stack pointer from task struct 8k */ 3189da63458SMichal Simek addik r1, r1, THREAD_SIZE; 3199da63458SMichal Simek tophys(r1,r1); 3209da63458SMichal Simek 321*6e83557cSMichal Simek addik r1, r1, -PT_SIZE; /* Make room on the stack. */ 322ca54502bSMichal Simek SAVE_REGS 323*6e83557cSMichal Simek swi r0, r1, PT_R3 324*6e83557cSMichal Simek swi r0, r1, PT_R4 325ca54502bSMichal Simek 326*6e83557cSMichal Simek swi r0, r1, PT_MODE; /* Was in user-mode. */ 327ca54502bSMichal Simek lwi r11, r0, TOPHYS(PER_CPU(ENTRY_SP)); 328*6e83557cSMichal Simek swi r11, r1, PT_R1; /* Store user SP. */ 32925f6e596SMichal Simek clear_ums; 3309da63458SMichal Simek2: lwi CURRENT_TASK, r0, TOPHYS(PER_CPU(CURRENT_SAVE)); 331ca54502bSMichal Simek /* Save away the syscall number. */ 332*6e83557cSMichal Simek swi r12, r1, PT_R0; 333ca54502bSMichal Simek tovirt(r1,r1) 334ca54502bSMichal Simek 335ca54502bSMichal Simek/* where the trap should return need -8 to adjust for rtsd r15, 8*/ 336ca54502bSMichal Simek/* Jump to the appropriate function for the system call number in r12 337ca54502bSMichal Simek * (r12 is not preserved), or return an error if r12 is not valid. The LP 338ca54502bSMichal Simek * register should point to the location where 339ca54502bSMichal Simek * the called function should return. [note that MAKE_SYS_CALL uses label 1] */ 34023575483SMichal Simek 34125f6e596SMichal Simek /* Step into virtual mode */ 34225f6e596SMichal Simek rtbd r0, 3f 34323575483SMichal Simek nop 34423575483SMichal Simek3: 345b1d70c62SMichal Simek lwi r11, CURRENT_TASK, TS_THREAD_INFO /* get thread info */ 34623575483SMichal Simek lwi r11, r11, TI_FLAGS /* get flags in thread info */ 34723575483SMichal Simek andi r11, r11, _TIF_WORK_SYSCALL_MASK 34823575483SMichal Simek beqi r11, 4f 34923575483SMichal Simek 35023575483SMichal Simek addik r3, r0, -ENOSYS 351*6e83557cSMichal Simek swi r3, r1, PT_R3 35223575483SMichal Simek brlid r15, do_syscall_trace_enter 353*6e83557cSMichal Simek addik r5, r1, PT_R0 35423575483SMichal Simek 35523575483SMichal Simek # do_syscall_trace_enter returns the new syscall nr. 35623575483SMichal Simek addk r12, r0, r3 357*6e83557cSMichal Simek lwi r5, r1, PT_R5; 358*6e83557cSMichal Simek lwi r6, r1, PT_R6; 359*6e83557cSMichal Simek lwi r7, r1, PT_R7; 360*6e83557cSMichal Simek lwi r8, r1, PT_R8; 361*6e83557cSMichal Simek lwi r9, r1, PT_R9; 362*6e83557cSMichal Simek lwi r10, r1, PT_R10; 36323575483SMichal Simek4: 36423575483SMichal Simek/* Jump to the appropriate function for the system call number in r12 36523575483SMichal Simek * (r12 is not preserved), or return an error if r12 is not valid. 36623575483SMichal Simek * The LP register should point to the location where the called function 36723575483SMichal Simek * should return. [note that MAKE_SYS_CALL uses label 1] */ 36823575483SMichal Simek /* See if the system call number is valid */ 369ca54502bSMichal Simek addi r11, r12, -__NR_syscalls; 37023575483SMichal Simek bgei r11,5f; 371ca54502bSMichal Simek /* Figure out which function to use for this system call. */ 372ca54502bSMichal Simek /* Note Microblaze barrel shift is optional, so don't rely on it */ 373ca54502bSMichal Simek add r12, r12, r12; /* convert num -> ptr */ 374ca54502bSMichal Simek add r12, r12, r12; 375ca54502bSMichal Simek 37611d51360SMichal Simek#ifdef DEBUG 377d8748e73SMichal Simek /* Trac syscalls and stored them to syscall_debug_table */ 378d8748e73SMichal Simek /* The first syscall location stores total syscall number */ 379d8748e73SMichal Simek lwi r3, r0, syscall_debug_table 380ca54502bSMichal Simek addi r3, r3, 1 381d8748e73SMichal Simek swi r3, r0, syscall_debug_table 382d8748e73SMichal Simek lwi r3, r12, syscall_debug_table 383d8748e73SMichal Simek addi r3, r3, 1 384d8748e73SMichal Simek swi r3, r12, syscall_debug_table 38511d51360SMichal Simek#endif 386ca54502bSMichal Simek 38723575483SMichal Simek # Find and jump into the syscall handler. 38823575483SMichal Simek lwi r12, r12, sys_call_table 38923575483SMichal Simek /* where the trap should return need -8 to adjust for rtsd r15, 8 */ 390b9ea77e2SMichal Simek addi r15, r0, ret_from_trap-8 39123575483SMichal Simek bra r12 39223575483SMichal Simek 393ca54502bSMichal Simek /* The syscall number is invalid, return an error. */ 39423575483SMichal Simek5: 395ca54502bSMichal Simek rtsd r15, 8; /* looks like a normal subroutine return */ 3969814cc11SMichal Simek addi r3, r0, -ENOSYS; 397ca54502bSMichal Simek 39823575483SMichal Simek/* Entry point used to return from a syscall/trap */ 399ca54502bSMichal Simek/* We re-enable BIP bit before state restore */ 400ca54502bSMichal SimekC_ENTRY(ret_from_trap): 401*6e83557cSMichal Simek swi r3, r1, PT_R3 402*6e83557cSMichal Simek swi r4, r1, PT_R4 403b1d70c62SMichal Simek 404*6e83557cSMichal Simek lwi r11, r1, PT_MODE; 4059da63458SMichal Simek/* See if returning to kernel mode, if so, skip resched &c. */ 4069da63458SMichal Simek bnei r11, 2f; 407ca54502bSMichal Simek /* We're returning to user mode, so check for various conditions that 408ca54502bSMichal Simek * trigger rescheduling. */ 409b1d70c62SMichal Simek /* FIXME: Restructure all these flag checks. */ 410b1d70c62SMichal Simek lwi r11, CURRENT_TASK, TS_THREAD_INFO; /* get thread info */ 41123575483SMichal Simek lwi r11, r11, TI_FLAGS; /* get flags in thread info */ 41223575483SMichal Simek andi r11, r11, _TIF_WORK_SYSCALL_MASK 41323575483SMichal Simek beqi r11, 1f 41423575483SMichal Simek 41523575483SMichal Simek brlid r15, do_syscall_trace_leave 416*6e83557cSMichal Simek addik r5, r1, PT_R0 41723575483SMichal Simek1: 41823575483SMichal Simek /* We're returning to user mode, so check for various conditions that 41923575483SMichal Simek * trigger rescheduling. */ 420b1d70c62SMichal Simek /* get thread info from current task */ 421b1d70c62SMichal Simek lwi r11, CURRENT_TASK, TS_THREAD_INFO; 422ca54502bSMichal Simek lwi r11, r11, TI_FLAGS; /* get flags in thread info */ 423ca54502bSMichal Simek andi r11, r11, _TIF_NEED_RESCHED; 424ca54502bSMichal Simek beqi r11, 5f; 425ca54502bSMichal Simek 426ca54502bSMichal Simek bralid r15, schedule; /* Call scheduler */ 427ca54502bSMichal Simek nop; /* delay slot */ 428ca54502bSMichal Simek 429ca54502bSMichal Simek /* Maybe handle a signal */ 430b1d70c62SMichal Simek5: /* get thread info from current task*/ 431b1d70c62SMichal Simek lwi r11, CURRENT_TASK, TS_THREAD_INFO; 432ca54502bSMichal Simek lwi r11, r11, TI_FLAGS; /* get flags in thread info */ 433ca54502bSMichal Simek andi r11, r11, _TIF_SIGPENDING; 434ca54502bSMichal Simek beqi r11, 1f; /* Signals to handle, handle them */ 435ca54502bSMichal Simek 436*6e83557cSMichal Simek addik r5, r1, 0; /* Arg 1: struct pt_regs *regs */ 437ca54502bSMichal Simek addi r7, r0, 1; /* Arg 3: int in_syscall */ 438ca54502bSMichal Simek bralid r15, do_signal; /* Handle any signals */ 439841d6e8cSMichal Simek add r6, r0, r0; /* Arg 2: sigset_t *oldset */ 440b1d70c62SMichal Simek 441b1d70c62SMichal Simek/* Finally, return to user state. */ 44296014cc3SMichal Simek1: set_bip; /* Ints masked for state restore */ 4438633bebcSMichal Simek swi CURRENT_TASK, r0, PER_CPU(CURRENT_SAVE); /* save current */ 444ca54502bSMichal Simek VM_OFF; 445ca54502bSMichal Simek tophys(r1,r1); 446ca54502bSMichal Simek RESTORE_REGS; 447*6e83557cSMichal Simek addik r1, r1, PT_SIZE /* Clean up stack space. */ 448ca54502bSMichal Simek lwi r1, r1, PT_R1 - PT_SIZE;/* Restore user stack pointer. */ 4499da63458SMichal Simek bri 6f; 4509da63458SMichal Simek 4519da63458SMichal Simek/* Return to kernel state. */ 4529da63458SMichal Simek2: set_bip; /* Ints masked for state restore */ 4539da63458SMichal Simek VM_OFF; 4549da63458SMichal Simek tophys(r1,r1); 4559da63458SMichal Simek RESTORE_REGS; 456*6e83557cSMichal Simek addik r1, r1, PT_SIZE /* Clean up stack space. */ 4579da63458SMichal Simek tovirt(r1,r1); 4589da63458SMichal Simek6: 459ca54502bSMichal SimekTRAP_return: /* Make global symbol for debugging */ 460ca54502bSMichal Simek rtbd r14, 0; /* Instructions to return from an IRQ */ 461ca54502bSMichal Simek nop; 462ca54502bSMichal Simek 463ca54502bSMichal Simek 464ca54502bSMichal Simek/* These syscalls need access to the struct pt_regs on the stack, so we 465ca54502bSMichal Simek implement them in assembly (they're basically all wrappers anyway). */ 466ca54502bSMichal Simek 467ca54502bSMichal SimekC_ENTRY(sys_fork_wrapper): 468ca54502bSMichal Simek addi r5, r0, SIGCHLD /* Arg 0: flags */ 469*6e83557cSMichal Simek lwi r6, r1, PT_R1 /* Arg 1: child SP (use parent's) */ 470*6e83557cSMichal Simek addik r7, r1, 0 /* Arg 2: parent context */ 471ca54502bSMichal Simek add r8. r0, r0 /* Arg 3: (unused) */ 472ca54502bSMichal Simek add r9, r0, r0; /* Arg 4: (unused) */ 473ca54502bSMichal Simek brid do_fork /* Do real work (tail-call) */ 4749814cc11SMichal Simek add r10, r0, r0; /* Arg 5: (unused) */ 475ca54502bSMichal Simek 476ca54502bSMichal Simek/* This the initial entry point for a new child thread, with an appropriate 477ca54502bSMichal Simek stack in place that makes it look the the child is in the middle of an 478ca54502bSMichal Simek syscall. This function is actually `returned to' from switch_thread 479ca54502bSMichal Simek (copy_thread makes ret_from_fork the return address in each new thread's 480ca54502bSMichal Simek saved context). */ 481ca54502bSMichal SimekC_ENTRY(ret_from_fork): 482ca54502bSMichal Simek bralid r15, schedule_tail; /* ...which is schedule_tail's arg */ 483ca54502bSMichal Simek add r3, r5, r0; /* switch_thread returns the prev task */ 484ca54502bSMichal Simek /* ( in the delay slot ) */ 485ca54502bSMichal Simek brid ret_from_trap; /* Do normal trap return */ 4869814cc11SMichal Simek add r3, r0, r0; /* Child's fork call should return 0. */ 487ca54502bSMichal Simek 488e513588fSArnd BergmannC_ENTRY(sys_vfork): 489e513588fSArnd Bergmann brid microblaze_vfork /* Do real work (tail-call) */ 490*6e83557cSMichal Simek addik r5, r1, 0 491ca54502bSMichal Simek 492e513588fSArnd BergmannC_ENTRY(sys_clone): 493ca54502bSMichal Simek bnei r6, 1f; /* See if child SP arg (arg 1) is 0. */ 494*6e83557cSMichal Simek lwi r6, r1, PT_R1; /* If so, use paret's stack ptr */ 495*6e83557cSMichal Simek1: addik r7, r1, 0; /* Arg 2: parent context */ 496b9ea77e2SMichal Simek add r8, r0, r0; /* Arg 3: (unused) */ 497b9ea77e2SMichal Simek add r9, r0, r0; /* Arg 4: (unused) */ 498ca54502bSMichal Simek brid do_fork /* Do real work (tail-call) */ 4999814cc11SMichal Simek add r10, r0, r0; /* Arg 5: (unused) */ 500ca54502bSMichal Simek 501e513588fSArnd BergmannC_ENTRY(sys_execve): 502e513588fSArnd Bergmann brid microblaze_execve; /* Do real work (tail-call).*/ 503*6e83557cSMichal Simek addik r8, r1, 0; /* add user context as 4th arg */ 504ca54502bSMichal Simek 505ca54502bSMichal SimekC_ENTRY(sys_rt_sigreturn_wrapper): 506791d0a16SMichal Simek brid sys_rt_sigreturn /* Do real work */ 507*6e83557cSMichal Simek addik r5, r1, 0; /* add user context as 1st arg */ 508ca54502bSMichal Simek 509ca54502bSMichal Simek/* 510ca54502bSMichal Simek * HW EXCEPTION rutine start 511ca54502bSMichal Simek */ 512ca54502bSMichal SimekC_ENTRY(full_exception_trap): 513ca54502bSMichal Simek /* adjust exception address for privileged instruction 514ca54502bSMichal Simek * for finding where is it */ 515ca54502bSMichal Simek addik r17, r17, -4 516ca54502bSMichal Simek SAVE_STATE /* Save registers */ 51706a54604SMichal Simek /* PC, before IRQ/trap - this is one instruction above */ 518*6e83557cSMichal Simek swi r17, r1, PT_PC; 51906a54604SMichal Simek tovirt(r1,r1) 520ca54502bSMichal Simek /* FIXME this can be store directly in PT_ESR reg. 521ca54502bSMichal Simek * I tested it but there is a fault */ 522ca54502bSMichal Simek /* where the trap should return need -8 to adjust for rtsd r15, 8 */ 523b9ea77e2SMichal Simek addik r15, r0, ret_from_exc - 8 524ca54502bSMichal Simek mfs r6, resr 525ca54502bSMichal Simek mfs r7, rfsr; /* save FSR */ 526131e4e97SMichal Simek mts rfsr, r0; /* Clear sticky fsr */ 527c318d483SMichal Simek rted r0, full_exception 528*6e83557cSMichal Simek addik r5, r1, 0 /* parameter struct pt_regs * regs */ 529ca54502bSMichal Simek 530ca54502bSMichal Simek/* 531ca54502bSMichal Simek * Unaligned data trap. 532ca54502bSMichal Simek * 533ca54502bSMichal Simek * Unaligned data trap last on 4k page is handled here. 534ca54502bSMichal Simek * 535ca54502bSMichal Simek * Trap entered via exception, so EE bit is set, and interrupts 536ca54502bSMichal Simek * are masked. This is nice, means we don't have to CLI before state save 537ca54502bSMichal Simek * 538ca54502bSMichal Simek * The assembler routine is in "arch/microblaze/kernel/hw_exception_handler.S" 539ca54502bSMichal Simek */ 540ca54502bSMichal SimekC_ENTRY(unaligned_data_trap): 5418b110d15SMichal Simek /* MS: I have to save r11 value and then restore it because 5428b110d15SMichal Simek * set_bit, clear_eip, set_ee use r11 as temp register if MSR 5438b110d15SMichal Simek * instructions are not used. We don't need to do if MSR instructions 5448b110d15SMichal Simek * are used and they use r0 instead of r11. 5458b110d15SMichal Simek * I am using ENTRY_SP which should be primary used only for stack 5468b110d15SMichal Simek * pointer saving. */ 5478b110d15SMichal Simek swi r11, r0, TOPHYS(PER_CPU(ENTRY_SP)); 5488b110d15SMichal Simek set_bip; /* equalize initial state for all possible entries */ 5498b110d15SMichal Simek clear_eip; 5508b110d15SMichal Simek set_ee; 5518b110d15SMichal Simek lwi r11, r0, TOPHYS(PER_CPU(ENTRY_SP)); 552ca54502bSMichal Simek SAVE_STATE /* Save registers.*/ 55306a54604SMichal Simek /* PC, before IRQ/trap - this is one instruction above */ 554*6e83557cSMichal Simek swi r17, r1, PT_PC; 55506a54604SMichal Simek tovirt(r1,r1) 556ca54502bSMichal Simek /* where the trap should return need -8 to adjust for rtsd r15, 8 */ 557b9ea77e2SMichal Simek addik r15, r0, ret_from_exc-8 558ca54502bSMichal Simek mfs r3, resr /* ESR */ 559ca54502bSMichal Simek mfs r4, rear /* EAR */ 560c318d483SMichal Simek rtbd r0, _unaligned_data_exception 561*6e83557cSMichal Simek addik r7, r1, 0 /* parameter struct pt_regs * regs */ 562ca54502bSMichal Simek 563ca54502bSMichal Simek/* 564ca54502bSMichal Simek * Page fault traps. 565ca54502bSMichal Simek * 566ca54502bSMichal Simek * If the real exception handler (from hw_exception_handler.S) didn't find 567ca54502bSMichal Simek * the mapping for the process, then we're thrown here to handle such situation. 568ca54502bSMichal Simek * 569ca54502bSMichal Simek * Trap entered via exceptions, so EE bit is set, and interrupts 570ca54502bSMichal Simek * are masked. This is nice, means we don't have to CLI before state save 571ca54502bSMichal Simek * 572ca54502bSMichal Simek * Build a standard exception frame for TLB Access errors. All TLB exceptions 573ca54502bSMichal Simek * will bail out to this point if they can't resolve the lightweight TLB fault. 574ca54502bSMichal Simek * 575ca54502bSMichal Simek * The C function called is in "arch/microblaze/mm/fault.c", declared as: 576ca54502bSMichal Simek * void do_page_fault(struct pt_regs *regs, 577ca54502bSMichal Simek * unsigned long address, 578ca54502bSMichal Simek * unsigned long error_code) 579ca54502bSMichal Simek */ 580ca54502bSMichal Simek/* data and intruction trap - which is choose is resolved int fault.c */ 581ca54502bSMichal SimekC_ENTRY(page_fault_data_trap): 582ca54502bSMichal Simek SAVE_STATE /* Save registers.*/ 58306a54604SMichal Simek /* PC, before IRQ/trap - this is one instruction above */ 584*6e83557cSMichal Simek swi r17, r1, PT_PC; 58506a54604SMichal Simek tovirt(r1,r1) 586ca54502bSMichal Simek /* where the trap should return need -8 to adjust for rtsd r15, 8 */ 587b9ea77e2SMichal Simek addik r15, r0, ret_from_exc-8 588ca54502bSMichal Simek mfs r6, rear /* parameter unsigned long address */ 589ca54502bSMichal Simek mfs r7, resr /* parameter unsigned long error_code */ 590c318d483SMichal Simek rted r0, do_page_fault 591*6e83557cSMichal Simek addik r5, r1, 0 /* parameter struct pt_regs * regs */ 592ca54502bSMichal Simek 593ca54502bSMichal SimekC_ENTRY(page_fault_instr_trap): 594ca54502bSMichal Simek SAVE_STATE /* Save registers.*/ 59506a54604SMichal Simek /* PC, before IRQ/trap - this is one instruction above */ 596*6e83557cSMichal Simek swi r17, r1, PT_PC; 59706a54604SMichal Simek tovirt(r1,r1) 598ca54502bSMichal Simek /* where the trap should return need -8 to adjust for rtsd r15, 8 */ 599b9ea77e2SMichal Simek addik r15, r0, ret_from_exc-8 600ca54502bSMichal Simek mfs r6, rear /* parameter unsigned long address */ 601ca54502bSMichal Simek ori r7, r0, 0 /* parameter unsigned long error_code */ 6029814cc11SMichal Simek rted r0, do_page_fault 603*6e83557cSMichal Simek addik r5, r1, 0 /* parameter struct pt_regs * regs */ 604ca54502bSMichal Simek 605ca54502bSMichal Simek/* Entry point used to return from an exception. */ 606ca54502bSMichal SimekC_ENTRY(ret_from_exc): 607*6e83557cSMichal Simek lwi r11, r1, PT_MODE; 608ca54502bSMichal Simek bnei r11, 2f; /* See if returning to kernel mode, */ 609ca54502bSMichal Simek /* ... if so, skip resched &c. */ 610ca54502bSMichal Simek 611ca54502bSMichal Simek /* We're returning to user mode, so check for various conditions that 612ca54502bSMichal Simek trigger rescheduling. */ 613b1d70c62SMichal Simek lwi r11, CURRENT_TASK, TS_THREAD_INFO; /* get thread info */ 614ca54502bSMichal Simek lwi r11, r11, TI_FLAGS; /* get flags in thread info */ 615ca54502bSMichal Simek andi r11, r11, _TIF_NEED_RESCHED; 616ca54502bSMichal Simek beqi r11, 5f; 617ca54502bSMichal Simek 618ca54502bSMichal Simek/* Call the scheduler before returning from a syscall/trap. */ 619ca54502bSMichal Simek bralid r15, schedule; /* Call scheduler */ 620ca54502bSMichal Simek nop; /* delay slot */ 621ca54502bSMichal Simek 622ca54502bSMichal Simek /* Maybe handle a signal */ 623b1d70c62SMichal Simek5: lwi r11, CURRENT_TASK, TS_THREAD_INFO; /* get thread info */ 624ca54502bSMichal Simek lwi r11, r11, TI_FLAGS; /* get flags in thread info */ 625ca54502bSMichal Simek andi r11, r11, _TIF_SIGPENDING; 626ca54502bSMichal Simek beqi r11, 1f; /* Signals to handle, handle them */ 627ca54502bSMichal Simek 628ca54502bSMichal Simek /* 629ca54502bSMichal Simek * Handle a signal return; Pending signals should be in r18. 630ca54502bSMichal Simek * 631ca54502bSMichal Simek * Not all registers are saved by the normal trap/interrupt entry 632ca54502bSMichal Simek * points (for instance, call-saved registers (because the normal 633ca54502bSMichal Simek * C-compiler calling sequence in the kernel makes sure they're 634ca54502bSMichal Simek * preserved), and call-clobbered registers in the case of 635ca54502bSMichal Simek * traps), but signal handlers may want to examine or change the 636ca54502bSMichal Simek * complete register state. Here we save anything not saved by 637ca54502bSMichal Simek * the normal entry sequence, so that it may be safely restored 63836f60954SMichal Simek * (in a possibly modified form) after do_signal returns. */ 639*6e83557cSMichal Simek addik r5, r1, 0; /* Arg 1: struct pt_regs *regs */ 640ca54502bSMichal Simek addi r7, r0, 0; /* Arg 3: int in_syscall */ 641ca54502bSMichal Simek bralid r15, do_signal; /* Handle any signals */ 642841d6e8cSMichal Simek add r6, r0, r0; /* Arg 2: sigset_t *oldset */ 643ca54502bSMichal Simek 644ca54502bSMichal Simek/* Finally, return to user state. */ 64596014cc3SMichal Simek1: set_bip; /* Ints masked for state restore */ 6468633bebcSMichal Simek swi CURRENT_TASK, r0, PER_CPU(CURRENT_SAVE); /* save current */ 647ca54502bSMichal Simek VM_OFF; 648ca54502bSMichal Simek tophys(r1,r1); 649ca54502bSMichal Simek 650ca54502bSMichal Simek RESTORE_REGS; 651*6e83557cSMichal Simek addik r1, r1, PT_SIZE /* Clean up stack space. */ 652ca54502bSMichal Simek 653ca54502bSMichal Simek lwi r1, r1, PT_R1 - PT_SIZE; /* Restore user stack pointer. */ 654ca54502bSMichal Simek bri 6f; 655ca54502bSMichal Simek/* Return to kernel state. */ 65696014cc3SMichal Simek2: set_bip; /* Ints masked for state restore */ 65796014cc3SMichal Simek VM_OFF; 658ca54502bSMichal Simek tophys(r1,r1); 659ca54502bSMichal Simek RESTORE_REGS; 660*6e83557cSMichal Simek addik r1, r1, PT_SIZE /* Clean up stack space. */ 661ca54502bSMichal Simek 662ca54502bSMichal Simek tovirt(r1,r1); 663ca54502bSMichal Simek6: 664ca54502bSMichal SimekEXC_return: /* Make global symbol for debugging */ 665ca54502bSMichal Simek rtbd r14, 0; /* Instructions to return from an IRQ */ 666ca54502bSMichal Simek nop; 667ca54502bSMichal Simek 668ca54502bSMichal Simek/* 669ca54502bSMichal Simek * HW EXCEPTION rutine end 670ca54502bSMichal Simek */ 671ca54502bSMichal Simek 672ca54502bSMichal Simek/* 673ca54502bSMichal Simek * Hardware maskable interrupts. 674ca54502bSMichal Simek * 675ca54502bSMichal Simek * The stack-pointer (r1) should have already been saved to the memory 676ca54502bSMichal Simek * location PER_CPU(ENTRY_SP). 677ca54502bSMichal Simek */ 678ca54502bSMichal SimekC_ENTRY(_interrupt): 679ca54502bSMichal Simek/* MS: we are in physical address */ 680ca54502bSMichal Simek/* Save registers, switch to proper stack, convert SP to virtual.*/ 681ca54502bSMichal Simek swi r1, r0, TOPHYS(PER_CPU(ENTRY_SP)) 682ca54502bSMichal Simek /* MS: See if already in kernel mode. */ 683653e447eSMichal Simek mfs r1, rmsr 6845c0d72b1SMichal Simek nop 685653e447eSMichal Simek andi r1, r1, MSR_UMS 686653e447eSMichal Simek bnei r1, 1f 687ca54502bSMichal Simek 688ca54502bSMichal Simek/* Kernel-mode state save. */ 689653e447eSMichal Simek lwi r1, r0, TOPHYS(PER_CPU(ENTRY_SP)) 690653e447eSMichal Simek tophys(r1,r1); /* MS: I have in r1 physical address where stack is */ 691ca54502bSMichal Simek /* save registers */ 692ca54502bSMichal Simek/* MS: Make room on the stack -> activation record */ 693*6e83557cSMichal Simek addik r1, r1, -PT_SIZE; 694ca54502bSMichal Simek SAVE_REGS 695ca54502bSMichal Simek brid 2f; 696*6e83557cSMichal Simek swi r1, r1, PT_MODE; /* 0 - user mode, 1 - kernel mode */ 697ca54502bSMichal Simek1: 698ca54502bSMichal Simek/* User-mode state save. */ 699ca54502bSMichal Simek /* MS: get the saved current */ 700ca54502bSMichal Simek lwi r1, r0, TOPHYS(PER_CPU(CURRENT_SAVE)); 701ca54502bSMichal Simek tophys(r1,r1); 702ca54502bSMichal Simek lwi r1, r1, TS_THREAD_INFO; 703ca54502bSMichal Simek addik r1, r1, THREAD_SIZE; 704ca54502bSMichal Simek tophys(r1,r1); 705ca54502bSMichal Simek /* save registers */ 706*6e83557cSMichal Simek addik r1, r1, -PT_SIZE; 707ca54502bSMichal Simek SAVE_REGS 708ca54502bSMichal Simek /* calculate mode */ 709*6e83557cSMichal Simek swi r0, r1, PT_MODE; 710ca54502bSMichal Simek lwi r11, r0, TOPHYS(PER_CPU(ENTRY_SP)); 711*6e83557cSMichal Simek swi r11, r1, PT_R1; 71280c5ff6bSMichal Simek clear_ums; 713ca54502bSMichal Simek2: 714b1d70c62SMichal Simek lwi CURRENT_TASK, r0, TOPHYS(PER_CPU(CURRENT_SAVE)); 715ca54502bSMichal Simek tovirt(r1,r1) 716b9ea77e2SMichal Simek addik r15, r0, irq_call; 71780c5ff6bSMichal Simekirq_call:rtbd r0, do_IRQ; 718*6e83557cSMichal Simek addik r5, r1, 0; 719ca54502bSMichal Simek 720ca54502bSMichal Simek/* MS: we are in virtual mode */ 721ca54502bSMichal Simekret_from_irq: 722*6e83557cSMichal Simek lwi r11, r1, PT_MODE; 723ca54502bSMichal Simek bnei r11, 2f; 724ca54502bSMichal Simek 725b1d70c62SMichal Simek lwi r11, CURRENT_TASK, TS_THREAD_INFO; 726ca54502bSMichal Simek lwi r11, r11, TI_FLAGS; /* MS: get flags from thread info */ 727ca54502bSMichal Simek andi r11, r11, _TIF_NEED_RESCHED; 728ca54502bSMichal Simek beqi r11, 5f 729ca54502bSMichal Simek bralid r15, schedule; 730ca54502bSMichal Simek nop; /* delay slot */ 731ca54502bSMichal Simek 732ca54502bSMichal Simek /* Maybe handle a signal */ 733b1d70c62SMichal Simek5: lwi r11, CURRENT_TASK, TS_THREAD_INFO; /* MS: get thread info */ 734ca54502bSMichal Simek lwi r11, r11, TI_FLAGS; /* get flags in thread info */ 735ca54502bSMichal Simek andi r11, r11, _TIF_SIGPENDING; 736ca54502bSMichal Simek beqid r11, no_intr_resched 737ca54502bSMichal Simek/* Handle a signal return; Pending signals should be in r18. */ 738ca54502bSMichal Simek addi r7, r0, 0; /* Arg 3: int in_syscall */ 739*6e83557cSMichal Simek addik r5, r1, 0; /* Arg 1: struct pt_regs *regs */ 740ca54502bSMichal Simek bralid r15, do_signal; /* Handle any signals */ 741ca54502bSMichal Simek add r6, r0, r0; /* Arg 2: sigset_t *oldset */ 742ca54502bSMichal Simek 743ca54502bSMichal Simek/* Finally, return to user state. */ 744ca54502bSMichal Simekno_intr_resched: 745ca54502bSMichal Simek /* Disable interrupts, we are now committed to the state restore */ 746ca54502bSMichal Simek disable_irq 7478633bebcSMichal Simek swi CURRENT_TASK, r0, PER_CPU(CURRENT_SAVE); 748ca54502bSMichal Simek VM_OFF; 749ca54502bSMichal Simek tophys(r1,r1); 750ca54502bSMichal Simek RESTORE_REGS 751*6e83557cSMichal Simek addik r1, r1, PT_SIZE /* MS: Clean up stack space. */ 752ca54502bSMichal Simek lwi r1, r1, PT_R1 - PT_SIZE; 753ca54502bSMichal Simek bri 6f; 754ca54502bSMichal Simek/* MS: Return to kernel state. */ 75577753790SMichal Simek2: 75677753790SMichal Simek#ifdef CONFIG_PREEMPT 757b1d70c62SMichal Simek lwi r11, CURRENT_TASK, TS_THREAD_INFO; 75877753790SMichal Simek /* MS: get preempt_count from thread info */ 75977753790SMichal Simek lwi r5, r11, TI_PREEMPT_COUNT; 76077753790SMichal Simek bgti r5, restore; 76177753790SMichal Simek 76277753790SMichal Simek lwi r5, r11, TI_FLAGS; /* get flags in thread info */ 76377753790SMichal Simek andi r5, r5, _TIF_NEED_RESCHED; 76477753790SMichal Simek beqi r5, restore /* if zero jump over */ 76577753790SMichal Simek 76677753790SMichal Simekpreempt: 76777753790SMichal Simek /* interrupts are off that's why I am calling preempt_chedule_irq */ 76877753790SMichal Simek bralid r15, preempt_schedule_irq 76977753790SMichal Simek nop 770b1d70c62SMichal Simek lwi r11, CURRENT_TASK, TS_THREAD_INFO; /* get thread info */ 77177753790SMichal Simek lwi r5, r11, TI_FLAGS; /* get flags in thread info */ 77277753790SMichal Simek andi r5, r5, _TIF_NEED_RESCHED; 77377753790SMichal Simek bnei r5, preempt /* if non zero jump to resched */ 77477753790SMichal Simekrestore: 77577753790SMichal Simek#endif 77677753790SMichal Simek VM_OFF /* MS: turn off MMU */ 777ca54502bSMichal Simek tophys(r1,r1) 778ca54502bSMichal Simek RESTORE_REGS 779*6e83557cSMichal Simek addik r1, r1, PT_SIZE /* MS: Clean up stack space. */ 780ca54502bSMichal Simek tovirt(r1,r1); 781ca54502bSMichal Simek6: 782ca54502bSMichal SimekIRQ_return: /* MS: Make global symbol for debugging */ 783ca54502bSMichal Simek rtid r14, 0 784ca54502bSMichal Simek nop 785ca54502bSMichal Simek 786ca54502bSMichal Simek/* 7872d5973cbSMichal Simek * Debug trap for KGDB. Enter to _debug_exception by brki r16, 0x18 7882d5973cbSMichal Simek * and call handling function with saved pt_regs 789ca54502bSMichal Simek */ 790ca54502bSMichal SimekC_ENTRY(_debug_exception): 791ca54502bSMichal Simek /* BIP bit is set on entry, no interrupts can occur */ 792ca54502bSMichal Simek swi r1, r0, TOPHYS(PER_CPU(ENTRY_SP)) 793ca54502bSMichal Simek 794653e447eSMichal Simek mfs r1, rmsr 7955c0d72b1SMichal Simek nop 796653e447eSMichal Simek andi r1, r1, MSR_UMS 797653e447eSMichal Simek bnei r1, 1f 7982d5973cbSMichal Simek/* MS: Kernel-mode state save - kgdb */ 799653e447eSMichal Simek lwi r1, r0, TOPHYS(PER_CPU(ENTRY_SP)); /* Reload kernel stack-ptr*/ 800ca54502bSMichal Simek 8012d5973cbSMichal Simek /* BIP bit is set on entry, no interrupts can occur */ 802*6e83557cSMichal Simek addik r1, r1, CONFIG_KERNEL_BASE_ADDR - CONFIG_KERNEL_START - PT_SIZE; 803ca54502bSMichal Simek SAVE_REGS; 8042d5973cbSMichal Simek /* save all regs to pt_reg structure */ 805*6e83557cSMichal Simek swi r0, r1, PT_R0; /* R0 must be saved too */ 806*6e83557cSMichal Simek swi r14, r1, PT_R14 /* rewrite saved R14 value */ 807*6e83557cSMichal Simek swi r16, r1, PT_PC; /* PC and r16 are the same */ 8082d5973cbSMichal Simek /* save special purpose registers to pt_regs */ 8092d5973cbSMichal Simek mfs r11, rear; 810*6e83557cSMichal Simek swi r11, r1, PT_EAR; 8112d5973cbSMichal Simek mfs r11, resr; 812*6e83557cSMichal Simek swi r11, r1, PT_ESR; 8132d5973cbSMichal Simek mfs r11, rfsr; 814*6e83557cSMichal Simek swi r11, r1, PT_FSR; 815ca54502bSMichal Simek 8162d5973cbSMichal Simek /* stack pointer is in physical address at it is decrease 817*6e83557cSMichal Simek * by PT_SIZE but we need to get correct R1 value */ 818*6e83557cSMichal Simek addik r11, r1, CONFIG_KERNEL_START - CONFIG_KERNEL_BASE_ADDR + PT_SIZE; 819*6e83557cSMichal Simek swi r11, r1, PT_R1 8202d5973cbSMichal Simek /* MS: r31 - current pointer isn't changed */ 8212d5973cbSMichal Simek tovirt(r1,r1) 8222d5973cbSMichal Simek#ifdef CONFIG_KGDB 823*6e83557cSMichal Simek addi r5, r1, 0 /* pass pt_reg address as the first arg */ 8242d5973cbSMichal Simek la r15, r0, dbtrap_call; /* return address */ 8252d5973cbSMichal Simek rtbd r0, microblaze_kgdb_break 8262d5973cbSMichal Simek nop; 8272d5973cbSMichal Simek#endif 8282d5973cbSMichal Simek /* MS: Place handler for brki from kernel space if KGDB is OFF. 8292d5973cbSMichal Simek * It is very unlikely that another brki instruction is called. */ 8302d5973cbSMichal Simek bri 0 8312d5973cbSMichal Simek 8322d5973cbSMichal Simek/* MS: User-mode state save - gdb */ 8332d5973cbSMichal Simek1: lwi r1, r0, TOPHYS(PER_CPU(CURRENT_SAVE)); /* get saved current */ 834ca54502bSMichal Simek tophys(r1,r1); 835ca54502bSMichal Simek lwi r1, r1, TS_THREAD_INFO; /* get the thread info */ 836ca54502bSMichal Simek addik r1, r1, THREAD_SIZE; /* calculate kernel stack pointer */ 837ca54502bSMichal Simek tophys(r1,r1); 838ca54502bSMichal Simek 839*6e83557cSMichal Simek addik r1, r1, -PT_SIZE; /* Make room on the stack. */ 840ca54502bSMichal Simek SAVE_REGS; 841*6e83557cSMichal Simek swi r16, r1, PT_PC; /* Save LP */ 842*6e83557cSMichal Simek swi r0, r1, PT_MODE; /* Was in user-mode. */ 843ca54502bSMichal Simek lwi r11, r0, TOPHYS(PER_CPU(ENTRY_SP)); 844*6e83557cSMichal Simek swi r11, r1, PT_R1; /* Store user SP. */ 8452d5973cbSMichal Simek lwi CURRENT_TASK, r0, TOPHYS(PER_CPU(CURRENT_SAVE)); 846ca54502bSMichal Simek tovirt(r1,r1) 84706b28640SMichal Simek set_vms; 848*6e83557cSMichal Simek addik r5, r1, 0; 849b9ea77e2SMichal Simek addik r15, r0, dbtrap_call; 8502d5973cbSMichal Simekdbtrap_call: /* Return point for kernel/user entry + 8 because of rtsd r15, 8 */ 851751f1605SMichal Simek rtbd r0, sw_exception 852751f1605SMichal Simek nop 853ca54502bSMichal Simek 8542d5973cbSMichal Simek /* MS: The first instruction for the second part of the gdb/kgdb */ 855ca54502bSMichal Simek set_bip; /* Ints masked for state restore */ 856*6e83557cSMichal Simek lwi r11, r1, PT_MODE; 857ca54502bSMichal Simek bnei r11, 2f; 8582d5973cbSMichal Simek/* MS: Return to user space - gdb */ 859ca54502bSMichal Simek /* Get current task ptr into r11 */ 860b1d70c62SMichal Simek lwi r11, CURRENT_TASK, TS_THREAD_INFO; /* get thread info */ 861ca54502bSMichal Simek lwi r11, r11, TI_FLAGS; /* get flags in thread info */ 862ca54502bSMichal Simek andi r11, r11, _TIF_NEED_RESCHED; 863ca54502bSMichal Simek beqi r11, 5f; 864ca54502bSMichal Simek 865ca54502bSMichal Simek /* Call the scheduler before returning from a syscall/trap. */ 866ca54502bSMichal Simek bralid r15, schedule; /* Call scheduler */ 867ca54502bSMichal Simek nop; /* delay slot */ 868ca54502bSMichal Simek 869ca54502bSMichal Simek /* Maybe handle a signal */ 870b1d70c62SMichal Simek5: lwi r11, CURRENT_TASK, TS_THREAD_INFO; /* get thread info */ 871ca54502bSMichal Simek lwi r11, r11, TI_FLAGS; /* get flags in thread info */ 872ca54502bSMichal Simek andi r11, r11, _TIF_SIGPENDING; 873ca54502bSMichal Simek beqi r11, 1f; /* Signals to handle, handle them */ 874ca54502bSMichal Simek 875*6e83557cSMichal Simek addik r5, r1, 0; /* Arg 1: struct pt_regs *regs */ 876ca54502bSMichal Simek addi r7, r0, 0; /* Arg 3: int in_syscall */ 877ca54502bSMichal Simek bralid r15, do_signal; /* Handle any signals */ 878841d6e8cSMichal Simek add r6, r0, r0; /* Arg 2: sigset_t *oldset */ 879ca54502bSMichal Simek 880ca54502bSMichal Simek/* Finally, return to user state. */ 8812d5973cbSMichal Simek1: swi CURRENT_TASK, r0, PER_CPU(CURRENT_SAVE); /* save current */ 882ca54502bSMichal Simek VM_OFF; 883ca54502bSMichal Simek tophys(r1,r1); 8842d5973cbSMichal Simek /* MS: Restore all regs */ 885ca54502bSMichal Simek RESTORE_REGS 886*6e83557cSMichal Simek addik r1, r1, PT_SIZE /* Clean up stack space */ 8872d5973cbSMichal Simek lwi r1, r1, PT_R1 - PT_SIZE; /* Restore user stack pointer */ 8882d5973cbSMichal SimekDBTRAP_return_user: /* MS: Make global symbol for debugging */ 8892d5973cbSMichal Simek rtbd r16, 0; /* MS: Instructions to return from a debug trap */ 8902d5973cbSMichal Simek nop; 891ca54502bSMichal Simek 8922d5973cbSMichal Simek/* MS: Return to kernel state - kgdb */ 893ca54502bSMichal Simek2: VM_OFF; 894ca54502bSMichal Simek tophys(r1,r1); 8952d5973cbSMichal Simek /* MS: Restore all regs */ 896ca54502bSMichal Simek RESTORE_REGS 897*6e83557cSMichal Simek lwi r14, r1, PT_R14; 898*6e83557cSMichal Simek lwi r16, r1, PT_PC; 899*6e83557cSMichal Simek addik r1, r1, PT_SIZE; /* MS: Clean up stack space */ 900ca54502bSMichal Simek tovirt(r1,r1); 9012d5973cbSMichal SimekDBTRAP_return_kernel: /* MS: Make global symbol for debugging */ 9022d5973cbSMichal Simek rtbd r16, 0; /* MS: Instructions to return from a debug trap */ 903ca54502bSMichal Simek nop; 904ca54502bSMichal Simek 905ca54502bSMichal Simek 906ca54502bSMichal SimekENTRY(_switch_to) 907ca54502bSMichal Simek /* prepare return value */ 908b1d70c62SMichal Simek addk r3, r0, CURRENT_TASK 909ca54502bSMichal Simek 910ca54502bSMichal Simek /* save registers in cpu_context */ 911ca54502bSMichal Simek /* use r11 and r12, volatile registers, as temp register */ 912ca54502bSMichal Simek /* give start of cpu_context for previous process */ 913ca54502bSMichal Simek addik r11, r5, TI_CPU_CONTEXT 914ca54502bSMichal Simek swi r1, r11, CC_R1 915ca54502bSMichal Simek swi r2, r11, CC_R2 916ca54502bSMichal Simek /* skip volatile registers. 917ca54502bSMichal Simek * they are saved on stack when we jumped to _switch_to() */ 918ca54502bSMichal Simek /* dedicated registers */ 919ca54502bSMichal Simek swi r13, r11, CC_R13 920ca54502bSMichal Simek swi r14, r11, CC_R14 921ca54502bSMichal Simek swi r15, r11, CC_R15 922ca54502bSMichal Simek swi r16, r11, CC_R16 923ca54502bSMichal Simek swi r17, r11, CC_R17 924ca54502bSMichal Simek swi r18, r11, CC_R18 925ca54502bSMichal Simek /* save non-volatile registers */ 926ca54502bSMichal Simek swi r19, r11, CC_R19 927ca54502bSMichal Simek swi r20, r11, CC_R20 928ca54502bSMichal Simek swi r21, r11, CC_R21 929ca54502bSMichal Simek swi r22, r11, CC_R22 930ca54502bSMichal Simek swi r23, r11, CC_R23 931ca54502bSMichal Simek swi r24, r11, CC_R24 932ca54502bSMichal Simek swi r25, r11, CC_R25 933ca54502bSMichal Simek swi r26, r11, CC_R26 934ca54502bSMichal Simek swi r27, r11, CC_R27 935ca54502bSMichal Simek swi r28, r11, CC_R28 936ca54502bSMichal Simek swi r29, r11, CC_R29 937ca54502bSMichal Simek swi r30, r11, CC_R30 938ca54502bSMichal Simek /* special purpose registers */ 939ca54502bSMichal Simek mfs r12, rmsr 940ca54502bSMichal Simek swi r12, r11, CC_MSR 941ca54502bSMichal Simek mfs r12, rear 942ca54502bSMichal Simek swi r12, r11, CC_EAR 943ca54502bSMichal Simek mfs r12, resr 944ca54502bSMichal Simek swi r12, r11, CC_ESR 945ca54502bSMichal Simek mfs r12, rfsr 946ca54502bSMichal Simek swi r12, r11, CC_FSR 947ca54502bSMichal Simek 948b1d70c62SMichal Simek /* update r31, the current-give me pointer to task which will be next */ 949b1d70c62SMichal Simek lwi CURRENT_TASK, r6, TI_TASK 950ca54502bSMichal Simek /* stored it to current_save too */ 951b1d70c62SMichal Simek swi CURRENT_TASK, r0, PER_CPU(CURRENT_SAVE) 952ca54502bSMichal Simek 953ca54502bSMichal Simek /* get new process' cpu context and restore */ 954ca54502bSMichal Simek /* give me start where start context of next task */ 955ca54502bSMichal Simek addik r11, r6, TI_CPU_CONTEXT 956ca54502bSMichal Simek 957ca54502bSMichal Simek /* non-volatile registers */ 958ca54502bSMichal Simek lwi r30, r11, CC_R30 959ca54502bSMichal Simek lwi r29, r11, CC_R29 960ca54502bSMichal Simek lwi r28, r11, CC_R28 961ca54502bSMichal Simek lwi r27, r11, CC_R27 962ca54502bSMichal Simek lwi r26, r11, CC_R26 963ca54502bSMichal Simek lwi r25, r11, CC_R25 964ca54502bSMichal Simek lwi r24, r11, CC_R24 965ca54502bSMichal Simek lwi r23, r11, CC_R23 966ca54502bSMichal Simek lwi r22, r11, CC_R22 967ca54502bSMichal Simek lwi r21, r11, CC_R21 968ca54502bSMichal Simek lwi r20, r11, CC_R20 969ca54502bSMichal Simek lwi r19, r11, CC_R19 970ca54502bSMichal Simek /* dedicated registers */ 971ca54502bSMichal Simek lwi r18, r11, CC_R18 972ca54502bSMichal Simek lwi r17, r11, CC_R17 973ca54502bSMichal Simek lwi r16, r11, CC_R16 974ca54502bSMichal Simek lwi r15, r11, CC_R15 975ca54502bSMichal Simek lwi r14, r11, CC_R14 976ca54502bSMichal Simek lwi r13, r11, CC_R13 977ca54502bSMichal Simek /* skip volatile registers */ 978ca54502bSMichal Simek lwi r2, r11, CC_R2 979ca54502bSMichal Simek lwi r1, r11, CC_R1 980ca54502bSMichal Simek 981ca54502bSMichal Simek /* special purpose registers */ 982ca54502bSMichal Simek lwi r12, r11, CC_FSR 983ca54502bSMichal Simek mts rfsr, r12 984ca54502bSMichal Simek lwi r12, r11, CC_MSR 985ca54502bSMichal Simek mts rmsr, r12 986ca54502bSMichal Simek 987ca54502bSMichal Simek rtsd r15, 8 988ca54502bSMichal Simek nop 989ca54502bSMichal Simek 990ca54502bSMichal SimekENTRY(_reset) 991ca54502bSMichal Simek brai 0x70; /* Jump back to FS-boot */ 992ca54502bSMichal Simek 993ca54502bSMichal Simek /* These are compiled and loaded into high memory, then 994ca54502bSMichal Simek * copied into place in mach_early_setup */ 995ca54502bSMichal Simek .section .init.ivt, "ax" 996ca54502bSMichal Simek .org 0x0 997ca54502bSMichal Simek /* this is very important - here is the reset vector */ 998ca54502bSMichal Simek /* in current MMU branch you don't care what is here - it is 999ca54502bSMichal Simek * used from bootloader site - but this is correct for FS-BOOT */ 1000ca54502bSMichal Simek brai 0x70 1001ca54502bSMichal Simek nop 1002ca54502bSMichal Simek brai TOPHYS(_user_exception); /* syscall handler */ 1003ca54502bSMichal Simek brai TOPHYS(_interrupt); /* Interrupt handler */ 1004ca54502bSMichal Simek brai TOPHYS(_debug_exception); /* debug trap handler */ 1005751f1605SMichal Simek brai TOPHYS(_hw_exception_handler); /* HW exception handler */ 1006ca54502bSMichal Simek 1007ca54502bSMichal Simek.section .rodata,"a" 1008ca54502bSMichal Simek#include "syscall_table.S" 1009ca54502bSMichal Simek 1010ca54502bSMichal Simeksyscall_table_size=(.-sys_call_table) 1011ca54502bSMichal Simek 1012ce3266c0SSteven J. Magnanitype_SYSCALL: 1013ce3266c0SSteven J. Magnani .ascii "SYSCALL\0" 1014ce3266c0SSteven J. Magnanitype_IRQ: 1015ce3266c0SSteven J. Magnani .ascii "IRQ\0" 1016ce3266c0SSteven J. Magnanitype_IRQ_PREEMPT: 1017ce3266c0SSteven J. Magnani .ascii "IRQ (PREEMPTED)\0" 1018ce3266c0SSteven J. Magnanitype_SYSCALL_PREEMPT: 1019ce3266c0SSteven J. Magnani .ascii " SYSCALL (PREEMPTED)\0" 1020ce3266c0SSteven J. Magnani 1021ce3266c0SSteven J. Magnani /* 1022ce3266c0SSteven J. Magnani * Trap decoding for stack unwinder 1023ce3266c0SSteven J. Magnani * Tuples are (start addr, end addr, string) 1024ce3266c0SSteven J. Magnani * If return address lies on [start addr, end addr], 1025ce3266c0SSteven J. Magnani * unwinder displays 'string' 1026ce3266c0SSteven J. Magnani */ 1027ce3266c0SSteven J. Magnani 1028ce3266c0SSteven J. Magnani .align 4 1029ce3266c0SSteven J. Magnani.global microblaze_trap_handlers 1030ce3266c0SSteven J. Magnanimicroblaze_trap_handlers: 1031ce3266c0SSteven J. Magnani /* Exact matches come first */ 1032ce3266c0SSteven J. Magnani .word ret_from_trap; .word ret_from_trap ; .word type_SYSCALL 1033ce3266c0SSteven J. Magnani .word ret_from_irq ; .word ret_from_irq ; .word type_IRQ 1034ce3266c0SSteven J. Magnani /* Fuzzy matches go here */ 1035ce3266c0SSteven J. Magnani .word ret_from_irq ; .word no_intr_resched ; .word type_IRQ_PREEMPT 1036ce3266c0SSteven J. Magnani .word ret_from_trap; .word TRAP_return ; .word type_SYSCALL_PREEMPT 1037ce3266c0SSteven J. Magnani /* End of table */ 1038ce3266c0SSteven J. Magnani .word 0 ; .word 0 ; .word 0 1039