1 /* 2 * A system pager implementation residing in a separate task. 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/util/util.h> 24 25 #include <ipc/mem_ipc.h> 26 #include <mem/memory_utils.h> 27 28 #include <stdio.h> 29 30 #include "external_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 ExternalPager::ExternalPager(address_t start, address_t end) 41 : ExecPager(start, end) 42 { 43 } 44 45 /* Handle a general exception. */ 46 47 long ExternalPager::exception(l4_exc_regs_t regs, l4_snd_fpage_t *region) 48 { 49 (void) region; 50 51 printf("ExternalPager::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 ExternalPager::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 #endif 70 71 MappedRegions::iterator it = _regions.upper_bound(addr); 72 73 if (it != _regions.begin()) 74 it--; 75 else 76 { 77 printf("not mapped at %lx for pc %lx\n", addr, pc); 78 return -L4_ENOMEM; 79 } 80 81 MappedRegion &r = it->second; 82 83 if ((addr >= r.start) && (addr < r.start + r.size)) 84 { 85 l4_addr_t page_addr = trunc(addr, L4_PAGESIZE); 86 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); 87 88 if (!map_flags) 89 map_flags = L4RE_DS_F_R; 90 91 region->fpage = l4_fpage(r.ds_start + (page_addr - r.start), L4_PAGESHIFT, map_flags & r.flags); 92 region->snd_base = page_addr; 93 94 #if DEBUG 95 printf("%lx...%lx from %lx...%lx offset %lx size %d rights %x ds %lx\n", 96 r.start, region->snd_base, 97 r.ds_start, l4_fpage_memaddr(region->fpage), 98 addr - r.start, 99 l4_fpage_size(region->fpage), 100 l4_fpage_rights(region->fpage), 101 r.ds); 102 103 printf("%lx -> ", addr); 104 105 for (unsigned int i = 0; i < sizeof(l4_umword_t); i++) 106 printf("%02x", *((unsigned char *)(r.ds_start + (addr - r.start) + i))); 107 108 printf("\n"); 109 #endif 110 111 if (r.flags & L4RE_RM_F_W) 112 l4_touch_rw((const void *) (r.ds_start + (page_addr - r.start)), L4_PAGESIZE); 113 else 114 l4_touch_ro((const void *) (r.ds_start + (page_addr - r.start)), L4_PAGESIZE); 115 116 return L4_EOK; 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 ExternalPager::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 /* Attach the provided dataspace. 139 NOTE: This is only done in this implementation to support the paging 140 mechanism. In a region mapper residing within the actual task, the 141 dataspace's map operation would be invoked to obtain mappings. */ 142 143 l4_addr_t ds_start; 144 err = ipc_attach_dataspace(ds, size, (void **) &ds_start); 145 146 if (err) 147 return err; 148 149 l4_touch_rw((const void *) ds_start, size); 150 151 MappedRegion r(*start, size, flags & L4RE_DS_F_RIGHTS_MASK, ds, ds_start); 152 add(r); 153 } 154 155 return err; 156 } 157 158 /* vim: tabstop=2 expandtab shiftwidth=2 159 */