Subversion Repositories HelenOS

Rev

Rev 2012 | Rev 2071 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2012 Rev 2015
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 <arch.h>
51
#include <arch.h>
51
#include <align.h>
52
#include <align.h>
52
#include <errno.h>
53
#include <errno.h>
53
 
54
 
-
 
55
/** This lock protects the parea_btree. */
-
 
56
SPINLOCK_INITIALIZE(parea_lock);
-
 
57
 
-
 
58
/** B+tree with enabled physical memory areas. */
-
 
59
static btree_t parea_btree;
-
 
60
 
-
 
61
/** Initialize DDI. */
-
 
62
void ddi_init(void)
-
 
63
{
-
 
64
    btree_create(&parea_btree);
-
 
65
}
-
 
66
 
-
 
67
/** Enable piece of physical memory for mapping by physmem_map().
-
 
68
 *
-
 
69
 * @param parea Pointer to physical area structure.
-
 
70
 *
-
 
71
 * @todo This function doesn't check for overlaps. It depends on the kernel to
-
 
72
 * create disjunct physical memory areas.
-
 
73
 */
-
 
74
void ddi_parea_register(parea_t *parea)
-
 
75
{
-
 
76
    ipl_t ipl;
-
 
77
 
-
 
78
    ipl = interrupts_disable();
-
 
79
    spinlock_lock(&parea_lock);
-
 
80
   
-
 
81
    /*
-
 
82
     * TODO: we should really check for overlaps here.
-
 
83
     * However, we should be safe because the kernel is pretty sane and
-
 
84
     * memory of different devices doesn't overlap.
-
 
85
     */
-
 
86
    btree_insert(&parea_btree, (btree_key_t) parea->pbase, parea, NULL);
-
 
87
 
-
 
88
    spinlock_unlock(&parea_lock);
-
 
89
    interrupts_restore(ipl);   
-
 
90
}
-
 
91
 
54
/** Map piece of physical memory into virtual address space of current task.
92
/** Map piece of physical memory into virtual address space of current task.
55
 *
93
 *
56
 * @param pf Physical frame address of the starting frame.
94
 * @param pf Physical address of the starting frame.
57
 * @param vp Virtual page address of the starting page.
95
 * @param vp Virtual address of the starting page.
58
 * @param pages Number of pages to map.
96
 * @param pages Number of pages to map.
59
 * @param flags Address space area flags for the mapping.
97
 * @param flags Address space area flags for the mapping.
60
 *
98
 *
61
 * @return 0 on success, EPERM if the caller lacks capabilities to use this syscall,
99
 * @return 0 on success, EPERM if the caller lacks capabilities to use this
62
 *     ENOENT if there is no task matching the specified ID and ENOMEM if
100
 *  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
63
 *     there was a problem in creating address space area.
102
 *  was a problem in creating address space area. ENOTSUP is returned when
-
 
103
 *  an attempt to create an illegal address alias is detected.
64
 */
104
 */
