Subversion Repositories HelenOS

Rev

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

Rev 3940 Rev 3973
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);
59
static btree_t parea_btree;
60
 
60
 
61
/** Initialize DDI. */
61
/** Initialize DDI. */
62
void ddi_init(void)
62
void ddi_init(void)
63
{
63
{
64
    hw_area();
64
    btree_create(&parea_btree);
65
}
65
}
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.
78
     * We don't check for overlaps here as the kernel is pretty sane.
84
     */
79
     */
85
    link_initialize(&parea->link);
-
 
86
    list_append(&parea->link, &parea_head);
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, pfn_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
   
120
    ipl = interrupts_disable();
113
    mem_backend_data_t backend_data;
121
   
-
 
122
    /*
-
 
123
     * Check if the physical memory area is enabled for mapping.
114
    backend_data.base = pf;
124
     */
-
 
125
    spinlock_lock(&parea_lock);
115
    backend_data.frames = pages;
126
   
116
   
127
    bool fnd = false;
117
    ipl_t ipl = interrupts_disable();
128
    link_t *cur;
-
 
129
   
118
   
130
    for (cur = parea_head.next; cur != &parea_head; cur = cur->next) {
119
    /* Find the zone of the physical memory */
131
        parea_t *parea = list_get_instance(cur, parea_t, link);
120
    spinlock_lock(&zones.lock);
132
        if ((parea->pbase <= pf) && (ADDR2PFN(pf - parea->pbase) + pages <= parea->frames)) {
121
    count_t znum = find_zone(ADDR2PFN(pf), pages, 0);
-
 
122
   
133
            fnd = true;
123
    if (znum == (count_t) -1) {
134
            break;
124
        /* Frames not found in any zones
-
 
125
         * -> assume it is hardware device and allow mapping
135
        }
126
         */
-
 
127
        spinlock_unlock(&zones.lock);
-
 
128
        goto map;
136
    }
129
    }
137
   
130
   
-
 
131
    if (zones.info[znum].flags & ZONE_FIRMWARE) {
-
 
132
        /* Frames are part of firmware */
138
    spinlock_unlock(&parea_lock);
133
        spinlock_unlock(&zones.lock);
-
 
134
        goto map;
-
 
135
    }
139
   
136
   
140
    if (!fnd) {
137
    if (zone_flags_available(zones.info[znum].flags)) {
141
        /*
-
 
-
 
138
        /* Frames are part of physical memory, check if the memory
142
         * Physical memory area cannot be mapped.
139
         * region is enabled for mapping.
143
         */
140
         */
-
 
141
        spinlock_unlock(&zones.lock);
-
 
142
       
-
 
143
        spinlock_lock(&parea_lock);
144
        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);
145
        return ENOENT;
152
        goto map;
146
    }
153
    }
147
   
154
   
-
 
155
err:
-
 
156
    spinlock_unlock(&zones.lock);
-
 
157
    interrupts_restore(ipl);
-
 
158
    return ENOENT;
-
 
159
   
-
 
160
map:
148
    spinlock_lock(&TASK->lock);
161
    spinlock_lock(&TASK->lock);
149
   
162
   
150
    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,
151
        &phys_backend, &backend_data)) {
164
        AS_AREA_ATTR_NONE, &phys_backend, &backend_data)) {
152
        /*
165
        /*
153
         * The address space area could not have been created.
166
         * The address space area could not have been created.
154
         * We report it using ENOMEM.
167
         * We report it using ENOMEM.
155
         */
168
         */
156
        spinlock_unlock(&TASK->lock);
169
        spinlock_unlock(&TASK->lock);
Line 172... Line 185...
172
 * @param id Task ID of the destination task.
185
 * @param id Task ID of the destination task.
173
 * @param ioaddr Starting I/O address.
186
 * @param ioaddr Starting I/O address.
174
 * @param size Size of the enabled I/O space..
187
 * @param size Size of the enabled I/O space..
175
 *
188
 *
176
 * @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
177
 *  syscall, ENOENT if there is no task matching the specified ID.
190
 *           syscall, ENOENT if there is no task matching the specified ID.
-
 
191
 *
178
 */
192
 */
179
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)
180
{
194
{
181
    ipl_t ipl;
-
 
182
    cap_t caps;
-
 
183
    task_t *t;
-
 
184
    int rc;
-
 
185
   
-
 
186
    /*
195
    /*
187
     * Make sure the caller is authorised to make this syscall.
196
     * Make sure the caller is authorised to make this syscall.
188
     */
197
     */
189
    caps = cap_get(TASK);
198
    cap_t caps = cap_get(TASK);
190
    if (!(caps & CAP_IO_MANAGER))
199
    if (!(caps & CAP_IO_MANAGER))
191
        return EPERM;
200
        return EPERM;
192
   
201
   
193
    ipl = interrupts_disable();
202
    ipl_t ipl = interrupts_disable();
194
    spinlock_lock(&tasks_lock);
203
    spinlock_lock(&tasks_lock);
195
   
204
   
196
    t = task_find_by_id(id);
205
    task_t *task = task_find_by_id(id);
197
   
206
   
198
    if ((!t) || (!context_check(CONTEXT, t->context))) {
207
    if ((!task) || (!context_check(CONTEXT, task->context))) {
199
        /*
208
        /*
200
         * There is no task with the specified ID
209
         * There is no task with the specified ID
201
         * or the task belongs to a different security
210
         * or the task belongs to a different security
202
         * context.
211
         * context.
203
         */
212
         */
204
        spinlock_unlock(&tasks_lock);
213
        spinlock_unlock(&tasks_lock);
205
        interrupts_restore(ipl);
214
        interrupts_restore(ipl);
206
        return ENOENT;
215
        return ENOENT;
207
    }
216
    }
208
 
217
   
209
    /* Lock the task and release the lock protecting tasks_btree. */
218
    /* Lock the task and release the lock protecting tasks_btree. */
210
    spinlock_lock(&t->lock);
219
    spinlock_lock(&task->lock);
211
    spinlock_unlock(&tasks_lock);
220
    spinlock_unlock(&tasks_lock);
212
 
-
 
213
    rc = ddi_iospace_enable_arch(t, ioaddr, size);
-
 
214
   
221
   
-
 
222
    int rc = ddi_iospace_enable_arch(task, ioaddr, size);
-
 
223
   
215
    spinlock_unlock(&t->lock);
224
    spinlock_unlock(&task->lock);
216
    interrupts_restore(ipl);
225
    interrupts_restore(ipl);
-
 
226
   
217
    return rc;
227
    return rc;
218
}
228
}
219
 
