Subversion Repositories HelenOS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3624 svoboda 1
/*
2
 * Copyright (c) 2008 Jiri Svoboda
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
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
15
 *   derived from this software without specific prior written permission.
16
 *
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
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
29
/** @addtogroup genericmm
30
 * @{
31
 */
32
 
33
/**
34
 * @file
35
 * @brief   Debugger access to adress spaces.
36
 *
37
 * This file contains functions allowing debugger access to the user
38
 * address space. It allows bypassing the access mode restrictions.
39
 *
40
 */
41
 
42
#include <mm/as_debug.h>
43
#include <mm/slab.h>
44
#include <mm/page.h>
45
#include <errno.h>
46
#include <synch/mutex.h>
47
#include <memstr.h>
48
#include <align.h>
49
#include <arch.h>
50
#include <mm/as.h>
51
 
52
/** Write directly into a page, bypassing area flags.
53
 *
54
 * This allows a debugger to write into a page that is mapped read-only
55
 * (such as the text segment). Naturally, this can only be done if the
56
 * correspoinding area is private (not shared) and anonymous.
57
 *
58
 * If this is not the case, this function calls as_area_make_writeable()
59
 * first.
60
 */
61
static int debug_write_inside_page(uintptr_t va, void *data, size_t n)
62
{
63
    uintptr_t page;
64
    pte_t *pte;
65
    as_area_t *area;
66
    uintptr_t frame;
67
    ipl_t ipl;
68
    int rc;
69
 
70
    page = ALIGN_DOWN(va, PAGE_SIZE);
71
    ASSERT(ALIGN_DOWN(va + n - 1, PAGE_SIZE) == page);
72
 
73
restart:
74
    mutex_lock(&AS->lock);
75
    ipl = interrupts_disable();
76
    area = find_area_and_lock(AS, page);
77
    if (area->backend != &anon_backend || area->sh_info != NULL) {
78
        mutex_unlock(&area->lock);
79
        mutex_unlock(&AS->lock);
80
        interrupts_restore(ipl);
81
 
82
        rc = as_area_make_writeable(area->base);
83
        if (rc != 0) return rc;
84
 
85
        goto restart;
86
    }
87
 
88
    pte = page_mapping_find(AS, page);
89
    if (! (pte && PTE_VALID(pte) && PTE_PRESENT(pte)) ) {
90
        mutex_unlock(&area->lock);
91
        mutex_unlock(&AS->lock);
92
        interrupts_restore(ipl);
93
 
94
        rc = as_page_fault(page, PF_ACCESS_WRITE, NULL);
95
        if (rc == AS_PF_FAULT) return EINVAL;
96
 
97
        goto restart;
98
    }
99
 
100
    frame = PTE_GET_FRAME(pte);
101
    memcpy((void *)(PA2KA(frame) + (va - page)), data, n);
102
 
103
    mutex_unlock(&area->lock);
104
    mutex_unlock(&AS->lock);
105
    interrupts_restore(ipl);
106
 
107
    return EOK;
108
}
109
 
110
/** Write data bypassing area flags.
111
 *
112
 * See debug_write_inside_page().
113
 */
114
int as_debug_write(uintptr_t va, void *data, size_t n)
115
{
116
    size_t now;
117
    int rc;
118
 
119
    while (n > 0) {
120
        /* Number of bytes until the end of page */
121
        now = ALIGN_DOWN(va, PAGE_SIZE) + PAGE_SIZE - va;
122
        if (now > n) now = n;
123
 
124
        rc = debug_write_inside_page(va, data, now);
125
        if (rc != EOK) return rc;
126
 
127
        va += now;
128
        data += now;
129
        n -= now;
130
    }
131
 
132
    return EOK;
133
}
134
 
135
/** Make sure area is private and anonymous.
136
 *
137
 * Not atomic atm.
138
 * @param address   Virtual address in AS.
139
 */
