Rev 2141 | Rev 3940 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 2141 | Rev 3908 | ||
|---|---|---|---|
| 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/btree.h> |
50 | #include <adt/list.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 | /** B+tree with enabled physical memory areas. */ |
58 | /** List with enabled physical memory areas. */ |
| - | 59 | static LIST_INITIALIZE(parea_head); |
|
| - | 60 | ||
| - | 61 | /** Physical memory area for devices. */ |
|
| 59 | static btree_t parea_btree; |
62 | static parea_t dev_area; |
| 60 | 63 | ||
| 61 | /** Initialize DDI. */ |
64 | /** Initialize DDI. */ |
| 62 | void ddi_init(void) |
65 | void ddi_init(void) |
| 63 | { |
66 | { |
| - | 67 | hw_area(&dev_area.pbase, &dev_area.frames); |
|
| 64 | btree_create(&parea_btree); |
68 | ddi_parea_register(&dev_area); |
| 65 | } |
69 | } |
| 66 | 70 | ||
| 67 | /** Enable piece of physical memory for mapping by physmem_map(). |
71 | /** Enable piece of physical memory for mapping by physmem_map(). |
| 68 | * |
72 | * |
| 69 | * @param parea Pointer to physical area structure. |
73 | * @param parea Pointer to physical area structure. |
| Line 72... | Line 76... | ||
| 72 | * create disjunct physical memory areas. |
76 | * create disjunct physical memory areas. |
| 73 | */ |
77 | */ |
| 74 | void ddi_parea_register(parea_t *parea) |
78 | void ddi_parea_register(parea_t *parea) |
| 75 | { |
79 | { |
| 76 | ipl_t ipl; |
80 | ipl_t ipl; |
| 77 | 81 | ||
| 78 | ipl = interrupts_disable(); |
82 | ipl = interrupts_disable(); |
| 79 | spinlock_lock(&parea_lock); |
83 | spinlock_lock(&parea_lock); |
| 80 | 84 | ||
| 81 | /* |
85 | /* |
| 82 | * TODO: we should really check for overlaps here. |
86 | * TODO: we should really check for overlaps here. |
| 83 | * However, we should be safe because the kernel is pretty sane and |
87 | * However, we should be safe because the kernel is pretty sane. |
| 84 | * memory of different devices doesn't overlap. |
- | |
| 85 | */ |
88 | */ |
| - | 89 | link_initialize(&parea->link); |
|
| 86 | btree_insert(&parea_btree, (btree_key_t) parea->pbase, parea, NULL); |
90 | list_append(&parea->link, &parea_head); |
| 87 | 91 | ||
| 88 | spinlock_unlock(&parea_lock); |
92 | spinlock_unlock(&parea_lock); |
| 89 | interrupts_restore(ipl); |
93 | interrupts_restore(ipl); |
| 90 | } |
94 | } |
| 91 | 95 | ||
| 92 | /** Map piece of physical memory into virtual address space of current task. |
96 | /** Map piece of physical memory into virtual address space of current task. |
| 93 | * |
97 | * |
| 94 | * @param pf Physical address of the starting frame. |
98 | * @param pf Physical address of the starting frame. |
| 95 | * @param vp Virtual address of the starting page. |
99 | * @param vp Virtual address of the starting page. |
| 96 | * @param pages Number of pages to map. |
100 | * @param pages Number of pages to map. |
| 97 | * @param flags Address space area flags for the mapping. |
101 | * @param flags Address space area flags for the mapping. |
| 98 | * |
102 | * |
| 99 | * @return 0 on success, EPERM if the caller lacks capabilities to use this |
103 | * @return 0 on success, EPERM if the caller lacks capabilities to use this |
| 100 | * syscall, ENOENT if there is no task matching the specified ID or the |
104 | * syscall, ENOENT if there is no task matching the specified ID or the |
| 101 | * physical address space is not enabled for mapping and ENOMEM if there |
105 | * physical address space is not enabled for mapping and ENOMEM if there |
| 102 | * was a problem in creating address space area. |
106 | * was a problem in creating address space area. |
| 103 | */ |
107 | */ |
| 104 | static int ddi_physmem_map(uintptr_t pf, uintptr_t vp, count_t pages, int flags) |
108 | static int ddi_physmem_map(uintptr_t pf, uintptr_t vp, pfn_t pages, int flags) |
| 105 | { |
109 | { |
| 106 | ipl_t ipl; |
110 | ipl_t ipl; |
| 107 | cap_t caps; |
111 | cap_t caps; |
| 108 | mem_backend_data_t backend_data; |
112 | mem_backend_data_t backend_data; |
| 109 | 113 | ||
| 110 | backend_data.base = pf; |
114 | backend_data.base = pf; |
| 111 | backend_data.frames = pages; |
115 | backend_data.frames = pages; |
| 112 | 116 | ||
| 113 | /* |
117 | /* |
| 114 | * Make sure the caller is authorised to make this syscall. |
118 | * Make sure the caller is authorised to make this syscall. |
| 115 | */ |
119 | */ |
| 116 | caps = cap_get(TASK); |
120 | caps = cap_get(TASK); |
| 117 | if (!(caps & CAP_MEM_MANAGER)) |
121 | if (!(caps & CAP_MEM_MANAGER)) |
| 118 | return EPERM; |
122 | return EPERM; |
| 119 | 123 | ||
| 120 | ipl = interrupts_disable(); |
124 | ipl = interrupts_disable(); |
| 121 | 125 | ||
| 122 | /* |
126 | /* |
| 123 | * Check if the physical memory area is enabled for mapping. |
127 | * Check if the physical memory area is enabled for mapping. |
| 124 | * If the architecture supports virtually indexed caches, intercept |
- | |
| 125 | * attempts to create an illegal address alias. |
- | |
| 126 | */ |
128 | */ |
| 127 | spinlock_lock(&parea_lock); |
129 | spinlock_lock(&parea_lock); |
| - | 130 | ||
| 128 | parea_t *parea; |
131 | bool fnd = false; |
| 129 | btree_node_t *nodep; |
132 | link_t *cur; |
| - | 133 | ||
| 130 | parea = (parea_t *) btree_search(&parea_btree, (btree_key_t) pf, &nodep); |
134 | for (cur = parea_head.next; cur != &parea_head; cur = cur->next) { |
| 131 | if (!parea || parea->frames < pages || ((flags & AS_AREA_CACHEABLE) && |
135 | parea_t *parea = list_get_instance(cur, parea_t, link); |
| 132 | !parea->cacheable) || (!(flags & AS_AREA_CACHEABLE) && |
136 | if ((parea->pbase <= pf) && (ADDR2PFN(pf - parea->pbase) + pages <= parea->frames)) { |
| - | 137 | fnd = true; |
|
| - | 138 | break; |
|
| - | 139 | } |
|
| - | 140 | } |
|
| - | 141 | ||
| 133 | parea->cacheable)) { |
142 | spinlock_unlock(&parea_lock); |
| - | 143 | ||
| - | 144 | if (!fnd) { |
|
| 134 | /* |
145 | /* |
| 135 | * This physical memory area cannot be mapped. |
146 | * Physical memory area cannot be mapped. |
| 136 | */ |
147 | */ |
| 137 | spinlock_unlock(&parea_lock); |
- | |
| 138 | interrupts_restore(ipl); |
148 | interrupts_restore(ipl); |
| 139 | return ENOENT; |
149 | return ENOENT; |
| 140 | } |
150 | } |
| 141 | spinlock_unlock(&parea_lock); |
- | |
| 142 | 151 | ||
| 143 | spinlock_lock(&TASK->lock); |
152 | spinlock_lock(&TASK->lock); |
| 144 | 153 | ||
| 145 | if (!as_area_create(TASK->as, flags, pages * PAGE_SIZE, vp, AS_AREA_ATTR_NONE, |
154 | if (!as_area_create(TASK->as, flags, pages * PAGE_SIZE, vp, AS_AREA_ATTR_NONE, |
| 146 | &phys_backend, &backend_data)) { |
155 | &phys_backend, &backend_data)) { |
| 147 | /* |
156 | /* |
| Line 224... | Line 233... | ||
| 224 | unative_t sys_physmem_map(unative_t phys_base, unative_t virt_base, |
233 | unative_t sys_physmem_map(unative_t phys_base, unative_t virt_base, |
| 225 | unative_t pages, unative_t flags) |
234 | unative_t pages, unative_t flags) |
| 226 | { |
235 | { |
| 227 | return (unative_t) ddi_physmem_map(ALIGN_DOWN((uintptr_t) phys_base, |
236 | return (unative_t) ddi_physmem_map(ALIGN_DOWN((uintptr_t) phys_base, |
| 228 | FRAME_SIZE), ALIGN_DOWN((uintptr_t) virt_base, PAGE_SIZE), |
237 | FRAME_SIZE), ALIGN_DOWN((uintptr_t) virt_base, PAGE_SIZE), |
| 229 | (count_t) pages, (int) flags); |
238 | (pfn_t) pages, (int) flags); |
| 230 | } |
239 | } |
| 231 | 240 | ||
| 232 | /** Wrapper for SYS_ENABLE_IOSPACE syscall. |
241 | /** Wrapper for SYS_ENABLE_IOSPACE syscall. |
| 233 | * |
242 | * |
| 234 | * @param uspace_io_arg User space address of DDI argument structure. |
243 | * @param uspace_io_arg User space address of DDI argument structure. |
| Line 256... | Line 265... | ||
| 256 | * |
265 | * |
| 257 | * @return Zero on success or EPERM if callers capabilities are not sufficient. |
266 | * @return Zero on success or EPERM if callers capabilities are not sufficient. |
| 258 | */ |
267 | */ |
| 259 | unative_t sys_preempt_control(int enable) |
268 | unative_t sys_preempt_control(int enable) |
| 260 | { |
269 | { |
| 261 | if (!cap_get(TASK) & CAP_PREEMPT_CONTROL) |
270 | if (!cap_get(TASK) & CAP_PREEMPT_CONTROL) |
| 262 | return EPERM; |
271 | return EPERM; |
| 263 | if (enable) |
272 | if (enable) |
| 264 | preemption_enable(); |
273 | preemption_enable(); |
| 265 | else |
274 | else |
| 266 | preemption_disable(); |
275 | preemption_disable(); |
| 267 | return 0; |
276 | return 0; |
| 268 | } |
277 | } |
| 269 | 278 | ||
| 270 | /** @} |
279 | /** @} |
| 271 | */ |
280 | */ |