Subversion Repositories HelenOS

Rev

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

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