Subversion Repositories HelenOS

Rev

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

Rev 3022 Rev 4055
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 66... Line 66...
66
 
66
 
67
/** Enable piece of physical memory for mapping by physmem_map().
67
/** Enable piece of physical memory for mapping by physmem_map().
68
 *
68
 *
69
 * @param parea Pointer to physical area structure.
69
 * @param parea Pointer to physical area structure.
70
 *
70
 *
71
 * @todo This function doesn't check for overlaps. It depends on the kernel to
-
 
72
 * create disjunct physical memory areas.
-
 
73
 */
71
 */
74
void ddi_parea_register(parea_t *parea)
72
void ddi_parea_register(parea_t *parea)
75
{
73
{
76
    ipl_t ipl;
-
 
77
 
-
 
78
    ipl = interrupts_disable();
74
    ipl_t ipl = interrupts_disable();
79
    spinlock_lock(&parea_lock);
75
    spinlock_lock(&parea_lock);
80
   
76
   
81
    /*
77
    /*
82
     * TODO: we should really check for overlaps here.
-
 
83
     * However, we should be safe because the kernel is pretty sane and
78
     * We don't check for overlaps here as the kernel is pretty sane.
84
     * memory of different devices doesn't overlap.
-
 
85
     */
79
     */
86
    btree_insert(&parea_btree, (btree_key_t) parea->pbase, parea, NULL);
80
    btree_insert(&parea_btree, (btree_key_t) parea->pbase, parea, NULL);
87
 
81
   
88
    spinlock_unlock(&parea_lock);
82
    spinlock_unlock(&parea_lock);
89
    interrupts_restore(ipl);   
83
    interrupts_restore(ipl);
90
}
84
}
91
 
85
 
92
/** 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.
93
 *
87
 *
94
 * @param pf Physical address of the starting frame.
88
 * @param pf    Physical address of the starting frame.
95
 * @param vp Virtual address of the starting page.
89
 * @param vp    Virtual address of the starting page.
96
 * @param pages Number of pages to map.
90
 * @param pages Number of pages to map.
97
 * @param flags Address space area flags for the mapping.
91
 * @param flags Address space area flags for the mapping.
98
 *
92
 *
99
 * @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
100
 *  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
101
 *  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
102
 *  was a problem in creating address space area.
97
 *         creating address space area.
-
 
98
 *
103
 */
99
 */
