Subversion Repositories HelenOS

Rev

Rev 1424 | Rev 1426 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1424 jermar 1
/*
2
 * Copyright (C) 2006 Jakub Jermar
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
/**
30
 * @file	backend_elf.c
31
 * @brief	Backend for address space areas backed by an ELF image.
32
 */
33
 
34
#include <elf.h>
35
#include <debug.h>
36
#include <arch/types.h>
37
#include <typedefs.h>
38
#include <mm/as.h>
39
#include <mm/frame.h>
40
#include <mm/slab.h>
41
#include <align.h>
42
#include <memstr.h>
43
#include <macros.h>
44
#include <arch.h>
45
 
46
static int elf_page_fault(as_area_t *area, __address addr, pf_access_t access);
47
static void elf_frame_free(as_area_t *area, __address page, __address frame);
48
 
49
mem_backend_t elf_backend = {
50
	.page_fault = elf_page_fault,
51
	.frame_free = elf_frame_free,
52
	.share = NULL
53
};
54
 
55
/** Service a page fault in the ELF backend address space area.
56
 *
57
 * The address space area and page tables must be already locked.
58
 *
59
 * @param area Pointer to the address space area.
60
 * @param addr Faulting virtual address.
61
 * @param access Access mode that caused the fault (i.e. read/write/exec).
62
 *
63
 * @return AS_PF_FAULT on failure (i.e. page fault) or AS_PF_OK on success (i.e. serviced).
64
 */
65
int elf_page_fault(as_area_t *area, __address addr, pf_access_t access)
66
{
1425 jermar 67
	elf_header_t *elf = area->backend_data.elf;
68
	elf_segment_header_t *entry = area->backend_data.segment;
1424 jermar 69
	__address base, frame;
70
	index_t i;
71
 
72
	if (!as_area_check_access(area, access))
73
		return AS_PF_FAULT;
74
 
75
	ASSERT((addr >= entry->p_vaddr) && (addr < entry->p_vaddr + entry->p_memsz));
76
	i = (addr - entry->p_vaddr) >> PAGE_WIDTH;
77
	base = (__address) (((void *) elf) + entry->p_offset);
78
	ASSERT(ALIGN_UP(base, FRAME_SIZE) == base);
79
 
80
	if (ALIGN_DOWN(addr, PAGE_SIZE) + PAGE_SIZE < entry->p_vaddr + entry->p_filesz) {
81
		/*
82
		 * Initialized portion of the segment. The memory is backed
83
		 * directly by the content of the ELF image. Pages are
84
		 * only copied if the segment is writable so that there
85
		 * can be more instantions of the same memory ELF image
86
		 * used at a time. Note that this could be later done
87
		 * as COW.
88
		 */
89
		if (entry->p_flags & PF_W) {
90
			frame = PFN2ADDR(frame_alloc(ONE_FRAME, 0));
91
			memcpy((void *) PA2KA(frame), (void *) (base + i*FRAME_SIZE), FRAME_SIZE);
92
		} else {
93
			frame = KA2PA(base + i*FRAME_SIZE);
94
		}	
95
	} else if (ALIGN_DOWN(addr, PAGE_SIZE) >= ALIGN_UP(entry->p_vaddr + entry->p_filesz, PAGE_SIZE)) {
96
		/*
97
		 * This is the uninitialized portion of the segment.
98
		 * It is not physically present in the ELF image.
99
		 * To resolve the situation, a frame must be allocated
100
		 * and cleared.
101
		 */
102
		frame = PFN2ADDR(frame_alloc(ONE_FRAME, 0));
103
		memsetb(PA2KA(frame), FRAME_SIZE, 0);
104
	} else {
105
		size_t size;
106
		/*
107
		 * The mixed case.
108
		 * The lower part is backed by the ELF image and
109
		 * the upper part is anonymous memory.
110
		 */
111
		size = entry->p_filesz - (i<<PAGE_WIDTH);
112
		frame = PFN2ADDR(frame_alloc(ONE_FRAME, 0));
113
		memsetb(PA2KA(frame) + size, FRAME_SIZE - size, 0);
114
		memcpy((void *) PA2KA(frame), (void *) (base + i*FRAME_SIZE), size);
115
	}
116
 
117
	page_mapping_insert(AS, addr, frame, as_area_get_flags(area));
118
        if (!used_space_insert(area, ALIGN_DOWN(addr, PAGE_SIZE), 1))
119
                panic("Could not insert used space.\n");
120
 
121
	return AS_PF_OK;
122
}
123
 
124
/** Free a frame that is backed by the ELF backend.
125
 *
126
 * The address space area and page tables must be already locked.
127
 *
128
 * @param area Pointer to the address space area.
129
 * @param page Page that is mapped to frame. Must be aligned to PAGE_SIZE.
130
 * @param frame Frame to be released.
131
 *
132
 */
133
void elf_frame_free(as_area_t *area, __address page, __address frame)
134
{
1425 jermar 135
	elf_header_t *elf = area->backend_data.elf;
136
	elf_segment_header_t *entry = area->backend_data.segment;
1424 jermar 137
	__address base;
138
	index_t i;
139
 
140
	ASSERT((page >= entry->p_vaddr) && (page < entry->p_vaddr + entry->p_memsz));
141
	i = (page - entry->p_vaddr) >> PAGE_WIDTH;
142
	base = (__address) (((void *) elf) + entry->p_offset);
143
	ASSERT(ALIGN_UP(base, FRAME_SIZE) == base);
144
 
145
	if (page + PAGE_SIZE < ALIGN_UP(entry->p_vaddr + entry->p_filesz, PAGE_SIZE)) {
146
		if (entry->p_flags & PF_W) {
147
			/*
148
			 * Free the frame with the copy of writable segment data.
149
			 */
150
			frame_free(ADDR2PFN(frame));
151
		}
152
	} else {
153
		/*
154
		 * The frame is either anonymous memory or the mixed case (i.e. lower
155
		 * part is backed by the ELF image and the upper is anonymous).
156
		 * In any case, a frame needs to be freed.
157
		 */
158
		frame_free(ADDR2PFN(frame)); 
159
	}
160
}