fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / arch / parisc / kernel / entry.S
1 /*
2  * Linux/PA-RISC Project (http://www.parisc-linux.org/)
3  *
4  * kernel entry points (interruptions, system call wrappers)
5  *  Copyright (C) 1999,2000 Philipp Rumpf 
6  *  Copyright (C) 1999 SuSE GmbH Nuernberg 
7  *  Copyright (C) 2000 Hewlett-Packard (John Marvin)
8  *  Copyright (C) 1999 Hewlett-Packard (Frank Rowand)
9  *
10  *    This program is free software; you can redistribute it and/or modify
11  *    it under the terms of the GNU General Public License as published by
12  *    the Free Software Foundation; either version 2, or (at your option)
13  *    any later version.
14  *
15  *    This program is distributed in the hope that it will be useful,
16  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *    GNU General Public License for more details.
19  *
20  *    You should have received a copy of the GNU General Public License
21  *    along with this program; if not, write to the Free Software
22  *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25 #include <asm/asm-offsets.h>
26
27 /* we have the following possibilities to act on an interruption:
28  *  - handle in assembly and use shadowed registers only
29  *  - save registers to kernel stack and handle in assembly or C */
30
31
32 #include <asm/psw.h>
33 #include <asm/cache.h>          /* for L1_CACHE_SHIFT */
34 #include <asm/assembly.h>       /* for LDREG/STREG defines */
35 #include <asm/pgtable.h>
36 #include <asm/signal.h>
37 #include <asm/unistd.h>
38 #include <asm/thread_info.h>
39
40 #ifdef CONFIG_64BIT
41 #define CMPIB           cmpib,*
42 #define CMPB            cmpb,*
43 #define COND(x)         *x
44
45         .level 2.0w
46 #else
47 #define CMPIB           cmpib,
48 #define CMPB            cmpb,
49 #define COND(x)         x
50
51         .level 2.0
52 #endif
53
54         .import         pa_dbit_lock,data
55
56         /* space_to_prot macro creates a prot id from a space id */
57
58 #if (SPACEID_SHIFT) == 0
59         .macro  space_to_prot spc prot
60         depd,z  \spc,62,31,\prot
61         .endm
62 #else
63         .macro  space_to_prot spc prot
64         extrd,u \spc,(64 - (SPACEID_SHIFT)),32,\prot
65         .endm
66 #endif
67
68         /* Switch to virtual mapping, trashing only %r1 */
69         .macro  virt_map
70         /* pcxt_ssm_bug */
71         rsm     PSW_SM_I, %r0   /* barrier for "Relied upon Translation */
72         mtsp    %r0, %sr4
73         mtsp    %r0, %sr5
74         mfsp    %sr7, %r1
75         or,=    %r0,%r1,%r0     /* Only save sr7 in sr3 if sr7 != 0 */
76         mtsp    %r1, %sr3
77         tovirt_r1 %r29
78         load32  KERNEL_PSW, %r1
79
80         rsm     PSW_SM_QUIET,%r0        /* second "heavy weight" ctl op */
81         mtsp    %r0, %sr6
82         mtsp    %r0, %sr7
83         mtctl   %r0, %cr17      /* Clear IIASQ tail */
84         mtctl   %r0, %cr17      /* Clear IIASQ head */
85         mtctl   %r1, %ipsw
86         load32  4f, %r1
87         mtctl   %r1, %cr18      /* Set IIAOQ tail */
88         ldo     4(%r1), %r1
89         mtctl   %r1, %cr18      /* Set IIAOQ head */
90         rfir
91         nop
92 4:
93         .endm
94
95         /*
96          * The "get_stack" macros are responsible for determining the
97          * kernel stack value.
98          *
99          * For Faults:
100          *      If sr7 == 0
101          *          Already using a kernel stack, so call the
102          *          get_stack_use_r30 macro to push a pt_regs structure
103          *          on the stack, and store registers there.
104          *      else
105          *          Need to set up a kernel stack, so call the
106          *          get_stack_use_cr30 macro to set up a pointer
107          *          to the pt_regs structure contained within the
108          *          task pointer pointed to by cr30. Set the stack
109          *          pointer to point to the end of the task structure.
110          *
111          * For Interrupts:
112          *      If sr7 == 0
113          *          Already using a kernel stack, check to see if r30
114          *          is already pointing to the per processor interrupt
115          *          stack. If it is, call the get_stack_use_r30 macro
116          *          to push a pt_regs structure on the stack, and store
117          *          registers there. Otherwise, call get_stack_use_cr31
118          *          to get a pointer to the base of the interrupt stack
119          *          and push a pt_regs structure on that stack.
120          *      else
121          *          Need to set up a kernel stack, so call the
122          *          get_stack_use_cr30 macro to set up a pointer
123          *          to the pt_regs structure contained within the
124          *          task pointer pointed to by cr30. Set the stack
125          *          pointer to point to the end of the task structure.
126          *          N.B: We don't use the interrupt stack for the
127          *          first interrupt from userland, because signals/
128          *          resched's are processed when returning to userland,
129          *          and we can sleep in those cases.
130          *
131          * Note that we use shadowed registers for temps until
132          * we can save %r26 and %r29. %r26 is used to preserve
133          * %r8 (a shadowed register) which temporarily contained
134          * either the fault type ("code") or the eirr. We need
135          * to use a non-shadowed register to carry the value over
136          * the rfir in virt_map. We use %r26 since this value winds
137          * up being passed as the argument to either do_cpu_irq_mask
138          * or handle_interruption. %r29 is used to hold a pointer
139          * the register save area, and once again, it needs to
140          * be a non-shadowed register so that it survives the rfir.
141          *
142          * N.B. TASK_SZ_ALGN and PT_SZ_ALGN include space for a stack frame.
143          */
144
145         .macro  get_stack_use_cr30
146
147         /* we save the registers in the task struct */
148
149         mfctl   %cr30, %r1
150         tophys  %r1,%r9
151         LDREG   TI_TASK(%r9), %r1       /* thread_info -> task_struct */
152         tophys  %r1,%r9
153         ldo     TASK_REGS(%r9),%r9
154         STREG   %r30, PT_GR30(%r9)
155         STREG   %r29,PT_GR29(%r9)
156         STREG   %r26,PT_GR26(%r9)
157         copy    %r9,%r29
158         mfctl   %cr30, %r1
159         ldo     THREAD_SZ_ALGN(%r1), %r30
160         .endm
161
162         .macro  get_stack_use_r30
163
164         /* we put a struct pt_regs on the stack and save the registers there */
165
166         tophys  %r30,%r9
167         STREG   %r30,PT_GR30(%r9)
168         ldo     PT_SZ_ALGN(%r30),%r30
169         STREG   %r29,PT_GR29(%r9)
170         STREG   %r26,PT_GR26(%r9)
171         copy    %r9,%r29
172         .endm
173
174         .macro  rest_stack
175         LDREG   PT_GR1(%r29), %r1
176         LDREG   PT_GR30(%r29),%r30
177         LDREG   PT_GR29(%r29),%r29
178         .endm
179
180         /* default interruption handler
181          * (calls traps.c:handle_interruption) */
182         .macro  def code
183         b       intr_save
184         ldi     \code, %r8
185         .align  32
186         .endm
187
188         /* Interrupt interruption handler
189          * (calls irq.c:do_cpu_irq_mask) */
190         .macro  extint code
191         b       intr_extint
192         mfsp    %sr7,%r16
193         .align  32
194         .endm   
195
196         .import os_hpmc, code
197
198         /* HPMC handler */
199         .macro  hpmc code
200         nop                     /* must be a NOP, will be patched later */
201         load32  PA(os_hpmc), %r3
202         bv,n    0(%r3)
203         nop
204         .word   0               /* checksum (will be patched) */
205         .word   PA(os_hpmc)     /* address of handler */
206         .word   0               /* length of handler */
207         .endm
208
209         /*
210          * Performance Note: Instructions will be moved up into
211          * this part of the code later on, once we are sure
212          * that the tlb miss handlers are close to final form.
213          */
214
215         /* Register definitions for tlb miss handler macros */
216
217         va  = r8        /* virtual address for which the trap occured */
218         spc = r24       /* space for which the trap occured */
219
220 #ifndef CONFIG_64BIT
221
222         /*
223          * itlb miss interruption handler (parisc 1.1 - 32 bit)
224          */
225
226         .macro  itlb_11 code
227
228         mfctl   %pcsq, spc
229         b       itlb_miss_11
230         mfctl   %pcoq, va
231
232         .align          32
233         .endm
234 #endif
235         
236         /*
237          * itlb miss interruption handler (parisc 2.0)
238          */
239
240         .macro  itlb_20 code
241         mfctl   %pcsq, spc
242 #ifdef CONFIG_64BIT
243         b       itlb_miss_20w
244 #else
245         b       itlb_miss_20
246 #endif
247         mfctl   %pcoq, va
248
249         .align          32
250         .endm
251         
252 #ifndef CONFIG_64BIT
253         /*
254          * naitlb miss interruption handler (parisc 1.1 - 32 bit)
255          *
256          * Note: naitlb misses will be treated
257          * as an ordinary itlb miss for now.
258          * However, note that naitlb misses
259          * have the faulting address in the
260          * IOR/ISR.
261          */
262
263         .macro  naitlb_11 code
264
265         mfctl   %isr,spc
266         b       itlb_miss_11
267         mfctl   %ior,va
268         /* FIXME: If user causes a naitlb miss, the priv level may not be in
269          * lower bits of va, where the itlb miss handler is expecting them
270          */
271
272         .align          32
273         .endm
274 #endif
275         
276         /*
277          * naitlb miss interruption handler (parisc 2.0)
278          *
279          * Note: naitlb misses will be treated
280          * as an ordinary itlb miss for now.
281          * However, note that naitlb misses
282          * have the faulting address in the
283          * IOR/ISR.
284          */
285
286         .macro  naitlb_20 code
287
288         mfctl   %isr,spc
289 #ifdef CONFIG_64BIT
290         b       itlb_miss_20w
291 #else
292         b       itlb_miss_20
293 #endif
294         mfctl   %ior,va
295         /* FIXME: If user causes a naitlb miss, the priv level may not be in
296          * lower bits of va, where the itlb miss handler is expecting them
297          */
298
299         .align          32
300         .endm
301         
302 #ifndef CONFIG_64BIT
303         /*
304          * dtlb miss interruption handler (parisc 1.1 - 32 bit)
305          */
306
307         .macro  dtlb_11 code
308
309         mfctl   %isr, spc
310         b       dtlb_miss_11
311         mfctl   %ior, va
312
313         .align          32
314         .endm
315 #endif
316
317         /*
318          * dtlb miss interruption handler (parisc 2.0)
319          */
320
321         .macro  dtlb_20 code
322
323         mfctl   %isr, spc
324 #ifdef CONFIG_64BIT
325         b       dtlb_miss_20w
326 #else
327         b       dtlb_miss_20
328 #endif
329         mfctl   %ior, va
330
331         .align          32
332         .endm
333         
334 #ifndef CONFIG_64BIT
335         /* nadtlb miss interruption handler (parisc 1.1 - 32 bit) */
336
337         .macro  nadtlb_11 code
338
339         mfctl   %isr,spc
340         b       nadtlb_miss_11
341         mfctl   %ior,va
342
343         .align          32
344         .endm
345 #endif
346         
347         /* nadtlb miss interruption handler (parisc 2.0) */
348
349         .macro  nadtlb_20 code
350
351         mfctl   %isr,spc
352 #ifdef CONFIG_64BIT
353         b       nadtlb_miss_20w
354 #else
355         b       nadtlb_miss_20
356 #endif
357         mfctl   %ior,va
358
359         .align          32
360         .endm
361         
362 #ifndef CONFIG_64BIT
363         /*
364          * dirty bit trap interruption handler (parisc 1.1 - 32 bit)
365          */
366
367         .macro  dbit_11 code
368
369         mfctl   %isr,spc
370         b       dbit_trap_11
371         mfctl   %ior,va
372
373         .align          32
374         .endm
375 #endif
376
377         /*
378          * dirty bit trap interruption handler (parisc 2.0)
379          */
380
381         .macro  dbit_20 code
382
383         mfctl   %isr,spc
384 #ifdef CONFIG_64BIT
385         b       dbit_trap_20w
386 #else
387         b       dbit_trap_20
388 #endif
389         mfctl   %ior,va
390
391         .align          32
392         .endm
393
394         /* The following are simple 32 vs 64 bit instruction
395          * abstractions for the macros */
396         .macro          EXTR    reg1,start,length,reg2
397 #ifdef CONFIG_64BIT
398         extrd,u         \reg1,32+\start,\length,\reg2
399 #else
400         extrw,u         \reg1,\start,\length,\reg2
401 #endif
402         .endm
403
404         .macro          DEP     reg1,start,length,reg2
405 #ifdef CONFIG_64BIT
406         depd            \reg1,32+\start,\length,\reg2
407 #else
408         depw            \reg1,\start,\length,\reg2
409 #endif
410         .endm
411
412         .macro          DEPI    val,start,length,reg
413 #ifdef CONFIG_64BIT
414         depdi           \val,32+\start,\length,\reg
415 #else
416         depwi           \val,\start,\length,\reg
417 #endif
418         .endm
419
420         /* In LP64, the space contains part of the upper 32 bits of the
421          * fault.  We have to extract this and place it in the va,
422          * zeroing the corresponding bits in the space register */
423         .macro          space_adjust    spc,va,tmp
424 #ifdef CONFIG_64BIT
425         extrd,u         \spc,63,SPACEID_SHIFT,\tmp
426         depd            %r0,63,SPACEID_SHIFT,\spc
427         depd            \tmp,31,SPACEID_SHIFT,\va
428 #endif
429         .endm
430
431         .import         swapper_pg_dir,code
432
433         /* Get the pgd.  For faults on space zero (kernel space), this
434          * is simply swapper_pg_dir.  For user space faults, the
435          * pgd is stored in %cr25 */
436         .macro          get_pgd         spc,reg
437         ldil            L%PA(swapper_pg_dir),\reg
438         ldo             R%PA(swapper_pg_dir)(\reg),\reg
439         or,COND(=)      %r0,\spc,%r0
440         mfctl           %cr25,\reg
441         .endm
442
443         /* 
444                 space_check(spc,tmp,fault)
445
446                 spc - The space we saw the fault with.
447                 tmp - The place to store the current space.
448                 fault - Function to call on failure.
449
450                 Only allow faults on different spaces from the
451                 currently active one if we're the kernel 
452
453         */
454         .macro          space_check     spc,tmp,fault
455         mfsp            %sr7,\tmp
456         or,COND(<>)     %r0,\spc,%r0    /* user may execute gateway page
457                                          * as kernel, so defeat the space
458                                          * check if it is */
459         copy            \spc,\tmp
460         or,COND(=)      %r0,\tmp,%r0    /* nullify if executing as kernel */
461         cmpb,COND(<>),n \tmp,\spc,\fault
462         .endm
463
464         /* Look up a PTE in a 2-Level scheme (faulting at each
465          * level if the entry isn't present 
466          *
467          * NOTE: we use ldw even for LP64, since the short pointers
468          * can address up to 1TB
469          */
470         .macro          L2_ptep pmd,pte,index,va,fault
471 #if PT_NLEVELS == 3
472         EXTR            \va,31-ASM_PMD_SHIFT,ASM_BITS_PER_PMD,\index
473 #else
474         EXTR            \va,31-ASM_PGDIR_SHIFT,ASM_BITS_PER_PGD,\index
475 #endif
476         DEP             %r0,31,PAGE_SHIFT,\pmd  /* clear offset */
477         copy            %r0,\pte
478         ldw,s           \index(\pmd),\pmd
479         bb,>=,n         \pmd,_PxD_PRESENT_BIT,\fault
480         DEP             %r0,31,PxD_FLAG_SHIFT,\pmd /* clear flags */
481         copy            \pmd,%r9
482         SHLREG          %r9,PxD_VALUE_SHIFT,\pmd
483         EXTR            \va,31-PAGE_SHIFT,ASM_BITS_PER_PTE,\index
484         DEP             %r0,31,PAGE_SHIFT,\pmd  /* clear offset */
485         shladd          \index,BITS_PER_PTE_ENTRY,\pmd,\pmd
486         LDREG           %r0(\pmd),\pte          /* pmd is now pte */
487         bb,>=,n         \pte,_PAGE_PRESENT_BIT,\fault
488         .endm
489
490         /* Look up PTE in a 3-Level scheme.
491          *
492          * Here we implement a Hybrid L2/L3 scheme: we allocate the
493          * first pmd adjacent to the pgd.  This means that we can
494          * subtract a constant offset to get to it.  The pmd and pgd
495          * sizes are arranged so that a single pmd covers 4GB (giving
496          * a full LP64 process access to 8TB) so our lookups are
497          * effectively L2 for the first 4GB of the kernel (i.e. for
498          * all ILP32 processes and all the kernel for machines with
499          * under 4GB of memory) */
500         .macro          L3_ptep pgd,pte,index,va,fault
501 #if PT_NLEVELS == 3 /* we might have a 2-Level scheme, e.g. with 16kb page size */
502         extrd,u         \va,63-ASM_PGDIR_SHIFT,ASM_BITS_PER_PGD,\index
503         copy            %r0,\pte
504         extrd,u,*=      \va,63-ASM_PGDIR_SHIFT,64-ASM_PGDIR_SHIFT,%r0
505         ldw,s           \index(\pgd),\pgd
506         extrd,u,*=      \va,63-ASM_PGDIR_SHIFT,64-ASM_PGDIR_SHIFT,%r0
507         bb,>=,n         \pgd,_PxD_PRESENT_BIT,\fault
508         extrd,u,*=      \va,63-ASM_PGDIR_SHIFT,64-ASM_PGDIR_SHIFT,%r0
509         shld            \pgd,PxD_VALUE_SHIFT,\index
510         extrd,u,*=      \va,63-ASM_PGDIR_SHIFT,64-ASM_PGDIR_SHIFT,%r0
511         copy            \index,\pgd
512         extrd,u,*<>     \va,63-ASM_PGDIR_SHIFT,64-ASM_PGDIR_SHIFT,%r0
513         ldo             ASM_PGD_PMD_OFFSET(\pgd),\pgd
514 #endif
515         L2_ptep         \pgd,\pte,\index,\va,\fault
516         .endm
517
518         /* Set the _PAGE_ACCESSED bit of the PTE.  Be clever and
519          * don't needlessly dirty the cache line if it was already set */
520         .macro          update_ptep     ptep,pte,tmp,tmp1
521         ldi             _PAGE_ACCESSED,\tmp1
522         or              \tmp1,\pte,\tmp
523         and,COND(<>)    \tmp1,\pte,%r0
524         STREG           \tmp,0(\ptep)
525         .endm
526
527         /* Set the dirty bit (and accessed bit).  No need to be
528          * clever, this is only used from the dirty fault */
529         .macro          update_dirty    ptep,pte,tmp
530         ldi             _PAGE_ACCESSED|_PAGE_DIRTY,\tmp
531         or              \tmp,\pte,\pte
532         STREG           \pte,0(\ptep)
533         .endm
534
535         /* Convert the pte and prot to tlb insertion values.  How
536          * this happens is quite subtle, read below */
537         .macro          make_insert_tlb spc,pte,prot
538         space_to_prot   \spc \prot        /* create prot id from space */
539         /* The following is the real subtlety.  This is depositing
540          * T <-> _PAGE_REFTRAP
541          * D <-> _PAGE_DIRTY
542          * B <-> _PAGE_DMB (memory break)
543          *
544          * Then incredible subtlety: The access rights are
545          * _PAGE_GATEWAY _PAGE_EXEC _PAGE_READ
546          * See 3-14 of the parisc 2.0 manual
547          *
548          * Finally, _PAGE_READ goes in the top bit of PL1 (so we
549          * trigger an access rights trap in user space if the user
550          * tries to read an unreadable page */
551         depd            \pte,8,7,\prot
552
553         /* PAGE_USER indicates the page can be read with user privileges,
554          * so deposit X1|11 to PL1|PL2 (remember the upper bit of PL1
555          * contains _PAGE_READ */
556         extrd,u,*=      \pte,_PAGE_USER_BIT+32,1,%r0
557         depdi           7,11,3,\prot
558         /* If we're a gateway page, drop PL2 back to zero for promotion
559          * to kernel privilege (so we can execute the page as kernel).
560          * Any privilege promotion page always denys read and write */
561         extrd,u,*=      \pte,_PAGE_GATEWAY_BIT+32,1,%r0
562         depd            %r0,11,2,\prot  /* If Gateway, Set PL2 to 0 */
563
564         /* Enforce uncacheable pages.
565          * This should ONLY be use for MMIO on PA 2.0 machines.
566          * Memory/DMA is cache coherent on all PA2.0 machines we support
567          * (that means T-class is NOT supported) and the memory controllers
568          * on most of those machines only handles cache transactions.
569          */
570         extrd,u,*=      \pte,_PAGE_NO_CACHE_BIT+32,1,%r0
571         depi            1,12,1,\prot
572
573         /* Drop prot bits and convert to page addr for iitlbt and idtlbt */
574         extrd,u         \pte,(63-ASM_PFN_PTE_SHIFT)+(63-58),64-PAGE_SHIFT,\pte
575         depdi           _PAGE_SIZE_ENCODING_DEFAULT,63,63-58,\pte
576         .endm
577
578         /* Identical macro to make_insert_tlb above, except it
579          * makes the tlb entry for the differently formatted pa11
580          * insertion instructions */
581         .macro          make_insert_tlb_11      spc,pte,prot
582         zdep            \spc,30,15,\prot
583         dep             \pte,8,7,\prot
584         extru,=         \pte,_PAGE_NO_CACHE_BIT,1,%r0
585         depi            1,12,1,\prot
586         extru,=         \pte,_PAGE_USER_BIT,1,%r0
587         depi            7,11,3,\prot   /* Set for user space (1 rsvd for read) */
588         extru,=         \pte,_PAGE_GATEWAY_BIT,1,%r0
589         depi            0,11,2,\prot    /* If Gateway, Set PL2 to 0 */
590
591         /* Get rid of prot bits and convert to page addr for iitlba */
592
593         depi            _PAGE_SIZE_ENCODING_DEFAULT,31,ASM_PFN_PTE_SHIFT,\pte
594         extru           \pte,24,25,\pte
595         .endm
596
597         /* This is for ILP32 PA2.0 only.  The TLB insertion needs
598          * to extend into I/O space if the address is 0xfXXXXXXX
599          * so we extend the f's into the top word of the pte in
600          * this case */
601         .macro          f_extend        pte,tmp
602         extrd,s         \pte,42,4,\tmp
603         addi,<>         1,\tmp,%r0
604         extrd,s         \pte,63,25,\pte
605         .endm
606
607         /* The alias region is an 8MB aligned 16MB to do clear and
608          * copy user pages at addresses congruent with the user
609          * virtual address.
610          *
611          * To use the alias page, you set %r26 up with the to TLB
612          * entry (identifying the physical page) and %r23 up with
613          * the from tlb entry (or nothing if only a to entry---for
614          * clear_user_page_asm) */
615         .macro          do_alias        spc,tmp,tmp1,va,pte,prot,fault
616         cmpib,COND(<>),n 0,\spc,\fault
617         ldil            L%(TMPALIAS_MAP_START),\tmp
618 #if defined(CONFIG_64BIT) && (TMPALIAS_MAP_START >= 0x80000000)
619         /* on LP64, ldi will sign extend into the upper 32 bits,
620          * which is behaviour we don't want */
621         depdi           0,31,32,\tmp
622 #endif
623         copy            \va,\tmp1
624         DEPI            0,31,23,\tmp1
625         cmpb,COND(<>),n \tmp,\tmp1,\fault
626         ldi             (_PAGE_DIRTY|_PAGE_WRITE|_PAGE_READ),\prot
627         depd,z          \prot,8,7,\prot
628         /*
629          * OK, it is in the temp alias region, check whether "from" or "to".
630          * Check "subtle" note in pacache.S re: r23/r26.
631          */
632 #ifdef CONFIG_64BIT
633         extrd,u,*=      \va,41,1,%r0
634 #else
635         extrw,u,=       \va,9,1,%r0
636 #endif
637         or,COND(tr)     %r23,%r0,\pte
638         or              %r26,%r0,\pte
639         .endm 
640
641
642         /*
643          * Align fault_vector_20 on 4K boundary so that both
644          * fault_vector_11 and fault_vector_20 are on the
645          * same page. This is only necessary as long as we
646          * write protect the kernel text, which we may stop
647          * doing once we use large page translations to cover
648          * the static part of the kernel address space.
649          */
650
651         .export fault_vector_20
652
653         .text
654
655         .align 4096
656
657 fault_vector_20:
658         /* First vector is invalid (0) */
659         .ascii  "cows can fly"
660         .byte 0
661         .align 32
662
663         hpmc             1
664         def              2
665         def              3
666         extint           4
667         def              5
668         itlb_20          6
669         def              7
670         def              8
671         def              9
672         def             10
673         def             11
674         def             12
675         def             13
676         def             14
677         dtlb_20         15
678 #if 0
679         naitlb_20       16
680 #else
681         def             16
682 #endif
683         nadtlb_20       17
684         def             18
685         def             19
686         dbit_20         20
687         def             21
688         def             22
689         def             23
690         def             24
691         def             25
692         def             26
693         def             27
694         def             28
695         def             29
696         def             30
697         def             31
698
699 #ifndef CONFIG_64BIT
700
701         .export fault_vector_11
702         
703         .align 2048
704
705 fault_vector_11:
706         /* First vector is invalid (0) */
707         .ascii  "cows can fly"
708         .byte 0
709         .align 32
710
711         hpmc             1
712         def              2
713         def              3
714         extint           4
715         def              5
716         itlb_11          6
717         def              7
718         def              8
719         def              9
720         def             10
721         def             11
722         def             12
723         def             13
724         def             14
725         dtlb_11         15
726 #if 0
727         naitlb_11       16
728 #else
729         def             16
730 #endif
731         nadtlb_11       17
732         def             18
733         def             19
734         dbit_11         20
735         def             21
736         def             22
737         def             23
738         def             24
739         def             25
740         def             26
741         def             27
742         def             28
743         def             29
744         def             30
745         def             31
746
747 #endif
748
749         .import         handle_interruption,code
750         .import         do_cpu_irq_mask,code
751
752         /*
753          * r26 = function to be called
754          * r25 = argument to pass in
755          * r24 = flags for do_fork()
756          *
757          * Kernel threads don't ever return, so they don't need
758          * a true register context. We just save away the arguments
759          * for copy_thread/ret_ to properly set up the child.
760          */
761
762 #define CLONE_VM 0x100  /* Must agree with <linux/sched.h> */
763 #define CLONE_UNTRACED 0x00800000
764 #define CLONE_KTHREAD 0x10000000
765
766         .export __kernel_thread, code
767         .import do_fork
768 __kernel_thread:
769         STREG   %r2, -RP_OFFSET(%r30)
770
771         copy    %r30, %r1
772         ldo     PT_SZ_ALGN(%r30),%r30
773 #ifdef CONFIG_64BIT
774         /* Yo, function pointers in wide mode are little structs... -PB */
775         ldd     24(%r26), %r2
776         STREG   %r2, PT_GR27(%r1)       /* Store childs %dp */
777         ldd     16(%r26), %r26
778
779         STREG   %r22, PT_GR22(%r1)      /* save r22 (arg5) */
780         copy    %r0, %r22               /* user_tid */
781 #endif
782         STREG   %r26, PT_GR26(%r1)  /* Store function & argument for child */
783         STREG   %r25, PT_GR25(%r1)
784         ldil    L%CLONE_UNTRACED, %r26
785         ldo     CLONE_VM(%r26), %r26   /* Force CLONE_VM since only init_mm */
786         or      %r26, %r24, %r26      /* will have kernel mappings.      */
787         ldi     1, %r25                 /* stack_start, signals kernel thread */
788         stw     %r0, -52(%r30)          /* user_tid */
789 #ifdef CONFIG_64BIT
790         ldo     -16(%r30),%r29          /* Reference param save area */
791 #endif
792         BL      do_fork, %r2
793         copy    %r1, %r24               /* pt_regs */
794
795         /* Parent Returns here */
796
797         LDREG   -PT_SZ_ALGN-RP_OFFSET(%r30), %r2
798         ldo     -PT_SZ_ALGN(%r30), %r30
799         bv      %r0(%r2)
800         nop
801
802         /*
803          * Child Returns here
804          *
805          * copy_thread moved args from temp save area set up above
806          * into task save area.
807          */
808
809         .export ret_from_kernel_thread
810 ret_from_kernel_thread:
811
812         /* Call schedule_tail first though */
813         BL      schedule_tail, %r2
814         nop
815
816         LDREG   TI_TASK-THREAD_SZ_ALGN(%r30), %r1
817         LDREG   TASK_PT_GR25(%r1), %r26
818 #ifdef CONFIG_64BIT
819         LDREG   TASK_PT_GR27(%r1), %r27
820         LDREG   TASK_PT_GR22(%r1), %r22
821 #endif
822         LDREG   TASK_PT_GR26(%r1), %r1
823         ble     0(%sr7, %r1)
824         copy    %r31, %r2
825
826 #ifdef CONFIG_64BIT
827         ldo     -16(%r30),%r29          /* Reference param save area */
828         loadgp                          /* Thread could have been in a module */
829 #endif
830 #ifndef CONFIG_64BIT
831         b       sys_exit
832 #else
833         load32  sys_exit, %r1
834         bv      %r0(%r1)
835 #endif
836         ldi     0, %r26
837
838         .import sys_execve, code
839         .export __execve, code
840 __execve:
841         copy    %r2, %r15
842         copy    %r30, %r16
843         ldo     PT_SZ_ALGN(%r30), %r30
844         STREG   %r26, PT_GR26(%r16)
845         STREG   %r25, PT_GR25(%r16)
846         STREG   %r24, PT_GR24(%r16)
847 #ifdef CONFIG_64BIT
848         ldo     -16(%r30),%r29          /* Reference param save area */
849 #endif
850         BL      sys_execve, %r2
851         copy    %r16, %r26
852
853         cmpib,=,n 0,%r28,intr_return    /* forward */
854
855         /* yes, this will trap and die. */
856         copy    %r15, %r2
857         copy    %r16, %r30
858         bv      %r0(%r2)
859         nop
860
861         .align 4
862
863         /*
864          * struct task_struct *_switch_to(struct task_struct *prev,
865          *      struct task_struct *next)
866          *
867          * switch kernel stacks and return prev */
868         .export _switch_to, code
869 _switch_to:
870         STREG    %r2, -RP_OFFSET(%r30)
871
872         callee_save_float
873         callee_save
874
875         load32  _switch_to_ret, %r2
876
877         STREG   %r2, TASK_PT_KPC(%r26)
878         LDREG   TASK_PT_KPC(%r25), %r2
879
880         STREG   %r30, TASK_PT_KSP(%r26)
881         LDREG   TASK_PT_KSP(%r25), %r30
882         LDREG   TASK_THREAD_INFO(%r25), %r25
883         bv      %r0(%r2)
884         mtctl   %r25,%cr30
885
886 _switch_to_ret:
887         mtctl   %r0, %cr0               /* Needed for single stepping */
888         callee_rest
889         callee_rest_float
890
891         LDREG   -RP_OFFSET(%r30), %r2
892         bv      %r0(%r2)
893         copy    %r26, %r28
894
895         /*
896          * Common rfi return path for interruptions, kernel execve, and
897          * sys_rt_sigreturn (sometimes).  The sys_rt_sigreturn syscall will
898          * return via this path if the signal was received when the process
899          * was running; if the process was blocked on a syscall then the
900          * normal syscall_exit path is used.  All syscalls for traced
901          * proceses exit via intr_restore.
902          *
903          * XXX If any syscalls that change a processes space id ever exit
904          * this way, then we will need to copy %sr3 in to PT_SR[3..7], and
905          * adjust IASQ[0..1].
906          *
907          */
908
909         .align 4096
910
911         .export syscall_exit_rfi
912 syscall_exit_rfi:
913         mfctl   %cr30,%r16
914         LDREG   TI_TASK(%r16), %r16     /* thread_info -> task_struct */
915         ldo     TASK_REGS(%r16),%r16
916         /* Force iaoq to userspace, as the user has had access to our current
917          * context via sigcontext. Also Filter the PSW for the same reason.
918          */
919         LDREG   PT_IAOQ0(%r16),%r19
920         depi    3,31,2,%r19
921         STREG   %r19,PT_IAOQ0(%r16)
922         LDREG   PT_IAOQ1(%r16),%r19
923         depi    3,31,2,%r19
924         STREG   %r19,PT_IAOQ1(%r16)
925         LDREG   PT_PSW(%r16),%r19
926         load32  USER_PSW_MASK,%r1
927 #ifdef CONFIG_64BIT
928         load32  USER_PSW_HI_MASK,%r20
929         depd    %r20,31,32,%r1
930 #endif
931         and     %r19,%r1,%r19 /* Mask out bits that user shouldn't play with */
932         load32  USER_PSW,%r1
933         or      %r19,%r1,%r19 /* Make sure default USER_PSW bits are set */
934         STREG   %r19,PT_PSW(%r16)
935
936         /*
937          * If we aren't being traced, we never saved space registers
938          * (we don't store them in the sigcontext), so set them
939          * to "proper" values now (otherwise we'll wind up restoring
940          * whatever was last stored in the task structure, which might
941          * be inconsistent if an interrupt occured while on the gateway
942          * page). Note that we may be "trashing" values the user put in
943          * them, but we don't support the user changing them.
944          */
945
946         STREG   %r0,PT_SR2(%r16)
947         mfsp    %sr3,%r19
948         STREG   %r19,PT_SR0(%r16)
949         STREG   %r19,PT_SR1(%r16)
950         STREG   %r19,PT_SR3(%r16)
951         STREG   %r19,PT_SR4(%r16)
952         STREG   %r19,PT_SR5(%r16)
953         STREG   %r19,PT_SR6(%r16)
954         STREG   %r19,PT_SR7(%r16)
955
956 intr_return:
957         /* NOTE: Need to enable interrupts incase we schedule. */
958         ssm     PSW_SM_I, %r0
959
960         /* Check for software interrupts */
961
962         .import irq_stat,data
963
964         load32  irq_stat,%r19
965 #ifdef CONFIG_SMP
966         mfctl   %cr30,%r1
967         ldw     TI_CPU(%r1),%r1 /* get cpu # - int */
968         /* shift left ____cacheline_aligned (aka L1_CACHE_BYTES) amount
969         ** irq_stat[] is defined using ____cacheline_aligned.
970         */
971         SHLREG  %r1,L1_CACHE_SHIFT,%r20
972         add     %r19,%r20,%r19  /* now have &irq_stat[smp_processor_id()] */
973 #endif /* CONFIG_SMP */
974
975 intr_check_resched:
976
977         /* check for reschedule */
978         mfctl   %cr30,%r1
979         LDREG   TI_FLAGS(%r1),%r19      /* sched.h: TIF_NEED_RESCHED */
980         bb,<,n  %r19,31-TIF_NEED_RESCHED,intr_do_resched /* forward */
981
982 intr_check_sig:
983         /* As above */
984         mfctl   %cr30,%r1
985         LDREG   TI_FLAGS(%r1),%r19      /* sched.h: TIF_SIGPENDING */
986         bb,<,n %r19, 31-TIF_SIGPENDING, intr_do_signal /* forward */
987
988 intr_restore:
989         copy            %r16,%r29
990         ldo             PT_FR31(%r29),%r1
991         rest_fp         %r1
992         rest_general    %r29
993
994         /* inverse of virt_map */
995         pcxt_ssm_bug
996         rsm             PSW_SM_QUIET,%r0        /* prepare for rfi */
997         tophys_r1       %r29
998
999         /* Restore space id's and special cr's from PT_REGS
1000          * structure pointed to by r29
1001          */
1002         rest_specials   %r29
1003
1004         /* IMPORTANT: rest_stack restores r29 last (we are using it)!
1005          * It also restores r1 and r30.
1006          */
1007         rest_stack
1008
1009         rfi
1010         nop
1011         nop
1012         nop
1013         nop
1014         nop
1015         nop
1016         nop
1017         nop
1018
1019 #ifndef CONFIG_PREEMPT
1020 # define intr_do_preempt        intr_restore
1021 #endif /* !CONFIG_PREEMPT */
1022
1023         .import schedule,code
1024 intr_do_resched:
1025         /* Only call schedule on return to userspace. If we're returning
1026          * to kernel space, we may schedule if CONFIG_PREEMPT, otherwise
1027          * we jump back to intr_restore.
1028          */
1029         LDREG   PT_IASQ0(%r16), %r20
1030         CMPIB=  0, %r20, intr_do_preempt
1031         nop
1032         LDREG   PT_IASQ1(%r16), %r20
1033         CMPIB=  0, %r20, intr_do_preempt
1034         nop
1035
1036 #ifdef CONFIG_64BIT
1037         ldo     -16(%r30),%r29          /* Reference param save area */
1038 #endif
1039
1040         ldil    L%intr_check_sig, %r2
1041 #ifndef CONFIG_64BIT
1042         b       schedule
1043 #else
1044         load32  schedule, %r20
1045         bv      %r0(%r20)
1046 #endif
1047         ldo     R%intr_check_sig(%r2), %r2
1048
1049         /* preempt the current task on returning to kernel
1050          * mode from an interrupt, iff need_resched is set,
1051          * and preempt_count is 0. otherwise, we continue on
1052          * our merry way back to the current running task.
1053          */
1054 #ifdef CONFIG_PREEMPT
1055         .import preempt_schedule_irq,code
1056 intr_do_preempt:
1057         rsm     PSW_SM_I, %r0           /* disable interrupts */
1058
1059         /* current_thread_info()->preempt_count */
1060         mfctl   %cr30, %r1
1061         LDREG   TI_PRE_COUNT(%r1), %r19
1062         CMPIB<> 0, %r19, intr_restore   /* if preempt_count > 0 */
1063         nop                             /* prev insn branched backwards */
1064
1065         /* check if we interrupted a critical path */
1066         LDREG   PT_PSW(%r16), %r20
1067         bb,<,n  %r20, 31 - PSW_SM_I, intr_restore
1068         nop
1069
1070         BL      preempt_schedule_irq, %r2
1071         nop
1072
1073         b,n     intr_restore            /* ssm PSW_SM_I done by intr_restore */
1074 #endif /* CONFIG_PREEMPT */
1075
1076         .import do_signal,code
1077 intr_do_signal:
1078         /* 
1079                 This check is critical to having LWS
1080                 working. The IASQ is zero on the gateway
1081                 page and we cannot deliver any signals until
1082                 we get off the gateway page.
1083
1084                 Only do signals if we are returning to user space 
1085         */
1086         LDREG   PT_IASQ0(%r16), %r20
1087         CMPIB= 0,%r20,intr_restore /* backward */
1088         nop
1089         LDREG   PT_IASQ1(%r16), %r20
1090         CMPIB= 0,%r20,intr_restore /* backward */
1091         nop
1092
1093         copy    %r0, %r24                       /* unsigned long in_syscall */
1094         copy    %r16, %r25                      /* struct pt_regs *regs */
1095 #ifdef CONFIG_64BIT
1096         ldo     -16(%r30),%r29                  /* Reference param save area */
1097 #endif
1098
1099         BL      do_signal,%r2
1100         copy    %r0, %r26                       /* sigset_t *oldset = NULL */
1101
1102         b       intr_check_sig
1103         nop
1104
1105         /*
1106          * External interrupts.
1107          */
1108
1109 intr_extint:
1110         CMPIB=,n 0,%r16,1f
1111         get_stack_use_cr30
1112         b,n 3f
1113
1114 1:
1115 #if 0  /* Interrupt Stack support not working yet! */
1116         mfctl   %cr31,%r1
1117         copy    %r30,%r17
1118         /* FIXME! depi below has hardcoded idea of interrupt stack size (32k)*/
1119 #ifdef CONFIG_64BIT
1120         depdi   0,63,15,%r17
1121 #else
1122         depi    0,31,15,%r17
1123 #endif
1124         CMPB=,n %r1,%r17,2f
1125         get_stack_use_cr31
1126         b,n 3f
1127 #endif
1128 2:
1129         get_stack_use_r30
1130
1131 3:
1132         save_specials   %r29
1133         virt_map
1134         save_general    %r29
1135
1136         ldo     PT_FR0(%r29), %r24
1137         save_fp %r24
1138         
1139         loadgp
1140
1141         copy    %r29, %r26      /* arg0 is pt_regs */
1142         copy    %r29, %r16      /* save pt_regs */
1143
1144         ldil    L%intr_return, %r2
1145
1146 #ifdef CONFIG_64BIT
1147         ldo     -16(%r30),%r29  /* Reference param save area */
1148 #endif
1149
1150         b       do_cpu_irq_mask
1151         ldo     R%intr_return(%r2), %r2 /* return to intr_return, not here */
1152
1153
1154         /* Generic interruptions (illegal insn, unaligned, page fault, etc) */
1155
1156         .export         intr_save, code /* for os_hpmc */
1157
1158 intr_save:
1159         mfsp    %sr7,%r16
1160         CMPIB=,n 0,%r16,1f
1161         get_stack_use_cr30
1162         b       2f
1163         copy    %r8,%r26
1164
1165 1:
1166         get_stack_use_r30
1167         copy    %r8,%r26
1168
1169 2:
1170         save_specials   %r29
1171
1172         /* If this trap is a itlb miss, skip saving/adjusting isr/ior */
1173
1174         /*
1175          * FIXME: 1) Use a #define for the hardwired "6" below (and in
1176          *           traps.c.
1177          *        2) Once we start executing code above 4 Gb, we need
1178          *           to adjust iasq/iaoq here in the same way we
1179          *           adjust isr/ior below.
1180          */
1181
1182         CMPIB=,n        6,%r26,skip_save_ior
1183
1184
1185         mfctl           %cr20, %r16 /* isr */
1186         nop             /* serialize mfctl on PA 2.0 to avoid 4 cycle penalty */
1187         mfctl           %cr21, %r17 /* ior */
1188
1189
1190 #ifdef CONFIG_64BIT
1191         /*
1192          * If the interrupted code was running with W bit off (32 bit),
1193          * clear the b bits (bits 0 & 1) in the ior.
1194          * save_specials left ipsw value in r8 for us to test.
1195          */
1196         extrd,u,*<>     %r8,PSW_W_BIT,1,%r0
1197         depdi           0,1,2,%r17
1198
1199         /*
1200          * FIXME: This code has hardwired assumptions about the split
1201          *        between space bits and offset bits. This will change
1202          *        when we allow alternate page sizes.
1203          */
1204
1205         /* adjust isr/ior. */
1206         extrd,u         %r16,63,SPACEID_SHIFT,%r1       /* get high bits from isr for ior */
1207         depd            %r1,31,SPACEID_SHIFT,%r17       /* deposit them into ior */
1208         depdi           0,63,SPACEID_SHIFT,%r16         /* clear them from isr */
1209 #endif
1210         STREG           %r16, PT_ISR(%r29)
1211         STREG           %r17, PT_IOR(%r29)
1212
1213
1214 skip_save_ior:
1215         virt_map
1216         save_general    %r29
1217
1218         ldo             PT_FR0(%r29), %r25
1219         save_fp         %r25
1220         
1221         loadgp
1222
1223         copy            %r29, %r25      /* arg1 is pt_regs */
1224 #ifdef CONFIG_64BIT
1225         ldo             -16(%r30),%r29  /* Reference param save area */
1226 #endif
1227
1228         ldil            L%intr_check_sig, %r2
1229         copy            %r25, %r16      /* save pt_regs */
1230
1231         b               handle_interruption
1232         ldo             R%intr_check_sig(%r2), %r2
1233
1234
1235         /*
1236          * Note for all tlb miss handlers:
1237          *
1238          * cr24 contains a pointer to the kernel address space
1239          * page directory.
1240          *
1241          * cr25 contains a pointer to the current user address
1242          * space page directory.
1243          *
1244          * sr3 will contain the space id of the user address space
1245          * of the current running thread while that thread is
1246          * running in the kernel.
1247          */
1248
1249         /*
1250          * register number allocations.  Note that these are all
1251          * in the shadowed registers
1252          */
1253
1254         t0 = r1         /* temporary register 0 */
1255         va = r8         /* virtual address for which the trap occured */
1256         t1 = r9         /* temporary register 1 */
1257         pte  = r16      /* pte/phys page # */
1258         prot = r17      /* prot bits */
1259         spc  = r24      /* space for which the trap occured */
1260         ptp = r25       /* page directory/page table pointer */
1261
1262 #ifdef CONFIG_64BIT
1263
1264 dtlb_miss_20w:
1265         space_adjust    spc,va,t0
1266         get_pgd         spc,ptp
1267         space_check     spc,t0,dtlb_fault
1268
1269         L3_ptep         ptp,pte,t0,va,dtlb_check_alias_20w
1270
1271         update_ptep     ptp,pte,t0,t1
1272
1273         make_insert_tlb spc,pte,prot
1274         
1275         idtlbt          pte,prot
1276
1277         rfir
1278         nop
1279
1280 dtlb_check_alias_20w:
1281         do_alias        spc,t0,t1,va,pte,prot,dtlb_fault
1282
1283         idtlbt          pte,prot
1284
1285         rfir
1286         nop
1287
1288 nadtlb_miss_20w:
1289         space_adjust    spc,va,t0
1290         get_pgd         spc,ptp
1291         space_check     spc,t0,nadtlb_fault
1292
1293         L3_ptep         ptp,pte,t0,va,nadtlb_check_flush_20w
1294
1295         update_ptep     ptp,pte,t0,t1
1296
1297         make_insert_tlb spc,pte,prot
1298
1299         idtlbt          pte,prot
1300
1301         rfir
1302         nop
1303
1304 nadtlb_check_flush_20w:
1305         bb,>=,n          pte,_PAGE_FLUSH_BIT,nadtlb_emulate
1306
1307         /* Insert a "flush only" translation */
1308
1309         depdi,z         7,7,3,prot
1310         depdi           1,10,1,prot
1311
1312         /* Get rid of prot bits and convert to page addr for idtlbt */
1313
1314         depdi           0,63,12,pte
1315         extrd,u         pte,56,52,pte
1316         idtlbt          pte,prot
1317
1318         rfir
1319         nop
1320
1321 #else
1322
1323 dtlb_miss_11:
1324         get_pgd         spc,ptp
1325
1326         space_check     spc,t0,dtlb_fault
1327
1328         L2_ptep         ptp,pte,t0,va,dtlb_check_alias_11
1329
1330         update_ptep     ptp,pte,t0,t1
1331
1332         make_insert_tlb_11      spc,pte,prot
1333
1334         mfsp            %sr1,t0  /* Save sr1 so we can use it in tlb inserts */
1335         mtsp            spc,%sr1
1336
1337         idtlba          pte,(%sr1,va)
1338         idtlbp          prot,(%sr1,va)
1339
1340         mtsp            t0, %sr1        /* Restore sr1 */
1341
1342         rfir
1343         nop
1344
1345 dtlb_check_alias_11:
1346
1347         /* Check to see if fault is in the temporary alias region */
1348
1349         cmpib,<>,n      0,spc,dtlb_fault /* forward */
1350         ldil            L%(TMPALIAS_MAP_START),t0
1351         copy            va,t1
1352         depwi           0,31,23,t1
1353         cmpb,<>,n       t0,t1,dtlb_fault /* forward */
1354         ldi             (_PAGE_DIRTY|_PAGE_WRITE|_PAGE_READ),prot
1355         depw,z          prot,8,7,prot
1356
1357         /*
1358          * OK, it is in the temp alias region, check whether "from" or "to".
1359          * Check "subtle" note in pacache.S re: r23/r26.
1360          */
1361
1362         extrw,u,=       va,9,1,r0
1363         or,tr           %r23,%r0,pte    /* If "from" use "from" page */
1364         or              %r26,%r0,pte    /* else "to", use "to" page  */
1365
1366         idtlba          pte,(va)
1367         idtlbp          prot,(va)
1368
1369         rfir
1370         nop
1371
1372 nadtlb_miss_11:
1373         get_pgd         spc,ptp
1374
1375         space_check     spc,t0,nadtlb_fault
1376
1377         L2_ptep         ptp,pte,t0,va,nadtlb_check_flush_11
1378
1379         update_ptep     ptp,pte,t0,t1
1380
1381         make_insert_tlb_11      spc,pte,prot
1382
1383
1384         mfsp            %sr1,t0  /* Save sr1 so we can use it in tlb inserts */
1385         mtsp            spc,%sr1
1386
1387         idtlba          pte,(%sr1,va)
1388         idtlbp          prot,(%sr1,va)
1389
1390         mtsp            t0, %sr1        /* Restore sr1 */
1391
1392         rfir
1393         nop
1394
1395 nadtlb_check_flush_11:
1396         bb,>=,n          pte,_PAGE_FLUSH_BIT,nadtlb_emulate
1397
1398         /* Insert a "flush only" translation */
1399
1400         zdepi           7,7,3,prot
1401         depi            1,10,1,prot
1402
1403         /* Get rid of prot bits and convert to page addr for idtlba */
1404
1405         depi            0,31,12,pte
1406         extru           pte,24,25,pte
1407
1408         mfsp            %sr1,t0  /* Save sr1 so we can use it in tlb inserts */
1409         mtsp            spc,%sr1
1410
1411         idtlba          pte,(%sr1,va)
1412         idtlbp          prot,(%sr1,va)
1413
1414         mtsp            t0, %sr1        /* Restore sr1 */
1415
1416         rfir
1417         nop
1418
1419 dtlb_miss_20:
1420         space_adjust    spc,va,t0
1421         get_pgd         spc,ptp
1422         space_check     spc,t0,dtlb_fault
1423
1424         L2_ptep         ptp,pte,t0,va,dtlb_check_alias_20
1425
1426         update_ptep     ptp,pte,t0,t1
1427
1428         make_insert_tlb spc,pte,prot
1429
1430         f_extend        pte,t0
1431
1432         idtlbt          pte,prot
1433
1434         rfir
1435         nop
1436
1437 dtlb_check_alias_20:
1438         do_alias        spc,t0,t1,va,pte,prot,dtlb_fault
1439         
1440         idtlbt          pte,prot
1441
1442         rfir
1443         nop
1444
1445 nadtlb_miss_20:
1446         get_pgd         spc,ptp
1447
1448         space_check     spc,t0,nadtlb_fault
1449
1450         L2_ptep         ptp,pte,t0,va,nadtlb_check_flush_20
1451
1452         update_ptep     ptp,pte,t0,t1
1453
1454         make_insert_tlb spc,pte,prot
1455
1456         f_extend        pte,t0
1457         
1458         idtlbt          pte,prot
1459
1460         rfir
1461         nop
1462
1463 nadtlb_check_flush_20:
1464         bb,>=,n          pte,_PAGE_FLUSH_BIT,nadtlb_emulate
1465
1466         /* Insert a "flush only" translation */
1467
1468         depdi,z         7,7,3,prot
1469         depdi           1,10,1,prot
1470
1471         /* Get rid of prot bits and convert to page addr for idtlbt */
1472
1473         depdi           0,63,12,pte
1474         extrd,u         pte,56,32,pte
1475         idtlbt          pte,prot
1476
1477         rfir
1478         nop
1479 #endif
1480
1481 nadtlb_emulate:
1482
1483         /*
1484          * Non access misses can be caused by fdc,fic,pdc,lpa,probe and
1485          * probei instructions. We don't want to fault for these
1486          * instructions (not only does it not make sense, it can cause
1487          * deadlocks, since some flushes are done with the mmap
1488          * semaphore held). If the translation doesn't exist, we can't
1489          * insert a translation, so have to emulate the side effects
1490          * of the instruction. Since we don't insert a translation
1491          * we can get a lot of faults during a flush loop, so it makes
1492          * sense to try to do it here with minimum overhead. We only
1493          * emulate fdc,fic,pdc,probew,prober instructions whose base 
1494          * and index registers are not shadowed. We defer everything 
1495          * else to the "slow" path.
1496          */
1497
1498         mfctl           %cr19,%r9 /* Get iir */
1499
1500         /* PA 2.0 Arch Ref. Book pg 382 has a good description of the insn bits.
1501            Checks for fdc,fdce,pdc,"fic,4f",prober,probeir,probew, probeiw */
1502
1503         /* Checks for fdc,fdce,pdc,"fic,4f" only */
1504         ldi             0x280,%r16
1505         and             %r9,%r16,%r17
1506         cmpb,<>,n       %r16,%r17,nadtlb_probe_check
1507         bb,>=,n         %r9,26,nadtlb_nullify  /* m bit not set, just nullify */
1508         BL              get_register,%r25
1509         extrw,u         %r9,15,5,%r8           /* Get index register # */
1510         CMPIB=,n        -1,%r1,nadtlb_fault    /* have to use slow path */
1511         copy            %r1,%r24
1512         BL              get_register,%r25
1513         extrw,u         %r9,10,5,%r8           /* Get base register # */
1514         CMPIB=,n        -1,%r1,nadtlb_fault    /* have to use slow path */
1515         BL              set_register,%r25
1516         add,l           %r1,%r24,%r1           /* doesn't affect c/b bits */
1517
1518 nadtlb_nullify:
1519         mfctl           %ipsw,%r8
1520         ldil            L%PSW_N,%r9
1521         or              %r8,%r9,%r8            /* Set PSW_N */
1522         mtctl           %r8,%ipsw
1523
1524         rfir
1525         nop
1526
1527         /* 
1528                 When there is no translation for the probe address then we
1529                 must nullify the insn and return zero in the target regsiter.
1530                 This will indicate to the calling code that it does not have 
1531                 write/read privileges to this address.
1532
1533                 This should technically work for prober and probew in PA 1.1,
1534                 and also probe,r and probe,w in PA 2.0
1535
1536                 WARNING: USE ONLY NON-SHADOW REGISTERS WITH PROBE INSN!
1537                 THE SLOW-PATH EMULATION HAS NOT BEEN WRITTEN YET.
1538
1539         */
1540 nadtlb_probe_check:
1541         ldi             0x80,%r16
1542         and             %r9,%r16,%r17
1543         cmpb,<>,n       %r16,%r17,nadtlb_fault /* Must be probe,[rw]*/
1544         BL              get_register,%r25      /* Find the target register */
1545         extrw,u         %r9,31,5,%r8           /* Get target register */
1546         CMPIB=,n        -1,%r1,nadtlb_fault    /* have to use slow path */
1547         BL              set_register,%r25
1548         copy            %r0,%r1                /* Write zero to target register */
1549         b nadtlb_nullify                       /* Nullify return insn */
1550         nop
1551
1552
1553 #ifdef CONFIG_64BIT
1554 itlb_miss_20w:
1555
1556         /*
1557          * I miss is a little different, since we allow users to fault
1558          * on the gateway page which is in the kernel address space.
1559          */
1560
1561         space_adjust    spc,va,t0
1562         get_pgd         spc,ptp
1563         space_check     spc,t0,itlb_fault
1564
1565         L3_ptep         ptp,pte,t0,va,itlb_fault
1566
1567         update_ptep     ptp,pte,t0,t1
1568
1569         make_insert_tlb spc,pte,prot
1570         
1571         iitlbt          pte,prot
1572
1573         rfir
1574         nop
1575
1576 #else
1577
1578 itlb_miss_11:
1579         get_pgd         spc,ptp
1580
1581         space_check     spc,t0,itlb_fault
1582
1583         L2_ptep         ptp,pte,t0,va,itlb_fault
1584
1585         update_ptep     ptp,pte,t0,t1
1586
1587         make_insert_tlb_11      spc,pte,prot
1588
1589         mfsp            %sr1,t0  /* Save sr1 so we can use it in tlb inserts */
1590         mtsp            spc,%sr1
1591
1592         iitlba          pte,(%sr1,va)
1593         iitlbp          prot,(%sr1,va)
1594
1595         mtsp            t0, %sr1        /* Restore sr1 */
1596
1597         rfir
1598         nop
1599
1600 itlb_miss_20:
1601         get_pgd         spc,ptp
1602
1603         space_check     spc,t0,itlb_fault
1604
1605         L2_ptep         ptp,pte,t0,va,itlb_fault
1606
1607         update_ptep     ptp,pte,t0,t1
1608
1609         make_insert_tlb spc,pte,prot
1610
1611         f_extend        pte,t0  
1612
1613         iitlbt          pte,prot
1614
1615         rfir
1616         nop
1617
1618 #endif
1619
1620 #ifdef CONFIG_64BIT
1621
1622 dbit_trap_20w:
1623         space_adjust    spc,va,t0
1624         get_pgd         spc,ptp
1625         space_check     spc,t0,dbit_fault
1626
1627         L3_ptep         ptp,pte,t0,va,dbit_fault
1628
1629 #ifdef CONFIG_SMP
1630         CMPIB=,n        0,spc,dbit_nolock_20w
1631         load32          PA(pa_dbit_lock),t0
1632
1633 dbit_spin_20w:
1634         LDCW            0(t0),t1
1635         cmpib,=         0,t1,dbit_spin_20w
1636         nop
1637
1638 dbit_nolock_20w:
1639 #endif
1640         update_dirty    ptp,pte,t1
1641
1642         make_insert_tlb spc,pte,prot
1643                 
1644         idtlbt          pte,prot
1645 #ifdef CONFIG_SMP
1646         CMPIB=,n        0,spc,dbit_nounlock_20w
1647         ldi             1,t1
1648         stw             t1,0(t0)
1649
1650 dbit_nounlock_20w:
1651 #endif
1652
1653         rfir
1654         nop
1655 #else
1656
1657 dbit_trap_11:
1658
1659         get_pgd         spc,ptp
1660
1661         space_check     spc,t0,dbit_fault
1662
1663         L2_ptep         ptp,pte,t0,va,dbit_fault
1664
1665 #ifdef CONFIG_SMP
1666         CMPIB=,n        0,spc,dbit_nolock_11
1667         load32          PA(pa_dbit_lock),t0
1668
1669 dbit_spin_11:
1670         LDCW            0(t0),t1
1671         cmpib,=         0,t1,dbit_spin_11
1672         nop
1673
1674 dbit_nolock_11:
1675 #endif
1676         update_dirty    ptp,pte,t1
1677
1678         make_insert_tlb_11      spc,pte,prot
1679
1680         mfsp            %sr1,t1  /* Save sr1 so we can use it in tlb inserts */
1681         mtsp            spc,%sr1
1682
1683         idtlba          pte,(%sr1,va)
1684         idtlbp          prot,(%sr1,va)
1685
1686         mtsp            t1, %sr1     /* Restore sr1 */
1687 #ifdef CONFIG_SMP
1688         CMPIB=,n        0,spc,dbit_nounlock_11
1689         ldi             1,t1
1690         stw             t1,0(t0)
1691
1692 dbit_nounlock_11:
1693 #endif
1694
1695         rfir
1696         nop
1697
1698 dbit_trap_20:
1699         get_pgd         spc,ptp
1700
1701         space_check     spc,t0,dbit_fault
1702
1703         L2_ptep         ptp,pte,t0,va,dbit_fault
1704
1705 #ifdef CONFIG_SMP
1706         CMPIB=,n        0,spc,dbit_nolock_20
1707         load32          PA(pa_dbit_lock),t0
1708
1709 dbit_spin_20:
1710         LDCW            0(t0),t1
1711         cmpib,=         0,t1,dbit_spin_20
1712         nop
1713
1714 dbit_nolock_20:
1715 #endif
1716         update_dirty    ptp,pte,t1
1717
1718         make_insert_tlb spc,pte,prot
1719
1720         f_extend        pte,t1
1721         
1722         idtlbt          pte,prot
1723
1724 #ifdef CONFIG_SMP
1725         CMPIB=,n        0,spc,dbit_nounlock_20
1726         ldi             1,t1
1727         stw             t1,0(t0)
1728
1729 dbit_nounlock_20:
1730 #endif
1731
1732         rfir
1733         nop
1734 #endif
1735
1736         .import handle_interruption,code
1737
1738 kernel_bad_space:
1739         b               intr_save
1740         ldi             31,%r8  /* Use an unused code */
1741
1742 dbit_fault:
1743         b               intr_save
1744         ldi             20,%r8
1745
1746 itlb_fault:
1747         b               intr_save
1748         ldi             6,%r8
1749
1750 nadtlb_fault:
1751         b               intr_save
1752         ldi             17,%r8
1753
1754 dtlb_fault:
1755         b               intr_save
1756         ldi             15,%r8
1757
1758         /* Register saving semantics for system calls:
1759
1760            %r1             clobbered by system call macro in userspace
1761            %r2             saved in PT_REGS by gateway page
1762            %r3  - %r18     preserved by C code (saved by signal code)
1763            %r19 - %r20     saved in PT_REGS by gateway page
1764            %r21 - %r22     non-standard syscall args
1765                            stored in kernel stack by gateway page
1766            %r23 - %r26     arg3-arg0, saved in PT_REGS by gateway page
1767            %r27 - %r30     saved in PT_REGS by gateway page
1768            %r31            syscall return pointer
1769          */
1770
1771         /* Floating point registers (FIXME: what do we do with these?)
1772
1773            %fr0  - %fr3    status/exception, not preserved
1774            %fr4  - %fr7    arguments
1775            %fr8  - %fr11   not preserved by C code
1776            %fr12 - %fr21   preserved by C code
1777            %fr22 - %fr31   not preserved by C code
1778          */
1779
1780         .macro  reg_save regs
1781         STREG   %r3, PT_GR3(\regs)
1782         STREG   %r4, PT_GR4(\regs)
1783         STREG   %r5, PT_GR5(\regs)
1784         STREG   %r6, PT_GR6(\regs)
1785         STREG   %r7, PT_GR7(\regs)
1786         STREG   %r8, PT_GR8(\regs)
1787         STREG   %r9, PT_GR9(\regs)
1788         STREG   %r10,PT_GR10(\regs)
1789         STREG   %r11,PT_GR11(\regs)
1790         STREG   %r12,PT_GR12(\regs)
1791         STREG   %r13,PT_GR13(\regs)
1792         STREG   %r14,PT_GR14(\regs)
1793         STREG   %r15,PT_GR15(\regs)
1794         STREG   %r16,PT_GR16(\regs)
1795         STREG   %r17,PT_GR17(\regs)
1796         STREG   %r18,PT_GR18(\regs)
1797         .endm
1798
1799         .macro  reg_restore regs
1800         LDREG   PT_GR3(\regs), %r3
1801         LDREG   PT_GR4(\regs), %r4
1802         LDREG   PT_GR5(\regs), %r5
1803         LDREG   PT_GR6(\regs), %r6
1804         LDREG   PT_GR7(\regs), %r7
1805         LDREG   PT_GR8(\regs), %r8
1806         LDREG   PT_GR9(\regs), %r9
1807         LDREG   PT_GR10(\regs),%r10
1808         LDREG   PT_GR11(\regs),%r11
1809         LDREG   PT_GR12(\regs),%r12
1810         LDREG   PT_GR13(\regs),%r13
1811         LDREG   PT_GR14(\regs),%r14
1812         LDREG   PT_GR15(\regs),%r15
1813         LDREG   PT_GR16(\regs),%r16
1814         LDREG   PT_GR17(\regs),%r17
1815         LDREG   PT_GR18(\regs),%r18
1816         .endm
1817
1818         .export sys_fork_wrapper
1819         .export child_return
1820 sys_fork_wrapper:
1821         LDREG   TI_TASK-THREAD_SZ_ALGN-FRAME_SIZE(%r30), %r1
1822         ldo     TASK_REGS(%r1),%r1
1823         reg_save %r1
1824         mfctl   %cr27, %r3
1825         STREG   %r3, PT_CR27(%r1)
1826
1827         STREG   %r2,-RP_OFFSET(%r30)
1828         ldo     FRAME_SIZE(%r30),%r30
1829 #ifdef CONFIG_64BIT
1830         ldo     -16(%r30),%r29          /* Reference param save area */
1831 #endif
1832
1833         /* These are call-clobbered registers and therefore
1834            also syscall-clobbered (we hope). */
1835         STREG   %r2,PT_GR19(%r1)        /* save for child */
1836         STREG   %r30,PT_GR21(%r1)
1837
1838         LDREG   PT_GR30(%r1),%r25
1839         copy    %r1,%r24
1840         BL      sys_clone,%r2
1841         ldi     SIGCHLD,%r26
1842
1843         LDREG   -RP_OFFSET-FRAME_SIZE(%r30),%r2
1844 wrapper_exit:
1845         ldo     -FRAME_SIZE(%r30),%r30          /* get the stackframe */
1846         LDREG   TI_TASK-THREAD_SZ_ALGN-FRAME_SIZE(%r30),%r1
1847         ldo     TASK_REGS(%r1),%r1       /* get pt regs */
1848
1849         LDREG   PT_CR27(%r1), %r3
1850         mtctl   %r3, %cr27
1851         reg_restore %r1
1852
1853         /* strace expects syscall # to be preserved in r20 */
1854         ldi     __NR_fork,%r20
1855         bv %r0(%r2)
1856         STREG   %r20,PT_GR20(%r1)
1857
1858         /* Set the return value for the child */
1859 child_return:
1860         BL      schedule_tail, %r2
1861         nop
1862
1863         LDREG   TI_TASK-THREAD_SZ_ALGN-FRAME_SIZE-FRAME_SIZE(%r30), %r1
1864         LDREG   TASK_PT_GR19(%r1),%r2
1865         b       wrapper_exit
1866         copy    %r0,%r28
1867
1868         
1869         .export sys_clone_wrapper
1870 sys_clone_wrapper:
1871         LDREG   TI_TASK-THREAD_SZ_ALGN-FRAME_SIZE(%r30),%r1
1872         ldo     TASK_REGS(%r1),%r1      /* get pt regs */
1873         reg_save %r1
1874         mfctl   %cr27, %r3
1875         STREG   %r3, PT_CR27(%r1)
1876
1877         STREG   %r2,-RP_OFFSET(%r30)
1878         ldo     FRAME_SIZE(%r30),%r30
1879 #ifdef CONFIG_64BIT
1880         ldo     -16(%r30),%r29          /* Reference param save area */
1881 #endif
1882
1883         /* WARNING - Clobbers r19 and r21, userspace must save these! */
1884         STREG   %r2,PT_GR19(%r1)        /* save for child */
1885         STREG   %r30,PT_GR21(%r1)
1886         BL      sys_clone,%r2
1887         copy    %r1,%r24
1888
1889         b       wrapper_exit
1890         LDREG   -RP_OFFSET-FRAME_SIZE(%r30),%r2
1891
1892         .export sys_vfork_wrapper
1893 sys_vfork_wrapper:
1894         LDREG   TI_TASK-THREAD_SZ_ALGN-FRAME_SIZE(%r30),%r1
1895         ldo     TASK_REGS(%r1),%r1      /* get pt regs */
1896         reg_save %r1
1897         mfctl   %cr27, %r3
1898         STREG   %r3, PT_CR27(%r1)
1899
1900         STREG   %r2,-RP_OFFSET(%r30)
1901         ldo     FRAME_SIZE(%r30),%r30
1902 #ifdef CONFIG_64BIT
1903         ldo     -16(%r30),%r29          /* Reference param save area */
1904 #endif
1905
1906         STREG   %r2,PT_GR19(%r1)        /* save for child */
1907         STREG   %r30,PT_GR21(%r1)
1908
1909         BL      sys_vfork,%r2
1910         copy    %r1,%r26
1911
1912         b       wrapper_exit
1913         LDREG   -RP_OFFSET-FRAME_SIZE(%r30),%r2
1914
1915         
1916         .macro  execve_wrapper execve
1917         LDREG   TI_TASK-THREAD_SZ_ALGN-FRAME_SIZE(%r30),%r1
1918         ldo     TASK_REGS(%r1),%r1      /* get pt regs */
1919
1920         /*
1921          * Do we need to save/restore r3-r18 here?
1922          * I don't think so. why would new thread need old
1923          * threads registers?
1924          */
1925
1926         /* %arg0 - %arg3 are already saved for us. */
1927
1928         STREG %r2,-RP_OFFSET(%r30)
1929         ldo FRAME_SIZE(%r30),%r30
1930 #ifdef CONFIG_64BIT
1931         ldo     -16(%r30),%r29          /* Reference param save area */
1932 #endif
1933         BL \execve,%r2
1934         copy %r1,%arg0
1935
1936         ldo -FRAME_SIZE(%r30),%r30
1937         LDREG -RP_OFFSET(%r30),%r2
1938
1939         /* If exec succeeded we need to load the args */
1940
1941         ldo -1024(%r0),%r1
1942         cmpb,>>= %r28,%r1,error_\execve
1943         copy %r2,%r19
1944
1945 error_\execve:
1946         bv %r0(%r19)
1947         nop
1948         .endm
1949
1950         .export sys_execve_wrapper
1951         .import sys_execve
1952
1953 sys_execve_wrapper:
1954         execve_wrapper sys_execve
1955
1956 #ifdef CONFIG_64BIT
1957         .export sys32_execve_wrapper
1958         .import sys32_execve
1959
1960 sys32_execve_wrapper:
1961         execve_wrapper sys32_execve
1962 #endif
1963
1964         .export sys_rt_sigreturn_wrapper
1965 sys_rt_sigreturn_wrapper:
1966         LDREG   TI_TASK-THREAD_SZ_ALGN-FRAME_SIZE(%r30),%r26
1967         ldo     TASK_REGS(%r26),%r26    /* get pt regs */
1968         /* Don't save regs, we are going to restore them from sigcontext. */
1969         STREG   %r2, -RP_OFFSET(%r30)
1970 #ifdef CONFIG_64BIT
1971         ldo     FRAME_SIZE(%r30), %r30
1972         BL      sys_rt_sigreturn,%r2
1973         ldo     -16(%r30),%r29          /* Reference param save area */
1974 #else
1975         BL      sys_rt_sigreturn,%r2
1976         ldo     FRAME_SIZE(%r30), %r30
1977 #endif
1978
1979         ldo     -FRAME_SIZE(%r30), %r30
1980         LDREG   -RP_OFFSET(%r30), %r2
1981
1982         /* FIXME: I think we need to restore a few more things here. */
1983         LDREG   TI_TASK-THREAD_SZ_ALGN-FRAME_SIZE(%r30),%r1
1984         ldo     TASK_REGS(%r1),%r1      /* get pt regs */
1985         reg_restore %r1
1986
1987         /* If the signal was received while the process was blocked on a
1988          * syscall, then r2 will take us to syscall_exit; otherwise r2 will
1989          * take us to syscall_exit_rfi and on to intr_return.
1990          */
1991         bv      %r0(%r2)
1992         LDREG   PT_GR28(%r1),%r28  /* reload original r28 for syscall_exit */
1993
1994         .export sys_sigaltstack_wrapper
1995 sys_sigaltstack_wrapper:
1996         /* Get the user stack pointer */
1997         LDREG   TI_TASK-THREAD_SZ_ALGN-FRAME_SIZE(%r30),%r1
1998         ldo     TASK_REGS(%r1),%r24     /* get pt regs */
1999         LDREG   TASK_PT_GR30(%r24),%r24
2000         STREG   %r2, -RP_OFFSET(%r30)
2001 #ifdef CONFIG_64BIT
2002         ldo     FRAME_SIZE(%r30), %r30
2003         b,l     do_sigaltstack,%r2
2004         ldo     -16(%r30),%r29          /* Reference param save area */
2005 #else
2006         bl      do_sigaltstack,%r2
2007         ldo     FRAME_SIZE(%r30), %r30
2008 #endif
2009
2010         ldo     -FRAME_SIZE(%r30), %r30
2011         LDREG   -RP_OFFSET(%r30), %r2
2012         bv      %r0(%r2)
2013         nop
2014
2015 #ifdef CONFIG_64BIT
2016         .export sys32_sigaltstack_wrapper
2017 sys32_sigaltstack_wrapper:
2018         /* Get the user stack pointer */
2019         LDREG   TI_TASK-THREAD_SZ_ALGN-FRAME_SIZE(%r30),%r24
2020         LDREG   TASK_PT_GR30(%r24),%r24
2021         STREG   %r2, -RP_OFFSET(%r30)
2022         ldo     FRAME_SIZE(%r30), %r30
2023         b,l     do_sigaltstack32,%r2
2024         ldo     -16(%r30),%r29          /* Reference param save area */
2025
2026         ldo     -FRAME_SIZE(%r30), %r30
2027         LDREG   -RP_OFFSET(%r30), %r2
2028         bv      %r0(%r2)
2029         nop
2030 #endif
2031
2032         .export sys_rt_sigsuspend_wrapper
2033 sys_rt_sigsuspend_wrapper:
2034         LDREG   TI_TASK-THREAD_SZ_ALGN-FRAME_SIZE(%r30), %r1
2035         ldo     TASK_REGS(%r1),%r24
2036         reg_save %r24
2037
2038         STREG   %r2, -RP_OFFSET(%r30)
2039 #ifdef CONFIG_64BIT
2040         ldo     FRAME_SIZE(%r30), %r30
2041         b,l     sys_rt_sigsuspend,%r2
2042         ldo     -16(%r30),%r29          /* Reference param save area */
2043 #else
2044         bl      sys_rt_sigsuspend,%r2
2045         ldo     FRAME_SIZE(%r30), %r30
2046 #endif
2047
2048         ldo     -FRAME_SIZE(%r30), %r30
2049         LDREG   -RP_OFFSET(%r30), %r2
2050
2051         LDREG   TI_TASK-THREAD_SZ_ALGN-FRAME_SIZE(%r30), %r1
2052         ldo     TASK_REGS(%r1),%r1
2053         reg_restore %r1
2054
2055         bv      %r0(%r2)
2056         nop
2057
2058         .export syscall_exit
2059 syscall_exit:
2060
2061         /* NOTE: HP-UX syscalls also come through here
2062          * after hpux_syscall_exit fixes up return
2063          * values. */
2064
2065         /* NOTE: Not all syscalls exit this way.  rt_sigreturn will exit
2066          * via syscall_exit_rfi if the signal was received while the process
2067          * was running.
2068          */
2069
2070         /* save return value now */
2071
2072         mfctl     %cr30, %r1
2073         LDREG     TI_TASK(%r1),%r1
2074         STREG     %r28,TASK_PT_GR28(%r1)
2075
2076 #ifdef CONFIG_HPUX
2077
2078 /* <linux/personality.h> cannot be easily included */
2079 #define PER_HPUX 0x10
2080         LDREG     TASK_PERSONALITY(%r1),%r19
2081
2082         /* We can't use "CMPIB<> PER_HPUX" since "im5" field is sign extended */
2083         ldo       -PER_HPUX(%r19), %r19
2084         CMPIB<>,n 0,%r19,1f
2085
2086         /* Save other hpux returns if personality is PER_HPUX */
2087         STREG     %r22,TASK_PT_GR22(%r1)
2088         STREG     %r29,TASK_PT_GR29(%r1)
2089 1:
2090
2091 #endif /* CONFIG_HPUX */
2092
2093         /* Seems to me that dp could be wrong here, if the syscall involved
2094          * calling a module, and nothing got round to restoring dp on return.
2095          */
2096         loadgp
2097
2098 syscall_check_bh:
2099
2100         /* Check for software interrupts */
2101
2102         .import irq_stat,data
2103
2104         load32  irq_stat,%r19
2105
2106 #ifdef CONFIG_SMP
2107         /* sched.h: int processor */
2108         /* %r26 is used as scratch register to index into irq_stat[] */
2109         ldw     TI_CPU-THREAD_SZ_ALGN-FRAME_SIZE(%r30),%r26 /* cpu # */
2110
2111         /* shift left ____cacheline_aligned (aka L1_CACHE_BYTES) bits */
2112         SHLREG  %r26,L1_CACHE_SHIFT,%r20
2113         add     %r19,%r20,%r19  /* now have &irq_stat[smp_processor_id()] */
2114 #endif /* CONFIG_SMP */
2115
2116 syscall_check_resched:
2117
2118         /* check for reschedule */
2119
2120         LDREG   TI_FLAGS-THREAD_SZ_ALGN-FRAME_SIZE(%r30),%r19   /* long */
2121         bb,<,n  %r19, 31-TIF_NEED_RESCHED, syscall_do_resched /* forward */
2122
2123 syscall_check_sig:
2124         LDREG   TI_FLAGS-THREAD_SZ_ALGN-FRAME_SIZE(%r30),%r19    /* get ti flags */
2125         bb,<,n  %r19, 31-TIF_SIGPENDING, syscall_do_signal /* forward */
2126
2127 syscall_restore:
2128         /* Are we being ptraced? */
2129         LDREG   TI_TASK-THREAD_SZ_ALGN-FRAME_SIZE(%r30),%r1
2130
2131         LDREG   TASK_PTRACE(%r1), %r19
2132         bb,<    %r19,31,syscall_restore_rfi
2133         nop
2134
2135         ldo     TASK_PT_FR31(%r1),%r19             /* reload fpregs */
2136         rest_fp %r19
2137
2138         LDREG   TASK_PT_SAR(%r1),%r19              /* restore SAR */
2139         mtsar   %r19
2140
2141         LDREG   TASK_PT_GR2(%r1),%r2               /* restore user rp */
2142         LDREG   TASK_PT_GR19(%r1),%r19
2143         LDREG   TASK_PT_GR20(%r1),%r20
2144         LDREG   TASK_PT_GR21(%r1),%r21
2145         LDREG   TASK_PT_GR22(%r1),%r22
2146         LDREG   TASK_PT_GR23(%r1),%r23
2147         LDREG   TASK_PT_GR24(%r1),%r24
2148         LDREG   TASK_PT_GR25(%r1),%r25
2149         LDREG   TASK_PT_GR26(%r1),%r26
2150         LDREG   TASK_PT_GR27(%r1),%r27     /* restore user dp */
2151         LDREG   TASK_PT_GR28(%r1),%r28     /* syscall return value */
2152         LDREG   TASK_PT_GR29(%r1),%r29
2153         LDREG   TASK_PT_GR31(%r1),%r31     /* restore syscall rp */
2154
2155         /* NOTE: We use rsm/ssm pair to make this operation atomic */
2156         rsm     PSW_SM_I, %r0
2157         LDREG   TASK_PT_GR30(%r1),%r30             /* restore user sp */
2158         mfsp    %sr3,%r1                           /* Get users space id */
2159         mtsp    %r1,%sr7                           /* Restore sr7 */
2160         ssm     PSW_SM_I, %r0
2161
2162         /* Set sr2 to zero for userspace syscalls to work. */
2163         mtsp    %r0,%sr2 
2164         mtsp    %r1,%sr4                           /* Restore sr4 */
2165         mtsp    %r1,%sr5                           /* Restore sr5 */
2166         mtsp    %r1,%sr6                           /* Restore sr6 */
2167
2168         depi    3,31,2,%r31                        /* ensure return to user mode. */
2169
2170 #ifdef CONFIG_64BIT
2171         /* decide whether to reset the wide mode bit
2172          *
2173          * For a syscall, the W bit is stored in the lowest bit
2174          * of sp.  Extract it and reset W if it is zero */
2175         extrd,u,*<>     %r30,63,1,%r1
2176         rsm     PSW_SM_W, %r0
2177         /* now reset the lowest bit of sp if it was set */
2178         xor     %r30,%r1,%r30
2179 #endif
2180         be,n    0(%sr3,%r31)                       /* return to user space */
2181
2182         /* We have to return via an RFI, so that PSW T and R bits can be set
2183          * appropriately.
2184          * This sets up pt_regs so we can return via intr_restore, which is not
2185          * the most efficient way of doing things, but it works.
2186          */
2187 syscall_restore_rfi:
2188         ldo     -1(%r0),%r2                        /* Set recovery cntr to -1 */
2189         mtctl   %r2,%cr0                           /*   for immediate trap */
2190         LDREG   TASK_PT_PSW(%r1),%r2               /* Get old PSW */
2191         ldi     0x0b,%r20                          /* Create new PSW */
2192         depi    -1,13,1,%r20                       /* C, Q, D, and I bits */
2193
2194         /* The values of PA_SINGLESTEP_BIT and PA_BLOCKSTEP_BIT are
2195          * set in include/linux/ptrace.h and converted to PA bitmap
2196          * numbers in asm-offsets.c */
2197
2198         /* if ((%r19.PA_SINGLESTEP_BIT)) { %r20.27=1} */
2199         extru,= %r19,PA_SINGLESTEP_BIT,1,%r0
2200         depi    -1,27,1,%r20                       /* R bit */
2201
2202         /* if ((%r19.PA_BLOCKSTEP_BIT)) { %r20.7=1} */
2203         extru,= %r19,PA_BLOCKSTEP_BIT,1,%r0
2204         depi    -1,7,1,%r20                        /* T bit */
2205
2206         STREG   %r20,TASK_PT_PSW(%r1)
2207
2208         /* Always store space registers, since sr3 can be changed (e.g. fork) */
2209
2210         mfsp    %sr3,%r25
2211         STREG   %r25,TASK_PT_SR3(%r1)
2212         STREG   %r25,TASK_PT_SR4(%r1)
2213         STREG   %r25,TASK_PT_SR5(%r1)
2214         STREG   %r25,TASK_PT_SR6(%r1)
2215         STREG   %r25,TASK_PT_SR7(%r1)
2216         STREG   %r25,TASK_PT_IASQ0(%r1)
2217         STREG   %r25,TASK_PT_IASQ1(%r1)
2218
2219         /* XXX W bit??? */
2220         /* Now if old D bit is clear, it means we didn't save all registers
2221          * on syscall entry, so do that now.  This only happens on TRACEME
2222          * calls, or if someone attached to us while we were on a syscall.
2223          * We could make this more efficient by not saving r3-r18, but
2224          * then we wouldn't be able to use the common intr_restore path.
2225          * It is only for traced processes anyway, so performance is not
2226          * an issue.
2227          */
2228         bb,<    %r2,30,pt_regs_ok                  /* Branch if D set */
2229         ldo     TASK_REGS(%r1),%r25
2230         reg_save %r25                              /* Save r3 to r18 */
2231
2232         /* Save the current sr */
2233         mfsp    %sr0,%r2
2234         STREG   %r2,TASK_PT_SR0(%r1)
2235
2236         /* Save the scratch sr */
2237         mfsp    %sr1,%r2
2238         STREG   %r2,TASK_PT_SR1(%r1)
2239
2240         /* sr2 should be set to zero for userspace syscalls */
2241         STREG   %r0,TASK_PT_SR2(%r1)
2242
2243 pt_regs_ok:
2244         LDREG   TASK_PT_GR31(%r1),%r2
2245         depi    3,31,2,%r2                         /* ensure return to user mode. */
2246         STREG   %r2,TASK_PT_IAOQ0(%r1)
2247         ldo     4(%r2),%r2
2248         STREG   %r2,TASK_PT_IAOQ1(%r1)
2249         copy    %r25,%r16
2250         b       intr_restore
2251         nop
2252
2253         .import schedule,code
2254 syscall_do_resched:
2255         BL      schedule,%r2
2256 #ifdef CONFIG_64BIT
2257         ldo     -16(%r30),%r29          /* Reference param save area */
2258 #else
2259         nop
2260 #endif
2261         b       syscall_check_bh  /* if resched, we start over again */
2262         nop
2263
2264         .import do_signal,code
2265 syscall_do_signal:
2266         /* Save callee-save registers (for sigcontext).
2267            FIXME: After this point the process structure should be
2268            consistent with all the relevant state of the process
2269            before the syscall.  We need to verify this. */
2270         LDREG   TI_TASK-THREAD_SZ_ALGN-FRAME_SIZE(%r30),%r1 
2271         ldo     TASK_REGS(%r1), %r25            /* struct pt_regs *regs */
2272         reg_save %r25
2273
2274         ldi     1, %r24                         /* unsigned long in_syscall */
2275
2276 #ifdef CONFIG_64BIT
2277         ldo     -16(%r30),%r29                  /* Reference param save area */
2278 #endif
2279         BL      do_signal,%r2
2280         copy    %r0, %r26                       /* sigset_t *oldset = NULL */
2281
2282         LDREG   TI_TASK-THREAD_SZ_ALGN-FRAME_SIZE(%r30),%r1
2283         ldo     TASK_REGS(%r1), %r20            /* reload pt_regs */
2284         reg_restore %r20
2285
2286         b,n     syscall_check_sig
2287
2288         /*
2289          * get_register is used by the non access tlb miss handlers to
2290          * copy the value of the general register specified in r8 into
2291          * r1. This routine can't be used for shadowed registers, since
2292          * the rfir will restore the original value. So, for the shadowed
2293          * registers we put a -1 into r1 to indicate that the register
2294          * should not be used (the register being copied could also have
2295          * a -1 in it, but that is OK, it just means that we will have
2296          * to use the slow path instead).
2297          */
2298
2299 get_register:
2300         blr     %r8,%r0
2301         nop
2302         bv      %r0(%r25)    /* r0 */
2303         copy    %r0,%r1
2304         bv      %r0(%r25)    /* r1 - shadowed */
2305         ldi     -1,%r1
2306         bv      %r0(%r25)    /* r2 */
2307         copy    %r2,%r1
2308         bv      %r0(%r25)    /* r3 */
2309         copy    %r3,%r1
2310         bv      %r0(%r25)    /* r4 */
2311         copy    %r4,%r1
2312         bv      %r0(%r25)    /* r5 */
2313         copy    %r5,%r1
2314         bv      %r0(%r25)    /* r6 */
2315         copy    %r6,%r1
2316         bv      %r0(%r25)    /* r7 */
2317         copy    %r7,%r1
2318         bv      %r0(%r25)    /* r8 - shadowed */
2319         ldi     -1,%r1
2320         bv      %r0(%r25)    /* r9 - shadowed */
2321         ldi     -1,%r1
2322         bv      %r0(%r25)    /* r10 */
2323         copy    %r10,%r1
2324         bv      %r0(%r25)    /* r11 */
2325         copy    %r11,%r1
2326         bv      %r0(%r25)    /* r12 */
2327         copy    %r12,%r1
2328         bv      %r0(%r25)    /* r13 */
2329         copy    %r13,%r1
2330         bv      %r0(%r25)    /* r14 */
2331         copy    %r14,%r1
2332         bv      %r0(%r25)    /* r15 */
2333         copy    %r15,%r1
2334         bv      %r0(%r25)    /* r16 - shadowed */
2335         ldi     -1,%r1
2336         bv      %r0(%r25)    /* r17 - shadowed */
2337         ldi     -1,%r1
2338         bv      %r0(%r25)    /* r18 */
2339         copy    %r18,%r1
2340         bv      %r0(%r25)    /* r19 */
2341         copy    %r19,%r1
2342         bv      %r0(%r25)    /* r20 */
2343         copy    %r20,%r1
2344         bv      %r0(%r25)    /* r21 */
2345         copy    %r21,%r1
2346         bv      %r0(%r25)    /* r22 */
2347         copy    %r22,%r1
2348         bv      %r0(%r25)    /* r23 */
2349         copy    %r23,%r1
2350         bv      %r0(%r25)    /* r24 - shadowed */
2351         ldi     -1,%r1
2352         bv      %r0(%r25)    /* r25 - shadowed */
2353         ldi     -1,%r1
2354         bv      %r0(%r25)    /* r26 */
2355         copy    %r26,%r1
2356         bv      %r0(%r25)    /* r27 */
2357         copy    %r27,%r1
2358         bv      %r0(%r25)    /* r28 */
2359         copy    %r28,%r1
2360         bv      %r0(%r25)    /* r29 */
2361         copy    %r29,%r1
2362         bv      %r0(%r25)    /* r30 */
2363         copy    %r30,%r1
2364         bv      %r0(%r25)    /* r31 */
2365         copy    %r31,%r1
2366
2367         /*
2368          * set_register is used by the non access tlb miss handlers to
2369          * copy the value of r1 into the general register specified in
2370          * r8.
2371          */
2372
2373 set_register:
2374         blr     %r8,%r0
2375         nop
2376         bv      %r0(%r25)    /* r0 (silly, but it is a place holder) */
2377         copy    %r1,%r0
2378         bv      %r0(%r25)    /* r1 */
2379         copy    %r1,%r1
2380         bv      %r0(%r25)    /* r2 */
2381         copy    %r1,%r2
2382         bv      %r0(%r25)    /* r3 */
2383         copy    %r1,%r3
2384         bv      %r0(%r25)    /* r4 */
2385         copy    %r1,%r4
2386         bv      %r0(%r25)    /* r5 */
2387         copy    %r1,%r5
2388         bv      %r0(%r25)    /* r6 */
2389         copy    %r1,%r6
2390         bv      %r0(%r25)    /* r7 */
2391         copy    %r1,%r7
2392         bv      %r0(%r25)    /* r8 */
2393         copy    %r1,%r8
2394         bv      %r0(%r25)    /* r9 */
2395         copy    %r1,%r9
2396         bv      %r0(%r25)    /* r10 */
2397         copy    %r1,%r10
2398         bv      %r0(%r25)    /* r11 */
2399         copy    %r1,%r11
2400         bv      %r0(%r25)    /* r12 */
2401         copy    %r1,%r12
2402         bv      %r0(%r25)    /* r13 */
2403         copy    %r1,%r13
2404         bv      %r0(%r25)    /* r14 */
2405         copy    %r1,%r14
2406         bv      %r0(%r25)    /* r15 */
2407         copy    %r1,%r15
2408         bv      %r0(%r25)    /* r16 */
2409         copy    %r1,%r16
2410         bv      %r0(%r25)    /* r17 */
2411         copy    %r1,%r17
2412         bv      %r0(%r25)    /* r18 */
2413         copy    %r1,%r18
2414         bv      %r0(%r25)    /* r19 */
2415         copy    %r1,%r19
2416         bv      %r0(%r25)    /* r20 */
2417         copy    %r1,%r20
2418         bv      %r0(%r25)    /* r21 */
2419         copy    %r1,%r21
2420         bv      %r0(%r25)    /* r22 */
2421         copy    %r1,%r22
2422         bv      %r0(%r25)    /* r23 */
2423         copy    %r1,%r23
2424         bv      %r0(%r25)    /* r24 */
2425         copy    %r1,%r24
2426         bv      %r0(%r25)    /* r25 */
2427         copy    %r1,%r25
2428         bv      %r0(%r25)    /* r26 */
2429         copy    %r1,%r26
2430         bv      %r0(%r25)    /* r27 */
2431         copy    %r1,%r27
2432         bv      %r0(%r25)    /* r28 */
2433         copy    %r1,%r28
2434         bv      %r0(%r25)    /* r29 */
2435         copy    %r1,%r29
2436         bv      %r0(%r25)    /* r30 */
2437         copy    %r1,%r30
2438         bv      %r0(%r25)    /* r31 */
2439         copy    %r1,%r31