104
static int ddi_physmem_map(uintptr_t pf, uintptr_t vp, count_t pages, int flags)
100
static int ddi_physmem_map(uintptr_t pf, uintptr_t vp, count_t pages, int flags)
105
{
101
{
106
    ipl_t ipl;
-
 
107
    cap_t caps;
102
    ASSERT(TASK);
108
    mem_backend_data_t backend_data;
103
    ASSERT((pf % FRAME_SIZE) == 0);
109
 
-
 
110
    backend_data.base = pf;
-
 
111
    backend_data.frames = pages;
104
    ASSERT((vp % PAGE_SIZE) == 0);
112
   
105
   
113
    /*
106
    /*
114
     * Make sure the caller is authorised to make this syscall.
107
     * Make sure the caller is authorised to make this syscall.
115
     */
108
     */
116
    caps = cap_get(TASK);
109
    cap_t caps = cap_get(TASK);
117
    if (!(caps & CAP_MEM_MANAGER))
110
    if (!(caps & CAP_MEM_MANAGER))
118
        return EPERM;
111
        return EPERM;
119
 
112
   
-
 
113
    mem_backend_data_t backend_data;
-
 
114
    backend_data.base = pf;
120
    ipl = interrupts_disable();
115
    backend_data.frames = pages;
121
 
116
   
-
 
117
    ipl_t ipl = interrupts_disable();
122
    /*
118
   
123
     * Check if the physical memory area is enabled for mapping.
119
    /* Find the zone of the physical memory */
124
     * If the architecture supports virtually indexed caches, intercept
120
    spinlock_lock(&zones.lock);
125
     * attempts to create an illegal address alias.
121
    count_t znum = find_zone(ADDR2PFN(pf), pages, 0);
126
     */
122
   
127
    spinlock_lock(&parea_lock);
123
    if (znum == (count_t) -1) {
128
    parea_t *parea;
-
 
129
    btree_node_t *nodep;
124
        /* Frames not found in any zones
130
    parea = (parea_t *) btree_search(&parea_btree, (btree_key_t) pf, &nodep);
-
 
131
    if (!parea || parea->frames < pages || ((flags & AS_AREA_CACHEABLE) &&
-
 
132
        !parea->cacheable) || (!(flags & AS_AREA_CACHEABLE) &&
-
 
133
        parea->cacheable)) {
-
 
134
        /*
-
 
135
         * This physical memory area cannot be mapped.
125
         * -> assume it is hardware device and allow mapping
136
         */
126
         */
-
 
127
        spinlock_unlock(&zones.lock);
-
 
128
        goto map;
-
 
129
    }
-
 
130
   
-
 
131
    if (zones.info[znum].flags & ZONE_FIRMWARE) {
-
 
132
        /* Frames are part of firmware */
-
 
133
        spinlock_unlock(&zones.lock);
-
 
134
        goto map;
-
 
135
    }
-
 
136
   
-
 
137
    if (zone_flags_available(zones.info[znum].flags)) {
-
 
138
        /* Frames are part of physical memory, check if the memory
-
 
139
         * region is enabled for mapping.
-
 
140
         */
-
 
141
        spinlock_unlock(&zones.lock);
-
 
142
       
-
 
143
        spinlock_lock(&parea_lock);
-
 
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
       
137
        spinlock_unlock(&parea_lock);
151
        spinlock_unlock(&parea_lock);
138
        interrupts_restore(ipl);
-
 
139
        return ENOENT;
152
        goto map;
140
    }
153
    }
-
 
154
   
-
 
155
err:
141
    spinlock_unlock(&parea_lock);
156
    spinlock_unlock(&zones.lock);
-
 
157
    interrupts_restore(ipl);
-
 
158
    return ENOENT;
142
 
159
   
-
 
160
map:
143
    spinlock_lock(&TASK->lock);
161
    spinlock_lock(&TASK->lock);
144
   
162
   
145
    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,
146
        &phys_backend, &backend_data)) {
164
        AS_AREA_ATTR_NONE, &phys_backend, &backend_data)) {
147
        /*
165
        /*
148
         * The address space area could not have been created.
166
         * The address space area could not have been created.
149
         * We report it using ENOMEM.
167
         * We report it using ENOMEM.
150
         */
168
         */
151
        spinlock_unlock(&TASK->lock);
169
        spinlock_unlock(&TASK->lock);
Line 167... Line 185...
167
 * @param id Task ID of the destination task.
185
 * @param id Task ID of the destination task.
168
 * @param ioaddr Starting I/O address.
186
 * @param ioaddr Starting I/O address.
169
 * @param size Size of the enabled I/O space..
187
 * @param size Size of the enabled I/O space..
170
 *
188
 *
171
 * @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
172
 *  syscall, ENOENT if there is no task matching the specified ID.
190
 *           syscall, ENOENT if there is no task matching the specified ID.
-
 
191
 *
173
 */
192
 */
174
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)
175
{
194
{
176
    ipl_t ipl;
-
 
177
    cap_t caps;
-
 
178
    task_t *t;
-
 
179
    int rc;
-
 
180
   
-
 
181
    /*
195
    /*
182
     * Make sure the caller is authorised to make this syscall.
196
     * Make sure the caller is authorised to make this syscall.
183
     */
197
     */
184
    caps = cap_get(TASK);
198
    cap_t caps = cap_get(TASK);
185
    if (!(caps & CAP_IO_MANAGER))
199
    if (!(caps & CAP_IO_MANAGER))
186
        return EPERM;
200
        return EPERM;
187
   
201
   
188
    ipl = interrupts_disable();
202
    ipl_t ipl = interrupts_disable();
189
    spinlock_lock(&tasks_lock);
203
    spinlock_lock(&tasks_lock);
190
   
204
   
191
    t = task_find_by_id(id);
205
    task_t *task = task_find_by_id(id);
192
   
206
   
193
    if ((!t) || (!context_check(CONTEXT, t->context))) {
207
    if ((!task) || (!context_check(CONTEXT, task->context))) {
194
        /*
208
        /*
195
         * There is no task with the specified ID
209
         * There is no task with the specified ID
196
         * or the task belongs to a different security
210
         * or the task belongs to a different security
197
         * context.
211
         * context.
198
         */
212
         */
199
        spinlock_unlock(&tasks_lock);
213
        spinlock_unlock(&tasks_lock);
200
        interrupts_restore(ipl);
214
        interrupts_restore(ipl);
201
        return ENOENT;
215
        return ENOENT;
202
    }
216
    }
203
 
217
   
204
    /* Lock the task and release the lock protecting tasks_btree. */
218
    /* Lock the task and release the lock protecting tasks_btree. */
205
    spinlock_lock(&t->lock);
219
    spinlock_lock(&task->lock);
206
    spinlock_unlock(&tasks_lock);
220
    spinlock_unlock(&tasks_lock);
207
 
-
 
208
    rc = ddi_iospace_enable_arch(t, ioaddr, size);
-
 
209
   
221
   
-
 
222
    int rc = ddi_iospace_enable_arch(task, ioaddr, size);
-
 
223
   
210
    spinlock_unlock(&t->lock);
224
    spinlock_unlock(&task->lock);
211
    interrupts_restore(ipl);
225
    interrupts_restore(ipl);
-
 
226
   
212
    return rc;
227
    return rc;
213
}
228
}
214
 
