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/sys/err.h> 24 #include <l4/util/util.h> 25 26 #include <ipc/mem_ipc.h> 27 #include <mem/memory_utils.h> 28 29 #include <stdio.h> 30 #include <string.h> 31 32 #include "external_pager.h" 33 34 35 36 /* A simple system pager also acting as a region mapper. */ 37 38 /* Add a region to the pager. */ 39 40 void ExternalPager::add(MappedRegion ®ion) 41 { 42 _regions[region.map_start] = region; 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("exception(...) -> pfa = %lx, pc = %lx\n", l4_utcb_exc_pfa(®s), l4_utcb_exc_pc(®s)); 52 53 #if 0 54 printf("r15 = %lx\n", regs.r15); 55 printf("r14 = %lx\n", regs.r14); 56 printf("r13 = %lx\n", regs.r13); 57 printf("r12 = %lx\n", regs.r12); 58 printf("r11 = %lx\n", regs.r11); 59 printf("r10 = %lx\n", regs.r10); 60 printf("r9 = %lx\n", regs.r9); 61 printf("r8 = %lx\n", regs.r8); 62 printf("rdi = %lx\n", regs.rdi); 63 printf("rsi = %lx\n", regs.rsi); 64 printf("rbp = %lx\n", regs.rbp); 65 printf("pfa = %lx\n", regs.pfa); 66 printf("rbx = %lx\n", regs.rbx); 67 printf("rdx = %lx\n", regs.rdx); 68 printf("rcx = %lx\n", regs.rcx); 69 printf("rax = %lx\n", regs.rax); 70 printf("trapno = %lx\n", regs.trapno); 71 printf("err = %lx\n", regs.err); 72 printf("ip = %lx\n", regs.ip); 73 printf("flags = %lx\n", regs.flags); 74 printf("sp = %lx\n", regs.sp); 75 printf("ss = %lx\n", regs.ss); 76 printf("fs_base = %lx\n", regs.fs_base); 77 printf("gs_base = %lx\n", regs.gs_base); 78 #endif 79 80 while (1) 81 l4_sleep_forever(); 82 83 return L4_EOK; 84 } 85 86 #define DEBUG 0 87 88 /* Handle a page fault using any configured regions. */ 89 90 long ExternalPager::page_fault(l4_umword_t pfa, l4_umword_t pc, l4_snd_fpage_t *region) 91 { 92 l4_umword_t addr = pfa & ~7UL, flags = pfa & 7; 93 94 #if DEBUG 95 printf("page_fault(%lx, %lx) -> %lx (%lx) -> ", pfa, pc, addr, flags); 96 #endif 97 98 MappedRegions::iterator it = _regions.upper_bound(addr); 99 100 if (it != _regions.begin()) 101 it--; 102 else 103 { 104 printf("not mapped!\n"); 105 return -L4_ENOMEM; 106 } 107 108 MappedRegion &r = it->second; 109 110 if ((addr >= r.map_start) && (addr < r.map_start + r.size)) 111 { 112 l4_addr_t page_addr = trunc(addr, L4_PAGESIZE); 113 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); 114 115 if (!map_flags) 116 map_flags = L4RE_DS_F_R; 117 118 region->fpage = l4_fpage(r.start + (page_addr - r.map_start), L4_PAGESHIFT, map_flags & r.flags); 119 region->snd_base = page_addr; 120 121 #if DEBUG 122 printf("%lx...%lx from %lx...%lx offset %lx size %d rights %x ds %lx\n", 123 r.map_start, region->snd_base, 124 r.start, l4_fpage_memaddr(region->fpage), 125 addr - r.map_start, 126 l4_fpage_size(region->fpage), 127 l4_fpage_rights(region->fpage), 128 r.ds); 129 130 printf("%lx -> ", addr); 131 132 for (unsigned int i = 0; i < sizeof(l4_umword_t); i++) 133 printf("%02x", *((unsigned char *)(r.start + (addr - r.map_start) + i))); 134 135 printf("\n"); 136 #endif 137 138 if (r.flags & L4RE_RM_F_W) 139 l4_touch_rw((const void *) (r.start + (page_addr - r.map_start)), L4_PAGESIZE); 140 else 141 l4_touch_ro((const void *) (r.start + (page_addr - r.map_start)), L4_PAGESIZE); 142 143 return L4_EOK; 144 } 145 146 #if DEBUG 147 printf("not mapped!\n"); 148 #endif 149 150 return -L4_ENOMEM; 151 } 152 153 /* Attach a region for provision when page faults occur. This is required in 154 the initialisation of a program by the C library which requires a region 155 mapper. */ 156 157 long ExternalPager::attach(address_t *start, address_t size, map_flags_t flags, 158 l4_cap_idx_t ds, address_t offset, 159 unsigned char align) 160 { 161 #if DEBUG 162 printf("attach(%lx, %ld, %lx, ..., %lx, %d)\n", *start, size, flags, offset, align); 163 #endif 164 165 if (align < L4_PAGESHIFT) 166 align = L4_PAGESHIFT; 167 168 address_t increment = 1UL << align; 169 address_t region_size = round(size, increment); 170 171 /* Either attempt to find an address for the specified region, starting from 172 any indicated address. */ 173 174 if (flags & L4RE_RM_F_SEARCH_ADDR) 175 { 176 address_t region_start = trunc(*start, increment); 177 MappedRegions::iterator it = _regions.upper_bound(*start); 178 179 if (!region_start) 180 region_start += increment; 181 182 #if DEBUG 183 printf("-> search from %lx -> %lx...\n", *start, region_start); 184 #endif 185 186 /* Before last known region. */ 187 188 while (it != _regions.end()) 189 { 190 MappedRegions::iterator next = it; 191 MappedRegion &r = it->second; 192 address_t start_limit; 193 address_t end_limit = r.map_start; 194 195 /* Consider any preceding region. If no such region exists, choose an 196 address at the start of memory. */ 197 198 if (it == _regions.begin()) 199 start_limit = L4_PAGESIZE; 200 else 201 { 202 it--; 203 MappedRegion &pr = it->second; 204 start_limit = pr.map_start + pr.size; 205 it = next; 206 } 207 208 /* Test against the limits. */ 209 210 if (region_start < start_limit) 211 region_start = round(start_limit, increment); 212 213 /* Investigate subsequent regions if not enough space exists between the 214 preceding region (or start of memory) and the current region. */ 215 216 if ((region_start + region_size) > end_limit) 217 { 218 it++; 219 if (it == _regions.end()) 220 return -L4_ENOMEM; 221 } 222 else 223 break; 224 } 225 226 /* Attach the provided dataspace. 227 NOTE: This is only done in this implementation to support the paging 228 mechanism. In a region mapper residing within the actual task, the 229 dataspace's map operation would be invoked to obtain mappings. */ 230 231 l4_addr_t ds_start; 232 233 long err = ipc_attach_dataspace(ds, size, (void **) &ds_start); 234 235 if (err) 236 return err; 237 238 l4_touch_rw((const void *) ds_start, size); 239 240 #if DEBUG 241 printf("-> added region at %lx size %ld (%d) from %lx ds %lx\n", region_start, region_size, page_order(region_size), ds_start, ds); 242 #endif 243 244 MappedRegion r(ds_start, region_size, flags & L4RE_DS_F_RIGHTS_MASK, region_start, ds); 245 246 add(r); 247 248 *start = region_start; 249 return L4_EOK; 250 } 251 252 /* Or attempt to add the specified region at a specific address. */ 253 254 else 255 { 256 // NOTE: To be implemented. 257 258 #if DEBUG 259 printf("-> region of size %ld (%d) not added!\n", region_size, page_order(region_size)); 260 #endif 261 262 return -L4_ENOMEM; 263 } 264 } 265 266 /* vim: tabstop=2 expandtab shiftwidth=2 267 */