blob: 3c32baf8b49447a591e6e552e634eafe5be8d09e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
Steven J. Hill0131f2b2013-03-25 13:41:18 -05006 * Copyright (C) 1996, 1999 by Ralf Baechle
7 * Copyright (C) 2011 MIPS Technologies, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9#include <linux/errno.h>
10#include <asm/asm.h>
Sam Ravnborg048eb582005-09-09 22:32:31 +020011#include <asm/asm-offsets.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <asm/regdef.h>
13
14#define EX(insn,reg,addr,handler) \
159: insn reg, addr; \
16 .section __ex_table,"a"; \
17 PTR 9b, handler; \
18 .previous
19
20/*
21 * Returns: -EFAULT if exception before terminator, N if the entire
22 * buffer filled, else strlen.
23 */
24
25/*
26 * Ugly special case have to check: we might get passed a user space
Ralf Baechle70342282013-01-22 12:59:30 +010027 * pointer which wraps into the kernel space. We don't deal with that. If
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * it happens at most some bytes of the exceptions handlers will be copied.
29 */
30
Markos Chandrascc59fe52014-01-02 16:36:49 +000031 .macro __BUILD_STRNCPY_ASM func
32LEAF(__strncpy_from_\func\()_asm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 LONG_L v0, TI_ADDR_LIMIT($28) # pointer ok?
34 and v0, a1
Markos Chandrascc59fe52014-01-02 16:36:49 +000035 bnez v0, .Lfault\@
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Markos Chandrascc59fe52014-01-02 16:36:49 +000037FEXPORT(__strncpy_from_\func\()_nocheck_asm)
Steven J. Hill0131f2b2013-03-25 13:41:18 -050038 move t0, zero
39 move v1, a1
Markos Chandrasb3c30252014-01-02 16:40:20 +000040.ifeqs "\func","kernel"
Markos Chandrascc59fe52014-01-02 16:36:49 +0000411: EX(lbu, v0, (v1), .Lfault\@)
Markos Chandrasb3c30252014-01-02 16:40:20 +000042.else
431: EX(lbue, v0, (v1), .Lfault\@)
44.endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 PTR_ADDIU v1, 1
Thomas Bogendoerfer930bff82007-11-25 11:47:56 +010046 R10KCBARRIER(0(ra))
Maciej W. Rozycki465ca5d62014-04-04 03:32:29 +010047 sb v0, (a0)
Steven J. Hill0131f2b2013-03-25 13:41:18 -050048 beqz v0, 2f
Steven J. Hill0131f2b2013-03-25 13:41:18 -050049 PTR_ADDIU t0, 1
Maciej W. Rozycki465ca5d62014-04-04 03:32:29 +010050 PTR_ADDIU a0, 1
Steven J. Hill0131f2b2013-03-25 13:41:18 -050051 bne t0, a2, 1b
Steven J. Hill0131f2b2013-03-25 13:41:18 -0500522: PTR_ADDU v0, a1, t0
53 xor v0, a1
Markos Chandrascc59fe52014-01-02 16:36:49 +000054 bltz v0, .Lfault\@
Maciej W. Rozycki465ca5d62014-04-04 03:32:29 +010055 move v0, t0
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 jr ra # return n
Markos Chandrascc59fe52014-01-02 16:36:49 +000057 END(__strncpy_from_\func\()_asm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Maciej W. Rozycki465ca5d62014-04-04 03:32:29 +010059.Lfault\@:
60 li v0, -EFAULT
61 jr ra
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 .section __ex_table,"a"
Markos Chandrascc59fe52014-01-02 16:36:49 +000064 PTR 1b, .Lfault\@
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 .previous
Markos Chandrascc59fe52014-01-02 16:36:49 +000066
67 .endm
68
Markos Chandrasb3c30252014-01-02 16:40:20 +000069#ifndef CONFIG_EVA
70 /* Set aliases */
71 .global __strncpy_from_user_asm
72 .global __strncpy_from_user_nocheck_asm
73 .set __strncpy_from_user_asm, __strncpy_from_kernel_asm
74 .set __strncpy_from_user_nocheck_asm, __strncpy_from_kernel_nocheck_asm
75#endif
76
77__BUILD_STRNCPY_ASM kernel
78
79#ifdef CONFIG_EVA
80 .set push
81 .set eva
Markos Chandrascc59fe52014-01-02 16:36:49 +000082__BUILD_STRNCPY_ASM user
Markos Chandrasb3c30252014-01-02 16:40:20 +000083 .set pop
84#endif