Subversion Repositories HelenOS

Compare Revisions

Ignore whitespace Rev 3623 → Rev 3624

/branches/tracing/kernel/generic/include/mm/as_debug.h
0,0 → 1,50
/*
* Copyright (c) 2008 Jiri Svoboda
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
 
/** @addtogroup genericmm
* @{
*/
/** @file
*/
 
#ifndef KERN_AS_DEBUG_H_
#define KERN_AS_DEBUG_H_
 
#ifdef CONFIG_UDEBUG
 
#include <arch/types.h>
 
extern int as_area_make_writeable(uintptr_t address);
extern int as_debug_write(uintptr_t va, void *data, size_t n);
 
#endif /* CONFIG_UDEBUG */
 
#endif
 
/** @}
*/
/branches/tracing/kernel/generic/include/mm/as.h
221,7 → 221,9
int as_area_share(as_t *src_as, uintptr_t src_base, size_t acc_size,
as_t *dst_as, uintptr_t dst_base, int dst_flags_mask);
extern int as_area_change_flags(as_t *as, int flags, uintptr_t address);
extern as_area_t *find_area_and_lock(as_t *, uintptr_t);
 
 
extern int as_area_get_flags(as_area_t *area);
extern bool as_area_check_access(as_area_t *area, pf_access_t access);
extern size_t as_area_get_size(uintptr_t base);
/branches/tracing/kernel/generic/src/mm/as.c
112,7 → 112,6
as_t *AS_KERNEL = NULL;
 
static int area_flags_to_page_flags(int);
static as_area_t *find_area_and_lock(as_t *, uintptr_t);
static bool check_area_conflicts(as_t *, uintptr_t, size_t, as_area_t *);
static void sh_info_remove_reference(share_info_t *);
 
1107,210 → 1106,6
AS = new_as;
}
 
#ifdef CONFIG_UDEBUG
 
/** Write directly into a page, bypassing area flags.
*
* This allows a debugger to write into a page that is mapped read-only
* (such as the text segment). Naturally, this can only be done if the
* correspoinding area is private (not shared) and anonymous.
*
* If this is not the case, this function calls as_area_make_writeable()
* first.
*/
static int debug_write_inside_page(uintptr_t va, void *data, size_t n)
{
uintptr_t page;
pte_t *pte;
as_area_t *area;
uintptr_t frame;
ipl_t ipl;
int rc;
 
page = ALIGN_DOWN(va, PAGE_SIZE);
ASSERT(ALIGN_DOWN(va + n - 1, PAGE_SIZE) == page);
 
restart:
mutex_lock(&AS->lock);
ipl = interrupts_disable();
area = find_area_and_lock(AS, page);
if (area->backend != &anon_backend || area->sh_info != NULL) {
mutex_unlock(&area->lock);
mutex_unlock(&AS->lock);
interrupts_restore(ipl);
 
rc = as_area_make_writeable(area->base);
if (rc != 0) return rc;
 
goto restart;
}
 
pte = page_mapping_find(AS, page);
if (! (pte && PTE_VALID(pte) && PTE_PRESENT(pte)) ) {
mutex_unlock(&area->lock);
mutex_unlock(&AS->lock);
interrupts_restore(ipl);
 
rc = as_page_fault(page, PF_ACCESS_WRITE, NULL);
if (rc == AS_PF_FAULT) return EINVAL;
 
goto restart;
}
 
frame = PTE_GET_FRAME(pte);
memcpy((void *)(PA2KA(frame) + (va - page)), data, n);
 
mutex_unlock(&area->lock);
mutex_unlock(&AS->lock);
interrupts_restore(ipl);
 
return EOK;
}
 
/** Write data bypassing area flags.
*
* See debug_write_inside_page().
*/
int as_debug_write(uintptr_t va, void *data, size_t n)
{
size_t now;
int rc;
 
while (n > 0) {
/* Number of bytes until the end of page */
now = ALIGN_DOWN(va, PAGE_SIZE) + PAGE_SIZE - va;
if (now > n) now = n;
 
rc = debug_write_inside_page(va, data, now);
if (rc != EOK) return rc;
 
va += now;
data += now;
n -= now;
}
 
return EOK;
}
 
