Subversion Repositories HelenOS-historic

Rev

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

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