Subversion Repositories HelenOS-historic

Rev

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

Rev 1425 Rev 1429
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
/**
29
/**
30
 * @file    ddi.c
30
 * @file    ddi.c
31
 * @brief   Device Driver Interface functions.
31
 * @brief   Device Driver Interface functions.
32
 *
32
 *
33
 * This file contains functions that comprise the Device Driver Interface.
33
 * This file contains functions that comprise the Device Driver Interface.
34
 * These are the functions for mapping physical memory and enabling I/O
34
 * These are the functions for mapping physical memory and enabling I/O
35
 * space to tasks.
35
 * space to tasks.
36
 */
36
 */
37
 
37
 
38
#include <ddi/ddi.h>
38
#include <ddi/ddi.h>
39
#include <ddi/ddi_arg.h>
39
#include <ddi/ddi_arg.h>
40
#include <proc/task.h>
40
#include <proc/task.h>
41
#include <security/cap.h>
41
#include <security/cap.h>
42
#include <mm/frame.h>
42
#include <mm/frame.h>
43
#include <mm/as.h>
43
#include <mm/as.h>
44
#include <synch/spinlock.h>
44
#include <synch/spinlock.h>
45
#include <syscall/copy.h>
45
#include <syscall/copy.h>
46
#include <arch.h>
46
#include <arch.h>
47
#include <align.h>
47
#include <align.h>
48
#include <errno.h>
48
#include <errno.h>
49
 
49
 
50
/** Map piece of physical memory into virtual address space of specified task.
50
/** Map piece of physical memory into virtual address space of specified task.
51
 *
51
 *
52
 * @param id Task ID of the destination task.
52
 * @param id Task ID of the destination task.
53
 * @param pf Physical frame address of the starting frame.
53
 * @param pf Physical frame address of the starting frame.
54
 * @param vp Virtual page address of the starting page.
54
 * @param vp Virtual page address of the starting page.
55
 * @param pages Number of pages to map.
55
 * @param pages Number of pages to map.
56
 * @param writable If true, the mapping will be created writable.
56
 * @param flags Address space area flags for the mapping.
57
 *
57
 *
58
 * @return 0 on success, EPERM if the caller lacks capabilities to use this syscall,
58
 * @return 0 on success, EPERM if the caller lacks capabilities to use this syscall,
59
 *     ENOENT if there is no task matching the specified ID and ENOMEM if
59
 *     ENOENT if there is no task matching the specified ID and ENOMEM if
60
 *     there was a problem in creating address space area.
60
 *     there was a problem in creating address space area.
61
 */
61
 */
62
static int ddi_physmem_map(task_id_t id, __address pf, __address vp, count_t pages, bool writable)
62
static int ddi_physmem_map(task_id_t id, __address pf, __address vp, count_t pages, int flags)
63
{
63
{
64
    ipl_t ipl;
64
    ipl_t ipl;
65
    cap_t caps;
65
    cap_t caps;
66
    task_t *t;
66
    task_t *t;
67
    int flags;
-
 
68
    mem_backend_data_t backend_data;
67
    mem_backend_data_t backend_data;
69
 
68
 
70
    backend_data.base = pf;
69
    backend_data.base = pf;
71
    backend_data.frames = pages;
70
    backend_data.frames = pages;
72
   
71
   
73
    /*
72
    /*
74
     * Make sure the caller is authorised to make this syscall.
73
     * Make sure the caller is authorised to make this syscall.
75
     */
74
     */
76
    caps = cap_get(TASK);
75
    caps = cap_get(TASK);
77
    if (!(caps & CAP_MEM_MANAGER))
76
    if (!(caps & CAP_MEM_MANAGER))
78
        return EPERM;
77
        return EPERM;
79
   
78
   
80
    ipl = interrupts_disable();
79
    ipl = interrupts_disable();
81
    spinlock_lock(&tasks_lock);
80
    spinlock_lock(&tasks_lock);
82
   
81
   
83
    t = task_find_by_id(id);
82
    t = task_find_by_id(id);
84
   
83
   
85
    if (!t) {
84
    if (!t) {
86
        /*
85
        /*
87
         * There is no task with the specified ID.
86
         * There is no task with the specified ID.
88
         */
87
         */
89
        spinlock_unlock(&tasks_lock);
88
        spinlock_unlock(&tasks_lock);
90
        interrupts_restore(ipl);
89
        interrupts_restore(ipl);
91
        return ENOENT;
90
        return ENOENT;
92
    }
91
    }
93
 
92
 
94
    /*
93
    /*
95
     * TODO: We are currently lacking support for task destroying.
94
     * TODO: We are currently lacking support for task destroying.
96
     * Once it is added to the kernel, we must take care to
95
     * Once it is added to the kernel, we must take care to
97
     * synchronize in a way that prevents race conditions here.
96
     * synchronize in a way that prevents race conditions here.
98
     */
97
     */
99
   
98
   
100
    /* Lock the task and release the lock protecting tasks_btree. */
99
    /* Lock the task and release the lock protecting tasks_btree. */
101
    spinlock_lock(&t->lock);
100
    spinlock_lock(&t->lock);
102
    spinlock_unlock(&tasks_lock);
101
    spinlock_unlock(&tasks_lock);
103
   
102
   
104
    flags = AS_AREA_READ;
-
 
105
    if (writable)
-
 
106
        flags |= AS_AREA_WRITE;
-
 
107
    if (!as_area_create(t->as, flags, pages * PAGE_SIZE, vp, AS_AREA_ATTR_NONE,
103
    if (!as_area_create(t->as, flags, pages * PAGE_SIZE, vp, AS_AREA_ATTR_NONE,
108
        &phys_backend, &backend_data)) {
104
        &phys_backend, &backend_data)) {
109
        /*
105
        /*
110
         * The address space area could not have been created.
106
         * The address space area could not have been created.
111
         * We report it using ENOMEM.
107
         * We report it using ENOMEM.
112
         */
108
         */
113
        spinlock_unlock(&t->lock);
109
        spinlock_unlock(&t->lock);
114
        interrupts_restore(ipl);
110
        interrupts_restore(ipl);
115
        return ENOMEM;
111
        return ENOMEM;
116
    }
112
    }
117
   
113
   
118
    /*
114
    /*
119
     * Mapping is created on-demand during page fault.
115
     * Mapping is created on-demand during page fault.
120
     */
116
     */
121
   
117
   
122
    spinlock_unlock(&t->lock);
118
    spinlock_unlock(&t->lock);
123
    interrupts_restore(ipl);
119
    interrupts_restore(ipl);
124
    return 0;
120
    return 0;
125
}
121
}
126
 
