Rev 4343 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4343 | Rev 4344 | ||
---|---|---|---|
Line 27... | Line 27... | ||
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup genericddi |
29 | /** @addtogroup genericddi |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** |
33 | /** |
34 | * @file |
34 | * @file |
35 | * @brief Device Driver Interface functions. |
35 | * @brief Device Driver Interface functions. |
36 | * |
36 | * |
37 | * This file contains functions that comprise the Device Driver Interface. |
37 | * This file contains functions that comprise the Device Driver Interface. |
38 | * These are the functions for mapping physical memory and enabling I/O |
38 | * These are the functions for mapping physical memory and enabling I/O |
39 | * space to tasks. |
39 | * space to tasks. |
40 | */ |
40 | */ |
Line 45... | Line 45... | ||
45 | #include <security/cap.h> |
45 | #include <security/cap.h> |
46 | #include <mm/frame.h> |
46 | #include <mm/frame.h> |
47 | #include <mm/as.h> |
47 | #include <mm/as.h> |
48 | #include <synch/spinlock.h> |
48 | #include <synch/spinlock.h> |
49 | #include <syscall/copy.h> |
49 | #include <syscall/copy.h> |
50 | #include <adt/list.h> |
50 | #include <adt/btree.h> |
51 | #include <arch.h> |
51 | #include <arch.h> |
52 | #include <align.h> |
52 | #include <align.h> |
53 | #include <errno.h> |
53 | #include <errno.h> |
54 | 54 | ||
55 | /** This lock protects the parea_btree. */ |
55 | /** This lock protects the parea_btree. */ |
56 | SPINLOCK_INITIALIZE(parea_lock); |
56 | SPINLOCK_INITIALIZE(parea_lock); |
57 | 57 | ||
58 | /** List with enabled physical memory areas. */ |
58 | /** B+tree with enabled physical memory areas. */ |
59 | static LIST_INITIALIZE(parea_head); |
- | |
60 | - | ||
61 | /** Physical memory area for devices. */ |
- | |
62 | static parea_t dev_area; |
59 | static btree_t parea_btree; |
63 | 60 | ||
64 | /** Initialize DDI. */ |
61 | /** Initialize DDI. */ |
65 | void ddi_init(void) |
62 | void ddi_init(void) |
66 | { |
63 | { |
67 | hw_area(&dev_area.pbase, &dev_area.frames); |
- | |
68 | ddi_parea_register(&dev_area); |
64 | btree_create(&parea_btree); |
69 | } |
65 | } |
70 | 66 | ||
71 | /** Enable piece of physical memory for mapping by physmem_map(). |
67 | /** Enable piece of physical memory for mapping by physmem_map(). |
72 | * |
68 | * |
73 | * @param parea Pointer to physical area structure. |
69 | * @param parea Pointer to physical area structure. |
74 | * |
70 | * |
75 | * @todo This function doesn't check for overlaps. It depends on the kernel to |
- | |
76 | * create disjunct physical memory areas. |
- | |
77 | */ |
71 | */ |
78 | void ddi_parea_register(parea_t *parea) |
72 | void ddi_parea_register(parea_t *parea) |
79 | { |
73 | { |
80 | ipl_t ipl; |
- | |
81 | - | ||
82 | ipl = interrupts_disable(); |
74 | ipl_t ipl = interrupts_disable(); |
83 | spinlock_lock(&parea_lock); |
75 | spinlock_lock(&parea_lock); |
84 | 76 | ||
85 | /* |
77 | /* |
86 | * TODO: we should really check for overlaps here. |
- | |
87 | * However, we should be safe because the kernel is pretty sane. |
78 | * We don't check for overlaps here as the kernel is pretty sane. |
88 | */ |
79 | */ |
89 | link_initialize(&parea->link); |
- | |
90 | list_append(&parea->link, &parea_head); |
80 | btree_insert(&parea_btree, (btree_key_t) parea->pbase, parea, NULL); |
91 | 81 | ||
92 | spinlock_unlock(&parea_lock); |
82 | spinlock_unlock(&parea_lock); |
93 | interrupts_restore(ipl); |
83 | interrupts_restore(ipl); |
94 | } |
84 | } |
95 | 85 | ||
96 | /** Map piece of physical memory into virtual address space of current task. |
86 | /** Map piece of physical memory into virtual address space of current task. |
97 | * |
87 | * |
98 | * @param pf Physical address of the starting frame. |
88 | * @param pf Physical address of the starting frame. |
99 | * @param vp Virtual address of the starting page. |
89 | * @param vp Virtual address of the starting page. |
100 | * @param pages Number of pages to map. |
90 | * @param pages Number of pages to map. |
101 | * @param flags Address space area flags for the mapping. |
91 | * @param flags Address space area flags for the mapping. |
102 | * |
92 | * |
103 | * @return 0 on success, EPERM if the caller lacks capabilities to use this |
93 | * @return 0 on success, EPERM if the caller lacks capabilities to use this |
- | 94 | * syscall, EBADMEM if pf or vf is not page aligned, ENOENT if there |
|
104 | * syscall, ENOENT if there is no task matching the specified ID or the |
95 | * is no task matching the specified ID or the physical address space |
105 | * physical address space is not enabled for mapping and ENOMEM if there |
96 | * is not enabled for mapping and ENOMEM if there was a problem in |
106 | * was a problem in creating address space area. |
97 | * creating address space area. |
- | 98 | * |
|
107 | */ |
99 | */ |
108 | static int ddi_physmem_map(uintptr_t pf, uintptr_t vp, pfn_t pages, int flags) |
100 | static int ddi_physmem_map(uintptr_t pf, uintptr_t vp, count_t pages, int flags) |
109 | { |
101 | { |
110 | ipl_t ipl; |
- | |
111 | cap_t caps; |
102 | ASSERT(TASK); |
112 | mem_backend_data_t backend_data; |
103 | ASSERT((pf % FRAME_SIZE) == 0); |
113 | - | ||
114 | backend_data.base = pf; |
- | |
115 | backend_data.frames = pages; |
104 | ASSERT((vp % PAGE_SIZE) == 0); |
116 | 105 | ||
117 | /* |
106 | /* |
118 | * Make sure the caller is authorised to make this syscall. |
107 | * Make sure the caller is authorised to make this syscall. |
119 | */ |
108 | */ |
120 | caps = cap_get(TASK); |
109 | cap_t caps = cap_get(TASK); |
121 | if (!(caps & CAP_MEM_MANAGER)) |
110 | if (!(caps & CAP_MEM_MANAGER)) |
122 | return EPERM; |
111 | return EPERM; |
123 | 112 | ||
124 | ipl = interrupts_disable(); |
113 | mem_backend_data_t backend_data; |
125 | - | ||
126 | /* |
- | |
127 | * Check if the physical memory area is enabled for mapping. |
114 | backend_data.base = pf; |
128 | */ |
- | |
129 | spinlock_lock(&parea_lock); |
115 | backend_data.frames = pages; |
130 | 116 | ||
131 | bool fnd = false; |
117 | ipl_t ipl = interrupts_disable(); |
132 | link_t *cur; |
- | |
133 | 118 | ||
134 | for (cur = parea_head.next; cur != &parea_head; cur = cur->next) { |
119 | /* Find the zone of the physical memory */ |
135 | parea_t *parea = list_get_instance(cur, parea_t, link); |
120 | spinlock_lock(&zones.lock); |
136 | if ((parea->pbase <= pf) && (ADDR2PFN(pf - parea->pbase) + pages <= parea->frames)) { |
121 | count_t znum = find_zone(ADDR2PFN(pf), pages, 0); |
- | 122 | ||
137 | fnd = true; |
123 | if (znum == (count_t) -1) { |
138 | break; |
124 | /* Frames not found in any zones |
- | 125 | * -> assume it is hardware device and allow mapping |
|
139 | } |
126 | */ |
- | 127 | spinlock_unlock(&zones.lock); |
|
- | 128 | goto map; |
|
140 | } |
129 | } |
141 | 130 | ||
- | 131 | if (zones.info[znum].flags & ZONE_FIRMWARE) { |
|
- | 132 | /* Frames are part of firmware */ |
|
142 | spinlock_unlock(&parea_lock); |
133 | spinlock_unlock(&zones.lock); |
- | 134 | goto map; |
|
- | 135 | } |
|
143 | 136 | ||
144 | if (!fnd) { |
137 | if (zone_flags_available(zones.info[znum].flags)) { |
145 | /* |
- | |
- | 138 | /* Frames are part of physical memory, check if the memory |
|
146 | * Physical memory area cannot be mapped. |
139 | * region is enabled for mapping. |
147 | */ |
140 | */ |
- | 141 | spinlock_unlock(&zones.lock); |
|
- | 142 | ||
- | 143 | spinlock_lock(&parea_lock); |
|
148 | interrupts_restore(ipl); |
144 | btree_node_t *nodep; |
- | 145 | parea_t *parea = (parea_t *) btree_search(&parea_btree, |
|
- | 146 | (btree_key_t) pf, &nodep); |
|
- | 147 | ||
- | 148 | if ((!parea) || (parea->frames < pages)) |
|
- | 149 | goto err; |
|
- | 150 | ||
- | 151 | spinlock_unlock(&parea_lock); |
|
149 | return ENOENT; |
152 | goto map; |
150 | } |
153 | } |
151 | 154 | ||
- | 155 | err: |
|
- | 156 | spinlock_unlock(&zones.lock); |
|
- | 157 | interrupts_restore(ipl); |
|
- | 158 | return ENOENT; |
|
- | 159 | ||
- | 160 | map: |
|
152 | spinlock_lock(&TASK->lock); |
161 | spinlock_lock(&TASK->lock); |
153 | 162 | ||
154 | if (!as_area_create(TASK->as, flags, pages * PAGE_SIZE, vp, AS_AREA_ATTR_NONE, |
163 | if (!as_area_create(TASK->as, flags, pages * PAGE_SIZE, vp, |
155 | &phys_backend, &backend_data)) { |
164 | AS_AREA_ATTR_NONE, &phys_backend, &backend_data)) { |
156 | /* |
165 | /* |
157 | * The address space area could not have been created. |
166 | * The address space area could not have been created. |
158 | * We report it using ENOMEM. |
167 | * We report it using ENOMEM. |
159 | */ |
168 | */ |
160 | spinlock_unlock(&TASK->lock); |
169 | spinlock_unlock(&TASK->lock); |
Line 176... | Line 185... | ||
176 | * @param id Task ID of the destination task. |
185 | * @param id Task ID of the destination task. |
177 | * @param ioaddr Starting I/O address. |
186 | * @param ioaddr Starting I/O address. |
178 | * @param size Size of the enabled I/O space.. |
187 | * @param size Size of the enabled I/O space.. |
179 | * |
188 | * |
180 | * @return 0 on success, EPERM if the caller lacks capabilities to use this |
189 | * @return 0 on success, EPERM if the caller lacks capabilities to use this |
181 | * syscall, ENOENT if there is no task matching the specified ID. |
190 | * syscall, ENOENT if there is no task matching the specified ID. |
- | 191 | * |
|
182 | */ |
192 | */ |
183 | static int ddi_iospace_enable(task_id_t id, uintptr_t ioaddr, size_t size) |
193 | static int ddi_iospace_enable(task_id_t id, uintptr_t ioaddr, size_t size) |
184 | { |
194 | { |
185 | ipl_t ipl; |
- | |
186 | cap_t caps; |
- | |
187 | task_t *t; |
- | |
188 | int rc; |
- | |
189 | - | ||
190 | /* |
195 | /* |
191 | * Make sure the caller is authorised to make this syscall. |
196 | * Make sure the caller is authorised to make this syscall. |
192 | */ |
197 | */ |
193 | caps = cap_get(TASK); |
198 | cap_t caps = cap_get(TASK); |
194 | if (!(caps & CAP_IO_MANAGER)) |
199 | if (!(caps & CAP_IO_MANAGER)) |
195 | return EPERM; |
200 | return EPERM; |
196 | 201 | ||
197 | ipl = interrupts_disable(); |
202 | ipl_t ipl = interrupts_disable(); |
198 | spinlock_lock(&tasks_lock); |
203 | spinlock_lock(&tasks_lock); |
199 | 204 | ||
200 | t = task_find_by_id(id); |
205 | task_t *task = task_find_by_id(id); |
201 | 206 | ||
202 | if ((!t) || (!context_check(CONTEXT, t->context))) { |
207 | if ((!task) || (!context_check(CONTEXT, task->context))) { |
203 | /* |
208 | /* |
204 | * There is no task with the specified ID |
209 | * There is no task with the specified ID |
205 | * or the task belongs to a different security |
210 | * or the task belongs to a different security |
206 | * context. |
211 | * context. |
207 | */ |
212 | */ |
208 | spinlock_unlock(&tasks_lock); |
213 | spinlock_unlock(&tasks_lock); |
209 | interrupts_restore(ipl); |
214 | interrupts_restore(ipl); |
210 | return ENOENT; |
215 | return ENOENT; |
211 | } |
216 | } |
212 | 217 | ||
213 | /* Lock the task and release the lock protecting tasks_btree. */ |
218 | /* Lock the task and release the lock protecting tasks_btree. */ |
214 | spinlock_lock(&t->lock); |
219 | spinlock_lock(&task->lock); |
215 | spinlock_unlock(&tasks_lock); |
220 | spinlock_unlock(&tasks_lock); |
216 | - | ||
217 | rc = ddi_iospace_enable_arch(t, ioaddr, size); |
- | |
218 | 221 | ||
- | 222 | int rc = ddi_iospace_enable_arch(task, ioaddr, size); |
|
- | 223 | ||
219 | spinlock_unlock(&t->lock); |
224 | spinlock_unlock(&task->lock); |
220 | interrupts_restore(ipl); |
225 | interrupts_restore(ipl); |
- | 226 | ||
221 | return rc; |
227 | return rc; |
222 | } |
228 | } |
223 | 229 | ||
224 | /** Wrapper for SYS_PHYSMEM_MAP syscall. |
230 | /** Wrapper for SYS_PHYSMEM_MAP syscall. |
225 | * |
231 | * |
Line 227... | Line 233... | ||
227 | * @param virt_base Destination virtual address |
233 | * @param virt_base Destination virtual address |
228 | * @param pages Number of pages |
234 | * @param pages Number of pages |
229 | * @param flags Flags of newly mapped pages |
235 | * @param flags Flags of newly mapped pages |
230 | * |
236 | * |
231 | * @return 0 on success, otherwise it returns error code found in errno.h |
237 | * @return 0 on success, otherwise it returns error code found in errno.h |
- | 238 | * |
|
232 | */ |
239 | */ |
233 | unative_t sys_physmem_map(unative_t phys_base, unative_t virt_base, |
240 | unative_t sys_physmem_map(unative_t phys_base, unative_t virt_base, |
234 | unative_t pages, unative_t flags) |
241 | unative_t pages, unative_t flags) |
235 | { |
242 | { |
236 | return (unative_t) ddi_physmem_map(ALIGN_DOWN((uintptr_t) phys_base, |
243 | return (unative_t) ddi_physmem_map(ALIGN_DOWN((uintptr_t) phys_base, |
237 | FRAME_SIZE), ALIGN_DOWN((uintptr_t) virt_base, PAGE_SIZE), |
244 | FRAME_SIZE), ALIGN_DOWN((uintptr_t) virt_base, PAGE_SIZE), |
238 | (pfn_t) pages, (int) flags); |
245 | (count_t) pages, (int) flags); |
239 | } |
246 | } |
240 | 247 | ||
241 | /** Wrapper for SYS_ENABLE_IOSPACE syscall. |
248 | /** Wrapper for SYS_ENABLE_IOSPACE syscall. |
242 | * |
249 | * |
243 | * @param uspace_io_arg User space address of DDI argument structure. |
250 | * @param uspace_io_arg User space address of DDI argument structure. |
244 | * |
251 | * |
245 | * @return 0 on success, otherwise it returns error code found in errno.h |
252 | * @return 0 on success, otherwise it returns error code found in errno.h |
- | 253 | * |
|
246 | */ |
254 | */ |
247 | unative_t sys_iospace_enable(ddi_ioarg_t *uspace_io_arg) |
255 | unative_t sys_iospace_enable(ddi_ioarg_t *uspace_io_arg) |
248 | { |
256 | { |
249 | ddi_ioarg_t arg; |
257 | ddi_ioarg_t arg; |
250 | int rc; |
- | |
251 | - | ||
252 | rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t)); |
258 | int rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t)); |
253 | if (rc != 0) |
259 | if (rc != 0) |
254 | return (unative_t) rc; |
260 | return (unative_t) rc; |
255 | 261 | ||
256 | return (unative_t) ddi_iospace_enable((task_id_t) arg.task_id, |
262 | return (unative_t) ddi_iospace_enable((task_id_t) arg.task_id, |
257 | (uintptr_t) arg.ioaddr, (size_t) arg.size); |
263 | (uintptr_t) arg.ioaddr, (size_t) arg.size); |
258 | } |
264 | } |
259 | 265 | ||
260 | /** Disable or enable preemption. |
266 | /** Disable or enable preemption. |
261 | * |
267 | * |
262 | * @param enable If non-zero, the preemption counter will be decremented, |
268 | * @param enable If non-zero, the preemption counter will be decremented, |
263 | * leading to potential enabling of preemption. Otherwise the preemption |
269 | * leading to potential enabling of preemption. Otherwise |
- | 270 | * the preemption counter will be incremented, preventing |
|
264 | * counter will be incremented, preventing preemption from occurring. |
271 | * preemption from occurring. |
265 | * |
272 | * |
266 | * @return Zero on success or EPERM if callers capabilities are not sufficient. |
273 | * @return Zero on success or EPERM if callers capabilities are not sufficient. |
- | 274 | * |
|
267 | */ |
275 | */ |
268 | unative_t sys_preempt_control(int enable) |
276 | unative_t sys_preempt_control(int enable) |
269 | { |
277 | { |
270 | if (!cap_get(TASK) & CAP_PREEMPT_CONTROL) |
278 | if (!cap_get(TASK) & CAP_PREEMPT_CONTROL) |
271 | return EPERM; |
279 | return EPERM; |
- | 280 | ||
272 | if (enable) |
281 | if (enable) |
273 | preemption_enable(); |
282 | preemption_enable(); |
274 | else |
283 | else |
275 | preemption_disable(); |
284 | preemption_disable(); |
- | 285 | ||
276 | return 0; |
286 | return 0; |
277 | } |
287 | } |
278 | 288 | ||
279 | /** @} |
289 | /** @} |
280 | */ |
290 | */ |