65
static int ddi_physmem_map(uintptr_t pf, uintptr_t vp, count_t pages, int flags)
105
static int ddi_physmem_map(uintptr_t pf, uintptr_t vp, count_t pages, int flags)
66
{
106
{
67
    ipl_t ipl;
107
    ipl_t ipl;
68
    cap_t caps;
108
    cap_t caps;
Line 77... Line 117...
77
    caps = cap_get(TASK);
117
    caps = cap_get(TASK);
78
    if (!(caps & CAP_MEM_MANAGER))
118
    if (!(caps & CAP_MEM_MANAGER))
79
        return EPERM;
119
        return EPERM;
80
 
120
 
81
    ipl = interrupts_disable();
121
    ipl = interrupts_disable();
-
 
122
 
-
 
123
    /*
-
 
124
     * Check if the physical memory area is enabled for mapping.
-
 
125
     * If the architecture supports virtually indexed caches, intercept
-
 
126
     * attempts to create an illegal address alias.
-
 
127
     */
-
 
128
    spinlock_lock(&parea_lock);
-
 
129
    parea_t *parea;
-
 
130
    btree_node_t *nodep;
-
 
131
    parea = btree_search(&parea_btree, (btree_key_t) pf, &nodep);
-
 
132
    if (!parea || parea->frames < pages || ((flags & AS_AREA_CACHEABLE) &&
-
 
133
        !parea->cacheable) || (!(flags & AS_AREA_CACHEABLE) &&
-
 
134
        parea->cacheable)) {
-
 
135
        /*
-
 
136
         * This physical memory area cannot be mapped.
-
 
137
         */
-
 
138
        spinlock_unlock(&parea_lock);
-
 
139
        interrupts_restore(ipl);
-
 
140
        return ENOENT;
-
 
141
    }
-
 
142
 
-
 
143
#ifdef CONFIG_VIRT_IDX_DCACHE
-
 
144
    if (PAGE_COLOR(parea->vbase) != PAGE_COLOR(vp)) {
-
 
145
        /*
-
 
146
         * Refuse to create an illegal address alias.
-
 
147
         */
-
 
148
        spinlock_unlock(&parea_lock);
-
 
149
        interrupts_restore(ipl);
-
 
150
        return ENOTSUP;
-
 
151
    }
-
 
152
#endif /* CONFIG_VIRT_IDX_DCACHE */
-
 
153
 
-
 
154
    spinlock_unlock(&parea_lock);
-
 
155
 
82
    spinlock_lock(&TASK->lock);
156
    spinlock_lock(&TASK->lock);
83
   
157
   
84
    if (!as_area_create(TASK->as, flags, pages * PAGE_SIZE, vp, AS_AREA_ATTR_NONE,
158
    if (!as_area_create(TASK->as, flags, pages * PAGE_SIZE, vp, AS_AREA_ATTR_NONE,
85
        &phys_backend, &backend_data)) {
159
        &phys_backend, &backend_data)) {
86
        /*
160
        /*
Line 105... Line 179...
105
 *
179
 *
106
 * @param id Task ID of the destination task.
180
 * @param id Task ID of the destination task.
107
 * @param ioaddr Starting I/O address.
181
 * @param ioaddr Starting I/O address.
108
 * @param size Size of the enabled I/O space..
182
 * @param size Size of the enabled I/O space..
109
 *
183
 *
110
 * @return 0 on success, EPERM if the caller lacks capabilities to use this syscall,
184
 * @return 0 on success, EPERM if the caller lacks capabilities to use this
111
 *     ENOENT if there is no task matching the specified ID.
185
 *  syscall, ENOENT if there is no task matching the specified ID.
112
 */
186
 */
113
static int ddi_iospace_enable(task_id_t id, uintptr_t ioaddr, size_t size)
187
static int ddi_iospace_enable(task_id_t id, uintptr_t ioaddr, size_t size)
114
{
188
{
115
    ipl_t ipl;
189
    ipl_t ipl;
116
    cap_t caps;
190
    cap_t caps;
Line 158... Line 232...
158
 * @param pages Number of pages
232
 * @param pages Number of pages
159
 * @param flags Flags of newly mapped pages
233
 * @param flags Flags of newly mapped pages
160
 *
234
 *
161
 * @return 0 on success, otherwise it returns error code found in errno.h
235
 * @return 0 on success, otherwise it returns error code found in errno.h
162
 */
236
 */
163
unative_t sys_physmem_map(unative_t phys_base, unative_t virt_base, unative_t pages,
237
unative_t sys_physmem_map(unative_t phys_base, unative_t virt_base, unative_t
164
             unative_t flags)
238
    pages, unative_t flags)
165
{
239
{
166
    return (unative_t) ddi_physmem_map(ALIGN_DOWN((uintptr_t) phys_base, FRAME_SIZE),
240
    return (unative_t) ddi_physmem_map(ALIGN_DOWN((uintptr_t) phys_base,
167
                      ALIGN_DOWN((uintptr_t) virt_base, PAGE_SIZE), (count_t) pages,
241
        FRAME_SIZE), ALIGN_DOWN((uintptr_t) virt_base, PAGE_SIZE),
168
                      (int) flags);
242
        (count_t) pages, (int) flags);
169
}
243
}
170
 
244
 
171
/** Wrapper for SYS_ENABLE_IOSPACE syscall.
245
/** Wrapper for SYS_ENABLE_IOSPACE syscall.
172
 *
246
 *
173
 * @param uspace_io_arg User space address of DDI argument structure.
247
 * @param uspace_io_arg User space address of DDI argument structure.
Line 181... Line 255...
181
   
255
   
182
    rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t));
256
    rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t));
183
    if (rc != 0)
257
    if (rc != 0)
184
        return (unative_t) rc;
258
        return (unative_t) rc;
185
       
259
       
186
    return (unative_t) ddi_iospace_enable((task_id_t) arg.task_id, (uintptr_t) arg.ioaddr, (size_t) arg.size);
260
    return (unative_t) ddi_iospace_enable((task_id_t) arg.task_id,
-
 
261
        (uintptr_t) arg.ioaddr, (size_t) arg.size);
187
}
262
}
188
 
263
 
189
/** Disable or enable preemption.
264
/** Disable or enable preemption.
190
 *
265
 *
191
 * @param enable If non-zero, the preemption counter will be decremented, leading to potential
266
 * @param enable If non-zero, the preemption counter will be decremented,
192
 *       enabling of preemption. Otherwise the preemption counter will be incremented,
267
 *  leading to potential enabling of preemption. Otherwise the preemption
193
 *       preventing preemption from occurring.
268
 *  counter will be incremented, preventing preemption from occurring.
194
 *
269
 *
195
 * @return Zero on success or EPERM if callers capabilities are not sufficient.
270
 * @return Zero on success or EPERM if callers capabilities are not sufficient.
196
 */
271
 */
197
unative_t sys_preempt_control(int enable)
272
unative_t sys_preempt_control(int enable)
198
{
273
{