1 /* 2 * A system pager implementation residing in the same task as a program. 3 * 4 * Copyright (C) 2022, 2023, 2024 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <l4/re/env.h> 23 #include <l4/re/c/dataspace.h> 24 #include <l4/re/c/rm.h> 25 #include <l4/sys/err.h> 26 #include <l4/util/util.h> 27 28 #include <ipc/cap_alloc.h> 29 #include <mem/memory_utils.h> 30 #include <systypes/base.h> 31 32 #include <stdio.h> 33 34 #include "dataspace_client.h" 35 #include "internal_pager.h" 36 #include "mapped_region.h" 37 #include "pager_object_server.h" 38 39 40 41 #define DEBUG 0 42 43 44 45 /* A simple system pager also acting as a region mapper. */ 46 47 InternalPager::InternalPager(address_t start, address_t end) 48 : ExecPager(start, end) 49 { 50 } 51 52 ipc_server_default_config_type InternalPager::config() 53 { 54 return config_PagerObject; 55 } 56 57 58 59 /* Close the pager. */ 60 61 void InternalPager::close() 62 { 63 #if DEBUG 64 printf("Internal pager closing...\n"); 65 #endif 66 } 67 68 69 70 /* Handle a general exception. */ 71 72 long InternalPager::exception(l4_exc_regs_t regs, l4_snd_fpage_t *region) 73 { 74 (void) region; 75 76 printf("InternalPager::exception(...) -> pfa = %lx, pc = %lx\n", l4_utcb_exc_pfa(®s), l4_utcb_exc_pc(®s)); 77 78 while (1) 79 l4_sleep_forever(); 80 81 return L4_EOK; 82 } 83 84 /* Handle a page fault using any configured regions. */ 85 86 long InternalPager::page_fault(l4_umword_t pfa, l4_umword_t pc, l4_snd_fpage_t *region) 87 { 88 l4_umword_t addr = pfa & ~7UL, flags = pfa & 7; 89 90 #if DEBUG 91 printf("page_fault(%lx, %lx) -> %lx (%lx) -> ", pfa, pc, addr, flags); 92 l4_thread_regs_t *regs = l4_utcb_tcr(); 93 printf("regs = %p\n; regs->user[0] = %lx\n", regs, regs->user[0]); 94 #endif 95 96 /* Obtain a region supporting the fault address. */ 97 98 MemoryArea *r, *parent; 99 long err = _area.find(addr, &r, &parent); 100 101 if (!err) 102 { 103 address_t window_size = L4_PAGESIZE; 104 address_t window_base = trunc(addr, window_size); 105 address_t offset = addr - r->area_start(); 106 address_t page_addr = trunc(addr, L4_PAGESIZE); 107 address_t hot_spot = page_addr - window_base; 108 109 /* Interact with the region's dataspace, specifying a receive window for a 110 map operation. Here, a single page is specified. */ 111 112 client_Dataspace dataspace(r->dataspace()); 113 l4_snd_fpage_t rw_region = {0, l4_fpage(window_base, L4_PAGESHIFT, 0)}; 114 map_flags_t map_flags = map_flags_for_fault(flags); 115 116 #if DEBUG 117 printf("window_base = %lx; window_size = %lx\n", window_base, window_size); 118 printf("region = {%lx, {%lx, %d}}\n", rw_region.snd_base, l4_fpage_memaddr(rw_region.fpage), l4_fpage_size(rw_region.fpage)); 119 printf("map(%lx, %lx, %lx) -> %lx\n", offset, hot_spot, map_flags, r->dataspace()); 120 #endif 121 122 err = dataspace.map(offset, hot_spot, map_flags & r->flags(), &rw_region); 123 124 /* Indicate an unspecified result, since the mapping should have taken 125 place. */ 126 127 *region = {0, l4_fpage_invalid()}; 128 129 if (err) 130 printf("InternalPager: mapping error: %s\n", l4sys_errtostr(err)); 131 132 return err; 133 } 134 135 printf("InternalPager: not mapped at %lx for pc %lx\n", addr, pc); 136 137 return err; 138 } 139 140 /* Attach a region for provision when page faults occur. This is required in 141 the initialisation of a program by the C library which requires a region 142 mapper. */ 143 144 long InternalPager::attach(address_t *start, address_t size, rm_flags_t flags, 145 l4_cap_idx_t ds, rm_offset_t offset, 146 unsigned char align) 147 { 148 /* NOTE: The offset apparently indicates the offset within the dataspace of 149 the start of the region. */ 150 151 (void) offset; 152 153 MemoryArea *area; 154 long err = ExecPager::find(start, &size, flags, align, &area); 155 156 if (!err) 157 { 158 MappedRegion r(*start, *start + size, flags & L4RE_DS_F_RIGHTS_MASK, ds); 159 area->add(r); 160 } 161 else 162 ipc_cap_free_um(ds); 163 164 return err; 165 } 166 167 long InternalPager::detach(address_t addr, address_t size, rm_flags_t flags, 168 address_t *start, address_t *rsize, l4_cap_idx_t *ds) 169 { 170 (void) size; (void) flags; 171 172 /* Obtain the region supporting the given address. 173 NOTE: All regions within the given range should be detached. */ 174 175 MemoryArea *r, *parent; 176 long err = _area.find(addr, &r, &parent); 177 178 if (!err) 179 { 180 *start = r->area_start(); 181 *rsize = r->area_end() - r->area_start(); 182 *ds = r->dataspace(); 183 parent->remove(*r); 184 } 185 186 return err; 187 } 188 189 long InternalPager::reserve_area(address_t *start, address_t size, 190 rm_flags_t flags, unsigned char align) 191 { 192 MemoryArea *area; 193 long err = ExecPager::find(start, &size, flags, align, &area); 194 195 if (!err) 196 { 197 /* Add an object acting like a region but without any associated 198 dataspace. */ 199 200 if (flags & L4RE_RM_F_RESERVED) 201 { 202 ReservedMemoryArea r(*start, *start + size); 203 area->add(r); 204 } 205 206 /* Add an object permitting regions to be added within it. */ 207 208 else 209 { 210 AvailableMemoryArea r(*start, *start + size); 211 area->add(r); 212 } 213 } 214 215 return err; 216 } 217 218 /* vim: tabstop=2 expandtab shiftwidth=2 219 */