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