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