/** Make sure area is private and anonymous.
*
* Not atomic atm.
* @param address Virtual address in AS.
*/
int as_area_make_writeable(uintptr_t address)
{
ipl_t ipl;
as_area_t *area;
uintptr_t base, page;
uintptr_t old_frame, frame;
size_t size;
int flags;
int page_flags;
pte_t *pte;
int rc;
uintptr_t *pagemap;
 
ipl = interrupts_disable();
mutex_lock(&AS->lock);
area = find_area_and_lock(AS, address);
if (!area) {
/*
* Could not find the address space area.
*/
mutex_unlock(&AS->lock);
interrupts_restore(ipl);
return ENOENT;
}
 
if (area->backend == &anon_backend && !area->sh_info) {
/* Nothing to do */
mutex_unlock(&area->lock);
mutex_unlock(&AS->lock);
interrupts_restore(ipl);
return EOK;
}
 
base = area->base;
size = area->pages * PAGE_SIZE;
flags = area->flags;
page_flags = as_area_get_flags(area);
 
pagemap = malloc(area->pages * sizeof(uintptr_t), 0);
page_table_lock(AS, false);
 
for (page = base; page < base + size; page += PAGE_SIZE) {
pte = page_mapping_find(AS, page);
if (!pte || !PTE_PRESENT(pte) || !PTE_READABLE(pte)) {
/* Fetch the missing page */
if (!area->backend || !area->backend->page_fault) {
page_table_unlock(AS, false);
mutex_unlock(&area->lock);
mutex_unlock(&AS->lock);
interrupts_restore(ipl);
return EINVAL;
}
if (area->backend->page_fault(area, page, PF_ACCESS_READ) != AS_PF_OK) {
page_table_unlock(AS, false);
mutex_unlock(&area->lock);
mutex_unlock(&AS->lock);
interrupts_restore(ipl);
return EINVAL;
}
}
ASSERT(PTE_VALID(pte));
 
old_frame = PTE_GET_FRAME(pte);
 
frame = (uintptr_t)frame_alloc(ONE_FRAME, 0);
memcpy((void *) PA2KA(frame), (void *)PA2KA(old_frame),
FRAME_SIZE);
 
pagemap[(page - base) / PAGE_SIZE] = frame;
}
 
page_table_unlock(AS, false);
mutex_unlock(&area->lock);
mutex_unlock(&AS->lock);
interrupts_restore(ipl);
 
rc = as_area_destroy(AS, address);
if (rc < 0) {
free(pagemap);
return rc;
}
 
area = as_area_create(AS, flags, size, base, AS_AREA_ATTR_PARTIAL,
&anon_backend, NULL);
if (area == NULL) {
free(pagemap);
return rc;
}
 
mutex_lock(&AS->lock);
mutex_lock(&area->lock);
page_table_lock(AS, false);
for (page = base; page < base + size; page += PAGE_SIZE) {
frame = pagemap[(page - base) / PAGE_SIZE];
 
page_mapping_insert(AS, page, frame, page_flags);
if (!used_space_insert(area, page, 1))
panic("Could not insert used space.\n");
}
 
page_table_unlock(AS, false);
 
area->attributes &= ~AS_AREA_ATTR_PARTIAL;
 
mutex_unlock(&area->lock);
mutex_unlock(&AS->lock);
 
free(pagemap);
 
return EOK;
}
 
#endif /* defined(CONFIG_UDEBUG) */
 
/** Convert address space area flags to page flags.
*
* @param aflags Flags of some address space area.
/branches/tracing/kernel/generic/src/mm/as_debug.c
0,0 → 1,253
/*
* Copyright (c) 2008 Jiri Svoboda
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
 
/** @addtogroup genericmm
* @{
*/
 
/**
* @file
* @brief Debugger access to adress spaces.
*
* This file contains functions allowing debugger access to the user
* address space. It allows bypassing the access mode restrictions.
*
*/
 
#include <mm/as_debug.h>
#include <mm/slab.h>
#include <mm/page.h>
#include <errno.h>
#include <synch/mutex.h>
#include <memstr.h>
#include <align.h>
#include <arch.h>
#include <mm/as.h>
 
/** Write directly into a page, bypassing area flags.
*
* This allows a debugger to write into a page that is mapped read-only
* (such as the text segment). Naturally, this can only be done if the
* correspoinding area is private (not shared) and anonymous.
*
* If this is not the case, this function calls as_area_make_writeable()
* first.
*/
static int debug_write_inside_page(uintptr_t va, void *data, size_t n)
{
uintptr_t page;
pte_t *pte;
as_area_t *area;
uintptr_t frame;
ipl_t ipl;
int rc;
 
page = ALIGN_DOWN(va, PAGE_SIZE);
ASSERT(ALIGN_DOWN(va + n - 1, PAGE_SIZE) == page);
 
restart:
mutex_lock(&AS->lock);
ipl = interrupts_disable();
area = find_area_and_lock(AS, page);
if (area->backend != &anon_backend || area->sh_info != NULL) {
mutex_unlock(&area->lock);
mutex_unlock(&AS->lock);
interrupts_restore(ipl);
 
rc = as_area_make_writeable(area->base);
if (rc != 0) return rc;
 
goto restart;
}
 
pte = page_mapping_find(AS, page);
if (! (pte && PTE_VALID(pte) && PTE_PRESENT(pte)) ) {
mutex_unlock(&area->lock);
mutex_unlock(&AS->lock);
interrupts_restore(ipl);
 
rc = as_page_fault(page, PF_ACCESS_WRITE, NULL);
if (rc == AS_PF_FAULT) return EINVAL;
 
goto restart;
}
 
frame = PTE_GET_FRAME(pte);
memcpy((void *)(PA2KA(frame) + (va - page)), data, n);
 
mutex_unlock(&area->lock);
mutex_unlock(&AS->lock);
interrupts_restore(ipl);
 
return EOK;
}
 
