Subversion Repositories HelenOS-historic

Rev

Rev 1708 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1708 Rev 1780
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 <arch.h>
50
#include <arch.h>
51
#include <align.h>
51
#include <align.h>
52
#include <errno.h>
52
#include <errno.h>
53
 
53
 
54
/** 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.
55
 *
55
 *
56
 * @param pf Physical frame address of the starting frame.
56
 * @param pf Physical frame address of the starting frame.
57
 * @param vp Virtual page address of the starting page.
57
 * @param vp Virtual page address of the starting page.
58
 * @param pages Number of pages to map.
58
 * @param pages Number of pages to map.
59
 * @param flags Address space area flags for the mapping.
59
 * @param flags Address space area flags for the mapping.
60
 *
60
 *
61
 * @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,
62
 *     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
63
 *     there was a problem in creating address space area.
63
 *     there was a problem in creating address space area.
64
 */
64
 */
65
static int ddi_physmem_map(__address pf, __address vp, count_t pages, int flags)
65
static int ddi_physmem_map(uintptr_t pf, uintptr_t vp, count_t pages, int flags)
66
{
66
{
67
    ipl_t ipl;
67
    ipl_t ipl;
68
    cap_t caps;
68
    cap_t caps;
69
    mem_backend_data_t backend_data;
69
    mem_backend_data_t backend_data;
70
 
70
 
71
    backend_data.base = pf;
71
    backend_data.base = pf;
72
    backend_data.frames = pages;
72
    backend_data.frames = pages;
73
   
73
   
74
    /*
74
    /*
75
     * Make sure the caller is authorised to make this syscall.
75
     * Make sure the caller is authorised to make this syscall.
76
     */
76
     */
77
    caps = cap_get(TASK);
77
    caps = cap_get(TASK);
78
    if (!(caps & CAP_MEM_MANAGER))
78
    if (!(caps & CAP_MEM_MANAGER))
79
        return EPERM;
79
        return EPERM;
80
 
80
 
81
    ipl = interrupts_disable();
81
    ipl = interrupts_disable();
82
    spinlock_lock(&TASK->lock);
82
    spinlock_lock(&TASK->lock);
83
   
83
   
84
    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,
85
        &phys_backend, &backend_data)) {
85
        &phys_backend, &backend_data)) {
86
        /*
86
        /*
87
         * The address space area could not have been created.
87
         * The address space area could not have been created.
88
         * We report it using ENOMEM.
88
         * We report it using ENOMEM.
89
         */
89
         */
90
        spinlock_unlock(&TASK->lock);
90
        spinlock_unlock(&TASK->lock);
91
        interrupts_restore(ipl);
91
        interrupts_restore(ipl);
92
        return ENOMEM;
92
        return ENOMEM;
93
    }
93
    }
94
   
94
   
95
    /*
95
    /*
96
     * Mapping is created on-demand during page fault.
96
     * Mapping is created on-demand during page fault.
97
     */
97
     */
98
   
98
   
99
    spinlock_unlock(&TASK->lock);
99
    spinlock_unlock(&TASK->lock);
100
    interrupts_restore(ipl);
100
    interrupts_restore(ipl);
101
    return 0;
101
    return 0;
102
}
102
}
103
 
103
 
104
/** Enable range of I/O space for task.
104
/** Enable range of I/O space for task.
105
 *
105
 *
106
 * @param id Task ID of the destination task.
106
 * @param id Task ID of the destination task.
107
 * @param ioaddr Starting I/O address.
107
 * @param ioaddr Starting I/O address.
108
 * @param size Size of the enabled I/O space..
108
 * @param size Size of the enabled I/O space..
109
 *
109
 *
110
 * @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,
111
 *     ENOENT if there is no task matching the specified ID.
111
 *     ENOENT if there is no task matching the specified ID.
112
 */
112
 */