140
int as_area_make_writeable(uintptr_t address)
141
{
142
    ipl_t ipl;
143
    as_area_t *area;
144
    uintptr_t base, page;
145
    uintptr_t old_frame, frame;
146
    size_t size;
147
    int flags;
148
    int page_flags;
149
    pte_t *pte;
150
    int rc;
151
    uintptr_t *pagemap;
152
 
153
    ipl = interrupts_disable();
154
    mutex_lock(&AS->lock);
155
    area = find_area_and_lock(AS, address);
156
    if (!area) {
157
        /*
158
         * Could not find the address space area.
159
         */
160
        mutex_unlock(&AS->lock);
161
        interrupts_restore(ipl);
162
        return ENOENT;
163
    }
164
 
165
    if (area->backend == &anon_backend && !area->sh_info) {
166
        /* Nothing to do */
167
        mutex_unlock(&area->lock);
168
        mutex_unlock(&AS->lock);
169
        interrupts_restore(ipl);
170
        return EOK;
171
    }
172
 
173
    base = area->base;
174
    size = area->pages * PAGE_SIZE;
175
    flags = area->flags;
176
    page_flags = as_area_get_flags(area);
177
 
178
    pagemap = malloc(area->pages * sizeof(uintptr_t), 0);
179
    page_table_lock(AS, false);
180
 
181
    for (page = base; page < base + size; page += PAGE_SIZE) {
182
        pte = page_mapping_find(AS, page);
183
        if (!pte || !PTE_PRESENT(pte) || !PTE_READABLE(pte)) {
184
            /* Fetch the missing page */
185
            if (!area->backend || !area->backend->page_fault) {
186
                page_table_unlock(AS, false);
187
                mutex_unlock(&area->lock);
188
                mutex_unlock(&AS->lock);
189
                interrupts_restore(ipl);
190
                return EINVAL;
191
            }
192
            if (area->backend->page_fault(area, page, PF_ACCESS_READ) != AS_PF_OK) {
193
                page_table_unlock(AS, false);
194
                mutex_unlock(&area->lock);
195
                mutex_unlock(&AS->lock);
196
                interrupts_restore(ipl);
197
                return EINVAL;
198
            }
199
        }
200
        ASSERT(PTE_VALID(pte));
201
 
202
        old_frame = PTE_GET_FRAME(pte);
203
 
204
        frame = (uintptr_t)frame_alloc(ONE_FRAME, 0);
205
        memcpy((void *) PA2KA(frame), (void *)PA2KA(old_frame),
206
            FRAME_SIZE);
207
 
208
        pagemap[(page - base) / PAGE_SIZE] = frame;
209
    }
210
 
211
    page_table_unlock(AS, false);
212
    mutex_unlock(&area->lock);
213
    mutex_unlock(&AS->lock);
214
    interrupts_restore(ipl);
215
 
216
    rc = as_area_destroy(AS, address);
217
    if (rc < 0) {
218
        free(pagemap);
219
        return rc;
220
    }
221
 
222
    area = as_area_create(AS, flags, size, base, AS_AREA_ATTR_PARTIAL,
223
        &anon_backend, NULL);
224
    if (area == NULL) {
225
        free(pagemap);
226
        return rc;
227
    }
228
 
229
    mutex_lock(&AS->lock);
230
    mutex_lock(&area->lock);
231
    page_table_lock(AS, false);
232
    for (page = base; page < base + size; page += PAGE_SIZE) {
233
        frame = pagemap[(page - base) / PAGE_SIZE];
234
 
235
        page_mapping_insert(AS, page, frame, page_flags);
236
        if (!used_space_insert(area, page, 1))
237
            panic("Could not insert used space.\n");
238
    }
239
 
240
    page_table_unlock(AS, false);
241
 
242
    area->attributes &= ~AS_AREA_ATTR_PARTIAL;
243
 
244
    mutex_unlock(&area->lock);
245
    mutex_unlock(&AS->lock);
246
 
247
    free(pagemap);
248
 
249
    return EOK;
250
}
251
 
252
/** @}
253
 */