Subversion Repositories HelenOS

Rev

Rev 2927 | Rev 4344 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2927 Rev 4343
1
/*
1
/*
2
 * Copyright (c) 2006 Jakub Jermar
2
 * Copyright (c) 2006 Jakub Jermar
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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
 */
41
 
41
 
42
#include <ddi/ddi.h>
42
#include <ddi/ddi.h>
43
#include <ddi/ddi_arg.h>
43
#include <ddi/ddi_arg.h>
44
#include <proc/task.h>
44
#include <proc/task.h>
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.
70
 *
74
 *
71
 * @todo This function doesn't check for overlaps. It depends on the kernel to
75
 * @todo This function doesn't check for overlaps. It depends on the kernel to
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
        /*
148
         * The address space area could not have been created.
157
         * The address space area could not have been created.
149
         * We report it using ENOMEM.
158
         * We report it using ENOMEM.
150
         */
159
         */
151
        spinlock_unlock(&TASK->lock);
160
        spinlock_unlock(&TASK->lock);
152
        interrupts_restore(ipl);
161
        interrupts_restore(ipl);
153
        return ENOMEM;
162
        return ENOMEM;
154
    }
163
    }
155
   
164
   
156
    /*
165
    /*
157
     * Mapping is created on-demand during page fault.
166
     * Mapping is created on-demand during page fault.
158
     */
167
     */
159
   
168
   
160
    spinlock_unlock(&TASK->lock);
169
    spinlock_unlock(&TASK->lock);
161
    interrupts_restore(ipl);
170
    interrupts_restore(ipl);
162
    return 0;
171
    return 0;
163
}
172
}
164
 
173
 
165
/** Enable range of I/O space for task.
174
/** Enable range of I/O space for task.
166
 *
175
 *
167
 * @param id Task ID of the destination task.
176
 * @param id Task ID of the destination task.
168
 * @param ioaddr Starting I/O address.
177
 * @param ioaddr Starting I/O address.
169
 * @param size Size of the enabled I/O space..
178
 * @param size Size of the enabled I/O space..
170
 *
179
 *
171
 * @return 0 on success, EPERM if the caller lacks capabilities to use this
180
 * @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.
181
 *  syscall, ENOENT if there is no task matching the specified ID.
173
 */
182
 */
174
static int ddi_iospace_enable(task_id_t id, uintptr_t ioaddr, size_t size)
183
static int ddi_iospace_enable(task_id_t id, uintptr_t ioaddr, size_t size)
175
{
184
{
176
    ipl_t ipl;
185
    ipl_t ipl;
177
    cap_t caps;
186
    cap_t caps;
178
    task_t *t;
187
    task_t *t;
179
    int rc;
188
    int rc;
180
   
189
   
181
    /*
190
    /*
182
     * Make sure the caller is authorised to make this syscall.
191
     * Make sure the caller is authorised to make this syscall.
183
     */
192
     */
184
    caps = cap_get(TASK);
193
    caps = cap_get(TASK);
185
    if (!(caps & CAP_IO_MANAGER))
194
    if (!(caps & CAP_IO_MANAGER))
186
        return EPERM;
195
        return EPERM;
187
   
196
   
188
    ipl = interrupts_disable();
197
    ipl = interrupts_disable();
189
    spinlock_lock(&tasks_lock);
198
    spinlock_lock(&tasks_lock);
190
   
199
   
191
    t = task_find_by_id(id);
200
    t = task_find_by_id(id);
192
   
201
   
193
    if ((!t) || (!context_check(CONTEXT, t->context))) {
202
    if ((!t) || (!context_check(CONTEXT, t->context))) {
194
        /*
203
        /*
195
         * There is no task with the specified ID
204
         * There is no task with the specified ID
196
         * or the task belongs to a different security
205
         * or the task belongs to a different security
197
         * context.
206
         * context.
198
         */
207
         */
199
        spinlock_unlock(&tasks_lock);
208
        spinlock_unlock(&tasks_lock);
200
        interrupts_restore(ipl);
209
        interrupts_restore(ipl);
201
        return ENOENT;
210
        return ENOENT;
202
    }
211
    }
203
 
212
 
204
    /* Lock the task and release the lock protecting tasks_btree. */
213
    /* Lock the task and release the lock protecting tasks_btree. */
205
    spinlock_lock(&t->lock);
214
    spinlock_lock(&t->lock);
206
    spinlock_unlock(&tasks_lock);
215
    spinlock_unlock(&tasks_lock);
207
 
216
 
208
    rc = ddi_iospace_enable_arch(t, ioaddr, size);
217
    rc = ddi_iospace_enable_arch(t, ioaddr, size);
209
   
218
   
210
    spinlock_unlock(&t->lock);
219
    spinlock_unlock(&t->lock);
211
    interrupts_restore(ipl);
220
    interrupts_restore(ipl);
212
    return rc;
221
    return rc;
213
}
222
}
214
 
223
 
215
/** Wrapper for SYS_PHYSMEM_MAP syscall.
224
/** Wrapper for SYS_PHYSMEM_MAP syscall.
216
 *
225
 *
217
 * @param phys_base Physical base address to map
226
 * @param phys_base Physical base address to map
218
 * @param virt_base Destination virtual address
227
 * @param virt_base Destination virtual address
219
 * @param pages Number of pages
228
 * @param pages Number of pages
220
 * @param flags Flags of newly mapped pages
229
 * @param flags Flags of newly mapped pages
221
 *
230
 *
222
 * @return 0 on success, otherwise it returns error code found in errno.h
231
 * @return 0 on success, otherwise it returns error code found in errno.h
223
 */
232
 */
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.
235
 *
244
 *
236
 * @return 0 on success, otherwise it returns error code found in errno.h
245
 * @return 0 on success, otherwise it returns error code found in errno.h
237
 */
246
 */
238
unative_t sys_iospace_enable(ddi_ioarg_t *uspace_io_arg)
247
unative_t sys_iospace_enable(ddi_ioarg_t *uspace_io_arg)
239
{
248
{
240
    ddi_ioarg_t arg;
249
    ddi_ioarg_t arg;
241
    int rc;
250
    int rc;
242
   
251
   
243
    rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t));
252
    rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t));
244
    if (rc != 0)
253
    if (rc != 0)
245
        return (unative_t) rc;
254
        return (unative_t) rc;
246
       
255
       
247
    return (unative_t) ddi_iospace_enable((task_id_t) arg.task_id,
256
    return (unative_t) ddi_iospace_enable((task_id_t) arg.task_id,
248
        (uintptr_t) arg.ioaddr, (size_t) arg.size);
257
        (uintptr_t) arg.ioaddr, (size_t) arg.size);
249
}
258
}
250
 
259
 
251
/** Disable or enable preemption.
260
/** Disable or enable preemption.
252
 *
261
 *
253
 * @param enable If non-zero, the preemption counter will be decremented,
262
 * @param enable If non-zero, the preemption counter will be decremented,
254
 *  leading to potential enabling of preemption. Otherwise the preemption
263
 *  leading to potential enabling of preemption. Otherwise the preemption
255
 *  counter will be incremented, preventing preemption from occurring.
264
 *  counter will be incremented, preventing preemption from occurring.
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
 */
272
 
281