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