122
 
127
/** Enable range of I/O space for task.
123
/** Enable range of I/O space for task.
128
 *
124
 *
129
 * @param id Task ID of the destination task.
125
 * @param id Task ID of the destination task.
130
 * @param ioaddr Starting I/O address.
126
 * @param ioaddr Starting I/O address.
131
 * @param size Size of the enabled I/O space..
127
 * @param size Size of the enabled I/O space..
132
 *
128
 *
133
 * @return 0 on success, EPERM if the caller lacks capabilities to use this syscall,
129
 * @return 0 on success, EPERM if the caller lacks capabilities to use this syscall,
134
 *     ENOENT if there is no task matching the specified ID.
130
 *     ENOENT if there is no task matching the specified ID.
135
 */
131
 */
136
static int ddi_iospace_enable(task_id_t id, __address ioaddr, size_t size)
132
static int ddi_iospace_enable(task_id_t id, __address ioaddr, size_t size)
137
{
133
{
138
    ipl_t ipl;
134
    ipl_t ipl;
139
    cap_t caps;
135
    cap_t caps;
140
    task_t *t;
136
    task_t *t;
141
    int rc;
137
    int rc;
142
   
138
   
143
    /*
139
    /*
144
     * Make sure the caller is authorised to make this syscall.
140
     * Make sure the caller is authorised to make this syscall.
145
     */
141
     */
146
    caps = cap_get(TASK);
142
    caps = cap_get(TASK);
147
    if (!(caps & CAP_IO_MANAGER))
143
    if (!(caps & CAP_IO_MANAGER))
148
        return EPERM;
144
        return EPERM;
149
   
145
   
150
    ipl = interrupts_disable();
146
    ipl = interrupts_disable();
151
    spinlock_lock(&tasks_lock);
147
    spinlock_lock(&tasks_lock);
152
   
148
   
153
    t = task_find_by_id(id);
149
    t = task_find_by_id(id);
154
   
150
   
155
    if (!t) {
151
    if (!t) {
156
        /*
152
        /*
157
         * There is no task with the specified ID.
153
         * There is no task with the specified ID.
158
         */
154
         */
159
        spinlock_unlock(&tasks_lock);
155
        spinlock_unlock(&tasks_lock);
160
        interrupts_restore(ipl);
156
        interrupts_restore(ipl);
161
        return ENOENT;
157
        return ENOENT;
162
    }
158
    }
163
 
159
 
164
    /*
160
    /*
165
     * TODO: We are currently lacking support for task destroying.
161
     * TODO: We are currently lacking support for task destroying.
166
     * Once it is added to the kernel, we must take care to
162
     * Once it is added to the kernel, we must take care to
167
     * synchronize in a way that prevents race conditions here.
163
     * synchronize in a way that prevents race conditions here.
168
     */
164
     */
169
   
165
   
170
    /* Lock the task and release the lock protecting tasks_btree. */
166
    /* Lock the task and release the lock protecting tasks_btree. */
171
    spinlock_lock(&t->lock);
167
    spinlock_lock(&t->lock);
172
    spinlock_unlock(&tasks_lock);
168
    spinlock_unlock(&tasks_lock);
173
 
169
 
174
    rc = ddi_iospace_enable_arch(t, ioaddr, size);
170
    rc = ddi_iospace_enable_arch(t, ioaddr, size);
175
   
171
   
176
    spinlock_unlock(&t->lock);
172
    spinlock_unlock(&t->lock);
177
    interrupts_restore(ipl);
173
    interrupts_restore(ipl);
178
    return rc;
174
    return rc;
179
}
175
}
180
 
