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