/*1* Linux/PA-RISC Project (http://www.parisc-linux.org/)2*3* System call entry code Copyright (c) Matthew Wilcox 1999 <[email protected]>4* Licensed under the GNU GPL.5* thanks to Philipp Rumpf, Mike Shaver and various others6* sorry about the wall, puffin..7*/89#include <asm/asm-offsets.h>10#include <asm/unistd.h>11#include <asm/errno.h>12#include <asm/page.h>13#include <asm/psw.h>14#include <asm/thread_info.h>15#include <asm/assembly.h>16#include <asm/processor.h>1718#include <linux/linkage.h>1920/* We fill the empty parts of the gateway page with21* something that will kill the kernel or a22* userspace application.23*/24#define KILL_INSN break 0,02526.level LEVEL2728.text2930.import syscall_exit,code31.import syscall_exit_rfi,code3233/* Linux gateway page is aliased to virtual page 0 in the kernel34* address space. Since it is a gateway page it cannot be35* dereferenced, so null pointers will still fault. We start36* the actual entry point at 0x100. We put break instructions37* at the beginning of the page to trap null indirect function38* pointers.39*/4041.align PAGE_SIZE42ENTRY(linux_gateway_page)4344/* ADDRESS 0x00 to 0xb0 = 176 bytes / 4 bytes per insn = 44 insns */45.rept 4446KILL_INSN47.endr4849/* ADDRESS 0xb0 to 0xb8, lws uses two insns for entry */50/* Light-weight-syscall entry must always be located at 0xb0 */51/* WARNING: Keep this number updated with table size changes */52#define __NR_lws_entries (2)5354lws_entry:55gate lws_start, %r0 /* increase privilege */56depi 3, 31, 2, %r31 /* Ensure we return into user mode. */5758/* Fill from 0xb8 to 0xe0 */59.rept 1060KILL_INSN61.endr6263/* This function MUST be located at 0xe0 for glibc's threading64mechanism to work. DO NOT MOVE THIS CODE EVER! */65set_thread_pointer:66gate .+8, %r0 /* increase privilege */67depi 3, 31, 2, %r31 /* Ensure we return into user mode. */68be 0(%sr7,%r31) /* return to user space */69mtctl %r26, %cr27 /* move arg0 to the control register */7071/* Increase the chance of trapping if random jumps occur to this72address, fill from 0xf0 to 0x100 */73.rept 474KILL_INSN75.endr7677/* This address must remain fixed at 0x100 for glibc's syscalls to work */78.align 25679linux_gateway_entry:80gate .+8, %r0 /* become privileged */81mtsp %r0,%sr4 /* get kernel space into sr4 */82mtsp %r0,%sr5 /* get kernel space into sr5 */83mtsp %r0,%sr6 /* get kernel space into sr6 */84mfsp %sr7,%r1 /* save user sr7 */85mtsp %r1,%sr3 /* and store it in sr3 */8687#ifdef CONFIG_64BIT88/* for now we can *always* set the W bit on entry to the syscall89* since we don't support wide userland processes. We could90* also save the current SM other than in r0 and restore it on91* exit from the syscall, and also use that value to know92* whether to do narrow or wide syscalls. -PB93*/94ssm PSW_SM_W, %r195extrd,u %r1,PSW_W_BIT,1,%r196/* sp must be aligned on 4, so deposit the W bit setting into97* the bottom of sp temporarily */98or,ev %r1,%r30,%r3099b,n 1f100/* The top halves of argument registers must be cleared on syscall101* entry from narrow executable.102*/103depdi 0, 31, 32, %r26104depdi 0, 31, 32, %r25105depdi 0, 31, 32, %r24106depdi 0, 31, 32, %r23107depdi 0, 31, 32, %r22108depdi 0, 31, 32, %r211091:110#endif111mfctl %cr30,%r1112xor %r1,%r30,%r30 /* ye olde xor trick */113xor %r1,%r30,%r1114xor %r1,%r30,%r30115116ldo THREAD_SZ_ALGN+FRAME_SIZE(%r30),%r30 /* set up kernel stack */117118/* N.B.: It is critical that we don't set sr7 to 0 until r30119* contains a valid kernel stack pointer. It is also120* critical that we don't start using the kernel stack121* until after sr7 has been set to 0.122*/123124mtsp %r0,%sr7 /* get kernel space into sr7 */125STREGM %r1,FRAME_SIZE(%r30) /* save r1 (usp) here for now */126mfctl %cr30,%r1 /* get task ptr in %r1 */127LDREG TI_TASK(%r1),%r1128129/* Save some registers for sigcontext and potential task130switch (see entry.S for the details of which ones are131saved/restored). TASK_PT_PSW is zeroed so we can see whether132a process is on a syscall or not. For an interrupt the real133PSW value is stored. This is needed for gdb and sys_ptrace. */134STREG %r0, TASK_PT_PSW(%r1)135STREG %r2, TASK_PT_GR2(%r1) /* preserve rp */136STREG %r19, TASK_PT_GR19(%r1)137138LDREGM -FRAME_SIZE(%r30), %r2 /* get users sp back */139#ifdef CONFIG_64BIT140extrd,u %r2,63,1,%r19 /* W hidden in bottom bit */141#if 0142xor %r19,%r2,%r2 /* clear bottom bit */143depd,z %r19,1,1,%r19144std %r19,TASK_PT_PSW(%r1)145#endif146#endif147STREG %r2, TASK_PT_GR30(%r1) /* ... and save it */148149STREG %r20, TASK_PT_GR20(%r1) /* Syscall number */150STREG %r21, TASK_PT_GR21(%r1)151STREG %r22, TASK_PT_GR22(%r1)152STREG %r23, TASK_PT_GR23(%r1) /* 4th argument */153STREG %r24, TASK_PT_GR24(%r1) /* 3rd argument */154STREG %r25, TASK_PT_GR25(%r1) /* 2nd argument */155STREG %r26, TASK_PT_GR26(%r1) /* 1st argument */156STREG %r27, TASK_PT_GR27(%r1) /* user dp */157STREG %r28, TASK_PT_GR28(%r1) /* return value 0 */158STREG %r28, TASK_PT_ORIG_R28(%r1) /* return value 0 (saved for signals) */159STREG %r29, TASK_PT_GR29(%r1) /* return value 1 */160STREG %r31, TASK_PT_GR31(%r1) /* preserve syscall return ptr */161162ldo TASK_PT_FR0(%r1), %r27 /* save fpregs from the kernel */163save_fp %r27 /* or potential task switch */164165mfctl %cr11, %r27 /* i.e. SAR */166STREG %r27, TASK_PT_SAR(%r1)167168loadgp169170#ifdef CONFIG_64BIT171ldo -16(%r30),%r29 /* Reference param save area */172copy %r19,%r2 /* W bit back to r2 */173#else174/* no need to save these on stack in wide mode because the first 8175* args are passed in registers */176stw %r22, -52(%r30) /* 5th argument */177stw %r21, -56(%r30) /* 6th argument */178#endif179180/* Are we being ptraced? */181mfctl %cr30, %r1182LDREG TI_TASK(%r1),%r1183ldw TASK_PTRACE(%r1), %r1184bb,<,n %r1,31,.Ltracesys185186/* Note! We cannot use the syscall table that is mapped187nearby since the gateway page is mapped execute-only. */188189#ifdef CONFIG_64BIT190ldil L%sys_call_table, %r1191or,= %r2,%r2,%r2192addil L%(sys_call_table64-sys_call_table), %r1193ldo R%sys_call_table(%r1), %r19194or,= %r2,%r2,%r2195ldo R%sys_call_table64(%r1), %r19196#else197ldil L%sys_call_table, %r1198ldo R%sys_call_table(%r1), %r19199#endif200comiclr,>> __NR_Linux_syscalls, %r20, %r0201b,n .Lsyscall_nosys202203LDREGX %r20(%r19), %r19204205/* If this is a sys_rt_sigreturn call, and the signal was received206* when not in_syscall, then we want to return via syscall_exit_rfi,207* not syscall_exit. Signal no. in r20, in_syscall in r25 (see208* trampoline code in signal.c).209*/210ldi __NR_rt_sigreturn,%r2211comb,= %r2,%r20,.Lrt_sigreturn212.Lin_syscall:213ldil L%syscall_exit,%r2214be 0(%sr7,%r19)215ldo R%syscall_exit(%r2),%r2216.Lrt_sigreturn:217comib,<> 0,%r25,.Lin_syscall218ldil L%syscall_exit_rfi,%r2219be 0(%sr7,%r19)220ldo R%syscall_exit_rfi(%r2),%r2221222/* Note! Because we are not running where we were linked, any223calls to functions external to this file must be indirect. To224be safe, we apply the opposite rule to functions within this225file, with local labels given to them to ensure correctness. */226227.Lsyscall_nosys:228syscall_nosys:229ldil L%syscall_exit,%r1230be R%syscall_exit(%sr7,%r1)231ldo -ENOSYS(%r0),%r28 /* set errno */232233234/* Warning! This trace code is a virtual duplicate of the code above so be235* sure to maintain both! */236.Ltracesys:237tracesys:238/* Need to save more registers so the debugger can see where we239* are. This saves only the lower 8 bits of PSW, so that the C240* bit is still clear on syscalls, and the D bit is set if this241* full register save path has been executed. We check the D242* bit on syscall_return_rfi to determine which registers to243* restore. An interrupt results in a full PSW saved with the244* C bit set, a non-straced syscall entry results in C and D clear245* in the saved PSW.246*/247ldo -THREAD_SZ_ALGN-FRAME_SIZE(%r30),%r1 /* get task ptr */248LDREG TI_TASK(%r1), %r1249ssm 0,%r2250STREG %r2,TASK_PT_PSW(%r1) /* Lower 8 bits only!! */251mfsp %sr0,%r2252STREG %r2,TASK_PT_SR0(%r1)253mfsp %sr1,%r2254STREG %r2,TASK_PT_SR1(%r1)255mfsp %sr2,%r2256STREG %r2,TASK_PT_SR2(%r1)257mfsp %sr3,%r2258STREG %r2,TASK_PT_SR3(%r1)259STREG %r2,TASK_PT_SR4(%r1)260STREG %r2,TASK_PT_SR5(%r1)261STREG %r2,TASK_PT_SR6(%r1)262STREG %r2,TASK_PT_SR7(%r1)263STREG %r2,TASK_PT_IASQ0(%r1)264STREG %r2,TASK_PT_IASQ1(%r1)265LDREG TASK_PT_GR31(%r1),%r2266STREG %r2,TASK_PT_IAOQ0(%r1)267ldo 4(%r2),%r2268STREG %r2,TASK_PT_IAOQ1(%r1)269ldo TASK_REGS(%r1),%r2270/* reg_save %r2 */271STREG %r3,PT_GR3(%r2)272STREG %r4,PT_GR4(%r2)273STREG %r5,PT_GR5(%r2)274STREG %r6,PT_GR6(%r2)275STREG %r7,PT_GR7(%r2)276STREG %r8,PT_GR8(%r2)277STREG %r9,PT_GR9(%r2)278STREG %r10,PT_GR10(%r2)279STREG %r11,PT_GR11(%r2)280STREG %r12,PT_GR12(%r2)281STREG %r13,PT_GR13(%r2)282STREG %r14,PT_GR14(%r2)283STREG %r15,PT_GR15(%r2)284STREG %r16,PT_GR16(%r2)285STREG %r17,PT_GR17(%r2)286STREG %r18,PT_GR18(%r2)287/* Finished saving things for the debugger */288289copy %r2,%r26290ldil L%do_syscall_trace_enter,%r1291ldil L%tracesys_next,%r2292be R%do_syscall_trace_enter(%sr7,%r1)293ldo R%tracesys_next(%r2),%r2294295tracesys_next:296/* do_syscall_trace_enter either returned the syscallno, or -1L,297* so we skip restoring the PT_GR20 below, since we pulled it from298* task->thread.regs.gr[20] above.299*/300copy %ret0,%r20301ldil L%sys_call_table,%r1302ldo R%sys_call_table(%r1), %r19303304ldo -THREAD_SZ_ALGN-FRAME_SIZE(%r30),%r1 /* get task ptr */305LDREG TI_TASK(%r1), %r1306LDREG TASK_PT_GR26(%r1), %r26 /* Restore the users args */307LDREG TASK_PT_GR25(%r1), %r25308LDREG TASK_PT_GR24(%r1), %r24309LDREG TASK_PT_GR23(%r1), %r23310#ifdef CONFIG_64BIT311LDREG TASK_PT_GR22(%r1), %r22312LDREG TASK_PT_GR21(%r1), %r21313ldo -16(%r30),%r29 /* Reference param save area */314#endif315316comiclr,>>= __NR_Linux_syscalls, %r20, %r0317b,n .Lsyscall_nosys318319LDREGX %r20(%r19), %r19320321/* If this is a sys_rt_sigreturn call, and the signal was received322* when not in_syscall, then we want to return via syscall_exit_rfi,323* not syscall_exit. Signal no. in r20, in_syscall in r25 (see324* trampoline code in signal.c).325*/326ldi __NR_rt_sigreturn,%r2327comb,= %r2,%r20,.Ltrace_rt_sigreturn328.Ltrace_in_syscall:329ldil L%tracesys_exit,%r2330be 0(%sr7,%r19)331ldo R%tracesys_exit(%r2),%r2332333/* Do *not* call this function on the gateway page, because it334makes a direct call to syscall_trace. */335336tracesys_exit:337ldo -THREAD_SZ_ALGN-FRAME_SIZE(%r30),%r1 /* get task ptr */338LDREG TI_TASK(%r1), %r1339#ifdef CONFIG_64BIT340ldo -16(%r30),%r29 /* Reference param save area */341#endif342ldo TASK_REGS(%r1),%r26343bl do_syscall_trace_exit,%r2344STREG %r28,TASK_PT_GR28(%r1) /* save return value now */345ldo -THREAD_SZ_ALGN-FRAME_SIZE(%r30),%r1 /* get task ptr */346LDREG TI_TASK(%r1), %r1347LDREG TASK_PT_GR28(%r1), %r28 /* Restore return val. */348349ldil L%syscall_exit,%r1350be,n R%syscall_exit(%sr7,%r1)351352.Ltrace_rt_sigreturn:353comib,<> 0,%r25,.Ltrace_in_syscall354ldil L%tracesys_sigexit,%r2355be 0(%sr7,%r19)356ldo R%tracesys_sigexit(%r2),%r2357358tracesys_sigexit:359ldo -THREAD_SZ_ALGN-FRAME_SIZE(%r30),%r1 /* get task ptr */360LDREG TI_TASK(%r1), %r1361#ifdef CONFIG_64BIT362ldo -16(%r30),%r29 /* Reference param save area */363#endif364bl do_syscall_trace_exit,%r2365ldo TASK_REGS(%r1),%r26366367ldil L%syscall_exit_rfi,%r1368be,n R%syscall_exit_rfi(%sr7,%r1)369370371/*********************************************************37232/64-bit Light-Weight-Syscall ABI373374* - Indicates a hint for userspace inline asm375implementations.376377Syscall number (caller-saves)378- %r20379* In asm clobber.380381Argument registers (caller-saves)382- %r26, %r25, %r24, %r23, %r22383* In asm input.384385Return registers (caller-saves)386- %r28 (return), %r21 (errno)387* In asm output.388389Caller-saves registers390- %r1, %r27, %r29391- %r2 (return pointer)392- %r31 (ble link register)393* In asm clobber.394395Callee-saves registers396- %r3-%r18397- %r30 (stack pointer)398* Not in asm clobber.399400If userspace is 32-bit:401Callee-saves registers402- %r19 (32-bit PIC register)403404Differences from 32-bit calling convention:405- Syscall number in %r20406- Additional argument register %r22 (arg4)407- Callee-saves %r19.408409If userspace is 64-bit:410Callee-saves registers411- %r27 (64-bit PIC register)412413Differences from 64-bit calling convention:414- Syscall number in %r20415- Additional argument register %r22 (arg4)416- Callee-saves %r27.417418Error codes returned by entry path:419420ENOSYS - r20 was an invalid LWS number.421422*********************************************************/423lws_start:424425#ifdef CONFIG_64BIT426/* FIXME: If we are a 64-bit kernel just427* turn this on unconditionally.428*/429ssm PSW_SM_W, %r1430extrd,u %r1,PSW_W_BIT,1,%r1431/* sp must be aligned on 4, so deposit the W bit setting into432* the bottom of sp temporarily */433or,ev %r1,%r30,%r30434435/* Clip LWS number to a 32-bit value always */436depdi 0, 31, 32, %r20437#endif438439/* Is the lws entry number valid? */440comiclr,>> __NR_lws_entries, %r20, %r0441b,n lws_exit_nosys442443/* WARNING: Trashing sr2 and sr3 */444mfsp %sr7,%r1 /* get userspace into sr3 */445mtsp %r1,%sr3446mtsp %r0,%sr2 /* get kernel space into sr2 */447448/* Load table start */449ldil L%lws_table, %r1450ldo R%lws_table(%r1), %r28 /* Scratch use of r28 */451LDREGX %r20(%sr2,r28), %r21 /* Scratch use of r21 */452453/* Jump to lws, lws table pointers already relocated */454be,n 0(%sr2,%r21)455456lws_exit_nosys:457ldo -ENOSYS(%r0),%r21 /* set errno */458/* Fall through: Return to userspace */459460lws_exit:461#ifdef CONFIG_64BIT462/* decide whether to reset the wide mode bit463*464* For a syscall, the W bit is stored in the lowest bit465* of sp. Extract it and reset W if it is zero */466extrd,u,*<> %r30,63,1,%r1467rsm PSW_SM_W, %r0468/* now reset the lowest bit of sp if it was set */469xor %r30,%r1,%r30470#endif471be,n 0(%sr7, %r31)472473474475/***************************************************476Implementing CAS as an atomic operation:477478%r26 - Address to examine479%r25 - Old value to check (old)480%r24 - New value to set (new)481%r28 - Return prev through this register.482%r21 - Kernel error code483484If debugging is DISabled:485486%r21 has the following meanings:487488EAGAIN - CAS is busy, ldcw failed, try again.489EFAULT - Read or write failed.490491If debugging is enabled:492493EDEADLOCK - CAS called recursively.494EAGAIN && r28 == 1 - CAS is busy. Lock contended.495EAGAIN && r28 == 2 - CAS is busy. ldcw failed.496EFAULT - Read or write failed.497498Scratch: r20, r28, r1499500****************************************************/501502/* Do not enable LWS debugging */503#define ENABLE_LWS_DEBUG 0504505/* ELF64 Process entry path */506lws_compare_and_swap64:507#ifdef CONFIG_64BIT508b,n lws_compare_and_swap509#else510/* If we are not a 64-bit kernel, then we don't511* have 64-bit input registers, and calling512* the 64-bit LWS CAS returns ENOSYS.513*/514b,n lws_exit_nosys515#endif516517/* ELF32 Process entry path */518lws_compare_and_swap32:519#ifdef CONFIG_64BIT520/* Clip all the input registers */521depdi 0, 31, 32, %r26522depdi 0, 31, 32, %r25523depdi 0, 31, 32, %r24524#endif525526lws_compare_and_swap:527/* Load start of lock table */528ldil L%lws_lock_start, %r20529ldo R%lws_lock_start(%r20), %r28530531/* Extract four bits from r26 and hash lock (Bits 4-7) */532extru %r26, 27, 4, %r20533534/* Find lock to use, the hash is either one of 0 to53515, multiplied by 16 (keep it 16-byte aligned)536and add to the lock table offset. */537shlw %r20, 4, %r20538add %r20, %r28, %r20539540# if ENABLE_LWS_DEBUG541/*542DEBUG, check for deadlock!543If the thread register values are the same544then we were the one that locked it last and545this is a recurisve call that will deadlock.546We *must* giveup this call and fail.547*/548ldw 4(%sr2,%r20), %r28 /* Load thread register */549/* WARNING: If cr27 cycles to the same value we have problems */550mfctl %cr27, %r21 /* Get current thread register */551cmpb,<>,n %r21, %r28, cas_lock /* Called recursive? */552b lws_exit /* Return error! */553ldo -EDEADLOCK(%r0), %r21554cas_lock:555cmpb,=,n %r0, %r28, cas_nocontend /* Is nobody using it? */556ldo 1(%r0), %r28 /* 1st case */557b lws_exit /* Contended... */558ldo -EAGAIN(%r0), %r21 /* Spin in userspace */559cas_nocontend:560# endif561/* ENABLE_LWS_DEBUG */562563LDCW 0(%sr2,%r20), %r28 /* Try to acquire the lock */564cmpb,<>,n %r0, %r28, cas_action /* Did we get it? */565cas_wouldblock:566ldo 2(%r0), %r28 /* 2nd case */567b lws_exit /* Contended... */568ldo -EAGAIN(%r0), %r21 /* Spin in userspace */569570/*571prev = *addr;572if ( prev == old )573*addr = new;574return prev;575*/576577/* NOTES:578This all works becuse intr_do_signal579and schedule both check the return iasq580and see that we are on the kernel page581so this process is never scheduled off582or is ever sent any signal of any sort,583thus it is wholly atomic from usrspaces584perspective585*/586cas_action:587#if defined CONFIG_SMP && ENABLE_LWS_DEBUG588/* DEBUG */589mfctl %cr27, %r1590stw %r1, 4(%sr2,%r20)591#endif592/* The load and store could fail */5931: ldw 0(%sr3,%r26), %r28594sub,<> %r28, %r25, %r05952: stw %r24, 0(%sr3,%r26)596/* Free lock */597stw %r20, 0(%sr2,%r20)598#if ENABLE_LWS_DEBUG599/* Clear thread register indicator */600stw %r0, 4(%sr2,%r20)601#endif602/* Return to userspace, set no error */603b lws_exit604copy %r0, %r216056063:607/* Error occurred on load or store */608/* Free lock */609stw %r20, 0(%sr2,%r20)610#if ENABLE_LWS_DEBUG611stw %r0, 4(%sr2,%r20)612#endif613b lws_exit614ldo -EFAULT(%r0),%r21 /* set errno */615nop616nop617nop618nop619620/* Two exception table entries, one for the load,621the other for the store. Either return -EFAULT.622Each of the entries must be relocated. */623.section __ex_table,"aw"624ASM_ULONG_INSN (1b - linux_gateway_page), (3b - linux_gateway_page)625ASM_ULONG_INSN (2b - linux_gateway_page), (3b - linux_gateway_page)626.previous627628629/* Make sure nothing else is placed on this page */630.align PAGE_SIZE631END(linux_gateway_page)632ENTRY(end_linux_gateway_page)633634/* Relocate symbols assuming linux_gateway_page is mapped635to virtual address 0x0 */636637#define LWS_ENTRY(_name_) ASM_ULONG_INSN (lws_##_name_ - linux_gateway_page)638639.section .rodata,"a"640641.align PAGE_SIZE642/* Light-weight-syscall table */643/* Start of lws table. */644ENTRY(lws_table)645LWS_ENTRY(compare_and_swap32) /* 0 - ELF32 Atomic compare and swap */646LWS_ENTRY(compare_and_swap64) /* 1 - ELF64 Atomic compare and swap */647END(lws_table)648/* End of lws table */649650.align PAGE_SIZE651ENTRY(sys_call_table)652#include "syscall_table.S"653END(sys_call_table)654655#ifdef CONFIG_64BIT656.align PAGE_SIZE657ENTRY(sys_call_table64)658#define SYSCALL_TABLE_64BIT659#include "syscall_table.S"660END(sys_call_table64)661#endif662663/*664All light-weight-syscall atomic operations665will use this set of locks666667NOTE: The lws_lock_start symbol must be668at least 16-byte aligned for safe use669with ldcw.670*/671.section .data672.align PAGE_SIZE673ENTRY(lws_lock_start)674/* lws locks */675.rept 16676/* Keep locks aligned at 16-bytes */677.word 1678.word 0679.word 0680.word 0681.endr682END(lws_lock_start)683.previous684685.end686687688689690