Rev 1288 | Rev 1702 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1288 | jermar | 1 | /* |
| 2 | * Copyright (C) 2006 Jakub Jermar |
||
| 3 | * All rights reserved. |
||
| 4 | * |
||
| 5 | * Redistribution and use in source and binary forms, with or without |
||
| 6 | * modification, are permitted provided that the following conditions |
||
| 7 | * are met: |
||
| 8 | * |
||
| 9 | * - Redistributions of source code must retain the above copyright |
||
| 10 | * notice, this list of conditions and the following disclaimer. |
||
| 11 | * - Redistributions in binary form must reproduce the above copyright |
||
| 12 | * notice, this list of conditions and the following disclaimer in the |
||
| 13 | * documentation and/or other materials provided with the distribution. |
||
| 14 | * - The name of the author may not be used to endorse or promote products |
||
| 15 | * derived from this software without specific prior written permission. |
||
| 16 | * |
||
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
| 27 | */ |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @file copy.c |
||
| 31 | * @brief Copying between kernel and userspace. |
||
| 32 | * |
||
| 33 | * This file contains sanitized functions for copying data |
||
| 34 | * between kernel and userspace. |
||
| 35 | */ |
||
| 36 | |||
| 37 | #include <syscall/copy.h> |
||
| 38 | #include <proc/thread.h> |
||
| 39 | #include <mm/as.h> |
||
| 40 | #include <macros.h> |
||
| 41 | #include <arch.h> |
||
| 42 | #include <errno.h> |
||
| 43 | #include <typedefs.h> |
||
| 44 | |||
| 45 | /** Copy data from userspace to kernel. |
||
| 46 | * |
||
| 47 | * Provisions are made to return value even after page fault. |
||
| 48 | * |
||
| 49 | * This function can be called only from syscall. |
||
| 50 | * |
||
| 51 | * @param dst Destination kernel address. |
||
| 52 | * @param uspace_src Source userspace address. |
||
| 53 | * @param size Size of the data to be copied. |
||
| 54 | * |
||
| 55 | * @return 0 on success or error code from @ref errno.h. |
||
| 56 | */ |
||
| 1293 | palkovsky | 57 | int copy_from_uspace(void *dst, const void *uspace_src, size_t size) |
| 1288 | jermar | 58 | { |
| 59 | ipl_t ipl; |
||
| 60 | int rc; |
||
| 61 | |||
| 62 | ASSERT(THREAD); |
||
| 63 | ASSERT(!THREAD->in_copy_from_uspace); |
||
| 64 | |||
| 65 | if (!KERNEL_ADDRESS_SPACE_SHADOWED) { |
||
| 66 | if (overlaps((__address) uspace_src, size, |
||
| 67 | KERNEL_ADDRESS_SPACE_START, KERNEL_ADDRESS_SPACE_END-KERNEL_ADDRESS_SPACE_START)) { |
||
| 68 | /* |
||
| 69 | * The userspace source block conflicts with kernel address space. |
||
| 70 | */ |
||
| 71 | return EPERM; |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | ipl = interrupts_disable(); |
||
| 76 | THREAD->in_copy_from_uspace = true; |
||
| 77 | |||
| 78 | rc = memcpy_from_uspace(dst, uspace_src, size); |
||
| 79 | |||
| 80 | THREAD->in_copy_from_uspace = false; |
||
| 81 | |||
| 82 | interrupts_restore(ipl); |
||
| 83 | return !rc ? EPERM : 0; |
||
| 84 | } |
||
| 85 | |||
| 86 | /** Copy data from kernel to userspace. |
||
| 87 | * |
||
| 88 | * Provisions are made to return value even after page fault. |
||
| 89 | * |
||
| 90 | * This function can be called only from syscall. |
||
| 91 | * |
||
| 92 | * @param uspace_dst Destination userspace address. |
||
| 93 | * @param uspace_src Source kernel address. |
||
| 94 | * @param size Size of the data to be copied. |
||
| 95 | * |
||
| 96 | * @return 0 on success or error code from @ref errno.h. |
||
| 97 | */ |
||
| 1293 | palkovsky | 98 | int copy_to_uspace(void *uspace_dst, const void *src, size_t size) |
| 1288 | jermar | 99 | { |
| 100 | ipl_t ipl; |
||
| 101 | int rc; |
||
| 102 | |||
| 103 | ASSERT(THREAD); |
||
| 104 | ASSERT(!THREAD->in_copy_from_uspace); |
||
| 105 | |||
| 106 | if (!KERNEL_ADDRESS_SPACE_SHADOWED) { |
||
| 107 | if (overlaps((__address) uspace_dst, size, |
||
| 108 | KERNEL_ADDRESS_SPACE_START, KERNEL_ADDRESS_SPACE_END-KERNEL_ADDRESS_SPACE_START)) { |
||
| 109 | /* |
||
| 110 | * The userspace destination block conflicts with kernel address space. |
||
| 111 | */ |
||
| 112 | return EPERM; |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | ipl = interrupts_disable(); |
||
| 117 | THREAD->in_copy_from_uspace = true; |
||
| 118 | |||
| 119 | rc = memcpy_to_uspace(uspace_dst, src, size); |
||
| 120 | |||
| 121 | THREAD->in_copy_from_uspace = false; |
||
| 122 | |||
| 123 | interrupts_restore(ipl); |
||
| 124 | return !rc ? EPERM : 0; |
||
| 125 | } |