Path: blob/master/arch/microblaze/include/asm/uaccess.h
26439 views
/* SPDX-License-Identifier: GPL-2.0 */1/*2* Copyright (C) 2008-2009 Michal Simek <[email protected]>3* Copyright (C) 2008-2009 PetaLogix4* Copyright (C) 2006 Atmark Techno, Inc.5*/67#ifndef _ASM_MICROBLAZE_UACCESS_H8#define _ASM_MICROBLAZE_UACCESS_H910#include <linux/kernel.h>1112#include <asm/mmu.h>13#include <asm/page.h>14#include <linux/pgtable.h>15#include <asm/extable.h>16#include <linux/string.h>17#include <asm-generic/access_ok.h>1819# define __FIXUP_SECTION ".section .fixup,\"ax\"\n"20# define __EX_TABLE_SECTION ".section __ex_table,\"a\"\n"2122extern unsigned long __copy_tofrom_user(void __user *to,23const void __user *from, unsigned long size);2425/* Return: number of not copied bytes, i.e. 0 if OK or non-zero if fail. */26static inline unsigned long __must_check __clear_user(void __user *to,27unsigned long n)28{29/* normal memset with two words to __ex_table */30__asm__ __volatile__ ( \31"1: sb r0, %1, r0;" \32" addik %0, %0, -1;" \33" bneid %0, 1b;" \34" addik %1, %1, 1;" \35"2: " \36__EX_TABLE_SECTION \37".word 1b,2b;" \38".previous;" \39: "=r"(n), "=r"(to) \40: "0"(n), "1"(to)41);42return n;43}4445static inline unsigned long __must_check clear_user(void __user *to,46unsigned long n)47{48might_fault();49if (unlikely(!access_ok(to, n)))50return n;5152return __clear_user(to, n);53}5455/* put_user and get_user macros */56extern long __user_bad(void);5758#define __get_user_asm(insn, __gu_ptr, __gu_val, __gu_err) \59({ \60__asm__ __volatile__ ( \61"1:" insn " %1, %2, r0;" \62" addk %0, r0, r0;" \63"2: " \64__FIXUP_SECTION \65"3: brid 2b;" \66" addik %0, r0, %3;" \67".previous;" \68__EX_TABLE_SECTION \69".word 1b,3b;" \70".previous;" \71: "=&r"(__gu_err), "=r"(__gu_val) \72: "r"(__gu_ptr), "i"(-EFAULT) \73); \74})7576/**77* get_user: - Get a simple variable from user space.78* @x: Variable to store result.79* @ptr: Source address, in user space.80*81* Context: User context only. This function may sleep if pagefaults are82* enabled.83*84* This macro copies a single simple variable from user space to kernel85* space. It supports simple types like char and int, but not larger86* data types like structures or arrays.87*88* @ptr must have pointer-to-simple-variable type, and the result of89* dereferencing @ptr must be assignable to @x without a cast.90*91* Returns zero on success, or -EFAULT on error.92* On error, the variable @x is set to zero.93*/94#define get_user(x, ptr) ({ \95const typeof(*(ptr)) __user *__gu_ptr = (ptr); \96access_ok(__gu_ptr, sizeof(*__gu_ptr)) ? \97__get_user(x, __gu_ptr) : -EFAULT; \98})99100#define __get_user(x, ptr) \101({ \102long __gu_err; \103switch (sizeof(*(ptr))) { \104case 1: \105__get_user_asm("lbu", (ptr), x, __gu_err); \106break; \107case 2: \108__get_user_asm("lhu", (ptr), x, __gu_err); \109break; \110case 4: \111__get_user_asm("lw", (ptr), x, __gu_err); \112break; \113case 8: { \114__u64 __x = 0; \115__gu_err = raw_copy_from_user(&__x, ptr, 8) ? \116-EFAULT : 0; \117(x) = (typeof(x))(typeof((x) - (x)))__x; \118break; \119} \120default: \121/* __gu_val = 0; __gu_err = -EINVAL;*/ __gu_err = __user_bad();\122} \123__gu_err; \124})125126127#define __put_user_asm(insn, __gu_ptr, __gu_val, __gu_err) \128({ \129__asm__ __volatile__ ( \130"1:" insn " %1, %2, r0;" \131" addk %0, r0, r0;" \132"2: " \133__FIXUP_SECTION \134"3: brid 2b;" \135" addik %0, r0, %3;" \136".previous;" \137__EX_TABLE_SECTION \138".word 1b,3b;" \139".previous;" \140: "=&r"(__gu_err) \141: "r"(__gu_val), "r"(__gu_ptr), "i"(-EFAULT) \142); \143})144145#define __put_user_asm_8(__gu_ptr, __gu_val, __gu_err) \146({ \147__asm__ __volatile__ (" lwi %0, %1, 0;" \148"1: swi %0, %2, 0;" \149" lwi %0, %1, 4;" \150"2: swi %0, %2, 4;" \151" addk %0, r0, r0;" \152"3: " \153__FIXUP_SECTION \154"4: brid 3b;" \155" addik %0, r0, %3;" \156".previous;" \157__EX_TABLE_SECTION \158".word 1b,4b,2b,4b;" \159".previous;" \160: "=&r"(__gu_err) \161: "r"(&__gu_val), "r"(__gu_ptr), "i"(-EFAULT) \162); \163})164165/**166* put_user: - Write a simple value into user space.167* @x: Value to copy to user space.168* @ptr: Destination address, in user space.169*170* Context: User context only. This function may sleep if pagefaults are171* enabled.172*173* This macro copies a single simple value from kernel space to user174* space. It supports simple types like char and int, but not larger175* data types like structures or arrays.176*177* @ptr must have pointer-to-simple-variable type, and @x must be assignable178* to the result of dereferencing @ptr.179*180* Returns zero on success, or -EFAULT on error.181*/182#define put_user(x, ptr) \183__put_user_check((x), (ptr), sizeof(*(ptr)))184185#define __put_user_check(x, ptr, size) \186({ \187typeof(*(ptr)) volatile __pu_val = x; \188typeof(*(ptr)) __user *__pu_addr = (ptr); \189int __pu_err = 0; \190\191if (access_ok(__pu_addr, size)) { \192switch (size) { \193case 1: \194__put_user_asm("sb", __pu_addr, __pu_val, \195__pu_err); \196break; \197case 2: \198__put_user_asm("sh", __pu_addr, __pu_val, \199__pu_err); \200break; \201case 4: \202__put_user_asm("sw", __pu_addr, __pu_val, \203__pu_err); \204break; \205case 8: \206__put_user_asm_8(__pu_addr, __pu_val, __pu_err);\207break; \208default: \209__pu_err = __user_bad(); \210break; \211} \212} else { \213__pu_err = -EFAULT; \214} \215__pu_err; \216})217218#define __put_user(x, ptr) \219({ \220__typeof__(*(ptr)) volatile __gu_val = (x); \221long __gu_err = 0; \222switch (sizeof(__gu_val)) { \223case 1: \224__put_user_asm("sb", (ptr), __gu_val, __gu_err); \225break; \226case 2: \227__put_user_asm("sh", (ptr), __gu_val, __gu_err); \228break; \229case 4: \230__put_user_asm("sw", (ptr), __gu_val, __gu_err); \231break; \232case 8: \233__put_user_asm_8((ptr), __gu_val, __gu_err); \234break; \235default: \236/*__gu_err = -EINVAL;*/ __gu_err = __user_bad(); \237} \238__gu_err; \239})240241static inline unsigned long242raw_copy_from_user(void *to, const void __user *from, unsigned long n)243{244return __copy_tofrom_user((__force void __user *)to, from, n);245}246247static inline unsigned long248raw_copy_to_user(void __user *to, const void *from, unsigned long n)249{250return __copy_tofrom_user(to, (__force const void __user *)from, n);251}252#define INLINE_COPY_FROM_USER253#define INLINE_COPY_TO_USER254255/*256* Copy a null terminated string from userspace.257*/258__must_check long strncpy_from_user(char *dst, const char __user *src,259long count);260261/*262* Return the size of a string (including the ending 0)263*264* Return 0 on exception, a value greater than N if too long265*/266__must_check long strnlen_user(const char __user *sstr, long len);267268#endif /* _ASM_MICROBLAZE_UACCESS_H */269270271