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