229
 
220
/** Wrapper for SYS_PHYSMEM_MAP syscall.
230
/** Wrapper for SYS_PHYSMEM_MAP syscall.
221
 *
231
 *
Line 223... Line 233...
223
 * @param virt_base Destination virtual address
233
 * @param virt_base Destination virtual address
224
 * @param pages Number of pages
234
 * @param pages Number of pages
225
 * @param flags Flags of newly mapped pages
235
 * @param flags Flags of newly mapped pages
226
 *
236
 *
227
 * @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
 *
228
 */
239
 */
229
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,
230
    unative_t pages, unative_t flags)
241
    unative_t pages, unative_t flags)
231
{
242
{
232
    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,
233
        FRAME_SIZE), ALIGN_DOWN((uintptr_t) virt_base, PAGE_SIZE),
244
        FRAME_SIZE), ALIGN_DOWN((uintptr_t) virt_base, PAGE_SIZE),
234
        (pfn_t) pages, (int) flags);
245
        (count_t) pages, (int) flags);
235
}
246
}
236
 
247
 
237
/** Wrapper for SYS_ENABLE_IOSPACE syscall.
248
/** Wrapper for SYS_ENABLE_IOSPACE syscall.
238
 *
249
 *
239
 * @param uspace_io_arg User space address of DDI argument structure.
250
 * @param uspace_io_arg User space address of DDI argument structure.
240
 *
251
 *
241
 * @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
 *
242
 */
254
 */
243
unative_t sys_iospace_enable(ddi_ioarg_t *uspace_io_arg)
255
unative_t sys_iospace_enable(ddi_ioarg_t *uspace_io_arg)
244
{
256
{
245
    ddi_ioarg_t arg;
257
    ddi_ioarg_t arg;
246
    int rc;
-
 
247
   
-
 
248
    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));
249
    if (rc != 0)
259
    if (rc != 0)
250
        return (unative_t) rc;
260
        return (unative_t) rc;
251
       
261
   
252
    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,
253
        (uintptr_t) arg.ioaddr, (size_t) arg.size);
263
        (uintptr_t) arg.ioaddr, (size_t) arg.size);
254
}
264
}
255
 
265
 
256
/** Disable or enable preemption.
266
/** Disable or enable preemption.
257
 *
267
 *
258
 * @param enable If non-zero, the preemption counter will be decremented,
268
 * @param enable If non-zero, the preemption counter will be decremented,
259
 *  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
260
 *  counter will be incremented, preventing preemption from occurring.
271
 *               preemption from occurring.
261
 *
272
 *
262
 * @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
 *
263
 */
275
 */
264
unative_t sys_preempt_control(int enable)
276
unative_t sys_preempt_control(int enable)
265
{
277
{
266
    if (!cap_get(TASK) & CAP_PREEMPT_CONTROL)
278
    if (!cap_get(TASK) & CAP_PREEMPT_CONTROL)
267
        return EPERM;
279
        return EPERM;
-
 
280
   
268
    if (enable)
281
    if (enable)
269
        preemption_enable();
282
        preemption_enable();
270
    else
283
    else
271
        preemption_disable();
284
        preemption_disable();
-
 
285
   
272
    return 0;
286
    return 0;
273
}
287
}
274
 
288
 
275
/** @}
289
/** @}
276
 */
290
 */