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