Rev 1589 | Rev 1705 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1589 | Rev 1702 | ||
---|---|---|---|
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 | ||
- | 29 | /** @defgroup ddi Device Driver Interface |
|
- | 30 | * @ingroup kernel |
|
- | 31 | * @{ |
|
- | 32 | * @} |
|
- | 33 | */ |
|
- | 34 | ||
- | 35 | /** @addtogroup genericddi generic |
|
- | 36 | * @ingroup ddi |
|
- | 37 | * @{ |
|
- | 38 | */ |
|
28 | 39 | ||
29 | /** |
40 | /** |
30 | * @file ddi.c |
41 | * @file |
31 | * @brief Device Driver Interface functions. |
42 | * @brief Device Driver Interface functions. |
32 | * |
43 | * |
33 | * This file contains functions that comprise the Device Driver Interface. |
44 | * This file contains functions that comprise the Device Driver Interface. |
34 | * These are the functions for mapping physical memory and enabling I/O |
45 | * These are the functions for mapping physical memory and enabling I/O |
35 | * space to tasks. |
46 | * space to tasks. |
36 | */ |
47 | */ |
37 | 48 | ||
38 | #include <ddi/ddi.h> |
49 | #include <ddi/ddi.h> |
39 | #include <ddi/ddi_arg.h> |
50 | #include <ddi/ddi_arg.h> |
40 | #include <proc/task.h> |
51 | #include <proc/task.h> |
41 | #include <security/cap.h> |
52 | #include <security/cap.h> |
42 | #include <mm/frame.h> |
53 | #include <mm/frame.h> |
43 | #include <mm/as.h> |
54 | #include <mm/as.h> |
44 | #include <synch/spinlock.h> |
55 | #include <synch/spinlock.h> |
45 | #include <syscall/copy.h> |
56 | #include <syscall/copy.h> |
46 | #include <arch.h> |
57 | #include <arch.h> |
47 | #include <align.h> |
58 | #include <align.h> |
48 | #include <errno.h> |
59 | #include <errno.h> |
49 | 60 | ||
50 | /** Map piece of physical memory into virtual address space of current task. |
61 | /** Map piece of physical memory into virtual address space of current task. |
51 | * |
62 | * |
52 | * @param pf Physical frame address of the starting frame. |
63 | * @param pf Physical frame address of the starting frame. |
53 | * @param vp Virtual page address of the starting page. |
64 | * @param vp Virtual page address of the starting page. |
54 | * @param pages Number of pages to map. |
65 | * @param pages Number of pages to map. |
55 | * @param flags Address space area flags for the mapping. |
66 | * @param flags Address space area flags for the mapping. |
56 | * |
67 | * |
57 | * @return 0 on success, EPERM if the caller lacks capabilities to use this syscall, |
68 | * @return 0 on success, EPERM if the caller lacks capabilities to use this syscall, |
58 | * ENOENT if there is no task matching the specified ID and ENOMEM if |
69 | * ENOENT if there is no task matching the specified ID and ENOMEM if |
59 | * there was a problem in creating address space area. |
70 | * there was a problem in creating address space area. |
60 | */ |
71 | */ |
61 | static int ddi_physmem_map(__address pf, __address vp, count_t pages, int flags) |
72 | static int ddi_physmem_map(__address pf, __address vp, count_t pages, int flags) |
62 | { |
73 | { |
63 | ipl_t ipl; |
74 | ipl_t ipl; |
64 | cap_t caps; |
75 | cap_t caps; |
65 | mem_backend_data_t backend_data; |
76 | mem_backend_data_t backend_data; |
66 | 77 | ||
67 | backend_data.base = pf; |
78 | backend_data.base = pf; |
68 | backend_data.frames = pages; |
79 | backend_data.frames = pages; |
69 | 80 | ||
70 | /* |
81 | /* |
71 | * Make sure the caller is authorised to make this syscall. |
82 | * Make sure the caller is authorised to make this syscall. |
72 | */ |
83 | */ |
73 | caps = cap_get(TASK); |
84 | caps = cap_get(TASK); |
74 | if (!(caps & CAP_MEM_MANAGER)) |
85 | if (!(caps & CAP_MEM_MANAGER)) |
75 | return EPERM; |
86 | return EPERM; |
76 | 87 | ||
77 | ipl = interrupts_disable(); |
88 | ipl = interrupts_disable(); |
78 | spinlock_lock(&TASK->lock); |
89 | spinlock_lock(&TASK->lock); |
79 | 90 | ||
80 | if (!as_area_create(TASK->as, flags, pages * PAGE_SIZE, vp, AS_AREA_ATTR_NONE, |
91 | if (!as_area_create(TASK->as, flags, pages * PAGE_SIZE, vp, AS_AREA_ATTR_NONE, |
81 | &phys_backend, &backend_data)) { |
92 | &phys_backend, &backend_data)) { |
82 | /* |
93 | /* |
83 | * The address space area could not have been created. |
94 | * The address space area could not have been created. |
84 | * We report it using ENOMEM. |
95 | * We report it using ENOMEM. |
85 | */ |
96 | */ |
86 | spinlock_unlock(&TASK->lock); |
97 | spinlock_unlock(&TASK->lock); |
87 | interrupts_restore(ipl); |
98 | interrupts_restore(ipl); |
88 | return ENOMEM; |
99 | return ENOMEM; |
89 | } |
100 | } |
90 | 101 | ||
91 | /* |
102 | /* |
92 | * Mapping is created on-demand during page fault. |
103 | * Mapping is created on-demand during page fault. |
93 | */ |
104 | */ |
94 | 105 | ||
95 | spinlock_unlock(&TASK->lock); |
106 | spinlock_unlock(&TASK->lock); |
96 | interrupts_restore(ipl); |
107 | interrupts_restore(ipl); |
97 | return 0; |
108 | return 0; |
98 | } |
109 | } |
99 | 110 | ||
100 | /** Enable range of I/O space for task. |
111 | /** Enable range of I/O space for task. |
101 | * |
112 | * |
102 | * @param id Task ID of the destination task. |
113 | * @param id Task ID of the destination task. |
103 | * @param ioaddr Starting I/O address. |
114 | * @param ioaddr Starting I/O address. |
104 | * @param size Size of the enabled I/O space.. |
115 | * @param size Size of the enabled I/O space.. |
105 | * |
116 | * |
106 | * @return 0 on success, EPERM if the caller lacks capabilities to use this syscall, |
117 | * @return 0 on success, EPERM if the caller lacks capabilities to use this syscall, |
107 | * ENOENT if there is no task matching the specified ID. |
118 | * ENOENT if there is no task matching the specified ID. |
108 | */ |
119 | */ |
109 | static int ddi_iospace_enable(task_id_t id, __address ioaddr, size_t size) |
120 | static int ddi_iospace_enable(task_id_t id, __address ioaddr, size_t size) |
110 | { |
121 | { |
111 | ipl_t ipl; |
122 | ipl_t ipl; |
112 | cap_t caps; |
123 | cap_t caps; |
113 | task_t *t; |
124 | task_t *t; |
114 | int rc; |
125 | int rc; |
115 | 126 | ||
116 | /* |
127 | /* |
117 | * Make sure the caller is authorised to make this syscall. |
128 | * Make sure the caller is authorised to make this syscall. |
118 | */ |
129 | */ |
119 | caps = cap_get(TASK); |
130 | caps = cap_get(TASK); |
120 | if (!(caps & CAP_IO_MANAGER)) |
131 | if (!(caps & CAP_IO_MANAGER)) |
121 | return EPERM; |
132 | return EPERM; |
122 | 133 | ||
123 | ipl = interrupts_disable(); |
134 | ipl = interrupts_disable(); |
124 | spinlock_lock(&tasks_lock); |
135 | spinlock_lock(&tasks_lock); |
125 | 136 | ||
126 | t = task_find_by_id(id); |
137 | t = task_find_by_id(id); |
127 | 138 | ||
128 | if (!t) { |
139 | if (!t) { |
129 | /* |
140 | /* |
130 | * There is no task with the specified ID. |
141 | * There is no task with the specified ID. |
131 | */ |
142 | */ |
132 | spinlock_unlock(&tasks_lock); |
143 | spinlock_unlock(&tasks_lock); |
133 | interrupts_restore(ipl); |
144 | interrupts_restore(ipl); |
134 | return ENOENT; |
145 | return ENOENT; |
135 | } |
146 | } |
136 | 147 | ||
137 | /* Lock the task and release the lock protecting tasks_btree. */ |
148 | /* Lock the task and release the lock protecting tasks_btree. */ |
138 | spinlock_lock(&t->lock); |
149 | spinlock_lock(&t->lock); |
139 | spinlock_unlock(&tasks_lock); |
150 | spinlock_unlock(&tasks_lock); |
140 | 151 | ||
141 | rc = ddi_iospace_enable_arch(t, ioaddr, size); |
152 | rc = ddi_iospace_enable_arch(t, ioaddr, size); |
142 | 153 | ||
143 | spinlock_unlock(&t->lock); |
154 | spinlock_unlock(&t->lock); |
144 | interrupts_restore(ipl); |
155 | interrupts_restore(ipl); |
145 | return rc; |
156 | return rc; |
146 | } |
157 | } |
147 | 158 | ||
148 | /** Wrapper for SYS_MAP_PHYSMEM syscall. |
159 | /** Wrapper for SYS_MAP_PHYSMEM syscall. |
149 | * |
160 | * |
150 | * @param phys_base Physical base address to map |
161 | * @param phys_base Physical base address to map |
151 | * @param virt_base Destination virtual address |
162 | * @param virt_base Destination virtual address |
152 | * @param pages Number of pages |
163 | * @param pages Number of pages |
153 | * @param flags Flags of newly mapped pages |
164 | * @param flags Flags of newly mapped pages |
154 | * |
165 | * |
155 | * @return 0 on success, otherwise it returns error code found in errno.h |
166 | * @return 0 on success, otherwise it returns error code found in errno.h |
156 | */ |
167 | */ |
157 | __native sys_physmem_map(__native phys_base, __native virt_base, __native pages, |
168 | __native sys_physmem_map(__native phys_base, __native virt_base, __native pages, |
158 | __native flags) |
169 | __native flags) |
159 | { |
170 | { |
160 | return (__native) ddi_physmem_map(ALIGN_DOWN((__address) phys_base, FRAME_SIZE), |
171 | return (__native) ddi_physmem_map(ALIGN_DOWN((__address) phys_base, FRAME_SIZE), |
161 | ALIGN_DOWN((__address) virt_base, PAGE_SIZE), (count_t) pages, |
172 | ALIGN_DOWN((__address) virt_base, PAGE_SIZE), (count_t) pages, |
162 | (int) flags); |
173 | (int) flags); |
163 | } |
174 | } |
164 | 175 | ||
165 | /** Wrapper for SYS_ENABLE_IOSPACE syscall. |
176 | /** Wrapper for SYS_ENABLE_IOSPACE syscall. |
166 | * |
177 | * |
167 | * @param User space address of DDI argument structure. |
178 | * @param User space address of DDI argument structure. |
168 | * |
179 | * |
169 | * @return 0 on success, otherwise it returns error code found in errno.h |
180 | * @return 0 on success, otherwise it returns error code found in errno.h |
170 | */ |
181 | */ |
171 | __native sys_iospace_enable(ddi_ioarg_t *uspace_io_arg) |
182 | __native sys_iospace_enable(ddi_ioarg_t *uspace_io_arg) |
172 | { |
183 | { |
173 | ddi_ioarg_t arg; |
184 | ddi_ioarg_t arg; |
174 | int rc; |
185 | int rc; |
175 | 186 | ||
176 | rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t)); |
187 | rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t)); |
177 | if (rc != 0) |
188 | if (rc != 0) |
178 | return (__native) rc; |
189 | return (__native) rc; |
179 | 190 | ||
180 | return (__native) ddi_iospace_enable((task_id_t) arg.task_id, (__address) arg.ioaddr, (size_t) arg.size); |
191 | return (__native) ddi_iospace_enable((task_id_t) arg.task_id, (__address) arg.ioaddr, (size_t) arg.size); |
181 | } |
192 | } |
182 | 193 | ||
183 | /** Disable or enable preemption. |
194 | /** Disable or enable preemption. |
184 | * |
195 | * |
185 | * @param enable If non-zero, the preemption counter will be decremented, leading to potential |
196 | * @param enable If non-zero, the preemption counter will be decremented, leading to potential |
186 | * enabling of preemption. Otherwise the preemption counter will be incremented, |
197 | * enabling of preemption. Otherwise the preemption counter will be incremented, |
187 | * preventing preemption from occurring. |
198 | * preventing preemption from occurring. |
188 | * |
199 | * |
189 | * @return Zero on success or EPERM if callers capabilities are not sufficient. |
200 | * @return Zero on success or EPERM if callers capabilities are not sufficient. |
190 | */ |
201 | */ |
191 | __native sys_preempt_control(int enable) |
202 | __native sys_preempt_control(int enable) |
192 | { |
203 | { |
193 | if (! cap_get(TASK) & CAP_PREEMPT_CONTROL) |
204 | if (! cap_get(TASK) & CAP_PREEMPT_CONTROL) |
194 | return EPERM; |
205 | return EPERM; |
195 | if (enable) |
206 | if (enable) |
196 | preemption_enable(); |
207 | preemption_enable(); |
197 | else |
208 | else |
198 | preemption_disable(); |
209 | preemption_disable(); |
199 | return 0; |
210 | return 0; |
200 | } |
211 | } |
- | 212 | ||
- | 213 | /** @} |
|
- | 214 | */ |
|
- | 215 | ||
201 | 216 |