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 printf("Free %lx\n", r.ds); 71 ipc_cap_free_um(r.ds); 72 } 73 } 74 75 76 77 /* Handle a general exception. */ 78 79 long InternalPager::exception(l4_exc_regs_t regs, l4_snd_fpage_t *region) 80 { 81 (void) region; 82 83 printf("InternalPager::exception(...) -> pfa = %lx, pc = %lx\n", l4_utcb_exc_pfa(®s), l4_utcb_exc_pc(®s)); 84 85 #if DEBUG 86 while (1) 87 l4_sleep_forever(); 88 #endif 89 90 return L4_EOK; 91 } 92 93 /* Handle a page fault using any configured regions. */ 94 95 long InternalPager::page_fault(l4_umword_t pfa, l4_umword_t pc, l4_snd_fpage_t *region) 96 { 97 l4_umword_t addr = pfa & ~7UL, flags = pfa & 7; 98 99 #if DEBUG 100 printf("page_fault(%lx, %lx) -> %lx (%lx) -> ", pfa, pc, addr, flags); 101 l4_thread_regs_t *regs = l4_utcb_tcr(); 102 printf("regs = %p\n; regs->user[0] = %lx\n", regs, regs->user[0]); 103 #endif 104 105 /* Find the first region whose start address is beyond the fault address, 106 testing if any immediately preceding region contains the fault address. */ 107 108 MappedRegions::iterator it = _regions.upper_bound(addr); 109 110 if (it != _regions.begin()) 111 it--; 112 else 113 { 114 printf("not mapped at %lx for pc %lx\n", addr, pc); 115 return -L4_ENOMEM; 116 } 117 118 MappedRegion &r = it->second; 119 120 if ((addr >= r.start) && (addr < r.start + r.size)) 121 { 122 address_t window_size = L4_PAGESIZE; 123 address_t window_base = trunc(addr, window_size); 124 address_t offset = addr - r.start; 125 address_t page_addr = trunc(addr, L4_PAGESIZE); 126 address_t hot_spot = page_addr - window_base; 127 128 /* Interact with the region's dataspace, specifying a receive window for a 129 map operation. Here, a single page is specified. */ 130 131 client_Dataspace dataspace(r.ds); 132 l4_snd_fpage_t rw_region = {0, l4_fpage(window_base, L4_PAGESHIFT, 0)}; 133 map_flags_t map_flags = map_flags_for_fault(flags); 134 135 #if DEBUG 136 printf("window_base = %lx; window_size = %lx\n", window_base, window_size); 137 printf("region = {%lx, {%lx, %d}}\n", rw_region.snd_base, l4_fpage_memaddr(rw_region.fpage), l4_fpage_size(rw_region.fpage)); 138 printf("map(%lx, %lx, %lx) -> %lx\n", offset, hot_spot, map_flags, r.ds); 139 #endif 140 141 long err = dataspace.map(offset, hot_spot, map_flags & r.flags, &rw_region); 142 143 /* Indicate an unspecified result, since the mapping should have taken 144 place. */ 145 146 *region = {0, l4_fpage_invalid()}; 147 148 return err; 149 } 150 151 #if DEBUG 152 printf("not mapped!\n"); 153 #endif 154 155 return -L4_ENOMEM; 156 } 157 158 /* Attach a region for provision when page faults occur. This is required in 159 the initialisation of a program by the C library which requires a region 160 mapper. */ 161 162 long InternalPager::attach(address_t *start, address_t size, map_flags_t flags, 163 l4_cap_idx_t ds, address_t offset, 164 unsigned char align) 165 { 166 long err = ExecPager::find(start, &size, flags, offset, align); 167 168 if (!err) 169 { 170 MappedRegion r(*start, size, flags & L4RE_DS_F_RIGHTS_MASK, ds); 171 add(r); 172 } 173 174 return err; 175 } 176 177 /* vim: tabstop=2 expandtab shiftwidth=2 178 */