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