/* * Copyright (C) 2004-2006 Atmel Corporation * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. */ #ifndef __ASM_AVR32_UACCESS_H #define __ASM_AVR32_UACCESS_H #include #include #define VERIFY_READ 0 #define VERIFY_WRITE 1 typedef struct { unsigned int is_user_space; } mm_segment_t; /* * The fs value determines whether argument validity checking should be * performed or not. If get_fs() == USER_DS, checking is performed, with * get_fs() == KERNEL_DS, checking is bypassed. * * For historical reasons (Data Segment Register?), these macros are misnamed. */ #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) }) #define segment_eq(a,b) ((a).is_user_space == (b).is_user_space) #define USER_ADDR_LIMIT 0x80000000 #define KERNEL_DS MAKE_MM_SEG(0) #define USER_DS MAKE_MM_SEG(1) #define get_ds() (KERNEL_DS) static inline mm_segment_t get_fs(void) { return MAKE_MM_SEG(test_thread_flag(TIF_USERSPACE)); } static inline void set_fs(mm_segment_t s) { if (s.is_user_space) set_thread_flag(TIF_USERSPACE); else clear_thread_flag(TIF_USERSPACE); } /* * Test whether a block of memory is a valid user space address. * Returns 0 if the range is valid, nonzero otherwise. * * We do the following checks: * 1. Is the access from kernel space? * 2. Does (addr + size) set the carry bit? * 3. Is (addr + size) a negative number (i.e. >= 0x80000000)? * * If yes on the first check, access is granted. * If no on any of the others, access is denied. */ #define __range_ok(addr, size) \ (test_thread_flag(TIF_USERSPACE) \ && (((unsigned long)(addr) >= 0x80000000) \ || ((unsigned long)(size) > 0x80000000) \ || (((unsigned long)(addr) + (unsigned long)(size)) > 0x80000000))) #define access_ok(type, addr, size) (likely(__range_ok(addr, size) == 0)) /* Generic arbitrary sized copy. Return the number of bytes NOT copied */ extern __kernel_size_t __copy_user(void *to, const void *from, __kernel_size_t n); extern __kernel_size_t copy_to_user(void __user *to, const void *from, __kernel_size_t n); extern __kernel_size_t copy_from_user(void *to, const void __user *from, __kernel_size_t n); static inline __kernel_size_t __copy_to_user(void __user *to, const void *from, __kernel_size_t n) { return __copy_user((void __force *)to, from, n); } static inline __kernel_size_t __copy_from_user(void *to, const void __user *from, __kernel_size_t n) { return __copy_user(to, (const void __force *)from, n); } #define __copy_to_user_inatomic __copy_to_user #define __copy_from_user_inatomic __copy_from_user /* * put_user: - Write a simple value into user space. * @x: Value to copy to user space. * @ptr: Destination address, in user space. * * Context: User context only. This function may sleep. * * This macro copies a single simple value from kernel space to user * space. It supports simple types like char and int, but not larger * data types like structures or arrays. * * @ptr must have pointer-to-simple-variable type, and @x must be assignable * to the result of dereferencing @ptr. * * Returns zero on success, or -EFAULT on error. */ #define put_user(x,ptr) \ __put_user_check((x),(ptr),sizeof(*(ptr)))