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