1 /* 2 * A system pager implementation residing in a separate task. 3 * 4 * Copyright (C) 2022, 2023 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/task.h> 24 #include <l4/util/util.h> 25 26 #include <ipc/cap_alloc.h> 27 #include <ipc/mem_ipc.h> 28 #include <mem/memory_utils.h> 29 #include <systypes/base.h> 30 31 #include <stdio.h> 32 33 #include "external_pager.h" 34 #include "parent_pager_object_server.h" 35 36 37 38 #define DEBUG 0 39 40 41 42 /* A simple system pager also acting as a region mapper. */ 43 44 ExternalPager::ExternalPager(address_t start, address_t end) 45 : ExecPager(start, end), NotificationSupport() 46 { 47 } 48 49 ipc_server_default_config_type ExternalPager::config() 50 { 51 return config_ParentPagerObject; 52 } 53 54 55 56 /* Close the pager. */ 57 58 void ExternalPager::close() 59 { 60 printf("Pager closing...\n"); 61 62 /* Unmap all regions. */ 63 64 MappedRegions::iterator it; 65 66 for (it = _regions.begin(); it != _regions.end(); it++) 67 { 68 MappedRegion &r = it->second; 69 70 ipc_detach_dataspace((void *) r.ds_start); 71 printf("Free %lx\n", r.ds); 72 ipc_cap_free_um(r.ds); 73 } 74 75 if (l4_is_valid_cap(_task)) 76 { 77 ipc_cap_free_um(_task); 78 _task = L4_INVALID_CAP; 79 } 80 } 81 82 83 84 /* Manage the task and thread capabilities. */ 85 86 void ExternalPager::add_thread(l4_cap_idx_t cap, l4_cap_idx_t rcap) 87 { 88 _threads.push_back(cap); 89 _rthreads.push_back(rcap); 90 } 91 92 void ExternalPager::set_gate(l4_cap_idx_t cap) 93 { 94 _ipc_gate = cap; 95 } 96 97 void ExternalPager::set_pager(l4_cap_idx_t cap, l4_cap_idx_t rcap) 98 { 99 _pager = cap; 100 _rpager = rcap; 101 } 102 103 void ExternalPager::set_parent(l4_cap_idx_t cap, l4_cap_idx_t rcap) 104 { 105 _parent = cap; 106 _rparent = rcap; 107 } 108 109 void ExternalPager::set_task(l4_cap_idx_t cap) 110 { 111 _task = cap; 112 } 113 114 115 116 /* Handle a general exception. */ 117 118 long ExternalPager::exception(l4_exc_regs_t regs, l4_snd_fpage_t *region) 119 { 120 (void) region; 121 122 printf("ExternalPager::exception(...) -> pfa = %lx, pc = %lx\n", l4_utcb_exc_pfa(®s), l4_utcb_exc_pc(®s)); 123 124 #if DEBUG 125 while (1) 126 l4_sleep_forever(); 127 #endif 128 129 return L4_EOK; 130 } 131 132 /* Handle a page fault using any configured regions. */ 133 134 long ExternalPager::page_fault(l4_umword_t pfa, l4_umword_t pc, l4_snd_fpage_t *region) 135 { 136 l4_umword_t addr = pfa & ~7UL, flags = pfa & 7; 137 138 #if DEBUG 139 printf("page_fault(%lx, %lx) -> %lx (%lx) -> ", pfa, pc, addr, flags); 140 #endif 141 142 /* Find the first region whose start address is beyond the fault address, 143 testing if any immediately preceding region contains the fault address. */ 144 145 MappedRegions::iterator it = _regions.upper_bound(addr); 146 147 if (it != _regions.begin()) 148 it--; 149 else 150 { 151 printf("not mapped at %lx for pc %lx\n", addr, pc); 152 return -L4_ENOMEM; 153 } 154 155 /* Obtain the region and test if it contains the fault address. */ 156 157 MappedRegion &r = it->second; 158 159 if ((addr >= r.start) && (addr < r.start + r.size)) 160 { 161 l4_addr_t page_addr = trunc(addr, L4_PAGESIZE); 162 map_flags_t map_flags = map_flags_for_fault(flags); 163 164 region->fpage = l4_fpage(r.ds_start + (page_addr - r.start), L4_PAGESHIFT, map_flags & r.flags); 165 region->snd_base = page_addr; 166 167 #if DEBUG 168 printf("%lx...%lx from %lx...%lx offset %lx size %d rights %x ds %lx\n", 169 r.start, region->snd_base, 170 r.ds_start, l4_fpage_memaddr(region->fpage), 171 addr - r.start, 172 l4_fpage_size(region->fpage), 173 l4_fpage_rights(region->fpage), 174 r.ds); 175 176 printf("%lx -> ", addr); 177 178 for (unsigned int i = 0; i < sizeof(l4_umword_t); i++) 179 printf("%02x", *((unsigned char *)(r.ds_start + (addr - r.start) + i))); 180 181 printf("\n"); 182 #endif 183 184 if (r.flags & L4RE_RM_F_W) 185 l4_touch_rw((const void *) (r.ds_start + (page_addr - r.start)), L4_PAGESIZE); 186 else 187 l4_touch_ro((const void *) (r.ds_start + (page_addr - r.start)), L4_PAGESIZE); 188 189 return L4_EOK; 190 } 191 192 #if DEBUG 193 printf("not mapped!\n"); 194 #endif 195 196 return -L4_ENOMEM; 197 } 198 199 /* Attach a region for provision when page faults occur. This is required in 200 the initialisation of a program by the C library which requires a region 201 mapper. */ 202 203 long ExternalPager::attach(address_t *start, address_t size, map_flags_t flags, 204 l4_cap_idx_t ds, address_t offset, 205 unsigned char align) 206 { 207 long err = ExecPager::find(start, &size, flags, offset, align); 208 209 /* Without an error, attach the dataspace. */ 210 211 if (!err) 212 { 213 /* Attach the provided dataspace within this task. 214 215 This is only done in this implementation to support the paging 216 mechanism. In a region mapper residing within the actual task, the 217 dataspace's map operation would be invoked to obtain mappings. */ 218 219 l4_addr_t ds_start; 220 err = ipc_attach_dataspace(ds, size, (void **) &ds_start); 221 222 if (err) 223 return err; 224 225 l4_touch_rw((const void *) ds_start, size); 226 227 MappedRegion r(*start, size, flags & L4RE_DS_F_RIGHTS_MASK, ds, ds_start); 228 add(r); 229 } 230 231 /* Discard the imported dataspace if its region cannot be accommodated. */ 232 233 else 234 ipc_cap_free_um(ds); 235 236 return err; 237 } 238 239 240 241 /* Receive signals from a task. */ 242 243 long ExternalPager::signal(unsigned long sig, unsigned long val) 244 { 245 notify_all(NOTIFY_TASK_SIGNAL, (notify_values_t) {sig, val}); 246 247 /* Handle the termination event. */ 248 249 if (sig == 0) 250 { 251 /* Once the program exits, the IPC gate connecting the program with its 252 internal pager can be released. */ 253 254 if (l4_is_valid_cap(_ipc_gate)) 255 { 256 l4_task_unmap(L4RE_THIS_TASK_CAP, l4_obj_fpage(_ipc_gate, 0, L4_CAP_FPAGE_RWSD), L4_FP_ALL_SPACES); 257 _ipc_gate = L4_INVALID_CAP; 258 } 259 260 /* Upon exit of the program's internal pager, release the capabilities 261 associated with this server. */ 262 263 else if (l4_is_valid_cap(_task)) 264 { 265 /* NOTE: Capability indexes to be obtained from the process creation 266 activity. */ 267 268 /* Log. */ 269 270 l4_task_unmap(_task, l4_obj_fpage(0x05UL << L4_CAP_SHIFT, 0, L4_CAP_FPAGE_RWSD), L4_FP_ALL_SPACES); 271 272 /* Parent and pager/region mapper. */ 273 274 l4_task_unmap(_task, l4_obj_fpage(_rparent, 0, L4_CAP_FPAGE_RWSD), L4_FP_ALL_SPACES); 275 l4_task_unmap(_task, l4_obj_fpage(_rpager, 0, L4_CAP_FPAGE_RWSD), L4_FP_ALL_SPACES); 276 277 /* Threads. For some reason, these cannot be released by the process, so 278 they are also unmapped on its behalf. */ 279 280 std::vector<l4_cap_idx_t>::iterator it; 281 282 for (it = _threads.begin(); it != _threads.end(); it++) 283 ipc_cap_free_um(*it); 284 285 for (it = _rthreads.begin(); it != _rthreads.end(); it++) 286 l4_task_unmap(_task, l4_obj_fpage(*it, 0, L4_CAP_FPAGE_RWSD), L4_FP_ALL_SPACES); 287 } 288 } 289 290 return L4_EOK; 291 } 292 293 294 295 /* Subscribe to notifications. */ 296 297 long ExternalPager::subscribe(l4_cap_idx_t notifier, notify_flags_t flags) 298 { 299 return NotificationSupport::subscribe(notifier, flags); 300 } 301 302 /* Unsubscribe from notifications. */ 303 304 long ExternalPager::unsubscribe(l4_cap_idx_t notifier) 305 { 306 return NotificationSupport::unsubscribe(notifier); 307 } 308 309 /* vim: tabstop=2 expandtab shiftwidth=2 310 */