/*1* Copyright 2010 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{ move r0, zero; move r1, zero }25{ movei r2, -EFAULT; jrp lr }26ENDPROC(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 r236* on success, with the value in r0; or else -EFAULT in r2.37*/38#define __get_user_N(bytes, LOAD) \39STD_ENTRY(__get_user_##bytes); \401: { LOAD r0, r0; move r1, zero; move r2, zero }; \41jrp lr; \42STD_ENDPROC(__get_user_##bytes); \43.pushsection __ex_table,"a"; \44.word 1b, get_user_fault; \45.popsection4647__get_user_N(1, lb_u)48__get_user_N(2, lh_u)49__get_user_N(4, lw)5051/*52* __get_user_8 takes a pointer in r0, and returns 0 in r253* on success, with the value in r0/r1; or else -EFAULT in r2.54*/55STD_ENTRY(__get_user_8);561: { lw r0, r0; addi r1, r0, 4 };572: { lw r1, r1; move r2, zero };58jrp lr;59STD_ENDPROC(__get_user_8);60.pushsection __ex_table,"a";61.word 1b, get_user_fault;62.word 2b, get_user_fault;63.popsection6465/*66* __put_user_N functions take a value in r0 and a pointer in r1,67* and return 0 in r0 on success or -EFAULT on failure.68*/69#define __put_user_N(bytes, STORE) \70STD_ENTRY(__put_user_##bytes); \711: { STORE r1, r0; move r0, zero }; \72jrp lr; \73STD_ENDPROC(__put_user_##bytes); \74.pushsection __ex_table,"a"; \75.word 1b, put_user_fault; \76.popsection7778__put_user_N(1, sb)79__put_user_N(2, sh)80__put_user_N(4, sw)8182/*83* __put_user_8 takes a value in r0/r1 and a pointer in r2,84* and returns 0 in r0 on success or -EFAULT on failure.85*/86STD_ENTRY(__put_user_8)871: { sw r2, r0; addi r2, r2, 4 }882: { sw r2, r1; move r0, zero }89jrp lr90STD_ENDPROC(__put_user_8)91.pushsection __ex_table,"a"92.word 1b, put_user_fault93.word 2b, put_user_fault94.popsection959697/*98* strnlen_user_asm takes the pointer in r0, and the length bound in r1.99* It returns the length, including the terminating NUL, or zero on exception.100* If length is greater than the bound, returns one plus the bound.101*/102STD_ENTRY(strnlen_user_asm)103{ bz r1, 2f; addi r3, r0, -1 } /* bias down to include NUL */1041: { lb_u r4, r0; addi r1, r1, -1 }105bz r4, 2f106{ bnzt r1, 1b; addi r0, r0, 1 }1072: { sub r0, r0, r3; jrp lr }108STD_ENDPROC(strnlen_user_asm)109.pushsection .fixup,"ax"110strnlen_user_fault:111{ move r0, zero; jrp lr }112ENDPROC(strnlen_user_fault)113.section __ex_table,"a"114.word 1b, strnlen_user_fault115.popsection116117/*118* strncpy_from_user_asm takes the kernel target pointer in r0,119* the userspace source pointer in r1, and the length bound (including120* the trailing NUL) in r2. On success, it returns the string length121* (not including the trailing NUL), or -EFAULT on failure.122*/123STD_ENTRY(strncpy_from_user_asm)124{ bz r2, 2f; move r3, r0 }1251: { lb_u r4, r1; addi r1, r1, 1; addi r2, r2, -1 }126{ sb r0, r4; addi r0, r0, 1 }127bz r2, 2f128bnzt r4, 1b129addi r0, r0, -1 /* don't count the trailing NUL */1302: { sub r0, r0, r3; jrp lr }131STD_ENDPROC(strncpy_from_user_asm)132.pushsection .fixup,"ax"133strncpy_from_user_fault:134{ movei r0, -EFAULT; jrp lr }135ENDPROC(strncpy_from_user_fault)136.section __ex_table,"a"137.word 1b, strncpy_from_user_fault138.popsection139140/*141* clear_user_asm takes the user target address in r0 and the142* number of bytes to zero in r1.143* It returns the number of uncopiable bytes (hopefully zero) in r0.144* Note that we don't use a separate .fixup section here since we fall145* through into the "fixup" code as the last straight-line bundle anyway.146*/147STD_ENTRY(clear_user_asm)148{ bz r1, 2f; or r2, r0, r1 }149andi r2, r2, 3150bzt r2, .Lclear_aligned_user_asm1511: { sb r0, zero; addi r0, r0, 1; addi r1, r1, -1 }152bnzt r1, 1b1532: { move r0, r1; jrp lr }154.pushsection __ex_table,"a"155.word 1b, 2b156.popsection157158.Lclear_aligned_user_asm:1591: { sw r0, zero; addi r0, r0, 4; addi r1, r1, -4 }160bnzt r1, 1b1612: { move r0, r1; jrp lr }162STD_ENDPROC(clear_user_asm)163.pushsection __ex_table,"a"164.word 1b, 2b165.popsection166167/*168* flush_user_asm takes the user target address in r0 and the169* number of bytes to flush in r1.170* It returns the number of unflushable bytes (hopefully zero) in r0.171*/172STD_ENTRY(flush_user_asm)173bz r1, 2f174{ movei r2, L2_CACHE_BYTES; add r1, r0, r1 }175{ sub r2, zero, r2; addi r1, r1, L2_CACHE_BYTES-1 }176{ and r0, r0, r2; and r1, r1, r2 }177{ sub r1, r1, r0 }1781: { flush r0; addi r1, r1, -CHIP_FLUSH_STRIDE() }179{ addi r0, r0, CHIP_FLUSH_STRIDE(); bnzt r1, 1b }1802: { move r0, r1; jrp lr }181STD_ENDPROC(flush_user_asm)182.pushsection __ex_table,"a"183.word 1b, 2b184.popsection185186/*187* inv_user_asm takes the user target address in r0 and the188* number of bytes to invalidate in r1.189* It returns the number of not inv'able bytes (hopefully zero) in r0.190*/191STD_ENTRY(inv_user_asm)192bz r1, 2f193{ movei r2, L2_CACHE_BYTES; add r1, r0, r1 }194{ sub r2, zero, r2; addi r1, r1, L2_CACHE_BYTES-1 }195{ and r0, r0, r2; and r1, r1, r2 }196{ sub r1, r1, r0 }1971: { inv r0; addi r1, r1, -CHIP_INV_STRIDE() }198{ addi r0, r0, CHIP_INV_STRIDE(); bnzt r1, 1b }1992: { move r0, r1; jrp lr }200STD_ENDPROC(inv_user_asm)201.pushsection __ex_table,"a"202.word 1b, 2b203.popsection204205/*206* finv_user_asm takes the user target address in r0 and the207* number of bytes to flush-invalidate in r1.208* It returns the number of not finv'able bytes (hopefully zero) in r0.209*/210STD_ENTRY(finv_user_asm)211bz r1, 2f212{ movei r2, L2_CACHE_BYTES; add r1, r0, r1 }213{ sub r2, zero, r2; addi r1, r1, L2_CACHE_BYTES-1 }214{ and r0, r0, r2; and r1, r1, r2 }215{ sub r1, r1, r0 }2161: { finv r0; addi r1, r1, -CHIP_FINV_STRIDE() }217{ addi r0, r0, CHIP_FINV_STRIDE(); bnzt r1, 1b }2182: { move r0, r1; jrp lr }219STD_ENDPROC(finv_user_asm)220.pushsection __ex_table,"a"221.word 1b, 2b222.popsection223224225