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/util/util.h> 25 26 #include <ipc/cap_alloc.h> 27 #include <mem/memory_utils.h> 28 #include <systypes/base.h> 29 30 #include <stdio.h> 31 32 #include "dataspace_client.h" 33 #include "internal_pager.h" 34 #include "pager_object_server.h" 35 36 37 38 #define DEBUG 0 39 40 41 42 /* A simple system pager also acting as a region mapper. */ 43 44 InternalPager::InternalPager(address_t start, address_t end) 45 : ExecPager(start, end) 46 { 47 } 48 49 ipc_server_default_config_type InternalPager::config() 50 { 51 return config_PagerObject; 52 } 53 54 55 56 /* Close the pager. */ 57 58 void InternalPager::close() 59 { 60 printf("Internal pager closing...\n"); 61 62 /* Unmap all regions. */ 63 64 MappedRegions::iterator it; 65 66 for (it = _regions.begin(); it != _regions.end(); it++) 67 { 68 MappedRegion &r = it->second; 69 70 ipc_cap_free_um(r.ds); 71 } 72 } 73 74 75 76 /* Handle a general exception. */ 77 78 long InternalPager::exception(l4_exc_regs_t regs, l4_snd_fpage_t *region) 79 { 80 (void) region; 81 82 printf("InternalPager::exception(...) -> pfa = %lx, pc = %lx\n", l4_utcb_exc_pfa(®s), l4_utcb_exc_pc(®s)); 83 84 #if DEBUG 85 while (1) 86 l4_sleep_forever(); 87 #endif 88 89 return L4_EOK; 90 } 91 92 /* Handle a page fault using any configured regions. */ 93 94 long InternalPager::page_fault(l4_umword_t pfa, l4_umword_t pc, l4_snd_fpage_t *region) 95 { 96 l4_umword_t addr = pfa & ~7UL, flags = pfa & 7; 97 98 #if DEBUG 99 printf("page_fault(%lx, %lx) -> %lx (%lx) -> ", pfa, pc, addr, flags); 100 l4_thread_regs_t *regs = l4_utcb_tcr(); 101 printf("regs = %p\n; regs->user[0] = %lx\n", regs, regs->user[0]); 102 #endif 103 104 /* Find the first region whose start address is beyond the fault address, 105 testing if any immediately preceding region contains the fault address. */ 106 107 MappedRegions::iterator it = _regions.upper_bound(addr); 108 109 if (it != _regions.begin()) 110 it--; 111 else 112 { 113 printf("not mapped at %lx for pc %lx\n", addr, pc); 114 return -L4_ENOMEM; 115 } 116 117 MappedRegion &r = it->second; 118 119 if ((addr >= r.start) && (addr < r.start + r.size)) 120 { 121 address_t window_size = L4_PAGESIZE; 122 address_t window_base = trunc(addr, window_size); 123 address_t offset = addr - r.start; 124 address_t page_addr = trunc(addr, L4_PAGESIZE); 125 address_t hot_spot = page_addr - window_base; 126 127 /* Interact with the region's dataspace, specifying a receive window for a 128 map operation. Here, a single page is specified. */ 129 130 client_Dataspace dataspace(r.ds); 131 l4_snd_fpage_t rw_region = {0, l4_fpage(window_base, L4_PAGESHIFT, 0)}; 132 map_flags_t map_flags = map_flags_for_fault(flags); 133 134 #if DEBUG 135 printf("window_base = %lx; window_size = %lx\n", window_base, window_size); 136 printf("region = {%lx, {%lx, %d}}\n", rw_region.snd_base, l4_fpage_memaddr(rw_region.fpage), l4_fpage_size(rw_region.fpage)); 137 printf("map(%lx, %lx, %lx) -> %lx\n", offset, hot_spot, map_flags, r.ds); 138 #endif 139 140 long err = dataspace.map(offset, hot_spot, map_flags & r.flags, &rw_region); 141 142 /* Indicate an unspecified result, since the mapping should have taken 143 place. */ 144 145 *region = {0, l4_fpage_invalid()}; 146 147 return err; 148 } 149 150 #if DEBUG 151 printf("not mapped!\n"); 152 #endif 153 154 return -L4_ENOMEM; 155 } 156 157 /* Attach a region for provision when page faults occur. This is required in 158 the initialisation of a program by the C library which requires a region 159 mapper. */ 160 161 long InternalPager::attach(address_t *start, address_t size, map_flags_t flags, 162 l4_cap_idx_t ds, address_t offset, 163 unsigned char align) 164 { 165 long err = ExecPager::find(start, &size, flags, offset, align); 166 167 if (!err) 168 { 169 MappedRegion r(*start, size, flags & L4RE_DS_F_RIGHTS_MASK, ds); 170 add(r); 171 } 172 else 173 ipc_cap_free_um(ds); 174 175 return err; 176 } 177 178 /* vim: tabstop=2 expandtab shiftwidth=2 179 */