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