Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

nios2: Memory management

This patch contains the initialisation of the memory blocks, MMU
attributes and the memory map.

Signed-off-by: Ley Foon Tan <lftan@altera.com>

+663
+16
arch/nios2/include/asm/mmu.h
··· 1 + /* 2 + * Copyright (C) 2010 Tobias Klauser <tklauser@distanz.ch> 3 + * Copyright (C) 2004 Microtronix Datacom Ltd. 4 + * 5 + * This file is subject to the terms and conditions of the GNU General Public 6 + * License. See the file "COPYING" in the main directory of this archive 7 + * for more details. 8 + */ 9 + 10 + #ifndef _ASM_NIOS2_MMU_H 11 + #define _ASM_NIOS2_MMU_H 12 + 13 + /* Default "unsigned long" context */ 14 + typedef unsigned long mm_context_t; 15 + 16 + #endif /* _ASM_NIOS2_MMU_H */
+111
arch/nios2/include/asm/page.h
··· 1 + /* 2 + * Copyright (C) 2011 Tobias Klauser <tklauser@distanz.ch> 3 + * Copyright (C) 2004 Microtronix Datacom Ltd. 4 + * 5 + * MMU support based on asm/page.h from mips which is: 6 + * 7 + * Copyright (C) 1994 - 1999, 2000, 03 Ralf Baechle 8 + * Copyright (C) 1999, 2000 Silicon Graphics, Inc. 9 + * 10 + * This file is subject to the terms and conditions of the GNU General Public 11 + * License. See the file "COPYING" in the main directory of this archive 12 + * for more details. 13 + */ 14 + 15 + #ifndef _ASM_NIOS2_PAGE_H 16 + #define _ASM_NIOS2_PAGE_H 17 + 18 + #include <linux/pfn.h> 19 + #include <linux/const.h> 20 + 21 + /* 22 + * PAGE_SHIFT determines the page size 23 + */ 24 + #define PAGE_SHIFT 12 25 + #define PAGE_SIZE (_AC(1, UL) << PAGE_SHIFT) 26 + #define PAGE_MASK (~(PAGE_SIZE - 1)) 27 + 28 + /* 29 + * PAGE_OFFSET -- the first address of the first page of memory. 30 + */ 31 + #define PAGE_OFFSET \ 32 + (CONFIG_NIOS2_MEM_BASE + CONFIG_NIOS2_KERNEL_REGION_BASE) 33 + 34 + #ifndef __ASSEMBLY__ 35 + 36 + /* 37 + * This gives the physical RAM offset. 38 + */ 39 + #define PHYS_OFFSET CONFIG_NIOS2_MEM_BASE 40 + 41 + /* 42 + * It's normally defined only for FLATMEM config but it's 43 + * used in our early mem init code for all memory models. 44 + * So always define it. 45 + */ 46 + #define ARCH_PFN_OFFSET PFN_UP(PHYS_OFFSET) 47 + 48 + #define clear_page(page) memset((page), 0, PAGE_SIZE) 49 + #define copy_page(to, from) memcpy((to), (from), PAGE_SIZE) 50 + 51 + struct page; 52 + 53 + extern void clear_user_page(void *addr, unsigned long vaddr, struct page *page); 54 + extern void copy_user_page(void *vto, void *vfrom, unsigned long vaddr, 55 + struct page *to); 56 + 57 + extern unsigned long shm_align_mask; 58 + 59 + /* 60 + * These are used to make use of C type-checking. 61 + */ 62 + typedef struct page *pgtable_t; 63 + typedef struct { unsigned long pte; } pte_t; 64 + typedef struct { unsigned long pgd; } pgd_t; 65 + typedef struct { unsigned long pgprot; } pgprot_t; 66 + 67 + #define pte_val(x) ((x).pte) 68 + #define pgd_val(x) ((x).pgd) 69 + #define pgprot_val(x) ((x).pgprot) 70 + 71 + #define __pte(x) ((pte_t) { (x) }) 72 + #define __pgd(x) ((pgd_t) { (x) }) 73 + #define __pgprot(x) ((pgprot_t) { (x) }) 74 + 75 + extern unsigned long memory_start; 76 + extern unsigned long memory_end; 77 + extern unsigned long memory_size; 78 + 79 + extern struct page *mem_map; 80 + 81 + #endif /* !__ASSEMBLY__ */ 82 + 83 + # define __pa(x) \ 84 + ((unsigned long)(x) - PAGE_OFFSET + PHYS_OFFSET) 85 + # define __va(x) \ 86 + ((void *)((unsigned long)(x) + PAGE_OFFSET - PHYS_OFFSET)) 87 + 88 + #define page_to_virt(page) \ 89 + ((((page) - mem_map) << PAGE_SHIFT) + PAGE_OFFSET) 90 + 91 + # define pfn_to_kaddr(pfn) __va((pfn) << PAGE_SHIFT) 92 + # define pfn_valid(pfn) ((pfn) >= ARCH_PFN_OFFSET && \ 93 + (pfn) < max_mapnr) 94 + 95 + # define virt_to_page(vaddr) pfn_to_page(PFN_DOWN(virt_to_phys(vaddr))) 96 + # define virt_addr_valid(vaddr) pfn_valid(PFN_DOWN(virt_to_phys(vaddr))) 97 + 98 + # define VM_DATA_DEFAULT_FLAGS (VM_READ | VM_WRITE | \ 99 + VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC) 100 + 101 + # define UNCAC_ADDR(addr) \ 102 + ((void *)((unsigned)(addr) | CONFIG_NIOS2_IO_REGION_BASE)) 103 + # define CAC_ADDR(addr) \ 104 + ((void *)(((unsigned)(addr) & ~CONFIG_NIOS2_IO_REGION_BASE) | \ 105 + CONFIG_NIOS2_KERNEL_REGION_BASE)) 106 + 107 + #include <asm-generic/memory_model.h> 108 + 109 + #include <asm-generic/getorder.h> 110 + 111 + #endif /* _ASM_NIOS2_PAGE_H */
+231
arch/nios2/include/asm/uaccess.h
··· 1 + /* 2 + * User space memory access functions for Nios II 3 + * 4 + * Copyright (C) 2010-2011, Tobias Klauser <tklauser@distanz.ch> 5 + * Copyright (C) 2009, Wind River Systems Inc 6 + * Implemented by fredrik.markstrom@gmail.com and ivarholmqvist@gmail.com 7 + * 8 + * This file is subject to the terms and conditions of the GNU General Public 9 + * License. See the file "COPYING" in the main directory of this archive 10 + * for more details. 11 + */ 12 + 13 + #ifndef _ASM_NIOS2_UACCESS_H 14 + #define _ASM_NIOS2_UACCESS_H 15 + 16 + #include <linux/errno.h> 17 + #include <linux/thread_info.h> 18 + #include <linux/string.h> 19 + 20 + #include <asm/page.h> 21 + 22 + #define VERIFY_READ 0 23 + #define VERIFY_WRITE 1 24 + 25 + /* 26 + * The exception table consists of pairs of addresses: the first is the 27 + * address of an instruction that is allowed to fault, and the second is 28 + * the address at which the program should continue. No registers are 29 + * modified, so it is entirely up to the continuation code to figure out 30 + * what to do. 31 + * 32 + * All the routines below use bits of fixup code that are out of line 33 + * with the main instruction path. This means when everything is well, 34 + * we don't even have to jump over them. Further, they do not intrude 35 + * on our cache or tlb entries. 36 + */ 37 + struct exception_table_entry { 38 + unsigned long insn; 39 + unsigned long fixup; 40 + }; 41 + 42 + extern int fixup_exception(struct pt_regs *regs); 43 + 44 + /* 45 + * Segment stuff 46 + */ 47 + #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) }) 48 + #define USER_DS MAKE_MM_SEG(0x80000000UL) 49 + #define KERNEL_DS MAKE_MM_SEG(0) 50 + 51 + #define get_ds() (KERNEL_DS) 52 + 53 + #define get_fs() (current_thread_info()->addr_limit) 54 + #define set_fs(seg) (current_thread_info()->addr_limit = (seg)) 55 + 56 + #define segment_eq(a, b) ((a).seg == (b).seg) 57 + 58 + #define __access_ok(addr, len) \ 59 + (((signed long)(((long)get_fs().seg) & \ 60 + ((long)(addr) | (((long)(addr)) + (len)) | (len)))) == 0) 61 + 62 + #define access_ok(type, addr, len) \ 63 + likely(__access_ok((unsigned long)(addr), (unsigned long)(len))) 64 + 65 + # define __EX_TABLE_SECTION ".section __ex_table,\"a\"\n" 66 + 67 + /* 68 + * Zero Userspace 69 + */ 70 + 71 + static inline unsigned long __must_check __clear_user(void __user *to, 72 + unsigned long n) 73 + { 74 + __asm__ __volatile__ ( 75 + "1: stb zero, 0(%1)\n" 76 + " addi %0, %0, -1\n" 77 + " addi %1, %1, 1\n" 78 + " bne %0, zero, 1b\n" 79 + "2:\n" 80 + __EX_TABLE_SECTION 81 + ".word 1b, 2b\n" 82 + ".previous\n" 83 + : "=r" (n), "=r" (to) 84 + : "0" (n), "1" (to) 85 + ); 86 + 87 + return n; 88 + } 89 + 90 + static inline unsigned long __must_check clear_user(void __user *to, 91 + unsigned long n) 92 + { 93 + if (!access_ok(VERIFY_WRITE, to, n)) 94 + return n; 95 + return __clear_user(to, n); 96 + } 97 + 98 + extern long __copy_from_user(void *to, const void __user *from, 99 + unsigned long n); 100 + extern long __copy_to_user(void __user *to, const void *from, unsigned long n); 101 + 102 + static inline long copy_from_user(void *to, const void __user *from, 103 + unsigned long n) 104 + { 105 + if (!access_ok(VERIFY_READ, from, n)) 106 + return n; 107 + return __copy_from_user(to, from, n); 108 + } 109 + 110 + static inline long copy_to_user(void __user *to, const void *from, 111 + unsigned long n) 112 + { 113 + if (!access_ok(VERIFY_WRITE, to, n)) 114 + return n; 115 + return __copy_to_user(to, from, n); 116 + } 117 + 118 + extern long strncpy_from_user(char *__to, const char __user *__from, 119 + long __len); 120 + extern long strnlen_user(const char __user *s, long n); 121 + 122 + #define __copy_from_user_inatomic __copy_from_user 123 + #define __copy_to_user_inatomic __copy_to_user 124 + 125 + /* Optimized macros */ 126 + #define __get_user_asm(val, insn, addr, err) \ 127 + { \ 128 + __asm__ __volatile__( \ 129 + " movi %0, %3\n" \ 130 + "1: " insn " %1, 0(%2)\n" \ 131 + " movi %0, 0\n" \ 132 + "2:\n" \ 133 + " .section __ex_table,\"a\"\n" \ 134 + " .word 1b, 2b\n" \ 135 + " .previous" \ 136 + : "=&r" (err), "=r" (val) \ 137 + : "r" (addr), "i" (-EFAULT)); \ 138 + } 139 + 140 + #define __get_user_unknown(val, size, ptr, err) do { \ 141 + err = 0; \ 142 + if (copy_from_user(&(val), ptr, size)) { \ 143 + err = -EFAULT; \ 144 + } \ 145 + } while (0) 146 + 147 + #define __get_user_common(val, size, ptr, err) \ 148 + do { \ 149 + switch (size) { \ 150 + case 1: \ 151 + __get_user_asm(val, "ldbu", ptr, err); \ 152 + break; \ 153 + case 2: \ 154 + __get_user_asm(val, "ldhu", ptr, err); \ 155 + break; \ 156 + case 4: \ 157 + __get_user_asm(val, "ldw", ptr, err); \ 158 + break; \ 159 + default: \ 160 + __get_user_unknown(val, size, ptr, err); \ 161 + break; \ 162 + } \ 163 + } while (0) 164 + 165 + #define __get_user(x, ptr) \ 166 + ({ \ 167 + long __gu_err = -EFAULT; \ 168 + const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \ 169 + unsigned long __gu_val; \ 170 + __get_user_common(__gu_val, sizeof(*(ptr)), __gu_ptr, __gu_err);\ 171 + (x) = (__typeof__(x))__gu_val; \ 172 + __gu_err; \ 173 + }) 174 + 175 + #define get_user(x, ptr) \ 176 + ({ \ 177 + long __gu_err = -EFAULT; \ 178 + const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \ 179 + unsigned long __gu_val = 0; \ 180 + if (access_ok(VERIFY_READ, __gu_ptr, sizeof(*__gu_ptr))) \ 181 + __get_user_common(__gu_val, sizeof(*__gu_ptr), \ 182 + __gu_ptr, __gu_err); \ 183 + (x) = (__typeof__(x))__gu_val; \ 184 + __gu_err; \ 185 + }) 186 + 187 + #define __put_user_asm(val, insn, ptr, err) \ 188 + { \ 189 + __asm__ __volatile__( \ 190 + " movi %0, %3\n" \ 191 + "1: " insn " %1, 0(%2)\n" \ 192 + " movi %0, 0\n" \ 193 + "2:\n" \ 194 + " .section __ex_table,\"a\"\n" \ 195 + " .word 1b, 2b\n" \ 196 + " .previous\n" \ 197 + : "=&r" (err) \ 198 + : "r" (val), "r" (ptr), "i" (-EFAULT)); \ 199 + } 200 + 201 + #define put_user(x, ptr) \ 202 + ({ \ 203 + long __pu_err = -EFAULT; \ 204 + __typeof__(*(ptr)) __user *__pu_ptr = (ptr); \ 205 + __typeof__(*(ptr)) __pu_val = (__typeof(*ptr))(x); \ 206 + if (access_ok(VERIFY_WRITE, __pu_ptr, sizeof(*__pu_ptr))) { \ 207 + switch (sizeof(*__pu_ptr)) { \ 208 + case 1: \ 209 + __put_user_asm(__pu_val, "stb", __pu_ptr, __pu_err); \ 210 + break; \ 211 + case 2: \ 212 + __put_user_asm(__pu_val, "sth", __pu_ptr, __pu_err); \ 213 + break; \ 214 + case 4: \ 215 + __put_user_asm(__pu_val, "stw", __pu_ptr, __pu_err); \ 216 + break; \ 217 + default: \ 218 + /* XXX: This looks wrong... */ \ 219 + __pu_err = 0; \ 220 + if (copy_to_user(__pu_ptr, &(__pu_val), \ 221 + sizeof(*__pu_ptr))) \ 222 + __pu_err = -EFAULT; \ 223 + break; \ 224 + } \ 225 + } \ 226 + __pu_err; \ 227 + }) 228 + 229 + #define __put_user(x, ptr) put_user(x, ptr) 230 + 231 + #endif /* _ASM_NIOS2_UACCESS_H */
+142
arch/nios2/mm/init.c
··· 1 + /* 2 + * Copyright (C) 2013 Altera Corporation 3 + * Copyright (C) 2010 Tobias Klauser <tklauser@distanz.ch> 4 + * Copyright (C) 2009 Wind River Systems Inc 5 + * Implemented by fredrik.markstrom@gmail.com and ivarholmqvist@gmail.com 6 + * Copyright (C) 2004 Microtronix Datacom Ltd 7 + * 8 + * based on arch/m68k/mm/init.c 9 + * 10 + * This file is subject to the terms and conditions of the GNU General Public 11 + * License. See the file "COPYING" in the main directory of this archive 12 + * for more details. 13 + */ 14 + 15 + #include <linux/signal.h> 16 + #include <linux/sched.h> 17 + #include <linux/kernel.h> 18 + #include <linux/errno.h> 19 + #include <linux/string.h> 20 + #include <linux/types.h> 21 + #include <linux/ptrace.h> 22 + #include <linux/mman.h> 23 + #include <linux/mm.h> 24 + #include <linux/init.h> 25 + #include <linux/pagemap.h> 26 + #include <linux/bootmem.h> 27 + #include <linux/slab.h> 28 + #include <linux/binfmts.h> 29 + 30 + #include <asm/setup.h> 31 + #include <asm/page.h> 32 + #include <asm/pgtable.h> 33 + #include <asm/sections.h> 34 + #include <asm/tlb.h> 35 + #include <asm/mmu_context.h> 36 + #include <asm/cpuinfo.h> 37 + #include <asm/processor.h> 38 + 39 + pgd_t *pgd_current; 40 + 41 + /* 42 + * paging_init() continues the virtual memory environment setup which 43 + * was begun by the code in arch/head.S. 44 + * The parameters are pointers to where to stick the starting and ending 45 + * addresses of available kernel virtual memory. 46 + */ 47 + void __init paging_init(void) 48 + { 49 + unsigned long zones_size[MAX_NR_ZONES]; 50 + 51 + memset(zones_size, 0, sizeof(zones_size)); 52 + 53 + pagetable_init(); 54 + pgd_current = swapper_pg_dir; 55 + 56 + zones_size[ZONE_NORMAL] = max_mapnr; 57 + 58 + /* pass the memory from the bootmem allocator to the main allocator */ 59 + free_area_init(zones_size); 60 + 61 + flush_dcache_range((unsigned long)empty_zero_page, 62 + (unsigned long)empty_zero_page + PAGE_SIZE); 63 + } 64 + 65 + void __init mem_init(void) 66 + { 67 + unsigned long end_mem = memory_end; /* this must not include 68 + kernel stack at top */ 69 + 70 + pr_debug("mem_init: start=%lx, end=%lx\n", memory_start, memory_end); 71 + 72 + end_mem &= PAGE_MASK; 73 + high_memory = __va(end_mem); 74 + 75 + /* this will put all memory onto the freelists */ 76 + free_all_bootmem(); 77 + mem_init_print_info(NULL); 78 + } 79 + 80 + void __init mmu_init(void) 81 + { 82 + flush_tlb_all(); 83 + } 84 + 85 + #ifdef CONFIG_BLK_DEV_INITRD 86 + void __init free_initrd_mem(unsigned long start, unsigned long end) 87 + { 88 + free_reserved_area((void *)start, (void *)end, -1, "initrd"); 89 + } 90 + #endif 91 + 92 + void __init_refok free_initmem(void) 93 + { 94 + free_initmem_default(-1); 95 + } 96 + 97 + #define __page_aligned(order) __aligned(PAGE_SIZE << (order)) 98 + pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned(PGD_ORDER); 99 + pte_t invalid_pte_table[PTRS_PER_PTE] __page_aligned(PTE_ORDER); 100 + static struct page *kuser_page[1]; 101 + 102 + static int alloc_kuser_page(void) 103 + { 104 + extern char __kuser_helper_start[], __kuser_helper_end[]; 105 + int kuser_sz = __kuser_helper_end - __kuser_helper_start; 106 + unsigned long vpage; 107 + 108 + vpage = get_zeroed_page(GFP_ATOMIC); 109 + if (!vpage) 110 + return -ENOMEM; 111 + 112 + /* Copy kuser helpers */ 113 + memcpy((void *)vpage, __kuser_helper_start, kuser_sz); 114 + 115 + flush_icache_range(vpage, vpage + KUSER_SIZE); 116 + kuser_page[0] = virt_to_page(vpage); 117 + 118 + return 0; 119 + } 120 + arch_initcall(alloc_kuser_page); 121 + 122 + int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp) 123 + { 124 + struct mm_struct *mm = current->mm; 125 + int ret; 126 + 127 + down_write(&mm->mmap_sem); 128 + 129 + /* Map kuser helpers to user space address */ 130 + ret = install_special_mapping(mm, KUSER_BASE, KUSER_SIZE, 131 + VM_READ | VM_EXEC | VM_MAYREAD | 132 + VM_MAYEXEC, kuser_page); 133 + 134 + up_write(&mm->mmap_sem); 135 + 136 + return ret; 137 + } 138 + 139 + const char *arch_vma_name(struct vm_area_struct *vma) 140 + { 141 + return (vma->vm_start == KUSER_BASE) ? "[kuser]" : NULL; 142 + }
+163
arch/nios2/mm/uaccess.c
··· 1 + /* 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 + * 6 + * Copyright (C) 2009, Wind River Systems Inc 7 + * Implemented by fredrik.markstrom@gmail.com and ivarholmqvist@gmail.com 8 + */ 9 + 10 + #include <linux/export.h> 11 + #include <linux/uaccess.h> 12 + 13 + asm(".global __copy_from_user\n" 14 + " .type __copy_from_user, @function\n" 15 + "__copy_from_user:\n" 16 + " movi r2,7\n" 17 + " mov r3,r4\n" 18 + " bge r2,r6,1f\n" 19 + " xor r2,r4,r5\n" 20 + " andi r2,r2,3\n" 21 + " movi r7,3\n" 22 + " beq r2,zero,4f\n" 23 + "1: addi r6,r6,-1\n" 24 + " movi r2,-1\n" 25 + " beq r6,r2,3f\n" 26 + " mov r7,r2\n" 27 + "2: ldbu r2,0(r5)\n" 28 + " addi r6,r6,-1\n" 29 + " addi r5,r5,1\n" 30 + " stb r2,0(r3)\n" 31 + " addi r3,r3,1\n" 32 + " bne r6,r7,2b\n" 33 + "3:\n" 34 + " addi r2,r6,1\n" 35 + " ret\n" 36 + "13:mov r2,r6\n" 37 + " ret\n" 38 + "4: andi r2,r4,1\n" 39 + " cmpeq r2,r2,zero\n" 40 + " beq r2,zero,7f\n" 41 + "5: andi r2,r3,2\n" 42 + " beq r2,zero,6f\n" 43 + "9: ldhu r2,0(r5)\n" 44 + " addi r6,r6,-2\n" 45 + " addi r5,r5,2\n" 46 + " sth r2,0(r3)\n" 47 + " addi r3,r3,2\n" 48 + "6: bge r7,r6,1b\n" 49 + "10:ldw r2,0(r5)\n" 50 + " addi r6,r6,-4\n" 51 + " addi r5,r5,4\n" 52 + " stw r2,0(r3)\n" 53 + " addi r3,r3,4\n" 54 + " br 6b\n" 55 + "7: ldbu r2,0(r5)\n" 56 + " addi r6,r6,-1\n" 57 + " addi r5,r5,1\n" 58 + " addi r3,r4,1\n" 59 + " stb r2,0(r4)\n" 60 + " br 5b\n" 61 + ".section __ex_table,\"a\"\n" 62 + ".word 2b,3b\n" 63 + ".word 9b,13b\n" 64 + ".word 10b,13b\n" 65 + ".word 7b,13b\n" 66 + ".previous\n" 67 + ); 68 + EXPORT_SYMBOL(__copy_from_user); 69 + 70 + asm( 71 + " .global __copy_to_user\n" 72 + " .type __copy_to_user, @function\n" 73 + "__copy_to_user:\n" 74 + " movi r2,7\n" 75 + " mov r3,r4\n" 76 + " bge r2,r6,1f\n" 77 + " xor r2,r4,r5\n" 78 + " andi r2,r2,3\n" 79 + " movi r7,3\n" 80 + " beq r2,zero,4f\n" 81 + /* Bail if we try to copy zero bytes */ 82 + "1: addi r6,r6,-1\n" 83 + " movi r2,-1\n" 84 + " beq r6,r2,3f\n" 85 + /* Copy byte by byte for small copies and if src^dst != 0 */ 86 + " mov r7,r2\n" 87 + "2: ldbu r2,0(r5)\n" 88 + " addi r5,r5,1\n" 89 + "9: stb r2,0(r3)\n" 90 + " addi r6,r6,-1\n" 91 + " addi r3,r3,1\n" 92 + " bne r6,r7,2b\n" 93 + "3: addi r2,r6,1\n" 94 + " ret\n" 95 + "13:mov r2,r6\n" 96 + " ret\n" 97 + /* If 'to' is an odd address byte copy */ 98 + "4: andi r2,r4,1\n" 99 + " cmpeq r2,r2,zero\n" 100 + " beq r2,zero,7f\n" 101 + /* If 'to' is not divideable by four copy halfwords */ 102 + "5: andi r2,r3,2\n" 103 + " beq r2,zero,6f\n" 104 + " ldhu r2,0(r5)\n" 105 + " addi r5,r5,2\n" 106 + "10:sth r2,0(r3)\n" 107 + " addi r6,r6,-2\n" 108 + " addi r3,r3,2\n" 109 + /* Copy words */ 110 + "6: bge r7,r6,1b\n" 111 + " ldw r2,0(r5)\n" 112 + " addi r5,r5,4\n" 113 + "11:stw r2,0(r3)\n" 114 + " addi r6,r6,-4\n" 115 + " addi r3,r3,4\n" 116 + " br 6b\n" 117 + /* Copy remaining bytes */ 118 + "7: ldbu r2,0(r5)\n" 119 + " addi r5,r5,1\n" 120 + " addi r3,r4,1\n" 121 + "12: stb r2,0(r4)\n" 122 + " addi r6,r6,-1\n" 123 + " br 5b\n" 124 + ".section __ex_table,\"a\"\n" 125 + ".word 9b,3b\n" 126 + ".word 10b,13b\n" 127 + ".word 11b,13b\n" 128 + ".word 12b,13b\n" 129 + ".previous\n"); 130 + EXPORT_SYMBOL(__copy_to_user); 131 + 132 + long strncpy_from_user(char *__to, const char __user *__from, long __len) 133 + { 134 + int l = strnlen_user(__from, __len); 135 + int is_zt = 1; 136 + 137 + if (l > __len) { 138 + is_zt = 0; 139 + l = __len; 140 + } 141 + 142 + if (l == 0 || copy_from_user(__to, __from, l)) 143 + return -EFAULT; 144 + 145 + if (is_zt) 146 + l--; 147 + return l; 148 + } 149 + 150 + long strnlen_user(const char __user *s, long n) 151 + { 152 + long i; 153 + 154 + for (i = 0; i < n; i++) { 155 + char c; 156 + 157 + if (get_user(c, s + i) == -EFAULT) 158 + return 0; 159 + if (c == 0) 160 + return i + 1; 161 + } 162 + return n + 1; 163 + }