1 /* $OpenBSD: locore.S,v 1.18 1998/09/15 10:58:53 pefo Exp $ */
3 * Copyright (c) 1992, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * Digital Equipment Corporation and Ralph Campbell.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Copyright (C) 1989 Digital Equipment Corporation.
34 * Permission to use, copy, modify, and distribute this software and
35 * its documentation for any purpose and without fee is hereby granted,
36 * provided that the above copyright notice appears in all copies.
37 * Digital Equipment Corporation makes no representations about the
38 * suitability of this software for any purpose. It is provided "as is"
39 * without express or implied warranty.
41 * from: Header: /sprite/src/kernel/mach/ds3100.md/RCS/loMem.s,
42 * v 1.1 89/07/11 17:55:04 nelson Exp SPRITE (DECWRL)
43 * from: Header: /sprite/src/kernel/mach/ds3100.md/RCS/machAsm.s,
44 * v 9.2 90/01/29 18:00:39 shirriff Exp SPRITE (DECWRL)
45 * from: Header: /sprite/src/kernel/vm/ds3100.md/vmPmaxAsm.s,
46 * v 1.1 89/07/10 14:27:41 nelson Exp SPRITE (DECWRL)
48 * from: @(#)locore.s 8.5 (Berkeley) 1/4/94
49 * JNPR: support.S,v 1.5.2.2 2007/08/29 10:03:49 girish
54 * Copyright (c) 1997 Jonathan Stone (hereinafter referred to as the author)
55 * All rights reserved.
57 * Redistribution and use in source and binary forms, with or without
58 * modification, are permitted provided that the following conditions
60 * 1. Redistributions of source code must retain the above copyright
61 * notice, this list of conditions and the following disclaimer.
62 * 2. Redistributions in binary form must reproduce the above copyright
63 * notice, this list of conditions and the following disclaimer in the
64 * documentation and/or other materials provided with the distribution.
65 * 3. All advertising materials mentioning features or use of this software
66 * must display the following acknowledgement:
67 * This product includes software developed by Jonathan R. Stone for
69 * 4. The name of the author may not be used to endorse or promote products
70 * derived from this software without specific prior written permission.
72 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
73 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
74 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
75 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
76 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
77 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
78 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
79 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
80 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
81 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
86 * Contains assembly language support routines.
90 #include <sys/errno.h>
91 #include <machine/asm.h>
92 #include <machine/cpu.h>
93 #include <machine/regnum.h>
94 #include <machine/cpuregs.h>
95 #include <machine/pcb.h>
99 .set noreorder # Noreorder is default style!
108 * int copystr(void *kfaddr, void *kdaddr, size_t maxlen, size_t *lencopied)
109 * Copy a NIL-terminated string, at most maxlen characters long. Return the
110 * number of characters copied (including the NIL) in *lencopied. If the
111 * string is too long, return ENAMETOOLONG; else return 0.
120 sb v0, 0(a1) # each byte until NIL
122 bne a2, zero, 1b # less than maxlen
125 li v0, ENAMETOOLONG # run out of space
127 beq a3, zero, 3f # return num. of copied bytes
128 PTR_SUBU a2, t0, a2 # if the 4th arg was non-NULL
131 j ra # v0 is 0 or ENAMETOOLONG
137 * Copy a null terminated string from the user address space into
138 * the kernel address space.
140 * copyinstr(fromaddr, toaddr, maxlength, &lencopied)
146 NESTED(copyinstr, CALLFRAME_SIZ, ra)
147 PTR_SUBU sp, sp, CALLFRAME_SIZ
148 .mask 0x80000000, (CALLFRAME_RA - CALLFRAME_SIZ)
150 blt a0, zero, _C_LABEL(copyerr) # make sure address is in user space
151 REG_S ra, CALLFRAME_RA(sp)
153 PTR_L v1, PC_CURPCB(v1)
154 jal _C_LABEL(copystr)
155 PTR_S v0, U_PCB_ONFAULT(v1)
156 REG_L ra, CALLFRAME_RA(sp)
158 PTR_L v1, PC_CURPCB(v1)
159 PTR_S zero, U_PCB_ONFAULT(v1)
161 PTR_ADDU sp, sp, CALLFRAME_SIZ
165 * Copy specified amount of data from user space into the kernel
166 * copyin(from, to, len)
167 * caddr_t *from; (user source address)
168 * caddr_t *to; (kernel destination address)
171 NESTED(copyin, CALLFRAME_SIZ, ra)
172 PTR_SUBU sp, sp, CALLFRAME_SIZ
173 .mask 0x80000000, (CALLFRAME_RA - CALLFRAME_SIZ)
175 blt a0, zero, _C_LABEL(copyerr) # make sure address is in user space
176 REG_S ra, CALLFRAME_RA(sp)
178 PTR_L v1, PC_CURPCB(v1)
180 PTR_S v0, U_PCB_ONFAULT(v1)
181 REG_L ra, CALLFRAME_RA(sp)
183 PTR_L v1, PC_CURPCB(v1) # bcopy modified v1, so reload
184 PTR_S zero, U_PCB_ONFAULT(v1)
185 PTR_ADDU sp, sp, CALLFRAME_SIZ
191 * Copy specified amount of data from kernel to the user space
192 * copyout(from, to, len)
193 * caddr_t *from; (kernel source address)
194 * caddr_t *to; (user destination address)
197 NESTED(copyout, CALLFRAME_SIZ, ra)
198 PTR_SUBU sp, sp, CALLFRAME_SIZ
199 .mask 0x80000000, (CALLFRAME_RA - CALLFRAME_SIZ)
201 blt a1, zero, _C_LABEL(copyerr) # make sure address is in user space
202 REG_S ra, CALLFRAME_RA(sp)
204 PTR_L v1, PC_CURPCB(v1)
206 PTR_S v0, U_PCB_ONFAULT(v1)
207 REG_L ra, CALLFRAME_RA(sp)
209 PTR_L v1, PC_CURPCB(v1) # bcopy modified v1, so reload
210 PTR_S zero, U_PCB_ONFAULT(v1)
211 PTR_ADDU sp, sp, CALLFRAME_SIZ
217 REG_L ra, CALLFRAME_RA(sp)
218 PTR_ADDU sp, sp, CALLFRAME_SIZ
220 li v0, EFAULT # return error
224 * {fu,su},{byte,sword,word}, fetch or store a byte, short or word to
231 blt a0, zero, fswberr # make sure address is in user space
234 PTR_L v1, PC_CURPCB(v1)
235 PTR_S v0, U_PCB_ONFAULT(v1)
236 ld v0, 0(a0) # fetch word
237 PTR_S zero, U_PCB_ONFAULT(v1)
238 sd v0, 0(a1) # store word
249 blt a0, zero, fswberr # make sure address is in user space
252 PTR_L v1, PC_CURPCB(v1)
253 PTR_S v0, U_PCB_ONFAULT(v1)
254 lw v0, 0(a0) # fetch word
255 PTR_S zero, U_PCB_ONFAULT(v1)
256 sw v0, 0(a1) # store word
263 blt a0, zero, fswberr # make sure address is in user space
266 PTR_L v1, PC_CURPCB(v1)
267 PTR_S v0, U_PCB_ONFAULT(v1)
268 lhu v0, 0(a0) # fetch short
269 PTR_S zero, U_PCB_ONFAULT(v1)
270 sh v0, 0(a1) # store short
277 blt a0, zero, fswberr # make sure address is in user space
280 PTR_L v1, PC_CURPCB(v1)
281 PTR_S v0, U_PCB_ONFAULT(v1)
282 lbu v0, 0(a0) # fetch byte
284 PTR_S zero, U_PCB_ONFAULT(v1)
292 blt a0, zero, fswberr # make sure address is in user space
295 PTR_L v1, PC_CURPCB(v1)
296 PTR_S v0, U_PCB_ONFAULT(v1)
297 sw a1, 0(a0) # store word
298 PTR_S zero, U_PCB_ONFAULT(v1)
307 blt a0, zero, fswberr # make sure address is in user space
310 PTR_L v1, PC_CURPCB(v1)
311 PTR_S v0, U_PCB_ONFAULT(v1)
312 sd a1, 0(a0) # store word
313 PTR_S zero, U_PCB_ONFAULT(v1)
321 * <v0>u_long casueword(<a0>u_long *p, <a1>u_long oldval, <a2>u_long *oldval_p,
326 * <v0>uint32_t casueword(<a0>uint32_t *p, <a1>uint32_t oldval,
327 * <a2>uint32_t newval)
334 blt a0, zero, fswberr # make sure address is in user space
337 PTR_L v1, PC_CURPCB(v1)
338 PTR_S v0, U_PCB_ONFAULT(v1)
344 sc t0, 0(a0) # store word
352 PTR_S zero, U_PCB_ONFAULT(v1)
354 sw t1, 0(a2) # unconditionally store old word
361 blt a0, zero, fswberr # make sure address is in user space
364 PTR_L v1, PC_CURPCB(v1)
365 PTR_S v0, U_PCB_ONFAULT(v1)
371 scd t0, 0(a0) # store double word
379 PTR_S zero, U_PCB_ONFAULT(v1)
381 sd t1, 0(a2) # unconditionally store old word
386 * Will have to flush the instruction cache if byte merging is done in hardware.
390 blt a0, zero, fswberr # make sure address is in user space
393 PTR_L v1, PC_CURPCB(v1)
394 PTR_S v0, U_PCB_ONFAULT(v1)
395 sh a1, 0(a0) # store short
396 PTR_S zero, U_PCB_ONFAULT(v1)
403 blt a0, zero, fswberr # make sure address is in user space
406 PTR_L v1, PC_CURPCB(v1)
407 PTR_S v0, U_PCB_ONFAULT(v1)
408 sb a1, 0(a0) # store byte
409 PTR_S zero, U_PCB_ONFAULT(v1)
420 * memset(void *s1, int c, int len)
421 * NetBSD: memset.S,v 1.3 2001/10/16 15:40:53 uch Exp
425 blt a2, 12, memsetsmallclr # small amount to clear?
426 move v0, a0 # save s1 for result
428 sll t1, a1, 8 # compute c << 8 in t1
429 or t1, t1, a1 # compute c << 8 | c in 11
430 sll t2, t1, 16 # shift that left 16
431 or t1, t2, t1 # or together
433 PTR_SUBU t0, zero, a0 # compute # bytes to word align address
435 beq t0, zero, 1f # skip if word aligned
436 PTR_SUBU a2, a2, t0 # subtract from remaining count
437 SWHI t1, 0(a0) # store 1, 2, or 3 bytes to align
440 and v1, a2, 3 # compute number of whole words left
443 PTR_ADDU t0, t0, a0 # compute ending address
445 PTR_ADDU a0, a0, 4 # clear words
446 bne a0, t0, 2b # unrolling loop does not help
447 sw t1, -4(a0) # since we are limited by memory speed
451 PTR_ADDU t0, a2, a0 # compute ending address
453 PTR_ADDU a0, a0, 1 # clear bytes
468 blt a1, 12, smallclr # small amount to clear?
469 PTR_SUBU a3, zero, a0 # compute # bytes to word align address
471 beq a3, zero, 1f # skip if word aligned
472 PTR_SUBU a1, a1, a3 # subtract from remaining count
473 SWHI zero, 0(a0) # clear 1, 2, or 3 bytes to align
476 and v0, a1, 3 # compute number of words left
479 PTR_ADDU a3, a3, a0 # compute ending address
481 PTR_ADDU a0, a0, 4 # clear words
482 bne a0, a3, 2b # unrolling loop does not help
483 sw zero, -4(a0) # since we are limited by memory speed
486 PTR_ADDU a3, a1, a0 # compute ending address
488 PTR_ADDU a0, a0, 1 # clear bytes
502 blt a2, 16, smallcmp # is it worth any trouble?
503 xor v0, a0, a1 # compare low two bits of addresses
505 PTR_SUBU a3, zero, a1 # compute # bytes to word align address
506 bne v0, zero, unalignedcmp # not possible to align addresses
510 PTR_SUBU a2, a2, a3 # subtract from remaining count
511 move v0, v1 # init v0,v1 so unmodified bytes match
512 LWHI v0, 0(a0) # read 1, 2, or 3 bytes
518 and a3, a2, ~3 # compute number of whole words left
519 PTR_SUBU a2, a2, a3 # which has to be >= (16-3) & ~3
520 PTR_ADDU a3, a3, a0 # compute ending address
522 lw v0, 0(a0) # compare words
529 b smallcmp # finish remainder
533 PTR_SUBU a2, a2, a3 # subtract from remaining count
534 PTR_ADDU a3, a3, a0 # compute ending address
536 lbu v0, 0(a0) # compare bytes until a1 word aligned
544 and a3, a2, ~3 # compute number of whole words left
545 PTR_SUBU a2, a2, a3 # which has to be >= (16-3) & ~3
546 PTR_ADDU a3, a3, a0 # compute ending address
548 LWHI v0, 0(a0) # compare words a0 unaligned, a1 aligned
558 PTR_ADDU a3, a2, a0 # compute ending address
584 and v1, a0, 1 # bit set?
586 beq v1, zero, 1b # no, continue
595 * atomic_set_16(u_int16_t *a, u_int16_t b)
602 srl a0, a0, 2 # round down address to be 32-bit aligned
617 * atomic_clear_16(u_int16_t *a, u_int16_t b)
622 LEAF(atomic_clear_16)
624 srl a0, a0, 2 # round down address to be 32-bit aligned
630 andi t1, t1, 0xffff # t1 has the original lower 16 bits
631 and t1, t1, a1 # t1 has the new lower 16 bits
632 srl t0, t0, 16 # preserve original top 16 bits
645 * atomic_subtract_16(uint16_t *a, uint16_t b)
650 LEAF(atomic_subtract_16)
652 srl a0, a0, 2 # round down address to be 32-bit aligned
657 andi t1, t1, 0xffff # t1 has the original lower 16 bits
659 andi t1, t1, 0xffff # t1 has the new lower 16 bits
660 srl t0, t0, 16 # preserve original top 16 bits
668 END(atomic_subtract_16)
672 * atomic_add_16(uint16_t *a, uint16_t b)
679 srl a0, a0, 2 # round down address to be 32-bit aligned
684 andi t1, t1, 0xffff # t1 has the original lower 16 bits
686 andi t1, t1, 0xffff # t1 has the new lower 16 bits
687 srl t0, t0, 16 # preserve original top 16 bits
699 * atomic_add_8(uint8_t *a, uint8_t b)
706 srl a0, a0, 2 # round down address to be 32-bit aligned
711 andi t1, t1, 0xff # t1 has the original lower 8 bits
713 andi t1, t1, 0xff # t1 has the new lower 8 bits
714 srl t0, t0, 8 # preserve original top 24 bits
727 * atomic_subtract_8(uint8_t *a, uint8_t b)
732 LEAF(atomic_subtract_8)
734 srl a0, a0, 2 # round down address to be 32-bit aligned
739 andi t1, t1, 0xff # t1 has the original lower 8 bits
741 andi t1, t1, 0xff # t1 has the new lower 8 bits
742 srl t0, t0, 8 # preserve original top 24 bits
750 END(atomic_subtract_8)
752 .set noreorder # Noreorder is default style!
754 #if defined(DDB) || defined(DEBUG)
758 and v0, a0, 3 # unaligned ?
760 PTR_L t1, PC_CURPCB(t1)
762 PTR_S v1, U_PCB_ONFAULT(t1)
766 PTR_S zero, U_PCB_ONFAULT(t1)
772 PTR_S zero, U_PCB_ONFAULT(t1)
777 and v0, a0, 3 # unaligned ?
779 PTR_L t1, PC_CURPCB(t1)
781 PTR_S v1, U_PCB_ONFAULT(t1)
785 PTR_S zero, U_PCB_ONFAULT(t1)
791 PTR_S zero, U_PCB_ONFAULT(t1)
801 and v0, a0, 3 # unaligned ?
803 PTR_L t1, PC_CURPCB(t1)
805 PTR_S v1, U_PCB_ONFAULT(t1)
809 PTR_S zero, U_PCB_ONFAULT(t1)
815 PTR_S zero, U_PCB_ONFAULT(t1)
823 #endif /* DDB || DEBUG */
827 break MIPS_BREAK_SOVER_VAL
833 mfc0 v0, MIPS_COP_0_STATUS # Later the "real" spl value!
834 REG_S s0, (SZREG * PCB_REG_S0)(a0)
835 REG_S s1, (SZREG * PCB_REG_S1)(a0)
836 REG_S s2, (SZREG * PCB_REG_S2)(a0)
837 REG_S s3, (SZREG * PCB_REG_S3)(a0)
838 REG_S s4, (SZREG * PCB_REG_S4)(a0)
839 REG_S s5, (SZREG * PCB_REG_S5)(a0)
840 REG_S s6, (SZREG * PCB_REG_S6)(a0)
841 REG_S s7, (SZREG * PCB_REG_S7)(a0)
842 REG_S s8, (SZREG * PCB_REG_S8)(a0)
843 REG_S sp, (SZREG * PCB_REG_SP)(a0)
844 REG_S ra, (SZREG * PCB_REG_RA)(a0)
845 REG_S v0, (SZREG * PCB_REG_SR)(a0)
847 li v0, 0 # setjmp return
851 REG_L v0, (SZREG * PCB_REG_SR)(a0)
852 REG_L ra, (SZREG * PCB_REG_RA)(a0)
853 REG_L s0, (SZREG * PCB_REG_S0)(a0)
854 REG_L s1, (SZREG * PCB_REG_S1)(a0)
855 REG_L s2, (SZREG * PCB_REG_S2)(a0)
856 REG_L s3, (SZREG * PCB_REG_S3)(a0)
857 REG_L s4, (SZREG * PCB_REG_S4)(a0)
858 REG_L s5, (SZREG * PCB_REG_S5)(a0)
859 REG_L s6, (SZREG * PCB_REG_S6)(a0)
860 REG_L s7, (SZREG * PCB_REG_S7)(a0)
861 REG_L s8, (SZREG * PCB_REG_S8)(a0)
862 REG_L sp, (SZREG * PCB_REG_SP)(a0)
863 mtc0 v0, MIPS_COP_0_STATUS # Later the "real" spl value!
866 li v0, 1 # longjmp return
873 #if defined(__mips_o32)
874 mfc0 t0, MIPS_COP_0_STATUS # turn off interrupts
875 and t1, t0, ~(MIPS_SR_INT_IE)
876 mtc0 t1, MIPS_COP_0_STATUS
883 #if _BYTE_ORDER == _BIG_ENDIAN
885 dsra v1, v1, 32 # low word in v1
886 dsra v0, v0, 32 # high word in v0
888 dsra v1, v0, 32 # high word in v1
890 dsra v0, v0, 32 # low word in v0
893 mtc0 t0, MIPS_COP_0_STATUS # restore intr status.
896 #else /* !__mips_o32 */
898 #endif /* !__mips_o32 */
909 #if defined(__mips_o32)
910 mfc0 t0, MIPS_COP_0_STATUS # turn off interrupts
911 and t1, t0, ~(MIPS_SR_INT_IE)
912 mtc0 t1, MIPS_COP_0_STATUS
918 # NOTE: a1 is padding!
920 #if _BYTE_ORDER == _BIG_ENDIAN
921 dsll a2, a2, 32 # high word in a2
922 dsll a3, a3, 32 # low word in a3
925 dsll a2, a2, 32 # low word in a2
927 dsll a3, a3, 32 # high word in a3
932 mtc0 t0, MIPS_COP_0_STATUS # restore intr status.
935 #else /* !__mips_o32 */
937 #endif /* !__mips_o32 */