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