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