113
static int ddi_iospace_enable(task_id_t id, __address ioaddr, size_t size)
113
static int ddi_iospace_enable(task_id_t id, uintptr_t ioaddr, size_t size)
114
{
114
{
115
    ipl_t ipl;
115
    ipl_t ipl;
116
    cap_t caps;
116
    cap_t caps;
117
    task_t *t;
117
    task_t *t;
118
    int rc;
118
    int rc;
119
   
119
   
120
    /*
120
    /*
121
     * Make sure the caller is authorised to make this syscall.
121
     * Make sure the caller is authorised to make this syscall.
122
     */
122
     */
123
    caps = cap_get(TASK);
123
    caps = cap_get(TASK);
124
    if (!(caps & CAP_IO_MANAGER))
124
    if (!(caps & CAP_IO_MANAGER))
125
        return EPERM;
125
        return EPERM;
126
   
126
   
127
    ipl = interrupts_disable();
127
    ipl = interrupts_disable();
128
    spinlock_lock(&tasks_lock);
128
    spinlock_lock(&tasks_lock);
129
   
129
   
130
    t = task_find_by_id(id);
130
    t = task_find_by_id(id);
131
   
131
   
132
    if (!t) {
132
    if (!t) {
133
        /*
133
        /*
134
         * There is no task with the specified ID.
134
         * There is no task with the specified ID.
135
         */
135
         */
136
        spinlock_unlock(&tasks_lock);
136
        spinlock_unlock(&tasks_lock);
137
        interrupts_restore(ipl);
137
        interrupts_restore(ipl);
138
        return ENOENT;
138
        return ENOENT;
139
    }
139
    }
140
 
140
 
141
    /* Lock the task and release the lock protecting tasks_btree. */
141
    /* Lock the task and release the lock protecting tasks_btree. */
142
    spinlock_lock(&t->lock);
142
    spinlock_lock(&t->lock);
143
    spinlock_unlock(&tasks_lock);
143
    spinlock_unlock(&tasks_lock);
144
 
144
 
145
    rc = ddi_iospace_enable_arch(t, ioaddr, size);
145
    rc = ddi_iospace_enable_arch(t, ioaddr, size);
146
   
146
   
147
    spinlock_unlock(&t->lock);
147
    spinlock_unlock(&t->lock);
148
    interrupts_restore(ipl);
148
    interrupts_restore(ipl);
149
    return rc;
149
    return rc;
150
}
150
}
151
 
151
 
152
/** Wrapper for SYS_MAP_PHYSMEM syscall.
152
/** Wrapper for SYS_MAP_PHYSMEM syscall.
153
 *
153
 *
154
 * @param phys_base Physical base address to map
154
 * @param phys_base Physical base address to map
155
 * @param virt_base Destination virtual address
155
 * @param virt_base Destination virtual address
156
 * @param pages Number of pages
156
 * @param pages Number of pages
157
 * @param flags Flags of newly mapped pages
157
 * @param flags Flags of newly mapped pages
158
 *
158
 *
159
 * @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
160
 */
160
 */
161
__native sys_physmem_map(__native phys_base, __native virt_base, __native pages,
161
unative_t sys_physmem_map(unative_t phys_base, unative_t virt_base, unative_t pages,
162
             __native flags)
162
             unative_t flags)
163
{
163
{
164
    return (__native) ddi_physmem_map(ALIGN_DOWN((__address) phys_base, FRAME_SIZE),
164
    return (unative_t) ddi_physmem_map(ALIGN_DOWN((uintptr_t) phys_base, FRAME_SIZE),
165
                      ALIGN_DOWN((__address) virt_base, PAGE_SIZE), (count_t) pages,
165
                      ALIGN_DOWN((uintptr_t) virt_base, PAGE_SIZE), (count_t) pages,
166
                      (int) flags);
166
                      (int) flags);
167
}
167
}
168
 
168
 
169
/** Wrapper for SYS_ENABLE_IOSPACE syscall.
169
/** Wrapper for SYS_ENABLE_IOSPACE syscall.
170
 *
170
 *
171
 * @param uspace_io_arg User space address of DDI argument structure.
171
 * @param uspace_io_arg User space address of DDI argument structure.
172
 *
172
 *
173
 * @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
174
 */
174
 */
175
__native sys_iospace_enable(ddi_ioarg_t *uspace_io_arg)
175
unative_t sys_iospace_enable(ddi_ioarg_t *uspace_io_arg)
176
{
176
{
177
    ddi_ioarg_t arg;
177
    ddi_ioarg_t arg;
178
    int rc;
178
    int rc;
179
   
179
   
180
    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));
181
    if (rc != 0)
181
    if (rc != 0)
182
        return (__native) rc;
182
        return (unative_t) rc;
183
       
183
       
184
    return (__native) ddi_iospace_enable((task_id_t) arg.task_id, (__address) arg.ioaddr, (size_t) arg.size);
184
    return (unative_t) ddi_iospace_enable((task_id_t) arg.task_id, (uintptr_t) arg.ioaddr, (size_t) arg.size);
185
}
185
}
186
 
186
 
187
/** Disable or enable preemption.
187
/** Disable or enable preemption.
188
 *
188
 *
189
 * @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
190
 *       enabling of preemption. Otherwise the preemption counter will be incremented,
190
 *       enabling of preemption. Otherwise the preemption counter will be incremented,
191
 *       preventing preemption from occurring.
191
 *       preventing preemption from occurring.
192
 *
192
 *
193
 * @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.
194
 */
194
 */
195
__native sys_preempt_control(int enable)
195
unative_t sys_preempt_control(int enable)
196
{
196
{
197
        if (! cap_get(TASK) & CAP_PREEMPT_CONTROL)
197
        if (! cap_get(TASK) & CAP_PREEMPT_CONTROL)
198
                return EPERM;
198
                return EPERM;
199
        if (enable)
199
        if (enable)
200
                preemption_enable();
200
                preemption_enable();
201
        else
201
        else
202
                preemption_disable();
202
                preemption_disable();
203
        return 0;
203
        return 0;
204
}
204
}
205
 
205
 
206
 /** @}
206
 /** @}
207
 */
207
 */
208
 
208
 
209
 
209