/*1* Copyright 2011 Tilera Corporation. All Rights Reserved.2*3* This program is free software; you can redistribute it and/or4* modify it under the terms of the GNU General Public License5* as published by the Free Software Foundation, version 2.6*7* This program is distributed in the hope that it will be useful, but8* WITHOUT ANY WARRANTY; without even the implied warranty of9* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or10* NON INFRINGEMENT. See the GNU General Public License for11* more details.12*/1314#include <linux/linkage.h>15#include <asm/errno.h>16#include <asm/cache.h>17#include <arch/chip.h>1819/* Access user memory, but use MMU to avoid propagating kernel exceptions. */2021.pushsection .fixup,"ax"2223get_user_fault:24{ movei r1, -EFAULT; move r0, zero }25jrp lr26ENDPROC(get_user_fault)2728put_user_fault:29{ movei r0, -EFAULT; jrp lr }30ENDPROC(put_user_fault)3132.popsection3334/*35* __get_user_N functions take a pointer in r0, and return 0 in r136* on success, with the value in r0; or else -EFAULT in r1.37*/38#define __get_user_N(bytes, LOAD) \39STD_ENTRY(__get_user_##bytes); \401: { LOAD r0, r0; move r1, zero }; \41jrp lr; \42STD_ENDPROC(__get_user_##bytes); \43.pushsection __ex_table,"a"; \44.quad 1b, get_user_fault; \45.popsection4647__get_user_N(1, ld1u)48__get_user_N(2, ld2u)49__get_user_N(4, ld4u)50__get_user_N(8, ld)5152/*53* __put_user_N functions take a value in r0 and a pointer in r1,54* and return 0 in r0 on success or -EFAULT on failure.55*/56#define __put_user_N(bytes, STORE) \57STD_ENTRY(__put_user_##bytes); \581: { STORE r1, r0; move r0, zero }; \59jrp lr; \60STD_ENDPROC(__put_user_##bytes); \61.pushsection __ex_table,"a"; \62.quad 1b, put_user_fault; \63.popsection6465__put_user_N(1, st1)66__put_user_N(2, st2)67__put_user_N(4, st4)68__put_user_N(8, st)6970/*71* strnlen_user_asm takes the pointer in r0, and the length bound in r1.72* It returns the length, including the terminating NUL, or zero on exception.73* If length is greater than the bound, returns one plus the bound.74*/75STD_ENTRY(strnlen_user_asm)76{ beqz r1, 2f; addi r3, r0, -1 } /* bias down to include NUL */771: { ld1u r4, r0; addi r1, r1, -1 }78beqz r4, 2f79{ bnezt r1, 1b; addi r0, r0, 1 }802: { sub r0, r0, r3; jrp lr }81STD_ENDPROC(strnlen_user_asm)82.pushsection .fixup,"ax"83strnlen_user_fault:84{ move r0, zero; jrp lr }85ENDPROC(strnlen_user_fault)86.section __ex_table,"a"87.quad 1b, strnlen_user_fault88.popsection8990/*91* strncpy_from_user_asm takes the kernel target pointer in r0,92* the userspace source pointer in r1, and the length bound (including93* the trailing NUL) in r2. On success, it returns the string length94* (not including the trailing NUL), or -EFAULT on failure.95*/96STD_ENTRY(strncpy_from_user_asm)97{ beqz r2, 2f; move r3, r0 }981: { ld1u r4, r1; addi r1, r1, 1; addi r2, r2, -1 }99{ st1 r0, r4; addi r0, r0, 1 }100beqz r2, 2f101bnezt r4, 1b102addi r0, r0, -1 /* don't count the trailing NUL */1032: { sub r0, r0, r3; jrp lr }104STD_ENDPROC(strncpy_from_user_asm)105.pushsection .fixup,"ax"106strncpy_from_user_fault:107{ movei r0, -EFAULT; jrp lr }108ENDPROC(strncpy_from_user_fault)109.section __ex_table,"a"110.quad 1b, strncpy_from_user_fault111.popsection112113/*114* clear_user_asm takes the user target address in r0 and the115* number of bytes to zero in r1.116* It returns the number of uncopiable bytes (hopefully zero) in r0.117* Note that we don't use a separate .fixup section here since we fall118* through into the "fixup" code as the last straight-line bundle anyway.119*/120STD_ENTRY(clear_user_asm)121{ beqz r1, 2f; or r2, r0, r1 }122andi r2, r2, 7123beqzt r2, .Lclear_aligned_user_asm1241: { st1 r0, zero; addi r0, r0, 1; addi r1, r1, -1 }125bnezt r1, 1b1262: { move r0, r1; jrp lr }127.pushsection __ex_table,"a"128.quad 1b, 2b129.popsection130131.Lclear_aligned_user_asm:1321: { st r0, zero; addi r0, r0, 8; addi r1, r1, -8 }133bnezt r1, 1b1342: { move r0, r1; jrp lr }135STD_ENDPROC(clear_user_asm)136.pushsection __ex_table,"a"137.quad 1b, 2b138.popsection139140/*141* flush_user_asm takes the user target address in r0 and the142* number of bytes to flush in r1.143* It returns the number of unflushable bytes (hopefully zero) in r0.144*/145STD_ENTRY(flush_user_asm)146beqz r1, 2f147{ movei r2, L2_CACHE_BYTES; add r1, r0, r1 }148{ sub r2, zero, r2; addi r1, r1, L2_CACHE_BYTES-1 }149{ and r0, r0, r2; and r1, r1, r2 }150{ sub r1, r1, r0 }1511: { flush r0; addi r1, r1, -CHIP_FLUSH_STRIDE() }152{ addi r0, r0, CHIP_FLUSH_STRIDE(); bnezt r1, 1b }1532: { move r0, r1; jrp lr }154STD_ENDPROC(flush_user_asm)155.pushsection __ex_table,"a"156.quad 1b, 2b157.popsection158159/*160* inv_user_asm takes the user target address in r0 and the161* number of bytes to invalidate in r1.162* It returns the number of not inv'able bytes (hopefully zero) in r0.163*/164STD_ENTRY(inv_user_asm)165beqz r1, 2f166{ movei r2, L2_CACHE_BYTES; add r1, r0, r1 }167{ sub r2, zero, r2; addi r1, r1, L2_CACHE_BYTES-1 }168{ and r0, r0, r2; and r1, r1, r2 }169{ sub r1, r1, r0 }1701: { inv r0; addi r1, r1, -CHIP_INV_STRIDE() }171{ addi r0, r0, CHIP_INV_STRIDE(); bnezt r1, 1b }1722: { move r0, r1; jrp lr }173STD_ENDPROC(inv_user_asm)174.pushsection __ex_table,"a"175.quad 1b, 2b176.popsection177178/*179* finv_user_asm takes the user target address in r0 and the180* number of bytes to flush-invalidate in r1.181* It returns the number of not finv'able bytes (hopefully zero) in r0.182*/183STD_ENTRY(finv_user_asm)184beqz r1, 2f185{ movei r2, L2_CACHE_BYTES; add r1, r0, r1 }186{ sub r2, zero, r2; addi r1, r1, L2_CACHE_BYTES-1 }187{ and r0, r0, r2; and r1, r1, r2 }188{ sub r1, r1, r0 }1891: { finv r0; addi r1, r1, -CHIP_FINV_STRIDE() }190{ addi r0, r0, CHIP_FINV_STRIDE(); bnezt r1, 1b }1912: { move r0, r1; jrp lr }192STD_ENDPROC(finv_user_asm)193.pushsection __ex_table,"a"194.quad 1b, 2b195.popsection196197198