229
 
215
/** Wrapper for SYS_PHYSMEM_MAP syscall.
230
/** Wrapper for SYS_PHYSMEM_MAP syscall.
216
 *
231
 *
Line 218... Line 233...
218
 * @param virt_base Destination virtual address
233
 * @param virt_base Destination virtual address
219
 * @param pages Number of pages
234
 * @param pages Number of pages
220
 * @param flags Flags of newly mapped pages
235
 * @param flags Flags of newly mapped pages
221
 *
236
 *
222
 * @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
 *
223
 */
239
 */
224
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,
225
    unative_t pages, unative_t flags)
241
    unative_t pages, unative_t flags)
226
{
242
{
227
    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,
228
        FRAME_SIZE), ALIGN_DOWN((uintptr_t) virt_base, PAGE_SIZE),
244
        FRAME_SIZE), ALIGN_DOWN((uintptr_t) virt_base, PAGE_SIZE),
Line 232... Line 248...
232
/** Wrapper for SYS_ENABLE_IOSPACE syscall.
248
/** Wrapper for SYS_ENABLE_IOSPACE syscall.
233
 *
249
 *
234
 * @param uspace_io_arg User space address of DDI argument structure.
250
 * @param uspace_io_arg User space address of DDI argument structure.
235
 *
251
 *
236
 * @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
 *
237
 */
254
 */
238
unative_t sys_iospace_enable(ddi_ioarg_t *uspace_io_arg)
255
unative_t sys_iospace_enable(ddi_ioarg_t *uspace_io_arg)
239
{
256
{
240
    ddi_ioarg_t arg;
257
    ddi_ioarg_t arg;
241
    int rc;
-
 
242
   
-
 
243
    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));
244
    if (rc != 0)
259
    if (rc != 0)
245
        return (unative_t) rc;
260
        return (unative_t) rc;
246
       
261
   
247
    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,
248
        (uintptr_t) arg.ioaddr, (size_t) arg.size);
263
        (uintptr_t) arg.ioaddr, (size_t) arg.size);
249
}
264
}
250
 
265
 
251
/** Disable or enable preemption.
266
/** Disable or enable preemption.
252
 *
267
 *
253
 * @param enable If non-zero, the preemption counter will be decremented,
268
 * @param enable If non-zero, the preemption counter will be decremented,
254
 *  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
255
 *  counter will be incremented, preventing preemption from occurring.
271
 *               preemption from occurring.
256
 *
272
 *
257
 * @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
 *
258
 */
275
 */
259
unative_t sys_preempt_control(int enable)
276
unative_t sys_preempt_control(int enable)
260
{
277
{
261
        if (!cap_get(TASK) & CAP_PREEMPT_CONTROL)
278
    if (!cap_get(TASK) & CAP_PREEMPT_CONTROL)
262
                return EPERM;
279
        return EPERM;
-
 
280
   
263
        if (enable)
281
    if (enable)
264
                preemption_enable();
282
        preemption_enable();
265
        else
283
    else
266
                preemption_disable();
284
        preemption_disable();
-
 
285
   
267
        return 0;
286
    return 0;
268
}
287
}
269
 
288
 
270
/** @}
289
/** @}
271
 */
290
 */