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