blob: 0d94844eb21839113eaf38252c4c77efba715ef8 [file] [log] [blame]
Chris Metcalf18aecc22011-05-04 14:38:26 -04001/*
2 * Copyright 2011 Tilera Corporation. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation, version 2.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11 * NON INFRINGEMENT. See the GNU General Public License for
12 * more details.
13 */
14
15#include <linux/linkage.h>
16#include <asm/errno.h>
17#include <asm/cache.h>
18#include <arch/chip.h>
19
20/* Access user memory, but use MMU to avoid propagating kernel exceptions. */
21
Chris Metcalf18aecc22011-05-04 14:38:26 -040022/*
23 * strnlen_user_asm takes the pointer in r0, and the length bound in r1.
24 * It returns the length, including the terminating NUL, or zero on exception.
25 * If length is greater than the bound, returns one plus the bound.
26 */
27STD_ENTRY(strnlen_user_asm)
28 { beqz r1, 2f; addi r3, r0, -1 } /* bias down to include NUL */
291: { ld1u r4, r0; addi r1, r1, -1 }
30 beqz r4, 2f
31 { bnezt r1, 1b; addi r0, r0, 1 }
322: { sub r0, r0, r3; jrp lr }
33 STD_ENDPROC(strnlen_user_asm)
34 .pushsection .fixup,"ax"
35strnlen_user_fault:
36 { move r0, zero; jrp lr }
37 ENDPROC(strnlen_user_fault)
38 .section __ex_table,"a"
Chris Metcalfd4d9eab2013-08-09 15:38:43 -040039 .align 8
Chris Metcalf18aecc22011-05-04 14:38:26 -040040 .quad 1b, strnlen_user_fault
41 .popsection
42
43/*
44 * strncpy_from_user_asm takes the kernel target pointer in r0,
45 * the userspace source pointer in r1, and the length bound (including
46 * the trailing NUL) in r2. On success, it returns the string length
47 * (not including the trailing NUL), or -EFAULT on failure.
48 */
49STD_ENTRY(strncpy_from_user_asm)
50 { beqz r2, 2f; move r3, r0 }
511: { ld1u r4, r1; addi r1, r1, 1; addi r2, r2, -1 }
52 { st1 r0, r4; addi r0, r0, 1 }
53 beqz r2, 2f
54 bnezt r4, 1b
55 addi r0, r0, -1 /* don't count the trailing NUL */
562: { sub r0, r0, r3; jrp lr }
57 STD_ENDPROC(strncpy_from_user_asm)
58 .pushsection .fixup,"ax"
59strncpy_from_user_fault:
60 { movei r0, -EFAULT; jrp lr }
61 ENDPROC(strncpy_from_user_fault)
62 .section __ex_table,"a"
Chris Metcalfd4d9eab2013-08-09 15:38:43 -040063 .align 8
Chris Metcalf18aecc22011-05-04 14:38:26 -040064 .quad 1b, strncpy_from_user_fault
65 .popsection
66
67/*
68 * clear_user_asm takes the user target address in r0 and the
69 * number of bytes to zero in r1.
70 * It returns the number of uncopiable bytes (hopefully zero) in r0.
71 * Note that we don't use a separate .fixup section here since we fall
72 * through into the "fixup" code as the last straight-line bundle anyway.
73 */
74STD_ENTRY(clear_user_asm)
75 { beqz r1, 2f; or r2, r0, r1 }
76 andi r2, r2, 7
77 beqzt r2, .Lclear_aligned_user_asm
781: { st1 r0, zero; addi r0, r0, 1; addi r1, r1, -1 }
79 bnezt r1, 1b
802: { move r0, r1; jrp lr }
81 .pushsection __ex_table,"a"
Chris Metcalfd4d9eab2013-08-09 15:38:43 -040082 .align 8
Chris Metcalf18aecc22011-05-04 14:38:26 -040083 .quad 1b, 2b
84 .popsection
85
86.Lclear_aligned_user_asm:
871: { st r0, zero; addi r0, r0, 8; addi r1, r1, -8 }
88 bnezt r1, 1b
892: { move r0, r1; jrp lr }
90 STD_ENDPROC(clear_user_asm)
91 .pushsection __ex_table,"a"
Chris Metcalfd4d9eab2013-08-09 15:38:43 -040092 .align 8
Chris Metcalf18aecc22011-05-04 14:38:26 -040093 .quad 1b, 2b
94 .popsection
95
96/*
97 * flush_user_asm takes the user target address in r0 and the
98 * number of bytes to flush in r1.
99 * It returns the number of unflushable bytes (hopefully zero) in r0.
100 */
101STD_ENTRY(flush_user_asm)
102 beqz r1, 2f
103 { movei r2, L2_CACHE_BYTES; add r1, r0, r1 }
104 { sub r2, zero, r2; addi r1, r1, L2_CACHE_BYTES-1 }
105 { and r0, r0, r2; and r1, r1, r2 }
106 { sub r1, r1, r0 }
1071: { flush r0; addi r1, r1, -CHIP_FLUSH_STRIDE() }
108 { addi r0, r0, CHIP_FLUSH_STRIDE(); bnezt r1, 1b }
1092: { move r0, r1; jrp lr }
110 STD_ENDPROC(flush_user_asm)
111 .pushsection __ex_table,"a"
Chris Metcalfd4d9eab2013-08-09 15:38:43 -0400112 .align 8
Chris Metcalf18aecc22011-05-04 14:38:26 -0400113 .quad 1b, 2b
114 .popsection
115
116/*
Chris Metcalf18aecc22011-05-04 14:38:26 -0400117 * finv_user_asm takes the user target address in r0 and the
118 * number of bytes to flush-invalidate in r1.
119 * It returns the number of not finv'able bytes (hopefully zero) in r0.
120 */
121STD_ENTRY(finv_user_asm)
122 beqz r1, 2f
123 { movei r2, L2_CACHE_BYTES; add r1, r0, r1 }
124 { sub r2, zero, r2; addi r1, r1, L2_CACHE_BYTES-1 }
125 { and r0, r0, r2; and r1, r1, r2 }
126 { sub r1, r1, r0 }
1271: { finv r0; addi r1, r1, -CHIP_FINV_STRIDE() }
128 { addi r0, r0, CHIP_FINV_STRIDE(); bnezt r1, 1b }
1292: { move r0, r1; jrp lr }
130 STD_ENDPROC(finv_user_asm)
131 .pushsection __ex_table,"a"
Chris Metcalfd4d9eab2013-08-09 15:38:43 -0400132 .align 8
Chris Metcalf18aecc22011-05-04 14:38:26 -0400133 .quad 1b, 2b
134 .popsection