176
 
181
/** Wrapper for SYS_MAP_PHYSMEM syscall.
177
/** Wrapper for SYS_MAP_PHYSMEM syscall.
182
 *
178
 *
183
 * @param User space address of memory DDI argument structure.
179
 * @param User space address of memory DDI argument structure.
184
 *
180
 *
185
 * @return 0 on success, otherwise it returns error code found in errno.h
181
 * @return 0 on success, otherwise it returns error code found in errno.h
186
 */
182
 */
187
__native sys_physmem_map(ddi_memarg_t *uspace_mem_arg)
183
__native sys_physmem_map(ddi_memarg_t *uspace_mem_arg)
188
{
184
{
189
    ddi_memarg_t arg;
185
    ddi_memarg_t arg;
190
    int rc;
186
    int rc;
191
   
187
   
192
    rc = copy_from_uspace(&arg, uspace_mem_arg, sizeof(ddi_memarg_t));
188
    rc = copy_from_uspace(&arg, uspace_mem_arg, sizeof(ddi_memarg_t));
193
    if (rc != 0)
189
    if (rc != 0)
194
        return (__native) rc;
190
        return (__native) rc;
195
       
191
       
196
    return (__native) ddi_physmem_map((task_id_t) arg.task_id, ALIGN_DOWN((__address) arg.phys_base, FRAME_SIZE),
192
    return (__native) ddi_physmem_map((task_id_t) arg.task_id, ALIGN_DOWN((__address) arg.phys_base, FRAME_SIZE),
197
                      ALIGN_DOWN((__address) arg.virt_base, PAGE_SIZE), (count_t) arg.pages,
193
                      ALIGN_DOWN((__address) arg.virt_base, PAGE_SIZE), (count_t) arg.pages,
198
                      (bool) arg.writable);
194
                      (int) arg.flags);
199
}
195
}
200
 
196
 
201
/** Wrapper for SYS_ENABLE_IOSPACE syscall.
197
/** Wrapper for SYS_ENABLE_IOSPACE syscall.
202
 *
198
 *
203
 * @param User space address of DDI argument structure.
199
 * @param User space address of DDI argument structure.
204
 *
200
 *
205
 * @return 0 on success, otherwise it returns error code found in errno.h
201
 * @return 0 on success, otherwise it returns error code found in errno.h
206
 */
202
 */
207
__native sys_iospace_enable(ddi_ioarg_t *uspace_io_arg)
203
__native sys_iospace_enable(ddi_ioarg_t *uspace_io_arg)
208
{
204
{
209
    ddi_ioarg_t arg;
205
    ddi_ioarg_t arg;
210
    int rc;
206
    int rc;
211
   
207
   
212
    rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t));
208
    rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t));
213
    if (rc != 0)
209
    if (rc != 0)
214
        return (__native) rc;
210
        return (__native) rc;
215
       
211
       
216
    return (__native) ddi_iospace_enable((task_id_t) arg.task_id, (__address) arg.ioaddr, (size_t) arg.size);
212
    return (__native) ddi_iospace_enable((task_id_t) arg.task_id, (__address) arg.ioaddr, (size_t) arg.size);
217
}
213
}
218
 
214
 
219
/** Disable or enable preemption.
215
/** Disable or enable preemption.
220
 *
216
 *
221
 * @param enable If non-zero, the preemption counter will be decremented, leading to potential
217
 * @param enable If non-zero, the preemption counter will be decremented, leading to potential
222
 *       enabling of preemption. Otherwise the preemption counter will be incremented,
218
 *       enabling of preemption. Otherwise the preemption counter will be incremented,
223
 *       preventing preemption from occurring.
219
 *       preventing preemption from occurring.
224
 *
220
 *
225
 * @return Zero on success or EPERM if callers capabilities are not sufficient.
221
 * @return Zero on success or EPERM if callers capabilities are not sufficient.
226
 */
222
 */
227
__native sys_preempt_control(int enable)
223
__native sys_preempt_control(int enable)
228
{
224
{
229
        if (! cap_get(TASK) & CAP_PREEMPT_CONTROL)
225
        if (! cap_get(TASK) & CAP_PREEMPT_CONTROL)
230
                return EPERM;
226
                return EPERM;
231
        if (enable)
227
        if (enable)
232
                preemption_enable();
228
                preemption_enable();
233
        else
229
        else
234
                preemption_disable();
230
                preemption_disable();
235
        return 0;
231
        return 0;
236
}
232
}
237
 
233