/** Write data bypassing area flags.
*
* See debug_write_inside_page().
*/
int as_debug_write(uintptr_t va, void *data, size_t n)
{
size_t now;
int rc;
 
while (n > 0) {
/* Number of bytes until the end of page */
now = ALIGN_DOWN(va, PAGE_SIZE) + PAGE_SIZE - va;
if (now > n) now = n;
 
rc = debug_write_inside_page(va, data, now);
if (rc != EOK) return rc;
 
va += now;
data += now;
n -= now;
}
 
return EOK;
}
 
/** Make sure area is private and anonymous.
*
* Not atomic atm.
* @param address Virtual address in AS.
*/
int as_area_make_writeable(uintptr_t address)
{
ipl_t ipl;
as_area_t *area;
uintptr_t base, page;
uintptr_t old_frame, frame;
size_t size;
int flags;
int page_flags;
pte_t *pte;
int rc;
uintptr_t *pagemap;
 
ipl = interrupts_disable();
mutex_lock(&AS->lock);
area = find_area_and_lock(AS, address);
if (!area) {
/*
* Could not find the address space area.
*/
mutex_unlock(&AS->lock);
interrupts_restore(ipl);
return ENOENT;
}
 
if (area->backend == &anon_backend && !area->sh_info) {
/* Nothing to do */
mutex_unlock(&area->lock);
mutex_unlock(&AS->lock);
interrupts_restore(ipl);
return EOK;
}
 
base = area->base;
size = area->pages * PAGE_SIZE;
flags = area->flags;
page_flags = as_area_get_flags(area);
 
pagemap = malloc(area->pages * sizeof(uintptr_t), 0);
page_table_lock(AS, false);
 
for (page = base; page < base + size; page += PAGE_SIZE) {
pte = page_mapping_find(AS, page);
if (!pte || !PTE_PRESENT(pte) || !PTE_READABLE(pte)) {
/* Fetch the missing page */
if (!area->backend || !area->backend->page_fault) {
page_table_unlock(AS, false);
mutex_unlock(&area->lock);
mutex_unlock(&AS->lock);
interrupts_restore(ipl);
return EINVAL;
}
if (area->backend->page_fault(area, page, PF_ACCESS_READ) != AS_PF_OK) {
page_table_unlock(AS, false);
mutex_unlock(&area->lock);
mutex_unlock(&AS->lock);
interrupts_restore(ipl);
return EINVAL;
}
}
ASSERT(PTE_VALID(pte));
 
old_frame = PTE_GET_FRAME(pte);
 
frame = (uintptr_t)frame_alloc(ONE_FRAME, 0);
memcpy((void *) PA2KA(frame), (void *)PA2KA(old_frame),
FRAME_SIZE);
 
pagemap[(page - base) / PAGE_SIZE] = frame;
}
 
page_table_unlock(AS, false);
mutex_unlock(&area->lock);
mutex_unlock(&AS->lock);
interrupts_restore(ipl);
 
rc = as_area_destroy(AS, address);
if (rc < 0) {
free(pagemap);
return rc;
}
 
area = as_area_create(AS, flags, size, base, AS_AREA_ATTR_PARTIAL,
&anon_backend, NULL);
if (area == NULL) {
free(pagemap);
return rc;
}
 
mutex_lock(&AS->lock);
mutex_lock(&area->lock);
page_table_lock(AS, false);
for (page = base; page < base + size; page += PAGE_SIZE) {
frame = pagemap[(page - base) / PAGE_SIZE];
 
page_mapping_insert(AS, page, frame, page_flags);
if (!used_space_insert(area, page, 1))
panic("Could not insert used space.\n");
}
 
page_table_unlock(AS, false);
 
area->attributes &= ~AS_AREA_ATTR_PARTIAL;
 
mutex_unlock(&area->lock);
mutex_unlock(&AS->lock);
 
free(pagemap);
 
return EOK;
}
 
/** @}
*/
/branches/tracing/kernel/Makefile
293,6 → 293,7
ifeq ($(CONFIG_UDEBUG),y)
GENERIC_SOURCES += \
generic/src/ipc/kbox.c \
generic/src/mm/as_debug.c \
generic/src/udebug/udebug.c \
generic/src/udebug/udebug_ops.c \
generic/src/udebug/udebug_ipc.c